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