package cmon
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Module Cmon
Source
"Caml Object Notation", a library for pretty-printing ocaml values with sharing.
Unique identifiers to make sharing explicit. Variable names are automatically generated.
type t =
The output is the syntax of OCaml values (with unique identifiers for structured values) extended with variables and let-bindings to represent sharing.
Primitive values
print a literal string (without quoting), useful for non-parameterized data constructors. constant "None"
prints `None`.
print a parameterized dataconstructor. constructor "Some" unit
prints `Some ()`.
print an OCaml record. record ["a", int 1; "b", bool false]
prints `a: 1, b: false
`.
construct a cons cell. cons (int 1) nil
prints `1
`, cons (int 1) (constant "xs")
prints `1 :: xs`,
Shortcut for a data constructor with multiple arguments. construct "None" []
= constant "None"
prints `None`, construct "Some" [int 1]
= constructor "Some" (int 1)
prints `Some 1`, construct "A" [int 1; int 2] = [constructor "A" (tuple [int 1; int 2])] prints `A (1, 2)`.
Shortcut for constructor with inline record. crecord "A" ["a", int 1; "b", bool false]
prints `A a: 1, b: false
`.
list xs
= List.fold_right cons xs nil
. list [int 1; int 2; int 3]
prints `1; 2; 3
`.
Variants that prevent sharing these values
Rewrite a value, introducing let-binders to make sharing explicit.
Print the value as it is (without changing sharing) to a PPrint.document
.
Print the value with explicit sharing to a PPrint.document
. print t == print_as_is (explicit_sharing t)
.
Print a PPrint document on a formatter while trying to follow respect the margin specification of the formatter.
Format the value as it is (without changing sharing) to a Format.formatter
Format the value with explicit sharing to a Format.formatter
. format t == format_as_is (explicit_sharing t)
.
To display cmon values in a top-level, you can use #install_printer format. For instance:
utop # Cmon.unit;;
- : Cmon.t = Cmon.Unit utop # #install_printer Cmon.format;; utop # Cmon.unit;;
- : Cmon.t = ()