src/HOL/Word/Misc_Typedef.thy
changeset 72292 4a58c38b85ff
parent 71942 d2654b30f7bd
child 72397 48013583e8e6
equal deleted inserted replaced
72291:ccc104786829 72292:4a58c38b85ff
     5 *)
     5 *)
     6 
     6 
     7 section \<open>Type Definition Theorems\<close>
     7 section \<open>Type Definition Theorems\<close>
     8 
     8 
     9 theory Misc_Typedef
     9 theory Misc_Typedef
    10   imports Main
    10   imports Main Word
    11 begin
    11 begin
    12 
    12 
    13 section "More lemmas about normal type definitions"
    13 subsection "More lemmas about normal type definitions"
    14 
    14 
    15 lemma tdD1: "type_definition Rep Abs A \<Longrightarrow> \<forall>x. Rep x \<in> A"
    15 lemma tdD1: "type_definition Rep Abs A \<Longrightarrow> \<forall>x. Rep x \<in> A"
    16   and tdD2: "type_definition Rep Abs A \<Longrightarrow> \<forall>x. Abs (Rep x) = x"
    16   and tdD2: "type_definition Rep Abs A \<Longrightarrow> \<forall>x. Abs (Rep x) = x"
    17   and tdD3: "type_definition Rep Abs A \<Longrightarrow> \<forall>y. y \<in> A \<longrightarrow> Rep (Abs y) = y"
    17   and tdD3: "type_definition Rep Abs A \<Longrightarrow> \<forall>y. y \<in> A \<longrightarrow> Rep (Abs y) = y"
    18   by (auto simp: type_definition_def)
    18   by (auto simp: type_definition_def)
   195 end
   195 end
   196 
   196 
   197 lemmas td_ext_def' =
   197 lemmas td_ext_def' =
   198   td_ext_def [unfolded type_definition_def td_ext_axioms_def]
   198   td_ext_def [unfolded type_definition_def td_ext_axioms_def]
   199 
   199 
       
   200 
       
   201 subsection \<open>Type-definition locale instantiations\<close>
       
   202 
       
   203 definition uints :: "nat \<Rightarrow> int set"
       
   204   \<comment> \<open>the sets of integers representing the words\<close>
       
   205   where "uints n = range (take_bit n)"
       
   206 
       
   207 definition sints :: "nat \<Rightarrow> int set"
       
   208   where "sints n = range (signed_take_bit (n - 1))"
       
   209 
       
   210 lemma uints_num: "uints n = {i. 0 \<le> i \<and> i < 2 ^ n}"
       
   211   by (simp add: uints_def range_bintrunc)
       
   212 
       
   213 lemma sints_num: "sints n = {i. - (2 ^ (n - 1)) \<le> i \<and> i < 2 ^ (n - 1)}"
       
   214   by (simp add: sints_def range_sbintrunc)
       
   215 
       
   216 definition unats :: "nat \<Rightarrow> nat set"
       
   217   where "unats n = {i. i < 2 ^ n}"
       
   218 
       
   219 \<comment> \<open>naturals\<close>
       
   220 lemma uints_unats: "uints n = int ` unats n"
       
   221   apply (unfold unats_def uints_num)
       
   222   apply safe
       
   223     apply (rule_tac image_eqI)
       
   224      apply (erule_tac nat_0_le [symmetric])
       
   225   by auto
       
   226 
       
   227 lemma unats_uints: "unats n = nat ` uints n"
       
   228   by (auto simp: uints_unats image_iff)
       
   229 
       
   230 lemma td_ext_uint:
       
   231   "td_ext (uint :: 'a word \<Rightarrow> int) word_of_int (uints (LENGTH('a::len)))
       
   232     (\<lambda>w::int. w mod 2 ^ LENGTH('a))"
       
   233   apply (unfold td_ext_def')
       
   234   apply transfer
       
   235   apply (simp add: uints_num take_bit_eq_mod)
       
   236   done
       
   237 
       
   238 interpretation word_uint:
       
   239   td_ext
       
   240     "uint::'a::len word \<Rightarrow> int"
       
   241     word_of_int
       
   242     "uints (LENGTH('a::len))"
       
   243     "\<lambda>w. w mod 2 ^ LENGTH('a::len)"
       
   244   by (fact td_ext_uint)
       
   245 
       
   246 lemmas td_uint = word_uint.td_thm
       
   247 lemmas int_word_uint = word_uint.eq_norm
       
   248 
       
   249 lemma td_ext_ubin:
       
   250   "td_ext (uint :: 'a word \<Rightarrow> int) word_of_int (uints (LENGTH('a::len)))
       
   251     (take_bit (LENGTH('a)))"
       
   252   apply standard
       
   253   apply transfer
       
   254   apply simp
       
   255   done
       
   256 
       
   257 interpretation word_ubin:
       
   258   td_ext
       
   259     "uint::'a::len word \<Rightarrow> int"
       
   260     word_of_int
       
   261     "uints (LENGTH('a::len))"
       
   262     "take_bit (LENGTH('a::len))"
       
   263   by (fact td_ext_ubin)
       
   264 
       
   265 lemma td_ext_unat [OF refl]:
       
   266   "n = LENGTH('a::len) \<Longrightarrow>
       
   267     td_ext (unat :: 'a word \<Rightarrow> nat) of_nat (unats n) (\<lambda>i. i mod 2 ^ n)"
       
   268   apply (standard; transfer)
       
   269      apply (simp_all add: unats_def take_bit_of_nat take_bit_nat_eq_self_iff
       
   270       flip: take_bit_eq_mod)
       
   271   done
       
   272 
       
   273 lemmas unat_of_nat = td_ext_unat [THEN td_ext.eq_norm]
       
   274 
       
   275 interpretation word_unat:
       
   276   td_ext
       
   277     "unat::'a::len word \<Rightarrow> nat"
       
   278     of_nat
       
   279     "unats (LENGTH('a::len))"
       
   280     "\<lambda>i. i mod 2 ^ LENGTH('a::len)"
       
   281   by (rule td_ext_unat)
       
   282 
       
   283 lemmas td_unat = word_unat.td_thm
       
   284 
       
   285 lemmas unat_lt2p [iff] = word_unat.Rep [unfolded unats_def mem_Collect_eq]
       
   286 
       
   287 lemma unat_le: "y \<le> unat z \<Longrightarrow> y \<in> unats (LENGTH('a))"
       
   288   for z :: "'a::len word"
       
   289   apply (unfold unats_def)
       
   290   apply clarsimp
       
   291   apply (rule xtrans, rule unat_lt2p, assumption)
       
   292   done
       
   293 
       
   294 lemma td_ext_sbin:
       
   295   "td_ext (sint :: 'a word \<Rightarrow> int) word_of_int (sints (LENGTH('a::len)))
       
   296     (signed_take_bit (LENGTH('a) - 1))"
       
   297   by (standard; transfer) (auto simp add: sints_def)
       
   298 
       
   299 lemma td_ext_sint:
       
   300   "td_ext (sint :: 'a word \<Rightarrow> int) word_of_int (sints (LENGTH('a::len)))
       
   301      (\<lambda>w. (w + 2 ^ (LENGTH('a) - 1)) mod 2 ^ LENGTH('a) -
       
   302          2 ^ (LENGTH('a) - 1))"
       
   303   using td_ext_sbin [where ?'a = 'a] by (simp add: no_sbintr_alt2)
       
   304 
       
   305 text \<open>
       
   306   We do \<open>sint\<close> before \<open>sbin\<close>, before \<open>sint\<close> is the user version
       
   307   and interpretations do not produce thm duplicates. I.e.
       
   308   we get the name \<open>word_sint.Rep_eqD\<close>, but not \<open>word_sbin.Req_eqD\<close>,
       
   309   because the latter is the same thm as the former.
       
   310 \<close>
       
   311 interpretation word_sint:
       
   312   td_ext
       
   313     "sint ::'a::len word \<Rightarrow> int"
       
   314     word_of_int
       
   315     "sints (LENGTH('a::len))"
       
   316     "\<lambda>w. (w + 2^(LENGTH('a::len) - 1)) mod 2^LENGTH('a::len) -
       
   317       2 ^ (LENGTH('a::len) - 1)"
       
   318   by (rule td_ext_sint)
       
   319 
       
   320 interpretation word_sbin:
       
   321   td_ext
       
   322     "sint ::'a::len word \<Rightarrow> int"
       
   323     word_of_int
       
   324     "sints (LENGTH('a::len))"
       
   325     "signed_take_bit (LENGTH('a::len) - 1)"
       
   326   by (rule td_ext_sbin)
       
   327 
       
   328 lemmas int_word_sint = td_ext_sint [THEN td_ext.eq_norm]
       
   329 
       
   330 lemmas td_sint = word_sint.td
       
   331 
       
   332 lemma uints_mod: "uints n = range (\<lambda>w. w mod 2 ^ n)"
       
   333   by (fact uints_def [unfolded no_bintr_alt1])
       
   334 
       
   335 lemmas uint_range' = word_uint.Rep [unfolded uints_num mem_Collect_eq]
       
   336 lemmas sint_range' = word_sint.Rep [unfolded One_nat_def sints_num mem_Collect_eq]
       
   337 
       
   338 lemmas bintr_num =
       
   339   word_ubin.norm_eq_iff [of "numeral a" "numeral b", symmetric, folded word_numeral_alt] for a b
       
   340 lemmas sbintr_num =
       
   341   word_sbin.norm_eq_iff [of "numeral a" "numeral b", symmetric, folded word_numeral_alt] for a b
       
   342 
       
   343 lemmas uint_div_alt = word_div_def [THEN trans [OF uint_cong int_word_uint]]
       
   344 lemmas uint_mod_alt = word_mod_def [THEN trans [OF uint_cong int_word_uint]]
       
   345 
       
   346 interpretation test_bit:
       
   347   td_ext
       
   348     "(!!) :: 'a::len word \<Rightarrow> nat \<Rightarrow> bool"
       
   349     set_bits
       
   350     "{f. \<forall>i. f i \<longrightarrow> i < LENGTH('a::len)}"
       
   351     "(\<lambda>h i. h i \<and> i < LENGTH('a::len))"
       
   352   by standard
       
   353     (auto simp add: test_bit_word_eq bit_imp_le_length bit_set_bits_word_iff set_bits_bit_eq)
       
   354 
       
   355 lemmas td_nth = test_bit.td_thm
       
   356 
   200 end
   357 end
   201