caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Gabriel Scherer <gabriel.scherer@gmail.com>
To: Thomas Fischbacher <t.fischbacher@soton.ac.uk>
Cc: OCaML Mailing List <caml-list@inria.fr>
Subject: Re: [Caml-list] Weird GC behaviour
Date: Wed, 28 Sep 2011 14:12:45 +0200	[thread overview]
Message-ID: <CAPFanBGJJaYH51z8c9Lfg11QXJOt1dqJmyJ=2G7T2cCNzbHMWw@mail.gmail.com> (raw)
In-Reply-To: <4E830AC3.6020405@soton.ac.uk>

On Wed, Sep 28, 2011 at 1:53 PM, Thomas Fischbacher
<t.fischbacher@soton.ac.uk> 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
> t.fischbacher@soton.ac.uk
>
> --
> 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	other threads:[~2011-09-28 12:13 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-09-27 17:30 Thomas Fischbacher
2011-09-28 11:19 ` Damien Doligez
2011-09-28 11:53   ` Thomas Fischbacher
2011-09-28 12:12     ` Gabriel Scherer [this message]
2011-09-28 13:07     ` John Carr
2011-09-28 15:30     ` Damien Doligez
2011-09-28 23:32       ` Philippe Wang

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='CAPFanBGJJaYH51z8c9Lfg11QXJOt1dqJmyJ=2G7T2cCNzbHMWw@mail.gmail.com' \
    --to=gabriel.scherer@gmail.com \
    --cc=caml-list@inria.fr \
    --cc=t.fischbacher@soton.ac.uk \
    /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).