From: Francisco Valverde <fva@tsc.uc3m.es>
To: Jeff Henrikson <jehenrik@yahoo.com>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] toplevel printing of polymorphic values
Date: Mon, 30 Aug 2004 11:35:52 -0700 [thread overview]
Message-ID: <41337388.7030207@tsc.uc3m.es> (raw)
In-Reply-To: <20040829012654.GA3011@respighi.local.>
Jeff Henrikson wrote:
Hi,
what I've been using are polymorphic versions of the pp_print_<type> 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 "@[<hov 2>%s%s@]" pre pos
| l -> fprintf fter "@[<hov 2>%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 = <fun>
#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
prev parent reply other threads:[~2004-08-30 18:35 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2004-08-29 1:26 Jeff Henrikson
2004-08-30 18:35 ` Francisco Valverde [this message]
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=41337388.7030207@tsc.uc3m.es \
--to=fva@tsc.uc3m.es \
--cc=caml-list@inria.fr \
--cc=jehenrik@yahoo.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