Hello,
It’s my pleasure to announce the first release of typegist:
Typegist represents the essence of OCaml types as values.
This dynamic type representation can be used to devise generic type-indexed functions – value serializers, printers, parsers, differs, random generators, editors, ffi glue, etc. Any accessible type can be described up to the limits defined by its public interface.
Typegist does not model OCaml’s type language in full detail, but focuses on a core structural subset decorated with typed-indexed metadata to provide an ergonomic interface for both producers and processors of the representation.
Typegist is distributed under the ISC license. It has no dependencies.
As mentioned above these values only partially model OCaml’s type definition language, that’s the reason why they are Type.Gist.t and not Type.Repr.t values. You should see typegist as a data interfacing language for your types rather than a faithful or canonical representation of your types (which I find less useful in practice).
The representation special cases and annotates some of the Stdlib types: being too generic and losing all semantics in favour of generalized abstract non-sense is undesirable when you interface with other systems. For example. You want list values to show up as arrays in JSON, not as nested cons case objects. You want None to map to null not to a constant case object. You want string values that hold textual data to show up as plain JSON strings rather than hex digits or base64. Etc.
This means that part of the representation is decidedly ad-hoc. It balances precision and genericity while making it reasonably easy to devise your own gist processors without getting bogged into pointless details of OCaml’s type expression language.
So next time it’s time for you to write an M.pp : t Fmt.t function, write an M.gist : t Type.Gist.t instead. You’ll get your printer and more. A companion release of jsont was made with the new optional jsont.typegist library that translates type gists into jsont JSON types for your JSON serialization pleasure (if that exists).
While I don’t expect typegist to change much, it hasn’t been used in anger yet – but I’ll waste no time. It’s again a design that has been rotting for too long in a repo. This means that changes in the representation could still occur based on feedback if more precision is needed or better representation are found. However I’d expect such changes to mostly affect gist processors. Get in touch on the issue tracker if you run into difficulties or improvements.
I have no plan to propose any mean to automate gist derivations from type definitions, but some people have expressed interest in doing that in the past.
Happy typed-indexed programming!
This first release was made possible thanks to a grant from the OCaml Software Foundation. I also thank my donors for their support.
Homepage: https://erratique.ch/software/typegist
Docs: https://erratique.ch/software/typegist/doc or odig doc typegist
Install: opam install typegist (opam PR)
Best,
Daniel
P.S. The API makes use – for good – of every new type gimick that was introduced in OCaml 5.5 :–)