src/HOL/Word/Word.thy
changeset 71990 66beb9d92e43
parent 71986 76193dd4aec8
child 71991 8bff286878bf
equal deleted inserted replaced
71989:bad75618fb82 71990:66beb9d92e43
    13   Bit_Comprehension
    13   Bit_Comprehension
    14   Misc_Typedef
    14   Misc_Typedef
    15   Misc_Arithmetic
    15   Misc_Arithmetic
    16 begin
    16 begin
    17 
    17 
    18 text \<open>See \<^file>\<open>Word_Examples.thy\<close> for examples.\<close>
    18 subsection \<open>Prelude\<close>
       
    19 
       
    20 lemma (in semiring_bit_shifts) bit_push_bit_iff: \<comment> \<open>TODO move\<close>
       
    21   \<open>bit (push_bit m a) n \<longleftrightarrow> m \<le> n \<and> 2 ^ n \<noteq> 0 \<and> bit a (n - m)\<close>
       
    22   by (auto simp add: bit_iff_odd push_bit_eq_mult even_mult_exp_div_exp_iff)
       
    23 
       
    24 lemma (in semiring_bit_shifts) push_bit_numeral [simp]: \<comment> \<open>TODO: move\<close>
       
    25   \<open>push_bit (numeral l) (numeral k) = push_bit (pred_numeral l) (numeral (Num.Bit0 k))\<close>
       
    26   by (simp add: numeral_eq_Suc mult_2_right) (simp add: numeral_Bit0)
       
    27 
       
    28 lemma minus_mod_int_eq: \<comment> \<open>TODO move\<close>
       
    29   \<open>- k mod l = l - 1 - (k - 1) mod l\<close> if \<open>l \<ge> 0\<close> for k l :: int
       
    30 proof (cases \<open>l = 0\<close>)
       
    31   case True
       
    32   then show ?thesis
       
    33     by simp 
       
    34 next
       
    35   case False
       
    36   with that have \<open>l > 0\<close>
       
    37     by simp
       
    38   then show ?thesis
       
    39   proof (cases \<open>l dvd k\<close>)
       
    40     case True
       
    41     then obtain j where \<open>k = l * j\<close> ..
       
    42     moreover have \<open>(l * j mod l - 1) mod l = l - 1\<close>
       
    43       using \<open>l > 0\<close> by (simp add: zmod_minus1)
       
    44     then have \<open>(l * j - 1) mod l = l - 1\<close>
       
    45       by (simp only: mod_simps)
       
    46     ultimately show ?thesis
       
    47       by simp
       
    48   next
       
    49     case False
       
    50     moreover have \<open>0 < k mod l\<close> \<open>k mod l < 1 + l\<close>
       
    51       using \<open>0 < l\<close> le_imp_0_less pos_mod_conj False apply auto
       
    52       using le_less apply fastforce
       
    53       using pos_mod_bound [of l k] apply linarith 
       
    54       done
       
    55     with \<open>l > 0\<close> have \<open>(k mod l - 1) mod l = k mod l - 1\<close>
       
    56       by (simp add: zmod_trival_iff)
       
    57     ultimately show ?thesis
       
    58       apply (simp only: zmod_zminus1_eq_if)
       
    59       apply (simp add: mod_eq_0_iff_dvd algebra_simps mod_simps)
       
    60       done
       
    61   qed
       
    62 qed
       
    63 
       
    64 lemma nth_rotate: \<comment> \<open>TODO move\<close>
       
    65   \<open>rotate m xs ! n = xs ! ((m + n) mod length xs)\<close> if \<open>n < length xs\<close>
       
    66   using that apply (auto simp add: rotate_drop_take nth_append not_less less_diff_conv ac_simps dest!: le_Suc_ex)
       
    67    apply (metis add.commute mod_add_right_eq mod_less)
       
    68   apply (metis (no_types, lifting) Nat.diff_diff_right add.commute add_diff_cancel_right' diff_le_self dual_order.strict_trans2 length_greater_0_conv less_nat_zero_code list.size(3) mod_add_right_eq mod_add_self2 mod_le_divisor mod_less)
       
    69   done
       
    70 
       
    71 lemma nth_rotate1: \<comment> \<open>TODO move\<close>
       
    72   \<open>rotate1 xs ! n = xs ! (Suc n mod length xs)\<close> if \<open>n < length xs\<close>
       
    73   using that nth_rotate [of n xs 1] by simp
       
    74 
    19 
    75 
    20 subsection \<open>Type definition\<close>
    76 subsection \<open>Type definition\<close>
    21 
    77 
    22 quotient_type (overloaded) 'a word = int / \<open>\<lambda>k l. take_bit LENGTH('a) k = take_bit LENGTH('a::len) l\<close>
    78 quotient_type (overloaded) 'a word = int / \<open>\<lambda>k l. take_bit LENGTH('a) k = take_bit LENGTH('a::len) l\<close>
    23   morphisms rep_word word_of_int by (auto intro!: equivpI reflpI sympI transpI)
    79   morphisms rep_word word_of_int by (auto intro!: equivpI reflpI sympI transpI)
   811 
   867 
   812 end
   868 end
   813 
   869 
   814 lemma bit_word_eqI:
   870 lemma bit_word_eqI:
   815   \<open>a = b\<close> if \<open>\<And>n. n \<le> LENGTH('a) \<Longrightarrow> bit a n \<longleftrightarrow> bit b n\<close>
   871   \<open>a = b\<close> if \<open>\<And>n. n \<le> LENGTH('a) \<Longrightarrow> bit a n \<longleftrightarrow> bit b n\<close>
   816     for a b :: \<open>'a::len word\<close>
   872   for a b :: \<open>'a::len word\<close>
   817   by (rule bit_eqI, rule that) (simp add: exp_eq_zero_iff)
   873   using that by transfer (auto simp add: nat_less_le bit_eq_iff bit_take_bit_iff)
       
   874 
       
   875 lemma bit_imp_le_length:
       
   876   \<open>n < LENGTH('a)\<close> if \<open>bit w n\<close>
       
   877     for w :: \<open>'a::len word\<close>
       
   878   using that by transfer simp
       
   879 
       
   880 lemma not_bit_length [simp]:
       
   881   \<open>\<not> bit w LENGTH('a)\<close> for w :: \<open>'a::len word\<close>
       
   882   by transfer simp
       
   883 
       
   884 lemma bit_word_of_int_iff:
       
   885   \<open>bit (word_of_int k :: 'a::len word) n \<longleftrightarrow> n < LENGTH('a) \<and> bit k n\<close>
       
   886   by transfer rule
       
   887 
       
   888 lemma bit_uint_iff:
       
   889   \<open>bit (uint w) n \<longleftrightarrow> n < LENGTH('a) \<and> bit w n\<close>
       
   890     for w :: \<open>'a::len word\<close>
       
   891   by transfer (simp add: bit_take_bit_iff)
       
   892 
       
   893 lemma bit_sint_iff:
       
   894   \<open>bit (sint w) n \<longleftrightarrow> n \<ge> LENGTH('a) \<and> bit w (LENGTH('a) - 1) \<or> bit w n\<close>
       
   895   for w :: \<open>'a::len word\<close>
       
   896   apply (cases \<open>LENGTH('a)\<close>)
       
   897    apply simp
       
   898   apply (simp add: sint_uint nth_sbintr not_less bit_uint_iff not_le Suc_le_eq)
       
   899   apply (auto simp add: le_less dest: bit_imp_le_length)
       
   900   done
       
   901 
       
   902 lemma bit_word_ucast_iff:
       
   903   \<open>bit (ucast w :: 'b::len word) n \<longleftrightarrow> n < LENGTH('a) \<and> n < LENGTH('b) \<and> bit w n\<close>
       
   904   for w :: \<open>'a::len word\<close>
       
   905   by (simp add: ucast_def bit_word_of_int_iff bit_uint_iff ac_simps)
       
   906 
       
   907 lemma bit_word_scast_iff:
       
   908   \<open>bit (scast w :: 'b::len word) n \<longleftrightarrow>
       
   909     n < LENGTH('b) \<and> (bit w n \<or> LENGTH('a) \<le> n \<and> bit w (LENGTH('a) - Suc 0))\<close>
       
   910   for w :: \<open>'a::len word\<close>
       
   911   by (simp add: scast_def bit_word_of_int_iff bit_sint_iff ac_simps)
   818 
   912 
   819 definition shiftl1 :: "'a::len word \<Rightarrow> 'a word"
   913 definition shiftl1 :: "'a::len word \<Rightarrow> 'a word"
   820   where "shiftl1 w = word_of_int (2 * uint w)"
   914   where "shiftl1 w = word_of_int (2 * uint w)"
   821 
   915 
   822 lemma shiftl1_eq_mult_2:
   916 lemma shiftl1_eq_mult_2:
   823   \<open>shiftl1 = (*) 2\<close>
   917   \<open>shiftl1 = (*) 2\<close>
   824   apply (simp add: fun_eq_iff shiftl1_def)
   918   apply (simp add: fun_eq_iff shiftl1_def)
   825   apply (simp only: mult_2)
       
   826   apply transfer
   919   apply transfer
   827   apply (simp only: take_bit_add)
   920   apply (simp only: mult_2 take_bit_add)
   828   apply simp
   921   apply simp
   829   done
   922   done
       
   923 
       
   924 lemma bit_shiftl1_iff:
       
   925   \<open>bit (shiftl1 w) n \<longleftrightarrow> 0 < n \<and> n < LENGTH('a) \<and> bit w (n - 1)\<close>
       
   926     for w :: \<open>'a::len word\<close>
       
   927   by (simp add: shiftl1_eq_mult_2 bit_double_iff exp_eq_zero_iff not_le) (simp add: ac_simps)
   830 
   928 
   831 definition shiftr1 :: "'a::len word \<Rightarrow> 'a word"
   929 definition shiftr1 :: "'a::len word \<Rightarrow> 'a word"
   832   \<comment> \<open>shift right as unsigned or as signed, ie logical or arithmetic\<close>
   930   \<comment> \<open>shift right as unsigned or as signed, ie logical or arithmetic\<close>
   833   where "shiftr1 w = word_of_int (bin_rest (uint w))"
   931   where "shiftr1 w = word_of_int (bin_rest (uint w))"
   834 
   932 
   836   \<open>shiftr1 w = w div 2\<close>
   934   \<open>shiftr1 w = w div 2\<close>
   837   apply (simp add: fun_eq_iff shiftr1_def)
   935   apply (simp add: fun_eq_iff shiftr1_def)
   838   apply transfer
   936   apply transfer
   839   apply (auto simp add: not_le dest: less_2_cases)
   937   apply (auto simp add: not_le dest: less_2_cases)
   840   done
   938   done
       
   939 
       
   940 lemma bit_shiftr1_iff:
       
   941   \<open>bit (shiftr1 w) n \<longleftrightarrow> bit w (Suc n)\<close>
       
   942     for w :: \<open>'a::len word\<close>
       
   943   by (simp add: shiftr1_eq_div_2 bit_Suc)
   841 
   944 
   842 instantiation word :: (len) ring_bit_operations
   945 instantiation word :: (len) ring_bit_operations
   843 begin
   946 begin
   844 
   947 
   845 lift_definition not_word :: \<open>'a word \<Rightarrow> 'a word\<close>
   948 lift_definition not_word :: \<open>'a word \<Rightarrow> 'a word\<close>
   898   apply (simp add: word_test_bit_def fun_eq_iff)
  1001   apply (simp add: word_test_bit_def fun_eq_iff)
   899   apply transfer
  1002   apply transfer
   900   apply (simp add: bit_take_bit_iff)
  1003   apply (simp add: bit_take_bit_iff)
   901   done
  1004   done
   902 
  1005 
       
  1006 lemma set_bit_unfold:
       
  1007   \<open>set_bit w n b = (if b then Bit_Operations.set_bit n w else unset_bit n w)\<close>
       
  1008   for w :: \<open>'a::len word\<close>
       
  1009   apply (auto simp add: word_set_bit_def bin_clr_conv_NAND bin_set_conv_OR unset_bit_def set_bit_def shiftl_int_def; transfer)
       
  1010    apply simp_all
       
  1011   done
       
  1012 
       
  1013 lemma bit_set_bit_word_iff:
       
  1014   \<open>bit (set_bit w m b) n \<longleftrightarrow> (if m = n then n < LENGTH('a) \<and> b else bit w n)\<close>
       
  1015   for w :: \<open>'a::len word\<close>
       
  1016   by (auto simp add: set_bit_unfold bit_unset_bit_iff bit_set_bit_iff exp_eq_zero_iff not_le bit_imp_le_length)
       
  1017 
   903 lemma lsb_word_eq:
  1018 lemma lsb_word_eq:
   904   \<open>lsb = (odd :: 'a word \<Rightarrow> bool)\<close> for w :: \<open>'a::len word\<close>
  1019   \<open>lsb = (odd :: 'a word \<Rightarrow> bool)\<close> for w :: \<open>'a::len word\<close>
   905   apply (simp add: word_lsb_def fun_eq_iff)
  1020   apply (simp add: word_lsb_def fun_eq_iff)
   906   apply transfer
  1021   apply transfer
   907   apply simp
  1022   apply simp
   916 
  1031 
   917 lemma shiftl_word_eq:
  1032 lemma shiftl_word_eq:
   918   \<open>w << n = push_bit n w\<close> for w :: \<open>'a::len word\<close>
  1033   \<open>w << n = push_bit n w\<close> for w :: \<open>'a::len word\<close>
   919   by (induction n) (simp_all add: shiftl_def shiftl1_eq_mult_2 push_bit_double)
  1034   by (induction n) (simp_all add: shiftl_def shiftl1_eq_mult_2 push_bit_double)
   920 
  1035 
       
  1036 lemma bit_shiftl_word_iff:
       
  1037   \<open>bit (w << m) n \<longleftrightarrow> m \<le> n \<and> n < LENGTH('a) \<and> bit w (n - m)\<close>
       
  1038   for w :: \<open>'a::len word\<close>
       
  1039   by (simp add: shiftl_word_eq bit_push_bit_iff exp_eq_zero_iff not_le)
       
  1040 
   921 lemma [code]:
  1041 lemma [code]:
   922   \<open>push_bit n w = w << n\<close> for w :: \<open>'a::len word\<close>
  1042   \<open>push_bit n w = w << n\<close> for w :: \<open>'a::len word\<close>
   923   by (simp add: shiftl_word_eq)
  1043   by (simp add: shiftl_word_eq)
   924 
  1044 
   925 lemma shiftr_word_eq:
  1045 lemma shiftr_word_eq:
   926   \<open>w >> n = drop_bit n w\<close> for w :: \<open>'a::len word\<close>
  1046   \<open>w >> n = drop_bit n w\<close> for w :: \<open>'a::len word\<close>
   927   by (induction n) (simp_all add: shiftr_def shiftr1_eq_div_2 drop_bit_Suc drop_bit_half)
  1047   by (induction n) (simp_all add: shiftr_def shiftr1_eq_div_2 drop_bit_Suc drop_bit_half)
       
  1048 
       
  1049 lemma bit_shiftr_word_iff:
       
  1050   \<open>bit (w >> m) n \<longleftrightarrow> bit w (m + n)\<close>
       
  1051   for w :: \<open>'a::len word\<close>
       
  1052   by (simp add: shiftr_word_eq bit_drop_bit_eq)
   928 
  1053 
   929 lemma [code]:
  1054 lemma [code]:
   930   \<open>drop_bit n w = w >> n\<close> for w :: \<open>'a::len word\<close>
  1055   \<open>drop_bit n w = w >> n\<close> for w :: \<open>'a::len word\<close>
   931   by (simp add: shiftr_word_eq)
  1056   by (simp add: shiftr_word_eq)
   932 
  1057 
   950   by (transfer, simp add: take_bit_not_take_bit)+
  1075   by (transfer, simp add: take_bit_not_take_bit)+
   951 
  1076 
   952 definition setBit :: "'a::len word \<Rightarrow> nat \<Rightarrow> 'a word"
  1077 definition setBit :: "'a::len word \<Rightarrow> nat \<Rightarrow> 'a word"
   953   where "setBit w n = set_bit w n True"
  1078   where "setBit w n = set_bit w n True"
   954 
  1079 
       
  1080 lemma setBit_eq_set_bit:
       
  1081   \<open>setBit w n = Bit_Operations.set_bit n w\<close>
       
  1082   for w :: \<open>'a::len word\<close>
       
  1083   by (simp add: setBit_def set_bit_unfold)
       
  1084 
       
  1085 lemma bit_setBit_iff:
       
  1086   \<open>bit (setBit w m) n \<longleftrightarrow> (m = n \<and> n < LENGTH('a) \<or> bit w n)\<close>
       
  1087   for w :: \<open>'a::len word\<close>
       
  1088   by (simp add: setBit_eq_set_bit bit_set_bit_iff exp_eq_zero_iff not_le ac_simps)
       
  1089 
   955 definition clearBit :: "'a::len word \<Rightarrow> nat \<Rightarrow> 'a word"
  1090 definition clearBit :: "'a::len word \<Rightarrow> nat \<Rightarrow> 'a word"
   956   where "clearBit w n = set_bit w n False"
  1091   where "clearBit w n = set_bit w n False"
       
  1092 
       
  1093 lemma clearBit_eq_unset_bit:
       
  1094   \<open>clearBit w n = unset_bit n w\<close>
       
  1095   for w :: \<open>'a::len word\<close>
       
  1096   by (simp add: clearBit_def set_bit_unfold)
       
  1097 
       
  1098 lemma bit_clearBit_iff:
       
  1099   \<open>bit (clearBit w m) n \<longleftrightarrow> m \<noteq> n \<and> bit w n\<close>
       
  1100   for w :: \<open>'a::len word\<close>
       
  1101   by (simp add: clearBit_eq_unset_bit bit_unset_bit_iff ac_simps)
   957 
  1102 
   958 definition even_word :: \<open>'a::len word \<Rightarrow> bool\<close>
  1103 definition even_word :: \<open>'a::len word \<Rightarrow> bool\<close>
   959   where [code_abbrev]: \<open>even_word = even\<close>
  1104   where [code_abbrev]: \<open>even_word = even\<close>
   960 
  1105 
   961 lemma even_word_iff [code]:
  1106 lemma even_word_iff [code]:
   979   where "w >>> n = (sshiftr1 ^^ n) w"
  1124   where "w >>> n = (sshiftr1 ^^ n) w"
   980 
  1125 
   981 definition mask :: "nat \<Rightarrow> 'a::len word"
  1126 definition mask :: "nat \<Rightarrow> 'a::len word"
   982   where "mask n = (1 << n) - 1"
  1127   where "mask n = (1 << n) - 1"
   983 
  1128 
       
  1129 definition slice1 :: "nat \<Rightarrow> 'a::len word \<Rightarrow> 'b::len word"
       
  1130   where "slice1 n w = of_bl (takefill False n (to_bl w))"
       
  1131 
   984 definition revcast :: "'a::len word \<Rightarrow> 'b::len word"
  1132 definition revcast :: "'a::len word \<Rightarrow> 'b::len word"
   985   where "revcast w =  of_bl (takefill False (LENGTH('b)) (to_bl w))"
  1133   where "revcast w =  of_bl (takefill False (LENGTH('b)) (to_bl w))"
   986 
  1134 
   987 definition slice1 :: "nat \<Rightarrow> 'a::len word \<Rightarrow> 'b::len word"
  1135 lemma revcast_eq:
   988   where "slice1 n w = of_bl (takefill False n (to_bl w))"
  1136   \<open>(revcast :: 'a::len word \<Rightarrow> 'b::len word) = slice1 LENGTH('b)\<close>
       
  1137   by (simp add: fun_eq_iff revcast_def slice1_def)
   989 
  1138 
   990 definition slice :: "nat \<Rightarrow> 'a::len word \<Rightarrow> 'b::len word"
  1139 definition slice :: "nat \<Rightarrow> 'a::len word \<Rightarrow> 'b::len word"
   991   where "slice n w = slice1 (size w - n) w"
  1140   where "slice n w = slice1 (size w - n) w"
   992 
  1141 
   993 
  1142 
  1013 
  1162 
  1014 subsection \<open>Split and cat operations\<close>
  1163 subsection \<open>Split and cat operations\<close>
  1015 
  1164 
  1016 definition word_cat :: "'a::len word \<Rightarrow> 'b::len word \<Rightarrow> 'c::len word"
  1165 definition word_cat :: "'a::len word \<Rightarrow> 'b::len word \<Rightarrow> 'c::len word"
  1017   where "word_cat a b = word_of_int (bin_cat (uint a) (LENGTH('b)) (uint b))"
  1166   where "word_cat a b = word_of_int (bin_cat (uint a) (LENGTH('b)) (uint b))"
       
  1167 
       
  1168 lemma word_cat_eq:
       
  1169   \<open>(word_cat v w :: 'c::len word) = push_bit LENGTH('b) (ucast v) + ucast w\<close>
       
  1170   for v :: \<open>'a::len word\<close> and w :: \<open>'b::len word\<close>
       
  1171   apply (simp add: word_cat_def bin_cat_eq_push_bit_add_take_bit ucast_def)
       
  1172   apply transfer apply simp
       
  1173   done
       
  1174 
       
  1175 lemma bit_word_cat_iff:
       
  1176   \<open>bit (word_cat v w :: 'c::len word) n \<longleftrightarrow> n < LENGTH('c) \<and> (if n < LENGTH('b) then bit w n else bit v (n - LENGTH('b)))\<close> 
       
  1177   for v :: \<open>'a::len word\<close> and w :: \<open>'b::len word\<close>
       
  1178   by (auto simp add: word_cat_def bit_word_of_int_iff bin_nth_cat bit_uint_iff not_less bit_imp_le_length)
  1018 
  1179 
  1019 definition word_split :: "'a::len word \<Rightarrow> 'b::len word \<times> 'c::len word"
  1180 definition word_split :: "'a::len word \<Rightarrow> 'b::len word \<times> 'c::len word"
  1020   where "word_split a =
  1181   where "word_split a =
  1021     (case bin_split (LENGTH('c)) (uint a) of
  1182     (case bin_split (LENGTH('c)) (uint a) of
  1022       (u, v) \<Rightarrow> (word_of_int u, word_of_int v))"
  1183       (u, v) \<Rightarrow> (word_of_int u, word_of_int v))"
  1402 lemma test_bit_of_bl:
  1563 lemma test_bit_of_bl:
  1403   "(of_bl bl::'a::len word) !! n = (rev bl ! n \<and> n < LENGTH('a) \<and> n < length bl)"
  1564   "(of_bl bl::'a::len word) !! n = (rev bl ! n \<and> n < LENGTH('a) \<and> n < length bl)"
  1404   by (auto simp add: of_bl_def word_test_bit_def word_size
  1565   by (auto simp add: of_bl_def word_test_bit_def word_size
  1405       word_ubin.eq_norm nth_bintr bin_nth_of_bl)
  1566       word_ubin.eq_norm nth_bintr bin_nth_of_bl)
  1406 
  1567 
       
  1568 lemma bit_of_bl_iff:
       
  1569   \<open>bit (of_bl bs :: 'a word) n \<longleftrightarrow> rev bs ! n \<and> n < LENGTH('a::len) \<and> n < length bs\<close>
       
  1570   using test_bit_of_bl [of bs n] by (simp add: test_bit_word_eq)
       
  1571 
  1407 lemma no_of_bl: "(numeral bin ::'a::len word) = of_bl (bin_to_bl (LENGTH('a)) (numeral bin))"
  1572 lemma no_of_bl: "(numeral bin ::'a::len word) = of_bl (bin_to_bl (LENGTH('a)) (numeral bin))"
  1408   by (simp add: of_bl_def)
  1573   by (simp add: of_bl_def)
  1409 
  1574 
  1410 lemma uint_bl: "to_bl w = bin_to_bl (size w) (uint w)"
  1575 lemma uint_bl: "to_bl w = bin_to_bl (size w) (uint w)"
  1411   by (auto simp: word_size to_bl_def)
  1576   by (auto simp: word_size to_bl_def)
  3027   apply (rule trans)
  3192   apply (rule trans)
  3028    apply (rule nth_rev_alt)
  3193    apply (rule nth_rev_alt)
  3029    apply (auto simp add: word_size)
  3194    apply (auto simp add: word_size)
  3030   done
  3195   done
  3031 
  3196 
       
  3197 lemma map_bit_interval_eq:
       
  3198   \<open>map (bit w) [0..<n] = takefill False n (rev (to_bl w))\<close> for w :: \<open>'a::len word\<close>
       
  3199 proof (rule nth_equalityI)
       
  3200   show \<open>length (map (bit w) [0..<n]) =
       
  3201     length (takefill False n (rev (to_bl w)))\<close>
       
  3202     by simp
       
  3203   fix m
       
  3204   assume \<open>m < length (map (bit w) [0..<n])\<close>
       
  3205   then have \<open>m < n\<close>
       
  3206     by simp
       
  3207   then have \<open>bit w m \<longleftrightarrow> takefill False n (rev (to_bl w)) ! m\<close>
       
  3208     by (auto simp add: nth_takefill not_less rev_nth to_bl_nth word_size test_bit_word_eq dest: bit_imp_le_length)
       
  3209   with \<open>m < n \<close>show \<open>map (bit w) [0..<n] ! m \<longleftrightarrow> takefill False n (rev (to_bl w)) ! m\<close>
       
  3210     by simp
       
  3211 qed
       
  3212 
       
  3213 lemma to_bl_unfold:
       
  3214   \<open>to_bl w = rev (map (bit w) [0..<LENGTH('a)])\<close> for w :: \<open>'a::len word\<close>
       
  3215   by (simp add: map_bit_interval_eq takefill_bintrunc to_bl_def flip: bin_to_bl_def)
       
  3216 
       
  3217 lemma nth_rev_to_bl:
       
  3218   \<open>rev (to_bl w) ! n \<longleftrightarrow> bit w n\<close>
       
  3219   if \<open>n < LENGTH('a)\<close> for w :: \<open>'a::len word\<close>
       
  3220   using that by (simp add: to_bl_unfold)
       
  3221 
       
  3222 lemma nth_to_bl:
       
  3223   \<open>to_bl w ! n \<longleftrightarrow> bit w (LENGTH('a) - Suc n)\<close>
       
  3224   if \<open>n < LENGTH('a)\<close> for w :: \<open>'a::len word\<close>
       
  3225   using that by (simp add: to_bl_unfold rev_nth)
       
  3226 
  3032 lemma test_bit_set: "(set_bit w n x) !! n \<longleftrightarrow> n < size w \<and> x"
  3227 lemma test_bit_set: "(set_bit w n x) !! n \<longleftrightarrow> n < size w \<and> x"
  3033   for w :: "'a::len word"
  3228   for w :: "'a::len word"
  3034   by (auto simp: word_size word_test_bit_def word_set_bit_def word_ubin.eq_norm nth_bintr)
  3229   by (auto simp: word_size word_test_bit_def word_set_bit_def word_ubin.eq_norm nth_bintr)
  3035 
  3230 
  3036 lemma test_bit_set_gen:
  3231 lemma test_bit_set_gen:
  3042       simp add: word_test_bit_def [symmetric])
  3237       simp add: word_test_bit_def [symmetric])
  3043   done
  3238   done
  3044 
  3239 
  3045 lemma of_bl_rep_False: "of_bl (replicate n False @ bs) = of_bl bs"
  3240 lemma of_bl_rep_False: "of_bl (replicate n False @ bs) = of_bl bs"
  3046   by (auto simp: of_bl_def bl_to_bin_rep_F)
  3241   by (auto simp: of_bl_def bl_to_bin_rep_F)
       
  3242 
       
  3243 lemma bit_word_reverse_iff:
       
  3244   \<open>bit (word_reverse w) n \<longleftrightarrow> n < LENGTH('a) \<and> bit w (LENGTH('a) - Suc n)\<close>
       
  3245   for w :: \<open>'a::len word\<close>
       
  3246   by (cases \<open>n < LENGTH('a)\<close>) (simp_all add: word_reverse_def bit_of_bl_iff nth_to_bl)
       
  3247 
       
  3248 lemma bit_slice1_iff:
       
  3249   \<open>bit (slice1 m w :: 'b::len word) n \<longleftrightarrow> m - LENGTH('a) \<le> n \<and> n < min LENGTH('b) m
       
  3250     \<and> bit w (n + (LENGTH('a) - m) - (m - LENGTH('a)))\<close>
       
  3251   for w :: \<open>'a::len word\<close>
       
  3252   by (cases \<open>n + (LENGTH('a) - m) - (m - LENGTH('a)) < LENGTH('a)\<close>)
       
  3253     (auto simp add: slice1_def bit_of_bl_iff takefill_alt rev_take nth_append not_less nth_rev_to_bl ac_simps)
       
  3254 
       
  3255 lemma bit_revcast_iff:
       
  3256   \<open>bit (revcast w :: 'b::len word) n \<longleftrightarrow> LENGTH('b) - LENGTH('a) \<le> n \<and> n < LENGTH('b)
       
  3257     \<and> bit w (n + (LENGTH('a) - LENGTH('b)) - (LENGTH('b) - LENGTH('a)))\<close>
       
  3258   for w :: \<open>'a::len word\<close>
       
  3259   by (simp add: revcast_eq bit_slice1_iff)
       
  3260 
       
  3261 lemma bit_slice_iff:
       
  3262   \<open>bit (slice m w :: 'b::len word) n \<longleftrightarrow> n < min LENGTH('b) (LENGTH('a) - m) \<and> bit w (n + LENGTH('a) - (LENGTH('a) - m))\<close>
       
  3263   for w :: \<open>'a::len word\<close>
       
  3264   by (simp add: slice_def word_size bit_slice1_iff)
  3047 
  3265 
  3048 lemma msb_nth: "msb w = w !! (LENGTH('a) - 1)"
  3266 lemma msb_nth: "msb w = w !! (LENGTH('a) - 1)"
  3049   for w :: "'a::len word"
  3267   for w :: "'a::len word"
  3050   by (simp add: word_msb_nth word_test_bit_def)
  3268   by (simp add: word_msb_nth word_test_bit_def)
  3051 
  3269 
  3212 
  3430 
  3213 instance ..
  3431 instance ..
  3214 
  3432 
  3215 end
  3433 end
  3216 
  3434 
       
  3435 lemma bit_set_bits_word_iff:
       
  3436   \<open>bit (set_bits P :: 'a::len word) n \<longleftrightarrow> n < LENGTH('a) \<and> P n\<close>
       
  3437   by (auto simp add: word_set_bits_def bit_of_bl_iff)
       
  3438 
  3217 lemmas of_nth_def = word_set_bits_def (* FIXME duplicate *)
  3439 lemmas of_nth_def = word_set_bits_def (* FIXME duplicate *)
  3218 
  3440 
  3219 lemma td_ext_nth [OF refl refl refl, unfolded word_size]:
  3441 lemma td_ext_nth [OF refl refl refl, unfolded word_size]:
  3220   "n = size w \<Longrightarrow> ofn = set_bits \<Longrightarrow> [w, ofn g] = l \<Longrightarrow>
  3442   "n = size w \<Longrightarrow> ofn = set_bits \<Longrightarrow> [w, ofn g] = l \<Longrightarrow>
  3221     td_ext test_bit ofn {f. \<forall>i. f i \<longrightarrow> i < n} (\<lambda>h i. h i \<and> i < n)"
  3443     td_ext test_bit ofn {f. \<forall>i. f i \<longrightarrow> i < n} (\<lambda>h i. h i \<and> i < n)"
  3334 lemma uint_shiftr1: "uint (shiftr1 w) = bin_rest (uint w)"
  3556 lemma uint_shiftr1: "uint (shiftr1 w) = bin_rest (uint w)"
  3335   apply (unfold shiftr1_def word_ubin.eq_norm bin_rest_trunc_i)
  3557   apply (unfold shiftr1_def word_ubin.eq_norm bin_rest_trunc_i)
  3336   apply (subst bintr_uint [symmetric, OF order_refl])
  3558   apply (subst bintr_uint [symmetric, OF order_refl])
  3337   apply (simp only : bintrunc_bintrunc_l)
  3559   apply (simp only : bintrunc_bintrunc_l)
  3338   apply simp
  3560   apply simp
       
  3561   done
       
  3562 
       
  3563 lemma bit_sshiftr1_iff:
       
  3564   \<open>bit (sshiftr1 w) n \<longleftrightarrow> bit w (if n = LENGTH('a) - 1 then LENGTH('a) - 1 else Suc n)\<close>
       
  3565   for w :: \<open>'a::len word\<close>
       
  3566   apply (cases \<open>LENGTH('a)\<close>)
       
  3567   apply simp
       
  3568   apply (simp add: sshiftr1_def bit_word_of_int_iff bit_sint_iff flip: bit_Suc)
       
  3569   apply transfer apply auto
       
  3570   done
       
  3571 
       
  3572 lemma bit_sshiftr_word_iff:
       
  3573   \<open>bit (w >>> m) n \<longleftrightarrow> bit w (if LENGTH('a) - m \<le> n \<and> n < LENGTH('a) then LENGTH('a) - 1 else (m + n))\<close>
       
  3574   for w :: \<open>'a::len word\<close>
       
  3575   apply (cases \<open>LENGTH('a)\<close>)
       
  3576    apply simp
       
  3577   apply (simp only:)
       
  3578   apply (induction m arbitrary: n)
       
  3579    apply (auto simp add: sshiftr_def bit_sshiftr1_iff not_le less_diff_conv)
  3339   done
  3580   done
  3340 
  3581 
  3341 lemma nth_sshiftr1: "sshiftr1 w !! n = (if n = size w - 1 then w !! n else w !! Suc n)"
  3582 lemma nth_sshiftr1: "sshiftr1 w !! n = (if n = size w - 1 then w !! n else w !! Suc n)"
  3342   apply (unfold sshiftr1_def word_test_bit_def)
  3583   apply (unfold sshiftr1_def word_test_bit_def)
  3343   apply (simp add: nth_bintr word_ubin.eq_norm bit_Suc [symmetric] word_size)
  3584   apply (simp add: nth_bintr word_ubin.eq_norm bit_Suc [symmetric] word_size)
  3402   apply (induct n)
  3643   apply (induct n)
  3403    apply simp
  3644    apply simp
  3404   apply (simp add: sshiftr1_div_2 mult.commute zdiv_zmult2_eq [symmetric])
  3645   apply (simp add: sshiftr1_div_2 mult.commute zdiv_zmult2_eq [symmetric])
  3405   done
  3646   done
  3406 
  3647 
       
  3648 lemma bit_bshiftr1_iff:
       
  3649   \<open>bit (bshiftr1 b w) n \<longleftrightarrow> b \<and> n = LENGTH('a) - 1 \<or> bit w (Suc n)\<close>
       
  3650   for w :: \<open>'a::len word\<close>
       
  3651   apply (cases \<open>LENGTH('a)\<close>)
       
  3652   apply simp
       
  3653   apply (simp add: bshiftr1_def bit_of_bl_iff nth_append not_less rev_nth nth_butlast nth_to_bl)
       
  3654   apply (use bit_imp_le_length in fastforce)
       
  3655   done
       
  3656 
  3407 
  3657 
  3408 subsubsection \<open>shift functions in terms of lists of bools\<close>
  3658 subsubsection \<open>shift functions in terms of lists of bools\<close>
  3409 
  3659 
  3410 lemmas bshiftr1_numeral [simp] =
  3660 lemmas bshiftr1_numeral [simp] =
  3411   bshiftr1_def [where w="numeral w", unfolded to_bl_numeral] for w
  3661   bshiftr1_def [where w="numeral w", unfolded to_bl_numeral] for w
  3704   by (simp add: mask_eq)
  3954   by (simp add: mask_eq)
  3705 
  3955 
  3706 context
  3956 context
  3707 begin
  3957 begin
  3708 
  3958 
  3709 qualified lemma bit_mask_iff [simp]:
  3959 qualified lemma bit_mask_iff:
  3710   \<open>bit (mask m :: 'a::len word) n \<longleftrightarrow> n < LENGTH('a) \<and> n < m\<close>
  3960   \<open>bit (mask m :: 'a::len word) n \<longleftrightarrow> n < min LENGTH('a) m\<close>
  3711   by (simp add: mask_eq_mask bit_mask_iff exp_eq_zero_iff not_le)
  3961   by (simp add: mask_eq_mask bit_mask_iff exp_eq_zero_iff not_le)
  3712 
  3962 
  3713 end
  3963 end
  3714 
  3964 
  3715 lemma nth_mask [simp]:
  3965 lemma nth_mask [simp]:
  3716   \<open>(mask n :: 'a::len word) !! i \<longleftrightarrow> i < n \<and> i < size (mask n :: 'a word)\<close>
  3966   \<open>(mask n :: 'a::len word) !! i \<longleftrightarrow> i < n \<and> i < size (mask n :: 'a word)\<close>
  3717   by (auto simp add: test_bit_word_eq word_size)
  3967   by (auto simp add: test_bit_word_eq word_size Word.bit_mask_iff)
  3718 
  3968 
  3719 lemma mask_bl: "mask n = of_bl (replicate n True)"
  3969 lemma mask_bl: "mask n = of_bl (replicate n True)"
  3720   by (auto simp add : test_bit_of_bl word_size intro: word_eqI)
  3970   by (auto simp add : test_bit_of_bl word_size intro: word_eqI)
  3721 
  3971 
  3722 lemma mask_bin: "mask n = word_of_int (bintrunc n (- 1))"
  3972 lemma mask_bin: "mask n = word_of_int (bintrunc n (- 1))"
  4445 
  4695 
  4446 subsection \<open>Rotation\<close>
  4696 subsection \<open>Rotation\<close>
  4447 
  4697 
  4448 lemmas rotater_0' [simp] = rotater_def [where n = "0", simplified]
  4698 lemmas rotater_0' [simp] = rotater_def [where n = "0", simplified]
  4449 
  4699 
       
  4700 lemma bit_word_rotl_iff:
       
  4701   \<open>bit (word_rotl m w) n \<longleftrightarrow>
       
  4702     n < LENGTH('a) \<and> bit w ((n + (LENGTH('a) - m mod LENGTH('a))) mod LENGTH('a))\<close>
       
  4703   for w :: \<open>'a::len word\<close>
       
  4704 proof (cases \<open>n < LENGTH('a)\<close>)
       
  4705   case False
       
  4706   then show ?thesis
       
  4707     by (auto dest: bit_imp_le_length)
       
  4708 next
       
  4709   case True
       
  4710   define k where \<open>k = int n - int m\<close>
       
  4711   then have k: \<open>int n = k + int m\<close>
       
  4712     by simp
       
  4713   define l where \<open>l = int LENGTH('a)\<close>
       
  4714   then have l: \<open>int LENGTH('a) = l\<close> \<open>l > 0\<close>
       
  4715     by simp_all
       
  4716   have *: \<open>int (m - n) = int m - int n\<close> if \<open>n \<le> m\<close> for n m
       
  4717     using that by (simp add: int_minus)
       
  4718   from \<open>l > 0\<close> have \<open>l = 1 + (k mod l + ((- 1 - k) mod l))\<close>
       
  4719     using minus_mod_int_eq [of l \<open>k + 1\<close>] by (simp add: algebra_simps)
       
  4720   then have \<open>int (LENGTH('a) - Suc ((m + LENGTH('a) - Suc n) mod LENGTH('a))) =
       
  4721     int ((n + LENGTH('a) - m mod LENGTH('a)) mod LENGTH('a))\<close>
       
  4722     by (simp add: * k l zmod_int Suc_leI trans_le_add2 algebra_simps mod_simps \<open>n < LENGTH('a)\<close>)
       
  4723   then have \<open>LENGTH('a) - Suc ((m + LENGTH('a) - Suc n) mod LENGTH('a)) =
       
  4724     (n + LENGTH('a) - m mod LENGTH('a)) mod LENGTH('a)\<close>
       
  4725     by simp
       
  4726   with True show ?thesis
       
  4727     by (simp add: word_rotl_def bit_of_bl_iff rev_nth nth_rotate nth_to_bl)
       
  4728 qed
       
  4729 
  4450 lemmas word_rot_defs = word_roti_def word_rotr_def word_rotl_def
  4730 lemmas word_rot_defs = word_roti_def word_rotr_def word_rotl_def
  4451 
  4731 
  4452 lemma rotate_eq_mod: "m mod length xs = n mod length xs \<Longrightarrow> rotate m xs = rotate n xs"
  4732 lemma rotate_eq_mod: "m mod length xs = n mod length xs \<Longrightarrow> rotate m xs = rotate n xs"
  4453   apply (rule box_equals)
  4733   apply (rule box_equals)
  4454     defer
  4734     defer
  4495   by (auto simp: rotater_rev rotate_drop_take rev_take rev_drop)
  4775   by (auto simp: rotater_rev rotate_drop_take rev_take rev_drop)
  4496 
  4776 
  4497 lemma rotater_Suc [simp]: "rotater (Suc n) xs = rotater1 (rotater n xs)"
  4777 lemma rotater_Suc [simp]: "rotater (Suc n) xs = rotater1 (rotater n xs)"
  4498   unfolding rotater_def by auto
  4778   unfolding rotater_def by auto
  4499 
  4779 
  4500 lemma rotate_inv_plus [rule_format] :
  4780 lemma nth_rotater:
       
  4781   \<open>rotater m xs ! n = xs ! ((n + (length xs - m mod length xs)) mod length xs)\<close> if \<open>n < length xs\<close>
       
  4782   using that by (simp add: rotater_drop_take nth_append not_less less_diff_conv ac_simps le_mod_geq)
       
  4783 
       
  4784 lemma nth_rotater1:
       
  4785   \<open>rotater1 xs ! n = xs ! ((n + (length xs - 1)) mod length xs)\<close> if \<open>n < length xs\<close>
       
  4786   using that nth_rotater [of n xs 1] by simp
       
  4787 
       
  4788 lemma rotate_inv_plus [rule_format]:
  4501   "\<forall>k. k = m + n \<longrightarrow> rotater k (rotate n xs) = rotater m xs \<and>
  4789   "\<forall>k. k = m + n \<longrightarrow> rotater k (rotate n xs) = rotater m xs \<and>
  4502     rotate k (rotater n xs) = rotate m xs \<and>
  4790     rotate k (rotater n xs) = rotate m xs \<and>
  4503     rotater n (rotate k xs) = rotate m xs \<and>
  4791     rotater n (rotate k xs) = rotate m xs \<and>
  4504     rotate n (rotater k xs) = rotater m xs"
  4792     rotate n (rotater k xs) = rotater m xs"
  4505   by (induct n) (auto simp: rotater_def rotate_def intro: funpow_swap1 [THEN trans])
  4793   by (induct n) (auto simp: rotater_def rotate_def intro: funpow_swap1 [THEN trans])
  4517 lemma rotate_gal': "ys = rotater n xs \<longleftrightarrow> xs = rotate n ys"
  4805 lemma rotate_gal': "ys = rotater n xs \<longleftrightarrow> xs = rotate n ys"
  4518   by auto
  4806   by auto
  4519 
  4807 
  4520 lemma length_rotater [simp]: "length (rotater n xs) = length xs"
  4808 lemma length_rotater [simp]: "length (rotater n xs) = length xs"
  4521   by (simp add : rotater_rev)
  4809   by (simp add : rotater_rev)
       
  4810 
       
  4811 lemma bit_word_rotr_iff:
       
  4812   \<open>bit (word_rotr m w) n \<longleftrightarrow>
       
  4813     n < LENGTH('a) \<and> bit w ((n + m) mod LENGTH('a))\<close>
       
  4814   for w :: \<open>'a::len word\<close>
       
  4815 proof (cases \<open>n < LENGTH('a)\<close>)
       
  4816   case False
       
  4817   then show ?thesis
       
  4818     by (auto dest: bit_imp_le_length)
       
  4819 next
       
  4820   case True
       
  4821   define k where \<open>k = int n + int m\<close>
       
  4822   then have k: \<open>int n = k - int m\<close>
       
  4823     by simp
       
  4824   define l where \<open>l = int LENGTH('a)\<close>
       
  4825   then have l: \<open>int LENGTH('a) = l\<close> \<open>l > 0\<close>
       
  4826     by simp_all
       
  4827   have *: \<open>int (m - n) = int m - int n\<close> if \<open>n \<le> m\<close> for n m
       
  4828     using that by (simp add: int_minus)
       
  4829   have \<open>int ((LENGTH('a)
       
  4830     - Suc ((LENGTH('a) + LENGTH('a) - Suc (n + m mod LENGTH('a))) mod LENGTH('a)))) =
       
  4831     int ((n + m) mod LENGTH('a))\<close>
       
  4832     using True
       
  4833     apply (simp add: l * zmod_int Suc_leI add_strict_mono)
       
  4834     apply (subst mod_diff_left_eq [symmetric])
       
  4835     apply simp
       
  4836     using l minus_mod_int_eq [of l \<open>int n + int m mod l + 1\<close>] apply simp
       
  4837     apply (simp add: mod_simps)
       
  4838     done
       
  4839   then have \<open>(LENGTH('a)
       
  4840     - Suc ((LENGTH('a) + LENGTH('a) - Suc (n + m mod LENGTH('a))) mod LENGTH('a))) =
       
  4841     ((n + m) mod LENGTH('a))\<close>
       
  4842     by simp
       
  4843   with True show ?thesis
       
  4844     by (simp add: word_rotr_def bit_of_bl_iff rev_nth nth_rotater nth_to_bl)
       
  4845 qed
       
  4846 
       
  4847 lemma bit_word_roti_iff:
       
  4848   \<open>bit (word_roti k w) n \<longleftrightarrow>
       
  4849     n < LENGTH('a) \<and> bit w (nat ((int n + k) mod int LENGTH('a)))\<close>
       
  4850   for w :: \<open>'a::len word\<close>
       
  4851 proof (cases \<open>k \<ge> 0\<close>)
       
  4852   case True
       
  4853   moreover define m where \<open>m = nat k\<close>
       
  4854   ultimately have \<open>k = int m\<close>
       
  4855     by simp
       
  4856   then show ?thesis
       
  4857     by (simp add: word_roti_def bit_word_rotr_iff nat_mod_distrib nat_add_distrib)
       
  4858 next
       
  4859   have *: \<open>int (m - n) = int m - int n\<close> if \<open>n \<le> m\<close> for n m
       
  4860     using that by (simp add: int_minus)
       
  4861   case False
       
  4862   moreover define m where \<open>m = nat (- k)\<close>
       
  4863   ultimately have \<open>k = - int m\<close> \<open>k < 0\<close>
       
  4864     by simp_all
       
  4865   moreover have \<open>(int n - int m) mod int LENGTH('a) =
       
  4866     int ((n + LENGTH('a) - m mod LENGTH('a)) mod LENGTH('a))\<close>
       
  4867     apply (simp add: zmod_int * trans_le_add2 mod_simps)
       
  4868     apply (metis mod_add_self2 mod_diff_cong)
       
  4869     done
       
  4870   ultimately show ?thesis
       
  4871     by (simp add: word_roti_def bit_word_rotl_iff nat_mod_distrib)
       
  4872 qed
  4522 
  4873 
  4523 lemma restrict_to_left: "x = y \<Longrightarrow> x = z \<longleftrightarrow> y = z"
  4874 lemma restrict_to_left: "x = y \<Longrightarrow> x = z \<longleftrightarrow> y = z"
  4524   by simp
  4875   by simp
  4525 
  4876 
  4526 lemmas rrs0 = rotate_eqs [THEN restrict_to_left,
  4877 lemmas rrs0 = rotate_eqs [THEN restrict_to_left,
  5171   shows "(pcr_word ===> (=) ===> pcr_word) (<<) (<<)"
  5522   shows "(pcr_word ===> (=) ===> pcr_word) (<<) (<<)"
  5172   by (auto intro!: rel_funI word_eqI simp add: word.pcr_cr_eq cr_word_def word_size nth_shiftl)
  5523   by (auto intro!: rel_funI word_eqI simp add: word.pcr_cr_eq cr_word_def word_size nth_shiftl)
  5173 
  5524 
  5174 lemma uint_shiftl:
  5525 lemma uint_shiftl:
  5175   "uint (n << i) = bintrunc (size n) (uint n << i)"
  5526   "uint (n << i) = bintrunc (size n) (uint n << i)"
  5176   unfolding word_size by transfer (simp add: bintrunc_shiftl)
  5527   apply (simp add: word_size shiftl_eq_push_bit shiftl_word_eq)
       
  5528   apply transfer
       
  5529   apply (simp add: push_bit_take_bit)
       
  5530   done
  5177 
  5531 
  5178 
  5532 
  5179 subsection \<open>Misc\<close>
  5533 subsection \<open>Misc\<close>
  5180 
  5534 
  5181 declare bin_to_bl_def [simp]
  5535 declare bin_to_bl_def [simp]