Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Custom prettyprinter for polymorphic datatype
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Xavier Leroy <Xavier.Leroy@i...>
Subject: Re: [Caml-list] Custom prettyprinter for polymorphic datatype
> In CAML Light and OCaml, how do I install a custom printer for a
> polymorphic datatype so that the subcomponents print in the usual way?
> If I could just get hold of a "standard printer" function
>   print_value : 'a->unit
> then presumably I could use that. Is this possible? Or is there a
> better way?

It is not currently possible.  The system printer works by traversing
both the value to be printed and its type.  In the toplevel loop, the
type is provided by the type-checker.  But user-defined printers have
no convenient way to recover and manipulate this type information, so
they can't call back the system printer.

There is no really good solution to this problem.  The workaround that
I use is to write custom printers for polymorphic datatypes that are
parameterized by printers for the type parameters, e.g.

        print_list : ('a -> unit) -> 'a list -> unit
or
        print_hashtable : ('a -> unit) -> ('b -> unit) ->
                                ('a, 'b) Hashtbl.t -> unit

Then, I partially apply these printers to the (monomorphic) types of
interest and install the partial applications as custom printers:

        let print_int_list = print_list Format.print_int;;
        #install_printer print_int_list;;
        let print_string_list = print_list Format.print_string;;
        #install_printer print_string_list;;

Provided there are only a few list types you're interested in, this
can work.  But I agree a more general solution would be nicer.

- Xavier Leroy
-------------------
To unsubscribe, mail caml-list-request@inria.fr.  Archives: http://caml.inria.fr