caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] On the equality of functional values
@ 2002-10-27 14:10 Alessandro Baretta
  2002-10-27 15:34 ` David Brown
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Alessandro Baretta @ 2002-10-27 14:10 UTC (permalink / raw)
  To: Ocaml

I have been experimenting with equal signs, and I noticed 
that the equality operator (=) behaves in a strange way with 
respect to functional values.

         Objective Caml version 3.06

# (=) = (=) ;;
Exception: Invalid_argument "equal: functional value".
# let x = (=) in x = x ;;
- : bool = true

The first line seems to imply that no comparisons are 
possible between functional values. However, the second line 
I typed does not raise the same exception. This seems to 
imply that comparisons are allowed between functional 
values, too.

Now, which of two hypotheses is correct?

Alex

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

* Re: [Caml-list] On the equality of functional values
  2002-10-27 14:10 [Caml-list] On the equality of functional values Alessandro Baretta
@ 2002-10-27 15:34 ` David Brown
  2002-10-27 17:58 ` David Monniaux
  2002-10-27 18:02 ` Remi VANICAT
  2 siblings, 0 replies; 7+ messages in thread
From: David Brown @ 2002-10-27 15:34 UTC (permalink / raw)
  To: Alessandro Baretta; +Cc: Ocaml

On Sun, Oct 27, 2002 at 03:10:59PM +0100, Alessandro Baretta wrote:

> # (=) = (=) ;;
> Exception: Invalid_argument "equal: functional value".
> # let x = (=) in x = x ;;
> - : bool = true

# let x = (=) and y = (=) in x = y;;
Exception: Invalid_argument "equal: functional value".

I suspect that the x = x is getting handled specially.

Dave Brown
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

* Re: [Caml-list] On the equality of functional values
  2002-10-27 14:10 [Caml-list] On the equality of functional values Alessandro Baretta
  2002-10-27 15:34 ` David Brown
@ 2002-10-27 17:58 ` David Monniaux
  2002-10-28  6:15   ` Alessandro Baretta
  2002-10-27 18:02 ` Remi VANICAT
  2 siblings, 1 reply; 7+ messages in thread
From: David Monniaux @ 2002-10-27 17:58 UTC (permalink / raw)
  To: Alessandro Baretta; +Cc: Ocaml

On Sun, 27 Oct 2002, Alessandro Baretta wrote:

> I typed does not raise the same exception. This seems to 
> imply that comparisons are allowed between functional 
> values, too.

If I remember well, = is implemented by doing first a == (equality of
pointers) test, and returning true if that test succeeds; otherwise,
the more complex recursive equality test is performed, which fails with
an exception if it encounters functional values.

So if x and y are the same closure, x=y succeeds and yields the true
value. Otherwise, it throws an exception.


David Monniaux            http://www.di.ens.fr/~monniaux
Laboratoire d'informatique de l'École Normale Supérieure,
Paris, France

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

* Re: [Caml-list] On the equality of functional values
  2002-10-27 14:10 [Caml-list] On the equality of functional values Alessandro Baretta
  2002-10-27 15:34 ` David Brown
  2002-10-27 17:58 ` David Monniaux
@ 2002-10-27 18:02 ` Remi VANICAT
  2 siblings, 0 replies; 7+ messages in thread
From: Remi VANICAT @ 2002-10-27 18:02 UTC (permalink / raw)
  To: caml-list

Alessandro Baretta <alex@baretta.com> writes:

> I have been experimenting with equal signs, and I noticed that the
> equality operator (=) behaves in a strange way with respect to
> functional values.
>
>          Objective Caml version 3.06
>
> # (=) = (=) ;;
> Exception: Invalid_argument "equal: functional value".
> # let x = (=) in x = x ;;
> - : bool = true
>
> The first line seems to imply that no comparisons are possible between
> functional values. However, the second line I typed does not raise the
> same exception. This seems to imply that comparisons are allowed
> between functional values, too.
>
> Now, which of two hypotheses is correct?

The first one. But the fact is that before making a structural
equality test, ocaml try to see if both argument have the same address,
and then return true, so equality may work if both argument are
exactly the same closure. (then, there is a subtlety : the = function
is a c function, not a caml one. So when one refer to (=), caml build
a new fresh closure, that is different from any other closure).
(and there is another subtlety, making executable generated by ocaml
and by ocamlopt having some subtle difference in this case).

-- 
Rémi Vanicat
vanicat@labri.u-bordeaux.fr
http://dept-info.labri.u-bordeaux.fr/~vanicat
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

* Re: [Caml-list] On the equality of functional values
  2002-10-27 17:58 ` David Monniaux
@ 2002-10-28  6:15   ` Alessandro Baretta
  2002-10-28  8:34     ` jeanmarc.eber
  2002-10-29 10:32     ` Pierre Weis
  0 siblings, 2 replies; 7+ messages in thread
From: Alessandro Baretta @ 2002-10-28  6:15 UTC (permalink / raw)
  To: ocaml



David Monniaux wrote:
> On Sun, 27 Oct 2002, Alessandro Baretta wrote:
> 
> 
> If I remember well, = is implemented by doing first a == (equality of
> pointers) test, and returning true if that test succeeds; otherwise,
> the more complex recursive equality test is performed, which fails with
> an exception if it encounters functional values.
> 
> So if x and y are the same closure, x=y succeeds and yields the true
> value. Otherwise, it throws an exception.

This is what I suspected. I wonder if this behavior conforms 
to specification, or if it is an incidental concequence of 
the implementation of (=).

Alex

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

* Re: [Caml-list] On the equality of functional values
  2002-10-28  6:15   ` Alessandro Baretta
@ 2002-10-28  8:34     ` jeanmarc.eber
  2002-10-29 10:32     ` Pierre Weis
  1 sibling, 0 replies; 7+ messages in thread
From: jeanmarc.eber @ 2002-10-28  8:34 UTC (permalink / raw)
  To: Alessandro Baretta; +Cc: ocaml

documentation from pervasives.mli (3.06): 
 
Look, in particular, at the use of the word "may": 
 
external ( = ) : 'a -> 'a -> bool = "%equal" 
(** [e1 = e2] tests for structural equality of [e1] and [e2]. 
   Mutable structures (e.g., references and arrays) are equal 
   if and only if their current contents are structurally equal, 
   even if the two mutable objects are not the same physical object. 
   Equality between functional values may raise [Invalid_argument]. 
   Equality between cyclic data structures may not terminate. *) 
 
Jean-Marc Eber 
 
 
Quoting Alessandro Baretta <alex@baretta.com>: 
 
>  
>  
> David Monniaux wrote: 
> > On Sun, 27 Oct 2002, Alessandro Baretta wrote: 
> >  
> >  
> > If I remember well, = is implemented by doing first a == (equality of 
> > pointers) test, and returning true if that test succeeds; otherwise, 
> > the more complex recursive equality test is performed, which fails 
> with 
> > an exception if it encounters functional values. 
> >  
> > So if x and y are the same closure, x=y succeeds and yields the true 
> > value. Otherwise, it throws an exception. 
>  
> This is what I suspected. I wonder if this behavior conforms  
> to specification, or if it is an incidental concequence of  
> the implementation of (=). 
>  
> Alex 
>  
> ------------------- 
> To unsubscribe, mail caml-list-request@inria.fr Archives: 
> http://caml.inria.fr 
> Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: 
> http://caml.inria.fr/FAQ/ 
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners 
>  
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

* Re: [Caml-list] On the equality of functional values
  2002-10-28  6:15   ` Alessandro Baretta
  2002-10-28  8:34     ` jeanmarc.eber
@ 2002-10-29 10:32     ` Pierre Weis
  1 sibling, 0 replies; 7+ messages in thread
From: Pierre Weis @ 2002-10-29 10:32 UTC (permalink / raw)
  To: Alessandro Baretta; +Cc: caml-list

> David Monniaux wrote:
> > On Sun, 27 Oct 2002, Alessandro Baretta wrote:
> > 
> > 
> > If I remember well, = is implemented by doing first a == (equality of
> > pointers) test, and returning true if that test succeeds; otherwise,
> > the more complex recursive equality test is performed, which fails with
> > an exception if it encounters functional values.
> > 
> > So if x and y are the same closure, x=y succeeds and yields the true
> > value. Otherwise, it throws an exception.
> 
> This is what I suspected. I wonder if this behavior conforms 
> to specification, or if it is an incidental concequence of 
> the implementation of (=).
> 
> Alex

This is in the spec of = and ==.

This is explained in large in the FAQ:

-- What's the difference between = and == ?
   http://pauillac.inria.fr/caml/FAQ/FAQ_EXPERT-eng.html#egalite
-- How to compare functional values ?
   (Invalid_argument "equal: functional value") ?
   http://pauillac.inria.fr/caml/FAQ/FAQ_EXPERT-eng.html#egalitefonctionnelle

Best regards,

Pierre Weis

INRIA, Projet Cristal, Pierre.Weis@inria.fr, http://pauillac.inria.fr/~weis/


-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

end of thread, other threads:[~2002-10-29 10:32 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-10-27 14:10 [Caml-list] On the equality of functional values Alessandro Baretta
2002-10-27 15:34 ` David Brown
2002-10-27 17:58 ` David Monniaux
2002-10-28  6:15   ` Alessandro Baretta
2002-10-28  8:34     ` jeanmarc.eber
2002-10-29 10:32     ` Pierre Weis
2002-10-27 18:02 ` Remi VANICAT

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