1

the type of 1+ is given as :

Prelude> :t (1+)
(1+) :: Num a => a -> a

Is the correct way to read this function as :

1+ takes a Num and returns a function of type a -> a ?

thepen
  • 371
  • 1
  • 11
  • 3
    No. The type of `(1+)` is `a -> a`, where there must be an instance for `Num a`. So it can be `Int -> Int` or `Integer -> Integer`, because the instances `Num Int` and `Num Integer` are defined by the Prelude, but `(1+)` can't be `String -> String` unless you define an instance `Num String`. –  Jul 11 '16 at 21:38
  • Possible duplicate of [Why sum x y is of type (Num a) => a -> a -> a in Haskell?](http://stackoverflow.com/questions/3742637/why-sum-x-y-is-of-type-num-a-a-a-a-in-haskell) – amalloy Jul 11 '16 at 22:57
  • 2
    Actually you are kinda right... Haskell has type classes (`Num` is one of them). They are implemented using so-called *dictionaries*. Basically functions that have some constraints like `Num a =>` will become functions with *extra dictionary arguments* which are the dictionaries related to the classes in the constraints and they bring around the implementation of the various operations. In this case `(1+)` is equivalent to `\numDict x -> (+) numDict 1 x` (where `+` as you see takes an extra argument too). But forget about this until you have learnt more haskell. – Bakuriu Jul 12 '16 at 18:54
  • Exactly. And a `Num`-dictionary is not at all a number, it's more like _the toolkit_ that allows you to use values as numbers. – leftaroundabout Jul 13 '16 at 09:37

3 Answers3

5

1+ takes an a and returns an a with the restriction that a must be an instance of Num.

sepp2k
  • 363,768
  • 54
  • 674
  • 675
4

No, Num a is a class constraint, which is implied by the different arrow (=>).

+1 is a function from a to a where a must be an instance of the Num typeclass.

For more information see the part of Learn you a Haskell.

ThreeFx
  • 7,250
  • 1
  • 27
  • 51
0

No, in Haskell the part before the => means the class constraint to which a parameter has to be an instance of. So,

(1+) :: Num a => a -> a 

It means (1+) is a function that takes a parameter "a" and returns a parameter "a" where the parameter "a" has to be an instance of the class constraint Num.

Here you can see the entire definition of the Num class constraint: http://hackage.haskell.org/package/base-4.9.0.0/docs/Prelude.html#t:Num

Miguelme
  • 609
  • 1
  • 6
  • 17