3

Recently, I decided to port some javascript code to Fay, so that I could guarantee type correctness, and so that I could handle async in a neat way (and also to experiment). I had built a few javascript objects whose only purpose was to synchronise parallel ajax calls. I am quite new to Haskell.

In Fay, I was hoping to fire off a few ajax calls, and use, say, Control.Monad.Parallel.sequence to make them synchronize. However, I'm stuck and in desperate need of guidance because the fay-jquery AJAX functions don't return a monad for the success/failure callbacks, and this example in the fay snaplet doesn't either. They all return a Fay () monad which just appears to be for sequencing lazy haskell calls into strict javascript statements together, whereas I was kind of expecting something like a tuple of (Fay (), IO (Either SuccessResult FailureResult)), since firing off an AJAX request results in two different actions.

I have lots of questions about this:

  1. Is the continuation monad what I'm looking for?
  2. Are there any modules that work with Fay, and use this monad for asynchronous javascript?
  3. Since the side effect of fay-jquery's AJAX functions occurs after anything bound or sequenced after the returned Fay (), does this make the functions impure?
  4. I notice a continuation monad in the Fay repo - Why isn't it used in Fay itself? The only thing I can find is this gist which wraps async node.js fs calls.

Answers would be much appreciated! They would massively help my shaky understanding of this stuff.

user1158559
  • 1,954
  • 1
  • 18
  • 23

1 Answers1

2
  1. Yes. The Cont example essentially does what you want, sans error handling.
  2. ^
  3. It depends on your definition of "impure". I'd say that a function returning before all its effects have been performed can still be pure. This is the case for pretty much all lazy computations in Haskell. see for instance Haskell's forkIO :: IO () -> IO ThreadId
  4. I'm not sure what you mean with "in Fay itself". I don't think the Prelude should make use of this but other libraries could use Cont and if you want this feel free to make a package out of the example.
Adam Bergmark
  • 7,316
  • 3
  • 20
  • 23
  • Thanks for answering. I've been researching myself, and I've determined that extending the Cont example is indeed what I want. The example is necessary because Control.Monad.Cont relies on typeclasses which aren't supported by Fay. – user1158559 Sep 08 '14 at 12:58
  • Surely, for a function with side effects to be pure, it must return a representation of those side effects. For instance, `forkIO` would return immediately with something of type `IO ThreadID`, which represents the side effect of a thread being created some time in the future. Since binding a function to the fay monad just means that that function is executed immediately after the function that produced the Fay monad, then surely the `Fay` monad does not represent the side effect of the AJAX call succeeding or failing, so the AJAX call must be impure. – user1158559 Sep 08 '14 at 13:14
  • 4 is a good point - Fay itself as a Haskell to JS compiler has no use for a continuation monad. However, I wondered if it was something that might be very useful for bridging the gap between Javascript's callbacks and Haskell's use of monads for handling async. Personally, part of my motivation for looking into Fay was to see if I could use monads to synchronise asynchronous calls in a cleaner way. – user1158559 Sep 08 '14 at 13:27
  • I'm also going to give GHCJS a shot – user1158559 Sep 08 '14 at 13:29
  • Just noticed that you coauthored Fay! Very impressive, and thankyou! – user1158559 Sep 08 '14 at 13:36