`Num a =>是什么?在Haskell类型系统中意味着什么?

问题描述:

如果我强迫Haskell推断数字的类型,我会得到Num a => a.例如:

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

但是a => a是什么意思?

1 :: Num a => a表示1具有某种类型a,其中aNum类型类的实例.请注意,Num不是类型,而是类型类,它描述了各种类型的通用属性.例如,Num类型类描述数字类型,因此支持基本算术.本地机器整数类型IntNum的实例,任意大小的Integer,浮点类型Double甚至有理数类型Rational都是如此.

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.