我正在寻找一种从Java程序中执行Haskell函数的有效方法.我已经考虑过使用exec()与GHC进行交互,但似乎应该有更好的方法.
这可能听起来像是一场噩梦,但我真的很想让它发挥作用.我大部分时间都在使用这个例子:从Haskell调用C并尝试在ubuntu上运行.
我在java中运行它:
package test;
public class JniTest {
public native int fib(int x);
}
Run Code Online (Sandbox Code Playgroud)
使用javah创建.h文件之后的这个:(test_JniTest.c)
#include "test_JniTest.h"
#include "Safe_stub.h"
JNIEXPORT jint JNICALL Java_test_JniTest_fib(JNIEnv * e, jobject o, jint f)
{
return fibonacci_hs(f);
}
Run Code Online (Sandbox Code Playgroud)
然后在haskell中引用(在存根之前):( Safe.hs)
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 …Run Code Online (Sandbox Code Playgroud) 假设我在math.hs中添加了haskell函数
如何通过Java程序运行add函数并将输出存储为变量?
可能类似于以下内容:
public int runHaskell(String haskellFile) {
int output;
//run add function from file 'math.hs' and store result to output
return output;
}
Run Code Online (Sandbox Code Playgroud)
(如果需要,我也可以访问目标文件:math.o和解释文件math.hi以及可执行MAIN.EXE.)