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