[Haskell-cafe] need help with understanding expression

Sean Leather leather at cs.uu.nl
Fri Nov 16 09:57:28 CET 2012

Hi Daryoush,

Prelude> :t 3 2
> 3 2 :: (Num a, Num (a -> t)) => t
> What does the type mean in plain english?

It's important to remember that numeric literals are polymorphic. That is,
3 :: Num a => a. They do not have monomorphic types such as Int or Integer.

In the above, GHCi is inferring the principal type of 3 applied to 2. Since
3 is in the position of function application, it should have a function
type, e.g. a -> t. And 2 is the argument to 3, so it has the type 'a'. But
there must be Num constraints on these types, and that's what the
context (Num a, Num (a -> t)) is telling you. Num (a -> t) is a strange
constraint but so is applying 3 to 2. Whenever you see a Num constraint on
a function type, it probably means you're doing something wrong.

You might find the (brief) description of typing numeric literals in the
language definition helpful:


Also, in response to your initial query...

I am having hard time understanding how removing the  outer parenthesis in
> (max.(+1)) 2 2
> to
> max.(+1) 2 2

Keep in mind the precedence of the function composition operator (.) here:

Prelude> :i (.)
(.) :: (b -> c) -> (a -> b) -> a -> c -- Defined in GHC.Base
infixr 9 .

Function application is infixl 10, so even though max . (+1) :: (Num b, Ord
b) => b -> b -> b, when you do max . (+1) 2, the application of (+1) to 2
binds more tightly than (.).

A common idiom for removing parentheses with a sequence of compositions is
to use ($):

Prelude> :i ($)
($) :: (a -> b) -> a -> b -- Defined in GHC.Base
infixr 0 $

Note that it has the lowest possible precedence. So, you often see the
composition of functions as f . g . h $ arg. But this doesn't work well
for (curried) functions with 2 arguments. So, in your case, I'd guess the
parentheses is simplest idiomatic solution. Though, I think I'd prefer max
(succ 2) 2.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20121116/27bef1b8/attachment.htm>

More information about the Haskell-Cafe mailing list