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