src/HOL/Library/Bit.thy
changeset 60500 903bb1495239
parent 60429 d3d1e185cd63
child 60679 ade12ef2773c
equal deleted inserted replaced
60499:54a3db2ed201 60500:903bb1495239
     1 (*  Title:      HOL/Library/Bit.thy
     1 (*  Title:      HOL/Library/Bit.thy
     2     Author:     Brian Huffman
     2     Author:     Brian Huffman
     3 *)
     3 *)
     4 
     4 
     5 section {* The Field of Integers mod 2 *}
     5 section \<open>The Field of Integers mod 2\<close>
     6 
     6 
     7 theory Bit
     7 theory Bit
     8 imports Main
     8 imports Main
     9 begin
     9 begin
    10 
    10 
    11 subsection {* Bits as a datatype *}
    11 subsection \<open>Bits as a datatype\<close>
    12 
    12 
    13 typedef bit = "UNIV :: bool set"
    13 typedef bit = "UNIV :: bool set"
    14   morphisms set Bit
    14   morphisms set Bit
    15   ..
    15   ..
    16 
    16 
    94   "HOL.equal 0 b \<longleftrightarrow> \<not> set b"
    94   "HOL.equal 0 b \<longleftrightarrow> \<not> set b"
    95   "HOL.equal 1 b \<longleftrightarrow> set b"
    95   "HOL.equal 1 b \<longleftrightarrow> set b"
    96   by (simp_all add: equal set_iff)  
    96   by (simp_all add: equal set_iff)  
    97 
    97 
    98   
    98   
    99 subsection {* Type @{typ bit} forms a field *}
    99 subsection \<open>Type @{typ bit} forms a field\<close>
   100 
   100 
   101 instantiation bit :: field
   101 instantiation bit :: field
   102 begin
   102 begin
   103 
   103 
   104 definition plus_bit_def:
   104 definition plus_bit_def:
   132   unfolding plus_bit_def by (simp split: bit.split)
   132   unfolding plus_bit_def by (simp split: bit.split)
   133 
   133 
   134 lemma bit_mult_eq_1_iff [simp]: "x * y = (1 :: bit) \<longleftrightarrow> x = 1 \<and> y = 1"
   134 lemma bit_mult_eq_1_iff [simp]: "x * y = (1 :: bit) \<longleftrightarrow> x = 1 \<and> y = 1"
   135   unfolding times_bit_def by (simp split: bit.split)
   135   unfolding times_bit_def by (simp split: bit.split)
   136 
   136 
   137 text {* Not sure whether the next two should be simp rules. *}
   137 text \<open>Not sure whether the next two should be simp rules.\<close>
   138 
   138 
   139 lemma bit_add_eq_0_iff: "x + y = (0 :: bit) \<longleftrightarrow> x = y"
   139 lemma bit_add_eq_0_iff: "x + y = (0 :: bit) \<longleftrightarrow> x = y"
   140   unfolding plus_bit_def by (simp split: bit.split)
   140   unfolding plus_bit_def by (simp split: bit.split)
   141 
   141 
   142 lemma bit_add_eq_1_iff: "x + y = (1 :: bit) \<longleftrightarrow> x \<noteq> y"
   142 lemma bit_add_eq_1_iff: "x + y = (1 :: bit) \<longleftrightarrow> x \<noteq> y"
   143   unfolding plus_bit_def by (simp split: bit.split)
   143   unfolding plus_bit_def by (simp split: bit.split)
   144 
   144 
   145 
   145 
   146 subsection {* Numerals at type @{typ bit} *}
   146 subsection \<open>Numerals at type @{typ bit}\<close>
   147 
   147 
   148 text {* All numerals reduce to either 0 or 1. *}
   148 text \<open>All numerals reduce to either 0 or 1.\<close>
   149 
   149 
   150 lemma bit_minus1 [simp]: "- 1 = (1 :: bit)"
   150 lemma bit_minus1 [simp]: "- 1 = (1 :: bit)"
   151   by (simp only: uminus_bit_def)
   151   by (simp only: uminus_bit_def)
   152 
   152 
   153 lemma bit_neg_numeral [simp]: "(- numeral w :: bit) = numeral w"
   153 lemma bit_neg_numeral [simp]: "(- numeral w :: bit) = numeral w"
   158 
   158 
   159 lemma bit_numeral_odd [simp]: "numeral (Num.Bit1 w) = (1 :: bit)"
   159 lemma bit_numeral_odd [simp]: "numeral (Num.Bit1 w) = (1 :: bit)"
   160   by (simp only: numeral_Bit1 bit_add_self add_0_left)
   160   by (simp only: numeral_Bit1 bit_add_self add_0_left)
   161 
   161 
   162 
   162 
   163 subsection {* Conversion from @{typ bit} *}
   163 subsection \<open>Conversion from @{typ bit}\<close>
   164 
   164 
   165 context zero_neq_one
   165 context zero_neq_one
   166 begin
   166 begin
   167 
   167 
   168 definition of_bit :: "bit \<Rightarrow> 'a"
   168 definition of_bit :: "bit \<Rightarrow> 'a"