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 BAA01372 for caml-redistribution@pauillac.inria.fr; Mon, 10 Apr 2000 01:25:08 +0200 (MET DST) Resent-Message-Id: <200004092325.BAA01372@pauillac.inria.fr> 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 QAA12082 for ; Thu, 6 Apr 2000 16:25:53 +0200 (MET DST) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by concorde.inria.fr (8.8.7/8.8.7) with ESMTP id QAA22065; Thu, 6 Apr 2000 16:25:52 +0200 (MET DST) Received: (from xleroy@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id QAA22815; Thu, 6 Apr 2000 16:25:52 +0200 (MET DST) Message-ID: <20000406162552.49765@pauillac.inria.fr> Date: Thu, 6 Apr 2000 16:25:52 +0200 From: Xavier Leroy To: Markus Mottl , Pierre Weis Cc: OCAML Subject: Re: cyclic value construction ("let rec") References: <20000406151819.42281@pauillac.inria.fr> <200004061334.PAA18159@miss.wu-wien.ac.at> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Mailer: Mutt 0.89.1 In-Reply-To: <200004061334.PAA18159@miss.wu-wien.ac.at>; from Markus Mottl on Thu, Apr 06, 2000 at 03:34:18PM +0200 Resent-From: weis@pauillac.inria.fr Resent-Date: Mon, 10 Apr 2000 01:25:08 +0200 Resent-To: caml-redistribution@pauillac.inria.fr > > [Pierre Weis:] > > This certainly suggests to allow the export of an immutable view of a > > record type with mutable fields. This way you could do the > > initialization in a safe way (no magic) using side effects, and still > > export a safe immutable type to the external world. > > [Markus Mottl:] > Sounds like a good idea! Using powerful "magic" is probably too dangerous > for "everyday"-use and definitely not in accordance with the "zero defect" > ambitions of the type system... > Although it would sometimes be nice to even hide specific fields of the > record, this would probably not work well together with separate > compilation. However, the memory layout of the fields does not change by > just omitting the "mutable" declaration, so this should not do any harm. Alas, it can do a lot of harm. For one thing, you could break type safety this way, just like with polymorphic references: A.ml: type 'a t = { mutable contents: 'a } let assign t v = t.contents <- v A.mli: type 'a t = { contents: 'a} val assign: 'a t -> 'a -> unit Client.ml: open A ... let x = { contents = [] } in assign x [1]; x.contents = [true] When typing Client.ml, since "contents" is assumed immutable, the definition of x is a syntactic value, hence x receives type forall 'a. 'a list t But of course this typing is invalidated by the call to "assign", and you end up comparing an int list to a bool list -- a typing violation. Some compiler optimisations, specific to immutable structures, could similarly be broken. So, no, we can't allow exporting a record with different mutability annotations than in its definition. - Xavier Leroy