13405
|
1 |
(* Title: HOL/Extraction/QuotRem.thy
|
|
2 |
ID: $Id$
|
|
3 |
Author: Stefan Berghofer, TU Muenchen
|
|
4 |
*)
|
|
5 |
|
|
6 |
header {* Quotient and remainder *}
|
|
7 |
|
16417
|
8 |
theory QuotRem imports Main begin
|
13405
|
9 |
text {* Derivation of quotient and remainder using program extraction. *}
|
|
10 |
|
13931
|
11 |
lemma nat_eq_dec: "\<And>n::nat. m = n \<or> m \<noteq> n"
|
|
12 |
apply (induct m)
|
|
13 |
apply (case_tac n)
|
15244
|
14 |
apply (case_tac [3] n)
|
17604
|
15 |
apply (simp only: nat.simps, iprover?)+
|
13405
|
16 |
done
|
|
17 |
|
13931
|
18 |
theorem division: "\<exists>r q. a = Suc b * q + r \<and> r \<le> b"
|
|
19 |
proof (induct a)
|
|
20 |
case 0
|
|
21 |
have "0 = Suc b * 0 + 0 \<and> 0 \<le> b" by simp
|
17604
|
22 |
thus ?case by iprover
|
13931
|
23 |
next
|
|
24 |
case (Suc a)
|
17604
|
25 |
then obtain r q where I: "a = Suc b * q + r" and "r \<le> b" by iprover
|
13931
|
26 |
from nat_eq_dec show ?case
|
|
27 |
proof
|
|
28 |
assume "r = b"
|
|
29 |
with I have "Suc a = Suc b * (Suc q) + 0 \<and> 0 \<le> b" by simp
|
17604
|
30 |
thus ?case by iprover
|
13931
|
31 |
next
|
|
32 |
assume "r \<noteq> b"
|
|
33 |
hence "r < b" by (simp add: order_less_le)
|
|
34 |
with I have "Suc a = Suc b * q + (Suc r) \<and> (Suc r) \<le> b" by simp
|
17604
|
35 |
thus ?case by iprover
|
13931
|
36 |
qed
|
|
37 |
qed
|
13405
|
38 |
|
|
39 |
extract division
|
|
40 |
|
|
41 |
text {*
|
|
42 |
The program extracted from the above proof looks as follows
|
|
43 |
@{thm [display] division_def [no_vars]}
|
|
44 |
The corresponding correctness theorem is
|
|
45 |
@{thm [display] division_correctness [no_vars]}
|
|
46 |
*}
|
|
47 |
|
17145
|
48 |
code_module Div
|
|
49 |
contains
|
13405
|
50 |
test = "division 9 2"
|
|
51 |
|
22845
|
52 |
code_gen division in SML
|
20593
|
53 |
|
13405
|
54 |
end
|