Module Fun.Generic

Generic functions.

See the generic function template to write your own.

Pretty-printing

module Fmt : sig ... end

Custom formatter key.

val pp : 'a Type.Gist.t -> Stdlib.Format.formatter -> 'a -> unit

pp g ppf v formats v as described by g on ppf with a pseudo OCaml syntax similar to what you would see in the toplevel. The function can be selectively overriden on a gist with the Fmt metadata key. Fields whose Type.Gist.Meta.Ignore is true are not printed.

Equality

module Equal : sig ... end

Custom equality key.

val equal : 'a Type.Gist.t -> 'a -> 'a -> bool

equal g v0 v1 determines the equality of values v0 and v1 as described by g. The function can be selectively overriden on a gist with the Equal metadata key. Fields whose Type.Gist.Meta.Ignore is true are ignored (are always equal).

The function raises Invalid_argument on functional values and abstract types without representation; you can ignore them by using Equal.ignore for their Equal key.

Comparison

module Compare : sig ... end

Custom comparison key.

val compare : 'a Type.Gist.t -> 'a -> 'a -> int

compare g v0 v1 compares values v0 and v1 as described by g. The function can be selectively overriden on a gist with the Compare metadata key. Fields whose Type.Gist.Meta.Ignore is true are ignored (are always equal).

The function raises Invalid_argument on function values and abstract types without representation; you can ignore them by using Compare.ignore for their Compare key.

Random generation

module Random : sig ... end

Custom random generator and random sizing keys.

val random : 'a Type.Gist.t -> ?size:int -> ?state:Stdlib.Random.State.t -> unit -> 'a

random g ~state () generates a random value described by g. state is the random state to use, defaults to Random.get_state ().

FIXME do something with Type.Gist.Meta.Ignore.

size is an indicative strictly positive, size factor to let users control an upper bound on the size and complexity of generated values. It should be used by generators to proportionally bound the length of sequences or the number of nodes in trees. It can be altered by gists via the Random.Size key.

The function raises Invalid_argument on functional values (may be lifted in the future) and abstract types without representation; you can represent them by using a suitable Random.Gen.const constant value for the Random.Gen key.

Unmagic & unmarshal

module Unmagic : sig ... end

Custom unmagic key.

val unmagic : 'a Type.Gist.t -> 'b -> ('a, string) Stdlib.result

unmagic g v is Ok v if the memory layout of v is compatible with g.

Note. At the moment a few things are unsupported: bigarrays, functional values.

val safer_unmarshal : 'a Type.Gist.t -> string -> int -> ('a, string) Stdlib.result

safer_unmarshal g s pos is unmagic g (Marshall.from_string s pos).