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