aboutsummaryrefslogtreecommitdiffhomepage
path: root/etc/isa/depends/Primes.ML
blob: 102419da544f5c1560a323f8ca15e19dbd8d0e6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
(*  Title:      HOL/ex/Primes.ML
    ID:         $Id$
    Author:     Christophe Tabacznyj and Lawrence C Paulson
    Copyright   1996  University of Cambridge

The "divides" relation, the greatest common divisor and Euclid's algorithm

See H. Davenport, "The Higher Arithmetic".  6th edition.  (CUP, 1992)
*)

eta_contract:=false;

(************************************************)
(** Greatest Common Divisor                    **)
(************************************************)

(*** Euclid's Algorithm ***)


val [gcd_eq] = gcd.simps;


val prems = goal thy
     "[| !!m. P m 0;     \
\        !!m n. [| 0<n;  P n (m mod n) |] ==> P m n  \
\     |] ==> P (m::nat) (n::nat)";
by (res_inst_tac [("u","m"),("v","n")] gcd.induct 1);
by (case_tac "n=0" 1);
by (asm_simp_tac (simpset() addsimps prems) 1);
by Safe_tac;
by (asm_simp_tac (simpset() addsimps prems) 1);
qed "gcd_induct";


Goal "gcd(m,0) = m";
by (Simp_tac 1);
qed "gcd_0";
Addsimps [gcd_0];

Goal "0<n ==> gcd(m,n) = gcd (n, m mod n)";
by (Asm_simp_tac 1);
qed "gcd_non_0";

Delsimps gcd.simps;

Goal "gcd(m,1) = 1";
by (simp_tac (simpset() addsimps [gcd_non_0]) 1);
qed "gcd_1";
Addsimps [gcd_1];

(*gcd(m,n) divides m and n.  The conjunctions don't seem provable separately*)
Goal "(gcd(m,n) dvd m) & (gcd(m,n) dvd n)";
by (res_inst_tac [("m","m"),("n","n")] gcd_induct 1);
by (ALLGOALS (asm_full_simp_tac (simpset() addsimps [gcd_non_0])));
by (blast_tac (claset() addDs [dvd_mod_imp_dvd]) 1);
qed "gcd_dvd_both";

bind_thm ("gcd_dvd1", gcd_dvd_both RS conjunct1);
bind_thm ("gcd_dvd2", gcd_dvd_both RS conjunct2);


(*Maximality: for all m,n,f naturals, 
                if f divides m and f divides n then f divides gcd(m,n)*)
Goal "(f dvd m) --> (f dvd n) --> f dvd gcd(m,n)";
by (res_inst_tac [("m","m"),("n","n")] gcd_induct 1);
by (ALLGOALS (asm_full_simp_tac (simpset() addsimps[gcd_non_0, dvd_mod])));
qed_spec_mp "gcd_greatest";

(*Function gcd yields the Greatest Common Divisor*)
Goalw [is_gcd_def] "is_gcd (gcd(m,n)) m n";
by (asm_simp_tac (simpset() addsimps [gcd_greatest, gcd_dvd_both]) 1);
qed "is_gcd";

(*uniqueness of GCDs*)
Goalw [is_gcd_def] "[| is_gcd m a b; is_gcd n a b |] ==> m=n";
by (blast_tac (claset() addIs [dvd_anti_sym]) 1);
qed "is_gcd_unique";

(*USED??*)
Goalw [is_gcd_def]
    "[| is_gcd m a b; k dvd a; k dvd b |] ==> k dvd m";
by (Blast_tac 1);
qed "is_gcd_dvd";

(** Commutativity **)

Goalw [is_gcd_def] "is_gcd k m n = is_gcd k n m";
by (Blast_tac 1);
qed "is_gcd_commute";

Goal "gcd(m,n) = gcd(n,m)";
by (rtac is_gcd_unique 1);
by (rtac is_gcd 2);
by (asm_simp_tac (simpset() addsimps [is_gcd, is_gcd_commute]) 1);
qed "gcd_commute";

Goal "gcd(gcd(k,m),n) = gcd(k,gcd(m,n))";
by (rtac is_gcd_unique 1);
by (rtac is_gcd 2);
by (rewtac is_gcd_def);
by (blast_tac (claset() addSIs [gcd_dvd1, gcd_dvd2]
   	                addIs  [gcd_greatest, dvd_trans]) 1);
qed "gcd_assoc";

Goal "gcd(0,m) = m";
by (stac gcd_commute 1);
by (rtac gcd_0 1);
qed "gcd_0_left";

Goal "gcd(1,m) = 1";
by (stac gcd_commute 1);
by (rtac gcd_1 1);
qed "gcd_1_left";
Addsimps [gcd_0_left, gcd_1_left];


(** Multiplication laws **)

(*Davenport, page 27*)
Goal "k * gcd(m,n) = gcd(k*m, k*n)";
by (res_inst_tac [("m","m"),("n","n")] gcd_induct 1);
by (Asm_full_simp_tac 1);
by (case_tac "k=0" 1);
 by (Asm_full_simp_tac 1);
by (asm_full_simp_tac
    (simpset() addsimps [mod_geq, gcd_non_0, mod_mult_distrib2]) 1);
qed "gcd_mult_distrib2";

Goal "gcd(m,m) = m";
by (cut_inst_tac [("k","m"),("m","1"),("n","1")] gcd_mult_distrib2 1);
by (Asm_full_simp_tac 1);
qed "gcd_self";
Addsimps [gcd_self];

Goal "gcd(k, k*n) = k";
by (cut_inst_tac [("k","k"),("m","1"),("n","n")] gcd_mult_distrib2 1);
by (Asm_full_simp_tac 1);
qed "gcd_mult";
Addsimps [gcd_mult];

Goal "[| gcd(k,n)=1; k dvd (m*n) |] ==> k dvd m";
by (subgoal_tac "m = gcd(m*k, m*n)" 1);
by (etac ssubst 1 THEN rtac gcd_greatest 1);
by (ALLGOALS (asm_simp_tac (simpset() addsimps [gcd_mult_distrib2 RS sym])));
qed "relprime_dvd_mult";

Goalw [prime_def] "[| p: prime;  ~ p dvd n |] ==> gcd (p, n) = 1";
by (cut_inst_tac [("m","p"),("n","n")] gcd_dvd_both 1);
by Auto_tac;
qed "prime_imp_relprime";

(*This theorem leads immediately to a proof of the uniqueness of factorization.
  If p divides a product of primes then it is one of those primes.*)
Goal "[| p: prime; p dvd (m*n) |] ==> p dvd m | p dvd n";
by (blast_tac (claset() addIs [relprime_dvd_mult, prime_imp_relprime]) 1);
qed "prime_dvd_mult";


(** Addition laws **)

Goal "gcd(m, m+n) = gcd(m,n)";
by (res_inst_tac [("n1", "m+n")] (gcd_commute RS ssubst) 1);
by (rtac (gcd_eq RS trans) 1);
by Auto_tac;
by (asm_simp_tac (simpset() addsimps [mod_add_self1]) 1);
by (stac gcd_commute 1);
by (stac gcd_non_0 1);
by Safe_tac;
qed "gcd_add";

Goal "gcd(m, n+m) = gcd(m,n)";
by (asm_simp_tac (simpset() addsimps [add_commute, gcd_add]) 1);
qed "gcd_add2";

Goal "gcd(m, k*m+n) = gcd(m,n)";
by (induct_tac "k" 1);
by (asm_simp_tac (simpset() addsimps [gcd_add, add_assoc]) 2); 
by (Simp_tac 1);
qed "gcd_add_mult";


(** More multiplication laws **)

Goal "gcd(m,n) dvd gcd(k*m, n)";
by (blast_tac (claset() addIs [gcd_greatest, dvd_trans, 
                               gcd_dvd1, gcd_dvd2]) 1);
qed "gcd_dvd_gcd_mult";

Goal "gcd(k,n) = 1 ==> gcd(k*m, n) = gcd(m,n)";
by (rtac dvd_anti_sym 1);
by (rtac gcd_dvd_gcd_mult 2);
by (rtac ([relprime_dvd_mult, gcd_dvd2] MRS gcd_greatest) 1);
by (stac mult_commute 2);
by (rtac gcd_dvd1 2);
by (stac gcd_commute 1);
by (asm_simp_tac (simpset() addsimps [gcd_assoc RS sym]) 1);
qed "gcd_mult_cancel";