Module Types
Representation of types and declarations
type type_expr={mutable desc : type_desc;mutable level : int;mutable scope : int;id : int;}Type expressions for the core language.
The
type_descvariant defines all the possible type expressions one can find in OCaml.type_exprwraps this with some annotations.The
levelfield 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). Seehttp://okmij.org/ftp/ML/generalization.htmlfor more information.Note about
type_declaration: one should not make the confusion betweentype_exprandtype_declaration.type_declarationrefers specifically to thetypeconstruct in OCaml language, where you create and name a new type or type alias.type_expris 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_declarationcan have the side-effect of introducing a new type constructor, different from all other known types. Whereastype_expris a pure construct which allows referring to existing types.Note on mutability: TBD.
and type_desc=|Tvar of string optionTvar (Some "a")==>'aor'_aTvar None==>_|Tarrow of Asttypes.arg_label * type_expr * type_expr * commutableTarrow (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 type_expr listTtuple [t1;...;tn]==>(t1 * ... * tn)|Tconstr of Path.t * type_expr list * abbrev_memo Stdlib.refTconstr (`A.B.t', [t1;...;tn], _)==>(t1,...,tn) A.B.tThe last parameter keep tracks of known expansions, seeabbrev_memo.|Tobject of type_expr * (Path.t * type_expr list) option Stdlib.refTobject (`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 * field_kind * type_expr * type_exprTfield ("foo", Fpresent, t, ts)==><...; foo : t; ts>|TnilTnil==><...; >|Tlink of type_exprIndirection used by unification engine.
|Tsubst of type_exprTsubstis used temporarily to store information in low-level functions manipulating representation of types, such as instantiation or copy. This constructor should not appear outside of these cases.|Tvariant of row_descRepresentation of polymorphic variants, see
row_desc.|Tunivar of string optionOccurrence of a type variable introduced by a forall quantifier /
Tpoly.|Tpoly of type_expr * type_expr listTpoly (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 * Longident.t list * type_expr listType of a first-class module (a.k.a package).
and row_desc={row_fields : (Asttypes.label * row_field) list;row_more : type_expr;row_bound : unit;row_closed : bool;row_fixed : bool;row_name : (Path.t * type_expr list) option;}`X | `Y(row_closed = true)< `X | `Y(row_closed = true)> `X | `Y(row_closed = false)< `X | `Y > `X(row_closed = true)type t =
> `Xas '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
Tarrowwhose lhs will (basically) be:Tvariant
row_fields = [("X", _)]; row_more = Tvariant { row_fields = [("Y", _)]; row_more = Tvariant { row_fields = []; row_more = _; _; _}
; _
}
and row_field=|Rpresent of type_expr option|Reither of bool * type_expr list * bool * row_field option Stdlib.ref|Rabsentand abbrev_memo=|MnilNo known abbreviation
|Mcons of Asttypes.private_flag * Path.t * type_expr * type_expr * abbrev_memoFound 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 Stdlib.refAbbreviations can be found after this indirection
abbrev_memoallows 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 * 'atype. This expansion will be stored in theabbrev_memoof the correspondingTconstrnode.In practice,
abbrev_memobehaves like list of expansions with a mutable tail.Note on marshalling:
abbrev_memomust not appear in saved types.Btype, withcleanup_abbrevandmemo, takes care of tracking and removing abbreviations.
and field_kind=|Fvar of field_kind option Stdlib.ref|Fpresent|Fabsentand commutable=|Cok|Cunknown|Clink of commutable Stdlib.refcommutableis a flag appended to every arrow type.When typing an application, if the type of the functional is known, its type is instantiated with
Cokarrows, otherwise asClink (ref Cunknown).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 relying on
Cunknownarrows 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.
module TypeOps : sig ... endmodule Meths : Stdlib.Map.S with type key = stringmodule Vars : Stdlib.Map.S with type key = stringtype value_description={val_type : type_expr;val_kind : value_kind;val_loc : Location.t;val_attributes : Parsetree.attributes;}and value_kind=|Val_reg|Val_prim of Primitive.description|Val_ivar of Asttypes.mutable_flag * string|Val_self of (Ident.t * type_expr) Meths.t Stdlib.ref * (Ident.t * Asttypes.mutable_flag * Asttypes.virtual_flag * type_expr) Vars.t Stdlib.ref * string * type_expr|Val_anc of (string * Ident.t) list * string|Val_unbound of value_unbound_reasonand value_unbound_reason=|Val_unbound_instance_variable|Val_unbound_ghost_recursive
module Variance : sig ... endtype type_declaration={type_params : type_expr list;type_arity : int;type_kind : type_kind;type_private : Asttypes.private_flag;type_manifest : type_expr option;type_variance : Variance.t list;type_is_newtype : bool;type_expansion_scope : int;type_loc : Location.t;type_attributes : Parsetree.attributes;type_immediate : bool;type_unboxed : unboxed_status;}and type_kind=|Type_abstract|Type_record of label_declaration list * record_representation|Type_variant of constructor_declaration list|Type_openand record_representation=|Record_regular|Record_float|Record_unboxed of bool|Record_inlined of int|Record_extension of Path.tand label_declaration={ld_id : Ident.t;ld_mutable : Asttypes.mutable_flag;ld_type : type_expr;ld_loc : Location.t;ld_attributes : Parsetree.attributes;}and constructor_declaration={cd_id : Ident.t;cd_args : constructor_arguments;cd_res : type_expr option;cd_loc : Location.t;cd_attributes : Parsetree.attributes;}and constructor_arguments=|Cstr_tuple of type_expr list|Cstr_record of label_declaration listand unboxed_status= private{unboxed : bool;default : bool;}
val unboxed_false_default_false : unboxed_statusval unboxed_false_default_true : unboxed_statusval unboxed_true_default_false : unboxed_statusval unboxed_true_default_true : unboxed_status
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 : Asttypes.private_flag;ext_loc : Location.t;ext_attributes : Parsetree.attributes;}and type_transparence=|Type_public|Type_new|Type_private
module Concr : Stdlib.Set.S with type elt = stringtype class_type=|Cty_constr of Path.t * type_expr list * class_type|Cty_signature of class_signature|Cty_arrow of Asttypes.arg_label * type_expr * class_typeand class_signature={csig_self : type_expr;csig_vars : (Asttypes.mutable_flag * Asttypes.virtual_flag * type_expr) Vars.t;csig_concr : Concr.t;csig_inher : (Path.t * type_expr list) list;}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 : Location.t;cty_attributes : Parsetree.attributes;}type class_type_declaration={clty_params : type_expr list;clty_type : class_type;clty_path : Path.t;clty_variance : Variance.t list;clty_loc : Location.t;clty_attributes : Parsetree.attributes;}type visibility=|Exported|Hiddentype module_type=|Mty_ident of Path.t|Mty_signature of signature|Mty_functor of Ident.t * module_type option * module_type|Mty_alias of Path.tand module_presence=|Mp_present|Mp_absentand signature= signature_item listand 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 * visibilityand module_declaration={md_type : module_type;md_attributes : Parsetree.attributes;md_loc : Location.t;}and modtype_declaration={mtd_type : module_type option;mtd_attributes : Parsetree.attributes;mtd_loc : Location.t;}and rec_status=|Trec_not|Trec_first|Trec_nextand ext_status=|Text_first|Text_next|Text_exceptiontype 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_normal : int;cstr_generalized : bool;cstr_private : Asttypes.private_flag;cstr_loc : Location.t;cstr_attributes : Parsetree.attributes;cstr_inlined : type_declaration option;}and constructor_tag=|Cstr_constant of int|Cstr_block of int|Cstr_unboxed|Cstr_extension of Path.t * bool
val equal_tag : constructor_tag -> constructor_tag -> boolval may_equal_constr : constructor_description -> constructor_description -> bool
type label_description={lbl_name : string;lbl_res : type_expr;lbl_arg : type_expr;lbl_mut : Asttypes.mutable_flag;lbl_pos : int;lbl_all : label_description array;lbl_repres : record_representation;lbl_private : Asttypes.private_flag;lbl_loc : Location.t;lbl_attributes : Parsetree.attributes;}
val bound_value_identifiers : 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 : signature_item -> Ident.t