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