author | wenzelm |
Wed, 21 Sep 1994 15:40:41 +0200 | |
changeset 145 | a9f7ff3a464c |
parent 90 | 5c7a69cef18b |
child 171 | 16c4ea954511 |
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 |
||
77
d64593bb95d3
HOL/Arith: definition of diff now uses pred, not nat_rec
lcp
parents:
67
diff
changeset
|
24 |
val diff_0_eq_0 = prove_goalw Arith.thy [diff_def, pred_def] |
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) *) |
|
77
d64593bb95d3
HOL/Arith: definition of diff now uses pred, not nat_rec
lcp
parents:
67
diff
changeset
|
30 |
val diff_Suc_Suc = prove_goalw Arith.thy [diff_def, pred_def] |
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 |
||
47 |
val add_0_right = prove_goal Arith.thy "m + 0 = m" |
|
48 |
(fn _ => [nat_ind_tac "m" 1, ALLGOALS(asm_simp_tac arith_ss)]); |
|
49 |
||
50 |
val add_Suc_right = prove_goal Arith.thy "m + Suc(n) = Suc(m+n)" |
|
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*) |
|
90
5c7a69cef18b
added parentheses made necessary by change of constrain's precedence
clasohm
parents:
85
diff
changeset
|
56 |
val add_assoc = prove_goal 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*) |
90
5c7a69cef18b
added parentheses made necessary by change of constrain's precedence
clasohm
parents:
85
diff
changeset
|
60 |
val add_commute = prove_goal Arith.thy "m + n = n + (m::nat)" |
0 | 61 |
(fn _ => [nat_ind_tac "m" 1, ALLGOALS(asm_simp_tac arith_ss)]); |
62 |
||
90
5c7a69cef18b
added parentheses made necessary by change of constrain's precedence
clasohm
parents:
85
diff
changeset
|
63 |
val add_left_commute = prove_goal 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*) |
|
74 |
val mult_0_right = prove_goal Arith.thy "m * 0 = 0" |
|
75 |
(fn _ => [nat_ind_tac "m" 1, ALLGOALS(asm_simp_tac arith_ss)]); |
|
76 |
||
77 |
(*right Sucessor law for multiplication*) |
|
54 | 78 |
val mult_Suc_right = prove_goal Arith.thy "m * Suc(n) = m + (m * n)" |
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*) |
|
90
5c7a69cef18b
added parentheses made necessary by change of constrain's precedence
clasohm
parents:
85
diff
changeset
|
85 |
val mult_commute = prove_goal 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*) |
|
90
5c7a69cef18b
added parentheses made necessary by change of constrain's precedence
clasohm
parents:
85
diff
changeset
|
89 |
val add_mult_distrib = prove_goal 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 |
||
90
5c7a69cef18b
added parentheses made necessary by change of constrain's precedence
clasohm
parents:
85
diff
changeset
|
93 |
val add_mult_distrib2 = prove_goal 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*) |
|
90
5c7a69cef18b
added parentheses made necessary by change of constrain's precedence
clasohm
parents:
85
diff
changeset
|
100 |
val mult_assoc = prove_goal 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 |
|
90
5c7a69cef18b
added parentheses made necessary by change of constrain's precedence
clasohm
parents:
85
diff
changeset
|
103 |
val mult_left_commute = prove_goal 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 |
||
111 |
val diff_self_eq_0 = prove_goal Arith.thy "m - m = 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)); |
|
119 |
val add_diff_inverse = result(); |
|
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)); |
|
128 |
val diff_less_Suc = result(); |
|
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]))); |
|
136 |
val div_termination = result(); |
|
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); |
|
142 |
val less_eq = result(); |
|
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); |
|
147 |
val mod_less = result(); |
|
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); |
|
152 |
val mod_geq = result(); |
|
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); |
|
160 |
val div_less = result(); |
|
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); |
|
165 |
val div_geq = result(); |
|
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])))); |
0 | 175 |
val mod_div_equality = result(); |
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)); |
|
184 |
val less_imp_diff_is_0 = result(); |
|
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))); |
|
189 |
val diffs0_imp_equal_lemma = result(); |
|
190 |
||
191 |
(* [| m-n = 0; n-m = 0 |] ==> m=n *) |
|
192 |
val diffs0_imp_equal = standard (diffs0_imp_equal_lemma RS mp RS mp); |
|
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)); |
|
198 |
val less_imp_diff_positive = result(); |
|
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)); |
|
204 |
val Suc_diff_n = result(); |
|
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); |
|
209 |
val if_Suc_diff_n = result(); |
|
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)); |
0 | 214 |
val zero_induct_lemma = result(); |
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)); |
|
220 |
val zero_induct = result(); |
|
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); |
|
231 |
val le_add2 = result(); |
|
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); |
236 |
val le_add1 = result(); |
|
237 |
||
238 |
val less_add_Suc1 = standard (lessI RS (le_add1 RS le_less_trans)); |
|
239 |
val less_add_Suc2 = standard (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); |
|
245 |
val plus_leD1_lemma = result(); |
|
246 |
val plus_leD1 = plus_leD1_lemma RS mp; |