Module Types
Representation of types and declarations
Types 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")==>'aor'_aTvar None==>_| Tarrow of Asttypes.arg_label * Types.type_expr * Types.type_expr * Types.commutable(*Tarrow (Nolabel, e1, e2, c)==>e1 -> e2Tarrow (Labelled "l", e1, e2, c)==>l:e1 -> e2Tarrow (Optional "l", e1, e2, c)==>?l:e1 -> e2See
*)commutablefor the last argument.| Ttuple of (string option * Types.type_expr) list(*Ttuple [None, t1; ...; None, tn]==>t1 * ... * tnTtuple [Some "l1", t1; ...; Some "ln", tn]==>l1:t1 * ... * ln:tnAny mix of labeled and unlabeled components also works:
*)Ttuple [Some "l1", t1; None, t2; Some "l3", t3]==>l1:t1 * t2 * l3:t3| Tconstr of Path.t * Types.type_expr list * Types.abbrev_memo Stdlib.ref(*
*)Tconstr (`A.B.t', [t1;...;tn], _)==>(t1,...,tn) A.B.tThe last parameter keep tracks of known expansions, seeabbrev_memo.| Tobject of Types.type_expr * (Path.t * Types.type_expr list) option Stdlib.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.
TypeclassandCtype.set_object_name:Tobject (Tfield(_,_,...(Tfield(_,_,rv)...), Some(`A.#ct`, [rv;t1;...;tn])==>(t1, ..., tn) #A.ctTobject (_, Some(`A.#ct`, [Tnil;t1;...;tn])==>(t1, ..., tn) A.ctwhere
*)rvis the hidden row variable.| Tfield of string * Types.field_kind * Types.type_expr * Types.type_expr(*
*)Tfield ("foo", field_public, t, ts)==><...; foo : t; ts>| Tnil(*
*)Tnil==><...; >| Tlink of Types.type_expr(*Indirection used by unification engine.
*)| Tsubst of Types.type_expr * Types.type_expr option(*
*)Tsubstis 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 Types.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 Types.type_expr * Types.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 Types.package(*Type of a first-class module (a.k.a package).
*)
package corresponds to the type of a first-class module
and fixed_explanation = | Univar of Types.type_expr(*The row type was bound to an univar
*)| Fixed_private(*The row type is private
*)| Reified of Path.t(*The row was reified
*)| Rigid(*The row type was made rigid during constraint verification
*)
and abbrev_memo = | Mnil(*No known abbreviation
*)| Mcons of Asttypes.private_flag * Path.t * Types.type_expr * Types.type_expr * Types.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 Types.abbrev_memo Stdlib.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.
val is_commu_ok : Types.commutable -> boolval commu_ok : Types.commutableval commu_var : unit -> Types.commutablefield_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).
val field_kind_repr : Types.field_kind -> Types.field_kind_viewval field_public : Types.field_kindval field_absent : Types.field_kindval field_private : unit -> Types.field_kindval field_kind_internal_repr : Types.field_kind -> Types.field_kindGetters for type_expr; calls repr before answering a value
val get_desc : Types.type_expr -> Types.type_descval get_level : Types.type_expr -> intval get_scope : Types.type_expr -> intval get_id : Types.type_expr -> intval with_type_mark : (Types.type_mark -> 'a) -> 'aval not_marked_node : Types.type_mark -> Types.type_expr -> boolval try_mark_node : Types.type_mark -> Types.type_expr -> booltype transient_expr = private {mutable desc : Types.type_desc;mutable level : int;mutable scope : Types.scope_field;id : int;
}Transient type_expr. Should only be used immediately after Transient_expr.repr
module Transient_expr : sig ... endOperations on transient_expr
val create_expr :
Types.type_desc ->
level:int ->
scope:int ->
id:int ->
Types.type_exprFunctions and definitions moved from Btype
val proto_newty3 :
level:int ->
scope:int ->
Types.type_desc ->
Types.transient_exprCreate a type with a fresh id
module TransientTypeOps : sig ... endComparisons for functors
module TransientTypeHash :
Stdlib.Hashtbl.S with type key = Types.transient_exprComparisons for type_expr; cannot be used for functors
val eq_type : Types.type_expr -> Types.type_expr -> boolval compare_type : Types.type_expr -> Types.type_expr -> intConstructor 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:(Asttypes.label * Types.row_field) list ->
more:Types.type_expr ->
closed:bool ->
fixed:Types.fixed_explanation option ->
name:(Path.t * Types.type_expr list) option ->
Types.row_descval row_fields : Types.row_desc -> (Asttypes.label * Types.row_field) listval row_more : Types.row_desc -> Types.type_exprval row_closed : Types.row_desc -> boolval row_fixed : Types.row_desc -> Types.fixed_explanation optionval row_name : Types.row_desc -> (Path.t * Types.type_expr list) optionval set_row_name :
Types.row_desc ->
(Path.t * Types.type_expr list) option ->
Types.row_descval get_row_field : Asttypes.label -> Types.row_desc -> Types.row_fieldtype row_desc_repr = | Row of {fields : (Asttypes.label * Types.row_field) list;more : Types.type_expr;closed : bool;fixed : Types.fixed_explanation option;name : (Path.t * Types.type_expr list) option;
}
get all fields at once; different from the old row_repr
val row_repr : Types.row_desc -> Types.row_desc_reprtype row_field_view = | Rpresent of Types.type_expr option| Reither of bool * Types.type_expr list * bool| Rabsent
Current contents of a row field
val row_field_repr : Types.row_field -> Types.row_field_viewval rf_present : Types.type_expr option -> Types.row_fieldval rf_absent : Types.row_fieldval rf_either :
?use_ext_of:Types.row_field ->
no_arg:bool ->
Types.type_expr list ->
matched:bool ->
Types.row_fieldval rf_either_of : Types.type_expr option -> Types.row_fieldval eq_row_field_ext : Types.row_field -> Types.row_field -> boolval changed_row_field_exts : Types.row_field list -> (unit -> unit) -> boolval match_row_field :
present:(Types.type_expr option -> 'a) ->
absent:(unit -> 'a) ->
either:
(bool ->
Types.type_expr list ->
bool ->
(Types.row_field_cell * Types.row_field option) ->
'a) ->
Types.row_field ->
'amodule Uid = Shape.Uidmodule MethSet : Stdlib.Set.S with type elt = stringmodule VarSet : Stdlib.Set.S with type elt = stringmodule Meths : Stdlib.Map.S with type key = stringmodule Vars : Stdlib.Map.S with type key = stringtype value_description = {val_type : Types.type_expr;val_kind : Types.value_kind;val_loc : Location.t;val_attributes : Parsetree.attributes;val_uid : Uid.t;
}and value_kind = | Val_reg| Val_prim of Primitive.description| Val_ivar of Asttypes.mutable_flag * string| Val_self of Types.class_signature * Types.self_meths * Ident.t Types.Vars.t * string| Val_anc of Types.class_signature * Ident.t Types.Meths.t * string
and self_meths = | Self_concrete of Ident.t Types.Meths.t| Self_virtual of Ident.t Types.Meths.t Stdlib.ref
and class_signature = {csig_self : Types.type_expr;mutable csig_self_row : Types.type_expr;mutable csig_vars : (Asttypes.mutable_flag * Asttypes.virtual_flag * Types.type_expr) Types.Vars.t;mutable csig_meths : (Types.method_privacy * Asttypes.virtual_flag * Types.type_expr) Types.Meths.t;
}module Variance : sig ... endmodule Separability : sig ... endsee Typedecl_separability for an explanation of separability and separability modes.
type type_declaration = {type_params : Types.type_expr list;type_arity : int;type_kind : Types.type_decl_kind;type_private : Asttypes.private_flag;type_manifest : Types.type_expr option;type_variance : Types.Variance.t list;type_separability : Types.Separability.t list;type_is_newtype : bool;type_expansion_scope : int;type_loc : Location.t;type_attributes : Parsetree.attributes;type_immediate : Type_immediacy.t;type_unboxed_default : bool;type_uid : Uid.t;
}and type_decl_kind =
(Types.label_declaration, Types.constructor_declaration) Types.type_kindand ('lbl, 'cstr) type_kind = | Type_abstract of Types.type_origin| Type_record of 'lbl list * Types.record_representation| Type_variant of 'cstr list * Types.variant_representation| Type_open
and record_representation = | Record_regular| Record_float| Record_unboxed of bool| Record_inlined of int| Record_extension of Path.t
and label_declaration = {ld_id : Ident.t;ld_mutable : Asttypes.mutable_flag;ld_atomic : Asttypes.atomic_flag;ld_type : Types.type_expr;ld_loc : Location.t;ld_attributes : Parsetree.attributes;ld_uid : Uid.t;
}and constructor_declaration = {cd_id : Ident.t;cd_args : Types.constructor_arguments;cd_res : Types.type_expr option;cd_loc : Location.t;cd_attributes : Parsetree.attributes;cd_uid : Uid.t;
}and constructor_arguments = | Cstr_tuple of Types.type_expr list| Cstr_record of Types.label_declaration list
type extension_constructor = {ext_type_path : Path.t;ext_type_params : Types.type_expr list;ext_args : Types.constructor_arguments;ext_ret_type : Types.type_expr option;ext_private : Asttypes.private_flag;ext_loc : Location.t;ext_attributes : Parsetree.attributes;ext_uid : Uid.t;
}type class_type = | Cty_constr of Path.t * Types.type_expr list * Types.class_type| Cty_signature of Types.class_signature| Cty_arrow of Asttypes.arg_label * Types.type_expr * Types.class_type
type class_declaration = {cty_params : Types.type_expr list;mutable cty_type : Types.class_type;cty_path : Path.t;cty_new : Types.type_expr option;cty_variance : Types.Variance.t list;cty_loc : Location.t;cty_attributes : Parsetree.attributes;cty_uid : Uid.t;
}type class_type_declaration = {clty_params : Types.type_expr list;clty_type : Types.class_type;clty_path : Path.t;clty_hash_type : Types.type_declaration;clty_variance : Types.Variance.t list;clty_loc : Location.t;clty_attributes : Parsetree.attributes;clty_uid : Uid.t;
}type module_type = | Mty_ident of Path.t| Mty_signature of Types.signature| Mty_functor of Types.functor_parameter * Types.module_type| Mty_alias of Path.t
and signature = Types.signature_item listand signature_item = | Sig_value of Ident.t * Types.value_description * Types.visibility| Sig_type of Ident.t * Types.type_declaration * Types.rec_status * Types.visibility| Sig_typext of Ident.t * Types.extension_constructor * Types.ext_status * Types.visibility| Sig_module of Ident.t * Types.module_presence * Types.module_declaration * Types.rec_status * Types.visibility| Sig_modtype of Ident.t * Types.modtype_declaration * Types.visibility| Sig_class of Ident.t * Types.class_declaration * Types.rec_status * Types.visibility| Sig_class_type of Ident.t * Types.class_type_declaration * Types.rec_status * Types.visibility
and module_declaration = {md_type : Types.module_type;md_attributes : Parsetree.attributes;md_loc : Location.t;md_uid : Uid.t;
}and modtype_declaration = {mtd_type : Types.module_type option;mtd_attributes : Parsetree.attributes;mtd_loc : Location.t;mtd_uid : Uid.t;
}val item_visibility : Types.signature_item -> Types.visibilityval bound_value_identifiers : Types.signature -> Ident.t listExtracts 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!
val signature_item_id : Types.signature_item -> Ident.tval snapshot : unit -> Types.snapshotval backtrack : cleanup_abbrev:(unit -> unit) -> Types.snapshot -> unitval undo_first_change_after : Types.snapshot -> unitval undo_compress : Types.snapshot -> unitFunctions to use when modifying a type (only Ctype?). The old values are logged and reverted on backtracking.
val link_type : Types.type_expr -> Types.type_expr -> unitval set_type_desc : Types.type_expr -> Types.type_desc -> unitval set_level : Types.type_expr -> int -> unitval set_scope : Types.type_expr -> int -> unitval set_name :
(Path.t * Types.type_expr list) option Stdlib.ref ->
(Path.t * Types.type_expr list) option ->
unitval link_row_field_ext : inside:Types.row_field -> Types.row_field -> unitval set_univar : Types.type_expr option Stdlib.ref -> Types.type_expr -> unitval link_kind : inside:Types.field_kind -> Types.field_kind -> unitval link_commu : inside:Types.commutable -> Types.commutable -> unitval set_commu_ok : Types.commutable -> unit