src/HOL/Word/Word.thy
changeset 71957 3e162c63371a
parent 71955 a9f913d17d00
child 71958 4320875eb8a1
equal deleted inserted replaced
71956:a4bffc0de967 71957:3e162c63371a
     6 
     6 
     7 theory Word
     7 theory Word
     8 imports
     8 imports
     9   "HOL-Library.Type_Length"
     9   "HOL-Library.Type_Length"
    10   "HOL-Library.Boolean_Algebra"
    10   "HOL-Library.Boolean_Algebra"
       
    11   "HOL-Library.Bit_Operations"
    11   Bits_Int
    12   Bits_Int
    12   Bits_Z2
       
    13   Bit_Comprehension
    13   Bit_Comprehension
    14   Misc_Typedef
    14   Misc_Typedef
    15   Misc_Arithmetic
    15   Misc_Arithmetic
    16 begin
    16 begin
    17 
    17 
   465 
   465 
   466 instance
   466 instance
   467   by (standard; transfer) auto
   467   by (standard; transfer) auto
   468 
   468 
   469 end
   469 end
       
   470 
       
   471 interpretation word_order: ordering_top \<open>(\<le>)\<close> \<open>(<)\<close> \<open>- 1 :: 'a::len word\<close>
       
   472   by (standard; transfer) (simp add: take_bit_eq_mod zmod_minus1)
       
   473 
       
   474 interpretation word_coorder: ordering_top \<open>(\<ge>)\<close> \<open>(>)\<close> \<open>0 :: 'a::len word\<close>
       
   475   by (standard; transfer) simp
   470 
   476 
   471 lemma word_le_def [code]:
   477 lemma word_le_def [code]:
   472   "a \<le> b \<longleftrightarrow> uint a \<le> uint b"
   478   "a \<le> b \<longleftrightarrow> uint a \<le> uint b"
   473   by transfer rule
   479   by transfer rule
   474 
   480 
   794   apply (simp add: fun_eq_iff shiftr1_def)
   800   apply (simp add: fun_eq_iff shiftr1_def)
   795   apply transfer
   801   apply transfer
   796   apply (auto simp add: not_le dest: less_2_cases)
   802   apply (auto simp add: not_le dest: less_2_cases)
   797   done
   803   done
   798 
   804 
       
   805 instantiation word :: (len) ring_bit_operations
       
   806 begin
       
   807 
       
   808 lift_definition not_word :: \<open>'a word \<Rightarrow> 'a word\<close>
       
   809   is not
       
   810   by (simp add: take_bit_not_iff)
       
   811 
       
   812 lift_definition and_word :: \<open>'a word \<Rightarrow> 'a word \<Rightarrow> 'a word\<close>
       
   813   is \<open>and\<close>
       
   814   by simp
       
   815 
       
   816 lift_definition or_word :: \<open>'a word \<Rightarrow> 'a word \<Rightarrow> 'a word\<close>
       
   817   is or
       
   818   by simp
       
   819 
       
   820 lift_definition xor_word ::  \<open>'a word \<Rightarrow> 'a word \<Rightarrow> 'a word\<close>
       
   821   is xor
       
   822   by simp
       
   823 
       
   824 instance proof
       
   825   fix a b :: \<open>'a word\<close> and n :: nat
       
   826   show \<open>- a = NOT (a - 1)\<close>
       
   827     by transfer (simp add: minus_eq_not_minus_1)
       
   828   show \<open>bit (NOT a) n \<longleftrightarrow> (2 :: 'a word) ^ n \<noteq> 0 \<and> \<not> bit a n\<close>
       
   829     by transfer (simp add: bit_not_iff)
       
   830   show \<open>bit (a AND b) n \<longleftrightarrow> bit a n \<and> bit b n\<close>
       
   831     by transfer (auto simp add: bit_and_iff)
       
   832   show \<open>bit (a OR b) n \<longleftrightarrow> bit a n \<or> bit b n\<close>
       
   833     by transfer (auto simp add: bit_or_iff)
       
   834   show \<open>bit (a XOR b) n \<longleftrightarrow> bit a n \<noteq> bit b n\<close>
       
   835     by transfer (auto simp add: bit_xor_iff)
       
   836 qed
       
   837 
       
   838 end
       
   839 
   799 instantiation word :: (len) bit_operations
   840 instantiation word :: (len) bit_operations
   800 begin
   841 begin
   801 
       
   802 lift_definition bitNOT_word :: "'a word \<Rightarrow> 'a word" is NOT
       
   803   by (metis bin_trunc_not)
       
   804 
       
   805 lift_definition bitAND_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word" is \<open>(AND)\<close>
       
   806   by (metis bin_trunc_and)
       
   807 
       
   808 lift_definition bitOR_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word" is \<open>(OR)\<close>
       
   809   by (metis bin_trunc_or)
       
   810 
       
   811 lift_definition bitXOR_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word" is \<open>(XOR)\<close>
       
   812   by (metis bin_trunc_xor)
       
   813 
   842 
   814 definition word_test_bit_def: "test_bit a = bin_nth (uint a)"
   843 definition word_test_bit_def: "test_bit a = bin_nth (uint a)"
   815 
   844 
   816 definition word_set_bit_def: "set_bit a n x = word_of_int (bin_sc n x (uint a))"
   845 definition word_set_bit_def: "set_bit a n x = word_of_int (bin_sc n x (uint a))"
   817 
   846 
   875 lemma [code]:
   904 lemma [code]:
   876   shows word_not_def: "NOT (a::'a::len word) = word_of_int (NOT (uint a))"
   905   shows word_not_def: "NOT (a::'a::len word) = word_of_int (NOT (uint a))"
   877     and word_and_def: "(a::'a word) AND b = word_of_int (uint a AND uint b)"
   906     and word_and_def: "(a::'a word) AND b = word_of_int (uint a AND uint b)"
   878     and word_or_def: "(a::'a word) OR b = word_of_int (uint a OR uint b)"
   907     and word_or_def: "(a::'a word) OR b = word_of_int (uint a OR uint b)"
   879     and word_xor_def: "(a::'a word) XOR b = word_of_int (uint a XOR uint b)"
   908     and word_xor_def: "(a::'a word) XOR b = word_of_int (uint a XOR uint b)"
   880   by (simp_all flip: bitNOT_word.abs_eq bitAND_word.abs_eq
   909   by (transfer, simp add: take_bit_not_take_bit)+
   881      bitOR_word.abs_eq bitXOR_word.abs_eq)
       
   882 
   910 
   883 definition setBit :: "'a::len word \<Rightarrow> nat \<Rightarrow> 'a word"
   911 definition setBit :: "'a::len word \<Rightarrow> nat \<Rightarrow> 'a word"
   884   where "setBit w n = set_bit w n True"
   912   where "setBit w n = set_bit w n True"
   885 
   913 
   886 definition clearBit :: "'a::len word \<Rightarrow> nat \<Rightarrow> 'a word"
   914 definition clearBit :: "'a::len word \<Rightarrow> nat \<Rightarrow> 'a word"
   887   where "clearBit w n = set_bit w n False"
   915   where "clearBit w n = set_bit w n False"
       
   916 
       
   917 definition even_word :: \<open>'a::len word \<Rightarrow> bool\<close>
       
   918   where [code_abbrev]: \<open>even_word = even\<close>
       
   919 
       
   920 lemma even_word_iff [code]:
       
   921   \<open>even_word a \<longleftrightarrow> a AND 1 = 0\<close>
       
   922   by (simp add: and_one_eq even_iff_mod_2_eq_zero even_word_def)
       
   923 
       
   924 definition bit_word :: \<open>'a::len word \<Rightarrow> nat \<Rightarrow> bool\<close>
       
   925   where [code_abbrev]: \<open>bit_word = bit\<close>
       
   926 
       
   927 lemma bit_word_iff [code]:
       
   928   \<open>bit_word a n \<longleftrightarrow> drop_bit n a AND 1 = 1\<close>
       
   929   by (simp add: bit_word_def bit_iff_odd_drop_bit odd_iff_mod_2_eq_one and_one_eq)
   888 
   930 
   889 
   931 
   890 subsection \<open>Shift operations\<close>
   932 subsection \<open>Shift operations\<close>
   891 
   933 
   892 definition sshiftr1 :: "'a::len word \<Rightarrow> 'a word"
   934 definition sshiftr1 :: "'a::len word \<Rightarrow> 'a word"
  1392 text \<open>
  1434 text \<open>
  1393   \<open>cast\<close> -- note, no arg for new length, as it's determined by type of result,
  1435   \<open>cast\<close> -- note, no arg for new length, as it's determined by type of result,
  1394   thus in \<open>cast w = w\<close>, the type means cast to length of \<open>w\<close>!
  1436   thus in \<open>cast w = w\<close>, the type means cast to length of \<open>w\<close>!
  1395 \<close>
  1437 \<close>
  1396 
  1438 
       
  1439 lemma bit_ucast_iff:
       
  1440   \<open>Parity.bit (ucast a :: 'a::len word) n \<longleftrightarrow> n < LENGTH('a::len) \<and> Parity.bit a n\<close>
       
  1441   by (simp add: ucast_def, transfer) (auto simp add: bit_take_bit_iff)
       
  1442 
  1397 lemma ucast_id: "ucast w = w"
  1443 lemma ucast_id: "ucast w = w"
  1398   by (auto simp: ucast_def)
  1444   by (auto simp: ucast_def)
  1399 
  1445 
  1400 lemma scast_id: "scast w = w"
  1446 lemma scast_id: "scast w = w"
  1401   by (auto simp: scast_def)
  1447   by (auto simp: scast_def)
  1404   by (auto simp: ucast_def of_bl_def uint_bl word_size)
  1450   by (auto simp: ucast_def of_bl_def uint_bl word_size)
  1405 
  1451 
  1406 lemma nth_ucast: "(ucast w::'a::len word) !! n = (w !! n \<and> n < LENGTH('a))"
  1452 lemma nth_ucast: "(ucast w::'a::len word) !! n = (w !! n \<and> n < LENGTH('a))"
  1407   by (simp add: ucast_def test_bit_bin word_ubin.eq_norm nth_bintr word_size)
  1453   by (simp add: ucast_def test_bit_bin word_ubin.eq_norm nth_bintr word_size)
  1408     (fast elim!: bin_nth_uint_imp)
  1454     (fast elim!: bin_nth_uint_imp)
       
  1455 
       
  1456 lemma ucast_mask_eq:
       
  1457   \<open>ucast (Bit_Operations.mask n :: 'b word) = Bit_Operations.mask (min LENGTH('b::len) n)\<close>
       
  1458   by (simp add: bit_eq_iff) (auto simp add: bit_mask_iff bit_ucast_iff exp_eq_zero_iff)
  1409 
  1459 
  1410 \<comment> \<open>literal u(s)cast\<close>
  1460 \<comment> \<open>literal u(s)cast\<close>
  1411 lemma ucast_bintr [simp]:
  1461 lemma ucast_bintr [simp]:
  1412   "ucast (numeral w :: 'a::len word) =
  1462   "ucast (numeral w :: 'a::len word) =
  1413     word_of_int (bintrunc (LENGTH('a)) (numeral w))"
  1463     word_of_int (bintrunc (LENGTH('a)) (numeral w))"
  1750 lemma word_m1_ge [simp] : "word_pred 0 \<ge> y" (* FIXME: delete *)
  1800 lemma word_m1_ge [simp] : "word_pred 0 \<ge> y" (* FIXME: delete *)
  1751   by (simp only: word_le_def word_pred_0_n1 word_uint.eq_norm m1mod2k) auto
  1801   by (simp only: word_le_def word_pred_0_n1 word_uint.eq_norm m1mod2k) auto
  1752 
  1802 
  1753 lemma word_n1_ge [simp]: "y \<le> -1"
  1803 lemma word_n1_ge [simp]: "y \<le> -1"
  1754   for y :: "'a::len word"
  1804   for y :: "'a::len word"
  1755   by (simp only: word_le_def word_m1_wi word_uint.eq_norm m1mod2k) auto
  1805   by (fact word_order.extremum)
  1756 
  1806 
  1757 lemmas word_not_simps [simp] =
  1807 lemmas word_not_simps [simp] =
  1758   word_zero_le [THEN leD] word_m1_ge [THEN leD] word_n1_ge [THEN leD]
  1808   word_zero_le [THEN leD] word_m1_ge [THEN leD] word_n1_ge [THEN leD]
  1759 
  1809 
  1760 lemma word_gt_0: "0 < y \<longleftrightarrow> 0 \<noteq> y"
  1810 lemma word_gt_0: "0 < y \<longleftrightarrow> 0 \<noteq> y"
  2674   "x OR (-1::'a::len word) = -1"
  2724   "x OR (-1::'a::len word) = -1"
  2675   " (-1::'a::len word) XOR x = NOT x"
  2725   " (-1::'a::len word) XOR x = NOT x"
  2676   "x XOR (-1::'a::len word) = NOT x"
  2726   "x XOR (-1::'a::len word) = NOT x"
  2677   by (transfer, simp)+
  2727   by (transfer, simp)+
  2678 
  2728 
  2679 lemma uint_or: "uint (x OR y) = uint x OR uint y"
  2729 lemma uint_and:
  2680   by (transfer, simp add: bin_trunc_ao)
  2730   \<open>uint (x AND y) = uint x AND uint y\<close>
  2681 
  2731   by transfer simp
  2682 lemma uint_and: "uint (x AND y) = uint x AND uint y"
  2732 
  2683   by (transfer, simp add: bin_trunc_ao)
  2733 lemma uint_or:
       
  2734   \<open>uint (x OR y) = uint x OR uint y\<close>
       
  2735   by transfer simp
       
  2736 
       
  2737 lemma uint_xor:
       
  2738   \<open>uint (x XOR y) = uint x XOR uint y\<close>
       
  2739   by transfer simp
  2684 
  2740 
  2685 lemma test_bit_wi [simp]:
  2741 lemma test_bit_wi [simp]:
  2686   "(word_of_int x :: 'a::len word) !! n \<longleftrightarrow> n < LENGTH('a) \<and> bin_nth x n"
  2742   "(word_of_int x :: 'a::len word) !! n \<longleftrightarrow> n < LENGTH('a) \<and> bin_nth x n"
  2687   by (simp add: word_test_bit_def word_ubin.eq_norm nth_bintr)
  2743   by (simp add: word_test_bit_def word_ubin.eq_norm nth_bintr)
  2688 
  2744 
  2749   "y OR x OR z = x OR y OR z"
  2805   "y OR x OR z = x OR y OR z"
  2750   "y XOR x XOR z = x XOR y XOR z"
  2806   "y XOR x XOR z = x XOR y XOR z"
  2751   for x :: "'a::len word"
  2807   for x :: "'a::len word"
  2752   by (auto simp: word_eq_iff word_ops_nth_size [unfolded word_size])
  2808   by (auto simp: word_eq_iff word_ops_nth_size [unfolded word_size])
  2753 
  2809 
  2754 lemma word_log_esimps [simp]:
  2810 lemma word_log_esimps:
  2755   "x AND 0 = 0"
  2811   "x AND 0 = 0"
  2756   "x AND -1 = x"
  2812   "x AND -1 = x"
  2757   "x OR 0 = x"
  2813   "x OR 0 = x"
  2758   "x OR -1 = -1"
  2814   "x OR -1 = -1"
  2759   "x XOR 0 = x"
  2815   "x XOR 0 = x"
  2763   "0 OR x = x"
  2819   "0 OR x = x"
  2764   "-1 OR x = -1"
  2820   "-1 OR x = -1"
  2765   "0 XOR x = x"
  2821   "0 XOR x = x"
  2766   "-1 XOR x = NOT x"
  2822   "-1 XOR x = NOT x"
  2767   for x :: "'a::len word"
  2823   for x :: "'a::len word"
  2768   by (auto simp: word_eq_iff word_ops_nth_size [unfolded word_size])
  2824   by simp_all
  2769 
  2825 
  2770 lemma word_not_dist:
  2826 lemma word_not_dist:
  2771   "NOT (x OR y) = NOT x AND NOT y"
  2827   "NOT (x OR y) = NOT x AND NOT y"
  2772   "NOT (x AND y) = NOT x OR NOT y"
  2828   "NOT (x AND y) = NOT x OR NOT y"
  2773   for x :: "'a::len word"
  2829   for x :: "'a::len word"
  2774   by (auto simp: word_eq_iff word_ops_nth_size [unfolded word_size])
  2830   by simp_all
  2775 
  2831 
  2776 lemma word_bw_same:
  2832 lemma word_bw_same:
  2777   "x AND x = x"
  2833   "x AND x = x"
  2778   "x OR x = x"
  2834   "x OR x = x"
  2779   "x XOR x = 0"
  2835   "x XOR x = 0"
  2780   for x :: "'a::len word"
  2836   for x :: "'a::len word"
  2781   by (auto simp: word_eq_iff word_ops_nth_size [unfolded word_size])
  2837   by simp_all
  2782 
  2838 
  2783 lemma word_ao_absorbs [simp]:
  2839 lemma word_ao_absorbs [simp]:
  2784   "x AND (y OR x) = x"
  2840   "x AND (y OR x) = x"
  2785   "x OR y AND x = x"
  2841   "x OR y AND x = x"
  2786   "x AND (x OR y) = x"
  2842   "x AND (x OR y) = x"
  2792   for x :: "'a::len word"
  2848   for x :: "'a::len word"
  2793   by (auto simp: word_eq_iff word_ops_nth_size [unfolded word_size])
  2849   by (auto simp: word_eq_iff word_ops_nth_size [unfolded word_size])
  2794 
  2850 
  2795 lemma word_not_not [simp]: "NOT (NOT x) = x"
  2851 lemma word_not_not [simp]: "NOT (NOT x) = x"
  2796   for x :: "'a::len word"
  2852   for x :: "'a::len word"
  2797   by (auto simp: word_eq_iff word_ops_nth_size [unfolded word_size])
  2853   by simp
  2798 
  2854 
  2799 lemma word_ao_dist: "(x OR y) AND z = x AND z OR y AND z"
  2855 lemma word_ao_dist: "(x OR y) AND z = x AND z OR y AND z"
  2800   for x :: "'a::len word"
  2856   for x :: "'a::len word"
  2801   by (auto simp: word_eq_iff word_ops_nth_size [unfolded word_size])
  2857   by (auto simp: word_eq_iff word_ops_nth_size [unfolded word_size])
  2802 
  2858 
  3564   done
  3620   done
  3565 
  3621 
  3566 lemma aligned_bl_add_size [OF refl]:
  3622 lemma aligned_bl_add_size [OF refl]:
  3567   "size x - n = m \<Longrightarrow> n \<le> size x \<Longrightarrow> drop m (to_bl x) = replicate n False \<Longrightarrow>
  3623   "size x - n = m \<Longrightarrow> n \<le> size x \<Longrightarrow> drop m (to_bl x) = replicate n False \<Longrightarrow>
  3568     take m (to_bl y) = replicate m False \<Longrightarrow>
  3624     take m (to_bl y) = replicate m False \<Longrightarrow>
  3569     to_bl (x + y) = take m (to_bl x) @ drop m (to_bl y)"
  3625     to_bl (x + y) = take m (to_bl x) @ drop m (to_bl y)" for x :: \<open>'a::len word\<close>
  3570   apply (subgoal_tac "x AND y = 0")
  3626   apply (subgoal_tac "x AND y = 0")
  3571    prefer 2
  3627    prefer 2
  3572    apply (rule word_bl.Rep_eqD)
  3628    apply (rule word_bl.Rep_eqD)
  3573    apply (simp add: bl_word_and)
  3629    apply (simp add: bl_word_and)
  3574    apply (rule align_lem_and [THEN trans])
  3630    apply (rule align_lem_and [THEN trans])
  3581   done
  3637   done
  3582 
  3638 
  3583 
  3639 
  3584 subsubsection \<open>Mask\<close>
  3640 subsubsection \<open>Mask\<close>
  3585 
  3641 
       
  3642 lemma minus_1_eq_mask:
       
  3643   \<open>- 1 = (Bit_Operations.mask LENGTH('a) :: 'a::len word)\<close>
       
  3644   by (rule bit_eqI) (simp add: bit_exp_iff bit_mask_iff exp_eq_zero_iff)
       
  3645   
       
  3646 lemma mask_eq_mask:
       
  3647   \<open>mask = Bit_Operations.mask\<close>
       
  3648   by (simp add: fun_eq_iff mask_eq_exp_minus_1 mask_def shiftl_word_eq push_bit_eq_mult)
       
  3649 
  3586 lemma mask_eq:
  3650 lemma mask_eq:
  3587   \<open>mask n = 2 ^ n - 1\<close>
  3651   \<open>mask n = 2 ^ n - 1\<close>
  3588   by (simp add: mask_def shiftl_word_eq push_bit_eq_mult)
  3652   by (simp add: mask_eq_mask mask_eq_exp_minus_1)
  3589 
  3653 
  3590 lemma mask_Suc_rec:
  3654 lemma mask_Suc_rec:
  3591   \<open>mask (Suc n) = 2 * mask n + 1\<close>
  3655   \<open>mask (Suc n) = 2 * mask n + 1\<close>
  3592   by (simp add: mask_eq)
  3656   by (simp add: mask_eq)
  3593 
  3657 
  3594 context
  3658 context
  3595 begin
  3659 begin
  3596 
  3660 
  3597 qualified lemma bit_mask_iff [simp]:
  3661 qualified lemma bit_mask_iff [simp]:
  3598   \<open>bit (mask m :: 'a::len word) n \<longleftrightarrow> n < LENGTH('a) \<and> n < m\<close>
  3662   \<open>bit (mask m :: 'a::len word) n \<longleftrightarrow> n < LENGTH('a) \<and> n < m\<close>
  3599   by (simp add: mask_eq bit_mask_iff exp_eq_zero_iff not_le)
  3663   by (simp add: mask_eq_mask bit_mask_iff exp_eq_zero_iff not_le)
  3600 
  3664 
  3601 end
  3665 end
  3602 
  3666 
  3603 lemma nth_mask [simp]:
  3667 lemma nth_mask [simp]:
  3604   \<open>(mask n :: 'a::len word) !! i \<longleftrightarrow> i < n \<and> i < size (mask n :: 'a word)\<close>
  3668   \<open>(mask n :: 'a::len word) !! i \<longleftrightarrow> i < n \<and> i < size (mask n :: 'a word)\<close>
  4693   fixes x :: "'a::len word"
  4757   fixes x :: "'a::len word"
  4694   obtains n where "x = of_nat n" and "n < 2^LENGTH('a)"
  4758   obtains n where "x = of_nat n" and "n < 2^LENGTH('a)"
  4695   by (cases x rule: word_unat.Abs_cases) (simp add: unats_def)
  4759   by (cases x rule: word_unat.Abs_cases) (simp add: unats_def)
  4696 
  4760 
  4697 lemma max_word_max [intro!]: "n \<le> max_word"
  4761 lemma max_word_max [intro!]: "n \<le> max_word"
  4698   by (fact word_n1_ge)
  4762   by (fact word_order.extremum)
  4699 
  4763 
  4700 lemma word_of_int_2p_len: "word_of_int (2 ^ LENGTH('a)) = (0::'a::len word)"
  4764 lemma word_of_int_2p_len: "word_of_int (2 ^ LENGTH('a)) = (0::'a::len word)"
  4701   by (subst word_uint.Abs_norm [symmetric]) simp
  4765   by (subst word_uint.Abs_norm [symmetric]) simp
  4702 
  4766 
  4703 lemma word_pow_0: "(2::'a::len word) ^ LENGTH('a) = 0"
  4767 lemma word_pow_0: "(2::'a::len word) ^ LENGTH('a) = 0"
  4704 proof -
  4768   by (fact word_exp_length_eq_0)
  4705   have "word_of_int (2 ^ LENGTH('a)) = (0::'a word)"
       
  4706     by (rule word_of_int_2p_len)
       
  4707   then show ?thesis by (simp add: word_of_int_2p)
       
  4708 qed
       
  4709 
  4769 
  4710 lemma max_word_wrap: "x + 1 = 0 \<Longrightarrow> x = max_word"
  4770 lemma max_word_wrap: "x + 1 = 0 \<Longrightarrow> x = max_word"
  4711   by (simp add: eq_neg_iff_add_eq_0)
  4771   by (simp add: eq_neg_iff_add_eq_0)
  4712 
  4772 
  4713 lemma max_word_bl: "to_bl (max_word::'a::len word) = replicate LENGTH('a) True"
  4773 lemma max_word_bl: "to_bl (max_word::'a::len word) = replicate LENGTH('a) True"
  4734   for x :: "'a::len word"
  4794   for x :: "'a::len word"
  4735   by (rule word_eqI) (auto simp add: word_ops_nth_size word_size)
  4795   by (rule word_eqI) (auto simp add: word_ops_nth_size word_size)
  4736 
  4796 
  4737 lemma word_or_not [simp]: "x OR NOT x = max_word"
  4797 lemma word_or_not [simp]: "x OR NOT x = max_word"
  4738   by (rule word_eqI) (auto simp add: word_ops_nth_size word_size)
  4798   by (rule word_eqI) (auto simp add: word_ops_nth_size word_size)
  4739 
       
  4740 global_interpretation word_bool_alg: boolean_algebra
       
  4741   where conj = "(AND)" and disj = "(OR)" and compl = NOT
       
  4742     and zero = 0 and one = \<open>- 1 :: 'a::len word\<close>
       
  4743   rewrites "word_bool_alg.xor = (XOR)"
       
  4744 proof -
       
  4745   interpret boolean_algebra
       
  4746     where conj = "(AND)" and disj = "(OR)" and compl = NOT
       
  4747       and zero = 0 and one = \<open>- 1 :: 'a word\<close>
       
  4748     apply standard
       
  4749              apply (simp_all add: word_bw_assocs word_bw_comms word_bw_lcs)
       
  4750      apply (fact word_ao_dist2)
       
  4751     apply (fact word_oa_dist2)
       
  4752     done
       
  4753   show "boolean_algebra (AND) (OR) NOT 0 (- 1 :: 'a word)" ..
       
  4754   show "xor = (XOR)"
       
  4755     by (auto simp add: fun_eq_iff word_eq_iff xor_def word_ops_nth_size word_size)
       
  4756 qed
       
  4757 
  4799 
  4758 lemma word_xor_and_or: "x XOR y = x AND NOT y OR NOT x AND y"
  4800 lemma word_xor_and_or: "x XOR y = x AND NOT y OR NOT x AND y"
  4759   for x y :: "'a::len word"
  4801   for x y :: "'a::len word"
  4760   by (rule word_eqI) (auto simp add: word_ops_nth_size word_size)
  4802   by (rule word_eqI) (auto simp add: word_ops_nth_size word_size)
  4761 
  4803 
  5037 
  5079 
  5038 subsection \<open>More\<close>
  5080 subsection \<open>More\<close>
  5039 
  5081 
  5040 lemma test_bit_1' [simp]:
  5082 lemma test_bit_1' [simp]:
  5041   "(1 :: 'a :: len word) !! n \<longleftrightarrow> 0 < LENGTH('a) \<and> n = 0"
  5083   "(1 :: 'a :: len word) !! n \<longleftrightarrow> 0 < LENGTH('a) \<and> n = 0"
  5042   by (cases n) (simp_all only: one_word_def test_bit_wi, simp_all)
  5084   by simp
  5043 
  5085 
  5044 lemma mask_0 [simp]:
  5086 lemma mask_0 [simp]:
  5045   "mask 0 = 0"
  5087   "mask 0 = 0"
  5046   by (simp add: Word.mask_def)
  5088   by (simp add: Word.mask_def)
  5047 
  5089 
  5048 lemma shiftl0 [simp]:
  5090 lemma shiftl0:
  5049   "x << 0 = (x :: 'a :: len word)"
  5091   "x << 0 = (x :: 'a :: len word)"
  5050   by (metis shiftl_rev shiftr_x_0 word_rev_gal)
  5092   by (fact shiftl_x_0)
  5051 
  5093 
  5052 lemma mask_1: "mask 1 = 1"
  5094 lemma mask_1: "mask 1 = 1"
  5053   by (simp add: mask_def)
  5095   by (simp add: mask_def)
  5054 
  5096 
  5055 lemma mask_Suc_0: "mask (Suc 0) = 1"
  5097 lemma mask_Suc_0: "mask (Suc 0) = 1"