首页 > 解决方案 > 这个 ocaml 代码段是什么意思?

问题描述

我无法从 ocaml 编译器源代码中理解这个 ocaml 代码:

File: d:\src\ocaml-4.07.0\driver\pparse.ml
50: type 'a ast_kind =
51: | Structure : Parsetree.structure ast_kind
52: | Signature : Parsetree.signature ast_kind

有定义一个类型ast_kind,定义类型参数'a,但不使用它?

我知道类型定义的常见用法是这样的:

type a=
|A of int
|B of int

所以

Structure : Parsetree.structure ast_kind

是什么意思?Structure 的类型是 Parsetree.structure?还是 ast_kind?

我阅读了官方文档: http ://caml.inria.fr/pub/docs/manual-ocaml-312/manual016.html#@manual.kwd53

它告诉我只有在记录的定义中才能使用“:”

type-representation ::= = constr-decl  { | constr-decl }   
                     ∣  = { field-decl  { ; field-decl } }

field-decl ::= field-name :  poly-typexpr   
           ∣  mutable field-name :  poly-typexpr 

那么这段代码是什么意思呢?谢谢!

标签: ocaml

解决方案


从...开始 :

    50: type 'a ast_kind =
    51: | Structure : Parsetree.structure ast_kind
    52: | Signature : Parsetree.signature ast_kind

内容如下:

第 50 行:我们定义了一个参数化类型ast_kind,其参数为'a. 该参数稍后在第 51 行和第 52 行中定义。在第 51 行:'a参数类型是Parsetree.structure And 与第 52 行类似。

现在,更一般地说,ast_kind是 GADT 类型(广义代数数据类型),请参阅GADT 手册和另一个示例:Mads-hartmann

请注意,GADT 已在 Ocaml 4.00 中引入 - 因此您引用的有关文档的链接对于该特定功能已过时,因为它指的是 Ocaml 3.12。您当前正在检查 Ocaml 4.07 的源代码。


推荐阅读