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