Module Generic_core_desc.Ext

module Ext: sig .. end
Generic representation of extensible variants.


Extensible variants allow new constructors to be added to a type after it has been defined. The generic view for extensible variants must also be extensible so that the description of the new constructors may be added to the description of the extensible variant.

We define an abstract type 'a extensible to describe the type 'a with the following operations:


type con = {
   con : 'a. 'a Ty.T.ty -> 'a Generic_core_desc.Con.t;
}
A constructor of an extensible type ('a0,...'an) t is described by a value of type con which is a partial polymorphic function that computes the constructor representation for any closed type expression (s0,...,sn) t (without any type variable). Thus the con function may only be called on the witnesses of type (s0,...sn) t ty.

This design supports GADTs by default.

type cons 
type 'a extensible = {
   name : string;
   ty : 'a Ty.T.ty;
   cons : cons;
}
type 'a t = 'a extensible 
Synonym for convenience.
val create : unit -> cons
Initialises the collection of constructors for an extensible type
val add_con : 'a extensible -> con -> unit
add_con ext c adds c to the constructors of extensible type described by ext
val fold : 'a extensible ->
('a Generic_core_desc.Con.t -> 'b -> 'b) -> 'b -> 'b
fold ext computes List.foldr on the constructor list.
val iter : 'a extensible ->
('a Generic_core_desc.Con.t -> unit) -> unit
Executes an effectful function on each of the (known) constructors of an extensible datatype.
val con_list : 'a extensible -> 'a Generic_core_desc.Con.t list
returns the current list of constructors
val con : 'a extensible -> 'a -> 'a Generic_core_desc.Con.t
con ext x
Raises Returns the constructor description corresponding to the constructor of x
val conap : 'a extensible -> 'a -> 'a Generic_core_desc.Con.conap
Deconstructs a value into a pair of a constructor and its arguments.
Raises
val fix : 'a extensible -> 'a -> 'a
Serialising/Deserializing an extensible value (including exceptions) doesn't preserve structural equality:

        umarshall (marshall x) <> x
      
In particular pattern matching the deserialized value doesn't work as expected. fix fixes that: when given a deserialized value, it returns a value structurally equal to the one that was serialized:

        fix (unmarshall (marshall x)) = x
      

In details, when an extensible value has been deserialized, it's memory representation will be different from that of the value before it was serialized. This is because constructors of extensible variants are implemented as object blocks (same as OCaml objects), and they get assigned a unique identifier when created. fix ext x will replace the constructor of x with the original constructor object that is stored in the 'a extensible datastructure ext.
Raises