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