1 / 39

Introduction to PROLOG ME 409 Lab - 1

Introduction to PROLOG ME 409 Lab - 1 . Introduction to PROLOG . PRO gramming in LOG ic Used for symbolic and non-numerical computation Has a built in intelligent search mechanism Can handle complex problems in compact programs

Download Presentation

Introduction to PROLOG ME 409 Lab - 1

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to PROLOGME 409Lab - 1

  2. Introduction to PROLOG • PROgramming in LOGic • Used for symbolic and non-numerical computation • Has a built in intelligent search mechanism • Can handle complex problems in compact programs • Writing a program in Prolog means writing facts and rules which together comprise knowledge base • Facts and rules use predicates which represent relationships among data objects.

  3. Data Objects

  4. 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’

  5. 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

  6. Data Objects • Structures • Objects that have many components • Components can themselves be structures • Functor is used to combine components into single structure date(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)

  7. 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

  8. 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

  9. 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

  10. Clauses • Recursive rules • Example • Rule 1: predecessor(X,Z):- parent (X,Z). • Rule 2: predecessor(X,Z):- parent(X,Y),predecessor(Y,Z)

  11. 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.

  12. 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

  13. 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

  14. Example Program • And-Or Graph

  15. 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

  16. How PROLOG works ? predecessor(tom,pat).

  17. 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.

  18. 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.

  19. 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

  20. 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. • goalpred(arg1, ..., argn) gives rise to subgoals cond1, ..., condm to be satisfied from left to right.

  21. 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

  22. 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)

  23. 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.

  24. 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| [ ] ]

  25. 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.

  26. 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).

  27. 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).

  28. 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

  29. 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

  30. 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] ]

  31. L L1 S L3 L2 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

  32. 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]; …

  33. 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)

  34. 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.

  35. 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 – c Interpreted as (a-b)-c and not as a-(b-c)

  36. Arithmetic • ‘=‘ is matching operator (it does not evaluate on its own) • Matches right hand side with left hand side • Examples: 1. ? X=1+2 X=1+2 2. ? 1+3=2+2 No 3. ?X+3=4+Y X=4 Y=3

  37. Arithmetic • ‘is’ operator forces evaluation of expression on RHS forcing instantiation of values on LHS to the evaluated value • Examples: • X is 1+2 X=3 • Y is 5 mod 2 Y=1 • At the time of evaluation all arguments must be instantiated to numbers

  38. 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

  39. Example • Calculating number of elements in a list • length(List,N) % N is the number of elements in List length([ ], 0). %R1 length( [Head|Tail], N):- length(Tail, N1), N is 1+N1. %R2 • Query: ? length ([a,b,c,d],N). N = 4

More Related