caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Xavier Leroy <Xavier.Leroy@inria.fr>
To: John R Harrison <johnh@ichips.intel.com>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Custom prettyprinter for polymorphic datatype
Date: Tue, 27 Mar 2001 10:44:11 +0200	[thread overview]
Message-ID: <20010327104411.A7643@pauillac.inria.fr> (raw)
In-Reply-To: <200103222102.NAA10479@dhpc0010.pdx.intel.com>; from johnh@ichips.intel.com on Thu, Mar 22, 2001 at 01:02:46PM -0800

> 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


  reply	other threads:[~2001-03-27  8:47 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-03-22 21:02 John R Harrison
2001-03-27  8:44 ` Xavier Leroy [this message]
2001-04-05  8:07   ` [Caml-list] Does ocamldebug work for the non-cygwin version of Ocaml 3.01 for Windows? Mattias Waldau
2001-04-06 15:38     ` Xavier Leroy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20010327104411.A7643@pauillac.inria.fr \
    --to=xavier.leroy@inria.fr \
    --cc=caml-list@inria.fr \
    --cc=johnh@ichips.intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).