List Info

Thread: "ocaml_beginners"::[] generic 'to string' function




"ocaml_beginners"::[] generic 'to string' function
country flaguser name
United States
2007-05-24 20:00:17

Hi all,
I amuse this is a has already been discussed somewhere, but i cant
find any satisfactory answers. Since thanks to the its strict static
typing ocaml always knows the type of the objects/variables its
handling, i was curious as to whether there is anything resembling a
generic 'to_string' method in ocaml. or if anyone has pointers on how
to go about writing one.

As always, thanks and all your responses are much appreciated

-
deeq

__._,_.___
.

__,_._,___
Re: "ocaml_beginners"::[] generic 'to string' function
country flaguser name
United States
2007-05-24 20:31:26

mo.deeq wrote:
> Since thanks to the its strict static
> typing ocaml always knows the type of the objects/variables its
> handling, i was curious as to whether there is anything resembling a
> generic 'to_string' method in ocaml. or if anyone has pointers on how
> to go about writing one.

So in ocaml, the compiler may know certain information about the types
of certain variables, but the runtime contains absolutely no information
at all.

This makes writing a generic to_string method difficult. After all,
what would the signature of such a function be? 'a -> string. It could
take any type.. so at this point the compiler doesn't know anything
about the type of variable it's dealing with.

You can look at:

http://merjis.com/developers/dumper
(which is also now contained in extlib)

This function will dump the data, but has to make some assumptions about
the form of the data and doesn't contain any info on variant names or
field names in records.

There is also:
http://pauillac.inria.fr/~ddr/IoXML/index.html

Which is a caml4p syntax extension. It actually creates functions that
parse all the types in your program from and to xml. If they can do it
for xml, I don't see why you couldn't alter it to print out something
more human readable.

Of course, this wouldn't be a generic to_string, but instead it would be
a specific to_string automatically generated for each type.

__._,_.___
.

__,_._,___
Re: "ocaml_beginners"::[] generic 'to string' function
country flaguser name
United States
2007-05-24 21:31:48

On Thu, 24 May 2007, Karl Zilles wrote:

> This makes writing a generic to_string method difficult. After all,
>; what would the signature of such a function be? 'a -> string. It could
> take any type.. so at this point the compiler doesn't know anything
> about the type of variable it's dealing with.

It can, however, be done in MetaOCaml, as demonstrated by Oleg (who can
seemingly work magic in many ways). You can have a look at his work on
this (and other things) at: <http://okmij.org/ftp/ML/>

William D. Neumann

---

"There's just so many extra children, we could just feed the
children to these tigers. We don't need them, we're not doing
anything with them.

Tigers are noble and sleek; children are loud and messy.&quot;

-- Neko Case

Life is unfair. Kill yourself or get over it.
-- Black Box Recorder

__._,_.___
.

__,_._,___
Re: "ocaml_beginners"::[] generic 'to string' function
country flaguser name
United States
2007-05-24 21:47:06

On Fri, May 25, 2007 at 01:00:17AM -0000, mo.deeq wrote:
&gt; Hi all,
>; I amuse this is a has already been discussed somewhere, but i cant
>; find any satisfactory answers. Since thanks to the its strict static
&gt; typing ocaml always knows the type of the objects/variables its
> handling, i was curious as to whether there is anything resembling a
> generic 'to_string' method in ocaml. or if anyone has pointers on how
> to go about writing one.
>
> As always, thanks and all your responses are much appreciated

Others have already pointed this out, but it bears repeating, because
it's the (good) reason why some thing are hard in OCaml.

There is an important difference between the _compiler_ knowing all
the types (which it does; this is called "static" typing) and the
_runtime_ knowing all the types, which it doesn't.

The advantages of doing it the OCaml way are basically twofold:
because the runtime can ignore type information, function calls are
faster: instead of needing to figure out what sort of type something
is (in order to call the correct function on it), the compiler can
just set it up so that the correct function is called, because there's
only one. This means that all the type information can be left out of
the resulting binary; this makes for smaller, tighter code, at the
expense of having no way to do what you're talking about. (Awesome
runtime compilation magic aside)

The other advantage is that by forcing the programmer to get all the
types right at compile time, a lot of the cognitive load on the
programmer is moved to the compiler; this advantage is harder to
quantify, but you'll never hear somebody say "My C code compiles, so
it's probably correct&quot;, but I say that about my OCaml code all the
time.

Constrast this approach to how languages like LISP and Python do it,
where no types (or very nearly none) are determined at compile time;
this means more runtime overhead, but also the ability to have
functions that return different types under different circumstances:

def demo(foo):
if foo == 0.5:
return "Hello!"
elif foo == "goodbye":
return 9

is a Python function where both the input type and the output type
vary; you can't do that directly in ML. (And there are many good
reasons not to do it in Python, either)

If you're a Java programmer, think about why Java is both like ML
(types known at compile time) but also needs to maintain runtime type
information...

Hope that was helpful, and I hope the gurus don't tear me to bits for
some small thinko...

--Mac

--
Julian "Mac&quot; Mason mac%40cs.duke.edu">maccs.duke.edu

[Non-text portions of this message have been removed]

__._,_.___
.

__,_._,___
Re: "ocaml_beginners"::[] generic 'to string' function
country flaguser name
United Kingdom
2007-05-24 21:51:49

On Friday 25 May 2007 02:00:17 mo.deeq wrote:
&gt; I amuse this is a has already been discussed somewhere, but i cant
>; find any satisfactory answers. Since thanks to the its strict static
&gt; typing ocaml always knows the type of the objects/variables its
> handling, i was curious as to whether there is anything resembling a
> generic 'to_string' method in ocaml. or if anyone has pointers on how
> to go about writing one.

This cannot be written in OCaml because the language lacks run-time type
information and whole program optimization.

F# has run-time type information, which burdens the run-time with more work
and slows the language but makes things like a print_any function trivial.
Run-time types (reflection) is used quite a bit in .NET. You can even
autogenerate a GUI editor for the properties of an object using something
called a PropertyGrid.

--
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
The F#.NET Journal
http://www.ffconsultancy.com/products/fsharp_journal/?e

__._,_.___
.

__,_._,___
Re: "ocaml_beginners"::[] generic 'to string' function
country flaguser name
United Kingdom
2007-05-24 22:28:04

On Friday 25 May 2007 03:47:06 Mac Mason wrote:
&gt; Hope that was helpful, and I hope the gurus don't tear me to bits for
> some small thinko...

You are quite right that Lisp and Python do not enforce any type checking at
compile time, leading to many more run-time errors. There are trade-offs here
(e.g. some correct programs do not type check in OCaml's type system) but I
agree that static typing is vastly superior.

However, you are implying that the existence of run-time type information
implies the use of run-time type checking and less type safety, which is very
wrong.

MetaOCaml and F# both provide the same static type checking as OCaml but
supplement the run-time with type information. The extra type information
_improves_ reliability by allowing types to be checked where they cannot be
in OCaml, e.g. marshalling.

--
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
The F#.NET Journal
http://www.ffconsultancy.com/products/fsharp_journal/?e

__._,_.___
.

__,_._,___
Re: "ocaml_beginners"::[] generic 'to string' function
country flaguser name
France
2007-05-25 06:57:42

LE VENDREDI 25 MAI 2007 à 01:00 +0000, MO.DEEQ A éCRIT :
> HANDLING, I WAS CURIOUS AS TO WHETHER THERE IS ANYTHING RESEMBLING A
> GENERIC 'TO_STRING' METHOD IN OCAML. OR IF ANYONE HAS POINTERS ON HOW
> TO GO ABOUT WRITING ONE.

IT'S DEPEND WHAT YOU WANT THIS FUNCTION TO DO. IF YOUR LOOKING FOR SOME
WAY TO PRINT ANY VALUE (LIKE .NET TO_STRING METHOD) THERE IS DEFINITELY
NOTHING IN OCAML, BUT IF YOU JUST NEED A STRING REPRESENTING ANY VALUE
(BUT IN UNREADABLE FORM), YOU CAN USE THE MARSHAL.TO_STRING FUNCTION.
BUT MAYBE YOU KNEW THAT AND WAS LOOKING FOR THE FIRST TYPE OF TO_STRING
METHOD.

MATHIAS

__._,_.___
.

__,_._,___
[1-7]

about | contact  Other archives ( Real Estate discussion Medical topics )