Automatically installing toplevel printers from different file

Hej!

I have a short question. For a compiler I am writing I have a module Ast, which holds the definition of a couple of datatypes that make up the abstract syntax tree (which has type Ast.t). I have also defined a format function, however, since the type definitions of the AST are already quite large, I have decided to move the fmt function into its own module Ast_printer. Now I would like to install the printer in the toplevel. However, it seems that the usual

val fmt : Format.formatter -> Ast.t -> unit
[@@ocaml.toplevel_printer]

in the Ast_printer.mli file does not do the trick. I assume that the reason is because I have defined the printer in a different file as the datatype, is that correct? If so, is there any way of still having the toplevel printer automatically loaded when I start utop? Since the Ast type definitions are changing quite a bit, I would like to avoid having both an Ast.mli and Ast.ml file, since I keep having to copy the changes between those two …

Thanks in advance :slight_smile:
/Nick

I didn’t know about this annotation at all! Do you have a pointer to some description of how it works? maybe if I could have a look at that, I might even be able to answer your question! (ha!)

There’s Uri example in utop documentation: GitHub - ocaml-community/utop: Universal toplevel for OCaml. That has it in a .mli file: https://github.com/mirage/ocaml-uri/blob/56c143bcdfb0066c11dd897fba0c90f482bce450/lib/uri.mli#L275-L277. So that shouldn’t be an issue, but I don’t know more about this.

o.i.c. This appears to be something specific to utop ? I only use the standard ocaml toplevel, so have no experience with that. Ah, well, sorry I can’t be of help.

If I were to debug this, I’d start by making sure that if I did #install_printer with the function fmt, that things worked as desired. If they didn’t, then I’d check with the standard ocaml toplevel (to eliminate utop).

If they did work, then I’d go dig thru the source to utop and find the code that pulled out this annotation, and add a bit of logging, so I could watch what it did, that might give an idea of what’s going wrong.

Of course, I’d start by reproducing with a contrived and extremely-small example, just to eliminate the possibility that it’s something in my own code.

Yes.

There was an issue upstream about this but it has been closed by “the friendly bot”.