6 section\<open>The Divides Relation and Euclid's algorithm for the GCD\<close> |
6 section\<open>The Divides Relation and Euclid's algorithm for the GCD\<close> |
7 |
7 |
8 theory Primes imports ZF begin |
8 theory Primes imports ZF begin |
9 |
9 |
10 definition |
10 definition |
11 divides :: "[i,i]=>o" (infixl \<open>dvd\<close> 50) where |
11 divides :: "[i,i]\<Rightarrow>o" (infixl \<open>dvd\<close> 50) where |
12 "m dvd n \<equiv> m \<in> nat \<and> n \<in> nat \<and> (\<exists>k \<in> nat. n = m#*k)" |
12 "m dvd n \<equiv> m \<in> nat \<and> n \<in> nat \<and> (\<exists>k \<in> nat. n = m#*k)" |
13 |
13 |
14 definition |
14 definition |
15 is_gcd :: "[i,i,i]=>o" \<comment> \<open>definition of great common divisor\<close> where |
15 is_gcd :: "[i,i,i]\<Rightarrow>o" \<comment> \<open>definition of great common divisor\<close> where |
16 "is_gcd(p,m,n) \<equiv> ((p dvd m) \<and> (p dvd n)) \<and> |
16 "is_gcd(p,m,n) \<equiv> ((p dvd m) \<and> (p dvd n)) \<and> |
17 (\<forall>d\<in>nat. (d dvd m) \<and> (d dvd n) \<longrightarrow> d dvd p)" |
17 (\<forall>d\<in>nat. (d dvd m) \<and> (d dvd n) \<longrightarrow> d dvd p)" |
18 |
18 |
19 definition |
19 definition |
20 gcd :: "[i,i]=>i" \<comment> \<open>Euclid's algorithm for the gcd\<close> where |
20 gcd :: "[i,i]\<Rightarrow>i" \<comment> \<open>Euclid's algorithm for the gcd\<close> where |
21 "gcd(m,n) \<equiv> transrec(natify(n), |
21 "gcd(m,n) \<equiv> transrec(natify(n), |
22 %n f. \<lambda>m \<in> nat. |
22 \<lambda>n f. \<lambda>m \<in> nat. |
23 if n=0 then m else f`(m mod n)`n) ` natify(m)" |
23 if n=0 then m else f`(m mod n)`n) ` natify(m)" |
24 |
24 |
25 definition |
25 definition |
26 coprime :: "[i,i]=>o" \<comment> \<open>the coprime relation\<close> where |
26 coprime :: "[i,i]\<Rightarrow>o" \<comment> \<open>the coprime relation\<close> where |
27 "coprime(m,n) \<equiv> gcd(m,n) = 1" |
27 "coprime(m,n) \<equiv> gcd(m,n) = 1" |
28 |
28 |
29 definition |
29 definition |
30 prime :: i \<comment> \<open>the set of prime numbers\<close> where |
30 prime :: i \<comment> \<open>the set of prime numbers\<close> where |
31 "prime \<equiv> {p \<in> nat. 1<p \<and> (\<forall>m \<in> nat. m dvd p \<longrightarrow> m=1 | m=p)}" |
31 "prime \<equiv> {p \<in> nat. 1<p \<and> (\<forall>m \<in> nat. m dvd p \<longrightarrow> m=1 | m=p)}" |
91 by (simp add: gcd_def) |
91 by (simp add: gcd_def) |
92 |
92 |
93 lemma gcd_non_0_raw: |
93 lemma gcd_non_0_raw: |
94 "\<lbrakk>0<n; n \<in> nat\<rbrakk> \<Longrightarrow> gcd(m,n) = gcd(n, m mod n)" |
94 "\<lbrakk>0<n; n \<in> nat\<rbrakk> \<Longrightarrow> gcd(m,n) = gcd(n, m mod n)" |
95 apply (simp add: gcd_def) |
95 apply (simp add: gcd_def) |
96 apply (rule_tac P = "%z. left (z) = right" for left right in transrec [THEN ssubst]) |
96 apply (rule_tac P = "\<lambda>z. left (z) = right" for left right in transrec [THEN ssubst]) |
97 apply (simp add: ltD [THEN mem_imp_not_eq, THEN not_sym] |
97 apply (simp add: ltD [THEN mem_imp_not_eq, THEN not_sym] |
98 mod_less_divisor [THEN ltD]) |
98 mod_less_divisor [THEN ltD]) |
99 done |
99 done |
100 |
100 |
101 lemma gcd_non_0: "0 < natify(n) \<Longrightarrow> gcd(m,n) = gcd(n, m mod n)" |
101 lemma gcd_non_0: "0 < natify(n) \<Longrightarrow> gcd(m,n) = gcd(n, m mod n)" |