Parsetree(3) OCaml library Parsetree(3)

# [NAME](#NAME)

Parsetree - Abstract syntax tree produced by parsing

# [Module](#Module)

Module Parsetree

# [Documentation](#Documentation)

Module **Parsetree**  
: **sig end**

Abstract syntax tree produced by parsing

Warning: this module is unstable and part of **Compiler\_libs** .

*type constant* =  
| Pconst\_integer **of** **string * char option**  
(* Integer constants such as **3** **3l** **3L** **3n** .

Suffixes **\[g-z]\[G-Z]** are accepted by the parser. Suffixes except **'l'** , **'L'** and **'n'** are rejected by the typechecker  
\*)  
| Pconst\_char **of** **char**  
(* Character such as **'c'** .  
\*)  
| Pconst\_string **of** **string * Location.t * string option**  
(* Constant string such as **"constant"** or **{delim|other constant|delim}** .

The location span the content of the string, without the delimiters.  
\*)  
| Pconst\_float **of** **string * char option**  
(* Float constant such as **3.4** , **2e5** or **1.4e-4** .

Suffixes **g-z** **G-Z** are accepted by the parser. Suffixes are rejected by the typechecker.  
\*)

*type location\_stack* = **Location.t list**

## [Extension points](#Extension_points)

*type attribute* = {  
attr\_name : **string Asttypes.loc** ;  
attr\_payload : **payload** ;  
attr\_loc : **Location.t** ;  
}

Attributes such as **\[@id ARG]** and **\[@@id ARG]** .

Metadata containers passed around within the AST. The compiler ignores unknown attributes.

*type extension* = **string Asttypes.loc * payload**

Extension points such as **\[%id ARG] and \[%%id ARG]** .

Sub-language placeholder -- rejected by the typechecker.

*type attributes* = **attribute list**

*type payload* =  
| PStr **of** **structure**  
| PSig **of** **signature**  
(* **: SIG** in an attribute or an extension point  
\*)  
| PTyp **of** **core\_type**  
(* **: T** in an attribute or an extension point  
\*)  
| PPat **of** **pattern * expression option**  
(* **? P** or **? P when E** , in an attribute or an extension point  
\*)

## [Core language](#Core_language)

## [Type expressions](#Type_expressions)

*type core\_type* = {  
ptyp\_desc : **core\_type\_desc** ;  
ptyp\_loc : **Location.t** ;  
ptyp\_loc\_stack : **location\_stack** ;  
ptyp\_attributes : **attributes** ; (* **... \[@id1] \[@id2]**

\*)  
}

*type core\_type\_desc* =  
| Ptyp\_any (* **\_**

\*)  
| Ptyp\_var **of** **string**  
(* A type variable such as **'a**

\*)  
| Ptyp\_arrow **of** **Asttypes.arg\_label * core\_type * core\_type**  
(* **Ptyp\_arrow(lbl, T1, T2)** represents:

\- **T1 -&gt; T2** when **lbl** is **Asttypes.arg\_label.Nolabel** ,

\- **~l:T1 -&gt; T2** when **lbl** is **Asttypes.arg\_label.Labelled** ,

\- **?l:T1 -&gt; T2** when **lbl** is **Asttypes.arg\_label.Optional** .

\*)  
| Ptyp\_tuple **of** **core\_type list**  
(* **Ptyp\_tuple(\[T1 ; ... ; Tn])** represents a product type **T1 * ... * Tn** .

Invariant: **n &gt;= 2** .  
\*)  
| Ptyp\_constr **of** **Longident.t Asttypes.loc * core\_type list**  
(* **Ptyp\_constr(lident, l)** represents:

\- **tconstr** when **l=\[]** ,

\- **T tconstr** when **l=\[T]** ,

\- **(T1, ..., Tn) tconstr** when **l=\[T1 ; ... ; Tn]** .

\*)  
| Ptyp\_object **of** **object\_field list * Asttypes.closed\_flag**  
(* **Ptyp\_object(\[ l1:T1; ...; ln:Tn ], flag)** represents:

\- **&lt; l1:T1; ...; ln:Tn &gt;** when **flag** is **Asttypes.closed\_flag.Closed** ,

\- **&lt; l1:T1; ...; ln:Tn; .. &gt;** when **flag** is **Asttypes.closed\_flag.Open** .

\*)  
| Ptyp\_class **of** **Longident.t Asttypes.loc * core\_type list**  
(* **Ptyp\_class(tconstr, l)** represents:

\- **#tconstr** when **l=\[]** ,

\- **T #tconstr** when **l=\[T]** ,

\- **(T1, ..., Tn) #tconstr** when **l=\[T1 ; ... ; Tn]** .

\*)  
| Ptyp\_alias **of** **core\_type * string**  
(* **T as 'a** .  
\*)  
| Ptyp\_variant **of** **row\_field list * Asttypes.closed\_flag * Asttypes.label list option**  
(* **Ptyp\_variant(\[\`A;\`B], flag, labels)** represents:

\- **\[ \`A|\`B ]** when **flag** is **Asttypes.closed\_flag.Closed** , and **labels** is **None** ,

\- **\[&gt; \`A|\`B ]** when **flag** is **Asttypes.closed\_flag.Open** , and **labels** is **None** ,

\- **\[&lt; \`A|\`B ]** when **flag** is **Asttypes.closed\_flag.Closed** , and **labels** is **Some \[]** ,

\- **\[&lt; \`A|\`B &gt; \`X \`Y ]** when **flag** is **Asttypes.closed\_flag.Closed** , and **labels** is **Some \["X";"Y"]** .

\*)  
| Ptyp\_poly **of** **string Asttypes.loc list * core\_type**  
(* **'a1 ... 'an. T**

Can only appear in the following context:

-As the **Parsetree.core\_type** of a **Parsetree.pattern\_desc.Ppat\_constraint** node corresponding to a constraint on a let-binding:

```
let x : 'a1 ... 'an. T = e ...
```

-Under **Parsetree.class\_field\_kind.Cfk\_virtual** for methods (not values).

-As the **Parsetree.core\_type** of a **Parsetree.class\_type\_field\_desc.Pctf\_method** node.

-As the **Parsetree.core\_type** of a **Parsetree.expression\_desc.Pexp\_poly** node.

-As the **Parsetree.label\_declaration.pld\_type** field of a **Parsetree.label\_declaration** .

-As a **Parsetree.core\_type** of a **Parsetree.core\_type\_desc.Ptyp\_object** node.

-As the **Parsetree.value\_description.pval\_type** field of a **Parsetree.value\_description** .

\*)  
| Ptyp\_package **of** **package\_type**  
(* **(module S)** .  
\*)  
| Ptyp\_extension **of** **extension**  
(* **\[%id]** .  
\*)

*type package\_type* = **Longident.t Asttypes.loc \*** **(Longident.t Asttypes.loc * core\_type) list**

As **Parsetree.package\_type** typed values:

\- **(S, \[])** represents **(module S)** ,

\- **(S, \[(t1, T1) ; ... ; (tn, Tn)])** represents **(module S with type t1 = T1 and ... and tn = Tn)** .

*type row\_field* = {  
prf\_desc : **row\_field\_desc** ;  
prf\_loc : **Location.t** ;  
prf\_attributes : **attributes** ;  
}

*type row\_field\_desc* =  
| Rtag **of** **Asttypes.label Asttypes.loc * bool * core\_type list**  
(* **Rtag(\`A, b, l)** represents:

\- **\`A** when **b** is **true** and **l** is **\[]** ,

\- **\`A of T** when **b** is **false** and **l** is **\[T]** ,

\- **\`A of T1 & .. & Tn** when **b** is **false** and **l** is **\[T1;...Tn]** ,

\- **\`A of & T1 & .. & Tn** when **b** is **true** and **l** is **\[T1;...Tn]** .

-The **bool** field is true if the tag contains a constant (empty) constructor.

\- **&** occurs when several types are used for the same constructor (see 4.2 in the manual)

\*)  
| Rinherit **of** **core\_type**  
(* **\[ | t ]**

\*)

*type object\_field* = {  
pof\_desc : **object\_field\_desc** ;  
pof\_loc : **Location.t** ;  
pof\_attributes : **attributes** ;  
}

*type object\_field\_desc* =  
| Otag **of** **Asttypes.label Asttypes.loc * core\_type**  
| Oinherit **of** **core\_type**

## [Patterns](#Patterns)

*type pattern* = {  
ppat\_desc : **pattern\_desc** ;  
ppat\_loc : **Location.t** ;  
ppat\_loc\_stack : **location\_stack** ;  
ppat\_attributes : **attributes** ; (* **... \[@id1] \[@id2]**

\*)  
}

*type pattern\_desc* =  
| Ppat\_any (* The pattern **\_** .  
\*)  
| Ppat\_var **of** **string Asttypes.loc**  
(* A variable pattern such as **x**

\*)  
| Ppat\_alias **of** **pattern * string Asttypes.loc**  
(* An alias pattern such as **P as 'a**

\*)  
| Ppat\_constant **of** **constant**  
(* Patterns such as **1** , **'a'** , **"true"** , **1.0** , **1l** , **1L** , **1n**

\*)  
| Ppat\_interval **of** **constant * constant**  
(* Patterns such as **'a'..'z'** .

Other forms of interval are recognized by the parser but rejected by the type-checker.  
\*)  
| Ppat\_tuple **of** **pattern list**  
(* Patterns **(P1, ..., Pn)** .

Invariant: **n &gt;= 2**

\*)  
| Ppat\_construct **of** **Longident.t Asttypes.loc** *** (string Asttypes.loc list * pattern) option**  
(* **Ppat\_construct(C, args)** represents:

\- **C** when **args** is **None** ,

\- **C P** when **args** is **Some (\[], P)**

\- **C (P1, ..., Pn)** when **args** is **Some (\[], Ppat\_tuple \[P1; ...; Pn])**

\- **C (type a b) P** when **args** is **Some (\[a; b], P)**

\*)  
| Ppat\_variant **of** **Asttypes.label * pattern option**  
(* **Ppat\_variant(\`A, pat)** represents:

\- **\`A** when **pat** is **None** ,

\- **\`A P** when **pat** is **Some P**

\*)  
| Ppat\_record **of** **(Longident.t Asttypes.loc * pattern) list * Asttypes.closed\_flag**  
(* **Ppat\_record(\[(l1, P1) ; ... ; (ln, Pn)], flag)** represents:

\- **{ l1=P1; ...; ln=Pn }** when **flag** is **Asttypes.closed\_flag.Closed**

\- **{ l1=P1; ...; ln=Pn; \_}** when **flag** is **Asttypes.closed\_flag.Open**

Invariant: **n &gt; 0**

\*)  
| Ppat\_array **of** **pattern list**  
(* Pattern **\[| P1; ...; Pn |]**

\*)  
| Ppat\_or **of** **pattern * pattern**  
(* Pattern **P1 | P2**

\*)  
| Ppat\_constraint **of** **pattern * core\_type**  
(* Pattern **(P : T)**

\*)  
| Ppat\_type **of** **Longident.t Asttypes.loc**  
(* Pattern **#tconst**

\*)  
| Ppat\_lazy **of** **pattern**  
(* Pattern **lazy P**

\*)  
| Ppat\_unpack **of** **string option Asttypes.loc**  
(* **Ppat\_unpack(s)** represents:

\- **(module P)** when **s** is **Some "P"**

\- **(module \_)** when **s** is **None**

Note: **(module P : S)** is represented as **Ppat\_constraint(Ppat\_unpack(Some "P"), Ptyp\_package S)**

\*)  
| Ppat\_exception **of** **pattern**  
(* Pattern **exception P**

\*)  
| Ppat\_extension **of** **extension**  
(* Pattern **\[%id]**

\*)  
| Ppat\_open **of** **Longident.t Asttypes.loc * pattern**  
(* Pattern **M.(P)**

\*)

## [Value expressions](#Value_expressions)

*type expression* = {  
pexp\_desc : **expression\_desc** ;  
pexp\_loc : **Location.t** ;  
pexp\_loc\_stack : **location\_stack** ;  
pexp\_attributes : **attributes** ; (* **... \[@id1] \[@id2]**

\*)  
}

*type expression\_desc* =  
| Pexp\_ident **of** **Longident.t Asttypes.loc**  
(* Identifiers such as **x** and **M.x**

\*)  
| Pexp\_constant **of** **constant**  
(* Expressions constant such as **1** , **'a'** , **"true"** , **1.0** , **1l** , **1L** , **1n**

\*)  
| Pexp\_let **of** **Asttypes.rec\_flag * value\_binding list * expression**  
(* **Pexp\_let(flag, \[(P1,E1) ; ... ; (Pn,En)], E)** represents:

\- **let P1 = E1 and ... and Pn = EN in E** when **flag** is **Asttypes.rec\_flag.Nonrecursive** ,

\- **let rec P1 = E1 and ... and Pn = EN in E** when **flag** is **Asttypes.rec\_flag.Recursive** .

\*)  
| Pexp\_function **of** **case list**  
(* **function P1 -&gt; E1 | ... | Pn -&gt; En**

\*)  
| Pexp\_fun **of** **Asttypes.arg\_label * expression option * pattern** *** expression**  
(* **Pexp\_fun(lbl, exp0, P, E1)** represents:

\- **fun P -&gt; E1** when **lbl** is **Asttypes.arg\_label.Nolabel** and **exp0** is **None**

\- **fun ~l:P -&gt; E1** when **lbl** is **Asttypes.arg\_label.Labelled** and **exp0** is **None**

\- **fun ?l:P -&gt; E1** when **lbl** is **Asttypes.arg\_label.Optional** and **exp0** is **None**

\- **fun ?l:(P = E0) -&gt; E1** when **lbl** is **Asttypes.arg\_label.Optional** and **exp0** is **Some E0**

Notes:

-If **E0** is provided, only **Asttypes.arg\_label.Optional** is allowed.

\- **fun P1 P2 .. Pn -&gt; E1** is represented as nested **Parsetree.expression\_desc.Pexp\_fun** .

\- **let f P = E** is represented using **Parsetree.expression\_desc.Pexp\_fun** .

\*)  
| Pexp\_apply **of** **expression * (Asttypes.arg\_label * expression) list**  
(* **Pexp\_apply(E0, \[(l1, E1) ; ... ; (ln, En)])** represents **E0 ~l1:E1 ... ~ln:En**

**li** can be **Asttypes.arg\_label.Nolabel** (non labeled argument), **Asttypes.arg\_label.Labelled** (labelled arguments) or **Asttypes.arg\_label.Optional** (optional argument).

Invariant: **n &gt; 0**

\*)  
| Pexp\_match **of** **expression * case list**  
(* **match E0 with P1 -&gt; E1 | ... | Pn -&gt; En**

\*)  
| Pexp\_try **of** **expression * case list**  
(* **try E0 with P1 -&gt; E1 | ... | Pn -&gt; En**

\*)  
| Pexp\_tuple **of** **expression list**  
(* Expressions **(E1, ..., En)**

Invariant: **n &gt;= 2**

\*)  
| Pexp\_construct **of** **Longident.t Asttypes.loc * expression option**  
(* **Pexp\_construct(C, exp)** represents:

\- **C** when **exp** is **None** ,

\- **C E** when **exp** is **Some E** ,

\- **C (E1, ..., En)** when **exp** is **Some (Pexp\_tuple\[E1;...;En])**

\*)  
| Pexp\_variant **of** **Asttypes.label * expression option**  
(* **Pexp\_variant(\`A, exp)** represents

\- **\`A** when **exp** is **None**

\- **\`A E** when **exp** is **Some E**

\*)  
| Pexp\_record **of** **(Longident.t Asttypes.loc * expression) list** *** expression option**  
(* **Pexp\_record(\[(l1,P1) ; ... ; (ln,Pn)], exp0)** represents

\- **{ l1=P1; ...; ln=Pn }** when **exp0** is **None**

\- **{ E0 with l1=P1; ...; ln=Pn }** when **exp0** is **Some E0**

Invariant: **n &gt; 0**

\*)  
| Pexp\_field **of** **expression * Longident.t Asttypes.loc**  
(* **E.l**

\*)  
| Pexp\_setfield **of** **expression * Longident.t Asttypes.loc * expression**  
(* **E1.l &lt;- E2**

\*)  
| Pexp\_array **of** **expression list**  
(* **\[| E1; ...; En |]**

\*)  
| Pexp\_ifthenelse **of** **expression * expression * expression option**  
(* **if E1 then E2 else E3**

\*)  
| Pexp\_sequence **of** **expression * expression**  
(* **E1; E2**

\*)  
| Pexp\_while **of** **expression * expression**  
(* **while E1 do E2 done**

\*)  
| Pexp\_for **of** **pattern * expression * expression** *** Asttypes.direction\_flag * expression**  
(* **Pexp\_for(i, E1, E2, direction, E3)** represents:

\- **for i = E1 to E2 do E3 done** when **direction** is **Asttypes.direction\_flag.Upto**

\- **for i = E1 downto E2 do E3 done** when **direction** is **Asttypes.direction\_flag.Downto**

\*)  
| Pexp\_constraint **of** **expression * core\_type**  
(* **(E : T)**

\*)  
| Pexp\_coerce **of** **expression * core\_type option * core\_type**  
(* **Pexp\_coerce(E, from, T)** represents

\- **(E :&gt; T)** when **from** is **None** ,

\- **(E : T0 :&gt; T)** when **from** is **Some T0** .

\*)  
| Pexp\_send **of** **expression * Asttypes.label Asttypes.loc**  
(* **E # m**

\*)  
| Pexp\_new **of** **Longident.t Asttypes.loc**  
(* **new M.c**

\*)  
| Pexp\_setinstvar **of** **Asttypes.label Asttypes.loc * expression**  
(* **x &lt;- 2**

\*)  
| Pexp\_override **of** **(Asttypes.label Asttypes.loc * expression) list**  
(* **{&lt; x1 = E1; ...; xn = En &gt;}**

\*)  
| Pexp\_letmodule **of** **string option Asttypes.loc * module\_expr * expression**  
(* **let module M = ME in E**

\*)  
| Pexp\_letexception **of** **extension\_constructor * expression**  
(* **let exception C in E**

\*)  
| Pexp\_assert **of** **expression**  
(* **assert E** .

Note: **assert false** is treated in a special way by the type-checker.  
\*)  
| Pexp\_lazy **of** **expression**  
(* **lazy E**

\*)  
| Pexp\_poly **of** **expression * core\_type option**  
(* Used for method bodies.

Can only be used as the expression under **Parsetree.class\_field\_kind.Cfk\_concrete** for methods (not values).  
\*)  
| Pexp\_object **of** **class\_structure**  
(* **object ... end**

\*)  
| Pexp\_newtype **of** **string Asttypes.loc * expression**  
(* **fun (type t) -&gt; E**

\*)  
| Pexp\_pack **of** **module\_expr**  
(* **(module ME)** .

**(module ME : S)** is represented as **Pexp\_constraint(Pexp\_pack ME, Ptyp\_package S)**

\*)  
| Pexp\_open **of** **open\_declaration * expression**  
(* - **M.(E)**

\- **let open M in E**

\- **let open! M in E**

\*)  
| Pexp\_letop **of** **letop**  
(* - **let* P = E0 in E1**

\- **let* P0 = E00 and* P1 = E01 in E1**

\*)  
| Pexp\_extension **of** **extension**  
(* **\[%id]**

\*)  
| Pexp\_unreachable (* **.**

\*)

*type case* = {  
pc\_lhs : **pattern** ;  
pc\_guard : **expression option** ;  
pc\_rhs : **expression** ;  
}

Values of type **Parsetree.case** represents **(P -&gt; E)** or **(P when E0 -&gt; E)**

*type letop* = {  
let_ : **binding\_op** ;  
ands : **binding\_op list** ;  
body : **expression** ;  
}

*type binding\_op* = {  
pbop\_op : **string Asttypes.loc** ;  
pbop\_pat : **pattern** ;  
pbop\_exp : **expression** ;  
pbop\_loc : **Location.t** ;  
}

## [Value descriptions](#Value_descriptions)

*type value\_description* = {  
pval\_name : **string Asttypes.loc** ;  
pval\_type : **core\_type** ;  
pval\_prim : **string list** ;  
pval\_attributes : **attributes** ; (* **... \[@@id1] \[@@id2]**

\*)  
pval\_loc : **Location.t** ;  
}

Values of type **Parsetree.value\_description** represents:

\- **val x: T** , when **Parsetree.value\_description.pval\_prim** is **\[]**

\- **external x: T = "s1" ... "sn"** when **Parsetree.value\_description.pval\_prim** is **\["s1";..."sn"]**

## [Type declarations](#Type_declarations)

*type type\_declaration* = {  
ptype\_name : **string Asttypes.loc** ;  
ptype\_params : **(core\_type * (Asttypes.variance * Asttypes.injectivity)) list** ; (* **('a1,...'an) t**

\*)  
ptype\_cstrs : **(core\_type * core\_type * Location.t) list** ; (* **... constraint T1=T1' ... constraint Tn=Tn'**

\*)  
ptype\_kind : **type\_kind** ;  
ptype\_private : **Asttypes.private\_flag** ; (* for **= private ...**

\*)  
ptype\_manifest : **core\_type option** ; (* represents **= T**

\*)  
ptype\_attributes : **attributes** ; (* **... \[@@id1] \[@@id2]**

\*)  
ptype\_loc : **Location.t** ;  
}

Here are type declarations and their representation, for various **Parsetree.type\_declaration.ptype\_kind** and **Parsetree.type\_declaration.ptype\_manifest** values:

\- **type t** when **type\_kind** is **Parsetree.type\_kind.Ptype\_abstract** , and **manifest** is **None** ,

\- **type t = T0** when **type\_kind** is **Parsetree.type\_kind.Ptype\_abstract** , and **manifest** is **Some T0** ,

\- **type t = C of T | ...** when **type\_kind** is **Parsetree.type\_kind.Ptype\_variant** , and **manifest** is **None** ,

\- **type t = T0 = C of T | ...** when **type\_kind** is **Parsetree.type\_kind.Ptype\_variant** , and **manifest** is **Some T0** ,

\- **type t = {l: T; ...}** when **type\_kind** is **Parsetree.type\_kind.Ptype\_record** , and **manifest** is **None** ,

\- **type t = T0 = {l : T; ...}** when **type\_kind** is **Parsetree.type\_kind.Ptype\_record** , and **manifest** is **Some T0** ,

\- **type t = ..** when **type\_kind** is **Parsetree.type\_kind.Ptype\_open** , and **manifest** is **None** .

*type type\_kind* =  
| Ptype\_abstract  
| Ptype\_variant **of** **constructor\_declaration list**  
| Ptype\_record **of** **label\_declaration list**  
(* Invariant: non-empty list  
\*)  
| Ptype\_open

*type label\_declaration* = {  
pld\_name : **string Asttypes.loc** ;  
pld\_mutable : **Asttypes.mutable\_flag** ;  
pld\_type : **core\_type** ;  
pld\_loc : **Location.t** ;  
pld\_attributes : **attributes** ; (* **l : T \[@id1] \[@id2]**

\*)  
}

\- **{ ...; l: T; ... }** when **Parsetree.label\_declaration.pld\_mutable** is **Asttypes.mutable\_flag.Immutable** ,

\- **{ ...; mutable l: T; ... }** when **Parsetree.label\_declaration.pld\_mutable** is **Asttypes.mutable\_flag.Mutable** .

Note: **T** can be a **Parsetree.core\_type\_desc.Ptyp\_poly** .

*type constructor\_declaration* = {  
pcd\_name : **string Asttypes.loc** ;  
pcd\_vars : **string Asttypes.loc list** ;  
pcd\_args : **constructor\_arguments** ;  
pcd\_res : **core\_type option** ;  
pcd\_loc : **Location.t** ;  
pcd\_attributes : **attributes** ; (* **C of ... \[@id1] \[@id2]**

\*)  
}

*type constructor\_arguments* =  
| Pcstr\_tuple **of** **core\_type list**  
| Pcstr\_record **of** **label\_declaration list**  
(* Values of type **Parsetree.constructor\_declaration** represents the constructor arguments of:

\- **C of T1 * ... * Tn** when **res = None** , and **args = Pcstr\_tuple \[T1; ... ; Tn]** ,

\- **C: T0** when **res = Some T0** , and **args = Pcstr\_tuple \[]** ,

\- **C: T1 * ... * Tn -&gt; T0** when **res = Some T0** , and **args = Pcstr\_tuple \[T1; ... ; Tn]** ,

\- **C of {...}** when **res = None** , and **args = Pcstr\_record \[...]** ,

\- **C: {...} -&gt; T0** when **res = Some T0** , and **args = Pcstr\_record \[...]** .

\*)

*type type\_extension* = {  
ptyext\_path : **Longident.t Asttypes.loc** ;  
ptyext\_params : **(core\_type * (Asttypes.variance * Asttypes.injectivity)) list** ;  
ptyext\_constructors : **extension\_constructor list** ;  
ptyext\_private : **Asttypes.private\_flag** ;  
ptyext\_loc : **Location.t** ;  
ptyext\_attributes : **attributes** ; (* ... **@@id1** **@@id2**

\*)  
}

Definition of new extensions constructors for the extensive sum type **t** ( **type t += ...** ).

*type extension\_constructor* = {  
pext\_name : **string Asttypes.loc** ;  
pext\_kind : **extension\_constructor\_kind** ;  
pext\_loc : **Location.t** ;  
pext\_attributes : **attributes** ; (* **C of ... \[@id1] \[@id2]**

\*)  
}

*type type\_exception* = {  
ptyexn\_constructor : **extension\_constructor** ;  
ptyexn\_loc : **Location.t** ;  
ptyexn\_attributes : **attributes** ; (* **... \[@@id1] \[@@id2]**

\*)  
}

Definition of a new exception ( **exception E** ).

*type extension\_constructor\_kind* =  
| Pext\_decl **of** **string Asttypes.loc list * constructor\_arguments** *** core\_type option**  
(* **Pext\_decl(existentials, c\_args, t\_opt)** describes a new extension constructor. It can be:

\- **C of T1 * ... * Tn** when:

\- **existentials** is **\[]** ,

\- **c\_args** is **\[T1; ...; Tn]** ,

\- **t\_opt** is **None**

\- **C: T0** when

\- **existentials** is **\[]** ,

\- **c\_args** is **\[]** ,

\- **t\_opt** is **Some T0** .

\- **C: T1 * ... * Tn -&gt; T0** when

\- **existentials** is **\[]** ,

\- **c\_args** is **\[T1; ...; Tn]** ,

\- **t\_opt** is **Some T0** .

\- **C: 'a... . T1 * ... * Tn -&gt; T0** when

\- **existentials** is **\['a;...]** ,

\- **c\_args** is **\[T1; ... ; Tn]** ,

\- **t\_opt** is **Some T0** .

\*)  
| Pext\_rebind **of** **Longident.t Asttypes.loc**  
(* **Pext\_rebind(D)** re-export the constructor **D** with the new name **C**

\*)

## [Class language](#Class_language)

## [Type expressions for the class language](#Type_expressions_for_the_class_language)

*type class\_type* = {  
pcty\_desc : **class\_type\_desc** ;  
pcty\_loc : **Location.t** ;  
pcty\_attributes : **attributes** ; (* **... \[@id1] \[@id2]**

\*)  
}

*type class\_type\_desc* =  
| Pcty\_constr **of** **Longident.t Asttypes.loc * core\_type list**  
(* - **c**

\- **\['a1, ..., 'an] c**

\*)  
| Pcty\_signature **of** **class\_signature**  
(* **object ... end**

\*)  
| Pcty\_arrow **of** **Asttypes.arg\_label * core\_type * class\_type**  
(* **Pcty\_arrow(lbl, T, CT)** represents:

\- **T -&gt; CT** when **lbl** is **Asttypes.arg\_label.Nolabel** ,

\- **~l:T -&gt; CT** when **lbl** is **Asttypes.arg\_label.Labelled** ,

\- **?l:T -&gt; CT** when **lbl** is **Asttypes.arg\_label.Optional** .

\*)  
| Pcty\_extension **of** **extension**  
(* **%id**

\*)  
| Pcty\_open **of** **open\_description * class\_type**  
(* **let open M in CT**

\*)

*type class\_signature* = {  
pcsig\_self : **core\_type** ;  
pcsig\_fields : **class\_type\_field list** ;  
}

Values of type **class\_signature** represents:

\- **object('selfpat) ... end**

\- **object ... end** when **Parsetree.class\_signature.pcsig\_self** is **Parsetree.core\_type\_desc.Ptyp\_any**

*type class\_type\_field* = {  
pctf\_desc : **class\_type\_field\_desc** ;  
pctf\_loc : **Location.t** ;  
pctf\_attributes : **attributes** ; (* **... \[@@id1] \[@@id2]**

\*)  
}

*type class\_type\_field\_desc* =  
| Pctf\_inherit **of** **class\_type**  
(* **inherit CT**

\*)  
| Pctf\_val **of** **(Asttypes.label Asttypes.loc * Asttypes.mutable\_flag \*** **Asttypes.virtual\_flag * core\_type)**  
(* **val x: T**

\*)  
| Pctf\_method **of** **(Asttypes.label Asttypes.loc * Asttypes.private\_flag \*** **Asttypes.virtual\_flag * core\_type)**  
(* **method x: T**

Note: **T** can be a **Parsetree.core\_type\_desc.Ptyp\_poly** .  
\*)  
| Pctf\_constraint **of** **(core\_type * core\_type)**  
(* **constraint T1 = T2**

\*)  
| Pctf\_attribute **of** **attribute**  
(* **\[@@@id]**

\*)  
| Pctf\_extension **of** **extension**  
(* **\[%%id]**

\*)

*type* **'a** *class\_infos* = {  
pci\_virt : **Asttypes.virtual\_flag** ;  
pci\_params : **(core\_type * (Asttypes.variance * Asttypes.injectivity)) list** ;  
pci\_name : **string Asttypes.loc** ;  
pci\_expr : **'a** ;  
pci\_loc : **Location.t** ;  
pci\_attributes : **attributes** ; (* **... \[@@id1] \[@@id2]**

\*)  
}

Values of type **class\_expr class\_infos** represents:

\- **class c = ...**

\- **class \['a1,...,'an] c = ...**

\- **class virtual c = ...**

They are also used for "class type" declaration.

*type class\_description* = **class\_type class\_infos**

*type class\_type\_declaration* = **class\_type class\_infos**

## [Value expressions for the class language](#Value_expressions_for_the_class_language)

*type class\_expr* = {  
pcl\_desc : **class\_expr\_desc** ;  
pcl\_loc : **Location.t** ;  
pcl\_attributes : **attributes** ; (* **... \[@id1] \[@id2]**

\*)  
}

*type class\_expr\_desc* =  
| Pcl\_constr **of** **Longident.t Asttypes.loc * core\_type list**  
(* **c** and **\['a1, ..., 'an] c**

\*)  
| Pcl\_structure **of** **class\_structure**  
(* **object ... end**

\*)  
| Pcl\_fun **of** **Asttypes.arg\_label * expression option * pattern** *** class\_expr**  
(* **Pcl\_fun(lbl, exp0, P, CE)** represents:

\- **fun P -&gt; CE** when **lbl** is **Asttypes.arg\_label.Nolabel** and **exp0** is **None** ,

\- **fun ~l:P -&gt; CE** when **lbl** is **Asttypes.arg\_label.Labelled** and **exp0** is **None** ,

\- **fun ?l:P -&gt; CE** when **lbl** is **Asttypes.arg\_label.Optional** and **exp0** is **None** ,

\- **fun ?l:(P = E0) -&gt; CE** when **lbl** is **Asttypes.arg\_label.Optional** and **exp0** is **Some E0** .

\*)  
| Pcl\_apply **of** **class\_expr * (Asttypes.arg\_label * expression) list**  
(* **Pcl\_apply(CE, \[(l1,E1) ; ... ; (ln,En)])** represents **CE ~l1:E1 ... ~ln:En** . **li** can be empty (non labeled argument) or start with **?** (optional argument).

Invariant: **n &gt; 0**

\*)  
| Pcl\_let **of** **Asttypes.rec\_flag * value\_binding list * class\_expr**  
(* **Pcl\_let(rec, \[(P1, E1); ... ; (Pn, En)], CE)** represents:

\- **let P1 = E1 and ... and Pn = EN in CE** when **rec** is **Asttypes.rec\_flag.Nonrecursive** ,

\- **let rec P1 = E1 and ... and Pn = EN in CE** when **rec** is **Asttypes.rec\_flag.Recursive** .

\*)  
| Pcl\_constraint **of** **class\_expr * class\_type**  
(* **(CE : CT)**

\*)  
| Pcl\_extension **of** **extension**  
(* **\[%id]**

\*)  
| Pcl\_open **of** **open\_description * class\_expr**  
(* **let open M in CE**

\*)

*type class\_structure* = {  
pcstr\_self : **pattern** ;  
pcstr\_fields : **class\_field list** ;  
}

Values of type **Parsetree.class\_structure** represents:

\- **object(selfpat) ... end**

\- **object ... end** when **Parsetree.class\_structure.pcstr\_self** is **Parsetree.pattern\_desc.Ppat\_any**

*type class\_field* = {  
pcf\_desc : **class\_field\_desc** ;  
pcf\_loc : **Location.t** ;  
pcf\_attributes : **attributes** ; (* **... \[@@id1] \[@@id2]**

\*)  
}

*type class\_field\_desc* =  
| Pcf\_inherit **of** **Asttypes.override\_flag * class\_expr * string Asttypes.loc option**  
(* **Pcf\_inherit(flag, CE, s)** represents:

\- **inherit CE** when **flag** is **Asttypes.override\_flag.Fresh** and **s** is **None** ,

\- **inherit CE as x** when **flag** is **Asttypes.override\_flag.Fresh** and **s** is **Some x** ,

\- **inherit! CE** when **flag** is **Asttypes.override\_flag.Override** and **s** is **None** ,

\- **inherit! CE as x** when **flag** is **Asttypes.override\_flag.Override** and **s** is **Some x**

\*)  
| Pcf\_val **of** **(Asttypes.label Asttypes.loc * Asttypes.mutable\_flag \*** **class\_field\_kind)**  
(* **Pcf\_val(x,flag, kind)** represents:

\- **val x = E** when **flag** is **Asttypes.mutable\_flag.Immutable** and **kind** is **Parsetree.class\_field\_kind.Cfk\_concrete**

\- **val virtual x: T** when **flag** is **Asttypes.mutable\_flag.Immutable** and **kind** is **Parsetree.class\_field\_kind.Cfk\_virtual**

\- **val mutable x = E** when **flag** is **Asttypes.mutable\_flag.Mutable** and **kind** is **Parsetree.class\_field\_kind.Cfk\_concrete**

\- **val mutable virtual x: T** when **flag** is **Asttypes.mutable\_flag.Mutable** and **kind** is **Parsetree.class\_field\_kind.Cfk\_virtual**

\*)  
| Pcf\_method **of** **(Asttypes.label Asttypes.loc * Asttypes.private\_flag \*** **class\_field\_kind)**  
(* - **method x = E** ( **E** can be a **Parsetree.expression\_desc.Pexp\_poly** )

\- **method virtual x: T** ( **T** can be a **Parsetree.core\_type\_desc.Ptyp\_poly** )

\*)  
| Pcf\_constraint **of** **(core\_type * core\_type)**  
(* **constraint T1 = T2**

\*)  
| Pcf\_initializer **of** **expression**  
(* **initializer E**

\*)  
| Pcf\_attribute **of** **attribute**  
(* **\[@@@id]**

\*)  
| Pcf\_extension **of** **extension**  
(* **\[%%id]**

\*)

*type class\_field\_kind* =  
| Cfk\_virtual **of** **core\_type**  
| Cfk\_concrete **of** **Asttypes.override\_flag * expression**

*type class\_declaration* = **class\_expr class\_infos**

## [Module language](#Module_language)

## [Type expressions for the module language](#Type_expressions_for_the_module_language)

*type module\_type* = {  
pmty\_desc : **module\_type\_desc** ;  
pmty\_loc : **Location.t** ;  
pmty\_attributes : **attributes** ; (* **... \[@id1] \[@id2]**

\*)  
}

*type module\_type\_desc* =  
| Pmty\_ident **of** **Longident.t Asttypes.loc**  
(* **Pmty\_ident(S)** represents **S**

\*)  
| Pmty\_signature **of** **signature**  
(* **sig ... end**

\*)  
| Pmty\_functor **of** **functor\_parameter * module\_type**  
(* **functor(X : MT1) -&gt; MT2**

\*)  
| Pmty\_with **of** **module\_type * with\_constraint list**  
(* **MT with ...**

\*)  
| Pmty\_typeof **of** **module\_expr**  
(* **module type of ME**

\*)  
| Pmty\_extension **of** **extension**  
(* **\[%id]**

\*)  
| Pmty\_alias **of** **Longident.t Asttypes.loc**  
(* **(module M)**

\*)

*type functor\_parameter* =  
| Unit (* **()**

\*)  
| Named **of** **string option Asttypes.loc * module\_type**  
(* **Named(name, MT)** represents:

\- **(X : MT)** when **name** is **Some X** ,

\- **(_ : MT)** when **name** is **None**

\*)

*type signature* = **signature\_item list**

*type signature\_item* = {  
psig\_desc : **signature\_item\_desc** ;  
psig\_loc : **Location.t** ;  
}

*type signature\_item\_desc* =  
| Psig\_value **of** **value\_description**  
(* - **val x: T**

\- **external x: T = "s1" ... "sn"**

\*)  
| Psig\_type **of** **Asttypes.rec\_flag * type\_declaration list**  
(* **type t1 = ... and ... and tn = ...**

\*)  
| Psig\_typesubst **of** **type\_declaration list**  
(* **type t1 := ... and ... and tn := ...**

\*)  
| Psig\_typext **of** **type\_extension**  
(* **type t1 += ...**

\*)  
| Psig\_exception **of** **type\_exception**  
(* **exception C of T**

\*)  
| Psig\_module **of** **module\_declaration**  
(* **module X = M** and **module X : MT**

\*)  
| Psig\_modsubst **of** **module\_substitution**  
(* **module X := M**

\*)  
| Psig\_recmodule **of** **module\_declaration list**  
(* **module rec X1 : MT1 and ... and Xn : MTn**

\*)  
| Psig\_modtype **of** **module\_type\_declaration**  
(* **module type S = MT** and **module type S**

\*)  
| Psig\_modtypesubst **of** **module\_type\_declaration**  
(* **module type S := ...**

\*)  
| Psig\_open **of** **open\_description**  
(* **open X**

\*)  
| Psig\_include **of** **include\_description**  
(* **include MT**

\*)  
| Psig\_class **of** **class\_description list**  
(* **class c1 : ... and ... and cn : ...**

\*)  
| Psig\_class\_type **of** **class\_type\_declaration list**  
(* **class type ct1 = ... and ... and ctn = ...**

\*)  
| Psig\_attribute **of** **attribute**  
(* **\[@@@id]**

\*)  
| Psig\_extension **of** **extension * attributes**  
(* **\[%%id]**

\*)

*type module\_declaration* = {  
pmd\_name : **string option Asttypes.loc** ;  
pmd\_type : **module\_type** ;  
pmd\_attributes : **attributes** ; (* **... \[@@id1] \[@@id2]**

\*)  
pmd\_loc : **Location.t** ;  
}

Values of type **module\_declaration** represents **S : MT**

*type module\_substitution* = {  
pms\_name : **string Asttypes.loc** ;  
pms\_manifest : **Longident.t Asttypes.loc** ;  
pms\_attributes : **attributes** ; (* **... \[@@id1] \[@@id2]**

\*)  
pms\_loc : **Location.t** ;  
}

Values of type **module\_substitution** represents **S := M**

*type module\_type\_declaration* = {  
pmtd\_name : **string Asttypes.loc** ;  
pmtd\_type : **module\_type option** ;  
pmtd\_attributes : **attributes** ; (* **... \[@@id1] \[@@id2]**

\*)  
pmtd\_loc : **Location.t** ;  
}

Values of type **module\_type\_declaration** represents:

\- **S = MT** ,

\- **S** for abstract module type declaration, when **Parsetree.module\_type\_declaration.pmtd\_type** is **None** .

*type* **'a** *open\_infos* = {  
popen\_expr : **'a** ;  
popen\_override : **Asttypes.override\_flag** ;  
popen\_loc : **Location.t** ;  
popen\_attributes : **attributes** ;  
}

Values of type **'a open\_infos** represents:

\- **open! X** when **Parsetree.open\_infos.popen\_override** is **Asttypes.override\_flag.Override** (silences the "used identifier shadowing" warning)

\- **open X** when **Parsetree.open\_infos.popen\_override** is **Asttypes.override\_flag.Fresh**

*type open\_description* = **Longident.t Asttypes.loc open\_infos**

Values of type **open\_description** represents:

\- **open M.N**

\- **open [M(N)](/man/enterprise-packages/ocaml-4/M.N).O**

*type open\_declaration* = **module\_expr open\_infos**

Values of type **open\_declaration** represents:

\- **open M.N**

\- **open [M(N)](/man/enterprise-packages/ocaml-4/M.N).O**

\- **open struct ... end**

*type* **'a** *include\_infos* = {  
pincl\_mod : **'a** ;  
pincl\_loc : **Location.t** ;  
pincl\_attributes : **attributes** ;  
}

*type include\_description* = **module\_type include\_infos**

Values of type **include\_description** represents **include MT**

*type include\_declaration* = **module\_expr include\_infos**

Values of type **include\_declaration** represents **include ME**

*type with\_constraint* =  
| Pwith\_type **of** **Longident.t Asttypes.loc * type\_declaration**  
(* **with type X.t = ...**

Note: the last component of the longident must match the name of the type\_declaration.  
\*)  
| Pwith\_module **of** **Longident.t Asttypes.loc * Longident.t Asttypes.loc**  
(* **with module X.Y = Z**

\*)  
| Pwith\_modtype **of** **Longident.t Asttypes.loc * module\_type**  
(* **with module type X.Y = Z**

\*)  
| Pwith\_modtypesubst **of** **Longident.t Asttypes.loc * module\_type**  
(* **with module type X.Y := sig end**

\*)  
| Pwith\_typesubst **of** **Longident.t Asttypes.loc * type\_declaration**  
(* **with type X.t := ..., same format as \[Pwith\_type]**

\*)  
| Pwith\_modsubst **of** **Longident.t Asttypes.loc * Longident.t Asttypes.loc**  
(* **with module X.Y := Z**

\*)

## [Value expressions for the module language](#Value_expressions_for_the_module_language)

*type module\_expr* = {  
pmod\_desc : **module\_expr\_desc** ;  
pmod\_loc : **Location.t** ;  
pmod\_attributes : **attributes** ; (* **... \[@id1] \[@id2]**

\*)  
}

*type module\_expr\_desc* =  
| Pmod\_ident **of** **Longident.t Asttypes.loc**  
(* **X**

\*)  
| Pmod\_structure **of** **structure**  
(* **struct ... end**

\*)  
| Pmod\_functor **of** **functor\_parameter * module\_expr**  
(* **functor(X : MT1) -&gt; ME**

\*)  
| Pmod\_apply **of** **module\_expr * module\_expr**  
(* **ME1(ME2)**

\*)  
| Pmod\_constraint **of** **module\_expr * module\_type**  
(* **(ME : MT)**

\*)  
| Pmod\_unpack **of** **expression**  
(* **(val E)**

\*)  
| Pmod\_extension **of** **extension**  
(* **\[%id]**

\*)

*type structure* = **structure\_item list**

*type structure\_item* = {  
pstr\_desc : **structure\_item\_desc** ;  
pstr\_loc : **Location.t** ;  
}

*type structure\_item\_desc* =  
| Pstr\_eval **of** **expression * attributes**  
(* **E**

\*)  
| Pstr\_value **of** **Asttypes.rec\_flag * value\_binding list**  
(* **Pstr\_value(rec, \[(P1, E1 ; ... ; (Pn, En))])** represents:

\- **let P1 = E1 and ... and Pn = EN** when **rec** is **Asttypes.rec\_flag.Nonrecursive** ,

\- **let rec P1 = E1 and ... and Pn = EN** when **rec** is **Asttypes.rec\_flag.Recursive** .

\*)  
| Pstr\_primitive **of** **value\_description**  
(* - **val x: T**

\- **external x: T = "s1" ... "sn"**

\*)  
| Pstr\_type **of** **Asttypes.rec\_flag * type\_declaration list**  
(* **type t1 = ... and ... and tn = ...**

\*)  
| Pstr\_typext **of** **type\_extension**  
(* **type t1 += ...**

\*)  
| Pstr\_exception **of** **type\_exception**  
(* - **exception C of T**

\- **exception C = M.X**

\*)  
| Pstr\_module **of** **module\_binding**  
(* **module X = ME**

\*)  
| Pstr\_recmodule **of** **module\_binding list**  
(* **module rec X1 = ME1 and ... and Xn = MEn**

\*)  
| Pstr\_modtype **of** **module\_type\_declaration**  
(* **module type S = MT**

\*)  
| Pstr\_open **of** **open\_declaration**  
(* **open X**

\*)  
| Pstr\_class **of** **class\_declaration list**  
(* **class c1 = ... and ... and cn = ...**

\*)  
| Pstr\_class\_type **of** **class\_type\_declaration list**  
(* **class type ct1 = ... and ... and ctn = ...**

\*)  
| Pstr\_include **of** **include\_declaration**  
(* **include ME**

\*)  
| Pstr\_attribute **of** **attribute**  
(* **\[@@@id]**

\*)  
| Pstr\_extension **of** **extension * attributes**  
(* **\[%%id]**

\*)

*type value\_binding* = {  
pvb\_pat : **pattern** ;  
pvb\_expr : **expression** ;  
pvb\_attributes : **attributes** ;  
pvb\_loc : **Location.t** ;  
}

*type module\_binding* = {  
pmb\_name : **string option Asttypes.loc** ;  
pmb\_expr : **module\_expr** ;  
pmb\_attributes : **attributes** ;  
pmb\_loc : **Location.t** ;  
}

Values of type **module\_binding** represents **module X = ME**

## [Toplevel](#Toplevel)

## [Toplevel phrases](#Toplevel_phrases)

*type toplevel\_phrase* =  
| Ptop\_def **of** **structure**  
| Ptop\_dir **of** **toplevel\_directive**  
(* **#use** , **#load** ...  
\*)

*type toplevel\_directive* = {  
pdir\_name : **string Asttypes.loc** ;  
pdir\_arg : **directive\_argument option** ;  
pdir\_loc : **Location.t** ;  
}

*type directive\_argument* = {  
pdira\_desc : **directive\_argument\_desc** ;  
pdira\_loc : **Location.t** ;  
}

*type directive\_argument\_desc* =  
| Pdir\_string **of** **string**  
| Pdir\_int **of** **string * char option**  
| Pdir\_ident **of** **Longident.t**  
| Pdir\_bool **of** **bool**

2026-10-27 OCamldoc

---

Document: Parsetree(3o) from ocaml-4 4.14.4-r4 (Chainguard OS)
Source: https://github.com/ocaml/ocaml
