If we have the following in Haskell:

f x y = x + y
:type f
f :: Num a => a -> a -> a

then GHC would report Num a1 => (a2 -> a1) -> a2 -> a1 -> a1 as the type for (f .). Would you please explain me the steps through which GHC comes to such conclusion?

  • $\begingroup$ Note: Haskell specific question is off-topic here. $\endgroup$ – Apiwat Chantawibul Sep 28 at 2:56
  • $\begingroup$ First, (.) is an infix binary operator (.) :: (b -> c) -> (a -> b) -> a -> c. Next, (f .) is the same operator, but partially applied with the first argument. So, (f .) :: (a -> b) -> a -> c $\endgroup$ – Apiwat Chantawibul Sep 28 at 2:59
  • $\begingroup$ You can see that f is taken to provide something of type b -> c for the first argument of (.). But since you already define previously that f :: Num a => a -> a -> a. So GHC put bracket as f :: Num a => a -> (a -> a) to match the expected type b -> c $\endgroup$ – Apiwat Chantawibul Sep 28 at 3:12
  • $\begingroup$ @Apwit Thank you for your clear answer. But for my question being off-topic I just used Haskell as an expressing platform, and my question was generally about inferring the type in general. Thanks again. $\endgroup$ – al pal Sep 28 at 17:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.