150 [Cn (vl - 1) (constn 0) [id (vl - 1) 0]])) |
150 [Cn (vl - 1) (constn 0) [id (vl - 1) 0]])) |
151 (Cn (Suc vl) rec_add [id (Suc vl) vl, |
151 (Cn (Suc vl) rec_add [id (Suc vl) vl, |
152 Cn (Suc vl) rf (get_fstn_args (Suc vl) (vl - 1) |
152 Cn (Suc vl) rf (get_fstn_args (Suc vl) (vl - 1) |
153 @ [Cn (Suc vl) s [id (Suc vl) (vl - 1)]])]))" |
153 @ [Cn (Suc vl) s [id (Suc vl) (vl - 1)]])]))" |
154 |
154 |
155 |
155 text {* |
156 declare rec_eval.simps[simp del] constn.simps[simp del] |
156 @{text "rec_exec"} is the interpreter function for |
157 |
157 reursive functions. The function is defined such that |
158 |
158 it always returns meaningful results for primitive recursive |
159 section {* Correctness of various recursive functions *} |
159 functions. |
160 |
160 *} |
161 |
161 |
162 lemma add_lemma: "rec_eval rec_add [x, y] = x + y" |
162 declare rec_exec.simps[simp del] constn.simps[simp del] |
163 by(induct_tac y, auto simp: rec_add_def rec_eval.simps) |
163 |
164 |
164 text {* |
165 lemma mult_lemma: "rec_eval rec_mult [x, y] = x * y" |
165 Correctness of @{text "rec_add"}. |
166 by(induct_tac y, auto simp: rec_mult_def rec_eval.simps add_lemma) |
166 *} |
167 |
167 lemma add_lemma: "\<And> x y. rec_exec rec_add [x, y] = x + y" |
168 lemma pred_lemma: "rec_eval rec_pred [x] = x - 1" |
168 by(induct_tac y, auto simp: rec_add_def rec_exec.simps) |
169 by(induct_tac x, auto simp: rec_pred_def rec_eval.simps) |
169 |
170 |
170 text {* |
171 lemma minus_lemma: "rec_eval rec_minus [x, y] = x - y" |
171 Correctness of @{text "rec_mult"}. |
172 by(induct_tac y, auto simp: rec_eval.simps rec_minus_def pred_lemma) |
172 *} |
173 |
173 lemma mult_lemma: "\<And> x y. rec_exec rec_mult [x, y] = x * y" |
174 lemma sg_lemma: "rec_eval rec_sg [x] = (if x = 0 then 0 else 1)" |
174 by(induct_tac y, auto simp: rec_mult_def rec_exec.simps add_lemma) |
175 by(auto simp: rec_sg_def minus_lemma rec_eval.simps constn.simps) |
175 |
176 |
176 text {* |
177 lemma constn_lemma: "rec_eval (constn n) [x] = n" |
177 Correctness of @{text "rec_pred"}. |
178 by(induct n, auto simp: rec_eval.simps constn.simps) |
178 *} |
179 |
179 lemma pred_lemma: "\<And> x. rec_exec rec_pred [x] = x - 1" |
180 lemma less_lemma: "rec_eval rec_less [x, y] = (if x < y then 1 else 0)" |
180 by(induct_tac x, auto simp: rec_pred_def rec_exec.simps) |
181 by(induct_tac y, auto simp: rec_eval.simps |
181 |
|
182 text {* |
|
183 Correctness of @{text "rec_minus"}. |
|
184 *} |
|
185 lemma minus_lemma: "\<And> x y. rec_exec rec_minus [x, y] = x - y" |
|
186 by(induct_tac y, auto simp: rec_exec.simps rec_minus_def pred_lemma) |
|
187 |
|
188 text {* |
|
189 Correctness of @{text "rec_sg"}. |
|
190 *} |
|
191 lemma sg_lemma: "\<And> x. rec_exec rec_sg [x] = (if x = 0 then 0 else 1)" |
|
192 by(auto simp: rec_sg_def minus_lemma rec_exec.simps constn.simps) |
|
193 |
|
194 text {* |
|
195 Correctness of @{text "constn"}. |
|
196 *} |
|
197 lemma constn_lemma: "rec_exec (constn n) [x] = n" |
|
198 by(induct n, auto simp: rec_exec.simps constn.simps) |
|
199 |
|
200 text {* |
|
201 Correctness of @{text "rec_less"}. |
|
202 *} |
|
203 lemma less_lemma: "\<And> x y. rec_exec rec_less [x, y] = |
|
204 (if x < y then 1 else 0)" |
|
205 by(induct_tac y, auto simp: rec_exec.simps |
182 rec_less_def minus_lemma sg_lemma) |
206 rec_less_def minus_lemma sg_lemma) |
183 |
207 |
184 lemma not_lemma: "rec_eval rec_not [x] = (if x = 0 then 1 else 0)" |
208 text {* |
185 by(induct_tac x, auto simp: rec_eval.simps rec_not_def |
209 Correctness of @{text "rec_not"}. |
|
210 *} |
|
211 lemma not_lemma: |
|
212 "\<And> x. rec_exec rec_not [x] = (if x = 0 then 1 else 0)" |
|
213 by(induct_tac x, auto simp: rec_exec.simps rec_not_def |
186 constn_lemma minus_lemma) |
214 constn_lemma minus_lemma) |
187 |
215 |
188 lemma eq_lemma: "rec_eval rec_eq [x, y] = (if x = y then 1 else 0)" |
216 text {* |
189 by(induct_tac y, auto simp: rec_eval.simps rec_eq_def constn_lemma add_lemma minus_lemma) |
217 Correctness of @{text "rec_eq"}. |
190 |
218 *} |
191 lemma conj_lemma: "rec_eval rec_conj [x, y] = (if x = 0 \<or> y = 0 then 0 else 1)" |
219 lemma eq_lemma: "\<And> x y. rec_exec rec_eq [x, y] = (if x = y then 1 else 0)" |
192 by(induct_tac y, auto simp: rec_eval.simps sg_lemma rec_conj_def mult_lemma) |
220 by(induct_tac y, auto simp: rec_exec.simps rec_eq_def constn_lemma add_lemma minus_lemma) |
193 |
221 |
194 lemma disj_lemma: "rec_eval rec_disj [x, y] = (if x = 0 \<and> y = 0 then 0 else 1)" |
222 text {* |
195 by(induct_tac y, auto simp: rec_disj_def sg_lemma add_lemma rec_eval.simps) |
223 Correctness of @{text "rec_conj"}. |
196 |
224 *} |
197 declare mult_lemma[simp] add_lemma[simp] pred_lemma[simp] |
225 lemma conj_lemma: "\<And> x y. rec_exec rec_conj [x, y] = (if x = 0 \<or> y = 0 then 0 |
198 minus_lemma[simp] sg_lemma[simp] constn_lemma[simp] |
226 else 1)" |
199 less_lemma[simp] not_lemma[simp] eq_lemma[simp] |
227 by(induct_tac y, auto simp: rec_exec.simps sg_lemma rec_conj_def mult_lemma) |
200 conj_lemma[simp] disj_lemma[simp] |
228 |
201 |
229 text {* |
202 text {* |
230 Correctness of @{text "rec_disj"}. |
203 @{text "primrec recf n"} is true iff @{text "recf"} is a primitive |
231 *} |
204 recursive function with arity @{text "n"}. |
232 lemma disj_lemma: "\<And> x y. rec_exec rec_disj [x, y] = (if x = 0 \<and> y = 0 then 0 |
205 *} |
233 else 1)" |
206 |
234 by(induct_tac y, auto simp: rec_disj_def sg_lemma add_lemma rec_exec.simps) |
|
235 |
|
236 |
|
237 text {* |
|
238 @{text "primrec recf n"} is true iff |
|
239 @{text "recf"} is a primitive recursive function |
|
240 with arity @{text "n"}. |
|
241 *} |
207 inductive primerec :: "recf \<Rightarrow> nat \<Rightarrow> bool" |
242 inductive primerec :: "recf \<Rightarrow> nat \<Rightarrow> bool" |
208 where |
243 where |
209 prime_z[intro]: "primerec z (Suc 0)" | |
244 prime_z[intro]: "primerec z (Suc 0)" | |
210 prime_s[intro]: "primerec s (Suc 0)" | |
245 prime_s[intro]: "primerec s (Suc 0)" | |
211 prime_id[intro!]: "\<lbrakk>n < m\<rbrakk> \<Longrightarrow> primerec (id m n) m" | |
246 prime_id[intro!]: "\<lbrakk>n < m\<rbrakk> \<Longrightarrow> primerec (id m n) m" | |
222 inductive_cases prime_s_reverse[elim]: "primerec s n" |
257 inductive_cases prime_s_reverse[elim]: "primerec s n" |
223 inductive_cases prime_id_reverse[elim]: "primerec (id m n) k" |
258 inductive_cases prime_id_reverse[elim]: "primerec (id m n) k" |
224 inductive_cases prime_cn_reverse[elim]: "primerec (Cn n f gs) m" |
259 inductive_cases prime_cn_reverse[elim]: "primerec (Cn n f gs) m" |
225 inductive_cases prime_pr_reverse[elim]: "primerec (Pr n f g) m" |
260 inductive_cases prime_pr_reverse[elim]: "primerec (Pr n f g) m" |
226 |
261 |
227 |
262 declare mult_lemma[simp] add_lemma[simp] pred_lemma[simp] |
|
263 minus_lemma[simp] sg_lemma[simp] constn_lemma[simp] |
|
264 less_lemma[simp] not_lemma[simp] eq_lemma[simp] |
|
265 conj_lemma[simp] disj_lemma[simp] |
228 |
266 |
229 text {* |
267 text {* |
230 @{text "Sigma"} is the logical specification of |
268 @{text "Sigma"} is the logical specification of |
231 the recursive function @{text "rec_sigma"}. |
269 the recursive function @{text "rec_sigma"}. |
232 *} |
270 *} |
233 function Sigma :: "(nat list \<Rightarrow> nat) \<Rightarrow> nat list \<Rightarrow> nat" |
271 function Sigma :: "(nat list \<Rightarrow> nat) \<Rightarrow> nat list \<Rightarrow> nat" |
234 where |
272 where |
235 "Sigma g xs = (if last xs = 0 then g xs |
273 "Sigma g xs = (if last xs = 0 then g xs |
236 else (Sigma g (butlast xs @ [last xs - 1]) + g xs)) " |
274 else (Sigma g (butlast xs @ [last xs - 1]) + |
|
275 g xs)) " |
237 by pat_completeness auto |
276 by pat_completeness auto |
238 |
|
239 termination |
277 termination |
240 proof |
278 proof |
241 show "wf (measure (\<lambda> (f, xs). last xs))" by auto |
279 show "wf (measure (\<lambda> (f, xs). last xs))" by auto |
242 next |
280 next |
243 fix g xs |
281 fix g xs |
244 assume "last (xs::nat list) \<noteq> 0" |
282 assume "last (xs::nat list) \<noteq> 0" |
245 thus "((g, butlast xs @ [last xs - 1]), g, xs) \<in> measure (\<lambda>(f, xs). last xs)" |
283 thus "((g, butlast xs @ [last xs - 1]), g, xs) |
|
284 \<in> measure (\<lambda>(f, xs). last xs)" |
246 by auto |
285 by auto |
247 qed |
286 qed |
248 |
287 |
249 declare rec_eval.simps[simp del] get_fstn_args.simps[simp del] |
288 declare rec_exec.simps[simp del] get_fstn_args.simps[simp del] |
250 arity.simps[simp del] Sigma.simps[simp del] |
289 arity.simps[simp del] Sigma.simps[simp del] |
251 rec_sigma.simps[simp del] |
290 rec_sigma.simps[simp del] |
252 |
291 |
253 lemma [simp]: "arity z = 1" |
292 lemma [simp]: "arity z = 1" |
254 by(simp add: arity.simps) |
293 by(simp add: arity.simps) |
255 |
294 |
256 lemma rec_pr_0_simp_rewrite: " |
295 lemma rec_pr_0_simp_rewrite: " |
257 rec_eval (Pr n f g) (xs @ [0]) = rec_eval f xs" |
296 rec_exec (Pr n f g) (xs @ [0]) = rec_exec f xs" |
258 by(simp add: rec_eval.simps) |
297 by(simp add: rec_exec.simps) |
259 |
298 |
260 lemma rec_pr_0_simp_rewrite_single_param: " |
299 lemma rec_pr_0_simp_rewrite_single_param: " |
261 rec_eval (Pr n f g) [0] = rec_eval f []" |
300 rec_exec (Pr n f g) [0] = rec_exec f []" |
262 by(simp add: rec_eval.simps) |
301 by(simp add: rec_exec.simps) |
263 |
302 |
264 lemma rec_pr_Suc_simp_rewrite: |
303 lemma rec_pr_Suc_simp_rewrite: |
265 "rec_eval (Pr n f g) (xs @ [Suc x]) = rec_eval g (xs @ [x] @ [rec_eval (Pr n f g) (xs @ [x])])" |
304 "rec_exec (Pr n f g) (xs @ [Suc x]) = |
266 by(simp add: rec_eval.simps) |
305 rec_exec g (xs @ [x] @ |
|
306 [rec_exec (Pr n f g) (xs @ [x])])" |
|
307 by(simp add: rec_exec.simps) |
267 |
308 |
268 lemma rec_pr_Suc_simp_rewrite_single_param: |
309 lemma rec_pr_Suc_simp_rewrite_single_param: |
269 "rec_eval (Pr n f g) ([Suc x]) = rec_eval g ([x] @ [rec_eval (Pr n f g) ([x])])" |
310 "rec_exec (Pr n f g) ([Suc x]) = |
270 by(simp add: rec_eval.simps) |
311 rec_exec g ([x] @ [rec_exec (Pr n f g) ([x])])" |
|
312 by(simp add: rec_exec.simps) |
271 |
313 |
272 lemma Sigma_0_simp_rewrite_single_param: |
314 lemma Sigma_0_simp_rewrite_single_param: |
273 "Sigma f [0] = f [0]" |
315 "Sigma f [0] = f [0]" |
274 by(simp add: Sigma.simps) |
316 by(simp add: Sigma.simps) |
275 |
317 |
305 apply(erule_tac prime_mn_reverse) |
347 apply(erule_tac prime_mn_reverse) |
306 done |
348 done |
307 |
349 |
308 lemma rec_sigma_Suc_simp_rewrite: |
350 lemma rec_sigma_Suc_simp_rewrite: |
309 "primerec f (Suc (length xs)) |
351 "primerec f (Suc (length xs)) |
310 \<Longrightarrow> rec_eval (rec_sigma f) (xs @ [Suc x]) = |
352 \<Longrightarrow> rec_exec (rec_sigma f) (xs @ [Suc x]) = |
311 rec_eval (rec_sigma f) (xs @ [x]) + rec_eval f (xs @ [Suc x])" |
353 rec_exec (rec_sigma f) (xs @ [x]) + rec_exec f (xs @ [Suc x])" |
312 apply(induct x) |
354 apply(induct x) |
313 apply(auto simp: rec_sigma.simps Let_def rec_pr_Suc_simp_rewrite |
355 apply(auto simp: rec_sigma.simps Let_def rec_pr_Suc_simp_rewrite |
314 rec_eval.simps get_fstn_args_take) |
356 rec_exec.simps get_fstn_args_take) |
315 done |
357 done |
316 |
358 |
317 text {* |
359 text {* |
318 The correctness of @{text "rec_sigma"} with respect to its specification. |
360 The correctness of @{text "rec_sigma"} with respect to its specification. |
319 *} |
361 *} |
320 lemma sigma_lemma: |
362 lemma sigma_lemma: |
321 "primerec rg (Suc (length xs)) |
363 "primerec rg (Suc (length xs)) |
322 \<Longrightarrow> rec_eval (rec_sigma rg) (xs @ [x]) = Sigma (rec_eval rg) (xs @ [x])" |
364 \<Longrightarrow> rec_exec (rec_sigma rg) (xs @ [x]) = Sigma (rec_exec rg) (xs @ [x])" |
323 apply(induct x) |
365 apply(induct x) |
324 apply(auto simp: rec_eval.simps rec_sigma.simps Let_def |
366 apply(auto simp: rec_exec.simps rec_sigma.simps Let_def |
325 get_fstn_args_take Sigma_0_simp_rewrite |
367 get_fstn_args_take Sigma_0_simp_rewrite |
326 Sigma_Suc_simp_rewrite) |
368 Sigma_Suc_simp_rewrite) |
327 done |
369 done |
328 |
370 |
329 text {* |
371 text {* |
397 (let vl = arity rf in |
439 (let vl = arity rf in |
398 Cn (vl - 1) rec_sg [Cn (vl - 1) (rec_accum rf) |
440 Cn (vl - 1) rec_sg [Cn (vl - 1) (rec_accum rf) |
399 (get_fstn_args (vl - 1) (vl - 1) @ [rt])])" |
441 (get_fstn_args (vl - 1) (vl - 1) @ [rt])])" |
400 |
442 |
401 lemma rec_accum_ex: "primerec rf (Suc (length xs)) \<Longrightarrow> |
443 lemma rec_accum_ex: "primerec rf (Suc (length xs)) \<Longrightarrow> |
402 (rec_eval (rec_accum rf) (xs @ [x]) = 0) = |
444 (rec_exec (rec_accum rf) (xs @ [x]) = 0) = |
403 (\<exists> t \<le> x. rec_eval rf (xs @ [t]) = 0)" |
445 (\<exists> t \<le> x. rec_exec rf (xs @ [t]) = 0)" |
404 apply(induct x, simp_all add: rec_accum_Suc_simp_rewrite) |
446 apply(induct x, simp_all add: rec_accum_Suc_simp_rewrite) |
405 apply(simp add: rec_eval.simps rec_accum.simps get_fstn_args_take, |
447 apply(simp add: rec_exec.simps rec_accum.simps get_fstn_args_take, |
406 auto) |
448 auto) |
407 apply(rule_tac x = ta in exI, simp) |
449 apply(rule_tac x = ta in exI, simp) |
408 apply(case_tac "t = Suc x", simp_all) |
450 apply(case_tac "t = Suc x", simp_all) |
409 apply(rule_tac x = t in exI, simp) |
451 apply(rule_tac x = t in exI, simp) |
410 done |
452 done |
413 The correctness of @{text "rec_all"}. |
455 The correctness of @{text "rec_all"}. |
414 *} |
456 *} |
415 lemma all_lemma: |
457 lemma all_lemma: |
416 "\<lbrakk>primerec rf (Suc (length xs)); |
458 "\<lbrakk>primerec rf (Suc (length xs)); |
417 primerec rt (length xs)\<rbrakk> |
459 primerec rt (length xs)\<rbrakk> |
418 \<Longrightarrow> rec_eval (rec_all rt rf) xs = (if (\<forall> x \<le> (rec_eval rt xs). 0 < rec_eval rf (xs @ [x])) then 1 |
460 \<Longrightarrow> rec_exec (rec_all rt rf) xs = (if (\<forall> x \<le> (rec_exec rt xs). 0 < rec_exec rf (xs @ [x])) then 1 |
419 else 0)" |
461 else 0)" |
420 apply(auto simp: rec_all.simps) |
462 apply(auto simp: rec_all.simps) |
421 apply(simp add: rec_eval.simps map_append get_fstn_args_take split: if_splits) |
463 apply(simp add: rec_exec.simps map_append get_fstn_args_take split: if_splits) |
422 apply(drule_tac x = "rec_eval rt xs" in rec_accum_ex) |
464 apply(drule_tac x = "rec_exec rt xs" in rec_accum_ex) |
423 apply(case_tac "rec_eval (rec_accum rf) (xs @ [rec_eval rt xs]) = 0", simp_all) |
465 apply(case_tac "rec_exec (rec_accum rf) (xs @ [rec_exec rt xs]) = 0", simp_all) |
424 apply(erule_tac exE, erule_tac x = t in allE, simp) |
466 apply(erule_tac exE, erule_tac x = t in allE, simp) |
425 apply(simp add: rec_eval.simps map_append get_fstn_args_take) |
467 apply(simp add: rec_exec.simps map_append get_fstn_args_take) |
426 apply(drule_tac x = "rec_eval rt xs" in rec_accum_ex) |
468 apply(drule_tac x = "rec_exec rt xs" in rec_accum_ex) |
427 apply(case_tac "rec_eval (rec_accum rf) (xs @ [rec_eval rt xs]) = 0", simp, simp) |
469 apply(case_tac "rec_exec (rec_accum rf) (xs @ [rec_exec rt xs]) = 0", simp, simp) |
428 apply(erule_tac x = x in allE, simp) |
470 apply(erule_tac x = x in allE, simp) |
429 done |
471 done |
430 |
472 |
431 text {* |
473 text {* |
432 @{text "rec_ex t f (x1, x2, \<dots>, xn)"} |
474 @{text "rec_ex t f (x1, x2, \<dots>, xn)"} |
439 (let vl = arity rf in |
481 (let vl = arity rf in |
440 Cn (vl - 1) rec_sg [Cn (vl - 1) (rec_sigma rf) |
482 Cn (vl - 1) rec_sg [Cn (vl - 1) (rec_sigma rf) |
441 (get_fstn_args (vl - 1) (vl - 1) @ [rt])])" |
483 (get_fstn_args (vl - 1) (vl - 1) @ [rt])])" |
442 |
484 |
443 lemma rec_sigma_ex: "primerec rf (Suc (length xs)) |
485 lemma rec_sigma_ex: "primerec rf (Suc (length xs)) |
444 \<Longrightarrow> (rec_eval (rec_sigma rf) (xs @ [x]) = 0) = |
486 \<Longrightarrow> (rec_exec (rec_sigma rf) (xs @ [x]) = 0) = |
445 (\<forall> t \<le> x. rec_eval rf (xs @ [t]) = 0)" |
487 (\<forall> t \<le> x. rec_exec rf (xs @ [t]) = 0)" |
446 apply(induct x, simp_all add: rec_sigma_Suc_simp_rewrite) |
488 apply(induct x, simp_all add: rec_sigma_Suc_simp_rewrite) |
447 apply(simp add: rec_eval.simps rec_sigma.simps |
489 apply(simp add: rec_exec.simps rec_sigma.simps |
448 get_fstn_args_take, auto) |
490 get_fstn_args_take, auto) |
449 apply(case_tac "t = Suc x", simp_all) |
491 apply(case_tac "t = Suc x", simp_all) |
450 done |
492 done |
451 |
493 |
452 text {* |
494 text {* |
453 The correctness of @{text "ex_lemma"}. |
495 The correctness of @{text "ex_lemma"}. |
454 *} |
496 *} |
455 lemma ex_lemma:" |
497 lemma ex_lemma:" |
456 \<lbrakk>primerec rf (Suc (length xs)); |
498 \<lbrakk>primerec rf (Suc (length xs)); |
457 primerec rt (length xs)\<rbrakk> |
499 primerec rt (length xs)\<rbrakk> |
458 \<Longrightarrow> (rec_eval (rec_ex rt rf) xs = |
500 \<Longrightarrow> (rec_exec (rec_ex rt rf) xs = |
459 (if (\<exists> x \<le> (rec_eval rt xs). 0 <rec_eval rf (xs @ [x])) then 1 |
501 (if (\<exists> x \<le> (rec_exec rt xs). 0 <rec_exec rf (xs @ [x])) then 1 |
460 else 0))" |
502 else 0))" |
461 apply(auto simp: rec_ex.simps rec_eval.simps map_append get_fstn_args_take |
503 apply(auto simp: rec_ex.simps rec_exec.simps map_append get_fstn_args_take |
462 split: if_splits) |
504 split: if_splits) |
463 apply(drule_tac x = "rec_eval rt xs" in rec_sigma_ex, simp) |
505 apply(drule_tac x = "rec_exec rt xs" in rec_sigma_ex, simp) |
464 apply(drule_tac x = "rec_eval rt xs" in rec_sigma_ex, simp) |
506 apply(drule_tac x = "rec_exec rt xs" in rec_sigma_ex, simp) |
465 done |
507 done |
466 |
508 |
467 text {* |
509 text {* |
468 Definition of @{text "Min[R]"} on page 77 of Boolos's book. |
510 Definition of @{text "Min[R]"} on page 77 of Boolos's book. |
469 *} |
511 *} |
521 else if (Rr (xs @ [Suc w])) then (Suc w) |
563 else if (Rr (xs @ [Suc w])) then (Suc w) |
522 else Suc (Suc w))" |
564 else Suc (Suc w))" |
523 by(insert Minr_range[of Rr xs w], auto) |
565 by(insert Minr_range[of Rr xs w], auto) |
524 |
566 |
525 text {* |
567 text {* |
526 @{text "rec_Minmr"} is the recursive function |
568 @{text "rec_Minr"} is the recursive function |
527 used to implement @{text "Minr"}: |
569 used to implement @{text "Minr"}: |
528 if @{text "Rr"} is implemented by a recursive function @{text "recf"}, |
570 if @{text "Rr"} is implemented by a recursive function @{text "recf"}, |
529 then @{text "rec_Minr recf"} is the recursive function used to |
571 then @{text "rec_Minr recf"} is the recursive function used to |
530 implement @{text "Minr Rr"} |
572 implement @{text "Minr Rr"} |
531 *} |
573 *} |
532 fun rec_Minr :: "recf \<Rightarrow> recf" |
574 fun rec_Minr :: "recf \<Rightarrow> recf" |
533 where |
575 where |
534 "rec_Minr rf = |
576 "rec_Minr rf = |
535 (let vl = arity rf |
577 (let vl = arity rf |
536 in let rq = rec_all (id vl (vl - 1)) |
578 in let rq = rec_all (id vl (vl - 1)) (Cn (Suc vl) |
537 (Cn (Suc vl) rec_not [Cn (Suc vl) rf |
579 rec_not [Cn (Suc vl) rf |
538 (get_fstn_args (Suc vl) (vl - 1) @ [id (Suc vl) vl])]) |
580 (get_fstn_args (Suc vl) (vl - 1) @ |
|
581 [id (Suc vl) (vl)])]) |
539 in rec_sigma rq)" |
582 in rec_sigma rq)" |
540 |
583 |
541 lemma length_getpren_params[simp]: "length (get_fstn_args m n) = n" |
584 lemma length_getpren_params[simp]: "length (get_fstn_args m n) = n" |
542 by(induct n, auto simp: get_fstn_args.simps) |
585 by(induct n, auto simp: get_fstn_args.simps) |
543 |
586 |
635 apply(simp add: rec_not_def) |
678 apply(simp add: rec_not_def) |
636 apply(rule prime_cn, auto) |
679 apply(rule prime_cn, auto) |
637 apply(case_tac i, auto intro: prime_id) |
680 apply(case_tac i, auto intro: prime_id) |
638 done |
681 done |
639 |
682 |
640 lemma Min_false1[simp]: "\<lbrakk>\<not> Min {uu. uu \<le> w \<and> 0 < rec_eval rf (xs @ [uu])} \<le> w; |
683 lemma Min_false1[simp]: "\<lbrakk>\<not> Min {uu. uu \<le> w \<and> 0 < rec_exec rf (xs @ [uu])} \<le> w; |
641 x \<le> w; 0 < rec_eval rf (xs @ [x])\<rbrakk> |
684 x \<le> w; 0 < rec_exec rf (xs @ [x])\<rbrakk> |
642 \<Longrightarrow> False" |
685 \<Longrightarrow> False" |
643 apply(subgoal_tac "finite {uu. uu \<le> w \<and> 0 < rec_eval rf (xs @ [uu])}") |
686 apply(subgoal_tac "finite {uu. uu \<le> w \<and> 0 < rec_exec rf (xs @ [uu])}") |
644 apply(subgoal_tac "{uu. uu \<le> w \<and> 0 < rec_eval rf (xs @ [uu])} \<noteq> {}") |
687 apply(subgoal_tac "{uu. uu \<le> w \<and> 0 < rec_exec rf (xs @ [uu])} \<noteq> {}") |
645 apply(simp add: Min_le_iff, simp) |
688 apply(simp add: Min_le_iff, simp) |
646 apply(rule_tac x = x in exI, simp) |
689 apply(rule_tac x = x in exI, simp) |
647 apply(simp) |
690 apply(simp) |
648 done |
691 done |
649 |
692 |
650 lemma sigma_minr_lemma: |
693 lemma sigma_minr_lemma: |
651 assumes prrf: "primerec rf (Suc (length xs))" |
694 assumes prrf: "primerec rf (Suc (length xs))" |
652 shows "UF.Sigma (rec_eval (rec_all (recf.id (Suc (length xs)) (length xs)) |
695 shows "UF.Sigma (rec_exec (rec_all (recf.id (Suc (length xs)) (length xs)) |
653 (Cn (Suc (Suc (length xs))) rec_not |
696 (Cn (Suc (Suc (length xs))) rec_not |
654 [Cn (Suc (Suc (length xs))) rf (get_fstn_args (Suc (Suc (length xs))) |
697 [Cn (Suc (Suc (length xs))) rf (get_fstn_args (Suc (Suc (length xs))) |
655 (length xs) @ [recf.id (Suc (Suc (length xs))) (Suc (length xs))])]))) |
698 (length xs) @ [recf.id (Suc (Suc (length xs))) (Suc (length xs))])]))) |
656 (xs @ [w]) = |
699 (xs @ [w]) = |
657 Minr (\<lambda>args. 0 < rec_eval rf args) xs w" |
700 Minr (\<lambda>args. 0 < rec_exec rf args) xs w" |
658 proof(induct w) |
701 proof(induct w) |
659 let ?rt = "(recf.id (Suc (length xs)) ((length xs)))" |
702 let ?rt = "(recf.id (Suc (length xs)) ((length xs)))" |
660 let ?rf = "(Cn (Suc (Suc (length xs))) |
703 let ?rf = "(Cn (Suc (Suc (length xs))) |
661 rec_not [Cn (Suc (Suc (length xs))) rf |
704 rec_not [Cn (Suc (Suc (length xs))) rf |
662 (get_fstn_args (Suc (Suc (length xs))) (length xs) @ |
705 (get_fstn_args (Suc (Suc (length xs))) (length xs) @ |
682 (get_fstn_args (Suc (Suc (length xs))) (length xs) @ |
725 (get_fstn_args (Suc (Suc (length xs))) (length xs) @ |
683 [recf.id (Suc (Suc (length xs))) |
726 [recf.id (Suc (Suc (length xs))) |
684 (Suc ((length xs)))])])" |
727 (Suc ((length xs)))])])" |
685 let ?rq = "(rec_all ?rt ?rf)" |
728 let ?rq = "(rec_all ?rt ?rf)" |
686 assume ind: |
729 assume ind: |
687 "Sigma (rec_eval (rec_all ?rt ?rf)) (xs @ [w]) = Minr (\<lambda>args. 0 < rec_eval rf args) xs w" |
730 "Sigma (rec_exec (rec_all ?rt ?rf)) (xs @ [w]) = Minr (\<lambda>args. 0 < rec_exec rf args) xs w" |
688 have prrf: "primerec ?rf (Suc (length (xs @ [Suc w]))) \<and> |
731 have prrf: "primerec ?rf (Suc (length (xs @ [Suc w]))) \<and> |
689 primerec ?rt (length (xs @ [Suc w]))" |
732 primerec ?rt (length (xs @ [Suc w]))" |
690 apply(auto simp: prrf nth_append)+ |
733 apply(auto simp: prrf nth_append)+ |
691 done |
734 done |
692 show "UF.Sigma (rec_eval (rec_all ?rt ?rf)) |
735 show "UF.Sigma (rec_exec (rec_all ?rt ?rf)) |
693 (xs @ [Suc w]) = |
736 (xs @ [Suc w]) = |
694 Minr (\<lambda>args. 0 < rec_eval rf args) xs (Suc w)" |
737 Minr (\<lambda>args. 0 < rec_exec rf args) xs (Suc w)" |
695 apply(auto simp: Sigma_Suc_simp_rewrite ind Minr_Suc_simp) |
738 apply(auto simp: Sigma_Suc_simp_rewrite ind Minr_Suc_simp) |
696 apply(simp_all only: prrf all_lemma) |
739 apply(simp_all only: prrf all_lemma) |
697 apply(auto simp: rec_eval.simps get_fstn_args_take Let_def Minr.simps split: if_splits) |
740 apply(auto simp: rec_exec.simps get_fstn_args_take Let_def Minr.simps split: if_splits) |
698 apply(drule_tac Min_false1, simp, simp, simp) |
741 apply(drule_tac Min_false1, simp, simp, simp) |
699 apply(case_tac "x = Suc w", simp, simp) |
742 apply(case_tac "x = Suc w", simp, simp) |
700 apply(drule_tac Min_false1, simp, simp, simp) |
743 apply(drule_tac Min_false1, simp, simp, simp) |
701 apply(drule_tac Min_false1, simp, simp, simp) |
744 apply(drule_tac Min_false1, simp, simp, simp) |
702 done |
745 done |
703 qed |
746 qed |
704 |
747 |
705 text {* |
748 text {* |
706 The correctness of @{text "rec_Minr"}. |
749 The correctness of @{text "rec_Minr"}. |
707 *} |
750 *} |
708 lemma Minr_lemma: |
751 lemma Minr_lemma: " |
709 assumes h: "primerec rf (Suc (length xs))" |
752 \<lbrakk>primerec rf (Suc (length xs))\<rbrakk> |
710 shows "rec_eval (rec_Minr rf) (xs @ [w]) = Minr (\<lambda> args. (0 < rec_eval rf args)) xs w" |
753 \<Longrightarrow> rec_exec (rec_Minr rf) (xs @ [w]) = |
|
754 Minr (\<lambda> args. (0 < rec_exec rf args)) xs w" |
711 proof - |
755 proof - |
712 let ?rt = "(recf.id (Suc (length xs)) ((length xs)))" |
756 let ?rt = "(recf.id (Suc (length xs)) ((length xs)))" |
713 let ?rf = "(Cn (Suc (Suc (length xs))) |
757 let ?rf = "(Cn (Suc (Suc (length xs))) |
714 rec_not [Cn (Suc (Suc (length xs))) rf |
758 rec_not [Cn (Suc (Suc (length xs))) rf |
715 (get_fstn_args (Suc (Suc (length xs))) (length xs) @ |
759 (get_fstn_args (Suc (Suc (length xs))) (length xs) @ |
716 [recf.id (Suc (Suc (length xs))) |
760 [recf.id (Suc (Suc (length xs))) |
717 (Suc ((length xs)))])])" |
761 (Suc ((length xs)))])])" |
718 let ?rq = "(rec_all ?rt ?rf)" |
762 let ?rq = "(rec_all ?rt ?rf)" |
|
763 assume h: "primerec rf (Suc (length xs))" |
719 have h1: "primerec ?rq (Suc (length xs))" |
764 have h1: "primerec ?rq (Suc (length xs))" |
720 apply(rule_tac primerec_all_iff) |
765 apply(rule_tac primerec_all_iff) |
721 apply(auto simp: h nth_append)+ |
766 apply(auto simp: h nth_append)+ |
722 done |
767 done |
723 moreover have "arity rf = Suc (length xs)" |
768 moreover have "arity rf = Suc (length xs)" |
724 using h by auto |
769 using h by auto |
725 ultimately show "rec_eval (rec_Minr rf) (xs @ [w]) = Minr (\<lambda> args. (0 < rec_eval rf args)) xs w" |
770 ultimately show "rec_exec (rec_Minr rf) (xs @ [w]) = |
726 apply(simp add: rec_eval.simps rec_Minr.simps arity.simps Let_def |
771 Minr (\<lambda> args. (0 < rec_exec rf args)) xs w" |
|
772 apply(simp add: rec_exec.simps rec_Minr.simps arity.simps Let_def |
727 sigma_lemma all_lemma) |
773 sigma_lemma all_lemma) |
728 apply(rule_tac sigma_minr_lemma) |
774 apply(rule_tac sigma_minr_lemma) |
729 apply(simp add: h) |
775 apply(simp add: h) |
730 done |
776 done |
731 qed |
777 qed |
808 apply(rule_tac prime_cn, auto) |
855 apply(rule_tac prime_cn, auto) |
809 apply(case_tac i, auto) |
856 apply(case_tac i, auto) |
810 done |
857 done |
811 |
858 |
812 lemma [simp]: |
859 lemma [simp]: |
813 "length ys = Suc n \<Longrightarrow> (take n ys @ [ys ! n, ys ! n]) = ys @ [ys ! n]" |
860 "length ys = Suc n \<Longrightarrow> (take n ys @ [ys ! n, ys ! n]) = |
|
861 ys @ [ys ! n]" |
814 apply(simp) |
862 apply(simp) |
815 apply(subgoal_tac "\<exists> xs y. ys = xs @ [y]", auto) |
863 apply(subgoal_tac "\<exists> xs y. ys = xs @ [y]", auto) |
816 apply(rule_tac x = "butlast ys" in exI, rule_tac x = "last ys" in exI) |
864 apply(rule_tac x = "butlast ys" in exI, rule_tac x = "last ys" in exI) |
817 apply(case_tac "ys = []", simp_all) |
865 apply(case_tac "ys = []", simp_all) |
818 done |
866 done |
819 |
867 |
820 lemma Maxr_Suc_simp: |
868 lemma Maxr_Suc_simp: |
821 "Maxr Rr xs (Suc w) =(if Rr (xs @ [Suc w]) then Suc w else Maxr Rr xs w)" |
869 "Maxr Rr xs (Suc w) =(if Rr (xs @ [Suc w]) then Suc w |
|
870 else Maxr Rr xs w)" |
822 apply(auto simp: Maxr.simps Max.insert) |
871 apply(auto simp: Maxr.simps Max.insert) |
823 apply(rule_tac Max_eqI, auto) |
872 apply(rule_tac Max_eqI, auto) |
824 done |
873 done |
825 |
874 |
826 lemma [simp]: "min (Suc n) n = n" by simp |
875 lemma [simp]: "min (Suc n) n = n" by simp |
827 |
876 |
828 lemma Sigma_0: "\<forall> i \<le> n. (f (xs @ [i]) = 0) \<Longrightarrow> Sigma f (xs @ [n]) = 0" |
877 lemma Sigma_0: "\<forall> i \<le> n. (f (xs @ [i]) = 0) \<Longrightarrow> |
|
878 Sigma f (xs @ [n]) = 0" |
829 apply(induct n, simp add: Sigma.simps) |
879 apply(induct n, simp add: Sigma.simps) |
830 apply(simp add: Sigma_Suc_simp_rewrite) |
880 apply(simp add: Sigma_Suc_simp_rewrite) |
831 done |
881 done |
832 |
882 |
833 lemma [elim]: "\<forall>k<Suc w. f (xs @ [k]) = Suc 0 \<Longrightarrow> Sigma f (xs @ [w]) = Suc w" |
883 lemma [elim]: "\<forall>k<Suc w. f (xs @ [k]) = Suc 0 |
|
884 \<Longrightarrow> Sigma f (xs @ [w]) = Suc w" |
834 apply(induct w) |
885 apply(induct w) |
835 apply(simp add: Sigma.simps, simp) |
886 apply(simp add: Sigma.simps, simp) |
836 apply(simp add: Sigma.simps) |
887 apply(simp add: Sigma.simps) |
837 done |
888 done |
838 |
889 |
845 apply(case_tac "ma = Suc w", auto) |
896 apply(case_tac "ma = Suc w", auto) |
846 done |
897 done |
847 |
898 |
848 lemma Sigma_Max_lemma: |
899 lemma Sigma_Max_lemma: |
849 assumes prrf: "primerec rf (Suc (length xs))" |
900 assumes prrf: "primerec rf (Suc (length xs))" |
850 shows "UF.Sigma (rec_eval (Cn (Suc (Suc (length xs))) rec_not |
901 shows "UF.Sigma (rec_exec (Cn (Suc (Suc (length xs))) rec_not |
851 [rec_all (recf.id (Suc (Suc (length xs))) (length xs)) |
902 [rec_all (recf.id (Suc (Suc (length xs))) (length xs)) |
852 (Cn (Suc (Suc (Suc (length xs)))) rec_disj |
903 (Cn (Suc (Suc (Suc (length xs)))) rec_disj |
853 [Cn (Suc (Suc (Suc (length xs)))) rec_le |
904 [Cn (Suc (Suc (Suc (length xs)))) rec_le |
854 [recf.id (Suc (Suc (Suc (length xs)))) (Suc (Suc (length xs))), |
905 [recf.id (Suc (Suc (Suc (length xs)))) (Suc (Suc (length xs))), |
855 recf.id (Suc (Suc (Suc (length xs)))) (Suc (length xs))], |
906 recf.id (Suc (Suc (Suc (length xs)))) (Suc (length xs))], |
856 Cn (Suc (Suc (Suc (length xs)))) rec_not |
907 Cn (Suc (Suc (Suc (length xs)))) rec_not |
857 [Cn (Suc (Suc (Suc (length xs)))) rf |
908 [Cn (Suc (Suc (Suc (length xs)))) rf |
858 (get_fstn_args (Suc (Suc (Suc (length xs)))) (length xs) @ |
909 (get_fstn_args (Suc (Suc (Suc (length xs)))) (length xs) @ |
859 [recf.id (Suc (Suc (Suc (length xs)))) (Suc (Suc (length xs)))])]])])) |
910 [recf.id (Suc (Suc (Suc (length xs)))) (Suc (Suc (length xs)))])]])])) |
860 ((xs @ [w]) @ [w]) = |
911 ((xs @ [w]) @ [w]) = |
861 Maxr (\<lambda>args. 0 < rec_eval rf args) xs w" |
912 Maxr (\<lambda>args. 0 < rec_exec rf args) xs w" |
862 proof - |
913 proof - |
863 let ?rt = "(recf.id (Suc (Suc (length xs))) ((length xs)))" |
914 let ?rt = "(recf.id (Suc (Suc (length xs))) ((length xs)))" |
864 let ?rf1 = "Cn (Suc (Suc (Suc (length xs)))) |
915 let ?rf1 = "Cn (Suc (Suc (Suc (length xs)))) |
865 rec_le [recf.id (Suc (Suc (Suc (length xs)))) |
916 rec_le [recf.id (Suc (Suc (Suc (length xs)))) |
866 ((Suc (Suc (length xs)))), recf.id |
917 ((Suc (Suc (length xs)))), recf.id |
874 let ?rf = "Cn (Suc (Suc (Suc (length xs)))) rec_disj [?rf1, ?rf3]" |
925 let ?rf = "Cn (Suc (Suc (Suc (length xs)))) rec_disj [?rf1, ?rf3]" |
875 let ?rq = "rec_all ?rt ?rf" |
926 let ?rq = "rec_all ?rt ?rf" |
876 let ?notrq = "Cn (Suc (Suc (length xs))) rec_not [?rq]" |
927 let ?notrq = "Cn (Suc (Suc (length xs))) rec_not [?rq]" |
877 show "?thesis" |
928 show "?thesis" |
878 proof(auto simp: Maxr.simps) |
929 proof(auto simp: Maxr.simps) |
879 assume h: "\<forall>x\<le>w. rec_eval rf (xs @ [x]) = 0" |
930 assume h: "\<forall>x\<le>w. rec_exec rf (xs @ [x]) = 0" |
880 have "primerec ?rf (Suc (length (xs @ [w, i]))) \<and> |
931 have "primerec ?rf (Suc (length (xs @ [w, i]))) \<and> |
881 primerec ?rt (length (xs @ [w, i]))" |
932 primerec ?rt (length (xs @ [w, i]))" |
882 using prrf |
933 using prrf |
883 apply(auto) |
934 apply(auto) |
884 apply(case_tac i, auto) |
935 apply(case_tac i, auto) |
885 apply(case_tac ia, auto simp: h nth_append) |
936 apply(case_tac ia, auto simp: h nth_append) |
886 done |
937 done |
887 hence "Sigma (rec_eval ?notrq) ((xs@[w])@[w]) = 0" |
938 hence "Sigma (rec_exec ?notrq) ((xs@[w])@[w]) = 0" |
888 apply(rule_tac Sigma_0) |
939 apply(rule_tac Sigma_0) |
889 apply(auto simp: rec_eval.simps all_lemma |
940 apply(auto simp: rec_exec.simps all_lemma |
890 get_fstn_args_take nth_append h) |
941 get_fstn_args_take nth_append h) |
891 done |
942 done |
892 thus "UF.Sigma (rec_eval ?notrq) |
943 thus "UF.Sigma (rec_exec ?notrq) |
893 (xs @ [w, w]) = 0" |
944 (xs @ [w, w]) = 0" |
894 by simp |
945 by simp |
895 next |
946 next |
896 fix x |
947 fix x |
897 assume h: "x \<le> w" "0 < rec_eval rf (xs @ [x])" |
948 assume h: "x \<le> w" "0 < rec_exec rf (xs @ [x])" |
898 hence "\<exists> ma. Max {y. y \<le> w \<and> 0 < rec_eval rf (xs @ [y])} = ma" |
949 hence "\<exists> ma. Max {y. y \<le> w \<and> 0 < rec_exec rf (xs @ [y])} = ma" |
899 by auto |
950 by auto |
900 from this obtain ma where k1: |
951 from this obtain ma where k1: |
901 "Max {y. y \<le> w \<and> 0 < rec_eval rf (xs @ [y])} = ma" .. |
952 "Max {y. y \<le> w \<and> 0 < rec_exec rf (xs @ [y])} = ma" .. |
902 hence k2: "ma \<le> w \<and> 0 < rec_eval rf (xs @ [ma])" |
953 hence k2: "ma \<le> w \<and> 0 < rec_exec rf (xs @ [ma])" |
903 using h |
954 using h |
904 apply(subgoal_tac |
955 apply(subgoal_tac |
905 "Max {y. y \<le> w \<and> 0 < rec_eval rf (xs @ [y])} \<in> {y. y \<le> w \<and> 0 < rec_eval rf (xs @ [y])}") |
956 "Max {y. y \<le> w \<and> 0 < rec_exec rf (xs @ [y])} \<in> {y. y \<le> w \<and> 0 < rec_exec rf (xs @ [y])}") |
906 apply(erule_tac CollectE, simp) |
957 apply(erule_tac CollectE, simp) |
907 apply(rule_tac Max_in, auto) |
958 apply(rule_tac Max_in, auto) |
908 done |
959 done |
909 hence k3: "\<forall> k < ma. (rec_eval ?notrq (xs @ [w, k]) = 1)" |
960 hence k3: "\<forall> k < ma. (rec_exec ?notrq (xs @ [w, k]) = 1)" |
910 apply(auto simp: nth_append) |
961 apply(auto simp: nth_append) |
911 apply(subgoal_tac "primerec ?rf (Suc (length (xs @ [w, k]))) \<and> |
962 apply(subgoal_tac "primerec ?rf (Suc (length (xs @ [w, k]))) \<and> |
912 primerec ?rt (length (xs @ [w, k]))") |
963 primerec ?rt (length (xs @ [w, k]))") |
913 apply(auto simp: rec_eval.simps all_lemma get_fstn_args_take nth_append) |
964 apply(auto simp: rec_exec.simps all_lemma get_fstn_args_take nth_append) |
914 using prrf |
965 using prrf |
915 apply(case_tac i, auto) |
966 apply(case_tac i, auto) |
916 apply(case_tac ia, auto simp: h nth_append) |
967 apply(case_tac ia, auto simp: h nth_append) |
917 done |
968 done |
918 have k4: "\<forall> k \<ge> ma. (rec_eval ?notrq (xs @ [w, k]) = 0)" |
969 have k4: "\<forall> k \<ge> ma. (rec_exec ?notrq (xs @ [w, k]) = 0)" |
919 apply(auto) |
970 apply(auto) |
920 apply(subgoal_tac "primerec ?rf (Suc (length (xs @ [w, k]))) \<and> |
971 apply(subgoal_tac "primerec ?rf (Suc (length (xs @ [w, k]))) \<and> |
921 primerec ?rt (length (xs @ [w, k]))") |
972 primerec ?rt (length (xs @ [w, k]))") |
922 apply(auto simp: rec_eval.simps all_lemma get_fstn_args_take nth_append) |
973 apply(auto simp: rec_exec.simps all_lemma get_fstn_args_take nth_append) |
923 apply(subgoal_tac "x \<le> Max {y. y \<le> w \<and> 0 < rec_eval rf (xs @ [y])}", |
974 apply(subgoal_tac "x \<le> Max {y. y \<le> w \<and> 0 < rec_exec rf (xs @ [y])}", |
924 simp add: k1) |
975 simp add: k1) |
925 apply(rule_tac Max_ge, auto) |
976 apply(rule_tac Max_ge, auto) |
926 using prrf |
977 using prrf |
927 apply(case_tac i, auto) |
978 apply(case_tac i, auto) |
928 apply(case_tac ia, auto simp: h nth_append) |
979 apply(case_tac ia, auto simp: h nth_append) |
929 done |
980 done |
930 from k3 k4 k1 have "Sigma (rec_eval ?notrq) ((xs @ [w]) @ [w]) = ma" |
981 from k3 k4 k1 have "Sigma (rec_exec ?notrq) ((xs @ [w]) @ [w]) = ma" |
931 apply(rule_tac Sigma_max_point, simp, simp, simp add: k2) |
982 apply(rule_tac Sigma_max_point, simp, simp, simp add: k2) |
932 done |
983 done |
933 from k1 and this show "Sigma (rec_eval ?notrq) (xs @ [w, w]) = |
984 from k1 and this show "Sigma (rec_exec ?notrq) (xs @ [w, w]) = |
934 Max {y. y \<le> w \<and> 0 < rec_eval rf (xs @ [y])}" |
985 Max {y. y \<le> w \<and> 0 < rec_exec rf (xs @ [y])}" |
935 by simp |
986 by simp |
936 qed |
987 qed |
937 qed |
988 qed |
938 |
989 |
939 text {* |
990 text {* |
940 The correctness of @{text "rec_maxr"}. |
991 The correctness of @{text "rec_maxr"}. |
941 *} |
992 *} |
942 lemma Maxr_lemma: |
993 lemma Maxr_lemma: |
943 assumes h: "primerec rf (Suc (length xs))" |
994 assumes h: "primerec rf (Suc (length xs))" |
944 shows "rec_eval (rec_maxr rf) (xs @ [w]) = |
995 shows "rec_exec (rec_maxr rf) (xs @ [w]) = |
945 Maxr (\<lambda> args. 0 < rec_eval rf args) xs w" |
996 Maxr (\<lambda> args. 0 < rec_exec rf args) xs w" |
946 proof - |
997 proof - |
947 from h have "arity rf = Suc (length xs)" |
998 from h have "arity rf = Suc (length xs)" |
948 by auto |
999 by auto |
949 thus "?thesis" |
1000 thus "?thesis" |
950 proof(simp add: rec_eval.simps rec_maxr.simps nth_append get_fstn_args_take) |
1001 proof(simp add: rec_exec.simps rec_maxr.simps nth_append get_fstn_args_take) |
951 let ?rt = "(recf.id (Suc (Suc (length xs))) ((length xs)))" |
1002 let ?rt = "(recf.id (Suc (Suc (length xs))) ((length xs)))" |
952 let ?rf1 = "Cn (Suc (Suc (Suc (length xs)))) |
1003 let ?rf1 = "Cn (Suc (Suc (Suc (length xs)))) |
953 rec_le [recf.id (Suc (Suc (Suc (length xs)))) |
1004 rec_le [recf.id (Suc (Suc (Suc (length xs)))) |
954 ((Suc (Suc (length xs)))), recf.id |
1005 ((Suc (Suc (length xs)))), recf.id |
955 (Suc (Suc (Suc (length xs)))) ((Suc (length xs)))]" |
1006 (Suc (Suc (Suc (length xs)))) ((Suc (length xs)))]" |
974 from prt and prrf have prrq: "primerec ?rq |
1025 from prt and prrf have prrq: "primerec ?rq |
975 (Suc (Suc (length xs)))" |
1026 (Suc (Suc (length xs)))" |
976 by(erule_tac primerec_all_iff, auto) |
1027 by(erule_tac primerec_all_iff, auto) |
977 hence prnotrp: "primerec ?notrq (Suc (length ((xs @ [w]))))" |
1028 hence prnotrp: "primerec ?notrq (Suc (length ((xs @ [w]))))" |
978 by(rule_tac prime_cn, auto) |
1029 by(rule_tac prime_cn, auto) |
979 have g1: "rec_eval (rec_sigma ?notrq) ((xs @ [w]) @ [w]) |
1030 have g1: "rec_exec (rec_sigma ?notrq) ((xs @ [w]) @ [w]) |
980 = Maxr (\<lambda>args. 0 < rec_eval rf args) xs w" |
1031 = Maxr (\<lambda>args. 0 < rec_exec rf args) xs w" |
981 using prnotrp |
1032 using prnotrp |
982 using sigma_lemma |
1033 using sigma_lemma |
983 apply(simp only: sigma_lemma) |
1034 apply(simp only: sigma_lemma) |
984 apply(rule_tac Sigma_Max_lemma) |
1035 apply(rule_tac Sigma_Max_lemma) |
985 apply(simp add: h) |
1036 apply(simp add: h) |
986 done |
1037 done |
987 thus "rec_eval (rec_sigma ?notrq) |
1038 thus "rec_exec (rec_sigma ?notrq) |
988 (xs @ [w, w]) = |
1039 (xs @ [w, w]) = |
989 Maxr (\<lambda>args. 0 < rec_eval rf args) xs w" |
1040 Maxr (\<lambda>args. 0 < rec_exec rf args) xs w" |
990 apply(simp) |
1041 apply(simp) |
991 done |
1042 done |
992 qed |
1043 qed |
993 qed |
1044 qed |
994 |
1045 |
995 text {* |
1046 text {* |
996 @{text "quo"} is the formal specification of division. |
1047 @{text "quo"} is the formal specification of division. |
997 *} |
1048 *} |
998 fun quo :: "nat list \<Rightarrow> nat" |
1049 fun quo :: "nat list \<Rightarrow> nat" |
999 where |
1050 where |
1000 "quo [x, y] = |
1051 "quo [x, y] = (let Rr = |
1001 (let Rr = (\<lambda> zs. ((zs ! (Suc 0) * zs ! (Suc (Suc 0)) \<le> zs ! 0) \<and> zs ! Suc 0 \<noteq> 0)) |
1052 (\<lambda> zs. ((zs ! (Suc 0) * zs ! (Suc (Suc 0)) |
1002 in Maxr Rr [x, y] x)" |
1053 \<le> zs ! 0) \<and> zs ! Suc 0 \<noteq> (0::nat))) |
|
1054 in Maxr Rr [x, y] x)" |
1003 |
1055 |
1004 declare quo.simps[simp del] |
1056 declare quo.simps[simp del] |
1005 |
1057 |
1006 text {* |
1058 text {* |
1007 The following lemmas shows more directly the menaing of @{text "quo"}: |
1059 The following lemmas shows more directly the menaing of @{text "quo"}: |
1077 apply(rule_tac prime_cn, auto)+ |
1131 apply(rule_tac prime_cn, auto)+ |
1078 apply(case_tac i, auto intro: prime_id) |
1132 apply(case_tac i, auto intro: prime_id) |
1079 done |
1133 done |
1080 |
1134 |
1081 |
1135 |
1082 lemma quo_lemma1: "rec_eval rec_quo [x, y] = quo [x, y]" |
1136 lemma quo_lemma1: "rec_exec rec_quo [x, y] = quo [x, y]" |
1083 proof(simp add: rec_eval.simps rec_quo_def) |
1137 proof(simp add: rec_exec.simps rec_quo_def) |
1084 let ?rR = "(Cn (Suc (Suc (Suc 0))) rec_conj |
1138 let ?rR = "(Cn (Suc (Suc (Suc 0))) rec_conj |
1085 [Cn (Suc (Suc (Suc 0))) rec_le |
1139 [Cn (Suc (Suc (Suc 0))) rec_le |
1086 [Cn (Suc (Suc (Suc 0))) rec_mult |
1140 [Cn (Suc (Suc (Suc 0))) rec_mult |
1087 [recf.id (Suc (Suc (Suc 0))) (Suc (0)), |
1141 [recf.id (Suc (Suc (Suc 0))) (Suc (0)), |
1088 recf.id (Suc (Suc (Suc 0))) (Suc (Suc (0)))], |
1142 recf.id (Suc (Suc (Suc 0))) (Suc (Suc (0)))], |
1089 recf.id (Suc (Suc (Suc 0))) (0)], |
1143 recf.id (Suc (Suc (Suc 0))) (0)], |
1090 Cn (Suc (Suc (Suc 0))) rec_noteq |
1144 Cn (Suc (Suc (Suc 0))) rec_noteq |
1091 [recf.id (Suc (Suc (Suc 0))) |
1145 [recf.id (Suc (Suc (Suc 0))) |
1092 (Suc (0)), Cn (Suc (Suc (Suc 0))) (constn 0) |
1146 (Suc (0)), Cn (Suc (Suc (Suc 0))) (constn 0) |
1093 [recf.id (Suc (Suc (Suc 0))) (0)]]])" |
1147 [recf.id (Suc (Suc (Suc 0))) (0)]]])" |
1094 have "rec_eval (rec_maxr ?rR) ([x, y]@ [ x]) = Maxr (\<lambda> args. 0 < rec_eval ?rR args) [x, y] x" |
1148 have "rec_exec (rec_maxr ?rR) ([x, y]@ [ x]) = Maxr (\<lambda> args. 0 < rec_exec ?rR args) [x, y] x" |
1095 proof(rule_tac Maxr_lemma, simp) |
1149 proof(rule_tac Maxr_lemma, simp) |
1096 show "primerec ?rR (Suc (Suc (Suc 0)))" |
1150 show "primerec ?rR (Suc (Suc (Suc 0)))" |
1097 apply(auto) |
1151 apply(auto) |
1098 apply(case_tac i, auto) |
1152 apply(case_tac i, auto) |
1099 apply(case_tac [!] ia, auto) |
1153 apply(case_tac [!] ia, auto) |
1100 apply(case_tac i, auto) |
1154 apply(case_tac i, auto) |
1101 done |
1155 done |
1102 qed |
1156 qed |
1103 hence g1: "rec_eval (rec_maxr ?rR) ([x, y, x]) = |
1157 hence g1: "rec_exec (rec_maxr ?rR) ([x, y, x]) = |
1104 Maxr (\<lambda> args. if rec_eval ?rR args = 0 then False |
1158 Maxr (\<lambda> args. if rec_exec ?rR args = 0 then False |
1105 else True) [x, y] x" |
1159 else True) [x, y] x" |
1106 by simp |
1160 by simp |
1107 have g2: "Maxr (\<lambda> args. if rec_eval ?rR args = 0 then False |
1161 have g2: "Maxr (\<lambda> args. if rec_exec ?rR args = 0 then False |
1108 else True) [x, y] x = quo [x, y]" |
1162 else True) [x, y] x = quo [x, y]" |
1109 apply(simp add: rec_eval.simps) |
1163 apply(simp add: rec_exec.simps) |
1110 apply(simp add: Maxr.simps quo.simps, auto) |
1164 apply(simp add: Maxr.simps quo.simps, auto) |
1111 done |
1165 done |
1112 from g1 and g2 show |
1166 from g1 and g2 show |
1113 "rec_eval (rec_maxr ?rR) ([x, y, x]) = quo [x, y]" |
1167 "rec_exec (rec_maxr ?rR) ([x, y, x]) = quo [x, y]" |
1114 by simp |
1168 by simp |
1115 qed |
1169 qed |
1116 |
1170 |
1117 text {* |
1171 text {* |
1118 The correctness of @{text "quo"}. |
1172 The correctness of @{text "quo"}. |
1119 *} |
1173 *} |
1120 lemma quo_lemma2: "rec_eval rec_quo [x, y] = x div y" |
1174 lemma quo_lemma2: "rec_exec rec_quo [x, y] = x div y" |
1121 using quo_lemma1[of x y] quo_div[of x y] |
1175 using quo_lemma1[of x y] quo_div[of x y] |
1122 by simp |
1176 by simp |
1123 |
1177 |
1124 text {* |
1178 text {* |
1125 @{text "rec_mod"} is the recursive function used to implement |
1179 @{text "rec_mod"} is the recursive function used to implement |
1176 rec_embranch' ((rg, rc) # rgcs) vl)" |
1229 rec_embranch' ((rg, rc) # rgcs) vl)" |
1177 |
1230 |
1178 declare Embranch.simps[simp del] rec_embranch.simps[simp del] |
1231 declare Embranch.simps[simp del] rec_embranch.simps[simp del] |
1179 |
1232 |
1180 lemma embranch_all0: |
1233 lemma embranch_all0: |
1181 "\<lbrakk>\<forall> j < length rcs. rec_eval (rcs ! j) xs = 0; |
1234 "\<lbrakk>\<forall> j < length rcs. rec_exec (rcs ! j) xs = 0; |
1182 length rgs = length rcs; |
1235 length rgs = length rcs; |
1183 rcs \<noteq> []; |
1236 rcs \<noteq> []; |
1184 list_all (\<lambda> rf. primerec rf (length xs)) (rgs @ rcs)\<rbrakk> \<Longrightarrow> |
1237 list_all (\<lambda> rf. primerec rf (length xs)) (rgs @ rcs)\<rbrakk> \<Longrightarrow> |
1185 rec_eval (rec_embranch (zip rgs rcs)) xs = 0" |
1238 rec_exec (rec_embranch (zip rgs rcs)) xs = 0" |
1186 proof(induct rcs arbitrary: rgs, simp, case_tac rgs, simp) |
1239 proof(induct rcs arbitrary: rgs, simp, case_tac rgs, simp) |
1187 fix a rcs rgs aa list |
1240 fix a rcs rgs aa list |
1188 assume ind: |
1241 assume ind: |
1189 "\<And>rgs. \<lbrakk>\<forall>j<length rcs. rec_eval (rcs ! j) xs = 0; |
1242 "\<And>rgs. \<lbrakk>\<forall>j<length rcs. rec_exec (rcs ! j) xs = 0; |
1190 length rgs = length rcs; rcs \<noteq> []; |
1243 length rgs = length rcs; rcs \<noteq> []; |
1191 list_all (\<lambda>rf. primerec rf (length xs)) (rgs @ rcs)\<rbrakk> \<Longrightarrow> |
1244 list_all (\<lambda>rf. primerec rf (length xs)) (rgs @ rcs)\<rbrakk> \<Longrightarrow> |
1192 rec_eval (rec_embranch (zip rgs rcs)) xs = 0" |
1245 rec_exec (rec_embranch (zip rgs rcs)) xs = 0" |
1193 and h: "\<forall>j<length (a # rcs). rec_eval ((a # rcs) ! j) xs = 0" |
1246 and h: "\<forall>j<length (a # rcs). rec_exec ((a # rcs) ! j) xs = 0" |
1194 "length rgs = length (a # rcs)" |
1247 "length rgs = length (a # rcs)" |
1195 "a # rcs \<noteq> []" |
1248 "a # rcs \<noteq> []" |
1196 "list_all (\<lambda>rf. primerec rf (length xs)) (rgs @ a # rcs)" |
1249 "list_all (\<lambda>rf. primerec rf (length xs)) (rgs @ a # rcs)" |
1197 "rgs = aa # list" |
1250 "rgs = aa # list" |
1198 have g: "rcs \<noteq> [] \<Longrightarrow> rec_eval (rec_embranch (zip list rcs)) xs = 0" |
1251 have g: "rcs \<noteq> [] \<Longrightarrow> rec_exec (rec_embranch (zip list rcs)) xs = 0" |
1199 using h |
1252 using h |
1200 by(rule_tac ind, auto) |
1253 by(rule_tac ind, auto) |
1201 show "rec_eval (rec_embranch (zip rgs (a # rcs))) xs = 0" |
1254 show "rec_exec (rec_embranch (zip rgs (a # rcs))) xs = 0" |
1202 proof(case_tac "rcs = []", simp) |
1255 proof(case_tac "rcs = []", simp) |
1203 show "rec_eval (rec_embranch (zip rgs [a])) xs = 0" |
1256 show "rec_exec (rec_embranch (zip rgs [a])) xs = 0" |
1204 using h |
1257 using h |
1205 apply(simp add: rec_embranch.simps rec_eval.simps) |
1258 apply(simp add: rec_embranch.simps rec_exec.simps) |
1206 apply(erule_tac x = 0 in allE, simp) |
1259 apply(erule_tac x = 0 in allE, simp) |
1207 done |
1260 done |
1208 next |
1261 next |
1209 assume "rcs \<noteq> []" |
1262 assume "rcs \<noteq> []" |
1210 hence "rec_eval (rec_embranch (zip list rcs)) xs = 0" |
1263 hence "rec_exec (rec_embranch (zip list rcs)) xs = 0" |
1211 using g by simp |
1264 using g by simp |
1212 thus "rec_eval (rec_embranch (zip rgs (a # rcs))) xs = 0" |
1265 thus "rec_exec (rec_embranch (zip rgs (a # rcs))) xs = 0" |
1213 using h |
1266 using h |
1214 apply(simp add: rec_embranch.simps rec_eval.simps) |
1267 apply(simp add: rec_embranch.simps rec_exec.simps) |
1215 apply(case_tac rcs, |
1268 apply(case_tac rcs, |
1216 auto simp: rec_eval.simps rec_embranch.simps) |
1269 auto simp: rec_exec.simps rec_embranch.simps) |
1217 apply(case_tac list, |
1270 apply(case_tac list, |
1218 auto simp: rec_eval.simps rec_embranch.simps) |
1271 auto simp: rec_exec.simps rec_embranch.simps) |
1219 done |
1272 done |
1220 qed |
1273 qed |
1221 qed |
1274 qed |
1222 |
1275 |
1223 |
1276 |
1224 lemma embranch_exec_0: "\<lbrakk>rec_eval aa xs = 0; zip rgs list \<noteq> []; |
1277 lemma embranch_exec_0: "\<lbrakk>rec_exec aa xs = 0; zip rgs list \<noteq> []; |
1225 list_all (\<lambda> rf. primerec rf (length xs)) ([a, aa] @ rgs @ list)\<rbrakk> |
1278 list_all (\<lambda> rf. primerec rf (length xs)) ([a, aa] @ rgs @ list)\<rbrakk> |
1226 \<Longrightarrow> rec_eval (rec_embranch ((a, aa) # zip rgs list)) xs |
1279 \<Longrightarrow> rec_exec (rec_embranch ((a, aa) # zip rgs list)) xs |
1227 = rec_eval (rec_embranch (zip rgs list)) xs" |
1280 = rec_exec (rec_embranch (zip rgs list)) xs" |
1228 apply(simp add: rec_eval.simps rec_embranch.simps) |
1281 apply(simp add: rec_exec.simps rec_embranch.simps) |
1229 apply(case_tac "zip rgs list", simp, case_tac ab, |
1282 apply(case_tac "zip rgs list", simp, case_tac ab, |
1230 simp add: rec_embranch.simps rec_eval.simps) |
1283 simp add: rec_embranch.simps rec_exec.simps) |
1231 apply(subgoal_tac "arity a = length xs", auto) |
1284 apply(subgoal_tac "arity a = length xs", auto) |
1232 apply(subgoal_tac "arity aaa = length xs", auto) |
1285 apply(subgoal_tac "arity aaa = length xs", auto) |
1233 apply(case_tac rgs, simp, case_tac list, simp, simp) |
1286 apply(case_tac rgs, simp, case_tac list, simp, simp) |
1234 done |
1287 done |
1235 |
1288 |
1242 apply(case_tac xs, simp, simp) |
1295 apply(case_tac xs, simp, simp) |
1243 done |
1296 done |
1244 |
1297 |
1245 lemma Embranch_0: |
1298 lemma Embranch_0: |
1246 "\<lbrakk>length rgs = k; length rcs = k; k > 0; |
1299 "\<lbrakk>length rgs = k; length rcs = k; k > 0; |
1247 \<forall> j < k. rec_eval (rcs ! j) xs = 0\<rbrakk> \<Longrightarrow> |
1300 \<forall> j < k. rec_exec (rcs ! j) xs = 0\<rbrakk> \<Longrightarrow> |
1248 Embranch (zip (map rec_eval rgs) (map (\<lambda>r args. 0 < rec_eval r args) rcs)) xs = 0" |
1301 Embranch (zip (map rec_exec rgs) (map (\<lambda>r args. 0 < rec_exec r args) rcs)) xs = 0" |
1249 proof(induct rgs arbitrary: rcs k, simp, simp) |
1302 proof(induct rgs arbitrary: rcs k, simp, simp) |
1250 fix a rgs rcs k |
1303 fix a rgs rcs k |
1251 assume ind: |
1304 assume ind: |
1252 "\<And>rcs k. \<lbrakk>length rgs = k; length rcs = k; 0 < k; \<forall>j<k. rec_eval (rcs ! j) xs = 0\<rbrakk> |
1305 "\<And>rcs k. \<lbrakk>length rgs = k; length rcs = k; 0 < k; \<forall>j<k. rec_exec (rcs ! j) xs = 0\<rbrakk> |
1253 \<Longrightarrow> Embranch (zip (map rec_eval rgs) (map (\<lambda>r args. 0 < rec_eval r args) rcs)) xs = 0" |
1306 \<Longrightarrow> Embranch (zip (map rec_exec rgs) (map (\<lambda>r args. 0 < rec_exec r args) rcs)) xs = 0" |
1254 and h: "Suc (length rgs) = k" "length rcs = k" |
1307 and h: "Suc (length rgs) = k" "length rcs = k" |
1255 "\<forall>j<k. rec_eval (rcs ! j) xs = 0" |
1308 "\<forall>j<k. rec_exec (rcs ! j) xs = 0" |
1256 from h show |
1309 from h show |
1257 "Embranch (zip (rec_eval a # map rec_eval rgs) |
1310 "Embranch (zip (rec_exec a # map rec_exec rgs) |
1258 (map (\<lambda>r args. 0 < rec_eval r args) rcs)) xs = 0" |
1311 (map (\<lambda>r args. 0 < rec_exec r args) rcs)) xs = 0" |
1259 apply(case_tac rcs, simp, case_tac "rgs = []", simp) |
1312 apply(case_tac rcs, simp, case_tac "rgs = []", simp) |
1260 apply(simp add: Embranch.simps) |
1313 apply(simp add: Embranch.simps) |
1261 apply(erule_tac x = 0 in allE, simp) |
1314 apply(erule_tac x = 0 in allE, simp) |
1262 apply(simp add: Embranch.simps) |
1315 apply(simp add: Embranch.simps) |
1263 apply(erule_tac x = 0 in all_dupE, simp) |
1316 apply(erule_tac x = 0 in all_dupE, simp) |
1271 *} |
1324 *} |
1272 lemma embranch_lemma: |
1325 lemma embranch_lemma: |
1273 assumes branch_num: |
1326 assumes branch_num: |
1274 "length rgs = n" "length rcs = n" "n > 0" |
1327 "length rgs = n" "length rcs = n" "n > 0" |
1275 and partition: |
1328 and partition: |
1276 "(\<exists> i < n. (rec_eval (rcs ! i) xs = 1 \<and> (\<forall> j < n. j \<noteq> i \<longrightarrow> |
1329 "(\<exists> i < n. (rec_exec (rcs ! i) xs = 1 \<and> (\<forall> j < n. j \<noteq> i \<longrightarrow> |
1277 rec_eval (rcs ! j) xs = 0)))" |
1330 rec_exec (rcs ! j) xs = 0)))" |
1278 and prime_all: "list_all (\<lambda> rf. primerec rf (length xs)) (rgs @ rcs)" |
1331 and prime_all: "list_all (\<lambda> rf. primerec rf (length xs)) (rgs @ rcs)" |
1279 shows "rec_eval (rec_embranch (zip rgs rcs)) xs = |
1332 shows "rec_exec (rec_embranch (zip rgs rcs)) xs = |
1280 Embranch (zip (map rec_eval rgs) |
1333 Embranch (zip (map rec_exec rgs) |
1281 (map (\<lambda> r args. 0 < rec_eval r args) rcs)) xs" |
1334 (map (\<lambda> r args. 0 < rec_exec r args) rcs)) xs" |
1282 using branch_num partition prime_all |
1335 using branch_num partition prime_all |
1283 proof(induct rgs arbitrary: rcs n, simp) |
1336 proof(induct rgs arbitrary: rcs n, simp) |
1284 fix a rgs rcs n |
1337 fix a rgs rcs n |
1285 assume ind: |
1338 assume ind: |
1286 "\<And>rcs n. \<lbrakk>length rgs = n; length rcs = n; 0 < n; |
1339 "\<And>rcs n. \<lbrakk>length rgs = n; length rcs = n; 0 < n; |
1287 \<exists>i<n. rec_eval (rcs ! i) xs = 1 \<and> (\<forall>j<n. j \<noteq> i \<longrightarrow> rec_eval (rcs ! j) xs = 0); |
1340 \<exists>i<n. rec_exec (rcs ! i) xs = 1 \<and> (\<forall>j<n. j \<noteq> i \<longrightarrow> rec_exec (rcs ! j) xs = 0); |
1288 list_all (\<lambda>rf. primerec rf (length xs)) (rgs @ rcs)\<rbrakk> |
1341 list_all (\<lambda>rf. primerec rf (length xs)) (rgs @ rcs)\<rbrakk> |
1289 \<Longrightarrow> rec_eval (rec_embranch (zip rgs rcs)) xs = |
1342 \<Longrightarrow> rec_exec (rec_embranch (zip rgs rcs)) xs = |
1290 Embranch (zip (map rec_eval rgs) (map (\<lambda>r args. 0 < rec_eval r args) rcs)) xs" |
1343 Embranch (zip (map rec_exec rgs) (map (\<lambda>r args. 0 < rec_exec r args) rcs)) xs" |
1291 and h: "length (a # rgs) = n" "length (rcs::recf list) = n" "0 < n" |
1344 and h: "length (a # rgs) = n" "length (rcs::recf list) = n" "0 < n" |
1292 " \<exists>i<n. rec_eval (rcs ! i) xs = 1 \<and> |
1345 " \<exists>i<n. rec_exec (rcs ! i) xs = 1 \<and> |
1293 (\<forall>j<n. j \<noteq> i \<longrightarrow> rec_eval (rcs ! j) xs = 0)" |
1346 (\<forall>j<n. j \<noteq> i \<longrightarrow> rec_exec (rcs ! j) xs = 0)" |
1294 "list_all (\<lambda>rf. primerec rf (length xs)) ((a # rgs) @ rcs)" |
1347 "list_all (\<lambda>rf. primerec rf (length xs)) ((a # rgs) @ rcs)" |
1295 from h show "rec_eval (rec_embranch (zip (a # rgs) rcs)) xs = |
1348 from h show "rec_exec (rec_embranch (zip (a # rgs) rcs)) xs = |
1296 Embranch (zip (map rec_eval (a # rgs)) (map (\<lambda>r args. |
1349 Embranch (zip (map rec_exec (a # rgs)) (map (\<lambda>r args. |
1297 0 < rec_eval r args) rcs)) xs" |
1350 0 < rec_exec r args) rcs)) xs" |
1298 apply(case_tac rcs, simp, simp) |
1351 apply(case_tac rcs, simp, simp) |
1299 apply(case_tac "rec_eval aa xs = 0") |
1352 apply(case_tac "rec_exec aa xs = 0") |
1300 apply(case_tac [!] "zip rgs list = []", simp) |
1353 apply(case_tac [!] "zip rgs list = []", simp) |
1301 apply(subgoal_tac "rgs = [] \<and> list = []", simp add: Embranch.simps rec_eval.simps rec_embranch.simps) |
1354 apply(subgoal_tac "rgs = [] \<and> list = []", simp add: Embranch.simps rec_exec.simps rec_embranch.simps) |
1302 apply(rule_tac zip_null_iff, simp, simp, simp) |
1355 apply(rule_tac zip_null_iff, simp, simp, simp) |
1303 proof - |
1356 proof - |
1304 fix aa list |
1357 fix aa list |
1305 assume g: |
1358 assume g: |
1306 "Suc (length rgs) = n" "Suc (length list) = n" |
1359 "Suc (length rgs) = n" "Suc (length list) = n" |
1307 "\<exists>i<n. rec_eval ((aa # list) ! i) xs = Suc 0 \<and> |
1360 "\<exists>i<n. rec_exec ((aa # list) ! i) xs = Suc 0 \<and> |
1308 (\<forall>j<n. j \<noteq> i \<longrightarrow> rec_eval ((aa # list) ! j) xs = 0)" |
1361 (\<forall>j<n. j \<noteq> i \<longrightarrow> rec_exec ((aa # list) ! j) xs = 0)" |
1309 "primerec a (length xs) \<and> |
1362 "primerec a (length xs) \<and> |
1310 list_all (\<lambda>rf. primerec rf (length xs)) rgs \<and> |
1363 list_all (\<lambda>rf. primerec rf (length xs)) rgs \<and> |
1311 primerec aa (length xs) \<and> |
1364 primerec aa (length xs) \<and> |
1312 list_all (\<lambda>rf. primerec rf (length xs)) list" |
1365 list_all (\<lambda>rf. primerec rf (length xs)) list" |
1313 "rec_eval aa xs = 0" "rcs = aa # list" "zip rgs list \<noteq> []" |
1366 "rec_exec aa xs = 0" "rcs = aa # list" "zip rgs list \<noteq> []" |
1314 have "rec_eval (rec_embranch ((a, aa) # zip rgs list)) xs |
1367 have "rec_exec (rec_embranch ((a, aa) # zip rgs list)) xs |
1315 = rec_eval (rec_embranch (zip rgs list)) xs" |
1368 = rec_exec (rec_embranch (zip rgs list)) xs" |
1316 apply(rule embranch_exec_0, simp_all add: g) |
1369 apply(rule embranch_exec_0, simp_all add: g) |
1317 done |
1370 done |
1318 from g and this show "rec_eval (rec_embranch ((a, aa) # zip rgs list)) xs = |
1371 from g and this show "rec_exec (rec_embranch ((a, aa) # zip rgs list)) xs = |
1319 Embranch ((rec_eval a, \<lambda>args. 0 < rec_eval aa args) # |
1372 Embranch ((rec_exec a, \<lambda>args. 0 < rec_exec aa args) # |
1320 zip (map rec_eval rgs) (map (\<lambda>r args. 0 < rec_eval r args) list)) xs" |
1373 zip (map rec_exec rgs) (map (\<lambda>r args. 0 < rec_exec r args) list)) xs" |
1321 apply(simp add: Embranch.simps) |
1374 apply(simp add: Embranch.simps) |
1322 apply(rule_tac n = "n - Suc 0" in ind) |
1375 apply(rule_tac n = "n - Suc 0" in ind) |
1323 apply(case_tac n, simp, simp) |
1376 apply(case_tac n, simp, simp) |
1324 apply(case_tac n, simp, simp) |
1377 apply(case_tac n, simp, simp) |
1325 apply(case_tac n, simp, simp add: zip_null_gr ) |
1378 apply(case_tac n, simp, simp add: zip_null_gr ) |
1329 apply(rule_tac allI, erule_tac x = "Suc j" in allE, simp) |
1382 apply(rule_tac allI, erule_tac x = "Suc j" in allE, simp) |
1330 done |
1383 done |
1331 next |
1384 next |
1332 fix aa list |
1385 fix aa list |
1333 assume g: "Suc (length rgs) = n" "Suc (length list) = n" |
1386 assume g: "Suc (length rgs) = n" "Suc (length list) = n" |
1334 "\<exists>i<n. rec_eval ((aa # list) ! i) xs = Suc 0 \<and> |
1387 "\<exists>i<n. rec_exec ((aa # list) ! i) xs = Suc 0 \<and> |
1335 (\<forall>j<n. j \<noteq> i \<longrightarrow> rec_eval ((aa # list) ! j) xs = 0)" |
1388 (\<forall>j<n. j \<noteq> i \<longrightarrow> rec_exec ((aa # list) ! j) xs = 0)" |
1336 "primerec a (length xs) \<and> list_all (\<lambda>rf. primerec rf (length xs)) rgs \<and> |
1389 "primerec a (length xs) \<and> list_all (\<lambda>rf. primerec rf (length xs)) rgs \<and> |
1337 primerec aa (length xs) \<and> list_all (\<lambda>rf. primerec rf (length xs)) list" |
1390 primerec aa (length xs) \<and> list_all (\<lambda>rf. primerec rf (length xs)) list" |
1338 "rcs = aa # list" "rec_eval aa xs \<noteq> 0" "zip rgs list = []" |
1391 "rcs = aa # list" "rec_exec aa xs \<noteq> 0" "zip rgs list = []" |
1339 thus "rec_eval (rec_embranch ((a, aa) # zip rgs list)) xs = |
1392 thus "rec_exec (rec_embranch ((a, aa) # zip rgs list)) xs = |
1340 Embranch ((rec_eval a, \<lambda>args. 0 < rec_eval aa args) # |
1393 Embranch ((rec_exec a, \<lambda>args. 0 < rec_exec aa args) # |
1341 zip (map rec_eval rgs) (map (\<lambda>r args. 0 < rec_eval r args) list)) xs" |
1394 zip (map rec_exec rgs) (map (\<lambda>r args. 0 < rec_exec r args) list)) xs" |
1342 apply(subgoal_tac "rgs = [] \<and> list = []", simp) |
1395 apply(subgoal_tac "rgs = [] \<and> list = []", simp) |
1343 prefer 2 |
1396 prefer 2 |
1344 apply(rule_tac zip_null_iff, simp, simp, simp) |
1397 apply(rule_tac zip_null_iff, simp, simp, simp) |
1345 apply(simp add: rec_eval.simps rec_embranch.simps Embranch.simps, auto) |
1398 apply(simp add: rec_exec.simps rec_embranch.simps Embranch.simps, auto) |
1346 done |
1399 done |
1347 next |
1400 next |
1348 fix aa list |
1401 fix aa list |
1349 assume g: "Suc (length rgs) = n" "Suc (length list) = n" |
1402 assume g: "Suc (length rgs) = n" "Suc (length list) = n" |
1350 "\<exists>i<n. rec_eval ((aa # list) ! i) xs = Suc 0 \<and> |
1403 "\<exists>i<n. rec_exec ((aa # list) ! i) xs = Suc 0 \<and> |
1351 (\<forall>j<n. j \<noteq> i \<longrightarrow> rec_eval ((aa # list) ! j) xs = 0)" |
1404 (\<forall>j<n. j \<noteq> i \<longrightarrow> rec_exec ((aa # list) ! j) xs = 0)" |
1352 "primerec a (length xs) \<and> list_all (\<lambda>rf. primerec rf (length xs)) rgs |
1405 "primerec a (length xs) \<and> list_all (\<lambda>rf. primerec rf (length xs)) rgs |
1353 \<and> primerec aa (length xs) \<and> list_all (\<lambda>rf. primerec rf (length xs)) list" |
1406 \<and> primerec aa (length xs) \<and> list_all (\<lambda>rf. primerec rf (length xs)) list" |
1354 "rcs = aa # list" "rec_eval aa xs \<noteq> 0" "zip rgs list \<noteq> []" |
1407 "rcs = aa # list" "rec_exec aa xs \<noteq> 0" "zip rgs list \<noteq> []" |
1355 have "rec_eval aa xs = Suc 0" |
1408 have "rec_exec aa xs = Suc 0" |
1356 using g |
1409 using g |
1357 apply(case_tac "rec_eval aa xs", simp, auto) |
1410 apply(case_tac "rec_exec aa xs", simp, auto) |
1358 done |
1411 done |
1359 moreover have "rec_eval (rec_embranch' (zip rgs list) (length xs)) xs = 0" |
1412 moreover have "rec_exec (rec_embranch' (zip rgs list) (length xs)) xs = 0" |
1360 proof - |
1413 proof - |
1361 have "rec_embranch' (zip rgs list) (length xs) = rec_embranch (zip rgs list)" |
1414 have "rec_embranch' (zip rgs list) (length xs) = rec_embranch (zip rgs list)" |
1362 using g |
1415 using g |
1363 apply(case_tac "zip rgs list", simp, case_tac ab) |
1416 apply(case_tac "zip rgs list", simp, case_tac ab) |
1364 apply(simp add: rec_embranch.simps) |
1417 apply(simp add: rec_embranch.simps) |
1365 apply(subgoal_tac "arity aaa = length xs", simp, auto) |
1418 apply(subgoal_tac "arity aaa = length xs", simp, auto) |
1366 apply(case_tac rgs, simp, simp, case_tac list, simp, simp) |
1419 apply(case_tac rgs, simp, simp, case_tac list, simp, simp) |
1367 done |
1420 done |
1368 moreover have "rec_eval (rec_embranch (zip rgs list)) xs = 0" |
1421 moreover have "rec_exec (rec_embranch (zip rgs list)) xs = 0" |
1369 proof(rule embranch_all0) |
1422 proof(rule embranch_all0) |
1370 show " \<forall>j<length list. rec_eval (list ! j) xs = 0" |
1423 show " \<forall>j<length list. rec_exec (list ! j) xs = 0" |
1371 using g |
1424 using g |
1372 apply(auto) |
1425 apply(auto) |
1373 apply(case_tac i, simp) |
1426 apply(case_tac i, simp) |
1374 apply(erule_tac x = "Suc j" in allE, simp) |
1427 apply(erule_tac x = "Suc j" in allE, simp) |
1375 apply(simp) |
1428 apply(simp) |
1442 [Cn 3 rec_mult [id 3 1, id 3 2], id 3 0]))]" |
1503 [Cn 3 rec_mult [id 3 1, id 3 2], id 3 0]))]" |
1443 |
1504 |
1444 declare numeral_2_eq_2[simp del] numeral_3_eq_3[simp del] |
1505 declare numeral_2_eq_2[simp del] numeral_3_eq_3[simp del] |
1445 |
1506 |
1446 lemma exec_tmp: |
1507 lemma exec_tmp: |
1447 "rec_eval (rec_all (Cn 2 rec_minus [recf.id 2 0, Cn 2 (constn (Suc 0)) [recf.id 2 0]]) |
1508 "rec_exec (rec_all (Cn 2 rec_minus [recf.id 2 0, Cn 2 (constn (Suc 0)) [recf.id 2 0]]) |
1448 (Cn 3 rec_noteq [Cn 3 rec_mult [recf.id 3 (Suc 0), recf.id 3 2], recf.id 3 0])) [x, k] = |
1509 (Cn 3 rec_noteq [Cn 3 rec_mult [recf.id 3 (Suc 0), recf.id 3 2], recf.id 3 0])) [x, k] = |
1449 ((if (\<forall>w\<le>rec_eval (Cn 2 rec_minus [recf.id 2 0, Cn 2 (constn (Suc 0)) [recf.id 2 0]]) ([x, k]). |
1510 ((if (\<forall>w\<le>rec_exec (Cn 2 rec_minus [recf.id 2 0, Cn 2 (constn (Suc 0)) [recf.id 2 0]]) ([x, k]). |
1450 0 < rec_eval (Cn 3 rec_noteq [Cn 3 rec_mult [recf.id 3 (Suc 0), recf.id 3 2], recf.id 3 0]) |
1511 0 < rec_exec (Cn 3 rec_noteq [Cn 3 rec_mult [recf.id 3 (Suc 0), recf.id 3 2], recf.id 3 0]) |
1451 ([x, k] @ [w])) then 1 else 0))" |
1512 ([x, k] @ [w])) then 1 else 0))" |
1452 apply(rule_tac all_lemma) |
1513 apply(rule_tac all_lemma) |
1453 apply(auto) |
1514 apply(auto) |
1454 apply(case_tac [!] i, auto) |
1515 apply(case_tac [!] i, auto) |
1455 apply(case_tac ia, auto simp: numeral_3_eq_3 numeral_2_eq_2) |
1516 apply(case_tac ia, auto simp: numeral_3_eq_3 numeral_2_eq_2) |
1456 done |
1517 done |
1457 |
1518 |
1458 text {* |
1519 text {* |
1459 The correctness of @{text "Prime"}. |
1520 The correctness of @{text "Prime"}. |
1460 *} |
1521 *} |
1461 lemma prime_lemma: "rec_eval rec_prime [x] = (if prime x then 1 else 0)" |
1522 lemma prime_lemma: "rec_exec rec_prime [x] = (if Prime x then 1 else 0)" |
1462 proof(simp add: rec_eval.simps rec_prime_def) |
1523 proof(simp add: rec_exec.simps rec_prime_def) |
1463 let ?rt1 = "(Cn 2 rec_minus [recf.id 2 0, |
1524 let ?rt1 = "(Cn 2 rec_minus [recf.id 2 0, |
1464 Cn 2 (constn (Suc 0)) [recf.id 2 0]])" |
1525 Cn 2 (constn (Suc 0)) [recf.id 2 0]])" |
1465 let ?rf1 = "(Cn 3 rec_noteq [Cn 3 rec_mult |
1526 let ?rf1 = "(Cn 3 rec_noteq [Cn 3 rec_mult |
1466 [recf.id 3 (Suc 0), recf.id 3 2], recf.id 3 (0)])" |
1527 [recf.id 3 (Suc 0), recf.id 3 2], recf.id 3 (0)])" |
1467 let ?rt2 = "(Cn (Suc 0) rec_minus |
1528 let ?rt2 = "(Cn (Suc 0) rec_minus |
1468 [recf.id (Suc 0) 0, constn (Suc 0)])" |
1529 [recf.id (Suc 0) 0, constn (Suc 0)])" |
1469 let ?rf2 = "rec_all ?rt1 ?rf1" |
1530 let ?rf2 = "rec_all ?rt1 ?rf1" |
1470 have h1: "rec_eval (rec_all ?rt2 ?rf2) ([x]) = |
1531 have h1: "rec_exec (rec_all ?rt2 ?rf2) ([x]) = |
1471 (if (\<forall>k\<le>rec_eval ?rt2 ([x]). 0 < rec_eval ?rf2 ([x] @ [k])) then 1 else 0)" |
1532 (if (\<forall>k\<le>rec_exec ?rt2 ([x]). 0 < rec_exec ?rf2 ([x] @ [k])) then 1 else 0)" |
1472 proof(rule_tac all_lemma, simp_all) |
1533 proof(rule_tac all_lemma, simp_all) |
1473 show "primerec ?rf2 (Suc (Suc 0))" |
1534 show "primerec ?rf2 (Suc (Suc 0))" |
1474 apply(rule_tac primerec_all_iff) |
1535 apply(rule_tac primerec_all_iff) |
1475 apply(auto) |
1536 apply(auto) |
1476 apply(case_tac [!] i, auto simp: numeral_2_eq_2) |
1537 apply(case_tac [!] i, auto simp: numeral_2_eq_2) |
1482 apply(auto) |
1543 apply(auto) |
1483 apply(case_tac i, auto) |
1544 apply(case_tac i, auto) |
1484 done |
1545 done |
1485 qed |
1546 qed |
1486 from h1 show |
1547 from h1 show |
1487 "(Suc 0 < x \<longrightarrow> (rec_eval (rec_all ?rt2 ?rf2) [x] = 0 \<longrightarrow> |
1548 "(Suc 0 < x \<longrightarrow> (rec_exec (rec_all ?rt2 ?rf2) [x] = 0 \<longrightarrow> |
1488 \<not> prime x) \<and> |
1549 \<not> Prime x) \<and> |
1489 (0 < rec_eval (rec_all ?rt2 ?rf2) [x] \<longrightarrow> prime x)) \<and> |
1550 (0 < rec_exec (rec_all ?rt2 ?rf2) [x] \<longrightarrow> Prime x)) \<and> |
1490 (\<not> Suc 0 < x \<longrightarrow> \<not> prime x \<and> (rec_eval (rec_all ?rt2 ?rf2) [x] = 0 |
1551 (\<not> Suc 0 < x \<longrightarrow> \<not> Prime x \<and> (rec_exec (rec_all ?rt2 ?rf2) [x] = 0 |
1491 \<longrightarrow> \<not> prime x))" |
1552 \<longrightarrow> \<not> Prime x))" |
1492 apply(auto simp:rec_eval.simps) |
1553 apply(auto simp:rec_exec.simps) |
1493 apply(simp add: exec_tmp rec_eval.simps) |
1554 apply(simp add: exec_tmp rec_exec.simps) |
1494 proof - |
1555 proof - |
1495 assume "\<forall>k\<le>x - Suc 0. (0::nat) < (if \<forall>w\<le>x - Suc 0. |
1556 assume "\<forall>k\<le>x - Suc 0. (0::nat) < (if \<forall>w\<le>x - Suc 0. |
1496 0 < (if k * w \<noteq> x then 1 else (0 :: nat)) then 1 else 0)" "Suc 0 < x" |
1557 0 < (if k * w \<noteq> x then 1 else (0 :: nat)) then 1 else 0)" "Suc 0 < x" |
1497 thus "prime x" |
1558 thus "Prime x" |
1498 apply(simp add: rec_eval.simps split: if_splits) |
1559 apply(simp add: rec_exec.simps split: if_splits) |
1499 apply(simp add: prime_nat_def dvd_def, auto) |
1560 apply(simp add: Prime.simps, auto) |
1500 apply(erule_tac x = m in allE, auto) |
1561 apply(erule_tac x = u in allE, auto) |
1501 apply(case_tac m, simp, case_tac nat, simp, simp) |
1562 apply(case_tac u, simp, case_tac nat, simp, simp) |
1502 apply(case_tac k, simp, case_tac nat, simp, simp) |
1563 apply(case_tac v, simp, case_tac nat, simp, simp) |
1503 done |
1564 done |
1504 next |
1565 next |
1505 assume "\<not> Suc 0 < x" "prime x" |
1566 assume "\<not> Suc 0 < x" "Prime x" |
1506 thus "False" |
1567 thus "False" |
1507 by (simp add: prime_nat_def) |
1568 apply(simp add: Prime.simps) |
|
1569 done |
1508 next |
1570 next |
1509 fix k |
1571 fix k |
1510 assume "rec_eval (rec_all ?rt1 ?rf1) |
1572 assume "rec_exec (rec_all ?rt1 ?rf1) |
1511 [x, k] = 0" "k \<le> x - Suc 0" "prime x" |
1573 [x, k] = 0" "k \<le> x - Suc 0" "Prime x" |
1512 thus "False" |
1574 thus "False" |
1513 by (auto simp add: exec_tmp rec_eval.simps prime_nat_def dvd_def split: if_splits) |
1575 apply(simp add: exec_tmp rec_exec.simps Prime.simps split: if_splits) |
|
1576 done |
1514 next |
1577 next |
1515 fix k |
1578 fix k |
1516 assume "rec_eval (rec_all ?rt1 ?rf1) |
1579 assume "rec_exec (rec_all ?rt1 ?rf1) |
1517 [x, k] = 0" "k \<le> x - Suc 0" "prime x" |
1580 [x, k] = 0" "k \<le> x - Suc 0" "Prime x" |
1518 thus "False" |
1581 thus "False" |
1519 by(auto simp add: exec_tmp rec_eval.simps prime_nat_def dvd_def split: if_splits) |
1582 apply(simp add: exec_tmp rec_exec.simps Prime.simps split: if_splits) |
|
1583 done |
1520 qed |
1584 qed |
1521 qed |
1585 qed |
1522 |
1586 |
1523 definition rec_dummyfac :: "recf" |
1587 definition rec_dummyfac :: "recf" |
1524 where |
1588 where |
1525 "rec_dummyfac = Pr 1 (constn 1) (Cn 3 rec_mult [id 3 2, Cn 3 s [id 3 1]])" |
1589 "rec_dummyfac = Pr 1 (constn 1) |
|
1590 (Cn 3 rec_mult [id 3 2, Cn 3 s [id 3 1]])" |
1526 |
1591 |
1527 text {* |
1592 text {* |
1528 The recursive function used to implment factorization. |
1593 The recursive function used to implment factorization. |
1529 *} |
1594 *} |
1530 definition rec_fac :: "recf" |
1595 definition rec_fac :: "recf" |
1537 fun fac :: "nat \<Rightarrow> nat" ("_!" [100] 99) |
1602 fun fac :: "nat \<Rightarrow> nat" ("_!" [100] 99) |
1538 where |
1603 where |
1539 "fac 0 = 1" | |
1604 "fac 0 = 1" | |
1540 "fac (Suc x) = (Suc x) * fac x" |
1605 "fac (Suc x) = (Suc x) * fac x" |
1541 |
1606 |
1542 lemma [simp]: "rec_eval rec_dummyfac [0, 0] = Suc 0" |
1607 lemma [simp]: "rec_exec rec_dummyfac [0, 0] = Suc 0" |
1543 by(simp add: rec_dummyfac_def rec_eval.simps) |
1608 by(simp add: rec_dummyfac_def rec_exec.simps) |
1544 |
1609 |
1545 lemma rec_cn_simp: |
1610 lemma rec_cn_simp: "rec_exec (Cn n f gs) xs = |
1546 "rec_eval (Cn n f gs) xs = (let rgs = map (\<lambda> g. rec_eval g xs) gs in rec_eval f rgs)" |
1611 (let rgs = map (\<lambda> g. rec_exec g xs) gs in |
1547 by(simp add: rec_eval.simps) |
1612 rec_exec f rgs)" |
1548 |
1613 by(simp add: rec_exec.simps) |
1549 lemma rec_id_simp: "rec_eval (id m n) xs = xs ! n" |
1614 |
1550 by(simp add: rec_eval.simps) |
1615 lemma rec_id_simp: "rec_exec (id m n) xs = xs ! n" |
1551 |
1616 by(simp add: rec_exec.simps) |
1552 lemma fac_dummy: "rec_eval rec_dummyfac [x, y] = y !" |
1617 |
|
1618 lemma fac_dummy: "rec_exec rec_dummyfac [x, y] = y !" |
1553 apply(induct y) |
1619 apply(induct y) |
1554 apply(auto simp: rec_dummyfac_def rec_eval.simps) |
1620 apply(auto simp: rec_dummyfac_def rec_exec.simps) |
1555 done |
1621 done |
1556 |
1622 |
1557 text {* |
1623 text {* |
1558 The correctness of @{text "rec_fac"}. |
1624 The correctness of @{text "rec_fac"}. |
1559 *} |
1625 *} |
1560 lemma fac_lemma: "rec_eval rec_fac [x] = x!" |
1626 lemma fac_lemma: "rec_exec rec_fac [x] = x!" |
1561 apply(simp add: rec_fac_def rec_eval.simps fac_dummy) |
1627 apply(simp add: rec_fac_def rec_exec.simps fac_dummy) |
1562 done |
1628 done |
1563 |
1629 |
1564 declare fac.simps[simp del] |
1630 declare fac.simps[simp del] |
1565 |
1631 |
1566 text {* |
1632 text {* |
1567 @{text "Np x"} returns the first prime number after @{text "x"}. |
1633 @{text "Np x"} returns the first prime number after @{text "x"}. |
1568 *} |
1634 *} |
1569 fun Np ::"nat \<Rightarrow> nat" |
1635 fun Np ::"nat \<Rightarrow> nat" |
1570 where |
1636 where |
1571 "Np x = Min {y. y \<le> Suc (x!) \<and> x < y \<and> prime y}" |
1637 "Np x = Min {y. y \<le> Suc (x!) \<and> x < y \<and> Prime y}" |
1572 |
1638 |
1573 declare Np.simps[simp del] rec_Minr.simps[simp del] |
1639 declare Np.simps[simp del] rec_Minr.simps[simp del] |
1574 |
1640 |
1575 text {* |
1641 text {* |
1576 @{text "rec_np"} is the recursive function used to implement |
1642 @{text "rec_np"} is the recursive function used to implement |
1587 apply(simp add: fac.simps) |
1653 apply(simp add: fac.simps) |
1588 apply(case_tac n, auto simp: fac.simps) |
1654 apply(case_tac n, auto simp: fac.simps) |
1589 done |
1655 done |
1590 |
1656 |
1591 lemma divsor_ex: |
1657 lemma divsor_ex: |
1592 "\<lbrakk>\<not> prime x; x > Suc 0\<rbrakk> \<Longrightarrow> (\<exists> u > Suc 0. (\<exists> v > Suc 0. u * v = x))" |
1658 "\<lbrakk>\<not> Prime x; x > Suc 0\<rbrakk> \<Longrightarrow> (\<exists> u > Suc 0. (\<exists> v > Suc 0. u * v = x))" |
1593 apply(auto simp: prime_nat_def dvd_def) |
1659 by(auto simp: Prime.simps) |
1594 by (metis Suc_lessI mult_0 nat_mult_commute neq0_conv) |
1660 |
1595 |
1661 lemma divsor_prime_ex: "\<lbrakk>\<not> Prime x; x > Suc 0\<rbrakk> \<Longrightarrow> |
1596 |
1662 \<exists> p. Prime p \<and> p dvd x" |
1597 lemma divsor_prime_ex: "\<lbrakk>\<not> prime x; x > Suc 0\<rbrakk> \<Longrightarrow> |
|
1598 \<exists> p. prime p \<and> p dvd x" |
|
1599 apply(induct x rule: wf_induct[where r = "measure (\<lambda> y. y)"], simp) |
1663 apply(induct x rule: wf_induct[where r = "measure (\<lambda> y. y)"], simp) |
1600 apply(drule_tac divsor_ex, simp, auto) |
1664 apply(drule_tac divsor_ex, simp, auto) |
1601 apply(erule_tac x = u in allE, simp) |
1665 apply(erule_tac x = u in allE, simp) |
1602 apply(case_tac "prime u", simp) |
1666 apply(case_tac "Prime u", simp) |
1603 apply(rule_tac x = u in exI, simp, auto) |
1667 apply(rule_tac x = u in exI, simp, auto) |
1604 done |
1668 done |
1605 |
1669 |
1606 lemma [intro]: "0 < n!" |
1670 lemma [intro]: "0 < n!" |
1607 apply(induct n) |
1671 apply(induct n) |
1635 from this obtain d where "d > 0 \<and> ka = d + k" .. |
1699 from this obtain d where "d > 0 \<and> ka = d + k" .. |
1636 from h2 and this and h1 show "False" |
1700 from h2 and this and h1 show "False" |
1637 by(simp add: add_mult_distrib2) |
1701 by(simp add: add_mult_distrib2) |
1638 qed |
1702 qed |
1639 |
1703 |
1640 lemma prime_ex: "\<exists> p. n < p \<and> p \<le> Suc (n!) \<and> prime p" |
1704 lemma prime_ex: "\<exists> p. n < p \<and> p \<le> Suc (n!) \<and> Prime p" |
1641 proof(cases "prime (n! + 1)") |
1705 proof(cases "Prime (n! + 1)") |
1642 case True thus "?thesis" |
1706 case True thus "?thesis" |
1643 by(rule_tac x = "Suc (n!)" in exI, simp) |
1707 by(rule_tac x = "Suc (n!)" in exI, simp) |
1644 next |
1708 next |
1645 assume h: "\<not> prime (n! + 1)" |
1709 assume h: "\<not> Prime (n! + 1)" |
1646 hence "\<exists> p. prime p \<and> p dvd (n! + 1)" |
1710 hence "\<exists> p. Prime p \<and> p dvd (n! + 1)" |
1647 by(erule_tac divsor_prime_ex, auto) |
1711 by(erule_tac divsor_prime_ex, auto) |
1648 from this obtain q where k: "prime q \<and> q dvd (n! + 1)" .. |
1712 from this obtain q where k: "Prime q \<and> q dvd (n! + 1)" .. |
1649 thus "?thesis" |
1713 thus "?thesis" |
1650 proof(cases "q > n") |
1714 proof(cases "q > n") |
1651 case True thus "?thesis" |
1715 case True thus "?thesis" |
1652 using k |
1716 using k |
1653 apply(rule_tac x = q in exI, auto) |
1717 apply(rule_tac x = q in exI, auto) |
1684 done |
1748 done |
1685 |
1749 |
1686 text {* |
1750 text {* |
1687 The correctness of @{text "rec_np"}. |
1751 The correctness of @{text "rec_np"}. |
1688 *} |
1752 *} |
1689 lemma np_lemma: "rec_eval rec_np [x] = Np x" |
1753 lemma np_lemma: "rec_exec rec_np [x] = Np x" |
1690 proof(auto simp: rec_np_def rec_eval.simps Let_def fac_lemma) |
1754 proof(auto simp: rec_np_def rec_exec.simps Let_def fac_lemma) |
1691 let ?rr = "(Cn 2 rec_conj [Cn 2 rec_less [recf.id 2 0, |
1755 let ?rr = "(Cn 2 rec_conj [Cn 2 rec_less [recf.id 2 0, |
1692 recf.id 2 (Suc 0)], Cn 2 rec_prime [recf.id 2 (Suc 0)]])" |
1756 recf.id 2 (Suc 0)], Cn 2 rec_prime [recf.id 2 (Suc 0)]])" |
1693 let ?R = "\<lambda> zs. zs ! 0 < zs ! 1 \<and> prime (zs ! 1)" |
1757 let ?R = "\<lambda> zs. zs ! 0 < zs ! 1 \<and> Prime (zs ! 1)" |
1694 have g1: "rec_eval (rec_Minr ?rr) ([x] @ [Suc (x!)]) = |
1758 have g1: "rec_exec (rec_Minr ?rr) ([x] @ [Suc (x!)]) = |
1695 Minr (\<lambda> args. 0 < rec_eval ?rr args) [x] (Suc (x!))" |
1759 Minr (\<lambda> args. 0 < rec_exec ?rr args) [x] (Suc (x!))" |
1696 by(rule_tac Minr_lemma, auto simp: rec_eval.simps |
1760 by(rule_tac Minr_lemma, auto simp: rec_exec.simps |
1697 prime_lemma, auto simp: numeral_2_eq_2 numeral_3_eq_3) |
1761 prime_lemma, auto simp: numeral_2_eq_2 numeral_3_eq_3) |
1698 have g2: "Minr (\<lambda> args. 0 < rec_eval ?rr args) [x] (Suc (x!)) = Np x" |
1762 have g2: "Minr (\<lambda> args. 0 < rec_exec ?rr args) [x] (Suc (x!)) = Np x" |
1699 using prime_ex[of x] |
1763 using prime_ex[of x] |
1700 apply(auto simp: Minr.simps Np.simps rec_eval.simps) |
1764 apply(auto simp: Minr.simps Np.simps rec_exec.simps) |
1701 apply(erule_tac x = p in allE, simp add: prime_lemma) |
1765 apply(erule_tac x = p in allE, simp add: prime_lemma) |
1702 apply(simp add: prime_lemma split: if_splits) |
1766 apply(simp add: prime_lemma split: if_splits) |
1703 apply(subgoal_tac |
1767 apply(subgoal_tac |
1704 "{uu. (prime uu \<longrightarrow> (x < uu \<longrightarrow> uu \<le> Suc (x!)) \<and> x < uu) \<and> prime uu} |
1768 "{uu. (Prime uu \<longrightarrow> (x < uu \<longrightarrow> uu \<le> Suc (x!)) \<and> x < uu) \<and> Prime uu} |
1705 = {y. y \<le> Suc (x!) \<and> x < y \<and> prime y}", auto) |
1769 = {y. y \<le> Suc (x!) \<and> x < y \<and> Prime y}", auto) |
1706 done |
1770 done |
1707 from g1 and g2 show "rec_eval (rec_Minr ?rr) ([x, Suc (x!)]) = Np x" |
1771 from g1 and g2 show "rec_exec (rec_Minr ?rr) ([x, Suc (x!)]) = Np x" |
1708 by simp |
1772 by simp |
1709 qed |
1773 qed |
1710 |
1774 |
1711 text {* |
1775 text {* |
1712 @{text "rec_power"} is the recursive function used to implement |
1776 @{text "rec_power"} is the recursive function used to implement |
1831 in Cn 2 rec_add [Cn 2 rec_mult [rb, rcond], |
1895 in Cn 2 rec_add [Cn 2 rec_mult [rb, rcond], |
1832 Cn 2 rec_mult [Cn 2 (constn 0) [id 2 0], rcond2]])" |
1896 Cn 2 rec_mult [Cn 2 (constn 0) [id 2 0], rcond2]])" |
1833 |
1897 |
1834 lemma rec_lo_Maxr_lor: |
1898 lemma rec_lo_Maxr_lor: |
1835 "\<lbrakk>Suc 0 < x; Suc 0 < y\<rbrakk> \<Longrightarrow> |
1899 "\<lbrakk>Suc 0 < x; Suc 0 < y\<rbrakk> \<Longrightarrow> |
1836 rec_eval rec_lo [x, y] = Maxr loR [x, y] x" |
1900 rec_exec rec_lo [x, y] = Maxr loR [x, y] x" |
1837 proof(auto simp: rec_eval.simps rec_lo_def Let_def |
1901 proof(auto simp: rec_exec.simps rec_lo_def Let_def |
1838 numeral_2_eq_2 numeral_3_eq_3) |
1902 numeral_2_eq_2 numeral_3_eq_3) |
1839 let ?rR = "(Cn (Suc (Suc (Suc 0))) rec_eq |
1903 let ?rR = "(Cn (Suc (Suc (Suc 0))) rec_eq |
1840 [Cn (Suc (Suc (Suc 0))) rec_mod [recf.id (Suc (Suc (Suc 0))) 0, |
1904 [Cn (Suc (Suc (Suc 0))) rec_mod [recf.id (Suc (Suc (Suc 0))) 0, |
1841 Cn (Suc (Suc (Suc 0))) rec_power [recf.id (Suc (Suc (Suc 0))) |
1905 Cn (Suc (Suc (Suc 0))) rec_power [recf.id (Suc (Suc (Suc 0))) |
1842 (Suc 0), recf.id (Suc (Suc (Suc 0))) (Suc (Suc 0))]], |
1906 (Suc 0), recf.id (Suc (Suc (Suc 0))) (Suc (Suc 0))]], |
1843 Cn (Suc (Suc (Suc 0))) (constn 0) [recf.id (Suc (Suc (Suc 0))) (Suc 0)]])" |
1907 Cn (Suc (Suc (Suc 0))) (constn 0) [recf.id (Suc (Suc (Suc 0))) (Suc 0)]])" |
1844 have h: "rec_eval (rec_maxr ?rR) ([x, y] @ [x]) = |
1908 have h: "rec_exec (rec_maxr ?rR) ([x, y] @ [x]) = |
1845 Maxr (\<lambda> args. 0 < rec_eval ?rR args) [x, y] x" |
1909 Maxr (\<lambda> args. 0 < rec_exec ?rR args) [x, y] x" |
1846 by(rule_tac Maxr_lemma, auto simp: rec_eval.simps |
1910 by(rule_tac Maxr_lemma, auto simp: rec_exec.simps |
1847 mod_lemma power_lemma, auto simp: numeral_2_eq_2 numeral_3_eq_3) |
1911 mod_lemma power_lemma, auto simp: numeral_2_eq_2 numeral_3_eq_3) |
1848 have "Maxr loR [x, y] x = Maxr (\<lambda> args. 0 < rec_eval ?rR args) [x, y] x" |
1912 have "Maxr loR [x, y] x = Maxr (\<lambda> args. 0 < rec_exec ?rR args) [x, y] x" |
1849 apply(simp add: rec_eval.simps mod_lemma power_lemma) |
1913 apply(simp add: rec_exec.simps mod_lemma power_lemma) |
1850 apply(simp add: Maxr.simps loR.simps) |
1914 apply(simp add: Maxr.simps loR.simps) |
1851 done |
1915 done |
1852 from h and this show "rec_eval (rec_maxr ?rR) [x, y, x] = |
1916 from h and this show "rec_exec (rec_maxr ?rR) [x, y, x] = |
1853 Maxr loR [x, y] x" |
1917 Maxr loR [x, y] x" |
1854 apply(simp) |
1918 apply(simp) |
1855 done |
1919 done |
1856 qed |
1920 qed |
1857 |
1921 |
1899 apply(simp add: Maxr.simps lo.simps, auto) |
1963 apply(simp add: Maxr.simps lo.simps, auto) |
1900 apply(erule_tac x = xa in allE, simp, simp add: uplimit_loR) |
1964 apply(erule_tac x = xa in allE, simp, simp add: uplimit_loR) |
1901 done |
1965 done |
1902 |
1966 |
1903 lemma lo_lemma': "\<lbrakk>Suc 0 < x; Suc 0 < y\<rbrakk> \<Longrightarrow> |
1967 lemma lo_lemma': "\<lbrakk>Suc 0 < x; Suc 0 < y\<rbrakk> \<Longrightarrow> |
1904 rec_eval rec_lo [x, y] = lo x y" |
1968 rec_exec rec_lo [x, y] = lo x y" |
1905 by(simp add: Maxr_lo rec_lo_Maxr_lor) |
1969 by(simp add: Maxr_lo rec_lo_Maxr_lor) |
1906 |
1970 |
1907 lemma lo_lemma'': "\<lbrakk>\<not> Suc 0 < x\<rbrakk> \<Longrightarrow> rec_eval rec_lo [x, y] = lo x y" |
1971 lemma lo_lemma'': "\<lbrakk>\<not> Suc 0 < x\<rbrakk> \<Longrightarrow> rec_exec rec_lo [x, y] = lo x y" |
1908 apply(case_tac x, auto simp: rec_eval.simps rec_lo_def |
1972 apply(case_tac x, auto simp: rec_exec.simps rec_lo_def |
1909 Let_def lo.simps) |
1973 Let_def lo.simps) |
1910 done |
1974 done |
1911 |
1975 |
1912 lemma lo_lemma''': "\<lbrakk>\<not> Suc 0 < y\<rbrakk> \<Longrightarrow> rec_eval rec_lo [x, y] = lo x y" |
1976 lemma lo_lemma''': "\<lbrakk>\<not> Suc 0 < y\<rbrakk> \<Longrightarrow> rec_exec rec_lo [x, y] = lo x y" |
1913 apply(case_tac y, auto simp: rec_eval.simps rec_lo_def |
1977 apply(case_tac y, auto simp: rec_exec.simps rec_lo_def |
1914 Let_def lo.simps) |
1978 Let_def lo.simps) |
1915 done |
1979 done |
1916 |
1980 |
1917 text {* |
1981 text {* |
1918 The correctness of @{text "rec_lo"}: |
1982 The correctness of @{text "rec_lo"}: |
1919 *} |
1983 *} |
1920 lemma lo_lemma: "rec_eval rec_lo [x, y] = lo x y" |
1984 lemma lo_lemma: "rec_exec rec_lo [x, y] = lo x y" |
1921 apply(case_tac "Suc 0 < x \<and> Suc 0 < y") |
1985 apply(case_tac "Suc 0 < x \<and> Suc 0 < y") |
1922 apply(auto simp: lo_lemma' lo_lemma'' lo_lemma''') |
1986 apply(auto simp: lo_lemma' lo_lemma'' lo_lemma''') |
1923 done |
1987 done |
1924 |
1988 |
1925 fun lgR :: "nat list \<Rightarrow> bool" |
1989 fun lgR :: "nat list \<Rightarrow> bool" |
1942 text {* |
2006 text {* |
1943 @{text "rec_lg"} is the recursive function used to implement @{text "lg"}. |
2007 @{text "rec_lg"} is the recursive function used to implement @{text "lg"}. |
1944 *} |
2008 *} |
1945 definition rec_lg :: "recf" |
2009 definition rec_lg :: "recf" |
1946 where |
2010 where |
1947 "rec_lg = |
2011 "rec_lg = (let rec_lgR = Cn 3 rec_le |
1948 (let rec_lgR = Cn 3 rec_le [Cn 3 rec_power [id 3 1, id 3 2], id 3 0] in |
2012 [Cn 3 rec_power [id 3 1, id 3 2], id 3 0] in |
1949 let conR1 = Cn 2 rec_conj [Cn 2 rec_less [Cn 2 (constn 1) [id 2 0], id 2 0], |
2013 let conR1 = Cn 2 rec_conj [Cn 2 rec_less |
1950 Cn 2 rec_less [Cn 2 (constn 1) [id 2 0], id 2 1]] in |
2014 [Cn 2 (constn 1) [id 2 0], id 2 0], |
1951 let conR2 = Cn 2 rec_not [conR1] in |
2015 Cn 2 rec_less [Cn 2 (constn 1) |
1952 Cn 2 rec_add [Cn 2 rec_mult [conR1, Cn 2 (rec_maxr rec_lgR) |
2016 [id 2 0], id 2 1]] in |
1953 [id 2 0, id 2 1, id 2 0]], |
2017 let conR2 = Cn 2 rec_not [conR1] in |
1954 Cn 2 rec_mult [conR2, Cn 2 (constn 0) [id 2 0]]])" |
2018 Cn 2 rec_add [Cn 2 rec_mult |
|
2019 [conR1, Cn 2 (rec_maxr rec_lgR) |
|
2020 [id 2 0, id 2 1, id 2 0]], |
|
2021 Cn 2 rec_mult [conR2, Cn 2 (constn 0) |
|
2022 [id 2 0]]])" |
1955 |
2023 |
1956 lemma lg_maxr: "\<lbrakk>Suc 0 < x; Suc 0 < y\<rbrakk> \<Longrightarrow> |
2024 lemma lg_maxr: "\<lbrakk>Suc 0 < x; Suc 0 < y\<rbrakk> \<Longrightarrow> |
1957 rec_eval rec_lg [x, y] = Maxr lgR [x, y] x" |
2025 rec_exec rec_lg [x, y] = Maxr lgR [x, y] x" |
1958 proof(simp add: rec_eval.simps rec_lg_def Let_def) |
2026 proof(simp add: rec_exec.simps rec_lg_def Let_def) |
1959 assume h: "Suc 0 < x" "Suc 0 < y" |
2027 assume h: "Suc 0 < x" "Suc 0 < y" |
1960 let ?rR = "(Cn 3 rec_le [Cn 3 rec_power |
2028 let ?rR = "(Cn 3 rec_le [Cn 3 rec_power |
1961 [recf.id 3 (Suc 0), recf.id 3 2], recf.id 3 0])" |
2029 [recf.id 3 (Suc 0), recf.id 3 2], recf.id 3 0])" |
1962 have "rec_eval (rec_maxr ?rR) ([x, y] @ [x]) |
2030 have "rec_exec (rec_maxr ?rR) ([x, y] @ [x]) |
1963 = Maxr ((\<lambda> args. 0 < rec_eval ?rR args)) [x, y] x" |
2031 = Maxr ((\<lambda> args. 0 < rec_exec ?rR args)) [x, y] x" |
1964 proof(rule Maxr_lemma) |
2032 proof(rule Maxr_lemma) |
1965 show "primerec (Cn 3 rec_le [Cn 3 rec_power |
2033 show "primerec (Cn 3 rec_le [Cn 3 rec_power |
1966 [recf.id 3 (Suc 0), recf.id 3 2], recf.id 3 0]) (Suc (length [x, y]))" |
2034 [recf.id 3 (Suc 0), recf.id 3 2], recf.id 3 0]) (Suc (length [x, y]))" |
1967 apply(auto simp: numeral_3_eq_3)+ |
2035 apply(auto simp: numeral_3_eq_3)+ |
1968 done |
2036 done |
1969 qed |
2037 qed |
1970 moreover have "Maxr lgR [x, y] x = Maxr ((\<lambda> args. 0 < rec_eval ?rR args)) [x, y] x" |
2038 moreover have "Maxr lgR [x, y] x = Maxr ((\<lambda> args. 0 < rec_exec ?rR args)) [x, y] x" |
1971 apply(simp add: rec_eval.simps power_lemma) |
2039 apply(simp add: rec_exec.simps power_lemma) |
1972 apply(simp add: Maxr.simps lgR.simps) |
2040 apply(simp add: Maxr.simps lgR.simps) |
1973 done |
2041 done |
1974 ultimately show "rec_eval (rec_maxr ?rR) [x, y, x] = Maxr lgR [x, y] x" |
2042 ultimately show "rec_exec (rec_maxr ?rR) [x, y, x] = Maxr lgR [x, y] x" |
1975 by simp |
2043 by simp |
1976 qed |
2044 qed |
1977 |
2045 |
1978 lemma [simp]: "\<lbrakk>Suc 0 < y; lgR [x, y, xa]\<rbrakk> \<Longrightarrow> xa \<le> x" |
2046 lemma [simp]: "\<lbrakk>Suc 0 < y; lgR [x, y, xa]\<rbrakk> \<Longrightarrow> xa \<le> x" |
1979 apply(simp add: lgR.simps) |
2047 apply(simp add: lgR.simps) |
1989 lemma maxr_lg: "\<lbrakk>Suc 0 < x; Suc 0 < y\<rbrakk> \<Longrightarrow> Maxr lgR [x, y] x = lg x y" |
2057 lemma maxr_lg: "\<lbrakk>Suc 0 < x; Suc 0 < y\<rbrakk> \<Longrightarrow> Maxr lgR [x, y] x = lg x y" |
1990 apply(simp add: lg.simps Maxr.simps, auto) |
2058 apply(simp add: lg.simps Maxr.simps, auto) |
1991 apply(erule_tac x = xa in allE, simp) |
2059 apply(erule_tac x = xa in allE, simp) |
1992 done |
2060 done |
1993 |
2061 |
1994 lemma lg_lemma': "\<lbrakk>Suc 0 < x; Suc 0 < y\<rbrakk> \<Longrightarrow> rec_eval rec_lg [x, y] = lg x y" |
2062 lemma lg_lemma': "\<lbrakk>Suc 0 < x; Suc 0 < y\<rbrakk> \<Longrightarrow> rec_exec rec_lg [x, y] = lg x y" |
1995 apply(simp add: maxr_lg lg_maxr) |
2063 apply(simp add: maxr_lg lg_maxr) |
1996 done |
2064 done |
1997 |
2065 |
1998 lemma lg_lemma'': "\<not> Suc 0 < x \<Longrightarrow> rec_eval rec_lg [x, y] = lg x y" |
2066 lemma lg_lemma'': "\<not> Suc 0 < x \<Longrightarrow> rec_exec rec_lg [x, y] = lg x y" |
1999 apply(simp add: rec_eval.simps rec_lg_def Let_def lg.simps) |
2067 apply(simp add: rec_exec.simps rec_lg_def Let_def lg.simps) |
2000 done |
2068 done |
2001 |
2069 |
2002 lemma lg_lemma''': "\<not> Suc 0 < y \<Longrightarrow> rec_eval rec_lg [x, y] = lg x y" |
2070 lemma lg_lemma''': "\<not> Suc 0 < y \<Longrightarrow> rec_exec rec_lg [x, y] = lg x y" |
2003 apply(simp add: rec_eval.simps rec_lg_def Let_def lg.simps) |
2071 apply(simp add: rec_exec.simps rec_lg_def Let_def lg.simps) |
2004 done |
2072 done |
2005 |
2073 |
2006 text {* |
2074 text {* |
2007 The correctness of @{text "rec_lg"}. |
2075 The correctness of @{text "rec_lg"}. |
2008 *} |
2076 *} |
2009 lemma lg_lemma: "rec_eval rec_lg [x, y] = lg x y" |
2077 lemma lg_lemma: "rec_exec rec_lg [x, y] = lg x y" |
2010 apply(case_tac "Suc 0 < x \<and> Suc 0 < y", auto simp: |
2078 apply(case_tac "Suc 0 < x \<and> Suc 0 < y", auto simp: |
2011 lg_lemma' lg_lemma'' lg_lemma''') |
2079 lg_lemma' lg_lemma'' lg_lemma''') |
2012 done |
2080 done |
2013 |
2081 |
2014 text {* |
2082 text {* |
2106 fun rec_strt :: "nat \<Rightarrow> recf" |
2174 fun rec_strt :: "nat \<Rightarrow> recf" |
2107 where |
2175 where |
2108 "rec_strt vl = Cn vl (rec_strt' vl vl) (rec_map s vl)" |
2176 "rec_strt vl = Cn vl (rec_strt' vl vl) (rec_map s vl)" |
2109 |
2177 |
2110 lemma map_s_lemma: "length xs = vl \<Longrightarrow> |
2178 lemma map_s_lemma: "length xs = vl \<Longrightarrow> |
2111 map ((\<lambda>a. rec_eval a xs) \<circ> (\<lambda>i. Cn vl s [recf.id vl i])) |
2179 map ((\<lambda>a. rec_exec a xs) \<circ> (\<lambda>i. Cn vl s [recf.id vl i])) |
2112 [0..<vl] |
2180 [0..<vl] |
2113 = map Suc xs" |
2181 = map Suc xs" |
2114 apply(induct vl arbitrary: xs, simp, auto simp: rec_eval.simps) |
2182 apply(induct vl arbitrary: xs, simp, auto simp: rec_exec.simps) |
2115 apply(subgoal_tac "\<exists> ys y. xs = ys @ [y]", auto) |
2183 apply(subgoal_tac "\<exists> ys y. xs = ys @ [y]", auto) |
2116 proof - |
2184 proof - |
2117 fix ys y |
2185 fix ys y |
2118 assume ind: "\<And>xs. length xs = length (ys::nat list) \<Longrightarrow> |
2186 assume ind: "\<And>xs. length xs = length (ys::nat list) \<Longrightarrow> |
2119 map ((\<lambda>a. rec_eval a xs) \<circ> (\<lambda>i. Cn (length ys) s |
2187 map ((\<lambda>a. rec_exec a xs) \<circ> (\<lambda>i. Cn (length ys) s |
2120 [recf.id (length ys) (i)])) [0..<length ys] = map Suc xs" |
2188 [recf.id (length ys) (i)])) [0..<length ys] = map Suc xs" |
2121 show |
2189 show |
2122 "map ((\<lambda>a. rec_eval a (ys @ [y])) \<circ> (\<lambda>i. Cn (Suc (length ys)) s |
2190 "map ((\<lambda>a. rec_exec a (ys @ [y])) \<circ> (\<lambda>i. Cn (Suc (length ys)) s |
2123 [recf.id (Suc (length ys)) (i)])) [0..<length ys] = map Suc ys" |
2191 [recf.id (Suc (length ys)) (i)])) [0..<length ys] = map Suc ys" |
2124 proof - |
2192 proof - |
2125 have "map ((\<lambda>a. rec_eval a ys) \<circ> (\<lambda>i. Cn (length ys) s |
2193 have "map ((\<lambda>a. rec_exec a ys) \<circ> (\<lambda>i. Cn (length ys) s |
2126 [recf.id (length ys) (i)])) [0..<length ys] = map Suc ys" |
2194 [recf.id (length ys) (i)])) [0..<length ys] = map Suc ys" |
2127 apply(rule_tac ind, simp) |
2195 apply(rule_tac ind, simp) |
2128 done |
2196 done |
2129 moreover have |
2197 moreover have |
2130 "map ((\<lambda>a. rec_eval a (ys @ [y])) \<circ> (\<lambda>i. Cn (Suc (length ys)) s |
2198 "map ((\<lambda>a. rec_exec a (ys @ [y])) \<circ> (\<lambda>i. Cn (Suc (length ys)) s |
2131 [recf.id (Suc (length ys)) (i)])) [0..<length ys] |
2199 [recf.id (Suc (length ys)) (i)])) [0..<length ys] |
2132 = map ((\<lambda>a. rec_eval a ys) \<circ> (\<lambda>i. Cn (length ys) s |
2200 = map ((\<lambda>a. rec_exec a ys) \<circ> (\<lambda>i. Cn (length ys) s |
2133 [recf.id (length ys) (i)])) [0..<length ys]" |
2201 [recf.id (length ys) (i)])) [0..<length ys]" |
2134 apply(rule_tac map_ext, auto simp: rec_eval.simps nth_append) |
2202 apply(rule_tac map_ext, auto simp: rec_exec.simps nth_append) |
2135 done |
2203 done |
2136 ultimately show "?thesis" |
2204 ultimately show "?thesis" |
2137 by simp |
2205 by simp |
2138 qed |
2206 qed |
2139 next |
2207 next |
2293 |
2361 |
2294 text {* |
2362 text {* |
2295 The correctness of @{text "rec_newleft"}. |
2363 The correctness of @{text "rec_newleft"}. |
2296 *} |
2364 *} |
2297 lemma newleft_lemma: |
2365 lemma newleft_lemma: |
2298 "rec_eval rec_newleft [p, r, a] = newleft p r a" |
2366 "rec_exec rec_newleft [p, r, a] = newleft p r a" |
2299 proof(simp only: rec_newleft_def Let_def) |
2367 proof(simp only: rec_newleft_def Let_def) |
2300 let ?rgs = "[Cn 3 rec_newleft0 [recf.id 3 0, recf.id 3 1], Cn 3 rec_newleft2 |
2368 let ?rgs = "[Cn 3 rec_newleft0 [recf.id 3 0, recf.id 3 1], Cn 3 rec_newleft2 |
2301 [recf.id 3 0, recf.id 3 1], Cn 3 rec_newleft3 [recf.id 3 0, recf.id 3 1], recf.id 3 0]" |
2369 [recf.id 3 0, recf.id 3 1], Cn 3 rec_newleft3 [recf.id 3 0, recf.id 3 1], recf.id 3 0]" |
2302 let ?rrs = |
2370 let ?rrs = |
2303 "[Cn 3 rec_disj [Cn 3 rec_eq [recf.id 3 2, Cn 3 (constn 0) |
2371 "[Cn 3 rec_disj [Cn 3 rec_eq [recf.id 3 2, Cn 3 (constn 0) |
2304 [recf.id 3 0]], Cn 3 rec_eq [recf.id 3 2, Cn 3 (constn 1) [recf.id 3 0]]], |
2372 [recf.id 3 0]], Cn 3 rec_eq [recf.id 3 2, Cn 3 (constn 1) [recf.id 3 0]]], |
2305 Cn 3 rec_eq [recf.id 3 2, Cn 3 (constn 2) [recf.id 3 0]], |
2373 Cn 3 rec_eq [recf.id 3 2, Cn 3 (constn 2) [recf.id 3 0]], |
2306 Cn 3 rec_eq [recf.id 3 2, Cn 3 (constn 3) [recf.id 3 0]], |
2374 Cn 3 rec_eq [recf.id 3 2, Cn 3 (constn 3) [recf.id 3 0]], |
2307 Cn 3 rec_less [Cn 3 (constn 3) [recf.id 3 0], recf.id 3 2]]" |
2375 Cn 3 rec_less [Cn 3 (constn 3) [recf.id 3 0], recf.id 3 2]]" |
2308 have k1: "rec_eval (rec_embranch (zip ?rgs ?rrs)) [p, r, a] |
2376 have k1: "rec_exec (rec_embranch (zip ?rgs ?rrs)) [p, r, a] |
2309 = Embranch (zip (map rec_eval ?rgs) (map (\<lambda>r args. 0 < rec_eval r args) ?rrs)) [p, r, a]" |
2377 = Embranch (zip (map rec_exec ?rgs) (map (\<lambda>r args. 0 < rec_exec r args) ?rrs)) [p, r, a]" |
2310 apply(rule_tac embranch_lemma ) |
2378 apply(rule_tac embranch_lemma ) |
2311 apply(auto simp: numeral_3_eq_3 numeral_2_eq_2 rec_newleft0_def |
2379 apply(auto simp: numeral_3_eq_3 numeral_2_eq_2 rec_newleft0_def |
2312 rec_newleft1_def rec_newleft2_def rec_newleft3_def)+ |
2380 rec_newleft1_def rec_newleft2_def rec_newleft3_def)+ |
2313 apply(case_tac "a = 0 \<or> a = 1", rule_tac x = 0 in exI) |
2381 apply(case_tac "a = 0 \<or> a = 1", rule_tac x = 0 in exI) |
2314 prefer 2 |
2382 prefer 2 |
2315 apply(case_tac "a = 2", rule_tac x = "Suc 0" in exI) |
2383 apply(case_tac "a = 2", rule_tac x = "Suc 0" in exI) |
2316 prefer 2 |
2384 prefer 2 |
2317 apply(case_tac "a = 3", rule_tac x = "2" in exI) |
2385 apply(case_tac "a = 3", rule_tac x = "2" in exI) |
2318 prefer 2 |
2386 prefer 2 |
2319 apply(case_tac "a > 3", rule_tac x = "3" in exI, auto) |
2387 apply(case_tac "a > 3", rule_tac x = "3" in exI, auto) |
2320 apply(auto simp: rec_eval.simps) |
2388 apply(auto simp: rec_exec.simps) |
2321 apply(erule_tac [!] Suc_Suc_Suc_Suc_induct, auto simp: rec_eval.simps) |
2389 apply(erule_tac [!] Suc_Suc_Suc_Suc_induct, auto simp: rec_exec.simps) |
2322 done |
2390 done |
2323 have k2: "Embranch (zip (map rec_eval ?rgs) (map (\<lambda>r args. 0 < rec_eval r args) ?rrs)) [p, r, a] = newleft p r a" |
2391 have k2: "Embranch (zip (map rec_exec ?rgs) (map (\<lambda>r args. 0 < rec_exec r args) ?rrs)) [p, r, a] = newleft p r a" |
2324 apply(simp add: Embranch.simps) |
2392 apply(simp add: Embranch.simps) |
2325 apply(simp add: rec_eval.simps) |
2393 apply(simp add: rec_exec.simps) |
2326 apply(auto simp: newleft.simps rec_newleft0_def rec_eval.simps |
2394 apply(auto simp: newleft.simps rec_newleft0_def rec_exec.simps |
2327 rec_newleft1_def rec_newleft2_def rec_newleft3_def) |
2395 rec_newleft1_def rec_newleft2_def rec_newleft3_def) |
2328 done |
2396 done |
2329 from k1 and k2 show |
2397 from k1 and k2 show |
2330 "rec_eval (rec_embranch (zip ?rgs ?rrs)) [p, r, a] = newleft p r a" |
2398 "rec_exec (rec_embranch (zip ?rgs ?rrs)) [p, r, a] = newleft p r a" |
2331 by simp |
2399 by simp |
2332 qed |
2400 qed |
2333 |
2401 |
2334 text {* |
2402 text {* |
2335 The @{text "newrght"} function is one similar to @{text "newleft"}, but used to |
2403 The @{text "newrght"} function is one similar to @{text "newleft"}, but used to |
2403 "[Cn 3 rec_eq [recf.id 3 2, Cn 3 (constn 0) [recf.id 3 0]], Cn 3 rec_eq [recf.id 3 2, |
2471 "[Cn 3 rec_eq [recf.id 3 2, Cn 3 (constn 0) [recf.id 3 0]], Cn 3 rec_eq [recf.id 3 2, |
2404 Cn 3 (constn 1) [recf.id 3 0]], Cn 3 rec_eq [recf.id 3 2, Cn 3 (constn 2) [recf.id 3 0]], |
2472 Cn 3 (constn 1) [recf.id 3 0]], Cn 3 rec_eq [recf.id 3 2, Cn 3 (constn 2) [recf.id 3 0]], |
2405 Cn 3 rec_eq [recf.id 3 2, Cn 3 (constn 3) [recf.id 3 0]], |
2473 Cn 3 rec_eq [recf.id 3 2, Cn 3 (constn 3) [recf.id 3 0]], |
2406 Cn 3 rec_less [Cn 3 (constn 3) [recf.id 3 0], recf.id 3 2]]" |
2474 Cn 3 rec_less [Cn 3 (constn 3) [recf.id 3 0], recf.id 3 2]]" |
2407 |
2475 |
2408 have k1: "rec_eval (rec_embranch (zip ?rgs ?rrs)) [p, r, a] |
2476 have k1: "rec_exec (rec_embranch (zip ?rgs ?rrs)) [p, r, a] |
2409 = Embranch (zip (map rec_eval ?rgs) (map (\<lambda>r args. 0 < rec_eval r args) ?rrs)) [p, r, a]" |
2477 = Embranch (zip (map rec_exec ?rgs) (map (\<lambda>r args. 0 < rec_exec r args) ?rrs)) [p, r, a]" |
2410 apply(rule_tac embranch_lemma) |
2478 apply(rule_tac embranch_lemma) |
2411 apply(auto simp: numeral_3_eq_3 numeral_2_eq_2 rec_newrgt0_def |
2479 apply(auto simp: numeral_3_eq_3 numeral_2_eq_2 rec_newrgt0_def |
2412 rec_newrgt1_def rec_newrgt2_def rec_newrgt3_def)+ |
2480 rec_newrgt1_def rec_newrgt2_def rec_newrgt3_def)+ |
2413 apply(case_tac "a = 0", rule_tac x = 0 in exI) |
2481 apply(case_tac "a = 0", rule_tac x = 0 in exI) |
2414 prefer 2 |
2482 prefer 2 |
2416 prefer 2 |
2484 prefer 2 |
2417 apply(case_tac "a = 2", rule_tac x = "2" in exI) |
2485 apply(case_tac "a = 2", rule_tac x = "2" in exI) |
2418 prefer 2 |
2486 prefer 2 |
2419 apply(case_tac "a = 3", rule_tac x = "3" in exI) |
2487 apply(case_tac "a = 3", rule_tac x = "3" in exI) |
2420 prefer 2 |
2488 prefer 2 |
2421 apply(case_tac "a > 3", rule_tac x = "4" in exI, auto simp: rec_eval.simps) |
2489 apply(case_tac "a > 3", rule_tac x = "4" in exI, auto simp: rec_exec.simps) |
2422 apply(erule_tac [!] Suc_5_induct, auto simp: rec_eval.simps) |
2490 apply(erule_tac [!] Suc_5_induct, auto simp: rec_exec.simps) |
2423 done |
2491 done |
2424 have k2: "Embranch (zip (map rec_eval ?rgs) |
2492 have k2: "Embranch (zip (map rec_exec ?rgs) |
2425 (map (\<lambda>r args. 0 < rec_eval r args) ?rrs)) [p, r, a] = newrght p r a" |
2493 (map (\<lambda>r args. 0 < rec_exec r args) ?rrs)) [p, r, a] = newrght p r a" |
2426 apply(auto simp:Embranch.simps rec_eval.simps) |
2494 apply(auto simp:Embranch.simps rec_exec.simps) |
2427 apply(auto simp: newrght.simps rec_newrgt3_def rec_newrgt2_def |
2495 apply(auto simp: newrght.simps rec_newrgt3_def rec_newrgt2_def |
2428 rec_newrgt1_def rec_newrgt0_def rec_eval.simps |
2496 rec_newrgt1_def rec_newrgt0_def rec_exec.simps |
2429 scan_lemma) |
2497 scan_lemma) |
2430 done |
2498 done |
2431 from k1 and k2 show |
2499 from k1 and k2 show |
2432 "rec_eval (rec_embranch (zip ?rgs ?rrs)) [p, r, a] = |
2500 "rec_exec (rec_embranch (zip ?rgs ?rrs)) [p, r, a] = |
2433 newrght p r a" by simp |
2501 newrght p r a" by simp |
2434 qed |
2502 qed |
2435 |
2503 |
2436 declare Entry.simps[simp del] |
2504 declare Entry.simps[simp del] |
2437 |
2505 |
2502 "rec_trpl = Cn 3 rec_mult [Cn 3 rec_mult |
2570 "rec_trpl = Cn 3 rec_mult [Cn 3 rec_mult |
2503 [Cn 3 rec_power [Cn 3 (constn (Pi 0)) [id 3 0], id 3 0], |
2571 [Cn 3 rec_power [Cn 3 (constn (Pi 0)) [id 3 0], id 3 0], |
2504 Cn 3 rec_power [Cn 3 (constn (Pi 1)) [id 3 0], id 3 1]], |
2572 Cn 3 rec_power [Cn 3 (constn (Pi 1)) [id 3 0], id 3 1]], |
2505 Cn 3 rec_power [Cn 3 (constn (Pi 2)) [id 3 0], id 3 2]]" |
2573 Cn 3 rec_power [Cn 3 (constn (Pi 2)) [id 3 0], id 3 2]]" |
2506 declare trpl.simps[simp del] |
2574 declare trpl.simps[simp del] |
2507 lemma trpl_lemma: "rec_eval rec_trpl [p, q, r] = trpl p q r" |
2575 lemma trpl_lemma: "rec_exec rec_trpl [p, q, r] = trpl p q r" |
2508 by(auto simp: rec_trpl_def rec_eval.simps power_lemma trpl.simps) |
2576 by(auto simp: rec_trpl_def rec_exec.simps power_lemma trpl.simps) |
2509 |
2577 |
2510 text{*left, stat, rght: decode func*} |
2578 text{*left, stat, rght: decode func*} |
2511 fun left :: "nat \<Rightarrow> nat" |
2579 fun left :: "nat \<Rightarrow> nat" |
2512 where |
2580 where |
2513 "left c = lo c (Pi 0)" |
2581 "left c = lo c (Pi 0)" |
2553 [Cn vl (constn 0) [id vl 0], |
2621 [Cn vl (constn 0) [id vl 0], |
2554 Cn vl (constn 1) [id vl 0], |
2622 Cn vl (constn 1) [id vl 0], |
2555 Cn vl (rec_strt (vl - 1)) |
2623 Cn vl (rec_strt (vl - 1)) |
2556 (map (\<lambda> i. id vl (i)) [1..<vl])]" |
2624 (map (\<lambda> i. id vl (i)) [1..<vl])]" |
2557 |
2625 |
2558 lemma left_lemma: "rec_eval rec_left [c] = left c" |
2626 lemma left_lemma: "rec_exec rec_left [c] = left c" |
2559 by(simp add: rec_eval.simps rec_left_def left.simps lo_lemma) |
2627 by(simp add: rec_exec.simps rec_left_def left.simps lo_lemma) |
2560 |
2628 |
2561 lemma right_lemma: "rec_eval rec_right [c] = rght c" |
2629 lemma right_lemma: "rec_exec rec_right [c] = rght c" |
2562 by(simp add: rec_eval.simps rec_right_def rght.simps lo_lemma) |
2630 by(simp add: rec_exec.simps rec_right_def rght.simps lo_lemma) |
2563 |
2631 |
2564 lemma stat_lemma: "rec_eval rec_stat [c] = stat c" |
2632 lemma stat_lemma: "rec_exec rec_stat [c] = stat c" |
2565 by(simp add: rec_eval.simps rec_stat_def stat.simps lo_lemma) |
2633 by(simp add: rec_exec.simps rec_stat_def stat.simps lo_lemma) |
2566 |
2634 |
2567 declare rec_strt.simps[simp del] strt.simps[simp del] |
2635 declare rec_strt.simps[simp del] strt.simps[simp del] |
2568 |
2636 |
2569 lemma map_cons_eq: |
2637 lemma map_cons_eq: |
2570 "(map ((\<lambda>a. rec_eval a (m # xs)) \<circ> |
2638 "(map ((\<lambda>a. rec_exec a (m # xs)) \<circ> |
2571 (\<lambda>i. recf.id (Suc (length xs)) (i))) |
2639 (\<lambda>i. recf.id (Suc (length xs)) (i))) |
2572 [Suc 0..<Suc (length xs)]) |
2640 [Suc 0..<Suc (length xs)]) |
2573 = map (\<lambda> i. xs ! (i - 1)) [Suc 0..<Suc (length xs)]" |
2641 = map (\<lambda> i. xs ! (i - 1)) [Suc 0..<Suc (length xs)]" |
2574 apply(rule map_ext, auto) |
2642 apply(rule map_ext, auto) |
2575 apply(auto simp: rec_eval.simps nth_append nth_Cons split: nat.split) |
2643 apply(auto simp: rec_exec.simps nth_append nth_Cons split: nat.split) |
2576 done |
2644 done |
2577 |
2645 |
2578 lemma list_map_eq: |
2646 lemma list_map_eq: |
2579 "vl = length (xs::nat list) \<Longrightarrow> map (\<lambda> i. xs ! (i - 1)) |
2647 "vl = length (xs::nat list) \<Longrightarrow> map (\<lambda> i. xs ! (i - 1)) |
2580 [Suc 0..<Suc vl] = xs" |
2648 [Suc 0..<Suc vl] = xs" |
2614 done |
2682 done |
2615 qed |
2683 qed |
2616 |
2684 |
2617 lemma [elim]: |
2685 lemma [elim]: |
2618 "Suc 0 \<le> length xs \<Longrightarrow> |
2686 "Suc 0 \<le> length xs \<Longrightarrow> |
2619 (map ((\<lambda>a. rec_eval a (m # xs)) \<circ> |
2687 (map ((\<lambda>a. rec_exec a (m # xs)) \<circ> |
2620 (\<lambda>i. recf.id (Suc (length xs)) (i))) |
2688 (\<lambda>i. recf.id (Suc (length xs)) (i))) |
2621 [Suc 0..<length xs] @ [(m # xs) ! length xs]) = xs" |
2689 [Suc 0..<length xs] @ [(m # xs) ! length xs]) = xs" |
2622 using map_cons_eq[of m xs] |
2690 using map_cons_eq[of m xs] |
2623 apply(simp del: map_eq_conv add: rec_eval.simps) |
2691 apply(simp del: map_eq_conv add: rec_exec.simps) |
2624 using list_map_eq[of "length xs" xs] |
2692 using list_map_eq[of "length xs" xs] |
2625 apply(simp) |
2693 apply(simp) |
2626 done |
2694 done |
2627 |
2695 |
2628 |
2696 |
2629 lemma inpt_lemma: |
2697 lemma inpt_lemma: |
2630 "\<lbrakk>Suc (length xs) = vl\<rbrakk> \<Longrightarrow> |
2698 "\<lbrakk>Suc (length xs) = vl\<rbrakk> \<Longrightarrow> |
2631 rec_eval (rec_inpt vl) (m # xs) = inpt m xs" |
2699 rec_exec (rec_inpt vl) (m # xs) = inpt m xs" |
2632 apply(auto simp: rec_eval.simps rec_inpt_def |
2700 apply(auto simp: rec_exec.simps rec_inpt_def |
2633 trpl_lemma inpt.simps strt_lemma) |
2701 trpl_lemma inpt.simps strt_lemma) |
2634 apply(subgoal_tac |
2702 apply(subgoal_tac |
2635 "(map ((\<lambda>a. rec_eval a (m # xs)) \<circ> |
2703 "(map ((\<lambda>a. rec_exec a (m # xs)) \<circ> |
2636 (\<lambda>i. recf.id (Suc (length xs)) (i))) |
2704 (\<lambda>i. recf.id (Suc (length xs)) (i))) |
2637 [Suc 0..<length xs] @ [(m # xs) ! length xs]) = xs", simp) |
2705 [Suc 0..<length xs] @ [(m # xs) ! length xs]) = xs", simp) |
2638 apply(auto, case_tac xs, auto) |
2706 apply(auto, case_tac xs, auto) |
2639 done |
2707 done |
2640 |
2708 |
2683 where |
2751 where |
2684 "rec_conf = Pr 2 (Cn 2 rec_trpl [Cn 2 (constn 0) [id 2 0], Cn 2 (constn (Suc 0)) [id 2 0], id 2 1]) |
2752 "rec_conf = Pr 2 (Cn 2 rec_trpl [Cn 2 (constn 0) [id 2 0], Cn 2 (constn (Suc 0)) [id 2 0], id 2 1]) |
2685 (Cn 4 rec_newconf [id 4 0, id 4 3])" |
2753 (Cn 4 rec_newconf [id 4 0, id 4 3])" |
2686 |
2754 |
2687 lemma conf_step: |
2755 lemma conf_step: |
2688 "rec_eval rec_conf [m, r, Suc t] = |
2756 "rec_exec rec_conf [m, r, Suc t] = |
2689 rec_eval rec_newconf [m, rec_eval rec_conf [m, r, t]]" |
2757 rec_exec rec_newconf [m, rec_exec rec_conf [m, r, t]]" |
2690 proof - |
2758 proof - |
2691 have "rec_eval rec_conf ([m, r] @ [Suc t]) = |
2759 have "rec_exec rec_conf ([m, r] @ [Suc t]) = |
2692 rec_eval rec_newconf [m, rec_eval rec_conf [m, r, t]]" |
2760 rec_exec rec_newconf [m, rec_exec rec_conf [m, r, t]]" |
2693 by(simp only: rec_conf_def rec_pr_Suc_simp_rewrite, |
2761 by(simp only: rec_conf_def rec_pr_Suc_simp_rewrite, |
2694 simp add: rec_eval.simps) |
2762 simp add: rec_exec.simps) |
2695 thus "rec_eval rec_conf [m, r, Suc t] = |
2763 thus "rec_exec rec_conf [m, r, Suc t] = |
2696 rec_eval rec_newconf [m, rec_eval rec_conf [m, r, t]]" |
2764 rec_exec rec_newconf [m, rec_exec rec_conf [m, r, t]]" |
2697 by simp |
2765 by simp |
2698 qed |
2766 qed |
2699 |
2767 |
2700 text {* |
2768 text {* |
2701 The correctness of @{text "rec_conf"}. |
2769 The correctness of @{text "rec_conf"}. |
2702 *} |
2770 *} |
2703 lemma conf_lemma: |
2771 lemma conf_lemma: |
2704 "rec_eval rec_conf [m, r, t] = conf m r t" |
2772 "rec_exec rec_conf [m, r, t] = conf m r t" |
2705 apply(induct t) |
2773 apply(induct t) |
2706 apply(simp add: rec_conf_def rec_eval.simps conf.simps inpt_lemma trpl_lemma) |
2774 apply(simp add: rec_conf_def rec_exec.simps conf.simps inpt_lemma trpl_lemma) |
2707 apply(simp add: conf_step conf.simps) |
2775 apply(simp add: conf_step conf.simps) |
2708 done |
2776 done |
2709 |
2777 |
2710 text {* |
2778 text {* |
2711 @{text "NSTD c"} returns true if the configureation coded by @{text "c"} is no a stardard |
2779 @{text "NSTD c"} returns true if the configureation coded by @{text "c"} is no a stardard |
2733 [Cn 1 rec_add |
2801 [Cn 1 rec_add |
2734 [rec_right, constn 1], |
2802 [rec_right, constn 1], |
2735 constn 2]], constn 1]]], |
2803 constn 2]], constn 1]]], |
2736 Cn 1 rec_eq [rec_right, constn 0]]" |
2804 Cn 1 rec_eq [rec_right, constn 0]]" |
2737 |
2805 |
2738 lemma NSTD_lemma1: "rec_eval rec_NSTD [c] = Suc 0 \<or> |
2806 lemma NSTD_lemma1: "rec_exec rec_NSTD [c] = Suc 0 \<or> |
2739 rec_eval rec_NSTD [c] = 0" |
2807 rec_exec rec_NSTD [c] = 0" |
2740 by(simp add: rec_eval.simps rec_NSTD_def) |
2808 by(simp add: rec_exec.simps rec_NSTD_def) |
2741 |
2809 |
2742 declare NSTD.simps[simp del] |
2810 declare NSTD.simps[simp del] |
2743 lemma NSTD_lemma2': "(rec_eval rec_NSTD [c] = Suc 0) \<Longrightarrow> NSTD c" |
2811 lemma NSTD_lemma2': "(rec_exec rec_NSTD [c] = Suc 0) \<Longrightarrow> NSTD c" |
2744 apply(simp add: rec_eval.simps rec_NSTD_def stat_lemma left_lemma |
2812 apply(simp add: rec_exec.simps rec_NSTD_def stat_lemma left_lemma |
2745 lg_lemma right_lemma power_lemma NSTD.simps eq_lemma) |
2813 lg_lemma right_lemma power_lemma NSTD.simps eq_lemma) |
2746 apply(auto) |
2814 apply(auto) |
2747 apply(case_tac "0 < left c", simp, simp) |
2815 apply(case_tac "0 < left c", simp, simp) |
2748 done |
2816 done |
2749 |
2817 |
2750 lemma NSTD_lemma2'': |
2818 lemma NSTD_lemma2'': |
2751 "NSTD c \<Longrightarrow> (rec_eval rec_NSTD [c] = Suc 0)" |
2819 "NSTD c \<Longrightarrow> (rec_exec rec_NSTD [c] = Suc 0)" |
2752 apply(simp add: rec_eval.simps rec_NSTD_def stat_lemma |
2820 apply(simp add: rec_exec.simps rec_NSTD_def stat_lemma |
2753 left_lemma lg_lemma right_lemma power_lemma NSTD.simps) |
2821 left_lemma lg_lemma right_lemma power_lemma NSTD.simps) |
2754 apply(auto split: if_splits) |
2822 apply(auto split: if_splits) |
2755 done |
2823 done |
2756 |
2824 |
2757 text {* |
2825 text {* |
2758 The correctness of @{text "NSTD"}. |
2826 The correctness of @{text "NSTD"}. |
2759 *} |
2827 *} |
2760 lemma NSTD_lemma2: "(rec_eval rec_NSTD [c] = Suc 0) = NSTD c" |
2828 lemma NSTD_lemma2: "(rec_exec rec_NSTD [c] = Suc 0) = NSTD c" |
2761 using NSTD_lemma1 |
2829 using NSTD_lemma1 |
2762 apply(auto intro: NSTD_lemma2' NSTD_lemma2'') |
2830 apply(auto intro: NSTD_lemma2' NSTD_lemma2'') |
2763 done |
2831 done |
2764 |
2832 |
2765 fun nstd :: "nat \<Rightarrow> nat" |
2833 fun nstd :: "nat \<Rightarrow> nat" |
2766 where |
2834 where |
2767 "nstd c = (if NSTD c then 1 else 0)" |
2835 "nstd c = (if NSTD c then 1 else 0)" |
2768 |
2836 |
2769 lemma nstd_lemma: "rec_eval rec_NSTD [c] = nstd c" |
2837 lemma nstd_lemma: "rec_exec rec_NSTD [c] = nstd c" |
2770 using NSTD_lemma1 |
2838 using NSTD_lemma1 |
2771 apply(simp add: NSTD_lemma2, auto) |
2839 apply(simp add: NSTD_lemma2, auto) |
2772 done |
2840 done |
2773 |
2841 |
2774 text{* |
2842 text{* |
2983 rec_newstat_def) |
3051 rec_newstat_def) |
2984 apply(tactic {* resolve_tac [@{thm prime_cn}, |
3052 apply(tactic {* resolve_tac [@{thm prime_cn}, |
2985 @{thm prime_id}, @{thm prime_pr}] 1*}, auto+)+ |
3053 @{thm prime_id}, @{thm prime_pr}] 1*}, auto+)+ |
2986 done |
3054 done |
2987 |
3055 |
2988 lemma primerec_terminates: |
3056 lemma primerec_terminate: |
2989 "\<lbrakk>primerec f x; length xs = x\<rbrakk> \<Longrightarrow> terminates f xs" |
3057 "\<lbrakk>primerec f x; length xs = x\<rbrakk> \<Longrightarrow> terminate f xs" |
2990 proof(induct arbitrary: xs rule: primerec.induct) |
3058 proof(induct arbitrary: xs rule: primerec.induct) |
2991 fix xs |
3059 fix xs |
2992 assume "length (xs::nat list) = Suc 0" thus "terminates z xs" |
3060 assume "length (xs::nat list) = Suc 0" thus "terminate z xs" |
2993 by(case_tac xs, auto intro: termi_z) |
3061 by(case_tac xs, auto intro: termi_z) |
2994 next |
3062 next |
2995 fix xs |
3063 fix xs |
2996 assume "length (xs::nat list) = Suc 0" thus "terminates s xs" |
3064 assume "length (xs::nat list) = Suc 0" thus "terminate s xs" |
2997 by(case_tac xs, auto intro: termi_s) |
3065 by(case_tac xs, auto intro: termi_s) |
2998 next |
3066 next |
2999 fix n m xs |
3067 fix n m xs |
3000 assume "n < m" "length (xs::nat list) = m" thus "terminates (id m n) xs" |
3068 assume "n < m" "length (xs::nat list) = m" thus "terminate (id m n) xs" |
3001 by(erule_tac termi_id, simp) |
3069 by(erule_tac termi_id, simp) |
3002 next |
3070 next |
3003 fix f k gs m n xs |
3071 fix f k gs m n xs |
3004 assume ind: "\<forall>i<length gs. primerec (gs ! i) m \<and> (\<forall>x. length x = m \<longrightarrow> terminates (gs ! i) x)" |
3072 assume ind: "\<forall>i<length gs. primerec (gs ! i) m \<and> (\<forall>x. length x = m \<longrightarrow> terminate (gs ! i) x)" |
3005 and ind2: "\<And> xs. length xs = k \<Longrightarrow> terminates f xs" |
3073 and ind2: "\<And> xs. length xs = k \<Longrightarrow> terminate f xs" |
3006 and h: "primerec f k" "length gs = k" "m = n" "length (xs::nat list) = m" |
3074 and h: "primerec f k" "length gs = k" "m = n" "length (xs::nat list) = m" |
3007 have "terminates f (map (\<lambda>g. rec_eval g xs) gs)" |
3075 have "terminate f (map (\<lambda>g. rec_exec g xs) gs)" |
3008 using ind2[of "(map (\<lambda>g. rec_eval g xs) gs)"] h |
3076 using ind2[of "(map (\<lambda>g. rec_exec g xs) gs)"] h |
3009 by simp |
3077 by simp |
3010 moreover have "\<forall>g\<in>set gs. terminates g xs" |
3078 moreover have "\<forall>g\<in>set gs. terminate g xs" |
3011 using ind h |
3079 using ind h |
3012 by(auto simp: set_conv_nth) |
3080 by(auto simp: set_conv_nth) |
3013 ultimately show "terminates (Cn n f gs) xs" |
3081 ultimately show "terminate (Cn n f gs) xs" |
3014 using h |
3082 using h |
3015 by(rule_tac termi_cn, auto) |
3083 by(rule_tac termi_cn, auto) |
3016 next |
3084 next |
3017 fix f n g m xs |
3085 fix f n g m xs |
3018 assume ind1: "\<And>xs. length xs = n \<Longrightarrow> terminates f xs" |
3086 assume ind1: "\<And>xs. length xs = n \<Longrightarrow> terminate f xs" |
3019 and ind2: "\<And>xs. length xs = Suc (Suc n) \<Longrightarrow> terminates g xs" |
3087 and ind2: "\<And>xs. length xs = Suc (Suc n) \<Longrightarrow> terminate g xs" |
3020 and h: "primerec f n" " primerec g (Suc (Suc n))" " m = Suc n" "length (xs::nat list) = m" |
3088 and h: "primerec f n" " primerec g (Suc (Suc n))" " m = Suc n" "length (xs::nat list) = m" |
3021 have "\<forall>y<last xs. terminates g (butlast xs @ [y, rec_eval (Pr n f g) (butlast xs @ [y])])" |
3089 have "\<forall>y<last xs. terminate g (butlast xs @ [y, rec_exec (Pr n f g) (butlast xs @ [y])])" |
3022 using h |
3090 using h |
3023 apply(auto) |
3091 apply(auto) |
3024 by(rule_tac ind2, simp) |
3092 by(rule_tac ind2, simp) |
3025 moreover have "terminates f (butlast xs)" |
3093 moreover have "terminate f (butlast xs)" |
3026 using ind1[of "butlast xs"] h |
3094 using ind1[of "butlast xs"] h |
3027 by simp |
3095 by simp |
3028 moreover have "length (butlast xs) = n" |
3096 moreover have "length (butlast xs) = n" |
3029 using h by simp |
3097 using h by simp |
3030 ultimately have "terminates (Pr n f g) (butlast xs @ [last xs])" |
3098 ultimately have "terminate (Pr n f g) (butlast xs @ [last xs])" |
3031 by(rule_tac termi_pr, simp_all) |
3099 by(rule_tac termi_pr, simp_all) |
3032 thus "terminates (Pr n f g) xs" |
3100 thus "terminate (Pr n f g) xs" |
3033 using h |
3101 using h |
3034 by(case_tac "xs = []", auto) |
3102 by(case_tac "xs = []", auto) |
3035 qed |
3103 qed |
3036 |
3104 |
3037 text {* |
3105 text {* |
3038 The following lemma gives the correctness of @{text "rec_halt"}. |
3106 The following lemma gives the correctness of @{text "rec_halt"}. |
3039 It says: if @{text "rec_halt"} calculates that the TM coded by @{text "m"} |
3107 It says: if @{text "rec_halt"} calculates that the TM coded by @{text "m"} |
3040 will reach a standard final configuration after @{text "t"} steps of execution, |
3108 will reach a standard final configuration after @{text "t"} steps of execution, then it is indeed so. |
3041 then it is indeed so. |
|
3042 *} |
3109 *} |
3043 |
3110 |
3044 |
|
3045 text {*F: universal machine*} |
3111 text {*F: universal machine*} |
3046 |
3112 |
3047 text {* |
3113 text {* |
3048 @{text "valu r"} extracts computing result out of the right number @{text "r"}. |
3114 @{text "valu r"} extracts computing result out of the right number @{text "r"}. |
3049 *} |
3115 *} |
3099 k < length ys\<rbrakk> \<Longrightarrow> |
3165 k < length ys\<rbrakk> \<Longrightarrow> |
3100 (get_fstn_args (length ys) (length ys) ! k) = |
3166 (get_fstn_args (length ys) (length ys) ! k) = |
3101 id (length ys) (k)" |
3167 id (length ys) (k)" |
3102 by(erule_tac get_fstn_args_nth) |
3168 by(erule_tac get_fstn_args_nth) |
3103 |
3169 |
3104 lemma terminates_halt_lemma: |
3170 lemma terminate_halt_lemma: |
3105 "\<lbrakk>rec_eval rec_nonstop ([m, r] @ [t]) = 0; |
3171 "\<lbrakk>rec_exec rec_nonstop ([m, r] @ [t]) = 0; |
3106 \<forall>i<t. 0 < rec_eval rec_nonstop ([m, r] @ [i])\<rbrakk> \<Longrightarrow> terminates rec_halt [m, r]" |
3172 \<forall>i<t. 0 < rec_exec rec_nonstop ([m, r] @ [i])\<rbrakk> \<Longrightarrow> terminate rec_halt [m, r]" |
3107 apply(simp add: rec_halt_def) |
3173 apply(simp add: rec_halt_def) |
3108 apply(rule_tac termi_mn, auto) |
3174 apply(rule_tac termi_mn, auto) |
3109 apply(rule_tac [!] primerec_terminates, auto) |
3175 apply(rule_tac [!] primerec_terminate, auto) |
3110 done |
3176 done |
3111 |
3177 |
3112 |
3178 |
3113 text {* |
3179 text {* |
3114 The correctness of @{text "rec_F"}, halt case. |
3180 The correctness of @{text "rec_F"}, halt case. |
3115 *} |
3181 *} |
3116 |
3182 |
3117 lemma F_lemma: "rec_eval rec_halt [m, r] = t \<Longrightarrow> rec_eval rec_F [m, r] = (valu (rght (conf m r t)))" |
3183 lemma F_lemma: "rec_exec rec_halt [m, r] = t \<Longrightarrow> rec_exec rec_F [m, r] = (valu (rght (conf m r t)))" |
3118 by(simp add: rec_F_def rec_eval.simps value_lemma right_lemma conf_lemma halt_lemma) |
3184 by(simp add: rec_F_def rec_exec.simps value_lemma right_lemma conf_lemma halt_lemma) |
3119 |
3185 |
3120 lemma terminates_F_lemma: "terminates rec_halt [m, r] \<Longrightarrow> terminates rec_F [m, r]" |
3186 lemma terminate_F_lemma: "terminate rec_halt [m, r] \<Longrightarrow> terminate rec_F [m, r]" |
3121 apply(simp add: rec_F_def) |
3187 apply(simp add: rec_F_def) |
3122 apply(rule_tac termi_cn, auto) |
3188 apply(rule_tac termi_cn, auto) |
3123 apply(rule_tac primerec_terminates, auto) |
3189 apply(rule_tac primerec_terminate, auto) |
3124 apply(rule_tac termi_cn, auto) |
3190 apply(rule_tac termi_cn, auto) |
3125 apply(rule_tac primerec_terminates, auto) |
3191 apply(rule_tac primerec_terminate, auto) |
3126 apply(rule_tac termi_cn, auto) |
3192 apply(rule_tac termi_cn, auto) |
3127 apply(rule_tac primerec_terminates, auto) |
3193 apply(rule_tac primerec_terminate, auto) |
3128 apply(rule_tac [!] termi_id, auto) |
3194 apply(rule_tac [!] termi_id, auto) |
3129 done |
3195 done |
3130 |
3196 |
3131 text {* |
3197 text {* |
3132 The correctness of @{text "rec_F"}, nonhalt case. |
3198 The correctness of @{text "rec_F"}, nonhalt case. |
3246 "\<lbrakk>i < length nl; \<not> Suc 0 < godel_code nl\<rbrakk> \<Longrightarrow> nl ! i = 0" |
3312 "\<lbrakk>i < length nl; \<not> Suc 0 < godel_code nl\<rbrakk> \<Longrightarrow> nl ! i = 0" |
3247 using godel_code_great[of nl] godel_code_eq_1[of nl] |
3313 using godel_code_great[of nl] godel_code_eq_1[of nl] |
3248 apply(simp) |
3314 apply(simp) |
3249 done |
3315 done |
3250 |
3316 |
|
3317 lemma prime_coprime: "\<lbrakk>Prime x; Prime y; x\<noteq>y\<rbrakk> \<Longrightarrow> coprime x y" |
|
3318 proof(simp only: Prime.simps coprime_nat, auto simp: dvd_def, |
|
3319 rule_tac classical, simp) |
|
3320 fix d k ka |
|
3321 assume case_ka: "\<forall>u<d * ka. \<forall>v<d * ka. u * v \<noteq> d * ka" |
|
3322 and case_k: "\<forall>u<d * k. \<forall>v<d * k. u * v \<noteq> d * k" |
|
3323 and h: "(0::nat) < d" "d \<noteq> Suc 0" "Suc 0 < d * ka" |
|
3324 "ka \<noteq> k" "Suc 0 < d * k" |
|
3325 from h have "k > Suc 0 \<or> ka >Suc 0" |
|
3326 apply(auto) |
|
3327 apply(case_tac ka, simp, simp) |
|
3328 apply(case_tac k, simp, simp) |
|
3329 done |
|
3330 from this show "False" |
|
3331 proof(erule_tac disjE) |
|
3332 assume "(Suc 0::nat) < k" |
|
3333 hence "k < d*k \<and> d < d*k" |
|
3334 using h |
|
3335 by(auto) |
|
3336 thus "?thesis" |
|
3337 using case_k |
|
3338 apply(erule_tac x = d in allE) |
|
3339 apply(simp) |
|
3340 apply(erule_tac x = k in allE) |
|
3341 apply(simp) |
|
3342 done |
|
3343 next |
|
3344 assume "(Suc 0::nat) < ka" |
|
3345 hence "ka < d * ka \<and> d < d*ka" |
|
3346 using h by auto |
|
3347 thus "?thesis" |
|
3348 using case_ka |
|
3349 apply(erule_tac x = d in allE) |
|
3350 apply(simp) |
|
3351 apply(erule_tac x = ka in allE) |
|
3352 apply(simp) |
|
3353 done |
|
3354 qed |
|
3355 qed |
|
3356 |
3251 lemma Pi_inc: "Pi (Suc i) > Pi i" |
3357 lemma Pi_inc: "Pi (Suc i) > Pi i" |
3252 proof(simp add: Pi.simps Np.simps) |
3358 proof(simp add: Pi.simps Np.simps) |
3253 let ?setx = "{y. y \<le> Suc (Pi i!) \<and> Pi i < y \<and> prime y}" |
3359 let ?setx = "{y. y \<le> Suc (Pi i!) \<and> Pi i < y \<and> Prime y}" |
3254 have "finite ?setx" by simp |
3360 have "finite ?setx" by simp |
3255 moreover have "?setx \<noteq> {}" |
3361 moreover have "?setx \<noteq> {}" |
3256 using prime_ex[of "Pi i"] |
3362 using prime_ex[of "Pi i"] |
3257 apply(auto) |
3363 apply(auto) |
3258 done |
3364 done |
4210 qed |
4316 qed |
4211 |
4317 |
4212 lemma rec_t_eq_steps: |
4318 lemma rec_t_eq_steps: |
4213 "tm_wf (tp,0) \<Longrightarrow> |
4319 "tm_wf (tp,0) \<Longrightarrow> |
4214 trpl_code (steps0 (Suc 0, Bk\<up>l, <lm>) tp stp) = |
4320 trpl_code (steps0 (Suc 0, Bk\<up>l, <lm>) tp stp) = |
4215 rec_eval rec_conf [code tp, bl2wc (<lm>), stp]" |
4321 rec_exec rec_conf [code tp, bl2wc (<lm>), stp]" |
4216 proof(induct stp) |
4322 proof(induct stp) |
4217 case 0 thus "?case" by(simp) |
4323 case 0 thus "?case" by(simp) |
4218 next |
4324 next |
4219 case (Suc n) thus "?case" |
4325 case (Suc n) thus "?case" |
4220 proof - |
4326 proof - |
4221 assume ind: |
4327 assume ind: |
4222 "tm_wf (tp,0) \<Longrightarrow> trpl_code (steps0 (Suc 0, Bk\<up> l, <lm>) tp n) |
4328 "tm_wf (tp,0) \<Longrightarrow> trpl_code (steps0 (Suc 0, Bk\<up> l, <lm>) tp n) |
4223 = rec_eval rec_conf [code tp, bl2wc (<lm>), n]" |
4329 = rec_exec rec_conf [code tp, bl2wc (<lm>), n]" |
4224 and h: "tm_wf (tp, 0)" |
4330 and h: "tm_wf (tp, 0)" |
4225 show |
4331 show |
4226 "trpl_code (steps0 (Suc 0, Bk\<up> l, <lm>) tp (Suc n)) = |
4332 "trpl_code (steps0 (Suc 0, Bk\<up> l, <lm>) tp (Suc n)) = |
4227 rec_eval rec_conf [code tp, bl2wc (<lm>), Suc n]" |
4333 rec_exec rec_conf [code tp, bl2wc (<lm>), Suc n]" |
4228 proof(case_tac "steps0 (Suc 0, Bk\<up> l, <lm>) tp n", |
4334 proof(case_tac "steps0 (Suc 0, Bk\<up> l, <lm>) tp n", |
4229 simp only: step_red conf_lemma conf.simps) |
4335 simp only: step_red conf_lemma conf.simps) |
4230 fix a b c |
4336 fix a b c |
4231 assume g: "steps0 (Suc 0, Bk\<up> l, <lm>) tp n = (a, b, c) " |
4337 assume g: "steps0 (Suc 0, Bk\<up> l, <lm>) tp n = (a, b, c) " |
4232 hence "conf (code tp) (bl2wc (<lm>)) n= trpl_code (a, b, c)" |
4338 hence "conf (code tp) (bl2wc (<lm>)) n= trpl_code (a, b, c)" |
4233 using ind h |
4339 using ind h |
4234 apply(simp add: conf_lemma) |
4340 apply(simp add: conf_lemma) |
4235 done |
4341 done |
4236 moreover hence |
4342 moreover hence |
4237 "trpl_code (step0 (a, b, c) tp) = |
4343 "trpl_code (step0 (a, b, c) tp) = |
4238 rec_eval rec_newconf [code tp, trpl_code (a, b, c)]" |
4344 rec_exec rec_newconf [code tp, trpl_code (a, b, c)]" |
4239 apply(rule_tac rec_t_eq_step) |
4345 apply(rule_tac rec_t_eq_step) |
4240 using h g |
4346 using h g |
4241 apply(simp add: state_in_range) |
4347 apply(simp add: state_in_range) |
4242 done |
4348 done |
4243 ultimately show |
4349 ultimately show |
4291 |
4397 |
4292 lemma nonstop_t_eq: |
4398 lemma nonstop_t_eq: |
4293 "\<lbrakk>steps0 (Suc 0, Bk\<up>l, <lm>) tp stp = (0, Bk\<up> m, Oc\<up> rs @ Bk\<up> n); |
4399 "\<lbrakk>steps0 (Suc 0, Bk\<up>l, <lm>) tp stp = (0, Bk\<up> m, Oc\<up> rs @ Bk\<up> n); |
4294 tm_wf (tp, 0); |
4400 tm_wf (tp, 0); |
4295 rs > 0\<rbrakk> |
4401 rs > 0\<rbrakk> |
4296 \<Longrightarrow> rec_eval rec_nonstop [code tp, bl2wc (<lm>), stp] = 0" |
4402 \<Longrightarrow> rec_exec rec_nonstop [code tp, bl2wc (<lm>), stp] = 0" |
4297 proof(simp add: nonstop_lemma nonstop.simps nstd.simps) |
4403 proof(simp add: nonstop_lemma nonstop.simps nstd.simps) |
4298 assume h: "steps0 (Suc 0, Bk\<up>l, <lm>) tp stp = (0, Bk\<up> m, Oc\<up> rs @ Bk\<up> n)" |
4404 assume h: "steps0 (Suc 0, Bk\<up>l, <lm>) tp stp = (0, Bk\<up> m, Oc\<up> rs @ Bk\<up> n)" |
4299 and tc_t: "tm_wf (tp, 0)" "rs > 0" |
4405 and tc_t: "tm_wf (tp, 0)" "rs > 0" |
4300 have g: "rec_eval rec_conf [code tp, bl2wc (<lm>), stp] = |
4406 have g: "rec_exec rec_conf [code tp, bl2wc (<lm>), stp] = |
4301 trpl_code (0, Bk\<up> m, Oc\<up> rs@Bk\<up> n)" |
4407 trpl_code (0, Bk\<up> m, Oc\<up> rs@Bk\<up> n)" |
4302 using rec_t_eq_steps[of tp l lm stp] tc_t h |
4408 using rec_t_eq_steps[of tp l lm stp] tc_t h |
4303 by(simp) |
4409 by(simp) |
4304 thus "\<not> NSTD (conf (code tp) (bl2wc (<lm>)) stp)" |
4410 thus "\<not> NSTD (conf (code tp) (bl2wc (<lm>)) stp)" |
4305 proof(auto simp: NSTD.simps) |
4411 proof(auto simp: NSTD.simps) |
4485 text {* |
4591 text {* |
4486 The correntess of @{text "rec_F"} which relates the interpreter function @{text "rec_F"} with the |
4592 The correntess of @{text "rec_F"} which relates the interpreter function @{text "rec_F"} with the |
4487 execution of of TMs. |
4593 execution of of TMs. |
4488 *} |
4594 *} |
4489 |
4595 |
4490 lemma terminates_halt: |
4596 lemma terminate_halt: |
4491 "\<lbrakk>steps0 (Suc 0, Bk\<up>l, <lm>) tp stp = (0, Bk\<up>m, Oc\<up>rs@Bk\<up>n); |
4597 "\<lbrakk>steps0 (Suc 0, Bk\<up>l, <lm>) tp stp = (0, Bk\<up>m, Oc\<up>rs@Bk\<up>n); |
4492 tm_wf (tp,0); 0<rs\<rbrakk> \<Longrightarrow> terminates rec_halt [code tp, (bl2wc (<lm>))]" |
4598 tm_wf (tp,0); 0<rs\<rbrakk> \<Longrightarrow> terminate rec_halt [code tp, (bl2wc (<lm>))]" |
4493 apply(frule_tac halt_least_step, auto) |
4599 apply(frule_tac halt_least_step, auto) |
4494 thm terminates_halt_lemma |
4600 thm terminate_halt_lemma |
4495 apply(rule_tac t = stpa in terminates_halt_lemma) |
4601 apply(rule_tac t = stpa in terminate_halt_lemma) |
4496 apply(simp_all add: nonstop_lemma, auto) |
4602 apply(simp_all add: nonstop_lemma, auto) |
4497 done |
4603 done |
4498 |
4604 |
4499 lemma terminates_F: |
4605 lemma terminate_F: |
4500 "\<lbrakk>steps0 (Suc 0, Bk\<up>l, <lm>) tp stp = (0, Bk\<up>m, Oc\<up>rs@Bk\<up>n); |
4606 "\<lbrakk>steps0 (Suc 0, Bk\<up>l, <lm>) tp stp = (0, Bk\<up>m, Oc\<up>rs@Bk\<up>n); |
4501 tm_wf (tp,0); 0<rs\<rbrakk> \<Longrightarrow> terminates rec_F [code tp, (bl2wc (<lm>))]" |
4607 tm_wf (tp,0); 0<rs\<rbrakk> \<Longrightarrow> terminate rec_F [code tp, (bl2wc (<lm>))]" |
4502 apply(drule_tac terminates_halt, simp_all) |
4608 apply(drule_tac terminate_halt, simp_all) |
4503 apply(erule_tac terminates_F_lemma) |
4609 apply(erule_tac terminate_F_lemma) |
4504 done |
4610 done |
4505 |
4611 |
4506 lemma F_correct: |
4612 lemma F_correct: |
4507 "\<lbrakk>steps0 (Suc 0, Bk\<up>l, <lm>) tp stp = (0, Bk\<up>m, Oc\<up>rs@Bk\<up>n); |
4613 "\<lbrakk>steps0 (Suc 0, Bk\<up>l, <lm>) tp stp = (0, Bk\<up>m, Oc\<up>rs@Bk\<up>n); |
4508 tm_wf (tp,0); 0<rs\<rbrakk> |
4614 tm_wf (tp,0); 0<rs\<rbrakk> |
4509 \<Longrightarrow> rec_eval rec_F [code tp, (bl2wc (<lm>))] = (rs - Suc 0)" |
4615 \<Longrightarrow> rec_exec rec_F [code tp, (bl2wc (<lm>))] = (rs - Suc 0)" |
4510 apply(frule_tac halt_least_step, auto) |
4616 apply(frule_tac halt_least_step, auto) |
4511 apply(frule_tac nonstop_t_eq, auto simp: nonstop_lemma) |
4617 apply(frule_tac nonstop_t_eq, auto simp: nonstop_lemma) |
4512 using rec_t_eq_steps[of tp l lm stp] |
4618 using rec_t_eq_steps[of tp l lm stp] |
4513 apply(simp add: conf_lemma) |
4619 apply(simp add: conf_lemma) |
4514 proof - |
4620 proof - |
4521 "steps0 (Suc 0, Bk\<up> l, <lm>) tp stp = (0, Bk\<up> m, Oc\<up> rs @ Bk\<up> n)" |
4627 "steps0 (Suc 0, Bk\<up> l, <lm>) tp stp = (0, Bk\<up> m, Oc\<up> rs @ Bk\<up> n)" |
4522 hence g1: "conf (code tp) (bl2wc (<lm>)) stpa = trpl_code (0, Bk\<up> m, Oc\<up> rs @ Bk\<up>n)" |
4628 hence g1: "conf (code tp) (bl2wc (<lm>)) stpa = trpl_code (0, Bk\<up> m, Oc\<up> rs @ Bk\<up>n)" |
4523 using halt_state_keep[of "code tp" lm stpa stp] |
4629 using halt_state_keep[of "code tp" lm stpa stp] |
4524 by(simp) |
4630 by(simp) |
4525 moreover have g2: |
4631 moreover have g2: |
4526 "rec_eval rec_halt [code tp, (bl2wc (<lm>))] = stpa" |
4632 "rec_exec rec_halt [code tp, (bl2wc (<lm>))] = stpa" |
4527 using h |
4633 using h |
4528 by(auto simp: rec_eval.simps rec_halt_def nonstop_lemma intro!: Least_equality) |
4634 by(auto simp: rec_exec.simps rec_halt_def nonstop_lemma intro!: Least_equality) |
4529 show |
4635 show |
4530 "rec_eval rec_F [code tp, (bl2wc (<lm>))] = (rs - Suc 0)" |
4636 "rec_exec rec_F [code tp, (bl2wc (<lm>))] = (rs - Suc 0)" |
4531 proof - |
4637 proof - |
4532 have |
4638 have |
4533 "valu (rght (conf (code tp) (bl2wc (<lm>)) stpa)) = rs - Suc 0" |
4639 "valu (rght (conf (code tp) (bl2wc (<lm>)) stpa)) = rs - Suc 0" |
4534 using g1 |
4640 using g1 |
4535 apply(simp add: valu.simps trpl_code.simps |
4641 apply(simp add: valu.simps trpl_code.simps |
4536 bl2wc.simps bl2nat_append lg_power) |
4642 bl2wc.simps bl2nat_append lg_power) |
4537 done |
4643 done |
4538 thus "?thesis" |
4644 thus "?thesis" |
4539 by(simp add: rec_eval.simps F_lemma g2) |
4645 by(simp add: rec_exec.simps F_lemma g2) |
4540 qed |
4646 qed |
4541 qed |
4647 qed |
4542 |
4648 |
4543 end |
4649 end |