Helsinki

University

of

Technology

Department

of

Computer

Science

Digital

Systems

Laboratory

Otaniemi

Otakaari

FIN

-LCB-

ESPOO

FINLAND

HELSINKI

UNIVERSITY

OF

TECHNOLOGY

DIGITAL

SYSTEMS

LABORATORY

Series

A

Research

Reports

ISSN

-LCB-

No.

September

ISBN

-LCB-

-LCB-

-LCB-

EFFICIENT

IMPLEMENTATION

OF

THE

STABLE

MODEL

SEMANTICS

FOR

NORMAL

LOGIC

PROGRAMS

Patrik

Simons

Digital

Systems

Laboratory

Department

of

Computer

Science

Helsinki

University

of

Technology

Otaniemi

FINLAND

HELSINKI

UNIVERSITY

OF

TECHNOLOGY

DIGITAL

SYSTEMS

LABORATORY

Series

A

Research

Reports

ISSN

-LCB-

No.

September

ISBN

-LCB-

-LCB-

-LCB-

Efficient

Implementation

of

the

Stable

Model

Semantics

for

Normal

Logic

Programs

Patrik

Simons

Abstract

The

aim

of

this

work

is

to

develop

an

efficient

implementation

method

of

the

stable

model

semantics

of

logic

programs

with

negation

using

as

a

basis

a

lately

proposed

decision

procedure

for

Reiter

's

default

logic

The

problem

of

finding

a

stable

model

of

a

logic

program

is

NP-complete

An

algorithm

for

computing

the

stable

model

semantics

of

propositional

logic

programs

is

presented

The

time

complexity

of

the

algorithm

is

examined

and

found

to

be

exponential

in

the

size

of

the

input

in

the

worst

case

The

algorithm

is

shown

to

compute

the

unique

stable

model

of

a

logic

program

with

a

total

well-founded

model

in

time

roughly

quadratic

in

the

size

of

the

program

The

algorithm

is

compared

with

three

other

advanced

algorithms

for

computing

the

stable

models

semantics

It

is

found

to

be

outperforming

the

other

algorithms

mainly

due

to

a

better

technique

for

pruning

the

search

space

A

method

for

implementing

the

algorithm

efficiently

is

presented

and

a

prototype

implementation

is

developed

As

examples

of

applications

of

stable

model

semantics

model-based

diagnosis

of

logic

circuits

and

the

problem

of

finding

Hamiltonian

circuits

and

N-colorings

in

undirected

graphs

are

encoded

as

propositional

logic

programs

These

encodings

are

then

used

as

test

cases

in

a

comparison

between

the

prototype

implementation

of

the

algorithm

presented

in

this

work

and

the

SLG

system

a

stateof-the-art

implementation

of

the

stable

model

semantics

The

performance

of

the

prototype

implementation

compares

favorably

to

that

of

the

SLG

system

Printing

TKK

Monistamo

Otaniemi

Helsinki

University

of

Technology

Phone

-LCB-

Department

of

Computer

Science

Digital

Systems

Laboratory

Telex

htkk

Otaniemi

Otakaari

Telefax

-LCB-

-LCB-

FIN

-LCB-

ESPOO

FINLAND

E-mail

lab@saturn.hut.fi

-LCB-

i

-LCB-

Contents

Introduction

Outline

of

the

Work

Fundamental

Concepts

Stable

Model

Semantics

Well-Founded

Semantics

An

Algorithm

for

Computing

Stable

Models

The

Algorithm

Expanding

B

Detecting

Conflicts

The

F

Subset

of

Bn

Test

Improving

the

Algorithm

Computing

Deductive

Closures

Deductive

Closures

Diminishing

Deductive

Closures

Logic

Programs

with

Total

Well-Founded

Models

Complexity

Analysis

Backtracking

Based

on

the

Dependency

Graph

The

Dependency

Graph

Backtracking

and

the

Dependency

Graph

Handling

Rules

with

Variables

Ground

Logic

Programs

Range

Restricted

Logic

Programs

-LCB-

ii

-LCB-

Implementation

Data

Structures

Deductive

Closures

and

Conflicts

Backtracking

A

Heuristic

Comparison

with

other

Algorithms

Branch

and

Bound

The

SLG

System

The

Mixed

Integer

Programming

Approach

Comparison

Applications

Diagnosis

Graph

Problems

Remarks

Experimental

Results

Conclusions

Acknowledgments

References

-LCB-

iii

-LCB-

List

of

Figures

The

Decision

Procedure

for

Logic

Programs

with

the

Stable

Model

Semantics

Implementation

of

the

Function

expand

Implementation

of

the

Function

conflict

Improved

Decision

Procedure

for

Logic

Programs

with

the

Sta

ble

Model

Semantics

Improved

Version

of

the

Function

expand

Improved

Version

of

the

Function

conflict

The

Procedure

for

Computing

Deductive

Closures

The

Procedure

for

Computing

Diminishing

Deductive

Closures

The

Procedure

for

Grounding

Range

Restricted

Programs

Not-gate

Xor-gate

And-gate

Or-gate

Ladder

Graph

Ripple

Carry

Adder

Parity

Circuit

Test

Results

Test

Results

Test

Results

Introduction

In

recent

years

declarative

semantics

for

logic

programs

with

negation

have

been

intensively

investigated

However

there

has

been

quite

limited

amount

of

research

on

developing

implementation

techniques

for

these

semantics

In

this

work

we

consider

the

stable

model

semantics

one

of

the

leading

declarative

semantics

for

logic

programs

with

negation

Our

aim

is

to

develop

an

efficient

implementation

technique

for

this

semantics

using

as

a

basis

a

recently

proposed

decision

procedure

for

Reiter

's

default

logic

-LSB-

-RSB-

An

early

proposition

for

the

interpretation

of

negation

in

logic

programs

was

the

negation-as-failure

rule

of

prolog

This

procedural

approach

of

the

goal

directed

prolog

interpreter

assumes

that

if

a

goal

fails

then

that

goal

is

false

Thus

when

a

negative

goal

is

encountered

by

the

interpreter

it

tries

to

prove

the

goal

and

if

that

fails

the

negative

goal

succeeds

The

negative

side

of

this

proof

procedure

is

that

it

never

terminates

for

some

logic

programs

A

declarative

interpretation

of

negation

in

logic

programs

soon

followed

the

program

completion

of

Clark

-LSB-

-RSB-

later

extended

by

Lloyd

-LSB-

-RSB-

The

program

completion

completes

the

logic

program

in

the

sense

that

the

rules

in

the

program

are

assumed

to

be

logical

equivalences

instead

of

implications

or

inference

rules

Another

proposition

for

the

interpretation

of

negation

in

logic

programs

was

the

closed

world

assumption

of

Reiter

-LSB-

-RSB-

The

closed

world

assumption

is

the

assumption

that

all

facts

that

are

not

logical

consequences

of

the

program

are

false

Hence

the

logic

program

defines

the

world

and

everything

that

is

not

mentioned

in

the

program

is

false

A

later

approach

to

the

problem

of

defining

a

semantics

for

logic

programs

with

negation

consists

of

declaring

a

canonical

model

of

the

program

A

canonical

model

is

one

model

of

possibly

several

that

is

preferred

over

the

others

Typically

these

semantics

define

a

unique

canonical

model

only

for

a

small

class

of

logic

programs

The

iterated

fixpoint

semantics

defines

a

unique

canonical

model

for

the

class

of

stratified

logic

programs

The

stratified

logic

programs

have

been

examined

by

-LSB-

-RSB-

and

others

The

well-founded

semantics

-LSB-

-RSB-

and

the

stable

model

semantics

of

Gelfond

and

Lifschitz

-LSB-

-RSB-

are

two

generalizations

of

the

iterated

fixpoint

semantics

that

define

unique

canonical

models

for

classes

of

programs

that

include

the

class

of

stratified

programs

A

final

generalization

of

the

well-founded

semantics

is

the

tie-breaking

semantics

of

Papadimitriou

and

Yannakakis

-LSB-

-RSB-

The

intuition

behind

the

stable

model

semantics

is

derived

from

autoepistemic

logic

-LSB-

-RSB-

A

stable

model

is

a

set

of

beliefs

of

a

rational

agent

whose

inference

rules

are

those

of

the

logic

program

and

who

is

able

to

perform

negative

introspection

if

the

agent

does

not

believe

a

fact

it

concludes

the

negation

of

the

fact

The

stable

model

semantics

is

thus

nonmonotonic

as

adding

rules

to

the

program

may

cause

the

agent

to

retract

some

of

its

beliefs

In

addition

this

intuition

makes

it

possible

for

a

logic

program

to

lack

a

stable

model

have

one

or

several

We

give

some

examples

Consider

the

program

f

lies

-LRB-

X

-RRB-

bird

-LRB-

X

-RRB-

not

-LRB-

abnormal

-LRB-

X

-RRB-

-RRB-

bird

-LRB-

T

weety

-RRB-

which

is

intended

to

express

that

birds

that

are

not

abnormal

can

fly

and

that

Tweety

is

a

bird

Now

a

rational

agent

believes

that

Tweety

is

not

abnormal

as

nothing

in

the

program

implies

otherwise

Hence

the

agent

concludes

that

Tweety

can

fly

However

if

we

add

the

rule

abnormal

-LRB-

Tweety

-RRB-

then

the

agent

can

no

longer

infer

that

Tweety

can

fly

The

only

stable

model

of

the

original

program

is

thus

ff

lies

-LRB-

T

weety

-RRB-

bird

-LRB-

T

weety

-RRB-

g

and

the

only

stable

model

of

the

expanded

program

is

fbird

-LRB-

T

weety

-RRB-

g.

Consider

the

program

green

-LRB-

apple

-RRB-

not

-LRB-

red

-LRB-

apple

-RRB-

-RRB-

red

-LRB-

apple

-RRB-

not

-LRB-

green

-LRB-

apple

-RRB-

-RRB-

which

says

that

the

apple

is

green

if

it

is

not

red

and

red

if

it

is

not

green

A

rational

agent

may

in

this

case

hold

the

belief

that

the

apple

is

green

as

it

then

can

not

infer

that

the

apple

is

red

Conversely

the

agent

may

believe

that

the

apple

is

red

as

it

then

can

not

infer

that

the

apple

is

green

Hence

the

program

has

two

stable

models

fgreen

-LRB-

apple

-RRB-

g

and

fred

-LRB-

apple

-RRB-

g.

Finally

consider

the

program

cold

not

-LRB-

cold

-RRB-

A

rational

agent

can

not

hold

any

beliefs

whether

it

is

cold

or

not

using

this

program

If

the

agent

assumes

that

it

is

not

cold

then

it

must

believe

that

it

is

cold

which

is

a

contradiction

Hence

as

the

agent

is

rational

it

can

not

believe

that

it

is

not

cold

But

then

it

can

not

infer

that

it

is

cold

either

This

program

has

therefore

no

stable

models

The

stable

model

semantics

is

a

form

of

nonmonotonic

reasoning

that

is

closely

related

to

the

default

logic

of

Reiter

-LSB-

-RSB-

circumscription

of

McCarthy

-LSB-

-RSB-

and

the

autoepistemic

logic

of

Moore

-LSB-

-RSB-

As

the

stable

model

semantics

has

become

a

standard

method

for

supplying

semantics

to

nonmonotonic

logic

programs

there

is

considerable

interest

in

automating

the

computation

of

stable

models

The

earliest

methods

for

finding

stable

models

were

based

on

the

truth

maintenance

system

of

Doyle

-LSB-

-RSB-

and

the

assumption-based

TMS

of

deKleer

-LSB-

-RSB-

see

-LSB-

-RSB-

Recently

more

specialized

algorithms

have

been

developed

-LSB-

-RSB-

The

problem

of

computing

stable

models

is

not

trivial

In

fact

the

problem

of

finding

a

stable

model

of

a

logic

program

is

NP-complete

-LSB-

-RSB-

In

this

work

we

develop

a

novel

algorithm

for

computing

the

stable

models

of

finite

propositional

logic

programs

As

a

basis

for

the

algorithm

we

use

a

recently

proposed

decision

method

for

default

logic

-LSB-

-RSB-

We

devise

a

number

of

optimizations

resulting

in

a

simpler

and

more

efficient

implementation

method

for

the

stable

model

semantics

A

key

task

in

the

algorithm

is

the

computation

of

deductive

closures

for

monotonically

increasing

and

decreasing

sets

of

premises

We

develop

an

efficient

incremental

method

for

computing

the

closures

We

find

an

upper

bound

on

the

time

complexity

of

the

algorithm

and

discuss

how

it

computes

well-founded

models

We

examine

a

possibility

of

improving

the

backtracking

of

the

algorithm

and

present

a

method

of

handling

variables

by

restricting

their

use

We

also

present

some

applications

of

logic

programs

with

stable

model

semantics

diagnosis

of

logic

circuits

and

two

combinatorial

graph

problems

We

develop

a

prototype

implementation

of

the

algorithm

and

compare

the

implementation

with

the

SLG

system

a

state-ofthe-art

implementation

of

the

stable

model

semantics

using

the

applications

as

test

cases

Outline

of

the

Work

In

Section

we

recall

definitions

and

theorems

concerning

the

stable

model

semantics

and

the

well-founded

semantics

In

Section

we

present

two

algorithms

for

computing

stable

models

of

finite

propositional

logic

programs

together

with

proofs

of

their

correctness

In

Section

we

continue

by

examining

how

one

can

compute

the

deductive

closures

needed

by

the

algorithms

efficiently

We

note

how

the

algorithm

computes

well-founded

models

in

Section

An

upper

bound

on

the

time

complexity

of

the

novel

algorithm

is

given

in

Section

In

Section

we

examine

a

strategy

for

backtracking

Logic

programs

with

variables

are

examined

in

Section

In

Section

we

describe

how

to

implement

the

algorithm

in

an

efficient

way

We

compare

the

algorithms

presented

in

this

work

with

some

other

algorithms

computing

the

stable

model

semantics

in

Section

In

Section

we

present

some

applications

of

logic

programs

with

stable

model

semantics

We

compare

the

execution

times

of

the

algorithm

and

the

SLG

system

-LSB-

-RSB-

a

state

of

the

art

implemen

tation

of

the

stable

model

semantics

in

Section

Finally

in

Section

we

present

our

conclusions

Fundamental

Concepts

In

this

section

we

present

some

concepts

and

definitions

that

are

needed

in

the

following

sections

In

Section

we

present

the

stable

model

semantics

of

-LSB-

-RSB-

and

a

characterization

of

the

semantics

defined

in

-LSB-

-RSB-

In

Section

we

present

the

well-founded

semantics

of

-LSB-

-RSB-

Stable

Model

Semantics

We

define

stable

models

following

the

presentation

in

-LSB-

-RSB-

Consider

a

logic

program

that

consists

of

rules

of

the

form

h

an

not

-LRB-

-RRB-

not

-LRB-

bm

-RRB-

-LRB-

-RRB-

where

an

bm

and

h

are

propositional

atoms

We

call

the

expression

not

-LRB-

b

-RRB-

a

not-atom

For

a

set

of

atoms

A

not

-LRB-

A

-RRB-

fnot

-LRB-

a

-RRB-

j

a

Ag

-LRB-

-RRB-

For

a

set

of

atoms

and

not-atoms

we

denote

the

the

set

of

atoms

in

by

and

the

set

of

not-atoms

by

Atoms

and

not-atoms

will

also

be

called

literals

The

stable

model

semantics

for

a

general

logic

program

P

is

defined

as

follows

-LSB-

-RSB-

The

reduct

P

S

of

P

with

respect

to

the

set

of

atoms

S

is

obtained

by

deleting

each

rule

in

P

that

has

a

not-atom

not

-LRB-

b

-RRB-

in

its

body

such

that

b

S

and

by

deleting

all

not-atoms

in

the

remaining

rules

A

set

of

atoms

S

is

a

stable

model

of

P

if

and

only

if

S

is

the

deductive

closure

of

P

S

when

the

rules

in

P

S

are

seen

as

inference

rules

Example

Let

P

be

the

set

of

rules

fa

not

-LRB-

b

-RRB-

b

bg

The

only

stable

model

of

P

is

fag

which

is

seen

as

follows

If

the

atom

b

is

not

in

the

set

of

atoms

S

then

the

reduct

of

P

with

respect

to

S

is

P

S

fa

b

bg

The

closure

of

this

reduct

is

fag

If

on

the

other

hand

b

belongs

to

the

set

S

then

the

reduct

P

S

fb

bg

But

as

the

closure

of

fb

bg

is

empty

the

set

S

is

not

a

stable

model

of

P

The

deductive

closure

of

a

set

of

rules

P

and

a

set

of

atoms

and

not-atoms

L

is

denoted

by

Dcl

-LRB-

P

L

-RRB-

Dcl

-LRB-

P

L

-RRB-

is

the

smallest

set

of

atoms

that

contains

L

and

is

closed

under

R

-LRB-

P

L

-RRB-

where

R

-LRB-

P

L

-RRB-

fh

an

j

h

an

not

-LRB-

-RRB-

not

-LRB-

bm

-RRB-

P

and

not

-LRB-

bi

-RRB-

L

for

i

mg

-LRB-

-RRB-

is

seen

as

a

set

of

inference

rules

Notice

that

if

L

then

Dcl

-LRB-

P

L

-RRB-

Dcl

-LRB-

P

-RRB-

i.e.

Dcl

-LRB-

P

-RRB-

is

monotonic

Example

Let

P

be

the

set

of

rules

fa

not

-LRB-

b

-RRB-

b

not

-LRB-

c

-RRB-

g

and

let

L

be

the

set

of

literals

fnot

-LRB-

b

-RRB-

cg

Then

R

-LRB-

P

L

-RRB-

fa

g

and

the

deductive

closure

Dcl

-LRB-

P

L

-RRB-

fa

cg

The

following

proposition

is

stated

in

-LSB-

-RSB-

Proposition

The

set

is

a

stable

model

of

a

set

of

rules

P

if

and

only

if

Dcl

-LRB-

P

not

-LRB-

-RRB-

-RRB-

Proof

Note

that

P

R

-LRB-

P

not

-LRB-

-RRB-

-RRB-

For

a

set

of

rules

P

we

denote

the

set

of

negative

antecedents

in

P

by

NAnt

-LRB-

P

-RRB-

i.e.

the

set

of

atoms

a

such

that

not

-LRB-

a

-RRB-

appears

in

P

For

example

NAnt

-LRB-

fa

not

-LRB-

b

-RRB-

d

cg

-RRB-

fbg

Evidently

Dcl

-LRB-

P

not

-LRB-

-RRB-

-RRB-

Dcl

-LRB-

P

not

-LRB-

NAnt

-LRB-

P

-RRB-

-RRB-

-RRB-

-LRB-

-RRB-

holds

We

call

a

set

not

-LRB-

NAnt

-LRB-

P

-RRB-

-RRB-

of

not-atoms

P

full

if

and

only

if

it

is

the

case

that

for

all

a

NAnt

-LRB-

P

-RRB-

not

-LRB-

a

-RRB-

if

and

only

if

a

Dcl

-LRB-

P

-RRB-

Interestingly

there

is

a

correspondence

between

the

P

full

sets

and

the

stable

models

of

P

Theorem

If

is

P

full

then

Dcl

-LRB-

P

-RRB-

is

a

stable

model

of

P

and

not

-LRB-

NAnt

-LRB-

P

-RRB-

-RRB-

If

is

a

stable

model

of

P

then

not

-LRB-

NAnt

-LRB-

P

-RRB-

-RRB-

is

a

P

full

set

and

Dcl

-LRB-

P

-RRB-

Proof

Let

be

a

P

full

set

Let

Dcl

-LRB-

P

-RRB-

Now

for

all

a

NAnt

-LRB-

P

-RRB-

a

if

and

only

if

not

-LRB-

a

-RRB-

which

implies

that

not

-LRB-

NAnt

-LRB-

P

-RRB-

-RRB-

Thus

Dcl

-LRB-

P

not

-LRB-

NAnt

-LRB-

P

-RRB-

-RRB-

-RRB-

and

is

a

stable

model

of

P

Let

be

a

stable

model

of

P

Let

not

-LRB-

NAnt

-LRB-

P

-RRB-

-RRB-

Then

Dcl

-LRB-

P

-RRB-

as

is

a

stable

model

of

P

Hence

for

all

a

NAnt

-LRB-

P

-RRB-

not

-LRB-

a

-RRB-

if

and

only

if

a

if

and

only

if

a

Dcl

-LRB-

P

-RRB-

which

is

the

definition

of

a

P

full

set

Example

Consider

the

set

of

rules

P

fa

not

-LRB-

b

-RRB-

b

not

-LRB-

a

-RRB-

g

The

set

of

rules

P

has

two

stable

models

fag

and

fbg

For

instance

the

set

fnot

-LRB-

b

-RRB-

g

is

P

full

as

b

Dcl

-LRB-

P

fnot

-LRB-

b

-RRB-

g

-RRB-

fag

Therefore

we

see

that

the

P

full

set

fnot

-LRB-

b

-RRB-

g

corresponds

to

the

stable

model

fag

The

empty

set

serves

as

a

counter

example

As

the

closure

Dcl

-LRB-

P

;-RRB-

and

as

the

atom

a

NAnt

-LRB-

P

-RRB-

but

a

the

empty

set

is

not

a

P

full

set

Let

be

a

set

of

atoms

and

not-atoms

let

P

be

a

set

of

rules

and

let

A

be

a

set

of

atoms

The

set

is

grounded

in

P

if

Dcl

-LRB-

P

;-RRB-

The

set

of

atoms

A

agrees

with

if

for

all

atoms

a

a

A

and

for

all

not

-LRB-

a

-RRB-

a

A.

Finally

is

said

to

cover

A

if

for

every

a

A

either

a

or

not

-LRB-

a

-RRB-

Example

We

examine

the

set

of

rules

P

fa

not

-LRB-

b

-RRB-

b

bg

The

set

fa

not

-LRB-

b

-RRB-

g

is

grounded

in

P

as

a

Dcl

-LRB-

P

-RRB-

Dcl

-LRB-

P

fnot

-LRB-

b

-RRB-

g

-RRB-

However

the

set

fbg

is

not

grounded

in

P

as

b

Dcl

-LRB-

P

;-RRB-

The

set

of

atoms

A

fag

agrees

with

as

b

A

and

as

a

A

As

a

is

not

in

the

empty

set

we

know

that

the

empty

set

does

not

agree

with

Finally

covers

the

set

fa

bg

but

not

the

set

fcg

Well-Founded

Semantics

We

review

the

well-founded

semantics

of

-LSB-

-RSB-

A

set

of

literals

I

is

an

interpretation

if

for

every

atom

a

a

I

implies

not

-LRB-

a

-RRB-

I.

Thus

the

set

fa

not

-LRB-

b

-RRB-

g

is

an

interpretation

but

the

set

fb

not

-LRB-

b

-RRB-

g

is

not

Define

Atoms

-LRB-

P

-RRB-

to

be

the

set

of

atoms

such

that

a

Atoms

-LRB-

P

-RRB-

if

and

only

if

there

exists

a

rule

r

P

such

that

a

is

the

head

of

r

or

a

or

not

-LRB-

a

-RRB-

is

in

the

body

of

r

In

other

words

the

set

Atoms

-LRB-

P

-RRB-

consists

of

the

atoms

that

appear

in

some

rule

of

P

For

example

Atoms

-LRB-

fa

not

-LRB-

b

-RRB-

c

dg

-RRB-

fa

b

c

dg

Definition

Let

P

be

a

logic

program

let

I

be

an

interpretation

and

let

U

be

a

set

of

atoms

The

set

U

is

an

unfounded

set

of

P

with

respect

to

I

if

every

atom

a

U

satisfies

the

following

condition

for

every

rule

r

P

whose

head

is

a

at

least

one

of

the

following

conditions

holds

there

is

a

literal

b

or

not

-LRB-

c

-RRB-

in

the

body

of

r

such

that

not

-LRB-

b

-RRB-

I

or

c

I.

there

is

an

atom

b

in

the

body

of

r

such

that

b

U

Example

Consider

the

set

of

rules

P

fp

not

-LRB-

p

-RRB-

q

not

-LRB-

p

-RRB-

a

b

not

-LRB-

c

-RRB-

b

ag

Then

fa

b

cg

is

an

unfounded

set

of

P

with

respect

to

the

empty

set

The

set

fp

qg

is

not

an

unfounded

set

with

respect

to

the

empty

set

However

the

set

fp

qg

is

an

unfounded

set

with

respect

to

the

interpretation

fpg

The

larger

set

fa

b

c

p

qg

is

also

an

unfounded

set

with

respect

to

fpg

The

union

of

all

unfounded

sets

of

P

with

respect

to

I

is

the

greatest

unfounded

set

of

P

with

respect

to

I

We

denote

this

set

by

GUSP

-LRB-

I

-RRB-

and

define

UP

-LRB-

I

-RRB-

not

-LRB-

GUSP

-LRB-

I

-RRB-

-RRB-

-LRB-

-RRB-

Definition

Let

P

be

a

logic

program

and

let

I

be

an

interpretation

The

transformation

TP

-LRB-

I

-RRB-

is

defined

as

follows

An

atom

a

is

in

TP

-LRB-

I

-RRB-

if

and

only

if

there

is

a

rule

in

P

with

head

a

such

that

all

literals

in

the

body

of

the

rule

are

in

I.

For

example

if

P

fa

b

d

not

-LRB-

c

-RRB-

g

then

the

transformation

TP

-LRB-

fbg

-RRB-

fag

and

TP

-LRB-

fnot

-LRB-

c

-RRB-

g

-RRB-

fdg

Define

a

sequence

of

interpretations

as

follows

Let

-LRB-

-RRB-

and

for

all

k

Ik

TP

-LRB-

Ik

-RRB-

-LSB-

UP

-LRB-

Ik

-RRB-

-LRB-

-RRB-

The

well-founded

model

of

the

program

P

is

then

I

-LSB-

k

Ik

-LRB-

-RRB-

An

interpretation

of

a

logic

program

P

that

covers

Atoms

-LRB-

P

-RRB-

is

a

total

interpretation

Similarly

a

well-founded

model

of

P

is

a

total

well-founded

model

if

it

covers

all

atoms

in

Atoms

-LRB-

P

-RRB-

Example

Let

P

be

the

set

of

rules

fa

not

-LRB-

b

-RRB-

b

not

-LRB-

a

-RRB-

not

-LRB-

c

-RRB-

d

a

b

c

g

We

will

compute

the

well-founded

model

of

P

As

the

interpretation

TP

-LRB-

;-RRB-

fcg

and

UP

-LRB-

;-RRB-

we

set

fcg

Now

TP

-LRB-

-RRB-

fcg

and

UP

-LRB-

-RRB-

fnot

-LRB-

b

-RRB-

not

-LRB-

d

-RRB-

g

which

gives

fc

not

-LRB-

b

-RRB-

not

-LRB-

d

-RRB-

g.

Thus

the

transformation

TP

-LRB-

-RRB-

fa

cg

UP

-LRB-

-RRB-

fnot

-LRB-

b

-RRB-

not

-LRB-

d

-RRB-

g

and

fa

c

not

-LRB-

b

-RRB-

not

-LRB-

d

-RRB-

g.

Finally

as

TP

-LRB-

-RRB-

fa

cg

and

UP

-LRB-

-RRB-

fnot

-LRB-

b

-RRB-

not

-LRB-

d

-RRB-

g.

Hence

the

interpretations

and

the

well-founded

model

of

P

is

k

Ik

fa

c

not

-LRB-

b

-RRB-

not

-LRB-

d

-RRB-

g.

By

noting

that

Atoms

-LRB-

P

-RRB-

fa

b

c

dg

we

see

that

the

well-founded

model

of

P

is

in

fact

a

total

well-founded

model

An

Algorithm

for

Computing

Stable

Models

In

this

section

we

examine

an

algorithm

for

computing

stable

models

This

algorithm

was

presented

in

-LSB-

-RSB-

as

a

decision

procedure

for

default

logic

-LSB-

-RSB-

In

-LSB-

-RSB-

it

was

used

to

implement

the

stable

model

semantics

The

algorithm

is

based

on

ideas

introduced

in

-LSB-

-RSB-

in

the

context

of

autoepistemic

reasoning

We

describe

the

algorithm

in

Section

In

Sections

-LCB-

we

describe

an

implementation

of

the

two

functions

expand

and

conflict

needed

by

the

algorithm

In

Section

we

note

a

redundancy

in

the

algorithm

Finally

in

Section

we

present

a

novel

algorithm

based

on

the

algorithm

described

in

Sections

-LCB-

that

is

simpler

and

prunes

the

search

space

more

effectively

The

Algorithm

The

algorithm

smodels

described

in

Figure

is

a

procedure

for

computing

stable

models

of

a

set

of

rules

P

The

algorithm

takes

as

input

a

set

of

rules

P

a

set

of

literals

B

and

a

set

of

atoms

F

The

aim

of

the

algorithm

is

to

compute

a

stable

model

of

P

agreeing

with

B

-LSB-

F

such

that

the

function

test

-LRB-

-RRB-

returns

true

This

goal

is

achieved

by

constructing

P

full

sets

until

a

stable

model

that

is

accepted

by

test

is

found

The

P

full

sets

are

constructed

one

at

a

time

by

adding

not-atoms

to

the

set

B.

All

P

full

sets

are

found

using

backtracking

The

not-atoms

that

are

removed

from

B

during

backtracking

are

stored

as

atoms

in

the

set

F

The

set

F

therefore

contains

the

atoms

that

have

to

be

included

in

the

stable

model

The

algorithm

smodels

has

to

examine

a

very

large

search

space

as

every

subset

of

NAnt

-LRB-

P

-RRB-

is

a

possible

P

full

set

In

order

to

reduce

the

search

space

smodels

uses

two

functions

expand

and

conflict

The

function

expand

tries

to

expand

the

set

B

as

much

as

possible

Literals

that

can

not

be

in

the

P

full

set

under

construction

are

added

to

B

and

literals

that

have

to

be

in

the

P

full

set

are

added

to

B

The

remaining

search

space

is

halved

each

time

a

literal

is

added

to

the

set

B.

By

remaining

search

space

we

mean

all

subsets

of

the

set

of

atoms

in

NAnt

-LRB-

P

-RRB-

not

covered

by

B

-LSB-

F

The

function

conflict

detects

conflicts

i.e.

situations

where

there

are

no

stable

models

agreeing

with

B

-LSB-

F

It

is

therefore

possible

to

begin

backtracking

as

soon

as

a

conflict

is

detected

The

remaining

search

space

is

also

halved

when

a

conflict

is

found

The

algorithm

smodels

is

sound

and

complete

if

the

functions

expand

and

conflict

fulfill

some

basic

conditions

Soundness

guarantees

that

if

smodels

returns

true

there

is

a

stable

model

of

P

agreeing

with

B

-LSB-

F

such

that

test

-LRB-

-RRB-

returns

true

Completeness

on

the

other

hand

guarantees

that

if

function

smodels

-LRB-

P

B

F

-RRB-

Bn

expand

-LRB-

P

B

F

-RRB-

if

conflict

-LRB-

P

Bn

F

-RRB-

returns

true

then

return

false

else

if

NAnt

-LRB-

P

-RRB-

is

covered

by

Bn

-LSB-

F

then

if

F

Bn

then

return

test

-LRB-

Dcl

-LRB-

P

B?n

-RRB-

-RRB-

else

return

false

endif

else

take

some

O

NAnt

-LRB-

P

-RRB-

not

covered

by

Bn

-LSB-

F

if

smodels

-LRB-

P

Bn

-LSB-

fnot

-LRB-

O

-RRB-

g

F

-RRB-

returns

true

then

return

true

else

return

smodels

-LRB-

P

Bn

F

-LSB-

fOg

-RRB-

endif

endif

Figure

The

Decision

Procedure

for

Logic

Programs

with

the

Stable

Model

Semantics

there

is

a

stable

model

agreeing

with

B

-LSB-

F

such

that

test

-LRB-

-RRB-

returns

true

then

smodels

also

returns

true

Let

Bn

expand

-LRB-

P

B

F

-RRB-

We

assume

that

the

function

expand

fulfills

the

following

conditions

B

Bn

If

B

is

grounded

in

P

then

Bn

is

grounded

in

P

If

there

is

an

extension

of

P

such

that

agrees

with

B

-LSB-

F

then

agrees

with

Bn

-LSB-

F

For

all

a

NAnt

-LRB-

P

-RRB-

a

Dcl

-LRB-

P

Bn

-RRB-

implies

that

a

is

covered

by

Bn

The

function

conflict

returns

true

or

false

and

satisfies

the

following

conditions

The

function

conflict

returns

true

if

there

exists

a

literal

not

-LRB-

a

-RRB-

B

such

that

a

Dcl

-LRB-

P

B

-RRB-

If

conflict

returns

true

then

there

exists

no

extension

of

P

such

that

agrees

with

B

-LSB-

F

We

now

prove

that

smodels

is

sound

and

complete

Theorem

-LRB-

Soundness

-RRB-

Let

P

be

a

set

of

rules

let

B

be

a

set

of

literals

grounded

in

P

and

let

F

be

a

set

of

atoms

If

smodels

-LRB-

P

B

F

-RRB-

returns

true

then

there

exists

a

stable

model

of

P

agreeing

with

B

-LSB-

F

such

that

test

-LRB-

-RRB-

returns

true

Proof

The

proof

is

by

induction

on

the

number

of

atoms

in

NAnt

-LRB-

P

-RRB-

not

covered

by

B

-LSB-

F

We

prove

that

if

NAnt

-LRB-

P

-RRB-

is

covered

by

B

-LSB-

F

if

B

is

grounded

in

P

and

if

the

function

smodels

-LRB-

P

B

F

-RRB-

returns

true

then

there

exists

a

stable

model

Dcl

-LRB-

P

B?n

-RRB-

of

P

agreeing

with

B

-LSB-

F

such

that

test

-LRB-

-RRB-

returns

true

Assume

that

NAnt

-LRB-

P

-RRB-

is

covered

by

B

-LSB-

F

B

is

grounded

in

P

and

that

smodels

-LRB-

P

B

F

-RRB-

returns

true

We

have

that

F

Bn

NAnt

-LRB-

P

-RRB-

is

covered

by

Bn

and

that

conflict

-LRB-

P

Bn

F

-RRB-

returns

false

Now

a

Dcl

-LRB-

P

B?n

-RRB-

implies

not

-LRB-

a

-RRB-

Bn

by

If

a

NAnt

-LRB-

P

-RRB-

and

not

-LRB-

a

-RRB-

Bn

then

a

Bn

Since

Bn

is

grounded

in

P

by

a

Bn

implies

a

Dcl

-LRB-

P

B?n

-RRB-

Thus

Bn

is

P

full

and

as

B

Bn

by

there

exists

a

stable

model

Dcl

-LRB-

P

B?n

-RRB-

of

P

agreeing

with

B

-LSB-

F

such

that

test

-LRB-

-RRB-

returns

true

Induction

hypothesis

assume

that

if

the

number

of

atoms

in

NAnt

-LRB-

P

-RRB-

not

covered

by

B

-LSB-

F

is

less

than

n

if

B

is

grounded

in

P

and

if

the

function

smodels

-LRB-

P

B

F

-RRB-

returns

true

then

there

exists

a

stable

model

of

P

agreeing

with

B

-LSB-

F

such

that

test

-LRB-

-RRB-

returns

true

Suppose

the

number

of

atoms

in

NAnt

-LRB-

P

-RRB-

not

covered

by

B

-LSB-

F

is

n

B

is

grounded

in

P

and

smodels

-LRB-

P

B

F

-RRB-

returns

true

As

smodels

-LRB-

P

B

F

-RRB-

returns

true

conflict

-LRB-

P

Bn

F

-RRB-

returns

false

If

NAnt

-LRB-

P

-RRB-

is

covered

by

Bn

-LSB-

F

then

by

the

proof

above

Dcl

-LRB-

P

B?n

-RRB-

is

a

stable

model

of

P

agreeing

with

B

-LSB-

F

such

that

test

-LRB-

-RRB-

returns

true

Otherwise

an

atom

O

NAnt

-LRB-

P

-RRB-

not

covered

by

Bn

-LSB-

F

is

taken

Now

either

smodels

-LRB-

P

Bn

-LSB-

fnot

-LRB-

O

-RRB-

g

F

-RRB-

returns

true

or

smodels

-LRB-

P

Bn

F

-LSB-

fOg

-RRB-

returns

true

In

addition

B

-LSB-

F

Bn

-LSB-

F

-LSB-

fnot

-LRB-

O

-RRB-

g

and

B

-LSB-

F

Bn

-LSB-

F

-LSB-

fOg

by

the

condition

Hence

the

induction

hypothesis

proves

that

there

exists

a

stable

model

of

P

agreeing

with

B

-LSB-

F

such

that

test

-LRB-

-RRB-

returns

true

Theorem

-LRB-

Completeness

-RRB-

Let

P

be

a

set

of

rules

let

B

be

a

set

of

literals

and

let

F

be

a

set

of

atoms

If

there

exists

a

stable

model

of

P

agreeing

with

B

-LSB-

F

such

that

test

-LRB-

-RRB-

returns

true

then

smodels

-LRB-

P

B

F

-RRB-

returns

true

Proof

The

proof

is

by

induction

on

the

number

of

atoms

in

NAnt

-LRB-

P

-RRB-

not

covered

by

B

-LSB-

F

We

prove

that

if

there

exists

a

stable

model

of

P

agreeing

with

B

-LSB-

F

such

that

test

-LRB-

-RRB-

returns

true

then

smodels

-LRB-

P

B

F

-RRB-

returns

true

Assume

there

exists

a

stable

model

of

P

agreeing

with

B

-LSB-

F

such

that

test

-LRB-

-RRB-

returns

true

Assume

that

NAnt

-LRB-

P

-RRB-

is

covered

by

B

-LSB-

F

The

function

conflict

-LRB-

P

Bn

F

-RRB-

returns

false

by

the

conditions

and

As

agrees

with

Bn

-LSB-

F

and

as

implies

that

NAnt

-LRB-

P

-RRB-

is

covered

by

Bn

-LSB-

F

Dcl

-LRB-

P

not

-LRB-

NAnt

-LRB-

P

-RRB-

-RRB-

-RRB-

Dcl

-LRB-

P

Bn

-LSB-

F

-RRB-

Dcl

-LRB-

P

B?n

-RRB-

Thus

F

Bn

holds

by

condition

Hence

smodels

-LRB-

P

B

F

-RRB-

returns

true

as

Dcl

-LRB-

P

B?n

-RRB-

and

test

-LRB-

-RRB-

returns

true

Induction

hypothesis

if

there

exists

a

stable

model

of

P

agreeing

with

B

-LSB-

F

such

that

test

-LRB-

-RRB-

returns

true

and

if

the

number

of

atoms

in

NAnt

-LRB-

P

-RRB-

not

covered

by

B

-LSB-

F

is

less

than

n

then

smodels

-LRB-

P

B

F

-RRB-

returns

true

Assume

that

the

number

of

atoms

in

NAnt

-LRB-

P

-RRB-

not

covered

by

B

-LSB-

F

is

n.

Assume

there

exists

a

stable

model

agreeing

with

B

-LSB-

F

such

that

test

-LRB-

-RRB-

returns

true

The

conditions

and

implies

that

conflict

-LRB-

P

Bn

F

-RRB-

returns

false

If

NAnt

-LRB-

P

-RRB-

is

covered

by

Bn

-LSB-

F

then

smodels

returns

true

by

the

proof

above

If

not

an

atom

O

NAnt

-LRB-

P

-RRB-

not

covered

by

Bn

-LSB-

F

is

taken

If

there

exists

a

stable

model

agreeing

with

Bn

-LSB-

F

-LSB-

fnot

-LRB-

O

-RRB-

g

such

that

test

-LRB-

-RRB-

returns

true

then

smodels

-LRB-

P

Bn

-LSB-

fnot

-LRB-

O

-RRB-

g

F

-RRB-

returns

true

by

and

the

induction

hypothesis

Otherwise

is

a

stable

model

agreeing

with

Bn

-LSB-

F

-LSB-

fOg

and

smodels

-LRB-

P

Bn

F

-LSB-

fnot

-LRB-

O

-RRB-

g

-RRB-

returns

true

Thus

smodels

-LRB-

P

B

F

-RRB-

returns

true

These

theorems

imply

that

it

is

possible

to

solve

different

reasoning

tasks

by

choosing

suitable

test

functions

Corollary

Let

P

be

a

set

of

rules

and

let

OE

be

an

atom

If

test

-LRB-

-RRB-

returns

true

for

all

and

OE

then

smodels

-LRB-

P

;-RRB-

returns

true

if

and

only

if

P

has

a

stable

model

If

test

-LRB-

-RRB-

returns

true

if

and

only

if

OE

then

smodels

-LRB-

P

;-RRB-

returns

true

if

and

only

if

there

exists

a

stable

model

of

P

containing

OE

If

test

-LRB-

-RRB-

returns

true

if

and

only

if

OE

then

smodels

-LRB-

P

;-RRB-

returns

true

if

and

only

if

there

exists

a

stable

model

of

P

not

containing

OE

and

in

other

words

smodels

-LRB-

P

;-RRB-

returns

false

if

and

only

if

OE

is

contained

in

every

stable

model

of

P

Expanding

B

An

implementation

of

the

function

expand

is

given

in

Figure

The

function

expand

enlarges

the

set

B

by

assuming

that

there

exists

a

stable

model

agreeing

with

B

-LSB-

F

and

by

using

the

relation

Dcl

-LRB-

P

B

-RRB-

Dcl

-LRB-

P

not

-LRB-

NAnt

-LRB-

P

-RRB-

-LRB-

B

-LSB-

F

-RRB-

-RRB-

-RRB-

-LRB-

-RRB-

As

Dcl

-LRB-

P

not

-LRB-

NAnt

-LRB-

P

-RRB-

-RRB-

-RRB-

and

as

B

not

-LRB-

NAnt

-LRB-

P

-RRB-

-RRB-

if

agrees

with

B

we

see

that

Dcl

-LRB-

P

B

-RRB-

holds

The

other

set

inclusion

is

proved

in

Lemma

We

prove

that

the

function

satisfies

the

conditions

Lemma

Let

be

a

stable

model

of

the

set

of

rules

P

and

let

agree

with

a

set

of

literals

Then

OE

implies

OE

Dcl

-LRB-

P

not

-LRB-

NAnt

-LRB-

P

-RRB-

-RRB-

-RRB-

Proof

If

OE

then

OE

as

agrees

with

Thus

NAnt

-LRB-

P

-RRB-

NAnt

-LRB-

P

-RRB-

and

as

Dcl

-LRB-

P

not

-LRB-

NAnt

-LRB-

P

-RRB-

-RRB-

-RRB-

we

can

conclude

that

the

stable

model

Dcl

-LRB-

P

not

-LRB-

NAnt

-LRB-

P

-RRB-

-RRB-

-RRB-

Proposition

The

implementation

in

Figure

satisfies

conditions

-LCB-

function

expand

-LRB-

P

B

F

-RRB-

repeat

B

B

B

-LSB-

fa

NAnt

-LRB-

P

-RRB-

j

a

not

covered

by

B

a

Dcl

-LRB-

P

B

-RRB-

g

B

B

-LSB-

fnot

-LRB-

a

-RRB-

j

a

NAnt

-LRB-

P

-RRB-

a

not

covered

by

B

a

Dcl

-LRB-

P

not

-LRB-

NAnt

-LRB-

P

-RRB-

-LRB-

B

-LSB-

F

-RRB-

-RRB-

-RRB-

g

until

B

return

B

Figure

Implementation

of

the

Function

expand

function

conflict

-LRB-

P

B

F

-RRB-

if

there

exists

not

-LRB-

a

-RRB-

B

such

that

a

Dcl

-LRB-

P

B

-LSB-

F

-RRB-

then

return

true

else

if

there

exists

a

F

such

that

a

Dcl

-LRB-

P

not

-LRB-

NAnt

-LRB-

P

-RRB-

-LRB-

B

-LSB-

F

-RRB-

-RRB-

-RRB-

then

return

true

else

return

false

endif

Figure

Implementation

of

the

Function

conflict

Proof

The

condition

obviously

holds

as

no

literals

are

removed

from

B

The

conditions

and

hold

as

B

fa

NAnt

-LRB-

P

-RRB-

j

a

not

covered

by

B

a

Dcl

-LRB-

P

B

-RRB-

g

holds

when

expand

returns

If

agrees

with

B

-LSB-

F

then

agrees

with

B

and

Dcl

-LRB-

P

B

-RRB-

as

B

not

-LRB-

-RRB-

Since

Lemma

guarantees

that

if

OE

Dcl

-LRB-

P

not

-LRB-

NAnt

-LRB-

P

-RRB-

-LRB-

B

-LSB-

F

-RRB-

-RRB-

-RRB-

then

OE

we

have

shown

that

expand

also

fulfills

condition

Detecting

Conflicts

The

function

conflict

is

described

in

Figure

The

function

takes

advantage

of

the

fact

that

the

set

F

is

supposed

to

be

in

the

final

stable

model

to

test

for

conflicts

We

prove

that

the

function

satisfies

the

conditions

-LCB-

Proposition

The

implementation

in

Figure

satisfies

conditions

-LCB-

Proof

Since

Dcl

-LRB-

P

B

-RRB-

Dcl

-LRB-

P

B

-LSB-

F

-RRB-

the

condition

holds

To

prove

assume

there

exists

a

stable

model

of

P

agreeing

with

B

-LSB-

F

Then

B

-LSB-

F

and

B

not

-LRB-

-RRB-

As

Dcl

-LRB-

P

not

-LRB-

-RRB-

-RRB-

we

immediately

get

Dcl

-LRB-

P

B

-LSB-

F

-RRB-

Hence

not

-LRB-

OE

-RRB-

B

implies

OE

Dcl

-LRB-

P

B

-LSB-

F

-RRB-

If

OE

F

then

OE

and

by

Lemma

OE

Dcl

-LRB-

P

not

-LRB-

NAnt

-LRB-

P

-RRB-

-LRB-

B

-LSB-

F

-RRB-

-RRB-

-RRB-

Thus

condition

is

satisfied

The

F

Subset

of

Bn

Test

The

test

F

Bn

in

the

function

smodels

is

not

needed

when

one

is

using

the

conflict

and

expand

functions

defined

in

the

previous

sections

Assume

that

the

atom

a

F

but

a

Bn

conflict

-LRB-

P

Bn

F

-RRB-

returns

false

and

that

NAnt

-LRB-

P

-RRB-

is

covered

by

Bn

-LSB-

F

Then

a

F

implies

a

Dcl

-LRB-

P

Bn

-LSB-

F

-RRB-

which

implies

that

not

-LRB-

a

-RRB-

B?n

as

conflict

returns

false

Furthermore

if

a

literal

not

-LRB-

b

-RRB-

Bn

but

b

NAnt

-LRB-

P

-RRB-

then

b

Bn

or

b

F

as

NAnt

-LRB-

P

-RRB-

is

covered

by

Bn

-LSB-

F

Thus

not

-LRB-

NAnt

-LRB-

P

-RRB-

-LRB-

Bn

-LSB-

F

-RRB-

-RRB-

B?n

By

the

monotonicity

of

the

deductive

closure

operator

Dcl

-LRB-

P

-RRB-

we

have

that

Dcl

-LRB-

P

not

-LRB-

NAnt

-LRB-

P

-RRB-

-LRB-

Bn

-LSB-

F

-RRB-

-RRB-

-RRB-

Dcl

-LRB-

P

B?n

-RRB-

Now

for

a

NAnt

-LRB-

P

-RRB-

a

Bn

and

not

-LRB-

a

-RRB-

Bn

implies

a

Dcl

-LRB-

P

B?n

-RRB-

by

the

definition

of

expand

But

a

F

implies

a

Dcl

-LRB-

P

not

-LRB-

NAnt

-LRB-

P

-RRB-

-LRB-

Bn

-LSB-

F

-RRB-

-RRB-

-RRB-

as

conflict

returns

false

This

contradiction

proves

that

a

F

implies

a

Bn

or

in

other

words

that

F

Bn

Hence

this

test

is

superfluous

and

can

be

removed

Improving

the

Algorithm

Eliminating

the

F

Bn

test

simplifies

the

algorithm

somewhat

It

is

however

possible

to

simplify

it

even

further

By

strengthening

the

expand

function

one

improves

the

algorithm

as

the

search

space

is

then

pruned

more

effectively

We

strengthen

the

expand

function

in

a

suitable

way

thereby

making

it

possible

to

eliminate

the

set

F

The

new

algorithm

is

described

in

Figures

-LCB-

The

algorithm

is

improved

in

three

ways

Instead

of

three

different

deductive

closures

only

two

different

deductive

closures

need

to

be

computed

The

function

is

strengthened

by

replacing

B

B

-LSB-

fa

NAnt

-LRB-

P

-RRB-

j

a

not

covered

by

B

a

Dcl

-LRB-

P

B

-RRB-

g

with

B

B

-LSB-

fa

NAnt

-LRB-

P

-RRB-

j

a

not

covered

by

B

a

Dcl

-LRB-

P

B

-RRB-

g

As

the

set

B

in

contains

the

set

F

of

expand

the

search

space

in

pruned

more

effectively

Finally

removing

the

set

F

makes

the

algorithm

simpler

which

in

turn

makes

it

easier

to

implement

We

prove

that

the

algorithm

is

sound

and

complete

Theorem

-LRB-

Soundness

-RRB-

Let

P

be

a

set

of

rules

and

let

B

be

a

set

of

literals

If

-LRB-

P

B

-RRB-

returns

true

then

there

exists

a

stable

model

of

P

agreeing

with

B

such

that

test

-LRB-

-RRB-

returns

true

function

-LRB-

P

B

-RRB-

Bn

-LRB-

P

B

-RRB-

if

-LRB-

P

Bn

-RRB-

returns

true

then

return

false

else

if

NAnt

-LRB-

P

-RRB-

is

covered

by

Bn

then

return

test

-LRB-

Dcl

-LRB-

P

Bn

-RRB-

-RRB-

else

take

some

O

NAnt

-LRB-

P

-RRB-

not

covered

by

Bn

if

-LRB-

P

Bn

-LSB-

fnot

-LRB-

O

-RRB-

g

-RRB-

returns

true

then

return

true

else

return

-LRB-

P

Bn

-LSB-

fOg

-RRB-

endif

endif

Figure

Improved

Decision

Procedure

for

Logic

Programs

with

the

Stable

Model

Semantics

function

-LRB-

P

B

-RRB-

repeat

B

B

B

-LSB-

fa

NAnt

-LRB-

P

-RRB-

j

a

not

covered

by

B

a

Dcl

-LRB-

P

B

-RRB-

g

B

B

-LSB-

fnot

-LRB-

a

-RRB-

j

a

NAnt

-LRB-

P

-RRB-

a

not

covered

by

B

a

Dcl

-LRB-

P

not

-LRB-

NAnt

-LRB-

P

-RRB-

B

-RRB-

-RRB-

g

until

B

return

B

Figure

Improved

Version

of

the

Function

expand

function

-LRB-

P

B

-RRB-

if

there

exists

not

-LRB-

a

-RRB-

B

such

that

a

Dcl

-LRB-

P

B

-RRB-

then

return

true

else

if

there

exists

a

B

such

that

a

Dcl

-LRB-

P

not

-LRB-

NAnt

-LRB-

P

-RRB-

B

-RRB-

-RRB-

then

return

true

else

return

false

endif

Figure

Improved

Version

of

the

Function

conflict

Proof

The

proof

is

by

induction

on

the

number

of

atoms

in

NAnt

-LRB-

P

-RRB-

not

covered

by

B

We

prove

that

if

NAnt

-LRB-

P

-RRB-

is

covered

by

B

and

if

-LRB-

P

B

-RRB-

returns

true

then

there

exists

a

stable

model

Dcl

-LRB-

P

Bn

-RRB-

agreeing

with

B

such

that

test

-LRB-

-RRB-

returns

true

Assume

that

NAnt

-LRB-

P

-RRB-

is

covered

by

B

and

that

-LRB-

P

B

-RRB-

returns

true

Then

B

Bn

as

does

not

remove

any

atoms

from

B

and

not

-LRB-

B

n

-RRB-

B?n

as

-LRB-

P

Bn

-RRB-

returns

false

Furthermore

a

Bn

implies

a

Dcl

-LRB-

P

not

-LRB-

NAnt

-LRB-

P

-RRB-

Bn

-RRB-

-RRB-

as

returns

false

Hence

the

deductive

closure

Dcl

-LRB-

P

Bn

-RRB-

Dcl

-LRB-

P

not

-LRB-

NAnt

-LRB-

P

-RRB-

Bn

-RRB-

-RRB-

as

not-atoms

must

be

in

not

-LRB-

NAnt

-LRB-

P

-RRB-

-RRB-

to

have

any

effect

on

the

size

of

the

closure

As

not

-LRB-

NAnt

-LRB-

P

-RRB-

Bn

-RRB-

B?n

we

deduce

that

Dcl

-LRB-

P

Bn

-RRB-

Dcl

-LRB-

P

not

-LRB-

NAnt

-LRB-

P

-RRB-

Bn

-RRB-

-RRB-

Dcl

-LRB-

P

B?n

-RRB-

It

is

then

clear

that

a

Dcl

-LRB-

P

B?n

-RRB-

implies

not

-LRB-

a

-RRB-

B?n

Finally

if

not

-LRB-

a

-RRB-

Bn

then

a

Dcl

-LRB-

P

Bn

-RRB-

as

returns

false

Hence

NAnt

-LRB-

P

-RRB-

B?n

is

P

full

and

there

exists

a

stable

model

Dcl

-LRB-

P

Bn

-RRB-

agreeing

with

B

such

that

test

-LRB-

-RRB-

returns

true

Induction

hypothesis

assume

that

if

the

number

of

atoms

in

NAnt

-LRB-

P

-RRB-

not

covered

by

B

is

less

than

n

and

if

-LRB-

P

B

-RRB-

returns

true

then

there

exists

a

stable

model

of

P

agreeing

with

B

such

that

test

-LRB-

-RRB-

returns

true

Suppose

the

number

of

atoms

in

NAnt

-LRB-

P

-RRB-

not

covered

by

B

is

n

and

suppose

that

-LRB-

P

B

-RRB-

returns

true

As

-LRB-

P

B

-RRB-

returns

true

-LRB-

P

Bn

-RRB-

returns

false

As

does

not

remove

any

atoms

from

B

we

see

that

B

Bn

If

the

set

NAnt

-LRB-

P

-RRB-

is

covered

by

Bn

then

as

is

proved

above

Dcl

-LRB-

P

Bn

-RRB-

is

a

stable

model

of

P

agreeing

with

B

such

that

test

-LRB-

-RRB-

returns

true

Otherwise

an

atom

O

NAnt

-LRB-

P

-RRB-

not

covered

by

Bn

is

taken

Now

either

-LRB-

P

Bn

-LSB-

fnot

-LRB-

O

-RRB-

g

-RRB-

returns

true

or

-LRB-

P

Bn

-LSB-

fOg

-RRB-

returns

true

Hence

the

induction

hypothesis

proves

that

there

exists

a

stable

model

of

P

agreeing

with

B

such

that

test

-LRB-

-RRB-

returns

true

If

the

stable

model

agrees

with

B

then

Dcl

-LRB-

P

B

-RRB-

as

B

not

-LRB-

-RRB-

and

as

B

Lemma

proves

that

if

OE

Dcl

-LRB-

P

not

-LRB-

NAnt

-LRB-

P

-RRB-

B

-RRB-

-RRB-

then

OE

Thus

we

have

proved

that

fulfills

condition

of

Section

Theorem

-LRB-

Completeness

-RRB-

Let

P

be

a

set

of

rules

and

let

B

be

a

set

of

literals

If

there

exists

a

stable

model

of

P

agreeing

with

B

such

that

test

-LRB-

-RRB-

returns

true

then

-LRB-

P

B

-RRB-

returns

true

Proof

The

proof

is

by

induction

on

the

number

of

atoms

in

NAnt

-LRB-

P

-RRB-

not

covered

by

B

We

prove

that

if

there

exists

a

stable

model

of

P

agreeing

with

B

such

that

test

-LRB-

-RRB-

returns

true

then

-LRB-

P

B

-RRB-

returns

true

Assume

there

exists

a

stable

model

of

P

agreeing

with

B

such

that

the

function

test

-LRB-

-RRB-

returns

true

Assume

that

NAnt

-LRB-

P

-RRB-

is

covered

by

B.

Then

B

Bn

as

does

not

remove

any

atoms

from

B.

Now

OE

NAnt

-LRB-

P

-RRB-

if

and

only

if

OE

NAnt

-LRB-

P

-RRB-

and

OE

which

is

equivalent

to

OE

NAnt

-LRB-

P

-RRB-

and

OE

B

n

as

NAnt

-LRB-

P

-RRB-

is

covered

by

Bn

Thus

not

-LRB-

NAnt

-LRB-

P

-RRB-

-RRB-

not

-LRB-

NAnt

-LRB-

P

-RRB-

-RRB-

B?n

and

therefore

Dcl

-LRB-

P

not

-LRB-

NAnt

-LRB-

P

-RRB-

-RRB-

-RRB-

Dcl

-LRB-

P

B?n

-RRB-

Dcl

-LRB-

P

not

-LRB-

NAnt

-LRB-

P

-RRB-

Bn

-RRB-

-RRB-

As

B

n

we

conclude

that

Dcl

-LRB-

P

Bn

-RRB-

Dcl

-LRB-

P

not

-LRB-

NAnt

-LRB-

P

-RRB-

Bn

-RRB-

-RRB-

Hence

-LRB-

P

Bn

-RRB-

returns

false

test

-LRB-

Dcl

-LRB-

P

Bn

-RRB-

-RRB-

returns

true

and

-LRB-

P

B

-RRB-

returns

true

Induction

hypothesis

if

there

exists

a

stable

model

of

P

agreeing

with

B

such

that

test

-LRB-

-RRB-

returns

true

and

if

the

number

of

atoms

in

NAnt

-LRB-

P

-RRB-

not

covered

by

B

is

less

than

n

then

-LRB-

P

B

-RRB-

returns

true

Assume

that

the

number

of

atoms

in

NAnt

-LRB-

P

-RRB-

not

covered

by

B

is

n

and

there

exists

a

stable

model

agreeing

with

B

such

that

test

-LRB-

-RRB-

returns

true

The

condition

implies

that

agrees

with

Bn

Now

OE

B

n

implies

OE

which

by

Lemma

implies

that

OE

Dcl

-LRB-

P

not

-LRB-

NAnt

-LRB-

P

-RRB-

Bn

-RRB-

-RRB-

In

addition

not

-LRB-

OE

-RRB-

Bn

implies

OE

which

in

turn

implies

OE

Dcl

-LRB-

P

Bn

-RRB-

as

Dcl

-LRB-

P

Bn

-RRB-

Thus

-LRB-

P

Bn

-RRB-

returns

false

If

NAnt

-LRB-

P

-RRB-

is

covered

by

Bn

then

returns

true

by

the

proof

above

If

not

an

atom

O

NAnt

-LRB-

P

-RRB-

not

covered

by

Bn

is

taken

If

there

exists

a

stable

model

agreeing

with

Bn

-LSB-

fnot

-LRB-

O

-RRB-

g

such

that

test

-LRB-

-RRB-

returns

true

then

-LRB-

P

Bn

-LSB-

fnot

-LRB-

O

-RRB-

g

-RRB-

returns

true

by

the

induction

hypothesis

Otherwise

is

a

stable

model

agreeing

with

Bn

-LSB-

fOg

and

-LRB-

P

Bn

-LSB-

fOg

-RRB-

returns

true

Thus

-LRB-

P

B

-RRB-

returns

true

Example

By

examining

the

execution

of

smodels

and

on

a

simple

example

we

show

that

indeed

prunes

the

search

space

more

effectively

than

smodels

Let

P

be

the

set

of

rules

fa

not

-LRB-

a

-RRB-

b

a

h

not

-LRB-

b

-RRB-

h

not

-LRB-

h

-RRB-

a

not

-LRB-

h

-RRB-

b

not

-LRB-

h

-RRB-

g

We

examine

the

execution

of

the

function

call

smodels

-LRB-

P

;-RRB-

The

call

to

expand

-LRB-

P

;-RRB-

returns

the

empty

set

and

conflict

-LRB-

P

;-RRB-

returns

false

Assume

that

a

is

chosen

from

NAnt

-LRB-

P

-RRB-

Then

we

call

smodels

-LRB-

P

fnot

-LRB-

a

-RRB-

g

;-RRB-

As

the

deductive

closure

Dcl

-LRB-

P

fnot

-LRB-

a

-RRB-

g

-RRB-

fa

bg

conflict

returns

true

and

smodels

-LRB-

P

fnot

-LRB-

a

-RRB-

g

;-RRB-

returns

false

Next

smodels

-LRB-

P

fag

-RRB-

is

called

expand

-LRB-

P

fag

-RRB-

returns

the

empty

set

and

conflict

returns

false

Now

assume

that

b

is

chosen

and

that

smodels

-LRB-

P

fnot

-LRB-

b

-RRB-

g

fag

-RRB-

is

called

Then

expand

-LRB-

P

fnot

-LRB-

b

-RRB-

g

fag

-RRB-

returns

the

set

fh

not

-LRB-

b

-RRB-

not

-LRB-

a

-RRB-

g

and

the

function

conflict

returns

true

Hence

smodels

-LRB-

P

fnot

-LRB-

b

-RRB-

g

fag

-RRB-

returns

false

and

we

call

smodels

-LRB-

P

fa

bg

-RRB-

The

call

to

expand

-LRB-

P

fa

bg

-RRB-

returns

the

empty

set

and

conflict

returns

false

Next

smodels

-LRB-

P

fnot

-LRB-

h

-RRB-

g

fa

bg

-RRB-

is

called

whereupon

expand

-LRB-

P

fnot

-LRB-

h

-RRB-

g

fa

bg

-RRB-

returns

fa

b

not

-LRB-

h

-RRB-

g

and

the

function

conflict

returns

true

Finally

smodels

-LRB-

P

fa

b

hg

-RRB-

is

called

the

function

expand

-LRB-

P

fa

b

hg

-RRB-

returns

fnot

-LRB-

a

-RRB-

not

-LRB-

b

-RRB-

not

-LRB-

h

-RRB-

g

and

conflict

returns

true

Thus

we

conclude

that

smodels

-LRB-

P

;-RRB-

returns

false

after

four

conflicts

We

now

compare

this

with

the

execution

of

-LRB-

P

;-RRB-

The

call

to

the

function

-LRB-

P

;-RRB-

returns

the

empty

set

and

-LRB-

P

;-RRB-

returns

false

Assume

that

a

is

chosen

from

the

atoms

in

NAnt

-LRB-

P

-RRB-

Then

we

call

-LRB-

P

fnot

-LRB-

a

-RRB-

g

-RRB-

The

function

-LRB-

P

fnot

-LRB-

a

-RRB-

g

-RRB-

returns

the

set

fb

not

-LRB-

a

-RRB-

g

returns

true

and

-LRB-

P

fnot

-LRB-

a

-RRB-

g

-RRB-

returns

false

In

the

next

step

-LRB-

P

fag

-RRB-

is

called

the

function

-LRB-

P

fag

-RRB-

returns

the

set

fa

bg

and

returns

false

Now

h

is

chosen

and

-LRB-

P

fa

b

not

-LRB-

h

-RRB-

g

-RRB-

is

called

Then

the

function

returns

true

as

Dcl

-LRB-

P

fa

b

not

-LRB-

h

-RRB-

g

-RRB-

fa

b

hg

Finally

-LRB-

P

fa

b

hg

-RRB-

is

called

whereupon

returns

true

as

Dcl

-LRB-

P

not

-LRB-

NAnt

-LRB-

P

-RRB-

fa

b

hg

-RRB-

-RRB-

Thus

we

conclude

that

-LRB-

P

;-RRB-

returns

false

after

three

conflicts

which

is

one

less

than

what

smodels

needs

Computing

Deductive

Closures

We

describe

two

linear

time

algorithms

for

computing

deductive

closures

of

a

set

of

rules

The

deductive

closures

are

calculated

using

a

modified

version

of

the

first

linear

time

algorithm

of

Dowling

and

Gallier

-LSB-

-RSB-

for

computing

the

deductive

closure

of

a

set

of

Horn

clauses

The

modification

is

necessary

as

the

rules

contain

apart

from

ordinary

atoms

also

not-atoms

In

addition

we

exploit

the

fact

that

the

set

R

-LRB-

P

B

-RRB-

shrink

or

grow

when

the

set

of

literals

B

shrink

or

grow

to

compute

the

deductive

closures

of

the

set

of

rules

P

incrementally

In

Section

we

describe

the

basic

algorithm

for

computing

deductive

closures

and

describe

how

it

can

be

used

to

compute

deductive

closures

incrementally

In

Section

we

modify

the

basic

algorithm

to

compute

diminishing

deductive

closures

Deductive

Closures

We

assume

that

the

rules

and

atoms

are

stored

in

arrays

In

order

to

guarantee

constant

access

times

we

let

an

atom

or

rule

be

the

index

into

the

array

If

r

is

the

rule

h

an

not

-LRB-

-RRB-

not

-LRB-

bm

-RRB-

we

let

r

head

denote

the

head

of

the

rule

i.e.

the

atom

h

In

addition

we

join

two

counters

poslit

and

neglit

to

each

rule

These

counters

are

initially

initialized

with

the

number

of

respectively

atoms

and

not-atoms

in

the

body

of

the

rule

For

each

atom

a

we

initialize

a

list

a

poslitlist

with

all

rules

whose

bodies

contain

the

atom

a

as

a

positive

literal

Similarly

for

each

atom

a

we

initialize

a

list

a

headlist

with

all

rules

whose

heads

are

a

An

array

closure

is

used

to

tell

which

atoms

belong

to

the

closure

an

atom

a

belonging

to

the

closure

if

closure

-LSB-

a

-RSB-

true

The

algorithm

that

computes

the

deductive

closure

Dcl

-LRB-

P

B

-RRB-

of

a

set

of

rules

P

and

a

set

of

literals

B

is

described

in

Figure

The

algorithm

works

by

putting

rules

whose

heads

belong

in

the

closure

but

are

not

yet

in

the

closure

on

the

queue

When

a

rule

is

removed

from

the

queue

and

when

the

head

of

the

rule

is

not

in

the

closure

the

algorithm

adds

the

head

to

the

closure

and

then

puts

every

newly

activated

rule

on

the

queue

By

an

activated

rule

we

mean

of

course

a

rule

which

has

the

atoms

in

its

body

in

the

closure

array

and

which

has

the

not-atoms

in

its

body

in

B

The

counter

r

poslit

of

a

rule

r

holds

the

number

of

atoms

in

the

body

of

r

not

in

the

closure

array

and

the

counter

r

neglit

holds

the

number

of

not-atoms

in

the

body

of

r

not

in

B

As

the

deductive

closure

Dcl

-LRB-

P

B

-RRB-

is

the

closure

of

the

set

R

-LRB-

P

B

-RRB-

we

only

want

to

consider

the

rules

whose

neglit

counters

are

zero

Therefore

the

algorithm

checks

this

before

adding

a

rule

to

the

queue

queue

This

is

the

modification

to

the

Dowling-Gallier

algorithm

mentioned

above

In

order

to

compute

the

function

dcl

-LRB-

queue

P

-RRB-

while

queue

is

not

empty

do

r

pop

-LRB-

queue

-RRB-

a

r

head

if

closure

-LSB-

a

-RSB-

false

then

closure

-LSB-

a

-RSB-

true

for

each

rule

a

poslitlist

do

poslit

poslit

if

poslit

and

neglit

then

push

-LRB-

queue

-RRB-

endif

end

for

endif

end

while

Figure

The

Procedure

for

Computing

Deductive

Closures

deductive

closure

Dcl

-LRB-

P

B

-RRB-

we

have

to

initialize

the

queue

queue

with

the

rules

in

P

whose

bodies

are

contained

in

the

set

B

i.e.

with

the

rules

whose

counters

are

zero

The

algorithm

computes

the

deductive

closure

linearly

in

the

size

of

the

set

of

rules

P

as

each

rule

is

added

at

most

once

to

the

queue

and

as

each

atom

is

added

at

most

once

to

the

deductive

closure

The

algorithm

can

be

used

to

compute

the

deductive

closure

incrementally

Suppose

the

algorithm

has

computed

the

closure

Dcl

-LRB-

P

B

-RRB-

Suppose

we

want

to

compute

Dcl

-LRB-

P

-RRB-

and

suppose

B

In

order

to

compute

the

larger

closure

starting

from

the

smaller

we

only

have

to

initialize

the

queue

with

the

rules

whose

bodies

are

in

and

whose

heads

are

not

in

the

closure

The

algorithm

will

then

compute

the

new

closure

when

invoked

Diminishing

Deductive

Closures

What

can

we

do

if

the

set

is

contained

in

B

A

slightly

different

algorithm

is

needed

if

we

want

to

compute

the

closure

efficiently

in

this

case

see

Figure

The

idea

is

that

instead

of

propagating

the

fact

that

an

atom

should

belong

to

the

closure

we

propagate

the

fact

that

an

atom

should

be

removed

from

the

closure

This

is

done

by

pushing

rules

whose

heads

should

be

removed

from

the

deductive

closure

onto

the

queue

When

a

rule

is

removed

from

the

queue

we

check

if

the

head

h

of

the

rule

is

in

the

closure

If

this

is

the

case

then

the

head

is

removed

from

the

closure

and

for

each

rule

who

has

h

in

function

-LRB-

P

B

-RRB-

N

while

is

not

empty

do

r

pop

-LRB-

-RRB-

a

r

head

if

closure

-LSB-

a

-RSB-

true

and

a

B

then

closure

-LSB-

a

-RSB-

false

N

N

-LSB-

fag

for

each

rule

a

poslitlist

do

poslit

poslit

if

poslit

and

neglit

then

push

-LRB-

-RRB-

endif

end

for

endif

end

while

queue

empty

for

each

atom

a

N

do

if

there

exists

a

rule

r

a

headlist

such

that

r

poslit

and

r

neglit

then

push

-LRB-

r

queue

-RRB-

endif

end

for

dcl

-LRB-

queue

P

-RRB-

Figure

The

Procedure

for

Computing

Diminishing

Deductive

Closures

its

body

the

counter

poslit

is

incremented

Finally

those

rules

whose

poslit

counters

are

thus

set

to

one

and

whose

neglit

counters

are

zero

are

added

to

the

queue

as

they

no

longer

imply

that

their

heads

should

belong

to

the

closure

The

problem

with

this

approach

is

that

too

many

atoms

are

possibly

removed

from

the

closure

When

the

algorithm

removes

an

atom

from

the

closure

it

removes

the

atom

even

if

the

atom

is

the

head

of

a

rule

which

is

active

in

the

final

closure

To

remedy

this

problem

we

store

the

atoms

removed

from

the

closure

in

a

temporary

set

N

and

then

use

the

first

algorithm

to

add

the

atoms

wrongly

removed

from

the

closure

to

the

closure

We

can

not

abstain

from

removing

atoms

which

are

heads

of

active

rules

as

we

do

not

know

if

the

rules

will

still

be

active

in

the

final

closure

We

give

a

simple

example

demonstrating

this

Let

P

be

the

set

of

rules

fa

b

b

a

b

not

-LRB-

c

-RRB-

g

If

we

have

computed

the

closure

Dcl

-LRB-

P

fnot

-LRB-

c

-RRB-

g

-RRB-

and

want

to

compute

Dcl

-LRB-

P

;-RRB-

we

remove

not

-LRB-

c

-RRB-

This

makes

the

rule

b

not

-LRB-

c

-RRB-

inactive

Now

if

we

at

this

point

check

if

b

is

the

head

of

an

active

rule

we

find

that

it

is

The

rule

b

a

is

still

active

Thus

if

we

for

this

reason

do

not

remove

b

we

see

that

both

a

and

b

remain

in

the

closure

However

as

Dcl

-LRB-

P

;-RRB-

this

is

not

correct

The

part

of

the

algorithm

that

propagates

the

fact

that

atoms

should

be

removed

from

the

closure

works

in

linear

time

in

the

size

of

the

set

of

rules

P

as

each

rule

is

added

to

the

queue

and

each

atom

is

removed

from

the

closure

at

most

once

The

part

that

adds

rules

whose

heads

have

been

wrongly

removed

from

the

closure

to

the

queue

queue

is

also

linearly

bounded

by

the

size

of

P

as

the

size

of

N

is

bounded

by

the

number

of

atoms

and

as

the

number

of

rules

in

the

headlist

lists

are

bounded

by

the

number

of

rules

in

P

Thus

the

time

complexity

of

the

algorithm

is

linear

in

the

size

of

P

as

the

time

complexity

of

dcl

is

linear

in

the

size

of

P

If

Dcl

-LRB-

P

B

-RRB-

has

been

computed

and

we

want

to

compute

Dcl

-LRB-

P

-RRB-

we

initialize

the

queue

with

the

rules

whose

bodies

are

entirely

contained

in

B

but

not

contained

in

The

second

algorithm

will

then

compute

the

new

closure

correctly

This

method

of

computing

the

deductive

closure

is

however

not

always

efficient

There

is

always

the

possibility

that

the

second

algorithm

will

remove

all

atoms

from

the

closure

before

calling

the

first

algorithm

In

this

case

it

would

be

better

to

only

call

the

first

algorithm

provided

that

the

counters

poslit

and

neglit

can

be

initialized

with

precomputed

values

Logic

Programs

with

Total

Well-Founded

Models

We

will

now

examine

the

computation

of

stable

models

of

sets

of

rules

with

a

total

well-founded

model

We

are

going

to

prove

that

-LRB-

P

;-RRB-

in

a

sense

computes

the

well-founded

model

of

P

We

begin

with

a

definition

and

a

theorem

that

together

characterize

the

set

UP

-LRB-

I

-RRB-

Definition

Let

P

be

a

logic

program

and

let

I

be

an

interpretation

Define

the

set

of

potentially

founded

atoms

in

P

with

respect

to

the

interpretation

I

as

Potfound

-LRB-

P

I

-RRB-

i

Potf

-LRB-

P

I

-RRB-

i

where

Potf

-LRB-

P

I

-RRB-

fa

j

a

is

the

head

of

a

rule

r

P

whose

body

contains

only

not-atoms

and

if

not

-LRB-

b

-RRB-

is

in

the

body

of

r

then

b

Ig

Potf

-LRB-

P

I

-RRB-

i

Potf

-LRB-

P

I

-RRB-

i

-LSB-

fa

j

a

is

the

head

of

a

rule

r

P

if

b

is

in

the

body

of

r

then

not

-LRB-

b

-RRB-

I

and

b

Potf

-LRB-

P

I

-RRB-

i

and

if

not

-LRB-

b

-RRB-

is

in

the

body

of

r

then

b

Ig

Example

Let

P

be

the

set

of

rules

fa

not

-LRB-

b

-RRB-

b

not

-LRB-

a

-RRB-

not

-LRB-

c

-RRB-

d

a

c

g

Then

Potf

-LRB-

P

fcg

-RRB-

fa

cg

and

Potf

-LRB-

P

fcg

-RRB-

i

fa

c

dg

for

i

Hence

Potfound

-LRB-

P

fcg

-RRB-

fa

c

dg

Theorem

-LRB-

-LSB-

-RSB-

-RRB-

Let

P

be

a

logic

program

and

let

I

be

an

interpretation

Then

UP

-LRB-

I

-RRB-

not

-LRB-

Atoms

-LRB-

P

-RRB-

Potfound

-LRB-

P

I

-RRB-

-RRB-

Consider

the

sequence

of

interpretations

defining

the

well-founded

model

of

the

program

P

Recall

that

and

for

all

k

Ik

TP

-LRB-

Ik

-RRB-

-LSB-

UP

-LRB-

Ik

-RRB-

Lemma

Let

P

be

a

logic

program

let

I

be

the

well-founded

model

of

P

and

let

B

-LRB-

P

;-RRB-

Then

the

well-founded

model

I

Dcl

-LRB-

P

B

-RRB-

-LSB-

not

-LRB-

Atoms

-LRB-

P

-RRB-

Dcl

-LRB-

P

not

-LRB-

NAnt

-LRB-

P

-RRB-

B

-RRB-

-RRB-

-RRB-

Proof

We

prove

by

induction

on

k

that

for

every

k

Ik

Dcl

-LRB-

P

B

-RRB-

-LSB-

not

-LRB-

Atoms

-LRB-

P

-RRB-

Dcl

-LRB-

P

not

-LRB-

NAnt

-LRB-

P

-RRB-

B

-RRB-

-RRB-

-RRB-

Note

that

Dcl

-LRB-

P

B

-RRB-

-LSB-

not

-LRB-

Atoms

-LRB-

P

-RRB-

Dcl

-LRB-

P

not

-LRB-

NAnt

-LRB-

P

-RRB-

B

-RRB-

-RRB-

-RRB-

as

Induction

hypothesis

assume

that

Ik

Dcl

-LRB-

P

B

-RRB-

-LSB-

not

-LRB-

Atoms

-LRB-

P

-RRB-

Dcl

-LRB-

P

not

-LRB-

NAnt

-LRB-

P

-RRB-

B

-RRB-

-RRB-

-RRB-

If

a

TP

-LRB-

Ik

-RRB-

then

there

exists

a

rule

r

such

that

the

body

of

r

is

in

Ik

If

b

is

in

the

body

of

r

then

b

Ik

and

by

the

induction

hypothesis

b

Dcl

-LRB-

P

B

-RRB-

If

not

-LRB-

b

-RRB-

is

in

the

body

of

r

then

not

-LRB-

b

-RRB-

Ik

and

by

the

induction

hypothesis

b

Dcl

-LRB-

P

not

-LRB-

NAnt

-LRB-

P

-RRB-

B

-RRB-

-RRB-

which

by

the

definition

of

implies

that

not

-LRB-

b

-RRB-

B

as

Dcl

-LRB-

P

B

-RRB-

Dcl

-LRB-

P

not

-LRB-

NAnt

-LRB-

P

-RRB-

B

-RRB-

-RRB-

Thus

the

body

of

r

is

in

Dcl

-LRB-

P

B

-RRB-

-LSB-

B

which

implies

a

Dcl

-LRB-

P

B

-RRB-

Hence

TP

-LRB-

Ik

-RRB-

Dcl

-LRB-

P

B

-RRB-

If

not

-LRB-

a

-RRB-

UP

-LRB-

Ik

-RRB-

then

a

Atoms

-LRB-

P

-RRB-

Potfound

-LRB-

P

Ik

-RRB-

by

Theorem

which

in

turn

implies

a

Potfound

-LRB-

P

Ik

-RRB-

If

b

Dcl

-LRB-

P

not

-LRB-

NAnt

-LRB-

P

-RRB-

B

-RRB-

-RRB-

then

b

Atoms

-LRB-

P

-RRB-

Dcl

-LRB-

P

not

-LRB-

NAnt

-LRB-

P

-RRB-

B

-RRB-

-RRB-

which

implies

not

-LRB-

b

-RRB-

Ik

On

the

other

hand

if

not

-LRB-

b

-RRB-

not

-LRB-

NAnt

-LRB-

P

-RRB-

B

-RRB-

then

b

B

which

implies

b

Dcl

-LRB-

P

B

-RRB-

Thus

b

Ik

by

the

induction

hypothesis

Now

define

and

Ai

Ai

-LSB-

fa

j

a

is

the

head

of

a

rule

r

P

if

b

is

in

the

body

of

r

then

b

A

and

if

not

-LRB-

b

-RRB-

is

in

the

body

of

r

then

not

-LRB-

b

-RRB-

not

-LRB-

NAnt

-LRB-

P

-RRB-

B

-RRB-

g.

Clearly

Dcl

-LRB-

P

not

-LRB-

NAnt

-LRB-

P

-RRB-

B

-RRB-

-RRB-

i

Ai

Furthermore

Potf

-LRB-

P

Ik

-RRB-

and

if

Ai

Potf

-LRB-

P

Ik

-RRB-

i

then

Ai

Potf

-LRB-

P

Ik

-RRB-

i

by

the

definition

of

Potf

-LRB-

P

Ik

-RRB-

i

Therefore

induction

shows

that

a

Dcl

-LRB-

P

not

-LRB-

NAnt

-LRB-

P

-RRB-

B

-RRB-

-RRB-

implies

a

Potfound

-LRB-

P

Ik

-RRB-

or

in

other

words

UP

-LRB-

Ik

-RRB-

not

-LRB-

Atoms

-LRB-

P

-RRB-

Dcl

-LRB-

P

not

-LRB-

NAnt

-LRB-

P

-RRB-

B

-RRB-

-RRB-

-RRB-

Hence

Ik

Dcl

-LRB-

P

B

-RRB-

-LSB-

not

-LRB-

Atoms

-LRB-

P

-RRB-

Dcl

-LRB-

P

not

-LRB-

NAnt

-LRB-

P

-RRB-

B

-RRB-

-RRB-

-RRB-

and

induction

proves

that

the

well-founded

model

I

k

Ik

is

a

subset

of

Dcl

-LRB-

P

B

-RRB-

-LSB-

not

-LRB-

Atoms

-LRB-

P

-RRB-

Dcl

-LRB-

P

not

-LRB-

NAnt

-LRB-

P

-RRB-

B

-RRB-

-RRB-

-RRB-

Lemma

Let

P

be

a

logic

program

let

I

be

the

well-founded

model

of

P

and

let

B

-LRB-

P

;-RRB-

Then

the

set

of

literals

Dcl

-LRB-

P

B

-RRB-

-LSB-

not

-LRB-

Atoms

-LRB-

P

-RRB-

Dcl

-LRB-

P

not

-LRB-

NAnt

-LRB-

P

-RRB-

B

-RRB-

-RRB-

-RRB-

I

Proof

We

prove

by

induction

on

i

that

for

every

i

Potf

-LRB-

P

I

-RRB-

i

Dcl

-LRB-

P

not

-LRB-

NAnt

-LRB-

P

-RRB-

B

-RRB-

-RRB-

If

a

Potf

-LRB-

P

I

-RRB-

then

there

is

a

rule

r

with

head

a

such

that

there

is

no

atoms

in

its

body

and

if

the

not-atom

not

-LRB-

b

-RRB-

is

in

its

body

then

b

I

which

implies

b

B

by

Lemma

which

in

turn

implies

not

-LRB-

b

-RRB-

not

-LRB-

NAnt

-LRB-

P

-RRB-

B

-RRB-

Thus

a

Dcl

-LRB-

P

not

-LRB-

NAnt

-LRB-

P

-RRB-

B

-RRB-

-RRB-

and

Potf

-LRB-

P

I

-RRB-

Dcl

-LRB-

P

not

-LRB-

NAnt

-LRB-

P

-RRB-

B

-RRB-

-RRB-

Induction

hypothesis

assume

that

Potf

-LRB-

P

I

-RRB-

i

Dcl

-LRB-

P

not

-LRB-

NAnt

-LRB-

P

-RRB-

B

-RRB-

-RRB-

If

a

Potf

-LRB-

P

I

-RRB-

i

then

there

is

a

rule

r

with

head

a

such

that

if

b

is

in

the

body

of

r

then

b

Potf

-LRB-

P

I

-RRB-

i

and

if

not

-LRB-

b

-RRB-

is

in

the

body

of

r

then

b

I

Now

b

Potf

-LRB-

P

I

-RRB-

i

implies

b

Dcl

-LRB-

P

not

-LRB-

NAnt

-LRB-

P

-RRB-

B

-RRB-

-RRB-

by

the

induction

hypothesis

and

b

I

implies

not

-LRB-

b

-RRB-

not

-LRB-

NAnt

-LRB-

P

-RRB-

B

-RRB-

as

in

the

case

i

Thus

a

Dcl

-LRB-

P

not

-LRB-

NAnt

-LRB-

P

-RRB-

B

-RRB-

-RRB-

and

therefore

Potf

-LRB-

P

I

-RRB-

i

Dcl

-LRB-

P

not

-LRB-

NAnt

-LRB-

P

-RRB-

B

-RRB-

-RRB-

Hence

induction

proves

that

Potfound

-LRB-

P

I

-RRB-

Dcl

-LRB-

P

not

-LRB-

NAnt

-LRB-

P

-RRB-

B

-RRB-

-RRB-

Therefore

not

-LRB-

Atoms

-LRB-

P

-RRB-

Dcl

-LRB-

P

not

-LRB-

NAnt

-LRB-

P

-RRB-

B

-RRB-

-RRB-

-RRB-

UP

-LRB-

I

-RRB-

by

the

definition

of

UP

-LRB-

I

-RRB-

We

prove

that

Dcl

-LRB-

P

B

-RRB-

is

a

subset

of

I

is

a

loop

invariant

of

the

function

-LRB-

P

;-RRB-

If

B

is

a

subset

of

I

then

Dcl

-LRB-

P

-RRB-

is

also

a

subset

of

I

as

I

is

closed

under

the

transformation

TP

-LRB-

I

-RRB-

Furthermore

not

-LRB-

NAnt

-LRB-

P

-RRB-

Dcl

-LRB-

P

not

-LRB-

NAnt

-LRB-

P

-RRB-

-RRB-

-RRB-

-RRB-

is

a

subset

of

not

-LRB-

Atoms

-LRB-

P

-RRB-

Dcl

-LRB-

P

not

-LRB-

NAnt

-LRB-

P

-RRB-

B

-RRB-

-RRB-

-RRB-

which

is

a

subset

of

UP

-LRB-

I

-RRB-

Thus

B

I

as

I

TP

-LRB-

I

-RRB-

-LSB-

UP

-LRB-

I

-RRB-

Consequently

Dcl

-LRB-

P

B

-RRB-

I

Hence

if

B

-LRB-

P

;-RRB-

then

the

inclusion

Dcl

-LRB-

P

B

-RRB-

-LSB-

not

-LRB-

Atoms

-LRB-

P

-RRB-

Dcl

-LRB-

P

not

-LRB-

NAnt

-LRB-

P

-RRB-

B

-RRB-

-RRB-

-RRB-

I

holds

Theorem

Let

P

be

a

logic

program

let

I

be

the

well-founded

model

of

P

and

let

B

-LRB-

P

;-RRB-

Then

the

well-founded

model

I

Dcl

-LRB-

P

B

-RRB-

-LSB-

not

-LRB-

Atoms

-LRB-

P

-RRB-

Dcl

-LRB-

P

not

-LRB-

NAnt

-LRB-

P

-RRB-

B

-RRB-

-RRB-

-RRB-

Proof

Immediate

from

lemmas

and

Theorem

Let

I

be

the

total

well-founded

model

of

the

logic

program

P

Let

the

set

B

-LRB-

P

;-RRB-

Then

the

program

P

has

a

unique

stable

model

Dcl

-LRB-

P

B

-RRB-

Proof

Assume

there

is

an

atom

a

NAnt

-LRB-

P

-RRB-

such

that

a

is

not

covered

by

B.

Thus

a

Dcl

-LRB-

P

B

-RRB-

and

a

Dcl

-LRB-

P

not

-LRB-

NAnt

-LRB-

P

-RRB-

B

-RRB-

-RRB-

by

the

definition

of

B

But

a

Dcl

-LRB-

P

not

-LRB-

NAnt

-LRB-

P

-RRB-

B

-RRB-

-RRB-

implies

not

-LRB-

a

-RRB-

not

-LRB-

Atoms

-LRB-

P

-RRB-

Dcl

-LRB-

P

not

-LRB-

NAnt

-LRB-

P

-RRB-

B

-RRB-

-RRB-

-RRB-

which

by

Lemma

implies

that

a

is

not

covered

by

I

This

is

a

contradiction

as

I

is

a

total

well-founded

model

of

P

Hence

NAnt

-LRB-

P

-RRB-

is

covered

by

B.

Now

for

a

NAnt

-LRB-

P

-RRB-

not

-LRB-

a

-RRB-

B

implies

a

B

which

implies

a

Dcl

-LRB-

P

B

-RRB-

Furthermore

a

Dcl

-LRB-

P

B

-RRB-

implies

a

B

which

in

turn

implies

not

-LRB-

a

-RRB-

B

Thus

the

set

B

is

P

full

and

Dcl

-LRB-

P

B

-RRB-

is

a

stable

model

of

P

Finally

is

the

only

stable

model

of

P

as

the

fact

that

-LRB-

P

;-RRB-

covers

NAnt

-LRB-

P

-RRB-

implies

that

-LRB-

P

;-RRB-

can

not

invoke

itself

recursively

and

thus

passes

at

most

one

stable

model

to

test

and

as

Theorem

guarantees

that

the

function

is

complete

Corollary

The

function

computes

the

full

set

of

all

effectively

stratified

-LSB-

-RSB-

weakly

stratified

-LSB-

-RSB-

locally

stratified

-LSB-

-RSB-

and

stratified

-LSB-

-RSB-

logic

programs

Proof

The

classes

of

stratified

locally

startified

and

weakly

stratified

logic

programs

are

included

in

the

class

of

effectively

stratified

logic

programs

and

a

logic

program

is

effectively

stratified

if

and

only

if

it

has

a

total

well-founded

model

-LSB-

-RSB-

We

now

consider

the

complexity

of

the

computation

in

-LRB-

P

;-RRB-

The

initialization

of

the

arrays

used

when

computing

the

deductive

closures

Dcl

-LRB-

P

B

-RRB-

and

Dcl

-LRB-

P

not

-LRB-

NAnt

-LRB-

P

-RRB-

B

-RRB-

-RRB-

can

be

done

in

linear

time

As

the

deductive

closure

Dcl

-LRB-

P

B

-RRB-

grows

during

the

computation

and

as

each

atom

in

NAnt

-LRB-

P

-RRB-

only

can

be

added

once

to

B

the

total

work

done

computing

Dcl

-LRB-

P

B

-RRB-

is

linear

in

the

size

of

P

However

the

complexity

of

calculating

Dcl

-LRB-

P

not

-LRB-

NAnt

-LRB-

P

-RRB-

B

-RRB-

-RRB-

is

linear

in

the

size

of

P

and

as

the

number

of

times

it

is

calculated

is

bounded

by

the

size

of

NAnt

-LRB-

P

-RRB-

we

see

that

the

total

complexity

of

computing

the

stable

model

of

a

set

of

rules

with

a

total

well-founded

model

is

at

most

quadratic

in

the

size

of

the

set

of

rules

More

precisely

if

the

size

of

the

set

of

rules

is

p

and

the

number

of

atoms

in

NAnt

-LRB-

P

-RRB-

is

n

then

the

time

complexity

of

is

bounded

by

O

-LRB-

np

-RRB-

-LRB-

-RRB-

It

is

possible

to

compute

the

unique

stable

model

of

a

stratified

set

of

rules

in

linear

time

with

respect

to

the

size

of

the

set

-LSB-

-RSB-

The

function

fails

to

do

this

as

the

computations

of

Dcl

-LRB-

P

not

-LRB-

NAnt

-LRB-

P

-RRB-

B

-RRB-

-RRB-

might

add

and

remove

an

atom

from

the

deductive

closure

several

times

Complexity

Analysis

We

give

a

naive

upper

bound

on

the

time

complexity

of

the

improved

algorithm

Let

P

be

a

set

of

rules

let

the

size

of

P

be

p

and

let

the

number

of

atoms

in

NAnt

-LRB-

P

-RRB-

be

n.

Disregarding

the

function

we

see

that

have

to

add

literals

to

the

set

B

at

most

ni

times

The

function

must

remove

them

equally

many

times

As

the

time

complexity

of

is

O

-LRB-

np

-RRB-

we

see

that

the

time

complexity

of

is

O

-LRB-

-RRB-

-LRB-

-RRB-

We

continue

with

an

example

showing

that

the

algorithm

has

worst-case

exponential

time

complexity

Example

Let

P

be

the

set

of

rules

fh

h

not

-LRB-

-RRB-

not

-LRB-

an

-RRB-

h

an

h

not

-LRB-

h

-RRB-

h

an

hg

If

the

set

B

does

not

contain

h

not

-LRB-

h

-RRB-

nor

all

of

an

then

Dcl

-LRB-

P

B

-RRB-

and

Dcl

-LRB-

P

not

-LRB-

NAnt

-LRB-

P

-RRB-

B

-RRB-

-RRB-

Atoms

-LRB-

P

-RRB-

Consequently

-LRB-

P

B

-RRB-

B

and

-LRB-

P

B

-RRB-

returns

false

If

chooses

the

atoms

an

before

choosing

the

atom

h

then

the

algorithm

adds

literals

to

the

set

B

by

choosing

or

backtracking

a

total

of

n

i

times

When

an

atom

is

chosen

in

it

is

first

added

to

B

and

then

upon

backtracking

added

to

B

Thus

the

first

atom

chosen

is

added

two

times

to

B

The

second

is

added

two

times

to

B

for

each

time

the

first

atom

is

added

to

B.

Similarly

every

atom

is

added

two

times

to

B

for

each

time

the

previously

chosen

atom

is

added

to

B

This

is

the

reason

for

the

sum

n

i

However

since

the

atom

h

is

added

to

B

by

the

rule

h

an

when

all

of

the

atoms

an

are

in

B

the

literal

not

-LRB-

h

-RRB-

is

not

added

to

B

prior

to

the

addition

of

h

to

B

Hence

we

have

to

decrease

the

sum

by

one

Backtracking

Based

on

the

Dependency

Graph

The

algorithm

backtracks

in

a

simple

manner

the

atoms

added

last

to

B

are

removed

first

This

straightforward

approach

has

however

a

weakness

The

algorithm

may

repeatedly

backtrack

to

points

that

do

not

influence

the

subsequent

computations

Identical

computations

will

then

be

performed

several

times

We

examine

the

use

of

the

dependency

graph

as

a

means

to

improve

the

way

the

algorithm

backtracks

In

Section

we

define

the

dependency

graph

and

in

Section

we

examine

a

backtracking

strategy

based

on

the

dependency

graph

The

Dependency

Graph

Definition

A

dependency

graph

D

of

a

set

of

rules

P

is

a

pair

-LRB-

A

D

-RRB-

where

A

Atoms

-LRB-

P

-RRB-

and

D

is

a

subset

of

A?A

for

which

the

following

holds

The

pair

-LRB-

a

b

-RRB-

D

if

and

only

if

there

exists

a

rule

r

P

such

that

a

is

in

the

head

of

r

and

b

is

in

the

body

of

r

We

call

an

edge

-LRB-

a

b

-RRB-

in

the

dependency

graph

a

not-edge

if

there

is

a

rule

r

such

that

a

is

in

the

head

of

r

and

not

-LRB-

b

-RRB-

is

in

the

body

of

r.

Definition

An

atom

a

depends

on

another

atom

b

in

a

dependency

graph

D

if

there

is

a

directed

path

from

a

to

b

in

the

graph

Definition

A

strongly

connected

component

A

is

dependent

on

another

strongly

connected

component

B

in

a

dependency

graph

D

if

there

is

a

directed

path

from

some

atom

in

A

to

any

atom

in

B

in

the

graph

We

also

say

that

a

literal

depends

on

another

literal

if

the

atom

appearing

in

the

first

literal

is

dependent

on

the

atom

in

the

second

literal

Backtracking

and

the

Dependency

Graph

We

demonstrate

some

problems

one

encounters

if

one

wants

to

use

the

dependency

graph

as

an

aid

in

backtracking

Think

of

a

set

of

rules

P

Let

a

and

b

be

two

atoms

in

the

set

NAnt

-LRB-

P

-RRB-

such

that

a

is

dependent

on

b

but

b

is

not

dependent

on

a.

Let

B

be

a

set

of

literals

and

consider

the

deductive

closure

Dcl

-LRB-

P

B

-RRB-

Now

b

might

be

in

the

deductive

closure

or

it

might

not

This

fact

is

however

not

dependent

on

whether

a

is

covered

by

B

or

not

One

is

thus

led

to

believe

that

the

dependency

graph

can

be

used

when

improving

the

way

the

algorithm

backtracks

The

idea

would

be

to

compute

the

strongly

connected

components

of

the

dependency

graph

and

choose

the

atoms

from

one

component

at

a

time

The

algorithm

would

for

instance

choose

a

strongly

connected

component

that

is

not

dependent

on

any

component

that

is

not

fully

covered

by

the

partially

constructed

P

full

set

If

the

algorithm

then

had

to

backtrack

it

would

backtrack

normally

inside

the

strongly

connected

components

but

use

the

dependency

graph

when

choosing

the

next

component

to

backtrack

in

This

would

hopefully

help

the

algorithm

to

avoid

recomputing

parts

of

the

P

full

set

Definition

Let

P

be

a

set

of

rules

let

B

be

a

set

of

atoms

Define

RulesP

-LRB-

B

-RRB-

fr

P

j

the

head

of

r

is

in

Bg

Consider

a

set

of

rules

P

such

that

the

dependency

graph

consists

of

two

separate

strongly

connected

components

A

and

B.

Assume

that

RulesP

-LRB-

A

-RRB-

has

two

stable

models

and

that

RulesP

-LRB-

B

-RRB-

has

no

stable

model

If

the

algorithm

starts

with

computing

one

stable

model

of

RulesP

-LRB-

A

-RRB-

and

then

fails

to

compute

a

stable

model

of

RulesP

-LRB-

B

-RRB-

it

is

clear

that

P

has

no

stable

models

The

subsequent

backtracking

and

computation

of

the

second

stable

model

of

RulesP

-LRB-

A

-RRB-

are

therefore

unnecessary

On

the

other

hand

assume

that

RulesP

-LRB-

B

-RRB-

also

has

two

stable

models

Assume

the

algorithm

computes

one

stable

model

of

RulesP

-LRB-

A

-RRB-

and

then

computes

the

first

and

second

stable

models

of

RulesP

-LRB-

B

-RRB-

and

finally

fails

to

compute

a

third

stable

model

of

RulesP

-LRB-

B

-RRB-

Then

the

algorithm

has

to

backtrack

and

compute

the

second

stable

model

of

RulesP

-LRB-

A

-RRB-

in

order

to

find

all

stable

models

of

P

The

above

example

makes

it

evident

that

it

is

not

possible

to

implement

a

simple

backtracking

method

that

only

exploits

the

way

the

strongly

connected

components

in

the

dependency

graph

are

connected

The

problem

lies

in

guaranteeing

completeness

of

the

algorithm

If

the

algorithm

is

not

complete

it

might

not

compute

all

stable

models

thereby

failing

to

compute

the

single

stable

model

of

some

sets

of

rules

Examples

of

sets

of

rules

showing

this

are

however

dependent

on

the

details

of

the

backtracking

strategy

The

previous

example

demonstrates

the

possibility

of

using

the

dependency

graph

for

early

detection

of

sets

of

rules

with

no

stable

models

A

backtracking

scheme

that

is

only

based

on

the

strongly

connected

components

in

the

dependency

graph

presumes

that

the

heuristic

choosing

atoms

not

covered

by

NAnt

-LRB-

P

-RRB-

chooses

all

atoms

in

one

component

before

choosing

any

in

another

component

Consider

a

set

of

rules

P

with

two

strongly

connected

components

A

and

B

such

that

A

is

dependent

on

B

but

B

is

not

dependent

on

A

As

the

backtracking

scheme

uses

the

dependency

graph

one

might

think

that

it

is

beneficial

to

choose

the

atoms

in

B

before

the

atoms

in

A

But

this

is

not

always

the

case

To

mention

a

simple

example

showing

this

assume

that

B

has

a

lot

of

stable

models

and

that

A

has

Now

if

the

algorithm

begins

with

computing

a

stable

model

of

B

it

will

have

to

compute

all

stable

models

of

B

before

arriving

at

the

conclusion

that

P

has

no

stable

models

If

the

algorithm

instead

tries

to

compute

a

stable

model

of

A

it

might

very

well

be

the

case

that

it

fails

immediately

thereby

avoiding

a

huge

amount

of

computation

Handling

Rules

with

Variables

In

this

section

we

examine

a

class

of

logic

programs

with

variables

that

are

easily

grounded

i.e.

converted

to

propositional

logic

programs

without

variables

The

reason

for

allowing

the

use

of

variables

is

to

simplify

the

writing

of

logic

programs

The

reason

for

limiting

this

use

is

to

avoid

the

floundering

problems

of

ungrounded

negative

literals

-LSB-

-RSB-

If

the

use

of

variables

is

suitably

limited

it

is

also

possible

to

avoid

the

generation

of

grounded

rules

that

do

not

influence

the

set

of

stable

models

of

the

program

In

Section

we

describe

logic

programs

with

variables

and

how

they

are

grounded

In

Section

we

describe

the

class

of

range

restricted

programs

and

how

they

can

be

grounded

Ground

Logic

Programs

We

begin

by

defining

logic

programs

with

variables

A

logic

program

with

variables

is

a

set

of

rules

with

variables

A

rule

with

variables

is

a

rule

that

contain

apart

from

atoms

also

predicates

A

predicate

is

of

the

form

p

-LRB-

Xn

-RRB-

where

Xn

are

variables

or

atoms

A

not-predicate

is

of

course

of

the

form

not

-LRB-

p

-LRB-

Xn

-RRB-

-RRB-

A

substitution

is

a

mapping

from

the

variables

in

the

logic

program

to

the

atoms

in

the

logic

program

A

substitution

is

applied

to

a

rule

by

replacing

every

variable

in

the

rule

by

the

atom

it

is

mapped

to

A

program

is

grounded

by

replacing

each

rule

r

by

the

rules

obtained

by

applying

all

possible

substitutions

of

the

program

to

the

rule

r.

Example

Let

P

be

the

logic

program

fp

-LRB-

X

-RRB-

q

-LRB-

X

Y

-RRB-

r

-LRB-

a

-RRB-

r

-LRB-

b

-RRB-

g

where

variables

are

denoted

by

uppercase

letters

The

corresponding

ground

program

is

fp

-LRB-

a

-RRB-

q

-LRB-

a

a

-RRB-

p

-LRB-

a

-RRB-

q

-LRB-

a

b

-RRB-

p

-LRB-

b

-RRB-

q

-LRB-

b

a

-RRB-

p

-LRB-

b

-RRB-

q

-LRB-

b

b

-RRB-

r

-LRB-

a

-RRB-

r

-LRB-

b

-RRB-

g

The

predicates

of

a

ground

logic

program

can

be

considered

as

atoms

This

allows

us

to

interpret

a

ground

logic

program

as

an

ordinary

logic

program

without

predicates

Range

Restricted

Logic

Programs

When

a

logic

program

is

grounded

a

lot

of

rules

are

typically

generated

that

may

be

removed

from

the

ground

program

without

changing

the

set

of

stable

models

of

the

program

By

limiting

the

use

of

variables

in

the

rules

it

is

possible

to

generate

a

smaller

program

that

has

the

same

stable

models

as

the

ground

program

We

examine

the

class

of

range

restricted

logic

programs

A

rule

containing

variables

is

range

restricted

if

every

variable

that

occurs

in

a

predicate

in

the

head

or

in

a

not-predicate

in

the

body

of

the

rule

also

occurs

in

a

predicate

in

the

body

of

the

rule

A

logic

program

is

range

restricted

if

every

rule

in

the

program

is

range

restricted

Example

The

rules

a

-LRB-

X

-RRB-

b

-LRB-

X

-RRB-

not

-LRB-

c

-RRB-

and

p

q

-LRB-

X

-RRB-

not

-LRB-

r

-LRB-

X

-RRB-

-RRB-

are

range

restricted

but

the

rules

a

-LRB-

X

-RRB-

b

-LRB-

Y

-RRB-

and

p

not

-LRB-

q

-LRB-

X

-RRB-

-RRB-

are

not

All

rules

in

a

ground

logic

program

do

not

affect

the

set

of

stable

models

of

the

program

Proposition

Let

P

be

a

ground

logic

program

Then

any

rule

in

P

that

has

its

head

or

an

atom

in

its

body

outside

of

Dcl

-LRB-

P

not

-LRB-

NAnt

-LRB-

P

-RRB-

-RRB-

-RRB-

can

be

removed

from

P

without

affecting

the

set

of

stable

models

of

P

Proof

For

any

stable

model

of

P

Dcl

-LRB-

P

not

-LRB-

NAnt

-LRB-

P

-RRB-

-RRB-

-RRB-

Dcl

-LRB-

P

not

-LRB-

NAnt

-LRB-

P

-RRB-

-RRB-

-RRB-

In

Figure

we

present

an

algorithm

that

directly

computes

the

smaller

ground

program

from

the

ungrounded

one

The

algorithm

ground

-LRB-

P

-RRB-

works

by

computing

the

deductive

closure

of

P

while

disregarding

the

not-atoms

and

not-predicates

in

the

rules

When

a

ground

predicate

is

found

to

belong

to

the

closure

it

is

put

in

the

set

A

The

predicates

in

the

set

A

are

then

used

to

ground

the

rules

in

P

When

a

predicate

has

been

used

it

is

moved

from

the

set

A

to

the

set

B

to

avoid

using

the

same

predicate

twice

The

ground

rules

in

P

are

moved

to

the

set

as

they

are

created

When

the

set

A

is

empty

it

is

not

possible

to

ground

any

more

rules

and

the

algorithm

returns

the

grounded

program

Example

The

program

P

fp

-LRB-

X

-RRB-

q

-LRB-

X

Y

-RRB-

r

-LRB-

a

-RRB-

r

-LRB-

b

-RRB-

g

in

the

example

in

the

previous

section

is

range

restricted

The

ground

program

contains

five

rules

but

the

smaller

program

ground

-LRB-

P

-RRB-

does

not

contain

any

rules

Both

the

empty

program

and

the

ground

program

have

one

stable

model

which

is

empty

function

ground

-LRB-

P

-RRB-

fr

P

j

the

body

of

r

does

not

contain

any

positive

predicatesg

P

P

A

fa

j

there

exists

a

rule

r

whose

head

is

ag

B

while

A

is

not

empty

do

Let

a

A

A

A

fag

B

B

-LSB-

fag

for

each

rule

r

in

P

do

for

every

rule

resulting

from

applying

a

substitution

to

r

such

that

a

is

in

the

head

or

body

of

the

rule

do

if

is

grounded

and

all

positive

literals

in

the

body

of

is

in

A

-LSB-

B

then

-LSB-

if

the

head

of

is

not

in

B

then

A

A

-LSB-

fthe

head

of

endif

else

P

P

-LSB-

endif

end

for

end

for

end

while

return

Figure

The

Procedure

for

Grounding

Range

Restricted

Programs

Even

if

the

algorithm

ground

can

reduce

the

number

of

rules

in

the

ground

program

the

program

might

still

grow

substantially

when

grounded

For

example

the

program

P

fp

-LRB-

Xm

-RRB-

q

-LRB-

-RRB-

q

-LRB-

Xm

-RRB-

q

-LRB-

-RRB-

q

-LRB-

an

-RRB-

g

contains

n

rules

and

the

program

ground

-LRB-

P

-RRB-

contains

n

nm

rules

Implementation

In

this

section

we

describe

the

implementation

specific

choices

we

made

to

ensure

an

efficient

implementation

of

the

algorithm

In

particular

this

concerns

the

choice

of

data

structures

the

implementation

of

the

deductive

closures

the

detection

of

conflicts

and

the

way

backtracking

is

handled

As

the

algorithm

contains

a

non-deterministic

choice

we

describe

a

heuristic

for

making

a

hopefully

good

one

In

Section

we

describe

the

data

structures

In

Section

we

describe

the

implementation

of

the

two

deductive

closures

needed

and

how

conflicts

are

detected

Finally

in

Section

we

describe

how

we

handle

backtracking

and

in

Section

we

describe

the

heuristic

Data

Structures

The

algorithm

-LRB-

P

B

-RRB-

relies

heavily

on

the

use

of

sets

It

is

therefore

necessary

to

implement

these

efficiently

A

closer

inspection

of

the

algorithm

reveals

that

the

set

operations

needed

are

addition

or

removal

of

an

element

from

a

set

and

membership

test

In

addition

we

must

be

able

to

determine

if

the

set

NAnt

-LRB-

P

-RRB-

is

covered

by

the

sets

B

and

B

As

we

know

all

possible

members

of

the

sets

we

implement

the

sets

as

arrays

The

elements

are

represented

by

indices

into

the

arrays

and

the

value

of

the

array

at

index

n

tells

if

element

n

belongs

to

the

set

or

not

If

the

value

is

zero

the

element

does

not

belong

to

the

set

and

if

the

value

is

one

it

does

The

particular

problem

of

determining

if

NAnt

-LRB-

P

-RRB-

is

covered

by

the

sets

B

and

B

is

solved

by

introducing

a

variable

that

keeps

count

of

the

number

of

elements

in

NAnt

-LRB-

P

-RRB-

not

covered

by

B

and

B

The

variable

is

initialized

with

the

number

of

elements

in

NAnt

-LRB-

P

-RRB-

not

covered

by

B.

Then

the

variable

is

decremented

or

incremented

each

time

an

element

is

added

to

or

removed

from

the

sets

B

and

B

When

the

variable

has

value

zero

NAnt

-LRB-

P

-RRB-

is

covered

Deductive

Closures

and

Conflicts

The

computation

of

the

deductive

closures

is

to

a

great

extent

described

in

Section

We

fill

in

some

details

regarding

the

implementation

of

the

deductive

closures

We

also

describe

how

to

detect

conflicts

as

early

as

possible

The

rules

are

represented

as

indices

into

an

array

To

each

rule

we

must

associate

the

head

of

the

rule

the

rules

in

whose

bodies

the

head

appears

and

the

two

counters

poslit

and

neglit

The

value

of

an

element

of

the

array

a

rule

points

into

therefore

contains

the

head

of

the

rule

and

the

counters

poslit

and

neglit

But

as

the

number

of

rules

a

particular

head

appears

in

varies

for

different

heads

we

store

the

rules

which

has

a

particular

head

in

their

bodies

consecutively

in

a

different

array

The

beginning

and

the

end

of

this

segment

of

the

array

is

then

stored

in

the

same

element

as

the

head

We

want

to

reduce

the

amount

of

computation

as

much

as

possible

Therefore

we

make

the

conflict

test

each

time

an

atom

or

not-atom

is

added

to

B

in

the

function

This

incurs

very

little

overhead

as

the

deductive

closures

needed

already

are

calculated

when

the

literal

is

added

to

the

set

B

and

as

we

only

have

to

test

the

literal

being

added

Thus

we

gain

the

ability

to

interrupt

the

computations

of

the

deductive

closures

and

the

function

as

soon

as

a

conflict

is

detected

Backtracking

The

algorithm

handles

backtracking

by

recursively

passing

along

the

changing

set

B

as

one

of

its

parameters

This

practice

would

in

an

implementation

require

a

lot

of

memory

To

avoid

this

inconvenience

we

use

iteration

instead

of

recursion

and

use

a

stack

to

record

the

information

necessary

for

backtracking

When

a

literal

is

added

to

B

or

B

we

push

it

onto

the

stack

If

the

literal

is

added

to

B

as

the

result

of

a

choice

in

NAnt

-LRB-

P

-RRB-

we

mark

it

The

state

of

the

arrays

used

when

computing

the

deductive

closures

must

be

reset

appropriately

when

backtracking

We

avoid

storing

any

backtracking

information

pertaining

to

these

arrays

Instead

we

take

advantage

of

our

ability

to

compute

both

growing

and

diminishing

deductive

closures

When

a

literal

is

removed

from

the

stack

during

backtracking

we

modify

the

queues

used

by

the

deductive

closure

functions

accordingly

Then

we

call

the

opposite

deductive

closure

function

to

what

is

normally

called

to

reset

the

state

of

the

arrays

For

example

when

we

compute

Dcl

-LRB-

P

B

-RRB-

we

call

the

function

computing

the

growing

deductive

closure

During

backtracking

we

therefore

call

the

function

computing

the

diminishing

deductive

closure

When

backtracking

we

remove

literals

from

the

stack

until

a

marked

literal

has

been

removed

This

not-atom

is

then

added

as

an

atom

to

the

set

B

after

the

states

of

the

deductive

closures

have

been

reset

A

Heuristic

The

order

the

atoms

are

picked

from

NAnt

-LRB-

P

-RRB-

drastically

affect

the

performance

of

the

stable

models

algorithm

If

only

atoms

that

appear

in

a

specific

stable

model

are

chosen

the

algorithm

finds

the

first

stable

model

without

backtracking

It

is

therefore

important

to

use

a

good

heuristic

when

selecting

atoms

The

heuristic

selects

a

not-atom

from

the

body

of

an

applicable

rule

with

the

least

number

of

not-atoms

not

in

B

in

its

body

An

applicable

rule

is

a

rule

that

might

change

the

set

B

i.e.

the

head

of

an

applicable

rule

is

not

in

Dcl

-LRB-

P

B

-RRB-

all

atoms

in

the

body

of

an

applicable

rule

are

in

Dcl

-LRB-

P

B

-RRB-

and

no

not-atom

in

the

body

of

an

applicable

rule

covers

any

atom

in

B

Thus

the

heuristic

tries

to

select

the

atom

that

has

the

greatest

possibility

of

changing

B

thereby

helping

the

algorithm

to

find

conflicts

or

full

sets

with

a

minimal

number

of

choices

If

there

are

no

applicable

rules

left

the

atoms

in

NAnt

-LRB-

P

-RRB-

not

covered

by

B

can

not

affect

the

deductive

closures

Therefore

they

are

immediately

added

as

not-atoms

to

B

We

give

an

explanation

of

this

Adding

not-atoms

to

B

does

not

change

the

set

not

-LRB-

NAnt

-LRB-

P

-RRB-

B

-RRB-

nor

the

deductive

closure

Dcl

-LRB-

P

not

-LRB-

NAnt

-LRB-

P

-RRB-

B

-RRB-

-RRB-

Adding

not-atoms

to

B

does

not

change

Dcl

-LRB-

P

B

-RRB-

either

as

there

are

no

applicable

rules

left

Let

denote

the

set

B

to

which

all

atoms

in

NAnt

-LRB-

P

-RRB-

not

covered

by

B

have

been

added

as

not-atoms

Then

Dcl

-LRB-

P

B

-RRB-

Dcl

-LRB-

P

-RRB-

and

Dcl

-LRB-

P

not

-LRB-

NAnt

-LRB-

P

-RRB-

B

-RRB-

-RRB-

Dcl

-LRB-

P

not

-LRB-

NAnt

-LRB-

P

-RRB-

-RRB-

-RRB-

which

guarantees

that

does

not

change

Now

-LRB-

P

B

-RRB-

returns

false

as

otherwise

the

not-atoms

would

not

be

added

to

B

by

the

heuristic

Thus

-LRB-

P

-RRB-

also

returns

false

and

is

a

P

full

set

But

this

means

that

Dcl

-LRB-

P

-RRB-

Dcl

-LRB-

P

not

-LRB-

NAnt

-LRB-

P

-RRB-

-RRB-

-RRB-

and

consequently

that

Dcl

-LRB-

P

B

-RRB-

Dcl

-LRB-

P

not

-LRB-

NAnt

-LRB-

P

-RRB-

B

-RRB-

-RRB-

Hence

Lemma

implies

that

there

is

only

one

stable

model

agreeing

with

B

which

in

turn

implies

that

no

atom

in

NAnt

-LRB-

P

-RRB-

can

be

added

to

B

without

introducing

a

conflict

The

addition

of

the

not-atoms

to

B

is

therefore

justified

Example

Consider

the

rule

a

b

not

-LRB-

c

-RRB-

in

the

set

of

rules

P

If

the

atom

b

is

not

in

Dcl

-LRB-

P

B

-RRB-

then

adding

not

-LRB-

c

-RRB-

to

B

will

not

affect

whether

a

belongs

to

Dcl

-LRB-

P

B

-RRB-

or

not

On

the

other

hand

if

b

is

in

Dcl

-LRB-

P

B

-RRB-

and

a

is

not

then

adding

not

-LRB-

c

-RRB-

to

B

will

make

a

a

member

of

Dcl

-LRB-

P

B

-RRB-

The

heuristic

can

be

implemented

by

examining

the

rules

that

have

not-atoms

in

their

bodies

that

are

not

covered

by

B

The

counter

poslit

provides

an

easy

way

of

detecting

whether

all

atoms

in

the

body

of

a

rule

are

in

Dcl

-LRB-

P

B

-RRB-

Hence

computing

the

heuristic

is

linear

in

the

number

of

rules

in

the

logic

program

in

the

worst

case

We

examine

the

example

in

Section

that

was

used

to

show

that

the

algorithm

can

exhibit

worst-case

exponential

time

complexity

The

set

of

rules

P

is

fh

h

not

-LRB-

-RRB-

not

-LRB-

an

-RRB-

h

an

h

not

-LRB-

h

-RRB-

h

an

hg

Regardless

of

the

value

of

n

the

heuristic

will

first

choose

to

add

not

-LRB-

h

-RRB-

to

the

set

B

The

rule

h

not

-LRB-

h

-RRB-

immediately

gives

rise

to

a

conflict

Then

the

literal

not

-LRB-

h

-RRB-

is

removed

from

the

set

B

and

the

atom

h

is

added

Now

all

the

atoms

an

are

added

to

B

a

conflict

is

found

and

the

algorithm

returns

false

Hence

the

algorithm

no

longer

needs

exponential

time

to

deduce

that

P

has

no

stable

models

Comparison

with

other

Algorithms

We

compare

the

algorithm

described

in

this

work

with

some

other

algorithms

for

computing

stable

models

of

logic

programs

In

Section

we

examine

the

branch

and

bound

algorithm

of

-LSB-

-RSB-

in

Section

the

SLG

system

of

-LSB-

-RSB-

and

in

Section

the

mixed

integer

programming

methods

of

-LSB-

-RSB-

Finally

we

present

the

comparison

in

Section

Branch

and

Bound

The

branch

and

bound

algorithm

of

-LSB-

-RSB-

computes

the

stable

models

of

ground

logic

programs

The

algorithm

works

in

two

stages

In

the

first

stage

the

logic

program

is

simplified

while

the

algorithm

computes

the

well-founded

semantics

of

the

program

In

the

second

stage

the

branch

and

bound

stage

all

stable

models

of

the

logic

program

are

constructed

The

algorithm

computes

the

stable

models

by

generating

smaller

and

smaller

programs

from

programs

already

generated

starting

with

the

simplified

program

Two

new

programs

are

generated

from

a

program

P

by

choosing

an

atom

whose

truth

value

is

unknown

according

to

the

well-founded

semantics

and

then

simplifying

the

program

P

assuming

that

the

atom

belongs

or

do

not

belong

to

the

stable

model

under

construction

The

algorithm

prunes

the

search

space

by

disregarding

every

newly

created

program

that

is

inconsistent

or

whose

partial

stable

model

is

a

superset

of

a

stable

model

already

found

Although

the

algorithm

prunes

the

search

space

effectively

it

must

keep

all

constructed

stable

models

as

well

as

all

partially

constructed

stable

models

in

memory

This

indicates

that

the

algorithm

will

necessarily

perform

badly

if

the

number

of

stable

models

is

large

Even

if

the

number

of

stable

models

is

small

there

is

no

guarantee

that

the

number

of

partially

constructed

models

also

stays

small

The

algorithm

is

therefore

not

suitable

if

one

wants

to

find

one

or

maybe

a

few

stable

models

of

a

ground

logic

program

The

SLG

System

The

SLG

system

-LSB-

-RSB-

supports

goal-oriented

query

evaluation

of

logic

programs

under

the

well-founded

semantics

The

system

simplifies

the

logic

program

during

query

evaluation

producing

a

residual

program

The

SLG

System

can

then

compute

the

stable

models

of

the

residual

program

using

an

assume-and-reduce

algorithm

provided

that

all

negative

literals

in

the

residual

program

are

ground

The

assume-and-reduce

algorithm

constructs

the

stable

models

of

a

logic

program

in

a

similar

fashion

to

the

branch

and

bound

algorithm

The

assumeand-reduce

algorithm

differs

in

that

it

constructs

one

model

at

a

time

finding

all

models

by

backtracking

and

in

that

it

does

not

enlarge

the

partially

constructed

stable

model

using

the

well-founded

semantics

The

algorithm

alternately

assumes

the

truth

values

of

atoms

in

the

program

and

reduces

the

logic

program

The

reduction

of

the

program

that

at

the

same

time

enlarges

the

model

under

construction

does

not

simplify

the

program

as

much

as

is

possible

if

one

uses

the

well-founded

semantics

This

means

that

the

algorithm

does

not

prune

the

search

space

as

effectively

as

the

branch

and

bound

algorithm

However

the

assume-and-reduce

algorithm

can

derive

whether

an

atom

belongs

to

the

stable

model

or

not

using

backward

propagation

in

some

specialized

circumstances

This

improves

the

pruning

of

the

search

space

slightly

The

Mixed

Integer

Programming

Approach

The

mixed

integer

programming

methods

of

-LSB-

-RSB-

computes

the

stable

models

of

a

logic

program

by

translating

the

program

into

an

integer

linear

program

The

integer

linear

program

is

then

used

to

compute

all

minimal

models

of

the

logic

program

The

minimal

models

are

then

tested

by

another

integer

linear

program

to

exclude

those

models

that

are

not

stable

As

the

number

of

minimal

models

of

a

logic

program

can

be

very

large

compared

to

the

number

of

stable

models

of

the

same

program

we

conclude

that

this

approach

is

very

inefficient

for

that

class

of

logic

programs

Comparison

We

compare

the

branch

and

bound

algorithm

the

SLG

system

and

the

mixed

integer

programming

methods

with

the

improved

algorithm

presented

in

this

work

The

performance

of

the

branch

and

bound

algorithm

deteriorates

due

to

the

amount

of

memory

needed

when

the

number

of

stable

models

is

large

This

is

not

the

case

with

the

algorithm

nor

with

the

SLG

system

Both

the

branch

and

bound

algorithm

and

the

SLG

system

prunes

the

search

space

less

effectively

than

The

mixed

integer

programming

approach

computes

all

minimal

models

of

a

logic

program

and

then

tests

if

these

models

are

stable

This

corresponds

to

a

very

weak

pruning

of

the

search

space

For

instance

the

set

of

rules

fai

not

-LRB-

bi

-RRB-

gni

has

one

stable

model

but

minimal

models

We

conclude

that

the

improved

algorithm

as

well

as

the

original

algorithm

are

significantly

better

than

the

branch

and

bound

algorithm

and

the

mixed

integer

programming

methods

and

slightly

better

than

the

SLG

system

However

experimental

results

in

-LSB-

-RSB-

and

Section

indicate

that

both

smodels

and

also

perform

significantly

better

than

the

SLG

system

Applications

In

this

section

we

exhibit

some

applications

of

logic

programs

with

the

stable

model

semantics

In

Section

we

present

an

encoding

of

logic

circuits

whose

stable

models

represent

diagnoses

of

the

circuit

In

Section

we

present

the

encodings

of

two

NP-complete

graph

problems

-LSB-

-RSB-

the

N-coloring

and

the

Hamiltonian

circuit

problem

in

undirected

graphs

These

encodings

can

be

used

when

evaluating

the

performance

of

algorithms

computing

the

stable

model

semantics

We

end

with

some

remarks

on

the

algorithm

in

Section

Diagnosis

Given

a

description

and

some

observations

of

a

system

a

diagnosis

is

a

conjecture

that

some

minimal

set

of

components

are

faulty

the

rest

being

normal

that

explains

the

behavior

of

the

system

i.e.

that

explains

the

discrepancy

between

the

observations

and

the

correct

behavior

of

the

system

-LSB-

-RSB-

We

will

use

this

definition

to

encode

logic

gates

their

connections

and

their

inputs

and

outputs

as

logic

programs

Reiter

-LSB-

-RSB-

assumes

that

the

logic

encoding

the

description

of

the

system

contains

at

least

the

logical

connectives

conjunction

and

negation

with

their

normal

interpretation

He

then

encodes

the

system

description

as

implications

the

assumption

that

a

component

is

not

abnormal

implies

the

correct

behavior

of

the

component

As

we

encode

logic

circuits

it

suffices

to

use

propositional

logic

to

describe

the

circuits

Definition

A

system

is

a

pair

-LRB-

SD

C

-RRB-

where

SD

the

system

description

is

a

finite

set

of

propositional

logic

sentences

and

C

the

system

components

is

a

finite

set

of

constants

An

observation

OBS

of

a

system

is

a

finite

set

of

propositional

sentences

As

a

rule

in

a

logic

program

with

stable

model

semantics

is

not

an

implication

we

can

not

use

the

encoding

in

-LSB-

-RSB-

directly

We

have

to

encode

A

implies

B

and

not

B

implies

not

A

separately

We

proceed

with

the

encoding

of

the

system

description

of

the

not

xor

andand

or-gates

The

not

and

xor-gates

are

easily

encoded

as

three

sets

of

rules

The

first

set

encodes

the

normal

behavior

of

the

gate

from

the

input

to

the

output

side

The

second

set

encodes

the

normal

behavior

from

the

output

to

the

input

side

Finally

the

third

set

encodes

input

and

output

combinations

that

lead

to

abnormal

behavior

of

the

gate

The

rules

corresponding

to

the

not-gate

and

the

xor-gate

are

presented

in

Figures

and

We

use

a

notation

containing

the

variable

X

The

variable

X

should

be

replaced

by

a

constant

representing

a

component

of

the

logic

circuit

in

an

actual

encoding

The

resulting

construct

then

denotes

a

propositional

atom

For

example

if

is

a

constant

then

ab

-LRB-

-RRB-

is

a

propositional

atom

that

determines

whether

the

component

is

abnormal

The

rules

are

the

encoding

of

a

system

description

where

negation

is

replaced

by

not

and

the

symbol

replaces

the

implication

sign

We

encode

the

and

and

or-gates

in

a

similar

way

to

the

not

and

xor-gates

However

we

have

to

add

some

rules

as

some

outputs

of

these

two

gates

does

not

uniquely

determine

their

inputs

The

problem

lies

in

that

we

would

need

to

use

disjunction

to

encode

the

and

and

or-gates

correctly

For

instance

if

the

output

of

an

and-gate

is

zero

the

first

or

the

second

input

should

be

zero

We

simulate

the

disjunction

by

using

two

rules

to

determine

two

different

stable

models

in

which

one

or

the

other

of

the

inputs

is

zero

This

technique

has

its

drawbacks

however

as

it

in

some

cases

leads

to

multiple

stable

models

per

diagnosis

The

and

and

or-gates

are

described

in

Figures

and

Example

Let

a

be

an

and-gate

Then

the

rules

that

encode

the

disjunction

are

p

-LRB-

a

-RRB-

and

-LRB-

a

-RRB-

out

-LRB-

a

-RRB-

not

-LRB-

q

-LRB-

a

-RRB-

-RRB-

not

-LRB-

r

-LRB-

a

-RRB-

-RRB-

not

-LRB-

ab

-LRB-

a

-RRB-

-RRB-

q

-LRB-

a

-RRB-

and

-LRB-

a

-RRB-

out

-LRB-

a

-RRB-

not

-LRB-

p

-LRB-

a

-RRB-

-RRB-

not

-LRB-

r

-LRB-

a

-RRB-

-RRB-

not

-LRB-

ab

-LRB-

a

-RRB-

-RRB-

r

-LRB-

a

-RRB-

and

-LRB-

a

-RRB-

out

-LRB-

a

-RRB-

-LRB-

a

-RRB-

not

-LRB-

ab

-LRB-

a

-RRB-

-RRB-

r

-LRB-

a

-RRB-

and

-LRB-

a

-RRB-

out

-LRB-

a

-RRB-

-LRB-

a

-RRB-

not

-LRB-

ab

-LRB-

a

-RRB-

-RRB-

-LRB-

a

-RRB-

and

-LRB-

a

-RRB-

p

-LRB-

a

-RRB-

-LRB-

a

-RRB-

and

-LRB-

a

-RRB-

q

-LRB-

a

-RRB-

The

middle

two

rules

puts

r

-LRB-

a

-RRB-

in

the

stable

model

if

the

output

is

zero

and

one

of

the

inputs

is

one

The

first

two

rules

determine

that

either

p

-LRB-

a

-RRB-

or

q

-LRB-

a

-RRB-

should

be

in

the

stable

model

if

the

output

is

zero

and

if

r

-LRB-

a

-RRB-

is

not

in

the

stable

model

i.e.

if

neither

of

the

inputs

is

one

Finally

the

last

two

rules

set

the

first

input

to

one

if

p

-LRB-

a

-RRB-

is

in

the

stable

model

and

set

the

second

input

to

one

if

q

-LRB-

a

-RRB-

is

in

the

stable

model

The

interconnections

between

different

gates

are

encoded

using

four

rules

as

follows

Suppose

the

first

input

of

the

and-gate

and

the

output

of

the

or-gate

are

connected

This

is

encoded

using

the

rules

fout

-LRB-

-RRB-

-LRB-

-RRB-

out

-LRB-

-RRB-

-LRB-

-RRB-

-LRB-

-RRB-

out

-LRB-

-RRB-

-LRB-

-RRB-

out

-LRB-

-RRB-

g

If

an

input

or

an

output

is

set

to

zero

or

one

we

encode

this

using

one

rule

Assume

that

the

second

input

of

the

and-gate

is

zero

and

that

the

output

Forward

out

-LRB-

X

-RRB-

nt

-LRB-

X

-RRB-

in

-LRB-

X

-RRB-

not

-LRB-

ab

-LRB-

X

-RRB-

-RRB-

out

-LRB-

X

-RRB-

nt

-LRB-

X

-RRB-

in

-LRB-

X

-RRB-

not

-LRB-

ab

-LRB-

X

-RRB-

-RRB-

Backward

in

-LRB-

X

-RRB-

nt

-LRB-

X

-RRB-

out

-LRB-

X

-RRB-

not

-LRB-

ab

-LRB-

X

-RRB-

-RRB-

in

-LRB-

X

-RRB-

nt

-LRB-

X

-RRB-

out

-LRB-

X

-RRB-

not

-LRB-

ab

-LRB-

X

-RRB-

-RRB-

Abnormal

ab

-LRB-

X

-RRB-

nt

-LRB-

X

-RRB-

in

-LRB-

X

-RRB-

out

-LRB-

X

-RRB-

ab

-LRB-

X

-RRB-

nt

-LRB-

X

-RRB-

in

-LRB-

X

-RRB-

out

-LRB-

X

-RRB-

Figure

Not-gate

Forward

out

-LRB-

X

-RRB-

xor

-LRB-

X

-RRB-

-LRB-

X

-RRB-

-LRB-

X

-RRB-

not

-LRB-

ab

-LRB-

X

-RRB-

-RRB-

out

-LRB-

X

-RRB-

xor

-LRB-

X

-RRB-

-LRB-

X

-RRB-

-LRB-

X

-RRB-

not

-LRB-

ab

-LRB-

X

-RRB-

-RRB-

out

-LRB-

X

-RRB-

xor

-LRB-

X

-RRB-

-LRB-

X

-RRB-

-LRB-

X

-RRB-

not

-LRB-

ab

-LRB-

X

-RRB-

-RRB-

out

-LRB-

X

-RRB-

xor

-LRB-

X

-RRB-

-LRB-

X

-RRB-

-LRB-

X

-RRB-

not

-LRB-

ab

-LRB-

X

-RRB-

-RRB-

Backward

-LRB-

X

-RRB-

xor

-LRB-

X

-RRB-

-LRB-

X

-RRB-

out

-LRB-

X

-RRB-

not

-LRB-

ab

-LRB-

X

-RRB-

-RRB-

-LRB-

X

-RRB-

xor

-LRB-

X

-RRB-

-LRB-

X

-RRB-

out

-LRB-

X

-RRB-

not

-LRB-

ab

-LRB-

X

-RRB-

-RRB-

-LRB-

X

-RRB-

xor

-LRB-

X

-RRB-

-LRB-

X

-RRB-

out

-LRB-

X

-RRB-

not

-LRB-

ab

-LRB-

X

-RRB-

-RRB-

-LRB-

X

-RRB-

xor

-LRB-

X

-RRB-

-LRB-

X

-RRB-

out

-LRB-

X

-RRB-

not

-LRB-

ab

-LRB-

X

-RRB-

-RRB-

-LRB-

X

-RRB-

xor

-LRB-

X

-RRB-

-LRB-

X

-RRB-

out

-LRB-

X

-RRB-

not

-LRB-

ab

-LRB-

X

-RRB-

-RRB-

-LRB-

X

-RRB-

xor

-LRB-

X

-RRB-

-LRB-

X

-RRB-

out

-LRB-

X

-RRB-

not

-LRB-

ab

-LRB-

X

-RRB-

-RRB-

-LRB-

X

-RRB-

xor

-LRB-

X

-RRB-

-LRB-

X

-RRB-

out

-LRB-

X

-RRB-

not

-LRB-

ab

-LRB-

X

-RRB-

-RRB-

-LRB-

X

-RRB-

xor

-LRB-

X

-RRB-

-LRB-

X

-RRB-

out

-LRB-

X

-RRB-

not

-LRB-

ab

-LRB-

X

-RRB-

-RRB-

Abnormal

ab

-LRB-

X

-RRB-

xor

-LRB-

X

-RRB-

-LRB-

X

-RRB-

-LRB-

X

-RRB-

out

-LRB-

X

-RRB-

ab

-LRB-

X

-RRB-

xor

-LRB-

X

-RRB-

-LRB-

X

-RRB-

-LRB-

X

-RRB-

out

-LRB-

X

-RRB-

ab

-LRB-

X

-RRB-

xor

-LRB-

X

-RRB-

-LRB-

X

-RRB-

-LRB-

X

-RRB-

out

-LRB-

X

-RRB-

ab

-LRB-

X

-RRB-

xor

-LRB-

X

-RRB-

-LRB-

X

-RRB-

-LRB-

X

-RRB-

out

-LRB-

X

-RRB-

Figure

Xor-gate

Forward

out

-LRB-

X

-RRB-

and

-LRB-

X

-RRB-

-LRB-

X

-RRB-

-LRB-

X

-RRB-

not

-LRB-

ab

-LRB-

X

-RRB-

-RRB-

out

-LRB-

X

-RRB-

and

-LRB-

X

-RRB-

-LRB-

X

-RRB-

-LRB-

X

-RRB-

not

-LRB-

ab

-LRB-

X

-RRB-

-RRB-

out

-LRB-

X

-RRB-

and

-LRB-

X

-RRB-

-LRB-

X

-RRB-

-LRB-

X

-RRB-

not

-LRB-

ab

-LRB-

X

-RRB-

-RRB-

out

-LRB-

X

-RRB-

and

-LRB-

X

-RRB-

-LRB-

X

-RRB-

-LRB-

X

-RRB-

not

-LRB-

ab

-LRB-

X

-RRB-

-RRB-

Backward

-LRB-

X

-RRB-

and

-LRB-

X

-RRB-

out

-LRB-

X

-RRB-

not

-LRB-

ab

-LRB-

X

-RRB-

-RRB-

-LRB-

X

-RRB-

and

-LRB-

X

-RRB-

out

-LRB-

X

-RRB-

not

-LRB-

ab

-LRB-

X

-RRB-

-RRB-

-LRB-

X

-RRB-

and

-LRB-

X

-RRB-

-LRB-

X

-RRB-

out

-LRB-

X

-RRB-

not

-LRB-

ab

-LRB-

X

-RRB-

-RRB-

-LRB-

X

-RRB-

and

-LRB-

X

-RRB-

-LRB-

X

-RRB-

out

-LRB-

X

-RRB-

not

-LRB-

ab

-LRB-

X

-RRB-

-RRB-

p

-LRB-

X

-RRB-

and

-LRB-

X

-RRB-

out

-LRB-

X

-RRB-

not

-LRB-

q

-LRB-

X

-RRB-

-RRB-

not

-LRB-

r

-LRB-

X

-RRB-

-RRB-

not

-LRB-

ab

-LRB-

X

-RRB-

-RRB-

q

-LRB-

X

-RRB-

and

-LRB-

X

-RRB-

out

-LRB-

X

-RRB-

not

-LRB-

p

-LRB-

X

-RRB-

-RRB-

not

-LRB-

r

-LRB-

X

-RRB-

-RRB-

not

-LRB-

ab

-LRB-

X

-RRB-

-RRB-

r

-LRB-

X

-RRB-

and

-LRB-

X

-RRB-

out

-LRB-

X

-RRB-

-LRB-

X

-RRB-

not

-LRB-

ab

-LRB-

X

-RRB-

-RRB-

r

-LRB-

X

-RRB-

and

-LRB-

X

-RRB-

out

-LRB-

X

-RRB-

-LRB-

X

-RRB-

not

-LRB-

ab

-LRB-

X

-RRB-

-RRB-

-LRB-

X

-RRB-

and

-LRB-

X

-RRB-

p

-LRB-

X

-RRB-

-LRB-

X

-RRB-

and

-LRB-

X

-RRB-

q

-LRB-

X

-RRB-

Abnormal

ab

-LRB-

X

-RRB-

and

-LRB-

X

-RRB-

-LRB-

X

-RRB-

out

-LRB-

X

-RRB-

ab

-LRB-

X

-RRB-

and

-LRB-

X

-RRB-

-LRB-

X

-RRB-

out

-LRB-

X

-RRB-

ab

-LRB-

X

-RRB-

and

-LRB-

X

-RRB-

-LRB-

X

-RRB-

-LRB-

X

-RRB-

out

-LRB-

X

-RRB-

Figure

And-gate

Forward

out

-LRB-

X

-RRB-

or

-LRB-

X

-RRB-

-LRB-

X

-RRB-

-LRB-

X

-RRB-

not

-LRB-

ab

-LRB-

X

-RRB-

-RRB-

out

-LRB-

X

-RRB-

or

-LRB-

X

-RRB-

-LRB-

X

-RRB-

-LRB-

X

-RRB-

not

-LRB-

ab

-LRB-

X

-RRB-

-RRB-

out

-LRB-

X

-RRB-

or

-LRB-

X

-RRB-

-LRB-

X

-RRB-

-LRB-

X

-RRB-

not

-LRB-

ab

-LRB-

X

-RRB-

-RRB-

out

-LRB-

X

-RRB-

or

-LRB-

X

-RRB-

-LRB-

X

-RRB-

-LRB-

X

-RRB-

not

-LRB-

ab

-LRB-

X

-RRB-

-RRB-

Backward

-LRB-

X

-RRB-

or

-LRB-

X

-RRB-

out

-LRB-

X

-RRB-

not

-LRB-

ab

-LRB-

X

-RRB-

-RRB-

-LRB-

X

-RRB-

or

-LRB-

X

-RRB-

out

-LRB-

X

-RRB-

not

-LRB-

ab

-LRB-

X

-RRB-

-RRB-

-LRB-

X

-RRB-

or

-LRB-

X

-RRB-

-LRB-

X

-RRB-

out

-LRB-

X

-RRB-

not

-LRB-

ab

-LRB-

X

-RRB-

-RRB-

-LRB-

X

-RRB-

or

-LRB-

X

-RRB-

-LRB-

X

-RRB-

out

-LRB-

X

-RRB-

not

-LRB-

ab

-LRB-

X

-RRB-

-RRB-

p

-LRB-

X

-RRB-

or

-LRB-

X

-RRB-

out

-LRB-

X

-RRB-

not

-LRB-

q

-LRB-

X

-RRB-

-RRB-

not

-LRB-

r

-LRB-

X

-RRB-

-RRB-

not

-LRB-

ab

-LRB-

X

-RRB-

-RRB-

q

-LRB-

X

-RRB-

or

-LRB-

X

-RRB-

out

-LRB-

X

-RRB-

not

-LRB-

p

-LRB-

X

-RRB-

-RRB-

not

-LRB-

r

-LRB-

X

-RRB-

-RRB-

not

-LRB-

ab

-LRB-

X

-RRB-

-RRB-

r

-LRB-

X

-RRB-

or

-LRB-

X

-RRB-

out

-LRB-

X

-RRB-

-LRB-

X

-RRB-

not

-LRB-

ab

-LRB-

X

-RRB-

-RRB-

r

-LRB-

X

-RRB-

or

-LRB-

X

-RRB-

out

-LRB-

X

-RRB-

-LRB-

X

-RRB-

not

-LRB-

ab

-LRB-

X

-RRB-

-RRB-

-LRB-

X

-RRB-

or

-LRB-

X

-RRB-

p

-LRB-

X

-RRB-

-LRB-

X

-RRB-

or

-LRB-

X

-RRB-

q

-LRB-

X

-RRB-

Abnormal

ab

-LRB-

X

-RRB-

or

-LRB-

X

-RRB-

-LRB-

X

-RRB-

-LRB-

X

-RRB-

out

-LRB-

X

-RRB-

ab

-LRB-

X

-RRB-

or

-LRB-

X

-RRB-

-LRB-

X

-RRB-

out

-LRB-

X

-RRB-

ab

-LRB-

X

-RRB-

or

-LRB-

X

-RRB-

-LRB-

X

-RRB-

out

-LRB-

X

-RRB-

Figure

Or-gate

is

one

This

is

encoded

as

-LRB-

-RRB-

out

-LRB-

-RRB-

g

The

encoding

of

the

outputs

can

be

considered

an

observation

of

the

system

Example

Consider

the

logic

circuit

consisting

of

two

and-gates

and

where

the

output

of

is

connected

to

both

inputs

of

Set

the

inputs

of

to

one

and

set

the

output

of

to

zero

The

resulting

circuit

has

a

diagnosis

where

the

and-gate

is

abnormal

and

the

and-gate

is

normal

To

this

diagnosis

corresponds

two

stable

models

One

of

them

contains

the

atom

p

-LRB-

-RRB-

and

the

other

contains

the

atom

q

-LRB-

-RRB-

Definition

-LRB-

-LSB-

-RSB-

-RRB-

A

diagnosis

for

-LRB-

SD

C

-RRB-

and

OBS

is

a

minimal

set

C

such

that

SD

-LSB-

OBS

-LSB-

fab

-LRB-

c

-RRB-

j

c

g

-LSB-

f

ab

-LRB-

c

-RRB-

j

c

Cg

is

consistent

Let

P

be

the

encoding

of

a

logic

circuit

including

its

inputs

and

outputs

as

described

in

the

previous

paragraphs

This

program

P

determines

a

system

description

SD

and

the

observations

OBS

of

the

logic

circuit

if

the

rules

are

replaced

by

propositional

logic

sentences

in

the

following

way

A

rule

a

bn

not

-LRB-

-RRB-

not

-LRB-

cm

-RRB-

is

replaced

by

the

propositional

sentence

bn

cm

a

The

set

components

C

of

the

system

is

determined

by

the

atoms

of

P

of

form

ab

-LRB-

-RRB-

We

note

that

the

p

q

r

rules

should

in

the

propositional

case

be

replaced

by

suitable

sentences

containing

disjunctions

We

now

show

that

each

diagnosis

of

-LRB-

SD

C

-RRB-

and

OBS

determines

at

least

one

stable

model

of

P

and

that

each

stable

model

determines

one

diagnosis

Lemma

Let

P

be

a

set

of

rules

and

let

B

be

a

P

full

set

Then

B

is

maximal

i.e.

B

is

not

a

proper

subset

of

any

P

full

set

Proof

Let

be

a

subset

of

NAnt

-LRB-

P

-RRB-

such

that

B

is

a

proper

subset

of

Take

an

arbitrary

not-atom

not

-LRB-

a

-RRB-

not

in

B.

Now

not

-LRB-

a

-RRB-

B

implies

a

Dcl

-LRB-

P

B

-RRB-

as

B

is

P

full

But

a

Dcl

-LRB-

P

B

-RRB-

implies

a

Dcl

-LRB-

P

-RRB-

and

as

not

-LRB-

a

-RRB-

we

see

that

is

not

P

full

A

diagnosis

of

a

logic

circuit

is

a

minimal

set

of

gates

considered

abnormal

Equivalently

a

diagnosis

is

a

maximal

set

of

gates

not

considered

abnormal

If

we

interpret

not-atoms

of

the

form

not

-LRB-

ab

-LRB-

X

-RRB-

-RRB-

as

meaning

that

gate

X

is

not

abnormal

we

can

interpret

sets

of

not-atoms

as

diagnoses

of

the

corresponding

system

description

SD

and

observations

OBS

Now

if

P

is

a

set

of

rules

encoding

a

logic

circuit

we

have

to

show

that

a

set

is

P

full

if

and

only

if

the

set

is

a

diagnosis

Let

the

set

B

be

a

P

full

set

and

consider

the

rules

defining

an

and-gate

If

not

-LRB-

ab

-LRB-

-RRB-

-RRB-

is

not

in

B

then

not

-LRB-

p

-LRB-

-RRB-

-RRB-

not

-LRB-

q

-LRB-

-RRB-

-RRB-

and

not

-LRB-

r

-LRB-

-RRB-

-RRB-

are

in

B.

On

the

other

hand

assume

that

not

-LRB-

ab

-LRB-

-RRB-

-RRB-

is

in

B

If

out

-LRB-

-RRB-

Dcl

-LRB-

P

B

-RRB-

holds

then

not

-LRB-

p

-LRB-

-RRB-

-RRB-

not

-LRB-

q

-LRB-

-RRB-

-RRB-

and

not

-LRB-

r

-LRB-

-RRB-

-RRB-

are

in

B

If

out

-LRB-

-RRB-

Dcl

-LRB-

P

B

-RRB-

then

either

one

of

-LRB-

-RRB-

or

-LRB-

-RRB-

belongs

to

Dcl

-LRB-

P

B

-RRB-

and

the

literals

not

-LRB-

p

-LRB-

-RRB-

-RRB-

and

not

-LRB-

q

-LRB-

-RRB-

-RRB-

are

in

B

or

not

-LRB-

r

-LRB-

-RRB-

-RRB-

and

one

of

not

-LRB-

q

-LRB-

-RRB-

-RRB-

and

not

-LRB-

r

-LRB-

-RRB-

-RRB-

is

in

B

A

similar

relation

holds

for

or-gates

Thus

a

maximal

P

full

set

contains

a

maximal

number

of

gates

not

considered

abnormal

Let

be

a

stable

model

of

the

set

P

and

let

be

the

components

that

correspond

to

the

atoms

ab

-LRB-

-RRB-

in

As

the

stable

model

is

the

deductive

closure

of

the

rules

and

the

P

full

set

and

as

no

atom

in

the

stable

model

is

covered

by

the

P

full

set

the

set

SD

-LSB-

OBS

-LSB-

fab

-LRB-

c

-RRB-

j

c

-LSB-

f

ab

-LRB-

c

-RRB-

j

c

Cg

is

consistent

Hence

a

P

full

set

determines

a

diagnosis

Conversely

as

the

set

of

sentences

is

consistent

and

as

the

diagnosis

is

minimal

we

see

that

a

diagnosis

determines

at

least

one

P

full

set

Logic-based

abduction

-LSB-

-RSB-

provides

a

slightly

different

approach

to

diagnosis

than

Reiter

's

consistency-based

approach

A

diagnosis

is

in

this

case

a

subset

of

a

given

set

of

hypotheses

that

is

consistent

with

the

system

description

and

logically

entails

the

observations

An

additional

condition

that

the

subset

of

hypotheses

is

inclusion

or

cardinality

minimal

is

often

added

As

the

stable

models

are

the

deductive

closures

of

the

program

P

with

respect

to

the

P

full

sets

the

diagnoses

are

also

abductive

if

we

take

the

set

of

hypotheses

to

be

the

set

of

not-atoms

The

additional

condition

is

in

this

case

inclusion

maximality

Graph

Problems

We

encode

two

NP-complete

graph

problems

as

logic

programs

such

that

the

stable

models

of

the

programs

correspond

to

the

solutions

to

the

problems

But

first

we

clarify

a

part

of

the

terminology

connected

with

graphs

Definition

A

graph

G

is

a

pair

-LRB-

V

E

-RRB-

where

V

is

a

set

of

vertices

and

E

V

V

is

a

set

of

edges

Let

G

-LRB-

V

E

-RRB-

be

a

graph

The

graph

G

is

undirected

if

for

any

two

vertices

a

and

b

such

that

there

is

an

edge

from

a

to

b

i.e.

-LRB-

a

b

-RRB-

E

there

is

also

an

edge

from

b

to

a.

Two

vertices

a

and

b

are

neighbors

if

there

is

an

edge

between

them

A

path

in

a

graph

is

a

finite

sequence

of

vertices

an

such

that

there

is

an

edge

from

ai

to

ai

for

i

n.

Finally

the

path

is

closed

if

an

The

N-coloring

problem

consists

of

finding

an

N-coloring

of

an

undirected

graph

An

N-coloring

assigns

one

of

N

colors

to

each

vertex

of

the

graph

in

such

a

way

that

no

two

neighbors

have

a

color

in

common

A

Hamiltonian

circuit

in

a

graph

is

a

closed

path

that

visits

each

vertex

exactly

once

The

Hamiltonian

circuit

problem

is

then

the

problem

of

finding

a

Hamiltonian

circuit

The

N-coloring

problem

is

translated

from

a

graph

into

a

logic

program

as

follows

For

each

vertex

a

with

neighbors

pj

and

each

color

i

N

include

the

rule

color

-LRB-

a

i

-RRB-

not

-LRB-

color

-LRB-

i

-RRB-

-RRB-

not

-LRB-

color

-LRB-

pj

i

-RRB-

-RRB-

not

-LRB-

color

-LRB-

a

i

mod

N

-RRB-

-RRB-

not

-LRB-

color

-LRB-

a

i

N

mod

N

-RRB-

-RRB-

and

for

each

vertex

a

include

the

rule

h

not

-LRB-

color

-LRB-

a

-RRB-

-RRB-

not

-LRB-

color

-LRB-

a

N

-RRB-

-RRB-

Finally

add

the

rules

not

-LRB-

h

-RRB-

and

not

-LRB-

-RRB-

The

number

of

rules

is

thus

-LRB-

N

-RRB-

V

if

V

is

the

number

of

vertices

Example

We

give

the

translation

of

the

problem

into

a

logic

program

for

the

complete

graph

of

two

vertices

a

and

b.

fcolor

-LRB-

b

-RRB-

not

-LRB-

color

-LRB-

a

-RRB-

-RRB-

not

-LRB-

color

-LRB-

b

-RRB-

-RRB-

not

-LRB-

color

-LRB-

b

-RRB-

-RRB-

color

-LRB-

b

-RRB-

not

-LRB-

color

-LRB-

a

-RRB-

-RRB-

not

-LRB-

color

-LRB-

b

-RRB-

-RRB-

not

-LRB-

color

-LRB-

b

-RRB-

-RRB-

color

-LRB-

b

-RRB-

not

-LRB-

color

-LRB-

a

-RRB-

-RRB-

not

-LRB-

color

-LRB-

b

-RRB-

-RRB-

not

-LRB-

color

-LRB-

b

-RRB-

-RRB-

color

-LRB-

a

-RRB-

not

-LRB-

color

-LRB-

b

-RRB-

-RRB-

not

-LRB-

color

-LRB-

a

-RRB-

-RRB-

not

-LRB-

color

-LRB-

a

-RRB-

-RRB-

color

-LRB-

a

-RRB-

not

-LRB-

color

-LRB-

b

-RRB-

-RRB-

not

-LRB-

color

-LRB-

a

-RRB-

-RRB-

not

-LRB-

color

-LRB-

a

-RRB-

-RRB-

color

-LRB-

a

-RRB-

not

-LRB-

color

-LRB-

b

-RRB-

-RRB-

not

-LRB-

color

-LRB-

a

-RRB-

-RRB-

not

-LRB-

color

-LRB-

a

-RRB-

-RRB-

h

not

-LRB-

color

-LRB-

a

-RRB-

-RRB-

not

-LRB-

color

-LRB-

a

-RRB-

-RRB-

not

-LRB-

color

-LRB-

a

-RRB-

-RRB-

h

not

-LRB-

color

-LRB-

b

-RRB-

-RRB-

not

-LRB-

color

-LRB-

b

-RRB-

-RRB-

not

-LRB-

color

-LRB-

b

-RRB-

-RRB-

not

-LRB-

h

-RRB-

not

-LRB-

-RRB-

g

Proposition

Let

G

-LRB-

V

E

-RRB-

be

an

undirected

graph

Let

P

be

the

logic

program

encoding

the

N-coloring

problem

for

the

graph

G.

Then

is

a

stable

model

of

P

if

and

only

if

C

fcolor

-LRB-

a

i

-RRB-

j

color

-LRB-

a

i

-RRB-

i

Ng

is

an

N-coloring

of

G

in

the

sense

that

a

V

has

color

i

if

color

-LRB-

a

i

-RRB-

C.

Proof

-LRB-

-RRB-

-RRB-

Let

be

a

stable

model

of

P

Let

a

be

a

vertex

and

let

i

N

Note

that

there

is

only

one

rule

in

P

with

head

color

-LRB-

a

i

-RRB-

and

that

not

-LRB-

color

-LRB-

a

i

mod

N

-RRB-

-RRB-

not

-LRB-

color

-LRB-

a

i

N

mod

N

-RRB-

-RRB-

is

in

the

body

of

this

rule

Thus

color

-LRB-

a

i

-RRB-

color

-LRB-

a

j

-RRB-

for

i

j

is

not

possible

Furthermore

as

the

rules

not

-LRB-

h

-RRB-

and

not

-LRB-

-RRB-

are

in

P

we

must

have

h

The

rule

h

not

-LRB-

color

-LRB-

a

-RRB-

-RRB-

not

-LRB-

color

-LRB-

a

N

-RRB-

-RRB-

then

implies

that

there

is

an

i

such

that

color

-LRB-

a

i

-RRB-

Hence

each

vertex

has

exactly

one

color

The

one

rule

in

P

with

head

color

-LRB-

a

i

-RRB-

has

not

-LRB-

color

-LRB-

b

i

-RRB-

-RRB-

in

its

body

for

each

neighbor

b

of

a.

Thus

color

-LRB-

a

i

-RRB-

and

color

-LRB-

b

i

-RRB-

can

not

both

belong

to

the

stable

model

Consequently

no

two

neighbors

have

a

color

in

common

and

C

is

an

N-coloring

-LRB-

-LRB-

-RRB-

Let

C

fcolor

-LRB-

a

i

-RRB-

j

a

V

i

Ng

be

an

N-coloring

of

G.

Let

fnot

-LRB-

color

-LRB-

a

i

-RRB-

-RRB-

j

a

V

i

N

color

-LRB-

a

i

-RRB-

Cg

-LSB-

fnot

-LRB-

h

-RRB-

g

If

not

-LRB-

color

-LRB-

a

i

-RRB-

-RRB-

then

color

-LRB-

a

j

-RRB-

C

for

a

j

i.

Thus

color

-LRB-

a

i

-RRB-

Dcl

-LRB-

P

-RRB-

On

the

other

hand

if

not

-LRB-

color

-LRB-

a

i

-RRB-

-RRB-

then

color

-LRB-

a

i

-RRB-

C

and

color

-LRB-

a

j

-RRB-

C

for

j

i

j

N

Therefore

not

-LRB-

color

-LRB-

a

j

-RRB-

-RRB-

and

not

-LRB-

color

-LRB-

b

i

-RRB-

-RRB-

for

all

neighbors

b

of

a.

Thus

color

-LRB-

a

i

-RRB-

Dcl

-LRB-

P

-RRB-

In

addition

h

Dcl

-LRB-

P

-RRB-

and

Dcl

-LRB-

P

-RRB-

Hence

is

P

full

and

Dcl

-LRB-

P

-RRB-

is

a

stable

model

of

P

The

translation

of

the

Hamiltonian

circuit

problem

is

somewhat

more

complicated

For

each

pair

-LRB-

a

b

-RRB-

of

vertices

in

the

graph

such

that

there

is

an

edge

between

a

and

b

and

where

bi

are

the

neighbors

of

a

excluding

b

and

aj

are

the

neighbors

of

b

excluding

a

include

the

rule

edge

-LRB-

a

b

-RRB-

not

-LRB-

edge

-LRB-

a

-RRB-

-RRB-

not

-LRB-

edge

-LRB-

a

bi

-RRB-

-RRB-

not

-LRB-

edge

-LRB-

b

-RRB-

-RRB-

not

-LRB-

edge

-LRB-

aj

b

-RRB-

-RRB-

not

-LRB-

edge

-LRB-

b

a

-RRB-

-RRB-

Fix

a

vertex

d.

For

each

pair

-LRB-

a

b

-RRB-

of

vertices

in

the

graph

such

that

there

is

an

edge

between

a

and

b

if

b

d

include

the

rule

b

a

edge

-LRB-

a

b

-RRB-

and

if

b

d

include

the

rule

a

edge

-LRB-

a

d

-RRB-

Finally

for

each

vertex

a

d

add

a

not

-LRB-

a

-RRB-

and

then

add

the

rules

d

and

not

-LRB-

-RRB-

If

the

number

of

vertices

is

denoted

by

V

and

the

number

of

edges

by

E

the

number

of

rules

in

this

problem

is

V

Example

We

give

the

set

of

rules

for

the

translation

of

the

Hamiltonian

circuit

problem

for

the

complete

graph

of

the

three

vertices

a

b

and

c.

fedge

-LRB-

a

b

-RRB-

not

-LRB-

edge

-LRB-

a

c

-RRB-

-RRB-

not

-LRB-

edge

-LRB-

c

b

-RRB-

-RRB-

not

-LRB-

edge

-LRB-

b

a

-RRB-

-RRB-

edge

-LRB-

b

a

-RRB-

not

-LRB-

edge

-LRB-

b

c

-RRB-

-RRB-

not

-LRB-

edge

-LRB-

c

a

-RRB-

-RRB-

not

-LRB-

edge

-LRB-

a

b

-RRB-

-RRB-

edge

-LRB-

a

c

-RRB-

not

-LRB-

edge

-LRB-

a

b

-RRB-

-RRB-

not

-LRB-

edge

-LRB-

b

c

-RRB-

-RRB-

not

-LRB-

edge

-LRB-

c

a

-RRB-

-RRB-

edge

-LRB-

c

a

-RRB-

not

-LRB-

edge

-LRB-

c

b

-RRB-

-RRB-

not

-LRB-

edge

-LRB-

b

a

-RRB-

-RRB-

not

-LRB-

edge

-LRB-

a

c

-RRB-

-RRB-

edge

-LRB-

b

c

-RRB-

not

-LRB-

edge

-LRB-

b

a

-RRB-

-RRB-

not

-LRB-

edge

-LRB-

c

a

-RRB-

-RRB-

not

-LRB-

edge

-LRB-

c

b

-RRB-

-RRB-

edge

-LRB-

c

b

-RRB-

not

-LRB-

edge

-LRB-

c

a

-RRB-

-RRB-

not

-LRB-

edge

-LRB-

a

b

-RRB-

-RRB-

not

-LRB-

edge

-LRB-

b

c

-RRB-

-RRB-

b

a

edge

-LRB-

a

b

-RRB-

c

a

edge

-LRB-

a

c

-RRB-

c

b

edge

-LRB-

b

c

-RRB-

b

edge

-LRB-

b

a

-RRB-

b

c

edge

-LRB-

c

b

-RRB-

c

edge

-LRB-

c

a

-RRB-

not

-LRB-

-RRB-

b

not

-LRB-

b

-RRB-

c

not

-LRB-

c

-RRB-

a

g

Proposition

Let

G

-LRB-

V

E

-RRB-

be

an

undirected

graph

Let

P

be

the

logic

program

encoding

the

Hamiltonian

circuit

problem

for

the

graph

G.

Then

is

a

stable

model

of

P

if

and

only

if

F

f

-LRB-

a

b

-RRB-

E

j

edge

-LRB-

a

b

-RRB-

g

is

a

Hamiltonian

circuit

in

G.

Proof

-LRB-

-RRB-

-RRB-

Let

be

a

stable

model

of

P

Assume

there

are

vertices

a

b

and

c

such

that

b

c

and

edge

-LRB-

a

b

-RRB-

Then

edge

-LRB-

a

c

-RRB-

as

there

is

only

one

rule

in

P

with

head

edge

-LRB-

a

b

-RRB-

and

not

-LRB-

edge

-LRB-

a

c

-RRB-

-RRB-

is

in

the

body

of

this

rule

Thus

at

most

one

edge

begins

at

any

vertex

Similarly

at

most

one

edge

ends

at

any

vertex

As

a

not

-LRB-

a

-RRB-

P

and

d

P

a

for

a

V

-LSB-

But

a

vertex

a

is

in

only

if

there

is

a

directed

path

from

d

to

a

in

F

as

is

seen

by

the

following

argument

If

there

is

an

edge

in

F

going

from

a

to

b

and

a

is

in

then

edge

-LRB-

a

b

-RRB-

and

as

b

a

edge

-LRB-

a

b

-RRB-

P

we

have

that

b

Now

as

d

we

have

proved

by

induction

that

there

is

a

directed

path

from

d

to

a

in

F

if

a

Finally

implies

that

the

path

is

a

Hamiltonian

circuit

-LRB-

-LRB-

-RRB-

Let

F

E

be

a

Hamiltonian

circuit

in

G.

Let

fnot

-LRB-

edge

-LRB-

a

b

-RRB-

-RRB-

j

-LRB-

a

b

-RRB-

E?Fg

If

not

-LRB-

edge

-LRB-

a

b

-RRB-

-RRB-

then

-LRB-

a

b

-RRB-

F

and

there

exists

a

vertex

c

such

that

-LRB-

a

c

-RRB-

F

Thus

not

-LRB-

edge

-LRB-

a

c

-RRB-

-RRB-

and

edge

-LRB-

a

b

-RRB-

Dcl

-LRB-

P

-RRB-

If

on

the

other

hand

not

-LRB-

edge

-LRB-

a

b

-RRB-

-RRB-

then

-LRB-

a

b

-RRB-

F

As

F

is

a

Hamiltonian

circuit

-LRB-

a

c

-RRB-

F

for

all

neighbors

of

a

except

b

-LRB-

d

b

-RRB-

F

for

all

neighbors

of

b

except

a

and

-LRB-

b

a

-RRB-

F

Hence

edge

-LRB-

a

b

-RRB-

Dcl

-LRB-

P

-RRB-

In

addition

as

there

is

a

directed

path

from

d

to

any

vertex

a

a

Dcl

-LRB-

P

-RRB-

We

therefore

conclude

that

is

P

full

Remarks

The

algorithm

is

not

especially

suited

for

finding

diagnoses

The

algorithm

finds

the

diagnoses

in

a

rather

arbitrary

order

Furthermore

one

would

certainly

wish

to

relate

a

probability

of

failure

to

the

components

in

the

system

one

is

diagnosing

thus

finding

the

more

probable

diagnoses

first

Although

the

algorithm

does

not

directly

support

such

specialized

behavior

it

serves

as

a

good

framework

for

implementing

application

specific

algorithms

Another

example

of

possible

specialization

can

be

taken

from

the

Hamiltonian

circuit

problem

If

we

add

weights

to

the

edges

in

the

graph

and

search

for

the

Hamiltonian

circuit

with

least

total

weight

we

obtain

the

traveling

salesman

problem

A

test

for

comparing

the

weight

of

a

partial

circuit

with

the

weight

of

the

last

circuit

found

is

easily

incorporated

in

the

algorithm

We

remark

that

the

algorithm

is

a

general

algorithm

for

computing

stable

models

of

logic

programs

that

can

be

enhanced

to

cope

with

application

specific

demands

Experimental

Results

In

this

section

we

compare

the

execution

times

of

the

algorithm

and

the

SLG

system

-LSB-

-RSB-

by

using

the

two

algorithms

to

search

for

colorings

and

Hamiltonian

circuits

in

different

graphs

The

graphs

were

created

using

the

Stanford

GraphBase

-LSB-

-RSB-

which

is

a

highly

portable

collection

of

programs

and

data

that

serves

as

a

platform

for

combinatorial

algorithms

In

addition

has

been

tested

on

the

two

benchmark

programs

used

in

-LSB-

-RSB-

and

on

two

diagnosis

problems

a

ripple

carry

adder

and

a

parity

circuit

Two

types

of

graphs

have

been

used

in

the

tests

A

special

type

of

planar

graph

created

by

the

plane

function

found

in

the

Stanford

GraphBase

and

a

ladder

graph

see

Figure

The

planar

graphs

are

constructed

by

Delaunay

triangulation

of

randomly

inserted

points

in

a

plane

The

test

cases

are

generated

from

the

graphs

by

translating

the

graphs

into

propositional

logic

programs

using

the

techniques

introduced

in

Section

Since

the

SLG

system

is

goal-directed

we

must

add

some

rules

to

the

translations

of

the

logic

circuits

and

the

graph

problems

to

make

the

system

compute

the

whole

stable

model

We

add

the

rule

s

to

the

Hamiltonian

circuit

problem

For

each

vertex

a

and

each

color

i

we

add

the

rule

s

color

-LRB-

a

i

-RRB-

to

the

problem

Finally

for

each

atom

ab

that

determines

whether

a

gate

is

abnormal

we

add

a

rule

s

ab

to

the

diagnosis

problem

The

resulting

set

of

rules

is

then

used

when

testing

the

SLG

system

using

the

goal

s

and

the

algorithm

s

s

s

s

s

s

s

s

p

p

p

Figure

Ladder

Graph

The

two

benchmark

programs

found

in

-LSB-

-RSB-

have

also

been

used

to

generate

test

cases

The

first

benchmark

program

consists

of

the

following

rules

-LRB-

X

-RRB-

-LRB-

X

-RRB-

-LRB-

X

-RRB-

-LRB-

X

-RRB-

-LRB-

X

-RRB-

-LRB-

X

-RRB-

-LRB-

X

-RRB-

-LRB-

X

-RRB-

-LRB-

X

-RRB-

-LRB-

X

-RRB-

-LRB-

X

-RRB-

-LRB-

X

-RRB-

-LRB-

X

-RRB-

s

-LRB-

X

-RRB-

-LRB-

X

-RRB-

t

-LRB-

X

-RRB-

-LRB-

X

-RRB-

p

-LRB-

X

-RRB-

-LRB-

X

-RRB-

q

-LRB-

X

-RRB-

t

-LRB-

X

-RRB-

not

-LRB-

s

-LRB-

X

-RRB-

-RRB-

s

-LRB-

X

-RRB-

not

-LRB-

t

-LRB-

X

-RRB-

-RRB-

p

-LRB-

X

-RRB-

not

-LRB-

q

-LRB-

X

-RRB-

-RRB-

q

-LRB-

X

-RRB-

not

-LRB-

p

-LRB-

X

-RRB-

-RRB-

m

-LRB-

X

-RRB-

-LRB-

X

-RRB-

m

-LRB-

X

-RRB-

-LRB-

X

-RRB-

m

-LRB-

X

-RRB-

-LRB-

X

-RRB-

m

-LRB-

X

-RRB-

-LRB-

X

-RRB-

r

r

r

r

e

e

e

e

ecarry

out

carry

in

Figure

Ripple

Carry

Adder

Before

the

stable

models

are

computed

a

number

of

constants

are

introduced

and

the

program

is

grounded

i.e.

the

variables

in

the

rules

are

instantiated

with

the

constants

If

the

number

of

constants

is

n

the

number

of

stable

models

is

The

second

benchmark

program

consists

of

the

following

rules

s

-LRB-

X

-RRB-

p

-LRB-

X

-RRB-

q

-LRB-

X

-RRB-

s

-LRB-

X

-RRB-

p

-LRB-

X

-RRB-

r

-LRB-

X

-RRB-

s

-LRB-

X

-RRB-

q

-LRB-

X

-RRB-

r

-LRB-

X

-RRB-

p

-LRB-

X

-RRB-

not

-LRB-

q

-LRB-

X

-RRB-

-RRB-

q

-LRB-

X

-RRB-

not

-LRB-

r

-LRB-

X

-RRB-

-RRB-

r

-LRB-

X

-RRB-

not

-LRB-

p

-LRB-

X

-RRB-

-RRB-

We

introduce

a

number

of

constants

and

ground

the

program

This

program

does

not

have

any

stable

models

Both

benchmark

programs

are

very

simple

as

there

are

few

interdependencies

between

the

propositional

atoms

This

fact

is

clearly

reflected

in

the

timing

results

The

circuit

diagram

of

the

ripple

carry

adder

is

shown

in

Figure

and

the

circuit

diagram

of

the

parity

circuit

is

shown

in

Figure

We

set

all

inputs

of

the

ripple

carry

adder

and

the

parity

circuit

to

zero

In

order

to

get

diagnoses

we

set

the

last

output

of

the

adder

and

the

output

of

the

parity

circuit

to

one

The

results

are

tabulated

in

Figures

-LCB-

They

have

been

calculated

as

the

minimum

maximum

and

average

times

for

ten

different

runs

on

a

pseudorandomly

shuffled

set

of

rules

The

rationale

for

shuffling

the

set

of

rules

is

that

a

particular

ordering

of

the

rules

might

help

the

algorithm

to

avoid

backtracking

and

thus

give

a

skewed

picture

of

its

behavior

All

times

are

in

seconds

and

they

represent

the

time

to

find

one

stable

model

if

one

exists

or

the

time

to

decide

that

there

are

no

stable

models

As

an

exception

all

stable

models

of

the

first

benchmark

program

were

computed

A

blank

entry

indicates

that

the

corresponding

test

was

not

performed

All

tests

were

run

on

a

SPARCstation

IPX

workstation

with

MB

of

memory

and

the

SunOS

operating

system

The

SLG

system

was

compiled

in

d

d

d

r

r

r

d

eparity

e

e

e

r

r

r

r

r

Figure

Parity

Circuit

SICStus

with

the

fastcode

compilation-flag

set

The

C++

program

was

compiled

using

gcc

version

The

timing

was

handled

by

the

SICStus

command

statistics

in

SLG

and

by

the

C

function

ftime

in

The

test

results

show

quite

clearly

that

computes

the

stable

model

semantics

significantly

faster

than

the

SLG

system

We

can

explain

some

of

the

performance

dissimilarities

by

the

differing

implementation

techniques

is

implemented

in

C++

while

SLG

is

implemented

as

a

metainterpreter

on

top

of

Prolog

However

the

execution

times

display

in

spite

of

this

drastic

differences

The

more

efficient

pruning

technique

of

is

reflected

in

the

timing

results

of

the

and

the

Hamiltonian

circuit

tests

The

SLG

system

performs

poorly

on

the

tests

while

the

performance

of

is

much

better

The

SLG

system

does

not

perform

any

better

on

the

Hamiltonian

circuit

tests

The

performance

of

is

not

as

good

as

on

the

tests

but

can

take

better

advantage

of

the

simple

structure

of

the

ladder

graph

than

can

SLG

The

pruning

capabilities

of

the

SLG

system

and

do

not

play

an

important

role

in

the

simple

benchmark

tests

The

differences

in

these

tests

are

better

explained

by

the

different

implementation

techniques

The

SLG

system

prunes

the

search

space

by

reducing

the

program

This

probably

adds

more

overhead

than

what

the

direct

computation

of

deductive

closures

adds

for

the

algorithm

The

logic

circuit

tests

demonstrate

an

intermediate

level

of

complexity

The

ripple

carry

adders

are

easily

handled

by

both

algorithms

The

parity

circuits

Ladder

graphs

Vertices

Rules

SLG

min

max

ave

min

max

ave

Planar

graphs

Vertices

Rules

SLG

min

max

ave

min

max

ave

Figure

Test

Results

on

the

other

hand

are

easy

tests

for

the

algorithm

but

hard

tests

for

the

SLG

system

To

give

an

idea

of

the

size

of

the

search

spaces

we

note

that

the

search

space

of

the

program

P

is

all

subsets

of

the

set

NAnt

-LRB-

P

-RRB-

i.e.

all

sets

of

atoms

that

might

be

a

stable

model

Thus

if

there

are

n

atoms

in

NAnt

-LRB-

P

-RRB-

then

the

size

of

the

search

space

is

For

example

the

encoding

of

the

problem

for

the

ladder

graph

of

vertices

contains

not-atoms

These

atoms

produce

a

search

space

of

size

which

is

roughly

of

the

order

Of

course

the

number

of

of

the

ladder

graph

of

vertices

is

which

is

about

This

fact

alone

should

make

it

possible

for

an

algorithm

to

reduce

the

search

space

quite

a

lot

Ladder

graphs

Hamiltonian

circuit

Vertices

Rules

SLG

min

max

ave

min

max

ave

Planar

graphs

Hamiltonian

circuit

Vertices

Rules

SLG

min

max

ave

min

max

ave

12 130 139

14 156 1145

SLG

benchmark

all

stable

models

Constants

Rules

SLG

min

max

ave

min

max

ave

Figure

Test

Results

SLG

benchmark

Constants

Rules

SLG

min

max

ave

min

max

ave

Ripple

carry

adder

Bits

Rules

SLG

min

max

ave

min

max

ave

Parity

circuit

Bits

Rules

SLG

min

max

ave

min

max

ave

Figure

Test

Results

Conclusions

In

this

work

we

have

made

improvements

on

an

algorithm

of

Niemela

-LSB-

-RSB-

for

computing

stable

models

of

propositional

logic

programs

The

improvements

consists

of

removing

redundancies

from

the

algorithm

and

strengthening

the

ability

of

the

algorithm

to

prune

the

search

space

Removing

redundancies

simplifies

the

algorithm

and

has

a

slight

impact

on

the

performance

of

the

algorithm

A

simpler

algorithm

is

also

easier

to

implement

Improving

the

pruning

capabilities

has

the

potential

of

drastically

increasing

the

performance

of

the

algorithm

since

the

search

space

is

exponentially

dependent

on

the

size

of

the

input

in

the

worst

case

We

have

examined

how

the

algorithm

computes

stable

models

of

logic

programs

with

a

total

well-founded

model

The

algorithm

computes

the

well-founded

model

roughly

in

time

proportional

to

the

square

of

the

size

of

the

input

The

set

of

logic

programs

with

a

total

well-founded

model

contains

the

important

set

of

stratified

logic

programs

As

it

is

possible

to

compute

the

total

wellfounded

model

of

a

stratified

logic

program

in

time

proportional

to

the

size

of

the

program

the

question

whether

the

same

time

complexity

bound

is

attainable

for

programs

with

a

total

well-founded

model

naturally

arises

Finding

a

tight

lower

bound

for

the

time

complexity

needed

to

compute

a

well-founded

model

is

an

interesting

area

of

further

research

We

have

given

a

simple

upper

bound

on

the

time

complexity

of

the

algorithm

We

have

also

shown

that

the

algorithm

has

worst-case

exponential

time

complexity

This

was

not

surprising

since

the

problem

the

algorithm

solves

is

NP-complete

The

problem

of

finding

a

better

upper

bound

is

primarily

connected

to

the

problem

of

computing

the

well-founded

model

semantics

The

final

question

regarding

the

time

complexity

is

of

course

not

answered

until

the

tantalizing

open

problem

concerning

the

structure

of

the

polynomial

time

hierarchy

is

solved

In

connection

with

the

problem

of

computing

well-founded

models

we

have

presented

two

linear

time

algorithms

for

computing

deductive

closures

of

logic

programs

The

algorithms

are

based

on

a

linear

time

algorithm

of

Dowling

and

Gallier

-LSB-

-RSB-

We

have

shown

that

the

first

algorithm

which

is

almost

identical

to

the

algorithm

of

Dowling

and

Gallier

can

be

used

to

incrementally

compute

the

deductive

closures

of

a

program

with

a

growing

set

of

literals

The

second

algorithm

have

been

shown

to

compute

the

deductive

closures

of

programs

with

a

shrinking

set

of

literals

more

efficiently

than

a

plain

recomputation

of

the

whole

closure

The

efficiency

with

which

the

deductive

closures

are

computed

immediately

affects

the

time

complexity

of

the

algorithm

computing

stable

models

The

dependency

graph

and

its

strongly

connected

components

can

be

used

to

efficiently

compute

the

stable

model

semantics

for

stratified

logic

programs

We

have

demonstrated

that

this

is

not

possible

in

the

general

case

Allowing

variables

in

the

logic

programs

can

reduce

the

number

of

rules

needed

to

encode

a

problem

thus

simplifying

the

work

of

the

programmer

By

limiting

the

use

of

variables

to

range

restricted

logic

programs

we

have

demonstrated

that

the

number

of

rules

in

the

ground

program

can

be

reduced

We

have

also

exhibited

logic

program

encodings

of

the

problem

of

finding

diagnoses

in

logic

circuits

and

of

finding

Hamiltonian

circuits

and

N-colorings

in

undirected

graphs

These

encodings

can

be

used

when

comparing

implementations

of

algorithms

for

computing

stable

models

Their

use

in

real

world

applications

is

limited

as

such

applications

often

contain

restrictions

that

are

hard

to

express

in

propositional

logic

programs

The

algorithm

presented

in

this

work

serves

however

as

a

good

base

for

implementations

handling

these

restrictions

The

algorithm

for

computing

the

stable

model

semantics

have

been

compared

to

three

other

algorithms

One

of

these

the

SLG

system

is

considered

the

state

of

the

art

among

the

algorithms

computing

stable

models

We

have

arrived

at

the

conclusion

that

the

algorithm

presented

in

this

work

surpasses

all

the

other

three

algorithms

in

performance

We

have

described

how

to

implement

the

algorithm

in

order

to

achieve

this

high

performance

and

we

have

developed

a

prototype

implementation

Finally

we

have

experimentally

compared

the

implementation

to

the

SLG

system

and

concluded

that

the

test

results

clearly

reflects

the

better

performance

of

the

algorithm

Acknowledgments

This

report

contains

results

of

my

work

in

the

Digital

Systems

Laboratory

at

Helsinki

University

of

Technology

from

February

to

August

I

thank

Professor

Leo

Ojala

and

Dr.Tech

Ilkka

Niemela

for

giving

me

the

opportunity

to

work

in

the

laboratory

I

am

very

grateful

to

Dr.Tech

Ilkka

Niemela

for

his

comments

and

advice

I

also

thank

my

colleagues

in

the

laboratory

Otaniemi

September

Patrik

Simons

References

-LSB-

-RSB-

K.R.

Apt

H.A.

Blair

and

A.

Walker

Towards

a

theory

of

declarative

knowledge

In

J.

Minker

editor

Foundations

of

Deductive

Databases

and

Logic

Programming

pages

-LCB-

Morgan

Kaufmann

Publishers

Los

Altos

-LSB-

-RSB-

N.

Bidoit

and

C.

Froidevaux

Negation

by

default

and

unstratified

logic

programs

Theoretical

Computer

Science

-LCB-

-LSB-

-RSB-

C.

Bell

A.

Nerode

R.T.

Ng

and

V.S.

Subrahmanian

Mixed

integer

programming

methods

for

computing

nonmonotonic

deductive

databases

Journal

of

the

ACM

-LRB-

-RRB-

-LCB-

November

-LSB-

-RSB-

A.

Chandra

and

D.

Harel

Horn

clause

queries

and

generalizations

Journal

of

Logic

Programming

-LCB-

-LSB-

-RSB-

K.L.

Clark

Negation

as

failure

In

H.

Gallaire

and

J.

Minker

editors

Logic

and

Data

Bases

pages

-LCB-

Plenum

Press

New

York

-LSB-

-RSB-

W.

Chen

and

D.S.

Warren

The

SLG

system

Available

by

ftp

from

seas.smu.edu

pub

or

sbcs.sunysv.edu

pub/XSB

-LSB-

-RSB-

W.

Chen

and

D.S.

Warren

Computation

of

stable

models

and

its

integration

with

logical

query

processing

Available

by

ftp

from

ftp.ms.uky.edu

-LSB-

-RSB-

W.F.

Dowling

and

J.H.

Gallier

Linear-time

algorithms

for

testing

the

satisfiability

of

propositional

Horn

formulae

Journal

of

Logic

Programming

-LCB-

-LSB-

-RSB-

J.

de

Kleer

An

assumption-based

TMS

Artificial

Intelligence

-LCB-

-LSB-

-RSB-

J.

Doyle

A

truth

maintenance

system

Artificial

Intelligence

-LCB-

-LSB-

-RSB-

T.

Eiter

and

G.

Gottlob

The

complexity

of

logic-based

abduction

Journal

of

the

ACM

-LRB-

-RRB-

-LCB-

January

-LSB-

-RSB-

C.

Elkan

A

rational

reconstruction

of

nonmonotonic

truth

maintenance

systems

Artificial

Intelligence

-LCB-

-LSB-

-RSB-

K.

Eshghi

Computing

stable

models

by

using

the

ATMS

In

Proceedings

of

the

National

Conference

on

Artificial

Intelligence

pages

-LCB-

Boston

MA

USA

July

The

MIT

Press

-LSB-

-RSB-

M.

Gelfond

On

stratified

autoepistemic

theories

In

Proceedings

of

the

National

Conference

on

Artificial

Intelligence

pages

-LCB-

Seattle

USA

July

American

Association

for

Artificial

Intelligence

-LSB-

-RSB-

M.R.

Garey

and

D.S.

Johnson

Computers

and

Intractability

W.H.

Freeman

and

Company

San

Francisco

-LSB-

-RSB-

M.

Gelfond

and

V.

Lifschitz

The

stable

model

semantics

for

logic

programming

In

Proceedings

of

the

International

Conference

on

Logic

Programming

pages

-LCB-

Seattle

USA

August

The

MIT

Press

-LSB-

-RSB-

D.E.

Knuth

The

Stanford

GraphBase

A

collection

of

programs

and

data

Available

by

ftp

from

labrea.stanford.edu

pub/sgb

-LSB-

-RSB-

V.

Lifschitz

On

the

declarative

semantics

of

logic

programs

with

negation

In

J.

Minker

editor

Foundations

of

Deductive

Databases

and

Logic

Programming

pages

-LCB-

Morgan

Kaufmann

Publishers

Los

Altos

-LSB-

-RSB-

J.W.

Lloyd

Foundations

of

Logic

Programming

Springer-Verlag

Berlin

-LSB-

-RSB-

J.

McCarthy

Circumscription

a

form

of

non-monotonic

reasoning

Artificial

Intelligence

-LCB-

-LSB-

-RSB-

J.

McCarthy

Applications

of

circumscription

to

formalizing

commonsense

knowledge

Artificial

Intelligence

-LCB-

-LSB-

-RSB-

R.C.

Moore

Semantical

considerations

on

nonmonotonic

logic

Artificial

Intelligence

-LCB-

-LSB-

-RSB-

W.

Marek

and

V.S.

Subrahmanian

The

relationship

between

stable

supported

default

and

autoepistemic

semantics

for

general

logic

programs

Theoretical

Computer

Science

-LRB-

-RRB-

-LCB-

September

-LSB-

-RSB-

W.

Marek

and

M.

Truszczy?nski

Autoepistemic

logic

Journal

of

the

ACM

-LCB-

-LSB-

-RSB-

I.

Niemela

A

decision

method

for

nonmonotonic

reasoning

based

on

autoepistemic

reasoning

In

Proceedings

of

the

International

Conference

on

Principles

of

Knowledge

Representation

and

Reasoning

pages

-LCB-

Bonn

Germany

May

Morgan

Kaufmann

Publishers

-LSB-

-RSB-

I.

Niemela

Towards

efficient

default

reasoning

In

Proceedings

of

the

International

Joint

Conference

on

Artificial

Intelligence

pages

-LCB-

Montreal

Canada

August

Morgan

Kaufmann

Publishers

-LSB-

-RSB-

I.

Niemela

and

J.

Rintanen

On

the

impact

of

stratification

on

the

complexity

of

nonmonotonic

reasoning

Journal

of

Applied

NonClassical

Logics

-LRB-

-RRB-

-LCB-

-LSB-

-RSB-

I.

Niemela

and

P.

Simons

Evaluating

an

algorithm

for

default

reasoning

Accepted

to

be

presented

at

the

IJCAI

Workshop

on

Applications

and

Implementations

of

Nonmonotonic

Reasoning

Systems

August

-LSB-

-RSB-

H.

Przymusinska

and

T.C.

Przymusinski

Weakly

perfect

model

semantics

for

logic

programs

In

K.A.

Bowen

and

R.A.

Kowalski

editors

Proceedings

of

the

International

Conference

and

Symposium

on

Logic

Programming

pages

-LCB-

Cambridge

MA

USA

The

MIT

Press

-LSB-

-RSB-

S.G.

Pimentel

and

W.L.

Rodi

A

nonmonotonic

assumption-based

TMS

using

stable

bases

In

Proceedings

of

the

International

Conference

on

Principles

of

Knowledge

Representation

and

Reasoning

pages

-LCB-

Cambridge

MA

USA

April

Morgan

Kaufmann

Publishers

-LSB-

-RSB-

T.C.

Przymusinski

On

the

declarative

semantics

of

stratified

deductive

databases

and

logic

programs

In

J.

Minker

editor

Foundations

of

Deductive

Databases

and

Logic

Programming

pages

-LCB-

Morgan

Kaufmann

Publishers

-LSB-

-RSB-

C.H.

Papadimitriou

and

M.

Yannakakis

Tie-breaking

semantics

and

structural

totality

In

Proceedings

of

the

Symposium

on

Principles

of

Database

Systems

New

York

June

ACM

-LSB-

-RSB-

R.

Reiter

On

closed

world

data

bases

In

H.

Gallaire

and

J.

Minker

editors

Logic

and

Data

Bases

pages

-LCB-

Plenum

Press

New

York

-LSB-

-RSB-

R.

Reiter

A

logic

for

default

reasoning

Artificial

Intelligence

-LCB-

-LSB-

-RSB-

R.

Reiter

A

theory

of

diagnosis

from

first

principles

Artificial

Intelligence

-LCB-

-LSB-

-RSB-

J.C.

Shepherdson

Negation

in

logic

programming

In

J.

Minker

editor

Foundations

of

Deductive

Databases

and

Logic

Programming

pages

-LCB-

Morgan

Kaufmann

Publishers

Los

Altos

-LSB-

-RSB-

V.S.

Subrahmanian

D.

Nau

and

C.

Vago

Wfs

branch

and

bound

stable

models

IEEE

Transactions

on

Knowledge

and

Data

Engineering

-LRB-

-RRB-

-LCB-

June

-LSB-

-RSB-

A.

Van

Gelder

Negation

as

failure

using

tight

derivations

for

general

logic

programs

In

J.

Minker

editor

Foundations

of

Deductive

Databases

and

Logic

Programming

pages

-LCB-

Morgan

Kaufmann

Publishers

Los

Altos

-LSB-

-RSB-

A.

Van

Gelder

K.A.

Ross

and

J.S.

Schlipf

Unfounded

sets

and

the

well-founded

semantics

for

general

logic

programs

extended

abstract

In

Proceedings

of

the

Symposium

on

Principles

of

Database

Systems

pages

-LCB-

Austin

Texas

March

ACM

Press

-LSB-

-RSB-

A.

Van

Gelder

K.A.

Ross

and

J.S.

Schlipf

The

well-founded

semantics

for

general

logic

programs

Journal

of

the

ACM

-LRB-

-RRB-

-LCB-

July

logic programming
nonmonotonic reasoning
stable model semantics

automatic theorem proving
well-founded semantics