src/HOL/Set.thy
author nipkow
Mon Aug 16 14:22:27 2004 +0200 (2004-08-16)
changeset 15131 c69542757a4d
parent 15120 f0359f75682e
child 15140 322485b816ac
permissions -rw-r--r--
New theory header syntax.
     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 import HOL
    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: includes mono shows "f A \<union> f B \<subseteq> f (A \<union> B)"
  1090   apply (rule Un_least)
  1091    apply (rule Un_upper1 [THEN mono])
  1092   apply (rule Un_upper2 [THEN mono])
  1093   done
  1094 
  1095 lemma mono_Int: includes mono shows "f (A \<inter> B) \<subseteq> f A \<inter> f B"
  1096   apply (rule Int_greatest)
  1097    apply (rule Int_lower1 [THEN mono])
  1098   apply (rule Int_lower2 [THEN mono])
  1099   done
  1100 
  1101 
  1102 subsubsection {* Equalities involving union, intersection, inclusion, etc. *}
  1103 
  1104 text {* @{text "{}"}. *}
  1105 
  1106 lemma Collect_const [simp]: "{s. P} = (if P then UNIV else {})"
  1107   -- {* supersedes @{text "Collect_False_empty"} *}
  1108   by auto
  1109 
  1110 lemma subset_empty [simp]: "(A \<subseteq> {}) = (A = {})"
  1111   by blast
  1112 
  1113 lemma not_psubset_empty [iff]: "\<not> (A < {})"
  1114   by (unfold psubset_def) blast
  1115 
  1116 lemma Collect_empty_eq [simp]: "(Collect P = {}) = (\<forall>x. \<not> P x)"
  1117   by auto
  1118 
  1119 lemma Collect_neg_eq: "{x. \<not> P x} = - {x. P x}"
  1120   by blast
  1121 
  1122 lemma Collect_disj_eq: "{x. P x | Q x} = {x. P x} \<union> {x. Q x}"
  1123   by blast
  1124 
  1125 lemma Collect_imp_eq: "{x. P x --> Q x} = -{x. P x} \<union> {x. Q x}"
  1126   by blast
  1127 
  1128 lemma Collect_conj_eq: "{x. P x & Q x} = {x. P x} \<inter> {x. Q x}"
  1129   by blast
  1130 
  1131 lemma Collect_all_eq: "{x. \<forall>y. P x y} = (\<Inter>y. {x. P x y})"
  1132   by blast
  1133 
  1134 lemma Collect_ball_eq: "{x. \<forall>y\<in>A. P x y} = (\<Inter>y\<in>A. {x. P x y})"
  1135   by blast
  1136 
  1137 lemma Collect_ex_eq: "{x. \<exists>y. P x y} = (\<Union>y. {x. P x y})"
  1138   by blast
  1139 
  1140 lemma Collect_bex_eq: "{x. \<exists>y\<in>A. P x y} = (\<Union>y\<in>A. {x. P x y})"
  1141   by blast
  1142 
  1143 
  1144 text {* \medskip @{text insert}. *}
  1145 
  1146 lemma insert_is_Un: "insert a A = {a} Un A"
  1147   -- {* NOT SUITABLE FOR REWRITING since @{text "{a} == insert a {}"} *}
  1148   by blast
  1149 
  1150 lemma insert_not_empty [simp]: "insert a A \<noteq> {}"
  1151   by blast
  1152 
  1153 lemmas empty_not_insert [simp] = insert_not_empty [symmetric, standard]
  1154 
  1155 lemma insert_absorb: "a \<in> A ==> insert a A = A"
  1156   -- {* @{text "[simp]"} causes recursive calls when there are nested inserts *}
  1157   -- {* with \emph{quadratic} running time *}
  1158   by blast
  1159 
  1160 lemma insert_absorb2 [simp]: "insert x (insert x A) = insert x A"
  1161   by blast
  1162 
  1163 lemma insert_commute: "insert x (insert y A) = insert y (insert x A)"
  1164   by blast
  1165 
  1166 lemma insert_subset [simp]: "(insert x A \<subseteq> B) = (x \<in> B & A \<subseteq> B)"
  1167   by blast
  1168 
  1169 lemma mk_disjoint_insert: "a \<in> A ==> \<exists>B. A = insert a B & a \<notin> B"
  1170   -- {* use new @{text B} rather than @{text "A - {a}"} to avoid infinite unfolding *}
  1171   apply (rule_tac x = "A - {a}" in exI, blast)
  1172   done
  1173 
  1174 lemma insert_Collect: "insert a (Collect P) = {u. u \<noteq> a --> P u}"
  1175   by auto
  1176 
  1177 lemma UN_insert_distrib: "u \<in> A ==> (\<Union>x\<in>A. insert a (B x)) = insert a (\<Union>x\<in>A. B x)"
  1178   by blast
  1179 
  1180 lemma insert_inter_insert[simp]: "insert a A \<inter> insert a B = insert a (A \<inter> B)"
  1181   by blast
  1182 
  1183 lemma insert_disjoint[simp]:
  1184  "(insert a A \<inter> B = {}) = (a \<notin> B \<and> A \<inter> B = {})"
  1185  "({} = insert a A \<inter> B) = (a \<notin> B \<and> {} = A \<inter> B)"
  1186 by auto
  1187 
  1188 lemma disjoint_insert[simp]:
  1189  "(B \<inter> insert a A = {}) = (a \<notin> B \<and> B \<inter> A = {})"
  1190  "({} = A \<inter> insert b B) = (b \<notin> A \<and> {} = A \<inter> B)"
  1191 by auto
  1192 
  1193 text {* \medskip @{text image}. *}
  1194 
  1195 lemma image_empty [simp]: "f`{} = {}"
  1196   by blast
  1197 
  1198 lemma image_insert [simp]: "f ` insert a B = insert (f a) (f`B)"
  1199   by blast
  1200 
  1201 lemma image_constant: "x \<in> A ==> (\<lambda>x. c) ` A = {c}"
  1202   by blast
  1203 
  1204 lemma image_image: "f ` (g ` A) = (\<lambda>x. f (g x)) ` A"
  1205   by blast
  1206 
  1207 lemma insert_image [simp]: "x \<in> A ==> insert (f x) (f`A) = f`A"
  1208   by blast
  1209 
  1210 lemma image_is_empty [iff]: "(f`A = {}) = (A = {})"
  1211   by blast
  1212 
  1213 lemma image_Collect: "f ` {x. P x} = {f x | x. P x}"
  1214   -- {* NOT suitable as a default simprule: the RHS isn't simpler than the LHS, *}
  1215   -- {* with its implicit quantifier and conjunction.  Also image enjoys better *}
  1216   -- {* equational properties than does the RHS. *}
  1217   by blast
  1218 
  1219 lemma if_image_distrib [simp]:
  1220   "(\<lambda>x. if P x then f x else g x) ` S
  1221     = (f ` (S \<inter> {x. P x})) \<union> (g ` (S \<inter> {x. \<not> P x}))"
  1222   by (auto simp add: image_def)
  1223 
  1224 lemma image_cong: "M = N ==> (!!x. x \<in> N ==> f x = g x) ==> f`M = g`N"
  1225   by (simp add: image_def)
  1226 
  1227 
  1228 text {* \medskip @{text range}. *}
  1229 
  1230 lemma full_SetCompr_eq: "{u. \<exists>x. u = f x} = range f"
  1231   by auto
  1232 
  1233 lemma range_composition [simp]: "range (\<lambda>x. f (g x)) = f`range g"
  1234 by (subst image_image, simp)
  1235 
  1236 
  1237 text {* \medskip @{text Int} *}
  1238 
  1239 lemma Int_absorb [simp]: "A \<inter> A = A"
  1240   by blast
  1241 
  1242 lemma Int_left_absorb: "A \<inter> (A \<inter> B) = A \<inter> B"
  1243   by blast
  1244 
  1245 lemma Int_commute: "A \<inter> B = B \<inter> A"
  1246   by blast
  1247 
  1248 lemma Int_left_commute: "A \<inter> (B \<inter> C) = B \<inter> (A \<inter> C)"
  1249   by blast
  1250 
  1251 lemma Int_assoc: "(A \<inter> B) \<inter> C = A \<inter> (B \<inter> C)"
  1252   by blast
  1253 
  1254 lemmas Int_ac = Int_assoc Int_left_absorb Int_commute Int_left_commute
  1255   -- {* Intersection is an AC-operator *}
  1256 
  1257 lemma Int_absorb1: "B \<subseteq> A ==> A \<inter> B = B"
  1258   by blast
  1259 
  1260 lemma Int_absorb2: "A \<subseteq> B ==> A \<inter> B = A"
  1261   by blast
  1262 
  1263 lemma Int_empty_left [simp]: "{} \<inter> B = {}"
  1264   by blast
  1265 
  1266 lemma Int_empty_right [simp]: "A \<inter> {} = {}"
  1267   by blast
  1268 
  1269 lemma disjoint_eq_subset_Compl: "(A \<inter> B = {}) = (A \<subseteq> -B)"
  1270   by blast
  1271 
  1272 lemma disjoint_iff_not_equal: "(A \<inter> B = {}) = (\<forall>x\<in>A. \<forall>y\<in>B. x \<noteq> y)"
  1273   by blast
  1274 
  1275 lemma Int_UNIV_left [simp]: "UNIV \<inter> B = B"
  1276   by blast
  1277 
  1278 lemma Int_UNIV_right [simp]: "A \<inter> UNIV = A"
  1279   by blast
  1280 
  1281 lemma Int_eq_Inter: "A \<inter> B = \<Inter>{A, B}"
  1282   by blast
  1283 
  1284 lemma Int_Un_distrib: "A \<inter> (B \<union> C) = (A \<inter> B) \<union> (A \<inter> C)"
  1285   by blast
  1286 
  1287 lemma Int_Un_distrib2: "(B \<union> C) \<inter> A = (B \<inter> A) \<union> (C \<inter> A)"
  1288   by blast
  1289 
  1290 lemma Int_UNIV [simp]: "(A \<inter> B = UNIV) = (A = UNIV & B = UNIV)"
  1291   by blast
  1292 
  1293 lemma Int_subset_iff [simp]: "(C \<subseteq> A \<inter> B) = (C \<subseteq> A & C \<subseteq> B)"
  1294   by blast
  1295 
  1296 lemma Int_Collect: "(x \<in> A \<inter> {x. P x}) = (x \<in> A & P x)"
  1297   by blast
  1298 
  1299 
  1300 text {* \medskip @{text Un}. *}
  1301 
  1302 lemma Un_absorb [simp]: "A \<union> A = A"
  1303   by blast
  1304 
  1305 lemma Un_left_absorb: "A \<union> (A \<union> B) = A \<union> B"
  1306   by blast
  1307 
  1308 lemma Un_commute: "A \<union> B = B \<union> A"
  1309   by blast
  1310 
  1311 lemma Un_left_commute: "A \<union> (B \<union> C) = B \<union> (A \<union> C)"
  1312   by blast
  1313 
  1314 lemma Un_assoc: "(A \<union> B) \<union> C = A \<union> (B \<union> C)"
  1315   by blast
  1316 
  1317 lemmas Un_ac = Un_assoc Un_left_absorb Un_commute Un_left_commute
  1318   -- {* Union is an AC-operator *}
  1319 
  1320 lemma Un_absorb1: "A \<subseteq> B ==> A \<union> B = B"
  1321   by blast
  1322 
  1323 lemma Un_absorb2: "B \<subseteq> A ==> A \<union> B = A"
  1324   by blast
  1325 
  1326 lemma Un_empty_left [simp]: "{} \<union> B = B"
  1327   by blast
  1328 
  1329 lemma Un_empty_right [simp]: "A \<union> {} = A"
  1330   by blast
  1331 
  1332 lemma Un_UNIV_left [simp]: "UNIV \<union> B = UNIV"
  1333   by blast
  1334 
  1335 lemma Un_UNIV_right [simp]: "A \<union> UNIV = UNIV"
  1336   by blast
  1337 
  1338 lemma Un_eq_Union: "A \<union> B = \<Union>{A, B}"
  1339   by blast
  1340 
  1341 lemma Un_insert_left [simp]: "(insert a B) \<union> C = insert a (B \<union> C)"
  1342   by blast
  1343 
  1344 lemma Un_insert_right [simp]: "A \<union> (insert a B) = insert a (A \<union> B)"
  1345   by blast
  1346 
  1347 lemma Int_insert_left:
  1348     "(insert a B) Int C = (if a \<in> C then insert a (B \<inter> C) else B \<inter> C)"
  1349   by auto
  1350 
  1351 lemma Int_insert_right:
  1352     "A \<inter> (insert a B) = (if a \<in> A then insert a (A \<inter> B) else A \<inter> B)"
  1353   by auto
  1354 
  1355 lemma Un_Int_distrib: "A \<union> (B \<inter> C) = (A \<union> B) \<inter> (A \<union> C)"
  1356   by blast
  1357 
  1358 lemma Un_Int_distrib2: "(B \<inter> C) \<union> A = (B \<union> A) \<inter> (C \<union> A)"
  1359   by blast
  1360 
  1361 lemma Un_Int_crazy:
  1362     "(A \<inter> B) \<union> (B \<inter> C) \<union> (C \<inter> A) = (A \<union> B) \<inter> (B \<union> C) \<inter> (C \<union> A)"
  1363   by blast
  1364 
  1365 lemma subset_Un_eq: "(A \<subseteq> B) = (A \<union> B = B)"
  1366   by blast
  1367 
  1368 lemma Un_empty [iff]: "(A \<union> B = {}) = (A = {} & B = {})"
  1369   by blast
  1370 
  1371 lemma Un_subset_iff [simp]: "(A \<union> B \<subseteq> C) = (A \<subseteq> C & B \<subseteq> C)"
  1372   by blast
  1373 
  1374 lemma Un_Diff_Int: "(A - B) \<union> (A \<inter> B) = A"
  1375   by blast
  1376 
  1377 
  1378 text {* \medskip Set complement *}
  1379 
  1380 lemma Compl_disjoint [simp]: "A \<inter> -A = {}"
  1381   by blast
  1382 
  1383 lemma Compl_disjoint2 [simp]: "-A \<inter> A = {}"
  1384   by blast
  1385 
  1386 lemma Compl_partition: "A \<union> -A = UNIV"
  1387   by blast
  1388 
  1389 lemma Compl_partition2: "-A \<union> A = UNIV"
  1390   by blast
  1391 
  1392 lemma double_complement [simp]: "- (-A) = (A::'a set)"
  1393   by blast
  1394 
  1395 lemma Compl_Un [simp]: "-(A \<union> B) = (-A) \<inter> (-B)"
  1396   by blast
  1397 
  1398 lemma Compl_Int [simp]: "-(A \<inter> B) = (-A) \<union> (-B)"
  1399   by blast
  1400 
  1401 lemma Compl_UN [simp]: "-(\<Union>x\<in>A. B x) = (\<Inter>x\<in>A. -B x)"
  1402   by blast
  1403 
  1404 lemma Compl_INT [simp]: "-(\<Inter>x\<in>A. B x) = (\<Union>x\<in>A. -B x)"
  1405   by blast
  1406 
  1407 lemma subset_Compl_self_eq: "(A \<subseteq> -A) = (A = {})"
  1408   by blast
  1409 
  1410 lemma Un_Int_assoc_eq: "((A \<inter> B) \<union> C = A \<inter> (B \<union> C)) = (C \<subseteq> A)"
  1411   -- {* Halmos, Naive Set Theory, page 16. *}
  1412   by blast
  1413 
  1414 lemma Compl_UNIV_eq [simp]: "-UNIV = {}"
  1415   by blast
  1416 
  1417 lemma Compl_empty_eq [simp]: "-{} = UNIV"
  1418   by blast
  1419 
  1420 lemma Compl_subset_Compl_iff [iff]: "(-A \<subseteq> -B) = (B \<subseteq> A)"
  1421   by blast
  1422 
  1423 lemma Compl_eq_Compl_iff [iff]: "(-A = -B) = (A = (B::'a set))"
  1424   by blast
  1425 
  1426 
  1427 text {* \medskip @{text Union}. *}
  1428 
  1429 lemma Union_empty [simp]: "Union({}) = {}"
  1430   by blast
  1431 
  1432 lemma Union_UNIV [simp]: "Union UNIV = UNIV"
  1433   by blast
  1434 
  1435 lemma Union_insert [simp]: "Union (insert a B) = a \<union> \<Union>B"
  1436   by blast
  1437 
  1438 lemma Union_Un_distrib [simp]: "\<Union>(A Un B) = \<Union>A \<union> \<Union>B"
  1439   by blast
  1440 
  1441 lemma Union_Int_subset: "\<Union>(A \<inter> B) \<subseteq> \<Union>A \<inter> \<Union>B"
  1442   by blast
  1443 
  1444 lemma Union_empty_conv [iff]: "(\<Union>A = {}) = (\<forall>x\<in>A. x = {})"
  1445   by blast
  1446 
  1447 lemma empty_Union_conv [iff]: "({} = \<Union>A) = (\<forall>x\<in>A. x = {})"
  1448   by blast
  1449 
  1450 lemma Union_disjoint: "(\<Union>C \<inter> A = {}) = (\<forall>B\<in>C. B \<inter> A = {})"
  1451   by blast
  1452 
  1453 
  1454 text {* \medskip @{text Inter}. *}
  1455 
  1456 lemma Inter_empty [simp]: "\<Inter>{} = UNIV"
  1457   by blast
  1458 
  1459 lemma Inter_UNIV [simp]: "\<Inter>UNIV = {}"
  1460   by blast
  1461 
  1462 lemma Inter_insert [simp]: "\<Inter>(insert a B) = a \<inter> \<Inter>B"
  1463   by blast
  1464 
  1465 lemma Inter_Un_subset: "\<Inter>A \<union> \<Inter>B \<subseteq> \<Inter>(A \<inter> B)"
  1466   by blast
  1467 
  1468 lemma Inter_Un_distrib: "\<Inter>(A \<union> B) = \<Inter>A \<inter> \<Inter>B"
  1469   by blast
  1470 
  1471 lemma Inter_UNIV_conv [iff]:
  1472   "(\<Inter>A = UNIV) = (\<forall>x\<in>A. x = UNIV)"
  1473   "(UNIV = \<Inter>A) = (\<forall>x\<in>A. x = UNIV)"
  1474   by blast+
  1475 
  1476 
  1477 text {*
  1478   \medskip @{text UN} and @{text INT}.
  1479 
  1480   Basic identities: *}
  1481 
  1482 lemma UN_empty [simp]: "(\<Union>x\<in>{}. B x) = {}"
  1483   by blast
  1484 
  1485 lemma UN_empty2 [simp]: "(\<Union>x\<in>A. {}) = {}"
  1486   by blast
  1487 
  1488 lemma UN_singleton [simp]: "(\<Union>x\<in>A. {x}) = A"
  1489   by blast
  1490 
  1491 lemma UN_absorb: "k \<in> I ==> A k \<union> (\<Union>i\<in>I. A i) = (\<Union>i\<in>I. A i)"
  1492   by auto
  1493 
  1494 lemma INT_empty [simp]: "(\<Inter>x\<in>{}. B x) = UNIV"
  1495   by blast
  1496 
  1497 lemma INT_absorb: "k \<in> I ==> A k \<inter> (\<Inter>i\<in>I. A i) = (\<Inter>i\<in>I. A i)"
  1498   by blast
  1499 
  1500 lemma UN_insert [simp]: "(\<Union>x\<in>insert a A. B x) = B a \<union> UNION A B"
  1501   by blast
  1502 
  1503 lemma UN_Un: "(\<Union>i \<in> A \<union> B. M i) = (\<Union>i\<in>A. M i) \<union> (\<Union>i\<in>B. M i)"
  1504   by blast
  1505 
  1506 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)"
  1507   by blast
  1508 
  1509 lemma UN_subset_iff: "((\<Union>i\<in>I. A i) \<subseteq> B) = (\<forall>i\<in>I. A i \<subseteq> B)"
  1510   by blast
  1511 
  1512 lemma INT_subset_iff: "(B \<subseteq> (\<Inter>i\<in>I. A i)) = (\<forall>i\<in>I. B \<subseteq> A i)"
  1513   by blast
  1514 
  1515 lemma INT_insert [simp]: "(\<Inter>x \<in> insert a A. B x) = B a \<inter> INTER A B"
  1516   by blast
  1517 
  1518 lemma INT_Un: "(\<Inter>i \<in> A \<union> B. M i) = (\<Inter>i \<in> A. M i) \<inter> (\<Inter>i\<in>B. M i)"
  1519   by blast
  1520 
  1521 lemma INT_insert_distrib:
  1522     "u \<in> A ==> (\<Inter>x\<in>A. insert a (B x)) = insert a (\<Inter>x\<in>A. B x)"
  1523   by blast
  1524 
  1525 lemma Union_image_eq [simp]: "\<Union>(B`A) = (\<Union>x\<in>A. B x)"
  1526   by blast
  1527 
  1528 lemma image_Union: "f ` \<Union>S = (\<Union>x\<in>S. f ` x)"
  1529   by blast
  1530 
  1531 lemma Inter_image_eq [simp]: "\<Inter>(B`A) = (\<Inter>x\<in>A. B x)"
  1532   by blast
  1533 
  1534 lemma UN_constant [simp]: "(\<Union>y\<in>A. c) = (if A = {} then {} else c)"
  1535   by auto
  1536 
  1537 lemma INT_constant [simp]: "(\<Inter>y\<in>A. c) = (if A = {} then UNIV else c)"
  1538   by auto
  1539 
  1540 lemma UN_eq: "(\<Union>x\<in>A. B x) = \<Union>({Y. \<exists>x\<in>A. Y = B x})"
  1541   by blast
  1542 
  1543 lemma INT_eq: "(\<Inter>x\<in>A. B x) = \<Inter>({Y. \<exists>x\<in>A. Y = B x})"
  1544   -- {* Look: it has an \emph{existential} quantifier *}
  1545   by blast
  1546 
  1547 lemma UNION_empty_conv[iff]:
  1548   "({} = (UN x:A. B x)) = (\<forall>x\<in>A. B x = {})"
  1549   "((UN x:A. B x) = {}) = (\<forall>x\<in>A. B x = {})"
  1550 by blast+
  1551 
  1552 lemma INTER_UNIV_conv[iff]:
  1553  "(UNIV = (INT x:A. B x)) = (\<forall>x\<in>A. B x = UNIV)"
  1554  "((INT x:A. B x) = UNIV) = (\<forall>x\<in>A. B x = UNIV)"
  1555 by blast+
  1556 
  1557 
  1558 text {* \medskip Distributive laws: *}
  1559 
  1560 lemma Int_Union: "A \<inter> \<Union>B = (\<Union>C\<in>B. A \<inter> C)"
  1561   by blast
  1562 
  1563 lemma Int_Union2: "\<Union>B \<inter> A = (\<Union>C\<in>B. C \<inter> A)"
  1564   by blast
  1565 
  1566 lemma Un_Union_image: "(\<Union>x\<in>C. A x \<union> B x) = \<Union>(A`C) \<union> \<Union>(B`C)"
  1567   -- {* Devlin, Fundamentals of Contemporary Set Theory, page 12, exercise 5: *}
  1568   -- {* Union of a family of unions *}
  1569   by blast
  1570 
  1571 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)"
  1572   -- {* Equivalent version *}
  1573   by blast
  1574 
  1575 lemma Un_Inter: "A \<union> \<Inter>B = (\<Inter>C\<in>B. A \<union> C)"
  1576   by blast
  1577 
  1578 lemma Int_Inter_image: "(\<Inter>x\<in>C. A x \<inter> B x) = \<Inter>(A`C) \<inter> \<Inter>(B`C)"
  1579   by blast
  1580 
  1581 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)"
  1582   -- {* Equivalent version *}
  1583   by blast
  1584 
  1585 lemma Int_UN_distrib: "B \<inter> (\<Union>i\<in>I. A i) = (\<Union>i\<in>I. B \<inter> A i)"
  1586   -- {* Halmos, Naive Set Theory, page 35. *}
  1587   by blast
  1588 
  1589 lemma Un_INT_distrib: "B \<union> (\<Inter>i\<in>I. A i) = (\<Inter>i\<in>I. B \<union> A i)"
  1590   by blast
  1591 
  1592 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)"
  1593   by blast
  1594 
  1595 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)"
  1596   by blast
  1597 
  1598 
  1599 text {* \medskip Bounded quantifiers.
  1600 
  1601   The following are not added to the default simpset because
  1602   (a) they duplicate the body and (b) there are no similar rules for @{text Int}. *}
  1603 
  1604 lemma ball_Un: "(\<forall>x \<in> A \<union> B. P x) = ((\<forall>x\<in>A. P x) & (\<forall>x\<in>B. P x))"
  1605   by blast
  1606 
  1607 lemma bex_Un: "(\<exists>x \<in> A \<union> B. P x) = ((\<exists>x\<in>A. P x) | (\<exists>x\<in>B. P x))"
  1608   by blast
  1609 
  1610 lemma ball_UN: "(\<forall>z \<in> UNION A B. P z) = (\<forall>x\<in>A. \<forall>z \<in> B x. P z)"
  1611   by blast
  1612 
  1613 lemma bex_UN: "(\<exists>z \<in> UNION A B. P z) = (\<exists>x\<in>A. \<exists>z\<in>B x. P z)"
  1614   by blast
  1615 
  1616 
  1617 text {* \medskip Set difference. *}
  1618 
  1619 lemma Diff_eq: "A - B = A \<inter> (-B)"
  1620   by blast
  1621 
  1622 lemma Diff_eq_empty_iff [simp]: "(A - B = {}) = (A \<subseteq> B)"
  1623   by blast
  1624 
  1625 lemma Diff_cancel [simp]: "A - A = {}"
  1626   by blast
  1627 
  1628 lemma Diff_idemp [simp]: "(A - B) - B = A - (B::'a set)"
  1629 by blast
  1630 
  1631 lemma Diff_triv: "A \<inter> B = {} ==> A - B = A"
  1632   by (blast elim: equalityE)
  1633 
  1634 lemma empty_Diff [simp]: "{} - A = {}"
  1635   by blast
  1636 
  1637 lemma Diff_empty [simp]: "A - {} = A"
  1638   by blast
  1639 
  1640 lemma Diff_UNIV [simp]: "A - UNIV = {}"
  1641   by blast
  1642 
  1643 lemma Diff_insert0 [simp]: "x \<notin> A ==> A - insert x B = A - B"
  1644   by blast
  1645 
  1646 lemma Diff_insert: "A - insert a B = A - B - {a}"
  1647   -- {* NOT SUITABLE FOR REWRITING since @{text "{a} == insert a 0"} *}
  1648   by blast
  1649 
  1650 lemma Diff_insert2: "A - insert a B = A - {a} - B"
  1651   -- {* NOT SUITABLE FOR REWRITING since @{text "{a} == insert a 0"} *}
  1652   by blast
  1653 
  1654 lemma insert_Diff_if: "insert x A - B = (if x \<in> B then A - B else insert x (A - B))"
  1655   by auto
  1656 
  1657 lemma insert_Diff1 [simp]: "x \<in> B ==> insert x A - B = A - B"
  1658   by blast
  1659 
  1660 lemma insert_Diff_single[simp]: "insert a (A - {a}) = insert a A"
  1661 by blast
  1662 
  1663 lemma insert_Diff: "a \<in> A ==> insert a (A - {a}) = A"
  1664   by blast
  1665 
  1666 lemma Diff_insert_absorb: "x \<notin> A ==> (insert x A) - {x} = A"
  1667   by auto
  1668 
  1669 lemma Diff_disjoint [simp]: "A \<inter> (B - A) = {}"
  1670   by blast
  1671 
  1672 lemma Diff_partition: "A \<subseteq> B ==> A \<union> (B - A) = B"
  1673   by blast
  1674 
  1675 lemma double_diff: "A \<subseteq> B ==> B \<subseteq> C ==> B - (C - A) = A"
  1676   by blast
  1677 
  1678 lemma Un_Diff_cancel [simp]: "A \<union> (B - A) = A \<union> B"
  1679   by blast
  1680 
  1681 lemma Un_Diff_cancel2 [simp]: "(B - A) \<union> A = B \<union> A"
  1682   by blast
  1683 
  1684 lemma Diff_Un: "A - (B \<union> C) = (A - B) \<inter> (A - C)"
  1685   by blast
  1686 
  1687 lemma Diff_Int: "A - (B \<inter> C) = (A - B) \<union> (A - C)"
  1688   by blast
  1689 
  1690 lemma Un_Diff: "(A \<union> B) - C = (A - C) \<union> (B - C)"
  1691   by blast
  1692 
  1693 lemma Int_Diff: "(A \<inter> B) - C = A \<inter> (B - C)"
  1694   by blast
  1695 
  1696 lemma Diff_Int_distrib: "C \<inter> (A - B) = (C \<inter> A) - (C \<inter> B)"
  1697   by blast
  1698 
  1699 lemma Diff_Int_distrib2: "(A - B) \<inter> C = (A \<inter> C) - (B \<inter> C)"
  1700   by blast
  1701 
  1702 lemma Diff_Compl [simp]: "A - (- B) = A \<inter> B"
  1703   by auto
  1704 
  1705 lemma Compl_Diff_eq [simp]: "- (A - B) = -A \<union> B"
  1706   by blast
  1707 
  1708 
  1709 text {* \medskip Quantification over type @{typ bool}. *}
  1710 
  1711 lemma all_bool_eq: "(\<forall>b::bool. P b) = (P True & P False)"
  1712   apply auto
  1713   apply (tactic {* case_tac "b" 1 *}, auto)
  1714   done
  1715 
  1716 lemma bool_induct: "P True \<Longrightarrow> P False \<Longrightarrow> P x"
  1717   by (rule conjI [THEN all_bool_eq [THEN iffD2], THEN spec])
  1718 
  1719 lemma ex_bool_eq: "(\<exists>b::bool. P b) = (P True | P False)"
  1720   apply auto
  1721   apply (tactic {* case_tac "b" 1 *}, auto)
  1722   done
  1723 
  1724 lemma Un_eq_UN: "A \<union> B = (\<Union>b. if b then A else B)"
  1725   by (auto simp add: split_if_mem2)
  1726 
  1727 lemma UN_bool_eq: "(\<Union>b::bool. A b) = (A True \<union> A False)"
  1728   apply auto
  1729   apply (tactic {* case_tac "b" 1 *}, auto)
  1730   done
  1731 
  1732 lemma INT_bool_eq: "(\<Inter>b::bool. A b) = (A True \<inter> A False)"
  1733   apply auto
  1734   apply (tactic {* case_tac "b" 1 *}, auto)
  1735   done
  1736 
  1737 
  1738 text {* \medskip @{text Pow} *}
  1739 
  1740 lemma Pow_empty [simp]: "Pow {} = {{}}"
  1741   by (auto simp add: Pow_def)
  1742 
  1743 lemma Pow_insert: "Pow (insert a A) = Pow A \<union> (insert a ` Pow A)"
  1744   by (blast intro: image_eqI [where ?x = "u - {a}", standard])
  1745 
  1746 lemma Pow_Compl: "Pow (- A) = {-B | B. A \<in> Pow B}"
  1747   by (blast intro: exI [where ?x = "- u", standard])
  1748 
  1749 lemma Pow_UNIV [simp]: "Pow UNIV = UNIV"
  1750   by blast
  1751 
  1752 lemma Un_Pow_subset: "Pow A \<union> Pow B \<subseteq> Pow (A \<union> B)"
  1753   by blast
  1754 
  1755 lemma UN_Pow_subset: "(\<Union>x\<in>A. Pow (B x)) \<subseteq> Pow (\<Union>x\<in>A. B x)"
  1756   by blast
  1757 
  1758 lemma subset_Pow_Union: "A \<subseteq> Pow (\<Union>A)"
  1759   by blast
  1760 
  1761 lemma Union_Pow_eq [simp]: "\<Union>(Pow A) = A"
  1762   by blast
  1763 
  1764 lemma Pow_Int_eq [simp]: "Pow (A \<inter> B) = Pow A \<inter> Pow B"
  1765   by blast
  1766 
  1767 lemma Pow_INT_eq: "Pow (\<Inter>x\<in>A. B x) = (\<Inter>x\<in>A. Pow (B x))"
  1768   by blast
  1769 
  1770 
  1771 text {* \medskip Miscellany. *}
  1772 
  1773 lemma set_eq_subset: "(A = B) = (A \<subseteq> B & B \<subseteq> A)"
  1774   by blast
  1775 
  1776 lemma subset_iff: "(A \<subseteq> B) = (\<forall>t. t \<in> A --> t \<in> B)"
  1777   by blast
  1778 
  1779 lemma subset_iff_psubset_eq: "(A \<subseteq> B) = ((A \<subset> B) | (A = B))"
  1780   by (unfold psubset_def) blast
  1781 
  1782 lemma all_not_in_conv [iff]: "(\<forall>x. x \<notin> A) = (A = {})"
  1783   by blast
  1784 
  1785 lemma ex_in_conv: "(\<exists>x. x \<in> A) = (A \<noteq> {})"
  1786   by blast
  1787 
  1788 lemma distinct_lemma: "f x \<noteq> f y ==> x \<noteq> y"
  1789   by rules
  1790 
  1791 
  1792 text {* \medskip Miniscoping: pushing in quantifiers and big Unions
  1793            and Intersections. *}
  1794 
  1795 lemma UN_simps [simp]:
  1796   "!!a B C. (UN x:C. insert a (B x)) = (if C={} then {} else insert a (UN x:C. B x))"
  1797   "!!A B C. (UN x:C. A x Un B)   = ((if C={} then {} else (UN x:C. A x) Un B))"
  1798   "!!A B C. (UN x:C. A Un B x)   = ((if C={} then {} else A Un (UN x:C. B x)))"
  1799   "!!A B C. (UN x:C. A x Int B)  = ((UN x:C. A x) Int B)"
  1800   "!!A B C. (UN x:C. A Int B x)  = (A Int (UN x:C. B x))"
  1801   "!!A B C. (UN x:C. A x - B)    = ((UN x:C. A x) - B)"
  1802   "!!A B C. (UN x:C. A - B x)    = (A - (INT x:C. B x))"
  1803   "!!A B. (UN x: Union A. B x) = (UN y:A. UN x:y. B x)"
  1804   "!!A B C. (UN z: UNION A B. C z) = (UN  x:A. UN z: B(x). C z)"
  1805   "!!A B f. (UN x:f`A. B x)     = (UN a:A. B (f a))"
  1806   by auto
  1807 
  1808 lemma INT_simps [simp]:
  1809   "!!A B C. (INT x:C. A x Int B) = (if C={} then UNIV else (INT x:C. A x) Int B)"
  1810   "!!A B C. (INT x:C. A Int B x) = (if C={} then UNIV else A Int (INT x:C. B x))"
  1811   "!!A B C. (INT x:C. A x - B)   = (if C={} then UNIV else (INT x:C. A x) - B)"
  1812   "!!A B C. (INT x:C. A - B x)   = (if C={} then UNIV else A - (UN x:C. B x))"
  1813   "!!a B C. (INT x:C. insert a (B x)) = insert a (INT x:C. B x)"
  1814   "!!A B C. (INT x:C. A x Un B)  = ((INT x:C. A x) Un B)"
  1815   "!!A B C. (INT x:C. A Un B x)  = (A Un (INT x:C. B x))"
  1816   "!!A B. (INT x: Union A. B x) = (INT y:A. INT x:y. B x)"
  1817   "!!A B C. (INT z: UNION A B. C z) = (INT x:A. INT z: B(x). C z)"
  1818   "!!A B f. (INT x:f`A. B x)    = (INT a:A. B (f a))"
  1819   by auto
  1820 
  1821 lemma ball_simps [simp]:
  1822   "!!A P Q. (ALL x:A. P x | Q) = ((ALL x:A. P x) | Q)"
  1823   "!!A P Q. (ALL x:A. P | Q x) = (P | (ALL x:A. Q x))"
  1824   "!!A P Q. (ALL x:A. P --> Q x) = (P --> (ALL x:A. Q x))"
  1825   "!!A P Q. (ALL x:A. P x --> Q) = ((EX x:A. P x) --> Q)"
  1826   "!!P. (ALL x:{}. P x) = True"
  1827   "!!P. (ALL x:UNIV. P x) = (ALL x. P x)"
  1828   "!!a B P. (ALL x:insert a B. P x) = (P a & (ALL x:B. P x))"
  1829   "!!A P. (ALL x:Union A. P x) = (ALL y:A. ALL x:y. P x)"
  1830   "!!A B P. (ALL x: UNION A B. P x) = (ALL a:A. ALL x: B a. P x)"
  1831   "!!P Q. (ALL x:Collect Q. P x) = (ALL x. Q x --> P x)"
  1832   "!!A P f. (ALL x:f`A. P x) = (ALL x:A. P (f x))"
  1833   "!!A P. (~(ALL x:A. P x)) = (EX x:A. ~P x)"
  1834   by auto
  1835 
  1836 lemma bex_simps [simp]:
  1837   "!!A P Q. (EX x:A. P x & Q) = ((EX x:A. P x) & Q)"
  1838   "!!A P Q. (EX x:A. P & Q x) = (P & (EX x:A. Q x))"
  1839   "!!P. (EX x:{}. P x) = False"
  1840   "!!P. (EX x:UNIV. P x) = (EX x. P x)"
  1841   "!!a B P. (EX x:insert a B. P x) = (P(a) | (EX x:B. P x))"
  1842   "!!A P. (EX x:Union A. P x) = (EX y:A. EX x:y. P x)"
  1843   "!!A B P. (EX x: UNION A B. P x) = (EX a:A. EX x:B a. P x)"
  1844   "!!P Q. (EX x:Collect Q. P x) = (EX x. Q x & P x)"
  1845   "!!A P f. (EX x:f`A. P x) = (EX x:A. P (f x))"
  1846   "!!A P. (~(EX x:A. P x)) = (ALL x:A. ~P x)"
  1847   by auto
  1848 
  1849 lemma ball_conj_distrib:
  1850   "(ALL x:A. P x & Q x) = ((ALL x:A. P x) & (ALL x:A. Q x))"
  1851   by blast
  1852 
  1853 lemma bex_disj_distrib:
  1854   "(EX x:A. P x | Q x) = ((EX x:A. P x) | (EX x:A. Q x))"
  1855   by blast
  1856 
  1857 
  1858 text {* \medskip Maxiscoping: pulling out big Unions and Intersections. *}
  1859 
  1860 lemma UN_extend_simps:
  1861   "!!a B C. insert a (UN x:C. B x) = (if C={} then {a} else (UN x:C. insert a (B x)))"
  1862   "!!A B C. (UN x:C. A x) Un B    = (if C={} then B else (UN x:C. A x Un B))"
  1863   "!!A B C. A Un (UN x:C. B x)   = (if C={} then A else (UN x:C. A Un B x))"
  1864   "!!A B C. ((UN x:C. A x) Int B) = (UN x:C. A x Int B)"
  1865   "!!A B C. (A Int (UN x:C. B x)) = (UN x:C. A Int B x)"
  1866   "!!A B C. ((UN x:C. A x) - B) = (UN x:C. A x - B)"
  1867   "!!A B C. (A - (INT x:C. B x)) = (UN x:C. A - B x)"
  1868   "!!A B. (UN y:A. UN x:y. B x) = (UN x: Union A. B x)"
  1869   "!!A B C. (UN  x:A. UN z: B(x). C z) = (UN z: UNION A B. C z)"
  1870   "!!A B f. (UN a:A. B (f a)) = (UN x:f`A. B x)"
  1871   by auto
  1872 
  1873 lemma INT_extend_simps:
  1874   "!!A B C. (INT x:C. A x) Int B = (if C={} then B else (INT x:C. A x Int B))"
  1875   "!!A B C. A Int (INT x:C. B x) = (if C={} then A else (INT x:C. A Int B x))"
  1876   "!!A B C. (INT x:C. A x) - B   = (if C={} then UNIV-B else (INT x:C. A x - B))"
  1877   "!!A B C. A - (UN x:C. B x)   = (if C={} then A else (INT x:C. A - B x))"
  1878   "!!a B C. insert a (INT x:C. B x) = (INT x:C. insert a (B x))"
  1879   "!!A B C. ((INT x:C. A x) Un B)  = (INT x:C. A x Un B)"
  1880   "!!A B C. A Un (INT x:C. B x)  = (INT x:C. A Un B x)"
  1881   "!!A B. (INT y:A. INT x:y. B x) = (INT x: Union A. B x)"
  1882   "!!A B C. (INT x:A. INT z: B(x). C z) = (INT z: UNION A B. C z)"
  1883   "!!A B f. (INT a:A. B (f a))    = (INT x:f`A. B x)"
  1884   by auto
  1885 
  1886 
  1887 subsubsection {* Monotonicity of various operations *}
  1888 
  1889 lemma image_mono: "A \<subseteq> B ==> f`A \<subseteq> f`B"
  1890   by blast
  1891 
  1892 lemma Pow_mono: "A \<subseteq> B ==> Pow A \<subseteq> Pow B"
  1893   by blast
  1894 
  1895 lemma Union_mono: "A \<subseteq> B ==> \<Union>A \<subseteq> \<Union>B"
  1896   by blast
  1897 
  1898 lemma Inter_anti_mono: "B \<subseteq> A ==> \<Inter>A \<subseteq> \<Inter>B"
  1899   by blast
  1900 
  1901 lemma UN_mono:
  1902   "A \<subseteq> B ==> (!!x. x \<in> A ==> f x \<subseteq> g x) ==>
  1903     (\<Union>x\<in>A. f x) \<subseteq> (\<Union>x\<in>B. g x)"
  1904   by (blast dest: subsetD)
  1905 
  1906 lemma INT_anti_mono:
  1907   "B \<subseteq> A ==> (!!x. x \<in> A ==> f x \<subseteq> g x) ==>
  1908     (\<Inter>x\<in>A. f x) \<subseteq> (\<Inter>x\<in>A. g x)"
  1909   -- {* The last inclusion is POSITIVE! *}
  1910   by (blast dest: subsetD)
  1911 
  1912 lemma insert_mono: "C \<subseteq> D ==> insert a C \<subseteq> insert a D"
  1913   by blast
  1914 
  1915 lemma Un_mono: "A \<subseteq> C ==> B \<subseteq> D ==> A \<union> B \<subseteq> C \<union> D"
  1916   by blast
  1917 
  1918 lemma Int_mono: "A \<subseteq> C ==> B \<subseteq> D ==> A \<inter> B \<subseteq> C \<inter> D"
  1919   by blast
  1920 
  1921 lemma Diff_mono: "A \<subseteq> C ==> D \<subseteq> B ==> A - B \<subseteq> C - D"
  1922   by blast
  1923 
  1924 lemma Compl_anti_mono: "A \<subseteq> B ==> -B \<subseteq> -A"
  1925   by blast
  1926 
  1927 text {* \medskip Monotonicity of implications. *}
  1928 
  1929 lemma in_mono: "A \<subseteq> B ==> x \<in> A --> x \<in> B"
  1930   apply (rule impI)
  1931   apply (erule subsetD, assumption)
  1932   done
  1933 
  1934 lemma conj_mono: "P1 --> Q1 ==> P2 --> Q2 ==> (P1 & P2) --> (Q1 & Q2)"
  1935   by rules
  1936 
  1937 lemma disj_mono: "P1 --> Q1 ==> P2 --> Q2 ==> (P1 | P2) --> (Q1 | Q2)"
  1938   by rules
  1939 
  1940 lemma imp_mono: "Q1 --> P1 ==> P2 --> Q2 ==> (P1 --> P2) --> (Q1 --> Q2)"
  1941   by rules
  1942 
  1943 lemma imp_refl: "P --> P" ..
  1944 
  1945 lemma ex_mono: "(!!x. P x --> Q x) ==> (EX x. P x) --> (EX x. Q x)"
  1946   by rules
  1947 
  1948 lemma all_mono: "(!!x. P x --> Q x) ==> (ALL x. P x) --> (ALL x. Q x)"
  1949   by rules
  1950 
  1951 lemma Collect_mono: "(!!x. P x --> Q x) ==> Collect P \<subseteq> Collect Q"
  1952   by blast
  1953 
  1954 lemma Int_Collect_mono:
  1955     "A \<subseteq> B ==> (!!x. x \<in> A ==> P x --> Q x) ==> A \<inter> Collect P \<subseteq> B \<inter> Collect Q"
  1956   by blast
  1957 
  1958 lemmas basic_monos =
  1959   subset_refl imp_refl disj_mono conj_mono
  1960   ex_mono Collect_mono in_mono
  1961 
  1962 lemma eq_to_mono: "a = b ==> c = d ==> b --> d ==> a --> c"
  1963   by rules
  1964 
  1965 lemma eq_to_mono2: "a = b ==> c = d ==> ~ b --> ~ d ==> ~ a --> ~ c"
  1966   by rules
  1967 
  1968 lemma Least_mono:
  1969   "mono (f::'a::order => 'b::order) ==> EX x:S. ALL y:S. x <= y
  1970     ==> (LEAST y. y : f ` S) = f (LEAST x. x : S)"
  1971     -- {* Courtesy of Stephan Merz *}
  1972   apply clarify
  1973   apply (erule_tac P = "%x. x : S" in LeastI2, fast)
  1974   apply (rule LeastI2)
  1975   apply (auto elim: monoD intro!: order_antisym)
  1976   done
  1977 
  1978 
  1979 subsection {* Inverse image of a function *}
  1980 
  1981 constdefs
  1982   vimage :: "('a => 'b) => 'b set => 'a set"    (infixr "-`" 90)
  1983   "f -` B == {x. f x : B}"
  1984 
  1985 
  1986 subsubsection {* Basic rules *}
  1987 
  1988 lemma vimage_eq [simp]: "(a : f -` B) = (f a : B)"
  1989   by (unfold vimage_def) blast
  1990 
  1991 lemma vimage_singleton_eq: "(a : f -` {b}) = (f a = b)"
  1992   by simp
  1993 
  1994 lemma vimageI [intro]: "f a = b ==> b:B ==> a : f -` B"
  1995   by (unfold vimage_def) blast
  1996 
  1997 lemma vimageI2: "f a : A ==> a : f -` A"
  1998   by (unfold vimage_def) fast
  1999 
  2000 lemma vimageE [elim!]: "a: f -` B ==> (!!x. f a = x ==> x:B ==> P) ==> P"
  2001   by (unfold vimage_def) blast
  2002 
  2003 lemma vimageD: "a : f -` A ==> f a : A"
  2004   by (unfold vimage_def) fast
  2005 
  2006 
  2007 subsubsection {* Equations *}
  2008 
  2009 lemma vimage_empty [simp]: "f -` {} = {}"
  2010   by blast
  2011 
  2012 lemma vimage_Compl: "f -` (-A) = -(f -` A)"
  2013   by blast
  2014 
  2015 lemma vimage_Un [simp]: "f -` (A Un B) = (f -` A) Un (f -` B)"
  2016   by blast
  2017 
  2018 lemma vimage_Int [simp]: "f -` (A Int B) = (f -` A) Int (f -` B)"
  2019   by fast
  2020 
  2021 lemma vimage_Union: "f -` (Union A) = (UN X:A. f -` X)"
  2022   by blast
  2023 
  2024 lemma vimage_UN: "f-`(UN x:A. B x) = (UN x:A. f -` B x)"
  2025   by blast
  2026 
  2027 lemma vimage_INT: "f-`(INT x:A. B x) = (INT x:A. f -` B x)"
  2028   by blast
  2029 
  2030 lemma vimage_Collect_eq [simp]: "f -` Collect P = {y. P (f y)}"
  2031   by blast
  2032 
  2033 lemma vimage_Collect: "(!!x. P (f x) = Q x) ==> f -` (Collect P) = Collect Q"
  2034   by blast
  2035 
  2036 lemma vimage_insert: "f-`(insert a B) = (f-`{a}) Un (f-`B)"
  2037   -- {* NOT suitable for rewriting because of the recurrence of @{term "{a}"}. *}
  2038   by blast
  2039 
  2040 lemma vimage_Diff: "f -` (A - B) = (f -` A) - (f -` B)"
  2041   by blast
  2042 
  2043 lemma vimage_UNIV [simp]: "f -` UNIV = UNIV"
  2044   by blast
  2045 
  2046 lemma vimage_eq_UN: "f-`B = (UN y: B. f-`{y})"
  2047   -- {* NOT suitable for rewriting *}
  2048   by blast
  2049 
  2050 lemma vimage_mono: "A \<subseteq> B ==> f -` A \<subseteq> f -` B"
  2051   -- {* monotonicity *}
  2052   by blast
  2053 
  2054 
  2055 subsection {* Getting the Contents of a Singleton Set *}
  2056 
  2057 constdefs
  2058   contents :: "'a set => 'a"
  2059    "contents X == THE x. X = {x}"
  2060 
  2061 lemma contents_eq [simp]: "contents {x} = x"
  2062 by (simp add: contents_def)
  2063 
  2064 
  2065 subsection {* Transitivity rules for calculational reasoning *}
  2066 
  2067 lemma forw_subst: "a = b ==> P b ==> P a"
  2068   by (rule ssubst)
  2069 
  2070 lemma back_subst: "P a ==> a = b ==> P b"
  2071   by (rule subst)
  2072 
  2073 lemma set_rev_mp: "x:A ==> A \<subseteq> B ==> x:B"
  2074   by (rule subsetD)
  2075 
  2076 lemma set_mp: "A \<subseteq> B ==> x:A ==> x:B"
  2077   by (rule subsetD)
  2078 
  2079 lemma ord_le_eq_trans: "a <= b ==> b = c ==> a <= c"
  2080   by (rule subst)
  2081 
  2082 lemma ord_eq_le_trans: "a = b ==> b <= c ==> a <= c"
  2083   by (rule ssubst)
  2084 
  2085 lemma ord_less_eq_trans: "a < b ==> b = c ==> a < c"
  2086   by (rule subst)
  2087 
  2088 lemma ord_eq_less_trans: "a = b ==> b < c ==> a < c"
  2089   by (rule ssubst)
  2090 
  2091 lemma order_less_subst2: "(a::'a::order) < b ==> f b < (c::'c::order) ==>
  2092   (!!x y. x < y ==> f x < f y) ==> f a < c"
  2093 proof -
  2094   assume r: "!!x y. x < y ==> f x < f y"
  2095   assume "a < b" hence "f a < f b" by (rule r)
  2096   also assume "f b < c"
  2097   finally (order_less_trans) show ?thesis .
  2098 qed
  2099 
  2100 lemma order_less_subst1: "(a::'a::order) < f b ==> (b::'b::order) < c ==>
  2101   (!!x y. x < y ==> f x < f y) ==> a < f c"
  2102 proof -
  2103   assume r: "!!x y. x < y ==> f x < f y"
  2104   assume "a < f b"
  2105   also assume "b < c" hence "f b < f c" by (rule r)
  2106   finally (order_less_trans) show ?thesis .
  2107 qed
  2108 
  2109 lemma order_le_less_subst2: "(a::'a::order) <= b ==> f b < (c::'c::order) ==>
  2110   (!!x y. x <= y ==> f x <= f y) ==> f a < c"
  2111 proof -
  2112   assume r: "!!x y. x <= y ==> f x <= f y"
  2113   assume "a <= b" hence "f a <= f b" by (rule r)
  2114   also assume "f b < c"
  2115   finally (order_le_less_trans) show ?thesis .
  2116 qed
  2117 
  2118 lemma order_le_less_subst1: "(a::'a::order) <= f b ==> (b::'b::order) < c ==>
  2119   (!!x y. x < y ==> f x < f y) ==> a < f c"
  2120 proof -
  2121   assume r: "!!x y. x < y ==> f x < f y"
  2122   assume "a <= f b"
  2123   also assume "b < c" hence "f b < f c" by (rule r)
  2124   finally (order_le_less_trans) show ?thesis .
  2125 qed
  2126 
  2127 lemma order_less_le_subst2: "(a::'a::order) < b ==> f b <= (c::'c::order) ==>
  2128   (!!x y. x < y ==> f x < f y) ==> f a < c"
  2129 proof -
  2130   assume r: "!!x y. x < y ==> f x < f y"
  2131   assume "a < b" hence "f a < f b" by (rule r)
  2132   also assume "f b <= c"
  2133   finally (order_less_le_trans) show ?thesis .
  2134 qed
  2135 
  2136 lemma order_less_le_subst1: "(a::'a::order) < f b ==> (b::'b::order) <= c ==>
  2137   (!!x y. x <= y ==> f x <= f y) ==> a < f c"
  2138 proof -
  2139   assume r: "!!x y. x <= y ==> f x <= f y"
  2140   assume "a < f b"
  2141   also assume "b <= c" hence "f b <= f c" by (rule r)
  2142   finally (order_less_le_trans) show ?thesis .
  2143 qed
  2144 
  2145 lemma order_subst1: "(a::'a::order) <= f b ==> (b::'b::order) <= c ==>
  2146   (!!x y. x <= y ==> f x <= f y) ==> a <= f c"
  2147 proof -
  2148   assume r: "!!x y. x <= y ==> f x <= f y"
  2149   assume "a <= f b"
  2150   also assume "b <= c" hence "f b <= f c" by (rule r)
  2151   finally (order_trans) show ?thesis .
  2152 qed
  2153 
  2154 lemma order_subst2: "(a::'a::order) <= b ==> f b <= (c::'c::order) ==>
  2155   (!!x y. x <= y ==> f x <= f y) ==> f a <= c"
  2156 proof -
  2157   assume r: "!!x y. x <= y ==> f x <= f y"
  2158   assume "a <= b" hence "f a <= f b" by (rule r)
  2159   also assume "f b <= c"
  2160   finally (order_trans) show ?thesis .
  2161 qed
  2162 
  2163 lemma ord_le_eq_subst: "a <= b ==> f b = c ==>
  2164   (!!x y. x <= y ==> f x <= f y) ==> f a <= c"
  2165 proof -
  2166   assume r: "!!x y. x <= y ==> f x <= f y"
  2167   assume "a <= b" hence "f a <= f b" by (rule r)
  2168   also assume "f b = c"
  2169   finally (ord_le_eq_trans) show ?thesis .
  2170 qed
  2171 
  2172 lemma ord_eq_le_subst: "a = f b ==> b <= c ==>
  2173   (!!x y. x <= y ==> f x <= f y) ==> a <= f c"
  2174 proof -
  2175   assume r: "!!x y. x <= y ==> f x <= f y"
  2176   assume "a = f b"
  2177   also assume "b <= c" hence "f b <= f c" by (rule r)
  2178   finally (ord_eq_le_trans) show ?thesis .
  2179 qed
  2180 
  2181 lemma ord_less_eq_subst: "a < b ==> f b = c ==>
  2182   (!!x y. x < y ==> f x < f y) ==> f a < c"
  2183 proof -
  2184   assume r: "!!x y. x < y ==> f x < f y"
  2185   assume "a < b" hence "f a < f b" by (rule r)
  2186   also assume "f b = c"
  2187   finally (ord_less_eq_trans) show ?thesis .
  2188 qed
  2189 
  2190 lemma ord_eq_less_subst: "a = f b ==> b < c ==>
  2191   (!!x y. x < y ==> f x < f y) ==> a < f c"
  2192 proof -
  2193   assume r: "!!x y. x < y ==> f x < f y"
  2194   assume "a = f b"
  2195   also assume "b < c" hence "f b < f c" by (rule r)
  2196   finally (ord_eq_less_trans) show ?thesis .
  2197 qed
  2198 
  2199 text {*
  2200   Note that this list of rules is in reverse order of priorities.
  2201 *}
  2202 
  2203 lemmas basic_trans_rules [trans] =
  2204   order_less_subst2
  2205   order_less_subst1
  2206   order_le_less_subst2
  2207   order_le_less_subst1
  2208   order_less_le_subst2
  2209   order_less_le_subst1
  2210   order_subst2
  2211   order_subst1
  2212   ord_le_eq_subst
  2213   ord_eq_le_subst
  2214   ord_less_eq_subst
  2215   ord_eq_less_subst
  2216   forw_subst
  2217   back_subst
  2218   rev_mp
  2219   mp
  2220   set_rev_mp
  2221   set_mp
  2222   order_neq_le_trans
  2223   order_le_neq_trans
  2224   order_less_trans
  2225   order_less_asym'
  2226   order_le_less_trans
  2227   order_less_le_trans
  2228   order_trans
  2229   order_antisym
  2230   ord_le_eq_trans
  2231   ord_eq_le_trans
  2232   ord_less_eq_trans
  2233   ord_eq_less_trans
  2234   trans
  2235 
  2236 end