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