Abstract
This chapter demonstrates how Prolog uses unification to match goals with the heads of clauses and how it uses the combination of unification and backtracking to evaluate goals entered by the user and to find multiple solutions if required. The chapter ends with a warning about the use of ’user-controlled’ backtracking.
After reading this chapter you should be able to:
-
Determine whether two call terms unify and thus whether a goal can be matched with a clause in the database
-
Understand how Prolog uses unification and backtracking to evaluate a sequence of goals entered by the user
Access this chapter
Tax calculation will be finalised at checkout
Purchases are for personal use only
Notes
- 1.
Start at the top (for evaluation) or after the clause matched when the goal was last satisfied (for re-evaluation).
Author information
Authors and Affiliations
Practical Exercise 3
Practical Exercise 3
The program below is a variant of the family relationships program used in Section 3.4. As before, [M1] etc. are labels added to make it easier to refer to the clauses.
[M1] mother(ann,henry).[M2] mother(ann,mary).[M3] mother(jane,mark). [M4] mother(jane,francis).[M5] mother(annette,jonathan).[M6] mother(mary,bill). [M7] mother(janice,louise).[M8] mother(lucy,janet).[M9] mother(louise,caroline). [M10] mother(caroline,david).[M11] mother(caroline,janet).[F1] father(henry,jonathan).
[F2] father(john,mary).[F3] father(francis,william).[F4] father(francis,louise).[F5] father(john,mark).[F6] father(gavin,lucy).[F7] father(john,francis).[P1] parent(victoria,george).[P2] parent(victoria,edward).[P3] parent(X,Y):-mother(X,Y).[P4] parent(X,Y):-father(X,Y).[P5] parent(elizabeth,charles).[P6] parent(elizabeth,andrew).[A1] ancestor(X,Y):-parent(X,Y).[A2] ancestor(X,Y):-parent(X,Z),ancestor(Z,Y).
The most important change is the addition of two clauses defining the ancestor/2 predicate. Clause [A1] simply states that X is an ancestor of Y if X is a parent of Y. Clause [A2] is a recursive definition of more distant ancestor relationships which can be read as ’X is the ancestor of Y if there is some person Z such that X is the parent of Z and Z is the ancestor of Y’.
-
(1)
Extend the program above by devising rules to define each of the following. Load your extended program and test it.
-
(a)
child_of(A,B)
-
(b)
grandfather_of(A,B)
-
(c)
grandmother_of(A,B)
-
(d)
great_grandfather_of(A,B)
-
(2)
Construct a sequence of diagrams similar to those in Section 3.4 to show the sequence of events when the Prolog system attempts to satisfy the goal
?-ancestor(louise,Desc).
Find (using backtracking) the first two people the Prolog system will identify as louise’s descendants.
Predict the output that will be produced if the user repeatedly forces the system to backtrack. Verify your prediction by loading the program and testing it.
Rights and permissions
Copyright information
© 2013 Springer-Verlag London
About this chapter
Cite this chapter
Bramer, M. (2013). Satisfying Goals. In: Logic Programming with Prolog. Springer, London. https://doi.org/10.1007/978-1-4471-5487-7_3
Download citation
DOI: https://doi.org/10.1007/978-1-4471-5487-7_3
Published:
Publisher Name: Springer, London
Print ISBN: 978-1-4471-5486-0
Online ISBN: 978-1-4471-5487-7
eBook Packages: Computer ScienceComputer Science (R0)