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 printer
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2004-02-28 (18:59)
From: Christophe TROESTLER <debian00@t...>
Subject: [Caml-list] Polymorphic pretty printer

I am trying to create a pretty printer (PP) for a polymorphic data
structure.  The problem is to delegate the printing of the polymorphic
values to the default PP of the toploop.  My best try so far is (on a
boiled down example):

type 'a exp = A of 'a

open Outcometree

let print_out_value_orig = !Toploop.print_out_value

let rec print_out_value ppf = function
  | Oval_constr(Oide_ident(name), [params]) when name = "A" ->
      Format.fprintf ppf "@[<2>(|%a|)@]" print_out_value params
  | tree -> print_out_value_orig ppf tree

let () =
  Toploop.print_out_value := print_out_value

This seems to work

# A (A 3);;
- : int exp exp = (|(|3|)|)

but of course does not because the recursive calls inside
print_out_value_orig call print_out_value_orig and
not !Toploop.print_out_value.  Thus

# [ A 3 ];;
- : int exp list = [A 3]

Also the above code is not so nice as it requires to check for the A
structure manually while this is not necessary with #install_printer.

Any help to solve this problem will be greatly appreciated (I do not
want to copy and modify it).  Maybe a third parameter to the
function passed to #install_printer that would handle the rec call is
all that is needed -- so it would be enough to write something like

let print_exp print ppf (A a) =
  Format.fprintf ppf "@[<2>(|@%a|)@]" print a

#install_printer print_exp;;

Is something like this possible ???


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