Presentation is loading. Please wait.

Presentation is loading. Please wait.

26 October 2006 Foundations of Logic and Constraint Programming 1 Negation: Procedural Interpretation ­An overview Why negation Extended programs and queries.

Similar presentations


Presentation on theme: "26 October 2006 Foundations of Logic and Constraint Programming 1 Negation: Procedural Interpretation ­An overview Why negation Extended programs and queries."— Presentation transcript:

1 26 October 2006 Foundations of Logic and Constraint Programming 1 Negation: Procedural Interpretation ­An overview Why negation Extended programs and queries The computation mechanism: SLDNF-derivations Allowed programs and queries [ApBo94] Krzysztof Apt and Roland Bol, Logic Programming and Negation: A Survey, Journal of Logic Programming, 19/20: 9-71, 1994.

2 26 October 2006 Foundations of Logic and Constraint Programming 2 Example (1) ­In many cases, it is important to find solutions that do not satisfy some property. The introduction of negation in Logic Programming aims at enabling a declarative style for specifying such situations. ­Example: Given program P below for which airports do I have a direct flight from Lisbon, but not from Porto? ­In this case, a declarative specification of the intended query is direct(lisbon,X),  direct(porto,X). where  stands for negation. direct(lisbon, london). direct(lisbon,paris). direct(lisbon, madrid).kfurt) direct(lisbon,frankfurt). direct(porto,madrid). direct(porto,paris). direct(porto,london).

3 26 October 2006 Foundations of Logic and Constraint Programming 3 Example (2) ­When sets are represented by lists, some operations on sets are also conveniently expressed by means of negation. ­Example1 : Set Difference % diff(S,T,R) : R = S \ T diff([],_,[]) ← diff([H|T], L, R) ← member(H,L), diff(T,L,R]. diff([H|T], L, [H|R])←  member(H,L), diff(T,L,R]. ­Example2 : Set Disjointness (S  T =  ) % disjoint(S,T) : S  T =  disjoint([], _) ← disjoint([H|T], L) ←  member(H,L), disjoint(T,L).

4 26 October 2006 Foundations of Logic and Constraint Programming 4 Extended Logic Programs and Queries First, some definitions regarding negation “  ” denotes the negation sign A,  A are literals :  A is an atom A,  A are ground literals :  A is a ground atom Q is an extended query :  Q is a finite sequence of literals H ← B is an extended clause :  H is an atom, B is an extended query. P is an extended programs :  P is a finite set of extended clauses. Now, to “compute” negation, a particular semantics for negation has been widely adopted in logic programming, namely negation as failure. Intuitively, “if you cannot prove that P is true, then P must be false (?)”.

5 26 October 2006 Foundations of Logic and Constraint Programming 5 Negation as Failure To compute negation, it has been adopted a particular semantics for negation, namely negation as failure. Intuitively, if you cannot prove that P is true, then P must be false (?). Negation as Failure (NF) :  1.Assume  A is selected in the query Q = L,  A, N. 2.If P  {A} succeeds then the derivation of P  {Q} fails at this point. 3.If all derivations of P  {A} fail then Q resolves to Q’ = L, N. Hence  A succeeds iff A finitely fails  A finitely fails iff A succeeds

6 26 October 2006 Foundations of Logic and Constraint Programming 6 SLDNF resolvents 1.Let Q = L, A, N be a query, A the selected, positive, literal. Let H ← M be a variant of a clause c, which is variable disjoint with Q,  is a mgu of A and H; then Q’ = ( L, M, N )  is the SLDNF-resolvent of Q (wrt A with  ); the SLDNF-derivation step is denoted by Q   c Q’ 2.Let Q = L,  A, N be a query, A the selected, negative and ground, literal. Then Q’ = ( L, N ) is the SLDNF-resolvent of Q (wrt A with  ); the SLDNF-derivation step is denoted by Q   Q’ Note: No substitution is performed due to the negative literal. This literal, that should be ground, is only checked for not true.

7 26 October 2006 Foundations of Logic and Constraint Programming 7 Pseudo Derivations 1.A maximal sequence of SLDNF-derivation steps Q 0   1 c 1 Q 1... Q n-1   n c n Q n... is a pseudo-derivation of P  {Q} :  Q 0,..., Q n,... are queries, each empty or with one literal selected in it;  1  n  are substitutions; c 1,..., c n,... are clauses of program P (in case a positive literal is selected in the preceeding query); For every SLDNF-derivation step with input clause, the standardisation apart (“fresh variables”) condition holds.

8 26 October 2006 Foundations of Logic and Constraint Programming 8 Forests Since for each negated ground literal a “distinct” derivation is started, the whole procedure is better modeled as a set of trees (forest). F = ( T, T, subs) is a forest :  T is a set of trees where -Nodes are queries -A literal is selected in each non empty query -Leaves are marked as “success”, “failure” or “floundered” T  T is the main tree subs assigns to some nodes of trees in T with selected negative ground literal  A a subsidiary tree of T, with root A. Tree T  T is successful :  it contains a leaf marked as “success” Tree T  T is finitely failed :  it is finite and all leaves are marked as “failure”

9 26 October 2006 Foundations of Logic and Constraint Programming 9 Pre-SLDNF Trees The class of pre-SLDNF trees for a program P is the smallest class C of forests such that for every query Q: -The initial pre-SLDNF tree ({T Q }, T Q, subs) is in C, where T Q contains the single node Q and subs(Q) are undefined. for every F  C : -The extension of F is in C.

10 26 October 2006 Foundations of Logic and Constraint Programming 10 Extension of Pre-SLDNF Trees (1) Extension of forest F = ( T, T, subs) :  a.Every occurrence of the empty query is marked as “success” b.For every non-empty query Q, which is an unmarked leaf in some tree in T, perform the following action: Let L be the selected literal of Q 1.L is positive If Q has no SLDNF-resolvents then Q is marked as “failure” else for every program clause c which is applicable to L, exactly one direct descendant of Q is added. This resolvent is the SLDNF- resolvent of Q with c wrt. L.

11 26 October 2006 Foundations of Logic and Constraint Programming 11 Extension of Pre-SLDNF Trees (2) Extension of forest F = ( T, T, subs) :  (cont.) 2.L =  A is negative If A non-ground then Q is marked as “floundered” else (A is ground) subs(Q) undefined new tree T’ with single root A is added to T and subs(Q) is set to T’ subs(Q) defined and successful Q is marked as failure subs(Q) defined and finitely failed SLDNF-resolvent of Q is added as the only direct descendent of Q subs(Q) defined and neither successful nor finitely failed no action

12 26 October 2006 Foundations of Logic and Constraint Programming 12 SLDNF-Trees An SLDNF-tree :  limit of a sequence F 0, F 1, F 2,..., where F 0 is the initial pre-SLDNF tree F i+1 is an extension of F i, for every i  N An SLDNF-tree for P  {Q} :  SLDNF-tree in which Q is the root of the main tree

13 26 October 2006 Foundations of Logic and Constraint Programming 13 Successful, Failed and Finite SLDNF-Trees A (pre-)SLDNF-tree is successful :  its main tree is successful A (pre-)SLDNF-tree is finitely failed :  its main tree is finitely failed A SLDNF-tree is finite :  no infinite path exists in it, where a path is a sequence of nodes N 0, N 1, N 2,... such that for every i (  N), N i+1 is either a direct descendant of N i ; or the root of subs(N i )

14 26 October 2006 Foundations of Logic and Constraint Programming 14 Example (1) Program P: q ← q  qqqqq qqqqq The SLDNF-tree for P  {  q} is infinite

15 26 October 2006 Foundations of Logic and Constraint Programming 15 Example (2) The SLDNF-tree for P  {  q} is successful Program P: q ←  r r ← r ← r  q q q  r failure r □ success □ success r □ success r □ success 1 2 3 4 4a 4b

16 26 October 2006 Foundations of Logic and Constraint Programming 16 SLDNF- derivation A SLDNF-derivation of P  {Q} :  branch in the main tree of a SLDNF-tree F for P  {Q} together with the set of all trees of F whose roots can be reached from the nodes in this branch. A SLDNF-derivation is successful :  it ends with success ( □ ). A computed answer substitution (CAS) of Q 0 (wrt  ) :  (  1   2   n ) | var(Q 0 ) where the main tree of an SLDNF-tree for P  {Q 0 } contains the branch  = Q 0   1 Q 1... Q n-1   n  Q n = □

17 26 October 2006 Foundations of Logic and Constraint Programming 17 A Theorem on Limits The following theorem justifies that the determination of whether a SLDNF- tree is successful or finitely failed is a search problem in a finite search space. Theorem 1.Every SLDNF-tree is the limit of a unique sequence of pre-SLDNF-trees. 2.If the SLDNF-tree F is the the limit of the sequence F 0, F 1, F 2,..., then: i. F is successful and yields CAS  iff some F i is successful and yields CAS  ii. F is finitely failed iff some F i is finitely failed.

18 26 October 2006 Foundations of Logic and Constraint Programming 18 Why Select Ground Literals Only Ideally, when a  A is selected in a query, we would like SLDNF-resolution to answer the question: “Is there a substitution  that makes atom A false?” Unfortunately, SLDNF-resolution answers adiffrent question: “Are there no substitutions that make atom A true?” Indeed SLDNF does not check the existencial closure of  A, i.e. P |=  A, but rather the negation (failure) of the existencial closure of A, i.e. P |=   A, which is in fact equivalent to P |=  A. It is easy to see that the only situation where (  A)  (  A), i.e.  x 1,  x k  A   x 1,  x k  A occurs when there are no variables in atom A, thus justifying the need for ground literals.

19 26 October 2006 Foundations of Logic and Constraint Programming 19 Why Select Ground Literals Only (Example 1) The difference between (  A)  (  A) can be observed in program zero(0) ← positive(x) ←  zero(x) for query Q = positive(y). Indeed, rather than finding whether there are positive numbers, i.e. “are there non-zero numbers”, SLDNF answers the quite different question of “are all numbers non-zero”, which is of course false (0 is zero!). positive(y)  zero(y) failure zero(y) □ success {x/y} {y/0}

20 26 October 2006 Foundations of Logic and Constraint Programming 20 Why Select Ground Literals Only (Example 2) The “unintended” behaviour od SLDNF does not occur when the query is ground; neither with Q0 = positive(0), nor Q1 = positive(s(0)). zero(0) ← positive(x) ←  zero(x) In both cases, the “correct” answers are given: 0 is not positive s(0) is positive positive(0)  zero(0) failure zero(0) □ success {x/0} {} positive(s(0))  zero(s(0)) sucess zero(s(0))  failure {x/s(0)}

21 26 October 2006 Foundations of Logic and Constraint Programming 21 Extended Selection Rule (Extended) selection rule :  Function which, given a pre-SLDNF-tree F = ( T, T, subs) selects a literal in every non-empty unmarked leaf in every tree in T. SLDNF-tree F is via a selection rule R :  F is the limit of a sequence of pre-SLDNF-trees, in which literals are selected according to R. Selection rule R is safe :  R never selects a non-ground negative literal.

22 26 October 2006 Foundations of Logic and Constraint Programming 22 Blocked Queries Query Q is blocked :  Q is non-empty and contains exclusively non-ground negative literals P  {Q} flounders :  some SLDNF-tree for P  {Q} contains a blocked node. Notice that floundering causes a the program to behave in the “unintended way”, discussed before.

23 26 October 2006 Foundations of Logic and Constraint Programming 23 Allowed Programs and Queries do not Flounder Query Q is allowed :  every x  var(Q) occurs in a positive literal of Q Clause H ← B is allowed :   H, B is allowed (Unit) clause H ← is allowed :  H is a ground atom Program P is allowed :  all clauses of P are allowed Theorem 3.13 (Apt and Bol, 1994) Assume that P and Q are allowed. Then i. P  {Q} does not flounder; ii.If  is a CAS of Q, then Q  is ground.

24 26 October 2006 Foundations of Logic and Constraint Programming 24 SLDNF implementation in Prolog Operationally, Prolog systems use the cut (“!”) mechanism to implement negation as failure literally, by means of the built-in meta-predicate “\+”, defined as follows % \+ is the negation operator % (1)  A finitely fails iff A succeeds \+A :- call(A), !, fail. % (2)  A succeeds iff A finitely fails \+A. The negation of an atom (or goal) is treated separately, starting a new tree. a)If the goal succeeds with some substitution the meta-predicate is forceed to fail, with no alternative (due to the !). b)If the goal fails the cut is not invoked and the catch all clause makes the meta-predicate \+ to succeed, with no substitution.

25 26 October 2006 Foundations of Logic and Constraint Programming 25 Specifics of Prolog In Prolog, SLDNF-resolution (Selection rule driven Linear Resolution for Definite clauses augmented by Negation as Failure) is instantiated by adopting Leftmost selection rule% leftmost literals from resolvents A program is a sequence of clauses% top clauses first Non-ground negative literals selected!% floundering! Unification without occurs check% incorrect unification Depth-first search, backtracking% trapped in infinite branches The notions of SLDNF-resolution, -resolvents, -trees, etc. are “replaced” by LNDF- correspondents.

26 26 October 2006 Foundations of Logic and Constraint Programming 26 Extended Prolog Trees Let P be an extended program and Q 0 an extended query. Then The Extended Prolog Tree for P  {Q 0 } is the forest of finitely branching, ordered trees of queries possibly marked with “success” or “failure”, produced as follows: Start with forest ({T Q 0 }, T Q 0, subs), where T Q 0 contains the single node Q 0 and subs(Q 0 ) is undefined Repeatedly apply operation expand ( F,Q). to the current forest F = ( T, T, subs) where T 1  T is leftmost, bottommost (i.e. most nested subsidiary) tree with an unmarked leaf. Q is the leftmost unmarked leaf in T 1,

27 26 October 2006 Foundations of Logic and Constraint Programming 27 Operation Expand (1) Let F = ( T,T,subs) be the current forest, where T 1  T is leftmost, bottommost tree with an unmarked leaf, and Q is the leftmost unmarked leaf in T 1. Operation expand ( F,Q). is defined as follows: If Q = □ then 1.Mark Q with “sucess” 2.If T 1  T, then remove from T 1 all edges to the right of the branch that ends with Q (i.e. do not repeat success in a subsidiary tree). If Q has no LDNF-resolvents, then mark Q with “failure” else let L be the leftmost literal in Q

28 26 October 2006 Foundations of Logic and Constraint Programming 28 Operation Expand (2) –L is positive Add for each clause that is applicable to L, an LDNF-resolvent as a descendant of Q (such that the order of the clauses is preserved) –L =  A is negative (not necessarily ground!) If subs(Q) is undefined then add a new tree T’ = A and set subs(Q) to T’ If subs(Q) is defined and successful then mark Q with “failure” If subs(Q) is defined and finitely failed then add in T 1 the LNDF-resolvent of Q as the only descendant of Q

29 26 October 2006 Foundations of Logic and Constraint Programming 29 Prolog Negation does not Check Floundering From the specification, Prolog does not check whether a negated goal is ground or not. Hence, computation is never blocked, but the price to pay is floundering. This can be shown in the following interaction. | ?- positive(0). no | ?- positive(s(s(0))). yes | ?- positive(X). no % ???? The reason for floundering is, of course, the call of the nonground negative goal \+zero(X). zero(0). positive(X):- \+ zero(X)

30 26 October 2006 Foundations of Logic and Constraint Programming 30 Prolog Users Must Avoid Floundering To avoid floundering, an user must take care to write programs which are allowed. This can in general be achieved by specifying what the variables “might be”, before specifying what they “are not”. Example: Instead of program use program | ?- positive(X).| ?- positive(X). no% ???? X = s(0) ? ; X = s(s(0)) ? ; X = s(s(s(0))) ? ;... zero(0). positive(X):- \+ zero(X) zero(0). positive(X):- num(X), \+ zero(X) num(0). num(s(X)):- num(X).


Download ppt "26 October 2006 Foundations of Logic and Constraint Programming 1 Negation: Procedural Interpretation ­An overview Why negation Extended programs and queries."

Similar presentations


Ads by Google