From: Jon Harrop <jon@ffconsultancy.com>
To: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Newbie question: OCaml equivalent of Haskell's show?
Date: Sun, 6 Jul 2008 22:09:11 +0100 [thread overview]
Message-ID: <200807062209.11682.jon@ffconsultancy.com> (raw)
In-Reply-To: <3be64c030807060833y155230a2gaeaf0e531827ddb3@mail.gmail.com>
On Sunday 06 July 2008 16:33:35 Antony Courtney wrote:
> I'm an experienced Haskell hacker trying OCaml for the first time.
>
> One thing I am desperately searching for but have been unable to find
> is some direct runtime access to the string representation of
> arbitrary OCaml values.
OCaml has no run-time type information, no structural pretty printers in
compiled code and no type classes to help you implement your own in source
code.
> I have written a little option pricer that constructs a
> float option array array
> in a function. I've got a little buglet in my function so I'd like to
> print the intermediate states of this value from inside the function.
> How do I do that, short of writing my own recursive pretty printer /
> formatter?
You basically have two choices:
. Run in the top-level and get the value you require as the result of
evaluating an expression (e.g. by raising an exception, or by setting a
mutable).
. Write your own print function(s) and call them.
The benefit of the latter is, of course, that it works from compiled code.
> An OCaml form of the Haskell Show type class would be great,
IMHO, type classes are great for some things but pretty printing is not one of
them.
Start with a function to print a list with an arbitrary separator:
# open Format;;
# let rec fprintfs sep f ff = function
| [] -> ()
| [h] -> fprintf ff "%a" f h
| h::t -> fprintf ff "%a%a%a" f h sep () (fprintfs sep f) t;;
val fprintfs :
(Format.formatter -> unit -> unit) ->
(Format.formatter -> 'a -> unit) -> Format.formatter -> 'a list -> unit =
<fun>
For example, we can now write a function to pretty print a list in Mathematica
notation:
# let fprintf_mma ff list =
let sep ff () = fprintf ff ",@ " in
let int ff = fprintf ff "%d" in
fprintf ff "{@[%a@]}" (fprintfs sep int) list;;
val fprintf_mma : Format.formatter -> int list -> unit = <fun>
# fprintf std_formatter "%a\n" fprintf_mma (Array.to_list(Array.init 100 (fun
n -> n)));;
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58,
59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77,
78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96,
97, 98, 99}
- : unit = ()
Note the automatically-aligned wrapping thanks to OCaml's pretty
printing "Format" module and the use of "@[", "@ " and "@]" in my code. This
also works from compiled code and is an excellent way to print indented
source code (e.g. when generating other languages).
To pretty print a generic array:
# let fprintf_array f ff array =
let sep ff () = fprintf ff ";@ " in
fprintf ff "[|@[%a@]|]" (fprintfs sep f) (Array.to_list array);;
val fprintf_array :
(Format.formatter -> 'a -> unit) -> Format.formatter -> 'a array -> unit =
<fun>
To print an option:
# let rec fprintf_opt f ff = function
| None -> fprintf ff "None"
| Some x -> fprintf ff "Some %a" f x;;
val fprintf_opt :
(Format.formatter -> 'a -> unit) -> Format.formatter -> 'a option -> unit =
<fun>
Now you can print your array of arrays of float options:
# let print ff xss =
let float ff = fprintf ff "%g" in
fprintf_array (fprintf_array (fprintf_opt float)) ff xss;;
val print : Format.formatter -> float option array array -> unit = <fun>
# fprintf std_formatter "%a\n" print
[|[|Some 3.; None; None|]; [|None; Some 5.; None|]; [|Some 1.|]|];;
[|[|Some 3; None; None|]; [|None; Some 5; None|]; [|Some 1|]|]
- : unit = ()
> but a hack to provide programmatic access to the polymorphic
> pretty printer that obviously already exists in the OCaml toplevel
> would be fine.
In addition to Richard's suggestions, you might look at generating OCaml
expressions using camlp4 and then printing those instead. Just an idea (I've
never tried it).
> I've scoured the standard library docs, manual, tutorials and a few
> books, looked at the FAQ and am amazed that I haven't found the answer
> to this. I am hopefully just missing something obvious; would be
> grateful if someone could point me towards the answer!
Surprisingly, structural printing is something that OCamlers rarely do. As
part of my evolution as a programmer I had migrated everything from C++ and
Mathematica to OCaml a few years ago but I am actually about to move a little
parsing-related project from OCaml to F# simply because its built-in pretty
printers make life so much easier.
Anyway, I shall write an OCaml Journal article about OCaml's
wonderful "Format" module. Incidentally, the OCaml Journal has recovered from
a lull in Q1 and has really started to pick up now. So I still think there is
plenty of opportunity for budding authors to write OCaml books!
--
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
http://www.ffconsultancy.com/products/?e
prev parent reply other threads:[~2008-07-06 21:11 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-07-06 15:33 Antony Courtney
2008-07-06 19:02 ` [Caml-list] " Richard Jones
2008-07-07 19:08 ` Jeremy Yallop
2008-07-06 21:09 ` Jon Harrop [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=200807062209.11682.jon@ffconsultancy.com \
--to=jon@ffconsultancy.com \
--cc=caml-list@yquem.inria.fr \
/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;
as well as URLs for NNTP newsgroup(s).