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