From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id TAA17592 for caml-red; Thu, 9 Nov 2000 19:06:46 +0100 (MET) Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id MAA12832 for ; Thu, 9 Nov 2000 12:06:08 +0100 (MET) Received: from pcrm.win.tue.nl (pcrm.win.tue.nl [131.155.69.41]) by concorde.inria.fr (8.11.1/8.10.0) with ESMTP id eA9B63j19563 for ; Thu, 9 Nov 2000 12:06:03 +0100 (MET) Received: from pcrm (stephan@localhost [127.0.0.1]) by pcrm.win.tue.nl (8.9.3/8.9.3/SuSE Linux 8.9.3-0.1) with SMTP id MAA16282; Thu, 9 Nov 2000 12:09:44 +0100 From: Stephan Houben Reply-To: stephanh@win.tue.nl Organization: Scientific Computing Group To: Chris Hecker Subject: Re: practical functional programming Date: Thu, 9 Nov 2000 11:56:05 +0100 X-Mailer: KMail [version 1.0.28] Content-Type: text/plain Cc: caml-list@inria.fr References: <4.3.2.7.2.20001106100700.00c447b0@shell16.ba.best.com> <4.3.2.7.2.20001107021125.00acbcd0@shell16.ba.best.com> In-Reply-To: <4.3.2.7.2.20001107021125.00acbcd0@shell16.ba.best.com> MIME-Version: 1.0 Message-Id: <00110912094400.16244@pcrm> Content-Transfer-Encoding: 8bit Sender: weis@pauillac.inria.fr On Tue, 07 Nov 2000, Chris Hecker wrote: > How does the compiler "know" the datastructure is purely functional? It doesn't. Why would it have to? > In other words, is there any way for me to get a type into a tree that has a > mutable member (perhaps through an abstract type), Yes there is. You can make a map that contains, for example, values of type `int ref' or `int array'. > and if so, what happens in > that case? Well, as you already might know, O'Caml *never* copies anything unless you explicitely tell it to. For example, the following session: # let ar1 = [|1|];; val ar1 : int arr# ar1.(0) <- # ar2;; - : int array = [|2|]2;; - : unit = () ay = [|1|] # let ar2 = ar1;; val ar2 : int array = [|1|] # ar2;; - : int array = [|1|] # ar1.(0) <- 2;; - : unit = () # ar2;; - : int array = [|2|] So I mutated ar1, but the update is reflected in ar2. In C++ speech, all variables are implicitely "references" to the underlying object. ar1 and ar2 refer to the same object. If you want ar2 to be a new array, then you have to use Array.copy to explicitely tell O'Caml that you want a new array. So yes, if I have a map map1, and I get an "updated" map2 from that, then mutations to the objects held in map1 will be reflected in map2. Stephan -- ir. Stephan H.M.J. Houben tel. +31-40-2474358 / +31-40-2743497 e-mail: stephanh@win.tue.nl