9 begin |
9 begin |
10 |
10 |
11 subsection \<open>Bits as a datatype\<close> |
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 .. |
|
16 |
15 |
17 instantiation bit :: "{zero, one}" |
16 instantiation bit :: "{zero, one}" |
18 begin |
17 begin |
19 |
18 |
20 definition zero_bit_def: |
19 definition zero_bit_def: "0 = Bit False" |
21 "0 = Bit False" |
|
22 |
20 |
23 definition one_bit_def: |
21 definition one_bit_def: "1 = Bit True" |
24 "1 = Bit True" |
|
25 |
22 |
26 instance .. |
23 instance .. |
27 |
24 |
28 end |
25 end |
29 |
26 |
30 old_rep_datatype "0::bit" "1::bit" |
27 old_rep_datatype "0::bit" "1::bit" |
31 proof - |
28 proof - |
32 fix P and x :: bit |
29 fix P :: "bit \<Rightarrow> bool" |
33 assume "P (0::bit)" and "P (1::bit)" |
30 fix x :: bit |
|
31 assume "P 0" and "P 1" |
34 then have "\<forall>b. P (Bit b)" |
32 then have "\<forall>b. P (Bit b)" |
35 unfolding zero_bit_def one_bit_def |
33 unfolding zero_bit_def one_bit_def |
36 by (simp add: all_bool_eq) |
34 by (simp add: all_bool_eq) |
37 then show "P x" |
35 then show "P x" |
38 by (induct x) simp |
36 by (induct x) simp |
40 show "(0::bit) \<noteq> (1::bit)" |
38 show "(0::bit) \<noteq> (1::bit)" |
41 unfolding zero_bit_def one_bit_def |
39 unfolding zero_bit_def one_bit_def |
42 by (simp add: Bit_inject) |
40 by (simp add: Bit_inject) |
43 qed |
41 qed |
44 |
42 |
45 lemma Bit_set_eq [simp]: |
43 lemma Bit_set_eq [simp]: "Bit (set b) = b" |
46 "Bit (set b) = b" |
|
47 by (fact set_inverse) |
44 by (fact set_inverse) |
48 |
45 |
49 lemma set_Bit_eq [simp]: |
46 lemma set_Bit_eq [simp]: "set (Bit P) = P" |
50 "set (Bit P) = P" |
|
51 by (rule Bit_inverse) rule |
47 by (rule Bit_inverse) rule |
52 |
48 |
53 lemma bit_eq_iff: |
49 lemma bit_eq_iff: "x = y \<longleftrightarrow> (set x \<longleftrightarrow> set y)" |
54 "x = y \<longleftrightarrow> (set x \<longleftrightarrow> set y)" |
|
55 by (auto simp add: set_inject) |
50 by (auto simp add: set_inject) |
56 |
51 |
57 lemma Bit_inject [simp]: |
52 lemma Bit_inject [simp]: "Bit P = Bit Q \<longleftrightarrow> (P \<longleftrightarrow> Q)" |
58 "Bit P = Bit Q \<longleftrightarrow> (P \<longleftrightarrow> Q)" |
53 by (auto simp add: Bit_inject) |
59 by (auto simp add: Bit_inject) |
|
60 |
54 |
61 lemma set [iff]: |
55 lemma set [iff]: |
62 "\<not> set 0" |
56 "\<not> set 0" |
63 "set 1" |
57 "set 1" |
64 by (simp_all add: zero_bit_def one_bit_def Bit_inverse) |
58 by (simp_all add: zero_bit_def one_bit_def Bit_inverse) |
80 lemma Bit [simp, code]: |
73 lemma Bit [simp, code]: |
81 "Bit False = 0" |
74 "Bit False = 0" |
82 "Bit True = 1" |
75 "Bit True = 1" |
83 by (simp_all add: zero_bit_def one_bit_def) |
76 by (simp_all add: zero_bit_def one_bit_def) |
84 |
77 |
85 lemma bit_not_0_iff [iff]: |
78 lemma bit_not_0_iff [iff]: "x \<noteq> 0 \<longleftrightarrow> x = 1" for x :: bit |
86 "(x::bit) \<noteq> 0 \<longleftrightarrow> x = 1" |
|
87 by (simp add: bit_eq_iff) |
79 by (simp add: bit_eq_iff) |
88 |
80 |
89 lemma bit_not_1_iff [iff]: |
81 lemma bit_not_1_iff [iff]: "x \<noteq> 1 \<longleftrightarrow> x = 0" for x :: bit |
90 "(x::bit) \<noteq> 1 \<longleftrightarrow> x = 0" |
|
91 by (simp add: bit_eq_iff) |
82 by (simp add: bit_eq_iff) |
92 |
83 |
93 lemma [code]: |
84 lemma [code]: |
94 "HOL.equal 0 b \<longleftrightarrow> \<not> set b" |
85 "HOL.equal 0 b \<longleftrightarrow> \<not> set b" |
95 "HOL.equal 1 b \<longleftrightarrow> set b" |
86 "HOL.equal 1 b \<longleftrightarrow> set b" |
96 by (simp_all add: equal set_iff) |
87 by (simp_all add: equal set_iff) |
97 |
88 |
98 |
89 |
99 subsection \<open>Type @{typ bit} forms a field\<close> |
90 subsection \<open>Type @{typ bit} forms a field\<close> |
100 |
91 |
101 instantiation bit :: field |
92 instantiation bit :: field |
102 begin |
93 begin |
103 |
94 |
104 definition plus_bit_def: |
95 definition plus_bit_def: "x + y = case_bit y (case_bit 1 0 y) x" |
105 "x + y = case_bit y (case_bit 1 0 y) x" |
|
106 |
96 |
107 definition times_bit_def: |
97 definition times_bit_def: "x * y = case_bit 0 y x" |
108 "x * y = case_bit 0 y x" |
|
109 |
98 |
110 definition uminus_bit_def [simp]: |
99 definition uminus_bit_def [simp]: "- x = x" for x :: bit |
111 "- x = (x :: bit)" |
|
112 |
100 |
113 definition minus_bit_def [simp]: |
101 definition minus_bit_def [simp]: "x - y = x + y" for x y :: bit |
114 "x - y = (x + y :: bit)" |
|
115 |
102 |
116 definition inverse_bit_def [simp]: |
103 definition inverse_bit_def [simp]: "inverse x = x" for x :: bit |
117 "inverse x = (x :: bit)" |
|
118 |
104 |
119 definition divide_bit_def [simp]: |
105 definition divide_bit_def [simp]: "x div y = x * y" for x y :: bit |
120 "x div y = (x * y :: bit)" |
|
121 |
106 |
122 lemmas field_bit_defs = |
107 lemmas field_bit_defs = |
123 plus_bit_def times_bit_def minus_bit_def uminus_bit_def |
108 plus_bit_def times_bit_def minus_bit_def uminus_bit_def |
124 divide_bit_def inverse_bit_def |
109 divide_bit_def inverse_bit_def |
125 |
110 |
126 instance |
111 instance |
127 by standard (auto simp: field_bit_defs split: bit.split) |
112 by standard (auto simp: field_bit_defs split: bit.split) |
128 |
113 |
129 end |
114 end |
130 |
115 |
131 lemma bit_add_self: "x + x = (0 :: bit)" |
116 lemma bit_add_self: "x + x = 0" for x :: bit |
132 unfolding plus_bit_def by (simp split: bit.split) |
117 unfolding plus_bit_def by (simp split: bit.split) |
133 |
118 |
134 lemma bit_mult_eq_1_iff [simp]: "x * y = (1 :: bit) \<longleftrightarrow> x = 1 \<and> y = 1" |
119 lemma bit_mult_eq_1_iff [simp]: "x * y = 1 \<longleftrightarrow> x = 1 \<and> y = 1" for x y :: bit |
135 unfolding times_bit_def by (simp split: bit.split) |
120 unfolding times_bit_def by (simp split: bit.split) |
136 |
121 |
137 text \<open>Not sure whether the next two should be simp rules.\<close> |
122 text \<open>Not sure whether the next two should be simp rules.\<close> |
138 |
123 |
139 lemma bit_add_eq_0_iff: "x + y = (0 :: bit) \<longleftrightarrow> x = y" |
124 lemma bit_add_eq_0_iff: "x + y = 0 \<longleftrightarrow> x = y" for x y :: bit |
140 unfolding plus_bit_def by (simp split: bit.split) |
125 unfolding plus_bit_def by (simp split: bit.split) |
141 |
126 |
142 lemma bit_add_eq_1_iff: "x + y = (1 :: bit) \<longleftrightarrow> x \<noteq> y" |
127 lemma bit_add_eq_1_iff: "x + y = 1 \<longleftrightarrow> x \<noteq> y" for x y :: bit |
143 unfolding plus_bit_def by (simp split: bit.split) |
128 unfolding plus_bit_def by (simp split: bit.split) |
144 |
129 |
145 |
130 |
146 subsection \<open>Numerals at type @{typ bit}\<close> |
131 subsection \<open>Numerals at type @{typ bit}\<close> |
147 |
132 |
164 |
149 |
165 context zero_neq_one |
150 context zero_neq_one |
166 begin |
151 begin |
167 |
152 |
168 definition of_bit :: "bit \<Rightarrow> 'a" |
153 definition of_bit :: "bit \<Rightarrow> 'a" |
169 where |
154 where "of_bit b = case_bit 0 1 b" |
170 "of_bit b = case_bit 0 1 b" |
|
171 |
155 |
172 lemma of_bit_eq [simp, code]: |
156 lemma of_bit_eq [simp, code]: |
173 "of_bit 0 = 0" |
157 "of_bit 0 = 0" |
174 "of_bit 1 = 1" |
158 "of_bit 1 = 1" |
175 by (simp_all add: of_bit_def) |
159 by (simp_all add: of_bit_def) |
176 |
160 |
177 lemma of_bit_eq_iff: |
161 lemma of_bit_eq_iff: "of_bit x = of_bit y \<longleftrightarrow> x = y" |
178 "of_bit x = of_bit y \<longleftrightarrow> x = y" |
162 by (cases x) (cases y; simp)+ |
179 by (cases x) (cases y, simp_all)+ |
|
180 |
|
181 end |
|
182 |
|
183 context semiring_1 |
|
184 begin |
|
185 |
|
186 lemma of_nat_of_bit_eq: |
|
187 "of_nat (of_bit b) = of_bit b" |
|
188 by (cases b) simp_all |
|
189 |
163 |
190 end |
164 end |
191 |
165 |
192 context ring_1 |
166 lemma (in semiring_1) of_nat_of_bit_eq: "of_nat (of_bit b) = of_bit b" |
193 begin |
|
194 |
|
195 lemma of_int_of_bit_eq: |
|
196 "of_int (of_bit b) = of_bit b" |
|
197 by (cases b) simp_all |
167 by (cases b) simp_all |
198 |
168 |
199 end |
169 lemma (in ring_1) of_int_of_bit_eq: "of_int (of_bit b) = of_bit b" |
|
170 by (cases b) simp_all |
200 |
171 |
201 hide_const (open) set |
172 hide_const (open) set |
202 |
173 |
203 end |
174 end |