CS 2104 – Prog. Lang. Concepts Logic Programming - II Dr. Abhik Roychoudhury School of Computing.

Slides:



Advertisements
Similar presentations
Formal Models of Computation Part II The Logic Model
Advertisements

CS4026 Formal Models of Computation Part II The Logic Model Lecture 6 – Arithmetic, fail and the cut.
After the Practical on Clausal Form and Resolution Question 5 Proving the query of Question 4.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10: Cuts and Negation Theory –Explain how to control Prolog`s backtracking behaviour with.
Chapter 11 :: Logic Languages
Logic Programming – Part 2 Lists Backtracking Optimization (via the cut operator) Meta-Circular Interpreters.
Logic Programming – Part 2 Lists Backtracking Optimization (via the cut operator) Meta-Circular Interpreters.
Prolog The language of logic. History Kowalski: late 60’s Logician who showed logical proof can support computation. Colmerauer: early 70’s Developed.
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
Logic Programming (cont’d): Lists Lists in Prolog are represented by a functor (similar to cons in Scheme): 1.The empty list is represented by the constant.
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).
Lecture 10 Logic programming, PROLOG (PROgramming in LOGic)
INTRODUCTION TO PROLOG. PROLOG BASICS Atoms - most primitive terms that the language manipulates start with lower case letter includes strings (‘inside.
1 Knowledge Based Systems (CM0377) Lecture 8 (Last modified 5th March 2001)
CS 2104 – Prog. Lang. Concepts Final wrap-up Dr. Abhik Roychoudhury School of Computing.
CS 330 Programming Languages 12 / 02 / 2008 Instructor: Michael Eckmann.
MB: 2 March 2001CS360 Lecture 31 Programming in Logic: Prolog Prolog’s Declarative & Procedural Semantics Readings: Sections
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
Outline Recap Knowledge Representation I Textbook: Chapters 6, 7, 9 and 10.
LING 388 Language and Computers Lecture 2 9/04/03 Sandiway FONG.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CSCE 580 Sp03 Marco Valtorta.
Constraint Logic Programming Ryan Kinworthy. Overview Introduction Logic Programming LP as a constraint programming language Constraint Logic Programming.
1 CILOG User Manual Bayesian Networks Seminar Sep 7th, 2006.
Introduction to prolog (1). Introduction to Prolog 2 Overviews Interacting with Prolog Terms Existential Queries Universal Facts and Rules Negation as.
Logic Programming Part 2: Semantics James Cheney CS 411.
CS1502 Formal Methods in Computer Science Lecture Notes 10 Resolution and Horn Sentences.
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 7 Implementing Prolog unification, backtracking with coroutines Ras Bodik Shaon Barman Thibaud Hottelier Hack Your Language! CS164: Introduction.
FATIH UNIVERSITY Department of Computer Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CENG 421 Fall03.
CS 321 Programming Languages and Compilers Prolog part 2.
CS 403: Programming Languages Lecture 19 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
1 Lecture 7 Implementing Prolog unification, backtracking with coroutines Ras Bodik with Mangpo and Ali Hack Your Language! CS164: Introduction to Programming.
Logic Programming (cont’d): Lists Lists in Prolog are represented by a functor (similar to cons in Scheme): 1.The empty list is represented by the constant.
14/10/04 AIPP Lecture 7: The Cut1 Controlling Backtracking: The Cut Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 7 14/10/04.
Prolog Programming. 2 DATA STRUCTURES IN PROLOG PROGRAMMING TECHNIQUES CONTROL IN PROLOG CUTS.
Control Algorithms 1 Chapter 6 Control Algorithms 1 Chapter 6 Pattern Search.
CSE S. Tanimoto Horn Clauses and Unification 1 Horn Clauses and Unification Propositional Logic Clauses Resolution Predicate Logic Horn Clauses.
1 Knowledge Based Systems (CM0377) Lecture 3 (Last modified 5th February 2001)
1 COMP 205 Introduction to Prolog Dr. Chunbo Chu Week 14.
O A procedure: a set of axioms (rules and facts) with identical signature (predicate symbol and arity). o A logic program: a set of procedures defining.
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
Logic Programming (cont’d): Lists Lists in Prolog are represented by a functor (similar to cons in Scheme): 1.The empty list is represented by the constant.
CS4026 Formal Models of Computation Part II The Logic Model Lecture 2 – Prolog: History and Introduction.
LDK R Logics for Data and Knowledge Representation Propositional Logic: Reasoning First version by Alessandro Agostini and Fausto Giunchiglia Second version.
Logic Programming and Prolog Goal: use formalism of first-order logic Output described by logical formula (theorem) Input described by set of formulae.
Ch. 13 Ch. 131 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (notes?) Dr. Carter Tiernan.
Lab Lecture#4 Lecturer : Sheriff Nafisa TA : Mubarakah Otbi, Duaa al Ofi, Huda al Hakami.
Programming Languages Third Edition Chapter 4 Logic Programming.
Cs774 (Prasad)L6Backtracking1 Controlling Backtracking : Cuts
Principles of programming languages 12: Logic programming Isao Sasano Department of Information Science and Engineering.
Prolog Unification and clause order. The notion of Unification Unification is when two things “become one” Unification is kind of like assignment Unification.
Dr. Muhammed Al-Mulhem ICS An Introduction to Prolog.
In The Name Of Allah Lab 03 1Tahani Aldweesh. objectives Searching for the solution’s. Declaration. Query. Comments. Prolog Concepts. Unification. Disjunction.
For Friday No reading Prolog Handout 2. Homework.
07/10/04 AIPP Lecture 5: List Processing1 List Processing Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 5 07/10/04.
23-Feb-16 Prolog II Unification and clause order.
Answer Extraction To use resolution to answer questions, for example a query of the form  X C(X), we must keep track of the substitutions made during.
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.
1-1 An Introduction to Prolog Sept Prolog statements Like other programming languages, Prolog consists of collection of statements. Prolog.
O A procedure: a set of axioms (rules and facts) with identical signature (predicate symbol and arity). o A logic program: a set of procedures (predicates),
Logic Programming Lecture 2: Unification and proof search.
Prolog Programming.
CSE 341, S. Tanimoto Logic Programming -
Prolog Concepts.
Logic Programming (cont’d): Lists
Horn Clauses and Unification
Horn Clauses and Unification
Prolog Concepts.
Presentation transcript:

CS 2104 – Prog. Lang. Concepts Logic Programming - II Dr. Abhik Roychoudhury School of Computing

Facts link(fortran, algol60).link(c,cplusplus). link(algol60,cpl).link(algol60,simula67). link(cpl,bcpl).link(simula67,cplusplus). link(bcpl,c).link(simula67,smalltalk80). fortran algol60 cpl bcpl c simula67 cplusplus smalltalk80 “Facts”

Meaning of Rules and Facts path(L, L). path(L, M):-link(L,X),path(X,M). Variables in the head of a rule or a fact are universally quantified. path(L,L). For all L, path(L,L). Hence, we have path(a,a), or path(you,you), but we don’t know if we have path(you,me). variable

Rules or Clauses The clause path(L,L). Stands for the logical formula  L path(L, L) The clause path(L,M) :- link(L,X), path(X, M). Stands for the logical formula  L,M path(L, M)   X link(L,X)  path(X, M)

Meaning of Rules and Facts path(L, L). path(L, M):-link(L,X),path(X,M). For all L and M, path(L,M) if there exists X such that link(L,X) and path(X,M). link(fortran, algol60).link(c, cplusplus). link(algol60,cpl).link(algol60, simula67). link(cpl,bcpl).link(simula67, cplusplus). link(bcpl,c).link(simula67, smalltalk80). path(fortran,cpl) if link(fortran, algol60) and path(algol60,cpl). New variables in the body of a rule are existentially quantified.

The Meaning of Queries Queries are existentially quantified logical formulae ?- link(algo60,L), link(L,M). Do there exist some values for L and M such that link(algo60,L) and link(L,M) ? A solution to a query is a binding of variables (in the query) to values that makes the query true. When a solution exists, the query is said to be satisfiable. Prolog answers no to a query if it fails to satisfy the query subgoal

Query Evaluation (last lecture) Recall that Prolog computation proceeds by query evaluation. This corresponds to generating bindings for variables in the query which allow the query to be deduced. Truth/falsehood of the query is deduced from the program which stands for a set of universally quantified first order logic formulae. Contrast this with the procedural manner in which we viewed query evaluation in the last lecture !

Query Evaluation A query evaluation succeeds only when the truth can be established from the rules and facts. Otherwise, it is considered false. link(fortran, algol60).link(c, cplusplus). link(algol60,cpl).link(algol60, simula67). link(cpl,bcpl).link(simula67, cplusplus). link(bcpl,c).link(simula67, smalltalk80). link(bcpl,c). Is true link(bcpl,cplusplus) is false.

Example path(L, L). path(L, M):-link(L,X),path(X,M). path(c,smalltalk) is not true, neither is path(you,me). path(cpl,cpl) is true, so is path(you,you). path(algol60,smalltalk80) is true.

Negative Answers and Queries Query answer : no  “I can’t prove it”. ?- link(lisp,scheme). no ?- not(link(lisp,scheme)). yes ?- not(P). returns false if Prolog can deduce P (make P true). returns true if Prolog fails to deduce P.

Negation as Failure Negation as Failure  logical negation Logical negation : We can prove that it is false Negation as failure : We can’t prove that it is true

Example ?- link(L,N), link(M,N). L = fortran N = algol60 M= fortran ?- link(L,N), link(M,N), not(L=M). L= c N= cplusplus M= simula67 ?- not(L=M), link(L,N), link(M,N). no Subgoal ordering can affect the solution

Example Negation can appear in program as well as query. bachelor(X) :- male(X), not(married(X)). male(bill). married(bill). male(jim). married(mary). ?- bachelor(X) X = jim not(married(bill) fails. not(married(jim)) succeeds. (Negation as failure)

Now… Control in Prolog computation Ordering of goals Ordering of rules Search trees Some programming practices Generate and test Cuts – A piece of hackery !

Control in Prolog Start with a query as the current goal; while the current goal is nonempty do { choose the leftmost subgoal ; if a rule applies to the subgoal then { select the 1st applicable rule ; form a new current goal } else {backtrack } } ; if the current goal is empty then succeed else fail. append1([ ],Y,Y). append1([X|Xs],Ys,[X|Zs]) :- append1(Xs,Ys,Zs). prefix(X,Z) :- append1(X, _,Z). suffix(Y,Z) :- append1(_,Y,Z). ?- prefix([a],[a,b,c]). X Z Y append1([a],_,[a,b,c]) append1([],Ys,[b,c]). yes prefix([a],[a,b,c])

Ordering of Subgoals Start with a query as the current goal; while the current goal is nonempty do { choose the leftmost subgoal ; if a rule applies to the subgoal then { select the 1st applicable rule ; form a new current goal } else {backtrack } } ; if the current goal is empty then succeed else fail. append1([ ],Y,Y). append1([X|Xs],Ys,[X|Zs]) :- append1(Xs,Ys,Zs). prefix(X,Z) :- append1(X, _,Z). suffix(Y,Z) :- append1(_,Y,Z). ?- prefix(X,[a,b,c]), suffix([e],X). no X Z Y ?- suffix([e],X), prefix(X,[a,b,c]).

Start with a query as the current goal; while the current goal is nonempty do { choose the leftmost subgoal ; if a rule applies to the subgoal then { select the 1st applicable rule ; form a new current goal } else {backtrack } } ; if the current goal is empty then succeed else fail. Ordering of Rules append1([ ],Y,Y). append1([X|Xs],Ys,[X|Zs]) :- append1(Xs,Ys,Zs). app([X|Xs],Ys,[X|Zs]) :- app(Xs,Ys,Zs). app([ ],Y,Y). ?- append1(X,[c],Z). X = [ ], Z = [c] ; X = [ _1 ], Z = [ _1,c] ; X = [ _1,_2 ], Z = [ _1,_2,c ] ; ….. ?- app(X,[c],Z).

A Refined Description of Control Start with a query as the current goal; while the current goal is nonempty do { let the current goal be G 1,..,G k, k>0; choose the leftmost subgoal G 1 ; if a rule applies to G 1 then { select the 1st applicable rule A :- B 1,.., B j. (j  1) ; let  be the most general unifier of G 1 and A ; set the current goal to be B 1 ,..,B j ,G 2 ,..,G k  } else {backtrack } } ; if the current goal is empty then succeed else fail.

Search in Prolog – Example: Program: append([], X, X). append([X|Xs], Y,[X|Zs]) :- append(Xs,Y,Zs). Query: A conjunction of subgoals append(A, B, [a]), append(B, [a], A).

Prolog’s Search Trees ?- append(A,B,[a]),append(B,[a],A). ?- append([a],[a],[ ]). C1 {A->[ ],B->[a],Y1->[a]} ?- append(Xs1,B,[ ]), append( B,[a],[a|Xs1]). C2 {A->[a|Xs1], Ys1->B,Zs1->[]} ?- append([ ],[a],[a]). C1 {Xs1->[],B->[ ]} Fail and Backtrack C1 Fail and Backtrack C2 A=[a],B=[ ] C1 User requests Backtrack Fail and Backtrack C2 Fail and Backtrack

Generate-and-Test Technique Generate : Generate possible solutions Test : Verify the solutions member(M,[M|_]). member(M,[ _|T]) :- member(M,T). overlap(X,Y) :- member(M,X), member(M,Y). Generate a value for M in X Test if it is in Y ?- overlap([a,b],[b,c]). member(a,[a,b]), member(a,[b,c]) member(b,[a,b]), member(b,[b,c]) yes

Cuts A cut prunes an explored part of a Prolog search tree. a(1):-b. a(2):-e. b:- c. b:-d. d. e. ?- a(X). X = 1 ; X = 2 ; no Cut in a clause commits to the use of that clause. Cut has the effect of making b fails if c fails. X=2 X=1 a(X) b e c d X=1 X=2 !, c ?- a(X). X = 2 ; no

a(X):-b(X). a(X) :-f(X). b(X):-g(X), v(X). b(X):-X = 4, v(X). g(1). g(2). g(3). v(1). v(X) :- f(X). f(5). ?- a(Z). Z = 1 ; Z = 5 ; no v(3) a(Z) b(Z)f(Z) g(Z), v(Z) Z=4,v(4) backtrack v(1)f(1) Z = 1 v(1)v(2) f(2) backtrack f(3) backtrack f(4) backtrack f(5) Z = 5 !, ?- a(Z). Z = 1 ; Z = 5 ; no

Green Cuts, Red Cuts Green Cut a cut the prunes part of a Prolog search tree that cannot possibly reach a solution used mainly for efficiency sake. Red Cut a cut that alters the set of possible solutions reachable. Powerful tool, yet fatal and can be confusing Handle with care

Merging two lists merge([X|Xs],[Y|Ys],[X|Zs]) :- X <Y, merge(Xs,[Y|Ys],Zs). merge([X|Xs],[Y|Ys],[X,Y|Zs]) :- X = Y, merge(Xs,Ys,Zs). merge([X|Xs],[Y|Ys],[Y|Zs]) :- X > Y, merge([X|Xs], Ys, Zs). merge(X, [], X). merge([], Y, Y). First three clauses mutually exclusive No need to try the others, if one of them succeeds. This is made explicit by a green cut.

Example: Green cut merge([X|Xs],[Y|Ys],[X|Zs]) :- X <Y, !, merge(Xs,[Y|Ys],Zs). merge([X|Xs],[Y|Ys],[X,Y|Zs]) :- X = Y, !, merge(Xs,Ys,Zs). merge([X|Xs],[Y|Ys],[Y|Zs]) :- X > Y, !, merge([X|Xs], Ys, Zs). merge(X, [], X) :- !. merge([], Y, Y). Inserting these cuts does not change the answers to any merge query.

Example: Red Cut member(X,[X|Xs]) :- !. member(X,[Y|Ys]) :- member(X, Ys). member(1, [1,2,1,1,3]) is more efficient. But member(X, [1,2,3]) produces only one answer X = 1

Summary Prolog is a procedural language with: Assign once variables Nondeterminism As a result of having assign once variables Assignment is symmetric Test and assignment represented by same operator. Unification combines the concepts of test, assignment and pattern matching.

Summary As a result of having nondeterminism Control issues for the search Cuts (allows the programmer explcit control) Meaning of Prolog program given by queries that can be evaluated to true. Applications of Prolog (in next lecture) Database query language Grammar Processing