I'm a Haskell beginner, and I'm trying to use the dynamic loading in the 'plugins' package. I'm kind of lost. Here is a minimal program with two files.
Main.hs:
module Main (main) where
import System.Plugins
main :: IO ()
main = do
putStrLn "Loading"
mv <- dynload "Plug.o" [] [] "thing" -- also try 'load' here
putStrLn "Loaded"
case mv of
LoadFailure msgs -> putStrLn "fail" >> print msgs
LoadSuccess _ v -> putStrLn "success" >> print (v::Integer)
And Plug.hs:
module Plug (thing) where
thing :: Integer
thing = 1234000
I compile Plug with ghc -c Plug.hs
which produces Plug.o. I then compile Main.hs with ghc -o Main Main.hs
, and run Main. I also try replacing load
with dynload
, and running with runhaskell
. Only one of these four combinations works. What am I doing wrong?
- with
dynload
- compiled → prints "Loaded", then seg faults
- runhaskell → prints "Loading", then "Main.hs: Prelude.undefined"
- with
load
- compiled → successful, prints the Integer
- runhaskell → prints "Loading", hangs for 5-10 seconds, disappears
I'm on Mac OS X. GHC version 7.0.2. What am I doing wrong?
thanks,
Rob
Update
I can fix the compiled dynload
by changing Plug.hs to the following...
module Plug (thing) where
import Data.Dynamic
thing :: Dynamic
thing = toDyn (1234000::Integer)
It would be nice if it didn't seg fault on error. I guess it doesn't have enough meta data in Plug.o to check the type. Anyway, that leaves the runhaskell
cases. I filed a bug for those.