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