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