src/HOL/Library/Z2.thy
changeset 71988 ace45a11a45e
parent 71965 d45f5d4c41bd
child 72082 41393ecb57ac
equal deleted inserted replaced
71987:ec17263ec38d 71988:ace45a11a45e
    12   Note that in most cases \<^typ>\<open>bool\<close> is appropriate hen a binary type is needed; the
    12   Note that in most cases \<^typ>\<open>bool\<close> is appropriate hen a binary type is needed; the
    13   type provided here, for historical reasons named \<^text>\<open>bit\<close>, is only needed if proper
    13   type provided here, for historical reasons named \<^text>\<open>bit\<close>, is only needed if proper
    14   field operations are required.
    14   field operations are required.
    15 \<close>
    15 \<close>
    16 
    16 
    17 subsection \<open>Bits as a datatype\<close>
    17 typedef bit = \<open>UNIV :: bool set\<close> ..
    18 
    18 
    19 typedef bit = "UNIV :: bool set"
    19 instantiation bit :: zero_neq_one
    20   morphisms set Bit ..
    20 begin
    21 
    21 
    22 instantiation bit :: "{zero, one}"
    22 definition zero_bit :: bit
    23 begin
    23   where \<open>0 = Abs_bit False\<close>
    24 
    24 
    25 definition zero_bit_def: "0 = Bit False"
    25 definition one_bit :: bit
    26 
    26   where \<open>1 = Abs_bit True\<close>
    27 definition one_bit_def: "1 = Bit True"
    27 
    28 
    28 instance
    29 instance ..
    29   by standard (simp add: zero_bit_def one_bit_def Abs_bit_inject)
    30 
    30 
    31 end
    31 end
    32 
    32 
    33 old_rep_datatype "0::bit" "1::bit"
    33 free_constructors case_bit for \<open>0::bit\<close> | \<open>1::bit\<close>
    34 proof -
    34 proof -
    35   fix P :: "bit \<Rightarrow> bool"
    35   fix P :: bool
    36   fix x :: bit
    36   fix a :: bit
    37   assume "P 0" and "P 1"
    37   assume \<open>a = 0 \<Longrightarrow> P\<close> and \<open>a = 1 \<Longrightarrow> P\<close>
    38   then have "\<forall>b. P (Bit b)"
    38   then show P
    39     unfolding zero_bit_def one_bit_def
    39     by (cases a) (auto simp add: zero_bit_def one_bit_def Abs_bit_inject)
    40     by (simp add: all_bool_eq)
    40 qed simp
    41   then show "P x"
    41 
    42     by (induct x) simp
    42 lemma bit_not_zero_iff [simp]:
    43 next
    43   \<open>a \<noteq> 0 \<longleftrightarrow> a = 1\<close> for a :: bit
    44   show "(0::bit) \<noteq> (1::bit)"
    44   by (cases a) simp_all
    45     unfolding zero_bit_def one_bit_def
    45 
    46     by (simp add: Bit_inject)
    46 lemma bit_not_one_imp [simp]:
       
    47   \<open>a \<noteq> 1 \<longleftrightarrow> a = 0\<close> for a :: bit
       
    48   by (cases a) simp_all
       
    49 
       
    50 instantiation bit :: semidom_modulo
       
    51 begin
       
    52 
       
    53 definition plus_bit :: \<open>bit \<Rightarrow> bit \<Rightarrow> bit\<close>
       
    54   where \<open>a + b = Abs_bit (Rep_bit a \<noteq> Rep_bit b)\<close>
       
    55 
       
    56 definition minus_bit :: \<open>bit \<Rightarrow> bit \<Rightarrow> bit\<close>
       
    57   where [simp]: \<open>minus_bit = plus\<close>
       
    58 
       
    59 definition times_bit :: \<open>bit \<Rightarrow> bit \<Rightarrow> bit\<close>
       
    60   where \<open>a * b = Abs_bit (Rep_bit a \<and> Rep_bit b)\<close>
       
    61 
       
    62 definition divide_bit :: \<open>bit \<Rightarrow> bit \<Rightarrow> bit\<close>
       
    63   where [simp]: \<open>divide_bit = times\<close>
       
    64 
       
    65 definition modulo_bit :: \<open>bit \<Rightarrow> bit \<Rightarrow> bit\<close>
       
    66   where \<open>a mod b = Abs_bit (Rep_bit a \<and> \<not> Rep_bit b)\<close>
       
    67 
       
    68 instance
       
    69   by standard
       
    70     (auto simp flip: Rep_bit_inject
       
    71     simp add: zero_bit_def one_bit_def plus_bit_def times_bit_def modulo_bit_def Abs_bit_inverse Rep_bit_inverse)
       
    72 
       
    73 end
       
    74 
       
    75 lemma bit_2_eq_0 [simp]:
       
    76   \<open>2 = (0::bit)\<close>
       
    77   by (simp flip: one_add_one add: zero_bit_def plus_bit_def)
       
    78 
       
    79 instance bit :: semiring_parity
       
    80   apply standard
       
    81     apply (auto simp flip: Rep_bit_inject simp add: modulo_bit_def Abs_bit_inverse Rep_bit_inverse)
       
    82          apply (auto simp add: zero_bit_def one_bit_def Abs_bit_inverse Rep_bit_inverse)
       
    83   done
       
    84 
       
    85 lemma Abs_bit_eq_of_bool [code_abbrev]:
       
    86   \<open>Abs_bit = of_bool\<close>
       
    87   by (simp add: fun_eq_iff zero_bit_def one_bit_def)
       
    88 
       
    89 lemma Rep_bit_eq_odd:
       
    90   \<open>Rep_bit = odd\<close>
       
    91 proof -
       
    92   have \<open>\<not> Rep_bit 0\<close>
       
    93     by (simp only: zero_bit_def) (subst Abs_bit_inverse, auto)
       
    94   then show ?thesis
       
    95     by (auto simp flip: Rep_bit_inject simp add: fun_eq_iff)
    47 qed
    96 qed
    48 
    97 
    49 lemma Bit_set_eq [simp]: "Bit (set b) = b"
    98 lemma Rep_bit_iff_odd [code_abbrev]:
    50   by (fact set_inverse)
    99   \<open>Rep_bit b \<longleftrightarrow> odd b\<close>
    51 
   100   by (simp add: Rep_bit_eq_odd)
    52 lemma set_Bit_eq [simp]: "set (Bit P) = P"
   101 
    53   by (rule Bit_inverse) rule
   102 lemma Not_Rep_bit_iff_even [code_abbrev]:
       
   103   \<open>\<not> Rep_bit b \<longleftrightarrow> even b\<close>
       
   104   by (simp add: Rep_bit_eq_odd)
       
   105 
       
   106 lemma Not_Not_Rep_bit [code_unfold]:
       
   107   \<open>\<not> \<not> Rep_bit b \<longleftrightarrow> Rep_bit b\<close>
       
   108   by simp
       
   109 
       
   110 code_datatype \<open>0::bit\<close> \<open>1::bit\<close>
       
   111 
       
   112 lemma Abs_bit_code [code]:
       
   113   \<open>Abs_bit False = 0\<close>
       
   114   \<open>Abs_bit True = 1\<close>
       
   115   by (simp_all add: Abs_bit_eq_of_bool)
       
   116 
       
   117 lemma Rep_bit_code [code]:
       
   118   \<open>Rep_bit 0 \<longleftrightarrow> False\<close>
       
   119   \<open>Rep_bit 1 \<longleftrightarrow> True\<close>
       
   120   by (simp_all add: Rep_bit_eq_odd)
       
   121 
       
   122 context zero_neq_one
       
   123 begin
       
   124 
       
   125 abbreviation of_bit :: \<open>bit \<Rightarrow> 'a\<close>
       
   126   where \<open>of_bit b \<equiv> of_bool (odd b)\<close>
       
   127 
       
   128 end
    54 
   129 
    55 context
   130 context
    56 begin
   131 begin
    57 
   132 
    58 qualified lemma bit_eq_iff: "x = y \<longleftrightarrow> (set x \<longleftrightarrow> set y)"
   133 qualified lemma bit_eq_iff:
    59   by (auto simp add: set_inject)
   134   \<open>a = b \<longleftrightarrow> (even a \<longleftrightarrow> even b)\<close> for a b :: bit
    60 
   135   by (cases a; cases b) simp_all
    61 end
   136 
    62 
   137 end
    63 lemma Bit_inject [simp]: "Bit P = Bit Q \<longleftrightarrow> (P \<longleftrightarrow> Q)"
   138 
    64   by (auto simp add: Bit_inject)
   139 lemma modulo_bit_unfold [simp, code]:
    65 
   140   \<open>a mod b = of_bool (odd a \<and> even b)\<close> for a b :: bit
    66 lemma set [iff]:
   141   by (simp add: modulo_bit_def Abs_bit_eq_of_bool Rep_bit_eq_odd)
    67   "\<not> set 0"
   142 
    68   "set 1"
   143 lemma power_bit_unfold [simp, code]:
    69   by (simp_all add: zero_bit_def one_bit_def Bit_inverse)
   144   \<open>a ^ n = of_bool (odd a \<or> n = 0)\<close> for a :: bit
    70 
   145   by (cases a) simp_all
    71 lemma [code]:
   146 
    72   "set 0 \<longleftrightarrow> False"
   147 instantiation bit :: semiring_bits
    73   "set 1 \<longleftrightarrow> True"
   148 begin
    74   by simp_all
   149 
    75 
   150 definition bit_bit :: \<open>bit \<Rightarrow> nat \<Rightarrow> bool\<close>
    76 lemma set_iff: "set b \<longleftrightarrow> b = 1"
   151   where [simp]: \<open>bit_bit b n \<longleftrightarrow> odd b \<and> n = 0\<close>
    77   by (cases b) simp_all
   152 
    78 
   153 instance
    79 lemma bit_eq_iff_set:
   154   apply standard
    80   "b = 0 \<longleftrightarrow> \<not> set b"
   155               apply auto
    81   "b = 1 \<longleftrightarrow> set b"
   156   apply (metis bit.exhaust of_bool_eq(2))
    82   by (simp_all add: Z2.bit_eq_iff)
   157   done
    83 
   158 
    84 lemma Bit [simp, code]:
   159 end
    85   "Bit False = 0"
   160 
    86   "Bit True = 1"
   161 instantiation bit :: semiring_bit_shifts
    87   by (simp_all add: zero_bit_def one_bit_def)
   162 begin
    88 
   163 
    89 lemma bit_not_0_iff [iff]: "x \<noteq> 0 \<longleftrightarrow> x = 1" for x :: bit
   164 definition push_bit_bit :: \<open>nat \<Rightarrow> bit \<Rightarrow> bit\<close>
    90   by (simp add: Z2.bit_eq_iff)
   165   where [simp]: \<open>push_bit n b = of_bool (odd b \<and> n = 0)\<close> for b :: bit
    91 
   166 
    92 lemma bit_not_1_iff [iff]: "x \<noteq> 1 \<longleftrightarrow> x = 0" for x :: bit
   167 definition drop_bit_bit :: \<open>nat \<Rightarrow> bit \<Rightarrow> bit\<close>
    93   by (simp add: Z2.bit_eq_iff)
   168   where [simp]: \<open>drop_bit_bit = push_bit\<close>
    94 
   169 
    95 lemma [code]:
   170 definition take_bit_bit :: \<open>nat \<Rightarrow> bit \<Rightarrow> bit\<close>
    96   "HOL.equal 0 b \<longleftrightarrow> \<not> set b"
   171   where [simp]: \<open>take_bit n b = of_bool (odd b \<and> n > 0)\<close> for b :: bit
    97   "HOL.equal 1 b \<longleftrightarrow> set b"
   172 
    98   by (simp_all add: equal set_iff)
   173 instance
    99 
   174   by standard simp_all
   100 
   175 
   101 subsection \<open>Type \<^typ>\<open>bit\<close> forms a field\<close>
   176 end
       
   177 
       
   178 instantiation bit :: semiring_bit_operations
       
   179 begin
       
   180 
       
   181 definition and_bit :: \<open>bit \<Rightarrow> bit \<Rightarrow> bit\<close>
       
   182   where [simp]: \<open>b AND c = of_bool (odd b \<and> odd c)\<close> for b c :: bit
       
   183 
       
   184 definition or_bit :: \<open>bit \<Rightarrow> bit \<Rightarrow> bit\<close>
       
   185   where [simp]: \<open>b OR c = of_bool (odd b \<or> odd c)\<close> for b c :: bit
       
   186 
       
   187 definition xor_bit :: \<open>bit \<Rightarrow> bit \<Rightarrow> bit\<close>
       
   188   where [simp]: \<open>b XOR c = of_bool (odd b \<noteq> odd c)\<close> for b c :: bit
       
   189 
       
   190 instance
       
   191   by standard auto
       
   192 
       
   193 end
       
   194 
       
   195 lemma add_bit_eq_xor [simp, code]:
       
   196   \<open>(+) = ((XOR) :: bit \<Rightarrow> _)\<close>
       
   197   by (auto simp add: fun_eq_iff)
       
   198 
       
   199 lemma mult_bit_eq_and [simp, code]:
       
   200   \<open>(*) = ((AND) :: bit \<Rightarrow> _)\<close>
       
   201   by (simp add: fun_eq_iff)
   102 
   202 
   103 instantiation bit :: field
   203 instantiation bit :: field
   104 begin
   204 begin
   105 
   205 
   106 definition plus_bit_def: "x + y = case_bit y (case_bit 1 0 y) x"
   206 definition uminus_bit :: \<open>bit \<Rightarrow> bit\<close>
   107 
   207   where [simp]: \<open>uminus_bit = id\<close>
   108 definition times_bit_def: "x * y = case_bit 0 y x"
   208 
   109 
   209 definition inverse_bit :: \<open>bit \<Rightarrow> bit\<close>
   110 definition uminus_bit_def [simp]: "- x = x" for x :: bit
   210   where [simp]: \<open>inverse_bit = id\<close>
   111 
   211 
   112 definition minus_bit_def [simp]: "x - y = x + y" for x y :: bit
   212 instance
   113 
   213   by standard simp_all
   114 definition inverse_bit_def [simp]: "inverse x = x" for x :: bit
   214 
   115 
   215 end
   116 definition divide_bit_def [simp]: "x div y = x * y" for x y :: bit
   216 
   117 
   217 instantiation bit :: ring_bit_operations
   118 lemmas field_bit_defs =
   218 begin
   119   plus_bit_def times_bit_def minus_bit_def uminus_bit_def
   219 
   120   divide_bit_def inverse_bit_def
   220 definition not_bit :: \<open>bit \<Rightarrow> bit\<close>
   121 
   221   where [simp]: \<open>NOT b = of_bool (even b)\<close> for b :: bit
   122 instance
   222 
   123   by standard (auto simp: plus_bit_def times_bit_def split: bit.split)
   223 instance
   124 
   224   by standard simp_all
   125 end
   225 
   126 
   226 end
   127 lemma bit_add_self: "x + x = 0" for x :: bit
       
   128   unfolding plus_bit_def by (simp split: bit.split)
       
   129 
       
   130 lemma bit_mult_eq_1_iff [simp]: "x * y = 1 \<longleftrightarrow> x = 1 \<and> y = 1" for x y :: bit
       
   131   unfolding times_bit_def by (simp split: bit.split)
       
   132 
       
   133 text \<open>Not sure whether the next two should be simp rules.\<close>
       
   134 
       
   135 lemma bit_add_eq_0_iff: "x + y = 0 \<longleftrightarrow> x = y" for x y :: bit
       
   136   unfolding plus_bit_def by (simp split: bit.split)
       
   137 
       
   138 lemma bit_add_eq_1_iff: "x + y = 1 \<longleftrightarrow> x \<noteq> y" for x y :: bit
       
   139   unfolding plus_bit_def by (simp split: bit.split)
       
   140 
       
   141 
       
   142 subsection \<open>Numerals at type \<^typ>\<open>bit\<close>\<close>
       
   143 
       
   144 text \<open>All numerals reduce to either 0 or 1.\<close>
       
   145 
       
   146 lemma bit_minus1 [simp]: "- 1 = (1 :: bit)"
       
   147   by (simp only: uminus_bit_def)
       
   148 
       
   149 lemma bit_neg_numeral [simp]: "(- numeral w :: bit) = numeral w"
       
   150   by (simp only: uminus_bit_def)
       
   151 
   227 
   152 lemma bit_numeral_even [simp]: "numeral (Num.Bit0 w) = (0 :: bit)"
   228 lemma bit_numeral_even [simp]: "numeral (Num.Bit0 w) = (0 :: bit)"
   153   by (simp only: numeral_Bit0 bit_add_self)
   229   by (simp only: Z2.bit_eq_iff even_numeral) simp
   154 
   230 
   155 lemma bit_numeral_odd [simp]: "numeral (Num.Bit1 w) = (1 :: bit)"
   231 lemma bit_numeral_odd [simp]: "numeral (Num.Bit1 w) = (1 :: bit)"
   156   by (simp only: numeral_Bit1 bit_add_self add_0_left)
   232   by (simp only: Z2.bit_eq_iff odd_numeral)  simp
   157 
   233 
   158 
   234 end
   159 subsection \<open>Conversion from \<^typ>\<open>bit\<close>\<close>
       
   160 
       
   161 context zero_neq_one
       
   162 begin
       
   163 
       
   164 definition of_bit :: "bit \<Rightarrow> 'a"
       
   165   where "of_bit b = case_bit 0 1 b"
       
   166 
       
   167 lemma of_bit_eq [simp, code]:
       
   168   "of_bit 0 = 0"
       
   169   "of_bit 1 = 1"
       
   170   by (simp_all add: of_bit_def)
       
   171 
       
   172 lemma of_bit_eq_iff: "of_bit x = of_bit y \<longleftrightarrow> x = y"
       
   173   by (cases x) (cases y; simp)+
       
   174 
       
   175 end
       
   176 
       
   177 lemma (in semiring_1) of_nat_of_bit_eq: "of_nat (of_bit b) = of_bit b"
       
   178   by (cases b) simp_all
       
   179 
       
   180 lemma (in ring_1) of_int_of_bit_eq: "of_int (of_bit b) = of_bit b"
       
   181   by (cases b) simp_all
       
   182 
       
   183 
       
   184 subsection \<open>Bit structure\<close>
       
   185 
       
   186 instantiation bit :: semidom_modulo
       
   187 begin
       
   188 
       
   189 definition modulo_bit :: \<open>bit \<Rightarrow> bit \<Rightarrow> bit\<close>
       
   190   where [simp]: \<open>a mod b = a * of_bool (b = 0)\<close> for a b :: bit
       
   191 
       
   192 instance
       
   193   by standard simp
       
   194 
       
   195 end
       
   196 
       
   197 instantiation bit :: semiring_bits
       
   198 begin
       
   199 
       
   200 definition bit_bit :: \<open>bit \<Rightarrow> nat \<Rightarrow> bool\<close>
       
   201   where [simp]: \<open>bit_bit b n \<longleftrightarrow> b = 1 \<and> n = 0\<close>
       
   202 
       
   203 instance
       
   204   apply standard
       
   205                 apply (auto simp add: power_0_left power_add set_iff)
       
   206    apply (metis bit_not_1_iff of_bool_eq(2))
       
   207   done
       
   208 
       
   209 end
       
   210 
       
   211 instantiation bit :: semiring_bit_shifts
       
   212 begin
       
   213 
       
   214 definition push_bit_bit :: \<open>nat \<Rightarrow> bit \<Rightarrow> bit\<close>
       
   215   where \<open>push_bit n b = (if n = 0 then b else 0)\<close> for b :: bit
       
   216 
       
   217 definition drop_bit_bit :: \<open>nat \<Rightarrow> bit \<Rightarrow> bit\<close>
       
   218   where \<open>drop_bit n b = (if n = 0 then b else 0)\<close> for b :: bit
       
   219 
       
   220 definition take_bit_bit :: \<open>nat \<Rightarrow> bit \<Rightarrow> bit\<close>
       
   221   where \<open>take_bit n b = (if n = 0 then 0 else b)\<close> for b :: bit
       
   222 
       
   223 instance
       
   224   by standard (simp_all add: push_bit_bit_def drop_bit_bit_def take_bit_bit_def)
       
   225 
       
   226 end
       
   227 
       
   228 instantiation bit :: ring_bit_operations
       
   229 begin
       
   230 
       
   231 definition not_bit :: \<open>bit \<Rightarrow> bit\<close>
       
   232   where \<open>NOT b = of_bool (even b)\<close> for b :: bit
       
   233 
       
   234 definition and_bit :: \<open>bit \<Rightarrow> bit \<Rightarrow> bit\<close>
       
   235   where \<open>b AND c = of_bool (odd b \<and> odd c)\<close> for b c :: bit
       
   236 
       
   237 definition or_bit :: \<open>bit \<Rightarrow> bit \<Rightarrow> bit\<close>
       
   238   where \<open>b OR c = of_bool (odd b \<or> odd c)\<close> for b c :: bit
       
   239 
       
   240 definition xor_bit :: \<open>bit \<Rightarrow> bit \<Rightarrow> bit\<close>
       
   241   where \<open>b XOR c = of_bool (odd b \<noteq> odd c)\<close> for b c :: bit
       
   242 
       
   243 instance
       
   244   by standard (auto simp add: and_bit_def or_bit_def xor_bit_def not_bit_def add_eq_0_iff)
       
   245 
       
   246 end
       
   247 
       
   248 lemma add_bit_eq_xor:
       
   249   \<open>(+) = ((XOR) :: bit \<Rightarrow> _)\<close>
       
   250   by (auto simp add: fun_eq_iff plus_bit_def xor_bit_def)
       
   251 
       
   252 lemma mult_bit_eq_and:
       
   253   \<open>(*) = ((AND) :: bit \<Rightarrow> _)\<close>
       
   254   by (simp add: fun_eq_iff times_bit_def and_bit_def split: bit.split)
       
   255 
       
   256 lemma bit_NOT_eq_1_iff [simp]: "NOT b = 1 \<longleftrightarrow> b = 0"
       
   257   for b :: bit
       
   258   by (cases b) simp_all
       
   259 
       
   260 lemma bit_AND_eq_1_iff [simp]: "a AND b = 1 \<longleftrightarrow> a = 1 \<and> b = 1"
       
   261   for a b :: bit
       
   262   by (cases a; cases b) simp_all
       
   263 
       
   264 
       
   265 hide_const (open) set
       
   266 
       
   267 end