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 UAA04599; Mon, 30 Aug 2004 20:35:58 +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 UAA06933 for ; Mon, 30 Aug 2004 20:35:56 +0200 (MET DST) Received: from fruitcake.ICSI.Berkeley.EDU (fruitcake.ICSI.Berkeley.EDU [192.150.186.11]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id i7UIZsVC007344 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Mon, 30 Aug 2004 20:35:56 +0200 Received: from tsc.uc3m.es (lemon [192.150.186.44]) by fruitcake.ICSI.Berkeley.EDU (8.12.10/8.12.9) with ESMTP id i7UIZqkf018680; Mon, 30 Aug 2004 11:35:53 -0700 (PDT) Message-ID: <41337388.7030207@tsc.uc3m.es> Date: Mon, 30 Aug 2004 11:35:52 -0700 From: Francisco Valverde User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.2.1) Gecko/20021130 X-Accept-Language: en-us, en MIME-Version: 1.0 To: Jeff Henrikson CC: caml-list@inria.fr Subject: Re: [Caml-list] toplevel printing of polymorphic values References: <20040829012654.GA3011@respighi.local.> In-Reply-To: <20040829012654.GA3011@respighi.local.> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Miltered: at concorde with ID 4133738A.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Loop: caml-list@inria.fr X-Spam: no; 0.00; caml-list:01 henrikson:01 val:01 formatter:01 formatter:01 val:01 breakpoints:01 plist:01 fprintf:01 fprintf:01 plist:01 hov:99 hov:99 sig:01 sig:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk Jeff Henrikson wrote: Hi, what I've been using are polymorphic versions of the pp_print_ in Format. For lists I use these two flavours (of course you can rip them from their modules): module Poly : sig module type PPRINTER = sig type 'a p (** A pretty printer in the spirit of Format.pp_print(ers) for singly polymorphic types *) val pp_print : pp:(Format.formatter -> 'a -> unit) -> Format.formatter -> 'a p -> unit (* [pp_print ~ppa ~ppb fter t] A formatter for polymorphic objects, it formats the data [t] to [fter] with the aid of formatter [pp]. @param pp A pretty printer for the parameter type. @param fter A Format.formatter. @param t The data to be formatted. @return Done entirely for side-effects. *) end module type DELIMITED_PPRINTER = sig type 'a p val pp_print : ?pre:string -> ?pos:string -> ?sep:string -> pp:(Format.formatter -> 'a -> unit) -> Format.formatter -> 'a p -> unit (* [pp_print ?pre ?pos ?sep ~pp fter t] A formatter for polymorphic objects, it formats the data [t] to [fter] with the aid of formatter [pp]. It will first prefix with [pos], postfix with [pos] and intersperse all items with [sep] if they are provided. @param pre A prefix to enclose the string @param pos A postfix to enclose the string @param sep A separator for substructures in the string. @param pp A pretty printer for the parameter type. @param fter A Format.formatter. @param t The data to be formatted. @return Done entirely for side-effects. *) end So that for pretty-printing lists I would use: (** Defaults for pre, pos and sep print more or less in the standard way *) (** The breakpoints are as tricky as ever. These aren't right *) let pp_print ?(pre="[") ?(pos="]") ?(sep=";") ~pp fter = let rec plist fter = function | a::[] -> fprintf fter "%a" pp a | a::rest -> fprintf fter "%a%s@ %a" pp a sep plist rest in function [] -> fprintf fter "@[%s%s@]" pre pos | l -> fprintf fter "@[%s%a%s@]" pre plist l pos So for printing integer lists you can use, # let pp_print_int_list = pp_print ~pp:Format.pp_print_int;; val pp_print_int_list : ?pre:string -> ?pos:string -> ?sep:string -> Format.formatter -> int L.p -> unit = #pp_print_int_list Format.std_formatter [1;2;3];; [1; 2; 3]- : unit = () ------------------------------------ Hope it helps! F. Valverde ------------------- 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