为列表输入签名类型等(Type signature types for lists, etc)

编程入门 行业动态 更新时间:2024-10-21 11:44:23
为列表输入签名类型等(Type signature types for lists, etc)

你如何定义以下简单英文的类型签名:

Ord a => ... Eq a => ... Num a => ...

你能描述一下这些意义吗?让我知道它们之间的区别是什么(就我将如何向其他人解释)而言?

谢谢。

How would you define the following type signatures in plain english:

Ord a => ... Eq a => ... Num a => ...

Could you describe the meaning of these and let me know what the differences are (in terms of how I would explain it to someone else)?

Thanks.

最满意答案

这些都是“类约束”的例子:它们约束哪些类型可以用来代替它们后面的类型变量(在这种情况下为a),要求它属于特定类型类 。 Ord , Eq和Num是类型类的例子。

Ord a => ...表示a是一种具有与之相关的自然顺序概念的类型。 例如,整数可以自然地从小到大排列。 用数学术语来说,a上总有一个顺序 。 一个需要这个约束的函数的明显例子是sort :: Ord a => [a] -> [a] ; 阅读这个签名的时候说, sort只适用于可以按顺序排列的事物列表。

Eq a => ...意味着a是一种类型,其成员可以相互比较以得到某种平等的概念。 用数学术语来说,在a上存在一个等价关系 。 请注意,这是Ord的超类,意味着任何具有排序概念的东西都必须具有等价的概念。 一个需要这个约束的函数的例子是elem :: Eq a => a -> [a] -> Bool (它决定列表是否包含给定的元素); 阅读这个签名,说elem只能用于可以相互比较以获得平等的事物清单。 如果你考虑如何自己写elem ,那应该是有道理的。

Num a => ...表示a是一个数字类型,这意味着它支持一些基本的算术运算: + , * , - , abs 。 我相信这与戒指的数学概念大致相似。 基本上所有你认为是“数字类型”的类型都属于这个类: Int , Double等。如果函数被写入一般的任何类型的数字,你会在签名前看到Num a =>约束。 例如, sum :: Num a => [a] -> a ,它将数字列表中的所有元素相加,可以在[Int] , [Double] , [Rational]等方面同样适用... all它所要做的就是把它的内容加起来,不管它们是什么样的数字。 但他们必须是数字!

基本上,这些类型/约束是一种“功能有效超载”的方法。 我们可以在各种类型上使用(==) :: Eq a => a -> a -> Bool ,但不仅限于任何类型 。 有些东西,比如函数,对于比较平等没有意义(也许是因为平等对于这种类型是不可判定的),并且将两种不同类型的东西进行平等比较是没有意义的(与Java对比,你可以比较任何两个可能不同类型的对象是否相等)。

为了进一步(非常容易理解)阅读类型类和约束,我强烈建议学习你一个Haskell 。

These are all examples of "class constraints": they constrain what types can be used in place of the type variable which follows them (a in this case), requiring that it belong to a particular type class. Ord, Eq and Num are examples of type classes.

Ord a => ... means a is a type which has a natural notion of order associated with it. For example, integers can be naturally arranged from smaller to larger. In mathematical terms, there exists a total order on a. An obvious example a function which requires this constraint is sort :: Ord a => [a] -> [a]; read this signature as saying that sort only works on lists of things which can be put in order relative to one another.

Eq a => ... means a is a type whose members can be compared to each other for some notion of equality. In mathematical terms, there exists an equivalence relation on a. Note that this is a superclass of Ord, meaning anything that has a notion of ordering must also have a notion of equivalence. An example of a function which requires this constraint is elem :: Eq a => a -> [a] -> Bool (which determines if a list contains a given element); read this signature as saying that elem only works on lists of things which can be compared to one another for equality. If you think about how you would write elem yourself, that should make sense.

Num a => ... means a is a numeric type, meaning it supports some basic arithmetical operations: +, *, -, abs. I believe this is roughly similar to the mathematical notion of a ring. Basically all the types you think of as "number types" belong to this class: Int, Double, etc. You would see the Num a => constraint in front of a signature if the function was written to work generically with any kind of number. For example, sum :: Num a => [a] -> a, which sums all the elements of a list of numbers, can work equally well on [Int], [Double], [Rational], etc... all it has to do is add up its contents, no matter what kind of numbers they are. But numbers they must be!

Basically, these type classes/constraints are an approach to "principled overloading" of functions. We can use (==) :: Eq a => a -> a -> Bool on various types, but not just any types. Some things, for example functions, don't make sense to compare for equality (perhaps because equality isn't decidable for that type), and it never makes sense to compare two things of different types for equality (contrast this with Java, where you can compare any two objects of possibly different types for equality).

For further (very accessible) reading on type classes and constraints, I highly recommend Learn You a Haskell.

更多推荐

本文发布于:2023-04-29 11:04:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1336325.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:类型   列表   Type   lists   types

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!