In order to program declaratively, we have to think about what we are trying to declare and then we can describe it. To this end, I would try to write the predicate so that it expresses why the value of C
alternates between three possible states depending on the relative sizes of A
and B
. Here's a predicate with the same form that you describe with foo
:
comparison(A, B, less) :- A < B.
comparison(A, B, equal) :- A =:= B. %% =:=/2 is "True if expression A evaluates to a number equal to B".
comparison(A, B, greater) :- A > B.
You don't need the front-end predicate foo/3
. You might read the lines thus: "The comparison
of A
and B
is <Value>
if A
is <relation>
with regard to B
." This program consists of three rules that describe the rule comparison/3
. The '3' indicates that this rule defines a relation between the 3 elements given to it as arguments.
You can query this program:
?- comparison(1,3, X).
X = less ;
false.