Behavioural Differential Equations and Coinduction for Binary Trees.
-
Citations (0)
- Cited In (1)
-
Article: Coalgebras in functional programming and type theory.
Theor. Comput. Sci. 01/2011; 412:5006-5024.
Page 1
Behavioural Differential Equations and Coinduction for
Binary Trees
Alexandra Silva1?and Jan Rutten1,2
1Centrum voor Wiskunde en Informatica (CWI)
2Vrije Universiteit Amsterdam (VUA)
{ams,janr}@cwi.nl
Abstract. We study the set TA of infinite binary trees with nodes labelled in a semiring
A from a coalgebraic perspective. We present coinductive definition and proof principles
based on the fact that TA carries a final coalgebra structure. By viewing trees as formal
power series, we develop a calculus where definitions are presented as behavioural differential
equations. We present a general format for these equations that guarantees the existence
and uniqueness of solutions. Although technically not very difficult, the resulting framework
has surprisingly nice applications, which is illustrated by various concrete examples.
1Introduction
Infinite data structures are often used to model problems and computing solutions for them.
Therefore, reasoning tools for such structures have become more and more relevant. Coal-
gebraic techniques turned out to be suited for proving and deriving properties of infinite
systems.
In [6], a coinductive calculus of formal power series was developed. In close analogy to
classical analysis, the definitions were presented as behavioural differential equations and
properties were proved in a calculational (and very natural) way. This approach has shown
to be quite effective for reasoning about streams [6,7] and it seems worthwhile to explore its
effectiveness for other data structures as well.
In this paper, we shall take a coalgebraic perspective on a classical data structure – infinite
binary trees, and develop a similar calculus, using the fact that binary trees are a particular
case of formal power series.
The contributions of the present paper are: a coinductive calculus, based on the notion of
derivative, to define and to reason about trees and functions on trees; a set of illustrative
examples and properties that show the usefulness and expressiveness of such calculus; the
formulation of a general format that guarantees the existence and uniqueness of solutions
of behavioural differential equations.
Infinite trees arise in several forms in other areas. Formal tree series (functions from trees
to an arbitrary semiring) have been studied in [3], closely related to distributive Σ-algebras.
The work presented in this paper is completely different since we are concerned with infinite
binary trees and not with formal series over trees. In [5], infinite trees appear as generalisa-
tions of infinite words and an extensive study of tree automata and topological aspects of
trees is made. We have not yet addressed the relation of our work with automata theory.
Here we emphasize coinductive definition and proof principles for defining and reasoning
about (functions on) trees.
At the end of the paper, in Section 6, the novelty of our approach is discussed further. Also
several directions for further applications are mentioned there.
2Trees and coinduction
We introduce the set TA of infinite node-labelled binary trees, show that TA satisfies a
coinduction proof principle and illustrate its usefulness.
?Partially supported by the Funda¸ c˜ ao para a Ciˆ encia e a Tecnologia, Portugal, under grant number
SFRH/BD/27482/2006.
Page 2
The set TA of infinite node-labelled binary trees, where to each node is assigned a value in
A, is the final coalgebra for the functor FX = X × A × X and can be formally defined by:
TA = {t | t : {L,R}∗→ A}
The set TA carries a final coalgebra structure consisting of the following function:
?l,i,r? : TA → TA× A × TA
t ?→ ?λw.t(Lw),t(ε),λw.t(Rw)?
where l and r return the left and right subtrees, respectively, and i gives the label of the
root node of the tree. Here, ε denotes the empty word and, for b ∈ {L,R}, bw denotes the
word resulting from prefixing the word w with the letter b.
These definitions of the set TA and the respective coalgebra map may not seem obvious.
The follow reasoning justifies its correctness:
– It is well known from the literature [4] that the final system for the functor G(X) =
A × XBis (AB∗,π), where
π : AB∗→ A × (AB∗)B
π(φ) = ?φ(ε),λb v. φ(bv)?
– The functor F is isomorphic to H(X) = A × X2.
– Therefore, the set A2∗is the final coalgebra for the functor F. Considering 2 = {L,R}
we can derive the definition of ?l,i,r? from the one presented above for π.
The fact that TA is a final coalgebra means that for any arbitrary coalgebra ?lt,o,rt? : U →
U × A × U, there exists a unique f : U → TA, such that the following diagram commutes:
∃!f
?????
U
?? ?????
?lt,o,rt?
??
TA
?l,i,r?
??
U × A × U
f×idA×f
??TA× A × TA
The existence part of this statement gives us a coinductive definition principle. Every triplet
of functions lt : U → U, o : U → A and rt : U → U defines a function h : U → TA, such
that:
i(h(x)) = o(x)l(h(x)) = h(lt(x)) r(h(x)) = h(rt(x))
We will see a more general formulation of this principle in section 3, where the right hand
side of the above equations will be more general.
Taking A = R we present the definition of the elementwise sum as an example.
ar
fg
c
de
b
+
wx
t
uv
s
=
f+w
g+x
c+t
a+r
d+u
e+v
b+s
By the definition principle presented above, taking o(?σ,τ?) = i(σ) + i(τ), lt(?σ,τ?) =
?l(σ),l(τ)? and rt(?σ,τ?) = ?r(σ),r(τ)? there is a unique function + : TR × TR → TR
satisfying:
i(σ + τ) = i(σ) + i(τ)l(σ + τ) = l(σ) + l(τ)r(σ + τ) = r(σ) + r(τ)
Note that in the first equation above we are using + to represent both the sum of trees and
the sum of real numbers.
Now that we have explained the formal definition for the set TA and how one can uniquely
define functions into TA, another important question is still to be answered: how do we
prove equality on TA? In order to prove that two trees σ and τ are equal it is necessary and
sufficient to prove
∀w∈{L,R}∗ σ(w) = τ(w) (1)
The use of induction on w (prove that σ(ε) = τ(ε) and that whenever σ(w) = τ(w) holds
then σ(aw) = τ(aw) also holds, for a ∈ {L,R}) clearly is a correct method to establish
the validity of (1). However, we will often encounter examples where there is not a general
formula for σ(w) and τ(w). Instead, we take a coalgebraic perspective on TA and use the
coinduction proof principle in order to establish equalities. This proof principle is based on
Page 3
the notion of bisimulation. A bisimulation on TA is a relation S ⊆ TA× TA such that, for
all σ and τ in TA,
(σ,τ) ∈ S ⇒ σ(ε) = τ(ε) ∧ (l(σ),l(τ)) ∈ S ∧ (r(σ),r(τ)) ∈ S
We will write σ ∼ τ whenever there exists a bisimulation that contains (σ,τ). The relation
∼, called the bisimilarity relation, is the union of all bisimulations (one can easily check that
the union of bisimulation is itself a bisimulation).
The following theorem expresses the proof principle mentioned above.
Theorem 1 (Coinduction). For all trees σ and τ in TA, if σ ∼ τ then σ = τ.
Proof. Consider two trees σ and τ in TA and let S ⊆ TA× TA be a bisimulation relation
which contains the pair (σ,τ). The equality σ(w) = τ(w) now follows by induction on the
length of w. We have that σ(ε) = τ(ε), because S is a bisimulation. If w = Lw?, then
σ(Lw?) = l(σ)(w?)
= l(τ)(w?)
= τ(Lw?)
(Definition of l)
(S is a bisimulation and induction hypothesis)
(Definition of l)
Similarly, if w = Rw?, then σ(Rw?) = r(σ)(w?) = r(τ)(w?) = τ(Rw?). Therefore, for all
w ∈ {L,R}∗, σ(w) = τ(w). This proves σ = τ.
Thus, in order to prove that two trees are equal, it is sufficient to show that they are
bisimilar. We shall see several examples of proofs by coinduction below.
As a first simple example, let us prove that the pointwise sum for trees of real numbers
defined before is commutative. Let S = {?σ + τ,τ + σ? | σ,τ ∈ TR}. Since i(σ + τ) =
i(σ) + i(τ) = i(τ + σ) and
l(σ + τ) = l(σ) + l(τ) S l(τ) + l(σ) = l(τ + σ)
r(σ + τ) = r(σ) + r(τ) S r(τ) + r(σ) = r(τ + σ)
for any σ and τ, S is a bisimulation relation on TR. The commutativity property follows by
coinduction.
3Behavioural differential equations
In this section, we shall view trees as formal power series. Following [6], coinductive defini-
tions of operators into TA and constant trees then take the shape of so-called behavioural
differential equations. We shall prove a theorem guaranteeing the existence of a unique
solution for a large family of systems of behavioural differential equations.
Formal power series are functions σ : X∗→ k from the set of words over an alphabet X to
a semiring k. If A is a semiring, TA, as defined in section 2, is the set of all formal power
series over the alphabet {L,R} with coefficients in A. In accordance with the general notion
of derivative of formal power series [6] we shall write σL for l(σ) and σR for r(σ). We will
often refer to σL, σR and σ(ε) as the left derivative, right derivative and initial value of σ.
Following [6], we will develop a coinductive calculus of infinite binary trees. From now
on coinductive definitions will have the shape of behavioural differential equations. Let us
illustrate this approach by a simple example – the coinductive definition of a tree, called
one, decorated with 1’s in every node.
11
1
1
11
1
A formal definition of this tree consists the following behavioural differential equations:
differential equations initial value
oneL = one
oneR = one
one(ε) = 1
Page 4
The fact that there exists a unique tree that satisfies the above equations will follow from
the theorem below, which presents a general format for behavioural differential equations
guaranteeing the existence and uniqueness of solutions.
Behavioural differential equations will be used not just to define single constant trees but
also functions on trees. We shall see examples below. Before we present the main result of
this section, we need one more definition. We want to be able to view any element n ∈ A as
a tree (which we will denote by [n]):
n
00
0
00
0
The tree [n] is formally defined as
[n](ε) = n
[n](w) = 0 w ?= ε
Next we present a syntax describing the format of behavioural differential equations that
we will consider. Let Σ be a set of function symbols, each with an arity r(f) ≥ 0 for f ∈ Σ.
(As usual we call f a constant if r(f) = 0.) Let X = {x1,x2,...} be a set of (syntactic)
variables, and let XL = {xL | x ∈ X}, XR = {xR | x ∈ X}, [X(ε)] = {[x(ε)] | x ∈ X} and
X(ε) = {x(ε) | x ∈ X} be sets of notational variants of them. The variables x ∈ X will play
the role of place holders for trees τ ∈ TA. Variables xL, xR, and [x(ε)] will then act as place
holders for the corresponding trees τL, τR and [τ(ε)] in TA, while x(ε) (without the square
brackets) will correspond to τ’s initial value τ(ε) ∈ A. We call a behavioural differential
equation for a function symbol f ∈ Σ with arity r = r(f) well-formed if it is of the form
differential equations initial value
f (x1,...,xr)L= t1
f (x1,...,xr)R= t2
(f (x1,...,xr))(ε) = c(x1(ε),...,xr(ε))
where c : Ar→ A is a given function, and where t1 and t2 are terms built out of function
symbols in Σ and variables in {x1,...,xr} and their corresponding notational variants in
XL, XR and [X(ε)]. A well-formed system of equations for Σ will then consist of one well-
formed equation for each f ∈ Σ. A solution of such a system of equations is a set of tree
functions
˜ Σ = {˜f : (TA)r→ TA | f ∈ Σ}
satisfying, for all f ∈ Σ with arity r and for all τ1,...,τr ∈ TA,
“
and
“
where the tree ˜ t1 ∈ TA (and similarly for ˜ t2) is obtained from the term t1 by replacing
(all occurrences of) xi by τi, (xi)L by (τi)L, (xi)R by (τi)R, and [xi(ε)] by [τi(ε)], for all
i = 1,...,r, and all function symbols g ∈ Σ by their corresponding function ˜ g.
Theorem 2. Let Σ be a set of function symbols. Every well-formed system of behavioural
differential equations for Σ has precisely one solution of tree functions˜ Σ.
˜f(τ1,...,τr)
”
(ε) = c(τ1(ε),...,τr(ε))
˜f(τ1,...,τr)
”
L=˜ t1
and
“
˜f(τ1,...,τr)
”
R=˜ t2
Proof. Please see appendix A.
Let us illustrate the generality of this theorem by mentioning a few examples of systems
of differential equations that satisfy the format above. As a first example, take Σ = {one}
consisting of a single constant symbol (with arity 0) and X = ∅. We observe that the
differential equations for one mentioned at the beginning of this section satisfy the format
of the theorem. For a second example, let Σ = {+,×} with arities r(+) = r(×) = 2 and let
X = {σ,τ}. Consider the following equations:
differential equations initial value
(σ + τ)L = σL+ τL
(σ + τ)R = σR+ τR
(σ + τ)(ε) = σ(ε) + τ(ε)
differential equations
(σ × τ)L = (σL× τ) + ([σ(ε)] × τL)
(σ × τ)R = (σR× τ) + ([σ(ε)] × τR)(σ × τ)(ε) = σ(ε) × τ(ε)
initial value
Page 5
These equations define the operations of sum and convolution product of trees, to be further
discussed in Section 4. Note that the right-hand side of the equation for (σ × τ)L (and
similarly for (σ × τ)R) is a good illustration of the general format: it is built from the
functions + and ×, applied to (a subset of) the variables on the left (τ), their derivatives
(σL and τL), and their initial values viewed as trees ([σ(ε)]).
Clearly there are many interesting interesting instances of well-formed differential equations.
Note, however, that the format does impose certain restrictions. The main point is that in
the right-hand sides of the equations, only single L and R derivatives are allowed. The
following is an example of a system of equations that is not well-formed and that does not
have a unique solution.
Let Σ = {f}, with arity r(f) = 1, and let X = {σ}. The equations for f are
differential equations initial value
f(σ)L = f(f(σLL))
f(σ)R = [0]
f(σ)(ε) = σ(ε)
Both g(σ) = [σ(ε)]+(L×[σLL(ε)]) and h(σ) = [σ(ε)]+(L×[σLL(ε)]+L2×(1−L)−1) are
solutions.
All the examples of systems of behavioural differential equations that will appear in the rest
of this document fit into the format of Theorem 2. Therefore, we will omit proofs of the
existence and uniqueness of solutions for those systems.
In the next section we will define operators on trees, based on some general operators on
formal power series [6].
4Tree calculus
In this section, we present operators on trees, namely sum, convolution product and inverse,
and state some elementary properties, which we will prove using coinduction.
The sum of two trees is defined as the unique operator satisfying:
differential equations initial value
(σ + τ)L = σL+ τL
(σ + τ)R = σR+ τR
(σ + τ)(ε) = σ(ε) + τ(ε)
Note that this is a generalisation of the sum on trees of real numbers defined in section 2
and that again we are overloading the use of + to represent both sum on trees and sum on
the elements of the semiring.
Sum satisfies some desired properties, easily proved by coinduction, such as commutativity
or associativity:
Theorem 3. For all σ,τ and ρ in TA, σ+0 = σ, σ+τ = τ +σ and σ+(τ +ρ) = (σ+τ)+ρ.
Here, we are using 0 as a shorthand for [0]. We shall use this convention (for all n ∈ A)
throughout this document. We define the convolution product of two trees as the unique
operation satisfying:
differential equations
(σ × τ)L = (σL× τ) + (σ(ε) × τL)
(σ × τ)R = (σR× τ) + (σ(ε) × τR)(σ × τ)(ε) = σ(ε) × τ(ε)
Note that in the above definition we use × for representing both multiplication on trees and
on the elements of the semiring. Following the convention mentioned above σ(ε) × τL and
σ(ε) × τR are shorthands for [σ(ε)] × τL and [σ(ε)] × τR. We shall also use the standard
convention of writing στ for σ × τ.
The general formula to compute the value of σ according to a path given by the word
w ∈ {L,R}∗is:
(σ × τ)(w) =
w=u·v
where · denotes word concatenation.
To give the reader some intuition about this operation we will give a concrete example. Take
A to be the Boolean semiring B = {0,1}, with operations + = ∨ and × = ∧. Then, TA
corresponds to the languages over a two letter alphabet, and in this case the tree product
operator coincides with language concatenation.
The following theorem states some familiar properties of this operator.
initial value
X
σ(u) × τ(v)
Page 6
Theorem 4. For all σ,τ,ρ in TA and a,b in A, σ × 1 = 1 × σ = σ, σ × 0 = 0 × σ = 0,
σ × (τ + ρ) = (σ × τ) + (σ × ρ), σ × (τ × ρ) = (σ × τ) × ρ and [a] × [b] = [a × b].
Proof. An exercise in coinduction. In [7], these properties are proved for streams.
Note that the convolution product is not commutative. Before we present the inverse oper-
ation, let us introduce two (very useful) constants, which we shall call left constant L and
right constant R. They will have an important role in the tree calculus that we are about to
develop and will turn out to have interesting properties when interacting with the product
operation. The left constant L is a tree with 0’s in every node except in the root of the left
branch where it has a 1:
00
0
0
00
1
L =
It is formally defined as
L(w) = 1 if w = L
L(w) = 0 otherwise
Similarly, the right constant R is only different from 0 in the root of its right branch:
00
1
0
00
0
R =
and is defined as
R(w) = 1 if w = R
R(w) = 0 otherwise
These constants have interesting properties when multiplied by an arbitrary tree. L × σ
produces a tree whose root and right subtrees are null and the left branch is σ:
00
0
0
00
1
X
fg
c
a
de
b
=
0
fg
c
a
de
b
0
0
0
Dually, R × σ produces a tree whose root and left subtrees are null and the right branch is
σ:
0p
00
1
00
0
X
uv
r
st
q
=
0
uv
r
p
st
q
0
0
0
As before, if we see L and σ as languages and the product as concatenation, we can gain
some intuition on the meaning of this operation. L × σ will prefix every word of σ with the
letter L, meaning that no word starting by R will be an element of L × σ, and thus, L × σ
has a null right branch. Similar for R × σ.
As we pointed out before, the product operation is not commutative. For example, σ ×L ?=
L×σ and σ×R ?= R×σ. In fact, multiplying a tree σ on the right with L or R is interesting
in itself. For instance, σ × L satisfies
(σ × L)(w) =
σ(u) w = uL
0otherwise
which corresponds to the following transformation:
00
0
0
00
1
X
fg
c
a
de
b
=
0
a
d0
b
e0
0
0
f0
c
g0
0
Similarly, σ × R produces the following tree:
00
1
0
00
0
X
fg
c
a
de
b
=
0
0
0d
0
0e
b
a
0f
0
0g
c
Again, if you interpret these operations in the language setting, what is being constructed
is the language that has all words of the form uL and uR, respectively, such that σ(u) ?= 0.
Page 7
We define the inverse of a tree as the unique operator satisfying:
differential equations
(σ−1)L = σ(ε)−1× (−σL× (σ−1))
(σ−1)R = σ(ε)−1× (−σR× (σ−1))σ−1(ε) = σ(ε)−1
We are using −σL and −σR as shorthands for [−1]×σL and [−1]×σR, respectively. In this
definition, we need to require A to be a ring, in order to have additive inverses. Moreover,
the tree σ is supposed to have also a multiplicative inverse for its initial value.
The inverse of a tree has the usual properties:
initial value
Theorem 5. For all σ and τ in TA:
σ−1is the unique tree s.t. σ × σ−1= 1
(σ × τ)−1= τ−1× σ−1
(2)
(3)
Proof. For the existence part of (2), note that
1. (σ × σ−1)(ε) = σ(ε) × σ(ε)−1= 1
2. (σ × σ−1)L = (σL× σ−1) + (σ(ε) × (σ(ε)−1× (−σL× σ−1))) = 0
3. (σ × σ−1)R = (σR× σ−1) + (σ(ε) × (σ(ε)−1× (−σR× σ−1))) = 0
So, by uniqueness (using the behavioural differential equations that define 1) we have proved
that σ × σ−1= 1. Now, for the uniqueness part of (2), suppose that there is a tree τ such
that σ × τ = 1. We shall prove that τ = σ−1. Note that from the equality σ × τ = 1 we
derive that
1. τ(ε) = σ(ε)−1
2. τL = σ(ε) × (−σL× τ)
3. τR = σ(ε) × (−σR× τ)
Thus, by uniqueness of solutions for systems of behavioural differential equations, τ = σ−1.
For (3), note that (σ × τ) × τ−1× σ−1= σ × (τ × τ−1) × σ−1= 1. Therefore, using the
uniqueness property of (2), (σ × τ)−1= τ−1× σ−1.
5Applications of tree calculus
We will illustrate the usefulness of our calculus by looking at a series of interesting examples.
In order to compute closed formulae for trees we will be using the following identity:
∀σ∈TAσ = σ(ε) + (L × σL) + (R × σR) (4)
which can be easily proved by coinduction. We will now show how to use this identity to
construct the closed formula for a tree.
Recall our first system of behavioural differential equations:
differential equations initial value
oneL = one
oneR = one
one(ε) = 1
There we saw that the unique solution for this system was the tree with 1’s in every node.
Alternatively, we can compute the solution using (4) as follows.
one= one(ε) + (L × oneL) + (R × oneR)
= 1 + (L × one) + (R × one)
⇔
⇔ (1 − L − R)one = 1
⇔
one
one= (1 − L − R)−1
Therefore, one can be represented by the (very compact) closed formula3(1 − L − R)−1.
Let us see a few more examples. From now on we will work with A = R, where we have the
extra property: [n] × σ = σ × [n], for all n ∈ R and σ ∈ TR.
3Note the similarity of this closed formula with the one obtained for the stream (1,1,...) in [7]: (1−X)−1.
Page 8
The tree where every node at level k is labelled with the value 2k, called pow,
44
2
1
44
2
is defined by the following system:
differential equations initial value
powL = 2 × pow
powR = 2 × pow
pow(ε) = 1
We proceed as before, applying (4):
pow= pow(ε) + (L × powL) + (R × powR)
= 1 + (2L × pow) + (2R × pow)
⇔
⇔ (1 − 2L − 2R)pow = 1
⇔
pow
pow= (1 − 2L − 2R)−1
which gives us a nice closed formula for pow4.
The tree with the natural numbers
67
3
1
45
2
is represented by the following system of differential equations:
differential equations
natL = nat + pow
natR = nat + (2 × pow)nat(ε) = 1
initial value
Applying identity (4):
nat= nat(ε) + (L × natL) + (R × natR)
= 1 + (L × (nat + pow)) + (R × (nat + 2pow))
⇔ (1 − L − R)nat = 1 + L(1 − 2L − 2R)−1+ 2R(1 − 2L − 2R)−1
⇔ (1 − L − R)nat = (1 − L) × (1 − 2L − 2R)−1
⇔
The Thue-Morse sequence [1] can be obtained by taking the parities of the counts of 1’s in
the binary representation of non-negative integers. Alternatively, it can be defined by the
repeated application of the substitution map {0 → 01;1 → 10}:
0 → 01 → 0110 → 01101001 → ...
We can encode this substitution map in a binary tree, called thue, which at each level k will
have the first 2kdigits of the Thue-Morse sequence:
⇔
nat
nat= (1 − L − R)−1× (1 − L) × (1 − 2L − 2R)−1
10
1
0
01
0
In this example, we take for A the Boolean ring 2 = {0,1} (where 1+1 = 0). The following
system of differential equations defines thue:
differential equations initial value
thueL = thue
thueR = thue + onethue(ε) = 0
4Again, there is a strong similarity with streams: the closed formula for the stream (1,2,4,8,...) is
(1 − 2X)−1
Page 9
Note that thue + one equals the (elementwise) complement of thue. Applying (4) to thue,
we calculate:
thue = (L × thue) + (R × (thue + one))
⇔ (1 − L − R) × thue = R × one
⇔ thue
which then leads to the following pretty formula for thue:
= (1 − L − R)−1× R × one
thue = one × R × one
Let us present another example – a substitution operation, which given two trees σ and τ,
replaces the left subtree of σ by τ.
σ(ε)
σL
σR
=
subst (
)
, τ
σ(ε)
τ
σR
It is easy to see that the equations that define this operation are:
differential equations initial value
subst(σ,τ)L = τ
subst(σ,τ)R = σR
subst(σ,τ)(ε) = σ(ε)
Then, we apply (4) and we reason:
subst(σ,τ) = σ(ε) + (L × τ) + (R × σR)
⇔ subst(σ,τ) = σ − (L × σL) + (L × τ)
⇔ subst(σ,τ) = σ − L(σL− τ)
Note that in the second step, we applied identity (4) to σ. Moreover, remark that the final
closed formula for subst(σ,τ) gives us the algorithm to compute the substitution:
σ(ε)
0
σR
+
0
τ
0
-
0
σL
0
σ(ε)
σL
σR
=
subst (
)
, τ
σ(ε)
τ
σR
We can now wonder how to define a more general substitution operation that has an arbitrary
path P ∈ {L,R}+as an extra argument and replaces the subtree of σ given by this path
by τ. It seems obvious to define it as
subst(σ,τ,P) = σ − P(σP − τ)
where, in the right hand side, P = a1a2...an is interpreted as a1× a2× ... × an and the
derivative σP is defined as
σδ
with δ being either L or R.
Let us check that our intuition is correct. First, we present the definition for this operation:
σP =
P = δ
(σδ)P? P = δ.P?
differential equations initial value
subst(σ,τ,P)δ =
8
:
<
τ
subst(σδ,τ,P?) P = δ.P?
σδ
P = δ
P = δ?.P?
subst(σ,τ,P)(ε) = σ(ε)
where δ??= δ. Now, observe that
R = {?subst(σ,τ,P),σ − P(σP − τ)? | σ,τ ∈ TR, P ∈ {L,R}+} ∪ {?σ,σ? | σ ∈ TR}
is a bisimulation relation because:
1. (σ − P(σP − τ))(ε) = σ(ε) = subst(σ,τ,P)(ε)
Page 10
2. For δ ∈ {L,R},
(σ − P(σP − τ))δ = σδ− Pδ(σP − τ)
=
8
:
<
<
8
:
τ
σδ− P?((σδ)P? − τ) P = δ.P?
σδ
P = δ
P = δ?.P?
R
τ
subst(σδ,τ,P?) P = δ.P?
σδ
P = δ
P = δ?.P?
= subst(σ,τ,P)δ
Therefore, by Theorem 1, subst(σ,τ,P) = σ − P(σP − τ).
Using this formula we can now prove properties about this operation. For instance, one
would expect that subst(σ,σP,P) = σ and also subst(subst(σ,τ,P),σP,P) = σ.
The first equality follows easily: subst(σ,σP,P) = σ − P(σP − σP) = σ. For the second one
we have:
subst(subst(σ,τ,P),σP,P)
= subst(σ − P(σP − τ),σP,P)
= σ − P(σP − τ) − P((σ − P(σP − τ))P − σP) (Definition of subst)
= σ − P(σP − τ) − P(τ − σP)
= σ
Remark that this operation is a standard example in introductory courses on algorithms
and data structures. It is often presented either as a recursive expression (very much in the
style of our differential equations) or as a contrived iterative procedure. This example shows
that our compact formulae constitute a clear way of presenting algorithms and that they
can be used to eliminate recursion. Moreover, the differential equations are directly imple-
mentable algorithms (in functional programming) and our calculus provides a systematic
way of reasoning about such programs.
(Definition of subst)
((σ − P(σP − τ))P = σP − σP + τ = τ)
6 Discussion
We have modelled binary trees as formal power series and, using the fact that the latter
constitute a final coalgebra, this has enabled us to apply some coalgebraic reasoning. Tech-
nically, none of this is very difficult. Rather, it is an application of well known coalgebraic
insights. As is the case with many of such applications, it has the flavour of an exercise. At
the same time, the result contains several new elements that have surprised us. Although
technically Theorem 2 is an easy extension of a similar such theorem for streams, the re-
sulting format for differential equations for trees is surprisingly general and useful. It has
allowed us to define various non-trivial trees by means of simple differential equations, and
to compute rather pleasant closed formulae for them. We have also illustrated that based on
this, coinduction is a convenient proof method for trees. As an application, all of this is new,
to the best of our knowledge. (Formal tree series, which have been studied extensively, may
seem to be closely related but are not: here we are dealing with differential equations that
characterise single trees.) In addition to the illustrations of the present differential calculus
for trees, we see various directions for further applications: (i) The connection with (vari-
ous types of) automata and the final coalgebra TA of binary trees needs further study. For
instance, every Moore automaton with input in 2 = {L,R} and output in A has a minimal
representation in TA. Details of these automata (and on weighted variants of them) are not
complicated but have not been worked out here because of lack of space. (ii) The closed
formula that we have obtained for the (binary tree representing the) Thue-Morse sequence
suggests a possible use of coinduction and differential equations in the area of automatic
sequences [2]. Typically, automatic sequences are represented by automata. The present cal-
culus seems an interesting alternative, in which properties such as algebraicity of sequences
can be derived from the tree differential equations that define them. (iii) Finally, the closed
formulae that we obtain for tree substitution suggest many further applications of our tree
calculus to (functional) programs on trees, including the analysis of their complexity.
Page 11
References
1. J.-P. Allouche and J. Shallit.
C. Ding, T. Helleseth, and N. H., editors, Sequences and their applications, Proceedings
of SETA’98, pages 1–16. Springer Verlag, 1999.
2. J.-P. Allouche and J. Shallit. Automatic sequences: theory, applications, generalizations.
Cambridge University Press, 2003.
3. Z.´Esik and W. Kuich. Formal tree series. Journal of Automata, Languages and Combi-
natorics, 8(2):219–285, 2003.
4. E. G. Manes and M. A. Arbib. Algebraic approaches to program semantics. Springer-
Verlag New York, Inc., New York, NY, USA, 1986.
5. D. Perrin and J.-E. Pin. Infinite Words, volume 141 of Pure and Applied Mathematics.
Elsevier, 2004. ISBN 0-12-532111-2.
6. J. J. M. M. Rutten. Behavioural differential equations: a coinductive calculus of streams,
automata, and power series. Theor. Comput. Sci., 308(1-3):1–53, 2003.
7. J. J. M. M. Rutten. A coinductive calculus of streams. Mathematical Structures in
Computer Science, 15(1):93–147, 2005.
The ubiquitous Prouhet-Thue-Morse sequence.In
AProof of theorem 2
Proof (Proof of theorem 2). Consider a well-formed system of differential equations for Σ,
as defined above. We define a set T of terms t by the following syntax:
t ::= τ
(τ ∈ TA) | f(t1,...,tr(f)) (f ∈ Σ)
where for every tree τ ∈ TA the set T contains a corresponding term, denoted by τ, and
where new terms are constructed by (syntactic) composition of function symbols from Σ
with the appropriate number of argument terms. Next we turn T into an F-coalgebra by
defining a function ?l,o,r? : T → (T × A × T ) by induction on the structure of terms, as
follows. First we define o : T → A by
o(τ) = τ(ε)
o`f(t1,...tr(f))´= c`o(t1),...,o(tr(f))´
(where c is the function used in the equations for f). Next we define l : T → T and r : T → T
by l(τ) = τL and r(τ) = τR, and by
l(f(t1,...tr)) = t1
andl(f(t1,...tr)) = t2
Here the terms t1and t2are obtained from the terms t1and t2used in the equations for f, by
replacing (every occurrence of) xi by ti, (xi)L by l(ti), (xi)R by r(ti), and [xi(ε)] by [o(t)],
for i = 1,...,r. Because TA is a final F-coalgebra, there exists a unique homomorphism
h : T → TA. We can use it to define tree functions˜f : (TA)r→ TA, for every f ∈ Σ, by
putting, for all τ1,...,τr ∈ TA,
˜f(τ1,...,τr) = h`f(τ1,...,τr)´
This gives us a set˜ Σ of tree functions. One can prove that it is a solution of our system of
differential equations by coinduction, using the facts that h(τ) = τ, for all τ ∈ TA, and
h(f(t1,...,tr)) =˜f (h(t1),...,h(tr))
for all f ∈ Σ and ti ∈ T . This solution is unique because, by finality of TA, the homomor-
phism h is.