[Haskell-cafe] Is "let" special?

Petr Pudlak deb at pudlak.name
Wed Nov 3 13:05:12 EDT 2010


     Hi Günther,

from the semantical point of view, you can replace
> let x = e' in e
by
> (\x -> e) e'
Both should evaluate to the same thing.

However, from the typing point of view, it makes quite a difference. It is 
an integral part of the Hindley-Milner type inference algorithm, which 
is used by many functional languages. Consider the following two 
expressions:

> f = (\x -> x x) (\y -> y)
> g = let x = \y -> y in x x

The function "f" is not typable in the Hindley-Milner type system, while 
"g" is is (and its type is "a -> a"). The reason is that in the first 
case (f), the typing system tries to assign a single type to "x", which is 
impossible (one would have to unify types "a" and "a -> b"). In the 
second case (g), the typing system assigns "x" a polymorphic type schema
   forall a.a -> a
which can be instantiated to both "a -> a" (the second "x") and "(b -> 
b) -> (b -> b)" (the first "x"), and then applied together. I'd say that 
"let ... in ..." is a core feature of the language that gives us 
parametric polymorphism.

If you are interested in the details, I'd suggest you to read the 
original paper [damas1982principal].

     Best regards,
     Petr

@conference{damas1982principal,
   title={{Principal type-schemes for functional programs}},
   author={Damas, L. and Milner, R.},
   booktitle={Proceedings of the 9th ACM SIGPLAN-SIGACT symposium on Principles of programming languages},
   pages={207--212},
   year={1982},
   organization={ACM},
   url   =   {http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.122.3604&rep=rep1&type=pdf}
}

On Tue, Nov 02, 2010 at 02:34:29AM +0100, Günther Schmidt wrote:
>Hi all,
>
>is there something special about "let"? I don't mean only its use in 
>haskell, but in the general context of programming languages.
>
>I've been given a few hints over time when I asked question 
>concerning DSLs but regretfully didn't follow them up.
>
>Günther
>
>_______________________________________________
>Haskell-Cafe mailing list
>Haskell-Cafe at haskell.org
>http://www.haskell.org/mailman/listinfo/haskell-cafe
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 490 bytes
Desc: Digital signature
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20101103/6dc0d752/attachment.bin


More information about the Haskell-Cafe mailing list