本文介绍了SML中的'a和'a有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如:

fun example (a:'a list) : list = a

将具有以下签名:

'a list -> 'a list

如果我对其定义不同但内容相同的话怎么办

What if I define it differently but with same content like

fun example (a : ''a list) : list = a

其签名为:

''a list -> ''a list

有什么区别?

推荐答案

'a这样的普通类型变量可以替换为任意类型.形式''a是所谓的平等类型变量,这意味着它只能由允许在其上使用相等运算符=(或<>)的类型替换.值.

A plain type variable like 'a can be substituted with an arbitrary type. The form ''a is a so-called equality type variable, which means that it can only be substituted by types that admit the use of the equality operator = (or <>) on their values.

例如,此功能:

fun contains(x, [])    = false
  | contains(x, y::ys) = x = y orelse contains (x, ys)

不能具有类型'a * 'a list -> bool,因为它在x上使用相等.它被赋予了更严格的类型''a * ''a list -> bool.

cannot have type 'a * 'a list -> bool because it uses equality on x. It is given the more restrictive type ''a * ''a list -> bool.

大多数类型允许相等,但某些类型不允许,例如realexn,尤其是任何函数类型t -> u.记录,元组或数据类型之类的组合类型,如果它们的所有组成部分都相同,则承认相等.

Most types allow equality, but some don't, e.g., real, exn, and in particular, any function type t -> u. Composed types like records, tuples, or datatypes admit equality if all their components do.

侧面说明:Haskell后来将此概念推广到其类型类的概念,该概念允许对类型进行任意的用户定义的约束.相等类型变量由Eq类型类替换.

Side remark: Haskell later generalised this concept to its notion of type classes, which allows arbitrary user-defined constraints of this sort on types. Equality type variables are replaced by the Eq type class.

这篇关于SML中的'a和'a有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 03:29