ghc-api

Is it possible to generate and run TemplateHaskell generated code at runtime?

拟墨画扇 提交于 2019-12-30 03:22:12
问题 Is it possible to generate and run TemplateHaskell generated code at runtime? Using C, at runtime, I can: create the source code of a function, call out to gcc to compile it to a .so (linux) (or use llvm, etc.), load the .so and call the function. Is a similar thing possible with Template Haskell? 回答1: Yes, it's possible. The GHC API will compile Template Haskell. A proof-of-concept is available at https://github.com/JohnLato/meta-th, which, although not very sophisticated, shows one general

Haskell GHC Dynamic Compliation Only works on first compile

柔情痞子 提交于 2019-12-22 04:58:20
问题 Following the GHC tutorial posted here and alterations to this code following the advice in a previous stack overflow question I asked, I have created a program which is able to compile and run a module in Test.hs with a function print to print a string to the screen: import GHC import GHC.Paths import DynFlags import Unsafe.Coerce main :: IO () main = defaultErrorHandler defaultLogAction $ do func <- runGhc (Just libdir) $ do dflags <- getSessionDynFlags setSessionDynFlags dflags target <-

Dynamically loading compiled Haskell module - GHC 7.6

♀尐吖头ヾ 提交于 2019-12-20 18:29:49
问题 I'm trying to dynamically compile and load Haskell modules using GHC API. I understand the API fluctuates quite a bit from on one version to another so I'm specifically talking about GHC 7.6.*. I have tried running the same code on MacOS and Linux. In both cases the Plugin module compiles fine but gives the following error on load: Cannot add module Plugin to context: not interpreted The problem is similar to the one in this where the module would only load if it was compiled in the same run

Why cannot top level module be set to main in Hint

做~自己de王妃 提交于 2019-12-11 03:13:06
问题 Why cannot top level module be set to "Main" in Hint (Language.Haskell.Interpreter)? Allow me to demonstrate: module Main where import Language.Haskell.Interpreter import Control.Monad main = do res <- runInterpreter (test "test") case res of Left e -> putStrLn (show e) Right t -> putStrLn (show t) return () test :: String -> Interpreter () test mname = do loadModules [mname ++ ".hs"] setTopLevelModules ["Main"] Will result in: NotAllowed "These modules are not interpreted:\nMain\n" 回答1: As

Dynamic loading of Haskell abstract syntax expression

China☆狼群 提交于 2019-12-10 05:10:15
问题 Can we use GHC API or something else to load not text source modules, but AST expressions, similar to haskell-src-exts Exp type? This way we could save time for code generation and parsing. 回答1: I don't think the GHC API exposes an AST interface (could be wrong though), but Template Haskell does. If you build expressions using the Language.Haskell.TH Exp structure, you can create functions/declarations and make use of them by the $(someTHFunction) syntax. A fairly major caveat is that TH only

get the renamed (with fully qualified import) haskell AST using ghc api

左心房为你撑大大i 提交于 2019-12-08 04:35:54
问题 I can get the following ghc compiler working using ghc api to compile a single file.I would like to get the renamed AST of haskell source (AST with all function calls fully qualified) ghcmake = defaultErrorHandler defaultFatalMessager defaultFlushOut $ do runGhc (Just GP.libdir) $ do dflags <- getSessionDynFlags setSessionDynFlags dflags target <- guessTarget targetFile Nothing setTargets [target] load LoadAllTargets From this simple demo,the renaming process ought be done in "load" function.

How to handle “panic: the impossible happened” and continue in Haskell

穿精又带淫゛_ 提交于 2019-12-07 01:48:55
问题 I have the following code that uses the GHC API to load modules and get the type of an expression: typeObjects :: [String] -> [String] -> IO [Type] typeObjects modules objects = do defaultErrorHandler defaultDynFlags $ do runGhc (Just libdir) $ do dflags <- getSessionDynFlags setSessionDynFlags dflags targets <- mapM ((flip guessTarget) Nothing) modules setTargets targets result <- load LoadAllTargets case result of Failed -> error "Compilation failed" Succeeded -> do m <- mapM (((flip

Dynamic loading of Haskell abstract syntax expression

五迷三道 提交于 2019-12-05 10:00:18
Can we use GHC API or something else to load not text source modules, but AST expressions, similar to haskell-src-exts Exp type ? This way we could save time for code generation and parsing. I don't think the GHC API exposes an AST interface (could be wrong though), but Template Haskell does. If you build expressions using the Language.Haskell.TH Exp structure, you can create functions/declarations and make use of them by the $(someTHFunction) syntax. A fairly major caveat is that TH only runs at compile time, so you would need to pre-generate everything. If you want to use TH at run-time, I

Haskell GHC Dynamic Compliation Only works on first compile

谁都会走 提交于 2019-12-05 07:05:33
Following the GHC tutorial posted here and alterations to this code following the advice in a previous stack overflow question I asked , I have created a program which is able to compile and run a module in Test.hs with a function print to print a string to the screen: import GHC import GHC.Paths import DynFlags import Unsafe.Coerce main :: IO () main = defaultErrorHandler defaultLogAction $ do func <- runGhc (Just libdir) $ do dflags <- getSessionDynFlags setSessionDynFlags dflags target <- guessTarget "Test.hs" Nothing addTarget target r <- load LoadAllTargets case r of Failed -> error

How to handle “panic: the impossible happened” and continue in Haskell

怎甘沉沦 提交于 2019-12-05 05:01:18
I have the following code that uses the GHC API to load modules and get the type of an expression: typeObjects :: [String] -> [String] -> IO [Type] typeObjects modules objects = do defaultErrorHandler defaultDynFlags $ do runGhc (Just libdir) $ do dflags <- getSessionDynFlags setSessionDynFlags dflags targets <- mapM ((flip guessTarget) Nothing) modules setTargets targets result <- load LoadAllTargets case result of Failed -> error "Compilation failed" Succeeded -> do m <- mapM (((flip findModule) Nothing) . mkModuleName) modules setContext m [] values <- mapM exprType objects return values If