11.1 Example of a rule

Old students program

We begin our treatment of rules with an example. Consider the familiar program describing students and their semesters:

student(name(Lisa), semester(5)).
student(name(Martin), semester(3)).
student(name(John), semester(3)).
student(name(Edward), semester(7)).

Three interesting problems

One interesting problem would be: Given the names of two students, how can we easily find out whether they are in the same semester?

Another problem would be: Given the name of one student, which students are in the same semester as this student?

And finally, a third problem would be: Which pairs of students are in the same semester?

The solution

The solution to all of these problems turns out to be solvable by means of a rule. Consider the following rule:

same_semester(A,B) :- student(A,S), 

This rule, along with the students database, can be found in "Students3.plgCG".

Meaning of rule

The rule means "A and B are in the same semester IF we can find student A in semester S, AND THEN we can find student B in semester S (the same semester)".

Application of rule

There are three ways this rule can be applied:

  • If we have two known students,
  • If we have one unknown student and one known student,
  • If we have two unknown students.

We will treat each of these in turn.

Two known students

If we now ask:

?- same_semester(name(Martin), name(John)).

the answer is:


meaning "yes". This is because we can find two students with the names Martin and John in the same semester.

If we ask

?- same_semester(name(Martin), name(Lisa)).

the answer is:


This is because Martin and Lisa are not in the same semester. Thus, while the first subgoal in the rule succeeded with the variable binding

{S = semester(3)}

the second subgoal did not succeed, since S was not bound to


and thus the matching failed.

One unknown student

What happens if we use a variable instead of one of the names? Let's try:

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

Perhaps a bit surprisingly, the answer is:

{X = name(Martin)}
{X = name(John)}

We see that John is an answer to the question, but we also see that Martin himself is in the set of answers. But this is just because we haven't included anything in the rule to say that students who are themselves should not be included in the answer. So, in fact, we got what we asked for. Logically, the answer is correct.

Two unknown students

Finally, let us ask which pairs of students are in the same semester.

?- same_semester(X,Y).

The answer is:

{X = name(Lisa), Y = name(Lisa)}
{X = name(Martin), Y = name(Martin)}
{X = name(Martin), Y = name(John)}
{X = name(John), Y = name(Martin)}
{X = name(John), Y = name(John)}
{X = name(Edward), Y = name(Edward)}

It will be noted that all students in the database are listed in the answer as being in the same semester as themselves. This was to be expected, given the answer to the previous question. It will also be noted that both "Martin,John" and "John,Martin" are listed. The reason for all of this will become clear in the next chapter on Prolog's solution strategy.


Now let us turn from this example to some useful terminology.

PrevLite: 11 Rules
NextLite: 11.2 Terminology

Prev: 11 Rules
Up: 11 Rules
Next: 11.2 Terminology