1
在ocaml manual§7.20的GADT的基本示例中,「type a」的含義是什麼。 ? 爲什麼宣稱「eval:一個術語 - > a」還不夠?ocaml GADT:爲什麼「鍵入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)
我不是一種類型的理論家,但通過閱讀@nnarklrh給出的參考,在我看來'eval'函數需要多態遞歸,並且存在'type a。 ......部分是一個打開對它的支持的信號。如果你忽略'type',你會得到你不希望嘗試使用多態遞歸的錯誤。 –