
haftmann@33366: (* Author: Various *)
haftmann@33366: 
haftmann@33366: header {* Combination and Cancellation Simprocs for Numeral Expressions *}
haftmann@33366: 
haftmann@33366: theory Numeral_Simprocs
haftmann@33366: imports Divides
haftmann@33366: begin
haftmann@33366: 
wenzelm@48891: ML_file "~~/src/Provers/Arith/assoc_fold.ML"
wenzelm@48891: ML_file "~~/src/Provers/Arith/cancel_numerals.ML"
wenzelm@48891: ML_file "~~/src/Provers/Arith/combine_numerals.ML"
wenzelm@48891: ML_file "~~/src/Provers/Arith/cancel_numeral_factor.ML"
wenzelm@48891: ML_file "~~/src/Provers/Arith/extract_common_term.ML"
wenzelm@48891: 
huffman@47255: lemmas semiring_norm =
huffman@47255:   Let_def arith_simps nat_arith rel_simps
huffman@47255:   if_False if_True
huffman@47255:   add_0 add_Suc add_numeral_left
huffman@47255:   add_neg_numeral_left mult_numeral_left
huffman@47255:   numeral_1_eq_1 [symmetric] Suc_eq_plus1
huffman@47255:   eq_numeral_iff_iszero not_iszero_Numeral1
huffman@47255: 
huffman@47108: declare split_div [of _ _ "numeral k", arith_split] for k
huffman@47108: declare split_mod [of _ _ "numeral k", arith_split] for k
haftmann@33366: 
haftmann@33366: text {* For @{text combine_numerals} *}
haftmann@33366: 
haftmann@33366: lemma left_add_mult_distrib: "i*u + (j*u + k) = (i+j)*u + (k::nat)"
haftmann@33366: by (simp add: add_mult_distrib)
haftmann@33366: 
haftmann@33366: text {* For @{text cancel_numerals} *}
haftmann@33366: 
haftmann@33366: lemma nat_diff_add_eq1:
haftmann@33366:      "j <= (i::nat) ==> ((i*u + m) - (j*u + n)) = (((i-j)*u + m) - n)"
haftmann@33366: by (simp split add: nat_diff_split add: add_mult_distrib)
haftmann@33366: 
haftmann@33366: lemma nat_diff_add_eq2:
haftmann@33366:      "i <= (j::nat) ==> ((i*u + m) - (j*u + n)) = (m - ((j-i)*u + n))"
haftmann@33366: by (simp split add: nat_diff_split add: add_mult_distrib)
haftmann@33366: 
haftmann@33366: lemma nat_eq_add_iff1:
haftmann@33366:      "j <= (i::nat) ==> (i*u + m = j*u + n) = ((i-j)*u + m = n)"
haftmann@33366: by (auto split add: nat_diff_split simp add: add_mult_distrib)
haftmann@33366: 
haftmann@33366: lemma nat_eq_add_iff2:
haftmann@33366:      "i <= (j::nat) ==> (i*u + m = j*u + n) = (m = (j-i)*u + n)"
haftmann@33366: by (auto split add: nat_diff_split simp add: add_mult_distrib)
haftmann@33366: 
haftmann@33366: lemma nat_less_add_iff1:
haftmann@33366:      "j <= (i::nat) ==> (i*u + m < j*u + n) = ((i-j)*u + m < n)"
haftmann@33366: by (auto split add: nat_diff_split simp add: add_mult_distrib)
haftmann@33366: 
haftmann@33366: lemma nat_less_add_iff2:
haftmann@33366:      "i <= (j::nat) ==> (i*u + m < j*u + n) = (m < (j-i)*u + n)"
haftmann@33366: by (auto split add: nat_diff_split simp add: add_mult_distrib)
haftmann@33366: 
haftmann@33366: lemma nat_le_add_iff1:
haftmann@33366:      "j <= (i::nat) ==> (i*u + m <= j*u + n) = ((i-j)*u + m <= n)"
haftmann@33366: by (auto split add: nat_diff_split simp add: add_mult_distrib)
haftmann@33366: 
haftmann@33366: lemma nat_le_add_iff2:
haftmann@33366:      "i <= (j::nat) ==> (i*u + m <= j*u + n) = (m <= (j-i)*u + n)"
haftmann@33366: by (auto split add: nat_diff_split simp add: add_mult_distrib)
haftmann@33366: 
haftmann@33366: text {* For @{text cancel_numeral_factors} *}
haftmann@33366: 
haftmann@33366: lemma nat_mult_le_cancel1: "(0::nat) < k ==> (k*m <= k*n) = (m<=n)"
haftmann@33366: by auto
haftmann@33366: 
haftmann@33366: lemma nat_mult_less_cancel1: "(0::nat) < k ==> (k*m < k*n) = (m<n)"
haftmann@33366: by auto
haftmann@33366: 
haftmann@33366: lemma nat_mult_eq_cancel1: "(0::nat) < k ==> (k*m = k*n) = (m=n)"
haftmann@33366: by auto
haftmann@33366: 
haftmann@33366: lemma nat_mult_div_cancel1: "(0::nat) < k ==> (k*m) div (k*n) = (m div n)"
haftmann@33366: by auto
haftmann@33366: 
haftmann@33366: lemma nat_mult_dvd_cancel_disj[simp]:
haftmann@33366:   "(k*m) dvd (k*n) = (k=0 | m dvd (n::nat))"
huffman@47159: by (auto simp: dvd_eq_mod_eq_0 mod_mult_mult1)
haftmann@33366: 
haftmann@33366: lemma nat_mult_dvd_cancel1: "0 < k \<Longrightarrow> (k*m) dvd (k*n::nat) = (m dvd n)"
haftmann@33366: by(auto)
haftmann@33366: 
haftmann@33366: text {* For @{text cancel_factor} *}
haftmann@33366: 
haftmann@33366: lemma nat_mult_le_cancel_disj: "(k*m <= k*n) = ((0::nat) < k --> m<=n)"
haftmann@33366: by auto
haftmann@33366: 
haftmann@33366: lemma nat_mult_less_cancel_disj: "(k*m < k*n) = ((0::nat) < k & m<n)"
haftmann@33366: by auto
haftmann@33366: 
haftmann@33366: lemma nat_mult_eq_cancel_disj: "(k*m = k*n) = (k = (0::nat) | m=n)"
haftmann@33366: by auto
haftmann@33366: 
haftmann@33366: lemma nat_mult_div_cancel_disj[simp]:
haftmann@33366:      "(k*m) div (k*n) = (if k = (0::nat) then 0 else m div n)"
haftmann@33366: by (simp add: nat_mult_div_cancel1)
haftmann@33366: 
wenzelm@48891: ML_file "Tools/numeral_simprocs.ML"
haftmann@33366: 
huffman@45284: simproc_setup semiring_assoc_fold
huffman@45284:   ("(a::'a::comm_semiring_1_cancel) * b") =
huffman@45284:   {* fn phi => Numeral_Simprocs.assoc_fold *}
huffman@45284: 
huffman@47108: (* TODO: see whether the type class can be generalized further *)
huffman@45284: simproc_setup int_combine_numerals
huffman@47108:   ("(i::'a::comm_ring_1) + j" | "(i::'a::comm_ring_1) - j") =
huffman@45284:   {* fn phi => Numeral_Simprocs.combine_numerals *}
huffman@45284: 
huffman@45284: simproc_setup field_combine_numerals
huffman@47108:   ("(i::'a::{field_inverse_zero,ring_char_0}) + j"
huffman@47108:   |"(i::'a::{field_inverse_zero,ring_char_0}) - j") =
huffman@45284:   {* fn phi => Numeral_Simprocs.field_combine_numerals *}
huffman@45284: 
huffman@45284: simproc_setup inteq_cancel_numerals
huffman@47108:   ("(l::'a::comm_ring_1) + m = n"
huffman@47108:   |"(l::'a::comm_ring_1) = m + n"
huffman@47108:   |"(l::'a::comm_ring_1) - m = n"
huffman@47108:   |"(l::'a::comm_ring_1) = m - n"
huffman@47108:   |"(l::'a::comm_ring_1) * m = n"
huffman@47108:   |"(l::'a::comm_ring_1) = m * n"
huffman@47108:   |"- (l::'a::comm_ring_1) = m"
huffman@47108:   |"(l::'a::comm_ring_1) = - m") =
huffman@45284:   {* fn phi => Numeral_Simprocs.eq_cancel_numerals *}
huffman@45284: 
huffman@45284: simproc_setup intless_cancel_numerals
huffman@47108:   ("(l::'a::linordered_idom) + m < n"
huffman@47108:   |"(l::'a::linordered_idom) < m + n"
huffman@47108:   |"(l::'a::linordered_idom) - m < n"
huffman@47108:   |"(l::'a::linordered_idom) < m - n"
huffman@47108:   |"(l::'a::linordered_idom) * m < n"
huffman@47108:   |"(l::'a::linordered_idom) < m * n"
huffman@47108:   |"- (l::'a::linordered_idom) < m"
huffman@47108:   |"(l::'a::linordered_idom) < - m") =
huffman@45284:   {* fn phi => Numeral_Simprocs.less_cancel_numerals *}
huffman@45284: 
huffman@45284: simproc_setup intle_cancel_numerals
huffman@47108:   ("(l::'a::linordered_idom) + m \<le> n"
huffman@47108:   |"(l::'a::linordered_idom) \<le> m + n"
huffman@47108:   |"(l::'a::linordered_idom) - m \<le> n"
huffman@47108:   |"(l::'a::linordered_idom) \<le> m - n"
huffman@47108:   |"(l::'a::linordered_idom) * m \<le> n"
huffman@47108:   |"(l::'a::linordered_idom) \<le> m * n"
huffman@47108:   |"- (l::'a::linordered_idom) \<le> m"
huffman@47108:   |"(l::'a::linordered_idom) \<le> - m") =
huffman@45284:   {* fn phi => Numeral_Simprocs.le_cancel_numerals *}
huffman@45284: 
huffman@45284: simproc_setup ring_eq_cancel_numeral_factor
huffman@47108:   ("(l::'a::{idom,ring_char_0}) * m = n"
huffman@47108:   |"(l::'a::{idom,ring_char_0}) = m * n") =
huffman@45284:   {* fn phi => Numeral_Simprocs.eq_cancel_numeral_factor *}
huffman@45284: 
huffman@45284: simproc_setup ring_less_cancel_numeral_factor
huffman@47108:   ("(l::'a::linordered_idom) * m < n"
huffman@47108:   |"(l::'a::linordered_idom) < m * n") =
huffman@45284:   {* fn phi => Numeral_Simprocs.less_cancel_numeral_factor *}
huffman@45284: 
huffman@45284: simproc_setup ring_le_cancel_numeral_factor
huffman@47108:   ("(l::'a::linordered_idom) * m <= n"
huffman@47108:   |"(l::'a::linordered_idom) <= m * n") =
huffman@45284:   {* fn phi => Numeral_Simprocs.le_cancel_numeral_factor *}
huffman@45284: 
huffman@47108: (* TODO: remove comm_ring_1 constraint if possible *)
huffman@45284: simproc_setup int_div_cancel_numeral_factors
huffman@47108:   ("((l::'a::{semiring_div,comm_ring_1,ring_char_0}) * m) div n"
huffman@47108:   |"(l::'a::{semiring_div,comm_ring_1,ring_char_0}) div (m * n)") =
huffman@45284:   {* fn phi => Numeral_Simprocs.div_cancel_numeral_factor *}
huffman@45284: 
huffman@45284: simproc_setup divide_cancel_numeral_factor
huffman@47108:   ("((l::'a::{field_inverse_zero,ring_char_0}) * m) / n"
huffman@47108:   |"(l::'a::{field_inverse_zero,ring_char_0}) / (m * n)"
huffman@47108:   |"((numeral v)::'a::{field_inverse_zero,ring_char_0}) / (numeral w)") =
huffman@45284:   {* fn phi => Numeral_Simprocs.divide_cancel_numeral_factor *}
huffman@45284: 
huffman@45284: simproc_setup ring_eq_cancel_factor
huffman@45284:   ("(l::'a::idom) * m = n" | "(l::'a::idom) = m * n") =
huffman@45284:   {* fn phi => Numeral_Simprocs.eq_cancel_factor *}
huffman@45284: 
huffman@45284: simproc_setup linordered_ring_le_cancel_factor
huffman@45296:   ("(l::'a::linordered_idom) * m <= n"
huffman@45296:   |"(l::'a::linordered_idom) <= m * n") =
huffman@45284:   {* fn phi => Numeral_Simprocs.le_cancel_factor *}
huffman@45284: 
huffman@45284: simproc_setup linordered_ring_less_cancel_factor
huffman@45296:   ("(l::'a::linordered_idom) * m < n"
huffman@45296:   |"(l::'a::linordered_idom) < m * n") =
huffman@45284:   {* fn phi => Numeral_Simprocs.less_cancel_factor *}
huffman@45284: 
huffman@45284: simproc_setup int_div_cancel_factor
huffman@45284:   ("((l::'a::semiring_div) * m) div n"
huffman@45284:   |"(l::'a::semiring_div) div (m * n)") =
huffman@45284:   {* fn phi => Numeral_Simprocs.div_cancel_factor *}
huffman@45284: 
huffman@45284: simproc_setup int_mod_cancel_factor
huffman@45284:   ("((l::'a::semiring_div) * m) mod n"
huffman@45284:   |"(l::'a::semiring_div) mod (m * n)") =
huffman@45284:   {* fn phi => Numeral_Simprocs.mod_cancel_factor *}
huffman@45284: 
huffman@45284: simproc_setup dvd_cancel_factor
huffman@45284:   ("((l::'a::idom) * m) dvd n"
huffman@45284:   |"(l::'a::idom) dvd (m * n)") =
huffman@45284:   {* fn phi => Numeral_Simprocs.dvd_cancel_factor *}
huffman@45284: 
huffman@45284: simproc_setup divide_cancel_factor
huffman@45284:   ("((l::'a::field_inverse_zero) * m) / n"
huffman@45284:   |"(l::'a::field_inverse_zero) / (m * n)") =
huffman@45284:   {* fn phi => Numeral_Simprocs.divide_cancel_factor *}
huffman@45284: 
wenzelm@48891: ML_file "Tools/nat_numeral_simprocs.ML"
haftmann@33366: 
huffman@45462: simproc_setup nat_combine_numerals
huffman@45462:   ("(i::nat) + j" | "Suc (i + j)") =
huffman@45462:   {* fn phi => Nat_Numeral_Simprocs.combine_numerals *}
huffman@45462: 
huffman@45436: simproc_setup nateq_cancel_numerals
huffman@45436:   ("(l::nat) + m = n" | "(l::nat) = m + n" |
huffman@45436:    "(l::nat) * m = n" | "(l::nat) = m * n" |
huffman@45436:    "Suc m = n" | "m = Suc n") =
huffman@45436:   {* fn phi => Nat_Numeral_Simprocs.eq_cancel_numerals *}
huffman@45436: 
huffman@45436: simproc_setup natless_cancel_numerals
huffman@45436:   ("(l::nat) + m < n" | "(l::nat) < m + n" |
huffman@45436:    "(l::nat) * m < n" | "(l::nat) < m * n" |
huffman@45436:    "Suc m < n" | "m < Suc n") =
huffman@45436:   {* fn phi => Nat_Numeral_Simprocs.less_cancel_numerals *}
huffman@45436: 
huffman@45436: simproc_setup natle_cancel_numerals
huffman@45436:   ("(l::nat) + m \<le> n" | "(l::nat) \<le> m + n" |
huffman@45436:    "(l::nat) * m \<le> n" | "(l::nat) \<le> m * n" |
huffman@45436:    "Suc m \<le> n" | "m \<le> Suc n") =
huffman@45436:   {* fn phi => Nat_Numeral_Simprocs.le_cancel_numerals *}
huffman@45436: 
huffman@45436: simproc_setup natdiff_cancel_numerals
huffman@45436:   ("((l::nat) + m) - n" | "(l::nat) - (m + n)" |
huffman@45436:    "(l::nat) * m - n" | "(l::nat) - m * n" |
huffman@45436:    "Suc m - n" | "m - Suc n") =
huffman@45436:   {* fn phi => Nat_Numeral_Simprocs.diff_cancel_numerals *}
huffman@45436: 
huffman@45463: simproc_setup nat_eq_cancel_numeral_factor
huffman@45463:   ("(l::nat) * m = n" | "(l::nat) = m * n") =
huffman@45463:   {* fn phi => Nat_Numeral_Simprocs.eq_cancel_numeral_factor *}
huffman@45463: 
huffman@45463: simproc_setup nat_less_cancel_numeral_factor
huffman@45463:   ("(l::nat) * m < n" | "(l::nat) < m * n") =
huffman@45463:   {* fn phi => Nat_Numeral_Simprocs.less_cancel_numeral_factor *}
huffman@45463: 
huffman@45463: simproc_setup nat_le_cancel_numeral_factor
huffman@45463:   ("(l::nat) * m <= n" | "(l::nat) <= m * n") =
huffman@45463:   {* fn phi => Nat_Numeral_Simprocs.le_cancel_numeral_factor *}
huffman@45463: 
huffman@45463: simproc_setup nat_div_cancel_numeral_factor
huffman@45463:   ("((l::nat) * m) div n" | "(l::nat) div (m * n)") =
huffman@45463:   {* fn phi => Nat_Numeral_Simprocs.div_cancel_numeral_factor *}
huffman@45463: 
huffman@45463: simproc_setup nat_dvd_cancel_numeral_factor
huffman@45463:   ("((l::nat) * m) dvd n" | "(l::nat) dvd (m * n)") =
huffman@45463:   {* fn phi => Nat_Numeral_Simprocs.dvd_cancel_numeral_factor *}
huffman@45463: 
huffman@45462: simproc_setup nat_eq_cancel_factor
huffman@45462:   ("(l::nat) * m = n" | "(l::nat) = m * n") =
huffman@45462:   {* fn phi => Nat_Numeral_Simprocs.eq_cancel_factor *}
huffman@45462: 
huffman@45462: simproc_setup nat_less_cancel_factor
huffman@45462:   ("(l::nat) * m < n" | "(l::nat) < m * n") =
huffman@45462:   {* fn phi => Nat_Numeral_Simprocs.less_cancel_factor *}
huffman@45462: 
huffman@45462: simproc_setup nat_le_cancel_factor
huffman@45462:   ("(l::nat) * m <= n" | "(l::nat) <= m * n") =
huffman@45462:   {* fn phi => Nat_Numeral_Simprocs.le_cancel_factor *}
huffman@45462: 
huffman@45463: simproc_setup nat_div_cancel_factor
huffman@45462:   ("((l::nat) * m) div n" | "(l::nat) div (m * n)") =
huffman@45463:   {* fn phi => Nat_Numeral_Simprocs.div_cancel_factor *}
huffman@45462: 
huffman@45462: simproc_setup nat_dvd_cancel_factor
huffman@45462:   ("((l::nat) * m) dvd n" | "(l::nat) dvd (m * n)") =
huffman@45462:   {* fn phi => Nat_Numeral_Simprocs.dvd_cancel_factor *}
huffman@45462: 
huffman@47108: (* FIXME: duplicate rule warnings for:
huffman@47108:   ring_distribs
huffman@47108:   numeral_plus_numeral numeral_times_numeral
huffman@47108:   numeral_eq_iff numeral_less_iff numeral_le_iff
huffman@47108:   numeral_neq_zero zero_neq_numeral zero_less_numeral
huffman@47108:   if_True if_False *)
haftmann@33366: declaration {* 
huffman@47108:   K (Lin_Arith.add_simps ([@{thm Suc_numeral}, @{thm int_numeral}])
huffman@47108:   #> Lin_Arith.add_simps (@{thms ring_distribs} @ [@{thm Let_numeral}, @{thm Let_neg_numeral}, @{thm Let_0}, @{thm Let_1},
haftmann@33366:      @{thm nat_0}, @{thm nat_1},
huffman@47108:      @{thm numeral_plus_numeral}, @{thm diff_nat_numeral}, @{thm numeral_times_numeral},
huffman@47108:      @{thm numeral_eq_iff}, @{thm numeral_less_iff}, @{thm numeral_le_iff},
huffman@47108:      @{thm le_Suc_numeral}, @{thm le_numeral_Suc},
huffman@47108:      @{thm less_Suc_numeral}, @{thm less_numeral_Suc},
huffman@47108:      @{thm Suc_eq_numeral}, @{thm eq_numeral_Suc},
haftmann@33366:      @{thm mult_Suc}, @{thm mult_Suc_right},
haftmann@33366:      @{thm add_Suc}, @{thm add_Suc_right},
huffman@47108:      @{thm numeral_neq_zero}, @{thm zero_neq_numeral}, @{thm zero_less_numeral},
huffman@47108:      @{thm of_int_numeral}, @{thm of_nat_numeral}, @{thm nat_numeral},
haftmann@33366:      @{thm if_True}, @{thm if_False}])
huffman@45284:   #> Lin_Arith.add_simprocs
huffman@45284:       [@{simproc semiring_assoc_fold},
huffman@45284:        @{simproc int_combine_numerals},
huffman@45284:        @{simproc inteq_cancel_numerals},
huffman@45284:        @{simproc intless_cancel_numerals},
huffman@45284:        @{simproc intle_cancel_numerals}]
huffman@45436:   #> Lin_Arith.add_simprocs
huffman@45462:       [@{simproc nat_combine_numerals},
huffman@45436:        @{simproc nateq_cancel_numerals},
huffman@45436:        @{simproc natless_cancel_numerals},
huffman@45436:        @{simproc natle_cancel_numerals},
huffman@45436:        @{simproc natdiff_cancel_numerals}])
haftmann@33366: *}
haftmann@33366: 
haftmann@37886: end




