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 allow to directly inject types that satisfy a given interface in the representation.
module type ARRAY = sig ... end
Array interface.
The type for representing array types of type 'a
with elements of type 'elt
.
module type MAP = sig ... end
Map interface.
The type for representing map types of type 'm
mapping keys of type 'k
to values of type 'v
.
module Meta : sig ... end
Gist metadata.
type 'a scalar =
| Unit : unit Meta.t -> unit scalar
| Bool : bool Meta.t -> bool scalar
| Char : char Meta.t -> char scalar
| Uchar : Stdlib.Uchar.t Meta.t -> Stdlib.Uchar.t scalar
| Int : int Meta.t -> int scalar
| Int32 : int32 Meta.t -> int32 scalar
| Int64 : int64 Meta.t -> int64 scalar
| Nativeint : nativeint Meta.t -> nativeint scalar
| Float : float Meta.t -> float scalar
The type for representing scalar types of type 'a
. See scalars.
type ('elt, 'arr) arraylike =
| String : string Meta.t * char t -> (char, string) arraylike
| Bytes : bytes Meta.t * char t -> (char, bytes) arraylike
| Array : 'elt array Meta.t * 'elt t -> ('elt, 'elt array) arraylike
| 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
| 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.
The type for representing map types of type 'm
mapping keys of type 'k
to values of type 'v
. See maplike.
The type for representing product types of type 'p
. See products.
The type for representing variants of type 'v
. See variants.
and 's sum =
| Option : 'a option Meta.t * 'a t -> 'a option sum
| Either : ('a, 'b) Stdlib.Either.t Meta.t
* 'a t
* 'b t -> ('a, 'b) Stdlib.Either.t sum
| Result : ('a, 'b) Stdlib.result Meta.t
* 'a t
* 'b t -> ('a, 'b) Stdlib.result sum
| List : 'a list Meta.t * 'a t -> 'a list sum
| Variant : 'v variant -> 'v sum
The type for representing sum types of type 's
. See sums.
The type for representing functions types 'a -> 'b
. See functions.
The type for representing abstract types of type 'a
. See abstract types.
and 'a t =
| Scalar : 'a scalar -> 'a t
| Arraylike : ('elt, 'arr) arraylike -> 'arr t
| Maplike : ('k, 'v, 'm) maplike -> 'm t
| Product : 'p product -> 'p t
| Record : 'r record -> 'r t
| Sum : 's sum -> 's t
| Func : ('a, 'b) func -> ('a -> 'b) t
| Abstract : 'a abstract -> 'a t
| Lazy : 'a lazy_t Meta.t * 'a t -> 'a lazy_t t
| Ref : 'a Stdlib.ref Meta.t * 'a t -> 'a Stdlib.ref t
| Rec : 'a t lazy_t -> 'a t
Recursion
*)The type for type gists.
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)
.
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))
.
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.
The maplike type gathers generic functional key value maps and a special case for hash tables.
module Maplike : sig ... end
Operations on maplikes.
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 (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.
The type for representing the field of type 'f
of a product of type 'p
. See the Field
module.
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
.
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
.
product dims
is a product with dimensions and construction represented by dims
. This is Product.make
wrapped in a Product
gist value.
p2
represents pairs with given dimensions types.
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 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
.
record name fields
is a record named name
with fields field
. This is Record.make
wrapped in a Record
gist value.
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.
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.
module Func : sig ... end
Operating on functions.
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.
val pp_type : Stdlib.Format.formatter -> 'a t -> unit
pp_type g
formats a pseudo OCaml type expression for g
.