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