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=1.8 required=5.0 tests=AWL,DNS_FROM_RFC_POST, SPF_NEUTRAL autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by yquem.inria.fr (Postfix) with ESMTP id 2A646BB84 for ; Sun, 14 Dec 2008 02:48:09 +0100 (CET) X-IronPort-AV: E=Sophos;i="4.36,218,1228086000"; d="scan'208";a="20339219" Received: from discorde.inria.fr ([192.93.2.38]) by mail3-smtp-sop.national.inria.fr with ESMTP; 14 Dec 2008 02:48:08 +0100 Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by discorde.inria.fr (8.13.6/8.13.6) with ESMTP id mBE1m7VV002270 (version=TLSv1/SSLv3 cipher=RC4-SHA bits=128 verify=OK) for ; Sun, 14 Dec 2008 02:48:08 +0100 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AjYCANr0Q0lKfSwekGdsb2JhbACTEz4BAQEBCQkMBxEDrh6BBIsSAQMBA4J6g3c X-IronPort-AV: E=Sophos;i="4.36,218,1228086000"; d="scan'208";a="20339209" Received: from yx-out-2324.google.com ([74.125.44.30]) by mail3-smtp-sop.national.inria.fr with ESMTP; 14 Dec 2008 02:47:50 +0100 Received: by yx-out-2324.google.com with SMTP id 8so820456yxg.27 for ; Sat, 13 Dec 2008 17:47:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from :user-agent:mime-version:to:cc:subject:references:in-reply-to :content-type:content-transfer-encoding; bh=07LO76ZeVtFkYLvx2Vz8QoJmgqknCWEblSElZ87vEII=; b=CiG3RMM92ROzLkn2oLcXHaMJymewraAOfKypdd8dorj7K/kcQW8EyfhlTwWkkPwvYH 5G1Ok3oacINlfloUM96tZECMy/y55IcJ14jp2h7pPEQw5u4UX7uo2PLReCpZDVVqij2V U+BajRFRpTUi6vJacDV2i27/K/ABX+xlsW2W8= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:user-agent:mime-version:to:cc:subject :references:in-reply-to:content-type:content-transfer-encoding; b=ZBYpyWm6H8eHel3R9Z4eEOQ1Z4Dlc35TA/HW7tpVZnpgtM/nLC3H2h0QvHG3QCkoyP kAj96wKZilStKbsXHMoXCmbR12oOLsxzw1qKzynSQcM8ShwRt7cfr+oVnnV0nsNesx8U z3Ab24U0HhXY9XMDLauTn4rTXWbBPwVKnrsp4= Received: by 10.64.204.13 with SMTP id b13mr4637723qbg.15.1229219269369; Sat, 13 Dec 2008 17:47:49 -0800 (PST) Received: from ?192.168.0.11? (adsl-69-154-34-21.dsl.stlsmo.swbell.net [69.154.34.21]) by mx.google.com with ESMTPS id p6sm5467334qbp.12.2008.12.13.17.47.47 (version=TLSv1/SSLv3 cipher=RC4-MD5); Sat, 13 Dec 2008 17:47:48 -0800 (PST) Message-ID: <494465C2.2000807@gmail.com> Date: Sat, 13 Dec 2008 19:47:46 -0600 From: Edgar Friendly User-Agent: Thunderbird 2.0.0.18 (X11/20081125) MIME-Version: 1.0 To: Christopher L Conway Cc: caml-list@inria.fr Subject: Re: [Caml-list] Immutable cyclic data structures via a References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Miltered: at discorde with ID 494465D7.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; mutable:01 mutable:01 ocaml:01 ugliness:01 haskell:01 mut:01 edgar:98 wrote:01 caml-list:01 immutable:01 cyclic:01 cyclic:01 immutable:01 data:02 data:02 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] > > You might have an operation, call it "unmute", that changes the type > of the structure. There would need to be a corresponding type-level > operation, call it "UNMUTE", that strips the "mutable" qualifier from > mutable fields. E.g., > I'm pretty confident this isn't possible in plain-vanilla OCaml. You > can do something similar (at a high cost in ugliness) using a > variation of "Tying the Knot" a la Haskell.[2] ExtLib (and thus batteries) uses your unmute (called [inj]) for converting a mutable list into the normal immutable list. The definition: external inj : 'a mut_list -> 'a list = "%identity" It's effectively using Obj.magic, but specialized on these types. As always, be *very* careful when using this kind of type system voodoo, but it can work under certain circumstances. E.