我正在编写一个需要解释和评估haskell代码的C ++应用程序。此代码在编译时是未知的,但由用户给出。 有没有办法使用haskell编译器/解释器(如GHCi或拥抱)作为库?
答案 0 :(得分:8)
对于这种特殊方法,我建议不要使用GHC api,而是使用Hint,这只是围绕GHC api的简化包装。我推荐这个的原因是因为GHC api有一点陡峭的学习曲线。
但无论如何,就像我说的在我的评论中,根据你想要的深度,它将需要令人惊讶的FFI呼叫。下面我举例说明如何从加载的文件中运行表达式并返回结果(仅当有一个show实例时)。这只是基础知识,将结果作为应该的结构返回。
module FFIInterpreter where
import Language.Haskell.Interpreter
import Data.IORef
import Foreign.StablePtr
type Session = Interpreter ()
type Context = StablePtr (IORef Session)
-- @@ Export
-- | Create a new empty Context to be used when calling any functions inside
-- this class.
-- .
-- String: The path to the module to load or the module name
createContext :: ModuleName -> IO Context
createContext name
= do let session = newModule name
_ <- runInterpreter session
liftIO $ newStablePtr =<< newIORef session
newModule :: ModuleName -> Session
newModule name = loadModules [name] >> setTopLevelModules [name]
-- @@ Export
-- | free a context up
freeContext :: Context -> IO ()
freeContext = freeStablePtr
-- @@ Export = evalExpression
runExpr :: Context -> String -> IO String
runExpr env input
= do env_value <- deRefStablePtr env
tcs_value <- readIORef env_value
result <- runInterpreter (tcs_value >> eval input)
return $ either show id result
由于我们必须退出haskell land,我们必须有一些方法来引用Context,我们可以使用StablePtr
执行此操作,然后将其包装在IORef
中以使其可变为如果你想改变未来的事情。请注意,GHC API不支持对内存缓冲区进行类型检查,因此您必须在加载之前将要解释的代码保存到临时文件中。
-- @@
注释适用于我的工具Hs2lib,如果您不使用它,请不要介意。
我的测试文件是
module Test where
import Control.Monad
import Control.Monad.Instances
-- | This function calculates the value \x->x*x
bar :: Int -> Int
bar = join (*)
我们可以使用简单的测试来测试这个
*FFIInterpreter> session <- createContext "Test"
*FFIInterpreter> runExpr session "bar 5"
"25"
所以是的,它适用于Haskell,现在让它在haskell之外工作。
只需在文件顶部添加一些关于如何编组ModuleName
的Hs2lib的说明,因为该类型是在没有源代码的文件中定义的。
{- @@ INSTANCE ModuleName 0 @@ -}
{- @@ HS2HS ModuleName CWString @@ -}
{- @@ IMPORT "Data.IORef" @@ -}
{- @@ IMPORT "Language.Haskell.Interpreter" @@ -}
{- @@ HS2C ModuleName "wchar_t*@4" @@ -}
或
{- @@ HS2C ModuleName "wchar_t*@8" @@ -}
如果是64位架构,
并调用Hs2lib
PS Haskell\FFIInterpreter> hs2lib .\FFIInterpreter.hs -n "HsInterpreter"
Linking main.exe ...
Done.
你最终将与包含
的Include文件结束#ifdef __cplusplus
extern "C" {
#endif
// Runtime control methods
// HsStart :: IO ()
extern CALLTYPE(void) HsStart ( void );
// HsEnd :: IO ()
extern CALLTYPE(void) HsEnd ( void );
// createContext :: ModuleName -> IO (StablePtr (IORef (Interpreter ())))
//
// Create a new empty Context to be used when calling any functionsinside this class.
// String: The path to the module to load or themodule name
//
extern CALLTYPE(void*) createContext (wchar_t* arg1);
// freeContext :: StablePtr (IORef (Interpreter ())) -> IO ()
//
// free a context up
//
extern CALLTYPE(void) freeContext (void* arg1);
// evalExpression :: StablePtr (IORef (Interpreter ())) -> String -> IO String
extern CALLTYPE(wchar_t*) evalExpression (void* arg1, wchar_t* arg2);
#ifdef __cplusplus
}
#endif
我还没有对C ++方面进行过测试,但是没有理由不应该这样做。 这是一个非常简单的例子,如果你将它编译成动态库,你可能想要重定向stdout,stderr和stdin。
答案 1 :(得分:4)
由于GHC是用Haskell编写的,因此它的API只能从Haskell获得。正如Daniel Wagner建议的那样,在Haskell中编写所需的接口并使用FFI将它们绑定到C将是最简单的路径。这可能比使用GHC API直接绑定到C更容易;你可以使用Haskell的优势来构建你需要的接口,并且只在顶层使用C ++与它们进行交互。
请注意,Haskell的FFI只会导出到C;如果你想要一个C ++ - ish包装器,你必须把它写成另一层。
(顺便说一下,拥抱是古老的,没有维护。)