
Let's see tthis:
Prelude> :t 3 "a"
3 "a" :: (Num ([Char] -> t)) => t
No complaint from GHC; but now see this:
Prelude> :t "a" 3
<interactive>:1:0:
Couldn't match expected type `t1 -> t'
against inferred type `[Char]'
In the expression: "a" 3
Why does it not fail for (:t 3 "a") but does fail for (:t "a" 3)?
In the second case, why the GHC doesn't give something like?
([Char] (a -> t), Num a) => t
In other words, how the first one does make "sense" (however strange the
"sense" may seem) to GHC while the second one doesn't?
Now let's consider the evaluation scenario:
Prelude> 3 2
<interactive>:1:0:
No instance for (Num (t -> t1))
arising from the literal `3' at <interactive>:1:0-2
Possible fix: add an instance declaration for (Num (t -> t1))
In the expression: 3 2
In the definition of `it': it = 3 2
Note, how the GHC suggests us this "strange constraint" as a *possible
fix*BUT see this:
Prelude> "a" 2
<interactive>:1:0:
Couldn't match expected type `t1 -> t'
against inferred type `[Char]'
In the expression: "a" 2
In the definition of `it': it = "a" 2
In this case the GHC doesn't even allow us to add any "possible fix".
-Damodar
On Fri, Nov 16, 2012 at 2:27 PM, Sean Leather
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:
http://www.haskell.org/onlinereport/haskell2010/haskellch6.html#x13-1360006....
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.
Regards, Sean
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- Thanks and regards, -Damodar Kulkarni