* Request for comments: Printf list conversion
@ 2006-01-12 9:19 Alessandro Baretta
2006-01-12 9:31 ` [Caml-list] " Jean-Marc EBER
` (2 more replies)
0 siblings, 3 replies; 11+ messages in thread
From: Alessandro Baretta @ 2006-01-12 9:19 UTC (permalink / raw)
To: Ocaml
I would like to transform the following rather vague idea into a formal feature
request. Before doing this, I would like to know what others think about it.
I continually come across the need to pretty-print via Printf a list of
values--for example, let's say an int list. Printf has conversion specifiers for
int, but int list cannot be handled easily. What I usually end up doing is
converting values in the list to strings, concatenate them, and output the
result with the %s conversion specifier.
Printf.bprintf buf "A beatiful list of ints: [ %s ]\n" (String.concat " ;"
(List.map string_of_int [ 1;2;3;4;5 ] )
Obviously, this is radically ineffiecient. A better solution might be the following.
Printf.bprintf buf "A beatiful list of ints: [ %t ]\n" (fun buf ->
List.iter (Printf.bprintf buf " %d;") [ 1;2;3;4;5 ]
)
The above is adequately fast, but terribly ugly.
I believe that it should be possible to add a conversion-flag--let me use '&' as
an example--telling the type-system and the Printf module that the corresponding
parameter is a list of values whose type is determined by the
conversion-specifier proper. Let me show what I would like to be able to write:
Printf.bprintf buf "A beatiful list of ints: [ %&d ]\n" [ 1;2;3;4;5 ]
Here, the &-flag would tell the type-system that the first parameter is an 'a
list. The fact that 'a = int is to be inferred from the actual conversion
specifier ('d'). Upon recognizing a %& conversion, Printf would have to iterate
over the corresponding parameter with the appropriate conversion
(format_int_with_conv, in this case).
Now, of course, the '&' conversion-flag does not settle the issue, as a
parameter like the first one of String.concat would also be needed. Another
issue which is brought up by the idea I have jotted down is how to handle
conversions like '%&a' and '%&t'.
What do you guys think about all this?
Alex
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [Caml-list] Request for comments: Printf list conversion
2006-01-12 9:19 Request for comments: Printf list conversion Alessandro Baretta
@ 2006-01-12 9:31 ` Jean-Marc EBER
2006-01-12 9:55 ` Jean-Christophe Filliatre
2006-01-12 15:50 ` Bill Wood
2 siblings, 0 replies; 11+ messages in thread
From: Jean-Marc EBER @ 2006-01-12 9:31 UTC (permalink / raw)
To: Alessandro Baretta; +Cc: Ocaml
Just for info:
Not identical of course, but related is
0003882: Wish: Format.pp_print_list
in the bug tracker (and wish list) system of ocaml.
Jean-Marc
Alessandro Baretta wrote:
> I would like to transform the following rather vague idea into a formal
> feature request. Before doing this, I would like to know what others
> think about it.
>
> I continually come across the need to pretty-print via Printf a list of
> values--for example, let's say an int list. Printf has conversion
> specifiers for int, but int list cannot be handled easily. What I
> usually end up doing is converting values in the list to strings,
> concatenate them, and output the result with the %s conversion specifier.
>
> Printf.bprintf buf "A beatiful list of ints: [ %s ]\n" (String.concat "
> ;" (List.map string_of_int [ 1;2;3;4;5 ] )
>
> Obviously, this is radically ineffiecient. A better solution might be
> the following.
>
> Printf.bprintf buf "A beatiful list of ints: [ %t ]\n" (fun buf ->
> List.iter (Printf.bprintf buf " %d;") [ 1;2;3;4;5 ]
> )
>
> The above is adequately fast, but terribly ugly.
>
> I believe that it should be possible to add a conversion-flag--let me
> use '&' as an example--telling the type-system and the Printf module
> that the corresponding parameter is a list of values whose type is
> determined by the conversion-specifier proper. Let me show what I would
> like to be able to write:
>
> Printf.bprintf buf "A beatiful list of ints: [ %&d ]\n" [ 1;2;3;4;5 ]
>
> Here, the &-flag would tell the type-system that the first parameter is
> an 'a list. The fact that 'a = int is to be inferred from the actual
> conversion specifier ('d'). Upon recognizing a %& conversion, Printf
> would have to iterate over the corresponding parameter with the
> appropriate conversion (format_int_with_conv, in this case).
>
> Now, of course, the '&' conversion-flag does not settle the issue, as a
> parameter like the first one of String.concat would also be needed.
> Another issue which is brought up by the idea I have jotted down is how
> to handle conversions like '%&a' and '%&t'.
>
> What do you guys think about all this?
>
> Alex
>
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>
>
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [Caml-list] Request for comments: Printf list conversion
2006-01-12 9:19 Request for comments: Printf list conversion Alessandro Baretta
2006-01-12 9:31 ` [Caml-list] " Jean-Marc EBER
@ 2006-01-12 9:55 ` Jean-Christophe Filliatre
2006-01-12 12:55 ` Damien Doligez
2006-01-12 15:50 ` Bill Wood
2 siblings, 1 reply; 11+ messages in thread
From: Jean-Christophe Filliatre @ 2006-01-12 9:55 UTC (permalink / raw)
To: Alessandro Baretta; +Cc: Ocaml
Hello,
Alessandro Baretta writes:
> I would like to transform the following rather vague idea into a
> formal feature
> request. Before doing this, I would like to know what others think about it.
>
> I continually come across the need to pretty-print via Printf a list of
> values--for example, let's say an int list.
I recently submitted a similar wish on the bug tracking system, which
is slightly more general than your proposal:
http://caml.inria.fr/mantis/view.php?id=3882
Meanwhile, I'm using a rather different solution than yours, that is a
custom function
val print_list : (formatter -> 'a -> unit) -> formatter -> 'a list -> unit
that I use with %a, e.g.
fprintf fmt "this is the list %a" (print_list pp_print_int) [1;2;3]
I actually use a more general version of print_list that allows a
separator to be specified (to be printed in between the list
elements):
val print_list :
?sep:(formatter -> unit -> unit) ->
(formatter -> 'a -> unit) -> formatter -> 'a list -> unit
which comes with a handy list of predefined separators:
val space : formatter -> unit -> unit
val newline : formatter -> unit -> unit
val comma : formatter -> unit -> unit
val semi : formatter -> unit -> unit
val nothing : formatter -> unit -> unit
etc.
Hope this helps,
--
Jean-Christophe Filliâtre (http://www.lri.fr/~filliatr)
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [Caml-list] Request for comments: Printf list conversion
2006-01-12 9:55 ` Jean-Christophe Filliatre
@ 2006-01-12 12:55 ` Damien Doligez
2006-01-12 13:27 ` Virgile Prevosto
0 siblings, 1 reply; 11+ messages in thread
From: Damien Doligez @ 2006-01-12 12:55 UTC (permalink / raw)
To: Ocaml
On Jan 12, 2006, at 10:55, Jean-Christophe Filliatre wrote:
> val print_list :
> ?sep:(formatter -> unit -> unit) ->
> (formatter -> 'a -> unit) -> formatter -> 'a list -> unit
>
> which comes with a handy list of predefined separators:
>
> val space : formatter -> unit -> unit
> val newline : formatter -> unit -> unit
> val comma : formatter -> unit -> unit
> val semi : formatter -> unit -> unit
> val nothing : formatter -> unit -> unit
Yuck. Isn't this type (formatter -> unit -> unit) isomorphic to string,
in the absence of side-effects? Or do you really need separators that
behave differently depending on the state of some reference?
-- Damien
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [Caml-list] Request for comments: Printf list conversion
2006-01-12 12:55 ` Damien Doligez
@ 2006-01-12 13:27 ` Virgile Prevosto
2006-01-13 8:47 ` Jean-Christophe Filliatre
0 siblings, 1 reply; 11+ messages in thread
From: Virgile Prevosto @ 2006-01-12 13:27 UTC (permalink / raw)
To: caml-list
Le jeu 12 jan 2006 13:55:56 CET, Damien Doligez a écrit:
> On Jan 12, 2006, at 10:55, Jean-Christophe Filliatre wrote:
>
> > val print_list :
> > ?sep:(formatter -> unit -> unit) ->
> > (formatter -> 'a -> unit) -> formatter -> 'a list -> unit
> >
> Yuck. Isn't this type (formatter -> unit -> unit) isomorphic to string,
> in the absence of side-effects? Or do you really need separators that
> behave differently depending on the state of some reference?
>
With Format, yes: you may want to use the pretty-printing features of the
module, and for instance add a break hint after each element of the list.
For Printf, on the other hand, I guess that a string would be good enough
--
E tutto per oggi, a la prossima volta
Virgile
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [Caml-list] Request for comments: Printf list conversion
2006-01-12 13:27 ` Virgile Prevosto
@ 2006-01-13 8:47 ` Jean-Christophe Filliatre
0 siblings, 0 replies; 11+ messages in thread
From: Jean-Christophe Filliatre @ 2006-01-13 8:47 UTC (permalink / raw)
To: Virgile Prevosto; +Cc: caml-list
> > > val print_list :
> > > ?sep:(formatter -> unit -> unit) ->
> > > (formatter -> 'a -> unit) -> formatter -> 'a list -> unit
> > >
> > Yuck. Isn't this type (formatter -> unit -> unit) isomorphic to string,
> > in the absence of side-effects? Or do you really need separators that
> > behave differently depending on the state of some reference?
>
> With Format, yes: you may want to use the pretty-printing features of the
> module, and for instance add a break hint after each element of the list.
> For Printf, on the other hand, I guess that a string would be good enough
Yes, that's precisely what I'm doing:
let comma fmt () = fprintf fmt ",@ "
...
--
Jean-Christophe
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [Caml-list] Request for comments: Printf list conversion
2006-01-12 9:19 Request for comments: Printf list conversion Alessandro Baretta
2006-01-12 9:31 ` [Caml-list] " Jean-Marc EBER
2006-01-12 9:55 ` Jean-Christophe Filliatre
@ 2006-01-12 15:50 ` Bill Wood
2006-01-12 17:39 ` Richard Jones
2006-01-13 9:31 ` Hendrik Tews
2 siblings, 2 replies; 11+ messages in thread
From: Bill Wood @ 2006-01-12 15:50 UTC (permalink / raw)
To: Alessandro Baretta; +Cc: Ocaml
On Thu, 2006-01-12 at 10:19 +0100, Alessandro Baretta wrote:
. . .
> What do you guys think about all this?
Just a comment. I've been anticipating getting into something like this
since I started considering OCaml as a work-horse language. I've gone
through this exercise several times -- When I worked in Prolog, I hacked
together a list formatter; after using Scheme a while I hacked out a
list formatter package (and I'm dreading doing it yet again :-). The
thing is, it's so terribly useful.
I'm always inspired in my hacks by Common Lisp's (format...) form, which
includes a ~{/~} pair containing conversion directives and that consumes
a list argument. I don't want to appall you with complexity too soon,
but when you think about this, consider the possibility that the list is
a list of lists, each of which contains several items that are to be
formatted using a a recursively specified (sub) format. This does
enhance usability a great deal. I'm sure that there are people who will
yelp in protest at this, but I do recommend looking at CL's format as at
least one point in this design space.
Good luck; I eagerly await your results :-)
-- Bill Wood
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [Caml-list] Request for comments: Printf list conversion
2006-01-12 15:50 ` Bill Wood
@ 2006-01-12 17:39 ` Richard Jones
2006-01-12 19:29 ` Bill Wood
2006-01-13 9:31 ` Hendrik Tews
1 sibling, 1 reply; 11+ messages in thread
From: Richard Jones @ 2006-01-12 17:39 UTC (permalink / raw)
To: Bill Wood; +Cc: Ocaml
On Thu, Jan 12, 2006 at 09:50:54AM -0600, Bill Wood wrote:
> Just a comment. I've been anticipating getting into something like this
> since I started considering OCaml as a work-horse language. I've gone
> through this exercise several times -- When I worked in Prolog, I hacked
> together a list formatter; after using Scheme a while I hacked out a
> list formatter package (and I'm dreading doing it yet again :-). The
> thing is, it's so terribly useful.
>
> I'm always inspired in my hacks by Common Lisp's (format...) form, which
> includes a ~{/~} pair containing conversion directives and that consumes
> a list argument. I don't want to appall you with complexity too soon,
> but when you think about this, consider the possibility that the list is
> a list of lists, each of which contains several items that are to be
> formatted using a a recursively specified (sub) format. This does
> enhance usability a great deal. I'm sure that there are people who will
> yelp in protest at this, but I do recommend looking at CL's format as at
> least one point in this design space.
If you just want to dump out data structures (for debugging, for
example) then you might want to take a look at the Std.dump function
in Extlib:
http://cvs.sourceforge.net/viewcvs.py/ocaml-lib/extlib-dev/std.mli?rev=1.15&view=markup
Or: http://merjis.com/developers/dumper
Of course this isn't suitable for end user reports.
Rich.
--
Richard Jones, CTO Merjis Ltd.
Merjis - web marketing and technology - http://merjis.com
Team Notepad - intranets and extranets for business - http://team-notepad.com
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [Caml-list] Request for comments: Printf list conversion
2006-01-12 17:39 ` Richard Jones
@ 2006-01-12 19:29 ` Bill Wood
0 siblings, 0 replies; 11+ messages in thread
From: Bill Wood @ 2006-01-12 19:29 UTC (permalink / raw)
To: Richard Jones; +Cc: Ocaml
On Thu, 2006-01-12 at 17:39 +0000, Richard Jones wrote:
. . .
> If you just want to dump out data structures (for debugging, for
> example) then you might want to take a look at the Std.dump function
> in Extlib:
>
> http://cvs.sourceforge.net/viewcvs.py/ocaml-lib/extlib-dev/std.mli?rev=1.15&view=markup
>
> Or: http://merjis.com/developers/dumper
>
> Of course this isn't suitable for end user reports.
This is good to know, but I'm talking about situations where I need more
structured output. For example, I wrote a scheduling program for an
organization I belonged to, and found it convenient to output the
results as a LaTeX table. So there was pre-matter boiler plate, then
the table, and finally post-matter boiler plate. The results of the
scheduler were produced as a list of lists, where each member list had
an item for each column of the table. A single (format...) form
consumed the results and wrote out the table. All I had to do was run
the program, then run latex, and finally pipe the output of dvips to my
printer. Of course, a major part of the convenience was that *I didn't
have to write the formatter*.
-- Bill Wood
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [Caml-list] Request for comments: Printf list conversion
2006-01-12 15:50 ` Bill Wood
2006-01-12 17:39 ` Richard Jones
@ 2006-01-13 9:31 ` Hendrik Tews
2006-01-13 16:28 ` Bill Wood
1 sibling, 1 reply; 11+ messages in thread
From: Hendrik Tews @ 2006-01-13 9:31 UTC (permalink / raw)
To: Ocaml
Bill Wood <william.wood3@comcast.net> writes:
I'm always inspired in my hacks by Common Lisp's (format...) form, which
includes a ~{/~} pair containing conversion directives and that consumes
a list argument. I don't want to appall you with complexity too soon,
but when you think about this, consider the possibility that the list is
a list of lists, each of which contains several items that are to be
formatted using a a recursively specified (sub) format. This does
enhance usability a great deal. I'm sure that there are people who will
yelp in protest at this, but I do recommend looking at CL's format as at
least one point in this design space.
I doubt anybody will yelp. Having a printf format for 'a list it
is just natural to permit arbitrary formats for the 'a elements
of the list. Therefore I would suggest something like
Printf.bprintf buf "int pair list: %[(%d, %d) %]" [ 1;2;3;4;5 ]
Here %[ is the list format and everything inbetween %[ and %] is
the format for the list elements (like the %{ .. %} format).
It remains to deal with separators that you don't want to print
after the last element. Maybe this could be solved by permitting
flags for %], like
- flag space "% ]": use space separator, omitted after the last
element
- flag comma "%,]": similar for comma
- flag s "%s]": use next argument as format and interpred
this format as separator
A flag that permits a format would also Virgile Prevosto problem:
Virgile Prevosto <virgile.prevosto@m4x.org> writes:
Le jeu 12 jan 2006 13:55:56 CET, Damien Doligez a écrit:
>
> Yuck. Isn't this type (formatter -> unit -> unit) isomorphic to string,
> in the absence of side-effects? Or do you really need separators that
> behave differently depending on the state of some reference?
>
With Format, yes: you may want to use the pretty-printing features of the
module, and for instance add a break hint after each element of the list.
Just use
Format.printf "int pair list: %[(%d, %d)%s]" [ 1;2;3;4;5 ] "@;<2 2>"
BTW, what is the common list solution for printing separators
just between the list elements?
Bye,
Hendrik
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [Caml-list] Request for comments: Printf list conversion
2006-01-13 9:31 ` Hendrik Tews
@ 2006-01-13 16:28 ` Bill Wood
0 siblings, 0 replies; 11+ messages in thread
From: Bill Wood @ 2006-01-13 16:28 UTC (permalink / raw)
To: Hendrik Tews; +Cc: Ocaml
On Fri, 2006-01-13 at 10:31 +0100, Hendrik Tews wrote:
. . .
> I doubt anybody will yelp. Having a printf format for 'a list it
That would be nice :-). However, even some lispers have complained
(with some justification) that writing format forms was like writing
TECO macros, or minimalistic Perl.
> BTW, what is the common list solution for printing separators
> just between the list elements?
There is no special provision for separators that I know of. I always
just took the cheap way out, first formatting the head of the list and
then formatting the tail with a list format that prints the separator
and then the next item. A sample transcript in CLISP is*
(let ((xs '(0 1 2 3)))
(format t "~%>~s~{, ~s~}<~%" (first xs) (rest xs)))
>0, 1, 2, 3<
This is facilitated by the conditional format directive, allowing
different formats depending on whether the list argument is empty or
non-empty.
-- Bill Wood
* The syntax of the CL format form is
(format <target> <format string> <arg> ...)
where <target> specifies returning a string, printing to stdout, or
printing to a stream, <format string> is a string containing format
directives together with ordinary characters, and the remaining args
are the objects being formatted out.
Format directives are prefixed with "~"; the directive "~%" produces a
newline and the directive "~s" formats any lisp s-expression, like a
Haskell "show x".
-- wtw
^ permalink raw reply [flat|nested] 11+ messages in thread
end of thread, other threads:[~2006-01-13 16:28 UTC | newest]
Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-01-12 9:19 Request for comments: Printf list conversion Alessandro Baretta
2006-01-12 9:31 ` [Caml-list] " Jean-Marc EBER
2006-01-12 9:55 ` Jean-Christophe Filliatre
2006-01-12 12:55 ` Damien Doligez
2006-01-12 13:27 ` Virgile Prevosto
2006-01-13 8:47 ` Jean-Christophe Filliatre
2006-01-12 15:50 ` Bill Wood
2006-01-12 17:39 ` Richard Jones
2006-01-12 19:29 ` Bill Wood
2006-01-13 9:31 ` Hendrik Tews
2006-01-13 16:28 ` Bill Wood
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox