author | nipkow |
Sun, 29 Jan 1995 14:02:17 +0100 | |
changeset 204 | 21c405b4039f |
parent 202 | c533bc92e882 |
child 219 | 1c9d5895d824 |
permissions | -rw-r--r-- |
0 | 1 |
(* Title: HOL/arith.ML |
2 |
ID: $Id$ |
|
3 |
Author: Lawrence C Paulson, Cambridge University Computer Laboratory |
|
4 |
Copyright 1993 University of Cambridge |
|
5 |
||
6 |
For HOL/arith.thy. |
|
7 |
||
8 |
Proofs about elementary arithmetic: addition, multiplication, etc. |
|
9 |
Tests definitions and simplifier. |
|
10 |
*) |
|
11 |
||
12 |
open Arith; |
|
13 |
||
21 | 14 |
(*** Basic rewrite rules for the arithmetic operators ***) |
0 | 15 |
|
21 | 16 |
val [pred_0, pred_Suc] = nat_recs pred_def; |
0 | 17 |
val [add_0,add_Suc] = nat_recs add_def; |
18 |
val [mult_0,mult_Suc] = nat_recs mult_def; |
|
19 |
||
20 |
(** Difference **) |
|
21 |
||
22 |
val diff_0 = diff_def RS def_nat_rec_0; |
|
23 |
||
179 | 24 |
qed_goalw "diff_0_eq_0" Arith.thy [diff_def, pred_def] |
77
d64593bb95d3
HOL/Arith: definition of diff now uses pred, not nat_rec
lcp
parents:
67
diff
changeset
|
25 |
"0 - n = 0" |
0 | 26 |
(fn _ => [nat_ind_tac "n" 1, ALLGOALS(asm_simp_tac nat_ss)]); |
27 |
||
28 |
(*Must simplify BEFORE the induction!! (Else we get a critical pair) |
|
29 |
Suc(m) - Suc(n) rewrites to pred(Suc(m) - n) *) |
|
179 | 30 |
qed_goalw "diff_Suc_Suc" Arith.thy [diff_def, pred_def] |
77
d64593bb95d3
HOL/Arith: definition of diff now uses pred, not nat_rec
lcp
parents:
67
diff
changeset
|
31 |
"Suc(m) - Suc(n) = m - n" |
0 | 32 |
(fn _ => |
33 |
[simp_tac nat_ss 1, nat_ind_tac "n" 1, ALLGOALS(asm_simp_tac nat_ss)]); |
|
34 |
||
35 |
(*** Simplification over add, mult, diff ***) |
|
36 |
||
29 | 37 |
val arith_simps = |
38 |
[pred_0, pred_Suc, add_0, add_Suc, mult_0, mult_Suc, |
|
39 |
diff_0, diff_0_eq_0, diff_Suc_Suc]; |
|
0 | 40 |
|
41 |
val arith_ss = nat_ss addsimps arith_simps; |
|
42 |
||
43 |
(**** Inductive properties of the operators ****) |
|
44 |
||
45 |
(*** Addition ***) |
|
46 |
||
179 | 47 |
qed_goal "add_0_right" Arith.thy "m + 0 = m" |
0 | 48 |
(fn _ => [nat_ind_tac "m" 1, ALLGOALS(asm_simp_tac arith_ss)]); |
49 |
||
179 | 50 |
qed_goal "add_Suc_right" Arith.thy "m + Suc(n) = Suc(m+n)" |
0 | 51 |
(fn _ => [nat_ind_tac "m" 1, ALLGOALS(asm_simp_tac arith_ss)]); |
52 |
||
53 |
val arith_ss = arith_ss addsimps [add_0_right,add_Suc_right]; |
|
54 |
||
55 |
(*Associative law for addition*) |
|
179 | 56 |
qed_goal "add_assoc" Arith.thy "(m + n) + k = m + ((n + k)::nat)" |
0 | 57 |
(fn _ => [nat_ind_tac "m" 1, ALLGOALS(asm_simp_tac arith_ss)]); |
58 |
||
54 | 59 |
(*Commutative law for addition*) |
179 | 60 |
qed_goal "add_commute" Arith.thy "m + n = n + (m::nat)" |
0 | 61 |
(fn _ => [nat_ind_tac "m" 1, ALLGOALS(asm_simp_tac arith_ss)]); |
62 |
||
179 | 63 |
qed_goal "add_left_commute" Arith.thy "x+(y+z)=y+((x+z)::nat)" |
85 | 64 |
(fn _ => [rtac (add_commute RS trans) 1, rtac (add_assoc RS trans) 1, |
65 |
rtac (add_commute RS arg_cong) 1]); |
|
54 | 66 |
|
85 | 67 |
(*Addition is an AC-operator*) |
62 | 68 |
val add_ac = [add_assoc, add_commute, add_left_commute]; |
54 | 69 |
|
70 |
||
0 | 71 |
(*** Multiplication ***) |
72 |
||
73 |
(*right annihilation in product*) |
|
179 | 74 |
qed_goal "mult_0_right" Arith.thy "m * 0 = 0" |
0 | 75 |
(fn _ => [nat_ind_tac "m" 1, ALLGOALS(asm_simp_tac arith_ss)]); |
76 |
||
77 |
(*right Sucessor law for multiplication*) |
|
179 | 78 |
qed_goal "mult_Suc_right" Arith.thy "m * Suc(n) = m + (m * n)" |
54 | 79 |
(fn _ => [nat_ind_tac "m" 1, |
80 |
ALLGOALS(asm_simp_tac (arith_ss addsimps add_ac))]); |
|
0 | 81 |
|
82 |
val arith_ss = arith_ss addsimps [mult_0_right,mult_Suc_right]; |
|
83 |
||
84 |
(*Commutative law for multiplication*) |
|
179 | 85 |
qed_goal "mult_commute" Arith.thy "m * n = n * (m::nat)" |
0 | 86 |
(fn _ => [nat_ind_tac "m" 1, ALLGOALS (asm_simp_tac arith_ss)]); |
87 |
||
88 |
(*addition distributes over multiplication*) |
|
179 | 89 |
qed_goal "add_mult_distrib" Arith.thy "(m + n)*k = (m*k) + ((n*k)::nat)" |
0 | 90 |
(fn _ => [nat_ind_tac "m" 1, |
54 | 91 |
ALLGOALS(asm_simp_tac (arith_ss addsimps add_ac))]); |
92 |
||
179 | 93 |
qed_goal "add_mult_distrib2" Arith.thy "k*(m + n) = (k*m) + ((k*n)::nat)" |
62 | 94 |
(fn _ => [nat_ind_tac "m" 1, |
95 |
ALLGOALS(asm_simp_tac (arith_ss addsimps add_ac))]); |
|
96 |
||
97 |
val arith_ss = arith_ss addsimps [add_mult_distrib,add_mult_distrib2]; |
|
0 | 98 |
|
99 |
(*Associative law for multiplication*) |
|
179 | 100 |
qed_goal "mult_assoc" Arith.thy "(m * n) * k = m * ((n * k)::nat)" |
54 | 101 |
(fn _ => [nat_ind_tac "m" 1, ALLGOALS(asm_simp_tac arith_ss)]); |
0 | 102 |
|
179 | 103 |
qed_goal "mult_left_commute" Arith.thy "x*(y*z) = y*((x*z)::nat)" |
54 | 104 |
(fn _ => [rtac trans 1, rtac mult_commute 1, rtac trans 1, |
105 |
rtac mult_assoc 1, rtac (mult_commute RS arg_cong) 1]); |
|
106 |
||
62 | 107 |
val mult_ac = [mult_assoc,mult_commute,mult_left_commute]; |
0 | 108 |
|
109 |
(*** Difference ***) |
|
110 |
||
179 | 111 |
qed_goal "diff_self_eq_0" Arith.thy "m - m = 0" |
0 | 112 |
(fn _ => [nat_ind_tac "m" 1, ALLGOALS(asm_simp_tac arith_ss)]); |
113 |
||
114 |
(*Addition is the inverse of subtraction: if n<=m then n+(m-n) = m. *) |
|
90
5c7a69cef18b
added parentheses made necessary by change of constrain's precedence
clasohm
parents:
85
diff
changeset
|
115 |
val [prem] = goal Arith.thy "[| ~ m<n |] ==> n+(m-n) = (m::nat)"; |
0 | 116 |
by (rtac (prem RS rev_mp) 1); |
117 |
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1); |
|
118 |
by (ALLGOALS(asm_simp_tac arith_ss)); |
|
171 | 119 |
qed "add_diff_inverse"; |
0 | 120 |
|
121 |
||
122 |
(*** Remainder ***) |
|
123 |
||
124 |
goal Arith.thy "m - n < Suc(m)"; |
|
125 |
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1); |
|
126 |
by (etac less_SucE 3); |
|
127 |
by (ALLGOALS(asm_simp_tac arith_ss)); |
|
171 | 128 |
qed "diff_less_Suc"; |
0 | 129 |
|
130 |
(*In ordinary notation: if 0<n and n<=m then m-n < m *) |
|
131 |
goal Arith.thy "!!m. [| 0<n; ~ m<n |] ==> m - n < m"; |
|
132 |
by (subgoal_tac "0<n --> ~ m<n --> m - n < m" 1); |
|
133 |
by (fast_tac HOL_cs 1); |
|
134 |
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1); |
|
135 |
by (ALLGOALS(asm_simp_tac(arith_ss addsimps [diff_less_Suc]))); |
|
171 | 136 |
qed "div_termination"; |
0 | 137 |
|
138 |
val wf_less_trans = wf_pred_nat RS wf_trancl RSN (2, def_wfrec RS trans); |
|
139 |
||
140 |
goalw Nat.thy [less_def] "<m,n> : pred_nat^+ = (m<n)"; |
|
141 |
by (rtac refl 1); |
|
171 | 142 |
qed "less_eq"; |
0 | 143 |
|
144 |
goal Arith.thy "!!m. m<n ==> m mod n = m"; |
|
145 |
by (rtac (mod_def RS wf_less_trans) 1); |
|
146 |
by(asm_simp_tac HOL_ss 1); |
|
171 | 147 |
qed "mod_less"; |
0 | 148 |
|
149 |
goal Arith.thy "!!m. [| 0<n; ~m<n |] ==> m mod n = (m-n) mod n"; |
|
150 |
by (rtac (mod_def RS wf_less_trans) 1); |
|
151 |
by(asm_simp_tac (nat_ss addsimps [div_termination, cut_apply, less_eq]) 1); |
|
171 | 152 |
qed "mod_geq"; |
0 | 153 |
|
154 |
||
155 |
(*** Quotient ***) |
|
156 |
||
157 |
goal Arith.thy "!!m. m<n ==> m div n = 0"; |
|
158 |
by (rtac (div_def RS wf_less_trans) 1); |
|
159 |
by(asm_simp_tac nat_ss 1); |
|
171 | 160 |
qed "div_less"; |
0 | 161 |
|
162 |
goal Arith.thy "!!M. [| 0<n; ~m<n |] ==> m div n = Suc((m-n) div n)"; |
|
163 |
by (rtac (div_def RS wf_less_trans) 1); |
|
164 |
by(asm_simp_tac (nat_ss addsimps [div_termination, cut_apply, less_eq]) 1); |
|
171 | 165 |
qed "div_geq"; |
0 | 166 |
|
167 |
(*Main Result about quotient and remainder.*) |
|
168 |
goal Arith.thy "!!m. 0<n ==> (m div n)*n + m mod n = m"; |
|
169 |
by (res_inst_tac [("n","m")] less_induct 1); |
|
170 |
by (rename_tac "k" 1); (*Variable name used in line below*) |
|
54 | 171 |
by (case_tac "k<n" 1); |
172 |
by (ALLGOALS (asm_simp_tac(arith_ss addsimps (add_ac @ |
|
0 | 173 |
[mod_less, mod_geq, div_less, div_geq, |
54 | 174 |
add_diff_inverse, div_termination])))); |
171 | 175 |
qed "mod_div_equality"; |
0 | 176 |
|
177 |
||
178 |
(*** More results about difference ***) |
|
179 |
||
180 |
val [prem] = goal Arith.thy "m < Suc(n) ==> m-n = 0"; |
|
181 |
by (rtac (prem RS rev_mp) 1); |
|
182 |
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1); |
|
183 |
by (ALLGOALS (asm_simp_tac arith_ss)); |
|
171 | 184 |
qed "less_imp_diff_is_0"; |
0 | 185 |
|
186 |
val prems = goal Arith.thy "m-n = 0 --> n-m = 0 --> m=n"; |
|
187 |
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1); |
|
188 |
by (REPEAT(simp_tac arith_ss 1 THEN TRY(atac 1))); |
|
171 | 189 |
qed "diffs0_imp_equal_lemma"; |
0 | 190 |
|
191 |
(* [| m-n = 0; n-m = 0 |] ==> m=n *) |
|
202 | 192 |
bind_thm ("diffs0_imp_equal", (diffs0_imp_equal_lemma RS mp RS mp)); |
0 | 193 |
|
194 |
val [prem] = goal Arith.thy "m<n ==> 0<n-m"; |
|
195 |
by (rtac (prem RS rev_mp) 1); |
|
196 |
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1); |
|
197 |
by (ALLGOALS(asm_simp_tac arith_ss)); |
|
171 | 198 |
qed "less_imp_diff_positive"; |
0 | 199 |
|
200 |
val [prem] = goal Arith.thy "n < Suc(m) ==> Suc(m)-n = Suc(m-n)"; |
|
201 |
by (rtac (prem RS rev_mp) 1); |
|
202 |
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1); |
|
203 |
by (ALLGOALS(asm_simp_tac arith_ss)); |
|
171 | 204 |
qed "Suc_diff_n"; |
0 | 205 |
|
206 |
goal Arith.thy "Suc(m)-n = if(m<n, 0, Suc(m-n))"; |
|
207 |
by(simp_tac (nat_ss addsimps [less_imp_diff_is_0, not_less_eq, Suc_diff_n] |
|
208 |
setloop (split_tac [expand_if])) 1); |
|
171 | 209 |
qed "if_Suc_diff_n"; |
0 | 210 |
|
211 |
goal Arith.thy "P(k) --> (!n. P(Suc(n))--> P(n)) --> P(k-i)"; |
|
212 |
by (res_inst_tac [("m","k"),("n","i")] diff_induct 1); |
|
29 | 213 |
by (ALLGOALS (strip_tac THEN' simp_tac arith_ss THEN' TRY o fast_tac HOL_cs)); |
171 | 214 |
qed "zero_induct_lemma"; |
0 | 215 |
|
216 |
val prems = goal Arith.thy "[| P(k); !!n. P(Suc(n)) ==> P(n) |] ==> P(0)"; |
|
217 |
by (rtac (diff_self_eq_0 RS subst) 1); |
|
218 |
by (rtac (zero_induct_lemma RS mp RS mp) 1); |
|
219 |
by (REPEAT (ares_tac ([impI,allI]@prems) 1)); |
|
171 | 220 |
qed "zero_induct"; |
0 | 221 |
|
222 |
(*13 July 1992: loaded in 105.7s*) |
|
223 |
||
224 |
(**** Additional theorems about "less than" ****) |
|
225 |
||
90
5c7a69cef18b
added parentheses made necessary by change of constrain's precedence
clasohm
parents:
85
diff
changeset
|
226 |
goal Arith.thy "n <= ((m + n)::nat)"; |
0 | 227 |
by (nat_ind_tac "m" 1); |
54 | 228 |
by (ALLGOALS(simp_tac arith_ss)); |
0 | 229 |
by (etac le_trans 1); |
230 |
by (rtac (lessI RS less_imp_le) 1); |
|
171 | 231 |
qed "le_add2"; |
0 | 232 |
|
90
5c7a69cef18b
added parentheses made necessary by change of constrain's precedence
clasohm
parents:
85
diff
changeset
|
233 |
goal Arith.thy "n <= ((n + m)::nat)"; |
54 | 234 |
by (simp_tac (arith_ss addsimps add_ac) 1); |
0 | 235 |
by (rtac le_add2 1); |
171 | 236 |
qed "le_add1"; |
0 | 237 |
|
202 | 238 |
bind_thm ("less_add_Suc1", (lessI RS (le_add1 RS le_less_trans))); |
239 |
bind_thm ("less_add_Suc2", (lessI RS (le_add2 RS le_less_trans))); |
|
40 | 240 |
|
90
5c7a69cef18b
added parentheses made necessary by change of constrain's precedence
clasohm
parents:
85
diff
changeset
|
241 |
goal Arith.thy "m+k<=n --> m<=(n::nat)"; |
67 | 242 |
by (nat_ind_tac "k" 1); |
243 |
by (ALLGOALS (asm_simp_tac arith_ss)); |
|
244 |
by (fast_tac (HOL_cs addDs [Suc_leD]) 1); |
|
171 | 245 |
qed "plus_leD1_lemma"; |
67 | 246 |
val plus_leD1 = plus_leD1_lemma RS mp; |