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