src/HOL/Numeral_Simprocs.thy
author paulson <lp15@cam.ac.uk>
Fri Mar 20 14:00:22 2015 +0000 (2015-03-20)
changeset 59757 93d4169e07dc
parent 58889 5b7a9633cfa8
child 59867 58043346ca64
permissions -rw-r--r--
fixed crash in cancel_numeral_simprocs. NB they still don't work except for type nat!
     1 (* Author: Various *)
     2 
     3 section {* 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 lemma numeral_times_minus_swap:
   103   fixes x:: "'a::comm_ring_1" shows  "numeral w * -x = x * - numeral w"
   104   by (simp add: mult.commute)
   105 
   106 ML_file "Tools/numeral_simprocs.ML"
   107 
   108 simproc_setup semiring_assoc_fold
   109   ("(a::'a::comm_semiring_1_cancel) * b") =
   110   {* fn phi => Numeral_Simprocs.assoc_fold *}
   111 
   112 (* TODO: see whether the type class can be generalized further *)
   113 simproc_setup int_combine_numerals
   114   ("(i::'a::comm_ring_1) + j" | "(i::'a::comm_ring_1) - j") =
   115   {* fn phi => Numeral_Simprocs.combine_numerals *}
   116 
   117 simproc_setup field_combine_numerals
   118   ("(i::'a::{field_inverse_zero,ring_char_0}) + j"
   119   |"(i::'a::{field_inverse_zero,ring_char_0}) - j") =
   120   {* fn phi => Numeral_Simprocs.field_combine_numerals *}
   121 
   122 simproc_setup inteq_cancel_numerals
   123   ("(l::'a::comm_ring_1) + m = n"
   124   |"(l::'a::comm_ring_1) = m + n"
   125   |"(l::'a::comm_ring_1) - m = n"
   126   |"(l::'a::comm_ring_1) = m - n"
   127   |"(l::'a::comm_ring_1) * m = n"
   128   |"(l::'a::comm_ring_1) = m * n"
   129   |"- (l::'a::comm_ring_1) = m"
   130   |"(l::'a::comm_ring_1) = - m") =
   131   {* fn phi => Numeral_Simprocs.eq_cancel_numerals *}
   132 
   133 simproc_setup intless_cancel_numerals
   134   ("(l::'a::linordered_idom) + m < n"
   135   |"(l::'a::linordered_idom) < m + n"
   136   |"(l::'a::linordered_idom) - m < n"
   137   |"(l::'a::linordered_idom) < m - n"
   138   |"(l::'a::linordered_idom) * m < n"
   139   |"(l::'a::linordered_idom) < m * n"
   140   |"- (l::'a::linordered_idom) < m"
   141   |"(l::'a::linordered_idom) < - m") =
   142   {* fn phi => Numeral_Simprocs.less_cancel_numerals *}
   143 
   144 simproc_setup intle_cancel_numerals
   145   ("(l::'a::linordered_idom) + m \<le> n"
   146   |"(l::'a::linordered_idom) \<le> m + n"
   147   |"(l::'a::linordered_idom) - m \<le> n"
   148   |"(l::'a::linordered_idom) \<le> m - n"
   149   |"(l::'a::linordered_idom) * m \<le> n"
   150   |"(l::'a::linordered_idom) \<le> m * n"
   151   |"- (l::'a::linordered_idom) \<le> m"
   152   |"(l::'a::linordered_idom) \<le> - m") =
   153   {* fn phi => Numeral_Simprocs.le_cancel_numerals *}
   154 
   155 simproc_setup ring_eq_cancel_numeral_factor
   156   ("(l::'a::{idom,ring_char_0}) * m = n"
   157   |"(l::'a::{idom,ring_char_0}) = m * n") =
   158   {* fn phi => Numeral_Simprocs.eq_cancel_numeral_factor *}
   159 
   160 simproc_setup ring_less_cancel_numeral_factor
   161   ("(l::'a::linordered_idom) * m < n"
   162   |"(l::'a::linordered_idom) < m * n") =
   163   {* fn phi => Numeral_Simprocs.less_cancel_numeral_factor *}
   164 
   165 simproc_setup ring_le_cancel_numeral_factor
   166   ("(l::'a::linordered_idom) * m <= n"
   167   |"(l::'a::linordered_idom) <= m * n") =
   168   {* fn phi => Numeral_Simprocs.le_cancel_numeral_factor *}
   169 
   170 (* TODO: remove comm_ring_1 constraint if possible *)
   171 simproc_setup int_div_cancel_numeral_factors
   172   ("((l::'a::{semiring_div,comm_ring_1,ring_char_0}) * m) div n"
   173   |"(l::'a::{semiring_div,comm_ring_1,ring_char_0}) div (m * n)") =
   174   {* fn phi => Numeral_Simprocs.div_cancel_numeral_factor *}
   175 
   176 simproc_setup divide_cancel_numeral_factor
   177   ("((l::'a::{field_inverse_zero,ring_char_0}) * m) / n"
   178   |"(l::'a::{field_inverse_zero,ring_char_0}) / (m * n)"
   179   |"((numeral v)::'a::{field_inverse_zero,ring_char_0}) / (numeral w)") =
   180   {* fn phi => Numeral_Simprocs.divide_cancel_numeral_factor *}
   181 
   182 simproc_setup ring_eq_cancel_factor
   183   ("(l::'a::idom) * m = n" | "(l::'a::idom) = m * n") =
   184   {* fn phi => Numeral_Simprocs.eq_cancel_factor *}
   185 
   186 simproc_setup linordered_ring_le_cancel_factor
   187   ("(l::'a::linordered_idom) * m <= n"
   188   |"(l::'a::linordered_idom) <= m * n") =
   189   {* fn phi => Numeral_Simprocs.le_cancel_factor *}
   190 
   191 simproc_setup linordered_ring_less_cancel_factor
   192   ("(l::'a::linordered_idom) * m < n"
   193   |"(l::'a::linordered_idom) < m * n") =
   194   {* fn phi => Numeral_Simprocs.less_cancel_factor *}
   195 
   196 simproc_setup int_div_cancel_factor
   197   ("((l::'a::semiring_div) * m) div n"
   198   |"(l::'a::semiring_div) div (m * n)") =
   199   {* fn phi => Numeral_Simprocs.div_cancel_factor *}
   200 
   201 simproc_setup int_mod_cancel_factor
   202   ("((l::'a::semiring_div) * m) mod n"
   203   |"(l::'a::semiring_div) mod (m * n)") =
   204   {* fn phi => Numeral_Simprocs.mod_cancel_factor *}
   205 
   206 simproc_setup dvd_cancel_factor
   207   ("((l::'a::idom) * m) dvd n"
   208   |"(l::'a::idom) dvd (m * n)") =
   209   {* fn phi => Numeral_Simprocs.dvd_cancel_factor *}
   210 
   211 simproc_setup divide_cancel_factor
   212   ("((l::'a::field_inverse_zero) * m) / n"
   213   |"(l::'a::field_inverse_zero) / (m * n)") =
   214   {* fn phi => Numeral_Simprocs.divide_cancel_factor *}
   215 
   216 ML_file "Tools/nat_numeral_simprocs.ML"
   217 
   218 simproc_setup nat_combine_numerals
   219   ("(i::nat) + j" | "Suc (i + j)") =
   220   {* fn phi => Nat_Numeral_Simprocs.combine_numerals *}
   221 
   222 simproc_setup nateq_cancel_numerals
   223   ("(l::nat) + m = n" | "(l::nat) = m + n" |
   224    "(l::nat) * m = n" | "(l::nat) = m * n" |
   225    "Suc m = n" | "m = Suc n") =
   226   {* fn phi => Nat_Numeral_Simprocs.eq_cancel_numerals *}
   227 
   228 simproc_setup natless_cancel_numerals
   229   ("(l::nat) + m < n" | "(l::nat) < m + n" |
   230    "(l::nat) * m < n" | "(l::nat) < m * n" |
   231    "Suc m < n" | "m < Suc n") =
   232   {* fn phi => Nat_Numeral_Simprocs.less_cancel_numerals *}
   233 
   234 simproc_setup natle_cancel_numerals
   235   ("(l::nat) + m \<le> n" | "(l::nat) \<le> m + n" |
   236    "(l::nat) * m \<le> n" | "(l::nat) \<le> m * n" |
   237    "Suc m \<le> n" | "m \<le> Suc n") =
   238   {* fn phi => Nat_Numeral_Simprocs.le_cancel_numerals *}
   239 
   240 simproc_setup natdiff_cancel_numerals
   241   ("((l::nat) + m) - n" | "(l::nat) - (m + n)" |
   242    "(l::nat) * m - n" | "(l::nat) - m * n" |
   243    "Suc m - n" | "m - Suc n") =
   244   {* fn phi => Nat_Numeral_Simprocs.diff_cancel_numerals *}
   245 
   246 simproc_setup nat_eq_cancel_numeral_factor
   247   ("(l::nat) * m = n" | "(l::nat) = m * n") =
   248   {* fn phi => Nat_Numeral_Simprocs.eq_cancel_numeral_factor *}
   249 
   250 simproc_setup nat_less_cancel_numeral_factor
   251   ("(l::nat) * m < n" | "(l::nat) < m * n") =
   252   {* fn phi => Nat_Numeral_Simprocs.less_cancel_numeral_factor *}
   253 
   254 simproc_setup nat_le_cancel_numeral_factor
   255   ("(l::nat) * m <= n" | "(l::nat) <= m * n") =
   256   {* fn phi => Nat_Numeral_Simprocs.le_cancel_numeral_factor *}
   257 
   258 simproc_setup nat_div_cancel_numeral_factor
   259   ("((l::nat) * m) div n" | "(l::nat) div (m * n)") =
   260   {* fn phi => Nat_Numeral_Simprocs.div_cancel_numeral_factor *}
   261 
   262 simproc_setup nat_dvd_cancel_numeral_factor
   263   ("((l::nat) * m) dvd n" | "(l::nat) dvd (m * n)") =
   264   {* fn phi => Nat_Numeral_Simprocs.dvd_cancel_numeral_factor *}
   265 
   266 simproc_setup nat_eq_cancel_factor
   267   ("(l::nat) * m = n" | "(l::nat) = m * n") =
   268   {* fn phi => Nat_Numeral_Simprocs.eq_cancel_factor *}
   269 
   270 simproc_setup nat_less_cancel_factor
   271   ("(l::nat) * m < n" | "(l::nat) < m * n") =
   272   {* fn phi => Nat_Numeral_Simprocs.less_cancel_factor *}
   273 
   274 simproc_setup nat_le_cancel_factor
   275   ("(l::nat) * m <= n" | "(l::nat) <= m * n") =
   276   {* fn phi => Nat_Numeral_Simprocs.le_cancel_factor *}
   277 
   278 simproc_setup nat_div_cancel_factor
   279   ("((l::nat) * m) div n" | "(l::nat) div (m * n)") =
   280   {* fn phi => Nat_Numeral_Simprocs.div_cancel_factor *}
   281 
   282 simproc_setup nat_dvd_cancel_factor
   283   ("((l::nat) * m) dvd n" | "(l::nat) dvd (m * n)") =
   284   {* fn phi => Nat_Numeral_Simprocs.dvd_cancel_factor *}
   285 
   286 declaration {* 
   287   K (Lin_Arith.add_simprocs
   288       [@{simproc semiring_assoc_fold},
   289        @{simproc int_combine_numerals},
   290        @{simproc inteq_cancel_numerals},
   291        @{simproc intless_cancel_numerals},
   292        @{simproc intle_cancel_numerals},
   293        @{simproc field_combine_numerals}]
   294   #> Lin_Arith.add_simprocs
   295       [@{simproc nat_combine_numerals},
   296        @{simproc nateq_cancel_numerals},
   297        @{simproc natless_cancel_numerals},
   298        @{simproc natle_cancel_numerals},
   299        @{simproc natdiff_cancel_numerals}])
   300 *}
   301 
   302 end