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