6

I am trying to learn Haskell.

We have very simple functions. As I understood all the following are equivalent.

But I don't understand why f2 and f2' have different types, and why exactly Integer.

Also, if I specify the type of f2' manually, it accepts it and works same as f2.

f x = x + 1
-- f :: Num a => a -> a

f' = \x -> x + 1
-- f' :: Num a => a -> a

f2 a b = a + b
-- f2 :: Num a => a -> a -> a

f2' = \a b -> a + b
-- f2 :: Integer -> Integer -> Integer

f2'' = \a -> \b -> a + b
-- f2 :: Integer -> Integer -> Integer

Any ideas?

DonJoe
  • 1,603
  • 1
  • 13
  • 24
  • 2
    The answer to this good question is not all that interesting: that happens merely due to a technicality called the *monomorphism restriction*. For a detailed explanation of the reason why the behaviour is different, go to the "When does it happen?" section of the answer to the linked question. You can tell GHC to use the more general type in all cases by adding the corresponding type signature -- e.g. `f2' :: Num a => a -> a` – duplode Apr 14 '18 at 15:09
  • I understand this is a duplicate question, but you know how it is when you don't know something good enough in order to make a good question. – DonJoe Apr 14 '18 at 15:09
  • I understand now. Thanks for your help. – DonJoe Apr 14 '18 at 15:10
  • 8
    You're welcome. (By the way, don't worry about having posted a duplicate question. Non-obvious duplicates are useful as a signpost for people who land in them; furthermore, the answer here is arcane enough that it would be unreasonable to expect you to have guessed what to look for.) – duplode Apr 14 '18 at 15:20

0 Answers0