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