Function output type in GHCi differs from that loaded from file

I wrote a function add'

in test.hs

:

add' = \x y -> x + y

      

Then I downloaded test.hs

to GHCi (version 7.8.3) and typed :t add'

to see what type add'

. The result looks incorrect:

*Main> :t add'
add' :: Integer -> Integer -> Integer

      

But if I typed :t (\x y -> x + y)

directly into GHCi, the result is correct:

*Main> :t (\x y -> x + y)
(\x y -> x + y) :: Num a => a -> a -> a

      

Then I tried to rewrite add'

in test.hs

and then :type

these in GHCi:

add1 = \x y -> x + y
add2 = \x -> \y -> x + y
add3 x y = x + y

      

Results:

add1 :: Integer -> Integer -> Integer
add2 :: Integer -> Integer -> Integer
add3 :: Num a => a -> a -> a

      

But use a suggestion let

to define a function and then :type

or :type

lambdas right in GHCi everything casts to the typeNum a => a -> a -> a

Why are they different? Is this a GHC bug?

+3


source to share


1 answer


You have suffered from a terrible limitation of monomorphism . The monomorphism constraint makes the type signature of your function specialized for one type. You can disable this with an extension NoMonomorphismRestriction

:

{-# LANGUAGE NoMonomorphismRestriction #-}

add1 = \x y -> x + y
add2 = \x -> \y -> x + y
add3 x y = x + y

      



And then when you load the types into ghci they will be:

λ> :t add1
add1 :: Num a => a -> a -> a
λ> :t add2
add2 :: Num a => a -> a -> a
λ> :t add3
add3 :: Num a => a -> a -> a

      

+10


source







All Articles