12

If so, then I could let people play around with a project of mine by changing its (Haskell) configuration: recompiling it in the browser, and then rerunning it in the browser. I don't care about speed. I just want to be able to demo a hard-to-setup fundamentally-desktop project in the browser. So possible new users can see if they like it, within seconds and without setup.

How cool would that be!

from what I've read, GHCJS compiles STG into JavaScript. it says it uses the GHC API, so we should be able to compile that into JavaScript too, like any other Haskell library. (right?) Is it true that, while the GHC runtime has a lot of C, the compiler itself is pure Haskell?

so:

  1. Can GHCJS compile itself into JavaScript?
  2. Can Haste compile itself into JavaScript?
  3. Have either of these been done? (is that how http://haste-lang.org/try/ works? It seems to need a network.)
sam boosalis
  • 1,997
  • 4
  • 20
  • 32
  • Extensions shouldn't be an issue for either of these, I don't think. They both grab things out of a GHC intermediate language. I believe library support is a bigger concern. – dfeuer Feb 04 '15 at 18:25
  • indeed. https://github.com/valderman/haste-compiler says it supports every extension except Template Haskell. btw, what do you mean by library support? – sam boosalis Feb 04 '15 at 22:48
  • 2
    Libraries that rely on GHC's C FFI won't work. As I recall, Haste's support for parallel and concurrent programming is somewhat limited, while GHCJS's is not. As I understand it, the main advantage of Haste over GHCJS is that it produces compact compiled code, which is particularly important in a browser environment. But I've never personally used *either* of them. – dfeuer Feb 04 '15 at 23:33

1 Answers1

11

GHCJS and Haste both, to my knowledge, leverage GHC directly. GHC, though mainly haskell, is tied to a fair amount of cbits in terms of the runtime and how it operates. I am fairly certain neither can, at the moment, be compiled to run directly in browser in a "self-hosted" way.

However, Fay, which is a "Haskell-like-dialect" of JS can be compiled via GHCJS, which is pretty nifty (https://github.com/ghcjs/ghcjs-examples/tree/master/fay-hello) though perhaps not what you want.

Another alternative, if your system is simple enough, is to use emscripten to place hugs in-browser, and let your users play with that.

As you surmise, the various "try it in the browser" sites all actually use a connection to machines, typically running something like mueval that actually execute the code given.

sclv
  • 38,665
  • 7
  • 99
  • 204
  • thank you. My use case is to take any project I put on hackage, with arbitrary (non-FFI) GHC extensions (so no Fay), and have a slow/limited demo that can be recompiled in the browser. it would be nice to not need to pay for servers to recompile that code, or to worry about security. though from what I've read, mueval seems very solid. – sam boosalis Feb 20 '15 at 06:17
  • 2
    I don't see how the GHC runtime really relates. GHCJS and Haste produce code that does not use the usual GHC runtime at all. – dfeuer Feb 20 '15 at 14:57
  • @dfeuer right, all the "cbits" should be JavaScript in ghcjs. but I haven't studied GHC, and it's like over 100k loc, so maybe there's some implicit dependencies from typechecking/codegen onto the runtime. – sam boosalis Apr 21 '15 at 20:08
  • 1
    GHC might be possible to compile using emscripten(which compiles C to javascript), which would allow you to realize this project. I don't know what effort would be involved in this though.. – ggVGc Apr 27 '15 at 19:19