9722
|
1 |
%
|
|
2 |
\begin{isabellebody}%
|
9924
|
3 |
\def\isabellecontext{Ifexpr}%
|
8749
|
4 |
%
|
9933
|
5 |
\isamarkupsubsection{Case study: boolean expressions}
|
|
6 |
%
|
8749
|
7 |
\begin{isamarkuptext}%
|
9933
|
8 |
\label{sec:boolex}
|
|
9 |
The aim of this case study is twofold: it shows how to model boolean
|
|
10 |
expressions and some algorithms for manipulating them, and it demonstrates
|
|
11 |
the constructs introduced above.%
|
|
12 |
\end{isamarkuptext}%
|
|
13 |
%
|
|
14 |
\isamarkupsubsubsection{How can we model boolean expressions?}
|
|
15 |
%
|
|
16 |
\begin{isamarkuptext}%
|
8749
|
17 |
We want to represent boolean expressions built up from variables and
|
|
18 |
constants by negation and conjunction. The following datatype serves exactly
|
|
19 |
that purpose:%
|
|
20 |
\end{isamarkuptext}%
|
9673
|
21 |
\isacommand{datatype}\ boolex\ {\isacharequal}\ Const\ bool\ {\isacharbar}\ Var\ nat\ {\isacharbar}\ Neg\ boolex\isanewline
|
|
22 |
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ {\isacharbar}\ And\ boolex\ boolex%
|
8749
|
23 |
\begin{isamarkuptext}%
|
|
24 |
\noindent
|
9541
|
25 |
The two constants are represented by \isa{Const\ True} and
|
|
26 |
\isa{Const\ False}. Variables are represented by terms of the form
|
9792
|
27 |
\isa{Var\ n}, where \isa{n} is a natural number (type \isa{nat}).
|
8749
|
28 |
For example, the formula $P@0 \land \neg P@1$ is represented by the term
|
10187
|
29 |
\isa{And\ {\isacharparenleft}Var\ {\isadigit{0}}{\isacharparenright}\ {\isacharparenleft}Neg\ {\isacharparenleft}Var\ {\isadigit{1}}{\isacharparenright}{\isacharparenright}}.
|
8749
|
30 |
|
|
31 |
\subsubsection{What is the value of a boolean expression?}
|
|
32 |
|
|
33 |
The value of a boolean expression depends on the value of its variables.
|
9792
|
34 |
Hence the function \isa{value} takes an additional parameter, an
|
|
35 |
\emph{environment} of type \isa{nat\ {\isasymRightarrow}\ bool}, which maps variables to their
|
|
36 |
values:%
|
8749
|
37 |
\end{isamarkuptext}%
|
9673
|
38 |
\isacommand{consts}\ value\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}boolex\ {\isasymRightarrow}\ {\isacharparenleft}nat\ {\isasymRightarrow}\ bool{\isacharparenright}\ {\isasymRightarrow}\ bool{\isachardoublequote}\isanewline
|
8749
|
39 |
\isacommand{primrec}\isanewline
|
9673
|
40 |
{\isachardoublequote}value\ {\isacharparenleft}Const\ b{\isacharparenright}\ env\ {\isacharequal}\ b{\isachardoublequote}\isanewline
|
|
41 |
{\isachardoublequote}value\ {\isacharparenleft}Var\ x{\isacharparenright}\ \ \ env\ {\isacharequal}\ env\ x{\isachardoublequote}\isanewline
|
|
42 |
{\isachardoublequote}value\ {\isacharparenleft}Neg\ b{\isacharparenright}\ \ \ env\ {\isacharequal}\ {\isacharparenleft}{\isasymnot}\ value\ b\ env{\isacharparenright}{\isachardoublequote}\isanewline
|
|
43 |
{\isachardoublequote}value\ {\isacharparenleft}And\ b\ c{\isacharparenright}\ env\ {\isacharequal}\ {\isacharparenleft}value\ b\ env\ {\isasymand}\ value\ c\ env{\isacharparenright}{\isachardoublequote}%
|
8749
|
44 |
\begin{isamarkuptext}%
|
|
45 |
\noindent
|
|
46 |
\subsubsection{If-expressions}
|
|
47 |
|
|
48 |
An alternative and often more efficient (because in a certain sense
|
|
49 |
canonical) representation are so-called \emph{If-expressions} built up
|
|
50 |
from constants (\isa{CIF}), variables (\isa{VIF}) and conditionals
|
|
51 |
(\isa{IF}):%
|
|
52 |
\end{isamarkuptext}%
|
9673
|
53 |
\isacommand{datatype}\ ifex\ {\isacharequal}\ CIF\ bool\ {\isacharbar}\ VIF\ nat\ {\isacharbar}\ IF\ ifex\ ifex\ ifex%
|
8749
|
54 |
\begin{isamarkuptext}%
|
|
55 |
\noindent
|
|
56 |
The evaluation if If-expressions proceeds as for \isa{boolex}:%
|
|
57 |
\end{isamarkuptext}%
|
9673
|
58 |
\isacommand{consts}\ valif\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}ifex\ {\isasymRightarrow}\ {\isacharparenleft}nat\ {\isasymRightarrow}\ bool{\isacharparenright}\ {\isasymRightarrow}\ bool{\isachardoublequote}\isanewline
|
8749
|
59 |
\isacommand{primrec}\isanewline
|
9673
|
60 |
{\isachardoublequote}valif\ {\isacharparenleft}CIF\ b{\isacharparenright}\ \ \ \ env\ {\isacharequal}\ b{\isachardoublequote}\isanewline
|
|
61 |
{\isachardoublequote}valif\ {\isacharparenleft}VIF\ x{\isacharparenright}\ \ \ \ env\ {\isacharequal}\ env\ x{\isachardoublequote}\isanewline
|
|
62 |
{\isachardoublequote}valif\ {\isacharparenleft}IF\ b\ t\ e{\isacharparenright}\ env\ {\isacharequal}\ {\isacharparenleft}if\ valif\ b\ env\ then\ valif\ t\ env\isanewline
|
|
63 |
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ else\ valif\ e\ env{\isacharparenright}{\isachardoublequote}%
|
8749
|
64 |
\begin{isamarkuptext}%
|
|
65 |
\subsubsection{Transformation into and of If-expressions}
|
|
66 |
|
|
67 |
The type \isa{boolex} is close to the customary representation of logical
|
8771
|
68 |
formulae, whereas \isa{ifex} is designed for efficiency. It is easy to
|
8749
|
69 |
translate from \isa{boolex} into \isa{ifex}:%
|
|
70 |
\end{isamarkuptext}%
|
10187
|
71 |
\isacommand{consts}\ bool{\isadigit{2}}if\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}boolex\ {\isasymRightarrow}\ ifex{\isachardoublequote}\isanewline
|
8749
|
72 |
\isacommand{primrec}\isanewline
|
10187
|
73 |
{\isachardoublequote}bool{\isadigit{2}}if\ {\isacharparenleft}Const\ b{\isacharparenright}\ {\isacharequal}\ CIF\ b{\isachardoublequote}\isanewline
|
|
74 |
{\isachardoublequote}bool{\isadigit{2}}if\ {\isacharparenleft}Var\ x{\isacharparenright}\ \ \ {\isacharequal}\ VIF\ x{\isachardoublequote}\isanewline
|
|
75 |
{\isachardoublequote}bool{\isadigit{2}}if\ {\isacharparenleft}Neg\ b{\isacharparenright}\ \ \ {\isacharequal}\ IF\ {\isacharparenleft}bool{\isadigit{2}}if\ b{\isacharparenright}\ {\isacharparenleft}CIF\ False{\isacharparenright}\ {\isacharparenleft}CIF\ True{\isacharparenright}{\isachardoublequote}\isanewline
|
|
76 |
{\isachardoublequote}bool{\isadigit{2}}if\ {\isacharparenleft}And\ b\ c{\isacharparenright}\ {\isacharequal}\ IF\ {\isacharparenleft}bool{\isadigit{2}}if\ b{\isacharparenright}\ {\isacharparenleft}bool{\isadigit{2}}if\ c{\isacharparenright}\ {\isacharparenleft}CIF\ False{\isacharparenright}{\isachardoublequote}%
|
8749
|
77 |
\begin{isamarkuptext}%
|
|
78 |
\noindent
|
10187
|
79 |
At last, we have something we can verify: that \isa{bool{\isadigit{2}}if} preserves the
|
8749
|
80 |
value of its argument:%
|
|
81 |
\end{isamarkuptext}%
|
10187
|
82 |
\isacommand{lemma}\ {\isachardoublequote}valif\ {\isacharparenleft}bool{\isadigit{2}}if\ b{\isacharparenright}\ env\ {\isacharequal}\ value\ b\ env{\isachardoublequote}%
|
8749
|
83 |
\begin{isamarkuptxt}%
|
|
84 |
\noindent
|
|
85 |
The proof is canonical:%
|
|
86 |
\end{isamarkuptxt}%
|
9673
|
87 |
\isacommand{apply}{\isacharparenleft}induct{\isacharunderscore}tac\ b{\isacharparenright}\isanewline
|
10171
|
88 |
\isacommand{apply}{\isacharparenleft}auto{\isacharparenright}\isanewline
|
|
89 |
\isacommand{done}%
|
8749
|
90 |
\begin{isamarkuptext}%
|
|
91 |
\noindent
|
|
92 |
In fact, all proofs in this case study look exactly like this. Hence we do
|
|
93 |
not show them below.
|
|
94 |
|
|
95 |
More interesting is the transformation of If-expressions into a normal form
|
|
96 |
where the first argument of \isa{IF} cannot be another \isa{IF} but
|
|
97 |
must be a constant or variable. Such a normal form can be computed by
|
9792
|
98 |
repeatedly replacing a subterm of the form \isa{IF\ {\isacharparenleft}IF\ b\ x\ y{\isacharparenright}\ z\ u} by
|
|
99 |
\isa{IF\ b\ {\isacharparenleft}IF\ x\ z\ u{\isacharparenright}\ {\isacharparenleft}IF\ y\ z\ u{\isacharparenright}}, which has the same value. The following
|
8749
|
100 |
primitive recursive functions perform this task:%
|
|
101 |
\end{isamarkuptext}%
|
9673
|
102 |
\isacommand{consts}\ normif\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}ifex\ {\isasymRightarrow}\ ifex\ {\isasymRightarrow}\ ifex\ {\isasymRightarrow}\ ifex{\isachardoublequote}\isanewline
|
8749
|
103 |
\isacommand{primrec}\isanewline
|
9673
|
104 |
{\isachardoublequote}normif\ {\isacharparenleft}CIF\ b{\isacharparenright}\ \ \ \ t\ e\ {\isacharequal}\ IF\ {\isacharparenleft}CIF\ b{\isacharparenright}\ t\ e{\isachardoublequote}\isanewline
|
|
105 |
{\isachardoublequote}normif\ {\isacharparenleft}VIF\ x{\isacharparenright}\ \ \ \ t\ e\ {\isacharequal}\ IF\ {\isacharparenleft}VIF\ x{\isacharparenright}\ t\ e{\isachardoublequote}\isanewline
|
|
106 |
{\isachardoublequote}normif\ {\isacharparenleft}IF\ b\ t\ e{\isacharparenright}\ u\ f\ {\isacharequal}\ normif\ b\ {\isacharparenleft}normif\ t\ u\ f{\isacharparenright}\ {\isacharparenleft}normif\ e\ u\ f{\isacharparenright}{\isachardoublequote}\isanewline
|
8749
|
107 |
\isanewline
|
9673
|
108 |
\isacommand{consts}\ norm\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}ifex\ {\isasymRightarrow}\ ifex{\isachardoublequote}\isanewline
|
8749
|
109 |
\isacommand{primrec}\isanewline
|
9673
|
110 |
{\isachardoublequote}norm\ {\isacharparenleft}CIF\ b{\isacharparenright}\ \ \ \ {\isacharequal}\ CIF\ b{\isachardoublequote}\isanewline
|
|
111 |
{\isachardoublequote}norm\ {\isacharparenleft}VIF\ x{\isacharparenright}\ \ \ \ {\isacharequal}\ VIF\ x{\isachardoublequote}\isanewline
|
|
112 |
{\isachardoublequote}norm\ {\isacharparenleft}IF\ b\ t\ e{\isacharparenright}\ {\isacharequal}\ normif\ b\ {\isacharparenleft}norm\ t{\isacharparenright}\ {\isacharparenleft}norm\ e{\isacharparenright}{\isachardoublequote}%
|
8749
|
113 |
\begin{isamarkuptext}%
|
|
114 |
\noindent
|
|
115 |
Their interplay is a bit tricky, and we leave it to the reader to develop an
|
|
116 |
intuitive understanding. Fortunately, Isabelle can help us to verify that the
|
|
117 |
transformation preserves the value of the expression:%
|
|
118 |
\end{isamarkuptext}%
|
9673
|
119 |
\isacommand{theorem}\ {\isachardoublequote}valif\ {\isacharparenleft}norm\ b{\isacharparenright}\ env\ {\isacharequal}\ valif\ b\ env{\isachardoublequote}%
|
8749
|
120 |
\begin{isamarkuptext}%
|
|
121 |
\noindent
|
|
122 |
The proof is canonical, provided we first show the following simplification
|
|
123 |
lemma (which also helps to understand what \isa{normif} does):%
|
|
124 |
\end{isamarkuptext}%
|
9673
|
125 |
\isacommand{lemma}\ {\isacharbrackleft}simp{\isacharbrackright}{\isacharcolon}\isanewline
|
|
126 |
\ \ {\isachardoublequote}{\isasymforall}t\ e{\isachardot}\ valif\ {\isacharparenleft}normif\ b\ t\ e{\isacharparenright}\ env\ {\isacharequal}\ valif\ {\isacharparenleft}IF\ b\ t\ e{\isacharparenright}\ env{\isachardoublequote}%
|
8749
|
127 |
\begin{isamarkuptext}%
|
|
128 |
\noindent
|
|
129 |
Note that the lemma does not have a name, but is implicitly used in the proof
|
9792
|
130 |
of the theorem shown above because of the \isa{{\isacharbrackleft}simp{\isacharbrackright}} attribute.
|
8749
|
131 |
|
|
132 |
But how can we be sure that \isa{norm} really produces a normal form in
|
|
133 |
the above sense? We define a function that tests If-expressions for normality%
|
|
134 |
\end{isamarkuptext}%
|
9673
|
135 |
\isacommand{consts}\ normal\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}ifex\ {\isasymRightarrow}\ bool{\isachardoublequote}\isanewline
|
8749
|
136 |
\isacommand{primrec}\isanewline
|
9673
|
137 |
{\isachardoublequote}normal{\isacharparenleft}CIF\ b{\isacharparenright}\ {\isacharequal}\ True{\isachardoublequote}\isanewline
|
|
138 |
{\isachardoublequote}normal{\isacharparenleft}VIF\ x{\isacharparenright}\ {\isacharequal}\ True{\isachardoublequote}\isanewline
|
|
139 |
{\isachardoublequote}normal{\isacharparenleft}IF\ b\ t\ e{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}normal\ t\ {\isasymand}\ normal\ e\ {\isasymand}\isanewline
|
|
140 |
\ \ \ \ \ {\isacharparenleft}case\ b\ of\ CIF\ b\ {\isasymRightarrow}\ True\ {\isacharbar}\ VIF\ x\ {\isasymRightarrow}\ True\ {\isacharbar}\ IF\ x\ y\ z\ {\isasymRightarrow}\ False{\isacharparenright}{\isacharparenright}{\isachardoublequote}%
|
8749
|
141 |
\begin{isamarkuptext}%
|
|
142 |
\noindent
|
9792
|
143 |
and prove \isa{normal\ {\isacharparenleft}norm\ b{\isacharparenright}}. Of course, this requires a lemma about
|
8749
|
144 |
normality of \isa{normif}:%
|
|
145 |
\end{isamarkuptext}%
|
9933
|
146 |
\isacommand{lemma}{\isacharbrackleft}simp{\isacharbrackright}{\isacharcolon}\ {\isachardoublequote}{\isasymforall}t\ e{\isachardot}\ normal{\isacharparenleft}normif\ b\ t\ e{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}normal\ t\ {\isasymand}\ normal\ e{\isacharparenright}{\isachardoublequote}%
|
|
147 |
\begin{isamarkuptext}%
|
|
148 |
\medskip
|
|
149 |
How does one come up with the required lemmas? Try to prove the main theorems
|
|
150 |
without them and study carefully what \isa{auto} leaves unproved. This has
|
|
151 |
to provide the clue. The necessity of universal quantification
|
|
152 |
(\isa{{\isasymforall}t\ e}) in the two lemmas is explained in
|
|
153 |
\S\ref{sec:InductionHeuristics}
|
|
154 |
|
|
155 |
\begin{exercise}
|
|
156 |
We strengthen the definition of a \isa{normal} If-expression as follows:
|
|
157 |
the first argument of all \isa{IF}s must be a variable. Adapt the above
|
|
158 |
development to this changed requirement. (Hint: you may need to formulate
|
|
159 |
some of the goals as implications (\isa{{\isasymlongrightarrow}}) rather than
|
|
160 |
equalities (\isa{{\isacharequal}}).)
|
|
161 |
\end{exercise}%
|
|
162 |
\end{isamarkuptext}%
|
|
163 |
\end{isabellebody}%
|
9145
|
164 |
%%% Local Variables:
|
|
165 |
%%% mode: latex
|
|
166 |
%%% TeX-master: "root"
|
|
167 |
%%% End:
|