src/HOL/Set.thy
author haftmann
Wed Jan 02 15:14:02 2008 +0100 (2008-01-02)
changeset 25762 c03e9d04b3e4
parent 25510 38c15efe603b
child 25965 05df64f786a4
permissions -rw-r--r--
splitted class uminus from class minus
     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 ML_setup {*
   432   change_claset (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 
   649 subsubsection {* The empty set *}
   650 
   651 lemma empty_iff [simp]: "(c : {}) = False"
   652   by (simp add: empty_def)
   653 
   654 lemma emptyE [elim!]: "a : {} ==> P"
   655   by simp
   656 
   657 lemma empty_subsetI [iff]: "{} \<subseteq> A"
   658     -- {* One effect is to delete the ASSUMPTION @{prop "{} <= A"} *}
   659   by blast
   660 
   661 lemma equals0I: "(!!y. y \<in> A ==> False) ==> A = {}"
   662   by blast
   663 
   664 lemma equals0D: "A = {} ==> a \<notin> A"
   665     -- {* Use for reasoning about disjointness: @{prop "A Int B = {}"} *}
   666   by blast
   667 
   668 lemma ball_empty [simp]: "Ball {} P = True"
   669   by (simp add: Ball_def)
   670 
   671 lemma bex_empty [simp]: "Bex {} P = False"
   672   by (simp add: Bex_def)
   673 
   674 lemma UNIV_not_empty [iff]: "UNIV ~= {}"
   675   by (blast elim: equalityE)
   676 
   677 
   678 subsubsection {* The Powerset operator -- Pow *}
   679 
   680 lemma Pow_iff [iff]: "(A \<in> Pow B) = (A \<subseteq> B)"
   681   by (simp add: Pow_def)
   682 
   683 lemma PowI: "A \<subseteq> B ==> A \<in> Pow B"
   684   by (simp add: Pow_def)
   685 
   686 lemma PowD: "A \<in> Pow B ==> A \<subseteq> B"
   687   by (simp add: Pow_def)
   688 
   689 lemma Pow_bottom: "{} \<in> Pow B"
   690   by simp
   691 
   692 lemma Pow_top: "A \<in> Pow A"
   693   by (simp add: subset_refl)
   694 
   695 
   696 subsubsection {* Set complement *}
   697 
   698 lemma Compl_iff [simp]: "(c \<in> -A) = (c \<notin> A)"
   699   by (unfold Compl_def) blast
   700 
   701 lemma ComplI [intro!]: "(c \<in> A ==> False) ==> c \<in> -A"
   702   by (unfold Compl_def) blast
   703 
   704 text {*
   705   \medskip This form, with negated conclusion, works well with the
   706   Classical prover.  Negated assumptions behave like formulae on the
   707   right side of the notional turnstile ... *}
   708 
   709 lemma ComplD [dest!]: "c : -A ==> c~:A"
   710   by (unfold Compl_def) blast
   711 
   712 lemmas ComplE = ComplD [elim_format]
   713 
   714 
   715 subsubsection {* Binary union -- Un *}
   716 
   717 lemma Un_iff [simp]: "(c : A Un B) = (c:A | c:B)"
   718   by (unfold Un_def) blast
   719 
   720 lemma UnI1 [elim?]: "c:A ==> c : A Un B"
   721   by simp
   722 
   723 lemma UnI2 [elim?]: "c:B ==> c : A Un B"
   724   by simp
   725 
   726 text {*
   727   \medskip Classical introduction rule: no commitment to @{prop A} vs
   728   @{prop B}.
   729 *}
   730 
   731 lemma UnCI [intro!]: "(c~:B ==> c:A) ==> c : A Un B"
   732   by auto
   733 
   734 lemma UnE [elim!]: "c : A Un B ==> (c:A ==> P) ==> (c:B ==> P) ==> P"
   735   by (unfold Un_def) blast
   736 
   737 
   738 subsubsection {* Binary intersection -- Int *}
   739 
   740 lemma Int_iff [simp]: "(c : A Int B) = (c:A & c:B)"
   741   by (unfold Int_def) blast
   742 
   743 lemma IntI [intro!]: "c:A ==> c:B ==> c : A Int B"
   744   by simp
   745 
   746 lemma IntD1: "c : A Int B ==> c:A"
   747   by simp
   748 
   749 lemma IntD2: "c : A Int B ==> c:B"
   750   by simp
   751 
   752 lemma IntE [elim!]: "c : A Int B ==> (c:A ==> c:B ==> P) ==> P"
   753   by simp
   754 
   755 
   756 subsubsection {* Set difference *}
   757 
   758 lemma Diff_iff [simp]: "(c : A - B) = (c:A & c~:B)"
   759   by (unfold set_diff_def) blast
   760 
   761 lemma DiffI [intro!]: "c : A ==> c ~: B ==> c : A - B"
   762   by simp
   763 
   764 lemma DiffD1: "c : A - B ==> c : A"
   765   by simp
   766 
   767 lemma DiffD2: "c : A - B ==> c : B ==> P"
   768   by simp
   769 
   770 lemma DiffE [elim!]: "c : A - B ==> (c:A ==> c~:B ==> P) ==> P"
   771   by simp
   772 
   773 
   774 subsubsection {* Augmenting a set -- insert *}
   775 
   776 lemma insert_iff [simp]: "(a : insert b A) = (a = b | a:A)"
   777   by (unfold insert_def) blast
   778 
   779 lemma insertI1: "a : insert a B"
   780   by simp
   781 
   782 lemma insertI2: "a : B ==> a : insert b B"
   783   by simp
   784 
   785 lemma insertE [elim!]: "a : insert b A ==> (a = b ==> P) ==> (a:A ==> P) ==> P"
   786   by (unfold insert_def) blast
   787 
   788 lemma insertCI [intro!]: "(a~:B ==> a = b) ==> a: insert b B"
   789   -- {* Classical introduction rule. *}
   790   by auto
   791 
   792 lemma subset_insert_iff: "(A \<subseteq> insert x B) = (if x:A then A - {x} \<subseteq> B else A \<subseteq> B)"
   793   by auto
   794 
   795 lemma set_insert:
   796   assumes "x \<in> A"
   797   obtains B where "A = insert x B" and "x \<notin> B"
   798 proof
   799   from assms show "A = insert x (A - {x})" by blast
   800 next
   801   show "x \<notin> A - {x}" by blast
   802 qed
   803 
   804 lemma insert_ident: "x ~: A ==> x ~: B ==> (insert x A = insert x B) = (A = B)"
   805 by auto
   806 
   807 subsubsection {* Singletons, using insert *}
   808 
   809 lemma singletonI [intro!,noatp]: "a : {a}"
   810     -- {* Redundant? But unlike @{text insertCI}, it proves the subgoal immediately! *}
   811   by (rule insertI1)
   812 
   813 lemma singletonD [dest!,noatp]: "b : {a} ==> b = a"
   814   by blast
   815 
   816 lemmas singletonE = singletonD [elim_format]
   817 
   818 lemma singleton_iff: "(b : {a}) = (b = a)"
   819   by blast
   820 
   821 lemma singleton_inject [dest!]: "{a} = {b} ==> a = b"
   822   by blast
   823 
   824 lemma singleton_insert_inj_eq [iff,noatp]:
   825      "({b} = insert a A) = (a = b & A \<subseteq> {b})"
   826   by blast
   827 
   828 lemma singleton_insert_inj_eq' [iff,noatp]:
   829      "(insert a A = {b}) = (a = b & A \<subseteq> {b})"
   830   by blast
   831 
   832 lemma subset_singletonD: "A \<subseteq> {x} ==> A = {} | A = {x}"
   833   by fast
   834 
   835 lemma singleton_conv [simp]: "{x. x = a} = {a}"
   836   by blast
   837 
   838 lemma singleton_conv2 [simp]: "{x. a = x} = {a}"
   839   by blast
   840 
   841 lemma diff_single_insert: "A - {x} \<subseteq> B ==> x \<in> A ==> A \<subseteq> insert x B"
   842   by blast
   843 
   844 lemma doubleton_eq_iff: "({a,b} = {c,d}) = (a=c & b=d | a=d & b=c)"
   845   by (blast elim: equalityE)
   846 
   847 
   848 subsubsection {* Unions of families *}
   849 
   850 text {*
   851   @{term [source] "UN x:A. B x"} is @{term "Union (B`A)"}.
   852 *}
   853 
   854 declare UNION_def [noatp]
   855 
   856 lemma UN_iff [simp]: "(b: (UN x:A. B x)) = (EX x:A. b: B x)"
   857   by (unfold UNION_def) blast
   858 
   859 lemma UN_I [intro]: "a:A ==> b: B a ==> b: (UN x:A. B x)"
   860   -- {* The order of the premises presupposes that @{term A} is rigid;
   861     @{term b} may be flexible. *}
   862   by auto
   863 
   864 lemma UN_E [elim!]: "b : (UN x:A. B x) ==> (!!x. x:A ==> b: B x ==> R) ==> R"
   865   by (unfold UNION_def) blast
   866 
   867 lemma UN_cong [cong]:
   868     "A = B ==> (!!x. x:B ==> C x = D x) ==> (UN x:A. C x) = (UN x:B. D x)"
   869   by (simp add: UNION_def)
   870 
   871 
   872 subsubsection {* Intersections of families *}
   873 
   874 text {* @{term [source] "INT x:A. B x"} is @{term "Inter (B`A)"}. *}
   875 
   876 lemma INT_iff [simp]: "(b: (INT x:A. B x)) = (ALL x:A. b: B x)"
   877   by (unfold INTER_def) blast
   878 
   879 lemma INT_I [intro!]: "(!!x. x:A ==> b: B x) ==> b : (INT x:A. B x)"
   880   by (unfold INTER_def) blast
   881 
   882 lemma INT_D [elim]: "b : (INT x:A. B x) ==> a:A ==> b: B a"
   883   by auto
   884 
   885 lemma INT_E [elim]: "b : (INT x:A. B x) ==> (b: B a ==> R) ==> (a~:A ==> R) ==> R"
   886   -- {* "Classical" elimination -- by the Excluded Middle on @{prop "a:A"}. *}
   887   by (unfold INTER_def) blast
   888 
   889 lemma INT_cong [cong]:
   890     "A = B ==> (!!x. x:B ==> C x = D x) ==> (INT x:A. C x) = (INT x:B. D x)"
   891   by (simp add: INTER_def)
   892 
   893 
   894 subsubsection {* Union *}
   895 
   896 lemma Union_iff [simp,noatp]: "(A : Union C) = (EX X:C. A:X)"
   897   by (unfold Union_def) blast
   898 
   899 lemma UnionI [intro]: "X:C ==> A:X ==> A : Union C"
   900   -- {* The order of the premises presupposes that @{term C} is rigid;
   901     @{term A} may be flexible. *}
   902   by auto
   903 
   904 lemma UnionE [elim!]: "A : Union C ==> (!!X. A:X ==> X:C ==> R) ==> R"
   905   by (unfold Union_def) blast
   906 
   907 
   908 subsubsection {* Inter *}
   909 
   910 lemma Inter_iff [simp,noatp]: "(A : Inter C) = (ALL X:C. A:X)"
   911   by (unfold Inter_def) blast
   912 
   913 lemma InterI [intro!]: "(!!X. X:C ==> A:X) ==> A : Inter C"
   914   by (simp add: Inter_def)
   915 
   916 text {*
   917   \medskip A ``destruct'' rule -- every @{term X} in @{term C}
   918   contains @{term A} as an element, but @{prop "A:X"} can hold when
   919   @{prop "X:C"} does not!  This rule is analogous to @{text spec}.
   920 *}
   921 
   922 lemma InterD [elim]: "A : Inter C ==> X:C ==> A:X"
   923   by auto
   924 
   925 lemma InterE [elim]: "A : Inter C ==> (X~:C ==> R) ==> (A:X ==> R) ==> R"
   926   -- {* ``Classical'' elimination rule -- does not require proving
   927     @{prop "X:C"}. *}
   928   by (unfold Inter_def) blast
   929 
   930 text {*
   931   \medskip Image of a set under a function.  Frequently @{term b} does
   932   not have the syntactic form of @{term "f x"}.
   933 *}
   934 
   935 declare image_def [noatp]
   936 
   937 lemma image_eqI [simp, intro]: "b = f x ==> x:A ==> b : f`A"
   938   by (unfold image_def) blast
   939 
   940 lemma imageI: "x : A ==> f x : f ` A"
   941   by (rule image_eqI) (rule refl)
   942 
   943 lemma rev_image_eqI: "x:A ==> b = f x ==> b : f`A"
   944   -- {* This version's more effective when we already have the
   945     required @{term x}. *}
   946   by (unfold image_def) blast
   947 
   948 lemma imageE [elim!]:
   949   "b : (%x. f x)`A ==> (!!x. b = f x ==> x:A ==> P) ==> P"
   950   -- {* The eta-expansion gives variable-name preservation. *}
   951   by (unfold image_def) blast
   952 
   953 lemma image_Un: "f`(A Un B) = f`A Un f`B"
   954   by blast
   955 
   956 lemma image_iff: "(z : f`A) = (EX x:A. z = f x)"
   957   by blast
   958 
   959 lemma image_subset_iff: "(f`A \<subseteq> B) = (\<forall>x\<in>A. f x \<in> B)"
   960   -- {* This rewrite rule would confuse users if made default. *}
   961   by blast
   962 
   963 lemma subset_image_iff: "(B \<subseteq> f`A) = (EX AA. AA \<subseteq> A & B = f`AA)"
   964   apply safe
   965    prefer 2 apply fast
   966   apply (rule_tac x = "{a. a : A & f a : B}" in exI, fast)
   967   done
   968 
   969 lemma image_subsetI: "(!!x. x \<in> A ==> f x \<in> B) ==> f`A \<subseteq> B"
   970   -- {* Replaces the three steps @{text subsetI}, @{text imageE},
   971     @{text hypsubst}, but breaks too many existing proofs. *}
   972   by blast
   973 
   974 text {*
   975   \medskip Range of a function -- just a translation for image!
   976 *}
   977 
   978 lemma range_eqI: "b = f x ==> b \<in> range f"
   979   by simp
   980 
   981 lemma rangeI: "f x \<in> range f"
   982   by simp
   983 
   984 lemma rangeE [elim?]: "b \<in> range (\<lambda>x. f x) ==> (!!x. b = f x ==> P) ==> P"
   985   by blast
   986 
   987 
   988 subsubsection {* Set reasoning tools *}
   989 
   990 text {*
   991   Rewrite rules for boolean case-splitting: faster than @{text
   992   "split_if [split]"}.
   993 *}
   994 
   995 lemma split_if_eq1: "((if Q then x else y) = b) = ((Q --> x = b) & (~ Q --> y = b))"
   996   by (rule split_if)
   997 
   998 lemma split_if_eq2: "(a = (if Q then x else y)) = ((Q --> a = x) & (~ Q --> a = y))"
   999   by (rule split_if)
  1000 
  1001 text {*
  1002   Split ifs on either side of the membership relation.  Not for @{text
  1003   "[simp]"} -- can cause goals to blow up!
  1004 *}
  1005 
  1006 lemma split_if_mem1: "((if Q then x else y) : b) = ((Q --> x : b) & (~ Q --> y : b))"
  1007   by (rule split_if)
  1008 
  1009 lemma split_if_mem2: "(a : (if Q then x else y)) = ((Q --> a : x) & (~ Q --> a : y))"
  1010   by (rule split_if)
  1011 
  1012 lemmas split_ifs = if_bool_eq_conj split_if_eq1 split_if_eq2 split_if_mem1 split_if_mem2
  1013 
  1014 lemmas mem_simps =
  1015   insert_iff empty_iff Un_iff Int_iff Compl_iff Diff_iff
  1016   mem_Collect_eq UN_iff Union_iff INT_iff Inter_iff
  1017   -- {* Each of these has ALREADY been added @{text "[simp]"} above. *}
  1018 
  1019 (*Would like to add these, but the existing code only searches for the
  1020   outer-level constant, which in this case is just "op :"; we instead need
  1021   to use term-nets to associate patterns with rules.  Also, if a rule fails to
  1022   apply, then the formula should be kept.
  1023   [("HOL.uminus", Compl_iff RS iffD1), ("HOL.minus", [Diff_iff RS iffD1]),
  1024    ("op Int", [IntD1,IntD2]),
  1025    ("Collect", [CollectD]), ("Inter", [InterD]), ("INTER", [INT_D])]
  1026  *)
  1027 
  1028 ML_setup {*
  1029   val mksimps_pairs = [("Ball", @{thms bspec})] @ mksimps_pairs;
  1030   change_simpset (fn ss => ss setmksimps (mksimps mksimps_pairs));
  1031 *}
  1032 
  1033 
  1034 subsubsection {* The ``proper subset'' relation *}
  1035 
  1036 lemma psubsetI [intro!,noatp]: "A \<subseteq> B ==> A \<noteq> B ==> A \<subset> B"
  1037   by (unfold psubset_def) blast
  1038 
  1039 lemma psubsetE [elim!,noatp]: 
  1040     "[|A \<subset> B;  [|A \<subseteq> B; ~ (B\<subseteq>A)|] ==> R|] ==> R"
  1041   by (unfold psubset_def) blast
  1042 
  1043 lemma psubset_insert_iff:
  1044   "(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)"
  1045   by (auto simp add: psubset_def subset_insert_iff)
  1046 
  1047 lemma psubset_eq: "(A \<subset> B) = (A \<subseteq> B & A \<noteq> B)"
  1048   by (simp only: psubset_def)
  1049 
  1050 lemma psubset_imp_subset: "A \<subset> B ==> A \<subseteq> B"
  1051   by (simp add: psubset_eq)
  1052 
  1053 lemma psubset_trans: "[| A \<subset> B; B \<subset> C |] ==> A \<subset> C"
  1054 apply (unfold psubset_def)
  1055 apply (auto dest: subset_antisym)
  1056 done
  1057 
  1058 lemma psubsetD: "[| A \<subset> B; c \<in> A |] ==> c \<in> B"
  1059 apply (unfold psubset_def)
  1060 apply (auto dest: subsetD)
  1061 done
  1062 
  1063 lemma psubset_subset_trans: "A \<subset> B ==> B \<subseteq> C ==> A \<subset> C"
  1064   by (auto simp add: psubset_eq)
  1065 
  1066 lemma subset_psubset_trans: "A \<subseteq> B ==> B \<subset> C ==> A \<subset> C"
  1067   by (auto simp add: psubset_eq)
  1068 
  1069 lemma psubset_imp_ex_mem: "A \<subset> B ==> \<exists>b. b \<in> (B - A)"
  1070   by (unfold psubset_def) blast
  1071 
  1072 lemma atomize_ball:
  1073     "(!!x. x \<in> A ==> P x) == Trueprop (\<forall>x\<in>A. P x)"
  1074   by (simp only: Ball_def atomize_all atomize_imp)
  1075 
  1076 lemmas [symmetric, rulify] = atomize_ball
  1077   and [symmetric, defn] = atomize_ball
  1078 
  1079 
  1080 subsection {* Further set-theory lemmas *}
  1081 
  1082 subsubsection {* Derived rules involving subsets. *}
  1083 
  1084 text {* @{text insert}. *}
  1085 
  1086 lemma subset_insertI: "B \<subseteq> insert a B"
  1087   by (rule subsetI) (erule insertI2)
  1088 
  1089 lemma subset_insertI2: "A \<subseteq> B \<Longrightarrow> A \<subseteq> insert b B"
  1090   by blast
  1091 
  1092 lemma subset_insert: "x \<notin> A ==> (A \<subseteq> insert x B) = (A \<subseteq> B)"
  1093   by blast
  1094 
  1095 
  1096 text {* \medskip Big Union -- least upper bound of a set. *}
  1097 
  1098 lemma Union_upper: "B \<in> A ==> B \<subseteq> Union A"
  1099   by (iprover intro: subsetI UnionI)
  1100 
  1101 lemma Union_least: "(!!X. X \<in> A ==> X \<subseteq> C) ==> Union A \<subseteq> C"
  1102   by (iprover intro: subsetI elim: UnionE dest: subsetD)
  1103 
  1104 
  1105 text {* \medskip General union. *}
  1106 
  1107 lemma UN_upper: "a \<in> A ==> B a \<subseteq> (\<Union>x\<in>A. B x)"
  1108   by blast
  1109 
  1110 lemma UN_least: "(!!x. x \<in> A ==> B x \<subseteq> C) ==> (\<Union>x\<in>A. B x) \<subseteq> C"
  1111   by (iprover intro: subsetI elim: UN_E dest: subsetD)
  1112 
  1113 
  1114 text {* \medskip Big Intersection -- greatest lower bound of a set. *}
  1115 
  1116 lemma Inter_lower: "B \<in> A ==> Inter A \<subseteq> B"
  1117   by blast
  1118 
  1119 lemma Inter_subset:
  1120   "[| !!X. X \<in> A ==> X \<subseteq> B; A ~= {} |] ==> \<Inter>A \<subseteq> B"
  1121   by blast
  1122 
  1123 lemma Inter_greatest: "(!!X. X \<in> A ==> C \<subseteq> X) ==> C \<subseteq> Inter A"
  1124   by (iprover intro: InterI subsetI dest: subsetD)
  1125 
  1126 lemma INT_lower: "a \<in> A ==> (\<Inter>x\<in>A. B x) \<subseteq> B a"
  1127   by blast
  1128 
  1129 lemma INT_greatest: "(!!x. x \<in> A ==> C \<subseteq> B x) ==> C \<subseteq> (\<Inter>x\<in>A. B x)"
  1130   by (iprover intro: INT_I subsetI dest: subsetD)
  1131 
  1132 
  1133 text {* \medskip Finite Union -- the least upper bound of two sets. *}
  1134 
  1135 lemma Un_upper1: "A \<subseteq> A \<union> B"
  1136   by blast
  1137 
  1138 lemma Un_upper2: "B \<subseteq> A \<union> B"
  1139   by blast
  1140 
  1141 lemma Un_least: "A \<subseteq> C ==> B \<subseteq> C ==> A \<union> B \<subseteq> C"
  1142   by blast
  1143 
  1144 
  1145 text {* \medskip Finite Intersection -- the greatest lower bound of two sets. *}
  1146 
  1147 lemma Int_lower1: "A \<inter> B \<subseteq> A"
  1148   by blast
  1149 
  1150 lemma Int_lower2: "A \<inter> B \<subseteq> B"
  1151   by blast
  1152 
  1153 lemma Int_greatest: "C \<subseteq> A ==> C \<subseteq> B ==> C \<subseteq> A \<inter> B"
  1154   by blast
  1155 
  1156 
  1157 text {* \medskip Set difference. *}
  1158 
  1159 lemma Diff_subset: "A - B \<subseteq> A"
  1160   by blast
  1161 
  1162 lemma Diff_subset_conv: "(A - B \<subseteq> C) = (A \<subseteq> B \<union> C)"
  1163 by blast
  1164 
  1165 
  1166 subsubsection {* Equalities involving union, intersection, inclusion, etc. *}
  1167 
  1168 text {* @{text "{}"}. *}
  1169 
  1170 lemma Collect_const [simp]: "{s. P} = (if P then UNIV else {})"
  1171   -- {* supersedes @{text "Collect_False_empty"} *}
  1172   by auto
  1173 
  1174 lemma subset_empty [simp]: "(A \<subseteq> {}) = (A = {})"
  1175   by blast
  1176 
  1177 lemma not_psubset_empty [iff]: "\<not> (A < {})"
  1178   by (unfold psubset_def) blast
  1179 
  1180 lemma Collect_empty_eq [simp]: "(Collect P = {}) = (\<forall>x. \<not> P x)"
  1181 by blast
  1182 
  1183 lemma empty_Collect_eq [simp]: "({} = Collect P) = (\<forall>x. \<not> P x)"
  1184 by blast
  1185 
  1186 lemma Collect_neg_eq: "{x. \<not> P x} = - {x. P x}"
  1187   by blast
  1188 
  1189 lemma Collect_disj_eq: "{x. P x | Q x} = {x. P x} \<union> {x. Q x}"
  1190   by blast
  1191 
  1192 lemma Collect_imp_eq: "{x. P x --> Q x} = -{x. P x} \<union> {x. Q x}"
  1193   by blast
  1194 
  1195 lemma Collect_conj_eq: "{x. P x & Q x} = {x. P x} \<inter> {x. Q x}"
  1196   by blast
  1197 
  1198 lemma Collect_all_eq: "{x. \<forall>y. P x y} = (\<Inter>y. {x. P x y})"
  1199   by blast
  1200 
  1201 lemma Collect_ball_eq: "{x. \<forall>y\<in>A. P x y} = (\<Inter>y\<in>A. {x. P x y})"
  1202   by blast
  1203 
  1204 lemma Collect_ex_eq [noatp]: "{x. \<exists>y. P x y} = (\<Union>y. {x. P x y})"
  1205   by blast
  1206 
  1207 lemma Collect_bex_eq [noatp]: "{x. \<exists>y\<in>A. P x y} = (\<Union>y\<in>A. {x. P x y})"
  1208   by blast
  1209 
  1210 
  1211 text {* \medskip @{text insert}. *}
  1212 
  1213 lemma insert_is_Un: "insert a A = {a} Un A"
  1214   -- {* NOT SUITABLE FOR REWRITING since @{text "{a} == insert a {}"} *}
  1215   by blast
  1216 
  1217 lemma insert_not_empty [simp]: "insert a A \<noteq> {}"
  1218   by blast
  1219 
  1220 lemmas empty_not_insert = insert_not_empty [symmetric, standard]
  1221 declare empty_not_insert [simp]
  1222 
  1223 lemma insert_absorb: "a \<in> A ==> insert a A = A"
  1224   -- {* @{text "[simp]"} causes recursive calls when there are nested inserts *}
  1225   -- {* with \emph{quadratic} running time *}
  1226   by blast
  1227 
  1228 lemma insert_absorb2 [simp]: "insert x (insert x A) = insert x A"
  1229   by blast
  1230 
  1231 lemma insert_commute: "insert x (insert y A) = insert y (insert x A)"
  1232   by blast
  1233 
  1234 lemma insert_subset [simp]: "(insert x A \<subseteq> B) = (x \<in> B & A \<subseteq> B)"
  1235   by blast
  1236 
  1237 lemma mk_disjoint_insert: "a \<in> A ==> \<exists>B. A = insert a B & a \<notin> B"
  1238   -- {* use new @{text B} rather than @{text "A - {a}"} to avoid infinite unfolding *}
  1239   apply (rule_tac x = "A - {a}" in exI, blast)
  1240   done
  1241 
  1242 lemma insert_Collect: "insert a (Collect P) = {u. u \<noteq> a --> P u}"
  1243   by auto
  1244 
  1245 lemma UN_insert_distrib: "u \<in> A ==> (\<Union>x\<in>A. insert a (B x)) = insert a (\<Union>x\<in>A. B x)"
  1246   by blast
  1247 
  1248 lemma insert_inter_insert[simp]: "insert a A \<inter> insert a B = insert a (A \<inter> B)"
  1249   by blast
  1250 
  1251 lemma insert_disjoint [simp,noatp]:
  1252  "(insert a A \<inter> B = {}) = (a \<notin> B \<and> A \<inter> B = {})"
  1253  "({} = insert a A \<inter> B) = (a \<notin> B \<and> {} = A \<inter> B)"
  1254   by auto
  1255 
  1256 lemma disjoint_insert [simp,noatp]:
  1257  "(B \<inter> insert a A = {}) = (a \<notin> B \<and> B \<inter> A = {})"
  1258  "({} = A \<inter> insert b B) = (b \<notin> A \<and> {} = A \<inter> B)"
  1259   by auto
  1260 
  1261 text {* \medskip @{text image}. *}
  1262 
  1263 lemma image_empty [simp]: "f`{} = {}"
  1264   by blast
  1265 
  1266 lemma image_insert [simp]: "f ` insert a B = insert (f a) (f`B)"
  1267   by blast
  1268 
  1269 lemma image_constant: "x \<in> A ==> (\<lambda>x. c) ` A = {c}"
  1270   by auto
  1271 
  1272 lemma image_constant_conv: "(%x. c) ` A = (if A = {} then {} else {c})"
  1273 by auto
  1274 
  1275 lemma image_image: "f ` (g ` A) = (\<lambda>x. f (g x)) ` A"
  1276   by blast
  1277 
  1278 lemma insert_image [simp]: "x \<in> A ==> insert (f x) (f`A) = f`A"
  1279   by blast
  1280 
  1281 lemma image_is_empty [iff]: "(f`A = {}) = (A = {})"
  1282   by blast
  1283 
  1284 
  1285 lemma image_Collect [noatp]: "f ` {x. P x} = {f x | x. P x}"
  1286   -- {* NOT suitable as a default simprule: the RHS isn't simpler than the LHS,
  1287       with its implicit quantifier and conjunction.  Also image enjoys better
  1288       equational properties than does the RHS. *}
  1289   by blast
  1290 
  1291 lemma if_image_distrib [simp]:
  1292   "(\<lambda>x. if P x then f x else g x) ` S
  1293     = (f ` (S \<inter> {x. P x})) \<union> (g ` (S \<inter> {x. \<not> P x}))"
  1294   by (auto simp add: image_def)
  1295 
  1296 lemma image_cong: "M = N ==> (!!x. x \<in> N ==> f x = g x) ==> f`M = g`N"
  1297   by (simp add: image_def)
  1298 
  1299 
  1300 text {* \medskip @{text range}. *}
  1301 
  1302 lemma full_SetCompr_eq [noatp]: "{u. \<exists>x. u = f x} = range f"
  1303   by auto
  1304 
  1305 lemma range_composition [simp]: "range (\<lambda>x. f (g x)) = f`range g"
  1306 by (subst image_image, simp)
  1307 
  1308 
  1309 text {* \medskip @{text Int} *}
  1310 
  1311 lemma Int_absorb [simp]: "A \<inter> A = A"
  1312   by blast
  1313 
  1314 lemma Int_left_absorb: "A \<inter> (A \<inter> B) = A \<inter> B"
  1315   by blast
  1316 
  1317 lemma Int_commute: "A \<inter> B = B \<inter> A"
  1318   by blast
  1319 
  1320 lemma Int_left_commute: "A \<inter> (B \<inter> C) = B \<inter> (A \<inter> C)"
  1321   by blast
  1322 
  1323 lemma Int_assoc: "(A \<inter> B) \<inter> C = A \<inter> (B \<inter> C)"
  1324   by blast
  1325 
  1326 lemmas Int_ac = Int_assoc Int_left_absorb Int_commute Int_left_commute
  1327   -- {* Intersection is an AC-operator *}
  1328 
  1329 lemma Int_absorb1: "B \<subseteq> A ==> A \<inter> B = B"
  1330   by blast
  1331 
  1332 lemma Int_absorb2: "A \<subseteq> B ==> A \<inter> B = A"
  1333   by blast
  1334 
  1335 lemma Int_empty_left [simp]: "{} \<inter> B = {}"
  1336   by blast
  1337 
  1338 lemma Int_empty_right [simp]: "A \<inter> {} = {}"
  1339   by blast
  1340 
  1341 lemma disjoint_eq_subset_Compl: "(A \<inter> B = {}) = (A \<subseteq> -B)"
  1342   by blast
  1343 
  1344 lemma disjoint_iff_not_equal: "(A \<inter> B = {}) = (\<forall>x\<in>A. \<forall>y\<in>B. x \<noteq> y)"
  1345   by blast
  1346 
  1347 lemma Int_UNIV_left [simp]: "UNIV \<inter> B = B"
  1348   by blast
  1349 
  1350 lemma Int_UNIV_right [simp]: "A \<inter> UNIV = A"
  1351   by blast
  1352 
  1353 lemma Int_eq_Inter: "A \<inter> B = \<Inter>{A, B}"
  1354   by blast
  1355 
  1356 lemma Int_Un_distrib: "A \<inter> (B \<union> C) = (A \<inter> B) \<union> (A \<inter> C)"
  1357   by blast
  1358 
  1359 lemma Int_Un_distrib2: "(B \<union> C) \<inter> A = (B \<inter> A) \<union> (C \<inter> A)"
  1360   by blast
  1361 
  1362 lemma Int_UNIV [simp,noatp]: "(A \<inter> B = UNIV) = (A = UNIV & B = UNIV)"
  1363   by blast
  1364 
  1365 lemma Int_subset_iff [simp]: "(C \<subseteq> A \<inter> B) = (C \<subseteq> A & C \<subseteq> B)"
  1366   by blast
  1367 
  1368 lemma Int_Collect: "(x \<in> A \<inter> {x. P x}) = (x \<in> A & P x)"
  1369   by blast
  1370 
  1371 
  1372 text {* \medskip @{text Un}. *}
  1373 
  1374 lemma Un_absorb [simp]: "A \<union> A = A"
  1375   by blast
  1376 
  1377 lemma Un_left_absorb: "A \<union> (A \<union> B) = A \<union> B"
  1378   by blast
  1379 
  1380 lemma Un_commute: "A \<union> B = B \<union> A"
  1381   by blast
  1382 
  1383 lemma Un_left_commute: "A \<union> (B \<union> C) = B \<union> (A \<union> C)"
  1384   by blast
  1385 
  1386 lemma Un_assoc: "(A \<union> B) \<union> C = A \<union> (B \<union> C)"
  1387   by blast
  1388 
  1389 lemmas Un_ac = Un_assoc Un_left_absorb Un_commute Un_left_commute
  1390   -- {* Union is an AC-operator *}
  1391 
  1392 lemma Un_absorb1: "A \<subseteq> B ==> A \<union> B = B"
  1393   by blast
  1394 
  1395 lemma Un_absorb2: "B \<subseteq> A ==> A \<union> B = A"
  1396   by blast
  1397 
  1398 lemma Un_empty_left [simp]: "{} \<union> B = B"
  1399   by blast
  1400 
  1401 lemma Un_empty_right [simp]: "A \<union> {} = A"
  1402   by blast
  1403 
  1404 lemma Un_UNIV_left [simp]: "UNIV \<union> B = UNIV"
  1405   by blast
  1406 
  1407 lemma Un_UNIV_right [simp]: "A \<union> UNIV = UNIV"
  1408   by blast
  1409 
  1410 lemma Un_eq_Union: "A \<union> B = \<Union>{A, B}"
  1411   by blast
  1412 
  1413 lemma Un_insert_left [simp]: "(insert a B) \<union> C = insert a (B \<union> C)"
  1414   by blast
  1415 
  1416 lemma Un_insert_right [simp]: "A \<union> (insert a B) = insert a (A \<union> B)"
  1417   by blast
  1418 
  1419 lemma Int_insert_left:
  1420     "(insert a B) Int C = (if a \<in> C then insert a (B \<inter> C) else B \<inter> C)"
  1421   by auto
  1422 
  1423 lemma Int_insert_right:
  1424     "A \<inter> (insert a B) = (if a \<in> A then insert a (A \<inter> B) else A \<inter> B)"
  1425   by auto
  1426 
  1427 lemma Un_Int_distrib: "A \<union> (B \<inter> C) = (A \<union> B) \<inter> (A \<union> C)"
  1428   by blast
  1429 
  1430 lemma Un_Int_distrib2: "(B \<inter> C) \<union> A = (B \<union> A) \<inter> (C \<union> A)"
  1431   by blast
  1432 
  1433 lemma Un_Int_crazy:
  1434     "(A \<inter> B) \<union> (B \<inter> C) \<union> (C \<inter> A) = (A \<union> B) \<inter> (B \<union> C) \<inter> (C \<union> A)"
  1435   by blast
  1436 
  1437 lemma subset_Un_eq: "(A \<subseteq> B) = (A \<union> B = B)"
  1438   by blast
  1439 
  1440 lemma Un_empty [iff]: "(A \<union> B = {}) = (A = {} & B = {})"
  1441   by blast
  1442 
  1443 lemma Un_subset_iff [simp]: "(A \<union> B \<subseteq> C) = (A \<subseteq> C & B \<subseteq> C)"
  1444   by blast
  1445 
  1446 lemma Un_Diff_Int: "(A - B) \<union> (A \<inter> B) = A"
  1447   by blast
  1448 
  1449 lemma Diff_Int2: "A \<inter> C - B \<inter> C = A \<inter> C - B"
  1450   by blast
  1451 
  1452 
  1453 text {* \medskip Set complement *}
  1454 
  1455 lemma Compl_disjoint [simp]: "A \<inter> -A = {}"
  1456   by blast
  1457 
  1458 lemma Compl_disjoint2 [simp]: "-A \<inter> A = {}"
  1459   by blast
  1460 
  1461 lemma Compl_partition: "A \<union> -A = UNIV"
  1462   by blast
  1463 
  1464 lemma Compl_partition2: "-A \<union> A = UNIV"
  1465   by blast
  1466 
  1467 lemma double_complement [simp]: "- (-A) = (A::'a set)"
  1468   by blast
  1469 
  1470 lemma Compl_Un [simp]: "-(A \<union> B) = (-A) \<inter> (-B)"
  1471   by blast
  1472 
  1473 lemma Compl_Int [simp]: "-(A \<inter> B) = (-A) \<union> (-B)"
  1474   by blast
  1475 
  1476 lemma Compl_UN [simp]: "-(\<Union>x\<in>A. B x) = (\<Inter>x\<in>A. -B x)"
  1477   by blast
  1478 
  1479 lemma Compl_INT [simp]: "-(\<Inter>x\<in>A. B x) = (\<Union>x\<in>A. -B x)"
  1480   by blast
  1481 
  1482 lemma subset_Compl_self_eq: "(A \<subseteq> -A) = (A = {})"
  1483   by blast
  1484 
  1485 lemma Un_Int_assoc_eq: "((A \<inter> B) \<union> C = A \<inter> (B \<union> C)) = (C \<subseteq> A)"
  1486   -- {* Halmos, Naive Set Theory, page 16. *}
  1487   by blast
  1488 
  1489 lemma Compl_UNIV_eq [simp]: "-UNIV = {}"
  1490   by blast
  1491 
  1492 lemma Compl_empty_eq [simp]: "-{} = UNIV"
  1493   by blast
  1494 
  1495 lemma Compl_subset_Compl_iff [iff]: "(-A \<subseteq> -B) = (B \<subseteq> A)"
  1496   by blast
  1497 
  1498 lemma Compl_eq_Compl_iff [iff]: "(-A = -B) = (A = (B::'a set))"
  1499   by blast
  1500 
  1501 
  1502 text {* \medskip @{text Union}. *}
  1503 
  1504 lemma Union_empty [simp]: "Union({}) = {}"
  1505   by blast
  1506 
  1507 lemma Union_UNIV [simp]: "Union UNIV = UNIV"
  1508   by blast
  1509 
  1510 lemma Union_insert [simp]: "Union (insert a B) = a \<union> \<Union>B"
  1511   by blast
  1512 
  1513 lemma Union_Un_distrib [simp]: "\<Union>(A Un B) = \<Union>A \<union> \<Union>B"
  1514   by blast
  1515 
  1516 lemma Union_Int_subset: "\<Union>(A \<inter> B) \<subseteq> \<Union>A \<inter> \<Union>B"
  1517   by blast
  1518 
  1519 lemma Union_empty_conv [simp,noatp]: "(\<Union>A = {}) = (\<forall>x\<in>A. x = {})"
  1520   by blast
  1521 
  1522 lemma empty_Union_conv [simp,noatp]: "({} = \<Union>A) = (\<forall>x\<in>A. x = {})"
  1523   by blast
  1524 
  1525 lemma Union_disjoint: "(\<Union>C \<inter> A = {}) = (\<forall>B\<in>C. B \<inter> A = {})"
  1526   by blast
  1527 
  1528 
  1529 text {* \medskip @{text Inter}. *}
  1530 
  1531 lemma Inter_empty [simp]: "\<Inter>{} = UNIV"
  1532   by blast
  1533 
  1534 lemma Inter_UNIV [simp]: "\<Inter>UNIV = {}"
  1535   by blast
  1536 
  1537 lemma Inter_insert [simp]: "\<Inter>(insert a B) = a \<inter> \<Inter>B"
  1538   by blast
  1539 
  1540 lemma Inter_Un_subset: "\<Inter>A \<union> \<Inter>B \<subseteq> \<Inter>(A \<inter> B)"
  1541   by blast
  1542 
  1543 lemma Inter_Un_distrib: "\<Inter>(A \<union> B) = \<Inter>A \<inter> \<Inter>B"
  1544   by blast
  1545 
  1546 lemma Inter_UNIV_conv [simp,noatp]:
  1547   "(\<Inter>A = UNIV) = (\<forall>x\<in>A. x = UNIV)"
  1548   "(UNIV = \<Inter>A) = (\<forall>x\<in>A. x = UNIV)"
  1549   by blast+
  1550 
  1551 
  1552 text {*
  1553   \medskip @{text UN} and @{text INT}.
  1554 
  1555   Basic identities: *}
  1556 
  1557 lemma UN_empty [simp,noatp]: "(\<Union>x\<in>{}. B x) = {}"
  1558   by blast
  1559 
  1560 lemma UN_empty2 [simp]: "(\<Union>x\<in>A. {}) = {}"
  1561   by blast
  1562 
  1563 lemma UN_singleton [simp]: "(\<Union>x\<in>A. {x}) = A"
  1564   by blast
  1565 
  1566 lemma UN_absorb: "k \<in> I ==> A k \<union> (\<Union>i\<in>I. A i) = (\<Union>i\<in>I. A i)"
  1567   by auto
  1568 
  1569 lemma INT_empty [simp]: "(\<Inter>x\<in>{}. B x) = UNIV"
  1570   by blast
  1571 
  1572 lemma INT_absorb: "k \<in> I ==> A k \<inter> (\<Inter>i\<in>I. A i) = (\<Inter>i\<in>I. A i)"
  1573   by blast
  1574 
  1575 lemma UN_insert [simp]: "(\<Union>x\<in>insert a A. B x) = B a \<union> UNION A B"
  1576   by blast
  1577 
  1578 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)"
  1579   by blast
  1580 
  1581 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)"
  1582   by blast
  1583 
  1584 lemma UN_subset_iff: "((\<Union>i\<in>I. A i) \<subseteq> B) = (\<forall>i\<in>I. A i \<subseteq> B)"
  1585   by blast
  1586 
  1587 lemma INT_subset_iff: "(B \<subseteq> (\<Inter>i\<in>I. A i)) = (\<forall>i\<in>I. B \<subseteq> A i)"
  1588   by blast
  1589 
  1590 lemma INT_insert [simp]: "(\<Inter>x \<in> insert a A. B x) = B a \<inter> INTER A B"
  1591   by blast
  1592 
  1593 lemma INT_Un: "(\<Inter>i \<in> A \<union> B. M i) = (\<Inter>i \<in> A. M i) \<inter> (\<Inter>i\<in>B. M i)"
  1594   by blast
  1595 
  1596 lemma INT_insert_distrib:
  1597     "u \<in> A ==> (\<Inter>x\<in>A. insert a (B x)) = insert a (\<Inter>x\<in>A. B x)"
  1598   by blast
  1599 
  1600 lemma Union_image_eq [simp]: "\<Union>(B`A) = (\<Union>x\<in>A. B x)"
  1601   by blast
  1602 
  1603 lemma image_Union: "f ` \<Union>S = (\<Union>x\<in>S. f ` x)"
  1604   by blast
  1605 
  1606 lemma Inter_image_eq [simp]: "\<Inter>(B`A) = (\<Inter>x\<in>A. B x)"
  1607   by blast
  1608 
  1609 lemma UN_constant [simp]: "(\<Union>y\<in>A. c) = (if A = {} then {} else c)"
  1610   by auto
  1611 
  1612 lemma INT_constant [simp]: "(\<Inter>y\<in>A. c) = (if A = {} then UNIV else c)"
  1613   by auto
  1614 
  1615 lemma UN_eq: "(\<Union>x\<in>A. B x) = \<Union>({Y. \<exists>x\<in>A. Y = B x})"
  1616   by blast
  1617 
  1618 lemma INT_eq: "(\<Inter>x\<in>A. B x) = \<Inter>({Y. \<exists>x\<in>A. Y = B x})"
  1619   -- {* Look: it has an \emph{existential} quantifier *}
  1620   by blast
  1621 
  1622 lemma UNION_empty_conv[simp]:
  1623   "({} = (UN x:A. B x)) = (\<forall>x\<in>A. B x = {})"
  1624   "((UN x:A. B x) = {}) = (\<forall>x\<in>A. B x = {})"
  1625 by blast+
  1626 
  1627 lemma INTER_UNIV_conv[simp]:
  1628  "(UNIV = (INT x:A. B x)) = (\<forall>x\<in>A. B x = UNIV)"
  1629  "((INT x:A. B x) = UNIV) = (\<forall>x\<in>A. B x = UNIV)"
  1630 by blast+
  1631 
  1632 
  1633 text {* \medskip Distributive laws: *}
  1634 
  1635 lemma Int_Union: "A \<inter> \<Union>B = (\<Union>C\<in>B. A \<inter> C)"
  1636   by blast
  1637 
  1638 lemma Int_Union2: "\<Union>B \<inter> A = (\<Union>C\<in>B. C \<inter> A)"
  1639   by blast
  1640 
  1641 lemma Un_Union_image: "(\<Union>x\<in>C. A x \<union> B x) = \<Union>(A`C) \<union> \<Union>(B`C)"
  1642   -- {* Devlin, Fundamentals of Contemporary Set Theory, page 12, exercise 5: *}
  1643   -- {* Union of a family of unions *}
  1644   by blast
  1645 
  1646 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)"
  1647   -- {* Equivalent version *}
  1648   by blast
  1649 
  1650 lemma Un_Inter: "A \<union> \<Inter>B = (\<Inter>C\<in>B. A \<union> C)"
  1651   by blast
  1652 
  1653 lemma Int_Inter_image: "(\<Inter>x\<in>C. A x \<inter> B x) = \<Inter>(A`C) \<inter> \<Inter>(B`C)"
  1654   by blast
  1655 
  1656 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)"
  1657   -- {* Equivalent version *}
  1658   by blast
  1659 
  1660 lemma Int_UN_distrib: "B \<inter> (\<Union>i\<in>I. A i) = (\<Union>i\<in>I. B \<inter> A i)"
  1661   -- {* Halmos, Naive Set Theory, page 35. *}
  1662   by blast
  1663 
  1664 lemma Un_INT_distrib: "B \<union> (\<Inter>i\<in>I. A i) = (\<Inter>i\<in>I. B \<union> A i)"
  1665   by blast
  1666 
  1667 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)"
  1668   by blast
  1669 
  1670 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)"
  1671   by blast
  1672 
  1673 
  1674 text {* \medskip Bounded quantifiers.
  1675 
  1676   The following are not added to the default simpset because
  1677   (a) they duplicate the body and (b) there are no similar rules for @{text Int}. *}
  1678 
  1679 lemma ball_Un: "(\<forall>x \<in> A \<union> B. P x) = ((\<forall>x\<in>A. P x) & (\<forall>x\<in>B. P x))"
  1680   by blast
  1681 
  1682 lemma bex_Un: "(\<exists>x \<in> A \<union> B. P x) = ((\<exists>x\<in>A. P x) | (\<exists>x\<in>B. P x))"
  1683   by blast
  1684 
  1685 lemma ball_UN: "(\<forall>z \<in> UNION A B. P z) = (\<forall>x\<in>A. \<forall>z \<in> B x. P z)"
  1686   by blast
  1687 
  1688 lemma bex_UN: "(\<exists>z \<in> UNION A B. P z) = (\<exists>x\<in>A. \<exists>z\<in>B x. P z)"
  1689   by blast
  1690 
  1691 
  1692 text {* \medskip Set difference. *}
  1693 
  1694 lemma Diff_eq: "A - B = A \<inter> (-B)"
  1695   by blast
  1696 
  1697 lemma Diff_eq_empty_iff [simp,noatp]: "(A - B = {}) = (A \<subseteq> B)"
  1698   by blast
  1699 
  1700 lemma Diff_cancel [simp]: "A - A = {}"
  1701   by blast
  1702 
  1703 lemma Diff_idemp [simp]: "(A - B) - B = A - (B::'a set)"
  1704 by blast
  1705 
  1706 lemma Diff_triv: "A \<inter> B = {} ==> A - B = A"
  1707   by (blast elim: equalityE)
  1708 
  1709 lemma empty_Diff [simp]: "{} - A = {}"
  1710   by blast
  1711 
  1712 lemma Diff_empty [simp]: "A - {} = A"
  1713   by blast
  1714 
  1715 lemma Diff_UNIV [simp]: "A - UNIV = {}"
  1716   by blast
  1717 
  1718 lemma Diff_insert0 [simp,noatp]: "x \<notin> A ==> A - insert x B = A - B"
  1719   by blast
  1720 
  1721 lemma Diff_insert: "A - insert a B = A - B - {a}"
  1722   -- {* NOT SUITABLE FOR REWRITING since @{text "{a} == insert a 0"} *}
  1723   by blast
  1724 
  1725 lemma Diff_insert2: "A - insert a B = A - {a} - B"
  1726   -- {* NOT SUITABLE FOR REWRITING since @{text "{a} == insert a 0"} *}
  1727   by blast
  1728 
  1729 lemma insert_Diff_if: "insert x A - B = (if x \<in> B then A - B else insert x (A - B))"
  1730   by auto
  1731 
  1732 lemma insert_Diff1 [simp]: "x \<in> B ==> insert x A - B = A - B"
  1733   by blast
  1734 
  1735 lemma insert_Diff_single[simp]: "insert a (A - {a}) = insert a A"
  1736 by blast
  1737 
  1738 lemma insert_Diff: "a \<in> A ==> insert a (A - {a}) = A"
  1739   by blast
  1740 
  1741 lemma Diff_insert_absorb: "x \<notin> A ==> (insert x A) - {x} = A"
  1742   by auto
  1743 
  1744 lemma Diff_disjoint [simp]: "A \<inter> (B - A) = {}"
  1745   by blast
  1746 
  1747 lemma Diff_partition: "A \<subseteq> B ==> A \<union> (B - A) = B"
  1748   by blast
  1749 
  1750 lemma double_diff: "A \<subseteq> B ==> B \<subseteq> C ==> B - (C - A) = A"
  1751   by blast
  1752 
  1753 lemma Un_Diff_cancel [simp]: "A \<union> (B - A) = A \<union> B"
  1754   by blast
  1755 
  1756 lemma Un_Diff_cancel2 [simp]: "(B - A) \<union> A = B \<union> A"
  1757   by blast
  1758 
  1759 lemma Diff_Un: "A - (B \<union> C) = (A - B) \<inter> (A - C)"
  1760   by blast
  1761 
  1762 lemma Diff_Int: "A - (B \<inter> C) = (A - B) \<union> (A - C)"
  1763   by blast
  1764 
  1765 lemma Un_Diff: "(A \<union> B) - C = (A - C) \<union> (B - C)"
  1766   by blast
  1767 
  1768 lemma Int_Diff: "(A \<inter> B) - C = A \<inter> (B - C)"
  1769   by blast
  1770 
  1771 lemma Diff_Int_distrib: "C \<inter> (A - B) = (C \<inter> A) - (C \<inter> B)"
  1772   by blast
  1773 
  1774 lemma Diff_Int_distrib2: "(A - B) \<inter> C = (A \<inter> C) - (B \<inter> C)"
  1775   by blast
  1776 
  1777 lemma Diff_Compl [simp]: "A - (- B) = A \<inter> B"
  1778   by auto
  1779 
  1780 lemma Compl_Diff_eq [simp]: "- (A - B) = -A \<union> B"
  1781   by blast
  1782 
  1783 
  1784 text {* \medskip Quantification over type @{typ bool}. *}
  1785 
  1786 lemma bool_induct: "P True \<Longrightarrow> P False \<Longrightarrow> P x"
  1787   by (cases x) auto
  1788 
  1789 lemma all_bool_eq: "(\<forall>b. P b) \<longleftrightarrow> P True \<and> P False"
  1790   by (auto intro: bool_induct)
  1791 
  1792 lemma bool_contrapos: "P x \<Longrightarrow> \<not> P False \<Longrightarrow> P True"
  1793   by (cases x) auto
  1794 
  1795 lemma ex_bool_eq: "(\<exists>b. P b) \<longleftrightarrow> P True \<or> P False"
  1796   by (auto intro: bool_contrapos)
  1797 
  1798 lemma Un_eq_UN: "A \<union> B = (\<Union>b. if b then A else B)"
  1799   by (auto simp add: split_if_mem2)
  1800 
  1801 lemma UN_bool_eq: "(\<Union>b::bool. A b) = (A True \<union> A False)"
  1802   by (auto intro: bool_contrapos)
  1803 
  1804 lemma INT_bool_eq: "(\<Inter>b::bool. A b) = (A True \<inter> A False)"
  1805   by (auto intro: bool_induct)
  1806 
  1807 text {* \medskip @{text Pow} *}
  1808 
  1809 lemma Pow_empty [simp]: "Pow {} = {{}}"
  1810   by (auto simp add: Pow_def)
  1811 
  1812 lemma Pow_insert: "Pow (insert a A) = Pow A \<union> (insert a ` Pow A)"
  1813   by (blast intro: image_eqI [where ?x = "u - {a}", standard])
  1814 
  1815 lemma Pow_Compl: "Pow (- A) = {-B | B. A \<in> Pow B}"
  1816   by (blast intro: exI [where ?x = "- u", standard])
  1817 
  1818 lemma Pow_UNIV [simp]: "Pow UNIV = UNIV"
  1819   by blast
  1820 
  1821 lemma Un_Pow_subset: "Pow A \<union> Pow B \<subseteq> Pow (A \<union> B)"
  1822   by blast
  1823 
  1824 lemma UN_Pow_subset: "(\<Union>x\<in>A. Pow (B x)) \<subseteq> Pow (\<Union>x\<in>A. B x)"
  1825   by blast
  1826 
  1827 lemma subset_Pow_Union: "A \<subseteq> Pow (\<Union>A)"
  1828   by blast
  1829 
  1830 lemma Union_Pow_eq [simp]: "\<Union>(Pow A) = A"
  1831   by blast
  1832 
  1833 lemma Pow_Int_eq [simp]: "Pow (A \<inter> B) = Pow A \<inter> Pow B"
  1834   by blast
  1835 
  1836 lemma Pow_INT_eq: "Pow (\<Inter>x\<in>A. B x) = (\<Inter>x\<in>A. Pow (B x))"
  1837   by blast
  1838 
  1839 
  1840 text {* \medskip Miscellany. *}
  1841 
  1842 lemma set_eq_subset: "(A = B) = (A \<subseteq> B & B \<subseteq> A)"
  1843   by blast
  1844 
  1845 lemma subset_iff: "(A \<subseteq> B) = (\<forall>t. t \<in> A --> t \<in> B)"
  1846   by blast
  1847 
  1848 lemma subset_iff_psubset_eq: "(A \<subseteq> B) = ((A \<subset> B) | (A = B))"
  1849   by (unfold psubset_def) blast
  1850 
  1851 lemma all_not_in_conv [simp]: "(\<forall>x. x \<notin> A) = (A = {})"
  1852   by blast
  1853 
  1854 lemma ex_in_conv: "(\<exists>x. x \<in> A) = (A \<noteq> {})"
  1855   by blast
  1856 
  1857 lemma distinct_lemma: "f x \<noteq> f y ==> x \<noteq> y"
  1858   by iprover
  1859 
  1860 
  1861 text {* \medskip Miniscoping: pushing in quantifiers and big Unions
  1862            and Intersections. *}
  1863 
  1864 lemma UN_simps [simp]:
  1865   "!!a B C. (UN x:C. insert a (B x)) = (if C={} then {} else insert a (UN x:C. B x))"
  1866   "!!A B C. (UN x:C. A x Un B)   = ((if C={} then {} else (UN x:C. A x) Un B))"
  1867   "!!A B C. (UN x:C. A Un B x)   = ((if C={} then {} else A Un (UN x:C. B x)))"
  1868   "!!A B C. (UN x:C. A x Int B)  = ((UN x:C. A x) Int B)"
  1869   "!!A B C. (UN x:C. A Int B x)  = (A Int (UN x:C. B x))"
  1870   "!!A B C. (UN x:C. A x - B)    = ((UN x:C. A x) - B)"
  1871   "!!A B C. (UN x:C. A - B x)    = (A - (INT x:C. B x))"
  1872   "!!A B. (UN x: Union A. B x) = (UN y:A. UN x:y. B x)"
  1873   "!!A B C. (UN z: UNION A B. C z) = (UN  x:A. UN z: B(x). C z)"
  1874   "!!A B f. (UN x:f`A. B x)     = (UN a:A. B (f a))"
  1875   by auto
  1876 
  1877 lemma INT_simps [simp]:
  1878   "!!A B C. (INT x:C. A x Int B) = (if C={} then UNIV else (INT x:C. A x) Int B)"
  1879   "!!A B C. (INT x:C. A Int B x) = (if C={} then UNIV else A Int (INT x:C. B x))"
  1880   "!!A B C. (INT x:C. A x - B)   = (if C={} then UNIV else (INT x:C. A x) - B)"
  1881   "!!A B C. (INT x:C. A - B x)   = (if C={} then UNIV else A - (UN x:C. B x))"
  1882   "!!a B C. (INT x:C. insert a (B x)) = insert a (INT x:C. B x)"
  1883   "!!A B C. (INT x:C. A x Un B)  = ((INT x:C. A x) Un B)"
  1884   "!!A B C. (INT x:C. A Un B x)  = (A Un (INT x:C. B x))"
  1885   "!!A B. (INT x: Union A. B x) = (INT y:A. INT x:y. B x)"
  1886   "!!A B C. (INT z: UNION A B. C z) = (INT x:A. INT z: B(x). C z)"
  1887   "!!A B f. (INT x:f`A. B x)    = (INT a:A. B (f a))"
  1888   by auto
  1889 
  1890 lemma ball_simps [simp,noatp]:
  1891   "!!A P Q. (ALL x:A. P x | Q) = ((ALL x:A. P x) | Q)"
  1892   "!!A P Q. (ALL x:A. P | Q x) = (P | (ALL x:A. Q x))"
  1893   "!!A P Q. (ALL x:A. P --> Q x) = (P --> (ALL x:A. Q x))"
  1894   "!!A P Q. (ALL x:A. P x --> Q) = ((EX x:A. P x) --> Q)"
  1895   "!!P. (ALL x:{}. P x) = True"
  1896   "!!P. (ALL x:UNIV. P x) = (ALL x. P x)"
  1897   "!!a B P. (ALL x:insert a B. P x) = (P a & (ALL x:B. P x))"
  1898   "!!A P. (ALL x:Union A. P x) = (ALL y:A. ALL x:y. P x)"
  1899   "!!A B P. (ALL x: UNION A B. P x) = (ALL a:A. ALL x: B a. P x)"
  1900   "!!P Q. (ALL x:Collect Q. P x) = (ALL x. Q x --> P x)"
  1901   "!!A P f. (ALL x:f`A. P x) = (ALL x:A. P (f x))"
  1902   "!!A P. (~(ALL x:A. P x)) = (EX x:A. ~P x)"
  1903   by auto
  1904 
  1905 lemma bex_simps [simp,noatp]:
  1906   "!!A P Q. (EX x:A. P x & Q) = ((EX x:A. P x) & Q)"
  1907   "!!A P Q. (EX x:A. P & Q x) = (P & (EX x:A. Q x))"
  1908   "!!P. (EX x:{}. P x) = False"
  1909   "!!P. (EX x:UNIV. P x) = (EX x. P x)"
  1910   "!!a B P. (EX x:insert a B. P x) = (P(a) | (EX x:B. P x))"
  1911   "!!A P. (EX x:Union A. P x) = (EX y:A. EX x:y. P x)"
  1912   "!!A B P. (EX x: UNION A B. P x) = (EX a:A. EX x:B a. P x)"
  1913   "!!P Q. (EX x:Collect Q. P x) = (EX x. Q x & P x)"
  1914   "!!A P f. (EX x:f`A. P x) = (EX x:A. P (f x))"
  1915   "!!A P. (~(EX x:A. P x)) = (ALL x:A. ~P x)"
  1916   by auto
  1917 
  1918 lemma ball_conj_distrib:
  1919   "(ALL x:A. P x & Q x) = ((ALL x:A. P x) & (ALL x:A. Q x))"
  1920   by blast
  1921 
  1922 lemma bex_disj_distrib:
  1923   "(EX x:A. P x | Q x) = ((EX x:A. P x) | (EX x:A. Q x))"
  1924   by blast
  1925 
  1926 
  1927 text {* \medskip Maxiscoping: pulling out big Unions and Intersections. *}
  1928 
  1929 lemma UN_extend_simps:
  1930   "!!a B C. insert a (UN x:C. B x) = (if C={} then {a} else (UN x:C. insert a (B x)))"
  1931   "!!A B C. (UN x:C. A x) Un B    = (if C={} then B else (UN x:C. A x Un B))"
  1932   "!!A B C. A Un (UN x:C. B x)   = (if C={} then A else (UN x:C. A Un B x))"
  1933   "!!A B C. ((UN x:C. A x) Int B) = (UN x:C. A x Int B)"
  1934   "!!A B C. (A Int (UN x:C. B x)) = (UN x:C. A Int B x)"
  1935   "!!A B C. ((UN x:C. A x) - B) = (UN x:C. A x - B)"
  1936   "!!A B C. (A - (INT x:C. B x)) = (UN x:C. A - B x)"
  1937   "!!A B. (UN y:A. UN x:y. B x) = (UN x: Union A. B x)"
  1938   "!!A B C. (UN  x:A. UN z: B(x). C z) = (UN z: UNION A B. C z)"
  1939   "!!A B f. (UN a:A. B (f a)) = (UN x:f`A. B x)"
  1940   by auto
  1941 
  1942 lemma INT_extend_simps:
  1943   "!!A B C. (INT x:C. A x) Int B = (if C={} then B else (INT x:C. A x Int B))"
  1944   "!!A B C. A Int (INT x:C. B x) = (if C={} then A else (INT x:C. A Int B x))"
  1945   "!!A B C. (INT x:C. A x) - B   = (if C={} then UNIV-B else (INT x:C. A x - B))"
  1946   "!!A B C. A - (UN x:C. B x)   = (if C={} then A else (INT x:C. A - B x))"
  1947   "!!a B C. insert a (INT x:C. B x) = (INT x:C. insert a (B x))"
  1948   "!!A B C. ((INT x:C. A x) Un B)  = (INT x:C. A x Un B)"
  1949   "!!A B C. A Un (INT x:C. B x)  = (INT x:C. A Un B x)"
  1950   "!!A B. (INT y:A. INT x:y. B x) = (INT x: Union A. B x)"
  1951   "!!A B C. (INT x:A. INT z: B(x). C z) = (INT z: UNION A B. C z)"
  1952   "!!A B f. (INT a:A. B (f a))    = (INT x:f`A. B x)"
  1953   by auto
  1954 
  1955 
  1956 subsubsection {* Monotonicity of various operations *}
  1957 
  1958 lemma image_mono: "A \<subseteq> B ==> f`A \<subseteq> f`B"
  1959   by blast
  1960 
  1961 lemma Pow_mono: "A \<subseteq> B ==> Pow A \<subseteq> Pow B"
  1962   by blast
  1963 
  1964 lemma Union_mono: "A \<subseteq> B ==> \<Union>A \<subseteq> \<Union>B"
  1965   by blast
  1966 
  1967 lemma Inter_anti_mono: "B \<subseteq> A ==> \<Inter>A \<subseteq> \<Inter>B"
  1968   by blast
  1969 
  1970 lemma UN_mono:
  1971   "A \<subseteq> B ==> (!!x. x \<in> A ==> f x \<subseteq> g x) ==>
  1972     (\<Union>x\<in>A. f x) \<subseteq> (\<Union>x\<in>B. g x)"
  1973   by (blast dest: subsetD)
  1974 
  1975 lemma INT_anti_mono:
  1976   "B \<subseteq> A ==> (!!x. x \<in> A ==> f x \<subseteq> g x) ==>
  1977     (\<Inter>x\<in>A. f x) \<subseteq> (\<Inter>x\<in>A. g x)"
  1978   -- {* The last inclusion is POSITIVE! *}
  1979   by (blast dest: subsetD)
  1980 
  1981 lemma insert_mono: "C \<subseteq> D ==> insert a C \<subseteq> insert a D"
  1982   by blast
  1983 
  1984 lemma Un_mono: "A \<subseteq> C ==> B \<subseteq> D ==> A \<union> B \<subseteq> C \<union> D"
  1985   by blast
  1986 
  1987 lemma Int_mono: "A \<subseteq> C ==> B \<subseteq> D ==> A \<inter> B \<subseteq> C \<inter> D"
  1988   by blast
  1989 
  1990 lemma Diff_mono: "A \<subseteq> C ==> D \<subseteq> B ==> A - B \<subseteq> C - D"
  1991   by blast
  1992 
  1993 lemma Compl_anti_mono: "A \<subseteq> B ==> -B \<subseteq> -A"
  1994   by blast
  1995 
  1996 text {* \medskip Monotonicity of implications. *}
  1997 
  1998 lemma in_mono: "A \<subseteq> B ==> x \<in> A --> x \<in> B"
  1999   apply (rule impI)
  2000   apply (erule subsetD, assumption)
  2001   done
  2002 
  2003 lemma conj_mono: "P1 --> Q1 ==> P2 --> Q2 ==> (P1 & P2) --> (Q1 & Q2)"
  2004   by iprover
  2005 
  2006 lemma disj_mono: "P1 --> Q1 ==> P2 --> Q2 ==> (P1 | P2) --> (Q1 | Q2)"
  2007   by iprover
  2008 
  2009 lemma imp_mono: "Q1 --> P1 ==> P2 --> Q2 ==> (P1 --> P2) --> (Q1 --> Q2)"
  2010   by iprover
  2011 
  2012 lemma imp_refl: "P --> P" ..
  2013 
  2014 lemma ex_mono: "(!!x. P x --> Q x) ==> (EX x. P x) --> (EX x. Q x)"
  2015   by iprover
  2016 
  2017 lemma all_mono: "(!!x. P x --> Q x) ==> (ALL x. P x) --> (ALL x. Q x)"
  2018   by iprover
  2019 
  2020 lemma Collect_mono: "(!!x. P x --> Q x) ==> Collect P \<subseteq> Collect Q"
  2021   by blast
  2022 
  2023 lemma Int_Collect_mono:
  2024     "A \<subseteq> B ==> (!!x. x \<in> A ==> P x --> Q x) ==> A \<inter> Collect P \<subseteq> B \<inter> Collect Q"
  2025   by blast
  2026 
  2027 lemmas basic_monos =
  2028   subset_refl imp_refl disj_mono conj_mono
  2029   ex_mono Collect_mono in_mono
  2030 
  2031 lemma eq_to_mono: "a = b ==> c = d ==> b --> d ==> a --> c"
  2032   by iprover
  2033 
  2034 lemma eq_to_mono2: "a = b ==> c = d ==> ~ b --> ~ d ==> ~ a --> ~ c"
  2035   by iprover
  2036 
  2037 
  2038 subsection {* Inverse image of a function *}
  2039 
  2040 constdefs
  2041   vimage :: "('a => 'b) => 'b set => 'a set"    (infixr "-`" 90)
  2042   "f -` B == {x. f x : B}"
  2043 
  2044 
  2045 subsubsection {* Basic rules *}
  2046 
  2047 lemma vimage_eq [simp]: "(a : f -` B) = (f a : B)"
  2048   by (unfold vimage_def) blast
  2049 
  2050 lemma vimage_singleton_eq: "(a : f -` {b}) = (f a = b)"
  2051   by simp
  2052 
  2053 lemma vimageI [intro]: "f a = b ==> b:B ==> a : f -` B"
  2054   by (unfold vimage_def) blast
  2055 
  2056 lemma vimageI2: "f a : A ==> a : f -` A"
  2057   by (unfold vimage_def) fast
  2058 
  2059 lemma vimageE [elim!]: "a: f -` B ==> (!!x. f a = x ==> x:B ==> P) ==> P"
  2060   by (unfold vimage_def) blast
  2061 
  2062 lemma vimageD: "a : f -` A ==> f a : A"
  2063   by (unfold vimage_def) fast
  2064 
  2065 
  2066 subsubsection {* Equations *}
  2067 
  2068 lemma vimage_empty [simp]: "f -` {} = {}"
  2069   by blast
  2070 
  2071 lemma vimage_Compl: "f -` (-A) = -(f -` A)"
  2072   by blast
  2073 
  2074 lemma vimage_Un [simp]: "f -` (A Un B) = (f -` A) Un (f -` B)"
  2075   by blast
  2076 
  2077 lemma vimage_Int [simp]: "f -` (A Int B) = (f -` A) Int (f -` B)"
  2078   by fast
  2079 
  2080 lemma vimage_Union: "f -` (Union A) = (UN X:A. f -` X)"
  2081   by blast
  2082 
  2083 lemma vimage_UN: "f-`(UN x:A. B x) = (UN x:A. f -` B x)"
  2084   by blast
  2085 
  2086 lemma vimage_INT: "f-`(INT x:A. B x) = (INT x:A. f -` B x)"
  2087   by blast
  2088 
  2089 lemma vimage_Collect_eq [simp]: "f -` Collect P = {y. P (f y)}"
  2090   by blast
  2091 
  2092 lemma vimage_Collect: "(!!x. P (f x) = Q x) ==> f -` (Collect P) = Collect Q"
  2093   by blast
  2094 
  2095 lemma vimage_insert: "f-`(insert a B) = (f-`{a}) Un (f-`B)"
  2096   -- {* NOT suitable for rewriting because of the recurrence of @{term "{a}"}. *}
  2097   by blast
  2098 
  2099 lemma vimage_Diff: "f -` (A - B) = (f -` A) - (f -` B)"
  2100   by blast
  2101 
  2102 lemma vimage_UNIV [simp]: "f -` UNIV = UNIV"
  2103   by blast
  2104 
  2105 lemma vimage_eq_UN: "f-`B = (UN y: B. f-`{y})"
  2106   -- {* NOT suitable for rewriting *}
  2107   by blast
  2108 
  2109 lemma vimage_mono: "A \<subseteq> B ==> f -` A \<subseteq> f -` B"
  2110   -- {* monotonicity *}
  2111   by blast
  2112 
  2113 
  2114 subsection {* Getting the Contents of a Singleton Set *}
  2115 
  2116 definition
  2117   contents :: "'a set \<Rightarrow> 'a"
  2118 where
  2119   [code func del]: "contents X = (THE x. X = {x})"
  2120 
  2121 lemma contents_eq [simp]: "contents {x} = x"
  2122   by (simp add: contents_def)
  2123 
  2124 
  2125 subsection {* Transitivity rules for calculational reasoning *}
  2126 
  2127 lemma set_rev_mp: "x:A ==> A \<subseteq> B ==> x:B"
  2128   by (rule subsetD)
  2129 
  2130 lemma set_mp: "A \<subseteq> B ==> x:A ==> x:B"
  2131   by (rule subsetD)
  2132 
  2133 
  2134 subsection {* Code generation for finite sets *}
  2135 
  2136 code_datatype "{}" insert
  2137 
  2138 
  2139 subsubsection {* Primitive predicates *}
  2140 
  2141 definition
  2142   is_empty :: "'a set \<Rightarrow> bool"
  2143 where
  2144   [code func del]: "is_empty A \<longleftrightarrow> A = {}"
  2145 lemmas [code inline] = is_empty_def [symmetric]
  2146 
  2147 lemma is_empty_insert [code func]:
  2148   "is_empty (insert a A) \<longleftrightarrow> False"
  2149   by (simp add: is_empty_def)
  2150 
  2151 lemma is_empty_empty [code func]:
  2152   "is_empty {} \<longleftrightarrow> True"
  2153   by (simp add: is_empty_def)
  2154 
  2155 lemma Ball_insert [code func]:
  2156   "Ball (insert a A) P \<longleftrightarrow> P a \<and> Ball A P"
  2157   by simp
  2158 
  2159 lemma Ball_empty [code func]:
  2160   "Ball {} P \<longleftrightarrow> True"
  2161   by simp
  2162 
  2163 lemma Bex_insert [code func]:
  2164   "Bex (insert a A) P \<longleftrightarrow> P a \<or> Bex A P"
  2165   by simp
  2166 
  2167 lemma Bex_empty [code func]:
  2168   "Bex {} P \<longleftrightarrow> False"
  2169   by simp
  2170 
  2171 
  2172 subsubsection {* Primitive operations *}
  2173 
  2174 lemma minus_insert [code func]:
  2175   "insert (a\<Colon>'a\<Colon>eq) A - B = (let C = A - B in if a \<in> B then C else insert a C)"
  2176   by (auto simp add: Let_def)
  2177 
  2178 lemma minus_empty1 [code func]:
  2179   "{} - A = {}"
  2180   by simp
  2181 
  2182 lemma minus_empty2 [code func]:
  2183   "A - {} = A"
  2184   by simp
  2185 
  2186 lemma inter_insert [code func]:
  2187   "insert a A \<inter> B = (let C = A \<inter> B in if a \<in> B then insert a C else C)"
  2188   by (auto simp add: Let_def)
  2189 
  2190 lemma inter_empty1 [code func]:
  2191   "{} \<inter> A = {}"
  2192   by simp
  2193 
  2194 lemma inter_empty2 [code func]:
  2195   "A \<inter> {} = {}"
  2196   by simp
  2197 
  2198 lemma union_insert [code func]:
  2199   "insert a A \<union> B = (let C = A \<union> B in if a \<in> B then C else insert a C)"
  2200   by (auto simp add: Let_def)
  2201 
  2202 lemma union_empty1 [code func]:
  2203   "{} \<union> A = A"
  2204   by simp
  2205 
  2206 lemma union_empty2 [code func]:
  2207   "A \<union> {} = A"
  2208   by simp
  2209 
  2210 lemma INTER_insert [code func]:
  2211   "INTER (insert a A) f = f a \<inter> INTER A f"
  2212   by auto
  2213 
  2214 lemma INTER_singleton [code func]:
  2215   "INTER {a} f = f a"
  2216   by auto
  2217 
  2218 lemma UNION_insert [code func]:
  2219   "UNION (insert a A) f = f a \<union> UNION A f"
  2220   by auto
  2221 
  2222 lemma UNION_empty [code func]:
  2223   "UNION {} f = {}"
  2224   by auto
  2225 
  2226 lemma contents_insert [code func]:
  2227   "contents (insert a A) = contents (insert a (A - {a}))"
  2228   by auto
  2229 declare contents_eq [code func]
  2230 
  2231 
  2232 subsubsection {* Derived predicates *}
  2233 
  2234 lemma in_code [code func]:
  2235   "a \<in> A \<longleftrightarrow> (\<exists>x\<in>A. a = x)"
  2236   by simp
  2237 
  2238 instance set :: (eq) eq ..
  2239 
  2240 lemma eq_set_code [code func]:
  2241   fixes A B :: "'a\<Colon>eq set"
  2242   shows "A = B \<longleftrightarrow> A \<subseteq> B \<and> B \<subseteq> A"
  2243   by auto
  2244 
  2245 lemma subset_eq_code [code func]:
  2246   fixes A B :: "'a\<Colon>eq set"
  2247   shows "A \<subseteq> B \<longleftrightarrow> (\<forall>x\<in>A. x \<in> B)"
  2248   by auto
  2249 
  2250 lemma subset_code [code func]:
  2251   fixes A B :: "'a\<Colon>eq set"
  2252   shows "A \<subset> B \<longleftrightarrow> A \<subseteq> B \<and> \<not> B \<subseteq> A"
  2253   by auto
  2254 
  2255 
  2256 subsubsection {* Derived operations *}
  2257 
  2258 lemma image_code [code func]:
  2259   "image f A = UNION A (\<lambda>x. {f x})" by auto
  2260 
  2261 definition
  2262   project :: "('a \<Rightarrow> bool) \<Rightarrow> 'a set \<Rightarrow> 'a set" where
  2263   [code func del, code post]: "project P A = {a\<in>A. P a}"
  2264 
  2265 lemmas [symmetric, code inline] = project_def
  2266 
  2267 lemma project_code [code func]:
  2268   "project P A = UNION A (\<lambda>a. if P a then {a} else {})"
  2269   by (auto simp add: project_def split: if_splits)
  2270 
  2271 lemma Inter_code [code func]:
  2272   "Inter A = INTER A (\<lambda>x. x)"
  2273   by auto
  2274 
  2275 lemma Union_code [code func]:
  2276   "Union A = UNION A (\<lambda>x. x)"
  2277   by auto
  2278 
  2279 code_reserved SML union inter (* Avoid clashes with ML infixes *)
  2280 
  2281 subsection {* Basic ML bindings *}
  2282 
  2283 ML {*
  2284 val Ball_def = @{thm Ball_def}
  2285 val Bex_def = @{thm Bex_def}
  2286 val CollectD = @{thm CollectD}
  2287 val CollectE = @{thm CollectE}
  2288 val CollectI = @{thm CollectI}
  2289 val Collect_conj_eq = @{thm Collect_conj_eq}
  2290 val Collect_mem_eq = @{thm Collect_mem_eq}
  2291 val IntD1 = @{thm IntD1}
  2292 val IntD2 = @{thm IntD2}
  2293 val IntE = @{thm IntE}
  2294 val IntI = @{thm IntI}
  2295 val Int_Collect = @{thm Int_Collect}
  2296 val UNIV_I = @{thm UNIV_I}
  2297 val UNIV_witness = @{thm UNIV_witness}
  2298 val UnE = @{thm UnE}
  2299 val UnI1 = @{thm UnI1}
  2300 val UnI2 = @{thm UnI2}
  2301 val ballE = @{thm ballE}
  2302 val ballI = @{thm ballI}
  2303 val bexCI = @{thm bexCI}
  2304 val bexE = @{thm bexE}
  2305 val bexI = @{thm bexI}
  2306 val bex_triv = @{thm bex_triv}
  2307 val bspec = @{thm bspec}
  2308 val contra_subsetD = @{thm contra_subsetD}
  2309 val distinct_lemma = @{thm distinct_lemma}
  2310 val eq_to_mono = @{thm eq_to_mono}
  2311 val eq_to_mono2 = @{thm eq_to_mono2}
  2312 val equalityCE = @{thm equalityCE}
  2313 val equalityD1 = @{thm equalityD1}
  2314 val equalityD2 = @{thm equalityD2}
  2315 val equalityE = @{thm equalityE}
  2316 val equalityI = @{thm equalityI}
  2317 val imageE = @{thm imageE}
  2318 val imageI = @{thm imageI}
  2319 val image_Un = @{thm image_Un}
  2320 val image_insert = @{thm image_insert}
  2321 val insert_commute = @{thm insert_commute}
  2322 val insert_iff = @{thm insert_iff}
  2323 val mem_Collect_eq = @{thm mem_Collect_eq}
  2324 val rangeE = @{thm rangeE}
  2325 val rangeI = @{thm rangeI}
  2326 val range_eqI = @{thm range_eqI}
  2327 val subsetCE = @{thm subsetCE}
  2328 val subsetD = @{thm subsetD}
  2329 val subsetI = @{thm subsetI}
  2330 val subset_refl = @{thm subset_refl}
  2331 val subset_trans = @{thm subset_trans}
  2332 val vimageD = @{thm vimageD}
  2333 val vimageE = @{thm vimageE}
  2334 val vimageI = @{thm vimageI}
  2335 val vimageI2 = @{thm vimageI2}
  2336 val vimage_Collect = @{thm vimage_Collect}
  2337 val vimage_Int = @{thm vimage_Int}
  2338 val vimage_Un = @{thm vimage_Un}
  2339 *}
  2340 
  2341 end