src/HOL/Numeral_Simprocs.thy
author haftmann
Sun Sep 21 16:56:11 2014 +0200 (2014-09-21)
changeset 58410 6d46ad54a2ab
parent 55375 d26d5f988d71
child 58889 5b7a9633cfa8
permissions -rw-r--r--
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
     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 diff_nat_numeral 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_One [symmetric] uminus_numeral_One [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 lemmas nat_mult_le_cancel_disj = mult_le_cancel1
    89 
    90 lemmas nat_mult_less_cancel_disj = mult_less_cancel1
    91 
    92 lemma nat_mult_eq_cancel_disj:
    93   fixes k m n :: nat
    94   shows "k * m = k * n \<longleftrightarrow> k = 0 \<or> m = n"
    95   by auto
    96 
    97 lemma nat_mult_div_cancel_disj [simp]:
    98   fixes k m n :: nat
    99   shows "(k * m) div (k * n) = (if k = 0 then 0 else m div n)"
   100   by (fact div_mult_mult1_if)
   101 
   102 ML_file "Tools/numeral_simprocs.ML"
   103 
   104 simproc_setup semiring_assoc_fold
   105   ("(a::'a::comm_semiring_1_cancel) * b") =
   106   {* fn phi => Numeral_Simprocs.assoc_fold *}
   107 
   108 (* TODO: see whether the type class can be generalized further *)
   109 simproc_setup int_combine_numerals
   110   ("(i::'a::comm_ring_1) + j" | "(i::'a::comm_ring_1) - j") =
   111   {* fn phi => Numeral_Simprocs.combine_numerals *}
   112 
   113 simproc_setup field_combine_numerals
   114   ("(i::'a::{field_inverse_zero,ring_char_0}) + j"
   115   |"(i::'a::{field_inverse_zero,ring_char_0}) - j") =
   116   {* fn phi => Numeral_Simprocs.field_combine_numerals *}
   117 
   118 simproc_setup inteq_cancel_numerals
   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 * n"
   125   |"- (l::'a::comm_ring_1) = m"
   126   |"(l::'a::comm_ring_1) = - m") =
   127   {* fn phi => Numeral_Simprocs.eq_cancel_numerals *}
   128 
   129 simproc_setup intless_cancel_numerals
   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 * n"
   136   |"- (l::'a::linordered_idom) < m"
   137   |"(l::'a::linordered_idom) < - m") =
   138   {* fn phi => Numeral_Simprocs.less_cancel_numerals *}
   139 
   140 simproc_setup intle_cancel_numerals
   141   ("(l::'a::linordered_idom) + m \<le> n"
   142   |"(l::'a::linordered_idom) \<le> m + n"
   143   |"(l::'a::linordered_idom) - m \<le> n"
   144   |"(l::'a::linordered_idom) \<le> m - n"
   145   |"(l::'a::linordered_idom) * m \<le> n"
   146   |"(l::'a::linordered_idom) \<le> m * n"
   147   |"- (l::'a::linordered_idom) \<le> m"
   148   |"(l::'a::linordered_idom) \<le> - m") =
   149   {* fn phi => Numeral_Simprocs.le_cancel_numerals *}
   150 
   151 simproc_setup ring_eq_cancel_numeral_factor
   152   ("(l::'a::{idom,ring_char_0}) * m = n"
   153   |"(l::'a::{idom,ring_char_0}) = m * n") =
   154   {* fn phi => Numeral_Simprocs.eq_cancel_numeral_factor *}
   155 
   156 simproc_setup ring_less_cancel_numeral_factor
   157   ("(l::'a::linordered_idom) * m < n"
   158   |"(l::'a::linordered_idom) < m * n") =
   159   {* fn phi => Numeral_Simprocs.less_cancel_numeral_factor *}
   160 
   161 simproc_setup ring_le_cancel_numeral_factor
   162   ("(l::'a::linordered_idom) * m <= n"
   163   |"(l::'a::linordered_idom) <= m * n") =
   164   {* fn phi => Numeral_Simprocs.le_cancel_numeral_factor *}
   165 
   166 (* TODO: remove comm_ring_1 constraint if possible *)
   167 simproc_setup int_div_cancel_numeral_factors
   168   ("((l::'a::{semiring_div,comm_ring_1,ring_char_0}) * m) div n"
   169   |"(l::'a::{semiring_div,comm_ring_1,ring_char_0}) div (m * n)") =
   170   {* fn phi => Numeral_Simprocs.div_cancel_numeral_factor *}
   171 
   172 simproc_setup divide_cancel_numeral_factor
   173   ("((l::'a::{field_inverse_zero,ring_char_0}) * m) / n"
   174   |"(l::'a::{field_inverse_zero,ring_char_0}) / (m * n)"
   175   |"((numeral v)::'a::{field_inverse_zero,ring_char_0}) / (numeral w)") =
   176   {* fn phi => Numeral_Simprocs.divide_cancel_numeral_factor *}
   177 
   178 simproc_setup ring_eq_cancel_factor
   179   ("(l::'a::idom) * m = n" | "(l::'a::idom) = m * n") =
   180   {* fn phi => Numeral_Simprocs.eq_cancel_factor *}
   181 
   182 simproc_setup linordered_ring_le_cancel_factor
   183   ("(l::'a::linordered_idom) * m <= n"
   184   |"(l::'a::linordered_idom) <= m * n") =
   185   {* fn phi => Numeral_Simprocs.le_cancel_factor *}
   186 
   187 simproc_setup linordered_ring_less_cancel_factor
   188   ("(l::'a::linordered_idom) * m < n"
   189   |"(l::'a::linordered_idom) < m * n") =
   190   {* fn phi => Numeral_Simprocs.less_cancel_factor *}
   191 
   192 simproc_setup int_div_cancel_factor
   193   ("((l::'a::semiring_div) * m) div n"
   194   |"(l::'a::semiring_div) div (m * n)") =
   195   {* fn phi => Numeral_Simprocs.div_cancel_factor *}
   196 
   197 simproc_setup int_mod_cancel_factor
   198   ("((l::'a::semiring_div) * m) mod n"
   199   |"(l::'a::semiring_div) mod (m * n)") =
   200   {* fn phi => Numeral_Simprocs.mod_cancel_factor *}
   201 
   202 simproc_setup dvd_cancel_factor
   203   ("((l::'a::idom) * m) dvd n"
   204   |"(l::'a::idom) dvd (m * n)") =
   205   {* fn phi => Numeral_Simprocs.dvd_cancel_factor *}
   206 
   207 simproc_setup divide_cancel_factor
   208   ("((l::'a::field_inverse_zero) * m) / n"
   209   |"(l::'a::field_inverse_zero) / (m * n)") =
   210   {* fn phi => Numeral_Simprocs.divide_cancel_factor *}
   211 
   212 ML_file "Tools/nat_numeral_simprocs.ML"
   213 
   214 simproc_setup nat_combine_numerals
   215   ("(i::nat) + j" | "Suc (i + j)") =
   216   {* fn phi => Nat_Numeral_Simprocs.combine_numerals *}
   217 
   218 simproc_setup nateq_cancel_numerals
   219   ("(l::nat) + m = n" | "(l::nat) = m + n" |
   220    "(l::nat) * m = n" | "(l::nat) = m * n" |
   221    "Suc m = n" | "m = Suc n") =
   222   {* fn phi => Nat_Numeral_Simprocs.eq_cancel_numerals *}
   223 
   224 simproc_setup natless_cancel_numerals
   225   ("(l::nat) + m < n" | "(l::nat) < m + n" |
   226    "(l::nat) * m < n" | "(l::nat) < m * n" |
   227    "Suc m < n" | "m < Suc n") =
   228   {* fn phi => Nat_Numeral_Simprocs.less_cancel_numerals *}
   229 
   230 simproc_setup natle_cancel_numerals
   231   ("(l::nat) + m \<le> n" | "(l::nat) \<le> m + n" |
   232    "(l::nat) * m \<le> n" | "(l::nat) \<le> m * n" |
   233    "Suc m \<le> n" | "m \<le> Suc n") =
   234   {* fn phi => Nat_Numeral_Simprocs.le_cancel_numerals *}
   235 
   236 simproc_setup natdiff_cancel_numerals
   237   ("((l::nat) + m) - n" | "(l::nat) - (m + n)" |
   238    "(l::nat) * m - n" | "(l::nat) - m * n" |
   239    "Suc m - n" | "m - Suc n") =
   240   {* fn phi => Nat_Numeral_Simprocs.diff_cancel_numerals *}
   241 
   242 simproc_setup nat_eq_cancel_numeral_factor
   243   ("(l::nat) * m = n" | "(l::nat) = m * n") =
   244   {* fn phi => Nat_Numeral_Simprocs.eq_cancel_numeral_factor *}
   245 
   246 simproc_setup nat_less_cancel_numeral_factor
   247   ("(l::nat) * m < n" | "(l::nat) < m * n") =
   248   {* fn phi => Nat_Numeral_Simprocs.less_cancel_numeral_factor *}
   249 
   250 simproc_setup nat_le_cancel_numeral_factor
   251   ("(l::nat) * m <= n" | "(l::nat) <= m * n") =
   252   {* fn phi => Nat_Numeral_Simprocs.le_cancel_numeral_factor *}
   253 
   254 simproc_setup nat_div_cancel_numeral_factor
   255   ("((l::nat) * m) div n" | "(l::nat) div (m * n)") =
   256   {* fn phi => Nat_Numeral_Simprocs.div_cancel_numeral_factor *}
   257 
   258 simproc_setup nat_dvd_cancel_numeral_factor
   259   ("((l::nat) * m) dvd n" | "(l::nat) dvd (m * n)") =
   260   {* fn phi => Nat_Numeral_Simprocs.dvd_cancel_numeral_factor *}
   261 
   262 simproc_setup nat_eq_cancel_factor
   263   ("(l::nat) * m = n" | "(l::nat) = m * n") =
   264   {* fn phi => Nat_Numeral_Simprocs.eq_cancel_factor *}
   265 
   266 simproc_setup nat_less_cancel_factor
   267   ("(l::nat) * m < n" | "(l::nat) < m * n") =
   268   {* fn phi => Nat_Numeral_Simprocs.less_cancel_factor *}
   269 
   270 simproc_setup nat_le_cancel_factor
   271   ("(l::nat) * m <= n" | "(l::nat) <= m * n") =
   272   {* fn phi => Nat_Numeral_Simprocs.le_cancel_factor *}
   273 
   274 simproc_setup nat_div_cancel_factor
   275   ("((l::nat) * m) div n" | "(l::nat) div (m * n)") =
   276   {* fn phi => Nat_Numeral_Simprocs.div_cancel_factor *}
   277 
   278 simproc_setup nat_dvd_cancel_factor
   279   ("((l::nat) * m) dvd n" | "(l::nat) dvd (m * n)") =
   280   {* fn phi => Nat_Numeral_Simprocs.dvd_cancel_factor *}
   281 
   282 declaration {* 
   283   K (Lin_Arith.add_simprocs
   284       [@{simproc semiring_assoc_fold},
   285        @{simproc int_combine_numerals},
   286        @{simproc inteq_cancel_numerals},
   287        @{simproc intless_cancel_numerals},
   288        @{simproc intle_cancel_numerals},
   289        @{simproc field_combine_numerals}]
   290   #> Lin_Arith.add_simprocs
   291       [@{simproc nat_combine_numerals},
   292        @{simproc nateq_cancel_numerals},
   293        @{simproc natless_cancel_numerals},
   294        @{simproc natle_cancel_numerals},
   295        @{simproc natdiff_cancel_numerals}])
   296 *}
   297 
   298 end