author | blanchet |
Mon, 26 Nov 2012 13:35:05 +0100 | |
changeset 50222 | 40e3c3be6bca |
parent 47937 | 70375fa2679d |
child 50227 | 01d545993e8c |
permissions | -rw-r--r-- |
47660 | 1 |
(* Title: HOL/Quotient_Examples/Lift_FSet.thy |
2 |
Author: Brian Huffman, TU Munich |
|
3 |
*) |
|
4 |
||
5 |
header {* Lifting and transfer with a finite set type *} |
|
6 |
||
7 |
theory Lift_FSet |
|
8 |
imports "~~/src/HOL/Library/Quotient_List" |
|
9 |
begin |
|
10 |
||
11 |
subsection {* Equivalence relation and quotient type definition *} |
|
12 |
||
13 |
definition list_eq :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool" |
|
14 |
where [simp]: "list_eq xs ys \<longleftrightarrow> set xs = set ys" |
|
15 |
||
16 |
lemma reflp_list_eq: "reflp list_eq" |
|
17 |
unfolding reflp_def by simp |
|
18 |
||
19 |
lemma symp_list_eq: "symp list_eq" |
|
20 |
unfolding symp_def by simp |
|
21 |
||
22 |
lemma transp_list_eq: "transp list_eq" |
|
23 |
unfolding transp_def by simp |
|
24 |
||
25 |
lemma equivp_list_eq: "equivp list_eq" |
|
26 |
by (intro equivpI reflp_list_eq symp_list_eq transp_list_eq) |
|
27 |
||
28 |
quotient_type 'a fset = "'a list" / "list_eq" |
|
29 |
by (rule equivp_list_eq) |
|
30 |
||
31 |
subsection {* Lifted constant definitions *} |
|
32 |
||
33 |
lift_definition fnil :: "'a fset" is "[]" |
|
34 |
by simp |
|
35 |
||
36 |
lift_definition fcons :: "'a \<Rightarrow> 'a fset \<Rightarrow> 'a fset" is Cons |
|
37 |
by simp |
|
38 |
||
39 |
lift_definition fappend :: "'a fset \<Rightarrow> 'a fset \<Rightarrow> 'a fset" is append |
|
40 |
by simp |
|
41 |
||
42 |
lift_definition fmap :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a fset \<Rightarrow> 'b fset" is map |
|
43 |
by simp |
|
44 |
||
45 |
lift_definition ffilter :: "('a \<Rightarrow> bool) \<Rightarrow> 'a fset \<Rightarrow> 'a fset" is filter |
|
46 |
by simp |
|
47 |
||
48 |
lift_definition fset :: "'a fset \<Rightarrow> 'a set" is set |
|
49 |
by simp |
|
50 |
||
51 |
text {* Constants with nested types (like concat) yield a more |
|
52 |
complicated proof obligation. *} |
|
53 |
||
54 |
lemma list_all2_cr_fset: |
|
55 |
"list_all2 cr_fset xs ys \<longleftrightarrow> map abs_fset xs = ys" |
|
56 |
unfolding cr_fset_def |
|
57 |
apply safe |
|
58 |
apply (erule list_all2_induct, simp, simp) |
|
59 |
apply (simp add: list_all2_map2 List.list_all2_refl) |
|
60 |
done |
|
61 |
||
62 |
lemma abs_fset_eq_iff: "abs_fset xs = abs_fset ys \<longleftrightarrow> list_eq xs ys" |
|
63 |
using Quotient_rel [OF Quotient_fset] by simp |
|
64 |
||
65 |
lift_definition fconcat :: "'a fset fset \<Rightarrow> 'a fset" is concat |
|
66 |
proof - |
|
67 |
fix xss yss :: "'a list list" |
|
68 |
assume "(list_all2 cr_fset OO list_eq OO (list_all2 cr_fset)\<inverse>\<inverse>) xss yss" |
|
69 |
then obtain uss vss where |
|
70 |
"list_all2 cr_fset xss uss" and "list_eq uss vss" and |
|
71 |
"list_all2 cr_fset yss vss" by clarsimp |
|
72 |
hence "list_eq (map abs_fset xss) (map abs_fset yss)" |
|
73 |
unfolding list_all2_cr_fset by simp |
|
74 |
thus "list_eq (concat xss) (concat yss)" |
|
75 |
apply (simp add: set_eq_iff image_def) |
|
76 |
apply safe |
|
77 |
apply (rename_tac xs, drule_tac x="abs_fset xs" in spec) |
|
78 |
apply (drule iffD1, fast, clarsimp simp add: abs_fset_eq_iff, fast) |
|
79 |
apply (rename_tac xs, drule_tac x="abs_fset xs" in spec) |
|
80 |
apply (drule iffD2, fast, clarsimp simp add: abs_fset_eq_iff, fast) |
|
81 |
done |
|
82 |
qed |
|
83 |
||
47937
70375fa2679d
generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents:
47676
diff
changeset
|
84 |
text {* We can export code: *} |
70375fa2679d
generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents:
47676
diff
changeset
|
85 |
|
70375fa2679d
generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents:
47676
diff
changeset
|
86 |
export_code fnil fcons fappend fmap ffilter fset in SML |
70375fa2679d
generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents:
47676
diff
changeset
|
87 |
|
47660 | 88 |
text {* Note that the generated transfer rule contains a composition |
89 |
of relations. The transfer rule is not yet very useful in this form. *} |
|
90 |
||
91 |
lemma "(list_all2 cr_fset OO cr_fset ===> cr_fset) concat fconcat" |
|
92 |
by (fact fconcat.transfer) |
|
93 |
||
94 |
||
95 |
subsection {* Using transfer with type @{text "fset"} *} |
|
96 |
||
97 |
text {* The correspondence relation @{text "cr_fset"} can only relate |
|
98 |
@{text "list"} and @{text "fset"} types with the same element type. |
|
99 |
To relate nested types like @{text "'a list list"} and |
|
100 |
@{text "'a fset fset"}, we define a parameterized version of the |
|
101 |
correspondence relation, @{text "cr_fset'"}. *} |
|
102 |
||
103 |
definition cr_fset' :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'b fset \<Rightarrow> bool" |
|
104 |
where "cr_fset' R = list_all2 R OO cr_fset" |
|
105 |
||
106 |
lemma right_unique_cr_fset' [transfer_rule]: |
|
107 |
"right_unique A \<Longrightarrow> right_unique (cr_fset' A)" |
|
108 |
unfolding cr_fset'_def |
|
109 |
by (intro right_unique_OO right_unique_list_all2 fset.right_unique) |
|
110 |
||
111 |
lemma right_total_cr_fset' [transfer_rule]: |
|
112 |
"right_total A \<Longrightarrow> right_total (cr_fset' A)" |
|
113 |
unfolding cr_fset'_def |
|
114 |
by (intro right_total_OO right_total_list_all2 fset.right_total) |
|
115 |
||
116 |
lemma bi_total_cr_fset' [transfer_rule]: |
|
117 |
"bi_total A \<Longrightarrow> bi_total (cr_fset' A)" |
|
118 |
unfolding cr_fset'_def |
|
119 |
by (intro bi_total_OO bi_total_list_all2 fset.bi_total) |
|
120 |
||
121 |
text {* Transfer rules for @{text "cr_fset'"} can be derived from the |
|
122 |
existing transfer rules for @{text "cr_fset"} together with the |
|
123 |
transfer rules for the polymorphic raw constants. *} |
|
124 |
||
125 |
text {* Note that the proofs below all have a similar structure and |
|
126 |
could potentially be automated. *} |
|
127 |
||
128 |
lemma fnil_transfer [transfer_rule]: |
|
129 |
"(cr_fset' A) [] fnil" |
|
130 |
unfolding cr_fset'_def |
|
131 |
apply (rule relcomppI) |
|
132 |
apply (rule Nil_transfer) |
|
133 |
apply (rule fnil.transfer) |
|
134 |
done |
|
135 |
||
136 |
lemma fcons_transfer [transfer_rule]: |
|
137 |
"(A ===> cr_fset' A ===> cr_fset' A) Cons fcons" |
|
138 |
unfolding cr_fset'_def |
|
139 |
apply (intro fun_relI) |
|
140 |
apply (elim relcomppE) |
|
141 |
apply (rule relcomppI) |
|
142 |
apply (erule (1) Cons_transfer [THEN fun_relD, THEN fun_relD]) |
|
143 |
apply (erule fcons.transfer [THEN fun_relD, THEN fun_relD, OF refl]) |
|
144 |
done |
|
145 |
||
146 |
lemma fappend_transfer [transfer_rule]: |
|
147 |
"(cr_fset' A ===> cr_fset' A ===> cr_fset' A) append fappend" |
|
148 |
unfolding cr_fset'_def |
|
149 |
apply (intro fun_relI) |
|
150 |
apply (elim relcomppE) |
|
151 |
apply (rule relcomppI) |
|
152 |
apply (erule (1) append_transfer [THEN fun_relD, THEN fun_relD]) |
|
153 |
apply (erule (1) fappend.transfer [THEN fun_relD, THEN fun_relD]) |
|
154 |
done |
|
155 |
||
156 |
lemma fmap_transfer [transfer_rule]: |
|
157 |
"((A ===> B) ===> cr_fset' A ===> cr_fset' B) map fmap" |
|
158 |
unfolding cr_fset'_def |
|
159 |
apply (intro fun_relI) |
|
160 |
apply (elim relcomppE) |
|
161 |
apply (rule relcomppI) |
|
162 |
apply (erule (1) map_transfer [THEN fun_relD, THEN fun_relD]) |
|
47676
ec235f564444
adapt to changes in generated transfer rules (cf. 4483c004499a)
huffman
parents:
47660
diff
changeset
|
163 |
apply (erule fmap.transfer [THEN fun_relD, THEN fun_relD, unfolded relator_eq, OF refl]) |
47660 | 164 |
done |
165 |
||
166 |
lemma ffilter_transfer [transfer_rule]: |
|
167 |
"((A ===> op =) ===> cr_fset' A ===> cr_fset' A) filter ffilter" |
|
168 |
unfolding cr_fset'_def |
|
169 |
apply (intro fun_relI) |
|
170 |
apply (elim relcomppE) |
|
171 |
apply (rule relcomppI) |
|
172 |
apply (erule (1) filter_transfer [THEN fun_relD, THEN fun_relD]) |
|
47676
ec235f564444
adapt to changes in generated transfer rules (cf. 4483c004499a)
huffman
parents:
47660
diff
changeset
|
173 |
apply (erule ffilter.transfer [THEN fun_relD, THEN fun_relD, unfolded relator_eq, OF refl]) |
47660 | 174 |
done |
175 |
||
176 |
lemma fset_transfer [transfer_rule]: |
|
177 |
"(cr_fset' A ===> set_rel A) set fset" |
|
178 |
unfolding cr_fset'_def |
|
179 |
apply (intro fun_relI) |
|
180 |
apply (elim relcomppE) |
|
47676
ec235f564444
adapt to changes in generated transfer rules (cf. 4483c004499a)
huffman
parents:
47660
diff
changeset
|
181 |
apply (drule fset.transfer [THEN fun_relD, unfolded relator_eq]) |
47660 | 182 |
apply (erule subst) |
183 |
apply (erule set_transfer [THEN fun_relD]) |
|
184 |
done |
|
185 |
||
186 |
lemma fconcat_transfer [transfer_rule]: |
|
187 |
"(cr_fset' (cr_fset' A) ===> cr_fset' A) concat fconcat" |
|
188 |
unfolding cr_fset'_def |
|
189 |
unfolding list_all2_OO |
|
190 |
apply (intro fun_relI) |
|
191 |
apply (elim relcomppE) |
|
192 |
apply (rule relcomppI) |
|
193 |
apply (erule concat_transfer [THEN fun_relD]) |
|
194 |
apply (rule fconcat.transfer [THEN fun_relD]) |
|
195 |
apply (erule (1) relcomppI) |
|
196 |
done |
|
197 |
||
198 |
lemma list_eq_transfer [transfer_rule]: |
|
199 |
assumes [transfer_rule]: "bi_unique A" |
|
200 |
shows "(list_all2 A ===> list_all2 A ===> op =) list_eq list_eq" |
|
201 |
unfolding list_eq_def [abs_def] by transfer_prover |
|
202 |
||
203 |
lemma fset_eq_transfer [transfer_rule]: |
|
204 |
assumes "bi_unique A" |
|
205 |
shows "(cr_fset' A ===> cr_fset' A ===> op =) list_eq (op =)" |
|
206 |
unfolding cr_fset'_def |
|
207 |
apply (intro fun_relI) |
|
208 |
apply (elim relcomppE) |
|
209 |
apply (rule trans) |
|
210 |
apply (erule (1) list_eq_transfer [THEN fun_relD, THEN fun_relD, OF assms]) |
|
211 |
apply (erule (1) fset.rel_eq_transfer [THEN fun_relD, THEN fun_relD]) |
|
212 |
done |
|
213 |
||
214 |
text {* We don't need the original transfer rules any more: *} |
|
215 |
||
216 |
lemmas [transfer_rule del] = |
|
217 |
fset.bi_total fset.right_total fset.right_unique |
|
218 |
fnil.transfer fcons.transfer fappend.transfer fmap.transfer |
|
219 |
ffilter.transfer fset.transfer fconcat.transfer fset.rel_eq_transfer |
|
220 |
||
221 |
subsection {* Transfer examples *} |
|
222 |
||
223 |
text {* The @{text "transfer"} method replaces equality on @{text |
|
224 |
"fset"} with the @{text "list_eq"} relation on lists, which is |
|
225 |
logically equivalent. *} |
|
226 |
||
227 |
lemma "fmap f (fmap g xs) = fmap (f \<circ> g) xs" |
|
228 |
apply transfer |
|
229 |
apply simp |
|
230 |
done |
|
231 |
||
232 |
text {* The @{text "transfer'"} variant can replace equality on @{text |
|
233 |
"fset"} with equality on @{text "list"}, which is logically stronger |
|
234 |
but sometimes more convenient. *} |
|
235 |
||
236 |
lemma "fmap f (fmap g xs) = fmap (f \<circ> g) xs" |
|
237 |
apply transfer' |
|
238 |
apply (rule map_map) |
|
239 |
done |
|
240 |
||
241 |
lemma "ffilter p (fmap f xs) = fmap f (ffilter (p \<circ> f) xs)" |
|
242 |
apply transfer' |
|
243 |
apply (rule filter_map) |
|
244 |
done |
|
245 |
||
246 |
lemma "ffilter p (ffilter q xs) = ffilter (\<lambda>x. q x \<and> p x) xs" |
|
247 |
apply transfer' |
|
248 |
apply (rule filter_filter) |
|
249 |
done |
|
250 |
||
251 |
lemma "fset (fcons x xs) = insert x (fset xs)" |
|
252 |
apply transfer |
|
253 |
apply (rule set.simps) |
|
254 |
done |
|
255 |
||
256 |
lemma "fset (fappend xs ys) = fset xs \<union> fset ys" |
|
257 |
apply transfer |
|
258 |
apply (rule set_append) |
|
259 |
done |
|
260 |
||
261 |
lemma "fset (fconcat xss) = (\<Union>xs\<in>fset xss. fset xs)" |
|
262 |
apply transfer |
|
263 |
apply (rule set_concat) |
|
264 |
done |
|
265 |
||
266 |
lemma "\<forall>x\<in>fset xs. f x = g x \<Longrightarrow> fmap f xs = fmap g xs" |
|
267 |
apply transfer |
|
268 |
apply (simp cong: map_cong del: set_map) |
|
269 |
done |
|
270 |
||
271 |
lemma "fnil = fconcat xss \<longleftrightarrow> (\<forall>xs\<in>fset xss. xs = fnil)" |
|
272 |
apply transfer |
|
273 |
apply simp |
|
274 |
done |
|
275 |
||
276 |
lemma "fconcat (fmap (\<lambda>x. fcons x fnil) xs) = xs" |
|
277 |
apply transfer' |
|
278 |
apply simp |
|
279 |
done |
|
280 |
||
281 |
lemma concat_map_concat: "concat (map concat xsss) = concat (concat xsss)" |
|
282 |
by (induct xsss, simp_all) |
|
283 |
||
284 |
lemma "fconcat (fmap fconcat xss) = fconcat (fconcat xss)" |
|
285 |
apply transfer' |
|
286 |
apply (rule concat_map_concat) |
|
287 |
done |
|
288 |
||
289 |
end |