learn-ocaml icon indicating copy to clipboard operation
learn-ocaml copied to clipboard

Custom datatype printers?

Open tsani opened this issue 7 years ago • 3 comments

Suppose I have a datatype for expressions such as

type exp =
  | Num of int
  | Plus of exp * exp
  | Times of exp * exp

I would like to display these trees using an infix notation, so I can define a custom pretty-printer for them. Is there a way to register printers with learn-ocaml so that the datatype is printed according to the custom printer in the toplevel as well as when the automatic grader shows computing ... in the report?

Edit: upon further investigation, the pretty-printing routine in the test_lib.ml ultimately calls out to Toploop.print_value, which I believe will use printers registered through Toploop.install_printer. So if Toploop is exposed to test.ml, it should be possible to install custom printers.

tsani avatar Oct 18 '18 13:10 tsani

In fact, in (eg) prepare.ml, you can do #install_printer your_printer;; : this will register your printer, which will be used in the test reports and in the toplevel.

Armael avatar Nov 08 '18 14:11 Armael

When I try this, Cannot find type Topdirs.printer_type_new is printed in the toplevel and no printer is registered.

wyschean avatar Oct 14 '19 22:10 wyschean

Hi With learn-ocaml 0.12, I also experienced Cannot find type Topdirs.printer_type_new just as @wyschean. But with the recent learnocaml 0.13.0 (docker image) this seems to be fixed, and #install_printer in prepare.ml does work correctly for me (custom display of values both in the toplevel and in the grading report).

letouzey avatar Oct 07 '21 10:10 letouzey