
2009/7/17 Andrew Coppin
I've been working hard this week, and I'm stumbled upon something which is probably of absolutely no surprise to anybody but me.
Consider the following expression:
(foo True, foo 'x')
Is this expression well-typed?
Astonishingly, the answer depends on where "foo" is defined. If "foo" is a local variable, then the above expression is guaranteed to be ill-typed. However, if we have (for example)
foo :: x -> x
as a top-level function, then the above expression becomes well-typed.
Some useful reading material: Section 22.7 of the book "Types and Programming Languages" by Benjamin Pierce. The classic paper "Basic Polymorphic Typechecking" by Luca Cardelli: http://lucacardelli.name/Papers/BasicTypechecking.pdf Both of these are very readable introductions to the let-style polymorphism found in the Hindley/Milner type system. Haskell's type system is essentially an elaboration of that idea. Pierce's book shows how to achieve let-polymorphism by inlining non-recursive let bindings during type checking/inference, which is a nice way to understand what is going on.
participants (1)
-
Bernie Pope