Logic Programming: Prolog Logic Programming: Prolog Course: CS40002 Course: CS40002 Instructor: Dr. Pallab Dasgupta Pallab Dasgupta Instructor: Dr. Department of Computer Science & Engineering Department of Computer Science & Engineering Indian Institute of Technology Kharagpur Kharagpur Indian Institute of Technology
Basics Basics � The notion of instantiation The notion of instantiation � likes( harry harry, school ) , school ) likes( likes( ron ron, broom ) , broom ) likes( likes( harry harry, X) : , X) :- - likes( likes( ron ron, X ) , X ) likes( � Consider the following goals: Consider the following goals: � ?- - likes( likes( harry harry, broom ) , broom ) ? ?- - likes( likes( harry harry, Y ) , Y ) ? ?- - likes( Z, school ) likes( Z, school ) ? ?- - likes( Z, Y ) likes( Z, Y ) ? CSE, IIT Kharagpur Kharagpur CSE, IIT 2
Family Tree Example Family Tree Example offspring( Y, X ) :- - parent( X, Y ). parent( X, Y ). offspring( Y, X ) : mother( X, Y ) :- - parent( X, Y ), female( X ). mother( X, Y ) : parent( X, Y ), female( X ). grandparent( X, Z ) :- - grandparent( X, Z ) : parent( X, Y ), parent( Y, Z ). parent( X, Y ), parent( Y, Z ). sister( X, Y ) :- - parent( Z, X ), parent( Z, Y ), parent( Z, X ), parent( Z, Y ), sister( X, Y ) : female( X ), different( X, Y ). female( X ), different( X, Y ). predecessor( X, Z ) :- - parent( X, Z ). parent( X, Z ). predecessor( X, Z ) : predecessor( X, Z ) :- - predecessor( X, Z ) : parent( X, Y ), predecessor( Y, Z ). parent( X, Y ), predecessor( Y, Z ). CSE, IIT Kharagpur Kharagpur CSE, IIT 3
Monkey and Banana Example Monkey and Banana Example � There is a monkey at the door of a room. There is a monkey at the door of a room. � � In the middle of the room a banana hangs In the middle of the room a banana hangs � from the ceiling. The monkey wants it, but from the ceiling. The monkey wants it, but cannot jump high enough from the floor. cannot jump high enough from the floor. � At the window of the room there is a box that At the window of the room there is a box that � the monkey can use. the monkey can use. CSE, IIT Kharagpur Kharagpur CSE, IIT 4
Monkey and Banana Example Monkey and Banana Example � The monkey can perform the following The monkey can perform the following � actions: actions: � Walk on the floor Walk on the floor � � Climb the box Climb the box � � Push the box around Push the box around (if it is beside the box) (if it is beside the box) � � Grasp the banana if it is standing on the Grasp the banana if it is standing on the � box directly under the banana box directly under the banana � We define the state as a 4 We define the state as a 4- -tuple tuple: : � (monkey- -at, on at, on- -floor, box floor, box- -at, has at, has- -banana) banana) (monkey CSE, IIT Kharagpur Kharagpur CSE, IIT 5
The program The program The order of the rules is important (Why?) The order of the rules is important (Why?) move( state( middle, onbox onbox, middle, , middle, hasnot hasnot ), ), move( state( middle, grasp, state( middle, onbox onbox, middle, has)). , middle, has)). grasp, state( middle, move( state( P, onfloor onfloor, P, H ), , P, H ), move( state( P, climb, state( P, onbox onbox, P, H )). , P, H )). climb, state( P, move( state( P1, onfloor onfloor, P1, H ), , P1, H ), move( state( P1, push( P1, P2 ), state( P2, onfloor onfloor, P2, H)). , P2, H)). push( P1, P2 ), state( P2, move( state( P1, onfloor onfloor, B, H ), , B, H ), move( state( P1, walk( P1, P2 ), state( P2, onfloor onfloor, B, H )). , B, H )). walk( P1, P2 ), state( P2, CSE, IIT Kharagpur Kharagpur CSE, IIT 6
The program The program canget( state( _, _, _, has )). ( state( _, _, _, has )). canget canget( State1 ) : ( State1 ) :- - canget move( State1, Move, State2 ), move( State1, Move, State2 ), canget( State2 ). ( State2 ). canget ?- - canget canget( ( ? state( atdoor atdoor, , onfloor onfloor, , atwindow atwindow, , hasnot hasnot )). )). state( CSE, IIT Kharagpur Kharagpur CSE, IIT 7
Lists Lists � Lists can be written as: Lists can be written as: � [ Item1, Item2, … ] [ Item1, Item2, … ] or [ Head | Tail ] or [ Head | Tail ] or [ Item1, Item2, … | Others ] or [ Item1, Item2, … | Others ] [a, b, c] = [a | [ b,c] ] = [a,b | [c] ] = [a,b,c | [ ] ] [a, b, c] = [a | [ b,c] ] = [a,b | [c] ] = [a,b,c | [ ] ] � Items can be lists as well Items can be lists as well – – � [ [a,b], c, [d, [e,f] ] ] [ [a,b], c, [d, [e,f] ] ] Head of the above list is the list [a,b] Head of the above list is the list [a,b] CSE, IIT Kharagpur Kharagpur CSE, IIT 8
List examples List examples Membership: Membership: member( X, [X, Tail] ). member( X, [X, Tail] ). member( X, [Head, Tail] ) :- - member( X, [Head, Tail] ) : member( X, Tail ). member( X, Tail ). Concatenation: Concatenation: conc( [ ], L, L ). ( [ ], L, L ). conc conc( [X | L1], L2, [X | L3] ) : ( [X | L1], L2, [X | L3] ) :- - conc conc( L1, L2, L3 ). ( L1, L2, L3 ). conc CSE, IIT Kharagpur Kharagpur CSE, IIT 9
List examples List examples Adding in front: Adding in front: add( X, L, [X | L] ). add( X, L, [X | L] ). Deletion: Deletion: del( X, [X | Tail], Tail ). del( X, [X | Tail], Tail ). del( X, [Y | Tail], [Y | Tail1] ) :- - del( X, [Y | Tail], [Y | Tail1] ) : del( X, Tail, Tail1). del( X, Tail, Tail1). CSE, IIT Kharagpur Kharagpur CSE, IIT 10
List examples List examples Sublist: : Sublist sublist(S, L) : (S, L) :- - conc conc(L1,L2,L), (L1,L2,L), conc conc(S,L3,L2). (S,L3,L2). sublist Permutation: Permutation: permutation( [ ], [ ] ). permutation( [ ], [ ] ). permutation( [X | L], P ) :- - permutation( [X | L], P ) : permutation( L, L1 ), insert( X, L1, P ). permutation( L, L1 ), insert( X, L1, P ). or or permutation( [ ], [ ] ). permutation( [ ], [ ] ). permutation( L, [X | P] ) :- - permutation( L, [X | P] ) : del( X, L, L1 ), permutation( L1, P ). del( X, L, L1 ), permutation( L1, P ). CSE, IIT Kharagpur Kharagpur CSE, IIT 11
Arithmetic and Logical operators Arithmetic and Logical operators � We have +, We have +, - -, *, /, mod , *, /, mod � � The “ The “is is” operator forces evaluation ” operator forces evaluation � � ? ?- - X is 3/2. X is 3/2. – – will be answered by X=1.5 will be answered by X=1.5 � � We have We have � � X > Y, X < Y, X >= Y, X =< Y X > Y, X < Y, X >= Y, X =< Y � � X =:= Y X =:= Y – X and Y are equal X and Y are equal – � � X = X =\ \= Y = Y – X and Y are not equal X and Y are not equal – � CSE, IIT Kharagpur Kharagpur CSE, IIT 12
Examples Examples � GCD of two numbers GCD of two numbers � gcd( X, X, X ). ( X, X, X ). gcd gcd( X, Y, D ) : ( X, Y, D ) :- - gcd X < Y, Y1 is Y – – X, X, gcd gcd( X, Y1, D ). ( X, Y1, D ). X < Y, Y1 is Y � Length of a list Length of a list � length( [ ], 0 ). length( [ ], 0 ). length( [ _ | Tail ], N ) :- - length( [ _ | Tail ], N ) : length( Tail, N1 ), N is 1 + N1 length( Tail, N1 ), N is 1 + N1 CSE, IIT Kharagpur Kharagpur CSE, IIT 13
Eight Queens Problem Eight Queens Problem solution( Queens ) :- - solution( Queens ) : permutation( [1,2,3,4,5,6,7,8], Queens ), permutation( [1,2,3,4,5,6,7,8], Queens ), safe( Queens ). safe( Queens ). permutation( [ ], [ ] ). permutation( [ ], [ ] ). permutation( [Head | Tail], Permlist Permlist ) : ) :- - permutation( [Head | Tail], permutation( Tail, PermTail PermTail ), ), permutation( Tail, del( Head, Permlist Permlist, , PermTail PermTail ). ). del( Head, CSE, IIT Kharagpur Kharagpur CSE, IIT 14
Eight Queens Problem (Contd.) Eight Queens Problem (Contd.) safe( [ ] ). safe( [ ] ). safe( [Queen | Others] ) :- - safe( [Queen | Others] ) : safe( Others ), noattack noattack( Queen, Others, 1 ). ( Queen, Others, 1 ). safe( Others ), noattack( _, [ ], _ ). ( _, [ ], _ ). noattack noattack( Y, [ Y1 | ( Y, [ Y1 | Ylist Ylist ], ], Xdist Xdist ) : ) :- - noattack Y1 – – Y = Y =\ \= = Xdist Xdist, Y , Y – – Y1 = Y1 =\ \= = Xdist Xdist, , Y1 Dist1 is Xdist Xdist + 1, + 1, noattacks noattacks( Y, ( Y, Ylist Ylist, Dist1 ). , Dist1 ). Dist1 is CSE, IIT Kharagpur Kharagpur CSE, IIT 15
Cuts – – for controlling backtracking for controlling backtracking Cuts C :- - P, Q, R, P, Q, R, ! !, S, T, U. , S, T, U. C : C :- - V. V. C : A :- - B, C, D B, C, D A : ?- - A A ? � Backtracking within the goal list P, Q, R Backtracking within the goal list P, Q, R � � As soon as the cut is reached: As soon as the cut is reached: � � All alternatives of P, Q, R are suppressed. All alternatives of P, Q, R are suppressed. � � The clause The clause C: C:- - V V will also be discarded will also be discarded � � Backtracking possible within S, T, U. Backtracking possible within S, T, U. � � No effect within No effect within A : A :- - B, C, D B, C, D, that is, , that is, � backtracking within B, C, D remains active. backtracking within B, C, D remains active. CSE, IIT Kharagpur Kharagpur CSE, IIT 16
Recommend
More recommend