author | nipkow |
Thu, 12 Jun 2008 14:20:07 +0200 | |
changeset 27166 | 968a1450ae35 |
parent 27144 | ef2634bef947 |
child 27319 | 6584901d694c |
permissions | -rw-r--r-- |
9722 | 1 |
% |
2 |
\begin{isabellebody}% |
|
9924 | 3 |
\def\isabellecontext{ABexpr}% |
17056 | 4 |
% |
5 |
\isadelimtheory |
|
6 |
% |
|
7 |
\endisadelimtheory |
|
8 |
% |
|
9 |
\isatagtheory |
|
10 |
% |
|
11 |
\endisatagtheory |
|
12 |
{\isafoldtheory}% |
|
13 |
% |
|
14 |
\isadelimtheory |
|
15 |
% |
|
16 |
\endisadelimtheory |
|
8749 | 17 |
% |
18 |
\begin{isamarkuptext}% |
|
11458 | 19 |
\index{datatypes!mutually recursive}% |
8749 | 20 |
Sometimes it is necessary to define two datatypes that depend on each |
21 |
other. This is called \textbf{mutual recursion}. As an example consider a |
|
22 |
language of arithmetic and boolean expressions where |
|
23 |
\begin{itemize} |
|
24 |
\item arithmetic expressions contain boolean expressions because there are |
|
11458 | 25 |
conditional expressions like ``if $m<n$ then $n-m$ else $m-n$'', |
8749 | 26 |
and |
27 |
\item boolean expressions contain arithmetic expressions because of |
|
28 |
comparisons like ``$m<n$''. |
|
29 |
\end{itemize} |
|
30 |
In Isabelle this becomes% |
|
31 |
\end{isamarkuptext}% |
|
17175 | 32 |
\isamarkuptrue% |
33 |
\isacommand{datatype}\isamarkupfalse% |
|
34 |
\ {\isacharprime}a\ aexp\ {\isacharequal}\ IF\ \ \ {\isachardoublequoteopen}{\isacharprime}a\ bexp{\isachardoublequoteclose}\ {\isachardoublequoteopen}{\isacharprime}a\ aexp{\isachardoublequoteclose}\ {\isachardoublequoteopen}{\isacharprime}a\ aexp{\isachardoublequoteclose}\isanewline |
|
35 |
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ {\isacharbar}\ Sum\ \ {\isachardoublequoteopen}{\isacharprime}a\ aexp{\isachardoublequoteclose}\ {\isachardoublequoteopen}{\isacharprime}a\ aexp{\isachardoublequoteclose}\isanewline |
|
36 |
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ {\isacharbar}\ Diff\ {\isachardoublequoteopen}{\isacharprime}a\ aexp{\isachardoublequoteclose}\ {\isachardoublequoteopen}{\isacharprime}a\ aexp{\isachardoublequoteclose}\isanewline |
|
9673 | 37 |
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ {\isacharbar}\ Var\ {\isacharprime}a\isanewline |
38 |
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ {\isacharbar}\ Num\ nat\isanewline |
|
17175 | 39 |
\isakeyword{and}\ \ \ \ \ \ {\isacharprime}a\ bexp\ {\isacharequal}\ Less\ {\isachardoublequoteopen}{\isacharprime}a\ aexp{\isachardoublequoteclose}\ {\isachardoublequoteopen}{\isacharprime}a\ aexp{\isachardoublequoteclose}\isanewline |
40 |
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ {\isacharbar}\ And\ \ {\isachardoublequoteopen}{\isacharprime}a\ bexp{\isachardoublequoteclose}\ {\isachardoublequoteopen}{\isacharprime}a\ bexp{\isachardoublequoteclose}\isanewline |
|
41 |
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ {\isacharbar}\ Neg\ \ {\isachardoublequoteopen}{\isacharprime}a\ bexp{\isachardoublequoteclose}% |
|
8749 | 42 |
\begin{isamarkuptext}% |
43 |
\noindent |
|
44 |
Type \isa{aexp} is similar to \isa{expr} in \S\ref{sec:ExprCompiler}, |
|
11309 | 45 |
except that we have added an \isa{IF} constructor, |
46 |
fixed the values to be of type \isa{nat} and declared the two binary |
|
47 |
operations \isa{Sum} and \isa{Diff}. Boolean |
|
8749 | 48 |
expressions can be arithmetic comparisons, conjunctions and negations. |
11458 | 49 |
The semantics is given by two evaluation functions:% |
8749 | 50 |
\end{isamarkuptext}% |
17175 | 51 |
\isamarkuptrue% |
27015 | 52 |
\isacommand{primrec}\isamarkupfalse% |
53 |
\ evala\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a\ aexp\ {\isasymRightarrow}\ {\isacharparenleft}{\isacharprime}a\ {\isasymRightarrow}\ nat{\isacharparenright}\ {\isasymRightarrow}\ nat{\isachardoublequoteclose}\ \isakeyword{and}\isanewline |
|
54 |
\ \ \ \ \ \ \ \ \ evalb\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a\ bexp\ {\isasymRightarrow}\ {\isacharparenleft}{\isacharprime}a\ {\isasymRightarrow}\ nat{\isacharparenright}\ {\isasymRightarrow}\ bool{\isachardoublequoteclose}\ \isakeyword{where}\isanewline |
|
55 |
{\isachardoublequoteopen}evala\ {\isacharparenleft}IF\ b\ a{\isadigit{1}}\ a{\isadigit{2}}{\isacharparenright}\ env\ {\isacharequal}\isanewline |
|
56 |
\ \ \ {\isacharparenleft}if\ evalb\ b\ env\ then\ evala\ a{\isadigit{1}}\ env\ else\ evala\ a{\isadigit{2}}\ env{\isacharparenright}{\isachardoublequoteclose}\ {\isacharbar}\isanewline |
|
57 |
{\isachardoublequoteopen}evala\ {\isacharparenleft}Sum\ a{\isadigit{1}}\ a{\isadigit{2}}{\isacharparenright}\ env\ {\isacharequal}\ evala\ a{\isadigit{1}}\ env\ {\isacharplus}\ evala\ a{\isadigit{2}}\ env{\isachardoublequoteclose}\ {\isacharbar}\isanewline |
|
58 |
{\isachardoublequoteopen}evala\ {\isacharparenleft}Diff\ a{\isadigit{1}}\ a{\isadigit{2}}{\isacharparenright}\ env\ {\isacharequal}\ evala\ a{\isadigit{1}}\ env\ {\isacharminus}\ evala\ a{\isadigit{2}}\ env{\isachardoublequoteclose}\ {\isacharbar}\isanewline |
|
59 |
{\isachardoublequoteopen}evala\ {\isacharparenleft}Var\ v{\isacharparenright}\ env\ {\isacharequal}\ env\ v{\isachardoublequoteclose}\ {\isacharbar}\isanewline |
|
60 |
{\isachardoublequoteopen}evala\ {\isacharparenleft}Num\ n{\isacharparenright}\ env\ {\isacharequal}\ n{\isachardoublequoteclose}\ {\isacharbar}\isanewline |
|
61 |
\isanewline |
|
62 |
{\isachardoublequoteopen}evalb\ {\isacharparenleft}Less\ a{\isadigit{1}}\ a{\isadigit{2}}{\isacharparenright}\ env\ {\isacharequal}\ {\isacharparenleft}evala\ a{\isadigit{1}}\ env\ {\isacharless}\ evala\ a{\isadigit{2}}\ env{\isacharparenright}{\isachardoublequoteclose}\ {\isacharbar}\isanewline |
|
63 |
{\isachardoublequoteopen}evalb\ {\isacharparenleft}And\ b{\isadigit{1}}\ b{\isadigit{2}}{\isacharparenright}\ env\ {\isacharequal}\ {\isacharparenleft}evalb\ b{\isadigit{1}}\ env\ {\isasymand}\ evalb\ b{\isadigit{2}}\ env{\isacharparenright}{\isachardoublequoteclose}\ {\isacharbar}\isanewline |
|
64 |
{\isachardoublequoteopen}evalb\ {\isacharparenleft}Neg\ b{\isacharparenright}\ env\ {\isacharequal}\ {\isacharparenleft}{\isasymnot}\ evalb\ b\ env{\isacharparenright}{\isachardoublequoteclose}% |
|
8749 | 65 |
\begin{isamarkuptext}% |
66 |
\noindent |
|
27015 | 67 |
|
68 |
Both take an expression and an environment (a mapping from variables |
|
69 |
\isa{{\isacharprime}a} to values \isa{nat}) and return its arithmetic/boolean |
|
70 |
value. Since the datatypes are mutually recursive, so are functions |
|
71 |
that operate on them. Hence they need to be defined in a single |
|
72 |
\isacommand{primrec} section. Notice the \isakeyword{and} separating |
|
73 |
the declarations of \isa{evala} and \isa{evalb}. Their defining |
|
74 |
equations need not be split into two groups; |
|
75 |
the empty line is purely for readability. |
|
76 |
||
77 |
In the same fashion we also define two functions that perform substitution:% |
|
8749 | 78 |
\end{isamarkuptext}% |
17175 | 79 |
\isamarkuptrue% |
80 |
\isacommand{primrec}\isamarkupfalse% |
|
27015 | 81 |
\ substa\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharparenleft}{\isacharprime}a\ {\isasymRightarrow}\ {\isacharprime}b\ aexp{\isacharparenright}\ {\isasymRightarrow}\ {\isacharprime}a\ aexp\ {\isasymRightarrow}\ {\isacharprime}b\ aexp{\isachardoublequoteclose}\ \isakeyword{and}\isanewline |
82 |
\ \ \ \ \ \ \ \ \ substb\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharparenleft}{\isacharprime}a\ {\isasymRightarrow}\ {\isacharprime}b\ aexp{\isacharparenright}\ {\isasymRightarrow}\ {\isacharprime}a\ bexp\ {\isasymRightarrow}\ {\isacharprime}b\ bexp{\isachardoublequoteclose}\ \isakeyword{where}\isanewline |
|
83 |
{\isachardoublequoteopen}substa\ s\ {\isacharparenleft}IF\ b\ a{\isadigit{1}}\ a{\isadigit{2}}{\isacharparenright}\ {\isacharequal}\isanewline |
|
84 |
\ \ \ IF\ {\isacharparenleft}substb\ s\ b{\isacharparenright}\ {\isacharparenleft}substa\ s\ a{\isadigit{1}}{\isacharparenright}\ {\isacharparenleft}substa\ s\ a{\isadigit{2}}{\isacharparenright}{\isachardoublequoteclose}\ {\isacharbar}\isanewline |
|
85 |
{\isachardoublequoteopen}substa\ s\ {\isacharparenleft}Sum\ a{\isadigit{1}}\ a{\isadigit{2}}{\isacharparenright}\ {\isacharequal}\ Sum\ {\isacharparenleft}substa\ s\ a{\isadigit{1}}{\isacharparenright}\ {\isacharparenleft}substa\ s\ a{\isadigit{2}}{\isacharparenright}{\isachardoublequoteclose}\ {\isacharbar}\isanewline |
|
86 |
{\isachardoublequoteopen}substa\ s\ {\isacharparenleft}Diff\ a{\isadigit{1}}\ a{\isadigit{2}}{\isacharparenright}\ {\isacharequal}\ Diff\ {\isacharparenleft}substa\ s\ a{\isadigit{1}}{\isacharparenright}\ {\isacharparenleft}substa\ s\ a{\isadigit{2}}{\isacharparenright}{\isachardoublequoteclose}\ {\isacharbar}\isanewline |
|
87 |
{\isachardoublequoteopen}substa\ s\ {\isacharparenleft}Var\ v{\isacharparenright}\ {\isacharequal}\ s\ v{\isachardoublequoteclose}\ {\isacharbar}\isanewline |
|
88 |
{\isachardoublequoteopen}substa\ s\ {\isacharparenleft}Num\ n{\isacharparenright}\ {\isacharequal}\ Num\ n{\isachardoublequoteclose}\ {\isacharbar}\isanewline |
|
8749 | 89 |
\isanewline |
27015 | 90 |
{\isachardoublequoteopen}substb\ s\ {\isacharparenleft}Less\ a{\isadigit{1}}\ a{\isadigit{2}}{\isacharparenright}\ {\isacharequal}\ Less\ {\isacharparenleft}substa\ s\ a{\isadigit{1}}{\isacharparenright}\ {\isacharparenleft}substa\ s\ a{\isadigit{2}}{\isacharparenright}{\isachardoublequoteclose}\ {\isacharbar}\isanewline |
91 |
{\isachardoublequoteopen}substb\ s\ {\isacharparenleft}And\ b{\isadigit{1}}\ b{\isadigit{2}}{\isacharparenright}\ {\isacharequal}\ And\ {\isacharparenleft}substb\ s\ b{\isadigit{1}}{\isacharparenright}\ {\isacharparenleft}substb\ s\ b{\isadigit{2}}{\isacharparenright}{\isachardoublequoteclose}\ {\isacharbar}\isanewline |
|
92 |
{\isachardoublequoteopen}substb\ s\ {\isacharparenleft}Neg\ b{\isacharparenright}\ {\isacharequal}\ Neg\ {\isacharparenleft}substb\ s\ b{\isacharparenright}{\isachardoublequoteclose}% |
|
8749 | 93 |
\begin{isamarkuptext}% |
94 |
\noindent |
|
27015 | 95 |
Their first argument is a function mapping variables to expressions, the |
8749 | 96 |
substitution. It is applied to all variables in the second argument. As a |
9792 | 97 |
result, the type of variables in the expression may change from \isa{{\isacharprime}a} |
27015 | 98 |
to \isa{{\isacharprime}b}. Note that there are only arithmetic and no boolean variables. |
99 |
||
8749 | 100 |
Now we can prove a fundamental theorem about the interaction between |
101 |
evaluation and substitution: applying a substitution $s$ to an expression $a$ |
|
102 |
and evaluating the result in an environment $env$ yields the same result as |
|
103 |
evaluation $a$ in the environment that maps every variable $x$ to the value |
|
104 |
of $s(x)$ under $env$. If you try to prove this separately for arithmetic or |
|
105 |
boolean expressions (by induction), you find that you always need the other |
|
106 |
theorem in the induction step. Therefore you need to state and prove both |
|
107 |
theorems simultaneously:% |
|
108 |
\end{isamarkuptext}% |
|
17175 | 109 |
\isamarkuptrue% |
110 |
\isacommand{lemma}\isamarkupfalse% |
|
111 |
\ {\isachardoublequoteopen}evala\ {\isacharparenleft}substa\ s\ a{\isacharparenright}\ env\ {\isacharequal}\ evala\ a\ {\isacharparenleft}{\isasymlambda}x{\isachardot}\ evala\ {\isacharparenleft}s\ x{\isacharparenright}\ env{\isacharparenright}\ {\isasymand}\isanewline |
|
112 |
\ \ \ \ \ \ \ \ evalb\ {\isacharparenleft}substb\ s\ b{\isacharparenright}\ env\ {\isacharequal}\ evalb\ b\ {\isacharparenleft}{\isasymlambda}x{\isachardot}\ evala\ {\isacharparenleft}s\ x{\isacharparenright}\ env{\isacharparenright}{\isachardoublequoteclose}\isanewline |
|
17056 | 113 |
% |
114 |
\isadelimproof |
|
115 |
% |
|
116 |
\endisadelimproof |
|
117 |
% |
|
118 |
\isatagproof |
|
17175 | 119 |
\isacommand{apply}\isamarkupfalse% |
27144 | 120 |
{\isacharparenleft}induct{\isacharunderscore}tac\ a\ \isakeyword{and}\ b\ rule{\isacharcolon}\ aexp{\isacharunderscore}bexp{\isachardot}induct{\isacharparenright}% |
16069 | 121 |
\begin{isamarkuptxt}% |
27166
968a1450ae35
had to add rule: because induct_tac no longer works correctly
nipkow
parents:
27144
diff
changeset
|
122 |
\noindent Unfortunately, induction needs to be told explicitly which induction rule to use (via \isa{rule{\isacharcolon}}). |
16069 | 123 |
The resulting 8 goals (one for each constructor) are proved in one fell swoop:% |
124 |
\end{isamarkuptxt}% |
|
17175 | 125 |
\isamarkuptrue% |
126 |
\isacommand{apply}\isamarkupfalse% |
|
17181 | 127 |
\ simp{\isacharunderscore}all% |
17056 | 128 |
\endisatagproof |
129 |
{\isafoldproof}% |
|
130 |
% |
|
131 |
\isadelimproof |
|
132 |
% |
|
133 |
\endisadelimproof |
|
11866 | 134 |
% |
8749 | 135 |
\begin{isamarkuptext}% |
136 |
In general, given $n$ mutually recursive datatypes $\tau@1$, \dots, $\tau@n$, |
|
137 |
an inductive proof expects a goal of the form |
|
138 |
\[ P@1(x@1)\ \land \dots \land P@n(x@n) \] |
|
139 |
where each variable $x@i$ is of type $\tau@i$. Induction is started by |
|
9792 | 140 |
\begin{isabelle} |
27166
968a1450ae35
had to add rule: because induct_tac no longer works correctly
nipkow
parents:
27144
diff
changeset
|
141 |
\isacommand{apply}\isa{{\isacharparenleft}induct{\isacharunderscore}tac} $x@1$ \isacommand{and} \dots\ \isacommand{and} $x@n$ \isa{rule{\isacharcolon}} $\tau@1$\isa{{\isacharunderscore}}\dots\isa{{\isacharunderscore}}$\tau@n$\isa{{\isachardot}induct{\isacharparenright}} |
9792 | 142 |
\end{isabelle} |
8749 | 143 |
|
144 |
\begin{exercise} |
|
9792 | 145 |
Define a function \isa{norma} of type \isa{{\isacharprime}a\ aexp\ {\isasymRightarrow}\ {\isacharprime}a\ aexp} that |
8749 | 146 |
replaces \isa{IF}s with complex boolean conditions by nested |
11458 | 147 |
\isa{IF}s; it should eliminate the constructors |
148 |
\isa{And} and \isa{Neg}, leaving only \isa{Less}. |
|
149 |
Prove that \isa{norma} |
|
8749 | 150 |
preserves the value of an expression and that the result of \isa{norma} |
151 |
is really normal, i.e.\ no more \isa{And}s and \isa{Neg}s occur in |
|
12334 | 152 |
it. ({\em Hint:} proceed as in \S\ref{sec:boolex} and read the discussion |
153 |
of type annotations following lemma \isa{subst{\isacharunderscore}id} below). |
|
8749 | 154 |
\end{exercise}% |
155 |
\end{isamarkuptext}% |
|
17175 | 156 |
\isamarkuptrue% |
17056 | 157 |
% |
158 |
\isadelimproof |
|
159 |
% |
|
160 |
\endisadelimproof |
|
161 |
% |
|
162 |
\isatagproof |
|
163 |
% |
|
164 |
\endisatagproof |
|
165 |
{\isafoldproof}% |
|
166 |
% |
|
167 |
\isadelimproof |
|
168 |
% |
|
169 |
\endisadelimproof |
|
170 |
% |
|
171 |
\isadelimproof |
|
172 |
% |
|
173 |
\endisadelimproof |
|
174 |
% |
|
175 |
\isatagproof |
|
176 |
% |
|
177 |
\endisatagproof |
|
178 |
{\isafoldproof}% |
|
179 |
% |
|
180 |
\isadelimproof |
|
181 |
% |
|
182 |
\endisadelimproof |
|
183 |
% |
|
184 |
\isadelimtheory |
|
185 |
% |
|
186 |
\endisadelimtheory |
|
187 |
% |
|
188 |
\isatagtheory |
|
189 |
% |
|
190 |
\endisatagtheory |
|
191 |
{\isafoldtheory}% |
|
192 |
% |
|
193 |
\isadelimtheory |
|
194 |
% |
|
195 |
\endisadelimtheory |
|
9722 | 196 |
\end{isabellebody}% |
9145 | 197 |
%%% Local Variables: |
198 |
%%% mode: latex |
|
199 |
%%% TeX-master: "root" |
|
200 |
%%% End: |