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