Professional Documents
Culture Documents
Prolog Programming
Prolog Programming
Prolog Programming
DATA STRUCTURES IN PROLOG
PROGRAMMING TECHNIQUES
CONTROL IN PROLOG CUTS
List notation is a way of writing terms Terms as Data Term correspond with list
Lists in Prolog
The simplest way of writing a list is to
Lists in Prolog
We can also specify an initial sequence of
head H and tail T, written as [H|T] The head is the first element of a list, and The tail is the list consisting of the remaining elements.
The list [a, b, c] can also be separated as Head:The first element is a Tail:The list of remaining elements = [b, c]
6
Lists : Unification
Unification can be used to extract the
components of a list, so explicit operators for extracting the head and tail are not needed. The solution of the query
?- [H | T] = [a, b, c]. H = a T = [b, c]
list with head a and unknown tail denoted by variable T. Similarly, [ H, b, c] is a partial specification of a list with unknown head H and tail [b, c]. These two specification to unify H = a, T =[b,c]
8
Lists in Prolog
Example 2 The append relation on lists is
In words, The result of appending the empty list [ ] and a list Y is Y. If the result of appending X and Y is Z, then the result of appending [H | X] and Y is [H | Z]
Terms as Data
The Dot operator or functor . corresponds to
[H | T ] is syntactic sugar for the term .(H,T) Lists are terms. The term for the list [a, b, c] is
.(a, .(b, .(c, [])))
11
Terms as Data
following terms can be drawn a tree
.(a, .(b, .(c, [])))
a b
c []
nonleaf(leaf,leaf)
nonleaf(leaf,nonleaf(leaf,leaf))
nonleaf(nonleaf(leaf,leaf), nonleaf(leaf,leaf))
13
List : tree
Example 3 A binary search tree is either
Each node holds an integer. Smaller elements appear in the left subtree of a node and larger elements appear in the right subtree. Let a term node(K,S,T) represent a tree
K S T
14
0
9 3
10
12
19
0
3
9
15
16
3
15
whether an integer appear at some node in a tree. The two arguments of member are an integer and a tree.
member(K,_,_). member(K, node(N,S,_)) :- K < N, member(K, S). member(K, node(N,_,T)) :- K > N, member(K, T).
16
PROGRAMMING TECHNIQUES
The strengths of Prolog namely, backtracking
and unification.
Backtracking allows a solution to be found if one exists Unification allows variables to be used as placeholders for data to be filled in later.
can lead to efficient programs. The programs rely on left-to-right evaluation of subgoals.
17
Where guess(S) and verify(S) are subgoals. Prolog respond to a query by generating solutions to guess(S) until a solution satisfying verify(S) is found. Such queries are also called generate-and-test queries.
18
following form:
Conslusion() if guess(,S,) and verify(,S,)
Example
overlap(X, Y) :- member(M, X), member(M, Y).
Two lists X and Y overlap if there is some M that is a member of both X and Y. The first goal member(M, X) guesses an M from list X, and the second goal member(M, Y) verifies that M also appears in list Y. 19
The first rule says that M is a member of a list with head M. The second rule says that M is a member of a list if M is a member of its tail T.
20
Consider query
These query
?- overlap([a,b,c,d],[1,2,c,d]). yes
?- member(M,[a,b,c,d]),member(M,[1,2,c,d]).
solutions and the second goal tests to see whether they are acceptable.
21
Consider query
The solutions generated by the first goal are
?- member(M,[a,b,c,d]). M = a; M = b; M = c; M = d; no
22
Hint
Since computation in Prolog proceeds from
left to right, the order of the subgoals in a guess-and-verify query can affect efficiency. Choose the subgoal with fewer solutions as the guess goal. Example of the effect of goal order
?- X = [1,2,3], member(a,X). no ?- member(a,X), X = [1,2,3]). [infinite computation]
23
queries but not in terms representing objects. Terms containing varibales can be used to simulate modifiable data structures; The variables serve as placeholders for subterms to be filled in later.
24
nonleaf(leaf,leaf)
25
Its first element : a Its second element : b Do not yet know what X represents
26
27
responds to a query or applies a rule. An open list can be modified by unifying its end marker
marker.
X
_172
X
_236
29
to an assignment to that variable. List L changes from [a, b | _172] [a, b, c | _236] when _172 unifies with [c | _236] Advantage of working with open lists is that the end of a list can be accessed quickly.
30
enter(a,Q,R)
When element a enters queue Q, we get queue R.
leave(a,Q,R)
When element a leaves queue Q, we get queue R.
31
32
Test queue
?-setup(Q),enter(a,Q,R),enter(b,R,S),leave(X,S,T), leave(Y,T,U),wrapup(U). Q R S X T Y U = q([a, b], [a, b]) = q([a, b], [b]) = q([a, b], []) =a = q([b], []) =b = q([], [])
Yes ?-
33
Operations on a queue
setup(Q) Q
_1
enter(a,Q,R)
R
_2
enter(b,R,S)
a Q
T
_3
b
34
Operations on a queue
leave(X,S,T) X T
_3
b Y T
_3
leave(Y,T,U) a
b
35
Internal Prolog
A queue q(L,E) consists of open list L with
end marker E.
setup(q(X,X)).
Second goal
enter(A,q(X,Y),q(X,Z)):- Y = [A|Z].
bind Y to a list [A|Z], where Z is a fresh end marker, and return q(X,Z).
?-setup(Q),enter(a,Q,R). Q = q([a|_2], [a|_2]) R = q([a|_2], _2)
37
resulting queue has the tail of L in place of L. Note in the diagram to the right of leave(X,S,T) that the open list for queue T is the tail of the open list for S. The final goal wrapup(U) checks that the enter and leave operations leave U in an initial state q(L,E), where L is an empty openlist with end marker E.
38
Difference Lists
Difference List are a technique for coping with
such changes. Difference List consists of a list and its suffix. We write this difference list as
dl(L,E).
39
the elements that are in L but not in E. Examples of difference lists with contents [a,b] are
dl([a,b],[]). Dl([a,b,c],[c]). Dl([a,b|E],E). Dl([a,b,c|F],[c|F]).
40
CONTROL IN PROLOG
In the informal equation
algorithm = logic + control
logic program and control refers to how a language computes a response to a query.
41
CONTROL IN PROLOG
Control in Prolog is characterized by two
decisions
Goal order : Choose the leftmost subgoal. Rule order : Select the first applicable rule.
goal order within the query and by rule order with in the database of facts and rules.
42
CONTROL IN PROLOG
start with a query as the current goal; while the current goal is nonempty do
choose the leftmost subgoal; if a rule applies to the subgoal then select the first applicable rule; form a new current goal else backtrack end if
Example
A sublist S of Z can be specified in the
appen1([],Y,Y). appen1([H|X],Y,[H|Z]):- appen1(X,Y,Z). Prefix(X,Z) :- appen1(X,Y,Z). Suffix(Y,Z) :- appen1(X,Y,Z). appen2([H|X],Y,[H|Z]):- appen2(X,Y,Z). appen2([],Y,Y).
44
Queries
The corresponding queries usually produce
45
Queries
New Solutions are produced on demand for
?- appen1(X,[c],Z). X = [] Z = [c] ; X = [_G230] Z = [_G230, c] ;
Unification an Substitutions
Unification is central to control in Prolog
terms
47
applies to a subgoal G if its head A unifies with G Variables in the rule are renamed before unification to keep them distinct from variables in the subgoal.
A :- B1, B2, , Bn
48
GOAL Suffix([a],L),prefix(L,[a,b,c]). suffix([a],L) if append(_1,[a],L). Append(_1,[a],L),prefix(L,[a,b,c]). {_1[],L[a]} append([],[a],[a]). Prefix([a],[a,b,c]). prefix([a],[a,b,c]) if append([a],_2,[a,b,c]) append([a],_2,[a,b,c]). prefix([a],[a,b,c]) if append([],_2,[b,c]) Append([],_2,[b,c]). {_2[b,c]} append([],[b,c],[b,c]) yes
49
50
51
Cuts
A cut prunes or cuts out and unexplored
part of a Prolog search tree. Cuts can therefore be used to make a computation more efficient by eliminating futile searching and backtracking. Cuts can also be used to implement a form of negation
52
Cuts
A cut, written as !, appears as a condition
is applied, the cut tells control to backtrack past Cj-1,,C1,B, without considering any remaining rules for them.
53
past B without considering any remaining rules for B. Thus the cut has the effect of making B fail if C fails.
54
Example
b,G
b :- c. b :- d. b :- e. b :- c. b :- !,d. b :- e.
c,G !,d,G d,G
X
e,G
d,G
55
Example
?-a(X).
b c d X=1
a(X)
e Yes X=2 !c c
backtrack
a(X)
b d e Yes X=2
backtrack Yes
a(1) a(2) b :b :-
:- b; :- e; c. d.
a(1) a(2) b :b :-
:- b; :- e; !,c. d.
56
is applied during a computation The cut tells control to backtrack past Cj1,..C1,B without considering any remaining rules for them.
The effect of inserting a cut in the middle of a
guess-and-verify rule.
57
the form guess(S), verify(S), where guess(S) generates potential solutions until one satisfying verify(S) is found. The effect of insering a cut between them, as
Conclusion(S) :- guess(S), !, verify(S)
58
(a)
(b)
59
a(Z)
a(Z)
b(Z)
f(5)
b(Z)
f(5)
g(Z),v(Z)
v(4)
g(Z),!,v(Z)
v(4)
v(1)
v(2)
v(3)
!v(X) v(2)
v(1)
v(3)
(a)
(b)
60
Negation as Failure
The not operator in Prolog is implemented by
the rules
not(X) :- X, !, fail. not(_).
61