13

If I force Haskell to infer the type of a number I'll get Num a => a. For example:

Prelude> :t 1
1 :: Num a => a

But what does a => a mean?

Finkelson
  • 2,921
  • 4
  • 31
  • 49

4 Answers4

20

1 :: Num a => a means that 1 has some type a, where a is an instance of the Num typeclass. Note that Num is not a type, but a typeclass, which describes common properties of various types. The Num typeclass, for example, describes types that are numeric, and so support basic arithmetic. The native machine integer type Int is an instance of Num, as is the arbitrary-sized Integer, the floating point type Double, and even the rational number type Rational.

Sebastian Redl
  • 69,373
  • 8
  • 123
  • 157
  • Not some type. Every type. – dfeuer Apr 29 '21 at 14:11
  • @dfeuer Not every type. Any type. Each instance of `1` has one and only one type at runtime. Try to do something like `(,) <$> (+ (1 :: Int)) <*> (+ (1 :: Float)) $ 1` and you won't have a good time. – A. R. Apr 29 '21 at 19:35
  • @AndrewRay, I suppose it's arguably more like "each type". `1` is operationally a function from a `Num` dictionary to a value. OOP programmers coming to Haskell often assume it's "some", which would allow things like `p :: Num a => a; p = 1 :: Int`. – dfeuer Apr 29 '21 at 20:07
10

a => a doesn't mean anything. The complete phrase is Num a => a. This means "a" is an instance of the Num type class.

You could also read it as (Num a) => a. It provides a context to say that a is numerical.
However, it is not the type of a; it just says that a should be in the Num type class .
Type classes are a little like interfaces in object-oriented programming, in that they define certain behaviours, without defining a in detail.

Note that there is a difference between -> and => . The first is used for the function signature; the second is used to show type classes.

MasterMastic
  • 20,711
  • 12
  • 68
  • 90
  • I didn't get it. The type of `a` is `Num`. But why we need to say `=> a` then? – Finkelson Jan 24 '16 at 11:06
  • For me `Num a => a` is something like this: `a` has type `Num` and it is an `a`. It's a fuzzy. – Finkelson Jan 24 '16 at 11:07
  • 5
    `Num a` is telling you something about `a`. `Num` is a type class, not a type. A little like an interface in object-oriented programming, it defines some conditions that `a` should fulfill without defining `a` in detail. – S.L. Barth is on codidact.com Jan 24 '16 at 11:17
5

The typing 1 :: Num a => a means "1 is of type a, for all types a in typeclass Num". More succintly, it means "1 is of any numeric type".

Because of this typing, you can pass 1 to any function requiring any numeric type, such as Int, Double, etc.

Extending your example a bit, we also have [1,2,3] :: Num a => [a]. This means "[1,2,3] is a list of values of type a, for all types a in typeclass Num". In other words, "[1,2,3] is a list of values of any numeric type".

chi
  • 111,837
  • 3
  • 133
  • 218
0

Expression 1::Num a => a can be broken down in 3 parts for reading/understanding purposes.

Let's build step by step :

1 :: says 1 has the type of

c :: Char says c has the type of Char

Everything before => is a "class constraint",so inserting Num a between :: and =>

i.e. 1 :: Num a => a says "1 has the type of a, but not just any type but with a class constraint where a is a member of Num class.

user1278577
  • 325
  • 4
  • 7