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