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