Prolog Programming Department of Computer Science Engineering Indian

  • Slides: 15
Download presentation
Prolog Programming Department of Computer Science & Engineering Indian Institute of Technology Kharagpur CSE,

Prolog Programming Department of Computer Science & Engineering Indian Institute of Technology Kharagpur CSE, IIT Kharagpur

Basics • The notion of instantiation likes( harry, school ) likes( ron, broom )

Basics • The notion of instantiation likes( harry, school ) likes( ron, broom ) likes( harry, X) : - likes( ron, X ) • Consider the following goals: ? - likes( harry, broom ) ? - likes( harry, Y ) ? - likes( Z, school ) ? - likes( Z, Y ) CSE, IIT Kharagpur 2

Family Tree Example offspring( Y, X ) : - parent( X, Y ). mother(

Family Tree Example offspring( Y, X ) : - parent( X, Y ). mother( X, Y ) : - parent( X, Y ), female( X ). grandparent( X, Z ) : - parent( X, Y ), parent( Y, Z ). sister( X, Y ) : - parent( Z, X ), parent( Z, Y ), female( X ), different( X, Y ). predecessor( X, Z ) : - parent( X, Z ). predecessor( X, Z ) : - parent( X, Y ), predecessor( Y, Z ). CSE, IIT Kharagpur 3

Monkey and Banana Example • • There is a monkey at the door into

Monkey and Banana Example • • There is a monkey at the door into a room. In the middle of the room a banana is hanging from the ceiling. The monkey wants the banana, but he cannot jump high enough from the floor. At the window of the room there is a box that the monkey can use. The monkey can perform the following actions: – – • Walk on the floor Climb the box Push the box around (if it is already at the box) Grasp the banana if standing on the box directly under the banana We define the state as a 4 -tuple: (monkey-at, on-floor, box-at, has-banana) CSE, IIT Kharagpur 4

The program (Ordering of clauses is important – why? ) move( state( middle, onbox,

The program (Ordering of clauses is important – why? ) move( state( middle, onbox, middle, hasnot ), grasp, state( middle, onbox, middle, has )). move( state( P, onfloor, P, H ), climb, state( P, onbox, P, H )). move( state( P 1, onfloor, P 1, H ), push( P 1, P 2 ), state( P 2, onfloor, P 2, H )). move( state( P 1, onfloor, B, H ), walk( P 1, P 2 ), state( P 2, onfloor, B, H )). canget( state( _, _, _, has )). canget( State 1 ) : - move( State 1, Move, State 2 ), canget( State 2 ). ? - canget( state( atdoor, onfloor, atwindow, hasnot )). CSE, IIT Kharagpur 5

Lists in Prolog • Lists can be written as: [ Item 1, Item 2,

Lists in Prolog • Lists can be written as: [ Item 1, Item 2, … ] or [ Head | Tail ] or [ Item 1, Item 2, … | Others ] [a, b, c] = [a | [b, c]] = [a, b | [c]] = [a, b, c | []] • Items can be lists as well – [[a, b], c, [d, [e, f]]] Head of the above list is the list [a, b] CSE, IIT Kharagpur 6

List examples Membership: member( X, [X, Tail] ). member( X, [Head, Tail] ) :

List examples Membership: member( X, [X, Tail] ). member( X, [Head, Tail] ) : - member( X, Tail ). Concatenation: conc( [], L, L ). conc( [X | L 1], L 2, [X | L 3] ) : - conc( L 1, L 2, L 3 ). Adding in front: add( X, L, [X | L] ). Deletion: del( X, [X | Tail], Tail ). del( X, [Y | Tail], [Y | Tail 1] ) : - del( X, Tail 1). CSE, IIT Kharagpur 7

List examples (contd. . . ) Sublist: sublist( S, L ) : - conc(

List examples (contd. . . ) Sublist: sublist( S, L ) : - conc( L 1, L 2, L ), conc( S, L 3, L 2 ). Permutation: permutation( [], [] ). permutation( [X | L], P ) : permutation( L, L 1 ), insert( X, L 1, P ). or permutation( [], [] ). permutation( L, [X | P] ) : del( X, L, L 1 ), permutation( L 1, P ). CSE, IIT Kharagpur 8

Arithmetic and Logical operators • We have +, -, *, /, mod – The

Arithmetic and Logical operators • We have +, -, *, /, mod – The “is” operator forces evaluation – ? - X is 3/2. -- will be answered by X=1. 5 • We have – X > Y, X < Y, X >= Y, X =< Y – X =: = Y -- indicating that X and Y are equal – X == Y – indicating that X and Y are not equal CSE, IIT Kharagpur 9

Examples • GCD of two numbers gcd( X, X, X ). gcd( X, Y,

Examples • GCD of two numbers gcd( X, X, X ). gcd( X, Y, D ) : X < Y, Y 1 is Y – X, gcd( X, Y 1, D ). • Length of a list length( [], 0 ). length( [ _ | Tail], N ) : length( Tail, N 1 ), N is 1 + N 1 CSE, IIT Kharagpur 10

Eight Queens Problem solution( Queens ) : permutation( [1, 2, 3, 4, 5, 6,

Eight Queens Problem solution( Queens ) : permutation( [1, 2, 3, 4, 5, 6, 7, 8], Queens ), safe( Queens ). permutation( [], [] ). permutation( [Head | Tail], Permlist ) : permutation( Tail, Perm. Tail ), del( Head, Permlist, Perm. Tail ). safe( [] ). safe( [Queen | Others] ) : - safe( Others ), noattack( Queen, Others, 1 ). noattack( _, [], _ ). noattack( Y, [Y 1 | Ylist], Xdist ) : Y 1 – Y == Xdist, Y – Y 1 == Xdist, Dist 1 is Xdist + 1, noattacks( Y, Ylist, Dist 1 ). CSE, IIT Kharagpur 11

Cuts – for controlling backtracking C : - P, Q, R, !, S, T,

Cuts – for controlling backtracking C : - P, Q, R, !, S, T, U. C : - V. A : - B, C, D ? - A • • Backtracking will be possible within the goal list P, Q, R However, as soon as the cut is reached: – – All alternative solutions of the goal list P, Q, R are suppressed. The alternative clause C: - V will also be discarded Backtracking will be possible within the goal list S, T, U. This will have no effect within the clause A : - B, C, D, which means that automatic backtracking within the goal list B, C, D will remain active. CSE, IIT Kharagpur 12

Examples • Finding the maximum of two numbers If X >= Y then Max

Examples • Finding the maximum of two numbers If X >= Y then Max = X, otherwise Max = Y. max( X, Y, X ) : - X >= Y, !. max( X, Y, Y ). • Adding an element into a list without duplication add( X, L, L ) : - member( X, L ), !. add( X, L, [X | L] ). CSE, IIT Kharagpur 13

Negation as failure • Frodo likes all jewellery except rings likes( frodo, X )

Negation as failure • Frodo likes all jewellery except rings likes( frodo, X ) : - ring( X ), !, fail. likes( frodo, X ) : - jewellery( X ). • The “different” predicate: different( X, X ) : - !, fail. different( X, Y ). CSE, IIT Kharagpur 14

Quicksort quicksort( [], [] ). quicksort( [X | Tail], sorted ) : split( X,

Quicksort quicksort( [], [] ). quicksort( [X | Tail], sorted ) : split( X, Tail, Small, Big ), quicksort( Small, Sorted. Small ), quicksort( Big, Sorted. Big ), conc( Sorted. Small, [X | Sorted. Big], Sorted ). split( X, [], [] ). split( X, [Y | Tail], [Y | Small], Big ) : - gt( X, Y ), !, split( X, Tail, Small, Big ). split( X, [Y | Tail], Small, [Y | Big] ) : - split( X, Tail, Small, Big ). CSE, IIT Kharagpur 15