From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by yquem.inria.fr (Postfix) with ESMTP id 1B082BB83 for ; Wed, 7 Jun 2006 02:34:12 +0200 (CEST) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by nez-perce.inria.fr (8.13.6/8.13.6) with ESMTP id k570YB9B032734 for ; Wed, 7 Jun 2006 02:34:11 +0200 Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id CAA12183 for ; Wed, 7 Jun 2006 02:34:10 +0200 (MET DST) Received: from rabbit.math.nagoya-u.ac.jp (rabbit.math.nagoya-u.ac.jp [133.6.130.5]) by nez-perce.inria.fr (8.13.6/8.13.6) with ESMTP id k570Y8Mn032700 for ; Wed, 7 Jun 2006 02:34:09 +0200 Received: from localhost (nat00-c2 [172.16.60.194]) by rabbit.math.nagoya-u.ac.jp (8.12.11/3.7W) with ESMTP id k570Xvsn020814; Wed, 7 Jun 2006 09:33:57 +0900 (JST) Date: Wed, 07 Jun 2006 09:36:26 +0900 (JST) Message-Id: <20060607.093626.07649187.garrigue@math.nagoya-u.ac.jp> To: moj@utu.fi Cc: caml-list@inria.fr Subject: Re: [Caml-list] Re: immutable strings From: Jacques Garrigue In-Reply-To: <20060605205433.GA14267@utu.fi> References: <196F1D996F92CD46A542EA519DB8CE4703CCA9F0@orsmsx409> <20060605205433.GA14267@utu.fi> X-Mailer: Mew version 4.2 on Emacs 21.3 / Mule 5.0 (SAKAKI) Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Miltered: at nez-perce with ID 44861F03.001 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Miltered: at nez-perce with ID 44861F00.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; utu:01 matti:01 jokinen:01 genlex:01 lexer:01 genlex:01 kwd:01 mutable:01 caml-list:01 token:01 behaviour:01 functions:01 explicitly:01 explicitly:01 immutable:01 X-Spam-Checker-Version: SpamAssassin 3.0.3 (2005-04-27) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.0.3 From: moj@utu.fi (Matti Jokinen) > > In practice, the obvious library calls are safe, so like Aleksey, I use > > the built-in strings for the sake of convenience and compatibility. But > > it's unsatisfactory intellectually. > > Actually, there are cases of unsafe sharing even in the standard library. > > > # let x = "X" in > let g = Genlex.make_lexer [x] in > let s = Stream.of_string "X" in > let t = g s in > let _ = Stream.peek t in > x.[0] <- 'Y'; > Stream.peek t;; > > result: > > - : Genlex.token option = Some (Genlex.Kwd "Y") [...] > I think this demonstrates that the problem is real: it is too easy to > forget copying. I don't think this is what the original poster meant by "unsafe". Standard library functions do not mutate strings when this is not explicitly stated. If you apply this principle to user behaviour, it means that you shouldn't mutate a string passed to or from a library function except when it is explicitly ok. In practice this usually works well, because the string type is actually used as two independent types: * mutable strings for some I/O and buffers * immutable strings for all other uses But this still puts a burden on users. Jacques Garrigue