【问题标题】:What's meaning of this ocaml code segment?这个 ocaml 代码段是什么意思?
【发布时间】:2018-11-06 08:42:42
【问题描述】:

我无法从 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


    【解决方案1】:

    开始于:

        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 第 52 行也是如此。

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

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

    【讨论】:

      猜你喜欢
      • 2016-03-28
      • 1970-01-01
      • 1970-01-01
      • 2016-12-16
      相关资源
      最近更新 更多