src/HOL/Divides.thy
changeset 33296 a3924d1069e5
parent 33274 b6ff7db522b5
child 33318 ddd97d9dfbfb
     1.1 --- a/src/HOL/Divides.thy	Wed Oct 28 17:44:03 2009 +0100
     1.2 +++ b/src/HOL/Divides.thy	Wed Oct 28 19:09:47 2009 +0100
     1.3 @@ -6,8 +6,16 @@
     1.4  header {* The division operators div and mod *}
     1.5  
     1.6  theory Divides
     1.7 -imports Nat Power Product_Type
     1.8 -uses "~~/src/Provers/Arith/cancel_div_mod.ML"
     1.9 +imports Nat_Numeral
    1.10 +uses
    1.11 +  "~~/src/Provers/Arith/assoc_fold.ML"
    1.12 +  "~~/src/Provers/Arith/cancel_numerals.ML"
    1.13 +  "~~/src/Provers/Arith/combine_numerals.ML"
    1.14 +  "~~/src/Provers/Arith/cancel_numeral_factor.ML"
    1.15 +  "~~/src/Provers/Arith/extract_common_term.ML"
    1.16 +  ("Tools/numeral_simprocs.ML")
    1.17 +  ("Tools/nat_numeral_simprocs.ML")
    1.18 +  "~~/src/Provers/Arith/cancel_div_mod.ML"
    1.19  begin
    1.20  
    1.21  subsection {* Syntactic division operations *}
    1.22 @@ -1092,4 +1100,158 @@
    1.23    with j show ?thesis by blast
    1.24  qed
    1.25  
    1.26 +lemma div2_Suc_Suc [simp]: "Suc (Suc m) div 2 = Suc (m div 2)"
    1.27 +by (auto simp add: numeral_2_eq_2 le_div_geq)
    1.28 +
    1.29 +lemma add_self_div_2 [simp]: "(m + m) div 2 = (m::nat)"
    1.30 +by (simp add: nat_mult_2 [symmetric])
    1.31 +
    1.32 +lemma mod2_Suc_Suc [simp]: "Suc(Suc(m)) mod 2 = m mod 2"
    1.33 +apply (subgoal_tac "m mod 2 < 2")
    1.34 +apply (erule less_2_cases [THEN disjE])
    1.35 +apply (simp_all (no_asm_simp) add: Let_def mod_Suc nat_1)
    1.36 +done
    1.37 +
    1.38 +lemma mod2_gr_0 [simp]: "0 < (m\<Colon>nat) mod 2 \<longleftrightarrow> m mod 2 = 1"
    1.39 +proof -
    1.40 +  { fix n :: nat have  "(n::nat) < 2 \<Longrightarrow> n = 0 \<or> n = 1" by (induct n) simp_all }
    1.41 +  moreover have "m mod 2 < 2" by simp
    1.42 +  ultimately have "m mod 2 = 0 \<or> m mod 2 = 1" .
    1.43 +  then show ?thesis by auto
    1.44 +qed
    1.45 +
    1.46 +text{*These lemmas collapse some needless occurrences of Suc:
    1.47 +    at least three Sucs, since two and fewer are rewritten back to Suc again!
    1.48 +    We already have some rules to simplify operands smaller than 3.*}
    1.49 +
    1.50 +lemma div_Suc_eq_div_add3 [simp]: "m div (Suc (Suc (Suc n))) = m div (3+n)"
    1.51 +by (simp add: Suc3_eq_add_3)
    1.52 +
    1.53 +lemma mod_Suc_eq_mod_add3 [simp]: "m mod (Suc (Suc (Suc n))) = m mod (3+n)"
    1.54 +by (simp add: Suc3_eq_add_3)
    1.55 +
    1.56 +lemma Suc_div_eq_add3_div: "(Suc (Suc (Suc m))) div n = (3+m) div n"
    1.57 +by (simp add: Suc3_eq_add_3)
    1.58 +
    1.59 +lemma Suc_mod_eq_add3_mod: "(Suc (Suc (Suc m))) mod n = (3+m) mod n"
    1.60 +by (simp add: Suc3_eq_add_3)
    1.61 +
    1.62 +lemmas Suc_div_eq_add3_div_number_of =
    1.63 +    Suc_div_eq_add3_div [of _ "number_of v", standard]
    1.64 +declare Suc_div_eq_add3_div_number_of [simp]
    1.65 +
    1.66 +lemmas Suc_mod_eq_add3_mod_number_of =
    1.67 +    Suc_mod_eq_add3_mod [of _ "number_of v", standard]
    1.68 +declare Suc_mod_eq_add3_mod_number_of [simp]
    1.69 +
    1.70 +
    1.71 +subsection {* Proof Tools setup; Combination and Cancellation Simprocs *}
    1.72 +
    1.73 +declare split_div[of _ _ "number_of k", standard, arith_split]
    1.74 +declare split_mod[of _ _ "number_of k", standard, arith_split]
    1.75 +
    1.76 +
    1.77 +subsubsection{*For @{text combine_numerals}*}
    1.78 +
    1.79 +lemma left_add_mult_distrib: "i*u + (j*u + k) = (i+j)*u + (k::nat)"
    1.80 +by (simp add: add_mult_distrib)
    1.81 +
    1.82 +
    1.83 +subsubsection{*For @{text cancel_numerals}*}
    1.84 +
    1.85 +lemma nat_diff_add_eq1:
    1.86 +     "j <= (i::nat) ==> ((i*u + m) - (j*u + n)) = (((i-j)*u + m) - n)"
    1.87 +by (simp split add: nat_diff_split add: add_mult_distrib)
    1.88 +
    1.89 +lemma nat_diff_add_eq2:
    1.90 +     "i <= (j::nat) ==> ((i*u + m) - (j*u + n)) = (m - ((j-i)*u + n))"
    1.91 +by (simp split add: nat_diff_split add: add_mult_distrib)
    1.92 +
    1.93 +lemma nat_eq_add_iff1:
    1.94 +     "j <= (i::nat) ==> (i*u + m = j*u + n) = ((i-j)*u + m = n)"
    1.95 +by (auto split add: nat_diff_split simp add: add_mult_distrib)
    1.96 +
    1.97 +lemma nat_eq_add_iff2:
    1.98 +     "i <= (j::nat) ==> (i*u + m = j*u + n) = (m = (j-i)*u + n)"
    1.99 +by (auto split add: nat_diff_split simp add: add_mult_distrib)
   1.100 +
   1.101 +lemma nat_less_add_iff1:
   1.102 +     "j <= (i::nat) ==> (i*u + m < j*u + n) = ((i-j)*u + m < n)"
   1.103 +by (auto split add: nat_diff_split simp add: add_mult_distrib)
   1.104 +
   1.105 +lemma nat_less_add_iff2:
   1.106 +     "i <= (j::nat) ==> (i*u + m < j*u + n) = (m < (j-i)*u + n)"
   1.107 +by (auto split add: nat_diff_split simp add: add_mult_distrib)
   1.108 +
   1.109 +lemma nat_le_add_iff1:
   1.110 +     "j <= (i::nat) ==> (i*u + m <= j*u + n) = ((i-j)*u + m <= n)"
   1.111 +by (auto split add: nat_diff_split simp add: add_mult_distrib)
   1.112 +
   1.113 +lemma nat_le_add_iff2:
   1.114 +     "i <= (j::nat) ==> (i*u + m <= j*u + n) = (m <= (j-i)*u + n)"
   1.115 +by (auto split add: nat_diff_split simp add: add_mult_distrib)
   1.116 +
   1.117 +
   1.118 +subsubsection{*For @{text cancel_numeral_factors} *}
   1.119 +
   1.120 +lemma nat_mult_le_cancel1: "(0::nat) < k ==> (k*m <= k*n) = (m<=n)"
   1.121 +by auto
   1.122 +
   1.123 +lemma nat_mult_less_cancel1: "(0::nat) < k ==> (k*m < k*n) = (m<n)"
   1.124 +by auto
   1.125 +
   1.126 +lemma nat_mult_eq_cancel1: "(0::nat) < k ==> (k*m = k*n) = (m=n)"
   1.127 +by auto
   1.128 +
   1.129 +lemma nat_mult_div_cancel1: "(0::nat) < k ==> (k*m) div (k*n) = (m div n)"
   1.130 +by auto
   1.131 +
   1.132 +lemma nat_mult_dvd_cancel_disj[simp]:
   1.133 +  "(k*m) dvd (k*n) = (k=0 | m dvd (n::nat))"
   1.134 +by(auto simp: dvd_eq_mod_eq_0 mod_mult_distrib2[symmetric])
   1.135 +
   1.136 +lemma nat_mult_dvd_cancel1: "0 < k \<Longrightarrow> (k*m) dvd (k*n::nat) = (m dvd n)"
   1.137 +by(auto)
   1.138 +
   1.139 +
   1.140 +subsubsection{*For @{text cancel_factor} *}
   1.141 +
   1.142 +lemma nat_mult_le_cancel_disj: "(k*m <= k*n) = ((0::nat) < k --> m<=n)"
   1.143 +by auto
   1.144 +
   1.145 +lemma nat_mult_less_cancel_disj: "(k*m < k*n) = ((0::nat) < k & m<n)"
   1.146 +by auto
   1.147 +
   1.148 +lemma nat_mult_eq_cancel_disj: "(k*m = k*n) = (k = (0::nat) | m=n)"
   1.149 +by auto
   1.150 +
   1.151 +lemma nat_mult_div_cancel_disj[simp]:
   1.152 +     "(k*m) div (k*n) = (if k = (0::nat) then 0 else m div n)"
   1.153 +by (simp add: nat_mult_div_cancel1)
   1.154 +
   1.155 +
   1.156 +use "Tools/numeral_simprocs.ML"
   1.157 +
   1.158 +use "Tools/nat_numeral_simprocs.ML"
   1.159 +
   1.160 +declaration {* 
   1.161 +  K (Lin_Arith.add_simps (@{thms neg_simps} @ [@{thm Suc_nat_number_of}, @{thm int_nat_number_of}])
   1.162 +  #> Lin_Arith.add_simps (@{thms ring_distribs} @ [@{thm Let_number_of}, @{thm Let_0}, @{thm Let_1},
   1.163 +     @{thm nat_0}, @{thm nat_1},
   1.164 +     @{thm add_nat_number_of}, @{thm diff_nat_number_of}, @{thm mult_nat_number_of},
   1.165 +     @{thm eq_nat_number_of}, @{thm less_nat_number_of}, @{thm le_number_of_eq_not_less},
   1.166 +     @{thm le_Suc_number_of}, @{thm le_number_of_Suc},
   1.167 +     @{thm less_Suc_number_of}, @{thm less_number_of_Suc},
   1.168 +     @{thm Suc_eq_number_of}, @{thm eq_number_of_Suc},
   1.169 +     @{thm mult_Suc}, @{thm mult_Suc_right},
   1.170 +     @{thm add_Suc}, @{thm add_Suc_right},
   1.171 +     @{thm eq_number_of_0}, @{thm eq_0_number_of}, @{thm less_0_number_of},
   1.172 +     @{thm of_int_number_of_eq}, @{thm of_nat_number_of_eq}, @{thm nat_number_of},
   1.173 +     @{thm if_True}, @{thm if_False}])
   1.174 +  #> Lin_Arith.add_simprocs (Numeral_Simprocs.assoc_fold_simproc
   1.175 +      :: Numeral_Simprocs.combine_numerals
   1.176 +      :: Numeral_Simprocs.cancel_numerals)
   1.177 +  #> Lin_Arith.add_simprocs (Nat_Numeral_Simprocs.combine_numerals :: Nat_Numeral_Simprocs.cancel_numerals))
   1.178 +*}
   1.179 +
   1.180  end