Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to use data and newtype? #171

Open
Archaversine opened this issue Mar 11, 2024 · 2 comments
Open

Ability to use data and newtype? #171

Archaversine opened this issue Mar 11, 2024 · 2 comments

Comments

@Archaversine
Copy link

I may be missing something, but is it possible to declare new datatypes? For example, I tried runStmt "data MyBool = MyTrue | MyFalse", but I get:

parse error on input 'data'

Is there any way to do this or any work around to do this?

@gelisam
Copy link
Contributor

gelisam commented Mar 12, 2024

Alas, runStmt does not currently support declarations, as it directly delegates to GHC's execStmt, which doesn't support them either.

ghci is smarter. it first parses the input line to check if execStmt would accept it, and if not, it assumes the line is a declaration. in that case, it parses the line as a module instead of a statement, and then loads the module.

ghci doesn't support parsing a string into a module yet, so you'll have to do it the old fashioned way, by loading it from a file:

import Language.Haskell.Interpreter

-- |
-- >>> main
-- Right 6
main :: IO ()
main = do
  writeFile "MyModule.hs"
    "module MyModule where\n\
    \data MyBool = MyTrue | MyFalse deriving Show\n\
    \"
  r <- runInterpreter $ do
    loadModules ["MyModule.hs"]
    setImports ["Prelude", "MyModule"]
    interpret "length (show MyTrue)" (as :: Int)
  print r

hint's implementation actually has some machinery called "phantom modules" for creating and loading anonymous modules by saving them to temporary files, so I don't think it would be too hard to automate the above trick and to expose it as a function which loads a module defined by a string rather than by a filename. But now that ghc supports loading modules from strings, it would be more elegant to get rid of phantom modules and to use that functionality directly. And then to do the same thing ghci does for loading declarations.

@Archaversine
Copy link
Author

After looking at the GHC module I see what you mean. Would it be possible to add something like the following?

runDecls :: MonadInterpreter m => String -> m ()
runDecls s = runGhc $ void $ GHC.runDecls s

Obviously it would need to be wrapped in mayFail but other than that it seems to work as expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants