From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id KAA08040; Tue, 27 Mar 2001 10:47:42 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id KAA08037 for ; Tue, 27 Mar 2001 10:47:41 +0200 (MET DST) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by concorde.inria.fr (8.11.1/8.10.0) with ESMTP id f2R8iBn17057; Tue, 27 Mar 2001 10:44:11 +0200 (MET DST) Received: (from xleroy@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id KAA08027; Tue, 27 Mar 2001 10:44:11 +0200 (MET DST) Date: Tue, 27 Mar 2001 10:44:11 +0200 From: Xavier Leroy To: John R Harrison Cc: caml-list@inria.fr Subject: Re: [Caml-list] Custom prettyprinter for polymorphic datatype Message-ID: <20010327104411.A7643@pauillac.inria.fr> References: <200103222102.NAA10479@dhpc0010.pdx.intel.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Mailer: Mutt 1.0i In-Reply-To: <200103222102.NAA10479@dhpc0010.pdx.intel.com>; from johnh@ichips.intel.com on Thu, Mar 22, 2001 at 01:02:46PM -0800 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk > 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