English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
[Caml-list] Does this function exist?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2002-09-09 (21:40)
From: Yutaka OIWA <oiwa@y...>
Subject: Re: [Caml-list] Does this function exist?
>> On Mon, 9 Sep 2002 22:18:08 +0200 (CEST), Lukasz Lew <ll189417@zodiac.mimuw.edu.pl> said:

> I can use printf, but it isn't very easy to write when you have i.e. 
> list of pairs of lists. And i believe that there is unversal ``print''.
> Can you help?

No. Why can you believe it?  Or is it just a wish? :-)

In OCaml runtime, several different data share the same representation.
For example, all (0 : int), ('\000' : char), (None: 'a option), 
((): unit), ([]: 'a list), (FP_normal : fpclass), (Open_rdonly : open_flag), 
and etc...... are represented by the same 32/64bit word "0x00000001".
Also, (0, 0), [0], and [| 0; 0 |] share the same representation.
See Section 18.3 of the OCaml manual, and the header file
"mlvalues.h" in the Ocaml's lib directory for further detail.
One can not distinguish those on-memory data in any way.

Therefore, the type information is strictly required for
pretty-printing values correctly. Even bytecode does
not have an enough information to recover the type information *1 .

If your requirement is only to acquire some programmer-readable
output for debugging purpose, you can try 
[ http://www.yl.is.s.u-tokyo.ac.jp/~oiwa/pub/misc/datadumper.ml ] *2 .
This module walks on-memory data structures
and prints the values in some "simplest" form like integers and pairs.
For example, the values above are printed just as 0 and (0, 0).

-- Footnote --
*1: The two functions

let f1 (x : int) = f x
let f2 (x : int) = f (Char.unsafe_chr x)

emits exactly the same bytecode. Even inspecting all instructions,
one can not determine whether the argument passed to a polymorphic
function f is an integer or a character.

*2: datadumper.ml is written for OCaml 3.04.
I did not tried it with the new representation of the lazy values
in OCaml 3.05/3.06.

Yutaka Oiwa              Yonezawa Lab., Dept. of Computer Science,
      Graduate School of Information Sci. & Tech., Univ. of Tokyo.
      <oiwa@yl.is.s.u-tokyo.ac.jp>, <yutaka@oiwa.shibuya.tokyo.jp>
PGP fingerprint = C9 8D 5C B8 86 ED D8 07  EA 59 34 D8 F4 65 53 61
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