[Haskell-cafe] Re: Applicative do?

Nicolas Pouillard nicolas.pouillard at gmail.com
Sat Oct 10 06:32:45 EDT 2009

Excerpts from oleg's message of Sat Oct 10 11:36:44 +0200 2009:
> It seems the applicative do is almost identical to the form ML and
> Scheme has had since the beginning. Perhaps that semantic similarity
> might inform the syntactic debate.
> > do a <- f
> >    g
> >    b <- h
> >    pure $ foo a b
> >
> > into this:
> >
> > (\a b -> pure $ foo a b) <*> (f <*> g *> h)
> This form, although very similar in appearance to the monadic do, is
> very different in the scoping of bound identifiers. To me, that is
> quite unfortunate, greatly increasing confusion. In the monadic do,
>     do
>     x <- e1
>     y <- e2
>     e3
> the identifier 'x' may appear in e2 and e3. To be more precise, the
> scope of each bound identifier extends to the rest of the do-block and
> can be used on the right-hand-side of other bindings in the rest of
> the block. In the proposed applicative do above, the identifier 'a'
> may not appear within g and h. Such a significant difference in
> scoping calls for a different name at the very least; furthermore, it
> calls for a compiler supports so that errors, such as using the
> identifier 'a' in 'h' by mistake (common during editing and
> refactoring) could be reported with an intelligent error message.

I also think that an applicative 'do' will be very confusing and unfortunate. 

And these are very good arguments.

> Also, in monadic-do, it makes sense to bind the same identifier
> several times, as in do {x <- e1; x <- e2; e3}. It makes little sense
> doing that in applicative-do, and probably should be disallowed.

Again good argument.


> P.S. For completeness, both Scheme and OCaml have the exact equivalent
> of monadic do. Haskell's
>     do
>     x <- e1
>     y <- e2
>         x <- e3
>     e4
>     e5
> can be written in Scheme as
>     (let* ((x e1) (y e2) (x e3) (_ e4)) e5)
> and in OCaml as
>     let x = e1 in 
>     let y = e2 in 
>     let x = e3 in 
>     let () = e4 in 
>     e5
> The latter pattern is very common in OCaml.

Seriously, are you joking? I agree that the OCaml 'let' denotes both
a local binding and the sequence of effects. But the relation with
the monadic do is very thin.

As you know OCaml always lives in an implicit big IO monad and so its
'let' syntax is a monadic do.

However there is plenty of other monads that could use the do notation,
and of course you know them well (and there is Camlp4 extensions in
OCaml for that).

Best regards,

Nicolas Pouillard

More information about the Haskell-Cafe mailing list