1 / 39

Chapter 7 Control and Negation

Chapter 7 Control and Negation. Taif University Fall 2010 Dr. Yasser Ahmed Nada. Some Useful Predicates for Control. true/0 ---  Always succeeds. father( jim,fred ) . is logically equivalent to father( jim,fred ):- true. father( jim,fred ):- true. ?-father( jim,fred ).

Télécharger la présentation

Chapter 7 Control and Negation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. prolog programming ....Dr.Yasser Nada

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

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

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

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

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

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

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

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

  10. 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) ). Query : ?- woman(jim). Result : no Query : ?- woman(jane). Result : yes prolog programming ....Dr.Yasser Nada

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

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

  13. 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 prolog programming ....Dr.Yasser Nada

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

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

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

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

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

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

  20. 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. Finite generator . Query : ?-integer_with_two_digit_square(X),write(X),fail. Result: 45no prolog programming ....Dr.Yasser Nada

  21. 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. inFinite generator . Query : ?-integer_with_two_digit_square(X),write(X),fail. Result: 456789no prolog programming ....Dr.Yasser Nada

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

  23. We have already met a program that satisfies this schema —one for parity/2 parity(X,Y):- odd(X), Y=odd. parity(X,Y):- \+(odd(X)), Y=even. plus set of facts defining odd/1 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

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

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

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

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

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

  29. 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 : ?-(write(hello),fail). Result : Hello no read/1 will be used to read some input from the user (we assume) and the call to fail/0 will fail. Query: ?-(read(X),fail) prolog programming ....Dr.Yasser Nada

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

  31. 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: 1 2 3 4 5 Yes prolog programming ....Dr.Yasser Nada

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

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

  34. 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 resatisfythe 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

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

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

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

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

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

More Related