帮助 Haskell 动态插件加载

发布于 2024-10-27 02:56:33 字数 1563 浏览 2 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

坏尐絯 2024-11-03 02:56:33

我已经在 Ubuntu 10.10 和 GHC 6.12.1 下尝试了您的示例,结果是:dynloadload 都运行编译或通过 runhaskell > 给我“Prelude.undefined”错误,所以我认为你应该向开发人员报告错误。

我在其模块的 haddock 文档,所以我不认为你做错了什么。

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.

貪欢 2024-11-03 02:56:33

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文