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