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