Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.

Similar presentations


Presentation on theme: "Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC."— Presentation transcript:

1 Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC PROGRAMMING (WEEK 6) Eleni E. Mangina Department of Computer Science University College Dublin

2 Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Lecture 16

3 Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD The procedure box model GOAL CALL FAIL EXIT REDO EXCEPTION ERROR

4 Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD The flow of processing A Prolog goal can have the following happen to it: CALL. This is a normal invocation (“from the left”). To carry this out, the system looks for a matching clause, starting from the first clause with the same predicate-name and number of arguments. EXIT. When a goal has been satisfied, processing moves to the next goal in the program, leaving a note of the clause and variable bindings by which the goal succeeded. FAIL. This happens if: there are no matching clauses left (either when a chosen clause body fails or when a REDO is attempted), or the built-in predicate fail is performed, or a built-in predicate does not succeed. REDO. This happens if the immediately succeeding goal fails (or “;” is typed at the user interface). The sequence of subgoals (if any) that achieved the previous success is reconsidered “from the right”, with a REDO happening to the rightmost one; if there are no subgoals available, the goal itself is matched against available clauses in the program, starting from the marker left when the previous EXIT occurred.

5 Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD A closer look at REDO Consider a program such as: scan_message(M, info(H,T)):- header(M, H), text(M,T). header(M, H):- find_start(M, M1), interpret(M1, H), check(M1).....

6 Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Suppose: a scan_message goal matches the clause shown, this CALLs a header goal, that goal matches the clause shown, all subgoals in that clause succeed (EXIT) a text goal is CALLed, that text goal FAILs. The next step is not a search for further clauses for header. Instead, the sequence of subgoals (find_start, interpret, check) has a REDO from its righthand end.

7 Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD REDO - To REDO a previously successful (EXITed) goal – REDO the subgoals that caused that success, starting from the righthand (most recent) one.

8 Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Call vs. Redo Consider a sequence of goals: a(X, Y), b(Y,Z), c(Z,W). Suppose (ignoring variable bindings): a(X, Y) is CALLed, a(X, Y) succeeds (EXITs), b(Y, Z) succeeds (EXITs), c(Z,W) FAILs, the REDO of b(Y, Z) FAILs, the REDO of a(X, Y) succeeds (EXITs), there is then a CALL of b(Y, Z) (with new bindings, probably). This is a CALL, not a REDO – it comes “from the left”. Hence, the search for matching clauses for b(Y, Z) starts from the first (top) clause for that predicate – not from any stored marker part-way through the clauses.

9 Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD More Non-logical Features of Prolog Commit Operators Implementing Negation as Failure

10 Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Commit Operators In logic programming, a commit operator allows us to choose a particular proof of a predicate over all others – we commit to a particular choice Warning: commits are generally very difficult to account for logically, and it is often wise to avoid their use Prolog has two commit operators, ! Cut – prevents backtracking within the predicate in which it appears -> local cut – prevents backtracking into and within the goal to its left Oddly, we write ! As though it were a literal in Prolog, and not an operator

11 Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD The cut The special built-in operator “!” can be used to cause stored information about backtracking choices to be discarded. Used for: Efficiency – avoids needless REDO-ing which cannot succeed. Simpler programs – conditions for choosing clauses can be simpler. Robust predicates – definitions behave properly when forced to REDO.

12 Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD The cut in action

13 Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Using the cut The cut symbol acts like a (built-in) predicate. The cut always succeeds (and EXITs) when CALLed. When an attempt is made to REDO the cut (i.e. the goal immediately following it FAILs), it causes a FAIL of the goal which caused the clause containing the cut to be chosen. Even if the cut which triggers the FAIL occurs in a clause which is not the last one for that predicate, no further clauses will be tried for the goal being failed. “When a cut is encountered as a goal, the system thereupon becomes committed to all choices made since the parent goal was invoked. All other alternatives are discarded. Hence an attempt to re-satisfy any goal between the parent goal and the cut goal will fail.”

14 Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Uses of the cut To indicate that other clauses of this predicate are to be skipped, even on backtracking. Cut placed at whatever stage of clause counts as commitment to this clause being chosen – could be at start, if unification of the head is sufficient evidence, or part-way through, or even at the end, if whole body has to be processed before it is certain that this is the right clause. Between a sequence of pre-conditions (whose manner of succeeding is irrelevant) and some further subgoals (whose chances of success are unaffected by the way the pre- conditions succeeded). Cut part-way through a clause. Along with the special predicate fail, to indicate that if this point has been reached, the goal should fail regardless of other clauses. The sequence “!, fail” wherever goal is to be abandoned.

15 Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Robust backtracking factorial

16 Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD More robust factorial

17 Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Count

18 Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Count

19 Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Avoiding pointless recomputing Suppose that to qualify for a grant, an applicant needs to meet certain basic eligibility conditions, and to have on file at least one referee’s report that gives him/her a good rating; eligibility conditions can be met in a number of ways. A program handling such information might have a (one-clause) predicate thus: grantcandidate(Person):- eligible(Person), hasreferee(Person, Ref), goodrating(Person, Ref). There is no point in re-computing eligibility in many different ways if the later conditions (hasreferee, goodrating) fail. grantcandidate(Person):- eligible(Person), !, hasreferee(Person, Ref), goodrating(Person, Ref).

20 Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Summary It is important to distinguish the CALL and the REDO of a goal. To REDO a goal, REDO its subgoals from the right. A CALL starts searching from the first clause, a REDO picks up from where it left off previously. The cut symbol discards choice point information. The cut can be used in various ways to control the flow of backtracking.

21 Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Lecture 17

22 Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Cut Example p(a).p(a) :- !.p(b).?- p(X).X = a? ; X = b? ;no no

23 Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Cut (2) Example 2:p(a). p(b): - !.p(b). p(c).p(c):- !.?- p(X).X = a? ;X = b? ; noX = c? ; no

24 Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Cut (3) Example 3:p(a). p(b): - !.p(b) :- !.p(c). ?- p(c).?- (X=b; X=c), p(X). yesX = b? ; X = c? ; no

25 Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Guarded commit We can use ! In a logically friendly way, by building clauses of this general form: p(X) :- guard1(X),!,call1(X). p(X) :- guard2(X),!,call2(X). … p(X) :- guardN(X),!,callN(X). This is the commonest form of use of what we call a “green” cut As usual, green means “go on, it’s OK” So a red cut is not acceptable

26 Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Red and green cuts A green cut is one which can be removed without changing the logical behaviour of the program For example max(X,Y,X) :- X > Y, !. max(X,Y,Y) :- X =< Y. The effect here is purely procedural… … we do not need to test the = has succeeded So we save some processing power Be careful when using green cuts!!!

27 Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Red and green cuts (2) A red cut is one which can not be removed without changing the logical behaviour of the program For example max(X,Y,X) :- X > Y, !. max(X,Y,Y). If we remove the !, we can get an incorrect result of backtracking, because the logical semantics of the program does not match the intended interpretation Do not use red cuts!!!

28 Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Local cut Local cut -> can be even more unfriendly than ! In some circumstances, -> behaves exactly like logical implies In others, it has no proper logical interpretation Example: p(X) :- (q(X) -> r(X)). logically means “p(X) is true if r(X) is true whenever q(X) is true” In fact, it means “find the first solution for q(x), and then try to prove r(X); if there is no solution for q(X), fail”

29 Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Local cut (2) Because of local cut’s procedural behaviour, incorrect results can be obtained Example: p(a).p(b). p(b).p(a).q(b).?- p(X) -> q(X). noX = b? ; no

30 Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Local cut (3) Local cut is commonly used to implement an “if-then- else” construction ?- (p(X) -> q(X) ; r(X) ). which is supposed to mean “if p(X) is true, prove q(X); otherwise, prove r(X)”. However, this is only guaranteed to be correct if p(X) is fully instantiated when the call is made. In SICStus, -> ; is a special operator which means (roughly) if-then-else. In some Prolog, backtracking on ; can give incorrect answers.

31 Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Implementing negation as failure Once we have !, we can implement NAF \+ Goal :- call (Goal), !, fail. \+ _Goal. call/1 attempts prove the goal given as its argument; fail/0 is always false

32 Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Lecture 18

33 Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Operators What operators do How the built-in operators work How to define your own operators Operators as functors for structures Operators as predicate-names

34 Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD What is an operator? Functors and predicate names generally precede arguments, with arguments grouped using brackets: ancestor(fred, P). find_age(person(fred,smith), Age). To allow these names to be positioned elsewhere, they have to be operators. An operator can be: – infix (placed between its arguments) – prefix (placed before its arguments, without the need for brackets) – prefix (placed after its arguments) PURELY A NOTATIONAL CONVENIENCE.

35 Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Standard Operators There are several built-in operators. All the arithmetical symbols are operators; e.g.: 2 + 3: infix -4 : prefix All the standard punctuation symbols are operators; e.g.: :- is an infix operator, with the head and body of the clause as its arguments. (There is also a prefix version.) The comma is an infix operator. ?- is a prefix operator.

36 Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Operator positions Usually, an operator can be written in conventional position, and means exactly the same: ?- X is +(3, *(2,4)). X = 11 yes ?- X is 3 + 2 * 4. X = 11 yes ?- +(3, *(2,4)) = 3 + 2 * 4. yes

37 Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD For some of the fundamental punctuation symbols of Prolog, such as comma, the name of the operator has to be put in quotes before this can be done: | ?- X = ’,’(a, b). X = a,b ? yes But it’s usually not a good idea to use very basic Prolog notation in non-standard positions.

38 Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Operator grouping a + b * c means the same as: a + (b * c) but not: (a + b) * c Arithmetical operators obey grouping conventions, just as in ordinary algebra/arithmetic.

39 Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Operator precedence In Prolog, grouping is controlled by every operator having a precedence, which indicates its priority relative to other operators. (All values shown are for Sicstus Prolog). Precedence 500 (both for infix and for prefix versions) : + - Precedence 400 : * / // Precedence 300 : mod Lower numbers “stick together” more; higher numbers become the “outer” connectors. All the operators have a precedence, ranging from 1200 (for :- and for -->) to 200 (for ˆ).

40 Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Associativity a + b + c might seem to be ambiguous between: a + (b + c) and: (a + b) + c This makes a difference. Should this happen: ?- X + Y = a + b + c. X = a Y = (b+c) yes or this? ?- X + Y = a + b + c. X = (a+b) Y = c yes

41 Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD A similar problem could arise for any infix operator. Operators are defined to take, alongside, expressions whose functor/operator is either: – of a strictly lower precedence value (notated x); – of an equal or lower precedence value (notated y). The “+” sign is ‘y’ to its left, ‘x’ to its right. Hencea + b + cmust mean(a + b) + c, as this makes its left argument be(a + b), whose principal connector is also “+”, i.e. of the same precedence. If the right argument were(b + c), that would violate the “strictly lower precedence to the right” constraint.

42 Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Example

43 Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD

44 Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Operator Definitions The Prolog notation for defining an operator uses the predicate op/3, with arguments – the numerical precedence – an expression indicating the associativity – the name of the operator, or a list of names for several operators. So the arithmetical operators are defined as if the system had executed the goals: ?- op(500, yfx, [ +, -]). ?- op(500, fx, [ +, - ]). ?- op(400, yfx, [ *, /, //]). ?- op(300, xfx, [ mod ]). A programmer can define new operators in this way.

45 Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Defining an operator as a structure Suppose we want a data structure for time- durations, in hours and minutes. Could use a structure duration(3, 14) where the 1st component represents hours, 2nd is minutes. Could make this an infix operator “hr” so our structures would look like 3 hr 14

46 Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Choosing precedence What precedence level? (Relative position in hierarchy matters, not exact numerical code.) If we want to allow 3 + 2 hr 5 * 10 to be grouped as: (3 +2) hr (5 * 10) then place “hr” higher than the arithmetical ops. Defining “hr” lower than arithmetic would interpret it as: 3 + (2 hr 5) * 10 We will choose the former – somewhere between 500 and 550 (the next op upwards) will do.

47 Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Choosing associativity We need only consider left- or right-associativity if we might have expressions such as: 2 hr 5 hr 10 We can assume these won’t occur. So make both sides same, either “x” or “y”. Eventual definition: ?- op(525, xfx, hr) This allows “hr” to be used as an infix operator in Prolog expressions, meaning a structure with functor “hr” and two arguments.

48 Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Defining an operator predicate Suppose we wanted to compare our time structures (items with an hr operator) for size, and wanted to define an suitable-looking operator for this “less than” comparison. Let us choose a name for this operator: <<< So we want to allow goals such as: ?- 3 hr 45 <<< 4 hr 20. yes

49 Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Precedence? We want 3 hr 45 <<< 4 hr 20 to be grouped as (3 hr 45) <<< (4 hr 20) so <<< should be higher than hr. Could put at the same level as the arithmetical comparison operators (, etc.), namely 700. Again, no issue regarding associativity. So definition is: ?- op(700, xfx, <<<). This merely ensures the Prolog system correctly groups expressions containing <<<. It gives no meaning to the operator!

50 Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Giving meaning to an operator predicate

51 Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD Summary Operators are a notational convenience in some situations. An operator can be infix, prefix or postfix. An operators are ordered according to precedence. An operator has an associativity. The programmer can define new operators using op/3.


Download ppt "Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC."

Similar presentations


Ads by Google