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 |
|
1909
|
11 |
text {* Definiton of List relation and the quotient type *}
|
|
12 |
|
1518
|
13 |
fun
|
|
14 |
list_eq :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool" (infix "\<approx>" 50)
|
|
15 |
where
|
|
16 |
"list_eq xs ys = (\<forall>x. x \<in> set xs \<longleftrightarrow> x \<in> set ys)"
|
|
17 |
|
|
18 |
lemma list_eq_equivp:
|
|
19 |
shows "equivp list_eq"
|
1909
|
20 |
unfolding equivp_reflp_symp_transp
|
1889
|
21 |
unfolding reflp_def symp_def transp_def
|
|
22 |
by auto
|
|
23 |
|
1909
|
24 |
quotient_type
|
|
25 |
'a fset = "'a list" / "list_eq"
|
|
26 |
by (rule list_eq_equivp)
|
|
27 |
|
|
28 |
text {* Raw definitions *}
|
|
29 |
|
1889
|
30 |
definition
|
|
31 |
memb :: "'a \<Rightarrow> 'a list \<Rightarrow> bool"
|
|
32 |
where
|
|
33 |
"memb x xs \<equiv> x \<in> set xs"
|
|
34 |
|
|
35 |
definition
|
|
36 |
sub_list :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool"
|
|
37 |
where
|
|
38 |
"sub_list xs ys \<equiv> (\<forall>x. x \<in> set xs \<longrightarrow> x \<in> set ys)"
|
|
39 |
|
1909
|
40 |
fun
|
|
41 |
fcard_raw :: "'a list \<Rightarrow> nat"
|
|
42 |
where
|
|
43 |
fcard_raw_nil: "fcard_raw [] = 0"
|
|
44 |
| fcard_raw_cons: "fcard_raw (x # xs) = (if memb x xs then fcard_raw xs else Suc (fcard_raw xs))"
|
1518
|
45 |
|
1909
|
46 |
primrec
|
|
47 |
finter_raw :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list"
|
|
48 |
where
|
|
49 |
"finter_raw [] l = []"
|
|
50 |
| "finter_raw (h # t) l =
|
|
51 |
(if memb h l then h # (finter_raw t l) else finter_raw t l)"
|
1893
|
52 |
|
1909
|
53 |
fun
|
|
54 |
delete_raw :: "'a list \<Rightarrow> 'a \<Rightarrow> 'a list"
|
|
55 |
where
|
|
56 |
"delete_raw [] x = []"
|
|
57 |
| "delete_raw (a # A) x = (if (a = x) then delete_raw A x else a # (delete_raw A x))"
|
|
58 |
|
|
59 |
definition
|
|
60 |
rsp_fold
|
|
61 |
where
|
|
62 |
"rsp_fold f = (\<forall>u v w. (f u (f v w) = f v (f u w)))"
|
1893
|
63 |
|
1909
|
64 |
primrec
|
|
65 |
ffold_raw :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a list \<Rightarrow> 'b"
|
|
66 |
where
|
|
67 |
"ffold_raw f z [] = z"
|
|
68 |
| "ffold_raw f z (a # A) =
|
|
69 |
(if (rsp_fold f) then
|
|
70 |
if memb a A then ffold_raw f z A
|
|
71 |
else f a (ffold_raw f z A)
|
|
72 |
else z)"
|
|
73 |
|
1935
|
74 |
text {* Composition Quotient *}
|
|
75 |
|
1938
|
76 |
lemma list_rel_refl:
|
|
77 |
shows "(list_rel op \<approx>) r r"
|
|
78 |
by (rule list_rel_refl) (metis equivp_def fset_equivp)
|
|
79 |
|
1935
|
80 |
lemma compose_list_refl:
|
|
81 |
shows "(list_rel op \<approx> OOO op \<approx>) r r"
|
|
82 |
proof
|
1938
|
83 |
show c: "list_rel op \<approx> r r" by (rule list_rel_refl)
|
1935
|
84 |
have d: "r \<approx> r" by (rule equivp_reflp[OF fset_equivp])
|
|
85 |
show b: "(op \<approx> OO list_rel op \<approx>) r r" by (rule pred_compI) (rule d, rule c)
|
|
86 |
qed
|
|
87 |
|
|
88 |
lemma Quotient_fset_list:
|
|
89 |
shows "Quotient (list_rel op \<approx>) (map abs_fset) (map rep_fset)"
|
|
90 |
by (fact list_quotient[OF Quotient_fset])
|
|
91 |
|
|
92 |
lemma set_in_eq: "(\<forall>e. ((e \<in> A) \<longleftrightarrow> (e \<in> B))) \<equiv> A = B"
|
|
93 |
by (rule eq_reflection) auto
|
|
94 |
|
|
95 |
lemma map_rel_cong: "b \<approx> ba \<Longrightarrow> map f b \<approx> map f ba"
|
|
96 |
unfolding list_eq.simps
|
|
97 |
by (simp only: set_map set_in_eq)
|
|
98 |
|
|
99 |
lemma quotient_compose_list[quot_thm]:
|
|
100 |
shows "Quotient ((list_rel op \<approx>) OOO (op \<approx>))
|
|
101 |
(abs_fset \<circ> (map abs_fset)) ((map rep_fset) \<circ> rep_fset)"
|
|
102 |
unfolding Quotient_def comp_def
|
|
103 |
proof (intro conjI allI)
|
|
104 |
fix a r s
|
|
105 |
show "abs_fset (map abs_fset (map rep_fset (rep_fset a))) = a"
|
|
106 |
by (simp add: abs_o_rep[OF Quotient_fset] Quotient_abs_rep[OF Quotient_fset] map_id)
|
|
107 |
have b: "list_rel op \<approx> (map rep_fset (rep_fset a)) (map rep_fset (rep_fset a))"
|
|
108 |
by (rule list_rel_refl)
|
|
109 |
have c: "(op \<approx> OO list_rel op \<approx>) (map rep_fset (rep_fset a)) (map rep_fset (rep_fset a))"
|
|
110 |
by (rule, rule equivp_reflp[OF fset_equivp]) (rule b)
|
|
111 |
show "(list_rel op \<approx> OOO op \<approx>) (map rep_fset (rep_fset a)) (map rep_fset (rep_fset a))"
|
|
112 |
by (rule, rule list_rel_refl) (rule c)
|
|
113 |
show "(list_rel op \<approx> OOO op \<approx>) r s = ((list_rel op \<approx> OOO op \<approx>) r r \<and>
|
|
114 |
(list_rel op \<approx> OOO op \<approx>) s s \<and> abs_fset (map abs_fset r) = abs_fset (map abs_fset s))"
|
|
115 |
proof (intro iffI conjI)
|
|
116 |
show "(list_rel op \<approx> OOO op \<approx>) r r" by (rule compose_list_refl)
|
|
117 |
show "(list_rel op \<approx> OOO op \<approx>) s s" by (rule compose_list_refl)
|
|
118 |
next
|
|
119 |
assume a: "(list_rel op \<approx> OOO op \<approx>) r s"
|
|
120 |
then have b: "map abs_fset r \<approx> map abs_fset s" proof (elim pred_compE)
|
|
121 |
fix b ba
|
|
122 |
assume c: "list_rel op \<approx> r b"
|
|
123 |
assume d: "b \<approx> ba"
|
|
124 |
assume e: "list_rel op \<approx> ba s"
|
|
125 |
have f: "map abs_fset r = map abs_fset b"
|
1938
|
126 |
using Quotient_rel[OF Quotient_fset_list] c by blast
|
|
127 |
have "map abs_fset ba = map abs_fset s"
|
|
128 |
using Quotient_rel[OF Quotient_fset_list] e by blast
|
|
129 |
then have g: "map abs_fset s = map abs_fset ba" by simp
|
|
130 |
then show "map abs_fset r \<approx> map abs_fset s" using d f map_rel_cong by simp
|
1935
|
131 |
qed
|
|
132 |
then show "abs_fset (map abs_fset r) = abs_fset (map abs_fset s)"
|
1938
|
133 |
using Quotient_rel[OF Quotient_fset] by blast
|
1935
|
134 |
next
|
|
135 |
assume a: "(list_rel op \<approx> OOO op \<approx>) r r \<and> (list_rel op \<approx> OOO op \<approx>) s s
|
|
136 |
\<and> abs_fset (map abs_fset r) = abs_fset (map abs_fset s)"
|
|
137 |
then have s: "(list_rel op \<approx> OOO op \<approx>) s s" by simp
|
|
138 |
have d: "map abs_fset r \<approx> map abs_fset s"
|
|
139 |
by (subst Quotient_rel[OF Quotient_fset]) (simp add: a)
|
|
140 |
have b: "map rep_fset (map abs_fset r) \<approx> map rep_fset (map abs_fset s)"
|
|
141 |
by (rule map_rel_cong[OF d])
|
|
142 |
have y: "list_rel op \<approx> (map rep_fset (map abs_fset s)) s"
|
|
143 |
by (fact rep_abs_rsp_left[OF Quotient_fset_list, OF list_rel_refl[of s]])
|
|
144 |
have c: "(op \<approx> OO list_rel op \<approx>) (map rep_fset (map abs_fset r)) s"
|
|
145 |
by (rule pred_compI) (rule b, rule y)
|
|
146 |
have z: "list_rel op \<approx> r (map rep_fset (map abs_fset r))"
|
|
147 |
by (fact rep_abs_rsp[OF Quotient_fset_list, OF list_rel_refl[of r]])
|
|
148 |
then show "(list_rel op \<approx> OOO op \<approx>) r s"
|
|
149 |
using a c pred_compI by simp
|
|
150 |
qed
|
|
151 |
qed
|
|
152 |
|
1909
|
153 |
text {* Respectfullness *}
|
1893
|
154 |
|
1895
|
155 |
lemma [quot_respect]:
|
|
156 |
shows "(op \<approx> ===> op \<approx> ===> op \<approx>) op @ op @"
|
|
157 |
by auto
|
|
158 |
|
1909
|
159 |
lemma [quot_respect]:
|
|
160 |
shows "(op \<approx> ===> op \<approx> ===> op =) sub_list sub_list"
|
|
161 |
by (auto simp add: sub_list_def)
|
|
162 |
|
|
163 |
lemma memb_rsp[quot_respect]:
|
|
164 |
shows "(op = ===> op \<approx> ===> op =) memb memb"
|
|
165 |
by (auto simp add: memb_def)
|
|
166 |
|
|
167 |
lemma nil_rsp[quot_respect]:
|
|
168 |
shows "[] \<approx> []"
|
|
169 |
by simp
|
|
170 |
|
|
171 |
lemma cons_rsp[quot_respect]:
|
|
172 |
shows "(op = ===> op \<approx> ===> op \<approx>) op # op #"
|
|
173 |
by simp
|
|
174 |
|
|
175 |
lemma map_rsp[quot_respect]:
|
|
176 |
shows "(op = ===> op \<approx> ===> op \<approx>) map map"
|
|
177 |
by auto
|
|
178 |
|
|
179 |
lemma set_rsp[quot_respect]:
|
|
180 |
"(op \<approx> ===> op =) set set"
|
|
181 |
by auto
|
|
182 |
|
|
183 |
lemma list_equiv_rsp[quot_respect]:
|
|
184 |
shows "(op \<approx> ===> op \<approx> ===> op =) op \<approx> op \<approx>"
|
|
185 |
by auto
|
|
186 |
|
|
187 |
lemma not_memb_nil:
|
|
188 |
shows "\<not> memb x []"
|
|
189 |
by (simp add: memb_def)
|
|
190 |
|
|
191 |
lemma memb_cons_iff:
|
|
192 |
shows "memb x (y # xs) = (x = y \<or> memb x xs)"
|
|
193 |
by (induct xs) (auto simp add: memb_def)
|
|
194 |
|
|
195 |
lemma memb_finter_raw:
|
|
196 |
"memb x (finter_raw xs ys) \<longleftrightarrow> memb x xs \<and> memb x ys"
|
|
197 |
by (induct xs) (auto simp add: not_memb_nil memb_cons_iff)
|
|
198 |
|
|
199 |
lemma [quot_respect]:
|
|
200 |
"(op \<approx> ===> op \<approx> ===> op \<approx>) finter_raw finter_raw"
|
|
201 |
by (simp add: memb_def[symmetric] memb_finter_raw)
|
|
202 |
|
|
203 |
lemma memb_delete_raw:
|
|
204 |
"memb x (delete_raw xs y) = (memb x xs \<and> x \<noteq> y)"
|
|
205 |
by (induct xs arbitrary: x y) (auto simp add: memb_def)
|
|
206 |
|
|
207 |
lemma [quot_respect]:
|
|
208 |
"(op \<approx> ===> op = ===> op \<approx>) delete_raw delete_raw"
|
|
209 |
by (simp add: memb_def[symmetric] memb_delete_raw)
|
|
210 |
|
|
211 |
lemma fcard_raw_gt_0:
|
|
212 |
assumes a: "x \<in> set xs"
|
|
213 |
shows "0 < fcard_raw xs"
|
|
214 |
using a by (induct xs) (auto simp add: memb_def)
|
|
215 |
|
|
216 |
lemma fcard_raw_delete_one:
|
|
217 |
shows "fcard_raw ([x \<leftarrow> xs. x \<noteq> y]) = (if memb y xs then fcard_raw xs - 1 else fcard_raw xs)"
|
|
218 |
by (induct xs) (auto dest: fcard_raw_gt_0 simp add: memb_def)
|
|
219 |
|
|
220 |
lemma fcard_raw_rsp_aux:
|
|
221 |
assumes a: "xs \<approx> ys"
|
|
222 |
shows "fcard_raw xs = fcard_raw ys"
|
|
223 |
using a
|
|
224 |
apply (induct xs arbitrary: ys)
|
|
225 |
apply (auto simp add: memb_def)
|
|
226 |
apply (subgoal_tac "\<forall>x. (x \<in> set xs) = (x \<in> set ys)")
|
|
227 |
apply (auto)
|
|
228 |
apply (drule_tac x="x" in spec)
|
|
229 |
apply (blast)
|
|
230 |
apply (drule_tac x="[x \<leftarrow> ys. x \<noteq> a]" in meta_spec)
|
|
231 |
apply (simp add: fcard_raw_delete_one memb_def)
|
|
232 |
apply (case_tac "a \<in> set ys")
|
|
233 |
apply (simp only: if_True)
|
|
234 |
apply (subgoal_tac "\<forall>x. (x \<in> set xs) = (x \<in> set ys \<and> x \<noteq> a)")
|
|
235 |
apply (drule Suc_pred'[OF fcard_raw_gt_0])
|
|
236 |
apply (auto)
|
|
237 |
done
|
|
238 |
|
|
239 |
lemma fcard_raw_rsp[quot_respect]:
|
|
240 |
shows "(op \<approx> ===> op =) fcard_raw fcard_raw"
|
|
241 |
by (simp add: fcard_raw_rsp_aux)
|
|
242 |
|
|
243 |
lemma memb_absorb:
|
|
244 |
shows "memb x xs \<Longrightarrow> x # xs \<approx> xs"
|
|
245 |
by (induct xs) (auto simp add: memb_def)
|
|
246 |
|
|
247 |
lemma none_memb_nil:
|
|
248 |
"(\<forall>x. \<not> memb x xs) = (xs \<approx> [])"
|
|
249 |
by (simp add: memb_def)
|
|
250 |
|
|
251 |
lemma not_memb_delete_raw_ident:
|
|
252 |
shows "\<not> memb x xs \<Longrightarrow> delete_raw xs x = xs"
|
|
253 |
by (induct xs) (auto simp add: memb_def)
|
|
254 |
|
|
255 |
lemma memb_commute_ffold_raw:
|
|
256 |
"rsp_fold f \<Longrightarrow> memb h b \<Longrightarrow> ffold_raw f z b = f h (ffold_raw f z (delete_raw b h))"
|
|
257 |
apply (induct b)
|
|
258 |
apply (simp_all add: not_memb_nil)
|
|
259 |
apply (auto)
|
1938
|
260 |
apply (simp_all add: memb_delete_raw not_memb_delete_raw_ident rsp_fold_def memb_cons_iff)
|
1909
|
261 |
done
|
|
262 |
|
|
263 |
lemma ffold_raw_rsp_pre:
|
|
264 |
"\<forall>e. memb e a = memb e b \<Longrightarrow> ffold_raw f z a = ffold_raw f z b"
|
|
265 |
apply (induct a arbitrary: b)
|
|
266 |
apply (simp add: memb_absorb memb_def none_memb_nil)
|
|
267 |
apply (simp)
|
|
268 |
apply (rule conjI)
|
|
269 |
apply (rule_tac [!] impI)
|
|
270 |
apply (rule_tac [!] conjI)
|
|
271 |
apply (rule_tac [!] impI)
|
|
272 |
apply (subgoal_tac "\<forall>e. memb e a2 = memb e b")
|
|
273 |
apply (simp)
|
|
274 |
apply (simp add: memb_cons_iff memb_def)
|
|
275 |
apply (auto)[1]
|
|
276 |
apply (drule_tac x="e" in spec)
|
|
277 |
apply (blast)
|
|
278 |
apply (case_tac b)
|
|
279 |
apply (simp_all)
|
|
280 |
apply (subgoal_tac "ffold_raw f z b = f a1 (ffold_raw f z (delete_raw b a1))")
|
|
281 |
apply (simp only:)
|
|
282 |
apply (rule_tac f="f a1" in arg_cong)
|
|
283 |
apply (subgoal_tac "\<forall>e. memb e a2 = memb e (delete_raw b a1)")
|
|
284 |
apply (simp)
|
|
285 |
apply (simp add: memb_delete_raw)
|
|
286 |
apply (auto simp add: memb_cons_iff)[1]
|
|
287 |
apply (erule memb_commute_ffold_raw)
|
|
288 |
apply (drule_tac x="a1" in spec)
|
|
289 |
apply (simp add: memb_cons_iff)
|
|
290 |
apply (simp add: memb_cons_iff)
|
|
291 |
apply (case_tac b)
|
|
292 |
apply (simp_all)
|
|
293 |
done
|
|
294 |
|
|
295 |
lemma [quot_respect]:
|
|
296 |
"(op = ===> op = ===> op \<approx> ===> op =) ffold_raw ffold_raw"
|
|
297 |
by (simp add: memb_def[symmetric] ffold_raw_rsp_pre)
|
|
298 |
|
1935
|
299 |
lemma concat_rsp_pre:
|
|
300 |
assumes a: "list_rel op \<approx> x x'"
|
|
301 |
and b: "x' \<approx> y'"
|
|
302 |
and c: "list_rel op \<approx> y' y"
|
|
303 |
and d: "\<exists>x\<in>set x. xa \<in> set x"
|
|
304 |
shows "\<exists>x\<in>set y. xa \<in> set x"
|
|
305 |
proof -
|
|
306 |
obtain xb where e: "xb \<in> set x" and f: "xa \<in> set xb" using d by auto
|
|
307 |
have "\<exists>y. y \<in> set x' \<and> xb \<approx> y" by (rule list_rel_find_element[OF e a])
|
|
308 |
then obtain ya where h: "ya \<in> set x'" and i: "xb \<approx> ya" by auto
|
|
309 |
have j: "ya \<in> set y'" using b h by simp
|
|
310 |
have "\<exists>yb. yb \<in> set y \<and> ya \<approx> yb" by (rule list_rel_find_element[OF j c])
|
|
311 |
then show ?thesis using f i by auto
|
|
312 |
qed
|
|
313 |
|
|
314 |
lemma [quot_respect]:
|
|
315 |
shows "(list_rel op \<approx> OOO op \<approx> ===> op \<approx>) concat concat"
|
|
316 |
proof (rule fun_relI, elim pred_compE)
|
|
317 |
fix a b ba bb
|
|
318 |
assume a: "list_rel op \<approx> a ba"
|
|
319 |
assume b: "ba \<approx> bb"
|
|
320 |
assume c: "list_rel op \<approx> bb b"
|
|
321 |
have "\<forall>x. (\<exists>xa\<in>set a. x \<in> set xa) = (\<exists>xa\<in>set b. x \<in> set xa)" proof
|
|
322 |
fix x
|
|
323 |
show "(\<exists>xa\<in>set a. x \<in> set xa) = (\<exists>xa\<in>set b. x \<in> set xa)" proof
|
|
324 |
assume d: "\<exists>xa\<in>set a. x \<in> set xa"
|
|
325 |
show "\<exists>xa\<in>set b. x \<in> set xa" by (rule concat_rsp_pre[OF a b c d])
|
|
326 |
next
|
|
327 |
assume e: "\<exists>xa\<in>set b. x \<in> set xa"
|
|
328 |
have a': "list_rel op \<approx> ba a" by (rule list_rel_symp[OF list_eq_equivp, OF a])
|
|
329 |
have b': "bb \<approx> ba" by (rule equivp_symp[OF list_eq_equivp, OF b])
|
|
330 |
have c': "list_rel op \<approx> b bb" by (rule list_rel_symp[OF list_eq_equivp, OF c])
|
|
331 |
show "\<exists>xa\<in>set a. x \<in> set xa" by (rule concat_rsp_pre[OF c' b' a' e])
|
|
332 |
qed
|
|
333 |
qed
|
|
334 |
then show "concat a \<approx> concat b" by simp
|
|
335 |
qed
|
|
336 |
|
1909
|
337 |
text {* Distributive lattice with bot *}
|
|
338 |
|
1893
|
339 |
lemma sub_list_not_eq:
|
|
340 |
"(sub_list x y \<and> \<not> list_eq x y) = (sub_list x y \<and> \<not> sub_list y x)"
|
|
341 |
by (auto simp add: sub_list_def)
|
|
342 |
|
|
343 |
lemma sub_list_refl:
|
|
344 |
"sub_list x x"
|
|
345 |
by (simp add: sub_list_def)
|
|
346 |
|
|
347 |
lemma sub_list_trans:
|
|
348 |
"sub_list x y \<Longrightarrow> sub_list y z \<Longrightarrow> sub_list x z"
|
|
349 |
by (simp add: sub_list_def)
|
|
350 |
|
|
351 |
lemma sub_list_empty:
|
|
352 |
"sub_list [] x"
|
|
353 |
by (simp add: sub_list_def)
|
|
354 |
|
1905
|
355 |
lemma sub_list_append_left:
|
|
356 |
"sub_list x (x @ y)"
|
|
357 |
by (simp add: sub_list_def)
|
|
358 |
|
|
359 |
lemma sub_list_append_right:
|
|
360 |
"sub_list y (x @ y)"
|
|
361 |
by (simp add: sub_list_def)
|
|
362 |
|
|
363 |
lemma sub_list_inter_left:
|
|
364 |
shows "sub_list (finter_raw x y) x"
|
|
365 |
by (simp add: sub_list_def memb_def[symmetric] memb_finter_raw)
|
|
366 |
|
|
367 |
lemma sub_list_inter_right:
|
|
368 |
shows "sub_list (finter_raw x y) y"
|
|
369 |
by (simp add: sub_list_def memb_def[symmetric] memb_finter_raw)
|
|
370 |
|
|
371 |
lemma sub_list_list_eq:
|
|
372 |
"sub_list x y \<Longrightarrow> sub_list y x \<Longrightarrow> list_eq x y"
|
|
373 |
unfolding sub_list_def list_eq.simps by blast
|
|
374 |
|
|
375 |
lemma sub_list_append:
|
|
376 |
"sub_list y x \<Longrightarrow> sub_list z x \<Longrightarrow> sub_list (y @ z) x"
|
|
377 |
unfolding sub_list_def by auto
|
|
378 |
|
|
379 |
lemma sub_list_inter:
|
|
380 |
"sub_list x y \<Longrightarrow> sub_list x z \<Longrightarrow> sub_list x (finter_raw y z)"
|
|
381 |
by (simp add: sub_list_def memb_def[symmetric] memb_finter_raw)
|
|
382 |
|
|
383 |
lemma append_inter_distrib:
|
|
384 |
"x @ (finter_raw y z) \<approx> finter_raw (x @ y) (x @ z)"
|
|
385 |
apply (induct x)
|
|
386 |
apply (simp_all add: memb_def)
|
|
387 |
apply (simp add: memb_def[symmetric] memb_finter_raw)
|
1909
|
388 |
by (auto simp add: memb_def)
|
1905
|
389 |
|
|
390 |
instantiation fset :: (type) "{bot,distrib_lattice}"
|
1893
|
391 |
begin
|
|
392 |
|
|
393 |
quotient_definition
|
|
394 |
"bot :: 'a fset" is "[] :: 'a list"
|
|
395 |
|
|
396 |
abbreviation
|
|
397 |
fempty ("{||}")
|
|
398 |
where
|
|
399 |
"{||} \<equiv> bot :: 'a fset"
|
|
400 |
|
|
401 |
quotient_definition
|
|
402 |
"less_eq_fset \<Colon> ('a fset \<Rightarrow> 'a fset \<Rightarrow> bool)"
|
|
403 |
is
|
|
404 |
"sub_list \<Colon> ('a list \<Rightarrow> 'a list \<Rightarrow> bool)"
|
|
405 |
|
|
406 |
abbreviation
|
|
407 |
f_subset_eq :: "'a fset \<Rightarrow> 'a fset \<Rightarrow> bool" (infix "|\<subseteq>|" 50)
|
|
408 |
where
|
|
409 |
"xs |\<subseteq>| ys \<equiv> xs \<le> ys"
|
|
410 |
|
|
411 |
definition
|
|
412 |
less_fset:
|
|
413 |
"(xs :: 'a fset) < ys \<equiv> xs \<le> ys \<and> xs \<noteq> ys"
|
|
414 |
|
|
415 |
abbreviation
|
|
416 |
f_subset :: "'a fset \<Rightarrow> 'a fset \<Rightarrow> bool" (infix "|\<subset>|" 50)
|
|
417 |
where
|
|
418 |
"xs |\<subset>| ys \<equiv> xs < ys"
|
|
419 |
|
1895
|
420 |
quotient_definition
|
|
421 |
"sup \<Colon> ('a fset \<Rightarrow> 'a fset \<Rightarrow> 'a fset)"
|
|
422 |
is
|
|
423 |
"(op @) \<Colon> ('a list \<Rightarrow> 'a list \<Rightarrow> 'a list)"
|
|
424 |
|
|
425 |
abbreviation
|
|
426 |
funion (infixl "|\<union>|" 65)
|
|
427 |
where
|
|
428 |
"xs |\<union>| ys \<equiv> sup (xs :: 'a fset) ys"
|
|
429 |
|
1905
|
430 |
quotient_definition
|
|
431 |
"inf \<Colon> ('a fset \<Rightarrow> 'a fset \<Rightarrow> 'a fset)"
|
|
432 |
is
|
|
433 |
"finter_raw \<Colon> ('a list \<Rightarrow> 'a list \<Rightarrow> 'a list)"
|
|
434 |
|
|
435 |
abbreviation
|
|
436 |
finter (infixl "|\<inter>|" 65)
|
|
437 |
where
|
|
438 |
"xs |\<inter>| ys \<equiv> inf (xs :: 'a fset) ys"
|
|
439 |
|
1895
|
440 |
instance
|
|
441 |
proof
|
1905
|
442 |
fix x y z :: "'a fset"
|
|
443 |
show "(x |\<subset>| y) = (x |\<subseteq>| y \<and> \<not> y |\<subseteq>| x)"
|
|
444 |
unfolding less_fset by (lifting sub_list_not_eq)
|
|
445 |
show "x |\<subseteq>| x" by (lifting sub_list_refl)
|
|
446 |
show "{||} |\<subseteq>| x" by (lifting sub_list_empty)
|
1895
|
447 |
show "x |\<subseteq>| x |\<union>| y" by (lifting sub_list_append_left)
|
|
448 |
show "y |\<subseteq>| x |\<union>| y" by (lifting sub_list_append_right)
|
1905
|
449 |
show "x |\<inter>| y |\<subseteq>| x" by (lifting sub_list_inter_left)
|
|
450 |
show "x |\<inter>| y |\<subseteq>| y" by (lifting sub_list_inter_right)
|
|
451 |
show "x |\<union>| (y |\<inter>| z) = x |\<union>| y |\<inter>| (x |\<union>| z)" by (lifting append_inter_distrib)
|
|
452 |
next
|
|
453 |
fix x y z :: "'a fset"
|
|
454 |
assume a: "x |\<subseteq>| y"
|
|
455 |
assume b: "y |\<subseteq>| z"
|
|
456 |
show "x |\<subseteq>| z" using a b by (lifting sub_list_trans)
|
1895
|
457 |
next
|
|
458 |
fix x y :: "'a fset"
|
|
459 |
assume a: "x |\<subseteq>| y"
|
|
460 |
assume b: "y |\<subseteq>| x"
|
|
461 |
show "x = y" using a b by (lifting sub_list_list_eq)
|
|
462 |
next
|
|
463 |
fix x y z :: "'a fset"
|
|
464 |
assume a: "y |\<subseteq>| x"
|
|
465 |
assume b: "z |\<subseteq>| x"
|
|
466 |
show "y |\<union>| z |\<subseteq>| x" using a b by (lifting sub_list_append)
|
1905
|
467 |
next
|
|
468 |
fix x y z :: "'a fset"
|
|
469 |
assume a: "x |\<subseteq>| y"
|
|
470 |
assume b: "x |\<subseteq>| z"
|
|
471 |
show "x |\<subseteq>| y |\<inter>| z" using a b by (lifting sub_list_inter)
|
1895
|
472 |
qed
|
1905
|
473 |
|
1893
|
474 |
end
|
|
475 |
|
1909
|
476 |
section {* Finsert and Membership *}
|
1518
|
477 |
|
|
478 |
quotient_definition
|
1893
|
479 |
"finsert :: 'a \<Rightarrow> 'a fset \<Rightarrow> 'a fset"
|
1518
|
480 |
is "op #"
|
|
481 |
|
|
482 |
syntax
|
|
483 |
"@Finset" :: "args => 'a fset" ("{|(_)|}")
|
|
484 |
|
|
485 |
translations
|
|
486 |
"{|x, xs|}" == "CONST finsert x {|xs|}"
|
|
487 |
"{|x|}" == "CONST finsert x {||}"
|
|
488 |
|
|
489 |
quotient_definition
|
1938
|
490 |
fin (infix "|\<in>|" 50)
|
1518
|
491 |
where
|
1816
|
492 |
"fin :: 'a \<Rightarrow> 'a fset \<Rightarrow> bool" is "memb"
|
1518
|
493 |
|
|
494 |
abbreviation
|
1938
|
495 |
fnotin :: "'a \<Rightarrow> 'a fset \<Rightarrow> bool" (infix "|\<notin>|" 50)
|
1518
|
496 |
where
|
1860
|
497 |
"x |\<notin>| S \<equiv> \<not> (x |\<in>| S)"
|
1518
|
498 |
|
1935
|
499 |
section {* Other constants on the Quotient Type *}
|
|
500 |
|
|
501 |
quotient_definition
|
|
502 |
"fcard :: 'a fset \<Rightarrow> nat"
|
|
503 |
is
|
|
504 |
"fcard_raw"
|
|
505 |
|
|
506 |
quotient_definition
|
|
507 |
"fmap :: ('a \<Rightarrow> 'b) \<Rightarrow> 'a fset \<Rightarrow> 'b fset"
|
|
508 |
is
|
|
509 |
"map"
|
|
510 |
|
|
511 |
quotient_definition
|
|
512 |
"fdelete :: 'a fset \<Rightarrow> 'a \<Rightarrow> 'a fset"
|
|
513 |
is "delete_raw"
|
|
514 |
|
|
515 |
quotient_definition
|
|
516 |
"fset_to_set :: 'a fset \<Rightarrow> 'a set"
|
|
517 |
is "set"
|
|
518 |
|
|
519 |
quotient_definition
|
|
520 |
"ffold :: ('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a fset \<Rightarrow> 'b"
|
|
521 |
is "ffold_raw"
|
|
522 |
|
|
523 |
quotient_definition
|
|
524 |
"fconcat :: ('a fset) fset \<Rightarrow> 'a fset"
|
|
525 |
is
|
|
526 |
"concat"
|
|
527 |
|
|
528 |
text {* Compositional Respectfullness and Preservation *}
|
|
529 |
|
|
530 |
lemma [quot_respect]: "(list_rel op \<approx> OOO op \<approx>) [] []"
|
1938
|
531 |
by (fact compose_list_refl)
|
1935
|
532 |
|
|
533 |
lemma [quot_preserve]: "(abs_fset \<circ> map f) [] = abs_fset []"
|
|
534 |
by simp
|
|
535 |
|
|
536 |
lemma [quot_respect]:
|
|
537 |
"(op \<approx> ===> list_rel op \<approx> OOO op \<approx> ===> list_rel op \<approx> OOO op \<approx>) op # op #"
|
|
538 |
apply auto
|
|
539 |
apply (simp add: set_in_eq)
|
|
540 |
apply (rule_tac b="x # b" in pred_compI)
|
|
541 |
apply auto
|
|
542 |
apply (rule_tac b="x # ba" in pred_compI)
|
|
543 |
apply auto
|
|
544 |
done
|
|
545 |
|
|
546 |
lemma [quot_preserve]:
|
|
547 |
"(rep_fset ---> (map rep_fset \<circ> rep_fset) ---> (abs_fset \<circ> map abs_fset)) op # = finsert"
|
|
548 |
by (simp add: expand_fun_eq Quotient_abs_rep[OF Quotient_fset]
|
|
549 |
abs_o_rep[OF Quotient_fset] map_id finsert_def)
|
|
550 |
|
|
551 |
lemma [quot_preserve]:
|
1938
|
552 |
"((map rep_fset \<circ> rep_fset) ---> (map rep_fset \<circ> rep_fset) ---> (abs_fset \<circ> map abs_fset)) op @ = funion"
|
1935
|
553 |
by (simp add: expand_fun_eq Quotient_abs_rep[OF Quotient_fset]
|
|
554 |
abs_o_rep[OF Quotient_fset] map_id sup_fset_def)
|
|
555 |
|
|
556 |
lemma list_rel_app_l:
|
|
557 |
assumes a: "reflp R"
|
|
558 |
and b: "list_rel R l r"
|
|
559 |
shows "list_rel R (z @ l) (z @ r)"
|
1938
|
560 |
by (induct z) (simp_all add: b rev_iffD1[OF a meta_eq_to_obj_eq[OF reflp_def]])
|
1935
|
561 |
|
|
562 |
lemma append_rsp2_pre0:
|
|
563 |
assumes a:"list_rel op \<approx> x x'"
|
|
564 |
shows "list_rel op \<approx> (x @ z) (x' @ z)"
|
|
565 |
using a apply (induct x x' rule: list_induct2')
|
1938
|
566 |
by simp_all (rule list_rel_refl)
|
1935
|
567 |
|
|
568 |
lemma append_rsp2_pre1:
|
|
569 |
assumes a:"list_rel op \<approx> x x'"
|
|
570 |
shows "list_rel op \<approx> (z @ x) (z @ x')"
|
|
571 |
using a apply (induct x x' arbitrary: z rule: list_induct2')
|
|
572 |
apply (rule list_rel_refl)
|
|
573 |
apply (simp_all del: list_eq.simps)
|
|
574 |
apply (rule list_rel_app_l)
|
|
575 |
apply (simp_all add: reflp_def)
|
|
576 |
done
|
|
577 |
|
|
578 |
lemma append_rsp2_pre:
|
|
579 |
assumes a:"list_rel op \<approx> x x'"
|
|
580 |
and b: "list_rel op \<approx> z z'"
|
|
581 |
shows "list_rel op \<approx> (x @ z) (x' @ z')"
|
|
582 |
apply (rule list_rel_transp[OF fset_equivp])
|
|
583 |
apply (rule append_rsp2_pre0)
|
|
584 |
apply (rule a)
|
|
585 |
using b apply (induct z z' rule: list_induct2')
|
|
586 |
apply (simp_all only: append_Nil2)
|
|
587 |
apply (rule list_rel_refl)
|
|
588 |
apply simp_all
|
|
589 |
apply (rule append_rsp2_pre1)
|
|
590 |
apply simp
|
|
591 |
done
|
|
592 |
|
|
593 |
lemma [quot_respect]:
|
|
594 |
"(list_rel op \<approx> OOO op \<approx> ===> list_rel op \<approx> OOO op \<approx> ===> list_rel op \<approx> OOO op \<approx>) op @ op @"
|
|
595 |
proof (intro fun_relI, elim pred_compE)
|
|
596 |
fix x y z w x' z' y' w' :: "'a list list"
|
|
597 |
assume a:"list_rel op \<approx> x x'"
|
|
598 |
and b: "x' \<approx> y'"
|
|
599 |
and c: "list_rel op \<approx> y' y"
|
|
600 |
assume aa: "list_rel op \<approx> z z'"
|
|
601 |
and bb: "z' \<approx> w'"
|
|
602 |
and cc: "list_rel op \<approx> w' w"
|
|
603 |
have a': "list_rel op \<approx> (x @ z) (x' @ z')" using a aa append_rsp2_pre by auto
|
|
604 |
have b': "x' @ z' \<approx> y' @ w'" using b bb by simp
|
|
605 |
have c': "list_rel op \<approx> (y' @ w') (y @ w)" using c cc append_rsp2_pre by auto
|
|
606 |
have d': "(op \<approx> OO list_rel op \<approx>) (x' @ z') (y @ w)"
|
|
607 |
by (rule pred_compI) (rule b', rule c')
|
|
608 |
show "(list_rel op \<approx> OOO op \<approx>) (x @ z) (y @ w)"
|
|
609 |
by (rule pred_compI) (rule a', rule d')
|
|
610 |
qed
|
|
611 |
|
|
612 |
text {* Raw theorems. Finsert, memb, singleron, sub_list *}
|
1518
|
613 |
|
1892
|
614 |
lemma nil_not_cons:
|
|
615 |
shows "\<not> ([] \<approx> x # xs)"
|
|
616 |
and "\<not> (x # xs \<approx> [])"
|
|
617 |
by auto
|
|
618 |
|
1878
|
619 |
lemma no_memb_nil:
|
|
620 |
"(\<forall>x. \<not> memb x xs) = (xs = [])"
|
|
621 |
by (simp add: memb_def)
|
|
622 |
|
1518
|
623 |
lemma memb_consI1:
|
|
624 |
shows "memb x (x # xs)"
|
|
625 |
by (simp add: memb_def)
|
|
626 |
|
|
627 |
lemma memb_consI2:
|
|
628 |
shows "memb x xs \<Longrightarrow> memb x (y # xs)"
|
|
629 |
by (simp add: memb_def)
|
|
630 |
|
|
631 |
lemma singleton_list_eq:
|
|
632 |
shows "[x] \<approx> [y] \<longleftrightarrow> x = y"
|
|
633 |
by (simp add: id_simps) auto
|
|
634 |
|
1892
|
635 |
lemma sub_list_cons:
|
|
636 |
"sub_list (x # xs) ys = (memb x ys \<and> sub_list xs ys)"
|
|
637 |
by (auto simp add: memb_def sub_list_def)
|
|
638 |
|
1935
|
639 |
text {* Cardinality of finite sets *}
|
1518
|
640 |
|
1813
|
641 |
lemma fcard_raw_0:
|
1860
|
642 |
shows "fcard_raw xs = 0 \<longleftrightarrow> xs \<approx> []"
|
1821
|
643 |
by (induct xs) (auto simp add: memb_def)
|
1813
|
644 |
|
|
645 |
lemma fcard_raw_not_memb:
|
1860
|
646 |
shows "\<not> memb x xs \<longleftrightarrow> fcard_raw (x # xs) = Suc (fcard_raw xs)"
|
1813
|
647 |
by auto
|
|
648 |
|
|
649 |
lemma fcard_raw_suc:
|
1860
|
650 |
assumes a: "fcard_raw xs = Suc n"
|
|
651 |
shows "\<exists>x ys. \<not> (memb x ys) \<and> xs \<approx> (x # ys) \<and> fcard_raw ys = n"
|
|
652 |
using a
|
|
653 |
by (induct xs) (auto simp add: memb_def split: if_splits)
|
1819
|
654 |
|
1935
|
655 |
lemma singleton_fcard_1:
|
1860
|
656 |
shows "set xs = {x} \<Longrightarrow> fcard_raw xs = 1"
|
|
657 |
by (induct xs) (auto simp add: memb_def subset_insert)
|
1819
|
658 |
|
|
659 |
lemma fcard_raw_1:
|
1823
|
660 |
shows "fcard_raw xs = 1 \<longleftrightarrow> (\<exists>x. xs \<approx> [x])"
|
|
661 |
apply (auto dest!: fcard_raw_suc)
|
1819
|
662 |
apply (simp add: fcard_raw_0)
|
1821
|
663 |
apply (rule_tac x="x" in exI)
|
1819
|
664 |
apply simp
|
1821
|
665 |
apply (subgoal_tac "set xs = {x}")
|
1860
|
666 |
apply (drule singleton_fcard_1)
|
1819
|
667 |
apply auto
|
1813
|
668 |
done
|
|
669 |
|
1878
|
670 |
lemma fcard_raw_suc_memb:
|
|
671 |
assumes a: "fcard_raw A = Suc n"
|
|
672 |
shows "\<exists>a. memb a A"
|
|
673 |
using a
|
1938
|
674 |
by (induct A) (auto simp add: memb_def)
|
1878
|
675 |
|
1884
|
676 |
lemma memb_card_not_0:
|
1878
|
677 |
assumes a: "memb a A"
|
|
678 |
shows "\<not>(fcard_raw A = 0)"
|
1883
|
679 |
proof -
|
|
680 |
have "\<not>(\<forall>x. \<not> memb x A)" using a by auto
|
|
681 |
then have "\<not>A \<approx> []" using none_memb_nil[of A] by simp
|
|
682 |
then show ?thesis using fcard_raw_0[of A] by simp
|
|
683 |
qed
|
1878
|
684 |
|
1935
|
685 |
text {* fmap *}
|
1518
|
686 |
|
1813
|
687 |
lemma map_append:
|
1821
|
688 |
"map f (xs @ ys) \<approx> (map f xs) @ (map f ys)"
|
1813
|
689 |
by simp
|
|
690 |
|
1819
|
691 |
lemma memb_append:
|
1823
|
692 |
"memb x (xs @ ys) \<longleftrightarrow> memb x xs \<or> memb x ys"
|
1821
|
693 |
by (induct xs) (simp_all add: not_memb_nil memb_cons_iff)
|
1819
|
694 |
|
1518
|
695 |
lemma cons_left_comm:
|
1821
|
696 |
"x # y # xs \<approx> y # x # xs"
|
|
697 |
by auto
|
1518
|
698 |
|
|
699 |
lemma cons_left_idem:
|
1821
|
700 |
"x # x # xs \<approx> x # xs"
|
|
701 |
by auto
|
1518
|
702 |
|
1819
|
703 |
lemma fset_raw_strong_cases:
|
1822
|
704 |
"(xs = []) \<or> (\<exists>x ys. ((\<not> memb x ys) \<and> (xs \<approx> x # ys)))"
|
1821
|
705 |
apply (induct xs)
|
1518
|
706 |
apply (simp)
|
|
707 |
apply (rule disjI2)
|
|
708 |
apply (erule disjE)
|
|
709 |
apply (rule_tac x="a" in exI)
|
|
710 |
apply (rule_tac x="[]" in exI)
|
1822
|
711 |
apply (simp add: memb_def)
|
1518
|
712 |
apply (erule exE)+
|
1821
|
713 |
apply (case_tac "x = a")
|
1518
|
714 |
apply (rule_tac x="a" in exI)
|
1821
|
715 |
apply (rule_tac x="ys" in exI)
|
1518
|
716 |
apply (simp)
|
1821
|
717 |
apply (rule_tac x="x" in exI)
|
|
718 |
apply (rule_tac x="a # ys" in exI)
|
1822
|
719 |
apply (auto simp add: memb_def)
|
1518
|
720 |
done
|
|
721 |
|
1860
|
722 |
section {* deletion *}
|
|
723 |
|
1819
|
724 |
lemma memb_delete_raw_ident:
|
1860
|
725 |
shows "\<not> memb x (delete_raw xs x)"
|
1821
|
726 |
by (induct xs) (auto simp add: memb_def)
|
1518
|
727 |
|
1819
|
728 |
lemma fset_raw_delete_raw_cases:
|
1821
|
729 |
"xs = [] \<or> (\<exists>x. memb x xs \<and> xs \<approx> x # delete_raw xs x)"
|
|
730 |
by (induct xs) (auto simp add: memb_def)
|
1518
|
731 |
|
1819
|
732 |
lemma fdelete_raw_filter:
|
|
733 |
"delete_raw xs y = [x \<leftarrow> xs. x \<noteq> y]"
|
|
734 |
by (induct xs) simp_all
|
1518
|
735 |
|
1819
|
736 |
lemma fcard_raw_delete:
|
|
737 |
"fcard_raw (delete_raw xs y) = (if memb y xs then fcard_raw xs - 1 else fcard_raw xs)"
|
|
738 |
by (simp add: fdelete_raw_filter fcard_raw_delete_one)
|
1518
|
739 |
|
1819
|
740 |
lemma finter_raw_empty:
|
|
741 |
"finter_raw l [] = []"
|
|
742 |
by (induct l) (simp_all add: not_memb_nil)
|
|
743 |
|
1821
|
744 |
lemma set_cong:
|
|
745 |
shows "(set x = set y) = (x \<approx> y)"
|
|
746 |
by auto
|
1533
|
747 |
|
|
748 |
lemma inj_map_eq_iff:
|
|
749 |
"inj f \<Longrightarrow> (map f l \<approx> map f m) = (l \<approx> m)"
|
|
750 |
by (simp add: expand_set_eq[symmetric] inj_image_eq_iff)
|
|
751 |
|
1888
|
752 |
text {* alternate formulation with a different decomposition principle
|
|
753 |
and a proof of equivalence *}
|
|
754 |
|
|
755 |
inductive
|
|
756 |
list_eq2
|
|
757 |
where
|
|
758 |
"list_eq2 (a # b # xs) (b # a # xs)"
|
|
759 |
| "list_eq2 [] []"
|
|
760 |
| "list_eq2 xs ys \<Longrightarrow> list_eq2 ys xs"
|
|
761 |
| "list_eq2 (a # a # xs) (a # xs)"
|
|
762 |
| "list_eq2 xs ys \<Longrightarrow> list_eq2 (a # xs) (a # ys)"
|
|
763 |
| "\<lbrakk>list_eq2 xs1 xs2; list_eq2 xs2 xs3\<rbrakk> \<Longrightarrow> list_eq2 xs1 xs3"
|
|
764 |
|
|
765 |
lemma list_eq2_refl:
|
|
766 |
shows "list_eq2 xs xs"
|
|
767 |
by (induct xs) (auto intro: list_eq2.intros)
|
|
768 |
|
|
769 |
lemma cons_delete_list_eq2:
|
|
770 |
shows "list_eq2 (a # (delete_raw A a)) (if memb a A then A else a # A)"
|
|
771 |
apply (induct A)
|
|
772 |
apply (simp add: memb_def list_eq2_refl)
|
|
773 |
apply (case_tac "memb a (aa # A)")
|
|
774 |
apply (simp_all only: memb_cons_iff)
|
|
775 |
apply (case_tac [!] "a = aa")
|
1909
|
776 |
apply (simp_all)
|
1888
|
777 |
apply (case_tac "memb a A")
|
|
778 |
apply (auto simp add: memb_def)[2]
|
|
779 |
apply (metis list_eq2.intros(3) list_eq2.intros(4) list_eq2.intros(5) list_eq2.intros(6))
|
1895
|
780 |
apply (metis list_eq2.intros(1) list_eq2.intros(5) list_eq2.intros(6))
|
1888
|
781 |
apply (auto simp add: list_eq2_refl not_memb_delete_raw_ident)
|
|
782 |
done
|
|
783 |
|
|
784 |
lemma memb_delete_list_eq2:
|
|
785 |
assumes a: "memb e r"
|
|
786 |
shows "list_eq2 (e # delete_raw r e) r"
|
|
787 |
using a cons_delete_list_eq2[of e r]
|
|
788 |
by simp
|
|
789 |
|
1909
|
790 |
lemma delete_raw_rsp:
|
|
791 |
"xs \<approx> ys \<Longrightarrow> delete_raw xs x \<approx> delete_raw ys x"
|
|
792 |
by (simp add: memb_def[symmetric] memb_delete_raw)
|
|
793 |
|
1888
|
794 |
lemma list_eq2_equiv:
|
|
795 |
"(l \<approx> r) \<longleftrightarrow> (list_eq2 l r)"
|
|
796 |
proof
|
|
797 |
show "list_eq2 l r \<Longrightarrow> l \<approx> r" by (induct rule: list_eq2.induct) auto
|
1938
|
798 |
next
|
|
799 |
{
|
|
800 |
fix n
|
|
801 |
assume a: "fcard_raw l = n" and b: "l \<approx> r"
|
|
802 |
have "list_eq2 l r"
|
|
803 |
using a b
|
|
804 |
proof (induct n arbitrary: l r)
|
|
805 |
case 0
|
|
806 |
have "fcard_raw l = 0" by fact
|
|
807 |
then have "\<forall>x. \<not> memb x l" using memb_card_not_0[of _ l] by auto
|
|
808 |
then have z: "l = []" using no_memb_nil by auto
|
|
809 |
then have "r = []" using `l \<approx> r` by simp
|
|
810 |
then show ?case using z list_eq2_refl by simp
|
|
811 |
next
|
|
812 |
case (Suc m)
|
|
813 |
have b: "l \<approx> r" by fact
|
|
814 |
have d: "fcard_raw l = Suc m" by fact
|
|
815 |
have "\<exists>a. memb a l" by (rule fcard_raw_suc_memb[OF d])
|
|
816 |
then obtain a where e: "memb a l" by auto
|
|
817 |
then have e': "memb a r" using list_eq.simps[simplified memb_def[symmetric], of l r] b by auto
|
|
818 |
have f: "fcard_raw (delete_raw l a) = m" using fcard_raw_delete[of l a] e d by simp
|
|
819 |
have g: "delete_raw l a \<approx> delete_raw r a" using delete_raw_rsp[OF b] by simp
|
|
820 |
have g': "list_eq2 (delete_raw l a) (delete_raw r a)" by (rule Suc.hyps[OF f g])
|
|
821 |
have h: "list_eq2 (a # delete_raw l a) (a # delete_raw r a)" by (rule list_eq2.intros(5)[OF g'])
|
|
822 |
have i: "list_eq2 l (a # delete_raw l a)"
|
|
823 |
by (rule list_eq2.intros(3)[OF memb_delete_list_eq2[OF e]])
|
|
824 |
have "list_eq2 l (a # delete_raw r a)" by (rule list_eq2.intros(6)[OF i h])
|
|
825 |
then show ?case using list_eq2.intros(6)[OF _ memb_delete_list_eq2[OF e']] by simp
|
|
826 |
qed
|
|
827 |
}
|
|
828 |
then show "l \<approx> r \<Longrightarrow> list_eq2 l r" by blast
|
1888
|
829 |
qed
|
|
830 |
|
1935
|
831 |
text {* Lifted theorems *}
|
1518
|
832 |
|
1819
|
833 |
lemma not_fin_fnil: "x |\<notin>| {||}"
|
|
834 |
by (lifting not_memb_nil)
|
1518
|
835 |
|
|
836 |
lemma fin_finsert_iff[simp]:
|
|
837 |
"x |\<in>| finsert y S = (x = y \<or> x |\<in>| S)"
|
|
838 |
by (lifting memb_cons_iff)
|
|
839 |
|
|
840 |
lemma
|
|
841 |
shows finsertI1: "x |\<in>| finsert x S"
|
|
842 |
and finsertI2: "x |\<in>| S \<Longrightarrow> x |\<in>| finsert y S"
|
|
843 |
by (lifting memb_consI1, lifting memb_consI2)
|
|
844 |
|
|
845 |
lemma finsert_absorb[simp]:
|
|
846 |
shows "x |\<in>| S \<Longrightarrow> finsert x S = S"
|
|
847 |
by (lifting memb_absorb)
|
|
848 |
|
|
849 |
lemma fempty_not_finsert[simp]:
|
1533
|
850 |
"{||} \<noteq> finsert x S"
|
|
851 |
"finsert x S \<noteq> {||}"
|
1518
|
852 |
by (lifting nil_not_cons)
|
|
853 |
|
|
854 |
lemma finsert_left_comm:
|
1822
|
855 |
"finsert x (finsert y S) = finsert y (finsert x S)"
|
1518
|
856 |
by (lifting cons_left_comm)
|
|
857 |
|
|
858 |
lemma finsert_left_idem:
|
1822
|
859 |
"finsert x (finsert x S) = finsert x S"
|
1518
|
860 |
by (lifting cons_left_idem)
|
|
861 |
|
|
862 |
lemma fsingleton_eq[simp]:
|
|
863 |
shows "{|x|} = {|y|} \<longleftrightarrow> x = y"
|
|
864 |
by (lifting singleton_list_eq)
|
|
865 |
|
|
866 |
text {* fset_to_set *}
|
|
867 |
|
1533
|
868 |
lemma fset_to_set_simps[simp]:
|
1819
|
869 |
"fset_to_set {||} = ({} :: 'a set)"
|
|
870 |
"fset_to_set (finsert (h :: 'a) t) = insert h (fset_to_set t)"
|
|
871 |
by (lifting set.simps)
|
1518
|
872 |
|
|
873 |
lemma in_fset_to_set:
|
1822
|
874 |
"x \<in> fset_to_set S \<equiv> x |\<in>| S"
|
1518
|
875 |
by (lifting memb_def[symmetric])
|
|
876 |
|
1819
|
877 |
lemma none_fin_fempty:
|
1824
|
878 |
"(\<forall>x. x |\<notin>| S) = (S = {||})"
|
|
879 |
by (lifting none_memb_nil)
|
1518
|
880 |
|
1533
|
881 |
lemma fset_cong:
|
1822
|
882 |
"(fset_to_set S = fset_to_set T) = (S = T)"
|
1533
|
883 |
by (lifting set_cong)
|
|
884 |
|
1518
|
885 |
text {* fcard *}
|
|
886 |
|
|
887 |
lemma fcard_fempty [simp]:
|
|
888 |
shows "fcard {||} = 0"
|
|
889 |
by (lifting fcard_raw_nil)
|
|
890 |
|
|
891 |
lemma fcard_finsert_if [simp]:
|
|
892 |
shows "fcard (finsert x S) = (if x |\<in>| S then fcard S else Suc (fcard S))"
|
|
893 |
by (lifting fcard_raw_cons)
|
|
894 |
|
1822
|
895 |
lemma fcard_0: "(fcard S = 0) = (S = {||})"
|
1813
|
896 |
by (lifting fcard_raw_0)
|
|
897 |
|
1821
|
898 |
lemma fcard_1:
|
1822
|
899 |
shows "(fcard S = 1) = (\<exists>x. S = {|x|})"
|
1819
|
900 |
by (lifting fcard_raw_1)
|
|
901 |
|
1860
|
902 |
lemma fcard_gt_0:
|
|
903 |
shows "x \<in> fset_to_set S \<Longrightarrow> 0 < fcard S"
|
1518
|
904 |
by (lifting fcard_raw_gt_0)
|
|
905 |
|
1860
|
906 |
lemma fcard_not_fin:
|
|
907 |
shows "(x |\<notin>| S) = (fcard (finsert x S) = Suc (fcard S))"
|
1813
|
908 |
by (lifting fcard_raw_not_memb)
|
|
909 |
|
1822
|
910 |
lemma fcard_suc: "fcard S = Suc n \<Longrightarrow> \<exists>x T. x |\<notin>| T \<and> S = finsert x T \<and> fcard T = n"
|
1813
|
911 |
by (lifting fcard_raw_suc)
|
|
912 |
|
1819
|
913 |
lemma fcard_delete:
|
1822
|
914 |
"fcard (fdelete S y) = (if y |\<in>| S then fcard S - 1 else fcard S)"
|
1819
|
915 |
by (lifting fcard_raw_delete)
|
|
916 |
|
1878
|
917 |
lemma fcard_suc_memb: "fcard A = Suc n \<Longrightarrow> \<exists>a. a |\<in>| A"
|
|
918 |
by (lifting fcard_raw_suc_memb)
|
|
919 |
|
|
920 |
lemma fin_fcard_not_0: "a |\<in>| A \<Longrightarrow> fcard A \<noteq> 0"
|
1887
|
921 |
by (lifting memb_card_not_0)
|
1878
|
922 |
|
1518
|
923 |
text {* funion *}
|
|
924 |
|
|
925 |
lemma funion_simps[simp]:
|
1860
|
926 |
shows "{||} |\<union>| S = S"
|
|
927 |
and "finsert x S |\<union>| T = finsert x (S |\<union>| T)"
|
1518
|
928 |
by (lifting append.simps)
|
|
929 |
|
1887
|
930 |
lemma funion_empty[simp]:
|
|
931 |
shows "S |\<union>| {||} = S"
|
|
932 |
by (lifting append_Nil2)
|
|
933 |
|
|
934 |
lemma singleton_union_left:
|
|
935 |
"{|a|} |\<union>| S = finsert a S"
|
|
936 |
by simp
|
|
937 |
|
|
938 |
lemma singleton_union_right:
|
|
939 |
"S |\<union>| {|a|} = finsert a S"
|
1907
|
940 |
by (subst sup.commute) simp
|
1887
|
941 |
|
1518
|
942 |
section {* Induction and Cases rules for finite sets *}
|
|
943 |
|
|
944 |
lemma fset_strong_cases:
|
1822
|
945 |
"S = {||} \<or> (\<exists>x T. x |\<notin>| T \<and> S = finsert x T)"
|
1819
|
946 |
by (lifting fset_raw_strong_cases)
|
1518
|
947 |
|
|
948 |
lemma fset_exhaust[case_names fempty finsert, cases type: fset]:
|
|
949 |
shows "\<lbrakk>S = {||} \<Longrightarrow> P; \<And>x S'. S = finsert x S' \<Longrightarrow> P\<rbrakk> \<Longrightarrow> P"
|
|
950 |
by (lifting list.exhaust)
|
|
951 |
|
1533
|
952 |
lemma fset_induct_weak[case_names fempty finsert]:
|
1518
|
953 |
shows "\<lbrakk>P {||}; \<And>x S. P S \<Longrightarrow> P (finsert x S)\<rbrakk> \<Longrightarrow> P S"
|
|
954 |
by (lifting list.induct)
|
|
955 |
|
1533
|
956 |
lemma fset_induct[case_names fempty finsert, induct type: fset]:
|
1518
|
957 |
assumes prem1: "P {||}"
|
|
958 |
and prem2: "\<And>x S. \<lbrakk>x |\<notin>| S; P S\<rbrakk> \<Longrightarrow> P (finsert x S)"
|
|
959 |
shows "P S"
|
1533
|
960 |
proof(induct S rule: fset_induct_weak)
|
1518
|
961 |
case fempty
|
|
962 |
show "P {||}" by (rule prem1)
|
|
963 |
next
|
|
964 |
case (finsert x S)
|
|
965 |
have asm: "P S" by fact
|
|
966 |
show "P (finsert x S)"
|
1938
|
967 |
by (cases "x |\<in>| S") (simp_all add: asm prem2)
|
1518
|
968 |
qed
|
|
969 |
|
1533
|
970 |
lemma fset_induct2:
|
|
971 |
"P {||} {||} \<Longrightarrow>
|
|
972 |
(\<And>x xs. x |\<notin>| xs \<Longrightarrow> P (finsert x xs) {||}) \<Longrightarrow>
|
|
973 |
(\<And>y ys. y |\<notin>| ys \<Longrightarrow> P {||} (finsert y ys)) \<Longrightarrow>
|
|
974 |
(\<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>
|
|
975 |
P xsa ysa"
|
|
976 |
apply (induct xsa arbitrary: ysa)
|
|
977 |
apply (induct_tac x rule: fset_induct)
|
|
978 |
apply simp_all
|
|
979 |
apply (induct_tac xa rule: fset_induct)
|
|
980 |
apply simp_all
|
|
981 |
done
|
1518
|
982 |
|
1819
|
983 |
text {* fmap *}
|
|
984 |
|
1533
|
985 |
lemma fmap_simps[simp]:
|
|
986 |
"fmap (f :: 'a \<Rightarrow> 'b) {||} = {||}"
|
1822
|
987 |
"fmap f (finsert x S) = finsert (f x) (fmap f S)"
|
1533
|
988 |
by (lifting map.simps)
|
|
989 |
|
|
990 |
lemma fmap_set_image:
|
1822
|
991 |
"fset_to_set (fmap f S) = f ` (fset_to_set S)"
|
|
992 |
by (induct S) (simp_all)
|
1533
|
993 |
|
|
994 |
lemma inj_fmap_eq_iff:
|
1822
|
995 |
"inj f \<Longrightarrow> (fmap f S = fmap f T) = (S = T)"
|
1533
|
996 |
by (lifting inj_map_eq_iff)
|
|
997 |
|
1822
|
998 |
lemma fmap_funion: "fmap f (S |\<union>| T) = fmap f S |\<union>| fmap f T"
|
1813
|
999 |
by (lifting map_append)
|
|
1000 |
|
1819
|
1001 |
lemma fin_funion:
|
1822
|
1002 |
"x |\<in>| S |\<union>| T \<longleftrightarrow> x |\<in>| S \<or> x |\<in>| T"
|
1819
|
1003 |
by (lifting memb_append)
|
|
1004 |
|
|
1005 |
text {* ffold *}
|
|
1006 |
|
|
1007 |
lemma ffold_nil: "ffold f z {||} = z"
|
|
1008 |
by (lifting ffold_raw.simps(1)[where 'a="'b" and 'b="'a"])
|
|
1009 |
|
|
1010 |
lemma ffold_finsert: "ffold f z (finsert a A) =
|
|
1011 |
(if rsp_fold f then if a |\<in>| A then ffold f z A else f a (ffold f z A) else z)"
|
|
1012 |
by (lifting ffold_raw.simps(2)[where 'a="'b" and 'b="'a"])
|
|
1013 |
|
|
1014 |
lemma fin_commute_ffold:
|
|
1015 |
"\<lbrakk>rsp_fold f; h |\<in>| b\<rbrakk> \<Longrightarrow> ffold f z b = f h (ffold f z (fdelete b h))"
|
|
1016 |
by (lifting memb_commute_ffold_raw)
|
|
1017 |
|
|
1018 |
text {* fdelete *}
|
|
1019 |
|
1822
|
1020 |
lemma fin_fdelete:
|
|
1021 |
shows "x |\<in>| fdelete S y \<longleftrightarrow> x |\<in>| S \<and> x \<noteq> y"
|
1819
|
1022 |
by (lifting memb_delete_raw)
|
|
1023 |
|
1822
|
1024 |
lemma fin_fdelete_ident:
|
|
1025 |
shows "x |\<notin>| fdelete S x"
|
1819
|
1026 |
by (lifting memb_delete_raw_ident)
|
|
1027 |
|
1822
|
1028 |
lemma not_memb_fdelete_ident:
|
|
1029 |
shows "x |\<notin>| S \<Longrightarrow> fdelete S x = S"
|
1819
|
1030 |
by (lifting not_memb_delete_raw_ident)
|
|
1031 |
|
|
1032 |
lemma fset_fdelete_cases:
|
1822
|
1033 |
shows "S = {||} \<or> (\<exists>x. x |\<in>| S \<and> S = finsert x (fdelete S x))"
|
1819
|
1034 |
by (lifting fset_raw_delete_raw_cases)
|
|
1035 |
|
|
1036 |
text {* inter *}
|
|
1037 |
|
1822
|
1038 |
lemma finter_empty_l: "({||} |\<inter>| S) = {||}"
|
1819
|
1039 |
by (lifting finter_raw.simps(1))
|
|
1040 |
|
1822
|
1041 |
lemma finter_empty_r: "(S |\<inter>| {||}) = {||}"
|
1819
|
1042 |
by (lifting finter_raw_empty)
|
|
1043 |
|
|
1044 |
lemma finter_finsert:
|
1822
|
1045 |
"finsert x S |\<inter>| T = (if x |\<in>| T then finsert x (S |\<inter>| T) else S |\<inter>| T)"
|
1819
|
1046 |
by (lifting finter_raw.simps(2))
|
|
1047 |
|
|
1048 |
lemma fin_finter:
|
1822
|
1049 |
"x |\<in>| (S |\<inter>| T) \<longleftrightarrow> x |\<in>| S \<and> x |\<in>| T"
|
1819
|
1050 |
by (lifting memb_finter_raw)
|
|
1051 |
|
1893
|
1052 |
lemma fsubset_finsert:
|
|
1053 |
"(finsert x xs |\<subseteq>| ys) = (x |\<in>| ys \<and> xs |\<subseteq>| ys)"
|
|
1054 |
by (lifting sub_list_cons)
|
|
1055 |
|
1936
|
1056 |
lemma "xs |\<subseteq>| ys \<equiv> \<forall>x. x |\<in>| xs \<longrightarrow> x |\<in>| ys"
|
|
1057 |
by (lifting sub_list_def[simplified memb_def[symmetric]])
|
1893
|
1058 |
|
|
1059 |
lemma fsubset_fin: "xs |\<subseteq>| ys = (\<forall>x. x |\<in>| xs \<longrightarrow> x |\<in>| ys)"
|
|
1060 |
by (rule meta_eq_to_obj_eq)
|
|
1061 |
(lifting sub_list_def[simplified memb_def[symmetric]])
|
|
1062 |
|
1820
|
1063 |
lemma expand_fset_eq:
|
1822
|
1064 |
"(S = T) = (\<forall>x. (x |\<in>| S) = (x |\<in>| T))"
|
1820
|
1065 |
by (lifting list_eq.simps[simplified memb_def[symmetric]])
|
|
1066 |
|
1888
|
1067 |
(* We cannot write it as "assumes .. shows" since Isabelle changes
|
|
1068 |
the quantifiers to schematic variables and reintroduces them in
|
|
1069 |
a different order *)
|
|
1070 |
lemma fset_eq_cases:
|
|
1071 |
"\<lbrakk>a1 = a2;
|
|
1072 |
\<And>a b xs. \<lbrakk>a1 = finsert a (finsert b xs); a2 = finsert b (finsert a xs)\<rbrakk> \<Longrightarrow> P;
|
|
1073 |
\<lbrakk>a1 = {||}; a2 = {||}\<rbrakk> \<Longrightarrow> P; \<And>xs ys. \<lbrakk>a1 = ys; a2 = xs; xs = ys\<rbrakk> \<Longrightarrow> P;
|
|
1074 |
\<And>a xs. \<lbrakk>a1 = finsert a (finsert a xs); a2 = finsert a xs\<rbrakk> \<Longrightarrow> P;
|
|
1075 |
\<And>xs ys a. \<lbrakk>a1 = finsert a xs; a2 = finsert a ys; xs = ys\<rbrakk> \<Longrightarrow> P;
|
|
1076 |
\<And>xs1 xs2 xs3. \<lbrakk>a1 = xs1; a2 = xs3; xs1 = xs2; xs2 = xs3\<rbrakk> \<Longrightarrow> P\<rbrakk>
|
|
1077 |
\<Longrightarrow> P"
|
|
1078 |
by (lifting list_eq2.cases[simplified list_eq2_equiv[symmetric]])
|
|
1079 |
|
|
1080 |
lemma fset_eq_induct:
|
|
1081 |
assumes "x1 = x2"
|
|
1082 |
and "\<And>a b xs. P (finsert a (finsert b xs)) (finsert b (finsert a xs))"
|
|
1083 |
and "P {||} {||}"
|
|
1084 |
and "\<And>xs ys. \<lbrakk>xs = ys; P xs ys\<rbrakk> \<Longrightarrow> P ys xs"
|
|
1085 |
and "\<And>a xs. P (finsert a (finsert a xs)) (finsert a xs)"
|
|
1086 |
and "\<And>xs ys a. \<lbrakk>xs = ys; P xs ys\<rbrakk> \<Longrightarrow> P (finsert a xs) (finsert a ys)"
|
|
1087 |
and "\<And>xs1 xs2 xs3. \<lbrakk>xs1 = xs2; P xs1 xs2; xs2 = xs3; P xs2 xs3\<rbrakk> \<Longrightarrow> P xs1 xs3"
|
|
1088 |
shows "P x1 x2"
|
|
1089 |
using assms
|
|
1090 |
by (lifting list_eq2.induct[simplified list_eq2_equiv[symmetric]])
|
1820
|
1091 |
|
1935
|
1092 |
text {* concat *}
|
|
1093 |
|
|
1094 |
lemma fconcat_empty:
|
|
1095 |
shows "fconcat {||} = {||}"
|
|
1096 |
by (lifting concat.simps(1))
|
|
1097 |
|
|
1098 |
lemma fconcat_insert:
|
|
1099 |
shows "fconcat (finsert x S) = x |\<union>| fconcat S"
|
|
1100 |
by (lifting concat.simps(2))
|
|
1101 |
|
|
1102 |
lemma "fconcat (xs |\<union>| ys) = fconcat xs |\<union>| fconcat ys"
|
|
1103 |
by (lifting concat_append)
|
|
1104 |
|
1533
|
1105 |
ML {*
|
|
1106 |
fun dest_fsetT (Type ("FSet.fset", [T])) = T
|
|
1107 |
| dest_fsetT T = raise TYPE ("dest_fsetT: fset type expected", [T], []);
|
|
1108 |
*}
|
1518
|
1109 |
|
1682
|
1110 |
no_notation
|
|
1111 |
list_eq (infix "\<approx>" 50)
|
|
1112 |
|
1518
|
1113 |
end
|