gpt4 book ai didi

haskell - 在 Haskell 中使用 llvm 绑定(bind)时,在模块中找不到“main”函数

转载 作者:行者123 更新时间:2023-12-02 18:39:13 25 4
gpt4 key购买 nike

我正在尝试使用 Haskell 的 LLVM 绑定(bind)来创建一个非常简单的“hello world”独立应用程序。这个想法是,当我运行 Haskell 应用程序时,它会吐出一些字节码,这些字节码可以依次运行并输出“hello world!”

-- hellofunc prints out "hello world"
hellofunc :: CodeGenModule (Function (IO ()))


_main :: (Function (IO ())) -> CodeGenModule (Function (IO ()))
_main func = createNamedFunction ExternalLinkage "main" $ do
call func
ret ()

main = writeCodeGenModule "hello.bc" (liftA _main hellofunc)

当我运行此程序时,我看到以下错误:

'main' function not found in module.

我使用 createNamedFunction 显式创建 main 函数。我错过了什么?

最佳答案

问题出在liftA的使用上或fmap而不是=<< 。当使用类型签名时,它变得更加明显(构建中断)。在 liftA 案例中,CodeGenModule 中未评估 _main,因此它不会出现在输出文件中。

修改writeCodeGenModule可能是合理的采取CodeGenModule ()而不是CodeGenModule a 。它使 LLVM JIT 用户的生活变得更加复杂,但有助于防止此类错误。

import Control.Applicative
import LLVM.Core
import LLVM.ExecutionEngine
import LLVM.Util.File

--
-- hellofunc prints out "hello world"
hellofunc :: CodeGenModule (Function (IO ()))
hellofunc = createNamedFunction ExternalLinkage "hello" $ do
ret ()

_main :: (Function (IO ())) -> CodeGenModule (Function (IO ()))
_main func = createNamedFunction ExternalLinkage "main" $ do
call func
ret ()

generateModuleBind :: CodeGenModule (Function (IO ()))
generateModuleBind = _main =<< hellofunc

-- Couldn't match expected type `Function (IO ())'
-- with actual type `CodeGenModule (Function (IO ()))'
--
-- this is the desired type:
-- generateModuleLiftA :: CodeGenModule (Function (IO ()))
--
-- but this is the actual type:
generateModuleLiftA :: CodeGenModule (CodeGenModule (Function (IO ())))
generateModuleLiftA = liftA _main hellofunc

main :: IO ()
main = writeCodeGenModule "hello.bc" generateModuleBind

关于haskell - 在 Haskell 中使用 llvm 绑定(bind)时,在模块中找不到“main”函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9427535/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com