caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* equality operators in OCaml
@ 2008-07-24 19:27 Raj Bandyopadhyay
  2008-07-24 22:15 ` [Caml-list] " Vincent Hanquez
  2008-07-24 22:32 ` Jon Harrop
  0 siblings, 2 replies; 8+ messages in thread
From: Raj Bandyopadhyay @ 2008-07-24 19:27 UTC (permalink / raw)
  To: caml-list

Hi OCaml folk

I apologize if I've been asking too many questions on this list 
recently, but I'm working on a heavy OCaml application and need help 
sometimes.

I am having a disagreement with a colleague about how the equality 
operators in OCaml work and am trying to resolve it conclusively.

1) I understand that the '==' operator is basically a pointer comparison 
i.e. a==b true  iff a and b are the same entity. Is this true?

2) Where can I find the code implementing the '==' operator in the OCaml 
code?

3) In case I do want to check object identity and can use the == 
operator, would it be faster to use '==', '=' or a match statement?

Thanks
Raj


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

* Re: [Caml-list] equality operators in OCaml
  2008-07-24 19:27 equality operators in OCaml Raj Bandyopadhyay
@ 2008-07-24 22:15 ` Vincent Hanquez
  2008-07-24 22:32 ` Jon Harrop
  1 sibling, 0 replies; 8+ messages in thread
From: Vincent Hanquez @ 2008-07-24 22:15 UTC (permalink / raw)
  To: Raj Bandyopadhyay; +Cc: caml-list

On Thu, Jul 24, 2008 at 02:27:51PM -0500, Raj Bandyopadhyay wrote:
> Hi OCaml folk
>
> I apologize if I've been asking too many questions on this list  
> recently, but I'm working on a heavy OCaml application and need help  
> sometimes.
>
> I am having a disagreement with a colleague about how the equality  
> operators in OCaml work and am trying to resolve it conclusively.
>
> 1) I understand that the '==' operator is basically a pointer comparison  
> i.e. a==b true  iff a and b are the same entity. Is this true?

yes. only valid for object that are in blocks though (int, char are not).
for object not in block, you end up with a simple =

> 2) Where can I find the code implementing the '==' operator in the OCaml  
> code?

byterun/compare.c : caml_equal which call compare_val with total=0 (which
means to not compare inside blocks)

> 3) In case I do want to check object identity and can use the ==  
> operator, would it be faster to use '==', '=' or a match statement?

i believe == is the fastest one. but that's just based on how the thing
works compare to the others compare, not on empirical benchs.

-- 
Vincent


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

* Re: [Caml-list] equality operators in OCaml
  2008-07-24 19:27 equality operators in OCaml Raj Bandyopadhyay
  2008-07-24 22:15 ` [Caml-list] " Vincent Hanquez
@ 2008-07-24 22:32 ` Jon Harrop
  2008-07-25  1:40   ` Peng Zang
  1 sibling, 1 reply; 8+ messages in thread
From: Jon Harrop @ 2008-07-24 22:32 UTC (permalink / raw)
  To: Raj Bandyopadhyay, caml-list

From: "Raj Bandyopadhyay" <rajb@rice.edu>
> Hi OCaml folk
>
> I apologize if I've been asking too many questions on this list recently, 
> but I'm working on a heavy OCaml application and need help sometimes.
>
> I am having a disagreement with a colleague about how the equality 
> operators in OCaml work and am trying to resolve it conclusively.
>
> 1) I understand that the '==' operator is basically a pointer comparison 
> i.e. a==b true  iff a and b are the same entity. Is this true?

There are some complications here. Firstly, NaN has multiple representations 
that may or may not be physically equal but is always structurally unequal 
to itself. Secondly, OCaml's implicit allocation can make "same entity" 
unclear, constant subexpressions (e.g. Some 3) may or may not be hoisted.

The only place I ever use == and != is when optimizing code in such a way 
that unexpected results from those functions result in slow but still 
correct code.

> 2) Where can I find the code implementing the '==' operator in the OCaml 
> code?

Can you just compile it and look at the assembler? There are probably many 
different outputs depending upon the statically resolved types.

> 3) In case I do want to check object identity and can use the == operator, 
> would it be faster to use '==', '=' or a match statement?

I believe the == operator would be fastest. More often that not, you want < 
and > so you end up augmenting your OCaml data structures with unique IDs 
anyway.

Note that physical equality does not even exist in SML and the designers of 
SML stand by that design decision. Perhaps it is best to avoid == in OCaml 
altogether.

Cheers,
Jon.


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

* Re: [Caml-list] equality operators in OCaml
  2008-07-24 22:32 ` Jon Harrop
@ 2008-07-25  1:40   ` Peng Zang
  2008-07-25 10:34     ` Christophe TROESTLER
  2008-07-25 20:14     ` Matthew William Cox
  0 siblings, 2 replies; 8+ messages in thread
From: Peng Zang @ 2008-07-25  1:40 UTC (permalink / raw)
  To: caml-list; +Cc: Jon Harrop, Raj Bandyopadhyay

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


On Thursday 24 July 2008 06:32:36 pm Jon Harrop wrote:
> From: "Raj Bandyopadhyay" <rajb@rice.edu>
>
> > Hi OCaml folk
> >
> > I apologize if I've been asking too many questions on this list recently,
> > but I'm working on a heavy OCaml application and need help sometimes.
> >
> > I am having a disagreement with a colleague about how the equality
> > operators in OCaml work and am trying to resolve it conclusively.
> >
> > 1) I understand that the '==' operator is basically a pointer comparison
> > i.e. a==b true  iff a and b are the same entity. Is this true?
>
> There are some complications here. Firstly, NaN has multiple
> representations that may or may not be physically equal but is always
> structurally unequal to itself.


Yeah, that always seemed broken to me.  If two things are physically equal 
(they occupy the same memory space) it doesn't make sense for them to be 
structurally unequal (contain different content).  Personally, one of the 
first things I did is redefined (=) to fix this.

Peng
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.7 (GNU/Linux)

iD8DBQFIiS8YfIRcEFL/JewRAnXqAKCAhHWGTHe6z58jRplmMmnNFgljUACfUhaJ
SQSWrxxpPpHqiJcFBcNlVsM=
=af4P
-----END PGP SIGNATURE-----


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

* Re: [Caml-list] equality operators in OCaml
  2008-07-25  1:40   ` Peng Zang
@ 2008-07-25 10:34     ` Christophe TROESTLER
  2008-07-25 12:04       ` Peng Zang
  2008-07-25 20:14     ` Matthew William Cox
  1 sibling, 1 reply; 8+ messages in thread
From: Christophe TROESTLER @ 2008-07-25 10:34 UTC (permalink / raw)
  To: peng.zang; +Cc: caml-list, rajb

On Thu, 24 Jul 2008 21:40:36 -0400, Peng Zang wrote:
> 
> On Thursday 24 July 2008 06:32:36 pm Jon Harrop wrote:
> > There are some complications here. Firstly, NaN has multiple
> > representations that may or may not be physically equal but is always
> > structurally unequal to itself.
> 
> Yeah, that always seemed broken to me.  If two things are physically equal 
> (they occupy the same memory space) it doesn't make sense for them to be 
> structurally unequal (contain different content).  Personally, one of the 
> first things I did is redefined (=) to fix this.

Humm...  http://citeseer.ist.psu.edu/goldberg91what.html is certainly
a good read...

My 0.02€,
C.


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

* Re: [Caml-list] equality operators in OCaml
  2008-07-25 10:34     ` Christophe TROESTLER
@ 2008-07-25 12:04       ` Peng Zang
  0 siblings, 0 replies; 8+ messages in thread
From: Peng Zang @ 2008-07-25 12:04 UTC (permalink / raw)
  To: Christophe TROESTLER; +Cc: caml-list, rajb

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


On Friday 25 July 2008 06:34:54 am Christophe TROESTLER wrote:
> On Thu, 24 Jul 2008 21:40:36 -0400, Peng Zang wrote:
> > On Thursday 24 July 2008 06:32:36 pm Jon Harrop wrote:
> > > There are some complications here. Firstly, NaN has multiple
> > > representations that may or may not be physically equal but is always
> > > structurally unequal to itself.
> >
> > Yeah, that always seemed broken to me.  If two things are physically
> > equal (they occupy the same memory space) it doesn't make sense for them
> > to be structurally unequal (contain different content).  Personally, one
> > of the first things I did is redefined (=) to fix this.
>
> Humm...  http://citeseer.ist.psu.edu/goldberg91what.html is certainly
> a good read...
>
> My 0.02€,
> C.

I second that.  Particularly relevant is how there is not one unique NaN but a 
family of them.  OCaml has three treatments for them:

1) (=) treats all NaNs as different (even if they may in fact, at the bit 
level, be the same)

2) (compare) treats all NaNs as the same (even if they may in fact, at the bit 
level be different)

3) (==) two NaNs are equal iff they share the same memory

(2) tends to come up the most often as you typically don't care what kind of 
NaN it is, as long as it is one.

Peng
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.7 (GNU/Linux)

iD8DBQFIicFAfIRcEFL/JewRAiFkAJ46YFay72ONIDD7qk75/izt9ne8gACgvMzu
EYZkQb7fYAlEU+KXnixp7to=
=5iAB
-----END PGP SIGNATURE-----


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

* Re: [Caml-list] equality operators in OCaml
  2008-07-25  1:40   ` Peng Zang
  2008-07-25 10:34     ` Christophe TROESTLER
@ 2008-07-25 20:14     ` Matthew William Cox
  2008-07-25 22:27       ` Peng Zang
  1 sibling, 1 reply; 8+ messages in thread
From: Matthew William Cox @ 2008-07-25 20:14 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 609 bytes --]

On Thu, Jul 24, 2008 at 09:40:36PM -0400, Peng Zang wrote:
> Yeah, that always seemed broken to me.  If two things are physically equal 
> (they occupy the same memory space) it doesn't make sense for them to be 
> structurally unequal (contain different content).  Personally, one of the 
> first things I did is redefined (=) to fix this.

It's not broken at all, but complient with a common and longstanding
idiom (at least amoung scientists working with numerical codes.) The way
we test for NaN is by comparing to itself, eg:

    if x = x then (* x is a number *) else (* x is NaN *)

Matt

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 197 bytes --]

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

* Re: [Caml-list] equality operators in OCaml
  2008-07-25 20:14     ` Matthew William Cox
@ 2008-07-25 22:27       ` Peng Zang
  0 siblings, 0 replies; 8+ messages in thread
From: Peng Zang @ 2008-07-25 22:27 UTC (permalink / raw)
  To: caml-list; +Cc: Matthew William Cox

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


On Friday 25 July 2008 04:14:22 pm Matthew William Cox wrote:
> On Thu, Jul 24, 2008 at 09:40:36PM -0400, Peng Zang wrote:
> > Yeah, that always seemed broken to me.  If two things are physically
> > equal (they occupy the same memory space) it doesn't make sense for them
> > to be structurally unequal (contain different content).  Personally, one
> > of the first things I did is redefined (=) to fix this.
>
> It's not broken at all, but complient with a common and longstanding
> idiom (at least amoung scientists working with numerical codes.) The way
> we test for NaN is by comparing to itself, eg:
>
>     if x = x then (* x is a number *) else (* x is NaN *)
>
> Matt

Yeah, I just learned that today.  But these days, most languages have 
functions for testing for NaN.  OCaml has classify_float for example.  Just 
FYI

Peng
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.7 (GNU/Linux)

iD8DBQFIilNnfIRcEFL/JewRAiRjAJ9jRQy37/JtI+nZ9fhiInp3HOvKWgCgtmIv
k2koED9DunAM73PmaEPhnbg=
=SaDz
-----END PGP SIGNATURE-----


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

end of thread, other threads:[~2008-07-25 22:27 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-07-24 19:27 equality operators in OCaml Raj Bandyopadhyay
2008-07-24 22:15 ` [Caml-list] " Vincent Hanquez
2008-07-24 22:32 ` Jon Harrop
2008-07-25  1:40   ` Peng Zang
2008-07-25 10:34     ` Christophe TROESTLER
2008-07-25 12:04       ` Peng Zang
2008-07-25 20:14     ` Matthew William Cox
2008-07-25 22:27       ` Peng Zang

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