Module Generic_core_desc.Variant

module Variant: sig .. end
Generic representation of variant datatypes.


A variant is described as a set of constructors. Each constructor is described by its name, the types of its arguments given as a nested product, a function to embed a value of the nested product to the variant type, and a partial projection function that only succeeds when the value has the same constructor.
type 'v cons 
Set of constructors. 'v cons is the set of constructors of variant 'v, it is an abstract type. We may access the constructors using Generic_core_desc.Variant.con_list, or using Generic_core_desc.Variant.cst and Generic_core_desc.Variant.ncst which respectively return constant and non-constant constructors in the order that they are listed in the datatype definition. cst_get and ncst_get offer O(1) access to each constructor.
val cons : 'v Generic_core_desc.Con.t list -> 'v cons
Builds a 'v cons from a list of constructor descriptions.

The constructors do not necessarily need to be given in the same order as in the datatype definition, but it is good practice to do so.

val unboxed_con : 'v Generic_core_desc.Con.t -> 'v cons
Builds a 'v cons from a single constructor. unboxed_con must be used only to represent unboxed variants.
val cst_len : 'v cons -> int
O(1). cst_len cs is the number of constant constructors in cs.
val cst_get : 'v cons -> int -> 'v Generic_core_desc.Con.t
O(1). cst_get cs i returns the ith constant constructors of cs (in the order of the datatype definition).

Indices start at 0 and end at cst_len cs - 1.
Raises Invalid_argument if i < 0 or i >= cst_len cs

val cst : 'v cons -> 'v Generic_core_desc.Con.t list
cst cs returns the list of the constant constructors in cs.
val ncst_len : 'v cons -> int
O(1). ncst_len cs is the number of non-constant constructors in cs.
val ncst_get : 'v cons -> int -> 'v Generic_core_desc.Con.t
O(1). ncst_get cs i returns the ith non-constant constructors of cs (in the order of the datatype definition).

Indices start at 0 and end at ncst_len cs - 1.
Raises Invalid_argument if i < 0 or i >= ncst_len cs

val ncst : 'v cons -> 'v Generic_core_desc.Con.t list
ncst cs returns the list of the non-constant constructors in cs.
val con_list : 'v cons -> 'v Generic_core_desc.Con.t list
Returns the list of constructors corresponding to the abstract cons value. con_list (cons cs) is a permutation of cs.
val conap : 'v cons -> 'v -> 'v Generic_core_desc.Con.conap
O(1). Deconstructs a value into a pair of a constructor and its arguments.
Raises Invalid_argument if 'v cons doesn't reflect all the possible constructors of variant 'v.
conap cs x = Conap (c,y)  ==>  c.embed y = x

type 'v variant = {
   name : string;
   module_path : string list;
   cons : 'v cons;
   unboxed : bool;
}
Variant. A variant is given by its name the module in which it was defined and the set cons of its constructors.
type 'v t = 'v variant 
Synonym for convenience.