Why is this function type-correct

Simon Marlow simonmar@microsoft.com
Mon, 4 Mar 2002 14:23:17 -0000

> Recently, I wrote a function similar to
> x :: a
> x =3D x 42
> which is type-correct (Hugs, Ghc, THIH).
> Still, from the expression it is clear
> that the type shoud have a function type.
> The definition
> x :: a -> b
> x =3D x 42
> is equally well accepted, though I can't
> see why this type would be correct. (I'd
> expect it to be too general.)

In two words: polymorphic recursion.  You'll find that the compiler
won't be able to derive a type for 'x' in either of the two examples you
gave, but x has several types the most general of which is 'forall a. a'
(ie. your first example).

The fact that x has type 'forall a. a' is also a useful hint as to its
behaviour - the only value that has such a type is bottom.