I am trying to understand below type
Prelude> :t map(10$)
map(10$) :: Num (a -> b) => [a] -> [b]
I am trying to understand below type
Prelude> :t map(10$)
map(10$) :: Num (a -> b) => [a] -> [b]
How to understand a constraint like Num (a->b)
? can be answered more generally: how to understand Num SomeCompoundType
?
Quite simple, such a constraint means that you tried to treat a type as a number type though it is not a number type. Such a type is just bogus, though it is not a type error (and theoretically, somebody could add a Num (a -> b)
instance).
With Num
in particular, “treated as a number” usually just means that you wrote a number literal in a place where e.g. a function is expected. The section (10$)
wants 10
to be a function. Because Haskell number literals are polymorphic, there is per se nothing wrong with that: 10
could be a function if only it had the suitable instance Num (a -> b)
. Just, this instance does not exist, but because somebody could in principle yet add it later, the compiler leaves this as a constraint-to-be-fulfilled.
(10 $)
is a function, (10 $) x = 10 $ x = 10 x
. This demands 10
to be of type a -> b
, because general function application is (f :: a -> b) (x :: a) :: b
.
In Haskell, numeric literals have type Num a => a
, meaning, any type a
which is in the type class Num
.
Thus the type of (10 $)
must be (Num a) => (a -> b)
. Indeed this is not a common situation. You probably didn't mean for 10
to serve as a function.
On the other hand, ($ 10) x = x $ 10 = x 10
. This is known as "operator sections": (a `op` b) = (a `op`) b = (`op` b) a = op a b
. When special characters ($
, :
, etc.) are used as "op" (operator), there's no need to put them inside the back-quotes.