[Next] [Prev] [Right] [Left] [Up] [Index] [Root]
Equality testing

Equality testing

Magma provides two equality operators: eq for strong (comparable) equality testing, and cmpeq for weak equality testing. The operators depend on the concept of comparability. Objects x and y in Magma are said to be comparable if both of the following points hold:

The possible automatic coercions are listed in the descriptions of the various Magma modules. For instance, the table in the introductory chapter on rings shows that integers can be coerced automatically into the rational field.
x eq y : Elt, Elt -> BoolElt
If x and y are comparable, return whether x equals y (which will always work by the second rule above). If x and y are not comparable, an error results.
x cmpeq y : Elt, Elt -> BoolElt
If x and y are comparable, return whether x equals y. Otherwise, return false. Thus this operator always returns a value and an error never results. It is useful when comparing two objects of completely different types where it is desired that no error can happen. However, it is strongly recommended that eq is usually used to allow Magma to pick up common unintentional type errors.

Example Lang_Equality (H1E28)

We illustrate the different semantics of eq and cmpeq.

> 1 eq 2/2;
true
> 1 cmpeq 2/2;
true
> 1 eq "x";
Runtime error in 'eq': Bad argument types
> 1 cmpeq "x";
false
> [1] eq ["x"];
Runtime error in 'eq': Incompatible sequences
> [1] cmpeq ["x"];
false

[Next] [Prev] [Right] [Left] [Up] [Index] [Root]