caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Is this use of Object.magic safe?
@ 2000-11-13  8:43 Stephan Houben
  2000-11-14 21:31 ` Anton Moscal
  0 siblings, 1 reply; 3+ messages in thread
From: Stephan Houben @ 2000-11-13  8:43 UTC (permalink / raw)
  To: caml-list

Hello list,

I am currently writing a small interpreted language for my app.
The language is dynamically typed, which means that a value is represented
somewhat like this:

type value =
  True | False
| Int of int
| String of string
| Array of value array

Now a well-known optimisation for such an interpreter is to make small
int's not heap-allocated objects but to represent them directly in the pointer.
In fact, O'Caml itself already does this. Moreover, it allows access to
this info via the Obj module.

My idea is to make "value" an abstract type, and have a separate type
allocated_value, which doesn't contain the Int constructor:

type value
 
type allocated_value =
  True | False
| String of string
| Array of value

Then I can use Obj.magic to make constructor and destructor functions
for value:

let value_of_int (i : int) : value =
  Obj.magic i

let value_of_allocated_value (av :allocated_value) : value=
  Obj.magic av
 
let unwrap (int_func : int -> 'a) 
                (aval_func : allocated_value -> 'a)
                (v : value) : 'a =
  if Obj.is_int (Obj.repr v)
  then int_func (Obj.magic v)
  else aval_func (Obj.magic v) 

I have three questions:
1. Is this safe w.r.t. the garbage collector? Or am I going to confuse the GC?
2. Can I safely marshall values?
3. Is it worth the trouble? Perhaps O'Caml already does something smart
   when a constructor of the form `Int of int' is encountered?

Stephan
-- 
ir. Stephan H.M.J. Houben
tel. +31-40-2474358 / +31-40-2743497
e-mail: stephanh@win.tue.nl



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

* Re: Is this use of Object.magic safe?
  2000-11-13  8:43 Is this use of Object.magic safe? Stephan Houben
@ 2000-11-14 21:31 ` Anton Moscal
  0 siblings, 0 replies; 3+ messages in thread
From: Anton Moscal @ 2000-11-14 21:31 UTC (permalink / raw)
  To: stephanh; +Cc: caml-list

On Mon, 13 Nov 2000, Stephan Houben wrote:

> I have three questions:
> 1. Is this safe w.r.t. the garbage collector? Or am I going to confuse the GC?
> 2. Can I safely marshall values?
> 3. Is it worth the trouble? Perhaps O'Caml already does something smart
>    when a constructor of the form `Int of int' is encountered?

Answers for 1 & 2 is true,
for 3 - false: if you try to run the following program:

let _ = Printf.printf "%b\n" 
  (value_of_int 0 == value_of_allocated_value True)

you got "true" as the result.

The cause is the following: variant without arguments 
represented in the same way with int's.

Correct solution will be:

type allocated_value =
  Bool of bool 
| String of string 
| Array of value

Regards,
Anton Moscal 



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

* Re:  Is this use of Object.magic safe?
@ 2000-11-14 14:41 Damien Doligez
  0 siblings, 0 replies; 3+ messages in thread
From: Damien Doligez @ 2000-11-14 14:41 UTC (permalink / raw)
  To: caml-list

>From: Stephan Houben <stephan@pcrm.win.tue.nl>
>
>let value_of_int (i : int) : value =
>  Obj.magic i
>
>let value_of_allocated_value (av :allocated_value) : value=
>  Obj.magic av
> 
>let unwrap (int_func : int -> 'a) 
>                (aval_func : allocated_value -> 'a)
>                (v : value) : 'a =
>  if Obj.is_int (Obj.repr v)
>  then int_func (Obj.magic v)
>  else aval_func (Obj.magic v) 

>1. Is this safe w.r.t. the garbage collector? Or am I going to confuse the GC?

No problem with the GC.


>2. Can I safely marshall values?

No problem with marshalling either.


>3. Is it worth the trouble? Perhaps O'Caml already does something smart
>   when a constructor of the form `Int of int' is encountered?

It doesn't work as you expect because O'Caml is strongly typed, so it
can (and does) use a smart representation for your constant
constructors True and False  They are represented as integers, so your
unwrap function cannot tell the difference between a boolean and an
integer.  This is typical of the dangers of using Obj.magic.

-- Damien



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

end of thread, other threads:[~2000-11-15 21:38 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-11-13  8:43 Is this use of Object.magic safe? Stephan Houben
2000-11-14 21:31 ` Anton Moscal
2000-11-14 14:41 Damien Doligez

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