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