Term comparison
Author: Daniel Diaz
Keywords: GNU Prolog, manual, Prolog, compiler, constraints, finite domains

### 8.3 Term comparison

- Standard total ordering of terms
- (==)/2 - term identical, (\==)/2 - term not identical,
  (@<)/2 - term less than, (@=<)/2 - term less than or equal to,
  (@>)/2 - term greater than, (@>=)/2 - term greater than or equal to
- compare/3

#### 8.3.1 Standard total ordering of terms

The built-in predicates described in this section allows the user to compare Prolog terms. Prolog terms are totally ordered according to the standard total ordering of terms which is as follows (from the smallest term to the greatest):

- variables, oldest first.
- finite domain variables (section 9.1.1), oldest first.
- floating point numbers, in numeric order.
- integers, in numeric order.
- atoms, in alphabetical (i.e. character code) order.
- compound terms, ordered first by arity, then by the name of the principal functor and by the arguments in left-to-right order.

A list is treated as a compound term (whose principal functor is ’.’/2).

The portability of the order of variables is not guaranteed (in the ISO reference the order of variables is system dependent).

#### 8.3.2 (==)/2 - term identical, (\==)/2 - term not identical,
(@<)/2 - term less than, (@=<)/2 - term less than or equal to,
(@>)/2 - term greater than, (@>=)/2 - term greater than or equal to

Templates

  ==(?term, ?term)
  \==(?term, ?term)
  @<(?term, ?term)
  @=<(?term, ?term)
  @>(?term, ?term)
  @>=(?term, ?term)

Description

These predicates compare two terms according to the standard total ordering of terms (section 8.3.1).

Term1 == Term2 succeeds if Term1 and Term2 are equal.

Term1 \== Term2 succeeds if Term1 and Term2 are different.

Term1 @< Term2 succeeds if Term1 is less than Term2.

Term1 @=< Term2 succeeds if Term1 is less than or equal to Term2.

Term1 @> Term2 succeeds if Term1 is greater than Term2.

Term1 @>= Term2 succeeds if Term1 is greater than or equal to Term2.

==, \==, @<, @=<, @> and @>= are predefined infix operators (section 8.14.10).

Errors

None.

Portability

ISO predicates.

#### 8.3.3 compare/3

Templates

  compare(?atom, +term, +term)

Description

compare(Order, Term1, Term2) compares Term1 and Term2 according to the standard (section 8.3.1) and unifies Order with:

- the atom < if Term1 is less than Term2.
- the atom = if Term1 and Term2 are equal.
- the atom > if Term1 is greater than Term2.

Errors

Order is neither a variable nor an atom |  | type_error(atom, Order)
Order is an atom but not <, = or > |  | domain_error(order, Order)

Portability

ISO predicate.

Copyright (C) 1999-2021 Daniel Diaz Verbatim copying and distribution of this entire article is permitted in any medium, provided this notice is preserved. More about the copyright