[ANN] Cmon 0.1, a printer that shares

I am happy to announce that the first version of Cmon is available.

Cmon stands for “CaMl Object Notation”, it is a library for printing values with OCaml syntax.

The unusual feature is that the printer represents sharing by introducing let-binders. It tries to put them at “visually pleasing” positions using the ideas described here.

Printing with let-binders allow handling structures whose serialised representation is exponentially larger than the in-memory one, or where printing wouldn’t terminate because of cyclic values. It is convenient to dump the internal state of tools that make use of sharing a lot, a situation that is common in program analysis tools.

# #install_printer Cmon.format;;
# let rec too_polite =
    lazy Cmon.(cons (string "thank you") (of_lazy too_polite))
  in Cmon.of_lazy too_polite;;
- : Cmon.t = let rec v0 = "thank you" :: v0 in
             v0

The actual formatting is handled by Pprint.

Note that no parser is provided: it is meant only for outputting (with debugging and logging in mind). When used carefully, copy-pasting to an OCaml top-level is enough to recover a value.

A few more examples are provided in the README.

10 Likes