What is the difference between 'a and ''a in SML?

风流意气都作罢 提交于 2019-12-22 10:58:24

问题


For example:

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

will have a signatures of:

'a list -> 'a list 

What if I define it differently but with same content like

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

its signature will be:

''a list -> ''a list

What's the difference?


回答1:


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.

For example, this function:

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

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

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.

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.



来源:https://stackoverflow.com/questions/20437520/what-is-the-difference-between-a-and-a-in-sml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!