Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] Polymorphic pretty printing
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Christophe TROESTLER <debian00@t...>
Subject: Re: [Caml-list] Polymorphic pretty printing
On Mon, 18 Oct 2004, Andrej Bauer <> wrote:
> how to install a pretty printer for a _polymorphic_ type.
> I should use Toplevel.print_out_value to do this. But,
> print_out_value expects an arguments of type Outcometree.out_value,
> and it is not clear to me where I will get it.

Since you will register a new printing function, you will not have to
get it, Outcometree.out_value will be provided to your function!  To
get you a flavor of how (I understand) it works, here is some code:

let oldpp = !Toploop.print_out_value
let pp f o =
  (match o with
   | Outcometree.Oval_list _ ->
       Format.fprintf f "list:"
   | _ -> ());
    oldpp f o
Toploop.print_out_value := pp

Now, a list is written as

# [1;2];;
- : int list = list:[1; 2]

> Am I supposed to rewrite half of to get this working?

I am afraid that the awser is yes :(.  Let's see why:

# ([1; 2], 2);;
- : int list * int = ([1; 2], 2)

Now the above [pp] pretty printer is registered but it does not to
work.  This is because the original function registered in
[Toploop.print_out_value] is recursive instead of calling
[!Toploop.print_out_value] (there is certainly a reason but I do not
know it) -- thus since the "top" structure is a couple, [oldpp] is
called and never calls back [pp].

Therefore, not only we have to duplicate all the code of the original
[!Toploop.print_out_value] (and check conformance at every upgrade)
but that also means it is difficult for several polymorphic pretty
printers to cooperate.

This is really unfortunate as this prevents libraries to use
[Sys.interactive] in order to install pretty printers for their
polymorphic types.

Hope a nice solution can be designed for this last usage.


To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: