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.

Slides:



Advertisements
Similar presentations
Chapter 11 :: Logic Languages
Advertisements

Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG.
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
1 Logic Programming. 2 A little bit of Prolog Objects and relations between objects Facts and rules. Upper case are variables. parent(pam, bob).parent(tom,bob).
Logic Languages Source: Scott, Michael Programming Language Pragmatics 3rd Ed.
CS 330 Programming Languages 12 / 02 / 2008 Instructor: Michael Eckmann.
COP4020 Programming Languages Logic programming and Prolog Prof. Xin Yuan.
MB: 2 March 2001CS360 Lecture 31 Programming in Logic: Prolog Prolog’s Declarative & Procedural Semantics Readings: Sections
CSE 425: Logic Programming I Logic and Programs Most programs use Boolean expressions over data Logic statements can express program semantics –I.e., axiomatic.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 27: Prolog’s Resolution and Programming Techniques COMP 144 Programming Language.
LING 388 Language and Computers Lecture 2 9/04/03 Sandiway FONG.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 26: Introduction to Logic Programming with Prolog COMP 144 Programming Language.
C. Varela1 Programming Languages (CSCI 4430/6969) History, Syntax, Semantics, Essentials, Paradigms Carlos Varela Rennselaer Polytechnic Institute August.
C. Varela1 Logic Programming (PLP 11.3) Prolog Imperative Control Flow: Backtracking Cut, Fail, Not Carlos Varela Rennselaer Polytechnic Institute September.
(9.1) COEN Logic Programming  Logic programming and predicate calculus  Prolog statements  Facts and rules  Matching  Subgoals and backtracking.
CS1502 Formal Methods in Computer Science Lecture Notes 10 Resolution and Horn Sentences.
ISBN Chapter 16 Logic Programming Languages.
Logic Programming Languages
Formal Models of Computation Part II The Logic Model
Notes for Chapter 12 Logic Programming The AI War Basic Concepts of Logic Programming Prolog Review questions.
1 Lecture 15: Introduction to Logic Programming with Prolog (Section 11.3) A modification of slides developed by Felix Hernandez-Campos at UNC Chapel Hill.
COP4020 Programming Languages Logical programming with Prolog Prof. Xin Yuan.
Introduction to Logic Programming with Prolog (Section 11.3)
30/09/04 AIPP Lecture 3: Recursion, Structures, and Lists1 Recursion, Structures, and Lists Artificial Intelligence Programming in Prolog Lecturer: Tim.
F28PL1 Programming Languages Lecture 16: Prolog 1.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now Chapter 9, exs 4 and 6. –6 must be in Horn clause form Prolog Handout 2.
Introduction To PROLOG World view of imperative languages. World view of relational languages. A PROLOG program. Running a PROLOG program. A PROLOG.
1 Knowledge Based Systems (CM0377) Lecture 3 (Last modified 5th February 2001)
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
Logic Programming Languages Session 13 Course : T Programming Language Concept Year : February 2011.
Logic Programming and Prolog Goal: use formalism of first-order logic Output described by logical formula (theorem) Input described by set of formulae.
Introduction to Prolog. Outline What is Prolog? Prolog basics Prolog Demo Syntax: –Atoms and Variables –Complex Terms –Facts & Queries –Rules Examples.
1 Knowledge Based Systems (CM0377) Lecture 6 (last modified 20th February 2002)
LING/C SC/PSYC 438/538 Lecture 15 Sandiway Fong. Did you install SWI Prolog?
Programming Language Concepts Lecture 17 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Logic Programming.
Logic Programming Tarik Booker. What will we cover?  Introduction  Definitions  Predicate Calculus  Prolog  Applications.
ISBN Chapter 16 Logic Programming Languages.
C. Varela1 Logic Programming (PLP 11, CTM 9.1) Terms, Resolution, Unification, Search, Backtracking (Prolog) Relational Computation Model (Oz) Carlos Varela.
Knowledge Based Information System
COP4020 Programming Languages Prolog Prof. Robert van Engelen (modified by Prof. Em. Chris Lacher)
In The Name Of Allah Lab 03 1Tahani Aldweesh. objectives Searching for the solution’s. Declaration. Query. Comments. Prolog Concepts. Unification. Disjunction.
Prolog 3 Tests and Backtracking 1. Arithmetic Operators Operators for arithmetic and value comparisons are built-in to Prolog = always accessible / don’t.
07/10/04 AIPP Lecture 5: List Processing1 List Processing Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 5 07/10/04.
Introduction to Prolog Asst. Prof. Dr. Senem Kumova Metin Revised lecture notes of “Concepts of Programmig Languages, Robert W. Sebesta, Ch. 16”
1 Announcements HW2 due Thursday, 2/18 in class and in Homework Server Note on Problem 3: Show means prove Note on 4&7: Will cover material today Download.
Announcements Rainbow grades up HW1, Quiz 1-2 in HW Server Deadline for HW3 (Prolog) is extended to Monday, March 7 th. Submit in HW Server A few notes.
C. Varela1 Logic Programming (PLP 11, CTM 9.3) Prolog Imperative Control Flow: Backtracking, Cut, Fail, Not Lists, Append Carlos Varela Rennselaer Polytechnic.
Prolog Fundamentals. 2 Review Last Lecture A Prolog program consists of a database of facts and rules, and queries (questions). –Fact:.... –Rule:... :-....
Copyright 1999Paul F. Reynolds, Jr. Foundations of Logic Programming.
Logic Programming Lecture 2: Unification and proof search.
Copyright © 2009 Elsevier Chapter 11 :: Logic Languages And other online sources (see links on schedule page) Michael L. Scott Based on Programming Language.
Section 16.5, 16.6 plus other references
By P. S. Suryateja Asst. Professor, CSE Vishnu Institute of Technology
Carlos Varela Rennselaer Polytechnic Institute November 11, 2016
CS 3304 Comparative Languages
For Friday No reading Prolog handout 3 Chapter 9, exercises 9-11.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
Carlos Varela Rensselaer Polytechnic Institute November 17, 2017
Carlos Varela Rensselaer Polytechnic Institute November 14, 2017
Logic Programming with Prolog: Resolution, Unification, Backtracking
Chapter 11 :: Logic Languages
Chapter 11 :: Logic Languages
Chapter 12 :: Logic Languages
Chapter 12 :: Logic Languages
Chapter 12 :: Logic Languages
Carlos Varela Rennselaer Polytechnic Institute November 15, 2016
Chapter 12 :: Logic Languages
Chapter 12 :: Logic Languages
Presentation transcript:

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 Last Class Bottom-up (LR) parsing Characteristic Finite State Machine (CFSM) SLR(1) parsing table Conflicts in SLR(1) LR parsing variants

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 Logic Programming and Prolog Read: Scott, Chapter 11.1,

Spring 16 CSCI 4430, A Milanova 5 Logic Programming Download and install SWI Prolog on laptop! J.R.Fisher’s Prolog Tutorial:

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

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

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

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 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 Lecture Outline Logic Programming Logic Programming Concepts Prolog Language constructs: facts, queries, rules Prolog concepts: search tree, unification, backtracking, backward chaining Lists

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

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

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

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

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

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

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

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 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 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 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 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 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 Improper and Proper Lists [1 | 2] versus [1, 2] [ ] Spring 16 CSCI 4430, A Milanova

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