src/HOL/Set.thy
author wenzelm
Tue Aug 06 11:22:05 2002 +0200 (2002-08-06)
changeset 13462 56610e2ba220
parent 13421 8fcdf4a26468
child 13550 5a176b8dda84
permissions -rw-r--r--
sane interface for simprocs;
     1 (*  Title:      HOL/Set.thy
     2     ID:         $Id$
     3     Author:     Tobias Nipkow, Lawrence C Paulson and Markus Wenzel
     4     License:    GPL (GNU GENERAL PUBLIC LICENSE)
     5 *)
     6 
     7 header {* Set theory for higher-order logic *}
     8 
     9 theory Set = HOL:
    10 
    11 text {* A set in HOL is simply a predicate. *}
    12 
    13 
    14 subsection {* Basic syntax *}
    15 
    16 global
    17 
    18 typedecl 'a set
    19 arities set :: (type) type
    20 
    21 consts
    22   "{}"          :: "'a set"                             ("{}")
    23   UNIV          :: "'a set"
    24   insert        :: "'a => 'a set => 'a set"
    25   Collect       :: "('a => bool) => 'a set"              -- "comprehension"
    26   Int           :: "'a set => 'a set => 'a set"          (infixl 70)
    27   Un            :: "'a set => 'a set => 'a set"          (infixl 65)
    28   UNION         :: "'a set => ('a => 'b set) => 'b set"  -- "general union"
    29   INTER         :: "'a set => ('a => 'b set) => 'b set"  -- "general intersection"
    30   Union         :: "'a set set => 'a set"                -- "union of a set"
    31   Inter         :: "'a set set => 'a set"                -- "intersection of a set"
    32   Pow           :: "'a set => 'a set set"                -- "powerset"
    33   Ball          :: "'a set => ('a => bool) => bool"      -- "bounded universal quantifiers"
    34   Bex           :: "'a set => ('a => bool) => bool"      -- "bounded existential quantifiers"
    35   image         :: "('a => 'b) => 'a set => 'b set"      (infixr "`" 90)
    36 
    37 syntax
    38   "op :"        :: "'a => 'a set => bool"                ("op :")
    39 consts
    40   "op :"        :: "'a => 'a set => bool"                ("(_/ : _)" [50, 51] 50)  -- "membership"
    41 
    42 local
    43 
    44 instance set :: (type) ord ..
    45 instance set :: (type) minus ..
    46 
    47 
    48 subsection {* Additional concrete syntax *}
    49 
    50 syntax
    51   range         :: "('a => 'b) => 'b set"             -- "of function"
    52 
    53   "op ~:"       :: "'a => 'a set => bool"                 ("op ~:")  -- "non-membership"
    54   "op ~:"       :: "'a => 'a set => bool"                 ("(_/ ~: _)" [50, 51] 50)
    55 
    56   "@Finset"     :: "args => 'a set"                       ("{(_)}")
    57   "@Coll"       :: "pttrn => bool => 'a set"              ("(1{_./ _})")
    58   "@SetCompr"   :: "'a => idts => bool => 'a set"         ("(1{_ |/_./ _})")
    59 
    60   "@INTER1"     :: "pttrns => 'b set => 'b set"           ("(3INT _./ _)" 10)
    61   "@UNION1"     :: "pttrns => 'b set => 'b set"           ("(3UN _./ _)" 10)
    62   "@INTER"      :: "pttrn => 'a set => 'b set => 'b set"  ("(3INT _:_./ _)" 10)
    63   "@UNION"      :: "pttrn => 'a set => 'b set => 'b set"  ("(3UN _:_./ _)" 10)
    64 
    65   "_Ball"       :: "pttrn => 'a set => bool => bool"      ("(3ALL _:_./ _)" [0, 0, 10] 10)
    66   "_Bex"        :: "pttrn => 'a set => bool => bool"      ("(3EX _:_./ _)" [0, 0, 10] 10)
    67 
    68 syntax (HOL)
    69   "_Ball"       :: "pttrn => 'a set => bool => bool"      ("(3! _:_./ _)" [0, 0, 10] 10)
    70   "_Bex"        :: "pttrn => 'a set => bool => bool"      ("(3? _:_./ _)" [0, 0, 10] 10)
    71 
    72 translations
    73   "range f"     == "f`UNIV"
    74   "x ~: y"      == "~ (x : y)"
    75   "{x, xs}"     == "insert x {xs}"
    76   "{x}"         == "insert x {}"
    77   "{x. P}"      == "Collect (%x. P)"
    78   "UN x y. B"   == "UN x. UN y. B"
    79   "UN x. B"     == "UNION UNIV (%x. B)"
    80   "INT x y. B"  == "INT x. INT y. B"
    81   "INT x. B"    == "INTER UNIV (%x. B)"
    82   "UN x:A. B"   == "UNION A (%x. B)"
    83   "INT x:A. B"  == "INTER A (%x. B)"
    84   "ALL x:A. P"  == "Ball A (%x. P)"
    85   "EX x:A. P"   == "Bex A (%x. P)"
    86 
    87 syntax (output)
    88   "_setle"      :: "'a set => 'a set => bool"             ("op <=")
    89   "_setle"      :: "'a set => 'a set => bool"             ("(_/ <= _)" [50, 51] 50)
    90   "_setless"    :: "'a set => 'a set => bool"             ("op <")
    91   "_setless"    :: "'a set => 'a set => bool"             ("(_/ < _)" [50, 51] 50)
    92 
    93 syntax (xsymbols)
    94   "_setle"      :: "'a set => 'a set => bool"             ("op \<subseteq>")
    95   "_setle"      :: "'a set => 'a set => bool"             ("(_/ \<subseteq> _)" [50, 51] 50)
    96   "_setless"    :: "'a set => 'a set => bool"             ("op \<subset>")
    97   "_setless"    :: "'a set => 'a set => bool"             ("(_/ \<subset> _)" [50, 51] 50)
    98   "op Int"      :: "'a set => 'a set => 'a set"           (infixl "\<inter>" 70)
    99   "op Un"       :: "'a set => 'a set => 'a set"           (infixl "\<union>" 65)
   100   "op :"        :: "'a => 'a set => bool"                 ("op \<in>")
   101   "op :"        :: "'a => 'a set => bool"                 ("(_/ \<in> _)" [50, 51] 50)
   102   "op ~:"       :: "'a => 'a set => bool"                 ("op \<notin>")
   103   "op ~:"       :: "'a => 'a set => bool"                 ("(_/ \<notin> _)" [50, 51] 50)
   104   "@UNION1"     :: "pttrns => 'b set => 'b set"           ("(3\<Union>_./ _)" 10)
   105   "@INTER1"     :: "pttrns => 'b set => 'b set"           ("(3\<Inter>_./ _)" 10)
   106   "@UNION"      :: "pttrn => 'a set => 'b set => 'b set"  ("(3\<Union>_\<in>_./ _)" 10)
   107   "@INTER"      :: "pttrn => 'a set => 'b set => 'b set"  ("(3\<Inter>_\<in>_./ _)" 10)
   108   Union         :: "'a set set => 'a set"                 ("\<Union>_" [90] 90)
   109   Inter         :: "'a set set => 'a set"                 ("\<Inter>_" [90] 90)
   110   "_Ball"       :: "pttrn => 'a set => bool => bool"      ("(3\<forall>_\<in>_./ _)" [0, 0, 10] 10)
   111   "_Bex"        :: "pttrn => 'a set => bool => bool"      ("(3\<exists>_\<in>_./ _)" [0, 0, 10] 10)
   112 
   113 translations
   114   "op \<subseteq>" => "op <= :: _ set => _ set => bool"
   115   "op \<subset>" => "op <  :: _ set => _ set => bool"
   116 
   117 
   118 typed_print_translation {*
   119   let
   120     fun le_tr' _ (Type ("fun", (Type ("set", _) :: _))) ts =
   121           list_comb (Syntax.const "_setle", ts)
   122       | le_tr' _ _ _ = raise Match;
   123 
   124     fun less_tr' _ (Type ("fun", (Type ("set", _) :: _))) ts =
   125           list_comb (Syntax.const "_setless", ts)
   126       | less_tr' _ _ _ = raise Match;
   127   in [("op <=", le_tr'), ("op <", less_tr')] end
   128 *}
   129 
   130 text {*
   131   \medskip Translate between @{text "{e | x1...xn. P}"} and @{text
   132   "{u. EX x1..xn. u = e & P}"}; @{text "{y. EX x1..xn. y = e & P}"} is
   133   only translated if @{text "[0..n] subset bvs(e)"}.
   134 *}
   135 
   136 parse_translation {*
   137   let
   138     val ex_tr = snd (mk_binder_tr ("EX ", "Ex"));
   139 
   140     fun nvars (Const ("_idts", _) $ _ $ idts) = nvars idts + 1
   141       | nvars _ = 1;
   142 
   143     fun setcompr_tr [e, idts, b] =
   144       let
   145         val eq = Syntax.const "op =" $ Bound (nvars idts) $ e;
   146         val P = Syntax.const "op &" $ eq $ b;
   147         val exP = ex_tr [idts, P];
   148       in Syntax.const "Collect" $ Abs ("", dummyT, exP) end;
   149 
   150   in [("@SetCompr", setcompr_tr)] end;
   151 *}
   152 
   153 print_translation {*
   154   let
   155     val ex_tr' = snd (mk_binder_tr' ("Ex", "DUMMY"));
   156 
   157     fun setcompr_tr' [Abs (_, _, P)] =
   158       let
   159         fun check (Const ("Ex", _) $ Abs (_, _, P), n) = check (P, n + 1)
   160           | check (Const ("op &", _) $ (Const ("op =", _) $ Bound m $ e) $ P, n) =
   161               if n > 0 andalso m = n andalso not (loose_bvar1 (P, n)) andalso
   162                 ((0 upto (n - 1)) subset add_loose_bnos (e, 0, [])) then ()
   163               else raise Match;
   164 
   165         fun tr' (_ $ abs) =
   166           let val _ $ idts $ (_ $ (_ $ _ $ e) $ Q) = ex_tr' [abs]
   167           in Syntax.const "@SetCompr" $ e $ idts $ Q end;
   168       in check (P, 0); tr' P end;
   169   in [("Collect", setcompr_tr')] end;
   170 *}
   171 
   172 
   173 subsection {* Rules and definitions *}
   174 
   175 text {* Isomorphisms between predicates and sets. *}
   176 
   177 axioms
   178   mem_Collect_eq [iff]: "(a : {x. P(x)}) = P(a)"
   179   Collect_mem_eq [simp]: "{x. x:A} = A"
   180 
   181 defs
   182   Ball_def:     "Ball A P       == ALL x. x:A --> P(x)"
   183   Bex_def:      "Bex A P        == EX x. x:A & P(x)"
   184 
   185 defs (overloaded)
   186   subset_def:   "A <= B         == ALL x:A. x:B"
   187   psubset_def:  "A < B          == (A::'a set) <= B & ~ A=B"
   188   Compl_def:    "- A            == {x. ~x:A}"
   189   set_diff_def: "A - B          == {x. x:A & ~x:B}"
   190 
   191 defs
   192   Un_def:       "A Un B         == {x. x:A | x:B}"
   193   Int_def:      "A Int B        == {x. x:A & x:B}"
   194   INTER_def:    "INTER A B      == {y. ALL x:A. y: B(x)}"
   195   UNION_def:    "UNION A B      == {y. EX x:A. y: B(x)}"
   196   Inter_def:    "Inter S        == (INT x:S. x)"
   197   Union_def:    "Union S        == (UN x:S. x)"
   198   Pow_def:      "Pow A          == {B. B <= A}"
   199   empty_def:    "{}             == {x. False}"
   200   UNIV_def:     "UNIV           == {x. True}"
   201   insert_def:   "insert a B     == {x. x=a} Un B"
   202   image_def:    "f`A            == {y. EX x:A. y = f(x)}"
   203 
   204 
   205 subsection {* Lemmas and proof tool setup *}
   206 
   207 subsubsection {* Relating predicates and sets *}
   208 
   209 lemma CollectI: "P(a) ==> a : {x. P(x)}"
   210   by simp
   211 
   212 lemma CollectD: "a : {x. P(x)} ==> P(a)"
   213   by simp
   214 
   215 lemma set_ext: assumes prem: "(!!x. (x:A) = (x:B))" shows "A = B"
   216   apply (rule prem [THEN ext, THEN arg_cong, THEN box_equals])
   217    apply (rule Collect_mem_eq)
   218   apply (rule Collect_mem_eq)
   219   done
   220 
   221 lemma Collect_cong: "(!!x. P x = Q x) ==> {x. P(x)} = {x. Q(x)}"
   222   by simp
   223 
   224 lemmas CollectE = CollectD [elim_format]
   225 
   226 
   227 subsubsection {* Bounded quantifiers *}
   228 
   229 lemma ballI [intro!]: "(!!x. x:A ==> P x) ==> ALL x:A. P x"
   230   by (simp add: Ball_def)
   231 
   232 lemmas strip = impI allI ballI
   233 
   234 lemma bspec [dest?]: "ALL x:A. P x ==> x:A ==> P x"
   235   by (simp add: Ball_def)
   236 
   237 lemma ballE [elim]: "ALL x:A. P x ==> (P x ==> Q) ==> (x ~: A ==> Q) ==> Q"
   238   by (unfold Ball_def) blast
   239 
   240 text {*
   241   \medskip This tactic takes assumptions @{prop "ALL x:A. P x"} and
   242   @{prop "a:A"}; creates assumption @{prop "P a"}.
   243 *}
   244 
   245 ML {*
   246   local val ballE = thm "ballE"
   247   in fun ball_tac i = etac ballE i THEN contr_tac (i + 1) end;
   248 *}
   249 
   250 text {*
   251   Gives better instantiation for bound:
   252 *}
   253 
   254 ML_setup {*
   255   claset_ref() := claset() addbefore ("bspec", datac (thm "bspec") 1);
   256 *}
   257 
   258 lemma bexI [intro]: "P x ==> x:A ==> EX x:A. P x"
   259   -- {* Normally the best argument order: @{prop "P x"} constrains the
   260     choice of @{prop "x:A"}. *}
   261   by (unfold Bex_def) blast
   262 
   263 lemma rev_bexI [intro?]: "x:A ==> P x ==> EX x:A. P x"
   264   -- {* The best argument order when there is only one @{prop "x:A"}. *}
   265   by (unfold Bex_def) blast
   266 
   267 lemma bexCI: "(ALL x:A. ~P x ==> P a) ==> a:A ==> EX x:A. P x"
   268   by (unfold Bex_def) blast
   269 
   270 lemma bexE [elim!]: "EX x:A. P x ==> (!!x. x:A ==> P x ==> Q) ==> Q"
   271   by (unfold Bex_def) blast
   272 
   273 lemma ball_triv [simp]: "(ALL x:A. P) = ((EX x. x:A) --> P)"
   274   -- {* Trival rewrite rule. *}
   275   by (simp add: Ball_def)
   276 
   277 lemma bex_triv [simp]: "(EX x:A. P) = ((EX x. x:A) & P)"
   278   -- {* Dual form for existentials. *}
   279   by (simp add: Bex_def)
   280 
   281 lemma bex_triv_one_point1 [simp]: "(EX x:A. x = a) = (a:A)"
   282   by blast
   283 
   284 lemma bex_triv_one_point2 [simp]: "(EX x:A. a = x) = (a:A)"
   285   by blast
   286 
   287 lemma bex_one_point1 [simp]: "(EX x:A. x = a & P x) = (a:A & P a)"
   288   by blast
   289 
   290 lemma bex_one_point2 [simp]: "(EX x:A. a = x & P x) = (a:A & P a)"
   291   by blast
   292 
   293 lemma ball_one_point1 [simp]: "(ALL x:A. x = a --> P x) = (a:A --> P a)"
   294   by blast
   295 
   296 lemma ball_one_point2 [simp]: "(ALL x:A. a = x --> P x) = (a:A --> P a)"
   297   by blast
   298 
   299 ML_setup {*
   300   local
   301     val Ball_def = thm "Ball_def";
   302     val Bex_def = thm "Bex_def";
   303 
   304     val prove_bex_tac =
   305       rewrite_goals_tac [Bex_def] THEN Quantifier1.prove_one_point_ex_tac;
   306     val rearrange_bex = Quantifier1.rearrange_bex prove_bex_tac;
   307 
   308     val prove_ball_tac =
   309       rewrite_goals_tac [Ball_def] THEN Quantifier1.prove_one_point_all_tac;
   310     val rearrange_ball = Quantifier1.rearrange_ball prove_ball_tac;
   311   in
   312     val defBEX_regroup = Simplifier.simproc (Theory.sign_of (the_context ()))
   313       "defined BEX" ["EX x:A. P x & Q x"] rearrange_bex;
   314     val defBALL_regroup = Simplifier.simproc (Theory.sign_of (the_context ()))
   315       "defined BALL" ["ALL x:A. P x --> Q x"] rearrange_ball;
   316   end;
   317 
   318   Addsimprocs [defBALL_regroup, defBEX_regroup];
   319 *}
   320 
   321 
   322 subsubsection {* Congruence rules *}
   323 
   324 lemma ball_cong [cong]:
   325   "A = B ==> (!!x. x:B ==> P x = Q x) ==>
   326     (ALL x:A. P x) = (ALL x:B. Q x)"
   327   by (simp add: Ball_def)
   328 
   329 lemma bex_cong [cong]:
   330   "A = B ==> (!!x. x:B ==> P x = Q x) ==>
   331     (EX x:A. P x) = (EX x:B. Q x)"
   332   by (simp add: Bex_def cong: conj_cong)
   333 
   334 
   335 subsubsection {* Subsets *}
   336 
   337 lemma subsetI [intro!]: "(!!x. x:A ==> x:B) ==> A \<subseteq> B"
   338   by (simp add: subset_def)
   339 
   340 text {*
   341   \medskip Map the type @{text "'a set => anything"} to just @{typ
   342   'a}; for overloading constants whose first argument has type @{typ
   343   "'a set"}.
   344 *}
   345 
   346 ML {*
   347   fun overload_1st_set s = Blast.overloaded (s, HOLogic.dest_setT o domain_type);
   348 *}
   349 
   350 ML "
   351   (* While (:) is not, its type must be kept
   352     for overloading of = to work. *)
   353   Blast.overloaded (\"op :\", domain_type);
   354 
   355   overload_1st_set \"Ball\";            (*need UNION, INTER also?*)
   356   overload_1st_set \"Bex\";
   357 
   358   (*Image: retain the type of the set being expressed*)
   359   Blast.overloaded (\"image\", domain_type);
   360 "
   361 
   362 lemma subsetD [elim]: "A \<subseteq> B ==> c \<in> A ==> c \<in> B"
   363   -- {* Rule in Modus Ponens style. *}
   364   by (unfold subset_def) blast
   365 
   366 declare subsetD [intro?] -- FIXME
   367 
   368 lemma rev_subsetD: "c \<in> A ==> A \<subseteq> B ==> c \<in> B"
   369   -- {* The same, with reversed premises for use with @{text erule} --
   370       cf @{text rev_mp}. *}
   371   by (rule subsetD)
   372 
   373 declare rev_subsetD [intro?] -- FIXME
   374 
   375 text {*
   376   \medskip Converts @{prop "A \<subseteq> B"} to @{prop "x \<in> A ==> x \<in> B"}.
   377 *}
   378 
   379 ML {*
   380   local val rev_subsetD = thm "rev_subsetD"
   381   in fun impOfSubs th = th RSN (2, rev_subsetD) end;
   382 *}
   383 
   384 lemma subsetCE [elim]: "A \<subseteq>  B ==> (c \<notin> A ==> P) ==> (c \<in> B ==> P) ==> P"
   385   -- {* Classical elimination rule. *}
   386   by (unfold subset_def) blast
   387 
   388 text {*
   389   \medskip Takes assumptions @{prop "A \<subseteq> B"}; @{prop "c \<in> A"} and
   390   creates the assumption @{prop "c \<in> B"}.
   391 *}
   392 
   393 ML {*
   394   local val subsetCE = thm "subsetCE"
   395   in fun set_mp_tac i = etac subsetCE i THEN mp_tac i end;
   396 *}
   397 
   398 lemma contra_subsetD: "A \<subseteq> B ==> c \<notin> B ==> c \<notin> A"
   399   by blast
   400 
   401 lemma subset_refl: "A \<subseteq> A"
   402   by fast
   403 
   404 lemma subset_trans: "A \<subseteq> B ==> B \<subseteq> C ==> A \<subseteq> C"
   405   by blast
   406 
   407 
   408 subsubsection {* Equality *}
   409 
   410 lemma subset_antisym [intro!]: "A \<subseteq> B ==> B \<subseteq> A ==> A = B"
   411   -- {* Anti-symmetry of the subset relation. *}
   412   by (rules intro: set_ext subsetD)
   413 
   414 lemmas equalityI [intro!] = subset_antisym
   415 
   416 text {*
   417   \medskip Equality rules from ZF set theory -- are they appropriate
   418   here?
   419 *}
   420 
   421 lemma equalityD1: "A = B ==> A \<subseteq> B"
   422   by (simp add: subset_refl)
   423 
   424 lemma equalityD2: "A = B ==> B \<subseteq> A"
   425   by (simp add: subset_refl)
   426 
   427 text {*
   428   \medskip Be careful when adding this to the claset as @{text
   429   subset_empty} is in the simpset: @{prop "A = {}"} goes to @{prop "{}
   430   \<subseteq> A"} and @{prop "A \<subseteq> {}"} and then back to @{prop "A = {}"}!
   431 *}
   432 
   433 lemma equalityE: "A = B ==> (A \<subseteq> B ==> B \<subseteq> A ==> P) ==> P"
   434   by (simp add: subset_refl)
   435 
   436 lemma equalityCE [elim]:
   437     "A = B ==> (c \<in> A ==> c \<in> B ==> P) ==> (c \<notin> A ==> c \<notin> B ==> P) ==> P"
   438   by blast
   439 
   440 text {*
   441   \medskip Lemma for creating induction formulae -- for "pattern
   442   matching" on @{text p}.  To make the induction hypotheses usable,
   443   apply @{text spec} or @{text bspec} to put universal quantifiers over the free
   444   variables in @{text p}.
   445 *}
   446 
   447 lemma setup_induction: "p:A ==> (!!z. z:A ==> p = z --> R) ==> R"
   448   by simp
   449 
   450 lemma eqset_imp_iff: "A = B ==> (x : A) = (x : B)"
   451   by simp
   452 
   453 
   454 subsubsection {* The universal set -- UNIV *}
   455 
   456 lemma UNIV_I [simp]: "x : UNIV"
   457   by (simp add: UNIV_def)
   458 
   459 declare UNIV_I [intro]  -- {* unsafe makes it less likely to cause problems *}
   460 
   461 lemma UNIV_witness [intro?]: "EX x. x : UNIV"
   462   by simp
   463 
   464 lemma subset_UNIV: "A \<subseteq> UNIV"
   465   by (rule subsetI) (rule UNIV_I)
   466 
   467 text {*
   468   \medskip Eta-contracting these two rules (to remove @{text P})
   469   causes them to be ignored because of their interaction with
   470   congruence rules.
   471 *}
   472 
   473 lemma ball_UNIV [simp]: "Ball UNIV P = All P"
   474   by (simp add: Ball_def)
   475 
   476 lemma bex_UNIV [simp]: "Bex UNIV P = Ex P"
   477   by (simp add: Bex_def)
   478 
   479 
   480 subsubsection {* The empty set *}
   481 
   482 lemma empty_iff [simp]: "(c : {}) = False"
   483   by (simp add: empty_def)
   484 
   485 lemma emptyE [elim!]: "a : {} ==> P"
   486   by simp
   487 
   488 lemma empty_subsetI [iff]: "{} \<subseteq> A"
   489     -- {* One effect is to delete the ASSUMPTION @{prop "{} <= A"} *}
   490   by blast
   491 
   492 lemma equals0I: "(!!y. y \<in> A ==> False) ==> A = {}"
   493   by blast
   494 
   495 lemma equals0D: "A = {} ==> a \<notin> A"
   496     -- {* Use for reasoning about disjointness: @{prop "A Int B = {}"} *}
   497   by blast
   498 
   499 lemma ball_empty [simp]: "Ball {} P = True"
   500   by (simp add: Ball_def)
   501 
   502 lemma bex_empty [simp]: "Bex {} P = False"
   503   by (simp add: Bex_def)
   504 
   505 lemma UNIV_not_empty [iff]: "UNIV ~= {}"
   506   by (blast elim: equalityE)
   507 
   508 
   509 subsubsection {* The Powerset operator -- Pow *}
   510 
   511 lemma Pow_iff [iff]: "(A \<in> Pow B) = (A \<subseteq> B)"
   512   by (simp add: Pow_def)
   513 
   514 lemma PowI: "A \<subseteq> B ==> A \<in> Pow B"
   515   by (simp add: Pow_def)
   516 
   517 lemma PowD: "A \<in> Pow B ==> A \<subseteq> B"
   518   by (simp add: Pow_def)
   519 
   520 lemma Pow_bottom: "{} \<in> Pow B"
   521   by simp
   522 
   523 lemma Pow_top: "A \<in> Pow A"
   524   by (simp add: subset_refl)
   525 
   526 
   527 subsubsection {* Set complement *}
   528 
   529 lemma Compl_iff [simp]: "(c \<in> -A) = (c \<notin> A)"
   530   by (unfold Compl_def) blast
   531 
   532 lemma ComplI [intro!]: "(c \<in> A ==> False) ==> c \<in> -A"
   533   by (unfold Compl_def) blast
   534 
   535 text {*
   536   \medskip This form, with negated conclusion, works well with the
   537   Classical prover.  Negated assumptions behave like formulae on the
   538   right side of the notional turnstile ... *}
   539 
   540 lemma ComplD: "c : -A ==> c~:A"
   541   by (unfold Compl_def) blast
   542 
   543 lemmas ComplE [elim!] = ComplD [elim_format]
   544 
   545 
   546 subsubsection {* Binary union -- Un *}
   547 
   548 lemma Un_iff [simp]: "(c : A Un B) = (c:A | c:B)"
   549   by (unfold Un_def) blast
   550 
   551 lemma UnI1 [elim?]: "c:A ==> c : A Un B"
   552   by simp
   553 
   554 lemma UnI2 [elim?]: "c:B ==> c : A Un B"
   555   by simp
   556 
   557 text {*
   558   \medskip Classical introduction rule: no commitment to @{prop A} vs
   559   @{prop B}.
   560 *}
   561 
   562 lemma UnCI [intro!]: "(c~:B ==> c:A) ==> c : A Un B"
   563   by auto
   564 
   565 lemma UnE [elim!]: "c : A Un B ==> (c:A ==> P) ==> (c:B ==> P) ==> P"
   566   by (unfold Un_def) blast
   567 
   568 
   569 subsubsection {* Binary intersection -- Int *}
   570 
   571 lemma Int_iff [simp]: "(c : A Int B) = (c:A & c:B)"
   572   by (unfold Int_def) blast
   573 
   574 lemma IntI [intro!]: "c:A ==> c:B ==> c : A Int B"
   575   by simp
   576 
   577 lemma IntD1: "c : A Int B ==> c:A"
   578   by simp
   579 
   580 lemma IntD2: "c : A Int B ==> c:B"
   581   by simp
   582 
   583 lemma IntE [elim!]: "c : A Int B ==> (c:A ==> c:B ==> P) ==> P"
   584   by simp
   585 
   586 
   587 subsubsection {* Set difference *}
   588 
   589 lemma Diff_iff [simp]: "(c : A - B) = (c:A & c~:B)"
   590   by (unfold set_diff_def) blast
   591 
   592 lemma DiffI [intro!]: "c : A ==> c ~: B ==> c : A - B"
   593   by simp
   594 
   595 lemma DiffD1: "c : A - B ==> c : A"
   596   by simp
   597 
   598 lemma DiffD2: "c : A - B ==> c : B ==> P"
   599   by simp
   600 
   601 lemma DiffE [elim!]: "c : A - B ==> (c:A ==> c~:B ==> P) ==> P"
   602   by simp
   603 
   604 
   605 subsubsection {* Augmenting a set -- insert *}
   606 
   607 lemma insert_iff [simp]: "(a : insert b A) = (a = b | a:A)"
   608   by (unfold insert_def) blast
   609 
   610 lemma insertI1: "a : insert a B"
   611   by simp
   612 
   613 lemma insertI2: "a : B ==> a : insert b B"
   614   by simp
   615 
   616 lemma insertE [elim!]: "a : insert b A ==> (a = b ==> P) ==> (a:A ==> P) ==> P"
   617   by (unfold insert_def) blast
   618 
   619 lemma insertCI [intro!]: "(a~:B ==> a = b) ==> a: insert b B"
   620   -- {* Classical introduction rule. *}
   621   by auto
   622 
   623 lemma subset_insert_iff: "(A \<subseteq> insert x B) = (if x:A then A - {x} \<subseteq> B else A \<subseteq> B)"
   624   by auto
   625 
   626 
   627 subsubsection {* Singletons, using insert *}
   628 
   629 lemma singletonI [intro!]: "a : {a}"
   630     -- {* Redundant? But unlike @{text insertCI}, it proves the subgoal immediately! *}
   631   by (rule insertI1)
   632 
   633 lemma singletonD: "b : {a} ==> b = a"
   634   by blast
   635 
   636 lemmas singletonE [elim!] = singletonD [elim_format]
   637 
   638 lemma singleton_iff: "(b : {a}) = (b = a)"
   639   by blast
   640 
   641 lemma singleton_inject [dest!]: "{a} = {b} ==> a = b"
   642   by blast
   643 
   644 lemma singleton_insert_inj_eq [iff]: "({b} = insert a A) = (a = b & A \<subseteq> {b})"
   645   by blast
   646 
   647 lemma singleton_insert_inj_eq' [iff]: "(insert a A = {b}) = (a = b & A \<subseteq> {b})"
   648   by blast
   649 
   650 lemma subset_singletonD: "A \<subseteq> {x} ==> A = {} | A = {x}"
   651   by fast
   652 
   653 lemma singleton_conv [simp]: "{x. x = a} = {a}"
   654   by blast
   655 
   656 lemma singleton_conv2 [simp]: "{x. a = x} = {a}"
   657   by blast
   658 
   659 lemma diff_single_insert: "A - {x} \<subseteq> B ==> x \<in> A ==> A \<subseteq> insert x B"
   660   by blast
   661 
   662 
   663 subsubsection {* Unions of families *}
   664 
   665 text {*
   666   @{term [source] "UN x:A. B x"} is @{term "Union (B`A)"}.
   667 *}
   668 
   669 lemma UN_iff [simp]: "(b: (UN x:A. B x)) = (EX x:A. b: B x)"
   670   by (unfold UNION_def) blast
   671 
   672 lemma UN_I [intro]: "a:A ==> b: B a ==> b: (UN x:A. B x)"
   673   -- {* The order of the premises presupposes that @{term A} is rigid;
   674     @{term b} may be flexible. *}
   675   by auto
   676 
   677 lemma UN_E [elim!]: "b : (UN x:A. B x) ==> (!!x. x:A ==> b: B x ==> R) ==> R"
   678   by (unfold UNION_def) blast
   679 
   680 lemma UN_cong [cong]:
   681     "A = B ==> (!!x. x:B ==> C x = D x) ==> (UN x:A. C x) = (UN x:B. D x)"
   682   by (simp add: UNION_def)
   683 
   684 
   685 subsubsection {* Intersections of families *}
   686 
   687 text {* @{term [source] "INT x:A. B x"} is @{term "Inter (B`A)"}. *}
   688 
   689 lemma INT_iff [simp]: "(b: (INT x:A. B x)) = (ALL x:A. b: B x)"
   690   by (unfold INTER_def) blast
   691 
   692 lemma INT_I [intro!]: "(!!x. x:A ==> b: B x) ==> b : (INT x:A. B x)"
   693   by (unfold INTER_def) blast
   694 
   695 lemma INT_D [elim]: "b : (INT x:A. B x) ==> a:A ==> b: B a"
   696   by auto
   697 
   698 lemma INT_E [elim]: "b : (INT x:A. B x) ==> (b: B a ==> R) ==> (a~:A ==> R) ==> R"
   699   -- {* "Classical" elimination -- by the Excluded Middle on @{prop "a:A"}. *}
   700   by (unfold INTER_def) blast
   701 
   702 lemma INT_cong [cong]:
   703     "A = B ==> (!!x. x:B ==> C x = D x) ==> (INT x:A. C x) = (INT x:B. D x)"
   704   by (simp add: INTER_def)
   705 
   706 
   707 subsubsection {* Union *}
   708 
   709 lemma Union_iff [simp]: "(A : Union C) = (EX X:C. A:X)"
   710   by (unfold Union_def) blast
   711 
   712 lemma UnionI [intro]: "X:C ==> A:X ==> A : Union C"
   713   -- {* The order of the premises presupposes that @{term C} is rigid;
   714     @{term A} may be flexible. *}
   715   by auto
   716 
   717 lemma UnionE [elim!]: "A : Union C ==> (!!X. A:X ==> X:C ==> R) ==> R"
   718   by (unfold Union_def) blast
   719 
   720 
   721 subsubsection {* Inter *}
   722 
   723 lemma Inter_iff [simp]: "(A : Inter C) = (ALL X:C. A:X)"
   724   by (unfold Inter_def) blast
   725 
   726 lemma InterI [intro!]: "(!!X. X:C ==> A:X) ==> A : Inter C"
   727   by (simp add: Inter_def)
   728 
   729 text {*
   730   \medskip A ``destruct'' rule -- every @{term X} in @{term C}
   731   contains @{term A} as an element, but @{prop "A:X"} can hold when
   732   @{prop "X:C"} does not!  This rule is analogous to @{text spec}.
   733 *}
   734 
   735 lemma InterD [elim]: "A : Inter C ==> X:C ==> A:X"
   736   by auto
   737 
   738 lemma InterE [elim]: "A : Inter C ==> (X~:C ==> R) ==> (A:X ==> R) ==> R"
   739   -- {* ``Classical'' elimination rule -- does not require proving
   740     @{prop "X:C"}. *}
   741   by (unfold Inter_def) blast
   742 
   743 text {*
   744   \medskip Image of a set under a function.  Frequently @{term b} does
   745   not have the syntactic form of @{term "f x"}.
   746 *}
   747 
   748 lemma image_eqI [simp, intro]: "b = f x ==> x:A ==> b : f`A"
   749   by (unfold image_def) blast
   750 
   751 lemma imageI: "x : A ==> f x : f ` A"
   752   by (rule image_eqI) (rule refl)
   753 
   754 lemma rev_image_eqI: "x:A ==> b = f x ==> b : f`A"
   755   -- {* This version's more effective when we already have the
   756     required @{term x}. *}
   757   by (unfold image_def) blast
   758 
   759 lemma imageE [elim!]:
   760   "b : (%x. f x)`A ==> (!!x. b = f x ==> x:A ==> P) ==> P"
   761   -- {* The eta-expansion gives variable-name preservation. *}
   762   by (unfold image_def) blast
   763 
   764 lemma image_Un: "f`(A Un B) = f`A Un f`B"
   765   by blast
   766 
   767 lemma image_iff: "(z : f`A) = (EX x:A. z = f x)"
   768   by blast
   769 
   770 lemma image_subset_iff: "(f`A \<subseteq> B) = (\<forall>x\<in>A. f x \<in> B)"
   771   -- {* This rewrite rule would confuse users if made default. *}
   772   by blast
   773 
   774 lemma subset_image_iff: "(B \<subseteq> f`A) = (EX AA. AA \<subseteq> A & B = f`AA)"
   775   apply safe
   776    prefer 2 apply fast
   777   apply (rule_tac x = "{a. a : A & f a : B}" in exI)
   778   apply fast
   779   done
   780 
   781 lemma image_subsetI: "(!!x. x \<in> A ==> f x \<in> B) ==> f`A \<subseteq> B"
   782   -- {* Replaces the three steps @{text subsetI}, @{text imageE},
   783     @{text hypsubst}, but breaks too many existing proofs. *}
   784   by blast
   785 
   786 text {*
   787   \medskip Range of a function -- just a translation for image!
   788 *}
   789 
   790 lemma range_eqI: "b = f x ==> b \<in> range f"
   791   by simp
   792 
   793 lemma rangeI: "f x \<in> range f"
   794   by simp
   795 
   796 lemma rangeE [elim?]: "b \<in> range (\<lambda>x. f x) ==> (!!x. b = f x ==> P) ==> P"
   797   by blast
   798 
   799 
   800 subsubsection {* Set reasoning tools *}
   801 
   802 text {*
   803   Rewrite rules for boolean case-splitting: faster than @{text
   804   "split_if [split]"}.
   805 *}
   806 
   807 lemma split_if_eq1: "((if Q then x else y) = b) = ((Q --> x = b) & (~ Q --> y = b))"
   808   by (rule split_if)
   809 
   810 lemma split_if_eq2: "(a = (if Q then x else y)) = ((Q --> a = x) & (~ Q --> a = y))"
   811   by (rule split_if)
   812 
   813 text {*
   814   Split ifs on either side of the membership relation.  Not for @{text
   815   "[simp]"} -- can cause goals to blow up!
   816 *}
   817 
   818 lemma split_if_mem1: "((if Q then x else y) : b) = ((Q --> x : b) & (~ Q --> y : b))"
   819   by (rule split_if)
   820 
   821 lemma split_if_mem2: "(a : (if Q then x else y)) = ((Q --> a : x) & (~ Q --> a : y))"
   822   by (rule split_if)
   823 
   824 lemmas split_ifs = if_bool_eq_conj split_if_eq1 split_if_eq2 split_if_mem1 split_if_mem2
   825 
   826 lemmas mem_simps =
   827   insert_iff empty_iff Un_iff Int_iff Compl_iff Diff_iff
   828   mem_Collect_eq UN_iff Union_iff INT_iff Inter_iff
   829   -- {* Each of these has ALREADY been added @{text "[simp]"} above. *}
   830 
   831 (*Would like to add these, but the existing code only searches for the
   832   outer-level constant, which in this case is just "op :"; we instead need
   833   to use term-nets to associate patterns with rules.  Also, if a rule fails to
   834   apply, then the formula should be kept.
   835   [("uminus", Compl_iff RS iffD1), ("op -", [Diff_iff RS iffD1]),
   836    ("op Int", [IntD1,IntD2]),
   837    ("Collect", [CollectD]), ("Inter", [InterD]), ("INTER", [INT_D])]
   838  *)
   839 
   840 ML_setup {*
   841   val mksimps_pairs = [("Ball", [thm "bspec"])] @ mksimps_pairs;
   842   simpset_ref() := simpset() setmksimps (mksimps mksimps_pairs);
   843 *}
   844 
   845 declare subset_UNIV [simp] subset_refl [simp]
   846 
   847 
   848 subsubsection {* The ``proper subset'' relation *}
   849 
   850 lemma psubsetI [intro!]: "A \<subseteq> B ==> A \<noteq> B ==> A \<subset> B"
   851   by (unfold psubset_def) blast
   852 
   853 lemma psubset_insert_iff:
   854   "(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)"
   855   by (auto simp add: psubset_def subset_insert_iff)
   856 
   857 lemma psubset_eq: "(A \<subset> B) = (A \<subseteq> B & A \<noteq> B)"
   858   by (simp only: psubset_def)
   859 
   860 lemma psubset_imp_subset: "A \<subset> B ==> A \<subseteq> B"
   861   by (simp add: psubset_eq)
   862 
   863 lemma psubset_subset_trans: "A \<subset> B ==> B \<subseteq> C ==> A \<subset> C"
   864   by (auto simp add: psubset_eq)
   865 
   866 lemma subset_psubset_trans: "A \<subseteq> B ==> B \<subset> C ==> A \<subset> C"
   867   by (auto simp add: psubset_eq)
   868 
   869 lemma psubset_imp_ex_mem: "A \<subset> B ==> \<exists>b. b \<in> (B - A)"
   870   by (unfold psubset_def) blast
   871 
   872 lemma atomize_ball:
   873     "(!!x. x \<in> A ==> P x) == Trueprop (\<forall>x\<in>A. P x)"
   874   by (simp only: Ball_def atomize_all atomize_imp)
   875 
   876 declare atomize_ball [symmetric, rulify]
   877 
   878 
   879 subsection {* Further set-theory lemmas *}
   880 
   881 subsubsection {* Derived rules involving subsets. *}
   882 
   883 text {* @{text insert}. *}
   884 
   885 lemma subset_insertI: "B \<subseteq> insert a B"
   886   apply (rule subsetI)
   887   apply (erule insertI2)
   888   done
   889 
   890 lemma subset_insert: "x \<notin> A ==> (A \<subseteq> insert x B) = (A \<subseteq> B)"
   891   by blast
   892 
   893 
   894 text {* \medskip Big Union -- least upper bound of a set. *}
   895 
   896 lemma Union_upper: "B \<in> A ==> B \<subseteq> Union A"
   897   by (rules intro: subsetI UnionI)
   898 
   899 lemma Union_least: "(!!X. X \<in> A ==> X \<subseteq> C) ==> Union A \<subseteq> C"
   900   by (rules intro: subsetI elim: UnionE dest: subsetD)
   901 
   902 
   903 text {* \medskip General union. *}
   904 
   905 lemma UN_upper: "a \<in> A ==> B a \<subseteq> (\<Union>x\<in>A. B x)"
   906   by blast
   907 
   908 lemma UN_least: "(!!x. x \<in> A ==> B x \<subseteq> C) ==> (\<Union>x\<in>A. B x) \<subseteq> C"
   909   by (rules intro: subsetI elim: UN_E dest: subsetD)
   910 
   911 
   912 text {* \medskip Big Intersection -- greatest lower bound of a set. *}
   913 
   914 lemma Inter_lower: "B \<in> A ==> Inter A \<subseteq> B"
   915   by blast
   916 
   917 lemma Inter_greatest: "(!!X. X \<in> A ==> C \<subseteq> X) ==> C \<subseteq> Inter A"
   918   by (rules intro: InterI subsetI dest: subsetD)
   919 
   920 lemma INT_lower: "a \<in> A ==> (\<Inter>x\<in>A. B x) \<subseteq> B a"
   921   by blast
   922 
   923 lemma INT_greatest: "(!!x. x \<in> A ==> C \<subseteq> B x) ==> C \<subseteq> (\<Inter>x\<in>A. B x)"
   924   by (rules intro: INT_I subsetI dest: subsetD)
   925 
   926 
   927 text {* \medskip Finite Union -- the least upper bound of two sets. *}
   928 
   929 lemma Un_upper1: "A \<subseteq> A \<union> B"
   930   by blast
   931 
   932 lemma Un_upper2: "B \<subseteq> A \<union> B"
   933   by blast
   934 
   935 lemma Un_least: "A \<subseteq> C ==> B \<subseteq> C ==> A \<union> B \<subseteq> C"
   936   by blast
   937 
   938 
   939 text {* \medskip Finite Intersection -- the greatest lower bound of two sets. *}
   940 
   941 lemma Int_lower1: "A \<inter> B \<subseteq> A"
   942   by blast
   943 
   944 lemma Int_lower2: "A \<inter> B \<subseteq> B"
   945   by blast
   946 
   947 lemma Int_greatest: "C \<subseteq> A ==> C \<subseteq> B ==> C \<subseteq> A \<inter> B"
   948   by blast
   949 
   950 
   951 text {* \medskip Set difference. *}
   952 
   953 lemma Diff_subset: "A - B \<subseteq> A"
   954   by blast
   955 
   956 
   957 text {* \medskip Monotonicity. *}
   958 
   959 lemma mono_Un: includes mono shows "f A \<union> f B \<subseteq> f (A \<union> B)"
   960   apply (rule Un_least)
   961    apply (rule Un_upper1 [THEN mono])
   962   apply (rule Un_upper2 [THEN mono])
   963   done
   964 
   965 lemma mono_Int: includes mono shows "f (A \<inter> B) \<subseteq> f A \<inter> f B"
   966   apply (rule Int_greatest)
   967    apply (rule Int_lower1 [THEN mono])
   968   apply (rule Int_lower2 [THEN mono])
   969   done
   970 
   971 
   972 subsubsection {* Equalities involving union, intersection, inclusion, etc. *}
   973 
   974 text {* @{text "{}"}. *}
   975 
   976 lemma Collect_const [simp]: "{s. P} = (if P then UNIV else {})"
   977   -- {* supersedes @{text "Collect_False_empty"} *}
   978   by auto
   979 
   980 lemma subset_empty [simp]: "(A \<subseteq> {}) = (A = {})"
   981   by blast
   982 
   983 lemma not_psubset_empty [iff]: "\<not> (A < {})"
   984   by (unfold psubset_def) blast
   985 
   986 lemma Collect_empty_eq [simp]: "(Collect P = {}) = (\<forall>x. \<not> P x)"
   987   by auto
   988 
   989 lemma Collect_neg_eq: "{x. \<not> P x} = - {x. P x}"
   990   by blast
   991 
   992 lemma Collect_disj_eq: "{x. P x | Q x} = {x. P x} \<union> {x. Q x}"
   993   by blast
   994 
   995 lemma Collect_conj_eq: "{x. P x & Q x} = {x. P x} \<inter> {x. Q x}"
   996   by blast
   997 
   998 lemma Collect_all_eq: "{x. \<forall>y. P x y} = (\<Inter>y. {x. P x y})"
   999   by blast
  1000 
  1001 lemma Collect_ball_eq: "{x. \<forall>y\<in>A. P x y} = (\<Inter>y\<in>A. {x. P x y})"
  1002   by blast
  1003 
  1004 lemma Collect_ex_eq: "{x. \<exists>y. P x y} = (\<Union>y. {x. P x y})"
  1005   by blast
  1006 
  1007 lemma Collect_bex_eq: "{x. \<exists>y\<in>A. P x y} = (\<Union>y\<in>A. {x. P x y})"
  1008   by blast
  1009 
  1010 
  1011 text {* \medskip @{text insert}. *}
  1012 
  1013 lemma insert_is_Un: "insert a A = {a} Un A"
  1014   -- {* NOT SUITABLE FOR REWRITING since @{text "{a} == insert a {}"} *}
  1015   by blast
  1016 
  1017 lemma insert_not_empty [simp]: "insert a A \<noteq> {}"
  1018   by blast
  1019 
  1020 lemmas empty_not_insert [simp] = insert_not_empty [symmetric, standard]
  1021 
  1022 lemma insert_absorb: "a \<in> A ==> insert a A = A"
  1023   -- {* @{text "[simp]"} causes recursive calls when there are nested inserts *}
  1024   -- {* with \emph{quadratic} running time *}
  1025   by blast
  1026 
  1027 lemma insert_absorb2 [simp]: "insert x (insert x A) = insert x A"
  1028   by blast
  1029 
  1030 lemma insert_commute: "insert x (insert y A) = insert y (insert x A)"
  1031   by blast
  1032 
  1033 lemma insert_subset [simp]: "(insert x A \<subseteq> B) = (x \<in> B & A \<subseteq> B)"
  1034   by blast
  1035 
  1036 lemma mk_disjoint_insert: "a \<in> A ==> \<exists>B. A = insert a B & a \<notin> B"
  1037   -- {* use new @{text B} rather than @{text "A - {a}"} to avoid infinite unfolding *}
  1038   apply (rule_tac x = "A - {a}" in exI)
  1039   apply blast
  1040   done
  1041 
  1042 lemma insert_Collect: "insert a (Collect P) = {u. u \<noteq> a --> P u}"
  1043   by auto
  1044 
  1045 lemma UN_insert_distrib: "u \<in> A ==> (\<Union>x\<in>A. insert a (B x)) = insert a (\<Union>x\<in>A. B x)"
  1046   by blast
  1047 
  1048 lemma insert_disjoint[simp]:
  1049  "(insert a A \<inter> B = {}) = (a \<notin> B \<and> A \<inter> B = {})"
  1050 by blast
  1051 
  1052 lemma disjoint_insert[simp]:
  1053  "(B \<inter> insert a A = {}) = (a \<notin> B \<and> B \<inter> A = {})"
  1054 by blast
  1055 
  1056 text {* \medskip @{text image}. *}
  1057 
  1058 lemma image_empty [simp]: "f`{} = {}"
  1059   by blast
  1060 
  1061 lemma image_insert [simp]: "f ` insert a B = insert (f a) (f`B)"
  1062   by blast
  1063 
  1064 lemma image_constant: "x \<in> A ==> (\<lambda>x. c) ` A = {c}"
  1065   by blast
  1066 
  1067 lemma image_image: "f ` (g ` A) = (\<lambda>x. f (g x)) ` A"
  1068   by blast
  1069 
  1070 lemma insert_image [simp]: "x \<in> A ==> insert (f x) (f`A) = f`A"
  1071   by blast
  1072 
  1073 lemma image_is_empty [iff]: "(f`A = {}) = (A = {})"
  1074   by blast
  1075 
  1076 lemma image_Collect: "f ` {x. P x} = {f x | x. P x}"
  1077   -- {* NOT suitable as a default simprule: the RHS isn't simpler than the LHS, *}
  1078   -- {* with its implicit quantifier and conjunction.  Also image enjoys better *}
  1079   -- {* equational properties than does the RHS. *}
  1080   by blast
  1081 
  1082 lemma if_image_distrib [simp]:
  1083   "(\<lambda>x. if P x then f x else g x) ` S
  1084     = (f ` (S \<inter> {x. P x})) \<union> (g ` (S \<inter> {x. \<not> P x}))"
  1085   by (auto simp add: image_def)
  1086 
  1087 lemma image_cong: "M = N ==> (!!x. x \<in> N ==> f x = g x) ==> f`M = g`N"
  1088   by (simp add: image_def)
  1089 
  1090 
  1091 text {* \medskip @{text range}. *}
  1092 
  1093 lemma full_SetCompr_eq: "{u. \<exists>x. u = f x} = range f"
  1094   by auto
  1095 
  1096 lemma range_composition [simp]: "range (\<lambda>x. f (g x)) = f`range g"
  1097   apply (subst image_image)
  1098   apply simp
  1099   done
  1100 
  1101 
  1102 text {* \medskip @{text Int} *}
  1103 
  1104 lemma Int_absorb [simp]: "A \<inter> A = A"
  1105   by blast
  1106 
  1107 lemma Int_left_absorb: "A \<inter> (A \<inter> B) = A \<inter> B"
  1108   by blast
  1109 
  1110 lemma Int_commute: "A \<inter> B = B \<inter> A"
  1111   by blast
  1112 
  1113 lemma Int_left_commute: "A \<inter> (B \<inter> C) = B \<inter> (A \<inter> C)"
  1114   by blast
  1115 
  1116 lemma Int_assoc: "(A \<inter> B) \<inter> C = A \<inter> (B \<inter> C)"
  1117   by blast
  1118 
  1119 lemmas Int_ac = Int_assoc Int_left_absorb Int_commute Int_left_commute
  1120   -- {* Intersection is an AC-operator *}
  1121 
  1122 lemma Int_absorb1: "B \<subseteq> A ==> A \<inter> B = B"
  1123   by blast
  1124 
  1125 lemma Int_absorb2: "A \<subseteq> B ==> A \<inter> B = A"
  1126   by blast
  1127 
  1128 lemma Int_empty_left [simp]: "{} \<inter> B = {}"
  1129   by blast
  1130 
  1131 lemma Int_empty_right [simp]: "A \<inter> {} = {}"
  1132   by blast
  1133 
  1134 lemma disjoint_eq_subset_Compl: "(A \<inter> B = {}) = (A \<subseteq> -B)"
  1135   by blast
  1136 
  1137 lemma disjoint_iff_not_equal: "(A \<inter> B = {}) = (\<forall>x\<in>A. \<forall>y\<in>B. x \<noteq> y)"
  1138   by blast
  1139 
  1140 lemma Int_UNIV_left [simp]: "UNIV \<inter> B = B"
  1141   by blast
  1142 
  1143 lemma Int_UNIV_right [simp]: "A \<inter> UNIV = A"
  1144   by blast
  1145 
  1146 lemma Int_eq_Inter: "A \<inter> B = \<Inter>{A, B}"
  1147   by blast
  1148 
  1149 lemma Int_Un_distrib: "A \<inter> (B \<union> C) = (A \<inter> B) \<union> (A \<inter> C)"
  1150   by blast
  1151 
  1152 lemma Int_Un_distrib2: "(B \<union> C) \<inter> A = (B \<inter> A) \<union> (C \<inter> A)"
  1153   by blast
  1154 
  1155 lemma Int_UNIV [simp]: "(A \<inter> B = UNIV) = (A = UNIV & B = UNIV)"
  1156   by blast
  1157 
  1158 lemma Int_subset_iff: "(C \<subseteq> A \<inter> B) = (C \<subseteq> A & C \<subseteq> B)"
  1159   by blast
  1160 
  1161 lemma Int_Collect: "(x \<in> A \<inter> {x. P x}) = (x \<in> A & P x)"
  1162   by blast
  1163 
  1164 
  1165 text {* \medskip @{text Un}. *}
  1166 
  1167 lemma Un_absorb [simp]: "A \<union> A = A"
  1168   by blast
  1169 
  1170 lemma Un_left_absorb: "A \<union> (A \<union> B) = A \<union> B"
  1171   by blast
  1172 
  1173 lemma Un_commute: "A \<union> B = B \<union> A"
  1174   by blast
  1175 
  1176 lemma Un_left_commute: "A \<union> (B \<union> C) = B \<union> (A \<union> C)"
  1177   by blast
  1178 
  1179 lemma Un_assoc: "(A \<union> B) \<union> C = A \<union> (B \<union> C)"
  1180   by blast
  1181 
  1182 lemmas Un_ac = Un_assoc Un_left_absorb Un_commute Un_left_commute
  1183   -- {* Union is an AC-operator *}
  1184 
  1185 lemma Un_absorb1: "A \<subseteq> B ==> A \<union> B = B"
  1186   by blast
  1187 
  1188 lemma Un_absorb2: "B \<subseteq> A ==> A \<union> B = A"
  1189   by blast
  1190 
  1191 lemma Un_empty_left [simp]: "{} \<union> B = B"
  1192   by blast
  1193 
  1194 lemma Un_empty_right [simp]: "A \<union> {} = A"
  1195   by blast
  1196 
  1197 lemma Un_UNIV_left [simp]: "UNIV \<union> B = UNIV"
  1198   by blast
  1199 
  1200 lemma Un_UNIV_right [simp]: "A \<union> UNIV = UNIV"
  1201   by blast
  1202 
  1203 lemma Un_eq_Union: "A \<union> B = \<Union>{A, B}"
  1204   by blast
  1205 
  1206 lemma Un_insert_left [simp]: "(insert a B) \<union> C = insert a (B \<union> C)"
  1207   by blast
  1208 
  1209 lemma Un_insert_right [simp]: "A \<union> (insert a B) = insert a (A \<union> B)"
  1210   by blast
  1211 
  1212 lemma Int_insert_left:
  1213     "(insert a B) Int C = (if a \<in> C then insert a (B \<inter> C) else B \<inter> C)"
  1214   by auto
  1215 
  1216 lemma Int_insert_right:
  1217     "A \<inter> (insert a B) = (if a \<in> A then insert a (A \<inter> B) else A \<inter> B)"
  1218   by auto
  1219 
  1220 lemma Un_Int_distrib: "A \<union> (B \<inter> C) = (A \<union> B) \<inter> (A \<union> C)"
  1221   by blast
  1222 
  1223 lemma Un_Int_distrib2: "(B \<inter> C) \<union> A = (B \<union> A) \<inter> (C \<union> A)"
  1224   by blast
  1225 
  1226 lemma Un_Int_crazy:
  1227     "(A \<inter> B) \<union> (B \<inter> C) \<union> (C \<inter> A) = (A \<union> B) \<inter> (B \<union> C) \<inter> (C \<union> A)"
  1228   by blast
  1229 
  1230 lemma subset_Un_eq: "(A \<subseteq> B) = (A \<union> B = B)"
  1231   by blast
  1232 
  1233 lemma Un_empty [iff]: "(A \<union> B = {}) = (A = {} & B = {})"
  1234   by blast
  1235 
  1236 lemma Un_subset_iff: "(A \<union> B \<subseteq> C) = (A \<subseteq> C & B \<subseteq> C)"
  1237   by blast
  1238 
  1239 lemma Un_Diff_Int: "(A - B) \<union> (A \<inter> B) = A"
  1240   by blast
  1241 
  1242 
  1243 text {* \medskip Set complement *}
  1244 
  1245 lemma Compl_disjoint [simp]: "A \<inter> -A = {}"
  1246   by blast
  1247 
  1248 lemma Compl_disjoint2 [simp]: "-A \<inter> A = {}"
  1249   by blast
  1250 
  1251 lemma Compl_partition: "A \<union> (-A) = UNIV"
  1252   by blast
  1253 
  1254 lemma double_complement [simp]: "- (-A) = (A::'a set)"
  1255   by blast
  1256 
  1257 lemma Compl_Un [simp]: "-(A \<union> B) = (-A) \<inter> (-B)"
  1258   by blast
  1259 
  1260 lemma Compl_Int [simp]: "-(A \<inter> B) = (-A) \<union> (-B)"
  1261   by blast
  1262 
  1263 lemma Compl_UN [simp]: "-(\<Union>x\<in>A. B x) = (\<Inter>x\<in>A. -B x)"
  1264   by blast
  1265 
  1266 lemma Compl_INT [simp]: "-(\<Inter>x\<in>A. B x) = (\<Union>x\<in>A. -B x)"
  1267   by blast
  1268 
  1269 lemma subset_Compl_self_eq: "(A \<subseteq> -A) = (A = {})"
  1270   by blast
  1271 
  1272 lemma Un_Int_assoc_eq: "((A \<inter> B) \<union> C = A \<inter> (B \<union> C)) = (C \<subseteq> A)"
  1273   -- {* Halmos, Naive Set Theory, page 16. *}
  1274   by blast
  1275 
  1276 lemma Compl_UNIV_eq [simp]: "-UNIV = {}"
  1277   by blast
  1278 
  1279 lemma Compl_empty_eq [simp]: "-{} = UNIV"
  1280   by blast
  1281 
  1282 lemma Compl_subset_Compl_iff [iff]: "(-A \<subseteq> -B) = (B \<subseteq> A)"
  1283   by blast
  1284 
  1285 lemma Compl_eq_Compl_iff [iff]: "(-A = -B) = (A = (B::'a set))"
  1286   by blast
  1287 
  1288 
  1289 text {* \medskip @{text Union}. *}
  1290 
  1291 lemma Union_empty [simp]: "Union({}) = {}"
  1292   by blast
  1293 
  1294 lemma Union_UNIV [simp]: "Union UNIV = UNIV"
  1295   by blast
  1296 
  1297 lemma Union_insert [simp]: "Union (insert a B) = a \<union> \<Union>B"
  1298   by blast
  1299 
  1300 lemma Union_Un_distrib [simp]: "\<Union>(A Un B) = \<Union>A \<union> \<Union>B"
  1301   by blast
  1302 
  1303 lemma Union_Int_subset: "\<Union>(A \<inter> B) \<subseteq> \<Union>A \<inter> \<Union>B"
  1304   by blast
  1305 
  1306 lemma Union_empty_conv [iff]: "(\<Union>A = {}) = (\<forall>x\<in>A. x = {})"
  1307   by auto
  1308 
  1309 lemma Union_disjoint: "(\<Union>C \<inter> A = {}) = (\<forall>B\<in>C. B \<inter> A = {})"
  1310   by blast
  1311 
  1312 
  1313 text {* \medskip @{text Inter}. *}
  1314 
  1315 lemma Inter_empty [simp]: "\<Inter>{} = UNIV"
  1316   by blast
  1317 
  1318 lemma Inter_UNIV [simp]: "\<Inter>UNIV = {}"
  1319   by blast
  1320 
  1321 lemma Inter_insert [simp]: "\<Inter>(insert a B) = a \<inter> \<Inter>B"
  1322   by blast
  1323 
  1324 lemma Inter_Un_subset: "\<Inter>A \<union> \<Inter>B \<subseteq> \<Inter>(A \<inter> B)"
  1325   by blast
  1326 
  1327 lemma Inter_Un_distrib: "\<Inter>(A \<union> B) = \<Inter>A \<inter> \<Inter>B"
  1328   by blast
  1329 
  1330 
  1331 text {*
  1332   \medskip @{text UN} and @{text INT}.
  1333 
  1334   Basic identities: *}
  1335 
  1336 lemma UN_empty [simp]: "(\<Union>x\<in>{}. B x) = {}"
  1337   by blast
  1338 
  1339 lemma UN_empty2 [simp]: "(\<Union>x\<in>A. {}) = {}"
  1340   by blast
  1341 
  1342 lemma UN_singleton [simp]: "(\<Union>x\<in>A. {x}) = A"
  1343   by blast
  1344 
  1345 lemma UN_absorb: "k \<in> I ==> A k \<union> (\<Union>i\<in>I. A i) = (\<Union>i\<in>I. A i)"
  1346   by blast
  1347 
  1348 lemma INT_empty [simp]: "(\<Inter>x\<in>{}. B x) = UNIV"
  1349   by blast
  1350 
  1351 lemma INT_absorb: "k \<in> I ==> A k \<inter> (\<Inter>i\<in>I. A i) = (\<Inter>i\<in>I. A i)"
  1352   by blast
  1353 
  1354 lemma UN_insert [simp]: "(\<Union>x\<in>insert a A. B x) = B a \<union> UNION A B"
  1355   by blast
  1356 
  1357 lemma UN_Un: "(\<Union>i \<in> A \<union> B. M i) = (\<Union>i\<in>A. M i) \<union> (\<Union>i\<in>B. M i)"
  1358   by blast
  1359 
  1360 lemma UN_UN_flatten: "(\<Union>x \<in> (\<Union>y\<in>A. B y). C x) = (\<Union>y\<in>A. \<Union>x\<in>B y. C x)"
  1361   by blast
  1362 
  1363 lemma UN_subset_iff: "((\<Union>i\<in>I. A i) \<subseteq> B) = (\<forall>i\<in>I. A i \<subseteq> B)"
  1364   by blast
  1365 
  1366 lemma INT_subset_iff: "(B \<subseteq> (\<Inter>i\<in>I. A i)) = (\<forall>i\<in>I. B \<subseteq> A i)"
  1367   by blast
  1368 
  1369 lemma INT_insert [simp]: "(\<Inter>x \<in> insert a A. B x) = B a \<inter> INTER A B"
  1370   by blast
  1371 
  1372 lemma INT_Un: "(\<Inter>i \<in> A \<union> B. M i) = (\<Inter>i \<in> A. M i) \<inter> (\<Inter>i\<in>B. M i)"
  1373   by blast
  1374 
  1375 lemma INT_insert_distrib:
  1376     "u \<in> A ==> (\<Inter>x\<in>A. insert a (B x)) = insert a (\<Inter>x\<in>A. B x)"
  1377   by blast
  1378 
  1379 lemma Union_image_eq [simp]: "\<Union>(B`A) = (\<Union>x\<in>A. B x)"
  1380   by blast
  1381 
  1382 lemma image_Union: "f ` \<Union>S = (\<Union>x\<in>S. f ` x)"
  1383   by blast
  1384 
  1385 lemma Inter_image_eq [simp]: "\<Inter>(B`A) = (\<Inter>x\<in>A. B x)"
  1386   by blast
  1387 
  1388 lemma UN_constant [simp]: "(\<Union>y\<in>A. c) = (if A = {} then {} else c)"
  1389   by auto
  1390 
  1391 lemma INT_constant [simp]: "(\<Inter>y\<in>A. c) = (if A = {} then UNIV else c)"
  1392   by auto
  1393 
  1394 lemma UN_eq: "(\<Union>x\<in>A. B x) = \<Union>({Y. \<exists>x\<in>A. Y = B x})"
  1395   by blast
  1396 
  1397 lemma INT_eq: "(\<Inter>x\<in>A. B x) = \<Inter>({Y. \<exists>x\<in>A. Y = B x})"
  1398   -- {* Look: it has an \emph{existential} quantifier *}
  1399   by blast
  1400 
  1401 lemma UN_empty3 [iff]: "(UNION A B = {}) = (\<forall>x\<in>A. B x = {})"
  1402   by auto
  1403 
  1404 
  1405 text {* \medskip Distributive laws: *}
  1406 
  1407 lemma Int_Union: "A \<inter> \<Union>B = (\<Union>C\<in>B. A \<inter> C)"
  1408   by blast
  1409 
  1410 lemma Int_Union2: "\<Union>B \<inter> A = (\<Union>C\<in>B. C \<inter> A)"
  1411   by blast
  1412 
  1413 lemma Un_Union_image: "(\<Union>x\<in>C. A x \<union> B x) = \<Union>(A`C) \<union> \<Union>(B`C)"
  1414   -- {* Devlin, Fundamentals of Contemporary Set Theory, page 12, exercise 5: *}
  1415   -- {* Union of a family of unions *}
  1416   by blast
  1417 
  1418 lemma UN_Un_distrib: "(\<Union>i\<in>I. A i \<union> B i) = (\<Union>i\<in>I. A i) \<union> (\<Union>i\<in>I. B i)"
  1419   -- {* Equivalent version *}
  1420   by blast
  1421 
  1422 lemma Un_Inter: "A \<union> \<Inter>B = (\<Inter>C\<in>B. A \<union> C)"
  1423   by blast
  1424 
  1425 lemma Int_Inter_image: "(\<Inter>x\<in>C. A x \<inter> B x) = \<Inter>(A`C) \<inter> \<Inter>(B`C)"
  1426   by blast
  1427 
  1428 lemma INT_Int_distrib: "(\<Inter>i\<in>I. A i \<inter> B i) = (\<Inter>i\<in>I. A i) \<inter> (\<Inter>i\<in>I. B i)"
  1429   -- {* Equivalent version *}
  1430   by blast
  1431 
  1432 lemma Int_UN_distrib: "B \<inter> (\<Union>i\<in>I. A i) = (\<Union>i\<in>I. B \<inter> A i)"
  1433   -- {* Halmos, Naive Set Theory, page 35. *}
  1434   by blast
  1435 
  1436 lemma Un_INT_distrib: "B \<union> (\<Inter>i\<in>I. A i) = (\<Inter>i\<in>I. B \<union> A i)"
  1437   by blast
  1438 
  1439 lemma Int_UN_distrib2: "(\<Union>i\<in>I. A i) \<inter> (\<Union>j\<in>J. B j) = (\<Union>i\<in>I. \<Union>j\<in>J. A i \<inter> B j)"
  1440   by blast
  1441 
  1442 lemma Un_INT_distrib2: "(\<Inter>i\<in>I. A i) \<union> (\<Inter>j\<in>J. B j) = (\<Inter>i\<in>I. \<Inter>j\<in>J. A i \<union> B j)"
  1443   by blast
  1444 
  1445 
  1446 text {* \medskip Bounded quantifiers.
  1447 
  1448   The following are not added to the default simpset because
  1449   (a) they duplicate the body and (b) there are no similar rules for @{text Int}. *}
  1450 
  1451 lemma ball_Un: "(\<forall>x \<in> A \<union> B. P x) = ((\<forall>x\<in>A. P x) & (\<forall>x\<in>B. P x))"
  1452   by blast
  1453 
  1454 lemma bex_Un: "(\<exists>x \<in> A \<union> B. P x) = ((\<exists>x\<in>A. P x) | (\<exists>x\<in>B. P x))"
  1455   by blast
  1456 
  1457 lemma ball_UN: "(\<forall>z \<in> UNION A B. P z) = (\<forall>x\<in>A. \<forall>z \<in> B x. P z)"
  1458   by blast
  1459 
  1460 lemma bex_UN: "(\<exists>z \<in> UNION A B. P z) = (\<exists>x\<in>A. \<exists>z\<in>B x. P z)"
  1461   by blast
  1462 
  1463 
  1464 text {* \medskip Set difference. *}
  1465 
  1466 lemma Diff_eq: "A - B = A \<inter> (-B)"
  1467   by blast
  1468 
  1469 lemma Diff_eq_empty_iff [simp]: "(A - B = {}) = (A \<subseteq> B)"
  1470   by blast
  1471 
  1472 lemma Diff_cancel [simp]: "A - A = {}"
  1473   by blast
  1474 
  1475 lemma Diff_triv: "A \<inter> B = {} ==> A - B = A"
  1476   by (blast elim: equalityE)
  1477 
  1478 lemma empty_Diff [simp]: "{} - A = {}"
  1479   by blast
  1480 
  1481 lemma Diff_empty [simp]: "A - {} = A"
  1482   by blast
  1483 
  1484 lemma Diff_UNIV [simp]: "A - UNIV = {}"
  1485   by blast
  1486 
  1487 lemma Diff_insert0 [simp]: "x \<notin> A ==> A - insert x B = A - B"
  1488   by blast
  1489 
  1490 lemma Diff_insert: "A - insert a B = A - B - {a}"
  1491   -- {* NOT SUITABLE FOR REWRITING since @{text "{a} == insert a 0"} *}
  1492   by blast
  1493 
  1494 lemma Diff_insert2: "A - insert a B = A - {a} - B"
  1495   -- {* NOT SUITABLE FOR REWRITING since @{text "{a} == insert a 0"} *}
  1496   by blast
  1497 
  1498 lemma insert_Diff_if: "insert x A - B = (if x \<in> B then A - B else insert x (A - B))"
  1499   by auto
  1500 
  1501 lemma insert_Diff1 [simp]: "x \<in> B ==> insert x A - B = A - B"
  1502   by blast
  1503 
  1504 lemma insert_Diff: "a \<in> A ==> insert a (A - {a}) = A"
  1505   by blast
  1506 
  1507 lemma Diff_insert_absorb: "x \<notin> A ==> (insert x A) - {x} = A"
  1508   by auto
  1509 
  1510 lemma Diff_disjoint [simp]: "A \<inter> (B - A) = {}"
  1511   by blast
  1512 
  1513 lemma Diff_partition: "A \<subseteq> B ==> A \<union> (B - A) = B"
  1514   by blast
  1515 
  1516 lemma double_diff: "A \<subseteq> B ==> B \<subseteq> C ==> B - (C - A) = A"
  1517   by blast
  1518 
  1519 lemma Un_Diff_cancel [simp]: "A \<union> (B - A) = A \<union> B"
  1520   by blast
  1521 
  1522 lemma Un_Diff_cancel2 [simp]: "(B - A) \<union> A = B \<union> A"
  1523   by blast
  1524 
  1525 lemma Diff_Un: "A - (B \<union> C) = (A - B) \<inter> (A - C)"
  1526   by blast
  1527 
  1528 lemma Diff_Int: "A - (B \<inter> C) = (A - B) \<union> (A - C)"
  1529   by blast
  1530 
  1531 lemma Un_Diff: "(A \<union> B) - C = (A - C) \<union> (B - C)"
  1532   by blast
  1533 
  1534 lemma Int_Diff: "(A \<inter> B) - C = A \<inter> (B - C)"
  1535   by blast
  1536 
  1537 lemma Diff_Int_distrib: "C \<inter> (A - B) = (C \<inter> A) - (C \<inter> B)"
  1538   by blast
  1539 
  1540 lemma Diff_Int_distrib2: "(A - B) \<inter> C = (A \<inter> C) - (B \<inter> C)"
  1541   by blast
  1542 
  1543 lemma Diff_Compl [simp]: "A - (- B) = A \<inter> B"
  1544   by auto
  1545 
  1546 lemma Compl_Diff_eq [simp]: "- (A - B) = -A \<union> B"
  1547   by blast
  1548 
  1549 
  1550 text {* \medskip Quantification over type @{typ bool}. *}
  1551 
  1552 lemma all_bool_eq: "(\<forall>b::bool. P b) = (P True & P False)"
  1553   apply auto
  1554   apply (tactic {* case_tac "b" 1 *})
  1555    apply auto
  1556   done
  1557 
  1558 lemma bool_induct: "P True \<Longrightarrow> P False \<Longrightarrow> P x"
  1559   by (rule conjI [THEN all_bool_eq [THEN iffD2], THEN spec])
  1560 
  1561 lemma ex_bool_eq: "(\<exists>b::bool. P b) = (P True | P False)"
  1562   apply auto
  1563   apply (tactic {* case_tac "b" 1 *})
  1564    apply auto
  1565   done
  1566 
  1567 lemma Un_eq_UN: "A \<union> B = (\<Union>b. if b then A else B)"
  1568   by (auto simp add: split_if_mem2)
  1569 
  1570 lemma UN_bool_eq: "(\<Union>b::bool. A b) = (A True \<union> A False)"
  1571   apply auto
  1572   apply (tactic {* case_tac "b" 1 *})
  1573    apply auto
  1574   done
  1575 
  1576 lemma INT_bool_eq: "(\<Inter>b::bool. A b) = (A True \<inter> A False)"
  1577   apply auto
  1578   apply (tactic {* case_tac "b" 1 *})
  1579   apply auto
  1580   done
  1581 
  1582 
  1583 text {* \medskip @{text Pow} *}
  1584 
  1585 lemma Pow_empty [simp]: "Pow {} = {{}}"
  1586   by (auto simp add: Pow_def)
  1587 
  1588 lemma Pow_insert: "Pow (insert a A) = Pow A \<union> (insert a ` Pow A)"
  1589   by (blast intro: image_eqI [where ?x = "u - {a}", standard])
  1590 
  1591 lemma Pow_Compl: "Pow (- A) = {-B | B. A \<in> Pow B}"
  1592   by (blast intro: exI [where ?x = "- u", standard])
  1593 
  1594 lemma Pow_UNIV [simp]: "Pow UNIV = UNIV"
  1595   by blast
  1596 
  1597 lemma Un_Pow_subset: "Pow A \<union> Pow B \<subseteq> Pow (A \<union> B)"
  1598   by blast
  1599 
  1600 lemma UN_Pow_subset: "(\<Union>x\<in>A. Pow (B x)) \<subseteq> Pow (\<Union>x\<in>A. B x)"
  1601   by blast
  1602 
  1603 lemma subset_Pow_Union: "A \<subseteq> Pow (\<Union>A)"
  1604   by blast
  1605 
  1606 lemma Union_Pow_eq [simp]: "\<Union>(Pow A) = A"
  1607   by blast
  1608 
  1609 lemma Pow_Int_eq [simp]: "Pow (A \<inter> B) = Pow A \<inter> Pow B"
  1610   by blast
  1611 
  1612 lemma Pow_INT_eq: "Pow (\<Inter>x\<in>A. B x) = (\<Inter>x\<in>A. Pow (B x))"
  1613   by blast
  1614 
  1615 
  1616 text {* \medskip Miscellany. *}
  1617 
  1618 lemma set_eq_subset: "(A = B) = (A \<subseteq> B & B \<subseteq> A)"
  1619   by blast
  1620 
  1621 lemma subset_iff: "(A \<subseteq> B) = (\<forall>t. t \<in> A --> t \<in> B)"
  1622   by blast
  1623 
  1624 lemma subset_iff_psubset_eq: "(A \<subseteq> B) = ((A \<subset> B) | (A = B))"
  1625   by (unfold psubset_def) blast
  1626 
  1627 lemma all_not_in_conv [iff]: "(\<forall>x. x \<notin> A) = (A = {})"
  1628   by blast
  1629 
  1630 lemma distinct_lemma: "f x \<noteq> f y ==> x \<noteq> y"
  1631   by rules
  1632 
  1633 
  1634 text {* \medskip Miniscoping: pushing in big Unions and Intersections. *}
  1635 
  1636 lemma UN_simps [simp]:
  1637   "!!a B C. (UN x:C. insert a (B x)) = (if C={} then {} else insert a (UN x:C. B x))"
  1638   "!!A B C. (UN x:C. A x Un B)   = ((if C={} then {} else (UN x:C. A x) Un B))"
  1639   "!!A B C. (UN x:C. A Un B x)   = ((if C={} then {} else A Un (UN x:C. B x)))"
  1640   "!!A B C. (UN x:C. A x Int B)  = ((UN x:C. A x) Int B)"
  1641   "!!A B C. (UN x:C. A Int B x)  = (A Int (UN x:C. B x))"
  1642   "!!A B C. (UN x:C. A x - B)    = ((UN x:C. A x) - B)"
  1643   "!!A B C. (UN x:C. A - B x)    = (A - (INT x:C. B x))"
  1644   "!!A B. (UN x: Union A. B x) = (UN y:A. UN x:y. B x)"
  1645   "!!A B C. (UN z: UNION A B. C z) = (UN  x:A. UN z: B(x). C z)"
  1646   "!!A B f. (UN x:f`A. B x)     = (UN a:A. B (f a))"
  1647   by auto
  1648 
  1649 lemma INT_simps [simp]:
  1650   "!!A B C. (INT x:C. A x Int B) = (if C={} then UNIV else (INT x:C. A x) Int B)"
  1651   "!!A B C. (INT x:C. A Int B x) = (if C={} then UNIV else A Int (INT x:C. B x))"
  1652   "!!A B C. (INT x:C. A x - B)   = (if C={} then UNIV else (INT x:C. A x) - B)"
  1653   "!!A B C. (INT x:C. A - B x)   = (if C={} then UNIV else A - (UN x:C. B x))"
  1654   "!!a B C. (INT x:C. insert a (B x)) = insert a (INT x:C. B x)"
  1655   "!!A B C. (INT x:C. A x Un B)  = ((INT x:C. A x) Un B)"
  1656   "!!A B C. (INT x:C. A Un B x)  = (A Un (INT x:C. B x))"
  1657   "!!A B. (INT x: Union A. B x) = (INT y:A. INT x:y. B x)"
  1658   "!!A B C. (INT z: UNION A B. C z) = (INT x:A. INT z: B(x). C z)"
  1659   "!!A B f. (INT x:f`A. B x)    = (INT a:A. B (f a))"
  1660   by auto
  1661 
  1662 lemma ball_simps [simp]:
  1663   "!!A P Q. (ALL x:A. P x | Q) = ((ALL x:A. P x) | Q)"
  1664   "!!A P Q. (ALL x:A. P | Q x) = (P | (ALL x:A. Q x))"
  1665   "!!A P Q. (ALL x:A. P --> Q x) = (P --> (ALL x:A. Q x))"
  1666   "!!A P Q. (ALL x:A. P x --> Q) = ((EX x:A. P x) --> Q)"
  1667   "!!P. (ALL x:{}. P x) = True"
  1668   "!!P. (ALL x:UNIV. P x) = (ALL x. P x)"
  1669   "!!a B P. (ALL x:insert a B. P x) = (P a & (ALL x:B. P x))"
  1670   "!!A P. (ALL x:Union A. P x) = (ALL y:A. ALL x:y. P x)"
  1671   "!!A B P. (ALL x: UNION A B. P x) = (ALL a:A. ALL x: B a. P x)"
  1672   "!!P Q. (ALL x:Collect Q. P x) = (ALL x. Q x --> P x)"
  1673   "!!A P f. (ALL x:f`A. P x) = (ALL x:A. P (f x))"
  1674   "!!A P. (~(ALL x:A. P x)) = (EX x:A. ~P x)"
  1675   by auto
  1676 
  1677 lemma bex_simps [simp]:
  1678   "!!A P Q. (EX x:A. P x & Q) = ((EX x:A. P x) & Q)"
  1679   "!!A P Q. (EX x:A. P & Q x) = (P & (EX x:A. Q x))"
  1680   "!!P. (EX x:{}. P x) = False"
  1681   "!!P. (EX x:UNIV. P x) = (EX x. P x)"
  1682   "!!a B P. (EX x:insert a B. P x) = (P(a) | (EX x:B. P x))"
  1683   "!!A P. (EX x:Union A. P x) = (EX y:A. EX x:y. P x)"
  1684   "!!A B P. (EX x: UNION A B. P x) = (EX a:A. EX x:B a. P x)"
  1685   "!!P Q. (EX x:Collect Q. P x) = (EX x. Q x & P x)"
  1686   "!!A P f. (EX x:f`A. P x) = (EX x:A. P (f x))"
  1687   "!!A P. (~(EX x:A. P x)) = (ALL x:A. ~P x)"
  1688   by auto
  1689 
  1690 lemma ball_conj_distrib:
  1691   "(ALL x:A. P x & Q x) = ((ALL x:A. P x) & (ALL x:A. Q x))"
  1692   by blast
  1693 
  1694 lemma bex_disj_distrib:
  1695   "(EX x:A. P x | Q x) = ((EX x:A. P x) | (EX x:A. Q x))"
  1696   by blast
  1697 
  1698 
  1699 subsubsection {* Monotonicity of various operations *}
  1700 
  1701 lemma image_mono: "A \<subseteq> B ==> f`A \<subseteq> f`B"
  1702   by blast
  1703 
  1704 lemma Pow_mono: "A \<subseteq> B ==> Pow A \<subseteq> Pow B"
  1705   by blast
  1706 
  1707 lemma Union_mono: "A \<subseteq> B ==> \<Union>A \<subseteq> \<Union>B"
  1708   by blast
  1709 
  1710 lemma Inter_anti_mono: "B \<subseteq> A ==> \<Inter>A \<subseteq> \<Inter>B"
  1711   by blast
  1712 
  1713 lemma UN_mono:
  1714   "A \<subseteq> B ==> (!!x. x \<in> A ==> f x \<subseteq> g x) ==>
  1715     (\<Union>x\<in>A. f x) \<subseteq> (\<Union>x\<in>B. g x)"
  1716   by (blast dest: subsetD)
  1717 
  1718 lemma INT_anti_mono:
  1719   "B \<subseteq> A ==> (!!x. x \<in> A ==> f x \<subseteq> g x) ==>
  1720     (\<Inter>x\<in>A. f x) \<subseteq> (\<Inter>x\<in>A. g x)"
  1721   -- {* The last inclusion is POSITIVE! *}
  1722   by (blast dest: subsetD)
  1723 
  1724 lemma insert_mono: "C \<subseteq> D ==> insert a C \<subseteq> insert a D"
  1725   by blast
  1726 
  1727 lemma Un_mono: "A \<subseteq> C ==> B \<subseteq> D ==> A \<union> B \<subseteq> C \<union> D"
  1728   by blast
  1729 
  1730 lemma Int_mono: "A \<subseteq> C ==> B \<subseteq> D ==> A \<inter> B \<subseteq> C \<inter> D"
  1731   by blast
  1732 
  1733 lemma Diff_mono: "A \<subseteq> C ==> D \<subseteq> B ==> A - B \<subseteq> C - D"
  1734   by blast
  1735 
  1736 lemma Compl_anti_mono: "A \<subseteq> B ==> -B \<subseteq> -A"
  1737   by blast
  1738 
  1739 text {* \medskip Monotonicity of implications. *}
  1740 
  1741 lemma in_mono: "A \<subseteq> B ==> x \<in> A --> x \<in> B"
  1742   apply (rule impI)
  1743   apply (erule subsetD)
  1744   apply assumption
  1745   done
  1746 
  1747 lemma conj_mono: "P1 --> Q1 ==> P2 --> Q2 ==> (P1 & P2) --> (Q1 & Q2)"
  1748   by rules
  1749 
  1750 lemma disj_mono: "P1 --> Q1 ==> P2 --> Q2 ==> (P1 | P2) --> (Q1 | Q2)"
  1751   by rules
  1752 
  1753 lemma imp_mono: "Q1 --> P1 ==> P2 --> Q2 ==> (P1 --> P2) --> (Q1 --> Q2)"
  1754   by rules
  1755 
  1756 lemma imp_refl: "P --> P" ..
  1757 
  1758 lemma ex_mono: "(!!x. P x --> Q x) ==> (EX x. P x) --> (EX x. Q x)"
  1759   by rules
  1760 
  1761 lemma all_mono: "(!!x. P x --> Q x) ==> (ALL x. P x) --> (ALL x. Q x)"
  1762   by rules
  1763 
  1764 lemma Collect_mono: "(!!x. P x --> Q x) ==> Collect P \<subseteq> Collect Q"
  1765   by blast
  1766 
  1767 lemma Int_Collect_mono:
  1768     "A \<subseteq> B ==> (!!x. x \<in> A ==> P x --> Q x) ==> A \<inter> Collect P \<subseteq> B \<inter> Collect Q"
  1769   by blast
  1770 
  1771 lemmas basic_monos =
  1772   subset_refl imp_refl disj_mono conj_mono
  1773   ex_mono Collect_mono in_mono
  1774 
  1775 lemma eq_to_mono: "a = b ==> c = d ==> b --> d ==> a --> c"
  1776   by rules
  1777 
  1778 lemma eq_to_mono2: "a = b ==> c = d ==> ~ b --> ~ d ==> ~ a --> ~ c"
  1779   by rules
  1780 
  1781 lemma Least_mono:
  1782   "mono (f::'a::order => 'b::order) ==> EX x:S. ALL y:S. x <= y
  1783     ==> (LEAST y. y : f ` S) = f (LEAST x. x : S)"
  1784     -- {* Courtesy of Stephan Merz *}
  1785   apply clarify
  1786   apply (erule_tac P = "%x. x : S" in LeastI2)
  1787    apply fast
  1788   apply (rule LeastI2)
  1789   apply (auto elim: monoD intro!: order_antisym)
  1790   done
  1791 
  1792 
  1793 subsection {* Inverse image of a function *}
  1794 
  1795 constdefs
  1796   vimage :: "('a => 'b) => 'b set => 'a set"    (infixr "-`" 90)
  1797   "f -` B == {x. f x : B}"
  1798 
  1799 
  1800 subsubsection {* Basic rules *}
  1801 
  1802 lemma vimage_eq [simp]: "(a : f -` B) = (f a : B)"
  1803   by (unfold vimage_def) blast
  1804 
  1805 lemma vimage_singleton_eq: "(a : f -` {b}) = (f a = b)"
  1806   by simp
  1807 
  1808 lemma vimageI [intro]: "f a = b ==> b:B ==> a : f -` B"
  1809   by (unfold vimage_def) blast
  1810 
  1811 lemma vimageI2: "f a : A ==> a : f -` A"
  1812   by (unfold vimage_def) fast
  1813 
  1814 lemma vimageE [elim!]: "a: f -` B ==> (!!x. f a = x ==> x:B ==> P) ==> P"
  1815   by (unfold vimage_def) blast
  1816 
  1817 lemma vimageD: "a : f -` A ==> f a : A"
  1818   by (unfold vimage_def) fast
  1819 
  1820 
  1821 subsubsection {* Equations *}
  1822 
  1823 lemma vimage_empty [simp]: "f -` {} = {}"
  1824   by blast
  1825 
  1826 lemma vimage_Compl: "f -` (-A) = -(f -` A)"
  1827   by blast
  1828 
  1829 lemma vimage_Un [simp]: "f -` (A Un B) = (f -` A) Un (f -` B)"
  1830   by blast
  1831 
  1832 lemma vimage_Int [simp]: "f -` (A Int B) = (f -` A) Int (f -` B)"
  1833   by fast
  1834 
  1835 lemma vimage_Union: "f -` (Union A) = (UN X:A. f -` X)"
  1836   by blast
  1837 
  1838 lemma vimage_UN: "f-`(UN x:A. B x) = (UN x:A. f -` B x)"
  1839   by blast
  1840 
  1841 lemma vimage_INT: "f-`(INT x:A. B x) = (INT x:A. f -` B x)"
  1842   by blast
  1843 
  1844 lemma vimage_Collect_eq [simp]: "f -` Collect P = {y. P (f y)}"
  1845   by blast
  1846 
  1847 lemma vimage_Collect: "(!!x. P (f x) = Q x) ==> f -` (Collect P) = Collect Q"
  1848   by blast
  1849 
  1850 lemma vimage_insert: "f-`(insert a B) = (f-`{a}) Un (f-`B)"
  1851   -- {* NOT suitable for rewriting because of the recurrence of @{term "{a}"}. *}
  1852   by blast
  1853 
  1854 lemma vimage_Diff: "f -` (A - B) = (f -` A) - (f -` B)"
  1855   by blast
  1856 
  1857 lemma vimage_UNIV [simp]: "f -` UNIV = UNIV"
  1858   by blast
  1859 
  1860 lemma vimage_eq_UN: "f-`B = (UN y: B. f-`{y})"
  1861   -- {* NOT suitable for rewriting *}
  1862   by blast
  1863 
  1864 lemma vimage_mono: "A \<subseteq> B ==> f -` A \<subseteq> f -` B"
  1865   -- {* monotonicity *}
  1866   by blast
  1867 
  1868 
  1869 subsection {* Transitivity rules for calculational reasoning *}
  1870 
  1871 lemma forw_subst: "a = b ==> P b ==> P a"
  1872   by (rule ssubst)
  1873 
  1874 lemma back_subst: "P a ==> a = b ==> P b"
  1875   by (rule subst)
  1876 
  1877 lemma set_rev_mp: "x:A ==> A \<subseteq> B ==> x:B"
  1878   by (rule subsetD)
  1879 
  1880 lemma set_mp: "A \<subseteq> B ==> x:A ==> x:B"
  1881   by (rule subsetD)
  1882 
  1883 lemma order_neq_le_trans: "a ~= b ==> (a::'a::order) <= b ==> a < b"
  1884   by (simp add: order_less_le)
  1885 
  1886 lemma order_le_neq_trans: "(a::'a::order) <= b ==> a ~= b ==> a < b"
  1887   by (simp add: order_less_le)
  1888 
  1889 lemma order_less_asym': "(a::'a::order) < b ==> b < a ==> P"
  1890   by (rule order_less_asym)
  1891 
  1892 lemma ord_le_eq_trans: "a <= b ==> b = c ==> a <= c"
  1893   by (rule subst)
  1894 
  1895 lemma ord_eq_le_trans: "a = b ==> b <= c ==> a <= c"
  1896   by (rule ssubst)
  1897 
  1898 lemma ord_less_eq_trans: "a < b ==> b = c ==> a < c"
  1899   by (rule subst)
  1900 
  1901 lemma ord_eq_less_trans: "a = b ==> b < c ==> a < c"
  1902   by (rule ssubst)
  1903 
  1904 lemma order_less_subst2: "(a::'a::order) < b ==> f b < (c::'c::order) ==>
  1905   (!!x y. x < y ==> f x < f y) ==> f a < c"
  1906 proof -
  1907   assume r: "!!x y. x < y ==> f x < f y"
  1908   assume "a < b" hence "f a < f b" by (rule r)
  1909   also assume "f b < c"
  1910   finally (order_less_trans) show ?thesis .
  1911 qed
  1912 
  1913 lemma order_less_subst1: "(a::'a::order) < f b ==> (b::'b::order) < c ==>
  1914   (!!x y. x < y ==> f x < f y) ==> a < f c"
  1915 proof -
  1916   assume r: "!!x y. x < y ==> f x < f y"
  1917   assume "a < f b"
  1918   also assume "b < c" hence "f b < f c" by (rule r)
  1919   finally (order_less_trans) show ?thesis .
  1920 qed
  1921 
  1922 lemma order_le_less_subst2: "(a::'a::order) <= b ==> f b < (c::'c::order) ==>
  1923   (!!x y. x <= y ==> f x <= f y) ==> f a < c"
  1924 proof -
  1925   assume r: "!!x y. x <= y ==> f x <= f y"
  1926   assume "a <= b" hence "f a <= f b" by (rule r)
  1927   also assume "f b < c"
  1928   finally (order_le_less_trans) show ?thesis .
  1929 qed
  1930 
  1931 lemma order_le_less_subst1: "(a::'a::order) <= f b ==> (b::'b::order) < c ==>
  1932   (!!x y. x < y ==> f x < f y) ==> a < f c"
  1933 proof -
  1934   assume r: "!!x y. x < y ==> f x < f y"
  1935   assume "a <= f b"
  1936   also assume "b < c" hence "f b < f c" by (rule r)
  1937   finally (order_le_less_trans) show ?thesis .
  1938 qed
  1939 
  1940 lemma order_less_le_subst2: "(a::'a::order) < b ==> f b <= (c::'c::order) ==>
  1941   (!!x y. x < y ==> f x < f y) ==> f a < c"
  1942 proof -
  1943   assume r: "!!x y. x < y ==> f x < f y"
  1944   assume "a < b" hence "f a < f b" by (rule r)
  1945   also assume "f b <= c"
  1946   finally (order_less_le_trans) show ?thesis .
  1947 qed
  1948 
  1949 lemma order_less_le_subst1: "(a::'a::order) < f b ==> (b::'b::order) <= c ==>
  1950   (!!x y. x <= y ==> f x <= f y) ==> a < f c"
  1951 proof -
  1952   assume r: "!!x y. x <= y ==> f x <= f y"
  1953   assume "a < f b"
  1954   also assume "b <= c" hence "f b <= f c" by (rule r)
  1955   finally (order_less_le_trans) show ?thesis .
  1956 qed
  1957 
  1958 lemma order_subst1: "(a::'a::order) <= f b ==> (b::'b::order) <= c ==>
  1959   (!!x y. x <= y ==> f x <= f y) ==> a <= f c"
  1960 proof -
  1961   assume r: "!!x y. x <= y ==> f x <= f y"
  1962   assume "a <= f b"
  1963   also assume "b <= c" hence "f b <= f c" by (rule r)
  1964   finally (order_trans) show ?thesis .
  1965 qed
  1966 
  1967 lemma order_subst2: "(a::'a::order) <= b ==> f b <= (c::'c::order) ==>
  1968   (!!x y. x <= y ==> f x <= f y) ==> f a <= c"
  1969 proof -
  1970   assume r: "!!x y. x <= y ==> f x <= f y"
  1971   assume "a <= b" hence "f a <= f b" by (rule r)
  1972   also assume "f b <= c"
  1973   finally (order_trans) show ?thesis .
  1974 qed
  1975 
  1976 lemma ord_le_eq_subst: "a <= b ==> f b = c ==>
  1977   (!!x y. x <= y ==> f x <= f y) ==> f a <= c"
  1978 proof -
  1979   assume r: "!!x y. x <= y ==> f x <= f y"
  1980   assume "a <= b" hence "f a <= f b" by (rule r)
  1981   also assume "f b = c"
  1982   finally (ord_le_eq_trans) show ?thesis .
  1983 qed
  1984 
  1985 lemma ord_eq_le_subst: "a = f b ==> b <= c ==>
  1986   (!!x y. x <= y ==> f x <= f y) ==> a <= f c"
  1987 proof -
  1988   assume r: "!!x y. x <= y ==> f x <= f y"
  1989   assume "a = f b"
  1990   also assume "b <= c" hence "f b <= f c" by (rule r)
  1991   finally (ord_eq_le_trans) show ?thesis .
  1992 qed
  1993 
  1994 lemma ord_less_eq_subst: "a < b ==> f b = c ==>
  1995   (!!x y. x < y ==> f x < f y) ==> f a < c"
  1996 proof -
  1997   assume r: "!!x y. x < y ==> f x < f y"
  1998   assume "a < b" hence "f a < f b" by (rule r)
  1999   also assume "f b = c"
  2000   finally (ord_less_eq_trans) show ?thesis .
  2001 qed
  2002 
  2003 lemma ord_eq_less_subst: "a = f b ==> b < c ==>
  2004   (!!x y. x < y ==> f x < f y) ==> a < f c"
  2005 proof -
  2006   assume r: "!!x y. x < y ==> f x < f y"
  2007   assume "a = f b"
  2008   also assume "b < c" hence "f b < f c" by (rule r)
  2009   finally (ord_eq_less_trans) show ?thesis .
  2010 qed
  2011 
  2012 text {*
  2013   Note that this list of rules is in reverse order of priorities.
  2014 *}
  2015 
  2016 lemmas basic_trans_rules [trans] =
  2017   order_less_subst2
  2018   order_less_subst1
  2019   order_le_less_subst2
  2020   order_le_less_subst1
  2021   order_less_le_subst2
  2022   order_less_le_subst1
  2023   order_subst2
  2024   order_subst1
  2025   ord_le_eq_subst
  2026   ord_eq_le_subst
  2027   ord_less_eq_subst
  2028   ord_eq_less_subst
  2029   forw_subst
  2030   back_subst
  2031   rev_mp
  2032   mp
  2033   set_rev_mp
  2034   set_mp
  2035   order_neq_le_trans
  2036   order_le_neq_trans
  2037   order_less_trans
  2038   order_less_asym'
  2039   order_le_less_trans
  2040   order_less_le_trans
  2041   order_trans
  2042   order_antisym
  2043   ord_le_eq_trans
  2044   ord_eq_le_trans
  2045   ord_less_eq_trans
  2046   ord_eq_less_trans
  2047   trans
  2048 
  2049 end