Module Type.Gist

Type gists.

Type gists reflect the essence of your types as OCaml values of a given representation type.

Type gists are useful to type the interface of your values at the boundaries of your program or to devise generic type indexed functions.

See description examples and a generic function template.

Interfaces

Interfaces allow to directly inject types that satisfy a given interface in the representation.

Arrays

module type ARRAY = sig ... end

Array interface.

type ('elt, 'arr) array_module = (module ARRAY with type elt = 'elt and type t = 'arr)

The type for representing array types of type 'a with elements of type 'elt.

Maps

module type MAP = sig ... end

Map interface.

type ('k, 'v, 'm) map_module = (module MAP with type key = 'k and type t = 'm and type value = 'v)

The type for representing map types of type 'm mapping keys of type 'k to values of type 'v.

Type representation

module Meta : sig ... end

Gist metadata.

type 'a scalar =
  1. | Unit : unit Meta.t -> unit scalar
  2. | Bool : bool Meta.t -> bool scalar
  3. | Char : char Meta.t -> char scalar
  4. | Uchar : Stdlib.Uchar.t Meta.t -> Stdlib.Uchar.t scalar
  5. | Int : int Meta.t -> int scalar
  6. | Int32 : int32 Meta.t -> int32 scalar
  7. | Int64 : int64 Meta.t -> int64 scalar
  8. | Nativeint : nativeint Meta.t -> nativeint scalar
  9. | Float : float Meta.t -> float scalar

The type for representing scalar types of type 'a. See scalars.

type ('elt, 'arr) arraylike =
  1. | String : string Meta.t * char t -> (char, string) arraylike
  2. | Bytes : bytes Meta.t * char t -> (char, bytes) arraylike
  3. | Array : 'elt array Meta.t * 'elt t -> ('elt, 'elt array) arraylike
  4. | Bigarray1 : ('elt, 'b, 'c) Bigarray.Array1.t Meta.t * ('elt, 'b) Bigarray.kind * 'c Bigarray.layout * 'elt t -> ('elt, ('elt, 'b, 'c) Bigarray.Array1.t) arraylike
  5. | Array_module : 'arr Meta.t * ('elt, 'arr) array_module * 'elt t -> ('elt, 'arr) arraylike

The type for representing array types of type 'a with elements of type 'elt. See arraylike.

and ('k, 'v, 'm) maplike =
  1. | Hashtbl : ('k, 'v) Stdlib.Hashtbl.t Meta.t * 'k t * 'v t -> ('k, 'v, ('k, 'v) Stdlib.Hashtbl.t) maplike
  2. | Map_module : 'm Meta.t * ('k, 'v, 'm) map_module * 'k t * 'v t -> ('k, 'v, 'm) maplike

The type for representing map types of type 'm mapping keys of type 'k to values of type 'v. See maplike.

and 'p product

The type for representing product types of type 'p. See products.

and 'r record = 'r product

The type for representing record types of type 'r. See records.

and 'v variant

The type for representing variants of type 'v. See variants.

and 's sum =
  1. | Option : 'a option Meta.t * 'a t -> 'a option sum
  2. | Either : ('a, 'b) Stdlib.Either.t Meta.t * 'a t * 'b t -> ('a, 'b) Stdlib.Either.t sum
  3. | Result : ('a, 'b) Stdlib.result Meta.t * 'a t * 'b t -> ('a, 'b) Stdlib.result sum
  4. | List : 'a list Meta.t * 'a t -> 'a list sum
  5. | Variant : 'v variant -> 'v sum

The type for representing sum types of type 's. See sums.

and ('a, 'b) func

The type for representing functions types 'a -> 'b. See functions.

and 'a abstract

The type for representing abstract types of type 'a. See abstract types.

and 'a t =
  1. | Scalar : 'a scalar -> 'a t
  2. | Arraylike : ('elt, 'arr) arraylike -> 'arr t
  3. | Maplike : ('k, 'v, 'm) maplike -> 'm t
  4. | Product : 'p product -> 'p t
  5. | Record : 'r record -> 'r t
  6. | Sum : 's sum -> 's t
  7. | Func : ('a, 'b) func -> ('a -> 'b) t
  8. | Abstract : 'a abstract -> 'a t
  9. | Lazy : 'a lazy_t Meta.t * 'a t -> 'a lazy_t t
  10. | Ref : 'a Stdlib.ref Meta.t * 'a t -> 'a Stdlib.ref t
  11. | Rec : 'a t lazy_t -> 'a t
    (*

    Recursion

    *)

The type for type gists.

Constructors and operations

val ref : ?meta:'a Stdlib.ref Meta.t -> 'a t -> 'a Stdlib.ref t

ref g is Ref g.

val lazy' : ?meta:'a lazy_t Meta.t -> 'a t -> 'a lazy_t t

lazy' ~meta g is Lazy (meta, g).

val rec' : 'a t lazy_t -> 'a t

rec' lg is Rec lg.

Scalars

module Scalar : sig ... end

Operating on scalar types.

val unit : unit t

unit is Scalar (Unit Meta.empty).

val bool : bool t

bool is Scalar (Bool Meta.empty).

val char : char t

char is Scalar (Char Meta.empty).

val uchar : Stdlib.Uchar.t t

uchar is Scalar (Uchar Meta.empty).

val int : int t

int is Scalar (Int Meta.empty).

val int32 : int32 t

int32 is Scalar (Int32 Meta.empty).

val int64 : int64 t

int64 is Scalar (Int64 Meta.empty).

val nativeint : nativeint t

nativeint is Scalar (Nativeint Meta.empt)y.

val float : float t

float is Scalar (Float Meta.empty).

Arraylike

The arraylike type gathers generic linear arrays and a few special cases for linear array types of the standard library. The specialisation can be converted to generic linear arrays.

module Arraylike : sig ... end

Operating on arraylike types.

val string : string t

string is Arraylike (String (Meta.empty, char)).

val bytes : bytes t

bytes is Arraylike (Bytes (Meta.empty, char)).

val array : ?meta:'elt array Meta.t -> 'elt t -> 'elt array t

array represents arrays with elements of given representation.

val bigarray1 : ?meta:('elt, 'b, 'c) Bigarray.Array1.t Meta.t -> ('elt, 'b) Bigarray.kind -> 'c Bigarray.layout -> 'elt t -> ('elt, 'b, 'c) Bigarray.Array1.t t

bigarray represents bigarrays with elements of given representation.

val array_module : ?meta:'arr Meta.t -> ('elt, 'arr) array_module -> 'elt t -> 'arr t

array_module represents array modules with elements of the given representation.

Maplike

The maplike type gathers generic functional key value maps and a special case for hash tables.

module Maplike : sig ... end

Operations on maplikes.

val hashtbl : ?meta:('k, 'v) Stdlib.Hashtbl.t Meta.t -> 'k t -> 'v t -> ('k, 'v) Stdlib.Hashtbl.t t

hashtbl represents hashtables with keys and values of given representations.

val map_module : ?meta:'m Meta.t -> ('k, 'v, 'm) map_module -> 'k t -> 'v t -> 'm t

map_module m k v represents map modules with keys and values of the given representations. See here create map modules from standard library maps.

Products

Products (tuples), records and variant cases are all products of types. The toplevel type representation distinguishes them in different cases but otherwise they share the same representation: a product of typed and possibly named fields.

The field type represents an individual field of a product. The fields type represent the ordered sequence of fields and the way to construct the product from its fields with a constructor function.

type ('p, 'f) field

The type for representing the field of type 'f of a product of type 'p. See the Field module.

type ('p, _) fields =
  1. | Ctor : 'ctor -> ('p, 'ctor) fields
  2. | App : ('p, 'f -> 'a) fields * ('p, 'f) field -> ('p, 'a) fields

The type for representing the fields of a product of type 'p and its construction via a constructor function. See the Fields module.

When a fields value types with ('p, 'p) fields we know how to project each field of product 'p and how to construct a value from its fields.

module Field : sig ... end

Operating on fields.

module Fields : sig ... end

Operating on sequences of fields.

val ctor : 'a -> ('p, 'a) fields

ctor f is Ctor f. This lifts the constructor function f for the type 'p in order to construct a list of fields with app yielding a result of type 'p.

val app : ('p, 'f -> 'a) fields -> ('p, 'f) field -> ('p, 'a) fields

app f arg is App (f, app)

val (*) : ('p, 'f -> 'a) fields -> ('p, 'f) field -> ('p, 'a) fields

fs * f is App (fs, f). Fancy operator so that you can write, for example :

let pair_gist gfst gsnd =
  let pair x y = (x, y) in
  let fst = Type.Gist.(dim gfst fst) in
  let snd = Type.Gist.(dim gsnd snd) in
  Type.Gist.(product @@ ctor pair * fst * snd)
module Product : sig ... end

Operating on products.

val dim : ?meta:'a Meta.t -> ?name:string -> ?inject:('p -> 'a -> 'p) -> ?default:'a -> 'a t -> ('p -> 'a) -> ('p, 'a) field

dim g project defines a dimension of a product of type 'p. This is just Field.make.

val product : ?meta:'p Meta.t -> ?name:string -> ('p, 'p) fields -> 'p t

product dims is a product with dimensions and construction represented by dims. This is Product.make wrapped in a Product gist value.

val p2 : ?meta:('a * 'b) Meta.t -> ?name:string -> 'a t -> 'b t -> ('a * 'b) t

p2 represents pairs with given dimensions types.

val p3 : ?meta:('a * 'b * 'c) Meta.t -> ?name:string -> 'a t -> 'b t -> 'c t -> ('a * 'b * 'c) t

p3 represents triplets with given dimensions types.

val p4 : ?meta:('a * 'b * 'c * 'd) Meta.t -> ?name:string -> 'a t -> 'b t -> 'c t -> 'd t -> ('a * 'b * 'c * 'd) t

p4 represents quadruplets with given dimensions types.

Records

Records are represented by named products of named fields.

module Record : sig ... end

Operating on records.

val field : ?meta:'f Meta.t -> ?inject:('r -> 'f -> 'r) -> ?set:('r -> 'f -> unit) -> ?default:'f -> string -> 'f t -> ('r -> 'f) -> ('r, 'f) field

field name g project defines a record field for a record 'r. This is just Field.make.

val record : ?meta:'r Meta.t -> string -> ('r, 'r) fields -> 'r t

record name fields is a record named name with fields field. This is Record.make wrapped in a Record gist value.

Variants

Variants are described by a list of cases and function that indicate how to select the case for a value. A case is a product named after the case name.

module Variant : sig ... end

Operating on variants.

val case : ?meta:'v Meta.t -> string -> ('v, 'v) fields -> 'v Variant.case

case name fields is a variant case with product defined by fields. This is Variant.Case.make.

val variant : ?meta:'v Meta.t -> string -> ('v -> 'v Variant.case) -> 'v Variant.case list -> 'v t

variant name project cases is a variant decontructed by project and whose cases are enumerated in cases. This is Variant.make wrapped in a Variant and Sum gist value.

Sums

The sum type gathers generic variants and a few special cases for variants of the standard library. The specialisation can be converted to a generic variant.

module Sum : sig ... end

Operating on sums.

val option : ?meta:'a option Meta.t -> 'a t -> 'a option t

option represents options of the given representation type as an Option wrapped in a Sum gist value.

val either : ?meta:('a, 'b) Stdlib.Either.t Meta.t -> 'a t -> 'b t -> ('a, 'b) Stdlib.Either.t t

either represents eithers of the given representation types as an Either wrapped in a Sum gist value.

val result : ?meta:('a, 'b) Stdlib.result Meta.t -> 'a t -> 'b t -> ('a, 'b) Stdlib.result t

result represents results of the given representation types as an Result wrapped in a Sum gist value.

val list : ?meta:'a list Meta.t -> 'a t -> 'a list t

list represents lists of the given representation type as List wrapped in a Sum gist value.

Functions

module Func : sig ... end

Operating on functions.

val func : ?meta:('a -> 'b) Meta.t -> 'a t -> 'b t -> ('a -> 'b) t

func ~meta d r represents a function from domain d to range r. This is Func.make wrapped in Func gist value.

val (@->) : 'a t -> 'b t -> ('a -> 'b) t

d @-> r is func d r.

Abstract types

Abstract types are represented by lists of versioned public representations with which they can be converted.

module Abstract : sig ... end

Operating on abstract types.

val abstract : ?meta:'a Meta.t -> string -> reprs:'a Abstract.repr list -> 'a t

abstract ~meta name ~reprs represents an abstract type named name with public representations reprs. This is Abstract.make wrapped in an Abstract gist value.

Gists

val meta : 'a t -> 'a Meta.t

meta g is g's top meta.

val pp_type : Stdlib.Format.formatter -> 'a t -> unit

pp_type g formats a pseudo OCaml type expression for g.