Suppose that the Prolog database contains just the single fact
likes(mary, pizza). and that there is a query:
?- likes(mary, What).
Prolog will search the (tiny) database, find that the query can
be satisfies if
What = pizza, do it will bind
pizza and report success:
?- likes(mary, What). What = pizza ; false.
Now suppose that there is a rule and facts in Prolog's database:
teaches(Teacher, Student):- lectures(Teacher, Subject), studies(Student, Subject). lectures(codd, databases). studies(fiona, databases). studies(fred, databases).
and that the user issues the query:
?- teaches(codd, Who).
The Prolog interpreter first matches the head of the rule with
the query, and so binds
It then finds a fact that indicates a subject
that Codd lectures - namely
At this point, the variable
Subject is bound to
Subject = databases).
In other words,
Subject, perhaps temporarily,
has the value
databases. Then Prolog tries to satisfy
the second goal
studies(Student, Subject) with
Subject = databases,
i.e. it tries to satisfy
When it finds the solution,
it will bind
and report the solution:
?- teaches(codd, Who). Who = fiona
Notice that the binding
Subject = databases was made
in solving the query, but it is not reported, as it is not
explicitly part of the query.
Then, if the user types "
;", Prolog will
backtrack and undo the binding
Student = fiona
and look for another value for
Subject that satisfies
studies(Student, databases), and find
Student = fred.
However, while binding (and unbinding) is involved in this step,
it is properly treated under backtracking.