src/HOL/Numeral_Simprocs.thy
author huffman
Sun Mar 25 20:15:39 2012 +0200 (2012-03-25)
changeset 47108 2a1953f0d20d
parent 45607 16b4f5774621
child 47159 978c00c20a59
permissions -rw-r--r--
merged fork with new numeral representation (see NEWS)
haftmann@33366
     1
(* Author: Various *)
haftmann@33366
     2
haftmann@33366
     3
header {* Combination and Cancellation Simprocs for Numeral Expressions *}
haftmann@33366
     4
haftmann@33366
     5
theory Numeral_Simprocs
haftmann@33366
     6
imports Divides
haftmann@33366
     7
uses
haftmann@33366
     8
  "~~/src/Provers/Arith/assoc_fold.ML"
haftmann@33366
     9
  "~~/src/Provers/Arith/cancel_numerals.ML"
haftmann@33366
    10
  "~~/src/Provers/Arith/combine_numerals.ML"
haftmann@33366
    11
  "~~/src/Provers/Arith/cancel_numeral_factor.ML"
haftmann@33366
    12
  "~~/src/Provers/Arith/extract_common_term.ML"
haftmann@33366
    13
  ("Tools/numeral_simprocs.ML")
haftmann@33366
    14
  ("Tools/nat_numeral_simprocs.ML")
haftmann@33366
    15
begin
haftmann@33366
    16
huffman@47108
    17
declare split_div [of _ _ "numeral k", arith_split] for k
huffman@47108
    18
declare split_mod [of _ _ "numeral k", arith_split] for k
haftmann@33366
    19
haftmann@33366
    20
text {* For @{text combine_numerals} *}
haftmann@33366
    21
haftmann@33366
    22
lemma left_add_mult_distrib: "i*u + (j*u + k) = (i+j)*u + (k::nat)"
haftmann@33366
    23
by (simp add: add_mult_distrib)
haftmann@33366
    24
haftmann@33366
    25
text {* For @{text cancel_numerals} *}
haftmann@33366
    26
haftmann@33366
    27
lemma nat_diff_add_eq1:
haftmann@33366
    28
     "j <= (i::nat) ==> ((i*u + m) - (j*u + n)) = (((i-j)*u + m) - n)"
haftmann@33366
    29
by (simp split add: nat_diff_split add: add_mult_distrib)
haftmann@33366
    30
haftmann@33366
    31
lemma nat_diff_add_eq2:
haftmann@33366
    32
     "i <= (j::nat) ==> ((i*u + m) - (j*u + n)) = (m - ((j-i)*u + n))"
haftmann@33366
    33
by (simp split add: nat_diff_split add: add_mult_distrib)
haftmann@33366
    34
haftmann@33366
    35
lemma nat_eq_add_iff1:
haftmann@33366
    36
     "j <= (i::nat) ==> (i*u + m = j*u + n) = ((i-j)*u + m = n)"
haftmann@33366
    37
by (auto split add: nat_diff_split simp add: add_mult_distrib)
haftmann@33366
    38
haftmann@33366
    39
lemma nat_eq_add_iff2:
haftmann@33366
    40
     "i <= (j::nat) ==> (i*u + m = j*u + n) = (m = (j-i)*u + n)"
haftmann@33366
    41
by (auto split add: nat_diff_split simp add: add_mult_distrib)
haftmann@33366
    42
haftmann@33366
    43
lemma nat_less_add_iff1:
haftmann@33366
    44
     "j <= (i::nat) ==> (i*u + m < j*u + n) = ((i-j)*u + m < n)"
haftmann@33366
    45
by (auto split add: nat_diff_split simp add: add_mult_distrib)
haftmann@33366
    46
haftmann@33366
    47
lemma nat_less_add_iff2:
haftmann@33366
    48
     "i <= (j::nat) ==> (i*u + m < j*u + n) = (m < (j-i)*u + n)"
haftmann@33366
    49
by (auto split add: nat_diff_split simp add: add_mult_distrib)
haftmann@33366
    50
haftmann@33366
    51
lemma nat_le_add_iff1:
haftmann@33366
    52
     "j <= (i::nat) ==> (i*u + m <= j*u + n) = ((i-j)*u + m <= n)"
haftmann@33366
    53
by (auto split add: nat_diff_split simp add: add_mult_distrib)
haftmann@33366
    54
haftmann@33366
    55
lemma nat_le_add_iff2:
haftmann@33366
    56
     "i <= (j::nat) ==> (i*u + m <= j*u + n) = (m <= (j-i)*u + n)"
haftmann@33366
    57
by (auto split add: nat_diff_split simp add: add_mult_distrib)
haftmann@33366
    58
haftmann@33366
    59
text {* For @{text cancel_numeral_factors} *}
haftmann@33366
    60
haftmann@33366
    61
lemma nat_mult_le_cancel1: "(0::nat) < k ==> (k*m <= k*n) = (m<=n)"
haftmann@33366
    62
by auto
haftmann@33366
    63
haftmann@33366
    64
lemma nat_mult_less_cancel1: "(0::nat) < k ==> (k*m < k*n) = (m<n)"
haftmann@33366
    65
by auto
haftmann@33366
    66
haftmann@33366
    67
lemma nat_mult_eq_cancel1: "(0::nat) < k ==> (k*m = k*n) = (m=n)"
haftmann@33366
    68
by auto
haftmann@33366
    69
haftmann@33366
    70
lemma nat_mult_div_cancel1: "(0::nat) < k ==> (k*m) div (k*n) = (m div n)"
haftmann@33366
    71
by auto
haftmann@33366
    72
haftmann@33366
    73
lemma nat_mult_dvd_cancel_disj[simp]:
haftmann@33366
    74
  "(k*m) dvd (k*n) = (k=0 | m dvd (n::nat))"
haftmann@33366
    75
by(auto simp: dvd_eq_mod_eq_0 mod_mult_distrib2[symmetric])
haftmann@33366
    76
haftmann@33366
    77
lemma nat_mult_dvd_cancel1: "0 < k \<Longrightarrow> (k*m) dvd (k*n::nat) = (m dvd n)"
haftmann@33366
    78
by(auto)
haftmann@33366
    79
haftmann@33366
    80
text {* For @{text cancel_factor} *}
haftmann@33366
    81
haftmann@33366
    82
lemma nat_mult_le_cancel_disj: "(k*m <= k*n) = ((0::nat) < k --> m<=n)"
haftmann@33366
    83
by auto
haftmann@33366
    84
haftmann@33366
    85
lemma nat_mult_less_cancel_disj: "(k*m < k*n) = ((0::nat) < k & m<n)"
haftmann@33366
    86
by auto
haftmann@33366
    87
haftmann@33366
    88
lemma nat_mult_eq_cancel_disj: "(k*m = k*n) = (k = (0::nat) | m=n)"
haftmann@33366
    89
by auto
haftmann@33366
    90
haftmann@33366
    91
lemma nat_mult_div_cancel_disj[simp]:
haftmann@33366
    92
     "(k*m) div (k*n) = (if k = (0::nat) then 0 else m div n)"
haftmann@33366
    93
by (simp add: nat_mult_div_cancel1)
haftmann@33366
    94
haftmann@33366
    95
use "Tools/numeral_simprocs.ML"
haftmann@33366
    96
huffman@45284
    97
simproc_setup semiring_assoc_fold
huffman@45284
    98
  ("(a::'a::comm_semiring_1_cancel) * b") =
huffman@45284
    99
  {* fn phi => Numeral_Simprocs.assoc_fold *}
huffman@45284
   100
huffman@47108
   101
(* TODO: see whether the type class can be generalized further *)
huffman@45284
   102
simproc_setup int_combine_numerals
huffman@47108
   103
  ("(i::'a::comm_ring_1) + j" | "(i::'a::comm_ring_1) - j") =
huffman@45284
   104
  {* fn phi => Numeral_Simprocs.combine_numerals *}
huffman@45284
   105
huffman@45284
   106
simproc_setup field_combine_numerals
huffman@47108
   107
  ("(i::'a::{field_inverse_zero,ring_char_0}) + j"
huffman@47108
   108
  |"(i::'a::{field_inverse_zero,ring_char_0}) - j") =
huffman@45284
   109
  {* fn phi => Numeral_Simprocs.field_combine_numerals *}
huffman@45284
   110
huffman@45284
   111
simproc_setup inteq_cancel_numerals
huffman@47108
   112
  ("(l::'a::comm_ring_1) + m = n"
huffman@47108
   113
  |"(l::'a::comm_ring_1) = m + n"
huffman@47108
   114
  |"(l::'a::comm_ring_1) - m = n"
huffman@47108
   115
  |"(l::'a::comm_ring_1) = m - n"
huffman@47108
   116
  |"(l::'a::comm_ring_1) * m = n"
huffman@47108
   117
  |"(l::'a::comm_ring_1) = m * n"
huffman@47108
   118
  |"- (l::'a::comm_ring_1) = m"
huffman@47108
   119
  |"(l::'a::comm_ring_1) = - m") =
huffman@45284
   120
  {* fn phi => Numeral_Simprocs.eq_cancel_numerals *}
huffman@45284
   121
huffman@45284
   122
simproc_setup intless_cancel_numerals
huffman@47108
   123
  ("(l::'a::linordered_idom) + m < n"
huffman@47108
   124
  |"(l::'a::linordered_idom) < m + n"
huffman@47108
   125
  |"(l::'a::linordered_idom) - m < n"
huffman@47108
   126
  |"(l::'a::linordered_idom) < m - n"
huffman@47108
   127
  |"(l::'a::linordered_idom) * m < n"
huffman@47108
   128
  |"(l::'a::linordered_idom) < m * n"
huffman@47108
   129
  |"- (l::'a::linordered_idom) < m"
huffman@47108
   130
  |"(l::'a::linordered_idom) < - m") =
huffman@45284
   131
  {* fn phi => Numeral_Simprocs.less_cancel_numerals *}
huffman@45284
   132
huffman@45284
   133
simproc_setup intle_cancel_numerals
huffman@47108
   134
  ("(l::'a::linordered_idom) + m \<le> n"
huffman@47108
   135
  |"(l::'a::linordered_idom) \<le> m + n"
huffman@47108
   136
  |"(l::'a::linordered_idom) - m \<le> n"
huffman@47108
   137
  |"(l::'a::linordered_idom) \<le> m - n"
huffman@47108
   138
  |"(l::'a::linordered_idom) * m \<le> n"
huffman@47108
   139
  |"(l::'a::linordered_idom) \<le> m * n"
huffman@47108
   140
  |"- (l::'a::linordered_idom) \<le> m"
huffman@47108
   141
  |"(l::'a::linordered_idom) \<le> - m") =
huffman@45284
   142
  {* fn phi => Numeral_Simprocs.le_cancel_numerals *}
huffman@45284
   143
huffman@45284
   144
simproc_setup ring_eq_cancel_numeral_factor
huffman@47108
   145
  ("(l::'a::{idom,ring_char_0}) * m = n"
huffman@47108
   146
  |"(l::'a::{idom,ring_char_0}) = m * n") =
huffman@45284
   147
  {* fn phi => Numeral_Simprocs.eq_cancel_numeral_factor *}
huffman@45284
   148
huffman@45284
   149
simproc_setup ring_less_cancel_numeral_factor
huffman@47108
   150
  ("(l::'a::linordered_idom) * m < n"
huffman@47108
   151
  |"(l::'a::linordered_idom) < m * n") =
huffman@45284
   152
  {* fn phi => Numeral_Simprocs.less_cancel_numeral_factor *}
huffman@45284
   153
huffman@45284
   154
simproc_setup ring_le_cancel_numeral_factor
huffman@47108
   155
  ("(l::'a::linordered_idom) * m <= n"
huffman@47108
   156
  |"(l::'a::linordered_idom) <= m * n") =
huffman@45284
   157
  {* fn phi => Numeral_Simprocs.le_cancel_numeral_factor *}
huffman@45284
   158
huffman@47108
   159
(* TODO: remove comm_ring_1 constraint if possible *)
huffman@45284
   160
simproc_setup int_div_cancel_numeral_factors
huffman@47108
   161
  ("((l::'a::{semiring_div,comm_ring_1,ring_char_0}) * m) div n"
huffman@47108
   162
  |"(l::'a::{semiring_div,comm_ring_1,ring_char_0}) div (m * n)") =
huffman@45284
   163
  {* fn phi => Numeral_Simprocs.div_cancel_numeral_factor *}
huffman@45284
   164
huffman@45284
   165
simproc_setup divide_cancel_numeral_factor
huffman@47108
   166
  ("((l::'a::{field_inverse_zero,ring_char_0}) * m) / n"
huffman@47108
   167
  |"(l::'a::{field_inverse_zero,ring_char_0}) / (m * n)"
huffman@47108
   168
  |"((numeral v)::'a::{field_inverse_zero,ring_char_0}) / (numeral w)") =
huffman@45284
   169
  {* fn phi => Numeral_Simprocs.divide_cancel_numeral_factor *}
huffman@45284
   170
huffman@45284
   171
simproc_setup ring_eq_cancel_factor
huffman@45284
   172
  ("(l::'a::idom) * m = n" | "(l::'a::idom) = m * n") =
huffman@45284
   173
  {* fn phi => Numeral_Simprocs.eq_cancel_factor *}
huffman@45284
   174
huffman@45284
   175
simproc_setup linordered_ring_le_cancel_factor
huffman@45296
   176
  ("(l::'a::linordered_idom) * m <= n"
huffman@45296
   177
  |"(l::'a::linordered_idom) <= m * n") =
huffman@45284
   178
  {* fn phi => Numeral_Simprocs.le_cancel_factor *}
huffman@45284
   179
huffman@45284
   180
simproc_setup linordered_ring_less_cancel_factor
huffman@45296
   181
  ("(l::'a::linordered_idom) * m < n"
huffman@45296
   182
  |"(l::'a::linordered_idom) < m * n") =
huffman@45284
   183
  {* fn phi => Numeral_Simprocs.less_cancel_factor *}
huffman@45284
   184
huffman@45284
   185
simproc_setup int_div_cancel_factor
huffman@45284
   186
  ("((l::'a::semiring_div) * m) div n"
huffman@45284
   187
  |"(l::'a::semiring_div) div (m * n)") =
huffman@45284
   188
  {* fn phi => Numeral_Simprocs.div_cancel_factor *}
huffman@45284
   189
huffman@45284
   190
simproc_setup int_mod_cancel_factor
huffman@45284
   191
  ("((l::'a::semiring_div) * m) mod n"
huffman@45284
   192
  |"(l::'a::semiring_div) mod (m * n)") =
huffman@45284
   193
  {* fn phi => Numeral_Simprocs.mod_cancel_factor *}
huffman@45284
   194
huffman@45284
   195
simproc_setup dvd_cancel_factor
huffman@45284
   196
  ("((l::'a::idom) * m) dvd n"
huffman@45284
   197
  |"(l::'a::idom) dvd (m * n)") =
huffman@45284
   198
  {* fn phi => Numeral_Simprocs.dvd_cancel_factor *}
huffman@45284
   199
huffman@45284
   200
simproc_setup divide_cancel_factor
huffman@45284
   201
  ("((l::'a::field_inverse_zero) * m) / n"
huffman@45284
   202
  |"(l::'a::field_inverse_zero) / (m * n)") =
huffman@45284
   203
  {* fn phi => Numeral_Simprocs.divide_cancel_factor *}
huffman@45284
   204
haftmann@33366
   205
use "Tools/nat_numeral_simprocs.ML"
haftmann@33366
   206
huffman@45462
   207
simproc_setup nat_combine_numerals
huffman@45462
   208
  ("(i::nat) + j" | "Suc (i + j)") =
huffman@45462
   209
  {* fn phi => Nat_Numeral_Simprocs.combine_numerals *}
huffman@45462
   210
huffman@45436
   211
simproc_setup nateq_cancel_numerals
huffman@45436
   212
  ("(l::nat) + m = n" | "(l::nat) = m + n" |
huffman@45436
   213
   "(l::nat) * m = n" | "(l::nat) = m * n" |
huffman@45436
   214
   "Suc m = n" | "m = Suc n") =
huffman@45436
   215
  {* fn phi => Nat_Numeral_Simprocs.eq_cancel_numerals *}
huffman@45436
   216
huffman@45436
   217
simproc_setup natless_cancel_numerals
huffman@45436
   218
  ("(l::nat) + m < n" | "(l::nat) < m + n" |
huffman@45436
   219
   "(l::nat) * m < n" | "(l::nat) < m * n" |
huffman@45436
   220
   "Suc m < n" | "m < Suc n") =
huffman@45436
   221
  {* fn phi => Nat_Numeral_Simprocs.less_cancel_numerals *}
huffman@45436
   222
huffman@45436
   223
simproc_setup natle_cancel_numerals
huffman@45436
   224
  ("(l::nat) + m \<le> n" | "(l::nat) \<le> m + n" |
huffman@45436
   225
   "(l::nat) * m \<le> n" | "(l::nat) \<le> m * n" |
huffman@45436
   226
   "Suc m \<le> n" | "m \<le> Suc n") =
huffman@45436
   227
  {* fn phi => Nat_Numeral_Simprocs.le_cancel_numerals *}
huffman@45436
   228
huffman@45436
   229
simproc_setup natdiff_cancel_numerals
huffman@45436
   230
  ("((l::nat) + m) - n" | "(l::nat) - (m + n)" |
huffman@45436
   231
   "(l::nat) * m - n" | "(l::nat) - m * n" |
huffman@45436
   232
   "Suc m - n" | "m - Suc n") =
huffman@45436
   233
  {* fn phi => Nat_Numeral_Simprocs.diff_cancel_numerals *}
huffman@45436
   234
huffman@45463
   235
simproc_setup nat_eq_cancel_numeral_factor
huffman@45463
   236
  ("(l::nat) * m = n" | "(l::nat) = m * n") =
huffman@45463
   237
  {* fn phi => Nat_Numeral_Simprocs.eq_cancel_numeral_factor *}
huffman@45463
   238
huffman@45463
   239
simproc_setup nat_less_cancel_numeral_factor
huffman@45463
   240
  ("(l::nat) * m < n" | "(l::nat) < m * n") =
huffman@45463
   241
  {* fn phi => Nat_Numeral_Simprocs.less_cancel_numeral_factor *}
huffman@45463
   242
huffman@45463
   243
simproc_setup nat_le_cancel_numeral_factor
huffman@45463
   244
  ("(l::nat) * m <= n" | "(l::nat) <= m * n") =
huffman@45463
   245
  {* fn phi => Nat_Numeral_Simprocs.le_cancel_numeral_factor *}
huffman@45463
   246
huffman@45463
   247
simproc_setup nat_div_cancel_numeral_factor
huffman@45463
   248
  ("((l::nat) * m) div n" | "(l::nat) div (m * n)") =
huffman@45463
   249
  {* fn phi => Nat_Numeral_Simprocs.div_cancel_numeral_factor *}
huffman@45463
   250
huffman@45463
   251
simproc_setup nat_dvd_cancel_numeral_factor
huffman@45463
   252
  ("((l::nat) * m) dvd n" | "(l::nat) dvd (m * n)") =
huffman@45463
   253
  {* fn phi => Nat_Numeral_Simprocs.dvd_cancel_numeral_factor *}
huffman@45463
   254
huffman@45462
   255
simproc_setup nat_eq_cancel_factor
huffman@45462
   256
  ("(l::nat) * m = n" | "(l::nat) = m * n") =
huffman@45462
   257
  {* fn phi => Nat_Numeral_Simprocs.eq_cancel_factor *}
huffman@45462
   258
huffman@45462
   259
simproc_setup nat_less_cancel_factor
huffman@45462
   260
  ("(l::nat) * m < n" | "(l::nat) < m * n") =
huffman@45462
   261
  {* fn phi => Nat_Numeral_Simprocs.less_cancel_factor *}
huffman@45462
   262
huffman@45462
   263
simproc_setup nat_le_cancel_factor
huffman@45462
   264
  ("(l::nat) * m <= n" | "(l::nat) <= m * n") =
huffman@45462
   265
  {* fn phi => Nat_Numeral_Simprocs.le_cancel_factor *}
huffman@45462
   266
huffman@45463
   267
simproc_setup nat_div_cancel_factor
huffman@45462
   268
  ("((l::nat) * m) div n" | "(l::nat) div (m * n)") =
huffman@45463
   269
  {* fn phi => Nat_Numeral_Simprocs.div_cancel_factor *}
huffman@45462
   270
huffman@45462
   271
simproc_setup nat_dvd_cancel_factor
huffman@45462
   272
  ("((l::nat) * m) dvd n" | "(l::nat) dvd (m * n)") =
huffman@45462
   273
  {* fn phi => Nat_Numeral_Simprocs.dvd_cancel_factor *}
huffman@45462
   274
huffman@47108
   275
(* FIXME: duplicate rule warnings for:
huffman@47108
   276
  ring_distribs
huffman@47108
   277
  numeral_plus_numeral numeral_times_numeral
huffman@47108
   278
  numeral_eq_iff numeral_less_iff numeral_le_iff
huffman@47108
   279
  numeral_neq_zero zero_neq_numeral zero_less_numeral
huffman@47108
   280
  if_True if_False *)
haftmann@33366
   281
declaration {* 
huffman@47108
   282
  K (Lin_Arith.add_simps ([@{thm Suc_numeral}, @{thm int_numeral}])
huffman@47108
   283
  #> Lin_Arith.add_simps (@{thms ring_distribs} @ [@{thm Let_numeral}, @{thm Let_neg_numeral}, @{thm Let_0}, @{thm Let_1},
haftmann@33366
   284
     @{thm nat_0}, @{thm nat_1},
huffman@47108
   285
     @{thm numeral_plus_numeral}, @{thm diff_nat_numeral}, @{thm numeral_times_numeral},
huffman@47108
   286
     @{thm numeral_eq_iff}, @{thm numeral_less_iff}, @{thm numeral_le_iff},
huffman@47108
   287
     @{thm le_Suc_numeral}, @{thm le_numeral_Suc},
huffman@47108
   288
     @{thm less_Suc_numeral}, @{thm less_numeral_Suc},
huffman@47108
   289
     @{thm Suc_eq_numeral}, @{thm eq_numeral_Suc},
haftmann@33366
   290
     @{thm mult_Suc}, @{thm mult_Suc_right},
haftmann@33366
   291
     @{thm add_Suc}, @{thm add_Suc_right},
huffman@47108
   292
     @{thm numeral_neq_zero}, @{thm zero_neq_numeral}, @{thm zero_less_numeral},
huffman@47108
   293
     @{thm of_int_numeral}, @{thm of_nat_numeral}, @{thm nat_numeral},
haftmann@33366
   294
     @{thm if_True}, @{thm if_False}])
huffman@45284
   295
  #> Lin_Arith.add_simprocs
huffman@45284
   296
      [@{simproc semiring_assoc_fold},
huffman@45284
   297
       @{simproc int_combine_numerals},
huffman@45284
   298
       @{simproc inteq_cancel_numerals},
huffman@45284
   299
       @{simproc intless_cancel_numerals},
huffman@45284
   300
       @{simproc intle_cancel_numerals}]
huffman@45436
   301
  #> Lin_Arith.add_simprocs
huffman@45462
   302
      [@{simproc nat_combine_numerals},
huffman@45436
   303
       @{simproc nateq_cancel_numerals},
huffman@45436
   304
       @{simproc natless_cancel_numerals},
huffman@45436
   305
       @{simproc natle_cancel_numerals},
huffman@45436
   306
       @{simproc natdiff_cancel_numerals}])
haftmann@33366
   307
*}
haftmann@33366
   308
haftmann@37886
   309
end