14499
|
1 |
|
|
2 |
(*<*) theory a5 = Main: (*>*)
|
|
3 |
|
|
4 |
subsection {* The Euclidean Algorithm -- Inductively *}
|
|
5 |
|
|
6 |
subsubsection {* Rules without Base Case *}
|
|
7 |
|
|
8 |
text {* Show that the following
|
|
9 |
*}
|
|
10 |
|
|
11 |
consts evenempty :: "nat set"
|
|
12 |
inductive evenempty
|
|
13 |
intros
|
|
14 |
Add2Ie: "n \<in> evenempty \<Longrightarrow> Suc(Suc n) \<in> evenempty"
|
|
15 |
|
|
16 |
text {* defines the empty set: *}
|
|
17 |
|
|
18 |
lemma evenempty_empty: "evenempty = {}"
|
|
19 |
(*<*) oops (*>*)
|
|
20 |
|
|
21 |
|
|
22 |
subsubsection {* The Euclidean Algorithm *}
|
|
23 |
|
|
24 |
text {* Define inductively the set @{text gcd}, which characterizes
|
|
25 |
the greatest common divisor of two natural numbers: *}
|
|
26 |
|
|
27 |
(*<*)consts(*>*)
|
|
28 |
gcd :: "(nat \<times> nat \<times> nat) set"
|
|
29 |
|
|
30 |
text {* Here, @{text "(a,b,g) \<in> gcd"} means that @{text g} is the gcd
|
|
31 |
of @{text a} und @{text b}. The definition should closely follow the
|
|
32 |
Euclidean algorithm.
|
|
33 |
|
|
34 |
Reminder: The Euclidean algorithm repeatedly subtracts the smaller
|
|
35 |
from the larger number, until one of the numbers is 0. Then, the other
|
|
36 |
number is the gcd. *}
|
|
37 |
|
|
38 |
text {* Now, compute the gcd of 15 and 10: *}
|
|
39 |
lemma "(15, 10, ?g) \<in> gcd"
|
|
40 |
(*<*) oops (*>*)
|
|
41 |
|
|
42 |
text {* How does your algorithm behave on special cases as the following? *}
|
|
43 |
lemma "(0, 0, ?g) \<in> gcd"
|
|
44 |
(*<*) oops (*>*)
|
|
45 |
|
|
46 |
text {* Show that the gcd is really a divisor (for the proof, you need an appropriate lemma): *}
|
|
47 |
|
|
48 |
lemma gcd_divides: "(a,b,g) \<in> gcd \<Longrightarrow> g dvd a \<and> g dvd b"
|
|
49 |
(*<*) oops (*>*)
|
|
50 |
|
|
51 |
text {* Show that the gcd is the greatest common divisor: *}
|
|
52 |
|
|
53 |
lemma gcd_greatest [rule_format]: "(a,b,g) \<in> gcd \<Longrightarrow>
|
|
54 |
0 < a \<or> 0 < b \<longrightarrow> (\<forall> d. d dvd a \<longrightarrow> d dvd b \<longrightarrow> d \<le> g)"
|
|
55 |
(*<*) oops (*>*)
|
|
56 |
|
|
57 |
text {* Here as well, you will have to prove a suitable lemma. What is
|
|
58 |
the precondition @{text "0 < a \<or> 0 < b"} good for?
|
|
59 |
|
|
60 |
So far, we have only shown that @{text gcd} is correct, but your
|
|
61 |
algorithm might not compute a result for all values @{text
|
|
62 |
"a,b"}. Thus, show completeness of the algorithm: *}
|
|
63 |
|
|
64 |
lemma gcd_defined: "\<forall> a b. \<exists> g. (a, b, g) \<in> gcd"
|
|
65 |
(*<*) oops (*>*)
|
|
66 |
|
|
67 |
text {* The following lemma, proved by course-of-value recursion over
|
|
68 |
@{text n}, may be useful. Why does standard induction over natural
|
|
69 |
numbers not work here? *}
|
|
70 |
|
|
71 |
lemma gcd_defined_aux [rule_format]:
|
|
72 |
"\<forall> a b. (a + b) \<le> n \<longrightarrow> (\<exists> g. (a, b, g) \<in> gcd)"
|
|
73 |
apply (induct rule: nat_less_induct)
|
|
74 |
apply clarify
|
|
75 |
|
|
76 |
(*<*) oops (*>*)
|
|
77 |
|
|
78 |
text {* The idea is to show that @{text gcd} yields a result for all
|
|
79 |
@{text "a, b"} whenever it is known that @{text gcd} yields a result
|
|
80 |
for all @{text "a', b'"} whose sum is smaller than @{text "a + b"}.
|
|
81 |
|
|
82 |
In order to prove this lemma, make case distinctions corresponding to
|
|
83 |
the different clauses of the algorithm, and show how to reduce
|
|
84 |
computation of @{text gcd} for @{text "a, b"} to computation of @{text
|
|
85 |
gcd} for suitable smaller @{text "a', b'"}.
|
|
86 |
|
|
87 |
*}
|
|
88 |
|
|
89 |
|
|
90 |
(*<*) end (*>*)
|
|
91 |
|