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