26348
|
1 |
(* Title: HOL/Library/Enum.thy
|
|
2 |
ID: $Id$
|
|
3 |
Author: Florian Haftmann, TU Muenchen
|
|
4 |
*)
|
|
5 |
|
|
6 |
header {* Finite types as explicit enumerations *}
|
|
7 |
|
|
8 |
theory Enum
|
|
9 |
imports Main
|
|
10 |
begin
|
|
11 |
|
|
12 |
subsection {* Class @{text enum} *}
|
|
13 |
|
|
14 |
class enum = finite + -- FIXME
|
|
15 |
fixes enum :: "'a list"
|
|
16 |
assumes enum_all: "set enum = UNIV"
|
|
17 |
begin
|
|
18 |
|
|
19 |
lemma in_enum [intro]: "x \<in> set enum"
|
|
20 |
unfolding enum_all by auto
|
|
21 |
|
|
22 |
lemma enum_eq_I:
|
|
23 |
assumes "\<And>x. x \<in> set xs"
|
|
24 |
shows "set enum = set xs"
|
|
25 |
proof -
|
|
26 |
from assms UNIV_eq_I have "UNIV = set xs" by auto
|
|
27 |
with enum_all show ?thesis by simp
|
|
28 |
qed
|
|
29 |
|
|
30 |
end
|
|
31 |
|
|
32 |
|
|
33 |
subsection {* Equality and order on functions *}
|
|
34 |
|
|
35 |
declare eq_fun [code func del] order_fun [code func del]
|
|
36 |
|
|
37 |
instance "fun" :: (enum, eq) eq ..
|
|
38 |
|
|
39 |
lemma eq_fun [code func]:
|
|
40 |
fixes f g :: "'a\<Colon>enum \<Rightarrow> 'b\<Colon>eq"
|
|
41 |
shows "f = g \<longleftrightarrow> (\<forall>x \<in> set enum. f x = g x)"
|
|
42 |
by (simp add: enum_all expand_fun_eq)
|
|
43 |
|
|
44 |
lemma order_fun [code func]:
|
|
45 |
fixes f g :: "'a\<Colon>enum \<Rightarrow> 'b\<Colon>order"
|
|
46 |
shows "f \<le> g \<longleftrightarrow> (\<forall>x \<in> set enum. f x \<le> g x)"
|
|
47 |
and "f < g \<longleftrightarrow> f \<le> g \<and> (\<exists>x \<in> set enum. f x \<noteq> g x)"
|
|
48 |
by (simp_all add: enum_all expand_fun_eq le_fun_def less_fun_def order_less_le)
|
|
49 |
|
|
50 |
|
|
51 |
subsection {* Default instances *}
|
|
52 |
|
|
53 |
instantiation unit :: enum
|
|
54 |
begin
|
|
55 |
|
|
56 |
definition
|
|
57 |
"enum = [()]"
|
|
58 |
|
|
59 |
instance by default
|
|
60 |
(simp add: enum_unit_def UNIV_unit)
|
|
61 |
|
|
62 |
end
|
|
63 |
|
|
64 |
instantiation bool :: enum
|
|
65 |
begin
|
|
66 |
|
|
67 |
definition
|
|
68 |
"enum = [False, True]"
|
|
69 |
|
|
70 |
instance by default
|
|
71 |
(simp add: enum_bool_def UNIV_bool)
|
|
72 |
|
|
73 |
end
|
|
74 |
|
|
75 |
primrec product :: "'a list \<Rightarrow> 'b list \<Rightarrow> ('a \<times> 'b) list" where
|
|
76 |
"product [] _ = []"
|
|
77 |
| "product (x#xs) ys = map (Pair x) ys @ product xs ys"
|
|
78 |
|
|
79 |
lemma product_list_set:
|
|
80 |
"set (product xs ys) = set xs \<times> set ys"
|
|
81 |
by (induct xs) auto
|
|
82 |
|
|
83 |
instantiation * :: (enum, enum) enum
|
|
84 |
begin
|
|
85 |
|
|
86 |
definition
|
|
87 |
"enum = product enum enum"
|
|
88 |
|
|
89 |
instance by default
|
|
90 |
(simp add: enum_prod_def product_list_set enum_all)
|
|
91 |
|
|
92 |
end
|
|
93 |
|
|
94 |
instantiation "+" :: (enum, enum) enum
|
|
95 |
begin
|
|
96 |
|
|
97 |
definition
|
|
98 |
"enum = map Inl enum @ map Inr enum"
|
|
99 |
|
|
100 |
instance by default
|
|
101 |
(auto simp add: enum_all enum_sum_def, case_tac x, auto)
|
|
102 |
|
|
103 |
end
|
|
104 |
|
|
105 |
primrec sublists :: "'a list \<Rightarrow> 'a list list" where
|
|
106 |
"sublists [] = [[]]"
|
|
107 |
| "sublists (x#xs) = (let xss = sublists xs in map (Cons x) xss @ xss)"
|
|
108 |
|
|
109 |
lemma sublists_powset:
|
|
110 |
"set (map set (sublists xs)) = Pow (set xs)"
|
|
111 |
proof -
|
|
112 |
have aux: "\<And>x A. set ` Cons x ` A = insert x ` set ` A"
|
|
113 |
by (auto simp add: image_def)
|
|
114 |
show ?thesis
|
|
115 |
by (induct xs)
|
|
116 |
(simp_all add: aux Let_def Pow_insert Un_commute)
|
|
117 |
qed
|
|
118 |
|
|
119 |
instantiation set :: (enum) enum
|
|
120 |
begin
|
|
121 |
|
|
122 |
definition
|
|
123 |
"enum = map set (sublists enum)"
|
|
124 |
|
|
125 |
instance by default
|
|
126 |
(simp add: enum_set_def sublists_powset enum_all del: set_map)
|
|
127 |
|
|
128 |
end
|
|
129 |
|
|
130 |
instantiation nibble :: enum
|
|
131 |
begin
|
|
132 |
|
|
133 |
definition
|
|
134 |
"enum = [Nibble0, Nibble1, Nibble2, Nibble3, Nibble4, Nibble5, Nibble6, Nibble7,
|
|
135 |
Nibble8, Nibble9, NibbleA, NibbleB, NibbleC, NibbleD, NibbleE, NibbleF]"
|
|
136 |
|
|
137 |
instance by default
|
|
138 |
(simp add: enum_nibble_def UNIV_nibble)
|
|
139 |
|
|
140 |
end
|
|
141 |
|
|
142 |
instantiation char :: enum
|
|
143 |
begin
|
|
144 |
|
|
145 |
definition
|
|
146 |
"enum = map (split Char) (product enum enum)"
|
|
147 |
|
|
148 |
instance by default
|
|
149 |
(auto intro: char.exhaust simp add: enum_char_def product_list_set enum_all full_SetCompr_eq [symmetric])
|
|
150 |
|
|
151 |
end
|
|
152 |
|
|
153 |
(*instantiation "fun" :: (enum, enum) enum
|
|
154 |
begin
|
|
155 |
|
|
156 |
|
|
157 |
definition
|
|
158 |
"enum
|
|
159 |
|
|
160 |
lemma inj_graph: "inj (%f. {(x, y). y = f x})"
|
|
161 |
by (rule inj_onI, auto simp add: expand_set_eq expand_fun_eq)
|
|
162 |
|
|
163 |
instance "fun" :: (finite, finite) finite
|
|
164 |
proof
|
|
165 |
show "finite (UNIV :: ('a => 'b) set)"
|
|
166 |
proof (rule finite_imageD)
|
|
167 |
let ?graph = "%f::'a => 'b. {(x, y). y = f x}"
|
|
168 |
show "finite (range ?graph)" by (rule finite)
|
|
169 |
show "inj ?graph" by (rule inj_graph)
|
|
170 |
qed
|
|
171 |
qed*)
|
|
172 |
|
|
173 |
end |