src/HOL/ex/Arith_Examples.thy
author wenzelm
Tue Jul 31 19:40:24 2007 +0200 (2007-07-31)
changeset 24093 5d0ecd0c8f3c
parent 24075 366d4d234814
child 24328 83afe527504d
permissions -rw-r--r--
tuned LinArith setup;
webertj@23193
     1
(*  Title:  HOL/ex/Arith_Examples.thy
webertj@23193
     2
    ID:     $Id$
webertj@23193
     3
    Author: Tjark Weber
webertj@23193
     4
*)
webertj@23193
     5
wenzelm@23218
     6
header {* Arithmetic *}
webertj@23193
     7
webertj@23193
     8
theory Arith_Examples imports Main begin
webertj@23193
     9
webertj@23193
    10
text {*
wenzelm@23218
    11
  The @{text arith} method is used frequently throughout the Isabelle
webertj@23193
    12
  distribution.  This file merely contains some additional tests and special
webertj@23193
    13
  corner cases.  Some rather technical remarks:
webertj@23193
    14
wenzelm@23218
    15
  @{ML fast_arith_tac} is a very basic version of the tactic.  It performs no
webertj@23193
    16
  meta-to-object-logic conversion, and only some splitting of operators.
wenzelm@23218
    17
  @{ML simple_arith_tac} performs meta-to-object-logic conversion, full
wenzelm@23218
    18
  splitting of operators, and NNF normalization of the goal.  The @{text arith}
wenzelm@23218
    19
  method combines them both, and tries other methods (e.g.~@{text presburger})
webertj@23193
    20
  as well.  This is the one that you should use in your proofs!
webertj@23193
    21
wenzelm@24093
    22
  An @{text arith}-based simproc is available as well (see @{ML
wenzelm@24093
    23
  LinArith.lin_arith_simproc}), which---for performance
wenzelm@24093
    24
  reasons---however does even less splitting than @{ML fast_arith_tac}
wenzelm@24093
    25
  at the moment (namely inequalities only).  (On the other hand, it
wenzelm@24093
    26
  does take apart conjunctions, which @{ML fast_arith_tac} currently
wenzelm@24093
    27
  does not do.)
webertj@23193
    28
*}
webertj@23193
    29
webertj@23196
    30
(*
webertj@23193
    31
ML {* set trace_arith; *}
webertj@23196
    32
*)
webertj@23193
    33
wenzelm@23218
    34
subsection {* Splitting of Operators: @{term max}, @{term min}, @{term abs},
webertj@23193
    35
           @{term HOL.minus}, @{term nat}, @{term Divides.mod},
webertj@23193
    36
           @{term Divides.div} *}
webertj@23193
    37
webertj@23193
    38
lemma "(i::nat) <= max i j"
wenzelm@24075
    39
  by (tactic {* fast_arith_tac @{context} 1 *})
webertj@23193
    40
webertj@23193
    41
lemma "(i::int) <= max i j"
wenzelm@24075
    42
  by (tactic {* fast_arith_tac @{context} 1 *})
webertj@23193
    43
webertj@23193
    44
lemma "min i j <= (i::nat)"
wenzelm@24075
    45
  by (tactic {* fast_arith_tac @{context} 1 *})
webertj@23193
    46
webertj@23193
    47
lemma "min i j <= (i::int)"
wenzelm@24075
    48
  by (tactic {* fast_arith_tac @{context} 1 *})
webertj@23193
    49
webertj@23193
    50
lemma "min (i::nat) j <= max i j"
wenzelm@24075
    51
  by (tactic {* fast_arith_tac @{context} 1 *})
webertj@23193
    52
webertj@23193
    53
lemma "min (i::int) j <= max i j"
wenzelm@24075
    54
  by (tactic {* fast_arith_tac @{context} 1 *})
webertj@23193
    55
webertj@23208
    56
lemma "min (i::nat) j + max i j = i + j"
wenzelm@24075
    57
  by (tactic {* fast_arith_tac @{context} 1 *})
webertj@23208
    58
webertj@23208
    59
lemma "min (i::int) j + max i j = i + j"
wenzelm@24075
    60
  by (tactic {* fast_arith_tac @{context} 1 *})
webertj@23208
    61
webertj@23193
    62
lemma "(i::nat) < j ==> min i j < max i j"
wenzelm@24075
    63
  by (tactic {* fast_arith_tac @{context} 1 *})
webertj@23193
    64
webertj@23193
    65
lemma "(i::int) < j ==> min i j < max i j"
wenzelm@24075
    66
  by (tactic {* fast_arith_tac @{context} 1 *})
webertj@23193
    67
webertj@23193
    68
lemma "(0::int) <= abs i"
wenzelm@24075
    69
  by (tactic {* fast_arith_tac @{context} 1 *})
webertj@23193
    70
webertj@23193
    71
lemma "(i::int) <= abs i"
wenzelm@24075
    72
  by (tactic {* fast_arith_tac @{context} 1 *})
webertj@23193
    73
webertj@23193
    74
lemma "abs (abs (i::int)) = abs i"
wenzelm@24075
    75
  by (tactic {* fast_arith_tac @{context} 1 *})
webertj@23193
    76
webertj@23193
    77
text {* Also testing subgoals with bound variables. *}
webertj@23193
    78
webertj@23193
    79
lemma "!!x. (x::nat) <= y ==> x - y = 0"
wenzelm@24075
    80
  by (tactic {* fast_arith_tac @{context} 1 *})
webertj@23193
    81
webertj@23193
    82
lemma "!!x. (x::nat) - y = 0 ==> x <= y"
wenzelm@24075
    83
  by (tactic {* fast_arith_tac @{context} 1 *})
webertj@23193
    84
webertj@23193
    85
lemma "!!x. ((x::nat) <= y) = (x - y = 0)"
wenzelm@24075
    86
  by (tactic {* simple_arith_tac @{context} 1 *})
webertj@23193
    87
webertj@23193
    88
lemma "[| (x::nat) < y; d < 1 |] ==> x - y = d"
wenzelm@24075
    89
  by (tactic {* fast_arith_tac @{context} 1 *})
webertj@23193
    90
webertj@23193
    91
lemma "[| (x::nat) < y; d < 1 |] ==> x - y - x = d - x"
wenzelm@24075
    92
  by (tactic {* fast_arith_tac @{context} 1 *})
webertj@23193
    93
webertj@23193
    94
lemma "(x::int) < y ==> x - y < 0"
wenzelm@24075
    95
  by (tactic {* fast_arith_tac @{context} 1 *})
webertj@23193
    96
webertj@23193
    97
lemma "nat (i + j) <= nat i + nat j"
wenzelm@24075
    98
  by (tactic {* fast_arith_tac @{context} 1 *})
webertj@23193
    99
webertj@23193
   100
lemma "i < j ==> nat (i - j) = 0"
wenzelm@24075
   101
  by (tactic {* fast_arith_tac @{context} 1 *})
webertj@23193
   102
webertj@23193
   103
lemma "(i::nat) mod 0 = i"
webertj@23198
   104
  (* FIXME: need to replace 0 by its numeral representation *)
webertj@23198
   105
  apply (subst nat_numeral_0_eq_0 [symmetric])
wenzelm@24075
   106
  by (tactic {* fast_arith_tac @{context} 1 *})
webertj@23198
   107
webertj@23198
   108
lemma "(i::nat) mod 1 = 0"
webertj@23198
   109
  (* FIXME: need to replace 1 by its numeral representation *)
webertj@23198
   110
  apply (subst nat_numeral_1_eq_1 [symmetric])
wenzelm@24075
   111
  by (tactic {* fast_arith_tac @{context} 1 *})
webertj@23193
   112
webertj@23198
   113
lemma "(i::nat) mod 42 <= 41"
wenzelm@24075
   114
  by (tactic {* fast_arith_tac @{context} 1 *})
webertj@23198
   115
webertj@23198
   116
lemma "(i::int) mod 0 = i"
webertj@23198
   117
  (* FIXME: need to replace 0 by its numeral representation *)
webertj@23198
   118
  apply (subst numeral_0_eq_0 [symmetric])
wenzelm@24075
   119
  by (tactic {* fast_arith_tac @{context} 1 *})
webertj@23198
   120
webertj@23198
   121
lemma "(i::int) mod 1 = 0"
webertj@23198
   122
  (* FIXME: need to replace 1 by its numeral representation *)
webertj@23198
   123
  apply (subst numeral_1_eq_1 [symmetric])
webertj@23198
   124
  (* FIXME: arith does not know about iszero *)
wenzelm@24093
   125
  apply (tactic {* lin_arith_pre_tac @{context} 1 *})
webertj@23193
   126
oops
webertj@23193
   127
webertj@23198
   128
lemma "(i::int) mod 42 <= 41"
webertj@23198
   129
  (* FIXME: arith does not know about iszero *)
wenzelm@24093
   130
  apply (tactic {* lin_arith_pre_tac @{context} 1 *})
webertj@23193
   131
oops
webertj@23193
   132
wenzelm@23218
   133
wenzelm@23218
   134
subsection {* Meta-Logic *}
webertj@23193
   135
webertj@23193
   136
lemma "x < Suc y == x <= y"
wenzelm@24075
   137
  by (tactic {* simple_arith_tac @{context} 1 *})
webertj@23193
   138
webertj@23193
   139
lemma "((x::nat) == z ==> x ~= y) ==> x ~= y | z ~= y"
wenzelm@24075
   140
  by (tactic {* simple_arith_tac @{context} 1 *})
webertj@23193
   141
wenzelm@23218
   142
wenzelm@23218
   143
subsection {* Various Other Examples *}
webertj@23193
   144
webertj@23198
   145
lemma "(x < Suc y) = (x <= y)"
wenzelm@24075
   146
  by (tactic {* simple_arith_tac @{context} 1 *})
webertj@23198
   147
webertj@23193
   148
lemma "[| (x::nat) < y; y < z |] ==> x < z"
wenzelm@24075
   149
  by (tactic {* fast_arith_tac @{context} 1 *})
webertj@23193
   150
webertj@23193
   151
lemma "(x::nat) < y & y < z ==> x < z"
wenzelm@24075
   152
  by (tactic {* simple_arith_tac @{context} 1 *})
webertj@23193
   153
webertj@23208
   154
text {* This example involves no arithmetic at all, but is solved by
webertj@23208
   155
  preprocessing (i.e. NNF normalization) alone. *}
webertj@23208
   156
webertj@23208
   157
lemma "(P::bool) = Q ==> Q = P"
wenzelm@24075
   158
  by (tactic {* simple_arith_tac @{context} 1 *})
webertj@23208
   159
webertj@23208
   160
lemma "[| P = (x = 0); (~P) = (y = 0) |] ==> min (x::nat) y = 0"
wenzelm@24075
   161
  by (tactic {* simple_arith_tac @{context} 1 *})
webertj@23208
   162
webertj@23208
   163
lemma "[| P = (x = 0); (~P) = (y = 0) |] ==> max (x::nat) y = x + y"
wenzelm@24075
   164
  by (tactic {* simple_arith_tac @{context} 1 *})
webertj@23208
   165
webertj@23193
   166
lemma "[| (x::nat) ~= y; a + 2 = b; a < y; y < b; a < x; x < b |] ==> False"
wenzelm@24075
   167
  by (tactic {* fast_arith_tac @{context} 1 *})
webertj@23193
   168
webertj@23193
   169
lemma "[| (x::nat) > y; y > z; z > x |] ==> False"
wenzelm@24075
   170
  by (tactic {* fast_arith_tac @{context} 1 *})
webertj@23193
   171
webertj@23193
   172
lemma "(x::nat) - 5 > y ==> y < x"
wenzelm@24075
   173
  by (tactic {* fast_arith_tac @{context} 1 *})
webertj@23193
   174
webertj@23193
   175
lemma "(x::nat) ~= 0 ==> 0 < x"
wenzelm@24075
   176
  by (tactic {* fast_arith_tac @{context} 1 *})
webertj@23193
   177
webertj@23193
   178
lemma "[| (x::nat) ~= y; x <= y |] ==> x < y"
wenzelm@24075
   179
  by (tactic {* fast_arith_tac @{context} 1 *})
webertj@23193
   180
webertj@23196
   181
lemma "[| (x::nat) < y; P (x - y) |] ==> P 0"
wenzelm@24075
   182
  by (tactic {* simple_arith_tac @{context} 1 *})
webertj@23193
   183
webertj@23193
   184
lemma "(x - y) - (x::nat) = (x - x) - y"
wenzelm@24075
   185
  by (tactic {* fast_arith_tac @{context} 1 *})
webertj@23193
   186
webertj@23193
   187
lemma "[| (a::nat) < b; c < d |] ==> (a - b) = (c - d)"
wenzelm@24075
   188
  by (tactic {* fast_arith_tac @{context} 1 *})
webertj@23193
   189
webertj@23193
   190
lemma "((a::nat) - (b - (c - (d - e)))) = (a - (b - (c - (d - e))))"
wenzelm@24075
   191
  by (tactic {* fast_arith_tac @{context} 1 *})
webertj@23193
   192
webertj@23198
   193
lemma "(n < m & m < n') | (n < m & m = n') | (n < n' & n' < m) |
webertj@23198
   194
  (n = n' & n' < m) | (n = m & m < n') |
webertj@23198
   195
  (n' < m & m < n) | (n' < m & m = n) |
webertj@23198
   196
  (n' < n & n < m) | (n' = n & n < m) | (n' = m & m < n) |
webertj@23198
   197
  (m < n & n < n') | (m < n & n' = n) | (m < n' & n' < n) |
webertj@23198
   198
  (m = n & n < n') | (m = n' & n' < n) |
webertj@23198
   199
  (n' = m & m = (n::nat))"
webertj@23198
   200
(* FIXME: this should work in principle, but is extremely slow because     *)
webertj@23198
   201
(*        preprocessing negates the goal and tries to compute its negation *)
webertj@23198
   202
(*        normal form, which creates lots of separate cases for this       *)
webertj@23198
   203
(*        disjunction of conjunctions                                      *)
webertj@23198
   204
(* by (tactic {* simple_arith_tac 1 *}) *)
webertj@23198
   205
oops
webertj@23198
   206
webertj@23198
   207
lemma "2 * (x::nat) ~= 1"
webertj@23208
   208
(* FIXME: this is beyond the scope of the decision procedure at the moment, *)
webertj@23208
   209
(*        because its negation is satisfiable in the rationals?             *)
webertj@23198
   210
(* by (tactic {* fast_arith_tac 1 *}) *)
webertj@23198
   211
oops
webertj@23198
   212
webertj@23198
   213
text {* Constants. *}
webertj@23198
   214
webertj@23198
   215
lemma "(0::nat) < 1"
wenzelm@24075
   216
  by (tactic {* fast_arith_tac @{context} 1 *})
webertj@23198
   217
webertj@23198
   218
lemma "(0::int) < 1"
wenzelm@24075
   219
  by (tactic {* fast_arith_tac @{context} 1 *})
webertj@23198
   220
webertj@23198
   221
lemma "(47::nat) + 11 < 08 * 15"
wenzelm@24075
   222
  by (tactic {* fast_arith_tac @{context} 1 *})
webertj@23198
   223
webertj@23198
   224
lemma "(47::int) + 11 < 08 * 15"
wenzelm@24075
   225
  by (tactic {* fast_arith_tac @{context} 1 *})
webertj@23198
   226
webertj@23193
   227
text {* Splitting of inequalities of different type. *}
webertj@23193
   228
webertj@23193
   229
lemma "[| (a::nat) ~= b; (i::int) ~= j; a < 2; b < 2 |] ==>
webertj@23193
   230
  a + b <= nat (max (abs i) (abs j))"
wenzelm@24075
   231
  by (tactic {* fast_arith_tac @{context} 1 *})
webertj@23193
   232
webertj@23198
   233
text {* Again, but different order. *}
webertj@23198
   234
webertj@23193
   235
lemma "[| (i::int) ~= j; (a::nat) ~= b; a < 2; b < 2 |] ==>
webertj@23193
   236
  a + b <= nat (max (abs i) (abs j))"
wenzelm@24075
   237
  by (tactic {* fast_arith_tac @{context} 1 *})
webertj@23193
   238
webertj@23196
   239
(*
webertj@23193
   240
ML {* reset trace_arith; *}
webertj@23196
   241
*)
webertj@23193
   242
webertj@23193
   243
end