1

I am trying to understand below type

Prelude> :t map(10$)
map(10$) :: Num (a -> b) => [a] -> [b]
NePh
  • 966
  • 9
  • 21
Aniketh
  • 1,411
  • 3
  • 12
  • 15
  • What do you understand about it? What do you have trouble understanding? – Vincent Savard Nov 14 '16 at 15:36
  • Having trouble understanding what Num(a-> b) means in this context ? Num instance for a function type ? I recently started learning haskell ,so far i have seen Num a type but not Num (function). – Aniketh Nov 14 '16 at 15:42
  • `(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. – Will Ness Nov 14 '16 at 15:49
  • You are right @WillNess, I dint mean 10 to serve as a function am just trying to understand how map function and dollar operator work in different combinations. Thanks – Aniketh Nov 14 '16 at 15:55
  • @WillNess , What happens if I put 10 on right hand side of $ like `($10) x`,how x is applied to 10 ? (for example x = (\y->y+1)) .Thanks – Aniketh Nov 14 '16 at 16:07
  • @anired: try it and see! If you're still confused, try asking another question here. – Jonathan Cast Nov 14 '16 at 16:08
  • @jcast I tried it `Prelude> ($10) (\x->x+3)` returns `13` , here I want to know how the literal `10 ` is applied to function ,as `($(\x->x+3)) 10` fails. – Aniketh Nov 14 '16 at 16:15
  • @anired In one case, `10` is the second argument to `($)`; in the other, it is the first, Now, take a second look at the type of `($)`. – duplode Nov 14 '16 at 16:24
  • 1
    @duplode I tried a lot to find documentation about ($) but no luck , would really appreciate if you can point me to one. Thanks much – Aniketh Nov 14 '16 at 16:28
  • `($10) (\x -> x+3)` is equivalent to `(\f -> f $ 10)(\x->x+3)`, which reduces to `(\x -> x + 3) $ 10`, and finally to `10 + 3` and `13`. `($)` is just function application in operator form: `f $ x = f x`. It might make more sense if you give it a different name and put it in normal prefix form: `apply f x = f x`. – chepner Nov 14 '16 at 16:32
  • @anired Try `:t ($)`. [Hoogle](https://www.haskell.org/hoogle/?hoogle=%24) can also help in such cases. – duplode Nov 14 '16 at 16:50

2 Answers2

4

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.

leftaroundabout
  • 117,950
  • 5
  • 174
  • 319
3

(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.

Community
  • 1
  • 1
Will Ness
  • 70,110
  • 9
  • 98
  • 181