caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Newbie question: semantics of 'mutable' in relation to deep / shallow copying of CAML values.
@ 2001-11-22 12:25 Gerard Murphy
  2001-11-22 13:11 ` Daniel de Rauglaudre
  2001-11-22 14:23 ` Alain Frisch
  0 siblings, 2 replies; 3+ messages in thread
From: Gerard Murphy @ 2001-11-22 12:25 UTC (permalink / raw)
  To: caml-list

Hello,

As a *novice* CAML programmer, I've observed the following behaviour using
the OCaml interpreter:-

#        type example = { mutable label: string; mutable number: int };;

type example = { mutable label: string; mutable number: int };;


#       let value1 = { label = "Hello"; number = 0 };;
val value1 : example = {label="Hello"; number=0}

#       let value2 = value1;;
val value2 : example = {label="Hello"; number=0}

#       value1.label <- "I have been mutated!";;
- : unit = ()


#       value2;;
val value2 : example = {label="I have been mutated!"; number=0}

Surprise! 'value2' is sharing state with 'value1', but neither 'value1' nor
'value2' are
CAML references.

OK, in one sense I expect this behaviour - as long as your dealing with the
applicative part of the
language, it should make no difference as to whether a value is actually
copied, or whether it is
simply referred to in two different places. In terms of the low-level
representation, the value is either
literally copied around or a pointer to the value is copied and the value
garbage collected in some manner.

(Looking at the notes on the CAML to C interface, I believe integers are
handled by the former
approach, everything else by the latter.)

So, it seems reasonable in terms of the implementation that if one copy of
the value is mutated, the
original will change too.

My question is: is this part of the expected semantics? I've seen no mention
of this in the OCaml
manual or FAQ. If it is part of the language definition, I presume that an
implementation is safe
to assert that record values with mutable fields have to be copied 'by
reference' as opposed to 'by value'?

Thanks in advance,

Gerard Murphy









-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


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

* Re: [Caml-list] Newbie question: semantics of 'mutable' in relation to deep / shallow copying of CAML values.
  2001-11-22 12:25 [Caml-list] Newbie question: semantics of 'mutable' in relation to deep / shallow copying of CAML values Gerard Murphy
@ 2001-11-22 13:11 ` Daniel de Rauglaudre
  2001-11-22 14:23 ` Alain Frisch
  1 sibling, 0 replies; 3+ messages in thread
From: Daniel de Rauglaudre @ 2001-11-22 13:11 UTC (permalink / raw)
  To: caml-list

On Thu, Nov 22, 2001 at 12:25:04PM -0000, Gerard Murphy wrote:

> Surprise! 'value2' is sharing state with 'value1', but neither
> 'value1' nor 'value2' are CAML references.

There is no specific notion of "reference" in OCaml: the type "ref" is
actually implemented as a mutable record:
   type 'a ref = { mutable contents : 'a }

 !x is just a shortcut for x.contents
  x := y is just a shortcut for x.contents <- y

-- 
Daniel de RAUGLAUDRE
daniel.de_rauglaudre@inria.fr
http://cristal.inria.fr/~ddr/
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


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

* Re: [Caml-list] Newbie question: semantics of 'mutable' in relation to deep / shallow copying of CAML values.
  2001-11-22 12:25 [Caml-list] Newbie question: semantics of 'mutable' in relation to deep / shallow copying of CAML values Gerard Murphy
  2001-11-22 13:11 ` Daniel de Rauglaudre
@ 2001-11-22 14:23 ` Alain Frisch
  1 sibling, 0 replies; 3+ messages in thread
From: Alain Frisch @ 2001-11-22 14:23 UTC (permalink / raw)
  To: Gerard Murphy; +Cc: caml-list

On Thu, 22 Nov 2001, Gerard Murphy wrote:

> #       let value2 = value1;;

This is just a binding, no copy is involved: value1 and value2
simply denotes the same value. There are several methods to
create shallow copies of values:
- for objects: use Oo.copy or the {< >} notation
- for records: use the { r with ...} notation (you have to make
  at least one field explicit !)
- for everything except objets, you can also use Obj.dup, but
  you are not encouraged to do so !

To create deep copies, the only generic method, AFAIK, it to
marshal/unmarshal values:

let deep (x : 'a) : 'a = Marshal.from_string (Marshal.to_string x [Marshal.Closures]) 0;;

(add Marshal.No_sharing to the list if you don't want to keep sharing
inside the value)

I never felt the need for such an operation in OCaml, though.

-- 
  Alain

-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


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

end of thread, other threads:[~2001-11-22 14:23 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-11-22 12:25 [Caml-list] Newbie question: semantics of 'mutable' in relation to deep / shallow copying of CAML values Gerard Murphy
2001-11-22 13:11 ` Daniel de Rauglaudre
2001-11-22 14:23 ` Alain Frisch

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