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