wang kai
wang kai

Reputation: 1747

What's meaning of this ocaml code segment?

I can't understand this ocaml code from ocaml compiler source code:

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

there define a type ast_kind,define the type parameter 'a,but not use it?

I know the common use of type define like this:

type a=
|A of int
|B of int

so the

Structure : Parsetree.structure ast_kind

means what?the type of Structure is Parsetree.structure? or ast_kind?

I read the offical doc: http://caml.inria.fr/pub/docs/manual-ocaml-312/manual016.html#@manual.kwd53

it tell me only in the defination of record can use the ":"

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

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

so what's the meaning of this code segment?Thanks!

Upvotes: 1

Views: 62

Answers (1)

Pierre G.
Pierre G.

Reputation: 4441

Starting from :

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

This is read as follows :

line 50 : we define a parametrized type ast_kind whose parameter is 'a. The parameter is defined later in the lines 51 & 52. On line 51 : the 'a parameter type is Parsetree.structure And similarly for line 52.

Now, more generally, ast_kind is a GADT type (generalized algebraic datatypes), see GADT-manual and another example : Mads-hartmann.

Note that GADT has been introduced in Ocaml 4.00 - so the link you quote regarding documentation is outdated for that particular feature as it refers to Ocaml 3.12. You are currently inspecting the source code of Ocaml 4.07.

Upvotes: 2

Related Questions