Rewrite Rules Automated Reasoning Recall: Equation used from lhs to - - PowerPoint PPT Presentation

rewrite rules automated reasoning
SMART_READER_LITE
LIVE PREVIEW

Rewrite Rules Automated Reasoning Recall: Equation used from lhs to - - PowerPoint PPT Presentation


slide-1
SLIDE 1

Rewrite Rules

Recall: a

  • b

e

  • a
  • b

b

  • b
  • a
  • b

b

  • e
  • b
  • a
  • b

b

  • a
  • a
  • b
  • a
  • b

b

  • a
  • e

b

  • a

a

e

a

For example, for first rewrite, use:

Importance of rewrite rules:

Rewrite terms on either side of = to be identical

Each step is justified by a rewrite rule like an equation but used only in one direction: makes use of matching

computationally efficient: little search

common in mathematics

can deal with inequalities as well ( )

can also deal with propositional equivalence e.g.

Terminology: A term with no variables is said to be ground a

e

a

Equation used from lhs to rhs

Algebraic Simplification

Sample set of rewrite rules lhs

rhs

✠ ✠

Always used in this direction Example: a

2

✡ ☛

5

b

☛ ✌

a

5

b

Rule 1

1

5

b

Rule 3

5

b

Rule 2

5

Rule 1

5

Redexes: match with lhs of a rule and replace with corresponding instance of rhs

There is some choice:

which subexpression to rewrite

which rule to use Terminology: A redex is a subexpression that can be rewritten

  • 1. X
✍ ✎
  • 2. 1

X

X

  • 3. X

1

  • 4. X
✏ ✎

X

There is a search tree

in this example, it is AND choice: so does not matter

not always AND choice

Multiplication

Automated Reasoning

Rewrite Rules Jacques Fleuriot

Lecture XII

Overview

Motivation for rewriting

Definition of rewrite rule of inference

We will look at examples in several familiar domains

e.g. Propositional logic and Peano Arithmetic

Termination

Church-Rosser and canonical properties

Strategies for applying rewrite rules

Methods for proving Church-Rosser and canonical properties

notions of critical pairs and confluence Of interest when dealing with sets of rewrite rules:

slide-2
SLIDE 2

To Rewrite

  • find a subexpression t
  • find a rule whose LHS matches t with substitution
  • replace subexpression t with

RHS

✂ ✄

Refer to example again:

expression

s a

s 0

s b s X

Y

s X

Y s a

s 0

s b

Subexpression t

where s X

Y

a

X , s 0

Y

s a

s 0 RHS

a

X , s 0

Y

s X

Y

a

X , s 0

Y

s a

s 0

Partial Rewrite Search Tree

a

2

✡ ☛

5

b

a

5

b

a

2

✎ ✏

5

a

2

✒ ✓

5 a

5

a

5

1

5

b

What evaluation strategy should be used?

innermost (inside-out)/leftmost redex

  • utermost (outside-in)/leftmost redex

s 0

s 0

s 0

X

e.g. apply to

s 0

s 0

s 0 X

s Y

s X

Y

e.g. apply to Questions:

is the tree finite i.e. will the rewriting terminate?

are all the leaf nodes the same i.e. does order of rewrites matter

Numeric Calculations

From Lecture 3: Peano axioms can be viewed as rewrite rules Example axiom: s X

Y

s X

Y Set of rewrite rules (addition and multiplication):

Cases for 0 and successor

Calculations:

X

X s X

Y

s X

Y

X

s X

Y

X

Y

Y s s 0

s 0

s 0

s 0

s 0

✪ ✩

s 0

s 0

s 0

✬ ✪

s s 0 For ground expressions that only contain the constant 0, rewriting evaluates the expression to a value

In general, we may not get a value for ground expressions using

  • rewriting. e.g.,

s a

s 0

✮ ✯ ✮

s s a This is known as symbolic evaluation: still contains variables

Rewrite Rule of Inference

Note: special case of paramodulation: rewrite rule

  • f inference uses matching not unification

exp[sub] lhs

rhs exp[ rhs

✱ ✲

] where lhs

✱ ✲ ✳

sub A rewrite rule must satisfy the following restrictions:

✴ ✵ ✶ ✷

is not a variable

is not allowed – matches anything and so hard to control

X

X

1

e.g.

vars

✻ ✼

vars

is not allowed - if we start rewriting a ground term, we will always have a ground term

✾ ✿

X

e.g.

expression

s a

s 0

s b s X

Y

s X

Y s a

s 0

s b

subexpression

where s X

Y

a

X ,s 0

Y

s a

s 0

Example:

target pattern

slide-3
SLIDE 3

Important Properties II

Normal Form: No more rewrite rules can be applied Canonical: all normal forms are identical Definitions: t t t t t t t t t s leaves of tree: normal forms

  • Therefore, t is the canonical form of s
  • Church Rosser implies canonical
  • It is not always possible to ensure set of rewrite rules is canonical

No need to search since result same in the end for all choices

  • f rewrites

Confluence

Confluence * * int1 int2 comm * * exp

zero or more rewrite steps Expression exp rewrites in two different ways, and in zero or more steps, to intermediate expressions int1 and int2 which can both be rewritten to some common rewriting comm

Local Confluence * * int1 int2 comm exp

  • ne rewrite step

immediate rewriting Choice of one step rewrites for exp. Questions:

  • when is there a choice?
  • does that choice matter?

Theorem: if terminating and locally confluent then confluent

Proof needs notion of critical pairs

Termination

Nice property that we want: application of rules cannot go on forever

  • 1. X
✁ ✂
  • 2. 1

X

X

  • 3. X

1

  • 4. X
✄ ✂

X algebraic rules To show termination, we need some sort of measure of expression that strictly decreases each time rule is applied non-negative: so cannot decrease forever Example: depth of expression tree decreases for all instances

Let DEPTH be the measure function then DEPTH X

☎ ✆

DEPTH 0 DEPTH 1

X

DEPTH X DEPTH X 0

DEPTH 1 DEPTH X

✝ ✆

DEPTH X

therefore, rules terminate

Typically, we look for a termination argument

There are problem cases:

X

Y

Y

X X

Y

Z

X

Y

X

Z X

Y

X

Z

X

Y

Z two rules together: non termination

Important Properties I

Church-Rosser: A set of rewrite rules is Church-Rosser when equal expressions have a common rewriting

✌ ✍

Notation: means zero or more rewrite steps reflexive transitive closure of

a

✏ ✑

b b

✏ ✑

c a

✏ ✑

c

a

✒ ✓

a

  • ne step rewriting

Diagram: * * * * * s t * * s t u Formally: if t

i1

i2

✔✖✕ ✔

in

s

then

t

✗ ✘

u

✘✚✙

s

for some u Church-Rosser if then

slide-4
SLIDE 4

Critical Pairs

*

where

exp"[ sub1[ sub2]"] exp"[ rhs1

] exp"[ sub1[ rhs2

✂ ✄

]"] lhs1

rhs1 lhs2

rhs2 subi

Each is instantiation of :

lhsi lhs1

✆ ✝ ✞

sub1 lhs2

sub2 and x [y]: y is a subexpression of x part (bit) of must match

sub2 lhs1 lhs1[bit ]

✡ ☛ ☞

sub1[ sub2]

so, making things explicit:

bit

✌ ✍ ✎

sub2 sub2

lhs2

✆ ✏

and

'

bit

✑ ✒ ✓

lhs2

✑ ✒

i.e. subexpression of rule1 unifies with lhs of rule2

bit and must be unifiable with mgu

lhs2

Critical Pairs (ii)

lhs1

✕ ✖ ✗

lhs1[bit ]

✕ ✖

lhs1

✘ ✙

rhs1

✕ ✖

lhs1[rhs2]

✚ ✛

lhs1

rhs1 lhs2

rhs2

comm

rhs1

✢ ✣

, lhs1[rhs2]

✢ ✣

Critical pair:

Example: Critical Pairs

How can choices arise in rewriting?

Two separate subexpressions are both a redex Need to rewrite one AND the other: Order does not matter

One subexpression can be rewritten in two different ways One redex is a subexpression of the other redex: Must rewrite

  • ne redex OR the other: Choice may matter

rewrite rules:

  • 1. X

1

  • 2. 0

Y

1

1 2

1,0

  • 1. W

e

W 2. X

Y

Z

X

Y

Z

X

e

Z X

e

Z X

Z

1 2

X

Z ,X

e

Z Question: Can we conflate all the possible rewritings? Examples:

critical pairs

Testing for Church-Rosser

A set of rewrite rules is Church-Rosser (C-R) iff it is confluent

A set of terminating rules is confluent iff it is locally confluent

Local confluence is decidable: due to Knuth and Bendix

Need notion of critical pair

slide-5
SLIDE 5

Knuth-Bendix Completion

Set of rewrite rules R, known to be terminating

  • 1. let i
  • 0 and R1
  • R
  • 2. increment i by 1
  • 3. find all critical pairs (c.p.s) of all pairs of rules in Ri
  • 4. for all c.p.s.

a ,b ,

find a normal form a' of a

find a normal form b' of b

  • 5. if a '

b' , extend the set Ri to Ri

1 follows:

if Ri

a'

b' is terminating, let Ri

1

  • Ri

a'

b' and goto step 2 if Ri

b'

a' is terminating, let Ri

1

  • Ri

b'

a ' and goto step 2 if neither is terminating then exit with FAIL

  • 6. let R
  • Ri

IF the above procedure terminates without failure THEN R

is confluent

Summary

Rewrite rules avoid search if

  • 1. terminating
  • 2. confluent (locally confluent and terminating)

Prove 1. using ingenuity

Prove 2. using critical pairs

Use Knuth-Bendix procedure to try to make a confluent set from a non-confluent set. Does not always work.

Read Bundy Chapter 9.

Testing for Local Confluence

Set of rewrite rules R, known to be terminating

Find all critical pairs of all pairs of rules

For each critical pair <a, b>, check that it is joinable (or conflatable)

find canonical form a' of a

find canonical form b' of b

ensure that a' b';

FAIL if they are different

Algorithm is a decision procedure

Establishing Local Confluence

Sometimes non-locally confluent (i.e. test fails) X

e

X f

X

X

f ,e

not locally confluent since does not conflate However, we can add a new rule, to make this critical pair joinable f

e CARE: Adding the new rule

must preserve termination

must give rise to new critical pairs need to start over again and check local confluence Algorithm due to Knuth and Bendix: Completion