Comparing values
The ways that you can compare values depends on the types of those values.
Many types allow the usual comparison operators: =
, !=
, <
, >
, ≤
and ≥
.
The syntax depends on the language.
Comparing values in this way produces a Boolean type that you can use in conditional instructions or to continue or terminate loops.
How values are compared depends on the type of the values:
nat
,int
,mutez
and timestamp values are compared numerically.- Strings,
bytes
,key_hash
,key
,signature
andchain_id
values are compared lexicographically. - Boolean values are compared so that false is strictly less than true.
- Address are compared as follows:
- Addresses of user accounts are strictly less than addresses of smart contracts.
- Addresses of the same type are compared lexicographically.
- Pair values (and therefore records) are compared component by component, starting with the first component.
- Options are compared as follows:
None
is strictly less than anySome
.Some x
andSome y
are compared asx
andy
.
- Values of
union
types built withor
are compared as follows:- any
Left x
is smaller than anyRight y
, Left x
andLeft y
are compared asx
andy
,Right x
andRight y
are compared asx
andy
.
- any
- Values of type
Unit
are all equal.
In Michelson, comparisons are done in two steps:
- A
COMPARE
instruction consumes the values and produces a value that is 0 if the two elements are equal, negative if the first element in the stack is less than the second, and positive otherwise. - The instructions
EQ
(equal),NEQ
(not equal),LT
(lower than),GT
(greater than),LE
(lower or equal) andGE
(greater or equal) consume this value and return the corresponding Boolean value.
Implementation details
- Michelson: Generic comparison
- Archetype: Comparison operators
- SmartPy: Comparing sp.int and sp.nat
- LIGO: Comparing values