author | paulson |
Thu, 09 Jan 1997 10:22:42 +0100 | |
changeset 2498 | 7914881f47c0 |
parent 2099 | c5f004bfcbab |
child 2682 | 13cdbf95ed92 |
permissions | -rw-r--r-- |
1465 | 1 |
(* Title: HOL/Arith.ML |
923 | 2 |
ID: $Id$ |
1465 | 3 |
Author: Lawrence C Paulson, Cambridge University Computer Laboratory |
923 | 4 |
Copyright 1993 University of Cambridge |
5 |
||
6 |
Proofs about elementary arithmetic: addition, multiplication, etc. |
|
7 |
Tests definitions and simplifier. |
|
8 |
*) |
|
9 |
||
10 |
open Arith; |
|
11 |
||
12 |
(*** Basic rewrite rules for the arithmetic operators ***) |
|
13 |
||
2099 | 14 |
goalw Arith.thy [pred_def] "pred 0 = 0"; |
15 |
by(Simp_tac 1); |
|
16 |
qed "pred_0"; |
|
17 |
||
18 |
goalw Arith.thy [pred_def] "pred(Suc n) = n"; |
|
19 |
by(Simp_tac 1); |
|
20 |
qed "pred_Suc"; |
|
21 |
||
923 | 22 |
val [add_0,add_Suc] = nat_recs add_def; |
1767
0c8f131eac40
Rules pred_0, pred_Suc etc. are now stored in theorem database.
berghofe
parents:
1760
diff
changeset
|
23 |
val [mult_0,mult_Suc] = nat_recs mult_def; |
0c8f131eac40
Rules pred_0, pred_Suc etc. are now stored in theorem database.
berghofe
parents:
1760
diff
changeset
|
24 |
store_thm("add_0",add_0); |
0c8f131eac40
Rules pred_0, pred_Suc etc. are now stored in theorem database.
berghofe
parents:
1760
diff
changeset
|
25 |
store_thm("add_Suc",add_Suc); |
0c8f131eac40
Rules pred_0, pred_Suc etc. are now stored in theorem database.
berghofe
parents:
1760
diff
changeset
|
26 |
store_thm("mult_0",mult_0); |
0c8f131eac40
Rules pred_0, pred_Suc etc. are now stored in theorem database.
berghofe
parents:
1760
diff
changeset
|
27 |
store_thm("mult_Suc",mult_Suc); |
1301 | 28 |
Addsimps [pred_0,pred_Suc,add_0,add_Suc,mult_0,mult_Suc]; |
29 |
||
30 |
(** pred **) |
|
31 |
||
32 |
val prems = goal Arith.thy "n ~= 0 ==> Suc(pred n) = n"; |
|
1552 | 33 |
by (res_inst_tac [("n","n")] natE 1); |
34 |
by (cut_facts_tac prems 1); |
|
35 |
by (ALLGOALS Asm_full_simp_tac); |
|
1301 | 36 |
qed "Suc_pred"; |
37 |
Addsimps [Suc_pred]; |
|
923 | 38 |
|
39 |
(** Difference **) |
|
40 |
||
1655 | 41 |
bind_thm("diff_0", diff_def RS def_nat_rec_0); |
923 | 42 |
|
43 |
qed_goalw "diff_0_eq_0" Arith.thy [diff_def, pred_def] |
|
44 |
"0 - n = 0" |
|
1264
3eb91524b938
added local simpsets; removed IOA from 'make test'
clasohm
parents:
1198
diff
changeset
|
45 |
(fn _ => [nat_ind_tac "n" 1, ALLGOALS Asm_simp_tac]); |
923 | 46 |
|
47 |
(*Must simplify BEFORE the induction!! (Else we get a critical pair) |
|
48 |
Suc(m) - Suc(n) rewrites to pred(Suc(m) - n) *) |
|
49 |
qed_goalw "diff_Suc_Suc" Arith.thy [diff_def, pred_def] |
|
50 |
"Suc(m) - Suc(n) = m - n" |
|
51 |
(fn _ => |
|
1264
3eb91524b938
added local simpsets; removed IOA from 'make test'
clasohm
parents:
1198
diff
changeset
|
52 |
[Simp_tac 1, nat_ind_tac "n" 1, ALLGOALS Asm_simp_tac]); |
923 | 53 |
|
1301 | 54 |
Addsimps [diff_0, diff_0_eq_0, diff_Suc_Suc]; |
923 | 55 |
|
56 |
||
1713 | 57 |
goal Arith.thy "!!k. 0<k ==> EX j. k = Suc(j)"; |
58 |
by (etac rev_mp 1); |
|
59 |
by (nat_ind_tac "k" 1); |
|
60 |
by (Simp_tac 1); |
|
1760
6f41a494f3b1
Replaced fast_tac by Fast_tac (which uses default claset)
berghofe
parents:
1713
diff
changeset
|
61 |
by (Fast_tac 1); |
1713 | 62 |
val lemma = result(); |
63 |
||
64 |
(* [| 0 < k; !!j. [| j: nat; k = succ(j) |] ==> Q |] ==> Q *) |
|
65 |
bind_thm ("zero_less_natE", lemma RS exE); |
|
66 |
||
67 |
||
68 |
||
923 | 69 |
(**** Inductive properties of the operators ****) |
70 |
||
71 |
(*** Addition ***) |
|
72 |
||
73 |
qed_goal "add_0_right" Arith.thy "m + 0 = m" |
|
1264
3eb91524b938
added local simpsets; removed IOA from 'make test'
clasohm
parents:
1198
diff
changeset
|
74 |
(fn _ => [nat_ind_tac "m" 1, ALLGOALS Asm_simp_tac]); |
923 | 75 |
|
76 |
qed_goal "add_Suc_right" Arith.thy "m + Suc(n) = Suc(m+n)" |
|
1264
3eb91524b938
added local simpsets; removed IOA from 'make test'
clasohm
parents:
1198
diff
changeset
|
77 |
(fn _ => [nat_ind_tac "m" 1, ALLGOALS Asm_simp_tac]); |
923 | 78 |
|
1264
3eb91524b938
added local simpsets; removed IOA from 'make test'
clasohm
parents:
1198
diff
changeset
|
79 |
Addsimps [add_0_right,add_Suc_right]; |
923 | 80 |
|
81 |
(*Associative law for addition*) |
|
82 |
qed_goal "add_assoc" Arith.thy "(m + n) + k = m + ((n + k)::nat)" |
|
1264
3eb91524b938
added local simpsets; removed IOA from 'make test'
clasohm
parents:
1198
diff
changeset
|
83 |
(fn _ => [nat_ind_tac "m" 1, ALLGOALS Asm_simp_tac]); |
923 | 84 |
|
85 |
(*Commutative law for addition*) |
|
86 |
qed_goal "add_commute" Arith.thy "m + n = n + (m::nat)" |
|
1264
3eb91524b938
added local simpsets; removed IOA from 'make test'
clasohm
parents:
1198
diff
changeset
|
87 |
(fn _ => [nat_ind_tac "m" 1, ALLGOALS Asm_simp_tac]); |
923 | 88 |
|
89 |
qed_goal "add_left_commute" Arith.thy "x+(y+z)=y+((x+z)::nat)" |
|
90 |
(fn _ => [rtac (add_commute RS trans) 1, rtac (add_assoc RS trans) 1, |
|
91 |
rtac (add_commute RS arg_cong) 1]); |
|
92 |
||
93 |
(*Addition is an AC-operator*) |
|
94 |
val add_ac = [add_assoc, add_commute, add_left_commute]; |
|
95 |
||
96 |
goal Arith.thy "!!k::nat. (k + m = k + n) = (m=n)"; |
|
97 |
by (nat_ind_tac "k" 1); |
|
1264
3eb91524b938
added local simpsets; removed IOA from 'make test'
clasohm
parents:
1198
diff
changeset
|
98 |
by (Simp_tac 1); |
3eb91524b938
added local simpsets; removed IOA from 'make test'
clasohm
parents:
1198
diff
changeset
|
99 |
by (Asm_simp_tac 1); |
923 | 100 |
qed "add_left_cancel"; |
101 |
||
102 |
goal Arith.thy "!!k::nat. (m + k = n + k) = (m=n)"; |
|
103 |
by (nat_ind_tac "k" 1); |
|
1264
3eb91524b938
added local simpsets; removed IOA from 'make test'
clasohm
parents:
1198
diff
changeset
|
104 |
by (Simp_tac 1); |
3eb91524b938
added local simpsets; removed IOA from 'make test'
clasohm
parents:
1198
diff
changeset
|
105 |
by (Asm_simp_tac 1); |
923 | 106 |
qed "add_right_cancel"; |
107 |
||
108 |
goal Arith.thy "!!k::nat. (k + m <= k + n) = (m<=n)"; |
|
109 |
by (nat_ind_tac "k" 1); |
|
1264
3eb91524b938
added local simpsets; removed IOA from 'make test'
clasohm
parents:
1198
diff
changeset
|
110 |
by (Simp_tac 1); |
3eb91524b938
added local simpsets; removed IOA from 'make test'
clasohm
parents:
1198
diff
changeset
|
111 |
by (Asm_simp_tac 1); |
923 | 112 |
qed "add_left_cancel_le"; |
113 |
||
114 |
goal Arith.thy "!!k::nat. (k + m < k + n) = (m<n)"; |
|
115 |
by (nat_ind_tac "k" 1); |
|
1264
3eb91524b938
added local simpsets; removed IOA from 'make test'
clasohm
parents:
1198
diff
changeset
|
116 |
by (Simp_tac 1); |
3eb91524b938
added local simpsets; removed IOA from 'make test'
clasohm
parents:
1198
diff
changeset
|
117 |
by (Asm_simp_tac 1); |
923 | 118 |
qed "add_left_cancel_less"; |
119 |
||
1327
6c29cfab679c
added new arithmetic lemmas and the functions take and drop.
nipkow
parents:
1301
diff
changeset
|
120 |
Addsimps [add_left_cancel, add_right_cancel, |
6c29cfab679c
added new arithmetic lemmas and the functions take and drop.
nipkow
parents:
1301
diff
changeset
|
121 |
add_left_cancel_le, add_left_cancel_less]; |
6c29cfab679c
added new arithmetic lemmas and the functions take and drop.
nipkow
parents:
1301
diff
changeset
|
122 |
|
6c29cfab679c
added new arithmetic lemmas and the functions take and drop.
nipkow
parents:
1301
diff
changeset
|
123 |
goal Arith.thy "(m+n = 0) = (m=0 & n=0)"; |
6c29cfab679c
added new arithmetic lemmas and the functions take and drop.
nipkow
parents:
1301
diff
changeset
|
124 |
by (nat_ind_tac "m" 1); |
6c29cfab679c
added new arithmetic lemmas and the functions take and drop.
nipkow
parents:
1301
diff
changeset
|
125 |
by (ALLGOALS Asm_simp_tac); |
6c29cfab679c
added new arithmetic lemmas and the functions take and drop.
nipkow
parents:
1301
diff
changeset
|
126 |
qed "add_is_0"; |
6c29cfab679c
added new arithmetic lemmas and the functions take and drop.
nipkow
parents:
1301
diff
changeset
|
127 |
Addsimps [add_is_0]; |
6c29cfab679c
added new arithmetic lemmas and the functions take and drop.
nipkow
parents:
1301
diff
changeset
|
128 |
|
6c29cfab679c
added new arithmetic lemmas and the functions take and drop.
nipkow
parents:
1301
diff
changeset
|
129 |
goal Arith.thy "!!n. n ~= 0 ==> m + pred n = pred(m+n)"; |
6c29cfab679c
added new arithmetic lemmas and the functions take and drop.
nipkow
parents:
1301
diff
changeset
|
130 |
by (nat_ind_tac "m" 1); |
6c29cfab679c
added new arithmetic lemmas and the functions take and drop.
nipkow
parents:
1301
diff
changeset
|
131 |
by (ALLGOALS Asm_simp_tac); |
6c29cfab679c
added new arithmetic lemmas and the functions take and drop.
nipkow
parents:
1301
diff
changeset
|
132 |
qed "add_pred"; |
6c29cfab679c
added new arithmetic lemmas and the functions take and drop.
nipkow
parents:
1301
diff
changeset
|
133 |
Addsimps [add_pred]; |
6c29cfab679c
added new arithmetic lemmas and the functions take and drop.
nipkow
parents:
1301
diff
changeset
|
134 |
|
923 | 135 |
(*** Multiplication ***) |
136 |
||
137 |
(*right annihilation in product*) |
|
138 |
qed_goal "mult_0_right" Arith.thy "m * 0 = 0" |
|
1264
3eb91524b938
added local simpsets; removed IOA from 'make test'
clasohm
parents:
1198
diff
changeset
|
139 |
(fn _ => [nat_ind_tac "m" 1, ALLGOALS Asm_simp_tac]); |
923 | 140 |
|
141 |
(*right Sucessor law for multiplication*) |
|
142 |
qed_goal "mult_Suc_right" Arith.thy "m * Suc(n) = m + (m * n)" |
|
143 |
(fn _ => [nat_ind_tac "m" 1, |
|
1264
3eb91524b938
added local simpsets; removed IOA from 'make test'
clasohm
parents:
1198
diff
changeset
|
144 |
ALLGOALS(asm_simp_tac (!simpset addsimps add_ac))]); |
923 | 145 |
|
1264
3eb91524b938
added local simpsets; removed IOA from 'make test'
clasohm
parents:
1198
diff
changeset
|
146 |
Addsimps [mult_0_right,mult_Suc_right]; |
923 | 147 |
|
1795 | 148 |
goal Arith.thy "1 * n = n"; |
149 |
by (Asm_simp_tac 1); |
|
150 |
qed "mult_1"; |
|
151 |
||
152 |
goal Arith.thy "n * 1 = n"; |
|
153 |
by (Asm_simp_tac 1); |
|
154 |
qed "mult_1_right"; |
|
155 |
||
923 | 156 |
(*Commutative law for multiplication*) |
157 |
qed_goal "mult_commute" Arith.thy "m * n = n * (m::nat)" |
|
1264
3eb91524b938
added local simpsets; removed IOA from 'make test'
clasohm
parents:
1198
diff
changeset
|
158 |
(fn _ => [nat_ind_tac "m" 1, ALLGOALS Asm_simp_tac]); |
923 | 159 |
|
160 |
(*addition distributes over multiplication*) |
|
161 |
qed_goal "add_mult_distrib" Arith.thy "(m + n)*k = (m*k) + ((n*k)::nat)" |
|
162 |
(fn _ => [nat_ind_tac "m" 1, |
|
1264
3eb91524b938
added local simpsets; removed IOA from 'make test'
clasohm
parents:
1198
diff
changeset
|
163 |
ALLGOALS(asm_simp_tac (!simpset addsimps add_ac))]); |
923 | 164 |
|
165 |
qed_goal "add_mult_distrib2" Arith.thy "k*(m + n) = (k*m) + ((k*n)::nat)" |
|
166 |
(fn _ => [nat_ind_tac "m" 1, |
|
1264
3eb91524b938
added local simpsets; removed IOA from 'make test'
clasohm
parents:
1198
diff
changeset
|
167 |
ALLGOALS(asm_simp_tac (!simpset addsimps add_ac))]); |
923 | 168 |
|
169 |
(*Associative law for multiplication*) |
|
170 |
qed_goal "mult_assoc" Arith.thy "(m * n) * k = m * ((n * k)::nat)" |
|
1795 | 171 |
(fn _ => [nat_ind_tac "m" 1, |
2031 | 172 |
ALLGOALS (asm_simp_tac (!simpset addsimps [add_mult_distrib]))]); |
923 | 173 |
|
174 |
qed_goal "mult_left_commute" Arith.thy "x*(y*z) = y*((x*z)::nat)" |
|
175 |
(fn _ => [rtac trans 1, rtac mult_commute 1, rtac trans 1, |
|
176 |
rtac mult_assoc 1, rtac (mult_commute RS arg_cong) 1]); |
|
177 |
||
178 |
val mult_ac = [mult_assoc,mult_commute,mult_left_commute]; |
|
179 |
||
180 |
(*** Difference ***) |
|
181 |
||
182 |
qed_goal "diff_self_eq_0" Arith.thy "m - m = 0" |
|
1264
3eb91524b938
added local simpsets; removed IOA from 'make test'
clasohm
parents:
1198
diff
changeset
|
183 |
(fn _ => [nat_ind_tac "m" 1, ALLGOALS Asm_simp_tac]); |
1496 | 184 |
Addsimps [diff_self_eq_0]; |
923 | 185 |
|
186 |
(*Addition is the inverse of subtraction: if n<=m then n+(m-n) = m. *) |
|
187 |
val [prem] = goal Arith.thy "[| ~ m<n |] ==> n+(m-n) = (m::nat)"; |
|
188 |
by (rtac (prem RS rev_mp) 1); |
|
189 |
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1); |
|
1660 | 190 |
by (ALLGOALS (Asm_simp_tac)); |
923 | 191 |
qed "add_diff_inverse"; |
192 |
||
193 |
||
194 |
(*** Remainder ***) |
|
195 |
||
196 |
goal Arith.thy "m - n < Suc(m)"; |
|
197 |
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1); |
|
198 |
by (etac less_SucE 3); |
|
1660 | 199 |
by (ALLGOALS (asm_simp_tac (!simpset addsimps [less_Suc_eq]))); |
923 | 200 |
qed "diff_less_Suc"; |
201 |
||
202 |
goal Arith.thy "!!m::nat. m - n <= m"; |
|
203 |
by (res_inst_tac [("m","m"), ("n","n")] diff_induct 1); |
|
1264
3eb91524b938
added local simpsets; removed IOA from 'make test'
clasohm
parents:
1198
diff
changeset
|
204 |
by (ALLGOALS Asm_simp_tac); |
923 | 205 |
qed "diff_le_self"; |
206 |
||
207 |
goal Arith.thy "!!n::nat. (n+m) - n = m"; |
|
208 |
by (nat_ind_tac "n" 1); |
|
1264
3eb91524b938
added local simpsets; removed IOA from 'make test'
clasohm
parents:
1198
diff
changeset
|
209 |
by (ALLGOALS Asm_simp_tac); |
923 | 210 |
qed "diff_add_inverse"; |
211 |
||
1713 | 212 |
goal Arith.thy "!!n::nat.(m+n) - n = m"; |
213 |
by (res_inst_tac [("m1","m")] (add_commute RS ssubst) 1); |
|
214 |
by (REPEAT (ares_tac [diff_add_inverse] 1)); |
|
215 |
qed "diff_add_inverse2"; |
|
216 |
||
217 |
goal Arith.thy "!!k::nat. (k+m) - (k+n) = m - n"; |
|
218 |
by (nat_ind_tac "k" 1); |
|
219 |
by (ALLGOALS Asm_simp_tac); |
|
220 |
qed "diff_cancel"; |
|
221 |
Addsimps [diff_cancel]; |
|
222 |
||
223 |
goal Arith.thy "!!m::nat. (m+k) - (n+k) = m - n"; |
|
224 |
val add_commute_k = read_instantiate [("n","k")] add_commute; |
|
225 |
by (asm_simp_tac (!simpset addsimps ([add_commute_k])) 1); |
|
226 |
qed "diff_cancel2"; |
|
227 |
Addsimps [diff_cancel2]; |
|
228 |
||
923 | 229 |
goal Arith.thy "!!n::nat. n - (n+m) = 0"; |
230 |
by (nat_ind_tac "n" 1); |
|
1264
3eb91524b938
added local simpsets; removed IOA from 'make test'
clasohm
parents:
1198
diff
changeset
|
231 |
by (ALLGOALS Asm_simp_tac); |
923 | 232 |
qed "diff_add_0"; |
1713 | 233 |
Addsimps [diff_add_0]; |
234 |
||
235 |
(** Difference distributes over multiplication **) |
|
236 |
||
237 |
goal Arith.thy "!!m::nat. (m - n) * k = (m * k) - (n * k)"; |
|
238 |
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1); |
|
239 |
by (ALLGOALS Asm_simp_tac); |
|
240 |
qed "diff_mult_distrib" ; |
|
241 |
||
242 |
goal Arith.thy "!!m::nat. k * (m - n) = (k * m) - (k * n)"; |
|
243 |
val mult_commute_k = read_instantiate [("m","k")] mult_commute; |
|
244 |
by (simp_tac (!simpset addsimps [diff_mult_distrib, mult_commute_k]) 1); |
|
245 |
qed "diff_mult_distrib2" ; |
|
246 |
(*NOT added as rewrites, since sometimes they are used from right-to-left*) |
|
247 |
||
248 |
||
249 |
(** Less-then properties **) |
|
923 | 250 |
|
251 |
(*In ordinary notation: if 0<n and n<=m then m-n < m *) |
|
252 |
goal Arith.thy "!!m. [| 0<n; ~ m<n |] ==> m - n < m"; |
|
253 |
by (subgoal_tac "0<n --> ~ m<n --> m - n < m" 1); |
|
1760
6f41a494f3b1
Replaced fast_tac by Fast_tac (which uses default claset)
berghofe
parents:
1713
diff
changeset
|
254 |
by (Fast_tac 1); |
923 | 255 |
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1); |
1264
3eb91524b938
added local simpsets; removed IOA from 'make test'
clasohm
parents:
1198
diff
changeset
|
256 |
by (ALLGOALS(asm_simp_tac(!simpset addsimps [diff_less_Suc]))); |
1398 | 257 |
qed "diff_less"; |
923 | 258 |
|
259 |
val wf_less_trans = wf_pred_nat RS wf_trancl RSN (2, def_wfrec RS trans); |
|
260 |
||
972
e61b058d58d2
changed syntax of tuples from <..., ...> to (..., ...)
clasohm
parents:
965
diff
changeset
|
261 |
goalw Nat.thy [less_def] "(m,n) : pred_nat^+ = (m<n)"; |
923 | 262 |
by (rtac refl 1); |
263 |
qed "less_eq"; |
|
264 |
||
1475 | 265 |
goal Arith.thy "(%m. m mod n) = wfrec (trancl pred_nat) \ |
266 |
\ (%f j. if j<n then j else f (j-n))"; |
|
267 |
by (simp_tac (HOL_ss addsimps [mod_def]) 1); |
|
268 |
val mod_def1 = result() RS eq_reflection; |
|
269 |
||
923 | 270 |
goal Arith.thy "!!m. m<n ==> m mod n = m"; |
1475 | 271 |
by (rtac (mod_def1 RS wf_less_trans) 1); |
1552 | 272 |
by (Asm_simp_tac 1); |
923 | 273 |
qed "mod_less"; |
274 |
||
275 |
goal Arith.thy "!!m. [| 0<n; ~m<n |] ==> m mod n = (m-n) mod n"; |
|
1475 | 276 |
by (rtac (mod_def1 RS wf_less_trans) 1); |
1552 | 277 |
by (asm_simp_tac (!simpset addsimps [diff_less, cut_apply, less_eq]) 1); |
923 | 278 |
qed "mod_geq"; |
279 |
||
280 |
||
281 |
(*** Quotient ***) |
|
282 |
||
1475 | 283 |
goal Arith.thy "(%m. m div n) = wfrec (trancl pred_nat) \ |
284 |
\ (%f j. if j<n then 0 else Suc (f (j-n)))"; |
|
285 |
by (simp_tac (HOL_ss addsimps [div_def]) 1); |
|
286 |
val div_def1 = result() RS eq_reflection; |
|
287 |
||
923 | 288 |
goal Arith.thy "!!m. m<n ==> m div n = 0"; |
1475 | 289 |
by (rtac (div_def1 RS wf_less_trans) 1); |
1552 | 290 |
by (Asm_simp_tac 1); |
923 | 291 |
qed "div_less"; |
292 |
||
293 |
goal Arith.thy "!!M. [| 0<n; ~m<n |] ==> m div n = Suc((m-n) div n)"; |
|
1475 | 294 |
by (rtac (div_def1 RS wf_less_trans) 1); |
1552 | 295 |
by (asm_simp_tac (!simpset addsimps [diff_less, cut_apply, less_eq]) 1); |
923 | 296 |
qed "div_geq"; |
297 |
||
298 |
(*Main Result about quotient and remainder.*) |
|
299 |
goal Arith.thy "!!m. 0<n ==> (m div n)*n + m mod n = m"; |
|
300 |
by (res_inst_tac [("n","m")] less_induct 1); |
|
301 |
by (rename_tac "k" 1); (*Variable name used in line below*) |
|
302 |
by (case_tac "k<n" 1); |
|
1660 | 303 |
by (ALLGOALS (asm_simp_tac(!simpset addsimps ([add_assoc] @ |
923 | 304 |
[mod_less, mod_geq, div_less, div_geq, |
1465 | 305 |
add_diff_inverse, diff_less])))); |
923 | 306 |
qed "mod_div_equality"; |
307 |
||
308 |
||
309 |
(*** More results about difference ***) |
|
310 |
||
311 |
val [prem] = goal Arith.thy "m < Suc(n) ==> m-n = 0"; |
|
312 |
by (rtac (prem RS rev_mp) 1); |
|
313 |
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1); |
|
1660 | 314 |
by (asm_simp_tac (!simpset addsimps [less_Suc_eq]) 1); |
315 |
by (ALLGOALS (Asm_simp_tac)); |
|
923 | 316 |
qed "less_imp_diff_is_0"; |
317 |
||
318 |
val prems = goal Arith.thy "m-n = 0 --> n-m = 0 --> m=n"; |
|
319 |
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1); |
|
1264
3eb91524b938
added local simpsets; removed IOA from 'make test'
clasohm
parents:
1198
diff
changeset
|
320 |
by (REPEAT(Simp_tac 1 THEN TRY(atac 1))); |
1485
240cc98b94a7
Added qed_spec_mp to avoid renaming of bound vars in 'th RS spec'
nipkow
parents:
1475
diff
changeset
|
321 |
qed_spec_mp "diffs0_imp_equal"; |
923 | 322 |
|
323 |
val [prem] = goal Arith.thy "m<n ==> 0<n-m"; |
|
324 |
by (rtac (prem RS rev_mp) 1); |
|
325 |
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1); |
|
1660 | 326 |
by (ALLGOALS (Asm_simp_tac)); |
923 | 327 |
qed "less_imp_diff_positive"; |
328 |
||
329 |
val [prem] = goal Arith.thy "n < Suc(m) ==> Suc(m)-n = Suc(m-n)"; |
|
330 |
by (rtac (prem RS rev_mp) 1); |
|
331 |
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1); |
|
1660 | 332 |
by (ALLGOALS (Asm_simp_tac)); |
923 | 333 |
qed "Suc_diff_n"; |
334 |
||
1398 | 335 |
goal Arith.thy "Suc(m)-n = (if m<n then 0 else Suc(m-n))"; |
1552 | 336 |
by (simp_tac (!simpset addsimps [less_imp_diff_is_0, not_less_eq, Suc_diff_n] |
923 | 337 |
setloop (split_tac [expand_if])) 1); |
338 |
qed "if_Suc_diff_n"; |
|
339 |
||
340 |
goal Arith.thy "P(k) --> (!n. P(Suc(n))--> P(n)) --> P(k-i)"; |
|
341 |
by (res_inst_tac [("m","k"),("n","i")] diff_induct 1); |
|
1760
6f41a494f3b1
Replaced fast_tac by Fast_tac (which uses default claset)
berghofe
parents:
1713
diff
changeset
|
342 |
by (ALLGOALS (strip_tac THEN' Simp_tac THEN' TRY o Fast_tac)); |
923 | 343 |
qed "zero_induct_lemma"; |
344 |
||
345 |
val prems = goal Arith.thy "[| P(k); !!n. P(Suc(n)) ==> P(n) |] ==> P(0)"; |
|
346 |
by (rtac (diff_self_eq_0 RS subst) 1); |
|
347 |
by (rtac (zero_induct_lemma RS mp RS mp) 1); |
|
348 |
by (REPEAT (ares_tac ([impI,allI]@prems) 1)); |
|
349 |
qed "zero_induct"; |
|
350 |
||
351 |
(*13 July 1992: loaded in 105.7s*) |
|
352 |
||
1618 | 353 |
|
354 |
(*** Further facts about mod (mainly for mutilated checkerboard ***) |
|
355 |
||
356 |
goal Arith.thy |
|
357 |
"!!m n. 0<n ==> \ |
|
358 |
\ Suc(m) mod n = (if Suc(m mod n) = n then 0 else Suc(m mod n))"; |
|
359 |
by (res_inst_tac [("n","m")] less_induct 1); |
|
360 |
by (excluded_middle_tac "Suc(na)<n" 1); |
|
361 |
(* case Suc(na) < n *) |
|
362 |
by (forward_tac [lessI RS less_trans] 2); |
|
363 |
by (asm_simp_tac (!simpset addsimps [mod_less, less_not_refl2 RS not_sym]) 2); |
|
364 |
(* case n <= Suc(na) *) |
|
365 |
by (asm_full_simp_tac (!simpset addsimps [not_less_iff_le, mod_geq]) 1); |
|
366 |
by (etac (le_imp_less_or_eq RS disjE) 1); |
|
367 |
by (asm_simp_tac (!simpset addsimps [Suc_diff_n]) 1); |
|
368 |
by (asm_full_simp_tac (!simpset addsimps [not_less_eq RS sym, |
|
369 |
diff_less, mod_geq]) 1); |
|
370 |
by (asm_simp_tac (!simpset addsimps [mod_less]) 1); |
|
371 |
qed "mod_Suc"; |
|
372 |
||
373 |
goal Arith.thy "!!m n. 0<n ==> m mod n < n"; |
|
374 |
by (res_inst_tac [("n","m")] less_induct 1); |
|
375 |
by (excluded_middle_tac "na<n" 1); |
|
376 |
(*case na<n*) |
|
377 |
by (asm_simp_tac (!simpset addsimps [mod_less]) 2); |
|
378 |
(*case n le na*) |
|
379 |
by (asm_full_simp_tac (!simpset addsimps [mod_geq, diff_less]) 1); |
|
380 |
qed "mod_less_divisor"; |
|
381 |
||
382 |
||
1626 | 383 |
(** Evens and Odds **) |
384 |
||
1909 | 385 |
(*With less_zeroE, causes case analysis on b<2*) |
386 |
AddSEs [less_SucE]; |
|
1760
6f41a494f3b1
Replaced fast_tac by Fast_tac (which uses default claset)
berghofe
parents:
1713
diff
changeset
|
387 |
|
1626 | 388 |
goal thy "!!k b. b<2 ==> k mod 2 = b | k mod 2 = (if b=1 then 0 else 1)"; |
389 |
by (subgoal_tac "k mod 2 < 2" 1); |
|
390 |
by (asm_simp_tac (!simpset addsimps [mod_less_divisor]) 2); |
|
391 |
by (asm_simp_tac (!simpset setloop split_tac [expand_if]) 1); |
|
1760
6f41a494f3b1
Replaced fast_tac by Fast_tac (which uses default claset)
berghofe
parents:
1713
diff
changeset
|
392 |
by (Fast_tac 1); |
1626 | 393 |
qed "mod2_cases"; |
394 |
||
395 |
goal thy "Suc(Suc(m)) mod 2 = m mod 2"; |
|
396 |
by (subgoal_tac "m mod 2 < 2" 1); |
|
397 |
by (asm_simp_tac (!simpset addsimps [mod_less_divisor]) 2); |
|
1909 | 398 |
by (Step_tac 1); |
1626 | 399 |
by (ALLGOALS (asm_simp_tac (!simpset addsimps [mod_Suc]))); |
400 |
qed "mod2_Suc_Suc"; |
|
401 |
Addsimps [mod2_Suc_Suc]; |
|
402 |
||
403 |
goal thy "(m+m) mod 2 = 0"; |
|
404 |
by (nat_ind_tac "m" 1); |
|
405 |
by (simp_tac (!simpset addsimps [mod_less]) 1); |
|
406 |
by (asm_simp_tac (!simpset addsimps [mod2_Suc_Suc, add_Suc_right]) 1); |
|
407 |
qed "mod2_add_self"; |
|
408 |
Addsimps [mod2_add_self]; |
|
409 |
||
1909 | 410 |
Delrules [less_SucE]; |
411 |
||
1626 | 412 |
|
923 | 413 |
(**** Additional theorems about "less than" ****) |
414 |
||
1909 | 415 |
goal Arith.thy "? k::nat. n = n+k"; |
416 |
by (res_inst_tac [("x","0")] exI 1); |
|
417 |
by (Simp_tac 1); |
|
418 |
val lemma = result(); |
|
419 |
||
923 | 420 |
goal Arith.thy "!!m. m<n --> (? k. n=Suc(m+k))"; |
421 |
by (nat_ind_tac "n" 1); |
|
1909 | 422 |
by (ALLGOALS (simp_tac (!simpset addsimps [less_Suc_eq]))); |
423 |
by (step_tac (!claset addSIs [lemma]) 1); |
|
923 | 424 |
by (res_inst_tac [("x","Suc(k)")] exI 1); |
1264
3eb91524b938
added local simpsets; removed IOA from 'make test'
clasohm
parents:
1198
diff
changeset
|
425 |
by (Simp_tac 1); |
1485
240cc98b94a7
Added qed_spec_mp to avoid renaming of bound vars in 'th RS spec'
nipkow
parents:
1475
diff
changeset
|
426 |
qed_spec_mp "less_eq_Suc_add"; |
923 | 427 |
|
428 |
goal Arith.thy "n <= ((m + n)::nat)"; |
|
429 |
by (nat_ind_tac "m" 1); |
|
1264
3eb91524b938
added local simpsets; removed IOA from 'make test'
clasohm
parents:
1198
diff
changeset
|
430 |
by (ALLGOALS Simp_tac); |
923 | 431 |
by (etac le_trans 1); |
432 |
by (rtac (lessI RS less_imp_le) 1); |
|
433 |
qed "le_add2"; |
|
434 |
||
435 |
goal Arith.thy "n <= ((n + m)::nat)"; |
|
1264
3eb91524b938
added local simpsets; removed IOA from 'make test'
clasohm
parents:
1198
diff
changeset
|
436 |
by (simp_tac (!simpset addsimps add_ac) 1); |
923 | 437 |
by (rtac le_add2 1); |
438 |
qed "le_add1"; |
|
439 |
||
440 |
bind_thm ("less_add_Suc1", (lessI RS (le_add1 RS le_less_trans))); |
|
441 |
bind_thm ("less_add_Suc2", (lessI RS (le_add2 RS le_less_trans))); |
|
442 |
||
443 |
(*"i <= j ==> i <= j+m"*) |
|
444 |
bind_thm ("trans_le_add1", le_add1 RSN (2,le_trans)); |
|
445 |
||
446 |
(*"i <= j ==> i <= m+j"*) |
|
447 |
bind_thm ("trans_le_add2", le_add2 RSN (2,le_trans)); |
|
448 |
||
449 |
(*"i < j ==> i < j+m"*) |
|
450 |
bind_thm ("trans_less_add1", le_add1 RSN (2,less_le_trans)); |
|
451 |
||
452 |
(*"i < j ==> i < m+j"*) |
|
453 |
bind_thm ("trans_less_add2", le_add2 RSN (2,less_le_trans)); |
|
454 |
||
1152 | 455 |
goal Arith.thy "!!i. i+j < (k::nat) ==> i<k"; |
1552 | 456 |
by (etac rev_mp 1); |
457 |
by (nat_ind_tac "j" 1); |
|
1264
3eb91524b938
added local simpsets; removed IOA from 'make test'
clasohm
parents:
1198
diff
changeset
|
458 |
by (ALLGOALS Asm_simp_tac); |
1760
6f41a494f3b1
Replaced fast_tac by Fast_tac (which uses default claset)
berghofe
parents:
1713
diff
changeset
|
459 |
by (fast_tac (!claset addDs [Suc_lessD]) 1); |
1152 | 460 |
qed "add_lessD1"; |
461 |
||
923 | 462 |
goal Arith.thy "!!k::nat. m <= n ==> m <= n+k"; |
1552 | 463 |
by (etac le_trans 1); |
464 |
by (rtac le_add1 1); |
|
923 | 465 |
qed "le_imp_add_le"; |
466 |
||
467 |
goal Arith.thy "!!k::nat. m < n ==> m < n+k"; |
|
1552 | 468 |
by (etac less_le_trans 1); |
469 |
by (rtac le_add1 1); |
|
923 | 470 |
qed "less_imp_add_less"; |
471 |
||
472 |
goal Arith.thy "m+k<=n --> m<=(n::nat)"; |
|
473 |
by (nat_ind_tac "k" 1); |
|
1264
3eb91524b938
added local simpsets; removed IOA from 'make test'
clasohm
parents:
1198
diff
changeset
|
474 |
by (ALLGOALS Asm_simp_tac); |
1760
6f41a494f3b1
Replaced fast_tac by Fast_tac (which uses default claset)
berghofe
parents:
1713
diff
changeset
|
475 |
by (fast_tac (!claset addDs [Suc_leD]) 1); |
1485
240cc98b94a7
Added qed_spec_mp to avoid renaming of bound vars in 'th RS spec'
nipkow
parents:
1475
diff
changeset
|
476 |
qed_spec_mp "add_leD1"; |
923 | 477 |
|
2498 | 478 |
goal Arith.thy "!!n::nat. m+k<=n ==> k<=n"; |
479 |
by (full_simp_tac (!simpset addsimps [add_commute]) 1); |
|
480 |
by (etac add_leD1 1); |
|
481 |
qed_spec_mp "add_leD2"; |
|
482 |
||
483 |
goal Arith.thy "!!n::nat. m+k<=n ==> m<=n & k<=n"; |
|
484 |
by (fast_tac (!claset addDs [add_leD1, add_leD2]) 1); |
|
485 |
bind_thm ("add_leE", result() RS conjE); |
|
486 |
||
923 | 487 |
goal Arith.thy "!!k l::nat. [| k<l; m+l = k+n |] ==> m<n"; |
1786
8a31d85d27b8
best_tac, deepen_tac and safe_tac now also use default claset.
berghofe
parents:
1767
diff
changeset
|
488 |
by (safe_tac (!claset addSDs [less_eq_Suc_add])); |
923 | 489 |
by (asm_full_simp_tac |
1264
3eb91524b938
added local simpsets; removed IOA from 'make test'
clasohm
parents:
1198
diff
changeset
|
490 |
(!simpset delsimps [add_Suc_right] |
3eb91524b938
added local simpsets; removed IOA from 'make test'
clasohm
parents:
1198
diff
changeset
|
491 |
addsimps ([add_Suc_right RS sym, add_left_cancel] @add_ac)) 1); |
1552 | 492 |
by (etac subst 1); |
1264
3eb91524b938
added local simpsets; removed IOA from 'make test'
clasohm
parents:
1198
diff
changeset
|
493 |
by (simp_tac (!simpset addsimps [less_add_Suc1]) 1); |
923 | 494 |
qed "less_add_eq_less"; |
495 |
||
496 |
||
1713 | 497 |
(*** Monotonicity of Addition ***) |
923 | 498 |
|
499 |
(*strict, in 1st argument*) |
|
500 |
goal Arith.thy "!!i j k::nat. i < j ==> i + k < j + k"; |
|
501 |
by (nat_ind_tac "k" 1); |
|
1264
3eb91524b938
added local simpsets; removed IOA from 'make test'
clasohm
parents:
1198
diff
changeset
|
502 |
by (ALLGOALS Asm_simp_tac); |
923 | 503 |
qed "add_less_mono1"; |
504 |
||
505 |
(*strict, in both arguments*) |
|
506 |
goal Arith.thy "!!i j k::nat. [|i < j; k < l|] ==> i + k < j + l"; |
|
507 |
by (rtac (add_less_mono1 RS less_trans) 1); |
|
1198 | 508 |
by (REPEAT (assume_tac 1)); |
923 | 509 |
by (nat_ind_tac "j" 1); |
1264
3eb91524b938
added local simpsets; removed IOA from 'make test'
clasohm
parents:
1198
diff
changeset
|
510 |
by (ALLGOALS Asm_simp_tac); |
923 | 511 |
qed "add_less_mono"; |
512 |
||
513 |
(*A [clumsy] way of lifting < monotonicity to <= monotonicity *) |
|
514 |
val [lt_mono,le] = goal Arith.thy |
|
1465 | 515 |
"[| !!i j::nat. i<j ==> f(i) < f(j); \ |
516 |
\ i <= j \ |
|
923 | 517 |
\ |] ==> f(i) <= (f(j)::nat)"; |
518 |
by (cut_facts_tac [le] 1); |
|
1264
3eb91524b938
added local simpsets; removed IOA from 'make test'
clasohm
parents:
1198
diff
changeset
|
519 |
by (asm_full_simp_tac (!simpset addsimps [le_eq_less_or_eq]) 1); |
1760
6f41a494f3b1
Replaced fast_tac by Fast_tac (which uses default claset)
berghofe
parents:
1713
diff
changeset
|
520 |
by (fast_tac (!claset addSIs [lt_mono]) 1); |
923 | 521 |
qed "less_mono_imp_le_mono"; |
522 |
||
523 |
(*non-strict, in 1st argument*) |
|
524 |
goal Arith.thy "!!i j k::nat. i<=j ==> i + k <= j + k"; |
|
525 |
by (res_inst_tac [("f", "%j.j+k")] less_mono_imp_le_mono 1); |
|
1552 | 526 |
by (etac add_less_mono1 1); |
923 | 527 |
by (assume_tac 1); |
528 |
qed "add_le_mono1"; |
|
529 |
||
530 |
(*non-strict, in both arguments*) |
|
531 |
goal Arith.thy "!!k l::nat. [|i<=j; k<=l |] ==> i + k <= j + l"; |
|
532 |
by (etac (add_le_mono1 RS le_trans) 1); |
|
1264
3eb91524b938
added local simpsets; removed IOA from 'make test'
clasohm
parents:
1198
diff
changeset
|
533 |
by (simp_tac (!simpset addsimps [add_commute]) 1); |
923 | 534 |
(*j moves to the end because it is free while k, l are bound*) |
1552 | 535 |
by (etac add_le_mono1 1); |
923 | 536 |
qed "add_le_mono"; |
1713 | 537 |
|
538 |
(*** Monotonicity of Multiplication ***) |
|
539 |
||
540 |
goal Arith.thy "!!i::nat. i<=j ==> i*k<=j*k"; |
|
541 |
by (nat_ind_tac "k" 1); |
|
542 |
by (ALLGOALS (asm_simp_tac (!simpset addsimps [add_le_mono]))); |
|
543 |
qed "mult_le_mono1"; |
|
544 |
||
545 |
(*<=monotonicity, BOTH arguments*) |
|
546 |
goal Arith.thy "!!i::nat. [| i<=j; k<=l |] ==> i*k<=j*l"; |
|
2007 | 547 |
by (etac (mult_le_mono1 RS le_trans) 1); |
1713 | 548 |
by (rtac le_trans 1); |
2007 | 549 |
by (stac mult_commute 2); |
550 |
by (etac mult_le_mono1 2); |
|
551 |
by (simp_tac (!simpset addsimps [mult_commute]) 1); |
|
1713 | 552 |
qed "mult_le_mono"; |
553 |
||
554 |
(*strict, in 1st argument; proof is by induction on k>0*) |
|
555 |
goal Arith.thy "!!i::nat. [| i<j; 0<k |] ==> k*i < k*j"; |
|
2031 | 556 |
by (etac zero_less_natE 1); |
1713 | 557 |
by (Asm_simp_tac 1); |
558 |
by (nat_ind_tac "x" 1); |
|
559 |
by (ALLGOALS (asm_simp_tac (!simpset addsimps [add_less_mono]))); |
|
560 |
qed "mult_less_mono2"; |
|
561 |
||
562 |
goal Arith.thy "(0 < m*n) = (0<m & 0<n)"; |
|
563 |
by (nat_ind_tac "m" 1); |
|
564 |
by (nat_ind_tac "n" 2); |
|
565 |
by (ALLGOALS Asm_simp_tac); |
|
566 |
qed "zero_less_mult_iff"; |
|
567 |
||
1795 | 568 |
goal Arith.thy "(m*n = 1) = (m=1 & n=1)"; |
569 |
by (nat_ind_tac "m" 1); |
|
570 |
by (Simp_tac 1); |
|
571 |
by (nat_ind_tac "n" 1); |
|
572 |
by (Simp_tac 1); |
|
573 |
by (fast_tac (!claset addss !simpset) 1); |
|
574 |
qed "mult_eq_1_iff"; |
|
575 |
||
1713 | 576 |
(*Cancellation law for division*) |
577 |
goal Arith.thy "!!k. [| 0<n; 0<k |] ==> (k*m) div (k*n) = m div n"; |
|
578 |
by (res_inst_tac [("n","m")] less_induct 1); |
|
579 |
by (case_tac "na<n" 1); |
|
580 |
by (asm_simp_tac (!simpset addsimps [div_less, zero_less_mult_iff, |
|
2031 | 581 |
mult_less_mono2]) 1); |
1713 | 582 |
by (subgoal_tac "~ k*na < k*n" 1); |
583 |
by (asm_simp_tac |
|
584 |
(!simpset addsimps [zero_less_mult_iff, div_geq, |
|
2031 | 585 |
diff_mult_distrib2 RS sym, diff_less]) 1); |
1713 | 586 |
by (asm_full_simp_tac (!simpset addsimps [not_less_iff_le, |
2031 | 587 |
le_refl RS mult_le_mono]) 1); |
1713 | 588 |
qed "div_cancel"; |
589 |
||
590 |
goal Arith.thy "!!k. [| 0<n; 0<k |] ==> (k*m) mod (k*n) = k * (m mod n)"; |
|
591 |
by (res_inst_tac [("n","m")] less_induct 1); |
|
592 |
by (case_tac "na<n" 1); |
|
593 |
by (asm_simp_tac (!simpset addsimps [mod_less, zero_less_mult_iff, |
|
2031 | 594 |
mult_less_mono2]) 1); |
1713 | 595 |
by (subgoal_tac "~ k*na < k*n" 1); |
596 |
by (asm_simp_tac |
|
597 |
(!simpset addsimps [zero_less_mult_iff, mod_geq, |
|
2031 | 598 |
diff_mult_distrib2 RS sym, diff_less]) 1); |
1713 | 599 |
by (asm_full_simp_tac (!simpset addsimps [not_less_iff_le, |
2031 | 600 |
le_refl RS mult_le_mono]) 1); |
1713 | 601 |
qed "mult_mod_distrib"; |
602 |
||
603 |
||
1795 | 604 |
(** Lemma for gcd **) |
605 |
||
606 |
goal Arith.thy "!!m n. m = m*n ==> n=1 | m=0"; |
|
607 |
by (dtac sym 1); |
|
608 |
by (rtac disjCI 1); |
|
609 |
by (rtac nat_less_cases 1 THEN assume_tac 2); |
|
1909 | 610 |
by (fast_tac (!claset addSEs [less_SucE] addss !simpset) 1); |
1979 | 611 |
by (best_tac (!claset addDs [mult_less_mono2] |
1795 | 612 |
addss (!simpset addsimps [zero_less_eq RS sym])) 1); |
613 |
qed "mult_eq_self_implies_10"; |
|
614 |
||
615 |