9722
|
1 |
%
|
|
2 |
\begin{isabellebody}%
|
9924
|
3 |
\def\isabellecontext{Fundata}%
|
11866
|
4 |
\isamarkupfalse%
|
|
5 |
\isacommand{datatype}\ {\isacharparenleft}{\isacharprime}a{\isacharcomma}{\isacharprime}i{\isacharparenright}bigtree\ {\isacharequal}\ Tip\ {\isacharbar}\ Br\ {\isacharprime}a\ {\isachardoublequote}{\isacharprime}i\ {\isasymRightarrow}\ {\isacharparenleft}{\isacharprime}a{\isacharcomma}{\isacharprime}i{\isacharparenright}bigtree{\isachardoublequote}\isamarkupfalse%
|
|
6 |
%
|
8749
|
7 |
\begin{isamarkuptext}%
|
9792
|
8 |
\noindent
|
|
9 |
Parameter \isa{{\isacharprime}a} is the type of values stored in
|
10420
|
10 |
the \isa{Br}anches of the tree, whereas \isa{{\isacharprime}i} is the index
|
9792
|
11 |
type over which the tree branches. If \isa{{\isacharprime}i} is instantiated to
|
8749
|
12 |
\isa{bool}, the result is a binary tree; if it is instantiated to
|
|
13 |
\isa{nat}, we have an infinitely branching tree because each node
|
|
14 |
has as many subtrees as there are natural numbers. How can we possibly
|
9541
|
15 |
write down such a tree? Using functional notation! For example, the term
|
|
16 |
\begin{isabelle}%
|
11706
|
17 |
\ \ \ \ \ Br\ {\isacharparenleft}{\isadigit{0}}{\isasymColon}{\isacharprime}a{\isacharparenright}\ {\isacharparenleft}{\isasymlambda}i{\isachardot}\ Br\ i\ {\isacharparenleft}{\isasymlambda}n{\isachardot}\ Tip{\isacharparenright}{\isacharparenright}%
|
9924
|
18 |
\end{isabelle}
|
9673
|
19 |
of type \isa{{\isacharparenleft}nat{\isacharcomma}\ nat{\isacharparenright}\ bigtree} is the tree whose
|
8771
|
20 |
root is labeled with 0 and whose $i$th subtree is labeled with $i$ and
|
8749
|
21 |
has merely \isa{Tip}s as further subtrees.
|
|
22 |
|
9792
|
23 |
Function \isa{map{\isacharunderscore}bt} applies a function to all labels in a \isa{bigtree}:%
|
8749
|
24 |
\end{isamarkuptext}%
|
11866
|
25 |
\isamarkuptrue%
|
9673
|
26 |
\isacommand{consts}\ map{\isacharunderscore}bt\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}{\isacharparenleft}{\isacharprime}a\ {\isasymRightarrow}\ {\isacharprime}b{\isacharparenright}\ {\isasymRightarrow}\ {\isacharparenleft}{\isacharprime}a{\isacharcomma}{\isacharprime}i{\isacharparenright}bigtree\ {\isasymRightarrow}\ {\isacharparenleft}{\isacharprime}b{\isacharcomma}{\isacharprime}i{\isacharparenright}bigtree{\isachardoublequote}\isanewline
|
11866
|
27 |
\isamarkupfalse%
|
8749
|
28 |
\isacommand{primrec}\isanewline
|
10420
|
29 |
{\isachardoublequote}map{\isacharunderscore}bt\ f\ Tip\ \ \ \ \ \ {\isacharequal}\ Tip{\isachardoublequote}\isanewline
|
11866
|
30 |
{\isachardoublequote}map{\isacharunderscore}bt\ f\ {\isacharparenleft}Br\ a\ F{\isacharparenright}\ {\isacharequal}\ Br\ {\isacharparenleft}f\ a{\isacharparenright}\ {\isacharparenleft}{\isasymlambda}i{\isachardot}\ map{\isacharunderscore}bt\ f\ {\isacharparenleft}F\ i{\isacharparenright}{\isacharparenright}{\isachardoublequote}\isamarkupfalse%
|
|
31 |
%
|
8749
|
32 |
\begin{isamarkuptext}%
|
|
33 |
\noindent This is a valid \isacommand{primrec} definition because the
|
9792
|
34 |
recursive calls of \isa{map{\isacharunderscore}bt} involve only subtrees obtained from
|
11458
|
35 |
\isa{F}: the left-hand side. Thus termination is assured. The
|
|
36 |
seasoned functional programmer might try expressing
|
|
37 |
\isa{{\isasymlambda}i{\isachardot}\ map{\isacharunderscore}bt\ f\ {\isacharparenleft}F\ i{\isacharparenright}} as \isa{map{\isacharunderscore}bt\ f\ {\isasymcirc}\ F}, which Isabelle
|
|
38 |
however will reject. Applying \isa{map{\isacharunderscore}bt} to only one of its arguments
|
|
39 |
makes the termination proof less obvious.
|
8749
|
40 |
|
11309
|
41 |
The following lemma has a simple proof by induction:%
|
8749
|
42 |
\end{isamarkuptext}%
|
11866
|
43 |
\isamarkuptrue%
|
9673
|
44 |
\isacommand{lemma}\ {\isachardoublequote}map{\isacharunderscore}bt\ {\isacharparenleft}g\ o\ f{\isacharparenright}\ T\ {\isacharequal}\ map{\isacharunderscore}bt\ g\ {\isacharparenleft}map{\isacharunderscore}bt\ f\ T{\isacharparenright}{\isachardoublequote}\isanewline
|
11866
|
45 |
\isamarkupfalse%
|
10171
|
46 |
\isacommand{apply}{\isacharparenleft}induct{\isacharunderscore}tac\ T{\isacharcomma}\ simp{\isacharunderscore}all{\isacharparenright}\isanewline
|
11866
|
47 |
\isamarkupfalse%
|
|
48 |
\isacommand{done}\isamarkupfalse%
|
|
49 |
\isamarkupfalse%
|
|
50 |
\isamarkupfalse%
|
|
51 |
%
|
10420
|
52 |
\begin{isamarkuptxt}%
|
8749
|
53 |
\noindent
|
11458
|
54 |
Because of the function type, the
|
|
55 |
the proof state after induction looks unusual.
|
10795
|
56 |
Notice the quantified induction hypothesis:
|
10420
|
57 |
\begin{isabelle}%
|
|
58 |
\ {\isadigit{1}}{\isachardot}\ map{\isacharunderscore}bt\ {\isacharparenleft}g\ {\isasymcirc}\ f{\isacharparenright}\ Tip\ {\isacharequal}\ map{\isacharunderscore}bt\ g\ {\isacharparenleft}map{\isacharunderscore}bt\ f\ Tip{\isacharparenright}\isanewline
|
10901
|
59 |
\ {\isadigit{2}}{\isachardot}\ {\isasymAnd}a\ F{\isachardot}\ {\isacharparenleft}{\isasymAnd}x{\isachardot}\ map{\isacharunderscore}bt\ {\isacharparenleft}g\ {\isasymcirc}\ f{\isacharparenright}\ {\isacharparenleft}F\ x{\isacharparenright}\ {\isacharequal}\ map{\isacharunderscore}bt\ g\ {\isacharparenleft}map{\isacharunderscore}bt\ f\ {\isacharparenleft}F\ x{\isacharparenright}{\isacharparenright}{\isacharparenright}\ {\isasymLongrightarrow}\isanewline
|
10950
|
60 |
\isaindent{\ {\isadigit{2}}{\isachardot}\ {\isasymAnd}a\ F{\isachardot}\ }map{\isacharunderscore}bt\ {\isacharparenleft}g\ {\isasymcirc}\ f{\isacharparenright}\ {\isacharparenleft}Br\ a\ F{\isacharparenright}\ {\isacharequal}\ map{\isacharunderscore}bt\ g\ {\isacharparenleft}map{\isacharunderscore}bt\ f\ {\isacharparenleft}Br\ a\ F{\isacharparenright}{\isacharparenright}%
|
9723
|
61 |
\end{isabelle}%
|
10420
|
62 |
\end{isamarkuptxt}%
|
11866
|
63 |
\isamarkuptrue%
|
|
64 |
\isamarkupfalse%
|
|
65 |
\isamarkupfalse%
|
9722
|
66 |
\end{isabellebody}%
|
9145
|
67 |
%%% Local Variables:
|
|
68 |
%%% mode: latex
|
|
69 |
%%% TeX-master: "root"
|
|
70 |
%%% End:
|