Type inference of a function in GHCi differs with loaded from a file

前端 未结 1 653
忘了有多久
忘了有多久 2021-01-20 03:46

I wrote a function add\' in test.hs:

add\' = \\x y -> x + y

Then I loaded test.hs in GHCi (versio

相关标签:
1条回答
  • You have hit upon the dreaded monomorphism restriction. Monomorphism restriction makes the type signature of your function specialized to a single type. You can turn off that using the 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 in 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
    
    0 讨论(0)
提交回复
热议问题