Hi,<br><br>I'm using GHC API to dynamically load some module, and evaluate it; and later change the content of the module, and re-evaluate it. But I found unless I delete the object file created by previous compilation, the module seems not reloaded. I have set ghcLink = LinkInMemory as an older post suggested<br>
<br>To illustrate what I'm saying, here is a piece of code (sorry for any naivety in the code, new to Haskell too)<br><br>import System.IO (IOMode(..),hClose,hPutStr,openFile)<br>import Directory (removeFile)<br>import GHC<br>
import GHC.Paths<br>import DynFlags<br>import Unsafe.Coerce<br> <br>src_file = "Target.hs" <br>obj_file = "Target.o"<br> <br>main = do<br> writeTarget "arg"<br> func0 <- compileTarget<br>
putStrLn $ show $ func0 2<br><br> writeTarget "arg*2"<br> func1 <- compileTarget<br> putStrLn $ show $ func1 2<br> <br>writeTarget input = do<br>-- removeFile obj_file `catch` (const $ return ()) -- uncomment this line to have correct results<br>
h <- openFile src_file WriteMode<br> hPutStr h "module Target (Target.target) where\n"<br> hPutStr h "target::Double -> Double\n"<br> hPutStr h "target arg = \n "<br> hPutStr h input<br>
hClose h<br><br>compileTarget =<br> defaultErrorHandler defaultDynFlags $ do<br> func <- runGhc (Just libdir) $ do<br> -- setup dynflags<br> dflags <- getSessionDynFlags<br> setSessionDynFlags dflags { ghcLink = LinkInMemory }<br>
<br> -- load target module<br> target <- guessTarget src_file Nothing<br> setTargets [target]<br> r <- load LoadAllTargets<br> case r of<br> Failed -> error "Compilation failed"<br>
Succeeded -> do<br> m <- findModule (mkModuleName "Target") Nothing<br> -- set context and compile<br> setContext [] [m]<br> value <- compileExpr ("Target.target")<br>
do<br> let value' = (unsafeCoerce value) :: Double -> Double<br> return value'<br> return func<br><br><br>The code basically write to a Haskell source file twice with different content, and hoping to get different results, but unless I uncomment the line with removeFile, the output of 2 runs are the same; using 'touch' to touch the source file being written between 2 runs also gives the correct results. So maybe caused by some caching mechanism?<br>
<br>I'm using GHC 6.12.1 in Ubuntu 10.04. I have this workaround of deleting the obj file, but I'm wondering the "correct" way of doing it. Did some search on GHC API, but never got something relevant.<br>
<br>Thanks,<br>Hongmin<br>