Preventing backtracking
Prolog will automatically backtrack if this is necessary for satisfying a goal.
Now, consider a double-step function. The relation between X and Y can be specified by three rules:
Rule 1: if X < 3 then Y = 0
Rule 2: if 3 <= X and X < 6 then Y = 2
Rule 3: if 6 <= X then Y = 3
This can be written in Prolog as a binary relation f(X, Y) as follows:
1 | f(X, 0) :- X < 3. % Rule 1 |
This program assumes that before f(X, Y) is executed X is already instantiated to a number as this is required by the comparison operators.