| 1 | (** [Shape.t] are constructed by the [bin_shape] syntax extension from OCaml type |
| 2 | definitions & expressions. |
| 3 | |
| 4 | There is a direct mapping from ocaml type definition syntax to the corresponding |
| 5 | [Shape.group] and from ocaml type expression syntax to the corresponding [Shape.t]. |
| 6 | *) |
| 7 | type t [@@deriving sexp_of] |
| 8 | |
| 9 | (** [Tid.t] & [Vid.t] are identifiers for type-constructors & type-vars. |
| 10 | i.e. Given [type 'a t = ... ] *) |
| 11 | |
| 12 | module Tid : sig |
| 13 | (* [t] *) |
| 14 | type t |
| 15 | |
| 16 | val of_string : string -> t |
| 17 | end |
| 18 | |
| 19 | module Vid : sig |
| 20 | (* ['a] *) |
| 21 | type t |
| 22 | |
| 23 | val of_string : string -> t |
| 24 | end |
| 25 | |
| 26 | (** [Location.t] is required when constructing shapes for which evaluation might fail. *) |
| 27 | module Location : sig |
| 28 | type t |
| 29 | |
| 30 | val of_string : string -> t |
| 31 | end |
| 32 | |
| 33 | (** [Uuid.t] is used by [basetype] and [annotate]. *) |
| 34 | module Uuid : sig |
| 35 | type t |
| 36 | |
| 37 | (** [of_string s] returns a [Uuid.t] wrapping [s]. |
| 38 | There are currently no requirements of the format of [s] although it is common to |
| 39 | use string in `uuid' format: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX |
| 40 | There is also no attempt to detect & reject duplicates *) |
| 41 | val of_string : string -> t |
| 42 | |
| 43 | val to_string : t -> string |
| 44 | end |
| 45 | |
| 46 | (** group of mutually recursive type definitions *) |
| 47 | type group |
| 48 | |
| 49 | (** This function is generative; repeated calls create distinct groups *) |
| 50 | val group : Location.t -> (Tid.t * Vid.t list * t) list -> group |
| 51 | |
| 52 | val tuple : t list -> t |
| 53 | val record : (string * t) list -> t |
| 54 | val variant : (string * t list) list -> t |
| 55 | |
| 56 | type poly_variant_row |
| 57 | |
| 58 | val constr : string -> t option -> poly_variant_row |
| 59 | val inherit_ : Location.t -> t -> poly_variant_row |
| 60 | val poly_variant : Location.t -> poly_variant_row list -> t |
| 61 | |
| 62 | (** recursive apps within the current group *) |
| 63 | val rec_app : Tid.t -> t list -> t |
| 64 | |
| 65 | (** apps from outside the group *) |
| 66 | val top_app : group -> Tid.t -> t list -> t |
| 67 | |
| 68 | val var : Location.t -> Vid.t -> t |
| 69 | |
| 70 | (** Built-in types and types with custom serialization: i.e. int,list,... To avoid |
| 71 | accidental protocol compatibility, pass a UUID as the [string] argument *) |
| 72 | val basetype : Uuid.t -> t list -> t |
| 73 | |
| 74 | (** [a = annotate s t] creates a shape [a] distinguished, but dependent on shape [t]. |
| 75 | Very much as [record [(s,t)]] does. |
| 76 | But with [annotate] the ocaml record type does not exist. *) |
| 77 | val annotate : Uuid.t -> t -> t |
| 78 | |
| 79 | module Stable : sig |
| 80 | module V1 : sig |
| 81 | type nonrec t = t [@@deriving equal, sexp] |
| 82 | end |
| 83 | end |
| 84 | |
| 85 | (** [Shape.Canonical.t] is the result of [eval]uating a shape to a canonical form, and |
| 86 | represents the shape of OCaml types w.r.t. bin_io serialization. |
| 87 | |
| 88 | The idea is that de-serialization is safe if the canonical-shape for the type produced |
| 89 | by de-serialization is equivalent to the canonical-shape of the serialized type. |
| 90 | |
| 91 | The representation is canonical, so equivalence is structural equality. |
| 92 | |
| 93 | [Canonical.t] also provides a useful human level description of a type. |
| 94 | |
| 95 | A [Canonical.t] can be `digested' to a [Digest.t], and except for nearly impossible |
| 96 | hash collisions, equality of the digests implies equality of canonical-shapes and |
| 97 | hence equivalence at the Shape.t level. |
| 98 | |
| 99 | [Canonical.t] may also be constructed with various functions: |
| 100 | [annotate, basetype, tuple, record, variant, poly_variant, fix, recurse, ..] |
| 101 | which might be used when setting up unit tests or expected shapes. *) |
| 102 | |
| 103 | module Digest : sig |
| 104 | type t [@@deriving compare, sexp] |
| 105 | |
| 106 | val to_hex : t -> string |
| 107 | val to_md5 : t -> Md5_lib.t |
| 108 | val of_md5 : Md5_lib.t -> t |
| 109 | end |
| 110 | |
| 111 | module Expert : sig |
| 112 | module Sorted_table : sig |
| 113 | type 'a t [@@deriving compare, sexp_of] |
| 114 | |
| 115 | val expose : 'a t -> (string * 'a) list |
| 116 | end |
| 117 | |
| 118 | module Canonical_exp_constructor : sig |
| 119 | type 'a t = |
| 120 | | Annotate of Uuid.t * 'a |
| 121 | | Base of Uuid.t * 'a list |
| 122 | | Tuple of 'a list |
| 123 | | Record of (string * 'a) list |
| 124 | | Variant of (string * 'a list) list |
| 125 | | Poly_variant of 'a option Sorted_table.t |
| 126 | | Application of 'a * 'a list |
| 127 | | Rec_app of int * 'a list |
| 128 | | Var of int |
| 129 | [@@deriving compare, sexp_of] |
| 130 | end |
| 131 | |
| 132 | module Canonical : sig |
| 133 | module Exp1 : sig |
| 134 | type t0 = Exp of t0 Canonical_exp_constructor.t [@@deriving compare, sexp_of] |
| 135 | end |
| 136 | |
| 137 | type t = Exp1.t0 [@@deriving compare, sexp_of] |
| 138 | end |
| 139 | end |
| 140 | |
| 141 | module Canonical : sig |
| 142 | type t = Expert.Canonical.t [@@deriving compare, sexp_of] |
| 143 | |
| 144 | val to_string_hum : t -> string |
| 145 | val to_digest : t -> Digest.t |
| 146 | |
| 147 | module Exp : sig |
| 148 | type t |
| 149 | end |
| 150 | |
| 151 | module Def : sig |
| 152 | type t |
| 153 | end |
| 154 | |
| 155 | module Create : sig |
| 156 | (** [Create.create defs exp] constructs a canonical-shape. The [defs] give context for |
| 157 | sub-expressions of the form: [apply n exps]; [n] being a reference to the n'th |
| 158 | definition in [defs]. |
| 159 | |
| 160 | Definition are required for [record]s and [variant]s, but may also occurs for any |
| 161 | cyclic expression: in this case being constructed using [define]. |
| 162 | |
| 163 | Within a definition body, [var i] refers to the i'th formal type-var, and |
| 164 | corresponds to the i'the argument of an application [args]. *) |
| 165 | val annotate : Uuid.t -> Exp.t -> Exp.t |
| 166 | |
| 167 | val basetype : Uuid.t -> Exp.t list -> Exp.t |
| 168 | val tuple : Exp.t list -> Exp.t |
| 169 | val poly_variant : Location.t -> (string * Exp.t option) list -> Exp.t |
| 170 | val var : int -> Exp.t |
| 171 | val apply : Def.t -> Exp.t list -> Exp.t |
| 172 | val recurse : int -> Exp.t list -> Exp.t |
| 173 | val define : Exp.t -> Def.t |
| 174 | val record : (string * Exp.t) list -> Exp.t |
| 175 | val variant : (string * Exp.t list) list -> Exp.t |
| 176 | val create : Exp.t -> t |
| 177 | end |
| 178 | end |
| 179 | |
| 180 | (** [eval t] returns the canonical-shape for a shape-expression [Shape.t]. Type aliases |
| 181 | are expanded, so that no [Tid.t] or [Vid.t] have significance in the resulting |
| 182 | canonical-shape. Type-recursion, including non-regular recursion, is translated to the |
| 183 | de-bruijn representation used in canonical-shapes. *) |
| 184 | val eval : t -> Canonical.t |
| 185 | |
| 186 | (** [eval_to_digest t] returns a hash-value direct from the [Shape.t], potentially |
| 187 | avoiding the intermediate [Canonical.t] from being constructed. This is important as |
| 188 | the size of a canonical-shape might be exponential in terms of the size of the shape |
| 189 | expression. The following holds: |
| 190 | [ Digest.(eval_to_digest exp = Canonical.to_digest (eval exp)) ] *) |
| 191 | val eval_to_digest : t -> Digest.t |
| 192 | |
| 193 | (** [eval_to_digest_string t] == [Digest.to_hex (eval_to_digest t)] |
| 194 | Convenience function useful for writing unit tests. *) |
| 195 | val eval_to_digest_string : t -> string |
| 196 | |
| 197 | module For_typerep : sig |
| 198 | val deconstruct_tuple_exn : t -> t list |
| 199 | end |