src/HOL/Numeral_Simprocs.thy
author huffman
Fri Oct 28 11:02:27 2011 +0200 (2011-10-28)
changeset 45284 ae78a4ffa81d
parent 37886 2f9d3fc1a8ac
child 45296 7a97b2bda137
permissions -rw-r--r--
use simproc_setup for cancellation simprocs, to get proper name bindings
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
haftmann@33366
    17
declare split_div [of _ _ "number_of k", standard, arith_split]
haftmann@33366
    18
declare split_mod [of _ _ "number_of k", standard, arith_split]
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@45284
   101
simproc_setup int_combine_numerals
huffman@45284
   102
  ("(i::'a::number_ring) + j" | "(i::'a::number_ring) - j") =
huffman@45284
   103
  {* fn phi => Numeral_Simprocs.combine_numerals *}
huffman@45284
   104
huffman@45284
   105
simproc_setup field_combine_numerals
huffman@45284
   106
  ("(i::'a::{field_inverse_zero, number_ring}) + j"
huffman@45284
   107
  |"(i::'a::{field_inverse_zero, number_ring}) - j") =
huffman@45284
   108
  {* fn phi => Numeral_Simprocs.field_combine_numerals *}
huffman@45284
   109
huffman@45284
   110
simproc_setup inteq_cancel_numerals
huffman@45284
   111
  ("(l::'a::number_ring) + m = n"
huffman@45284
   112
  |"(l::'a::number_ring) = m + n"
huffman@45284
   113
  |"(l::'a::number_ring) - m = n"
huffman@45284
   114
  |"(l::'a::number_ring) = m - n"
huffman@45284
   115
  |"(l::'a::number_ring) * m = n"
huffman@45284
   116
  |"(l::'a::number_ring) = m * n") =
huffman@45284
   117
  {* fn phi => Numeral_Simprocs.eq_cancel_numerals *}
huffman@45284
   118
huffman@45284
   119
simproc_setup intless_cancel_numerals
huffman@45284
   120
  ("(l::'a::{linordered_idom,number_ring}) + m < n"
huffman@45284
   121
  |"(l::'a::{linordered_idom,number_ring}) < m + n"
huffman@45284
   122
  |"(l::'a::{linordered_idom,number_ring}) - m < n"
huffman@45284
   123
  |"(l::'a::{linordered_idom,number_ring}) < m - n"
huffman@45284
   124
  |"(l::'a::{linordered_idom,number_ring}) * m < n"
huffman@45284
   125
  |"(l::'a::{linordered_idom,number_ring}) < m * n") =
huffman@45284
   126
  {* fn phi => Numeral_Simprocs.less_cancel_numerals *}
huffman@45284
   127
huffman@45284
   128
simproc_setup intle_cancel_numerals
huffman@45284
   129
  ("(l::'a::{linordered_idom,number_ring}) + m \<le> n"
huffman@45284
   130
  |"(l::'a::{linordered_idom,number_ring}) \<le> m + n"
huffman@45284
   131
  |"(l::'a::{linordered_idom,number_ring}) - m \<le> n"
huffman@45284
   132
  |"(l::'a::{linordered_idom,number_ring}) \<le> m - n"
huffman@45284
   133
  |"(l::'a::{linordered_idom,number_ring}) * m \<le> n"
huffman@45284
   134
  |"(l::'a::{linordered_idom,number_ring}) \<le> m * n") =
huffman@45284
   135
  {* fn phi => Numeral_Simprocs.le_cancel_numerals *}
huffman@45284
   136
huffman@45284
   137
simproc_setup ring_eq_cancel_numeral_factor
huffman@45284
   138
  ("(l::'a::{idom,number_ring}) * m = n"
huffman@45284
   139
  |"(l::'a::{idom,number_ring}) = m * n") =
huffman@45284
   140
  {* fn phi => Numeral_Simprocs.eq_cancel_numeral_factor *}
huffman@45284
   141
huffman@45284
   142
simproc_setup ring_less_cancel_numeral_factor
huffman@45284
   143
  ("(l::'a::{linordered_idom,number_ring}) * m < n"
huffman@45284
   144
  |"(l::'a::{linordered_idom,number_ring}) < m * n") =
huffman@45284
   145
  {* fn phi => Numeral_Simprocs.less_cancel_numeral_factor *}
huffman@45284
   146
huffman@45284
   147
simproc_setup ring_le_cancel_numeral_factor
huffman@45284
   148
  ("(l::'a::{linordered_idom,number_ring}) * m <= n"
huffman@45284
   149
  |"(l::'a::{linordered_idom,number_ring}) <= m * n") =
huffman@45284
   150
  {* fn phi => Numeral_Simprocs.le_cancel_numeral_factor *}
huffman@45284
   151
huffman@45284
   152
simproc_setup int_div_cancel_numeral_factors
huffman@45284
   153
  ("((l::'a::{semiring_div,number_ring}) * m) div n"
huffman@45284
   154
  |"(l::'a::{semiring_div,number_ring}) div (m * n)") =
huffman@45284
   155
  {* fn phi => Numeral_Simprocs.div_cancel_numeral_factor *}
huffman@45284
   156
huffman@45284
   157
simproc_setup divide_cancel_numeral_factor
huffman@45284
   158
  ("((l::'a::{field_inverse_zero,number_ring}) * m) / n"
huffman@45284
   159
  |"(l::'a::{field_inverse_zero,number_ring}) / (m * n)"
huffman@45284
   160
  |"((number_of v)::'a::{field_inverse_zero,number_ring}) / (number_of w)") =
huffman@45284
   161
  {* fn phi => Numeral_Simprocs.divide_cancel_numeral_factor *}
huffman@45284
   162
huffman@45284
   163
simproc_setup ring_eq_cancel_factor
huffman@45284
   164
  ("(l::'a::idom) * m = n" | "(l::'a::idom) = m * n") =
huffman@45284
   165
  {* fn phi => Numeral_Simprocs.eq_cancel_factor *}
huffman@45284
   166
huffman@45284
   167
simproc_setup linordered_ring_le_cancel_factor
huffman@45284
   168
  ("(l::'a::linordered_ring) * m <= n"
huffman@45284
   169
  |"(l::'a::linordered_ring) <= m * n") =
huffman@45284
   170
  {* fn phi => Numeral_Simprocs.le_cancel_factor *}
huffman@45284
   171
huffman@45284
   172
simproc_setup linordered_ring_less_cancel_factor
huffman@45284
   173
  ("(l::'a::linordered_ring) * m < n"
huffman@45284
   174
  |"(l::'a::linordered_ring) < m * n") =
huffman@45284
   175
  {* fn phi => Numeral_Simprocs.less_cancel_factor *}
huffman@45284
   176
huffman@45284
   177
simproc_setup int_div_cancel_factor
huffman@45284
   178
  ("((l::'a::semiring_div) * m) div n"
huffman@45284
   179
  |"(l::'a::semiring_div) div (m * n)") =
huffman@45284
   180
  {* fn phi => Numeral_Simprocs.div_cancel_factor *}
huffman@45284
   181
huffman@45284
   182
simproc_setup int_mod_cancel_factor
huffman@45284
   183
  ("((l::'a::semiring_div) * m) mod n"
huffman@45284
   184
  |"(l::'a::semiring_div) mod (m * n)") =
huffman@45284
   185
  {* fn phi => Numeral_Simprocs.mod_cancel_factor *}
huffman@45284
   186
huffman@45284
   187
simproc_setup dvd_cancel_factor
huffman@45284
   188
  ("((l::'a::idom) * m) dvd n"
huffman@45284
   189
  |"(l::'a::idom) dvd (m * n)") =
huffman@45284
   190
  {* fn phi => Numeral_Simprocs.dvd_cancel_factor *}
huffman@45284
   191
huffman@45284
   192
simproc_setup divide_cancel_factor
huffman@45284
   193
  ("((l::'a::field_inverse_zero) * m) / n"
huffman@45284
   194
  |"(l::'a::field_inverse_zero) / (m * n)") =
huffman@45284
   195
  {* fn phi => Numeral_Simprocs.divide_cancel_factor *}
huffman@45284
   196
haftmann@33366
   197
use "Tools/nat_numeral_simprocs.ML"
haftmann@33366
   198
haftmann@33366
   199
declaration {* 
haftmann@33366
   200
  K (Lin_Arith.add_simps (@{thms neg_simps} @ [@{thm Suc_nat_number_of}, @{thm int_nat_number_of}])
haftmann@33366
   201
  #> Lin_Arith.add_simps (@{thms ring_distribs} @ [@{thm Let_number_of}, @{thm Let_0}, @{thm Let_1},
haftmann@33366
   202
     @{thm nat_0}, @{thm nat_1},
haftmann@33366
   203
     @{thm add_nat_number_of}, @{thm diff_nat_number_of}, @{thm mult_nat_number_of},
haftmann@33366
   204
     @{thm eq_nat_number_of}, @{thm less_nat_number_of}, @{thm le_number_of_eq_not_less},
haftmann@33366
   205
     @{thm le_Suc_number_of}, @{thm le_number_of_Suc},
haftmann@33366
   206
     @{thm less_Suc_number_of}, @{thm less_number_of_Suc},
haftmann@33366
   207
     @{thm Suc_eq_number_of}, @{thm eq_number_of_Suc},
haftmann@33366
   208
     @{thm mult_Suc}, @{thm mult_Suc_right},
haftmann@33366
   209
     @{thm add_Suc}, @{thm add_Suc_right},
haftmann@33366
   210
     @{thm eq_number_of_0}, @{thm eq_0_number_of}, @{thm less_0_number_of},
haftmann@33366
   211
     @{thm of_int_number_of_eq}, @{thm of_nat_number_of_eq}, @{thm nat_number_of},
haftmann@33366
   212
     @{thm if_True}, @{thm if_False}])
huffman@45284
   213
  #> Lin_Arith.add_simprocs
huffman@45284
   214
      [@{simproc semiring_assoc_fold},
huffman@45284
   215
       @{simproc int_combine_numerals},
huffman@45284
   216
       @{simproc inteq_cancel_numerals},
huffman@45284
   217
       @{simproc intless_cancel_numerals},
huffman@45284
   218
       @{simproc intle_cancel_numerals}]
haftmann@33366
   219
  #> Lin_Arith.add_simprocs (Nat_Numeral_Simprocs.combine_numerals :: Nat_Numeral_Simprocs.cancel_numerals))
haftmann@33366
   220
*}
haftmann@33366
   221
haftmann@37886
   222
end