© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 3 “Input & Output”, Negation, Search.

Slides:



Advertisements
Similar presentations
CS4026 Formal Models of Computation Part II The Logic Model Lecture 6 – Arithmetic, fail and the cut.
Advertisements

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10: Cuts and Negation Theory –Explain how to control Prolog`s backtracking behaviour with.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 6: More Lists Theory –Define append/3, a predicate for concatenating two lists, and illustrate.
Prolog Programming (Volume 5) Dr W.F. Clocksin. List cells + Unification = Great Idea Normally we use ‘proper lists’, which are defined according to the.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 6: More Lists Theory –Define append/3, a predicate for concatenating two lists, and illustrate.
Prolog: List © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
Michael Alves, Patrick Dugan, Robert Daniels, Carlos Vicuna
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
Chapter 4: Trees Part II - AVL Tree
1 Knowledge Based Systems (CM0377) Lecture 8 (Last modified 5th March 2001)
Techniques for Dealing with Hard Problems Backtrack: –Systematically enumerates all potential solutions by continually trying to extend a partial solution.
CSC 423 ARTIFICIAL INTELLIGENCE
MB: 2 March 2001CS360 Lecture 31 Programming in Logic: Prolog Prolog’s Declarative & Procedural Semantics Readings: Sections
Chapter 12: Expert Systems Design Examples
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s.
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 Non-determinism and Cut  Non-determinism  Backtracking  Controlling Backtracking.
MAE 552 – Heuristic Optimization Lecture 27 April 3, 2002
Inference and Resolution for Problem Solving
The Theory of NP-Completeness
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 5: Arithmetic Theory –Introduce Prolog`s built-in abilities for performing arithmetic –Apply.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 4: Lists Theory –Introduce lists, an important recursive data structure often used in Prolog.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 2 Theory –Unification –Unification in Prolog –Proof search Exercises –Correction exercises.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 9: A closer look at terms Theory –Introduce the == predicate –Take a closer look at term structure.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 5: Arithmetic Theory –Introduce Prolog`s built-in abilities for performing arithmetic –Apply.
Using Search in Problem Solving
1 CILOG User Manual Bayesian Networks Seminar Sep 7th, 2006.
Lesson 6. Refinement of the Operator Model This page describes formally how we refine Figure 2.5 into a more detailed model so that we can connect it.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Chapter 3: Arrays, Linked Lists, and Recursion
Backtracking.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 4: Lists Theory –Introduce lists, an important recursive data structure often used in Prolog.
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.
LING 388: Language and Computers Sandiway Fong Lecture 4.
CS 321 Programming Languages and Compilers Prolog part 2.
Reading and Writing Mathematical Proofs
Recursion Chapter 7. Chapter Objectives  To understand how to think recursively  To learn how to trace a recursive method  To learn how to write recursive.
1 Chapter 8 Inference and Resolution for Problem Solving.
Agile Software Development Lab Dr. Günter Kniesel, Daniel Speicher, Tobias Rho Spring 2008 Prolog - Part 2 Patrick Rypalla Alexis Raptarchis
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.
30/09/04 AIPP Lecture 3: Recursion, Structures, and Lists1 Recursion, Structures, and Lists Artificial Intelligence Programming in Prolog Lecturer: Tim.
Games. Adversaries Consider the process of reasoning when an adversary is trying to defeat our efforts In game playing situations one searches down the.
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.
Jigar Gaglani.  Programming paradigm is a fundamental style of computer programming  Paradigms differ in concepts and abstractions used to represent.
Logic Programming CSC 358/ Outline Pattern matching Unification Logic programming.
Prolog Program Style (ch. 8) Many style issues are applicable to any program in any language. Many style issues are applicable to any program in any language.
Logic Programming and Prolog Goal: use formalism of first-order logic Output described by logical formula (theorem) Input described by set of formulae.
Automated Reasoning Early AI explored how to automated several reasoning tasks – these were solved by what we might call weak problem solving methods as.
Automated Reasoning Early AI explored how to automate several reasoning tasks – these were solved by what we might call weak problem solving methods as.
Ch. 13 Ch. 131 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (notes?) Dr. Carter Tiernan.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 9: A closer look at terms Theory –Introduce the == predicate –Take a closer look at term structure.
Operators in Prolog © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
Basic Problem Solving Search strategy  Problem can be solved by searching for a solution. An attempt is to transform initial state of a problem into some.
Goal-based Problem Solving Goal formation Based upon the current situation and performance measures. Result is moving into a desirable state (goal state).
Programming Languages Third Edition Chapter 4 Logic Programming.
Negation Chapter 5. Stating Negative Conditions n Sometimes you want to say that some condition does not hold n Prolog allows this –not/1this is a predicate.
CS 554: Knowledge base systems Part-4: Prolog- 2 By Dr. Syed Noman Hasany.
07/10/04 AIPP Lecture 5: List Processing1 List Processing Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 5 07/10/04.
Solving problems by searching A I C h a p t e r 3.
Chapter 11. Chapter Summary  Introduction to trees (11.1)  Application of trees (11.2)  Tree traversal (11.3)  Spanning trees (11.4)
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.
Theory of Computational Complexity Probability and Computing Chapter Hikaru Inada Iwama and Ito lab M1.
Prolog: cut & fail © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
Chapter 12 :: Logic Languages
Programming Paradigms and Languages
Presentation transcript:

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 3 “Input & Output”, Negation, Search

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Last Lecture In the previous lecture we have introduced: –recursion –declarative and procedural semantics –arithmetic operators –list, head & tail of a list, recursion on lists –predicates for collecting solutions

Aim of this lecture LPN Sect After this lecture, you should be able to explain the following key notions and their relation to Prolog: –“input/output” in Prolog –negation as failure –search algorithms

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Misc Info Prolog has many built-in predicates. See Prolog manual, e.g., list library documentation (Appendix 12)

“Input and output” in Prolog

Output Predicates do not return values. Variables are instantiated through unification. This is what gives Prolog its computation power! E.g., suppose you want to do something with the result of pairing the elements of a list with another element. Then don’t write doSomething(pair(L1,X,L2)) but:..... :- pair(L1,X,L2), doSomething(L2, L3). Here, L2 is taken as the “output” resulting from proving pair(L1,X,L2)

Input or Output When posing a query, often it is not fixed whether a parameter has to be “input” or “output”.

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Example: a2b/2 The predicate a2b/2 takes two lists as arguments and succeeds –if the first argument is a list of as, and –the second argument is a list of bs of exactly the same length ?- a2b([a,a,a,a],[b,b,b,b]). yes ?- a2b([a,a,a,a],[b,b,b]). no ?- a2b([a,c,a,a],[b,b,b,t]). no

© Patrick Blackburn, Johan Bos & Kristina Striegnitz a2b/2 a2b([],[]). a2b([a|L1],[b|L2]):- a2b(L1,L2).

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Both Parameters “Input” a2b([],[]). a2b([a|L1],[b|L2]):- a2b(L1,L2). ?- a2b([a,a,a],[b,b,b]). yes ?-

© Patrick Blackburn, Johan Bos & Kristina Striegnitz First Parameter “Input” Second Parameter “Output” a2b([],[]). a2b([a|L1],[b|L2]):- a2b(L1,L2). ?- a2b([a,a,a,a,a], X). X = [b,b,b,b,b] yes ?-

© Patrick Blackburn, Johan Bos & Kristina Striegnitz a2b([],[]). a2b([a|L1],[b|L2]):- a2b(L1,L2). ?- a2b(X,[b,b,b,b,b,b,b]). X = [a,a,a,a,a,a,a] yes ?- First Parameter “Output” Second Parameter “Input”

Input or Output Thus: When posing a query, often it is not fixed whether a parameter has to be “input” or “output”. But: Not in all cases can any parameter be used as input or as output. Sometimes parameters have to be instantiated.

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Defining predicates with arithmetic addThreeAndDouble(X, Y):- Y is 2 * (X+3). f(x) = 2(x+3)

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Defining predicates with arithmetic addThreeAndDouble(X, Y):- Y is 2 * (X+3). ?- addThreeAndDouble(1,Y). Y=8 yes ?- addThreeAndDouble(2,Y). Y=10 yes

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Defining predicates with arithmetic addThreeAndDouble(X, Y):- Y is 2 * (X+3). ?- addThreeAndDouble(X,10).

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Defining predicates with arithmetic addThreeAndDouble(X, Y):- Y is 2 * (X+3). ?- addThreeAndDouble(X,10). ERROR: is/2: Arguments are not sufficiently instantiated

Prolog Manual +Argument must be fully instantiated to a term that satisfies the required argument type. Think of the argument as input. - Argument must be unbound. Think of the argument as output. ?Argument must be bound to a partial term of the indicated type. Note that a variable is a partial term for any type. Think of the argument as either input or output or both input and output. E.g., flatten(+List1, ?List2)

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Meaning of Cut

© Patrick Blackburn, Johan Bos & Kristina Striegnitz The Cut Backtracking is a characteristic feature of Prolog But backtracking can lead to inefficiency: –Prolog can waste time exploring possibilities that lead nowhere –It would be nice to have some control The cut predicate !/0 offers a way to control backtracking

Warning! Only use cut when really needed! It affects Prolog’s procedural semantics, and can lead to difficult to understand code We also introduce it to explain negation as failure, which is used very often

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Example of cut The cut is a Prolog predicate, so we can add it to rules in the body: – Example: p(X):- b(X), c(X), !, d(X), e(X). Cut is a goal that always succeeds Commits Prolog to the choices that were made since the parent goal was called

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Explaining the cut In order to explain the cut, we will –Look at a piece of cut-free Prolog code and see what it does in terms of backtracking –Add cuts to this Prolog code –Examine the same piece of code with added cuts and look how the cuts affect backtracking

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Example: cut-free code p(X):- a(X). p(X):- b(X), c(X), d(X), e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- p(X).

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Example: cut-free code p(X):- a(X). p(X):- b(X), c(X), d(X), e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- p(X).

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Example: cut-free code p(X):- a(X). p(X):- b(X), c(X), d(X), e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- p(X).

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Example: cut-free code p(X):- a(X). p(X):- b(X), c(X), d(X), e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- p(X). ?- a(X).

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Example: cut-free code p(X):- a(X). p(X):- b(X), c(X), d(X), e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- p(X). X=1 ?- p(X). ?- a(X). X=1

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Example: cut-free code p(X):- a(X). p(X):- b(X), c(X), d(X), e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- p(X). X=1; ?- p(X). ?- a(X).?- b(X),c(X),d(X),e(X). X=1

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Example: cut-free code p(X):- a(X). p(X):- b(X), c(X), d(X), e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- p(X). X=1; ?- p(X). ?- a(X).?- b(X),c(X),d(X),e(X). X=1

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Example: cut-free code p(X):- a(X). p(X):- b(X), c(X), d(X), e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- p(X). X=1; ?- p(X). ?- a(X). ?- c(1),d(1),e(1). ?- b(X),c(X),d(X),e(X). X=1

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Example: cut-free code p(X):- a(X). p(X):- b(X), c(X), d(X), e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- p(X). X=1; ?- p(X). ?- a(X). ?- d(1), e(1). ?- c(1),d(1),e(1). ?- b(X),c(X),d(X),e(X). X=1

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Example: cut-free code p(X):- a(X). p(X):- b(X), c(X), d(X), e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- p(X). X=1; ?- p(X). ?- a(X). ?- d(1), e(1). ?- c(1),d(1),e(1). ?- b(X),c(X),d(X),e(X). X=1 †

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Example: cut-free code p(X):- a(X). p(X):- b(X), c(X), d(X), e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- p(X). X=1; ?- p(X). ?- a(X). ?- d(1), e(1). ?- c(1),d(1),e(1). ?- b(X),c(X),d(X),e(X). X=1 X=2 †

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Example: cut-free code p(X):- a(X). p(X):- b(X), c(X), d(X), e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- p(X). X=1; ?- p(X). ?- a(X). ?- d(1), e(1). ?- c(1),d(1),e(1).?- c(2),d(2),e(2). ?- b(X),c(X),d(X),e(X). X=1 X=2 †

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Example: cut-free code p(X):- a(X). p(X):- b(X), c(X), d(X), e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- p(X). X=1; ?- p(X). ?- a(X). ?- d(2), e(2).?- d(1), e(1). ?- c(1),d(1),e(1).?- c(2),d(2),e(2). ?- b(X),c(X),d(X),e(X). X=1 X=2 †

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Example: cut-free code p(X):- a(X). p(X):- b(X), c(X), d(X), e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- p(X). X=1; ?- p(X). ?- a(X). ?- d(2), e(2). ?- e(2). ?- d(1), e(1). ?- c(1),d(1),e(1).?- c(2),d(2),e(2). ?- b(X),c(X),d(X),e(X). X=1 X=2 †

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Example: cut-free code p(X):- a(X). p(X):- b(X), c(X), d(X), e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- p(X). X=1; X=2 ?- p(X). ?- a(X). ?- d(2), e(2). ?- e(2). ?- d(1), e(1). ?- c(1),d(1),e(1).?- c(2),d(2),e(2). ?- b(X),c(X),d(X),e(X). X=1 X=2 †

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Example: cut-free code p(X):- a(X). p(X):- b(X), c(X), d(X), e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- p(X). X=1; X=2; ?- p(X). ?- a(X).?- f(X). ?- d(2), e(2). ?- e(2). ?- d(1), e(1). ?- c(1),d(1),e(1).?- c(2),d(2),e(2). ?- b(X),c(X),d(X),e(X). X=1 X=2 †

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Example: cut-free code p(X):- a(X). p(X):- b(X), c(X), d(X), e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- p(X). X=1; X=2; X=3 ?- p(X). ?- a(X).?- f(X). ?- d(2), e(2). ?- e(2). ?- d(1), e(1). ?- c(1),d(1),e(1).?- c(2),d(2),e(2). ?- b(X),c(X),d(X),e(X). X=1 X=2 † X=3

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Example: cut-free code p(X):- a(X). p(X):- b(X), c(X), d(X), e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- p(X). X=1; X=2; X=3; no ?- p(X). ?- a(X).?- f(X). ?- d(2), e(2). ?- e(2). ?- d(1), e(1). ?- c(1),d(1),e(1).?- c(2),d(2),e(2). ?- b(X),c(X),d(X),e(X). X=1 X=2 † X=3

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Adding a cut Suppose we insert a cut in the second clause: If we now pose the same query we will get the following response: p(X):- b(X), c(X), !, d(X), e(X). ?- p(X). X=1; no

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Example: cut p(X):- a(X). p(X):- b(X),c(X),!,d(X),e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- p(X).

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Example: cut p(X):- a(X). p(X):- b(X),c(X),!,d(X),e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- p(X).

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Example: cut ?- p(X). p(X):- a(X). p(X):- b(X),c(X),!,d(X),e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3).

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Example: cut ?- p(X). X=1 ?- p(X). ?- a(X). X=1 p(X):- a(X). p(X):- b(X),c(X),!,d(X),e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3).

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Example: cut ?- p(X). X=1; ?- p(X). ?- a(X).?- b(X),c(X),!,d(X),e(X). X=1 p(X):- a(X). p(X):- b(X),c(X),!,d(X),e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3).

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Example: cut ?- p(X). X=1; ?- p(X). ?- a(X).?- b(X),c(X),!,d(X),e(X). X=1 p(X):- a(X). p(X):- b(X),c(X),!,d(X),e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3).

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Example: cut ?- p(X). X=1; ?- p(X). ?- a(X).?- b(X),c(X),!,d(X),e(X). X=1 p(X):- a(X). p(X):- b(X),c(X),!,d(X),e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- c(1),!,d(1),e(1). X=1

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Example: cut ?- p(X). X=1; ?- p(X). ?- a(X).?- b(X),c(X),!,d(X),e(X). X=1 p(X):- a(X). p(X):- b(X),c(X),!,d(X),e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- c(1), !, d(1), e(1). X=1 ?- !, d(1), e(1).

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Example: cut ?- p(X). X=1; ?- p(X). ?- a(X).?- b(X),c(X),!,d(X),e(X). X=1 p(X):- a(X). p(X):- b(X),c(X),!,d(X),e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- c(1), !, d(1), e(1). X=1 ?- !, d(1), e(1). ?- d(1), e(1). X X No backtracking on choices for b(X) and c(X) and not on choice for p(X).

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Example: cut ?- p(X). X=1; no ?- p(X). ?- a(X).?- b(X),c(X),!,d(X),e(X). X=1 p(X):- a(X). p(X):- b(X),c(X),!,d(X),e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- c(1), !, d(1), e(1). X=1 ?- !, d(1), e(1). ?- d(1), e(1). †

© Patrick Blackburn, Johan Bos & Kristina Striegnitz What the cut does The cut only commits us to choices made since the parent goal was unified with the left-hand side of the clause containing the cut For example, in a rule of the form q:- p 1, …, p n, !, r 1, …, r n. when we reach the cut it commits us: –to this particular clause of q –to the choices made by p 1, …, p n –NOT to choices made by r 1, …, r n

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Using Cut

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Using Cut Consider the following predicate max/3 that succeeds if the third argument is the maximum of the first two max(X,Y,Y):- X =< Y. max(X,Y,X):- X>Y.

© Patrick Blackburn, Johan Bos & Kristina Striegnitz The max/3 predicate What is the problem? There is a potential inefficiency –Suppose it is called with ?- max(3,4,Y). –It will correctly unify Y with 4 –But when asked for more solutions, it will try to satisfy the second clause. This is completely pointless since these clauses are mutually exclusive! max(X,Y,Y):- X =< Y. max(X,Y,X):- X>Y.

© Patrick Blackburn, Johan Bos & Kristina Striegnitz max/3 with cut With the help of cut this is easy to fix Note how this works: –If the X =< Y succeeds, the cut commits us to this choice, and the second clause of max/3 is not considered –If the X =< Y fails, Prolog goes on to the second clause max(X,Y,Y):- X =< Y, !. max(X,Y,X):- X>Y.

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Green Cuts Cuts that do not change the meaning of a predicate are called green cuts The cut in max/3 is an example of a green cut: –the new code gives exactly the same answers as the old version, –but it is more efficient

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Another max/3 with cut Why not remove the body of the second clause? After all, it is redundant. How good is it? max(X,Y,Y):- X =< Y, !. max(X,Y,X).

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Another max/3 with cut Why not remove the body of the second clause? After all, it is redundant. How good is it? –okay max(X,Y,Y):- X =< Y, !. max(X,Y,X). ?- max(200,300,X). X=300 yes

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Another max/3 with cut Why not remove the body of the second clause? After all, it is redundant. How good is it? –okay max(X,Y,Y):- X =< Y, !. max(X,Y,X). ?- max(400,300,X). X=400 yes

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Another max/3 with cut Why not remove the body of the second clause? After all, it is redundant. How good is it? –oops…. max(X,Y,Y):- X =< Y, !. max(X,Y,X). ?- max(200,300,200). yes

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Revised max/3 with cut Unification after crossing the cut This does work max(X,Y,Z):- X =< Y, !, Y=Z. max(X,Y,X). ?- max(200,300,200). no Important: this is a very specific use of unification in the body of the rule. Do not use this if you don’t have to!

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Recall: simplifying allSame allSame([ ]). allSame([ _ ]). allSame([X,X|T]) :- allSame([X|T]). allSame([ ]). allSame([X]). allSame([H1| [H2|T] ]) :- H1=H2, allSame([H2|T]).

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Red Cuts Cuts that change the meaning of a predicate are called red cuts The cut in the revised max/3 is an example of a red cut: –If we take out the cut, we don’t get an equivalent program Programs containing red cuts –Are not fully declarative –Can be hard to read –Can lead to subtle programming mistakes

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Exercise Are the cuts in the programs below green or red? f(X,0):- X<3, !. f(X,2):- 3=<X, X<6, !. f(X,4):- 6=<X. member1(X,[X|_]):- !. member1(X,[_|L]):- member1(X,L).

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Negation as Failure

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Another built-in predicate: fail/0 As the name suggests, this is a goal that will immediately fail when Prolog tries to prove it That may not sound too useful But in combination with cut it enables writing interesting programs

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Vincent and burgers The cut fail combination allows to code exceptions enjoys(vincent,X):- bigKahunaBurger(X), !, fail. enjoys(vincent,X):- burger(X). burger(X):- bigMac(X). burger(X):- bigKahunaBurger(X). burger(X):- whopper(X). bigMac(a). bigKahunaBurger(b). bigMac(c). whopper(d).

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Vincent and burgers The cut fail combination allows to code exceptions enjoys(vincent,X):- bigKahunaBurger(X), !, fail. enjoys(vincent,X):- burger(X). burger(X):- bigMac(X). burger(X):- bigKahunaBurger(X). burger(X):- whopper(X). bigMac(a). bigKahunaBurger(b). bigMac(c). whopper(d). ?- enjoys(vincent,a). yes

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Vincent and burgers The cut fail combination allows to code exceptions enjoys(vincent,X):- bigKahunaBurger(X), !, fail. enjoys(vincent,X):- burger(X). burger(X):- bigMac(X). burger(X):- bigKahunaBurger(X). burger(X):- whopper(X). bigMac(a). bigKahunaBurger(b). bigMac(c). whopper(d). ?- enjoys(vincent,b). no What would the result of the query below be, if the first two clauses would be reversed?

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Negation as Failure (NAF) The cut-fail combination seems to be offering us some form of negation It is called negation as failure, and defined as follows: neg(Goal):- Goal, !, fail. neg(Goal).

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Negation in Logic The assertion ¬p specifies that ¬p is true, and therefore p is false E.g.: ¬p, ¬p → q |= q

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Negation in Prolog Negation in Prolog is different from negation in logic! In Prolog, negation is represented using the special predicate ‘not/1’

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Example burger(X):- bigMac(X). burger(X):- bigKahunaBurger(X). burger(X):- whopper(X). bigMac(a). bigKahunaBurger(b). bigMac(c). whopper(d). enjoys(vincent,X) :- burger(X), not(bigKahunaBurger(X)). not(bigKahunaBurger(X)) succeeds if bigKahunaBurger(X) cannot be derived, i.e., if bigKahunaBurger(X) fails Prolog uses “negation as failure”

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Example burger(X):- bigMac(X). burger(X):- bigKahunaBurger(X). burger(X):- whopper(X). bigMac(a). bigKahunaBurger(b). bigMac(c). whopper(d). enjoys(vincent,X) :- burger(X), not(bigKahunaBurger(X)). ?- enjoys(vincent,X).

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Example burger(X):- bigMac(X). burger(X):- bigKahunaBurger(X). burger(X):- whopper(X). bigMac(a). bigKahunaBurger(b). bigMac(c). whopper(d). enjoys(vincent,X):- burger(X), not(bigKahunaBurger(X)). ?- enjoys(vincent,X). X = a;

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Example burger(X):- bigMac(X). burger(X):- bigKahunaBurger(X). burger(X):- whopper(X). bigMac(a). bigKahunaBurger(b). bigMac(c). whopper(d). enjoys(vincent,X):- burger(X), not(bigKahunaBurger(X)). ?- enjoys(vincent,X). X = a; X = c;

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Example burger(X):- bigMac(X). burger(X):- bigKahunaBurger(X). burger(X):- whopper(X). bigMac(a). bigKahunaBurger(b). bigMac(c). whopper(d). enjoys(vincent,X):- burger(X), not(bigKahunaBurger(X)). ?- enjoys(vincent,X). X = a; X = c; X = d.

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Built-in NAF pred.: not or \+ Because negation as failure is so often used, there is no need to define it In standard Prolog the prefix operator not/1 or \+/1 means negation as failure So we could define Vincent`s preferences as follows: enjoys(vincent,X):- burger(X), not(bigKahunaBurger(X)). ?- enjoys(vincent,X). X=a; X=c; X=d. Important: use “not” instead of the cut and fail combination.

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Negation as failure and logic Negation as failure is not logical negation Changing the order of the goals in the vincent and burgers program gives a different behaviour: enjoys(vincent,X):- not(bigKahunaBurger(X)), burger(X). ?- enjoys(vincent,X). no not(bigKahunaBurger(X)) succeeds if bigKahunaBurger(X) cannot be derived, i.e., if there are no bigKahunaBurgers.

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Negation as failure and logic Negation as failure is not logical negation “not(p(X))” succeeds if p(X) cannot be derived – this is different from classical negation p, p → q |= ¬r ?- not(r). yes :- dynamic(r). p. q :- p.

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Negation as failure and logic Negation as failure is not logical negation “not” has a non-monotonic behavior: if formulas are added, it could be that it can no longer be derived p, p → q, r |= ¬r ?- not(r). no :- dynamic(r). p. r. q :- p.

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Negation as failure and closed world assumption (CWA) Closed world assumption: what is not currently known to be true, is false Example: if the train schedule does not include a train at 15:00 to A’dam, conclude that there will not be a train at 15:00 to A’dam NAF: if we cannot derive that something is true, conclude that it is not true

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Search

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Route Planning

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Robot Navigation

Unreal

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Search Problems Common structure: We are faced with an initial situation and we would like to achieve a certain goal. At any point in time we have different simple actions available to us (e.g. “turn left” vs. “turn right”). Executing a particular sequence of such actions may or may not achieve the goal. Search is the process of inspecting several such sequences and choosing a path that achieves the goal.

Deep Blue & Watson IBM Chess Computer (1997) IBM Jeopardy Computer (2011) atson-jeopardy-day-2/

© Patrick Blackburn, Johan Bos & Kristina Striegnitz State Space and Moves Navigation in Unreal Tournament: States are navigation points Navigation points can be connected to other navigation points (determine moves) Use search to determine how to get from the current navigation point to a desired one Moving from one navigation point to the next is associated with a cost (distance); Try to reach desired navigation point at minimal cost: optimisation problem.

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Searching the State Space The set of all possible sequences of legal moves form a tree Each branch (path from root to leaf) corresponds to a sequence of states (and thereby also a sequence of moves). Two basic ways of moving through such a tree: depth-first and breadth-first

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Depth-First Depth-first traversal: A→B→D→E→C→F→G

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Depth-First solve_depthfirst(Node, TargetNode, SolPath) :- depthfirst([], Node, TargetNode, SolPath). depthfirst(Path, Node, Node, [Node|Path]). depthfirst(Path, Node, TargetNode, SolPath) :- move(Node, NextNode), depthfirst([Node|Path], NextNode, TargetNode, SolPath).

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Breadth-First Breadth-first traversal: A→B→C→D→E→F→G

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Breadth-First Store a set of candidate paths If head of the first candidate path = target node then this path is a solution Otherwise remove first candidate path, generate the set of all one-step extensions of this path, add this set of extensions at the end of the candidate set, and execute breadth-first search on this updated set.

Summary of this Lecture We introduced the following key notions and their relation to Prolog: –“input & output” –negation as failure –search algorithms

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Organization Reading: LPN Sect from "Negation as failure is an impotant tool." to "The difference is crucial." Tomorrow “werkcollege” - Assignment 2 Next lecture (Koen Hindriks) - GOAL: introduction to agent programming