Ocaml_typing.Types
SourceTypes
defines the representation of types and declarations (that is, the content of module signatures).
CMI files are made of marshalled types.
Asttypes exposes basic definitions shared both by Parsetree and Types.
Type expressions for the core language.
The type_desc
variant defines all the possible type expressions one can find in OCaml. type_expr
wraps this with some annotations.
The level
field tracks the level of polymorphism associated to a type, guiding the generalization algorithm. Put shortly, when referring to a type in a given environment, both the type and the environment have a level. If the type has an higher level, then it can be considered fully polymorphic (type variables will be printed as 'a
), otherwise it'll be weakly polymorphic, or non generalized (type variables printed as '_a
). See http://okmij.org/ftp/ML/generalization.html
for more information.
Note about type_declaration
: one should not make the confusion between type_expr
and type_declaration
.
type_declaration
refers specifically to the type
construct in OCaml language, where you create and name a new type or type alias.
type_expr
is used when you refer to existing types, e.g. when annotating the expected type of a value.
Also, as the type system of OCaml is generative, a type_declaration
can have the side-effect of introducing a new type constructor, different from all other known types. Whereas type_expr
is a pure construct which allows referring to existing types.
Note on mutability: TBD.
type type_desc =
| Tvar of string option
Tvar (Some "a")
==> 'a
or '_a
Tvar None
==> _
| Tarrow of Ocaml_parsing.Asttypes.arg_label * type_expr * type_expr * commutable
Tarrow (Nolabel, e1, e2, c)
==> e1 -> e2
Tarrow (Labelled "l", e1, e2, c)
==> l:e1 -> e2
Tarrow (Optional "l", e1, e2, c)
==> ?l:e1 -> e2
See commutable
for the last argument.
| Ttuple of type_expr list
Ttuple [t1;...;tn]
==> (t1 * ... * tn)
| Tconstr of Path.t * type_expr list * abbrev_memo ref
Tconstr (`A.B.t', [t1;...;tn], _)
==> (t1,...,tn) A.B.t
The last parameter keep tracks of known expansions, see abbrev_memo
.
| Tobject of type_expr * (Path.t * type_expr list) option ref
Tobject (`f1:t1;...;fn: tn', `None')
==> < f1: t1; ...; fn: tn >
f1, fn are represented as a linked list of types using Tfield and Tnil constructors.
Tobject (_, `Some (`A.ct', [t1;...;tn]')
==> (t1, ..., tn) A.ct
. where A.ct is the type of some class.
There are also special cases for so-called "class-types", cf. Typeclass
and Ctype.set_object_name
:
Tobject (Tfield(_,_,...(Tfield(_,_,rv)...), Some(`A.#ct`, [rv;t1;...;tn])
==> (t1, ..., tn) #A.ct
Tobject (_, Some(`A.#ct`, [Tnil;t1;...;tn])
==> (t1, ..., tn) A.ct
where rv
is the hidden row variable.
| Tfield of string * field_kind * type_expr * type_expr
Tfield ("foo", field_public, t, ts)
==> <...; foo : t; ts>
| Tnil
Tnil
==> <...; >
| Tlink of type_expr
Indirection used by unification engine.
*)| Tsubst of type_expr * type_expr option
Tsubst
is used temporarily to store information in low-level functions manipulating representation of types, such as instantiation or copy. The first argument contains a copy of the original node. The second is available only when the first is the row variable of a polymorphic variant. It then contains a copy of the whole variant. This constructor should not appear outside of these cases.
| Tvariant of row_desc
Representation of polymorphic variants, see row_desc
.
| Tunivar of string option
Occurrence of a type variable introduced by a forall quantifier / Tpoly
.
| Tpoly of type_expr * type_expr list
Tpoly (ty,tyl)
==> 'a1... 'an. ty
, where 'a1 ... 'an are names given to types in tyl and occurrences of those types in ty.
| Tpackage of Path.t * (Ocaml_parsing.Longident.t * type_expr) list
Type of a first-class module (a.k.a package).
*)and abbrev_memo =
| Mnil
No known abbreviation
*)| Mcons of Ocaml_parsing.Asttypes.private_flag
* Path.t
* type_expr
* type_expr
* abbrev_memo
Found one abbreviation. A valid abbreviation should be at least as visible and reachable by the same path. The first expression is the abbreviation and the second the expansion.
*)| Mlink of abbrev_memo ref
Abbreviations can be found after this indirection
*)abbrev_memo
allows one to keep track of different expansions of a type alias. This is done for performance purposes.
For instance, when defining type 'a pair = 'a * 'a
, when one refers to an 'a pair
, it is just a shortcut for the 'a * 'a
type. This expansion will be stored in the abbrev_memo
of the corresponding Tconstr
node.
In practice, abbrev_memo
behaves like list of expansions with a mutable tail.
Note on marshalling: abbrev_memo
must not appear in saved types. Btype
, with cleanup_abbrev
and memo
, takes care of tracking and removing abbreviations.
commutable
is a flag appended to every arrow type.
When typing an application, if the type of the functional is known, its type is instantiated with commu_ok
arrows, otherwise as commu_var ()
.
When the type is not known, the application will be used to infer the actual type. This is fragile in presence of labels where there is no principal type.
Two incompatible applications must rely on is_commu_ok
arrows, otherwise they will trigger an error.
let f g = g ~a:() ~b:(); g ~b:() ~a:();
Error: This function is applied to arguments in an order different from other calls. This is only allowed when the real type is known.
field_kind
indicates the accessibility of a method.
An Fprivate
field may become Fpublic
or Fabsent
during unification, but not the other way round.
The same field_kind
is kept shared when copying Tfield
nodes so that the copies of the self-type of a class share the same accessibility (see also PR#10539).
Getters for type_expr; calls repr before answering a value
type transient_expr = private {
mutable desc : type_desc;
mutable level : int;
mutable scope : int;
id : int;
}
Transient type_expr
. Should only be used immediately after Transient_expr.repr
Operations on transient_expr
Functions and definitions moved from Btype
Comparisons for functors
Comparisons for type_expr
; cannot be used for functors
Constructor and accessors for row_desc
`X | `Y
(row_closed = true) < `X | `Y
(row_closed = true) > `X | `Y
(row_closed = false) < `X | `Y > `X
(row_closed = true)
type t = > `X
as 'a (row_more = Tvar a) type t = private > `X
(row_more = Tconstr ("t#row", , ref Mnil))
And for:
let f = function `X -> `X -> | `Y -> `X
the type of "f" will be a Tarrow
whose lhs will (basically) be:
Tvariant row_fields = [("X", _)];
row_more =
Tvariant { row_fields = [("Y", _)];
row_more =
Tvariant { row_fields = [];
row_more = _;
_
; _
}
; _
}
val create_row :
fields:(Ocaml_parsing.Asttypes.label * row_field) list ->
more:type_expr ->
closed:bool ->
fixed:fixed_explanation option ->
name:(Path.t * type_expr list) option ->
row_desc
type row_desc_repr =
| Row of {
fields : (Ocaml_parsing.Asttypes.label * row_field) list;
more : type_expr;
closed : bool;
fixed : fixed_explanation option;
name : (Path.t * type_expr list) option;
}
get all fields at once; different from the old row_repr
Current contents of a row field
type value_description = {
val_type : type_expr;
val_kind : value_kind;
val_loc : Ocaml_parsing.Location.t;
val_attributes : Ocaml_parsing.Parsetree.attributes;
val_uid : Uid.t;
}
and value_kind =
| Val_reg
| Val_prim of Primitive.description
| Val_ivar of Ocaml_parsing.Asttypes.mutable_flag * string
| Val_self of class_signature * self_meths * Ident.t Vars.t * string
| Val_anc of class_signature * Ident.t Meths.t * string
and class_signature = {
csig_self : type_expr;
mutable csig_self_row : type_expr;
mutable csig_vars : (Ocaml_parsing.Asttypes.mutable_flag
* Ocaml_parsing.Asttypes.virtual_flag
* type_expr)
Vars.t;
mutable csig_meths : (method_privacy
* Ocaml_parsing.Asttypes.virtual_flag
* type_expr)
Meths.t;
}
see Typedecl_separability
for an explanation of separability and separability modes.
type type_declaration = {
type_params : type_expr list;
type_arity : int;
type_kind : type_decl_kind;
type_private : Ocaml_parsing.Asttypes.private_flag;
type_manifest : type_expr option;
type_variance : Variance.t list;
type_separability : Separability.t list;
type_is_newtype : bool;
type_expansion_scope : int;
type_loc : Ocaml_parsing.Location.t;
type_attributes : Ocaml_parsing.Parsetree.attributes;
type_immediate : Type_immediacy.t;
type_unboxed_default : bool;
type_uid : Uid.t;
}
and ('lbl, 'cstr) type_kind =
| Type_abstract
| Type_record of 'lbl list * record_representation
| Type_variant of 'cstr list * variant_representation
| Type_open
and label_declaration = {
ld_id : Ident.t;
ld_mutable : Ocaml_parsing.Asttypes.mutable_flag;
ld_type : type_expr;
ld_loc : Ocaml_parsing.Location.t;
ld_attributes : Ocaml_parsing.Parsetree.attributes;
ld_uid : Uid.t;
}
and constructor_declaration = {
cd_id : Ident.t;
cd_args : constructor_arguments;
cd_res : type_expr option;
cd_loc : Ocaml_parsing.Location.t;
cd_attributes : Ocaml_parsing.Parsetree.attributes;
cd_uid : Uid.t;
}
and constructor_arguments =
| Cstr_tuple of type_expr list
| Cstr_record of label_declaration list
type extension_constructor = {
ext_type_path : Path.t;
ext_type_params : type_expr list;
ext_args : constructor_arguments;
ext_ret_type : type_expr option;
ext_private : Ocaml_parsing.Asttypes.private_flag;
ext_loc : Ocaml_parsing.Location.t;
ext_attributes : Ocaml_parsing.Parsetree.attributes;
ext_uid : Uid.t;
}
type class_type =
| Cty_constr of Path.t * type_expr list * class_type
| Cty_signature of class_signature
| Cty_arrow of Ocaml_parsing.Asttypes.arg_label * type_expr * class_type
type class_declaration = {
cty_params : type_expr list;
mutable cty_type : class_type;
cty_path : Path.t;
cty_new : type_expr option;
cty_variance : Variance.t list;
cty_loc : Ocaml_parsing.Location.t;
cty_attributes : Ocaml_parsing.Parsetree.attributes;
cty_uid : Uid.t;
}
type class_type_declaration = {
clty_params : type_expr list;
clty_type : class_type;
clty_path : Path.t;
clty_hash_type : type_declaration;
clty_variance : Variance.t list;
clty_loc : Ocaml_parsing.Location.t;
clty_attributes : Ocaml_parsing.Parsetree.attributes;
clty_uid : Uid.t;
}
type module_type =
| Mty_ident of Path.t
| Mty_signature of signature
| Mty_functor of functor_parameter * module_type
| Mty_alias of Path.t
| Mty_for_hole
and signature_item =
| Sig_value of Ident.t * value_description * visibility
| Sig_type of Ident.t * type_declaration * rec_status * visibility
| Sig_typext of Ident.t * extension_constructor * ext_status * visibility
| Sig_module of Ident.t
* module_presence
* module_declaration
* rec_status
* visibility
| Sig_modtype of Ident.t * modtype_declaration * visibility
| Sig_class of Ident.t * class_declaration * rec_status * visibility
| Sig_class_type of Ident.t * class_type_declaration * rec_status * visibility
and module_declaration = {
md_type : module_type;
md_attributes : Ocaml_parsing.Parsetree.attributes;
md_loc : Ocaml_parsing.Location.t;
md_uid : Uid.t;
}
and modtype_declaration = {
mtd_type : module_type option;
mtd_attributes : Ocaml_parsing.Parsetree.attributes;
mtd_loc : Ocaml_parsing.Location.t;
mtd_uid : Uid.t;
}
type constructor_description = {
cstr_name : string;
cstr_res : type_expr;
cstr_existentials : type_expr list;
cstr_args : type_expr list;
cstr_arity : int;
cstr_tag : constructor_tag;
cstr_consts : int;
cstr_nonconsts : int;
cstr_generalized : bool;
cstr_private : Ocaml_parsing.Asttypes.private_flag;
cstr_loc : Ocaml_parsing.Location.t;
cstr_attributes : Ocaml_parsing.Parsetree.attributes;
cstr_inlined : type_declaration option;
cstr_uid : Uid.t;
}
type label_description = {
lbl_name : string;
lbl_res : type_expr;
lbl_arg : type_expr;
lbl_mut : Ocaml_parsing.Asttypes.mutable_flag;
lbl_pos : int;
lbl_all : label_description array;
lbl_repres : record_representation;
lbl_private : Ocaml_parsing.Asttypes.private_flag;
lbl_loc : Ocaml_parsing.Location.t;
lbl_attributes : Ocaml_parsing.Parsetree.attributes;
lbl_uid : Uid.t;
}
Extracts the list of "value" identifiers bound by a signature. "Value" identifiers are identifiers for signature components that correspond to a run-time value: values, extensions, modules, classes. Note: manifest primitives do not correspond to a run-time value!
Functions to use when modifying a type (only Ctype?). The old values are logged and reverted on backtracking.
also register changes to arbitrary references
Number of unification variables that have been linked so far. Used to estimate the "cost" of unification.