Version française
Home     About     Download     Resources     Contact us    
Browse thread
Newbie question: OCaml equivalent of Haskell's show?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Richard Jones <rich@a...>
Subject: Re: [Caml-list] Newbie question: OCaml equivalent of Haskell's show?
On Sun, Jul 06, 2008 at 11:33:35AM -0400, 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.

Note that OCaml doesn't carry very much information at runtime about
what is represented in a value.  However there are various generic
printers around.  Probably your best bet for a quick and dirty hack is
to use the 'Std.dump' function in extlib
(http://code.google.com/p/ocaml-extlib/).  This can turn anything into
a string, and tries to produce something which looks similar to an
OCaml toplevel value.

Documentation for Std.dump:
  http://ocaml-extlib.googlecode.com/svn/doc/apiref/Std.html

If you want to go further than this and have OCaml write a pretty-
printer for your types, then you'll want to look at one of the
following projects (and probably others ...)

  http://www.ocaml.info/home/ocaml_sources.html
  http://code.google.com/p/deriving/
  http://tools.assembla.com/tywith/wiki

Another alternative is to run your code in the OCaml toplevel.

Rich.

-- 
Richard Jones
Red Hat