Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Announcements We are almost done grading HW1 HW2 due today Download SWI Prolog! HW3 (Prolog) will be posted today, due on February 29 th Spring 16 CSCI.

Similar presentations


Presentation on theme: "1 Announcements We are almost done grading HW1 HW2 due today Download SWI Prolog! HW3 (Prolog) will be posted today, due on February 29 th Spring 16 CSCI."— Presentation transcript:

1 1 Announcements We are almost done grading HW1 HW2 due today Download SWI Prolog! HW3 (Prolog) will be posted today, due on February 29 th Spring 16 CSCI 4430, A Milanova

2 2 Last Class Bottom-up (LR) parsing Characteristic Finite State Machine (CFSM) SLR(1) parsing table Conflicts in SLR(1) LR parsing variants

3 Spring 16 CSCI 4430, A Milanova 3 Today’s Lecture Outline Logic Programming Logic Programming Concepts Prolog Language constructs: facts, queries, rules Prolog concepts: search tree, unification, backtracking, backward chaining Lists

4 4 Logic Programming and Prolog Read: Scott, Chapter 11.1, 11.2.1-4

5 Spring 16 CSCI 4430, A Milanova 5 Logic Programming Download and install SWI Prolog on laptop! J.R.Fisher’s Prolog Tutorial: http://www.cpp.edu/~jrfisher/www/prolog_tutorial/contents.html

6 Spring 16 CSCI 4430, A Milanova 6 Logic Programming Logic programming Logic programming is declarative programming Logic program states what (logic), not how (control) Programmer declares axioms In Prolog, facts and rules Programmer states a theorem, or a goal (the what) In Prolog, a query Language implementation determines how to use the axioms to prove the goal

7 7 Logic Programming Logic programming style is characterized by Database of facts and rules that represent logical relations. Computation is modeled as search (queries) over this database Manipulation of lists and use of recursion, in which it is very similar to the functional programming style Spring 16 CSCI 4430, A Milanova

8 8 Logic Programming Concepts A Horn Clause is: H  B 1,B 2,…,B n Antecedents (B’s): conjunction of zero or more terms in predicate calculus; this is the body of the horn clause Consequent (H): a term in predicate calculus Meaning of a Horn clause: H is true if B 1, B 2,..., and B n are all true When the antecedents B i are all true, we deduce that consequent H is true as well For example: rainy(seattle). cold(rochester). snowy(rochester)  rainy(rochester),cold(rochester).

9 Spring 16 CSCI 4430, A Milanova 9 Logic Programming Concepts Resolution Principle If two Horn clauses are such that A matches D 1, then we can replace D 1 with B 1,B 2,B 3,…, B m For example: A  B 1,B 2,B 3,…, B m C  D 1,D 2,D 3,…, D n C  B 1,B 2,B 3,…, B m,D 2,D 3,…, D n C  A,B D  C A D  A,B,C

10 Spring 16 CSCI 4430, A Milanova 10 Horn Clauses in Prolog In Prolog, a Horn clause is written h :- b 1,...,b n. Horn Clause is called clause Consequent is called goal or head Antecedents are called subgoals or tail Horn Clause with no tail is a fact E.g., rainy(seattle). Depends on no other conditions Horn Clause with a tail is a rule snowy(X) :- rainy(X),cold(X).

11 Horn Clauses in Prolog Clause is composed of terms Constants Number, e.g., 123, etc. Atoms e.g., seattle, rochester, rainy, foo In Prolog, atoms begin with a lower-case letter! Variables X, Foo, My_var In Prolog, variables must begin with upper-case letter! Structures consists of an atom, called a functor and a list of arguments rainy(seattle), snowy(X) 11

12 12 Horn Clauses in Prolog Variables may appear in the tail and head of a rule: c(X) :- h(X,Y). For all values of X, c(X) is true if there exist a value of Y such that h(X,Y) is true Call Y an auxiliary variable. Its value will be bound to make consequent true, but not reported by Prolog, because it does not appear in the head Spring 16 CSCI 4430, A Milanova

13 13 Lecture Outline Logic Programming Logic Programming Concepts Prolog Language constructs: facts, queries, rules Prolog concepts: search tree, unification, backtracking, backward chaining Lists

14 14 Prolog Program has a database of clauses i.e., facts and rules; the rules help derive more facts We add simple queries with constants, variables, conjunctions or disjunctions rainy(seattle). rainy(rochester). cold(rochester). snowy(X) :- rainy(X),cold(X). ? - rainy(C). ? – snowy(C). Spring 16 CSCI 4430, A Milanova

15 15 Facts likes(eve, pie).food(pie). likes(al, eve).food(apple). likes(eve, tom).person(tom). likes(eve, eve). The combination of the functor and its arity (i.e., its number of arguments) is called a predicate. functors constants

16 16 Queries likes(eve, pie).food(pie). likes(al, eve).food(apple). likes(eve, tom).person(tom). likes(eve, eve). ?-likes(al,eve).?-likes(al,Who). true.Who=eve. ?-likes(al,pie).?-likes(eve,W). false.W=pie ; ?-likes(eve,al).W=tom ; false.W=eve. query answer variable answer with variable binding force search for more answers Spring 16 CSCI 4430, A Milanova

17 17 Question likes(eve, pie).food(pie). likes(al, eve).food(apple). likes(eve, tom).person(tom). likes(eve, eve). ?-likes(eve,W). W = pie ; W = tom ; W = eve. Prolog gives us the answer precisely in this order: first W=pie then W=tom and finally W=eve. Can you guess why?

18 18 Harder Queries likes(eve, pie).food(pie). likes(al, eve). food(apple). likes(eve, tom).person(tom). likes(eve, eve). ?-likes(al,V), likes(eve,V). V=eve. ?-likes(eve,W), person(W). W=tom ?-likes(A,B). A=eve,B=pie ; A=al,B=eve ; A=eve,B=tom ; A=eve,B=eve. ?-likes(D,D). D=eve. and

19 19 Harder Queries likes(eve, pie).food(pie). likes(al, eve).food(apple). likes(eve, tom).person(tom). likes(eve, eve). ?-likes(eve,W),likes(W,V). W=eve,V=pie ; W=eve,V=tom ; W=eve,V=eve. ?-likes(eve,W),person(W),food(V). W=tom,V=pie ; W=tom,V=apple ?-likes(eve,V),(person(V);food(V)). V=pie ; V=tom or same binding Spring 16 CSCI 4430, A Milanova

20 20 Rules likes(eve, pie).food(pie). likes(al, eve).food(apple). likes(eve, tom).person(tom). likes(eve, eve). Add a rule to the database: rule1:-likes(eve,V),person(V). ?-rule1.

21 Spring 16 CSCI 4430, A Milanova 21 Rules likes(eve, pie).food(pie). likes(al, eve).food(apple). likes(eve, tom).person(tom). likes(eve, eve). rule2(V) :- likes(eve,V),person(V). ?-rule2(H). H=tom ?-rule2(pie). false. rule1 :- likes(eve,V),person(V).

22 Spring 16 CSCI 4430, A Milanova 22 Lecture Outline Logic Programming Logic Programming Concepts Prolog Language constructs: facts, queries, rules Prolog concepts: search tree, unification, backtracking, backward chaining Lists

23 Spring 16 CSCI 4430, A Milanova 23 Logical Semantics Prolog program consists of facts and rules Rules like snowy(X):- rainy(X),cold(X). correspond to logical formulas  X[snowy(X)  rainly(X) ^ cold(X)] /* For every X, X is snowy, if X is rainy and X is cold */ rainy(seattle). rainy(rochester). cold(rochester). snowy(X):-rainy(X),cold(X).

24 24 Logical Semantics A query such as ?- rainy(C). triggers resolution. Logical semantics does not impose restriction in the order of application of resolution rules C = seattleC = rochester C = rochesterC = seattle Spring 16 CSCI 4430, A Milanova rainy(seattle). rainy(rochester). cold(rochester). snowy(X):-rainy(X),cold(X).

25 25 Procedural Semantics ?- snowy(C) snowy(X) :- rainy(X),cold(X). First, find the first clause in the database whose head matches the query, in our case this is clause snowy(X) :- rainy(X), cold(X) Then, find a binding for X to make rainy(X) true; then, check if cold(X) is true with that binding If yes, report binding as successful Otherwise, backtrack to the binding of X, unbind and consider the next binding Prolog’s computation is well-defined procedurally by search tree, rule ordering, unification and backtracking rainy(seattle). rainy(rochester). cold(rochester). snowy(X):-rainy(X),cold(X). Spring 16 CSCI 4430, A Milanova

26 26 Question What does this query yield? ?- snowy(C). Answer: C = rochester ; C = troy. rainy(seattle). rainy(rochester). cold(rochester). snowy(X):-rainy(X),cold(X). snowy(troy). Spring 16 CSCI 4430, A Milanova

27 27 Procedural Semantics rainy(seattle). rainy(rochester). cold(rochester). snowy(X) :- rainy(X),cold(X). snowy(C) snowy(X) AND OR rainy(X) cold(X) rainy(seattle)rainy(rochester) cold(rochester) _C = _X X = seattle cold(seattle) fails; backtrack. X = rochester success Spring 16 CSCI 4430, A Milanova

28 28 Prolog Concepts: Search Tree snowy(C) snowy(X) AND OR rainy(X) cold(X) rainy(seattle)rainy(rochester) cold(rochester) rainy(seattle). rainy(rochester). cold(rochester). snowy(X):-rainy(X),cold(X). ?- snowy(C). OR levels: parent: goal (e.g., rainy(X) ) children: heads-of-clauses ( rainy(…) ) ORDER: from left to right AND levels: parent: goal (e.g., snowy(X) ) children: subgoals ( rainy(X), cold(X) ) ORDER: from left to right

29 Prolog Concepts: Unification At OR levels Prolog performs unification Unifies parent (goal), with child (head-of-clause) E.g., snowy(C) =snowy(X) success, _C = _X rainy(X)=rainy(seattle) success, X = seattle parents(alice,M,F) = parents(edward,victoria,albert) fail parents(alice,M,F) = parents(alice,victoria,albert) success, M = victoria, F = albert In Prolog, = denotes unification, not assignment! 29

30 30 Prolog Concepts: Unification A constant unifies only with itself E.g., alice=alice, but alice=edward fails Two structures unify if and only if (i) they have the same functor, (ii) they have the same number of arguments, and (iii) their arguments unify recursively E.g., rainy(X) = rainy(seattle) A variable unifies with anything. If the other thing has a value, then variable is bound to that value. If the other thing is an unbound variable, then the two variables are associated and if either one gets a value, both do. Spring 16 CSCI 4430, A Milanova

31 31 Prolog Concepts: Backtracking If at some point, a goal fails, Prolog backtracks to the last goal (i.e., last unification point) where there is an untried binding, undoes current binding and tries new binding (an alternative OR branch), etc. snowy(C) snowy(X) AND OR rainy(X) cold(X) rainy(seattle)rainy(rochester) cold(rochester) _C = _X X = seattle cold(seattle) fails; backtrack. rainy(seattle). rainy(rochester). cold(rochester). snowy(X):-rainy(X),cold(X). ?- snowy(C). Spring 16 CSCI 4430, A Milanova

32 32 Prolog Concepts: Backward Chaining Backward chaining: starts from goal, towards facts ? – snowy(rochester). snowy(rochester):- rainy(rochester), cold(rochester) rainy(rochester) ---------------------- snowy(rochester):- cold(rochester) cold(rocheter) ---------------------- snowy(rochester). Forward chaining: starts from facts towards goal ? – snowy(rochester). rainy(rochester) snowy(rochester):- rainy(rochester), cold(rochester) ----------------------------- cold(rochester) snowy(rochester):- cold(rochester) ----------------------------- snowy(rochester). Spring 16 CSCI 4430, A Milanova

33 Exercise Draw search tree for query. What are the bindings for C? Spring 16 CSCI 4430, A Milanova 33 takes(jane, his). takes(jane, cs). takes(ajit, art). takes(ajit, cs). classmates(X,Y):-takes(X,Z),takes(Y,Z). ?- classmates(jane,C).

34 Running Prolog Programs Enter the database of facts into a file. E.g., we can enter the classmates database into file classmates.pl At the interpreter prompt, load the file then run queries. E.g., ?- [classmates]. … true. ?- takes(ajit,C). C = art ; C = cs. 34

35 Spring 16 CSCI 4430, A Milanova 35 Lecture Outline Logic Programming Logic programming concepts Prolog Language constructs: facts, queries, rules Prolog concepts: search tree, unification, backtracking, backward chaining Lists

36 36 Lists listheadtail [a,b,c]a[b,c] [X,[cat],Y]X[[cat],Y] [a,[b,c],d]a[[b,c],d] [X | Y]XY a b c [ ] a b c d Spring 16 CSCI 4430, A Milanova

37 37 Lists: Unification Spring 16 CSCI 4430, A Milanova [ H1 | T1 ] = [ H2 | T2 ] Head H1 unifies with H2, possibly recursively Tail T1 unifies with T2, possibly recursively E.g., [ a | [b, c] ] = [ X | Y ] X = a Y = [b, c] NOTE: In Prolog, = denotes unification, not assignment!

38 38 Question [X,Y,Z] = [john, likes, fish] X = john, Y = likes, Z = fish [cat] = [X | Y] X = cat, Y = [ ] [[the, Y]|Z] = [[X, hare]|[is,here]] X = the, Y = hare, Z = [is, here] Spring 16 CSCI 4430, A Milanova

39 39 Lists: Unification Sequence of comma separated terms, or [ first term | rest_of_list ] [ [the | Y] | Z] = [ [X, hare] | [is, here] ] theY Z X hare [ ] is here [ ] Spring 16 CSCI 4430, A Milanova

40 40 Lists Unification look at the trees to see how this works! [ a, b, c ] = [ X | Y ] X = a, Y = [b,c]. [a | Z ] =? [ X | Y ] X = a, Y = Z. Spring 16 CSCI 4430, A Milanova

41 41 Improper and Proper Lists [1 | 2] versus [1, 2] 12 1 2[ ] Spring 16 CSCI 4430, A Milanova

42 42 Question. Can we unify these lists? [abc, Y] =? [ abc | Y ] abc Y[ ] abc Y Answer: No. There is no value binding for Y, that makes these two trees isomorphic What happens here?

43 43 ?- member(a,[a,b]). true. ?- member(a,[b,c]). false. ?- member(X,[a,b,c]). X = a ; X = b ; X = c ; false 1. member(A, [A | B] ). 2. member(A, [B | C]) :- member (A, C). Spring 16 CSCI 4430, A Milanova Member_of “Procedure”

44 44 Example ?- member(a,[b, c, X]). 1. member(A, [A | B]). 2. member(A, [B | C]) :- member (A, C). Spring 16 CSCI 4430, A Milanova

45 45 Member_of “Procedure” member(A, [A|B]). member(A, [B|C]) :- member (A,C). logical semantics: For every value assignment of A, B and C, we have member(A,[B|C]) if member(A,C); procedural semantics: Head of clause is procedure entry. Procedure body consists of calls within this procedure. Spring 16 CSCI 4430, A Milanova

46 46 Prolog Search Tree (OR levels only) member(X,[a,b,c]) member(X,[b,c]) A=X,B=a,C=[b,c] member(X,[c]) A’=X,B’=b,C’=[c] fail member(X,[ ]) A”=X B”=c, C”=[ ] 1. member(A, [A | B] ). 2. member(A, [B | C]) :- member (A, C). X=a success A=X=a,B=[b,c] X=b success A’=X=b,B’=[c]X=c success A”=X=c,B”=[ ]

47 47 Another Search Tree member(a, [b,c,X]) fail, a can’t unify with b member(a,[c, X]) A=a, B = b, C = [c,X] fail, a can’t unify with c A’=a, B’=c, C’=[X] member(a,[X]). A’’=X=a, B”= [ ] success member(a,[ ]) A’’=a,X=B”,C”= [ ] fail, ditto fail, can’t unify [ ] with a list 1. member(A, [A | B] ). 2. member(A, [B | C]) :- member (A, C). Spring 16 CSCI 4430, A Milanova

48 48 Append “Procedure” append([ ], A, A). append([A|B], C, [A|D]) :- append(B,C,D). Build a list ?- append([a],[b],Y). Y = [ a,b ] Break a list into constituent parts ?- append(X,[b],[a,b]). X = [ a ] ?- append([a],Y,[a,b]). Y = [ b ] Spring 16 CSCI 4430, A Milanova

49 49 More Append ? - append(X,Y,[a,b]). X = [ ] Y = [a,b] ; X = [a] Y = [b] ; X = [a,b] Y = [ ] ; false. Spring 16 CSCI 4430, A Milanova

50 50 More Append Generating an unbounded number of lists ? - append(X,[b],Y). X = [ ] Y = [b] ; X = [ _169] Y = [ _169, b] ; X = [ _169, _170 ] Y = [ _169, _170, b] ; etc. Spring 16 CSCI 4430, A Milanova

51 51 Group Exercise Create a file lists.pl, add member and append to it Write a few queries with member and append Now add another “procedure” brackets which encloses each element in brackets e.g., brackets([a,[b],c],R). returns R = [[a],[[b]],[c]] 1. member(A, [A | B]). 2. member(A, [B | C]) :- member (A, C). 1. append([ ], A, A). 2. append([A|B], C, [A|D]) :- append(B,C,D). Spring 16 CSCI 4430, A Milanova


Download ppt "1 Announcements We are almost done grading HW1 HW2 due today Download SWI Prolog! HW3 (Prolog) will be posted today, due on February 29 th Spring 16 CSCI."

Similar presentations


Ads by Google