简体   繁体   English

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

[英]What does `Num a => a` mean in Haskell type system?

If I force Haskell to infer the type of a number I'll get Num a => a .如果我强制 Haskell 推断数字的类型,我会得到Num a => a For example:例如:

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

But what does a => a mean?但是a => a是什么意思?

1:: Num a => a means that 1 has some type a , where a is an instance of the Num typeclass. 1:: Num a => a表示1具有某种类型a ,其中aNum类型类的一个实例。 Note that Num is not a type, but a typeclass, which describes common properties of various types.注意Num不是类型,而是类型类,它描述了各种类型的公共属性。 The Num typeclass, for example, describes types that are numeric, and so support basic arithmetic.例如, Num类型类描述了数字类型,因此支持基本算术。 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 .本机整数类型IntNum的实例,任意大小的Integer 、浮点类型Double甚至有理数类型Rational也是。

a => a doesn't mean anything. a => a没有任何意义。 The complete phrase is Num a => a .完整的短语是Num a => a This means "a" is an instance of the Num type class.这意味着“a”是Num类型类的一个实例。

You could also read it as (Num a) => a .您也可以将其读作(Num a) => a It provides a context to say that a is numerical.它提供了一个上下文来说明a是数字。
However, it is not the type of a ;但是,它不是a类型 it just says that a should be in the Num type class .它只是说a应该在Num类型中。
Type classes are a little like interfaces in object-oriented programming, in that they define certain behaviours, without defining a in detail.类型类有点像面向对象编程中的接口,因为它们定义a某些行为,但没有详细定义。

Note that there is a difference between -> and => .请注意->=>之间是有区别的。 The first is used for the function signature;第一个用于函数签名; the second is used to show type classes.第二个用于显示类型类。

The typing 1:: Num a => a means " 1 is of type a , for all types a in typeclass Num ".输入1:: Num a => a意味着“ 1是类型a ,对于类型类Num中的所有类型a ”。 More succintly, it means " 1 is of any numeric type".更简洁地说,它意味着“ 1是任何数字类型”。

Because of this typing, you can pass 1 to any function requiring any numeric type, such as Int , Double , etc.由于这种类型,您可以将1传递给任何需要任何数字类型的函数,例如IntDouble等。

Extending your example a bit, we also have [1,2,3]:: Num a => [a] .稍微扩展您的示例,我们还有[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 ".这意味着“ [1,2,3]是类型a的值列表,对于类型类Num中的所有类型a ”。 In other words, " [1,2,3] is a list of values of any numeric type".换句话说,“ [1,2,3]是任何数字类型的值列表”。

Expression 1::Num a => a can be broken down in 3 parts for reading/understanding purposes.表达式1::Num a => a可以分解为 3 个部分以便于阅读/理解。

Let's build step by step:让我们一步步构建:

1:: says 1 has the type of 1::表示1 的类型为

c:: Char says c has the type of Char c:: Char表示c 的类型为 Char

Everything before => is a "class constraint" ,so inserting Num a between:: and => => 之前的所有内容都是“类约束” ,因此在 :: 和 => 之间插入Num a

ie 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.1:: Num a => a表示“1 具有a的类型,但不仅是任何类型,而且具有类约束,其中a是 Num 类的成员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM