src/HOL/Numeral_Simprocs.thy
author haftmann
Tue Nov 19 10:05:53 2013 +0100 (2013-11-19)
changeset 54489 03ff4d1e6784
parent 54249 ce00f2fef556
child 55375 d26d5f988d71
permissions -rw-r--r--
eliminiated neg_numeral in favour of - (numeral _)
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
begin
haftmann@33366
     8
wenzelm@48891
     9
ML_file "~~/src/Provers/Arith/assoc_fold.ML"
wenzelm@48891
    10
ML_file "~~/src/Provers/Arith/cancel_numerals.ML"
wenzelm@48891
    11
ML_file "~~/src/Provers/Arith/combine_numerals.ML"
wenzelm@48891
    12
ML_file "~~/src/Provers/Arith/cancel_numeral_factor.ML"
wenzelm@48891
    13
ML_file "~~/src/Provers/Arith/extract_common_term.ML"
wenzelm@48891
    14
huffman@47255
    15
lemmas semiring_norm =
haftmann@54249
    16
  Let_def arith_simps diff_nat_numeral rel_simps
huffman@47255
    17
  if_False if_True
huffman@47255
    18
  add_0 add_Suc add_numeral_left
huffman@47255
    19
  add_neg_numeral_left mult_numeral_left
haftmann@54489
    20
  numeral_One [symmetric] uminus_numeral_One [symmetric] Suc_eq_plus1
huffman@47255
    21
  eq_numeral_iff_iszero not_iszero_Numeral1
huffman@47255
    22
huffman@47108
    23
declare split_div [of _ _ "numeral k", arith_split] for k
huffman@47108
    24
declare split_mod [of _ _ "numeral k", arith_split] for k
haftmann@33366
    25
haftmann@33366
    26
text {* For @{text combine_numerals} *}
haftmann@33366
    27
haftmann@33366
    28
lemma left_add_mult_distrib: "i*u + (j*u + k) = (i+j)*u + (k::nat)"
haftmann@33366
    29
by (simp add: add_mult_distrib)
haftmann@33366
    30
haftmann@33366
    31
text {* For @{text cancel_numerals} *}
haftmann@33366
    32
haftmann@33366
    33
lemma nat_diff_add_eq1:
haftmann@33366
    34
     "j <= (i::nat) ==> ((i*u + m) - (j*u + n)) = (((i-j)*u + m) - n)"
haftmann@33366
    35
by (simp split add: nat_diff_split add: add_mult_distrib)
haftmann@33366
    36
haftmann@33366
    37
lemma nat_diff_add_eq2:
haftmann@33366
    38
     "i <= (j::nat) ==> ((i*u + m) - (j*u + n)) = (m - ((j-i)*u + n))"
haftmann@33366
    39
by (simp split add: nat_diff_split add: add_mult_distrib)
haftmann@33366
    40
haftmann@33366
    41
lemma nat_eq_add_iff1:
haftmann@33366
    42
     "j <= (i::nat) ==> (i*u + m = j*u + n) = ((i-j)*u + m = n)"
haftmann@33366
    43
by (auto split add: nat_diff_split simp add: add_mult_distrib)
haftmann@33366
    44
haftmann@33366
    45
lemma nat_eq_add_iff2:
haftmann@33366
    46
     "i <= (j::nat) ==> (i*u + m = j*u + n) = (m = (j-i)*u + n)"
haftmann@33366
    47
by (auto split add: nat_diff_split simp add: add_mult_distrib)
haftmann@33366
    48
haftmann@33366
    49
lemma nat_less_add_iff1:
haftmann@33366
    50
     "j <= (i::nat) ==> (i*u + m < j*u + n) = ((i-j)*u + m < n)"
haftmann@33366
    51
by (auto split add: nat_diff_split simp add: add_mult_distrib)
haftmann@33366
    52
haftmann@33366
    53
lemma nat_less_add_iff2:
haftmann@33366
    54
     "i <= (j::nat) ==> (i*u + m < j*u + n) = (m < (j-i)*u + n)"
haftmann@33366
    55
by (auto split add: nat_diff_split simp add: add_mult_distrib)
haftmann@33366
    56
haftmann@33366
    57
lemma nat_le_add_iff1:
haftmann@33366
    58
     "j <= (i::nat) ==> (i*u + m <= j*u + n) = ((i-j)*u + m <= n)"
haftmann@33366
    59
by (auto split add: nat_diff_split simp add: add_mult_distrib)
haftmann@33366
    60
haftmann@33366
    61
lemma nat_le_add_iff2:
haftmann@33366
    62
     "i <= (j::nat) ==> (i*u + m <= j*u + n) = (m <= (j-i)*u + n)"
haftmann@33366
    63
by (auto split add: nat_diff_split simp add: add_mult_distrib)
haftmann@33366
    64
haftmann@33366
    65
text {* For @{text cancel_numeral_factors} *}
haftmann@33366
    66
haftmann@33366
    67
lemma nat_mult_le_cancel1: "(0::nat) < k ==> (k*m <= k*n) = (m<=n)"
haftmann@33366
    68
by auto
haftmann@33366
    69
haftmann@33366
    70
lemma nat_mult_less_cancel1: "(0::nat) < k ==> (k*m < k*n) = (m<n)"
haftmann@33366
    71
by auto
haftmann@33366
    72
haftmann@33366
    73
lemma nat_mult_eq_cancel1: "(0::nat) < k ==> (k*m = k*n) = (m=n)"
haftmann@33366
    74
by auto
haftmann@33366
    75
haftmann@33366
    76
lemma nat_mult_div_cancel1: "(0::nat) < k ==> (k*m) div (k*n) = (m div n)"
haftmann@33366
    77
by auto
haftmann@33366
    78
haftmann@33366
    79
lemma nat_mult_dvd_cancel_disj[simp]:
haftmann@33366
    80
  "(k*m) dvd (k*n) = (k=0 | m dvd (n::nat))"
huffman@47159
    81
by (auto simp: dvd_eq_mod_eq_0 mod_mult_mult1)
haftmann@33366
    82
haftmann@33366
    83
lemma nat_mult_dvd_cancel1: "0 < k \<Longrightarrow> (k*m) dvd (k*n::nat) = (m dvd n)"
haftmann@33366
    84
by(auto)
haftmann@33366
    85
haftmann@33366
    86
text {* For @{text cancel_factor} *}
haftmann@33366
    87
haftmann@54489
    88
lemmas nat_mult_le_cancel_disj = mult_le_cancel1
haftmann@33366
    89
haftmann@54489
    90
lemmas nat_mult_less_cancel_disj = mult_less_cancel1
haftmann@33366
    91
haftmann@54489
    92
lemma nat_mult_eq_cancel_disj:
haftmann@54489
    93
  fixes k m n :: nat
haftmann@54489
    94
  shows "k * m = k * n \<longleftrightarrow> k = 0 \<or> m = n"
haftmann@54489
    95
  by auto
haftmann@33366
    96
haftmann@54489
    97
lemma nat_mult_div_cancel_disj [simp]:
haftmann@54489
    98
  fixes k m n :: nat
haftmann@54489
    99
  shows "(k * m) div (k * n) = (if k = 0 then 0 else m div n)"
haftmann@54489
   100
  by (fact div_mult_mult1_if)
haftmann@33366
   101
wenzelm@48891
   102
ML_file "Tools/numeral_simprocs.ML"
haftmann@33366
   103
huffman@45284
   104
simproc_setup semiring_assoc_fold
huffman@45284
   105
  ("(a::'a::comm_semiring_1_cancel) * b") =
huffman@45284
   106
  {* fn phi => Numeral_Simprocs.assoc_fold *}
huffman@45284
   107
huffman@47108
   108
(* TODO: see whether the type class can be generalized further *)
huffman@45284
   109
simproc_setup int_combine_numerals
huffman@47108
   110
  ("(i::'a::comm_ring_1) + j" | "(i::'a::comm_ring_1) - j") =
huffman@45284
   111
  {* fn phi => Numeral_Simprocs.combine_numerals *}
huffman@45284
   112
huffman@45284
   113
simproc_setup field_combine_numerals
huffman@47108
   114
  ("(i::'a::{field_inverse_zero,ring_char_0}) + j"
huffman@47108
   115
  |"(i::'a::{field_inverse_zero,ring_char_0}) - j") =
huffman@45284
   116
  {* fn phi => Numeral_Simprocs.field_combine_numerals *}
huffman@45284
   117
huffman@45284
   118
simproc_setup inteq_cancel_numerals
huffman@47108
   119
  ("(l::'a::comm_ring_1) + m = n"
huffman@47108
   120
  |"(l::'a::comm_ring_1) = m + n"
huffman@47108
   121
  |"(l::'a::comm_ring_1) - m = n"
huffman@47108
   122
  |"(l::'a::comm_ring_1) = m - n"
huffman@47108
   123
  |"(l::'a::comm_ring_1) * m = n"
huffman@47108
   124
  |"(l::'a::comm_ring_1) = m * n"
huffman@47108
   125
  |"- (l::'a::comm_ring_1) = m"
huffman@47108
   126
  |"(l::'a::comm_ring_1) = - m") =
huffman@45284
   127
  {* fn phi => Numeral_Simprocs.eq_cancel_numerals *}
huffman@45284
   128
huffman@45284
   129
simproc_setup intless_cancel_numerals
huffman@47108
   130
  ("(l::'a::linordered_idom) + m < n"
huffman@47108
   131
  |"(l::'a::linordered_idom) < m + n"
huffman@47108
   132
  |"(l::'a::linordered_idom) - m < n"
huffman@47108
   133
  |"(l::'a::linordered_idom) < m - n"
huffman@47108
   134
  |"(l::'a::linordered_idom) * m < n"
huffman@47108
   135
  |"(l::'a::linordered_idom) < m * n"
huffman@47108
   136
  |"- (l::'a::linordered_idom) < m"
huffman@47108
   137
  |"(l::'a::linordered_idom) < - m") =
huffman@45284
   138
  {* fn phi => Numeral_Simprocs.less_cancel_numerals *}
huffman@45284
   139
huffman@45284
   140
simproc_setup intle_cancel_numerals
huffman@47108
   141
  ("(l::'a::linordered_idom) + m \<le> n"
huffman@47108
   142
  |"(l::'a::linordered_idom) \<le> m + n"
huffman@47108
   143
  |"(l::'a::linordered_idom) - m \<le> n"
huffman@47108
   144
  |"(l::'a::linordered_idom) \<le> m - n"
huffman@47108
   145
  |"(l::'a::linordered_idom) * m \<le> n"
huffman@47108
   146
  |"(l::'a::linordered_idom) \<le> m * n"
huffman@47108
   147
  |"- (l::'a::linordered_idom) \<le> m"
huffman@47108
   148
  |"(l::'a::linordered_idom) \<le> - m") =
huffman@45284
   149
  {* fn phi => Numeral_Simprocs.le_cancel_numerals *}
huffman@45284
   150
huffman@45284
   151
simproc_setup ring_eq_cancel_numeral_factor
huffman@47108
   152
  ("(l::'a::{idom,ring_char_0}) * m = n"
huffman@47108
   153
  |"(l::'a::{idom,ring_char_0}) = m * n") =
huffman@45284
   154
  {* fn phi => Numeral_Simprocs.eq_cancel_numeral_factor *}
huffman@45284
   155
huffman@45284
   156
simproc_setup ring_less_cancel_numeral_factor
huffman@47108
   157
  ("(l::'a::linordered_idom) * m < n"
huffman@47108
   158
  |"(l::'a::linordered_idom) < m * n") =
huffman@45284
   159
  {* fn phi => Numeral_Simprocs.less_cancel_numeral_factor *}
huffman@45284
   160
huffman@45284
   161
simproc_setup ring_le_cancel_numeral_factor
huffman@47108
   162
  ("(l::'a::linordered_idom) * m <= n"
huffman@47108
   163
  |"(l::'a::linordered_idom) <= m * n") =
huffman@45284
   164
  {* fn phi => Numeral_Simprocs.le_cancel_numeral_factor *}
huffman@45284
   165
huffman@47108
   166
(* TODO: remove comm_ring_1 constraint if possible *)
huffman@45284
   167
simproc_setup int_div_cancel_numeral_factors
huffman@47108
   168
  ("((l::'a::{semiring_div,comm_ring_1,ring_char_0}) * m) div n"
huffman@47108
   169
  |"(l::'a::{semiring_div,comm_ring_1,ring_char_0}) div (m * n)") =
huffman@45284
   170
  {* fn phi => Numeral_Simprocs.div_cancel_numeral_factor *}
huffman@45284
   171
huffman@45284
   172
simproc_setup divide_cancel_numeral_factor
huffman@47108
   173
  ("((l::'a::{field_inverse_zero,ring_char_0}) * m) / n"
huffman@47108
   174
  |"(l::'a::{field_inverse_zero,ring_char_0}) / (m * n)"
huffman@47108
   175
  |"((numeral v)::'a::{field_inverse_zero,ring_char_0}) / (numeral w)") =
huffman@45284
   176
  {* fn phi => Numeral_Simprocs.divide_cancel_numeral_factor *}
huffman@45284
   177
huffman@45284
   178
simproc_setup ring_eq_cancel_factor
huffman@45284
   179
  ("(l::'a::idom) * m = n" | "(l::'a::idom) = m * n") =
huffman@45284
   180
  {* fn phi => Numeral_Simprocs.eq_cancel_factor *}
huffman@45284
   181
huffman@45284
   182
simproc_setup linordered_ring_le_cancel_factor
huffman@45296
   183
  ("(l::'a::linordered_idom) * m <= n"
huffman@45296
   184
  |"(l::'a::linordered_idom) <= m * n") =
huffman@45284
   185
  {* fn phi => Numeral_Simprocs.le_cancel_factor *}
huffman@45284
   186
huffman@45284
   187
simproc_setup linordered_ring_less_cancel_factor
huffman@45296
   188
  ("(l::'a::linordered_idom) * m < n"
huffman@45296
   189
  |"(l::'a::linordered_idom) < m * n") =
huffman@45284
   190
  {* fn phi => Numeral_Simprocs.less_cancel_factor *}
huffman@45284
   191
huffman@45284
   192
simproc_setup int_div_cancel_factor
huffman@45284
   193
  ("((l::'a::semiring_div) * m) div n"
huffman@45284
   194
  |"(l::'a::semiring_div) div (m * n)") =
huffman@45284
   195
  {* fn phi => Numeral_Simprocs.div_cancel_factor *}
huffman@45284
   196
huffman@45284
   197
simproc_setup int_mod_cancel_factor
huffman@45284
   198
  ("((l::'a::semiring_div) * m) mod n"
huffman@45284
   199
  |"(l::'a::semiring_div) mod (m * n)") =
huffman@45284
   200
  {* fn phi => Numeral_Simprocs.mod_cancel_factor *}
huffman@45284
   201
huffman@45284
   202
simproc_setup dvd_cancel_factor
huffman@45284
   203
  ("((l::'a::idom) * m) dvd n"
huffman@45284
   204
  |"(l::'a::idom) dvd (m * n)") =
huffman@45284
   205
  {* fn phi => Numeral_Simprocs.dvd_cancel_factor *}
huffman@45284
   206
huffman@45284
   207
simproc_setup divide_cancel_factor
huffman@45284
   208
  ("((l::'a::field_inverse_zero) * m) / n"
huffman@45284
   209
  |"(l::'a::field_inverse_zero) / (m * n)") =
huffman@45284
   210
  {* fn phi => Numeral_Simprocs.divide_cancel_factor *}
huffman@45284
   211
wenzelm@48891
   212
ML_file "Tools/nat_numeral_simprocs.ML"
haftmann@33366
   213
huffman@45462
   214
simproc_setup nat_combine_numerals
huffman@45462
   215
  ("(i::nat) + j" | "Suc (i + j)") =
huffman@45462
   216
  {* fn phi => Nat_Numeral_Simprocs.combine_numerals *}
huffman@45462
   217
huffman@45436
   218
simproc_setup nateq_cancel_numerals
huffman@45436
   219
  ("(l::nat) + m = n" | "(l::nat) = m + n" |
huffman@45436
   220
   "(l::nat) * m = n" | "(l::nat) = m * n" |
huffman@45436
   221
   "Suc m = n" | "m = Suc n") =
huffman@45436
   222
  {* fn phi => Nat_Numeral_Simprocs.eq_cancel_numerals *}
huffman@45436
   223
huffman@45436
   224
simproc_setup natless_cancel_numerals
huffman@45436
   225
  ("(l::nat) + m < n" | "(l::nat) < m + n" |
huffman@45436
   226
   "(l::nat) * m < n" | "(l::nat) < m * n" |
huffman@45436
   227
   "Suc m < n" | "m < Suc n") =
huffman@45436
   228
  {* fn phi => Nat_Numeral_Simprocs.less_cancel_numerals *}
huffman@45436
   229
huffman@45436
   230
simproc_setup natle_cancel_numerals
huffman@45436
   231
  ("(l::nat) + m \<le> n" | "(l::nat) \<le> m + n" |
huffman@45436
   232
   "(l::nat) * m \<le> n" | "(l::nat) \<le> m * n" |
huffman@45436
   233
   "Suc m \<le> n" | "m \<le> Suc n") =
huffman@45436
   234
  {* fn phi => Nat_Numeral_Simprocs.le_cancel_numerals *}
huffman@45436
   235
huffman@45436
   236
simproc_setup natdiff_cancel_numerals
huffman@45436
   237
  ("((l::nat) + m) - n" | "(l::nat) - (m + n)" |
huffman@45436
   238
   "(l::nat) * m - n" | "(l::nat) - m * n" |
huffman@45436
   239
   "Suc m - n" | "m - Suc n") =
huffman@45436
   240
  {* fn phi => Nat_Numeral_Simprocs.diff_cancel_numerals *}
huffman@45436
   241
huffman@45463
   242
simproc_setup nat_eq_cancel_numeral_factor
huffman@45463
   243
  ("(l::nat) * m = n" | "(l::nat) = m * n") =
huffman@45463
   244
  {* fn phi => Nat_Numeral_Simprocs.eq_cancel_numeral_factor *}
huffman@45463
   245
huffman@45463
   246
simproc_setup nat_less_cancel_numeral_factor
huffman@45463
   247
  ("(l::nat) * m < n" | "(l::nat) < m * n") =
huffman@45463
   248
  {* fn phi => Nat_Numeral_Simprocs.less_cancel_numeral_factor *}
huffman@45463
   249
huffman@45463
   250
simproc_setup nat_le_cancel_numeral_factor
huffman@45463
   251
  ("(l::nat) * m <= n" | "(l::nat) <= m * n") =
huffman@45463
   252
  {* fn phi => Nat_Numeral_Simprocs.le_cancel_numeral_factor *}
huffman@45463
   253
huffman@45463
   254
simproc_setup nat_div_cancel_numeral_factor
huffman@45463
   255
  ("((l::nat) * m) div n" | "(l::nat) div (m * n)") =
huffman@45463
   256
  {* fn phi => Nat_Numeral_Simprocs.div_cancel_numeral_factor *}
huffman@45463
   257
huffman@45463
   258
simproc_setup nat_dvd_cancel_numeral_factor
huffman@45463
   259
  ("((l::nat) * m) dvd n" | "(l::nat) dvd (m * n)") =
huffman@45463
   260
  {* fn phi => Nat_Numeral_Simprocs.dvd_cancel_numeral_factor *}
huffman@45463
   261
huffman@45462
   262
simproc_setup nat_eq_cancel_factor
huffman@45462
   263
  ("(l::nat) * m = n" | "(l::nat) = m * n") =
huffman@45462
   264
  {* fn phi => Nat_Numeral_Simprocs.eq_cancel_factor *}
huffman@45462
   265
huffman@45462
   266
simproc_setup nat_less_cancel_factor
huffman@45462
   267
  ("(l::nat) * m < n" | "(l::nat) < m * n") =
huffman@45462
   268
  {* fn phi => Nat_Numeral_Simprocs.less_cancel_factor *}
huffman@45462
   269
huffman@45462
   270
simproc_setup nat_le_cancel_factor
huffman@45462
   271
  ("(l::nat) * m <= n" | "(l::nat) <= m * n") =
huffman@45462
   272
  {* fn phi => Nat_Numeral_Simprocs.le_cancel_factor *}
huffman@45462
   273
huffman@45463
   274
simproc_setup nat_div_cancel_factor
huffman@45462
   275
  ("((l::nat) * m) div n" | "(l::nat) div (m * n)") =
huffman@45463
   276
  {* fn phi => Nat_Numeral_Simprocs.div_cancel_factor *}
huffman@45462
   277
huffman@45462
   278
simproc_setup nat_dvd_cancel_factor
huffman@45462
   279
  ("((l::nat) * m) dvd n" | "(l::nat) dvd (m * n)") =
huffman@45462
   280
  {* fn phi => Nat_Numeral_Simprocs.dvd_cancel_factor *}
huffman@45462
   281
haftmann@33366
   282
declaration {* 
haftmann@54249
   283
  K (Lin_Arith.add_simprocs
huffman@45284
   284
      [@{simproc semiring_assoc_fold},
huffman@45284
   285
       @{simproc int_combine_numerals},
huffman@45284
   286
       @{simproc inteq_cancel_numerals},
huffman@45284
   287
       @{simproc intless_cancel_numerals},
huffman@45284
   288
       @{simproc intle_cancel_numerals}]
huffman@45436
   289
  #> Lin_Arith.add_simprocs
huffman@45462
   290
      [@{simproc nat_combine_numerals},
huffman@45436
   291
       @{simproc nateq_cancel_numerals},
huffman@45436
   292
       @{simproc natless_cancel_numerals},
huffman@45436
   293
       @{simproc natle_cancel_numerals},
huffman@45436
   294
       @{simproc natdiff_cancel_numerals}])
haftmann@33366
   295
*}
haftmann@33366
   296
haftmann@37886
   297
end