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