簡體   English   中英

OCaml的`類型a。 at`語法

[英]OCaml's `type a. a t` syntax

我剛剛在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)

一旦在utop評估,它具有以下簽名:

val eval : 'a term -> 'a = <fun>

我也注意到,當替換type a. a term -> a 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 

那么這個符號是什么? 是什么讓它與'at

它是否特定於GADT?

該手冊解釋了幾個部分的語法: http//caml.inria.fr/pub/docs/manual-ocaml-400/manual021.html#toc80

簡而言之, type a. ... type a. ...意味着本地抽象類型a必須是多態的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM