Help with Haskell dynamic plugin load

2019-02-01 21:56发布

问题:

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.

回答1:

I've tried your example under Ubuntu 10.10 with GHC 6.12.1 and the results are: both dynload and load with running both complied or through runhaskell gives me "Prelude.undefined" error, so I think you should report a bug to the developers.

I cannot see any special cases nor conditions in their module's haddock documentation, so I don't think you doing anything wrong.



回答2:

You might want to take a look at a similar problem with the GHC-API on haskell ghc dynamic compliation only works on first compile.