src/HOL/Word/Word.thy
changeset 47372 9ab4e22dac7b
parent 47168 8395d7d63fc8
child 47374 9475d524bafb
equal deleted inserted replaced
47371:4193098c4ec1 47372:9ab4e22dac7b
   225   fix x :: "'a word"
   225   fix x :: "'a word"
   226   assume "\<And>x. PROP P (word_of_int x)"
   226   assume "\<And>x. PROP P (word_of_int x)"
   227   hence "PROP P (word_of_int (uint x))" .
   227   hence "PROP P (word_of_int (uint x))" .
   228   thus "PROP P x" by simp
   228   thus "PROP P x" by simp
   229 qed
   229 qed
       
   230 
       
   231 subsection {* Correspondence relation for theorem transfer *}
       
   232 
       
   233 definition cr_word :: "int \<Rightarrow> 'a::len0 word \<Rightarrow> bool"
       
   234   where "cr_word \<equiv> (\<lambda>x y. word_of_int x = y)"
       
   235 
       
   236 lemma bi_total_cr_word [transfer_rule]: "bi_total cr_word"
       
   237   unfolding bi_total_def cr_word_def
       
   238   by (auto intro: word_of_int_uint)
       
   239 
       
   240 lemma right_unique_cr_word [transfer_rule]: "right_unique cr_word"
       
   241   unfolding right_unique_def cr_word_def by simp
       
   242 
       
   243 lemma word_eq_transfer [transfer_rule]:
       
   244   "(fun_rel cr_word (fun_rel cr_word op =))
       
   245     (\<lambda>x y. bintrunc (len_of TYPE('a)) x = bintrunc (len_of TYPE('a)) y)
       
   246     (op = :: 'a::len0 word \<Rightarrow> 'a word \<Rightarrow> bool)"
       
   247   unfolding fun_rel_def cr_word_def
       
   248   by (simp add: word_ubin.norm_eq_iff)
       
   249 
       
   250 lemma word_of_int_transfer [transfer_rule]:
       
   251   "(fun_rel op = cr_word) (\<lambda>x. x) word_of_int"
       
   252   unfolding fun_rel_def cr_word_def by simp
       
   253 
       
   254 lemma uint_transfer [transfer_rule]:
       
   255   "(fun_rel cr_word op =) (bintrunc (len_of TYPE('a)))
       
   256     (uint :: 'a::len0 word \<Rightarrow> int)"
       
   257   unfolding fun_rel_def cr_word_def by (simp add: word_ubin.eq_norm)
   230 
   258 
   231 subsection  "Arithmetic operations"
   259 subsection  "Arithmetic operations"
   232 
   260 
   233 definition
   261 definition
   234   word_succ :: "'a :: len0 word => 'a word"
   262   word_succ :: "'a :: len0 word => 'a word"
   288 
   316 
   289 lemmas word_of_int_homs = wi_homs word_0_wi word_1_wi
   317 lemmas word_of_int_homs = wi_homs word_0_wi word_1_wi
   290 
   318 
   291 lemmas word_of_int_hom_syms = word_of_int_homs [symmetric]
   319 lemmas word_of_int_hom_syms = word_of_int_homs [symmetric]
   292 
   320 
       
   321 lemma word_arith_transfer [transfer_rule]:
       
   322   "cr_word 0 0"
       
   323   "cr_word 1 1"
       
   324   "(fun_rel cr_word (fun_rel cr_word cr_word)) (op +) (op +)"
       
   325   "(fun_rel cr_word (fun_rel cr_word cr_word)) (op -) (op -)"
       
   326   "(fun_rel cr_word (fun_rel cr_word cr_word)) (op *) (op *)"
       
   327   "(fun_rel cr_word cr_word) uminus uminus"
       
   328   "(fun_rel cr_word cr_word) (\<lambda>x. x + 1) word_succ"
       
   329   "(fun_rel cr_word cr_word) (\<lambda>x. x - 1) word_pred"
       
   330   unfolding cr_word_def fun_rel_def by (simp_all add: word_of_int_homs)
       
   331 
   293 instance
   332 instance
   294   by default (auto simp: split_word_all word_of_int_homs algebra_simps)
   333   by default (transfer, simp add: algebra_simps)+
   295 
   334 
   296 end
   335 end
   297 
   336 
   298 instance word :: (len) comm_ring_1
   337 instance word :: (len) comm_ring_1
   299 proof
   338 proof
   300   have "0 < len_of TYPE('a)" by (rule len_gt_0)
   339   have "0 < len_of TYPE('a)" by (rule len_gt_0)
   301   then show "(0::'a word) \<noteq> 1"
   340   then show "(0::'a word) \<noteq> 1"
   302     unfolding word_0_wi word_1_wi
   341     by - (transfer, auto simp add: gr0_conv_Suc)
   303     by (auto simp add: word_ubin.norm_eq_iff [symmetric] gr0_conv_Suc)
       
   304 qed
   342 qed
   305 
   343 
   306 lemma word_of_nat: "of_nat n = word_of_int (int n)"
   344 lemma word_of_nat: "of_nat n = word_of_int (int n)"
   307   by (induct n) (auto simp add : word_of_int_hom_syms)
   345   by (induct n) (auto simp add : word_of_int_hom_syms)
   308 
   346 
   564 lemma word_neg_numeral_alt:
   602 lemma word_neg_numeral_alt:
   565   "neg_numeral b = word_of_int (neg_numeral b)"
   603   "neg_numeral b = word_of_int (neg_numeral b)"
   566   by (simp only: neg_numeral_def word_numeral_alt wi_hom_neg)
   604   by (simp only: neg_numeral_def word_numeral_alt wi_hom_neg)
   567 
   605 
   568 declare word_neg_numeral_alt [symmetric, code_abbrev]
   606 declare word_neg_numeral_alt [symmetric, code_abbrev]
       
   607 
       
   608 lemma word_numeral_transfer [transfer_rule]:
       
   609   "(fun_rel op = cr_word) numeral numeral"
       
   610   "(fun_rel op = cr_word) neg_numeral neg_numeral"
       
   611   unfolding fun_rel_def cr_word_def word_numeral_alt word_neg_numeral_alt
       
   612   by simp_all
   569 
   613 
   570 lemma uint_bintrunc [simp]:
   614 lemma uint_bintrunc [simp]:
   571   "uint (numeral bin :: 'a word) = 
   615   "uint (numeral bin :: 'a word) = 
   572     bintrunc (len_of TYPE ('a :: len0)) (numeral bin)"
   616     bintrunc (len_of TYPE ('a :: len0)) (numeral bin)"
   573   unfolding word_numeral_alt by (rule word_ubin.eq_norm)
   617   unfolding word_numeral_alt by (rule word_ubin.eq_norm)
  2118   "word_of_int a AND word_of_int b = word_of_int (a AND b)"
  2162   "word_of_int a AND word_of_int b = word_of_int (a AND b)"
  2119   "word_of_int a OR word_of_int b = word_of_int (a OR b)"
  2163   "word_of_int a OR word_of_int b = word_of_int (a OR b)"
  2120   "word_of_int a XOR word_of_int b = word_of_int (a XOR b)"
  2164   "word_of_int a XOR word_of_int b = word_of_int (a XOR b)"
  2121   unfolding word_log_defs wils1 by simp_all
  2165   unfolding word_log_defs wils1 by simp_all
  2122 
  2166 
       
  2167 lemma word_bitwise_transfer [transfer_rule]:
       
  2168   "(fun_rel cr_word cr_word) bitNOT bitNOT"
       
  2169   "(fun_rel cr_word (fun_rel cr_word cr_word)) bitAND bitAND"
       
  2170   "(fun_rel cr_word (fun_rel cr_word cr_word)) bitOR bitOR"
       
  2171   "(fun_rel cr_word (fun_rel cr_word cr_word)) bitXOR bitXOR"
       
  2172   unfolding fun_rel_def cr_word_def
       
  2173   by (simp_all add: word_wi_log_defs)
       
  2174 
  2123 lemma word_no_log_defs [simp]:
  2175 lemma word_no_log_defs [simp]:
  2124   "NOT (numeral a) = word_of_int (NOT (numeral a))"
  2176   "NOT (numeral a) = word_of_int (NOT (numeral a))"
  2125   "NOT (neg_numeral a) = word_of_int (NOT (neg_numeral a))"
  2177   "NOT (neg_numeral a) = word_of_int (NOT (neg_numeral a))"
  2126   "numeral a AND numeral b = word_of_int (numeral a AND numeral b)"
  2178   "numeral a AND numeral b = word_of_int (numeral a AND numeral b)"
  2127   "numeral a AND neg_numeral b = word_of_int (numeral a AND neg_numeral b)"
  2179   "numeral a AND neg_numeral b = word_of_int (numeral a AND neg_numeral b)"
  2133   "neg_numeral a OR neg_numeral b = word_of_int (neg_numeral a OR neg_numeral b)"
  2185   "neg_numeral a OR neg_numeral b = word_of_int (neg_numeral a OR neg_numeral b)"
  2134   "numeral a XOR numeral b = word_of_int (numeral a XOR numeral b)"
  2186   "numeral a XOR numeral b = word_of_int (numeral a XOR numeral b)"
  2135   "numeral a XOR neg_numeral b = word_of_int (numeral a XOR neg_numeral b)"
  2187   "numeral a XOR neg_numeral b = word_of_int (numeral a XOR neg_numeral b)"
  2136   "neg_numeral a XOR numeral b = word_of_int (neg_numeral a XOR numeral b)"
  2188   "neg_numeral a XOR numeral b = word_of_int (neg_numeral a XOR numeral b)"
  2137   "neg_numeral a XOR neg_numeral b = word_of_int (neg_numeral a XOR neg_numeral b)"
  2189   "neg_numeral a XOR neg_numeral b = word_of_int (neg_numeral a XOR neg_numeral b)"
  2138   unfolding word_numeral_alt word_neg_numeral_alt word_wi_log_defs by simp_all
  2190   by (transfer, rule refl)+
  2139 
  2191 
  2140 text {* Special cases for when one of the arguments equals 1. *}
  2192 text {* Special cases for when one of the arguments equals 1. *}
  2141 
  2193 
  2142 lemma word_bitwise_1_simps [simp]:
  2194 lemma word_bitwise_1_simps [simp]:
  2143   "NOT (1::'a::len0 word) = -2"
  2195   "NOT (1::'a::len0 word) = -2"
  2151   "neg_numeral a OR 1 = word_of_int (neg_numeral a OR 1)"
  2203   "neg_numeral a OR 1 = word_of_int (neg_numeral a OR 1)"
  2152   "1 XOR numeral b = word_of_int (1 XOR numeral b)"
  2204   "1 XOR numeral b = word_of_int (1 XOR numeral b)"
  2153   "1 XOR neg_numeral b = word_of_int (1 XOR neg_numeral b)"
  2205   "1 XOR neg_numeral b = word_of_int (1 XOR neg_numeral b)"
  2154   "numeral a XOR 1 = word_of_int (numeral a XOR 1)"
  2206   "numeral a XOR 1 = word_of_int (numeral a XOR 1)"
  2155   "neg_numeral a XOR 1 = word_of_int (neg_numeral a XOR 1)"
  2207   "neg_numeral a XOR 1 = word_of_int (neg_numeral a XOR 1)"
  2156   unfolding word_1_no word_no_log_defs by simp_all
  2208   by (transfer, simp)+
  2157 
  2209 
  2158 lemma uint_or: "uint (x OR y) = (uint x) OR (uint y)"
  2210 lemma uint_or: "uint (x OR y) = (uint x) OR (uint y)"
  2159   by (simp add: word_or_def word_wi_log_defs word_ubin.eq_norm
  2211   by (transfer, simp add: bin_trunc_ao)
  2160                 bin_trunc_ao(2) [symmetric])
       
  2161 
  2212 
  2162 lemma uint_and: "uint (x AND y) = (uint x) AND (uint y)"
  2213 lemma uint_and: "uint (x AND y) = (uint x) AND (uint y)"
  2163   by (simp add: word_and_def word_wi_log_defs word_ubin.eq_norm
  2214   by (transfer, simp add: bin_trunc_ao)
  2164                 bin_trunc_ao(1) [symmetric]) 
  2215 
       
  2216 lemma test_bit_wi [simp]:
       
  2217   "(word_of_int x::'a::len0 word) !! n \<longleftrightarrow> n < len_of TYPE('a) \<and> bin_nth x n"
       
  2218   unfolding word_test_bit_def
       
  2219   by (simp add: word_ubin.eq_norm nth_bintr)
       
  2220 
       
  2221 lemma word_test_bit_transfer [transfer_rule]:
       
  2222   "(fun_rel cr_word (fun_rel op = op =))
       
  2223     (\<lambda>x n. n < len_of TYPE('a) \<and> bin_nth x n) (test_bit :: 'a::len0 word \<Rightarrow> _)"
       
  2224   unfolding fun_rel_def cr_word_def by simp
  2165 
  2225 
  2166 lemma word_ops_nth_size:
  2226 lemma word_ops_nth_size:
  2167   "n < size (x::'a::len0 word) \<Longrightarrow> 
  2227   "n < size (x::'a::len0 word) \<Longrightarrow> 
  2168     (x OR y) !! n = (x !! n | y !! n) & 
  2228     (x OR y) !! n = (x !! n | y !! n) & 
  2169     (x AND y) !! n = (x !! n & y !! n) & 
  2229     (x AND y) !! n = (x !! n & y !! n) & 
  2170     (x XOR y) !! n = (x !! n ~= y !! n) & 
  2230     (x XOR y) !! n = (x !! n ~= y !! n) & 
  2171     (NOT x) !! n = (~ x !! n)"
  2231     (NOT x) !! n = (~ x !! n)"
  2172   unfolding word_size word_test_bit_def word_log_defs
  2232   unfolding word_size by transfer (simp add: bin_nth_ops)
  2173   by (clarsimp simp add : word_ubin.eq_norm nth_bintr bin_nth_ops)
       
  2174 
  2233 
  2175 lemma word_ao_nth:
  2234 lemma word_ao_nth:
  2176   fixes x :: "'a::len0 word"
  2235   fixes x :: "'a::len0 word"
  2177   shows "(x OR y) !! n = (x !! n | y !! n) & 
  2236   shows "(x OR y) !! n = (x !! n | y !! n) & 
  2178          (x AND y) !! n = (x !! n & y !! n)"
  2237          (x AND y) !! n = (x !! n & y !! n)"
  2179   apply (cases "n < size x")
  2238   by transfer (auto simp add: bin_nth_ops)
  2180    apply (drule_tac y = "y" in word_ops_nth_size)
       
  2181    apply simp
       
  2182   apply (simp add : test_bit_bin word_size)
       
  2183   done
       
  2184 
       
  2185 lemma test_bit_wi [simp]:
       
  2186   "(word_of_int x::'a::len0 word) !! n \<longleftrightarrow> n < len_of TYPE('a) \<and> bin_nth x n"
       
  2187   unfolding word_test_bit_def
       
  2188   by (simp add: nth_bintr [symmetric] word_ubin.eq_norm)
       
  2189 
  2239 
  2190 lemma test_bit_numeral [simp]:
  2240 lemma test_bit_numeral [simp]:
  2191   "(numeral w :: 'a::len0 word) !! n \<longleftrightarrow>
  2241   "(numeral w :: 'a::len0 word) !! n \<longleftrightarrow>
  2192     n < len_of TYPE('a) \<and> bin_nth (numeral w) n"
  2242     n < len_of TYPE('a) \<and> bin_nth (numeral w) n"
  2193   unfolding word_numeral_alt test_bit_wi ..
  2243   by transfer (rule refl)
  2194 
  2244 
  2195 lemma test_bit_neg_numeral [simp]:
  2245 lemma test_bit_neg_numeral [simp]:
  2196   "(neg_numeral w :: 'a::len0 word) !! n \<longleftrightarrow>
  2246   "(neg_numeral w :: 'a::len0 word) !! n \<longleftrightarrow>
  2197     n < len_of TYPE('a) \<and> bin_nth (neg_numeral w) n"
  2247     n < len_of TYPE('a) \<and> bin_nth (neg_numeral w) n"
  2198   unfolding word_neg_numeral_alt test_bit_wi ..
  2248   by transfer (rule refl)
  2199 
  2249 
  2200 lemma test_bit_1 [simp]: "(1::'a::len word) !! n \<longleftrightarrow> n = 0"
  2250 lemma test_bit_1 [simp]: "(1::'a::len word) !! n \<longleftrightarrow> n = 0"
  2201   unfolding word_1_wi test_bit_wi by auto
  2251   by transfer auto
  2202   
  2252   
  2203 lemma nth_0 [simp]: "~ (0::'a::len0 word) !! n"
  2253 lemma nth_0 [simp]: "~ (0::'a::len0 word) !! n"
  2204   unfolding word_test_bit_def by simp
  2254   by transfer simp
  2205 
  2255 
  2206 lemma nth_minus1 [simp]: "(-1::'a::len0 word) !! n \<longleftrightarrow> n < len_of TYPE('a)"
  2256 lemma nth_minus1 [simp]: "(-1::'a::len0 word) !! n \<longleftrightarrow> n < len_of TYPE('a)"
  2207   unfolding word_test_bit_def by (simp add: nth_bintr)
  2257   by transfer simp
  2208 
  2258 
  2209 (* get from commutativity, associativity etc of int_and etc
  2259 (* get from commutativity, associativity etc of int_and etc
  2210   to same for word_and etc *)
  2260   to same for word_and etc *)
  2211 
  2261 
  2212 lemmas bwsimps = 
  2262 lemmas bwsimps = 
  2297   by (auto simp: word_eq_iff word_ops_nth_size [unfolded word_size])
  2347   by (auto simp: word_eq_iff word_ops_nth_size [unfolded word_size])
  2298 
  2348 
  2299 lemma word_add_not [simp]: 
  2349 lemma word_add_not [simp]: 
  2300   fixes x :: "'a::len0 word"
  2350   fixes x :: "'a::len0 word"
  2301   shows "x + NOT x = -1"
  2351   shows "x + NOT x = -1"
  2302   using word_of_int_Ex [where x=x] 
  2352   by transfer (simp add: bin_add_not)
  2303   by (auto simp: bwsimps bin_add_not [unfolded Min_def])
       
  2304 
  2353 
  2305 lemma word_plus_and_or [simp]:
  2354 lemma word_plus_and_or [simp]:
  2306   fixes x :: "'a::len0 word"
  2355   fixes x :: "'a::len0 word"
  2307   shows "(x AND y) + (x OR y) = x + y"
  2356   shows "(x AND y) + (x OR y) = x + y"
  2308   using word_of_int_Ex [where x=x] 
  2357   by transfer (simp add: plus_and_or)
  2309         word_of_int_Ex [where x=y] 
       
  2310   by (auto simp: bwsimps plus_and_or)
       
  2311 
  2358 
  2312 lemma leoa:   
  2359 lemma leoa:   
  2313   fixes x :: "'a::len0 word"
  2360   fixes x :: "'a::len0 word"
  2314   shows "(w = (x OR y)) \<Longrightarrow> (y = (w AND y))" by auto
  2361   shows "(w = (x OR y)) \<Longrightarrow> (y = (w AND y))" by auto
  2315 lemma leao: 
  2362 lemma leao: