Ant*_*ine 5 syntax ocaml gadt locally-abstract-type
我刚刚在OCaml的有关GADT的文档中遇到了以下代码片段:
let rec eval : type a. a term -> a = function
| Int n -> n
| Add -> (fun x y -> x + y)
| App (f, x) -> (eval f) (eval x)
Run Code Online (Sandbox Code Playgroud)
一旦在utop中进行了评估,它就具有以下签名:
val eval : 'a term -> 'a = <fun>
Run Code Online (Sandbox Code Playgroud)
我还注意到,当替换type a. a term -> a为'a term -> 'a或仅删除签名时,该函数不再编译。
...
| Add -> (fun x y -> x + y)
...
Error: This pattern matches values of type (int -> int -> int) term
but a pattern was expected which matches values of type int term
Type int -> int -> int is not compatible with type int
Run Code Online (Sandbox Code Playgroud)
那么这个表示法是什么呢?有什么不同'a t呢?
它特定于GADT吗?
该手册解释了语法的几个部分:http://caml.inria.fr/pub/docs/manual-ocaml-400/manual021.html#toc80
简而言之,type a. ...意味着局部抽象类型a必须是多态的。