C. Varela1 Logic Programming (PLP 11.3) Prolog Imperative Control Flow: Backtracking Cut, Fail, Not Carlos Varela Rennselaer Polytechnic Institute September.

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and Prolog
Advertisements

CS4026 Formal Models of Computation Part II The Logic Model Lecture 6 – Arithmetic, fail and the cut.
Chapter 11 :: Logic Languages
Computational Models The exam. Models of computation. –The Turing machine. –The Von Neumann machine. –The calculus. –The predicate calculus. Turing.
CS344 : Introduction to Artificial Intelligence Pushpak Bhattacharyya CSE Dept., IIT Bombay Lecture 16- Prolog.
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
INTRODUCTION TO PROLOG. PROLOG BASICS Atoms - most primitive terms that the language manipulates start with lower case letter includes strings (‘inside.
1 Logic Programming with Prolog: Resolution, Unification, Backtracking COMP 144 Programming Language Concepts Spring 2003 Stotts, Hernandez-Campos Modified.
About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language.
1 Prolog’s Lists, Negation and Imperative Control Flow COMP 144 Programming Language Concepts Spring 2003 The University of North Carolina at Chapel Hill.
COP4020 Programming Languages Logic programming and Prolog Prof. Xin Yuan.
Cs774 (Prasad)L7Negation1 Negation by Failure
Programming Languages Third Edition
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CSCE 580 Sp03 Marco Valtorta.
C. Varela1 Programming Languages (CSCI 4430/6969) History, Syntax, Semantics, Essentials, Paradigms Carlos Varela Rennselaer Polytechnic Institute August.
Carlos Varela Rennselaer Polytechnic Institute September 10, 2007
Prolog Ross (Tate). Filling in the Blanks Rather than reverse((a,b)) returns (b,a) Rather than reverse((a,b)) returns (b,a) What X makes reverse((a,b),
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 28: Prolog’s Lists, Negation and Imperative Control Flow COMP 144 Programming Language.
Copyright © 2009 Elsevier Chapter 11 :: Logic Languages Programming Language Pragmatics Michael L. Scott.
Copyright © 2009 Elsevier Chapter 11 :: Logic Languages Programming Language Pragmatics Michael L. Scott.
Notes for Chapter 12 Logic Programming The AI War Basic Concepts of Logic Programming Prolog Review questions.
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.
Logic Programming Two possible work modes: 1.At the lab: Use SICstus Prolog. To load a prolog file (*.pl or *.pro extension) to the interpreter, use: ?-
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)
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.
1 Topics Covers everything –1) in slides –2) from the very beginning. Language classification, history, and comparison Programming paradigms –Structured.
1 COMP 205 Introduction to Prolog Dr. Chunbo Chu Week 14.
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
Logic Programming CSC 358/ Outline Pattern matching Unification Logic programming.
Ch. 13 Ch. 131 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (notes?) Dr. Carter Tiernan.
Programming Languages Third Edition Chapter 4 Logic Programming.
C. Varela1 Logic Programming Prolog: Arithmetic, Equalities, Operators, I/O. (PLP 11) Databases: assert, retract. (CTM 9.6) Carlos Varela Rennselaer Polytechnic.
Logic Programming Tarik Booker. What will we cover?  Introduction  Definitions  Predicate Calculus  Prolog  Applications.
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
For Monday Exam 1 is Monday Takehome due Prolog Handout 3 due.
COP4020 Programming Languages Prolog Prof. Robert van Engelen (modified by Prof. Em. Chris Lacher)
For Friday No reading Prolog Handout 2. Homework.
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.
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.
COP4020 Programming Languages Logic Programming Prof. Xin Yuan.
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.
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),
Copyright © 2009 Elsevier Chapter 11 :: Logic Languages And other online sources (see links on schedule page) Michael L. Scott Based on Programming Language.
1 Lecture 16: Prolog’s Lists, Negation and Imperative Control Flow (Section 11.3) CSCI 431 Programming Languages Fall 2002 A modification of slides developed.
Chapter 12 :: Logic Languages
Carlos Varela Rennselaer Polytechnic Institute November 11, 2016
CS 3304 Comparative Languages
Carlos Varela Rensselaer Polytechnic Institute November 17, 2017
Carlos Varela Rensselaer Polytechnic Institute November 14, 2017
Carlos Varela Rensselaer Polytechnic Institute November 28, 2017
Logic Programming with Prolog: Resolution, Unification, Backtracking
Carlos Varela Rennselaer Polytechnic Institute December 6, 2016
Chapter 11 :: Logic Languages
CS 3304 Comparative Languages
Chapter 11 :: Logic Languages
Chapter 12 :: Logic Languages
Chapter 12 :: Logic Languages
Chapter 12 :: Logic Languages
Carlos Varela Rennselaer Polytechnic Institute November 22, 2016
Prolog Based on: Chapter 12 of PLP “Seven languages in seven weeks”
Carlos Varela Rennselaer Polytechnic Institute November 15, 2016
Chapter 12 :: Logic Languages
Carlos Varela Rennselaer Polytechnic Institute August 30, 2007
Chapter 12 :: Logic Languages
Presentation transcript:

C. Varela1 Logic Programming (PLP 11.3) Prolog Imperative Control Flow: Backtracking Cut, Fail, Not Carlos Varela Rennselaer Polytechnic Institute September 6, 2007

C. Varela2 Backtracking Forward chaining goes from axioms forward into goals. Backward chaining starts from goals and works backwards to prove them with existing axioms.

C. Varela3 Backtracking example 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

C. Varela4 Imperative Control Flow Programmer has explicit control on backtracking process. Cut (!) As a goal it succeeds, but with a side effect: –Commits interpreter to choices made since unifying parent goal with left-hand side of current rule.

C. Varela5 Cut (!) Example rainy(seattle). rainy(rochester). cold(rochester). snowy(X) :- rainy(X), !, cold(X).

C. Varela6 Cut (!) Example 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; no backtracking to rainy(X). GOAL FAILS. !

C. Varela7 Cut (!) Example 2 rainy(seattle). rainy(rochester). cold(rochester). snowy(X) :- rainy(X), !, cold(X). snowy(troy).

C. Varela8 Cut (!) Example 2 rainy(seattle). rainy(rochester). cold(rochester). snowy(X) :- rainy(X), !, cold(X). snowy(troy). snowy(C) snowy(X) AND OR rainy(X) cold(X) rainy(seattle)rainy(rochester) cold(rochester) _C = _X X = seattle C = troy FAILS snowy(X) is committed to bindings (X = seattle). GOAL FAILS. ! OR snowy(troy) C = troy

C. Varela9 Cut (!) Example 3 rainy(seattle) :- !. rainy(rochester). cold(rochester). snowy(X) :- rainy(X), cold(X). snowy(troy).

C. Varela10 Cut (!) Example 3 rainy(seattle) :- !. rainy(rochester). cold(rochester). snowy(X) :- rainy(X), cold(X). snowy(troy). snowy(C) snowy(X) AND OR rainy(X) cold(X) rainy(seattle)rainy(rochester) cold(rochester) _C = _X X = seattle C = troy SUCCEEDS Only rainy(X) is committed to bindings (X = seattle). ! OR snowy(troy) C = troy

C. Varela11 Cut (!) Example 4 rainy(seattle). rainy(rochester). cold(rochester). snowy(X) :- !, rainy(X), cold(X).

C. Varela12 Cut (!) Example 4 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 !

C. Varela13 Cut (!) Example 5 rainy(seattle). rainy(rochester). cold(rochester). snowy(X) :- rainy(X), cold(X), !.

C. Varela14 Cut (!) Example 5 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 X = rochester success !

C. Varela15 First-Class Terms call(P) Invoke predicate as a goal. assert(P) Adds predicate to database. retract(P) Removes predicate from database. functor(T,F,A) Succeeds if T is a term with functor F and arity A.

C. Varela16 not P is not  P In Prolog, the database of facts and rules includes a list of things assumed to be true. It does not include anything assumed to be false. Unless our database contains everything that is true (the closed-world assumption), the goal not P can succeed simply because our current knowledge is insufficient to prove P.

C. Varela17 More not vs  ? - snowy(X). X = rochester ? - not(snowy(X)). no Prolog does not reply: X = seattle. The meaning of not(snowy(X)) is:  X [snowy(X)] rather than:  X [  snowy(X)]

C. Varela18 Fail, true, repeat fail Fails current goal. true Always succeeds. repeat Always succeeds, provides infinite choice points. repeat. repeat :- repeat.

C. Varela19 not Semantics not(P) :- call(P), !, fail. not(P). Definition of not in terms of failure ( fail ) means that variable bindings are lost whenever not succeeds, e.g.: ? - not(not(snowy(X))). X=_G147

C. Varela20 Conditionals and Loops statement :- condition, !, then. statement :- else. natural(1). natural(N) :- natural(M), N is M+1. my_loop(N) :- natural(I), I<=N, write(I), nl, I=N, !, fail. Also called generate-and-test.

C. Varela21 Prolog lists [a,b,c ] is syntactic sugar for:.(a,.(b,.(c, []))) where [] is the empty list, and. is a built-in cons-like functor. [a,b,c ] can also be expressed as: [a | [b,c ]], or [a, b | [c ]], or [a,b,c | []]

C. Varela22 Prolog lists append example append([],L,L). append([H|T ],A, [H,L ]) :- append(T,A,L).

C. Varela23 Exercises 8.What do the following Prolog queries do? ? - repeat. ? - repeat, true. ? - repeat, fail. Corroborate your thinking with a Prolog interpreter. 9.Draw the search tree for the query “not(not(snowy(City)))”. When are variables bound/unbound in the search/backtracking process? 10.PLP Exercise (pg 655). 11.*PLP Exercise (pg 656).