As stated previously in this book, a binary tree is a Braun tree if
it is empty or if its left and right subtrees are Braun trees and
the size of the left one minus the size of the right one is either 0 or 1.
Formally, we can declare the following dataprop isBraun to
capture the notion of Braun trees:
We first prove that there exists a Braun tree of any given size.
This property can be encoded as follows in ATS:
Literally, the type assigned to
lemma_existence means that there
exists a tree t for any given natural number n such that t is a Braun tree
and the size of t is n. The following code gives an implementation of
lemma_existence:
Note that
stadef is a keyword in ATS for introducting a static
binding between a name and a static term (of any sort). If one prefers,
this keyword can be chosen to replace the keyword
typedef (for
introducing a name and a static term of the sort
t@ype).
Next we show that two Braun trees of the same size are identical. This
property can be encoded as follows:
where
EQ is a prop-constructor introduced by the following
dataprop declaration:
Clearly,
EQ is the inductively defined equality on trees.
An implementation of the proof function
lemma_unicity is presented
as follows:
Note that the proof function
SZ_istot in this implementation of
lemma_unicity is given the following interface:
which simply states that
SZ is a functional relation with respect
to its first parameter, that is, there is at most one n for every given t
such that t and n are related according to
SZ.
Clearly, the mathematical proof corresponding to this implementation is of
induction on the size n of the two given trees t1 and t2. In the base case
where n is 0, t1 and t2 are equal as they both are the empty tree. In the
inductive case where n > 0, it is proven that n1l and n2l are of the same
value where n1l and n2l are the sizes of the left subtrees of t1 and t2,
respecitvely; similarly, it is also proven that n1r and n2r are of the same
value where n1r and n2r are the sizes of the right subtrees of t1 and t2,
respectively; by induction hypothesis on n1l, the left substrees of t1 and
t2 are the same; by induction hypothesis on n1r, the right substrees of t1
and t2 are the same; by the definition of tree equality (encoded by
EQ), t1 and t2 are the same.
As a comparison, the following code gives another implementation of
lemma_unicity that is of a different (and rather unusual) style:
This implementation corresponds to a proof by induction on the structure of
the given tree t1. Note that the use of the special symbol
=/=>,
which is a keyword in ATS, is to indicate to the typechecker of ATS that
the involved clause of patter matching is
unreachable: It is the
responsibility of the programmer to establish the falsehood on the
right-hand side of the clause. Please find the entirety of the above code
on-line.