30
26 October 2006 Foundations of Logic and Constrain t 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.

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

  • View
    215

  • Download
    2

Embed Size (px)

Citation preview

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.

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

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

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­(?)”.

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

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

26 October 2006 Foundations of Logic and Constraint Programming 7

Pseudo Derivations

1. A­maximal­sequence­of­SLDNF-derivation­steps­

Q0 1c1 Q1 ...

Qn-1 ncn Qn ...

is­a­pseudo-derivation­of­P {Q}

• ­Q0, ... , Qn, ...­are­queries,­each­empty­or­with­one­literal­selected­in­it;

• 1nare­substitutions;

• c1, ... , cn, ...­ 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.

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”

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­ ({TQ}, TQ, subs)­ is­ in­ C,­ where­ TQ­contains­the­single­node­Q­and­subs(Q)­are­undefined.

• for­every­F C :

- The­extension­of F is­in C.

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.

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

26 October 2006 Foundations of Logic and Constraint Programming 12

SLDNF-Trees

An SLDNF-tree

:

limit­of­a­sequence F0, F1, F2, ...,­where

• ­F0­is­the­initial­pre-SLDNF­tree­

• Fi+1 is­an­extension­of­Fi,­for­every­i­ N

An SLDNF-tree for P {Q}

:

SLDNF-tree in­which­Q­is­the­root­of­the­main­tree

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­N0, N1, N2, ... such­that­for­every i­( N), Ni+1­

is­

• either­a­direct­descendant­of­Ni ­;­

• or­the­root­of­subs(Ni)

26 October 2006 Foundations of Logic and Constraint Programming 14

Example (1)

Program P:

q ← q

q

q

q

q

q

• The­SLDNF-tree­for P { q} is­infinite

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

rfailure

r

□  success

□  success

r

□  success

r

□  success

1

2

3

4

4a

4b

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­Q0 (wrt )­­:

(12n) | var(Q0)

where­the­main­tree­of­an­SLDNF-tree­for­P {Q0}­­contains­the­branch

­­=­Q0 1 Q1 ... Qn-1 nQn = □

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­F0, F1, F2, ...,­then:

i. ­F­is­successful­and­yields­CAS­­

­­­­­­­iff­some­Fi is­successful­and yields­CAS­­

ii. ­F­is­finitely­failed

­­­­­­­iff­some­Fi is­finitely­failed.

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.

x1, xkA­­x1, xkA

occurs­ when­ there­ are­ no­ variables­ in­ atom­A,­ thus­ justifying­ the­ need­ for­ground­literals.

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}

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

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.

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.

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­

• ­P {Q}­does­not­flounder;

• If­­is­a­CAS­of­Q,­then­Q­is­ground.

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.

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 October 2006 Foundations of Logic and Constraint Programming 26

Extended Prolog Trees

Let­P be­an­extended­program­and­Q0­an­extended­query.­Then

The Extended Prolog Tree­ for­ P {Q0}­ is­ the­ forest­ of­ finitely­ branching,­ordered­ trees­of­queries­possibly­marked­with­ “success”­or­ “failure”,­produced­as­follows:

• Start­with­forest­({TQ0 }, TQ0

, subs),­

where­TQ0­contains­the­single­node­Q0­and­subs(Q0)­is­undefined

• Repeatedly­apply­operation­expand (F,Q).­to­the­current­forest­

F = (T, T, subs)­

where

• T1 T­­is­leftmost,­bottommost­(i.e.­most­nested­subsidiary)­tree­with­an­unmarked­leaf.

• Q­is­the­leftmost­unmarked­leaf­in­T1,­

26 October 2006 Foundations of Logic and Constraint Programming 27

Operation Expand (1)

Let­F = (T,T,subs)­be­the­current­forest,­where­T1 T­­is­leftmost,­bottommost­­tree­with­an­unmarked­leaf,­and­Q­is­the­leftmost­unmarked­leaf­in­T1.­

Operation­expand (F,Q).­is­defined­as­follows:

• If­Q = □­then­

1. Mark­Q­with­“sucess”

2. If­T1 T,­then­remove­from­T1­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

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­ T1­ the­ LNDF-resolvent­ of­ Q­ as­ the­ only­descendant­of­Q

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)

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