src/HOL/Set.thy
author wenzelm
Sun Nov 26 21:08:32 2017 +0100 (16 months ago)
changeset 67091 1393c2340eec
parent 67051 e7e54a0b9197
child 67268 bdf25939a550
permissions -rw-r--r--
more symbols;
     1 (*  Title:      HOL/Set.thy
     2     Author:     Tobias Nipkow
     3     Author:     Lawrence C Paulson
     4     Author:     Markus Wenzel
     5 *)
     6 
     7 section \<open>Set theory for higher-order logic\<close>
     8 
     9 theory Set
    10   imports Lattices
    11 begin
    12 
    13 subsection \<open>Sets as predicates\<close>
    14 
    15 typedecl 'a set
    16 
    17 axiomatization Collect :: "('a \<Rightarrow> bool) \<Rightarrow> 'a set" \<comment> "comprehension"
    18   and member :: "'a \<Rightarrow> 'a set \<Rightarrow> bool" \<comment> "membership"
    19   where mem_Collect_eq [iff, code_unfold]: "member a (Collect P) = P a"
    20     and Collect_mem_eq [simp]: "Collect (\<lambda>x. member x A) = A"
    21 
    22 notation
    23   member  ("op \<in>") and
    24   member  ("(_/ \<in> _)" [51, 51] 50)
    25 
    26 abbreviation not_member
    27   where "not_member x A \<equiv> \<not> (x \<in> A)" \<comment> "non-membership"
    28 notation
    29   not_member  ("op \<notin>") and
    30   not_member  ("(_/ \<notin> _)" [51, 51] 50)
    31 
    32 notation (ASCII)
    33   member  ("op :") and
    34   member  ("(_/ : _)" [51, 51] 50) and
    35   not_member  ("op ~:") and
    36   not_member  ("(_/ ~: _)" [51, 51] 50)
    37 
    38 
    39 text \<open>Set comprehensions\<close>
    40 
    41 syntax
    42   "_Coll" :: "pttrn \<Rightarrow> bool \<Rightarrow> 'a set"    ("(1{_./ _})")
    43 translations
    44   "{x. P}" \<rightleftharpoons> "CONST Collect (\<lambda>x. P)"
    45 
    46 syntax (ASCII)
    47   "_Collect" :: "pttrn \<Rightarrow> 'a set \<Rightarrow> bool \<Rightarrow> 'a set"  ("(1{_ :/ _./ _})")
    48 syntax
    49   "_Collect" :: "pttrn \<Rightarrow> 'a set \<Rightarrow> bool \<Rightarrow> 'a set"  ("(1{_ \<in>/ _./ _})")
    50 translations
    51   "{p:A. P}" \<rightharpoonup> "CONST Collect (\<lambda>p. p \<in> A \<and> P)"
    52 
    53 lemma CollectI: "P a \<Longrightarrow> a \<in> {x. P x}"
    54   by simp
    55 
    56 lemma CollectD: "a \<in> {x. P x} \<Longrightarrow> P a"
    57   by simp
    58 
    59 lemma Collect_cong: "(\<And>x. P x = Q x) \<Longrightarrow> {x. P x} = {x. Q x}"
    60   by simp
    61 
    62 text \<open>
    63   Simproc for pulling \<open>x = t\<close> in \<open>{x. \<dots> \<and> x = t \<and> \<dots>}\<close>
    64   to the front (and similarly for \<open>t = x\<close>):
    65 \<close>
    66 
    67 simproc_setup defined_Collect ("{x. P x \<and> Q x}") = \<open>
    68   fn _ => Quantifier1.rearrange_Collect
    69     (fn ctxt =>
    70       resolve_tac ctxt @{thms Collect_cong} 1 THEN
    71       resolve_tac ctxt @{thms iffI} 1 THEN
    72       ALLGOALS
    73         (EVERY' [REPEAT_DETERM o eresolve_tac ctxt @{thms conjE},
    74           DEPTH_SOLVE_1 o (assume_tac ctxt ORELSE' resolve_tac ctxt @{thms conjI})]))
    75 \<close>
    76 
    77 lemmas CollectE = CollectD [elim_format]
    78 
    79 lemma set_eqI:
    80   assumes "\<And>x. x \<in> A \<longleftrightarrow> x \<in> B"
    81   shows "A = B"
    82 proof -
    83   from assms have "{x. x \<in> A} = {x. x \<in> B}"
    84     by simp
    85   then show ?thesis by simp
    86 qed
    87 
    88 lemma set_eq_iff: "A = B \<longleftrightarrow> (\<forall>x. x \<in> A \<longleftrightarrow> x \<in> B)"
    89   by (auto intro:set_eqI)
    90 
    91 lemma Collect_eqI:
    92   assumes "\<And>x. P x = Q x"
    93   shows "Collect P = Collect Q"
    94   using assms by (auto intro: set_eqI)
    95 
    96 text \<open>Lifting of predicate class instances\<close>
    97 
    98 instantiation set :: (type) boolean_algebra
    99 begin
   100 
   101 definition less_eq_set
   102   where "A \<le> B \<longleftrightarrow> (\<lambda>x. member x A) \<le> (\<lambda>x. member x B)"
   103 
   104 definition less_set
   105   where "A < B \<longleftrightarrow> (\<lambda>x. member x A) < (\<lambda>x. member x B)"
   106 
   107 definition inf_set
   108   where "A \<sqinter> B = Collect ((\<lambda>x. member x A) \<sqinter> (\<lambda>x. member x B))"
   109 
   110 definition sup_set
   111   where "A \<squnion> B = Collect ((\<lambda>x. member x A) \<squnion> (\<lambda>x. member x B))"
   112 
   113 definition bot_set
   114   where "\<bottom> = Collect \<bottom>"
   115 
   116 definition top_set
   117   where "\<top> = Collect \<top>"
   118 
   119 definition uminus_set
   120   where "- A = Collect (- (\<lambda>x. member x A))"
   121 
   122 definition minus_set
   123   where "A - B = Collect ((\<lambda>x. member x A) - (\<lambda>x. member x B))"
   124 
   125 instance
   126   by standard
   127     (simp_all add: less_eq_set_def less_set_def inf_set_def sup_set_def
   128       bot_set_def top_set_def uminus_set_def minus_set_def
   129       less_le_not_le sup_inf_distrib1 diff_eq set_eqI fun_eq_iff
   130       del: inf_apply sup_apply bot_apply top_apply minus_apply uminus_apply)
   131 
   132 end
   133 
   134 text \<open>Set enumerations\<close>
   135 
   136 abbreviation empty :: "'a set" ("{}")
   137   where "{} \<equiv> bot"
   138 
   139 definition insert :: "'a \<Rightarrow> 'a set \<Rightarrow> 'a set"
   140   where insert_compr: "insert a B = {x. x = a \<or> x \<in> B}"
   141 
   142 syntax
   143   "_Finset" :: "args \<Rightarrow> 'a set"    ("{(_)}")
   144 translations
   145   "{x, xs}" \<rightleftharpoons> "CONST insert x {xs}"
   146   "{x}" \<rightleftharpoons> "CONST insert x {}"
   147 
   148 
   149 subsection \<open>Subsets and bounded quantifiers\<close>
   150 
   151 abbreviation subset :: "'a set \<Rightarrow> 'a set \<Rightarrow> bool"
   152   where "subset \<equiv> less"
   153 
   154 abbreviation subset_eq :: "'a set \<Rightarrow> 'a set \<Rightarrow> bool"
   155   where "subset_eq \<equiv> less_eq"
   156 
   157 notation
   158   subset  ("op \<subset>") and
   159   subset  ("(_/ \<subset> _)" [51, 51] 50) and
   160   subset_eq  ("op \<subseteq>") and
   161   subset_eq  ("(_/ \<subseteq> _)" [51, 51] 50)
   162 
   163 abbreviation (input)
   164   supset :: "'a set \<Rightarrow> 'a set \<Rightarrow> bool" where
   165   "supset \<equiv> greater"
   166 
   167 abbreviation (input)
   168   supset_eq :: "'a set \<Rightarrow> 'a set \<Rightarrow> bool" where
   169   "supset_eq \<equiv> greater_eq"
   170 
   171 notation
   172   supset  ("op \<supset>") and
   173   supset  ("(_/ \<supset> _)" [51, 51] 50) and
   174   supset_eq  ("op \<supseteq>") and
   175   supset_eq  ("(_/ \<supseteq> _)" [51, 51] 50)
   176 
   177 notation (ASCII output)
   178   subset  ("op <") and
   179   subset  ("(_/ < _)" [51, 51] 50) and
   180   subset_eq  ("op <=") and
   181   subset_eq  ("(_/ <= _)" [51, 51] 50)
   182 
   183 definition Ball :: "'a set \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> bool"
   184   where "Ball A P \<longleftrightarrow> (\<forall>x. x \<in> A \<longrightarrow> P x)"   \<comment> "bounded universal quantifiers"
   185 
   186 definition Bex :: "'a set \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> bool"
   187   where "Bex A P \<longleftrightarrow> (\<exists>x. x \<in> A \<and> P x)"   \<comment> "bounded existential quantifiers"
   188 
   189 syntax (ASCII)
   190   "_Ball"       :: "pttrn \<Rightarrow> 'a set \<Rightarrow> bool \<Rightarrow> bool"      ("(3ALL _:_./ _)" [0, 0, 10] 10)
   191   "_Bex"        :: "pttrn \<Rightarrow> 'a set \<Rightarrow> bool \<Rightarrow> bool"      ("(3EX _:_./ _)" [0, 0, 10] 10)
   192   "_Bex1"       :: "pttrn \<Rightarrow> 'a set \<Rightarrow> bool \<Rightarrow> bool"      ("(3EX! _:_./ _)" [0, 0, 10] 10)
   193   "_Bleast"     :: "id \<Rightarrow> 'a set \<Rightarrow> bool \<Rightarrow> 'a"           ("(3LEAST _:_./ _)" [0, 0, 10] 10)
   194 
   195 syntax (input)
   196   "_Ball"       :: "pttrn \<Rightarrow> 'a set \<Rightarrow> bool \<Rightarrow> bool"      ("(3! _:_./ _)" [0, 0, 10] 10)
   197   "_Bex"        :: "pttrn \<Rightarrow> 'a set \<Rightarrow> bool \<Rightarrow> bool"      ("(3? _:_./ _)" [0, 0, 10] 10)
   198   "_Bex1"       :: "pttrn \<Rightarrow> 'a set \<Rightarrow> bool \<Rightarrow> bool"      ("(3?! _:_./ _)" [0, 0, 10] 10)
   199 
   200 syntax
   201   "_Ball"       :: "pttrn \<Rightarrow> 'a set \<Rightarrow> bool \<Rightarrow> bool"      ("(3\<forall>_\<in>_./ _)" [0, 0, 10] 10)
   202   "_Bex"        :: "pttrn \<Rightarrow> 'a set \<Rightarrow> bool \<Rightarrow> bool"      ("(3\<exists>_\<in>_./ _)" [0, 0, 10] 10)
   203   "_Bex1"       :: "pttrn \<Rightarrow> 'a set \<Rightarrow> bool \<Rightarrow> bool"      ("(3\<exists>!_\<in>_./ _)" [0, 0, 10] 10)
   204   "_Bleast"     :: "id \<Rightarrow> 'a set \<Rightarrow> bool \<Rightarrow> 'a"           ("(3LEAST_\<in>_./ _)" [0, 0, 10] 10)
   205 
   206 translations
   207   "\<forall>x\<in>A. P" \<rightleftharpoons> "CONST Ball A (\<lambda>x. P)"
   208   "\<exists>x\<in>A. P" \<rightleftharpoons> "CONST Bex A (\<lambda>x. P)"
   209   "\<exists>!x\<in>A. P" \<rightharpoonup> "\<exists>!x. x \<in> A \<and> P"
   210   "LEAST x:A. P" \<rightharpoonup> "LEAST x. x \<in> A \<and> P"
   211 
   212 syntax (ASCII output)
   213   "_setlessAll" :: "[idt, 'a, bool] \<Rightarrow> bool"  ("(3ALL _<_./ _)"  [0, 0, 10] 10)
   214   "_setlessEx"  :: "[idt, 'a, bool] \<Rightarrow> bool"  ("(3EX _<_./ _)"  [0, 0, 10] 10)
   215   "_setleAll"   :: "[idt, 'a, bool] \<Rightarrow> bool"  ("(3ALL _<=_./ _)" [0, 0, 10] 10)
   216   "_setleEx"    :: "[idt, 'a, bool] \<Rightarrow> bool"  ("(3EX _<=_./ _)" [0, 0, 10] 10)
   217   "_setleEx1"   :: "[idt, 'a, bool] \<Rightarrow> bool"  ("(3EX! _<=_./ _)" [0, 0, 10] 10)
   218 
   219 syntax
   220   "_setlessAll" :: "[idt, 'a, bool] \<Rightarrow> bool"   ("(3\<forall>_\<subset>_./ _)"  [0, 0, 10] 10)
   221   "_setlessEx"  :: "[idt, 'a, bool] \<Rightarrow> bool"   ("(3\<exists>_\<subset>_./ _)"  [0, 0, 10] 10)
   222   "_setleAll"   :: "[idt, 'a, bool] \<Rightarrow> bool"   ("(3\<forall>_\<subseteq>_./ _)" [0, 0, 10] 10)
   223   "_setleEx"    :: "[idt, 'a, bool] \<Rightarrow> bool"   ("(3\<exists>_\<subseteq>_./ _)" [0, 0, 10] 10)
   224   "_setleEx1"   :: "[idt, 'a, bool] \<Rightarrow> bool"   ("(3\<exists>!_\<subseteq>_./ _)" [0, 0, 10] 10)
   225 
   226 translations
   227  "\<forall>A\<subset>B. P" \<rightharpoonup> "\<forall>A. A \<subset> B \<longrightarrow> P"
   228  "\<exists>A\<subset>B. P" \<rightharpoonup> "\<exists>A. A \<subset> B \<and> P"
   229  "\<forall>A\<subseteq>B. P" \<rightharpoonup> "\<forall>A. A \<subseteq> B \<longrightarrow> P"
   230  "\<exists>A\<subseteq>B. P" \<rightharpoonup> "\<exists>A. A \<subseteq> B \<and> P"
   231  "\<exists>!A\<subseteq>B. P" \<rightharpoonup> "\<exists>!A. A \<subseteq> B \<and> P"
   232 
   233 print_translation \<open>
   234   let
   235     val All_binder = Mixfix.binder_name @{const_syntax All};
   236     val Ex_binder = Mixfix.binder_name @{const_syntax Ex};
   237     val impl = @{const_syntax HOL.implies};
   238     val conj = @{const_syntax HOL.conj};
   239     val sbset = @{const_syntax subset};
   240     val sbset_eq = @{const_syntax subset_eq};
   241 
   242     val trans =
   243      [((All_binder, impl, sbset), @{syntax_const "_setlessAll"}),
   244       ((All_binder, impl, sbset_eq), @{syntax_const "_setleAll"}),
   245       ((Ex_binder, conj, sbset), @{syntax_const "_setlessEx"}),
   246       ((Ex_binder, conj, sbset_eq), @{syntax_const "_setleEx"})];
   247 
   248     fun mk v (v', T) c n P =
   249       if v = v' andalso not (Term.exists_subterm (fn Free (x, _) => x = v | _ => false) n)
   250       then Syntax.const c $ Syntax_Trans.mark_bound_body (v', T) $ n $ P
   251       else raise Match;
   252 
   253     fun tr' q = (q, fn _ =>
   254       (fn [Const (@{syntax_const "_bound"}, _) $ Free (v, Type (@{type_name set}, _)),
   255           Const (c, _) $
   256             (Const (d, _) $ (Const (@{syntax_const "_bound"}, _) $ Free (v', T)) $ n) $ P] =>
   257           (case AList.lookup (op =) trans (q, c, d) of
   258             NONE => raise Match
   259           | SOME l => mk v (v', T) l n P)
   260         | _ => raise Match));
   261   in
   262     [tr' All_binder, tr' Ex_binder]
   263   end
   264 \<close>
   265 
   266 
   267 text \<open>
   268   \<^medskip>
   269   Translate between \<open>{e | x1\<dots>xn. P}\<close> and \<open>{u. \<exists>x1\<dots>xn. u = e \<and> P}\<close>;
   270   \<open>{y. \<exists>x1\<dots>xn. y = e \<and> P}\<close> is only translated if \<open>[0..n] \<subseteq> bvs e\<close>.
   271 \<close>
   272 
   273 syntax
   274   "_Setcompr" :: "'a \<Rightarrow> idts \<Rightarrow> bool \<Rightarrow> 'a set"    ("(1{_ |/_./ _})")
   275 
   276 parse_translation \<open>
   277   let
   278     val ex_tr = snd (Syntax_Trans.mk_binder_tr ("EX ", @{const_syntax Ex}));
   279 
   280     fun nvars (Const (@{syntax_const "_idts"}, _) $ _ $ idts) = nvars idts + 1
   281       | nvars _ = 1;
   282 
   283     fun setcompr_tr ctxt [e, idts, b] =
   284       let
   285         val eq = Syntax.const @{const_syntax HOL.eq} $ Bound (nvars idts) $ e;
   286         val P = Syntax.const @{const_syntax HOL.conj} $ eq $ b;
   287         val exP = ex_tr ctxt [idts, P];
   288       in Syntax.const @{const_syntax Collect} $ absdummy dummyT exP end;
   289 
   290   in [(@{syntax_const "_Setcompr"}, setcompr_tr)] end;
   291 \<close>
   292 
   293 print_translation \<open>
   294  [Syntax_Trans.preserve_binder_abs2_tr' @{const_syntax Ball} @{syntax_const "_Ball"},
   295   Syntax_Trans.preserve_binder_abs2_tr' @{const_syntax Bex} @{syntax_const "_Bex"}]
   296 \<close> \<comment> \<open>to avoid eta-contraction of body\<close>
   297 
   298 print_translation \<open>
   299 let
   300   val ex_tr' = snd (Syntax_Trans.mk_binder_tr' (@{const_syntax Ex}, "DUMMY"));
   301 
   302   fun setcompr_tr' ctxt [Abs (abs as (_, _, P))] =
   303     let
   304       fun check (Const (@{const_syntax Ex}, _) $ Abs (_, _, P), n) = check (P, n + 1)
   305         | check (Const (@{const_syntax HOL.conj}, _) $
   306               (Const (@{const_syntax HOL.eq}, _) $ Bound m $ e) $ P, n) =
   307             n > 0 andalso m = n andalso not (loose_bvar1 (P, n)) andalso
   308             subset (op =) (0 upto (n - 1), add_loose_bnos (e, 0, []))
   309         | check _ = false;
   310 
   311         fun tr' (_ $ abs) =
   312           let val _ $ idts $ (_ $ (_ $ _ $ e) $ Q) = ex_tr' ctxt [abs]
   313           in Syntax.const @{syntax_const "_Setcompr"} $ e $ idts $ Q end;
   314     in
   315       if check (P, 0) then tr' P
   316       else
   317         let
   318           val (x as _ $ Free(xN, _), t) = Syntax_Trans.atomic_abs_tr' abs;
   319           val M = Syntax.const @{syntax_const "_Coll"} $ x $ t;
   320         in
   321           case t of
   322             Const (@{const_syntax HOL.conj}, _) $
   323               (Const (@{const_syntax Set.member}, _) $
   324                 (Const (@{syntax_const "_bound"}, _) $ Free (yN, _)) $ A) $ P =>
   325             if xN = yN then Syntax.const @{syntax_const "_Collect"} $ x $ A $ P else M
   326           | _ => M
   327         end
   328     end;
   329   in [(@{const_syntax Collect}, setcompr_tr')] end;
   330 \<close>
   331 
   332 simproc_setup defined_Bex ("\<exists>x\<in>A. P x \<and> Q x") = \<open>
   333   fn _ => Quantifier1.rearrange_bex
   334     (fn ctxt =>
   335       unfold_tac ctxt @{thms Bex_def} THEN
   336       Quantifier1.prove_one_point_ex_tac ctxt)
   337 \<close>
   338 
   339 simproc_setup defined_All ("\<forall>x\<in>A. P x \<longrightarrow> Q x") = \<open>
   340   fn _ => Quantifier1.rearrange_ball
   341     (fn ctxt =>
   342       unfold_tac ctxt @{thms Ball_def} THEN
   343       Quantifier1.prove_one_point_all_tac ctxt)
   344 \<close>
   345 
   346 lemma ballI [intro!]: "(\<And>x. x \<in> A \<Longrightarrow> P x) \<Longrightarrow> \<forall>x\<in>A. P x"
   347   by (simp add: Ball_def)
   348 
   349 lemmas strip = impI allI ballI
   350 
   351 lemma bspec [dest?]: "\<forall>x\<in>A. P x \<Longrightarrow> x \<in> A \<Longrightarrow> P x"
   352   by (simp add: Ball_def)
   353 
   354 text \<open>Gives better instantiation for bound:\<close>
   355 setup \<open>
   356   map_theory_claset (fn ctxt =>
   357     ctxt addbefore ("bspec", fn ctxt' => dresolve_tac ctxt' @{thms bspec} THEN' assume_tac ctxt'))
   358 \<close>
   359 
   360 ML \<open>
   361 structure Simpdata =
   362 struct
   363   open Simpdata;
   364   val mksimps_pairs = [(@{const_name Ball}, @{thms bspec})] @ mksimps_pairs;
   365 end;
   366 
   367 open Simpdata;
   368 \<close>
   369 
   370 declaration \<open>fn _ => Simplifier.map_ss (Simplifier.set_mksimps (mksimps mksimps_pairs))\<close>
   371 
   372 lemma ballE [elim]: "\<forall>x\<in>A. P x \<Longrightarrow> (P x \<Longrightarrow> Q) \<Longrightarrow> (x \<notin> A \<Longrightarrow> Q) \<Longrightarrow> Q"
   373   unfolding Ball_def by blast
   374 
   375 lemma bexI [intro]: "P x \<Longrightarrow> x \<in> A \<Longrightarrow> \<exists>x\<in>A. P x"
   376   \<comment> \<open>Normally the best argument order: \<open>P x\<close> constrains the choice of \<open>x \<in> A\<close>.\<close>
   377   unfolding Bex_def by blast
   378 
   379 lemma rev_bexI [intro?]: "x \<in> A \<Longrightarrow> P x \<Longrightarrow> \<exists>x\<in>A. P x"
   380   \<comment> \<open>The best argument order when there is only one \<open>x \<in> A\<close>.\<close>
   381   unfolding Bex_def by blast
   382 
   383 lemma bexCI: "(\<forall>x\<in>A. \<not> P x \<Longrightarrow> P a) \<Longrightarrow> a \<in> A \<Longrightarrow> \<exists>x\<in>A. P x"
   384   unfolding Bex_def by blast
   385 
   386 lemma bexE [elim!]: "\<exists>x\<in>A. P x \<Longrightarrow> (\<And>x. x \<in> A \<Longrightarrow> P x \<Longrightarrow> Q) \<Longrightarrow> Q"
   387   unfolding Bex_def by blast
   388 
   389 lemma ball_triv [simp]: "(\<forall>x\<in>A. P) \<longleftrightarrow> ((\<exists>x. x \<in> A) \<longrightarrow> P)"
   390   \<comment> \<open>Trival rewrite rule.\<close>
   391   by (simp add: Ball_def)
   392 
   393 lemma bex_triv [simp]: "(\<exists>x\<in>A. P) \<longleftrightarrow> ((\<exists>x. x \<in> A) \<and> P)"
   394   \<comment> \<open>Dual form for existentials.\<close>
   395   by (simp add: Bex_def)
   396 
   397 lemma bex_triv_one_point1 [simp]: "(\<exists>x\<in>A. x = a) \<longleftrightarrow> a \<in> A"
   398   by blast
   399 
   400 lemma bex_triv_one_point2 [simp]: "(\<exists>x\<in>A. a = x) \<longleftrightarrow> a \<in> A"
   401   by blast
   402 
   403 lemma bex_one_point1 [simp]: "(\<exists>x\<in>A. x = a \<and> P x) \<longleftrightarrow> a \<in> A \<and> P a"
   404   by blast
   405 
   406 lemma bex_one_point2 [simp]: "(\<exists>x\<in>A. a = x \<and> P x) \<longleftrightarrow> a \<in> A \<and> P a"
   407   by blast
   408 
   409 lemma ball_one_point1 [simp]: "(\<forall>x\<in>A. x = a \<longrightarrow> P x) \<longleftrightarrow> (a \<in> A \<longrightarrow> P a)"
   410   by blast
   411 
   412 lemma ball_one_point2 [simp]: "(\<forall>x\<in>A. a = x \<longrightarrow> P x) \<longleftrightarrow> (a \<in> A \<longrightarrow> P a)"
   413   by blast
   414 
   415 lemma ball_conj_distrib: "(\<forall>x\<in>A. P x \<and> Q x) \<longleftrightarrow> (\<forall>x\<in>A. P x) \<and> (\<forall>x\<in>A. Q x)"
   416   by blast
   417 
   418 lemma bex_disj_distrib: "(\<exists>x\<in>A. P x \<or> Q x) \<longleftrightarrow> (\<exists>x\<in>A. P x) \<or> (\<exists>x\<in>A. Q x)"
   419   by blast
   420 
   421 
   422 text \<open>Congruence rules\<close>
   423 
   424 lemma ball_cong:
   425   "A = B \<Longrightarrow> (\<And>x. x \<in> B \<Longrightarrow> P x \<longleftrightarrow> Q x) \<Longrightarrow>
   426     (\<forall>x\<in>A. P x) \<longleftrightarrow> (\<forall>x\<in>B. Q x)"
   427   by (simp add: Ball_def)
   428 
   429 lemma strong_ball_cong [cong]:
   430   "A = B \<Longrightarrow> (\<And>x. x \<in> B =simp=> P x \<longleftrightarrow> Q x) \<Longrightarrow>
   431     (\<forall>x\<in>A. P x) \<longleftrightarrow> (\<forall>x\<in>B. Q x)"
   432   by (simp add: simp_implies_def Ball_def)
   433 
   434 lemma bex_cong:
   435   "A = B \<Longrightarrow> (\<And>x. x \<in> B \<Longrightarrow> P x \<longleftrightarrow> Q x) \<Longrightarrow>
   436     (\<exists>x\<in>A. P x) \<longleftrightarrow> (\<exists>x\<in>B. Q x)"
   437   by (simp add: Bex_def cong: conj_cong)
   438 
   439 lemma strong_bex_cong [cong]:
   440   "A = B \<Longrightarrow> (\<And>x. x \<in> B =simp=> P x \<longleftrightarrow> Q x) \<Longrightarrow>
   441     (\<exists>x\<in>A. P x) \<longleftrightarrow> (\<exists>x\<in>B. Q x)"
   442   by (simp add: simp_implies_def Bex_def cong: conj_cong)
   443 
   444 lemma bex1_def: "(\<exists>!x\<in>X. P x) \<longleftrightarrow> (\<exists>x\<in>X. P x) \<and> (\<forall>x\<in>X. \<forall>y\<in>X. P x \<longrightarrow> P y \<longrightarrow> x = y)"
   445   by auto
   446 
   447 
   448 subsection \<open>Basic operations\<close>
   449 
   450 subsubsection \<open>Subsets\<close>
   451 
   452 lemma subsetI [intro!]: "(\<And>x. x \<in> A \<Longrightarrow> x \<in> B) \<Longrightarrow> A \<subseteq> B"
   453   by (simp add: less_eq_set_def le_fun_def)
   454 
   455 text \<open>
   456   \<^medskip>
   457   Map the type \<open>'a set \<Rightarrow> anything\<close> to just \<open>'a\<close>; for overloading constants
   458   whose first argument has type \<open>'a set\<close>.
   459 \<close>
   460 
   461 lemma subsetD [elim, intro?]: "A \<subseteq> B \<Longrightarrow> c \<in> A \<Longrightarrow> c \<in> B"
   462   by (simp add: less_eq_set_def le_fun_def)
   463   \<comment> \<open>Rule in Modus Ponens style.\<close>
   464 
   465 lemma rev_subsetD [intro?]: "c \<in> A \<Longrightarrow> A \<subseteq> B \<Longrightarrow> c \<in> B"
   466   \<comment> \<open>The same, with reversed premises for use with @{method erule} -- cf. @{thm rev_mp}.\<close>
   467   by (rule subsetD)
   468 
   469 lemma subsetCE [elim]: "A \<subseteq> B \<Longrightarrow> (c \<notin> A \<Longrightarrow> P) \<Longrightarrow> (c \<in> B \<Longrightarrow> P) \<Longrightarrow> P"
   470   \<comment> \<open>Classical elimination rule.\<close>
   471   by (auto simp add: less_eq_set_def le_fun_def)
   472 
   473 lemma subset_eq: "A \<subseteq> B \<longleftrightarrow> (\<forall>x\<in>A. x \<in> B)"
   474   by blast
   475 
   476 lemma contra_subsetD: "A \<subseteq> B \<Longrightarrow> c \<notin> B \<Longrightarrow> c \<notin> A"
   477   by blast
   478 
   479 lemma subset_refl: "A \<subseteq> A"
   480   by (fact order_refl) (* already [iff] *)
   481 
   482 lemma subset_trans: "A \<subseteq> B \<Longrightarrow> B \<subseteq> C \<Longrightarrow> A \<subseteq> C"
   483   by (fact order_trans)
   484 
   485 lemma set_rev_mp: "x \<in> A \<Longrightarrow> A \<subseteq> B \<Longrightarrow> x \<in> B"
   486   by (rule subsetD)
   487 
   488 lemma set_mp: "A \<subseteq> B \<Longrightarrow> x \<in> A \<Longrightarrow> x \<in> B"
   489   by (rule subsetD)
   490 
   491 lemma subset_not_subset_eq [code]: "A \<subset> B \<longleftrightarrow> A \<subseteq> B \<and> \<not> B \<subseteq> A"
   492   by (fact less_le_not_le)
   493 
   494 lemma eq_mem_trans: "a = b \<Longrightarrow> b \<in> A \<Longrightarrow> a \<in> A"
   495   by simp
   496 
   497 lemmas basic_trans_rules [trans] =
   498   order_trans_rules set_rev_mp set_mp eq_mem_trans
   499 
   500 
   501 subsubsection \<open>Equality\<close>
   502 
   503 lemma subset_antisym [intro!]: "A \<subseteq> B \<Longrightarrow> B \<subseteq> A \<Longrightarrow> A = B"
   504   \<comment> \<open>Anti-symmetry of the subset relation.\<close>
   505   by (iprover intro: set_eqI subsetD)
   506 
   507 text \<open>\<^medskip> Equality rules from ZF set theory -- are they appropriate here?\<close>
   508 
   509 lemma equalityD1: "A = B \<Longrightarrow> A \<subseteq> B"
   510   by simp
   511 
   512 lemma equalityD2: "A = B \<Longrightarrow> B \<subseteq> A"
   513   by simp
   514 
   515 text \<open>
   516   \<^medskip>
   517   Be careful when adding this to the claset as \<open>subset_empty\<close> is in the
   518   simpset: @{prop "A = {}"} goes to @{prop "{} \<subseteq> A"} and @{prop "A \<subseteq> {}"}
   519   and then back to @{prop "A = {}"}!
   520 \<close>
   521 
   522 lemma equalityE: "A = B \<Longrightarrow> (A \<subseteq> B \<Longrightarrow> B \<subseteq> A \<Longrightarrow> P) \<Longrightarrow> P"
   523   by simp
   524 
   525 lemma equalityCE [elim]: "A = B \<Longrightarrow> (c \<in> A \<Longrightarrow> c \<in> B \<Longrightarrow> P) \<Longrightarrow> (c \<notin> A \<Longrightarrow> c \<notin> B \<Longrightarrow> P) \<Longrightarrow> P"
   526   by blast
   527 
   528 lemma eqset_imp_iff: "A = B \<Longrightarrow> x \<in> A \<longleftrightarrow> x \<in> B"
   529   by simp
   530 
   531 lemma eqelem_imp_iff: "x = y \<Longrightarrow> x \<in> A \<longleftrightarrow> y \<in> A"
   532   by simp
   533 
   534 
   535 subsubsection \<open>The empty set\<close>
   536 
   537 lemma empty_def: "{} = {x. False}"
   538   by (simp add: bot_set_def bot_fun_def)
   539 
   540 lemma empty_iff [simp]: "c \<in> {} \<longleftrightarrow> False"
   541   by (simp add: empty_def)
   542 
   543 lemma emptyE [elim!]: "a \<in> {} \<Longrightarrow> P"
   544   by simp
   545 
   546 lemma empty_subsetI [iff]: "{} \<subseteq> A"
   547   \<comment> \<open>One effect is to delete the ASSUMPTION @{prop "{} \<subseteq> A"}\<close>
   548   by blast
   549 
   550 lemma equals0I: "(\<And>y. y \<in> A \<Longrightarrow> False) \<Longrightarrow> A = {}"
   551   by blast
   552 
   553 lemma equals0D: "A = {} \<Longrightarrow> a \<notin> A"
   554   \<comment> \<open>Use for reasoning about disjointness: \<open>A \<inter> B = {}\<close>\<close>
   555   by blast
   556 
   557 lemma ball_empty [simp]: "Ball {} P \<longleftrightarrow> True"
   558   by (simp add: Ball_def)
   559 
   560 lemma bex_empty [simp]: "Bex {} P \<longleftrightarrow> False"
   561   by (simp add: Bex_def)
   562 
   563 
   564 subsubsection \<open>The universal set -- UNIV\<close>
   565 
   566 abbreviation UNIV :: "'a set"
   567   where "UNIV \<equiv> top"
   568 
   569 lemma UNIV_def: "UNIV = {x. True}"
   570   by (simp add: top_set_def top_fun_def)
   571 
   572 lemma UNIV_I [simp]: "x \<in> UNIV"
   573   by (simp add: UNIV_def)
   574 
   575 declare UNIV_I [intro]  \<comment> \<open>unsafe makes it less likely to cause problems\<close>
   576 
   577 lemma UNIV_witness [intro?]: "\<exists>x. x \<in> UNIV"
   578   by simp
   579 
   580 lemma subset_UNIV: "A \<subseteq> UNIV"
   581   by (fact top_greatest) (* already simp *)
   582 
   583 text \<open>
   584   \<^medskip>
   585   Eta-contracting these two rules (to remove \<open>P\<close>) causes them
   586   to be ignored because of their interaction with congruence rules.
   587 \<close>
   588 
   589 lemma ball_UNIV [simp]: "Ball UNIV P \<longleftrightarrow> All P"
   590   by (simp add: Ball_def)
   591 
   592 lemma bex_UNIV [simp]: "Bex UNIV P \<longleftrightarrow> Ex P"
   593   by (simp add: Bex_def)
   594 
   595 lemma UNIV_eq_I: "(\<And>x. x \<in> A) \<Longrightarrow> UNIV = A"
   596   by auto
   597 
   598 lemma UNIV_not_empty [iff]: "UNIV \<noteq> {}"
   599   by (blast elim: equalityE)
   600 
   601 lemma empty_not_UNIV[simp]: "{} \<noteq> UNIV"
   602   by blast
   603 
   604 
   605 subsubsection \<open>The Powerset operator -- Pow\<close>
   606 
   607 definition Pow :: "'a set \<Rightarrow> 'a set set"
   608   where Pow_def: "Pow A = {B. B \<subseteq> A}"
   609 
   610 lemma Pow_iff [iff]: "A \<in> Pow B \<longleftrightarrow> A \<subseteq> B"
   611   by (simp add: Pow_def)
   612 
   613 lemma PowI: "A \<subseteq> B \<Longrightarrow> A \<in> Pow B"
   614   by (simp add: Pow_def)
   615 
   616 lemma PowD: "A \<in> Pow B \<Longrightarrow> A \<subseteq> B"
   617   by (simp add: Pow_def)
   618 
   619 lemma Pow_bottom: "{} \<in> Pow B"
   620   by simp
   621 
   622 lemma Pow_top: "A \<in> Pow A"
   623   by simp
   624 
   625 lemma Pow_not_empty: "Pow A \<noteq> {}"
   626   using Pow_top by blast
   627 
   628 
   629 subsubsection \<open>Set complement\<close>
   630 
   631 lemma Compl_iff [simp]: "c \<in> - A \<longleftrightarrow> c \<notin> A"
   632   by (simp add: fun_Compl_def uminus_set_def)
   633 
   634 lemma ComplI [intro!]: "(c \<in> A \<Longrightarrow> False) \<Longrightarrow> c \<in> - A"
   635   by (simp add: fun_Compl_def uminus_set_def) blast
   636 
   637 text \<open>
   638   \<^medskip>
   639   This form, with negated conclusion, works well with the Classical prover.
   640   Negated assumptions behave like formulae on the right side of the
   641   notional turnstile \dots
   642 \<close>
   643 
   644 lemma ComplD [dest!]: "c \<in> - A \<Longrightarrow> c \<notin> A"
   645   by simp
   646 
   647 lemmas ComplE = ComplD [elim_format]
   648 
   649 lemma Compl_eq: "- A = {x. \<not> x \<in> A}"
   650   by blast
   651 
   652 
   653 subsubsection \<open>Binary intersection\<close>
   654 
   655 abbreviation inter :: "'a set \<Rightarrow> 'a set \<Rightarrow> 'a set"  (infixl "\<inter>" 70)
   656   where "op \<inter> \<equiv> inf"
   657 
   658 notation (ASCII)
   659   inter  (infixl "Int" 70)
   660 
   661 lemma Int_def: "A \<inter> B = {x. x \<in> A \<and> x \<in> B}"
   662   by (simp add: inf_set_def inf_fun_def)
   663 
   664 lemma Int_iff [simp]: "c \<in> A \<inter> B \<longleftrightarrow> c \<in> A \<and> c \<in> B"
   665   unfolding Int_def by blast
   666 
   667 lemma IntI [intro!]: "c \<in> A \<Longrightarrow> c \<in> B \<Longrightarrow> c \<in> A \<inter> B"
   668   by simp
   669 
   670 lemma IntD1: "c \<in> A \<inter> B \<Longrightarrow> c \<in> A"
   671   by simp
   672 
   673 lemma IntD2: "c \<in> A \<inter> B \<Longrightarrow> c \<in> B"
   674   by simp
   675 
   676 lemma IntE [elim!]: "c \<in> A \<inter> B \<Longrightarrow> (c \<in> A \<Longrightarrow> c \<in> B \<Longrightarrow> P) \<Longrightarrow> P"
   677   by simp
   678 
   679 lemma mono_Int: "mono f \<Longrightarrow> f (A \<inter> B) \<subseteq> f A \<inter> f B"
   680   by (fact mono_inf)
   681 
   682 
   683 subsubsection \<open>Binary union\<close>
   684 
   685 abbreviation union :: "'a set \<Rightarrow> 'a set \<Rightarrow> 'a set"  (infixl "\<union>" 65)
   686   where "union \<equiv> sup"
   687 
   688 notation (ASCII)
   689   union  (infixl "Un" 65)
   690 
   691 lemma Un_def: "A \<union> B = {x. x \<in> A \<or> x \<in> B}"
   692   by (simp add: sup_set_def sup_fun_def)
   693 
   694 lemma Un_iff [simp]: "c \<in> A \<union> B \<longleftrightarrow> c \<in> A \<or> c \<in> B"
   695   unfolding Un_def by blast
   696 
   697 lemma UnI1 [elim?]: "c \<in> A \<Longrightarrow> c \<in> A \<union> B"
   698   by simp
   699 
   700 lemma UnI2 [elim?]: "c \<in> B \<Longrightarrow> c \<in> A \<union> B"
   701   by simp
   702 
   703 text \<open>\<^medskip> Classical introduction rule: no commitment to \<open>A\<close> vs. \<open>B\<close>.\<close>
   704 lemma UnCI [intro!]: "(c \<notin> B \<Longrightarrow> c \<in> A) \<Longrightarrow> c \<in> A \<union> B"
   705   by auto
   706 
   707 lemma UnE [elim!]: "c \<in> A \<union> B \<Longrightarrow> (c \<in> A \<Longrightarrow> P) \<Longrightarrow> (c \<in> B \<Longrightarrow> P) \<Longrightarrow> P"
   708   unfolding Un_def by blast
   709 
   710 lemma insert_def: "insert a B = {x. x = a} \<union> B"
   711   by (simp add: insert_compr Un_def)
   712 
   713 lemma mono_Un: "mono f \<Longrightarrow> f A \<union> f B \<subseteq> f (A \<union> B)"
   714   by (fact mono_sup)
   715 
   716 
   717 subsubsection \<open>Set difference\<close>
   718 
   719 lemma Diff_iff [simp]: "c \<in> A - B \<longleftrightarrow> c \<in> A \<and> c \<notin> B"
   720   by (simp add: minus_set_def fun_diff_def)
   721 
   722 lemma DiffI [intro!]: "c \<in> A \<Longrightarrow> c \<notin> B \<Longrightarrow> c \<in> A - B"
   723   by simp
   724 
   725 lemma DiffD1: "c \<in> A - B \<Longrightarrow> c \<in> A"
   726   by simp
   727 
   728 lemma DiffD2: "c \<in> A - B \<Longrightarrow> c \<in> B \<Longrightarrow> P"
   729   by simp
   730 
   731 lemma DiffE [elim!]: "c \<in> A - B \<Longrightarrow> (c \<in> A \<Longrightarrow> c \<notin> B \<Longrightarrow> P) \<Longrightarrow> P"
   732   by simp
   733 
   734 lemma set_diff_eq: "A - B = {x. x \<in> A \<and> x \<notin> B}"
   735   by blast
   736 
   737 lemma Compl_eq_Diff_UNIV: "- A = (UNIV - A)"
   738   by blast
   739 
   740 
   741 subsubsection \<open>Augmenting a set -- @{const insert}\<close>
   742 
   743 lemma insert_iff [simp]: "a \<in> insert b A \<longleftrightarrow> a = b \<or> a \<in> A"
   744   unfolding insert_def by blast
   745 
   746 lemma insertI1: "a \<in> insert a B"
   747   by simp
   748 
   749 lemma insertI2: "a \<in> B \<Longrightarrow> a \<in> insert b B"
   750   by simp
   751 
   752 lemma insertE [elim!]: "a \<in> insert b A \<Longrightarrow> (a = b \<Longrightarrow> P) \<Longrightarrow> (a \<in> A \<Longrightarrow> P) \<Longrightarrow> P"
   753   unfolding insert_def by blast
   754 
   755 lemma insertCI [intro!]: "(a \<notin> B \<Longrightarrow> a = b) \<Longrightarrow> a \<in> insert b B"
   756   \<comment> \<open>Classical introduction rule.\<close>
   757   by auto
   758 
   759 lemma subset_insert_iff: "A \<subseteq> insert x B \<longleftrightarrow> (if x \<in> A then A - {x} \<subseteq> B else A \<subseteq> B)"
   760   by auto
   761 
   762 lemma set_insert:
   763   assumes "x \<in> A"
   764   obtains B where "A = insert x B" and "x \<notin> B"
   765 proof
   766   show "A = insert x (A - {x})" using assms by blast
   767   show "x \<notin> A - {x}" by blast
   768 qed
   769 
   770 lemma insert_ident: "x \<notin> A \<Longrightarrow> x \<notin> B \<Longrightarrow> insert x A = insert x B \<longleftrightarrow> A = B"
   771   by auto
   772 
   773 lemma insert_eq_iff:
   774   assumes "a \<notin> A" "b \<notin> B"
   775   shows "insert a A = insert b B \<longleftrightarrow>
   776     (if a = b then A = B else \<exists>C. A = insert b C \<and> b \<notin> C \<and> B = insert a C \<and> a \<notin> C)"
   777     (is "?L \<longleftrightarrow> ?R")
   778 proof
   779   show ?R if ?L
   780   proof (cases "a = b")
   781     case True
   782     with assms \<open>?L\<close> show ?R
   783       by (simp add: insert_ident)
   784   next
   785     case False
   786     let ?C = "A - {b}"
   787     have "A = insert b ?C \<and> b \<notin> ?C \<and> B = insert a ?C \<and> a \<notin> ?C"
   788       using assms \<open>?L\<close> \<open>a \<noteq> b\<close> by auto
   789     then show ?R using \<open>a \<noteq> b\<close> by auto
   790   qed
   791   show ?L if ?R
   792     using that by (auto split: if_splits)
   793 qed
   794 
   795 lemma insert_UNIV: "insert x UNIV = UNIV"
   796   by auto
   797 
   798 
   799 subsubsection \<open>Singletons, using insert\<close>
   800 
   801 lemma singletonI [intro!]: "a \<in> {a}"
   802   \<comment> \<open>Redundant? But unlike \<open>insertCI\<close>, it proves the subgoal immediately!\<close>
   803   by (rule insertI1)
   804 
   805 lemma singletonD [dest!]: "b \<in> {a} \<Longrightarrow> b = a"
   806   by blast
   807 
   808 lemmas singletonE = singletonD [elim_format]
   809 
   810 lemma singleton_iff: "b \<in> {a} \<longleftrightarrow> b = a"
   811   by blast
   812 
   813 lemma singleton_inject [dest!]: "{a} = {b} \<Longrightarrow> a = b"
   814   by blast
   815 
   816 lemma singleton_insert_inj_eq [iff]: "{b} = insert a A \<longleftrightarrow> a = b \<and> A \<subseteq> {b}"
   817   by blast
   818 
   819 lemma singleton_insert_inj_eq' [iff]: "insert a A = {b} \<longleftrightarrow> a = b \<and> A \<subseteq> {b}"
   820   by blast
   821 
   822 lemma subset_singletonD: "A \<subseteq> {x} \<Longrightarrow> A = {} \<or> A = {x}"
   823   by fast
   824 
   825 lemma subset_singleton_iff: "X \<subseteq> {a} \<longleftrightarrow> X = {} \<or> X = {a}"
   826   by blast
   827 
   828 lemma singleton_conv [simp]: "{x. x = a} = {a}"
   829   by blast
   830 
   831 lemma singleton_conv2 [simp]: "{x. a = x} = {a}"
   832   by blast
   833 
   834 lemma Diff_single_insert: "A - {x} \<subseteq> B \<Longrightarrow> A \<subseteq> insert x B"
   835   by blast
   836 
   837 lemma subset_Diff_insert: "A \<subseteq> B - insert x C \<longleftrightarrow> A \<subseteq> B - C \<and> x \<notin> A"
   838   by blast
   839 
   840 lemma doubleton_eq_iff: "{a, b} = {c, d} \<longleftrightarrow> a = c \<and> b = d \<or> a = d \<and> b = c"
   841   by (blast elim: equalityE)
   842 
   843 lemma Un_singleton_iff: "A \<union> B = {x} \<longleftrightarrow> A = {} \<and> B = {x} \<or> A = {x} \<and> B = {} \<or> A = {x} \<and> B = {x}"
   844   by auto
   845 
   846 lemma singleton_Un_iff: "{x} = A \<union> B \<longleftrightarrow> A = {} \<and> B = {x} \<or> A = {x} \<and> B = {} \<or> A = {x} \<and> B = {x}"
   847   by auto
   848 
   849 
   850 subsubsection \<open>Image of a set under a function\<close>
   851 
   852 text \<open>Frequently \<open>b\<close> does not have the syntactic form of \<open>f x\<close>.\<close>
   853 
   854 definition image :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a set \<Rightarrow> 'b set"    (infixr "`" 90)
   855   where "f ` A = {y. \<exists>x\<in>A. y = f x}"
   856 
   857 lemma image_eqI [simp, intro]: "b = f x \<Longrightarrow> x \<in> A \<Longrightarrow> b \<in> f ` A"
   858   unfolding image_def by blast
   859 
   860 lemma imageI: "x \<in> A \<Longrightarrow> f x \<in> f ` A"
   861   by (rule image_eqI) (rule refl)
   862 
   863 lemma rev_image_eqI: "x \<in> A \<Longrightarrow> b = f x \<Longrightarrow> b \<in> f ` A"
   864   \<comment> \<open>This version's more effective when we already have the required \<open>x\<close>.\<close>
   865   by (rule image_eqI)
   866 
   867 lemma imageE [elim!]:
   868   assumes "b \<in> (\<lambda>x. f x) ` A"  \<comment> \<open>The eta-expansion gives variable-name preservation.\<close>
   869   obtains x where "b = f x" and "x \<in> A"
   870   using assms unfolding image_def by blast
   871 
   872 lemma Compr_image_eq: "{x \<in> f ` A. P x} = f ` {x \<in> A. P (f x)}"
   873   by auto
   874 
   875 lemma image_Un: "f ` (A \<union> B) = f ` A \<union> f ` B"
   876   by blast
   877 
   878 lemma image_iff: "z \<in> f ` A \<longleftrightarrow> (\<exists>x\<in>A. z = f x)"
   879   by blast
   880 
   881 lemma image_subsetI: "(\<And>x. x \<in> A \<Longrightarrow> f x \<in> B) \<Longrightarrow> f ` A \<subseteq> B"
   882   \<comment> \<open>Replaces the three steps \<open>subsetI\<close>, \<open>imageE\<close>,
   883     \<open>hypsubst\<close>, but breaks too many existing proofs.\<close>
   884   by blast
   885 
   886 lemma image_subset_iff: "f ` A \<subseteq> B \<longleftrightarrow> (\<forall>x\<in>A. f x \<in> B)"
   887   \<comment> \<open>This rewrite rule would confuse users if made default.\<close>
   888   by blast
   889 
   890 lemma subset_imageE:
   891   assumes "B \<subseteq> f ` A"
   892   obtains C where "C \<subseteq> A" and "B = f ` C"
   893 proof -
   894   from assms have "B = f ` {a \<in> A. f a \<in> B}" by fast
   895   moreover have "{a \<in> A. f a \<in> B} \<subseteq> A" by blast
   896   ultimately show thesis by (blast intro: that)
   897 qed
   898 
   899 lemma subset_image_iff: "B \<subseteq> f ` A \<longleftrightarrow> (\<exists>AA\<subseteq>A. B = f ` AA)"
   900   by (blast elim: subset_imageE)
   901 
   902 lemma image_ident [simp]: "(\<lambda>x. x) ` Y = Y"
   903   by blast
   904 
   905 lemma image_empty [simp]: "f ` {} = {}"
   906   by blast
   907 
   908 lemma image_insert [simp]: "f ` insert a B = insert (f a) (f ` B)"
   909   by blast
   910 
   911 lemma image_constant: "x \<in> A \<Longrightarrow> (\<lambda>x. c) ` A = {c}"
   912   by auto
   913 
   914 lemma image_constant_conv: "(\<lambda>x. c) ` A = (if A = {} then {} else {c})"
   915   by auto
   916 
   917 lemma image_image: "f ` (g ` A) = (\<lambda>x. f (g x)) ` A"
   918   by blast
   919 
   920 lemma insert_image [simp]: "x \<in> A \<Longrightarrow> insert (f x) (f ` A) = f ` A"
   921   by blast
   922 
   923 lemma image_is_empty [iff]: "f ` A = {} \<longleftrightarrow> A = {}"
   924   by blast
   925 
   926 lemma empty_is_image [iff]: "{} = f ` A \<longleftrightarrow> A = {}"
   927   by blast
   928 
   929 lemma image_Collect: "f ` {x. P x} = {f x | x. P x}"
   930   \<comment> \<open>NOT suitable as a default simp rule: the RHS isn't simpler than the LHS,
   931       with its implicit quantifier and conjunction.  Also image enjoys better
   932       equational properties than does the RHS.\<close>
   933   by blast
   934 
   935 lemma if_image_distrib [simp]:
   936   "(\<lambda>x. if P x then f x else g x) ` S = f ` (S \<inter> {x. P x}) \<union> g ` (S \<inter> {x. \<not> P x})"
   937   by auto
   938 
   939 lemma image_cong: "M = N \<Longrightarrow> (\<And>x. x \<in> N \<Longrightarrow> f x = g x) \<Longrightarrow> f ` M = g ` N"
   940   by (simp add: image_def)
   941 
   942 lemma image_Int_subset: "f ` (A \<inter> B) \<subseteq> f ` A \<inter> f ` B"
   943   by blast
   944 
   945 lemma image_diff_subset: "f ` A - f ` B \<subseteq> f ` (A - B)"
   946   by blast
   947 
   948 lemma Setcompr_eq_image: "{f x |x. x \<in> A} = f ` A"
   949   by blast
   950 
   951 lemma setcompr_eq_image: "{f x |x. P x} = f ` {x. P x}"
   952   by auto
   953 
   954 lemma ball_imageD: "\<forall>x\<in>f ` A. P x \<Longrightarrow> \<forall>x\<in>A. P (f x)"
   955   by simp
   956 
   957 lemma bex_imageD: "\<exists>x\<in>f ` A. P x \<Longrightarrow> \<exists>x\<in>A. P (f x)"
   958   by auto
   959 
   960 lemma image_add_0 [simp]: "op + (0::'a::comm_monoid_add) ` S = S"
   961   by auto
   962 
   963 
   964 text \<open>\<^medskip> Range of a function -- just an abbreviation for image!\<close>
   965 
   966 abbreviation range :: "('a \<Rightarrow> 'b) \<Rightarrow> 'b set"  \<comment> \<open>of function\<close>
   967   where "range f \<equiv> f ` UNIV"
   968 
   969 lemma range_eqI: "b = f x \<Longrightarrow> b \<in> range f"
   970   by simp
   971 
   972 lemma rangeI: "f x \<in> range f"
   973   by simp
   974 
   975 lemma rangeE [elim?]: "b \<in> range (\<lambda>x. f x) \<Longrightarrow> (\<And>x. b = f x \<Longrightarrow> P) \<Longrightarrow> P"
   976   by (rule imageE)
   977 
   978 lemma full_SetCompr_eq: "{u. \<exists>x. u = f x} = range f"
   979   by auto
   980 
   981 lemma range_composition: "range (\<lambda>x. f (g x)) = f ` range g"
   982   by auto
   983 
   984 lemma range_eq_singletonD: "range f = {a} \<Longrightarrow> f x = a"
   985   by auto
   986 
   987 
   988 subsubsection \<open>Some rules with \<open>if\<close>\<close>
   989 
   990 text \<open>Elimination of \<open>{x. \<dots> \<and> x = t \<and> \<dots>}\<close>.\<close>
   991 
   992 lemma Collect_conv_if: "{x. x = a \<and> P x} = (if P a then {a} else {})"
   993   by auto
   994 
   995 lemma Collect_conv_if2: "{x. a = x \<and> P x} = (if P a then {a} else {})"
   996   by auto
   997 
   998 text \<open>
   999   Rewrite rules for boolean case-splitting: faster than \<open>if_split [split]\<close>.
  1000 \<close>
  1001 
  1002 lemma if_split_eq1: "(if Q then x else y) = b \<longleftrightarrow> (Q \<longrightarrow> x = b) \<and> (\<not> Q \<longrightarrow> y = b)"
  1003   by (rule if_split)
  1004 
  1005 lemma if_split_eq2: "a = (if Q then x else y) \<longleftrightarrow> (Q \<longrightarrow> a = x) \<and> (\<not> Q \<longrightarrow> a = y)"
  1006   by (rule if_split)
  1007 
  1008 text \<open>
  1009   Split ifs on either side of the membership relation.
  1010   Not for \<open>[simp]\<close> -- can cause goals to blow up!
  1011 \<close>
  1012 
  1013 lemma if_split_mem1: "(if Q then x else y) \<in> b \<longleftrightarrow> (Q \<longrightarrow> x \<in> b) \<and> (\<not> Q \<longrightarrow> y \<in> b)"
  1014   by (rule if_split)
  1015 
  1016 lemma if_split_mem2: "(a \<in> (if Q then x else y)) \<longleftrightarrow> (Q \<longrightarrow> a \<in> x) \<and> (\<not> Q \<longrightarrow> a \<in> y)"
  1017   by (rule if_split [where P = "\<lambda>S. a \<in> S"])
  1018 
  1019 lemmas split_ifs = if_bool_eq_conj if_split_eq1 if_split_eq2 if_split_mem1 if_split_mem2
  1020 
  1021 (*Would like to add these, but the existing code only searches for the
  1022   outer-level constant, which in this case is just Set.member; we instead need
  1023   to use term-nets to associate patterns with rules.  Also, if a rule fails to
  1024   apply, then the formula should be kept.
  1025   [("uminus", Compl_iff RS iffD1), ("minus", [Diff_iff RS iffD1]),
  1026    ("Int", [IntD1,IntD2]),
  1027    ("Collect", [CollectD]), ("Inter", [InterD]), ("INTER", [INT_D])]
  1028  *)
  1029 
  1030 
  1031 subsection \<open>Further operations and lemmas\<close>
  1032 
  1033 subsubsection \<open>The ``proper subset'' relation\<close>
  1034 
  1035 lemma psubsetI [intro!]: "A \<subseteq> B \<Longrightarrow> A \<noteq> B \<Longrightarrow> A \<subset> B"
  1036   unfolding less_le by blast
  1037 
  1038 lemma psubsetE [elim!]: "A \<subset> B \<Longrightarrow> (A \<subseteq> B \<Longrightarrow> \<not> B \<subseteq> A \<Longrightarrow> R) \<Longrightarrow> R"
  1039   unfolding less_le by blast
  1040 
  1041 lemma psubset_insert_iff:
  1042   "A \<subset> insert x B \<longleftrightarrow> (if x \<in> B then A \<subset> B else if x \<in> A then A - {x} \<subset> B else A \<subseteq> B)"
  1043   by (auto simp add: less_le subset_insert_iff)
  1044 
  1045 lemma psubset_eq: "A \<subset> B \<longleftrightarrow> A \<subseteq> B \<and> A \<noteq> B"
  1046   by (simp only: less_le)
  1047 
  1048 lemma psubset_imp_subset: "A \<subset> B \<Longrightarrow> A \<subseteq> B"
  1049   by (simp add: psubset_eq)
  1050 
  1051 lemma psubset_trans: "A \<subset> B \<Longrightarrow> B \<subset> C \<Longrightarrow> A \<subset> C"
  1052   unfolding less_le by (auto dest: subset_antisym)
  1053 
  1054 lemma psubsetD: "A \<subset> B \<Longrightarrow> c \<in> A \<Longrightarrow> c \<in> B"
  1055   unfolding less_le by (auto dest: subsetD)
  1056 
  1057 lemma psubset_subset_trans: "A \<subset> B \<Longrightarrow> B \<subseteq> C \<Longrightarrow> A \<subset> C"
  1058   by (auto simp add: psubset_eq)
  1059 
  1060 lemma subset_psubset_trans: "A \<subseteq> B \<Longrightarrow> B \<subset> C \<Longrightarrow> A \<subset> C"
  1061   by (auto simp add: psubset_eq)
  1062 
  1063 lemma psubset_imp_ex_mem: "A \<subset> B \<Longrightarrow> \<exists>b. b \<in> B - A"
  1064   unfolding less_le by blast
  1065 
  1066 lemma atomize_ball: "(\<And>x. x \<in> A \<Longrightarrow> P x) \<equiv> Trueprop (\<forall>x\<in>A. P x)"
  1067   by (simp only: Ball_def atomize_all atomize_imp)
  1068 
  1069 lemmas [symmetric, rulify] = atomize_ball
  1070   and [symmetric, defn] = atomize_ball
  1071 
  1072 lemma image_Pow_mono: "f ` A \<subseteq> B \<Longrightarrow> image f ` Pow A \<subseteq> Pow B"
  1073   by blast
  1074 
  1075 lemma image_Pow_surj: "f ` A = B \<Longrightarrow> image f ` Pow A = Pow B"
  1076   by (blast elim: subset_imageE)
  1077 
  1078 
  1079 subsubsection \<open>Derived rules involving subsets.\<close>
  1080 
  1081 text \<open>\<open>insert\<close>.\<close>
  1082 
  1083 lemma subset_insertI: "B \<subseteq> insert a B"
  1084   by (rule subsetI) (erule insertI2)
  1085 
  1086 lemma subset_insertI2: "A \<subseteq> B \<Longrightarrow> A \<subseteq> insert b B"
  1087   by blast
  1088 
  1089 lemma subset_insert: "x \<notin> A \<Longrightarrow> A \<subseteq> insert x B \<longleftrightarrow> A \<subseteq> B"
  1090   by blast
  1091 
  1092 
  1093 text \<open>\<^medskip> Finite Union -- the least upper bound of two sets.\<close>
  1094 
  1095 lemma Un_upper1: "A \<subseteq> A \<union> B"
  1096   by (fact sup_ge1)
  1097 
  1098 lemma Un_upper2: "B \<subseteq> A \<union> B"
  1099   by (fact sup_ge2)
  1100 
  1101 lemma Un_least: "A \<subseteq> C \<Longrightarrow> B \<subseteq> C \<Longrightarrow> A \<union> B \<subseteq> C"
  1102   by (fact sup_least)
  1103 
  1104 
  1105 text \<open>\<^medskip> Finite Intersection -- the greatest lower bound of two sets.\<close>
  1106 
  1107 lemma Int_lower1: "A \<inter> B \<subseteq> A"
  1108   by (fact inf_le1)
  1109 
  1110 lemma Int_lower2: "A \<inter> B \<subseteq> B"
  1111   by (fact inf_le2)
  1112 
  1113 lemma Int_greatest: "C \<subseteq> A \<Longrightarrow> C \<subseteq> B \<Longrightarrow> C \<subseteq> A \<inter> B"
  1114   by (fact inf_greatest)
  1115 
  1116 
  1117 text \<open>\<^medskip> Set difference.\<close>
  1118 
  1119 lemma Diff_subset: "A - B \<subseteq> A"
  1120   by blast
  1121 
  1122 lemma Diff_subset_conv: "A - B \<subseteq> C \<longleftrightarrow> A \<subseteq> B \<union> C"
  1123   by blast
  1124 
  1125 
  1126 subsubsection \<open>Equalities involving union, intersection, inclusion, etc.\<close>
  1127 
  1128 text \<open>\<open>{}\<close>.\<close>
  1129 
  1130 lemma Collect_const [simp]: "{s. P} = (if P then UNIV else {})"
  1131   \<comment> \<open>supersedes \<open>Collect_False_empty\<close>\<close>
  1132   by auto
  1133 
  1134 lemma subset_empty [simp]: "A \<subseteq> {} \<longleftrightarrow> A = {}"
  1135   by (fact bot_unique)
  1136 
  1137 lemma not_psubset_empty [iff]: "\<not> (A < {})"
  1138   by (fact not_less_bot) (* FIXME: already simp *)
  1139 
  1140 lemma Collect_empty_eq [simp]: "Collect P = {} \<longleftrightarrow> (\<forall>x. \<not> P x)"
  1141   by blast
  1142 
  1143 lemma empty_Collect_eq [simp]: "{} = Collect P \<longleftrightarrow> (\<forall>x. \<not> P x)"
  1144   by blast
  1145 
  1146 lemma Collect_neg_eq: "{x. \<not> P x} = - {x. P x}"
  1147   by blast
  1148 
  1149 lemma Collect_disj_eq: "{x. P x \<or> Q x} = {x. P x} \<union> {x. Q x}"
  1150   by blast
  1151 
  1152 lemma Collect_imp_eq: "{x. P x \<longrightarrow> Q x} = - {x. P x} \<union> {x. Q x}"
  1153   by blast
  1154 
  1155 lemma Collect_conj_eq: "{x. P x \<and> Q x} = {x. P x} \<inter> {x. Q x}"
  1156   by blast
  1157 
  1158 lemma Collect_mono_iff: "Collect P \<subseteq> Collect Q \<longleftrightarrow> (\<forall>x. P x \<longrightarrow> Q x)"
  1159   by blast
  1160 
  1161 
  1162 text \<open>\<^medskip> \<open>insert\<close>.\<close>
  1163 
  1164 lemma insert_is_Un: "insert a A = {a} \<union> A"
  1165   \<comment> \<open>NOT SUITABLE FOR REWRITING since \<open>{a} \<equiv> insert a {}\<close>\<close>
  1166   by blast
  1167 
  1168 lemma insert_not_empty [simp]: "insert a A \<noteq> {}"
  1169   and empty_not_insert [simp]: "{} \<noteq> insert a A"
  1170   by blast+
  1171 
  1172 lemma insert_absorb: "a \<in> A \<Longrightarrow> insert a A = A"
  1173   \<comment> \<open>\<open>[simp]\<close> causes recursive calls when there are nested inserts\<close>
  1174   \<comment> \<open>with \<^emph>\<open>quadratic\<close> running time\<close>
  1175   by blast
  1176 
  1177 lemma insert_absorb2 [simp]: "insert x (insert x A) = insert x A"
  1178   by blast
  1179 
  1180 lemma insert_commute: "insert x (insert y A) = insert y (insert x A)"
  1181   by blast
  1182 
  1183 lemma insert_subset [simp]: "insert x A \<subseteq> B \<longleftrightarrow> x \<in> B \<and> A \<subseteq> B"
  1184   by blast
  1185 
  1186 lemma mk_disjoint_insert: "a \<in> A \<Longrightarrow> \<exists>B. A = insert a B \<and> a \<notin> B"
  1187   \<comment> \<open>use new \<open>B\<close> rather than \<open>A - {a}\<close> to avoid infinite unfolding\<close>
  1188   by (rule exI [where x = "A - {a}"]) blast
  1189 
  1190 lemma insert_Collect: "insert a (Collect P) = {u. u \<noteq> a \<longrightarrow> P u}"
  1191   by auto
  1192 
  1193 lemma insert_inter_insert [simp]: "insert a A \<inter> insert a B = insert a (A \<inter> B)"
  1194   by blast
  1195 
  1196 lemma insert_disjoint [simp]:
  1197   "insert a A \<inter> B = {} \<longleftrightarrow> a \<notin> B \<and> A \<inter> B = {}"
  1198   "{} = insert a A \<inter> B \<longleftrightarrow> a \<notin> B \<and> {} = A \<inter> B"
  1199   by auto
  1200 
  1201 lemma disjoint_insert [simp]:
  1202   "B \<inter> insert a A = {} \<longleftrightarrow> a \<notin> B \<and> B \<inter> A = {}"
  1203   "{} = A \<inter> insert b B \<longleftrightarrow> b \<notin> A \<and> {} = A \<inter> B"
  1204   by auto
  1205 
  1206 
  1207 text \<open>\<^medskip> \<open>Int\<close>\<close>
  1208 
  1209 lemma Int_absorb: "A \<inter> A = A"
  1210   by (fact inf_idem) (* already simp *)
  1211 
  1212 lemma Int_left_absorb: "A \<inter> (A \<inter> B) = A \<inter> B"
  1213   by (fact inf_left_idem)
  1214 
  1215 lemma Int_commute: "A \<inter> B = B \<inter> A"
  1216   by (fact inf_commute)
  1217 
  1218 lemma Int_left_commute: "A \<inter> (B \<inter> C) = B \<inter> (A \<inter> C)"
  1219   by (fact inf_left_commute)
  1220 
  1221 lemma Int_assoc: "(A \<inter> B) \<inter> C = A \<inter> (B \<inter> C)"
  1222   by (fact inf_assoc)
  1223 
  1224 lemmas Int_ac = Int_assoc Int_left_absorb Int_commute Int_left_commute
  1225   \<comment> \<open>Intersection is an AC-operator\<close>
  1226 
  1227 lemma Int_absorb1: "B \<subseteq> A \<Longrightarrow> A \<inter> B = B"
  1228   by (fact inf_absorb2)
  1229 
  1230 lemma Int_absorb2: "A \<subseteq> B \<Longrightarrow> A \<inter> B = A"
  1231   by (fact inf_absorb1)
  1232 
  1233 lemma Int_empty_left: "{} \<inter> B = {}"
  1234   by (fact inf_bot_left) (* already simp *)
  1235 
  1236 lemma Int_empty_right: "A \<inter> {} = {}"
  1237   by (fact inf_bot_right) (* already simp *)
  1238 
  1239 lemma disjoint_eq_subset_Compl: "A \<inter> B = {} \<longleftrightarrow> A \<subseteq> - B"
  1240   by blast
  1241 
  1242 lemma disjoint_iff_not_equal: "A \<inter> B = {} \<longleftrightarrow> (\<forall>x\<in>A. \<forall>y\<in>B. x \<noteq> y)"
  1243   by blast
  1244 
  1245 lemma Int_UNIV_left: "UNIV \<inter> B = B"
  1246   by (fact inf_top_left) (* already simp *)
  1247 
  1248 lemma Int_UNIV_right: "A \<inter> UNIV = A"
  1249   by (fact inf_top_right) (* already simp *)
  1250 
  1251 lemma Int_Un_distrib: "A \<inter> (B \<union> C) = (A \<inter> B) \<union> (A \<inter> C)"
  1252   by (fact inf_sup_distrib1)
  1253 
  1254 lemma Int_Un_distrib2: "(B \<union> C) \<inter> A = (B \<inter> A) \<union> (C \<inter> A)"
  1255   by (fact inf_sup_distrib2)
  1256 
  1257 lemma Int_UNIV [simp]: "A \<inter> B = UNIV \<longleftrightarrow> A = UNIV \<and> B = UNIV"
  1258   by (fact inf_eq_top_iff) (* already simp *)
  1259 
  1260 lemma Int_subset_iff [simp]: "C \<subseteq> A \<inter> B \<longleftrightarrow> C \<subseteq> A \<and> C \<subseteq> B"
  1261   by (fact le_inf_iff)
  1262 
  1263 lemma Int_Collect: "x \<in> A \<inter> {x. P x} \<longleftrightarrow> x \<in> A \<and> P x"
  1264   by blast
  1265 
  1266 
  1267 text \<open>\<^medskip> \<open>Un\<close>.\<close>
  1268 
  1269 lemma Un_absorb: "A \<union> A = A"
  1270   by (fact sup_idem) (* already simp *)
  1271 
  1272 lemma Un_left_absorb: "A \<union> (A \<union> B) = A \<union> B"
  1273   by (fact sup_left_idem)
  1274 
  1275 lemma Un_commute: "A \<union> B = B \<union> A"
  1276   by (fact sup_commute)
  1277 
  1278 lemma Un_left_commute: "A \<union> (B \<union> C) = B \<union> (A \<union> C)"
  1279   by (fact sup_left_commute)
  1280 
  1281 lemma Un_assoc: "(A \<union> B) \<union> C = A \<union> (B \<union> C)"
  1282   by (fact sup_assoc)
  1283 
  1284 lemmas Un_ac = Un_assoc Un_left_absorb Un_commute Un_left_commute
  1285   \<comment> \<open>Union is an AC-operator\<close>
  1286 
  1287 lemma Un_absorb1: "A \<subseteq> B \<Longrightarrow> A \<union> B = B"
  1288   by (fact sup_absorb2)
  1289 
  1290 lemma Un_absorb2: "B \<subseteq> A \<Longrightarrow> A \<union> B = A"
  1291   by (fact sup_absorb1)
  1292 
  1293 lemma Un_empty_left: "{} \<union> B = B"
  1294   by (fact sup_bot_left) (* already simp *)
  1295 
  1296 lemma Un_empty_right: "A \<union> {} = A"
  1297   by (fact sup_bot_right) (* already simp *)
  1298 
  1299 lemma Un_UNIV_left: "UNIV \<union> B = UNIV"
  1300   by (fact sup_top_left) (* already simp *)
  1301 
  1302 lemma Un_UNIV_right: "A \<union> UNIV = UNIV"
  1303   by (fact sup_top_right) (* already simp *)
  1304 
  1305 lemma Un_insert_left [simp]: "(insert a B) \<union> C = insert a (B \<union> C)"
  1306   by blast
  1307 
  1308 lemma Un_insert_right [simp]: "A \<union> (insert a B) = insert a (A \<union> B)"
  1309   by blast
  1310 
  1311 lemma Int_insert_left: "(insert a B) \<inter> C = (if a \<in> C then insert a (B \<inter> C) else B \<inter> C)"
  1312   by auto
  1313 
  1314 lemma Int_insert_left_if0 [simp]: "a \<notin> C \<Longrightarrow> (insert a B) \<inter> C = B \<inter> C"
  1315   by auto
  1316 
  1317 lemma Int_insert_left_if1 [simp]: "a \<in> C \<Longrightarrow> (insert a B) \<inter> C = insert a (B \<inter> C)"
  1318   by auto
  1319 
  1320 lemma Int_insert_right: "A \<inter> (insert a B) = (if a \<in> A then insert a (A \<inter> B) else A \<inter> B)"
  1321   by auto
  1322 
  1323 lemma Int_insert_right_if0 [simp]: "a \<notin> A \<Longrightarrow> A \<inter> (insert a B) = A \<inter> B"
  1324   by auto
  1325 
  1326 lemma Int_insert_right_if1 [simp]: "a \<in> A \<Longrightarrow> A \<inter> (insert a B) = insert a (A \<inter> B)"
  1327   by auto
  1328 
  1329 lemma Un_Int_distrib: "A \<union> (B \<inter> C) = (A \<union> B) \<inter> (A \<union> C)"
  1330   by (fact sup_inf_distrib1)
  1331 
  1332 lemma Un_Int_distrib2: "(B \<inter> C) \<union> A = (B \<union> A) \<inter> (C \<union> A)"
  1333   by (fact sup_inf_distrib2)
  1334 
  1335 lemma Un_Int_crazy: "(A \<inter> B) \<union> (B \<inter> C) \<union> (C \<inter> A) = (A \<union> B) \<inter> (B \<union> C) \<inter> (C \<union> A)"
  1336   by blast
  1337 
  1338 lemma subset_Un_eq: "A \<subseteq> B \<longleftrightarrow> A \<union> B = B"
  1339   by (fact le_iff_sup)
  1340 
  1341 lemma Un_empty [iff]: "A \<union> B = {} \<longleftrightarrow> A = {} \<and> B = {}"
  1342   by (fact sup_eq_bot_iff) (* FIXME: already simp *)
  1343 
  1344 lemma Un_subset_iff [simp]: "A \<union> B \<subseteq> C \<longleftrightarrow> A \<subseteq> C \<and> B \<subseteq> C"
  1345   by (fact le_sup_iff)
  1346 
  1347 lemma Un_Diff_Int: "(A - B) \<union> (A \<inter> B) = A"
  1348   by blast
  1349 
  1350 lemma Diff_Int2: "A \<inter> C - B \<inter> C = A \<inter> C - B"
  1351   by blast
  1352 
  1353 
  1354 text \<open>\<^medskip> Set complement\<close>
  1355 
  1356 lemma Compl_disjoint [simp]: "A \<inter> - A = {}"
  1357   by (fact inf_compl_bot)
  1358 
  1359 lemma Compl_disjoint2 [simp]: "- A \<inter> A = {}"
  1360   by (fact compl_inf_bot)
  1361 
  1362 lemma Compl_partition: "A \<union> - A = UNIV"
  1363   by (fact sup_compl_top)
  1364 
  1365 lemma Compl_partition2: "- A \<union> A = UNIV"
  1366   by (fact compl_sup_top)
  1367 
  1368 lemma double_complement: "- (-A) = A" for A :: "'a set"
  1369   by (fact double_compl) (* already simp *)
  1370 
  1371 lemma Compl_Un: "- (A \<union> B) = (- A) \<inter> (- B)"
  1372   by (fact compl_sup) (* already simp *)
  1373 
  1374 lemma Compl_Int: "- (A \<inter> B) = (- A) \<union> (- B)"
  1375   by (fact compl_inf) (* already simp *)
  1376 
  1377 lemma subset_Compl_self_eq: "A \<subseteq> - A \<longleftrightarrow> A = {}"
  1378   by blast
  1379 
  1380 lemma Un_Int_assoc_eq: "(A \<inter> B) \<union> C = A \<inter> (B \<union> C) \<longleftrightarrow> C \<subseteq> A"
  1381   \<comment> \<open>Halmos, Naive Set Theory, page 16.\<close>
  1382   by blast
  1383 
  1384 lemma Compl_UNIV_eq: "- UNIV = {}"
  1385   by (fact compl_top_eq) (* already simp *)
  1386 
  1387 lemma Compl_empty_eq: "- {} = UNIV"
  1388   by (fact compl_bot_eq) (* already simp *)
  1389 
  1390 lemma Compl_subset_Compl_iff [iff]: "- A \<subseteq> - B \<longleftrightarrow> B \<subseteq> A"
  1391   by (fact compl_le_compl_iff) (* FIXME: already simp *)
  1392 
  1393 lemma Compl_eq_Compl_iff [iff]: "- A = - B \<longleftrightarrow> A = B"
  1394   for A B :: "'a set"
  1395   by (fact compl_eq_compl_iff) (* FIXME: already simp *)
  1396 
  1397 lemma Compl_insert: "- insert x A = (- A) - {x}"
  1398   by blast
  1399 
  1400 text \<open>\<^medskip> Bounded quantifiers.
  1401 
  1402   The following are not added to the default simpset because
  1403   (a) they duplicate the body and (b) there are no similar rules for \<open>Int\<close>.
  1404 \<close>
  1405 
  1406 lemma ball_Un: "(\<forall>x \<in> A \<union> B. P x) \<longleftrightarrow> (\<forall>x\<in>A. P x) \<and> (\<forall>x\<in>B. P x)"
  1407   by blast
  1408 
  1409 lemma bex_Un: "(\<exists>x \<in> A \<union> B. P x) \<longleftrightarrow> (\<exists>x\<in>A. P x) \<or> (\<exists>x\<in>B. P x)"
  1410   by blast
  1411 
  1412 
  1413 text \<open>\<^medskip> Set difference.\<close>
  1414 
  1415 lemma Diff_eq: "A - B = A \<inter> (- B)"
  1416   by blast
  1417 
  1418 lemma Diff_eq_empty_iff [simp]: "A - B = {} \<longleftrightarrow> A \<subseteq> B"
  1419   by blast
  1420 
  1421 lemma Diff_cancel [simp]: "A - A = {}"
  1422   by blast
  1423 
  1424 lemma Diff_idemp [simp]: "(A - B) - B = A - B"
  1425   for A B :: "'a set"
  1426   by blast
  1427 
  1428 lemma Diff_triv: "A \<inter> B = {} \<Longrightarrow> A - B = A"
  1429   by (blast elim: equalityE)
  1430 
  1431 lemma empty_Diff [simp]: "{} - A = {}"
  1432   by blast
  1433 
  1434 lemma Diff_empty [simp]: "A - {} = A"
  1435   by blast
  1436 
  1437 lemma Diff_UNIV [simp]: "A - UNIV = {}"
  1438   by blast
  1439 
  1440 lemma Diff_insert0 [simp]: "x \<notin> A \<Longrightarrow> A - insert x B = A - B"
  1441   by blast
  1442 
  1443 lemma Diff_insert: "A - insert a B = A - B - {a}"
  1444   \<comment> \<open>NOT SUITABLE FOR REWRITING since \<open>{a} \<equiv> insert a 0\<close>\<close>
  1445   by blast
  1446 
  1447 lemma Diff_insert2: "A - insert a B = A - {a} - B"
  1448   \<comment> \<open>NOT SUITABLE FOR REWRITING since \<open>{a} \<equiv> insert a 0\<close>\<close>
  1449   by blast
  1450 
  1451 lemma insert_Diff_if: "insert x A - B = (if x \<in> B then A - B else insert x (A - B))"
  1452   by auto
  1453 
  1454 lemma insert_Diff1 [simp]: "x \<in> B \<Longrightarrow> insert x A - B = A - B"
  1455   by blast
  1456 
  1457 lemma insert_Diff_single[simp]: "insert a (A - {a}) = insert a A"
  1458   by blast
  1459 
  1460 lemma insert_Diff: "a \<in> A \<Longrightarrow> insert a (A - {a}) = A"
  1461   by blast
  1462 
  1463 lemma Diff_insert_absorb: "x \<notin> A \<Longrightarrow> (insert x A) - {x} = A"
  1464   by auto
  1465 
  1466 lemma Diff_disjoint [simp]: "A \<inter> (B - A) = {}"
  1467   by blast
  1468 
  1469 lemma Diff_partition: "A \<subseteq> B \<Longrightarrow> A \<union> (B - A) = B"
  1470   by blast
  1471 
  1472 lemma double_diff: "A \<subseteq> B \<Longrightarrow> B \<subseteq> C \<Longrightarrow> B - (C - A) = A"
  1473   by blast
  1474 
  1475 lemma Un_Diff_cancel [simp]: "A \<union> (B - A) = A \<union> B"
  1476   by blast
  1477 
  1478 lemma Un_Diff_cancel2 [simp]: "(B - A) \<union> A = B \<union> A"
  1479   by blast
  1480 
  1481 lemma Diff_Un: "A - (B \<union> C) = (A - B) \<inter> (A - C)"
  1482   by blast
  1483 
  1484 lemma Diff_Int: "A - (B \<inter> C) = (A - B) \<union> (A - C)"
  1485   by blast
  1486 
  1487 lemma Diff_Diff_Int: "A - (A - B) = A \<inter> B"
  1488   by blast
  1489 
  1490 lemma Un_Diff: "(A \<union> B) - C = (A - C) \<union> (B - C)"
  1491   by blast
  1492 
  1493 lemma Int_Diff: "(A \<inter> B) - C = A \<inter> (B - C)"
  1494   by blast
  1495 
  1496 lemma Diff_Int_distrib: "C \<inter> (A - B) = (C \<inter> A) - (C \<inter> B)"
  1497   by blast
  1498 
  1499 lemma Diff_Int_distrib2: "(A - B) \<inter> C = (A \<inter> C) - (B \<inter> C)"
  1500   by blast
  1501 
  1502 lemma Diff_Compl [simp]: "A - (- B) = A \<inter> B"
  1503   by auto
  1504 
  1505 lemma Compl_Diff_eq [simp]: "- (A - B) = - A \<union> B"
  1506   by blast
  1507 
  1508 lemma subset_Compl_singleton [simp]: "A \<subseteq> - {b} \<longleftrightarrow> b \<notin> A"
  1509   by blast
  1510 
  1511 text \<open>\<^medskip> Quantification over type @{typ bool}.\<close>
  1512 
  1513 lemma bool_induct: "P True \<Longrightarrow> P False \<Longrightarrow> P x"
  1514   by (cases x) auto
  1515 
  1516 lemma all_bool_eq: "(\<forall>b. P b) \<longleftrightarrow> P True \<and> P False"
  1517   by (auto intro: bool_induct)
  1518 
  1519 lemma bool_contrapos: "P x \<Longrightarrow> \<not> P False \<Longrightarrow> P True"
  1520   by (cases x) auto
  1521 
  1522 lemma ex_bool_eq: "(\<exists>b. P b) \<longleftrightarrow> P True \<or> P False"
  1523   by (auto intro: bool_contrapos)
  1524 
  1525 lemma UNIV_bool: "UNIV = {False, True}"
  1526   by (auto intro: bool_induct)
  1527 
  1528 text \<open>\<^medskip> \<open>Pow\<close>\<close>
  1529 
  1530 lemma Pow_empty [simp]: "Pow {} = {{}}"
  1531   by (auto simp add: Pow_def)
  1532 
  1533 lemma Pow_singleton_iff [simp]: "Pow X = {Y} \<longleftrightarrow> X = {} \<and> Y = {}"
  1534   by blast  (* somewhat slow *)
  1535 
  1536 lemma Pow_insert: "Pow (insert a A) = Pow A \<union> (insert a ` Pow A)"
  1537   by (blast intro: image_eqI [where ?x = "u - {a}" for u])
  1538 
  1539 lemma Pow_Compl: "Pow (- A) = {- B | B. A \<in> Pow B}"
  1540   by (blast intro: exI [where ?x = "- u" for u])
  1541 
  1542 lemma Pow_UNIV [simp]: "Pow UNIV = UNIV"
  1543   by blast
  1544 
  1545 lemma Un_Pow_subset: "Pow A \<union> Pow B \<subseteq> Pow (A \<union> B)"
  1546   by blast
  1547 
  1548 lemma Pow_Int_eq [simp]: "Pow (A \<inter> B) = Pow A \<inter> Pow B"
  1549   by blast
  1550 
  1551 
  1552 text \<open>\<^medskip> Miscellany.\<close>
  1553 
  1554 lemma set_eq_subset: "A = B \<longleftrightarrow> A \<subseteq> B \<and> B \<subseteq> A"
  1555   by blast
  1556 
  1557 lemma subset_iff: "A \<subseteq> B \<longleftrightarrow> (\<forall>t. t \<in> A \<longrightarrow> t \<in> B)"
  1558   by blast
  1559 
  1560 lemma subset_iff_psubset_eq: "A \<subseteq> B \<longleftrightarrow> A \<subset> B \<or> A = B"
  1561   unfolding less_le by blast
  1562 
  1563 lemma all_not_in_conv [simp]: "(\<forall>x. x \<notin> A) \<longleftrightarrow> A = {}"
  1564   by blast
  1565 
  1566 lemma ex_in_conv: "(\<exists>x. x \<in> A) \<longleftrightarrow> A \<noteq> {}"
  1567   by blast
  1568 
  1569 lemma ball_simps [simp, no_atp]:
  1570   "\<And>A P Q. (\<forall>x\<in>A. P x \<or> Q) \<longleftrightarrow> ((\<forall>x\<in>A. P x) \<or> Q)"
  1571   "\<And>A P Q. (\<forall>x\<in>A. P \<or> Q x) \<longleftrightarrow> (P \<or> (\<forall>x\<in>A. Q x))"
  1572   "\<And>A P Q. (\<forall>x\<in>A. P \<longrightarrow> Q x) \<longleftrightarrow> (P \<longrightarrow> (\<forall>x\<in>A. Q x))"
  1573   "\<And>A P Q. (\<forall>x\<in>A. P x \<longrightarrow> Q) \<longleftrightarrow> ((\<exists>x\<in>A. P x) \<longrightarrow> Q)"
  1574   "\<And>P. (\<forall>x\<in>{}. P x) \<longleftrightarrow> True"
  1575   "\<And>P. (\<forall>x\<in>UNIV. P x) \<longleftrightarrow> (\<forall>x. P x)"
  1576   "\<And>a B P. (\<forall>x\<in>insert a B. P x) \<longleftrightarrow> (P a \<and> (\<forall>x\<in>B. P x))"
  1577   "\<And>P Q. (\<forall>x\<in>Collect Q. P x) \<longleftrightarrow> (\<forall>x. Q x \<longrightarrow> P x)"
  1578   "\<And>A P f. (\<forall>x\<in>f`A. P x) \<longleftrightarrow> (\<forall>x\<in>A. P (f x))"
  1579   "\<And>A P. (\<not> (\<forall>x\<in>A. P x)) \<longleftrightarrow> (\<exists>x\<in>A. \<not> P x)"
  1580   by auto
  1581 
  1582 lemma bex_simps [simp, no_atp]:
  1583   "\<And>A P Q. (\<exists>x\<in>A. P x \<and> Q) \<longleftrightarrow> ((\<exists>x\<in>A. P x) \<and> Q)"
  1584   "\<And>A P Q. (\<exists>x\<in>A. P \<and> Q x) \<longleftrightarrow> (P \<and> (\<exists>x\<in>A. Q x))"
  1585   "\<And>P. (\<exists>x\<in>{}. P x) \<longleftrightarrow> False"
  1586   "\<And>P. (\<exists>x\<in>UNIV. P x) \<longleftrightarrow> (\<exists>x. P x)"
  1587   "\<And>a B P. (\<exists>x\<in>insert a B. P x) \<longleftrightarrow> (P a \<or> (\<exists>x\<in>B. P x))"
  1588   "\<And>P Q. (\<exists>x\<in>Collect Q. P x) \<longleftrightarrow> (\<exists>x. Q x \<and> P x)"
  1589   "\<And>A P f. (\<exists>x\<in>f`A. P x) \<longleftrightarrow> (\<exists>x\<in>A. P (f x))"
  1590   "\<And>A P. (\<not>(\<exists>x\<in>A. P x)) \<longleftrightarrow> (\<forall>x\<in>A. \<not> P x)"
  1591   by auto
  1592 
  1593 
  1594 subsubsection \<open>Monotonicity of various operations\<close>
  1595 
  1596 lemma image_mono: "A \<subseteq> B \<Longrightarrow> f ` A \<subseteq> f ` B"
  1597   by blast
  1598 
  1599 lemma Pow_mono: "A \<subseteq> B \<Longrightarrow> Pow A \<subseteq> Pow B"
  1600   by blast
  1601 
  1602 lemma insert_mono: "C \<subseteq> D \<Longrightarrow> insert a C \<subseteq> insert a D"
  1603   by blast
  1604 
  1605 lemma Un_mono: "A \<subseteq> C \<Longrightarrow> B \<subseteq> D \<Longrightarrow> A \<union> B \<subseteq> C \<union> D"
  1606   by (fact sup_mono)
  1607 
  1608 lemma Int_mono: "A \<subseteq> C \<Longrightarrow> B \<subseteq> D \<Longrightarrow> A \<inter> B \<subseteq> C \<inter> D"
  1609   by (fact inf_mono)
  1610 
  1611 lemma Diff_mono: "A \<subseteq> C \<Longrightarrow> D \<subseteq> B \<Longrightarrow> A - B \<subseteq> C - D"
  1612   by blast
  1613 
  1614 lemma Compl_anti_mono: "A \<subseteq> B \<Longrightarrow> - B \<subseteq> - A"
  1615   by (fact compl_mono)
  1616 
  1617 text \<open>\<^medskip> Monotonicity of implications.\<close>
  1618 
  1619 lemma in_mono: "A \<subseteq> B \<Longrightarrow> x \<in> A \<longrightarrow> x \<in> B"
  1620   by (rule impI) (erule subsetD)
  1621 
  1622 lemma conj_mono: "P1 \<longrightarrow> Q1 \<Longrightarrow> P2 \<longrightarrow> Q2 \<Longrightarrow> (P1 \<and> P2) \<longrightarrow> (Q1 \<and> Q2)"
  1623   by iprover
  1624 
  1625 lemma disj_mono: "P1 \<longrightarrow> Q1 \<Longrightarrow> P2 \<longrightarrow> Q2 \<Longrightarrow> (P1 \<or> P2) \<longrightarrow> (Q1 \<or> Q2)"
  1626   by iprover
  1627 
  1628 lemma imp_mono: "Q1 \<longrightarrow> P1 \<Longrightarrow> P2 \<longrightarrow> Q2 \<Longrightarrow> (P1 \<longrightarrow> P2) \<longrightarrow> (Q1 \<longrightarrow> Q2)"
  1629   by iprover
  1630 
  1631 lemma imp_refl: "P \<longrightarrow> P" ..
  1632 
  1633 lemma not_mono: "Q \<longrightarrow> P \<Longrightarrow> \<not> P \<longrightarrow> \<not> Q"
  1634   by iprover
  1635 
  1636 lemma ex_mono: "(\<And>x. P x \<longrightarrow> Q x) \<Longrightarrow> (\<exists>x. P x) \<longrightarrow> (\<exists>x. Q x)"
  1637   by iprover
  1638 
  1639 lemma all_mono: "(\<And>x. P x \<longrightarrow> Q x) \<Longrightarrow> (\<forall>x. P x) \<longrightarrow> (\<forall>x. Q x)"
  1640   by iprover
  1641 
  1642 lemma Collect_mono: "(\<And>x. P x \<longrightarrow> Q x) \<Longrightarrow> Collect P \<subseteq> Collect Q"
  1643   by blast
  1644 
  1645 lemma Int_Collect_mono: "A \<subseteq> B \<Longrightarrow> (\<And>x. x \<in> A \<Longrightarrow> P x \<longrightarrow> Q x) \<Longrightarrow> A \<inter> Collect P \<subseteq> B \<inter> Collect Q"
  1646   by blast
  1647 
  1648 lemmas basic_monos =
  1649   subset_refl imp_refl disj_mono conj_mono ex_mono Collect_mono in_mono
  1650 
  1651 lemma eq_to_mono: "a = b \<Longrightarrow> c = d \<Longrightarrow> b \<longrightarrow> d \<Longrightarrow> a \<longrightarrow> c"
  1652   by iprover
  1653 
  1654 
  1655 subsubsection \<open>Inverse image of a function\<close>
  1656 
  1657 definition vimage :: "('a \<Rightarrow> 'b) \<Rightarrow> 'b set \<Rightarrow> 'a set"  (infixr "-`" 90)
  1658   where "f -` B \<equiv> {x. f x \<in> B}"
  1659 
  1660 lemma vimage_eq [simp]: "a \<in> f -` B \<longleftrightarrow> f a \<in> B"
  1661   unfolding vimage_def by blast
  1662 
  1663 lemma vimage_singleton_eq: "a \<in> f -` {b} \<longleftrightarrow> f a = b"
  1664   by simp
  1665 
  1666 lemma vimageI [intro]: "f a = b \<Longrightarrow> b \<in> B \<Longrightarrow> a \<in> f -` B"
  1667   unfolding vimage_def by blast
  1668 
  1669 lemma vimageI2: "f a \<in> A \<Longrightarrow> a \<in> f -` A"
  1670   unfolding vimage_def by fast
  1671 
  1672 lemma vimageE [elim!]: "a \<in> f -` B \<Longrightarrow> (\<And>x. f a = x \<Longrightarrow> x \<in> B \<Longrightarrow> P) \<Longrightarrow> P"
  1673   unfolding vimage_def by blast
  1674 
  1675 lemma vimageD: "a \<in> f -` A \<Longrightarrow> f a \<in> A"
  1676   unfolding vimage_def by fast
  1677 
  1678 lemma vimage_empty [simp]: "f -` {} = {}"
  1679   by blast
  1680 
  1681 lemma vimage_Compl: "f -` (- A) = - (f -` A)"
  1682   by blast
  1683 
  1684 lemma vimage_Un [simp]: "f -` (A \<union> B) = (f -` A) \<union> (f -` B)"
  1685   by blast
  1686 
  1687 lemma vimage_Int [simp]: "f -` (A \<inter> B) = (f -` A) \<inter> (f -` B)"
  1688   by fast
  1689 
  1690 lemma vimage_Collect_eq [simp]: "f -` Collect P = {y. P (f y)}"
  1691   by blast
  1692 
  1693 lemma vimage_Collect: "(\<And>x. P (f x) = Q x) \<Longrightarrow> f -` (Collect P) = Collect Q"
  1694   by blast
  1695 
  1696 lemma vimage_insert: "f -` (insert a B) = (f -` {a}) \<union> (f -` B)"
  1697   \<comment> \<open>NOT suitable for rewriting because of the recurrence of \<open>{a}\<close>.\<close>
  1698   by blast
  1699 
  1700 lemma vimage_Diff: "f -` (A - B) = (f -` A) - (f -` B)"
  1701   by blast
  1702 
  1703 lemma vimage_UNIV [simp]: "f -` UNIV = UNIV"
  1704   by blast
  1705 
  1706 lemma vimage_mono: "A \<subseteq> B \<Longrightarrow> f -` A \<subseteq> f -` B"
  1707   \<comment> \<open>monotonicity\<close>
  1708   by blast
  1709 
  1710 lemma vimage_image_eq: "f -` (f ` A) = {y. \<exists>x\<in>A. f x = f y}"
  1711   by (blast intro: sym)
  1712 
  1713 lemma image_vimage_subset: "f ` (f -` A) \<subseteq> A"
  1714   by blast
  1715 
  1716 lemma image_vimage_eq [simp]: "f ` (f -` A) = A \<inter> range f"
  1717   by blast
  1718 
  1719 lemma image_subset_iff_subset_vimage: "f ` A \<subseteq> B \<longleftrightarrow> A \<subseteq> f -` B"
  1720   by blast
  1721 
  1722 lemma vimage_const [simp]: "((\<lambda>x. c) -` A) = (if c \<in> A then UNIV else {})"
  1723   by auto
  1724 
  1725 lemma vimage_if [simp]: "((\<lambda>x. if x \<in> B then c else d) -` A) =
  1726    (if c \<in> A then (if d \<in> A then UNIV else B)
  1727     else if d \<in> A then - B else {})"
  1728   by (auto simp add: vimage_def)
  1729 
  1730 lemma vimage_inter_cong: "(\<And> w. w \<in> S \<Longrightarrow> f w = g w) \<Longrightarrow> f -` y \<inter> S = g -` y \<inter> S"
  1731   by auto
  1732 
  1733 lemma vimage_ident [simp]: "(\<lambda>x. x) -` Y = Y"
  1734   by blast
  1735 
  1736 
  1737 subsubsection \<open>Singleton sets\<close>
  1738 
  1739 definition is_singleton :: "'a set \<Rightarrow> bool"
  1740   where "is_singleton A \<longleftrightarrow> (\<exists>x. A = {x})"
  1741 
  1742 lemma is_singletonI [simp, intro!]: "is_singleton {x}"
  1743   unfolding is_singleton_def by simp
  1744 
  1745 lemma is_singletonI': "A \<noteq> {} \<Longrightarrow> (\<And>x y. x \<in> A \<Longrightarrow> y \<in> A \<Longrightarrow> x = y) \<Longrightarrow> is_singleton A"
  1746   unfolding is_singleton_def by blast
  1747 
  1748 lemma is_singletonE: "is_singleton A \<Longrightarrow> (\<And>x. A = {x} \<Longrightarrow> P) \<Longrightarrow> P"
  1749   unfolding is_singleton_def by blast
  1750 
  1751 
  1752 subsubsection \<open>Getting the contents of a singleton set\<close>
  1753 
  1754 definition the_elem :: "'a set \<Rightarrow> 'a"
  1755   where "the_elem X = (THE x. X = {x})"
  1756 
  1757 lemma the_elem_eq [simp]: "the_elem {x} = x"
  1758   by (simp add: the_elem_def)
  1759 
  1760 lemma is_singleton_the_elem: "is_singleton A \<longleftrightarrow> A = {the_elem A}"
  1761   by (auto simp: is_singleton_def)
  1762 
  1763 lemma the_elem_image_unique:
  1764   assumes "A \<noteq> {}"
  1765     and *: "\<And>y. y \<in> A \<Longrightarrow> f y = f x"
  1766   shows "the_elem (f ` A) = f x"
  1767   unfolding the_elem_def
  1768 proof (rule the1_equality)
  1769   from \<open>A \<noteq> {}\<close> obtain y where "y \<in> A" by auto
  1770   with * have "f x = f y" by simp
  1771   with \<open>y \<in> A\<close> have "f x \<in> f ` A" by blast
  1772   with * show "f ` A = {f x}" by auto
  1773   then show "\<exists>!x. f ` A = {x}" by auto
  1774 qed
  1775 
  1776 
  1777 subsubsection \<open>Least value operator\<close>
  1778 
  1779 lemma Least_mono: "mono f \<Longrightarrow> \<exists>x\<in>S. \<forall>y\<in>S. x \<le> y \<Longrightarrow> (LEAST y. y \<in> f ` S) = f (LEAST x. x \<in> S)"
  1780   for f :: "'a::order \<Rightarrow> 'b::order"
  1781   \<comment> \<open>Courtesy of Stephan Merz\<close>
  1782   apply clarify
  1783   apply (erule_tac P = "\<lambda>x. x : S" in LeastI2_order)
  1784    apply fast
  1785   apply (rule LeastI2_order)
  1786     apply (auto elim: monoD intro!: order_antisym)
  1787   done
  1788 
  1789 
  1790 subsubsection \<open>Monad operation\<close>
  1791 
  1792 definition bind :: "'a set \<Rightarrow> ('a \<Rightarrow> 'b set) \<Rightarrow> 'b set"
  1793   where "bind A f = {x. \<exists>B \<in> f`A. x \<in> B}"
  1794 
  1795 hide_const (open) bind
  1796 
  1797 lemma bind_bind: "Set.bind (Set.bind A B) C = Set.bind A (\<lambda>x. Set.bind (B x) C)"
  1798   for A :: "'a set"
  1799   by (auto simp: bind_def)
  1800 
  1801 lemma empty_bind [simp]: "Set.bind {} f = {}"
  1802   by (simp add: bind_def)
  1803 
  1804 lemma nonempty_bind_const: "A \<noteq> {} \<Longrightarrow> Set.bind A (\<lambda>_. B) = B"
  1805   by (auto simp: bind_def)
  1806 
  1807 lemma bind_const: "Set.bind A (\<lambda>_. B) = (if A = {} then {} else B)"
  1808   by (auto simp: bind_def)
  1809 
  1810 lemma bind_singleton_conv_image: "Set.bind A (\<lambda>x. {f x}) = f ` A"
  1811   by (auto simp: bind_def)
  1812 
  1813 
  1814 subsubsection \<open>Operations for execution\<close>
  1815 
  1816 definition is_empty :: "'a set \<Rightarrow> bool"
  1817   where [code_abbrev]: "is_empty A \<longleftrightarrow> A = {}"
  1818 
  1819 hide_const (open) is_empty
  1820 
  1821 definition remove :: "'a \<Rightarrow> 'a set \<Rightarrow> 'a set"
  1822   where [code_abbrev]: "remove x A = A - {x}"
  1823 
  1824 hide_const (open) remove
  1825 
  1826 lemma member_remove [simp]: "x \<in> Set.remove y A \<longleftrightarrow> x \<in> A \<and> x \<noteq> y"
  1827   by (simp add: remove_def)
  1828 
  1829 definition filter :: "('a \<Rightarrow> bool) \<Rightarrow> 'a set \<Rightarrow> 'a set"
  1830   where [code_abbrev]: "filter P A = {a \<in> A. P a}"
  1831 
  1832 hide_const (open) filter
  1833 
  1834 lemma member_filter [simp]: "x \<in> Set.filter P A \<longleftrightarrow> x \<in> A \<and> P x"
  1835   by (simp add: filter_def)
  1836 
  1837 instantiation set :: (equal) equal
  1838 begin
  1839 
  1840 definition "HOL.equal A B \<longleftrightarrow> A \<subseteq> B \<and> B \<subseteq> A"
  1841 
  1842 instance by standard (auto simp add: equal_set_def)
  1843 
  1844 end
  1845 
  1846 
  1847 text \<open>Misc\<close>
  1848 
  1849 definition pairwise :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a set \<Rightarrow> bool"
  1850   where "pairwise R S \<longleftrightarrow> (\<forall>x \<in> S. \<forall>y \<in> S. x \<noteq> y \<longrightarrow> R x y)"
  1851 
  1852 lemma pairwiseI:
  1853   "pairwise R S" if "\<And>x y. x \<in> S \<Longrightarrow> y \<in> S \<Longrightarrow> x \<noteq> y \<Longrightarrow> R x y"
  1854   using that by (simp add: pairwise_def)
  1855 
  1856 lemma pairwiseD:
  1857   "R x y" and "R y x"
  1858   if "pairwise R S" "x \<in> S" and "y \<in> S" and "x \<noteq> y"
  1859   using that by (simp_all add: pairwise_def)
  1860 
  1861 lemma pairwise_empty [simp]: "pairwise P {}"
  1862   by (simp add: pairwise_def)
  1863 
  1864 lemma pairwise_singleton [simp]: "pairwise P {A}"
  1865   by (simp add: pairwise_def)
  1866 
  1867 lemma pairwise_insert:
  1868   "pairwise r (insert x s) \<longleftrightarrow> (\<forall>y. y \<in> s \<and> y \<noteq> x \<longrightarrow> r x y \<and> r y x) \<and> pairwise r s"
  1869   by (force simp: pairwise_def)
  1870 
  1871 lemma pairwise_subset: "pairwise P S \<Longrightarrow> T \<subseteq> S \<Longrightarrow> pairwise P T"
  1872   by (force simp: pairwise_def)
  1873 
  1874 lemma pairwise_mono: "\<lbrakk>pairwise P A; \<And>x y. P x y \<Longrightarrow> Q x y\<rbrakk> \<Longrightarrow> pairwise Q A"
  1875   by (auto simp: pairwise_def)
  1876 
  1877 lemma pairwise_imageI:
  1878   "pairwise P (f ` A)"
  1879   if "\<And>x y. x \<in> A \<Longrightarrow> y \<in> A \<Longrightarrow> x \<noteq> y \<Longrightarrow> f x \<noteq> f y \<Longrightarrow> P (f x) (f y)"
  1880   using that by (auto intro: pairwiseI)
  1881 
  1882 lemma pairwise_image: "pairwise r (f ` s) \<longleftrightarrow> pairwise (\<lambda>x y. (f x \<noteq> f y) \<longrightarrow> r (f x) (f y)) s"
  1883   by (force simp: pairwise_def)
  1884 
  1885 definition disjnt :: "'a set \<Rightarrow> 'a set \<Rightarrow> bool"
  1886   where "disjnt A B \<longleftrightarrow> A \<inter> B = {}"
  1887 
  1888 lemma disjnt_self_iff_empty [simp]: "disjnt S S \<longleftrightarrow> S = {}"
  1889   by (auto simp: disjnt_def)
  1890 
  1891 lemma disjnt_iff: "disjnt A B \<longleftrightarrow> (\<forall>x. \<not> (x \<in> A \<and> x \<in> B))"
  1892   by (force simp: disjnt_def)
  1893 
  1894 lemma disjnt_sym: "disjnt A B \<Longrightarrow> disjnt B A"
  1895   using disjnt_iff by blast
  1896 
  1897 lemma disjnt_empty1 [simp]: "disjnt {} A" and disjnt_empty2 [simp]: "disjnt A {}"
  1898   by (auto simp: disjnt_def)
  1899 
  1900 lemma disjnt_insert1 [simp]: "disjnt (insert a X) Y \<longleftrightarrow> a \<notin> Y \<and> disjnt X Y"
  1901   by (simp add: disjnt_def)
  1902 
  1903 lemma disjnt_insert2 [simp]: "disjnt Y (insert a X) \<longleftrightarrow> a \<notin> Y \<and> disjnt Y X"
  1904   by (simp add: disjnt_def)
  1905 
  1906 lemma disjnt_subset1 : "\<lbrakk>disjnt X Y; Z \<subseteq> X\<rbrakk> \<Longrightarrow> disjnt Z Y"
  1907   by (auto simp: disjnt_def)
  1908 
  1909 lemma disjnt_subset2 : "\<lbrakk>disjnt X Y; Z \<subseteq> Y\<rbrakk> \<Longrightarrow> disjnt X Z"
  1910   by (auto simp: disjnt_def)
  1911 
  1912 lemma disjoint_image_subset: "\<lbrakk>pairwise disjnt \<A>; \<And>X. X \<in> \<A> \<Longrightarrow> f X \<subseteq> X\<rbrakk> \<Longrightarrow> pairwise disjnt (f `\<A>)"
  1913   unfolding disjnt_def pairwise_def by fast
  1914 
  1915 lemma Int_emptyI: "(\<And>x. x \<in> A \<Longrightarrow> x \<in> B \<Longrightarrow> False) \<Longrightarrow> A \<inter> B = {}"
  1916   by blast
  1917 
  1918 lemma in_image_insert_iff:
  1919   assumes "\<And>C. C \<in> B \<Longrightarrow> x \<notin> C"
  1920   shows "A \<in> insert x ` B \<longleftrightarrow> x \<in> A \<and> A - {x} \<in> B" (is "?P \<longleftrightarrow> ?Q")
  1921 proof
  1922   assume ?P then show ?Q
  1923     using assms by auto
  1924 next
  1925   assume ?Q
  1926   then have "x \<in> A" and "A - {x} \<in> B"
  1927     by simp_all
  1928   from \<open>A - {x} \<in> B\<close> have "insert x (A - {x}) \<in> insert x ` B"
  1929     by (rule imageI)
  1930   also from \<open>x \<in> A\<close>
  1931   have "insert x (A - {x}) = A"
  1932     by auto
  1933   finally show ?P .
  1934 qed
  1935 
  1936 hide_const (open) member not_member
  1937 
  1938 lemmas equalityI = subset_antisym
  1939 
  1940 ML \<open>
  1941 val Ball_def = @{thm Ball_def}
  1942 val Bex_def = @{thm Bex_def}
  1943 val CollectD = @{thm CollectD}
  1944 val CollectE = @{thm CollectE}
  1945 val CollectI = @{thm CollectI}
  1946 val Collect_conj_eq = @{thm Collect_conj_eq}
  1947 val Collect_mem_eq = @{thm Collect_mem_eq}
  1948 val IntD1 = @{thm IntD1}
  1949 val IntD2 = @{thm IntD2}
  1950 val IntE = @{thm IntE}
  1951 val IntI = @{thm IntI}
  1952 val Int_Collect = @{thm Int_Collect}
  1953 val UNIV_I = @{thm UNIV_I}
  1954 val UNIV_witness = @{thm UNIV_witness}
  1955 val UnE = @{thm UnE}
  1956 val UnI1 = @{thm UnI1}
  1957 val UnI2 = @{thm UnI2}
  1958 val ballE = @{thm ballE}
  1959 val ballI = @{thm ballI}
  1960 val bexCI = @{thm bexCI}
  1961 val bexE = @{thm bexE}
  1962 val bexI = @{thm bexI}
  1963 val bex_triv = @{thm bex_triv}
  1964 val bspec = @{thm bspec}
  1965 val contra_subsetD = @{thm contra_subsetD}
  1966 val equalityCE = @{thm equalityCE}
  1967 val equalityD1 = @{thm equalityD1}
  1968 val equalityD2 = @{thm equalityD2}
  1969 val equalityE = @{thm equalityE}
  1970 val equalityI = @{thm equalityI}
  1971 val imageE = @{thm imageE}
  1972 val imageI = @{thm imageI}
  1973 val image_Un = @{thm image_Un}
  1974 val image_insert = @{thm image_insert}
  1975 val insert_commute = @{thm insert_commute}
  1976 val insert_iff = @{thm insert_iff}
  1977 val mem_Collect_eq = @{thm mem_Collect_eq}
  1978 val rangeE = @{thm rangeE}
  1979 val rangeI = @{thm rangeI}
  1980 val range_eqI = @{thm range_eqI}
  1981 val subsetCE = @{thm subsetCE}
  1982 val subsetD = @{thm subsetD}
  1983 val subsetI = @{thm subsetI}
  1984 val subset_refl = @{thm subset_refl}
  1985 val subset_trans = @{thm subset_trans}
  1986 val vimageD = @{thm vimageD}
  1987 val vimageE = @{thm vimageE}
  1988 val vimageI = @{thm vimageI}
  1989 val vimageI2 = @{thm vimageI2}
  1990 val vimage_Collect = @{thm vimage_Collect}
  1991 val vimage_Int = @{thm vimage_Int}
  1992 val vimage_Un = @{thm vimage_Un}
  1993 \<close>
  1994 
  1995 end