[Haskell-cafe] Tutorial on JS with Haskell: Fay or GHCJS?

Daniil Frumin difrumin at gmail.com
Wed Sep 4 18:32:19 CEST 2013


On Sep 4, 2013, at 13:02, Niklas Hambüchen <mail at nh2.me> wrote:

> Hi, I'm also interested in that.
> Have you already evaluated haste?
> It does not seem to have any of your cons, but maybe others.
> What I particularly miss from all solutions is the ability to simply
> call parts written in Haskell from Javascript, e.g. to write `fib` and
> then integrate it into an existing Javascript application (they are all
> more interested in doing the other direction).

So, I don't how Fay handles things but I can say the following about GHCJS:

I have to admit that it's not that easy to call Haskell from
JavaScript as I want it.
However, there are several functions you can use for doing that. GHCJS
supports running actions in "main" asynchronous threads (application
is considered to be terminated when the thread is finished), regular
asynchronous threads and synchronous threads. Since you want to call
pure Haskell code you probably want to use runSync.

One way to do that would be to use an intermediate object to store the
result of calling Fib:

fib :: Int -> Int
fib n = fibs !! n
  where fibs = 0:1:zipWith (+) fibs (tail fibs)

fibAction :: JSRef () -> Int -> IO ()
fibAction ref n = do
    res <- toJSRef (fib n)
    setProp ("result"::Text) res ref
    function go() {
       var i = parseInt($("#num").val(), 10);
       var o = { result : null };
       var oref = mkJSRefNew(o);
       var act = h$c3( h$ap2_e
                     , h$mainZCMainzifibAction
                     , oref
                     , i );

You can find the complete sourcecode and a runnable executable here:
<http://co-dan.github.io/ghcjs/examples/Fib.hs> &

I think it's nice that you've raised that question, I will think about
implementing a finer API for calling Haskell from JS.

NB: It also sounds like you might benefit from the non-concurrent
GHCJS runtime and codegen that Luite is now porting to the new API.
Since you plan to implement your interface/interactions in JS you
probably don't need support for concurrency and you can enjoy smaller
code & rts sizes.

> On Wed 04 Sep 2013 17:14:55 JST, Alejandro Serrano Mena wrote:
>> Hi,
>> I'm currently writing a tutorial on web applications using Haskell. I
>> know the pros and cons of each server-side library (Yesod, Snap,
>> Scotty, Warp, Happstack), but I'm looking for the right choice for
>> client-side programming that converts Haskell to JavaScript. I've
>> finally come to Fay vs. GHCJS, and would like your opinion on what's
>> the best to tackle. My current list of pros and cons is:
>> Fay
>> ===
>> Pros:
>> - Does not need GHC 7.8
>> - Easy FFI with JS
>> - Has libraries for integration with Yesod and Snap
>> Cons:
>> - Only supports a subset of GHC (in particular, no type classes)
>> ======
>> Pros:
>> - Supports full GHC
>> - Easy FFI with JS
>> - Highly opinionated point: will stay longer than Fay (but it's very
>> important for not having a tutorial that is old in few months)
>> Cons:
>> - Needs GHC 7.8 (but provides a Vagrant image)
>> _______________________________________________
>> Haskell-Cafe mailing list
>> Haskell-Cafe at haskell.org
>> http://www.haskell.org/mailman/listinfo/haskell-cafe
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe

More information about the Haskell-Cafe mailing list