src/HOL/ex/Word.thy
changeset 71965 d45f5d4c41bd
parent 71956 a4bffc0de967
child 71991 8bff286878bf
equal deleted inserted replaced
71964:235173749448 71965:d45f5d4c41bd
   546     n < LENGTH('a) \<and> odd (a div 2 ^ (n - m)))\<close> for a :: \<open>'a::len word\<close>
   546     n < LENGTH('a) \<and> odd (a div 2 ^ (n - m)))\<close> for a :: \<open>'a::len word\<close>
   547   by transfer
   547   by transfer
   548     (auto simp flip: drop_bit_eq_div simp add: even_drop_bit_iff_not_bit bit_take_bit_iff,
   548     (auto simp flip: drop_bit_eq_div simp add: even_drop_bit_iff_not_bit bit_take_bit_iff,
   549       simp_all flip: push_bit_eq_mult add: bit_push_bit_iff_int)
   549       simp_all flip: push_bit_eq_mult add: bit_push_bit_iff_int)
   550 
   550 
   551 instance word :: (len) semiring_bits
   551 instantiation word :: (len) semiring_bits
       
   552 begin
       
   553 
       
   554 lift_definition bit_word :: \<open>'a word \<Rightarrow> nat \<Rightarrow> bool\<close>
       
   555   is \<open>\<lambda>k n. n < LENGTH('a) \<and> bit k n\<close>
   552 proof
   556 proof
       
   557   fix k l :: int and n :: nat
       
   558   assume *: \<open>take_bit LENGTH('a) k = take_bit LENGTH('a) l\<close>
       
   559   show \<open>n < LENGTH('a) \<and> bit k n \<longleftrightarrow> n < LENGTH('a) \<and> bit l n\<close>
       
   560   proof (cases \<open>n < LENGTH('a)\<close>)
       
   561     case True
       
   562     from * have \<open>bit (take_bit LENGTH('a) k) n \<longleftrightarrow> bit (take_bit LENGTH('a) l) n\<close>
       
   563       by simp
       
   564     then show ?thesis
       
   565       by (simp add: bit_take_bit_iff)
       
   566   next
       
   567     case False
       
   568     then show ?thesis
       
   569       by simp
       
   570   qed
       
   571 qed
       
   572 
       
   573 instance proof
   553   show \<open>P a\<close> if stable: \<open>\<And>a. a div 2 = a \<Longrightarrow> P a\<close>
   574   show \<open>P a\<close> if stable: \<open>\<And>a. a div 2 = a \<Longrightarrow> P a\<close>
   554     and rec: \<open>\<And>a b. P a \<Longrightarrow> (of_bool b + 2 * a) div 2 = a \<Longrightarrow> P (of_bool b + 2 * a)\<close>
   575     and rec: \<open>\<And>a b. P a \<Longrightarrow> (of_bool b + 2 * a) div 2 = a \<Longrightarrow> P (of_bool b + 2 * a)\<close>
   555   for P and a :: \<open>'a word\<close>
   576   for P and a :: \<open>'a word\<close>
   556   proof (induction a rule: word_bit_induct)
   577   proof (induction a rule: word_bit_induct)
   557     case zero
   578     case zero
   564   next
   585   next
   565     case (odd a)
   586     case (odd a)
   566     with rec [of a True] show ?case
   587     with rec [of a True] show ?case
   567       using bit_word_half_eq [of a True] by (simp add: ac_simps)
   588       using bit_word_half_eq [of a True] by (simp add: ac_simps)
   568   qed
   589   qed
       
   590   show \<open>bit a n \<longleftrightarrow> odd (a div 2 ^ n)\<close> for a :: \<open>'a word\<close> and n
       
   591     by transfer (simp flip: drop_bit_eq_div add: drop_bit_take_bit bit_iff_odd_drop_bit)
   569   show \<open>0 div a = 0\<close>
   592   show \<open>0 div a = 0\<close>
   570     for a :: \<open>'a word\<close>
   593     for a :: \<open>'a word\<close>
   571     by transfer simp
   594     by transfer simp
   572   show \<open>a div 1 = a\<close>
   595   show \<open>a div 1 = a\<close>
   573     for a :: \<open>'a word\<close>
   596     for a :: \<open>'a word\<close>
   629       by (auto simp flip: take_bit_eq_mod drop_bit_eq_div push_bit_eq_mult
   652       by (auto simp flip: take_bit_eq_mod drop_bit_eq_div push_bit_eq_mult
   630         simp add: div_push_bit_of_1_eq_drop_bit drop_bit_take_bit drop_bit_push_bit_int [of n m])
   653         simp add: div_push_bit_of_1_eq_drop_bit drop_bit_take_bit drop_bit_push_bit_int [of n m])
   631   qed
   654   qed
   632 qed
   655 qed
   633 
   656 
   634 context
       
   635   includes lifting_syntax
       
   636 begin
       
   637 
       
   638 lemma transfer_rule_bit_word [transfer_rule]:
       
   639   \<open>((pcr_word :: int \<Rightarrow> 'a::len word \<Rightarrow> bool) ===> (=)) (\<lambda>k n. n < LENGTH('a) \<and> bit k n) bit\<close>
       
   640 proof -
       
   641   let ?t = \<open>\<lambda>a n. odd (take_bit LENGTH('a) a div take_bit LENGTH('a) ((2::int) ^ n))\<close>
       
   642   have \<open>((pcr_word :: int \<Rightarrow> 'a word \<Rightarrow> bool) ===> (=)) ?t bit\<close>
       
   643     by (unfold bit_def) transfer_prover
       
   644   also have \<open>?t = (\<lambda>k n. n < LENGTH('a) \<and> bit k n)\<close>
       
   645     by (simp add: fun_eq_iff bit_take_bit_iff flip: bit_def)
       
   646   finally show ?thesis .
       
   647 qed
       
   648 
       
   649 end
   657 end
   650 
   658 
   651 instantiation word :: (len) semiring_bit_shifts
   659 instantiation word :: (len) semiring_bit_shifts
   652 begin
   660 begin
   653 
   661 
   670 
   678 
   671 lift_definition drop_bit_word :: \<open>nat \<Rightarrow> 'a word \<Rightarrow> 'a word\<close>
   679 lift_definition drop_bit_word :: \<open>nat \<Rightarrow> 'a word \<Rightarrow> 'a word\<close>
   672   is \<open>\<lambda>n. drop_bit n \<circ> take_bit LENGTH('a)\<close>
   680   is \<open>\<lambda>n. drop_bit n \<circ> take_bit LENGTH('a)\<close>
   673   by (simp add: take_bit_eq_mod)
   681   by (simp add: take_bit_eq_mod)
   674 
   682 
       
   683 lift_definition take_bit_word :: \<open>nat \<Rightarrow> 'a word \<Rightarrow> 'a word\<close>
       
   684   is \<open>\<lambda>n. take_bit (min LENGTH('a) n)\<close>
       
   685   by (simp add: ac_simps) (simp only: flip: take_bit_take_bit)
       
   686 
   675 instance proof
   687 instance proof
   676   show \<open>push_bit n a = a * 2 ^ n\<close> for n :: nat and a :: "'a word"
   688   show \<open>push_bit n a = a * 2 ^ n\<close> for n :: nat and a :: "'a word"
   677     by transfer (simp add: push_bit_eq_mult)
   689     by transfer (simp add: push_bit_eq_mult)
   678   show \<open>drop_bit n a = a div 2 ^ n\<close> for n :: nat and a :: "'a word"
   690   show \<open>drop_bit n a = a div 2 ^ n\<close> for n :: nat and a :: "'a word"
   679     by transfer (simp flip: drop_bit_eq_div add: drop_bit_take_bit)
   691     by transfer (simp flip: drop_bit_eq_div add: drop_bit_take_bit)
       
   692   show \<open>take_bit n a = a mod 2 ^ n\<close> for n :: nat and a :: \<open>'a word\<close>
       
   693     by transfer (auto simp flip: take_bit_eq_mod)
   680 qed
   694 qed
   681 
   695 
   682 end
   696 end
   683 
   697 
   684 instantiation word :: (len) ring_bit_operations
   698 instantiation word :: (len) ring_bit_operations
   721 
   735 
   722 lemma even_word_iff [code]:
   736 lemma even_word_iff [code]:
   723   \<open>even_word a \<longleftrightarrow> a AND 1 = 0\<close>
   737   \<open>even_word a \<longleftrightarrow> a AND 1 = 0\<close>
   724   by (simp add: even_word_def and_one_eq even_iff_mod_2_eq_zero)
   738   by (simp add: even_word_def and_one_eq even_iff_mod_2_eq_zero)
   725 
   739 
   726 definition bit_word :: \<open>'a::len word \<Rightarrow> nat \<Rightarrow> bool\<close>
   740 lemma bit_word_iff_drop_bit_and [code]:
   727   where [code_abbrev]: \<open>bit_word = bit\<close>
   741   \<open>bit a n \<longleftrightarrow> drop_bit n a AND 1 = 1\<close> for a :: \<open>'a::len word\<close>
   728 
   742   by (simp add: bit_iff_odd_drop_bit odd_iff_mod_2_eq_one and_one_eq)
   729 lemma bit_word_iff [code]:
       
   730   \<open>bit_word a n \<longleftrightarrow> drop_bit n a AND 1 = 1\<close>
       
   731   by (simp add: bit_word_def bit_iff_odd_drop_bit and_one_eq odd_iff_mod_2_eq_one)
       
   732 
   743 
   733 lifting_update word.lifting
   744 lifting_update word.lifting
   734 lifting_forget word.lifting
   745 lifting_forget word.lifting
   735 
   746 
   736 end
   747 end