src/HOL/Set.thy
author haftmann
Sun Jul 17 19:48:02 2011 +0200 (2011-07-17)
changeset 43866 8a50dc70cbff
parent 43818 fcc5d3ffb6f5
child 43898 935359fd8210
permissions -rw-r--r--
moving UNIV = ... equations to their proper theories
     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 lemma ball_conj_distrib:
   420   "(\<forall>x\<in>A. P x \<and> Q x) \<longleftrightarrow> ((\<forall>x\<in>A. P x) \<and> (\<forall>x\<in>A. Q x))"
   421   by blast
   422 
   423 lemma bex_disj_distrib:
   424   "(\<exists>x\<in>A. P x \<or> Q x) \<longleftrightarrow> ((\<exists>x\<in>A. P x) \<or> (\<exists>x\<in>A. Q x))"
   425   by blast
   426 
   427 
   428 text {* Congruence rules *}
   429 
   430 lemma ball_cong:
   431   "A = B ==> (!!x. x:B ==> P x = Q x) ==>
   432     (ALL x:A. P x) = (ALL x:B. Q x)"
   433   by (simp add: Ball_def)
   434 
   435 lemma strong_ball_cong [cong]:
   436   "A = B ==> (!!x. x:B =simp=> P x = Q x) ==>
   437     (ALL x:A. P x) = (ALL x:B. Q x)"
   438   by (simp add: simp_implies_def Ball_def)
   439 
   440 lemma bex_cong:
   441   "A = B ==> (!!x. x:B ==> P x = Q x) ==>
   442     (EX x:A. P x) = (EX x:B. Q x)"
   443   by (simp add: Bex_def cong: conj_cong)
   444 
   445 lemma strong_bex_cong [cong]:
   446   "A = B ==> (!!x. x:B =simp=> P x = Q x) ==>
   447     (EX x:A. P x) = (EX x:B. Q x)"
   448   by (simp add: simp_implies_def Bex_def cong: conj_cong)
   449 
   450 
   451 subsection {* Basic operations *}
   452 
   453 subsubsection {* Subsets *}
   454 
   455 lemma subsetI [intro!]: "(\<And>x. x \<in> A \<Longrightarrow> x \<in> B) \<Longrightarrow> A \<subseteq> B"
   456   unfolding mem_def by (rule le_funI, rule le_boolI)
   457 
   458 text {*
   459   \medskip Map the type @{text "'a set => anything"} to just @{typ
   460   'a}; for overloading constants whose first argument has type @{typ
   461   "'a set"}.
   462 *}
   463 
   464 lemma subsetD [elim, intro?]: "A \<subseteq> B ==> c \<in> A ==> c \<in> B"
   465   unfolding mem_def by (erule le_funE, erule le_boolE)
   466   -- {* Rule in Modus Ponens style. *}
   467 
   468 lemma rev_subsetD [no_atp,intro?]: "c \<in> A ==> A \<subseteq> B ==> c \<in> B"
   469   -- {* The same, with reversed premises for use with @{text erule} --
   470       cf @{text rev_mp}. *}
   471   by (rule subsetD)
   472 
   473 text {*
   474   \medskip Converts @{prop "A \<subseteq> B"} to @{prop "x \<in> A ==> x \<in> B"}.
   475 *}
   476 
   477 lemma subsetCE [no_atp,elim]: "A \<subseteq> B ==> (c \<notin> A ==> P) ==> (c \<in> B ==> P) ==> P"
   478   -- {* Classical elimination rule. *}
   479   unfolding mem_def by (blast dest: le_funE le_boolE)
   480 
   481 lemma subset_eq [no_atp]: "A \<le> B = (\<forall>x\<in>A. x \<in> B)" by blast
   482 
   483 lemma contra_subsetD [no_atp]: "A \<subseteq> B ==> c \<notin> B ==> c \<notin> A"
   484   by blast
   485 
   486 lemma subset_refl [simp]: "A \<subseteq> A"
   487   by (fact order_refl)
   488 
   489 lemma subset_trans: "A \<subseteq> B ==> B \<subseteq> C ==> A \<subseteq> C"
   490   by (fact order_trans)
   491 
   492 lemma set_rev_mp: "x:A ==> A \<subseteq> B ==> x:B"
   493   by (rule subsetD)
   494 
   495 lemma set_mp: "A \<subseteq> B ==> x:A ==> x:B"
   496   by (rule subsetD)
   497 
   498 lemma eq_mem_trans: "a=b ==> b \<in> A ==> a \<in> A"
   499   by simp
   500 
   501 lemmas basic_trans_rules [trans] =
   502   order_trans_rules set_rev_mp set_mp eq_mem_trans
   503 
   504 
   505 subsubsection {* Equality *}
   506 
   507 lemma subset_antisym [intro!]: "A \<subseteq> B ==> B \<subseteq> A ==> A = B"
   508   -- {* Anti-symmetry of the subset relation. *}
   509   by (iprover intro: set_eqI subsetD)
   510 
   511 text {*
   512   \medskip Equality rules from ZF set theory -- are they appropriate
   513   here?
   514 *}
   515 
   516 lemma equalityD1: "A = B ==> A \<subseteq> B"
   517   by simp
   518 
   519 lemma equalityD2: "A = B ==> B \<subseteq> A"
   520   by simp
   521 
   522 text {*
   523   \medskip Be careful when adding this to the claset as @{text
   524   subset_empty} is in the simpset: @{prop "A = {}"} goes to @{prop "{}
   525   \<subseteq> A"} and @{prop "A \<subseteq> {}"} and then back to @{prop "A = {}"}!
   526 *}
   527 
   528 lemma equalityE: "A = B ==> (A \<subseteq> B ==> B \<subseteq> A ==> P) ==> P"
   529   by simp
   530 
   531 lemma equalityCE [elim]:
   532     "A = B ==> (c \<in> A ==> c \<in> B ==> P) ==> (c \<notin> A ==> c \<notin> B ==> P) ==> P"
   533   by blast
   534 
   535 lemma eqset_imp_iff: "A = B ==> (x : A) = (x : B)"
   536   by simp
   537 
   538 lemma eqelem_imp_iff: "x = y ==> (x : A) = (y : A)"
   539   by simp
   540 
   541 
   542 subsubsection {* The empty set *}
   543 
   544 lemma empty_def:
   545   "{} = {x. False}"
   546   by (simp add: bot_fun_def Collect_def)
   547 
   548 lemma empty_iff [simp]: "(c : {}) = False"
   549   by (simp add: empty_def)
   550 
   551 lemma emptyE [elim!]: "a : {} ==> P"
   552   by simp
   553 
   554 lemma empty_subsetI [iff]: "{} \<subseteq> A"
   555     -- {* One effect is to delete the ASSUMPTION @{prop "{} <= A"} *}
   556   by blast
   557 
   558 lemma equals0I: "(!!y. y \<in> A ==> False) ==> A = {}"
   559   by blast
   560 
   561 lemma equals0D: "A = {} ==> a \<notin> A"
   562     -- {* Use for reasoning about disjointness: @{text "A Int B = {}"} *}
   563   by blast
   564 
   565 lemma ball_empty [simp]: "Ball {} P = True"
   566   by (simp add: Ball_def)
   567 
   568 lemma bex_empty [simp]: "Bex {} P = False"
   569   by (simp add: Bex_def)
   570 
   571 
   572 subsubsection {* The universal set -- UNIV *}
   573 
   574 abbreviation UNIV :: "'a set" where
   575   "UNIV \<equiv> top"
   576 
   577 lemma UNIV_def:
   578   "UNIV = {x. True}"
   579   by (simp add: top_fun_def Collect_def)
   580 
   581 lemma UNIV_I [simp]: "x : UNIV"
   582   by (simp add: UNIV_def)
   583 
   584 declare UNIV_I [intro]  -- {* unsafe makes it less likely to cause problems *}
   585 
   586 lemma UNIV_witness [intro?]: "EX x. x : UNIV"
   587   by simp
   588 
   589 lemma subset_UNIV [simp]: "A \<subseteq> UNIV"
   590   by (rule subsetI) (rule UNIV_I)
   591 
   592 text {*
   593   \medskip Eta-contracting these two rules (to remove @{text P})
   594   causes them to be ignored because of their interaction with
   595   congruence rules.
   596 *}
   597 
   598 lemma ball_UNIV [simp]: "Ball UNIV P = All P"
   599   by (simp add: Ball_def)
   600 
   601 lemma bex_UNIV [simp]: "Bex UNIV P = Ex P"
   602   by (simp add: Bex_def)
   603 
   604 lemma UNIV_eq_I: "(\<And>x. x \<in> A) \<Longrightarrow> UNIV = A"
   605   by auto
   606 
   607 lemma UNIV_not_empty [iff]: "UNIV ~= {}"
   608   by (blast elim: equalityE)
   609 
   610 
   611 subsubsection {* The Powerset operator -- Pow *}
   612 
   613 definition Pow :: "'a set => 'a set set" where
   614   Pow_def: "Pow A = {B. B \<le> A}"
   615 
   616 lemma Pow_iff [iff]: "(A \<in> Pow B) = (A \<subseteq> B)"
   617   by (simp add: Pow_def)
   618 
   619 lemma PowI: "A \<subseteq> B ==> A \<in> Pow B"
   620   by (simp add: Pow_def)
   621 
   622 lemma PowD: "A \<in> Pow B ==> A \<subseteq> B"
   623   by (simp add: Pow_def)
   624 
   625 lemma Pow_bottom: "{} \<in> Pow B"
   626   by simp
   627 
   628 lemma Pow_top: "A \<in> Pow A"
   629   by simp
   630 
   631 lemma Pow_not_empty: "Pow A \<noteq> {}"
   632   using Pow_top by blast
   633 
   634 
   635 subsubsection {* Set complement *}
   636 
   637 lemma Compl_iff [simp]: "(c \<in> -A) = (c \<notin> A)"
   638   by (simp add: mem_def fun_Compl_def)
   639 
   640 lemma ComplI [intro!]: "(c \<in> A ==> False) ==> c \<in> -A"
   641   by (unfold mem_def fun_Compl_def bool_Compl_def) blast
   642 
   643 text {*
   644   \medskip This form, with negated conclusion, works well with the
   645   Classical prover.  Negated assumptions behave like formulae on the
   646   right side of the notional turnstile ... *}
   647 
   648 lemma ComplD [dest!]: "c : -A ==> c~:A"
   649   by (simp add: mem_def fun_Compl_def)
   650 
   651 lemmas ComplE = ComplD [elim_format]
   652 
   653 lemma Compl_eq: "- A = {x. ~ x : A}" by blast
   654 
   655 
   656 subsubsection {* Binary intersection *}
   657 
   658 abbreviation inter :: "'a set \<Rightarrow> 'a set \<Rightarrow> 'a set" (infixl "Int" 70) where
   659   "op Int \<equiv> inf"
   660 
   661 notation (xsymbols)
   662   inter  (infixl "\<inter>" 70)
   663 
   664 notation (HTML output)
   665   inter  (infixl "\<inter>" 70)
   666 
   667 lemma Int_def:
   668   "A \<inter> B = {x. x \<in> A \<and> x \<in> B}"
   669   by (simp add: inf_fun_def Collect_def mem_def)
   670 
   671 lemma Int_iff [simp]: "(c : A Int B) = (c:A & c:B)"
   672   by (unfold Int_def) blast
   673 
   674 lemma IntI [intro!]: "c:A ==> c:B ==> c : A Int B"
   675   by simp
   676 
   677 lemma IntD1: "c : A Int B ==> c:A"
   678   by simp
   679 
   680 lemma IntD2: "c : A Int B ==> c:B"
   681   by simp
   682 
   683 lemma IntE [elim!]: "c : A Int B ==> (c:A ==> c:B ==> P) ==> P"
   684   by simp
   685 
   686 lemma mono_Int: "mono f \<Longrightarrow> f (A \<inter> B) \<subseteq> f A \<inter> f B"
   687   by (fact mono_inf)
   688 
   689 
   690 subsubsection {* Binary union *}
   691 
   692 abbreviation union :: "'a set \<Rightarrow> 'a set \<Rightarrow> 'a set" (infixl "Un" 65) where
   693   "union \<equiv> sup"
   694 
   695 notation (xsymbols)
   696   union  (infixl "\<union>" 65)
   697 
   698 notation (HTML output)
   699   union  (infixl "\<union>" 65)
   700 
   701 lemma Un_def:
   702   "A \<union> B = {x. x \<in> A \<or> x \<in> B}"
   703   by (simp add: sup_fun_def Collect_def mem_def)
   704 
   705 lemma Un_iff [simp]: "(c : A Un B) = (c:A | c:B)"
   706   by (unfold Un_def) blast
   707 
   708 lemma UnI1 [elim?]: "c:A ==> c : A Un B"
   709   by simp
   710 
   711 lemma UnI2 [elim?]: "c:B ==> c : A Un B"
   712   by simp
   713 
   714 text {*
   715   \medskip Classical introduction rule: no commitment to @{prop A} vs
   716   @{prop B}.
   717 *}
   718 
   719 lemma UnCI [intro!]: "(c~:B ==> c:A) ==> c : A Un B"
   720   by auto
   721 
   722 lemma UnE [elim!]: "c : A Un B ==> (c:A ==> P) ==> (c:B ==> P) ==> P"
   723   by (unfold Un_def) blast
   724 
   725 lemma insert_def: "insert a B = {x. x = a} \<union> B"
   726   by (simp add: Collect_def mem_def insert_compr Un_def)
   727 
   728 lemma mono_Un: "mono f \<Longrightarrow> f A \<union> f B \<subseteq> f (A \<union> B)"
   729   by (fact mono_sup)
   730 
   731 
   732 subsubsection {* Set difference *}
   733 
   734 lemma Diff_iff [simp]: "(c : A - B) = (c:A & c~:B)"
   735   by (simp add: mem_def fun_diff_def)
   736 
   737 lemma DiffI [intro!]: "c : A ==> c ~: B ==> c : A - B"
   738   by simp
   739 
   740 lemma DiffD1: "c : A - B ==> c : A"
   741   by simp
   742 
   743 lemma DiffD2: "c : A - B ==> c : B ==> P"
   744   by simp
   745 
   746 lemma DiffE [elim!]: "c : A - B ==> (c:A ==> c~:B ==> P) ==> P"
   747   by simp
   748 
   749 lemma set_diff_eq: "A - B = {x. x : A & ~ x : B}" by blast
   750 
   751 lemma Compl_eq_Diff_UNIV: "-A = (UNIV - A)"
   752 by blast
   753 
   754 
   755 subsubsection {* Augmenting a set -- @{const insert} *}
   756 
   757 lemma insert_iff [simp]: "(a : insert b A) = (a = b | a:A)"
   758   by (unfold insert_def) blast
   759 
   760 lemma insertI1: "a : insert a B"
   761   by simp
   762 
   763 lemma insertI2: "a : B ==> a : insert b B"
   764   by simp
   765 
   766 lemma insertE [elim!]: "a : insert b A ==> (a = b ==> P) ==> (a:A ==> P) ==> P"
   767   by (unfold insert_def) blast
   768 
   769 lemma insertCI [intro!]: "(a~:B ==> a = b) ==> a: insert b B"
   770   -- {* Classical introduction rule. *}
   771   by auto
   772 
   773 lemma subset_insert_iff: "(A \<subseteq> insert x B) = (if x:A then A - {x} \<subseteq> B else A \<subseteq> B)"
   774   by auto
   775 
   776 lemma set_insert:
   777   assumes "x \<in> A"
   778   obtains B where "A = insert x B" and "x \<notin> B"
   779 proof
   780   from assms show "A = insert x (A - {x})" by blast
   781 next
   782   show "x \<notin> A - {x}" by blast
   783 qed
   784 
   785 lemma insert_ident: "x ~: A ==> x ~: B ==> (insert x A = insert x B) = (A = B)"
   786 by auto
   787 
   788 subsubsection {* Singletons, using insert *}
   789 
   790 lemma singletonI [intro!,no_atp]: "a : {a}"
   791     -- {* Redundant? But unlike @{text insertCI}, it proves the subgoal immediately! *}
   792   by (rule insertI1)
   793 
   794 lemma singletonD [dest!,no_atp]: "b : {a} ==> b = a"
   795   by blast
   796 
   797 lemmas singletonE = singletonD [elim_format]
   798 
   799 lemma singleton_iff: "(b : {a}) = (b = a)"
   800   by blast
   801 
   802 lemma singleton_inject [dest!]: "{a} = {b} ==> a = b"
   803   by blast
   804 
   805 lemma singleton_insert_inj_eq [iff,no_atp]:
   806      "({b} = insert a A) = (a = b & A \<subseteq> {b})"
   807   by blast
   808 
   809 lemma singleton_insert_inj_eq' [iff,no_atp]:
   810      "(insert a A = {b}) = (a = b & A \<subseteq> {b})"
   811   by blast
   812 
   813 lemma subset_singletonD: "A \<subseteq> {x} ==> A = {} | A = {x}"
   814   by fast
   815 
   816 lemma singleton_conv [simp]: "{x. x = a} = {a}"
   817   by blast
   818 
   819 lemma singleton_conv2 [simp]: "{x. a = x} = {a}"
   820   by blast
   821 
   822 lemma diff_single_insert: "A - {x} \<subseteq> B ==> x \<in> A ==> A \<subseteq> insert x B"
   823   by blast
   824 
   825 lemma doubleton_eq_iff: "({a,b} = {c,d}) = (a=c & b=d | a=d & b=c)"
   826   by (blast elim: equalityE)
   827 
   828 
   829 subsubsection {* Image of a set under a function *}
   830 
   831 text {*
   832   Frequently @{term b} does not have the syntactic form of @{term "f x"}.
   833 *}
   834 
   835 definition image :: "('a => 'b) => 'a set => 'b set" (infixr "`" 90) where
   836   image_def [no_atp]: "f ` A = {y. EX x:A. y = f(x)}"
   837 
   838 abbreviation
   839   range :: "('a => 'b) => 'b set" where -- "of function"
   840   "range f == f ` UNIV"
   841 
   842 lemma image_eqI [simp, intro]: "b = f x ==> x:A ==> b : f`A"
   843   by (unfold image_def) blast
   844 
   845 lemma imageI: "x : A ==> f x : f ` A"
   846   by (rule image_eqI) (rule refl)
   847 
   848 lemma rev_image_eqI: "x:A ==> b = f x ==> b : f`A"
   849   -- {* This version's more effective when we already have the
   850     required @{term x}. *}
   851   by (unfold image_def) blast
   852 
   853 lemma imageE [elim!]:
   854   "b : (%x. f x)`A ==> (!!x. b = f x ==> x:A ==> P) ==> P"
   855   -- {* The eta-expansion gives variable-name preservation. *}
   856   by (unfold image_def) blast
   857 
   858 lemma image_Un: "f`(A Un B) = f`A Un f`B"
   859   by blast
   860 
   861 lemma image_iff: "(z : f`A) = (EX x:A. z = f x)"
   862   by blast
   863 
   864 lemma image_subset_iff [no_atp]: "(f`A \<subseteq> B) = (\<forall>x\<in>A. f x \<in> B)"
   865   -- {* This rewrite rule would confuse users if made default. *}
   866   by blast
   867 
   868 lemma subset_image_iff: "(B \<subseteq> f`A) = (EX AA. AA \<subseteq> A & B = f`AA)"
   869   apply safe
   870    prefer 2 apply fast
   871   apply (rule_tac x = "{a. a : A & f a : B}" in exI, fast)
   872   done
   873 
   874 lemma image_subsetI: "(!!x. x \<in> A ==> f x \<in> B) ==> f`A \<subseteq> B"
   875   -- {* Replaces the three steps @{text subsetI}, @{text imageE},
   876     @{text hypsubst}, but breaks too many existing proofs. *}
   877   by blast
   878 
   879 text {*
   880   \medskip Range of a function -- just a translation for image!
   881 *}
   882 
   883 lemma range_eqI: "b = f x ==> b \<in> range f"
   884   by simp
   885 
   886 lemma rangeI: "f x \<in> range f"
   887   by simp
   888 
   889 lemma rangeE [elim?]: "b \<in> range (\<lambda>x. f x) ==> (!!x. b = f x ==> P) ==> P"
   890   by blast
   891 
   892 subsubsection {* Some rules with @{text "if"} *}
   893 
   894 text{* Elimination of @{text"{x. \<dots> & x=t & \<dots>}"}. *}
   895 
   896 lemma Collect_conv_if: "{x. x=a & P x} = (if P a then {a} else {})"
   897   by auto
   898 
   899 lemma Collect_conv_if2: "{x. a=x & P x} = (if P a then {a} else {})"
   900   by auto
   901 
   902 text {*
   903   Rewrite rules for boolean case-splitting: faster than @{text
   904   "split_if [split]"}.
   905 *}
   906 
   907 lemma split_if_eq1: "((if Q then x else y) = b) = ((Q --> x = b) & (~ Q --> y = b))"
   908   by (rule split_if)
   909 
   910 lemma split_if_eq2: "(a = (if Q then x else y)) = ((Q --> a = x) & (~ Q --> a = y))"
   911   by (rule split_if)
   912 
   913 text {*
   914   Split ifs on either side of the membership relation.  Not for @{text
   915   "[simp]"} -- can cause goals to blow up!
   916 *}
   917 
   918 lemma split_if_mem1: "((if Q then x else y) : b) = ((Q --> x : b) & (~ Q --> y : b))"
   919   by (rule split_if)
   920 
   921 lemma split_if_mem2: "(a : (if Q then x else y)) = ((Q --> a : x) & (~ Q --> a : y))"
   922   by (rule split_if [where P="%S. a : S"])
   923 
   924 lemmas split_ifs = if_bool_eq_conj split_if_eq1 split_if_eq2 split_if_mem1 split_if_mem2
   925 
   926 (*Would like to add these, but the existing code only searches for the
   927   outer-level constant, which in this case is just Set.member; we instead need
   928   to use term-nets to associate patterns with rules.  Also, if a rule fails to
   929   apply, then the formula should be kept.
   930   [("uminus", Compl_iff RS iffD1), ("minus", [Diff_iff RS iffD1]),
   931    ("Int", [IntD1,IntD2]),
   932    ("Collect", [CollectD]), ("Inter", [InterD]), ("INTER", [INT_D])]
   933  *)
   934 
   935 
   936 subsection {* Further operations and lemmas *}
   937 
   938 subsubsection {* The ``proper subset'' relation *}
   939 
   940 lemma psubsetI [intro!,no_atp]: "A \<subseteq> B ==> A \<noteq> B ==> A \<subset> B"
   941   by (unfold less_le) blast
   942 
   943 lemma psubsetE [elim!,no_atp]: 
   944     "[|A \<subset> B;  [|A \<subseteq> B; ~ (B\<subseteq>A)|] ==> R|] ==> R"
   945   by (unfold less_le) blast
   946 
   947 lemma psubset_insert_iff:
   948   "(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)"
   949   by (auto simp add: less_le subset_insert_iff)
   950 
   951 lemma psubset_eq: "(A \<subset> B) = (A \<subseteq> B & A \<noteq> B)"
   952   by (simp only: less_le)
   953 
   954 lemma psubset_imp_subset: "A \<subset> B ==> A \<subseteq> B"
   955   by (simp add: psubset_eq)
   956 
   957 lemma psubset_trans: "[| A \<subset> B; B \<subset> C |] ==> A \<subset> C"
   958 apply (unfold less_le)
   959 apply (auto dest: subset_antisym)
   960 done
   961 
   962 lemma psubsetD: "[| A \<subset> B; c \<in> A |] ==> c \<in> B"
   963 apply (unfold less_le)
   964 apply (auto dest: subsetD)
   965 done
   966 
   967 lemma psubset_subset_trans: "A \<subset> B ==> B \<subseteq> C ==> A \<subset> C"
   968   by (auto simp add: psubset_eq)
   969 
   970 lemma subset_psubset_trans: "A \<subseteq> B ==> B \<subset> C ==> A \<subset> C"
   971   by (auto simp add: psubset_eq)
   972 
   973 lemma psubset_imp_ex_mem: "A \<subset> B ==> \<exists>b. b \<in> (B - A)"
   974   by (unfold less_le) blast
   975 
   976 lemma atomize_ball:
   977     "(!!x. x \<in> A ==> P x) == Trueprop (\<forall>x\<in>A. P x)"
   978   by (simp only: Ball_def atomize_all atomize_imp)
   979 
   980 lemmas [symmetric, rulify] = atomize_ball
   981   and [symmetric, defn] = atomize_ball
   982 
   983 lemma image_Pow_mono:
   984   assumes "f ` A \<le> B"
   985   shows "(image f) ` (Pow A) \<le> Pow B"
   986 using assms by blast
   987 
   988 lemma image_Pow_surj:
   989   assumes "f ` A = B"
   990   shows "(image f) ` (Pow A) = Pow B"
   991 using assms unfolding Pow_def proof(auto)
   992   fix Y assume *: "Y \<le> f ` A"
   993   obtain X where X_def: "X = {x \<in> A. f x \<in> Y}" by blast
   994   have "f ` X = Y \<and> X \<le> A" unfolding X_def using * by auto
   995   thus "Y \<in> (image f) ` {X. X \<le> A}" by blast
   996 qed
   997 
   998 subsubsection {* Derived rules involving subsets. *}
   999 
  1000 text {* @{text insert}. *}
  1001 
  1002 lemma subset_insertI: "B \<subseteq> insert a B"
  1003   by (rule subsetI) (erule insertI2)
  1004 
  1005 lemma subset_insertI2: "A \<subseteq> B \<Longrightarrow> A \<subseteq> insert b B"
  1006   by blast
  1007 
  1008 lemma subset_insert: "x \<notin> A ==> (A \<subseteq> insert x B) = (A \<subseteq> B)"
  1009   by blast
  1010 
  1011 
  1012 text {* \medskip Finite Union -- the least upper bound of two sets. *}
  1013 
  1014 lemma Un_upper1: "A \<subseteq> A \<union> B"
  1015   by (fact sup_ge1)
  1016 
  1017 lemma Un_upper2: "B \<subseteq> A \<union> B"
  1018   by (fact sup_ge2)
  1019 
  1020 lemma Un_least: "A \<subseteq> C ==> B \<subseteq> C ==> A \<union> B \<subseteq> C"
  1021   by (fact sup_least)
  1022 
  1023 
  1024 text {* \medskip Finite Intersection -- the greatest lower bound of two sets. *}
  1025 
  1026 lemma Int_lower1: "A \<inter> B \<subseteq> A"
  1027   by (fact inf_le1)
  1028 
  1029 lemma Int_lower2: "A \<inter> B \<subseteq> B"
  1030   by (fact inf_le2)
  1031 
  1032 lemma Int_greatest: "C \<subseteq> A ==> C \<subseteq> B ==> C \<subseteq> A \<inter> B"
  1033   by (fact inf_greatest)
  1034 
  1035 
  1036 text {* \medskip Set difference. *}
  1037 
  1038 lemma Diff_subset: "A - B \<subseteq> A"
  1039   by blast
  1040 
  1041 lemma Diff_subset_conv: "(A - B \<subseteq> C) = (A \<subseteq> B \<union> C)"
  1042 by blast
  1043 
  1044 
  1045 subsubsection {* Equalities involving union, intersection, inclusion, etc. *}
  1046 
  1047 text {* @{text "{}"}. *}
  1048 
  1049 lemma Collect_const [simp]: "{s. P} = (if P then UNIV else {})"
  1050   -- {* supersedes @{text "Collect_False_empty"} *}
  1051   by auto
  1052 
  1053 lemma subset_empty [simp]: "(A \<subseteq> {}) = (A = {})"
  1054   by blast
  1055 
  1056 lemma not_psubset_empty [iff]: "\<not> (A < {})"
  1057   by (unfold less_le) blast
  1058 
  1059 lemma Collect_empty_eq [simp]: "(Collect P = {}) = (\<forall>x. \<not> P x)"
  1060 by blast
  1061 
  1062 lemma empty_Collect_eq [simp]: "({} = Collect P) = (\<forall>x. \<not> P x)"
  1063 by blast
  1064 
  1065 lemma Collect_neg_eq: "{x. \<not> P x} = - {x. P x}"
  1066   by blast
  1067 
  1068 lemma Collect_disj_eq: "{x. P x | Q x} = {x. P x} \<union> {x. Q x}"
  1069   by blast
  1070 
  1071 lemma Collect_imp_eq: "{x. P x --> Q x} = -{x. P x} \<union> {x. Q x}"
  1072   by blast
  1073 
  1074 lemma Collect_conj_eq: "{x. P x & Q x} = {x. P x} \<inter> {x. Q x}"
  1075   by blast
  1076 
  1077 
  1078 text {* \medskip @{text insert}. *}
  1079 
  1080 lemma insert_is_Un: "insert a A = {a} Un A"
  1081   -- {* NOT SUITABLE FOR REWRITING since @{text "{a} == insert a {}"} *}
  1082   by blast
  1083 
  1084 lemma insert_not_empty [simp]: "insert a A \<noteq> {}"
  1085   by blast
  1086 
  1087 lemmas empty_not_insert = insert_not_empty [symmetric, standard]
  1088 declare empty_not_insert [simp]
  1089 
  1090 lemma insert_absorb: "a \<in> A ==> insert a A = A"
  1091   -- {* @{text "[simp]"} causes recursive calls when there are nested inserts *}
  1092   -- {* with \emph{quadratic} running time *}
  1093   by blast
  1094 
  1095 lemma insert_absorb2 [simp]: "insert x (insert x A) = insert x A"
  1096   by blast
  1097 
  1098 lemma insert_commute: "insert x (insert y A) = insert y (insert x A)"
  1099   by blast
  1100 
  1101 lemma insert_subset [simp]: "(insert x A \<subseteq> B) = (x \<in> B & A \<subseteq> B)"
  1102   by blast
  1103 
  1104 lemma mk_disjoint_insert: "a \<in> A ==> \<exists>B. A = insert a B & a \<notin> B"
  1105   -- {* use new @{text B} rather than @{text "A - {a}"} to avoid infinite unfolding *}
  1106   apply (rule_tac x = "A - {a}" in exI, blast)
  1107   done
  1108 
  1109 lemma insert_Collect: "insert a (Collect P) = {u. u \<noteq> a --> P u}"
  1110   by auto
  1111 
  1112 lemma insert_inter_insert[simp]: "insert a A \<inter> insert a B = insert a (A \<inter> B)"
  1113   by blast
  1114 
  1115 lemma insert_disjoint [simp,no_atp]:
  1116  "(insert a A \<inter> B = {}) = (a \<notin> B \<and> A \<inter> B = {})"
  1117  "({} = insert a A \<inter> B) = (a \<notin> B \<and> {} = A \<inter> B)"
  1118   by auto
  1119 
  1120 lemma disjoint_insert [simp,no_atp]:
  1121  "(B \<inter> insert a A = {}) = (a \<notin> B \<and> B \<inter> A = {})"
  1122  "({} = A \<inter> insert b B) = (b \<notin> A \<and> {} = A \<inter> B)"
  1123   by auto
  1124 
  1125 text {* \medskip @{text image}. *}
  1126 
  1127 lemma image_empty [simp]: "f`{} = {}"
  1128   by blast
  1129 
  1130 lemma image_insert [simp]: "f ` insert a B = insert (f a) (f`B)"
  1131   by blast
  1132 
  1133 lemma image_constant: "x \<in> A ==> (\<lambda>x. c) ` A = {c}"
  1134   by auto
  1135 
  1136 lemma image_constant_conv: "(%x. c) ` A = (if A = {} then {} else {c})"
  1137 by auto
  1138 
  1139 lemma image_image: "f ` (g ` A) = (\<lambda>x. f (g x)) ` A"
  1140 by blast
  1141 
  1142 lemma insert_image [simp]: "x \<in> A ==> insert (f x) (f`A) = f`A"
  1143 by blast
  1144 
  1145 lemma image_is_empty [iff]: "(f`A = {}) = (A = {})"
  1146 by blast
  1147 
  1148 lemma empty_is_image[iff]: "({} = f ` A) = (A = {})"
  1149 by blast
  1150 
  1151 
  1152 lemma image_Collect [no_atp]: "f ` {x. P x} = {f x | x. P x}"
  1153   -- {* NOT suitable as a default simprule: the RHS isn't simpler than the LHS,
  1154       with its implicit quantifier and conjunction.  Also image enjoys better
  1155       equational properties than does the RHS. *}
  1156   by blast
  1157 
  1158 lemma if_image_distrib [simp]:
  1159   "(\<lambda>x. if P x then f x else g x) ` S
  1160     = (f ` (S \<inter> {x. P x})) \<union> (g ` (S \<inter> {x. \<not> P x}))"
  1161   by (auto simp add: image_def)
  1162 
  1163 lemma image_cong: "M = N ==> (!!x. x \<in> N ==> f x = g x) ==> f`M = g`N"
  1164   by (simp add: image_def)
  1165 
  1166 
  1167 text {* \medskip @{text range}. *}
  1168 
  1169 lemma full_SetCompr_eq [no_atp]: "{u. \<exists>x. u = f x} = range f"
  1170   by auto
  1171 
  1172 lemma range_composition: "range (\<lambda>x. f (g x)) = f`range g"
  1173 by (subst image_image, simp)
  1174 
  1175 
  1176 text {* \medskip @{text Int} *}
  1177 
  1178 lemma Int_absorb [simp]: "A \<inter> A = A"
  1179   by (fact inf_idem)
  1180 
  1181 lemma Int_left_absorb: "A \<inter> (A \<inter> B) = A \<inter> B"
  1182   by (fact inf_left_idem)
  1183 
  1184 lemma Int_commute: "A \<inter> B = B \<inter> A"
  1185   by (fact inf_commute)
  1186 
  1187 lemma Int_left_commute: "A \<inter> (B \<inter> C) = B \<inter> (A \<inter> C)"
  1188   by (fact inf_left_commute)
  1189 
  1190 lemma Int_assoc: "(A \<inter> B) \<inter> C = A \<inter> (B \<inter> C)"
  1191   by (fact inf_assoc)
  1192 
  1193 lemmas Int_ac = Int_assoc Int_left_absorb Int_commute Int_left_commute
  1194   -- {* Intersection is an AC-operator *}
  1195 
  1196 lemma Int_absorb1: "B \<subseteq> A ==> A \<inter> B = B"
  1197   by (fact inf_absorb2)
  1198 
  1199 lemma Int_absorb2: "A \<subseteq> B ==> A \<inter> B = A"
  1200   by (fact inf_absorb1)
  1201 
  1202 lemma Int_empty_left [simp]: "{} \<inter> B = {}"
  1203   by (fact inf_bot_left)
  1204 
  1205 lemma Int_empty_right [simp]: "A \<inter> {} = {}"
  1206   by (fact inf_bot_right)
  1207 
  1208 lemma disjoint_eq_subset_Compl: "(A \<inter> B = {}) = (A \<subseteq> -B)"
  1209   by blast
  1210 
  1211 lemma disjoint_iff_not_equal: "(A \<inter> B = {}) = (\<forall>x\<in>A. \<forall>y\<in>B. x \<noteq> y)"
  1212   by blast
  1213 
  1214 lemma Int_UNIV_left [simp]: "UNIV \<inter> B = B"
  1215   by (fact inf_top_left)
  1216 
  1217 lemma Int_UNIV_right [simp]: "A \<inter> UNIV = A"
  1218   by (fact inf_top_right)
  1219 
  1220 lemma Int_Un_distrib: "A \<inter> (B \<union> C) = (A \<inter> B) \<union> (A \<inter> C)"
  1221   by (fact inf_sup_distrib1)
  1222 
  1223 lemma Int_Un_distrib2: "(B \<union> C) \<inter> A = (B \<inter> A) \<union> (C \<inter> A)"
  1224   by (fact inf_sup_distrib2)
  1225 
  1226 lemma Int_UNIV [simp,no_atp]: "(A \<inter> B = UNIV) = (A = UNIV & B = UNIV)"
  1227   by (fact inf_eq_top_iff)
  1228 
  1229 lemma Int_subset_iff [no_atp, simp]: "(C \<subseteq> A \<inter> B) = (C \<subseteq> A & C \<subseteq> B)"
  1230   by (fact le_inf_iff)
  1231 
  1232 lemma Int_Collect: "(x \<in> A \<inter> {x. P x}) = (x \<in> A & P x)"
  1233   by blast
  1234 
  1235 
  1236 text {* \medskip @{text Un}. *}
  1237 
  1238 lemma Un_absorb [simp]: "A \<union> A = A"
  1239   by (fact sup_idem)
  1240 
  1241 lemma Un_left_absorb: "A \<union> (A \<union> B) = A \<union> B"
  1242   by (fact sup_left_idem)
  1243 
  1244 lemma Un_commute: "A \<union> B = B \<union> A"
  1245   by (fact sup_commute)
  1246 
  1247 lemma Un_left_commute: "A \<union> (B \<union> C) = B \<union> (A \<union> C)"
  1248   by (fact sup_left_commute)
  1249 
  1250 lemma Un_assoc: "(A \<union> B) \<union> C = A \<union> (B \<union> C)"
  1251   by (fact sup_assoc)
  1252 
  1253 lemmas Un_ac = Un_assoc Un_left_absorb Un_commute Un_left_commute
  1254   -- {* Union is an AC-operator *}
  1255 
  1256 lemma Un_absorb1: "A \<subseteq> B ==> A \<union> B = B"
  1257   by (fact sup_absorb2)
  1258 
  1259 lemma Un_absorb2: "B \<subseteq> A ==> A \<union> B = A"
  1260   by (fact sup_absorb1)
  1261 
  1262 lemma Un_empty_left [simp]: "{} \<union> B = B"
  1263   by (fact sup_bot_left)
  1264 
  1265 lemma Un_empty_right [simp]: "A \<union> {} = A"
  1266   by (fact sup_bot_right)
  1267 
  1268 lemma Un_UNIV_left [simp]: "UNIV \<union> B = UNIV"
  1269   by (fact sup_top_left)
  1270 
  1271 lemma Un_UNIV_right [simp]: "A \<union> UNIV = UNIV"
  1272   by (fact sup_top_right)
  1273 
  1274 lemma Un_insert_left [simp]: "(insert a B) \<union> C = insert a (B \<union> C)"
  1275   by blast
  1276 
  1277 lemma Un_insert_right [simp]: "A \<union> (insert a B) = insert a (A \<union> B)"
  1278   by blast
  1279 
  1280 lemma Int_insert_left:
  1281     "(insert a B) Int C = (if a \<in> C then insert a (B \<inter> C) else B \<inter> C)"
  1282   by auto
  1283 
  1284 lemma Int_insert_left_if0[simp]:
  1285     "a \<notin> C \<Longrightarrow> (insert a B) Int C = B \<inter> C"
  1286   by auto
  1287 
  1288 lemma Int_insert_left_if1[simp]:
  1289     "a \<in> C \<Longrightarrow> (insert a B) Int C = insert a (B Int C)"
  1290   by auto
  1291 
  1292 lemma Int_insert_right:
  1293     "A \<inter> (insert a B) = (if a \<in> A then insert a (A \<inter> B) else A \<inter> B)"
  1294   by auto
  1295 
  1296 lemma Int_insert_right_if0[simp]:
  1297     "a \<notin> A \<Longrightarrow> A Int (insert a B) = A Int B"
  1298   by auto
  1299 
  1300 lemma Int_insert_right_if1[simp]:
  1301     "a \<in> A \<Longrightarrow> A Int (insert a B) = insert a (A Int B)"
  1302   by auto
  1303 
  1304 lemma Un_Int_distrib: "A \<union> (B \<inter> C) = (A \<union> B) \<inter> (A \<union> C)"
  1305   by (fact sup_inf_distrib1)
  1306 
  1307 lemma Un_Int_distrib2: "(B \<inter> C) \<union> A = (B \<union> A) \<inter> (C \<union> A)"
  1308   by (fact sup_inf_distrib2)
  1309 
  1310 lemma Un_Int_crazy:
  1311     "(A \<inter> B) \<union> (B \<inter> C) \<union> (C \<inter> A) = (A \<union> B) \<inter> (B \<union> C) \<inter> (C \<union> A)"
  1312   by blast
  1313 
  1314 lemma subset_Un_eq: "(A \<subseteq> B) = (A \<union> B = B)"
  1315   by (fact le_iff_sup)
  1316 
  1317 lemma Un_empty [iff]: "(A \<union> B = {}) = (A = {} & B = {})"
  1318   by (fact sup_eq_bot_iff)
  1319 
  1320 lemma Un_subset_iff [no_atp, simp]: "(A \<union> B \<subseteq> C) = (A \<subseteq> C & B \<subseteq> C)"
  1321   by (fact le_sup_iff)
  1322 
  1323 lemma Un_Diff_Int: "(A - B) \<union> (A \<inter> B) = A"
  1324   by blast
  1325 
  1326 lemma Diff_Int2: "A \<inter> C - B \<inter> C = A \<inter> C - B"
  1327   by blast
  1328 
  1329 
  1330 text {* \medskip Set complement *}
  1331 
  1332 lemma Compl_disjoint [simp]: "A \<inter> -A = {}"
  1333   by (fact inf_compl_bot)
  1334 
  1335 lemma Compl_disjoint2 [simp]: "-A \<inter> A = {}"
  1336   by (fact compl_inf_bot)
  1337 
  1338 lemma Compl_partition: "A \<union> -A = UNIV"
  1339   by (fact sup_compl_top)
  1340 
  1341 lemma Compl_partition2: "-A \<union> A = UNIV"
  1342   by (fact compl_sup_top)
  1343 
  1344 lemma double_complement [simp]: "- (-A) = (A::'a set)"
  1345   by (fact double_compl)
  1346 
  1347 lemma Compl_Un [simp]: "-(A \<union> B) = (-A) \<inter> (-B)"
  1348   by (fact compl_sup)
  1349 
  1350 lemma Compl_Int [simp]: "-(A \<inter> B) = (-A) \<union> (-B)"
  1351   by (fact compl_inf)
  1352 
  1353 lemma subset_Compl_self_eq: "(A \<subseteq> -A) = (A = {})"
  1354   by blast
  1355 
  1356 lemma Un_Int_assoc_eq: "((A \<inter> B) \<union> C = A \<inter> (B \<union> C)) = (C \<subseteq> A)"
  1357   -- {* Halmos, Naive Set Theory, page 16. *}
  1358   by blast
  1359 
  1360 lemma Compl_UNIV_eq [simp]: "-UNIV = {}"
  1361   by (fact compl_top_eq)
  1362 
  1363 lemma Compl_empty_eq [simp]: "-{} = UNIV"
  1364   by (fact compl_bot_eq)
  1365 
  1366 lemma Compl_subset_Compl_iff [iff]: "(-A \<subseteq> -B) = (B \<subseteq> A)"
  1367   by (fact compl_le_compl_iff)
  1368 
  1369 lemma Compl_eq_Compl_iff [iff]: "(-A = -B) = (A = (B::'a set))"
  1370   by (fact compl_eq_compl_iff)
  1371 
  1372 text {* \medskip Bounded quantifiers.
  1373 
  1374   The following are not added to the default simpset because
  1375   (a) they duplicate the body and (b) there are no similar rules for @{text Int}. *}
  1376 
  1377 lemma ball_Un: "(\<forall>x \<in> A \<union> B. P x) = ((\<forall>x\<in>A. P x) & (\<forall>x\<in>B. P x))"
  1378   by blast
  1379 
  1380 lemma bex_Un: "(\<exists>x \<in> A \<union> B. P x) = ((\<exists>x\<in>A. P x) | (\<exists>x\<in>B. P x))"
  1381   by blast
  1382 
  1383 
  1384 text {* \medskip Set difference. *}
  1385 
  1386 lemma Diff_eq: "A - B = A \<inter> (-B)"
  1387   by blast
  1388 
  1389 lemma Diff_eq_empty_iff [simp,no_atp]: "(A - B = {}) = (A \<subseteq> B)"
  1390   by blast
  1391 
  1392 lemma Diff_cancel [simp]: "A - A = {}"
  1393   by blast
  1394 
  1395 lemma Diff_idemp [simp]: "(A - B) - B = A - (B::'a set)"
  1396 by blast
  1397 
  1398 lemma Diff_triv: "A \<inter> B = {} ==> A - B = A"
  1399   by (blast elim: equalityE)
  1400 
  1401 lemma empty_Diff [simp]: "{} - A = {}"
  1402   by blast
  1403 
  1404 lemma Diff_empty [simp]: "A - {} = A"
  1405   by blast
  1406 
  1407 lemma Diff_UNIV [simp]: "A - UNIV = {}"
  1408   by blast
  1409 
  1410 lemma Diff_insert0 [simp,no_atp]: "x \<notin> A ==> A - insert x B = A - B"
  1411   by blast
  1412 
  1413 lemma Diff_insert: "A - insert a B = A - B - {a}"
  1414   -- {* NOT SUITABLE FOR REWRITING since @{text "{a} == insert a 0"} *}
  1415   by blast
  1416 
  1417 lemma Diff_insert2: "A - insert a B = A - {a} - B"
  1418   -- {* NOT SUITABLE FOR REWRITING since @{text "{a} == insert a 0"} *}
  1419   by blast
  1420 
  1421 lemma insert_Diff_if: "insert x A - B = (if x \<in> B then A - B else insert x (A - B))"
  1422   by auto
  1423 
  1424 lemma insert_Diff1 [simp]: "x \<in> B ==> insert x A - B = A - B"
  1425   by blast
  1426 
  1427 lemma insert_Diff_single[simp]: "insert a (A - {a}) = insert a A"
  1428 by blast
  1429 
  1430 lemma insert_Diff: "a \<in> A ==> insert a (A - {a}) = A"
  1431   by blast
  1432 
  1433 lemma Diff_insert_absorb: "x \<notin> A ==> (insert x A) - {x} = A"
  1434   by auto
  1435 
  1436 lemma Diff_disjoint [simp]: "A \<inter> (B - A) = {}"
  1437   by blast
  1438 
  1439 lemma Diff_partition: "A \<subseteq> B ==> A \<union> (B - A) = B"
  1440   by blast
  1441 
  1442 lemma double_diff: "A \<subseteq> B ==> B \<subseteq> C ==> B - (C - A) = A"
  1443   by blast
  1444 
  1445 lemma Un_Diff_cancel [simp]: "A \<union> (B - A) = A \<union> B"
  1446   by blast
  1447 
  1448 lemma Un_Diff_cancel2 [simp]: "(B - A) \<union> A = B \<union> A"
  1449   by blast
  1450 
  1451 lemma Diff_Un: "A - (B \<union> C) = (A - B) \<inter> (A - C)"
  1452   by blast
  1453 
  1454 lemma Diff_Int: "A - (B \<inter> C) = (A - B) \<union> (A - C)"
  1455   by blast
  1456 
  1457 lemma Un_Diff: "(A \<union> B) - C = (A - C) \<union> (B - C)"
  1458   by blast
  1459 
  1460 lemma Int_Diff: "(A \<inter> B) - C = A \<inter> (B - C)"
  1461   by blast
  1462 
  1463 lemma Diff_Int_distrib: "C \<inter> (A - B) = (C \<inter> A) - (C \<inter> B)"
  1464   by blast
  1465 
  1466 lemma Diff_Int_distrib2: "(A - B) \<inter> C = (A \<inter> C) - (B \<inter> C)"
  1467   by blast
  1468 
  1469 lemma Diff_Compl [simp]: "A - (- B) = A \<inter> B"
  1470   by auto
  1471 
  1472 lemma Compl_Diff_eq [simp]: "- (A - B) = -A \<union> B"
  1473   by blast
  1474 
  1475 
  1476 text {* \medskip Quantification over type @{typ bool}. *}
  1477 
  1478 lemma bool_induct: "P True \<Longrightarrow> P False \<Longrightarrow> P x"
  1479   by (cases x) auto
  1480 
  1481 lemma all_bool_eq: "(\<forall>b. P b) \<longleftrightarrow> P True \<and> P False"
  1482   by (auto intro: bool_induct)
  1483 
  1484 lemma bool_contrapos: "P x \<Longrightarrow> \<not> P False \<Longrightarrow> P True"
  1485   by (cases x) auto
  1486 
  1487 lemma ex_bool_eq: "(\<exists>b. P b) \<longleftrightarrow> P True \<or> P False"
  1488   by (auto intro: bool_contrapos)
  1489 
  1490 lemma UNIV_bool [no_atp]: "UNIV = {False, True}"
  1491   by (auto intro: bool_induct)
  1492 
  1493 text {* \medskip @{text Pow} *}
  1494 
  1495 lemma Pow_empty [simp]: "Pow {} = {{}}"
  1496   by (auto simp add: Pow_def)
  1497 
  1498 lemma Pow_insert: "Pow (insert a A) = Pow A \<union> (insert a ` Pow A)"
  1499   by (blast intro: image_eqI [where ?x = "u - {a}", standard])
  1500 
  1501 lemma Pow_Compl: "Pow (- A) = {-B | B. A \<in> Pow B}"
  1502   by (blast intro: exI [where ?x = "- u", standard])
  1503 
  1504 lemma Pow_UNIV [simp]: "Pow UNIV = UNIV"
  1505   by blast
  1506 
  1507 lemma Un_Pow_subset: "Pow A \<union> Pow B \<subseteq> Pow (A \<union> B)"
  1508   by blast
  1509 
  1510 lemma Pow_Int_eq [simp]: "Pow (A \<inter> B) = Pow A \<inter> Pow B"
  1511   by blast
  1512 
  1513 
  1514 text {* \medskip Miscellany. *}
  1515 
  1516 lemma set_eq_subset: "(A = B) = (A \<subseteq> B & B \<subseteq> A)"
  1517   by blast
  1518 
  1519 lemma subset_iff [no_atp]: "(A \<subseteq> B) = (\<forall>t. t \<in> A --> t \<in> B)"
  1520   by blast
  1521 
  1522 lemma subset_iff_psubset_eq: "(A \<subseteq> B) = ((A \<subset> B) | (A = B))"
  1523   by (unfold less_le) blast
  1524 
  1525 lemma all_not_in_conv [simp]: "(\<forall>x. x \<notin> A) = (A = {})"
  1526   by blast
  1527 
  1528 lemma ex_in_conv: "(\<exists>x. x \<in> A) = (A \<noteq> {})"
  1529   by blast
  1530 
  1531 lemma distinct_lemma: "f x \<noteq> f y ==> x \<noteq> y"
  1532   by iprover
  1533 
  1534 
  1535 subsubsection {* Monotonicity of various operations *}
  1536 
  1537 lemma image_mono: "A \<subseteq> B ==> f`A \<subseteq> f`B"
  1538   by blast
  1539 
  1540 lemma Pow_mono: "A \<subseteq> B ==> Pow A \<subseteq> Pow B"
  1541   by blast
  1542 
  1543 lemma insert_mono: "C \<subseteq> D ==> insert a C \<subseteq> insert a D"
  1544   by blast
  1545 
  1546 lemma Un_mono: "A \<subseteq> C ==> B \<subseteq> D ==> A \<union> B \<subseteq> C \<union> D"
  1547   by (fact sup_mono)
  1548 
  1549 lemma Int_mono: "A \<subseteq> C ==> B \<subseteq> D ==> A \<inter> B \<subseteq> C \<inter> D"
  1550   by (fact inf_mono)
  1551 
  1552 lemma Diff_mono: "A \<subseteq> C ==> D \<subseteq> B ==> A - B \<subseteq> C - D"
  1553   by blast
  1554 
  1555 lemma Compl_anti_mono: "A \<subseteq> B ==> -B \<subseteq> -A"
  1556   by (fact compl_mono)
  1557 
  1558 text {* \medskip Monotonicity of implications. *}
  1559 
  1560 lemma in_mono: "A \<subseteq> B ==> x \<in> A --> x \<in> B"
  1561   apply (rule impI)
  1562   apply (erule subsetD, assumption)
  1563   done
  1564 
  1565 lemma conj_mono: "P1 --> Q1 ==> P2 --> Q2 ==> (P1 & P2) --> (Q1 & Q2)"
  1566   by iprover
  1567 
  1568 lemma disj_mono: "P1 --> Q1 ==> P2 --> Q2 ==> (P1 | P2) --> (Q1 | Q2)"
  1569   by iprover
  1570 
  1571 lemma imp_mono: "Q1 --> P1 ==> P2 --> Q2 ==> (P1 --> P2) --> (Q1 --> Q2)"
  1572   by iprover
  1573 
  1574 lemma imp_refl: "P --> P" ..
  1575 
  1576 lemma not_mono: "Q --> P ==> ~ P --> ~ Q"
  1577   by iprover
  1578 
  1579 lemma ex_mono: "(!!x. P x --> Q x) ==> (EX x. P x) --> (EX x. Q x)"
  1580   by iprover
  1581 
  1582 lemma all_mono: "(!!x. P x --> Q x) ==> (ALL x. P x) --> (ALL x. Q x)"
  1583   by iprover
  1584 
  1585 lemma Collect_mono: "(!!x. P x --> Q x) ==> Collect P \<subseteq> Collect Q"
  1586   by blast
  1587 
  1588 lemma Int_Collect_mono:
  1589     "A \<subseteq> B ==> (!!x. x \<in> A ==> P x --> Q x) ==> A \<inter> Collect P \<subseteq> B \<inter> Collect Q"
  1590   by blast
  1591 
  1592 lemmas basic_monos =
  1593   subset_refl imp_refl disj_mono conj_mono
  1594   ex_mono Collect_mono in_mono
  1595 
  1596 lemma eq_to_mono: "a = b ==> c = d ==> b --> d ==> a --> c"
  1597   by iprover
  1598 
  1599 
  1600 subsubsection {* Inverse image of a function *}
  1601 
  1602 definition vimage :: "('a => 'b) => 'b set => 'a set" (infixr "-`" 90) where
  1603   "f -` B == {x. f x : B}"
  1604 
  1605 lemma vimage_eq [simp]: "(a : f -` B) = (f a : B)"
  1606   by (unfold vimage_def) blast
  1607 
  1608 lemma vimage_singleton_eq: "(a : f -` {b}) = (f a = b)"
  1609   by simp
  1610 
  1611 lemma vimageI [intro]: "f a = b ==> b:B ==> a : f -` B"
  1612   by (unfold vimage_def) blast
  1613 
  1614 lemma vimageI2: "f a : A ==> a : f -` A"
  1615   by (unfold vimage_def) fast
  1616 
  1617 lemma vimageE [elim!]: "a: f -` B ==> (!!x. f a = x ==> x:B ==> P) ==> P"
  1618   by (unfold vimage_def) blast
  1619 
  1620 lemma vimageD: "a : f -` A ==> f a : A"
  1621   by (unfold vimage_def) fast
  1622 
  1623 lemma vimage_empty [simp]: "f -` {} = {}"
  1624   by blast
  1625 
  1626 lemma vimage_Compl: "f -` (-A) = -(f -` A)"
  1627   by blast
  1628 
  1629 lemma vimage_Un [simp]: "f -` (A Un B) = (f -` A) Un (f -` B)"
  1630   by blast
  1631 
  1632 lemma vimage_Int [simp]: "f -` (A Int B) = (f -` A) Int (f -` B)"
  1633   by fast
  1634 
  1635 lemma vimage_Collect_eq [simp]: "f -` Collect P = {y. P (f y)}"
  1636   by blast
  1637 
  1638 lemma vimage_Collect: "(!!x. P (f x) = Q x) ==> f -` (Collect P) = Collect Q"
  1639   by blast
  1640 
  1641 lemma vimage_insert: "f-`(insert a B) = (f-`{a}) Un (f-`B)"
  1642   -- {* NOT suitable for rewriting because of the recurrence of @{term "{a}"}. *}
  1643   by blast
  1644 
  1645 lemma vimage_Diff: "f -` (A - B) = (f -` A) - (f -` B)"
  1646   by blast
  1647 
  1648 lemma vimage_UNIV [simp]: "f -` UNIV = UNIV"
  1649   by blast
  1650 
  1651 lemma vimage_mono: "A \<subseteq> B ==> f -` A \<subseteq> f -` B"
  1652   -- {* monotonicity *}
  1653   by blast
  1654 
  1655 lemma vimage_image_eq [no_atp]: "f -` (f ` A) = {y. EX x:A. f x = f y}"
  1656 by (blast intro: sym)
  1657 
  1658 lemma image_vimage_subset: "f ` (f -` A) <= A"
  1659 by blast
  1660 
  1661 lemma image_vimage_eq [simp]: "f ` (f -` A) = A Int range f"
  1662 by blast
  1663 
  1664 lemma vimage_const [simp]: "((\<lambda>x. c) -` A) = (if c \<in> A then UNIV else {})"
  1665   by auto
  1666 
  1667 lemma vimage_if [simp]: "((\<lambda>x. if x \<in> B then c else d) -` A) = 
  1668    (if c \<in> A then (if d \<in> A then UNIV else B)
  1669     else if d \<in> A then -B else {})"  
  1670   by (auto simp add: vimage_def) 
  1671 
  1672 lemma vimage_inter_cong:
  1673   "(\<And> w. w \<in> S \<Longrightarrow> f w = g w) \<Longrightarrow> f -` y \<inter> S = g -` y \<inter> S"
  1674   by auto
  1675 
  1676 lemma image_Int_subset: "f`(A Int B) <= f`A Int f`B"
  1677 by blast
  1678 
  1679 lemma image_diff_subset: "f`A - f`B <= f`(A - B)"
  1680 by blast
  1681 
  1682 
  1683 subsubsection {* Getting the Contents of a Singleton Set *}
  1684 
  1685 definition the_elem :: "'a set \<Rightarrow> 'a" where
  1686   "the_elem X = (THE x. X = {x})"
  1687 
  1688 lemma the_elem_eq [simp]: "the_elem {x} = x"
  1689   by (simp add: the_elem_def)
  1690 
  1691 
  1692 subsubsection {* Least value operator *}
  1693 
  1694 lemma Least_mono:
  1695   "mono (f::'a::order => 'b::order) ==> EX x:S. ALL y:S. x <= y
  1696     ==> (LEAST y. y : f ` S) = f (LEAST x. x : S)"
  1697     -- {* Courtesy of Stephan Merz *}
  1698   apply clarify
  1699   apply (erule_tac P = "%x. x : S" in LeastI2_order, fast)
  1700   apply (rule LeastI2_order)
  1701   apply (auto elim: monoD intro!: order_antisym)
  1702   done
  1703 
  1704 subsection {* Misc *}
  1705 
  1706 text {* Rudimentary code generation *}
  1707 
  1708 lemma insert_code [code]: "insert y A x \<longleftrightarrow> y = x \<or> A x"
  1709   by (auto simp add: insert_compr Collect_def mem_def)
  1710 
  1711 lemma vimage_code [code]: "(f -` A) x = A (f x)"
  1712   by (simp add: vimage_def Collect_def mem_def)
  1713 
  1714 hide_const (open) member
  1715 
  1716 text {* Misc theorem and ML bindings *}
  1717 
  1718 lemmas equalityI = subset_antisym
  1719 
  1720 ML {*
  1721 val Ball_def = @{thm Ball_def}
  1722 val Bex_def = @{thm Bex_def}
  1723 val CollectD = @{thm CollectD}
  1724 val CollectE = @{thm CollectE}
  1725 val CollectI = @{thm CollectI}
  1726 val Collect_conj_eq = @{thm Collect_conj_eq}
  1727 val Collect_mem_eq = @{thm Collect_mem_eq}
  1728 val IntD1 = @{thm IntD1}
  1729 val IntD2 = @{thm IntD2}
  1730 val IntE = @{thm IntE}
  1731 val IntI = @{thm IntI}
  1732 val Int_Collect = @{thm Int_Collect}
  1733 val UNIV_I = @{thm UNIV_I}
  1734 val UNIV_witness = @{thm UNIV_witness}
  1735 val UnE = @{thm UnE}
  1736 val UnI1 = @{thm UnI1}
  1737 val UnI2 = @{thm UnI2}
  1738 val ballE = @{thm ballE}
  1739 val ballI = @{thm ballI}
  1740 val bexCI = @{thm bexCI}
  1741 val bexE = @{thm bexE}
  1742 val bexI = @{thm bexI}
  1743 val bex_triv = @{thm bex_triv}
  1744 val bspec = @{thm bspec}
  1745 val contra_subsetD = @{thm contra_subsetD}
  1746 val distinct_lemma = @{thm distinct_lemma}
  1747 val eq_to_mono = @{thm eq_to_mono}
  1748 val equalityCE = @{thm equalityCE}
  1749 val equalityD1 = @{thm equalityD1}
  1750 val equalityD2 = @{thm equalityD2}
  1751 val equalityE = @{thm equalityE}
  1752 val equalityI = @{thm equalityI}
  1753 val imageE = @{thm imageE}
  1754 val imageI = @{thm imageI}
  1755 val image_Un = @{thm image_Un}
  1756 val image_insert = @{thm image_insert}
  1757 val insert_commute = @{thm insert_commute}
  1758 val insert_iff = @{thm insert_iff}
  1759 val mem_Collect_eq = @{thm mem_Collect_eq}
  1760 val rangeE = @{thm rangeE}
  1761 val rangeI = @{thm rangeI}
  1762 val range_eqI = @{thm range_eqI}
  1763 val subsetCE = @{thm subsetCE}
  1764 val subsetD = @{thm subsetD}
  1765 val subsetI = @{thm subsetI}
  1766 val subset_refl = @{thm subset_refl}
  1767 val subset_trans = @{thm subset_trans}
  1768 val vimageD = @{thm vimageD}
  1769 val vimageE = @{thm vimageE}
  1770 val vimageI = @{thm vimageI}
  1771 val vimageI2 = @{thm vimageI2}
  1772 val vimage_Collect = @{thm vimage_Collect}
  1773 val vimage_Int = @{thm vimage_Int}
  1774 val vimage_Un = @{thm vimage_Un}
  1775 *}
  1776 
  1777 end