8749
|
1 |
\begin{isabelle}%
|
|
2 |
\isacommand{datatype}~('a,'i)bigtree~=~Tip~|~Branch~'a~{"}'i~{\isasymRightarrow}~('a,'i)bigtree{"}%
|
|
3 |
\begin{isamarkuptext}%
|
|
4 |
\noindent Parameter \isa{'a} is the type of values stored in
|
|
5 |
the \isa{Branch}es of the tree, whereas \isa{'i} is the index
|
|
6 |
type over which the tree branches. If \isa{'i} is instantiated to
|
|
7 |
\isa{bool}, the result is a binary tree; if it is instantiated to
|
|
8 |
\isa{nat}, we have an infinitely branching tree because each node
|
|
9 |
has as many subtrees as there are natural numbers. How can we possibly
|
|
10 |
write down such a tree? Using functional notation! For example, the%
|
|
11 |
\end{isamarkuptext}%
|
|
12 |
\isacommand{term}~{"}Branch~0~({\isasymlambda}i.~Branch~i~({\isasymlambda}n.~Tip)){"}%
|
|
13 |
\begin{isamarkuptext}%
|
|
14 |
\noindent of type \isa{(nat,nat)bigtree} is the tree whose
|
8771
|
15 |
root is labeled with 0 and whose $i$th subtree is labeled with $i$ and
|
8749
|
16 |
has merely \isa{Tip}s as further subtrees.
|
|
17 |
|
|
18 |
Function \isa{map_bt} applies a function to all labels in a \isa{bigtree}:%
|
|
19 |
\end{isamarkuptext}%
|
|
20 |
\isacommand{consts}~map\_bt~::~{"}('a~{\isasymRightarrow}~'b)~{\isasymRightarrow}~('a,'i)bigtree~{\isasymRightarrow}~('b,'i)bigtree{"}\isanewline
|
|
21 |
\isacommand{primrec}\isanewline
|
|
22 |
{"}map\_bt~f~Tip~~~~~~~~~~=~Tip{"}\isanewline
|
|
23 |
{"}map\_bt~f~(Branch~a~F)~=~Branch~(f~a)~({\isasymlambda}i.~map\_bt~f~(F~i)){"}%
|
|
24 |
\begin{isamarkuptext}%
|
|
25 |
\noindent This is a valid \isacommand{primrec} definition because the
|
|
26 |
recursive calls of \isa{map_bt} involve only subtrees obtained from
|
|
27 |
\isa{F}, i.e.\ the left-hand side. Thus termination is assured. The
|
|
28 |
seasoned functional programmer might have written \isa{map_bt~f~o~F} instead
|
|
29 |
of \isa{\isasymlambda{}i.~map_bt~f~(F~i)}, but the former is not accepted by
|
|
30 |
Isabelle because the termination proof is not as obvious since
|
|
31 |
\isa{map_bt} is only partially applied.
|
|
32 |
|
|
33 |
The following lemma has a canonical proof%
|
|
34 |
\end{isamarkuptext}%
|
8771
|
35 |
\isacommand{lemma}~{"}map\_bt~(g~o~f)~T~=~map\_bt~g~(map\_bt~f~T){"}\isanewline
|
8749
|
36 |
\isacommand{apply}(induct\_tac~{"}T{"},~auto)\isacommand{.}%
|
|
37 |
\begin{isamarkuptext}%
|
|
38 |
\noindent
|
|
39 |
but it is worth taking a look at the proof state after the induction step
|
|
40 |
to understand what the presence of the function type entails:
|
|
41 |
\begin{isabellepar}%
|
|
42 |
~1.~map\_bt~g~(map\_bt~f~Tip)~=~map\_bt~(g~{\isasymcirc}~f)~Tip\isanewline
|
|
43 |
~2.~{\isasymAnd}a~F.\isanewline
|
|
44 |
~~~~~~{\isasymforall}x.~map\_bt~g~(map\_bt~f~(F~x))~=~map\_bt~(g~{\isasymcirc}~f)~(F~x)~{\isasymLongrightarrow}\isanewline
|
|
45 |
~~~~~~map\_bt~g~(map\_bt~f~(Branch~a~F))~=~map\_bt~(g~{\isasymcirc}~f)~(Branch~a~F)%
|
|
46 |
\end{isabellepar}%%
|
|
47 |
\end{isamarkuptext}%
|
|
48 |
\end{isabelle}%
|