From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.8 required=5.0 tests=AWL,SPF_FAIL autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by yquem.inria.fr (Postfix) with ESMTP id 9A014BBAF for ; Sun, 14 Dec 2008 21:06:28 +0100 (CET) X-IronPort-AV: E=Sophos;i="4.36,219,1228086000"; d="scan'208";a="21254089" Received: from discorde.inria.fr ([192.93.2.38]) by mail1-smtp-roc.national.inria.fr with ESMTP; 14 Dec 2008 21:06:28 +0100 Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by discorde.inria.fr (8.13.6/8.13.6) with ESMTP id mBEK6Si8018085 (version=TLSv1/SSLv3 cipher=RC4-SHA bits=128 verify=OK) for ; Sun, 14 Dec 2008 21:06:28 +0100 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AtoDAEn1RElQRFuwgWdsb2JhbACTUwEBFiK5a4J+g3k X-IronPort-AV: E=Sophos;i="4.36,219,1228086000"; d="scan'208";a="21254088" Received: from furbychan.cocan.org ([80.68.91.176]) by mail1-smtp-roc.national.inria.fr with ESMTP/TLS/AES256-SHA; 14 Dec 2008 21:06:27 +0100 Received: from rich by furbychan.cocan.org with local (Exim 4.63) (envelope-from ) id 1LBxEK-0002xz-OT; Sun, 14 Dec 2008 20:06:23 +0000 Date: Sun, 14 Dec 2008 20:06:20 +0000 To: Christopher L Conway Cc: caml-list@inria.fr Subject: Re: [Caml-list] Immutable cyclic data structures via a Message-ID: <20081214200620.GA11368@annexia.org> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.13 (2006-08-11) From: Richard Jones X-Miltered: at discorde with ID 49456744.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; mutable:01 appending:01 iirc:01 compiler:01 mutable:01 pointers:01 extensively:01 garbage:01 wrote:01 heap:01 heap:01 caml-list:01 minor:01 immutable:01 cyclic:01 On Sat, Dec 13, 2008 at 09:45:07PM +0000, Christopher L Conway wrote: > This makes me wonder: if you add mutable fields to a type in order to > build a cyclic data structure, but you intend to use the data > structure immutably once it is built, is there any way to "freeze" the > value, producing an immutable cyclic structure? This is a common > pattern, similar to Bloch's Builder pattern for Java.[1] Note that extlib does this in the ExtList.List module, using lots of Obj.magic of course: http://code.google.com/p/ocaml-extlib/source/browse/trunk/extlib/extList.ml#31 This is purely for speed, so we can do things like appending to (private) lists when building them, and returning a real list. IIRC it was rather controversial at the time, and there is still some question as to whether it is really safe -- doesn't the compiler track mutable objects specially so the introduction of major heap -> minor heap pointers (ie. old -> new objects) works correctly with the garbage collector? In any case, it seems to work. I have used ExtList.List extensively in masses of production code and never found a problem. Rich. -- Richard Jones Red Hat