
























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!

























Prolog programs are constructed from terms : constants, variables, or structures.
Constants can be either atoms or numbers:
Variables are strings of characters beginning with an uppercase letter or an underscore.
Structures consist of a functor or function symbol , which looks like an atom, followed by a list of terms inside parentheses, separated by commas. Structures can be interpreted as predicates (relations): likes(john,mary). male(john). sitsBetween(X,mary,helen).
Figure A.1 depicts the following structure as trees: person(name('Kilgore','Trout'),date(november,11,1922)) tree(5, tree(3,nil,nil), tree(9,tree(7,nil,nil),nil))
A Prolog program is a sequence of statements − clauses − of the form
P 0 :- P 1 , P 2 , …, Pn. where each of P 0 , P 1 , …, Pn is an atom or a structure.
A period terminates every clause.
A clause can be read declaratively as
P 0 is true if P 1 and P 2 … Pn are true or procedurally as To satisfy goal P 0 , satisfy goal P 1 and then P 2 and then … and then Pn.
P 0 is the head goal; the conjunction of goals P 1 , P 2 , …, Pn is the body of the clause.
A clause without a body
P. is a unit clause or fact and means P is true. or goal P is satisfied.
A clause without a head,
?- P 1 , P 2 , …, Pn. is a goal clause or query and means Are P 1 and P 2 and … Pn true? or Satisfy goal P 1 and then P 2 and then … and then Pn.
A Prolog program consists of
A query sets the Prolog interpreter into action.
We develop an example incrementally.
User queries are shown in boldface followed by the response by the Prolog interpreter.
Comments start with the symbol % and continue to the end of the line.
Some facts: parent(chester,irvin). parent(chester,clarence). parent(chester,mildred). parent(irvin,ron). parent(irvin,ken). parent(clarence,shirley). parent(clarence,sharon). parent(clarence,charlie). parent(mildred,mary).
Some queries:
?- parent(chester,mildred). yes ?- parent(X,ron). X = irvin yes ?- parent(irvin,X). X = ron ; X = ken ; % The user-typed semicolon asks the no % system for more solutions. ?- parent(X,Y). X =chester Y = irvin % System will list all of the parent yes % pairs, one at a time,if semicolons % are entered.
Additional facts:
male(chester). female(mildred). male(irvin). female(shirley). male(clarence). female(sharon). male(ron). female(mary). male(ken). male(charlie).
Additional queries:
?- parent(clarence,X), male(X). X = charlie yes ?- male(X), parent(X,ken). X = irvin yes ?- parent(X,ken), female(X). no
Prolog obeys the “closed world assumption” that presumes that any predicatethat cannot be proved must be false.
?- parent(X,Y), parent(Y,sharon). X = chester Y = clarence yes
1. The equality predicate = permits infix as well as prefix notation − e.g.,
?- ken = ken. yes ?- =(ken,ron). no ?- ken = X. % Can a value be found for X to make X = ken % it the same as ken? yes % The equal operator represents the notion % of unification.
2. + (“not”) is a unary predicate:
+ P is true if P cannot be proved false and false if it can − e.g.,
?- + (ken=ron). yes ?- + (mary=mary). no
The closed world assumption − that any property not recorded in the database isn’t true − governs how + works.
Since the behavior of + diverges from the logical not of predicate calculus, we use + as little as possible (not at all in the laboratory exercises).
The following is a new sibling rule (the previous rule must be removed): sibling(X,Y) :- parent(Z,X), parent(Z,Y), + (X=Y).
Queries: ?- sibling(ken,X ). X = ron ; no ?- sibling(X,Y). X = irvin Y = clarence ; % sibling is a symmetric relation. X = irvin % 3 sets of siblings produce 6 answers. Y = mildred ; X = clarence % The database allows 14 answers. Y = irvin ; X = clarence Y = mildred ; X = mildred Y = irvin ; Y = mildred X = clarence % No semicolon here. yes
Suppose we want to define a predicate “X is an ancestor of Y,” which is true if parent(X,Y) or parent(X,Z) and parent(Z,Y) or parent(X,Z), parent(Z,Z1), and parent(Z1,Y) or …
A recursive definition is required to allow an arbitrary depth for the definition.
The first case above serves as the basis for the recursive definition.
The remaining cases are handled by an inductive step. ancestor(X,Y) :- parent(X,Y). ancestor(X,Y) :- parent(X,Z), ancestor(Z,Y).
To continue our example, we add some more facts: parent(ken,nora). female(nora). parent(ken,elizabeth). female(elizabeth).
Fig. A.3 shows the parent relation between the twelve people defined in our database.
Some queries: ?- ancestor(mildred,mary). yes % because parent(mildred,mary). ?- ancestor(irvin,nora). yes % because % parent(irvin,ken) and % ancestor(ken,nora) because parent(ken,nora). ?- ancestor(chester,elizabeth). yes % because % parent(chester,irvin) % and ancestor(irvin,elizabeth) % because parent(irvin,ken) and % ancestor(ken,elizabeth) % because parent(ken,elizabeth). ?- ancestor(irvin,clarence). no % because parent(irvin,clarence) is not provable % and,whoever is substituted for Z, it is % impossible to prove parent(irvin,Z) and % ancestor(Z,clarence).
All possibilities for Z are tried that make parent(irvin,Z) true, namely Z=ron and Z=ken, and both ancestor(ron,clarence) and ancestor(ken,clarence) fail.
A poor choice of goal order may give a search tree with an infinite
branch down which the interpreter gets trapped − e.g.,
ancestor3(X,Y) :- ancestor3(Z,Y), parent(X,Z). ancestor3(X,Y) :- parent(X,Y).
?- ancestor(irvin,elizabeth). yes ?- ancestor3(irvin,elizabeth). This query invokes a new query ancestor3(Z,elizabeth), parent(irvin,Z). which invokes ancestor3(Z1,elizabeth), parent(Z,Z1), parent(irvin,Z). which invokes ancestor3(Z2,elizabeth), parent(Z1,Z2), parent(Z,Z1), parent(irvin,Z). which invokes …
The eventual result is a message such as “Out of local stack during execution; execution aborted.”
The problem with this last definition of the ancestor relation is the left recursion with uninstantiated variables in the first clause.
If possible, the leftmost goal in the body of a clause should be nonrecursive so that a pattern match occurs and some variables are instantiated before a recursive call is made.
A list of terms can be represented between brackets − e.g., [a,b,c].
Here the head of the list is a, the tail is [b,c].
The tail of, e.g., [a] is [] (the empty list).
Lists may contain lists as elements − e.g., [a,[b,1],3,[c]] is a list of four elements.
As a special form of direct pattern matching, [H|T] matches any list with at least one element:
A list of elements is permitted to the left of the vertical bar − e.g., [X,a,Y|T] matches any list with at least three elements whose second element is the atom a:
Using these pattern matching facilities, values can be specified as the intersection of constraints instead of by direct assignment.
Lists are ordinary structures with syntactic sugar added.
The notation abbreviates terms constructed with the predefined “.” function symbol and the special atom []. E.g., [a,b,c] abbreviates .(a,.(b,.(c,[]))). [H|T] abbreviates .(H,T). [a,b|X] abbreviates .(a,.(b,X)).
For this predicate we need two clauses,
The predicate succeeds if X is the first element of L. member(X, [X|T]). If the first clause fails, check if X is a member of the tail of L. member(X, [H|T]) :- member(X,T).
If the item is not in the list, the recursion eventually tries a query of the form member(X,[]).
This fails since the head of no clause for member has [] as second argument.
“The variable NewList is to be bound to a copy of List with all instances of X removed”.
When X is removed from an empty list, we get the same empty list. delete(X,[ ],[ ]). When an item is removed from a list with that item as its head, we get the list that results from removing the item from the tail of the list (ignoring the head). delete(H,[H|T],R) :- delete(H,T,R). If the previous clause fails, X is not the head of the list, so we retain the head of L and take the tail that results from removing X from the tail of the original list. delete(X,[H|T],[H|R]) :- delete(X,T,R).
“The variable U is to be bound to the list that contains the unionof the elements of L1 and L2”.
If the first list is empty, the result is the second list. union([ ],L2,L2). % clause 1 If the head of L1 is a member of L2, it may be ignored since a union does not retain duplicate elements. union([H|T],L2,U) :- member(H,L2), union(T,L2,U). % clause 2 If the head of L1 is a not member of L2 (clause 2 fails), it must be included in the result. union([H|T],L2,[H|U]) :- union(T,L2,U). % clause 3 In the last two clauses, recursion is used to find the union of the tail of L1 and the list L2.
When the last query succeeds, the answer is constructed by unwinding the bindings: R = [a | M] = [a | [b | M1]] = [a,b | M1] = [a,b | [c | M2]] = [a,b,c | M2] = [a,b,c | [d,e]] = [a,b,c,d,e].
Figure A.5 shows the search tree for another application of concat using semicolons to generate all the solutions.