8745
|
1 |
(*<*)
|
58860
|
2 |
theory Ifexpr imports Main begin
|
8745
|
3 |
(*>*)
|
|
4 |
|
67406
|
5 |
subsection\<open>Case Study: Boolean Expressions\<close>
|
9933
|
6 |
|
67406
|
7 |
text\<open>\label{sec:boolex}\index{boolean expressions example|(}
|
9933
|
8 |
The aim of this case study is twofold: it shows how to model boolean
|
|
9 |
expressions and some algorithms for manipulating them, and it demonstrates
|
|
10 |
the constructs introduced above.
|
67406
|
11 |
\<close>
|
9933
|
12 |
|
67406
|
13 |
subsubsection\<open>Modelling Boolean Expressions\<close>
|
9933
|
14 |
|
67406
|
15 |
text\<open>
|
8745
|
16 |
We want to represent boolean expressions built up from variables and
|
|
17 |
constants by negation and conjunction. The following datatype serves exactly
|
|
18 |
that purpose:
|
67406
|
19 |
\<close>
|
8745
|
20 |
|
|
21 |
datatype boolex = Const bool | Var nat | Neg boolex
|
58860
|
22 |
| And boolex boolex
|
8745
|
23 |
|
67406
|
24 |
text\<open>\noindent
|
69597
|
25 |
The two constants are represented by \<^term>\<open>Const True\<close> and
|
|
26 |
\<^term>\<open>Const False\<close>. Variables are represented by terms of the form
|
|
27 |
\<^term>\<open>Var n\<close>, where \<^term>\<open>n\<close> is a natural number (type \<^typ>\<open>nat\<close>).
|
8745
|
28 |
For example, the formula $P@0 \land \neg P@1$ is represented by the term
|
69597
|
29 |
\<^term>\<open>And (Var 0) (Neg(Var 1))\<close>.
|
8745
|
30 |
|
10978
|
31 |
\subsubsection{The Value of a Boolean Expression}
|
8745
|
32 |
|
|
33 |
The value of a boolean expression depends on the value of its variables.
|
69505
|
34 |
Hence the function \<open>value\<close> takes an additional parameter, an
|
69597
|
35 |
\emph{environment} of type \<^typ>\<open>nat => bool\<close>, which maps variables to their
|
9792
|
36 |
values:
|
67406
|
37 |
\<close>
|
8745
|
38 |
|
27015
|
39 |
primrec "value" :: "boolex \<Rightarrow> (nat \<Rightarrow> bool) \<Rightarrow> bool" where
|
|
40 |
"value (Const b) env = b" |
|
|
41 |
"value (Var x) env = env x" |
|
|
42 |
"value (Neg b) env = (\<not> value b env)" |
|
|
43 |
"value (And b c) env = (value b env \<and> value c env)"
|
8745
|
44 |
|
67406
|
45 |
text\<open>\noindent
|
10885
|
46 |
\subsubsection{If-Expressions}
|
8745
|
47 |
|
|
48 |
An alternative and often more efficient (because in a certain sense
|
|
49 |
canonical) representation are so-called \emph{If-expressions} built up
|
69597
|
50 |
from constants (\<^term>\<open>CIF\<close>), variables (\<^term>\<open>VIF\<close>) and conditionals
|
|
51 |
(\<^term>\<open>IF\<close>):
|
67406
|
52 |
\<close>
|
8745
|
53 |
|
58860
|
54 |
datatype ifex = CIF bool | VIF nat | IF ifex ifex ifex
|
8745
|
55 |
|
67406
|
56 |
text\<open>\noindent
|
69597
|
57 |
The evaluation of If-expressions proceeds as for \<^typ>\<open>boolex\<close>:
|
67406
|
58 |
\<close>
|
8745
|
59 |
|
27015
|
60 |
primrec valif :: "ifex \<Rightarrow> (nat \<Rightarrow> bool) \<Rightarrow> bool" where
|
|
61 |
"valif (CIF b) env = b" |
|
|
62 |
"valif (VIF x) env = env x" |
|
8745
|
63 |
"valif (IF b t e) env = (if valif b env then valif t env
|
27015
|
64 |
else valif e env)"
|
8745
|
65 |
|
67406
|
66 |
text\<open>
|
10978
|
67 |
\subsubsection{Converting Boolean and If-Expressions}
|
8745
|
68 |
|
69597
|
69 |
The type \<^typ>\<open>boolex\<close> is close to the customary representation of logical
|
|
70 |
formulae, whereas \<^typ>\<open>ifex\<close> is designed for efficiency. It is easy to
|
|
71 |
translate from \<^typ>\<open>boolex\<close> into \<^typ>\<open>ifex\<close>:
|
67406
|
72 |
\<close>
|
8745
|
73 |
|
27015
|
74 |
primrec bool2if :: "boolex \<Rightarrow> ifex" where
|
|
75 |
"bool2if (Const b) = CIF b" |
|
|
76 |
"bool2if (Var x) = VIF x" |
|
|
77 |
"bool2if (Neg b) = IF (bool2if b) (CIF False) (CIF True)" |
|
|
78 |
"bool2if (And b c) = IF (bool2if b) (bool2if c) (CIF False)"
|
8745
|
79 |
|
67406
|
80 |
text\<open>\noindent
|
69597
|
81 |
At last, we have something we can verify: that \<^term>\<open>bool2if\<close> preserves the
|
8745
|
82 |
value of its argument:
|
67406
|
83 |
\<close>
|
8745
|
84 |
|
58860
|
85 |
lemma "valif (bool2if b) env = value b env"
|
8745
|
86 |
|
67406
|
87 |
txt\<open>\noindent
|
8745
|
88 |
The proof is canonical:
|
67406
|
89 |
\<close>
|
8745
|
90 |
|
58860
|
91 |
apply(induct_tac b)
|
|
92 |
apply(auto)
|
10171
|
93 |
done
|
8745
|
94 |
|
67406
|
95 |
text\<open>\noindent
|
8745
|
96 |
In fact, all proofs in this case study look exactly like this. Hence we do
|
|
97 |
not show them below.
|
|
98 |
|
|
99 |
More interesting is the transformation of If-expressions into a normal form
|
69597
|
100 |
where the first argument of \<^term>\<open>IF\<close> cannot be another \<^term>\<open>IF\<close> but
|
8745
|
101 |
must be a constant or variable. Such a normal form can be computed by
|
69597
|
102 |
repeatedly replacing a subterm of the form \<^term>\<open>IF (IF b x y) z u\<close> by
|
|
103 |
\<^term>\<open>IF b (IF x z u) (IF y z u)\<close>, which has the same value. The following
|
8745
|
104 |
primitive recursive functions perform this task:
|
67406
|
105 |
\<close>
|
8745
|
106 |
|
27015
|
107 |
primrec normif :: "ifex \<Rightarrow> ifex \<Rightarrow> ifex \<Rightarrow> ifex" where
|
|
108 |
"normif (CIF b) t e = IF (CIF b) t e" |
|
|
109 |
"normif (VIF x) t e = IF (VIF x) t e" |
|
|
110 |
"normif (IF b t e) u f = normif b (normif t u f) (normif e u f)"
|
8745
|
111 |
|
27015
|
112 |
primrec norm :: "ifex \<Rightarrow> ifex" where
|
|
113 |
"norm (CIF b) = CIF b" |
|
|
114 |
"norm (VIF x) = VIF x" |
|
|
115 |
"norm (IF b t e) = normif b (norm t) (norm e)"
|
8745
|
116 |
|
67406
|
117 |
text\<open>\noindent
|
11456
|
118 |
Their interplay is tricky; we leave it to you to develop an
|
8745
|
119 |
intuitive understanding. Fortunately, Isabelle can help us to verify that the
|
|
120 |
transformation preserves the value of the expression:
|
67406
|
121 |
\<close>
|
8745
|
122 |
|
58860
|
123 |
theorem "valif (norm b) env = valif b env"(*<*)oops(*>*)
|
8745
|
124 |
|
67406
|
125 |
text\<open>\noindent
|
8745
|
126 |
The proof is canonical, provided we first show the following simplification
|
69597
|
127 |
lemma, which also helps to understand what \<^term>\<open>normif\<close> does:
|
67406
|
128 |
\<close>
|
8745
|
129 |
|
|
130 |
lemma [simp]:
|
58860
|
131 |
"\<forall>t e. valif (normif b t e) env = valif (IF b t e) env"
|
8745
|
132 |
(*<*)
|
58860
|
133 |
apply(induct_tac b)
|
|
134 |
by(auto)
|
8745
|
135 |
|
58860
|
136 |
theorem "valif (norm b) env = valif b env"
|
|
137 |
apply(induct_tac b)
|
|
138 |
by(auto)
|
8745
|
139 |
(*>*)
|
67406
|
140 |
text\<open>\noindent
|
8745
|
141 |
Note that the lemma does not have a name, but is implicitly used in the proof
|
69505
|
142 |
of the theorem shown above because of the \<open>[simp]\<close> attribute.
|
8745
|
143 |
|
69597
|
144 |
But how can we be sure that \<^term>\<open>norm\<close> really produces a normal form in
|
11456
|
145 |
the above sense? We define a function that tests If-expressions for normality:
|
67406
|
146 |
\<close>
|
8745
|
147 |
|
27015
|
148 |
primrec normal :: "ifex \<Rightarrow> bool" where
|
|
149 |
"normal(CIF b) = True" |
|
|
150 |
"normal(VIF x) = True" |
|
10795
|
151 |
"normal(IF b t e) = (normal t \<and> normal e \<and>
|
27015
|
152 |
(case b of CIF b \<Rightarrow> True | VIF x \<Rightarrow> True | IF x y z \<Rightarrow> False))"
|
8745
|
153 |
|
67406
|
154 |
text\<open>\noindent
|
69597
|
155 |
Now we prove \<^term>\<open>normal(norm b)\<close>. Of course, this requires a lemma about
|
|
156 |
normality of \<^term>\<open>normif\<close>:
|
67406
|
157 |
\<close>
|
8745
|
158 |
|
58860
|
159 |
lemma [simp]: "\<forall>t e. normal(normif b t e) = (normal t \<and> normal e)"
|
8771
|
160 |
(*<*)
|
58860
|
161 |
apply(induct_tac b)
|
|
162 |
by(auto)
|
8745
|
163 |
|
58860
|
164 |
theorem "normal(norm b)"
|
|
165 |
apply(induct_tac b)
|
|
166 |
by(auto)
|
9933
|
167 |
(*>*)
|
8745
|
168 |
|
67406
|
169 |
text\<open>\medskip
|
10795
|
170 |
How do we come up with the required lemmas? Try to prove the main theorems
|
69505
|
171 |
without them and study carefully what \<open>auto\<close> leaves unproved. This
|
10795
|
172 |
can provide the clue. The necessity of universal quantification
|
69505
|
173 |
(\<open>\<forall>t e\<close>) in the two lemmas is explained in
|
9933
|
174 |
\S\ref{sec:InductionHeuristics}
|
|
175 |
|
|
176 |
\begin{exercise}
|
69597
|
177 |
We strengthen the definition of a \<^const>\<open>normal\<close> If-expression as follows:
|
|
178 |
the first argument of all \<^term>\<open>IF\<close>s must be a variable. Adapt the above
|
9933
|
179 |
development to this changed requirement. (Hint: you may need to formulate
|
69505
|
180 |
some of the goals as implications (\<open>\<longrightarrow>\<close>) rather than
|
|
181 |
equalities (\<open>=\<close>).)
|
9933
|
182 |
\end{exercise}
|
11456
|
183 |
\index{boolean expressions example|)}
|
67406
|
184 |
\<close>
|
9933
|
185 |
(*<*)
|
15243
|
186 |
|
27015
|
187 |
primrec normif2 :: "ifex => ifex => ifex => ifex" where
|
|
188 |
"normif2 (CIF b) t e = (if b then t else e)" |
|
|
189 |
"normif2 (VIF x) t e = IF (VIF x) t e" |
|
15243
|
190 |
"normif2 (IF b t e) u f = normif2 b (normif2 t u f) (normif2 e u f)"
|
|
191 |
|
27015
|
192 |
primrec norm2 :: "ifex => ifex" where
|
|
193 |
"norm2 (CIF b) = CIF b" |
|
|
194 |
"norm2 (VIF x) = VIF x" |
|
15243
|
195 |
"norm2 (IF b t e) = normif2 b (norm2 t) (norm2 e)"
|
|
196 |
|
27015
|
197 |
primrec normal2 :: "ifex => bool" where
|
|
198 |
"normal2(CIF b) = True" |
|
|
199 |
"normal2(VIF x) = True" |
|
15243
|
200 |
"normal2(IF b t e) = (normal2 t & normal2 e &
|
|
201 |
(case b of CIF b => False | VIF x => True | IF x y z => False))"
|
|
202 |
|
|
203 |
lemma [simp]:
|
67613
|
204 |
"\<forall>t e. valif (normif2 b t e) env = valif (IF b t e) env"
|
15243
|
205 |
apply(induct b)
|
|
206 |
by(auto)
|
|
207 |
|
|
208 |
theorem "valif (norm2 b) env = valif b env"
|
|
209 |
apply(induct b)
|
|
210 |
by(auto)
|
|
211 |
|
67613
|
212 |
lemma [simp]: "\<forall>t e. normal2 t & normal2 e --> normal2(normif2 b t e)"
|
15243
|
213 |
apply(induct b)
|
|
214 |
by(auto)
|
|
215 |
|
|
216 |
theorem "normal2(norm2 b)"
|
|
217 |
apply(induct b)
|
|
218 |
by(auto)
|
|
219 |
|
8771
|
220 |
end
|
|
221 |
(*>*)
|