caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Polymorphic pretty printing
@ 2004-10-18 15:42 Andrej Bauer
  2004-10-22 22:07 ` Christophe TROESTLER
  0 siblings, 1 reply; 8+ messages in thread
From: Andrej Bauer @ 2004-10-18 15:42 UTC (permalink / raw)
  To: caml-list

This is a question for gurus.

I am contemplating writing an enhanced toplevel that could display
graphics as we well as text (the evil plan is to replace Mathematica).
The first step seems to be: how to install a pretty printer for a
_polymorphic_ type.

As an example, consider this:

  type 'a set = { elements : 'a list }

I want a value of this type to print out as

  {a, b, c, ... d}

instead of

  {elements = [a; b; c; ...; d]}

The trouble is, how to print out the elements a, b, c, ..., d
since they are of a polymorphic type.

According to the somewhat old message at

http://caml.inria.fr/archives/200201/msg00234.html

I should use Toplevel.print_out_value to do this. But, print_out_value
expects an arguments of type Outcometree.out_value, and it is not
clear to me where I will get it. Is there another function that converts
an arbitrary value (of an arbitrary type!) to an Outcometree.out_value?

Am I supposed to rewrite half of toplevel.ml to get this working?

It would be helpful if there were, somewhere in the world, a minimal and
_complete_ example of how one can actually write a polymorphic pretty
printer. This _must_ be an FAQ.

Best regards,

Andrej

-------------------
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


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Caml-list] Polymorphic pretty printing
  2004-10-18 15:42 [Caml-list] Polymorphic pretty printing Andrej Bauer
@ 2004-10-22 22:07 ` Christophe TROESTLER
  2004-10-24 20:54   ` Andrej Bauer
  2004-11-26 22:54   ` Frédéric Gava
  0 siblings, 2 replies; 8+ messages in thread
From: Christophe TROESTLER @ 2004-10-22 22:07 UTC (permalink / raw)
  To: Andrej.Bauer; +Cc: O'Caml Mailing List

On Mon, 18 Oct 2004, Andrej Bauer <Andrej.Bauer@andrej.com> wrote:
> 
> how to install a pretty printer for a _polymorphic_ type.
> http://caml.inria.fr/archives/200201/msg00234.html
> 
> I should use Toplevel.print_out_value to do this. But,
> print_out_value expects an arguments of type Outcometree.out_value,
> and it is not clear to me where I will get it.

Since you will register a new printing function, you will not have to
get it, Outcometree.out_value will be provided to your function!  To
get you a flavor of how (I understand) it works, here is some code:

let oldpp = !Toploop.print_out_value
let pp f o =
  (match o with
   | Outcometree.Oval_list _ ->
       Format.fprintf f "list:"
   | _ -> ());
    oldpp f o
Toploop.print_out_value := pp

Now, a list is written as

# [1;2];;
- : int list = list:[1; 2]

> Am I supposed to rewrite half of toplevel.ml to get this working?

I am afraid that the awser is yes :(.  Let's see why:

# ([1; 2], 2);;
- : int list * int = ([1; 2], 2)

Now the above [pp] pretty printer is registered but it does not to
work.  This is because the original function registered in
[Toploop.print_out_value] is recursive instead of calling
[!Toploop.print_out_value] (there is certainly a reason but I do not
know it) -- thus since the "top" structure is a couple, [oldpp] is
called and never calls back [pp].

Therefore, not only we have to duplicate all the code of the original
[!Toploop.print_out_value] (and check conformance at every upgrade)
but that also means it is difficult for several polymorphic pretty
printers to cooperate.

This is really unfortunate as this prevents libraries to use
[Sys.interactive] in order to install pretty printers for their
polymorphic types.

Hope a nice solution can be designed for this last usage.

Cheers,
ChriS

-------------------
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


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Caml-list] Polymorphic pretty printing
  2004-10-22 22:07 ` Christophe TROESTLER
@ 2004-10-24 20:54   ` Andrej Bauer
  2004-10-25  1:04     ` skaller
  2004-10-25 14:45     ` Jon Harrop
  2004-11-26 22:54   ` Frédéric Gava
  1 sibling, 2 replies; 8+ messages in thread
From: Andrej Bauer @ 2004-10-24 20:54 UTC (permalink / raw)
  To: Christophe TROESTLER; +Cc: O'Caml Mailing List

Christophe TROESTLER wrote:
> 
>> Am I supposed to rewrite half of toplevel.ml to get this working?
> 
> I am afraid that the awser is yes :(.  Let's see why:

Thanks for the hints.

I'd be willing to take a shot at writing a more flexible toplevel, one
that allows to install polymorphic pretty-printers in a sane way.

I am imagining something like this. A pretty printer may be registered
with #install_printer, as before. But we need to fiddle with the types
of printers to get things working. A printer pp for type t (where t may
be polymorphic) would have type

pp : Format.formatter -> t -> (Format.formatter -> 'a -> unit) -> unit

If you compare this with the current type, you'll notice the extra
argument of type (Format.formatter -> 'a -> unit). This is a helper
function which would be passed to pp by the toplevel. Then pp can use it
to print out any polymorphic value.

Does this sound like a sound plan? Or am I missing something? Perhaps
the ocaml developers secretly posses such a toplevel already.

Andrej

-------------------
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


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Caml-list] Polymorphic pretty printing
  2004-10-24 20:54   ` Andrej Bauer
@ 2004-10-25  1:04     ` skaller
  2004-10-25  3:02       ` David Brown
  2004-10-25 14:45     ` Jon Harrop
  1 sibling, 1 reply; 8+ messages in thread
From: skaller @ 2004-10-25  1:04 UTC (permalink / raw)
  To: Andrej Bauer; +Cc: Christophe TROESTLER, O'Caml Mailing List

On Mon, 2004-10-25 at 06:54, Andrej Bauer wrote:
> Christophe TROESTLER wrote:
> > 
> >> Am I supposed to rewrite half of toplevel.ml to get this working?
> > 
> > I am afraid that the awser is yes :(.  Let's see why:
> 
> Thanks for the hints.
> 
> I'd be willing to take a shot at writing a more flexible toplevel, one
> that allows to install polymorphic pretty-printers in a sane way.

I'm curious why people want to use these kinds of routines.
What does printf style mini-language printing have to offer compared to
just using plain old Ocaml functions?

I almost never use this kind of printer, and even systematically
removed it from a program I was working on once. I would have guessed
printf style printing is nice for debugging or perhaps logs,
but not much more. Ocaml itself seems a stronger and better
language to me.

Similarly I never use iostreams in C++ .. and there we have
overloading, which is even more convenient for remembering
the names of the formatting routines.

Still, I use printf() itself in C.

What makes such printers better than just using ordinary routines?

> Does this sound like a sound plan? Or am I missing something? 

Well, how would you port code using such a printer to
an ocamlopt program?

-- 
John Skaller, mailto:skaller@users.sf.net
voice: 061-2-9660-0850, 
snail: PO BOX 401 Glebe NSW 2037 Australia
Checkout the Felix programming language http://felix.sf.net



-------------------
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


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Caml-list] Polymorphic pretty printing
  2004-10-25  1:04     ` skaller
@ 2004-10-25  3:02       ` David Brown
  2004-10-25  7:26         ` Andrej Bauer
  0 siblings, 1 reply; 8+ messages in thread
From: David Brown @ 2004-10-25  3:02 UTC (permalink / raw)
  To: skaller; +Cc: Andrej Bauer, Christophe TROESTLER, O'Caml Mailing List

On Mon, Oct 25, 2004 at 11:04:36AM +1000, skaller wrote:

> I'm curious why people want to use these kinds of routines.
> What does printf style mini-language printing have to offer compared to
> just using plain old Ocaml functions?

  sprintf "%04d/%02d/%02d %02d:%02d:%02d" y m d h m s

becomes rather verbose and clumsy when written out as functions.

Dave

-------------------
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


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Caml-list] Polymorphic pretty printing
  2004-10-25  3:02       ` David Brown
@ 2004-10-25  7:26         ` Andrej Bauer
  0 siblings, 0 replies; 8+ messages in thread
From: Andrej Bauer @ 2004-10-25  7:26 UTC (permalink / raw)
  To: O'Caml Mailing List

David Brown wrote:
> On Mon, Oct 25, 2004 at 11:04:36AM +1000, skaller wrote:
> 
>> I'm curious why people want to use these kinds of routines.
>> What does printf style mini-language printing have to offer compared to
>> just using plain old Ocaml functions?
> 
>   sprintf "%04d/%02d/%02d %02d:%02d:%02d" y m d h m s
> 
> becomes rather verbose and clumsy when written out as functions.

This discussion has nothing to do with printf and similar function.
Please do not derail the thread and stay on topic, which is
pretty-printing in ocaml toplevel.

Assuming the original question was "why pretty-printing" (as opposed to
"why printf", which is irrelevant to this discussion), the answer I hope
is clear: because a good interactive toplevel is useful for many
purposes. I have two kinds of motivation: teaching ocaml and enriching
ocaml toplevel with graphics and the like, so that it looks and feels
more like Mathematica. (Looks are important!)

Best regards,

Andrej

-------------------
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


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Caml-list] Polymorphic pretty printing
  2004-10-24 20:54   ` Andrej Bauer
  2004-10-25  1:04     ` skaller
@ 2004-10-25 14:45     ` Jon Harrop
  1 sibling, 0 replies; 8+ messages in thread
From: Jon Harrop @ 2004-10-25 14:45 UTC (permalink / raw)
  To: O'Caml Mailing List

On Sunday 24 October 2004 21:54, Andrej Bauer wrote:
> I'd be willing to take a shot at writing a more flexible toplevel, one
> that allows to install polymorphic pretty-printers in a sane way.
> ...

I'd be very interested to hear the design decisions involved in such a 
project, and about the design of the current approach.

Also, I think tutorial pretty printers could be very helpful for people (like 
me!) learning how to do this.

Polymorphic pretty printers for types in the core library (Set, Map, Hashtbl) 
could be a handy add-on too. As a computer science teaching tool, perhaps 
these could be productively supplemented with graphical printers to print the 
internal representations as they evolve?

Cheers,
Jon.


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Caml-list] Polymorphic pretty printing
  2004-10-22 22:07 ` Christophe TROESTLER
  2004-10-24 20:54   ` Andrej Bauer
@ 2004-11-26 22:54   ` Frédéric Gava
  1 sibling, 0 replies; 8+ messages in thread
From: Frédéric Gava @ 2004-11-26 22:54 UTC (permalink / raw)
  To: Andrej.Bauer, Christophe TROESTLER; +Cc: O'Caml Mailing List

Hi, I have test and it is possible.

Daniel de Rauglaudre wrote in
>From: Daniel de Rauglaudre <daniel.de_rauglaudre@inria.fr>
>Date: Fri, 25 Jan 2002 06:58:14 +0100

"Another example is the "revised syntax" of Camlp4 in the OCaml
sources, file "camlp4/top/rprint.ml". (use "camlp4r pr_o.cmo
camlp4/top/rprint.ml" to see this file in normal syntax, if you
prefer). "

Take the function "print_out_value" of camlp4r pr_o.cmo
camlp4/top/rprint.ml" , modify it (it is easy, the code is very clear) for
your application and
run : Toploop.print_out_value := print_out_value.
Do not forget do modify "True" to "true" (idem for "False") to have a
correct printing of Ocaml values.

I have
type 'a vec = Vector of 'a array
and I modify 'print_out_value" to print vector as <...,...,...> and
(Vector (Array.init 3 (fun i->i)), Vector (Array.init 3 (fun i->i))) prints
(<0,1,2>, <0,1,2>) without problem and I do not have modify at all the
toplevel.

Cheers,
Frédéric Gava
>----- Original Message -----


>From: "Christophe TROESTLER" <debian00@tiscali.be>
>To: <Andrej.Bauer@andrej.com>
>Cc: "O'Caml Mailing List" <caml-list@inria.fr>
>Sent: Friday, October 22, 2004 11:07 PM
>Subject: Re: [Caml-list] Polymorphic pretty printing


> On Mon, 18 Oct 2004, Andrej Bauer <Andrej.Bauer@andrej.com> wrote:
> >
> > how to install a pretty printer for a _polymorphic_ type.
> > http://caml.inria.fr/archives/200201/msg00234.html
> >
> > I should use Toplevel.print_out_value to do this. But,
> > print_out_value expects an arguments of type Outcometree.out_value,
> > and it is not clear to me where I will get it.
>
> Since you will register a new printing function, you will not have to
> get it, Outcometree.out_value will be provided to your function!  To
> get you a flavor of how (I understand) it works, here is some code:
>
> let oldpp = !Toploop.print_out_value
> let pp f o =
>   (match o with
>    | Outcometree.Oval_list _ ->
>        Format.fprintf f "list:"
>    | _ -> ());
>     oldpp f o
> Toploop.print_out_value := pp
>
> Now, a list is written as
>
> # [1;2];;
> - : int list = list:[1; 2]
>
> > Am I supposed to rewrite half of toplevel.ml to get this working?
>
> I am afraid that the awser is yes :(.  Let's see why:
>
> # ([1; 2], 2);;
> - : int list * int = ([1; 2], 2)
>
> Now the above [pp] pretty printer is registered but it does not to
> work.  This is because the original function registered in
> [Toploop.print_out_value] is recursive instead of calling
> [!Toploop.print_out_value] (there is certainly a reason but I do not
> know it) -- thus since the "top" structure is a couple, [oldpp] is
> called and never calls back [pp].
>
> Therefore, not only we have to duplicate all the code of the original
> [!Toploop.print_out_value] (and check conformance at every upgrade)
> but that also means it is difficult for several polymorphic pretty
> printers to cooperate.
>
> This is really unfortunate as this prevents libraries to use
> [Sys.interactive] in order to install pretty printers for their
> polymorphic types.
>
> Hope a nice solution can be designed for this last usage.
>
> Cheers,
> ChriS
>
> -------------------
> 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



^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2004-11-26 22:53 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-10-18 15:42 [Caml-list] Polymorphic pretty printing Andrej Bauer
2004-10-22 22:07 ` Christophe TROESTLER
2004-10-24 20:54   ` Andrej Bauer
2004-10-25  1:04     ` skaller
2004-10-25  3:02       ` David Brown
2004-10-25  7:26         ` Andrej Bauer
2004-10-25 14:45     ` Jon Harrop
2004-11-26 22:54   ` Frédéric Gava

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).