src/HOL/Numeral_Simprocs.thy
author wenzelm
Mon Dec 07 10:38:04 2015 +0100 (2015-12-07)
changeset 61799 4cf66f21b764
parent 60758 d8d85a8172b5
child 63648 f9f3006a5579
permissions -rw-r--r--
isabelle update_cartouches -c -t;
haftmann@33366
     1
(* Author: Various *)
haftmann@33366
     2
wenzelm@60758
     3
section \<open>Combination and Cancellation Simprocs for Numeral Expressions\<close>
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
wenzelm@61799
    26
text \<open>For \<open>combine_numerals\<close>\<close>
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
wenzelm@61799
    31
text \<open>For \<open>cancel_numerals\<close>\<close>
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
wenzelm@61799
    65
text \<open>For \<open>cancel_numeral_factors\<close>\<close>
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
wenzelm@61799
    86
text \<open>For \<open>cancel_factor\<close>\<close>
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
lp15@59757
   102
lemma numeral_times_minus_swap:
lp15@59757
   103
  fixes x:: "'a::comm_ring_1" shows  "numeral w * -x = x * - numeral w"
lp15@59757
   104
  by (simp add: mult.commute)
lp15@59757
   105
wenzelm@48891
   106
ML_file "Tools/numeral_simprocs.ML"
haftmann@33366
   107
huffman@45284
   108
simproc_setup semiring_assoc_fold
huffman@45284
   109
  ("(a::'a::comm_semiring_1_cancel) * b") =
wenzelm@60758
   110
  \<open>fn phi => Numeral_Simprocs.assoc_fold\<close>
huffman@45284
   111
huffman@47108
   112
(* TODO: see whether the type class can be generalized further *)
huffman@45284
   113
simproc_setup int_combine_numerals
huffman@47108
   114
  ("(i::'a::comm_ring_1) + j" | "(i::'a::comm_ring_1) - j") =
wenzelm@60758
   115
  \<open>fn phi => Numeral_Simprocs.combine_numerals\<close>
huffman@45284
   116
huffman@45284
   117
simproc_setup field_combine_numerals
haftmann@59867
   118
  ("(i::'a::{field,ring_char_0}) + j"
haftmann@59867
   119
  |"(i::'a::{field,ring_char_0}) - j") =
wenzelm@60758
   120
  \<open>fn phi => Numeral_Simprocs.field_combine_numerals\<close>
huffman@45284
   121
huffman@45284
   122
simproc_setup inteq_cancel_numerals
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 = n"
huffman@47108
   126
  |"(l::'a::comm_ring_1) = m - n"
huffman@47108
   127
  |"(l::'a::comm_ring_1) * m = n"
huffman@47108
   128
  |"(l::'a::comm_ring_1) = m * n"
huffman@47108
   129
  |"- (l::'a::comm_ring_1) = m"
huffman@47108
   130
  |"(l::'a::comm_ring_1) = - m") =
wenzelm@60758
   131
  \<open>fn phi => Numeral_Simprocs.eq_cancel_numerals\<close>
huffman@45284
   132
huffman@45284
   133
simproc_setup intless_cancel_numerals
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 < n"
huffman@47108
   137
  |"(l::'a::linordered_idom) < m - n"
huffman@47108
   138
  |"(l::'a::linordered_idom) * m < n"
huffman@47108
   139
  |"(l::'a::linordered_idom) < m * n"
huffman@47108
   140
  |"- (l::'a::linordered_idom) < m"
huffman@47108
   141
  |"(l::'a::linordered_idom) < - m") =
wenzelm@60758
   142
  \<open>fn phi => Numeral_Simprocs.less_cancel_numerals\<close>
huffman@45284
   143
huffman@45284
   144
simproc_setup intle_cancel_numerals
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) - m \<le> n"
huffman@47108
   148
  |"(l::'a::linordered_idom) \<le> m - n"
huffman@47108
   149
  |"(l::'a::linordered_idom) * m \<le> n"
huffman@47108
   150
  |"(l::'a::linordered_idom) \<le> m * n"
huffman@47108
   151
  |"- (l::'a::linordered_idom) \<le> m"
huffman@47108
   152
  |"(l::'a::linordered_idom) \<le> - m") =
wenzelm@60758
   153
  \<open>fn phi => Numeral_Simprocs.le_cancel_numerals\<close>
huffman@45284
   154
huffman@45284
   155
simproc_setup ring_eq_cancel_numeral_factor
huffman@47108
   156
  ("(l::'a::{idom,ring_char_0}) * m = n"
huffman@47108
   157
  |"(l::'a::{idom,ring_char_0}) = m * n") =
wenzelm@60758
   158
  \<open>fn phi => Numeral_Simprocs.eq_cancel_numeral_factor\<close>
huffman@45284
   159
huffman@45284
   160
simproc_setup ring_less_cancel_numeral_factor
huffman@47108
   161
  ("(l::'a::linordered_idom) * m < n"
huffman@47108
   162
  |"(l::'a::linordered_idom) < m * n") =
wenzelm@60758
   163
  \<open>fn phi => Numeral_Simprocs.less_cancel_numeral_factor\<close>
huffman@45284
   164
huffman@45284
   165
simproc_setup ring_le_cancel_numeral_factor
huffman@47108
   166
  ("(l::'a::linordered_idom) * m <= n"
huffman@47108
   167
  |"(l::'a::linordered_idom) <= m * n") =
wenzelm@60758
   168
  \<open>fn phi => Numeral_Simprocs.le_cancel_numeral_factor\<close>
huffman@45284
   169
huffman@47108
   170
(* TODO: remove comm_ring_1 constraint if possible *)
huffman@45284
   171
simproc_setup int_div_cancel_numeral_factors
huffman@47108
   172
  ("((l::'a::{semiring_div,comm_ring_1,ring_char_0}) * m) div n"
huffman@47108
   173
  |"(l::'a::{semiring_div,comm_ring_1,ring_char_0}) div (m * n)") =
wenzelm@60758
   174
  \<open>fn phi => Numeral_Simprocs.div_cancel_numeral_factor\<close>
huffman@45284
   175
huffman@45284
   176
simproc_setup divide_cancel_numeral_factor
haftmann@59867
   177
  ("((l::'a::{field,ring_char_0}) * m) / n"
haftmann@59867
   178
  |"(l::'a::{field,ring_char_0}) / (m * n)"
haftmann@59867
   179
  |"((numeral v)::'a::{field,ring_char_0}) / (numeral w)") =
wenzelm@60758
   180
  \<open>fn phi => Numeral_Simprocs.divide_cancel_numeral_factor\<close>
huffman@45284
   181
huffman@45284
   182
simproc_setup ring_eq_cancel_factor
huffman@45284
   183
  ("(l::'a::idom) * m = n" | "(l::'a::idom) = m * n") =
wenzelm@60758
   184
  \<open>fn phi => Numeral_Simprocs.eq_cancel_factor\<close>
huffman@45284
   185
huffman@45284
   186
simproc_setup linordered_ring_le_cancel_factor
huffman@45296
   187
  ("(l::'a::linordered_idom) * m <= n"
huffman@45296
   188
  |"(l::'a::linordered_idom) <= m * n") =
wenzelm@60758
   189
  \<open>fn phi => Numeral_Simprocs.le_cancel_factor\<close>
huffman@45284
   190
huffman@45284
   191
simproc_setup linordered_ring_less_cancel_factor
huffman@45296
   192
  ("(l::'a::linordered_idom) * m < n"
huffman@45296
   193
  |"(l::'a::linordered_idom) < m * n") =
wenzelm@60758
   194
  \<open>fn phi => Numeral_Simprocs.less_cancel_factor\<close>
huffman@45284
   195
huffman@45284
   196
simproc_setup int_div_cancel_factor
huffman@45284
   197
  ("((l::'a::semiring_div) * m) div n"
huffman@45284
   198
  |"(l::'a::semiring_div) div (m * n)") =
wenzelm@60758
   199
  \<open>fn phi => Numeral_Simprocs.div_cancel_factor\<close>
huffman@45284
   200
huffman@45284
   201
simproc_setup int_mod_cancel_factor
huffman@45284
   202
  ("((l::'a::semiring_div) * m) mod n"
huffman@45284
   203
  |"(l::'a::semiring_div) mod (m * n)") =
wenzelm@60758
   204
  \<open>fn phi => Numeral_Simprocs.mod_cancel_factor\<close>
huffman@45284
   205
huffman@45284
   206
simproc_setup dvd_cancel_factor
huffman@45284
   207
  ("((l::'a::idom) * m) dvd n"
huffman@45284
   208
  |"(l::'a::idom) dvd (m * n)") =
wenzelm@60758
   209
  \<open>fn phi => Numeral_Simprocs.dvd_cancel_factor\<close>
huffman@45284
   210
huffman@45284
   211
simproc_setup divide_cancel_factor
haftmann@59867
   212
  ("((l::'a::field) * m) / n"
haftmann@59867
   213
  |"(l::'a::field) / (m * n)") =
wenzelm@60758
   214
  \<open>fn phi => Numeral_Simprocs.divide_cancel_factor\<close>
huffman@45284
   215
wenzelm@48891
   216
ML_file "Tools/nat_numeral_simprocs.ML"
haftmann@33366
   217
huffman@45462
   218
simproc_setup nat_combine_numerals
huffman@45462
   219
  ("(i::nat) + j" | "Suc (i + j)") =
wenzelm@60758
   220
  \<open>fn phi => Nat_Numeral_Simprocs.combine_numerals\<close>
huffman@45462
   221
huffman@45436
   222
simproc_setup nateq_cancel_numerals
huffman@45436
   223
  ("(l::nat) + m = n" | "(l::nat) = m + n" |
huffman@45436
   224
   "(l::nat) * m = n" | "(l::nat) = m * n" |
huffman@45436
   225
   "Suc m = n" | "m = Suc n") =
wenzelm@60758
   226
  \<open>fn phi => Nat_Numeral_Simprocs.eq_cancel_numerals\<close>
huffman@45436
   227
huffman@45436
   228
simproc_setup natless_cancel_numerals
huffman@45436
   229
  ("(l::nat) + m < n" | "(l::nat) < m + n" |
huffman@45436
   230
   "(l::nat) * m < n" | "(l::nat) < m * n" |
huffman@45436
   231
   "Suc m < n" | "m < Suc n") =
wenzelm@60758
   232
  \<open>fn phi => Nat_Numeral_Simprocs.less_cancel_numerals\<close>
huffman@45436
   233
huffman@45436
   234
simproc_setup natle_cancel_numerals
huffman@45436
   235
  ("(l::nat) + m \<le> n" | "(l::nat) \<le> m + n" |
huffman@45436
   236
   "(l::nat) * m \<le> n" | "(l::nat) \<le> m * n" |
huffman@45436
   237
   "Suc m \<le> n" | "m \<le> Suc n") =
wenzelm@60758
   238
  \<open>fn phi => Nat_Numeral_Simprocs.le_cancel_numerals\<close>
huffman@45436
   239
huffman@45436
   240
simproc_setup natdiff_cancel_numerals
huffman@45436
   241
  ("((l::nat) + m) - n" | "(l::nat) - (m + n)" |
huffman@45436
   242
   "(l::nat) * m - n" | "(l::nat) - m * n" |
huffman@45436
   243
   "Suc m - n" | "m - Suc n") =
wenzelm@60758
   244
  \<open>fn phi => Nat_Numeral_Simprocs.diff_cancel_numerals\<close>
huffman@45436
   245
huffman@45463
   246
simproc_setup nat_eq_cancel_numeral_factor
huffman@45463
   247
  ("(l::nat) * m = n" | "(l::nat) = m * n") =
wenzelm@60758
   248
  \<open>fn phi => Nat_Numeral_Simprocs.eq_cancel_numeral_factor\<close>
huffman@45463
   249
huffman@45463
   250
simproc_setup nat_less_cancel_numeral_factor
huffman@45463
   251
  ("(l::nat) * m < n" | "(l::nat) < m * n") =
wenzelm@60758
   252
  \<open>fn phi => Nat_Numeral_Simprocs.less_cancel_numeral_factor\<close>
huffman@45463
   253
huffman@45463
   254
simproc_setup nat_le_cancel_numeral_factor
huffman@45463
   255
  ("(l::nat) * m <= n" | "(l::nat) <= m * n") =
wenzelm@60758
   256
  \<open>fn phi => Nat_Numeral_Simprocs.le_cancel_numeral_factor\<close>
huffman@45463
   257
huffman@45463
   258
simproc_setup nat_div_cancel_numeral_factor
huffman@45463
   259
  ("((l::nat) * m) div n" | "(l::nat) div (m * n)") =
wenzelm@60758
   260
  \<open>fn phi => Nat_Numeral_Simprocs.div_cancel_numeral_factor\<close>
huffman@45463
   261
huffman@45463
   262
simproc_setup nat_dvd_cancel_numeral_factor
huffman@45463
   263
  ("((l::nat) * m) dvd n" | "(l::nat) dvd (m * n)") =
wenzelm@60758
   264
  \<open>fn phi => Nat_Numeral_Simprocs.dvd_cancel_numeral_factor\<close>
huffman@45463
   265
huffman@45462
   266
simproc_setup nat_eq_cancel_factor
huffman@45462
   267
  ("(l::nat) * m = n" | "(l::nat) = m * n") =
wenzelm@60758
   268
  \<open>fn phi => Nat_Numeral_Simprocs.eq_cancel_factor\<close>
huffman@45462
   269
huffman@45462
   270
simproc_setup nat_less_cancel_factor
huffman@45462
   271
  ("(l::nat) * m < n" | "(l::nat) < m * n") =
wenzelm@60758
   272
  \<open>fn phi => Nat_Numeral_Simprocs.less_cancel_factor\<close>
huffman@45462
   273
huffman@45462
   274
simproc_setup nat_le_cancel_factor
huffman@45462
   275
  ("(l::nat) * m <= n" | "(l::nat) <= m * n") =
wenzelm@60758
   276
  \<open>fn phi => Nat_Numeral_Simprocs.le_cancel_factor\<close>
huffman@45462
   277
huffman@45463
   278
simproc_setup nat_div_cancel_factor
huffman@45462
   279
  ("((l::nat) * m) div n" | "(l::nat) div (m * n)") =
wenzelm@60758
   280
  \<open>fn phi => Nat_Numeral_Simprocs.div_cancel_factor\<close>
huffman@45462
   281
huffman@45462
   282
simproc_setup nat_dvd_cancel_factor
huffman@45462
   283
  ("((l::nat) * m) dvd n" | "(l::nat) dvd (m * n)") =
wenzelm@60758
   284
  \<open>fn phi => Nat_Numeral_Simprocs.dvd_cancel_factor\<close>
huffman@45462
   285
wenzelm@60758
   286
declaration \<open>
haftmann@54249
   287
  K (Lin_Arith.add_simprocs
huffman@45284
   288
      [@{simproc semiring_assoc_fold},
huffman@45284
   289
       @{simproc int_combine_numerals},
huffman@45284
   290
       @{simproc inteq_cancel_numerals},
huffman@45284
   291
       @{simproc intless_cancel_numerals},
nipkow@55375
   292
       @{simproc intle_cancel_numerals},
nipkow@55375
   293
       @{simproc field_combine_numerals}]
huffman@45436
   294
  #> Lin_Arith.add_simprocs
huffman@45462
   295
      [@{simproc nat_combine_numerals},
huffman@45436
   296
       @{simproc nateq_cancel_numerals},
huffman@45436
   297
       @{simproc natless_cancel_numerals},
huffman@45436
   298
       @{simproc natle_cancel_numerals},
huffman@45436
   299
       @{simproc natdiff_cancel_numerals}])
wenzelm@60758
   300
\<close>
haftmann@33366
   301
haftmann@37886
   302
end