14

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.

5
  • What’s your precise command line? Commented Jun 11, 2014 at 8:41
  • @JoachimBreitner, ` ghcjs Test.hs` where Test.hs is above code. ghcjs claims it is "generating JavaScript" but I don't see any js code. I am using version 0.1.0. Commented Jun 11, 2014 at 13:50
  • Have you tried ghcjs -c Test.hs? Does that work better? Commented Jun 11, 2014 at 19:08
  • @JoachimBreitner, adding -c option doesn't make any difference either. Commented Jun 12, 2014 at 1:43
  • Fay is; A proper subset of Haskell that compiles to JavaScript. github.com/faylang/fay/wiki Commented Sep 25, 2014 at 13:16

1 Answer 1

7

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.

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

2 Comments

but what is the status of this as or 2015 Apr?
The ticket hasn't been updated since February 2015, and it's still open, so looks like it isn't done yet. ghcjs development is still very active (with commits in the last day) so I'm still optimistic on having this feature in t he future.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.