author | haftmann |
Fri, 26 Aug 2011 23:02:00 +0200 | |
changeset 44556 | c0fd385a41f4 |
parent 43979 | 9f27d2bf4087 |
child 44558 | cc878a312673 |
permissions | -rw-r--r-- |
43241 | 1 |
|
2 |
(* Author: Florian Haftmann, TU Muenchen *) |
|
3 |
||
4 |
header {* implementation of Cset.sets based on lists *} |
|
5 |
||
6 |
theory List_Cset |
|
7 |
imports Cset |
|
8 |
begin |
|
9 |
||
10 |
declare mem_def [simp] |
|
43971
892030194015
added operations to Cset with code equations in backing implementations
Andreas Lochbihler
parents:
43307
diff
changeset
|
11 |
declare Cset.set_code [code del] |
43241 | 12 |
|
13 |
definition coset :: "'a list \<Rightarrow> 'a Cset.set" where |
|
14 |
"coset xs = Set (- set xs)" |
|
15 |
hide_const (open) coset |
|
16 |
||
44556 | 17 |
lemma set_of_coset [simp]: |
18 |
"set_of (List_Cset.coset xs) = - set xs" |
|
19 |
by (simp add: coset_def) |
|
20 |
||
43241 | 21 |
lemma member_coset [simp]: |
44556 | 22 |
"member (List_Cset.coset xs) = (\<lambda>x. x \<in> - set xs)" |
23 |
by (simp add: coset_def fun_eq_iff) |
|
43241 | 24 |
hide_fact (open) member_coset |
25 |
||
43971
892030194015
added operations to Cset with code equations in backing implementations
Andreas Lochbihler
parents:
43307
diff
changeset
|
26 |
code_datatype Cset.set List_Cset.coset |
43241 | 27 |
|
28 |
lemma member_code [code]: |
|
43971
892030194015
added operations to Cset with code equations in backing implementations
Andreas Lochbihler
parents:
43307
diff
changeset
|
29 |
"member (Cset.set xs) = List.member xs" |
43241 | 30 |
"member (List_Cset.coset xs) = Not \<circ> List.member xs" |
44556 | 31 |
by (simp_all add: fun_eq_iff member_def fun_Compl_def member_set) |
43241 | 32 |
|
33 |
definition (in term_syntax) |
|
34 |
setify :: "'a\<Colon>typerep list \<times> (unit \<Rightarrow> Code_Evaluation.term) |
|
35 |
\<Rightarrow> 'a Cset.set \<times> (unit \<Rightarrow> Code_Evaluation.term)" where |
|
43971
892030194015
added operations to Cset with code equations in backing implementations
Andreas Lochbihler
parents:
43307
diff
changeset
|
36 |
[code_unfold]: "setify xs = Code_Evaluation.valtermify Cset.set {\<cdot>} xs" |
43241 | 37 |
|
38 |
notation fcomp (infixl "\<circ>>" 60) |
|
39 |
notation scomp (infixl "\<circ>\<rightarrow>" 60) |
|
40 |
||
41 |
instantiation Cset.set :: (random) random |
|
42 |
begin |
|
43 |
||
44 |
definition |
|
45 |
"Quickcheck.random i = Quickcheck.random i \<circ>\<rightarrow> (\<lambda>xs. Pair (setify xs))" |
|
46 |
||
47 |
instance .. |
|
48 |
||
49 |
end |
|
50 |
||
51 |
no_notation fcomp (infixl "\<circ>>" 60) |
|
52 |
no_notation scomp (infixl "\<circ>\<rightarrow>" 60) |
|
53 |
||
54 |
subsection {* Basic operations *} |
|
55 |
||
56 |
lemma is_empty_set [code]: |
|
43971
892030194015
added operations to Cset with code equations in backing implementations
Andreas Lochbihler
parents:
43307
diff
changeset
|
57 |
"Cset.is_empty (Cset.set xs) \<longleftrightarrow> List.null xs" |
43241 | 58 |
by (simp add: is_empty_set null_def) |
59 |
hide_fact (open) is_empty_set |
|
60 |
||
61 |
lemma empty_set [code]: |
|
43971
892030194015
added operations to Cset with code equations in backing implementations
Andreas Lochbihler
parents:
43307
diff
changeset
|
62 |
"Cset.empty = Cset.set []" |
43241 | 63 |
by (simp add: set_def) |
64 |
hide_fact (open) empty_set |
|
65 |
||
66 |
lemma UNIV_set [code]: |
|
67 |
"top = List_Cset.coset []" |
|
68 |
by (simp add: coset_def) |
|
69 |
hide_fact (open) UNIV_set |
|
70 |
||
71 |
lemma remove_set [code]: |
|
43971
892030194015
added operations to Cset with code equations in backing implementations
Andreas Lochbihler
parents:
43307
diff
changeset
|
72 |
"Cset.remove x (Cset.set xs) = Cset.set (removeAll x xs)" |
43241 | 73 |
"Cset.remove x (List_Cset.coset xs) = List_Cset.coset (List.insert x xs)" |
43971
892030194015
added operations to Cset with code equations in backing implementations
Andreas Lochbihler
parents:
43307
diff
changeset
|
74 |
by (simp_all add: Cset.set_def coset_def) |
43241 | 75 |
(metis List.set_insert More_Set.remove_def remove_set_compl) |
76 |
||
77 |
lemma insert_set [code]: |
|
43971
892030194015
added operations to Cset with code equations in backing implementations
Andreas Lochbihler
parents:
43307
diff
changeset
|
78 |
"Cset.insert x (Cset.set xs) = Cset.set (List.insert x xs)" |
43241 | 79 |
"Cset.insert x (List_Cset.coset xs) = List_Cset.coset (removeAll x xs)" |
43971
892030194015
added operations to Cset with code equations in backing implementations
Andreas Lochbihler
parents:
43307
diff
changeset
|
80 |
by (simp_all add: Cset.set_def coset_def) |
43241 | 81 |
|
82 |
lemma map_set [code]: |
|
43971
892030194015
added operations to Cset with code equations in backing implementations
Andreas Lochbihler
parents:
43307
diff
changeset
|
83 |
"Cset.map f (Cset.set xs) = Cset.set (remdups (List.map f xs))" |
892030194015
added operations to Cset with code equations in backing implementations
Andreas Lochbihler
parents:
43307
diff
changeset
|
84 |
by (simp add: Cset.set_def) |
43241 | 85 |
|
86 |
lemma filter_set [code]: |
|
43971
892030194015
added operations to Cset with code equations in backing implementations
Andreas Lochbihler
parents:
43307
diff
changeset
|
87 |
"Cset.filter P (Cset.set xs) = Cset.set (List.filter P xs)" |
892030194015
added operations to Cset with code equations in backing implementations
Andreas Lochbihler
parents:
43307
diff
changeset
|
88 |
by (simp add: Cset.set_def project_set) |
43241 | 89 |
|
90 |
lemma forall_set [code]: |
|
43971
892030194015
added operations to Cset with code equations in backing implementations
Andreas Lochbihler
parents:
43307
diff
changeset
|
91 |
"Cset.forall P (Cset.set xs) \<longleftrightarrow> list_all P xs" |
892030194015
added operations to Cset with code equations in backing implementations
Andreas Lochbihler
parents:
43307
diff
changeset
|
92 |
by (simp add: Cset.set_def list_all_iff) |
43241 | 93 |
|
94 |
lemma exists_set [code]: |
|
43971
892030194015
added operations to Cset with code equations in backing implementations
Andreas Lochbihler
parents:
43307
diff
changeset
|
95 |
"Cset.exists P (Cset.set xs) \<longleftrightarrow> list_ex P xs" |
892030194015
added operations to Cset with code equations in backing implementations
Andreas Lochbihler
parents:
43307
diff
changeset
|
96 |
by (simp add: Cset.set_def list_ex_iff) |
43241 | 97 |
|
98 |
lemma card_set [code]: |
|
43971
892030194015
added operations to Cset with code equations in backing implementations
Andreas Lochbihler
parents:
43307
diff
changeset
|
99 |
"Cset.card (Cset.set xs) = length (remdups xs)" |
43241 | 100 |
proof - |
101 |
have "Finite_Set.card (set (remdups xs)) = length (remdups xs)" |
|
102 |
by (rule distinct_card) simp |
|
43971
892030194015
added operations to Cset with code equations in backing implementations
Andreas Lochbihler
parents:
43307
diff
changeset
|
103 |
then show ?thesis by (simp add: Cset.set_def) |
43241 | 104 |
qed |
105 |
||
106 |
lemma compl_set [simp, code]: |
|
43971
892030194015
added operations to Cset with code equations in backing implementations
Andreas Lochbihler
parents:
43307
diff
changeset
|
107 |
"- Cset.set xs = List_Cset.coset xs" |
892030194015
added operations to Cset with code equations in backing implementations
Andreas Lochbihler
parents:
43307
diff
changeset
|
108 |
by (simp add: Cset.set_def coset_def) |
43241 | 109 |
|
110 |
lemma compl_coset [simp, code]: |
|
43971
892030194015
added operations to Cset with code equations in backing implementations
Andreas Lochbihler
parents:
43307
diff
changeset
|
111 |
"- List_Cset.coset xs = Cset.set xs" |
892030194015
added operations to Cset with code equations in backing implementations
Andreas Lochbihler
parents:
43307
diff
changeset
|
112 |
by (simp add: Cset.set_def coset_def) |
43241 | 113 |
|
114 |
context complete_lattice |
|
115 |
begin |
|
116 |
||
117 |
lemma Infimum_inf [code]: |
|
43971
892030194015
added operations to Cset with code equations in backing implementations
Andreas Lochbihler
parents:
43307
diff
changeset
|
118 |
"Infimum (Cset.set As) = foldr inf As top" |
43241 | 119 |
"Infimum (List_Cset.coset []) = bot" |
44556 | 120 |
by (simp_all add: Inf_set_foldr) |
43241 | 121 |
|
122 |
lemma Supremum_sup [code]: |
|
43971
892030194015
added operations to Cset with code equations in backing implementations
Andreas Lochbihler
parents:
43307
diff
changeset
|
123 |
"Supremum (Cset.set As) = foldr sup As bot" |
43241 | 124 |
"Supremum (List_Cset.coset []) = top" |
44556 | 125 |
by (simp_all add: Sup_set_foldr) |
43241 | 126 |
|
127 |
end |
|
128 |
||
43979
9f27d2bf4087
fixed code generator setup in List_Cset
Andreas Lochbihler
parents:
43971
diff
changeset
|
129 |
declare Cset.single_code [code del] |
43971
892030194015
added operations to Cset with code equations in backing implementations
Andreas Lochbihler
parents:
43307
diff
changeset
|
130 |
lemma single_set [code]: |
892030194015
added operations to Cset with code equations in backing implementations
Andreas Lochbihler
parents:
43307
diff
changeset
|
131 |
"Cset.single a = Cset.set [a]" |
892030194015
added operations to Cset with code equations in backing implementations
Andreas Lochbihler
parents:
43307
diff
changeset
|
132 |
by(simp add: Cset.single_code) |
892030194015
added operations to Cset with code equations in backing implementations
Andreas Lochbihler
parents:
43307
diff
changeset
|
133 |
hide_fact (open) single_set |
892030194015
added operations to Cset with code equations in backing implementations
Andreas Lochbihler
parents:
43307
diff
changeset
|
134 |
|
892030194015
added operations to Cset with code equations in backing implementations
Andreas Lochbihler
parents:
43307
diff
changeset
|
135 |
lemma bind_set [code]: |
44556 | 136 |
"Cset.bind (Cset.set xs) f = fold (sup \<circ> f) xs (Cset.set [])" |
137 |
by (simp add: Cset.bind_def SUPR_set_fold) |
|
43971
892030194015
added operations to Cset with code equations in backing implementations
Andreas Lochbihler
parents:
43307
diff
changeset
|
138 |
|
892030194015
added operations to Cset with code equations in backing implementations
Andreas Lochbihler
parents:
43307
diff
changeset
|
139 |
lemma pred_of_cset_set [code]: |
892030194015
added operations to Cset with code equations in backing implementations
Andreas Lochbihler
parents:
43307
diff
changeset
|
140 |
"pred_of_cset (Cset.set xs) = foldr sup (map Predicate.single xs) bot" |
892030194015
added operations to Cset with code equations in backing implementations
Andreas Lochbihler
parents:
43307
diff
changeset
|
141 |
proof - |
892030194015
added operations to Cset with code equations in backing implementations
Andreas Lochbihler
parents:
43307
diff
changeset
|
142 |
have "pred_of_cset (Cset.set xs) = Predicate.Pred (\<lambda>x. x \<in> set xs)" |
44556 | 143 |
by (simp add: Cset.pred_of_cset_def member_code member_set) |
43971
892030194015
added operations to Cset with code equations in backing implementations
Andreas Lochbihler
parents:
43307
diff
changeset
|
144 |
moreover have "foldr sup (map Predicate.single xs) bot = \<dots>" |
44556 | 145 |
by (induct xs) (auto simp add: bot_pred_def simp del: mem_def intro: pred_eqI, simp) |
146 |
ultimately show ?thesis by simp |
|
43971
892030194015
added operations to Cset with code equations in backing implementations
Andreas Lochbihler
parents:
43307
diff
changeset
|
147 |
qed |
892030194015
added operations to Cset with code equations in backing implementations
Andreas Lochbihler
parents:
43307
diff
changeset
|
148 |
hide_fact (open) pred_of_cset_set |
43241 | 149 |
|
44556 | 150 |
|
43241 | 151 |
subsection {* Derived operations *} |
152 |
||
153 |
lemma subset_eq_forall [code]: |
|
154 |
"A \<le> B \<longleftrightarrow> Cset.forall (member B) A" |
|
44556 | 155 |
by (simp add: subset_eq member_def) |
43241 | 156 |
|
157 |
lemma subset_subset_eq [code]: |
|
158 |
"A < B \<longleftrightarrow> A \<le> B \<and> \<not> B \<le> (A :: 'a Cset.set)" |
|
159 |
by (fact less_le_not_le) |
|
160 |
||
161 |
instantiation Cset.set :: (type) equal |
|
162 |
begin |
|
163 |
||
164 |
definition [code]: |
|
165 |
"HOL.equal A B \<longleftrightarrow> A \<le> B \<and> B \<le> (A :: 'a Cset.set)" |
|
166 |
||
167 |
instance proof |
|
44556 | 168 |
qed (simp add: equal_set_def set_eq [symmetric] Cset.set_eq_iff fun_eq_iff member_def) |
43241 | 169 |
|
170 |
end |
|
171 |
||
172 |
lemma [code nbe]: |
|
173 |
"HOL.equal (A :: 'a Cset.set) A \<longleftrightarrow> True" |
|
174 |
by (fact equal_refl) |
|
175 |
||
176 |
||
177 |
subsection {* Functorial operations *} |
|
178 |
||
44556 | 179 |
lemma member_cset_of: |
180 |
"member = set_of" |
|
181 |
by (rule ext)+ (simp add: member_def) |
|
182 |
||
43241 | 183 |
lemma inter_project [code]: |
43971
892030194015
added operations to Cset with code equations in backing implementations
Andreas Lochbihler
parents:
43307
diff
changeset
|
184 |
"inf A (Cset.set xs) = Cset.set (List.filter (Cset.member A) xs)" |
43241 | 185 |
"inf A (List_Cset.coset xs) = foldr Cset.remove xs A" |
186 |
proof - |
|
43971
892030194015
added operations to Cset with code equations in backing implementations
Andreas Lochbihler
parents:
43307
diff
changeset
|
187 |
show "inf A (Cset.set xs) = Cset.set (List.filter (member A) xs)" |
44556 | 188 |
by (simp add: inter project_def Cset.set_def member_cset_of) |
43241 | 189 |
have *: "\<And>x::'a. Cset.remove = (\<lambda>x. Set \<circ> More_Set.remove x \<circ> member)" |
44556 | 190 |
by (simp add: fun_eq_iff More_Set.remove_def member_cset_of) |
43241 | 191 |
have "member \<circ> fold (\<lambda>x. Set \<circ> More_Set.remove x \<circ> member) xs = |
192 |
fold More_Set.remove xs \<circ> member" |
|
193 |
by (rule fold_commute) (simp add: fun_eq_iff) |
|
194 |
then have "fold More_Set.remove xs (member A) = |
|
195 |
member (fold (\<lambda>x. Set \<circ> More_Set.remove x \<circ> member) xs A)" |
|
196 |
by (simp add: fun_eq_iff) |
|
197 |
then have "inf A (List_Cset.coset xs) = fold Cset.remove xs A" |
|
44556 | 198 |
by (simp add: Diff_eq [symmetric] minus_set * member_cset_of) |
43241 | 199 |
moreover have "\<And>x y :: 'a. Cset.remove y \<circ> Cset.remove x = Cset.remove x \<circ> Cset.remove y" |
44556 | 200 |
by (auto simp add: More_Set.remove_def * member_cset_of intro: ext) |
43241 | 201 |
ultimately show "inf A (List_Cset.coset xs) = foldr Cset.remove xs A" |
202 |
by (simp add: foldr_fold) |
|
203 |
qed |
|
204 |
||
205 |
lemma subtract_remove [code]: |
|
43971
892030194015
added operations to Cset with code equations in backing implementations
Andreas Lochbihler
parents:
43307
diff
changeset
|
206 |
"A - Cset.set xs = foldr Cset.remove xs A" |
892030194015
added operations to Cset with code equations in backing implementations
Andreas Lochbihler
parents:
43307
diff
changeset
|
207 |
"A - List_Cset.coset xs = Cset.set (List.filter (member A) xs)" |
43241 | 208 |
by (simp_all only: diff_eq compl_set compl_coset inter_project) |
209 |
||
210 |
lemma union_insert [code]: |
|
43971
892030194015
added operations to Cset with code equations in backing implementations
Andreas Lochbihler
parents:
43307
diff
changeset
|
211 |
"sup (Cset.set xs) A = foldr Cset.insert xs A" |
43241 | 212 |
"sup (List_Cset.coset xs) A = List_Cset.coset (List.filter (Not \<circ> member A) xs)" |
213 |
proof - |
|
214 |
have *: "\<And>x::'a. Cset.insert = (\<lambda>x. Set \<circ> Set.insert x \<circ> member)" |
|
44556 | 215 |
by (simp add: fun_eq_iff member_cset_of) |
43241 | 216 |
have "member \<circ> fold (\<lambda>x. Set \<circ> Set.insert x \<circ> member) xs = |
217 |
fold Set.insert xs \<circ> member" |
|
218 |
by (rule fold_commute) (simp add: fun_eq_iff) |
|
219 |
then have "fold Set.insert xs (member A) = |
|
220 |
member (fold (\<lambda>x. Set \<circ> Set.insert x \<circ> member) xs A)" |
|
221 |
by (simp add: fun_eq_iff) |
|
43971
892030194015
added operations to Cset with code equations in backing implementations
Andreas Lochbihler
parents:
43307
diff
changeset
|
222 |
then have "sup (Cset.set xs) A = fold Cset.insert xs A" |
44556 | 223 |
by (simp add: union_set * member_cset_of) |
43241 | 224 |
moreover have "\<And>x y :: 'a. Cset.insert y \<circ> Cset.insert x = Cset.insert x \<circ> Cset.insert y" |
44556 | 225 |
by (auto simp add: * member_cset_of intro: ext) |
43971
892030194015
added operations to Cset with code equations in backing implementations
Andreas Lochbihler
parents:
43307
diff
changeset
|
226 |
ultimately show "sup (Cset.set xs) A = foldr Cset.insert xs A" |
43241 | 227 |
by (simp add: foldr_fold) |
228 |
show "sup (List_Cset.coset xs) A = List_Cset.coset (List.filter (Not \<circ> member A) xs)" |
|
44556 | 229 |
by (auto simp add: coset_def member_cset_of) |
43241 | 230 |
qed |
231 |
||
43307 | 232 |
declare mem_def[simp del] |
233 |
||
44556 | 234 |
end |