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