ocaml GADT:为什么;a类;需要?
在的§7.20中的GADT基本示例中,“a类”的含义是什么? 为什么仅仅声明“eval:a term->a”是不够的ocaml GADT:为什么;a类;需要?,ocaml,gadt,locally-abstract-type,Ocaml,Gadt,Locally Abstract Type,在的§7.20中的GADT基本示例中,“a类”的含义是什么? 为什么仅仅声明“eval:a term->a”是不够的 type _ term = | Int : int -> int term | Add : (int -> int -> int) term | App : ('b -> 'a) term * 'b term -> 'a term let rec eval : type a
type _ term =
| Int : int -> int term
| Add : (int -> int -> int) term
| App : ('b -> 'a) term * 'b term -> 'a term
let rec eval : type a. a term -> a = function
| Int n -> n (* a = int *)
| Add -> (fun x y -> x+y) (* a = int -> int -> int *)
| App(f,x) -> (eval f) (eval x)
Jacque2011年ML研讨会有一个很好的介绍。使用局部抽象类型语法来引入通用表达式范围变量的想法。我不是类型理论家,但通过@nnarklrh给出的参考,在我看来,
eval
函数需要多态递归,类型a的存在代码>在某种程度上是开启对它的支持的信号。如果省略类型
,则在不允许的情况下尝试使用多态递归会出现错误。