Module Ocaml_typing.TypesSource

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.

Sourcetype type_expr

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.

Sourcetype row_desc
Sourcetype row_field
Sourcetype field_kind
Sourcetype commutable
Sourcetype type_desc =
  1. | Tvar of string option
    (*

    Tvar (Some "a") ==> 'a or '_a Tvar None ==> _

    *)
  2. | 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.

    *)
  3. | Ttuple of type_expr list
    (*

    Ttuple [t1;...;tn] ==> (t1 * ... * tn)

    *)
  4. | 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.

    *)
  5. | 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.

    *)
  6. | Tfield of string * field_kind * type_expr * type_expr
    (*

    Tfield ("foo", field_public, t, ts) ==> <...; foo : t; ts>

    *)
  7. | Tnil
    (*

    Tnil ==> <...; >

    *)
  8. | 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.

    *)
  9. | Tvariant of row_desc
    (*

    Representation of polymorphic variants, see row_desc.

    *)
  10. | Tunivar of string option
    (*

    Occurrence of a type variable introduced by a forall quantifier / Tpoly.

    *)
  11. | 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.

    *)
  12. | Tpackage of Path.t * (Ocaml_parsing.Longident.t * type_expr) list
    (*

    Type of a first-class module (a.k.a package).

    *)
Sourceand fixed_explanation =
  1. | Univar of type_expr
    (*

    The row type was bound to an univar

    *)
  2. | Fixed_private
    (*

    The row type is private

    *)
  3. | Reified of Path.t
    (*

    The row was reified

    *)
  4. | Rigid
    (*

    The row type was made rigid during constraint verification

    *)
Sourceand abbrev_memo =
  1. | Mnil
    (*

    No known abbreviation

    *)
  2. | 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.

    *)

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.

Sourceval is_commu_ok : commutable -> bool
Sourceval commu_ok : commutable
Sourceval commu_var : unit -> commutable

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).

Sourcetype field_kind_view =
  1. | Fprivate
  2. | Fpublic
  3. | Fabsent
Sourceval field_kind_repr : field_kind -> field_kind_view
Sourceval field_public : field_kind
Sourceval field_absent : field_kind
Sourceval field_private : unit -> field_kind
Sourceval field_kind_internal_repr : field_kind -> field_kind

Getters for type_expr; calls repr before answering a value

Sourceval get_desc : type_expr -> type_desc
Sourceval get_level : type_expr -> int
Sourceval get_scope : type_expr -> int
Sourceval get_id : type_expr -> int
Sourcetype transient_expr = private {
  1. mutable desc : type_desc;
  2. mutable level : int;
  3. mutable scope : int;
  4. id : int;
}

Transient type_expr. Should only be used immediately after Transient_expr.repr

Sourcemodule Transient_expr : sig ... end

Operations on transient_expr

Sourceval create_expr : type_desc -> level:int -> scope:int -> id:int -> type_expr

Functions and definitions moved from Btype

Sourceval newty3 : level:int -> scope:int -> type_desc -> type_expr

Create a type with a fresh id

Sourceval newty2 : level:int -> type_desc -> type_expr

Create a type with a fresh id and no scope

Sourcemodule TransientTypeOps : sig ... end

Comparisons for functors

Comparisons for type_expr; cannot be used for functors

Sourceval eq_type : type_expr -> type_expr -> bool
Sourceval compare_type : type_expr -> type_expr -> int

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 = _; _ ; _

}

; _

}

Sourceval 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
Sourceval row_more : row_desc -> type_expr
Sourceval row_closed : row_desc -> bool
Sourceval row_fixed : row_desc -> fixed_explanation option
Sourceval row_name : row_desc -> (Path.t * type_expr list) option
Sourceval set_row_name : row_desc -> (Path.t * type_expr list) option -> row_desc
Sourcetype row_desc_repr =
  1. | Row of {
    1. fields : (Ocaml_parsing.Asttypes.label * row_field) list;
    2. more : type_expr;
    3. closed : bool;
    4. fixed : fixed_explanation option;
    5. name : (Path.t * type_expr list) option;
    }

get all fields at once; different from the old row_repr

Sourceval row_repr : row_desc -> row_desc_repr
Sourcetype row_field_view =
  1. | Rpresent of type_expr option
  2. | Reither of bool * type_expr list * bool
  3. | Rabsent

Current contents of a row field

Sourceval row_field_repr : row_field -> row_field_view
Sourceval rf_present : type_expr option -> row_field
Sourceval rf_absent : row_field
Sourceval rf_either : ?use_ext_of:row_field -> no_arg:bool -> type_expr list -> matched:bool -> row_field
Sourceval rf_either_of : type_expr option -> row_field
Sourceval eq_row_field_ext : row_field -> row_field -> bool
Sourceval changed_row_field_exts : row_field list -> (unit -> unit) -> bool
Sourceval match_row_field : present:(type_expr option -> 'a) -> absent:(unit -> 'a) -> either:(bool -> type_expr list -> bool -> row_field option -> 'a) -> row_field -> 'a
Sourcemodule Uid = Shape.Uid
Sourcemodule MethSet : Set.S with type elt = string
Sourcemodule VarSet : Set.S with type elt = string
Sourcemodule Meths : Map.S with type key = string
Sourcemodule Vars : Map.S with type key = string
Sourcetype value_description = {
  1. val_type : type_expr;
  2. val_kind : value_kind;
  3. val_loc : Ocaml_parsing.Location.t;
  4. val_attributes : Ocaml_parsing.Parsetree.attributes;
  5. val_uid : Uid.t;
}
Sourceand value_kind =
  1. | Val_reg
  2. | Val_prim of Primitive.description
  3. | Val_ivar of Ocaml_parsing.Asttypes.mutable_flag * string
  4. | Val_self of class_signature * self_meths * Ident.t Vars.t * string
  5. | Val_anc of class_signature * Ident.t Meths.t * string
Sourceand self_meths =
  1. | Self_concrete of Ident.t Meths.t
  2. | Self_virtual of Ident.t Meths.t ref
Sourceand class_signature = {
  1. csig_self : type_expr;
  2. mutable csig_self_row : type_expr;
  3. mutable csig_vars : (Ocaml_parsing.Asttypes.mutable_flag * Ocaml_parsing.Asttypes.virtual_flag * type_expr) Vars.t;
  4. mutable csig_meths : (method_privacy * Ocaml_parsing.Asttypes.virtual_flag * type_expr) Meths.t;
}
Sourceand method_privacy =
  1. | Mpublic
  2. | Mprivate of field_kind
Sourcemodule Variance : sig ... end
Sourcemodule Separability : sig ... end

see Typedecl_separability for an explanation of separability and separability modes.

Sourcetype type_declaration = {
  1. type_params : type_expr list;
  2. type_arity : int;
  3. type_kind : type_decl_kind;
  4. type_private : Ocaml_parsing.Asttypes.private_flag;
  5. type_manifest : type_expr option;
  6. type_variance : Variance.t list;
  7. type_separability : Separability.t list;
  8. type_is_newtype : bool;
  9. type_expansion_scope : int;
  10. type_loc : Ocaml_parsing.Location.t;
  11. type_attributes : Ocaml_parsing.Parsetree.attributes;
  12. type_immediate : Type_immediacy.t;
  13. type_unboxed_default : bool;
  14. type_uid : Uid.t;
}
Sourceand ('lbl, 'cstr) type_kind =
  1. | Type_abstract
  2. | Type_record of 'lbl list * record_representation
  3. | Type_variant of 'cstr list * variant_representation
  4. | Type_open
Sourceand record_representation =
  1. | Record_regular
  2. | Record_float
  3. | Record_unboxed of bool
  4. | Record_inlined of int
  5. | Record_extension of Path.t
Sourceand variant_representation =
  1. | Variant_regular
  2. | Variant_unboxed
Sourceand label_declaration = {
  1. ld_id : Ident.t;
  2. ld_mutable : Ocaml_parsing.Asttypes.mutable_flag;
  3. ld_type : type_expr;
  4. ld_loc : Ocaml_parsing.Location.t;
  5. ld_attributes : Ocaml_parsing.Parsetree.attributes;
  6. ld_uid : Uid.t;
}
Sourceand constructor_declaration = {
  1. cd_id : Ident.t;
  2. cd_args : constructor_arguments;
  3. cd_res : type_expr option;
  4. cd_loc : Ocaml_parsing.Location.t;
  5. cd_attributes : Ocaml_parsing.Parsetree.attributes;
  6. cd_uid : Uid.t;
}
Sourceand constructor_arguments =
  1. | Cstr_tuple of type_expr list
  2. | Cstr_record of label_declaration list
Sourcetype extension_constructor = {
  1. ext_type_path : Path.t;
  2. ext_type_params : type_expr list;
  3. ext_args : constructor_arguments;
  4. ext_ret_type : type_expr option;
  5. ext_private : Ocaml_parsing.Asttypes.private_flag;
  6. ext_loc : Ocaml_parsing.Location.t;
  7. ext_attributes : Ocaml_parsing.Parsetree.attributes;
  8. ext_uid : Uid.t;
}
Sourceand type_transparence =
  1. | Type_public
  2. | Type_new
  3. | Type_private
Sourcetype class_type =
  1. | Cty_constr of Path.t * type_expr list * class_type
  2. | Cty_signature of class_signature
  3. | Cty_arrow of Ocaml_parsing.Asttypes.arg_label * type_expr * class_type
Sourcetype class_declaration = {
  1. cty_params : type_expr list;
  2. mutable cty_type : class_type;
  3. cty_path : Path.t;
  4. cty_new : type_expr option;
  5. cty_variance : Variance.t list;
  6. cty_loc : Ocaml_parsing.Location.t;
  7. cty_attributes : Ocaml_parsing.Parsetree.attributes;
  8. cty_uid : Uid.t;
}
Sourcetype class_type_declaration = {
  1. clty_params : type_expr list;
  2. clty_type : class_type;
  3. clty_path : Path.t;
  4. clty_hash_type : type_declaration;
  5. clty_variance : Variance.t list;
  6. clty_loc : Ocaml_parsing.Location.t;
  7. clty_attributes : Ocaml_parsing.Parsetree.attributes;
  8. clty_uid : Uid.t;
}
Sourcetype visibility =
  1. | Exported
  2. | Hidden
Sourcetype module_type =
  1. | Mty_ident of Path.t
  2. | Mty_signature of signature
  3. | Mty_functor of functor_parameter * module_type
  4. | Mty_alias of Path.t
  5. | Mty_for_hole
Sourceand functor_parameter =
  1. | Unit
  2. | Named of Ident.t option * module_type
Sourceand module_presence =
  1. | Mp_present
  2. | Mp_absent
Sourceand signature = signature_item list
Sourceand module_declaration = {
  1. md_type : module_type;
  2. md_attributes : Ocaml_parsing.Parsetree.attributes;
  3. md_loc : Ocaml_parsing.Location.t;
  4. md_uid : Uid.t;
}
Sourceand modtype_declaration = {
  1. mtd_type : module_type option;
  2. mtd_attributes : Ocaml_parsing.Parsetree.attributes;
  3. mtd_loc : Ocaml_parsing.Location.t;
  4. mtd_uid : Uid.t;
}
Sourceand rec_status =
  1. | Trec_not
  2. | Trec_first
  3. | Trec_next
Sourceand ext_status =
  1. | Text_first
  2. | Text_next
  3. | Text_exception
Sourceval item_visibility : signature_item -> visibility
Sourcetype constructor_description = {
  1. cstr_name : string;
  2. cstr_res : type_expr;
  3. cstr_existentials : type_expr list;
  4. cstr_args : type_expr list;
  5. cstr_arity : int;
  6. cstr_tag : constructor_tag;
  7. cstr_consts : int;
  8. cstr_nonconsts : int;
  9. cstr_generalized : bool;
  10. cstr_private : Ocaml_parsing.Asttypes.private_flag;
  11. cstr_loc : Ocaml_parsing.Location.t;
  12. cstr_attributes : Ocaml_parsing.Parsetree.attributes;
  13. cstr_inlined : type_declaration option;
  14. cstr_uid : Uid.t;
}
Sourceand constructor_tag =
  1. | Cstr_constant of int
  2. | Cstr_block of int
  3. | Cstr_unboxed
  4. | Cstr_extension of Path.t * bool
Sourceval equal_tag : constructor_tag -> constructor_tag -> bool
Sourceval may_equal_constr : constructor_description -> constructor_description -> bool
Sourcetype label_description = {
  1. lbl_name : string;
  2. lbl_res : type_expr;
  3. lbl_arg : type_expr;
  4. lbl_mut : Ocaml_parsing.Asttypes.mutable_flag;
  5. lbl_pos : int;
  6. lbl_all : label_description array;
  7. lbl_repres : record_representation;
  8. lbl_private : Ocaml_parsing.Asttypes.private_flag;
  9. lbl_loc : Ocaml_parsing.Location.t;
  10. lbl_attributes : Ocaml_parsing.Parsetree.attributes;
  11. lbl_uid : Uid.t;
}
Sourceval bound_value_identifiers : signature -> Ident.t list

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!

Sourceval signature_item_id : signature_item -> Ident.t
Sourcetype snapshot
Sourceval snapshot : unit -> snapshot
Sourceval backtrack : cleanup_abbrev:(unit -> unit) -> snapshot -> unit
Sourceval undo_first_change_after : snapshot -> unit
Sourceval undo_compress : snapshot -> unit

Functions to use when modifying a type (only Ctype?). The old values are logged and reverted on backtracking.

Sourceval set_type_desc : type_expr -> type_desc -> unit
Sourceval set_level : type_expr -> int -> unit
Sourceval set_scope : type_expr -> int -> unit
Sourceval set_name : (Path.t * type_expr list) option ref -> (Path.t * type_expr list) option -> unit
Sourceval set_univar : type_expr option ref -> type_expr -> unit
Sourceval set_commu_ok : commutable -> unit
Sourceval is_valid : snapshot -> bool

check if a snapshot has been invalidated

Sourceval on_backtrack : (unit -> unit) -> unit

also register changes to arbitrary references

Sourceval linked_variables : unit -> int

Number of unification variables that have been linked so far. Used to estimate the "cost" of unification.