src/HOL/Quotient_Examples/FSet.thy
 author griff Wed Apr 04 15:15:48 2012 +0900 (2012-04-04) changeset 47435 e1b761c216ac parent 47308 9caab698dbe4 parent 47434 b75ce48a93ee child 47455 26315a545e26 permissions -rw-r--r--
manual merge
```     1 (*  Title:      HOL/Quotient3_Examples/FSet.thy
```
```     2     Author:     Cezary Kaliszyk, TU Munich
```
```     3     Author:     Christian Urban, TU Munich
```
```     4
```
```     5 Type of finite sets.
```
```     6 *)
```
```     7
```
```     8 theory FSet
```
```     9 imports "~~/src/HOL/Library/Multiset" "~~/src/HOL/Library/Quotient_List"
```
```    10 begin
```
```    11
```
```    12 text {*
```
```    13   The type of finite sets is created by a quotient construction
```
```    14   over lists. The definition of the equivalence:
```
```    15 *}
```
```    16
```
```    17 definition
```
```    18   list_eq :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool" (infix "\<approx>" 50)
```
```    19 where
```
```    20   [simp]: "xs \<approx> ys \<longleftrightarrow> set xs = set ys"
```
```    21
```
```    22 lemma list_eq_reflp:
```
```    23   "reflp list_eq"
```
```    24   by (auto intro: reflpI)
```
```    25
```
```    26 lemma list_eq_symp:
```
```    27   "symp list_eq"
```
```    28   by (auto intro: sympI)
```
```    29
```
```    30 lemma list_eq_transp:
```
```    31   "transp list_eq"
```
```    32   by (auto intro: transpI)
```
```    33
```
```    34 lemma list_eq_equivp:
```
```    35   "equivp list_eq"
```
```    36   by (auto intro: equivpI list_eq_reflp list_eq_symp list_eq_transp)
```
```    37
```
```    38 text {* The @{text fset} type *}
```
```    39
```
```    40 quotient_type
```
```    41   'a fset = "'a list" / "list_eq"
```
```    42   by (rule list_eq_equivp)
```
```    43
```
```    44 text {*
```
```    45   Definitions for sublist, cardinality,
```
```    46   intersection, difference and respectful fold over
```
```    47   lists.
```
```    48 *}
```
```    49
```
```    50 declare List.member_def [simp]
```
```    51
```
```    52 definition
```
```    53   sub_list :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool"
```
```    54 where
```
```    55   [simp]: "sub_list xs ys \<longleftrightarrow> set xs \<subseteq> set ys"
```
```    56
```
```    57 definition
```
```    58   card_list :: "'a list \<Rightarrow> nat"
```
```    59 where
```
```    60   [simp]: "card_list xs = card (set xs)"
```
```    61
```
```    62 definition
```
```    63   inter_list :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list"
```
```    64 where
```
```    65   [simp]: "inter_list xs ys = [x \<leftarrow> xs. x \<in> set xs \<and> x \<in> set ys]"
```
```    66
```
```    67 definition
```
```    68   diff_list :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list"
```
```    69 where
```
```    70   [simp]: "diff_list xs ys = [x \<leftarrow> xs. x \<notin> set ys]"
```
```    71
```
```    72 definition
```
```    73   rsp_fold :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> bool"
```
```    74 where
```
```    75   "rsp_fold f \<longleftrightarrow> (\<forall>u v. f u \<circ> f v = f v \<circ> f u)"
```
```    76
```
```    77 lemma rsp_foldI:
```
```    78   "(\<And>u v. f u \<circ> f v = f v \<circ> f u) \<Longrightarrow> rsp_fold f"
```
```    79   by (simp add: rsp_fold_def)
```
```    80
```
```    81 lemma rsp_foldE:
```
```    82   assumes "rsp_fold f"
```
```    83   obtains "f u \<circ> f v = f v \<circ> f u"
```
```    84   using assms by (simp add: rsp_fold_def)
```
```    85
```
```    86 definition
```
```    87   fold_once :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'a list \<Rightarrow> 'b \<Rightarrow> 'b"
```
```    88 where
```
```    89   "fold_once f xs = (if rsp_fold f then fold f (remdups xs) else id)"
```
```    90
```
```    91 lemma fold_once_default [simp]:
```
```    92   "\<not> rsp_fold f \<Longrightarrow> fold_once f xs = id"
```
```    93   by (simp add: fold_once_def)
```
```    94
```
```    95 lemma fold_once_fold_remdups:
```
```    96   "rsp_fold f \<Longrightarrow> fold_once f xs = fold f (remdups xs)"
```
```    97   by (simp add: fold_once_def)
```
```    98
```
```    99
```
```   100 section {* Quotient composition lemmas *}
```
```   101
```
```   102 lemma list_all2_refl':
```
```   103   assumes q: "equivp R"
```
```   104   shows "(list_all2 R) r r"
```
```   105   by (rule list_all2_refl) (metis equivp_def q)
```
```   106
```
```   107 lemma compose_list_refl:
```
```   108   assumes q: "equivp R"
```
```   109   shows "(list_all2 R OOO op \<approx>) r r"
```
```   110 proof
```
```   111   have *: "r \<approx> r" by (rule equivp_reflp[OF fset_equivp])
```
```   112   show "list_all2 R r r" by (rule list_all2_refl'[OF q])
```
```   113   with * show "(op \<approx> OO list_all2 R) r r" ..
```
```   114 qed
```
```   115
```
```   116 lemma map_list_eq_cong: "b \<approx> ba \<Longrightarrow> map f b \<approx> map f ba"
```
```   117   by (simp only: list_eq_def set_map)
```
```   118
```
```   119 lemma quotient_compose_list_g:
```
```   120   assumes q: "Quotient3 R Abs Rep"
```
```   121   and     e: "equivp R"
```
```   122   shows  "Quotient3 ((list_all2 R) OOO (op \<approx>))
```
```   123     (abs_fset \<circ> (map Abs)) ((map Rep) \<circ> rep_fset)"
```
```   124   unfolding Quotient3_def comp_def
```
```   125 proof (intro conjI allI)
```
```   126   fix a r s
```
```   127   show "abs_fset (map Abs (map Rep (rep_fset a))) = a"
```
```   128     by (simp add: abs_o_rep[OF q] Quotient3_abs_rep[OF Quotient3_fset] List.map.id)
```
```   129   have b: "list_all2 R (map Rep (rep_fset a)) (map Rep (rep_fset a))"
```
```   130     by (rule list_all2_refl'[OF e])
```
```   131   have c: "(op \<approx> OO list_all2 R) (map Rep (rep_fset a)) (map Rep (rep_fset a))"
```
```   132     by (rule, rule equivp_reflp[OF fset_equivp]) (rule b)
```
```   133   show "(list_all2 R OOO op \<approx>) (map Rep (rep_fset a)) (map Rep (rep_fset a))"
```
```   134     by (rule, rule list_all2_refl'[OF e]) (rule c)
```
```   135   show "(list_all2 R OOO op \<approx>) r s = ((list_all2 R OOO op \<approx>) r r \<and>
```
```   136         (list_all2 R OOO op \<approx>) s s \<and> abs_fset (map Abs r) = abs_fset (map Abs s))"
```
```   137   proof (intro iffI conjI)
```
```   138     show "(list_all2 R OOO op \<approx>) r r" by (rule compose_list_refl[OF e])
```
```   139     show "(list_all2 R OOO op \<approx>) s s" by (rule compose_list_refl[OF e])
```
```   140   next
```
```   141     assume a: "(list_all2 R OOO op \<approx>) r s"
```
```   142     then have b: "map Abs r \<approx> map Abs s"
```
```   143     proof (elim relcomppE)
```
```   144       fix b ba
```
```   145       assume c: "list_all2 R r b"
```
```   146       assume d: "b \<approx> ba"
```
```   147       assume e: "list_all2 R ba s"
```
```   148       have f: "map Abs r = map Abs b"
```
```   149         using Quotient3_rel[OF list_quotient3[OF q]] c by blast
```
```   150       have "map Abs ba = map Abs s"
```
```   151         using Quotient3_rel[OF list_quotient3[OF q]] e by blast
```
```   152       then have g: "map Abs s = map Abs ba" by simp
```
```   153       then show "map Abs r \<approx> map Abs s" using d f map_list_eq_cong by simp
```
```   154     qed
```
```   155     then show "abs_fset (map Abs r) = abs_fset (map Abs s)"
```
```   156       using Quotient3_rel[OF Quotient3_fset] by blast
```
```   157   next
```
```   158     assume a: "(list_all2 R OOO op \<approx>) r r \<and> (list_all2 R OOO op \<approx>) s s
```
```   159       \<and> abs_fset (map Abs r) = abs_fset (map Abs s)"
```
```   160     then have s: "(list_all2 R OOO op \<approx>) s s" by simp
```
```   161     have d: "map Abs r \<approx> map Abs s"
```
```   162       by (subst Quotient3_rel [OF Quotient3_fset, symmetric]) (simp add: a)
```
```   163     have b: "map Rep (map Abs r) \<approx> map Rep (map Abs s)"
```
```   164       by (rule map_list_eq_cong[OF d])
```
```   165     have y: "list_all2 R (map Rep (map Abs s)) s"
```
```   166       by (fact rep_abs_rsp_left[OF list_quotient3[OF q], OF list_all2_refl'[OF e, of s]])
```
```   167     have c: "(op \<approx> OO list_all2 R) (map Rep (map Abs r)) s"
```
```   168       by (rule relcomppI) (rule b, rule y)
```
```   169     have z: "list_all2 R r (map Rep (map Abs r))"
```
```   170       by (fact rep_abs_rsp[OF list_quotient3[OF q], OF list_all2_refl'[OF e, of r]])
```
```   171     then show "(list_all2 R OOO op \<approx>) r s"
```
```   172       using a c relcomppI by simp
```
```   173   qed
```
```   174 qed
```
```   175
```
```   176 lemma quotient_compose_list[quot_thm]:
```
```   177   shows  "Quotient3 ((list_all2 op \<approx>) OOO (op \<approx>))
```
```   178     (abs_fset \<circ> (map abs_fset)) ((map rep_fset) \<circ> rep_fset)"
```
```   179   by (rule quotient_compose_list_g, rule Quotient3_fset, rule list_eq_equivp)
```
```   180
```
```   181
```
```   182 section {* Quotient definitions for fsets *}
```
```   183
```
```   184
```
```   185 subsection {* Finite sets are a bounded, distributive lattice with minus *}
```
```   186
```
```   187 instantiation fset :: (type) "{bounded_lattice_bot, distrib_lattice, minus}"
```
```   188 begin
```
```   189
```
```   190 quotient_definition
```
```   191   "bot :: 'a fset"
```
```   192   is "Nil :: 'a list" done
```
```   193
```
```   194 abbreviation
```
```   195   empty_fset  ("{||}")
```
```   196 where
```
```   197   "{||} \<equiv> bot :: 'a fset"
```
```   198
```
```   199 quotient_definition
```
```   200   "less_eq_fset :: ('a fset \<Rightarrow> 'a fset \<Rightarrow> bool)"
```
```   201   is "sub_list :: ('a list \<Rightarrow> 'a list \<Rightarrow> bool)" by simp
```
```   202
```
```   203 abbreviation
```
```   204   subset_fset :: "'a fset \<Rightarrow> 'a fset \<Rightarrow> bool" (infix "|\<subseteq>|" 50)
```
```   205 where
```
```   206   "xs |\<subseteq>| ys \<equiv> xs \<le> ys"
```
```   207
```
```   208 definition
```
```   209   less_fset :: "'a fset \<Rightarrow> 'a fset \<Rightarrow> bool"
```
```   210 where
```
```   211   "xs < ys \<equiv> xs \<le> ys \<and> xs \<noteq> (ys::'a fset)"
```
```   212
```
```   213 abbreviation
```
```   214   psubset_fset :: "'a fset \<Rightarrow> 'a fset \<Rightarrow> bool" (infix "|\<subset>|" 50)
```
```   215 where
```
```   216   "xs |\<subset>| ys \<equiv> xs < ys"
```
```   217
```
```   218 quotient_definition
```
```   219   "sup :: 'a fset \<Rightarrow> 'a fset \<Rightarrow> 'a fset"
```
```   220   is "append :: 'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" by simp
```
```   221
```
```   222 abbreviation
```
```   223   union_fset (infixl "|\<union>|" 65)
```
```   224 where
```
```   225   "xs |\<union>| ys \<equiv> sup xs (ys::'a fset)"
```
```   226
```
```   227 quotient_definition
```
```   228   "inf :: 'a fset \<Rightarrow> 'a fset \<Rightarrow> 'a fset"
```
```   229   is "inter_list :: 'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" by simp
```
```   230
```
```   231 abbreviation
```
```   232   inter_fset (infixl "|\<inter>|" 65)
```
```   233 where
```
```   234   "xs |\<inter>| ys \<equiv> inf xs (ys::'a fset)"
```
```   235
```
```   236 quotient_definition
```
```   237   "minus :: 'a fset \<Rightarrow> 'a fset \<Rightarrow> 'a fset"
```
```   238   is "diff_list :: 'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" by fastforce
```
```   239
```
```   240 instance
```
```   241 proof
```
```   242   fix x y z :: "'a fset"
```
```   243   show "x |\<subset>| y \<longleftrightarrow> x |\<subseteq>| y \<and> \<not> y |\<subseteq>| x"
```
```   244     by (unfold less_fset_def, descending) auto
```
```   245   show "x |\<subseteq>| x" by (descending) (simp)
```
```   246   show "{||} |\<subseteq>| x" by (descending) (simp)
```
```   247   show "x |\<subseteq>| x |\<union>| y" by (descending) (simp)
```
```   248   show "y |\<subseteq>| x |\<union>| y" by (descending) (simp)
```
```   249   show "x |\<inter>| y |\<subseteq>| x" by (descending) (auto)
```
```   250   show "x |\<inter>| y |\<subseteq>| y" by (descending) (auto)
```
```   251   show "x |\<union>| (y |\<inter>| z) = x |\<union>| y |\<inter>| (x |\<union>| z)"
```
```   252     by (descending) (auto)
```
```   253 next
```
```   254   fix x y z :: "'a fset"
```
```   255   assume a: "x |\<subseteq>| y"
```
```   256   assume b: "y |\<subseteq>| z"
```
```   257   show "x |\<subseteq>| z" using a b by (descending) (simp)
```
```   258 next
```
```   259   fix x y :: "'a fset"
```
```   260   assume a: "x |\<subseteq>| y"
```
```   261   assume b: "y |\<subseteq>| x"
```
```   262   show "x = y" using a b by (descending) (auto)
```
```   263 next
```
```   264   fix x y z :: "'a fset"
```
```   265   assume a: "y |\<subseteq>| x"
```
```   266   assume b: "z |\<subseteq>| x"
```
```   267   show "y |\<union>| z |\<subseteq>| x" using a b by (descending) (simp)
```
```   268 next
```
```   269   fix x y z :: "'a fset"
```
```   270   assume a: "x |\<subseteq>| y"
```
```   271   assume b: "x |\<subseteq>| z"
```
```   272   show "x |\<subseteq>| y |\<inter>| z" using a b by (descending) (auto)
```
```   273 qed
```
```   274
```
```   275 end
```
```   276
```
```   277
```
```   278 subsection {* Other constants for fsets *}
```
```   279
```
```   280 quotient_definition
```
```   281   "insert_fset :: 'a \<Rightarrow> 'a fset \<Rightarrow> 'a fset"
```
```   282   is "Cons" by auto
```
```   283
```
```   284 syntax
```
```   285   "_insert_fset"     :: "args => 'a fset"  ("{|(_)|}")
```
```   286
```
```   287 translations
```
```   288   "{|x, xs|}" == "CONST insert_fset x {|xs|}"
```
```   289   "{|x|}"     == "CONST insert_fset x {||}"
```
```   290
```
```   291 quotient_definition
```
```   292   fset_member
```
```   293 where
```
```   294   "fset_member :: 'a fset \<Rightarrow> 'a \<Rightarrow> bool" is "List.member" by fastforce
```
```   295
```
```   296 abbreviation
```
```   297   in_fset :: "'a \<Rightarrow> 'a fset \<Rightarrow> bool" (infix "|\<in>|" 50)
```
```   298 where
```
```   299   "x |\<in>| S \<equiv> fset_member S x"
```
```   300
```
```   301 abbreviation
```
```   302   notin_fset :: "'a \<Rightarrow> 'a fset \<Rightarrow> bool" (infix "|\<notin>|" 50)
```
```   303 where
```
```   304   "x |\<notin>| S \<equiv> \<not> (x |\<in>| S)"
```
```   305
```
```   306
```
```   307 subsection {* Other constants on the Quotient Type *}
```
```   308
```
```   309 quotient_definition
```
```   310   "card_fset :: 'a fset \<Rightarrow> nat"
```
```   311   is card_list by simp
```
```   312
```
```   313 quotient_definition
```
```   314   "map_fset :: ('a \<Rightarrow> 'b) \<Rightarrow> 'a fset \<Rightarrow> 'b fset"
```
```   315   is map by simp
```
```   316
```
```   317 quotient_definition
```
```   318   "remove_fset :: 'a \<Rightarrow> 'a fset \<Rightarrow> 'a fset"
```
```   319   is removeAll by simp
```
```   320
```
```   321 quotient_definition
```
```   322   "fset :: 'a fset \<Rightarrow> 'a set"
```
```   323   is "set" by simp
```
```   324
```
```   325 lemma fold_once_set_equiv:
```
```   326   assumes "xs \<approx> ys"
```
```   327   shows "fold_once f xs = fold_once f ys"
```
```   328 proof (cases "rsp_fold f")
```
```   329   case False then show ?thesis by simp
```
```   330 next
```
```   331   case True
```
```   332   then have "\<And>x y. x \<in> set (remdups xs) \<Longrightarrow> y \<in> set (remdups xs) \<Longrightarrow> f x \<circ> f y = f y \<circ> f x"
```
```   333     by (rule rsp_foldE)
```
```   334   moreover from assms have "multiset_of (remdups xs) = multiset_of (remdups ys)"
```
```   335     by (simp add: set_eq_iff_multiset_of_remdups_eq)
```
```   336   ultimately have "fold f (remdups xs) = fold f (remdups ys)"
```
```   337     by (rule fold_multiset_equiv)
```
```   338   with True show ?thesis by (simp add: fold_once_fold_remdups)
```
```   339 qed
```
```   340
```
```   341 quotient_definition
```
```   342   "fold_fset :: ('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'a fset \<Rightarrow> 'b \<Rightarrow> 'b"
```
```   343   is fold_once by (rule fold_once_set_equiv)
```
```   344
```
```   345 lemma concat_rsp_pre:
```
```   346   assumes a: "list_all2 op \<approx> x x'"
```
```   347   and     b: "x' \<approx> y'"
```
```   348   and     c: "list_all2 op \<approx> y' y"
```
```   349   and     d: "\<exists>x\<in>set x. xa \<in> set x"
```
```   350   shows "\<exists>x\<in>set y. xa \<in> set x"
```
```   351 proof -
```
```   352   obtain xb where e: "xb \<in> set x" and f: "xa \<in> set xb" using d by auto
```
```   353   have "\<exists>y. y \<in> set x' \<and> xb \<approx> y" by (rule list_all2_find_element[OF e a])
```
```   354   then obtain ya where h: "ya \<in> set x'" and i: "xb \<approx> ya" by auto
```
```   355   have "ya \<in> set y'" using b h by simp
```
```   356   then have "\<exists>yb. yb \<in> set y \<and> ya \<approx> yb" using c by (rule list_all2_find_element)
```
```   357   then show ?thesis using f i by auto
```
```   358 qed
```
```   359
```
```   360 quotient_definition
```
```   361   "concat_fset :: ('a fset) fset \<Rightarrow> 'a fset"
```
```   362   is concat
```
```   363 proof (elim relcomppE)
```
```   364 fix a b ba bb
```
```   365   assume a: "list_all2 op \<approx> a ba"
```
```   366   with list_symp [OF list_eq_symp] have a': "list_all2 op \<approx> ba a" by (rule sympE)
```
```   367   assume b: "ba \<approx> bb"
```
```   368   with list_eq_symp have b': "bb \<approx> ba" by (rule sympE)
```
```   369   assume c: "list_all2 op \<approx> bb b"
```
```   370   with list_symp [OF list_eq_symp] have c': "list_all2 op \<approx> b bb" by (rule sympE)
```
```   371   have "\<forall>x. (\<exists>xa\<in>set a. x \<in> set xa) = (\<exists>xa\<in>set b. x \<in> set xa)"
```
```   372   proof
```
```   373     fix x
```
```   374     show "(\<exists>xa\<in>set a. x \<in> set xa) = (\<exists>xa\<in>set b. x \<in> set xa)"
```
```   375     proof
```
```   376       assume d: "\<exists>xa\<in>set a. x \<in> set xa"
```
```   377       show "\<exists>xa\<in>set b. x \<in> set xa" by (rule concat_rsp_pre[OF a b c d])
```
```   378     next
```
```   379       assume e: "\<exists>xa\<in>set b. x \<in> set xa"
```
```   380       show "\<exists>xa\<in>set a. x \<in> set xa" by (rule concat_rsp_pre[OF c' b' a' e])
```
```   381     qed
```
```   382   qed
```
```   383   then show "concat a \<approx> concat b" by auto
```
```   384 qed
```
```   385
```
```   386 quotient_definition
```
```   387   "filter_fset :: ('a \<Rightarrow> bool) \<Rightarrow> 'a fset \<Rightarrow> 'a fset"
```
```   388   is filter by force
```
```   389
```
```   390
```
```   391 subsection {* Compositional respectfulness and preservation lemmas *}
```
```   392
```
```   393 lemma Nil_rsp2 [quot_respect]:
```
```   394   shows "(list_all2 op \<approx> OOO op \<approx>) Nil Nil"
```
```   395   by (rule compose_list_refl, rule list_eq_equivp)
```
```   396
```
```   397 lemma Cons_rsp2 [quot_respect]:
```
```   398   shows "(op \<approx> ===> list_all2 op \<approx> OOO op \<approx> ===> list_all2 op \<approx> OOO op \<approx>) Cons Cons"
```
```   399   apply (auto intro!: fun_relI)
```
```   400   apply (rule_tac b="x # b" in relcomppI)
```
```   401   apply auto
```
```   402   apply (rule_tac b="x # ba" in relcomppI)
```
```   403   apply auto
```
```   404   done
```
```   405
```
```   406 lemma Nil_prs2 [quot_preserve]:
```
```   407   assumes "Quotient3 R Abs Rep"
```
```   408   shows "(Abs \<circ> map f) [] = Abs []"
```
```   409   by simp
```
```   410
```
```   411 lemma Cons_prs2 [quot_preserve]:
```
```   412   assumes q: "Quotient3 R1 Abs1 Rep1"
```
```   413   and     r: "Quotient3 R2 Abs2 Rep2"
```
```   414   shows "(Rep1 ---> (map Rep1 \<circ> Rep2) ---> (Abs2 \<circ> map Abs1)) (op #) = (id ---> Rep2 ---> Abs2) (op #)"
```
```   415   by (auto simp add: fun_eq_iff comp_def Quotient3_abs_rep [OF q])
```
```   416
```
```   417 lemma append_prs2 [quot_preserve]:
```
```   418   assumes q: "Quotient3 R1 Abs1 Rep1"
```
```   419   and     r: "Quotient3 R2 Abs2 Rep2"
```
```   420   shows "((map Rep1 \<circ> Rep2) ---> (map Rep1 \<circ> Rep2) ---> (Abs2 \<circ> map Abs1)) op @ =
```
```   421     (Rep2 ---> Rep2 ---> Abs2) op @"
```
```   422   by (simp add: fun_eq_iff abs_o_rep[OF q] List.map.id)
```
```   423
```
```   424 lemma list_all2_app_l:
```
```   425   assumes a: "reflp R"
```
```   426   and b: "list_all2 R l r"
```
```   427   shows "list_all2 R (z @ l) (z @ r)"
```
```   428   using a b by (induct z) (auto elim: reflpE)
```
```   429
```
```   430 lemma append_rsp2_pre0:
```
```   431   assumes a:"list_all2 op \<approx> x x'"
```
```   432   shows "list_all2 op \<approx> (x @ z) (x' @ z)"
```
```   433   using a apply (induct x x' rule: list_induct2')
```
```   434   by simp_all (rule list_all2_refl'[OF list_eq_equivp])
```
```   435
```
```   436 lemma append_rsp2_pre1:
```
```   437   assumes a:"list_all2 op \<approx> x x'"
```
```   438   shows "list_all2 op \<approx> (z @ x) (z @ x')"
```
```   439   using a apply (induct x x' arbitrary: z rule: list_induct2')
```
```   440   apply (rule list_all2_refl'[OF list_eq_equivp])
```
```   441   apply (simp_all del: list_eq_def)
```
```   442   apply (rule list_all2_app_l)
```
```   443   apply (simp_all add: reflpI)
```
```   444   done
```
```   445
```
```   446 lemma append_rsp2_pre:
```
```   447   assumes "list_all2 op \<approx> x x'"
```
```   448     and "list_all2 op \<approx> z z'"
```
```   449   shows "list_all2 op \<approx> (x @ z) (x' @ z')"
```
```   450   using assms by (rule list_all2_appendI)
```
```   451
```
```   452 lemma compositional_rsp3:
```
```   453   assumes "(R1 ===> R2 ===> R3) C C" and "(R4 ===> R5 ===> R6) C C"
```
```   454   shows "(R1 OOO R4 ===> R2 OOO R5 ===> R3 OOO R6) C C"
```
```   455   by (auto intro!: fun_relI)
```
```   456      (metis (full_types) assms fun_relE relcomppI)
```
```   457
```
```   458 lemma append_rsp2 [quot_respect]:
```
```   459   "(list_all2 op \<approx> OOO op \<approx> ===> list_all2 op \<approx> OOO op \<approx> ===> list_all2 op \<approx> OOO op \<approx>) append append"
```
```   460   by (intro compositional_rsp3)
```
```   461      (auto intro!: fun_relI simp add: append_rsp2_pre)
```
```   462
```
```   463 lemma map_rsp2 [quot_respect]:
```
```   464   "((op \<approx> ===> op \<approx>) ===> list_all2 op \<approx> OOO op \<approx> ===> list_all2 op \<approx> OOO op \<approx>) map map"
```
```   465 proof (auto intro!: fun_relI)
```
```   466   fix f f' :: "'a list \<Rightarrow> 'b list"
```
```   467   fix xa ya x y :: "'a list list"
```
```   468   assume fs: "(op \<approx> ===> op \<approx>) f f'" and x: "list_all2 op \<approx> xa x" and xy: "set x = set y" and y: "list_all2 op \<approx> y ya"
```
```   469   have a: "(list_all2 op \<approx>) (map f xa) (map f x)"
```
```   470     using x
```
```   471     by (induct xa x rule: list_induct2')
```
```   472        (simp_all, metis fs fun_relE list_eq_def)
```
```   473   have b: "set (map f x) = set (map f y)"
```
```   474     using xy fs
```
```   475     by (induct x y rule: list_induct2')
```
```   476        (simp_all, metis image_insert)
```
```   477   have c: "(list_all2 op \<approx>) (map f y) (map f' ya)"
```
```   478     using y fs
```
```   479     by (induct y ya rule: list_induct2')
```
```   480        (simp_all, metis apply_rsp' list_eq_def)
```
```   481   show "(list_all2 op \<approx> OOO op \<approx>) (map f xa) (map f' ya)"
```
```   482     by (metis a b c list_eq_def relcomppI)
```
```   483 qed
```
```   484
```
```   485 lemma map_prs2 [quot_preserve]:
```
```   486   shows "((abs_fset ---> rep_fset) ---> (map rep_fset \<circ> rep_fset) ---> abs_fset \<circ> map abs_fset) map = (id ---> rep_fset ---> abs_fset) map"
```
```   487   by (auto simp add: fun_eq_iff)
```
```   488      (simp only: map_map[symmetric] map_prs_aux[OF Quotient3_fset Quotient3_fset])
```
```   489
```
```   490 section {* Lifted theorems *}
```
```   491
```
```   492 subsection {* fset *}
```
```   493
```
```   494 lemma fset_simps [simp]:
```
```   495   shows "fset {||} = {}"
```
```   496   and   "fset (insert_fset x S) = insert x (fset S)"
```
```   497   by (descending, simp)+
```
```   498
```
```   499 lemma finite_fset [simp]:
```
```   500   shows "finite (fset S)"
```
```   501   by (descending) (simp)
```
```   502
```
```   503 lemma fset_cong:
```
```   504   shows "fset S = fset T \<longleftrightarrow> S = T"
```
```   505   by (descending) (simp)
```
```   506
```
```   507 lemma filter_fset [simp]:
```
```   508   shows "fset (filter_fset P xs) = Collect P \<inter> fset xs"
```
```   509   by (descending) (auto)
```
```   510
```
```   511 lemma remove_fset [simp]:
```
```   512   shows "fset (remove_fset x xs) = fset xs - {x}"
```
```   513   by (descending) (simp)
```
```   514
```
```   515 lemma inter_fset [simp]:
```
```   516   shows "fset (xs |\<inter>| ys) = fset xs \<inter> fset ys"
```
```   517   by (descending) (auto)
```
```   518
```
```   519 lemma union_fset [simp]:
```
```   520   shows "fset (xs |\<union>| ys) = fset xs \<union> fset ys"
```
```   521   by (lifting set_append)
```
```   522
```
```   523 lemma minus_fset [simp]:
```
```   524   shows "fset (xs - ys) = fset xs - fset ys"
```
```   525   by (descending) (auto)
```
```   526
```
```   527
```
```   528 subsection {* in_fset *}
```
```   529
```
```   530 lemma in_fset:
```
```   531   shows "x |\<in>| S \<longleftrightarrow> x \<in> fset S"
```
```   532   by descending simp
```
```   533
```
```   534 lemma notin_fset:
```
```   535   shows "x |\<notin>| S \<longleftrightarrow> x \<notin> fset S"
```
```   536   by (simp add: in_fset)
```
```   537
```
```   538 lemma notin_empty_fset:
```
```   539   shows "x |\<notin>| {||}"
```
```   540   by (simp add: in_fset)
```
```   541
```
```   542 lemma fset_eq_iff:
```
```   543   shows "S = T \<longleftrightarrow> (\<forall>x. (x |\<in>| S) = (x |\<in>| T))"
```
```   544   by descending auto
```
```   545
```
```   546 lemma none_in_empty_fset:
```
```   547   shows "(\<forall>x. x |\<notin>| S) \<longleftrightarrow> S = {||}"
```
```   548   by descending simp
```
```   549
```
```   550
```
```   551 subsection {* insert_fset *}
```
```   552
```
```   553 lemma in_insert_fset_iff [simp]:
```
```   554   shows "x |\<in>| insert_fset y S \<longleftrightarrow> x = y \<or> x |\<in>| S"
```
```   555   by descending simp
```
```   556
```
```   557 lemma
```
```   558   shows insert_fsetI1: "x |\<in>| insert_fset x S"
```
```   559   and   insert_fsetI2: "x |\<in>| S \<Longrightarrow> x |\<in>| insert_fset y S"
```
```   560   by simp_all
```
```   561
```
```   562 lemma insert_absorb_fset [simp]:
```
```   563   shows "x |\<in>| S \<Longrightarrow> insert_fset x S = S"
```
```   564   by (descending) (auto)
```
```   565
```
```   566 lemma empty_not_insert_fset[simp]:
```
```   567   shows "{||} \<noteq> insert_fset x S"
```
```   568   and   "insert_fset x S \<noteq> {||}"
```
```   569   by (descending, simp)+
```
```   570
```
```   571 lemma insert_fset_left_comm:
```
```   572   shows "insert_fset x (insert_fset y S) = insert_fset y (insert_fset x S)"
```
```   573   by (descending) (auto)
```
```   574
```
```   575 lemma insert_fset_left_idem:
```
```   576   shows "insert_fset x (insert_fset x S) = insert_fset x S"
```
```   577   by (descending) (auto)
```
```   578
```
```   579 lemma singleton_fset_eq[simp]:
```
```   580   shows "{|x|} = {|y|} \<longleftrightarrow> x = y"
```
```   581   by (descending) (auto)
```
```   582
```
```   583 lemma in_fset_mdef:
```
```   584   shows "x |\<in>| F \<longleftrightarrow> x |\<notin>| (F - {|x|}) \<and> F = insert_fset x (F - {|x|})"
```
```   585   by (descending) (auto)
```
```   586
```
```   587
```
```   588 subsection {* union_fset *}
```
```   589
```
```   590 lemmas [simp] =
```
```   591   sup_bot_left[where 'a="'a fset"]
```
```   592   sup_bot_right[where 'a="'a fset"]
```
```   593
```
```   594 lemma union_insert_fset [simp]:
```
```   595   shows "insert_fset x S |\<union>| T = insert_fset x (S |\<union>| T)"
```
```   596   by (lifting append.simps(2))
```
```   597
```
```   598 lemma singleton_union_fset_left:
```
```   599   shows "{|a|} |\<union>| S = insert_fset a S"
```
```   600   by simp
```
```   601
```
```   602 lemma singleton_union_fset_right:
```
```   603   shows "S |\<union>| {|a|} = insert_fset a S"
```
```   604   by (subst sup.commute) simp
```
```   605
```
```   606 lemma in_union_fset:
```
```   607   shows "x |\<in>| S |\<union>| T \<longleftrightarrow> x |\<in>| S \<or> x |\<in>| T"
```
```   608   by (descending) (simp)
```
```   609
```
```   610
```
```   611 subsection {* minus_fset *}
```
```   612
```
```   613 lemma minus_in_fset:
```
```   614   shows "x |\<in>| (xs - ys) \<longleftrightarrow> x |\<in>| xs \<and> x |\<notin>| ys"
```
```   615   by (descending) (simp)
```
```   616
```
```   617 lemma minus_insert_fset:
```
```   618   shows "insert_fset x xs - ys = (if x |\<in>| ys then xs - ys else insert_fset x (xs - ys))"
```
```   619   by (descending) (auto)
```
```   620
```
```   621 lemma minus_insert_in_fset[simp]:
```
```   622   shows "x |\<in>| ys \<Longrightarrow> insert_fset x xs - ys = xs - ys"
```
```   623   by (simp add: minus_insert_fset)
```
```   624
```
```   625 lemma minus_insert_notin_fset[simp]:
```
```   626   shows "x |\<notin>| ys \<Longrightarrow> insert_fset x xs - ys = insert_fset x (xs - ys)"
```
```   627   by (simp add: minus_insert_fset)
```
```   628
```
```   629 lemma in_minus_fset:
```
```   630   shows "x |\<in>| F - S \<Longrightarrow> x |\<notin>| S"
```
```   631   unfolding in_fset minus_fset
```
```   632   by blast
```
```   633
```
```   634 lemma notin_minus_fset:
```
```   635   shows "x |\<in>| S \<Longrightarrow> x |\<notin>| F - S"
```
```   636   unfolding in_fset minus_fset
```
```   637   by blast
```
```   638
```
```   639
```
```   640 subsection {* remove_fset *}
```
```   641
```
```   642 lemma in_remove_fset:
```
```   643   shows "x |\<in>| remove_fset y S \<longleftrightarrow> x |\<in>| S \<and> x \<noteq> y"
```
```   644   by (descending) (simp)
```
```   645
```
```   646 lemma notin_remove_fset:
```
```   647   shows "x |\<notin>| remove_fset x S"
```
```   648   by (descending) (simp)
```
```   649
```
```   650 lemma notin_remove_ident_fset:
```
```   651   shows "x |\<notin>| S \<Longrightarrow> remove_fset x S = S"
```
```   652   by (descending) (simp)
```
```   653
```
```   654 lemma remove_fset_cases:
```
```   655   shows "S = {||} \<or> (\<exists>x. x |\<in>| S \<and> S = insert_fset x (remove_fset x S))"
```
```   656   by (descending) (auto simp add: insert_absorb)
```
```   657
```
```   658
```
```   659 subsection {* inter_fset *}
```
```   660
```
```   661 lemma inter_empty_fset_l:
```
```   662   shows "{||} |\<inter>| S = {||}"
```
```   663   by simp
```
```   664
```
```   665 lemma inter_empty_fset_r:
```
```   666   shows "S |\<inter>| {||} = {||}"
```
```   667   by simp
```
```   668
```
```   669 lemma inter_insert_fset:
```
```   670   shows "insert_fset x S |\<inter>| T = (if x |\<in>| T then insert_fset x (S |\<inter>| T) else S |\<inter>| T)"
```
```   671   by (descending) (auto)
```
```   672
```
```   673 lemma in_inter_fset:
```
```   674   shows "x |\<in>| (S |\<inter>| T) \<longleftrightarrow> x |\<in>| S \<and> x |\<in>| T"
```
```   675   by (descending) (simp)
```
```   676
```
```   677
```
```   678 subsection {* subset_fset and psubset_fset *}
```
```   679
```
```   680 lemma subset_fset:
```
```   681   shows "xs |\<subseteq>| ys \<longleftrightarrow> fset xs \<subseteq> fset ys"
```
```   682   by (descending) (simp)
```
```   683
```
```   684 lemma psubset_fset:
```
```   685   shows "xs |\<subset>| ys \<longleftrightarrow> fset xs \<subset> fset ys"
```
```   686   unfolding less_fset_def
```
```   687   by (descending) (auto)
```
```   688
```
```   689 lemma subset_insert_fset:
```
```   690   shows "(insert_fset x xs) |\<subseteq>| ys \<longleftrightarrow> x |\<in>| ys \<and> xs |\<subseteq>| ys"
```
```   691   by (descending) (simp)
```
```   692
```
```   693 lemma subset_in_fset:
```
```   694   shows "xs |\<subseteq>| ys = (\<forall>x. x |\<in>| xs \<longrightarrow> x |\<in>| ys)"
```
```   695   by (descending) (auto)
```
```   696
```
```   697 lemma subset_empty_fset:
```
```   698   shows "xs |\<subseteq>| {||} \<longleftrightarrow> xs = {||}"
```
```   699   by (descending) (simp)
```
```   700
```
```   701 lemma not_psubset_empty_fset:
```
```   702   shows "\<not> xs |\<subset>| {||}"
```
```   703   by (metis fset_simps(1) psubset_fset not_psubset_empty)
```
```   704
```
```   705
```
```   706 subsection {* map_fset *}
```
```   707
```
```   708 lemma map_fset_simps [simp]:
```
```   709    shows "map_fset f {||} = {||}"
```
```   710   and   "map_fset f (insert_fset x S) = insert_fset (f x) (map_fset f S)"
```
```   711   by (descending, simp)+
```
```   712
```
```   713 lemma map_fset_image [simp]:
```
```   714   shows "fset (map_fset f S) = f ` (fset S)"
```
```   715   by (descending) (simp)
```
```   716
```
```   717 lemma inj_map_fset_cong:
```
```   718   shows "inj f \<Longrightarrow> map_fset f S = map_fset f T \<longleftrightarrow> S = T"
```
```   719   by (descending) (metis inj_vimage_image_eq list_eq_def set_map)
```
```   720
```
```   721 lemma map_union_fset:
```
```   722   shows "map_fset f (S |\<union>| T) = map_fset f S |\<union>| map_fset f T"
```
```   723   by (descending) (simp)
```
```   724
```
```   725
```
```   726 subsection {* card_fset *}
```
```   727
```
```   728 lemma card_fset:
```
```   729   shows "card_fset xs = card (fset xs)"
```
```   730   by (descending) (simp)
```
```   731
```
```   732 lemma card_insert_fset_iff [simp]:
```
```   733   shows "card_fset (insert_fset x S) = (if x |\<in>| S then card_fset S else Suc (card_fset S))"
```
```   734   by (descending) (simp add: insert_absorb)
```
```   735
```
```   736 lemma card_fset_0[simp]:
```
```   737   shows "card_fset S = 0 \<longleftrightarrow> S = {||}"
```
```   738   by (descending) (simp)
```
```   739
```
```   740 lemma card_empty_fset[simp]:
```
```   741   shows "card_fset {||} = 0"
```
```   742   by (simp add: card_fset)
```
```   743
```
```   744 lemma card_fset_1:
```
```   745   shows "card_fset S = 1 \<longleftrightarrow> (\<exists>x. S = {|x|})"
```
```   746   by (descending) (auto simp add: card_Suc_eq)
```
```   747
```
```   748 lemma card_fset_gt_0:
```
```   749   shows "x \<in> fset S \<Longrightarrow> 0 < card_fset S"
```
```   750   by (descending) (auto simp add: card_gt_0_iff)
```
```   751
```
```   752 lemma card_notin_fset:
```
```   753   shows "(x |\<notin>| S) = (card_fset (insert_fset x S) = Suc (card_fset S))"
```
```   754   by simp
```
```   755
```
```   756 lemma card_fset_Suc:
```
```   757   shows "card_fset S = Suc n \<Longrightarrow> \<exists>x T. x |\<notin>| T \<and> S = insert_fset x T \<and> card_fset T = n"
```
```   758   apply(descending)
```
```   759   apply(auto dest!: card_eq_SucD)
```
```   760   by (metis Diff_insert_absorb set_removeAll)
```
```   761
```
```   762 lemma card_remove_fset_iff [simp]:
```
```   763   shows "card_fset (remove_fset y S) = (if y |\<in>| S then card_fset S - 1 else card_fset S)"
```
```   764   by (descending) (simp)
```
```   765
```
```   766 lemma card_Suc_exists_in_fset:
```
```   767   shows "card_fset S = Suc n \<Longrightarrow> \<exists>a. a |\<in>| S"
```
```   768   by (drule card_fset_Suc) (auto)
```
```   769
```
```   770 lemma in_card_fset_not_0:
```
```   771   shows "a |\<in>| A \<Longrightarrow> card_fset A \<noteq> 0"
```
```   772   by (descending) (auto)
```
```   773
```
```   774 lemma card_fset_mono:
```
```   775   shows "xs |\<subseteq>| ys \<Longrightarrow> card_fset xs \<le> card_fset ys"
```
```   776   unfolding card_fset psubset_fset
```
```   777   by (simp add: card_mono subset_fset)
```
```   778
```
```   779 lemma card_subset_fset_eq:
```
```   780   shows "xs |\<subseteq>| ys \<Longrightarrow> card_fset ys \<le> card_fset xs \<Longrightarrow> xs = ys"
```
```   781   unfolding card_fset subset_fset
```
```   782   by (auto dest: card_seteq[OF finite_fset] simp add: fset_cong)
```
```   783
```
```   784 lemma psubset_card_fset_mono:
```
```   785   shows "xs |\<subset>| ys \<Longrightarrow> card_fset xs < card_fset ys"
```
```   786   unfolding card_fset subset_fset
```
```   787   by (metis finite_fset psubset_fset psubset_card_mono)
```
```   788
```
```   789 lemma card_union_inter_fset:
```
```   790   shows "card_fset xs + card_fset ys = card_fset (xs |\<union>| ys) + card_fset (xs |\<inter>| ys)"
```
```   791   unfolding card_fset union_fset inter_fset
```
```   792   by (rule card_Un_Int[OF finite_fset finite_fset])
```
```   793
```
```   794 lemma card_union_disjoint_fset:
```
```   795   shows "xs |\<inter>| ys = {||} \<Longrightarrow> card_fset (xs |\<union>| ys) = card_fset xs + card_fset ys"
```
```   796   unfolding card_fset union_fset
```
```   797   apply (rule card_Un_disjoint[OF finite_fset finite_fset])
```
```   798   by (metis inter_fset fset_simps(1))
```
```   799
```
```   800 lemma card_remove_fset_less1:
```
```   801   shows "x |\<in>| xs \<Longrightarrow> card_fset (remove_fset x xs) < card_fset xs"
```
```   802   unfolding card_fset in_fset remove_fset
```
```   803   by (rule card_Diff1_less[OF finite_fset])
```
```   804
```
```   805 lemma card_remove_fset_less2:
```
```   806   shows "x |\<in>| xs \<Longrightarrow> y |\<in>| xs \<Longrightarrow> card_fset (remove_fset y (remove_fset x xs)) < card_fset xs"
```
```   807   unfolding card_fset remove_fset in_fset
```
```   808   by (rule card_Diff2_less[OF finite_fset])
```
```   809
```
```   810 lemma card_remove_fset_le1:
```
```   811   shows "card_fset (remove_fset x xs) \<le> card_fset xs"
```
```   812   unfolding remove_fset card_fset
```
```   813   by (rule card_Diff1_le[OF finite_fset])
```
```   814
```
```   815 lemma card_psubset_fset:
```
```   816   shows "ys |\<subseteq>| xs \<Longrightarrow> card_fset ys < card_fset xs \<Longrightarrow> ys |\<subset>| xs"
```
```   817   unfolding card_fset psubset_fset subset_fset
```
```   818   by (rule card_psubset[OF finite_fset])
```
```   819
```
```   820 lemma card_map_fset_le:
```
```   821   shows "card_fset (map_fset f xs) \<le> card_fset xs"
```
```   822   unfolding card_fset map_fset_image
```
```   823   by (rule card_image_le[OF finite_fset])
```
```   824
```
```   825 lemma card_minus_insert_fset[simp]:
```
```   826   assumes "a |\<in>| A" and "a |\<notin>| B"
```
```   827   shows "card_fset (A - insert_fset a B) = card_fset (A - B) - 1"
```
```   828   using assms
```
```   829   unfolding in_fset card_fset minus_fset
```
```   830   by (simp add: card_Diff_insert[OF finite_fset])
```
```   831
```
```   832 lemma card_minus_subset_fset:
```
```   833   assumes "B |\<subseteq>| A"
```
```   834   shows "card_fset (A - B) = card_fset A - card_fset B"
```
```   835   using assms
```
```   836   unfolding subset_fset card_fset minus_fset
```
```   837   by (rule card_Diff_subset[OF finite_fset])
```
```   838
```
```   839 lemma card_minus_fset:
```
```   840   shows "card_fset (A - B) = card_fset A - card_fset (A |\<inter>| B)"
```
```   841   unfolding inter_fset card_fset minus_fset
```
```   842   by (rule card_Diff_subset_Int) (simp)
```
```   843
```
```   844
```
```   845 subsection {* concat_fset *}
```
```   846
```
```   847 lemma concat_empty_fset [simp]:
```
```   848   shows "concat_fset {||} = {||}"
```
```   849   by descending simp
```
```   850
```
```   851 lemma concat_insert_fset [simp]:
```
```   852   shows "concat_fset (insert_fset x S) = x |\<union>| concat_fset S"
```
```   853   by descending simp
```
```   854
```
```   855 lemma concat_union_fset [simp]:
```
```   856   shows "concat_fset (xs |\<union>| ys) = concat_fset xs |\<union>| concat_fset ys"
```
```   857   by descending simp
```
```   858
```
```   859 lemma map_concat_fset:
```
```   860   shows "map_fset f (concat_fset xs) = concat_fset (map_fset (map_fset f) xs)"
```
```   861   by (lifting map_concat)
```
```   862
```
```   863 subsection {* filter_fset *}
```
```   864
```
```   865 lemma subset_filter_fset:
```
```   866   "filter_fset P xs |\<subseteq>| filter_fset Q xs = (\<forall> x. x |\<in>| xs \<longrightarrow> P x \<longrightarrow> Q x)"
```
```   867   by descending auto
```
```   868
```
```   869 lemma eq_filter_fset:
```
```   870   "(filter_fset P xs = filter_fset Q xs) = (\<forall>x. x |\<in>| xs \<longrightarrow> P x = Q x)"
```
```   871   by descending auto
```
```   872
```
```   873 lemma psubset_filter_fset:
```
```   874   "(\<And>x. x |\<in>| xs \<Longrightarrow> P x \<Longrightarrow> Q x) \<Longrightarrow> (x |\<in>| xs & \<not> P x & Q x) \<Longrightarrow>
```
```   875     filter_fset P xs |\<subset>| filter_fset Q xs"
```
```   876   unfolding less_fset_def by (auto simp add: subset_filter_fset eq_filter_fset)
```
```   877
```
```   878
```
```   879 subsection {* fold_fset *}
```
```   880
```
```   881 lemma fold_empty_fset:
```
```   882   "fold_fset f {||} = id"
```
```   883   by descending (simp add: fold_once_def)
```
```   884
```
```   885 lemma fold_insert_fset: "fold_fset f (insert_fset a A) =
```
```   886   (if rsp_fold f then if a |\<in>| A then fold_fset f A else fold_fset f A \<circ> f a else id)"
```
```   887   by descending (simp add: fold_once_fold_remdups)
```
```   888
```
```   889 lemma remdups_removeAll:
```
```   890   "remdups (removeAll x xs) = remove1 x (remdups xs)"
```
```   891   by (induct xs) auto
```
```   892
```
```   893 lemma member_commute_fold_once:
```
```   894   assumes "rsp_fold f"
```
```   895     and "x \<in> set xs"
```
```   896   shows "fold_once f xs = fold_once f (removeAll x xs) \<circ> f x"
```
```   897 proof -
```
```   898   from assms have "fold f (remdups xs) = fold f (remove1 x (remdups xs)) \<circ> f x"
```
```   899     by (auto intro!: fold_remove1_split elim: rsp_foldE)
```
```   900   then show ?thesis using `rsp_fold f` by (simp add: fold_once_fold_remdups remdups_removeAll)
```
```   901 qed
```
```   902
```
```   903 lemma in_commute_fold_fset:
```
```   904   "rsp_fold f \<Longrightarrow> h |\<in>| b \<Longrightarrow> fold_fset f b = fold_fset f (remove_fset h b) \<circ> f h"
```
```   905   by descending (simp add: member_commute_fold_once)
```
```   906
```
```   907
```
```   908 subsection {* Choice in fsets *}
```
```   909
```
```   910 lemma fset_choice:
```
```   911   assumes a: "\<forall>x. x |\<in>| A \<longrightarrow> (\<exists>y. P x y)"
```
```   912   shows "\<exists>f. \<forall>x. x |\<in>| A \<longrightarrow> P x (f x)"
```
```   913   using a
```
```   914   apply(descending)
```
```   915   using finite_set_choice
```
```   916   by (auto simp add: Ball_def)
```
```   917
```
```   918
```
```   919 section {* Induction and Cases rules for fsets *}
```
```   920
```
```   921 lemma fset_exhaust [case_names empty insert, cases type: fset]:
```
```   922   assumes empty_fset_case: "S = {||} \<Longrightarrow> P"
```
```   923   and     insert_fset_case: "\<And>x S'. S = insert_fset x S' \<Longrightarrow> P"
```
```   924   shows "P"
```
```   925   using assms by (lifting list.exhaust)
```
```   926
```
```   927 lemma fset_induct [case_names empty insert]:
```
```   928   assumes empty_fset_case: "P {||}"
```
```   929   and     insert_fset_case: "\<And>x S. P S \<Longrightarrow> P (insert_fset x S)"
```
```   930   shows "P S"
```
```   931   using assms
```
```   932   by (descending) (blast intro: list.induct)
```
```   933
```
```   934 lemma fset_induct_stronger [case_names empty insert, induct type: fset]:
```
```   935   assumes empty_fset_case: "P {||}"
```
```   936   and     insert_fset_case: "\<And>x S. \<lbrakk>x |\<notin>| S; P S\<rbrakk> \<Longrightarrow> P (insert_fset x S)"
```
```   937   shows "P S"
```
```   938 proof(induct S rule: fset_induct)
```
```   939   case empty
```
```   940   show "P {||}" using empty_fset_case by simp
```
```   941 next
```
```   942   case (insert x S)
```
```   943   have "P S" by fact
```
```   944   then show "P (insert_fset x S)" using insert_fset_case
```
```   945     by (cases "x |\<in>| S") (simp_all)
```
```   946 qed
```
```   947
```
```   948 lemma fset_card_induct:
```
```   949   assumes empty_fset_case: "P {||}"
```
```   950   and     card_fset_Suc_case: "\<And>S T. Suc (card_fset S) = (card_fset T) \<Longrightarrow> P S \<Longrightarrow> P T"
```
```   951   shows "P S"
```
```   952 proof (induct S)
```
```   953   case empty
```
```   954   show "P {||}" by (rule empty_fset_case)
```
```   955 next
```
```   956   case (insert x S)
```
```   957   have h: "P S" by fact
```
```   958   have "x |\<notin>| S" by fact
```
```   959   then have "Suc (card_fset S) = card_fset (insert_fset x S)"
```
```   960     using card_fset_Suc by auto
```
```   961   then show "P (insert_fset x S)"
```
```   962     using h card_fset_Suc_case by simp
```
```   963 qed
```
```   964
```
```   965 lemma fset_raw_strong_cases:
```
```   966   obtains "xs = []"
```
```   967     | ys x where "\<not> List.member ys x" and "xs \<approx> x # ys"
```
```   968 proof (induct xs)
```
```   969   case Nil
```
```   970   then show thesis by simp
```
```   971 next
```
```   972   case (Cons a xs)
```
```   973   have a: "\<lbrakk>xs = [] \<Longrightarrow> thesis; \<And>x ys. \<lbrakk>\<not> List.member ys x; xs \<approx> x # ys\<rbrakk> \<Longrightarrow> thesis\<rbrakk> \<Longrightarrow> thesis"
```
```   974     by (rule Cons(1))
```
```   975   have b: "\<And>x' ys'. \<lbrakk>\<not> List.member ys' x'; a # xs \<approx> x' # ys'\<rbrakk> \<Longrightarrow> thesis" by fact
```
```   976   have c: "xs = [] \<Longrightarrow> thesis" using b
```
```   977     apply(simp)
```
```   978     by (metis List.set.simps(1) emptyE empty_subsetI)
```
```   979   have "\<And>x ys. \<lbrakk>\<not> List.member ys x; xs \<approx> x # ys\<rbrakk> \<Longrightarrow> thesis"
```
```   980   proof -
```
```   981     fix x :: 'a
```
```   982     fix ys :: "'a list"
```
```   983     assume d:"\<not> List.member ys x"
```
```   984     assume e:"xs \<approx> x # ys"
```
```   985     show thesis
```
```   986     proof (cases "x = a")
```
```   987       assume h: "x = a"
```
```   988       then have f: "\<not> List.member ys a" using d by simp
```
```   989       have g: "a # xs \<approx> a # ys" using e h by auto
```
```   990       show thesis using b f g by simp
```
```   991     next
```
```   992       assume h: "x \<noteq> a"
```
```   993       then have f: "\<not> List.member (a # ys) x" using d by auto
```
```   994       have g: "a # xs \<approx> x # (a # ys)" using e h by auto
```
```   995       show thesis using b f g by (simp del: List.member_def)
```
```   996     qed
```
```   997   qed
```
```   998   then show thesis using a c by blast
```
```   999 qed
```
```  1000
```
```  1001
```
```  1002 lemma fset_strong_cases:
```
```  1003   obtains "xs = {||}"
```
```  1004     | ys x where "x |\<notin>| ys" and "xs = insert_fset x ys"
```
```  1005   by (lifting fset_raw_strong_cases)
```
```  1006
```
```  1007
```
```  1008 lemma fset_induct2:
```
```  1009   "P {||} {||} \<Longrightarrow>
```
```  1010   (\<And>x xs. x |\<notin>| xs \<Longrightarrow> P (insert_fset x xs) {||}) \<Longrightarrow>
```
```  1011   (\<And>y ys. y |\<notin>| ys \<Longrightarrow> P {||} (insert_fset y ys)) \<Longrightarrow>
```
```  1012   (\<And>x xs y ys. \<lbrakk>P xs ys; x |\<notin>| xs; y |\<notin>| ys\<rbrakk> \<Longrightarrow> P (insert_fset x xs) (insert_fset y ys)) \<Longrightarrow>
```
```  1013   P xsa ysa"
```
```  1014   apply (induct xsa arbitrary: ysa)
```
```  1015   apply (induct_tac x rule: fset_induct_stronger)
```
```  1016   apply simp_all
```
```  1017   apply (induct_tac xa rule: fset_induct_stronger)
```
```  1018   apply simp_all
```
```  1019   done
```
```  1020
```
```  1021 text {* Extensionality *}
```
```  1022
```
```  1023 lemma fset_eqI:
```
```  1024   assumes "\<And>x. x \<in> fset A \<longleftrightarrow> x \<in> fset B"
```
```  1025   shows "A = B"
```
```  1026 using assms proof (induct A arbitrary: B)
```
```  1027   case empty then show ?case
```
```  1028     by (auto simp add: in_fset none_in_empty_fset [symmetric] sym)
```
```  1029 next
```
```  1030   case (insert x A)
```
```  1031   from insert.prems insert.hyps(1) have "\<And>z. z \<in> fset A \<longleftrightarrow> z \<in> fset (B - {|x|})"
```
```  1032     by (auto simp add: in_fset)
```
```  1033   then have "A = B - {|x|}" by (rule insert.hyps(2))
```
```  1034   moreover with insert.prems [symmetric, of x] have "x |\<in>| B" by (simp add: in_fset)
```
```  1035   ultimately show ?case by (metis in_fset_mdef)
```
```  1036 qed
```
```  1037
```
```  1038 subsection {* alternate formulation with a different decomposition principle
```
```  1039   and a proof of equivalence *}
```
```  1040
```
```  1041 inductive
```
```  1042   list_eq2 :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool" ("_ \<approx>2 _")
```
```  1043 where
```
```  1044   "(a # b # xs) \<approx>2 (b # a # xs)"
```
```  1045 | "[] \<approx>2 []"
```
```  1046 | "xs \<approx>2 ys \<Longrightarrow> ys \<approx>2 xs"
```
```  1047 | "(a # a # xs) \<approx>2 (a # xs)"
```
```  1048 | "xs \<approx>2 ys \<Longrightarrow> (a # xs) \<approx>2 (a # ys)"
```
```  1049 | "xs1 \<approx>2 xs2 \<Longrightarrow> xs2 \<approx>2 xs3 \<Longrightarrow> xs1 \<approx>2 xs3"
```
```  1050
```
```  1051 lemma list_eq2_refl:
```
```  1052   shows "xs \<approx>2 xs"
```
```  1053   by (induct xs) (auto intro: list_eq2.intros)
```
```  1054
```
```  1055 lemma cons_delete_list_eq2:
```
```  1056   shows "(a # (removeAll a A)) \<approx>2 (if List.member A a then A else a # A)"
```
```  1057   apply (induct A)
```
```  1058   apply (simp add: list_eq2_refl)
```
```  1059   apply (case_tac "List.member (aa # A) a")
```
```  1060   apply (simp_all)
```
```  1061   apply (case_tac [!] "a = aa")
```
```  1062   apply (simp_all)
```
```  1063   apply (case_tac "List.member A a")
```
```  1064   apply (auto)[2]
```
```  1065   apply (metis list_eq2.intros(3) list_eq2.intros(4) list_eq2.intros(5) list_eq2.intros(6))
```
```  1066   apply (metis list_eq2.intros(1) list_eq2.intros(5) list_eq2.intros(6))
```
```  1067   apply (auto simp add: list_eq2_refl)
```
```  1068   done
```
```  1069
```
```  1070 lemma member_delete_list_eq2:
```
```  1071   assumes a: "List.member r e"
```
```  1072   shows "(e # removeAll e r) \<approx>2 r"
```
```  1073   using a cons_delete_list_eq2[of e r]
```
```  1074   by simp
```
```  1075
```
```  1076 lemma list_eq2_equiv:
```
```  1077   "(l \<approx> r) \<longleftrightarrow> (list_eq2 l r)"
```
```  1078 proof
```
```  1079   show "list_eq2 l r \<Longrightarrow> l \<approx> r" by (induct rule: list_eq2.induct) auto
```
```  1080 next
```
```  1081   {
```
```  1082     fix n
```
```  1083     assume a: "card_list l = n" and b: "l \<approx> r"
```
```  1084     have "l \<approx>2 r"
```
```  1085       using a b
```
```  1086     proof (induct n arbitrary: l r)
```
```  1087       case 0
```
```  1088       have "card_list l = 0" by fact
```
```  1089       then have "\<forall>x. \<not> List.member l x" by auto
```
```  1090       then have z: "l = []" by auto
```
```  1091       then have "r = []" using `l \<approx> r` by simp
```
```  1092       then show ?case using z list_eq2_refl by simp
```
```  1093     next
```
```  1094       case (Suc m)
```
```  1095       have b: "l \<approx> r" by fact
```
```  1096       have d: "card_list l = Suc m" by fact
```
```  1097       then have "\<exists>a. List.member l a"
```
```  1098         apply(simp)
```
```  1099         apply(drule card_eq_SucD)
```
```  1100         apply(blast)
```
```  1101         done
```
```  1102       then obtain a where e: "List.member l a" by auto
```
```  1103       then have e': "List.member r a" using list_eq_def [simplified List.member_def [symmetric], of l r] b
```
```  1104         by auto
```
```  1105       have f: "card_list (removeAll a l) = m" using e d by (simp)
```
```  1106       have g: "removeAll a l \<approx> removeAll a r" using remove_fset.rsp b by simp
```
```  1107       have "(removeAll a l) \<approx>2 (removeAll a r)" by (rule Suc.hyps[OF f g])
```
```  1108       then have h: "(a # removeAll a l) \<approx>2 (a # removeAll a r)" by (rule list_eq2.intros(5))
```
```  1109       have i: "l \<approx>2 (a # removeAll a l)"
```
```  1110         by (rule list_eq2.intros(3)[OF member_delete_list_eq2[OF e]])
```
```  1111       have "l \<approx>2 (a # removeAll a r)" by (rule list_eq2.intros(6)[OF i h])
```
```  1112       then show ?case using list_eq2.intros(6)[OF _ member_delete_list_eq2[OF e']] by simp
```
```  1113     qed
```
```  1114     }
```
```  1115   then show "l \<approx> r \<Longrightarrow> l \<approx>2 r" by blast
```
```  1116 qed
```
```  1117
```
```  1118
```
```  1119 (* We cannot write it as "assumes .. shows" since Isabelle changes
```
```  1120    the quantifiers to schematic variables and reintroduces them in
```
```  1121    a different order *)
```
```  1122 lemma fset_eq_cases:
```
```  1123  "\<lbrakk>a1 = a2;
```
```  1124    \<And>a b xs. \<lbrakk>a1 = insert_fset a (insert_fset b xs); a2 = insert_fset b (insert_fset a xs)\<rbrakk> \<Longrightarrow> P;
```
```  1125    \<lbrakk>a1 = {||}; a2 = {||}\<rbrakk> \<Longrightarrow> P; \<And>xs ys. \<lbrakk>a1 = ys; a2 = xs; xs = ys\<rbrakk> \<Longrightarrow> P;
```
```  1126    \<And>a xs. \<lbrakk>a1 = insert_fset a (insert_fset a xs); a2 = insert_fset a xs\<rbrakk> \<Longrightarrow> P;
```
```  1127    \<And>xs ys a. \<lbrakk>a1 = insert_fset a xs; a2 = insert_fset a ys; xs = ys\<rbrakk> \<Longrightarrow> P;
```
```  1128    \<And>xs1 xs2 xs3. \<lbrakk>a1 = xs1; a2 = xs3; xs1 = xs2; xs2 = xs3\<rbrakk> \<Longrightarrow> P\<rbrakk>
```
```  1129   \<Longrightarrow> P"
```
```  1130   by (lifting list_eq2.cases[simplified list_eq2_equiv[symmetric]])
```
```  1131
```
```  1132 lemma fset_eq_induct:
```
```  1133   assumes "x1 = x2"
```
```  1134   and "\<And>a b xs. P (insert_fset a (insert_fset b xs)) (insert_fset b (insert_fset a xs))"
```
```  1135   and "P {||} {||}"
```
```  1136   and "\<And>xs ys. \<lbrakk>xs = ys; P xs ys\<rbrakk> \<Longrightarrow> P ys xs"
```
```  1137   and "\<And>a xs. P (insert_fset a (insert_fset a xs)) (insert_fset a xs)"
```
```  1138   and "\<And>xs ys a. \<lbrakk>xs = ys; P xs ys\<rbrakk> \<Longrightarrow> P (insert_fset a xs) (insert_fset a ys)"
```
```  1139   and "\<And>xs1 xs2 xs3. \<lbrakk>xs1 = xs2; P xs1 xs2; xs2 = xs3; P xs2 xs3\<rbrakk> \<Longrightarrow> P xs1 xs3"
```
```  1140   shows "P x1 x2"
```
```  1141   using assms
```
```  1142   by (lifting list_eq2.induct[simplified list_eq2_equiv[symmetric]])
```
```  1143
```
```  1144 ML {*
```
```  1145 fun dest_fsetT (Type (@{type_name fset}, [T])) = T
```
```  1146   | dest_fsetT T = raise TYPE ("dest_fsetT: fset type expected", [T], []);
```
```  1147 *}
```
```  1148
```
```  1149 no_notation
```
```  1150   list_eq (infix "\<approx>" 50) and
```
```  1151   list_eq2 (infix "\<approx>2" 50)
```
```  1152
```
```  1153 end
```