Hi All,
I found a really strange case where GHC and GHCi behave differently in inferring types. It took me hours to figure this out.
The following program
{-# LANGUAGE NoMonomorphismRestriction #-}
g x i = x ++ show i
[a,b] = map g ["X","Y"]
will not load without "NoMonomorphismRestriction". With that option, it will load and return the correct types (as expected):
*Main> :t [a,b]
[a,b] :: Show a => [a -> [Char]]
*Main> a 1
"X1"
However, if I do the same thing GHCi, the type inferencing seems to have been messed up:
*Main> let g x i = x ++ show i
*Main> let [a,b] = map g ["X","Y"]
*Main> :t [a,b]
[a,b] :: [() -> [Char]]
*Main> :t map g ["X","Y"]
map g ["X","Y"] :: Show a => [a -> [Char]]
Note how in the last two instances the terms on the left and right-hand sides of the definition return different types. Also, the correct return type should be a list of unary functions taking an (Show a) as
the parameter. Now the result is unusable:
*Main> a 1
<interactive>:52:3:
No instance for (Num ())
arising from the literal `1'
Possible fix: add an instance declaration for (Num ())
In the first argument of `a', namely `1'
In the expression: a 1
In an equation for `it': it = a 1
I am using GHCi 7.4.1 under windows. I also tried this under GHC 7.0x
Is this a GHCi bug or could anyone please explain why this can of strange behavior happens?
If this is a bug, could anyone with an account help file a bug for this?
Thanks in advance,
Ting
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20120415/1589d496/attachment.htm>