Calling Haskell from Java
Brandon Michael Moore
brandon@its.caltech.edu
Tue, 12 Aug 2003 10:15:54 -0700 (PDT)
Is it fine if the interface uses JNI? The jvm-bridge is an excellent tool
if you can use JNI, but I don't know of anything that compiles Haskell to
java bytecode. There was a post a few years ago about an experimental Java
backend for GHC, but I haven't heard anything since, and the -J switch
doesn't do anything in a recent GHC. The Mandarin people had a version of
GHC back when they were targeting Java, but they've moved to .NET. Does
anyone know of a project (or a CVS tag) for something that can compile
Haskell to java bytecode?
The jvm-bridge project includes tools for generating a Haskell interface
to a java class, another for generating a monad that wraps the JVM
initialization your program needs, using typeclasses to model the class
hierearch and convert parameters. There is a function that will
dynamically define a class with Haskell methods.
I don't know how much support jvm-bridge provides if you want to define a
class in Haskell and package it so you can use it from a normal java
program. You would need to declare a class in java with native methods,
and compile the haskell into a suitable library providing the native
implementation. I don't remember any tools for doing this.
I might have simply forgotten or overlooked a nice interface, or you might
need to write the JNI code. If you are determined to go this way you could
at least use the JNI binding JVM provides. Rather than doing that, it's
probably simpler for your program to start in Haskell, even if all
it does is define a class and invoke your main class (passing a factory
object).
I assume your haskell with need to call java at some point, if only to
unpack a collection, so I'll pass along two things that confused me for a
while. One thing to remember is that methods are defined in the class
module for the first class that defined them. If you want to use a method
that a class inherited from an ancestor you need to create and import the
class module for that ancestor. The method will have a name like
method_ancestor_args, but it calls the correct overridden method on
whatever object you pass in a this (first argument). The other thing (this
is more a Haskell issue) is that if you are writing a GUI program the main
(Haskell) thread has to survive until the program is supposed to end, and
it needs to be inside the "let java threads run" combinator. (sorry, I
forgot the name and I'm away from my home machine).
What are you trying to do? I'm thinking about porting a web testing
application from python to haskell for parser combinators and monads (I'm
using objects with a run method to control execution and thread through
some state), but I don't know of any alternative to the HttpUnit library
for testing webpages with javascript. I just need to call a bit of java in
the middle of a Haskell program
Tell us how your project works out.
Brandon
On 12 Aug 2003, Immanuel Litzroth wrote:
> Calling Haskell from java was supposed to be supported by a tool
> called lambada, but all I can seen to find of that on the net is a
> paper. Does anyone have any pointers to more information/implementation.
> I specifically want to call Java->Haskell and not the other way around.
> thanks in advance
> Immanuel
> ***************************************************************************
> It makes me uncomfortable to see
> An English spinster of the middle class
> Describe the amorous effects of `brass',
> Reveal so frankly and with such sobriety
> The economic basis of society.
> W.H. Auden
>
> --
> Immanuel Litzroth
> Software Development Engineer
> Enfocus Software
> Kleindokkaai 3-5
> B-9000 Gent
> Belgium
> Voice: +32 9 269 23 90
> Fax : +32 9 269 16 91
> Email: Immanuell@enfocus.be
> web : www.enfocus.be
> ***************************************************************************
>
> _______________________________________________
> Haskell mailing list
> Haskell@haskell.org
> http://www.haskell.org/mailman/listinfo/haskell
>
>