[Haskell-cafe] let vs do?

John Meacham john at repetae.net
Fri Jun 29 20:03:00 EDT 2007

On Fri, Jun 29, 2007 at 07:26:21AM -0700, Dave Bayer wrote:
> On Jun 28, 2007, at 9:49 PM, Stefan Holdermans wrote:
> >>That way you would have to use monads everywhere.
> >
> >As you already hinted at in a later message, this has to do with
> >let-bindings being potentially polymorphic and monadic bindings
> >being necessarily monomorphic:
> Are there papers that prove this need be the case in any language, or
> are we simply now trapped into this for the moment by some design
> choices?

Indeed, it is a requirement of the HM type inference system which
haskell is based on (though, it goes well beyond HM in a lot of ways)

Monads are in no way intrinsic to the language, they arn't part of core
to begin with but simply sugar for applications and lambda bindings. So,
asking whether we can drop 'let's in favor of 'do's is sort of
meaningless, dos are just translated away immediately, before any

do x <- f y; z is equivalent to

f y >>= \x -> z

notice that the 'x' is bound by a lambda binding, the tradeoff made in
the HM type system is that generalization (creating polymorphic types)
only occurs on let bindings, not lambda bound ones.

(ghc haskell has extensions that can lift this rule, at the expense of
needing user specified types in some circumstances)

monads are not core haskell, haskell just happens to be a really elegant
language for expressing them, and they happen to be useful enough to
haskell programmers that some nice syntatic sugar (do) is provided for
convenience but that is as far as the relationship goes.

That said, there is a precident (probably many, but this is the one I
know) for a language whose core is based on
monads rather than the lambda calculus, namely 'GRIN' the back end
language used by jhc. However, the cost (and benefit) of this is that
grin is first order, you cannot have closures or higher order functions,
(you can think of it as C but with a true pure functional type system
and all the goodness that implies). I have a toy mini-language called
'undo' (unboxed do) which I use sometimes to write things directly in
it which might be kind of neat to expose to the haskell programmer one
day... in any case, this isn't really relevant to your question, but
speaking generally, no, monads are not core haskell, yes monads can be
used as the core of a language, jhc actually does this, but not til very
far down the line and it has been transformed enough that I wouldn't
consider it core haskell. (in particular, the use of monads in grin have
no coorespondence to the use of monads in the original haskell source)  

(I am being sloppy with my use of 'core' here... we need some more


John Meacham - ⑆repetae.net⑆john⑈

More information about the Haskell-Cafe mailing list