Module Vprint


module Vprint: sig .. end
A runtime value printer for OCaml. Be warned! It's extremely experimental and dangerous. No assurance at all, use at your own risk!


Formatter type

type fmt 
Type of formatter
exception Fmt_Error of string
Format exception, raised when the given formatter is wrong (i.e. can not unify with) the data

Common format constructors

val __ : fmt
The "any" formatter, compatible with anything. In many case, __ is fairly enough to show the data, though maybe not so accurately; in some case, __ can identify the exact type and show the final result
val _i : fmt
The int formatter, not for int32/int64/nativeint (they don't need specific formatters)
val _c : fmt
The char formatter
val _f : fmt
The float formatter, its not necessary but we provide it anyway
val _s : fmt
The string formatter its not necessary but we provide it anyway
val _l : fmt -> fmt
the list formmater which take another formatter as argument to produce the list version of that formmater. _l sth_fmt means a formmater for a list sth. E.g. _l _i is the formatter for a list of int
val _a : fmt -> fmt
array formatter
val _t : fmt -> fmt
tuple formatter, check *~ for how to compose a formatter out of a collection of fmts
val _r : fmt -> fmt
record formatter
val _v : fmt -> fmt
variants formatter, for variants with at least one argument
val _v0 : fmt
variants formatter, for variants with no argument. Note that _v __ or _v_ can also cover this case.
val _'v : fmt -> fmt
polymorphic variants formatter, for variants with at least one argument
val _'v0 : fmt
polymorphic variants formatter, for variants with no argument. Note that _'v __ or _'v_ can also cover this case
val _u : fmt
unit () formatter

Other format constructors

val _fun : fmt -> fmt
function formatter, whose paramter is the formatter of the closure's environment
val _obj : fmt -> fmt
object formatter, whose paramter is the formatter of the object's instance variables etc.
val _ref : fmt -> fmt
reference formatter
val _lazy : fmt -> fmt
lazy value formatter

Shorthand definitions of formatters


... and longhand definitions, in case you care more about the readability
val _l_ : fmt
_l_ = _l __, i.e. any lists
val _a_ : fmt
_a_ = _a __, i.e. any array
val _t_ : fmt
_t_ = _t __, i.e. any tuple
val _r_ : fmt
_r_ = _r __, i.e. any record
val _v_ : fmt
_v_ = _v __, i.e. any variants, and it covers _v0
val _'v_ : fmt
_'v_ = _'v __, i.e. any variants, and it covers _'v0
val _int : fmt
_int = _i
val _char : fmt
_char = _c
val _float : fmt
_float = _f
val _string : fmt
_string = _s
val _list : fmt -> fmt
_list = _l
val _array : fmt -> fmt
_array = _a
val _tuple : fmt -> fmt
_tuple = _t
val _record : fmt -> fmt
_record = _r
val _variant : fmt -> fmt
_variant = _v
val _variant0 : fmt
_variant0 = _v0
val _'variant : fmt -> fmt
_'variant = _'v
val _'variant0 : fmt
_'varianto = _'v0
val _unit : fmt
_unit = _u

Formatter combinators

val (*~) : fmt -> fmt -> fmt
AND combinator, it combines a sequence of individual formatter to a formatters collection. It corresponds to the * of OCaml's type definition, e.g. _i *~ _f *~ _s corresponds to type (int * float * string). c.f. a pitfull at the end of the document.
val (//) : fmt -> fmt -> fmt
OR combinator, it connects two individual formatter to form a new formatter which compatible with either of them. It's usually used within variants, e.g. _v (_i // _f) or _v _i // _v _f can be used to match type t = A of int | B of float
val (@.) : (fmt -> fmt) -> fmt -> fmt
OF combinator, it's basicly a apply function f @. x = f x, You can read it as "of", e.g. _l @. _l _i stands for list of int list. The purpose of OF combinator is to reduce parenthesis. E.g. instead of writing _l (_v (_v (_l _i // _i *~ _f))), you can write _l @. _v @. _v @. _l _i // _i *~ _f

Print commands

val print : ?fmt:fmt -> 'a -> unit
Print any OCaml data to stdout. print ~fmt:f a print data to stdout according to formatter f, or raise a Fmt_Error exception when the given formatter f is not appropriate for the input data. The fmt field is optional, the default formatter is __. c.f. print_all for more details
val sprint : ?fmt:fmt -> 'a -> string
Print any OCaml data to string. The same as print, but instead, output the result as a string
val print_all : ?fmt:fmt -> 'a -> unit
Print all kinds of data which having the same memory layout as the input data to the stdout. When using the default formatter __ or a formatter not specific enought, the system will find that there could be more than one kinds of data having the same memory layout as the input data. Due to the absence of type information at running time, the system won't be able to figure out its exact data type. In such cases, print print the data with a generic printer (but still try to be as specific as it could, based on the information given by the formatter), on the other hand, print_all will print all the possibilities, each with its specific printer
val sprint_all : ?fmt:fmt -> 'a -> string Stream.t
Like print_all, but output a stream of string instead.

Pitfull: since *~ is defined as common infix function, it won't be able to distinguish between a *~ b *~ c and (a *~ b) *~ c and consider them as the same. In case you need the later, you must use the tuple formatter _t to explicitly group them such as _t (a *~ b) *~ c. However, this is probably the only situation you need to worry about, the system does try to figure it out in most other situations such a *~ (b *~ c) *~ d or (a *~ b) *~ (c *~ d).