Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

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: -- (:)
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 <> 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 
[ ] *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: 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.
      <>, <>
PGP fingerprint = C9 8D 5C B8 86 ED D8 07  EA 59 34 D8 F4 65 53 61
To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: