Types ocaml类型是什么';a';a->';是什么意思?

Types ocaml类型是什么';a';a->';是什么意思?,types,polymorphism,ocaml,Types,Polymorphism,Ocaml,在ocaml语言规范中,有一小段: poly-typexpr ::= typexpr | { ' ident }+ . typexpr 文本中没有解释,poly-typexpr的唯一实例是定义方法类型: method-type ::= method-name : poly-typexpr 这允许我做什么?请参阅。向下滚动到“当然,约束也可能是显式方法类型…”poly-typexpr也可以作为记录字段的类型(请参阅)。这些通常被称为“存在类型”,尽管有。以这种方式

在ocaml语言规范中,有一小段:

poly-typexpr ::= typexpr
               | { ' ident }+ . typexpr
文本中没有解释,
poly-typexpr
的唯一实例是定义方法类型:

method-type ::= method-name : poly-typexpr

这允许我做什么?

请参阅。向下滚动到“当然,约束也可能是显式方法类型…”

poly-typexpr
也可以作为记录字段的类型(请参阅)。这些通常被称为“存在类型”,尽管有。以这种方式使用多态类型会更改类型变量的范围。例如,比较以下类型:

type 'a t = { f : 'a -> int; }
type u = { g : 'a. 'a -> int; }
t
实际上是一系列类型,每个类型对应于
'a
的可能值。类型为
'a t
的每个值必须有一个类型为
'a->int
的字段
f
。例如:

# let x = { f = fun i -> i+1; } ;;
val x : int t = {f = <fun>}
# let y = { f = String.length; } ;;
val y : string t = {f = <fun>}
# let z = { g = fun _ -> 0; } ;;
val z : u = {g = <fun>}
# let x2 = { g = fun i -> i+1; } ;;
This field value has type int -> int which is less general than 'a. 'a -> int
请注意,
g
完全不依赖于其输入的类型;如果有,它就不会有
'a'类型a->int
。例如:

# let x = { f = fun i -> i+1; } ;;
val x : int t = {f = <fun>}
# let y = { f = String.length; } ;;
val y : string t = {f = <fun>}
# let z = { g = fun _ -> 0; } ;;
val z : u = {g = <fun>}
# let x2 = { g = fun i -> i+1; } ;;
This field value has type int -> int which is less general than 'a. 'a -> int