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=3.4 required=5.0 tests=AWL,DNS_FROM_RFC_POST, HTML_10_20,HTML_MESSAGE,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 mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by yquem.inria.fr (Postfix) with ESMTP id A222ABBC4 for ; Sun, 5 Apr 2009 01:35:26 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Am0DAGiL10lKfS4ejWdsb2JhbACCVIRhjVZbPwEBAQEJCQoJDwWkCAeBAo5rAQMBA4QMBoVD X-IronPort-AV: E=Sophos;i="4.39,324,1235948400"; d="scan'208";a="23960931" Received: from yw-out-2324.google.com ([74.125.46.30]) by mail2-smtp-roc.national.inria.fr with ESMTP; 05 Apr 2009 01:35:25 +0200 Received: by yw-out-2324.google.com with SMTP id 5so1102644ywh.3 for ; Sat, 04 Apr 2009 16:35:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:reply-to:in-reply-to :references:date:message-id:subject:from:to:content-type; bh=CCScKV8TKu4WMY+IXOhtXvn2WHOpqdks+qPpwkAco3c=; b=BRBFIrJC+6nOYozhjaPyC68HLrsZxmmtd49fq+cJrHMGidLMBHYSOcayD8qzFhNR/v 7V+rA3BAQPr/VrNusw/CnFpwg8ScZLYLnqBahsfs2MM5GXPon7Nq9K8AQAoHV6HLzIfP Pja1ZOs5lTXG9+to3ke+0PSYSfHu/TQ+99AKg= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:reply-to:in-reply-to:references:date:message-id :subject:from:to:content-type; b=IW0dIOWft3B0y8ky13z5OmaSCEHs0lOw8x0PJZZS+JBHN3hzS6Sy8ozhRdbGBRQcHL di+TE/czVJ9HvwLu9IinpgUS4LIuxeULxw3YYMjVhyWqhhozXC4HRsagT1n4t5RIFVa/ zdUd5+zBqdMhQu6QudVinmPPeIyP+gVt0jRm0= MIME-Version: 1.0 Received: by 10.100.254.15 with SMTP id b15mr4365141ani.155.1238888124949; Sat, 04 Apr 2009 16:35:24 -0700 (PDT) Reply-To: yminsky@gmail.com In-Reply-To: <87ocvc6qa1.fsf@frosties.localdomain> References: <200904031256.33357.jon@ffconsultancy.com> <1238836474.6250.11.camel@Blefuscu> <87ocvc6qa1.fsf@frosties.localdomain> Date: Sat, 4 Apr 2009 19:35:24 -0400 Message-ID: <891bd3390904041635x6a6997ddpc1e12f90d0fc3153@mail.gmail.com> Subject: Re: [Caml-list] Strings From: Yaron Minsky To: caml-list@yquem.inria.fr Content-Type: multipart/alternative; boundary=0016369fa3749aa6f30466c31ed2 X-Spam: no; 0.00; yaron:01 minsky:01 yminsky:01 mutable:01 arrays:01 lib:01 experimented:01 mutable:01 mutation:01 ocaml:01 node:01 arrays:01 lib:01 experimented:01 mutation:01 --0016369fa3749aa6f30466c31ed2 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit On Sat, Apr 4, 2009 at 5:51 PM, Goswin von Brederlow wrote: > Mutable/Immutable can really nicely done with phantom types and is > independent of the data structure used. It works for strings, lists, > arrays, sets, trees, ... and I think all standard modules should have > it. The official standard lib is rather lacking there but that is why > there is Batteries. The more I hear/see of it the more I like it. On this note, there's a small variation on this idea that we've experimented with at Jane Street that I think is worth mentioning. When people do this kind of thing, they usually have two phantom tags, "immutable" and "mutable", but, there is another natural one to add: "readonly". A mutable value is, as one would expect, a value that can be modified; and an immutable value is one that can not be modified. A readonly value is one that can not be modified, but that might change nonetheless because somewhere else in the program there is a mutable handle to the same underlying value. You implement this by making the interface for immutable and readonly values identical, except that a readonly value can be created from a mutable value without copying. Mutable and immutable are typically the most important types of access control to implement, but readonly can be useful in special cases, where you have a value that you really want to mutate, but that you want to control where in the program the mutation can happen. There's one extra tradeoff worth mentioning with using phantom types for controlling mutability, which is that values made immutable in this way are not quite as good as values that are immutable at the lowest level. The reason is that ordinary immutable values are understood by the type system as such, and this goes into the compiler's understanding of the value restriction and variance. This means there are cases where, say, a phantom-immutable list won't work, but an ordinary immutable list will. (For those interested in a short and elementary tutorial on how to implement access control with phantom types, look here: http://ocaml.janestreet.com/?q=node/11) y --0016369fa3749aa6f30466c31ed2 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable On Sat, Apr 4, 2009 at 5:51 PM, Goswin von Brederlow <= goswin-v-b@web.de> wrote= :
Mutable/Immutable can really nicely done with phantom types and is
independent of the data structure used. It works for strings, lists,
arrays, sets, trees, ... and I think all standard modules should have
it. The official standard lib is rather lacking there but that is why
there is Batteries. The more I hear/see of it the more I like it.

On this note, there's a small variation on this idea that w= e've experimented with at Jane Street that I think is worth mentioning.= =A0 When people do this kind of thing, they usually have two phantom tags, = "immutable" and "mutable", but, there is another natura= l one to add: "readonly".

A mutable value is, as one would expect, a value that can be modified; = and an immutable value is one that can not be modified.=A0 A readonly value= is one that can not be modified, but that might change nonetheless because= somewhere else in the program there is a mutable handle to the same underl= ying value.=A0 You implement this by making the interface for immutable and= readonly values identical, except that a readonly value can be created fro= m a mutable value without copying.=A0 Mutable and immutable are typically t= he most important types of access control to implement, but readonly can be= useful in special cases, where you have a value that you really want to mu= tate, but that you want to control where in the program the mutation can ha= ppen.

There's one extra tradeoff worth mentioning with using phantom type= s for controlling mutability, which is that values made immutable in this w= ay are not quite as good as values that are immutable at the lowest level.= =A0 The reason is that ordinary immutable values are understood by the type= system as such, and this goes into the compiler's understanding of the= value restriction and variance.=A0 This means there are cases where, say, = a phantom-immutable list won't work, but an ordinary immutable list wil= l.

(For those interested in a short and elementary tutorial on how to impl= ement access control with phantom types, look here: http://ocaml.janestreet.com/?q=3Dnode/11)=

y

--0016369fa3749aa6f30466c31ed2--