Download presentation
Presentation is loading. Please wait.
Published byMarsha Crawford Modified over 9 years ago
1
Prolog Cut to Control Search Abstract Data Types Practical Examples Summary
2
Cut to Control Search The search for more answers can be stopped with the cut operator (!). This has two side effects: 1.When originally found it always succeeds. 2.If it is “failed back” it causes the entire goal to fail.
3
Example move(1,6). move(1,8). move(6,7). move(6,1). move(8,3). move(8,1). path2(X,Y) :- move(X,Z), move(Z,Y). 1 6 8 7 3
4
Example ?- path2(1,W) W = 7; W = 1; W = 3; W = 1; no move(1,6). move(1,8). move(6,7). move(6,1). move(8,3). move(8,1). path2(X,Y) :- move(X,Z), move(Z,Y).
5
Operator Cut ?- path2(1,W) W = 7; W = 1; no move(1,6). move(1,8). move(6,7). move(6,1). move(8,3). move(8,1). path2(X,Y) :- move(X,Z), !, move(Z,Y) cut operator
6
Advantages of the Operator Cut There are several advantages that come with this operator: 1.It enables us to control explicitly the shape of the search tree. 2.When no more unifications are required the search can be made to stop. 3.The program can be made to run faster and to conserve memory space.
7
Prolog Cut to Control Search Abstract Data Types Practical Examples Summary
8
The ADT Stack Remember a stack is a linear structure with access on one end only. Operators for the stack are as follows: A.Test (whether the stack is empty) B.Push (an element on the stack) C.Pop (the top element from the stack) D.Peek (the top element without popping) E.Member_stack (check if an element is in the stack)
9
The ADT Stack Test = true Push A A Push B B A Test = false Pop (returns B) A Peek (returns A) A member_stack (A) returns true member_stack (B) returns false
10
Predicates for the Stack Empty Stack: empty_stack([]). ?- empty_stack([a,b]) no Push, Pop and Peek: stack(Top,Stack,[Top|Stack]).
11
Predicates for the Stack Push, Pop and Peek: stack(Top,Stack,[Top|Stack]). For Push: ?- stack(a,[b,c],X) X = [a,b,c] For Pop: ?- stack(X,Y,[a,b,c]) X = a, Y = [b,c]
12
Predicates for the Stack For Peek: ?- stack(X,_,[a,b,c]) X = a
13
Predicates for the Stack Member_stack: member_stack(Element,Stack) :- member(Element,Stack) ?- member_stack(a,[a,b,c]) yes
14
The ADT Queue Remember a queue is a first-in-first-out data structure. Elements are introduced on one end and extracted from the other end. Operators can be implemented as follows: empty_queue([]).
15
The ADT Queue Add an element to the queue: Element E is added to the queue (2 nd argument); the final queue is the 3 rd argument enqueue(E,[],[E]). enqueue(E,[H|T],[H|Tnew]) :- enqueue(E,T,Tnew). ?- enqueue(a,[],X) X = [a] ?- enqueue(b,[a],Y) Y = [a,b]
16
The ADT Queue Take an element off the queue: Extract the next element from the queue. The final queue is the 3 rd argument. dequeue(E,[E|T],T). ?- dequeue(X,[a,b,c],Y) X = a, Y = [b,c]
17
The ADT Queue To simply peek at the next element: dequeue(E,[E|T],_). ?- dequeue(X,[a,b,c],_) X = a
18
The ADT Queue To test if an element is a member of the queue: member_queue(Element,Queue) :- member(Element,Queue). ?- member_queue(a,[a,b,c]) yes
19
Prolog Cut to Control Search Abstract Data Types Practical Examples Summary
20
Binary Trees Represented by the function: tree(Element,Left,Right) tree(a,tree(b,[],[]),tree(c,[],[])). a bc
21
Binary Trees binary_tree([]). binary_tree(tree(Element,Left,Right)) :- binary_tree(Left), binary_tree(Right). Try: a b c d e
22
Binary Trees tree_member(X,tree(X,Left,Right)). tree_member(X,tree(Y,Left,Right)) :- tree_member(X,Left). tree_member(X,tree(Y,Left,Right)) :- tree_member(X,Right).
23
Minimum minimum(X,Y,X) :- X <= Y. minimum(X,Y,Y,) :- X > Y. ?- minimum(2,3,X). X = 2. ?- minimum(9,7,X). X = 7.
24
Arithmetic plus(X,Y,Z) :- Z = X + Y. factorial(1,1). factorial(0,1). factorial(N,F) :- (N1 = N-1), factorial(N1,F1), (F = N*F1)
25
Prolog Cut to Control Search Abstract Data Types Practical Examples Summary
26
The search for more answers can be stopped with the cut operator (!). We analyzed how to build a stack and a queue data structure in Prolog. Prolog facilitates implementing search programs.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.