137 |
141 |
138 lemma fcard_raw_suc: |
142 lemma fcard_raw_suc: |
139 fixes xs :: "'a list" |
143 fixes xs :: "'a list" |
140 fixes n :: "nat" |
144 fixes n :: "nat" |
141 assumes c: "fcard_raw xs = Suc n" |
145 assumes c: "fcard_raw xs = Suc n" |
142 shows "\<exists>a ys. ~(memb a ys) \<and> xs \<approx> (a # ys)" |
146 shows "\<exists>a ys. ~(memb a ys) \<and> xs \<approx> (a # ys) \<and> fcard_raw ys = n" |
|
147 unfolding memb_def |
143 using c |
148 using c |
144 apply(induct xs) |
149 proof (induct xs) |
|
150 case Nil |
|
151 then show ?case by simp |
|
152 next |
|
153 case (Cons a xs) |
|
154 have f1: "fcard_raw xs = Suc n \<Longrightarrow> \<exists>a ys. a \<notin> set ys \<and> xs \<approx> a # ys \<and> fcard_raw ys = n" by fact |
|
155 have f2: "fcard_raw (a # xs) = Suc n" by fact |
|
156 then show ?case proof (cases "a \<in> set xs") |
|
157 case True |
|
158 then show ?thesis using f1 f2 apply - |
|
159 apply (simp add: memb_def) |
|
160 apply clarify |
|
161 by metis |
|
162 case False |
|
163 then have ?thesis using f1 f2 apply - |
|
164 apply (rule_tac x="a" in exI) |
|
165 apply (rule_tac x="xs" in exI) |
|
166 apply (simp add: memb_def) |
|
167 done |
|
168 qed |
|
169 qed |
|
170 qed |
|
171 |
|
172 lemma singleton_fcard_1: "set a = {b} \<Longrightarrow> fcard_raw a = Suc 0" |
|
173 apply (induct a) |
|
174 apply simp_all |
|
175 apply auto |
|
176 apply (subgoal_tac "set a2 = {b}") |
145 apply simp |
177 apply simp |
146 apply (auto) |
178 apply (simp add: memb_def) |
147 apply (metis memb_def) |
179 apply auto |
|
180 apply (subgoal_tac "set a2 = {}") |
|
181 apply simp |
|
182 by (metis memb_def subset_empty subset_insert) |
|
183 |
|
184 lemma fcard_raw_1: |
|
185 fixes a :: "'a list" |
|
186 shows "(fcard_raw a = 1) = (\<exists>b. a \<approx> [b])" |
|
187 apply auto |
|
188 apply (drule fcard_raw_suc) |
|
189 apply clarify |
|
190 apply (simp add: fcard_raw_0) |
|
191 apply (rule_tac x="aa" in exI) |
|
192 apply simp |
|
193 apply (subgoal_tac "set a = {b}") |
|
194 apply (erule singleton_fcard_1) |
|
195 apply auto |
148 done |
196 done |
149 |
197 |
150 lemma fcard_raw_delete_one: |
198 lemma fcard_raw_delete_one: |
151 "fcard_raw ([x \<leftarrow> xs. x \<noteq> y]) = (if memb y xs then fcard_raw xs - 1 else fcard_raw xs)" |
199 "fcard_raw ([x \<leftarrow> xs. x \<noteq> y]) = (if memb y xs then fcard_raw xs - 1 else fcard_raw xs)" |
152 by (induct xs) (auto dest: fcard_raw_gt_0 simp add: memb_def) |
200 by (induct xs) (auto dest: fcard_raw_gt_0 simp add: memb_def) |
229 delete_raw :: "'a list \<Rightarrow> 'a \<Rightarrow> 'a list" |
281 delete_raw :: "'a list \<Rightarrow> 'a \<Rightarrow> 'a list" |
230 where |
282 where |
231 "delete_raw [] x = []" |
283 "delete_raw [] x = []" |
232 | "delete_raw (a # A) x = (if (a = x) then delete_raw A x else a # (delete_raw A x))" |
284 | "delete_raw (a # A) x = (if (a = x) then delete_raw A x else a # (delete_raw A x))" |
233 |
285 |
234 lemma mem_delete_raw: |
286 lemma memb_delete_raw: |
235 "x \<in> set (delete_raw A a) = (x \<in> set A \<and> \<not>(x = a))" |
287 "memb x (delete_raw A a) = (memb x A \<and> x \<noteq> a)" |
236 by (induct A arbitrary: x a) (auto) |
288 by (induct A arbitrary: x a) (auto simp add: memb_def) |
237 |
289 |
238 lemma mem_delete_raw_ident: |
290 lemma [quot_respect]: |
239 "\<not>(a \<in> set (delete_raw A a))" |
291 "(op \<approx> ===> op = ===> op \<approx>) delete_raw delete_raw" |
240 by (induct A) (auto) |
292 by (simp add: memb_def[symmetric] memb_delete_raw) |
241 |
293 |
242 lemma not_mem_delete_raw_ident: |
294 lemma memb_delete_raw_ident: |
243 "b \<notin> set A \<Longrightarrow> (delete_raw A b = A)" |
295 "\<not> memb a (delete_raw A a)" |
244 by (induct A) (auto) |
296 by (induct A) (auto simp add: memb_def) |
245 |
297 |
246 lemma finite_set_raw_delete_raw_cases: |
298 lemma not_memb_delete_raw_ident: |
247 "X = [] \<or> (\<exists>a. a mem X \<and> X \<approx> a # delete_raw X a)" |
299 "\<not> memb b A \<Longrightarrow> delete_raw A b = A" |
248 by (induct X) (auto) |
300 by (induct A) (auto simp add: memb_def) |
249 |
301 |
250 lemma list2set_thm: |
302 lemma fset_raw_delete_raw_cases: |
251 shows "set [] = {}" |
303 "X = [] \<or> (\<exists>a. memb a X \<and> X \<approx> a # delete_raw X a)" |
252 and "set (h # t) = insert h (set t)" |
304 by (induct X) (auto simp add: memb_def) |
253 by (auto) |
305 |
254 |
306 lemma fdelete_raw_filter: |
255 lemma list2set_rsp[quot_respect]: |
307 "delete_raw xs y = [x \<leftarrow> xs. x \<noteq> y]" |
|
308 by (induct xs) simp_all |
|
309 |
|
310 lemma fcard_raw_delete: |
|
311 "fcard_raw (delete_raw xs y) = (if memb y xs then fcard_raw xs - 1 else fcard_raw xs)" |
|
312 by (simp add: fdelete_raw_filter fcard_raw_delete_one) |
|
313 |
|
314 lemma set_rsp[quot_respect]: |
256 "(op \<approx> ===> op =) set set" |
315 "(op \<approx> ===> op =) set set" |
257 by auto |
316 by auto |
258 |
317 |
259 definition |
318 definition |
260 rsp_fold |
319 rsp_fold |
261 where |
320 where |
262 "rsp_fold f = (\<forall>u v w. (f u (f v w) = f v (f u w)))" |
321 "rsp_fold f = (\<forall>u v w. (f u (f v w) = f v (f u w)))" |
263 |
322 |
264 primrec |
323 primrec |
265 fold_raw :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a list \<Rightarrow> 'b" |
324 ffold_raw :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a list \<Rightarrow> 'b" |
266 where |
325 where |
267 "fold_raw f z [] = z" |
326 "ffold_raw f z [] = z" |
268 | "fold_raw f z (a # A) = |
327 | "ffold_raw f z (a # A) = |
269 (if (rsp_fold f) then |
328 (if (rsp_fold f) then |
270 if a mem A then fold_raw f z A |
329 if memb a A then ffold_raw f z A |
271 else f a (fold_raw f z A) |
330 else f a (ffold_raw f z A) |
272 else z)" |
331 else z)" |
|
332 |
|
333 lemma memb_commute_ffold_raw: |
|
334 "rsp_fold f \<Longrightarrow> memb h b \<Longrightarrow> ffold_raw f z b = f h (ffold_raw f z (delete_raw b h))" |
|
335 apply (induct b) |
|
336 apply (simp add: not_memb_nil) |
|
337 apply (simp add: ffold_raw.simps) |
|
338 apply (rule conjI) |
|
339 apply (rule_tac [!] impI) |
|
340 apply (rule_tac [!] conjI) |
|
341 apply (rule_tac [!] impI) |
|
342 apply (simp_all add: memb_delete_raw) |
|
343 apply (simp add: memb_cons_iff) |
|
344 apply (simp add: not_memb_delete_raw_ident) |
|
345 apply (simp add: memb_cons_iff rsp_fold_def) |
|
346 done |
|
347 |
|
348 primrec |
|
349 finter_raw :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" |
|
350 where |
|
351 "finter_raw [] l = []" |
|
352 | "finter_raw (h # t) l = |
|
353 (if memb h l then h # (finter_raw t l) else finter_raw t l)" |
|
354 |
|
355 lemma finter_raw_empty: |
|
356 "finter_raw l [] = []" |
|
357 by (induct l) (simp_all add: not_memb_nil) |
|
358 |
|
359 lemma memb_finter_raw: |
|
360 "memb e (finter_raw l r) = (memb e l \<and> memb e r)" |
|
361 apply (induct l) |
|
362 apply (simp add: not_memb_nil) |
|
363 apply (simp add: finter_raw.simps) |
|
364 apply (simp add: memb_cons_iff) |
|
365 apply auto |
|
366 done |
|
367 |
|
368 lemma [quot_respect]: |
|
369 "(op \<approx> ===> op \<approx> ===> op \<approx>) finter_raw finter_raw" |
|
370 by (simp add: memb_def[symmetric] memb_finter_raw) |
273 |
371 |
274 section {* Constants on the Quotient Type *} |
372 section {* Constants on the Quotient Type *} |
275 |
373 |
276 quotient_definition |
374 quotient_definition |
277 "fdelete :: 'a fset \<Rightarrow> 'a \<Rightarrow> 'a fset" |
375 "fdelete :: 'a fset \<Rightarrow> 'a \<Rightarrow> 'a fset" |
278 is "delete_raw" |
376 is "delete_raw" |
279 |
377 |
280 quotient_definition |
378 quotient_definition |
281 "fset_to_set :: 'a fset \<Rightarrow> 'a set" |
379 "fset_to_set :: 'a fset \<Rightarrow> 'a set" |
282 is "set" |
380 is "set" |
|
381 |
|
382 quotient_definition |
|
383 "ffold :: ('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a fset \<Rightarrow> 'b" |
|
384 is "ffold_raw" |
|
385 |
|
386 quotient_definition |
|
387 finter (infix "|\<inter>|" 50) |
|
388 where |
|
389 "finter :: 'a fset \<Rightarrow> 'a fset \<Rightarrow> 'a fset" |
|
390 is "finter_raw" |
283 |
391 |
284 lemma funion_sym_pre: |
392 lemma funion_sym_pre: |
285 "a @ b \<approx> b @ a" |
393 "a @ b \<approx> b @ a" |
286 by auto |
394 by auto |
287 |
395 |
344 by (lifting singleton_list_eq) |
454 by (lifting singleton_list_eq) |
345 |
455 |
346 text {* fset_to_set *} |
456 text {* fset_to_set *} |
347 |
457 |
348 lemma fset_to_set_simps[simp]: |
458 lemma fset_to_set_simps[simp]: |
349 "fset_to_set {||} = {}" |
459 "fset_to_set {||} = ({} :: 'a set)" |
350 "fset_to_set (finsert (h :: 'b) t) = insert h (fset_to_set t)" |
460 "fset_to_set (finsert (h :: 'a) t) = insert h (fset_to_set t)" |
351 by (lifting list2set_thm) |
461 by (lifting set.simps) |
352 |
462 |
353 lemma in_fset_to_set: |
463 lemma in_fset_to_set: |
354 "x \<in> fset_to_set xs \<equiv> x |\<in>| xs" |
464 "x \<in> fset_to_set xs \<equiv> x |\<in>| xs" |
355 by (lifting memb_def[symmetric]) |
465 by (lifting memb_def[symmetric]) |
356 |
466 |
357 lemma none_in_fempty: |
467 lemma none_fin_fempty: |
358 "(\<forall>a. a \<notin> fset_to_set A) = (A = {||})" |
468 "(\<forall>a. a \<notin> fset_to_set A) = (A = {||})" |
359 by (lifting none_mem_nil) |
469 by (lifting none_mem_nil) |
360 |
470 |
361 lemma fset_cong: |
471 lemma fset_cong: |
362 "(fset_to_set x = fset_to_set y) = (x = y)" |
472 "(fset_to_set x = fset_to_set y) = (x = y)" |
373 by (lifting fcard_raw_cons) |
483 by (lifting fcard_raw_cons) |
374 |
484 |
375 lemma fcard_0: "(fcard a = 0) = (a = {||})" |
485 lemma fcard_0: "(fcard a = 0) = (a = {||})" |
376 by (lifting fcard_raw_0) |
486 by (lifting fcard_raw_0) |
377 |
487 |
|
488 lemma fcard_1: "(fcard a = 1) = (\<exists>b. a = {|b|})" |
|
489 by (lifting fcard_raw_1) |
|
490 |
378 lemma fcard_gt_0: "x \<in> fset_to_set xs \<Longrightarrow> 0 < fcard xs" |
491 lemma fcard_gt_0: "x \<in> fset_to_set xs \<Longrightarrow> 0 < fcard xs" |
379 by (lifting fcard_raw_gt_0) |
492 by (lifting fcard_raw_gt_0) |
380 |
493 |
381 lemma fcard_not_memb: "(x |\<notin>| xs) = (fcard (finsert x xs) = Suc (fcard xs))" |
494 lemma fcard_not_fin: "(x |\<notin>| xs) = (fcard (finsert x xs) = Suc (fcard xs))" |
382 by (lifting fcard_raw_not_memb) |
495 by (lifting fcard_raw_not_memb) |
383 |
496 |
384 lemma fcard_suc: "fcard xs = Suc n \<Longrightarrow> \<exists>a ys. a |\<notin>| ys \<and> xs = finsert a ys" |
497 lemma fcard_suc: "fcard xs = Suc n \<Longrightarrow> \<exists>a ys. a |\<notin>| ys \<and> xs = finsert a ys \<and> fcard ys = n" |
385 by (lifting fcard_raw_suc) |
498 by (lifting fcard_raw_suc) |
|
499 |
|
500 lemma fcard_delete: |
|
501 "fcard (fdelete xs y) = (if y |\<in>| xs then fcard xs - 1 else fcard xs)" |
|
502 by (lifting fcard_raw_delete) |
386 |
503 |
387 text {* funion *} |
504 text {* funion *} |
388 |
505 |
389 lemma funion_simps[simp]: |
506 lemma funion_simps[simp]: |
390 "{||} |\<union>| ys = ys" |
507 "{||} |\<union>| ys = ys" |
465 by (lifting inj_map_eq_iff) |
583 by (lifting inj_map_eq_iff) |
466 |
584 |
467 lemma fmap_funion: "fmap f (a |\<union>| b) = fmap f a |\<union>| fmap f b" |
585 lemma fmap_funion: "fmap f (a |\<union>| b) = fmap f a |\<union>| fmap f b" |
468 by (lifting map_append) |
586 by (lifting map_append) |
469 |
587 |
|
588 lemma fin_funion: |
|
589 "(e |\<in>| l |\<union>| r) = (e |\<in>| l \<or> e |\<in>| r)" |
|
590 by (lifting memb_append) |
|
591 |
|
592 text {* ffold *} |
|
593 |
|
594 lemma ffold_nil: "ffold f z {||} = z" |
|
595 by (lifting ffold_raw.simps(1)[where 'a="'b" and 'b="'a"]) |
|
596 |
|
597 lemma ffold_finsert: "ffold f z (finsert a A) = |
|
598 (if rsp_fold f then if a |\<in>| A then ffold f z A else f a (ffold f z A) else z)" |
|
599 by (lifting ffold_raw.simps(2)[where 'a="'b" and 'b="'a"]) |
|
600 |
|
601 lemma fin_commute_ffold: |
|
602 "\<lbrakk>rsp_fold f; h |\<in>| b\<rbrakk> \<Longrightarrow> ffold f z b = f h (ffold f z (fdelete b h))" |
|
603 by (lifting memb_commute_ffold_raw) |
|
604 |
|
605 text {* fdelete *} |
|
606 |
|
607 lemma fin_fdelete: "(x |\<in>| fdelete A a) = (x |\<in>| A \<and> x \<noteq> a)" |
|
608 by (lifting memb_delete_raw) |
|
609 |
|
610 lemma fin_fdelete_ident: "a |\<notin>| fdelete A a" |
|
611 by (lifting memb_delete_raw_ident) |
|
612 |
|
613 lemma not_memb_fdelete_ident: "b |\<notin>| A \<Longrightarrow> fdelete A b = A" |
|
614 by (lifting not_memb_delete_raw_ident) |
|
615 |
|
616 lemma fset_fdelete_cases: |
|
617 "X = {||} \<or> (\<exists>a. a |\<in>| X \<and> X = finsert a (fdelete X a))" |
|
618 by (lifting fset_raw_delete_raw_cases) |
|
619 |
|
620 text {* inter *} |
|
621 |
|
622 lemma finter_empty_l: "({||} |\<inter>| r) = {||}" |
|
623 by (lifting finter_raw.simps(1)) |
|
624 |
|
625 lemma finter_empty_r: "(l |\<inter>| {||}) = {||}" |
|
626 by (lifting finter_raw_empty) |
|
627 |
|
628 lemma finter_finsert: |
|
629 "(finsert h t |\<inter>| l) = (if h |\<in>| l then finsert h (t |\<inter>| l) else t |\<inter>| l)" |
|
630 by (lifting finter_raw.simps(2)) |
|
631 |
|
632 lemma fin_finter: |
|
633 "(e |\<in>| (l |\<inter>| r)) = (e |\<in>| l \<and> e |\<in>| r)" |
|
634 by (lifting memb_finter_raw) |
|
635 |
470 ML {* |
636 ML {* |
471 fun dest_fsetT (Type ("FSet.fset", [T])) = T |
637 fun dest_fsetT (Type ("FSet.fset", [T])) = T |
472 | dest_fsetT T = raise TYPE ("dest_fsetT: fset type expected", [T], []); |
638 | dest_fsetT T = raise TYPE ("dest_fsetT: fset type expected", [T], []); |
473 *} |
639 *} |
474 |
640 |