src/HOL/Arith.ML
 author nipkow Tue Jan 05 17:27:59 1999 +0100 (1999-01-05) changeset 6059 aa00e235ea27 parent 6055 fdf4638bf726 child 6073 fba734ba6894 permissions -rw-r--r--
In Main: moved Bin to the left to preserve the solver in its simpset.
 clasohm@1465 ` 1` ```(* Title: HOL/Arith.ML ``` clasohm@923 ` 2` ``` ID: \$Id\$ ``` clasohm@1465 ` 3` ``` Author: Lawrence C Paulson, Cambridge University Computer Laboratory ``` paulson@4736 ` 4` ``` Copyright 1998 University of Cambridge ``` clasohm@923 ` 5` clasohm@923 ` 6` ```Proofs about elementary arithmetic: addition, multiplication, etc. ``` paulson@3234 ` 7` ```Some from the Hoare example from Norbert Galm ``` clasohm@923 ` 8` ```*) ``` clasohm@923 ` 9` clasohm@923 ` 10` ```(*** Basic rewrite rules for the arithmetic operators ***) ``` clasohm@923 ` 11` nipkow@3896 ` 12` clasohm@923 ` 13` ```(** Difference **) ``` clasohm@923 ` 14` paulson@4732 ` 15` ```qed_goal "diff_0_eq_0" thy ``` clasohm@923 ` 16` ``` "0 - n = 0" ``` paulson@3339 ` 17` ``` (fn _ => [induct_tac "n" 1, ALLGOALS Asm_simp_tac]); ``` clasohm@923 ` 18` paulson@5429 ` 19` ```(*Must simplify BEFORE the induction! (Else we get a critical pair) ``` clasohm@923 ` 20` ``` Suc(m) - Suc(n) rewrites to pred(Suc(m) - n) *) ``` paulson@4732 ` 21` ```qed_goal "diff_Suc_Suc" thy ``` clasohm@923 ` 22` ``` "Suc(m) - Suc(n) = m - n" ``` clasohm@923 ` 23` ``` (fn _ => ``` paulson@3339 ` 24` ``` [Simp_tac 1, induct_tac "n" 1, ALLGOALS Asm_simp_tac]); ``` clasohm@923 ` 25` pusch@2682 ` 26` ```Addsimps [diff_0_eq_0, diff_Suc_Suc]; ``` clasohm@923 ` 27` nipkow@4360 ` 28` ```(* Could be (and is, below) generalized in various ways; ``` nipkow@4360 ` 29` ``` However, none of the generalizations are currently in the simpset, ``` nipkow@4360 ` 30` ``` and I dread to think what happens if I put them in *) ``` paulson@5143 ` 31` ```Goal "0 < n ==> Suc(n-1) = n"; ``` berghofe@5183 ` 32` ```by (asm_simp_tac (simpset() addsplits [nat.split]) 1); ``` nipkow@4360 ` 33` ```qed "Suc_pred"; ``` nipkow@4360 ` 34` ```Addsimps [Suc_pred]; ``` nipkow@4360 ` 35` nipkow@4360 ` 36` ```Delsimps [diff_Suc]; ``` nipkow@4360 ` 37` clasohm@923 ` 38` clasohm@923 ` 39` ```(**** Inductive properties of the operators ****) ``` clasohm@923 ` 40` clasohm@923 ` 41` ```(*** Addition ***) ``` clasohm@923 ` 42` paulson@4732 ` 43` ```qed_goal "add_0_right" thy "m + 0 = m" ``` paulson@3339 ` 44` ``` (fn _ => [induct_tac "m" 1, ALLGOALS Asm_simp_tac]); ``` clasohm@923 ` 45` paulson@4732 ` 46` ```qed_goal "add_Suc_right" thy "m + Suc(n) = Suc(m+n)" ``` paulson@3339 ` 47` ``` (fn _ => [induct_tac "m" 1, ALLGOALS Asm_simp_tac]); ``` clasohm@923 ` 48` clasohm@1264 ` 49` ```Addsimps [add_0_right,add_Suc_right]; ``` clasohm@923 ` 50` clasohm@923 ` 51` ```(*Associative law for addition*) ``` paulson@4732 ` 52` ```qed_goal "add_assoc" thy "(m + n) + k = m + ((n + k)::nat)" ``` paulson@3339 ` 53` ``` (fn _ => [induct_tac "m" 1, ALLGOALS Asm_simp_tac]); ``` clasohm@923 ` 54` clasohm@923 ` 55` ```(*Commutative law for addition*) ``` paulson@4732 ` 56` ```qed_goal "add_commute" thy "m + n = n + (m::nat)" ``` paulson@3339 ` 57` ``` (fn _ => [induct_tac "m" 1, ALLGOALS Asm_simp_tac]); ``` clasohm@923 ` 58` paulson@4732 ` 59` ```qed_goal "add_left_commute" thy "x+(y+z)=y+((x+z)::nat)" ``` clasohm@923 ` 60` ``` (fn _ => [rtac (add_commute RS trans) 1, rtac (add_assoc RS trans) 1, ``` clasohm@923 ` 61` ``` rtac (add_commute RS arg_cong) 1]); ``` clasohm@923 ` 62` clasohm@923 ` 63` ```(*Addition is an AC-operator*) ``` clasohm@923 ` 64` ```val add_ac = [add_assoc, add_commute, add_left_commute]; ``` clasohm@923 ` 65` paulson@5429 ` 66` ```Goal "(k + m = k + n) = (m=(n::nat))"; ``` paulson@3339 ` 67` ```by (induct_tac "k" 1); ``` clasohm@1264 ` 68` ```by (Simp_tac 1); ``` clasohm@1264 ` 69` ```by (Asm_simp_tac 1); ``` clasohm@923 ` 70` ```qed "add_left_cancel"; ``` clasohm@923 ` 71` paulson@5429 ` 72` ```Goal "(m + k = n + k) = (m=(n::nat))"; ``` paulson@3339 ` 73` ```by (induct_tac "k" 1); ``` clasohm@1264 ` 74` ```by (Simp_tac 1); ``` clasohm@1264 ` 75` ```by (Asm_simp_tac 1); ``` clasohm@923 ` 76` ```qed "add_right_cancel"; ``` clasohm@923 ` 77` paulson@5429 ` 78` ```Goal "(k + m <= k + n) = (m<=(n::nat))"; ``` paulson@3339 ` 79` ```by (induct_tac "k" 1); ``` clasohm@1264 ` 80` ```by (Simp_tac 1); ``` clasohm@1264 ` 81` ```by (Asm_simp_tac 1); ``` clasohm@923 ` 82` ```qed "add_left_cancel_le"; ``` clasohm@923 ` 83` paulson@5429 ` 84` ```Goal "(k + m < k + n) = (m<(n::nat))"; ``` paulson@3339 ` 85` ```by (induct_tac "k" 1); ``` clasohm@1264 ` 86` ```by (Simp_tac 1); ``` clasohm@1264 ` 87` ```by (Asm_simp_tac 1); ``` clasohm@923 ` 88` ```qed "add_left_cancel_less"; ``` clasohm@923 ` 89` nipkow@1327 ` 90` ```Addsimps [add_left_cancel, add_right_cancel, ``` nipkow@1327 ` 91` ``` add_left_cancel_le, add_left_cancel_less]; ``` nipkow@1327 ` 92` paulson@3339 ` 93` ```(** Reasoning about m+0=0, etc. **) ``` paulson@3339 ` 94` wenzelm@5069 ` 95` ```Goal "(m+n = 0) = (m=0 & n=0)"; ``` nipkow@5598 ` 96` ```by (exhaust_tac "m" 1); ``` nipkow@5598 ` 97` ```by (Auto_tac); ``` nipkow@1327 ` 98` ```qed "add_is_0"; ``` nipkow@4360 ` 99` ```AddIffs [add_is_0]; ``` nipkow@1327 ` 100` nipkow@5598 ` 101` ```Goal "(0 = m+n) = (m=0 & n=0)"; ``` nipkow@5598 ` 102` ```by (exhaust_tac "m" 1); ``` nipkow@5598 ` 103` ```by (Auto_tac); ``` nipkow@5598 ` 104` ```qed "zero_is_add"; ``` nipkow@5598 ` 105` ```AddIffs [zero_is_add]; ``` nipkow@5598 ` 106` nipkow@5598 ` 107` ```Goal "(m+n=1) = (m=1 & n=0 | m=0 & n=1)"; ``` nipkow@5598 ` 108` ```by(exhaust_tac "m" 1); ``` nipkow@5598 ` 109` ```by(Auto_tac); ``` nipkow@5598 ` 110` ```qed "add_is_1"; ``` nipkow@5598 ` 111` nipkow@5598 ` 112` ```Goal "(1=m+n) = (m=1 & n=0 | m=0 & n=1)"; ``` nipkow@5598 ` 113` ```by(exhaust_tac "m" 1); ``` nipkow@5598 ` 114` ```by(Auto_tac); ``` nipkow@5598 ` 115` ```qed "one_is_add"; ``` nipkow@5598 ` 116` wenzelm@5069 ` 117` ```Goal "(0 m+(n-(Suc k)) = (m+n)-(Suc k)" *) ``` paulson@5143 ` 130` ```Goal "0 m + (n-1) = (m+n)-1"; ``` nipkow@4360 ` 131` ```by (exhaust_tac "m" 1); ``` nipkow@4360 ` 132` ```by (ALLGOALS (asm_simp_tac (simpset() addsimps [diff_Suc] ``` berghofe@5183 ` 133` ``` addsplits [nat.split]))); ``` nipkow@1327 ` 134` ```qed "add_pred"; ``` nipkow@1327 ` 135` ```Addsimps [add_pred]; ``` nipkow@1327 ` 136` paulson@5429 ` 137` ```Goal "m + n = m ==> n = 0"; ``` paulson@5078 ` 138` ```by (dtac (add_0_right RS ssubst) 1); ``` paulson@5078 ` 139` ```by (asm_full_simp_tac (simpset() addsimps [add_assoc] ``` paulson@5078 ` 140` ``` delsimps [add_0_right]) 1); ``` paulson@5078 ` 141` ```qed "add_eq_self_zero"; ``` paulson@5078 ` 142` paulson@1626 ` 143` clasohm@923 ` 144` ```(**** Additional theorems about "less than" ****) ``` clasohm@923 ` 145` paulson@5078 ` 146` ```(*Deleted less_natE; instead use less_eq_Suc_add RS exE*) ``` paulson@5143 ` 147` ```Goal "m (? k. n=Suc(m+k))"; ``` paulson@3339 ` 148` ```by (induct_tac "n" 1); ``` paulson@5604 ` 149` ```by (ALLGOALS (simp_tac (simpset() addsimps [order_le_less]))); ``` wenzelm@4089 ` 150` ```by (blast_tac (claset() addSEs [less_SucE] ``` paulson@5497 ` 151` ``` addSIs [add_0_right RS sym, add_Suc_right RS sym]) 1); ``` nipkow@1485 ` 152` ```qed_spec_mp "less_eq_Suc_add"; ``` clasohm@923 ` 153` wenzelm@5069 ` 154` ```Goal "n <= ((m + n)::nat)"; ``` paulson@3339 ` 155` ```by (induct_tac "m" 1); ``` clasohm@1264 ` 156` ```by (ALLGOALS Simp_tac); ``` nipkow@5983 ` 157` ```by (etac le_SucI 1); ``` clasohm@923 ` 158` ```qed "le_add2"; ``` clasohm@923 ` 159` wenzelm@5069 ` 160` ```Goal "n <= ((n + m)::nat)"; ``` wenzelm@4089 ` 161` ```by (simp_tac (simpset() addsimps add_ac) 1); ``` clasohm@923 ` 162` ```by (rtac le_add2 1); ``` clasohm@923 ` 163` ```qed "le_add1"; ``` clasohm@923 ` 164` clasohm@923 ` 165` ```bind_thm ("less_add_Suc1", (lessI RS (le_add1 RS le_less_trans))); ``` clasohm@923 ` 166` ```bind_thm ("less_add_Suc2", (lessI RS (le_add2 RS le_less_trans))); ``` clasohm@923 ` 167` paulson@5429 ` 168` ```Goal "(m i <= j+m"*) ``` clasohm@923 ` 174` ```bind_thm ("trans_le_add1", le_add1 RSN (2,le_trans)); ``` clasohm@923 ` 175` clasohm@923 ` 176` ```(*"i <= j ==> i <= m+j"*) ``` clasohm@923 ` 177` ```bind_thm ("trans_le_add2", le_add2 RSN (2,le_trans)); ``` clasohm@923 ` 178` clasohm@923 ` 179` ```(*"i < j ==> i < j+m"*) ``` clasohm@923 ` 180` ```bind_thm ("trans_less_add1", le_add1 RSN (2,less_le_trans)); ``` clasohm@923 ` 181` clasohm@923 ` 182` ```(*"i < j ==> i < m+j"*) ``` clasohm@923 ` 183` ```bind_thm ("trans_less_add2", le_add2 RSN (2,less_le_trans)); ``` clasohm@923 ` 184` nipkow@5654 ` 185` ```Goal "i+j < (k::nat) --> i m<=(n::nat)"; ``` paulson@3339 ` 202` ```by (induct_tac "k" 1); ``` paulson@5497 ` 203` ```by (ALLGOALS (asm_simp_tac (simpset() addsimps le_simps))); ``` nipkow@1485 ` 204` ```qed_spec_mp "add_leD1"; ``` clasohm@923 ` 205` paulson@5429 ` 206` ```Goal "m+k<=n ==> k<=(n::nat)"; ``` wenzelm@4089 ` 207` ```by (full_simp_tac (simpset() addsimps [add_commute]) 1); ``` paulson@2498 ` 208` ```by (etac add_leD1 1); ``` paulson@2498 ` 209` ```qed_spec_mp "add_leD2"; ``` paulson@2498 ` 210` paulson@5429 ` 211` ```Goal "m+k<=n ==> m<=n & k<=(n::nat)"; ``` wenzelm@4089 ` 212` ```by (blast_tac (claset() addDs [add_leD1, add_leD2]) 1); ``` paulson@2498 ` 213` ```bind_thm ("add_leE", result() RS conjE); ``` paulson@2498 ` 214` paulson@5429 ` 215` ```(*needs !!k for add_ac to work*) ``` paulson@5429 ` 216` ```Goal "!!k:: nat. [| k m i + k < j + (k::nat)"; ``` paulson@3339 ` 228` ```by (induct_tac "k" 1); ``` clasohm@1264 ` 229` ```by (ALLGOALS Asm_simp_tac); ``` clasohm@923 ` 230` ```qed "add_less_mono1"; ``` clasohm@923 ` 231` clasohm@923 ` 232` ```(*strict, in both arguments*) ``` paulson@5429 ` 233` ```Goal "[|i < j; k < l|] ==> i + k < j + (l::nat)"; ``` clasohm@923 ` 234` ```by (rtac (add_less_mono1 RS less_trans) 1); ``` lcp@1198 ` 235` ```by (REPEAT (assume_tac 1)); ``` paulson@3339 ` 236` ```by (induct_tac "j" 1); ``` clasohm@1264 ` 237` ```by (ALLGOALS Asm_simp_tac); ``` clasohm@923 ` 238` ```qed "add_less_mono"; ``` clasohm@923 ` 239` clasohm@923 ` 240` ```(*A [clumsy] way of lifting < monotonicity to <= monotonicity *) ``` paulson@5316 ` 241` ```val [lt_mono,le] = Goal ``` clasohm@1465 ` 242` ``` "[| !!i j::nat. i f(i) < f(j); \ ``` clasohm@1465 ` 243` ```\ i <= j \ ``` clasohm@923 ` 244` ```\ |] ==> f(i) <= (f(j)::nat)"; ``` clasohm@923 ` 245` ```by (cut_facts_tac [le] 1); ``` paulson@5604 ` 246` ```by (asm_full_simp_tac (simpset() addsimps [order_le_less]) 1); ``` wenzelm@4089 ` 247` ```by (blast_tac (claset() addSIs [lt_mono]) 1); ``` clasohm@923 ` 248` ```qed "less_mono_imp_le_mono"; ``` clasohm@923 ` 249` clasohm@923 ` 250` ```(*non-strict, in 1st argument*) ``` paulson@5429 ` 251` ```Goal "i<=j ==> i + k <= j + (k::nat)"; ``` wenzelm@3842 ` 252` ```by (res_inst_tac [("f", "%j. j+k")] less_mono_imp_le_mono 1); ``` paulson@1552 ` 253` ```by (etac add_less_mono1 1); ``` clasohm@923 ` 254` ```by (assume_tac 1); ``` clasohm@923 ` 255` ```qed "add_le_mono1"; ``` clasohm@923 ` 256` clasohm@923 ` 257` ```(*non-strict, in both arguments*) ``` paulson@5429 ` 258` ```Goal "[|i<=j; k<=l |] ==> i + k <= j + (l::nat)"; ``` clasohm@923 ` 259` ```by (etac (add_le_mono1 RS le_trans) 1); ``` wenzelm@4089 ` 260` ```by (simp_tac (simpset() addsimps [add_commute]) 1); ``` clasohm@923 ` 261` ```qed "add_le_mono"; ``` paulson@1713 ` 262` paulson@3234 ` 263` paulson@3234 ` 264` ```(*** Multiplication ***) ``` paulson@3234 ` 265` paulson@3234 ` 266` ```(*right annihilation in product*) ``` paulson@4732 ` 267` ```qed_goal "mult_0_right" thy "m * 0 = 0" ``` paulson@3339 ` 268` ``` (fn _ => [induct_tac "m" 1, ALLGOALS Asm_simp_tac]); ``` paulson@3234 ` 269` paulson@3293 ` 270` ```(*right successor law for multiplication*) ``` paulson@4732 ` 271` ```qed_goal "mult_Suc_right" thy "m * Suc(n) = m + (m * n)" ``` paulson@3339 ` 272` ``` (fn _ => [induct_tac "m" 1, ``` wenzelm@4089 ` 273` ``` ALLGOALS(asm_simp_tac (simpset() addsimps add_ac))]); ``` paulson@3234 ` 274` paulson@3293 ` 275` ```Addsimps [mult_0_right, mult_Suc_right]; ``` paulson@3234 ` 276` wenzelm@5069 ` 277` ```Goal "1 * n = n"; ``` paulson@3234 ` 278` ```by (Asm_simp_tac 1); ``` paulson@3234 ` 279` ```qed "mult_1"; ``` paulson@3234 ` 280` wenzelm@5069 ` 281` ```Goal "n * 1 = n"; ``` paulson@3234 ` 282` ```by (Asm_simp_tac 1); ``` paulson@3234 ` 283` ```qed "mult_1_right"; ``` paulson@3234 ` 284` paulson@3234 ` 285` ```(*Commutative law for multiplication*) ``` paulson@4732 ` 286` ```qed_goal "mult_commute" thy "m * n = n * (m::nat)" ``` paulson@3339 ` 287` ``` (fn _ => [induct_tac "m" 1, ALLGOALS Asm_simp_tac]); ``` paulson@3234 ` 288` paulson@3234 ` 289` ```(*addition distributes over multiplication*) ``` paulson@4732 ` 290` ```qed_goal "add_mult_distrib" thy "(m + n)*k = (m*k) + ((n*k)::nat)" ``` paulson@3339 ` 291` ``` (fn _ => [induct_tac "m" 1, ``` wenzelm@4089 ` 292` ``` ALLGOALS(asm_simp_tac (simpset() addsimps add_ac))]); ``` paulson@3234 ` 293` paulson@4732 ` 294` ```qed_goal "add_mult_distrib2" thy "k*(m + n) = (k*m) + ((k*n)::nat)" ``` paulson@3339 ` 295` ``` (fn _ => [induct_tac "m" 1, ``` wenzelm@4089 ` 296` ``` ALLGOALS(asm_simp_tac (simpset() addsimps add_ac))]); ``` paulson@3234 ` 297` paulson@3234 ` 298` ```(*Associative law for multiplication*) ``` paulson@4732 ` 299` ```qed_goal "mult_assoc" thy "(m * n) * k = m * ((n * k)::nat)" ``` paulson@3339 ` 300` ``` (fn _ => [induct_tac "m" 1, ``` wenzelm@4089 ` 301` ``` ALLGOALS (asm_simp_tac (simpset() addsimps [add_mult_distrib]))]); ``` paulson@3234 ` 302` paulson@4732 ` 303` ```qed_goal "mult_left_commute" thy "x*(y*z) = y*((x*z)::nat)" ``` paulson@3234 ` 304` ``` (fn _ => [rtac trans 1, rtac mult_commute 1, rtac trans 1, ``` paulson@3234 ` 305` ``` rtac mult_assoc 1, rtac (mult_commute RS arg_cong) 1]); ``` paulson@3234 ` 306` paulson@3234 ` 307` ```val mult_ac = [mult_assoc,mult_commute,mult_left_commute]; ``` paulson@3234 ` 308` wenzelm@5069 ` 309` ```Goal "(m*n = 0) = (m=0 | n=0)"; ``` paulson@3339 ` 310` ```by (induct_tac "m" 1); ``` paulson@3339 ` 311` ```by (induct_tac "n" 2); ``` paulson@3293 ` 312` ```by (ALLGOALS Asm_simp_tac); ``` paulson@3293 ` 313` ```qed "mult_is_0"; ``` paulson@3293 ` 314` ```Addsimps [mult_is_0]; ``` paulson@3293 ` 315` paulson@5429 ` 316` ```Goal "m <= m*(m::nat)"; ``` paulson@4158 ` 317` ```by (induct_tac "m" 1); ``` paulson@4158 ` 318` ```by (ALLGOALS (asm_simp_tac (simpset() addsimps [add_assoc RS sym]))); ``` paulson@4158 ` 319` ```by (etac (le_add2 RSN (2,le_trans)) 1); ``` paulson@4158 ` 320` ```qed "le_square"; ``` paulson@4158 ` 321` paulson@3234 ` 322` paulson@3234 ` 323` ```(*** Difference ***) ``` paulson@3234 ` 324` paulson@3234 ` 325` paulson@4732 ` 326` ```qed_goal "diff_self_eq_0" thy "m - m = 0" ``` paulson@3339 ` 327` ``` (fn _ => [induct_tac "m" 1, ALLGOALS Asm_simp_tac]); ``` paulson@3234 ` 328` ```Addsimps [diff_self_eq_0]; ``` paulson@3234 ` 329` paulson@3234 ` 330` ```(*Addition is the inverse of subtraction: if n<=m then n+(m-n) = m. *) ``` wenzelm@5069 ` 331` ```Goal "~ m n+(m-n) = (m::nat)"; ``` paulson@3234 ` 332` ```by (res_inst_tac [("m","m"),("n","n")] diff_induct 1); ``` paulson@3352 ` 333` ```by (ALLGOALS Asm_simp_tac); ``` paulson@3381 ` 334` ```qed_spec_mp "add_diff_inverse"; ``` paulson@3381 ` 335` paulson@5143 ` 336` ```Goal "n<=m ==> n+(m-n) = (m::nat)"; ``` wenzelm@4089 ` 337` ```by (asm_simp_tac (simpset() addsimps [add_diff_inverse, not_less_iff_le]) 1); ``` paulson@3381 ` 338` ```qed "le_add_diff_inverse"; ``` paulson@3234 ` 339` paulson@5143 ` 340` ```Goal "n<=m ==> (m-n)+n = (m::nat)"; ``` wenzelm@4089 ` 341` ```by (asm_simp_tac (simpset() addsimps [le_add_diff_inverse, add_commute]) 1); ``` paulson@3381 ` 342` ```qed "le_add_diff_inverse2"; ``` paulson@3381 ` 343` paulson@3381 ` 344` ```Addsimps [le_add_diff_inverse, le_add_diff_inverse2]; ``` paulson@3234 ` 345` paulson@3234 ` 346` paulson@3234 ` 347` ```(*** More results about difference ***) ``` paulson@3234 ` 348` paulson@5414 ` 349` ```Goal "n <= m ==> Suc(m)-n = Suc(m-n)"; ``` paulson@5316 ` 350` ```by (etac rev_mp 1); ``` paulson@3352 ` 351` ```by (res_inst_tac [("m","m"),("n","n")] diff_induct 1); ``` paulson@3352 ` 352` ```by (ALLGOALS Asm_simp_tac); ``` paulson@5414 ` 353` ```qed "Suc_diff_le"; ``` paulson@3352 ` 354` paulson@5429 ` 355` ```Goal "n<=(l::nat) --> Suc l - n + m = Suc (l - n + m)"; ``` paulson@5429 ` 356` ```by (res_inst_tac [("m","n"),("n","l")] diff_induct 1); ``` paulson@5429 ` 357` ```by (ALLGOALS Asm_simp_tac); ``` paulson@5429 ` 358` ```qed_spec_mp "Suc_diff_add_le"; ``` paulson@5429 ` 359` wenzelm@5069 ` 360` ```Goal "m - n < Suc(m)"; ``` paulson@3234 ` 361` ```by (res_inst_tac [("m","m"),("n","n")] diff_induct 1); ``` paulson@3234 ` 362` ```by (etac less_SucE 3); ``` wenzelm@4089 ` 363` ```by (ALLGOALS (asm_simp_tac (simpset() addsimps [less_Suc_eq]))); ``` paulson@3234 ` 364` ```qed "diff_less_Suc"; ``` paulson@3234 ` 365` paulson@5429 ` 366` ```Goal "m - n <= (m::nat)"; ``` paulson@3234 ` 367` ```by (res_inst_tac [("m","m"), ("n","n")] diff_induct 1); ``` paulson@3234 ` 368` ```by (ALLGOALS Asm_simp_tac); ``` paulson@3234 ` 369` ```qed "diff_le_self"; ``` paulson@3903 ` 370` ```Addsimps [diff_le_self]; ``` paulson@3234 ` 371` paulson@4732 ` 372` ```(* j j-n < k *) ``` paulson@4732 ` 373` ```bind_thm ("less_imp_diff_less", diff_le_self RS le_less_trans); ``` paulson@4732 ` 374` wenzelm@5069 ` 375` ```Goal "!!i::nat. i-j-k = i - (j+k)"; ``` paulson@3352 ` 376` ```by (res_inst_tac [("m","i"),("n","j")] diff_induct 1); ``` paulson@3352 ` 377` ```by (ALLGOALS Asm_simp_tac); ``` paulson@3352 ` 378` ```qed "diff_diff_left"; ``` paulson@3352 ` 379` wenzelm@5069 ` 380` ```Goal "(Suc m - n) - Suc k = m - n - k"; ``` wenzelm@4423 ` 381` ```by (simp_tac (simpset() addsimps [diff_diff_left]) 1); ``` paulson@4736 ` 382` ```qed "Suc_diff_diff"; ``` paulson@4736 ` 383` ```Addsimps [Suc_diff_diff]; ``` nipkow@4360 ` 384` paulson@5143 ` 385` ```Goal "0 n - Suc i < n"; ``` berghofe@5183 ` 386` ```by (exhaust_tac "n" 1); ``` paulson@4732 ` 387` ```by Safe_tac; ``` paulson@5497 ` 388` ```by (asm_simp_tac (simpset() addsimps le_simps) 1); ``` paulson@4732 ` 389` ```qed "diff_Suc_less"; ``` paulson@4732 ` 390` ```Addsimps [diff_Suc_less]; ``` paulson@4732 ` 391` paulson@5329 ` 392` ```Goal "i n - Suc i < n - i"; ``` paulson@5329 ` 393` ```by (exhaust_tac "n" 1); ``` paulson@5497 ` 394` ```by (auto_tac (claset(), ``` paulson@5537 ` 395` ``` simpset() addsimps [Suc_diff_le]@le_simps)); ``` paulson@5329 ` 396` ```qed "diff_Suc_less_diff"; ``` paulson@5329 ` 397` wenzelm@3396 ` 398` ```(*This and the next few suggested by Florian Kammueller*) ``` wenzelm@5069 ` 399` ```Goal "!!i::nat. i-j-k = i-k-j"; ``` wenzelm@4089 ` 400` ```by (simp_tac (simpset() addsimps [diff_diff_left, add_commute]) 1); ``` paulson@3352 ` 401` ```qed "diff_commute"; ``` paulson@3352 ` 402` paulson@5429 ` 403` ```Goal "k<=j --> j<=i --> i - (j - k) = i - j + (k::nat)"; ``` paulson@3352 ` 404` ```by (res_inst_tac [("m","i"),("n","j")] diff_induct 1); ``` paulson@3352 ` 405` ```by (ALLGOALS Asm_simp_tac); ``` paulson@5414 ` 406` ```by (asm_simp_tac (simpset() addsimps [Suc_diff_le, le_Suc_eq]) 1); ``` paulson@3352 ` 407` ```qed_spec_mp "diff_diff_right"; ``` paulson@3352 ` 408` paulson@5429 ` 409` ```Goal "k <= (j::nat) --> (i + j) - k = i + (j - k)"; ``` paulson@3352 ` 410` ```by (res_inst_tac [("m","j"),("n","k")] diff_induct 1); ``` paulson@3352 ` 411` ```by (ALLGOALS Asm_simp_tac); ``` paulson@3352 ` 412` ```qed_spec_mp "diff_add_assoc"; ``` paulson@3352 ` 413` paulson@5429 ` 414` ```Goal "k <= (j::nat) --> (j + i) - k = i + (j - k)"; ``` paulson@4732 ` 415` ```by (asm_simp_tac (simpset() addsimps [add_commute, diff_add_assoc]) 1); ``` paulson@4732 ` 416` ```qed_spec_mp "diff_add_assoc2"; ``` paulson@4732 ` 417` paulson@5429 ` 418` ```Goal "(n+m) - n = (m::nat)"; ``` paulson@3339 ` 419` ```by (induct_tac "n" 1); ``` paulson@3234 ` 420` ```by (ALLGOALS Asm_simp_tac); ``` paulson@3234 ` 421` ```qed "diff_add_inverse"; ``` paulson@3234 ` 422` ```Addsimps [diff_add_inverse]; ``` paulson@3234 ` 423` paulson@5429 ` 424` ```Goal "(m+n) - n = (m::nat)"; ``` wenzelm@4089 ` 425` ```by (simp_tac (simpset() addsimps [diff_add_assoc]) 1); ``` paulson@3234 ` 426` ```qed "diff_add_inverse2"; ``` paulson@3234 ` 427` ```Addsimps [diff_add_inverse2]; ``` paulson@3234 ` 428` paulson@5429 ` 429` ```Goal "i <= (j::nat) ==> (j-i=k) = (j=k+i)"; ``` paulson@3724 ` 430` ```by Safe_tac; ``` paulson@3381 ` 431` ```by (ALLGOALS Asm_simp_tac); ``` paulson@3366 ` 432` ```qed "le_imp_diff_is_add"; ``` paulson@3366 ` 433` paulson@5356 ` 434` ```Goal "(m-n = 0) = (m <= n)"; ``` paulson@3234 ` 435` ```by (res_inst_tac [("m","m"),("n","n")] diff_induct 1); ``` paulson@5497 ` 436` ```by (ALLGOALS Asm_simp_tac); ``` paulson@5356 ` 437` ```qed "diff_is_0_eq"; ``` paulson@5356 ` 438` ```Addsimps [diff_is_0_eq RS iffD2]; ``` paulson@3234 ` 439` paulson@5333 ` 440` ```Goal "(0 ? k. 0 (!n. P(Suc(n))--> P(n)) --> P(k-i)"; ``` paulson@3234 ` 460` ```by (res_inst_tac [("m","k"),("n","i")] diff_induct 1); ``` paulson@3718 ` 461` ```by (ALLGOALS (Clarify_tac THEN' Simp_tac THEN' TRY o Blast_tac)); ``` paulson@3234 ` 462` ```qed "zero_induct_lemma"; ``` paulson@3234 ` 463` paulson@5316 ` 464` ```val prems = Goal "[| P(k); !!n. P(Suc(n)) ==> P(n) |] ==> P(0)"; ``` paulson@3234 ` 465` ```by (rtac (diff_self_eq_0 RS subst) 1); ``` paulson@3234 ` 466` ```by (rtac (zero_induct_lemma RS mp RS mp) 1); ``` paulson@3234 ` 467` ```by (REPEAT (ares_tac ([impI,allI]@prems) 1)); ``` paulson@3234 ` 468` ```qed "zero_induct"; ``` paulson@3234 ` 469` paulson@5429 ` 470` ```Goal "(k+m) - (k+n) = m - (n::nat)"; ``` paulson@3339 ` 471` ```by (induct_tac "k" 1); ``` paulson@3234 ` 472` ```by (ALLGOALS Asm_simp_tac); ``` paulson@3234 ` 473` ```qed "diff_cancel"; ``` paulson@3234 ` 474` ```Addsimps [diff_cancel]; ``` paulson@3234 ` 475` paulson@5429 ` 476` ```Goal "(m+k) - (n+k) = m - (n::nat)"; ``` paulson@3234 ` 477` ```val add_commute_k = read_instantiate [("n","k")] add_commute; ``` paulson@5537 ` 478` ```by (asm_simp_tac (simpset() addsimps [add_commute_k]) 1); ``` paulson@3234 ` 479` ```qed "diff_cancel2"; ``` paulson@3234 ` 480` ```Addsimps [diff_cancel2]; ``` paulson@3234 ` 481` paulson@5414 ` 482` ```(*From Clemens Ballarin, proof by lcp*) ``` paulson@5429 ` 483` ```Goal "[| k<=n; n<=m |] ==> (m-k) - (n-k) = m-(n::nat)"; ``` paulson@5414 ` 484` ```by (REPEAT (etac rev_mp 1)); ``` paulson@5414 ` 485` ```by (res_inst_tac [("m","m"),("n","n")] diff_induct 1); ``` paulson@5414 ` 486` ```by (ALLGOALS Asm_simp_tac); ``` paulson@5414 ` 487` ```(*a confluence problem*) ``` paulson@5414 ` 488` ```by (asm_simp_tac (simpset() addsimps [Suc_diff_le, le_Suc_eq]) 1); ``` paulson@3234 ` 489` ```qed "diff_right_cancel"; ``` paulson@3234 ` 490` paulson@5429 ` 491` ```Goal "n - (n+m) = 0"; ``` paulson@3339 ` 492` ```by (induct_tac "n" 1); ``` paulson@3234 ` 493` ```by (ALLGOALS Asm_simp_tac); ``` paulson@3234 ` 494` ```qed "diff_add_0"; ``` paulson@3234 ` 495` ```Addsimps [diff_add_0]; ``` paulson@3234 ` 496` paulson@5409 ` 497` paulson@3234 ` 498` ```(** Difference distributes over multiplication **) ``` paulson@3234 ` 499` wenzelm@5069 ` 500` ```Goal "!!m::nat. (m - n) * k = (m * k) - (n * k)"; ``` paulson@3234 ` 501` ```by (res_inst_tac [("m","m"),("n","n")] diff_induct 1); ``` paulson@3234 ` 502` ```by (ALLGOALS Asm_simp_tac); ``` paulson@3234 ` 503` ```qed "diff_mult_distrib" ; ``` paulson@3234 ` 504` wenzelm@5069 ` 505` ```Goal "!!m::nat. k * (m - n) = (k * m) - (k * n)"; ``` paulson@3234 ` 506` ```val mult_commute_k = read_instantiate [("m","k")] mult_commute; ``` wenzelm@4089 ` 507` ```by (simp_tac (simpset() addsimps [diff_mult_distrib, mult_commute_k]) 1); ``` paulson@3234 ` 508` ```qed "diff_mult_distrib2" ; ``` paulson@3234 ` 509` ```(*NOT added as rewrites, since sometimes they are used from right-to-left*) ``` paulson@3234 ` 510` paulson@3234 ` 511` paulson@1713 ` 512` ```(*** Monotonicity of Multiplication ***) ``` paulson@1713 ` 513` paulson@5429 ` 514` ```Goal "i <= (j::nat) ==> i*k<=j*k"; ``` paulson@3339 ` 515` ```by (induct_tac "k" 1); ``` wenzelm@4089 ` 516` ```by (ALLGOALS (asm_simp_tac (simpset() addsimps [add_le_mono]))); ``` paulson@1713 ` 517` ```qed "mult_le_mono1"; ``` paulson@1713 ` 518` paulson@1713 ` 519` ```(*<=monotonicity, BOTH arguments*) ``` paulson@5429 ` 520` ```Goal "[| i <= (j::nat); k <= l |] ==> i*k <= j*l"; ``` paulson@2007 ` 521` ```by (etac (mult_le_mono1 RS le_trans) 1); ``` paulson@1713 ` 522` ```by (rtac le_trans 1); ``` paulson@2007 ` 523` ```by (stac mult_commute 2); ``` paulson@2007 ` 524` ```by (etac mult_le_mono1 2); ``` wenzelm@4089 ` 525` ```by (simp_tac (simpset() addsimps [mult_commute]) 1); ``` paulson@1713 ` 526` ```qed "mult_le_mono"; ``` paulson@1713 ` 527` paulson@1713 ` 528` ```(*strict, in 1st argument; proof is by induction on k>0*) ``` paulson@5429 ` 529` ```Goal "[| i k*i < k*j"; ``` paulson@5078 ` 530` ```by (eres_inst_tac [("m1","0")] (less_eq_Suc_add RS exE) 1); ``` paulson@1713 ` 531` ```by (Asm_simp_tac 1); ``` paulson@3339 ` 532` ```by (induct_tac "x" 1); ``` wenzelm@4089 ` 533` ```by (ALLGOALS (asm_simp_tac (simpset() addsimps [add_less_mono]))); ``` paulson@1713 ` 534` ```qed "mult_less_mono2"; ``` paulson@1713 ` 535` paulson@5429 ` 536` ```Goal "[| i i*k < j*k"; ``` paulson@3457 ` 537` ```by (dtac mult_less_mono2 1); ``` wenzelm@4089 ` 538` ```by (ALLGOALS (asm_full_simp_tac (simpset() addsimps [mult_commute]))); ``` paulson@3234 ` 539` ```qed "mult_less_mono1"; ``` paulson@3234 ` 540` wenzelm@5069 ` 541` ```Goal "(0 < m*n) = (0 (m*k < n*k) = (m (k*m < k*n) = (m (m*k = n*k) = (m=n)"; ``` paulson@3234 ` 580` ```by (cut_facts_tac [less_linear] 1); ``` paulson@3724 ` 581` ```by Safe_tac; ``` paulson@3457 ` 582` ```by (assume_tac 2); ``` paulson@3234 ` 583` ```by (ALLGOALS (dtac mult_less_mono1 THEN' assume_tac)); ``` paulson@3234 ` 584` ```by (ALLGOALS Asm_full_simp_tac); ``` paulson@3234 ` 585` ```qed "mult_cancel2"; ``` paulson@3234 ` 586` paulson@5143 ` 587` ```Goal "0 (k*m = k*n) = (m=n)"; ``` paulson@3457 ` 588` ```by (dtac mult_cancel2 1); ``` wenzelm@4089 ` 589` ```by (asm_full_simp_tac (simpset() addsimps [mult_commute]) 1); ``` paulson@3234 ` 590` ```qed "mult_cancel1"; ``` paulson@3234 ` 591` ```Addsimps [mult_cancel1, mult_cancel2]; ``` paulson@3234 ` 592` wenzelm@5069 ` 593` ```Goal "(Suc k * m = Suc k * n) = (m = n)"; ``` wenzelm@4423 ` 594` ```by (rtac mult_cancel1 1); ``` wenzelm@4297 ` 595` ```by (Simp_tac 1); ``` wenzelm@4297 ` 596` ```qed "Suc_mult_cancel1"; ``` wenzelm@4297 ` 597` paulson@3234 ` 598` paulson@1795 ` 599` ```(** Lemma for gcd **) ``` paulson@1795 ` 600` paulson@5143 ` 601` ```Goal "m = m*n ==> n=1 | m=0"; ``` paulson@1795 ` 602` ```by (dtac sym 1); ``` paulson@1795 ` 603` ```by (rtac disjCI 1); ``` paulson@1795 ` 604` ```by (rtac nat_less_cases 1 THEN assume_tac 2); ``` wenzelm@4089 ` 605` ```by (fast_tac (claset() addSEs [less_SucE] addss simpset()) 1); ``` nipkow@4356 ` 606` ```by (best_tac (claset() addDs [mult_less_mono2] addss simpset()) 1); ``` paulson@1795 ` 607` ```qed "mult_eq_self_implies_10"; ``` paulson@1795 ` 608` paulson@1795 ` 609` nipkow@5983 ` 610` nipkow@5983 ` 611` nipkow@5983 ` 612` ```(*---------------------------------------------------------------------------*) ``` nipkow@5983 ` 613` ```(* Various arithmetic proof procedures *) ``` nipkow@5983 ` 614` ```(*---------------------------------------------------------------------------*) ``` nipkow@5983 ` 615` nipkow@5983 ` 616` ```(*---------------------------------------------------------------------------*) ``` nipkow@5983 ` 617` ```(* 1. Cancellation of common terms *) ``` nipkow@5983 ` 618` ```(*---------------------------------------------------------------------------*) ``` nipkow@5983 ` 619` nipkow@5983 ` 620` ```(* Title: HOL/arith_data.ML ``` nipkow@5983 ` 621` ``` ID: \$Id\$ ``` nipkow@5983 ` 622` ``` Author: Markus Wenzel and Stefan Berghofer, TU Muenchen ``` nipkow@5983 ` 623` nipkow@5983 ` 624` ```Setup various arithmetic proof procedures. ``` nipkow@5983 ` 625` ```*) ``` nipkow@5983 ` 626` nipkow@5983 ` 627` ```signature ARITH_DATA = ``` nipkow@5983 ` 628` ```sig ``` nipkow@6055 ` 629` ``` val nat_cancel_sums_add: simproc list ``` nipkow@5983 ` 630` ``` val nat_cancel_sums: simproc list ``` nipkow@5983 ` 631` ``` val nat_cancel_factor: simproc list ``` nipkow@5983 ` 632` ``` val nat_cancel: simproc list ``` nipkow@5983 ` 633` ```end; ``` nipkow@5983 ` 634` nipkow@5983 ` 635` ```structure ArithData: ARITH_DATA = ``` nipkow@5983 ` 636` ```struct ``` nipkow@5983 ` 637` nipkow@5983 ` 638` nipkow@5983 ` 639` ```(** abstract syntax of structure nat: 0, Suc, + **) ``` nipkow@5983 ` 640` nipkow@5983 ` 641` ```(* mk_sum, mk_norm_sum *) ``` nipkow@5983 ` 642` nipkow@5983 ` 643` ```val one = HOLogic.mk_nat 1; ``` nipkow@5983 ` 644` ```val mk_plus = HOLogic.mk_binop "op +"; ``` nipkow@5983 ` 645` nipkow@5983 ` 646` ```fun mk_sum [] = HOLogic.zero ``` nipkow@5983 ` 647` ``` | mk_sum [t] = t ``` nipkow@5983 ` 648` ``` | mk_sum (t :: ts) = mk_plus (t, mk_sum ts); ``` nipkow@5983 ` 649` nipkow@5983 ` 650` ```(*normal form of sums: Suc (... (Suc (a + (b + ...))))*) ``` nipkow@5983 ` 651` ```fun mk_norm_sum ts = ``` nipkow@5983 ` 652` ``` let val (ones, sums) = partition (equal one) ts in ``` nipkow@5983 ` 653` ``` funpow (length ones) HOLogic.mk_Suc (mk_sum sums) ``` nipkow@5983 ` 654` ``` end; ``` nipkow@5983 ` 655` nipkow@5983 ` 656` nipkow@5983 ` 657` ```(* dest_sum *) ``` nipkow@5983 ` 658` nipkow@5983 ` 659` ```val dest_plus = HOLogic.dest_bin "op +" HOLogic.natT; ``` nipkow@5983 ` 660` nipkow@5983 ` 661` ```fun dest_sum tm = ``` nipkow@5983 ` 662` ``` if HOLogic.is_zero tm then [] ``` nipkow@5983 ` 663` ``` else ``` nipkow@5983 ` 664` ``` (case try HOLogic.dest_Suc tm of ``` nipkow@5983 ` 665` ``` Some t => one :: dest_sum t ``` nipkow@5983 ` 666` ``` | None => ``` nipkow@5983 ` 667` ``` (case try dest_plus tm of ``` nipkow@5983 ` 668` ``` Some (t, u) => dest_sum t @ dest_sum u ``` nipkow@5983 ` 669` ``` | None => [tm])); ``` nipkow@5983 ` 670` nipkow@5983 ` 671` nipkow@5983 ` 672` ```(** generic proof tools **) ``` nipkow@5983 ` 673` nipkow@5983 ` 674` ```(* prove conversions *) ``` nipkow@5983 ` 675` nipkow@5983 ` 676` ```val mk_eqv = HOLogic.mk_Trueprop o HOLogic.mk_eq; ``` nipkow@5983 ` 677` nipkow@5983 ` 678` ```fun prove_conv expand_tac norm_tac sg (t, u) = ``` nipkow@5983 ` 679` ``` mk_meta_eq (prove_goalw_cterm_nocheck [] (cterm_of sg (mk_eqv (t, u))) ``` nipkow@5983 ` 680` ``` (K [expand_tac, norm_tac])) ``` nipkow@5983 ` 681` ``` handle ERROR => error ("The error(s) above occurred while trying to prove " ^ ``` nipkow@5983 ` 682` ``` (string_of_cterm (cterm_of sg (mk_eqv (t, u))))); ``` nipkow@5983 ` 683` nipkow@5983 ` 684` ```val subst_equals = prove_goal HOL.thy "[| t = s; u = t |] ==> u = s" ``` nipkow@5983 ` 685` ``` (fn prems => [cut_facts_tac prems 1, SIMPSET' asm_simp_tac 1]); ``` nipkow@5983 ` 686` nipkow@5983 ` 687` nipkow@5983 ` 688` ```(* rewriting *) ``` nipkow@5983 ` 689` nipkow@5983 ` 690` ```fun simp_all rules = ALLGOALS (simp_tac (HOL_ss addsimps rules)); ``` nipkow@5983 ` 691` nipkow@5983 ` 692` ```val add_rules = [add_Suc, add_Suc_right, add_0, add_0_right]; ``` nipkow@5983 ` 693` ```val mult_rules = [mult_Suc, mult_Suc_right, mult_0, mult_0_right]; ``` nipkow@5983 ` 694` nipkow@5983 ` 695` nipkow@5983 ` 696` nipkow@5983 ` 697` ```(** cancel common summands **) ``` nipkow@5983 ` 698` nipkow@5983 ` 699` ```structure Sum = ``` nipkow@5983 ` 700` ```struct ``` nipkow@5983 ` 701` ``` val mk_sum = mk_norm_sum; ``` nipkow@5983 ` 702` ``` val dest_sum = dest_sum; ``` nipkow@5983 ` 703` ``` val prove_conv = prove_conv; ``` nipkow@5983 ` 704` ``` val norm_tac = simp_all add_rules THEN simp_all add_ac; ``` nipkow@5983 ` 705` ```end; ``` nipkow@5983 ` 706` nipkow@5983 ` 707` ```fun gen_uncancel_tac rule ct = ``` nipkow@5983 ` 708` ``` rtac (instantiate' [] [None, Some ct] (rule RS subst_equals)) 1; ``` nipkow@5983 ` 709` nipkow@5983 ` 710` nipkow@5983 ` 711` ```(* nat eq *) ``` nipkow@5983 ` 712` nipkow@5983 ` 713` ```structure EqCancelSums = CancelSumsFun ``` nipkow@5983 ` 714` ```(struct ``` nipkow@5983 ` 715` ``` open Sum; ``` nipkow@5983 ` 716` ``` val mk_bal = HOLogic.mk_eq; ``` nipkow@5983 ` 717` ``` val dest_bal = HOLogic.dest_bin "op =" HOLogic.natT; ``` nipkow@5983 ` 718` ``` val uncancel_tac = gen_uncancel_tac add_left_cancel; ``` nipkow@5983 ` 719` ```end); ``` nipkow@5983 ` 720` nipkow@5983 ` 721` nipkow@5983 ` 722` ```(* nat less *) ``` nipkow@5983 ` 723` nipkow@5983 ` 724` ```structure LessCancelSums = CancelSumsFun ``` nipkow@5983 ` 725` ```(struct ``` nipkow@5983 ` 726` ``` open Sum; ``` nipkow@5983 ` 727` ``` val mk_bal = HOLogic.mk_binrel "op <"; ``` nipkow@5983 ` 728` ``` val dest_bal = HOLogic.dest_bin "op <" HOLogic.natT; ``` nipkow@5983 ` 729` ``` val uncancel_tac = gen_uncancel_tac add_left_cancel_less; ``` nipkow@5983 ` 730` ```end); ``` nipkow@5983 ` 731` nipkow@5983 ` 732` nipkow@5983 ` 733` ```(* nat le *) ``` nipkow@5983 ` 734` nipkow@5983 ` 735` ```structure LeCancelSums = CancelSumsFun ``` nipkow@5983 ` 736` ```(struct ``` nipkow@5983 ` 737` ``` open Sum; ``` nipkow@5983 ` 738` ``` val mk_bal = HOLogic.mk_binrel "op <="; ``` nipkow@5983 ` 739` ``` val dest_bal = HOLogic.dest_bin "op <=" HOLogic.natT; ``` nipkow@5983 ` 740` ``` val uncancel_tac = gen_uncancel_tac add_left_cancel_le; ``` nipkow@5983 ` 741` ```end); ``` nipkow@5983 ` 742` nipkow@5983 ` 743` nipkow@5983 ` 744` ```(* nat diff *) ``` nipkow@5983 ` 745` nipkow@5983 ` 746` ```structure DiffCancelSums = CancelSumsFun ``` nipkow@5983 ` 747` ```(struct ``` nipkow@5983 ` 748` ``` open Sum; ``` nipkow@5983 ` 749` ``` val mk_bal = HOLogic.mk_binop "op -"; ``` nipkow@5983 ` 750` ``` val dest_bal = HOLogic.dest_bin "op -" HOLogic.natT; ``` nipkow@5983 ` 751` ``` val uncancel_tac = gen_uncancel_tac diff_cancel; ``` nipkow@5983 ` 752` ```end); ``` nipkow@5983 ` 753` nipkow@5983 ` 754` nipkow@5983 ` 755` nipkow@5983 ` 756` ```(** cancel common factor **) ``` nipkow@5983 ` 757` nipkow@5983 ` 758` ```structure Factor = ``` nipkow@5983 ` 759` ```struct ``` nipkow@5983 ` 760` ``` val mk_sum = mk_norm_sum; ``` nipkow@5983 ` 761` ``` val dest_sum = dest_sum; ``` nipkow@5983 ` 762` ``` val prove_conv = prove_conv; ``` nipkow@5983 ` 763` ``` val norm_tac = simp_all (add_rules @ mult_rules) THEN simp_all add_ac; ``` nipkow@5983 ` 764` ```end; ``` nipkow@5983 ` 765` nipkow@5983 ` 766` ```fun mk_cnat n = cterm_of (sign_of Nat.thy) (HOLogic.mk_nat n); ``` nipkow@5983 ` 767` nipkow@5983 ` 768` ```fun gen_multiply_tac rule k = ``` nipkow@5983 ` 769` ``` if k > 0 then ``` nipkow@5983 ` 770` ``` rtac (instantiate' [] [None, Some (mk_cnat (k - 1))] (rule RS subst_equals)) 1 ``` nipkow@5983 ` 771` ``` else no_tac; ``` nipkow@5983 ` 772` nipkow@5983 ` 773` nipkow@5983 ` 774` ```(* nat eq *) ``` nipkow@5983 ` 775` nipkow@5983 ` 776` ```structure EqCancelFactor = CancelFactorFun ``` nipkow@5983 ` 777` ```(struct ``` nipkow@5983 ` 778` ``` open Factor; ``` nipkow@5983 ` 779` ``` val mk_bal = HOLogic.mk_eq; ``` nipkow@5983 ` 780` ``` val dest_bal = HOLogic.dest_bin "op =" HOLogic.natT; ``` nipkow@5983 ` 781` ``` val multiply_tac = gen_multiply_tac Suc_mult_cancel1; ``` nipkow@5983 ` 782` ```end); ``` nipkow@5983 ` 783` nipkow@5983 ` 784` nipkow@5983 ` 785` ```(* nat less *) ``` nipkow@5983 ` 786` nipkow@5983 ` 787` ```structure LessCancelFactor = CancelFactorFun ``` nipkow@5983 ` 788` ```(struct ``` nipkow@5983 ` 789` ``` open Factor; ``` nipkow@5983 ` 790` ``` val mk_bal = HOLogic.mk_binrel "op <"; ``` nipkow@5983 ` 791` ``` val dest_bal = HOLogic.dest_bin "op <" HOLogic.natT; ``` nipkow@5983 ` 792` ``` val multiply_tac = gen_multiply_tac Suc_mult_less_cancel1; ``` nipkow@5983 ` 793` ```end); ``` nipkow@5983 ` 794` nipkow@5983 ` 795` nipkow@5983 ` 796` ```(* nat le *) ``` nipkow@5983 ` 797` nipkow@5983 ` 798` ```structure LeCancelFactor = CancelFactorFun ``` nipkow@5983 ` 799` ```(struct ``` nipkow@5983 ` 800` ``` open Factor; ``` nipkow@5983 ` 801` ``` val mk_bal = HOLogic.mk_binrel "op <="; ``` nipkow@5983 ` 802` ``` val dest_bal = HOLogic.dest_bin "op <=" HOLogic.natT; ``` nipkow@5983 ` 803` ``` val multiply_tac = gen_multiply_tac Suc_mult_le_cancel1; ``` nipkow@5983 ` 804` ```end); ``` nipkow@5983 ` 805` nipkow@5983 ` 806` nipkow@5983 ` 807` nipkow@5983 ` 808` ```(** prepare nat_cancel simprocs **) ``` nipkow@5983 ` 809` nipkow@5983 ` 810` ```fun prep_pat s = Thm.read_cterm (sign_of Arith.thy) (s, HOLogic.termTVar); ``` nipkow@5983 ` 811` ```val prep_pats = map prep_pat; ``` nipkow@5983 ` 812` nipkow@5983 ` 813` ```fun prep_simproc (name, pats, proc) = Simplifier.mk_simproc name pats proc; ``` nipkow@5983 ` 814` nipkow@5983 ` 815` ```val eq_pats = prep_pats ["(l::nat) + m = n", "(l::nat) = m + n", "Suc m = n", "m = Suc n"]; ``` nipkow@5983 ` 816` ```val less_pats = prep_pats ["(l::nat) + m < n", "(l::nat) < m + n", "Suc m < n", "m < Suc n"]; ``` nipkow@5983 ` 817` ```val le_pats = prep_pats ["(l::nat) + m <= n", "(l::nat) <= m + n", "Suc m <= n", "m <= Suc n"]; ``` nipkow@5983 ` 818` ```val diff_pats = prep_pats ["((l::nat) + m) - n", "(l::nat) - (m + n)", "Suc m - n", "m - Suc n"]; ``` nipkow@5983 ` 819` nipkow@6055 ` 820` ```val nat_cancel_sums_add = map prep_simproc ``` nipkow@5983 ` 821` ``` [("nateq_cancel_sums", eq_pats, EqCancelSums.proc), ``` nipkow@5983 ` 822` ``` ("natless_cancel_sums", less_pats, LessCancelSums.proc), ``` nipkow@6055 ` 823` ``` ("natle_cancel_sums", le_pats, LeCancelSums.proc)]; ``` nipkow@6055 ` 824` nipkow@6055 ` 825` ```val nat_cancel_sums = nat_cancel_sums_add @ ``` nipkow@6055 ` 826` ``` [prep_simproc("natdiff_cancel_sums", diff_pats, DiffCancelSums.proc)]; ``` nipkow@5983 ` 827` nipkow@5983 ` 828` ```val nat_cancel_factor = map prep_simproc ``` nipkow@5983 ` 829` ``` [("nateq_cancel_factor", eq_pats, EqCancelFactor.proc), ``` nipkow@5983 ` 830` ``` ("natless_cancel_factor", less_pats, LessCancelFactor.proc), ``` nipkow@5983 ` 831` ``` ("natle_cancel_factor", le_pats, LeCancelFactor.proc)]; ``` nipkow@5983 ` 832` nipkow@5983 ` 833` ```val nat_cancel = nat_cancel_factor @ nat_cancel_sums; ``` nipkow@5983 ` 834` nipkow@5983 ` 835` nipkow@5983 ` 836` ```end; ``` nipkow@5983 ` 837` nipkow@5983 ` 838` ```open ArithData; ``` nipkow@5983 ` 839` nipkow@5983 ` 840` ```Addsimprocs nat_cancel; ``` nipkow@5983 ` 841` nipkow@5983 ` 842` ```(*---------------------------------------------------------------------------*) ``` nipkow@5983 ` 843` ```(* 2. Linear arithmetic *) ``` nipkow@5983 ` 844` ```(*---------------------------------------------------------------------------*) ``` nipkow@5983 ` 845` nipkow@5983 ` 846` ```(* Parameter data for general linear arithmetic functor *) ``` nipkow@5983 ` 847` ```structure Nat_LA_Data: LIN_ARITH_DATA = ``` nipkow@5983 ` 848` ```struct ``` nipkow@5983 ` 849` ```val ccontr = ccontr; ``` nipkow@5983 ` 850` ```val conjI = conjI; ``` nipkow@5983 ` 851` ```val lessD = Suc_leI; ``` nipkow@6059 ` 852` ```val neqE = nat_neqE; ``` nipkow@5983 ` 853` ```val notI = notI; ``` nipkow@5983 ` 854` ```val not_leD = not_leE RS Suc_leI; ``` nipkow@5983 ` 855` ```val not_lessD = leI; ``` nipkow@5983 ` 856` ```val sym = sym; ``` nipkow@5983 ` 857` nipkow@5983 ` 858` ```(* Turn term into list of summand * multiplicity plus a constant *) ``` nipkow@5983 ` 859` ```fun poly(Const("Suc",_)\$t, (p,i)) = poly(t, (p,i+1)) ``` nipkow@6059 ` 860` ``` | poly(Const("op +",_) \$ s \$ t, pi) = poly(s,poly(t,pi)) ``` nipkow@5983 ` 861` ``` | poly(t,(p,i)) = ``` nipkow@6059 ` 862` ``` if t = Const("0",HOLogic.natT) then (p,i) ``` nipkow@5983 ` 863` ``` else (case assoc(p,t) of None => ((t,1)::p,i) ``` nipkow@5983 ` 864` ``` | Some m => (overwrite(p,(t,m+1)), i)) ``` nipkow@5983 ` 865` nipkow@6059 ` 866` ```fun nnb T = T = ([HOLogic.natT,HOLogic.natT] ---> HOLogic.boolT); ``` nipkow@6059 ` 867` nipkow@5983 ` 868` ```fun decomp2(rel,T,lhs,rhs) = ``` nipkow@5983 ` 869` ``` if not(nnb T) then None else ``` nipkow@5983 ` 870` ``` let val (p,i) = poly(lhs,([],0)) and (q,j) = poly(rhs,([],0)) ``` nipkow@5983 ` 871` ``` in case rel of ``` nipkow@5983 ` 872` ``` "op <" => Some(p,i,"<",q,j) ``` nipkow@5983 ` 873` ``` | "op <=" => Some(p,i,"<=",q,j) ``` nipkow@5983 ` 874` ``` | "op =" => Some(p,i,"=",q,j) ``` nipkow@5983 ` 875` ``` | _ => None ``` nipkow@5983 ` 876` ``` end; ``` nipkow@5983 ` 877` nipkow@5983 ` 878` ```fun negate(Some(x,i,rel,y,j)) = Some(x,i,"~"^rel,y,j) ``` nipkow@5983 ` 879` ``` | negate None = None; ``` nipkow@5983 ` 880` nipkow@5983 ` 881` ```fun decomp(_\$(Const(rel,T)\$lhs\$rhs)) = decomp2(rel,T,lhs,rhs) ``` nipkow@5983 ` 882` ``` | decomp(_\$(Const("Not",_)\$(Const(rel,T)\$lhs\$rhs))) = ``` nipkow@5983 ` 883` ``` negate(decomp2(rel,T,lhs,rhs)) ``` nipkow@5983 ` 884` ``` | decomp _ = None ``` nipkow@6055 ` 885` nipkow@5983 ` 886` ```(* reduce contradictory <= to False. ``` nipkow@5983 ` 887` ``` Most of the work is done by the cancel tactics. ``` nipkow@5983 ` 888` ```*) ``` nipkow@5983 ` 889` ```val add_rules = [Zero_not_Suc,Suc_not_Zero,le_0_eq]; ``` nipkow@5983 ` 890` nipkow@5983 ` 891` ```val cancel_sums_ss = HOL_basic_ss addsimps add_rules ``` nipkow@6055 ` 892` ``` addsimprocs nat_cancel_sums_add; ``` nipkow@5983 ` 893` nipkow@5983 ` 894` ```val simp = simplify cancel_sums_ss; ``` nipkow@5983 ` 895` nipkow@5983 ` 896` ```val add_mono_thms = map (fn s => prove_goal Arith.thy s ``` nipkow@5983 ` 897` ``` (fn prems => [cut_facts_tac prems 1, ``` nipkow@5983 ` 898` ``` blast_tac (claset() addIs [add_le_mono]) 1])) ``` nipkow@5983 ` 899` ```["(i <= j) & (k <= l) ==> i + k <= j + (l::nat)", ``` nipkow@6055 ` 900` ``` "(i = j) & (k <= l) ==> i + k <= j + (l::nat)", ``` nipkow@6055 ` 901` ``` "(i <= j) & (k = l) ==> i + k <= j + (l::nat)", ``` nipkow@6055 ` 902` ``` "(i = j) & (k = l) ==> i + k = j + (l::nat)" ``` nipkow@5983 ` 903` ```]; ``` nipkow@5983 ` 904` nipkow@6055 ` 905` ```fun is_False thm = ``` nipkow@6055 ` 906` ``` let val _ \$ t = #prop(rep_thm thm) ``` nipkow@6059 ` 907` ``` in t = Const("False",HOLogic.boolT) end; ``` nipkow@6055 ` 908` nipkow@6059 ` 909` ```fun is_nat(t) = fastype_of1 t = HOLogic.natT; ``` nipkow@6055 ` 910` nipkow@6055 ` 911` ```fun mk_nat_thm sg t = ``` nipkow@6059 ` 912` ``` let val ct = cterm_of sg t and cn = cterm_of sg (Var(("n",0),HOLogic.natT)) ``` nipkow@6055 ` 913` ``` in instantiate ([],[(cn,ct)]) le0 end; ``` nipkow@6055 ` 914` nipkow@5983 ` 915` ```end; ``` nipkow@5983 ` 916` nipkow@5983 ` 917` ```structure Fast_Nat_Arith = Fast_Lin_Arith(Nat_LA_Data); ``` nipkow@5983 ` 918` nipkow@5983 ` 919` ```simpset_ref () := (simpset() addSolver Fast_Nat_Arith.cut_lin_arith_tac); ``` nipkow@5983 ` 920` nipkow@6055 ` 921` ```val fast_nat_arith_tac = Fast_Nat_Arith.lin_arith_tac; ``` nipkow@6055 ` 922` nipkow@6055 ` 923` ```(* Elimination of `-' on nat due to John Harrison *) ``` nipkow@6055 ` 924` ```Goal "P(a - b::nat) = (!d. (b = a + d --> P 0) & (a = b + d --> P d))"; ``` nipkow@6055 ` 925` ```by(case_tac "a <= b" 1); ``` nipkow@6055 ` 926` ```by(Auto_tac); ``` nipkow@6055 ` 927` ```by(eres_inst_tac [("x","b-a")] allE 1); ``` nipkow@6055 ` 928` ```by(Auto_tac); ``` nipkow@6055 ` 929` ```qed "nat_diff_split"; ``` nipkow@6055 ` 930` nipkow@6055 ` 931` ```(* FIXME: K true should be replaced by a sensible test to speed things up ``` nipkow@6055 ` 932` ``` in case there are lots of irrelevant terms involved ``` nipkow@6055 ` 933` ```*) ``` nipkow@6055 ` 934` ```val nat_arith_tac = ``` nipkow@6055 ` 935` ``` refute_tac (K true) (REPEAT o split_tac[nat_diff_split]) ``` nipkow@6055 ` 936` ``` ((REPEAT o etac nat_neqE) THEN' fast_nat_arith_tac); ``` nipkow@6055 ` 937` nipkow@5983 ` 938` ```(*---------------------------------------------------------------------------*) ``` nipkow@5983 ` 939` ```(* End of proof procedures. Now go and USE them! *) ``` nipkow@5983 ` 940` ```(*---------------------------------------------------------------------------*) ``` nipkow@5983 ` 941` paulson@4736 ` 942` ```(*** Subtraction laws -- mostly from Clemens Ballarin ***) ``` paulson@3234 ` 943` paulson@5429 ` 944` ```Goal "[| a < (b::nat); c <= a |] ==> a-c < b-c"; ``` nipkow@6055 ` 945` ```by(nat_arith_tac 1); ``` paulson@3234 ` 946` ```qed "diff_less_mono"; ``` paulson@3234 ` 947` paulson@5429 ` 948` ```Goal "a+b < (c::nat) ==> a < c-b"; ``` nipkow@6055 ` 949` ```by(nat_arith_tac 1); ``` paulson@3234 ` 950` ```qed "add_less_imp_less_diff"; ``` paulson@3234 ` 951` nipkow@5427 ` 952` ```Goal "(i < j-k) = (i+k < (j::nat))"; ``` nipkow@6055 ` 953` ```by(nat_arith_tac 1); ``` nipkow@5427 ` 954` ```qed "less_diff_conv"; ``` nipkow@5427 ` 955` paulson@5497 ` 956` ```Goal "(j-k <= (i::nat)) = (j <= i+k)"; ``` nipkow@6055 ` 957` ```by(nat_arith_tac 1); ``` paulson@5485 ` 958` ```qed "le_diff_conv"; ``` paulson@5485 ` 959` paulson@5497 ` 960` ```Goal "k <= j ==> (i <= j-k) = (i+k <= (j::nat))"; ``` nipkow@6055 ` 961` ```by(nat_arith_tac 1); ``` paulson@5497 ` 962` ```qed "le_diff_conv2"; ``` paulson@5497 ` 963` paulson@5143 ` 964` ```Goal "Suc i <= n ==> Suc (n - Suc i) = n - i"; ``` nipkow@6055 ` 965` ```by(nat_arith_tac 1); ``` paulson@3234 ` 966` ```qed "Suc_diff_Suc"; ``` paulson@3234 ` 967` paulson@5429 ` 968` ```Goal "i <= (n::nat) ==> n - (n - i) = i"; ``` nipkow@6055 ` 969` ```by(nat_arith_tac 1); ``` paulson@3234 ` 970` ```qed "diff_diff_cancel"; ``` paulson@3381 ` 971` ```Addsimps [diff_diff_cancel]; ``` paulson@3234 ` 972` paulson@5429 ` 973` ```Goal "k <= (n::nat) ==> m <= n + m - k"; ``` nipkow@6055 ` 974` ```by(nat_arith_tac 1); ``` paulson@3234 ` 975` ```qed "le_add_diff"; ``` paulson@3234 ` 976` nipkow@6055 ` 977` ```Goal "[| 0 j+k-i < k"; ``` nipkow@6055 ` 978` ```by(nat_arith_tac 1); ``` nipkow@6055 ` 979` ```qed "add_diff_less"; ``` paulson@3234 ` 980` paulson@5356 ` 981` ```Goal "m-1 < n ==> m <= n"; ``` nipkow@6055 ` 982` ```by(nat_arith_tac 1); ``` paulson@5356 ` 983` ```qed "pred_less_imp_le"; ``` paulson@5356 ` 984` paulson@5356 ` 985` ```Goal "j<=i ==> i - j < Suc i - j"; ``` nipkow@6055 ` 986` ```by(nat_arith_tac 1); ``` paulson@5356 ` 987` ```qed "diff_less_Suc_diff"; ``` paulson@5356 ` 988` paulson@5356 ` 989` ```Goal "i - j <= Suc i - j"; ``` nipkow@6055 ` 990` ```by(nat_arith_tac 1); ``` paulson@5356 ` 991` ```qed "diff_le_Suc_diff"; ``` paulson@5356 ` 992` ```AddIffs [diff_le_Suc_diff]; ``` paulson@5356 ` 993` paulson@5356 ` 994` ```Goal "n - Suc i <= n - i"; ``` nipkow@6055 ` 995` ```by(nat_arith_tac 1); ``` paulson@5356 ` 996` ```qed "diff_Suc_le_diff"; ``` paulson@5356 ` 997` ```AddIffs [diff_Suc_le_diff]; ``` paulson@5356 ` 998` paulson@5409 ` 999` ```Goal "0 < n ==> (m <= n-1) = (m (m-1 < n) = (m<=n)"; ``` nipkow@6055 ` 1004` ```by(nat_arith_tac 1); ``` paulson@5409 ` 1005` ```qed "less_pred_eq"; ``` paulson@5409 ` 1006` paulson@5414 ` 1007` ```(*In ordinary notation: if 0 m - n < m"; ``` nipkow@6055 ` 1009` ```by(nat_arith_tac 1); ``` paulson@5414 ` 1010` ```qed "diff_less"; ``` paulson@5414 ` 1011` paulson@5414 ` 1012` ```Goal "[| 0 m - n < m"; ``` nipkow@6055 ` 1013` ```by(nat_arith_tac 1); ``` paulson@5414 ` 1014` ```qed "le_diff_less"; ``` paulson@5414 ` 1015` paulson@5356 ` 1016` paulson@4732 ` 1017` nipkow@3484 ` 1018` ```(** (Anti)Monotonicity of subtraction -- by Stefan Merz **) ``` nipkow@3484 ` 1019` nipkow@3484 ` 1020` ```(* Monotonicity of subtraction in first argument *) ``` nipkow@6055 ` 1021` ```Goal "m <= (n::nat) ==> (m-l) <= (n-l)"; ``` nipkow@6055 ` 1022` ```by(nat_arith_tac 1); ``` nipkow@6055 ` 1023` ```qed "diff_le_mono"; ``` nipkow@3484 ` 1024` paulson@5429 ` 1025` ```Goal "m <= (n::nat) ==> (l-n) <= (l-m)"; ``` nipkow@6055 ` 1026` ```by(nat_arith_tac 1); ``` nipkow@6055 ` 1027` ```qed "diff_le_mono2"; ``` nipkow@5983 ` 1028` nipkow@5983 ` 1029` nipkow@5983 ` 1030` ```(*This proof requires natdiff_cancel_sums*) ``` nipkow@6055 ` 1031` ```Goal "[| m < (n::nat); m (l-n) < (l-m)"; ``` nipkow@6055 ` 1032` ```by(nat_arith_tac 1); ``` nipkow@6055 ` 1033` ```qed "diff_less_mono2"; ``` nipkow@5983 ` 1034` nipkow@6055 ` 1035` ```Goal "[| m-n = 0; n-m = 0 |] ==> m=n"; ``` nipkow@6055 ` 1036` ```by(nat_arith_tac 1); ``` nipkow@6055 ` 1037` ```qed "diffs0_imp_equal"; ```