Carlos Varela Rennselaer Polytechnic Institute November 15, 2016

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and Prolog
Advertisements

Some Prolog Prolog is a logic programming language
Chapter 11 :: Logic Languages
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Programming Techniques Accumulators, Difference Lists (VRH ) Carlos Varela.
C. Varela; Adapted from S. Haridi and P. Van Roy1 Declarative Programming Techniques Lazy Execution (VRH 4.5) Carlos Varela RPI Adapted with permission.
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 with Prolog: Resolution, Unification, Backtracking COMP 144 Programming Language Concepts Spring 2003 Stotts, Hernandez-Campos Modified.
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.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Defining practical programming languages Carlos Varela RPI.
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.
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.
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)
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.
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
C. Varela1 Logic Programming (CTM ) Constraint Programming: Constraints and Computation Spaces Carlos Varela Rennselaer Polytechnic Institute.
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.
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.
Higher-Order Programming: Iterative computation (CTM Section 3
Functional Programming: Lists, Pattern Matching, Recursive Programming (CTM Sections , 3.2, , 4.7.2) Carlos Varela RPI September 12,
Chapter 12 :: Logic Languages
Recursive stack-based version of Back-chaining using Propositional Logic
Carlos Varela Rennselaer Polytechnic Institute November 11, 2016
CS 3304 Comparative Languages
Logic Programming (cont’d): Lists
Higher-Order Programming: Iterative computation (CTM Section 3
Carlos Varela RPI September 22, 2017 Adapted with permission from:
Declarative Programming Techniques Accumulators (CTM 3. 4
Carlos Varela Rensselaer Polytechnic Institute November 17, 2017
Carlos Varela Rensselaer Polytechnic Institute December 5, 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
Carlos Varela Rensselaer Polytechnic Institute December 8, 2017
Logic Programming Languages
Chapter 11 :: Logic Languages
CS 3304 Comparative Languages
Chapter 11 :: Logic Languages
Higher-Order Programming: Closures, procedural abstraction, genericity, instantiation, embedding. Control abstractions: iterate, map, reduce, fold, filter.
Chapter 12 :: Logic Languages
Chapter 12 :: Logic Languages
Logic Programming Language
Chapter 12 :: Logic Languages
Declarative Programming Techniques Accumulators (CTM 3. 4
Declarative Computation Model Single assignment store (VRH 2
Carlos Varela Rennselaer Polytechnic Institute November 22, 2016
Declarative Programming Techniques Accumulators (CTM 3. 4
Prolog Based on: Chapter 12 of PLP “Seven languages in seven weeks”
Chapter 12 :: Logic Languages
Chapter 12 :: Logic Languages
Presentation transcript:

Carlos Varela Rennselaer Polytechnic Institute November 15, 2016 Logic Programming (PLP 11, CTM 9.3) Prolog Imperative Control Flow: Backtracking, Cut, Fail, Not Lists, Append Carlos Varela Rennselaer Polytechnic Institute November 15, 2016 C. Varela

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

Backtracking example AND OR snowy(C) _C = _X success snowy(X) rainy(seattle). rainy(rochester). cold(rochester). snowy(X) :- rainy(X), cold(X). snowy(C) _C = _X success snowy(X) cold(seattle) fails; backtrack. AND rainy(X) cold(X) X = seattle X = rochester OR cold(rochester) rainy(seattle) rainy(rochester) C. Varela

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. Choices include variable unifications and rule to satisfy the parent goal. C. Varela

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

Cut (!) Example rainy(seattle). rainy(rochester). cold(rochester). snowy(X) :- rainy(X), !, cold(X). cold(seattle) fails; no backtracking to rainy(X). GOAL FAILS. snowy(C) _C = _X snowy(X) AND rainy(X) ! cold(X) X = seattle OR cold(rochester) rainy(seattle) rainy(rochester) C. Varela

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

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

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

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

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

Cut (!) Example 4 AND OR snowy(C) _C = _X success snowy(X) rainy(seattle). rainy(rochester). cold(rochester). snowy(X) :- !, rainy(X), cold(X). snowy(C) _C = _X success snowy(X) cold(seattle) fails; backtrack. AND ! rainy(X) cold(X) X = seattle X = rochester OR cold(rochester) rainy(seattle) rainy(rochester) C. Varela

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

Cut (!) Example 5 AND OR snowy(C) _C = _X success snowy(X) ! rainy(X) rainy(seattle). rainy(rochester). cold(rochester). snowy(X) :- rainy(X), cold(X), !. snowy(C) _C = _X success snowy(X) AND ! rainy(X) cold(X) X = seattle X = rochester OR cold(rochester) rainy(seattle) rainy(rochester) C. Varela

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. findall(F,P,L) Returns a list L with elements F satisfying predicate P C. Varela

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 (or \+ P in some Prolog implementations) can succeed simply because our current knowledge is insufficient to prove P. C. Varela

More not vs  Prolog does not reply: X = seattle. ?- 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. Varela

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

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

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

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

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

C. Varela; Adapted w. permission from S. Haridi and P. Van Roy Oz lists (Review) [a b c] is syntactic sugar for: ’|’(a ’|’(b ’|’(c nil))) where nil is the empty list, and ’|’ is the tuple’s functor. A list has two components: a head, and a tail declare L = [6 7 8] L.1 gives 6 L.2 give [7 8] ‘|’ 6 ‘|’ 7 ‘|’ 8 nil C. Varela; Adapted w. permission from S. Haridi and P. Van Roy

Oz lists append example proc {Append Xs Ys Zs} choice Xs = nil Zs = Ys [] X Xr Zr in Xs=X|Xr Zs=X|Zr {Append Xr Ys Zr} end % new search query proc {P S} X Y in {Append X Y [1 2 3]} S=X#Y end % new search engine E = {New Search.object script(P)} % calculate and display one at a time {Browse {E next($)}} % calculate all {Browse {Search.base.all P}} C. Varela; Adapted with permission from S. Haridi and P. Van Roy

Exercises What do the following Prolog queries do? ?- repeat. ?- repeat, true. ?- repeat, fail. Corroborate your thinking with a Prolog interpreter. Draw the search tree for the query “not(not(snowy(City)))”. When are variables bound/unbound in the search/backtracking process? PLP Exercise 11.7 (pg 571). Write the students example in Oz (including the has_taken(Student, Course) inference). C. Varela