src/HOL/ex/Arith_Examples.thy
author haftmann
Fri Oct 10 19:55:32 2014 +0200 (2014-10-10)
changeset 58646 cd63a4b12a33
parent 47108 2a1953f0d20d
child 58889 5b7a9633cfa8
permissions -rw-r--r--
specialized specification: avoid trivial instances
webertj@23193
     1
(*  Title:  HOL/ex/Arith_Examples.thy
webertj@23193
     2
    Author: Tjark Weber
webertj@23193
     3
*)
webertj@23193
     4
wenzelm@23218
     5
header {* Arithmetic *}
webertj@23193
     6
haftmann@31066
     7
theory Arith_Examples
haftmann@31066
     8
imports Main
haftmann@31066
     9
begin
webertj@23193
    10
webertj@23193
    11
text {*
wenzelm@23218
    12
  The @{text arith} method is used frequently throughout the Isabelle
webertj@23193
    13
  distribution.  This file merely contains some additional tests and special
webertj@23193
    14
  corner cases.  Some rather technical remarks:
webertj@23193
    15
haftmann@31101
    16
  @{ML Lin_Arith.simple_tac} is a very basic version of the tactic.  It performs no
webertj@23193
    17
  meta-to-object-logic conversion, and only some splitting of operators.
haftmann@31101
    18
  @{ML Lin_Arith.tac} performs meta-to-object-logic conversion, full
wenzelm@23218
    19
  splitting of operators, and NNF normalization of the goal.  The @{text arith}
wenzelm@23218
    20
  method combines them both, and tries other methods (e.g.~@{text presburger})
webertj@23193
    21
  as well.  This is the one that you should use in your proofs!
webertj@23193
    22
wenzelm@24093
    23
  An @{text arith}-based simproc is available as well (see @{ML
haftmann@31101
    24
  Lin_Arith.simproc}), which---for performance
haftmann@31101
    25
  reasons---however does even less splitting than @{ML Lin_Arith.simple_tac}
wenzelm@24093
    26
  at the moment (namely inequalities only).  (On the other hand, it
haftmann@31101
    27
  does take apart conjunctions, which @{ML Lin_Arith.simple_tac} currently
wenzelm@24093
    28
  does not do.)
webertj@23193
    29
*}
webertj@23193
    30
webertj@23193
    31
wenzelm@23218
    32
subsection {* Splitting of Operators: @{term max}, @{term min}, @{term abs},
haftmann@35267
    33
           @{term minus}, @{term nat}, @{term Divides.mod},
webertj@23193
    34
           @{term Divides.div} *}
webertj@23193
    35
webertj@23193
    36
lemma "(i::nat) <= max i j"
haftmann@31066
    37
  by linarith
webertj@23193
    38
webertj@23193
    39
lemma "(i::int) <= max i j"
haftmann@31066
    40
  by linarith
webertj@23193
    41
webertj@23193
    42
lemma "min i j <= (i::nat)"
haftmann@31066
    43
  by linarith
webertj@23193
    44
webertj@23193
    45
lemma "min i j <= (i::int)"
haftmann@31066
    46
  by linarith
webertj@23193
    47
webertj@23193
    48
lemma "min (i::nat) j <= max i j"
haftmann@31066
    49
  by linarith
webertj@23193
    50
webertj@23193
    51
lemma "min (i::int) j <= max i j"
haftmann@31066
    52
  by linarith
webertj@23193
    53
webertj@23208
    54
lemma "min (i::nat) j + max i j = i + j"
haftmann@31066
    55
  by linarith
webertj@23208
    56
webertj@23208
    57
lemma "min (i::int) j + max i j = i + j"
haftmann@31066
    58
  by linarith
webertj@23208
    59
webertj@23193
    60
lemma "(i::nat) < j ==> min i j < max i j"
haftmann@31066
    61
  by linarith
webertj@23193
    62
webertj@23193
    63
lemma "(i::int) < j ==> min i j < max i j"
haftmann@31066
    64
  by linarith
webertj@23193
    65
webertj@23193
    66
lemma "(0::int) <= abs i"
haftmann@31066
    67
  by linarith
webertj@23193
    68
webertj@23193
    69
lemma "(i::int) <= abs i"
haftmann@31066
    70
  by linarith
webertj@23193
    71
webertj@23193
    72
lemma "abs (abs (i::int)) = abs i"
haftmann@31066
    73
  by linarith
webertj@23193
    74
webertj@23193
    75
text {* Also testing subgoals with bound variables. *}
webertj@23193
    76
webertj@23193
    77
lemma "!!x. (x::nat) <= y ==> x - y = 0"
haftmann@31066
    78
  by linarith
webertj@23193
    79
webertj@23193
    80
lemma "!!x. (x::nat) - y = 0 ==> x <= y"
haftmann@31066
    81
  by linarith
webertj@23193
    82
webertj@23193
    83
lemma "!!x. ((x::nat) <= y) = (x - y = 0)"
haftmann@31066
    84
  by linarith
webertj@23193
    85
webertj@23193
    86
lemma "[| (x::nat) < y; d < 1 |] ==> x - y = d"
haftmann@31066
    87
  by linarith
webertj@23193
    88
webertj@23193
    89
lemma "[| (x::nat) < y; d < 1 |] ==> x - y - x = d - x"
haftmann@31066
    90
  by linarith
webertj@23193
    91
webertj@23193
    92
lemma "(x::int) < y ==> x - y < 0"
haftmann@31066
    93
  by linarith
webertj@23193
    94
webertj@23193
    95
lemma "nat (i + j) <= nat i + nat j"
haftmann@31066
    96
  by linarith
webertj@23193
    97
webertj@23193
    98
lemma "i < j ==> nat (i - j) = 0"
haftmann@31066
    99
  by linarith
webertj@23193
   100
webertj@23193
   101
lemma "(i::nat) mod 0 = i"
huffman@46597
   102
  (* rule split_mod is only declared by default for numerals *)
huffman@46597
   103
  using split_mod [of _ _ "0", arith_split]
haftmann@31066
   104
  by linarith
webertj@23198
   105
webertj@23198
   106
lemma "(i::nat) mod 1 = 0"
huffman@46597
   107
  (* rule split_mod is only declared by default for numerals *)
huffman@46597
   108
  using split_mod [of _ _ "1", arith_split]
haftmann@31066
   109
  by linarith
webertj@23193
   110
webertj@23198
   111
lemma "(i::nat) mod 42 <= 41"
haftmann@31066
   112
  by linarith
webertj@23198
   113
webertj@23198
   114
lemma "(i::int) mod 0 = i"
huffman@46597
   115
  (* rule split_zmod is only declared by default for numerals *)
huffman@46597
   116
  using split_zmod [of _ _ "0", arith_split]
haftmann@31066
   117
  by linarith
webertj@23198
   118
webertj@23198
   119
lemma "(i::int) mod 1 = 0"
huffman@46597
   120
  (* rule split_zmod is only declared by default for numerals *)
huffman@46597
   121
  using split_zmod [of _ _ "1", arith_split]
huffman@46597
   122
  by linarith
webertj@23193
   123
webertj@23198
   124
lemma "(i::int) mod 42 <= 41"
huffman@46597
   125
  by linarith
webertj@23193
   126
webertj@24328
   127
lemma "-(i::int) * 1 = 0 ==> i = 0"
haftmann@31066
   128
  by linarith
webertj@24328
   129
webertj@24328
   130
lemma "[| (0::int) < abs i; abs i * 1 < abs i * j |] ==> 1 < abs i * j"
haftmann@31066
   131
  by linarith
webertj@24328
   132
wenzelm@23218
   133
wenzelm@23218
   134
subsection {* Meta-Logic *}
webertj@23193
   135
webertj@23193
   136
lemma "x < Suc y == x <= y"
haftmann@31066
   137
  by linarith
webertj@23193
   138
webertj@23193
   139
lemma "((x::nat) == z ==> x ~= y) ==> x ~= y | z ~= y"
haftmann@31066
   140
  by linarith
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)"
haftmann@31066
   146
  by linarith
webertj@23198
   147
webertj@23193
   148
lemma "[| (x::nat) < y; y < z |] ==> x < z"
haftmann@31066
   149
  by linarith
webertj@23193
   150
webertj@23193
   151
lemma "(x::nat) < y & y < z ==> x < z"
haftmann@31066
   152
  by linarith
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"
haftmann@31066
   158
  by linarith
webertj@23208
   159
webertj@23208
   160
lemma "[| P = (x = 0); (~P) = (y = 0) |] ==> min (x::nat) y = 0"
haftmann@31066
   161
  by linarith
webertj@23208
   162
webertj@23208
   163
lemma "[| P = (x = 0); (~P) = (y = 0) |] ==> max (x::nat) y = x + y"
haftmann@31066
   164
  by linarith
webertj@23208
   165
webertj@23193
   166
lemma "[| (x::nat) ~= y; a + 2 = b; a < y; y < b; a < x; x < b |] ==> False"
haftmann@31066
   167
  by linarith
webertj@23193
   168
webertj@23193
   169
lemma "[| (x::nat) > y; y > z; z > x |] ==> False"
haftmann@31066
   170
  by linarith
webertj@23193
   171
webertj@23193
   172
lemma "(x::nat) - 5 > y ==> y < x"
haftmann@31066
   173
  by linarith
webertj@23193
   174
webertj@23193
   175
lemma "(x::nat) ~= 0 ==> 0 < x"
haftmann@31066
   176
  by linarith
webertj@23193
   177
webertj@23193
   178
lemma "[| (x::nat) ~= y; x <= y |] ==> x < y"
haftmann@31066
   179
  by linarith
webertj@23193
   180
webertj@23196
   181
lemma "[| (x::nat) < y; P (x - y) |] ==> P 0"
haftmann@31066
   182
  by linarith
webertj@23193
   183
webertj@23193
   184
lemma "(x - y) - (x::nat) = (x - x) - y"
haftmann@31066
   185
  by linarith
webertj@23193
   186
webertj@23193
   187
lemma "[| (a::nat) < b; c < d |] ==> (a - b) = (c - d)"
haftmann@31066
   188
  by linarith
webertj@23193
   189
webertj@23193
   190
lemma "((a::nat) - (b - (c - (d - e)))) = (a - (b - (c - (d - e))))"
haftmann@31066
   191
  by linarith
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                                      *)
haftmann@31101
   204
(* by (tactic {* Lin_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?             *)
haftmann@31101
   210
(* by (tactic {* Lin_Arith.simple_tac 1 *}) *)
webertj@23198
   211
oops
webertj@23198
   212
webertj@23198
   213
text {* Constants. *}
webertj@23198
   214
webertj@23198
   215
lemma "(0::nat) < 1"
haftmann@31066
   216
  by linarith
webertj@23198
   217
webertj@23198
   218
lemma "(0::int) < 1"
haftmann@31066
   219
  by linarith
webertj@23198
   220
huffman@47108
   221
lemma "(47::nat) + 11 < 8 * 15"
haftmann@31066
   222
  by linarith
webertj@23198
   223
huffman@47108
   224
lemma "(47::int) + 11 < 8 * 15"
haftmann@31066
   225
  by linarith
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))"
haftmann@31066
   231
  by linarith
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))"
haftmann@31066
   237
  by linarith
webertj@23193
   238
webertj@23193
   239
end