Nominal/FSet.thy
changeset 2534 f99578469d08
parent 2533 767161329839
child 2536 98e84b56543f
equal deleted inserted replaced
2533:767161329839 2534:f99578469d08
    46 definition
    46 definition
    47   fcard_raw :: "'a list \<Rightarrow> nat"
    47   fcard_raw :: "'a list \<Rightarrow> nat"
    48 where
    48 where
    49   "fcard_raw xs = card (set xs)"
    49   "fcard_raw xs = card (set xs)"
    50 
    50 
    51 (*
       
    52 definition
       
    53   finter_raw :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list"
       
    54 where
       
    55   "finter_raw xs ys \<equiv> [x \<leftarrow> xs. x\<notin>set ys]"
       
    56 *)
       
    57 
       
    58 primrec
    51 primrec
    59   finter_raw :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list"
    52   finter_raw :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list"
    60 where
    53 where
    61   "finter_raw [] ys = []"
    54   "finter_raw [] ys = []"
    62 | "finter_raw (x # xs) ys =
    55 | "finter_raw (x # xs) ys =
    63     (if x \<in> set ys then x # (finter_raw xs ys) else finter_raw xs ys)"
    56     (if x \<in> set ys then x # (finter_raw xs ys) else finter_raw xs ys)"
    64 
    57 
    65 primrec
    58 
       
    59 definition
    66   fminus_raw :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list"
    60   fminus_raw :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list"
    67 where
    61 where
    68   "fminus_raw ys [] = ys"
    62   "fminus_raw xs ys \<equiv> [x \<leftarrow> xs. x\<notin>set ys]"
    69 | "fminus_raw ys (x # xs) = fminus_raw (removeAll x ys) xs"
    63 
    70 
    64 
    71 definition
    65 definition
    72   rsp_fold
    66   rsp_fold
    73 where
    67 where
    74   "rsp_fold f = (\<forall>u v w. (f u (f v w) = f v (f u w)))"
    68   "rsp_fold f = (\<forall>u v w. (f u (f v w) = f v (f u w)))"
    89   shows "set (finter_raw xs ys) = set xs \<inter> set ys"
    83   shows "set (finter_raw xs ys) = set xs \<inter> set ys"
    90   by (induct xs) (auto simp add: memb_def)
    84   by (induct xs) (auto simp add: memb_def)
    91 
    85 
    92 lemma set_fminus_raw[simp]: 
    86 lemma set_fminus_raw[simp]: 
    93   shows "set (fminus_raw xs ys) = (set xs - set ys)"
    87   shows "set (fminus_raw xs ys) = (set xs - set ys)"
    94   by (induct ys arbitrary: xs) (auto)
    88   by (auto simp add: fminus_raw_def)
    95 
       
    96 
    89 
    97 
    90 
    98 section {* Quotient composition lemmas *}
    91 section {* Quotient composition lemmas *}
    99 
    92 
   100 lemma list_all2_refl1:
    93 lemma list_all2_refl1:
   403     by (descending) (simp add: sub_list_def memb_def[symmetric])
   396     by (descending) (simp add: sub_list_def memb_def[symmetric])
   404 qed
   397 qed
   405 
   398 
   406 end
   399 end
   407 
   400 
   408 section {* Finsert and Membership *}
   401 
       
   402 section {* Definitions for fsets *}
       
   403 
   409 
   404 
   410 quotient_definition
   405 quotient_definition
   411   "finsert :: 'a \<Rightarrow> 'a fset \<Rightarrow> 'a fset"
   406   "finsert :: 'a \<Rightarrow> 'a fset \<Rightarrow> 'a fset"
   412 is "Cons"
   407   is "Cons"
   413 
   408 
   414 syntax
   409 syntax
   415   "@Finset"     :: "args => 'a fset"  ("{|(_)|}")
   410   "@Finset"     :: "args => 'a fset"  ("{|(_)|}")
   416 
   411 
   417 translations
   412 translations
   430 
   425 
   431 section {* Other constants on the Quotient Type *}
   426 section {* Other constants on the Quotient Type *}
   432 
   427 
   433 quotient_definition
   428 quotient_definition
   434   "fcard :: 'a fset \<Rightarrow> nat"
   429   "fcard :: 'a fset \<Rightarrow> nat"
   435 is
   430   is fcard_raw
   436   fcard_raw
       
   437 
   431 
   438 quotient_definition
   432 quotient_definition
   439   "fmap :: ('a \<Rightarrow> 'b) \<Rightarrow> 'a fset \<Rightarrow> 'b fset"
   433   "fmap :: ('a \<Rightarrow> 'b) \<Rightarrow> 'a fset \<Rightarrow> 'b fset"
   440 is
   434   is map
   441   map
       
   442 
   435 
   443 quotient_definition
   436 quotient_definition
   444   "fdelete :: 'a \<Rightarrow> 'a fset \<Rightarrow> 'a fset"
   437   "fdelete :: 'a \<Rightarrow> 'a fset \<Rightarrow> 'a fset"
   445   is removeAll
   438   is removeAll
   446 
   439 
   448   "fset :: 'a fset \<Rightarrow> 'a set"
   441   "fset :: 'a fset \<Rightarrow> 'a set"
   449   is "set"
   442   is "set"
   450 
   443 
   451 quotient_definition
   444 quotient_definition
   452   "ffold :: ('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a fset \<Rightarrow> 'b"
   445   "ffold :: ('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a fset \<Rightarrow> 'b"
   453   is "ffold_raw"
   446   is ffold_raw
   454 
   447 
   455 quotient_definition
   448 quotient_definition
   456   "fconcat :: ('a fset) fset \<Rightarrow> 'a fset"
   449   "fconcat :: ('a fset) fset \<Rightarrow> 'a fset"
   457 is
   450   is concat
   458   "concat"
       
   459 
   451 
   460 quotient_definition
   452 quotient_definition
   461   "ffilter :: ('a \<Rightarrow> bool) \<Rightarrow> 'a fset \<Rightarrow> 'a fset"
   453   "ffilter :: ('a \<Rightarrow> bool) \<Rightarrow> 'a fset \<Rightarrow> 'a fset"
   462 is
   454   is filter
   463   "filter"
   455 
   464 
   456 
   465 text {* Compositional Respectfullness and Preservation *}
   457 subsection {* Compositional Respectfulness and Preservation *}
   466 
   458 
   467 lemma [quot_respect]: "(list_all2 op \<approx> OOO op \<approx>) [] []"
   459 lemma [quot_respect]: "(list_all2 op \<approx> OOO op \<approx>) [] []"
   468   by (fact compose_list_refl)
   460   by (fact compose_list_refl)
   469 
   461 
   470 lemma [quot_preserve]: "(abs_fset \<circ> map f) [] = abs_fset []"
   462 lemma [quot_preserve]: "(abs_fset \<circ> map f) [] = abs_fset []"
   543     by (rule pred_compI) (rule b', rule c')
   535     by (rule pred_compI) (rule b', rule c')
   544   show "(list_all2 op \<approx> OOO op \<approx>) (x @ z) (y @ w)"
   536   show "(list_all2 op \<approx> OOO op \<approx>) (x @ z) (y @ w)"
   545     by (rule pred_compI) (rule a', rule d')
   537     by (rule pred_compI) (rule a', rule d')
   546 qed
   538 qed
   547 
   539 
   548 text {* Raw theorems. Finsert, memb, singleron, sub_list *}
   540 
   549 
   541 
   550 lemma nil_not_cons:
   542 section {* Cases *}
   551   shows "\<not> ([] \<approx> x # xs)"
       
   552   and   "\<not> (x # xs \<approx> [])"
       
   553   by auto
       
   554 
       
   555 lemma no_memb_nil:
       
   556   "(\<forall>x. \<not> memb x xs) = (xs = [])"
       
   557   by (simp add: memb_def)
       
   558 
       
   559 lemma memb_consI1:
       
   560   shows "memb x (x # xs)"
       
   561   by (simp add: memb_def)
       
   562 
       
   563 lemma memb_consI2:
       
   564   shows "memb x xs \<Longrightarrow> memb x (y # xs)"
       
   565   by (simp add: memb_def)
       
   566 
       
   567 lemma singleton_list_eq:
       
   568   shows "[x] \<approx> [y] \<longleftrightarrow> x = y"
       
   569   by (simp)
       
   570 
       
   571 lemma sub_list_cons:
       
   572   "sub_list (x # xs) ys = (memb x ys \<and> sub_list xs ys)"
       
   573   by (auto simp add: memb_def sub_list_def)
       
   574 
       
   575 lemma fminus_raw_red: 
       
   576   "fminus_raw (x # xs) ys = (if x \<in> set ys then fminus_raw xs ys else x # (fminus_raw xs ys))"
       
   577   by (induct ys arbitrary: xs x) (simp_all)
       
   578 
       
   579 text {* Cardinality of finite sets *}
       
   580 
       
   581 (* used in memb_card_not_0 *)
       
   582 lemma fcard_raw_0:
       
   583   shows "fcard_raw xs = 0 \<longleftrightarrow> xs \<approx> []"
       
   584   unfolding fcard_raw_def
       
   585   by (induct xs) (auto)
       
   586 
       
   587 (* used in list_eq2_equiv *)
       
   588 lemma memb_card_not_0:
       
   589   assumes a: "memb a A"
       
   590   shows "\<not>(fcard_raw A = 0)"
       
   591 proof -
       
   592   have "\<not>(\<forall>x. \<not> memb x A)" using a by auto
       
   593   then have "\<not>A \<approx> []" by (simp add: memb_def)
       
   594   then show ?thesis using fcard_raw_0[of A] by simp
       
   595 qed
       
   596 
       
   597 
       
   598 
       
   599 section {* ? *}
       
   600 
   543 
   601 
   544 
   602 lemma fset_raw_strong_cases:
   545 lemma fset_raw_strong_cases:
   603   obtains "xs = []"
   546   obtains "xs = []"
   604     | x ys where "\<not> memb x ys" and "xs \<approx> x # ys"
   547     | x ys where "\<not> memb x ys" and "xs \<approx> x # ys"
   607   then show thesis by simp
   550   then show thesis by simp
   608 next
   551 next
   609   case (Cons a xs)
   552   case (Cons a xs)
   610   have a: "\<lbrakk>xs = [] \<Longrightarrow> thesis; \<And>x ys. \<lbrakk>\<not> memb x ys; xs \<approx> x # ys\<rbrakk> \<Longrightarrow> thesis\<rbrakk> \<Longrightarrow> thesis" by fact
   553   have a: "\<lbrakk>xs = [] \<Longrightarrow> thesis; \<And>x ys. \<lbrakk>\<not> memb x ys; xs \<approx> x # ys\<rbrakk> \<Longrightarrow> thesis\<rbrakk> \<Longrightarrow> thesis" by fact
   611   have b: "\<And>x' ys'. \<lbrakk>\<not> memb x' ys'; a # xs \<approx> x' # ys'\<rbrakk> \<Longrightarrow> thesis" by fact
   554   have b: "\<And>x' ys'. \<lbrakk>\<not> memb x' ys'; a # xs \<approx> x' # ys'\<rbrakk> \<Longrightarrow> thesis" by fact
   612   have c: "xs = [] \<Longrightarrow> thesis" by (metis no_memb_nil singleton_list_eq b)
   555   have c: "xs = [] \<Longrightarrow> thesis" using b unfolding memb_def
       
   556     by (metis in_set_conv_nth less_zeroE list.size(3) list_eq.simps member_set)
   613   have "\<And>x ys. \<lbrakk>\<not> memb x ys; xs \<approx> x # ys\<rbrakk> \<Longrightarrow> thesis"
   557   have "\<And>x ys. \<lbrakk>\<not> memb x ys; xs \<approx> x # ys\<rbrakk> \<Longrightarrow> thesis"
   614   proof -
   558   proof -
   615     fix x :: 'a
   559     fix x :: 'a
   616     fix ys :: "'a list"
   560     fix ys :: "'a list"
   617     assume d:"\<not> memb x ys"
   561     assume d:"\<not> memb x ys"
   630     qed
   574     qed
   631   qed
   575   qed
   632   then show thesis using a c by blast
   576   then show thesis using a c by blast
   633 qed
   577 qed
   634 
   578 
   635 section {* deletion *}
       
   636 
       
   637 
       
   638 lemma fset_raw_removeAll_cases:
       
   639   "xs = [] \<or> (\<exists>x. memb x xs \<and> xs \<approx> x # removeAll x xs)"
       
   640   by (induct xs) (auto simp add: memb_def)
       
   641 
       
   642 lemma fremoveAll_filter:
       
   643   "removeAll y xs = [x \<leftarrow> xs. x \<noteq> y]"
       
   644   by (induct xs) simp_all
       
   645 
       
   646 lemma fcard_raw_delete:
       
   647   "fcard_raw (removeAll y xs) = (if memb y xs then fcard_raw xs - 1 else fcard_raw xs)"
       
   648   by (auto simp add: fcard_raw_def memb_def)
       
   649 
       
   650 lemma inj_map_eq_iff:
       
   651   "inj f \<Longrightarrow> (map f l \<approx> map f m) = (l \<approx> m)"
       
   652   by (simp add: set_eq_iff[symmetric] inj_image_eq_iff)
       
   653 
   579 
   654 text {* alternate formulation with a different decomposition principle
   580 text {* alternate formulation with a different decomposition principle
   655   and a proof of equivalence *}
   581   and a proof of equivalence *}
   656 
   582 
   657 inductive
   583 inductive
   700     have "list_eq2 l r"
   626     have "list_eq2 l r"
   701       using a b
   627       using a b
   702     proof (induct n arbitrary: l r)
   628     proof (induct n arbitrary: l r)
   703       case 0
   629       case 0
   704       have "fcard_raw l = 0" by fact
   630       have "fcard_raw l = 0" by fact
   705       then have "\<forall>x. \<not> memb x l" using memb_card_not_0[of _ l] by auto
   631       then have "\<forall>x. \<not> memb x l" unfolding fcard_raw_def memb_def by auto
   706       then have z: "l = []" using no_memb_nil by auto
   632       then have z: "l = []" unfolding memb_def by auto
   707       then have "r = []" using `l \<approx> r` by simp
   633       then have "r = []" using `l \<approx> r` by simp
   708       then show ?case using z list_eq2_refl by simp
   634       then show ?case using z list_eq2_refl by simp
   709     next
   635     next
   710       case (Suc m)
   636       case (Suc m)
   711       have b: "l \<approx> r" by fact
   637       have b: "l \<approx> r" by fact
   716 	apply(blast)
   642 	apply(blast)
   717 	done
   643 	done
   718       then obtain a where e: "memb a l" by auto
   644       then obtain a where e: "memb a l" by auto
   719       then have e': "memb a r" using list_eq.simps[simplified memb_def[symmetric], of l r] b 
   645       then have e': "memb a r" using list_eq.simps[simplified memb_def[symmetric], of l r] b 
   720 	unfolding memb_def by auto
   646 	unfolding memb_def by auto
   721       have f: "fcard_raw (removeAll a l) = m" using fcard_raw_delete[of a l] e d by simp
   647       have f: "fcard_raw (removeAll a l) = m" using e d by (simp add: fcard_raw_def memb_def)
   722       have g: "removeAll a l \<approx> removeAll a r" using removeAll_rsp b by simp
   648       have g: "removeAll a l \<approx> removeAll a r" using removeAll_rsp b by simp
   723       have "list_eq2 (removeAll a l) (removeAll a r)" by (rule Suc.hyps[OF f g])
   649       have "list_eq2 (removeAll a l) (removeAll a r)" by (rule Suc.hyps[OF f g])
   724       then have h: "list_eq2 (a # removeAll a l) (a # removeAll a r)" by (rule list_eq2.intros(5))
   650       then have h: "list_eq2 (a # removeAll a l) (a # removeAll a r)" by (rule list_eq2.intros(5))
   725       have i: "list_eq2 l (a # removeAll a l)"
   651       have i: "list_eq2 l (a # removeAll a l)"
   726         by (rule list_eq2.intros(3)[OF memb_delete_list_eq2[OF e]])
   652         by (rule list_eq2.intros(3)[OF memb_delete_list_eq2[OF e]])
   729     qed
   655     qed
   730     }
   656     }
   731   then show "l \<approx> r \<Longrightarrow> list_eq2 l r" by blast
   657   then show "l \<approx> r \<Longrightarrow> list_eq2 l r" by blast
   732 qed
   658 qed
   733 
   659 
   734 text {* Lifted theorems *}
   660 
   735 
   661 section {* Lifted theorems *}
   736 lemma not_fin_fnil: "x |\<notin>| {||}"
   662 
       
   663 
       
   664 subsection {* fin *}
       
   665 
       
   666 lemma not_fin_fnil: 
       
   667   shows "x |\<notin>| {||}"
   737   by (descending) (simp add: memb_def)
   668   by (descending) (simp add: memb_def)
   738 
   669 
       
   670 lemma fin_set: 
       
   671   shows "x |\<in>| S \<longleftrightarrow> x \<in> fset S"
       
   672   by (descending) (simp add: memb_def)
       
   673 
       
   674 lemma fnotin_set: 
       
   675   shows "x |\<notin>| S \<longleftrightarrow> x \<notin> fset S"
       
   676   by (descending) (simp add: memb_def)
       
   677 
       
   678 lemma fset_eq_iff:
       
   679   shows "S = T \<longleftrightarrow> (\<forall>x. (x |\<in>| S) = (x |\<in>| T))"
       
   680   by (descending) (auto simp add: memb_def)
       
   681 
       
   682 lemma none_fin_fempty:
       
   683   shows "(\<forall>x. x |\<notin>| S) \<longleftrightarrow> S = {||}"
       
   684   by (descending) (simp add: memb_def)
       
   685 
       
   686 
       
   687 subsection {* finsert *}
       
   688 
   739 lemma fin_finsert_iff[simp]:
   689 lemma fin_finsert_iff[simp]:
   740   "x |\<in>| finsert y S \<longleftrightarrow> x = y \<or> x |\<in>| S"
   690   shows "x |\<in>| finsert y S \<longleftrightarrow> x = y \<or> x |\<in>| S"
   741   by (descending) (simp add: memb_def)
   691   by (descending) (simp add: memb_def)
   742 
   692 
   743 lemma
   693 lemma
   744   shows finsertI1: "x |\<in>| finsert x S"
   694   shows finsertI1: "x |\<in>| finsert x S"
   745   and   finsertI2: "x |\<in>| S \<Longrightarrow> x |\<in>| finsert y S"
   695   and   finsertI2: "x |\<in>| S \<Longrightarrow> x |\<in>| finsert y S"
   746   by (lifting memb_consI1 memb_consI2)
   696   by (descending, simp add: memb_def)+
   747 
   697 
   748 lemma finsert_absorb[simp]:
   698 lemma finsert_absorb[simp]:
   749   shows "x |\<in>| S \<Longrightarrow> finsert x S = S"
   699   shows "x |\<in>| S \<Longrightarrow> finsert x S = S"
   750   by (descending) (auto simp add: memb_def)
   700   by (descending) (auto simp add: memb_def)
   751 
   701 
   752 lemma fempty_not_finsert[simp]:
   702 lemma fempty_not_finsert[simp]:
   753   "{||} \<noteq> finsert x S"
   703   shows "{||} \<noteq> finsert x S"
   754   "finsert x S \<noteq> {||}"
   704   and   "finsert x S \<noteq> {||}"
   755   by (lifting nil_not_cons)
   705   by (descending, simp)+
   756 
   706 
   757 lemma finsert_left_comm:
   707 lemma finsert_left_comm:
   758   "finsert x (finsert y S) = finsert y (finsert x S)"
   708   shows "finsert x (finsert y S) = finsert y (finsert x S)"
   759   by (descending) (auto)
   709   by (descending) (auto)
   760 
   710 
   761 lemma finsert_left_idem:
   711 lemma finsert_left_idem:
   762   "finsert x (finsert x S) = finsert x S"
   712   shows "finsert x (finsert x S) = finsert x S"
   763   by (descending) (auto)
   713   by (descending) (auto)
   764 
   714 
   765 lemma fsingleton_eq[simp]:
   715 lemma fsingleton_eq[simp]:
   766   shows "{|x|} = {|y|} \<longleftrightarrow> x = y"
   716   shows "{|x|} = {|y|} \<longleftrightarrow> x = y"
   767   by (descending) (auto)
   717   by (descending) (auto)
   768 
   718 
   769 
   719 
   770 text {* fset *}
   720 (* FIXME: is this in any case a useful lemma *)
       
   721 lemma fin_mdef:
       
   722   shows "x |\<in>| F \<longleftrightarrow> x |\<notin>| (F - {|x|}) \<and> F = finsert x (F - {|x|})"
       
   723   by (descending) (auto simp add: memb_def fminus_raw_def)
       
   724 
       
   725 
       
   726 subsection {* fset *}
   771 
   727 
   772 lemma fset_simps[simp]:
   728 lemma fset_simps[simp]:
   773   "fset {||} = ({} :: 'a set)"
   729   "fset {||} = ({} :: 'a set)"
   774   "fset (finsert (x :: 'a) S) = insert x (fset S)"
   730   "fset (finsert (x :: 'a) S) = insert x (fset S)"
   775   by (lifting set.simps)
   731   by (lifting set.simps)
   776 
   732 
   777 lemma fin_fset:
   733 lemma finite_fset [simp]: 
   778   "x \<in> fset S \<longleftrightarrow> x |\<in>| S"
   734   shows "finite (fset S)"
   779   by (lifting memb_def[symmetric])
   735   by (descending) (simp)
   780 
   736 
   781 lemma none_fin_fempty:
   737 lemma fset_cong:
   782   "(\<forall>x. x |\<notin>| S) \<longleftrightarrow> S = {||}"
   738   shows "fset S = fset T \<longleftrightarrow> S = T"
       
   739   by (descending) (simp)
       
   740 
       
   741 lemma ffilter_set [simp]: 
       
   742   shows "fset (ffilter P xs) = P \<inter> fset xs"
       
   743   by (descending) (auto simp add: mem_def)
       
   744 
       
   745 lemma fdelete_set [simp]: 
       
   746   shows "fset (fdelete x xs) = fset xs - {x}"
       
   747   by (descending) (simp)
       
   748 
       
   749 lemma finter_set [simp]: 
       
   750   shows "fset (xs |\<inter>| ys) = fset xs \<inter> fset ys"
       
   751   by (lifting set_finter_raw)
       
   752 
       
   753 lemma funion_set [simp]: 
       
   754   shows "fset (xs |\<union>| ys) = fset xs \<union> fset ys"
       
   755   by (lifting set_append)
       
   756 
       
   757 lemma fminus_set [simp]: 
       
   758   shows "fset (xs - ys) = fset xs - fset ys"
       
   759   by (lifting set_fminus_raw)
       
   760 
       
   761 
       
   762 subsection {* funion *}
       
   763 
       
   764 lemmas [simp] =
       
   765   sup_bot_left[where 'a="'a fset", standard]
       
   766   sup_bot_right[where 'a="'a fset", standard]
       
   767 
       
   768 lemma funion_finsert[simp]:
       
   769   shows "finsert x S |\<union>| T = finsert x (S |\<union>| T)"
       
   770   by (lifting append.simps(2))
       
   771 
       
   772 lemma singleton_funion_left:
       
   773   shows "{|a|} |\<union>| S = finsert a S"
       
   774   by simp
       
   775 
       
   776 lemma singleton_funion_right:
       
   777   shows "S |\<union>| {|a|} = finsert a S"
       
   778   by (subst sup.commute) simp
       
   779 
       
   780 lemma fin_funion:
       
   781   shows "x |\<in>| S |\<union>| T \<longleftrightarrow> x |\<in>| S \<or> x |\<in>| T"
   783   by (descending) (simp add: memb_def)
   782   by (descending) (simp add: memb_def)
   784 
   783 
   785 lemma fset_cong:
   784 
   786   "S = T \<longleftrightarrow> fset S = fset T"
   785 subsection {* fminus *}
   787   by (lifting list_eq.simps)
   786 
   788 
   787 lemma fminus_fin: 
   789 
   788   shows "x |\<in>| (xs - ys) \<longleftrightarrow> x |\<in>| xs \<and> x |\<notin>| ys"
   790 section {* fcard *}
   789   by (descending) (simp add: memb_def)
       
   790 
       
   791 lemma fminus_red: 
       
   792   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)
       
   794 
       
   795 lemma fminus_red_fin[simp]: 
       
   796   shows "x |\<in>| ys \<Longrightarrow> finsert x xs - ys = xs - ys"
       
   797   by (simp add: fminus_red)
       
   798 
       
   799 lemma fminus_red_fnotin[simp]: 
       
   800   shows "x |\<notin>| ys \<Longrightarrow> finsert x xs - ys = finsert x (xs - ys)"
       
   801   by (simp add: fminus_red)
       
   802 
       
   803 lemma fin_fminus_fnotin: 
       
   804   shows "x |\<in>| F - S \<Longrightarrow> x |\<notin>| S"
       
   805   unfolding fin_set fminus_set
       
   806   by blast
       
   807 
       
   808 lemma fin_fnotin_fminus: 
       
   809   shows "x |\<in>| S \<Longrightarrow> x |\<notin>| F - S"
       
   810   unfolding fin_set fminus_set
       
   811   by blast
       
   812 
       
   813 
       
   814 section {* fdelete *}
       
   815 
       
   816 lemma fin_fdelete:
       
   817   shows "x |\<in>| fdelete y S \<longleftrightarrow> x |\<in>| S \<and> x \<noteq> y"
       
   818   by (descending) (simp add: memb_def)
       
   819 
       
   820 lemma fnotin_fdelete:
       
   821   shows "x |\<notin>| fdelete x S"
       
   822   by (descending) (simp add: memb_def)
       
   823 
       
   824 lemma fnotin_fdelete_ident:
       
   825   shows "x |\<notin>| S \<Longrightarrow> fdelete x S = S"
       
   826   by (descending) (simp add: memb_def)
       
   827 
       
   828 lemma fset_fdelete_cases:
       
   829   shows "S = {||} \<or> (\<exists>x. x |\<in>| S \<and> S = finsert x (fdelete x S))"
       
   830   by (descending) (auto simp add: memb_def insert_absorb)
       
   831   
       
   832 
       
   833 section {* finter *}
       
   834 
       
   835 lemma finter_empty_l:
       
   836   shows "{||} |\<inter>| S = {||}"
       
   837   by simp
       
   838 
       
   839 lemma finter_empty_r:
       
   840   shows "S |\<inter>| {||} = {||}"
       
   841   by simp
       
   842 
       
   843 lemma finter_finsert:
       
   844   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)
       
   846 
       
   847 lemma fin_finter:
       
   848   shows "x |\<in>| (S |\<inter>| T) \<longleftrightarrow> x |\<in>| S \<and> x |\<in>| T"
       
   849   by (descending) (simp add: memb_def)
       
   850 
       
   851 
       
   852 subsection {* fsubset *}
       
   853 
       
   854 lemma fsubseteq_set: 
       
   855   shows "xs |\<subseteq>| ys \<longleftrightarrow> fset xs \<subseteq> fset ys"
       
   856   by (descending) (simp add: sub_list_def)
       
   857 
       
   858 lemma fsubset_set: 
       
   859   shows "xs |\<subset>| ys \<longleftrightarrow> fset xs \<subset> fset ys"
       
   860   unfolding less_fset_def 
       
   861   by (descending) (auto simp add: sub_list_def)
       
   862 
       
   863 lemma fsubseteq_finsert:
       
   864   shows "(finsert x xs) |\<subseteq>| ys \<longleftrightarrow> x |\<in>| ys \<and> xs |\<subseteq>| ys"
       
   865   by (descending) (simp add: sub_list_def memb_def)
       
   866 
       
   867 lemma fsubset_fin: 
       
   868   shows "xs |\<subseteq>| ys = (\<forall>x. x |\<in>| xs \<longrightarrow> x |\<in>| ys)"
       
   869   by (descending) (auto simp add: sub_list_def memb_def)
       
   870 
       
   871 (* FIXME: no type ord *)
       
   872 (*
       
   873 lemma fsubset_finsert:
       
   874   shows "(finsert x xs) |\<subset>| ys \<longleftrightarrow> x |\<in>| ys \<and> xs |\<subset>| ys"
       
   875   by (descending) (simp add: sub_list_def memb_def)
       
   876 *)
       
   877 
       
   878 lemma fsubseteq_fempty:
       
   879   shows "xs |\<subseteq>| {||} \<longleftrightarrow> xs = {||}"
       
   880   by (descending) (simp add: sub_list_def)
       
   881 
       
   882 (* also problem with ord *)
       
   883 lemma not_fsubset_fnil: 
       
   884   shows "\<not> xs |\<subset>| {||}"
       
   885   by (metis fset_simps(1) fsubset_set not_psubset_empty)
       
   886 
       
   887 
       
   888 section {* fmap *}
       
   889 
       
   890 lemma fmap_simps [simp]:
       
   891    shows "fmap f {||} = {||}"
       
   892   and   "fmap f (finsert x S) = finsert (f x) (fmap f S)"
       
   893   by (descending, simp)+
       
   894 
       
   895 lemma fmap_set_image [simp]:
       
   896   shows "fset (fmap f S) = f ` (fset S)"
       
   897   by (descending) (simp)
       
   898 
       
   899 lemma inj_fmap_eq_iff:
       
   900   shows "inj f \<Longrightarrow> fmap f S = fmap f T \<longleftrightarrow> S = T"
       
   901   by (descending) (metis inj_vimage_image_eq list_eq.simps set_map)
       
   902 
       
   903 lemma fmap_funion: 
       
   904   shows "fmap f (S |\<union>| T) = fmap f S |\<union>| fmap f T"
       
   905   by (descending) (simp)
       
   906 
       
   907 
       
   908 subsection {* fcard *}
       
   909 
       
   910 lemma fcard_set: 
       
   911   shows "fcard xs = card (fset xs)"
       
   912   by (lifting fcard_raw_def)
   791 
   913 
   792 lemma fcard_finsert_if [simp]:
   914 lemma fcard_finsert_if [simp]:
   793   shows "fcard (finsert x S) = (if x |\<in>| S then fcard S else Suc (fcard S))"
   915   shows "fcard (finsert x S) = (if x |\<in>| S then fcard S else Suc (fcard S))"
   794   by (descending) (auto simp add: fcard_raw_def memb_def insert_absorb)
   916   by (descending) (auto simp add: fcard_raw_def memb_def insert_absorb)
   795 
   917 
   814   by (descending) (auto simp add: memb_def fcard_raw_def insert_absorb)
   936   by (descending) (auto simp add: memb_def fcard_raw_def insert_absorb)
   815 
   937 
   816 lemma fcard_suc: 
   938 lemma fcard_suc: 
   817   shows "fcard S = Suc n \<Longrightarrow> \<exists>x T. x |\<notin>| T \<and> S = finsert x T \<and> fcard T = n"
   939   shows "fcard S = Suc n \<Longrightarrow> \<exists>x T. x |\<notin>| T \<and> S = finsert x T \<and> fcard T = n"
   818   apply(descending)
   940   apply(descending)
   819   apply(simp add: fcard_raw_def memb_def)
   941   apply(auto simp add: fcard_raw_def memb_def)
   820   apply(drule card_eq_SucD)
   942   apply(drule card_eq_SucD)
   821   apply(auto)
   943   apply(auto)
   822   apply(rule_tac x="b" in exI)
   944   apply(rule_tac x="b" in exI)
   823   apply(rule_tac x="removeAll b S" in exI)
   945   apply(rule_tac x="removeAll b S" in exI)
   824   apply(auto)
   946   apply(auto)
   825   done
   947   done
   826 
   948 
   827 lemma fcard_delete:
   949 lemma fcard_delete:
   828   "fcard (fdelete y S) = (if y |\<in>| S then fcard S - 1 else fcard S)"
   950   shows "fcard (fdelete y S) = (if y |\<in>| S then fcard S - 1 else fcard S)"
   829   by (lifting fcard_raw_delete)
   951   by (descending) (simp add: fcard_raw_def memb_def)
   830 
   952 
   831 lemma fcard_suc_memb: 
   953 lemma fcard_suc_memb: 
   832   shows "fcard A = Suc n \<Longrightarrow> \<exists>a. a |\<in>| A"
   954   shows "fcard A = Suc n \<Longrightarrow> \<exists>a. a |\<in>| A"
   833   apply(descending)
   955   apply(descending)
   834   apply(simp add: fcard_raw_def memb_def)
   956   apply(simp add: fcard_raw_def memb_def)
   838 
   960 
   839 lemma fin_fcard_not_0: 
   961 lemma fin_fcard_not_0: 
   840   shows "a |\<in>| A \<Longrightarrow> fcard A \<noteq> 0"
   962   shows "a |\<in>| A \<Longrightarrow> fcard A \<noteq> 0"
   841   by (descending) (auto simp add: fcard_raw_def memb_def)
   963   by (descending) (auto simp add: fcard_raw_def memb_def)
   842 
   964 
   843 
   965 lemma fcard_mono: 
   844 section {* funion *}
   966   shows "xs |\<subseteq>| ys \<Longrightarrow> fcard xs \<le> fcard ys"
   845 
   967   unfolding fcard_set fsubseteq_set
   846 lemmas [simp] =
   968   by (simp add: card_mono[OF finite_fset])
   847   sup_bot_left[where 'a="'a fset", standard]
   969 
   848   sup_bot_right[where 'a="'a fset", standard]
   970 lemma fcard_fsubset_eq: 
   849 
   971   shows "xs |\<subseteq>| ys \<Longrightarrow> fcard ys \<le> fcard xs \<Longrightarrow> xs = ys"
   850 lemma funion_finsert[simp]:
   972   unfolding fcard_set fsubseteq_set
   851   shows "finsert x S |\<union>| T = finsert x (S |\<union>| T)"
   973   by (auto dest: card_seteq[OF finite_fset] simp add: fset_cong)
   852   by (lifting append.simps(2))
   974 
   853 
   975 lemma psubset_fcard_mono: 
   854 lemma singleton_union_left:
   976   shows "xs |\<subset>| ys \<Longrightarrow> fcard xs < fcard ys"
   855   shows "{|a|} |\<union>| S = finsert a S"
   977   unfolding fcard_set fsubset_set
   856   by simp
   978   by (rule psubset_card_mono[OF finite_fset])
   857 
   979 
   858 lemma singleton_union_right:
   980 lemma fcard_funion_finter: 
   859   shows "S |\<union>| {|a|} = finsert a S"
   981   shows "fcard xs + fcard ys = fcard (xs |\<union>| ys) + fcard (xs |\<inter>| ys)"
   860   by (subst sup.commute) simp
   982   unfolding fcard_set funion_set finter_set
       
   983   by (rule card_Un_Int[OF finite_fset finite_fset])
       
   984 
       
   985 lemma fcard_funion_disjoint: 
       
   986   shows "xs |\<inter>| ys = {||} \<Longrightarrow> fcard (xs |\<union>| ys) = fcard xs + fcard ys"
       
   987   unfolding fcard_set funion_set 
       
   988   apply (rule card_Un_disjoint[OF finite_fset finite_fset])
       
   989   by (metis finter_set fset_simps(1))
       
   990 
       
   991 lemma fcard_delete1_less: 
       
   992   shows "x |\<in>| xs \<Longrightarrow> fcard (fdelete x xs) < fcard xs"
       
   993   unfolding fcard_set fin_set fdelete_set 
       
   994   by (rule card_Diff1_less[OF finite_fset])
       
   995 
       
   996 lemma fcard_delete2_less: 
       
   997   shows "x |\<in>| xs \<Longrightarrow> y |\<in>| xs \<Longrightarrow> fcard (fdelete y (fdelete x xs)) < fcard xs"
       
   998   unfolding fcard_set fdelete_set fin_set
       
   999   by (rule card_Diff2_less[OF finite_fset])
       
  1000 
       
  1001 lemma fcard_delete1_le: 
       
  1002   shows "fcard (fdelete x xs) \<le> fcard xs"
       
  1003   unfolding fdelete_set fcard_set
       
  1004   by (rule card_Diff1_le[OF finite_fset])
       
  1005 
       
  1006 lemma fcard_psubset: 
       
  1007   shows "ys |\<subseteq>| xs \<Longrightarrow> fcard ys < fcard xs \<Longrightarrow> ys |\<subset>| xs"
       
  1008   unfolding fcard_set fsubseteq_set fsubset_set
       
  1009   by (rule card_psubset[OF finite_fset])
       
  1010 
       
  1011 lemma fcard_fmap_le: 
       
  1012   shows "fcard (fmap f xs) \<le> fcard xs"
       
  1013   unfolding fcard_set fmap_set_image
       
  1014   by (rule card_image_le[OF finite_fset])
       
  1015 
       
  1016 lemma fcard_fminus_finsert[simp]:
       
  1017   assumes "a |\<in>| A" and "a |\<notin>| B"
       
  1018   shows "fcard (A - finsert a B) = fcard (A - B) - 1"
       
  1019   using assms 
       
  1020   unfolding fin_set fcard_set fminus_set
       
  1021   by (simp add: card_Diff_insert[OF finite_fset])
       
  1022 
       
  1023 lemma fcard_fminus_fsubset:
       
  1024   assumes "B |\<subseteq>| A"
       
  1025   shows "fcard (A - B) = fcard A - fcard B"
       
  1026   using assms 
       
  1027   unfolding fsubseteq_set fcard_set fminus_set
       
  1028   by (rule card_Diff_subset[OF finite_fset])
       
  1029 
       
  1030 lemma fcard_fminus_subset_finter:
       
  1031   shows "fcard (A - B) = fcard A - fcard (A |\<inter>| B)"
       
  1032   unfolding finter_set fcard_set fminus_set
       
  1033   by (rule card_Diff_subset_Int) (simp)
       
  1034 
       
  1035 
       
  1036 section {* fconcat *}
       
  1037 
       
  1038 lemma fconcat_fempty:
       
  1039   shows "fconcat {||} = {||}"
       
  1040   by (lifting concat.simps(1))
       
  1041 
       
  1042 lemma fconcat_finsert:
       
  1043   shows "fconcat (finsert x S) = x |\<union>| fconcat S"
       
  1044   by (lifting concat.simps(2))
       
  1045 
       
  1046 lemma fconcat_finter:
       
  1047   shows "fconcat (xs |\<union>| ys) = fconcat xs |\<union>| fconcat ys"
       
  1048   by (lifting concat_append)
       
  1049 
       
  1050 
       
  1051 section {* ffilter *}
       
  1052 
       
  1053 lemma subseteq_filter: 
       
  1054   shows "ffilter P xs <= ffilter Q xs = (\<forall> x. x |\<in>| xs \<longrightarrow> P x \<longrightarrow> Q x)"
       
  1055   by  (descending) (auto simp add: memb_def sub_list_def)
       
  1056 
       
  1057 lemma eq_ffilter: 
       
  1058   shows "(ffilter P xs = ffilter Q xs) = (\<forall>x. x |\<in>| xs \<longrightarrow> P x = Q x)"
       
  1059   by (descending) (auto simp add: memb_def)
       
  1060 
       
  1061 lemma subset_ffilter:
       
  1062   shows "(\<And>x. x |\<in>| xs \<Longrightarrow> P x \<Longrightarrow> Q x) \<Longrightarrow> (x |\<in>| xs & \<not> P x & Q x) \<Longrightarrow> ffilter P xs < ffilter Q xs"
       
  1063   unfolding less_fset_def by (auto simp add: subseteq_filter eq_ffilter)
       
  1064 
       
  1065 
       
  1066 subsection {* ffold *}
       
  1067 
       
  1068 lemma ffold_nil: 
       
  1069   shows "ffold f z {||} = z"
       
  1070   by (descending) (simp)
       
  1071 
       
  1072 lemma ffold_finsert: "ffold f z (finsert a A) =
       
  1073   (if rsp_fold f then if a |\<in>| A then ffold f z A else f a (ffold f z A) else z)"
       
  1074   by (descending) (simp add: memb_def)
       
  1075 
       
  1076 lemma fin_commute_ffold:
       
  1077   "\<lbrakk>rsp_fold f; h |\<in>| b\<rbrakk> \<Longrightarrow> ffold f z b = f h (ffold f z (fdelete h b))"
       
  1078   by (descending) (simp add: memb_def memb_commute_ffold_raw)
       
  1079 
       
  1080 
       
  1081 subsection {* Choice in fsets *}
       
  1082 
       
  1083 lemma fset_choice: 
       
  1084   assumes a: "\<forall>x. x |\<in>| A \<longrightarrow> (\<exists>y. P x y)"
       
  1085   shows "\<exists>f. \<forall>x. x |\<in>| A \<longrightarrow> P x (f x)"
       
  1086   using a
       
  1087   apply(descending)
       
  1088   using finite_set_choice
       
  1089   by (auto simp add: memb_def Ball_def)
       
  1090 
       
  1091 
       
  1092 (* FIXME: is that in any way useful *)  
       
  1093 
   861 
  1094 
   862 
  1095 
   863 section {* Induction and Cases rules for fsets *}
  1096 section {* Induction and Cases rules for fsets *}
   864 
  1097 
   865 lemma fset_strong_cases:
  1098 lemma fset_strong_cases:
   915   then have H1: "Suc (fcard zs) = fcard (finsert x zs)" using fcard_suc by auto
  1148   then have H1: "Suc (fcard zs) = fcard (finsert x zs)" using fcard_suc by auto
   916   then show "P (finsert x zs)" using b h by simp
  1149   then show "P (finsert x zs)" using b h by simp
   917 qed
  1150 qed
   918 
  1151 
   919 
  1152 
   920 section {* fmap *}
       
   921 
       
   922 lemma fmap_simps[simp]:
       
   923   fixes f::"'a \<Rightarrow> 'b"
       
   924   shows "fmap f {||} = {||}"
       
   925   and   "fmap f (finsert x S) = finsert (f x) (fmap f S)"
       
   926   by (lifting map.simps)
       
   927 
       
   928 lemma fmap_set_image:
       
   929   "fset (fmap f S) = f ` (fset S)"
       
   930   by (induct S) simp_all
       
   931 
       
   932 lemma inj_fmap_eq_iff:
       
   933   "inj f \<Longrightarrow> fmap f S = fmap f T \<longleftrightarrow> S = T"
       
   934   by (lifting inj_map_eq_iff)
       
   935 
       
   936 lemma fmap_funion: 
       
   937   shows "fmap f (S |\<union>| T) = fmap f S |\<union>| fmap f T"
       
   938   by (descending) (simp)
       
   939 
       
   940 lemma fin_funion:
       
   941   shows "x |\<in>| S |\<union>| T \<longleftrightarrow> x |\<in>| S \<or> x |\<in>| T"
       
   942   by (descending) (simp add: memb_def)
       
   943 
       
   944 section {* fset *}
       
   945 
       
   946 lemma fin_set: 
       
   947   shows "x |\<in>| xs \<longleftrightarrow> x \<in> fset xs"
       
   948   by (lifting memb_def)
       
   949 
       
   950 lemma fnotin_set: 
       
   951   shows "x |\<notin>| xs \<longleftrightarrow> x \<notin> fset xs"
       
   952   by (simp add: fin_set)
       
   953 
       
   954 lemma fcard_set: 
       
   955   shows "fcard xs = card (fset xs)"
       
   956   by (lifting fcard_raw_def)
       
   957 
       
   958 lemma fsubseteq_set: 
       
   959   shows "xs |\<subseteq>| ys \<longleftrightarrow> fset xs \<subseteq> fset ys"
       
   960   by (lifting sub_list_def)
       
   961 
       
   962 lemma fsubset_set: 
       
   963   shows "xs |\<subset>| ys \<longleftrightarrow> fset xs \<subset> fset ys"
       
   964   unfolding less_fset_def 
       
   965   by (descending) (auto simp add: sub_list_def)
       
   966 
       
   967 lemma ffilter_set [simp]: 
       
   968   shows "fset (ffilter P xs) = P \<inter> fset xs"
       
   969   by (descending) (auto simp add: mem_def)
       
   970 
       
   971 lemma fdelete_set [simp]: 
       
   972   shows "fset (fdelete x xs) = fset xs - {x}"
       
   973   by (lifting set_removeAll)
       
   974 
       
   975 lemma finter_set [simp]: 
       
   976   shows "fset (xs |\<inter>| ys) = fset xs \<inter> fset ys"
       
   977   by (lifting set_finter_raw)
       
   978 
       
   979 lemma funion_set [simp]: 
       
   980   shows "fset (xs |\<union>| ys) = fset xs \<union> fset ys"
       
   981   by (lifting set_append)
       
   982 
       
   983 lemma fminus_set [simp]: 
       
   984   shows "fset (xs - ys) = fset xs - fset ys"
       
   985   by (lifting set_fminus_raw)
       
   986 
       
   987 
       
   988 
       
   989 section {* ffold *}
       
   990 
       
   991 lemma ffold_nil: 
       
   992   shows "ffold f z {||} = z"
       
   993   by (lifting ffold_raw.simps(1)[where 'a="'b" and 'b="'a"])
       
   994 
       
   995 lemma ffold_finsert: "ffold f z (finsert a A) =
       
   996   (if rsp_fold f then if a |\<in>| A then ffold f z A else f a (ffold f z A) else z)"
       
   997   by (descending) (simp add: memb_def)
       
   998 
       
   999 lemma fin_commute_ffold:
       
  1000   "\<lbrakk>rsp_fold f; h |\<in>| b\<rbrakk> \<Longrightarrow> ffold f z b = f h (ffold f z (fdelete h b))"
       
  1001   by (descending) (simp add: memb_def memb_commute_ffold_raw)
       
  1002 
       
  1003 
       
  1004 
       
  1005 section {* fdelete *}
       
  1006 
       
  1007 lemma fin_fdelete:
       
  1008   shows "x |\<in>| fdelete y S \<longleftrightarrow> x |\<in>| S \<and> x \<noteq> y"
       
  1009   by (descending) (simp add: memb_def)
       
  1010 
       
  1011 lemma fnotin_fdelete:
       
  1012   shows "x |\<notin>| fdelete x S"
       
  1013   by (descending) (simp add: memb_def)
       
  1014 
       
  1015 lemma fnotin_fdelete_ident:
       
  1016   shows "x |\<notin>| S \<Longrightarrow> fdelete x S = S"
       
  1017   by (descending) (simp add: memb_def)
       
  1018 
       
  1019 lemma fset_fdelete_cases:
       
  1020   shows "S = {||} \<or> (\<exists>x. x |\<in>| S \<and> S = finsert x (fdelete x S))"
       
  1021   by (lifting fset_raw_removeAll_cases)
       
  1022 
       
  1023 
       
  1024 section {* finter *}
       
  1025 
       
  1026 lemma finter_empty_l:
       
  1027   shows "{||} |\<inter>| S = {||}"
       
  1028   by simp
       
  1029 
       
  1030 lemma finter_empty_r:
       
  1031   shows "S |\<inter>| {||} = {||}"
       
  1032   by simp
       
  1033 
       
  1034 lemma finter_finsert:
       
  1035   shows "finsert x S |\<inter>| T = (if x |\<in>| T then finsert x (S |\<inter>| T) else S |\<inter>| T)"
       
  1036   by (descending) (simp add: memb_def)
       
  1037 
       
  1038 lemma fin_finter:
       
  1039   shows "x |\<in>| (S |\<inter>| T) \<longleftrightarrow> x |\<in>| S \<and> x |\<in>| T"
       
  1040   by (descending) (simp add: memb_def)
       
  1041 
       
  1042 lemma fsubset_finsert:
       
  1043   shows "finsert x xs |\<subseteq>| ys \<longleftrightarrow> x |\<in>| ys \<and> xs |\<subseteq>| ys"
       
  1044   by (lifting sub_list_cons)
       
  1045 
       
  1046 lemma 
       
  1047   shows "xs |\<subseteq>| ys \<equiv> \<forall>x. x |\<in>| xs \<longrightarrow> x |\<in>| ys"
       
  1048   by (descending) (auto simp add: sub_list_def memb_def)
       
  1049 
       
  1050 lemma fsubset_fin: 
       
  1051   shows "xs |\<subseteq>| ys = (\<forall>x. x |\<in>| xs \<longrightarrow> x |\<in>| ys)"
       
  1052   by (descending) (auto simp add: sub_list_def memb_def)
       
  1053 
       
  1054 lemma fminus_fin: 
       
  1055   shows "x |\<in>| xs - ys \<longleftrightarrow> x |\<in>| xs \<and> x |\<notin>| ys"
       
  1056   by (descending) (simp add: memb_def)
       
  1057 
       
  1058 lemma fminus_red: 
       
  1059   shows "finsert x xs - ys = (if x |\<in>| ys then xs - ys else finsert x (xs - ys))"
       
  1060   by (descending) (auto simp add: memb_def)
       
  1061 
       
  1062 lemma fminus_red_fin[simp]: 
       
  1063   shows "x |\<in>| ys \<Longrightarrow> finsert x xs - ys = xs - ys"
       
  1064   by (simp add: fminus_red)
       
  1065 
       
  1066 lemma fminus_red_fnotin[simp]: 
       
  1067   shows "x |\<notin>| ys \<Longrightarrow> finsert x xs - ys = finsert x (xs - ys)"
       
  1068   by (simp add: fminus_red)
       
  1069 
       
  1070 lemma fset_eq_iff:
       
  1071   shows "S = T \<longleftrightarrow> (\<forall>x. (x |\<in>| S) = (x |\<in>| T))"
       
  1072   by (descending) (auto simp add: memb_def)
       
  1073 
       
  1074 (* We cannot write it as "assumes .. shows" since Isabelle changes
  1153 (* We cannot write it as "assumes .. shows" since Isabelle changes
  1075    the quantifiers to schematic variables and reintroduces them in
  1154    the quantifiers to schematic variables and reintroduces them in
  1076    a different order *)
  1155    a different order *)
  1077 lemma fset_eq_cases:
  1156 lemma fset_eq_cases:
  1078  "\<lbrakk>a1 = a2;
  1157  "\<lbrakk>a1 = a2;
  1095   shows "P x1 x2"
  1174   shows "P x1 x2"
  1096   using assms
  1175   using assms
  1097   by (lifting list_eq2.induct[simplified list_eq2_equiv[symmetric]])
  1176   by (lifting list_eq2.induct[simplified list_eq2_equiv[symmetric]])
  1098 
  1177 
  1099 
  1178 
  1100 section {* fconcat *}
       
  1101 
       
  1102 lemma fconcat_empty:
       
  1103   shows "fconcat {||} = {||}"
       
  1104   by (lifting concat.simps(1))
       
  1105 
       
  1106 lemma fconcat_insert:
       
  1107   shows "fconcat (finsert x S) = x |\<union>| fconcat S"
       
  1108   by (lifting concat.simps(2))
       
  1109 
       
  1110 lemma 
       
  1111   shows "fconcat (xs |\<union>| ys) = fconcat xs |\<union>| fconcat ys"
       
  1112   by (lifting concat_append)
       
  1113 
       
  1114 
       
  1115 section {* ffilter *}
       
  1116 
       
  1117 lemma subseteq_filter: 
       
  1118   shows "ffilter P xs <= ffilter Q xs = (\<forall> x. x |\<in>| xs \<longrightarrow> P x \<longrightarrow> Q x)"
       
  1119   by  (descending) (auto simp add: memb_def sub_list_def)
       
  1120 
       
  1121 lemma eq_ffilter: 
       
  1122   shows "(ffilter P xs = ffilter Q xs) = (\<forall>x. x |\<in>| xs \<longrightarrow> P x = Q x)"
       
  1123   by (descending) (auto simp add: memb_def)
       
  1124 
       
  1125 lemma subset_ffilter:
       
  1126   shows "(\<And>x. x |\<in>| xs \<Longrightarrow> P x \<Longrightarrow> Q x) \<Longrightarrow> (x |\<in>| xs & \<not> P x & Q x) \<Longrightarrow> ffilter P xs < ffilter Q xs"
       
  1127   unfolding less_fset_def by (auto simp add: subseteq_filter eq_ffilter)
       
  1128 
       
  1129 
  1179 
  1130 section {* lemmas transferred from Finite_Set theory *}
  1180 section {* lemmas transferred from Finite_Set theory *}
  1131 
  1181 
  1132 text {* finiteness for finite sets holds *}
  1182 text {* finiteness for finite sets holds *}
  1133 lemma finite_fset [simp]: 
  1183 
  1134   shows "finite (fset S)"
  1184 
  1135   by (induct S) auto
       
  1136 
       
  1137 lemma fset_choice: 
       
  1138   shows "\<forall>x. x |\<in>| A \<longrightarrow> (\<exists>y. P x y) \<Longrightarrow> \<exists>f. \<forall>x. x |\<in>| A \<longrightarrow> P x (f x)"
       
  1139   apply(descending)
       
  1140   apply(simp add: memb_def)
       
  1141   apply(rule finite_set_choice[simplified Ball_def])
       
  1142   apply(simp_all)
       
  1143   done
       
  1144 
       
  1145 lemma fsubseteq_fempty:
       
  1146   shows "xs |\<subseteq>| {||} \<longleftrightarrow> xs = {||}"
       
  1147   by (metis finter_empty_r le_iff_inf)
       
  1148 
       
  1149 lemma not_fsubset_fnil: 
       
  1150   shows "\<not> xs |\<subset>| {||}"
       
  1151   by (metis fset_simps(1) fsubset_set not_psubset_empty)
       
  1152   
       
  1153 lemma fcard_mono: 
       
  1154   shows "xs |\<subseteq>| ys \<Longrightarrow> fcard xs \<le> fcard ys"
       
  1155   unfolding fcard_set fsubseteq_set
       
  1156   by (rule card_mono[OF finite_fset])
       
  1157 
       
  1158 lemma fcard_fseteq: 
       
  1159   shows "xs |\<subseteq>| ys \<Longrightarrow> fcard ys \<le> fcard xs \<Longrightarrow> xs = ys"
       
  1160   unfolding fcard_set fsubseteq_set
       
  1161   by (simp add: card_seteq[OF finite_fset] fset_cong)
       
  1162 
       
  1163 lemma psubset_fcard_mono: 
       
  1164   shows "xs |\<subset>| ys \<Longrightarrow> fcard xs < fcard ys"
       
  1165   unfolding fcard_set fsubset_set
       
  1166   by (rule psubset_card_mono[OF finite_fset])
       
  1167 
       
  1168 lemma fcard_funion_finter: 
       
  1169   shows "fcard xs + fcard ys = fcard (xs |\<union>| ys) + fcard (xs |\<inter>| ys)"
       
  1170   unfolding fcard_set funion_set finter_set
       
  1171   by (rule card_Un_Int[OF finite_fset finite_fset])
       
  1172 
       
  1173 lemma fcard_funion_disjoint: 
       
  1174   shows "xs |\<inter>| ys = {||} \<Longrightarrow> fcard (xs |\<union>| ys) = fcard xs + fcard ys"
       
  1175   unfolding fcard_set funion_set 
       
  1176   apply (rule card_Un_disjoint[OF finite_fset finite_fset])
       
  1177   by (metis finter_set fset_simps(1))
       
  1178 
       
  1179 lemma fcard_delete1_less: 
       
  1180   shows "x |\<in>| xs \<Longrightarrow> fcard (fdelete x xs) < fcard xs"
       
  1181   unfolding fcard_set fin_set fdelete_set 
       
  1182   by (rule card_Diff1_less[OF finite_fset])
       
  1183 
       
  1184 lemma fcard_delete2_less: 
       
  1185   shows "x |\<in>| xs \<Longrightarrow> y |\<in>| xs \<Longrightarrow> fcard (fdelete y (fdelete x xs)) < fcard xs"
       
  1186   unfolding fcard_set fdelete_set fin_set
       
  1187   by (rule card_Diff2_less[OF finite_fset])
       
  1188 
       
  1189 lemma fcard_delete1_le: 
       
  1190   shows "fcard (fdelete x xs) \<le> fcard xs"
       
  1191   unfolding fdelete_set fcard_set
       
  1192   by (rule card_Diff1_le[OF finite_fset])
       
  1193 
       
  1194 lemma fcard_psubset: 
       
  1195   shows "ys |\<subseteq>| xs \<Longrightarrow> fcard ys < fcard xs \<Longrightarrow> ys |\<subset>| xs"
       
  1196   unfolding fcard_set fsubseteq_set fsubset_set
       
  1197   by (rule card_psubset[OF finite_fset])
       
  1198 
       
  1199 lemma fcard_fmap_le: 
       
  1200   shows "fcard (fmap f xs) \<le> fcard xs"
       
  1201   unfolding fcard_set  fmap_set_image
       
  1202   by (rule card_image_le[OF finite_fset])
       
  1203 
       
  1204 lemma fin_fminus_fnotin: 
       
  1205   shows "x |\<in>| F - S \<Longrightarrow> x |\<notin>| S"
       
  1206   unfolding fin_set fminus_set
       
  1207   by blast
       
  1208 
       
  1209 lemma fin_fnotin_fminus: 
       
  1210   shows "x |\<in>| S \<Longrightarrow> x |\<notin>| F - S"
       
  1211   unfolding fin_set fminus_set
       
  1212   by blast
       
  1213 
       
  1214 lemma fin_mdef:
       
  1215   shows "x |\<in>| F \<longleftrightarrow> x |\<notin>| (F - {|x|}) \<and> F = finsert x (F - {|x|})"
       
  1216   unfolding fin_set fset_simps fset_cong fminus_set
       
  1217   by blast
       
  1218 
       
  1219 lemma fcard_fminus_finsert[simp]:
       
  1220   assumes "a |\<in>| A" and "a |\<notin>| B"
       
  1221   shows "fcard (A - finsert a B) = fcard (A - B) - 1"
       
  1222   using assms 
       
  1223   unfolding fin_set fcard_set fminus_set
       
  1224   by (simp add: card_Diff_insert[OF finite_fset])
       
  1225 
       
  1226 lemma fcard_fminus_fsubset:
       
  1227   assumes "B |\<subseteq>| A"
       
  1228   shows "fcard (A - B) = fcard A - fcard B"
       
  1229   using assms 
       
  1230   unfolding fsubseteq_set fcard_set fminus_set
       
  1231   by (rule card_Diff_subset[OF finite_fset])
       
  1232 
       
  1233 lemma fcard_fminus_subset_finter:
       
  1234   shows "fcard (A - B) = fcard A - fcard (A |\<inter>| B)"
       
  1235   unfolding finter_set fcard_set fminus_set
       
  1236   by (rule card_Diff_subset_Int) (simp)
       
  1237 
  1185 
  1238 
  1186 
  1239 lemma list_all2_refl:
  1187 lemma list_all2_refl:
  1240   assumes q: "equivp R"
  1188   assumes q: "equivp R"
  1241   shows "(list_all2 R) r r"
  1189   shows "(list_all2 R) r r"