Nominal/FSet.thy
changeset 2546 3a7155fce1da
parent 2544 3b24b5d2b68c
child 2547 17b369a73f15
equal deleted inserted replaced
2545:9746421224a3 2546:3a7155fce1da
     7 
     7 
     8 theory FSet
     8 theory FSet
     9 imports Quotient_List
     9 imports Quotient_List
    10 begin
    10 begin
    11 
    11 
    12 text {* Definition of the equivalence relation over lists *}
    12 text {* 
       
    13   The type of finite sets is created by a quotient construction
       
    14   over lists. The definition of the equivalence:
       
    15 *}
    13 
    16 
    14 fun
    17 fun
    15   list_eq :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool" (infix "\<approx>" 50)
    18   list_eq :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool" (infix "\<approx>" 50)
    16 where
    19 where
    17   "list_eq xs ys = (set xs = set ys)"
    20   "list_eq xs ys \<longleftrightarrow> set xs = set ys"
    18 
    21 
    19 lemma list_eq_equivp:
    22 lemma list_eq_equivp:
    20   shows "equivp list_eq"
    23   shows "equivp list_eq"
    21   unfolding equivp_reflp_symp_transp
    24   unfolding equivp_reflp_symp_transp
    22   unfolding reflp_def symp_def transp_def
    25   unfolding reflp_def symp_def transp_def
    32   Definitions for membership, sublist, cardinality, 
    35   Definitions for membership, sublist, cardinality, 
    33   intersection, difference and respectful fold over 
    36   intersection, difference and respectful fold over 
    34   lists.
    37   lists.
    35 *}
    38 *}
    36 
    39 
    37 definition
    40 fun
    38   "memb x xs \<equiv> x \<in> set xs"
    41   memb :: "'a \<Rightarrow> 'a list \<Rightarrow> bool"
    39 
    42 where
    40 definition
    43   "memb x xs \<longleftrightarrow> x \<in> set xs"
    41   "sub_list xs ys \<equiv> set xs \<subseteq> set ys"
    44 
    42 
    45 fun
    43 definition
    46   sub_list :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool"
       
    47 where 
       
    48   "sub_list xs ys \<longleftrightarrow> set xs \<subseteq> set ys"
       
    49 
       
    50 fun
       
    51   card_list :: "'a list \<Rightarrow> nat"
       
    52 where
    44   "card_list xs = card (set xs)"
    53   "card_list xs = card (set xs)"
    45 
    54 
    46 definition
    55 fun
       
    56   inter_list :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list"
       
    57 where
    47   "inter_list xs ys = [x \<leftarrow> xs. x \<in> set xs \<and> x \<in> set ys]"
    58   "inter_list xs ys = [x \<leftarrow> xs. x \<in> set xs \<and> x \<in> set ys]"
    48 
    59 
    49 definition
    60 fun
    50   "diff_list xs ys \<equiv> [x \<leftarrow> xs. x \<notin> set ys]"
    61   diff_list :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list"
       
    62 where
       
    63   "diff_list xs ys = [x \<leftarrow> xs. x \<notin> set ys]"
    51 
    64 
    52 definition
    65 definition
    53   rsp_fold
    66   rsp_fold
    54 where
    67 where
    55   "rsp_fold f \<equiv> \<forall>u v w. (f u (f v w) = f v (f u w))"
    68   "rsp_fold f \<equiv> \<forall>u v w. (f u (f v w) = f v (f u w))"
   158   shows "(op \<approx> ===> op \<approx> ===> op \<approx>) append append"
   171   shows "(op \<approx> ===> op \<approx> ===> op \<approx>) append append"
   159   by simp
   172   by simp
   160 
   173 
   161 lemma sub_list_rsp [quot_respect]:
   174 lemma sub_list_rsp [quot_respect]:
   162   shows "(op \<approx> ===> op \<approx> ===> op =) sub_list sub_list"
   175   shows "(op \<approx> ===> op \<approx> ===> op =) sub_list sub_list"
   163   by (auto simp add: sub_list_def)
   176   by simp
   164 
   177 
   165 lemma memb_rsp [quot_respect]:
   178 lemma memb_rsp [quot_respect]:
   166   shows "(op = ===> op \<approx> ===> op =) memb memb"
   179   shows "(op = ===> op \<approx> ===> op =) memb memb"
   167   by (auto simp add: memb_def)
   180   by simp
   168 
   181 
   169 lemma nil_rsp [quot_respect]:
   182 lemma nil_rsp [quot_respect]:
   170   shows "(op \<approx>) Nil Nil"
   183   shows "(op \<approx>) Nil Nil"
   171   by simp
   184   by simp
   172 
   185 
   182   "(op \<approx> ===> op =) set set"
   195   "(op \<approx> ===> op =) set set"
   183   by auto
   196   by auto
   184 
   197 
   185 lemma inter_list_rsp [quot_respect]:
   198 lemma inter_list_rsp [quot_respect]:
   186   shows "(op \<approx> ===> op \<approx> ===> op \<approx>) inter_list inter_list"
   199   shows "(op \<approx> ===> op \<approx> ===> op \<approx>) inter_list inter_list"
   187   by (simp add: inter_list_def)
   200   by simp
   188 
   201 
   189 lemma removeAll_rsp [quot_respect]:
   202 lemma removeAll_rsp [quot_respect]:
   190   shows "(op = ===> op \<approx> ===> op \<approx>) removeAll removeAll"
   203   shows "(op = ===> op \<approx> ===> op \<approx>) removeAll removeAll"
   191   by simp
   204   by simp
   192 
   205 
   193 lemma diff_list_rsp [quot_respect]:
   206 lemma diff_list_rsp [quot_respect]:
   194   shows "(op \<approx> ===> op \<approx> ===> op \<approx>) diff_list diff_list"
   207   shows "(op \<approx> ===> op \<approx> ===> op \<approx>) diff_list diff_list"
   195   by (simp add: diff_list_def)
   208   by simp
   196 
   209 
   197 lemma card_list_rsp [quot_respect]:
   210 lemma card_list_rsp [quot_respect]:
   198   shows "(op \<approx> ===> op =) card_list card_list"
   211   shows "(op \<approx> ===> op =) card_list card_list"
   199   by (simp add: card_list_def)
   212   by simp
   200 
   213 
   201 lemma filter_rsp [quot_respect]:
   214 lemma filter_rsp [quot_respect]:
   202   shows "(op = ===> op \<approx> ===> op \<approx>) filter filter"
   215   shows "(op = ===> op \<approx> ===> op \<approx>) filter filter"
   203   by simp
   216   by simp
   204 
   217 
   337 instance
   350 instance
   338 proof
   351 proof
   339   fix x y z :: "'a fset"
   352   fix x y z :: "'a fset"
   340   show "x |\<subset>| y \<longleftrightarrow> x |\<subseteq>| y \<and> \<not> y |\<subseteq>| x"
   353   show "x |\<subset>| y \<longleftrightarrow> x |\<subseteq>| y \<and> \<not> y |\<subseteq>| x"
   341     unfolding less_fset_def 
   354     unfolding less_fset_def 
   342     by (descending) (auto simp add: sub_list_def)
   355     by (descending) (auto)
   343   show "x |\<subseteq>| x"  by (descending) (simp add: sub_list_def)
   356   show "x |\<subseteq>| x"  by (descending) (simp)
   344   show "{||} |\<subseteq>| x" by (descending) (simp add: sub_list_def)
   357   show "{||} |\<subseteq>| x" by (descending) (simp)
   345   show "x |\<subseteq>| x |\<union>| y" by (descending) (simp add: sub_list_def)
   358   show "x |\<subseteq>| x |\<union>| y" by (descending) (simp)
   346   show "y |\<subseteq>| x |\<union>| y" by (descending) (simp add: sub_list_def)
   359   show "y |\<subseteq>| x |\<union>| y" by (descending) (simp)
   347   show "x |\<inter>| y |\<subseteq>| x"
   360   show "x |\<inter>| y |\<subseteq>| x" by (descending) (auto)
   348     by (descending) (auto simp add: inter_list_def sub_list_def memb_def)
   361   show "x |\<inter>| y |\<subseteq>| y" by (descending) (auto)
   349   show "x |\<inter>| y |\<subseteq>| y" 
       
   350     by (descending) (auto simp add: inter_list_def sub_list_def memb_def)
       
   351   show "x |\<union>| (y |\<inter>| z) = x |\<union>| y |\<inter>| (x |\<union>| z)" 
   362   show "x |\<union>| (y |\<inter>| z) = x |\<union>| y |\<inter>| (x |\<union>| z)" 
   352     by (descending) (auto simp add: inter_list_def)
   363     by (descending) (auto)
   353 next
   364 next
   354   fix x y z :: "'a fset"
   365   fix x y z :: "'a fset"
   355   assume a: "x |\<subseteq>| y"
   366   assume a: "x |\<subseteq>| y"
   356   assume b: "y |\<subseteq>| z"
   367   assume b: "y |\<subseteq>| z"
   357   show "x |\<subseteq>| z" using a b 
   368   show "x |\<subseteq>| z" using a b by (descending) (simp)
   358     by (descending) (simp add: sub_list_def)
       
   359 next
   369 next
   360   fix x y :: "'a fset"
   370   fix x y :: "'a fset"
   361   assume a: "x |\<subseteq>| y"
   371   assume a: "x |\<subseteq>| y"
   362   assume b: "y |\<subseteq>| x"
   372   assume b: "y |\<subseteq>| x"
   363   show "x = y" using a b 
   373   show "x = y" using a b by (descending) (auto)
   364     by (descending) (unfold sub_list_def list_eq.simps, blast)
       
   365 next
   374 next
   366   fix x y z :: "'a fset"
   375   fix x y z :: "'a fset"
   367   assume a: "y |\<subseteq>| x"
   376   assume a: "y |\<subseteq>| x"
   368   assume b: "z |\<subseteq>| x"
   377   assume b: "z |\<subseteq>| x"
   369   show "y |\<union>| z |\<subseteq>| x" using a b 
   378   show "y |\<union>| z |\<subseteq>| x" using a b by (descending) (simp)
   370     by (descending) (simp add: sub_list_def)
       
   371 next
   379 next
   372   fix x y z :: "'a fset"
   380   fix x y z :: "'a fset"
   373   assume a: "x |\<subseteq>| y"
   381   assume a: "x |\<subseteq>| y"
   374   assume b: "x |\<subseteq>| z"
   382   assume b: "x |\<subseteq>| z"
   375   show "x |\<subseteq>| y |\<inter>| z" using a b 
   383   show "x |\<subseteq>| y |\<inter>| z" using a b by (descending) (auto)
   376     by (descending) (auto simp add: inter_list_def sub_list_def memb_def)
       
   377 qed
   384 qed
   378 
   385 
   379 end
   386 end
   380 
   387 
   381 
   388 
   547   shows "fset (remove_fset x xs) = fset xs - {x}"
   554   shows "fset (remove_fset x xs) = fset xs - {x}"
   548   by (descending) (simp)
   555   by (descending) (simp)
   549 
   556 
   550 lemma inter_fset [simp]: 
   557 lemma inter_fset [simp]: 
   551   shows "fset (xs |\<inter>| ys) = fset xs \<inter> fset ys"
   558   shows "fset (xs |\<inter>| ys) = fset xs \<inter> fset ys"
   552   by (descending) (auto simp add: inter_list_def)
   559   by (descending) (auto)
   553 
   560 
   554 lemma union_fset [simp]: 
   561 lemma union_fset [simp]: 
   555   shows "fset (xs |\<union>| ys) = fset xs \<union> fset ys"
   562   shows "fset (xs |\<union>| ys) = fset xs \<union> fset ys"
   556   by (lifting set_append)
   563   by (lifting set_append)
   557 
   564 
   558 lemma minus_fset [simp]: 
   565 lemma minus_fset [simp]: 
   559   shows "fset (xs - ys) = fset xs - fset ys"
   566   shows "fset (xs - ys) = fset xs - fset ys"
   560   by (descending) (auto simp add: diff_list_def)
   567   by (descending) (auto)
   561 
   568 
   562 
   569 
   563 subsection {* in_fset *}
   570 subsection {* in_fset *}
   564 
   571 
   565 lemma in_fset: 
   572 lemma in_fset: 
   566   shows "x |\<in>| S \<longleftrightarrow> x \<in> fset S"
   573   shows "x |\<in>| S \<longleftrightarrow> x \<in> fset S"
   567   by (descending) (simp add: memb_def)
   574   by (descending) (simp)
   568 
   575 
   569 lemma notin_fset: 
   576 lemma notin_fset: 
   570   shows "x |\<notin>| S \<longleftrightarrow> x \<notin> fset S"
   577   shows "x |\<notin>| S \<longleftrightarrow> x \<notin> fset S"
   571   by (simp add: in_fset)
   578   by (simp add: in_fset)
   572 
   579 
   574   shows "x |\<notin>| {||}"
   581   shows "x |\<notin>| {||}"
   575   by (simp add: in_fset)
   582   by (simp add: in_fset)
   576 
   583 
   577 lemma fset_eq_iff:
   584 lemma fset_eq_iff:
   578   shows "S = T \<longleftrightarrow> (\<forall>x. (x |\<in>| S) = (x |\<in>| T))"
   585   shows "S = T \<longleftrightarrow> (\<forall>x. (x |\<in>| S) = (x |\<in>| T))"
   579   by (descending) (auto simp add: memb_def)
   586   by (descending) (auto)
   580 
   587 
   581 lemma none_in_empty_fset:
   588 lemma none_in_empty_fset:
   582   shows "(\<forall>x. x |\<notin>| S) \<longleftrightarrow> S = {||}"
   589   shows "(\<forall>x. x |\<notin>| S) \<longleftrightarrow> S = {||}"
   583   by (descending) (simp add: memb_def)
   590   by (descending) (simp)
   584 
   591 
   585 
   592 
   586 subsection {* insert_fset *}
   593 subsection {* insert_fset *}
   587 
   594 
   588 lemma in_insert_fset_iff [simp]:
   595 lemma in_insert_fset_iff [simp]:
   589   shows "x |\<in>| insert_fset y S \<longleftrightarrow> x = y \<or> x |\<in>| S"
   596   shows "x |\<in>| insert_fset y S \<longleftrightarrow> x = y \<or> x |\<in>| S"
   590   by (descending) (simp add: memb_def)
   597   by (descending) (simp)
   591 
   598 
   592 lemma
   599 lemma
   593   shows insert_fsetI1: "x |\<in>| insert_fset x S"
   600   shows insert_fsetI1: "x |\<in>| insert_fset x S"
   594   and   insert_fsetI2: "x |\<in>| S \<Longrightarrow> x |\<in>| insert_fset y S"
   601   and   insert_fsetI2: "x |\<in>| S \<Longrightarrow> x |\<in>| insert_fset y S"
   595   by simp_all
   602   by simp_all
   596 
   603 
   597 lemma insert_absorb_fset [simp]:
   604 lemma insert_absorb_fset [simp]:
   598   shows "x |\<in>| S \<Longrightarrow> insert_fset x S = S"
   605   shows "x |\<in>| S \<Longrightarrow> insert_fset x S = S"
   599   by (descending) (auto simp add: memb_def)
   606   by (descending) (auto)
   600 
   607 
   601 lemma empty_not_insert_fset[simp]:
   608 lemma empty_not_insert_fset[simp]:
   602   shows "{||} \<noteq> insert_fset x S"
   609   shows "{||} \<noteq> insert_fset x S"
   603   and   "insert_fset x S \<noteq> {||}"
   610   and   "insert_fset x S \<noteq> {||}"
   604   by (descending, simp)+
   611   by (descending, simp)+
   617 
   624 
   618 
   625 
   619 (* FIXME: is this a useful lemma ? *)
   626 (* FIXME: is this a useful lemma ? *)
   620 lemma in_fset_mdef:
   627 lemma in_fset_mdef:
   621   shows "x |\<in>| F \<longleftrightarrow> x |\<notin>| (F - {|x|}) \<and> F = insert_fset x (F - {|x|})"
   628   shows "x |\<in>| F \<longleftrightarrow> x |\<notin>| (F - {|x|}) \<and> F = insert_fset x (F - {|x|})"
   622   by (descending) (auto simp add: memb_def diff_list_def)
   629   by (descending) (auto)
   623 
   630 
   624 
   631 
   625 subsection {* union_fset *}
   632 subsection {* union_fset *}
   626 
   633 
   627 lemmas [simp] =
   634 lemmas [simp] =
   640   shows "S |\<union>| {|a|} = insert_fset a S"
   647   shows "S |\<union>| {|a|} = insert_fset a S"
   641   by (subst sup.commute) simp
   648   by (subst sup.commute) simp
   642 
   649 
   643 lemma in_union_fset:
   650 lemma in_union_fset:
   644   shows "x |\<in>| S |\<union>| T \<longleftrightarrow> x |\<in>| S \<or> x |\<in>| T"
   651   shows "x |\<in>| S |\<union>| T \<longleftrightarrow> x |\<in>| S \<or> x |\<in>| T"
   645   by (descending) (simp add: memb_def)
   652   by (descending) (simp)
   646 
   653 
   647 
   654 
   648 subsection {* minus_fset *}
   655 subsection {* minus_fset *}
   649 
   656 
   650 lemma minus_in_fset: 
   657 lemma minus_in_fset: 
   651   shows "x |\<in>| (xs - ys) \<longleftrightarrow> x |\<in>| xs \<and> x |\<notin>| ys"
   658   shows "x |\<in>| (xs - ys) \<longleftrightarrow> x |\<in>| xs \<and> x |\<notin>| ys"
   652   by (descending) (simp add: diff_list_def memb_def)
   659   by (descending) (simp)
   653 
   660 
   654 lemma minus_insert_fset: 
   661 lemma minus_insert_fset: 
   655   shows "insert_fset x xs - ys = (if x |\<in>| ys then xs - ys else insert_fset x (xs - ys))"
   662   shows "insert_fset x xs - ys = (if x |\<in>| ys then xs - ys else insert_fset x (xs - ys))"
   656   by (descending) (auto simp add: diff_list_def memb_def)
   663   by (descending) (auto)
   657 
   664 
   658 lemma minus_insert_in_fset[simp]: 
   665 lemma minus_insert_in_fset[simp]: 
   659   shows "x |\<in>| ys \<Longrightarrow> insert_fset x xs - ys = xs - ys"
   666   shows "x |\<in>| ys \<Longrightarrow> insert_fset x xs - ys = xs - ys"
   660   by (simp add: minus_insert_fset)
   667   by (simp add: minus_insert_fset)
   661 
   668 
   676 
   683 
   677 subsection {* remove_fset *}
   684 subsection {* remove_fset *}
   678 
   685 
   679 lemma in_remove_fset:
   686 lemma in_remove_fset:
   680   shows "x |\<in>| remove_fset y S \<longleftrightarrow> x |\<in>| S \<and> x \<noteq> y"
   687   shows "x |\<in>| remove_fset y S \<longleftrightarrow> x |\<in>| S \<and> x \<noteq> y"
   681   by (descending) (simp add: memb_def)
   688   by (descending) (simp)
   682 
   689 
   683 lemma notin_remove_fset:
   690 lemma notin_remove_fset:
   684   shows "x |\<notin>| remove_fset x S"
   691   shows "x |\<notin>| remove_fset x S"
   685   by (descending) (simp add: memb_def)
   692   by (descending) (simp)
   686 
   693 
   687 lemma notin_remove_ident_fset:
   694 lemma notin_remove_ident_fset:
   688   shows "x |\<notin>| S \<Longrightarrow> remove_fset x S = S"
   695   shows "x |\<notin>| S \<Longrightarrow> remove_fset x S = S"
   689   by (descending) (simp add: memb_def)
   696   by (descending) (simp)
   690 
   697 
   691 lemma remove_fset_cases:
   698 lemma remove_fset_cases:
   692   shows "S = {||} \<or> (\<exists>x. x |\<in>| S \<and> S = insert_fset x (remove_fset x S))"
   699   shows "S = {||} \<or> (\<exists>x. x |\<in>| S \<and> S = insert_fset x (remove_fset x S))"
   693   by (descending) (auto simp add: memb_def insert_absorb)
   700   by (descending) (auto simp add: insert_absorb)
   694   
   701   
   695 
   702 
   696 subsection {* inter_fset *}
   703 subsection {* inter_fset *}
   697 
   704 
   698 lemma inter_empty_fset_l:
   705 lemma inter_empty_fset_l:
   703   shows "S |\<inter>| {||} = {||}"
   710   shows "S |\<inter>| {||} = {||}"
   704   by simp
   711   by simp
   705 
   712 
   706 lemma inter_insert_fset:
   713 lemma inter_insert_fset:
   707   shows "insert_fset x S |\<inter>| T = (if x |\<in>| T then insert_fset x (S |\<inter>| T) else S |\<inter>| T)"
   714   shows "insert_fset x S |\<inter>| T = (if x |\<in>| T then insert_fset x (S |\<inter>| T) else S |\<inter>| T)"
   708   by (descending) (auto simp add: inter_list_def memb_def)
   715   by (descending) (auto)
   709 
   716 
   710 lemma in_inter_fset:
   717 lemma in_inter_fset:
   711   shows "x |\<in>| (S |\<inter>| T) \<longleftrightarrow> x |\<in>| S \<and> x |\<in>| T"
   718   shows "x |\<in>| (S |\<inter>| T) \<longleftrightarrow> x |\<in>| S \<and> x |\<in>| T"
   712   by (descending) (simp add: inter_list_def memb_def)
   719   by (descending) (simp)
   713 
   720 
   714 
   721 
   715 subsection {* subset_fset and psubset_fset *}
   722 subsection {* subset_fset and psubset_fset *}
   716 
   723 
   717 lemma subset_fset: 
   724 lemma subset_fset: 
   718   shows "xs |\<subseteq>| ys \<longleftrightarrow> fset xs \<subseteq> fset ys"
   725   shows "xs |\<subseteq>| ys \<longleftrightarrow> fset xs \<subseteq> fset ys"
   719   by (descending) (simp add: sub_list_def)
   726   by (descending) (simp)
   720 
   727 
   721 lemma psubset_fset: 
   728 lemma psubset_fset: 
   722   shows "xs |\<subset>| ys \<longleftrightarrow> fset xs \<subset> fset ys"
   729   shows "xs |\<subset>| ys \<longleftrightarrow> fset xs \<subset> fset ys"
   723   unfolding less_fset_def 
   730   unfolding less_fset_def 
   724   by (descending) (auto simp add: sub_list_def)
   731   by (descending) (auto)
   725 
   732 
   726 lemma subset_insert_fset:
   733 lemma subset_insert_fset:
   727   shows "(insert_fset x xs) |\<subseteq>| ys \<longleftrightarrow> x |\<in>| ys \<and> xs |\<subseteq>| ys"
   734   shows "(insert_fset x xs) |\<subseteq>| ys \<longleftrightarrow> x |\<in>| ys \<and> xs |\<subseteq>| ys"
   728   by (descending) (simp add: sub_list_def memb_def)
   735   by (descending) (simp)
   729 
   736 
   730 lemma subset_in_fset: 
   737 lemma subset_in_fset: 
   731   shows "xs |\<subseteq>| ys = (\<forall>x. x |\<in>| xs \<longrightarrow> x |\<in>| ys)"
   738   shows "xs |\<subseteq>| ys = (\<forall>x. x |\<in>| xs \<longrightarrow> x |\<in>| ys)"
   732   by (descending) (auto simp add: sub_list_def memb_def)
   739   by (descending) (auto)
   733 
   740 
   734 lemma subset_empty_fset:
   741 lemma subset_empty_fset:
   735   shows "xs |\<subseteq>| {||} \<longleftrightarrow> xs = {||}"
   742   shows "xs |\<subseteq>| {||} \<longleftrightarrow> xs = {||}"
   736   by (descending) (simp add: sub_list_def)
   743   by (descending) (simp)
   737 
   744 
   738 lemma not_psubset_empty_fset: 
   745 lemma not_psubset_empty_fset: 
   739   shows "\<not> xs |\<subset>| {||}"
   746   shows "\<not> xs |\<subset>| {||}"
   740   by (metis fset_simps(1) psubset_fset not_psubset_empty)
   747   by (metis fset_simps(1) psubset_fset not_psubset_empty)
   741 
   748 
   762 
   769 
   763 subsection {* card_fset *}
   770 subsection {* card_fset *}
   764 
   771 
   765 lemma card_fset: 
   772 lemma card_fset: 
   766   shows "card_fset xs = card (fset xs)"
   773   shows "card_fset xs = card (fset xs)"
   767   by (lifting card_list_def)
   774   by (descending) (simp)
   768 
   775 
   769 lemma card_insert_fset_iff [simp]:
   776 lemma card_insert_fset_iff [simp]:
   770   shows "card_fset (insert_fset x S) = (if x |\<in>| S then card_fset S else Suc (card_fset S))"
   777   shows "card_fset (insert_fset x S) = (if x |\<in>| S then card_fset S else Suc (card_fset S))"
   771   by (descending) (auto simp add: card_list_def memb_def insert_absorb)
   778   by (descending) (simp add: insert_absorb)
   772 
   779 
   773 lemma card_fset_0[simp]:
   780 lemma card_fset_0[simp]:
   774   shows "card_fset S = 0 \<longleftrightarrow> S = {||}"
   781   shows "card_fset S = 0 \<longleftrightarrow> S = {||}"
   775   by (descending) (simp add: card_list_def)
   782   by (descending) (simp)
   776 
   783 
   777 lemma card_empty_fset[simp]:
   784 lemma card_empty_fset[simp]:
   778   shows "card_fset {||} = 0"
   785   shows "card_fset {||} = 0"
   779   by (simp add: card_fset)
   786   by (simp add: card_fset)
   780 
   787 
   781 lemma card_fset_1:
   788 lemma card_fset_1:
   782   shows "card_fset S = 1 \<longleftrightarrow> (\<exists>x. S = {|x|})"
   789   shows "card_fset S = 1 \<longleftrightarrow> (\<exists>x. S = {|x|})"
   783   by (descending) (auto simp add: card_list_def card_Suc_eq)
   790   by (descending) (auto simp add: card_Suc_eq)
   784 
   791 
   785 lemma card_fset_gt_0:
   792 lemma card_fset_gt_0:
   786   shows "x \<in> fset S \<Longrightarrow> 0 < card_fset S"
   793   shows "x \<in> fset S \<Longrightarrow> 0 < card_fset S"
   787   by (descending) (auto simp add: card_list_def card_gt_0_iff)
   794   by (descending) (auto simp add: card_gt_0_iff)
   788   
   795   
   789 lemma card_notin_fset:
   796 lemma card_notin_fset:
   790   shows "(x |\<notin>| S) = (card_fset (insert_fset x S) = Suc (card_fset S))"
   797   shows "(x |\<notin>| S) = (card_fset (insert_fset x S) = Suc (card_fset S))"
   791   by simp
   798   by simp
   792 
   799 
   793 lemma card_fset_Suc: 
   800 lemma card_fset_Suc: 
   794   shows "card_fset S = Suc n \<Longrightarrow> \<exists>x T. x |\<notin>| T \<and> S = insert_fset x T \<and> card_fset T = n"
   801   shows "card_fset S = Suc n \<Longrightarrow> \<exists>x T. x |\<notin>| T \<and> S = insert_fset x T \<and> card_fset T = n"
   795   apply(descending)
   802   apply(descending)
   796   apply(auto simp add: card_list_def memb_def dest!: card_eq_SucD)
   803   apply(auto dest!: card_eq_SucD)
   797   by (metis Diff_insert_absorb set_removeAll)
   804   by (metis Diff_insert_absorb set_removeAll)
   798 
   805 
   799 lemma card_remove_fset_iff [simp]:
   806 lemma card_remove_fset_iff [simp]:
   800   shows "card_fset (remove_fset y S) = (if y |\<in>| S then card_fset S - 1 else card_fset S)"
   807   shows "card_fset (remove_fset y S) = (if y |\<in>| S then card_fset S - 1 else card_fset S)"
   801   by (descending) (simp add: card_list_def memb_def)
   808   by (descending) (simp)
   802 
   809 
   803 lemma card_Suc_exists_in_fset: 
   810 lemma card_Suc_exists_in_fset: 
   804   shows "card_fset S = Suc n \<Longrightarrow> \<exists>a. a |\<in>| S"
   811   shows "card_fset S = Suc n \<Longrightarrow> \<exists>a. a |\<in>| S"
   805   by (drule card_fset_Suc) (auto)
   812   by (drule card_fset_Suc) (auto)
   806 
   813 
   807 lemma in_card_fset_not_0: 
   814 lemma in_card_fset_not_0: 
   808   shows "a |\<in>| A \<Longrightarrow> card_fset A \<noteq> 0"
   815   shows "a |\<in>| A \<Longrightarrow> card_fset A \<noteq> 0"
   809   by (descending) (auto simp add: card_list_def memb_def)
   816   by (descending) (auto)
   810 
   817 
   811 lemma card_fset_mono: 
   818 lemma card_fset_mono: 
   812   shows "xs |\<subseteq>| ys \<Longrightarrow> card_fset xs \<le> card_fset ys"
   819   shows "xs |\<subseteq>| ys \<Longrightarrow> card_fset xs \<le> card_fset ys"
   813   unfolding card_fset psubset_fset
   820   unfolding card_fset psubset_fset
   814   by (simp add:  card_mono subset_fset)
   821   by (simp add: card_mono subset_fset)
   815 
   822 
   816 lemma card_subset_fset_eq: 
   823 lemma card_subset_fset_eq: 
   817   shows "xs |\<subseteq>| ys \<Longrightarrow> card_fset ys \<le> card_fset xs \<Longrightarrow> xs = ys"
   824   shows "xs |\<subseteq>| ys \<Longrightarrow> card_fset ys \<le> card_fset xs \<Longrightarrow> xs = ys"
   818   unfolding card_fset subset_fset
   825   unfolding card_fset subset_fset
   819   by (auto dest: card_seteq[OF finite_fset] simp add: fset_cong)
   826   by (auto dest: card_seteq[OF finite_fset] simp add: fset_cong)
   896 
   903 
   897 subsection {* filter_fset *}
   904 subsection {* filter_fset *}
   898 
   905 
   899 lemma subset_filter_fset: 
   906 lemma subset_filter_fset: 
   900   shows "filter_fset P xs |\<subseteq>| filter_fset Q xs = (\<forall> x. x |\<in>| xs \<longrightarrow> P x \<longrightarrow> Q x)"
   907   shows "filter_fset P xs |\<subseteq>| filter_fset Q xs = (\<forall> x. x |\<in>| xs \<longrightarrow> P x \<longrightarrow> Q x)"
   901   by  (descending) (auto simp add: memb_def sub_list_def)
   908   by  (descending) (auto)
   902 
   909 
   903 lemma eq_filter_fset: 
   910 lemma eq_filter_fset: 
   904   shows "(filter_fset P xs = filter_fset Q xs) = (\<forall>x. x |\<in>| xs \<longrightarrow> P x = Q x)"
   911   shows "(filter_fset P xs = filter_fset Q xs) = (\<forall>x. x |\<in>| xs \<longrightarrow> P x = Q x)"
   905   by (descending) (auto simp add: memb_def)
   912   by (descending) (auto)
   906 
   913 
   907 lemma psubset_filter_fset:
   914 lemma psubset_filter_fset:
   908   shows "(\<And>x. x |\<in>| xs \<Longrightarrow> P x \<Longrightarrow> Q x) \<Longrightarrow> (x |\<in>| xs & \<not> P x & Q x) \<Longrightarrow> 
   915   shows "(\<And>x. x |\<in>| xs \<Longrightarrow> P x \<Longrightarrow> Q x) \<Longrightarrow> (x |\<in>| xs & \<not> P x & Q x) \<Longrightarrow> 
   909     filter_fset P xs |\<subset>| filter_fset Q xs"
   916     filter_fset P xs |\<subset>| filter_fset Q xs"
   910   unfolding less_fset_def by (auto simp add: subset_filter_fset eq_filter_fset)
   917   unfolding less_fset_def by (auto simp add: subset_filter_fset eq_filter_fset)
   916   shows "fold_fset f z {||} = z"
   923   shows "fold_fset f z {||} = z"
   917   by (descending) (simp)
   924   by (descending) (simp)
   918 
   925 
   919 lemma fold_insert_fset: "fold_fset f z (insert_fset a A) =
   926 lemma fold_insert_fset: "fold_fset f z (insert_fset a A) =
   920   (if rsp_fold f then if a |\<in>| A then fold_fset f z A else f a (fold_fset f z A) else z)"
   927   (if rsp_fold f then if a |\<in>| A then fold_fset f z A else f a (fold_fset f z A) else z)"
   921   by (descending) (simp add: memb_def)
   928   by (descending) (simp)
   922 
   929 
   923 lemma in_commute_fold_fset:
   930 lemma in_commute_fold_fset:
   924   "\<lbrakk>rsp_fold f; h |\<in>| b\<rbrakk> \<Longrightarrow> fold_fset f z b = f h (fold_fset f z (remove_fset h b))"
   931   "\<lbrakk>rsp_fold f; h |\<in>| b\<rbrakk> \<Longrightarrow> fold_fset f z b = f h (fold_fset f z (remove_fset h b))"
   925   by (descending) (simp add: memb_def memb_commute_fold_list)
   932   by (descending) (simp add: memb_commute_fold_list)
   926 
   933 
   927 
   934 
   928 subsection {* Choice in fsets *}
   935 subsection {* Choice in fsets *}
   929 
   936 
   930 lemma fset_choice: 
   937 lemma fset_choice: 
   931   assumes a: "\<forall>x. x |\<in>| A \<longrightarrow> (\<exists>y. P x y)"
   938   assumes a: "\<forall>x. x |\<in>| A \<longrightarrow> (\<exists>y. P x y)"
   932   shows "\<exists>f. \<forall>x. x |\<in>| A \<longrightarrow> P x (f x)"
   939   shows "\<exists>f. \<forall>x. x |\<in>| A \<longrightarrow> P x (f x)"
   933   using a
   940   using a
   934   apply(descending)
   941   apply(descending)
   935   using finite_set_choice
   942   using finite_set_choice
   936   by (auto simp add: memb_def Ball_def)
   943   by (auto simp add: Ball_def)
   937 
   944 
   938 
   945 
   939 section {* Induction and Cases rules for fsets *}
   946 section {* Induction and Cases rules for fsets *}
   940 
   947 
   941 lemma fset_exhaust [case_names empty_fset insert_fset, cases type: fset]:
   948 lemma fset_exhaust [case_names empty_fset insert_fset, cases type: fset]:
   990   then show thesis by simp
   997   then show thesis by simp
   991 next
   998 next
   992   case (Cons a xs)
   999   case (Cons a xs)
   993   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
  1000   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
   994   have b: "\<And>x' ys'. \<lbrakk>\<not> memb x' ys'; a # xs \<approx> x' # ys'\<rbrakk> \<Longrightarrow> thesis" by fact
  1001   have b: "\<And>x' ys'. \<lbrakk>\<not> memb x' ys'; a # xs \<approx> x' # ys'\<rbrakk> \<Longrightarrow> thesis" by fact
   995   have c: "xs = [] \<Longrightarrow> thesis" using b unfolding memb_def
  1002   have c: "xs = [] \<Longrightarrow> thesis" using b 
   996     by (metis in_set_conv_nth less_zeroE list.size(3) list_eq.simps member_set)
  1003     apply(simp)
       
  1004     by (metis List.set.simps(1) emptyE empty_subsetI)
   997   have "\<And>x ys. \<lbrakk>\<not> memb x ys; xs \<approx> x # ys\<rbrakk> \<Longrightarrow> thesis"
  1005   have "\<And>x ys. \<lbrakk>\<not> memb x ys; xs \<approx> x # ys\<rbrakk> \<Longrightarrow> thesis"
   998   proof -
  1006   proof -
   999     fix x :: 'a
  1007     fix x :: 'a
  1000     fix ys :: "'a list"
  1008     fix ys :: "'a list"
  1001     assume d:"\<not> memb x ys"
  1009     assume d:"\<not> memb x ys"
  1006       then have f: "\<not> memb a ys" using d by simp
  1014       then have f: "\<not> memb a ys" using d by simp
  1007       have g: "a # xs \<approx> a # ys" using e h by auto
  1015       have g: "a # xs \<approx> a # ys" using e h by auto
  1008       show thesis using b f g by simp
  1016       show thesis using b f g by simp
  1009     next
  1017     next
  1010       assume h: "x \<noteq> a"
  1018       assume h: "x \<noteq> a"
  1011       then have f: "\<not> memb x (a # ys)" using d unfolding memb_def by auto
  1019       then have f: "\<not> memb x (a # ys)" using d by auto
  1012       have g: "a # xs \<approx> x # (a # ys)" using e h by auto
  1020       have g: "a # xs \<approx> x # (a # ys)" using e h by auto
  1013       show thesis using b f g by simp
  1021       show thesis using b f g by (simp del: memb.simps) 
  1014     qed
  1022     qed
  1015   qed
  1023   qed
  1016   then show thesis using a c by blast
  1024   then show thesis using a c by blast
  1017 qed
  1025 qed
  1018 
  1026 
  1056   by (induct xs) (auto intro: list_eq2.intros)
  1064   by (induct xs) (auto intro: list_eq2.intros)
  1057 
  1065 
  1058 lemma cons_delete_list_eq2:
  1066 lemma cons_delete_list_eq2:
  1059   shows "(a # (removeAll a A)) \<approx>2 (if memb a A then A else a # A)"
  1067   shows "(a # (removeAll a A)) \<approx>2 (if memb a A then A else a # A)"
  1060   apply (induct A)
  1068   apply (induct A)
  1061   apply (simp add: memb_def list_eq2_refl)
  1069   apply (simp add: list_eq2_refl)
  1062   apply (case_tac "memb a (aa # A)")
  1070   apply (case_tac "memb a (aa # A)")
  1063   apply (simp_all only: memb_def)
  1071   apply (simp_all)
  1064   apply (case_tac [!] "a = aa")
  1072   apply (case_tac [!] "a = aa")
  1065   apply (simp_all)
  1073   apply (simp_all)
  1066   apply (case_tac "memb a A")
  1074   apply (case_tac "memb a A")
  1067   apply (auto simp add: memb_def)[2]
  1075   apply (auto)[2]
  1068   apply (metis list_eq2.intros(3) list_eq2.intros(4) list_eq2.intros(5) list_eq2.intros(6))
  1076   apply (metis list_eq2.intros(3) list_eq2.intros(4) list_eq2.intros(5) list_eq2.intros(6))
  1069   apply (metis list_eq2.intros(1) list_eq2.intros(5) list_eq2.intros(6))
  1077   apply (metis list_eq2.intros(1) list_eq2.intros(5) list_eq2.intros(6))
  1070   apply (auto simp add: list_eq2_refl memb_def)
  1078   apply (auto simp add: list_eq2_refl memb_def)
  1071   done
  1079   done
  1072 
  1080 
  1087     have "l \<approx>2 r"
  1095     have "l \<approx>2 r"
  1088       using a b
  1096       using a b
  1089     proof (induct n arbitrary: l r)
  1097     proof (induct n arbitrary: l r)
  1090       case 0
  1098       case 0
  1091       have "card_list l = 0" by fact
  1099       have "card_list l = 0" by fact
  1092       then have "\<forall>x. \<not> memb x l" unfolding card_list_def memb_def by auto
  1100       then have "\<forall>x. \<not> memb x l" by auto
  1093       then have z: "l = []" unfolding memb_def by auto
  1101       then have z: "l = []" by auto
  1094       then have "r = []" using `l \<approx> r` by simp
  1102       then have "r = []" using `l \<approx> r` by simp
  1095       then show ?case using z list_eq2_refl by simp
  1103       then show ?case using z list_eq2_refl by simp
  1096     next
  1104     next
  1097       case (Suc m)
  1105       case (Suc m)
  1098       have b: "l \<approx> r" by fact
  1106       have b: "l \<approx> r" by fact
  1099       have d: "card_list l = Suc m" by fact
  1107       have d: "card_list l = Suc m" by fact
  1100       then have "\<exists>a. memb a l" 
  1108       then have "\<exists>a. memb a l" 
  1101 	apply(simp add: card_list_def memb_def)
  1109 	apply(simp)
  1102 	apply(drule card_eq_SucD)
  1110 	apply(drule card_eq_SucD)
  1103 	apply(blast)
  1111 	apply(blast)
  1104 	done
  1112 	done
  1105       then obtain a where e: "memb a l" by auto
  1113       then obtain a where e: "memb a l" by auto
  1106       then have e': "memb a r" using list_eq.simps[simplified memb_def[symmetric], of l r] b 
  1114       then have e': "memb a r" using list_eq.simps[simplified memb_def[symmetric], of l r] b 
  1107 	unfolding memb_def by auto
  1115 	by auto
  1108       have f: "card_list (removeAll a l) = m" using e d by (simp add: card_list_def memb_def)
  1116       have f: "card_list (removeAll a l) = m" using e d by (simp)
  1109       have g: "removeAll a l \<approx> removeAll a r" using removeAll_rsp b by simp
  1117       have g: "removeAll a l \<approx> removeAll a r" using removeAll_rsp b by simp
  1110       have "(removeAll a l) \<approx>2 (removeAll a r)" by (rule Suc.hyps[OF f g])
  1118       have "(removeAll a l) \<approx>2 (removeAll a r)" by (rule Suc.hyps[OF f g])
  1111       then have h: "(a # removeAll a l) \<approx>2 (a # removeAll a r)" by (rule list_eq2.intros(5))
  1119       then have h: "(a # removeAll a l) \<approx>2 (a # removeAll a r)" by (rule list_eq2.intros(5))
  1112       have i: "l \<approx>2 (a # removeAll a l)"	
  1120       have i: "l \<approx>2 (a # removeAll a l)"	
  1113         by (rule list_eq2.intros(3)[OF memb_delete_list_eq2[OF e]])
  1121         by (rule list_eq2.intros(3)[OF memb_delete_list_eq2[OF e]])