Pretty-printers are provided to the toplevel using the Toploop
module, in ipaddr
this is done as follows:
let printers = [ "Mymodule.pp" ]
let eval_string
?(print_outcome = false) ?(err_formatter = Format.err_formatter) str =
let lexbuf = Lexing.from_string str in
let phrase = !Toploop.parse_toplevel_phrase lexbuf in
Toploop.execute_phrase print_outcome err_formatter phrase
let rec install_printers = function
| [] -> true
| printer :: printers ->
let cmd = Printf.sprintf "#install_printer %s;;" printer in
eval_string cmd && install_printers printers
let () =
if not (install_printers printers) then
Format.eprintf "Problem installing Mymodule-printers@."
I would like to provide pretty-printers for Mymodule.F
which is a functor.
Toploop
has three functions that seem relevant:
val Toploop.install_printer
Path.t -> Types.type_expr -> (Format.formatter -> Obj.t -> unit) -> unit
val Toploop.install_generic_printer
Path.t ->
Path.t ->
(int ->
(int -> Obj.t -> Outcometree.out_value, Obj.t -> Outcometree.out_value)
gen_printer) ->
unit
val Toploop.install_generic_printer'
Path.t ->
Path.t ->
(Format.formatter -> Obj.t -> unit, Format.formatter -> Obj.t -> unit)
gen_printer -> unit
Unfortunately none of these have docstrings attached, so I’m a bit confused as to how to use them.
I think I understand the Toploop.install_printer
signature, but I’m not sure how to define a functor using Types.type_expr
; the others I have no clue about, but Obj.t
looks scary.
I would be very happy if someone could help me out! <3
EDIT: Oh, I forgot to provide an example signature. mymodule.mli
:
module F (Whatever : sig end) : sig
type t
val pp : Format.formatter -> t -> unit
end
I’d like to instruct the toplevel that no matter the module argumentWhatever
, F(Whatever).t
should be printed using F(Whatever).pp
.