In OCaml, the OCaml toplevel has a directive #install_printer which registers a function to print values. To use this,

  • we first write a pretty printing function (say, pp) of type Format.formatter -> t -> unit
  • invoke #install_printer pp in the toplevel

Then after that, pp will be used to print objects of type t.

For example, we can combine Format.fprintf to do this.

1
2
3
4
5
let kupo_pp fmt s = Format.fprintf fmt "%s kupo" s;;
#install_printer kupo_pp;;

let h = "Hello"
(* val h : stirng = Hello kupo *)

We can also pretty print for modules.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
(* First expose it in signature *)
module type Stack = sig
type 'a t
exception Empty
val empty : 'a t
val is_empty : 'a t -> bool
val push : 'a -> 'a t -> 'a t
val peek : 'a t -> 'a
val pop : 'a t -> 'a t
val size : 'a t -> int
val pp :
(Format.formatter -> 'a -> unit) -> Format.formatter -> 'a t -> unit
end

(* Then implement it *)
module ListStack : Stack = struct
type 'a t = 'a list
exception Empty
let empty = []
let is_empty = function [] -> true | _ -> false
let push x s = x :: s
let peek = function [] -> raise Empty | x :: _ -> x
let pop = function [] -> raise Empty | _ :: s -> s
let size = List.length
let pp pp_val fmt s =
let open Format in
let pp_break fmt () = fprintf fmt "@," in
fprintf fmt "@[<v 0>top of stack";
if s <> [] then fprintf fmt "@,";
pp_print_list ~pp_sep:pp_break pp_val fmt s;
fprintf fmt "@,bottom of stack@]"
end

(* Install printer *)
#install_printer ListStack.pp;;

(* Then it will display as we want *)

ListStack.empty
(**
- : 'a ListStack.t = top of stack
bottom of stack
*)

ListStack.(empty |> push 1 |> push 2)
(**
- : int ListStack.t = top of stack
2
1
bottom of stack
*)