从Common Lisp调用Haskell(SBCL)

Dim*_*ris 6 haskell sbcl common-lisp ghc cffi

我试图在debian pc上从Common Lisp(sbcl版本1.2.4)调用Haskell(ghc版本7.6.3).

Haskell代码是

{-# LANGUAGE ForeignFunctionInterface #-}

module Safe where

import Foreign.C.Types

fibonacci :: Int -> Int
fibonacci n = fibs !! n
    where fibs = 0 : 1 : zipWith (+) fibs (tail fibs)

fibonacci_hs :: CInt -> CInt
fibonacci_hs = fromIntegral . fibonacci . fromIntegral

foreign export ccall fibonacci_hs :: CInt -> CInt
Run Code Online (Sandbox Code Playgroud)

和C接口是

#include <HsFFI.h>
#ifdef __GLASGOW_HASKELL__
#include "Safe_stub.h"
extern void __stginit_Safe(void);
#endif
#include <stdio.h>

int foo(int i)
{
  int argc = 0;
  char *argv[] = {NULL};
  char **pargv = argv;

  int f;

  hs_init(&argc, &pargv);
#ifdef __GLASGOW_HASKELL__
  hs_add_root(__stginit_Safe);
#endif

  f = fibonacci_hs(i);

  hs_exit();

  return f;
}
Run Code Online (Sandbox Code Playgroud)

编译和链接由以下命令完成:

ghc -c -dynamic -fPIC Safe.hs
gcc -c -fPIC -I`ghc --print-libdir`/include foo.c
ghc -o libfoo.so -dynamic -shared -lHSrts-ghc7.6.3 Safe.o foo.o
Run Code Online (Sandbox Code Playgroud)

在sbcl中,我用quicklisp加载cffi,然后加载动态库

(cffi:load-foreign-library "~/work/test-haskell-lisp/libfoo.so")
Run Code Online (Sandbox Code Playgroud)

当我调用该函数时 foo

(cffi:foreign-funcall "foo" :int 3 :int)
Run Code Online (Sandbox Code Playgroud)

我从sbcl得到错误"Unhandled memory fault".欢迎任何建议或想法.