Samuele Giraudo
Samuele Giraudo

Reputation: 429

Physical equality test for functions in Caml

In Caml, the operator == tests the physical equality between two values of a same type. It can be used in particular to compare functions in this way.

One has for instance

# print_string == print_string;;
- : bool = true

but, surprisingly,

# (==) == (==);;
- : bool = false

This expression should be evaluated into true.

Can you explain this behavior?

Upvotes: 4

Views: 990

Answers (1)

Jeffrey Scofield
Jeffrey Scofield

Reputation: 66818

The behavior of == is defined in the Pervasives module:

e1 == e2 tests for physical equality of e1 and e2. On mutable types such as references, arrays, byte sequences, records with mutable fields and objects with mutable instance variables, e1 == e2 is true if and only if physical modification of e1 also affects e2. On non-mutable types, the behavior of ( == ) is implementation-dependent; however, it is guaranteed that e1 == e2 implies compare e1 e2 = 0

Since functions aren't mutable, the only guarantee is that if they compare equal with == they will also compare equal with compare. Since functions aren't guaranteed to be be comparable with compare, this essentially means that == isn't useful at all for comparing functions.

# compare (==) (==);;
Exception: Invalid_argument "equal: functional value".

If == returns false for immutable values, there are no guarantees at all. This means that == is free to return false at any time for any immutable values. So it's not improper to return false in your second example.

Upvotes: 6

Related Questions