src/HOL/ex/Word.thy
changeset 71954 13bb3f5cdc5b
parent 71921 a238074c5a9d
child 71956 a4bffc0de967
equal deleted inserted replaced
71953:428609096812 71954:13bb3f5cdc5b
    83 
    83 
    84 subsection \<open>Bit strings as quotient type\<close>
    84 subsection \<open>Bit strings as quotient type\<close>
    85 
    85 
    86 subsubsection \<open>Basic properties\<close>
    86 subsubsection \<open>Basic properties\<close>
    87 
    87 
    88 quotient_type (overloaded) 'a word = int / "\<lambda>k l. take_bit LENGTH('a) k = take_bit LENGTH('a::len0) l"
    88 quotient_type (overloaded) 'a word = int / "\<lambda>k l. take_bit LENGTH('a) k = take_bit LENGTH('a::len) l"
    89   by (auto intro!: equivpI reflpI sympI transpI)
    89   by (auto intro!: equivpI reflpI sympI transpI)
    90 
    90 
    91 instantiation word :: (len0) "{semiring_numeral, comm_semiring_0, comm_ring}"
    91 instantiation word :: (len) "{semiring_numeral, comm_semiring_0, comm_ring}"
    92 begin
    92 begin
    93 
    93 
    94 lift_definition zero_word :: "'a word"
    94 lift_definition zero_word :: "'a word"
    95   is 0
    95   is 0
    96   .
    96   .
   123 instance word :: (len) comm_ring_1
   123 instance word :: (len) comm_ring_1
   124   by standard (transfer; simp)+
   124   by standard (transfer; simp)+
   125 
   125 
   126 quickcheck_generator word
   126 quickcheck_generator word
   127   constructors:
   127   constructors:
   128     "zero_class.zero :: ('a::len0) word",
   128     "zero_class.zero :: ('a::len) word",
   129     "numeral :: num \<Rightarrow> ('a::len0) word",
   129     "numeral :: num \<Rightarrow> ('a::len) word",
   130     "uminus :: ('a::len0) word \<Rightarrow> ('a::len0) word"
   130     "uminus :: ('a::len) word \<Rightarrow> ('a::len) word"
   131 
   131 
   132 context
   132 context
   133   includes lifting_syntax
   133   includes lifting_syntax
   134   notes power_transfer [transfer_rule]
   134   notes power_transfer [transfer_rule]
   135 begin
   135 begin
   155 lemma [transfer_rule]:
   155 lemma [transfer_rule]:
   156   "((=) ===> (pcr_word :: int \<Rightarrow> 'a::len word \<Rightarrow> bool)) of_bool of_bool"
   156   "((=) ===> (pcr_word :: int \<Rightarrow> 'a::len word \<Rightarrow> bool)) of_bool of_bool"
   157   by transfer_prover
   157   by transfer_prover
   158 
   158 
   159 lemma [transfer_rule]:
   159 lemma [transfer_rule]:
   160   "((=) ===> (pcr_word :: int \<Rightarrow> 'a::len0 word \<Rightarrow> bool)) numeral numeral"
   160   "((=) ===> (pcr_word :: int \<Rightarrow> 'a::len word \<Rightarrow> bool)) numeral numeral"
   161   by transfer_prover
   161   by transfer_prover
   162 
   162 
   163 lemma [transfer_rule]:
   163 lemma [transfer_rule]:
   164   "((=) ===> pcr_word) int of_nat"
   164   "((=) ===> pcr_word) int of_nat"
   165   by transfer_prover
   165   by transfer_prover
   179   by (rule ext) (transfer, rule)
   179   by (rule ext) (transfer, rule)
   180 
   180 
   181 context semiring_1
   181 context semiring_1
   182 begin
   182 begin
   183 
   183 
   184 lift_definition unsigned :: "'b::len0 word \<Rightarrow> 'a"
   184 lift_definition unsigned :: "'b::len word \<Rightarrow> 'a"
   185   is "of_nat \<circ> nat \<circ> take_bit LENGTH('b)"
   185   is "of_nat \<circ> nat \<circ> take_bit LENGTH('b)"
   186   by simp
   186   by simp
   187 
   187 
   188 lemma unsigned_0 [simp]:
   188 lemma unsigned_0 [simp]:
   189   "unsigned 0 = 0"
   189   "unsigned 0 = 0"
   198   "a = b \<longleftrightarrow> unsigned a = unsigned b"
   198   "a = b \<longleftrightarrow> unsigned a = unsigned b"
   199   by safe (transfer; simp add: eq_nat_nat_iff)
   199   by safe (transfer; simp add: eq_nat_nat_iff)
   200 
   200 
   201 end
   201 end
   202 
   202 
   203 instantiation word :: (len0) equal
   203 instantiation word :: (len) equal
   204 begin
   204 begin
   205 
   205 
   206 definition equal_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> bool"
   206 definition equal_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> bool"
   207   where "equal_word a b \<longleftrightarrow> (unsigned a :: int) = unsigned b"
   207   where "equal_word a b \<longleftrightarrow> (unsigned a :: int) = unsigned b"
   208 
   208 
   238 lemma of_int_unsigned [simp]:
   238 lemma of_int_unsigned [simp]:
   239   "of_int (unsigned a) = a"
   239   "of_int (unsigned a) = a"
   240   by transfer simp
   240   by transfer simp
   241 
   241 
   242 lemma unsigned_nat_less:
   242 lemma unsigned_nat_less:
   243   \<open>unsigned a < (2 ^ LENGTH('a) :: nat)\<close> for a :: \<open>'a::len0 word\<close>
   243   \<open>unsigned a < (2 ^ LENGTH('a) :: nat)\<close> for a :: \<open>'a::len word\<close>
   244   by transfer (simp add: take_bit_eq_mod)
   244   by transfer (simp add: take_bit_eq_mod)
   245 
   245 
   246 lemma unsigned_int_less:
   246 lemma unsigned_int_less:
   247   \<open>unsigned a < (2 ^ LENGTH('a) :: int)\<close> for a :: \<open>'a::len0 word\<close>
   247   \<open>unsigned a < (2 ^ LENGTH('a) :: int)\<close> for a :: \<open>'a::len word\<close>
   248   by transfer (simp add: take_bit_eq_mod)
   248   by transfer (simp add: take_bit_eq_mod)
   249 
   249 
   250 context ring_char_0
   250 context ring_char_0
   251 begin
   251 begin
   252 
   252 
   272   by transfer simp
   272   by transfer simp
   273 
   273 
   274 
   274 
   275 subsubsection \<open>Division\<close>
   275 subsubsection \<open>Division\<close>
   276 
   276 
   277 instantiation word :: (len0) modulo
   277 instantiation word :: (len) modulo
   278 begin
   278 begin
   279 
   279 
   280 lift_definition divide_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word"
   280 lift_definition divide_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word"
   281   is "\<lambda>a b. take_bit LENGTH('a) a div take_bit LENGTH('a) b"
   281   is "\<lambda>a b. take_bit LENGTH('a) a div take_bit LENGTH('a) b"
   282   by simp
   282   by simp
   288 instance ..
   288 instance ..
   289 
   289 
   290 end
   290 end
   291 
   291 
   292 lemma zero_word_div_eq [simp]:
   292 lemma zero_word_div_eq [simp]:
   293   \<open>0 div a = 0\<close> for a :: \<open>'a::len0 word\<close>
   293   \<open>0 div a = 0\<close> for a :: \<open>'a::len word\<close>
   294   by transfer simp
   294   by transfer simp
   295 
   295 
   296 lemma div_zero_word_eq [simp]:
   296 lemma div_zero_word_eq [simp]:
   297   \<open>a div 0 = 0\<close> for a :: \<open>'a::len0 word\<close>
   297   \<open>a div 0 = 0\<close> for a :: \<open>'a::len word\<close>
   298   by transfer simp
   298   by transfer simp
   299 
   299 
   300 context
   300 context
   301   includes lifting_syntax
   301   includes lifting_syntax
   302 begin
   302 begin
   363 qed
   363 qed
   364 
   364 
   365 
   365 
   366 subsubsection \<open>Orderings\<close>
   366 subsubsection \<open>Orderings\<close>
   367 
   367 
   368 instantiation word :: (len0) linorder
   368 instantiation word :: (len) linorder
   369 begin
   369 begin
   370 
   370 
   371 lift_definition less_eq_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> bool"
   371 lift_definition less_eq_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> bool"
   372   is "\<lambda>a b. take_bit LENGTH('a) a \<le> take_bit LENGTH('a) b"
   372   is "\<lambda>a b. take_bit LENGTH('a) a \<le> take_bit LENGTH('a) b"
   373   by simp
   373   by simp
   393   by (transfer fixing: less) (auto dest: preorder_class.le_less_trans [OF take_bit_nonnegative])
   393   by (transfer fixing: less) (auto dest: preorder_class.le_less_trans [OF take_bit_nonnegative])
   394 
   394 
   395 end
   395 end
   396 
   396 
   397 lemma word_greater_zero_iff:
   397 lemma word_greater_zero_iff:
   398   \<open>a > 0 \<longleftrightarrow> a \<noteq> 0\<close> for a :: \<open>'a::len0 word\<close>
   398   \<open>a > 0 \<longleftrightarrow> a \<noteq> 0\<close> for a :: \<open>'a::len word\<close>
   399   by transfer (simp add: less_le)
   399   by transfer (simp add: less_le)
   400 
   400 
   401 lemma of_nat_word_eq_iff:
   401 lemma of_nat_word_eq_iff:
   402   \<open>of_nat m = (of_nat n :: 'a::len word) \<longleftrightarrow> take_bit LENGTH('a) m = take_bit LENGTH('a) n\<close>
   402   \<open>of_nat m = (of_nat n :: 'a::len word) \<longleftrightarrow> take_bit LENGTH('a) m = take_bit LENGTH('a) n\<close>
   403   by transfer (simp add: take_bit_of_nat)
   403   by transfer (simp add: take_bit_of_nat)