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