Equational Reasoning

263-2200
Types and Programming
Languages
1 / 64
Outline
Equational Reasoning
Overview
Matching
Motivation
Term Algebras
Matching in the Context of a Term Language
A Matching Algorithm
A Formal Definition of Equational Reasoning
Definition
Example
Unification
Motivation
A Unification Algorithm
2 / 64
Outline
Equational Reasoning
Overview
Matching
Motivation
Term Algebras
Matching in the Context of a Term Language
A Matching Algorithm
A Formal Definition of Equational Reasoning
Definition
Example
Unification
Motivation
A Unification Algorithm
3 / 64
Intuition
Equational reasoning is the method of manipulating structures
such as formulas and expressions. The basic idea is that
equals can be replaced by equals in any context.
4 / 64
Example
Suppose you are given the following axiom:
Axiom (A1)
x ∗ (y + z) = x ∗ y + x ∗ z
The axiom A1 states, in general terms, that multiplication
(denoted by the ∗ symbol) can be distributed over addition.
5 / 64
Axiom A2 also describes a distributive property of multiplication
over addition. However, it is important to note that A2 is not as
general as A1.
Axiom (A2)
2 ∗ (y + z) = 2 ∗ y + 2 ∗ z
6 / 64
Suppose you are asked if axiom A1 “can be applied” to the
following expression?
5 ∗ (6 + 7)
(1)
The answer to this question is “yes, A1 can be applied and will
produce the result 5 ∗ 6 + 5 ∗ 7”. We will capture this notion of
“applies” more formally and write:
→
A1
5 ∗ (6 + 7) −→ 5 ∗ 6 + 5 ∗ 7
Can A2 be applied to expression (1)?
7 / 64
Can A1 be applied to the following expression?
5∗6+5∗7
(2)
The answer to this question is “yes, A1 can be applied and will
produce the result 5 ∗ (6 + 7)”. We will capture this notion of
“applies” more formally and write:
←
A1
5 ∗ 6 + 5 ∗ 7 −→ 5 ∗ (6 + 7)
8 / 64
Goal
Equational reasoning is a formalization of the ideas
surrounding the application of equations (e.g., axioms) to terms
(e.g., expressions).
9 / 64
The Process of Equational Reasoning
We now revisit the application of A1 to 5 ∗ (6 + 7) and try to
precisely explain the process behind
→
A1
5 ∗ (6 + 7) −→ 5 ∗ 6 + 5 ∗ 7
10 / 64
The Process of Equational Reasoning
1. The axiom A1 makes use of the variables x, y , and z to
state in general terms that multiplication can be distributed
over addition.
2. The fact that x ∗ (y + z) = x ∗ y + x ∗ z is an equation
implies that 5 ∗ (6 + 7) = 5 ∗ 6 + 5 ∗ 7 is also an equation.
3. The expression on the left-hand side of
5 ∗ (6 + 7) = 5 ∗ 6 + 5 ∗ 7 is syntactically identical to the
expression we are interested in manipulating.
4. The law of equational reasoning states that equals may be
replaced by equals. Thus 5 ∗ (6 + 7) may be rewritten to
5 ∗ 6 + 5 ∗ 7.
11 / 64
A Small but Important Technical Detail
For this purposes of this set of slides we will explicitly distinguish
between semantic equality and syntactic equality . For the rest
of these slides we will use the symbol ≈ to denote semantic
equality and the symbol = to denote syntactic equality.
12 / 64
Example
Semantic Equality
1≈1
x ≈1
2+3≈2+3
1+1≈2
1+1≈3−1
true
?
true
Syntactic Equality
1=1
x =1
2+3=2+3
true
false
true
true
true
1+1=2
1+1=3−1
false
false
13 / 64
Using the New Notational Conventions
Axiom (A1)
x ∗ (y + z) ≈ x ∗ y + x ∗ z
Axiom (A2)
2 ∗ (y + z) ≈ 2 ∗ y + 2 ∗ z
14 / 64
Generalizing the Notion of Equational Reasoning
The general problem that equational reasoning is interested in
can be broadly stated as follows:
Given a set of axioms E called an equational theory , and a term
t, use E to rewrite t to produce another term t 0 .
15 / 64
Example
Given E = {A0, A1} where
Axiom A0: x ∗ y ≈ y ∗ x
Axiom A1: x ∗ (y + z) ≈ x ∗ y + x ∗ z
use only equational reasoning to show that (6 + 7) ∗ 5 ≈ 5 ∗ 6 + 5 ∗ 7.
→
A0
1. (6 + 7) ∗ 5 ≈ 5 ∗ (6 + 7)
because (6 + 7) ∗ 5 −→ 5 ∗ (6 + 7)
2. 5 ∗ (6 + 7) ≈ 5 ∗ 6 + 5 ∗ 7
because 5 ∗ (6 + 7) −→ 5 ∗ 6 + 5 ∗ 7
3. (6 + 7) ∗ 5 ≈ 5 ∗ 6 + 5 ∗ 7
due to (1) and (2) and the
transitivity of ≈.
→
A1
16 / 64
Summary
I
The use of equational reasoning to manipulate expressions
lies at the heart of mathematics. Mathematicians
extensively use equational reasoning to prove theorems.
I
The mechanization of restricted forms of equational
reasoning lies at the heart of computer science. Equational
reasoning provides computer scientists with a foundational
notion of what it means to compute.
17 / 64
Outline
Equational Reasoning
Overview
Matching
Motivation
Term Algebras
Matching in the Context of a Term Language
A Matching Algorithm
A Formal Definition of Equational Reasoning
Definition
Example
Unification
Motivation
A Unification Algorithm
18 / 64
Motivation
I
Answering the question of when an expression is an
instance of the left-hand (or right-hand) side of an equation
is non-trivial.
I
Issues:
1. The scope of a variable extends over an axiom. This means
that all occurrences of a variable in an axiom denote the
same value. It also means that variables occurring in
different axioms are unrelated.
2. The distinction between variables and constants must be
clear.
3. The kinds of values that a variable may denote must be
clearly understood.
19 / 64
Example
Axiom (A1)
x ∗ (y + z) ≈ x ∗ y + x ∗ z
Can axiom A1 be applied to the following expression?
(4 + 5) ∗ (6 + 7)
(3)
20 / 64
Example
Can axiom A1 be applied to the following expression?
5 ∗ (6 + 7 + 8)
(4)
Can axiom A1 be applied to the following expression?
5 ∗ (z + 7)
(5)
21 / 64
Parsing
I
The problems highlighted in the previous examples can all
be resolved using a technique known as parsing. Parsing
enables the components of expressions to be grouped in
such a way that the association between variables and
values is precisely defined. One problem with this
approach is that parsing can be difficult to carry out
manually.
I
We will minimize the computational effort needed to parse
an expression by greatly simplifying the structure of
expressions. In particular, we will restrict our consideration
to expressions that belong to recursively defined
term languages.
22 / 64
Term Algebra: Function Symbols
Definition
I
Let F i denote a set of function symbols whose arity is i.
For example, if f ∈ F i , then f takes i arguments as its input.
I
Functions that take 0 arguments are called nullary
functions and are used to denote constants (since the
value they output remains constant).
I
Define the set F of all function symbols (of all arities) as
follows:
F = F 0 ∪ F 1 ∪ ...
23 / 64
Term Algebra: Variable Symbols
Definition
Let X denote a set of variable symbols.
24 / 64
Term Algebra
Definition
Given a set of function symbols F and a set of variable symbols
X such that F and X are disjoint, the tuple T (F, X ) denotes a
term algebra (also sometimes known as a term-language).
25 / 64
Term Algebra: Ground Terms
I
When X = ∅ we may simplify T (F, X ) and simply write
T (F).
I
A term t ∈ T (F, X ) containing no variables is called a
ground term. A more formal way of saying this is that
t ∈ T (F).
26 / 64
A Deductive Definition
Membership in T (F, X ) can be deduced using the following
rules:
I
Constants
I
Variables
I
General Case
f ∈ Fn
c ∈ F0
c ∈ T (F, X )
x ∈X
x ∈ T (F, X )
t1 ∈ T (F, X )
···
tn ∈ T (F, X )
f (t1 , . . . , tn ) ∈ T (F, X )
27 / 64
Example
Define T (F, X ) such that:
1. F ≈ {plus, times} ∪ Integers
2. X ≈ {w, x, y , z}
Here we assume that the meaning of the functions plus and
times is understood by the reader. We also assume that the set
Integers denotes the set of nullary functions corresponding to
the integers.
28 / 64
Example
Standard Notation
5
(5)
(6 + 7)
5 ∗ (6 + 7 + 8)
5 ∗ (6 + 7 + 8)
Term Language Equivalents
5
5
plus(6, 7)
times(5, plus(6, plus(7, 8)))
times(5, plus(plus(6, 7), 8))
Remark
Note that the order of evaluation is explicit in a term. Thus,
terms are like fully parenthesized expressions.
29 / 64
Given a term language T (F, X ) we will (for now) assume the
following:
I
Equations have the form: lhs ≈ rhs where
lhs, rhs ∈ T (F, X ).
I
The terms we wish to rewrite belong to T (F). (In other
words they are ground terms).
I
The result of rewriting a (ground) term is also a ground
term.
30 / 64
Matching
Given two terms t1 ∈ T (F, X ) and t2 ∈ T (F), the goal of
matching is to determine whether it is possible for the variables
in t1 to be bound in such a way that the resulting instance of t1
is syntactically identical to t2 . That is, it is possible to bind the
variables in t1 to produce a result t10 such that t10 = t2 ?
31 / 64
Example
t1
x
x
t2
5
plus(5, 6)
Matching Binding
x := 5
x := plus(5, 6)
plus(x, 6)
plus(5, x)
plus(5, 6)
plus(5, 6)
x := 5
x := 6
plus(x, 6)
plus(times(4, 5), 6)
x := times(4, 5)
plus(x, y )
plus(times(4, 5), 6)
x := times(4, 5)
y := 6
32 / 64
The substitution σ
I
We will model a matching binding as a list (i.e., a
sequence) of the form
[var1 := t1 ][var2 := t2 ]...[varn := tn ]
and will refer to this list as a substitution.
I
We will use the symbol σ to refer to a substitution when the
details of the substitution are unimportant.
I
It is important to note that, in order to be well-formed, a
substitution may bind a variable at most once.
33 / 64
Example
t1
x
x
t2
5
f (5, 6)
Substitution
σ ≈ [x := 5]
σ ≈ [x := f (5, 6)]
f (x, 6)
f (5, x)
f (5, 6)
f (5, 6)
σ ≈ [x := 5]
σ ≈ [x := 6]
f (x, 6)
f (g(4, 5), 6)
σ ≈ [x := g(4, 5)]
f (x, y )
f (g(4, 5), 6)
σ ≈ [x := g(4, 5)][y := 6]
34 / 64
Observation
When considering matching over a term language T (F, X ) the
domain of a variable is the set of all ground terms T (F).
35 / 64
Applying σ to a Term
We will now focus on formalizing the application of a
substitution σ to a term t, which we denote σ(t). This
application takes place in the context of a term language
T (F, X ) which is assumed to be known.
Define [var := t] ∈ σ to be a boolean-valued expression that
evaluates to true if the binding [var := t] is an element of σ
and false otherwise.
36 / 64
A Formal Definition of σ(t)
Definition
σ(f (t1 , t2 , ..., tn ))
≈
f (σ(t1 ), σ(t2 ), ..., σ(tn ))
σ(var )
σ(var )
≈
≈
σ(t)
var
if ∃t : [var := t] ∈ σ
if 6 ∃t : [var := t] ∈ σ
37 / 64
Example
Define T (F, X ) such that:
1. F ≈ {f , g} ∪ Integers where f and g are binary functions.
2. X ≈ {w, x, y , z}
Let σ ≈ [x := 1][y := f (2, 3)][z := g(f (4, 4), 5)]
Term
f (x, x)
f (x, y )
w
f (z, w)
σ(Term)
f (1, 1)
f (1, f (2, 3))
w
f (g(f (4, 4), 5), w)
38 / 64
The Matching Function We define as a matching operation on terms that returns a
substitution σ when it can, and returns the value fail otherwise.
tvars tground =
σ
∃σ : σ(tvars ) = tground
fail 6 ∃σ : σ(tvars ) = tground
We call an expression of the form t1 t2 a match expression.
39 / 64
Example
Define T (F, X ) such that:
1. F ≈ {f , g} ∪ Integers where f and g are binary functions.
2. X ≈ {w, x, y , z}
Match Expression
f (x, x) f (5, 5)
f (x, 6) f (5, 5)
f (x, x) f (5, 6)
f (x, x) f (g(4, 5), g(4, 5))
f (x, y ) f (g(4, 5), 6)
Result
σ ≈ [x := 5]
false
false
σ ≈ [x := g(4, 5)]
σ ≈ [x := g(4, 5)][y := 6]
40 / 64
We consider a matching problem to involve a set of match
expressions:
S ≈ {t1 s1 , t2 s2 , ..., tn sn }
Important: We assume that the scope of a variable extends
over the entire set S.
41 / 64
A Matching Algorithm
Let x and ti denote a variable and an arbitrary ground term
respectively. Let si denote an arbitrary term.
match({x t} ∪ S, bindings)
≈
match(σ0 (S), bindings[x := t])
where σ0 ≈ [x := t]
match({t t} ∪ S, σ)
≈
match(S, σ)
match({}, σ)
≈
σ
match({f (s1 , ..., sn ) f (t1 , ..., tn )} ∪ S, σ)
≈ match({s1 t1 } ∪ ...{sn tn } ∪ S, σ)
match({f (s1 , ..., sn ) g(t1 , , tk )} ∪ S, σ) ≈ fail
42 / 64
Remark
Note: The last case of the matching algorithm also applies if
one is trying to match a function with a constant, or a constant
with a different constant (recall that constants are viewed as
nullary functions).
43 / 64
Outline
Equational Reasoning
Overview
Matching
Motivation
Term Algebras
Matching in the Context of a Term Language
A Matching Algorithm
A Formal Definition of Equational Reasoning
Definition
Example
Unification
Motivation
A Unification Algorithm
44 / 64
A Match-Based Equational Reasoning Step
Given an equation A : e1 ≈ e2 and a ground term t.
→
1. Try A (i.e., e1 → e2 )
1.1 If match({e1 t}, []) ≈ fail then no match-based
equational reasoning step can be performed using e1 .
→
A
σ(e2 )
1.2 If match({e1 t}, []) ≈ σ then t −→
←
2. Try A (i.e., e2 → e1 )
2.1 If match({e2 t}, []) ≈ fail then no match-based
equational reasoning step can be performed using e2 .
←
A
2.2 If match({e2 t}, []) ≈ σ then t −→
σ(e1 )
45 / 64
Subterms
Remark
I
Up to now, we have only focused on applying a rewriting
step to an entire term t. That is, we have only considered
matching with t and not with any of its subterms.
I
In general, rewriting can also occur at the subterm level.
That is, one can try to match with t or any subterm of t.
46 / 64
Basic Axioms for Equational Reasoning
(s ≈ t) ∈ E
E `s≈t
E `t ≈t
E `t ≈s
E `s≈t
E `s≈t
E ` σ(s) ≈ σ(t)
E `s≈t
E `s≈u
E `t ≈u
E ` s1 ≈ t1 · · · E ` sn ≈ tn
E ` f (s1 , ..., sn ) ≈ f (t1 , ..., tn )
47 / 64
The Axioms of a Boolean Algebra
Commutativity
or (x, y ) ≈ or (y , x)
and(x, y ) ≈ and(y , x)
Distributivity
or (x, and(y , z)) ≈ and(or (x, y ), or (x, z))
and(x, or (y , z)) ≈ or (and(x, y ), and(x, z))
Identity
or (x, false) ≈ x
and(x, true) ≈ x
Complement
or (x, not(x)) ≈ true
and(x, not(x)) ≈ false
48 / 64
Example
Use equational reasoning together with the axioms from boolean
algebra to show that: or (a, a) ≈ a
←−
a
≈
or (a, false)
Identity
≈
or (a, and(a, not(a)))
Compliment
≈
and(or (a, a), or (a, not(a)))
Distributivity
≈
and(or (a, a), true)
Compliment
≈
or (a, a)
Identity
←−
−→
−→
−→
49 / 64
Outline
Equational Reasoning
Overview
Matching
Motivation
Term Algebras
Matching in the Context of a Term Language
A Matching Algorithm
A Formal Definition of Equational Reasoning
Definition
Example
Unification
Motivation
A Unification Algorithm
50 / 64
Motivation
I
So far, we have been very strict about only permitting
variables to occur on the left-hand side of a match
expression.
I
Unification is a generalization of matching where variables
may occur in both terms.
I
Instead of writing t1 t2 we will write t1 ≺ t2 to denote
that a unification should be attempted between t1 and t2 .
We will call an expression of the form t1 ≺ t2 a
unify expression.
51 / 64
Given two terms t1 ∈ T (F, X ) and t2 ∈ T (F, X ), the goal of
unification is to discover a (most general) substitution σ such
that
σ(t1 ) = σ(t2 )
52 / 64
Example
Unification
plus(x1 , 5) ≺ plus(6, y1 )
Result
σ ≈ [x1 := 6][y1 := 5]
plus(x1 , 5) ≺ plus(5, x1 )
σ ≈ [x1 := 5]
plus(5, x1 ) ≺ plus(x1 , y1 )
σ ≈ [x1 := 5][y1 := 5]
plus(x1 , y1 ) ≺ plus(y1 , x1 )
σ ≈ [x1 := y1 ]
plus(x1 , y1 ) ≺ plus(z1 , z1 )
σ ≈ [x1 := z1 ][y1 := z1 ]
plus(x1 , 5) ≺ plus(6, x1 )
fail
53 / 64
The Occurs Check: A Special Case
Unification
y ≺ g(y )
Tentative Result
σ ≈ [y := g(y )]
σ(y ) 6= σ(g(y )
g(y ) 6= g(g(y ))
Recall that we want the terms to be syntactically identical after
the application of σ.
54 / 64
The Occurs Check: A Special Case
Unification
f (x, x) ≺ f (x, f (x, 7))
Tentative Result
σ ≈ [x := f (x, 7)]
σ(f (x, x)) 6= σ(f (x, f (x, 7)))
f (f (x, 7), f (x, 7)) 6= f (f (x, 7), f (f (x, 7), 7))
55 / 64
Observation
When considering matching over a term language T (F, X ) the
domain of a variable is the set of all terms T (F, X ).
56 / 64
Given a term language T (F, X ) we will assume the following:
I
Equations have the form: lhs ≈ rhs where
lhs, rhs ∈ T (F, X ).
I
The terms we wish to rewrite belong to T (F, X ).
I
Rewriting a term may produce a resulting term that
contains variables. That is, the result of rewriting is a term
belonging to T (F, X ).
57 / 64
We consider a unification problem to involve a set of unify
expressions:
S ≈ {t1 ≺ s1 , t2 ≺ s2 , ..., tn ≺ sn }
Important: We assume that the scope of a variable extends
over the entire set S.
58 / 64
Let x denote a variable and t denote a term, then:
Definition
occurs(x, t) ≈ true iff the variable x occurs as a (proper)
sub-term of t.
occurs(x, a)
occurs(x, y )
occurs(x, x)
≈ false
≈ false
≈ false
occurs(x, f (y , b))
occurs(x, f (b, x))
≈ false
≈ true
59 / 64
A Unification Algorithm by A. Martelli and U.
Montanari
unify ({x ≺ t} ∪ S, bindings) ≈
if ¬occurs(x, t) ∧ σ0 ≈ [x := t] then unify (σ0 (S), bindings[x := t])
else fail
unify ({t ≺ x} ∪ S, σ)
≈
unify ({x ≺ t} ∪ S, σ)
unify ({t ≺ t} ∪ S, σ)
≈
unify (S, σ)
unify ({}, σ)
≈
σ
unify ({f (t1 , ..., tn ) ≺ f (s1 , ..., sn )} ∪ S, σ)
≈ unify ({t1 ≺ s1 } ∪ ...{tn ≺ sn } ∪ S, σ)
unify ({f (t1 , ..., tn ) ≺ g(s1 , , sk )} ∪ S, σ) ≈ fail
60 / 64
Remark
Note: The last case also applies if one is trying to unify a
function with a constant, or a constant with a different constant
(recall that constants can be viewed as nullary functions).
61 / 64
Example
unify ({b ≺ g(f (a, b))}, [])
No unification possible. The nullary
function (i.e., constant) symbol b
does not match with the function
symbol g.
unify ({x ≺ g(f (x, b))}, [])
No unification possible. The
variable x occurs as a proper
sub-term in g(f (x, b)).
unify ({b ≺ c}, [])
No unification possible. The
constant b is syntactically
different from the constant c.
62 / 64
Example
Unification Problem
{g(x, f (x, b)) ≺ g(f (a, b), f (y , z))}
Substitution
σ0 ≈ []
{x ≺ f (a, b), f (x, b) ≺ f (y , z)}
σ1 ≈ []
{f (f (a, b), b) ≺ f (y , z)}
σ2 ≈ [x := f (a, b)]
{f (a, b) ≺ y , b ≺ z}
σ3 ≈ [x := f (a, b)]
{y ≺ f (a, b), b ≺ z}
σ4 ≈ [x := f (a, b)]
63 / 64
{b ≺ z}
σ5 ≈ [x := f (a, b)][y := f (a, b)]
{z ≺ b}
σ6 ≈ [x := f (a, b)][y := f (a, b)]
{}
σ7 ≈ [x := f (a, b)][y := f (a, b), z := b]
σanswer ≈ [x := f (a, b)][y := f (a, b)][z := b]
64 / 64