src/HOL/Integ/Bin.thy
changeset 14738 83f1a514dcb4
parent 14705 14b2c22a7e40
child 14754 a080eeeaec14
equal deleted inserted replaced
14737:77ea79aed99d 14738:83f1a514dcb4
     8 
     8 
     9 header{*Arithmetic on Binary Integers*}
     9 header{*Arithmetic on Binary Integers*}
    10 
    10 
    11 theory Bin = IntDef + Numeral:
    11 theory Bin = IntDef + Numeral:
    12 
    12 
    13 axclass number_ring \<subseteq> number, ring
    13 axclass number_ring \<subseteq> number, comm_ring_1
    14   number_of_Pls: "number_of bin.Pls = 0"
    14   number_of_Pls: "number_of bin.Pls = 0"
    15   number_of_Min: "number_of bin.Min = - 1"
    15   number_of_Min: "number_of bin.Min = - 1"
    16   number_of_BIT: "number_of(w BIT x) = (if x then 1 else 0) +
    16   number_of_BIT: "number_of(w BIT x) = (if x then 1 else 0) +
    17 	                               (number_of w) + (number_of w)"
    17 	                               (number_of w) + (number_of w)"
    18 subsection{*Converting Numerals to Rings: @{term number_of}*}
    18 subsection{*Converting Numerals to Rings: @{term number_of}*}
   116 by (simp add: iszero_def numeral_m1_eq_minus_1 eq_commute)
   116 by (simp add: iszero_def numeral_m1_eq_minus_1 eq_commute)
   117 
   117 
   118 
   118 
   119 subsection{*Comparisons, for Ordered Rings*}
   119 subsection{*Comparisons, for Ordered Rings*}
   120 
   120 
   121 lemma double_eq_0_iff: "(a + a = 0) = (a = (0::'a::ordered_ring))"
   121 lemma double_eq_0_iff: "(a + a = 0) = (a = (0::'a::ordered_idom))"
   122 proof -
   122 proof -
   123   have "a + a = (1+1)*a" by (simp add: left_distrib)
   123   have "a + a = (1+1)*a" by (simp add: left_distrib)
   124   with zero_less_two [where 'a = 'a]
   124   with zero_less_two [where 'a = 'a]
   125   show ?thesis by force
   125   show ?thesis by force
   126 qed
   126 qed
   155   qed
   155   qed
   156 qed
   156 qed
   157 
   157 
   158 
   158 
   159 text{*The premise involving @{term Ints} prevents @{term "a = 1/2"}.*}
   159 text{*The premise involving @{term Ints} prevents @{term "a = 1/2"}.*}
   160 lemma Ints_odd_nonzero: "a \<in> Ints ==> 1 + a + a \<noteq> (0::'a::ordered_ring)"
   160 lemma Ints_odd_nonzero: "a \<in> Ints ==> 1 + a + a \<noteq> (0::'a::ordered_idom)"
   161 proof (unfold Ints_def) 
   161 proof (unfold Ints_def) 
   162   assume "a \<in> range of_int"
   162   assume "a \<in> range of_int"
   163   then obtain z where a: "a = of_int z" ..
   163   then obtain z where a: "a = of_int z" ..
   164   show ?thesis
   164   show ?thesis
   165   proof
   165   proof
   173 lemma Ints_number_of: "(number_of w :: 'a::number_ring) \<in> Ints"
   173 lemma Ints_number_of: "(number_of w :: 'a::number_ring) \<in> Ints"
   174 by (induct_tac "w", simp_all add: number_of)
   174 by (induct_tac "w", simp_all add: number_of)
   175 
   175 
   176 lemma iszero_number_of_BIT:
   176 lemma iszero_number_of_BIT:
   177      "iszero (number_of (w BIT x)::'a) = 
   177      "iszero (number_of (w BIT x)::'a) = 
   178       (~x & iszero (number_of w::'a::{ordered_ring,number_ring}))"
   178       (~x & iszero (number_of w::'a::{ordered_idom,number_ring}))"
   179 by (simp add: iszero_def compare_rls zero_reorient double_eq_0_iff 
   179 by (simp add: iszero_def compare_rls zero_reorient double_eq_0_iff 
   180               number_of Ints_odd_nonzero [OF Ints_number_of])
   180               number_of Ints_odd_nonzero [OF Ints_number_of])
   181 
   181 
   182 lemma iszero_number_of_0:
   182 lemma iszero_number_of_0:
   183      "iszero (number_of (w BIT False) :: 'a::{ordered_ring,number_ring}) = 
   183      "iszero (number_of (w BIT False) :: 'a::{ordered_idom,number_ring}) = 
   184       iszero (number_of w :: 'a)"
   184       iszero (number_of w :: 'a)"
   185 by (simp only: iszero_number_of_BIT simp_thms)
   185 by (simp only: iszero_number_of_BIT simp_thms)
   186 
   186 
   187 lemma iszero_number_of_1:
   187 lemma iszero_number_of_1:
   188      "~ iszero (number_of (w BIT True)::'a::{ordered_ring,number_ring})"
   188      "~ iszero (number_of (w BIT True)::'a::{ordered_idom,number_ring})"
   189 by (simp only: iszero_number_of_BIT simp_thms)
   189 by (simp only: iszero_number_of_BIT simp_thms)
   190 
   190 
   191 
   191 
   192 
   192 
   193 subsection{*The Less-Than Relation*}
   193 subsection{*The Less-Than Relation*}
   194 
   194 
   195 lemma less_number_of_eq_neg:
   195 lemma less_number_of_eq_neg:
   196     "((number_of x::'a::{ordered_ring,number_ring}) < number_of y)
   196     "((number_of x::'a::{ordered_idom,number_ring}) < number_of y)
   197      = neg (number_of (bin_add x (bin_minus y)) :: 'a)"
   197      = neg (number_of (bin_add x (bin_minus y)) :: 'a)"
   198 apply (subst less_iff_diff_less_0) 
   198 apply (subst less_iff_diff_less_0) 
   199 apply (simp add: neg_def diff_minus number_of_add number_of_minus)
   199 apply (simp add: neg_def diff_minus number_of_add number_of_minus)
   200 done
   200 done
   201 
   201 
   202 text{*If @{term Numeral0} is rewritten to 0 then this rule can't be applied:
   202 text{*If @{term Numeral0} is rewritten to 0 then this rule can't be applied:
   203   @{term Numeral0} IS @{term "number_of Pls"} *}
   203   @{term Numeral0} IS @{term "number_of Pls"} *}
   204 lemma not_neg_number_of_Pls:
   204 lemma not_neg_number_of_Pls:
   205      "~ neg (number_of bin.Pls ::'a::{ordered_ring,number_ring})"
   205      "~ neg (number_of bin.Pls ::'a::{ordered_idom,number_ring})"
   206 by (simp add: neg_def numeral_0_eq_0)
   206 by (simp add: neg_def numeral_0_eq_0)
   207 
   207 
   208 lemma neg_number_of_Min:
   208 lemma neg_number_of_Min:
   209      "neg (number_of bin.Min ::'a::{ordered_ring,number_ring})"
   209      "neg (number_of bin.Min ::'a::{ordered_idom,number_ring})"
   210 by (simp add: neg_def zero_less_one numeral_m1_eq_minus_1)
   210 by (simp add: neg_def zero_less_one numeral_m1_eq_minus_1)
   211 
   211 
   212 lemma double_less_0_iff: "(a + a < 0) = (a < (0::'a::ordered_ring))"
   212 lemma double_less_0_iff: "(a + a < 0) = (a < (0::'a::ordered_idom))"
   213 proof -
   213 proof -
   214   have "(a + a < 0) = ((1+1)*a < 0)" by (simp add: left_distrib)
   214   have "(a + a < 0) = ((1+1)*a < 0)" by (simp add: left_distrib)
   215   also have "... = (a < 0)"
   215   also have "... = (a < 0)"
   216     by (simp add: mult_less_0_iff zero_less_two 
   216     by (simp add: mult_less_0_iff zero_less_two 
   217                   order_less_not_sym [OF zero_less_two]) 
   217                   order_less_not_sym [OF zero_less_two]) 
   229 			add: int_Suc0_eq_1 [symmetric] zadd_int compare_rls)
   229 			add: int_Suc0_eq_1 [symmetric] zadd_int compare_rls)
   230 qed
   230 qed
   231 
   231 
   232 text{*The premise involving @{term Ints} prevents @{term "a = 1/2"}.*}
   232 text{*The premise involving @{term Ints} prevents @{term "a = 1/2"}.*}
   233 lemma Ints_odd_less_0: 
   233 lemma Ints_odd_less_0: 
   234      "a \<in> Ints ==> (1 + a + a < 0) = (a < (0::'a::ordered_ring))";
   234      "a \<in> Ints ==> (1 + a + a < 0) = (a < (0::'a::ordered_idom))";
   235 proof (unfold Ints_def) 
   235 proof (unfold Ints_def) 
   236   assume "a \<in> range of_int"
   236   assume "a \<in> range of_int"
   237   then obtain z where a: "a = of_int z" ..
   237   then obtain z where a: "a = of_int z" ..
   238   hence "((1::'a) + a + a < 0) = (of_int (1 + z + z) < (of_int 0 :: 'a))"
   238   hence "((1::'a) + a + a < 0) = (of_int (1 + z + z) < (of_int 0 :: 'a))"
   239     by (simp add: a)
   239     by (simp add: a)
   242   finally show ?thesis .
   242   finally show ?thesis .
   243 qed
   243 qed
   244 
   244 
   245 lemma neg_number_of_BIT:
   245 lemma neg_number_of_BIT:
   246      "neg (number_of (w BIT x)::'a) = 
   246      "neg (number_of (w BIT x)::'a) = 
   247       neg (number_of w :: 'a::{ordered_ring,number_ring})"
   247       neg (number_of w :: 'a::{ordered_idom,number_ring})"
   248 by (simp add: number_of neg_def double_less_0_iff
   248 by (simp add: number_of neg_def double_less_0_iff
   249               Ints_odd_less_0 [OF Ints_number_of])
   249               Ints_odd_less_0 [OF Ints_number_of])
   250 
   250 
   251 
   251 
   252 text{*Less-Than or Equals*}
   252 text{*Less-Than or Equals*}
   255 lemmas le_number_of_eq_not_less =
   255 lemmas le_number_of_eq_not_less =
   256        linorder_not_less [of "number_of w" "number_of v", symmetric, 
   256        linorder_not_less [of "number_of w" "number_of v", symmetric, 
   257                           standard]
   257                           standard]
   258 
   258 
   259 lemma le_number_of_eq:
   259 lemma le_number_of_eq:
   260     "((number_of x::'a::{ordered_ring,number_ring}) \<le> number_of y)
   260     "((number_of x::'a::{ordered_idom,number_ring}) \<le> number_of y)
   261      = (~ (neg (number_of (bin_add y (bin_minus x)) :: 'a)))"
   261      = (~ (neg (number_of (bin_add y (bin_minus x)) :: 'a)))"
   262 by (simp add: le_number_of_eq_not_less less_number_of_eq_neg)
   262 by (simp add: le_number_of_eq_not_less less_number_of_eq_neg)
   263 
   263 
   264 
   264 
   265 text{*Absolute value (@{term abs})*}
   265 text{*Absolute value (@{term abs})*}
   266 
   266 
   267 lemma abs_number_of:
   267 lemma abs_number_of:
   268      "abs(number_of x::'a::{ordered_ring,number_ring}) =
   268      "abs(number_of x::'a::{ordered_idom,number_ring}) =
   269       (if number_of x < (0::'a) then -number_of x else number_of x)"
   269       (if number_of x < (0::'a) then -number_of x else number_of x)"
   270 by (simp add: abs_if)
   270 by (simp add: abs_if)
   271 
   271 
   272 
   272 
   273 text{*Re-orientation of the equation nnn=x*}
   273 text{*Re-orientation of the equation nnn=x*}