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