From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by yquem.inria.fr (Postfix) with ESMTP id 6EA54BB91 for ; Sat, 23 Jul 2005 14:34:06 +0200 (CEST) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id j6NCY5OQ019045 for ; Sat, 23 Jul 2005 14:34:05 +0200 Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id OAA11600 for ; Sat, 23 Jul 2005 14:34:05 +0200 (MET DST) Received: from kraid.nerim.net (smtp-106-saturday.nerim.net [62.4.16.106]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id j6NCY4Yj019040 for ; Sat, 23 Jul 2005 14:34:05 +0200 Received: from [192.168.1.2] (xleroy.net1.nerim.net [62.212.116.147]) by kraid.nerim.net (Postfix) with ESMTP id C6A5D40E1E; Sat, 23 Jul 2005 14:34:03 +0200 (CEST) Message-ID: <42E2393B.5030209@inria.fr> Date: Sat, 23 Jul 2005 14:34:03 +0200 From: Xavier Leroy User-Agent: Mozilla Thunderbird 1.0.2 (X11/20050317) X-Accept-Language: en-us, en MIME-Version: 1.0 To: Michael Alexander Hamburg Cc: Thomas Fischbacher , caml-list@inria.fr Subject: Re: [Caml-list] How to do this properly with OCaml? References: In-Reply-To: X-Enigmail-Version: 0.91.0.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Miltered: at nez-perce with ID 42E2393D.002 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Miltered: at nez-perce with ID 42E2393D.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 ocaml:01 binary:01 heap:01 heap:01 avoided:01 ocaml:01 compiler:01 invariants:01 arrays:01 debugging:01 bug:01 sidewalk:98 needle:98 functions:01 X-Spam-Checker-Version: SpamAssassin 3.0.3 (2005-04-27) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.0.3 > I was constructing a binary heap of tuples the other day. After pondering > these options, I just used Obj.magic 0 as the null value in the array. > The heap was in a module, so nothing else could see the array, and I could > prove that the code never accessed the null elements, so the use of > Obj.magic seemed justified. In other terms: " I was walking in the city the other day. I saw a syringe lying on the sidewalk. I stuck the needle in my forearm. That was a classy neighborhood, so the use of the syringe seemed justified. " Sorry for being sarcastic, but I strongly feel that any suggestion to use Obj functions should be avoided on this list. The OCaml compiler performs some type-dependent optimizations that can result in incorrect code (w.r.t. GC invariants) if wrong types are given using Obj.magic. For instance, the following implementation of "magic" arrays will eventually cause the GC to crash: type 'a t = int array let get (a: 'a t) i = (Obj.magic a.(i) : 'a) let set (a: 'a t) i (x: 'a) = a.(i) <- (Obj.magic x : int) while the same code with "string" instead of "int" will not. You don't understand why? Then, don't use Obj.magic. A few years ago, I spent one full day debugging a mysterious crash in code provided by a user, then realized that the problem was exactly the use of Obj.magic outlined above. I then swore to throw away all bug reports whose repro case uses Obj. So, you can break the type system with Obj, but you get to keep the pieces afterwards. Coming back to the initial question, I would first warn against premature optimization: quite possibly the overhead of the "option" solution is negligible. If not, just ask the user to pass an initial value of the heap element type to the "create heap" function. - Xavier Leroy