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%
|
|
52 |
\isacommand{consts}\isamarkupfalse%
|
|
53 |
\ \ evala\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a\ aexp\ {\isasymRightarrow}\ {\isacharparenleft}{\isacharprime}a\ {\isasymRightarrow}\ nat{\isacharparenright}\ {\isasymRightarrow}\ nat{\isachardoublequoteclose}\isanewline
|
|
54 |
\ \ \ \ \ \ \ \ evalb\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a\ bexp\ {\isasymRightarrow}\ {\isacharparenleft}{\isacharprime}a\ {\isasymRightarrow}\ nat{\isacharparenright}\ {\isasymRightarrow}\ bool{\isachardoublequoteclose}%
|
8749
|
55 |
\begin{isamarkuptext}%
|
|
56 |
\noindent
|
11458
|
57 |
Both take an expression and an environment (a mapping from variables \isa{{\isacharprime}a} to values
|
8771
|
58 |
\isa{nat}) and return its arithmetic/boolean
|
8749
|
59 |
value. Since the datatypes are mutually recursive, so are functions that
|
|
60 |
operate on them. Hence they need to be defined in a single \isacommand{primrec}
|
|
61 |
section:%
|
|
62 |
\end{isamarkuptext}%
|
17175
|
63 |
\isamarkuptrue%
|
|
64 |
\isacommand{primrec}\isamarkupfalse%
|
8749
|
65 |
\isanewline
|
17175
|
66 |
\ \ {\isachardoublequoteopen}evala\ {\isacharparenleft}IF\ b\ a{\isadigit{1}}\ a{\isadigit{2}}{\isacharparenright}\ env\ {\isacharequal}\isanewline
|
|
67 |
\ \ \ \ \ {\isacharparenleft}if\ evalb\ b\ env\ then\ evala\ a{\isadigit{1}}\ env\ else\ evala\ a{\isadigit{2}}\ env{\isacharparenright}{\isachardoublequoteclose}\isanewline
|
|
68 |
\ \ {\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}\isanewline
|
|
69 |
\ \ {\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}\isanewline
|
|
70 |
\ \ {\isachardoublequoteopen}evala\ {\isacharparenleft}Var\ v{\isacharparenright}\ env\ {\isacharequal}\ env\ v{\isachardoublequoteclose}\isanewline
|
|
71 |
\ \ {\isachardoublequoteopen}evala\ {\isacharparenleft}Num\ n{\isacharparenright}\ env\ {\isacharequal}\ n{\isachardoublequoteclose}\isanewline
|
|
72 |
\isanewline
|
|
73 |
\ \ {\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}\isanewline
|
|
74 |
\ \ {\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}\isanewline
|
|
75 |
\ \ {\isachardoublequoteopen}evalb\ {\isacharparenleft}Neg\ b{\isacharparenright}\ env\ {\isacharequal}\ {\isacharparenleft}{\isasymnot}\ evalb\ b\ env{\isacharparenright}{\isachardoublequoteclose}%
|
8749
|
76 |
\begin{isamarkuptext}%
|
|
77 |
\noindent
|
|
78 |
In the same fashion we also define two functions that perform substitution:%
|
|
79 |
\end{isamarkuptext}%
|
17175
|
80 |
\isamarkuptrue%
|
|
81 |
\isacommand{consts}\isamarkupfalse%
|
|
82 |
\ substa\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharparenleft}{\isacharprime}a\ {\isasymRightarrow}\ {\isacharprime}b\ aexp{\isacharparenright}\ {\isasymRightarrow}\ {\isacharprime}a\ aexp\ {\isasymRightarrow}\ {\isacharprime}b\ aexp{\isachardoublequoteclose}\isanewline
|
|
83 |
\ \ \ \ \ \ \ substb\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharparenleft}{\isacharprime}a\ {\isasymRightarrow}\ {\isacharprime}b\ aexp{\isacharparenright}\ {\isasymRightarrow}\ {\isacharprime}a\ bexp\ {\isasymRightarrow}\ {\isacharprime}b\ bexp{\isachardoublequoteclose}%
|
8749
|
84 |
\begin{isamarkuptext}%
|
|
85 |
\noindent
|
|
86 |
The first argument is a function mapping variables to expressions, the
|
|
87 |
substitution. It is applied to all variables in the second argument. As a
|
9792
|
88 |
result, the type of variables in the expression may change from \isa{{\isacharprime}a}
|
|
89 |
to \isa{{\isacharprime}b}. Note that there are only arithmetic and no boolean variables.%
|
8749
|
90 |
\end{isamarkuptext}%
|
17175
|
91 |
\isamarkuptrue%
|
|
92 |
\isacommand{primrec}\isamarkupfalse%
|
8749
|
93 |
\isanewline
|
17175
|
94 |
\ \ {\isachardoublequoteopen}substa\ s\ {\isacharparenleft}IF\ b\ a{\isadigit{1}}\ a{\isadigit{2}}{\isacharparenright}\ {\isacharequal}\isanewline
|
|
95 |
\ \ \ \ \ IF\ {\isacharparenleft}substb\ s\ b{\isacharparenright}\ {\isacharparenleft}substa\ s\ a{\isadigit{1}}{\isacharparenright}\ {\isacharparenleft}substa\ s\ a{\isadigit{2}}{\isacharparenright}{\isachardoublequoteclose}\isanewline
|
|
96 |
\ \ {\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}\isanewline
|
|
97 |
\ \ {\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}\isanewline
|
|
98 |
\ \ {\isachardoublequoteopen}substa\ s\ {\isacharparenleft}Var\ v{\isacharparenright}\ {\isacharequal}\ s\ v{\isachardoublequoteclose}\isanewline
|
|
99 |
\ \ {\isachardoublequoteopen}substa\ s\ {\isacharparenleft}Num\ n{\isacharparenright}\ {\isacharequal}\ Num\ n{\isachardoublequoteclose}\isanewline
|
|
100 |
\isanewline
|
|
101 |
\ \ {\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}\isanewline
|
|
102 |
\ \ {\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}\isanewline
|
|
103 |
\ \ {\isachardoublequoteopen}substb\ s\ {\isacharparenleft}Neg\ b{\isacharparenright}\ {\isacharequal}\ Neg\ {\isacharparenleft}substb\ s\ b{\isacharparenright}{\isachardoublequoteclose}%
|
8749
|
104 |
\begin{isamarkuptext}%
|
|
105 |
Now we can prove a fundamental theorem about the interaction between
|
|
106 |
evaluation and substitution: applying a substitution $s$ to an expression $a$
|
|
107 |
and evaluating the result in an environment $env$ yields the same result as
|
|
108 |
evaluation $a$ in the environment that maps every variable $x$ to the value
|
|
109 |
of $s(x)$ under $env$. If you try to prove this separately for arithmetic or
|
|
110 |
boolean expressions (by induction), you find that you always need the other
|
|
111 |
theorem in the induction step. Therefore you need to state and prove both
|
|
112 |
theorems simultaneously:%
|
|
113 |
\end{isamarkuptext}%
|
17175
|
114 |
\isamarkuptrue%
|
|
115 |
\isacommand{lemma}\isamarkupfalse%
|
|
116 |
\ {\isachardoublequoteopen}evala\ {\isacharparenleft}substa\ s\ a{\isacharparenright}\ env\ {\isacharequal}\ evala\ a\ {\isacharparenleft}{\isasymlambda}x{\isachardot}\ evala\ {\isacharparenleft}s\ x{\isacharparenright}\ env{\isacharparenright}\ {\isasymand}\isanewline
|
|
117 |
\ \ \ \ \ \ \ \ evalb\ {\isacharparenleft}substb\ s\ b{\isacharparenright}\ env\ {\isacharequal}\ evalb\ b\ {\isacharparenleft}{\isasymlambda}x{\isachardot}\ evala\ {\isacharparenleft}s\ x{\isacharparenright}\ env{\isacharparenright}{\isachardoublequoteclose}\isanewline
|
17056
|
118 |
%
|
|
119 |
\isadelimproof
|
|
120 |
%
|
|
121 |
\endisadelimproof
|
|
122 |
%
|
|
123 |
\isatagproof
|
17175
|
124 |
\isacommand{apply}\isamarkupfalse%
|
|
125 |
{\isacharparenleft}induct{\isacharunderscore}tac\ a\ \isakeyword{and}\ b{\isacharparenright}%
|
16069
|
126 |
\begin{isamarkuptxt}%
|
|
127 |
\noindent
|
|
128 |
The resulting 8 goals (one for each constructor) are proved in one fell swoop:%
|
|
129 |
\end{isamarkuptxt}%
|
17175
|
130 |
\isamarkuptrue%
|
|
131 |
\isacommand{apply}\isamarkupfalse%
|
17181
|
132 |
\ simp{\isacharunderscore}all%
|
17056
|
133 |
\endisatagproof
|
|
134 |
{\isafoldproof}%
|
|
135 |
%
|
|
136 |
\isadelimproof
|
|
137 |
%
|
|
138 |
\endisadelimproof
|
11866
|
139 |
%
|
8749
|
140 |
\begin{isamarkuptext}%
|
|
141 |
In general, given $n$ mutually recursive datatypes $\tau@1$, \dots, $\tau@n$,
|
|
142 |
an inductive proof expects a goal of the form
|
|
143 |
\[ P@1(x@1)\ \land \dots \land P@n(x@n) \]
|
|
144 |
where each variable $x@i$ is of type $\tau@i$. Induction is started by
|
9792
|
145 |
\begin{isabelle}
|
10971
|
146 |
\isacommand{apply}\isa{{\isacharparenleft}induct{\isacharunderscore}tac} $x@1$ \isacommand{and} \dots\ \isacommand{and} $x@n$\isa{{\isacharparenright}}
|
9792
|
147 |
\end{isabelle}
|
8749
|
148 |
|
|
149 |
\begin{exercise}
|
9792
|
150 |
Define a function \isa{norma} of type \isa{{\isacharprime}a\ aexp\ {\isasymRightarrow}\ {\isacharprime}a\ aexp} that
|
8749
|
151 |
replaces \isa{IF}s with complex boolean conditions by nested
|
11458
|
152 |
\isa{IF}s; it should eliminate the constructors
|
|
153 |
\isa{And} and \isa{Neg}, leaving only \isa{Less}.
|
|
154 |
Prove that \isa{norma}
|
8749
|
155 |
preserves the value of an expression and that the result of \isa{norma}
|
|
156 |
is really normal, i.e.\ no more \isa{And}s and \isa{Neg}s occur in
|
12334
|
157 |
it. ({\em Hint:} proceed as in \S\ref{sec:boolex} and read the discussion
|
|
158 |
of type annotations following lemma \isa{subst{\isacharunderscore}id} below).
|
8749
|
159 |
\end{exercise}%
|
|
160 |
\end{isamarkuptext}%
|
17175
|
161 |
\isamarkuptrue%
|
17056
|
162 |
%
|
|
163 |
\isadelimproof
|
|
164 |
%
|
|
165 |
\endisadelimproof
|
|
166 |
%
|
|
167 |
\isatagproof
|
|
168 |
%
|
|
169 |
\endisatagproof
|
|
170 |
{\isafoldproof}%
|
|
171 |
%
|
|
172 |
\isadelimproof
|
|
173 |
%
|
|
174 |
\endisadelimproof
|
|
175 |
%
|
|
176 |
\isadelimproof
|
|
177 |
%
|
|
178 |
\endisadelimproof
|
|
179 |
%
|
|
180 |
\isatagproof
|
|
181 |
%
|
|
182 |
\endisatagproof
|
|
183 |
{\isafoldproof}%
|
|
184 |
%
|
|
185 |
\isadelimproof
|
|
186 |
%
|
|
187 |
\endisadelimproof
|
|
188 |
%
|
|
189 |
\isadelimtheory
|
|
190 |
%
|
|
191 |
\endisadelimtheory
|
|
192 |
%
|
|
193 |
\isatagtheory
|
|
194 |
%
|
|
195 |
\endisatagtheory
|
|
196 |
{\isafoldtheory}%
|
|
197 |
%
|
|
198 |
\isadelimtheory
|
|
199 |
%
|
|
200 |
\endisadelimtheory
|
9722
|
201 |
\end{isabellebody}%
|
9145
|
202 |
%%% Local Variables:
|
|
203 |
%%% mode: latex
|
|
204 |
%%% TeX-master: "root"
|
|
205 |
%%% End:
|