Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
* operational semantics of = for objects
@ 1998-03-26 17:32 Sorin Stratulat
  1998-03-27 12:14 ` Jacques GARRIGUE
  1998-03-27 14:33 ` Jerome Vouillon
  0 siblings, 2 replies; 3+ messages in thread
From: Sorin Stratulat @ 1998-03-26 17:32 UTC (permalink / raw)
  To: caml-list

Hello !

After comparing the results of the code (see below) executed on
different versions of OCAML (1.05 and 1.07), I would like to know if
anybody can explain me how the equality operator works for objects (if
it is possible in both versions). As I understood from a previous
message, overloading the operators is not supported yet.

Here you have two examples where I mainly created a class and some
instances that are further compared:

example 1 ------------
        Objective Caml version 1.07

# class point x_init =
  val mutable x = x_init
  method get_x = x
  	method move d = x <- x + d
  end;;
class point (int) =
  val mutable x : int
  method get_x : int
  method move : int -> unit
end
# let p = new point 7;;
val p : point = <obj>
# let q = new point 7;;
val q : point = <obj>
# p=q;;
- : bool = false
# List.mem (new point 7) [p;q];;
- : bool = false
#  let r = p;;
val r : point = <obj>
# p=r;;
- : bool = true
# 

example 2 -----------------
	Objective Caml version 1.05

# class point x_init =
  val mutable x = x_init
  method get_x = x
  	method move d = x <- x + d
  end;;
        class point (int) =
  val mutable x : int
  method get_x : int
  method move : int -> unit
end
# let p = new point 7;;
val p : point = <obj>
# let q = new point 7;;
val q : point = <obj>
# p=q;;
- : bool = true
#  List.mem (new point 7) [p;q];;
- : bool = true
# let r = p;;
val r : point = <obj>
# p = r;;
- : bool = true
# 

Thank you,

Sorin


-- 
e-mail : Sorin.Stratulat@loria.fr
Project: PROTHEO
Office : A201                             | Home:
------                                    | ------
tel:  03.83.59.30.02                      | C.U. du Placieux, ch. 1128
LORIA - BP 239                            | Bd. Mare'chal Lyautey
F-54506 VANDOEUVRE-les-NANCY Cedex FRANCE | 54600 Villers-le`s-Nancy





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

* Re: operational semantics of = for objects
  1998-03-26 17:32 operational semantics of = for objects Sorin Stratulat
@ 1998-03-27 12:14 ` Jacques GARRIGUE
  1998-03-27 14:33 ` Jerome Vouillon
  1 sibling, 0 replies; 3+ messages in thread
From: Jacques GARRIGUE @ 1998-03-27 12:14 UTC (permalink / raw)
  To: Sorin.Stratulat; +Cc: caml-list

From: Sorin Stratulat <Sorin.Stratulat@loria.fr>

> After comparing the results of the code (see below) executed on
> different versions of OCAML (1.05 and 1.07), I would like to know if
> anybody can explain me how the equality operator works for objects (if
> it is possible in both versions). As I understood from a previous
> message, overloading the operators is not supported yet.

The fastest way to understand what has changed is to have a look at
oo.ml in the standard library. In 1.07, all objects have an oid,
unique to them, like in many object-oriented languages, and as a
result structural equality (=) will always give them different.

That is

	let o1 = new c ()
	and o2 = new c ()
	in o1 = o2;;
	- : bool = false

	let o1 = new c () in
	let o2 = Oo.copy o1 in
	o1 = o2;;
	- : bool = false

	let o1 = new c () in
	let o2 = o1 in
	o1 = o2;;
	- : bool = true

What you must understand is that the last example does not define two
objects but only two names for the same object, and this is why they
are equal.

In 1.05 there was no oid (I suppose), so two objects would be equal
when their instance variables are equal. This may look nice, but there
are several drawbacks:
  * if there are functions stored in instance variables, trying to
    compare two objects would raise an exception.
  * even if instance variables are equal, there are still ways to
    distinguish two objects (particularly if they have mutable
    instance variables), so you cannot consider them as rigorously
    equal.


	Jacques

---------------------------------------------------------------------------
Jacques Garrigue      Kyoto University     garrigue at kurims.kyoto-u.ac.jp
		<A HREF=http://wwwfun.kurims.kyoto-u.ac.jp/~garrigue/>JG</A>






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

* Re: operational semantics of = for objects
  1998-03-26 17:32 operational semantics of = for objects Sorin Stratulat
  1998-03-27 12:14 ` Jacques GARRIGUE
@ 1998-03-27 14:33 ` Jerome Vouillon
  1 sibling, 0 replies; 3+ messages in thread
From: Jerome Vouillon @ 1998-03-27 14:33 UTC (permalink / raw)
  To: Sorin Stratulat; +Cc: caml-list


On Thu, 26 Mar 1998, Sorin Stratulat wrote:

> After comparing the results of the code (see below) executed on
> different versions of OCAML (1.05 and 1.07), I would like to know if
> anybody can explain me how the equality operator works for objects (if
> it is possible in both versions). As I understood from a previous
> message, overloading the operators is not supported yet.

In Ocaml 1.05, the equality operator did not work reliably for
objects. Indeed, objects was not treated specially. So, the operator
structurally compared the structures of the two objects and in some
cases tried to compare two methods and as a consequence failed.

In Ocaml 1.07, two objects are equals iff they are physically equals.

-- Jerome Vouillon





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

end of thread, other threads:[~1998-03-27 15:34 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1998-03-26 17:32 operational semantics of = for objects Sorin Stratulat
1998-03-27 12:14 ` Jacques GARRIGUE
1998-03-27 14:33 ` Jerome Vouillon

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox