caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Differentiating lists from arrays with Obj
@ 2004-05-07 16:18 John Goerzen
  2004-05-07 16:35 ` Remi Vanicat
  0 siblings, 1 reply; 4+ messages in thread
From: John Goerzen @ 2004-05-07 16:18 UTC (permalink / raw)
  To: caml-list

Hello,

I am looking at using the Obj to access internal representations of some
types.

I have figured out how to access those types for ints, doubles (floats),
strings, and arrays of doubles (why those are special, I have no idea!).

I can also figure out how to access lists and arrays, for which Obj.tag
returns zero.  (Although that value does not occur in obj.ml, it is in
some of the header files.)

Now, here's the question: how can I accurately differentiate lists from
arrays?

Lists appear to be internally represented by structures that look
somewhat like this:

[1; [2; [3; 0]]]

That is, Obj.size will return 2 for the list [1; 2; 3] because it is a
collection of an int and a list.  That's fine, but its internal
representation of the list [1] is:

[1; 0]

Which happens to be identical to the representation of the array
[| 1; 0 |].  Eep.  Now, it's not a big deal to me whether I've got a
list or an array, but I need to be able to know whether that list or
array has an extra 0 at the end.

Worse, the internal representation of [] appears to be identical to the
internal representation of the integer 0.

I am a little puzzled here, and I do not know the type of the data I'm
dealing with in advance.

Thanks for any enlightenment.

-- John

-------------------
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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] Differentiating lists from arrays with Obj
  2004-05-07 16:18 [Caml-list] Differentiating lists from arrays with Obj John Goerzen
@ 2004-05-07 16:35 ` Remi Vanicat
  2004-05-07 19:00   ` John Goerzen
  0 siblings, 1 reply; 4+ messages in thread
From: Remi Vanicat @ 2004-05-07 16:35 UTC (permalink / raw)
  To: caml-list

John Goerzen <jgoerzen@complete.org> writes:

> Hello,
>
> I am looking at using the Obj to access internal representations of some
> types.

It is generally a bad idea. Why do you need this ?

>
> I have figured out how to access those types for ints, doubles (floats),
> strings, and arrays of doubles (why those are special, I have no
> idea!).

for optimization reason, and for interface with C.

> I can also figure out how to access lists and arrays, for which Obj.tag
> returns zero.  (Although that value does not occur in obj.ml, it is in
> some of the header files.)
>
> Now, here's the question: how can I accurately differentiate lists from
> arrays?

You can't. Type information are lost in the midst of the
compilation. You cannot recover them.

>
> Lists appear to be internally represented by structures that look
> somewhat like this:
>
> [1; [2; [3; 0]]]

Yes.

>
> That is, Obj.size will return 2 for the list [1; 2; 3] because it is a
> collection of an int and a list.  That's fine, but its internal
> representation of the list [1] is:
>
> [1; 0]

Exactly

>
> Which happens to be identical to the representation of the array
> [| 1; 0 |].  Eep. 

And of several other thins (like the couple (1,0), or the couple
(true, false), or the couple (1, None), 

>  Now, it's not a big deal to me whether I've got a list or an array,
> but I need to be able to know whether that list or array has an
> extra 0 at the end.

You can't.

>
> Worse, the internal representation of [] appears to be identical to the
> internal representation of the integer 0.

And it the same for false, None, and a lot of others....

> I am a little puzzled here, and I do not know the type of the data I'm
> dealing with in advance.

You should read the "Interfacing ocaml with C" chapter of the manual
language, it explain the internal representation of most ocaml
value. But there is no miracle : what you try to do is not doable.
-- 
Rémi Vanicat

-------------------
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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] Differentiating lists from arrays with Obj
  2004-05-07 16:35 ` Remi Vanicat
@ 2004-05-07 19:00   ` John Goerzen
  2004-05-07 20:01     ` Remi Vanicat
  0 siblings, 1 reply; 4+ messages in thread
From: John Goerzen @ 2004-05-07 19:00 UTC (permalink / raw)
  To: Remi Vanicat; +Cc: caml-list

On Fri, May 07, 2004 at 06:35:15PM +0200, Remi Vanicat wrote:
> John Goerzen <jgoerzen@complete.org> writes:
> 
> > Hello,
> >
> > I am looking at using the Obj to access internal representations of some
> > types.
> 
> It is generally a bad idea. Why do you need this ?

I know :-)

I'm experimenting with some camlp4 stuff.  One thing I'm trying to add
is a "print" keyword that takes a variable number of arguments of any
type and does the right thing.  That's my starting point, at least :-)
Even if it may not prove useful in practice (I think it will), it's a
good way for me to learn more about OCaml.

-- John

-------------------
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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] Differentiating lists from arrays with Obj
  2004-05-07 19:00   ` John Goerzen
@ 2004-05-07 20:01     ` Remi Vanicat
  0 siblings, 0 replies; 4+ messages in thread
From: Remi Vanicat @ 2004-05-07 20:01 UTC (permalink / raw)
  To: caml-list

John Goerzen <jgoerzen@complete.org> writes:

> On Fri, May 07, 2004 at 06:35:15PM +0200, Remi Vanicat wrote:
>> John Goerzen <jgoerzen@complete.org> writes:
>> 
>> > Hello,
>> >
>> > I am looking at using the Obj to access internal representations of some
>> > types.
>> 
>> It is generally a bad idea. Why do you need this ?
>
> I know :-)
>
> I'm experimenting with some camlp4 stuff.  One thing I'm trying to add
> is a "print" keyword that takes a variable number of arguments of any
> type and does the right thing.  That's my starting point, at least :-)
> Even if it may not prove useful in practice (I think it will), it's a
> good way for me to learn more about OCaml.

I don't think so. It is a difficult thing you are trying to do. That
use undocumented part of the standard library. I believe that there are
better way to learn caml.

-- 
Rémi Vanicat

-------------------
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


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2004-05-07 20:01 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-05-07 16:18 [Caml-list] Differentiating lists from arrays with Obj John Goerzen
2004-05-07 16:35 ` Remi Vanicat
2004-05-07 19:00   ` John Goerzen
2004-05-07 20:01     ` Remi Vanicat

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).