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: -- (:) From: Brian Hurt Subject: Re: [Caml-list] Equality of functional values
```Tom wrote:

> It's hard to perform equality on functional values, as it is hard to
> confirm two functions being the same... (even if you know the
> mathematical definition of the function, thus even harder if you only
> know the machine representation of it...). The best way around is to
> use == equality (= is structural equality, while == is referential
> equality - whether two names point to the same address in the memory),
> which might return false often, but will never fail on functional
> values. So I guess it's best you edit the source code (if you can) and
> change it...
>

The problem with this is that it fails on partially applied functions.
For example:

# let f x y = x + y;;
val f : int -> int -> int = <fun>
# f == f;;
- : bool = true
# (f 1) == (f 1);;
- : bool = false
#

This happens for the same reasons that:
# 1. == 1.;;
- : bool = false
#

does.  Basically, both the expressions 1. and (f 1) allocate blocks on
the heap.  Evaluating them twice allocates different blocks, and thus
referential comparisons return false.

My general advice is don't depend upon equality of functions.

Brian

```