caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Thomas Fischbacher <t.fischbacher@soton.ac.uk>
To: Damien Doligez <damien.doligez@inria.fr>
Cc: OCaML Mailing List <caml-list@inria.fr>
Subject: Re: [Caml-list] Weird GC behaviour
Date: Wed, 28 Sep 2011 12:53:39 +0100	[thread overview]
Message-ID: <4E830AC3.6020405@soton.ac.uk> (raw)
In-Reply-To: <2F6B5C48-88B0-4717-87FD-BD044B3D36E4@inria.fr>


Dear Damien,

>>  if works
>>    then (get_ftag "funny",get_rtag "funny")
>>    else (Ftag "funny", Rtag (ref "funny"))
>>  in
> 
> In this expression, you have four interesting subexpressions.  Three of
> them involve calling some function with "funny" as argument, and these
> functions allocate values that point to the "funny" string.  These values
> are heap-allocated and behave as you expect.
> 
> The fourth one is (Ftag "funny").  This is a constant expression and its
> evaluation does not allocate in the heap.  Since it's constant, it's
> allocated once by the compiler, and every time you execute this code
> it's the same value that is returned.

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:

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

  reply	other threads:[~2011-09-28 11:54 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 [this message]
2011-09-28 12:12     ` Gabriel Scherer
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=4E830AC3.6020405@soton.ac.uk \
    --to=t.fischbacher@soton.ac.uk \
    --cc=caml-list@inria.fr \
    --cc=damien.doligez@inria.fr \
    /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).