27 quotient_type |
27 quotient_type |
28 'a fset = "'a list" / "list_eq" |
28 'a fset = "'a list" / "list_eq" |
29 by (rule list_eq_equivp) |
29 by (rule list_eq_equivp) |
30 |
30 |
31 text {* |
31 text {* |
32 Definitions of membership, sublist, cardinality, |
32 Definitions of membership, sublist, cardinality, intersection, |
33 intersection etc over lists |
33 difference and respectful fold over lists |
34 *} |
34 *} |
35 |
35 |
36 definition |
36 definition |
37 memb :: "'a \<Rightarrow> 'a list \<Rightarrow> bool" |
|
38 where |
|
39 "memb x xs \<equiv> x \<in> set xs" |
37 "memb x xs \<equiv> x \<in> set xs" |
40 |
38 |
41 definition |
39 definition |
42 sub_list :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool" |
|
43 where |
|
44 "sub_list xs ys \<equiv> set xs \<subseteq> set ys" |
40 "sub_list xs ys \<equiv> set xs \<subseteq> set ys" |
45 |
41 |
46 definition |
42 definition |
47 card_list :: "'a list \<Rightarrow> nat" |
|
48 where |
|
49 "card_list xs = card (set xs)" |
43 "card_list xs = card (set xs)" |
50 |
44 |
51 primrec |
|
52 finter_raw :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" |
|
53 where |
|
54 "finter_raw [] ys = []" |
|
55 | "finter_raw (x # xs) ys = |
|
56 (if x \<in> set ys then x # (finter_raw xs ys) else finter_raw xs ys)" |
|
57 |
|
58 |
|
59 definition |
45 definition |
60 diff_list :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" |
46 "inter_list xs ys = [x \<leftarrow> xs. x \<in> set xs \<and> x \<in> set ys]" |
61 where |
47 |
|
48 definition |
62 "diff_list xs ys \<equiv> [x \<leftarrow> xs. x\<notin>set ys]" |
49 "diff_list xs ys \<equiv> [x \<leftarrow> xs. x\<notin>set ys]" |
63 |
|
64 |
50 |
65 definition |
51 definition |
66 rsp_fold |
52 rsp_fold |
67 where |
53 where |
68 "rsp_fold f = (\<forall>u v w. (f u (f v w) = f v (f u w)))" |
54 "rsp_fold f \<equiv> \<forall>u v w. (f u (f v w) = f v (f u w))" |
69 |
55 |
70 primrec |
56 primrec |
71 ffold_raw :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a list \<Rightarrow> 'b" |
57 ffold_raw :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a list \<Rightarrow> 'b" |
72 where |
58 where |
73 "ffold_raw f z [] = z" |
59 "ffold_raw f z [] = z" |
74 | "ffold_raw f z (a # xs) = |
60 | "ffold_raw f z (a # xs) = |
75 (if (rsp_fold f) then |
61 (if (rsp_fold f) then |
76 if a \<in> set xs then ffold_raw f z xs |
62 if a \<in> set xs then ffold_raw f z xs |
77 else f a (ffold_raw f z xs) |
63 else f a (ffold_raw f z xs) |
78 else z)" |
64 else z)" |
79 |
|
80 |
|
81 |
|
82 lemma set_finter_raw[simp]: |
|
83 shows "set (finter_raw xs ys) = set xs \<inter> set ys" |
|
84 by (induct xs) (auto simp add: memb_def) |
|
85 |
|
86 lemma set_diff_list[simp]: |
|
87 shows "set (diff_list xs ys) = (set xs - set ys)" |
|
88 by (auto simp add: diff_list_def) |
|
89 |
65 |
90 |
66 |
91 section {* Quotient composition lemmas *} |
67 section {* Quotient composition lemmas *} |
92 |
68 |
93 lemma list_all2_refl1: |
69 lemma list_all2_refl1: |
164 using a c pred_compI by simp |
140 using a c pred_compI by simp |
165 qed |
141 qed |
166 qed |
142 qed |
167 |
143 |
168 |
144 |
169 text {* Respectfulness *} |
145 subsection {* Respectfulness lemmas for list operations *} |
170 |
146 |
171 lemma append_rsp[quot_respect]: |
147 lemma list_equiv_rsp [quot_respect]: |
|
148 shows "(op \<approx> ===> op \<approx> ===> op =) op \<approx> op \<approx>" |
|
149 by auto |
|
150 |
|
151 lemma append_rsp [quot_respect]: |
172 shows "(op \<approx> ===> op \<approx> ===> op \<approx>) append append" |
152 shows "(op \<approx> ===> op \<approx> ===> op \<approx>) append append" |
173 by simp |
153 by simp |
174 |
154 |
175 lemma sub_list_rsp[quot_respect]: |
155 lemma sub_list_rsp [quot_respect]: |
176 shows "(op \<approx> ===> op \<approx> ===> op =) sub_list sub_list" |
156 shows "(op \<approx> ===> op \<approx> ===> op =) sub_list sub_list" |
177 by (auto simp add: sub_list_def) |
157 by (auto simp add: sub_list_def) |
178 |
158 |
179 lemma memb_rsp[quot_respect]: |
159 lemma memb_rsp [quot_respect]: |
180 shows "(op = ===> op \<approx> ===> op =) memb memb" |
160 shows "(op = ===> op \<approx> ===> op =) memb memb" |
181 by (auto simp add: memb_def) |
161 by (auto simp add: memb_def) |
182 |
162 |
183 lemma nil_rsp[quot_respect]: |
163 lemma nil_rsp [quot_respect]: |
184 shows "(op \<approx>) Nil Nil" |
164 shows "(op \<approx>) Nil Nil" |
185 by simp |
165 by simp |
186 |
166 |
187 lemma cons_rsp[quot_respect]: |
167 lemma cons_rsp [quot_respect]: |
188 shows "(op = ===> op \<approx> ===> op \<approx>) Cons Cons" |
168 shows "(op = ===> op \<approx> ===> op \<approx>) Cons Cons" |
189 by simp |
169 by simp |
190 |
170 |
191 lemma map_rsp[quot_respect]: |
171 lemma map_rsp [quot_respect]: |
192 shows "(op = ===> op \<approx> ===> op \<approx>) map map" |
172 shows "(op = ===> op \<approx> ===> op \<approx>) map map" |
193 by auto |
173 by auto |
194 |
174 |
195 lemma set_rsp[quot_respect]: |
175 lemma set_rsp [quot_respect]: |
196 "(op \<approx> ===> op =) set set" |
176 "(op \<approx> ===> op =) set set" |
197 by auto |
177 by auto |
198 |
178 |
199 lemma list_equiv_rsp[quot_respect]: |
179 lemma inter_list_rsp [quot_respect]: |
200 shows "(op \<approx> ===> op \<approx> ===> op =) op \<approx> op \<approx>" |
180 shows "(op \<approx> ===> op \<approx> ===> op \<approx>) inter_list inter_list" |
201 by auto |
181 by (simp add: inter_list_def) |
202 |
182 |
203 lemma finter_raw_rsp[quot_respect]: |
183 lemma removeAll_rsp [quot_respect]: |
204 shows "(op \<approx> ===> op \<approx> ===> op \<approx>) finter_raw finter_raw" |
|
205 by simp |
|
206 |
|
207 lemma removeAll_rsp[quot_respect]: |
|
208 shows "(op = ===> op \<approx> ===> op \<approx>) removeAll removeAll" |
184 shows "(op = ===> op \<approx> ===> op \<approx>) removeAll removeAll" |
209 by simp |
185 by simp |
210 |
186 |
211 lemma diff_list_rsp[quot_respect]: |
187 lemma diff_list_rsp [quot_respect]: |
212 shows "(op \<approx> ===> op \<approx> ===> op \<approx>) diff_list diff_list" |
188 shows "(op \<approx> ===> op \<approx> ===> op \<approx>) diff_list diff_list" |
213 by simp |
189 by (simp add: diff_list_def) |
214 |
190 |
215 lemma card_list_rsp[quot_respect]: |
191 lemma card_list_rsp [quot_respect]: |
216 shows "(op \<approx> ===> op =) card_list card_list" |
192 shows "(op \<approx> ===> op =) card_list card_list" |
217 by (simp add: card_list_def) |
193 by (simp add: card_list_def) |
218 |
194 |
219 |
195 lemma filter_rsp [quot_respect]: |
|
196 shows "(op = ===> op \<approx> ===> op \<approx>) filter filter" |
|
197 by simp |
220 |
198 |
221 lemma memb_commute_ffold_raw: |
199 lemma memb_commute_ffold_raw: |
222 "rsp_fold f \<Longrightarrow> h \<in> set b \<Longrightarrow> ffold_raw f z b = f h (ffold_raw f z (removeAll h b))" |
200 "rsp_fold f \<Longrightarrow> h \<in> set b \<Longrightarrow> ffold_raw f z b = f h (ffold_raw f z (removeAll h b))" |
223 apply (induct b) |
201 apply (induct b) |
224 apply (auto simp add: rsp_fold_def) |
202 apply (auto simp add: rsp_fold_def) |
260 have "ya \<in> set y'" using b h by simp |
238 have "ya \<in> set y'" using b h by simp |
261 then have "\<exists>yb. yb \<in> set y \<and> ya \<approx> yb" using c by (rule list_all2_find_element) |
239 then have "\<exists>yb. yb \<in> set y \<and> ya \<approx> yb" using c by (rule list_all2_find_element) |
262 then show ?thesis using f i by auto |
240 then show ?thesis using f i by auto |
263 qed |
241 qed |
264 |
242 |
265 lemma concat_rsp[quot_respect]: |
243 lemma concat_rsp [quot_respect]: |
266 shows "(list_all2 op \<approx> OOO op \<approx> ===> op \<approx>) concat concat" |
244 shows "(list_all2 op \<approx> OOO op \<approx> ===> op \<approx>) concat concat" |
267 proof (rule fun_relI, elim pred_compE) |
245 proof (rule fun_relI, elim pred_compE) |
268 fix a b ba bb |
246 fix a b ba bb |
269 assume a: "list_all2 op \<approx> a ba" |
247 assume a: "list_all2 op \<approx> a ba" |
270 assume b: "ba \<approx> bb" |
248 assume b: "ba \<approx> bb" |
285 qed |
263 qed |
286 qed |
264 qed |
287 then show "concat a \<approx> concat b" by auto |
265 then show "concat a \<approx> concat b" by auto |
288 qed |
266 qed |
289 |
267 |
290 lemma [quot_respect]: |
268 |
291 shows "(op = ===> op \<approx> ===> op \<approx>) filter filter" |
269 subsection {* Finite sets are a bounded, distributive lattice with minus *} |
292 by auto |
|
293 |
|
294 |
270 |
295 instantiation fset :: (type) "{bounded_lattice_bot, distrib_lattice, minus}" |
271 instantiation fset :: (type) "{bounded_lattice_bot, distrib_lattice, minus}" |
296 begin |
272 begin |
297 |
273 |
298 quotient_definition |
274 quotient_definition |
299 "bot :: 'a fset" is "[] :: 'a list" |
275 "bot :: 'a fset" |
|
276 is "Nil :: 'a list" |
300 |
277 |
301 abbreviation |
278 abbreviation |
302 fempty ("{||}") |
279 fempty ("{||}") |
303 where |
280 where |
304 "{||} \<equiv> bot :: 'a fset" |
281 "{||} \<equiv> bot :: 'a fset" |
305 |
282 |
306 quotient_definition |
283 quotient_definition |
307 "less_eq_fset \<Colon> ('a fset \<Rightarrow> 'a fset \<Rightarrow> bool)" |
284 "less_eq_fset :: ('a fset \<Rightarrow> 'a fset \<Rightarrow> bool)" |
308 is |
285 is "sub_list :: ('a list \<Rightarrow> 'a list \<Rightarrow> bool)" |
309 "sub_list \<Colon> ('a list \<Rightarrow> 'a list \<Rightarrow> bool)" |
|
310 |
286 |
311 abbreviation |
287 abbreviation |
312 f_subset_eq :: "'a fset \<Rightarrow> 'a fset \<Rightarrow> bool" (infix "|\<subseteq>|" 50) |
288 f_subset_eq :: "'a fset \<Rightarrow> 'a fset \<Rightarrow> bool" (infix "|\<subseteq>|" 50) |
313 where |
289 where |
314 "xs |\<subseteq>| ys \<equiv> xs \<le> ys" |
290 "xs |\<subseteq>| ys \<equiv> xs \<le> ys" |
323 where |
299 where |
324 "xs |\<subset>| ys \<equiv> xs < ys" |
300 "xs |\<subset>| ys \<equiv> xs < ys" |
325 |
301 |
326 quotient_definition |
302 quotient_definition |
327 "sup :: 'a fset \<Rightarrow> 'a fset \<Rightarrow> 'a fset" |
303 "sup :: 'a fset \<Rightarrow> 'a fset \<Rightarrow> 'a fset" |
328 is |
304 is "append :: 'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" |
329 "append :: 'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" |
|
330 |
305 |
331 abbreviation |
306 abbreviation |
332 funion (infixl "|\<union>|" 65) |
307 funion (infixl "|\<union>|" 65) |
333 where |
308 where |
334 "xs |\<union>| ys \<equiv> sup xs (ys::'a fset)" |
309 "xs |\<union>| ys \<equiv> sup xs (ys::'a fset)" |
335 |
310 |
336 quotient_definition |
311 quotient_definition |
337 "inf :: 'a fset \<Rightarrow> 'a fset \<Rightarrow> 'a fset" |
312 "inf :: 'a fset \<Rightarrow> 'a fset \<Rightarrow> 'a fset" |
338 is |
313 is "inter_list :: 'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" |
339 "finter_raw :: 'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" |
|
340 |
314 |
341 abbreviation |
315 abbreviation |
342 finter (infixl "|\<inter>|" 65) |
316 finter (infixl "|\<inter>|" 65) |
343 where |
317 where |
344 "xs |\<inter>| ys \<equiv> inf xs (ys::'a fset)" |
318 "xs |\<inter>| ys \<equiv> inf xs (ys::'a fset)" |
345 |
319 |
346 quotient_definition |
320 quotient_definition |
347 "minus :: 'a fset \<Rightarrow> 'a fset \<Rightarrow> 'a fset" |
321 "minus :: 'a fset \<Rightarrow> 'a fset \<Rightarrow> 'a fset" |
348 is |
322 is "diff_list :: 'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" |
349 "diff_list :: 'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" |
323 |
350 |
|
351 lemma append_finter_raw_distrib: |
|
352 "x @ (finter_raw y z) \<approx> finter_raw (x @ y) (x @ z)" |
|
353 apply (induct x) |
|
354 apply (auto) |
|
355 done |
|
356 |
324 |
357 instance |
325 instance |
358 proof |
326 proof |
359 fix x y z :: "'a fset" |
327 fix x y z :: "'a fset" |
360 show "x |\<subset>| y \<longleftrightarrow> x |\<subseteq>| y \<and> \<not> y |\<subseteq>| x" |
328 show "x |\<subset>| y \<longleftrightarrow> x |\<subseteq>| y \<and> \<not> y |\<subseteq>| x" |
363 show "x |\<subseteq>| x" by (descending) (simp add: sub_list_def) |
331 show "x |\<subseteq>| x" by (descending) (simp add: sub_list_def) |
364 show "{||} |\<subseteq>| x" by (descending) (simp add: sub_list_def) |
332 show "{||} |\<subseteq>| x" by (descending) (simp add: sub_list_def) |
365 show "x |\<subseteq>| x |\<union>| y" by (descending) (simp add: sub_list_def) |
333 show "x |\<subseteq>| x |\<union>| y" by (descending) (simp add: sub_list_def) |
366 show "y |\<subseteq>| x |\<union>| y" by (descending) (simp add: sub_list_def) |
334 show "y |\<subseteq>| x |\<union>| y" by (descending) (simp add: sub_list_def) |
367 show "x |\<inter>| y |\<subseteq>| x" |
335 show "x |\<inter>| y |\<subseteq>| x" |
368 by (descending) (simp add: sub_list_def memb_def[symmetric]) |
336 by (descending) (auto simp add: inter_list_def sub_list_def memb_def) |
369 show "x |\<inter>| y |\<subseteq>| y" |
337 show "x |\<inter>| y |\<subseteq>| y" |
370 by (descending) (simp add: sub_list_def memb_def[symmetric]) |
338 by (descending) (auto simp add: inter_list_def sub_list_def memb_def) |
371 show "x |\<union>| (y |\<inter>| z) = x |\<union>| y |\<inter>| (x |\<union>| z)" |
339 show "x |\<union>| (y |\<inter>| z) = x |\<union>| y |\<inter>| (x |\<union>| z)" |
372 by (descending) (rule append_finter_raw_distrib) |
340 by (descending) (auto simp add: inter_list_def) |
373 next |
341 next |
374 fix x y z :: "'a fset" |
342 fix x y z :: "'a fset" |
375 assume a: "x |\<subseteq>| y" |
343 assume a: "x |\<subseteq>| y" |
376 assume b: "y |\<subseteq>| z" |
344 assume b: "y |\<subseteq>| z" |
377 show "x |\<subseteq>| z" using a b |
345 show "x |\<subseteq>| z" using a b |
746 shows "fset (fdelete x xs) = fset xs - {x}" |
714 shows "fset (fdelete x xs) = fset xs - {x}" |
747 by (descending) (simp) |
715 by (descending) (simp) |
748 |
716 |
749 lemma finter_set [simp]: |
717 lemma finter_set [simp]: |
750 shows "fset (xs |\<inter>| ys) = fset xs \<inter> fset ys" |
718 shows "fset (xs |\<inter>| ys) = fset xs \<inter> fset ys" |
751 by (lifting set_finter_raw) |
719 by (descending) (auto simp add: inter_list_def) |
752 |
720 |
753 lemma funion_set [simp]: |
721 lemma funion_set [simp]: |
754 shows "fset (xs |\<union>| ys) = fset xs \<union> fset ys" |
722 shows "fset (xs |\<union>| ys) = fset xs \<union> fset ys" |
755 by (lifting set_append) |
723 by (lifting set_append) |
756 |
724 |
757 lemma fminus_set [simp]: |
725 lemma fminus_set [simp]: |
758 shows "fset (xs - ys) = fset xs - fset ys" |
726 shows "fset (xs - ys) = fset xs - fset ys" |
759 by (lifting set_diff_list) |
727 by (descending) (auto simp add: diff_list_def) |
760 |
728 |
761 |
729 |
762 subsection {* funion *} |
730 subsection {* funion *} |
763 |
731 |
764 lemmas [simp] = |
732 lemmas [simp] = |
784 |
752 |
785 subsection {* fminus *} |
753 subsection {* fminus *} |
786 |
754 |
787 lemma fminus_fin: |
755 lemma fminus_fin: |
788 shows "x |\<in>| (xs - ys) \<longleftrightarrow> x |\<in>| xs \<and> x |\<notin>| ys" |
756 shows "x |\<in>| (xs - ys) \<longleftrightarrow> x |\<in>| xs \<and> x |\<notin>| ys" |
789 by (descending) (simp add: memb_def) |
757 by (descending) (simp add: diff_list_def memb_def) |
790 |
758 |
791 lemma fminus_red: |
759 lemma fminus_red: |
792 shows "finsert x xs - ys = (if x |\<in>| ys then xs - ys else finsert x (xs - ys))" |
760 shows "finsert x xs - ys = (if x |\<in>| ys then xs - ys else finsert x (xs - ys))" |
793 by (descending) (auto simp add: memb_def) |
761 by (descending) (auto simp add: diff_list_def memb_def) |
794 |
762 |
795 lemma fminus_red_fin[simp]: |
763 lemma fminus_red_fin[simp]: |
796 shows "x |\<in>| ys \<Longrightarrow> finsert x xs - ys = xs - ys" |
764 shows "x |\<in>| ys \<Longrightarrow> finsert x xs - ys = xs - ys" |
797 by (simp add: fminus_red) |
765 by (simp add: fminus_red) |
798 |
766 |
840 shows "S |\<inter>| {||} = {||}" |
808 shows "S |\<inter>| {||} = {||}" |
841 by simp |
809 by simp |
842 |
810 |
843 lemma finter_finsert: |
811 lemma finter_finsert: |
844 shows "finsert x S |\<inter>| T = (if x |\<in>| T then finsert x (S |\<inter>| T) else S |\<inter>| T)" |
812 shows "finsert x S |\<inter>| T = (if x |\<in>| T then finsert x (S |\<inter>| T) else S |\<inter>| T)" |
845 by (descending) (simp add: memb_def) |
813 by (descending) (auto simp add: inter_list_def memb_def) |
846 |
814 |
847 lemma fin_finter: |
815 lemma fin_finter: |
848 shows "x |\<in>| (S |\<inter>| T) \<longleftrightarrow> x |\<in>| S \<and> x |\<in>| T" |
816 shows "x |\<in>| (S |\<inter>| T) \<longleftrightarrow> x |\<in>| S \<and> x |\<in>| T" |
849 by (descending) (simp add: memb_def) |
817 by (descending) (simp add: inter_list_def memb_def) |
850 |
818 |
851 |
819 |
852 subsection {* fsubset *} |
820 subsection {* fsubset *} |
853 |
821 |
854 lemma fsubseteq_set: |
822 lemma fsubseteq_set: |