8749
|
1 |
\begin{isabelle}%
|
|
2 |
%
|
|
3 |
\begin{isamarkuptext}%
|
|
4 |
Sometimes it is necessary to define two datatypes that depend on each
|
|
5 |
other. This is called \textbf{mutual recursion}. As an example consider a
|
|
6 |
language of arithmetic and boolean expressions where
|
|
7 |
\begin{itemize}
|
|
8 |
\item arithmetic expressions contain boolean expressions because there are
|
|
9 |
conditional arithmetic expressions like ``if $m<n$ then $n-m$ else $m-n$'',
|
|
10 |
and
|
|
11 |
\item boolean expressions contain arithmetic expressions because of
|
|
12 |
comparisons like ``$m<n$''.
|
|
13 |
\end{itemize}
|
|
14 |
In Isabelle this becomes%
|
|
15 |
\end{isamarkuptext}%
|
9541
|
16 |
\isacommand{datatype}\ 'a\ aexp\ =\ IF\ \ \ {"}'a\ bexp{"}\ {"}'a\ aexp{"}\ {"}'a\ aexp{"}\isanewline
|
|
17 |
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ |\ Sum\ \ {"}'a\ aexp{"}\ {"}'a\ aexp{"}\isanewline
|
|
18 |
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ |\ Diff\ {"}'a\ aexp{"}\ {"}'a\ aexp{"}\isanewline
|
|
19 |
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ |\ Var\ 'a\isanewline
|
|
20 |
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ |\ Num\ nat\isanewline
|
|
21 |
\isakeyword{and}\ \ \ \ \ \ 'a\ bexp\ =\ Less\ {"}'a\ aexp{"}\ {"}'a\ aexp{"}\isanewline
|
|
22 |
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ |\ And\ \ {"}'a\ bexp{"}\ {"}'a\ bexp{"}\isanewline
|
|
23 |
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ |\ Neg\ \ {"}'a\ bexp{"}%
|
8749
|
24 |
\begin{isamarkuptext}%
|
|
25 |
\noindent
|
|
26 |
Type \isa{aexp} is similar to \isa{expr} in \S\ref{sec:ExprCompiler},
|
|
27 |
except that we have fixed the values to be of type \isa{nat} and that we
|
|
28 |
have fixed the two binary operations \isa{Sum} and \isa{Diff}. Boolean
|
|
29 |
expressions can be arithmetic comparisons, conjunctions and negations.
|
|
30 |
The semantics is fixed via two evaluation functions%
|
|
31 |
\end{isamarkuptext}%
|
9541
|
32 |
\isacommand{consts}\ \ evala\ ::\ {"}'a\ aexp\ {\isasymRightarrow}\ ('a\ {\isasymRightarrow}\ nat)\ {\isasymRightarrow}\ nat{"}\isanewline
|
|
33 |
\ \ \ \ \ \ \ \ evalb\ ::\ {"}'a\ bexp\ {\isasymRightarrow}\ ('a\ {\isasymRightarrow}\ nat)\ {\isasymRightarrow}\ bool{"}%
|
8749
|
34 |
\begin{isamarkuptext}%
|
|
35 |
\noindent
|
8771
|
36 |
that take an expression and an environment (a mapping from variables \isa{'a} to values
|
|
37 |
\isa{nat}) and return its arithmetic/boolean
|
8749
|
38 |
value. Since the datatypes are mutually recursive, so are functions that
|
|
39 |
operate on them. Hence they need to be defined in a single \isacommand{primrec}
|
|
40 |
section:%
|
|
41 |
\end{isamarkuptext}%
|
|
42 |
\isacommand{primrec}\isanewline
|
9541
|
43 |
\ \ {"}evala\ (IF\ b\ a1\ a2)\ env\ =\isanewline
|
|
44 |
\ \ \ \ \ (if\ evalb\ b\ env\ then\ evala\ a1\ env\ else\ evala\ a2\ env){"}\isanewline
|
|
45 |
\ \ {"}evala\ (Sum\ a1\ a2)\ env\ =\ evala\ a1\ env\ +\ evala\ a2\ env{"}\isanewline
|
|
46 |
\ \ {"}evala\ (Diff\ a1\ a2)\ env\ =\ evala\ a1\ env\ -\ evala\ a2\ env{"}\isanewline
|
|
47 |
\ \ {"}evala\ (Var\ v)\ env\ =\ env\ v{"}\isanewline
|
|
48 |
\ \ {"}evala\ (Num\ n)\ env\ =\ n{"}\isanewline
|
8749
|
49 |
\isanewline
|
9541
|
50 |
\ \ {"}evalb\ (Less\ a1\ a2)\ env\ =\ (evala\ a1\ env\ <\ evala\ a2\ env){"}\isanewline
|
|
51 |
\ \ {"}evalb\ (And\ b1\ b2)\ env\ =\ (evalb\ b1\ env\ {\isasymand}\ evalb\ b2\ env){"}\isanewline
|
|
52 |
\ \ {"}evalb\ (Neg\ b)\ env\ =\ ({\isasymnot}\ evalb\ b\ env){"}%
|
8749
|
53 |
\begin{isamarkuptext}%
|
|
54 |
\noindent
|
|
55 |
In the same fashion we also define two functions that perform substitution:%
|
|
56 |
\end{isamarkuptext}%
|
9541
|
57 |
\isacommand{consts}\ substa\ ::\ {"}('a\ {\isasymRightarrow}\ 'b\ aexp)\ {\isasymRightarrow}\ 'a\ aexp\ {\isasymRightarrow}\ 'b\ aexp{"}\isanewline
|
|
58 |
\ \ \ \ \ \ \ substb\ ::\ {"}('a\ {\isasymRightarrow}\ 'b\ aexp)\ {\isasymRightarrow}\ 'a\ bexp\ {\isasymRightarrow}\ 'b\ bexp{"}%
|
8749
|
59 |
\begin{isamarkuptext}%
|
|
60 |
\noindent
|
|
61 |
The first argument is a function mapping variables to expressions, the
|
|
62 |
substitution. It is applied to all variables in the second argument. As a
|
|
63 |
result, the type of variables in the expression may change from \isa{'a}
|
|
64 |
to \isa{'b}. Note that there are only arithmetic and no boolean variables.%
|
|
65 |
\end{isamarkuptext}%
|
|
66 |
\isacommand{primrec}\isanewline
|
9541
|
67 |
\ \ {"}substa\ s\ (IF\ b\ a1\ a2)\ =\isanewline
|
|
68 |
\ \ \ \ \ IF\ (substb\ s\ b)\ (substa\ s\ a1)\ (substa\ s\ a2){"}\isanewline
|
|
69 |
\ \ {"}substa\ s\ (Sum\ a1\ a2)\ =\ Sum\ (substa\ s\ a1)\ (substa\ s\ a2){"}\isanewline
|
|
70 |
\ \ {"}substa\ s\ (Diff\ a1\ a2)\ =\ Diff\ (substa\ s\ a1)\ (substa\ s\ a2){"}\isanewline
|
|
71 |
\ \ {"}substa\ s\ (Var\ v)\ =\ s\ v{"}\isanewline
|
|
72 |
\ \ {"}substa\ s\ (Num\ n)\ =\ Num\ n{"}\isanewline
|
8749
|
73 |
\isanewline
|
9541
|
74 |
\ \ {"}substb\ s\ (Less\ a1\ a2)\ =\ Less\ (substa\ s\ a1)\ (substa\ s\ a2){"}\isanewline
|
|
75 |
\ \ {"}substb\ s\ (And\ b1\ b2)\ =\ And\ (substb\ s\ b1)\ (substb\ s\ b2){"}\isanewline
|
|
76 |
\ \ {"}substb\ s\ (Neg\ b)\ =\ Neg\ (substb\ s\ b){"}%
|
8749
|
77 |
\begin{isamarkuptext}%
|
|
78 |
Now we can prove a fundamental theorem about the interaction between
|
|
79 |
evaluation and substitution: applying a substitution $s$ to an expression $a$
|
|
80 |
and evaluating the result in an environment $env$ yields the same result as
|
|
81 |
evaluation $a$ in the environment that maps every variable $x$ to the value
|
|
82 |
of $s(x)$ under $env$. If you try to prove this separately for arithmetic or
|
|
83 |
boolean expressions (by induction), you find that you always need the other
|
|
84 |
theorem in the induction step. Therefore you need to state and prove both
|
|
85 |
theorems simultaneously:%
|
|
86 |
\end{isamarkuptext}%
|
9541
|
87 |
\isacommand{lemma}\ {"}evala\ (substa\ s\ a)\ env\ =\ evala\ a\ ({\isasymlambda}x.\ evala\ (s\ x)\ env)\ {\isasymand}\isanewline
|
|
88 |
\ \ \ \ \ \ \ \ evalb\ (substb\ s\ b)\ env\ =\ evalb\ b\ ({\isasymlambda}x.\ evala\ (s\ x)\ env){"}\isanewline
|
|
89 |
\isacommand{apply}(induct\_tac\ a\ \isakeyword{and}\ b)%
|
8749
|
90 |
\begin{isamarkuptxt}%
|
|
91 |
\noindent
|
|
92 |
The resulting 8 goals (one for each constructor) are proved in one fell swoop:%
|
|
93 |
\end{isamarkuptxt}%
|
9541
|
94 |
\isacommand{by}\ auto%
|
8749
|
95 |
\begin{isamarkuptext}%
|
|
96 |
In general, given $n$ mutually recursive datatypes $\tau@1$, \dots, $\tau@n$,
|
|
97 |
an inductive proof expects a goal of the form
|
|
98 |
\[ P@1(x@1)\ \land \dots \land P@n(x@n) \]
|
|
99 |
where each variable $x@i$ is of type $\tau@i$. Induction is started by
|
|
100 |
\begin{ttbox}
|
|
101 |
apply(induct_tac \(x@1\) \texttt{and} \(\dots\) \texttt{and} \(x@n\));
|
|
102 |
\end{ttbox}
|
|
103 |
|
|
104 |
\begin{exercise}
|
9541
|
105 |
Define a function \isa{norma} of type \isa{'a\ aexp\ {\isasymRightarrow}\ 'a\ aexp} that
|
8749
|
106 |
replaces \isa{IF}s with complex boolean conditions by nested
|
|
107 |
\isa{IF}s where each condition is a \isa{Less} --- \isa{And} and
|
|
108 |
\isa{Neg} should be eliminated completely. Prove that \isa{norma}
|
|
109 |
preserves the value of an expression and that the result of \isa{norma}
|
|
110 |
is really normal, i.e.\ no more \isa{And}s and \isa{Neg}s occur in
|
|
111 |
it. ({\em Hint:} proceed as in \S\ref{sec:boolex}).
|
|
112 |
\end{exercise}%
|
|
113 |
\end{isamarkuptext}%
|
|
114 |
\end{isabelle}%
|
9145
|
115 |
%%% Local Variables:
|
|
116 |
%%% mode: latex
|
|
117 |
%%% TeX-master: "root"
|
|
118 |
%%% End:
|