1823
|
1 |
(* Title: Quotient.thy
|
|
2 |
Author: Cezary Kaliszyk
|
|
3 |
Author: Christian Urban
|
|
4 |
|
|
5 |
provides a reasoning infrastructure for the type of finite sets
|
|
6 |
*)
|
1518
|
7 |
theory FSet
|
|
8 |
imports Quotient Quotient_List List
|
|
9 |
begin
|
|
10 |
|
|
11 |
fun
|
|
12 |
list_eq :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool" (infix "\<approx>" 50)
|
|
13 |
where
|
|
14 |
"list_eq xs ys = (\<forall>x. x \<in> set xs \<longleftrightarrow> x \<in> set ys)"
|
|
15 |
|
|
16 |
lemma list_eq_equivp:
|
|
17 |
shows "equivp list_eq"
|
1889
|
18 |
unfolding equivp_reflp_symp_transp
|
|
19 |
unfolding reflp_def symp_def transp_def
|
|
20 |
by auto
|
|
21 |
|
|
22 |
definition
|
|
23 |
memb :: "'a \<Rightarrow> 'a list \<Rightarrow> bool"
|
|
24 |
where
|
|
25 |
"memb x xs \<equiv> x \<in> set xs"
|
|
26 |
|
|
27 |
definition
|
|
28 |
sub_list :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool"
|
|
29 |
where
|
|
30 |
"sub_list xs ys \<equiv> (\<forall>x. x \<in> set xs \<longrightarrow> x \<in> set ys)"
|
|
31 |
|
|
32 |
lemma sub_list_cons:
|
|
33 |
"sub_list (x # xs) ys = (memb x ys \<and> sub_list xs ys)"
|
|
34 |
by (auto simp add: memb_def sub_list_def)
|
|
35 |
|
|
36 |
lemma nil_not_cons:
|
|
37 |
shows "\<not> ([] \<approx> x # xs)"
|
|
38 |
and "\<not> (x # xs \<approx> [])"
|
|
39 |
by auto
|
|
40 |
|
|
41 |
lemma sub_list_rsp1: "\<lbrakk>xs \<approx> ys\<rbrakk> \<Longrightarrow> sub_list xs zs = sub_list ys zs"
|
|
42 |
by (simp add: sub_list_def)
|
|
43 |
|
|
44 |
lemma sub_list_rsp2: "\<lbrakk>xs \<approx> ys\<rbrakk> \<Longrightarrow> sub_list zs xs = sub_list zs ys"
|
|
45 |
by (simp add: sub_list_def)
|
|
46 |
|
|
47 |
lemma [quot_respect]:
|
|
48 |
"(op \<approx> ===> op \<approx> ===> op =) sub_list sub_list"
|
|
49 |
by (auto simp only: fun_rel_def sub_list_rsp1 sub_list_rsp2)
|
1518
|
50 |
|
|
51 |
quotient_type
|
|
52 |
'a fset = "'a list" / "list_eq"
|
|
53 |
by (rule list_eq_equivp)
|
|
54 |
|
1860
|
55 |
section {* Empty fset, Finsert and Membership *}
|
1518
|
56 |
|
|
57 |
quotient_definition
|
|
58 |
fempty ("{||}")
|
|
59 |
where
|
|
60 |
"fempty :: 'a fset"
|
|
61 |
is "[]::'a list"
|
|
62 |
|
|
63 |
quotient_definition
|
|
64 |
"finsert :: 'a \<Rightarrow> 'a fset \<Rightarrow> 'a fset"
|
|
65 |
is "op #"
|
|
66 |
|
|
67 |
syntax
|
|
68 |
"@Finset" :: "args => 'a fset" ("{|(_)|}")
|
|
69 |
|
|
70 |
translations
|
|
71 |
"{|x, xs|}" == "CONST finsert x {|xs|}"
|
|
72 |
"{|x|}" == "CONST finsert x {||}"
|
|
73 |
|
|
74 |
quotient_definition
|
|
75 |
fin ("_ |\<in>| _" [50, 51] 50)
|
|
76 |
where
|
1816
|
77 |
"fin :: 'a \<Rightarrow> 'a fset \<Rightarrow> bool" is "memb"
|
1518
|
78 |
|
|
79 |
abbreviation
|
|
80 |
fnotin :: "'a \<Rightarrow> 'a fset \<Rightarrow> bool" ("_ |\<notin>| _" [50, 51] 50)
|
|
81 |
where
|
1860
|
82 |
"x |\<notin>| S \<equiv> \<not> (x |\<in>| S)"
|
1518
|
83 |
|
|
84 |
lemma memb_rsp[quot_respect]:
|
|
85 |
shows "(op = ===> op \<approx> ===> op =) memb memb"
|
|
86 |
by (auto simp add: memb_def)
|
|
87 |
|
|
88 |
lemma nil_rsp[quot_respect]:
|
|
89 |
shows "[] \<approx> []"
|
|
90 |
by simp
|
|
91 |
|
|
92 |
lemma cons_rsp[quot_respect]:
|
|
93 |
shows "(op = ===> op \<approx> ===> op \<approx>) op # op #"
|
|
94 |
by simp
|
|
95 |
|
1816
|
96 |
section {* Augmenting an fset -- @{const finsert} *}
|
1518
|
97 |
|
1819
|
98 |
lemma not_memb_nil:
|
1860
|
99 |
shows "\<not> memb x []"
|
1819
|
100 |
by (simp add: memb_def)
|
|
101 |
|
1878
|
102 |
lemma no_memb_nil:
|
|
103 |
"(\<forall>x. \<not> memb x xs) = (xs = [])"
|
|
104 |
by (simp add: memb_def)
|
|
105 |
|
|
106 |
lemma none_memb_nil:
|
|
107 |
"(\<forall>x. \<not> memb x xs) = (xs \<approx> [])"
|
|
108 |
by (simp add: memb_def)
|
|
109 |
|
1518
|
110 |
lemma memb_cons_iff:
|
|
111 |
shows "memb x (y # xs) = (x = y \<or> memb x xs)"
|
|
112 |
by (induct xs) (auto simp add: memb_def)
|
|
113 |
|
|
114 |
lemma memb_consI1:
|
|
115 |
shows "memb x (x # xs)"
|
|
116 |
by (simp add: memb_def)
|
|
117 |
|
|
118 |
lemma memb_consI2:
|
|
119 |
shows "memb x xs \<Longrightarrow> memb x (y # xs)"
|
|
120 |
by (simp add: memb_def)
|
|
121 |
|
|
122 |
lemma memb_absorb:
|
|
123 |
shows "memb x xs \<Longrightarrow> x # xs \<approx> xs"
|
|
124 |
by (induct xs) (auto simp add: memb_def id_simps)
|
|
125 |
|
|
126 |
section {* Singletons *}
|
|
127 |
|
|
128 |
lemma singleton_list_eq:
|
|
129 |
shows "[x] \<approx> [y] \<longleftrightarrow> x = y"
|
|
130 |
by (simp add: id_simps) auto
|
|
131 |
|
1860
|
132 |
section {* Unions *}
|
1518
|
133 |
|
|
134 |
quotient_definition
|
|
135 |
funion (infixl "|\<union>|" 65)
|
|
136 |
where
|
|
137 |
"funion :: 'a fset \<Rightarrow> 'a fset \<Rightarrow> 'a fset"
|
|
138 |
is
|
|
139 |
"op @"
|
|
140 |
|
|
141 |
section {* Cardinality of finite sets *}
|
|
142 |
|
|
143 |
fun
|
|
144 |
fcard_raw :: "'a list \<Rightarrow> nat"
|
|
145 |
where
|
|
146 |
fcard_raw_nil: "fcard_raw [] = 0"
|
|
147 |
| fcard_raw_cons: "fcard_raw (x # xs) = (if memb x xs then fcard_raw xs else Suc (fcard_raw xs))"
|
|
148 |
|
|
149 |
quotient_definition
|
|
150 |
"fcard :: 'a fset \<Rightarrow> nat"
|
|
151 |
is
|
|
152 |
"fcard_raw"
|
|
153 |
|
1813
|
154 |
lemma fcard_raw_0:
|
1860
|
155 |
shows "fcard_raw xs = 0 \<longleftrightarrow> xs \<approx> []"
|
1821
|
156 |
by (induct xs) (auto simp add: memb_def)
|
1813
|
157 |
|
1518
|
158 |
lemma fcard_raw_gt_0:
|
|
159 |
assumes a: "x \<in> set xs"
|
|
160 |
shows "0 < fcard_raw xs"
|
1860
|
161 |
using a by (induct xs) (auto simp add: memb_def)
|
1518
|
162 |
|
1813
|
163 |
lemma fcard_raw_not_memb:
|
1860
|
164 |
shows "\<not> memb x xs \<longleftrightarrow> fcard_raw (x # xs) = Suc (fcard_raw xs)"
|
1813
|
165 |
by auto
|
|
166 |
|
|
167 |
lemma fcard_raw_suc:
|
1860
|
168 |
assumes a: "fcard_raw xs = Suc n"
|
|
169 |
shows "\<exists>x ys. \<not> (memb x ys) \<and> xs \<approx> (x # ys) \<and> fcard_raw ys = n"
|
|
170 |
using a
|
|
171 |
by (induct xs) (auto simp add: memb_def split: if_splits)
|
1819
|
172 |
|
1821
|
173 |
lemma singleton_fcard_1:
|
1860
|
174 |
shows "set xs = {x} \<Longrightarrow> fcard_raw xs = 1"
|
|
175 |
by (induct xs) (auto simp add: memb_def subset_insert)
|
1819
|
176 |
|
|
177 |
lemma fcard_raw_1:
|
1823
|
178 |
shows "fcard_raw xs = 1 \<longleftrightarrow> (\<exists>x. xs \<approx> [x])"
|
|
179 |
apply (auto dest!: fcard_raw_suc)
|
1819
|
180 |
apply (simp add: fcard_raw_0)
|
1821
|
181 |
apply (rule_tac x="x" in exI)
|
1819
|
182 |
apply simp
|
1821
|
183 |
apply (subgoal_tac "set xs = {x}")
|
1860
|
184 |
apply (drule singleton_fcard_1)
|
1819
|
185 |
apply auto
|
1813
|
186 |
done
|
|
187 |
|
1518
|
188 |
lemma fcard_raw_delete_one:
|
1860
|
189 |
shows "fcard_raw ([x \<leftarrow> xs. x \<noteq> y]) = (if memb y xs then fcard_raw xs - 1 else fcard_raw xs)"
|
1518
|
190 |
by (induct xs) (auto dest: fcard_raw_gt_0 simp add: memb_def)
|
|
191 |
|
1878
|
192 |
lemma fcard_raw_suc_memb:
|
|
193 |
assumes a: "fcard_raw A = Suc n"
|
|
194 |
shows "\<exists>a. memb a A"
|
|
195 |
using a
|
|
196 |
apply (induct A)
|
|
197 |
apply simp
|
|
198 |
apply (rule_tac x="a" in exI)
|
|
199 |
apply (simp add: memb_def)
|
|
200 |
done
|
|
201 |
|
1884
|
202 |
lemma memb_card_not_0:
|
1878
|
203 |
assumes a: "memb a A"
|
|
204 |
shows "\<not>(fcard_raw A = 0)"
|
1883
|
205 |
proof -
|
|
206 |
have "\<not>(\<forall>x. \<not> memb x A)" using a by auto
|
|
207 |
then have "\<not>A \<approx> []" using none_memb_nil[of A] by simp
|
|
208 |
then show ?thesis using fcard_raw_0[of A] by simp
|
|
209 |
qed
|
1878
|
210 |
|
1518
|
211 |
lemma fcard_raw_rsp_aux:
|
1821
|
212 |
assumes a: "xs \<approx> ys"
|
|
213 |
shows "fcard_raw xs = fcard_raw ys"
|
1518
|
214 |
using a
|
1821
|
215 |
apply(induct xs arbitrary: ys)
|
1518
|
216 |
apply(auto simp add: memb_def)
|
1884
|
217 |
apply(subgoal_tac "\<forall>x. (x \<in> set xs) = (x \<in> set ys)")
|
|
218 |
apply simp
|
|
219 |
apply auto
|
|
220 |
apply (drule_tac x="x" in spec)
|
|
221 |
apply blast
|
1821
|
222 |
apply(drule_tac x="[x \<leftarrow> ys. x \<noteq> a]" in meta_spec)
|
1884
|
223 |
apply(simp add: fcard_raw_delete_one memb_def)
|
1885
|
224 |
apply (case_tac "a \<in> set ys")
|
|
225 |
apply (simp only: if_True)
|
|
226 |
apply (subgoal_tac "\<forall>x. (x \<in> set xs) = (x \<in> set ys \<and> x \<noteq> a)")
|
|
227 |
apply (drule Suc_pred'[OF fcard_raw_gt_0])
|
|
228 |
apply auto
|
1518
|
229 |
done
|
|
230 |
|
|
231 |
lemma fcard_raw_rsp[quot_respect]:
|
1860
|
232 |
shows "(op \<approx> ===> op =) fcard_raw fcard_raw"
|
1518
|
233 |
by (simp add: fcard_raw_rsp_aux)
|
|
234 |
|
|
235 |
|
|
236 |
section {* fmap and fset comprehension *}
|
|
237 |
|
|
238 |
quotient_definition
|
|
239 |
"fmap :: ('a \<Rightarrow> 'b) \<Rightarrow> 'a fset \<Rightarrow> 'b fset"
|
|
240 |
is
|
|
241 |
"map"
|
|
242 |
|
1813
|
243 |
lemma map_append:
|
1821
|
244 |
"map f (xs @ ys) \<approx> (map f xs) @ (map f ys)"
|
1813
|
245 |
by simp
|
|
246 |
|
1819
|
247 |
lemma memb_append:
|
1823
|
248 |
"memb x (xs @ ys) \<longleftrightarrow> memb x xs \<or> memb x ys"
|
1821
|
249 |
by (induct xs) (simp_all add: not_memb_nil memb_cons_iff)
|
1819
|
250 |
|
1518
|
251 |
text {* raw section *}
|
|
252 |
|
|
253 |
lemma map_rsp[quot_respect]:
|
|
254 |
shows "(op = ===> op \<approx> ===> op \<approx>) map map"
|
1822
|
255 |
by auto
|
1518
|
256 |
|
|
257 |
lemma cons_left_comm:
|
1821
|
258 |
"x # y # xs \<approx> y # x # xs"
|
|
259 |
by auto
|
1518
|
260 |
|
|
261 |
lemma cons_left_idem:
|
1821
|
262 |
"x # x # xs \<approx> x # xs"
|
|
263 |
by auto
|
1518
|
264 |
|
1819
|
265 |
lemma fset_raw_strong_cases:
|
1822
|
266 |
"(xs = []) \<or> (\<exists>x ys. ((\<not> memb x ys) \<and> (xs \<approx> x # ys)))"
|
1821
|
267 |
apply (induct xs)
|
1518
|
268 |
apply (simp)
|
|
269 |
apply (rule disjI2)
|
|
270 |
apply (erule disjE)
|
|
271 |
apply (rule_tac x="a" in exI)
|
|
272 |
apply (rule_tac x="[]" in exI)
|
1822
|
273 |
apply (simp add: memb_def)
|
1518
|
274 |
apply (erule exE)+
|
1821
|
275 |
apply (case_tac "x = a")
|
1518
|
276 |
apply (rule_tac x="a" in exI)
|
1821
|
277 |
apply (rule_tac x="ys" in exI)
|
1518
|
278 |
apply (simp)
|
1821
|
279 |
apply (rule_tac x="x" in exI)
|
|
280 |
apply (rule_tac x="a # ys" in exI)
|
1822
|
281 |
apply (auto simp add: memb_def)
|
1518
|
282 |
done
|
|
283 |
|
1860
|
284 |
section {* deletion *}
|
|
285 |
|
1518
|
286 |
fun
|
|
287 |
delete_raw :: "'a list \<Rightarrow> 'a \<Rightarrow> 'a list"
|
|
288 |
where
|
|
289 |
"delete_raw [] x = []"
|
|
290 |
| "delete_raw (a # A) x = (if (a = x) then delete_raw A x else a # (delete_raw A x))"
|
|
291 |
|
1819
|
292 |
lemma memb_delete_raw:
|
1821
|
293 |
"memb x (delete_raw xs y) = (memb x xs \<and> x \<noteq> y)"
|
|
294 |
by (induct xs arbitrary: x y) (auto simp add: memb_def)
|
1518
|
295 |
|
1825
|
296 |
lemma delete_raw_rsp:
|
1860
|
297 |
"xs \<approx> ys \<Longrightarrow> delete_raw xs x \<approx> delete_raw ys x"
|
1825
|
298 |
by (simp add: memb_def[symmetric] memb_delete_raw)
|
|
299 |
|
1819
|
300 |
lemma [quot_respect]:
|
|
301 |
"(op \<approx> ===> op = ===> op \<approx>) delete_raw delete_raw"
|
|
302 |
by (simp add: memb_def[symmetric] memb_delete_raw)
|
|
303 |
|
|
304 |
lemma memb_delete_raw_ident:
|
1860
|
305 |
shows "\<not> memb x (delete_raw xs x)"
|
1821
|
306 |
by (induct xs) (auto simp add: memb_def)
|
1518
|
307 |
|
1819
|
308 |
lemma not_memb_delete_raw_ident:
|
1860
|
309 |
shows "\<not> memb x xs \<Longrightarrow> delete_raw xs x = xs"
|
1821
|
310 |
by (induct xs) (auto simp add: memb_def)
|
1819
|
311 |
|
|
312 |
lemma fset_raw_delete_raw_cases:
|
1821
|
313 |
"xs = [] \<or> (\<exists>x. memb x xs \<and> xs \<approx> x # delete_raw xs x)"
|
|
314 |
by (induct xs) (auto simp add: memb_def)
|
1518
|
315 |
|
1819
|
316 |
lemma fdelete_raw_filter:
|
|
317 |
"delete_raw xs y = [x \<leftarrow> xs. x \<noteq> y]"
|
|
318 |
by (induct xs) simp_all
|
1518
|
319 |
|
1819
|
320 |
lemma fcard_raw_delete:
|
|
321 |
"fcard_raw (delete_raw xs y) = (if memb y xs then fcard_raw xs - 1 else fcard_raw xs)"
|
|
322 |
by (simp add: fdelete_raw_filter fcard_raw_delete_one)
|
1518
|
323 |
|
1819
|
324 |
lemma set_rsp[quot_respect]:
|
1518
|
325 |
"(op \<approx> ===> op =) set set"
|
|
326 |
by auto
|
|
327 |
|
|
328 |
definition
|
|
329 |
rsp_fold
|
|
330 |
where
|
|
331 |
"rsp_fold f = (\<forall>u v w. (f u (f v w) = f v (f u w)))"
|
|
332 |
|
|
333 |
primrec
|
1819
|
334 |
ffold_raw :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a list \<Rightarrow> 'b"
|
1518
|
335 |
where
|
1819
|
336 |
"ffold_raw f z [] = z"
|
|
337 |
| "ffold_raw f z (a # A) =
|
1518
|
338 |
(if (rsp_fold f) then
|
1819
|
339 |
if memb a A then ffold_raw f z A
|
|
340 |
else f a (ffold_raw f z A)
|
1518
|
341 |
else z)"
|
|
342 |
|
1819
|
343 |
lemma memb_commute_ffold_raw:
|
|
344 |
"rsp_fold f \<Longrightarrow> memb h b \<Longrightarrow> ffold_raw f z b = f h (ffold_raw f z (delete_raw b h))"
|
|
345 |
apply (induct b)
|
|
346 |
apply (simp add: not_memb_nil)
|
|
347 |
apply (simp add: ffold_raw.simps)
|
|
348 |
apply (rule conjI)
|
|
349 |
apply (rule_tac [!] impI)
|
|
350 |
apply (rule_tac [!] conjI)
|
|
351 |
apply (rule_tac [!] impI)
|
|
352 |
apply (simp_all add: memb_delete_raw)
|
|
353 |
apply (simp add: memb_cons_iff)
|
|
354 |
apply (simp add: not_memb_delete_raw_ident)
|
|
355 |
apply (simp add: memb_cons_iff rsp_fold_def)
|
|
356 |
done
|
|
357 |
|
1820
|
358 |
lemma ffold_raw_rsp_pre:
|
|
359 |
"\<forall>e. memb e a = memb e b \<Longrightarrow> ffold_raw f z a = ffold_raw f z b"
|
|
360 |
apply (induct a arbitrary: b)
|
1824
|
361 |
apply (simp add: hd_in_set memb_absorb memb_def none_memb_nil)
|
1820
|
362 |
apply (simp add: ffold_raw.simps)
|
|
363 |
apply (rule conjI)
|
|
364 |
apply (rule_tac [!] impI)
|
|
365 |
apply (rule_tac [!] conjI)
|
|
366 |
apply (rule_tac [!] impI)
|
1884
|
367 |
apply (subgoal_tac "\<forall>e. memb e a2 = memb e b")
|
|
368 |
apply (simp)
|
|
369 |
apply (simp add: memb_cons_iff memb_def)
|
|
370 |
apply auto
|
|
371 |
apply (drule_tac x="e" in spec)
|
|
372 |
apply blast
|
1886
|
373 |
apply (case_tac b)
|
|
374 |
apply simp_all
|
1820
|
375 |
apply (subgoal_tac "ffold_raw f z b = f a1 (ffold_raw f z (delete_raw b a1))")
|
|
376 |
apply (simp only:)
|
|
377 |
apply (rule_tac f="f a1" in arg_cong)
|
|
378 |
apply (subgoal_tac "\<forall>e. memb e a2 = memb e (delete_raw b a1)")
|
|
379 |
apply simp
|
|
380 |
apply (simp add: memb_delete_raw)
|
1883
|
381 |
apply (auto simp add: memb_cons_iff)[1]
|
1820
|
382 |
apply (erule memb_commute_ffold_raw)
|
|
383 |
apply (drule_tac x="a1" in spec)
|
|
384 |
apply (simp add: memb_cons_iff)
|
1886
|
385 |
apply (simp add: memb_cons_iff)
|
|
386 |
apply (case_tac b)
|
|
387 |
apply simp_all
|
1820
|
388 |
done
|
|
389 |
|
|
390 |
lemma [quot_respect]:
|
|
391 |
"(op = ===> op = ===> op \<approx> ===> op =) ffold_raw ffold_raw"
|
|
392 |
by (simp add: memb_def[symmetric] ffold_raw_rsp_pre)
|
|
393 |
|
1819
|
394 |
primrec
|
|
395 |
finter_raw :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list"
|
|
396 |
where
|
|
397 |
"finter_raw [] l = []"
|
|
398 |
| "finter_raw (h # t) l =
|
|
399 |
(if memb h l then h # (finter_raw t l) else finter_raw t l)"
|
|
400 |
|
|
401 |
lemma finter_raw_empty:
|
|
402 |
"finter_raw l [] = []"
|
|
403 |
by (induct l) (simp_all add: not_memb_nil)
|
|
404 |
|
|
405 |
lemma memb_finter_raw:
|
1823
|
406 |
"memb x (finter_raw xs ys) \<longleftrightarrow> memb x xs \<and> memb x ys"
|
1822
|
407 |
apply (induct xs)
|
1819
|
408 |
apply (simp add: not_memb_nil)
|
|
409 |
apply (simp add: finter_raw.simps)
|
|
410 |
apply (simp add: memb_cons_iff)
|
|
411 |
apply auto
|
|
412 |
done
|
|
413 |
|
|
414 |
lemma [quot_respect]:
|
|
415 |
"(op \<approx> ===> op \<approx> ===> op \<approx>) finter_raw finter_raw"
|
|
416 |
by (simp add: memb_def[symmetric] memb_finter_raw)
|
|
417 |
|
1518
|
418 |
section {* Constants on the Quotient Type *}
|
|
419 |
|
|
420 |
quotient_definition
|
|
421 |
"fdelete :: 'a fset \<Rightarrow> 'a \<Rightarrow> 'a fset"
|
|
422 |
is "delete_raw"
|
|
423 |
|
|
424 |
quotient_definition
|
|
425 |
"fset_to_set :: 'a fset \<Rightarrow> 'a set"
|
|
426 |
is "set"
|
|
427 |
|
1819
|
428 |
quotient_definition
|
|
429 |
"ffold :: ('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a fset \<Rightarrow> 'b"
|
|
430 |
is "ffold_raw"
|
|
431 |
|
|
432 |
quotient_definition
|
|
433 |
finter (infix "|\<inter>|" 50)
|
|
434 |
where
|
|
435 |
"finter :: 'a fset \<Rightarrow> 'a fset \<Rightarrow> 'a fset"
|
|
436 |
is "finter_raw"
|
|
437 |
|
1518
|
438 |
lemma funion_sym_pre:
|
1822
|
439 |
"xs @ ys \<approx> ys @ xs"
|
1518
|
440 |
by auto
|
|
441 |
|
|
442 |
lemma append_rsp[quot_respect]:
|
|
443 |
shows "(op \<approx> ===> op \<approx> ===> op \<approx>) op @ op @"
|
1822
|
444 |
by auto
|
1518
|
445 |
|
1821
|
446 |
lemma set_cong:
|
|
447 |
shows "(set x = set y) = (x \<approx> y)"
|
|
448 |
by auto
|
1533
|
449 |
|
|
450 |
lemma inj_map_eq_iff:
|
|
451 |
"inj f \<Longrightarrow> (map f l \<approx> map f m) = (l \<approx> m)"
|
|
452 |
by (simp add: expand_set_eq[symmetric] inj_image_eq_iff)
|
|
453 |
|
1813
|
454 |
quotient_definition
|
|
455 |
"fconcat :: ('a fset) fset \<Rightarrow> 'a fset"
|
|
456 |
is
|
|
457 |
"concat"
|
1533
|
458 |
|
1813
|
459 |
lemma list_equiv_rsp[quot_respect]:
|
|
460 |
shows "(op \<approx> ===> op \<approx> ===> op =) op \<approx> op \<approx>"
|
|
461 |
by auto
|
1518
|
462 |
|
1888
|
463 |
text {* alternate formulation with a different decomposition principle
|
|
464 |
and a proof of equivalence *}
|
|
465 |
|
|
466 |
inductive
|
|
467 |
list_eq2
|
|
468 |
where
|
|
469 |
"list_eq2 (a # b # xs) (b # a # xs)"
|
|
470 |
| "list_eq2 [] []"
|
|
471 |
| "list_eq2 xs ys \<Longrightarrow> list_eq2 ys xs"
|
|
472 |
| "list_eq2 (a # a # xs) (a # xs)"
|
|
473 |
| "list_eq2 xs ys \<Longrightarrow> list_eq2 (a # xs) (a # ys)"
|
|
474 |
| "\<lbrakk>list_eq2 xs1 xs2; list_eq2 xs2 xs3\<rbrakk> \<Longrightarrow> list_eq2 xs1 xs3"
|
|
475 |
|
|
476 |
lemma list_eq2_refl:
|
|
477 |
shows "list_eq2 xs xs"
|
|
478 |
by (induct xs) (auto intro: list_eq2.intros)
|
|
479 |
|
|
480 |
lemma cons_delete_list_eq2:
|
|
481 |
shows "list_eq2 (a # (delete_raw A a)) (if memb a A then A else a # A)"
|
|
482 |
apply (induct A)
|
|
483 |
apply (simp add: memb_def list_eq2_refl)
|
|
484 |
apply (case_tac "memb a (aa # A)")
|
|
485 |
apply (simp_all only: memb_cons_iff)
|
|
486 |
apply (case_tac [!] "a = aa")
|
|
487 |
apply (simp_all add: delete_raw.simps)
|
|
488 |
apply (case_tac "memb a A")
|
|
489 |
apply (auto simp add: memb_def)[2]
|
|
490 |
apply (metis list_eq2.intros(3) list_eq2.intros(4) list_eq2.intros(5) list_eq2.intros(6))
|
|
491 |
apply (metis delete_raw.simps(2) list_eq2.intros(1) list_eq2.intros(5) list_eq2.intros(6))
|
|
492 |
apply (auto simp add: list_eq2_refl not_memb_delete_raw_ident)
|
|
493 |
done
|
|
494 |
|
|
495 |
lemma memb_delete_list_eq2:
|
|
496 |
assumes a: "memb e r"
|
|
497 |
shows "list_eq2 (e # delete_raw r e) r"
|
|
498 |
using a cons_delete_list_eq2[of e r]
|
|
499 |
by simp
|
|
500 |
|
|
501 |
lemma list_eq2_equiv_aux:
|
|
502 |
assumes a: "fcard_raw l = n"
|
|
503 |
and b: "l \<approx> r"
|
|
504 |
shows "list_eq2 l r"
|
|
505 |
using a b
|
|
506 |
proof (induct n arbitrary: l r)
|
|
507 |
case 0
|
|
508 |
have "fcard_raw l = 0" by fact
|
1889
|
509 |
then have "\<forall>x. \<not> memb x l" using memb_card_not_0[of _ l] by auto
|
1888
|
510 |
then have z: "l = []" using no_memb_nil by auto
|
|
511 |
then have "r = []" sorry
|
|
512 |
then show ?case using z list_eq2_refl by simp
|
|
513 |
next
|
|
514 |
case (Suc m)
|
|
515 |
have b: "l \<approx> r" by fact
|
|
516 |
have d: "fcard_raw l = Suc m" by fact
|
|
517 |
have "\<exists>a. memb a l" by (rule fcard_raw_suc_memb[OF d])
|
|
518 |
then obtain a where e: "memb a l" by auto
|
|
519 |
then have e': "memb a r" using list_eq.simps[simplified memb_def[symmetric], of l r] b by auto
|
|
520 |
have f: "fcard_raw (delete_raw l a) = m" using fcard_raw_delete[of l a] e d by simp
|
|
521 |
have g: "delete_raw l a \<approx> delete_raw r a" using delete_raw_rsp[OF b] by simp
|
|
522 |
have g': "list_eq2 (delete_raw l a) (delete_raw r a)" by (rule Suc.hyps[OF f g])
|
|
523 |
have h: "list_eq2 (a # delete_raw l a) (a # delete_raw r a)" by (rule list_eq2.intros(5)[OF g'])
|
|
524 |
have i: "list_eq2 l (a # delete_raw l a)" by (rule list_eq2.intros(3)[OF memb_delete_list_eq2[OF e]])
|
|
525 |
have "list_eq2 l (a # delete_raw r a)" by (rule list_eq2.intros(6)[OF i h])
|
|
526 |
then show ?case using list_eq2.intros(6)[OF _ memb_delete_list_eq2[OF e']] by simp
|
|
527 |
qed
|
|
528 |
|
|
529 |
lemma list_eq2_equiv:
|
|
530 |
"(l \<approx> r) \<longleftrightarrow> (list_eq2 l r)"
|
|
531 |
proof
|
|
532 |
show "list_eq2 l r \<Longrightarrow> l \<approx> r" by (induct rule: list_eq2.induct) auto
|
|
533 |
show "l \<approx> r \<Longrightarrow> list_eq2 l r" using list_eq2_equiv_aux by blast
|
|
534 |
qed
|
|
535 |
|
1518
|
536 |
section {* lifted part *}
|
|
537 |
|
1819
|
538 |
lemma not_fin_fnil: "x |\<notin>| {||}"
|
|
539 |
by (lifting not_memb_nil)
|
1518
|
540 |
|
|
541 |
lemma fin_finsert_iff[simp]:
|
|
542 |
"x |\<in>| finsert y S = (x = y \<or> x |\<in>| S)"
|
|
543 |
by (lifting memb_cons_iff)
|
|
544 |
|
|
545 |
lemma
|
|
546 |
shows finsertI1: "x |\<in>| finsert x S"
|
|
547 |
and finsertI2: "x |\<in>| S \<Longrightarrow> x |\<in>| finsert y S"
|
|
548 |
by (lifting memb_consI1, lifting memb_consI2)
|
|
549 |
|
|
550 |
lemma finsert_absorb[simp]:
|
|
551 |
shows "x |\<in>| S \<Longrightarrow> finsert x S = S"
|
|
552 |
by (lifting memb_absorb)
|
|
553 |
|
|
554 |
lemma fempty_not_finsert[simp]:
|
1533
|
555 |
"{||} \<noteq> finsert x S"
|
|
556 |
"finsert x S \<noteq> {||}"
|
1518
|
557 |
by (lifting nil_not_cons)
|
|
558 |
|
|
559 |
lemma finsert_left_comm:
|
1822
|
560 |
"finsert x (finsert y S) = finsert y (finsert x S)"
|
1518
|
561 |
by (lifting cons_left_comm)
|
|
562 |
|
|
563 |
lemma finsert_left_idem:
|
1822
|
564 |
"finsert x (finsert x S) = finsert x S"
|
1518
|
565 |
by (lifting cons_left_idem)
|
|
566 |
|
|
567 |
lemma fsingleton_eq[simp]:
|
|
568 |
shows "{|x|} = {|y|} \<longleftrightarrow> x = y"
|
|
569 |
by (lifting singleton_list_eq)
|
|
570 |
|
|
571 |
text {* fset_to_set *}
|
|
572 |
|
1533
|
573 |
lemma fset_to_set_simps[simp]:
|
1819
|
574 |
"fset_to_set {||} = ({} :: 'a set)"
|
|
575 |
"fset_to_set (finsert (h :: 'a) t) = insert h (fset_to_set t)"
|
|
576 |
by (lifting set.simps)
|
1518
|
577 |
|
|
578 |
lemma in_fset_to_set:
|
1822
|
579 |
"x \<in> fset_to_set S \<equiv> x |\<in>| S"
|
1518
|
580 |
by (lifting memb_def[symmetric])
|
|
581 |
|
1819
|
582 |
lemma none_fin_fempty:
|
1824
|
583 |
"(\<forall>x. x |\<notin>| S) = (S = {||})"
|
|
584 |
by (lifting none_memb_nil)
|
1518
|
585 |
|
1533
|
586 |
lemma fset_cong:
|
1822
|
587 |
"(fset_to_set S = fset_to_set T) = (S = T)"
|
1533
|
588 |
by (lifting set_cong)
|
|
589 |
|
1518
|
590 |
text {* fcard *}
|
|
591 |
|
|
592 |
lemma fcard_fempty [simp]:
|
|
593 |
shows "fcard {||} = 0"
|
|
594 |
by (lifting fcard_raw_nil)
|
|
595 |
|
|
596 |
lemma fcard_finsert_if [simp]:
|
|
597 |
shows "fcard (finsert x S) = (if x |\<in>| S then fcard S else Suc (fcard S))"
|
|
598 |
by (lifting fcard_raw_cons)
|
|
599 |
|
1822
|
600 |
lemma fcard_0: "(fcard S = 0) = (S = {||})"
|
1813
|
601 |
by (lifting fcard_raw_0)
|
|
602 |
|
1821
|
603 |
lemma fcard_1:
|
1822
|
604 |
shows "(fcard S = 1) = (\<exists>x. S = {|x|})"
|
1819
|
605 |
by (lifting fcard_raw_1)
|
|
606 |
|
1860
|
607 |
lemma fcard_gt_0:
|
|
608 |
shows "x \<in> fset_to_set S \<Longrightarrow> 0 < fcard S"
|
1518
|
609 |
by (lifting fcard_raw_gt_0)
|
|
610 |
|
1860
|
611 |
lemma fcard_not_fin:
|
|
612 |
shows "(x |\<notin>| S) = (fcard (finsert x S) = Suc (fcard S))"
|
1813
|
613 |
by (lifting fcard_raw_not_memb)
|
|
614 |
|
1822
|
615 |
lemma fcard_suc: "fcard S = Suc n \<Longrightarrow> \<exists>x T. x |\<notin>| T \<and> S = finsert x T \<and> fcard T = n"
|
1813
|
616 |
by (lifting fcard_raw_suc)
|
|
617 |
|
1819
|
618 |
lemma fcard_delete:
|
1822
|
619 |
"fcard (fdelete S y) = (if y |\<in>| S then fcard S - 1 else fcard S)"
|
1819
|
620 |
by (lifting fcard_raw_delete)
|
|
621 |
|
1878
|
622 |
lemma fcard_suc_memb: "fcard A = Suc n \<Longrightarrow> \<exists>a. a |\<in>| A"
|
|
623 |
by (lifting fcard_raw_suc_memb)
|
|
624 |
|
|
625 |
lemma fin_fcard_not_0: "a |\<in>| A \<Longrightarrow> fcard A \<noteq> 0"
|
1887
|
626 |
by (lifting memb_card_not_0)
|
1878
|
627 |
|
1518
|
628 |
text {* funion *}
|
|
629 |
|
|
630 |
lemma funion_simps[simp]:
|
1860
|
631 |
shows "{||} |\<union>| S = S"
|
|
632 |
and "finsert x S |\<union>| T = finsert x (S |\<union>| T)"
|
1518
|
633 |
by (lifting append.simps)
|
|
634 |
|
1887
|
635 |
lemma funion_empty[simp]:
|
|
636 |
shows "S |\<union>| {||} = S"
|
|
637 |
by (lifting append_Nil2)
|
|
638 |
|
1518
|
639 |
lemma funion_sym:
|
1860
|
640 |
shows "S |\<union>| T = T |\<union>| S"
|
1518
|
641 |
by (lifting funion_sym_pre)
|
|
642 |
|
|
643 |
lemma funion_assoc:
|
1860
|
644 |
shows "S |\<union>| T |\<union>| U = S |\<union>| (T |\<union>| U)"
|
1518
|
645 |
by (lifting append_assoc)
|
|
646 |
|
1887
|
647 |
lemma singleton_union_left:
|
|
648 |
"{|a|} |\<union>| S = finsert a S"
|
|
649 |
by simp
|
|
650 |
|
|
651 |
lemma singleton_union_right:
|
|
652 |
"S |\<union>| {|a|} = finsert a S"
|
|
653 |
by (subst funion_sym) simp
|
|
654 |
|
1518
|
655 |
section {* Induction and Cases rules for finite sets *}
|
|
656 |
|
|
657 |
lemma fset_strong_cases:
|
1822
|
658 |
"S = {||} \<or> (\<exists>x T. x |\<notin>| T \<and> S = finsert x T)"
|
1819
|
659 |
by (lifting fset_raw_strong_cases)
|
1518
|
660 |
|
|
661 |
lemma fset_exhaust[case_names fempty finsert, cases type: fset]:
|
|
662 |
shows "\<lbrakk>S = {||} \<Longrightarrow> P; \<And>x S'. S = finsert x S' \<Longrightarrow> P\<rbrakk> \<Longrightarrow> P"
|
|
663 |
by (lifting list.exhaust)
|
|
664 |
|
1533
|
665 |
lemma fset_induct_weak[case_names fempty finsert]:
|
1518
|
666 |
shows "\<lbrakk>P {||}; \<And>x S. P S \<Longrightarrow> P (finsert x S)\<rbrakk> \<Longrightarrow> P S"
|
|
667 |
by (lifting list.induct)
|
|
668 |
|
1533
|
669 |
lemma fset_induct[case_names fempty finsert, induct type: fset]:
|
1518
|
670 |
assumes prem1: "P {||}"
|
|
671 |
and prem2: "\<And>x S. \<lbrakk>x |\<notin>| S; P S\<rbrakk> \<Longrightarrow> P (finsert x S)"
|
|
672 |
shows "P S"
|
1533
|
673 |
proof(induct S rule: fset_induct_weak)
|
1518
|
674 |
case fempty
|
|
675 |
show "P {||}" by (rule prem1)
|
|
676 |
next
|
|
677 |
case (finsert x S)
|
|
678 |
have asm: "P S" by fact
|
|
679 |
show "P (finsert x S)"
|
|
680 |
proof(cases "x |\<in>| S")
|
|
681 |
case True
|
|
682 |
have "x |\<in>| S" by fact
|
|
683 |
then show "P (finsert x S)" using asm by simp
|
|
684 |
next
|
|
685 |
case False
|
|
686 |
have "x |\<notin>| S" by fact
|
|
687 |
then show "P (finsert x S)" using prem2 asm by simp
|
|
688 |
qed
|
|
689 |
qed
|
|
690 |
|
1533
|
691 |
lemma fset_induct2:
|
|
692 |
"P {||} {||} \<Longrightarrow>
|
|
693 |
(\<And>x xs. x |\<notin>| xs \<Longrightarrow> P (finsert x xs) {||}) \<Longrightarrow>
|
|
694 |
(\<And>y ys. y |\<notin>| ys \<Longrightarrow> P {||} (finsert y ys)) \<Longrightarrow>
|
|
695 |
(\<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>
|
|
696 |
P xsa ysa"
|
|
697 |
apply (induct xsa arbitrary: ysa)
|
|
698 |
apply (induct_tac x rule: fset_induct)
|
|
699 |
apply simp_all
|
|
700 |
apply (induct_tac xa rule: fset_induct)
|
|
701 |
apply simp_all
|
|
702 |
done
|
1518
|
703 |
|
1819
|
704 |
text {* fmap *}
|
|
705 |
|
1533
|
706 |
lemma fmap_simps[simp]:
|
|
707 |
"fmap (f :: 'a \<Rightarrow> 'b) {||} = {||}"
|
1822
|
708 |
"fmap f (finsert x S) = finsert (f x) (fmap f S)"
|
1533
|
709 |
by (lifting map.simps)
|
|
710 |
|
|
711 |
lemma fmap_set_image:
|
1822
|
712 |
"fset_to_set (fmap f S) = f ` (fset_to_set S)"
|
|
713 |
by (induct S) (simp_all)
|
1533
|
714 |
|
|
715 |
lemma inj_fmap_eq_iff:
|
1822
|
716 |
"inj f \<Longrightarrow> (fmap f S = fmap f T) = (S = T)"
|
1533
|
717 |
by (lifting inj_map_eq_iff)
|
|
718 |
|
1822
|
719 |
lemma fmap_funion: "fmap f (S |\<union>| T) = fmap f S |\<union>| fmap f T"
|
1813
|
720 |
by (lifting map_append)
|
|
721 |
|
1819
|
722 |
lemma fin_funion:
|
1822
|
723 |
"x |\<in>| S |\<union>| T \<longleftrightarrow> x |\<in>| S \<or> x |\<in>| T"
|
1819
|
724 |
by (lifting memb_append)
|
|
725 |
|
|
726 |
text {* ffold *}
|
|
727 |
|
|
728 |
lemma ffold_nil: "ffold f z {||} = z"
|
|
729 |
by (lifting ffold_raw.simps(1)[where 'a="'b" and 'b="'a"])
|
|
730 |
|
|
731 |
lemma ffold_finsert: "ffold f z (finsert a A) =
|
|
732 |
(if rsp_fold f then if a |\<in>| A then ffold f z A else f a (ffold f z A) else z)"
|
|
733 |
by (lifting ffold_raw.simps(2)[where 'a="'b" and 'b="'a"])
|
|
734 |
|
|
735 |
lemma fin_commute_ffold:
|
|
736 |
"\<lbrakk>rsp_fold f; h |\<in>| b\<rbrakk> \<Longrightarrow> ffold f z b = f h (ffold f z (fdelete b h))"
|
|
737 |
by (lifting memb_commute_ffold_raw)
|
|
738 |
|
|
739 |
text {* fdelete *}
|
|
740 |
|
1822
|
741 |
lemma fin_fdelete:
|
|
742 |
shows "x |\<in>| fdelete S y \<longleftrightarrow> x |\<in>| S \<and> x \<noteq> y"
|
1819
|
743 |
by (lifting memb_delete_raw)
|
|
744 |
|
1822
|
745 |
lemma fin_fdelete_ident:
|
|
746 |
shows "x |\<notin>| fdelete S x"
|
1819
|
747 |
by (lifting memb_delete_raw_ident)
|
|
748 |
|
1822
|
749 |
lemma not_memb_fdelete_ident:
|
|
750 |
shows "x |\<notin>| S \<Longrightarrow> fdelete S x = S"
|
1819
|
751 |
by (lifting not_memb_delete_raw_ident)
|
|
752 |
|
|
753 |
lemma fset_fdelete_cases:
|
1822
|
754 |
shows "S = {||} \<or> (\<exists>x. x |\<in>| S \<and> S = finsert x (fdelete S x))"
|
1819
|
755 |
by (lifting fset_raw_delete_raw_cases)
|
|
756 |
|
|
757 |
text {* inter *}
|
|
758 |
|
1822
|
759 |
lemma finter_empty_l: "({||} |\<inter>| S) = {||}"
|
1819
|
760 |
by (lifting finter_raw.simps(1))
|
|
761 |
|
1822
|
762 |
lemma finter_empty_r: "(S |\<inter>| {||}) = {||}"
|
1819
|
763 |
by (lifting finter_raw_empty)
|
|
764 |
|
|
765 |
lemma finter_finsert:
|
1822
|
766 |
"finsert x S |\<inter>| T = (if x |\<in>| T then finsert x (S |\<inter>| T) else S |\<inter>| T)"
|
1819
|
767 |
by (lifting finter_raw.simps(2))
|
|
768 |
|
|
769 |
lemma fin_finter:
|
1822
|
770 |
"x |\<in>| (S |\<inter>| T) \<longleftrightarrow> x |\<in>| S \<and> x |\<in>| T"
|
1819
|
771 |
by (lifting memb_finter_raw)
|
|
772 |
|
1820
|
773 |
lemma expand_fset_eq:
|
1822
|
774 |
"(S = T) = (\<forall>x. (x |\<in>| S) = (x |\<in>| T))"
|
1820
|
775 |
by (lifting list_eq.simps[simplified memb_def[symmetric]])
|
|
776 |
|
1888
|
777 |
(* We cannot write it as "assumes .. shows" since Isabelle changes
|
|
778 |
the quantifiers to schematic variables and reintroduces them in
|
|
779 |
a different order *)
|
|
780 |
lemma fset_eq_cases:
|
|
781 |
"\<lbrakk>a1 = a2;
|
|
782 |
\<And>a b xs. \<lbrakk>a1 = finsert a (finsert b xs); a2 = finsert b (finsert a xs)\<rbrakk> \<Longrightarrow> P;
|
|
783 |
\<lbrakk>a1 = {||}; a2 = {||}\<rbrakk> \<Longrightarrow> P; \<And>xs ys. \<lbrakk>a1 = ys; a2 = xs; xs = ys\<rbrakk> \<Longrightarrow> P;
|
|
784 |
\<And>a xs. \<lbrakk>a1 = finsert a (finsert a xs); a2 = finsert a xs\<rbrakk> \<Longrightarrow> P;
|
|
785 |
\<And>xs ys a. \<lbrakk>a1 = finsert a xs; a2 = finsert a ys; xs = ys\<rbrakk> \<Longrightarrow> P;
|
|
786 |
\<And>xs1 xs2 xs3. \<lbrakk>a1 = xs1; a2 = xs3; xs1 = xs2; xs2 = xs3\<rbrakk> \<Longrightarrow> P\<rbrakk>
|
|
787 |
\<Longrightarrow> P"
|
|
788 |
by (lifting list_eq2.cases[simplified list_eq2_equiv[symmetric]])
|
|
789 |
|
|
790 |
lemma fset_eq_induct:
|
|
791 |
assumes "x1 = x2"
|
|
792 |
and "\<And>a b xs. P (finsert a (finsert b xs)) (finsert b (finsert a xs))"
|
|
793 |
and "P {||} {||}"
|
|
794 |
and "\<And>xs ys. \<lbrakk>xs = ys; P xs ys\<rbrakk> \<Longrightarrow> P ys xs"
|
|
795 |
and "\<And>a xs. P (finsert a (finsert a xs)) (finsert a xs)"
|
|
796 |
and "\<And>xs ys a. \<lbrakk>xs = ys; P xs ys\<rbrakk> \<Longrightarrow> P (finsert a xs) (finsert a ys)"
|
|
797 |
and "\<And>xs1 xs2 xs3. \<lbrakk>xs1 = xs2; P xs1 xs2; xs2 = xs3; P xs2 xs3\<rbrakk> \<Longrightarrow> P xs1 xs3"
|
|
798 |
shows "P x1 x2"
|
|
799 |
using assms
|
|
800 |
by (lifting list_eq2.induct[simplified list_eq2_equiv[symmetric]])
|
1820
|
801 |
|
1533
|
802 |
ML {*
|
|
803 |
fun dest_fsetT (Type ("FSet.fset", [T])) = T
|
|
804 |
| dest_fsetT T = raise TYPE ("dest_fsetT: fset type expected", [T], []);
|
|
805 |
*}
|
1518
|
806 |
|
1682
|
807 |
no_notation
|
|
808 |
list_eq (infix "\<approx>" 50)
|
|
809 |
|
1518
|
810 |
end
|