src/HOL/Arith.ML
 author paulson Thu Jun 25 13:57:34 1998 +0200 (1998-06-25) changeset 5078 7b5ea59c0275 parent 5069 3ea049f7979d child 5143 b94cd208f073 permissions -rw-r--r--
Installation of target HOL-Real
```     1 (*  Title:      HOL/Arith.ML
```
```     2     ID:         \$Id\$
```
```     3     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
```
```     4     Copyright   1998  University of Cambridge
```
```     5
```
```     6 Proofs about elementary arithmetic: addition, multiplication, etc.
```
```     7 Some from the Hoare example from Norbert Galm
```
```     8 *)
```
```     9
```
```    10 (*** Basic rewrite rules for the arithmetic operators ***)
```
```    11
```
```    12
```
```    13 (** Difference **)
```
```    14
```
```    15 qed_goal "diff_0_eq_0" thy
```
```    16     "0 - n = 0"
```
```    17  (fn _ => [induct_tac "n" 1,  ALLGOALS Asm_simp_tac]);
```
```    18
```
```    19 (*Must simplify BEFORE the induction!!  (Else we get a critical pair)
```
```    20   Suc(m) - Suc(n)   rewrites to   pred(Suc(m) - n)  *)
```
```    21 qed_goal "diff_Suc_Suc" thy
```
```    22     "Suc(m) - Suc(n) = m - n"
```
```    23  (fn _ =>
```
```    24   [Simp_tac 1, induct_tac "n" 1, ALLGOALS Asm_simp_tac]);
```
```    25
```
```    26 Addsimps [diff_0_eq_0, diff_Suc_Suc];
```
```    27
```
```    28 (* Could be (and is, below) generalized in various ways;
```
```    29    However, none of the generalizations are currently in the simpset,
```
```    30    and I dread to think what happens if I put them in *)
```
```    31 Goal "!!n. 0 < n ==> Suc(n-1) = n";
```
```    32 by (asm_simp_tac (simpset() addsplits [split_nat_case]) 1);
```
```    33 qed "Suc_pred";
```
```    34 Addsimps [Suc_pred];
```
```    35
```
```    36 Delsimps [diff_Suc];
```
```    37
```
```    38
```
```    39 (**** Inductive properties of the operators ****)
```
```    40
```
```    41 (*** Addition ***)
```
```    42
```
```    43 qed_goal "add_0_right" thy "m + 0 = m"
```
```    44  (fn _ => [induct_tac "m" 1, ALLGOALS Asm_simp_tac]);
```
```    45
```
```    46 qed_goal "add_Suc_right" thy "m + Suc(n) = Suc(m+n)"
```
```    47  (fn _ => [induct_tac "m" 1, ALLGOALS Asm_simp_tac]);
```
```    48
```
```    49 Addsimps [add_0_right,add_Suc_right];
```
```    50
```
```    51 (*Associative law for addition*)
```
```    52 qed_goal "add_assoc" thy "(m + n) + k = m + ((n + k)::nat)"
```
```    53  (fn _ => [induct_tac "m" 1, ALLGOALS Asm_simp_tac]);
```
```    54
```
```    55 (*Commutative law for addition*)
```
```    56 qed_goal "add_commute" thy "m + n = n + (m::nat)"
```
```    57  (fn _ =>  [induct_tac "m" 1, ALLGOALS Asm_simp_tac]);
```
```    58
```
```    59 qed_goal "add_left_commute" thy "x+(y+z)=y+((x+z)::nat)"
```
```    60  (fn _ => [rtac (add_commute RS trans) 1, rtac (add_assoc RS trans) 1,
```
```    61            rtac (add_commute RS arg_cong) 1]);
```
```    62
```
```    63 (*Addition is an AC-operator*)
```
```    64 val add_ac = [add_assoc, add_commute, add_left_commute];
```
```    65
```
```    66 Goal "!!k::nat. (k + m = k + n) = (m=n)";
```
```    67 by (induct_tac "k" 1);
```
```    68 by (Simp_tac 1);
```
```    69 by (Asm_simp_tac 1);
```
```    70 qed "add_left_cancel";
```
```    71
```
```    72 Goal "!!k::nat. (m + k = n + k) = (m=n)";
```
```    73 by (induct_tac "k" 1);
```
```    74 by (Simp_tac 1);
```
```    75 by (Asm_simp_tac 1);
```
```    76 qed "add_right_cancel";
```
```    77
```
```    78 Goal "!!k::nat. (k + m <= k + n) = (m<=n)";
```
```    79 by (induct_tac "k" 1);
```
```    80 by (Simp_tac 1);
```
```    81 by (Asm_simp_tac 1);
```
```    82 qed "add_left_cancel_le";
```
```    83
```
```    84 Goal "!!k::nat. (k + m < k + n) = (m<n)";
```
```    85 by (induct_tac "k" 1);
```
```    86 by (Simp_tac 1);
```
```    87 by (Asm_simp_tac 1);
```
```    88 qed "add_left_cancel_less";
```
```    89
```
```    90 Addsimps [add_left_cancel, add_right_cancel,
```
```    91           add_left_cancel_le, add_left_cancel_less];
```
```    92
```
```    93 (** Reasoning about m+0=0, etc. **)
```
```    94
```
```    95 Goal "(m+n = 0) = (m=0 & n=0)";
```
```    96 by (induct_tac "m" 1);
```
```    97 by (ALLGOALS Asm_simp_tac);
```
```    98 qed "add_is_0";
```
```    99 AddIffs [add_is_0];
```
```   100
```
```   101 Goal "(0<m+n) = (0<m | 0<n)";
```
```   102 by (simp_tac (simpset() delsimps [neq0_conv] addsimps [neq0_conv RS sym]) 1);
```
```   103 qed "add_gr_0";
```
```   104 AddIffs [add_gr_0];
```
```   105
```
```   106 (* FIXME: really needed?? *)
```
```   107 Goal "((m+n)-1 = 0) = (m=0 & n-1 = 0 | m-1 = 0 & n=0)";
```
```   108 by (exhaust_tac "m" 1);
```
```   109 by (ALLGOALS (fast_tac (claset() addss (simpset()))));
```
```   110 qed "pred_add_is_0";
```
```   111 Addsimps [pred_add_is_0];
```
```   112
```
```   113 (* Could be generalized, eg to "!!n. k<n ==> m+(n-(Suc k)) = (m+n)-(Suc k)" *)
```
```   114 Goal "!!n. 0<n ==> m + (n-1) = (m+n)-1";
```
```   115 by (exhaust_tac "m" 1);
```
```   116 by (ALLGOALS (asm_simp_tac (simpset() addsimps [diff_Suc]
```
```   117                                       addsplits [split_nat_case])));
```
```   118 qed "add_pred";
```
```   119 Addsimps [add_pred];
```
```   120
```
```   121 Goal "!!m::nat. m + n = m ==> n = 0";
```
```   122 by (dtac (add_0_right RS ssubst) 1);
```
```   123 by (asm_full_simp_tac (simpset() addsimps [add_assoc]
```
```   124                                  delsimps [add_0_right]) 1);
```
```   125 qed "add_eq_self_zero";
```
```   126
```
```   127
```
```   128 (**** Additional theorems about "less than" ****)
```
```   129
```
```   130 (*Deleted less_natE; instead use less_eq_Suc_add RS exE*)
```
```   131 Goal "!!m. m<n --> (? k. n=Suc(m+k))";
```
```   132 by (induct_tac "n" 1);
```
```   133 by (ALLGOALS (simp_tac (simpset() addsimps [less_Suc_eq])));
```
```   134 by (blast_tac (claset() addSEs [less_SucE]
```
```   135                        addSIs [add_0_right RS sym, add_Suc_right RS sym]) 1);
```
```   136 qed_spec_mp "less_eq_Suc_add";
```
```   137
```
```   138 Goal "n <= ((m + n)::nat)";
```
```   139 by (induct_tac "m" 1);
```
```   140 by (ALLGOALS Simp_tac);
```
```   141 by (etac le_trans 1);
```
```   142 by (rtac (lessI RS less_imp_le) 1);
```
```   143 qed "le_add2";
```
```   144
```
```   145 Goal "n <= ((n + m)::nat)";
```
```   146 by (simp_tac (simpset() addsimps add_ac) 1);
```
```   147 by (rtac le_add2 1);
```
```   148 qed "le_add1";
```
```   149
```
```   150 bind_thm ("less_add_Suc1", (lessI RS (le_add1 RS le_less_trans)));
```
```   151 bind_thm ("less_add_Suc2", (lessI RS (le_add2 RS le_less_trans)));
```
```   152
```
```   153 (*"i <= j ==> i <= j+m"*)
```
```   154 bind_thm ("trans_le_add1", le_add1 RSN (2,le_trans));
```
```   155
```
```   156 (*"i <= j ==> i <= m+j"*)
```
```   157 bind_thm ("trans_le_add2", le_add2 RSN (2,le_trans));
```
```   158
```
```   159 (*"i < j ==> i < j+m"*)
```
```   160 bind_thm ("trans_less_add1", le_add1 RSN (2,less_le_trans));
```
```   161
```
```   162 (*"i < j ==> i < m+j"*)
```
```   163 bind_thm ("trans_less_add2", le_add2 RSN (2,less_le_trans));
```
```   164
```
```   165 Goal "!!i. i+j < (k::nat) ==> i<k";
```
```   166 by (etac rev_mp 1);
```
```   167 by (induct_tac "j" 1);
```
```   168 by (ALLGOALS Asm_simp_tac);
```
```   169 by (blast_tac (claset() addDs [Suc_lessD]) 1);
```
```   170 qed "add_lessD1";
```
```   171
```
```   172 Goal "!!i::nat. ~ (i+j < i)";
```
```   173 by (rtac notI 1);
```
```   174 by (etac (add_lessD1 RS less_irrefl) 1);
```
```   175 qed "not_add_less1";
```
```   176
```
```   177 Goal "!!i::nat. ~ (j+i < i)";
```
```   178 by (simp_tac (simpset() addsimps [add_commute, not_add_less1]) 1);
```
```   179 qed "not_add_less2";
```
```   180 AddIffs [not_add_less1, not_add_less2];
```
```   181
```
```   182 Goal "!!k::nat. m <= n ==> m <= n+k";
```
```   183 by (etac le_trans 1);
```
```   184 by (rtac le_add1 1);
```
```   185 qed "le_imp_add_le";
```
```   186
```
```   187 Goal "!!k::nat. m < n ==> m < n+k";
```
```   188 by (etac less_le_trans 1);
```
```   189 by (rtac le_add1 1);
```
```   190 qed "less_imp_add_less";
```
```   191
```
```   192 Goal "m+k<=n --> m<=(n::nat)";
```
```   193 by (induct_tac "k" 1);
```
```   194 by (ALLGOALS Asm_simp_tac);
```
```   195 by (blast_tac (claset() addDs [Suc_leD]) 1);
```
```   196 qed_spec_mp "add_leD1";
```
```   197
```
```   198 Goal "!!n::nat. m+k<=n ==> k<=n";
```
```   199 by (full_simp_tac (simpset() addsimps [add_commute]) 1);
```
```   200 by (etac add_leD1 1);
```
```   201 qed_spec_mp "add_leD2";
```
```   202
```
```   203 Goal "!!n::nat. m+k<=n ==> m<=n & k<=n";
```
```   204 by (blast_tac (claset() addDs [add_leD1, add_leD2]) 1);
```
```   205 bind_thm ("add_leE", result() RS conjE);
```
```   206
```
```   207 Goal "!!k l::nat. [| k<l; m+l = k+n |] ==> m<n";
```
```   208 by (safe_tac (claset() addSDs [less_eq_Suc_add]));
```
```   209 by (asm_full_simp_tac
```
```   210     (simpset() delsimps [add_Suc_right]
```
```   211                 addsimps ([add_Suc_right RS sym, add_left_cancel] @add_ac)) 1);
```
```   212 by (etac subst 1);
```
```   213 by (simp_tac (simpset() addsimps [less_add_Suc1]) 1);
```
```   214 qed "less_add_eq_less";
```
```   215
```
```   216
```
```   217 (*** Monotonicity of Addition ***)
```
```   218
```
```   219 (*strict, in 1st argument*)
```
```   220 Goal "!!i j k::nat. i < j ==> i + k < j + k";
```
```   221 by (induct_tac "k" 1);
```
```   222 by (ALLGOALS Asm_simp_tac);
```
```   223 qed "add_less_mono1";
```
```   224
```
```   225 (*strict, in both arguments*)
```
```   226 Goal "!!i j k::nat. [|i < j; k < l|] ==> i + k < j + l";
```
```   227 by (rtac (add_less_mono1 RS less_trans) 1);
```
```   228 by (REPEAT (assume_tac 1));
```
```   229 by (induct_tac "j" 1);
```
```   230 by (ALLGOALS Asm_simp_tac);
```
```   231 qed "add_less_mono";
```
```   232
```
```   233 (*A [clumsy] way of lifting < monotonicity to <= monotonicity *)
```
```   234 val [lt_mono,le] = goal thy
```
```   235      "[| !!i j::nat. i<j ==> f(i) < f(j);       \
```
```   236 \        i <= j                                 \
```
```   237 \     |] ==> f(i) <= (f(j)::nat)";
```
```   238 by (cut_facts_tac [le] 1);
```
```   239 by (asm_full_simp_tac (simpset() addsimps [le_eq_less_or_eq]) 1);
```
```   240 by (blast_tac (claset() addSIs [lt_mono]) 1);
```
```   241 qed "less_mono_imp_le_mono";
```
```   242
```
```   243 (*non-strict, in 1st argument*)
```
```   244 Goal "!!i j k::nat. i<=j ==> i + k <= j + k";
```
```   245 by (res_inst_tac [("f", "%j. j+k")] less_mono_imp_le_mono 1);
```
```   246 by (etac add_less_mono1 1);
```
```   247 by (assume_tac 1);
```
```   248 qed "add_le_mono1";
```
```   249
```
```   250 (*non-strict, in both arguments*)
```
```   251 Goal "!!k l::nat. [|i<=j;  k<=l |] ==> i + k <= j + l";
```
```   252 by (etac (add_le_mono1 RS le_trans) 1);
```
```   253 by (simp_tac (simpset() addsimps [add_commute]) 1);
```
```   254 (*j moves to the end because it is free while k, l are bound*)
```
```   255 by (etac add_le_mono1 1);
```
```   256 qed "add_le_mono";
```
```   257
```
```   258
```
```   259 (*** Multiplication ***)
```
```   260
```
```   261 (*right annihilation in product*)
```
```   262 qed_goal "mult_0_right" thy "m * 0 = 0"
```
```   263  (fn _ => [induct_tac "m" 1, ALLGOALS Asm_simp_tac]);
```
```   264
```
```   265 (*right successor law for multiplication*)
```
```   266 qed_goal "mult_Suc_right" thy  "m * Suc(n) = m + (m * n)"
```
```   267  (fn _ => [induct_tac "m" 1,
```
```   268            ALLGOALS(asm_simp_tac (simpset() addsimps add_ac))]);
```
```   269
```
```   270 Addsimps [mult_0_right, mult_Suc_right];
```
```   271
```
```   272 Goal "1 * n = n";
```
```   273 by (Asm_simp_tac 1);
```
```   274 qed "mult_1";
```
```   275
```
```   276 Goal "n * 1 = n";
```
```   277 by (Asm_simp_tac 1);
```
```   278 qed "mult_1_right";
```
```   279
```
```   280 (*Commutative law for multiplication*)
```
```   281 qed_goal "mult_commute" thy "m * n = n * (m::nat)"
```
```   282  (fn _ => [induct_tac "m" 1, ALLGOALS Asm_simp_tac]);
```
```   283
```
```   284 (*addition distributes over multiplication*)
```
```   285 qed_goal "add_mult_distrib" thy "(m + n)*k = (m*k) + ((n*k)::nat)"
```
```   286  (fn _ => [induct_tac "m" 1,
```
```   287            ALLGOALS(asm_simp_tac (simpset() addsimps add_ac))]);
```
```   288
```
```   289 qed_goal "add_mult_distrib2" thy "k*(m + n) = (k*m) + ((k*n)::nat)"
```
```   290  (fn _ => [induct_tac "m" 1,
```
```   291            ALLGOALS(asm_simp_tac (simpset() addsimps add_ac))]);
```
```   292
```
```   293 (*Associative law for multiplication*)
```
```   294 qed_goal "mult_assoc" thy "(m * n) * k = m * ((n * k)::nat)"
```
```   295   (fn _ => [induct_tac "m" 1,
```
```   296             ALLGOALS (asm_simp_tac (simpset() addsimps [add_mult_distrib]))]);
```
```   297
```
```   298 qed_goal "mult_left_commute" thy "x*(y*z) = y*((x*z)::nat)"
```
```   299  (fn _ => [rtac trans 1, rtac mult_commute 1, rtac trans 1,
```
```   300            rtac mult_assoc 1, rtac (mult_commute RS arg_cong) 1]);
```
```   301
```
```   302 val mult_ac = [mult_assoc,mult_commute,mult_left_commute];
```
```   303
```
```   304 Goal "(m*n = 0) = (m=0 | n=0)";
```
```   305 by (induct_tac "m" 1);
```
```   306 by (induct_tac "n" 2);
```
```   307 by (ALLGOALS Asm_simp_tac);
```
```   308 qed "mult_is_0";
```
```   309 Addsimps [mult_is_0];
```
```   310
```
```   311 Goal "!!m::nat. m <= m*m";
```
```   312 by (induct_tac "m" 1);
```
```   313 by (ALLGOALS (asm_simp_tac (simpset() addsimps [add_assoc RS sym])));
```
```   314 by (etac (le_add2 RSN (2,le_trans)) 1);
```
```   315 qed "le_square";
```
```   316
```
```   317
```
```   318 (*** Difference ***)
```
```   319
```
```   320
```
```   321 qed_goal "diff_self_eq_0" thy "m - m = 0"
```
```   322  (fn _ => [induct_tac "m" 1, ALLGOALS Asm_simp_tac]);
```
```   323 Addsimps [diff_self_eq_0];
```
```   324
```
```   325 (*Addition is the inverse of subtraction: if n<=m then n+(m-n) = m. *)
```
```   326 Goal "~ m<n --> n+(m-n) = (m::nat)";
```
```   327 by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
```
```   328 by (ALLGOALS Asm_simp_tac);
```
```   329 qed_spec_mp "add_diff_inverse";
```
```   330
```
```   331 Goal "!!m. n<=m ==> n+(m-n) = (m::nat)";
```
```   332 by (asm_simp_tac (simpset() addsimps [add_diff_inverse, not_less_iff_le]) 1);
```
```   333 qed "le_add_diff_inverse";
```
```   334
```
```   335 Goal "!!m. n<=m ==> (m-n)+n = (m::nat)";
```
```   336 by (asm_simp_tac (simpset() addsimps [le_add_diff_inverse, add_commute]) 1);
```
```   337 qed "le_add_diff_inverse2";
```
```   338
```
```   339 Addsimps  [le_add_diff_inverse, le_add_diff_inverse2];
```
```   340
```
```   341
```
```   342 (*** More results about difference ***)
```
```   343
```
```   344 val [prem] = goal thy "n < Suc(m) ==> Suc(m)-n = Suc(m-n)";
```
```   345 by (rtac (prem RS rev_mp) 1);
```
```   346 by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
```
```   347 by (ALLGOALS Asm_simp_tac);
```
```   348 qed "Suc_diff_n";
```
```   349
```
```   350 Goal "m - n < Suc(m)";
```
```   351 by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
```
```   352 by (etac less_SucE 3);
```
```   353 by (ALLGOALS (asm_simp_tac (simpset() addsimps [less_Suc_eq])));
```
```   354 qed "diff_less_Suc";
```
```   355
```
```   356 Goal "!!m::nat. m - n <= m";
```
```   357 by (res_inst_tac [("m","m"), ("n","n")] diff_induct 1);
```
```   358 by (ALLGOALS Asm_simp_tac);
```
```   359 qed "diff_le_self";
```
```   360 Addsimps [diff_le_self];
```
```   361
```
```   362 (* j<k ==> j-n < k *)
```
```   363 bind_thm ("less_imp_diff_less", diff_le_self RS le_less_trans);
```
```   364
```
```   365 Goal "!!i::nat. i-j-k = i - (j+k)";
```
```   366 by (res_inst_tac [("m","i"),("n","j")] diff_induct 1);
```
```   367 by (ALLGOALS Asm_simp_tac);
```
```   368 qed "diff_diff_left";
```
```   369
```
```   370 Goal "(Suc m - n) - Suc k = m - n - k";
```
```   371 by (simp_tac (simpset() addsimps [diff_diff_left]) 1);
```
```   372 qed "Suc_diff_diff";
```
```   373 Addsimps [Suc_diff_diff];
```
```   374
```
```   375 Goal "!!n. 0<n ==> n - Suc i < n";
```
```   376 by (res_inst_tac [("n","n")] natE 1);
```
```   377 by Safe_tac;
```
```   378 by (asm_simp_tac (simpset() addsimps [le_eq_less_Suc RS sym]) 1);
```
```   379 qed "diff_Suc_less";
```
```   380 Addsimps [diff_Suc_less];
```
```   381
```
```   382 Goal "!!n::nat. m - n <= Suc m - n";
```
```   383 by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
```
```   384 by (ALLGOALS Asm_simp_tac);
```
```   385 qed "diff_le_Suc_diff";
```
```   386
```
```   387 (*This and the next few suggested by Florian Kammueller*)
```
```   388 Goal "!!i::nat. i-j-k = i-k-j";
```
```   389 by (simp_tac (simpset() addsimps [diff_diff_left, add_commute]) 1);
```
```   390 qed "diff_commute";
```
```   391
```
```   392 Goal "!!i j k:: nat. k<=j --> j<=i --> i - (j - k) = i - j + k";
```
```   393 by (res_inst_tac [("m","i"),("n","j")] diff_induct 1);
```
```   394 by (ALLGOALS Asm_simp_tac);
```
```   395 by (asm_simp_tac
```
```   396     (simpset() addsimps [Suc_diff_n, le_imp_less_Suc, le_Suc_eq]) 1);
```
```   397 qed_spec_mp "diff_diff_right";
```
```   398
```
```   399 Goal "!!i j k:: nat. k<=j --> (i + j) - k = i + (j - k)";
```
```   400 by (res_inst_tac [("m","j"),("n","k")] diff_induct 1);
```
```   401 by (ALLGOALS Asm_simp_tac);
```
```   402 qed_spec_mp "diff_add_assoc";
```
```   403
```
```   404 Goal "!!i j k:: nat. k<=j --> (j + i) - k = i + (j - k)";
```
```   405 by (asm_simp_tac (simpset() addsimps [add_commute, diff_add_assoc]) 1);
```
```   406 qed_spec_mp "diff_add_assoc2";
```
```   407
```
```   408 Goal "!!n::nat. (n+m) - n = m";
```
```   409 by (induct_tac "n" 1);
```
```   410 by (ALLGOALS Asm_simp_tac);
```
```   411 qed "diff_add_inverse";
```
```   412 Addsimps [diff_add_inverse];
```
```   413
```
```   414 Goal "!!n::nat.(m+n) - n = m";
```
```   415 by (simp_tac (simpset() addsimps [diff_add_assoc]) 1);
```
```   416 qed "diff_add_inverse2";
```
```   417 Addsimps [diff_add_inverse2];
```
```   418
```
```   419 Goal "!!i j k::nat. i<=j ==> (j-i=k) = (j=k+i)";
```
```   420 by Safe_tac;
```
```   421 by (ALLGOALS Asm_simp_tac);
```
```   422 qed "le_imp_diff_is_add";
```
```   423
```
```   424 val [prem] = goal thy "m < Suc(n) ==> m-n = 0";
```
```   425 by (rtac (prem RS rev_mp) 1);
```
```   426 by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
```
```   427 by (asm_simp_tac (simpset() addsimps [less_Suc_eq]) 1);
```
```   428 by (ALLGOALS Asm_simp_tac);
```
```   429 qed "less_imp_diff_is_0";
```
```   430
```
```   431 val prems = goal thy "m-n = 0  -->  n-m = 0  -->  m=n";
```
```   432 by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
```
```   433 by (REPEAT(Simp_tac 1 THEN TRY(atac 1)));
```
```   434 qed_spec_mp "diffs0_imp_equal";
```
```   435
```
```   436 val [prem] = goal thy "m<n ==> 0<n-m";
```
```   437 by (rtac (prem RS rev_mp) 1);
```
```   438 by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
```
```   439 by (ALLGOALS Asm_simp_tac);
```
```   440 qed "less_imp_diff_positive";
```
```   441
```
```   442 Goal "!! (i::nat). i < j  ==> ? k. 0<k & i+k = j";
```
```   443 by (res_inst_tac [("x","j - i")] exI 1);
```
```   444 by (fast_tac (claset() addDs [less_trans, less_irrefl]
```
```   445    	               addIs [less_imp_diff_positive, add_diff_inverse]) 1);
```
```   446 qed "less_imp_add_positive";
```
```   447
```
```   448 Goal "Suc(m)-n = (if m<n then 0 else Suc(m-n))";
```
```   449 by (simp_tac (simpset() addsimps [less_imp_diff_is_0, not_less_eq, Suc_diff_n]) 1);
```
```   450 qed "if_Suc_diff_n";
```
```   451
```
```   452 Goal "Suc(m)-n <= Suc(m-n)";
```
```   453 by (simp_tac (simpset() addsimps [if_Suc_diff_n]) 1);
```
```   454 qed "diff_Suc_le_Suc_diff";
```
```   455
```
```   456 Goal "P(k) --> (!n. P(Suc(n))--> P(n)) --> P(k-i)";
```
```   457 by (res_inst_tac [("m","k"),("n","i")] diff_induct 1);
```
```   458 by (ALLGOALS (Clarify_tac THEN' Simp_tac THEN' TRY o Blast_tac));
```
```   459 qed "zero_induct_lemma";
```
```   460
```
```   461 val prems = goal thy "[| P(k);  !!n. P(Suc(n)) ==> P(n) |] ==> P(0)";
```
```   462 by (rtac (diff_self_eq_0 RS subst) 1);
```
```   463 by (rtac (zero_induct_lemma RS mp RS mp) 1);
```
```   464 by (REPEAT (ares_tac ([impI,allI]@prems) 1));
```
```   465 qed "zero_induct";
```
```   466
```
```   467 Goal "!!k::nat. (k+m) - (k+n) = m - n";
```
```   468 by (induct_tac "k" 1);
```
```   469 by (ALLGOALS Asm_simp_tac);
```
```   470 qed "diff_cancel";
```
```   471 Addsimps [diff_cancel];
```
```   472
```
```   473 Goal "!!m::nat. (m+k) - (n+k) = m - n";
```
```   474 val add_commute_k = read_instantiate [("n","k")] add_commute;
```
```   475 by (asm_simp_tac (simpset() addsimps ([add_commute_k])) 1);
```
```   476 qed "diff_cancel2";
```
```   477 Addsimps [diff_cancel2];
```
```   478
```
```   479 (*From Clemens Ballarin*)
```
```   480 Goal "!!n::nat. [| k<=n; n<=m |] ==> (m-k) - (n-k) = m-n";
```
```   481 by (subgoal_tac "k<=n --> n<=m --> (m-k) - (n-k) = m-n" 1);
```
```   482 by (Asm_full_simp_tac 1);
```
```   483 by (induct_tac "k" 1);
```
```   484 by (Simp_tac 1);
```
```   485 (* Induction step *)
```
```   486 by (subgoal_tac "Suc na <= m --> n <= m --> Suc na <= n --> \
```
```   487 \                Suc (m - Suc na) - Suc (n - Suc na) = m-n" 1);
```
```   488 by (Asm_full_simp_tac 1);
```
```   489 by (blast_tac (claset() addIs [le_trans]) 1);
```
```   490 by (auto_tac (claset() addIs [Suc_leD], simpset() delsimps [diff_Suc_Suc]));
```
```   491 by (asm_full_simp_tac (simpset() delsimps [Suc_less_eq]
```
```   492 		       addsimps [Suc_diff_n RS sym, le_eq_less_Suc]) 1);
```
```   493 qed "diff_right_cancel";
```
```   494
```
```   495 Goal "!!n::nat. n - (n+m) = 0";
```
```   496 by (induct_tac "n" 1);
```
```   497 by (ALLGOALS Asm_simp_tac);
```
```   498 qed "diff_add_0";
```
```   499 Addsimps [diff_add_0];
```
```   500
```
```   501 (** Difference distributes over multiplication **)
```
```   502
```
```   503 Goal "!!m::nat. (m - n) * k = (m * k) - (n * k)";
```
```   504 by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
```
```   505 by (ALLGOALS Asm_simp_tac);
```
```   506 qed "diff_mult_distrib" ;
```
```   507
```
```   508 Goal "!!m::nat. k * (m - n) = (k * m) - (k * n)";
```
```   509 val mult_commute_k = read_instantiate [("m","k")] mult_commute;
```
```   510 by (simp_tac (simpset() addsimps [diff_mult_distrib, mult_commute_k]) 1);
```
```   511 qed "diff_mult_distrib2" ;
```
```   512 (*NOT added as rewrites, since sometimes they are used from right-to-left*)
```
```   513
```
```   514
```
```   515 (*** Monotonicity of Multiplication ***)
```
```   516
```
```   517 Goal "!!i::nat. i<=j ==> i*k<=j*k";
```
```   518 by (induct_tac "k" 1);
```
```   519 by (ALLGOALS (asm_simp_tac (simpset() addsimps [add_le_mono])));
```
```   520 qed "mult_le_mono1";
```
```   521
```
```   522 (*<=monotonicity, BOTH arguments*)
```
```   523 Goal "!!i::nat. [| i<=j; k<=l |] ==> i*k<=j*l";
```
```   524 by (etac (mult_le_mono1 RS le_trans) 1);
```
```   525 by (rtac le_trans 1);
```
```   526 by (stac mult_commute 2);
```
```   527 by (etac mult_le_mono1 2);
```
```   528 by (simp_tac (simpset() addsimps [mult_commute]) 1);
```
```   529 qed "mult_le_mono";
```
```   530
```
```   531 (*strict, in 1st argument; proof is by induction on k>0*)
```
```   532 Goal "!!i::nat. [| i<j; 0<k |] ==> k*i < k*j";
```
```   533 by (eres_inst_tac [("m1","0")] (less_eq_Suc_add RS exE) 1);
```
```   534 by (Asm_simp_tac 1);
```
```   535 by (induct_tac "x" 1);
```
```   536 by (ALLGOALS (asm_simp_tac (simpset() addsimps [add_less_mono])));
```
```   537 qed "mult_less_mono2";
```
```   538
```
```   539 Goal "!!i::nat. [| i<j; 0<k |] ==> i*k < j*k";
```
```   540 by (dtac mult_less_mono2 1);
```
```   541 by (ALLGOALS (asm_full_simp_tac (simpset() addsimps [mult_commute])));
```
```   542 qed "mult_less_mono1";
```
```   543
```
```   544 Goal "(0 < m*n) = (0<m & 0<n)";
```
```   545 by (induct_tac "m" 1);
```
```   546 by (induct_tac "n" 2);
```
```   547 by (ALLGOALS Asm_simp_tac);
```
```   548 qed "zero_less_mult_iff";
```
```   549 Addsimps [zero_less_mult_iff];
```
```   550
```
```   551 Goal "(m*n = 1) = (m=1 & n=1)";
```
```   552 by (induct_tac "m" 1);
```
```   553 by (Simp_tac 1);
```
```   554 by (induct_tac "n" 1);
```
```   555 by (Simp_tac 1);
```
```   556 by (fast_tac (claset() addss simpset()) 1);
```
```   557 qed "mult_eq_1_iff";
```
```   558 Addsimps [mult_eq_1_iff];
```
```   559
```
```   560 Goal "!!k. 0<k ==> (m*k < n*k) = (m<n)";
```
```   561 by (safe_tac (claset() addSIs [mult_less_mono1]));
```
```   562 by (cut_facts_tac [less_linear] 1);
```
```   563 by (blast_tac (claset() addIs [mult_less_mono1] addEs [less_asym]) 1);
```
```   564 qed "mult_less_cancel2";
```
```   565
```
```   566 Goal "!!k. 0<k ==> (k*m < k*n) = (m<n)";
```
```   567 by (dtac mult_less_cancel2 1);
```
```   568 by (asm_full_simp_tac (simpset() addsimps [mult_commute]) 1);
```
```   569 qed "mult_less_cancel1";
```
```   570 Addsimps [mult_less_cancel1, mult_less_cancel2];
```
```   571
```
```   572 Goal "(Suc k * m < Suc k * n) = (m < n)";
```
```   573 by (rtac mult_less_cancel1 1);
```
```   574 by (Simp_tac 1);
```
```   575 qed "Suc_mult_less_cancel1";
```
```   576
```
```   577 Goalw [le_def] "(Suc k * m <= Suc k * n) = (m <= n)";
```
```   578 by (simp_tac (simpset_of HOL.thy) 1);
```
```   579 by (rtac Suc_mult_less_cancel1 1);
```
```   580 qed "Suc_mult_le_cancel1";
```
```   581
```
```   582 Goal "!!k. 0<k ==> (m*k = n*k) = (m=n)";
```
```   583 by (cut_facts_tac [less_linear] 1);
```
```   584 by Safe_tac;
```
```   585 by (assume_tac 2);
```
```   586 by (ALLGOALS (dtac mult_less_mono1 THEN' assume_tac));
```
```   587 by (ALLGOALS Asm_full_simp_tac);
```
```   588 qed "mult_cancel2";
```
```   589
```
```   590 Goal "!!k. 0<k ==> (k*m = k*n) = (m=n)";
```
```   591 by (dtac mult_cancel2 1);
```
```   592 by (asm_full_simp_tac (simpset() addsimps [mult_commute]) 1);
```
```   593 qed "mult_cancel1";
```
```   594 Addsimps [mult_cancel1, mult_cancel2];
```
```   595
```
```   596 Goal "(Suc k * m = Suc k * n) = (m = n)";
```
```   597 by (rtac mult_cancel1 1);
```
```   598 by (Simp_tac 1);
```
```   599 qed "Suc_mult_cancel1";
```
```   600
```
```   601
```
```   602 (** Lemma for gcd **)
```
```   603
```
```   604 Goal "!!m n. m = m*n ==> n=1 | m=0";
```
```   605 by (dtac sym 1);
```
```   606 by (rtac disjCI 1);
```
```   607 by (rtac nat_less_cases 1 THEN assume_tac 2);
```
```   608 by (fast_tac (claset() addSEs [less_SucE] addss simpset()) 1);
```
```   609 by (best_tac (claset() addDs [mult_less_mono2] addss simpset()) 1);
```
```   610 qed "mult_eq_self_implies_10";
```
```   611
```
```   612
```
```   613 (*** Subtraction laws -- mostly from Clemens Ballarin ***)
```
```   614
```
```   615 Goal "!! a b c::nat. [| a < b; c <= a |] ==> a-c < b-c";
```
```   616 by (subgoal_tac "c+(a-c) < c+(b-c)" 1);
```
```   617 by (Full_simp_tac 1);
```
```   618 by (subgoal_tac "c <= b" 1);
```
```   619 by (blast_tac (claset() addIs [less_imp_le, le_trans]) 2);
```
```   620 by (Asm_simp_tac 1);
```
```   621 qed "diff_less_mono";
```
```   622
```
```   623 Goal "!! a b c::nat. a+b < c ==> a < c-b";
```
```   624 by (dtac diff_less_mono 1);
```
```   625 by (rtac le_add2 1);
```
```   626 by (Asm_full_simp_tac 1);
```
```   627 qed "add_less_imp_less_diff";
```
```   628
```
```   629 Goal "!! n. n <= m ==> Suc m - n = Suc (m - n)";
```
```   630 by (asm_full_simp_tac (simpset() addsimps [Suc_diff_n, le_eq_less_Suc]) 1);
```
```   631 qed "Suc_diff_le";
```
```   632
```
```   633 Goal "!! n. Suc i <= n ==> Suc (n - Suc i) = n - i";
```
```   634 by (asm_full_simp_tac
```
```   635     (simpset() addsimps [Suc_diff_n RS sym, le_eq_less_Suc]) 1);
```
```   636 qed "Suc_diff_Suc";
```
```   637
```
```   638 Goal "!! i::nat. i <= n ==> n - (n - i) = i";
```
```   639 by (etac rev_mp 1);
```
```   640 by (res_inst_tac [("m","n"),("n","i")] diff_induct 1);
```
```   641 by (ALLGOALS (asm_simp_tac  (simpset() addsimps [Suc_diff_le])));
```
```   642 qed "diff_diff_cancel";
```
```   643 Addsimps [diff_diff_cancel];
```
```   644
```
```   645 Goal "!!k::nat. k <= n ==> m <= n + m - k";
```
```   646 by (etac rev_mp 1);
```
```   647 by (res_inst_tac [("m", "k"), ("n", "n")] diff_induct 1);
```
```   648 by (Simp_tac 1);
```
```   649 by (simp_tac (simpset() addsimps [less_add_Suc2, less_imp_le]) 1);
```
```   650 by (Simp_tac 1);
```
```   651 qed "le_add_diff";
```
```   652
```
```   653 Goal "!!i::nat. 0<k ==> j<i --> j+k-i < k";
```
```   654 by (res_inst_tac [("m","j"),("n","i")] diff_induct 1);
```
```   655 by (ALLGOALS Asm_simp_tac);
```
```   656 qed_spec_mp "add_diff_less";
```
```   657
```
```   658
```
```   659
```
```   660 (** (Anti)Monotonicity of subtraction -- by Stefan Merz **)
```
```   661
```
```   662 (* Monotonicity of subtraction in first argument *)
```
```   663 Goal "!!n::nat. m<=n --> (m-l) <= (n-l)";
```
```   664 by (induct_tac "n" 1);
```
```   665 by (Simp_tac 1);
```
```   666 by (simp_tac (simpset() addsimps [le_Suc_eq]) 1);
```
```   667 by (blast_tac (claset() addIs [diff_le_Suc_diff, le_trans]) 1);
```
```   668 qed_spec_mp "diff_le_mono";
```
```   669
```
```   670 Goal "!!n::nat. m<=n ==> (l-n) <= (l-m)";
```
```   671 by (induct_tac "l" 1);
```
```   672 by (Simp_tac 1);
```
```   673 by (case_tac "n <= l" 1);
```
```   674 by (subgoal_tac "m <= l" 1);
```
```   675 by (asm_simp_tac (simpset() addsimps [Suc_diff_le]) 1);
```
```   676 by (fast_tac (claset() addEs [le_trans]) 1);
```
```   677 by (dtac not_leE 1);
```
```   678 by (asm_simp_tac (simpset() addsimps [if_Suc_diff_n]) 1);
```
```   679 qed_spec_mp "diff_le_mono2";
```