src/HOL/Set.thy
author haftmann
Thu Jan 28 11:48:49 2010 +0100 (2010-01-28)
changeset 34974 18b41bba42b5
parent 34209 c7f621786035
child 34999 5312d2ffee3b
permissions -rw-r--r--
new theory Algebras.thy for generic algebraic structures
     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 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 
   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             subset (op =) (0 upto (n - 1), 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 [intro!]: "(\<And>x. x \<in> A \<Longrightarrow> x \<in> B) \<Longrightarrow> A \<subseteq> B"
   449   unfolding mem_def by (rule le_funI, rule le_boolI)
   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   unfolding mem_def by (erule le_funE, erule le_boolE)
   459   -- {* Rule in Modus Ponens style. *}
   460 
   461 lemma rev_subsetD [noatp,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 [noatp,elim]: "A \<subseteq> B ==> (c \<notin> A ==> P) ==> (c \<in> B ==> P) ==> P"
   471   -- {* Classical elimination rule. *}
   472   unfolding mem_def by (blast dest: le_funE le_boolE)
   473 
   474 lemma subset_eq [noatp]: "A \<le> B = (\<forall>x\<in>A. x \<in> B)" by blast
   475 
   476 lemma contra_subsetD [noatp]: "A \<subseteq> B ==> c \<notin> B ==> c \<notin> A"
   477   by blast
   478 
   479 lemma subset_refl [simp]: "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 lemma eq_mem_trans: "a=b ==> b \<in> A ==> a \<in> A"
   492   by simp
   493 
   494 lemmas basic_trans_rules [trans] =
   495   order_trans_rules set_rev_mp set_mp eq_mem_trans
   496 
   497 
   498 subsubsection {* Equality *}
   499 
   500 lemma set_ext: assumes prem: "(!!x. (x:A) = (x:B))" shows "A = B"
   501   apply (rule prem [THEN ext, THEN arg_cong, THEN box_equals])
   502    apply (rule Collect_mem_eq)
   503   apply (rule Collect_mem_eq)
   504   done
   505 
   506 (* Due to Brian Huffman *)
   507 lemma expand_set_eq: "(A = B) = (ALL x. (x:A) = (x:B))"
   508 by(auto intro:set_ext)
   509 
   510 lemma subset_antisym [intro!]: "A \<subseteq> B ==> B \<subseteq> A ==> A = B"
   511   -- {* Anti-symmetry of the subset relation. *}
   512   by (iprover intro: set_ext subsetD)
   513 
   514 text {*
   515   \medskip Equality rules from ZF set theory -- are they appropriate
   516   here?
   517 *}
   518 
   519 lemma equalityD1: "A = B ==> A \<subseteq> B"
   520   by simp
   521 
   522 lemma equalityD2: "A = B ==> B \<subseteq> A"
   523   by simp
   524 
   525 text {*
   526   \medskip Be careful when adding this to the claset as @{text
   527   subset_empty} is in the simpset: @{prop "A = {}"} goes to @{prop "{}
   528   \<subseteq> A"} and @{prop "A \<subseteq> {}"} and then back to @{prop "A = {}"}!
   529 *}
   530 
   531 lemma equalityE: "A = B ==> (A \<subseteq> B ==> B \<subseteq> A ==> P) ==> P"
   532   by simp
   533 
   534 lemma equalityCE [elim]:
   535     "A = B ==> (c \<in> A ==> c \<in> B ==> P) ==> (c \<notin> A ==> c \<notin> B ==> P) ==> P"
   536   by blast
   537 
   538 lemma eqset_imp_iff: "A = B ==> (x : A) = (x : B)"
   539   by simp
   540 
   541 lemma eqelem_imp_iff: "x = y ==> (x : A) = (y : A)"
   542   by simp
   543 
   544 
   545 subsubsection {* The universal set -- UNIV *}
   546 
   547 abbreviation UNIV :: "'a set" where
   548   "UNIV \<equiv> top"
   549 
   550 lemma UNIV_def:
   551   "UNIV = {x. True}"
   552   by (simp add: top_fun_eq top_bool_eq Collect_def)
   553 
   554 lemma UNIV_I [simp]: "x : UNIV"
   555   by (simp add: UNIV_def)
   556 
   557 declare UNIV_I [intro]  -- {* unsafe makes it less likely to cause problems *}
   558 
   559 lemma UNIV_witness [intro?]: "EX x. x : UNIV"
   560   by simp
   561 
   562 lemma subset_UNIV [simp]: "A \<subseteq> UNIV"
   563   by (rule subsetI) (rule UNIV_I)
   564 
   565 text {*
   566   \medskip Eta-contracting these two rules (to remove @{text P})
   567   causes them to be ignored because of their interaction with
   568   congruence rules.
   569 *}
   570 
   571 lemma ball_UNIV [simp]: "Ball UNIV P = All P"
   572   by (simp add: Ball_def)
   573 
   574 lemma bex_UNIV [simp]: "Bex UNIV P = Ex P"
   575   by (simp add: Bex_def)
   576 
   577 lemma UNIV_eq_I: "(\<And>x. x \<in> A) \<Longrightarrow> UNIV = A"
   578   by auto
   579 
   580 
   581 subsubsection {* The empty set *}
   582 
   583 lemma empty_def:
   584   "{} = {x. False}"
   585   by (simp add: bot_fun_eq bot_bool_eq Collect_def)
   586 
   587 lemma empty_iff [simp]: "(c : {}) = False"
   588   by (simp add: empty_def)
   589 
   590 lemma emptyE [elim!]: "a : {} ==> P"
   591   by simp
   592 
   593 lemma empty_subsetI [iff]: "{} \<subseteq> A"
   594     -- {* One effect is to delete the ASSUMPTION @{prop "{} <= A"} *}
   595   by blast
   596 
   597 lemma equals0I: "(!!y. y \<in> A ==> False) ==> A = {}"
   598   by blast
   599 
   600 lemma equals0D: "A = {} ==> a \<notin> A"
   601     -- {* Use for reasoning about disjointness: @{text "A Int B = {}"} *}
   602   by blast
   603 
   604 lemma ball_empty [simp]: "Ball {} P = True"
   605   by (simp add: Ball_def)
   606 
   607 lemma bex_empty [simp]: "Bex {} P = False"
   608   by (simp add: Bex_def)
   609 
   610 lemma UNIV_not_empty [iff]: "UNIV ~= {}"
   611   by (blast elim: equalityE)
   612 
   613 
   614 subsubsection {* The Powerset operator -- Pow *}
   615 
   616 definition Pow :: "'a set => 'a set set" where
   617   Pow_def: "Pow A = {B. B \<le> A}"
   618 
   619 lemma Pow_iff [iff]: "(A \<in> Pow B) = (A \<subseteq> B)"
   620   by (simp add: Pow_def)
   621 
   622 lemma PowI: "A \<subseteq> B ==> A \<in> Pow B"
   623   by (simp add: Pow_def)
   624 
   625 lemma PowD: "A \<in> Pow B ==> A \<subseteq> B"
   626   by (simp add: Pow_def)
   627 
   628 lemma Pow_bottom: "{} \<in> Pow B"
   629   by simp
   630 
   631 lemma Pow_top: "A \<in> Pow A"
   632   by simp
   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 bool_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 bool_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 union -- Un *}
   657 
   658 abbreviation union :: "'a set \<Rightarrow> 'a set \<Rightarrow> 'a set" (infixl "Un" 65) where
   659   "op Un \<equiv> sup"
   660 
   661 notation (xsymbols)
   662   union  (infixl "\<union>" 65)
   663 
   664 notation (HTML output)
   665   union  (infixl "\<union>" 65)
   666 
   667 lemma Un_def:
   668   "A \<union> B = {x. x \<in> A \<or> x \<in> B}"
   669   by (simp add: sup_fun_eq sup_bool_eq Collect_def mem_def)
   670 
   671 lemma Un_iff [simp]: "(c : A Un B) = (c:A | c:B)"
   672   by (unfold Un_def) blast
   673 
   674 lemma UnI1 [elim?]: "c:A ==> c : A Un B"
   675   by simp
   676 
   677 lemma UnI2 [elim?]: "c:B ==> c : A Un B"
   678   by simp
   679 
   680 text {*
   681   \medskip Classical introduction rule: no commitment to @{prop A} vs
   682   @{prop B}.
   683 *}
   684 
   685 lemma UnCI [intro!]: "(c~:B ==> c:A) ==> c : A Un B"
   686   by auto
   687 
   688 lemma UnE [elim!]: "c : A Un B ==> (c:A ==> P) ==> (c:B ==> P) ==> P"
   689   by (unfold Un_def) blast
   690 
   691 lemma insert_def: "insert a B = {x. x = a} \<union> B"
   692   by (simp add: Collect_def mem_def insert_compr Un_def)
   693 
   694 lemma mono_Un: "mono f \<Longrightarrow> f A \<union> f B \<subseteq> f (A \<union> B)"
   695   by (fact mono_sup)
   696 
   697 
   698 subsubsection {* Binary intersection -- Int *}
   699 
   700 abbreviation inter :: "'a set \<Rightarrow> 'a set \<Rightarrow> 'a set" (infixl "Int" 70) where
   701   "op Int \<equiv> inf"
   702 
   703 notation (xsymbols)
   704   inter  (infixl "\<inter>" 70)
   705 
   706 notation (HTML output)
   707   inter  (infixl "\<inter>" 70)
   708 
   709 lemma Int_def:
   710   "A \<inter> B = {x. x \<in> A \<and> x \<in> B}"
   711   by (simp add: inf_fun_eq inf_bool_eq Collect_def mem_def)
   712 
   713 lemma Int_iff [simp]: "(c : A Int B) = (c:A & c:B)"
   714   by (unfold Int_def) blast
   715 
   716 lemma IntI [intro!]: "c:A ==> c:B ==> c : A Int B"
   717   by simp
   718 
   719 lemma IntD1: "c : A Int B ==> c:A"
   720   by simp
   721 
   722 lemma IntD2: "c : A Int B ==> c:B"
   723   by simp
   724 
   725 lemma IntE [elim!]: "c : A Int B ==> (c:A ==> c:B ==> P) ==> P"
   726   by simp
   727 
   728 lemma mono_Int: "mono f \<Longrightarrow> f (A \<inter> B) \<subseteq> f A \<inter> f B"
   729   by (fact mono_inf)
   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 bool_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!,noatp]: "a : {a}"
   791     -- {* Redundant? But unlike @{text insertCI}, it proves the subgoal immediately! *}
   792   by (rule insertI1)
   793 
   794 lemma singletonD [dest!,noatp]: "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,noatp]:
   806      "({b} = insert a A) = (a = b & A \<subseteq> {b})"
   807   by blast
   808 
   809 lemma singleton_insert_inj_eq' [iff,noatp]:
   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 [noatp]: "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: "(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 
   893 subsubsection {* Some rules with @{text "if"} *}
   894 
   895 text{* Elimination of @{text"{x. \<dots> & x=t & \<dots>}"}. *}
   896 
   897 lemma Collect_conv_if: "{x. x=a & P x} = (if P a then {a} else {})"
   898   by auto
   899 
   900 lemma Collect_conv_if2: "{x. a=x & P x} = (if P a then {a} else {})"
   901   by auto
   902 
   903 text {*
   904   Rewrite rules for boolean case-splitting: faster than @{text
   905   "split_if [split]"}.
   906 *}
   907 
   908 lemma split_if_eq1: "((if Q then x else y) = b) = ((Q --> x = b) & (~ Q --> y = b))"
   909   by (rule split_if)
   910 
   911 lemma split_if_eq2: "(a = (if Q then x else y)) = ((Q --> a = x) & (~ Q --> a = y))"
   912   by (rule split_if)
   913 
   914 text {*
   915   Split ifs on either side of the membership relation.  Not for @{text
   916   "[simp]"} -- can cause goals to blow up!
   917 *}
   918 
   919 lemma split_if_mem1: "((if Q then x else y) : b) = ((Q --> x : b) & (~ Q --> y : b))"
   920   by (rule split_if)
   921 
   922 lemma split_if_mem2: "(a : (if Q then x else y)) = ((Q --> a : x) & (~ Q --> a : y))"
   923   by (rule split_if [where P="%S. a : S"])
   924 
   925 lemmas split_ifs = if_bool_eq_conj split_if_eq1 split_if_eq2 split_if_mem1 split_if_mem2
   926 
   927 (*Would like to add these, but the existing code only searches for the
   928   outer-level constant, which in this case is just "op :"; we instead need
   929   to use term-nets to associate patterns with rules.  Also, if a rule fails to
   930   apply, then the formula should be kept.
   931   [("uminus", Compl_iff RS iffD1), ("minus", [Diff_iff RS iffD1]),
   932    ("Int", [IntD1,IntD2]),
   933    ("Collect", [CollectD]), ("Inter", [InterD]), ("INTER", [INT_D])]
   934  *)
   935 
   936 
   937 subsection {* Further operations and lemmas *}
   938 
   939 subsubsection {* The ``proper subset'' relation *}
   940 
   941 lemma psubsetI [intro!,noatp]: "A \<subseteq> B ==> A \<noteq> B ==> A \<subset> B"
   942   by (unfold less_le) blast
   943 
   944 lemma psubsetE [elim!,noatp]: 
   945     "[|A \<subset> B;  [|A \<subseteq> B; ~ (B\<subseteq>A)|] ==> R|] ==> R"
   946   by (unfold less_le) blast
   947 
   948 lemma psubset_insert_iff:
   949   "(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)"
   950   by (auto simp add: less_le subset_insert_iff)
   951 
   952 lemma psubset_eq: "(A \<subset> B) = (A \<subseteq> B & A \<noteq> B)"
   953   by (simp only: less_le)
   954 
   955 lemma psubset_imp_subset: "A \<subset> B ==> A \<subseteq> B"
   956   by (simp add: psubset_eq)
   957 
   958 lemma psubset_trans: "[| A \<subset> B; B \<subset> C |] ==> A \<subset> C"
   959 apply (unfold less_le)
   960 apply (auto dest: subset_antisym)
   961 done
   962 
   963 lemma psubsetD: "[| A \<subset> B; c \<in> A |] ==> c \<in> B"
   964 apply (unfold less_le)
   965 apply (auto dest: subsetD)
   966 done
   967 
   968 lemma psubset_subset_trans: "A \<subset> B ==> B \<subseteq> C ==> A \<subset> C"
   969   by (auto simp add: psubset_eq)
   970 
   971 lemma subset_psubset_trans: "A \<subseteq> B ==> B \<subset> C ==> A \<subset> C"
   972   by (auto simp add: psubset_eq)
   973 
   974 lemma psubset_imp_ex_mem: "A \<subset> B ==> \<exists>b. b \<in> (B - A)"
   975   by (unfold less_le) blast
   976 
   977 lemma atomize_ball:
   978     "(!!x. x \<in> A ==> P x) == Trueprop (\<forall>x\<in>A. P x)"
   979   by (simp only: Ball_def atomize_all atomize_imp)
   980 
   981 lemmas [symmetric, rulify] = atomize_ball
   982   and [symmetric, defn] = atomize_ball
   983 
   984 subsubsection {* Derived rules involving subsets. *}
   985 
   986 text {* @{text insert}. *}
   987 
   988 lemma subset_insertI: "B \<subseteq> insert a B"
   989   by (rule subsetI) (erule insertI2)
   990 
   991 lemma subset_insertI2: "A \<subseteq> B \<Longrightarrow> A \<subseteq> insert b B"
   992   by blast
   993 
   994 lemma subset_insert: "x \<notin> A ==> (A \<subseteq> insert x B) = (A \<subseteq> B)"
   995   by blast
   996 
   997 
   998 text {* \medskip Finite Union -- the least upper bound of two sets. *}
   999 
  1000 lemma Un_upper1: "A \<subseteq> A \<union> B"
  1001   by blast
  1002 
  1003 lemma Un_upper2: "B \<subseteq> A \<union> B"
  1004   by blast
  1005 
  1006 lemma Un_least: "A \<subseteq> C ==> B \<subseteq> C ==> A \<union> B \<subseteq> C"
  1007   by blast
  1008 
  1009 
  1010 text {* \medskip Finite Intersection -- the greatest lower bound of two sets. *}
  1011 
  1012 lemma Int_lower1: "A \<inter> B \<subseteq> A"
  1013   by blast
  1014 
  1015 lemma Int_lower2: "A \<inter> B \<subseteq> B"
  1016   by blast
  1017 
  1018 lemma Int_greatest: "C \<subseteq> A ==> C \<subseteq> B ==> C \<subseteq> A \<inter> B"
  1019   by blast
  1020 
  1021 
  1022 text {* \medskip Set difference. *}
  1023 
  1024 lemma Diff_subset: "A - B \<subseteq> A"
  1025   by blast
  1026 
  1027 lemma Diff_subset_conv: "(A - B \<subseteq> C) = (A \<subseteq> B \<union> C)"
  1028 by blast
  1029 
  1030 
  1031 subsubsection {* Equalities involving union, intersection, inclusion, etc. *}
  1032 
  1033 text {* @{text "{}"}. *}
  1034 
  1035 lemma Collect_const [simp]: "{s. P} = (if P then UNIV else {})"
  1036   -- {* supersedes @{text "Collect_False_empty"} *}
  1037   by auto
  1038 
  1039 lemma subset_empty [simp]: "(A \<subseteq> {}) = (A = {})"
  1040   by blast
  1041 
  1042 lemma not_psubset_empty [iff]: "\<not> (A < {})"
  1043   by (unfold less_le) blast
  1044 
  1045 lemma Collect_empty_eq [simp]: "(Collect P = {}) = (\<forall>x. \<not> P x)"
  1046 by blast
  1047 
  1048 lemma empty_Collect_eq [simp]: "({} = Collect P) = (\<forall>x. \<not> P x)"
  1049 by blast
  1050 
  1051 lemma Collect_neg_eq: "{x. \<not> P x} = - {x. P x}"
  1052   by blast
  1053 
  1054 lemma Collect_disj_eq: "{x. P x | Q x} = {x. P x} \<union> {x. Q x}"
  1055   by blast
  1056 
  1057 lemma Collect_imp_eq: "{x. P x --> Q x} = -{x. P x} \<union> {x. Q x}"
  1058   by blast
  1059 
  1060 lemma Collect_conj_eq: "{x. P x & Q x} = {x. P x} \<inter> {x. Q x}"
  1061   by blast
  1062 
  1063 
  1064 text {* \medskip @{text insert}. *}
  1065 
  1066 lemma insert_is_Un: "insert a A = {a} Un A"
  1067   -- {* NOT SUITABLE FOR REWRITING since @{text "{a} == insert a {}"} *}
  1068   by blast
  1069 
  1070 lemma insert_not_empty [simp]: "insert a A \<noteq> {}"
  1071   by blast
  1072 
  1073 lemmas empty_not_insert = insert_not_empty [symmetric, standard]
  1074 declare empty_not_insert [simp]
  1075 
  1076 lemma insert_absorb: "a \<in> A ==> insert a A = A"
  1077   -- {* @{text "[simp]"} causes recursive calls when there are nested inserts *}
  1078   -- {* with \emph{quadratic} running time *}
  1079   by blast
  1080 
  1081 lemma insert_absorb2 [simp]: "insert x (insert x A) = insert x A"
  1082   by blast
  1083 
  1084 lemma insert_commute: "insert x (insert y A) = insert y (insert x A)"
  1085   by blast
  1086 
  1087 lemma insert_subset [simp]: "(insert x A \<subseteq> B) = (x \<in> B & A \<subseteq> B)"
  1088   by blast
  1089 
  1090 lemma mk_disjoint_insert: "a \<in> A ==> \<exists>B. A = insert a B & a \<notin> B"
  1091   -- {* use new @{text B} rather than @{text "A - {a}"} to avoid infinite unfolding *}
  1092   apply (rule_tac x = "A - {a}" in exI, blast)
  1093   done
  1094 
  1095 lemma insert_Collect: "insert a (Collect P) = {u. u \<noteq> a --> P u}"
  1096   by auto
  1097 
  1098 lemma insert_inter_insert[simp]: "insert a A \<inter> insert a B = insert a (A \<inter> B)"
  1099   by blast
  1100 
  1101 lemma insert_disjoint [simp,noatp]:
  1102  "(insert a A \<inter> B = {}) = (a \<notin> B \<and> A \<inter> B = {})"
  1103  "({} = insert a A \<inter> B) = (a \<notin> B \<and> {} = A \<inter> B)"
  1104   by auto
  1105 
  1106 lemma disjoint_insert [simp,noatp]:
  1107  "(B \<inter> insert a A = {}) = (a \<notin> B \<and> B \<inter> A = {})"
  1108  "({} = A \<inter> insert b B) = (b \<notin> A \<and> {} = A \<inter> B)"
  1109   by auto
  1110 
  1111 text {* \medskip @{text image}. *}
  1112 
  1113 lemma image_empty [simp]: "f`{} = {}"
  1114   by blast
  1115 
  1116 lemma image_insert [simp]: "f ` insert a B = insert (f a) (f`B)"
  1117   by blast
  1118 
  1119 lemma image_constant: "x \<in> A ==> (\<lambda>x. c) ` A = {c}"
  1120   by auto
  1121 
  1122 lemma image_constant_conv: "(%x. c) ` A = (if A = {} then {} else {c})"
  1123 by auto
  1124 
  1125 lemma image_image: "f ` (g ` A) = (\<lambda>x. f (g x)) ` A"
  1126 by blast
  1127 
  1128 lemma insert_image [simp]: "x \<in> A ==> insert (f x) (f`A) = f`A"
  1129 by blast
  1130 
  1131 lemma image_is_empty [iff]: "(f`A = {}) = (A = {})"
  1132 by blast
  1133 
  1134 lemma empty_is_image[iff]: "({} = f ` A) = (A = {})"
  1135 by blast
  1136 
  1137 
  1138 lemma image_Collect [noatp]: "f ` {x. P x} = {f x | x. P x}"
  1139   -- {* NOT suitable as a default simprule: the RHS isn't simpler than the LHS,
  1140       with its implicit quantifier and conjunction.  Also image enjoys better
  1141       equational properties than does the RHS. *}
  1142   by blast
  1143 
  1144 lemma if_image_distrib [simp]:
  1145   "(\<lambda>x. if P x then f x else g x) ` S
  1146     = (f ` (S \<inter> {x. P x})) \<union> (g ` (S \<inter> {x. \<not> P x}))"
  1147   by (auto simp add: image_def)
  1148 
  1149 lemma image_cong: "M = N ==> (!!x. x \<in> N ==> f x = g x) ==> f`M = g`N"
  1150   by (simp add: image_def)
  1151 
  1152 
  1153 text {* \medskip @{text range}. *}
  1154 
  1155 lemma full_SetCompr_eq [noatp]: "{u. \<exists>x. u = f x} = range f"
  1156   by auto
  1157 
  1158 lemma range_composition: "range (\<lambda>x. f (g x)) = f`range g"
  1159 by (subst image_image, simp)
  1160 
  1161 
  1162 text {* \medskip @{text Int} *}
  1163 
  1164 lemma Int_absorb [simp]: "A \<inter> A = A"
  1165   by blast
  1166 
  1167 lemma Int_left_absorb: "A \<inter> (A \<inter> B) = A \<inter> B"
  1168   by blast
  1169 
  1170 lemma Int_commute: "A \<inter> B = B \<inter> A"
  1171   by blast
  1172 
  1173 lemma Int_left_commute: "A \<inter> (B \<inter> C) = B \<inter> (A \<inter> C)"
  1174   by blast
  1175 
  1176 lemma Int_assoc: "(A \<inter> B) \<inter> C = A \<inter> (B \<inter> C)"
  1177   by blast
  1178 
  1179 lemmas Int_ac = Int_assoc Int_left_absorb Int_commute Int_left_commute
  1180   -- {* Intersection is an AC-operator *}
  1181 
  1182 lemma Int_absorb1: "B \<subseteq> A ==> A \<inter> B = B"
  1183   by blast
  1184 
  1185 lemma Int_absorb2: "A \<subseteq> B ==> A \<inter> B = A"
  1186   by blast
  1187 
  1188 lemma Int_empty_left [simp]: "{} \<inter> B = {}"
  1189   by blast
  1190 
  1191 lemma Int_empty_right [simp]: "A \<inter> {} = {}"
  1192   by blast
  1193 
  1194 lemma disjoint_eq_subset_Compl: "(A \<inter> B = {}) = (A \<subseteq> -B)"
  1195   by blast
  1196 
  1197 lemma disjoint_iff_not_equal: "(A \<inter> B = {}) = (\<forall>x\<in>A. \<forall>y\<in>B. x \<noteq> y)"
  1198   by blast
  1199 
  1200 lemma Int_UNIV_left [simp]: "UNIV \<inter> B = B"
  1201   by blast
  1202 
  1203 lemma Int_UNIV_right [simp]: "A \<inter> UNIV = A"
  1204   by blast
  1205 
  1206 lemma Int_Un_distrib: "A \<inter> (B \<union> C) = (A \<inter> B) \<union> (A \<inter> C)"
  1207   by blast
  1208 
  1209 lemma Int_Un_distrib2: "(B \<union> C) \<inter> A = (B \<inter> A) \<union> (C \<inter> A)"
  1210   by blast
  1211 
  1212 lemma Int_UNIV [simp,noatp]: "(A \<inter> B = UNIV) = (A = UNIV & B = UNIV)"
  1213   by blast
  1214 
  1215 lemma Int_subset_iff [simp]: "(C \<subseteq> A \<inter> B) = (C \<subseteq> A & C \<subseteq> B)"
  1216   by blast
  1217 
  1218 lemma Int_Collect: "(x \<in> A \<inter> {x. P x}) = (x \<in> A & P x)"
  1219   by blast
  1220 
  1221 
  1222 text {* \medskip @{text Un}. *}
  1223 
  1224 lemma Un_absorb [simp]: "A \<union> A = A"
  1225   by blast
  1226 
  1227 lemma Un_left_absorb: "A \<union> (A \<union> B) = A \<union> B"
  1228   by blast
  1229 
  1230 lemma Un_commute: "A \<union> B = B \<union> A"
  1231   by blast
  1232 
  1233 lemma Un_left_commute: "A \<union> (B \<union> C) = B \<union> (A \<union> C)"
  1234   by blast
  1235 
  1236 lemma Un_assoc: "(A \<union> B) \<union> C = A \<union> (B \<union> C)"
  1237   by blast
  1238 
  1239 lemmas Un_ac = Un_assoc Un_left_absorb Un_commute Un_left_commute
  1240   -- {* Union is an AC-operator *}
  1241 
  1242 lemma Un_absorb1: "A \<subseteq> B ==> A \<union> B = B"
  1243   by blast
  1244 
  1245 lemma Un_absorb2: "B \<subseteq> A ==> A \<union> B = A"
  1246   by blast
  1247 
  1248 lemma Un_empty_left [simp]: "{} \<union> B = B"
  1249   by blast
  1250 
  1251 lemma Un_empty_right [simp]: "A \<union> {} = A"
  1252   by blast
  1253 
  1254 lemma Un_UNIV_left [simp]: "UNIV \<union> B = UNIV"
  1255   by blast
  1256 
  1257 lemma Un_UNIV_right [simp]: "A \<union> UNIV = UNIV"
  1258   by blast
  1259 
  1260 lemma Un_insert_left [simp]: "(insert a B) \<union> C = insert a (B \<union> C)"
  1261   by blast
  1262 
  1263 lemma Un_insert_right [simp]: "A \<union> (insert a B) = insert a (A \<union> B)"
  1264   by blast
  1265 
  1266 lemma Int_insert_left:
  1267     "(insert a B) Int C = (if a \<in> C then insert a (B \<inter> C) else B \<inter> C)"
  1268   by auto
  1269 
  1270 lemma Int_insert_left_if0[simp]:
  1271     "a \<notin> C \<Longrightarrow> (insert a B) Int C = B \<inter> C"
  1272   by auto
  1273 
  1274 lemma Int_insert_left_if1[simp]:
  1275     "a \<in> C \<Longrightarrow> (insert a B) Int C = insert a (B Int C)"
  1276   by auto
  1277 
  1278 lemma Int_insert_right:
  1279     "A \<inter> (insert a B) = (if a \<in> A then insert a (A \<inter> B) else A \<inter> B)"
  1280   by auto
  1281 
  1282 lemma Int_insert_right_if0[simp]:
  1283     "a \<notin> A \<Longrightarrow> A Int (insert a B) = A Int B"
  1284   by auto
  1285 
  1286 lemma Int_insert_right_if1[simp]:
  1287     "a \<in> A \<Longrightarrow> A Int (insert a B) = insert a (A Int B)"
  1288   by auto
  1289 
  1290 lemma Un_Int_distrib: "A \<union> (B \<inter> C) = (A \<union> B) \<inter> (A \<union> C)"
  1291   by blast
  1292 
  1293 lemma Un_Int_distrib2: "(B \<inter> C) \<union> A = (B \<union> A) \<inter> (C \<union> A)"
  1294   by blast
  1295 
  1296 lemma Un_Int_crazy:
  1297     "(A \<inter> B) \<union> (B \<inter> C) \<union> (C \<inter> A) = (A \<union> B) \<inter> (B \<union> C) \<inter> (C \<union> A)"
  1298   by blast
  1299 
  1300 lemma subset_Un_eq: "(A \<subseteq> B) = (A \<union> B = B)"
  1301   by blast
  1302 
  1303 lemma Un_empty [iff]: "(A \<union> B = {}) = (A = {} & B = {})"
  1304   by blast
  1305 
  1306 lemma Un_subset_iff [simp]: "(A \<union> B \<subseteq> C) = (A \<subseteq> C & B \<subseteq> C)"
  1307   by blast
  1308 
  1309 lemma Un_Diff_Int: "(A - B) \<union> (A \<inter> B) = A"
  1310   by blast
  1311 
  1312 lemma Diff_Int2: "A \<inter> C - B \<inter> C = A \<inter> C - B"
  1313   by blast
  1314 
  1315 
  1316 text {* \medskip Set complement *}
  1317 
  1318 lemma Compl_disjoint [simp]: "A \<inter> -A = {}"
  1319   by blast
  1320 
  1321 lemma Compl_disjoint2 [simp]: "-A \<inter> A = {}"
  1322   by blast
  1323 
  1324 lemma Compl_partition: "A \<union> -A = UNIV"
  1325   by blast
  1326 
  1327 lemma Compl_partition2: "-A \<union> A = UNIV"
  1328   by blast
  1329 
  1330 lemma double_complement [simp]: "- (-A) = (A::'a set)"
  1331   by blast
  1332 
  1333 lemma Compl_Un [simp]: "-(A \<union> B) = (-A) \<inter> (-B)"
  1334   by blast
  1335 
  1336 lemma Compl_Int [simp]: "-(A \<inter> B) = (-A) \<union> (-B)"
  1337   by blast
  1338 
  1339 lemma subset_Compl_self_eq: "(A \<subseteq> -A) = (A = {})"
  1340   by blast
  1341 
  1342 lemma Un_Int_assoc_eq: "((A \<inter> B) \<union> C = A \<inter> (B \<union> C)) = (C \<subseteq> A)"
  1343   -- {* Halmos, Naive Set Theory, page 16. *}
  1344   by blast
  1345 
  1346 lemma Compl_UNIV_eq [simp]: "-UNIV = {}"
  1347   by blast
  1348 
  1349 lemma Compl_empty_eq [simp]: "-{} = UNIV"
  1350   by blast
  1351 
  1352 lemma Compl_subset_Compl_iff [iff]: "(-A \<subseteq> -B) = (B \<subseteq> A)"
  1353   by blast
  1354 
  1355 lemma Compl_eq_Compl_iff [iff]: "(-A = -B) = (A = (B::'a set))"
  1356   by blast
  1357 
  1358 text {* \medskip Bounded quantifiers.
  1359 
  1360   The following are not added to the default simpset because
  1361   (a) they duplicate the body and (b) there are no similar rules for @{text Int}. *}
  1362 
  1363 lemma ball_Un: "(\<forall>x \<in> A \<union> B. P x) = ((\<forall>x\<in>A. P x) & (\<forall>x\<in>B. P x))"
  1364   by blast
  1365 
  1366 lemma bex_Un: "(\<exists>x \<in> A \<union> B. P x) = ((\<exists>x\<in>A. P x) | (\<exists>x\<in>B. P x))"
  1367   by blast
  1368 
  1369 
  1370 text {* \medskip Set difference. *}
  1371 
  1372 lemma Diff_eq: "A - B = A \<inter> (-B)"
  1373   by blast
  1374 
  1375 lemma Diff_eq_empty_iff [simp,noatp]: "(A - B = {}) = (A \<subseteq> B)"
  1376   by blast
  1377 
  1378 lemma Diff_cancel [simp]: "A - A = {}"
  1379   by blast
  1380 
  1381 lemma Diff_idemp [simp]: "(A - B) - B = A - (B::'a set)"
  1382 by blast
  1383 
  1384 lemma Diff_triv: "A \<inter> B = {} ==> A - B = A"
  1385   by (blast elim: equalityE)
  1386 
  1387 lemma empty_Diff [simp]: "{} - A = {}"
  1388   by blast
  1389 
  1390 lemma Diff_empty [simp]: "A - {} = A"
  1391   by blast
  1392 
  1393 lemma Diff_UNIV [simp]: "A - UNIV = {}"
  1394   by blast
  1395 
  1396 lemma Diff_insert0 [simp,noatp]: "x \<notin> A ==> A - insert x B = A - B"
  1397   by blast
  1398 
  1399 lemma Diff_insert: "A - insert a B = A - B - {a}"
  1400   -- {* NOT SUITABLE FOR REWRITING since @{text "{a} == insert a 0"} *}
  1401   by blast
  1402 
  1403 lemma Diff_insert2: "A - insert a B = A - {a} - B"
  1404   -- {* NOT SUITABLE FOR REWRITING since @{text "{a} == insert a 0"} *}
  1405   by blast
  1406 
  1407 lemma insert_Diff_if: "insert x A - B = (if x \<in> B then A - B else insert x (A - B))"
  1408   by auto
  1409 
  1410 lemma insert_Diff1 [simp]: "x \<in> B ==> insert x A - B = A - B"
  1411   by blast
  1412 
  1413 lemma insert_Diff_single[simp]: "insert a (A - {a}) = insert a A"
  1414 by blast
  1415 
  1416 lemma insert_Diff: "a \<in> A ==> insert a (A - {a}) = A"
  1417   by blast
  1418 
  1419 lemma Diff_insert_absorb: "x \<notin> A ==> (insert x A) - {x} = A"
  1420   by auto
  1421 
  1422 lemma Diff_disjoint [simp]: "A \<inter> (B - A) = {}"
  1423   by blast
  1424 
  1425 lemma Diff_partition: "A \<subseteq> B ==> A \<union> (B - A) = B"
  1426   by blast
  1427 
  1428 lemma double_diff: "A \<subseteq> B ==> B \<subseteq> C ==> B - (C - A) = A"
  1429   by blast
  1430 
  1431 lemma Un_Diff_cancel [simp]: "A \<union> (B - A) = A \<union> B"
  1432   by blast
  1433 
  1434 lemma Un_Diff_cancel2 [simp]: "(B - A) \<union> A = B \<union> A"
  1435   by blast
  1436 
  1437 lemma Diff_Un: "A - (B \<union> C) = (A - B) \<inter> (A - C)"
  1438   by blast
  1439 
  1440 lemma Diff_Int: "A - (B \<inter> C) = (A - B) \<union> (A - C)"
  1441   by blast
  1442 
  1443 lemma Un_Diff: "(A \<union> B) - C = (A - C) \<union> (B - C)"
  1444   by blast
  1445 
  1446 lemma Int_Diff: "(A \<inter> B) - C = A \<inter> (B - C)"
  1447   by blast
  1448 
  1449 lemma Diff_Int_distrib: "C \<inter> (A - B) = (C \<inter> A) - (C \<inter> B)"
  1450   by blast
  1451 
  1452 lemma Diff_Int_distrib2: "(A - B) \<inter> C = (A \<inter> C) - (B \<inter> C)"
  1453   by blast
  1454 
  1455 lemma Diff_Compl [simp]: "A - (- B) = A \<inter> B"
  1456   by auto
  1457 
  1458 lemma Compl_Diff_eq [simp]: "- (A - B) = -A \<union> B"
  1459   by blast
  1460 
  1461 
  1462 text {* \medskip Quantification over type @{typ bool}. *}
  1463 
  1464 lemma bool_induct: "P True \<Longrightarrow> P False \<Longrightarrow> P x"
  1465   by (cases x) auto
  1466 
  1467 lemma all_bool_eq: "(\<forall>b. P b) \<longleftrightarrow> P True \<and> P False"
  1468   by (auto intro: bool_induct)
  1469 
  1470 lemma bool_contrapos: "P x \<Longrightarrow> \<not> P False \<Longrightarrow> P True"
  1471   by (cases x) auto
  1472 
  1473 lemma ex_bool_eq: "(\<exists>b. P b) \<longleftrightarrow> P True \<or> P False"
  1474   by (auto intro: bool_contrapos)
  1475 
  1476 text {* \medskip @{text Pow} *}
  1477 
  1478 lemma Pow_empty [simp]: "Pow {} = {{}}"
  1479   by (auto simp add: Pow_def)
  1480 
  1481 lemma Pow_insert: "Pow (insert a A) = Pow A \<union> (insert a ` Pow A)"
  1482   by (blast intro: image_eqI [where ?x = "u - {a}", standard])
  1483 
  1484 lemma Pow_Compl: "Pow (- A) = {-B | B. A \<in> Pow B}"
  1485   by (blast intro: exI [where ?x = "- u", standard])
  1486 
  1487 lemma Pow_UNIV [simp]: "Pow UNIV = UNIV"
  1488   by blast
  1489 
  1490 lemma Un_Pow_subset: "Pow A \<union> Pow B \<subseteq> Pow (A \<union> B)"
  1491   by blast
  1492 
  1493 lemma Pow_Int_eq [simp]: "Pow (A \<inter> B) = Pow A \<inter> Pow B"
  1494   by blast
  1495 
  1496 
  1497 text {* \medskip Miscellany. *}
  1498 
  1499 lemma set_eq_subset: "(A = B) = (A \<subseteq> B & B \<subseteq> A)"
  1500   by blast
  1501 
  1502 lemma subset_iff: "(A \<subseteq> B) = (\<forall>t. t \<in> A --> t \<in> B)"
  1503   by blast
  1504 
  1505 lemma subset_iff_psubset_eq: "(A \<subseteq> B) = ((A \<subset> B) | (A = B))"
  1506   by (unfold less_le) blast
  1507 
  1508 lemma all_not_in_conv [simp]: "(\<forall>x. x \<notin> A) = (A = {})"
  1509   by blast
  1510 
  1511 lemma ex_in_conv: "(\<exists>x. x \<in> A) = (A \<noteq> {})"
  1512   by blast
  1513 
  1514 lemma distinct_lemma: "f x \<noteq> f y ==> x \<noteq> y"
  1515   by iprover
  1516 
  1517 
  1518 subsubsection {* Monotonicity of various operations *}
  1519 
  1520 lemma image_mono: "A \<subseteq> B ==> f`A \<subseteq> f`B"
  1521   by blast
  1522 
  1523 lemma Pow_mono: "A \<subseteq> B ==> Pow A \<subseteq> Pow B"
  1524   by blast
  1525 
  1526 lemma insert_mono: "C \<subseteq> D ==> insert a C \<subseteq> insert a D"
  1527   by blast
  1528 
  1529 lemma Un_mono: "A \<subseteq> C ==> B \<subseteq> D ==> A \<union> B \<subseteq> C \<union> D"
  1530   by blast
  1531 
  1532 lemma Int_mono: "A \<subseteq> C ==> B \<subseteq> D ==> A \<inter> B \<subseteq> C \<inter> D"
  1533   by blast
  1534 
  1535 lemma Diff_mono: "A \<subseteq> C ==> D \<subseteq> B ==> A - B \<subseteq> C - D"
  1536   by blast
  1537 
  1538 lemma Compl_anti_mono: "A \<subseteq> B ==> -B \<subseteq> -A"
  1539   by blast
  1540 
  1541 text {* \medskip Monotonicity of implications. *}
  1542 
  1543 lemma in_mono: "A \<subseteq> B ==> x \<in> A --> x \<in> B"
  1544   apply (rule impI)
  1545   apply (erule subsetD, assumption)
  1546   done
  1547 
  1548 lemma conj_mono: "P1 --> Q1 ==> P2 --> Q2 ==> (P1 & P2) --> (Q1 & Q2)"
  1549   by iprover
  1550 
  1551 lemma disj_mono: "P1 --> Q1 ==> P2 --> Q2 ==> (P1 | P2) --> (Q1 | Q2)"
  1552   by iprover
  1553 
  1554 lemma imp_mono: "Q1 --> P1 ==> P2 --> Q2 ==> (P1 --> P2) --> (Q1 --> Q2)"
  1555   by iprover
  1556 
  1557 lemma imp_refl: "P --> P" ..
  1558 
  1559 lemma not_mono: "Q --> P ==> ~ P --> ~ Q"
  1560   by iprover
  1561 
  1562 lemma ex_mono: "(!!x. P x --> Q x) ==> (EX x. P x) --> (EX x. Q x)"
  1563   by iprover
  1564 
  1565 lemma all_mono: "(!!x. P x --> Q x) ==> (ALL x. P x) --> (ALL x. Q x)"
  1566   by iprover
  1567 
  1568 lemma Collect_mono: "(!!x. P x --> Q x) ==> Collect P \<subseteq> Collect Q"
  1569   by blast
  1570 
  1571 lemma Int_Collect_mono:
  1572     "A \<subseteq> B ==> (!!x. x \<in> A ==> P x --> Q x) ==> A \<inter> Collect P \<subseteq> B \<inter> Collect Q"
  1573   by blast
  1574 
  1575 lemmas basic_monos =
  1576   subset_refl imp_refl disj_mono conj_mono
  1577   ex_mono Collect_mono in_mono
  1578 
  1579 lemma eq_to_mono: "a = b ==> c = d ==> b --> d ==> a --> c"
  1580   by iprover
  1581 
  1582 
  1583 subsubsection {* Inverse image of a function *}
  1584 
  1585 constdefs
  1586   vimage :: "('a => 'b) => 'b set => 'a set"    (infixr "-`" 90)
  1587   [code del]: "f -` B == {x. f x : B}"
  1588 
  1589 lemma vimage_eq [simp]: "(a : f -` B) = (f a : B)"
  1590   by (unfold vimage_def) blast
  1591 
  1592 lemma vimage_singleton_eq: "(a : f -` {b}) = (f a = b)"
  1593   by simp
  1594 
  1595 lemma vimageI [intro]: "f a = b ==> b:B ==> a : f -` B"
  1596   by (unfold vimage_def) blast
  1597 
  1598 lemma vimageI2: "f a : A ==> a : f -` A"
  1599   by (unfold vimage_def) fast
  1600 
  1601 lemma vimageE [elim!]: "a: f -` B ==> (!!x. f a = x ==> x:B ==> P) ==> P"
  1602   by (unfold vimage_def) blast
  1603 
  1604 lemma vimageD: "a : f -` A ==> f a : A"
  1605   by (unfold vimage_def) fast
  1606 
  1607 lemma vimage_empty [simp]: "f -` {} = {}"
  1608   by blast
  1609 
  1610 lemma vimage_Compl: "f -` (-A) = -(f -` A)"
  1611   by blast
  1612 
  1613 lemma vimage_Un [simp]: "f -` (A Un B) = (f -` A) Un (f -` B)"
  1614   by blast
  1615 
  1616 lemma vimage_Int [simp]: "f -` (A Int B) = (f -` A) Int (f -` B)"
  1617   by fast
  1618 
  1619 lemma vimage_Collect_eq [simp]: "f -` Collect P = {y. P (f y)}"
  1620   by blast
  1621 
  1622 lemma vimage_Collect: "(!!x. P (f x) = Q x) ==> f -` (Collect P) = Collect Q"
  1623   by blast
  1624 
  1625 lemma vimage_insert: "f-`(insert a B) = (f-`{a}) Un (f-`B)"
  1626   -- {* NOT suitable for rewriting because of the recurrence of @{term "{a}"}. *}
  1627   by blast
  1628 
  1629 lemma vimage_Diff: "f -` (A - B) = (f -` A) - (f -` B)"
  1630   by blast
  1631 
  1632 lemma vimage_UNIV [simp]: "f -` UNIV = UNIV"
  1633   by blast
  1634 
  1635 lemma vimage_mono: "A \<subseteq> B ==> f -` A \<subseteq> f -` B"
  1636   -- {* monotonicity *}
  1637   by blast
  1638 
  1639 lemma vimage_image_eq [noatp]: "f -` (f ` A) = {y. EX x:A. f x = f y}"
  1640 by (blast intro: sym)
  1641 
  1642 lemma image_vimage_subset: "f ` (f -` A) <= A"
  1643 by blast
  1644 
  1645 lemma image_vimage_eq [simp]: "f ` (f -` A) = A Int range f"
  1646 by blast
  1647 
  1648 lemma vimage_const [simp]: "((\<lambda>x. c) -` A) = (if c \<in> A then UNIV else {})"
  1649   by auto
  1650 
  1651 lemma vimage_if [simp]: "((\<lambda>x. if x \<in> B then c else d) -` A) = 
  1652    (if c \<in> A then (if d \<in> A then UNIV else B)
  1653     else if d \<in> A then -B else {})"  
  1654   by (auto simp add: vimage_def) 
  1655 
  1656 lemma image_Int_subset: "f`(A Int B) <= f`A Int f`B"
  1657 by blast
  1658 
  1659 lemma image_diff_subset: "f`A - f`B <= f`(A - B)"
  1660 by blast
  1661 
  1662 
  1663 subsubsection {* Getting the Contents of a Singleton Set *}
  1664 
  1665 definition contents :: "'a set \<Rightarrow> 'a" where
  1666   [code del]: "contents X = (THE x. X = {x})"
  1667 
  1668 lemma contents_eq [simp]: "contents {x} = x"
  1669   by (simp add: contents_def)
  1670 
  1671 
  1672 subsubsection {* Least value operator *}
  1673 
  1674 lemma Least_mono:
  1675   "mono (f::'a::order => 'b::order) ==> EX x:S. ALL y:S. x <= y
  1676     ==> (LEAST y. y : f ` S) = f (LEAST x. x : S)"
  1677     -- {* Courtesy of Stephan Merz *}
  1678   apply clarify
  1679   apply (erule_tac P = "%x. x : S" in LeastI2_order, fast)
  1680   apply (rule LeastI2_order)
  1681   apply (auto elim: monoD intro!: order_antisym)
  1682   done
  1683 
  1684 subsection {* Misc *}
  1685 
  1686 text {* Rudimentary code generation *}
  1687 
  1688 lemma insert_code [code]: "insert y A x \<longleftrightarrow> y = x \<or> A x"
  1689   by (auto simp add: insert_compr Collect_def mem_def)
  1690 
  1691 lemma vimage_code [code]: "(f -` A) x = A (f x)"
  1692   by (simp add: vimage_def Collect_def mem_def)
  1693 
  1694 
  1695 text {* Misc theorem and ML bindings *}
  1696 
  1697 lemmas equalityI = subset_antisym
  1698 
  1699 ML {*
  1700 val Ball_def = @{thm Ball_def}
  1701 val Bex_def = @{thm Bex_def}
  1702 val CollectD = @{thm CollectD}
  1703 val CollectE = @{thm CollectE}
  1704 val CollectI = @{thm CollectI}
  1705 val Collect_conj_eq = @{thm Collect_conj_eq}
  1706 val Collect_mem_eq = @{thm Collect_mem_eq}
  1707 val IntD1 = @{thm IntD1}
  1708 val IntD2 = @{thm IntD2}
  1709 val IntE = @{thm IntE}
  1710 val IntI = @{thm IntI}
  1711 val Int_Collect = @{thm Int_Collect}
  1712 val UNIV_I = @{thm UNIV_I}
  1713 val UNIV_witness = @{thm UNIV_witness}
  1714 val UnE = @{thm UnE}
  1715 val UnI1 = @{thm UnI1}
  1716 val UnI2 = @{thm UnI2}
  1717 val ballE = @{thm ballE}
  1718 val ballI = @{thm ballI}
  1719 val bexCI = @{thm bexCI}
  1720 val bexE = @{thm bexE}
  1721 val bexI = @{thm bexI}
  1722 val bex_triv = @{thm bex_triv}
  1723 val bspec = @{thm bspec}
  1724 val contra_subsetD = @{thm contra_subsetD}
  1725 val distinct_lemma = @{thm distinct_lemma}
  1726 val eq_to_mono = @{thm eq_to_mono}
  1727 val equalityCE = @{thm equalityCE}
  1728 val equalityD1 = @{thm equalityD1}
  1729 val equalityD2 = @{thm equalityD2}
  1730 val equalityE = @{thm equalityE}
  1731 val equalityI = @{thm equalityI}
  1732 val imageE = @{thm imageE}
  1733 val imageI = @{thm imageI}
  1734 val image_Un = @{thm image_Un}
  1735 val image_insert = @{thm image_insert}
  1736 val insert_commute = @{thm insert_commute}
  1737 val insert_iff = @{thm insert_iff}
  1738 val mem_Collect_eq = @{thm mem_Collect_eq}
  1739 val rangeE = @{thm rangeE}
  1740 val rangeI = @{thm rangeI}
  1741 val range_eqI = @{thm range_eqI}
  1742 val subsetCE = @{thm subsetCE}
  1743 val subsetD = @{thm subsetD}
  1744 val subsetI = @{thm subsetI}
  1745 val subset_refl = @{thm subset_refl}
  1746 val subset_trans = @{thm subset_trans}
  1747 val vimageD = @{thm vimageD}
  1748 val vimageE = @{thm vimageE}
  1749 val vimageI = @{thm vimageI}
  1750 val vimageI2 = @{thm vimageI2}
  1751 val vimage_Collect = @{thm vimage_Collect}
  1752 val vimage_Int = @{thm vimage_Int}
  1753 val vimage_Un = @{thm vimage_Un}
  1754 *}
  1755 
  1756 end