Prolog Programming (Volume 4) Dr W.F. Clocksin. Backtracking and Nondeterminism member(X, [X|_]). member(X, [_|T]) :- member(X, T). ?- member(fred, [john,

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and Prolog
Advertisements

CS4026 Formal Models of Computation Part II The Logic Model Lecture 1 – Programming in Logic.
CS4026 Formal Models of Computation Part II The Logic Model Lecture 6 – Arithmetic, fail and the cut.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10: Cuts and Negation Theory –Explain how to control Prolog`s backtracking behaviour with.
First Order Logic Logic is a mathematical attempt to formalize the way we think. First-order predicate calculus was created in an attempt to mechanize.
1 Chapter 7: Controlling Search. 2 Controlling Search u Estimating Efficiency of a CLP Program u Rule Ordering u Literal Ordering u Adding Redundant Constraints.
Prolog The language of logic. History Kowalski: late 60’s Logician who showed logical proof can support computation. Colmerauer: early 70’s Developed.
Declarative Programming Lists in PROLOG Autumn 2014.
Prolog Programming (Volume 5) Dr W.F. Clocksin. List cells + Unification = Great Idea Normally we use ‘proper lists’, which are defined according to the.
11/10/04 AIPP Lecture 6: Built-in Predicates1 Combining Lists & Built-in Predicates Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture.
Justification-based TMSs (JTMS) JTMS utilizes 3 types of nodes, where each node is associated with an assertion: 1.Premises. Their justifications (provided.
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
Inference and Reasoning. Basic Idea Given a set of statements, does a new statement logically follow from this. For example If an animal has wings and.
1 Knowledge Based Systems (CM0377) Lecture 8 (Last modified 5th March 2001)
Prolog OR (disjunction) “;” is same as a logical OR “;” is same as a logical OR It is also equivalent to using separate clauses... It is also equivalent.
5/15/2015Slide 1 SOLVING THE PROBLEM The one sample t-test compares two values for the population mean of a single variable. The two-sample test of a population.
CS 330 Programming Languages 12 / 12 / 2006 Instructor: Michael Eckmann.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
© 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.
1 CILOG User Manual Bayesian Networks Seminar Sep 7th, 2006.
Database Systems More SQL Database Design -- More SQL1.
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
Transaction. A transaction is an event which occurs on the database. Generally a transaction reads a value from the database or writes a value to the.
Multiplying, Dividing, and Simplifying Radicals
Algebra Problems… Solutions Algebra Problems… Solutions © 2007 Herbert I. Gross Set 6 By Herb I. Gross and Richard A. Medeiros next.
Formal Models of Computation Part II The Logic Model
What is Sure BDCs? BDC stands for Batch Data Communication and is also known as Batch Input. It is a technique for mass input of data into SAP by simulating.
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.
CSE314 Database Systems More SQL: Complex Queries, Triggers, Views, and Schema Modification Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson.
CS 321 Programming Languages and Compilers Prolog part 2.
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.
Flow of Control Chapter 5. Flow of Control n What order computer uses to get answers –sub-goal ordering –clause ordering n Prolog flow-of-control –sequence.
15/11/04 AIPP Lecture 14: Database Manipulation1 Database Manipulation Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 14 15/11/04.
Introduction to Exception Handling and Defensive Programming.
Lecture 1 Introduction Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Black-box Testing.
Introduction To PROLOG World view of imperative languages. World view of relational languages. A PROLOG program. Running a PROLOG program. A PROLOG.
Unit III. Views A table that is derived from other tables Considered as a virtual table Does not store data.
Built-in Data Structures in Python An Introduction.
1 Knowledge Based Systems (CM0377) Lecture 3 (Last modified 5th February 2001)
1 COMP 205 Introduction to Prolog Dr. Chunbo Chu Week 14.
This is a slide show to explain in detail how to solve a puzzle of a common sort. Use the right arrow key to go to the next step and left arrow keys to.
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
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.
6. 1 Multiplication with Exponents and Scientific Notation 6
Ch. 13 Ch. 131 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (notes?) Dr. Carter Tiernan.
Chapter 2 Real Numbers and algebraic expressions ©2002 by R. Villar All Rights Reserved Re-engineered by Mistah Flynn 2015.
1/32 This Lecture Substitution model An example using the substitution model Designing recursive procedures Designing iterative procedures Proving that.
Prolog programming....Dr.Yasser Nada. Chapter 7 Control and Negation Taif University Fall 2010 Dr. Yasser Ahmed Nada prolog programming....Dr.Yasser Nada.
Changing Bases. Base 10: example number ³ 10² 10¹ 10 ⁰ ₁₀ 10³∙2 + 10²∙1 + 10¹∙ ⁰ ∙0 = 2120 ₁₀ Implied base 10 Base 8: 4110 ₈ 8³ 8².
For Monday Exam 1 is Monday Takehome due Prolog Handout 3 due.
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.
1 SQL: The Query Language. 2 Example Instances R1 S1 S2 v We will use these instances of the Sailors and Reserves relations in our examples. v If the.
Thinking in Sets and SQL Query Logical Processing.
The inference and accuracy We learned how to estimate the probability that the percentage of some subjects in the sample would be in a given interval by.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Prolog: Control, Negation and Cut
This Lecture Substitution model
Programming Paradigms and Languages
Prolog Programming (Volume 4)
This Lecture Substitution model
Representations & Reasoning Systems (RRS) (2.2)
Presentation transcript:

Prolog Programming (Volume 4) Dr W.F. Clocksin

Backtracking and Nondeterminism member(X, [X|_]). member(X, [_|T]) :- member(X, T). ?- member(fred, [john, fred, paul, fred]). yes ?- member(X, [john, fred, paul, fred]). X = john; X = fred; X = paul; X = fred; no Deterministic query Nondeterministic query

The problem of controlling backtracking colour(cherry, red). colour(banana, yellow). colour(apple, red). colour(apple, green). colour(orange, orange). colour(X, unknown). ?- colour(banana, X). X = yellow ?- colour(physalis, X). X = unknown ?- colour(cherry, X). X = red; X = unknown; no

The cut A built-in predicate, used not for its logical properties, but for its effect. Gives control over backtracking. The cut is spelled: ! The cut always succeeds. When backtracking over a cut, the goal that caused the current procedure to be used fails.

How it works Suppose that goal H is called in a program, and consider the following two clauses for H: H 1 :- B 1, B 2,..., B i, !, B k,..., B m. H 2 :- B n,... B p. If H 1 matches, goals B 1...B i may backtrack among themselves. If B 1 fails, H 2 will be attempted. But as soon as the ‘cut’ is crossed, Prolog commits to the current choice. All other choices are discarded.

Commitment to the clause Goals B k...B m may backtrack amongst themselves, but if goal B k fails, then the predicate fails (the subsequent clauses are not matched). H 1 :- B 1, B 2,..., B i, !, B k,..., B m. H 2 :- B n,... B p.

How to remember it Think of the ‘cut’ as a ‘fence’ which, when crossed as a success, asserts a commitment to the current solution. However, when a failure tries to cross the fence, the entire goal is failed. H 1 :- B 1, B 2,..., B i, !, B k,..., B m. commit fail calling goal

Cut has several uses 1. To define a deterministic (functional) predicate. Example: a deterministic version of member, which is more efficient for doing ‘member checking’ because it needn’t give multiple solutions: membercheck(X, [X|_]) :- !. membercheck(X, [_|L]) :- membercheck(X,L). ?- membercheck(fred, [joe, john, fred, paul]). yes. ?-membercheck(X, [a, b, c]). X = a; no.

Cut has several uses 2. To specify exclusion of cases by ‘committing’ to the current choice. Example: the goal max(X, Y, Z) instantiates Z to the greater of X and Y: max(X, Y, X) :- X >= Y. max(X, Y, Y) :- X < Y. Note that each clause is a logically correct statement about maxima. A version using cut can get rid of the second test, and might look like this: max(X, Y, X) :- X >= Y, !. max(X, Y, Y). (next slide explains this)

Max with cut max(X, Y, X) :- X >= Y, !. max(X, Y, Y). If max is called with X  Y, the first clause will succeed, and the cut assures that the second clause is never made. The advantage is that the test does not have to be made twice if X<Y. The disadvantage is that each rule does not stand on its own as a logically correct statement about the predicate. To see why this is unwise, try ?- max(10, 0, 0).

Max with cut So, it is better to using the cut and both tests will give a program that backtracks correctly as well as trims unnecessary choices. max(X, Y, X) :- X >= Y, !. max(X, Y, Y) :- X < Y. Or, if your clause order might suddenly change (because of automatic program rewriting), you might need: max(X, Y, X) :- X >= Y, !. max(X, Y, Y) :- X < Y, !.

A larger example. We’ll define several versions of the disjoint partial map split. split(list of integers, non-negatives, negatives). 1. A version not using cut. Good code (each can be read on its own as a fact about the program). Not efficient because choice points are retained. The first solution is desired, but we must ignore backtracked solutions. split([], [], []). split([H|T], [H|Z], R) :- H >= 0, split(T, Z, R). split([H|T], R, [H|Z]) :- H < 0, split(T, R, Z).

A larger example. 2. A version using cut. Most efficient, but not the best ‘defensively written’ code. The third clause does not stand on its own as a fact about the problem. As in normal programming languages, it needs to be read in context. This style is often seen in practice, but is deprecated. split([], [], []). split([H|T], [H|Z], R) :- H >= 0, !, split(T, Z, R). split([H|T], R, [H|Z]) :- split(T, R, Z). Minor modifications (adding more clauses) may have unintended effects. Backtracked solutions invalid.

A larger example. 3. A version using cut which is also ‘safe’. The only inefficiency is that the goal H < 0 will be executed unnecessarily whenever H < 0. split([], [], []). split([H|T], [H|Z], R) :- H >= 0, !, split(T, Z, R). split([H|T], R, [H|Z]) :- H < 0, split(T, R, Z). Recommended for practical use. Hidden problem: the third clause does not capture the idea that H < 0 is a committal. Here committal is the default because H < 0 is in the last clause. Some new compilers detect that H < 0 is redundant.

A larger example. 3. A version with unnecessary cuts split([], [], []) :- !. split([H|T], [H|Z], R) :- H >= 0, !, split(T, Z, R). split([H|T], R, [H|Z]) :- H < 0, !, split(T, R, Z). First cut unnecessary because anything matching first clause will not match anything else anyway. Most Prolog compilers can detect this. Why is the third cut unnecessary? Because H<0 is in the last clause. Whether or not H<0 fails, there are no choices left for the caller of split. However, the above will work for any order of clauses.

Mapping with Cut Remember squint ? Map integers to their squares, map anything else to itself: squint([], []). squint([X|T], [Y|L]) :- integer(X), !, Y is X * X, squint(T, L). squint([X|T], [X|L]) :- squint(T, L).

More Mapping with Cut Extract the even numbers... evens([], []). evens([X|T], [X|L]) :- 0 is X mod 2, !, evens(T, L). evens([X|T], L) :- evens(T, L). Extract the unique members... setify([], []). setify([X|T], L) :- member(X, T), !, setify(T,L). setify([X|T], [X|L]) :- setify(T,L).

Negation as Failure Using cut together with the built-in predicate fail, we may define a kind of negation. Examples: Mary likes any animals except reptiles: likes(mary, X) :- reptile(X), !, fail. likes(mary, X) :- animal(X). A utility predicate meaning something like “not equals”: different(X, X) :- !, fail. different(_,_).

Negation as Failure We can use the same idea of “cut fail” to define the predicate not, which takes a term as an argument. not will “call” the term, that is evaluate it as though it is a goal: not(G) fails if G succeeds not(G) succeeds if G does not succeed. In Prolog, not(G) :- call(G), !, fail. not(_). Call is a built-in predicate.

Negation as Failure Most Prolog systems have a built-in predicate like not. SICStus Prolog calls it \+. Remember, not does not correspond to logical negation, because it is based on the success/failure of goals. It can, however, be useful: likes(mary, X) :- not(reptile(X)). different(X, Y) :- not(X = Y).

Negation as Failure can be Misleading Once upon a time, a student who missed some of these lectures was commissioned to write a Police database system in Prolog. The database held the names of members of the public, marked by whether they are innocent or guilty of some offence. Suppose the database contains the following: innocent(peter_pan). innocent(X) :- occupation(X, nun). innocent(winnie_the_pooh). innocent(julie_andrews) guilty(X) :- occupation(X, thief). guilty(joe_bloggs). guilty(rolf_harris). Consider the following dialogue: ?- innocent(st_francis). no.

Problem. This cannot be right, beause everyone knows that St Francis is innocent. But in Prolog the above happens because st_francis is not in the database. Because the database is hidden from the user, the user will believe it because the computer says so. How to solve this?

not makes things worse Using not will not help you. Do not try to remedy this by defining: guilty(X) :- not(innocent(X)). This is useless, and makes matters even worse: ?- guilty(st_francis). yes It is one thing to show that st_francis cannot be demonstrated to be innocent. But it is quite another thing to incorrectly show that he is guilty.

Negation-by-failure can be non-logical Some disturbing behaviour even more subtle than the innocent/guilty problem, and can lead to some extremely obscure programming errors. Here is a restaurant database: good_standard(goedels). good_standard(hilberts). expensive(goedels). reasonable(R) :- not(expensive(R)). Consider the following dialogue: ?- good_standard(X), reasonable(X). X = hilberts But if we ask the logically equivalent question: ?- reasonable(X), good_standard(X). no.

Question Why do we get different answers for what seem to be logically equivalent queries? The difference between the questions is as follows. In the first question, the variable X is always instantiated when reasonable(X) is executed. In the second question, X is not instantiated when reasonable(X) is executed. The semantics of reasonable(X) differ depending on whether its argument is instantiated.

Not a Good Idea! It is bad practice to write programs that destroy the correspondence between the logical and procedural meaning of a program without any good reason for doing so. Negation-by-failure does not correspond to logical negation, and so requires special care.

How to fix it? One way is to specify that negation is undefined whenever an attempt is made to negate a non- ground formula. A formula is ‘ground’ if is has no unbound variables. Some Prolog systems issue a run-time exception if you try to negate a non-ground goal.

Clauses and Databases In a relational database, relations are regarded as tables, in which each element of an n-ary relation is stored as a row of the table having n columns. supplier joneschairred10 smithdeskblack50 Using clauses, a table can be represented by a set of unit clauses. An n-ary relation is named by an n-ary predicate symbol. supplier(jones, chair, red, 10). supplier(smith, desk, black, 50).

Clauses and Databases Advantages of using clauses: 1.Rules as well as facts can coexist in the description of a relation. 2.Recursive definitions are allowed. 3.Multiple answers to the same query are allowed. 4.There is no role distinction between input and output. 5.Inference takes place automatically.

Negation and Representation Like databases, clauses cannot represent negative information. Only true instances are represented. The battle of Waterloo occurred in How can we show that the battle of Waterloo did not take place in 1923? The database cannot tell us when something is not the case, unless we do one of the following: 1. ‘Complete’ the database by adding clauses to specify the battle didn’t occur in 1814, 1813, 1812,..., 1816, 1817, 1818, Add another clause saying the battle did not take place in another year (the battle occurred in and only in 1815). 3. Make the ‘closed world assumption’, implemented by ‘negation by failure’.

WS23 Frequency Distribution Find the frequency distribution of a list of keys (integers) and sort into order. Represent “N many Xs” as N*X. Example: ?- evens([3, 3, 2, 2, 1, 1, 2, 2, 3, 3], A). A = [2*1, 4*2, 4*3] There are two 1’s, four 2’s and four 3’s. Something like run-length encoding, but each output entry gives the count (frequency) of each key in the whole input list. Also, note that keys are sorted into ascending order.

Frequency Distribution Define the predicate freq such that the goal freq(L,S) succeeds for input list L and frequency list S: freq(L, S) :- freq(L, [], S). freq([], S, S). freq([N|L], S1, S3) :- update(N, S1, S2), freq(L, S2, S3). All the work is done by update, which takes a key and the list before and after it is updated with the information in the key.

Frequency Distribution /* update(Key, BeforeList, AfterList) */ update(N, [], [1*N]). update(N, [F*N|S], [F1*N|S]) :- !, F1 is F + 1. update(N, [F*M|S], [1*N,F*M|S]) :- N < M, !. update(N, [F*M|S], [F*M|S1]) :- N \== M, update(N, S, S1). The cases are: 1. Reached end of list. 2. Found one we have already seen. 3. Found one that should be inserted before a seen one. 4. Still searching list, just recur.