caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Polymorphic pretty printer
@ 2004-02-28 19:00 Christophe TROESTLER
  0 siblings, 0 replies; only message in thread
From: Christophe TROESTLER @ 2004-02-28 19:00 UTC (permalink / raw)
  To: O'Caml Mailing List

Hi,

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 oprint.ml 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 ???


Thanks,
ChriS

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2004-02-28 18:59 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-02-28 19:00 [Caml-list] Polymorphic pretty printer Christophe TROESTLER

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).