Prolog Operators

Prolog Lists

Built-In Predicates

Miscellaneous

Prolog - Term Comparison Operators



Term Comparison Operators works on standard terms of prolog. Using term comparison operators, we can compare following types of terms in standard term order of prolog.

  • Variables − A variable if uninstantiated is ordered by its internal order by default.

  • Integers − Integers are compared and ordered by their numeric values.

  • Floating point numbers − Floating-point numbers are compared and ordered by their numeric values.

  • Atoms − Atoms are ordered lexicographically or alphabetically.

  • Compound Terms, Structures − In order to compare a compound term, prolog uses following rules −

    • First, arity is compared as number of arguments.

    • Second, functor name compared alphabetically.

    • lastly, arguments are compared from left to right recursively.

  • Lists − List is compared using following rules −

    • Each elements of the lists are compared from left to right.

    • In case, first list is prefix of second list, the first one comes first.

Following is the list of term comparison operators −

Operator Meaning
X @> Y X is strictly greater than Y
X @< Y X is strictly less than Y
X @>= Y X is strictly greater than or equal to Y
X @=< Y X is strictly less than or equal to Y
X == Y X and Y values are strictly identical.
X \== Y X and Y values are not strictly identical.

Example - Compare simple terms

| ?- apple @< orange
.

yes
| ?- 7 @< 10.

yes
| ?- 7 @< 5.

no
| ?- 

Example - Compare complex terms

| ?- func(a, b) @< func(a, c).  % arguments are compared.

yes
| ?- [a, b, c] @< [a, b, c, d].  % first list being prefix, is returned. 

yes
| ?- X = Y, X == Y. % As X and Y are same variables

Y = X

yes
| ?- X == 5. % X is uninstantiated variable and 5 is atom, not identical.

no
| ?-   

Key differences between == and = operators.

= operator is known a unification operator and is used to make terms equal by instantiated the variables. For example −

| ?-  X = 7.  % variable X is now 7

X = 7

yes
| ?- 

Where as == operator is used to compare two terms witout instantiating the variable.

| ?-  Y == 7.  % false as Y is unintantiated.

no
| ?- X = Y, X == Y.  % true as X and Y are now same variables.

Y = X

yes
| ?- 
Advertisements