caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Zheng Li <zheng_li@users.sourceforge.net>
To: David Rajchenbach-Teller <David.Teller@ens-lyon.org>
Cc: Goswin von Brederlow <goswin-v-b@web.de>,
	Daniel Buenzli <daniel.buenzli@erratique.ch>,
	OCaml List <caml-list@yquem.inria.fr>
Subject: Re: Strings
Date: Sun, 05 Apr 2009 12:06:04 +0200	[thread overview]
Message-ID: <49D8828C.40402@users.sourceforge.net> (raw)
In-Reply-To: <1238836234.6250.6.camel@Blefuscu>

On 4/4/2009 11:10 AM, David Rajchenbach-Teller wrote:
>
> On Fri, 2009-04-03 at 23:44 +0200, Goswin von Brederlow wrote:
>> It wouldn't be too hard to change the string module to allow for both
>> mutable and immutable strings:
>
> [...]
>
> Done in Batteries.
>
> # "foo";;   (*OCaml base string*)
> - : string = "foo"
> # ro"foo";;(*Read-only string*)
> - : [ `Read ] Batteries.String.Cap.t = ro"foo"

With phantom type alone, the abstraction can still leak.

----
# open Batteries.String.Cap;;
# let s = "asdf";;
val s : string = "asdf"
# let ro_s = read_only (of_string s);;
val ro_s : [ `Read ] Batteries.String.Cap.t = ro"asdf"
# s.[3] <- 'z';;
# ro_s;;
- : [ `Read ] Batteries.String.Cap.t = ro"asdz"
----

Well, this example is artificial. A more intuitive example would be 
(Look how similar it is with the previous os_type string example !):

----
# let ro_s = ro"asdf";;
val ro_s : [ `Read ] Batteries.String.Cap.t = ro"asdf"
# (to_string ro_s).[3] <- 'z';;
....
----

however, with no covariants defined in batteries' String.Cap.t type 
(why?), the second example won't compile. The compiler simply doesn't 
allow me to print out a read-only string, nor does it allow many 
reasonable things like <<join ro"asdf" ro"jkl">> etc.

I understand that the phantom trick can't guarantee the immutability in 
a whole since the underlying data type (if exposed) can still be 
mutable. However, String.Cap.t is not dealing with arbitrary polymorphic 
types here! it deals with just the specific primitive string type, and 
controlling capacity over standard string is the only thing it intend to do.

String.copy on the border might solve this problem, even though there 
could still be other issues left.

All the best
--
Zheng


  reply	other threads:[~2009-04-05 10:04 UTC|newest]

Thread overview: 47+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-04-03 11:56 Strings Jon Harrop
2009-04-03 12:25 ` [Caml-list] Strings Paolo Donadeo
2009-04-03 14:18 ` Ashish Agarwal
2009-04-03 14:46   ` Jon Harrop
2009-04-03 15:03     ` Daniel Bünzli
2009-04-03 16:52       ` Martin Jambon
2009-04-03 17:50         ` Daniel Bünzli
2009-04-03 19:46           ` Paolo Donadeo
2009-04-03 20:41             ` Harrison, John R
2009-04-04 10:11               ` Jon Harrop
2009-04-04 11:12                 ` David Teller
2009-04-04 11:40                   ` Jon Harrop
2009-04-04 12:34                     ` David Rajchenbach-Teller
2009-04-18 12:31                   ` Arkady Andrukonis
2009-04-04 10:13             ` Jon Harrop
2009-04-03 21:44           ` Goswin von Brederlow
2009-04-04  9:10             ` David Rajchenbach-Teller
2009-04-05 10:06               ` Zheng Li [this message]
2009-04-06  9:20                 ` Strings David Rajchenbach-Teller
2009-04-06 10:07                   ` Strings Goswin von Brederlow
2009-04-06 11:03                   ` Strings Zheng Li
2009-04-04 17:11           ` [Caml-list] Strings Kuba Ober
2009-04-04 17:26             ` Jon Harrop
2009-04-05 20:54           ` Richard Jones
2009-04-05 23:40             ` Daniel Bünzli
2009-04-03 18:24         ` Florian Hars
2009-04-03 20:34         ` Arnaud Spiwack
2009-04-04 10:20       ` Jon Harrop
2009-04-04  9:14 ` David Rajchenbach-Teller
2009-04-04  9:26   ` Alp Mestan
2009-04-04 10:55     ` blue storm
2009-04-04 21:51     ` Goswin von Brederlow
2009-04-04 23:35       ` Yaron Minsky
2009-04-05  9:36         ` David Rajchenbach-Teller
2009-04-05 10:08           ` Alp Mestan
2009-04-05 21:41             ` Goswin von Brederlow
2009-04-05 21:40           ` Goswin von Brederlow
2009-04-05  2:55       ` Jon Harrop
2009-04-05  4:22         ` Edgar Friendly
2009-04-05  7:03           ` Goswin von Brederlow
2009-04-05  6:57         ` Goswin von Brederlow
2009-04-05  7:11           ` Jon Harrop
2009-04-04 10:11   ` Jon Harrop
2009-04-04 21:39   ` Goswin von Brederlow
2009-04-05  7:14   ` Romain Beauxis
2009-04-05  9:34     ` David Rajchenbach-Teller
2009-04-05 21:37     ` Goswin von Brederlow

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=49D8828C.40402@users.sourceforge.net \
    --to=zheng_li@users.sourceforge.net \
    --cc=David.Teller@ens-lyon.org \
    --cc=caml-list@yquem.inria.fr \
    --cc=daniel.buenzli@erratique.ch \
    --cc=goswin-v-b@web.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).