# Automatically installing toplevel printers from different file

**URL:** <https://discuss.ocaml.org/t/automatically-installing-toplevel-printers-from-different-file/12884>\
**Category:** Learning\
**Created:** [August 22, 2023, 9:32pm UTC](https://discuss.ocaml.org/t/automatically-installing-toplevel-printers-from-different-file/12884 "2023-08-22T21:32:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nhuber](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/nhuber/32/3939_2.png) [@nhuber](https://discuss.ocaml.org/u/nhuber)\
**Post date:** [August 22, 2023, 9:32pm UTC](https://discuss.ocaml.org/t/automatically-installing-toplevel-printers-from-different-file/12884/1 "2023-08-22T21:32:58Z")

</div>

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

```auto
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 🙂  
/Nick

---

<div class="post-metadata">

**Author:** ![Chet\_Murthy](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/chet_murthy/32/1501_2.png) [@Chet\_Murthy](https://discuss.ocaml.org/u/Chet_Murthy)\
**Post date:** [August 23, 2023, 12:53am UTC](https://discuss.ocaml.org/t/automatically-installing-toplevel-printers-from-different-file/12884/2 "2023-08-23T00:53:59Z")

</div>

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!)

---

<div class="post-metadata">

**Author:** ![sim642](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/sim642/32/3597_2.png) [@sim642](https://discuss.ocaml.org/u/sim642)\
**Post date:** [August 23, 2023, 6:37am UTC](https://discuss.ocaml.org/t/automatically-installing-toplevel-printers-from-different-file/12884/3 "2023-08-23T06:37:43Z")

</div>

There’s `Uri` example in utop documentation: [GitHub - ocaml-community/utop: Universal toplevel for OCaml](https://github.com/ocaml-community/utop#automatically-installing-toplevel-printers). That has it in a `.mli` file: [https://github.com/mirage/ocaml-uri/blob/56c143bcdfb0066c11dd897fba0c90f482bce450/lib/uri.mli#L275-L277](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.

---

<div class="post-metadata">

**Author:** ![Chet\_Murthy](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/chet_murthy/32/1501_2.png) [@Chet\_Murthy](https://discuss.ocaml.org/u/Chet_Murthy)\
**Post date:** [August 23, 2023, 7:22am UTC](https://discuss.ocaml.org/t/automatically-installing-toplevel-printers-from-different-file/12884/4 "2023-08-23T07:22:53Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dbuenzli](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/dbuenzli/32/18_2.png) [@dbuenzli](https://discuss.ocaml.org/u/dbuenzli)\
**Post date:** [August 23, 2023, 7:52am UTC](https://discuss.ocaml.org/t/automatically-installing-toplevel-printers-from-different-file/12884/5 "2023-08-23T07:52:26Z")

</div>

> [@Chet\_Murthy](#):
>
> This appears to be something specific to `utop` ?

Yes.

[There was](https://github.com/ocaml/ocaml/issues/7770) an issue upstream about this but it has been closed by “the friendly bot”.
