caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* immutable Strings?
@ 2005-03-12 20:50 Oliver Bandel
  2005-03-14 10:30 ` [Caml-list] " Pascal Zimmer
  0 siblings, 1 reply; 9+ messages in thread
From: Oliver Bandel @ 2005-03-12 20:50 UTC (permalink / raw)
  To: caml-list


Hello,

for records it is possible to say "mutable" to change a normally
non mutable value into a mutable one.

It would be nice to have the possibility to turn on
immutability fpr strings with a keyword like "immutable"
or so.

So it could be forbidden to modify strings in cases,
where it makes sense; otherwise it must be provided
String.copy at a lot of cases in a program, that
want's to forbid modyfiing the original data.

And disallowing a modification is much stronger/more strict
than only allowing modyfiing a copy.
So, it would be nice to have such a feature in newer versions
of OCaml.

Ciao,
   Oliver

-- 
      "If you desire sanity in this embarassment,
        stuff not the ear of your mind with cotton"
                                         (David Torn) 


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Caml-list] immutable Strings?
  2005-03-12 20:50 immutable Strings? Oliver Bandel
@ 2005-03-14 10:30 ` Pascal Zimmer
  2005-03-14 10:41   ` Jon Harrop
  2005-03-14 12:57   ` Richard Jones
  0 siblings, 2 replies; 9+ messages in thread
From: Pascal Zimmer @ 2005-03-14 10:30 UTC (permalink / raw)
  To: Oliver Bandel; +Cc: caml-list

Oliver Bandel wrote:
> for records it is possible to say "mutable" to change a normally
> non mutable value into a mutable one.
> 
> It would be nice to have the possibility to turn on
> immutability fpr strings with a keyword like "immutable"
> or so.
> 
> So it could be forbidden to modify strings in cases,
> where it makes sense; otherwise it must be provided
> String.copy at a lot of cases in a program, that
> want's to forbid modyfiing the original data.
> 
> And disallowing a modification is much stronger/more strict
> than only allowing modyfiing a copy.
> So, it would be nice to have such a feature in newer versions
> of OCaml.

You can achieve the same effect quite easily using modules to provide a 
new interface to String, where you restrict the set of possible 
operations and abstract the datatype:

module type IMMSTRING =
  sig
   type t    (* abstract type *)

   val create : string -> t
   val copy : t -> t
   val copy_to_string : t -> string

   val get : t -> int -> char
   val cat : t -> t -> t
   val print_string : t -> unit

   (* ... *)
  end;;

module ImmString : IMMSTRING =
  struct
   type t = string

   let create = String.copy
   let copy = String.copy
   let copy_to_string = String.copy

   let get s i = s.[i]
   let cat s t = s ^ t
   let print_string = print_string

   (* ... *)
  end;;

Now you can write:

# let s = ImmString.create "abcde";;
val s : ImmString.t = <abstr>
# ImmString.get s 0;;
- : char = 'a'
# s.[0] <- 'f';;
This expression has type ImmString.t but is here used with type string

The bad points:
- you cannot use the shortcuts s.[i] and (^) anymore (this gets the code 
less readable especially for the first one)
- you have to make a copy when creating an immutable string; if you were 
not planning to keep the original string to modify it, this copy is in 
fact useless

Pascal


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Caml-list] immutable Strings?
  2005-03-14 10:30 ` [Caml-list] " Pascal Zimmer
@ 2005-03-14 10:41   ` Jon Harrop
  2005-03-14 11:15     ` Pascal Zimmer
  2005-03-14 12:57   ` Richard Jones
  1 sibling, 1 reply; 9+ messages in thread
From: Jon Harrop @ 2005-03-14 10:41 UTC (permalink / raw)
  To: caml-list

On Monday 14 March 2005 10:30, Pascal Zimmer wrote:
> The bad points:
> - you cannot use the shortcuts s.[i] and (^) anymore (this gets the code
> less readable especially for the first one)
> - you have to make a copy when creating an immutable string; if you were
> not planning to keep the original string to modify it, this copy is in
> fact useless

Do you not also lose the ability to do pattern matching and to use all of the 
existing string-based functionality (e.g. the Str module)?

-- 
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
Objective CAML for Scientists
http://www.ffconsultancy.com/products/ocaml_for_scientists


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Caml-list] immutable Strings?
  2005-03-14 10:41   ` Jon Harrop
@ 2005-03-14 11:15     ` Pascal Zimmer
  0 siblings, 0 replies; 9+ messages in thread
From: Pascal Zimmer @ 2005-03-14 11:15 UTC (permalink / raw)
  To: Jon Harrop; +Cc: caml-list

Jon Harrop wrote:
> On Monday 14 March 2005 10:30, Pascal Zimmer wrote:
> 
>>The bad points:
>>- you cannot use the shortcuts s.[i] and (^) anymore (this gets the code
>>less readable especially for the first one)
>>- you have to make a copy when creating an immutable string; if you were
>>not planning to keep the original string to modify it, this copy is in
>>fact useless
> 
> 
> Do you not also lose the ability to do pattern matching and to use all of the 
> existing string-based functionality (e.g. the Str module)?
> 

Yes, indeed, this is true for pattern matching. You have to replace it 
with comparisons.

For functions that use strings, you have to add a new version in your 
module, i.e. for every function
  Lib.f : ... -> string -> ...
using strings that you want to use on immutable strings, all you have to 
do is add the lines:
  val f : ... -> t -> ...
  let f = Lib.f
in module type and implementation respectively (see for example 
print_string in my previous post), and use ImmString.f instead of Lib.f.
I have to admit this can get painful if you have to use a lot of them...

Pascal


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Caml-list] immutable Strings?
  2005-03-14 10:30 ` [Caml-list] " Pascal Zimmer
  2005-03-14 10:41   ` Jon Harrop
@ 2005-03-14 12:57   ` Richard Jones
  2005-03-14 13:01     ` Richard Jones
  2005-03-15  8:40     ` Oliver Bandel
  1 sibling, 2 replies; 9+ messages in thread
From: Richard Jones @ 2005-03-14 12:57 UTC (permalink / raw)
  To: Pascal Zimmer; +Cc: Oliver Bandel, caml-list

On Mon, Mar 14, 2005 at 11:30:57AM +0100, Pascal Zimmer wrote:
> - you cannot use the shortcuts s.[i] and (^) anymore (this gets the code 
> less readable especially for the first one)

You can get the s.[i] shortcut by naming your module
'ImmString.String' (using a nested module in other words) and using
'open ImmString'.  However this has the further problem that you then
can no longer use mutable strings :-( although perhaps Oliver has
forsworn use of mutable strings, so this is an acceptable compromise
for him.

# module ImmString = struct
  module String = struct
  let get s i = prerr_endline "hello!"; 'c'   
  end
  end;;
module ImmString : sig module String : sig val get : 'a -> 'b -> char end end
# open ImmString;;
# "foo".[3];;
hello!
- : char = 'c'


Rich.

-- 
Richard Jones, CTO Merjis Ltd.
Merjis - web marketing and technology - http://merjis.com
Team Notepad - intranets and extranets for business - http://team-notepad.com


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Caml-list] immutable Strings?
  2005-03-14 12:57   ` Richard Jones
@ 2005-03-14 13:01     ` Richard Jones
  2005-03-15  2:13       ` Jacques Garrigue
  2005-03-15  8:40     ` Oliver Bandel
  1 sibling, 1 reply; 9+ messages in thread
From: Richard Jones @ 2005-03-14 13:01 UTC (permalink / raw)
  To: caml-list

On Mon, Mar 14, 2005 at 12:57:28PM +0000, rich wrote:
> You can get the s.[i] shortcut by naming your module
> 'ImmString.String' (using a nested module in other words) and using
> 'open ImmString'. [...]

(Replying to my own email ...)

This reminds me of another problem I was going to ask about.  Would it
be possible to have a syntax allowing you to always get to the "top"
of the module namespace.  It could be something like ".String.copy"
which would always refer to the real String module, not to any
submodule of an opened module.

Rich.

-- 
Richard Jones, CTO Merjis Ltd.
Merjis - web marketing and technology - http://merjis.com
Team Notepad - intranets and extranets for business - http://team-notepad.com


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Caml-list] immutable Strings?
  2005-03-14 13:01     ` Richard Jones
@ 2005-03-15  2:13       ` Jacques Garrigue
  2005-03-15  8:09         ` Oliver Bandel
  0 siblings, 1 reply; 9+ messages in thread
From: Jacques Garrigue @ 2005-03-15  2:13 UTC (permalink / raw)
  To: rich; +Cc: caml-list

From: Richard Jones <rich@annexia.org>
> On Mon, Mar 14, 2005 at 12:57:28PM +0000, rich wrote:
> > You can get the s.[i] shortcut by naming your module
> > 'ImmString.String' (using a nested module in other words) and using
> > 'open ImmString'. [...]
> 
> (Replying to my own email ...)
> 
> This reminds me of another problem I was going to ask about.  Would it
> be possible to have a syntax allowing you to always get to the "top"
> of the module namespace.  It could be something like ".String.copy"
> which would always refer to the real String module, not to any
> submodule of an opened module.

First remark, there is a workaround against name hiding through open:
you can bind your original module with another name before doing open.

  module OrigString = String
  open ImmString
  ...

Not to say that the feature you suggest would be useless: there are
cases where you want to avoid ambiguities. But the above syntax
wouldn't work: in many cases the leading dot would be confused for
a selection construct.
Except for the syntax, I believe the implementation would be easy:
the original environment is always kept around in the compiler.

If you have a better idea for the syntax, register a wish with the bug
report system...

Jacques Garrigue


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Caml-list] immutable Strings?
  2005-03-15  2:13       ` Jacques Garrigue
@ 2005-03-15  8:09         ` Oliver Bandel
  0 siblings, 0 replies; 9+ messages in thread
From: Oliver Bandel @ 2005-03-15  8:09 UTC (permalink / raw)
  To: caml-list

On Tue, Mar 15, 2005 at 11:13:39AM +0900, Jacques Garrigue wrote:
[...]
> 
> If you have a better idea for the syntax, register a wish with the bug
> report system...

It is possible to register wishes for Ocaml on the bug report system?

So when I want to have an "immutable" keyword for strings,
I can register that to the OCaml developpers?

If so... where to do that?


Ciao,
   Oliver


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Caml-list] immutable Strings?
  2005-03-14 12:57   ` Richard Jones
  2005-03-14 13:01     ` Richard Jones
@ 2005-03-15  8:40     ` Oliver Bandel
  1 sibling, 0 replies; 9+ messages in thread
From: Oliver Bandel @ 2005-03-15  8:40 UTC (permalink / raw)
  To: caml-list

On Mon, Mar 14, 2005 at 12:57:29PM +0000, Richard Jones wrote:
> On Mon, Mar 14, 2005 at 11:30:57AM +0100, Pascal Zimmer wrote:
> > - you cannot use the shortcuts s.[i] and (^) anymore (this gets the code 
> > less readable especially for the first one)
> 
> You can get the s.[i] shortcut by naming your module
> 'ImmString.String' (using a nested module in other words) and using
> 'open ImmString'.  However this has the further problem that you then
> can no longer use mutable strings :-( although perhaps Oliver has
> forsworn use of mutable strings, so this is an acceptable compromise
> for him.

forsworn mutable strings? ???

Mutable strings are ok for many tasks, but sometimes
immutable strings are better.

It's the same like wondering which style of programming is better for a task.
So, OCaml seems to be very open to different programming styles...
...why not to different kinds of strings?


At the time I have done it with String.copy and modyfing the copy.
But as soon as I has some time to look at the suggestions made here,
I may use them in a reimplementation of my module.
Thanks to *.mli this will not affect the user of the module. :)

Ciao,
   Oliver


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2005-03-15  8:40 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-03-12 20:50 immutable Strings? Oliver Bandel
2005-03-14 10:30 ` [Caml-list] " Pascal Zimmer
2005-03-14 10:41   ` Jon Harrop
2005-03-14 11:15     ` Pascal Zimmer
2005-03-14 12:57   ` Richard Jones
2005-03-14 13:01     ` Richard Jones
2005-03-15  2:13       ` Jacques Garrigue
2005-03-15  8:09         ` Oliver Bandel
2005-03-15  8:40     ` Oliver Bandel

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).