Why is this function type-correct
Hello, Recently, I wrote a function similar to x :: a x = 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 = 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.) For what reasons are these types considered correct? Thanks, Rijk-Jan
"Rijk J. C. van Haaften" <rjchaaft@cs.uu.nl> wrote:
Recently, I wrote a function similar to
x :: a x = 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 = 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.)
For what reasons are these types considered correct?
When you say x :: a you are asking that the compiler check that everything you say about x is consistent with x being acceptable where /any/ type is required. In the application x 42, it requires that x be a function, which is fine, because x has any type, and this includes functions. When you say x = x 42, this requires that the type returned from x 42 is the same as the type of x, again fine because if x::a, then x:: Integer -> a also, so x 42:: a. It works out in practise because x = x 42 gives x the value bottom, and bottom::a for all a. Jón -- Jón Fairbairn Jon.Fairbairn@cl.cam.ac.uk 31 Chalmers Road jf@cl.cam.ac.uk Cambridge CB1 3SZ +44 1223 570179 (after 14:00 only, please!)
mån 2002-03-04 klockan 15.11 skrev Rijk J. C. van Haaften:
Hello,
Recently, I wrote a function similar to
x :: a x = x 42
which is type-correct (Hugs, Ghc, THIH). Still, from the expression it is clear that the type shoud have a function type.
It might interest you to know that this function is also type correct: x :: x x = x x x x x x I would like to see a compiler derive that type though... Regards, Martin -- [ http://www.dtek.chalmers.se/~d95mback/ ] [ PGP: 0x453504F1 ] [ UIN: 4439498 ] Opinions expressed above are mine, and not those of my future employees. SIGBORE: Signature boring error, core dumped
participants (3)
-
Jon Fairbairn -
Martin Norbäck -
Rijk J. C. van Haaften