caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Pierre Weis <pierre.weis@inria.fr>
To: skaller@ozemail.com.au (John Max Skaller)
Cc: warplayer@free.fr, garrigue@kurims.kyoto-u.ac.jp, caml-list@inria.fr
Subject: Re: [Caml-list] about Obj.magic
Date: Sat, 1 Jun 2002 19:42:34 +0200 (MET DST)	[thread overview]
Message-ID: <200206011742.TAA16993@pauillac.inria.fr> (raw)
In-Reply-To: <3CF7BF45.40201@ozemail.com.au> from John Max Skaller at "Jun 1, 102 04:21:57 am"

> An 'almost legal' use of Obj.magic is to populate an array with dummy 
> values.
> For example:
> 
>     class Deep  ..
>     let a = Array.make 100  (Obj.magic 0 :> Deep) in
>     let len_a = ref 0 in
> 
> This is roughly as safe as an unsafe array access,
> only now the array length is a variable.
> 
> -- 
> John Max Skaller, mailto:skaller@ozemail.com.au
> snail:10/1 Toxteth Rd, Glebe, NSW 2037, Australia.
> voice:61-2-9660-0850

I'm afraid that this use is not ``almost legal'' and, on the contrary,
it is strongly discouraged by good programming practice!

However, your example is interesting as a good counterexample: this is
typical of a bad usage of Obj.magic! The program you gave is indeed
wrong, unless you have a proof that all the elements of array ``a''
will be assigned before accessed, and may be also you have an
additional proof that the memory management system of Caml can deal
with the integer "0" as a valid object of the class Deep. Hence, this
occurrence of Obj.magic is unsafe, error prone, and should not be
encouraged.

The rule of thumb for usages of Obj.magic is as Jacques already stated
it: you need a theorem to write Obj.magic. That's the way the Caml
system uses it: we always have a meta theorem that proves that this
very occurence of Obj.magic is safe. Otherwise, the resulting program
has always the same property : one day or another, it gives you Bus
error.

Thank you for your example that will help Caml users.

Pierre Weis

INRIA, Projet Cristal, Pierre.Weis@inria.fr, http://pauillac.inria.fr/~weis/


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


  reply	other threads:[~2002-06-01 17:44 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-05-30 21:09 dengping zhu
2002-05-30 22:38 ` Jacques Garrigue
2002-05-31  4:00 ` Daniel de Rauglaudre
2002-05-31  5:37   ` Oleg
2002-05-31  6:17     ` Daniel de Rauglaudre
     [not found]       ` <p05100300b91cea02dbf5@[192.168.1.22]>
2002-05-31  9:50         ` [Caml-list] SML->OCaml (was: about Obj.magic) Daniel de Rauglaudre
2002-05-31 15:39   ` [Caml-list] about Obj.magic dengping zhu
2002-05-31 16:28     ` John D. Barnett
2002-06-01  8:37     ` Jacques Garrigue
2002-06-01 16:00       ` dengping zhu
2002-06-01 17:20         ` Pierre Weis
2002-05-31  9:17 ` Warp
2002-05-31 13:58   ` Jacques Garrigue
2002-05-31 14:06     ` Warp
2002-05-31 18:21       ` John Max Skaller
2002-06-01 17:42         ` Pierre Weis [this message]
2002-06-02 15:15           ` John Max Skaller
     [not found]           ` <D37FA3E3-763B-11D6-BE8F-0003938819CE@inria.fr>
2002-06-04 10:22             ` Pierre Weis
2002-05-31 14:10     ` Remi VANICAT
2002-05-31 16:19     ` Christophe TROESTLER
2002-05-31 18:06       ` Olivier Andrieu
2002-05-31 22:03         ` Christophe TROESTLER

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=200206011742.TAA16993@pauillac.inria.fr \
    --to=pierre.weis@inria.fr \
    --cc=caml-list@inria.fr \
    --cc=garrigue@kurims.kyoto-u.ac.jp \
    --cc=skaller@ozemail.com.au \
    --cc=warplayer@free.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).