This site is updated infrequently. For up-to-date information, please visit the new OCaml website at ocaml.org.

Equality of functional values
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
 Date: 2007-01-30 (20:30) From: Gerd Stolpmann Subject: Re: [Caml-list] Equality of functional values
```Am Dienstag, den 30.01.2007, 21:18 +0100 schrieb Tom:
> I guess the "correct" way to do = equality would be to check for tag
> (Obj.tag (Obj.repr x)), and if it isn't float (or float array) then ==
> equality would be performed first. The problem is that this would have
> to be implemented by the OCaml developers, as the naive
> implementation:
>
> let equal x y =
>          if Obj.tag (Obj.repr x) = Obj.double_tag then x = y
>          else
>                  if x == y then true else x = y
>
> doesn't work,

But

let equal x y =
Pervasives.compare x y = 0

works!

> as it should be called recursively (comparing two lists of functions
> will fail, because the functions would be compared by =, not by
> equal).
>
> Actually, the implementation could be improved further, as it could
> actually return true on examples such as
>
> # let f x y = x + y;;
> val f : int -> int -> int = <fun>
> # f == f;;
> - : bool = true
> # (f 1) = (f 1);;
> - : bool = true
>
> because the parameters, passed to a closure, could be compared, too...
>
> Anyhow, this can be added to the wishlist :)

I can fully understand that ( = ) fails on functional values. The
additional test for floats would take a lot of time, given that equality
is the most frequent test.

A better improvement would be that the compiler emits a warning when
there is the possibility that the equality test compares functional
values. In my opinion this is a serious programming error, and the
compiler should help to track it down.

Gerd
--
------------------------------------------------------------
Gerd Stolpmann * Viktoriastr. 45 * 64293 Darmstadt * Germany
gerd@gerd-stolpmann.de          http://www.gerd-stolpmann.de
Phone: +49-6151-153855                  Fax: +49-6151-997714
------------------------------------------------------------

```