A

Framework

of

Directionality

for

Proving

Termination

of

Logic

Programs

Francois

Bronsard

T.K.

Uday

S.

Reddy

Department

of

Computer

Science

University

of

Illinois

at

Urbana-Champaign

Urbana

Illinois

USA

internet

hbronsard,lakshman,reddyi@cs.uiuc.edu

Abstract

In

this

paper

we

propose

a

rich

notion

of

directionality

of

predicates

that

combines

modes

and

regular

tree

types

We

provide

a

semantic

soundness

result

for

this

notion

and

give

inference

systems

to

decide

well-modedness

of

logic

programs

and

goals

We

show

how

this

rich

notion

of

directionality

can

be

used

to

prove

the

universal

termination

of

LD-resolution

for

logic

programs

with

non-ground

goals

by

using

simple

syntactic

orderings

of

the

kind

used

in

rewriting

theory

Introduction

Proof

techniques

for

proving

of

logic

programs

have

recently

emerged

as

a

focus

of

attention

-LSB-

-RSB-

A

key

concept

in

the

study

of

termination

of

logic

programs

is

the

notion

of

the

directionality

of

a

predicate

i.e.

the

designation

of

some

arguments

to

the

predicate

as

input

and

other

arguments

as

output

Directionality

is

normally

specified

via

notions

of

modes

of

predicates

and

well-modedness

of

programs

and

goals

In

general

the

mode

information

only

specifies

which

arguments

are

expected

to

be

ground

-LRB-

these

are

the

input

arguments

-RRB-

and

which

arguments

are

expected

to

be

non-ground

-LRB-

these

are

the

output

arguments

-RRB-

-LRB-

-LSB-

-RSB-

-RRB-

This

ground/non-ground

distinction

which

is

almost

always

used

to

express

directionality

is

however

too

rigid

for

most

programs

For

example

the

classical

program

for

append

terminates

if

the

first

-LRB-

or

the

third

-RRB-

argument

is

a

finite

list

independently

of

whether

the

elements

of

this

list

are

ground

or

not

In

this

work

we

generalize

the

original

notion

of

ground/non-ground

modes

by

combining

modes

and

a

subset

of

the

regular

tree

types

-LSB-

-RSB-

to

define

mode-annotations

Modeannotations

describe

sets

of

possibly

non-ground

terms

sharing

a

common

structure

For

example

the

mode-annotation

describing

a

set

of

arbitrary

terms

-LRB-

including

variables

-RRB-

is

at

IBM

Almaden

Research

Center

-LRB-

lakshman@almaden.ibm.com

-RRB-

Termination

is

taken

to

mean

universal

termination

-LSB-

-RSB-

i.e.

every

branch

of

the

SLD-tree

is

finite

lists

of

arbitrary

terms

have

the

mode-annotation

list

-LRB-

-RRB-

The

essential

difference

between

our

approach

and

-LSB-

-RSB-

is

that

mode-annotations

describe

sets

of

-LRB-

possibly

nonground

-RRB-

terms

whereas

types

include

only

ground

terms

Thus

the

syntax

of

mode-annotations

includes

the

additional

symbol

which

represents

arbitrary

terms

including

non-ground

terms

Hence

these

mode-annotations

provide

a

formal

basis

for

studying

termination

of

non-ground

goals

In

Section

we

discuss

the

possibility

of

using

the

full

regular

tree

types

of

-LSB-

-RSB-

to

give

directionality

to

programs

using

partially

instantiated

structures

such

as

difference

lists

Absence

of

directionality

distinctions

has

been

cited

as

one

of

the

strengths

of

logic

programming

Since

non-directional

predicate

definitions

can

be

reused

with

different

directionalities

non-directionality

is

indeed

an

advantage

in

terms

of

code

size

and

conciseness

However

properties

such

as

complexity

and

termination

do

depend

on

the

directionality

of

predicates

and

hence

need

to

be

verified

afresh

for

each

directionality

Furthermore

most

predicate

definitions

can

only

be

used

with

specific

directionalities

We

thus

view

a

predicate

as

a

directional

procedure

that

when

applied

to

a

tuple

of

terms

satisfying

an

input

mode

generates

a

tuple

of

terms

satisfying

a

more

output

mode

In

Section

we

propose

a

notion

of

mode-dependency

for

a

predicate

that

defines

such

a

relationship

between

the

structure

of

inputs

and

outputs

of

the

predicate

An

assignment

of

mode-dependencies

to

predicates

is

then

used

to

syntactically

define

the

notion

of

well-moded

clauses

and

goals

Well-modedness

is

justified

by

a

semantic

soundness

result

that

says

that

when

a

well-moded

goal

is

resolved

with

a

well-moded

program

if

its

inputs

are

restricted

to

satisfy

the

assigned

mode-dependency

then

after

resolution

the

outputs

also

satisfy

the

assigned

mode-dependency

Though

we

assume

the

standard

Prolog

computation

rule

the

results

extend

to

any

other

choice

of

a

computation

rule

Similarly

we

assume

that

predicates

are

assigned

a

unique

mode-dependency

-LRB-

or

equivalently

that

each

mode-dependency

defines

a

different

version

of

the

predicate

-RRB-

although

again

our

results

extend

to

multiple

mode-dependencies

for

predicates

The

second

part

of

this

paper

deals

with

the

use

of

well-founded

orderings

to

prove

termination

Typical

proofs

of

termination

of

programs

involve

showing

that

each

step

of

the

computation

represents

a

reduction

according

to

a

well-founded

ordering

Formally

this

is

expressed

for

logic

programs

as

follows

Remark

A

logic

program

P

universally

terminates

if

and

only

if

there

exists

a

well-founded

ordering

O

such

that

for

all

goals

P

implies

O

Two

observations

arise

from

this

remark

First

for

a

program

that

contains

recursive

predicates

it

is

not

normally

possible

to

show

universal

termination

unless

there

are

some

restrictions

on

goals

For

example

given

the

usual

definition

of

the

predicate

append

from

the

goal

append

-LRB-

X

Y

Z

-RRB-

we

can

infer

the

new

goal

append

-LRB-

X

Y

-RRB-

Since

this

new

goal

is

equivalent

to

the

initial

goal

up

to

renaming

of

variables

there

can

not

exist

a

well-founded

More

specific

intuitively

means

that

mode-annotations

of

the

form

are

replaced

by

mode-annotations

such

as

ground

or

list

-LRB-

-RRB-

P

denotes

one

step

of

SLD

resolution

ordering

O

on

goals

such

that

append

-LRB-

X

Y

Z

-RRB-

O

append

-LRB-

X

Y

-RRB-

Consequently

such

goals

should

not

be

allowed

Secondly

universal

termination

requires

showing

termination

of

all

possible

goals

that

can

arise

during

execution

To

be

practical

a

proof

technique

to

show

termination

must

instead

rely

on

the

static

structure

of

the

program

for

instance

relationships

between

heads

and

bodies

of

clauses

Hence

we

must

insure

that

the

relationships

between

heads

and

bodies

of

clauses

can

be

translated

to

goals

In

Section

we

show

that

if

the

head

and

body

of

clauses

are

compared

but

the

comparison

is

restricted

to

the

structure

described

by

the

mode

then

the

ordering

relation

holds

for

any

well-moded

goal

To

restrict

orderings

to

the

structure

described

by

the

mode-dependency

we

define

a

transformation

o

which

given

a

term

and

a

mode-annotation

strips

the

term

of

any

structure

not

explicitly

indicated

in

the

mode-annotation

This

transformation

is

extended

to

atoms

We

can

then

use

syntactic

orderings

to

compare

the

head

and

the

body

of

clauses

In

Section

we

present

some

simple

syntactic

orderings

adapted

from

the

orderings

commonly

used

in

term

rewriting

theory

-LRB-

see

-LSB-

-RSB-

for

a

survey

-RRB-

Our

adaptation

of

these

orderings

allows

the

use

of

inter-argument

relationships

which

are

sometimes

needed

to

prove

termination

of

programs

with

local

variables

Consider

the

following

illustrative

example

Example

append

-LRB-

Y

Y

-RRB-

append

-LRB-

A

X

Y

A

Z

-RRB-

append

-LRB-

X

Y

Z

-RRB-

Let

us

assume

that

the

mode-dependency

of

append

is

append

-LRB-

list

-LRB-

-RRB-

list

-LRB-

-RRB-

-RRB-

append

-LRB-

list

-LRB-

-RRB-

list

-LRB-

-RRB-

list

-LRB-

-RRB-

-RRB-

The

transformation

o

applied

to

the

the

head

and

the

body

of

the

clause

gives

-LRB-

denotes

arbitrary

terms

-RRB-

o

-LRB-

append

-LRB-

A

X

Y

A

Z

-RRB-

append

-LRB-

list

-LRB-

-RRB-

list

-LRB-

-RRB-

-RRB-

-RRB-

append

-LRB-

X

Y

-RRB-

o

-LRB-

append

-LRB-

X

Y

Z

-RRB-

append

-LRB-

list

-LRB-

-RRB-

list

-LRB-

-RRB-

-RRB-

-RRB-

append

-LRB-

X

Y

-RRB-

We

can

conclude

using

some

syntactic

ordering

-LRB-

see

Section

-RRB-

that

append

-LRB-

X

Y

-RRB-

append

-LRB-

X

Y

-RRB-

Hence

using

this

clause

with

a

well-moded

goal

generates

a

smaller

goal

so

the

program

terminates

for

all

such

goals

A

similar

construction

shows

that

any

goal

in

the

input

mode

append

-LRB-

list

-LRB-

-RRB-

-RRB-

also

leads

to

termination

Alternatively

with

the

input

mode

append

-LRB-

-RRB-

the

result

of

the

transformation

is

o

-LRB-

append

-LRB-

A

X

Y

A

Z

-RRB-

append

-LRB-

-RRB-

-RRB-

append

-LRB-

-RRB-

o

-LRB-

append

-LRB-

X

Y

Z

-RRB-

append

-LRB-

-RRB-

-RRB-

append

-LRB-

-RRB-

In

this

case

as

expected

we

can

not

show

termination

of

the

program

are

also

called

inter-arguments

constraints

The

two

main

contributions

of

this

paper

are

first

the

adaptation

of

regular

tree

types

to

define

a

powerful

notion

of

modes

that

can

express

a

rich

notion

of

directionality

and

second

the

adaptation

of

syntactic

orderings

to

the

context

of

well-moded

logic

programs

and

their

use

to

prove

universal

Our

method

highlights

the

distinction

between

directionality

orderings

and

the

use

of

inter-argument

relationships

We

believe

that

our

notion

of

directionality

can

also

be

used

for

applications

other

than

termination

such

as

specifying

control

in

concurrent

logic

languages

Lastly

for

programs

that

use

partially

instantiated

structures

such

as

difference

lists

the

treatment

of

directionality

is

a

difficult

problem

We

suggest

in

section

an

extension

to

our

approach

to

give

directionality

to

such

programs

A

promising

alternative

which

we

are

studying

is

the

use

of

linear

logic

in

defining

directional

logic

programs

-LSB-

-RSB-

Related

work

The

problem

of

improving

directionality

information

i.e.

to

go

beyond

the

simple

ground/nonground

characterization

has

been

studied

by

many

recent

works

In

-LSB-

-RSB-

a

combination

of

modes

orderings

and

inter-argument

relationships

provides

a

more

powerful

but

more

abstract

notion

of

directionality

Informally

the

input

ground

idea

is

replaced

by

the

notion

input

terms

acceptable

by

the

ordering

relation

This

technique

resembles

our

use

of

the

transformation

o

with

the

mode

However

we

believe

that

there

are

advantages

in

distinguishing

between

the

issues

of

directionality

and

orderings

An

approach

similar

to

our

notion

of

modes

is

the

notion

of

integrated

types

of

-LSB-

-RSB-

which

have

been

used

in

the

context

of

proving

termination

of

programs

by

-LSB-

-RSB-

Although

our

approach

is

more

general

than

-LSB-

-RSB-

because

of

our

treatment

of

mode-annotations

and

orderings

the

main

differences

between

our

modes

and

the

integrated

types

appears

when

we

discuss

the

extension

of

our

modes

with

implication

and

quantification

in

section

We

show

termination

using

purely

syntactic

orderings

By

allowing

the

use

of

interargument

relationships

our

approach

can

profit

from

the

many

works

concerned

with

deriving

inter-argument

relationships

-LRB-

e.g.

-LSB-

-RSB-

-RRB-

For

instance

one

could

express

the

notion

of

propagating

inter-argument

relationships

to

prove

termination

-LRB-

as

in

-LSB-

-RSB-

-RRB-

by

appropriate

inference

systems

for

orderings

It

should

be

pointed

out

that

the

use

of

inter-argument

inequality

relationships

as

done

in

-LSB-

-RSB-

is

from

a

theoretical

point

of

view

more

powerful

than

our

use

of

syntactic

orderings

with

inter-argument

monotonicity

relationships

Naturally

our

method

can

be

adapted

to

use

inter-argument

relationships

it

is

only

a

matter

of

expressing

reasoning

with

these

interargument

relationships

in

term

of

inference

systems

An

ever

more

powerful

tool

would

be

the

use

of

polynomial

interpretations

-LRB-

see

-LSB-

-RSB-

-RRB-

with

inter-argument

polynomial

inequality

relationships

Reasoning

with

these

is

however

much

harder

so

in

practice

one

most

often

rely

on

syntactic

orderings

It

should

be

noted

that

even

though

our

approach

uses

only

syntactic

ordering

and

monotonicity

relationships

it

can

show

the

termination

of

all

the

classical

examples

such

as

quicksort

and

mergesort

and

even

of

examples

that

seemed

to

require

inter-argument

inequality

relationships

such

as

Plumer

's

PERM

example

or

the

arithmetic

expression

parser

the

use

of

syntactic

orderings

can

clearly

be

automated

we

do

not

address

the

issue

of

the

automation

of

our

treatment

of

modes

in

Section

This

is

also

noted

by

-LSB-

-RSB-

whose

approach

can

be

we

believe

simulated

in

our

methodology

Since

all

that

is

really

necessary

to

prove

termination

is

to

show

that

recursive

calls

to

predicates

correspond

to

some

reduction

many

authors

-LSB-

-RSB-

have

either

relied

on

the

call

graph

generated

by

the

program

or

on

the

treatment

of

the

whole

program

as

an

interdependent

system

needed

to

be

solved

In

comparison

in

this

work

we

rely

on

comparisons

between

head

and

body

of

clauses

Although

this

is

often

simpler

and

it

is

theoretically

sufficient

it

might

require

more

ordering

information

than

these

other

methods

In

practice

a

rapid

study

of

the

call

graph

if

only

to

check

that

there

are

indeed

some

recursive

predicates

should

be

part

of

our

method

as

well

The

approaches

of

-LSB-

-RSB-

transform

logic

programs

into

rewrite

systems

and

then

perform

termination

analysis

using

the

classical

tool

of

rewrite

theory

In

contrast

our

approach

is

more

straightforward

in

adapting

the

techniques

from

rewrite

theory

directly

to

logic

programs

We

believe

that

the

work

of

-LSB-

-RSB-

although

it

concentrates

on

ground

goals

could

provides

an

abstract

framework

characterizing

our

use

of

syntactic

orderings

The

use

of

syntactic

orderings

and

do

n't

care

types

is

also

suggested

in

-LSB-

-RSB-

That

approach

is

however

geared

toward

the

development

of

programs

rather

than

an

a

posteriori

test

of

termination

Mode-Annotations

Syntax

of

Mode-annotations

Terms

and

mode-annotations

are

defined

using

the

following

syntactic

categories

X

V

V

ariables

f

F

Function

symbols

t

T

Terms

M

Mode

variables

a

A

Mode

annotations

The

expressions

belonging

to

the

categories

T

and

A

are

defined

with

the

following

context-free

syntax

t

X

j

f

-LRB-

tk

-RRB-

a

ground

j

j

j

f

-LRB-

ak

-RRB-

j

j

fixff

a

We

impose

two

restrictions

on

the

syntax

of

mode-annotations

First

mode-annotations

must

be

closed

a-terms

i.e.

they

contain

no

free

occurrences

of

mode

variables

Second

they

must

be

discriminative

i.e

for

mode-annotations

expressed

as

a

sum

the

outermost

symbol

of

and

must

be

distinct

As

a

notational

convenience

we

allow

in

addition

to

ground

mode-annotations

such

as

int

whose

semantics

is

well

defined

as

a

subset

of

ground

terms

We

also

use

the

following

section

we

discuss

the

need

to

drop

this

condition

and

extend

mode-annotations

to

full

regular

trees

-LSB-

-RSB-

to

handle

partially

instantiated

structures

condition

-LRB-

taken

from

-LSB-

-RSB-

-RRB-

is

imposed

for

reasons

of

efficiency

-LRB-

see

Appendix

B

-RRB-

shorthand

notation

for

mode-annotations

that

describe

commonly

used

structures

list

-LRB-

a

-RRB-

fixff

-LRB-

a

ff

-RRB-

tree

-LRB-

a

-RRB-

fixff

-LRB-

node

-LRB-

a

ff

-RRB-

-RRB-

Note

that

list

-LRB-

a

-RRB-

denotes

the

set

of

lists

whose

elements

satisfy

the

annotation

a.

Similarly

tree

-LRB-

a

-RRB-

denotes

the

set

of

binary

trees

whose

elements

satisfy

the

annotation

a.

Terms

and

mode-annotations

are

syntactically

related

by

the

notion

of

well-modedness

of

terms

Well-modedness

is

defined

via

the

notion

of

mode

contexts

A

mode

context

is

a

finite

set

of

-LRB-

unique

-RRB-

mode-annotation

assertions

for

variables

each

of

the

form

X

a

Mode

contexts

are

needed

to

disambiguate

the

role

of

variables

in

non-ground

terms

For

instance

variables

occurring

in

atomic

goals

should

be

interpreted

as

terms

with

the

mode

annotation

while

in

the

head

of

a

clause

variables

are

intended

as

parameters

to

which

the

context

binds

mode-annotations

Definition

A

term

t

is

well-moded

in

a

mode-annotation

a

relatively

to

a

mode

context

-LRB-

denoted

by

t

a

-RRB-

if

the

judgement

t

a

is

derivable

using

the

inference

rules

given

in

Appendix

A.

For

example

fX

list

-LRB-

-RRB-

g

f

-LRB-

X

-RRB-

f

-LRB-

list

-LRB-

-RRB-

-RRB-

X

and

fX

list

-LRB-

a

-RRB-

Y

ag

Y

X

list

-LRB-

a

-RRB-

By

extension

we

write

tn

ang

as

an

abbreviation

for

and

tn

an

Definition

A

mode-annotation

covers

a

mode-annotation

-LRB-

written

-RRB-

if

is

derivable

using

the

inclusion

checking

rules

-LRB-

adapted

from

-LSB-

-RSB-

-RRB-

given

in

Appendix

B.

For

example

list

-LRB-

-RRB-

ground

and

The

following

properties

will

be

used

later

to

show

the

soundness

of

LD-resolution

relatively

to

the

notion

of

well-modedness

Proposition

If

f

-LRB-

tn

-RRB-

a

then

there

exists

an

such

that

-RRB-

-LSB-

n

-RSB-

ti

ai

and

-RRB-

for

any

set

of

terms

sng

-LSB-

n

-RSB-

si

ai

-RRB-

f

-LRB-

sn

-RRB-

a.

Proof

By

induction

on

the

length

of

the

derivation

of

t

a

where

t

f

-LRB-

tn

-RRB-

ffl

-LRB-

basis

-RRB-

length

The

only

possibility

is

the

use

of

the

introduction

rule

for

function

or

predicate

symbols

Thus

a

must

have

the

form

f

-LRB-

an

-RRB-

and

the

result

is

immediate

ffl

-LRB-

Induction

step

-RRB-

length

n

Consider

the

last

inference

Four

cases

are

possible

-LCB-

Introduction

of

a

function

or

predicate

symbol

as

above

mode

context

can

also

be

viewed

as

a

partial

mapping

V

A.

-LCB-

Introduction

of

a

The

last

inference

was

t

t

First

note

that

t

so

by

the

inductive

hypothesis

there

exist

an

such

that

ti

ai

Moreover

for

any

set

sng

si

ai

implies

f

-LRB-

sn

-RRB-

this

in

turn

implies

f

-LRB-

sn

-RRB-

-LCB-

Use

of

inclusion

b

a

The

last

inference

was

t

b

t

a

We

have

t

b

so

by

the

inductive

hypothesis

there

exist

an

such

that

ti

ai

Moreover

for

any

set

sng

si

ai

implies

f

-LRB-

sn

-RRB-

b

this

in

turn

implies

f

-LRB-

sn

-RRB-

a.

-LCB-

Introduction

of

fix

similar

to

the

above

cases

Proposition

-LRB-

Instantiation

I

-RRB-

If

t

a

then

t

a

where

is

any

substitution

Proof

By

induction

on

the

structure

of

t.

ffl

t

X

Then

a

Then

X

a

since

t

for

any

term

t.

ffl

t

f

-LRB-

tn

-RRB-

So

we

must

have

that

tn

an

-LRB-

by

prop.

-RRB-

By

the

inductive

hypothesis

tn

an

so

f

-LRB-

tn

-RRB-

a.

If

Xn

ang

then

f

-LRB-

-RRB-

-LRB-

Xn

-RRB-

ang

is

a

set

of

-LRB-

term

mode-annotation

-RRB-

pairs

that

denotes

the

application

of

a

substitution

to

the

mode

context

Lemma

-LRB-

Instantiation

II

-RRB-

If

t

a

and

then

t

a.

Proof

By

induction

on

the

structure

of

t.

ffl

t

X

If

-LRB-

X

a

-RRB-

then

X

a

by

hypothesis

If

X

dom

-LRB-

-RRB-

then

since

X

a

a

must

be

Hence

X

a

so

X

a.

ffl

t

f

-LRB-

tn

-RRB-

Using

the

inductive

hypothesis

on

ti

ai

we

obtain

ti

ai

Hence

t

a.

Lemma

-LRB-

lifting

-RRB-

If

t

a

then

there

exists

a

such

that

t

a

and

Proof

Let

us

first

define

the

following

transformation

of

a

term

and

a

mode

context

into

a

mode-annotation

ae

-LRB-

X

-RRB-

-LRB-

if

X

dom

-LRB-

-RRB-

-LRB-

X

-RRB-

otherwise

ae

-LRB-

f

-LRB-

tn

-RRB-

-RRB-

f

-LRB-

ae

-LRB-

-RRB-

ae

-LRB-

tn

-RRB-

-RRB-

ae

satisfies

two

important

properties

ffl

t

ae

-LRB-

t

-RRB-

ffl

If

t

a

then

ae

-LRB-

t

-RRB-

a

This

can

be

proved

by

induction

Now

we

can

prove

the

lemma

by

constructing

the

appropriate

using

induction

on

t.

ffl

t

X

Let

fX

ae

-LRB-

X

-RRB-

Hence

we

have

since

X

ae

-LRB-

X

-RRB-

and

t

a

since

ae

-LRB-

t

-RRB-

a.

ffl

t

f

-LRB-

tn

-RRB-

Let

S

i

n

i

where

the

i

are

recursively

constructed

using

the

ti

and

We

trivially

have

that

this

construction

is

well-defined

and

that

Again

we

rely

on

the

property

that

there

exist

mode-annotations

an

such

that

for

all

i

-LSB-

n

-RSB-

ti

ai

to

conclude

that

ti

ai

and

so

that

t

a.

Semantics

of

Mode-annotations

As

before

let

T

denote

the

set

of

all

-LRB-

possibly

non-ground

-RRB-

terms

Note

that

-LRB-

-RRB-

is

a

reflexive

partial

order

where

is

the

set-inclusion

relation

on

sets

of

terms

Let

H

denote

the

Herbrand

universe

of

ground

terms

Definition

The

semantic

function

for

mode-annotations

-LSB-

-LSB-

-RSB-

-RSB-

A

is

defined

using

mode

valuations

M

as

follows

ffl

-LSB-

-LSB-

-RSB-

-RSB-

T

ffl

-LSB-

-LSB-

-RSB-

-RSB-

-LRB-

-RRB-

ffl

-LSB-

-LSB-

ground

-RSB-

-RSB-

H.

ffl

-LSB-

-LSB-

f

-LRB-

ak

-RRB-

-RSB-

-RSB-

ff

-LRB-

tk

-RRB-

j

-LSB-

-LSB-

-RSB-

-RSB-

tk

-LSB-

-LSB-

ak

-RSB-

-RSB-

g

ffl

-LSB-

-LSB-

-RSB-

-RSB-

-LSB-

-LSB-

-RSB-

-RSB-

-LSB-

-LSB-

-LSB-

-RSB-

-RSB-

ffl

-LSB-

-LSB-

fixff

a

-RSB-

-RSB-

the

least

S

T

such

that

S

-LSB-

-LSB-

a

-RSB-

-RSB-

-LRB-

-LSB-

S

ff

-RSB-

-RRB-

Since

we

restrict

mode-annotations

to

closed

a-terms

the

mode

valuation

is

only

a

notational

formalism

that

is

convenient

in

defining

the

semantics

of

fix

In

what

follows

we

generally

omit

For

example

the

semantics

of

recursively

defined

lists

and

trees

is

-LSB-

-LSB-

list

-LRB-

a

-RRB-

-RSB-

-RSB-

the

least

S

T

such

that

S

and

if

s

S

then

t

s

S

for

all

t

-LSB-

-LSB-

a

-RSB-

-RSB-

-LSB-

-LSB-

tree

-LRB-

a

-RRB-

-RSB-

-RSB-

the

least

S

T

such

that

S

and

if

s

S

then

node

-LRB-

t

s

-RRB-

S

for

all

t

-LSB-

-LSB-

a

-RSB-

-RSB-

We

can

now

provide

a

semantic

interpretation

for

t

a.

First

since

the

variables

in

the

domain

of

are

intended

as

parameters

we

need

the

notion

of

valuations

A

valuation

is

a

partial

mapping

V

T

A

valuation

is

said

to

respect

a

mode

context

if

and

have

the

same

domain

and

for

all

X

Dom

-LRB-

-RRB-

-LRB-

X

-RRB-

-LSB-

-LSB-

-LRB-

-LRB-

X

-RRB-

-RRB-

-RSB-

-RSB-

A

valuation

is

identified

with

its

extension

to

terms

Therefore

the

semantics

counterpart

of

t

a

is

the

following

for

all

valuations

which

respect

-LRB-

t

-RRB-

-LSB-

-LSB-

a

-RSB-

-RSB-

Mode

assignment

to

programs

An

n-ary

mode

is

an

n-tuple

over

mode-annotations

An

n-ary

mode

for

an

n-ary

predicate

p

is

syntactically

specified

as

p

-LRB-

an

-RRB-

Definition

An

n-ary

mode-dependency

is

a

pair

of

n-ary

modes

-LRB-

written

mi

mo

-RRB-

where

mi

-LRB-

mo

-RRB-

is

called

the

input

-LRB-

output

-RRB-

mode

Mode-dependencies

for

a

predicate

specify

the

structure

of

the

input

and

output

arguments

to

the

predicate

-LRB-

treated

as

a

directional

procedure

-RRB-

A

moded

clause

-LRB-

goal

-RRB-

is

a

clause

-LRB-

goal

-RRB-

together

with

an

assignment

of

mode-dependencies

to

every

literal

occurring

in

the

clause

-LRB-

goal

-RRB-

The

notion

of

a

well-moded

clause

is

now

defined

Definition

A

moded

clause

A

Bn

with

the

assigned

mode-dependencies

mia

moa

miBn

moBn

is

well-moded

if

for

all

mode

contexts

the

following

hold

n

A

mia

Bj

moBj

implies

Bj

miBj

A

mia

Bn

moBn

implies

A

moa

A

logic

program

P

is

well-moded

if

all

clauses

in

P

are

well-moded

For

example

the

program

defining

append

in

example

is

well-moded

with

respect

to

the

assigned

mode-dependency

for

append

Definition

A

moded

goal

Cn

is

well-moded

if

the

following

hold

For

all

mode

contexts

and

for

all

j

-LSB-

n

-RSB-

Cj

moCj

implies

Cj

miCj

For

example

with

the

mode-dependency

for

append

assumed

in

section

the

goals

append

-LRB-

-LSB-

X

-RSB-

-LSB-

Y

-RSB-

Z

-RRB-

and

append

-LRB-

-LSB-

X

-RSB-

-LSB-

Y

-RSB-

Z

-RRB-

append

-LRB-

Z

-LSB-

Y

-RSB-

U

-RRB-

are

well-moded

The

well-modedness

of

a

moded

clause

or

a

moded

goal

can

be

checked

as

follows

For

each

implication

condition

in

the

definition

of

well-modedness

first

the

elimination

rules

given

in

valuation

can

also

be

viewed

as

a

substitution

Appendix

A

are

used

to

construct

a

most

general

mode

context

in

which

the

atom

-LRB-

s

-RRB-

in

the

antecedent

of

the

implication

are

in

their

assigned

modes

Next

this

mode

context

is

used

to

check

-LRB-

using

the

introduction

rules

-RRB-

whether

the

atom

in

the

consequent

is

in

its

assigned

mode

Note

that

there

can

be

multiple

assignments

of

mode-dependencies

for

predicates

for

which

the

program

is

well-moded

For

example

even

with

the

mode-dependency

append

-LRB-

list

-LRB-

-RRB-

-RRB-

append

-LRB-

list

-LRB-

-RRB-

list

-LRB-

-RRB-

list

-LRB-

-RRB-

-RRB-

the

program

defining

append

in

Section

is

well-moded

On

the

other

hand

with

the

alternate

mode-dependency

reverse

-LRB-

list

-LRB-

-RRB-

-RRB-

reverse

-LRB-

list

-LRB-

-RRB-

list

-LRB-

-RRB-

-RRB-

the

second

clause

defining

reverse

in

Section

is

not

well-moded

The

body

literals

in

the

clause

have

to

be

exchanged

in

order

for

the

clause

to

be

well-moded

with

the

above

modedependency

We

view

the

resulting

program

for

reverse

as

having

a

directionality

that

is

distinct

from

the

original

version

Though

from

now

on

we

assume

that

a

unique

mode-dependency

is

assigned

to

each

predicate

occurring

in

a

program

our

approach

can

easily

be

extended

to

handle

multiple

mode-dependencies

for

predicates

Throughout

the

rest

of

this

paper

programs

and

goals

denote

well-moded

programs

and

well-moded

goals

The

following

theorems

express

the

correctness

of

the

notion

of

mode-dependency

Theorem

Mode-consistency

of

LD

resolution

One

step

of

LD

resolution

transforms

a

well-moded

goal

into

another

well-moded

goal

Proof

Consider

a

well-moded

goal

Bn

Let

be

the

selected

atom

and

A

where

A

Am

is

a

well-moded

clause

The

new

goal

derived

from

one

step

of

SLD

resolution

is

Am

Bn

We

must

show

that

this

new

goal

is

also

well-moded

i.e.

show

that

ffl

Since

-LRB-

the

initial

goal

is

well-moded

-RRB-

-RRB-

-LRB-

by

proposition

-RRB-

-RRB-

A

miA

-LRB-

since

A

-RRB-

-RRB-

such

that

A

miA

and

-LRB-

by

lemma

-RRB-

-RRB-

-LRB-

since

A

An

is

a

well-moded

clause

-RRB-

-RRB-

-LRB-

by

lemma

-RRB-

ffl

for

all

Aj

moAj

-RRB-

Aj

miAj

From

the

antecedent

and

since

A

miA

-RRB-

A

miA

-LRB-

trivially

-RRB-

-RRB-

such

that

A

miA

Aj

moAj

and

-LRB-

by

lemma

-RRB-

-RRB-

Aj

miAj

-LRB-

since

A

An

is

a

well-moded

clause

-RRB-

-RRB-

Aj

miAj

-LRB-

by

lemma

-RRB-

ffl

for

all

Am

moAm

-RRB-

and

so

on

From

the

antecedent

and

since

A

miA

-RRB-

A

miA

-RRB-

A

moA

-LRB-

since

A

An

is

a

well-moded

clause

-RRB-

-RRB-

-LRB-

since

A

-RRB-

-RRB-

such

that

and

-LRB-

by

lemma

-RRB-

-RRB-

-LRB-

since

the

original

goal

is

well-moded

-RRB-

-RRB-

-LRB-

by

lemma

-RRB-

From

the

above

theorem

we

see

that

LD

resolution

preserves

well-modedness

of

goals

Theorem

Correctness

of

Mode

dependency

Consider

a

well-moded

atomic

goal

p

-LRB-

tn

-RRB-

with

an

assigned

mode-dependency

p

mip

mop

If

the

LD

resolution

of

this

goal

with

respect

to

a

well-moded

logic

program

succeeds

with

a

computed

answer

substitution

then

p

-LRB-

tn

-RRB-

mo

Proof

By

induction

on

the

length

of

the

SLD

refutation

ffl

length

There

exists

a

well-moded

clause

p

-LRB-

un

-RRB-

p

-LRB-

min

-RRB-

p

-LRB-

mon

-RRB-

such

that

p

-LRB-

tn

-RRB-

p

-LRB-

un

-RRB-

Thus

p

-LRB-

tn

-RRB-

p

-LRB-

min

-RRB-

-RRB-

p

-LRB-

tn

-RRB-

p

-LRB-

min

-RRB-

-LRB-

by

proposition

-RRB-

-RRB-

p

-LRB-

un

-RRB-

p

-LRB-

min

-RRB-

-RRB-

p

-LRB-

un

-RRB-

p

-LRB-

mon

-RRB-

-LRB-

since

p

-LRB-

un

-RRB-

is

well-moded

-RRB-

ffl

length

There

exists

a

well-moded

clause

A

An

such

that

the

goal

p

-LRB-

tn

-RRB-

unifies

with

the

head

of

the

clause

The

new

goal

is

An

which

is

well-moded

-LRB-

by

Lemma

-RRB-

By

induction

hypothesis

each

of

the

subgoals

An

succeed

with

the

results

-LRB-

Aioe

-RRB-

in

the

corresponding

output

mode

Anoe

moAn

Hence

there

exists

a

such

that

An

moAn

and

-LRB-

by

lemma

-RRB-

Furthermore

A

miA

-LRB-

since

the

goal

is

well-moded

i.e.

A

miA

-RRB-

Hence

A

moA

-LRB-

since

the

clause

is

well-moded

-RRB-

Hence

A

moA

-LRB-

by

lemma

-RRB-

-RRB-

p

-LRB-

tn

-RRB-

mop

Characterizing

orderings

to

prove

termination

Simplifying

terms

with

mode-annotations

To

prove

termination

it

is

not

enough

to

show

that

the

head

of

a

clause

is

greater

than

the

body

since

goals

that

unify

with

the

head

of

a

clause

might

be

more

general

than

the

head

itself

Consequently

we

need

to

compare

generalized

versions

of

the

head

and

the

body

These

generalizations

depend

on

the

mode-dependency

of

the

predicates

in

the

clause

and

are

obtained

by

the

transformation

o

defined

below

Definition

Given

a

term

t

over

a

set

of

function

symbols

F

a

set

of

variables

V

and

a

mode-annotation

a

the

transformation

o

T

A

T

-LRB-

F

-LSB-

-LRB-

V

A

-RRB-

-LSB-

g

-RRB-

which

generalizes

t

to

the

most

general

term

in

the

mode-annotation

a

is

defined

recursively

as

follows

o

-LRB-

X

a

-RRB-

-LRB-

if

a

Xa

otherwise

o

-LRB-

f

-LRB-

tn

-RRB-

-RRB-

o

-LRB-

f

-LRB-

tn

-RRB-

ground

-RRB-

f

-LRB-

o

-LRB-

ground

-RRB-

o

-LRB-

tn

ground

-RRB-

-RRB-

o

-LRB-

f

-LRB-

tn

-RRB-

g

-LRB-

am

-RRB-

-RRB-

-LRB-

f

-LRB-

o

-LRB-

-RRB-

o

-LRB-

tn

an

-RRB-

-RRB-

if

g

f

and

n

m

otherwise

o

-LRB-

f

-LRB-

tn

-RRB-

fixff

a

-RRB-

o

-LRB-

f

-LRB-

tn

-RRB-

a

-LSB-

fixff

a

ff

-RSB-

-RRB-

o

-LRB-

f

-LRB-

tn

-RRB-

-RRB-

sup

-LRB-

o

-LRB-

f

-LRB-

tn

-RRB-

-RRB-

o

-LRB-

f

-LRB-

tn

-RRB-

-RRB-

-RRB-

where

the

binary

operator

sup

on

terms

over

F

-LSB-

-LRB-

V

A

-RRB-

-LSB-

is

defined

as

follows

sup

-LRB-

-RRB-

if

or

sup

-LRB-

t

-RRB-

t

sup

-LRB-

t

-RRB-

t

sup

-LRB-

f

-LRB-

tn

-RRB-

f

-LRB-

un

-RRB-

-RRB-

f

-LRB-

sup

-LRB-

-RRB-

sup

-LRB-

tn

un

-RRB-

-RRB-

The

value

denotes

arbitrary

terms

-LRB-

i.e.

a

lack

of

any

structure

information

-RRB-

while

the

value

denotes

an

erroneous

application

of

o

i.e.

an

application

o

-LRB-

t

a

-RRB-

where

no

instance

of

t

can

be

in

the

mode

a.

Furthermore

we

require

o

to

be

strict

in

Note

that

the

binary

operator

sup

operates

only

on

terms

such

that

either

one

of

the

two

terms

is

a

or

a

or

the

two

terms

have

the

same

outermost

function

symbol

For

a

term

t

and

a

mode-annotation

a

the

transformation

o

produces

the

generalization

of

t

compatible

with

the

mode-annotation

a

This

means

that

subterms

of

t

with

corresponding

mode-annotations

are

replaced

by

the

symbol

and

that

variables

are

tagged

with

the

corresponding

mode-annotation

For

example

o

-LRB-

X

list

-LRB-

-RRB-

-RRB-

X

list

-LRB-

-RRB-

and

o

-LRB-

f

-LRB-

X

X

-RRB-

f

-LRB-

g

-LRB-

-RRB-

ground

-RRB-

f

-LRB-

Xg

-LRB-

-RRB-

Xground

-RRB-

In

the

following

we

identify

o

with

its

-LRB-

obvious

-RRB-

extension

to

pairs

of

atom

and

mode

We

also

usually

omit

the

tag

on

the

variables

after

the

application

of

the

transformation

o

unless

as

in

the

last

example

above

such

an

omission

would

be

ambiguous

Notice

however

that

if

t

a

then

o

-LRB-

t

a

-RRB-

does

not

contain

any

tagged

variables

Lemma

Let

t

be

a

term

in

a

mode-annotation

a.

Then

for

every

substitution

oe

o

-LRB-

t

a

-RRB-

o

-LRB-

toe

a

-RRB-

Proof

-LRB-

by

induction

on

the

construction

of

o

-LRB-

t

a

-RRB-

-RRB-

Consider

the

possible

cases

ffl

a

is

ground

then

t

must

be

a

ground

term

so

t

toe

ffl

a

is

then

o

-LRB-

t

a

-RRB-

o

-LRB-

toe

a

-RRB-

ffl

a

is

f

-LRB-

an

-RRB-

then

since

t

is

in

mode

a

we

must

have

t

f

-LRB-

tn

-RRB-

and

naturally

toe

f

-LRB-

tnoe

-RRB-

by

the

inductive

hypothesis

o

-LRB-

ti

ai

-RRB-

o

-LRB-

tioe

ai

-RRB-

so

the

result

follows

ffl

a

is

fixff

a

then

by

the

inductive

hypothesis

o

-LRB-

t

a

-LSB-

fixff

a

ff

-RSB-

-RRB-

o

-LRB-

toe

a

-LSB-

fixff

a

ff

-RSB-

-RRB-

and

the

result

follows

trivially

ffl

a

is

then

again

by

the

inductive

hypothesis

the

result

follows

Lemma

Given

a

substitution

oe

the

substitution

def

ffXa

o

-LRB-

X

oe

a

-RRB-

j

X

dom

-LRB-

oe

-RRB-

a

Ag

is

such

that

for

any

term

t

and

any

mode-annotation

a

-LRB-

t

does

not

have

to

be

in

the

mode-annotation

a

-RRB-

o

-LRB-

toe

a

-RRB-

o

-LRB-

t

a

-RRB-

Proof

-LRB-

by

induction

on

the

definition

of

o

-LRB-

t

a

-RRB-

-RRB-

First

it

is

easy

to

see

-LRB-

by

induction

-RRB-

that

if

o

-LRB-

t

a

-RRB-

then

for

any

oe

o

-LRB-

toe

a

-RRB-

so

o

-LRB-

toe

a

-RRB-

o

-LRB-

t

a

-RRB-

Similarly

if

o

-LRB-

t

a

-RRB-

then

o

-LRB-

toe

a

-RRB-

o

-LRB-

t

a

-RRB-

Next

if

t

X

and

then

o

-LRB-

t

a

-RRB-

Xa

o

-LRB-

X

oe

-RRB-

o

-LRB-

toe

a

-RRB-

If

X

is

not

in

the

domain

of

oe

then

Xa

Xa

o

-LRB-

X

a

-RRB-

o

-LRB-

X

oe

a

-RRB-

Let

us

now

consider

the

cases

where

t

f

-LRB-

tn

-RRB-

if

a

is

ground

then

o

-LRB-

t

ground

-RRB-

f

-LRB-

o

-LRB-

ground

-RRB-

o

-LRB-

tn

ground

-RRB-

-RRB-

Ind

f

-LRB-

o

-LRB-

ground

-RRB-

o

-LRB-

tnoe

ground

-RRB-

-RRB-

o

-LRB-

toe

ground

-RRB-

if

a

is

f

-LRB-

an

-RRB-

then

o

-LRB-

t

f

-LRB-

an

-RRB-

-RRB-

f

-LRB-

o

-LRB-

-RRB-

o

-LRB-

tn

an

-RRB-

-RRB-

Ind

f

-LRB-

o

-LRB-

-RRB-

o

-LRB-

tnoe

an

-RRB-

-RRB-

o

-LRB-

toe

f

-LRB-

an

-RRB-

-RRB-

if

a

is

fixff

a

then

o

-LRB-

t

fixff

a

-RRB-

o

-LRB-

t

a

-LSB-

fixff

a

ff

-RSB-

-RRB-

Ind

o

-LRB-

toe

a

-LSB-

fixff

a

ff

-RSB-

-RRB-

o

-LRB-

toe

fixff

a

-RRB-

if

a

is

first

by

a

simple

induction

on

the

definition

of

sup

we

can

show

that

sup

-LRB-

-RRB-

sup

-LRB-

-RRB-

Now

we

have

o

-LRB-

t

-RRB-

sup

-LRB-

o

-LRB-

t

-RRB-

o

-LRB-

t

-RRB-

-RRB-

sup

-LRB-

o

-LRB-

t

-RRB-

o

-LRB-

t

-RRB-

-RRB-

Ind

sup

-LRB-

o

-LRB-

toe

-RRB-

o

-LRB-

toe

-RRB-

-RRB-

o

-LRB-

t

-RRB-

symbol

Ind

denotes

equality

by

the

inductive

hypothesis

Simple

Termination

Theorem

Theorem

A

logic

program

P

universally

terminates

if

there

exists

a

well-founded

stable

ordering

O

such

that

for

each

clause

A

Bn

-LRB-

with

mode-dependencies

A

ai

ao

Bn

bin

bon

;-RRB-

o

-LRB-

A

ai

-RRB-

O

o

-LRB-

Bj

bij

-RRB-

The

proof

of

this

theorem

follows

from

the

proof

of

the

more

complex

version

of

this

theorem

given

in

subsection

This

theorem

can

be

used

to

prove

the

termination

of

simple

programs

such

as

append

or

split

However

for

many

programs

such

as

quicksort

we

need

to

use

inter-argument

relationships

to

complete

the

termination

proof

Inter-argument

Relationships

Given

a

predicate

p

with

a

mode

dependence

p

-LRB-

ain

-RRB-

p

-LRB-

aon

-RRB-

an

inter-argument

relationship

on

p

is

expressed

by

the

syntax

p

-LRB-

Xn

-RRB-

R

-LRB-

o

-LRB-

-RRB-

o

-LRB-

Xn

aon

-RRB-

-RRB-

and

has

the

interpretation

p

-LRB-

un

-RRB-

p

-LRB-

aon

-RRB-

and

p

-LRB-

un

-RRB-

succeeds

implies

R

-LRB-

o

-LRB-

-RRB-

o

-LRB-

un

aon

-RRB-

-RRB-

In

defining

an

inter-argument

relationship

the

transformation

o

is

needed

because

we

must

allow

inter-argument

relationships

to

hold

on

non-ground

terms

For

example

the

semantic

property

that

in

the

output

tuple

of

append

the

length

of

the

first

argument

is

less

than

or

equal

to

the

length

of

the

third

can

be

expressed

by

append

-LRB-

X

Y

Z

-RRB-

X

Z

Inter-argument

relationships

can

also

be

defined

for

-LRB-

conjunction

of

-RRB-

literals

with

the

syntax

Ln

R

-LRB-

o

-LRB-

-RRB-

o

-LRB-

tk

aok

-RRB-

-RRB-

where

tk

are

terms

appearing

in

the

literals

and

ak

are

the

corresponding

modeannotations

The

interpretation

of

this

relationship

is

for

all

substitutions

oe

Lnoe

moLn

and

Lnoe

succeed

implies

R

-LRB-

o

-LRB-

-RRB-

o

-LRB-

tkoe

aok

-RRB-

-RRB-

As

an

example

consider

the

following

relationship

append

-LRB-

X

-LSB-

AjL

-RSB-

Y

-RRB-

append

-LRB-

X

L

Z

-RRB-

Y

Z

In

general

a

set

of

inter-argument

relationships

can

be

expressed

as

a

single

relationship

on

a

conjunction

of

the

literals

Consequently

in

the

following

we

assume

that

all

inter-argument

relationships

have

been

appropriately

defined

on

conjunctions

of

literals

ordering

O

is

stable

if

O

implies

for

all

substitutions

oe

oe

-LRB-

-RRB-

O

oe

-LRB-

-RRB-

We

define

an

ordering

relation

O

via

an

inference

system

O

Such

an

inference

system

can

use

an

inter-argument

relationship

-LRB-

Ln

R

-RRB-

as

a

hypothesis

for

inferences

Thus

derivations

have

the

form

R

O

O

For

example

we

define

in

the

next

section

an

inference

system

that

establishes

an

ordering

relation

O

by

combining

subterm

ordering

inter-argument

monotonicity

relationships

and

lexicographic

ordering

Definition

We

say

that

an

inference

system

O

is

stable

if

R

O

O

implies

Roe

O

O

Lemma

If

O

is

stable

then

given

an

inter-argument

relationship

Ln

R

such

that

R

O

o

-LRB-

-RRB-

O

o

-LRB-

-RRB-

and

given

a

substitution

oe

such

that

Lnoe

are

in

their

output

mode

and

succeed

then

o

-LRB-

-RRB-

O

o

-LRB-

-RRB-

Proof

By

the

definition

of

inter-argument

relationships

if

Lnoe

are

well-moded

and

true

then

R

-LRB-

o

-LRB-

tioe

ai

-RRB-

:-RRB-

is

true

as

well

By

lemma

there

exists

a

substitution

such

that

for

any

t

and

a

o

-LRB-

toe

a

-RRB-

o

-LRB-

t

a

-RRB-

Hence

R

-LRB-

o

-LRB-

tioe

ai

-RRB-

:-RRB-

R

-LRB-

o

-LRB-

ti

ai

-RRB-

:-RRB-

R

By

stability

R

O

O

implies

R

O

O

Since

R

is

true

the

result

follows

from

the

correctness

of

O.

Termination

Theorem

Definition

Given

a

stable

inference

system

defining

an

ordering

relation

a

clause

A

Bn

is

o-decreasing

if

Ri

O

o

-LRB-

A

a

-RRB-

O

o

-LRB-

Bi

bi

-RRB-

-LRB-

-RRB-

where

Ri

is

defined

by

the

following

inter-argument

relationship

Bi

Ri

Theorem

A

logic

program

P

terminates

if

there

exists

a

stable

inference

system

O

defining

a

well-founded

ordering

relation

O

such

that

each

clause

in

P

is

o-decreasing

Proof

Suppose

on

the

contrary

that

there

exists

some

infinite

derivation

Consider

the

-LRB-

infinite

-RRB-

proof

tree

represented

by

this

derivation

By

Koenig

's

lemma

since

the

branching

factor

is

finite

there

must

be

some

infinite

branch

Consider

adjacent

nodes

A

B

along

this

branch

The

subtree

below

A

was

obtained

by

resolving

A

with

a

clause

Bi

Bi

Bn

such

that

Aoe

-LRB-

oe

mgu

-LRB-

A

-RRB-

-RRB-

and

B

where

is

the

computed

answer

substitution

obtained

from

the

resolution

of

Bi

Therefore

each

of

Bi

succeed

and

are

in

their

output

mode

Since

the

clause

is

o-decreasing

Ri

O

o

-LRB-

a

-RRB-

O

o

-LRB-

b

-RRB-

By

lemma

we

obtain

that

o

-LRB-

a

-RRB-

O

o

-LRB-

b

-RRB-

By

lemma

o

-LRB-

A

a

-RRB-

o

-LRB-

Aoe

a

-RRB-

o

-LRB-

a

-RRB-

o

-LRB-

a

-RRB-

Similarly

we

have

o

-LRB-

B

b

-RRB-

o

-LRB-

b

-RRB-

Since

o

-LRB-

a

-RRB-

O

o

-LRB-

b

-RRB-

we

can

conclude

that

this

infinite

branch

represents

an

infinite

descending

sequence

This

contradicts

the

well-foundedness

of

O.

The

following

alternative

definition

of

o-decreasiveness

-LRB-

adapted

from

-LSB-

-RSB-

-RRB-

is

consistent

with

the

termination

theorem

above

and

has

proven

to

be

useful

in

practice

Definition

Given

a

stable

inference

system

defining

a

well-founded

ordering

relation

a

clause

A

Bn

is

said

to

be

o-decreasing

also

if

o

-LRB-

A

ai

-RRB-

O

o

-LRB-

-RRB-

Rj

O

o

-LRB-

Bj

boj

-RRB-

o

-LRB-

Bj

bij

-RRB-

Rn

O

o

-LRB-

Bn

bon

-RRB-

o

-LRB-

A

ao

-RRB-

or

Rn

O

o

-LRB-

A

ai

-RRB-

O

o

-LRB-

A

ao

-RRB-

where

Rj

is

the

inter-argument

relationship

of

Bj

The

advantage

of

this

alternative

definition

of

o-decreasiveness

is

that

sometimes

using

this

definition

and

a

carefully

chosen

ordering

we

can

avoid

the

need

for

inter-argument

relationships

The

example

illustrates

this

possibility

Using

the

following

proposition

we

can

easily

show

that

Theorem

still

holds

with

this

characterization

of

o-decreasing

clause

Proposition

Given

a

stable

inference

system

defining

a

well-founded

ordering

relation

For

all

o-decreasing

clauses

A

Bn

and

for

all

atoms

and

substitution

oe

such

that

ai

and

Aoe

we

have

ffl

o

-LRB-

ai

-RRB-

o

-LRB-

Bj

oe

j

bij

-RRB-

and

o

-LRB-

ai

-RRB-

o

-LRB-

Bj

oe

j

boj

-RRB-

where

j

is

the

computed

answer

substitution

resulting

from

the

evaluation

of

Bj

oe

ffl

o

-LRB-

ai

-RRB-

o

-LRB-

Aoe

n

ao

-RRB-

where

n

is

the

computed

answer

substitution

resulting

from

the

evaluation

of

Bnoe

Proof

-LRB-

by

induction

on

-RRB-

First

note

that

by

Lemma

o

-LRB-

ai

-RRB-

o

-LRB-

ai

-RRB-

o

-LRB-

Aoe

ai

-RRB-

Now

consider

the

following

two

cases

ffl

There

is

no

Bj

In

that

case

if

the

clause

is

o-decreasing

we

have

o

-LRB-

A

ai

-RRB-

o

-LRB-

A

ao

-RRB-

By

Lemma

and

the

stability

of

this

implies

o

-LRB-

Aoe

ai

-RRB-

o

-LRB-

Aoe

ao

-RRB-

as

desired

ffl

There

are

some

Bj

's

Let

us

do

a

secondary

induction

on

n

the

number

of

Bj

's

to

prove

the

first

part

of

the

property

-LCB-

n

Since

the

clause

is

o-decreasing

we

must

have

o

-LRB-

A

ai

-RRB-

o

-LRB-

-RRB-

so

o

-LRB-

Aoe

ai

-RRB-

o

-LRB-

-RRB-

Since

the

clause

is

well-moded

so

the

inductive

hypothesis

is

applicable

and

we

have

o

-LRB-

-RRB-

o

-LRB-

-RRB-

-LRB-

for

the

substitution

generated

by

the

execution

of

-RRB-

Hence

o

-LRB-

Aoe

ai

-RRB-

o

-LRB-

-RRB-

-LCB-

n

Assume

o

-LRB-

Aoe

ai

-RRB-

o

-LRB-

Bn

n

bon

-RRB-

By

o-decreasingness

we

have

Rn

o

-LRB-

Bn

bon

-RRB-

o

-LRB-

Bn

bin

-RRB-

so

by

stability

Rn

n

o

-LRB-

Bn

n

bon

-RRB-

o

-LRB-

Bnoe

n

bin

-RRB-

By

the

correctness

of

the

inter-argument

relationship

Rn

n

holds

so

using

the

inductive

hypothesis

o

-LRB-

Aoe

ai

-RRB-

o

-LRB-

Bnoe

n

bin

-RRB-

By

the

induction

argument

on

we

can

assume

that

o

-LRB-

Bnoe

n

bin

-RRB-

o

-LRB-

Bnoe

n

bon

-RRB-

From

this

the

result

follows

Since

the

clause

is

o-decreasing

we

must

have

either

Rn

o

-LRB-

Bn

bon

-RRB-

o

-LRB-

A

ao

-RRB-

so

by

stability

Rnoe

n

o

-LRB-

Bnoe

n

bon

-RRB-

o

-LRB-

Aoe

n

ao

-RRB-

Therefore

since

Rnoe

n

must

hold

o

-LRB-

Aoe

ai

-RRB-

o

-LRB-

Aoe

n

ao

-RRB-

We

can

also

have

Rn

o

-LRB-

A

ai

-RRB-

o

-LRB-

A

ao

-RRB-

so

by

stability

Rnoe

n

o

-LRB-

Aoe

n

ai

-RRB-

o

-LRB-

Aoe

n

ao

-RRB-

From

this

the

result

follows

Orderings

for

Logic

Programs

It

follows

from

Theorem

that

to

show

termination

we

need

inference

systems

that

define

wellfounded

stable

orderings

and

allow

the

use

of

inter-argument

relationships

We

now

present

one

such

system

inspired

by

-LSB-

-RSB-

which

is

based

on

the

lexicographic

ordering

-LRB-

-LSB-

-RSB-

-RRB-

Given

a

precedence

ordering

Op

on

predicate

symbols

and

a

term

ordering

Ot

-LRB-

discussed

below

-RRB-

we

can

define

the

following

lexicographic

ordering

among

literals

P

ssp

Q

ftigi

-LRB-

Ot

-RRB-

lexfsigi

P

-LRB-

tn

-RRB-

Q

-LRB-

sm

-RRB-

P

Op

Q

P

-LRB-

tn

-RRB-

Q

-LRB-

sm

-RRB-

In

general

the

term

ordering

Ot

must

be

well-founded

and

defined

by

a

stable

inference

system

Moreover

it

is

essential

that

the

ordering

induced

by

the

inter-argument

relationships

be

consistent

with

the

term

ordering

While

different

term

orderings

are

possible

in

practice

the

subterm

ordering

is

sufficient

for

most

programs

The

set

of

inference

rules

for

the

subterm

ordering

is

ffl

Subterm

relation

ti

t

s

f

-LRB-

ti

:-RRB-

Ot

s

t

t

g

-LRB-

sm

-RRB-

t

Ot

si

ffl

Structural

rules

transitivity

reflexivity

of

t

irreflexivity

of

Ot

The

correctness

of

these

rules

is

straightforward

as

is

the

stability

of

the

ordering

With

the

appropriate

structural

rules

this

set

is

complete

as

well

Other

terms

orderings

such

as

the

lexicographic

path

ordering

could

easily

be

adapted

to

form

sets

of

inference

rules

such

as

the

above

-LRB-

-LSB-

-RSB-

-RRB-

It

is

even

possible

to

define

inference

rules

that

use

mode

information

for

simplifications

but

such

inference

rules

do

not

seem

to

help

significantly

in

practice

Example

-LRB-

Quicksort

-RRB-

split

-LRB-

A

-RRB-

split

-LRB-

B

X

A

B

L

H

-RRB-

B

A

split

-LRB-

X

A

L

H

-RRB-

split

-LRB-

B

X

A

L

B

H

-RRB-

B

A

split

-LRB-

X

A

L

H

-RRB-

quicksort

-LRB-

-RRB-

quicksort

-LRB-

A

X

Y

-RRB-

split

-LRB-

X

A

L

H

-RRB-

quicksort

-LRB-

L

-RRB-

quicksort

-LRB-

H

-RRB-

append

-LRB-

A

Y

-RRB-

Mode-dependence

split

-LRB-

list

-LRB-

int

-RRB-

int

-RRB-

split

-LRB-

list

-LRB-

int

-RRB-

int

list

-LRB-

int

-RRB-

list

-LRB-

int

-RRB-

-RRB-

quicksort

-LRB-

list

-LRB-

int

-RRB-

-RRB-

quicksort

-LRB-

list

-LRB-

int

-RRB-

list

-LRB-

int

-RRB-

-RRB-

To

show

that

this

program

terminates

we

use

the

lexicographic

ordering

described

earlier

with

the

following

precedence

ordering

on

predicate

symbols

quicksort

Op

split

Op

append

Op

Op

The

term

ordering

Ot

is

simply

the

subterm

relation

We

can

show

the

termination

of

split

as

follows

fsplit

-LRB-

B

X

A

B

L

H

-RRB-

g

B

A

-LRB-

B

A

-RRB-

and

split

-LRB-

X

A

L

H

-RRB-

g

since

split

Op

and

split

Op

split

ssp

split

and

-LRB-

X

A

-RRB-

-LRB-

Ot

-RRB-

lex

-LRB-

X

A

-RRB-

To

show

the

termination

of

quicksort

we

need

the

inter-argument

relationships

split

-LRB-

X

A

L

H

-RRB-

X

L

and

split

-LRB-

X

A

L

H

-RRB-

X

H

and

then

we

must

prove

quicksort

-LRB-

A

X

-RRB-

split

-LRB-

X

A

-RRB-

X

L

X

H

-RRB-

quicksort

-LRB-

A

X

-RRB-

quicksort

-LRB-

L

-RRB-

X

L

X

H

-RRB-

quicksort

-LRB-

A

X

-RRB-

quicksort

-LRB-

H

-RRB-

X

L

X

H

-RRB-

quicksort

-LRB-

A

X

-RRB-

append

-LRB-

-RRB-

The

first

and

fourth

case

are

trivially

true

The

second

and

third

case

can

be

seen

to

follow

from

the

inter-argument

relationships

For

example

in

the

second

case

the

following

deduction

is

possible

quicksort

ss

quicksort

X

L

A

X

Ot

L

fA

X

-LRB-

Ot

-RRB-

lexfL

quicksort

-LRB-

A

X

-RRB-

quicksort

-LRB-

L

-RRB-

Recursive

term

orderings

for

literals

When

a

term

ordering

is

defined

recursively

using

a

precedence

ordering

on

function

symbols

one

can

in

addition

to

the

precedence

attach

a

status

to

each

function

symbol

indicating

whether

its

arguments

should

be

compared

lexicographically

or

as

a

multiset

-LSB-

-RSB-

This

notion

of

recursive

ordering

is

easily

adapted

to

orderings

for

literals

Moreover

the

notion

of

status

can

be

expanded

for

predicate

symbols

For

a

predicate

symbol

different

status

-LRB-

and

precedences

-RRB-

can

be

given

for

each

mode

of

the

predicate

and

furthermore

one

can

even

specify

as

part

of

the

status

that

only

certain

arguments

of

the

predicate

should

be

involved

in

further

comparisons

The

resulting

notion

of

recursive

orderings

provides

a

powerful

class

of

orderings

For

example

in

some

cases

such

orderings

obviate

the

need

for

inter-argument

relationships

Consider

terms

t

f

-LRB-

tn

-RRB-

and

s

g

-LRB-

sm

-RRB-

where

the

status

of

f

and

g

are

written

sf

and

sg

respectively

If

t

and

s

are

compared

by

a

recursively

defined

ordering

three

cases

can

occur

ffl

the

arguments

of

t

are

compared

with

s

in

accordance

with

the

status

of

f

This

is

written

tng

-LRB-

-RRB-

hsf

is

For

example

if

sf

indicates

that

only

the

second

and

third

arguments

should

be

involved

in

further

comparison

this

is

equivalent

to

s

s.

-LRB-

Whether

the

status

indicate

multiset

or

lexicographic

does

not

have

an

effect

in

this

case

-RRB-

ffl

the

arguments

of

s

are

compared

with

t

This

is

written

t

-LRB-

-RRB-

h

smg

ffl

the

arguments

of

s

are

compared

with

the

arguments

of

t

This

is

written

tng

-LRB-

-RRB-

hsf

sgi

smg

For

example

if

both

sf

and

sg

specify

that

a

lexicographic

order

should

be

used

and

sf

specifies

that

only

the

second

and

third

arguments

should

be

used

then

this

is

equivalent

to

-LRB-

-RRB-

lex

sng

Note

that

sf

and

sg

must

both

indicate

a

lexicographic

order

or

both

indicate

a

multiset

order

Definition

-LRB-

Recursive

Ordering

-RRB-

Assume

given

a

precedence

ordering

p

on

function

symbols

and

on

moded

predicate

symbols

-LRB-

i.e.

on

predicate

symbols

with

an

associated

mode

-RRB-

and

assume

that

each

function

and

each

moded

predicate

symbols

has

associated

with

it

some

status

Note

that

symbols

of

equal

precedence

must

have

the

same

lexicographic

or

multiset

status

Besides

the

usual

structural

rules

-LRB-

transitivity

-RRB-

the

inference

rules

specific

to

the

recursive

ordering

are

as

follows

let

A

p

-LRB-

tn

-RRB-

and

B

q

-LRB-

sm

-RRB-

p

p

q

A

-LRB-

-RRB-

h

smg

A

B

tng

-LRB-

-RRB-

hsp

iB

A

B

p

ssp

q

tng

-LRB-

-RRB-

hsp

smg

A

-LRB-

-RRB-

h

smg

A

B

Proposition

The

recursive

ordering

is

a

well-founded

order

Proof

ffl

Transitivity

Let

A

p

-LRB-

tn

-RRB-

B

q

-LRB-

sm

-RRB-

and

C

o

-LRB-

rl

-RRB-

Assume

A

B

and

B

C

We

want

to

show

A

C

We

proceed

by

induction

in

the

size

of

the

terms

A

B

C

According

to

the

definition

of

recursive

orderings

the

possible

cases

are

-LCB-

tng

-LRB-

-RRB-

hsp

iB

i.e.

some

selected

subterms

of

A

is

greater

than

B.

Hence

by

the

inductive

hypothesis

this

subterm

is

greater

than

C

so

A

is

greater

than

C

The

same

reasoning

applies

if

smg

-LRB-

-RRB-

sqC

-LCB-

p

p

q

>=p

o

in

this

case

we

must

have

B

-LRB-

-RRB-

h

rlg

so

by

the

inductive

hypothesis

A

-LRB-

-RRB-

h

rlg

Hence

A

C

The

same

reasoning

applies

if

p

>=p

q

p

o.

-LCB-

p

ssq

q

ssp

o.

By

the

same

reasoning

as

above

we

can

show

A

-LRB-

-RRB-

rlg

Moreover

in

this

case

A

B

C

implies

tng

-LRB-

-RRB-

hsp

smg

-LRB-

-RRB-

hsq

rlg

Hence

tng

-LRB-

-RRB-

hsp

rlg

by

the

transitivity

of

the

multiset

or

lexicographic

ordering

accordingly

ffl

Well-foundedness

Suppose

there

is

an

infinite

descending

sequence

of

literals

Consider

an

infinite

subsequence

where

all

the

literals

use

with

the

same

predicate

symbol

By

transitivity

this

is

an

infinite

descending

sequence

By

definition

of

the

recursive

ordering

the

multisets

of

arguments

from

these

literals

form

an

infinite

descending

sequence

of

multisets

of

fixed

size

of

terms

-LRB-

since

a

literal

can

not

be

a

subterm

of

another

literal

-RRB-

But

this

contradicts

the

well-foundedness

of

the

recursive

ordering

for

terms

-LRB-

-LSB-

-RSB-

-RRB-

Example

-LRB-

-LSB-

-RSB-

-RRB-

Consider

the

following

program

computing

the

transitive

closure

of

a

predicate

p

p

-LRB-

a

b

-RRB-

p

-LRB-

b

c

-RRB-

tc

-LRB-

X

X

-RRB-

tc

-LRB-

X

Y

-RRB-

p

-LRB-

X

Z

-RRB-

tc

-LRB-

Z

Y

-RRB-

Mode-dependencies

p

-LRB-

ground

-RRB-

p

-LRB-

ground

ground

-RRB-

tc

-LRB-

ground

-RRB-

tc

-LRB-

ground

ground

-RRB-

This

program

can

be

shown

to

terminate

without

inter-argument

relationships

by

using

the

recursive

ordering

defined

above

with

the

precedence

ordering

a

Op

b

Op

c

Op

p

p

-LRB-

ground

ground

-RRB-

Op

tc

tc

-LRB-

ground

-RRB-

Op

tc

tc

-LRB-

ground

ground

-RRB-

Op

p

p

-LRB-

ground

-RRB-

and

status

information

specifying

that

p

p

-LRB-

ground

-RRB-

and

tc

tc

-LRB-

ground

-RRB-

consider

only

their

first

argument

for

further

comparisons

and

p

p

-LRB-

ground

ground

-RRB-

and

tc

tc

-LRB-

ground

ground

-RRB-

consider

only

their

second

argument

for

further

comparisons

Then

the

clauses

defining

the

program

can

be

shown

to

be

o-decreasing

as

follows

ffl

For

the

first

clause

we

must

show

p

-LRB-

a

b

-RRB-

p

-LRB-

ground

-RRB-

p

-LRB-

a

b

-RRB-

p

-LRB-

ground

ground

-RRB-

which

follows

from

a

Op

p

p

-LRB-

ground

ground

-RRB-

and

a

Op

b.

ffl

The

second

and

third

clause

are

similarly

shown

to

be

o-decreasing

ffl

For

the

fourth

clause

we

must

show

tc

-LRB-

X

Y

-RRB-

tc

-LRB-

ground

-RRB-

p

-LRB-

X

Z

-RRB-

p

-LRB-

ground

-RRB-

p

-LRB-

X

Z

-RRB-

p

-LRB-

ground

ground

-RRB-

tc

-LRB-

Z

Y

-RRB-

tc

-LRB-

ground

-RRB-

and

tc

-LRB-

Z

Y

-RRB-

tc

-LRB-

ground

ground

-RRB-

tc

-LRB-

X

Y

-RRB-

tc

-LRB-

ground

ground

-RRB-

All

these

can

be

shown

by

our

recursive

ordering

Therefore

the

program

terminates

Inductive

Reasoning

and

Symbolic

Evaluation

Deriving

inter-argument

monotonicity

relationships

Inter-argument

monotonicity

relationships

can

be

derived

as

partial

correctness

properties

by

assuming

that

they

hold

for

the

recursive

call

to

predicates

-LRB-

or

of

the

goal

-RRB-

and

using

inference

systems

for

orderings

like

the

ones

discussed

earlier

to

test

that

they

hold

for

the

original

predicates

This

is

the

classical

Hoare

's

technique

to

prove

partial

correctness

properties

moreover

if

the

relevant

literals

are

terminating

then

the

inter-arguments

relationships

become

total

correctness

properties

and

this

technique

can

be

seen

as

a

special

case

of

Noetherian

induction

Example

Given

the

usual

definition

of

append

-LRB-

with

both

modes

append

-LRB-

list

-LRB-

-RRB-

-RRB-

append

-LRB-

list

-LRB-

-RRB-

list

-LRB-

-RRB-

list

-LRB-

-RRB-

-RRB-

and

append

-LRB-

list

-LRB-

-RRB-

list

-LRB-

-RRB-

-RRB-

append

-LRB-

list

-LRB-

-RRB-

list

-LRB-

-RRB-

list

-LRB-

-RRB-

-RRB-

-RRB-

suppose

we

want

to

prove

the

inter-argument

relationship

append

-LRB-

A

Y

-RRB-

append

-LRB-

Z

-RRB-

Y

Z

We

have

to

consider

two

cases

-LSB-

-RSB-

and

-LSB-

-RSB-

In

the

first

case

after

simplification

we

are

left

to

prove

A

which

follows

by

the

subterm

property

of

In

the

second

case

the

result

of

the

symbolic

evaluation

is

append

-LRB-

A

Y

-RRB-

append

-LRB-

-RRB-

B

Y

B

By

the

assumption

that

the

inter-argument

relationship

holds

for

the

recursive

call

of

the

goal

we

have

Y

It

is

now

trivial

to

show

that

Y

-RRB-

B

Y

B

Moreover

having

proved

that

append

in

the

above

given

modes

terminates

we

can

conclude

that

this

inter-argument

relationship

is

a

total

correctness

property

Using

this

we

could

for

example

easily

prove

the

termination

of

the

permutation

program

-LRB-

-LSB-

-RSB-

-RRB-

perm

-LRB-

-RRB-

perm

-LRB-

A

X

Y

-RRB-

append

-LRB-

A

Y

-RRB-

append

-LRB-

Z

-RRB-

perm

-LRB-

X

Z

-RRB-

Mode-dependency

perm

-LRB-

list

-LRB-

-RRB-

-RRB-

perm

-LRB-

list

-LRB-

-RRB-

list

-LRB-

-RRB-

-RRB-

Mutually

recursive

predicates

For

mutually

recursive

predicate

definitions

a

formal

inductive

argument

is

sometimes

necessary

to

prove

termination

The

technique

adopted

is

similar

to

the

one

advocated

above

for

deriving

inter-argument

relationships

It

uses

symbolic

evaluation

with

the

termination

of

the

predicates

as

the

inductive

hypotheses

and

syntactic

orderings

to

justify

the

use

of

the

inductive

hypotheses

Moreover

once

the

termination

of

some

literals

is

established

-LRB-

e.g.

by

the

inductive

hypothesis

-RRB-

we

can

use

the

relevant

inter-argument

relationships

as

a

total

correctness

property

to

propagate

ordering

information

henceforth

allowing

more

uses

of

the

inductive

hypotheses

The

following

example

-LRB-

from

-LSB-

-RSB-

-RRB-

illustrates

these

issues

Example

-LRB-

Parser

-RRB-

The

following

rules

specify

an

arithmetic

expression

parser

The

modedependence

is

e

-LRB-

list

-LRB-

ground

-RRB-

-RRB-

e

-LRB-

list

-LRB-

ground

-RRB-

list

-LRB-

ground

-RRB-

-RRB-

The

first

argument

to

e

is

the

ground

input

list

to

be

parsed

and

the

second

argument

is

the

unparsed

suffix

e

-LRB-

L

T

-RRB-

t

-LRB-

L

C

-RRB-

e

-LRB-

C

T

-RRB-

e

-LRB-

L

T

-RRB-

t

-LRB-

L

T

-RRB-

t

-LRB-

L

T

-RRB-

n

-LRB-

L

C

-RRB-

t

-LRB-

C

T

-RRB-

t

-LRB-

L

T

-RRB-

n

-LRB-

L

T

-RRB-

n

-LRB-

-LRB-

A

T

-RRB-

e

-LRB-

A

-RRB-

T

-RRB-

n

-LRB-

L

T

T

-RRB-

constant-or-identifier

-LRB-

L

-RRB-

The

technique

described

in

subsection

can

be

used

to

obtain

the

inter-argument

relationships

e

-LRB-

L

T

-RRB-

L

T

t

-LRB-

L

T

-RRB-

L

T

n

-LRB-

L

T

-RRB-

L

T

To

prove

the

termination

of

this

program

we

prove

by

induction

the

following

property

Any

goal

of

the

form

e

-LRB-

-RRB-

or

t

-LRB-

-RRB-

or

n

-LRB-

-RRB-

terminates

We

use

the

simple

lexicographic

ordering

on

literals

defined

earlier

The

precedence

ordering

on

predicates

is

e

ssp

t

ssp

n

Op

constant-or-identifier

Symbolic

evaluation

of

the

possible

goals

fe

-LRB-

L

T

-RRB-

t

-LRB-

L

T

-RRB-

n

-LRB-

L

T

-RRB-

g

generates

a

large

number

of

cases

like

e

-LRB-

-LRB-

A

T

-RRB-

e

-LRB-

L

C

T

-RRB-

t

-LRB-

-LRB-

C

T

-RRB-

Let

us

consider

for

example

the

case

t

-LRB-

-LRB-

A

T

-RRB-

whose

symbolic

evaluation

produces

t

-LRB-

-LRB-

A

T

-RRB-

e

-LRB-

A

-RRB-

C

-RRB-

t

-LRB-

C

T

-RRB-

e

-LRB-

A

-RRB-

T

-RRB-

It

is

possible

to

use

the

inductive

hypothesis

since

t

-LRB-

-LRB-

A

T

-RRB-

O

e

-LRB-

A

-RRB-

C

-RRB-

and

t

-LRB-

-LRB-

A

T

-RRB-

O

e

-LRB-

A

-RRB-

T

-RRB-

Hence

by

the

inductive

hypothesis

e

-LRB-

A

-RRB-

C

-RRB-

and

e

-LRB-

A

-RRB-

T

-RRB-

terminate

Moreover

the

inter-argument

relationship

holds

i.e

A

-RRB-

C

so

A

C

and

-RRB-

A

C.

Hence

t

-LRB-

-LRB-

A

T

-RRB-

O

t

-LRB-

C

T

-RRB-

This

allows

the

use

of

the

inductive

hypothesis

again

and

we

conclude

that

t

-LRB-

C

T

-RRB-

terminates

All

the

other

cases

can

be

treated

similarly

so

the

program

universally

terminates

Notice

however

that

we

could

show

the

termination

of

this

program

in

a

more

straightforward

way

without

inter-argument

relationships

if

we

use

the

recursive

ordering

discussed

earlier

with

the

precedence

ordering

Op

n

n

-LRB-

list

-LRB-

ground

-RRB-

list

-LRB-

ground

-RRB-

-RRB-

Op

t

t

-LRB-

list

-LRB-

ground

-RRB-

list

-LRB-

ground

-RRB-

-RRB-

Op

e

e

-LRB-

list

-LRB-

ground

-RRB-

list

-LRB-

ground

-RRB-

-RRB-

Op

e

e

-LRB-

list

-LRB-

ground

-RRB-

-RRB-

Op

t

t

-LRB-

list

-LRB-

ground

-RRB-

-RRB-

Op

n

n

-LRB-

list

-LRB-

ground

-RRB-

-RRB-

and

the

status

e

e

-LRB-

list

-LRB-

ground

-RRB-

-RRB-

consider

only

its

first

arguments

while

e

e

-LRB-

list

-LRB-

ground

-RRB-

list

-LRB-

ground

-RRB-

-RRB-

consider

only

its

second

arguments

and

similarly

for

t

and

n.

Extensions

The

techniques

for

showing

termination

presented

here

can

be

adapted

to

more

powerful

notions

of

mode-annotations

We

can

indeed

extend

mode-annotations

to

regular

trees

as

described

in

-LSB-

-RSB-

An

extension

is

in

fact

necessary

to

handle

partially

instantiated

structures

which

of

the

existing

approaches

to

termination

are

able

to

handle

Consider

the

following

motivating

example

a

tail-recursive

program

to

flatten

an

input

list

by

one

level

Example

-LRB-

Tail-recursive

one

level

Flatten

-RRB-

flatten

-LRB-

-RRB-

flatten

-LRB-

A

X

Y

-RRB-

mlist

-LRB-

A

-RRB-

append

-LRB-

A

Z

Y

-RRB-

flatten

-LRB-

X

Z

-RRB-

flatten

-LRB-

A

X

Y

-RRB-

constant

-LRB-

A

-RRB-

append

-LRB-

-LSB-

A

-RSB-

Z

Y

-RRB-

flatten

-LRB-

X

Z

-RRB-

constant

-LRB-

X

-RRB-

atom

-LRB-

X

-RRB-

integer

-LRB-

X

-RRB-

mlist

-LRB-

-LSB-

AjX

-RSB-

-RRB-

Mode-dependencies

flatten

-LRB-

list

-LRB-

ground

list

-LRB-

-RRB-

-RRB-

-RRB-

flatten

-LRB-

list

-LRB-

ground

list

-LRB-

-RRB-

-RRB-

list

-LRB-

-RRB-

-RRB-

constant

-LRB-

-RRB-

constant

-LRB-

ground

-RRB-

mlist

-LRB-

-RRB-

mlist

-LRB-

list

-LRB-

-RRB-

-RRB-

With

the

definition

of

mode-annotations

given

in

section

we

are

not

able

to

assign

a

mode-dependency

for

append

such

that

the

program

is

well-moded

If

we

assign

the

following

mode-dependency

to

append

append

-LRB-

list

-LRB-

-RRB-

-RRB-

append

-LRB-

list

-LRB-

-RRB-

list

-LRB-

-RRB-

list

-LRB-

-RRB-

-RRB-

then

the

first

clause

defining

append

is

not

well-moded

However

we

clearly

need

the

modedependency

append

-LRB-

list

-LRB-

-RRB-

-RRB-

mo

if

flatten

is

to

be

well-moded

For

mo

all

that

is

really

necessary

is

that

when

Z

is

in

the

mode

list

-LRB-

-RRB-

then

Y

is

also

in

that

mode

Informally

this

suggests

a

mode-dependency

of

the

form

ff

-LRB-

append

-LRB-

list

-LRB-

-RRB-

-RRB-

append

-LRB-

list

-LRB-

-RRB-

ff

-RRB-

list

-LRB-

-RRB-

-RRB-

-RRB-

list

-LRB-

-RRB-

-RRB-

After

the

subsequent

execution

of

the

-LRB-

recursive

-RRB-

call

to

flatten

since

Z

is

in

the

output

mode

of

flatten

Z

is

instantiated

to

a

term

in

the

mode

list

-LRB-

-RRB-

Hence

we

can

use

the

implication

in

the

mode-dependency

of

append

to

conclude

that

Y

must

also

be

in

mode

list

-LRB-

-RRB-

This

concludes

proving

the

well-modedness

of

the

program

The

termination

proof

is

then

straightforward

Thus

to

define

well-modedness

of

programs

such

as

flatten

above

we

need

to

extend

the

syntax

of

mode-annotations

by

allowing

a-terms

containing

existentially

quantified

mode

variables

and

mode-dependency

defined

by

logical

implication

The

extended

mode-annotations

are

similar

to

regular

trees

as

described

in

-LSB-

-RSB-

note

in

passing

that

the

alternative

prescriptive

approach

to

types

as

defined

in

-LSB-

-RSB-

is

not

able

to

handle

partially

instantiated

arguments

Mode-dependency

for

predicates

can

also

be

used

to

specify

control

in

concurrent

logic

programs

For

instance

a

consumer

process

may

have

to

suspend

until

an

argument

gets

sufficiently

instantiated

by

a

producer

process

to

unify

with

the

head

of

a

clause

Modeannotations

can

be

used

to

specify

the

exact

structure

of

the

arguments

that

would

allow

the

consumer

process

to

resume

Furthermore

in

parallel

evaluation

of

logic

programs

the

granularity

of

subtasks

-LRB-

i.e.

if

a

subtask

warrants

a

sub-computation

large

enough

to

outweigh

the

cost

of

forking

-RRB-

can

be

specified

via

the

richer

notion

of

modes

Conclusions

This

paper

presents

a

rich

framework

of

directionality

for

logic

programs

and

its

use

in

a

proof

method

for

proving

universal

termination

of

logic

programs

Notions

of

mode-annotations

-LRB-

defined

via

a

combination

of

modes

and

types

-RRB-

and

mode-dependencies

are

used

to

give

a

directionality

to

logic

programs

Well-founded

orderings

on

the

atoms

in

a

clause

are

then

used

to

show

the

finiteness

of

LD-derivations

Due

to

the

generality

of

mode-annotations

the

termination

methodology

can

handle

programs

and

goals

with

non-ground

inputs

and

nonground

outputs

We

believe

that

the

richness

of

mode-annotations

can

also

be

used

in

applications

other

than

termination

proofs

such

as

specifying

control

for

concurrent

logic

languages

References

-LSB-

-RSB-

K.

R.

Apt

R.

N.

Bol

and

J.

W.

Klop

On

the

safe

termination

of

Prolog

programs

In

G.

Levi

and

M.

Martelli

editors

Proc

Intl.

Conf

on

Logic

Programming

volume

Pisa

-LSB-

-RSB-

K.

R.

Apt

and

D.

Pedreschi

Studies

in

pure

Prolog

termination

Technical

Report

Centrum

voor

Wiskunde

en

Informatica

September

-LSB-

-RSB-

M.

Baudinet

Proving

termination

properties

of

Prolog

programs

A

semantic

approach

Proc

Symp

LICS

-LCB-

-LSB-

-RSB-

M.

Bezem

Characterising

Termination

of

logic

programs

with

level

mappings

In

E.

L.

Lusk

and

R.

A.

Overbeek

editors

Proc

North

American

Conf

on

Logic

Programming

volume

pages

-LCB-

MIT

Press

-LSB-

-RSB-

A.

Brodsky

and

Y.

Sagiv

Inference

of

inequality

constraints

in

logic

programs

In

ACM

Symp

on

Principles

of

Database

Systems

volume

ACM

Press

-LSB-

-RSB-

M.

Bruynooghe

Adding

redundancy

to

obtain

more

reliable

and

more

readable

Prolog

programs

In

First

Intl.

Logic

Programming

Conf.

pages

-LCB-

-LSB-

-RSB-

H.

Comon

Solving

inequations

in

term

algebras

In

LICS

pages

-LCB-

IEEE

June

-LSB-

-RSB-

N.

Dershowitz

Termination

of

rewriting

Journal

of

Symbolic

Computation

-LCB-

-LSB-

-RSB-

Y.

Deville

Logic

Programming

Systematic

Program

Development

International

Series

in

Logic

Programming

Addison

Wesley

-LSB-

-RSB-

H.

Ganzinger

and

U.

Waldmann

Termination

proofs

of

well-moded

logic

programs

via

conditional

rewrite

systems

In

M.

Rusinowitch

and

J.L.

Remy

editors

CTRS

pages

-LCB-

July

-LSB-

-RSB-

A.

Van

Gelder

Deriving

constraints

among

argument

sizes

in

logic

programs

In

ACM

Symp

on

Principles

of

Database

Systems

volume

pages

-LCB-

SIGACT-SIGMOD-SIGART

ACM

Press

-LSB-

-RSB-

G.

Janssens

and

M.

Bruynooghe

Deriving

descriptions

of

possible

values

of

program

variables

by

means

of

abstract

interpretation

J.

Logic

Programming

-LRB-

-RRB-

-LCB-

July

-LSB-

-RSB-

T.

K.

Lakshman

and

Uday

S.

Reddy

Typed

Prolog

A

semantic

reconstruction

of

the

MycroftO'Keefe

type

system

In

Vijay

Saraswat

and

Kazunori

Ueda

editors

Logic

Programming

Proceedings

of

the

International

Symposium

pages

-LCB-

Cambridge

Mass.

MIT

Press

-LSB-

-RSB-

P.

Mishra

Towards

a

theory

of

types

in

Prolog

In

IEEE

International

symposium

on

logic

programming

pages

-LCB-

-LSB-

-RSB-

P.

Mishra

and

U.

S.

Reddy

Declaration-free

type

checking

In

ACM

Symp

on

Principles

of

Programming

Languages

pages

-LCB-

-LSB-

-RSB-

Gerald

Peterson

Solving

term

inequalities

In

Proceedings

of

the

Eighth

National

Conference

on

Artificial

Intelligence

pages

-LCB-

Boston

MA

July

-LSB-

-RSB-

L.

Plumer

Termination

proofs

for

logic

programs

based

on

predicate

inequalities

In

D.H.D.

Warren

and

P.

Szeredi

editors

Proc

Intl.

Conf

on

Logic

Programming

volume

pages

-LCB-

MIT

Press

-LSB-

-RSB-

L.

Plumer

Automatic

termination

proofs

for

prolog

programs

operating

on

non-ground

terms

In

Proc

Intl.

Logic

Programming

Symp

MIT

Press

-LSB-

-RSB-

C.

Pyo

and

U.

S.

Reddy

Inference

of

polymorphic

types

for

logic

programs

In

E.

L.

Lusk

and

R.A.

Overbeek

editor

Logic

Programming

Proceedings

of

the

North

American

Conf.

pages

-LCB-

MIT

Press

-LSB-

-RSB-

M.R.K.

Krishna

Rao

Deepak

Kapur

and

R.K.

Shyamasundar

A

transformational

methodology

for

proving

termination

of

logic

programs

In

Conference

on

Computer

Science

Logic

LNCS

-LRB-

to

appear

-RRB-

-LSB-

-RSB-

U.

S.

Reddy

Transformation

of

logic

programs

into

functional

programs

In

Proc

Intl.

Symp

on

Logic

Programming

pages

-LCB-

New

Jersey

USA

IEEE

-LSB-

-RSB-

U.

S.

Reddy

Notions

of

polymorphism

for

predicate

logic

programs

In

Proc

Intl.

Conf

on

Logic

Programming

volume

-LSB-

-RSB-

U.

S.

Reddy

A

typed

foundation

for

directional

logic

programming

In

Proc

Workshop

on

Extensions

to

Logic

Programming

University

of

Bologna

Italy

pages

-LCB-

-LSB-

-RSB-

B.

J.

Ross

Using

algaebraic

semantics

for

proving

Prolog

termination

and

transformation

In

Proc

Assoc.

Logic

Programming

UK

-LSB-

-RSB-

Y.

Sagiv

A

termination

test

for

logic

programs

In

Proc

Intl.

Logic

Programming

Symp

MIT

Press

-LSB-

-RSB-

K.

Sohn

and

A.

Van

Gelder

Termination

detection

in

logic

programs

using

argument

sizes

In

ACM

Symp

on

Principles

of

Database

Systems

pages

-LCB-

-LSB-

-RSB-

T.

Vassak

and

J.

Potter

Characterization

of

terminating

logic

programs

In

IEEE

Symposium

on

Logic

Programming

Vancouver

-LSB-

-RSB-

K.

Verschaetse

and

D.

De

Schreye

Deriving

termination

proofs

for

logic

programs

using

abstract

procedures

In

Proceedings

ICLP

pages

-LCB-

Paris

June

MIT

Press

-LSB-

-RSB-

B.

Wang

and

R.

K.

Shyamasundar

Towards

a

characterization

of

termination

of

logic

prorgams

In

ACM

Conference

on

Programming

Languages

Implementation

and

Logic

programming

Appendix

A

Mode

Inference

Rules

Mode

judgements

of

the

form

t

a

are

derived

using

the

the

following

mode

inference

rules

Introduction

Rules

In

an

mode

context

mode

judgements

t

a

are

inferred

using

the

the

following

introduction

rules

X

a

if

-LRB-

X

a

-RRB-

X

if

-LRB-

X

dom

-LRB-

-RRB-

c

ground

if

c

is

a

constant

tk

ak

F

-LRB-

tk

-RRB-

F

-LRB-

ak

-RRB-

where

F

is

a

function

or

predicate

symbol

t

t

t

a

-LSB-

fixff

a

ff

-RSB-

t

fixff

a

Derived

Rules

The

following

rule

expresses

inclusion

relationship

between

well-moded

terms

t

a

t

b

if

b

a

Elimination

Rules

The

mode-context

generated

by

a

sequence

of

well-moded

literals

-LRB-

each

in

its

output

mode

-RRB-

is

determined

using

the

introduction

rules

backward

and

the

following

elimination

rule

t

-LSB-

ft

A

-LSB-

ft

A

A

Notice

that

because

of

the

implicit

non-determinism

involved

in

the

backward

use

of

the

inclusion

rule

in

practice

we

might

fail

to

effectively

generate

a

mode-context

The

soundness

of

these

rules

is

shown

as

follows

Proposition

If

t

a

then

for

all

which

respect

-LRB-

t

-RRB-

-LSB-

-LSB-

a

-RSB-

-RSB-

where

is

any

mode

valuation

Proof

By

induction

on

the

length

of

the

derivation

of

t

a.

ffl

X

a

if

-LRB-

X

a

-RRB-

For

all

which

respects

-LRB-

X

-RRB-

-LSB-

-LSB-

-LRB-

X

-RRB-

-RSB-

-RSB-

i.e.

-LRB-

X

-RRB-

-LSB-

-LSB-

a

-RSB-

-RSB-

ffl

X

if

-LRB-

X

a

-RRB-

For

all

which

respects

-LRB-

X

-RRB-

-LSB-

-LSB-

-RSB-

-RSB-

which

is

true

for

all

ffl

c

ground

For

all

which

respects

-LRB-

c

-RRB-

c

-LSB-

-LSB-

ground

-RSB-

-RSB-

which

is

true

for

all

ffl

f

-LRB-

t

-RRB-

f

-LRB-

a

-RRB-

For

all

which

respects

-LRB-

f

-LRB-

t

-RRB-

-RRB-

f

-LRB-

-LRB-

t

-RRB-

-RRB-

By

induction

hypothesis

-LRB-

t

-RRB-

-LSB-

-LSB-

a

-RSB-

-RSB-

Hence

f

-LRB-

-LRB-

t

-RRB-

-RRB-

f

-LRB-

-LSB-

-LSB-

a

-RSB-

-RSB-

-RRB-

i.e.

-LRB-

f

-LRB-

t

-RRB-

-RRB-

-LSB-

-LSB-

f

-LRB-

a

-RRB-

-RSB-

-RSB-

ffl

t

By

induction

hypothesis

for

all

which

respects

-LRB-

t

-RRB-

-LSB-

-LSB-

-RSB-

-RSB-

Now

since

-LSB-

-LSB-

-RSB-

-RSB-

-LSB-

-LSB-

-RSB-

-RSB-

hence

-LRB-

t

-RRB-

-LSB-

-LSB-

-RSB-

-RSB-

ffl

t

fixff

a.

By

induction

hypothesis

for

all

which

respect

-LRB-

t

-RRB-

-LSB-

-LSB-

a

-LSB-

fixff

a

ff

-RSB-

-RSB-

-RSB-

Hence

-LRB-

t

-RRB-

-LSB-

-LSB-

a

-RSB-

-RSB-

-LRB-

-LSB-

fixff

a

ff

-RSB-

-RRB-

Hence

-LRB-

t

-RRB-

least

S

such

that

S

-LSB-

-LSB-

a

-RSB-

-RSB-

-LRB-

-LSB-

S

ff

-RSB-

-RRB-

By

the

definition

of

fix

this

means

that

-LRB-

t

-RRB-

-LSB-

-LSB-

fixff

a

-RSB-

-RSB-

ffl

t

-LRB-

using

the

inclusion

rule

-RRB-

By

the

induction

hypothesis

for

all

which

respect

-LRB-

t

-RRB-

-LSB-

-LSB-

-RSB-

-RSB-

Hence

-LRB-

t

-RRB-

-LSB-

-LSB-

-RSB-

-RSB-

-LRB-

since

-RRB-

-LSB-

-LSB-

-RSB-

-RSB-

-LSB-

-LSB-

-RSB-

-RSB-

by

proposition

-RRB-

Appendix

B

Inclusion

Checking

Rules

The

following

inference

rules

-LRB-

which

have

been

adapted

from

-LSB-

-RSB-

-RRB-

are

used

to

infer

inclusion

-LRB-

covering

-RRB-

of

mode-annotations

a

a

ref

lexivity

transitivity

a

ground

an

ground

f

-LRB-

an

-RRB-

ground

an

bn

f

-LRB-

an

-RRB-

f

-LRB-

bn

-RRB-

a

a

a

a

a

a

-LSB-

fixff

a

ff

-RSB-

fixff

a

a

-LSB-

fixff

a

ff

-RSB-

fixff

a

The

above

rules

can

be

transformed

into

an

algorithm

for

checking

inclusion

Further

note

that

the

inference

system

is

deterministic

but

for

the

rule

for

in

and

the

unfolding

of

the

fix

The

rule

for

can

be

made

deterministic

by

requiring

that

be

discriminative

The

soundness

of

these

rules

is

shown

below

Proposition

If

then

-LSB-

-LSB-

-RSB-

-RSB-

-LSB-

-LSB-

-RSB-

-RSB-

where

is

any

mode

valuation

Proof

By

induction

on

the

length

of

the

derivation

of

ffl

a

Since

-LSB-

-LSB-

-RSB-

-RSB-

T

hence

-LSB-

-LSB-

-RSB-

-RSB-

-LSB-

-LSB-

-RSB-

-RSB-

ffl

If

the

last

rule

used

to

derive

is

ground

an

ground

f

-LRB-

an

-RRB-

ground

By

the

induction

hypothesis

we

can

assume

that

-LSB-

-LSB-

a

-RSB-

-RSB-

-LSB-

-LSB-

ground

-RSB-

-RSB-

Now

-LSB-

-LSB-

f

-LRB-

a

-RRB-

-RSB-

-RSB-

ff

-LRB-

t

-RRB-

j

t

-LSB-

-LSB-

a

-RSB-

-RSB-

g

ff

-LRB-

t

-RRB-

j

t

-LSB-

-LSB-

ground

-RSB-

-RSB-

g

-LSB-

-LSB-

ground

-RSB-

-RSB-

ffl

If

the

last

rule

used

to

derive

is

an

bn

f

-LRB-

an

-RRB-

f

-LRB-

bn

-RRB-

By

the

induction

hypothesis

we

can

assume

that

-LSB-

-LSB-

-RSB-

-RSB-

-LSB-

-LSB-

-RSB-

-RSB-

-LSB-

-LSB-

an

-RSB-

-RSB-

-LSB-

-LSB-

bn

-RSB-

-RSB-

Now

-LSB-

-LSB-

f

-LRB-

an

-RRB-

-RSB-

-RSB-

ff

-LRB-

tn

-RRB-

j

-LSB-

-LSB-

-RSB-

-RSB-

tn

-LSB-

-LSB-

an

-RSB-

-RSB-

g

ff

-LRB-

tn

-RRB-

j

-LSB-

-LSB-

-RSB-

-RSB-

tn

-LSB-

-LSB-

bn

-RSB-

-RSB-

g

-LSB-

-LSB-

f

-LRB-

bn

-RRB-

-RSB-

-RSB-

ffl

If

the

last

rule

used

to

derive

is

a

a

a

By

the

induction

hypothesis

we

can

assume

that

-LSB-

-LSB-

-RSB-

-RSB-

-LSB-

-LSB-

a

-RSB-

-RSB-

and

-LSB-

-LSB-

-RSB-

-RSB-

-LSB-

-LSB-

a

-RSB-

-RSB-

Now

-LSB-

-LSB-

-RSB-

-RSB-

-LSB-

-LSB-

-RSB-

-RSB-

-LSB-

-LSB-

-LSB-

-RSB-

-RSB-

-LSB-

-LSB-

a

-RSB-

-RSB-

ffl

If

the

last

rule

used

to

derive

is

a

a

By

the

induction

hypothesis

we

can

assume

that

-LSB-

-LSB-

a

-RSB-

-RSB-

-LSB-

-LSB-

-RSB-

-RSB-

-LSB-

-LSB-

-RSB-

-RSB-

ffl

If

the

last

rule

used

to

derive

is

a

-LSB-

fixff

a

ff

-RSB-

fixff

a

By

the

induction

hypothesis

we

can

assume

that

-LSB-

-LSB-

a

-LSB-

fixff

a

ff

-RSB-

-RSB-

-RSB-

-LSB-

-LSB-

-RSB-

-RSB-

Hence

-LSB-

-LSB-

fixff

a

-RSB-

-RSB-

-LSB-

-LSB-

-RSB-

-RSB-

-LRB-

by

the

semantics

of

fix

-RRB-

ffl

If

the

last

rule

used

to

derive

is

a

-LSB-

fixff

a

ff

-RSB-

fixff

a

By

the

induction

hypothesis

we

can

assume

that

-LSB-

-LSB-

-RSB-

-RSB-

-LSB-

-LSB-

a

-LSB-

fixff

a

ff

-RSB-

-RSB-

-RSB-

Hence

-LSB-

-LSB-

-RSB-

-RSB-

-LSB-

-LSB-

fixff

a

-RSB-

-RSB-

-LRB-

by

the

semantics

of

fix

-RRB-

directionality
modes
regular tree types
universal termination

mode-dependence
well-founded orderings
well-moded programs