14.1 Relational goals (Ad)

Introduction

On this page, we introduce three built-in primitive goals:

  1. eq/2
  2. eqv/2
  3. dif/2

eq/2

This goal tries to match (or unify) the two arguments. It succeeds if the matching succeeds, and fails if the matching fails.

For example, given the following program:

XIsNameY(X,Y) :- eq(X, name(Y)).

we can ask the following query:

?- XIsNameY(name(Martin), Martin).

and we get the answer:

{}

meaning "yes".

The reason is that X, which is bound to name(Martin), matches with name(Y), since Y is bound to Martin.

eqv/2

The eqv goal stands for "equivalent", and is the identity test on numbers. No matching occurs -- both arguments should have values. Thus you cannot make new variable bindings with this goal, since if either argument contains a variable, that variable should already have a value (or "be bound").

For example, if we ask:

?- eqv(X,54).

the answer is:

Error: any variable in an expression should have a value.

This is because X is a free variable here.

However, if we ask:

?- eqv(54,54).

the answer is:

{}

Again, if we ask

?- eq(X,54), eqv(X,54).

The answer is:

{X = 54}

thus the first subgoal (eq(X,54)) succeeded with the variable binding just given, and the second subgoal also succeeded, since X was bound to 54.

dif/2

This goal means "different".

dif(x,y) 

is equivalent to:

not(eq(x,y))

Thus, first matching is attempted between the two arguments. Then the result is negated: If the matching succeeded, the dif goal fails. If the matching failed, the dif goal succeeds.

For example,

?- dif(X,name(Martin)).

yields the answer:

no.

This is because X is a free variable, so X matches with name(Martin). Thus the matching succeeds, and therefore the dif subgoal fails.

On the other hand, if we ask:

?- eq(X,name(Lisa)), dif(X,name(Martin)).

we get the answer:

{X = name(Lisa)}

Thus the first subgoal succeeded with the variable binding just given, and the second subgoal also succeeded, since name(Lisa) does not match with name(Martin).

Next

Next, we look at two logical goals.


Prev: 14 Prolog+CG (Ad)
Up: 14 Prolog+CG (Ad)
Next: 14.2 Logical goals (Ad)