The OCaml repl (“toplevel”) has rich printing for any types, user-defined or otherwise. Is it possible to access this functionality outside the toplevel, rather than having to write an entirely custom set of value printers for one’s own entire set of types?
The OCaml repl (toplevel) has rich printing for any types, user-defined or otherwise. Is
Share
The pretty-printing facility is part of the toplevel library. You’ll find the source in
toplevel/genprintval.ml. It’s understandable, considering that it needs type information: you can’t just throw any value at it, the choice of pretty-printer is based on the type.If you want to use this code in your program, you’ll need to link with the toplevel library (
toplevellib.cma) or compile ingenprintval(which means bringing in enough bits of the type checker to analyse the type, it can get pretty big).There is a similar facility (but not sharing the code, I think) in the debugger (
debugger/printval.mlanddebugger/loadprinter.ml).There are third-party libraries that you can directly link against and that provide pretty-printing facilities. Extlib‘s
Std.dumpprovides a very crude facility (not based on the type). Deriving by Jeremy Yallop and Jake Donham is another approach. This Caml Weekly News item offers more suggestions.