introduction to prolog me 409 lab - 1. introduction to prolog

39
Introduction to PROLOG ME 409 Lab - 1

Upload: laila-alsbury

Post on 14-Dec-2015

245 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG

Introduction to PROLOGME 409Lab - 1

Page 2: Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG

Introduction to PROLOG

Introduction to PROLOG

Page 3: Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG

Data Objects

Page 4: Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG

Data Objects

Atoms Strings of letters, digits, underscore character

starting with lower case letter:

sarah_jones, x25, x__y, x_yAB String of special characters:

<-->, ===>, …. Strings of characters enclosed in single quotes:

‘India’, ‘Tom’

Page 5: Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG

Data Objects

Numbers Include integers and real numbers

1, 3131, -0.0035, 3.14 Variables

String of letters, digits and underscore characters that starts either with an upper-case letter or with an underscore:Y, Child, _a23, Student_List

Page 6: Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG

Data Objects

Structures Objects that have many components Components can themselves be structures Functor is used to combine components into

single structuredate(1, jan, 2007), date(Date, Month, 2007)date(31, cat, -4.3), segment(point(1,1),point(3,3))

Functors are recognized by: Name Number of arguments (Arity)

Page 7: Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG

Data Objects

Predicate A predicate consists of a head and a number of

arguments Is a function which returns true/false For example:

father(sam, pat). %sam is father of pat

Page 8: Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG

Clauses

Facts Declare things that are unconditionally true Have a head and empty body Examples:

parent( tom, bob). %tom is parent of bob likes(joe,dogs). % joe likes dogs

Page 9: Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG

Clauses

Rules Declare things (predicates) that are true

depending on a given condition Have a non-empty body Example: mother(X,Y):-parent(X,Y), female(X).

X is mother of Y if X is parent of Y and X is female

Page 10: Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG

Clauses

Recursive rules Example

Rule 1: predecessor(X,Z):- parent (X,Z). Rule 2: predecessor(X,Z):-

parent(X,Y),predecessor(Y,Z)

Page 11: Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG

Clauses

Queries Means of asking the program what things are true Have empty head Example: ? parent(tom, bob).

Is tom parent of bob ? Example: ? parent( tom, X).

Find X such that tom is parent of X.

Page 12: Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG

Matching

Two terms (eg. predicate instance) will match if: They are identical, or The variables in the terms should be instantiated such that

after substitution of variables by these objects the terms become identical

Example: ?date(D,M,2007)=date(10,jan,Y)

D=10

M=jan

Y=2007

Page 13: Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG

Example Program

% Facts:

parent(pam,bob).

parent(tom,bob).

parent(tom,liz).

parent(bob,ann).

parent(bob,pat).

parent(pat,jim).

% Rules:

predecessor(X,Z) :- parent(X,Z). % R1

predecessor(X,Z) :- parent(X,Y), predecessor(Y,Z). % R2

Page 14: Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG

Example Program

And-Or Graph

Page 15: Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG

Example Program

Queries and Output ?predecessor(tom,pat).

Yes. ?predecessor(bob,X).

X=ann;

X=pat;

X=jim;

No. “ ; “ is used to generate other (different) solutions

Page 16: Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG

How PROLOG works

? predecessor(tom,pat).

Page 17: Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG

PROLOG execution summary

At each stage there is a list of goals to be satisfied. PROLOG selects the leftmost subgoal and searches

for a rule/fact head which matches it. The matching process can bind variables. The first subgoal is then replaced by the body of the

matching rule/fact, yielding a new list of subgoals. If there are no (more) matching rule/fact (subgoal

fails!)

or if the user asks for the next solution, PROLOG backtracks.

Page 18: Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG

Program Execution Summary

Unless all instantiation fails for a given rule, next rule is not explored.

System remembers where call rests in AND/OR graph and what instantiations are there for each term.

The most recent choice of rule/fact is undone, including any variable bindings which were made when the head was matched.

PROLOG searches for an alternative matching rule/fact.

Page 19: Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG

Declarative Meaning

A goal G is true if and only if there is a clause C in the program such that C

matches G, or There is a clause instance I of C such that

the head of I is identical to G, and all the goals in the body of I are true

P:-Q,R. % P is true if Q and R are ture

Page 20: Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG

Procedural Meaning

Specifies how Prolog answer questions

Procedural meaning of a “call”:

First match all arguments argi with corresponding patterns pati.

Then perform calls cond1, ..., condm in that order. goal pred(arg1, ..., argn) gives rise to subgoals

cond1, ..., condm to be satisfied from left to right.

Page 21: Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG

Procedural meaning big(bear). % clause 1 big(elephant). % clause 2 small(cat). % clause 3

brown( bear). % clause 4 black( cat). % clause 5 gray( elephant). % clause 6

dark( Z) :- black( Z) % clause 7: anything black is dark dark( Z) :- brown( Z) % clause 8: anything brown is dark

Page 22: Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG

Procedural Meaning ?- dark( X), big( X).

Execution trace: (1) Initial goal list: dark( X), big( X). (2) Scan the program from top to bottom looking for a clause

whose head matches the first goal dark( X). Clause 7 is found, which is

dark( Z) :- black( Z).Replace the first goal by the instantiated body of clause 7, giving a new goal list:black( X), big( X).

(3) Scan the program to find a match with black( X). Clause 5 is found: black (cat). This clause has no body, so the goal list properly instantiated, shrinks to:

big( cat)

Page 23: Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG

Procedural Meaning

(4) Scan the program for the goal big( cat). No clause is found. Therefore it backtracks to step(3) and undo the instantiation X = cat. Now the goal list is again: black (X), big( X)Continue scanning the program below clause 5. No clause is found. Therefore backtrack to step(2) and continue scanning below clause 7. Clause 8 is found:

dark (Z) :- brown( Z) Replace the first goal in the goal list by brown (X), giving:

brown( X), big( X) (5) Scan the program to match brown( X), finding brown (bear).

This clause has no body, so the goal list shrinks to: big( bear)

(6) Scan the program to find clause with big( bear). This is given as fact in clause 1, and it has no body so the list shrinks to empty. This indicates successful termination, and the corresponding variable instantiation is:X = bear.

Page 24: Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG

List

List is a sequence of any number of items Example :

[mech, elec, civil, aero, cse, john, [1,2], [], [X]] List consists of two parts i.e L = [Head|Tail] :

Head – First item of the list Tail – Remaining part of the list (tail itself is a list)

Empty list is also a list ( [ ] ) Lists are handled in Prolog as trees List1=[a, b, c]

[a, b, c] = [a | [b,c] ] = [a, b | [c] ] = [a, b, c| [ ] ]

Page 25: Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG

Membership member(X,L) : X is an object that occurs in L X is a member of list L if either:

X is the head of L. or X is a member of the tail of L.

member( X, [X| Tail] ).member( X, [Head| Tail] ) :-

member( X, Tail ). Example:?- member( a, [a, b, c]).Yes.?- member( [b, c], [a, [b, c]]).Yes.?- member(b, [a, [b, c]]).No.

Page 26: Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG

Concatenation

conc(L1,L2,L3) : L3 is the concatenation of L1 & L2

If the first argument is empty list then second and third arguments must be the same list.

conc([ ], L, L). If the first argument is non-empty list then it

can be represented as [X|L1]. Then the result of concatenation will be [X|L3] where L3 is the concatenation of L1 and L2.

conc([X|L1], L2, [X|L3]) :- conc(L1, L2, L3).

Page 27: Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG

Concatenation

Examples: ?- conc( [a,[b,c],d], [a, [ ],b], L).

L = [a, [b,c], d, a, [ ], b] ?- conc( Before, [feb| After], [ jan, feb, mar]).

Before = [ jan ]

After = [ mar ]

Note: Refer to rule -

conc([X|L1], L2, [X|L3]) :- conc(L1, L2, L3).

Page 28: Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG

Deleting an item del(X,L,L1) : L1 is equal to L with the first occurrence of item X

removed. If X is the head of the list then the result after deletion is the tail

of the list.del( X, [X| Tail], Tail).

If X is in the tail then it is deleted from there.del(X, [Y| Tail], [Y| Tail1]) :- del(X, Tail, Tail1)

Example: ?- del(a, [a, b, a, c], L).L = [b, a, c];L = [a, b, c];no

Page 29: Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG

Inserting an item insert(X,L,L1) : L1 is any list such that deleting X

from L1 gives L. insert( X, List, BiggerList) :- del( X, BiggerList, List).Example:?- insert( a, [ b, c, d], L).L = [a, b, c, d];L = [b, a, c, d];L = [b, c, a, d];L = [b, c, d, a];no

Page 30: Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG

Adding an item

add(X,L,L1) : L1 is the list obtained by adding X to L. X is added in front of the list L.

The resulting list L1 is simply [X|L]. add(X,L,[X|L]). Example:

? add(4.2, [1, a, f, [2] ], L1).

L1 = [4.2, 1, a, f, [2] ]

Page 31: Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG

Sublist

S is a sublist of L if: L can be decomposed into two lists, L1 and L2, & L2 can be decomposed into two lists, S and some

L3

L

L2

L1 S L3

Page 32: Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG

Sublist sublist(S,L):- conc(L1,L2,L), conc(S,L3,L2) Example:

? sublist([b,c],[a,b,c,d,e]).Yes

? sublist (S, [a,b,c]).S = [ ];S = [a];S = [a,b];S = [a,b,c];S = [ ];S= [b];…

Page 33: Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG

Operators Some of the operators are in-built in Prolog. Some predefined arithmetic operators:

+ addition - subtraction * multiplication / division mod modulo, the remainder of integer division

Precedence of operator decides the correct interpretation of expressions.

Operator with highest precedence is the principle functor of the term.

Operators are functors. 3+4 = 3+4 ( 3+4 ≠ 7)

Page 34: Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG

Operators

New operators can be defined by inserting into special kind of clauses called directives. Example:

:-op(600,xfx,<==>) Three group of operators:

infix: xfx, xfy, yfx prefix: fx, fy postfix: xf, yf

These definition helps in unambiguous interpretation of expressions which have sequence of binary operators.

Page 35: Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG

Operators x represents an argument whose precedence must be

strictly lower than that of operator y represents an argument whose precedence is lower

or equal to that of operator Precedence of an argument in parentheses or

unstructured object is zero Precedence of a structured argument is equal to the

precedence of its principal functor Example:

:-op(500,yfx,- ) a – b – cInterpreted as (a-b)-c and not as a-(b-c)

Page 36: Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG

Arithmetic ‘=‘ is matching operator (it does not evaluate on its

own) Matches right hand side with left hand side Examples:

1. ? X=1+2X=1+2

2. ? 1+3=2+2No

3. ?X+3=4+YX=4Y=3

Page 37: Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG

Arithmetic

‘is’ operator forces evaluation of expression on RHS forcing instantiation of values on LHS to the evaluated value

Examples:1. X is 1+2

X=3

1. Y is 5 mod 2

Y=1

At the time of evaluation all arguments must be instantiated to numbers

Page 38: Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG

Arithmetic

Values of arithmetic expressions can be compared by arithmetic operators: X<Y :- X is less than Y X>Y :- X is greater than Y X >=Y :- X is greater than or equal to Y X =<Y :- X is less than or equal to Y X =:= Y :- the values of X and Y are equal X =\= Y :- the values of X and Y are not equal

Page 39: Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG

Example

Calculating number of elements in a list length(List,N) % N is the number of elements

in Listlength([ ], 0). %R1length( [Head|Tail], N):-

length(Tail, N1),N is 1+N1. %R2

Query: ? length ([a,b,c,d],N). N = 4