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