[Haskell-cafe] Stacking monads

Andrew Coppin andrewcoppin at btinternet.com
Fri Oct 3 13:34:26 EDT 2008


Tillmann Rendel wrote:
> Seriously, what are you talking about? The haddock page for
> Control.Applicative hoogle links to begins with
>
>> This module describes a structure intermediate between a functor and
>> a monad: it provides pure expressions and sequencing, but no binding.
>> (Technically, a strong lax monoidal functor.) For more details, see
>> Applicative Programming with Effects, by Conor McBride and Ross
>> Paterson, online at
>> http://www.soi.city.ac.uk/~ross/papers/Applicative.html.
>>
>> This interface was introduced for parsers by Niklas Röjemo, because
>> it admits more sharing than the monadic interface. The names here are
>> mostly based on recent parsing work by Doaitse Swierstra.
>>
>> This class is also useful with instances of the Traversable class.
>
> I agree that this is hard to understand, but it's more then just 
> "strong lax monoidal functor", isn't it? More importantly, there is a 
> reference to a wonderful and easy to read paper. (easy in the "easy 
> for Haskell programmers" sense, not in the "easy for the authors, and 
> maybe the inventors of Haskell" sense). Just give it a try.
>
> Just in case you missed the link for some reason, here is it again:
>
>   http://www.soi.city.ac.uk/~ross/papers/Applicative.html

You must have a radically different idea of "easy to read paper" than I 
have. ;-)

Anyway, after multiple hours of staring at this paper and watching 
intricate type signatures swim before my eyes, I simply ended up being 
highly confused. (I especially like the way that what's described in the 
paper doesn't quite match what's in the actual Haskell standard 
libraries...) After many hours of thinking about this, I eventually 
began to vaguely comprehend what it's saying. (I suspect the problem is 
that, rather like monads, the concepts it's attempting to explain are 
just so extremely abstract that it's hard to develop an intuitive notion 
about them.)

So... something that's "applicative" is sort-of like a monad, but where 
the next action cannot vary depending on the result of some prior 
action? Is that about the size of it? (If so, why didn't you just *say* so?)

But on the other hand, that would seem to imply that every monad is 
trivially applicative, yet studying the libraries this is not the case. 
Indeed several of the libraries seem to go out of their way to implement 
duplicate functionallity for monad and applicative. (Hence the sea of 
identical and nearly identical type sigantures for functions with 
totally different names that had me confused for so long.)

If you think in terms of containers, then "c x" is a container of "x 
values". Then, the type signature

  sequence :: c1 (c2 x) -> c2 (c1 x)

kind-of makes sense. (Obviously the two containers are constrained to 
particular classes.) So that's "traversable", is it?

Again, we have "sequence" and "sequenceA", indicating that monads and 
applicatives aren't actually the same somehow. Also, before you can put 
anything into Traversable, it has to be in Functor (no hardship there) 
and Foldable. Foldable seems simplish, except that it refers to some odd 
"monoid" class that looks suspiciously like "MonadPlus" but isn't... wuh?

OK, maybe I should just stop attempting to comprehend this stuff and 
write the code... At this point learning about applicative and 
traversable isn't actually solving my problem.



More information about the Haskell-Cafe mailing list