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