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