author | wenzelm |
Fri, 12 Jul 2013 16:19:05 +0200 | |
changeset 52622 | e0ff1625e96d |
parent 52147 | 9943f8067f11 |
child 55142 | 378ae9e46175 |
permissions | -rw-r--r-- |
29629 | 1 |
(* Title: HOL/Library/Numeral_Type.thy |
2 |
Author: Brian Huffman |
|
24332
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset
|
3 |
*) |
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset
|
4 |
|
29629 | 5 |
header {* Numeral Syntax for Types *} |
24332
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset
|
6 |
|
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset
|
7 |
theory Numeral_Type |
37653 | 8 |
imports Cardinality |
24332
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset
|
9 |
begin |
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset
|
10 |
|
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset
|
11 |
subsection {* Numeral Types *} |
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset
|
12 |
|
49834 | 13 |
typedef num0 = "UNIV :: nat set" .. |
14 |
typedef num1 = "UNIV :: unit set" .. |
|
29997 | 15 |
|
49834 | 16 |
typedef 'a bit0 = "{0 ..< 2 * int CARD('a::finite)}" |
29997 | 17 |
proof |
18 |
show "0 \<in> {0 ..< 2 * int CARD('a)}" |
|
19 |
by simp |
|
20 |
qed |
|
21 |
||
49834 | 22 |
typedef 'a bit1 = "{0 ..< 1 + 2 * int CARD('a::finite)}" |
29997 | 23 |
proof |
24 |
show "0 \<in> {0 ..< 1 + 2 * int CARD('a)}" |
|
25 |
by simp |
|
26 |
qed |
|
24332
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset
|
27 |
|
30001 | 28 |
lemma card_num0 [simp]: "CARD (num0) = 0" |
29 |
unfolding type_definition.card [OF type_definition_num0] |
|
30 |
by simp |
|
31 |
||
51153
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
32 |
lemma infinite_num0: "\<not> finite (UNIV :: num0 set)" |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
33 |
using card_num0[unfolded card_eq_0_iff] |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
34 |
by simp |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
35 |
|
30001 | 36 |
lemma card_num1 [simp]: "CARD(num1) = 1" |
37 |
unfolding type_definition.card [OF type_definition_num1] |
|
48063
f02b4302d5dd
remove duplicate lemma card_unit in favor of Finite_Set.card_UNIV_unit
huffman
parents:
47108
diff
changeset
|
38 |
by (simp only: card_UNIV_unit) |
30001 | 39 |
|
40 |
lemma card_bit0 [simp]: "CARD('a bit0) = 2 * CARD('a::finite)" |
|
41 |
unfolding type_definition.card [OF type_definition_bit0] |
|
42 |
by simp |
|
43 |
||
44 |
lemma card_bit1 [simp]: "CARD('a bit1) = Suc (2 * CARD('a::finite))" |
|
45 |
unfolding type_definition.card [OF type_definition_bit1] |
|
46 |
by simp |
|
47 |
||
24332
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset
|
48 |
instance num1 :: finite |
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset
|
49 |
proof |
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset
|
50 |
show "finite (UNIV::num1 set)" |
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset
|
51 |
unfolding type_definition.univ [OF type_definition_num1] |
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset
|
52 |
using finite by (rule finite_imageI) |
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset
|
53 |
qed |
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset
|
54 |
|
30001 | 55 |
instance bit0 :: (finite) card2 |
24332
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset
|
56 |
proof |
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset
|
57 |
show "finite (UNIV::'a bit0 set)" |
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset
|
58 |
unfolding type_definition.univ [OF type_definition_bit0] |
29997 | 59 |
by simp |
30001 | 60 |
show "2 \<le> CARD('a bit0)" |
61 |
by simp |
|
24332
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset
|
62 |
qed |
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset
|
63 |
|
30001 | 64 |
instance bit1 :: (finite) card2 |
24332
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset
|
65 |
proof |
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset
|
66 |
show "finite (UNIV::'a bit1 set)" |
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset
|
67 |
unfolding type_definition.univ [OF type_definition_bit1] |
29997 | 68 |
by simp |
30001 | 69 |
show "2 \<le> CARD('a bit1)" |
70 |
by simp |
|
24332
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset
|
71 |
qed |
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset
|
72 |
|
37653 | 73 |
subsection {* Locales for for modular arithmetic subtypes *} |
29997 | 74 |
|
75 |
locale mod_type = |
|
76 |
fixes n :: int |
|
30960 | 77 |
and Rep :: "'a::{zero,one,plus,times,uminus,minus} \<Rightarrow> int" |
78 |
and Abs :: "int \<Rightarrow> 'a::{zero,one,plus,times,uminus,minus}" |
|
29997 | 79 |
assumes type: "type_definition Rep Abs {0..<n}" |
80 |
and size1: "1 < n" |
|
81 |
and zero_def: "0 = Abs 0" |
|
82 |
and one_def: "1 = Abs 1" |
|
83 |
and add_def: "x + y = Abs ((Rep x + Rep y) mod n)" |
|
84 |
and mult_def: "x * y = Abs ((Rep x * Rep y) mod n)" |
|
85 |
and diff_def: "x - y = Abs ((Rep x - Rep y) mod n)" |
|
86 |
and minus_def: "- x = Abs ((- Rep x) mod n)" |
|
87 |
begin |
|
88 |
||
89 |
lemma size0: "0 < n" |
|
35362 | 90 |
using size1 by simp |
29997 | 91 |
|
92 |
lemmas definitions = |
|
30960 | 93 |
zero_def one_def add_def mult_def minus_def diff_def |
29997 | 94 |
|
95 |
lemma Rep_less_n: "Rep x < n" |
|
96 |
by (rule type_definition.Rep [OF type, simplified, THEN conjunct2]) |
|
97 |
||
98 |
lemma Rep_le_n: "Rep x \<le> n" |
|
99 |
by (rule Rep_less_n [THEN order_less_imp_le]) |
|
100 |
||
101 |
lemma Rep_inject_sym: "x = y \<longleftrightarrow> Rep x = Rep y" |
|
102 |
by (rule type_definition.Rep_inject [OF type, symmetric]) |
|
103 |
||
104 |
lemma Rep_inverse: "Abs (Rep x) = x" |
|
105 |
by (rule type_definition.Rep_inverse [OF type]) |
|
106 |
||
107 |
lemma Abs_inverse: "m \<in> {0..<n} \<Longrightarrow> Rep (Abs m) = m" |
|
108 |
by (rule type_definition.Abs_inverse [OF type]) |
|
109 |
||
110 |
lemma Rep_Abs_mod: "Rep (Abs (m mod n)) = m mod n" |
|
33361
1f18de40b43f
combined former theories Divides and IntDiv to one theory Divides
haftmann
parents:
33035
diff
changeset
|
111 |
by (simp add: Abs_inverse pos_mod_conj [OF size0]) |
29997 | 112 |
|
113 |
lemma Rep_Abs_0: "Rep (Abs 0) = 0" |
|
114 |
by (simp add: Abs_inverse size0) |
|
115 |
||
116 |
lemma Rep_0: "Rep 0 = 0" |
|
117 |
by (simp add: zero_def Rep_Abs_0) |
|
118 |
||
119 |
lemma Rep_Abs_1: "Rep (Abs 1) = 1" |
|
120 |
by (simp add: Abs_inverse size1) |
|
121 |
||
122 |
lemma Rep_1: "Rep 1 = 1" |
|
123 |
by (simp add: one_def Rep_Abs_1) |
|
124 |
||
125 |
lemma Rep_mod: "Rep x mod n = Rep x" |
|
126 |
apply (rule_tac x=x in type_definition.Abs_cases [OF type]) |
|
127 |
apply (simp add: type_definition.Abs_inverse [OF type]) |
|
128 |
apply (simp add: mod_pos_pos_trivial) |
|
129 |
done |
|
130 |
||
131 |
lemmas Rep_simps = |
|
132 |
Rep_inject_sym Rep_inverse Rep_Abs_mod Rep_mod Rep_Abs_0 Rep_Abs_1 |
|
133 |
||
134 |
lemma comm_ring_1: "OFCLASS('a, comm_ring_1_class)" |
|
135 |
apply (intro_classes, unfold definitions) |
|
36350 | 136 |
apply (simp_all add: Rep_simps zmod_simps field_simps) |
29997 | 137 |
done |
138 |
||
139 |
end |
|
140 |
||
46868 | 141 |
locale mod_ring = mod_type n Rep Abs |
142 |
for n :: int |
|
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46868
diff
changeset
|
143 |
and Rep :: "'a::{comm_ring_1} \<Rightarrow> int" |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46868
diff
changeset
|
144 |
and Abs :: "int \<Rightarrow> 'a::{comm_ring_1}" |
29997 | 145 |
begin |
146 |
||
147 |
lemma of_nat_eq: "of_nat k = Abs (int k mod n)" |
|
148 |
apply (induct k) |
|
149 |
apply (simp add: zero_def) |
|
150 |
apply (simp add: Rep_simps add_def one_def zmod_simps add_ac) |
|
151 |
done |
|
152 |
||
153 |
lemma of_int_eq: "of_int z = Abs (z mod n)" |
|
154 |
apply (cases z rule: int_diff_cases) |
|
155 |
apply (simp add: Rep_simps of_nat_eq diff_def zmod_simps) |
|
156 |
done |
|
157 |
||
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46868
diff
changeset
|
158 |
lemma Rep_numeral: |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46868
diff
changeset
|
159 |
"Rep (numeral w) = numeral w mod n" |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46868
diff
changeset
|
160 |
using of_int_eq [of "numeral w"] |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46868
diff
changeset
|
161 |
by (simp add: Rep_inject_sym Rep_Abs_mod) |
29997 | 162 |
|
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46868
diff
changeset
|
163 |
lemma iszero_numeral: |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46868
diff
changeset
|
164 |
"iszero (numeral w::'a) \<longleftrightarrow> numeral w mod n = 0" |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46868
diff
changeset
|
165 |
by (simp add: Rep_inject_sym Rep_numeral Rep_0 iszero_def) |
29997 | 166 |
|
167 |
lemma cases: |
|
168 |
assumes 1: "\<And>z. \<lbrakk>(x::'a) = of_int z; 0 \<le> z; z < n\<rbrakk> \<Longrightarrow> P" |
|
169 |
shows "P" |
|
170 |
apply (cases x rule: type_definition.Abs_cases [OF type]) |
|
171 |
apply (rule_tac z="y" in 1) |
|
172 |
apply (simp_all add: of_int_eq mod_pos_pos_trivial) |
|
173 |
done |
|
174 |
||
175 |
lemma induct: |
|
176 |
"(\<And>z. \<lbrakk>0 \<le> z; z < n\<rbrakk> \<Longrightarrow> P (of_int z)) \<Longrightarrow> P (x::'a)" |
|
177 |
by (cases x rule: cases) simp |
|
178 |
||
179 |
end |
|
180 |
||
181 |
||
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46868
diff
changeset
|
182 |
subsection {* Ring class instances *} |
29997 | 183 |
|
30032 | 184 |
text {* |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46868
diff
changeset
|
185 |
Unfortunately @{text ring_1} instance is not possible for |
30032 | 186 |
@{typ num1}, since 0 and 1 are not distinct. |
187 |
*} |
|
188 |
||
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46868
diff
changeset
|
189 |
instantiation num1 :: "{comm_ring,comm_monoid_mult,numeral}" |
30032 | 190 |
begin |
191 |
||
192 |
lemma num1_eq_iff: "(x::num1) = (y::num1) \<longleftrightarrow> True" |
|
193 |
by (induct x, induct y) simp |
|
194 |
||
195 |
instance proof |
|
196 |
qed (simp_all add: num1_eq_iff) |
|
197 |
||
198 |
end |
|
199 |
||
29997 | 200 |
instantiation |
30960 | 201 |
bit0 and bit1 :: (finite) "{zero,one,plus,times,uminus,minus}" |
29997 | 202 |
begin |
203 |
||
204 |
definition Abs_bit0' :: "int \<Rightarrow> 'a bit0" where |
|
29998 | 205 |
"Abs_bit0' x = Abs_bit0 (x mod int CARD('a bit0))" |
29997 | 206 |
|
207 |
definition Abs_bit1' :: "int \<Rightarrow> 'a bit1" where |
|
29998 | 208 |
"Abs_bit1' x = Abs_bit1 (x mod int CARD('a bit1))" |
29997 | 209 |
|
210 |
definition "0 = Abs_bit0 0" |
|
211 |
definition "1 = Abs_bit0 1" |
|
212 |
definition "x + y = Abs_bit0' (Rep_bit0 x + Rep_bit0 y)" |
|
213 |
definition "x * y = Abs_bit0' (Rep_bit0 x * Rep_bit0 y)" |
|
214 |
definition "x - y = Abs_bit0' (Rep_bit0 x - Rep_bit0 y)" |
|
215 |
definition "- x = Abs_bit0' (- Rep_bit0 x)" |
|
216 |
||
217 |
definition "0 = Abs_bit1 0" |
|
218 |
definition "1 = Abs_bit1 1" |
|
219 |
definition "x + y = Abs_bit1' (Rep_bit1 x + Rep_bit1 y)" |
|
220 |
definition "x * y = Abs_bit1' (Rep_bit1 x * Rep_bit1 y)" |
|
221 |
definition "x - y = Abs_bit1' (Rep_bit1 x - Rep_bit1 y)" |
|
222 |
definition "- x = Abs_bit1' (- Rep_bit1 x)" |
|
223 |
||
224 |
instance .. |
|
225 |
||
226 |
end |
|
227 |
||
30729
461ee3e49ad3
interpretation/interpret: prefixes are mandatory by default;
wenzelm
parents:
30663
diff
changeset
|
228 |
interpretation bit0: |
29998 | 229 |
mod_type "int CARD('a::finite bit0)" |
29997 | 230 |
"Rep_bit0 :: 'a::finite bit0 \<Rightarrow> int" |
231 |
"Abs_bit0 :: int \<Rightarrow> 'a::finite bit0" |
|
232 |
apply (rule mod_type.intro) |
|
29998 | 233 |
apply (simp add: int_mult type_definition_bit0) |
30001 | 234 |
apply (rule one_less_int_card) |
29997 | 235 |
apply (rule zero_bit0_def) |
236 |
apply (rule one_bit0_def) |
|
237 |
apply (rule plus_bit0_def [unfolded Abs_bit0'_def]) |
|
238 |
apply (rule times_bit0_def [unfolded Abs_bit0'_def]) |
|
239 |
apply (rule minus_bit0_def [unfolded Abs_bit0'_def]) |
|
240 |
apply (rule uminus_bit0_def [unfolded Abs_bit0'_def]) |
|
241 |
done |
|
242 |
||
30729
461ee3e49ad3
interpretation/interpret: prefixes are mandatory by default;
wenzelm
parents:
30663
diff
changeset
|
243 |
interpretation bit1: |
29998 | 244 |
mod_type "int CARD('a::finite bit1)" |
29997 | 245 |
"Rep_bit1 :: 'a::finite bit1 \<Rightarrow> int" |
246 |
"Abs_bit1 :: int \<Rightarrow> 'a::finite bit1" |
|
247 |
apply (rule mod_type.intro) |
|
29998 | 248 |
apply (simp add: int_mult type_definition_bit1) |
30001 | 249 |
apply (rule one_less_int_card) |
29997 | 250 |
apply (rule zero_bit1_def) |
251 |
apply (rule one_bit1_def) |
|
252 |
apply (rule plus_bit1_def [unfolded Abs_bit1'_def]) |
|
253 |
apply (rule times_bit1_def [unfolded Abs_bit1'_def]) |
|
254 |
apply (rule minus_bit1_def [unfolded Abs_bit1'_def]) |
|
255 |
apply (rule uminus_bit1_def [unfolded Abs_bit1'_def]) |
|
256 |
done |
|
257 |
||
31021 | 258 |
instance bit0 :: (finite) comm_ring_1 |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46868
diff
changeset
|
259 |
by (rule bit0.comm_ring_1) |
29997 | 260 |
|
31021 | 261 |
instance bit1 :: (finite) comm_ring_1 |
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46868
diff
changeset
|
262 |
by (rule bit1.comm_ring_1) |
29997 | 263 |
|
30729
461ee3e49ad3
interpretation/interpret: prefixes are mandatory by default;
wenzelm
parents:
30663
diff
changeset
|
264 |
interpretation bit0: |
29998 | 265 |
mod_ring "int CARD('a::finite bit0)" |
29997 | 266 |
"Rep_bit0 :: 'a::finite bit0 \<Rightarrow> int" |
267 |
"Abs_bit0 :: int \<Rightarrow> 'a::finite bit0" |
|
268 |
.. |
|
269 |
||
30729
461ee3e49ad3
interpretation/interpret: prefixes are mandatory by default;
wenzelm
parents:
30663
diff
changeset
|
270 |
interpretation bit1: |
29998 | 271 |
mod_ring "int CARD('a::finite bit1)" |
29997 | 272 |
"Rep_bit1 :: 'a::finite bit1 \<Rightarrow> int" |
273 |
"Abs_bit1 :: int \<Rightarrow> 'a::finite bit1" |
|
274 |
.. |
|
275 |
||
276 |
text {* Set up cases, induction, and arithmetic *} |
|
277 |
||
29999 | 278 |
lemmas bit0_cases [case_names of_int, cases type: bit0] = bit0.cases |
279 |
lemmas bit1_cases [case_names of_int, cases type: bit1] = bit1.cases |
|
29997 | 280 |
|
29999 | 281 |
lemmas bit0_induct [case_names of_int, induct type: bit0] = bit0.induct |
282 |
lemmas bit1_induct [case_names of_int, induct type: bit1] = bit1.induct |
|
29997 | 283 |
|
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46868
diff
changeset
|
284 |
lemmas bit0_iszero_numeral [simp] = bit0.iszero_numeral |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46868
diff
changeset
|
285 |
lemmas bit1_iszero_numeral [simp] = bit1.iszero_numeral |
29997 | 286 |
|
47108
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46868
diff
changeset
|
287 |
declare eq_numeral_iff_iszero [where 'a="('a::finite) bit0", standard, simp] |
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
huffman
parents:
46868
diff
changeset
|
288 |
declare eq_numeral_iff_iszero [where 'a="('a::finite) bit1", standard, simp] |
29997 | 289 |
|
51288
be7e9a675ec9
add wellorder instance for Numeral_Type (suggested by Jesus Aransay)
Andreas Lochbihler
parents:
51175
diff
changeset
|
290 |
subsection {* Order instances *} |
51153
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
291 |
|
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
292 |
instantiation bit0 and bit1 :: (finite) linorder begin |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
293 |
definition "a < b \<longleftrightarrow> Rep_bit0 a < Rep_bit0 b" |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
294 |
definition "a \<le> b \<longleftrightarrow> Rep_bit0 a \<le> Rep_bit0 b" |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
295 |
definition "a < b \<longleftrightarrow> Rep_bit1 a < Rep_bit1 b" |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
296 |
definition "a \<le> b \<longleftrightarrow> Rep_bit1 a \<le> Rep_bit1 b" |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
297 |
|
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
298 |
instance |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
299 |
by(intro_classes) |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
300 |
(auto simp add: less_eq_bit0_def less_bit0_def less_eq_bit1_def less_bit1_def Rep_bit0_inject Rep_bit1_inject) |
51288
be7e9a675ec9
add wellorder instance for Numeral_Type (suggested by Jesus Aransay)
Andreas Lochbihler
parents:
51175
diff
changeset
|
301 |
end |
51153
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
302 |
|
51288
be7e9a675ec9
add wellorder instance for Numeral_Type (suggested by Jesus Aransay)
Andreas Lochbihler
parents:
51175
diff
changeset
|
303 |
lemma (in preorder) tranclp_less: "op <\<^sup>+\<^sup>+ = op <" |
be7e9a675ec9
add wellorder instance for Numeral_Type (suggested by Jesus Aransay)
Andreas Lochbihler
parents:
51175
diff
changeset
|
304 |
by(auto simp add: fun_eq_iff intro: less_trans elim: tranclp.induct) |
be7e9a675ec9
add wellorder instance for Numeral_Type (suggested by Jesus Aransay)
Andreas Lochbihler
parents:
51175
diff
changeset
|
305 |
|
be7e9a675ec9
add wellorder instance for Numeral_Type (suggested by Jesus Aransay)
Andreas Lochbihler
parents:
51175
diff
changeset
|
306 |
instance bit0 and bit1 :: (finite) wellorder |
be7e9a675ec9
add wellorder instance for Numeral_Type (suggested by Jesus Aransay)
Andreas Lochbihler
parents:
51175
diff
changeset
|
307 |
proof - |
be7e9a675ec9
add wellorder instance for Numeral_Type (suggested by Jesus Aransay)
Andreas Lochbihler
parents:
51175
diff
changeset
|
308 |
have "wf {(x :: 'a bit0, y). x < y}" |
be7e9a675ec9
add wellorder instance for Numeral_Type (suggested by Jesus Aransay)
Andreas Lochbihler
parents:
51175
diff
changeset
|
309 |
by(auto simp add: trancl_def tranclp_less intro!: finite_acyclic_wf acyclicI) |
be7e9a675ec9
add wellorder instance for Numeral_Type (suggested by Jesus Aransay)
Andreas Lochbihler
parents:
51175
diff
changeset
|
310 |
thus "OFCLASS('a bit0, wellorder_class)" |
be7e9a675ec9
add wellorder instance for Numeral_Type (suggested by Jesus Aransay)
Andreas Lochbihler
parents:
51175
diff
changeset
|
311 |
by(rule wf_wellorderI) intro_classes |
be7e9a675ec9
add wellorder instance for Numeral_Type (suggested by Jesus Aransay)
Andreas Lochbihler
parents:
51175
diff
changeset
|
312 |
next |
be7e9a675ec9
add wellorder instance for Numeral_Type (suggested by Jesus Aransay)
Andreas Lochbihler
parents:
51175
diff
changeset
|
313 |
have "wf {(x :: 'a bit1, y). x < y}" |
be7e9a675ec9
add wellorder instance for Numeral_Type (suggested by Jesus Aransay)
Andreas Lochbihler
parents:
51175
diff
changeset
|
314 |
by(auto simp add: trancl_def tranclp_less intro!: finite_acyclic_wf acyclicI) |
be7e9a675ec9
add wellorder instance for Numeral_Type (suggested by Jesus Aransay)
Andreas Lochbihler
parents:
51175
diff
changeset
|
315 |
thus "OFCLASS('a bit1, wellorder_class)" |
be7e9a675ec9
add wellorder instance for Numeral_Type (suggested by Jesus Aransay)
Andreas Lochbihler
parents:
51175
diff
changeset
|
316 |
by(rule wf_wellorderI) intro_classes |
be7e9a675ec9
add wellorder instance for Numeral_Type (suggested by Jesus Aransay)
Andreas Lochbihler
parents:
51175
diff
changeset
|
317 |
qed |
51153
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
318 |
|
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
319 |
subsection {* Code setup and type classes for code generation *} |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
320 |
|
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
321 |
text {* Code setup for @{typ num0} and @{typ num1} *} |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
322 |
|
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
323 |
definition Num0 :: num0 where "Num0 = Abs_num0 0" |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
324 |
code_datatype Num0 |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
325 |
|
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
326 |
instantiation num0 :: equal begin |
52143 | 327 |
definition equal_num0 :: "num0 \<Rightarrow> num0 \<Rightarrow> bool" |
51153
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
328 |
where "equal_num0 = op =" |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
329 |
instance by intro_classes (simp add: equal_num0_def) |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
330 |
end |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
331 |
|
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
332 |
lemma equal_num0_code [code]: |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
333 |
"equal_class.equal Num0 Num0 = True" |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
334 |
by(rule equal_refl) |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
335 |
|
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
336 |
code_datatype "1 :: num1" |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
337 |
|
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
338 |
instantiation num1 :: equal begin |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
339 |
definition equal_num1 :: "num1 \<Rightarrow> num1 \<Rightarrow> bool" |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
340 |
where "equal_num1 = op =" |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
341 |
instance by intro_classes (simp add: equal_num1_def) |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
342 |
end |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
343 |
|
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
344 |
lemma equal_num1_code [code]: |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
345 |
"equal_class.equal (1 :: num1) 1 = True" |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
346 |
by(rule equal_refl) |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
347 |
|
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
348 |
instantiation num1 :: enum begin |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
349 |
definition "enum_class.enum = [1 :: num1]" |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
350 |
definition "enum_class.enum_all P = P (1 :: num1)" |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
351 |
definition "enum_class.enum_ex P = P (1 :: num1)" |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
352 |
instance |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
353 |
by intro_classes |
52143 | 354 |
(auto simp add: enum_num1_def enum_all_num1_def enum_ex_num1_def num1_eq_iff Ball_def, |
51153
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
355 |
(metis (full_types) num1_eq_iff)+) |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
356 |
end |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
357 |
|
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
358 |
instantiation num0 and num1 :: card_UNIV begin |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
359 |
definition "finite_UNIV = Phantom(num0) False" |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
360 |
definition "card_UNIV = Phantom(num0) 0" |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
361 |
definition "finite_UNIV = Phantom(num1) True" |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
362 |
definition "card_UNIV = Phantom(num1) 1" |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
363 |
instance |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
364 |
by intro_classes |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
365 |
(simp_all add: finite_UNIV_num0_def card_UNIV_num0_def infinite_num0 finite_UNIV_num1_def card_UNIV_num1_def) |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
366 |
end |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
367 |
|
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
368 |
|
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
369 |
text {* Code setup for @{typ "'a bit0"} and @{typ "'a bit1"} *} |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
370 |
|
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
371 |
declare |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
372 |
bit0.Rep_inverse[code abstype] |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
373 |
bit0.Rep_0[code abstract] |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
374 |
bit0.Rep_1[code abstract] |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
375 |
|
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
376 |
lemma Abs_bit0'_code [code abstract]: |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
377 |
"Rep_bit0 (Abs_bit0' x :: 'a :: finite bit0) = x mod int (CARD('a bit0))" |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
378 |
by(auto simp add: Abs_bit0'_def intro!: Abs_bit0_inverse) |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
379 |
(metis bit0.Rep_Abs_mod bit0.Rep_less_n card_bit0 of_nat_numeral zmult_int) |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
380 |
|
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
381 |
lemma inj_on_Abs_bit0: |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
382 |
"inj_on (Abs_bit0 :: int \<Rightarrow> 'a bit0) {0..<2 * int CARD('a :: finite)}" |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
383 |
by(auto intro: inj_onI simp add: Abs_bit0_inject) |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
384 |
|
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
385 |
declare |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
386 |
bit1.Rep_inverse[code abstype] |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
387 |
bit1.Rep_0[code abstract] |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
388 |
bit1.Rep_1[code abstract] |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
389 |
|
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
390 |
lemma Abs_bit1'_code [code abstract]: |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
391 |
"Rep_bit1 (Abs_bit1' x :: 'a :: finite bit1) = x mod int (CARD('a bit1))" |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
392 |
by(auto simp add: Abs_bit1'_def intro!: Abs_bit1_inverse) |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
393 |
(metis of_nat_0_less_iff of_nat_Suc of_nat_mult of_nat_numeral pos_mod_conj zero_less_Suc) |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
394 |
|
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
395 |
lemma inj_on_Abs_bit1: |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
396 |
"inj_on (Abs_bit1 :: int \<Rightarrow> 'a bit1) {0..<1 + 2 * int CARD('a :: finite)}" |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
397 |
by(auto intro: inj_onI simp add: Abs_bit1_inject) |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
398 |
|
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
399 |
instantiation bit0 and bit1 :: (finite) equal begin |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
400 |
|
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
401 |
definition "equal_class.equal x y \<longleftrightarrow> Rep_bit0 x = Rep_bit0 y" |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
402 |
definition "equal_class.equal x y \<longleftrightarrow> Rep_bit1 x = Rep_bit1 y" |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
403 |
|
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
404 |
instance |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
405 |
by intro_classes (simp_all add: equal_bit0_def equal_bit1_def Rep_bit0_inject Rep_bit1_inject) |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
406 |
|
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
407 |
end |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
408 |
|
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
409 |
instantiation bit0 :: (finite) enum begin |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
410 |
definition "(enum_class.enum :: 'a bit0 list) = map (Abs_bit0' \<circ> int) (upt 0 (CARD('a bit0)))" |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
411 |
definition "enum_class.enum_all P = (\<forall>b :: 'a bit0 \<in> set enum_class.enum. P b)" |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
412 |
definition "enum_class.enum_ex P = (\<exists>b :: 'a bit0 \<in> set enum_class.enum. P b)" |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
413 |
|
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
414 |
instance |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
415 |
proof(intro_classes) |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
416 |
show "distinct (enum_class.enum :: 'a bit0 list)" |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
417 |
by(auto intro!: inj_onI simp add: Abs_bit0'_def Abs_bit0_inject zmod_int[symmetric] enum_bit0_def distinct_map) |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
418 |
|
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
419 |
show univ_eq: "(UNIV :: 'a bit0 set) = set enum_class.enum" |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
420 |
unfolding enum_bit0_def type_definition.Abs_image[OF type_definition_bit0, symmetric] |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
421 |
by(simp add: image_comp inj_on_Abs_bit0 card_image image_int_atLeastLessThan) |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
422 |
(auto intro!: image_cong[OF refl] simp add: Abs_bit0'_def mod_pos_pos_trivial) |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
423 |
|
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
424 |
fix P :: "'a bit0 \<Rightarrow> bool" |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
425 |
show "enum_class.enum_all P = Ball UNIV P" |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
426 |
and "enum_class.enum_ex P = Bex UNIV P" |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
427 |
by(simp_all add: enum_all_bit0_def enum_ex_bit0_def univ_eq) |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
428 |
qed |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
429 |
|
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
430 |
end |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
431 |
|
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
432 |
instantiation bit1 :: (finite) enum begin |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
433 |
definition "(enum_class.enum :: 'a bit1 list) = map (Abs_bit1' \<circ> int) (upt 0 (CARD('a bit1)))" |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
434 |
definition "enum_class.enum_all P = (\<forall>b :: 'a bit1 \<in> set enum_class.enum. P b)" |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
435 |
definition "enum_class.enum_ex P = (\<exists>b :: 'a bit1 \<in> set enum_class.enum. P b)" |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
436 |
|
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
437 |
instance |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
438 |
proof(intro_classes) |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
439 |
show "distinct (enum_class.enum :: 'a bit1 list)" |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
440 |
by(simp only: Abs_bit1'_def zmod_int[symmetric] enum_bit1_def distinct_map Suc_eq_plus1 card_bit1 o_apply inj_on_def) |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
441 |
(clarsimp simp add: Abs_bit1_inject) |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
442 |
|
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
443 |
show univ_eq: "(UNIV :: 'a bit1 set) = set enum_class.enum" |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
444 |
unfolding enum_bit1_def type_definition.Abs_image[OF type_definition_bit1, symmetric] |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
445 |
by(simp add: image_comp inj_on_Abs_bit1 card_image image_int_atLeastLessThan) |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
446 |
(auto intro!: image_cong[OF refl] simp add: Abs_bit1'_def mod_pos_pos_trivial) |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
447 |
|
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
448 |
fix P :: "'a bit1 \<Rightarrow> bool" |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
449 |
show "enum_class.enum_all P = Ball UNIV P" |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
450 |
and "enum_class.enum_ex P = Bex UNIV P" |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
451 |
by(simp_all add: enum_all_bit1_def enum_ex_bit1_def univ_eq) |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
452 |
qed |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
453 |
|
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
454 |
end |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
455 |
|
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
456 |
instantiation bit0 and bit1 :: (finite) finite_UNIV begin |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
457 |
definition "finite_UNIV = Phantom('a bit0) True" |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
458 |
definition "finite_UNIV = Phantom('a bit1) True" |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
459 |
instance by intro_classes (simp_all add: finite_UNIV_bit0_def finite_UNIV_bit1_def) |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
460 |
end |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
461 |
|
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
462 |
instantiation bit0 and bit1 :: ("{finite,card_UNIV}") card_UNIV begin |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
463 |
definition "card_UNIV = Phantom('a bit0) (2 * of_phantom (card_UNIV :: 'a card_UNIV))" |
51175
9f472d5f112c
simplify definition as sort constraints ensure finiteness (thanks to Jesus Aransay)
Andreas Lochbihler
parents:
51153
diff
changeset
|
464 |
definition "card_UNIV = Phantom('a bit1) (1 + 2 * of_phantom (card_UNIV :: 'a card_UNIV))" |
51153
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
465 |
instance by intro_classes (simp_all add: card_UNIV_bit0_def card_UNIV_bit1_def card_UNIV) |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
466 |
end |
b14ee572cc7b
more type class instances for Numeral_Type (contributed by Jesus Aransay)
Andreas Lochbihler
parents:
49834
diff
changeset
|
467 |
|
24332
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset
|
468 |
subsection {* Syntax *} |
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset
|
469 |
|
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset
|
470 |
syntax |
46236
ae79f2978a67
position constraints for numerals enable PIDE markup;
wenzelm
parents:
37653
diff
changeset
|
471 |
"_NumeralType" :: "num_token => type" ("_") |
24332
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset
|
472 |
"_NumeralType0" :: type ("0") |
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset
|
473 |
"_NumeralType1" :: type ("1") |
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset
|
474 |
|
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset
|
475 |
translations |
35362 | 476 |
(type) "1" == (type) "num1" |
477 |
(type) "0" == (type) "num0" |
|
24332
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset
|
478 |
|
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset
|
479 |
parse_translation {* |
52143 | 480 |
let |
481 |
fun mk_bintype n = |
|
482 |
let |
|
483 |
fun mk_bit 0 = Syntax.const @{type_syntax bit0} |
|
484 |
| mk_bit 1 = Syntax.const @{type_syntax bit1}; |
|
485 |
fun bin_of n = |
|
486 |
if n = 1 then Syntax.const @{type_syntax num1} |
|
487 |
else if n = 0 then Syntax.const @{type_syntax num0} |
|
488 |
else if n = ~1 then raise TERM ("negative type numeral", []) |
|
489 |
else |
|
490 |
let val (q, r) = Integer.div_mod n 2; |
|
491 |
in mk_bit r $ bin_of q end; |
|
492 |
in bin_of n end; |
|
24332
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset
|
493 |
|
52143 | 494 |
fun numeral_tr [Free (str, _)] = mk_bintype (the (Int.fromString str)) |
495 |
| numeral_tr ts = raise TERM ("numeral_tr", ts); |
|
24332
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset
|
496 |
|
52143 | 497 |
in [(@{syntax_const "_NumeralType"}, K numeral_tr)] end; |
24332
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset
|
498 |
*} |
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset
|
499 |
|
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset
|
500 |
print_translation {* |
52143 | 501 |
let |
502 |
fun int_of [] = 0 |
|
503 |
| int_of (b :: bs) = b + 2 * int_of bs; |
|
24332
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset
|
504 |
|
52143 | 505 |
fun bin_of (Const (@{type_syntax num0}, _)) = [] |
506 |
| bin_of (Const (@{type_syntax num1}, _)) = [1] |
|
507 |
| bin_of (Const (@{type_syntax bit0}, _) $ bs) = 0 :: bin_of bs |
|
508 |
| bin_of (Const (@{type_syntax bit1}, _) $ bs) = 1 :: bin_of bs |
|
509 |
| bin_of t = raise TERM ("bin_of", [t]); |
|
24332
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset
|
510 |
|
52143 | 511 |
fun bit_tr' b [t] = |
512 |
let |
|
513 |
val rev_digs = b :: bin_of t handle TERM _ => raise Match |
|
514 |
val i = int_of rev_digs; |
|
515 |
val num = string_of_int (abs i); |
|
516 |
in |
|
517 |
Syntax.const @{syntax_const "_NumeralType"} $ Syntax.free num |
|
518 |
end |
|
519 |
| bit_tr' b _ = raise Match; |
|
520 |
in |
|
521 |
[(@{type_syntax bit0}, K (bit_tr' 0)), |
|
52147 | 522 |
(@{type_syntax bit1}, K (bit_tr' 1))] |
523 |
end; |
|
24332
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset
|
524 |
*} |
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset
|
525 |
|
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset
|
526 |
subsection {* Examples *} |
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset
|
527 |
|
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset
|
528 |
lemma "CARD(0) = 0" by simp |
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset
|
529 |
lemma "CARD(17) = 17" by simp |
29997 | 530 |
lemma "8 * 11 ^ 3 - 6 = (2::5)" by simp |
28920 | 531 |
|
24332
e3a2b75b1cf9
boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff
changeset
|
532 |
end |