85

I'm wondering if there is some way to make Haskell run on the JVM (compiled or interpreted)?

There exists JHaskell on Sourceforge but this one seems to be empty and dead.

GHC uses LLVM as compiler backend. Would it be a good idea or possible to compile LLVM to Java bytecode? Or maybe use a different compiler backend?

Stefanus
  • 1,619
  • 3
  • 12
  • 23
jeha
  • 10,562
  • 5
  • 50
  • 69
  • 24
    GHC FAQ: [Why isn't GHC available for .NET or on the JVM?](http://www.haskell.org/haskellwiki/GHC:FAQ#Why_isn.27t_GHC_available_for_.NET_or_on_the_JVM.3F) – Josh Lee Aug 31 '11 at 17:40
  • 4
    And [this whole thread](http://www.haskell.org/pipermail/haskell-cafe/2009-June/063454.html) on haskell-cafe. – Josh Lee Aug 31 '11 at 17:41
  • Thanks for the links. So there is some mainly experimental stuff. Did you use some of them? Is there something recommendable to have a closer look at? – jeha Aug 31 '11 at 17:59
  • I haven’t used them, but LLVM might be an interesting avenue. – Josh Lee Aug 31 '11 at 18:03
  • did tail call optimization make it into JVM 1.7? – rampion Aug 31 '11 at 18:06
  • 3
    Tail call optimization still seems to be an open issue. That is why other functional jvm languages like Clojure need special constructs (`recur`). – jeha Aug 31 '11 at 18:18
  • There is http://jaskell.codehaus.org/ for the JVM, not the real thing, but better than nothing. – Landei Sep 01 '11 at 06:57
  • 14
    @jeha: Nah, TCO is easy. Seph does it, Erjang does it, Kawa and all the other Scheme implementations on the JVM do it. The JVM has Exceptions, which are basically the same as `GOTO`, which can be used to implement TCO. Or you use trampolines. Or you don't use the JVM call stack at all and just implement your own. The reason why Clojure and Scala only provide limited TCO (basically, only tail *recursion* is optimized) is because they *want* to use the JVM call stack for interoperability and performance reasons. As Rich Hickey, Clojure's designer said: Interop, speed, TCO -- Pick two. – Jörg W Mittag Sep 01 '11 at 12:06
  • 3
    I can't imagine it would be hard to compile Haskell to the JVM if you did not care about performance at all and only focus on what the Haskell language requires. Use trampolines as Jorg mentions. They are easy to implement. Use the simplest data structures that do the job. Don't bother optimizing. I'm sure someone could hack up a working and full implementation within a week by himself if he actually cared. – Thomas Eding Sep 01 '11 at 23:56
  • Performance or interoperability are not my concerns so far. I'd be glad to have some kind running proof of concept thing. So using trampolines sounds to be an interesting way to go. @trinithis: one week sounds quite promising - so why didn't somebody else hack this up? – jeha Sep 02 '11 at 06:31
  • 1
    @jeha: Because performance and interop are usually the two reasons why anyone would want to implement a language on the Java platform to begin with. – Jörg W Mittag Sep 03 '11 at 09:25

4 Answers4

83

You may want to investigate Frege. Quoting from that page:

"Frege is a non-strict, pure functional programming language in the spirit of Haskell."

"Frege programs are compiled to Java and run in a JVM."

Based on a brief perusal of the language specification, Frege looks to be nearly a Haskell clone. Perhaps the phrase "in the spirit of Haskell" is simpy intended to set the proper expectation.

Community
  • 1
  • 1
Daniel Pratt
  • 12,007
  • 2
  • 44
  • 61
  • 2
    Looks like they've updated their page slightly - it now reads "Frege is a Haskell for the JVM." which is a much stronger claim IMO. – Michael Anderson Sep 27 '16 at 01:00
28

Haskell works beautifully on the JVM. See Eta, a project that brings full GHC 7.10.3 Haskell onto the JVM with type-safe Java interop.

rahulmutt
  • 426
  • 5
  • 4
12

The only language I know that is close to haskell in the JVM is CAL. CAL is heavily based on haskell but it doesn't have all haskell's features. The type system is similar to Haskell 98, and syntactic sugar like do notation is missing.

Here's a comparison of Haskell and CAL: CAL for Haskell Programmers

The eclipse plugin is very polished and useful.

Note that CAL is part of the Open Quark framework.

tgdavies
  • 10,307
  • 4
  • 35
  • 40
islon
  • 1,166
  • 2
  • 11
  • 24
  • Actually I'm looking for "the real one". I can't judge about CAL. But CAL seems to have type classes btw. First impression is indeed very haskelish. – jeha Sep 05 '11 at 20:37
  • CAL does indeed have type classes -- it's pretty close to Haskell 98 minus a fair bit of syntactic sugar. – tgdavies Sep 08 '11 at 00:33
  • thanks for extra information tgdavies, I'm not a CAL programmer myself – islon Sep 08 '11 at 12:13
  • For some days I try to get the CAL for Hakell Programmers document. The link you provide does not seem to help either. Can somebody post a working link to the pdf, please? – Ingo Sep 17 '11 at 20:26
  • @Ingo: this link to the user's guide should work but maybe it's not the latest one: http://resources.businessobjects.com/labs/cal/cal_users_guide.pdf – jeha Sep 30 '11 at 19:31
9

There are big but surmountable impediments to GHC building to the JVM:

http://www.haskell.org/haskellwiki/GHC:FAQ#Why_isn.27t_GHC_available_for_.NET_or_on_the_JVM.3F

(Got a spare year or two to make it happen?)

amindfv
  • 8,438
  • 5
  • 36
  • 58