7

Lets say I have a haskell function add in math.hs

How can I run the add function through a Java program and store the output as a variable?

Maybe something like the following:

public int runHaskell(String haskellFile) {
    int output;
    //run add function from file 'math.hs' and store result to output
    return output;
}

( If required I also have access to the object file: math.o and the interpreter file math.hi as well as the executable main.exe. )

4
  • This might involve some clever tricks with JNI. I'm curious to see what the answer is. Commented Feb 14, 2011 at 3:32
  • 1
    @templatetypedef I don't see how this would be any different than using JNI with C (GHC can generate intermediate C code) Commented Feb 14, 2011 at 3:36
  • @Rafe Kettler- True, but the naming conventions required by JNI and the fact that all the arguments have to be specific types would probably require you to build a "trampoline" C library that would then get linked in with the Haskell code. Commented Feb 14, 2011 at 3:49
  • If you need just a small portion of Haskell code, you might consider translating it to Jaskell ( docs.codehaus.org/display/JASKELL/Home ) Commented Feb 14, 2011 at 7:45

3 Answers 3

4

The easy (but clumsy) way:

Runtime.exec()

Then you can listen to the output of the Haskell program, and then parse it for the result.

Alternatively, you can write a small wrapper for JNI that calls directly into your Haskell stuff.

Sign up to request clarification or add additional context in comments.

2 Comments

Hmm. How do I listen for the output after I use exec() on the executable haskell program?
@Nick: exec returns a Process object, which you can use to get the stream that the process writes its output to.
1

You could use some of RPC frameworks, for example, Apache Thrift, which supports C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, Smalltalk, and OCaml.

There's also BERT-RPC client and server library for Haskell, but I'm not sure a Java port exists.

Comments

1

This might help: http://www.haskell.org/haskellwiki/Applications_and_libraries/Interfacing_other_languages. There is also Jaskell which might be able to run your entire source code under the JVM, allowing you to easily interface.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.