Prolog A Tutorial Introduction - Courses
Prolog A Tutorial Introduction - Courses
Prolog A Tutorial Introduction - Courses
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
Resolution: replace edge(a,SomeNode) by nothing (since we unified with a fact) and apply the substitution<br />
above to get the new query:<br />
edge(b,Node2)<br />
3. There is only one atom in the query.<br />
Unify<br />
edge(b,Node2) and edge(b,d),<br />
giving the substitution<br />
Node2 = d.<br />
Resolution: replace edge(b,Node2) by nothing (since we unified with a fact). Since the resulting query<br />
is empty, we are done.<br />
At this point the substitution can be determined. At this point it is a good idea to pause to observe what<br />
has happened with the parameters of the atoms involved in the computation. In our original query we had<br />
one constant and one variable. If you consider carefully, you may see that the constant parameter has been<br />
passed to each successive atom as a value, while the variable X has been passed a reference. Notice that at no<br />
point has there been anything like a traditional computation – just the unification to match up arguments<br />
(pass parameters?) and the resolution to reform the query.<br />
Backtracking<br />
We are actually not finished with the example. You may have noticed that there is another solution: we could<br />
redo the computation above and get the substitution X = b or X = c or X = d. The way <strong>Prolog</strong> does this<br />
is, when a query is finally reduced to the empty query, <strong>Prolog</strong> backtracks to the most recent unification to<br />
determine whether there is another fact or rule with which unification can succeed. If there is, an additional<br />
solution may be found. Backtracking continues until all possible answers are determined.<br />
Problem 3<br />
1. Consider the problem of backtracking in response to the query ‘?- tedge(a,X)’. Assume the graph<br />
of Example 3.3. Write out the steps of the unification/resolution above and clearly mark any place<br />
where more than one rule or fact must be checked for unification (remember that each one may lead<br />
to another solution). Then continue the unification/resolution process via backtracking to determine<br />
all possible answers to the query.<br />
2. (Extra Credit!) Your family tree — Probably the most common first example of a logic program is<br />
that involving a family tree. Create a set of facts that define for your immediate family the relations:<br />
male = { The males in your family. }<br />
female = { The females in your family. }<br />
parent_of = { The pairs (x,y) where x is the parent of y<br />
in your family. }<br />
[Don’t go overboard if you have a large family — you don’t want to be typing data till next week!]<br />
Note that the predicates male and female are unary, involving only one argument. You need one<br />
entry for each person in your family. Next define the following predicates based on the previous three<br />
predicates.<br />
father, mother, son, daughter, grandfather,<br />
aunt, uncle, cousin, ancestor<br />
CSCI 208 10 <strong>Prolog</strong> <strong>Tutorial</strong>