src/HOL/Library/Numeral_Type.thy
changeset 30240 5b25fee0362c
parent 29629 5111ce425e7a
child 30242 aea5d7fa7ef5
     1.1 --- a/src/HOL/Library/Numeral_Type.thy	Wed Mar 04 10:43:39 2009 +0100
     1.2 +++ b/src/HOL/Library/Numeral_Type.thy	Wed Mar 04 10:45:52 2009 +0100
     1.3 @@ -42,36 +42,87 @@
     1.4  end
     1.5  *}
     1.6  
     1.7 -lemma card_unit: "CARD(unit) = 1"
     1.8 +lemma card_unit [simp]: "CARD(unit) = 1"
     1.9    unfolding UNIV_unit by simp
    1.10  
    1.11 -lemma card_bool: "CARD(bool) = 2"
    1.12 +lemma card_bool [simp]: "CARD(bool) = 2"
    1.13    unfolding UNIV_bool by simp
    1.14  
    1.15 -lemma card_prod: "CARD('a::finite \<times> 'b::finite) = CARD('a) * CARD('b)"
    1.16 +lemma card_prod [simp]: "CARD('a \<times> 'b) = CARD('a::finite) * CARD('b::finite)"
    1.17    unfolding UNIV_Times_UNIV [symmetric] by (simp only: card_cartesian_product)
    1.18  
    1.19 -lemma card_sum: "CARD('a::finite + 'b::finite) = CARD('a) + CARD('b)"
    1.20 +lemma card_sum [simp]: "CARD('a + 'b) = CARD('a::finite) + CARD('b::finite)"
    1.21    unfolding UNIV_Plus_UNIV [symmetric] by (simp only: finite card_Plus)
    1.22  
    1.23 -lemma card_option: "CARD('a::finite option) = Suc CARD('a)"
    1.24 +lemma card_option [simp]: "CARD('a option) = Suc CARD('a::finite)"
    1.25    unfolding insert_None_conv_UNIV [symmetric]
    1.26    apply (subgoal_tac "(None::'a option) \<notin> range Some")
    1.27 -  apply (simp add: finite card_image)
    1.28 +  apply (simp add: card_image)
    1.29    apply fast
    1.30    done
    1.31  
    1.32 -lemma card_set: "CARD('a::finite set) = 2 ^ CARD('a)"
    1.33 +lemma card_set [simp]: "CARD('a set) = 2 ^ CARD('a::finite)"
    1.34    unfolding Pow_UNIV [symmetric]
    1.35    by (simp only: card_Pow finite numeral_2_eq_2)
    1.36  
    1.37 +lemma card_nat [simp]: "CARD(nat) = 0"
    1.38 +  by (simp add: infinite_UNIV_nat card_eq_0_iff)
    1.39 +
    1.40 +
    1.41 +subsection {* Classes with at least 1 and 2  *}
    1.42 +
    1.43 +text {* Class finite already captures "at least 1" *}
    1.44 +
    1.45 +lemma zero_less_card_finite [simp]: "0 < CARD('a::finite)"
    1.46 +  unfolding neq0_conv [symmetric] by simp
    1.47 +
    1.48 +lemma one_le_card_finite [simp]: "Suc 0 \<le> CARD('a::finite)"
    1.49 +  by (simp add: less_Suc_eq_le [symmetric])
    1.50 +
    1.51 +text {* Class for cardinality "at least 2" *}
    1.52 +
    1.53 +class card2 = finite + 
    1.54 +  assumes two_le_card: "2 \<le> CARD('a)"
    1.55 +
    1.56 +lemma one_less_card: "Suc 0 < CARD('a::card2)"
    1.57 +  using two_le_card [where 'a='a] by simp
    1.58 +
    1.59 +lemma one_less_int_card: "1 < int CARD('a::card2)"
    1.60 +  using one_less_card [where 'a='a] by simp
    1.61 +
    1.62  
    1.63  subsection {* Numeral Types *}
    1.64  
    1.65  typedef (open) num0 = "UNIV :: nat set" ..
    1.66  typedef (open) num1 = "UNIV :: unit set" ..
    1.67 -typedef (open) 'a bit0 = "UNIV :: (bool * 'a) set" ..
    1.68 -typedef (open) 'a bit1 = "UNIV :: (bool * 'a) option set" ..
    1.69 +
    1.70 +typedef (open) 'a bit0 = "{0 ..< 2 * int CARD('a::finite)}"
    1.71 +proof
    1.72 +  show "0 \<in> {0 ..< 2 * int CARD('a)}"
    1.73 +    by simp
    1.74 +qed
    1.75 +
    1.76 +typedef (open) 'a bit1 = "{0 ..< 1 + 2 * int CARD('a::finite)}"
    1.77 +proof
    1.78 +  show "0 \<in> {0 ..< 1 + 2 * int CARD('a)}"
    1.79 +    by simp
    1.80 +qed
    1.81 +
    1.82 +lemma card_num0 [simp]: "CARD (num0) = 0"
    1.83 +  unfolding type_definition.card [OF type_definition_num0]
    1.84 +  by simp
    1.85 +
    1.86 +lemma card_num1 [simp]: "CARD(num1) = 1"
    1.87 +  unfolding type_definition.card [OF type_definition_num1]
    1.88 +  by (simp only: card_unit)
    1.89 +
    1.90 +lemma card_bit0 [simp]: "CARD('a bit0) = 2 * CARD('a::finite)"
    1.91 +  unfolding type_definition.card [OF type_definition_bit0]
    1.92 +  by simp
    1.93 +
    1.94 +lemma card_bit1 [simp]: "CARD('a bit1) = Suc (2 * CARD('a::finite))"
    1.95 +  unfolding type_definition.card [OF type_definition_bit1]
    1.96 +  by simp
    1.97  
    1.98  instance num1 :: finite
    1.99  proof
   1.100 @@ -80,46 +131,263 @@
   1.101      using finite by (rule finite_imageI)
   1.102  qed
   1.103  
   1.104 -instance bit0 :: (finite) finite
   1.105 +instance bit0 :: (finite) card2
   1.106  proof
   1.107    show "finite (UNIV::'a bit0 set)"
   1.108      unfolding type_definition.univ [OF type_definition_bit0]
   1.109 -    using finite by (rule finite_imageI)
   1.110 +    by simp
   1.111 +  show "2 \<le> CARD('a bit0)"
   1.112 +    by simp
   1.113  qed
   1.114  
   1.115 -instance bit1 :: (finite) finite
   1.116 +instance bit1 :: (finite) card2
   1.117  proof
   1.118    show "finite (UNIV::'a bit1 set)"
   1.119      unfolding type_definition.univ [OF type_definition_bit1]
   1.120 -    using finite by (rule finite_imageI)
   1.121 +    by simp
   1.122 +  show "2 \<le> CARD('a bit1)"
   1.123 +    by simp
   1.124  qed
   1.125  
   1.126 -lemma card_num1: "CARD(num1) = 1"
   1.127 -  unfolding type_definition.card [OF type_definition_num1]
   1.128 -  by (simp only: card_unit)
   1.129 +
   1.130 +subsection {* Locale for modular arithmetic subtypes *}
   1.131 +
   1.132 +locale mod_type =
   1.133 +  fixes n :: int
   1.134 +  and Rep :: "'a::{zero,one,plus,times,uminus,minus,power} \<Rightarrow> int"
   1.135 +  and Abs :: "int \<Rightarrow> 'a::{zero,one,plus,times,uminus,minus,power}"
   1.136 +  assumes type: "type_definition Rep Abs {0..<n}"
   1.137 +  and size1: "1 < n"
   1.138 +  and zero_def: "0 = Abs 0"
   1.139 +  and one_def:  "1 = Abs 1"
   1.140 +  and add_def:  "x + y = Abs ((Rep x + Rep y) mod n)"
   1.141 +  and mult_def: "x * y = Abs ((Rep x * Rep y) mod n)"
   1.142 +  and diff_def: "x - y = Abs ((Rep x - Rep y) mod n)"
   1.143 +  and minus_def: "- x = Abs ((- Rep x) mod n)"
   1.144 +  and power_def: "x ^ k = Abs (Rep x ^ k mod n)"
   1.145 +begin
   1.146 +
   1.147 +lemma size0: "0 < n"
   1.148 +by (cut_tac size1, simp)
   1.149 +
   1.150 +lemmas definitions =
   1.151 +  zero_def one_def add_def mult_def minus_def diff_def power_def
   1.152 +
   1.153 +lemma Rep_less_n: "Rep x < n"
   1.154 +by (rule type_definition.Rep [OF type, simplified, THEN conjunct2])
   1.155 +
   1.156 +lemma Rep_le_n: "Rep x \<le> n"
   1.157 +by (rule Rep_less_n [THEN order_less_imp_le])
   1.158 +
   1.159 +lemma Rep_inject_sym: "x = y \<longleftrightarrow> Rep x = Rep y"
   1.160 +by (rule type_definition.Rep_inject [OF type, symmetric])
   1.161 +
   1.162 +lemma Rep_inverse: "Abs (Rep x) = x"
   1.163 +by (rule type_definition.Rep_inverse [OF type])
   1.164 +
   1.165 +lemma Abs_inverse: "m \<in> {0..<n} \<Longrightarrow> Rep (Abs m) = m"
   1.166 +by (rule type_definition.Abs_inverse [OF type])
   1.167 +
   1.168 +lemma Rep_Abs_mod: "Rep (Abs (m mod n)) = m mod n"
   1.169 +by (simp add: Abs_inverse IntDiv.pos_mod_conj [OF size0])
   1.170 +
   1.171 +lemma Rep_Abs_0: "Rep (Abs 0) = 0"
   1.172 +by (simp add: Abs_inverse size0)
   1.173 +
   1.174 +lemma Rep_0: "Rep 0 = 0"
   1.175 +by (simp add: zero_def Rep_Abs_0)
   1.176 +
   1.177 +lemma Rep_Abs_1: "Rep (Abs 1) = 1"
   1.178 +by (simp add: Abs_inverse size1)
   1.179 +
   1.180 +lemma Rep_1: "Rep 1 = 1"
   1.181 +by (simp add: one_def Rep_Abs_1)
   1.182  
   1.183 -lemma card_bit0: "CARD('a::finite bit0) = 2 * CARD('a)"
   1.184 -  unfolding type_definition.card [OF type_definition_bit0]
   1.185 -  by (simp only: card_prod card_bool)
   1.186 +lemma Rep_mod: "Rep x mod n = Rep x"
   1.187 +apply (rule_tac x=x in type_definition.Abs_cases [OF type])
   1.188 +apply (simp add: type_definition.Abs_inverse [OF type])
   1.189 +apply (simp add: mod_pos_pos_trivial)
   1.190 +done
   1.191 +
   1.192 +lemmas Rep_simps =
   1.193 +  Rep_inject_sym Rep_inverse Rep_Abs_mod Rep_mod Rep_Abs_0 Rep_Abs_1
   1.194 +
   1.195 +lemma comm_ring_1: "OFCLASS('a, comm_ring_1_class)"
   1.196 +apply (intro_classes, unfold definitions)
   1.197 +apply (simp_all add: Rep_simps zmod_simps ring_simps)
   1.198 +done
   1.199 +
   1.200 +lemma recpower: "OFCLASS('a, recpower_class)"
   1.201 +apply (intro_classes, unfold definitions)
   1.202 +apply (simp_all add: Rep_simps zmod_simps add_ac mult_assoc
   1.203 +                     mod_pos_pos_trivial size1)
   1.204 +done
   1.205 +
   1.206 +end
   1.207 +
   1.208 +locale mod_ring = mod_type +
   1.209 +  constrains n :: int
   1.210 +  and Rep :: "'a::{number_ring,power} \<Rightarrow> int"
   1.211 +  and Abs :: "int \<Rightarrow> 'a::{number_ring,power}"
   1.212 +begin
   1.213  
   1.214 -lemma card_bit1: "CARD('a::finite bit1) = Suc (2 * CARD('a))"
   1.215 -  unfolding type_definition.card [OF type_definition_bit1]
   1.216 -  by (simp only: card_prod card_option card_bool)
   1.217 +lemma of_nat_eq: "of_nat k = Abs (int k mod n)"
   1.218 +apply (induct k)
   1.219 +apply (simp add: zero_def)
   1.220 +apply (simp add: Rep_simps add_def one_def zmod_simps add_ac)
   1.221 +done
   1.222 +
   1.223 +lemma of_int_eq: "of_int z = Abs (z mod n)"
   1.224 +apply (cases z rule: int_diff_cases)
   1.225 +apply (simp add: Rep_simps of_nat_eq diff_def zmod_simps)
   1.226 +done
   1.227 +
   1.228 +lemma Rep_number_of:
   1.229 +  "Rep (number_of w) = number_of w mod n"
   1.230 +by (simp add: number_of_eq of_int_eq Rep_Abs_mod)
   1.231 +
   1.232 +lemma iszero_number_of:
   1.233 +  "iszero (number_of w::'a) \<longleftrightarrow> number_of w mod n = 0"
   1.234 +by (simp add: Rep_simps number_of_eq of_int_eq iszero_def zero_def)
   1.235 +
   1.236 +lemma cases:
   1.237 +  assumes 1: "\<And>z. \<lbrakk>(x::'a) = of_int z; 0 \<le> z; z < n\<rbrakk> \<Longrightarrow> P"
   1.238 +  shows "P"
   1.239 +apply (cases x rule: type_definition.Abs_cases [OF type])
   1.240 +apply (rule_tac z="y" in 1)
   1.241 +apply (simp_all add: of_int_eq mod_pos_pos_trivial)
   1.242 +done
   1.243 +
   1.244 +lemma induct:
   1.245 +  "(\<And>z. \<lbrakk>0 \<le> z; z < n\<rbrakk> \<Longrightarrow> P (of_int z)) \<Longrightarrow> P (x::'a)"
   1.246 +by (cases x rule: cases) simp
   1.247 +
   1.248 +end
   1.249 +
   1.250 +
   1.251 +subsection {* Number ring instances *}
   1.252  
   1.253 -lemma card_num0: "CARD (num0) = 0"
   1.254 -  by (simp add: infinite_UNIV_nat card_eq_0_iff type_definition.card [OF type_definition_num0])
   1.255 +text {*
   1.256 +  Unfortunately a number ring instance is not possible for
   1.257 +  @{typ num1}, since 0 and 1 are not distinct.
   1.258 +*}
   1.259 +
   1.260 +instantiation num1 :: "{comm_ring,comm_monoid_mult,number,recpower}"
   1.261 +begin
   1.262 +
   1.263 +lemma num1_eq_iff: "(x::num1) = (y::num1) \<longleftrightarrow> True"
   1.264 +  by (induct x, induct y) simp
   1.265 +
   1.266 +instance proof
   1.267 +qed (simp_all add: num1_eq_iff)
   1.268 +
   1.269 +end
   1.270 +
   1.271 +instantiation
   1.272 +  bit0 and bit1 :: (finite) "{zero,one,plus,times,uminus,minus,power}"
   1.273 +begin
   1.274 +
   1.275 +definition Abs_bit0' :: "int \<Rightarrow> 'a bit0" where
   1.276 +  "Abs_bit0' x = Abs_bit0 (x mod int CARD('a bit0))"
   1.277 +
   1.278 +definition Abs_bit1' :: "int \<Rightarrow> 'a bit1" where
   1.279 +  "Abs_bit1' x = Abs_bit1 (x mod int CARD('a bit1))"
   1.280 +
   1.281 +definition "0 = Abs_bit0 0"
   1.282 +definition "1 = Abs_bit0 1"
   1.283 +definition "x + y = Abs_bit0' (Rep_bit0 x + Rep_bit0 y)"
   1.284 +definition "x * y = Abs_bit0' (Rep_bit0 x * Rep_bit0 y)"
   1.285 +definition "x - y = Abs_bit0' (Rep_bit0 x - Rep_bit0 y)"
   1.286 +definition "- x = Abs_bit0' (- Rep_bit0 x)"
   1.287 +definition "x ^ k = Abs_bit0' (Rep_bit0 x ^ k)"
   1.288 +
   1.289 +definition "0 = Abs_bit1 0"
   1.290 +definition "1 = Abs_bit1 1"
   1.291 +definition "x + y = Abs_bit1' (Rep_bit1 x + Rep_bit1 y)"
   1.292 +definition "x * y = Abs_bit1' (Rep_bit1 x * Rep_bit1 y)"
   1.293 +definition "x - y = Abs_bit1' (Rep_bit1 x - Rep_bit1 y)"
   1.294 +definition "- x = Abs_bit1' (- Rep_bit1 x)"
   1.295 +definition "x ^ k = Abs_bit1' (Rep_bit1 x ^ k)"
   1.296 +
   1.297 +instance ..
   1.298 +
   1.299 +end
   1.300  
   1.301 -lemmas card_univ_simps [simp] =
   1.302 -  card_unit
   1.303 -  card_bool
   1.304 -  card_prod
   1.305 -  card_sum
   1.306 -  card_option
   1.307 -  card_set
   1.308 -  card_num1
   1.309 -  card_bit0
   1.310 -  card_bit1
   1.311 -  card_num0
   1.312 +interpretation bit0!:
   1.313 +  mod_type "int CARD('a::finite bit0)"
   1.314 +           "Rep_bit0 :: 'a::finite bit0 \<Rightarrow> int"
   1.315 +           "Abs_bit0 :: int \<Rightarrow> 'a::finite bit0"
   1.316 +apply (rule mod_type.intro)
   1.317 +apply (simp add: int_mult type_definition_bit0)
   1.318 +apply (rule one_less_int_card)
   1.319 +apply (rule zero_bit0_def)
   1.320 +apply (rule one_bit0_def)
   1.321 +apply (rule plus_bit0_def [unfolded Abs_bit0'_def])
   1.322 +apply (rule times_bit0_def [unfolded Abs_bit0'_def])
   1.323 +apply (rule minus_bit0_def [unfolded Abs_bit0'_def])
   1.324 +apply (rule uminus_bit0_def [unfolded Abs_bit0'_def])
   1.325 +apply (rule power_bit0_def [unfolded Abs_bit0'_def])
   1.326 +done
   1.327 +
   1.328 +interpretation bit1!:
   1.329 +  mod_type "int CARD('a::finite bit1)"
   1.330 +           "Rep_bit1 :: 'a::finite bit1 \<Rightarrow> int"
   1.331 +           "Abs_bit1 :: int \<Rightarrow> 'a::finite bit1"
   1.332 +apply (rule mod_type.intro)
   1.333 +apply (simp add: int_mult type_definition_bit1)
   1.334 +apply (rule one_less_int_card)
   1.335 +apply (rule zero_bit1_def)
   1.336 +apply (rule one_bit1_def)
   1.337 +apply (rule plus_bit1_def [unfolded Abs_bit1'_def])
   1.338 +apply (rule times_bit1_def [unfolded Abs_bit1'_def])
   1.339 +apply (rule minus_bit1_def [unfolded Abs_bit1'_def])
   1.340 +apply (rule uminus_bit1_def [unfolded Abs_bit1'_def])
   1.341 +apply (rule power_bit1_def [unfolded Abs_bit1'_def])
   1.342 +done
   1.343 +
   1.344 +instance bit0 :: (finite) "{comm_ring_1,recpower}"
   1.345 +  by (rule bit0.comm_ring_1 bit0.recpower)+
   1.346 +
   1.347 +instance bit1 :: (finite) "{comm_ring_1,recpower}"
   1.348 +  by (rule bit1.comm_ring_1 bit1.recpower)+
   1.349 +
   1.350 +instantiation bit0 and bit1 :: (finite) number_ring
   1.351 +begin
   1.352 +
   1.353 +definition "(number_of w :: _ bit0) = of_int w"
   1.354 +
   1.355 +definition "(number_of w :: _ bit1) = of_int w"
   1.356 +
   1.357 +instance proof
   1.358 +qed (rule number_of_bit0_def number_of_bit1_def)+
   1.359 +
   1.360 +end
   1.361 +
   1.362 +interpretation bit0!:
   1.363 +  mod_ring "int CARD('a::finite bit0)"
   1.364 +           "Rep_bit0 :: 'a::finite bit0 \<Rightarrow> int"
   1.365 +           "Abs_bit0 :: int \<Rightarrow> 'a::finite bit0"
   1.366 +  ..
   1.367 +
   1.368 +interpretation bit1!:
   1.369 +  mod_ring "int CARD('a::finite bit1)"
   1.370 +           "Rep_bit1 :: 'a::finite bit1 \<Rightarrow> int"
   1.371 +           "Abs_bit1 :: int \<Rightarrow> 'a::finite bit1"
   1.372 +  ..
   1.373 +
   1.374 +text {* Set up cases, induction, and arithmetic *}
   1.375 +
   1.376 +lemmas bit0_cases [case_names of_int, cases type: bit0] = bit0.cases
   1.377 +lemmas bit1_cases [case_names of_int, cases type: bit1] = bit1.cases
   1.378 +
   1.379 +lemmas bit0_induct [case_names of_int, induct type: bit0] = bit0.induct
   1.380 +lemmas bit1_induct [case_names of_int, induct type: bit1] = bit1.induct
   1.381 +
   1.382 +lemmas bit0_iszero_number_of [simp] = bit0.iszero_number_of
   1.383 +lemmas bit1_iszero_number_of [simp] = bit1.iszero_number_of
   1.384 +
   1.385 +declare power_Suc [where ?'a="'a::finite bit0", standard, simp]
   1.386 +declare power_Suc [where ?'a="'a::finite bit1", standard, simp]
   1.387  
   1.388  
   1.389  subsection {* Syntax *}
   1.390 @@ -184,42 +452,10 @@
   1.391  in [("bit0", bit_tr' 0), ("bit1", bit_tr' 1)] end;
   1.392  *}
   1.393  
   1.394 -
   1.395 -subsection {* Classes with at least 1 and 2  *}
   1.396 -
   1.397 -text {* Class finite already captures "at least 1" *}
   1.398 -
   1.399 -lemma zero_less_card_finite [simp]:
   1.400 -  "0 < CARD('a::finite)"
   1.401 -proof (cases "CARD('a::finite) = 0")
   1.402 -  case False thus ?thesis by (simp del: card_0_eq)
   1.403 -next
   1.404 -  case True
   1.405 -  thus ?thesis by (simp add: finite)
   1.406 -qed
   1.407 -
   1.408 -lemma one_le_card_finite [simp]:
   1.409 -  "Suc 0 <= CARD('a::finite)"
   1.410 -  by (simp add: less_Suc_eq_le [symmetric] zero_less_card_finite)
   1.411 -
   1.412 -
   1.413 -text {* Class for cardinality "at least 2" *}
   1.414 -
   1.415 -class card2 = finite + 
   1.416 -  assumes two_le_card: "2 <= CARD('a)"
   1.417 -
   1.418 -lemma one_less_card: "Suc 0 < CARD('a::card2)"
   1.419 -  using two_le_card [where 'a='a] by simp
   1.420 -
   1.421 -instance bit0 :: (finite) card2
   1.422 -  by intro_classes (simp add: one_le_card_finite)
   1.423 -
   1.424 -instance bit1 :: (finite) card2
   1.425 -  by intro_classes (simp add: one_le_card_finite)
   1.426 -
   1.427  subsection {* Examples *}
   1.428  
   1.429  lemma "CARD(0) = 0" by simp
   1.430  lemma "CARD(17) = 17" by simp
   1.431 +lemma "8 * 11 ^ 3 - 6 = (2::5)" by simp
   1.432  
   1.433  end