number_ring instances for numeral types
authorhuffman
Thu Feb 19 16:51:46 2009 -0800 (2009-02-19)
changeset 29997f6756c097c2d
parent 29996 c09f348ca88a
child 29998 19e1ef628b25
number_ring instances for numeral types
src/HOL/Library/Numeral_Type.thy
     1.1 --- a/src/HOL/Library/Numeral_Type.thy	Thu Feb 19 12:37:03 2009 -0800
     1.2 +++ b/src/HOL/Library/Numeral_Type.thy	Thu Feb 19 16:51:46 2009 -0800
     1.3 @@ -57,7 +57,7 @@
     1.4  lemma card_option: "CARD('a::finite option) = Suc CARD('a)"
     1.5    unfolding insert_None_conv_UNIV [symmetric]
     1.6    apply (subgoal_tac "(None::'a option) \<notin> range Some")
     1.7 -  apply (simp add: finite card_image)
     1.8 +  apply (simp add: card_image)
     1.9    apply fast
    1.10    done
    1.11  
    1.12 @@ -65,13 +65,26 @@
    1.13    unfolding Pow_UNIV [symmetric]
    1.14    by (simp only: card_Pow finite numeral_2_eq_2)
    1.15  
    1.16 +lemma card_finite_pos [simp]: "0 < CARD('a::finite)"
    1.17 +  unfolding neq0_conv [symmetric] by simp
    1.18 +
    1.19  
    1.20  subsection {* Numeral Types *}
    1.21  
    1.22  typedef (open) num0 = "UNIV :: nat set" ..
    1.23  typedef (open) num1 = "UNIV :: unit set" ..
    1.24 -typedef (open) 'a bit0 = "UNIV :: (bool * 'a) set" ..
    1.25 -typedef (open) 'a bit1 = "UNIV :: (bool * 'a) option set" ..
    1.26 +
    1.27 +typedef (open) 'a bit0 = "{0 ..< 2 * int CARD('a::finite)}"
    1.28 +proof
    1.29 +  show "0 \<in> {0 ..< 2 * int CARD('a)}"
    1.30 +    by simp
    1.31 +qed
    1.32 +
    1.33 +typedef (open) 'a bit1 = "{0 ..< 1 + 2 * int CARD('a::finite)}"
    1.34 +proof
    1.35 +  show "0 \<in> {0 ..< 1 + 2 * int CARD('a)}"
    1.36 +    by simp
    1.37 +qed
    1.38  
    1.39  instance num1 :: finite
    1.40  proof
    1.41 @@ -84,14 +97,14 @@
    1.42  proof
    1.43    show "finite (UNIV::'a bit0 set)"
    1.44      unfolding type_definition.univ [OF type_definition_bit0]
    1.45 -    using finite by (rule finite_imageI)
    1.46 +    by simp
    1.47  qed
    1.48  
    1.49  instance bit1 :: (finite) finite
    1.50  proof
    1.51    show "finite (UNIV::'a bit1 set)"
    1.52      unfolding type_definition.univ [OF type_definition_bit1]
    1.53 -    using finite by (rule finite_imageI)
    1.54 +    by simp
    1.55  qed
    1.56  
    1.57  lemma card_num1: "CARD(num1) = 1"
    1.58 @@ -100,11 +113,11 @@
    1.59  
    1.60  lemma card_bit0: "CARD('a::finite bit0) = 2 * CARD('a)"
    1.61    unfolding type_definition.card [OF type_definition_bit0]
    1.62 -  by (simp only: card_prod card_bool)
    1.63 +  by simp
    1.64  
    1.65  lemma card_bit1: "CARD('a::finite bit1) = Suc (2 * CARD('a))"
    1.66    unfolding type_definition.card [OF type_definition_bit1]
    1.67 -  by (simp only: card_prod card_option card_bool)
    1.68 +  by simp
    1.69  
    1.70  lemma card_num0: "CARD (num0) = 0"
    1.71    by (simp add: infinite_UNIV_nat card_eq_0_iff type_definition.card [OF type_definition_num0])
    1.72 @@ -122,6 +135,230 @@
    1.73    card_num0
    1.74  
    1.75  
    1.76 +subsection {* Locale for modular arithmetic subtypes *}
    1.77 +
    1.78 +locale mod_type =
    1.79 +  fixes n :: int
    1.80 +  and Rep :: "'a::{zero,one,plus,times,uminus,minus,power} \<Rightarrow> int"
    1.81 +  and Abs :: "int \<Rightarrow> 'a::{zero,one,plus,times,uminus,minus,power}"
    1.82 +  assumes type: "type_definition Rep Abs {0..<n}"
    1.83 +  and size1: "1 < n"
    1.84 +  and zero_def: "0 = Abs 0"
    1.85 +  and one_def:  "1 = Abs 1"
    1.86 +  and add_def:  "x + y = Abs ((Rep x + Rep y) mod n)"
    1.87 +  and mult_def: "x * y = Abs ((Rep x * Rep y) mod n)"
    1.88 +  and diff_def: "x - y = Abs ((Rep x - Rep y) mod n)"
    1.89 +  and minus_def: "- x = Abs ((- Rep x) mod n)"
    1.90 +  and power_def: "x ^ k = Abs (Rep x ^ k mod n)"
    1.91 +begin
    1.92 +
    1.93 +lemma size0: "0 < n"
    1.94 +by (cut_tac size1, simp)
    1.95 +
    1.96 +lemmas definitions =
    1.97 +  zero_def one_def add_def mult_def minus_def diff_def power_def
    1.98 +
    1.99 +lemma Rep_less_n: "Rep x < n"
   1.100 +by (rule type_definition.Rep [OF type, simplified, THEN conjunct2])
   1.101 +
   1.102 +lemma Rep_le_n: "Rep x \<le> n"
   1.103 +by (rule Rep_less_n [THEN order_less_imp_le])
   1.104 +
   1.105 +lemma Rep_inject_sym: "x = y \<longleftrightarrow> Rep x = Rep y"
   1.106 +by (rule type_definition.Rep_inject [OF type, symmetric])
   1.107 +
   1.108 +lemma Rep_inverse: "Abs (Rep x) = x"
   1.109 +by (rule type_definition.Rep_inverse [OF type])
   1.110 +
   1.111 +lemma Abs_inverse: "m \<in> {0..<n} \<Longrightarrow> Rep (Abs m) = m"
   1.112 +by (rule type_definition.Abs_inverse [OF type])
   1.113 +
   1.114 +lemma Rep_Abs_mod: "Rep (Abs (m mod n)) = m mod n"
   1.115 +by (simp add: Abs_inverse IntDiv.pos_mod_conj [OF size0])
   1.116 +
   1.117 +lemma Rep_Abs_0: "Rep (Abs 0) = 0"
   1.118 +by (simp add: Abs_inverse size0)
   1.119 +
   1.120 +lemma Rep_0: "Rep 0 = 0"
   1.121 +by (simp add: zero_def Rep_Abs_0)
   1.122 +
   1.123 +lemma Rep_Abs_1: "Rep (Abs 1) = 1"
   1.124 +by (simp add: Abs_inverse size1)
   1.125 +
   1.126 +lemma Rep_1: "Rep 1 = 1"
   1.127 +by (simp add: one_def Rep_Abs_1)
   1.128 +
   1.129 +lemma Rep_mod: "Rep x mod n = Rep x"
   1.130 +apply (rule_tac x=x in type_definition.Abs_cases [OF type])
   1.131 +apply (simp add: type_definition.Abs_inverse [OF type])
   1.132 +apply (simp add: mod_pos_pos_trivial)
   1.133 +done
   1.134 +
   1.135 +lemmas Rep_simps =
   1.136 +  Rep_inject_sym Rep_inverse Rep_Abs_mod Rep_mod Rep_Abs_0 Rep_Abs_1
   1.137 +
   1.138 +lemma comm_ring_1: "OFCLASS('a, comm_ring_1_class)"
   1.139 +apply (intro_classes, unfold definitions)
   1.140 +apply (simp_all add: Rep_simps zmod_simps ring_simps)
   1.141 +done
   1.142 +
   1.143 +lemma recpower: "OFCLASS('a, recpower_class)"
   1.144 +apply (intro_classes, unfold definitions)
   1.145 +apply (simp_all add: Rep_simps zmod_simps add_ac mult_assoc
   1.146 +                     mod_pos_pos_trivial size1)
   1.147 +done
   1.148 +
   1.149 +end
   1.150 +
   1.151 +locale mod_ring = mod_type +
   1.152 +  constrains n :: int
   1.153 +  and Rep :: "'a::{number_ring,power} \<Rightarrow> int"
   1.154 +  and Abs :: "int \<Rightarrow> 'a::{number_ring,power}"
   1.155 +begin
   1.156 +
   1.157 +lemma of_nat_eq: "of_nat k = Abs (int k mod n)"
   1.158 +apply (induct k)
   1.159 +apply (simp add: zero_def)
   1.160 +apply (simp add: Rep_simps add_def one_def zmod_simps add_ac)
   1.161 +done
   1.162 +
   1.163 +lemma of_int_eq: "of_int z = Abs (z mod n)"
   1.164 +apply (cases z rule: int_diff_cases)
   1.165 +apply (simp add: Rep_simps of_nat_eq diff_def zmod_simps)
   1.166 +done
   1.167 +
   1.168 +lemma Rep_number_of:
   1.169 +  "Rep (number_of w) = number_of w mod n"
   1.170 +by (simp add: number_of_eq of_int_eq Rep_Abs_mod)
   1.171 +
   1.172 +lemma iszero_number_of:
   1.173 +  "iszero (number_of w::'a) \<longleftrightarrow> number_of w mod n = 0"
   1.174 +by (simp add: Rep_simps number_of_eq of_int_eq iszero_def zero_def)
   1.175 +
   1.176 +lemma cases:
   1.177 +  assumes 1: "\<And>z. \<lbrakk>(x::'a) = of_int z; 0 \<le> z; z < n\<rbrakk> \<Longrightarrow> P"
   1.178 +  shows "P"
   1.179 +apply (cases x rule: type_definition.Abs_cases [OF type])
   1.180 +apply (rule_tac z="y" in 1)
   1.181 +apply (simp_all add: of_int_eq mod_pos_pos_trivial)
   1.182 +done
   1.183 +
   1.184 +lemma induct:
   1.185 +  "(\<And>z. \<lbrakk>0 \<le> z; z < n\<rbrakk> \<Longrightarrow> P (of_int z)) \<Longrightarrow> P (x::'a)"
   1.186 +by (cases x rule: cases) simp
   1.187 +
   1.188 +end
   1.189 +
   1.190 +
   1.191 +subsection {* Number ring instances *}
   1.192 +
   1.193 +instantiation
   1.194 +  bit0 and bit1 :: (finite) "{zero,one,plus,times,uminus,minus,power}"
   1.195 +begin
   1.196 +
   1.197 +definition Abs_bit0' :: "int \<Rightarrow> 'a bit0" where
   1.198 +  "Abs_bit0' x = Abs_bit0 (x mod (2 * int CARD('a)))"
   1.199 +
   1.200 +definition Abs_bit1' :: "int \<Rightarrow> 'a bit1" where
   1.201 +  "Abs_bit1' x = Abs_bit1 (x mod (1 + 2 * int CARD('a)))"
   1.202 +
   1.203 +definition "0 = Abs_bit0 0"
   1.204 +definition "1 = Abs_bit0 1"
   1.205 +definition "x + y = Abs_bit0' (Rep_bit0 x + Rep_bit0 y)"
   1.206 +definition "x * y = Abs_bit0' (Rep_bit0 x * Rep_bit0 y)"
   1.207 +definition "x - y = Abs_bit0' (Rep_bit0 x - Rep_bit0 y)"
   1.208 +definition "- x = Abs_bit0' (- Rep_bit0 x)"
   1.209 +definition "x ^ k = Abs_bit0' (Rep_bit0 x ^ k)"
   1.210 +
   1.211 +definition "0 = Abs_bit1 0"
   1.212 +definition "1 = Abs_bit1 1"
   1.213 +definition "x + y = Abs_bit1' (Rep_bit1 x + Rep_bit1 y)"
   1.214 +definition "x * y = Abs_bit1' (Rep_bit1 x * Rep_bit1 y)"
   1.215 +definition "x - y = Abs_bit1' (Rep_bit1 x - Rep_bit1 y)"
   1.216 +definition "- x = Abs_bit1' (- Rep_bit1 x)"
   1.217 +definition "x ^ k = Abs_bit1' (Rep_bit1 x ^ k)"
   1.218 +
   1.219 +instance ..
   1.220 +
   1.221 +end
   1.222 +
   1.223 +interpretation bit0!:
   1.224 +  mod_type "2 * int CARD('a::finite)"
   1.225 +           "Rep_bit0 :: 'a::finite bit0 \<Rightarrow> int"
   1.226 +           "Abs_bit0 :: int \<Rightarrow> 'a::finite bit0"
   1.227 +apply (rule mod_type.intro)
   1.228 +apply (rule type_definition_bit0)
   1.229 +using card_finite_pos [where ?'a='a] apply arith
   1.230 +apply (rule zero_bit0_def)
   1.231 +apply (rule one_bit0_def)
   1.232 +apply (rule plus_bit0_def [unfolded Abs_bit0'_def])
   1.233 +apply (rule times_bit0_def [unfolded Abs_bit0'_def])
   1.234 +apply (rule minus_bit0_def [unfolded Abs_bit0'_def])
   1.235 +apply (rule uminus_bit0_def [unfolded Abs_bit0'_def])
   1.236 +apply (rule power_bit0_def [unfolded Abs_bit0'_def])
   1.237 +done
   1.238 +
   1.239 +interpretation bit1!:
   1.240 +  mod_type "1 + 2 * int CARD('a::finite)"
   1.241 +           "Rep_bit1 :: 'a::finite bit1 \<Rightarrow> int"
   1.242 +           "Abs_bit1 :: int \<Rightarrow> 'a::finite bit1"
   1.243 +apply (rule mod_type.intro)
   1.244 +apply (rule type_definition_bit1)
   1.245 +apply simp
   1.246 +apply (rule zero_bit1_def)
   1.247 +apply (rule one_bit1_def)
   1.248 +apply (rule plus_bit1_def [unfolded Abs_bit1'_def])
   1.249 +apply (rule times_bit1_def [unfolded Abs_bit1'_def])
   1.250 +apply (rule minus_bit1_def [unfolded Abs_bit1'_def])
   1.251 +apply (rule uminus_bit1_def [unfolded Abs_bit1'_def])
   1.252 +apply (rule power_bit1_def [unfolded Abs_bit1'_def])
   1.253 +done
   1.254 +
   1.255 +instance bit0 :: (finite) "{comm_ring_1,recpower}"
   1.256 +  by (rule bit0.comm_ring_1 bit0.recpower)+
   1.257 +
   1.258 +instance bit1 :: (finite) "{comm_ring_1,recpower}"
   1.259 +  by (rule bit1.comm_ring_1 bit1.recpower)+
   1.260 +
   1.261 +instantiation bit0 and bit1 :: (finite) number_ring
   1.262 +begin
   1.263 +
   1.264 +definition "(number_of w :: _ bit0) = of_int w"
   1.265 +
   1.266 +definition "(number_of w :: _ bit1) = of_int w"
   1.267 +
   1.268 +instance proof
   1.269 +qed (rule number_of_bit0_def number_of_bit1_def)+
   1.270 +
   1.271 +end
   1.272 +
   1.273 +interpretation bit0!:
   1.274 +  mod_ring "2 * int CARD('a::finite)"
   1.275 +           "Rep_bit0 :: 'a::finite bit0 \<Rightarrow> int"
   1.276 +           "Abs_bit0 :: int \<Rightarrow> 'a::finite bit0"
   1.277 +  ..
   1.278 +
   1.279 +interpretation bit1!:
   1.280 +  mod_ring "1 + 2 * int CARD('a::finite)"
   1.281 +           "Rep_bit1 :: 'a::finite bit1 \<Rightarrow> int"
   1.282 +           "Abs_bit1 :: int \<Rightarrow> 'a::finite bit1"
   1.283 +  ..
   1.284 +
   1.285 +text {* Set up cases, induction, and arithmetic *}
   1.286 +
   1.287 +lemmas bit0_cases [cases type: bit0, case_names of_int] = bit0.cases
   1.288 +lemmas bit1_cases [cases type: bit1, case_names of_int] = bit1.cases
   1.289 +
   1.290 +lemmas bit0_induct [induct type: bit0, case_names of_int] = bit0.induct
   1.291 +lemmas bit1_induct [induct type: bit1, case_names of_int] = bit1.induct
   1.292 +
   1.293 +lemmas bit0_iszero_number_of [simp] = bit0.iszero_number_of
   1.294 +lemmas bit1_iszero_number_of [simp] = bit1.iszero_number_of
   1.295 +
   1.296 +declare power_Suc [where ?'a="'a::finite bit0", standard, simp]
   1.297 +declare power_Suc [where ?'a="'a::finite bit1", standard, simp]
   1.298 +
   1.299 +
   1.300  subsection {* Syntax *}
   1.301  
   1.302  syntax
   1.303 @@ -221,5 +458,6 @@
   1.304  
   1.305  lemma "CARD(0) = 0" by simp
   1.306  lemma "CARD(17) = 17" by simp
   1.307 +lemma "8 * 11 ^ 3 - 6 = (2::5)" by simp
   1.308  
   1.309  end