From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by walapai.inria.fr (8.13.6/8.13.6) with ESMTP id pB7GkFlc005918 for ; Wed, 7 Dec 2011 17:46:15 +0100 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AvcAAEWX307RVdY2kGdsb2JhbABDmjKIGAGIAQgiAQEBAQkJDQcUBCGBcgEBAQMBDAYCExkBGxIMAwELBgUEBxohIgERAQUBChIGEwkJEIdlCJoNCotkgmuEcT2IcQIFDINoh0AEgluFU4w9jW89hBU X-IronPort-AV: E=Sophos;i="4.71,314,1320620400"; d="scan'208";a="134392964" Received: from mail-bw0-f54.google.com ([209.85.214.54]) by mail1-smtp-roc.national.inria.fr with ESMTP/TLS/RC4-SHA; 07 Dec 2011 17:46:09 +0100 Received: by mail-bw0-f54.google.com with SMTP id zv15so788594bkb.27 for ; Wed, 07 Dec 2011 08:46:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=5rhvT7dH6opQ7Sq9Y3t9FhUOHyIiXo2K7XyR7lnwkgw=; b=Q9oUzMbrwqInh2EA22+TXrsKnvsBuMnCOw2Psz6cFf3QnDHDfI9W+DzI76e9754ZCs o4VtzZZO6TD30NzqM2ACWtuGZyGS5vfNtZIlfJNZNgtrfiMQxvjDXXPDoU1IAGgaVTdS U85D0Y+zBPVDkdDuhGOfvJpBtjmUQwXfx0LsU= MIME-Version: 1.0 Received: by 10.180.7.161 with SMTP id k1mr25098688wia.23.1323276369721; Wed, 07 Dec 2011 08:46:09 -0800 (PST) Received: by 10.180.126.105 with HTTP; Wed, 7 Dec 2011 08:46:09 -0800 (PST) Received: by 10.180.126.105 with HTTP; Wed, 7 Dec 2011 08:46:09 -0800 (PST) In-Reply-To: <1323271707.32238.17.camel@arrakis> References: <1323271707.32238.17.camel@arrakis> Date: Wed, 7 Dec 2011 11:46:09 -0500 Message-ID: From: Alex Rubinsteyn To: Caml List Content-Type: multipart/alternative; boundary=f46d044287c4f4f87104b3834922 Subject: Re: [Caml-list] Generic printer patch --f46d044287c4f4f87104b3834922 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Cool! Thanks for writing this. Can you explain how defining a custom printer works? The new language features still bewilder me. On Dec 7, 2011 10:29 AM, "J=E9r=E9mie Dimino" wrote: > Hi, > > I have made a patch for ocaml 3.12.1 which allow to have a generic > printing function. The patch is available here: > > http://www.dimino.org/ocaml-3.12.1-generic-print.patch > > Here is how to use it: > > external show : 'a -> string =3D "%show" > > let () =3D print_endline (show ([1; 2; 3], Some "foo")) > > this will print: > > ([1; 2; 3], Some "foo") > > It is also possible to define custom printers, for example: > > module StringMap : sig > include Map.S with type key =3D string > val string_of_t : ('a -> string) -> 'a t -> string > end =3D struct > include Map.Make(String) > let string_of_t (type a) string_of_a (m : a t) =3D > show (bindings m) > end > > let () =3D print_endline (show (StringMap.singleton "x" 1)) > > will print: > > [("x", 1)] > > Of course it is limited to what the compiler knows, for example the > following function will always returns "[; ]": > > let f x =3D show [x; x] > > But i think it is already very useful for debugging. > > The git repo is here: > > http://www.dimino.org/gitweb/?p=3Docaml-3.12.1-print.git;a=3Dsummary > > Cheers, > > -- > J=E9r=E9mie > > > > -- > Caml-list mailing list. Subscription management and archives: > https://sympa-roc.inria.fr/wws/info/caml-list > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs > > --f46d044287c4f4f87104b3834922 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable

Cool! Thanks for writing this. Can you explain how defining a custom pri= nter works? The new language features still bewilder me.

On Dec 7, 2011 10:29 AM, "J=E9r=E9mie Dimin= o" <jeremie@dimino.org>= ; wrote:
Hi,

I have made a patch for ocaml 3.12.1 which allow to have a generic
printing function. The patch is available here:

=A0http://www.dimino.org/ocaml-3.12.1-generic-print.patch

Here is how to use it:

=A0external show : 'a -> string =3D "%show"

=A0let () =3D print_endline (show ([1; 2; 3], Some "foo"))

this will print:

=A0([1; 2; 3], Some "foo")

It is also possible to define custom printers, for example:

=A0module StringMap : sig
=A0 =A0include Map.S with type key =3D string
=A0 =A0val string_of_t : ('a -> string) -> 'a t -> string=
=A0end =3D struct
=A0 =A0include Map.Make(String)
=A0 =A0let string_of_t (type a) string_of_a (m : a t) =3D
=A0 =A0 =A0show (bindings m)
=A0end

=A0let () =3D print_endline (show (StringMap.singleton "x" 1))
will print:

=A0[("x", 1)]

Of course it is limited to what the compiler knows, for example the
following function will always returns "[<poly>; <poly>]&q= uot;:

=A0let f x =3D show [x; x]

But i think it is already very useful for debugging.

The git repo is here:

=A0http://www.dimino.org/gitweb/?p=3Docaml-3.12.= 1-print.git;a=3Dsummary

Cheers,

--
J=E9r=E9mie



--
Caml-list mailing list. =A0Subscription management and archives:
https://sympa-roc.inria.fr/wws/info/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs

--f46d044287c4f4f87104b3834922--