Towards

a

Mathematical

Specification

of

a

Graph-Narrowing

Machine

Egon

B

orger

Dip

di

Informatica

Universita

di

Pisa

Cso

Italia

PISA

boerger@di.unipi.it

Francisco

J.

L

opez-Fraguas

Dep

Inform?atica

y

Autom?atica

Universidad

Complutense

Av

Complutense

s/n

Madrid

Spain

fraguas@dia.ucm.es

Mario

Rodr

iguez-Artalejo

Dep

Inform?atica

y

Autom?atica

Universidad

Complutense

Av

Complutense

s/n

Madrid

Spain

mario@dia.ucm.es

Abstract

The

paper

provides

a

mathematical

model

for

the

innermost

version

of

the

functional

logic

programming

language

BABEL

-LSB-

-RSB-

and

refines

it

stepwise

towards

a

mathematical

specification

of

its

implementation

by

a

graph-narrowing

machine

-LSB-

-RSB-

Our

description

directly

reflects

the

basic

intuitions

underlying

the

language

and

can

thus

be

used

as

a

primary

mathematical

definition

of

innermost

BABEL

For

each

refinement

step

a

mathematical

correctness

proof

is

given

thus

paving

the

way

for

a

correctness

proof

of

the

graph-narrowing

machine

implementation

-LRB-

a

full

correctness

proof

could

be

achieved

by

providing

some

further

refinement

steps

leading

to

the

machine

's

abstraction

level

-RRB-

The

specification

uses

evolving

algebras

thus

allowing

the

descriptions

to

be

procedural

and

nevertheless

abstract

readable

as

pseudocode

over

abstract

data

Introduction

Many

investigations

during

the

last

years

have

been

devoted

to

the

combination

of

the

functional

and

logic

programming

paradigms

-LRB-

for

surveys

on

different

approaches

and

proposals

see

e.g.

-LSB-

-RSB-

-LSB-

-RSB-

-LSB-

-RSB-

-RRB-

The

interest

of

such

an

integration

has

been

well

motivated

by

several

researchers

and

is

presently

quite

widely

accepted

One

of

the

possible

approaches

to

logic

functional

programming

builds

so

called

functional

logic

languages

-LSB-

-RSB-

which

are

syntactically

very

similar

to

functional

languages

but

have

narrowing

as

operational

semantics

From

the

point

of

view

of

functional

logic

programming

narrowing

is

a

natural

extension

of

the

reduction

mechanism

used

for

functional

programs

It

reduces

a

computation

expression

by

applying

rewrite

rules

but

using

unification

instead

of

matching

This

paper

deals

with

the

mathematical

specification

of

the

functional

logic

language

BABEL

-LSB-

-RSB-

ultimately

aiming

at

a

full

correctnes

proof

for

its

implementation

through

the

innermost

graphnarrowing

machine

IBAM

-LSB-

-RSB-

IBAM

belongs

to

a

series

of

abstract

machines

which

were

designed

during

the

last

years

for

compiler

implementations

of

logic

functional

languages

-LRB-

see

e.g

-LSB-

-RSB-

for

information

on

such

approaches

-RRB-

This

kind

of

machines

usually

combine

features

from

reduction

machines

for

functional

languages

-LSB-

-RSB-

and

Warren

's

Prolog

Engine

WAM

-LSB-

-RSB-

-LSB-

-RSB-

Technical

Report

DIA

March

Dep.de

Informatica

y

Automatica

Universidad

Complutense

Madrid

An

abridged

version

of

this

paper

has

appeared

under

the

title

A

model

for

mathematical

analysis

of

functional

logic

programs

and

their

implementations

in

B.

Pehrson

and

I.

Simon

-LRB-

Eds

-RRB-

IFIP

World

Computer

Congress

Volume

I

Technology/Foundations

Elsevier

Amsterdam

It

is

well

known

that

providing

mathematically

precise

but

nevertheless

understandable

specifications

of

complex

systems

is

a

difficult

task

Recently

Gurevich

's

evolving

algebra

approach

-LSB-

-RSB-

has

proved

very

adequate

for

providing

mathematical

descriptions

of

various

logic

programming

languages

and

implementation

methods

-LSB-

B

-RSB-

The

definitions

of

Prolog

and

the

WAM

-LSB-

-RSB-

yielding

correctness

conditions

for

Prolog

compilers

are

particularly

related

to

the

present

paper

which

can

be

understood

as

a

first

step

towards

extending

this

specification

methodology

to

functional

logic

languages

and

machines

Evolving

Algebras

provide

procedural

and

nevertheless

abstract

descriptions

easy

to

adapt

to

different

abstraction

levels

and

easy

to

read

-LRB-

without

special

mathematical

training

-RRB-

as

pseudocode

over

abstract

data

We

present

a

series

of

successively

more

refined

descriptions

of

BABEL

by

means

of

evolving

algebras

starting

at

the

abstract

level

of

the

observable

computational

behaviour

and

coming

close

to

the

level

of

machine

implementation

We

provide

detailed

hints

of

correctness

proofs

for

each

of

the

descriptions

with

respect

to

the

preceding

one

The

organization

of

this

paper

is

as

follows

Section

is

this

Introduction

In

Section

we

provide

the

information

on

Evolving

Algebras

and

the

BABEL

language

which

is

needed

for

understanding

the

rest

of

the

paper

Section

develops

the

description

of

innermost

BABEL

at

the

user

level

of

observable

behaviour

through

a

series

of

three

algebras

which

specify

essentially

the

search

tree

of

BABEL

goal

expressions

and

the

search

for

solutions

via

leftmost

innermost

narrowing

and

backtracking

Section

moves

closer

to

the

level

of

machine

implementation

by

replacing

the

tree

structure

of

BABEL

's

search

space

by

a

stack

structure

In

Section

we

refine

the

description

by

taking

care

of

features

more

related

to

the

functional

dimension

of

the

language

namely

the

creation

of

computation

tasks

responsible

for

evaluating

some

subexpression

of

the

main

goal

expression

and

returning

the

result

to

the

activator

task

This

organization

and

the

way

it

combines

with

backtracking

do

correspond

to

the

actual

IBAM

implementation

-LSB-

-RSB-

which

is

not

approached

closer

in

this

paper

-LRB-

next

refinement

steps

should

deal

with

machine

code

generation

-RRB-

Section

presents

on

an

abstract

level

some

optimizations

of

the

IBAM

implementation

Section

summarizes

our

conclusions

and

points

to

possible

lines

of

future

research

In

an

appendix

whole

sets

of

rules

for

all

the

refinements

of

the

algebra

can

be

found

Notation

and

prerequisites

We

expect

from

the

reader

only

rudimentary

knowledge

of

the

language

of

first

order

logic

logic

programming

and

term

rewriting

We

list

nevertheless

in

this

section

some

definitions

which

might

help

to

read

our

description

which

uses

Gurevich

's

notion

of

evolving

algebra

without

presupposing

more

than

what

is

listed

here

as

pseudocode

over

abstract

data

We

briefly

indicate

also

how

our

correctness

proofs

can

be

carried

out

in

this

framework

The

abstract

data

come

as

elements

of

-LRB-

not

further

analysed

-RRB-

sets

-LRB-

domains

universes

-RRB-

The

operations

allowed

on

universes

will

be

represented

by

partial

functions

We

shall

allow

the

setup

to

evolve

in

time

by

executing

function

updates

of

form

f

-LRB-

tn

-RRB-

t

whose

execution

is

to

be

understood

as

changing

-LRB-

or

defining

if

there

was

-RRB-

the

value

of

function

f

at

given

arguments.We

shall

also

allow

some

of

the

universes

to

grow

in

time

by

executing

universe

extensions

extend

A

by

tn

with

updates

endextend

where

updates

may

-LRB-

and

should

-RRB-

depend

on

ti

's

setting

the

values

of

some

functions

on

newly

created

elements

ti

of

A

The

precise

way

our

abstract

machines

-LRB-

evolving

algebras

-RRB-

may

evolve

in

time

will

be

determined

by

a

finite

set

of

rules

of

form

if

condition

then

updates

where

condition

or

guard

is

a

boolean

the

truth

of

which

triggers

simultaneous

execution

of

all

updates

listed

in

updates

Simultaneous

execution

helps

us

avoid

coding

to

say

interchange

two

values

Every

evolving

algebra

formally

pair

-LRB-

A

R

-RRB-

where

A

is

a

first-order

heterogeneous

algebra

with

partial

functions

and

possibly

empty

domains

and

R

is

a

finite

system

of

transition

rules

as

explained

above

determines

a

class

of

structures

that

we

shall

call

algebras

or

states

of

-LRB-

A

R

-RRB-

Within

such

classes

we

will

have

a

notion

of

initial

and

terminal

algebras

expressing

initial

resp

final

states

of

the

target

system

We

are

essentially

interested

only

in

those

states

which

are

reachable

from

inital

states

by

R

In

our

refinement

steps

we

typically

construct

a

more

concrete

evolving

algebra

-LRB-

B

S

-RRB-

out

of

a

given

more

abstract

evolving

algebra

-LRB-

A

R

-RRB-

and

relate

them

by

a

-LRB-

partial

-RRB-

proof

map

F

mapping

states

B

of

-LRB-

B

S

-RRB-

to

states

F

-LRB-

B

-RRB-

of

-LRB-

A

R

-RRB-

and

rule

sequences

R

of

R

to

rule

sequences

F

-LRB-

R

-RRB-

of

S

so

that

the

following

diagram

commutes

motivation

and

a

precise

definition

of

evolving

algebras

see

-LSB-

-RSB-

A

F

-LRB-

R

-RRB-

FF

B

R

We

shall

consider

such

a

proof

map

to

establish

correctness

of

-LRB-

B

S

-RRB-

with

respect

to

-LRB-

A

R

-RRB-

if

F

preserves

initiality

success

and

failure

-LRB-

value

of

stop

-RRB-

of

states

since

in

that

case

we

may

view

successful

-LRB-

failing

-RRB-

concrete

computations

as

implementing

successful

-LRB-

failing

-RRB-

abstract

computations

We

shall

consider

such

a

proof

map

to

establish

completeness

of

-LRB-

B

S

-RRB-

with

respect

to

-LRB-

A

R

-RRB-

if

every

terminating

computation

in

-LRB-

A

R

-RRB-

is

image

under

F

of

a

terminating

computation

in

-LRB-

B

S

-RRB-

since

in

that

case

we

may

view

every

successful

-LRB-

failing

-RRB-

abstract

computation

as

implemented

by

a

successful

-LRB-

failing

-RRB-

concrete

computation

In

case

we

establish

both

correctness

and

completeness

in

the

above

sense

as

we

do

on

every

of

our

refinement

steps

we

may

speak

of

operational

equivalence

of

evolving

algebras

Since

this

last

notion

is

symmetric

it

does

not

matter

any

more

which

way

F

goes

The

attentive

reader

will

notice

that

we

indeed

have

in

a

few

places

found

it

more

convenient

to

reverse

the

direction

of

F

mapping

the

abstract

algebra

into

the

concrete

one

The

forms

obviously

reducible

to

the

above

basic

syntax

which

we

shall

freely

use

as

abbreviations

are

let

and

if

then

else

We

shall

assume

that

we

have

the

standard

mathematical

universes

of

booleans

integers

lists

of

whatever

trees

etc

-LRB-

as

well

as

the

standard

operations

on

them

-RRB-

at

our

disposal

without

further

mention

We

use

usual

notations

in

particular

Prolog

notation

for

lists

In

the

rest

of

this

paper

the

name

BABEL

will

always

refer

to

the

innermost

version

of

the

BABEL

language

as

described

in

-LSB-

-RSB-

We

do

not

presupose

any

specific

knowledge

of

BABEL

or

its

implementation

But

obviously

some

elementary

knowledge

of

functional

logic

programming

languages

can

only

be

helpful

and

some

very

basic

facts

about

BABEL

are

reported

here

to

help

understanding

our

presentation

of

BABEL

algebras

BABEL

's

syntax

is

based

on

well-typed

expressions

built

from

variables

free

constructors

and

function

symbols

Types

are

statically

checked

at

compile

time

Expressions

can

be

first-order

terms

using

only

constructors

and

variables

or

possibly

higher-order

expressions

involving

also

application

function

symbols

and

predefined

operations

These

include

boolean

operations

a

predefined

equality

and

operators

used

for

building

two

kinds

of

conditional

expressions

-LRB-

b

e

-RRB-

meaning

if

b

then

e

else

undefined

-LRB-

b

-RRB-

meaning

if

b

then

else

Conditional

expressions

of

the

form

-LRB-

b

e

-RRB-

are

also

called

guarded

expressions

They

allow

to

express

the

effect

of

conditional

rewrite

rules

as

well

as

Prolog

clauses

-LRB-

taking

the

boolean

constant

true

as

e

-RRB-

In

fact

BABEL

programs

consist

of

defining

rules

for

function

symbols

having

the

form

f

-LRB-

tn

-RRB-

r

where

r

may

be

a

guarded

expression

-LRB-

b

e

-RRB-

In

spite

of

the

fact

that

a

rule

right

hand

side

may

be

also

not

guarded

we

shall

use

f

-LRB-

tn

-RRB-

b

e

as

our

default

notation

for

BABEL

rules

Number

n

noted

as

arity

-LRB-

f

-RRB-

is

called

the

program

arity

of

f

The

following

are

simple

examples

of

defining

rules

which

could

be

part

of

a

BABEL

program

using

lists

with

constructors

-LSB-

-RSB-

and

-LSB-

j

and

natural

numbers

with

constructors

and

suc

They

define

functions

called

map

plus

and

dominates

map

-LRB-

F

-LSB-

-RSB-

-RRB-

-LSB-

-RSB-

map

-LRB-

F

-LSB-

XjXs

-RSB-

-RRB-

-LSB-

-LRB-

F

X

-RRB-

jmap

-LRB-

F

Xs

-RRB-

-RSB-

plus

-LRB-

Y

-RRB-

Y

plus

-LRB-

suc

-LRB-

X

-RRB-

Y

-RRB-

suc

-LRB-

plus

-LRB-

X

Y

-RRB-

-RRB-

dominates

-LRB-

Xs

Y

s

-RRB-

map

-LRB-

plus

-LRB-

N

-RRB-

Y

s

-RRB-

Xs

true

This

shows

some

typical

uses

of

higher-order

programming

features

in

BABEL

Functions

can

be

partially

applied

as

plus

-LRB-

N

-RRB-

above

and

an

application

operator

is

also

available

BABEL

's

syntax

does

not

use

explicitely

but

making

it

explicit

is

harmless

and

helps

to

simplify

our

descriptions

Note

that

higherorder

variables

-LRB-

as

F

in

the

rules

for

map

-RRB-

can

be

used

only

in

defining

rules

which

introduce

them

in

the

left

hand

side

This

together

with

the

absence

of

terms

in

the

syntax

ensures

that

the

language

can

be

executed

without

higher-order

unification

Solving

BABEL

goals

yields

in

general

multiple

solutions

which

are

computed

by

narrowing

and

backtracking

For

instance

the

rules

above

allow

to

compute

two

solutions

for

a

particular

goal

as

indicated

below

solve

dominates

-LRB-

-LSB-

suc

-LRB-

-RRB-

X

-RSB-

-LSB-

Y

-RSB-

-RRB-

true

fX

Y

suc

-LRB-

-RRB-

g

true

fX

suc

-LRB-

-RRB-

Y

Before

giving

the

formal

specification

of

the

operational

semantics

by

means

of

evolving

algebras

we

still

present

an

even

simpler

example

which

will

serve

as

running

example

all

along

the

paper

to

help

understanding

our

specification

In

the

following

program

we

define

the

function

append

returning

the

concatenation

of

two

lists

and

the

predicate

-LCB-

defined

as

a

true-valued

function

-LCB-

prefix

which

is

true

for

two

lists

when

the

first

one

is

a

prefix

of

the

second

one

In

addition

we

have

a

function

g

simply

switching

the

constants

a

and

b.

-LRB-

-RRB-

append

-LRB-

-LSB-

-RSB-

Y

s

-RRB-

Y

s

-LRB-

-RRB-

append

-LRB-

-LSB-

XjXs

-RSB-

Y

s

-RRB-

-LSB-

Xjappend

-LRB-

Xs

Y

s

-RRB-

-RSB-

-LRB-

-RRB-

prefix

-LRB-

Xs

Y

s

-RRB-

append

-LRB-

Xs

Zs

-RRB-

Y

s

true

-LRB-

-RRB-

g

-LRB-

a

-RRB-

b

-LRB-

-RRB-

g

-LRB-

b

-RRB-

a

To

solve

for

instance

the

goal

prefix

-LRB-

-LSB-

g

-LRB-

X

-RRB-

g

-LRB-

Y

-RRB-

-RSB-

-LSB-

a

X

b

-RSB-

-RRB-

means

to

find

values

-LRB-

a

most

general

substitution

-RRB-

for

the

variables

X

and

Y

such

that

the

given

expression

is

reducible

to

normal

-LRB-

irreducible

-RRB-

form

by

using

the

rules

of

the

program

and

the

predefined

functions

of

the

language

The

reduction

of

expressions

and

the

bindings

for

the

variables

is

done

simultaneously

by

innermost

narrowing

Innermost

means

that

redexes

occupying

a

deeper

position

in

the

expression

must

be

reduced

before

trying

to

reduce

redexes

in

outer

positions

In

our

goal

example

there

are

two

innermost

redexes

namely

g

-LRB-

X

-RRB-

and

g

-LRB-

Y

-RRB-

One

of

them

must

be

selected

for

narrowing

it

A

selection

function

could

be

considered

for

that

purpose

as

in

the

case

of

logic

programming

for

selecting

a

literal

to

resolve

upon

In

BABEL

the

leftmost

innermost

redex

is

always

selected

In

our

example

the

leftmost

innermos

redex

is

g

-LRB-

X

-RRB-

for

which

there

are

two

rules

-LCB-

and

-LCB-

for

narrowing

it

These

two

alternatives

would

narrow

the

entire

expression

to

prefix

-LRB-

-LSB-

b

g

-LRB-

Y

-RRB-

-RSB-

-LSB-

a

X

b

-RSB-

-RRB-

and

prefix

-LRB-

-LSB-

a

g

-LRB-

Y

-RRB-

-RSB-

-LSB-

a

X

b

-RSB-

-RRB-

respectively

with

respective

substitutions

fX

ag

and

fX

bg

In

both

cases

the

new

leftmost

innermost

redex

is

g

-LRB-

Y

-RRB-

It

is

clear

that

solving

a

goal

generates

a

search

space

-LCB-

similar

to

SLD-trees

in

logic

programming

-LCB-

due

to

the

different

do

n't

know

alternative

rules

for

the

selected

redex

The

search

tree

for

our

example

is

depicted

in

Fig.

At

each

node

of

the

tree

the

current

expression

to

be

reduced

is

written

-LRB-

append

prefix

and

true

are

abbreviated

-RRB-

and

the

position

of

the

leftmost

innermost

redex

is

underscored

The

branches

for

a

node

correspond

to

the

alternative

program

rules

which

can

be

applied

to

the

selected

redex

and

we

annotate

them

with

the

substitution

coming

from

unification

with

the

head

of

the

rule

-LRB-

only

substitutions

for

the

variables

in

the

goal

expression

are

written

-RRB-

The

non

failed

leaves

contain

the

normal

forms

obtained

by

different

narrowing

derivations

and

for

each

one

the

answer

substitution

is

built

by

composing

all

the

substitutions

in

the

path

to

the

root

projected

into

the

variables

in

the

initial

goal

In

this

particular

tree

the

only

answer

consists

of

the

normal

form

true

together

with

the

answer

substitution

fX

b

Y

ag

As

in

the

case

of

Prolog

the

search

over

this

tree

in

BABEL

is

done

in

a

leftmost

depth

first

manner

with

chronological

backtracking

in

case

of

failure

or

asking

for

more

solutions

We

do

not

give

here

a

precise

definition

of

the

construction

of

the

tree

and

the

search

on

it

instead

we

leave

our

specification

to

do

the

work

Babel

tree

algebras

A

Babel

computation

can

be

seen

as

systematic

search

of

a

space

of

possible

solutions

to

an

initially

given

goal

The

goals

are

expressions

which

have

to

be

reduced

to

normal

form

using

narrowing

-LRB-

unification

and

reduction

-RRB-

with

respect

to

the

given

Babel

program

-LRB-

a

set

of

defining

rules

for

the

user

defined

functions

true

truefail

fail

fail

Z

-LSB-

b

-RSB-

ap

-LRB-

-LSB-

b

b

-RSB-

Z

-RRB-

-LSB-

a

a

b

-RSB-

t

-LSB-

bjap

-LRB-

-LSB-

b

-RSB-

Z

-RRB-

-RSB-

-LSB-

a

a

b

-RSB-

t

-LSB-

b

bjap

-LRB-

-LSB-

-RSB-

Z

-RRB-

-LSB-

a

a

b

-RSB-

t

-LSB-

b

bjZ

-RSB-

-LSB-

a

a

b

-RSB-

t

ap

-LRB-

-LSB-

b

a

-RSB-

Z

-RRB-

-LSB-

a

a

b

-RSB-

t

-LSB-

bjap

-LRB-

-LSB-

a

-RSB-

Z

-RRB-

-RSB-

-LSB-

a

a

b

-RSB-

t

-LSB-

b

ajap

-LRB-

-LSB-

-RSB-

Z

-RRB-

-LSB-

a

a

b

-RSB-

t

-LSB-

b

ajZ

-RSB-

-LSB-

a

a

b

-RSB-

t

ap

-LRB-

-LSB-

a

b

-RSB-

Z

-RRB-

-LSB-

a

b

b

-RSB-

t

ap

-LRB-

-LSB-

a

a

-RSB-

Z

-RRB-

-LSB-

a

b

b

-RSB-

t

-LSB-

ajap

-LRB-

-LSB-

b

-RSB-

Z

-RRB-

-RSB-

-LSB-

a

b

b

-RSB-

t

-LSB-

ajap

-LRB-

-LSB-

a

-RSB-

Z

-RRB-

-RSB-

-LSB-

a

b

b

-RSB-

t

-LSB-

a

bjap

-LRB-

-LSB-

-RSB-

Z

-RRB-

-LSB-

a

b

b

-RSB-

t

-LSB-

a

ajap

-LRB-

-LSB-

-RSB-

Z

-RRB-

-LSB-

a

b

b

-RSB-

t

-LSB-

a

bjZ

-RSB-

-LSB-

a

b

b

-RSB-

t

-LSB-

a

ajZ

-RSB-

-LSB-

a

b

b

-RSB-

t

X/a

X/b

Y/a

Y/aY/b

Y/b

pr

-LRB-

-LSB-

g

-LRB-

X

-RRB-

g

-LRB-

Y

-RRB-

-RSB-

-LSB-

a

X

b

-RSB-

-RRB-

pr

-LRB-

-LSB-

b

g

-LRB-

Y

-RRB-

-RSB-

-LSB-

a

a

b

-RSB-

-RRB-

pr

-LRB-

-LSB-

a

g

-LRB-

Y

-RRB-

-RSB-

-LSB-

a

b

b

-RSB-

-RRB-

true

pr

-LRB-

-LSB-

b

b

-RSB-

-LSB-

a

a

b

-RSB-

-RRB-

pr

-LRB-

-LSB-

b

a

-RSB-

-LSB-

a

a

b

-RSB-

-RRB-

pr

-LRB-

-LSB-

a

b

-RSB-

-LSB-

a

b

b

-RSB-

-RRB-

pr

-LRB-

-LSB-

a

a

-RSB-

-LSB-

a

b

b

-RSB-

-RRB-

Figure

A

narrowing

tree

as

explained

in

Section

-RRB-

Thus

a

description

of

Babel

's

operational

semantics

has

to

incorporate

besides

reduction

to

normal

form

the

concepts

of

unification

and

backtracking

which

are

well

known

from

logic

programming

languages

The

narrowing

-LCB-

backtracking

core

Our

top

level

description

of

Babel

reflects

the

backtracking

structure

as

dynamically

created

tree

structure

with

father

relation

representing

backtracking

to

alternative

-LRB-

single

-RRB-

narrowing

steps

corresponding

to

different

rules

for

a

certain

function

following

an

idea

used

already

in

-LSB-

B

-RSB-

-LRB-

in

hybrid

stack

oriented

form

-RRB-

and

in

-LSB-

-RSB-

as

basis

to

define

an

abstract

model

for

Prolog

Thus

we

represent

Babel

computation

states

in

a

set

NODE

with

two

distinguished

elements

root

and

currnode

the

latter

representing

the

-LRB-

dynamically

-RRB-

current

state

and

a

total

backtracking

father

function

bfather

NODE

frootg

NODE

such

that

from

each

state

-LRB-

except

root

-RRB-

there

is

a

unique

bfather

path

towards

root

We

create

the

tree

algebra

-LRB-

NODE

root

currnode

bfather

-RRB-

dynamically

as

the

computation

proceeds

out

of

the

initial

state

-LRB-

determined

by

given

program

and

expression

-RRB-

as

the

value

of

currnode

with

the

-LRB-

empty

-RRB-

root

as

the

value

of

bfather

-LRB-

currnode

-RRB-

Each

element

n

of

NODE

has

to

carry

all

information

relevant

at

the

desired

abstraction

level

for

the

computation

state

it

represents

At

the

top

level

this

information

we

will

call

it

environment

of

n

consists

of

the

expression

still

to

be

reduced

the

substitution

computed

so

far

and

the

sequence

of

alternative

states

still

to

be

tried

as

we

will

explain

below

When

in

currnode

a

redex

f

-LRB-

tn

-RRB-

with

user-defined

function

f

is

encountered

for

reduction

we

will

create

a

son

for

each

rule

in

the

program

which

might

be

applied

to

the

redex

to

control

the

alternative

computation

threads

All

such

candidate

sons

are

attached

to

n

as

a

list

cands

-LRB-

n

-RRB-

in

the

order

reflecting

the

ordering

in

which

corresponding

rules

in

the

program

should

be

applied

Therefore

the

signature

of

cands

is

cands

NODE

frootg

NODE

We

require

of

course

the

cands-lists

to

be

consistent

with

bfather

i.e.

whenever

Son

is

among

cands

-LRB-

Father

-RRB-

then

bfather

-LRB-

Son

-RRB-

Father

This

action

of

branching

the

tree

with

cands

-LRB-

n

-RRB-

takes

place

at

most

once

when

n

gets

first

visited

-LRB-

in

Apply

mode

-RRB-

The

mode

then

turns

to

Select

for

handling

the

alternatives

In

Select

mode

a

narrowing

step

is

attempted

whereby

the

first

son

from

cands

-LRB-

n

-RRB-

with

applicable

rule

gets

visited

-LRB-

becomes

the

value

of

currnode

-RRB-

and

switches

to

mode

Eval

-LRB-

for

further

evaluation

of

the

resulting

expression

see

below

-RRB-

The

selected

son

is

simultaneously

deleted

from

the

cands

-LRB-

n

-RRB-

list

If

control

ever

returns

to

n

-LRB-

by

backtracking

cf.

below

-RRB-

it

will

be

in

Select

mode

and

the

next

candidate

son

will

be

selected

if

any

If

in

Select

mode

cands

-LRB-

n

-RRB-

-LSB-

-RSB-

all

attempts

at

narrowing

from

the

state

represented

by

n

will

have

failed

and

n

will

be

abandoned

by

returning

control

to

its

backtracking

father

bfather

This

action

is

usually

called

backtracking

The

bfather

function

then

may

be

seen

as

representing

the

structure

of

Babel

's

backtracking

behaviour

The

narrowing

step

applied

at

a

son

from

the

cands

list

with

associated

candidate

rule

-LRB-

guarded

equation

-RRB-

f

-LRB-

tn

-RRB-

b

e

is

executed

in

terms

of

the

expression

attached

to

each

state

if

the

redex

f

-LRB-

en

-RRB-

in

expression

is

unifiable

with

the

left

hand

side

f

-LRB-

tn

-RRB-

of

the

defining

rule

associated

to

the

son

the

redex

is

replaced

by

the

right

hand

side

b

e

of

that

defining

rule

and

the

whole

expression

gets

updated

for

further

evaluation

-LRB-

see

below

-RRB-

by

the

-LRB-

most

general

-RRB-

unifier

of

the

redex

and

the

rule

's

left

hand

side

As

we

shall

see

it

is

convenient

to

represent

and

parse

also

expressions

in

the

form

of

trees

in

order

to

describe

the

search

for

redices

within

an

expression

We

complete

now

the

definition

of

signature

-LRB-

statics

-RRB-

and

transition

rules

-LRB-

dynamics

-RRB-

which

will

make

the

above

description

precise

The

environment

will

be

associated

to

states

by

appropriate

-LRB-

in

general

partial

-RRB-

functions

on

the

universe

NODE

For

each

state

we

have

to

know

the

expression

still

to

be

reduced

provided

by

a

function

exp

NODE

EXPRESSION

where

EXPRESSION

is

the

domain

of

Babel

expressions

Since

reduction

steps

might

take

place

in

subexpressions

of

the

given

expression

we

also

introduce

a

function

different

modes

and

their

respective

roles

will

become

clear

from

the

specifications

we

present

below

pos

NODE

POSITION

providing

for

given

state

the

position

in

the

associated

expression

at

which

the

next

reduction

step

will

take

place

As

usual

we

assume

that

positions

u

are

coded

as

finite

sequences

of

positive

integers

see

e.g.

-LSB-

-RSB-

The

specification

of

the

function

pos

depends

on

the

reduction

strategy

in

this

model

for

Babel

we

will

define

it

for

the

leftmost

innermost

strategy

-LRB-

which

corresponds

to

an

eager

implementation

-RRB-

For

accessing

and

manipulating

subexpressions

of

given

expressions

-LRB-

read

subtrees

of

trees

-RRB-

in

connection

with

POSITIONwe

will

use

four

standard

tree

yielding

the

subexpression

e

-LSB-

u

-RSB-

of

e

at

position

u

the

result

e

-LSB-

u

-RSB-

of

replacing

e

-LSB-

u

-RSB-

by

in

e

the

information

occurs

-LRB-

u

e

-RRB-

whether

u

is

a

legal

position

in

e

and

concatenation

of

positions

The

substitution

current

at

a

state

accumulated

from

the

unifications

in

preceding

narrowing

steps

is

represented

by

a

function

s

NODE

SUBST

where

SUBST

is

a

universe

of

abstract

substitutions

with

a

function

mgu

EXPRESSION

EXPRESSION

SUBST

-LSB-

fnilg

which

at

this

level

of

description

remains

abstract

associating

to

two

expressions

either

a

substitution

-LRB-

their

most

general

unifier

-RRB-

or

the

answer

that

there

is

For

technical

convenience

we

assume

that

mgu

returns

an

idempotent

substitution

Application

of

substitution

to

an

expression

e

and

its

composition

with

another

substitution

ae

will

be

denoted

by

the

usual

postfix

notation

e

ae

The

above

mentioned

switching

of

modes

will

be

represented

by

a

distinguished

element

mode

fApply

Select

Eval

g

indicating

the

action

to

be

taken

at

currnode

creating

the

alternative

states

for

narrowing

by

rule

application

selecting

among

them

evaluating

the

expression

obtained

by

narrowing

etc.

To

be

able

to

speak

about

termination

we

will

use

a

distinguished

element

stop

to

indicate

respectively

running

of

the

system

halting

with

success

and

final

failure

We

shall

keep

the

above

mentioned

notion

of

candidate

defining

rule

-LRB-

for

applying

a

narrowing

step

to

an

expression

-RRB-

abstract

-LRB-

regarding

it

as

implementation

defined

-RRB-

assuming

only

the

following

integrity

constraints

every

candidate

-LRB-

guarded

-RRB-

defining

rule

for

a

given

expression

ffl

has

the

proper

function

-LRB-

symbol

-RRB-

i.e.

the

same

function

as

the

expression

-LRB-

correctness

-RRB-

and

ffl

every

defining

rule

whose

left

hand

side

unifies

with

the

given

expression

is

candidate

defining

rule

for

this

expression

-LRB-

completeness

-RRB-

Access

to

candidate

equations

is

thus

handled

by

a

function

fundef

EXPRESSION

PROGRAM

RULES

of

which

we

assume

only

to

yield

the

-LRB-

properly

ordered

-RRB-

list

of

the

candidate

defining

rules

for

the

given

expression

in

the

given

program

The

program

is

represented

by

a

distinguished

element

pg

of

PROGRAM

-LRB-

the

program

-RRB-

Note

that

existence

of

fundef

is

all

that

we

assume

of

the

abstract

universe

PROGRAM

This

concludes

the

definition

of

the

signature

of

Babel

tree

algebras

Minor

additions

pertaining

only

to

some

special

constructs

will

be

presented

in

corresponding

sections

Notationally

since

we

deal

mainly

with

values

of

functions

at

currnode

we

usually

suppress

this

parameter

writing

bfather

bfather

-LRB-

currnode

-RRB-

cands

cands

-LRB-

currnode

-RRB-

fst

cand

fst

-LRB-

cands

-LRB-

currnode

-RRB-

-RRB-

exp

exp

-LRB-

currnode

-RRB-

pos

pos

-LRB-

currnode

-RRB-

s

s

-LRB-

currnode

-RRB-

exp

pos

and

s

constitute

the

environment

of

currnode

The

selected

exp-subexpression

the

current

expression

is

accessed

from

the

environment

by

currexp

exp

-LSB-

pos

-RSB-

Now

to

dynamics

We

assume

the

following

initialization

of

Babel

tree

algebras

The

child

of

root

is

supposed

to

be

the

initial

value

of

currnode

it

has

the

initial

expression

as

associated

expression

pos

is

the

empty

position

and

s

the

empty

substitution

the

mode

is

Eval

stop

has

value

pg

has

the

given

program

as

value

The

list

cands

of

alternative

states

is

not

-LRB-

yet

-RRB-

defined

at

currnode

This

initial

algebra

is

graphically

shown

in

Fig.

for

our

running

example

The

current

node

currnode

is

boldfaced

and

the

current

position

root

pr

-LRB-

-LSB-

g

-LRB-

X

-RRB-

g

-LRB-

Y

-RRB-

-RSB-

-LSB-

a

X

b

-RSB-

-RRB-

Eval

Figure

Initial

Algebra

pos

is

underscored

the

edges

in

the

tree

represent

the

bfather

relation

The

mode

can

be

found

in

the

right

upper

part

of

the

tree

We

now

define

the

rules

by

which

the

system

attempts

given

a

Babel

program

to

reach

a

state

with

stop

-LRB-

due

to

final

success

of

the

computation

-RRB-

or

with

stop

-LRB-

due

to

final

failure

by

backtracking

all

the

way

to

root

-RRB-

No

transition

rule

will

be

applicable

in

such

a

case

which

is

a

natural

notion

of

terminating

state

All

transition

rules

will

thus

be

tacitly

assumed

to

stand

under

the

guard

stop

We

introduce

the

following

abbreviation

for

backtracking

backtrack

if

bfather

root

then

stop

else

currnode

bfather

mode

Select

A

reduction

step

of

a

redex

f

-LRB-

tn

-RRB-

with

user-defined

f

and

arguments

ti

in

normal

form

is

split

into

applying

the

function

call

-LRB-

to

create

new

candidate

nodes

for

alternative

states

of

currnode

-RRB-

to

be

followed

by

selecting

one

of

them

We

will

correspondingly

have

two

rules

The

following

call

rule

-LRB-

Call

-RRB-

invoked

by

having

a

user

-LCB-

defined

function

call

in

Apply

mode

will

create

as

many

sons

of

currnode

as

there

are

candidate

defining

rules

in

the

procedure

definition

of

its

function

symbol

to

each

of

which

the

corresponding

defining

rule

will

be

associated

if

is

user

defined

-LRB-

currexp

-RRB-

mode

Apply

currexp

f

-LRB-

en

-RRB-

n

arity

-LRB-

f

-RRB-

then

let

-LSB-

drm

-RSB-

fundef

-LRB-

currexp

pg

-RRB-

extend

NODE

by

nm

with

bfather

-LRB-

ni

-RRB-

currnode

defrule

-LRB-

ni

-RRB-

dri

cands

-LSB-

nm

-RSB-

endextend

mode

Select

where

is

user

defined

is

a

Boolean

function

recognizing

those

expressions

whose

function

symbols

are

user

defined

-LRB-

as

opposed

to

free

constructors

and

predefined

operations

-RRB-

defrule

is

an

auxiliary

labeling

function

arity

-LRB-

f

-RRB-

indicates

the

-LRB-

program

-RRB-

arity

of

f

Note

that

expressions

and

substitutions

attached

to

candidate

sons

are

at

this

point

undefined

and

that

the

value

of

currnode

does

not

change

The

following

selection

rule

-LRB-

Sel

-RRB-

attempts

to

select

a

candidate

state

-LRB-

selecting

thereby

the

associated

rule

-RRB-

If

there

is

the

system

backtracks

If

the

left

hand

side

of

the

renamed

selected

rule

does

not

unify

with

the

call

pattern

the

corresponding

son

is

erased

from

the

list

of

candidates

Otherwise

the

selected

rule

of

the

first

successful

candidate

is

activated

the

corresponding

son

becomes

the

value

of

currnode

in

Eval

mode

-LRB-

and

gets

erased

from

its

father

's

cands

list

-RRB-

its

environment

is

defined

as

the

result

of

narrowing

applied

to

the

current

environment

replacing

in

currnode

's

expression

the

current

subexpression

by

the

right

hand

side

of

the

rule

and

applying

the

unifying

substitution

to

both

s

and

-LRB-

new

-RRB-

exp

for

which

the

evaluation

has

to

proceed

then

Renaming

of

variables

in

an

expression

e

is

realized

abstractly

by

introducing

the

current

variable

renaming

index

vi

N

and

denoting

by

rename

-LRB-

e

vi

-RRB-

or

shorter

the

result

of

e

consider

these

functions

here

as

static

i.e.

functions

which

are

known

and

not

updated

by

rules

One

might

view

them

as

procedures.See

-LSB-

-RSB-

-LRB-

i

-RRB-

is

-LRB-

next

to

the

right

-RRB-

brother

position

of

u

i

u

is

the

leftmost

child

of

position

u

and

u

is

father

of

all

positions

u

i.

after

renaming

of

all

variables

at

level

vi

The

update

vi

vi

ensures

freshness

of

subsequent

renamings

if

mode

Select

thenif

cands

-LSB-

-RSB-

then

backtrack

else

let

-LRB-

Lhs

Rhs

-RRB-

defrule

-LRB-

fst

cand

-RRB-

let

mgu

-LRB-

currexp

Lhs

-RRB-

cands

rest

-LRB-

cands

-RRB-

if

then

go

fst

cand

in

Eval

narrow

curr

env

-LRB-

Rhs

-RRB-

vi

vi

with

fst

cand

denoting

the

first

element

in

cands

and

with

abbreviations

go

fst

cand

in

Eval

currnode

fst

cand

mode

Eval

narrow

curr

env

-LRB-

E

-RRB-

exp

-LRB-

fst

cand

-RRB-

exp

-LSB-

pos

E

-RSB-

pos

-LRB-

fst

cand

-RRB-

pos

s

-LRB-

fst

cand

-RRB-

s

The

application

of

the

Call

and

Select

rules

for

the

running

example

is

shown

in

Fig.

Some

steps

of

the

backtracking

due

to

the

failure

of

the

left

part

of

the

tree

-LRB-

see

Fig.

-RRB-

are

shown

in

Fig.

where

discontinuous

lines

are

used

for

that

part

of

the

tree

which

will

not

be

visited

any

more

that

is

abandoned

nodes

qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq

root

cands

-LSB-

-RSB-

rule

root

cands

-LSB-

-RSB-

rule

root

s

fX/ag

pr

-LRB-

-LSB-

g

-LRB-

X

-RRB-

g

-LRB-

Y

-RRB-

-RSB-

-LSB-

a

X

b

-RSB-

-RRB-

pr

-LRB-

-LSB-

g

-LRB-

X

-RRB-

g

-LRB-

Y

-RRB-

-RSB-

-LSB-

a

X

b

-RSB-

-RRB-

pr

-LRB-

-LSB-

g

-LRB-

X

-RRB-

g

-LRB-

Y

-RRB-

-RSB-

-LSB-

a

X

b

-RSB-

-RRB-

pr

-LRB-

-LSB-

b

g

-LRB-

Y

-RRB-

-RSB-

-LSB-

a

a

b

-RSB-

-RRB-

Apply

Select

Call

Select

Eval

Figure

Call

and

Select

rules

root

cands

-LSB-

-RSB-

root

cands

-LSB-

-RSB-

rule

s

fX/ag

cands

-RSB-

root

cands

-LSB-

-RSB-

rule

s

fX/bg

pr

-LRB-

-LSB-

g

-LRB-

X

-RRB-

g

-LRB-

Y

-RRB-

-RSB-

-LSB-

a

X

b

-RSB-

-RRB-

pr

-LRB-

-LSB-

g

-LRB-

X

-RRB-

g

-LRB-

Y

-RRB-

-RSB-

-LSB-

a

X

b

-RSB-

-RRB-

pr

-LRB-

-LSB-

g

-LRB-

X

-RRB-

g

-LRB-

Y

-RRB-

-RSB-

-LSB-

a

X

b

-RSB-

-RRB-

pr

-LRB-

-LSB-

b

g

-LRB-

Y

-RRB-

-RSB-

-LSB-

a

a

b

-RSB-

-RRB-

Select

Select

Select

Select

Eval

pr

-LRB-

-LSB-

a

g

-LRB-

Y

-RRB-

-RSB-

-LSB-

a

b

b

-RSB-

-RRB-

Figure

Bactracking

Rules

for

reduction

to

normal

form

In

this

section

we

define

the

rules

for

evaluation

of

expressions

to

normal

form

Since

evaluation

follows

the

leftmost

innermost

strategy

it

is

started

by

searching

the

position

of

the

leftmost

innermost

subexpression

of

the

current

expression

which

is

not

in

normal

form

If

currexp

is

not

in

normal

form

we

check

whether

its

leftmost

subexpression

-LRB-

at

position

pos

i.e.

the

extension

of

the

current

position

by

-RRB-

is

responsible

for

this

Otherwise

we

have

to

continue

the

evaluation

for

the

next

relevant

subexpression

-LRB-

brother

or

fatherexpression

of

currexp

see

below

-RRB-

This

is

described

by

the

following

evaluation

starting

rule

-LRB-

EvStart

-RRB-

if

mode

Eval

thenif

is

not

in

normal

form

-LRB-

currexp

-RRB-

then

pos

pos

else

mode

Continue

where

is

not

in

normal

form

is

an

auxiliary

Boolean

-LCB-

valued

function

-LRB-

with

the

obvious

meaning

-RRB-

and

Continue

a

possible

value

of

mode

whose

appearence

here

signals

that

currexp

has

been

reduced

to

normal

form

Once

currexp

appears

to

be

in

normal

form

the

computation

goes

on

to

evaluate

the

next

brother

expression

if

there

is

one

otherwise

the

computation

passes

to

apply

the

outermost

symbol

of

the

father

expression

-LRB-

whose

arguments

have

been

already

reduced

to

normal

form

-RRB-

There

is

a

special

case

some

predefined

functions

are

considered

as

strict

only

in

the

first

argument

For

these

functions

once

the

first

argument

has

been

reduced

to

normal

form

the

evaluation

must

proceed

at

the

father

position

in

mode

Apply

All

this

is

formalized

by

the

following

evaluation

continuation

rule

-LRB-

EvCont

-RRB-

if

mode

Continue

pos

u

i

thenif

occurs

-LRB-

u

-LRB-

i

-RRB-

currexp

-RRB-

or

nonstrict

-LRB-

u

-RRB-

then

pos

u

mode

Apply

else

pos

u

-LRB-

i

-RRB-

mode

Eval

where

nonstrict

-LRB-

u

-RRB-

tells

whether

the

subexpression

exp

-LSB-

u

-RSB-

is

an

application

of

one

of

the

predefined

functions

which

are

strict

only

in

the

first

argument

i.e.

it

takes

one

of

the

forms

and

-LRB-

-RRB-

or

-LRB-

-RRB-

b

e

b

These

expressions

need

special

rules

given

below

to

describe

their

evaluation

If

there

is

no

father

expression

reflected

by

pos

coming

back

to

its

initial

empty

value

-LRB-

say

ffl

-RRB-

the

whole

initial

expression

has

been

reduced

to

normal

form

and

hence

the

computation

of

one

solution

has

been

completed

Then

the

computed

solution

is

added

to

the

solution

list

and

the

user

is

asked

interactively

whether

more

solutions

are

wanted

The

latter

feature

is

formalized

abstractly

by

using

a

Boolean

valued

function

more

external

in

the

sense

of

-LSB-

-RSB-

to

take

care

of

external

request

of

more

solutions

after

a

solution

has

been

found

Otherwise

the

computation

terminates

with

final

success

This

is

described

by

the

following

stop

rule

-LRB-

Stop

-RRB-

if

mode

Continue

pos

ffl

then

solution

list

-LSB-

hexp

s

jGoalvarsi

j

solution

list

-RSB-

if

more

then

backtrack

else

stop

By

s

jV

we

denote

the

restriction

of

the

substitution

s

to

the

set

of

variables

V

Goalvars

is

a

-LRB-

static

-RRB-

operation

ranging

over

a

universe

VAR

of

variables

Goalvars

is

initialized

to

be

the

set

of

variables

of

the

initial

expression

to

be

reduced

and

is

not

changed

during

the

computation

In

Apply

mode

there

are

several

cases

to

consider

depending

on

the

value

of

currexp

it

can

be

an

expression

formed

by

a

user

-LCB-

defined

function

applied

to

arguments

which

are

all

in

normal

form

-LRB-

in

which

case

narrowing

takes

place

as

described

above

by

call

and

selection

rules

-RRB-

it

may

be

a

constructor

expression

or

a

partial

application

of

a

user

defined

function

it

may

consist

of

an

application

of

the

apply

operator

and

last

but

not

least

it

may

be

the

application

of

some

other

predefined

function

For

each

of

these

cases

there

is

a

corresponding

rule

If

in

Apply

mode

currexp

is

an

expression

formed

by

a

constructor

c

with

number

of

arguments

which

does

not

exceed

the

constructor

's

arity

arity

-LRB-

c

-RRB-

or

an

expression

formed

by

a

user

-LCB-

defined

function

but

with

less

arguments

than

the

-LRB-

program

-RRB-

arity

arity

-LRB-

f

-RRB-

of

this

function

f

we

denote

this

by

a

Boolean

valued

function

is

construction

then

the

evaluation

of

currexp

has

finished

and

must

continue

at

another

-LRB-

brother

-LCB-

or

father

-LCB-

-RRB-

expression

as

described

by

the

following

construction

rule

-LRB-

Construction

-RRB-

if

mode

Apply

is

construction

-LRB-

currexp

-RRB-

then

mode

Continue

is

strict

in

its

i-th

argument

iff

f

-LRB-

ai

an

-RRB-

is

undefined

whenever

ai

is

undefined

that

partial

applications

of

user

defined

functions

are

viewed

in

BABEL

as

analogous

to

applications

of

free

constructors

If

the

current

subexpression

is

an

expression

-LRB-

-RRB-

then

the

arguments

ei

have

already

been

reduced

to

normal

form

and

due

to

well-typedness

is

a

partial

The

system

simply

adds

the

second

expression

as

further

argument

to

the

first

expression

Note

that

mode

does

not

change

This

is

described

by

the

following

partial

application

rule

-LRB-

PApp

-RRB-

ifmode

Apply

currexp

-LRB-

e

-RRB-

op

-LRB-

em

-RRB-

then

exp

exp

-LSB-

pos

op

-LRB-

em

e

-RRB-

-RSB-

where

op

is

a

constructor

or

a

function

symbol

For

the

sake

of

example

we

show

here

two

cases

for

predefined

functions

namely

the

self

-LCB-

explanatory

rules

for

conditionals

if

currexp

b

e

mode

Apply

thenif

b

true

then

exp

exp

-LSB-

pos

e

-RSB-

mode

Eval

else

backtrack

if

currexp

b

mode

Apply

then

if

b

true

then

exp

exp

-LSB-

pos

-RSB-

else

exp

exp

-LSB-

pos

-RSB-

mode

Eval

Similar

rules

for

the

other

predefined

Boolean

expressions

are

skipped

here

The

equality

rule

-LRB-

Eq

-RRB-

asking

for

unifiability

of

the

equated

expressions

relies

upon

the

abstract

unification

function

mgu

introduced

above

if

currexp

eq

-LRB-

-RRB-

mode

Apply

then

let

mgu

mgu

-LRB-

-RRB-

if

mgu

then

exp

exp

-LSB-

pos

false

-RSB-

else

exp

-LRB-

exp

-LSB-

pos

true

-RSB-

-RRB-

mgu

s

s

mgu

Babel

stack

algebras

In

order

to

come

closer

to

a

realistic

implementation

in

this

section

we

refine

Babel

tree

algebras

by

structure

sharing

for

expressions

explicit

computation

of

the

normal

form

condition

and

embedding

of

the

backtracking

tree

structure

into

a

stack

We

indicate

at

each

step

how

to

prove

the

correctness

of

the

refinement

Structure

sharing

for

expressions

Here

we

introduce

structure

sharing

for

expressions

when

during

narrowing

the

left

hand

side

of

an

equation

is

replaced

by

its

right

hand

side

we

put

only

the

source

code

expression

involved

without

applying

the

unifying

substitution

which

is

kept

apart

This

means

that

a

-RRB-

the

update

for

exp

-LRB-

fst

cand

-RRB-

exp

in

the

selection

equality

rule

respectively

is

changed

to

be

without

applying

the

unifying

substitution

and

b

-RRB-

to

those

occurrences

of

currexp

or

expressions

e

b

in

the

rules

the

substitutions

must

be

applied

where

the

condition

to

be

checked

or

the

operation

to

be

performed

on

those

expressions

really

depend

on

the

substitution

The

latter

happens

in

the

following

cases

ffl

in

currexp

when

procdef

is

used

in

the

call

rule

for

collecting

the

candidate

equations

for

narrowing

currexps

ffl

in

the

definition

of

mgu

in

the

selection

and

equality

rule

ffl

in

exp

when

solution

list

is

updated

in

the

stop

rule

ffl

in

b

for

the

test

b

true

in

the

rules

for

the

predefined

conditionals

and

e.

its

number

of

arguments

is

smaller

than

the

arity

of

its

functor

reader

will

notice

that

the

case

where

b

is

a

variable

say

X

is

missing

The

two

alternatives

for

the

computation

to

proceed

correspond

to

the

bindings

fX

trueg

and

fX

falseg

For

the

sake

of

simplicity

instead

of

describing

the

branching

by

new

rules

we

assume

X

to

be

replaced

by

an

expression

bool

gen

-LRB-

X

-RRB-

where

bool

gen

is

a

user-defined

function

assumed

to

be

in

every

program

with

BABEL

defining

rules

bool

gen

-LRB-

true

-RRB-

true

and

bool

gen

-LRB-

false

-RRB-

false

The

Call

rule

for

bool

gen

will

then

take

care

of

the

branching

This

is

indeed

what

is

done

in

the

real

implementation

refinement

in

combination

with

the

normal

form

test

refinement

of

the

next

subsection

allows

to

avoid

the

traversal

of

complex

terms

which

originated

from

substitutions

Note

that

s

always

substitutes

normal

forms

for

variables

because

the

patterns

in

rule

heads

are

normal

forms

and

because

arguments

of

a

function

application

are

reduced

to

normal

form

before

the

function

rule

is

applied

Hence

e

is

in

normal

form

iff

e

s

is

consider

here

currexps

is

not

needed

if

procdef

depends

only

on

the

outer

function

symbol

f

of

currexp

-LCB-

i.e.

if

procdef

returns

all

the

program

rules

for

f

-LCB-

But

it

would

be

needed

for

a

possible

refinement

including

some

indexing

mechanism

ffl

in

e

for

the

condition

e

op

-LRB-

em

-RRB-

in

the

partial

application

rule

e

s

is

needed

because

e

might

be

a

higher

order

variable

coming

from

a

program

rule

As

an

example

consider

F

in

the

rules

for

map

in

the

example

of

section

when

we

reduce

f.i.

map

-LRB-

plus

-LRB-

-RRB-

-LSB-

-RSB-

-RRB-

replacing

it

by

the

right

handside

of

the

second

rule

for

map

the

-LRB-

sub

-RRB-

expression

-LRB-

F

X

-RRB-

is

generated

together

with

the

substitution

fF

plus

-LRB-

-RRB-

X

We

need

to

dereference

F

for

the

partial

application

rule

in

order

to

reduce

-LRB-

F

X

-RRB-

to

plus

-LRB-

X

-RRB-

The

proof

mapping

F

-LRB-

-RRB-

-LRB-

-RRB-

is

defined

as

follows

F

preserves

all

the

universes

all

the

rules

-LRB-

in

the

sense

that

F

maps

rules

from

to

homonymous

rules

from

-RRB-

and

all

the

operations

except

exp

Given

an

instance

of

-LRB-

-RRB-

for

all

n

NODE

expF

-LRB-

-RRB-

-LRB-

n

-RRB-

-LSB-

-LRB-

n

-RRB-

-RSB-

s

-LRB-

n

-RRB-

Refining

normal

form

test

In

this

section

we

refine

the

is

normal

form

function

used

in

the

evaluation

starting

rule

The

idea

is

to

traverse

an

expression

in

mode

Eval

until

a

non

decomposable

normal

form

namely

a

variable

a

constructor

symbol

or

a

user

-LCB-

defined

non

evaluable

function

symbol

-LRB-

i.e.

of

arity

-RRB-

is

reached

Thus

the

new

evaluation

starting

rule

if

mode

Eval

thenif

atomic

-LRB-

currexp

-RRB-

then

pos

pos

else

mode

Continue

where

atomic

-LRB-

e

-RRB-

recognizes

if

the

expression

e

is

a

variable

a

constructor

symbol

c

or

a

function

symbol

f

with

arity

-LRB-

f

-RRB-

The

proof

mapping

F

-LRB-

-RRB-

-LRB-

-RRB-

preserves

all

the

universes

and

operations

and

maps

to

homonymous

except

the

evaluation

starting

rule

A

single

application

of

this

rule

in

may

correspond

to

a

sequence

of

rules

in

if

currexp

is

in

normal

form

and

of

form

-LRB-

tn

-RRB-

with

n

and

a

constructor

symbol

c

or

a

user

-LCB-

defined

function

symbol

f

with

n

arity

-LRB-

f

-RRB-

In

this

case

-LRB-

-RRB-

requires

a

sequence

of

rules

among

evaluation

starting/continuation/stop

rules

in

order

to

recognize

that

the

subexpression

is

in

normal

form

The

sequence

traversal

-LRB-

currexp

-RRB-

of

which

corresponds

to

an

appearance

of

EvStart

in

a

sequence

of

is

defined

by

traversal

-LRB-

e

-RRB-

EvStart

if

e

X

j

c

j

f

traversal

-LRB-

-LRB-

tn

-RRB-

-RRB-

EvStart

traversal

-LRB-

-RRB-

EvCont

traversal

-LRB-

tn

-RRB-

EvCont

Construction

if

e

-LRB-

tn

-RRB-

where

c

or

f

n

arity

-LRB-

f

-RRB-

From

backtracking

tree

to

backtracking

stack

Classification

of

Babel

tree

nodes

suggests

a

straightforward

stack

representation

disregarding

the

abandoned

nodes

-LRB-

since

they

once

abandoned

play

no

further

role

in

the

computation

-RRB-

we

may

view

the

path

of

active

nodes

as

a

stack

if

cands

lists

are

represented

in

some

other

way

It

suffices

to

change

the

signature

of

cands

to

cands

NODE

frootg

RULES

and

to

update

when

creating

-LRB-

and

passing

control

to

-RRB-

one

new

state

by

action

of

the

Call

rule

the

value

of

cands

for

this

new

state

to

the

complete

list

of

rules

applicable

to

that

call

Backtracking

will

then

consist

in

looking

for

remaining

candidate

rules

In

the

following

new

call

rule

we

create

therefore

one

new

state

which

becomes

current

state

which

upon

failure

will

backtrack

to

currnode

and

which

through

appropriate

decoration

of

its

cands

value

will

control

execution

of

the

alternatives

for

the

current

call

To

keep

our

correctness

proof

simple

we

let

here

the

new

node

fetch

its

environment

for

narrowing

from

its

backtracking

father

currnode

this

we

come

closer

to

implementations

where

in

the

compilation

process

all

the

expressions

in

the

source

program

are

traversed

and

code

instructions

are

generated

for

them

that

n

arity

-LRB-

f

-RRB-

would

mean

that

currexpis

not

in

normal

form

and

that

the

call

rule

could

be

applied

the

later

refinement

to

states

controlling

subcomputations

a

copy

of

the

narrowing

environment

will

be

stored

at

the

new

node

as

it

happens

in

the

IBAM

if

is

user

defined

-LRB-

currexp

-RRB-

mode

Apply

currexp

f

-LRB-

en

-RRB-

n

arity

-LRB-

f

-RRB-

then

extend

NODE

by

N

with

bfather

-LRB-

N

-RRB-

currnode

currnode

N

cands

-LRB-

N

-RRB-

fundef

-LRB-

currexp

pg

-RRB-

endextend

mode

Select

The

new

selection

rule

checks

whether

there

are

-LRB-

still

-RRB-

rules

to

consider

in

positive

case

it

executes

the

narrowing

step

and

restores

at

currnode

the

relevant

information

-LRB-

expression

position

substitution

-RRB-

for

the

now

starting

evaluation

of

the

narrowed

expression

Remember

that

this

information

is

kept

by

the

backtracking

father

-LRB-

which

is

responsible

for

the

currently

executed

call

-RRB-

if

mode

Select

thenif

cands

-LSB-

-RSB-

then

backtrack

else

let

-LRB-

Lhs

Rhs

-RRB-

fst

cands

let

mgu

-LRB-

exp

-LRB-

bfather

-RRB-

-LSB-

pos

-LRB-

bfather

-RRB-

-RSB-

s

-LRB-

bfather

-RRB-

Lhs

-RRB-

cands

rest

-LRB-

cands

-RRB-

if

then

mode

Eval

narrow

bfather

env

-LRB-

Rhs

-RRB-

vi

vi

where

narrow

bfather

env

-LRB-

E

-RRB-

exp

exp

-LRB-

bfather

-RRB-

-LSB-

pos

-LRB-

bfather

-RRB-

E

-RSB-

pos

pos

-LRB-

bfather

-RRB-

s

s

-LRB-

bfather

-RRB-

The

effect

of

the

new

Call

and

Select

rules

for

our

running

example

is

shown

in

Fig.

qq

root

cands

-LSB-

-RSB-

root

cands

-LSB-

-RSB-

cands

-LSB-

-RSB-

root

cands

-LSB-

-RSB-

s

fX/ag

cands

-LSB-

-RSB-

pr

-LRB-

-LSB-

g

-LRB-

X

-RRB-

g

-LRB-

Y

-RRB-

-RSB-

-LSB-

a

X

b

-RSB-

-RRB-

pr

-LRB-

-LSB-

g

-LRB-

X

-RRB-

g

-LRB-

Y

-RRB-

-RSB-

-LSB-

a

X

b

-RSB-

-RRB-

pr

-LRB-

-LSB-

g

-LRB-

X

-RRB-

g

-LRB-

Y

-RRB-

-RSB-

-LSB-

a

X

b

-RSB-

-RRB-

pr

-LRB-

-LSB-

b

g

-LRB-

Y

-RRB-

-RSB-

-LSB-

a

X

b

-RSB-

-RRB-

Call

Select

Apply

Select

Eval

Figure

Call

and

Select

rules

in

Stack

Algebras

For

the

definition

of

backtrack

to

work

properly

cands

must

be

initialized

in

this

algebra

to

be

-LSB-

-RSB-

for

the

child

of

root

-LRB-

the

initial

currnode

-RRB-

Otherwise

since

the

new

call

rule

attributes

cands

to

the

child

created

for

currnode

cands

would

remain

undefined

for

the

child

of

root

and

the

condition

cands

-LSB-

-RSB-

in

the

selection

rule

would

crash

if

backtracking

takes

place

at

the

child

of

root

We

define

the

proof

mapping

by

a

function

F

which

associates

homonymous

rules

and

maps

stack

elements

to

tree

nodes

using

an

auxiliary

function

F

NODE

such

that

exp

-LRB-

F

-LRB-

n

-RRB-

-RRB-

exp

-LRB-

bfather

-LRB-

n

-RRB-

-RRB-

pos

-LRB-

F

-LRB-

n

-RRB-

-RRB-

pos

-LRB-

bfather

-LRB-

n

-RRB-

-RRB-

s

-LRB-

F

-LRB-

n

-RRB-

-RRB-

s

-LRB-

bfather

-LRB-

n

-RRB-

-RRB-

cands

-LRB-

F

-LRB-

n

-RRB-

-RRB-

mk

cands

-LRB-

F

-LRB-

n

-RRB-

cands

-LRB-

n

-RRB-

-RRB-

bfather

-LRB-

F

-LRB-

n

-RRB-

-RRB-

F

-LRB-

bfather

-LRB-

n

-RRB-

-RRB-

F

-LRB-

root

-RRB-

root

where

mk

cands

-LRB-

n

-LSB-

lm

-RSB-

-RRB-

-LSB-

hn

hn

lmi

-RSB-

keeping

in

mind

that

candidate

nodes

in

the

tree

can

be

thought

of

as

h

node

defrule

i

pairs

since

these

are

their

only

decorations

Consequently

when

fst

cands

is

chosen

by

rule

then

F

-LRB-

-RRB-

is

a

pair

hn

fst

candsi

Babel

graph

algebras

In

this

section

the

normal

form

computation

for

expressions

will

be

localized

In

order

to

keep

the

correctness

proof

simple

this

is

done

in

two

steps

introduction

of

states

which

control

normalization

of

subexpressions

completed

by

subsequent

replacement

of

global

by

dedicated

local

environments

States

controlling

subcomputations

Up

to

now

states

-LRB-

nodes

of

NODE

-RRB-

reflect

only

backtracking

They

are

created

when

a

function

call

is

made

for

reducing

currexp

but

their

role

does

not

correspond

exactly

to

computing

just

that

subexpression

In

fact

if

no

more

calls

are

performed

the

created

state

remains

current

one

until

the

end

of

the

computation

for

the

whole

exp

We

now

make

states

responsible

only

for

the

computation

of

the

corresponding

subexpression

Once

this

computation

is

finished

control

will

be

returned

-LRB-

by

a

return

rule

-RRB-

to

the

state

which

was

current

in

the

moment

of

the

call

We

will

call

this

state

the

activator

of

the

state

created

by

the

call

and

denote

it

by

a

function

act

node

from

NODE

to

NODE

This

calling

structure

induces

a

new

tree

structure

in

NODEwhich

is

actually

the

core

of

the

IBAM

and

imposes

two

changes

in

connection

with

backtracking

ffl

currnode

may

now

be

updated

also

by

the

new

return

rule

Consequently

when

a

new

state

is

created

by

a

call

currnode

may

not

represent

the

last

created

state

to

which

to

backtrack

from

the

new

state

Therefore

a

function

-LRB-

global

variable

-RRB-

lastnode

is

introduced

to

store

the

-LRB-

chronologically

-RRB-

last

created

node

Initially

lastnode

is

set

to

be

the

child

of

root

ffl

All

the

alternatives

for

a

given

call

have

to

be

tried

with

the

same

environment

namely

exp

pos

s

as

they

were

in

the

moment

of

the

call

In

the

previous

algebras

these

values

were

accessed

from

the

environment

of

the

backtracking

father

This

will

not

be

safe

any

more

since

exp

pos

and

s

for

the

backtracking

father

could

have

changed

in

the

meantime

had

control

come

back

to

it

by

the

return

rule

Therefore

in

the

moment

of

a

call

safe

copies

of

exp

pos

and

s

are

stored

in

the

new

state

We

denote

these

values

by

functions

act

exp

act

pos

act

s

defined

on

NODE

act

node

act

env

lastnode

are

handled

by

additional

updates

to

Call

and

Sel

rules

Since

in

this

first

step

towards

local

subexpression-normalization

the

global

expressions

are

still

kept

-LRB-

to

simplify

the

proof

map

-RRB-

as

decorations

of

states

we

obtain

the

modified

call

rule

by

changing

the

update

of

bfather

to

bfather

-LRB-

N

-RRB-

lastnode

and

by

adding

the

updates

act

node

-LRB-

N

-RRB-

currnode

and

store

act

env

at

N

using

the

abbreviation

store

act

env

at

N

act

exp

-LRB-

N

-RRB-

exp

act

pos

-LRB-

N

-RRB-

pos

act

s

-LRB-

N

-RRB-

s

if

is

user

defined

-LRB-

currexp

-RRB-

mode

Apply

currexp

f

-LRB-

en

-RRB-

n

arity

-LRB-

f

-RRB-

then

extend

NODE

by

N

with

currnode

N

cands

-LRB-

N

-RRB-

fundef

-LRB-

currexp

s

pg

-RRB-

bfather

-LRB-

N

-RRB-

lastnode

act

node

-LRB-

N

-RRB-

currnode

store

act

env

at

N

endextend

mode

Select

The

modified

select

rule

is

literally

the

same

as

before

replacing

bfather

env

by

act

env

both

in

the

definition

of

the

mgu

and

in

the

abbreviation

narrow

bfather

env

Formally

mgu

-LRB-

act

exp

-LSB-

act

pos

-RSB-

act

s

Lhs

-RRB-

narrow

act

env

-LRB-

E

-RRB-

exp

act

exp

-LSB-

act

pos

E

-RSB-

pos

act

pos

s

act

s

In

addition

lastnode

to

which

bfather

will

be

set

by

the

following

execution

of

Call

rule

is

updated

to

currnode

The

modified

evaluation

continuation

rule

obtains

the

additional

test

whether

the

current

position

is

root

of

a

subcomputation

which

has

been

activated

by

currnode

in

this

case

it

switches

to

a

new

mode

Return

Formally

it

is

sufficient

to

replace

the

guard

in

the

evaluation

continuation

rule

by

the

following

one

if

mode

Continue

thenif

pos

act

pos

then

mode

Return

else

let

u

i

pos

if

occurs

-LRB-

u

-LRB-

i

-RRB-

currexp

-RRB-

or

nonstrict

-LRB-

u

-RRB-

For

technical

convenience

we

assume

that

act

pos

is

initialized

to

be

ffl

for

the

child

of

root

which

means

that

the

switching

to

mode

Return

will

also

happen

when

the

evaluation

of

the

whole

initial

expression

finishes

-LRB-

in

this

case

pos

act

pos

ffl

-RRB-

As

a

consequence

the

condition

mode

Continue

pos

ffl

in

the

Stop

rule

changes

to

mode

Return

bfather

root

in

the

modified

stop

rule

The

new

mode

Return

is

governed

by

a

Return

rule

-LRB-

Ret

-RRB-

through

which

control

is

returned

to

the

activating

node

with

expression

-LRB-

still

globally

-RRB-

updated

by

the

result

of

the

just

terminated

subcomputation

if

mode

Return

bfather

root

then

currnode

act

node

return

curr

env

to

act

env

mode

Continue

with

the

abbreviation

return

curr

env

to

act

env

exp

-LRB-

act

node

-RRB-

exp

pos

-LRB-

act

node

-RRB-

pos

s

-LRB-

act

node

-RRB-

s

In

Fig.

some

relevant

steps

for

our

running

example

are

displayed

We

have

graphically

emphasized

the

act

node

relation

which

is

represented

by

solid

edges

The

resulting

tree

can

be

understood

as

a

task

tree

The

bfather

relation

is

represented

by

discontinous

arrows

while

lastnode

is

no

explicitly

drawn

since

it

is

always

the

deepmost

rightmost

active

node

Following

these

arrows

from

lastnode

up

to

root

the

backtracking

stack

can

be

reconstructed

For

the

sake

of

clarity

the

decoration

of

nodes

is

omitted

when

unchanged

or

irrelevant

At

step

-LRB-

a

-RRB-

the

Call

rule

is

going

to

be

applied

for

narrowing

g

-LRB-

X

-RRB-

A

new

node

is

created

and

activated

for

this

purpose

its

backtracking

father

and

activator

node

being

the

previously

current

one

The

activator

environment

-LRB-

which

is

framed

in

the

picture

-RRB-

is

copied

into

-LRB-

step

-LRB-

b

-RRB-

-RRB-

This

safe

copy

will

remain

unchanged

for

all

the

life

of

After

using

the

program

rule

for

narrowing

g

-LRB-

X

-RRB-

into

b

step

-LRB-

c

-RRB-

will

be

reached

where

the

Return

rule

gives

back

the

current

environment

to

the

activator

as

the

result

of

the

subcomputation

-LRB-

step

-LRB-

d

-RRB-

-RRB-

The

computation

proceeds

under

the

control

of

and

after

some

steps

the

nodes

and

will

be

created

for

reducing

g

-LRB-

Y

-RRB-

and

prefix

-LRB-

-LSB-

b

b

-RSB-

-LSB-

a

X

b

-RSB-

-RRB-

respectively

both

and

have

as

activator

but

their

backtracking

fathers

are

and

respectively

since

the

latter

were

the

respective

last

nodes

when

and

were

introduced

-LRB-

steps

-LRB-

e

-RRB-

and

-LRB-

f

-RRB-

-RRB-

In

-LRB-

g

-RRB-

we

show

the

situation

encountered

when

is

reactivated

for

trying

the

alternative

rule

after

the

failure

of

all

the

alternatives

for

and

its

created

descendants

-LRB-

they

are

abandoned

nodes

now

-RRB-

is

at

this

point

as

it

was

in

-LRB-

f

-RRB-

Neither

the

environment

of

its

activator

-LRB-

which

also

remains

as

it

was

in

-LRB-

f

-RRB-

-RRB-

or

of

its

backtracking

father

-LRB-

which

remains

as

it

was

in

-LRB-

c

-RRB-

-RRB-

can

be

used

for

restoring

the

the

computation

to

the

state

as

it

was

when

was

created

This

clearly

shows

the

role

of

the

safe

copy

stored

together

with

Finally

-LRB-

i

-RRB-

illustrates

how

a

succesful

derivation

is

going

to

finish

in

mode

Return

with

the

child

of

root

as

currnode

having

the

empty

position

and

the

computed

normal

form

and

substitution

as

environment

The

Stop

rule

would

add

the

computed

solution

to

the

list

of

solutions

and

would

ask

the

user

for

more

solutions

to

be

computed

by

backtracking

For

the

proof

mapping

F

from

-LRB-

-RRB-

to

-LRB-

-RRB-

let

be

an

instance

of

-LRB-

-RRB-

The

instance

F

-LRB-

-RRB-

of

-LRB-

-RRB-

is

defined

as

follows

let

Sn

be

a

bfather-chain

from

to

i.e.

Sn

Si

-LRB-

Si

-RRB-

i

n

Then

we

define

Sng

Sn

The

for

Sn

is

that

of

pr

-LRB-

-LSB-

b

b

-RSB-

-LSB-

a

X

b

-RSB-

-RRB-

s

fX/a

Y/ag

Env

Env

root

root

cands

-LSB-

-RSB-

Env

s

fX/ag

root

root

s

fX/ag

pr

-LRB-

-LSB-

b

g

-LRB-

Y

-RRB-

-RSB-

-LSB-

a

X

b

-RSB-

-RRB-

En

v

cands

-LSB-

-RSB-

root

cands

-LSB-

-RSB-

Env

Env

s

fX/ag

root

Env

cands

-LSB-

-RSB-

-LRB-

-LSB-

b

b

-RSB-

-LSB-

a

X

b

-RSB-

-RRB-

s

fX/a

Y/ag

Env

cands

-LSB-

-RSB-

root

cands

-LSB-

-RSB-

root

root

Env

pr

-LRB-

-LSB-

b

a

-RSB-

-LSB-

a

X

b

-RSB-

-RRB-

s

fX/a

Y/bg

cands

-LSB-

-RSB-

true

s

fX/b

Y/ag

Apply

Select

Return

Select

Select

pr

-LRB-

-LSB-

g

-LRB-

X

-RRB-

g

-LRB-

Y

-RRB-

-RSB-

-LSB-

a

X

b

-RSB-

-RRB-

pr

-LRB-

-LSB-

b

g

-LRB-

Y

-RRB-

-RSB-

-LSB-

a

X

b

-RSB-

-RRB-

pr

-LRB-

-LSB-

b

g

-LRB-

Y

-RRB-

-RSB-

-LSB-

a

X

b

-RSB-

-RRB-

Select

Eval

Return

Continue

-LRB-

a

-RRB-

-LRB-

b

-RRB-

-LRB-

c

-RRB-

-LRB-

d

-RRB-

-LRB-

e

-RRB-

-LRB-

f

-RRB-

-LRB-

g

-RRB-

-LRB-

h

-RRB-

-LRB-

i

-RRB-

Figure

Graph

Algebras

-LRB-

Sn

-RRB-

-LRB-

-RRB-

-LRB-

Sn

-RRB-

-LRB-

-RRB-

-LRB-

Sn

-RRB-

-LRB-

-RRB-

The

for

the

remaining

nodes

in

is

reconstructed

from

the

act

env

in

the

backtracking

chain

therefore

for

all

i

n

-LRB-

Si

-RRB-

act

-LRB-

Si

-RRB-

-LRB-

Si

-RRB-

act

-LRB-

Si

-RRB-

-LRB-

Si

-RRB-

act

-LRB-

Si

-RRB-

is

the

same

as

if

it

is

not

Return

Otherwise

is

Continue

For

the

mapping

of

a

sequence

of

rules

in

to

a

sequence

in

let

us

notice

that

a

single

occurrence

of

EvCont

in

is

followed

in

by

Ret

EvCont

if

it

correspond

to

a

just

finished

subcomputation

-LRB-

more

of

such

pairs

of

rules

will

appear

if

there

is

a

chain

of

returns

-RRB-

At

the

end

of

the

computation

Stop

applies

in

while

EvCont

Stop

are

needed

in

Therefore

given

is

the

result

of

deleting

in

the

pairs

EvCont

Ret

and

replacing

EvCont

Stop

by

Stop

Subcomputations

with

local

expressions

In

this

refinement

step

the

global

environments

are

replaced

by

local

ones

i.e.

handling

of

act

env

-LRB-

narrowing

of

and

returning

to

in

Selection

and

Return

rules

-RRB-

is

done

with

the

relevant

local

expression

For

the

new

selection

rule

narrow

act

env

-LRB-

Rhs

-RRB-

is

refined

using

the

local

updates

exp

pos

ffl

In

the

new

return

rule

the

subcomputation

result

is

returned

to

act

node

by

placing

it

into

the

expression

of

act

node

at

its

activation

position

-LRB-

thus

making

it

global

relative

to

act

node

-RRB-

Formally

this

means

to

refine

return

curr

env

to

act

env

assigning

act

exp

-LSB-

act

pos

exp

-RSB-

to

exp

-LRB-

act

node

-RRB-

and

act

pos

to

pos

-LRB-

act

node

-RRB-

Notice

that

the

use

of

the

safe

copies

act

exp

act

pos

of

the

activator

environment

has

moved

from

the

Select

rule

to

the

Return

rule

In

addition

due

to

the

redefinition

of

pos

in

the

selection

rule

the

condition

pos

act

pos

in

EvCont

rule

for

switching

to

mode

Return

must

be

replaced

by

pos

ffl

The

proof

mapping

F

from

-LRB-

-RRB-

to

-LRB-

-RRB-

preserves

all

universes

rules

and

operations

except

exp

pos

act

exp

and

act

pos

which

are

local

in

For

a

given

instance

of

we

define

F

-LRB-

-RRB-

as

follows

for

each

S

NODE

frootg

-LRB-

S

-RRB-

if

bfather

-LRB-

S

-RRB-

root

then

-LRB-

S

-RRB-

else

u

where

act

-LRB-

S

-RRB-

u

-LRB-

S

-RRB-

-LRB-

S

-RRB-

if

bfather

-LRB-

S

-RRB-

root

then

-LRB-

S

-RRB-

else

-LRB-

S

-RRB-

-LSB-

act

-LRB-

S

-RRB-

-RSB-

act

-LRB-

S

-RRB-

if

bfather

-LRB-

S

-RRB-

root

then

act

-LRB-

S

-RRB-

else

u

where

act

-LRB-

act

-LRB-

S

-RRB-

-RRB-

u

act

-LRB-

S

-RRB-

act

-LRB-

S

-RRB-

if

bfather

-LRB-

S

-RRB-

root

then

act

-LRB-

S

-RRB-

else

act

-LRB-

S

-RRB-

-LSB-

act

-LRB-

S

-RRB-

-RSB-

Optimizations

In

this

section

we

introduce

abstract

versions

of

some

time

and

space

optimizations

in

the

IBAM

Their

role

and

justification

are

much

better

understood

in

this

more

abstract

setting

than

at

the

implementation

level

Return

without

restoring

When

a

task

returns

a

value

it

is

not

always

needed

to

use

the

safe

copies

-LRB-

act

exp

act

pos

-RRB-

of

its

activator

for

returning

to

it

the

computed

value

In

some

situations

the

current

values

exp

-LRB-

act

node

-RRB-

pos

-LRB-

act

node

-RRB-

is

another

optimization

in

the

IBAM

which

is

not

covered

by

this

section

namely

detection

of

deterministic

computations

also

called

dynamic

cut

It

consists

in

the

following

when

applying

a

program

rule

for

narrowing

an

expression

if

no

variable

becomes

bound

during

unification

with

the

head

of

the

rule

and

solving

the

conditions

in

the

guard

then

there

is

no

need

of

trying

alternative

rules

since

they

can

not

produce

more

general

solutions

This

should

be

not

considered

as

an

optimization

of

the

implementation

but

of

BABEL

operational

semantics

itself

hence

the

natural

place

for

considering

it

is

at

the

most

abstract

description

level

of

the

operational

semantics

It

would

not

be

difficult

to

do

so

and

to

proof

the

correctness

of

the

optimization

relying

on

the

theorems

-LSB-

-RSB-

stating

the

safeness

of

dynamic

cut

can

be

used

instead

for

this

purpose

This

will

save

the

time

of

restoring

the

activator

with

these

safe

values

The

obvious

conditions

for

this

to

be

correct

are

-LRB-

i

-RRB-

act

exp

-LSB-

act

pos

exp

-RSB-

exp

-LRB-

act

node

-RRB-

-LSB-

pos

-LRB-

act

node

-RRB-

exp

-RSB-

-LRB-

ii

-RRB-

pos

-LRB-

act

node

-RRB-

act

pos

But

-LRB-

ii

-RRB-

can

only

hold

-LRB-

in

mode

Return

-RRB-

if

act

pos

ffl

or

if

it

is

the

first

time

the

current

task

returns

a

value

to

its

activator

and

in

both

cases

-LRB-

i

-RRB-

is

always

verified

-LRB-

trivially

for

the

first

case

and

because

nothing

has

been

done

under

the

control

of

act

node

since

currnode

was

activated

-RRB-

The

optimized

return

rule

simply

needs

to

transform

the

updates

of

exp

-LRB-

act

node

-RRB-

and

pos

-LRB-

act

node

-RRB-

into

conditional

updates

distinguishing

the

optimizable

cases

from

the

rest

This

means

to

refine

once

more

the

abbreviation

return

curr

env

to

act

env

to

if

pos

-LRB-

act

node

-RRB-

act

pos

then

exp

-LRB-

act

node

-RRB-

exp

-LRB-

act

node

-RRB-

-LSB-

pos

-LRB-

act

node

-RRB-

exp

-RSB-

else

exp

-LRB-

act

node

-RRB-

act

exp

-LSB-

act

pos

exp

-RSB-

pos

-LRB-

act

node

-RRB-

act

pos

s

-LRB-

act

node

-RRB-

s

This

optimization

covers

the

first

return

optimization

of

the

IBAM

which

corresponds

to

the

second

case

discussed

above

Optimized

last

return

When

a

a

value

is

returned

by

a

task

which

is

the

last

-LRB-

active

-RRB-

created

task

and

has

an

empty

list

of

alternatives

then

this

task

will

not

perform

any

other

succesful

computation

If

by

backtracking

it

is

reactivated

later

on

it

will

fail

immediately

and

backtracking

will

be

done

to

its

backtracking

father

We

can

anticipate

this

situation

by

resetting

lastnode

to

the

backtracking

father

of

the

task

-LRB-

the

task

node

itself

could

in

fact

be

collected

as

garbage

-RRB-

The

optimized

last

return

rule

expresses

this

adding

a

conditional

update

for

lastnode

if

lastnode

currnode

cands

-LSB-

-RSB-

then

lastnode

bfather

Note

that

the

effect

of

this

optimization

and

the

previous

one

could

be

combined

into

a

single

optimized

return

rule

Optimized

call

This

optimization

complements

the

last

return

optimization

It

consists

in

the

following

if

the

current

node

is

the

last

created

one

the

list

of

its

alternatives

is

empty

and

furthermore

a

call

creating

a

child

node

is

going

to

be

done

as

the

last

thing

to

do

for

then

the

value

returned

by

will

be

returned

also

as

the

value

for

This

transmission

of

the

values

returned

by

maybe

several

times

because

of

backtracking

on

will

be

the

only

role

of

in

the

future

-LRB-

because

has

no

alternatives

and

there

is

no

task

between

-RRB-

This

fact

suggests

that

we

can

reuse

the

node

corresponding

to

for

The

optimized

call

rule

only

needs

an

additional

test

for

deciding

if

the

node

can

be

reused

if

is

user

defined

-LRB-

currexp

-RRB-

mode

Apply

currexp

f

-LRB-

en

-RRB-

n

arity

-LRB-

f

-RRB-

thenif

lastnode

currnode

cands

-LSB-

-RSB-

pos

ffl

then

cands

fundef

-LRB-

currexp

pg

-RRB-

The

node

is

reused

else

Conclusions

and

future

work

Starting

from

related

work

for

Prolog

and

the

WAM

we

have

used

Evolving

Algebras

to

give

a

description

of

the

innermost

narrowing

semantics

for

the

functional

logic

language

BABEL

and

we

have

specified

a

series

of

provably

correct

refinement

steps

towards

a

definition

of

BABEL

's

implementation

by

the

innermost

graph-narrowing

abstract

machine

IBAM

-LSB-

-RSB-

Thus

we

have

paved

the

way

for

a

full

correctness

proof

for

IBAM

which

we

believe

will

arise

quite

naturally

by

further

refinement

of

the

current

description

This

will

be

subject

of

future

work

Moreover

we

plan

to

develop

a

description

of

lazy

semantics

and

implementation

techniques

for

BABEL

which

involves

a

more

difficult

kind

of

control

-LSB-

-RSB-

and

will

require

substantial

modifications

of

the

present

description

References

-LSB-

-RSB-

H

A?it

Kaci

Warren

's

Abstract

Machine

A

Tutorial

Reconstruction

MIT

Press

-LSB-

-RSB-

H

A?it

Kaci

and

R.

Nasr

Integrating

logic

and

functional

programming

Lisp

and

Symbolic

Computation

-LCB-

-LSB-

B

-RSB-

E.

B?orger

A

logical

operational

semantics

for

full

Prolog

Part

i

Selection

core

and

control

In

E.

B?orger

H.

Kleine

B?uning

M.

M.

Richter

and

W.

Sch?onfeld

editors

CSL

Workshop

on

Computer

Science

Logic

volume

of

Lecture

Notes

in

Computer

Science

pages

-LCB-

Springer

-LSB-

B

-RSB-

E.

B?orger

A

logical

operational

semantics

for

full

Prolog

Part

ii

Built-in

predicates

for

database

manipulations

In

B.

Rovan

editor

Mathematical

Foundations

of

Computer

Science

volume

of

Lecture

Notes

in

Computer

Science

pages

-LCB-

Springer

-LSB-

-RSB-

C.

Beierle

and

E.

B?orger

Correctness

proof

for

the

WAM

with

types

In

E.

B?orger

G.

J?ager

H.

Kleine

B?uning

and

M.

M.

Richter

editors

Computer

Science

Logic

volume

of

Lecture

Notes

in

Computer

Science

pages

-LCB-

Springer

-LSB-

-RSB-

M.

Bellia

and

G.

Levi

The

relation

between

logic

and

functional

languages

Journal

of

Logic

Programming

-LCB-

-LSB-

-RSB-

E.

B?orger

and

D.

Rosenzweig

A

formal

specification

of

Prolog

by

tree

algebras

In

V.

Ceri?c

V.

Dobri?c

V.

Lu

zar

and

R.

Paul

editors

Information

Technology

Interfaces

pages

-LCB-

Zagreb

University

Computing

Centre

-LSB-

-RSB-

E.

B?orger

and

D.

Rosenzweig

A

simple

mathematical

model

for

full

Prolog

Research

report

Dipartimento

di

Informatica

Universit?a

di

Pisa

Pisa

October

to

appear

in

Sciene

of

Computer

Programming

-LSB-

-RSB-

E.

B?orger

and

D.

Rosenzweig

The

WAM

-LCB-

definition

and

compiler

correctness

Research

report

Dipartimento

di

Informatica

Universit?a

di

Pisa

Pisa

to

appear

in

Logic

Programming

Formal

Methods

and

Practical

Applications

-LRB-

C.Beierle

L.Pl?umer

Eds

-RRB-

NorthHolland

Series

in

Computer

Science

and

Artificial

Intelligence

-LSB-

-RSB-

E.

B?orger

and

E.

Riccobene

A

formal

specification

of

Parlog

In

M.

Droste

and

Y.

Gurevich

editors

Semantics

of

Programming

Languages

and

Model

Theory

Gordon

and

Breach

pages

-LSB-

-RSB-

E.

B?orger

and

P.

Schmitt

A

formal

operational

semantics

for

languages

of

type

Prolog

III

In

E.

B?orger

H.

Kleine

B?uning

M.

M.

Richter

and

W.

Sch?onfeld

editors

CSL

Workshop

on

Computer

Science

Logic

volume

of

Lecture

Notes

in

Computer

Science

pages

-LCB-

Springer

-LSB-

-RSB-

N.

Dershowitz

and

J.P.

Jouannaud

Rewrite

systems

In

J.

van

Leeuwen

editor

Handbook

of

Theoretical

Computer

Science

volume

B

pages

-LCB-

Elsevier

Noth-Holland

-LSB-

-RSB-

D.

DeGroot

and

G.

Lindstrom

editors

Logic

Programming

Functions

Relations

Equations

Prentice

Hall

-LSB-

-RSB-

A.J.

Field

and

P.G.

Harrison

Functional

Programming

Addison

Wesley

-LSB-

-RSB-

Y.

Gurevich

Logic

and

the

challenge

of

computer

science

In

E.

B?orger

editor

Current

Trends

in

Theoretical

Computer

Science

pages

-LCB-

Computer

Science

Press

-LSB-

-RSB-

Y.

Gurevich

Evolving

algebras

A

tutorial

introduction

Bulletin

of

EATCS

-LCB-

-LSB-

-RSB-

H.

Kuchen

R.

Loogen

J.J

Moreno

Navarro

and

M.

Rodr?iguez

Artalejo

Graph-based

implementation

of

a

functional

logic

language

In

ESOP

volume

of

Lecture

Notes

in

Computer

Science

pages

-LCB-

Springer

-LSB-

-RSB-

R.

Loogen

Relating

the

implementation

techniques

of

functional

and

functional

logic

languages

to

appear

in

New

Generation

Computing

-LSB-

-RSB-

R.

Loogen

and

S.

Winkler

Dinamyc

detection

of

determinism

in

functional

logic

languages

In

International

Symposium

on

Programming

Language

Implementation

and

Logic

Programming

-LRB-

PLILP

-RRB-

volume

of

Lecture

Notes

in

Computer

Science

pages

-LCB-

Springer

-LSB-

-RSB-

J.J

Moreno

Navarro

H.

Kuchen

R.

Loogen

and

M.

Rodr?iguez

Artalejo

Lazy

narrowing

in

a

graph

machine

In

International

Conference

on

Algebraic

and

Logic

Programming

-LRB-

ALP

-RRB-

volume

of

Lecture

Notes

in

Computer

Science

pages

-LCB-

Springer

-LSB-

-RSB-

J.J

Moreno

Navarro

and

M.

Rodr?iguez

Artalejo

BABEL

A

functional

and

logic

language

based

on

constructor

discipline

and

narrowing

In

International

Conference

on

Algebraic

and

Logic

Programming

-LRB-

ALP

-RRB-

volume

of

Lecture

Notes

in

Computer

Science

pages

-LCB-

Springer

-LSB-

-RSB-

J.J

Moreno

Navarro

and

M.

Rodr?iguez

Artalejo

Logic

programming

with

functions

and

predicates

The

language

BABEL

Journal

of

Logic

Programming

-LCB-

-LSB-

-RSB-

S.L.

Peyton-Jones

The

Implementation

of

Functional

Programming

Languages

Prentice

Hall

-LSB-

-RSB-

U.S.

Reddy

Narrowing

as

the

operational

semantics

of

functional

logic

languages

In

International

Symposium

on

Logic

Programming

pages

-LCB-

IEEE

Comp

Soc

Press

-LSB-

-RSB-

U.S.

Reddy

Functional

logic

languages

part

I

In

Workshop

on

Graph

reduction

volume

of

Lecture

Notes

in

Computer

Science

pages

-LCB-

Springer

-LSB-

-RSB-

D.H.D.

Warren

An

abstract

prolog

instruction

set

Technical

Note

SRI

International

Menlo

Park

A

Rules

for

the

algebras

This

appendix

contains

complete

sets

of

rules

for

all

the

refinements

of

the

algebra

A

mark

-LRB-

-RRB-

in

a

rule

indicates

that

it

has

been

changed

with

respect

to

the

previous

algebra

A.

BABEL

tree

algebras

Call

rule

if

is

user

defined

-LRB-

currexp

-RRB-

mode

Apply

currexp

f

-LRB-

en

-RRB-

n

arity

-LRB-

f

-RRB-

then

let

-LSB-

drm

-RSB-

fundef

-LRB-

currexp

pg

-RRB-

extend

NODE

by

nm

with

bfather

-LRB-

ni

-RRB-

currnode

cands

-LSB-

nm

-RSB-

endextend

defrule

-LRB-

ni

-RRB-

dri

mode

Select

Select

rule

if

mode

Select

thenif

cands

-LSB-

-RSB-

then

backtrack

else

let

-LRB-

Lhs

Rhs

-RRB-

defrule

-LRB-

fst

cand

-RRB-

let

mgu

-LRB-

currexp

Lhs

-RRB-

cands

rest

-LRB-

cands

-RRB-

if

then

go

fst

cand

in

Eval

narrow

curr

env

-LRB-

Rhs

-RRB-

vi

vi

with

fst

cand

denoting

the

first

element

in

cands

and

with

abbreviations

go

fst

cand

in

Eval

currnode

fst

cand

mode

Eval

narrow

curr

env

-LRB-

E

-RRB-

exp

-LRB-

fst

cand

-RRB-

exp

-LSB-

pos

E

-RSB-

s

-LRB-

fst

cand

-RRB-

s

pos

-LRB-

fst

cand

-RRB-

pos

backtrack

if

bfather

root

then

stop

else

currnode

bfather

mode

Select

Evaluation

starting

rule

if

mode

Eval

thenif

is

not

in

normal

form

-LRB-

currexp

-RRB-

then

pos

pos

else

mode

Continue

Evaluation

continuation

rule

if

mode

Continue

pos

u

i

thenif

occurs

-LRB-

u

-LRB-

i

-RRB-

currexp

-RRB-

or

nonstrict

-LRB-

u

-RRB-

then

pos

u

mode

Apply

else

pos

u

-LRB-

i

-RRB-

mode

Eval

Stop

rule

if

mode

Continue

pos

ffl

then

solution

list

-LSB-

hexp

s

jGoalvarsi

j

solution

list

-RSB-

if

more

then

backtrack

else

stop

Construction

rule

if

mode

Apply

is

construction

-LRB-

currexp

-RRB-

then

mode

Continue

Partial

application

rule

ifmode

Apply

currexp

-LRB-

e

-RRB-

op

-LRB-

em

-RRB-

then

exp

exp

-LSB-

pos

op

-LRB-

eme

-RRB-

-RSB-

Rules

for

conditionals

if

currexp

b

e

mode

Apply

thenif

b

true

then

exp

exp

-LSB-

pos

e

-RSB-

mode

Eval

else

backtrack

if

currexp

b

mode

Apply

then

if

b

true

then

exp

exp

-LSB-

pos

-RSB-

else

exp

exp

-LSB-

pos

-RSB-

mode

Eval

Equality

rule

if

currexp

eq

-LRB-

-RRB-

mode

Apply

then

let

mgu

mgu

-LRB-

-RRB-

if

mgu

then

exp

exp

-LSB-

pos

false

-RSB-

else

exp

-LRB-

exp

-LSB-

pos

true

-RSB-

-RRB-

mgu

s

s

mgu

A.

Structure

sharing

for

expressions

Call

rule

-LRB-

-RRB-

if

is

user

defined

-LRB-

currexp

-RRB-

mode

Apply

currexp

f

-LRB-

en

-RRB-

n

arity

-LRB-

f

-RRB-

then

let

-LSB-

drm

-RSB-

fundef

-LRB-

currexp

s

pg

-RRB-

extend

NODE

by

nm

with

bfather

-LRB-

ni

-RRB-

currnode

cands

-LSB-

nm

-RSB-

endextend

defrule

-LRB-

ni

-RRB-

dri

mode

Select

Select

rule

-LRB-

-RRB-

if

mode

Select

thenif

cands

-LSB-

-RSB-

then

backtrack

else

let

-LRB-

Lhs

Rhs

-RRB-

defrule

-LRB-

fst

cand

-RRB-

let

mgu

-LRB-

currexp

s

Lhs

-RRB-

cands

rest

-LRB-

cands

-RRB-

if

then

go

fst

cand

in

Eval

narrow

curr

env

-LRB-

Rhs

-RRB-

vi

vi

with

fst

cand

denoting

the

first

element

in

cands

and

with

abbreviations

go

fst

cand

in

Eval

currnode

fst

cand

mode

Eval

narrow

curr

env

-LRB-

E

-RRB-

exp

-LRB-

fst

cand

-RRB-

exp

-LSB-

pos

E

-RSB-

s

-LRB-

fst

cand

-RRB-

s

pos

-LRB-

fst

cand

-RRB-

pos

backtrack

if

bfather

root

then

stop

else

currnode

bfather

mode

Select

Evaluation

starting

rule

if

mode

Eval

thenif

is

not

in

normal

form

-LRB-

currexp

-RRB-

then

pos

pos

else

mode

Continue

Evaluation

continuation

rule

if

mode

Continue

pos

u

i

thenif

occurs

-LRB-

u

-LRB-

i

-RRB-

currexp

-RRB-

or

nonstrict

-LRB-

u

-RRB-

then

pos

u

mode

Apply

else

pos

u

-LRB-

i

-RRB-

mode

Eval

Stop

rule

-LRB-

-RRB-

if

mode

Continue

pos

ffl

then

solution

list

-LSB-

hexp

s

s

jGoalvarsi

j

solution

list

-RSB-

if

more

then

backtrack

else

stop

Construction

rule

if

mode

Apply

is

construction

-LRB-

currexp

-RRB-

then

mode

Continue

Partial

application

rule

-LRB-

-RRB-

ifmode

Apply

currexp

-LRB-

e

-RRB-

s

op

-LRB-

em

-RRB-

then

exp

exp

-LSB-

pos

op

-LRB-

eme

-RRB-

-RSB-

Rules

for

conditionals

-LRB-

-RRB-

if

currexp

b

e

mode

Apply

thenif

b

s

true

then

exp

exp

-LSB-

pos

e

-RSB-

mode

Eval

else

backtrack

if

currexp

b

mode

Apply

then

if

b

s

true

then

exp

exp

-LSB-

pos

-RSB-

else

exp

exp

-LSB-

pos

-RSB-

mode

Eval

Equality

rule

-LRB-

-RRB-

if

currexp

eq

-LRB-

-RRB-

mode

Apply

then

let

mgu

mgu

-LRB-

s

s

-RRB-

if

mgu

then

exp

exp

-LSB-

pos

false

-RSB-

else

exp

-LRB-

exp

-LSB-

pos

true

-RSB-

-RRB-

s

s

mgu

A.

Refining

test

for

normal

form

Call

rule

if

is

user

defined

-LRB-

currexp

-RRB-

mode

Apply

currexp

f

-LRB-

en

-RRB-

n

arity

-LRB-

f

-RRB-

then

let

-LSB-

drm

-RSB-

fundef

-LRB-

currexp

s

pg

-RRB-

extend

NODE

by

nm

with

bfather

-LRB-

ni

-RRB-

currnode

cands

-LSB-

nm

-RSB-

endextend

defrule

-LRB-

ni

-RRB-

dri

mode

Select

Select

rule

if

mode

Select

thenif

cands

-LSB-

-RSB-

then

backtrack

else

let

-LRB-

Lhs

Rhs

-RRB-

defrule

-LRB-

fst

cand

-RRB-

let

mgu

-LRB-

currexp

s

Lhs

-RRB-

cands

rest

-LRB-

cands

-RRB-

if

then

go

fst

cand

in

Eval

narrow

curr

env

-LRB-

Rhs

-RRB-

vi

vi

with

fst

cand

denoting

the

first

element

in

cands

and

with

abbreviations

go

fst

cand

in

Eval

currnode

fst

cand

mode

Eval

narrow

curr

env

-LRB-

E

-RRB-

exp

-LRB-

fst

cand

-RRB-

exp

-LSB-

pos

E

-RSB-

s

-LRB-

fst

cand

-RRB-

s

pos

-LRB-

fst

cand

-RRB-

pos

backtrack

if

bfather

root

then

stop

else

currnode

bfather

mode

Select

Evaluation

starting

rule

-LRB-

-RRB-

if

mode

Eval

thenif

atomic

-LRB-

currexp

-RRB-

then

pos

pos

else

mode

Continue

Evaluation

continuation

rule

if

mode

Continue

pos

u

i

thenif

occurs

-LRB-

u

-LRB-

i

-RRB-

currexp

-RRB-

or

nonstrict

-LRB-

u

-RRB-

then

pos

u

mode

Apply

else

pos

u

-LRB-

i

-RRB-

mode

Eval

Stop

rule

if

mode

Continue

pos

ffl

then

solution

list

-LSB-

hexp

s

s

jGoalvarsi

j

solution

list

-RSB-

if

more

then

backtrack

else

stop

Construction

rule

if

mode

Apply

is

construction

-LRB-

currexp

-RRB-

then

mode

Continue

Partial

application

rule

ifmode

Apply

currexp

-LRB-

e

-RRB-

s

op

-LRB-

em

-RRB-

then

exp

exp

-LSB-

pos

op

-LRB-

eme

-RRB-

-RSB-

Rules

for

conditionals

if

currexp

b

e

mode

Apply

thenif

b

s

true

then

exp

exp

-LSB-

pos

e

-RSB-

mode

Eval

else

backtrack

if

currexp

b

mode

Apply

then

if

b

s

true

then

exp

exp

-LSB-

pos

-RSB-

else

exp

exp

-LSB-

pos

-RSB-

mode

Eval

Equality

rule

if

currexp

eq

-LRB-

-RRB-

mode

Apply

then

let

mgu

mgu

-LRB-

s

s

-RRB-

if

mgu

then

exp

exp

-LSB-

pos

false

-RSB-

else

exp

-LRB-

exp

-LSB-

pos

true

-RSB-

-RRB-

s

s

mgu

A.

BABEL

stack

algebras

Call

rule

-LRB-

-RRB-

if

is

user

defined

-LRB-

currexp

-RRB-

mode

Apply

currexp

f

-LRB-

en

-RRB-

n

arity

-LRB-

f

-RRB-

then

extend

NODE

by

N

with

bfather

-LRB-

N

-RRB-

currnode

currnode

N

cands

-LRB-

N

-RRB-

fundef

-LRB-

currexp

pg

-RRB-

endextend

mode

Select

Select

rule

-LRB-

-RRB-

if

mode

Select

thenif

cands

-LSB-

-RSB-

then

backtrack

else

let

-LRB-

Lhs

Rhs

-RRB-

fst

cands

let

mgu

-LRB-

exp

-LRB-

bfather

-RRB-

-LSB-

pos

-LRB-

bfather

-RRB-

-RSB-

s

-LRB-

bfather

-RRB-

Lhs

-RRB-

cands

rest

-LRB-

cands

-RRB-

if

then

mode

Eval

narrow

bfather

env

-LRB-

Rhs

-RRB-

vi

vi

with

fst

cand

denoting

the

first

element

in

cands

and

with

abbreviations

narrow

bfather

env

-LRB-

E

-RRB-

exp

exp

-LRB-

bfather

-RRB-

-LSB-

pos

-LRB-

bfather

-RRB-

E

-RSB-

pos

pos

-LRB-

bfather

-RRB-

s

s

-LRB-

bfather

-RRB-

backtrack

if

bfather

root

then

stop

else

currnode

bfather

mode

Select

Since

this

point

cands

is

assumed

to

be

initialized

to

-LSB-

-RSB-

for

the

child

of

root

Evaluation

starting

rule

if

mode

Eval

thenif

atomic

-LRB-

currexp

-RRB-

then

pos

pos

else

mode

Continue

Evaluation

continuation

rule

if

mode

Continue

pos

u

i

thenif

occurs

-LRB-

u

-LRB-

i

-RRB-

currexp

-RRB-

or

nonstrict

-LRB-

u

-RRB-

then

pos

u

mode

Apply

else

pos

u

-LRB-

i

-RRB-

mode

Eval

Stop

rule

if

mode

Continue

pos

ffl

then

solution

list

-LSB-

hexp

s

s

jGoalvarsi

j

solution

list

-RSB-

if

more

then

backtrack

else

stop

Construction

rule

if

mode

Apply

is

construction

-LRB-

currexp

-RRB-

then

mode

Continue

Partial

application

rule

ifmode

Apply

currexp

-LRB-

e

-RRB-

s

op

-LRB-

em

-RRB-

then

exp

exp

-LSB-

pos

op

-LRB-

eme

-RRB-

-RSB-

Rules

for

conditionals

if

currexp

b

e

mode

Apply

thenif

b

s

true

then

exp

exp

-LSB-

pos

e

-RSB-

mode

Eval

else

backtrack

if

currexp

b

mode

Apply

then

if

b

s

true

then

exp

exp

-LSB-

pos

-RSB-

else

exp

exp

-LSB-

pos

-RSB-

mode

Eval

Equality

rule

if

currexp

eq

-LRB-

-RRB-

mode

Apply

then

let

mgu

mgu

-LRB-

s

s

-RRB-

if

mgu

then

exp

exp

-LSB-

pos

false

-RSB-

else

exp

-LRB-

exp

-LSB-

pos

true

-RSB-

-RRB-

s

s

mgu

A.

BABEL

graph

algebras

Call

rule

-LRB-

-RRB-

if

is

user

defined

-LRB-

currexp

-RRB-

mode

Apply

currexp

f

-LRB-

en

-RRB-

n

arity

-LRB-

f

-RRB-

then

extend

NODE

by

N

with

currnode

N

cands

-LRB-

N

-RRB-

fundef

-LRB-

currexp

pg

-RRB-

bfather

-LRB-

N

-RRB-

lastnode

act

node

-LRB-

N

-RRB-

currnode

store

act

env

at

N

endextend

mode

Select

with

the

abbreviation

store

act

env

at

N

act

exp

-LRB-

N

-RRB-

exp

act

pos

-LRB-

N

-RRB-

pos

act

s

-LRB-

N

-RRB-

s

Select

rule

-LRB-

-RRB-

if

mode

Select

thenif

cands

-LSB-

-RSB-

then

backtrack

else

let

-LRB-

Lhs

Rhs

-RRB-

fst

cands

let

mgu

-LRB-

act

exp

-LSB-

act

pos

-RSB-

act

s

Lhs

-RRB-

cands

rest

-LRB-

cands

-RRB-

if

then

mode

Eval

lastnode

currnode

narrow

act

env

-LRB-

Rhs

-RRB-

vi

vi

with

fst

cand

denoting

the

first

element

in

cands

and

with

abbreviations

narrow

act

env

-LRB-

E

-RRB-

exp

act

exp

-LSB-

act

pos

E

-RSB-

pos

act

pos

s

act

s

backtrack

if

bfather

root

then

stop

else

currnode

bfather

mode

Select

Evaluation

starting

rule

if

mode

Eval

thenif

atomic

-LRB-

currexp

-RRB-

then

pos

pos

else

mode

Continue

Evaluation

continuation

rule

-LRB-

-RRB-

if

mode

Continue

thenif

pos

act

pos

then

mode

Return

else

let

u

i

pos

if

occurs

-LRB-

u

-LRB-

i

-RRB-

currexp

-RRB-

or

nonstrict

-LRB-

u

-RRB-

then

pos

u

mode

Apply

else

pos

u

-LRB-

i

-RRB-

mode

Eval

We

assume

here

that

act

pos

is

initialized

to

be

ffl

for

the

child

of

root

Stop

rule

-LRB-

-RRB-

if

mode

Return

bfather

root

then

solution

list

-LSB-

hexp

s

s

jGoalvarsi

j

solution

list

-RSB-

if

more

then

backtrack

else

stop

Construction

rule

if

mode

Apply

is

construction

-LRB-

currexp

-RRB-

then

mode

Continue

Return

rule

-LRB-

-RRB-

if

mode

Return

bfather

root

then

currnode

act

node

return

curr

env

to

act

env

mode

Continue

with

the

abbreviation

return

curr

env

to

act

env

exp

-LRB-

act

node

-RRB-

exp

pos

-LRB-

act

node

-RRB-

pos

s

-LRB-

act

node

-RRB-

s

Partial

application

rule

ifmode

Apply

currexp

-LRB-

e

-RRB-

s

op

-LRB-

em

-RRB-

then

exp

exp

-LSB-

pos

op

-LRB-

eme

-RRB-

-RSB-

Rules

for

conditionals

if

currexp

b

e

mode

Apply

thenif

b

s

true

then

exp

exp

-LSB-

pos

e

-RSB-

mode

Eval

else

backtrack

if

currexp

b

mode

Apply

then

if

b

s

true

then

exp

exp

-LSB-

pos

-RSB-

else

exp

exp

-LSB-

pos

-RSB-

mode

Eval

Equality

rule

if

currexp

eq

-LRB-

-RRB-

mode

Apply

then

let

mgu

mgu

-LRB-

s

s

-RRB-

if

mgu

then

exp

exp

-LSB-

pos

false

-RSB-

else

exp

-LRB-

exp

-LSB-

pos

true

-RSB-

-RRB-

s

s

mgu

A.

Subcomputations

with

local

expressions

Call

rule

if

is

user

defined

-LRB-

currexp

-RRB-

mode

Apply

currexp

f

-LRB-

en

-RRB-

n

arity

-LRB-

f

-RRB-

then

extend

NODE

by

N

with

currnode

N

cands

-LRB-

N

-RRB-

fundef

-LRB-

currexp

pg

-RRB-

bfather

-LRB-

N

-RRB-

lastnode

act

node

-LRB-

N

-RRB-

currnode

store

act

env

at

N

endextend

mode

Select

with

the

abbreviation

store

act

env

at

N

act

exp

-LRB-

N

-RRB-

exp

act

pos

-LRB-

N

-RRB-

pos

act

s

-LRB-

N

-RRB-

s

Select

rule

-LRB-

-RRB-

if

mode

Select

thenif

cands

-LSB-

-RSB-

then

backtrack

else

let

-LRB-

Lhs

Rhs

-RRB-

fst

cands

let

mgu

-LRB-

act

exp

-LSB-

act

pos

-RSB-

act

s

Lhs

-RRB-

cands

rest

-LRB-

cands

-RRB-

if

then

mode

Eval

lastnode

currnode

narrow

act

env

-LRB-

Rhs

-RRB-

vi

vi

with

fst

cand

denoting

the

first

element

in

cands

and

with

abbreviations

narrow

act

env

-LRB-

E

-RRB-

exp

E

pos

ffl

s

act

s

backtrack

if

bfather

root

then

stop

else

currnode

bfather

mode

Select

Evaluation

starting

rule

if

mode

Eval

thenif

atomic

-LRB-

currexp

-RRB-

then

pos

pos

else

mode

Continue

Evaluation

continuation

rule

-LRB-

-RRB-

if

mode

Continue

thenif

pos

ffl

then

mode

Return

else

let

u

i

pos

if

occurs

-LRB-

u

-LRB-

i

-RRB-

currexp

-RRB-

or

nonstrict

-LRB-

u

-RRB-

then

pos

u

mode

Apply

else

pos

u

-LRB-

i

-RRB-

mode

Eval

Stop

rule

if

mode

Return

bfather

root

then

solution

list

-LSB-

hexp

s

s

jGoalvarsi

j

solution

list

-RSB-

if

more

then

backtrack

else

stop

Construction

rule

if

mode

Apply

is

construction

-LRB-

currexp

-RRB-

then

mode

Continue

Return

rule

-LRB-

-RRB-

if

mode

Return

bfather

root

then

currnode

act

node

return

curr

env

to

act

env

mode

Continue

with

the

abbreviation

return

curr

env

to

act

env

exp

-LRB-

act

node

-RRB-

act

exp

-LSB-

act

pos

exp

-RSB-

pos

-LRB-

act

node

-RRB-

act

pos

s

-LRB-

act

node

-RRB-

s

Partial

application

rule

ifmode

Apply

currexp

-LRB-

e

-RRB-

s

op

-LRB-

em

-RRB-

then

exp

exp

-LSB-

pos

op

-LRB-

eme

-RRB-

-RSB-

Rules

for

conditionals

if

currexp

b

e

mode

Apply

thenif

b

s

true

then

exp

exp

-LSB-

pos

e

-RSB-

mode

Eval

else

backtrack

if

currexp

b

mode

Apply

then

if

b

s

true

then

exp

exp

-LSB-

pos

-RSB-

else

exp

exp

-LSB-

pos

-RSB-

mode

Eval

Equality

rule

if

currexp

eq

-LRB-

-RRB-

mode

Apply

then

let

mgu

mgu

-LRB-

s

s

-RRB-

if

mgu

then

exp

exp

-LSB-

pos

false

-RSB-

else

exp

-LRB-

exp

-LSB-

pos

true

-RSB-

-RRB-

s

s

mgu

A.

Optimizations

Optimized

Call

rule

-LRB-

-RRB-

if

is

user

defined

-LRB-

currexp

-RRB-

mode

Apply

currexp

f

-LRB-

en

-RRB-

n

arity

-LRB-

f

-RRB-

thenif

lastnode

currnode

cands

-LSB-

-RSB-

pos

ffl

then

cands

-LRB-

t

-RRB-

fundef

-LRB-

currexp

pg

-RRB-

The

node

is

reused

else

extend

NODE

by

N

with

currnode

N

cands

-LRB-

N

-RRB-

fundef

-LRB-

currexp

pg

-RRB-

bfather

-LRB-

N

-RRB-

lastnode

act

node

-LRB-

N

-RRB-

currnode

store

act

env

at

N

endextend

mode

Select

with

the

abbreviation

store

act

env

at

N

act

exp

-LRB-

N

-RRB-

exp

act

pos

-LRB-

N

-RRB-

pos

act

s

-LRB-

N

-RRB-

s

Select

rule

if

mode

Select

thenif

cands

-LSB-

-RSB-

then

backtrack

else

let

-LRB-

Lhs

Rhs

-RRB-

fst

cands

let

mgu

-LRB-

act

exp

-LSB-

act

pos

-RSB-

act

s

Lhs

-RRB-

cands

rest

-LRB-

cands

-RRB-

if

then

mode

Eval

lastnode

currnode

narrow

act

env

-LRB-

Rhs

-RRB-

vi

vi

with

fst

cand

denoting

the

first

element

in

cands

and

with

abbreviations

narrow

act

env

-LRB-

E

-RRB-

exp

E

pos

ffl

s

act

s

backtrack

if

bfather

root

then

stop

else

currnode

bfather

mode

Select

Evaluation

starting

rule

if

mode

Eval

thenif

atomic

-LRB-

currexp

-RRB-

then

pos

pos

else

mode

Continue

Evaluation

continuation

rule

if

mode

Continue

thenif

pos

ffl

then

mode

Return

else

let

u

i

pos

if

occurs

-LRB-

u

-LRB-

i

-RRB-

currexp

-RRB-

or

nonstrict

-LRB-

u

-RRB-

then

pos

u

mode

Apply

else

pos

u

-LRB-

i

-RRB-

mode

Eval

Stop

rule

if

mode

Return

bfather

root

then

solution

list

-LSB-

hexp

s

s

jGoalvarsi

j

solution

list

-RSB-

if

more

then

backtrack

else

stop

Construction

rule

if

mode

Apply

is

construction

-LRB-

currexp

-RRB-

then

mode

Continue

Optimized

Return

rule

-LRB-

-RRB-

if

mode

Return

bfather

root

then

currnode

act

node

return

curr

env

to

act

env

mode

Continue

if

lastnode

currnode

cands

-LSB-

-RSB-

then

lastnode

bfather

with

the

abbreviation

return

curr

env

to

act

env

if

pos

-LRB-

act

node

-RRB-

act

pos

then

exp

-LRB-

act

node

-RRB-

exp

-LRB-

act

node

-RRB-

-LSB-

pos

-LRB-

act

node

-RRB-

exp

-RSB-

else

exp

-LRB-

act

node

-RRB-

act

exp

-LSB-

act

pos

exp

-RSB-

pos

-LRB-

act

node

-RRB-

act

pos

s

-LRB-

act

node

-RRB-

s

Partial

application

rule

ifmode

Apply

currexp

-LRB-

e

-RRB-

s

op

-LRB-

em

-RRB-

then

exp

exp

-LSB-

pos

op

-LRB-

eme

-RRB-

-RSB-

Rules

for

conditionals

if

currexp

b

e

mode

Apply

thenif

b

s

true

then

exp

exp

-LSB-

pos

e

-RSB-

mode

Eval

else

backtrack

if

currexp

b

mode

Apply

then

if

b

s

true

then

exp

exp

-LSB-

pos

-RSB-

else

exp

exp

-LSB-

pos

-RSB-

mode

Eval

Equality

rule

if

currexp

eq

-LRB-

-RRB-

mode

Apply

then

let

mgu

mgu

-LRB-

s

s

-RRB-

if

mgu

then

exp

exp

-LSB-

pos

false

-RSB-

else

exp

-LRB-

exp

-LSB-

pos

true

-RSB-

-RRB-

s

s

mgu

evolving algebras
functional logic languages
mathematical specification