21324
|
1 |
(*<*)theory FP1 imports Main begin(*>*)
|
13262
|
2 |
|
14138
|
3 |
subsection{*Quickcheck*}
|
|
4 |
|
|
5 |
lemma "rev(xs @ ys) = rev xs @ rev ys"
|
|
6 |
quickcheck
|
|
7 |
oops
|
|
8 |
|
|
9 |
subsection{*More Syntax*}
|
|
10 |
|
13262
|
11 |
lemma "if xs = ys
|
|
12 |
then rev xs = rev ys
|
|
13 |
else rev xs \<noteq> rev ys"
|
|
14 |
by auto
|
|
15 |
|
|
16 |
lemma "case xs of
|
|
17 |
[] \<Rightarrow> tl xs = xs
|
|
18 |
| y#ys \<Rightarrow> tl xs \<noteq> xs"
|
|
19 |
apply(case_tac xs)
|
|
20 |
by auto
|
|
21 |
|
|
22 |
|
|
23 |
subsection{*More Types*}
|
|
24 |
|
|
25 |
|
|
26 |
subsubsection{*Natural Numbers*}
|
|
27 |
|
|
28 |
consts sum :: "nat \<Rightarrow> nat"
|
|
29 |
primrec "sum 0 = 0"
|
|
30 |
"sum (Suc n) = Suc n + sum n"
|
|
31 |
|
|
32 |
lemma "sum n + sum n = n*(Suc n)"
|
|
33 |
apply(induct_tac n)
|
|
34 |
apply(auto)
|
|
35 |
done
|
|
36 |
|
|
37 |
text{*Some examples of linear arithmetic:*}
|
|
38 |
|
|
39 |
lemma "\<lbrakk> \<not> m < n; m < n+(1::int) \<rbrakk> \<Longrightarrow> m = n"
|
|
40 |
by(auto)
|
|
41 |
|
|
42 |
lemma "min i (max j k) = max (min k i) (min i (j::nat))"
|
|
43 |
by(arith)
|
|
44 |
|
14138
|
45 |
text{*Full Presburger arithmetic:*}
|
|
46 |
lemma "8 \<le> (n::int) \<Longrightarrow> \<exists>i j. 0\<le>i \<and> 0\<le>j \<and> n = 3*i + 5*j"
|
|
47 |
by(arith)
|
|
48 |
|
13262
|
49 |
text{*Not proved automatically because it involves multiplication:*}
|
|
50 |
lemma "n*n = n \<Longrightarrow> n=0 \<or> n=(1::int)"
|
|
51 |
(*<*)oops(*>*)
|
|
52 |
|
|
53 |
|
|
54 |
subsubsection{*Pairs*}
|
|
55 |
|
|
56 |
lemma "fst(x,y) = snd(z,x)"
|
|
57 |
by auto
|
|
58 |
|
|
59 |
|
|
60 |
subsection{*Definitions*}
|
|
61 |
|
|
62 |
consts xor :: "bool \<Rightarrow> bool \<Rightarrow> bool"
|
|
63 |
defs xor_def: "xor x y \<equiv> x \<and> \<not>y \<or> \<not>x \<and> y"
|
|
64 |
|
|
65 |
constdefs nand :: "bool \<Rightarrow> bool \<Rightarrow> bool"
|
|
66 |
"nand x y \<equiv> \<not>(x \<and> y)"
|
|
67 |
|
|
68 |
lemma "\<not> xor x x"
|
|
69 |
apply(unfold xor_def)
|
|
70 |
by auto
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
subsection{*Simplification*}
|
|
75 |
|
|
76 |
|
|
77 |
subsubsection{*Simplification Rules*}
|
|
78 |
|
|
79 |
lemma fst_conv[simp]: "fst(x,y) = x"
|
|
80 |
by auto
|
|
81 |
|
|
82 |
text{*Setting and resetting the @{text simp} attribute:*}
|
|
83 |
|
|
84 |
declare fst_conv[simp]
|
|
85 |
declare fst_conv[simp del]
|
|
86 |
|
|
87 |
|
|
88 |
subsubsection{*The Simplification Method*}
|
|
89 |
|
|
90 |
lemma "x*(y+1) = y*(x+1::nat)"
|
|
91 |
apply simp
|
|
92 |
(*<*)oops(*>*)
|
|
93 |
|
|
94 |
|
|
95 |
subsubsection{*Adding and Deleting Simplification Rules*}
|
|
96 |
|
|
97 |
lemma "\<forall>x::nat. x*(y+z) = r"
|
|
98 |
apply (simp add: add_mult_distrib2)
|
|
99 |
(*<*)oops(*>*)text_raw{* \isanewline\isanewline *}
|
|
100 |
|
|
101 |
lemma "rev(rev(xs @ [])) = xs"
|
|
102 |
apply (simp del: rev_rev_ident)
|
|
103 |
(*<*)oops(*>*)
|
|
104 |
|
|
105 |
|
|
106 |
subsubsection{*Rewriting with Definitions*}
|
|
107 |
|
|
108 |
lemma "xor A (\<not>A)"
|
|
109 |
apply(simp only: xor_def)
|
|
110 |
apply simp
|
|
111 |
done
|
|
112 |
|
|
113 |
|
|
114 |
subsubsection{*Conditional Equations*}
|
|
115 |
|
13489
|
116 |
(*<*)thm hd_Cons_tl(*>*)
|
|
117 |
text{*A pre-proved simplification rule: @{thm hd_Cons_tl[no_vars]}*}
|
|
118 |
lemma "hd(xs @ [x]) # tl(xs @ [x]) = xs @ [x]"
|
13262
|
119 |
by simp
|
|
120 |
|
|
121 |
|
|
122 |
subsubsection{*Automatic Case Splits*}
|
|
123 |
|
|
124 |
lemma "\<forall>xs. if xs = [] then A else B"
|
|
125 |
apply simp
|
|
126 |
(*<*)oops(*>*)
|
13489
|
127 |
text{*Case-expressions are only split on demand.*}
|
13262
|
128 |
|
|
129 |
|
|
130 |
subsubsection{*Arithmetic*}
|
|
131 |
|
13489
|
132 |
text{*Only simple arithmetic:*}
|
13262
|
133 |
lemma "\<lbrakk> \<not> m < n; m < n+(1::nat) \<rbrakk> \<Longrightarrow> m = n"
|
|
134 |
by simp
|
13489
|
135 |
text{*\noindent Complex goals need @{text arith}-method.*}
|
13262
|
136 |
|
|
137 |
(*<*)
|
|
138 |
subsubsection{*Tracing*}
|
|
139 |
|
|
140 |
lemma "rev [a] = []"
|
|
141 |
apply(simp)
|
|
142 |
oops
|
|
143 |
(*>*)
|
|
144 |
|
|
145 |
|
|
146 |
subsection{*Case Study: Compiling Expressions*}
|
|
147 |
|
|
148 |
|
|
149 |
subsubsection{*Expressions*}
|
|
150 |
|
|
151 |
types 'v binop = "'v \<Rightarrow> 'v \<Rightarrow> 'v"
|
|
152 |
|
|
153 |
datatype ('a,'v)expr = Cex 'v
|
|
154 |
| Vex 'a
|
|
155 |
| Bex "'v binop" "('a,'v)expr" "('a,'v)expr"
|
|
156 |
|
17656
|
157 |
consts "value" :: "('a,'v)expr \<Rightarrow> ('a \<Rightarrow> 'v) \<Rightarrow> 'v"
|
13262
|
158 |
primrec
|
|
159 |
"value (Cex v) env = v"
|
|
160 |
"value (Vex a) env = env a"
|
|
161 |
"value (Bex f e1 e2) env = f (value e1 env) (value e2 env)"
|
|
162 |
|
|
163 |
|
|
164 |
subsubsection{*The Stack Machine*}
|
|
165 |
|
|
166 |
datatype ('a,'v) instr = Const 'v
|
|
167 |
| Load 'a
|
|
168 |
| Apply "'v binop"
|
|
169 |
|
|
170 |
consts exec :: "('a,'v)instr list \<Rightarrow> ('a\<Rightarrow>'v) \<Rightarrow> 'v list \<Rightarrow> 'v list"
|
|
171 |
primrec
|
|
172 |
"exec [] s vs = vs"
|
|
173 |
"exec (i#is) s vs = (case i of
|
|
174 |
Const v \<Rightarrow> exec is s (v#vs)
|
|
175 |
| Load a \<Rightarrow> exec is s ((s a)#vs)
|
|
176 |
| Apply f \<Rightarrow> exec is s ((f (hd vs) (hd(tl vs)))#(tl(tl vs))))"
|
|
177 |
|
|
178 |
|
|
179 |
subsubsection{*The Compiler*}
|
|
180 |
|
14138
|
181 |
consts compile :: "('a,'v)expr \<Rightarrow> ('a,'v)instr list"
|
13262
|
182 |
primrec
|
14138
|
183 |
"compile (Cex v) = [Const v]"
|
|
184 |
"compile (Vex a) = [Load a]"
|
|
185 |
"compile (Bex f e1 e2) = (compile e2) @ (compile e1) @ [Apply f]"
|
13262
|
186 |
|
14138
|
187 |
theorem "exec (compile e) s [] = [value e s]"
|
13262
|
188 |
(*<*)oops(*>*)
|
|
189 |
|
|
190 |
|
|
191 |
|
|
192 |
subsection{*Advanced Datatypes*}
|
|
193 |
|
|
194 |
|
|
195 |
subsubsection{*Mutual Recursion*}
|
|
196 |
|
|
197 |
datatype 'a aexp = IF "'a bexp" "'a aexp" "'a aexp"
|
|
198 |
| Sum "'a aexp" "'a aexp"
|
|
199 |
| Var 'a
|
|
200 |
| Num nat
|
|
201 |
and 'a bexp = Less "'a aexp" "'a aexp"
|
|
202 |
| And "'a bexp" "'a bexp"
|
|
203 |
| Neg "'a bexp"
|
|
204 |
|
|
205 |
|
|
206 |
consts evala :: "'a aexp \<Rightarrow> ('a \<Rightarrow> nat) \<Rightarrow> nat"
|
|
207 |
evalb :: "'a bexp \<Rightarrow> ('a \<Rightarrow> nat) \<Rightarrow> bool"
|
|
208 |
|
|
209 |
primrec
|
|
210 |
"evala (IF b a1 a2) env =
|
|
211 |
(if evalb b env then evala a1 env else evala a2 env)"
|
|
212 |
"evala (Sum a1 a2) env = evala a1 env + evala a2 env"
|
|
213 |
"evala (Var v) env = env v"
|
|
214 |
"evala (Num n) env = n"
|
|
215 |
|
|
216 |
"evalb (Less a1 a2) env = (evala a1 env < evala a2 env)"
|
|
217 |
"evalb (And b1 b2) env = (evalb b1 env \<and> evalb b2 env)"
|
|
218 |
"evalb (Neg b) env = (\<not> evalb b env)"
|
|
219 |
|
|
220 |
consts substa :: "('a \<Rightarrow> 'b aexp) \<Rightarrow> 'a aexp \<Rightarrow> 'b aexp"
|
|
221 |
substb :: "('a \<Rightarrow> 'b aexp) \<Rightarrow> 'a bexp \<Rightarrow> 'b bexp"
|
|
222 |
|
|
223 |
primrec
|
|
224 |
"substa s (IF b a1 a2) =
|
|
225 |
IF (substb s b) (substa s a1) (substa s a2)"
|
|
226 |
"substa s (Sum a1 a2) = Sum (substa s a1) (substa s a2)"
|
|
227 |
"substa s (Var v) = s v"
|
|
228 |
"substa s (Num n) = Num n"
|
|
229 |
|
|
230 |
"substb s (Less a1 a2) = Less (substa s a1) (substa s a2)"
|
|
231 |
"substb s (And b1 b2) = And (substb s b1) (substb s b2)"
|
|
232 |
"substb s (Neg b) = Neg (substb s b)"
|
|
233 |
|
|
234 |
lemma substitution_lemma:
|
|
235 |
"evala (substa s a) env = evala a (\<lambda>x. evala (s x) env) \<and>
|
|
236 |
evalb (substb s b) env = evalb b (\<lambda>x. evala (s x) env)"
|
|
237 |
apply(induct_tac a and b)
|
|
238 |
by simp_all
|
|
239 |
|
|
240 |
|
|
241 |
subsubsection{*Nested Recursion*}
|
|
242 |
|
14138
|
243 |
datatype tree = Tree "tree list"
|
13262
|
244 |
|
|
245 |
text{*Some trees:*}
|
14138
|
246 |
term "Tree []"
|
|
247 |
term "Tree [Tree [Tree []], Tree []]"
|
13262
|
248 |
|
|
249 |
consts
|
|
250 |
mirror :: "tree \<Rightarrow> tree"
|
|
251 |
mirrors:: "tree list \<Rightarrow> tree list"
|
|
252 |
|
|
253 |
primrec
|
14138
|
254 |
"mirror(Tree ts) = Tree(mirrors ts)"
|
13262
|
255 |
|
|
256 |
"mirrors [] = []"
|
|
257 |
"mirrors (t # ts) = mirrors ts @ [mirror t]"
|
|
258 |
|
|
259 |
lemma "mirror(mirror t) = t \<and> mirrors(mirrors ts) = ts"
|
|
260 |
apply(induct_tac t and ts)
|
|
261 |
apply simp_all
|
|
262 |
(*<*)oops(*>*)
|
|
263 |
|
|
264 |
text{*
|
|
265 |
\begin{exercise}
|
|
266 |
Complete the above proof.
|
|
267 |
\end{exercise}
|
|
268 |
*}
|
|
269 |
|
|
270 |
subsubsection{*Datatypes Involving Functions*}
|
|
271 |
|
|
272 |
datatype ('a,'i)bigtree = Tip | Br 'a "'i \<Rightarrow> ('a,'i)bigtree"
|
|
273 |
|
|
274 |
text{*A big tree:*}
|
|
275 |
term "Br 0 (\<lambda>i. Br i (\<lambda>n. Tip))"
|
|
276 |
|
|
277 |
consts map_bt :: "('a \<Rightarrow> 'b) \<Rightarrow> ('a,'i)bigtree \<Rightarrow> ('b,'i)bigtree"
|
|
278 |
primrec
|
|
279 |
"map_bt f Tip = Tip"
|
|
280 |
"map_bt f (Br a F) = Br (f a) (\<lambda>i. map_bt f (F i))"
|
|
281 |
|
|
282 |
lemma "map_bt (g o f) T = map_bt g (map_bt f T)"
|
|
283 |
apply(induct_tac T, rename_tac[2] F)
|
|
284 |
apply simp_all
|
|
285 |
done
|
|
286 |
|
14138
|
287 |
text{*The ordinals:*}
|
|
288 |
datatype ord = Zero | Succ ord | Lim "nat \<Rightarrow> ord"
|
|
289 |
|
|
290 |
thm ord.induct[no_vars]
|
|
291 |
|
|
292 |
instance ord :: plus ..
|
|
293 |
instance ord :: times ..
|
|
294 |
|
|
295 |
primrec
|
|
296 |
"a + Zero = a"
|
|
297 |
"a + Succ b = Succ(a+b)"
|
|
298 |
"a + Lim F = Lim(\<lambda>n. a + F n)"
|
|
299 |
|
|
300 |
primrec
|
|
301 |
"a * Zero = Zero"
|
|
302 |
"a * Succ b = a*b + a"
|
|
303 |
"a * Lim F = Lim(\<lambda>n. a * F n)"
|
|
304 |
|
|
305 |
text{*An example provided by Stan Wainer:*}
|
|
306 |
consts H :: "ord \<Rightarrow> (nat \<Rightarrow> nat) \<Rightarrow> (nat \<Rightarrow> nat)"
|
|
307 |
primrec
|
|
308 |
"H Zero f n = n"
|
|
309 |
"H (Succ b) f n = H b f (f n)"
|
|
310 |
"H (Lim F) f n = H (F n) f n"
|
|
311 |
|
|
312 |
lemma [simp]: "H (a+b) f = H a f \<circ> H b f"
|
|
313 |
apply(induct b)
|
|
314 |
apply auto
|
|
315 |
done
|
|
316 |
|
|
317 |
lemma [simp]: "H (a*b) = H b \<circ> H a"
|
|
318 |
apply(induct b)
|
|
319 |
apply auto
|
|
320 |
done
|
|
321 |
|
13262
|
322 |
text{* This is \emph{not} allowed:
|
|
323 |
\begin{verbatim}
|
|
324 |
datatype lambda = C "lambda => lambda"
|
|
325 |
\end{verbatim}
|
|
326 |
|
|
327 |
\begin{exercise}
|
14138
|
328 |
Define the ordinal $\Gamma_0$.
|
13262
|
329 |
\end{exercise}
|
|
330 |
*}
|
|
331 |
(*<*)end(*>*)
|