src/HOL/Arith.ML
 author paulson Tue May 27 13:03:41 1997 +0200 (1997-05-27) changeset 3352 04502e5431fb parent 3339 cfa72a70f2b5 child 3366 2402c6ab1561 permissions -rw-r--r--
New theorems suggested by Florian Kammueller
```     1 (*  Title:      HOL/Arith.ML
```
```     2     ID:         \$Id\$
```
```     3     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
```
```     4     Copyright   1993  University of Cambridge
```
```     5
```
```     6 Proofs about elementary arithmetic: addition, multiplication, etc.
```
```     7 Some from the Hoare example from Norbert Galm
```
```     8 *)
```
```     9
```
```    10 open Arith;
```
```    11
```
```    12 (*** Basic rewrite rules for the arithmetic operators ***)
```
```    13
```
```    14 goalw Arith.thy [pred_def] "pred 0 = 0";
```
```    15 by(Simp_tac 1);
```
```    16 qed "pred_0";
```
```    17
```
```    18 goalw Arith.thy [pred_def] "pred(Suc n) = n";
```
```    19 by(Simp_tac 1);
```
```    20 qed "pred_Suc";
```
```    21
```
```    22 Addsimps [pred_0,pred_Suc];
```
```    23
```
```    24 (** pred **)
```
```    25
```
```    26 val prems = goal Arith.thy "n ~= 0 ==> Suc(pred n) = n";
```
```    27 by (res_inst_tac [("n","n")] natE 1);
```
```    28 by (cut_facts_tac prems 1);
```
```    29 by (ALLGOALS Asm_full_simp_tac);
```
```    30 qed "Suc_pred";
```
```    31 Addsimps [Suc_pred];
```
```    32
```
```    33 (** Difference **)
```
```    34
```
```    35 qed_goalw "diff_0_eq_0" Arith.thy [pred_def]
```
```    36     "0 - n = 0"
```
```    37  (fn _ => [induct_tac "n" 1,  ALLGOALS Asm_simp_tac]);
```
```    38
```
```    39 (*Must simplify BEFORE the induction!!  (Else we get a critical pair)
```
```    40   Suc(m) - Suc(n)   rewrites to   pred(Suc(m) - n)  *)
```
```    41 qed_goalw "diff_Suc_Suc" Arith.thy [pred_def]
```
```    42     "Suc(m) - Suc(n) = m - n"
```
```    43  (fn _ =>
```
```    44   [Simp_tac 1, induct_tac "n" 1, ALLGOALS Asm_simp_tac]);
```
```    45
```
```    46 Addsimps [diff_0_eq_0, diff_Suc_Suc];
```
```    47
```
```    48
```
```    49 (**** Inductive properties of the operators ****)
```
```    50
```
```    51 (*** Addition ***)
```
```    52
```
```    53 qed_goal "add_0_right" Arith.thy "m + 0 = m"
```
```    54  (fn _ => [induct_tac "m" 1, ALLGOALS Asm_simp_tac]);
```
```    55
```
```    56 qed_goal "add_Suc_right" Arith.thy "m + Suc(n) = Suc(m+n)"
```
```    57  (fn _ => [induct_tac "m" 1, ALLGOALS Asm_simp_tac]);
```
```    58
```
```    59 Addsimps [add_0_right,add_Suc_right];
```
```    60
```
```    61 (*Associative law for addition*)
```
```    62 qed_goal "add_assoc" Arith.thy "(m + n) + k = m + ((n + k)::nat)"
```
```    63  (fn _ => [induct_tac "m" 1, ALLGOALS Asm_simp_tac]);
```
```    64
```
```    65 (*Commutative law for addition*)
```
```    66 qed_goal "add_commute" Arith.thy "m + n = n + (m::nat)"
```
```    67  (fn _ =>  [induct_tac "m" 1, ALLGOALS Asm_simp_tac]);
```
```    68
```
```    69 qed_goal "add_left_commute" Arith.thy "x+(y+z)=y+((x+z)::nat)"
```
```    70  (fn _ => [rtac (add_commute RS trans) 1, rtac (add_assoc RS trans) 1,
```
```    71            rtac (add_commute RS arg_cong) 1]);
```
```    72
```
```    73 (*Addition is an AC-operator*)
```
```    74 val add_ac = [add_assoc, add_commute, add_left_commute];
```
```    75
```
```    76 goal Arith.thy "!!k::nat. (k + m = k + n) = (m=n)";
```
```    77 by (induct_tac "k" 1);
```
```    78 by (Simp_tac 1);
```
```    79 by (Asm_simp_tac 1);
```
```    80 qed "add_left_cancel";
```
```    81
```
```    82 goal Arith.thy "!!k::nat. (m + k = n + k) = (m=n)";
```
```    83 by (induct_tac "k" 1);
```
```    84 by (Simp_tac 1);
```
```    85 by (Asm_simp_tac 1);
```
```    86 qed "add_right_cancel";
```
```    87
```
```    88 goal Arith.thy "!!k::nat. (k + m <= k + n) = (m<=n)";
```
```    89 by (induct_tac "k" 1);
```
```    90 by (Simp_tac 1);
```
```    91 by (Asm_simp_tac 1);
```
```    92 qed "add_left_cancel_le";
```
```    93
```
```    94 goal Arith.thy "!!k::nat. (k + m < k + n) = (m<n)";
```
```    95 by (induct_tac "k" 1);
```
```    96 by (Simp_tac 1);
```
```    97 by (Asm_simp_tac 1);
```
```    98 qed "add_left_cancel_less";
```
```    99
```
```   100 Addsimps [add_left_cancel, add_right_cancel,
```
```   101           add_left_cancel_le, add_left_cancel_less];
```
```   102
```
```   103 (** Reasoning about m+0=0, etc. **)
```
```   104
```
```   105 goal Arith.thy "(m+n = 0) = (m=0 & n=0)";
```
```   106 by (induct_tac "m" 1);
```
```   107 by (ALLGOALS Asm_simp_tac);
```
```   108 qed "add_is_0";
```
```   109 Addsimps [add_is_0];
```
```   110
```
```   111 goal Arith.thy "(pred (m+n) = 0) = (m=0 & pred n = 0 | pred m = 0 & n=0)";
```
```   112 by (induct_tac "m" 1);
```
```   113 by (ALLGOALS (fast_tac (!claset addss (!simpset))));
```
```   114 qed "pred_add_is_0";
```
```   115 Addsimps [pred_add_is_0];
```
```   116
```
```   117 goal Arith.thy "!!n. n ~= 0 ==> m + pred n = pred(m+n)";
```
```   118 by (induct_tac "m" 1);
```
```   119 by (ALLGOALS Asm_simp_tac);
```
```   120 qed "add_pred";
```
```   121 Addsimps [add_pred];
```
```   122
```
```   123
```
```   124 (**** Additional theorems about "less than" ****)
```
```   125
```
```   126 goal Arith.thy "i<j --> (EX k. j = Suc(i+k))";
```
```   127 by (induct_tac "j" 1);
```
```   128 by (Simp_tac 1);
```
```   129 by (blast_tac (!claset addSEs [less_SucE]
```
```   130                        addSIs [add_0_right RS sym, add_Suc_right RS sym]) 1);
```
```   131 val lemma = result();
```
```   132
```
```   133 (* [| i<j;  !!x. j = Suc(i+x) ==> Q |] ==> Q *)
```
```   134 bind_thm ("less_natE", lemma RS mp RS exE);
```
```   135
```
```   136 goal Arith.thy "!!m. m<n --> (? k. n=Suc(m+k))";
```
```   137 by (induct_tac "n" 1);
```
```   138 by (ALLGOALS (simp_tac (!simpset addsimps [less_Suc_eq])));
```
```   139 by (blast_tac (!claset addSEs [less_SucE]
```
```   140                        addSIs [add_0_right RS sym, add_Suc_right RS sym]) 1);
```
```   141 qed_spec_mp "less_eq_Suc_add";
```
```   142
```
```   143 goal Arith.thy "n <= ((m + n)::nat)";
```
```   144 by (induct_tac "m" 1);
```
```   145 by (ALLGOALS Simp_tac);
```
```   146 by (etac le_trans 1);
```
```   147 by (rtac (lessI RS less_imp_le) 1);
```
```   148 qed "le_add2";
```
```   149
```
```   150 goal Arith.thy "n <= ((n + m)::nat)";
```
```   151 by (simp_tac (!simpset addsimps add_ac) 1);
```
```   152 by (rtac le_add2 1);
```
```   153 qed "le_add1";
```
```   154
```
```   155 bind_thm ("less_add_Suc1", (lessI RS (le_add1 RS le_less_trans)));
```
```   156 bind_thm ("less_add_Suc2", (lessI RS (le_add2 RS le_less_trans)));
```
```   157
```
```   158 (*"i <= j ==> i <= j+m"*)
```
```   159 bind_thm ("trans_le_add1", le_add1 RSN (2,le_trans));
```
```   160
```
```   161 (*"i <= j ==> i <= m+j"*)
```
```   162 bind_thm ("trans_le_add2", le_add2 RSN (2,le_trans));
```
```   163
```
```   164 (*"i < j ==> i < j+m"*)
```
```   165 bind_thm ("trans_less_add1", le_add1 RSN (2,less_le_trans));
```
```   166
```
```   167 (*"i < j ==> i < m+j"*)
```
```   168 bind_thm ("trans_less_add2", le_add2 RSN (2,less_le_trans));
```
```   169
```
```   170 goal Arith.thy "!!i. i+j < (k::nat) ==> i<k";
```
```   171 by (etac rev_mp 1);
```
```   172 by (induct_tac "j" 1);
```
```   173 by (ALLGOALS Asm_simp_tac);
```
```   174 by (blast_tac (!claset addDs [Suc_lessD]) 1);
```
```   175 qed "add_lessD1";
```
```   176
```
```   177 goal Arith.thy "!!i::nat. ~ (i+j < i)";
```
```   178 br notI 1;
```
```   179 be (add_lessD1 RS less_irrefl) 1;
```
```   180 qed "not_add_less1";
```
```   181
```
```   182 goal Arith.thy "!!i::nat. ~ (j+i < i)";
```
```   183 by (simp_tac (!simpset addsimps [add_commute, not_add_less1]) 1);
```
```   184 qed "not_add_less2";
```
```   185 AddIffs [not_add_less1, not_add_less2];
```
```   186
```
```   187 goal Arith.thy "!!k::nat. m <= n ==> m <= n+k";
```
```   188 by (etac le_trans 1);
```
```   189 by (rtac le_add1 1);
```
```   190 qed "le_imp_add_le";
```
```   191
```
```   192 goal Arith.thy "!!k::nat. m < n ==> m < n+k";
```
```   193 by (etac less_le_trans 1);
```
```   194 by (rtac le_add1 1);
```
```   195 qed "less_imp_add_less";
```
```   196
```
```   197 goal Arith.thy "m+k<=n --> m<=(n::nat)";
```
```   198 by (induct_tac "k" 1);
```
```   199 by (ALLGOALS Asm_simp_tac);
```
```   200 by (blast_tac (!claset addDs [Suc_leD]) 1);
```
```   201 qed_spec_mp "add_leD1";
```
```   202
```
```   203 goal Arith.thy "!!n::nat. m+k<=n ==> k<=n";
```
```   204 by (full_simp_tac (!simpset addsimps [add_commute]) 1);
```
```   205 by (etac add_leD1 1);
```
```   206 qed_spec_mp "add_leD2";
```
```   207
```
```   208 goal Arith.thy "!!n::nat. m+k<=n ==> m<=n & k<=n";
```
```   209 by (blast_tac (!claset addDs [add_leD1, add_leD2]) 1);
```
```   210 bind_thm ("add_leE", result() RS conjE);
```
```   211
```
```   212 goal Arith.thy "!!k l::nat. [| k<l; m+l = k+n |] ==> m<n";
```
```   213 by (safe_tac (!claset addSDs [less_eq_Suc_add]));
```
```   214 by (asm_full_simp_tac
```
```   215     (!simpset delsimps [add_Suc_right]
```
```   216                 addsimps ([add_Suc_right RS sym, add_left_cancel] @add_ac)) 1);
```
```   217 by (etac subst 1);
```
```   218 by (simp_tac (!simpset addsimps [less_add_Suc1]) 1);
```
```   219 qed "less_add_eq_less";
```
```   220
```
```   221
```
```   222 (*** Monotonicity of Addition ***)
```
```   223
```
```   224 (*strict, in 1st argument*)
```
```   225 goal Arith.thy "!!i j k::nat. i < j ==> i + k < j + k";
```
```   226 by (induct_tac "k" 1);
```
```   227 by (ALLGOALS Asm_simp_tac);
```
```   228 qed "add_less_mono1";
```
```   229
```
```   230 (*strict, in both arguments*)
```
```   231 goal Arith.thy "!!i j k::nat. [|i < j; k < l|] ==> i + k < j + l";
```
```   232 by (rtac (add_less_mono1 RS less_trans) 1);
```
```   233 by (REPEAT (assume_tac 1));
```
```   234 by (induct_tac "j" 1);
```
```   235 by (ALLGOALS Asm_simp_tac);
```
```   236 qed "add_less_mono";
```
```   237
```
```   238 (*A [clumsy] way of lifting < monotonicity to <= monotonicity *)
```
```   239 val [lt_mono,le] = goal Arith.thy
```
```   240      "[| !!i j::nat. i<j ==> f(i) < f(j);       \
```
```   241 \        i <= j                                 \
```
```   242 \     |] ==> f(i) <= (f(j)::nat)";
```
```   243 by (cut_facts_tac [le] 1);
```
```   244 by (asm_full_simp_tac (!simpset addsimps [le_eq_less_or_eq]) 1);
```
```   245 by (blast_tac (!claset addSIs [lt_mono]) 1);
```
```   246 qed "less_mono_imp_le_mono";
```
```   247
```
```   248 (*non-strict, in 1st argument*)
```
```   249 goal Arith.thy "!!i j k::nat. i<=j ==> i + k <= j + k";
```
```   250 by (res_inst_tac [("f", "%j.j+k")] less_mono_imp_le_mono 1);
```
```   251 by (etac add_less_mono1 1);
```
```   252 by (assume_tac 1);
```
```   253 qed "add_le_mono1";
```
```   254
```
```   255 (*non-strict, in both arguments*)
```
```   256 goal Arith.thy "!!k l::nat. [|i<=j;  k<=l |] ==> i + k <= j + l";
```
```   257 by (etac (add_le_mono1 RS le_trans) 1);
```
```   258 by (simp_tac (!simpset addsimps [add_commute]) 1);
```
```   259 (*j moves to the end because it is free while k, l are bound*)
```
```   260 by (etac add_le_mono1 1);
```
```   261 qed "add_le_mono";
```
```   262
```
```   263
```
```   264 (*** Multiplication ***)
```
```   265
```
```   266 (*right annihilation in product*)
```
```   267 qed_goal "mult_0_right" Arith.thy "m * 0 = 0"
```
```   268  (fn _ => [induct_tac "m" 1, ALLGOALS Asm_simp_tac]);
```
```   269
```
```   270 (*right successor law for multiplication*)
```
```   271 qed_goal "mult_Suc_right" Arith.thy  "m * Suc(n) = m + (m * n)"
```
```   272  (fn _ => [induct_tac "m" 1,
```
```   273            ALLGOALS(asm_simp_tac (!simpset addsimps add_ac))]);
```
```   274
```
```   275 Addsimps [mult_0_right, mult_Suc_right];
```
```   276
```
```   277 goal Arith.thy "1 * n = n";
```
```   278 by (Asm_simp_tac 1);
```
```   279 qed "mult_1";
```
```   280
```
```   281 goal Arith.thy "n * 1 = n";
```
```   282 by (Asm_simp_tac 1);
```
```   283 qed "mult_1_right";
```
```   284
```
```   285 (*Commutative law for multiplication*)
```
```   286 qed_goal "mult_commute" Arith.thy "m * n = n * (m::nat)"
```
```   287  (fn _ => [induct_tac "m" 1, ALLGOALS Asm_simp_tac]);
```
```   288
```
```   289 (*addition distributes over multiplication*)
```
```   290 qed_goal "add_mult_distrib" Arith.thy "(m + n)*k = (m*k) + ((n*k)::nat)"
```
```   291  (fn _ => [induct_tac "m" 1,
```
```   292            ALLGOALS(asm_simp_tac (!simpset addsimps add_ac))]);
```
```   293
```
```   294 qed_goal "add_mult_distrib2" Arith.thy "k*(m + n) = (k*m) + ((k*n)::nat)"
```
```   295  (fn _ => [induct_tac "m" 1,
```
```   296            ALLGOALS(asm_simp_tac (!simpset addsimps add_ac))]);
```
```   297
```
```   298 (*Associative law for multiplication*)
```
```   299 qed_goal "mult_assoc" Arith.thy "(m * n) * k = m * ((n * k)::nat)"
```
```   300   (fn _ => [induct_tac "m" 1,
```
```   301             ALLGOALS (asm_simp_tac (!simpset addsimps [add_mult_distrib]))]);
```
```   302
```
```   303 qed_goal "mult_left_commute" Arith.thy "x*(y*z) = y*((x*z)::nat)"
```
```   304  (fn _ => [rtac trans 1, rtac mult_commute 1, rtac trans 1,
```
```   305            rtac mult_assoc 1, rtac (mult_commute RS arg_cong) 1]);
```
```   306
```
```   307 val mult_ac = [mult_assoc,mult_commute,mult_left_commute];
```
```   308
```
```   309 goal Arith.thy "(m*n = 0) = (m=0 | n=0)";
```
```   310 by (induct_tac "m" 1);
```
```   311 by (induct_tac "n" 2);
```
```   312 by (ALLGOALS Asm_simp_tac);
```
```   313 qed "mult_is_0";
```
```   314 Addsimps [mult_is_0];
```
```   315
```
```   316
```
```   317 (*** Difference ***)
```
```   318
```
```   319 qed_goal "pred_Suc_diff" Arith.thy "pred(Suc m - n) = m - n"
```
```   320  (fn _ => [induct_tac "n" 1, ALLGOALS Asm_simp_tac]);
```
```   321 Addsimps [pred_Suc_diff];
```
```   322
```
```   323 qed_goal "diff_self_eq_0" Arith.thy "m - m = 0"
```
```   324  (fn _ => [induct_tac "m" 1, ALLGOALS Asm_simp_tac]);
```
```   325 Addsimps [diff_self_eq_0];
```
```   326
```
```   327 (*Addition is the inverse of subtraction: if n<=m then n+(m-n) = m. *)
```
```   328 val [prem] = goal Arith.thy "~ m<n ==> n+(m-n) = (m::nat)";
```
```   329 by (rtac (prem RS rev_mp) 1);
```
```   330 by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
```
```   331 by (ALLGOALS Asm_simp_tac);
```
```   332 qed "add_diff_inverse";
```
```   333
```
```   334 Delsimps  [diff_Suc];
```
```   335
```
```   336
```
```   337 (*** More results about difference ***)
```
```   338
```
```   339 val [prem] = goal Arith.thy "n < Suc(m) ==> Suc(m)-n = Suc(m-n)";
```
```   340 by (rtac (prem RS rev_mp) 1);
```
```   341 by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
```
```   342 by (ALLGOALS Asm_simp_tac);
```
```   343 qed "Suc_diff_n";
```
```   344
```
```   345 goal Arith.thy "m - n < Suc(m)";
```
```   346 by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
```
```   347 by (etac less_SucE 3);
```
```   348 by (ALLGOALS (asm_simp_tac (!simpset addsimps [less_Suc_eq])));
```
```   349 qed "diff_less_Suc";
```
```   350
```
```   351 goal Arith.thy "!!m::nat. m - n <= m";
```
```   352 by (res_inst_tac [("m","m"), ("n","n")] diff_induct 1);
```
```   353 by (ALLGOALS Asm_simp_tac);
```
```   354 qed "diff_le_self";
```
```   355
```
```   356 goal Arith.thy "!!i::nat. i-j-k = i - (j+k)";
```
```   357 by (res_inst_tac [("m","i"),("n","j")] diff_induct 1);
```
```   358 by (ALLGOALS Asm_simp_tac);
```
```   359 qed "diff_diff_left";
```
```   360
```
```   361 (*This and the next few suggested by Florian Kammüller*)
```
```   362 goal Arith.thy "!!i::nat. i-j-k = i-k-j";
```
```   363 by (simp_tac (!simpset addsimps [diff_diff_left, add_commute]) 1);
```
```   364 qed "diff_commute";
```
```   365
```
```   366 goal Arith.thy "!!i j k:: nat. k<=j --> j<=i --> i - (j - k) = i - j + k";
```
```   367 by (res_inst_tac [("m","i"),("n","j")] diff_induct 1);
```
```   368 by (ALLGOALS Asm_simp_tac);
```
```   369 by (asm_simp_tac
```
```   370     (!simpset addsimps [Suc_diff_n, le_imp_less_Suc, le_Suc_eq]) 1);
```
```   371 by (simp_tac
```
```   372     (!simpset addsimps [add_diff_inverse, not_less_iff_le, add_commute]) 1);
```
```   373 qed_spec_mp "diff_diff_right";
```
```   374
```
```   375 goal Arith.thy "!!i j k:: nat. k<=j --> (i + j) - k = i + (j - k)";
```
```   376 by (res_inst_tac [("m","j"),("n","k")] diff_induct 1);
```
```   377 by (ALLGOALS Asm_simp_tac);
```
```   378 qed_spec_mp "diff_add_assoc";
```
```   379
```
```   380 goal Arith.thy "!!n::nat. (n+m) - n = m";
```
```   381 by (induct_tac "n" 1);
```
```   382 by (ALLGOALS Asm_simp_tac);
```
```   383 qed "diff_add_inverse";
```
```   384 Addsimps [diff_add_inverse];
```
```   385
```
```   386 goal Arith.thy "!!n::nat.(m+n) - n = m";
```
```   387 by (simp_tac (!simpset addsimps [diff_add_assoc]) 1);
```
```   388 qed "diff_add_inverse2";
```
```   389 Addsimps [diff_add_inverse2];
```
```   390
```
```   391 val [prem] = goal Arith.thy "m < Suc(n) ==> m-n = 0";
```
```   392 by (rtac (prem RS rev_mp) 1);
```
```   393 by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
```
```   394 by (asm_simp_tac (!simpset addsimps [less_Suc_eq]) 1);
```
```   395 by (ALLGOALS Asm_simp_tac);
```
```   396 qed "less_imp_diff_is_0";
```
```   397
```
```   398 val prems = goal Arith.thy "m-n = 0  -->  n-m = 0  -->  m=n";
```
```   399 by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
```
```   400 by (REPEAT(Simp_tac 1 THEN TRY(atac 1)));
```
```   401 qed_spec_mp "diffs0_imp_equal";
```
```   402
```
```   403 val [prem] = goal Arith.thy "m<n ==> 0<n-m";
```
```   404 by (rtac (prem RS rev_mp) 1);
```
```   405 by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
```
```   406 by (ALLGOALS Asm_simp_tac);
```
```   407 qed "less_imp_diff_positive";
```
```   408
```
```   409 goal Arith.thy "Suc(m)-n = (if m<n then 0 else Suc(m-n))";
```
```   410 by (simp_tac (!simpset addsimps [less_imp_diff_is_0, not_less_eq, Suc_diff_n]
```
```   411                     setloop (split_tac [expand_if])) 1);
```
```   412 qed "if_Suc_diff_n";
```
```   413
```
```   414 goal Arith.thy "P(k) --> (!n. P(Suc(n))--> P(n)) --> P(k-i)";
```
```   415 by (res_inst_tac [("m","k"),("n","i")] diff_induct 1);
```
```   416 by (ALLGOALS (strip_tac THEN' Simp_tac THEN' TRY o Blast_tac));
```
```   417 qed "zero_induct_lemma";
```
```   418
```
```   419 val prems = goal Arith.thy "[| P(k);  !!n. P(Suc(n)) ==> P(n) |] ==> P(0)";
```
```   420 by (rtac (diff_self_eq_0 RS subst) 1);
```
```   421 by (rtac (zero_induct_lemma RS mp RS mp) 1);
```
```   422 by (REPEAT (ares_tac ([impI,allI]@prems) 1));
```
```   423 qed "zero_induct";
```
```   424
```
```   425 goal Arith.thy "!!k::nat. (k+m) - (k+n) = m - n";
```
```   426 by (induct_tac "k" 1);
```
```   427 by (ALLGOALS Asm_simp_tac);
```
```   428 qed "diff_cancel";
```
```   429 Addsimps [diff_cancel];
```
```   430
```
```   431 goal Arith.thy "!!m::nat. (m+k) - (n+k) = m - n";
```
```   432 val add_commute_k = read_instantiate [("n","k")] add_commute;
```
```   433 by (asm_simp_tac (!simpset addsimps ([add_commute_k])) 1);
```
```   434 qed "diff_cancel2";
```
```   435 Addsimps [diff_cancel2];
```
```   436
```
```   437 (*From Clemens Ballarin*)
```
```   438 goal Arith.thy "!!n::nat. [| k<=n; n<=m |] ==> (m-k) - (n-k) = m-n";
```
```   439 by (subgoal_tac "k<=n --> n<=m --> (m-k) - (n-k) = m-n" 1);
```
```   440 by (Asm_full_simp_tac 1);
```
```   441 by (induct_tac "k" 1);
```
```   442 by (Simp_tac 1);
```
```   443 (* Induction step *)
```
```   444 by (subgoal_tac "Suc na <= m --> n <= m --> Suc na <= n --> \
```
```   445 \                Suc (m - Suc na) - Suc (n - Suc na) = m-n" 1);
```
```   446 by (Asm_full_simp_tac 1);
```
```   447 by (blast_tac (!claset addIs [le_trans]) 1);
```
```   448 by (auto_tac (!claset addIs [Suc_leD], !simpset delsimps [diff_Suc_Suc]));
```
```   449 by (asm_full_simp_tac (!simpset delsimps [Suc_less_eq]
```
```   450 		       addsimps [Suc_diff_n RS sym, le_eq_less_Suc]) 1);
```
```   451 qed "diff_right_cancel";
```
```   452
```
```   453 goal Arith.thy "!!n::nat. n - (n+m) = 0";
```
```   454 by (induct_tac "n" 1);
```
```   455 by (ALLGOALS Asm_simp_tac);
```
```   456 qed "diff_add_0";
```
```   457 Addsimps [diff_add_0];
```
```   458
```
```   459 (** Difference distributes over multiplication **)
```
```   460
```
```   461 goal Arith.thy "!!m::nat. (m - n) * k = (m * k) - (n * k)";
```
```   462 by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
```
```   463 by (ALLGOALS Asm_simp_tac);
```
```   464 qed "diff_mult_distrib" ;
```
```   465
```
```   466 goal Arith.thy "!!m::nat. k * (m - n) = (k * m) - (k * n)";
```
```   467 val mult_commute_k = read_instantiate [("m","k")] mult_commute;
```
```   468 by (simp_tac (!simpset addsimps [diff_mult_distrib, mult_commute_k]) 1);
```
```   469 qed "diff_mult_distrib2" ;
```
```   470 (*NOT added as rewrites, since sometimes they are used from right-to-left*)
```
```   471
```
```   472
```
```   473 (** Less-then properties **)
```
```   474
```
```   475 (*In ordinary notation: if 0<n and n<=m then m-n < m *)
```
```   476 goal Arith.thy "!!m. [| 0<n; ~ m<n |] ==> m - n < m";
```
```   477 by (subgoal_tac "0<n --> ~ m<n --> m - n < m" 1);
```
```   478 by (Blast_tac 1);
```
```   479 by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
```
```   480 by (ALLGOALS(asm_simp_tac(!simpset addsimps [diff_less_Suc])));
```
```   481 qed "diff_less";
```
```   482
```
```   483 val wf_less_trans = [eq_reflection, wf_pred_nat RS wf_trancl] MRS
```
```   484                     def_wfrec RS trans;
```
```   485
```
```   486 goalw Nat.thy [less_def] "(m,n) : pred_nat^+ = (m<n)";
```
```   487 by (rtac refl 1);
```
```   488 qed "less_eq";
```
```   489
```
```   490 (*** Remainder ***)
```
```   491
```
```   492 goal Arith.thy "(%m. m mod n) = wfrec (trancl pred_nat) \
```
```   493              \                      (%f j. if j<n then j else f (j-n))";
```
```   494 by (simp_tac (!simpset addsimps [mod_def]) 1);
```
```   495 qed "mod_eq";
```
```   496
```
```   497 goal Arith.thy "!!m. m<n ==> m mod n = m";
```
```   498 by (rtac (mod_eq RS wf_less_trans) 1);
```
```   499 by (Asm_simp_tac 1);
```
```   500 qed "mod_less";
```
```   501
```
```   502 goal Arith.thy "!!m. [| 0<n;  ~m<n |] ==> m mod n = (m-n) mod n";
```
```   503 by (rtac (mod_eq RS wf_less_trans) 1);
```
```   504 by (asm_simp_tac (!simpset addsimps [diff_less, cut_apply, less_eq]) 1);
```
```   505 qed "mod_geq";
```
```   506
```
```   507 goal thy "!!n. 0<n ==> n mod n = 0";
```
```   508 by (rtac (mod_eq RS wf_less_trans) 1);
```
```   509 by (asm_simp_tac (!simpset addsimps [mod_less, diff_self_eq_0,
```
```   510 				     cut_def, less_eq]) 1);
```
```   511 qed "mod_nn_is_0";
```
```   512
```
```   513 goal thy "!!n. 0<n ==> (m+n) mod n = m mod n";
```
```   514 by (subgoal_tac "(n + m) mod n = (n+m-n) mod n" 1);
```
```   515 by (stac (mod_geq RS sym) 2);
```
```   516 by (ALLGOALS (asm_full_simp_tac (!simpset addsimps [add_commute])));
```
```   517 qed "mod_eq_add";
```
```   518
```
```   519 goal thy "!!n. 0<n ==> m*n mod n = 0";
```
```   520 by (induct_tac "m" 1);
```
```   521 by (asm_simp_tac (!simpset addsimps [mod_less]) 1);
```
```   522 by (dres_inst_tac [("m","m*n")] mod_eq_add 1);
```
```   523 by (asm_full_simp_tac (!simpset addsimps [add_commute]) 1);
```
```   524 qed "mod_prod_nn_is_0";
```
```   525
```
```   526
```
```   527 (*** Quotient ***)
```
```   528
```
```   529 goal Arith.thy "(%m. m div n) = wfrec (trancl pred_nat) \
```
```   530                         \            (%f j. if j<n then 0 else Suc (f (j-n)))";
```
```   531 by (simp_tac (!simpset addsimps [div_def]) 1);
```
```   532 qed "div_eq";
```
```   533
```
```   534 goal Arith.thy "!!m. m<n ==> m div n = 0";
```
```   535 by (rtac (div_eq RS wf_less_trans) 1);
```
```   536 by (Asm_simp_tac 1);
```
```   537 qed "div_less";
```
```   538
```
```   539 goal Arith.thy "!!M. [| 0<n;  ~m<n |] ==> m div n = Suc((m-n) div n)";
```
```   540 by (rtac (div_eq RS wf_less_trans) 1);
```
```   541 by (asm_simp_tac (!simpset addsimps [diff_less, cut_apply, less_eq]) 1);
```
```   542 qed "div_geq";
```
```   543
```
```   544 (*Main Result about quotient and remainder.*)
```
```   545 goal Arith.thy "!!m. 0<n ==> (m div n)*n + m mod n = m";
```
```   546 by (res_inst_tac [("n","m")] less_induct 1);
```
```   547 by (rename_tac "k" 1);    (*Variable name used in line below*)
```
```   548 by (case_tac "k<n" 1);
```
```   549 by (ALLGOALS (asm_simp_tac(!simpset addsimps ([add_assoc] @
```
```   550                        [mod_less, mod_geq, div_less, div_geq,
```
```   551                         add_diff_inverse, diff_less]))));
```
```   552 qed "mod_div_equality";
```
```   553
```
```   554
```
```   555 (*** Further facts about mod (mainly for the mutilated chess board ***)
```
```   556
```
```   557 goal Arith.thy
```
```   558     "!!m n. 0<n ==> \
```
```   559 \           Suc(m) mod n = (if Suc(m mod n) = n then 0 else Suc(m mod n))";
```
```   560 by (res_inst_tac [("n","m")] less_induct 1);
```
```   561 by (excluded_middle_tac "Suc(na)<n" 1);
```
```   562 (* case Suc(na) < n *)
```
```   563 by (forward_tac [lessI RS less_trans] 2);
```
```   564 by (asm_simp_tac (!simpset addsimps [mod_less, less_not_refl2 RS not_sym]) 2);
```
```   565 (* case n <= Suc(na) *)
```
```   566 by (asm_full_simp_tac (!simpset addsimps [not_less_iff_le, mod_geq]) 1);
```
```   567 by (etac (le_imp_less_or_eq RS disjE) 1);
```
```   568 by (asm_simp_tac (!simpset addsimps [Suc_diff_n]) 1);
```
```   569 by (asm_full_simp_tac (!simpset addsimps [not_less_eq RS sym,
```
```   570                                           diff_less, mod_geq]) 1);
```
```   571 by (asm_simp_tac (!simpset addsimps [mod_less]) 1);
```
```   572 qed "mod_Suc";
```
```   573
```
```   574 goal Arith.thy "!!m n. 0<n ==> m mod n < n";
```
```   575 by (res_inst_tac [("n","m")] less_induct 1);
```
```   576 by (excluded_middle_tac "na<n" 1);
```
```   577 (*case na<n*)
```
```   578 by (asm_simp_tac (!simpset addsimps [mod_less]) 2);
```
```   579 (*case n le na*)
```
```   580 by (asm_full_simp_tac (!simpset addsimps [mod_geq, diff_less]) 1);
```
```   581 qed "mod_less_divisor";
```
```   582
```
```   583
```
```   584 (** Evens and Odds **)
```
```   585
```
```   586 (*With less_zeroE, causes case analysis on b<2*)
```
```   587 AddSEs [less_SucE];
```
```   588
```
```   589 goal thy "!!k b. b<2 ==> k mod 2 = b | k mod 2 = (if b=1 then 0 else 1)";
```
```   590 by (subgoal_tac "k mod 2 < 2" 1);
```
```   591 by (asm_simp_tac (!simpset addsimps [mod_less_divisor]) 2);
```
```   592 by (asm_simp_tac (!simpset setloop split_tac [expand_if]) 1);
```
```   593 by (Blast_tac 1);
```
```   594 qed "mod2_cases";
```
```   595
```
```   596 goal thy "Suc(Suc(m)) mod 2 = m mod 2";
```
```   597 by (subgoal_tac "m mod 2 < 2" 1);
```
```   598 by (asm_simp_tac (!simpset addsimps [mod_less_divisor]) 2);
```
```   599 by (Step_tac 1);
```
```   600 by (ALLGOALS (asm_simp_tac (!simpset addsimps [mod_Suc])));
```
```   601 qed "mod2_Suc_Suc";
```
```   602 Addsimps [mod2_Suc_Suc];
```
```   603
```
```   604 goal Arith.thy "!!m. m mod 2 ~= 0 ==> m mod 2 = 1";
```
```   605 by (subgoal_tac "m mod 2 < 2" 1);
```
```   606 by (asm_simp_tac (!simpset addsimps [mod_less_divisor]) 2);
```
```   607 by (safe_tac (!claset addSEs [lessE]));
```
```   608 by (ALLGOALS (blast_tac (!claset addIs [sym])));
```
```   609 qed "mod2_neq_0";
```
```   610
```
```   611 goal thy "(m+m) mod 2 = 0";
```
```   612 by (induct_tac "m" 1);
```
```   613 by (simp_tac (!simpset addsimps [mod_less]) 1);
```
```   614 by (asm_simp_tac (!simpset addsimps [mod2_Suc_Suc, add_Suc_right]) 1);
```
```   615 qed "mod2_add_self";
```
```   616 Addsimps [mod2_add_self];
```
```   617
```
```   618 Delrules [less_SucE];
```
```   619
```
```   620
```
```   621 (*** Monotonicity of Multiplication ***)
```
```   622
```
```   623 goal Arith.thy "!!i::nat. i<=j ==> i*k<=j*k";
```
```   624 by (induct_tac "k" 1);
```
```   625 by (ALLGOALS (asm_simp_tac (!simpset addsimps [add_le_mono])));
```
```   626 qed "mult_le_mono1";
```
```   627
```
```   628 (*<=monotonicity, BOTH arguments*)
```
```   629 goal Arith.thy "!!i::nat. [| i<=j; k<=l |] ==> i*k<=j*l";
```
```   630 by (etac (mult_le_mono1 RS le_trans) 1);
```
```   631 by (rtac le_trans 1);
```
```   632 by (stac mult_commute 2);
```
```   633 by (etac mult_le_mono1 2);
```
```   634 by (simp_tac (!simpset addsimps [mult_commute]) 1);
```
```   635 qed "mult_le_mono";
```
```   636
```
```   637 (*strict, in 1st argument; proof is by induction on k>0*)
```
```   638 goal Arith.thy "!!i::nat. [| i<j; 0<k |] ==> k*i < k*j";
```
```   639 by (eres_inst_tac [("i","0")] less_natE 1);
```
```   640 by (Asm_simp_tac 1);
```
```   641 by (induct_tac "x" 1);
```
```   642 by (ALLGOALS (asm_simp_tac (!simpset addsimps [add_less_mono])));
```
```   643 qed "mult_less_mono2";
```
```   644
```
```   645 goal Arith.thy "!!i::nat. [| i<j; 0<k |] ==> i*k < j*k";
```
```   646 bd mult_less_mono2 1;
```
```   647 by (ALLGOALS (asm_full_simp_tac (!simpset addsimps [mult_commute])));
```
```   648 qed "mult_less_mono1";
```
```   649
```
```   650 goal Arith.thy "(0 < m*n) = (0<m & 0<n)";
```
```   651 by (induct_tac "m" 1);
```
```   652 by (induct_tac "n" 2);
```
```   653 by (ALLGOALS Asm_simp_tac);
```
```   654 qed "zero_less_mult_iff";
```
```   655
```
```   656 goal Arith.thy "(m*n = 1) = (m=1 & n=1)";
```
```   657 by (induct_tac "m" 1);
```
```   658 by (Simp_tac 1);
```
```   659 by (induct_tac "n" 1);
```
```   660 by (Simp_tac 1);
```
```   661 by (fast_tac (!claset addss !simpset) 1);
```
```   662 qed "mult_eq_1_iff";
```
```   663
```
```   664 goal Arith.thy "!!k. 0<k ==> (m*k < n*k) = (m<n)";
```
```   665 by (safe_tac (!claset addSIs [mult_less_mono1]));
```
```   666 by (cut_facts_tac [less_linear] 1);
```
```   667 by (blast_tac (!claset addDs [mult_less_mono1] addEs [less_asym]) 1);
```
```   668 qed "mult_less_cancel2";
```
```   669
```
```   670 goal Arith.thy "!!k. 0<k ==> (k*m < k*n) = (m<n)";
```
```   671 bd mult_less_cancel2 1;
```
```   672 by (asm_full_simp_tac (!simpset addsimps [mult_commute]) 1);
```
```   673 qed "mult_less_cancel1";
```
```   674 Addsimps [mult_less_cancel1, mult_less_cancel2];
```
```   675
```
```   676 goal Arith.thy "!!k. 0<k ==> (m*k = n*k) = (m=n)";
```
```   677 by (cut_facts_tac [less_linear] 1);
```
```   678 by(Step_tac 1);
```
```   679 ba 2;
```
```   680 by (ALLGOALS (dtac mult_less_mono1 THEN' assume_tac));
```
```   681 by (ALLGOALS Asm_full_simp_tac);
```
```   682 qed "mult_cancel2";
```
```   683
```
```   684 goal Arith.thy "!!k. 0<k ==> (k*m = k*n) = (m=n)";
```
```   685 bd mult_cancel2 1;
```
```   686 by (asm_full_simp_tac (!simpset addsimps [mult_commute]) 1);
```
```   687 qed "mult_cancel1";
```
```   688 Addsimps [mult_cancel1, mult_cancel2];
```
```   689
```
```   690
```
```   691 (*** More division laws ***)
```
```   692
```
```   693 goal thy "!!n. 0<n ==> m*n div n = m";
```
```   694 by (cut_inst_tac [("m", "m*n")] mod_div_equality 1);
```
```   695 ba 1;
```
```   696 by (asm_full_simp_tac (!simpset addsimps [mod_prod_nn_is_0]) 1);
```
```   697 qed "div_prod_nn_is_m";
```
```   698 Addsimps [div_prod_nn_is_m];
```
```   699
```
```   700 (*Cancellation law for division*)
```
```   701 goal Arith.thy "!!k. [| 0<n; 0<k |] ==> (k*m) div (k*n) = m div n";
```
```   702 by (res_inst_tac [("n","m")] less_induct 1);
```
```   703 by (case_tac "na<n" 1);
```
```   704 by (asm_simp_tac (!simpset addsimps [div_less, zero_less_mult_iff,
```
```   705                                      mult_less_mono2]) 1);
```
```   706 by (subgoal_tac "~ k*na < k*n" 1);
```
```   707 by (asm_simp_tac
```
```   708      (!simpset addsimps [zero_less_mult_iff, div_geq,
```
```   709                          diff_mult_distrib2 RS sym, diff_less]) 1);
```
```   710 by (asm_full_simp_tac (!simpset addsimps [not_less_iff_le,
```
```   711                                           le_refl RS mult_le_mono]) 1);
```
```   712 qed "div_cancel";
```
```   713 Addsimps [div_cancel];
```
```   714
```
```   715 goal Arith.thy "!!k. [| 0<n; 0<k |] ==> (k*m) mod (k*n) = k * (m mod n)";
```
```   716 by (res_inst_tac [("n","m")] less_induct 1);
```
```   717 by (case_tac "na<n" 1);
```
```   718 by (asm_simp_tac (!simpset addsimps [mod_less, zero_less_mult_iff,
```
```   719                                      mult_less_mono2]) 1);
```
```   720 by (subgoal_tac "~ k*na < k*n" 1);
```
```   721 by (asm_simp_tac
```
```   722      (!simpset addsimps [zero_less_mult_iff, mod_geq,
```
```   723                          diff_mult_distrib2 RS sym, diff_less]) 1);
```
```   724 by (asm_full_simp_tac (!simpset addsimps [not_less_iff_le,
```
```   725                                           le_refl RS mult_le_mono]) 1);
```
```   726 qed "mult_mod_distrib";
```
```   727
```
```   728
```
```   729 (** Lemma for gcd **)
```
```   730
```
```   731 goal Arith.thy "!!m n. m = m*n ==> n=1 | m=0";
```
```   732 by (dtac sym 1);
```
```   733 by (rtac disjCI 1);
```
```   734 by (rtac nat_less_cases 1 THEN assume_tac 2);
```
```   735 by (fast_tac (!claset addSEs [less_SucE] addss !simpset) 1);
```
```   736 by (best_tac (!claset addDs [mult_less_mono2]
```
```   737                       addss (!simpset addsimps [zero_less_eq RS sym])) 1);
```
```   738 qed "mult_eq_self_implies_10";
```
```   739
```
```   740
```
```   741 (*** Subtraction laws -- from Clemens Ballarin ***)
```
```   742
```
```   743 goal Arith.thy "!! a b c::nat. [| a < b; c <= a |] ==> a-c < b-c";
```
```   744 by (subgoal_tac "c+(a-c) < c+(b-c)" 1);
```
```   745 by (Asm_full_simp_tac 1);
```
```   746 by (subgoal_tac "c <= b" 1);
```
```   747 by (blast_tac (!claset addIs [less_imp_le, le_trans]) 2);
```
```   748 by (asm_simp_tac (!simpset addsimps [leD RS add_diff_inverse]) 1);
```
```   749 qed "diff_less_mono";
```
```   750
```
```   751 goal Arith.thy "!! a b c::nat. a+b < c ==> a < c-b";
```
```   752 bd diff_less_mono 1;
```
```   753 br le_add2 1;
```
```   754 by (Asm_full_simp_tac 1);
```
```   755 qed "add_less_imp_less_diff";
```
```   756
```
```   757 goal Arith.thy "!! n. n <= m ==> Suc m - n = Suc (m - n)";
```
```   758 br Suc_diff_n 1;
```
```   759 by (asm_full_simp_tac (!simpset addsimps [le_eq_less_Suc]) 1);
```
```   760 qed "Suc_diff_le";
```
```   761
```
```   762 goal Arith.thy "!! n. Suc i <= n ==> Suc (n - Suc i) = n - i";
```
```   763 by (asm_full_simp_tac
```
```   764     (!simpset addsimps [Suc_diff_n RS sym, le_eq_less_Suc]) 1);
```
```   765 qed "Suc_diff_Suc";
```
```   766
```
```   767 goal Arith.thy "!! i::nat. i <= n ==> n - (n - i) = i";
```
```   768 by (subgoal_tac "(n-i) + (n - (n-i)) = (n-i) + i" 1);
```
```   769 by (Asm_full_simp_tac 1);
```
```   770 by (asm_simp_tac (!simpset addsimps [leD RS add_diff_inverse, diff_le_self,
```
```   771 				     add_commute]) 1);
```
```   772 qed "diff_diff_cancel";
```
```   773
```
```   774 goal Arith.thy "!!k::nat. k <= n ==> m <= n + m - k";
```
```   775 be rev_mp 1;
```
```   776 by (res_inst_tac [("m", "k"), ("n", "n")] diff_induct 1);
```
```   777 by (Simp_tac 1);
```
```   778 by (simp_tac (!simpset addsimps [less_add_Suc2, less_imp_le]) 1);
```
```   779 by (Simp_tac 1);
```
```   780 qed "le_add_diff";
```
```   781
```
```   782
```