src/HOL/Arith.ML
 author nipkow Fri Sep 04 11:01:59 1998 +0200 (1998-09-04) changeset 5427 26c9a7c0b36b parent 5414 8a458866637c child 5429 0833486c23ce permissions -rw-r--r--
Arith: less_diff_conv
List: [i..j]
 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` clasohm@923 ` 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` wenzelm@5069 ` 66` ```Goal "!!k::nat. (k + m = k + n) = (m=n)"; ``` 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` wenzelm@5069 ` 72` ```Goal "!!k::nat. (m + k = n + k) = (m=n)"; ``` 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` wenzelm@5069 ` 78` ```Goal "!!k::nat. (k + m <= k + n) = (m<=n)"; ``` 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` wenzelm@5069 ` 84` ```Goal "!!k::nat. (k + m < k + n) = (m m+(n-(Suc k)) = (m+n)-(Suc k)" *) ``` paulson@5143 ` 114` ```Goal "0 m + (n-1) = (m+n)-1"; ``` nipkow@4360 ` 115` ```by (exhaust_tac "m" 1); ``` nipkow@4360 ` 116` ```by (ALLGOALS (asm_simp_tac (simpset() addsimps [diff_Suc] ``` berghofe@5183 ` 117` ``` addsplits [nat.split]))); ``` nipkow@1327 ` 118` ```qed "add_pred"; ``` nipkow@1327 ` 119` ```Addsimps [add_pred]; ``` nipkow@1327 ` 120` paulson@5078 ` 121` ```Goal "!!m::nat. m + n = m ==> n = 0"; ``` paulson@5078 ` 122` ```by (dtac (add_0_right RS ssubst) 1); ``` paulson@5078 ` 123` ```by (asm_full_simp_tac (simpset() addsimps [add_assoc] ``` paulson@5078 ` 124` ``` delsimps [add_0_right]) 1); ``` paulson@5078 ` 125` ```qed "add_eq_self_zero"; ``` paulson@5078 ` 126` paulson@1626 ` 127` clasohm@923 ` 128` ```(**** Additional theorems about "less than" ****) ``` clasohm@923 ` 129` paulson@5078 ` 130` ```(*Deleted less_natE; instead use less_eq_Suc_add RS exE*) ``` paulson@5143 ` 131` ```Goal "m (? k. n=Suc(m+k))"; ``` paulson@3339 ` 132` ```by (induct_tac "n" 1); ``` wenzelm@4089 ` 133` ```by (ALLGOALS (simp_tac (simpset() addsimps [less_Suc_eq]))); ``` wenzelm@4089 ` 134` ```by (blast_tac (claset() addSEs [less_SucE] ``` paulson@3339 ` 135` ``` addSIs [add_0_right RS sym, add_Suc_right RS sym]) 1); ``` nipkow@1485 ` 136` ```qed_spec_mp "less_eq_Suc_add"; ``` clasohm@923 ` 137` wenzelm@5069 ` 138` ```Goal "n <= ((m + n)::nat)"; ``` paulson@3339 ` 139` ```by (induct_tac "m" 1); ``` clasohm@1264 ` 140` ```by (ALLGOALS Simp_tac); ``` clasohm@923 ` 141` ```by (etac le_trans 1); ``` clasohm@923 ` 142` ```by (rtac (lessI RS less_imp_le) 1); ``` clasohm@923 ` 143` ```qed "le_add2"; ``` clasohm@923 ` 144` wenzelm@5069 ` 145` ```Goal "n <= ((n + m)::nat)"; ``` wenzelm@4089 ` 146` ```by (simp_tac (simpset() addsimps add_ac) 1); ``` clasohm@923 ` 147` ```by (rtac le_add2 1); ``` clasohm@923 ` 148` ```qed "le_add1"; ``` clasohm@923 ` 149` clasohm@923 ` 150` ```bind_thm ("less_add_Suc1", (lessI RS (le_add1 RS le_less_trans))); ``` clasohm@923 ` 151` ```bind_thm ("less_add_Suc2", (lessI RS (le_add2 RS le_less_trans))); ``` clasohm@923 ` 152` clasohm@923 ` 153` ```(*"i <= j ==> i <= j+m"*) ``` clasohm@923 ` 154` ```bind_thm ("trans_le_add1", le_add1 RSN (2,le_trans)); ``` clasohm@923 ` 155` clasohm@923 ` 156` ```(*"i <= j ==> i <= m+j"*) ``` clasohm@923 ` 157` ```bind_thm ("trans_le_add2", le_add2 RSN (2,le_trans)); ``` clasohm@923 ` 158` clasohm@923 ` 159` ```(*"i < j ==> i < j+m"*) ``` clasohm@923 ` 160` ```bind_thm ("trans_less_add1", le_add1 RSN (2,less_le_trans)); ``` clasohm@923 ` 161` clasohm@923 ` 162` ```(*"i < j ==> i < m+j"*) ``` clasohm@923 ` 163` ```bind_thm ("trans_less_add2", le_add2 RSN (2,less_le_trans)); ``` clasohm@923 ` 164` paulson@5143 ` 165` ```Goal "i+j < (k::nat) ==> i m<=(n::nat)"; ``` paulson@3339 ` 183` ```by (induct_tac "k" 1); ``` clasohm@1264 ` 184` ```by (ALLGOALS Asm_simp_tac); ``` wenzelm@4089 ` 185` ```by (blast_tac (claset() addDs [Suc_leD]) 1); ``` nipkow@1485 ` 186` ```qed_spec_mp "add_leD1"; ``` clasohm@923 ` 187` wenzelm@5069 ` 188` ```Goal "!!n::nat. m+k<=n ==> k<=n"; ``` wenzelm@4089 ` 189` ```by (full_simp_tac (simpset() addsimps [add_commute]) 1); ``` paulson@2498 ` 190` ```by (etac add_leD1 1); ``` paulson@2498 ` 191` ```qed_spec_mp "add_leD2"; ``` paulson@2498 ` 192` wenzelm@5069 ` 193` ```Goal "!!n::nat. m+k<=n ==> m<=n & k<=n"; ``` wenzelm@4089 ` 194` ```by (blast_tac (claset() addDs [add_leD1, add_leD2]) 1); ``` paulson@2498 ` 195` ```bind_thm ("add_leE", result() RS conjE); ``` paulson@2498 ` 196` wenzelm@5069 ` 197` ```Goal "!!k l::nat. [| k m i + k < j + k"; ``` paulson@3339 ` 211` ```by (induct_tac "k" 1); ``` clasohm@1264 ` 212` ```by (ALLGOALS Asm_simp_tac); ``` clasohm@923 ` 213` ```qed "add_less_mono1"; ``` clasohm@923 ` 214` clasohm@923 ` 215` ```(*strict, in both arguments*) ``` wenzelm@5069 ` 216` ```Goal "!!i j k::nat. [|i < j; k < l|] ==> i + k < j + l"; ``` clasohm@923 ` 217` ```by (rtac (add_less_mono1 RS less_trans) 1); ``` lcp@1198 ` 218` ```by (REPEAT (assume_tac 1)); ``` paulson@3339 ` 219` ```by (induct_tac "j" 1); ``` clasohm@1264 ` 220` ```by (ALLGOALS Asm_simp_tac); ``` clasohm@923 ` 221` ```qed "add_less_mono"; ``` clasohm@923 ` 222` clasohm@923 ` 223` ```(*A [clumsy] way of lifting < monotonicity to <= monotonicity *) ``` paulson@5316 ` 224` ```val [lt_mono,le] = Goal ``` clasohm@1465 ` 225` ``` "[| !!i j::nat. i f(i) < f(j); \ ``` clasohm@1465 ` 226` ```\ i <= j \ ``` clasohm@923 ` 227` ```\ |] ==> f(i) <= (f(j)::nat)"; ``` clasohm@923 ` 228` ```by (cut_facts_tac [le] 1); ``` wenzelm@4089 ` 229` ```by (asm_full_simp_tac (simpset() addsimps [le_eq_less_or_eq]) 1); ``` wenzelm@4089 ` 230` ```by (blast_tac (claset() addSIs [lt_mono]) 1); ``` clasohm@923 ` 231` ```qed "less_mono_imp_le_mono"; ``` clasohm@923 ` 232` clasohm@923 ` 233` ```(*non-strict, in 1st argument*) ``` wenzelm@5069 ` 234` ```Goal "!!i j k::nat. i<=j ==> i + k <= j + k"; ``` wenzelm@3842 ` 235` ```by (res_inst_tac [("f", "%j. j+k")] less_mono_imp_le_mono 1); ``` paulson@1552 ` 236` ```by (etac add_less_mono1 1); ``` clasohm@923 ` 237` ```by (assume_tac 1); ``` clasohm@923 ` 238` ```qed "add_le_mono1"; ``` clasohm@923 ` 239` clasohm@923 ` 240` ```(*non-strict, in both arguments*) ``` wenzelm@5069 ` 241` ```Goal "!!k l::nat. [|i<=j; k<=l |] ==> i + k <= j + l"; ``` clasohm@923 ` 242` ```by (etac (add_le_mono1 RS le_trans) 1); ``` wenzelm@4089 ` 243` ```by (simp_tac (simpset() addsimps [add_commute]) 1); ``` clasohm@923 ` 244` ```(*j moves to the end because it is free while k, l are bound*) ``` paulson@1552 ` 245` ```by (etac add_le_mono1 1); ``` clasohm@923 ` 246` ```qed "add_le_mono"; ``` paulson@1713 ` 247` paulson@3234 ` 248` paulson@3234 ` 249` ```(*** Multiplication ***) ``` paulson@3234 ` 250` paulson@3234 ` 251` ```(*right annihilation in product*) ``` paulson@4732 ` 252` ```qed_goal "mult_0_right" thy "m * 0 = 0" ``` paulson@3339 ` 253` ``` (fn _ => [induct_tac "m" 1, ALLGOALS Asm_simp_tac]); ``` paulson@3234 ` 254` paulson@3293 ` 255` ```(*right successor law for multiplication*) ``` paulson@4732 ` 256` ```qed_goal "mult_Suc_right" thy "m * Suc(n) = m + (m * n)" ``` paulson@3339 ` 257` ``` (fn _ => [induct_tac "m" 1, ``` wenzelm@4089 ` 258` ``` ALLGOALS(asm_simp_tac (simpset() addsimps add_ac))]); ``` paulson@3234 ` 259` paulson@3293 ` 260` ```Addsimps [mult_0_right, mult_Suc_right]; ``` paulson@3234 ` 261` wenzelm@5069 ` 262` ```Goal "1 * n = n"; ``` paulson@3234 ` 263` ```by (Asm_simp_tac 1); ``` paulson@3234 ` 264` ```qed "mult_1"; ``` paulson@3234 ` 265` wenzelm@5069 ` 266` ```Goal "n * 1 = n"; ``` paulson@3234 ` 267` ```by (Asm_simp_tac 1); ``` paulson@3234 ` 268` ```qed "mult_1_right"; ``` paulson@3234 ` 269` paulson@3234 ` 270` ```(*Commutative law for multiplication*) ``` paulson@4732 ` 271` ```qed_goal "mult_commute" thy "m * n = n * (m::nat)" ``` paulson@3339 ` 272` ``` (fn _ => [induct_tac "m" 1, ALLGOALS Asm_simp_tac]); ``` paulson@3234 ` 273` paulson@3234 ` 274` ```(*addition distributes over multiplication*) ``` paulson@4732 ` 275` ```qed_goal "add_mult_distrib" thy "(m + n)*k = (m*k) + ((n*k)::nat)" ``` paulson@3339 ` 276` ``` (fn _ => [induct_tac "m" 1, ``` wenzelm@4089 ` 277` ``` ALLGOALS(asm_simp_tac (simpset() addsimps add_ac))]); ``` paulson@3234 ` 278` paulson@4732 ` 279` ```qed_goal "add_mult_distrib2" thy "k*(m + n) = (k*m) + ((k*n)::nat)" ``` paulson@3339 ` 280` ``` (fn _ => [induct_tac "m" 1, ``` wenzelm@4089 ` 281` ``` ALLGOALS(asm_simp_tac (simpset() addsimps add_ac))]); ``` paulson@3234 ` 282` paulson@3234 ` 283` ```(*Associative law for multiplication*) ``` paulson@4732 ` 284` ```qed_goal "mult_assoc" thy "(m * n) * k = m * ((n * k)::nat)" ``` paulson@3339 ` 285` ``` (fn _ => [induct_tac "m" 1, ``` wenzelm@4089 ` 286` ``` ALLGOALS (asm_simp_tac (simpset() addsimps [add_mult_distrib]))]); ``` paulson@3234 ` 287` paulson@4732 ` 288` ```qed_goal "mult_left_commute" thy "x*(y*z) = y*((x*z)::nat)" ``` paulson@3234 ` 289` ``` (fn _ => [rtac trans 1, rtac mult_commute 1, rtac trans 1, ``` paulson@3234 ` 290` ``` rtac mult_assoc 1, rtac (mult_commute RS arg_cong) 1]); ``` paulson@3234 ` 291` paulson@3234 ` 292` ```val mult_ac = [mult_assoc,mult_commute,mult_left_commute]; ``` paulson@3234 ` 293` wenzelm@5069 ` 294` ```Goal "(m*n = 0) = (m=0 | n=0)"; ``` paulson@3339 ` 295` ```by (induct_tac "m" 1); ``` paulson@3339 ` 296` ```by (induct_tac "n" 2); ``` paulson@3293 ` 297` ```by (ALLGOALS Asm_simp_tac); ``` paulson@3293 ` 298` ```qed "mult_is_0"; ``` paulson@3293 ` 299` ```Addsimps [mult_is_0]; ``` paulson@3293 ` 300` wenzelm@5069 ` 301` ```Goal "!!m::nat. m <= m*m"; ``` paulson@4158 ` 302` ```by (induct_tac "m" 1); ``` paulson@4158 ` 303` ```by (ALLGOALS (asm_simp_tac (simpset() addsimps [add_assoc RS sym]))); ``` paulson@4158 ` 304` ```by (etac (le_add2 RSN (2,le_trans)) 1); ``` paulson@4158 ` 305` ```qed "le_square"; ``` paulson@4158 ` 306` paulson@3234 ` 307` paulson@3234 ` 308` ```(*** Difference ***) ``` paulson@3234 ` 309` paulson@3234 ` 310` paulson@4732 ` 311` ```qed_goal "diff_self_eq_0" thy "m - m = 0" ``` paulson@3339 ` 312` ``` (fn _ => [induct_tac "m" 1, ALLGOALS Asm_simp_tac]); ``` paulson@3234 ` 313` ```Addsimps [diff_self_eq_0]; ``` paulson@3234 ` 314` paulson@3234 ` 315` ```(*Addition is the inverse of subtraction: if n<=m then n+(m-n) = m. *) ``` wenzelm@5069 ` 316` ```Goal "~ m n+(m-n) = (m::nat)"; ``` paulson@3234 ` 317` ```by (res_inst_tac [("m","m"),("n","n")] diff_induct 1); ``` paulson@3352 ` 318` ```by (ALLGOALS Asm_simp_tac); ``` paulson@3381 ` 319` ```qed_spec_mp "add_diff_inverse"; ``` paulson@3381 ` 320` paulson@5143 ` 321` ```Goal "n<=m ==> n+(m-n) = (m::nat)"; ``` wenzelm@4089 ` 322` ```by (asm_simp_tac (simpset() addsimps [add_diff_inverse, not_less_iff_le]) 1); ``` paulson@3381 ` 323` ```qed "le_add_diff_inverse"; ``` paulson@3234 ` 324` paulson@5143 ` 325` ```Goal "n<=m ==> (m-n)+n = (m::nat)"; ``` wenzelm@4089 ` 326` ```by (asm_simp_tac (simpset() addsimps [le_add_diff_inverse, add_commute]) 1); ``` paulson@3381 ` 327` ```qed "le_add_diff_inverse2"; ``` paulson@3381 ` 328` paulson@3381 ` 329` ```Addsimps [le_add_diff_inverse, le_add_diff_inverse2]; ``` paulson@3234 ` 330` paulson@3234 ` 331` paulson@3234 ` 332` ```(*** More results about difference ***) ``` paulson@3234 ` 333` paulson@5414 ` 334` ```Goal "n <= m ==> Suc(m)-n = Suc(m-n)"; ``` paulson@5316 ` 335` ```by (etac rev_mp 1); ``` paulson@3352 ` 336` ```by (res_inst_tac [("m","m"),("n","n")] diff_induct 1); ``` paulson@3352 ` 337` ```by (ALLGOALS Asm_simp_tac); ``` paulson@5414 ` 338` ```qed "Suc_diff_le"; ``` paulson@3352 ` 339` wenzelm@5069 ` 340` ```Goal "m - n < Suc(m)"; ``` paulson@3234 ` 341` ```by (res_inst_tac [("m","m"),("n","n")] diff_induct 1); ``` paulson@3234 ` 342` ```by (etac less_SucE 3); ``` wenzelm@4089 ` 343` ```by (ALLGOALS (asm_simp_tac (simpset() addsimps [less_Suc_eq]))); ``` paulson@3234 ` 344` ```qed "diff_less_Suc"; ``` paulson@3234 ` 345` wenzelm@5069 ` 346` ```Goal "!!m::nat. m - n <= m"; ``` paulson@3234 ` 347` ```by (res_inst_tac [("m","m"), ("n","n")] diff_induct 1); ``` paulson@3234 ` 348` ```by (ALLGOALS Asm_simp_tac); ``` paulson@3234 ` 349` ```qed "diff_le_self"; ``` paulson@3903 ` 350` ```Addsimps [diff_le_self]; ``` paulson@3234 ` 351` paulson@4732 ` 352` ```(* j j-n < k *) ``` paulson@4732 ` 353` ```bind_thm ("less_imp_diff_less", diff_le_self RS le_less_trans); ``` paulson@4732 ` 354` wenzelm@5069 ` 355` ```Goal "!!i::nat. i-j-k = i - (j+k)"; ``` paulson@3352 ` 356` ```by (res_inst_tac [("m","i"),("n","j")] diff_induct 1); ``` paulson@3352 ` 357` ```by (ALLGOALS Asm_simp_tac); ``` paulson@3352 ` 358` ```qed "diff_diff_left"; ``` paulson@3352 ` 359` wenzelm@5069 ` 360` ```Goal "(Suc m - n) - Suc k = m - n - k"; ``` wenzelm@4423 ` 361` ```by (simp_tac (simpset() addsimps [diff_diff_left]) 1); ``` paulson@4736 ` 362` ```qed "Suc_diff_diff"; ``` paulson@4736 ` 363` ```Addsimps [Suc_diff_diff]; ``` nipkow@4360 ` 364` paulson@5143 ` 365` ```Goal "0 n - Suc i < n"; ``` berghofe@5183 ` 366` ```by (exhaust_tac "n" 1); ``` paulson@4732 ` 367` ```by Safe_tac; ``` paulson@4732 ` 368` ```by (asm_simp_tac (simpset() addsimps [le_eq_less_Suc RS sym]) 1); ``` paulson@4732 ` 369` ```qed "diff_Suc_less"; ``` paulson@4732 ` 370` ```Addsimps [diff_Suc_less]; ``` paulson@4732 ` 371` paulson@5329 ` 372` ```Goal "i n - Suc i < n - i"; ``` paulson@5329 ` 373` ```by (exhaust_tac "n" 1); ``` paulson@5329 ` 374` ```by Safe_tac; ``` paulson@5414 ` 375` ```by (asm_simp_tac (simpset() addsimps [le_eq_less_Suc, Suc_diff_le]) 1); ``` paulson@5329 ` 376` ```qed "diff_Suc_less_diff"; ``` paulson@5329 ` 377` paulson@5333 ` 378` ```Goal "m - n <= Suc m - n"; ``` paulson@4732 ` 379` ```by (res_inst_tac [("m","m"),("n","n")] diff_induct 1); ``` paulson@4732 ` 380` ```by (ALLGOALS Asm_simp_tac); ``` paulson@4732 ` 381` ```qed "diff_le_Suc_diff"; ``` paulson@4732 ` 382` wenzelm@3396 ` 383` ```(*This and the next few suggested by Florian Kammueller*) ``` wenzelm@5069 ` 384` ```Goal "!!i::nat. i-j-k = i-k-j"; ``` wenzelm@4089 ` 385` ```by (simp_tac (simpset() addsimps [diff_diff_left, add_commute]) 1); ``` paulson@3352 ` 386` ```qed "diff_commute"; ``` paulson@3352 ` 387` wenzelm@5069 ` 388` ```Goal "!!i j k:: nat. k<=j --> j<=i --> i - (j - k) = i - j + k"; ``` paulson@3352 ` 389` ```by (res_inst_tac [("m","i"),("n","j")] diff_induct 1); ``` paulson@3352 ` 390` ```by (ALLGOALS Asm_simp_tac); ``` paulson@5414 ` 391` ```by (asm_simp_tac (simpset() addsimps [Suc_diff_le, le_Suc_eq]) 1); ``` paulson@3352 ` 392` ```qed_spec_mp "diff_diff_right"; ``` paulson@3352 ` 393` wenzelm@5069 ` 394` ```Goal "!!i j k:: nat. k<=j --> (i + j) - k = i + (j - k)"; ``` paulson@3352 ` 395` ```by (res_inst_tac [("m","j"),("n","k")] diff_induct 1); ``` paulson@3352 ` 396` ```by (ALLGOALS Asm_simp_tac); ``` paulson@3352 ` 397` ```qed_spec_mp "diff_add_assoc"; ``` paulson@3352 ` 398` wenzelm@5069 ` 399` ```Goal "!!i j k:: nat. k<=j --> (j + i) - k = i + (j - k)"; ``` paulson@4732 ` 400` ```by (asm_simp_tac (simpset() addsimps [add_commute, diff_add_assoc]) 1); ``` paulson@4732 ` 401` ```qed_spec_mp "diff_add_assoc2"; ``` paulson@4732 ` 402` wenzelm@5069 ` 403` ```Goal "!!n::nat. (n+m) - n = m"; ``` paulson@3339 ` 404` ```by (induct_tac "n" 1); ``` paulson@3234 ` 405` ```by (ALLGOALS Asm_simp_tac); ``` paulson@3234 ` 406` ```qed "diff_add_inverse"; ``` paulson@3234 ` 407` ```Addsimps [diff_add_inverse]; ``` paulson@3234 ` 408` wenzelm@5069 ` 409` ```Goal "!!n::nat.(m+n) - n = m"; ``` wenzelm@4089 ` 410` ```by (simp_tac (simpset() addsimps [diff_add_assoc]) 1); ``` paulson@3234 ` 411` ```qed "diff_add_inverse2"; ``` paulson@3234 ` 412` ```Addsimps [diff_add_inverse2]; ``` paulson@3234 ` 413` wenzelm@5069 ` 414` ```Goal "!!i j k::nat. i<=j ==> (j-i=k) = (j=k+i)"; ``` paulson@3724 ` 415` ```by Safe_tac; ``` paulson@3381 ` 416` ```by (ALLGOALS Asm_simp_tac); ``` paulson@3366 ` 417` ```qed "le_imp_diff_is_add"; ``` paulson@3366 ` 418` paulson@5356 ` 419` ```Goal "(m-n = 0) = (m <= n)"; ``` paulson@3234 ` 420` ```by (res_inst_tac [("m","m"),("n","n")] diff_induct 1); ``` paulson@5356 ` 421` ```by (ALLGOALS (asm_simp_tac (simpset() addsimps [le_eq_less_Suc]))); ``` paulson@5356 ` 422` ```qed "diff_is_0_eq"; ``` paulson@5356 ` 423` ```Addsimps [diff_is_0_eq RS iffD2]; ``` paulson@3234 ` 424` paulson@5316 ` 425` ```Goal "m-n = 0 --> n-m = 0 --> m=n"; ``` paulson@3234 ` 426` ```by (res_inst_tac [("m","m"),("n","n")] diff_induct 1); ``` paulson@3234 ` 427` ```by (REPEAT(Simp_tac 1 THEN TRY(atac 1))); ``` paulson@3234 ` 428` ```qed_spec_mp "diffs0_imp_equal"; ``` paulson@3234 ` 429` paulson@5333 ` 430` ```Goal "(0 ? k. 0 (!n. P(Suc(n))--> P(n)) --> P(k-i)"; ``` paulson@3234 ` 450` ```by (res_inst_tac [("m","k"),("n","i")] diff_induct 1); ``` paulson@3718 ` 451` ```by (ALLGOALS (Clarify_tac THEN' Simp_tac THEN' TRY o Blast_tac)); ``` paulson@3234 ` 452` ```qed "zero_induct_lemma"; ``` paulson@3234 ` 453` paulson@5316 ` 454` ```val prems = Goal "[| P(k); !!n. P(Suc(n)) ==> P(n) |] ==> P(0)"; ``` paulson@3234 ` 455` ```by (rtac (diff_self_eq_0 RS subst) 1); ``` paulson@3234 ` 456` ```by (rtac (zero_induct_lemma RS mp RS mp) 1); ``` paulson@3234 ` 457` ```by (REPEAT (ares_tac ([impI,allI]@prems) 1)); ``` paulson@3234 ` 458` ```qed "zero_induct"; ``` paulson@3234 ` 459` wenzelm@5069 ` 460` ```Goal "!!k::nat. (k+m) - (k+n) = m - n"; ``` paulson@3339 ` 461` ```by (induct_tac "k" 1); ``` paulson@3234 ` 462` ```by (ALLGOALS Asm_simp_tac); ``` paulson@3234 ` 463` ```qed "diff_cancel"; ``` paulson@3234 ` 464` ```Addsimps [diff_cancel]; ``` paulson@3234 ` 465` wenzelm@5069 ` 466` ```Goal "!!m::nat. (m+k) - (n+k) = m - n"; ``` paulson@3234 ` 467` ```val add_commute_k = read_instantiate [("n","k")] add_commute; ``` wenzelm@4089 ` 468` ```by (asm_simp_tac (simpset() addsimps ([add_commute_k])) 1); ``` paulson@3234 ` 469` ```qed "diff_cancel2"; ``` paulson@3234 ` 470` ```Addsimps [diff_cancel2]; ``` paulson@3234 ` 471` paulson@5414 ` 472` ```(*From Clemens Ballarin, proof by lcp*) ``` wenzelm@5069 ` 473` ```Goal "!!n::nat. [| k<=n; n<=m |] ==> (m-k) - (n-k) = m-n"; ``` paulson@5414 ` 474` ```by (REPEAT (etac rev_mp 1)); ``` paulson@5414 ` 475` ```by (res_inst_tac [("m","m"),("n","n")] diff_induct 1); ``` paulson@5414 ` 476` ```by (ALLGOALS Asm_simp_tac); ``` paulson@5414 ` 477` ```(*a confluence problem*) ``` paulson@5414 ` 478` ```by (asm_simp_tac (simpset() addsimps [Suc_diff_le, le_Suc_eq]) 1); ``` paulson@3234 ` 479` ```qed "diff_right_cancel"; ``` paulson@3234 ` 480` wenzelm@5069 ` 481` ```Goal "!!n::nat. n - (n+m) = 0"; ``` paulson@3339 ` 482` ```by (induct_tac "n" 1); ``` paulson@3234 ` 483` ```by (ALLGOALS Asm_simp_tac); ``` paulson@3234 ` 484` ```qed "diff_add_0"; ``` paulson@3234 ` 485` ```Addsimps [diff_add_0]; ``` paulson@3234 ` 486` paulson@5409 ` 487` paulson@3234 ` 488` ```(** Difference distributes over multiplication **) ``` paulson@3234 ` 489` wenzelm@5069 ` 490` ```Goal "!!m::nat. (m - n) * k = (m * k) - (n * k)"; ``` paulson@3234 ` 491` ```by (res_inst_tac [("m","m"),("n","n")] diff_induct 1); ``` paulson@3234 ` 492` ```by (ALLGOALS Asm_simp_tac); ``` paulson@3234 ` 493` ```qed "diff_mult_distrib" ; ``` paulson@3234 ` 494` wenzelm@5069 ` 495` ```Goal "!!m::nat. k * (m - n) = (k * m) - (k * n)"; ``` paulson@3234 ` 496` ```val mult_commute_k = read_instantiate [("m","k")] mult_commute; ``` wenzelm@4089 ` 497` ```by (simp_tac (simpset() addsimps [diff_mult_distrib, mult_commute_k]) 1); ``` paulson@3234 ` 498` ```qed "diff_mult_distrib2" ; ``` paulson@3234 ` 499` ```(*NOT added as rewrites, since sometimes they are used from right-to-left*) ``` paulson@3234 ` 500` paulson@3234 ` 501` paulson@1713 ` 502` ```(*** Monotonicity of Multiplication ***) ``` paulson@1713 ` 503` wenzelm@5069 ` 504` ```Goal "!!i::nat. i<=j ==> i*k<=j*k"; ``` paulson@3339 ` 505` ```by (induct_tac "k" 1); ``` wenzelm@4089 ` 506` ```by (ALLGOALS (asm_simp_tac (simpset() addsimps [add_le_mono]))); ``` paulson@1713 ` 507` ```qed "mult_le_mono1"; ``` paulson@1713 ` 508` paulson@1713 ` 509` ```(*<=monotonicity, BOTH arguments*) ``` wenzelm@5069 ` 510` ```Goal "!!i::nat. [| i<=j; k<=l |] ==> i*k<=j*l"; ``` paulson@2007 ` 511` ```by (etac (mult_le_mono1 RS le_trans) 1); ``` paulson@1713 ` 512` ```by (rtac le_trans 1); ``` paulson@2007 ` 513` ```by (stac mult_commute 2); ``` paulson@2007 ` 514` ```by (etac mult_le_mono1 2); ``` wenzelm@4089 ` 515` ```by (simp_tac (simpset() addsimps [mult_commute]) 1); ``` paulson@1713 ` 516` ```qed "mult_le_mono"; ``` paulson@1713 ` 517` paulson@1713 ` 518` ```(*strict, in 1st argument; proof is by induction on k>0*) ``` wenzelm@5069 ` 519` ```Goal "!!i::nat. [| i k*i < k*j"; ``` paulson@5078 ` 520` ```by (eres_inst_tac [("m1","0")] (less_eq_Suc_add RS exE) 1); ``` paulson@1713 ` 521` ```by (Asm_simp_tac 1); ``` paulson@3339 ` 522` ```by (induct_tac "x" 1); ``` wenzelm@4089 ` 523` ```by (ALLGOALS (asm_simp_tac (simpset() addsimps [add_less_mono]))); ``` paulson@1713 ` 524` ```qed "mult_less_mono2"; ``` paulson@1713 ` 525` wenzelm@5069 ` 526` ```Goal "!!i::nat. [| i i*k < j*k"; ``` paulson@3457 ` 527` ```by (dtac mult_less_mono2 1); ``` wenzelm@4089 ` 528` ```by (ALLGOALS (asm_full_simp_tac (simpset() addsimps [mult_commute]))); ``` paulson@3234 ` 529` ```qed "mult_less_mono1"; ``` paulson@3234 ` 530` wenzelm@5069 ` 531` ```Goal "(0 < m*n) = (0 (m*k < n*k) = (m (k*m < k*n) = (m (m*k = n*k) = (m=n)"; ``` paulson@3234 ` 570` ```by (cut_facts_tac [less_linear] 1); ``` paulson@3724 ` 571` ```by Safe_tac; ``` paulson@3457 ` 572` ```by (assume_tac 2); ``` paulson@3234 ` 573` ```by (ALLGOALS (dtac mult_less_mono1 THEN' assume_tac)); ``` paulson@3234 ` 574` ```by (ALLGOALS Asm_full_simp_tac); ``` paulson@3234 ` 575` ```qed "mult_cancel2"; ``` paulson@3234 ` 576` paulson@5143 ` 577` ```Goal "0 (k*m = k*n) = (m=n)"; ``` paulson@3457 ` 578` ```by (dtac mult_cancel2 1); ``` wenzelm@4089 ` 579` ```by (asm_full_simp_tac (simpset() addsimps [mult_commute]) 1); ``` paulson@3234 ` 580` ```qed "mult_cancel1"; ``` paulson@3234 ` 581` ```Addsimps [mult_cancel1, mult_cancel2]; ``` paulson@3234 ` 582` wenzelm@5069 ` 583` ```Goal "(Suc k * m = Suc k * n) = (m = n)"; ``` wenzelm@4423 ` 584` ```by (rtac mult_cancel1 1); ``` wenzelm@4297 ` 585` ```by (Simp_tac 1); ``` wenzelm@4297 ` 586` ```qed "Suc_mult_cancel1"; ``` wenzelm@4297 ` 587` paulson@3234 ` 588` paulson@1795 ` 589` ```(** Lemma for gcd **) ``` paulson@1795 ` 590` paulson@5143 ` 591` ```Goal "m = m*n ==> n=1 | m=0"; ``` paulson@1795 ` 592` ```by (dtac sym 1); ``` paulson@1795 ` 593` ```by (rtac disjCI 1); ``` paulson@1795 ` 594` ```by (rtac nat_less_cases 1 THEN assume_tac 2); ``` wenzelm@4089 ` 595` ```by (fast_tac (claset() addSEs [less_SucE] addss simpset()) 1); ``` nipkow@4356 ` 596` ```by (best_tac (claset() addDs [mult_less_mono2] addss simpset()) 1); ``` paulson@1795 ` 597` ```qed "mult_eq_self_implies_10"; ``` paulson@1795 ` 598` paulson@1795 ` 599` paulson@4736 ` 600` ```(*** Subtraction laws -- mostly from Clemens Ballarin ***) ``` paulson@3234 ` 601` wenzelm@5069 ` 602` ```Goal "!! a b c::nat. [| a < b; c <= a |] ==> a-c < b-c"; ``` paulson@3234 ` 603` ```by (subgoal_tac "c+(a-c) < c+(b-c)" 1); ``` paulson@3381 ` 604` ```by (Full_simp_tac 1); ``` paulson@3234 ` 605` ```by (subgoal_tac "c <= b" 1); ``` wenzelm@4089 ` 606` ```by (blast_tac (claset() addIs [less_imp_le, le_trans]) 2); ``` paulson@3381 ` 607` ```by (Asm_simp_tac 1); ``` paulson@3234 ` 608` ```qed "diff_less_mono"; ``` paulson@3234 ` 609` wenzelm@5069 ` 610` ```Goal "!! a b c::nat. a+b < c ==> a < c-b"; ``` paulson@3457 ` 611` ```by (dtac diff_less_mono 1); ``` paulson@3457 ` 612` ```by (rtac le_add2 1); ``` paulson@3234 ` 613` ```by (Asm_full_simp_tac 1); ``` paulson@3234 ` 614` ```qed "add_less_imp_less_diff"; ``` paulson@3234 ` 615` nipkow@5427 ` 616` ```Goal "(i < j-k) = (i+k < (j::nat))"; ``` nipkow@5427 ` 617` ```br iffI 1; ``` nipkow@5427 ` 618` ``` by(case_tac "k <= j" 1); ``` nipkow@5427 ` 619` ``` bd le_add_diff_inverse2 1; ``` nipkow@5427 ` 620` ``` by(dres_inst_tac [("k","k")] add_less_mono1 1); ``` nipkow@5427 ` 621` ``` by(Asm_full_simp_tac 1); ``` nipkow@5427 ` 622` ``` by(rotate_tac 1 1); ``` nipkow@5427 ` 623` ``` by(asm_full_simp_tac (simpset() addSolver cut_trans_tac) 1); ``` nipkow@5427 ` 624` ```be add_less_imp_less_diff 1; ``` nipkow@5427 ` 625` ```qed "less_diff_conv"; ``` nipkow@5427 ` 626` paulson@5143 ` 627` ```Goal "Suc i <= n ==> Suc (n - Suc i) = n - i"; ``` paulson@5414 ` 628` ```by (asm_simp_tac (simpset() addsimps [Suc_diff_le RS sym]) 1); ``` paulson@3234 ` 629` ```qed "Suc_diff_Suc"; ``` paulson@3234 ` 630` wenzelm@5069 ` 631` ```Goal "!! i::nat. i <= n ==> n - (n - i) = i"; ``` paulson@3903 ` 632` ```by (etac rev_mp 1); ``` paulson@3903 ` 633` ```by (res_inst_tac [("m","n"),("n","i")] diff_induct 1); ``` wenzelm@4089 ` 634` ```by (ALLGOALS (asm_simp_tac (simpset() addsimps [Suc_diff_le]))); ``` paulson@3234 ` 635` ```qed "diff_diff_cancel"; ``` paulson@3381 ` 636` ```Addsimps [diff_diff_cancel]; ``` paulson@3234 ` 637` wenzelm@5069 ` 638` ```Goal "!!k::nat. k <= n ==> m <= n + m - k"; ``` paulson@3457 ` 639` ```by (etac rev_mp 1); ``` paulson@3234 ` 640` ```by (res_inst_tac [("m", "k"), ("n", "n")] diff_induct 1); ``` paulson@3234 ` 641` ```by (Simp_tac 1); ``` wenzelm@4089 ` 642` ```by (simp_tac (simpset() addsimps [less_add_Suc2, less_imp_le]) 1); ``` paulson@3234 ` 643` ```by (Simp_tac 1); ``` paulson@3234 ` 644` ```qed "le_add_diff"; ``` paulson@3234 ` 645` wenzelm@5069 ` 646` ```Goal "!!i::nat. 0 j j+k-i < k"; ``` paulson@4736 ` 647` ```by (res_inst_tac [("m","j"),("n","i")] diff_induct 1); ``` paulson@4736 ` 648` ```by (ALLGOALS Asm_simp_tac); ``` paulson@4736 ` 649` ```qed_spec_mp "add_diff_less"; ``` paulson@4736 ` 650` paulson@3234 ` 651` paulson@5356 ` 652` ```Goal "m-1 < n ==> m <= n"; ``` paulson@5356 ` 653` ```by (exhaust_tac "m" 1); ``` paulson@5356 ` 654` ```by (auto_tac (claset(), simpset() addsimps [Suc_le_eq])); ``` paulson@5356 ` 655` ```qed "pred_less_imp_le"; ``` paulson@5356 ` 656` paulson@5356 ` 657` ```Goal "j<=i ==> i - j < Suc i - j"; ``` paulson@5356 ` 658` ```by (REPEAT (etac rev_mp 1)); ``` paulson@5356 ` 659` ```by (res_inst_tac [("m","i"),("n","j")] diff_induct 1); ``` paulson@5356 ` 660` ```by Auto_tac; ``` paulson@5356 ` 661` ```qed "diff_less_Suc_diff"; ``` paulson@5356 ` 662` paulson@5356 ` 663` ```Goal "i - j <= Suc i - j"; ``` paulson@5356 ` 664` ```by (res_inst_tac [("m","i"),("n","j")] diff_induct 1); ``` paulson@5356 ` 665` ```by Auto_tac; ``` paulson@5356 ` 666` ```qed "diff_le_Suc_diff"; ``` paulson@5356 ` 667` ```AddIffs [diff_le_Suc_diff]; ``` paulson@5356 ` 668` paulson@5356 ` 669` ```Goal "n - Suc i <= n - i"; ``` paulson@5356 ` 670` ```by (case_tac "i (m <= n-1) = (m (m-1 < n) = (m<=n)"; ``` paulson@5409 ` 683` ```by (exhaust_tac "m" 1); ``` paulson@5409 ` 684` ```by (auto_tac (claset(), simpset() addsimps [Suc_le_eq])); ``` paulson@5409 ` 685` ```qed "less_pred_eq"; ``` paulson@5409 ` 686` paulson@5414 ` 687` ```(*In ordinary notation: if 0 m - n < m"; ``` paulson@5414 ` 689` ```by (subgoal_tac "0 ~ m m - n < m" 1); ``` paulson@5414 ` 690` ```by (Blast_tac 1); ``` paulson@5414 ` 691` ```by (res_inst_tac [("m","m"),("n","n")] diff_induct 1); ``` paulson@5414 ` 692` ```by (ALLGOALS(asm_simp_tac(simpset() addsimps [diff_less_Suc]))); ``` paulson@5414 ` 693` ```qed "diff_less"; ``` paulson@5414 ` 694` paulson@5414 ` 695` ```Goal "[| 0 m - n < m"; ``` paulson@5414 ` 696` ```by (asm_simp_tac (simpset() addsimps [diff_less, not_less_iff_le]) 1); ``` paulson@5414 ` 697` ```qed "le_diff_less"; ``` paulson@5414 ` 698` paulson@5356 ` 699` paulson@4732 ` 700` nipkow@3484 ` 701` ```(** (Anti)Monotonicity of subtraction -- by Stefan Merz **) ``` nipkow@3484 ` 702` nipkow@3484 ` 703` ```(* Monotonicity of subtraction in first argument *) ``` wenzelm@5069 ` 704` ```Goal "!!n::nat. m<=n --> (m-l) <= (n-l)"; ``` nipkow@3484 ` 705` ```by (induct_tac "n" 1); ``` nipkow@3484 ` 706` ```by (Simp_tac 1); ``` wenzelm@4089 ` 707` ```by (simp_tac (simpset() addsimps [le_Suc_eq]) 1); ``` paulson@4732 ` 708` ```by (blast_tac (claset() addIs [diff_le_Suc_diff, le_trans]) 1); ``` nipkow@3484 ` 709` ```qed_spec_mp "diff_le_mono"; ``` nipkow@3484 ` 710` wenzelm@5069 ` 711` ```Goal "!!n::nat. m<=n ==> (l-n) <= (l-m)"; ``` nipkow@3484 ` 712` ```by (induct_tac "l" 1); ``` nipkow@3484 ` 713` ```by (Simp_tac 1); ``` berghofe@5183 ` 714` ```by (case_tac "n <= na" 1); ``` berghofe@5183 ` 715` ```by (subgoal_tac "m <= na" 1); ``` wenzelm@4089 ` 716` ```by (asm_simp_tac (simpset() addsimps [Suc_diff_le]) 1); ``` wenzelm@4089 ` 717` ```by (fast_tac (claset() addEs [le_trans]) 1); ``` nipkow@3484 ` 718` ```by (dtac not_leE 1); ``` paulson@5414 ` 719` ```by (asm_simp_tac (simpset() addsimps [if_Suc_diff_le]) 1); ``` nipkow@3484 ` 720` ```qed_spec_mp "diff_le_mono2"; ```