Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Prolog programming....Dr.Yasser Nada. Chapter 7 Control and Negation Taif University Fall 2010 Dr. Yasser Ahmed Nada prolog programming....Dr.Yasser Nada."— 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 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 =

4 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

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

6 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

7 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

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

9 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

10 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

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

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

13 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

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

15 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

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

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

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

19 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

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

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

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

23 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

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

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

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

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

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

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

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

31 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: 1 2 3 4 5 Yes

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

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

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

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

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

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

38 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

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


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

Similar presentations


Ads by Google