帮助 Haskell 动态插件加载
我是 Haskell 初学者,我正在尝试使用“插件”包中的动态加载。我有点迷失了。这是一个包含两个文件的最小程序。
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)
和 Plug.hs:
module Plug (thing) where
thing :: Integer
thing = 1234000
我使用 ghc -c Plug.hs 编译 Plug,生成 Plug.o。然后,我使用 ghc -o Main Main.hs 编译 Main.hs,并运行 Main。我还尝试将 load
替换为 dynload
,并使用 runhaskell
运行。这四种组合中只有一种有效。我做错了什么?
- 使用
动态加载
- 已编译 → 打印“已加载”,然后出现段错误
- runhaskell → 打印“Loading”,然后打印“Main.hs: Prelude.undefined”
- runhaskell → 打印“Loading”,然后使用
load
打印- 编译→成功,打印整数
- runhaskell → 打印“Loading”,挂起 5-10 秒,消失
我使用的是 Mac OS X.GHC 版本 7.0.2。我做错了什么?
谢谢,
Rob
更新
我可以通过将 Plug.hs 更改为以下内容来修复已编译的 dynload
...
module Plug (thing) where
import Data.Dynamic
thing :: Dynamic
thing = toDyn (1234000::Integer)
如果错误时没有出现 seg 错误,那就太好了。我猜想 Plug.o 中没有足够的元数据来检查类型。无论如何,剩下了 runhaskell 案例。我为此提交了一个 bug。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我已经在 Ubuntu 10.10 和 GHC 6.12.1 下尝试了您的示例,结果是:
dynload
和load
都运行编译或通过runhaskell
> 给我“Prelude.undefined”错误,所以我认为你应该向开发人员报告错误。我在其模块的 haddock 文档,所以我不认为你做错了什么。
I've tried your example under Ubuntu 10.10 with GHC 6.12.1 and the results are: both
dynload
andload
with running both complied or throughrunhaskell
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.
您可能想在 haskell ghc 动态编译仅适用于第一次编译。
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.