On Wed, Sep 28, 2011 at 1:53 PM, Thomas Fischbacher
<[email protected]> wrote:
> How come (Ftag "funny") is regarded as constant while
> (Rtag (ref "funny")) is not? After all, strings are mutable in OCaml,
> so there really is not that much of a conceptual difference between a
> string and a string ref in that respect:

Of course references are not constant, and calls to `ref` allocate
each time they're evaluated. Doing otherwise would completely break
the reference semantics.

Your remark that strings are also mutable is pertinent : indeed, in
this case, the semantics of mutable strings is broken and lead to
weird things. The canonical example -- very close to your lisp example
-- is:

  # let should_be_fresh () = "toto";;
  val should_be_fresh : unit -> string = <fun>
  # let t1 = should_be_fresh ();;
  val t1 : string = "toto"
  # t1.[1] <- 'u';;
  - : unit = ()
  # should_be_fresh();;
  - : string = "tuto"

The reason why string are still optimized as if they were immutable is
that they are generally not mutated in practice, and that this
constant optimization may save quite a lot of memory. I believe there
is a consensus that making strings mutable is an historical mistake
that should not be repeated, where you to design a programming
language in the present times.

If you want to force a string to be freshly allocated, an easy way is
to concatenate the string literal with the empty string: "toto" ^ "".
I believe (Ftag ("hello"^"")) would suit your purposes here.

>
> http://caml.inria.fr/pub/docs/manual-ocaml/manual010.html#toc44
>
> # type funny_str = Ftag of string;;
> type funny_str = Ftag of string
> # let s1 = Ftag "Hello";;
> val s1 : funny_str = Ftag "Hello"
> # let Ftag s = s1 in s.[0]<-'h';;
> - : unit = ()
> # s1;;
> - : funny_str = Ftag "hello"
>
> The way I read the spec, it nowhere says that variant values that use a
> non-constant constructor plus a value can be treated as constant. I do
> see that in a sense, this may be a similar issue as the one that would
> arise with lisp code such as this:
>
> (defun example ()
>  (let ((text-segment-list '(1 2 3 4 5)))
>    (nreverse text-segment-list)))
>
> Calling (example) twice gives weird behaviour, as we are destructively
> modifying a lisp that conceptually was constant. So, one should have
> used:
>
> (defun example2 ()
>  (let ((text-segment-list (list 1 2 3 4 5)))
>    (nreverse text-segment-list)))
>
> But the problem I think I have with OCaml is: there just seems to be no
> way to properly express the conceptual difference between '(1 2 3 4 5)
> and (list 1 2 3 4 5): All I can say above is: Ftag "Hello".
>
> --
> best regards,
> Thomas Fischbacher
> [email protected]
>
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa-roc.inria.fr/wws/info/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>
>


-- 
Caml-list mailing list.  Subscription management and archives:
https://sympa-roc.inria.fr/wws/info/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs

Reply via email to