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