1518
|
1 |
theory FSet
|
|
2 |
imports Quotient Quotient_List List
|
|
3 |
begin
|
|
4 |
|
|
5 |
fun
|
|
6 |
list_eq :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool" (infix "\<approx>" 50)
|
|
7 |
where
|
|
8 |
"list_eq xs ys = (\<forall>x. x \<in> set xs \<longleftrightarrow> x \<in> set ys)"
|
|
9 |
|
|
10 |
lemma list_eq_equivp:
|
|
11 |
shows "equivp list_eq"
|
|
12 |
unfolding equivp_reflp_symp_transp reflp_def symp_def transp_def
|
|
13 |
by auto
|
|
14 |
|
|
15 |
quotient_type
|
|
16 |
'a fset = "'a list" / "list_eq"
|
|
17 |
by (rule list_eq_equivp)
|
|
18 |
|
|
19 |
section {* empty fset, finsert and membership *}
|
|
20 |
|
|
21 |
quotient_definition
|
|
22 |
fempty ("{||}")
|
|
23 |
where
|
|
24 |
"fempty :: 'a fset"
|
|
25 |
is "[]::'a list"
|
|
26 |
|
|
27 |
quotient_definition
|
|
28 |
"finsert :: 'a \<Rightarrow> 'a fset \<Rightarrow> 'a fset"
|
|
29 |
is "op #"
|
|
30 |
|
|
31 |
syntax
|
|
32 |
"@Finset" :: "args => 'a fset" ("{|(_)|}")
|
|
33 |
|
|
34 |
translations
|
|
35 |
"{|x, xs|}" == "CONST finsert x {|xs|}"
|
|
36 |
"{|x|}" == "CONST finsert x {||}"
|
|
37 |
|
|
38 |
definition
|
|
39 |
memb :: "'a \<Rightarrow> 'a list \<Rightarrow> bool"
|
|
40 |
where
|
|
41 |
"memb x xs \<equiv> x \<in> set xs"
|
|
42 |
|
|
43 |
quotient_definition
|
|
44 |
fin ("_ |\<in>| _" [50, 51] 50)
|
|
45 |
where
|
|
46 |
"fin :: 'a \<Rightarrow> 'a fset \<Rightarrow> bool"
|
|
47 |
is "memb"
|
|
48 |
|
|
49 |
abbreviation
|
|
50 |
fnotin :: "'a \<Rightarrow> 'a fset \<Rightarrow> bool" ("_ |\<notin>| _" [50, 51] 50)
|
|
51 |
where
|
|
52 |
"a |\<notin>| S \<equiv> \<not>(a |\<in>| S)"
|
|
53 |
|
|
54 |
lemma memb_rsp[quot_respect]:
|
|
55 |
shows "(op = ===> op \<approx> ===> op =) memb memb"
|
|
56 |
by (auto simp add: memb_def)
|
|
57 |
|
|
58 |
lemma nil_rsp[quot_respect]:
|
|
59 |
shows "[] \<approx> []"
|
|
60 |
by simp
|
|
61 |
|
|
62 |
lemma cons_rsp[quot_respect]:
|
|
63 |
shows "(op = ===> op \<approx> ===> op \<approx>) op # op #"
|
|
64 |
by simp
|
|
65 |
|
|
66 |
section {* Augmenting a set -- @{const finsert} *}
|
|
67 |
|
|
68 |
lemma nil_not_cons:
|
1533
|
69 |
shows
|
|
70 |
"\<not>[] \<approx> x # xs"
|
|
71 |
"\<not>x # xs \<approx> []"
|
1518
|
72 |
by auto
|
|
73 |
|
|
74 |
lemma memb_cons_iff:
|
|
75 |
shows "memb x (y # xs) = (x = y \<or> memb x xs)"
|
|
76 |
by (induct xs) (auto simp add: memb_def)
|
|
77 |
|
|
78 |
lemma memb_consI1:
|
|
79 |
shows "memb x (x # xs)"
|
|
80 |
by (simp add: memb_def)
|
|
81 |
|
|
82 |
lemma memb_consI2:
|
|
83 |
shows "memb x xs \<Longrightarrow> memb x (y # xs)"
|
|
84 |
by (simp add: memb_def)
|
|
85 |
|
|
86 |
lemma memb_absorb:
|
|
87 |
shows "memb x xs \<Longrightarrow> x # xs \<approx> xs"
|
|
88 |
by (induct xs) (auto simp add: memb_def id_simps)
|
|
89 |
|
|
90 |
section {* Singletons *}
|
|
91 |
|
|
92 |
lemma singleton_list_eq:
|
|
93 |
shows "[x] \<approx> [y] \<longleftrightarrow> x = y"
|
|
94 |
by (simp add: id_simps) auto
|
|
95 |
|
|
96 |
section {* Union *}
|
|
97 |
|
|
98 |
quotient_definition
|
|
99 |
funion (infixl "|\<union>|" 65)
|
|
100 |
where
|
|
101 |
"funion :: 'a fset \<Rightarrow> 'a fset \<Rightarrow> 'a fset"
|
|
102 |
is
|
|
103 |
"op @"
|
|
104 |
|
|
105 |
section {* Cardinality of finite sets *}
|
|
106 |
|
|
107 |
fun
|
|
108 |
fcard_raw :: "'a list \<Rightarrow> nat"
|
|
109 |
where
|
|
110 |
fcard_raw_nil: "fcard_raw [] = 0"
|
|
111 |
| fcard_raw_cons: "fcard_raw (x # xs) = (if memb x xs then fcard_raw xs else Suc (fcard_raw xs))"
|
|
112 |
|
|
113 |
quotient_definition
|
|
114 |
"fcard :: 'a fset \<Rightarrow> nat"
|
|
115 |
is
|
|
116 |
"fcard_raw"
|
|
117 |
|
|
118 |
lemma fcard_raw_gt_0:
|
|
119 |
assumes a: "x \<in> set xs"
|
|
120 |
shows "0 < fcard_raw xs"
|
|
121 |
using a
|
|
122 |
by (induct xs) (auto simp add: memb_def)
|
|
123 |
|
|
124 |
lemma fcard_raw_delete_one:
|
|
125 |
"fcard_raw ([x \<leftarrow> xs. x \<noteq> y]) = (if memb y xs then fcard_raw xs - 1 else fcard_raw xs)"
|
|
126 |
by (induct xs) (auto dest: fcard_raw_gt_0 simp add: memb_def)
|
|
127 |
|
|
128 |
lemma fcard_raw_rsp_aux:
|
|
129 |
assumes a: "a \<approx> b"
|
|
130 |
shows "fcard_raw a = fcard_raw b"
|
|
131 |
using a
|
|
132 |
apply(induct a arbitrary: b)
|
|
133 |
apply(auto simp add: memb_def)
|
|
134 |
apply(metis)
|
|
135 |
apply(drule_tac x="[x \<leftarrow> b. x \<noteq> a1]" in meta_spec)
|
|
136 |
apply(simp add: fcard_raw_delete_one)
|
1533
|
137 |
apply(metis Suc_pred'[OF fcard_raw_gt_0] fcard_raw_delete_one memb_def)
|
1518
|
138 |
done
|
|
139 |
|
|
140 |
lemma fcard_raw_rsp[quot_respect]:
|
|
141 |
"(op \<approx> ===> op =) fcard_raw fcard_raw"
|
|
142 |
by (simp add: fcard_raw_rsp_aux)
|
|
143 |
|
|
144 |
|
|
145 |
section {* fmap and fset comprehension *}
|
|
146 |
|
|
147 |
quotient_definition
|
|
148 |
"fmap :: ('a \<Rightarrow> 'b) \<Rightarrow> 'a fset \<Rightarrow> 'b fset"
|
|
149 |
is
|
|
150 |
"map"
|
|
151 |
|
|
152 |
text {* raw section *}
|
|
153 |
|
|
154 |
lemma map_rsp_aux:
|
|
155 |
assumes a: "a \<approx> b"
|
|
156 |
shows "map f a \<approx> map f b"
|
|
157 |
using a
|
|
158 |
apply(induct a arbitrary: b)
|
|
159 |
apply(auto)
|
|
160 |
apply(metis rev_image_eqI)
|
|
161 |
done
|
|
162 |
|
|
163 |
lemma map_rsp[quot_respect]:
|
|
164 |
shows "(op = ===> op \<approx> ===> op \<approx>) map map"
|
|
165 |
by (auto simp add: map_rsp_aux)
|
|
166 |
|
|
167 |
lemma cons_left_comm:
|
|
168 |
"x # y # A \<approx> y # x # A"
|
|
169 |
by (auto simp add: id_simps)
|
|
170 |
|
|
171 |
lemma cons_left_idem:
|
|
172 |
"x # x # A \<approx> x # A"
|
|
173 |
by (auto simp add: id_simps)
|
|
174 |
|
|
175 |
lemma none_mem_nil:
|
|
176 |
"(\<forall>a. a \<notin> set A) = (A \<approx> [])"
|
|
177 |
by simp
|
|
178 |
|
|
179 |
lemma finite_set_raw_strong_cases:
|
|
180 |
"(X = []) \<or> (\<exists>a Y. ((a \<notin> set Y) \<and> (X \<approx> a # Y)))"
|
|
181 |
apply (induct X)
|
|
182 |
apply (simp)
|
|
183 |
apply (rule disjI2)
|
|
184 |
apply (erule disjE)
|
|
185 |
apply (rule_tac x="a" in exI)
|
|
186 |
apply (rule_tac x="[]" in exI)
|
|
187 |
apply (simp)
|
|
188 |
apply (erule exE)+
|
|
189 |
apply (case_tac "a = aa")
|
|
190 |
apply (rule_tac x="a" in exI)
|
|
191 |
apply (rule_tac x="Y" in exI)
|
|
192 |
apply (simp)
|
|
193 |
apply (rule_tac x="aa" in exI)
|
|
194 |
apply (rule_tac x="a # Y" in exI)
|
|
195 |
apply (auto)
|
|
196 |
done
|
|
197 |
|
|
198 |
fun
|
|
199 |
delete_raw :: "'a list \<Rightarrow> 'a \<Rightarrow> 'a list"
|
|
200 |
where
|
|
201 |
"delete_raw [] x = []"
|
|
202 |
| "delete_raw (a # A) x = (if (a = x) then delete_raw A x else a # (delete_raw A x))"
|
|
203 |
|
|
204 |
lemma mem_delete_raw:
|
|
205 |
"x \<in> set (delete_raw A a) = (x \<in> set A \<and> \<not>(x = a))"
|
|
206 |
by (induct A arbitrary: x a) (auto)
|
|
207 |
|
|
208 |
lemma mem_delete_raw_ident:
|
|
209 |
"\<not>(a \<in> set (delete_raw A a))"
|
|
210 |
by (induct A) (auto)
|
|
211 |
|
|
212 |
lemma not_mem_delete_raw_ident:
|
|
213 |
"b \<notin> set A \<Longrightarrow> (delete_raw A b = A)"
|
|
214 |
by (induct A) (auto)
|
|
215 |
|
|
216 |
lemma finite_set_raw_delete_raw_cases:
|
|
217 |
"X = [] \<or> (\<exists>a. a mem X \<and> X \<approx> a # delete_raw X a)"
|
|
218 |
by (induct X) (auto)
|
|
219 |
|
|
220 |
lemma list2set_thm:
|
|
221 |
shows "set [] = {}"
|
|
222 |
and "set (h # t) = insert h (set t)"
|
|
223 |
by (auto)
|
|
224 |
|
|
225 |
lemma list2set_rsp[quot_respect]:
|
|
226 |
"(op \<approx> ===> op =) set set"
|
|
227 |
by auto
|
|
228 |
|
|
229 |
definition
|
|
230 |
rsp_fold
|
|
231 |
where
|
|
232 |
"rsp_fold f = (\<forall>u v w. (f u (f v w) = f v (f u w)))"
|
|
233 |
|
|
234 |
primrec
|
|
235 |
fold_raw :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a list \<Rightarrow> 'b"
|
|
236 |
where
|
|
237 |
"fold_raw f z [] = z"
|
|
238 |
| "fold_raw f z (a # A) =
|
|
239 |
(if (rsp_fold f) then
|
|
240 |
if a mem A then fold_raw f z A
|
|
241 |
else f a (fold_raw f z A)
|
|
242 |
else z)"
|
|
243 |
|
|
244 |
section {* Constants on the Quotient Type *}
|
|
245 |
|
|
246 |
quotient_definition
|
|
247 |
"fdelete :: 'a fset \<Rightarrow> 'a \<Rightarrow> 'a fset"
|
|
248 |
is "delete_raw"
|
|
249 |
|
|
250 |
quotient_definition
|
|
251 |
"fset_to_set :: 'a fset \<Rightarrow> 'a set"
|
|
252 |
is "set"
|
|
253 |
|
|
254 |
lemma funion_sym_pre:
|
|
255 |
"a @ b \<approx> b @ a"
|
|
256 |
by auto
|
|
257 |
|
|
258 |
lemma append_rsp[quot_respect]:
|
|
259 |
shows "(op \<approx> ===> op \<approx> ===> op \<approx>) op @ op @"
|
|
260 |
by (auto)
|
|
261 |
|
1533
|
262 |
lemma set_cong: "(set x = set y) = (x \<approx> y)"
|
|
263 |
apply rule
|
|
264 |
apply simp_all
|
|
265 |
apply (induct x y rule: list_induct2')
|
|
266 |
apply simp_all
|
|
267 |
apply auto
|
|
268 |
done
|
|
269 |
|
|
270 |
lemma inj_map_eq_iff:
|
|
271 |
"inj f \<Longrightarrow> (map f l \<approx> map f m) = (l \<approx> m)"
|
|
272 |
by (simp add: expand_set_eq[symmetric] inj_image_eq_iff)
|
|
273 |
|
|
274 |
|
1518
|
275 |
|
|
276 |
|
|
277 |
section {* lifted part *}
|
|
278 |
|
|
279 |
|
|
280 |
lemma fin_finsert_iff[simp]:
|
|
281 |
"x |\<in>| finsert y S = (x = y \<or> x |\<in>| S)"
|
|
282 |
by (lifting memb_cons_iff)
|
|
283 |
|
|
284 |
lemma
|
|
285 |
shows finsertI1: "x |\<in>| finsert x S"
|
|
286 |
and finsertI2: "x |\<in>| S \<Longrightarrow> x |\<in>| finsert y S"
|
|
287 |
by (lifting memb_consI1, lifting memb_consI2)
|
|
288 |
|
|
289 |
lemma finsert_absorb[simp]:
|
|
290 |
shows "x |\<in>| S \<Longrightarrow> finsert x S = S"
|
|
291 |
by (lifting memb_absorb)
|
|
292 |
|
|
293 |
lemma fempty_not_finsert[simp]:
|
1533
|
294 |
"{||} \<noteq> finsert x S"
|
|
295 |
"finsert x S \<noteq> {||}"
|
1518
|
296 |
by (lifting nil_not_cons)
|
|
297 |
|
|
298 |
lemma finsert_left_comm:
|
|
299 |
"finsert a (finsert b S) = finsert b (finsert a S)"
|
|
300 |
by (lifting cons_left_comm)
|
|
301 |
|
|
302 |
lemma finsert_left_idem:
|
|
303 |
"finsert a (finsert a S) = finsert a S"
|
|
304 |
by (lifting cons_left_idem)
|
|
305 |
|
|
306 |
lemma fsingleton_eq[simp]:
|
|
307 |
shows "{|x|} = {|y|} \<longleftrightarrow> x = y"
|
|
308 |
by (lifting singleton_list_eq)
|
|
309 |
|
|
310 |
text {* fset_to_set *}
|
|
311 |
|
1533
|
312 |
lemma fset_to_set_simps[simp]:
|
1518
|
313 |
"fset_to_set {||} = {}"
|
|
314 |
"fset_to_set (finsert (h :: 'b) t) = insert h (fset_to_set t)"
|
|
315 |
by (lifting list2set_thm)
|
|
316 |
|
|
317 |
lemma in_fset_to_set:
|
|
318 |
"x \<in> fset_to_set xs \<equiv> x |\<in>| xs"
|
|
319 |
by (lifting memb_def[symmetric])
|
|
320 |
|
|
321 |
lemma none_in_fempty:
|
|
322 |
"(\<forall>a. a \<notin> fset_to_set A) = (A = {||})"
|
|
323 |
by (lifting none_mem_nil)
|
|
324 |
|
1533
|
325 |
lemma fset_cong:
|
|
326 |
"(fset_to_set x = fset_to_set y) = (x = y)"
|
|
327 |
by (lifting set_cong)
|
|
328 |
|
1518
|
329 |
text {* fcard *}
|
|
330 |
|
|
331 |
lemma fcard_fempty [simp]:
|
|
332 |
shows "fcard {||} = 0"
|
|
333 |
by (lifting fcard_raw_nil)
|
|
334 |
|
|
335 |
lemma fcard_finsert_if [simp]:
|
|
336 |
shows "fcard (finsert x S) = (if x |\<in>| S then fcard S else Suc (fcard S))"
|
|
337 |
by (lifting fcard_raw_cons)
|
|
338 |
|
|
339 |
lemma fcard_gt_0: "x \<in> fset_to_set xs \<Longrightarrow> 0 < fcard xs"
|
|
340 |
by (lifting fcard_raw_gt_0)
|
|
341 |
|
|
342 |
text {* funion *}
|
|
343 |
|
|
344 |
lemma funion_simps[simp]:
|
|
345 |
"{||} |\<union>| ys = ys"
|
|
346 |
"finsert x xs |\<union>| ys = finsert x (xs |\<union>| ys)"
|
|
347 |
by (lifting append.simps)
|
|
348 |
|
|
349 |
lemma funion_sym:
|
|
350 |
"a |\<union>| b = b |\<union>| a"
|
|
351 |
by (lifting funion_sym_pre)
|
|
352 |
|
|
353 |
lemma funion_assoc:
|
|
354 |
"x |\<union>| xa |\<union>| xb = x |\<union>| (xa |\<union>| xb)"
|
|
355 |
by (lifting append_assoc)
|
|
356 |
|
|
357 |
section {* Induction and Cases rules for finite sets *}
|
|
358 |
|
|
359 |
lemma fset_strong_cases:
|
|
360 |
"X = {||} \<or> (\<exists>a Y. a \<notin> fset_to_set Y \<and> X = finsert a Y)"
|
|
361 |
by (lifting finite_set_raw_strong_cases)
|
|
362 |
|
|
363 |
lemma fset_exhaust[case_names fempty finsert, cases type: fset]:
|
|
364 |
shows "\<lbrakk>S = {||} \<Longrightarrow> P; \<And>x S'. S = finsert x S' \<Longrightarrow> P\<rbrakk> \<Longrightarrow> P"
|
|
365 |
by (lifting list.exhaust)
|
|
366 |
|
1533
|
367 |
lemma fset_induct_weak[case_names fempty finsert]:
|
1518
|
368 |
shows "\<lbrakk>P {||}; \<And>x S. P S \<Longrightarrow> P (finsert x S)\<rbrakk> \<Longrightarrow> P S"
|
|
369 |
by (lifting list.induct)
|
|
370 |
|
1533
|
371 |
lemma fset_induct[case_names fempty finsert, induct type: fset]:
|
1518
|
372 |
assumes prem1: "P {||}"
|
|
373 |
and prem2: "\<And>x S. \<lbrakk>x |\<notin>| S; P S\<rbrakk> \<Longrightarrow> P (finsert x S)"
|
|
374 |
shows "P S"
|
1533
|
375 |
proof(induct S rule: fset_induct_weak)
|
1518
|
376 |
case fempty
|
|
377 |
show "P {||}" by (rule prem1)
|
|
378 |
next
|
|
379 |
case (finsert x S)
|
|
380 |
have asm: "P S" by fact
|
|
381 |
show "P (finsert x S)"
|
|
382 |
proof(cases "x |\<in>| S")
|
|
383 |
case True
|
|
384 |
have "x |\<in>| S" by fact
|
|
385 |
then show "P (finsert x S)" using asm by simp
|
|
386 |
next
|
|
387 |
case False
|
|
388 |
have "x |\<notin>| S" by fact
|
|
389 |
then show "P (finsert x S)" using prem2 asm by simp
|
|
390 |
qed
|
|
391 |
qed
|
|
392 |
|
1533
|
393 |
lemma fset_induct2:
|
|
394 |
"P {||} {||} \<Longrightarrow>
|
|
395 |
(\<And>x xs. x |\<notin>| xs \<Longrightarrow> P (finsert x xs) {||}) \<Longrightarrow>
|
|
396 |
(\<And>y ys. y |\<notin>| ys \<Longrightarrow> P {||} (finsert y ys)) \<Longrightarrow>
|
|
397 |
(\<And>x xs y ys. \<lbrakk>P xs ys; x |\<notin>| xs; y |\<notin>| ys\<rbrakk> \<Longrightarrow> P (finsert x xs) (finsert y ys)) \<Longrightarrow>
|
|
398 |
P xsa ysa"
|
|
399 |
apply (induct xsa arbitrary: ysa)
|
|
400 |
apply (induct_tac x rule: fset_induct)
|
|
401 |
apply simp_all
|
|
402 |
apply (induct_tac xa rule: fset_induct)
|
|
403 |
apply simp_all
|
|
404 |
done
|
1518
|
405 |
|
1533
|
406 |
(* fmap *)
|
|
407 |
lemma fmap_simps[simp]:
|
|
408 |
"fmap (f :: 'a \<Rightarrow> 'b) {||} = {||}"
|
|
409 |
"fmap f (finsert x xs) = finsert (f x) (fmap f xs)"
|
|
410 |
by (lifting map.simps)
|
|
411 |
|
|
412 |
lemma fmap_set_image:
|
|
413 |
"fset_to_set (fmap f fs) = f ` (fset_to_set fs)"
|
|
414 |
apply (induct fs)
|
|
415 |
apply (simp_all)
|
|
416 |
done
|
|
417 |
|
|
418 |
lemma inj_fmap_eq_iff:
|
|
419 |
"inj f \<Longrightarrow> (fmap f l = fmap f m) = (l = m)"
|
|
420 |
by (lifting inj_map_eq_iff)
|
|
421 |
|
|
422 |
ML {*
|
|
423 |
fun dest_fsetT (Type ("FSet.fset", [T])) = T
|
|
424 |
| dest_fsetT T = raise TYPE ("dest_fsetT: fset type expected", [T], []);
|
|
425 |
*}
|
1518
|
426 |
|
1682
|
427 |
no_notation
|
|
428 |
list_eq (infix "\<approx>" 50)
|
|
429 |
|
1518
|
430 |
end
|