Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling Haskell module to Javascript using ghcjs

Tags:

haskell

ghcjs

Is it possible to compile a Haskell module to JS using ghcjs? I played with it, but it seems it requires main function to be defined before it would generate JS. A toy code below:

module Test where

add :: Int -> Int -> Int
add x y = x + y 

I can't figure out how to compile it to JS using ghcjs. It will compile only .hi and .o files if main function is not defined. If I can figure out a way to compile it to JS (with appropriate Z-encoded function name, and a corresponding JS object, like what we have in FFI for C), then I could use more powerful Haskell libraries like unordered-containers and vector to implement some algorithm, compile to JS, and use in Elm through JS FFI.

Fay seems to be capable of doing it. I will like to investigate it for ghcjs first.

like image 798
Sal Avatar asked Jun 11 '14 03:06

Sal


People also ask

Does Haskell compile to JavaScript?

You can use GHCJS to compile Haskell code to JavaScript. It is a fork of GHC and supports pretty much everything GHC supports including TemplateHaskell. Most libraries on hackage work just fine as long as they do not have dependencies on C libraries. The compile times are slow and the resulting .

What is Ghcjs?

GHCJS is a Haskell to JavaScript compiler that uses the GHC API.


1 Answers

I just asked the ghcjs author (luite) in IRC, and he said, "there is no good way yet to directly export a library."

He does seem very interested in supporting that capability, though, so I'm hoping, and optimistic that this will change in the near future. The place to watch this issue is the ticket requesting this feature on github.

like image 71
Justin Leitgeb Avatar answered Sep 22 '22 11:09

Justin Leitgeb