src/HOL/Arith.ML
 author paulson Thu Sep 23 13:06:31 1999 +0200 (1999-09-23) changeset 7584 5be4bb8e4e3f parent 7582 2650c9c2ab7f child 7622 dcb93b295683 permissions -rw-r--r--
tidied; added lemma restrict_to_left
 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@7007 ` 15` ```Goal "0 - n = 0"; ``` paulson@7007 ` 16` ```by (induct_tac "n" 1); ``` paulson@7007 ` 17` ```by (ALLGOALS Asm_simp_tac); ``` paulson@7007 ` 18` ```qed "diff_0_eq_0"; ``` clasohm@923 ` 19` paulson@5429 ` 20` ```(*Must simplify BEFORE the induction! (Else we get a critical pair) ``` clasohm@923 ` 21` ``` Suc(m) - Suc(n) rewrites to pred(Suc(m) - n) *) ``` paulson@7007 ` 22` ```Goal "Suc(m) - Suc(n) = m - n"; ``` paulson@7007 ` 23` ```by (Simp_tac 1); ``` paulson@7007 ` 24` ```by (induct_tac "n" 1); ``` paulson@7007 ` 25` ```by (ALLGOALS Asm_simp_tac); ``` paulson@7007 ` 26` ```qed "diff_Suc_Suc"; ``` clasohm@923 ` 27` pusch@2682 ` 28` ```Addsimps [diff_0_eq_0, diff_Suc_Suc]; ``` clasohm@923 ` 29` nipkow@4360 ` 30` ```(* Could be (and is, below) generalized in various ways; ``` nipkow@4360 ` 31` ``` However, none of the generalizations are currently in the simpset, ``` nipkow@4360 ` 32` ``` and I dread to think what happens if I put them in *) ``` paulson@5143 ` 33` ```Goal "0 < n ==> Suc(n-1) = n"; ``` berghofe@5183 ` 34` ```by (asm_simp_tac (simpset() addsplits [nat.split]) 1); ``` nipkow@4360 ` 35` ```qed "Suc_pred"; ``` nipkow@4360 ` 36` ```Addsimps [Suc_pred]; ``` nipkow@4360 ` 37` nipkow@4360 ` 38` ```Delsimps [diff_Suc]; ``` nipkow@4360 ` 39` clasohm@923 ` 40` clasohm@923 ` 41` ```(**** Inductive properties of the operators ****) ``` clasohm@923 ` 42` clasohm@923 ` 43` ```(*** Addition ***) ``` clasohm@923 ` 44` paulson@7007 ` 45` ```Goal "m + 0 = m"; ``` paulson@7007 ` 46` ```by (induct_tac "m" 1); ``` paulson@7007 ` 47` ```by (ALLGOALS Asm_simp_tac); ``` paulson@7007 ` 48` ```qed "add_0_right"; ``` clasohm@923 ` 49` paulson@7007 ` 50` ```Goal "m + Suc(n) = Suc(m+n)"; ``` paulson@7007 ` 51` ```by (induct_tac "m" 1); ``` paulson@7007 ` 52` ```by (ALLGOALS Asm_simp_tac); ``` paulson@7007 ` 53` ```qed "add_Suc_right"; ``` clasohm@923 ` 54` clasohm@1264 ` 55` ```Addsimps [add_0_right,add_Suc_right]; ``` clasohm@923 ` 56` paulson@7007 ` 57` clasohm@923 ` 58` ```(*Associative law for addition*) ``` paulson@7007 ` 59` ```Goal "(m + n) + k = m + ((n + k)::nat)"; ``` paulson@7007 ` 60` ```by (induct_tac "m" 1); ``` paulson@7007 ` 61` ```by (ALLGOALS Asm_simp_tac); ``` paulson@7007 ` 62` ```qed "add_assoc"; ``` clasohm@923 ` 63` clasohm@923 ` 64` ```(*Commutative law for addition*) ``` paulson@7007 ` 65` ```Goal "m + n = n + (m::nat)"; ``` paulson@7007 ` 66` ```by (induct_tac "m" 1); ``` paulson@7007 ` 67` ```by (ALLGOALS Asm_simp_tac); ``` paulson@7007 ` 68` ```qed "add_commute"; ``` clasohm@923 ` 69` paulson@7007 ` 70` ```Goal "x+(y+z)=y+((x+z)::nat)"; ``` paulson@7007 ` 71` ```by (rtac (add_commute RS trans) 1); ``` paulson@7007 ` 72` ```by (rtac (add_assoc RS trans) 1); ``` paulson@7007 ` 73` ```by (rtac (add_commute RS arg_cong) 1); ``` paulson@7007 ` 74` ```qed "add_left_commute"; ``` clasohm@923 ` 75` clasohm@923 ` 76` ```(*Addition is an AC-operator*) ``` wenzelm@7428 ` 77` ```bind_thms ("add_ac", [add_assoc, add_commute, add_left_commute]); ``` clasohm@923 ` 78` paulson@5429 ` 79` ```Goal "(k + m = k + n) = (m=(n::nat))"; ``` paulson@3339 ` 80` ```by (induct_tac "k" 1); ``` clasohm@1264 ` 81` ```by (Simp_tac 1); ``` clasohm@1264 ` 82` ```by (Asm_simp_tac 1); ``` clasohm@923 ` 83` ```qed "add_left_cancel"; ``` clasohm@923 ` 84` paulson@5429 ` 85` ```Goal "(m + k = n + k) = (m=(n::nat))"; ``` paulson@3339 ` 86` ```by (induct_tac "k" 1); ``` clasohm@1264 ` 87` ```by (Simp_tac 1); ``` clasohm@1264 ` 88` ```by (Asm_simp_tac 1); ``` clasohm@923 ` 89` ```qed "add_right_cancel"; ``` clasohm@923 ` 90` paulson@5429 ` 91` ```Goal "(k + m <= k + n) = (m<=(n::nat))"; ``` paulson@3339 ` 92` ```by (induct_tac "k" 1); ``` clasohm@1264 ` 93` ```by (Simp_tac 1); ``` clasohm@1264 ` 94` ```by (Asm_simp_tac 1); ``` clasohm@923 ` 95` ```qed "add_left_cancel_le"; ``` clasohm@923 ` 96` paulson@5429 ` 97` ```Goal "(k + m < k + n) = (m<(n::nat))"; ``` paulson@3339 ` 98` ```by (induct_tac "k" 1); ``` clasohm@1264 ` 99` ```by (Simp_tac 1); ``` clasohm@1264 ` 100` ```by (Asm_simp_tac 1); ``` clasohm@923 ` 101` ```qed "add_left_cancel_less"; ``` clasohm@923 ` 102` nipkow@1327 ` 103` ```Addsimps [add_left_cancel, add_right_cancel, ``` nipkow@1327 ` 104` ``` add_left_cancel_le, add_left_cancel_less]; ``` nipkow@1327 ` 105` paulson@3339 ` 106` ```(** Reasoning about m+0=0, etc. **) ``` paulson@3339 ` 107` wenzelm@5069 ` 108` ```Goal "(m+n = 0) = (m=0 & n=0)"; ``` nipkow@5598 ` 109` ```by (exhaust_tac "m" 1); ``` nipkow@5598 ` 110` ```by (Auto_tac); ``` nipkow@1327 ` 111` ```qed "add_is_0"; ``` nipkow@4360 ` 112` ```AddIffs [add_is_0]; ``` nipkow@1327 ` 113` nipkow@5598 ` 114` ```Goal "(0 = m+n) = (m=0 & n=0)"; ``` nipkow@5598 ` 115` ```by (exhaust_tac "m" 1); ``` nipkow@5598 ` 116` ```by (Auto_tac); ``` nipkow@5598 ` 117` ```qed "zero_is_add"; ``` nipkow@5598 ` 118` ```AddIffs [zero_is_add]; ``` nipkow@5598 ` 119` nipkow@5598 ` 120` ```Goal "(m+n=1) = (m=1 & n=0 | m=0 & n=1)"; ``` paulson@6301 ` 121` ```by (exhaust_tac "m" 1); ``` paulson@6301 ` 122` ```by (Auto_tac); ``` nipkow@5598 ` 123` ```qed "add_is_1"; ``` nipkow@5598 ` 124` nipkow@5598 ` 125` ```Goal "(1=m+n) = (m=1 & n=0 | m=0 & n=1)"; ``` paulson@6301 ` 126` ```by (exhaust_tac "m" 1); ``` paulson@6301 ` 127` ```by (Auto_tac); ``` nipkow@5598 ` 128` ```qed "one_is_add"; ``` nipkow@5598 ` 129` wenzelm@5069 ` 130` ```Goal "(0 m+(n-(Suc k)) = (m+n)-(Suc k)" *) ``` paulson@5143 ` 143` ```Goal "0 m + (n-1) = (m+n)-1"; ``` nipkow@4360 ` 144` ```by (exhaust_tac "m" 1); ``` nipkow@6075 ` 145` ```by (ALLGOALS (asm_simp_tac (simpset() addsimps [diff_Suc, Suc_n_not_n] ``` berghofe@5183 ` 146` ``` addsplits [nat.split]))); ``` nipkow@1327 ` 147` ```qed "add_pred"; ``` nipkow@1327 ` 148` ```Addsimps [add_pred]; ``` nipkow@1327 ` 149` paulson@5429 ` 150` ```Goal "m + n = m ==> n = 0"; ``` paulson@5078 ` 151` ```by (dtac (add_0_right RS ssubst) 1); ``` paulson@5078 ` 152` ```by (asm_full_simp_tac (simpset() addsimps [add_assoc] ``` paulson@5078 ` 153` ``` delsimps [add_0_right]) 1); ``` paulson@5078 ` 154` ```qed "add_eq_self_zero"; ``` paulson@5078 ` 155` paulson@1626 ` 156` clasohm@923 ` 157` ```(**** Additional theorems about "less than" ****) ``` clasohm@923 ` 158` paulson@5078 ` 159` ```(*Deleted less_natE; instead use less_eq_Suc_add RS exE*) ``` paulson@5143 ` 160` ```Goal "m (? k. n=Suc(m+k))"; ``` paulson@3339 ` 161` ```by (induct_tac "n" 1); ``` paulson@5604 ` 162` ```by (ALLGOALS (simp_tac (simpset() addsimps [order_le_less]))); ``` wenzelm@4089 ` 163` ```by (blast_tac (claset() addSEs [less_SucE] ``` paulson@5497 ` 164` ``` addSIs [add_0_right RS sym, add_Suc_right RS sym]) 1); ``` nipkow@1485 ` 165` ```qed_spec_mp "less_eq_Suc_add"; ``` clasohm@923 ` 166` wenzelm@5069 ` 167` ```Goal "n <= ((m + n)::nat)"; ``` paulson@3339 ` 168` ```by (induct_tac "m" 1); ``` clasohm@1264 ` 169` ```by (ALLGOALS Simp_tac); ``` nipkow@5983 ` 170` ```by (etac le_SucI 1); ``` clasohm@923 ` 171` ```qed "le_add2"; ``` clasohm@923 ` 172` wenzelm@5069 ` 173` ```Goal "n <= ((n + m)::nat)"; ``` wenzelm@4089 ` 174` ```by (simp_tac (simpset() addsimps add_ac) 1); ``` clasohm@923 ` 175` ```by (rtac le_add2 1); ``` clasohm@923 ` 176` ```qed "le_add1"; ``` clasohm@923 ` 177` clasohm@923 ` 178` ```bind_thm ("less_add_Suc1", (lessI RS (le_add1 RS le_less_trans))); ``` clasohm@923 ` 179` ```bind_thm ("less_add_Suc2", (lessI RS (le_add2 RS le_less_trans))); ``` clasohm@923 ` 180` paulson@5429 ` 181` ```Goal "(m i <= j+m"*) ``` clasohm@923 ` 187` ```bind_thm ("trans_le_add1", le_add1 RSN (2,le_trans)); ``` clasohm@923 ` 188` clasohm@923 ` 189` ```(*"i <= j ==> i <= m+j"*) ``` clasohm@923 ` 190` ```bind_thm ("trans_le_add2", le_add2 RSN (2,le_trans)); ``` clasohm@923 ` 191` clasohm@923 ` 192` ```(*"i < j ==> i < j+m"*) ``` clasohm@923 ` 193` ```bind_thm ("trans_less_add1", le_add1 RSN (2,less_le_trans)); ``` clasohm@923 ` 194` clasohm@923 ` 195` ```(*"i < j ==> i < m+j"*) ``` clasohm@923 ` 196` ```bind_thm ("trans_less_add2", le_add2 RSN (2,less_le_trans)); ``` clasohm@923 ` 197` nipkow@5654 ` 198` ```Goal "i+j < (k::nat) --> i m<=(n::nat)"; ``` paulson@3339 ` 215` ```by (induct_tac "k" 1); ``` paulson@5497 ` 216` ```by (ALLGOALS (asm_simp_tac (simpset() addsimps le_simps))); ``` nipkow@1485 ` 217` ```qed_spec_mp "add_leD1"; ``` clasohm@923 ` 218` paulson@5429 ` 219` ```Goal "m+k<=n ==> k<=(n::nat)"; ``` wenzelm@4089 ` 220` ```by (full_simp_tac (simpset() addsimps [add_commute]) 1); ``` paulson@2498 ` 221` ```by (etac add_leD1 1); ``` paulson@2498 ` 222` ```qed_spec_mp "add_leD2"; ``` paulson@2498 ` 223` paulson@5429 ` 224` ```Goal "m+k<=n ==> m<=n & k<=(n::nat)"; ``` wenzelm@4089 ` 225` ```by (blast_tac (claset() addDs [add_leD1, add_leD2]) 1); ``` paulson@2498 ` 226` ```bind_thm ("add_leE", result() RS conjE); ``` paulson@2498 ` 227` paulson@5429 ` 228` ```(*needs !!k for add_ac to work*) ``` paulson@5429 ` 229` ```Goal "!!k:: nat. [| k m i + k < j + (k::nat)"; ``` paulson@3339 ` 241` ```by (induct_tac "k" 1); ``` clasohm@1264 ` 242` ```by (ALLGOALS Asm_simp_tac); ``` clasohm@923 ` 243` ```qed "add_less_mono1"; ``` clasohm@923 ` 244` clasohm@923 ` 245` ```(*strict, in both arguments*) ``` paulson@5429 ` 246` ```Goal "[|i < j; k < l|] ==> i + k < j + (l::nat)"; ``` clasohm@923 ` 247` ```by (rtac (add_less_mono1 RS less_trans) 1); ``` lcp@1198 ` 248` ```by (REPEAT (assume_tac 1)); ``` paulson@3339 ` 249` ```by (induct_tac "j" 1); ``` clasohm@1264 ` 250` ```by (ALLGOALS Asm_simp_tac); ``` clasohm@923 ` 251` ```qed "add_less_mono"; ``` clasohm@923 ` 252` clasohm@923 ` 253` ```(*A [clumsy] way of lifting < monotonicity to <= monotonicity *) ``` paulson@5316 ` 254` ```val [lt_mono,le] = Goal ``` clasohm@1465 ` 255` ``` "[| !!i j::nat. i f(i) < f(j); \ ``` clasohm@1465 ` 256` ```\ i <= j \ ``` clasohm@923 ` 257` ```\ |] ==> f(i) <= (f(j)::nat)"; ``` clasohm@923 ` 258` ```by (cut_facts_tac [le] 1); ``` paulson@5604 ` 259` ```by (asm_full_simp_tac (simpset() addsimps [order_le_less]) 1); ``` wenzelm@4089 ` 260` ```by (blast_tac (claset() addSIs [lt_mono]) 1); ``` clasohm@923 ` 261` ```qed "less_mono_imp_le_mono"; ``` clasohm@923 ` 262` clasohm@923 ` 263` ```(*non-strict, in 1st argument*) ``` paulson@5429 ` 264` ```Goal "i<=j ==> i + k <= j + (k::nat)"; ``` wenzelm@3842 ` 265` ```by (res_inst_tac [("f", "%j. j+k")] less_mono_imp_le_mono 1); ``` paulson@1552 ` 266` ```by (etac add_less_mono1 1); ``` clasohm@923 ` 267` ```by (assume_tac 1); ``` clasohm@923 ` 268` ```qed "add_le_mono1"; ``` clasohm@923 ` 269` clasohm@923 ` 270` ```(*non-strict, in both arguments*) ``` paulson@5429 ` 271` ```Goal "[|i<=j; k<=l |] ==> i + k <= j + (l::nat)"; ``` clasohm@923 ` 272` ```by (etac (add_le_mono1 RS le_trans) 1); ``` wenzelm@4089 ` 273` ```by (simp_tac (simpset() addsimps [add_commute]) 1); ``` clasohm@923 ` 274` ```qed "add_le_mono"; ``` paulson@1713 ` 275` paulson@3234 ` 276` paulson@3234 ` 277` ```(*** Multiplication ***) ``` paulson@3234 ` 278` paulson@3234 ` 279` ```(*right annihilation in product*) ``` paulson@7007 ` 280` ```Goal "m * 0 = 0"; ``` paulson@7007 ` 281` ```by (induct_tac "m" 1); ``` paulson@7007 ` 282` ```by (ALLGOALS Asm_simp_tac); ``` paulson@7007 ` 283` ```qed "mult_0_right"; ``` paulson@3234 ` 284` paulson@3293 ` 285` ```(*right successor law for multiplication*) ``` paulson@7007 ` 286` ```Goal "m * Suc(n) = m + (m * n)"; ``` paulson@7007 ` 287` ```by (induct_tac "m" 1); ``` paulson@7007 ` 288` ```by (ALLGOALS(asm_simp_tac (simpset() addsimps add_ac))); ``` paulson@7007 ` 289` ```qed "mult_Suc_right"; ``` paulson@3234 ` 290` paulson@3293 ` 291` ```Addsimps [mult_0_right, mult_Suc_right]; ``` paulson@3234 ` 292` wenzelm@5069 ` 293` ```Goal "1 * n = n"; ``` paulson@3234 ` 294` ```by (Asm_simp_tac 1); ``` paulson@3234 ` 295` ```qed "mult_1"; ``` paulson@3234 ` 296` wenzelm@5069 ` 297` ```Goal "n * 1 = n"; ``` paulson@3234 ` 298` ```by (Asm_simp_tac 1); ``` paulson@3234 ` 299` ```qed "mult_1_right"; ``` paulson@3234 ` 300` paulson@3234 ` 301` ```(*Commutative law for multiplication*) ``` paulson@7007 ` 302` ```Goal "m * n = n * (m::nat)"; ``` paulson@7007 ` 303` ```by (induct_tac "m" 1); ``` paulson@7007 ` 304` ```by (ALLGOALS Asm_simp_tac); ``` paulson@7007 ` 305` ```qed "mult_commute"; ``` paulson@3234 ` 306` paulson@3234 ` 307` ```(*addition distributes over multiplication*) ``` paulson@7007 ` 308` ```Goal "(m + n)*k = (m*k) + ((n*k)::nat)"; ``` paulson@7007 ` 309` ```by (induct_tac "m" 1); ``` paulson@7007 ` 310` ```by (ALLGOALS(asm_simp_tac (simpset() addsimps add_ac))); ``` paulson@7007 ` 311` ```qed "add_mult_distrib"; ``` paulson@3234 ` 312` paulson@7007 ` 313` ```Goal "k*(m + n) = (k*m) + ((k*n)::nat)"; ``` paulson@7007 ` 314` ```by (induct_tac "m" 1); ``` paulson@7007 ` 315` ```by (ALLGOALS(asm_simp_tac (simpset() addsimps add_ac))); ``` paulson@7007 ` 316` ```qed "add_mult_distrib2"; ``` paulson@3234 ` 317` paulson@3234 ` 318` ```(*Associative law for multiplication*) ``` paulson@7007 ` 319` ```Goal "(m * n) * k = m * ((n * k)::nat)"; ``` paulson@7007 ` 320` ```by (induct_tac "m" 1); ``` paulson@7007 ` 321` ```by (ALLGOALS (asm_simp_tac (simpset() addsimps [add_mult_distrib]))); ``` paulson@7007 ` 322` ```qed "mult_assoc"; ``` paulson@3234 ` 323` paulson@7007 ` 324` ```Goal "x*(y*z) = y*((x*z)::nat)"; ``` paulson@7007 ` 325` ```by (rtac trans 1); ``` paulson@7007 ` 326` ```by (rtac mult_commute 1); ``` paulson@7007 ` 327` ```by (rtac trans 1); ``` paulson@7007 ` 328` ```by (rtac mult_assoc 1); ``` paulson@7007 ` 329` ```by (rtac (mult_commute RS arg_cong) 1); ``` paulson@7007 ` 330` ```qed "mult_left_commute"; ``` paulson@3234 ` 331` wenzelm@7428 ` 332` ```bind_thms ("mult_ac", [mult_assoc,mult_commute,mult_left_commute]); ``` paulson@3234 ` 333` wenzelm@5069 ` 334` ```Goal "(m*n = 0) = (m=0 | n=0)"; ``` paulson@3339 ` 335` ```by (induct_tac "m" 1); ``` paulson@3339 ` 336` ```by (induct_tac "n" 2); ``` paulson@3293 ` 337` ```by (ALLGOALS Asm_simp_tac); ``` paulson@3293 ` 338` ```qed "mult_is_0"; ``` paulson@3293 ` 339` ```Addsimps [mult_is_0]; ``` paulson@3293 ` 340` paulson@5429 ` 341` ```Goal "m <= m*(m::nat)"; ``` paulson@4158 ` 342` ```by (induct_tac "m" 1); ``` paulson@4158 ` 343` ```by (ALLGOALS (asm_simp_tac (simpset() addsimps [add_assoc RS sym]))); ``` paulson@4158 ` 344` ```by (etac (le_add2 RSN (2,le_trans)) 1); ``` paulson@4158 ` 345` ```qed "le_square"; ``` paulson@4158 ` 346` paulson@3234 ` 347` paulson@3234 ` 348` ```(*** Difference ***) ``` paulson@3234 ` 349` paulson@7007 ` 350` ```Goal "m - m = 0"; ``` paulson@7007 ` 351` ```by (induct_tac "m" 1); ``` paulson@7007 ` 352` ```by (ALLGOALS Asm_simp_tac); ``` paulson@7007 ` 353` ```qed "diff_self_eq_0"; ``` paulson@3234 ` 354` paulson@3234 ` 355` ```Addsimps [diff_self_eq_0]; ``` paulson@3234 ` 356` paulson@3234 ` 357` ```(*Addition is the inverse of subtraction: if n<=m then n+(m-n) = m. *) ``` wenzelm@5069 ` 358` ```Goal "~ m n+(m-n) = (m::nat)"; ``` paulson@3234 ` 359` ```by (res_inst_tac [("m","m"),("n","n")] diff_induct 1); ``` paulson@3352 ` 360` ```by (ALLGOALS Asm_simp_tac); ``` paulson@3381 ` 361` ```qed_spec_mp "add_diff_inverse"; ``` paulson@3381 ` 362` paulson@5143 ` 363` ```Goal "n<=m ==> n+(m-n) = (m::nat)"; ``` wenzelm@4089 ` 364` ```by (asm_simp_tac (simpset() addsimps [add_diff_inverse, not_less_iff_le]) 1); ``` paulson@3381 ` 365` ```qed "le_add_diff_inverse"; ``` paulson@3234 ` 366` paulson@5143 ` 367` ```Goal "n<=m ==> (m-n)+n = (m::nat)"; ``` wenzelm@4089 ` 368` ```by (asm_simp_tac (simpset() addsimps [le_add_diff_inverse, add_commute]) 1); ``` paulson@3381 ` 369` ```qed "le_add_diff_inverse2"; ``` paulson@3381 ` 370` paulson@3381 ` 371` ```Addsimps [le_add_diff_inverse, le_add_diff_inverse2]; ``` paulson@3234 ` 372` paulson@3234 ` 373` paulson@3234 ` 374` ```(*** More results about difference ***) ``` paulson@3234 ` 375` paulson@5414 ` 376` ```Goal "n <= m ==> Suc(m)-n = Suc(m-n)"; ``` paulson@5316 ` 377` ```by (etac rev_mp 1); ``` paulson@3352 ` 378` ```by (res_inst_tac [("m","m"),("n","n")] diff_induct 1); ``` paulson@3352 ` 379` ```by (ALLGOALS Asm_simp_tac); ``` paulson@5414 ` 380` ```qed "Suc_diff_le"; ``` paulson@3352 ` 381` wenzelm@5069 ` 382` ```Goal "m - n < Suc(m)"; ``` paulson@3234 ` 383` ```by (res_inst_tac [("m","m"),("n","n")] diff_induct 1); ``` paulson@3234 ` 384` ```by (etac less_SucE 3); ``` wenzelm@4089 ` 385` ```by (ALLGOALS (asm_simp_tac (simpset() addsimps [less_Suc_eq]))); ``` paulson@3234 ` 386` ```qed "diff_less_Suc"; ``` paulson@3234 ` 387` paulson@5429 ` 388` ```Goal "m - n <= (m::nat)"; ``` paulson@3234 ` 389` ```by (res_inst_tac [("m","m"), ("n","n")] diff_induct 1); ``` nipkow@6075 ` 390` ```by (ALLGOALS (asm_simp_tac (simpset() addsimps [le_SucI]))); ``` paulson@3234 ` 391` ```qed "diff_le_self"; ``` paulson@3903 ` 392` ```Addsimps [diff_le_self]; ``` paulson@3234 ` 393` paulson@4732 ` 394` ```(* j j-n < k *) ``` paulson@4732 ` 395` ```bind_thm ("less_imp_diff_less", diff_le_self RS le_less_trans); ``` paulson@4732 ` 396` wenzelm@5069 ` 397` ```Goal "!!i::nat. i-j-k = i - (j+k)"; ``` paulson@3352 ` 398` ```by (res_inst_tac [("m","i"),("n","j")] diff_induct 1); ``` paulson@3352 ` 399` ```by (ALLGOALS Asm_simp_tac); ``` paulson@3352 ` 400` ```qed "diff_diff_left"; ``` paulson@3352 ` 401` wenzelm@5069 ` 402` ```Goal "(Suc m - n) - Suc k = m - n - k"; ``` wenzelm@4423 ` 403` ```by (simp_tac (simpset() addsimps [diff_diff_left]) 1); ``` paulson@4736 ` 404` ```qed "Suc_diff_diff"; ``` paulson@4736 ` 405` ```Addsimps [Suc_diff_diff]; ``` nipkow@4360 ` 406` paulson@5143 ` 407` ```Goal "0 n - Suc i < n"; ``` berghofe@5183 ` 408` ```by (exhaust_tac "n" 1); ``` paulson@4732 ` 409` ```by Safe_tac; ``` paulson@5497 ` 410` ```by (asm_simp_tac (simpset() addsimps le_simps) 1); ``` paulson@4732 ` 411` ```qed "diff_Suc_less"; ``` paulson@4732 ` 412` ```Addsimps [diff_Suc_less]; ``` paulson@4732 ` 413` wenzelm@3396 ` 414` ```(*This and the next few suggested by Florian Kammueller*) ``` wenzelm@5069 ` 415` ```Goal "!!i::nat. i-j-k = i-k-j"; ``` wenzelm@4089 ` 416` ```by (simp_tac (simpset() addsimps [diff_diff_left, add_commute]) 1); ``` paulson@3352 ` 417` ```qed "diff_commute"; ``` paulson@3352 ` 418` paulson@5429 ` 419` ```Goal "k<=j --> j<=i --> i - (j - k) = i - j + (k::nat)"; ``` paulson@3352 ` 420` ```by (res_inst_tac [("m","i"),("n","j")] diff_induct 1); ``` paulson@3352 ` 421` ```by (ALLGOALS Asm_simp_tac); ``` paulson@5414 ` 422` ```by (asm_simp_tac (simpset() addsimps [Suc_diff_le, le_Suc_eq]) 1); ``` paulson@3352 ` 423` ```qed_spec_mp "diff_diff_right"; ``` paulson@3352 ` 424` paulson@5429 ` 425` ```Goal "k <= (j::nat) --> (i + j) - k = i + (j - k)"; ``` paulson@3352 ` 426` ```by (res_inst_tac [("m","j"),("n","k")] diff_induct 1); ``` paulson@3352 ` 427` ```by (ALLGOALS Asm_simp_tac); ``` paulson@3352 ` 428` ```qed_spec_mp "diff_add_assoc"; ``` paulson@3352 ` 429` paulson@5429 ` 430` ```Goal "k <= (j::nat) --> (j + i) - k = i + (j - k)"; ``` paulson@4732 ` 431` ```by (asm_simp_tac (simpset() addsimps [add_commute, diff_add_assoc]) 1); ``` paulson@4732 ` 432` ```qed_spec_mp "diff_add_assoc2"; ``` paulson@4732 ` 433` paulson@5429 ` 434` ```Goal "(n+m) - n = (m::nat)"; ``` paulson@3339 ` 435` ```by (induct_tac "n" 1); ``` paulson@3234 ` 436` ```by (ALLGOALS Asm_simp_tac); ``` paulson@3234 ` 437` ```qed "diff_add_inverse"; ``` paulson@3234 ` 438` ```Addsimps [diff_add_inverse]; ``` paulson@3234 ` 439` paulson@5429 ` 440` ```Goal "(m+n) - n = (m::nat)"; ``` wenzelm@4089 ` 441` ```by (simp_tac (simpset() addsimps [diff_add_assoc]) 1); ``` paulson@3234 ` 442` ```qed "diff_add_inverse2"; ``` paulson@3234 ` 443` ```Addsimps [diff_add_inverse2]; ``` paulson@3234 ` 444` paulson@5429 ` 445` ```Goal "i <= (j::nat) ==> (j-i=k) = (j=k+i)"; ``` paulson@3724 ` 446` ```by Safe_tac; ``` paulson@3381 ` 447` ```by (ALLGOALS Asm_simp_tac); ``` paulson@3366 ` 448` ```qed "le_imp_diff_is_add"; ``` paulson@3366 ` 449` paulson@5356 ` 450` ```Goal "(m-n = 0) = (m <= n)"; ``` paulson@3234 ` 451` ```by (res_inst_tac [("m","m"),("n","n")] diff_induct 1); ``` paulson@5497 ` 452` ```by (ALLGOALS Asm_simp_tac); ``` paulson@5356 ` 453` ```qed "diff_is_0_eq"; ``` paulson@7059 ` 454` paulson@7059 ` 455` ```Goal "(0 = m-n) = (m <= n)"; ``` paulson@7059 ` 456` ```by (stac (diff_is_0_eq RS sym) 1); ``` paulson@7059 ` 457` ```by (rtac eq_sym_conv 1); ``` paulson@7059 ` 458` ```qed "zero_is_diff_eq"; ``` paulson@7059 ` 459` ```Addsimps [diff_is_0_eq, zero_is_diff_eq]; ``` paulson@3234 ` 460` paulson@5333 ` 461` ```Goal "(0 ? k. 0 (!n. P(Suc(n))--> P(n)) --> P(k-i)"; ``` paulson@3234 ` 473` ```by (res_inst_tac [("m","k"),("n","i")] diff_induct 1); ``` paulson@3718 ` 474` ```by (ALLGOALS (Clarify_tac THEN' Simp_tac THEN' TRY o Blast_tac)); ``` paulson@3234 ` 475` ```qed "zero_induct_lemma"; ``` paulson@3234 ` 476` paulson@5316 ` 477` ```val prems = Goal "[| P(k); !!n. P(Suc(n)) ==> P(n) |] ==> P(0)"; ``` paulson@3234 ` 478` ```by (rtac (diff_self_eq_0 RS subst) 1); ``` paulson@3234 ` 479` ```by (rtac (zero_induct_lemma RS mp RS mp) 1); ``` paulson@3234 ` 480` ```by (REPEAT (ares_tac ([impI,allI]@prems) 1)); ``` paulson@3234 ` 481` ```qed "zero_induct"; ``` paulson@3234 ` 482` paulson@5429 ` 483` ```Goal "(k+m) - (k+n) = m - (n::nat)"; ``` paulson@3339 ` 484` ```by (induct_tac "k" 1); ``` paulson@3234 ` 485` ```by (ALLGOALS Asm_simp_tac); ``` paulson@3234 ` 486` ```qed "diff_cancel"; ``` paulson@3234 ` 487` ```Addsimps [diff_cancel]; ``` paulson@3234 ` 488` paulson@5429 ` 489` ```Goal "(m+k) - (n+k) = m - (n::nat)"; ``` paulson@3234 ` 490` ```val add_commute_k = read_instantiate [("n","k")] add_commute; ``` paulson@5537 ` 491` ```by (asm_simp_tac (simpset() addsimps [add_commute_k]) 1); ``` paulson@3234 ` 492` ```qed "diff_cancel2"; ``` paulson@3234 ` 493` ```Addsimps [diff_cancel2]; ``` paulson@3234 ` 494` paulson@5429 ` 495` ```Goal "n - (n+m) = 0"; ``` paulson@3339 ` 496` ```by (induct_tac "n" 1); ``` paulson@3234 ` 497` ```by (ALLGOALS Asm_simp_tac); ``` paulson@3234 ` 498` ```qed "diff_add_0"; ``` paulson@3234 ` 499` ```Addsimps [diff_add_0]; ``` paulson@3234 ` 500` paulson@5409 ` 501` paulson@3234 ` 502` ```(** Difference distributes over multiplication **) ``` paulson@3234 ` 503` wenzelm@5069 ` 504` ```Goal "!!m::nat. (m - n) * k = (m * k) - (n * k)"; ``` paulson@3234 ` 505` ```by (res_inst_tac [("m","m"),("n","n")] diff_induct 1); ``` paulson@3234 ` 506` ```by (ALLGOALS Asm_simp_tac); ``` paulson@3234 ` 507` ```qed "diff_mult_distrib" ; ``` paulson@3234 ` 508` wenzelm@5069 ` 509` ```Goal "!!m::nat. k * (m - n) = (k * m) - (k * n)"; ``` paulson@3234 ` 510` ```val mult_commute_k = read_instantiate [("m","k")] mult_commute; ``` wenzelm@4089 ` 511` ```by (simp_tac (simpset() addsimps [diff_mult_distrib, mult_commute_k]) 1); ``` paulson@3234 ` 512` ```qed "diff_mult_distrib2" ; ``` paulson@3234 ` 513` ```(*NOT added as rewrites, since sometimes they are used from right-to-left*) ``` paulson@3234 ` 514` paulson@3234 ` 515` paulson@1713 ` 516` ```(*** Monotonicity of Multiplication ***) ``` paulson@1713 ` 517` paulson@5429 ` 518` ```Goal "i <= (j::nat) ==> i*k<=j*k"; ``` paulson@3339 ` 519` ```by (induct_tac "k" 1); ``` wenzelm@4089 ` 520` ```by (ALLGOALS (asm_simp_tac (simpset() addsimps [add_le_mono]))); ``` paulson@1713 ` 521` ```qed "mult_le_mono1"; ``` paulson@1713 ` 522` paulson@6987 ` 523` ```Goal "i <= (j::nat) ==> k*i <= k*j"; ``` paulson@6987 ` 524` ```by (dtac mult_le_mono1 1); ``` paulson@6987 ` 525` ```by (asm_simp_tac (simpset() addsimps [mult_commute]) 1); ``` paulson@6987 ` 526` ```qed "mult_le_mono2"; ``` paulson@6987 ` 527` paulson@6987 ` 528` ```(* <= monotonicity, BOTH arguments*) ``` paulson@5429 ` 529` ```Goal "[| i <= (j::nat); k <= l |] ==> i*k <= j*l"; ``` paulson@2007 ` 530` ```by (etac (mult_le_mono1 RS le_trans) 1); ``` paulson@6987 ` 531` ```by (etac mult_le_mono2 1); ``` paulson@1713 ` 532` ```qed "mult_le_mono"; ``` paulson@1713 ` 533` paulson@1713 ` 534` ```(*strict, in 1st argument; proof is by induction on k>0*) ``` paulson@5429 ` 535` ```Goal "[| i k*i < k*j"; ``` paulson@5078 ` 536` ```by (eres_inst_tac [("m1","0")] (less_eq_Suc_add RS exE) 1); ``` paulson@1713 ` 537` ```by (Asm_simp_tac 1); ``` paulson@3339 ` 538` ```by (induct_tac "x" 1); ``` wenzelm@4089 ` 539` ```by (ALLGOALS (asm_simp_tac (simpset() addsimps [add_less_mono]))); ``` paulson@1713 ` 540` ```qed "mult_less_mono2"; ``` paulson@1713 ` 541` paulson@5429 ` 542` ```Goal "[| i i*k < j*k"; ``` paulson@3457 ` 543` ```by (dtac mult_less_mono2 1); ``` wenzelm@4089 ` 544` ```by (ALLGOALS (asm_full_simp_tac (simpset() addsimps [mult_commute]))); ``` paulson@3234 ` 545` ```qed "mult_less_mono1"; ``` paulson@3234 ` 546` wenzelm@5069 ` 547` ```Goal "(0 < m*n) = (0 (m*k < n*k) = (m (k*m < k*n) = (m (m*k <= n*k) = (m<=n)"; ``` paulson@6864 ` 576` ```by (asm_full_simp_tac (simpset() addsimps [linorder_not_less RS sym]) 1); ``` paulson@6864 ` 577` ```qed "mult_le_cancel2"; ``` paulson@6864 ` 578` paulson@6864 ` 579` ```Goal "0 (k*m <= k*n) = (m<=n)"; ``` paulson@6864 ` 580` ```by (asm_full_simp_tac (simpset() addsimps [linorder_not_less RS sym]) 1); ``` paulson@6864 ` 581` ```qed "mult_le_cancel1"; ``` paulson@6864 ` 582` ```Addsimps [mult_le_cancel1, mult_le_cancel2]; ``` paulson@6864 ` 583` wenzelm@5069 ` 584` ```Goal "(Suc k * m < Suc k * n) = (m < n)"; ``` wenzelm@4423 ` 585` ```by (rtac mult_less_cancel1 1); ``` wenzelm@4297 ` 586` ```by (Simp_tac 1); ``` wenzelm@4297 ` 587` ```qed "Suc_mult_less_cancel1"; ``` wenzelm@4297 ` 588` wenzelm@5069 ` 589` ```Goalw [le_def] "(Suc k * m <= Suc k * n) = (m <= n)"; ``` wenzelm@4297 ` 590` ```by (simp_tac (simpset_of HOL.thy) 1); ``` wenzelm@4423 ` 591` ```by (rtac Suc_mult_less_cancel1 1); ``` wenzelm@4297 ` 592` ```qed "Suc_mult_le_cancel1"; ``` wenzelm@4297 ` 593` paulson@5143 ` 594` ```Goal "0 (m*k = n*k) = (m=n)"; ``` paulson@3234 ` 595` ```by (cut_facts_tac [less_linear] 1); ``` paulson@3724 ` 596` ```by Safe_tac; ``` paulson@3457 ` 597` ```by (assume_tac 2); ``` paulson@3234 ` 598` ```by (ALLGOALS (dtac mult_less_mono1 THEN' assume_tac)); ``` paulson@3234 ` 599` ```by (ALLGOALS Asm_full_simp_tac); ``` paulson@3234 ` 600` ```qed "mult_cancel2"; ``` paulson@3234 ` 601` paulson@5143 ` 602` ```Goal "0 (k*m = k*n) = (m=n)"; ``` paulson@3457 ` 603` ```by (dtac mult_cancel2 1); ``` wenzelm@4089 ` 604` ```by (asm_full_simp_tac (simpset() addsimps [mult_commute]) 1); ``` paulson@3234 ` 605` ```qed "mult_cancel1"; ``` paulson@3234 ` 606` ```Addsimps [mult_cancel1, mult_cancel2]; ``` paulson@3234 ` 607` wenzelm@5069 ` 608` ```Goal "(Suc k * m = Suc k * n) = (m = n)"; ``` wenzelm@4423 ` 609` ```by (rtac mult_cancel1 1); ``` wenzelm@4297 ` 610` ```by (Simp_tac 1); ``` wenzelm@4297 ` 611` ```qed "Suc_mult_cancel1"; ``` wenzelm@4297 ` 612` paulson@3234 ` 613` paulson@1795 ` 614` ```(** Lemma for gcd **) ``` paulson@1795 ` 615` paulson@5143 ` 616` ```Goal "m = m*n ==> n=1 | m=0"; ``` paulson@1795 ` 617` ```by (dtac sym 1); ``` paulson@1795 ` 618` ```by (rtac disjCI 1); ``` paulson@1795 ` 619` ```by (rtac nat_less_cases 1 THEN assume_tac 2); ``` wenzelm@4089 ` 620` ```by (fast_tac (claset() addSEs [less_SucE] addss simpset()) 1); ``` nipkow@4356 ` 621` ```by (best_tac (claset() addDs [mult_less_mono2] addss simpset()) 1); ``` paulson@1795 ` 622` ```qed "mult_eq_self_implies_10"; ``` paulson@1795 ` 623` paulson@1795 ` 624` nipkow@5983 ` 625` nipkow@5983 ` 626` nipkow@5983 ` 627` ```(*---------------------------------------------------------------------------*) ``` nipkow@5983 ` 628` ```(* Various arithmetic proof procedures *) ``` nipkow@5983 ` 629` ```(*---------------------------------------------------------------------------*) ``` nipkow@5983 ` 630` nipkow@5983 ` 631` ```(*---------------------------------------------------------------------------*) ``` nipkow@5983 ` 632` ```(* 1. Cancellation of common terms *) ``` nipkow@5983 ` 633` ```(*---------------------------------------------------------------------------*) ``` nipkow@5983 ` 634` nipkow@5983 ` 635` ```(* Title: HOL/arith_data.ML ``` nipkow@5983 ` 636` ``` ID: \$Id\$ ``` nipkow@5983 ` 637` ``` Author: Markus Wenzel and Stefan Berghofer, TU Muenchen ``` nipkow@5983 ` 638` nipkow@5983 ` 639` ```Setup various arithmetic proof procedures. ``` nipkow@5983 ` 640` ```*) ``` nipkow@5983 ` 641` nipkow@5983 ` 642` ```signature ARITH_DATA = ``` nipkow@5983 ` 643` ```sig ``` nipkow@6055 ` 644` ``` val nat_cancel_sums_add: simproc list ``` nipkow@5983 ` 645` ``` val nat_cancel_sums: simproc list ``` nipkow@5983 ` 646` ``` val nat_cancel_factor: simproc list ``` nipkow@5983 ` 647` ``` val nat_cancel: simproc list ``` nipkow@5983 ` 648` ```end; ``` nipkow@5983 ` 649` nipkow@5983 ` 650` ```structure ArithData: ARITH_DATA = ``` nipkow@5983 ` 651` ```struct ``` nipkow@5983 ` 652` nipkow@5983 ` 653` nipkow@5983 ` 654` ```(** abstract syntax of structure nat: 0, Suc, + **) ``` nipkow@5983 ` 655` nipkow@5983 ` 656` ```(* mk_sum, mk_norm_sum *) ``` nipkow@5983 ` 657` nipkow@5983 ` 658` ```val one = HOLogic.mk_nat 1; ``` nipkow@5983 ` 659` ```val mk_plus = HOLogic.mk_binop "op +"; ``` nipkow@5983 ` 660` nipkow@5983 ` 661` ```fun mk_sum [] = HOLogic.zero ``` nipkow@5983 ` 662` ``` | mk_sum [t] = t ``` nipkow@5983 ` 663` ``` | mk_sum (t :: ts) = mk_plus (t, mk_sum ts); ``` nipkow@5983 ` 664` nipkow@5983 ` 665` ```(*normal form of sums: Suc (... (Suc (a + (b + ...))))*) ``` nipkow@5983 ` 666` ```fun mk_norm_sum ts = ``` nipkow@5983 ` 667` ``` let val (ones, sums) = partition (equal one) ts in ``` nipkow@5983 ` 668` ``` funpow (length ones) HOLogic.mk_Suc (mk_sum sums) ``` nipkow@5983 ` 669` ``` end; ``` nipkow@5983 ` 670` nipkow@5983 ` 671` nipkow@5983 ` 672` ```(* dest_sum *) ``` nipkow@5983 ` 673` nipkow@5983 ` 674` ```val dest_plus = HOLogic.dest_bin "op +" HOLogic.natT; ``` nipkow@5983 ` 675` nipkow@5983 ` 676` ```fun dest_sum tm = ``` nipkow@5983 ` 677` ``` if HOLogic.is_zero tm then [] ``` nipkow@5983 ` 678` ``` else ``` nipkow@5983 ` 679` ``` (case try HOLogic.dest_Suc tm of ``` nipkow@5983 ` 680` ``` Some t => one :: dest_sum t ``` nipkow@5983 ` 681` ``` | None => ``` nipkow@5983 ` 682` ``` (case try dest_plus tm of ``` nipkow@5983 ` 683` ``` Some (t, u) => dest_sum t @ dest_sum u ``` nipkow@5983 ` 684` ``` | None => [tm])); ``` nipkow@5983 ` 685` nipkow@5983 ` 686` nipkow@5983 ` 687` ```(** generic proof tools **) ``` nipkow@5983 ` 688` nipkow@5983 ` 689` ```(* prove conversions *) ``` nipkow@5983 ` 690` nipkow@5983 ` 691` ```val mk_eqv = HOLogic.mk_Trueprop o HOLogic.mk_eq; ``` nipkow@5983 ` 692` nipkow@5983 ` 693` ```fun prove_conv expand_tac norm_tac sg (t, u) = ``` nipkow@5983 ` 694` ``` mk_meta_eq (prove_goalw_cterm_nocheck [] (cterm_of sg (mk_eqv (t, u))) ``` nipkow@5983 ` 695` ``` (K [expand_tac, norm_tac])) ``` nipkow@5983 ` 696` ``` handle ERROR => error ("The error(s) above occurred while trying to prove " ^ ``` nipkow@5983 ` 697` ``` (string_of_cterm (cterm_of sg (mk_eqv (t, u))))); ``` nipkow@5983 ` 698` nipkow@5983 ` 699` ```val subst_equals = prove_goal HOL.thy "[| t = s; u = t |] ==> u = s" ``` nipkow@5983 ` 700` ``` (fn prems => [cut_facts_tac prems 1, SIMPSET' asm_simp_tac 1]); ``` nipkow@5983 ` 701` nipkow@5983 ` 702` nipkow@5983 ` 703` ```(* rewriting *) ``` nipkow@5983 ` 704` nipkow@5983 ` 705` ```fun simp_all rules = ALLGOALS (simp_tac (HOL_ss addsimps rules)); ``` nipkow@5983 ` 706` nipkow@5983 ` 707` ```val add_rules = [add_Suc, add_Suc_right, add_0, add_0_right]; ``` nipkow@5983 ` 708` ```val mult_rules = [mult_Suc, mult_Suc_right, mult_0, mult_0_right]; ``` nipkow@5983 ` 709` nipkow@5983 ` 710` nipkow@5983 ` 711` nipkow@5983 ` 712` ```(** cancel common summands **) ``` nipkow@5983 ` 713` nipkow@5983 ` 714` ```structure Sum = ``` nipkow@5983 ` 715` ```struct ``` nipkow@5983 ` 716` ``` val mk_sum = mk_norm_sum; ``` nipkow@5983 ` 717` ``` val dest_sum = dest_sum; ``` nipkow@5983 ` 718` ``` val prove_conv = prove_conv; ``` nipkow@5983 ` 719` ``` val norm_tac = simp_all add_rules THEN simp_all add_ac; ``` nipkow@5983 ` 720` ```end; ``` nipkow@5983 ` 721` nipkow@5983 ` 722` ```fun gen_uncancel_tac rule ct = ``` nipkow@5983 ` 723` ``` rtac (instantiate' [] [None, Some ct] (rule RS subst_equals)) 1; ``` nipkow@5983 ` 724` nipkow@5983 ` 725` nipkow@5983 ` 726` ```(* nat eq *) ``` nipkow@5983 ` 727` nipkow@5983 ` 728` ```structure EqCancelSums = CancelSumsFun ``` nipkow@5983 ` 729` ```(struct ``` nipkow@5983 ` 730` ``` open Sum; ``` nipkow@5983 ` 731` ``` val mk_bal = HOLogic.mk_eq; ``` nipkow@5983 ` 732` ``` val dest_bal = HOLogic.dest_bin "op =" HOLogic.natT; ``` nipkow@5983 ` 733` ``` val uncancel_tac = gen_uncancel_tac add_left_cancel; ``` nipkow@5983 ` 734` ```end); ``` nipkow@5983 ` 735` nipkow@5983 ` 736` nipkow@5983 ` 737` ```(* nat less *) ``` nipkow@5983 ` 738` nipkow@5983 ` 739` ```structure LessCancelSums = CancelSumsFun ``` nipkow@5983 ` 740` ```(struct ``` nipkow@5983 ` 741` ``` open Sum; ``` nipkow@5983 ` 742` ``` val mk_bal = HOLogic.mk_binrel "op <"; ``` nipkow@5983 ` 743` ``` val dest_bal = HOLogic.dest_bin "op <" HOLogic.natT; ``` nipkow@5983 ` 744` ``` val uncancel_tac = gen_uncancel_tac add_left_cancel_less; ``` nipkow@5983 ` 745` ```end); ``` nipkow@5983 ` 746` nipkow@5983 ` 747` nipkow@5983 ` 748` ```(* nat le *) ``` nipkow@5983 ` 749` nipkow@5983 ` 750` ```structure LeCancelSums = CancelSumsFun ``` nipkow@5983 ` 751` ```(struct ``` nipkow@5983 ` 752` ``` open Sum; ``` nipkow@5983 ` 753` ``` val mk_bal = HOLogic.mk_binrel "op <="; ``` nipkow@5983 ` 754` ``` val dest_bal = HOLogic.dest_bin "op <=" HOLogic.natT; ``` nipkow@5983 ` 755` ``` val uncancel_tac = gen_uncancel_tac add_left_cancel_le; ``` nipkow@5983 ` 756` ```end); ``` nipkow@5983 ` 757` nipkow@5983 ` 758` nipkow@5983 ` 759` ```(* nat diff *) ``` nipkow@5983 ` 760` nipkow@5983 ` 761` ```structure DiffCancelSums = CancelSumsFun ``` nipkow@5983 ` 762` ```(struct ``` nipkow@5983 ` 763` ``` open Sum; ``` nipkow@5983 ` 764` ``` val mk_bal = HOLogic.mk_binop "op -"; ``` nipkow@5983 ` 765` ``` val dest_bal = HOLogic.dest_bin "op -" HOLogic.natT; ``` nipkow@5983 ` 766` ``` val uncancel_tac = gen_uncancel_tac diff_cancel; ``` nipkow@5983 ` 767` ```end); ``` nipkow@5983 ` 768` nipkow@5983 ` 769` nipkow@5983 ` 770` nipkow@5983 ` 771` ```(** cancel common factor **) ``` nipkow@5983 ` 772` nipkow@5983 ` 773` ```structure Factor = ``` nipkow@5983 ` 774` ```struct ``` nipkow@5983 ` 775` ``` val mk_sum = mk_norm_sum; ``` nipkow@5983 ` 776` ``` val dest_sum = dest_sum; ``` nipkow@5983 ` 777` ``` val prove_conv = prove_conv; ``` nipkow@5983 ` 778` ``` val norm_tac = simp_all (add_rules @ mult_rules) THEN simp_all add_ac; ``` nipkow@5983 ` 779` ```end; ``` nipkow@5983 ` 780` wenzelm@6394 ` 781` ```fun mk_cnat n = cterm_of (Theory.sign_of Nat.thy) (HOLogic.mk_nat n); ``` nipkow@5983 ` 782` nipkow@5983 ` 783` ```fun gen_multiply_tac rule k = ``` nipkow@5983 ` 784` ``` if k > 0 then ``` nipkow@5983 ` 785` ``` rtac (instantiate' [] [None, Some (mk_cnat (k - 1))] (rule RS subst_equals)) 1 ``` nipkow@5983 ` 786` ``` else no_tac; ``` nipkow@5983 ` 787` nipkow@5983 ` 788` nipkow@5983 ` 789` ```(* nat eq *) ``` nipkow@5983 ` 790` nipkow@5983 ` 791` ```structure EqCancelFactor = CancelFactorFun ``` nipkow@5983 ` 792` ```(struct ``` nipkow@5983 ` 793` ``` open Factor; ``` nipkow@5983 ` 794` ``` val mk_bal = HOLogic.mk_eq; ``` nipkow@5983 ` 795` ``` val dest_bal = HOLogic.dest_bin "op =" HOLogic.natT; ``` nipkow@5983 ` 796` ``` val multiply_tac = gen_multiply_tac Suc_mult_cancel1; ``` nipkow@5983 ` 797` ```end); ``` nipkow@5983 ` 798` nipkow@5983 ` 799` nipkow@5983 ` 800` ```(* nat less *) ``` nipkow@5983 ` 801` nipkow@5983 ` 802` ```structure LessCancelFactor = CancelFactorFun ``` nipkow@5983 ` 803` ```(struct ``` nipkow@5983 ` 804` ``` open Factor; ``` nipkow@5983 ` 805` ``` val mk_bal = HOLogic.mk_binrel "op <"; ``` nipkow@5983 ` 806` ``` val dest_bal = HOLogic.dest_bin "op <" HOLogic.natT; ``` nipkow@5983 ` 807` ``` val multiply_tac = gen_multiply_tac Suc_mult_less_cancel1; ``` nipkow@5983 ` 808` ```end); ``` nipkow@5983 ` 809` nipkow@5983 ` 810` nipkow@5983 ` 811` ```(* nat le *) ``` nipkow@5983 ` 812` nipkow@5983 ` 813` ```structure LeCancelFactor = CancelFactorFun ``` nipkow@5983 ` 814` ```(struct ``` nipkow@5983 ` 815` ``` open Factor; ``` nipkow@5983 ` 816` ``` val mk_bal = HOLogic.mk_binrel "op <="; ``` nipkow@5983 ` 817` ``` val dest_bal = HOLogic.dest_bin "op <=" HOLogic.natT; ``` nipkow@5983 ` 818` ``` val multiply_tac = gen_multiply_tac Suc_mult_le_cancel1; ``` nipkow@5983 ` 819` ```end); ``` nipkow@5983 ` 820` nipkow@5983 ` 821` nipkow@5983 ` 822` nipkow@5983 ` 823` ```(** prepare nat_cancel simprocs **) ``` nipkow@5983 ` 824` wenzelm@6394 ` 825` ```fun prep_pat s = Thm.read_cterm (Theory.sign_of Arith.thy) (s, HOLogic.termTVar); ``` nipkow@5983 ` 826` ```val prep_pats = map prep_pat; ``` nipkow@5983 ` 827` nipkow@5983 ` 828` ```fun prep_simproc (name, pats, proc) = Simplifier.mk_simproc name pats proc; ``` nipkow@5983 ` 829` nipkow@5983 ` 830` ```val eq_pats = prep_pats ["(l::nat) + m = n", "(l::nat) = m + n", "Suc m = n", "m = Suc n"]; ``` nipkow@5983 ` 831` ```val less_pats = prep_pats ["(l::nat) + m < n", "(l::nat) < m + n", "Suc m < n", "m < Suc n"]; ``` nipkow@5983 ` 832` ```val le_pats = prep_pats ["(l::nat) + m <= n", "(l::nat) <= m + n", "Suc m <= n", "m <= Suc n"]; ``` nipkow@5983 ` 833` ```val diff_pats = prep_pats ["((l::nat) + m) - n", "(l::nat) - (m + n)", "Suc m - n", "m - Suc n"]; ``` nipkow@5983 ` 834` nipkow@6055 ` 835` ```val nat_cancel_sums_add = map prep_simproc ``` nipkow@5983 ` 836` ``` [("nateq_cancel_sums", eq_pats, EqCancelSums.proc), ``` nipkow@5983 ` 837` ``` ("natless_cancel_sums", less_pats, LessCancelSums.proc), ``` nipkow@6055 ` 838` ``` ("natle_cancel_sums", le_pats, LeCancelSums.proc)]; ``` nipkow@6055 ` 839` nipkow@6055 ` 840` ```val nat_cancel_sums = nat_cancel_sums_add @ ``` nipkow@6055 ` 841` ``` [prep_simproc("natdiff_cancel_sums", diff_pats, DiffCancelSums.proc)]; ``` nipkow@5983 ` 842` nipkow@5983 ` 843` ```val nat_cancel_factor = map prep_simproc ``` nipkow@5983 ` 844` ``` [("nateq_cancel_factor", eq_pats, EqCancelFactor.proc), ``` nipkow@5983 ` 845` ``` ("natless_cancel_factor", less_pats, LessCancelFactor.proc), ``` nipkow@5983 ` 846` ``` ("natle_cancel_factor", le_pats, LeCancelFactor.proc)]; ``` nipkow@5983 ` 847` nipkow@5983 ` 848` ```val nat_cancel = nat_cancel_factor @ nat_cancel_sums; ``` nipkow@5983 ` 849` nipkow@5983 ` 850` nipkow@5983 ` 851` ```end; ``` nipkow@5983 ` 852` nipkow@5983 ` 853` ```open ArithData; ``` nipkow@5983 ` 854` nipkow@5983 ` 855` ```Addsimprocs nat_cancel; ``` nipkow@5983 ` 856` nipkow@5983 ` 857` ```(*---------------------------------------------------------------------------*) ``` nipkow@5983 ` 858` ```(* 2. Linear arithmetic *) ``` nipkow@5983 ` 859` ```(*---------------------------------------------------------------------------*) ``` nipkow@5983 ` 860` nipkow@6101 ` 861` ```(* Parameters data for general linear arithmetic functor *) ``` nipkow@6101 ` 862` nipkow@6101 ` 863` ```structure LA_Logic: LIN_ARITH_LOGIC = ``` nipkow@5983 ` 864` ```struct ``` nipkow@5983 ` 865` ```val ccontr = ccontr; ``` nipkow@5983 ` 866` ```val conjI = conjI; ``` nipkow@6101 ` 867` ```val neqE = linorder_neqE; ``` nipkow@5983 ` 868` ```val notI = notI; ``` nipkow@5983 ` 869` ```val sym = sym; ``` nipkow@6109 ` 870` ```val not_lessD = linorder_not_less RS iffD1; ``` nipkow@6128 ` 871` ```val not_leD = linorder_not_le RS iffD1; ``` nipkow@5983 ` 872` nipkow@6128 ` 873` wenzelm@6968 ` 874` ```fun mk_Eq thm = (thm RS Eq_FalseI) handle THM _ => (thm RS Eq_TrueI); ``` nipkow@6128 ` 875` nipkow@6073 ` 876` ```val mk_Trueprop = HOLogic.mk_Trueprop; ``` nipkow@6073 ` 877` nipkow@6079 ` 878` ```fun neg_prop(TP\$(Const("Not",_)\$t)) = TP\$t ``` nipkow@6079 ` 879` ``` | neg_prop(TP\$t) = TP \$ (Const("Not",HOLogic.boolT-->HOLogic.boolT)\$t); ``` nipkow@6073 ` 880` nipkow@6101 ` 881` ```fun is_False thm = ``` nipkow@6101 ` 882` ``` let val _ \$ t = #prop(rep_thm thm) ``` nipkow@6101 ` 883` ``` in t = Const("False",HOLogic.boolT) end; ``` nipkow@6101 ` 884` nipkow@6128 ` 885` ```fun is_nat(t) = fastype_of1 t = HOLogic.natT; ``` nipkow@6128 ` 886` nipkow@6128 ` 887` ```fun mk_nat_thm sg t = ``` nipkow@6128 ` 888` ``` let val ct = cterm_of sg t and cn = cterm_of sg (Var(("n",0),HOLogic.natT)) ``` nipkow@6128 ` 889` ``` in instantiate ([],[(cn,ct)]) le0 end; ``` nipkow@6128 ` 890` nipkow@6101 ` 891` ```end; ``` nipkow@6101 ` 892` nipkow@7582 ` 893` ```signature LIN_ARITH_DATA2 = ``` nipkow@7582 ` 894` ```sig ``` nipkow@7582 ` 895` ``` include LIN_ARITH_DATA ``` nipkow@7582 ` 896` ``` val discrete: (string * bool)list ref ``` nipkow@7582 ` 897` ```end; ``` nipkow@5983 ` 898` nipkow@7582 ` 899` ```structure LA_Data_Ref: LIN_ARITH_DATA2 = ``` nipkow@7582 ` 900` ```struct ``` nipkow@7582 ` 901` ``` val add_mono_thms = ref ([]:thm list); ``` nipkow@7582 ` 902` ``` val lessD = ref ([]:thm list); ``` nipkow@7582 ` 903` ``` val ss_ref = ref HOL_basic_ss; ``` nipkow@7582 ` 904` ``` val discrete = ref ([]:(string*bool)list); ``` nipkow@5983 ` 905` nipkow@7548 ` 906` ```(* Decomposition of terms *) ``` nipkow@7548 ` 907` nipkow@7548 ` 908` ```fun nT (Type("fun",[N,_])) = N = HOLogic.natT ``` nipkow@7548 ` 909` ``` | nT _ = false; ``` nipkow@7548 ` 910` nipkow@7548 ` 911` ```fun add_atom(t,m,(p,i)) = (case assoc(p,t) of None => ((t,m)::p,i) ``` nipkow@7548 ` 912` ``` | Some n => (overwrite(p,(t,n+m:int)), i)); ``` nipkow@7548 ` 913` nipkow@7548 ` 914` ```(* Turn term into list of summand * multiplicity plus a constant *) ``` nipkow@7548 ` 915` ```fun poly(Const("op +",_) \$ s \$ t, m, pi) = poly(s,m,poly(t,m,pi)) ``` nipkow@7548 ` 916` ``` | poly(all as Const("op -",T) \$ s \$ t, m, pi) = ``` nipkow@7548 ` 917` ``` if nT T then add_atom(all,m,pi) ``` nipkow@7548 ` 918` ``` else poly(s,m,poly(t,~1*m,pi)) ``` nipkow@7548 ` 919` ``` | poly(Const("uminus",_) \$ t, m, pi) = poly(t,~1*m,pi) ``` nipkow@7548 ` 920` ``` | poly(Const("0",_), _, pi) = pi ``` nipkow@7548 ` 921` ``` | poly(Const("Suc",_)\$t, m, (p,i)) = poly(t, m, (p,i+m)) ``` nipkow@7548 ` 922` ``` | poly(all as Const("op *",_) \$ (Const("Numeral.number_of",_)\$c) \$ t, m, pi)= ``` nipkow@7548 ` 923` ``` (poly(t,m*HOLogic.dest_binum c,pi) ``` nipkow@7548 ` 924` ``` handle TERM _ => add_atom(all,m,pi)) ``` nipkow@7548 ` 925` ``` | poly(all as Const("op *",_) \$ t \$ (Const("Numeral.number_of",_)\$c), m, pi)= ``` nipkow@7548 ` 926` ``` (poly(t,m*HOLogic.dest_binum c,pi) ``` nipkow@7548 ` 927` ``` handle TERM _ => add_atom(all,m,pi)) ``` nipkow@7548 ` 928` ``` | poly(all as Const("Numeral.number_of",_)\$t,m,(p,i)) = ``` nipkow@7548 ` 929` ``` ((p,i + m*HOLogic.dest_binum t) ``` nipkow@7548 ` 930` ``` handle TERM _ => add_atom(all,m,(p,i))) ``` nipkow@7548 ` 931` ``` | poly x = add_atom x; ``` nipkow@7548 ` 932` nipkow@7548 ` 933` ```fun decomp2(rel,lhs,rhs) = ``` nipkow@7548 ` 934` ``` let val (p,i) = poly(lhs,1,([],0)) and (q,j) = poly(rhs,1,([],0)) ``` nipkow@7548 ` 935` ``` in case rel of ``` nipkow@7548 ` 936` ``` "op <" => Some(p,i,"<",q,j) ``` nipkow@7548 ` 937` ``` | "op <=" => Some(p,i,"<=",q,j) ``` nipkow@7548 ` 938` ``` | "op =" => Some(p,i,"=",q,j) ``` nipkow@7548 ` 939` ``` | _ => None ``` nipkow@7548 ` 940` ``` end; ``` nipkow@7548 ` 941` nipkow@7548 ` 942` ```fun negate(Some(x,i,rel,y,j,d)) = Some(x,i,"~"^rel,y,j,d) ``` nipkow@7548 ` 943` ``` | negate None = None; ``` nipkow@7548 ` 944` nipkow@7582 ` 945` ```fun decomp1 (T,xxx) = ``` nipkow@7548 ` 946` ``` (case T of ``` nipkow@7548 ` 947` ``` Type("fun",[Type(D,[]),_]) => ``` nipkow@7582 ` 948` ``` (case assoc(!discrete,D) of ``` nipkow@7548 ` 949` ``` None => None ``` nipkow@7548 ` 950` ``` | Some d => (case decomp2 xxx of ``` nipkow@7548 ` 951` ``` None => None ``` nipkow@7548 ` 952` ``` | Some(p,i,rel,q,j) => Some(p,i,rel,q,j,d))) ``` nipkow@7548 ` 953` ``` | _ => None); ``` nipkow@7548 ` 954` nipkow@7582 ` 955` ```fun decomp (_\$(Const(rel,T)\$lhs\$rhs)) = decomp1 (T,(rel,lhs,rhs)) ``` nipkow@7582 ` 956` ``` | decomp (_\$(Const("Not",_)\$(Const(rel,T)\$lhs\$rhs))) = ``` nipkow@7582 ` 957` ``` negate(decomp1 (T,(rel,lhs,rhs))) ``` nipkow@7582 ` 958` ``` | decomp _ = None ``` nipkow@6128 ` 959` ```end; ``` nipkow@6055 ` 960` nipkow@7582 ` 961` ```let ``` nipkow@7582 ` 962` nipkow@7582 ` 963` ```(* reduce contradictory <= to False. ``` nipkow@7582 ` 964` ``` Most of the work is done by the cancel tactics. ``` nipkow@7582 ` 965` ```*) ``` nipkow@7582 ` 966` ```val add_rules = [add_0,add_0_right,Zero_not_Suc,Suc_not_Zero,le_0_eq]; ``` nipkow@7582 ` 967` nipkow@7582 ` 968` ```val add_mono_thms = map (fn s => prove_goal Arith.thy s ``` nipkow@7582 ` 969` ``` (fn prems => [cut_facts_tac prems 1, ``` nipkow@7582 ` 970` ``` blast_tac (claset() addIs [add_le_mono]) 1])) ``` nipkow@7582 ` 971` ```["(i <= j) & (k <= l) ==> i + k <= j + (l::nat)", ``` nipkow@7582 ` 972` ``` "(i = j) & (k <= l) ==> i + k <= j + (l::nat)", ``` nipkow@7582 ` 973` ``` "(i <= j) & (k = l) ==> i + k <= j + (l::nat)", ``` nipkow@7582 ` 974` ``` "(i = j) & (k = l) ==> i + k = j + (l::nat)" ``` nipkow@7582 ` 975` ```]; ``` nipkow@7582 ` 976` nipkow@7582 ` 977` ```in ``` nipkow@7582 ` 978` ```LA_Data_Ref.add_mono_thms := !LA_Data_Ref.add_mono_thms @ add_mono_thms; ``` nipkow@7582 ` 979` ```LA_Data_Ref.lessD := !LA_Data_Ref.lessD @ [Suc_leI]; ``` nipkow@7582 ` 980` ```LA_Data_Ref.ss_ref := !LA_Data_Ref.ss_ref addsimps add_rules ``` nipkow@7582 ` 981` ``` addsimprocs nat_cancel_sums_add; ``` nipkow@7582 ` 982` ```LA_Data_Ref.discrete := !LA_Data_Ref.discrete @ [("nat",true)] ``` nipkow@5983 ` 983` ```end; ``` nipkow@5983 ` 984` nipkow@6128 ` 985` ```structure Fast_Arith = ``` nipkow@6128 ` 986` ``` Fast_Lin_Arith(structure LA_Logic=LA_Logic and LA_Data=LA_Data_Ref); ``` nipkow@5983 ` 987` nipkow@6128 ` 988` ```val fast_arith_tac = Fast_Arith.lin_arith_tac; ``` nipkow@6073 ` 989` nipkow@7582 ` 990` ```let ``` nipkow@6128 ` 991` ```val nat_arith_simproc_pats = ``` wenzelm@6394 ` 992` ``` map (fn s => Thm.read_cterm (Theory.sign_of Arith.thy) (s, HOLogic.boolT)) ``` nipkow@6128 ` 993` ``` ["(m::nat) < n","(m::nat) <= n", "(m::nat) = n"]; ``` nipkow@5983 ` 994` nipkow@7582 ` 995` ```val fast_nat_arith_simproc = mk_simproc ``` nipkow@7582 ` 996` ``` "fast_nat_arith" nat_arith_simproc_pats Fast_Arith.lin_arith_prover; ``` nipkow@7582 ` 997` ```in ``` nipkow@7582 ` 998` ```Addsimprocs [fast_nat_arith_simproc] ``` nipkow@7582 ` 999` ```end; ``` nipkow@6073 ` 1000` nipkow@6073 ` 1001` ```(* Because of fast_nat_arith_simproc, the arithmetic solver is really only ``` nipkow@6073 ` 1002` ```useful to detect inconsistencies among the premises for subgoals which are ``` nipkow@6073 ` 1003` ```*not* themselves (in)equalities, because the latter activate ``` nipkow@6073 ` 1004` ```fast_nat_arith_simproc anyway. However, it seems cheaper to activate the ``` nipkow@6073 ` 1005` ```solver all the time rather than add the additional check. *) ``` nipkow@6073 ` 1006` nipkow@7570 ` 1007` ```simpset_ref () := (simpset() addSolver ``` nipkow@7570 ` 1008` ``` (mk_solver "lin. arith." Fast_Arith.cut_lin_arith_tac)); ``` nipkow@6055 ` 1009` nipkow@6055 ` 1010` ```(* Elimination of `-' on nat due to John Harrison *) ``` nipkow@6055 ` 1011` ```Goal "P(a - b::nat) = (!d. (b = a + d --> P 0) & (a = b + d --> P d))"; ``` paulson@6301 ` 1012` ```by (case_tac "a <= b" 1); ``` paulson@7059 ` 1013` ```by Auto_tac; ``` paulson@6301 ` 1014` ```by (eres_inst_tac [("x","b-a")] allE 1); ``` paulson@7059 ` 1015` ```by (asm_simp_tac (simpset() addsimps [diff_is_0_eq RS iffD2]) 1); ``` nipkow@6055 ` 1016` ```qed "nat_diff_split"; ``` nipkow@6055 ` 1017` nipkow@6055 ` 1018` ```(* FIXME: K true should be replaced by a sensible test to speed things up ``` nipkow@6157 ` 1019` ``` in case there are lots of irrelevant terms involved; ``` nipkow@6157 ` 1020` ``` elimination of min/max can be optimized: ``` nipkow@6157 ` 1021` ``` (max m n + k <= r) = (m+k <= r & n+k <= r) ``` nipkow@6157 ` 1022` ``` (l <= min m n + k) = (l <= m+k & l <= n+k) ``` nipkow@6055 ` 1023` ```*) ``` nipkow@7582 ` 1024` ```val arith_tac_split_thms = ref [nat_diff_split,split_min,split_max]; ``` nipkow@7582 ` 1025` ```fun arith_tac i = ``` nipkow@7582 ` 1026` ``` refute_tac (K true) (REPEAT o split_tac (!arith_tac_split_thms)) ``` nipkow@7582 ` 1027` ``` ((REPEAT_DETERM o etac linorder_neqE) THEN' fast_arith_tac) i; ``` nipkow@6055 ` 1028` wenzelm@7131 ` 1029` wenzelm@7131 ` 1030` ```(* proof method setup *) ``` wenzelm@7131 ` 1031` wenzelm@7428 ` 1032` ```val arith_method = ``` wenzelm@7428 ` 1033` ``` Method.METHOD (fn facts => FIRSTGOAL (Method.insert_tac facts THEN' arith_tac)); ``` wenzelm@7131 ` 1034` wenzelm@7131 ` 1035` ```val arith_setup = ``` wenzelm@7131 ` 1036` ``` [Method.add_methods ``` wenzelm@7131 ` 1037` ``` [("arith", Method.no_args arith_method, "decide linear arithmethic")]]; ``` wenzelm@7131 ` 1038` nipkow@5983 ` 1039` ```(*---------------------------------------------------------------------------*) ``` nipkow@5983 ` 1040` ```(* End of proof procedures. Now go and USE them! *) ``` nipkow@5983 ` 1041` ```(*---------------------------------------------------------------------------*) ``` nipkow@5983 ` 1042` paulson@4736 ` 1043` ```(*** Subtraction laws -- mostly from Clemens Ballarin ***) ``` paulson@3234 ` 1044` paulson@5429 ` 1045` ```Goal "[| a < (b::nat); c <= a |] ==> a-c < b-c"; ``` paulson@6301 ` 1046` ```by (arith_tac 1); ``` paulson@3234 ` 1047` ```qed "diff_less_mono"; ``` paulson@3234 ` 1048` paulson@5429 ` 1049` ```Goal "a+b < (c::nat) ==> a < c-b"; ``` paulson@6301 ` 1050` ```by (arith_tac 1); ``` paulson@3234 ` 1051` ```qed "add_less_imp_less_diff"; ``` paulson@3234 ` 1052` nipkow@5427 ` 1053` ```Goal "(i < j-k) = (i+k < (j::nat))"; ``` paulson@6301 ` 1054` ```by (arith_tac 1); ``` nipkow@5427 ` 1055` ```qed "less_diff_conv"; ``` nipkow@5427 ` 1056` paulson@5497 ` 1057` ```Goal "(j-k <= (i::nat)) = (j <= i+k)"; ``` paulson@6301 ` 1058` ```by (arith_tac 1); ``` paulson@5485 ` 1059` ```qed "le_diff_conv"; ``` paulson@5485 ` 1060` paulson@5497 ` 1061` ```Goal "k <= j ==> (i <= j-k) = (i+k <= (j::nat))"; ``` paulson@6301 ` 1062` ```by (arith_tac 1); ``` paulson@5497 ` 1063` ```qed "le_diff_conv2"; ``` paulson@5497 ` 1064` paulson@5143 ` 1065` ```Goal "Suc i <= n ==> Suc (n - Suc i) = n - i"; ``` paulson@6301 ` 1066` ```by (arith_tac 1); ``` paulson@3234 ` 1067` ```qed "Suc_diff_Suc"; ``` paulson@3234 ` 1068` paulson@5429 ` 1069` ```Goal "i <= (n::nat) ==> n - (n - i) = i"; ``` paulson@6301 ` 1070` ```by (arith_tac 1); ``` paulson@3234 ` 1071` ```qed "diff_diff_cancel"; ``` paulson@3381 ` 1072` ```Addsimps [diff_diff_cancel]; ``` paulson@3234 ` 1073` paulson@5429 ` 1074` ```Goal "k <= (n::nat) ==> m <= n + m - k"; ``` paulson@6301 ` 1075` ```by (arith_tac 1); ``` paulson@3234 ` 1076` ```qed "le_add_diff"; ``` paulson@3234 ` 1077` nipkow@6055 ` 1078` ```Goal "[| 0 j+k-i < k"; ``` paulson@6301 ` 1079` ```by (arith_tac 1); ``` nipkow@6055 ` 1080` ```qed "add_diff_less"; ``` paulson@3234 ` 1081` paulson@5356 ` 1082` ```Goal "m-1 < n ==> m <= n"; ``` paulson@6301 ` 1083` ```by (arith_tac 1); ``` paulson@5356 ` 1084` ```qed "pred_less_imp_le"; ``` paulson@5356 ` 1085` paulson@5356 ` 1086` ```Goal "j<=i ==> i - j < Suc i - j"; ``` paulson@6301 ` 1087` ```by (arith_tac 1); ``` paulson@5356 ` 1088` ```qed "diff_less_Suc_diff"; ``` paulson@5356 ` 1089` paulson@5356 ` 1090` ```Goal "i - j <= Suc i - j"; ``` paulson@6301 ` 1091` ```by (arith_tac 1); ``` paulson@5356 ` 1092` ```qed "diff_le_Suc_diff"; ``` paulson@5356 ` 1093` ```AddIffs [diff_le_Suc_diff]; ``` paulson@5356 ` 1094` paulson@5356 ` 1095` ```Goal "n - Suc i <= n - i"; ``` paulson@6301 ` 1096` ```by (arith_tac 1); ``` paulson@5356 ` 1097` ```qed "diff_Suc_le_diff"; ``` paulson@5356 ` 1098` ```AddIffs [diff_Suc_le_diff]; ``` paulson@5356 ` 1099` paulson@5409 ` 1100` ```Goal "0 < n ==> (m <= n-1) = (m (m-1 < n) = (m<=n)"; ``` paulson@6301 ` 1105` ```by (arith_tac 1); ``` paulson@5409 ` 1106` ```qed "less_pred_eq"; ``` paulson@5409 ` 1107` paulson@7059 ` 1108` ```(*Replaces the previous diff_less and le_diff_less, which had the stronger ``` paulson@7059 ` 1109` ``` second premise n<=m*) ``` paulson@7059 ` 1110` ```Goal "[| 0 m - n < m"; ``` paulson@6301 ` 1111` ```by (arith_tac 1); ``` paulson@5414 ` 1112` ```qed "diff_less"; ``` paulson@5414 ` 1113` paulson@4732 ` 1114` paulson@7128 ` 1115` ```(*** Reducting subtraction to addition ***) ``` paulson@7128 ` 1116` paulson@7128 ` 1117` ```(*Intended for use with linear arithmetic, but useful in its own right*) ``` paulson@7128 ` 1118` ```Goal "P (x-y) = (ALL z. (x P 0) & (x = y+z --> P z))"; ``` paulson@7128 ` 1119` ```by (case_tac "x Suc l - n + m = Suc (l - n + m)"; ``` paulson@7128 ` 1132` ```by (simp_tac remove_diff_ss 1); ``` paulson@7128 ` 1133` ```qed_spec_mp "Suc_diff_add_le"; ``` paulson@7128 ` 1134` paulson@7128 ` 1135` ```Goal "i n - Suc i < n - i"; ``` paulson@7128 ` 1136` ```by (asm_simp_tac remove_diff_ss 1); ``` paulson@7128 ` 1137` ```qed "diff_Suc_less_diff"; ``` paulson@7128 ` 1138` paulson@7128 ` 1139` ```Goal "Suc(m)-n = (if m (m-k) - (n-k) = m-(n::nat)"; ``` paulson@7128 ` 1148` ```by (asm_simp_tac remove_diff_ss 1); ``` paulson@7128 ` 1149` ```qed "diff_right_cancel"; ``` paulson@7128 ` 1150` paulson@7128 ` 1151` wenzelm@7108 ` 1152` ```(** (Anti)Monotonicity of subtraction -- by Stephan Merz **) ``` nipkow@3484 ` 1153` nipkow@3484 ` 1154` ```(* Monotonicity of subtraction in first argument *) ``` nipkow@6055 ` 1155` ```Goal "m <= (n::nat) ==> (m-l) <= (n-l)"; ``` paulson@7128 ` 1156` ```by (asm_simp_tac remove_diff_ss 1); ``` nipkow@6055 ` 1157` ```qed "diff_le_mono"; ``` nipkow@3484 ` 1158` paulson@5429 ` 1159` ```Goal "m <= (n::nat) ==> (l-n) <= (l-m)"; ``` paulson@7128 ` 1160` ```by (asm_simp_tac remove_diff_ss 1); ``` nipkow@6055 ` 1161` ```qed "diff_le_mono2"; ``` nipkow@5983 ` 1162` nipkow@5983 ` 1163` ```(*This proof requires natdiff_cancel_sums*) ``` nipkow@6055 ` 1164` ```Goal "[| m < (n::nat); m (l-n) < (l-m)"; ``` paulson@7128 ` 1165` ```by (asm_simp_tac remove_diff_ss 1); ``` nipkow@6055 ` 1166` ```qed "diff_less_mono2"; ``` nipkow@5983 ` 1167` nipkow@6055 ` 1168` ```Goal "[| m-n = 0; n-m = 0 |] ==> m=n"; ``` paulson@7128 ` 1169` ```by (asm_full_simp_tac remove_diff_ss 1); ``` nipkow@6055 ` 1170` ```qed "diffs0_imp_equal"; ```