Prolog programming....Dr.Yasser Nada. Chapter 7 Control and Negation Taif University Fall 2010 Dr. Yasser Ahmed Nada prolog programming....Dr.Yasser Nada.

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and Prolog
Advertisements

Prolog programming....Dr.Yasser Nada. Chapter 8 Parsing in Prolog Taif University Fall 2010 Dr. Yasser Ahmed nada prolog programming....Dr.Yasser Nada.
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.
Copyright © Cengage Learning. All rights reserved.
Introduction to Computing Science and Programming I
F22H1 Logic and Proof Week 7 Clausal Form and Resolution.
Learning Objectives Explain similarities and differences among algorithms, programs, and heuristic solutions List the five essential properties of an algorithm.
Copyright © Cengage Learning. All rights reserved.
Algebra Problems… Solutions Algebra Problems… Solutions © 2007 Herbert I. Gross Set 4 By Herb I. Gross and Richard A. Medeiros next.
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
1 Introduction to Computability Theory Lecture12: Decidable Languages Prof. Amos Israeli.
1 Introduction to Computability Theory Lecture12: Reductions Prof. Amos Israeli.
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.
Topic 6 – Repetition and Loops. CISC 105 – Topic 6 Program Repetition Repetition refers to the repeats of certain program statements within a program.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
Loops – While, Do, For Repetition Statements Introduction to Arrays
1 CILOG User Manual Bayesian Networks Seminar Sep 7th, 2006.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Intro to Robots Conditionals and Recursion. Intro to Robots Modulus Two integer division operators - / and %. When dividing an integer by an integer we.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
Fundamentals of Python: From First Programs Through Data Structures
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
Fundamentals of Python: First Programs
Testing Hypotheses Tuesday, October 28. Objectives: Understand the logic of hypothesis testing and following related concepts Sidedness of a test (left-,
CHAPTER 2 LIMITS AND DERIVATIVES. 2.2 The Limit of a Function LIMITS AND DERIVATIVES In this section, we will learn: About limits in general and about.
FATIH UNIVERSITY Department of Computer Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CENG 421 Fall03.
Chapter 5: Control Structures II (Repetition)
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
CS 321 Programming Languages and Compilers Prolog part 2.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
MATH 224 – Discrete Mathematics
Copyright © Cengage Learning. All rights reserved. CHAPTER 4 ELEMENTARY NUMBER THEORY AND METHODS OF PROOF ELEMENTARY NUMBER THEORY AND METHODS OF PROOF.
Institute for Personal Robots in Education (IPRE)‏ CSC 170 Computing: Science and Creativity.
Extending the Definition of Exponents © Math As A Second Language All Rights Reserved next #10 Taking the Fear out of Math 2 -8.
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.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Black Box Testing Techniques Chapter 7. Black Box Testing Techniques Prepared by: Kris C. Calpotura, CoE, MSME, MIT  Introduction Introduction  Equivalence.
CPS120: Introduction to Computer Science Decision Making in Programs.
Copyright © Cengage Learning. All rights reserved. CHAPTER 8 RELATIONS.
Automated Reasoning Early AI explored how to automated 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.
Using Loops. Goals Understand how to create while loops in JavaScript. Understand how to create do/while loops in JavaScript. Understand how to create.
CS 106 Introduction to Computer Science I 09 / 26 / 2007 Instructor: Michael Eckmann.
Function Definition by Cases and Recursion Lecture 2, Programmeringsteknik del A.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
Chapter 10 Algorithmic Thinking. Learning Objectives Explain similarities and differences among algorithms, programs, and heuristic solutions List the.
Copyright © Cengage Learning. All rights reserved. Sequences and Series.
07/10/04 AIPP Lecture 5: List Processing1 List Processing Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 5 07/10/04.
Fortran: Control Structures Session Three ICoCSIS.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Overview of the theory of computation Episode 3 0 Turing machines The traditional concepts of computability, decidability and recursive enumerability.
While loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
Foundations of Discrete Mathematics Chapter 1 By Dr. Dalia M. Gil, Ph.D.
Programming in Java (COP 2250) Lecture 12 & 13 Chengyong Yang Fall, 2005.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Notions & Notations - 1ICOM 4075 (Fall 2010) UPRM Department of Electrical and Computer Engineering University of Puerto Rico at Mayagüez Fall 2010 ICOM.
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Chapter 6: Repetition Statements
Computing Fundamentals
Programming Techniques
Java Programming Loops
Presentation transcript:

prolog programming....Dr.Yasser Nada

Chapter 7 Control and Negation Taif University Fall 2010 Dr. Yasser Ahmed Nada prolog programming....Dr.Yasser Nada

Some Useful Predicates for Control prolog programming....Dr.Yasser Nada  true/0 ---  Always succeeds. father(jim,fred). is logically equivalent to father(jim,fred):- true. father(jim,fred). ?-father(jim,fred). Result : Yes father(jim,fred):- true. ?-father(jim,fred). Result : yes =

prolog programming....Dr.Yasser Nada  fail/0…… Always fails. lives forever(X):- fail. mean that any attempt to solve the goal lives forever(X) will fail. lives_forever(X):- fail. ?-lives_forever(a). Result : No lives_forever(X):- fail. ?-lives_forever(X). Result : No

prolog programming....Dr.Yasser Nada  repeat/0 ….If it is asked to Redo then it will keep on succeeding test:- repeat, write(hello), fail. Result : hellohellohellohellohellohellohellohellohello......

prolog programming....Dr.Yasser Nada call/1 The goal call(X) will call the interpreter as if the system were given the goal X. Therefore X must be bound to a legal Prolog goal. ?- call(write(hello)). Result : hello yes ?- call((write(hello),nl)). Result : hello yes

prolog programming....Dr.Yasser Nada The Problem of Negation  To maintain the connection with predicate logic, we would like to be able to represent the negation of a statement. man(jim). man(fred). Query : ?- man(bert). Result : no

prolog programming....Dr.Yasser Nada Turning to Prolog, If we try to solve a goal for which there is no clause (as in the case above) then we assume that we have provided Prolog with all the necessary data to solve the problem. This is known as the Closed World Assumption. This enables us to stick to the desirable property that a goal can have only two outcomes. +/1 This strangely named predicate is Prolog’s equivalent to the not (often written as : which stands for negation) of predicate logic.

prolog programming....Dr.Yasser Nada Query : ?- \+( man(jim) ). Result : No Data Base man(jim). man(fred). will succeed if man(jim) fails and will fail if man(jim) succeeds. Query : ?- \+( man(ali) ). Result : yes

prolog programming....Dr.Yasser Nada Negation as Failure Negation as failure is the term used to describe how we use the closed world assumption to implement a form of negation in Prolog. Example : man(jim). man(fred). woman(X):- \+( man(X) ). man(jim). man(fred). woman(X):- \+( man(X) ). Query : ?- woman(jim). Result : no Query : ?- woman(jim). Result : no Query : ?- woman(jane). Result : yes Query : ?- woman(jane). Result : yes

prolog programming....Dr.Yasser Nada Consider: ?- woman(X). It succeeds if man(X) fails —but man(X) succeeds with X bound to jim. So woman(X) fails and, because it fails, X cannot be bound to anything. We can read ?- woman(X) as a query “is there a woman?” and this query failed. note that \+(\+(man(X))) is not identical to man(X)

prolog programming....Dr.Yasser Nada Using Negation in Case Selection We can use \+/1 to define relations more carefully than previously To illustrate, consider parity(X,odd):- odd(X). parity(X,even). together with the set of facts defining odd/1.

prolog programming....Dr.Yasser Nada Data Base : odd(1). odd(3). odd(5). odd(7). odd(9). parity(X,odd):- odd(X). parity(X,even). Data Base : odd(1). odd(3). odd(5). odd(7). odd(9). parity(X,odd):- odd(X). parity(X,even). Query: ?- parity(7,X). Result : yes Query: ?- parity(7,X). Result : yes

prolog programming....Dr.Yasser Nada The goal parity(7,X) is intended to succeed using the first clause But by using fail Query : ?- parity(7,X),write(X),fail. Result : odd even no Query : ?- parity(7,X),write(X),fail. Result : odd even no Using first and second clause. This is error.

prolog programming....Dr.Yasser Nada Thus \+/1 provides extra expressivity as we do not need a set of facts to define even/1. We can fix this using \+/1. parity(X,odd):- odd(X). parity(X,even):- \+(odd(X)). We can fix this using \+/1. parity(X,odd):- odd(X). parity(X,even):- \+(odd(X)). Query: ?- parity(2,X),write(X). Result: evenYes. Query: ?- parity(2,X),write(X). Result: evenYes. Query: ?- parity(7,X),write(X),fail. Result : odd no Query: ?- parity(7,X),write(X),fail. Result : odd no

prolog programming....Dr.Yasser Nada Some General Program Schemata Generate — Test One of the most common techniques in Prolog is to use the backtracking in first generating a possible solution, then testing the possible solution to see if it is acceptable. If not, backtracking takes place so that another possible solution can be generated. generate and test(Info,X):-... generate(Info,X), test(Info,X),...

prolog programming....Dr.Yasser Nada  two kinds of generator: a finite generator and an infinite generator. We define a predicate integer with two digit square/1 to produce a positive integer that has a square which is greater than or equal to 10 and less than 100. integer with two digit square(X):- int(X), test square(X). test square(X):- Y is X*X, Y >= 10, Y < 100.

prolog programming....Dr.Yasser Nada Here is the definition of int/1 which is a finite generator —because there are only a finite number of unit clauses (containing no variables) used to define int/1. int(1). int(2). int(3). int(4). int(5). The goal integer _with _two _digit _square(X) eventually fails because the generator runs out of potential solutions.

prolog programming....Dr.Yasser Nada  Now we define a version of int/1 which is an infinite generator int(1). int(N):- int(N1), N is N1 +1. On backtracking, this will generate a new solution for integer with two digit square(X) until we test 10

prolog programming....Dr.Yasser Nada Data Base: int(1). int(2). int(3). int(4). int(5). integer_with_two_digit_square(X):- int(X), test_square(X). test_square(X):- Y is X*X, Y >= 10, Y < 100. Query : ?- integer_with_two_digit_square(X),write(X),fail. Result: 45no Query : ?- integer_with_two_digit_square(X),write(X),fail. Result: 45no Finite generator.

prolog programming....Dr.Yasser Nada Data Base int(1). int(N):- int(N1), N is N1 +1. integer_with_two_digit_square(X):- int(X), test_square(X). test_square(X):- Y is X*X, Y >= 10, Y < 100. Query : ?- integer_with_two_digit_square(X),write(X),fail. Result: no Query : ?- integer_with_two_digit_square(X),write(X),fail. Result: no inFinite generator.

prolog programming....Dr.Yasser Nada Test — Process Now we look at another fundamental schema. The idea with test — process is to guarantee that some inputs will only be ‘processed’ if the input passes a test. test process(Info,X,Y):- test(Info,X), process(Info,X,Y). We usually want to make sure that 1. test does not have alternative ways of confirming that the generated element is ok 2. process does not have alternative ways of‘processing’ the input. In short, we often want only one way of finding an output.

prolog programming....Dr.Yasser Nada parity(X,Y):- odd(X), Y=odd. parity(X,Y):- \+(odd(X)), Y=even. plus set of facts defining odd/1 We have already met a program that satisfies this schema — one for parity/2 This example illustrates that if the input argument is an integer then we see two cases: either the integer is even or it is odd. There is no third case. Nor can any integer be both even and odd

prolog programming....Dr.Yasser Nada  As in the above example, the usage of test — process is closely coupled with the idea of writing all the clauses for a predicate in this form —each clause is designed to handle one ‘class’ of input. The whole scheme falls down if we do not design the ‘classes’ of input to be disjoint –i.e. no input falls into more than one category. We also require that each input falls in at least one category —to summarise, each input falls in one and only one class.

prolog programming....Dr.Yasser Nada member(Element,[Element|Tail]). member(Element,[Head|Tail]):- member(Element,Tail). Now member/2 can be used as a generator if the first argument is a variable and its second argument is a list —as in the goal member(X,[a,b,c,d,e,f]. The first solution for X is the first element of the list [a,b,c,d,e,f]. On redoing, we get, in succession, X bound to the different elements in the list.

prolog programming....Dr.Yasser Nada We now rewrite using the test — process schema. We also rename the predicate to the standard name of memberchk/2 memberchk(Element,[Head|Tail]):- Element = Head. memberchk(Element,[Head|Tail]):- \+(Element = Head), memberchk(Element,Tail). If the mode of use is mode memberchk(+,+) then the meaning is that we check that the first argument is an element of the list (which is the second argument).

prolog programming....Dr.Yasser Nada Failure-Driven Loop We now introduce an extremely procedural programming technique for simulating a kind of iteration. The idea is deliberately generate a term and then fail. This suggests the useless schema failure driven loop(Info):- generate(Info,Term), fail. failure driven loop(Info).

prolog programming....Dr.Yasser Nada We now use side effecting predicates to do something useful with the generated term. A side-effecting predicate is one that is (often) logically equivalent to true but also does something else that is non-logical. For example, write/1 and nl/0 have the side-effect of writing material onto the terminal screen. Also, consult/1 and reconsult/1 have the side-effect of changing the program. The predicate read/1 has the side-effect of destructively reading input from the terminal (or whatever).

prolog programming....Dr.Yasser Nada Query : ?-(write(hello),fail). Result : Hello no Query : ?-(write(hello),fail). Result : Hello no write/1 will be used to write hello on the terminal screen and the call to fail/0 will fail. logically, we have a statement with the truth value of false —so we have proved that the goal cannot succeed and therefore there should be no message (hello) on the screen. write/1 will be used to write hello on the terminal screen and the call to fail/0 will fail. logically, we have a statement with the truth value of false —so we have proved that the goal cannot succeed and therefore there should be no message (hello) on the screen. Query: ?-(read(X),fail) Query: ?-(read(X),fail) read/1 will be used to read some input from the user (we assume) and the call to fail/0 will fail. read/1 will be used to read some input from the user (we assume) and the call to fail/0 will fail.

prolog programming....Dr.Yasser Nada We can see that any predicate succeeds generating an effect that cannot be undone on backtracking must be a side-effecting predicate. The complete failure-driven loop schema can be taken as: failure driven loop(Info):- generate(Info,Term), side effect(Term), fail. failure driven loop(Info).

prolog programming....Dr.Yasser Nada We illustrate with a simple example. We will use int/1 as a finite generator and then print out the valid arguments for this relation on the screen. int(1). int(2). int(3). int(4). int(5). print int:- int(X), write(X),nl, fail. print int. Query : ?-print_int. Result: Yes

prolog programming....Dr.Yasser Nada Some Practical Problems We now come to some needs that cannot easily be satisfied and still retain a clean declarative reading. We look at three problems that are interconnected.  Commit We have outlined the use of test — process to do case analysis but it was necessary to have one clause for each case. If we have a goal which can be satisfied via two different clauses then, on redoing, the same goal may generate a different solution.

prolog programming....Dr.Yasser Nada In reality, this situation can arise quite often —i.e. the tests we do on the input do not divide the input into non-overlapping classes. Essentially, we have two problems. We often want to make sure that only one clause is legitimate —once it has been determined that the input passes some test. We think of this as a statement of commitment to the solution(s) derived through ‘processing’ the input. test process(Info,X,Y):- test(Info,X), commit, process(Info,X,Y).

prolog programming....Dr.Yasser Nada When we backtrack and try to find another way of satisfying some program that makes use of the test — process schema then we first try to find another way of satisfying the process part. If that fails, then we try to resatisfy the test part. We do not want this to happen. Then, assuming that we cannot resatisfy the test part, we try to resatisfy the goal making use of this program by trying different clauses. Therefore there are two senses in which we may want to be ‘committed’: we want to commit to using a single clause and we want to commit to the result of a test — we do not want to run the risk that the test can be successful(with the same input) twice.

prolog programming....Dr.Yasser Nada  Satisfy Once Only Sometimes, we would like a way of stopping Prolog looking for other solutions. That is, we want some predicate to have only one solution (if it has one at all). This is the requirement that the predicate be determinate. Naturally, predicates which do not have this property are indeterminate. This is a desirable property sometimes — e.g. the generate — test schema makes use of the generator being indeterminate. On the other hand, it can cause major problems when a program has many predicates which are unintentionally indeterminate. Our aim is to make sure that those predicates which should be determinate actually are determinate.

prolog programming....Dr.Yasser Nada We have already met an example of a predicate (memberchk/2) that might have been written with this situation in mind. We recall that member/2 used with mode member(-,+) behaves as a generator. Perhaps it is worth pointing out that member/2 with mode member(+,+) is also, under certain circumstances, resatisfiable —precisely when there are repetitions of the sought element in the list which constitutes the second argument. Of course, if we are dealing with lists–as–sets, we should have arranged it so that the second argument does not have repeated elements. Anyway, it is very desirable to have a determinate version of member/2 available. memberchk(X,[X|Y]):- make determinate. memberchk(X,[Y|Z]):- memberchk(X,Z).

prolog programming....Dr.Yasser Nada  Fail Goal Now We often search for the solution to a goal using several clauses for some predicate. For example, we might have a social security calculation which tries to assign how much money to give a claimant. Here is a fragment of program: calculate benefit(Claim Number,Nationality,Age,Other Details):- Nationality = british, calculate british entitlement(Age,Other Details). calculate benefit(Claim Number,Nationality,Age,Other Details):- Nationality = martian, give up. calculate benefit(Claim Number,Nationality,Age,Other Details):- Nationality = french, calculate french entitlement(Age,Other Details).

prolog programming....Dr.Yasser Nada If we reach the situation where we realise that the whole search is doomed then we may want to say something informally like ‘stop this line of approach to the solution and any other corresponding line’. In the above, if we find we are trying to assign benefit to a martian then we make the decision that calculate benefit/4 should fail and therefore that there is no point in trying to use any remaining clauses to find a solution. In practice, we need to make use of this kind of action. Again, we are potentially asking Prolog to behave abnormally. In fact, in all these situations, we are asking Prolog to behave in a nonstandard way. Whatever the complications, it is hard top make do without ways to:  Commit  Make Determinate  Fail Goal Now

prolog programming....Dr.Yasser Nada End of chapter