6 *) |
6 *) |
7 theory FSet |
7 theory FSet |
8 imports Quotient Quotient_List List |
8 imports Quotient Quotient_List List |
9 begin |
9 begin |
10 |
10 |
|
11 text {* Definiton of List relation and the quotient type *} |
|
12 |
11 fun |
13 fun |
12 list_eq :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool" (infix "\<approx>" 50) |
14 list_eq :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool" (infix "\<approx>" 50) |
13 where |
15 where |
14 "list_eq xs ys = (\<forall>x. x \<in> set xs \<longleftrightarrow> x \<in> set ys)" |
16 "list_eq xs ys = (\<forall>x. x \<in> set xs \<longleftrightarrow> x \<in> set ys)" |
15 |
17 |
16 lemma list_eq_equivp: |
18 lemma list_eq_equivp: |
17 shows "equivp list_eq" |
19 shows "equivp list_eq" |
18 unfolding equivp_reflp_symp_transp |
20 unfolding equivp_reflp_symp_transp |
19 unfolding reflp_def symp_def transp_def |
21 unfolding reflp_def symp_def transp_def |
20 by auto |
22 by auto |
21 |
23 |
|
24 quotient_type |
|
25 'a fset = "'a list" / "list_eq" |
|
26 by (rule list_eq_equivp) |
|
27 |
|
28 text {* Raw definitions *} |
|
29 |
22 definition |
30 definition |
23 memb :: "'a \<Rightarrow> 'a list \<Rightarrow> bool" |
31 memb :: "'a \<Rightarrow> 'a list \<Rightarrow> bool" |
24 where |
32 where |
25 "memb x xs \<equiv> x \<in> set xs" |
33 "memb x xs \<equiv> x \<in> set xs" |
26 |
34 |
27 definition |
35 definition |
28 sub_list :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool" |
36 sub_list :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool" |
29 where |
37 where |
30 "sub_list xs ys \<equiv> (\<forall>x. x \<in> set xs \<longrightarrow> x \<in> set ys)" |
38 "sub_list xs ys \<equiv> (\<forall>x. x \<in> set xs \<longrightarrow> x \<in> set ys)" |
31 |
39 |
32 quotient_type |
40 fun |
33 'a fset = "'a list" / "list_eq" |
41 fcard_raw :: "'a list \<Rightarrow> nat" |
34 by (rule list_eq_equivp) |
42 where |
35 |
43 fcard_raw_nil: "fcard_raw [] = 0" |
36 lemma sub_list_rsp1: "\<lbrakk>xs \<approx> ys\<rbrakk> \<Longrightarrow> sub_list xs zs = sub_list ys zs" |
44 | fcard_raw_cons: "fcard_raw (x # xs) = (if memb x xs then fcard_raw xs else Suc (fcard_raw xs))" |
37 by (simp add: sub_list_def) |
45 |
38 |
46 primrec |
39 lemma sub_list_rsp2: "\<lbrakk>xs \<approx> ys\<rbrakk> \<Longrightarrow> sub_list zs xs = sub_list zs ys" |
47 finter_raw :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" |
40 by (simp add: sub_list_def) |
48 where |
41 |
49 "finter_raw [] l = []" |
42 lemma [quot_respect]: |
50 | "finter_raw (h # t) l = |
43 shows "(op \<approx> ===> op \<approx> ===> op =) sub_list sub_list" |
51 (if memb h l then h # (finter_raw t l) else finter_raw t l)" |
44 by (auto simp only: fun_rel_def sub_list_rsp1 sub_list_rsp2) |
52 |
|
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)))" |
|
63 |
|
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 |
|
74 text {* Respectfullness *} |
45 |
75 |
46 lemma [quot_respect]: |
76 lemma [quot_respect]: |
47 shows "(op \<approx> ===> op \<approx> ===> op \<approx>) op @ op @" |
77 shows "(op \<approx> ===> op \<approx> ===> op \<approx>) op @ op @" |
48 by auto |
78 by auto |
49 |
79 |
|
80 lemma [quot_respect]: |
|
81 shows "(op \<approx> ===> op \<approx> ===> op =) sub_list sub_list" |
|
82 by (auto simp add: sub_list_def) |
|
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 |
|
96 lemma map_rsp[quot_respect]: |
|
97 shows "(op = ===> op \<approx> ===> op \<approx>) map map" |
|
98 by auto |
|
99 |
|
100 lemma set_rsp[quot_respect]: |
|
101 "(op \<approx> ===> op =) set set" |
|
102 by auto |
|
103 |
|
104 lemma list_equiv_rsp[quot_respect]: |
|
105 shows "(op \<approx> ===> op \<approx> ===> op =) op \<approx> op \<approx>" |
|
106 by auto |
|
107 |
|
108 lemma not_memb_nil: |
|
109 shows "\<not> memb x []" |
|
110 by (simp add: memb_def) |
|
111 |
|
112 lemma memb_cons_iff: |
|
113 shows "memb x (y # xs) = (x = y \<or> memb x xs)" |
|
114 by (induct xs) (auto simp add: memb_def) |
|
115 |
|
116 lemma memb_finter_raw: |
|
117 "memb x (finter_raw xs ys) \<longleftrightarrow> memb x xs \<and> memb x ys" |
|
118 by (induct xs) (auto simp add: not_memb_nil memb_cons_iff) |
|
119 |
|
120 lemma [quot_respect]: |
|
121 "(op \<approx> ===> op \<approx> ===> op \<approx>) finter_raw finter_raw" |
|
122 by (simp add: memb_def[symmetric] memb_finter_raw) |
|
123 |
|
124 lemma memb_delete_raw: |
|
125 "memb x (delete_raw xs y) = (memb x xs \<and> x \<noteq> y)" |
|
126 by (induct xs arbitrary: x y) (auto simp add: memb_def) |
|
127 |
|
128 lemma [quot_respect]: |
|
129 "(op \<approx> ===> op = ===> op \<approx>) delete_raw delete_raw" |
|
130 by (simp add: memb_def[symmetric] memb_delete_raw) |
|
131 |
|
132 lemma fcard_raw_gt_0: |
|
133 assumes a: "x \<in> set xs" |
|
134 shows "0 < fcard_raw xs" |
|
135 using a by (induct xs) (auto simp add: memb_def) |
|
136 |
|
137 lemma fcard_raw_delete_one: |
|
138 shows "fcard_raw ([x \<leftarrow> xs. x \<noteq> y]) = (if memb y xs then fcard_raw xs - 1 else fcard_raw xs)" |
|
139 by (induct xs) (auto dest: fcard_raw_gt_0 simp add: memb_def) |
|
140 |
|
141 lemma fcard_raw_rsp_aux: |
|
142 assumes a: "xs \<approx> ys" |
|
143 shows "fcard_raw xs = fcard_raw ys" |
|
144 using a |
|
145 apply (induct xs arbitrary: ys) |
|
146 apply (auto simp add: memb_def) |
|
147 apply (subgoal_tac "\<forall>x. (x \<in> set xs) = (x \<in> set ys)") |
|
148 apply (auto) |
|
149 apply (drule_tac x="x" in spec) |
|
150 apply (blast) |
|
151 apply (drule_tac x="[x \<leftarrow> ys. x \<noteq> a]" in meta_spec) |
|
152 apply (simp add: fcard_raw_delete_one memb_def) |
|
153 apply (case_tac "a \<in> set ys") |
|
154 apply (simp only: if_True) |
|
155 apply (subgoal_tac "\<forall>x. (x \<in> set xs) = (x \<in> set ys \<and> x \<noteq> a)") |
|
156 apply (drule Suc_pred'[OF fcard_raw_gt_0]) |
|
157 apply (auto) |
|
158 done |
|
159 |
|
160 lemma fcard_raw_rsp[quot_respect]: |
|
161 shows "(op \<approx> ===> op =) fcard_raw fcard_raw" |
|
162 by (simp add: fcard_raw_rsp_aux) |
|
163 |
|
164 lemma memb_absorb: |
|
165 shows "memb x xs \<Longrightarrow> x # xs \<approx> xs" |
|
166 by (induct xs) (auto simp add: memb_def) |
|
167 |
|
168 lemma none_memb_nil: |
|
169 "(\<forall>x. \<not> memb x xs) = (xs \<approx> [])" |
|
170 by (simp add: memb_def) |
|
171 |
|
172 lemma not_memb_delete_raw_ident: |
|
173 shows "\<not> memb x xs \<Longrightarrow> delete_raw xs x = xs" |
|
174 by (induct xs) (auto simp add: memb_def) |
|
175 |
|
176 lemma memb_commute_ffold_raw: |
|
177 "rsp_fold f \<Longrightarrow> memb h b \<Longrightarrow> ffold_raw f z b = f h (ffold_raw f z (delete_raw b h))" |
|
178 apply (induct b) |
|
179 apply (simp_all add: not_memb_nil) |
|
180 apply (auto) |
|
181 apply (simp_all add: memb_delete_raw not_memb_delete_raw_ident rsp_fold_def memb_cons_iff) |
|
182 done |
|
183 |
|
184 lemma ffold_raw_rsp_pre: |
|
185 "\<forall>e. memb e a = memb e b \<Longrightarrow> ffold_raw f z a = ffold_raw f z b" |
|
186 apply (induct a arbitrary: b) |
|
187 apply (simp add: memb_absorb memb_def none_memb_nil) |
|
188 apply (simp) |
|
189 apply (rule conjI) |
|
190 apply (rule_tac [!] impI) |
|
191 apply (rule_tac [!] conjI) |
|
192 apply (rule_tac [!] impI) |
|
193 apply (subgoal_tac "\<forall>e. memb e a2 = memb e b") |
|
194 apply (simp) |
|
195 apply (simp add: memb_cons_iff memb_def) |
|
196 apply (auto)[1] |
|
197 apply (drule_tac x="e" in spec) |
|
198 apply (blast) |
|
199 apply (case_tac b) |
|
200 apply (simp_all) |
|
201 apply (subgoal_tac "ffold_raw f z b = f a1 (ffold_raw f z (delete_raw b a1))") |
|
202 apply (simp only:) |
|
203 apply (rule_tac f="f a1" in arg_cong) |
|
204 apply (subgoal_tac "\<forall>e. memb e a2 = memb e (delete_raw b a1)") |
|
205 apply (simp) |
|
206 apply (simp add: memb_delete_raw) |
|
207 apply (auto simp add: memb_cons_iff)[1] |
|
208 apply (erule memb_commute_ffold_raw) |
|
209 apply (drule_tac x="a1" in spec) |
|
210 apply (simp add: memb_cons_iff) |
|
211 apply (simp add: memb_cons_iff) |
|
212 apply (case_tac b) |
|
213 apply (simp_all) |
|
214 done |
|
215 |
|
216 lemma [quot_respect]: |
|
217 "(op = ===> op = ===> op \<approx> ===> op =) ffold_raw ffold_raw" |
|
218 by (simp add: memb_def[symmetric] ffold_raw_rsp_pre) |
|
219 |
|
220 text {* Distributive lattice with bot *} |
|
221 |
50 lemma sub_list_not_eq: |
222 lemma sub_list_not_eq: |
51 "(sub_list x y \<and> \<not> list_eq x y) = (sub_list x y \<and> \<not> sub_list y x)" |
223 "(sub_list x y \<and> \<not> list_eq x y) = (sub_list x y \<and> \<not> sub_list y x)" |
52 by (auto simp add: sub_list_def) |
224 by (auto simp add: sub_list_def) |
53 |
225 |
54 lemma sub_list_refl: |
226 lemma sub_list_refl: |
435 apply (auto simp add: memb_def) |
508 apply (auto simp add: memb_def) |
436 done |
509 done |
437 |
510 |
438 section {* deletion *} |
511 section {* deletion *} |
439 |
512 |
440 fun |
|
441 delete_raw :: "'a list \<Rightarrow> 'a \<Rightarrow> 'a list" |
|
442 where |
|
443 "delete_raw [] x = []" |
|
444 | "delete_raw (a # A) x = (if (a = x) then delete_raw A x else a # (delete_raw A x))" |
|
445 |
|
446 lemma memb_delete_raw: |
|
447 "memb x (delete_raw xs y) = (memb x xs \<and> x \<noteq> y)" |
|
448 by (induct xs arbitrary: x y) (auto simp add: memb_def) |
|
449 |
|
450 lemma delete_raw_rsp: |
|
451 "xs \<approx> ys \<Longrightarrow> delete_raw xs x \<approx> delete_raw ys x" |
|
452 by (simp add: memb_def[symmetric] memb_delete_raw) |
|
453 |
|
454 lemma [quot_respect]: |
|
455 "(op \<approx> ===> op = ===> op \<approx>) delete_raw delete_raw" |
|
456 by (simp add: memb_def[symmetric] memb_delete_raw) |
|
457 |
|
458 lemma memb_delete_raw_ident: |
513 lemma memb_delete_raw_ident: |
459 shows "\<not> memb x (delete_raw xs x)" |
514 shows "\<not> memb x (delete_raw xs x)" |
460 by (induct xs) (auto simp add: memb_def) |
515 by (induct xs) (auto simp add: memb_def) |
461 |
516 |
462 lemma not_memb_delete_raw_ident: |
|
463 shows "\<not> memb x xs \<Longrightarrow> delete_raw xs x = xs" |
|
464 by (induct xs) (auto simp add: memb_def) |
|
465 |
|
466 lemma fset_raw_delete_raw_cases: |
517 lemma fset_raw_delete_raw_cases: |
467 "xs = [] \<or> (\<exists>x. memb x xs \<and> xs \<approx> x # delete_raw xs x)" |
518 "xs = [] \<or> (\<exists>x. memb x xs \<and> xs \<approx> x # delete_raw xs x)" |
468 by (induct xs) (auto simp add: memb_def) |
519 by (induct xs) (auto simp add: memb_def) |
469 |
520 |
470 lemma fdelete_raw_filter: |
521 lemma fdelete_raw_filter: |
473 |
524 |
474 lemma fcard_raw_delete: |
525 lemma fcard_raw_delete: |
475 "fcard_raw (delete_raw xs y) = (if memb y xs then fcard_raw xs - 1 else fcard_raw xs)" |
526 "fcard_raw (delete_raw xs y) = (if memb y xs then fcard_raw xs - 1 else fcard_raw xs)" |
476 by (simp add: fdelete_raw_filter fcard_raw_delete_one) |
527 by (simp add: fdelete_raw_filter fcard_raw_delete_one) |
477 |
528 |
478 lemma set_rsp[quot_respect]: |
529 |
479 "(op \<approx> ===> op =) set set" |
530 |
480 by auto |
531 |
481 |
|
482 definition |
|
483 rsp_fold |
|
484 where |
|
485 "rsp_fold f = (\<forall>u v w. (f u (f v w) = f v (f u w)))" |
|
486 |
|
487 primrec |
|
488 ffold_raw :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a list \<Rightarrow> 'b" |
|
489 where |
|
490 "ffold_raw f z [] = z" |
|
491 | "ffold_raw f z (a # A) = |
|
492 (if (rsp_fold f) then |
|
493 if memb a A then ffold_raw f z A |
|
494 else f a (ffold_raw f z A) |
|
495 else z)" |
|
496 |
|
497 lemma memb_commute_ffold_raw: |
|
498 "rsp_fold f \<Longrightarrow> memb h b \<Longrightarrow> ffold_raw f z b = f h (ffold_raw f z (delete_raw b h))" |
|
499 apply (induct b) |
|
500 apply (simp add: not_memb_nil) |
|
501 apply (simp add: ffold_raw.simps) |
|
502 apply (rule conjI) |
|
503 apply (rule_tac [!] impI) |
|
504 apply (rule_tac [!] conjI) |
|
505 apply (rule_tac [!] impI) |
|
506 apply (simp_all add: memb_delete_raw) |
|
507 apply (simp add: memb_cons_iff) |
|
508 apply (simp add: not_memb_delete_raw_ident) |
|
509 apply (simp add: memb_cons_iff rsp_fold_def) |
|
510 done |
|
511 |
|
512 lemma ffold_raw_rsp_pre: |
|
513 "\<forall>e. memb e a = memb e b \<Longrightarrow> ffold_raw f z a = ffold_raw f z b" |
|
514 apply (induct a arbitrary: b) |
|
515 apply (simp add: hd_in_set memb_absorb memb_def none_memb_nil) |
|
516 apply (simp add: ffold_raw.simps) |
|
517 apply (rule conjI) |
|
518 apply (rule_tac [!] impI) |
|
519 apply (rule_tac [!] conjI) |
|
520 apply (rule_tac [!] impI) |
|
521 apply (subgoal_tac "\<forall>e. memb e a2 = memb e b") |
|
522 apply (simp) |
|
523 apply (simp add: memb_cons_iff memb_def) |
|
524 apply auto |
|
525 apply (drule_tac x="e" in spec) |
|
526 apply blast |
|
527 apply (case_tac b) |
|
528 apply simp_all |
|
529 apply (subgoal_tac "ffold_raw f z b = f a1 (ffold_raw f z (delete_raw b a1))") |
|
530 apply (simp only:) |
|
531 apply (rule_tac f="f a1" in arg_cong) |
|
532 apply (subgoal_tac "\<forall>e. memb e a2 = memb e (delete_raw b a1)") |
|
533 apply simp |
|
534 apply (simp add: memb_delete_raw) |
|
535 apply (auto simp add: memb_cons_iff)[1] |
|
536 apply (erule memb_commute_ffold_raw) |
|
537 apply (drule_tac x="a1" in spec) |
|
538 apply (simp add: memb_cons_iff) |
|
539 apply (simp add: memb_cons_iff) |
|
540 apply (case_tac b) |
|
541 apply simp_all |
|
542 done |
|
543 |
|
544 lemma [quot_respect]: |
|
545 "(op = ===> op = ===> op \<approx> ===> op =) ffold_raw ffold_raw" |
|
546 by (simp add: memb_def[symmetric] ffold_raw_rsp_pre) |
|
547 |
532 |
548 lemma finter_raw_empty: |
533 lemma finter_raw_empty: |
549 "finter_raw l [] = []" |
534 "finter_raw l [] = []" |
550 by (induct l) (simp_all add: not_memb_nil) |
535 by (induct l) (simp_all add: not_memb_nil) |
551 |
536 |