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