Re: [Caml-list] equality operators in OCaml

2008-07-25 Thread Peng Zang
-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-

___
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs


Re: [Caml-list] equality operators in OCaml

2008-07-25 Thread Matthew William Cox
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


signature.asc
Description: Digital signature
___
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs


Re: [Caml-list] equality operators in OCaml

2008-07-25 Thread Peng Zang
-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-

___
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs


Re: [Caml-list] equality operators in OCaml

2008-07-25 Thread Christophe TROESTLER
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.

___
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs


Re: [Caml-list] equality operators in OCaml

2008-07-24 Thread Peng Zang
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1


On Thursday 24 July 2008 06:32:36 pm Jon Harrop wrote:
> From: "Raj Bandyopadhyay" <[EMAIL PROTECTED]>
>
> > 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-

___
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs


Re: [Caml-list] equality operators in OCaml

2008-07-24 Thread Jon Harrop

From: "Raj Bandyopadhyay" <[EMAIL PROTECTED]>

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.

___
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs


Re: [Caml-list] equality operators in OCaml

2008-07-24 Thread Vincent Hanquez
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

___
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs