src/HOL/Set.thy
author hoelzl
Thu Dec 02 14:34:58 2010 +0100 (2010-12-02)
changeset 40872 7c556a9240de
parent 40703 d1fc454d6735
child 41076 a7fba340058c
permissions -rw-r--r--
Move SUP_commute, SUP_less_iff to HOL image;
Cleanup generic complete_lattice lemmas in Positive_Infinite_Real;
Cleanup lemma positive_integral_alt;
     1 (*  Author:     Tobias Nipkow, Lawrence C Paulson and Markus Wenzel *)
     2 
     3 header {* Set theory for higher-order logic *}
     4 
     5 theory Set
     6 imports Lattices
     7 begin
     8 
     9 subsection {* Sets as predicates *}
    10 
    11 types 'a set = "'a \<Rightarrow> bool"
    12 
    13 definition Collect :: "('a \<Rightarrow> bool) \<Rightarrow> 'a set" where -- "comprehension"
    14   "Collect P = P"
    15 
    16 definition member :: "'a \<Rightarrow> 'a set \<Rightarrow> bool" where -- "membership"
    17   mem_def: "member x A = A x"
    18 
    19 notation
    20   member  ("op :") and
    21   member  ("(_/ : _)" [50, 51] 50)
    22 
    23 abbreviation not_member where
    24   "not_member x A \<equiv> ~ (x : A)" -- "non-membership"
    25 
    26 notation
    27   not_member  ("op ~:") and
    28   not_member  ("(_/ ~: _)" [50, 51] 50)
    29 
    30 notation (xsymbols)
    31   member      ("op \<in>") and
    32   member      ("(_/ \<in> _)" [50, 51] 50) and
    33   not_member  ("op \<notin>") and
    34   not_member  ("(_/ \<notin> _)" [50, 51] 50)
    35 
    36 notation (HTML output)
    37   member      ("op \<in>") and
    38   member      ("(_/ \<in> _)" [50, 51] 50) and
    39   not_member  ("op \<notin>") and
    40   not_member  ("(_/ \<notin> _)" [50, 51] 50)
    41 
    42 text {* Set comprehensions *}
    43 
    44 syntax
    45   "_Coll" :: "pttrn => bool => 'a set"    ("(1{_./ _})")
    46 translations
    47   "{x. P}" == "CONST Collect (%x. P)"
    48 
    49 syntax
    50   "_Collect" :: "idt => 'a set => bool => 'a set"    ("(1{_ :/ _./ _})")
    51 syntax (xsymbols)
    52   "_Collect" :: "idt => 'a set => bool => 'a set"    ("(1{_ \<in>/ _./ _})")
    53 translations
    54   "{x:A. P}" => "{x. x:A & P}"
    55 
    56 lemma mem_Collect_eq [iff]: "(a : {x. P(x)}) = P(a)"
    57   by (simp add: Collect_def mem_def)
    58 
    59 lemma Collect_mem_eq [simp]: "{x. x:A} = A"
    60   by (simp add: Collect_def mem_def)
    61 
    62 lemma CollectI: "P(a) ==> a : {x. P(x)}"
    63   by simp
    64 
    65 lemma CollectD: "a : {x. P(x)} ==> P(a)"
    66   by simp
    67 
    68 lemma Collect_cong: "(!!x. P x = Q x) ==> {x. P(x)} = {x. Q(x)}"
    69   by simp
    70 
    71 text {*
    72 Simproc for pulling @{text "x=t"} in @{text "{x. \<dots> & x=t & \<dots>}"}
    73 to the front (and similarly for @{text "t=x"}):
    74 *}
    75 
    76 setup {*
    77 let
    78   val Coll_perm_tac = rtac @{thm Collect_cong} 1 THEN rtac @{thm iffI} 1 THEN
    79     ALLGOALS(EVERY'[REPEAT_DETERM o (etac @{thm conjE}),
    80                     DEPTH_SOLVE_1 o (ares_tac [@{thm conjI}])])
    81   val defColl_regroup = Simplifier.simproc_global @{theory}
    82     "defined Collect" ["{x. P x & Q x}"]
    83     (Quantifier1.rearrange_Coll Coll_perm_tac)
    84 in
    85   Simplifier.map_simpset (fn ss => ss addsimprocs [defColl_regroup])
    86 end
    87 *}
    88 
    89 lemmas CollectE = CollectD [elim_format]
    90 
    91 text {* Set enumerations *}
    92 
    93 abbreviation empty :: "'a set" ("{}") where
    94   "{} \<equiv> bot"
    95 
    96 definition insert :: "'a \<Rightarrow> 'a set \<Rightarrow> 'a set" where
    97   insert_compr: "insert a B = {x. x = a \<or> x \<in> B}"
    98 
    99 syntax
   100   "_Finset" :: "args => 'a set"    ("{(_)}")
   101 translations
   102   "{x, xs}" == "CONST insert x {xs}"
   103   "{x}" == "CONST insert x {}"
   104 
   105 
   106 subsection {* Subsets and bounded quantifiers *}
   107 
   108 abbreviation
   109   subset :: "'a set \<Rightarrow> 'a set \<Rightarrow> bool" where
   110   "subset \<equiv> less"
   111 
   112 abbreviation
   113   subset_eq :: "'a set \<Rightarrow> 'a set \<Rightarrow> bool" where
   114   "subset_eq \<equiv> less_eq"
   115 
   116 notation (output)
   117   subset  ("op <") and
   118   subset  ("(_/ < _)" [50, 51] 50) and
   119   subset_eq  ("op <=") and
   120   subset_eq  ("(_/ <= _)" [50, 51] 50)
   121 
   122 notation (xsymbols)
   123   subset  ("op \<subset>") and
   124   subset  ("(_/ \<subset> _)" [50, 51] 50) and
   125   subset_eq  ("op \<subseteq>") and
   126   subset_eq  ("(_/ \<subseteq> _)" [50, 51] 50)
   127 
   128 notation (HTML output)
   129   subset  ("op \<subset>") and
   130   subset  ("(_/ \<subset> _)" [50, 51] 50) and
   131   subset_eq  ("op \<subseteq>") and
   132   subset_eq  ("(_/ \<subseteq> _)" [50, 51] 50)
   133 
   134 abbreviation (input)
   135   supset :: "'a set \<Rightarrow> 'a set \<Rightarrow> bool" where
   136   "supset \<equiv> greater"
   137 
   138 abbreviation (input)
   139   supset_eq :: "'a set \<Rightarrow> 'a set \<Rightarrow> bool" where
   140   "supset_eq \<equiv> greater_eq"
   141 
   142 notation (xsymbols)
   143   supset  ("op \<supset>") and
   144   supset  ("(_/ \<supset> _)" [50, 51] 50) and
   145   supset_eq  ("op \<supseteq>") and
   146   supset_eq  ("(_/ \<supseteq> _)" [50, 51] 50)
   147 
   148 definition Ball :: "'a set \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> bool" where
   149   "Ball A P \<longleftrightarrow> (\<forall>x. x \<in> A \<longrightarrow> P x)"   -- "bounded universal quantifiers"
   150 
   151 definition Bex :: "'a set \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> bool" where
   152   "Bex A P \<longleftrightarrow> (\<exists>x. x \<in> A \<and> P x)"   -- "bounded existential quantifiers"
   153 
   154 syntax
   155   "_Ball"       :: "pttrn => 'a set => bool => bool"      ("(3ALL _:_./ _)" [0, 0, 10] 10)
   156   "_Bex"        :: "pttrn => 'a set => bool => bool"      ("(3EX _:_./ _)" [0, 0, 10] 10)
   157   "_Bex1"       :: "pttrn => 'a set => bool => bool"      ("(3EX! _:_./ _)" [0, 0, 10] 10)
   158   "_Bleast"     :: "id => 'a set => bool => 'a"           ("(3LEAST _:_./ _)" [0, 0, 10] 10)
   159 
   160 syntax (HOL)
   161   "_Ball"       :: "pttrn => 'a set => bool => bool"      ("(3! _:_./ _)" [0, 0, 10] 10)
   162   "_Bex"        :: "pttrn => 'a set => bool => bool"      ("(3? _:_./ _)" [0, 0, 10] 10)
   163   "_Bex1"       :: "pttrn => 'a set => bool => bool"      ("(3?! _:_./ _)" [0, 0, 10] 10)
   164 
   165 syntax (xsymbols)
   166   "_Ball"       :: "pttrn => 'a set => bool => bool"      ("(3\<forall>_\<in>_./ _)" [0, 0, 10] 10)
   167   "_Bex"        :: "pttrn => 'a set => bool => bool"      ("(3\<exists>_\<in>_./ _)" [0, 0, 10] 10)
   168   "_Bex1"       :: "pttrn => 'a set => bool => bool"      ("(3\<exists>!_\<in>_./ _)" [0, 0, 10] 10)
   169   "_Bleast"     :: "id => 'a set => bool => 'a"           ("(3LEAST_\<in>_./ _)" [0, 0, 10] 10)
   170 
   171 syntax (HTML output)
   172   "_Ball"       :: "pttrn => 'a set => bool => bool"      ("(3\<forall>_\<in>_./ _)" [0, 0, 10] 10)
   173   "_Bex"        :: "pttrn => 'a set => bool => bool"      ("(3\<exists>_\<in>_./ _)" [0, 0, 10] 10)
   174   "_Bex1"       :: "pttrn => 'a set => bool => bool"      ("(3\<exists>!_\<in>_./ _)" [0, 0, 10] 10)
   175 
   176 translations
   177   "ALL x:A. P" == "CONST Ball A (%x. P)"
   178   "EX x:A. P" == "CONST Bex A (%x. P)"
   179   "EX! x:A. P" => "EX! x. x:A & P"
   180   "LEAST x:A. P" => "LEAST x. x:A & P"
   181 
   182 syntax (output)
   183   "_setlessAll" :: "[idt, 'a, bool] => bool"  ("(3ALL _<_./ _)"  [0, 0, 10] 10)
   184   "_setlessEx"  :: "[idt, 'a, bool] => bool"  ("(3EX _<_./ _)"  [0, 0, 10] 10)
   185   "_setleAll"   :: "[idt, 'a, bool] => bool"  ("(3ALL _<=_./ _)" [0, 0, 10] 10)
   186   "_setleEx"    :: "[idt, 'a, bool] => bool"  ("(3EX _<=_./ _)" [0, 0, 10] 10)
   187   "_setleEx1"   :: "[idt, 'a, bool] => bool"  ("(3EX! _<=_./ _)" [0, 0, 10] 10)
   188 
   189 syntax (xsymbols)
   190   "_setlessAll" :: "[idt, 'a, bool] => bool"   ("(3\<forall>_\<subset>_./ _)"  [0, 0, 10] 10)
   191   "_setlessEx"  :: "[idt, 'a, bool] => bool"   ("(3\<exists>_\<subset>_./ _)"  [0, 0, 10] 10)
   192   "_setleAll"   :: "[idt, 'a, bool] => bool"   ("(3\<forall>_\<subseteq>_./ _)" [0, 0, 10] 10)
   193   "_setleEx"    :: "[idt, 'a, bool] => bool"   ("(3\<exists>_\<subseteq>_./ _)" [0, 0, 10] 10)
   194   "_setleEx1"   :: "[idt, 'a, bool] => bool"   ("(3\<exists>!_\<subseteq>_./ _)" [0, 0, 10] 10)
   195 
   196 syntax (HOL output)
   197   "_setlessAll" :: "[idt, 'a, bool] => bool"   ("(3! _<_./ _)"  [0, 0, 10] 10)
   198   "_setlessEx"  :: "[idt, 'a, bool] => bool"   ("(3? _<_./ _)"  [0, 0, 10] 10)
   199   "_setleAll"   :: "[idt, 'a, bool] => bool"   ("(3! _<=_./ _)" [0, 0, 10] 10)
   200   "_setleEx"    :: "[idt, 'a, bool] => bool"   ("(3? _<=_./ _)" [0, 0, 10] 10)
   201   "_setleEx1"   :: "[idt, 'a, bool] => bool"   ("(3?! _<=_./ _)" [0, 0, 10] 10)
   202 
   203 syntax (HTML output)
   204   "_setlessAll" :: "[idt, 'a, bool] => bool"   ("(3\<forall>_\<subset>_./ _)"  [0, 0, 10] 10)
   205   "_setlessEx"  :: "[idt, 'a, bool] => bool"   ("(3\<exists>_\<subset>_./ _)"  [0, 0, 10] 10)
   206   "_setleAll"   :: "[idt, 'a, bool] => bool"   ("(3\<forall>_\<subseteq>_./ _)" [0, 0, 10] 10)
   207   "_setleEx"    :: "[idt, 'a, bool] => bool"   ("(3\<exists>_\<subseteq>_./ _)" [0, 0, 10] 10)
   208   "_setleEx1"   :: "[idt, 'a, bool] => bool"   ("(3\<exists>!_\<subseteq>_./ _)" [0, 0, 10] 10)
   209 
   210 translations
   211  "\<forall>A\<subset>B. P"   =>  "ALL A. A \<subset> B --> P"
   212  "\<exists>A\<subset>B. P"   =>  "EX A. A \<subset> B & P"
   213  "\<forall>A\<subseteq>B. P"   =>  "ALL A. A \<subseteq> B --> P"
   214  "\<exists>A\<subseteq>B. P"   =>  "EX A. A \<subseteq> B & P"
   215  "\<exists>!A\<subseteq>B. P"  =>  "EX! A. A \<subseteq> B & P"
   216 
   217 print_translation {*
   218 let
   219   val Type (set_type, _) = @{typ "'a set"};   (* FIXME 'a => bool (!?!) *)
   220   val All_binder = Syntax.binder_name @{const_syntax All};
   221   val Ex_binder = Syntax.binder_name @{const_syntax Ex};
   222   val impl = @{const_syntax HOL.implies};
   223   val conj = @{const_syntax HOL.conj};
   224   val sbset = @{const_syntax subset};
   225   val sbset_eq = @{const_syntax subset_eq};
   226 
   227   val trans =
   228    [((All_binder, impl, sbset), @{syntax_const "_setlessAll"}),
   229     ((All_binder, impl, sbset_eq), @{syntax_const "_setleAll"}),
   230     ((Ex_binder, conj, sbset), @{syntax_const "_setlessEx"}),
   231     ((Ex_binder, conj, sbset_eq), @{syntax_const "_setleEx"})];
   232 
   233   fun mk v v' c n P =
   234     if v = v' andalso not (Term.exists_subterm (fn Free (x, _) => x = v | _ => false) n)
   235     then Syntax.const c $ Syntax.mark_bound v' $ n $ P else raise Match;
   236 
   237   fun tr' q = (q,
   238         fn [Const (@{syntax_const "_bound"}, _) $ Free (v, Type (T, _)),
   239             Const (c, _) $
   240               (Const (d, _) $ (Const (@{syntax_const "_bound"}, _) $ Free (v', _)) $ n) $ P] =>
   241             if T = set_type then
   242               (case AList.lookup (op =) trans (q, c, d) of
   243                 NONE => raise Match
   244               | SOME l => mk v v' l n P)
   245             else raise Match
   246          | _ => raise Match);
   247 in
   248   [tr' All_binder, tr' Ex_binder]
   249 end
   250 *}
   251 
   252 
   253 text {*
   254   \medskip Translate between @{text "{e | x1...xn. P}"} and @{text
   255   "{u. EX x1..xn. u = e & P}"}; @{text "{y. EX x1..xn. y = e & P}"} is
   256   only translated if @{text "[0..n] subset bvs(e)"}.
   257 *}
   258 
   259 syntax
   260   "_Setcompr" :: "'a => idts => bool => 'a set"    ("(1{_ |/_./ _})")
   261 
   262 parse_translation {*
   263   let
   264     val ex_tr = snd (mk_binder_tr ("EX ", @{const_syntax Ex}));
   265 
   266     fun nvars (Const (@{syntax_const "_idts"}, _) $ _ $ idts) = nvars idts + 1
   267       | nvars _ = 1;
   268 
   269     fun setcompr_tr [e, idts, b] =
   270       let
   271         val eq = Syntax.const @{const_syntax HOL.eq} $ Bound (nvars idts) $ e;
   272         val P = Syntax.const @{const_syntax HOL.conj} $ eq $ b;
   273         val exP = ex_tr [idts, P];
   274       in Syntax.const @{const_syntax Collect} $ Term.absdummy (dummyT, exP) end;
   275 
   276   in [(@{syntax_const "_Setcompr"}, setcompr_tr)] end;
   277 *}
   278 
   279 print_translation {*
   280  [Syntax.preserve_binder_abs2_tr' @{const_syntax Ball} @{syntax_const "_Ball"},
   281   Syntax.preserve_binder_abs2_tr' @{const_syntax Bex} @{syntax_const "_Bex"}]
   282 *} -- {* to avoid eta-contraction of body *}
   283 
   284 print_translation {*
   285 let
   286   val ex_tr' = snd (mk_binder_tr' (@{const_syntax Ex}, "DUMMY"));
   287 
   288   fun setcompr_tr' [Abs (abs as (_, _, P))] =
   289     let
   290       fun check (Const (@{const_syntax Ex}, _) $ Abs (_, _, P), n) = check (P, n + 1)
   291         | check (Const (@{const_syntax HOL.conj}, _) $
   292               (Const (@{const_syntax HOL.eq}, _) $ Bound m $ e) $ P, n) =
   293             n > 0 andalso m = n andalso not (loose_bvar1 (P, n)) andalso
   294             subset (op =) (0 upto (n - 1), add_loose_bnos (e, 0, []))
   295         | check _ = false;
   296 
   297         fun tr' (_ $ abs) =
   298           let val _ $ idts $ (_ $ (_ $ _ $ e) $ Q) = ex_tr' [abs]
   299           in Syntax.const @{syntax_const "_Setcompr"} $ e $ idts $ Q end;
   300     in
   301       if check (P, 0) then tr' P
   302       else
   303         let
   304           val (x as _ $ Free(xN, _), t) = atomic_abs_tr' abs;
   305           val M = Syntax.const @{syntax_const "_Coll"} $ x $ t;
   306         in
   307           case t of
   308             Const (@{const_syntax HOL.conj}, _) $
   309               (Const (@{const_syntax Set.member}, _) $
   310                 (Const (@{syntax_const "_bound"}, _) $ Free (yN, _)) $ A) $ P =>
   311             if xN = yN then Syntax.const @{syntax_const "_Collect"} $ x $ A $ P else M
   312           | _ => M
   313         end
   314     end;
   315   in [(@{const_syntax Collect}, setcompr_tr')] end;
   316 *}
   317 
   318 setup {*
   319 let
   320   val unfold_bex_tac = unfold_tac @{thms "Bex_def"};
   321   fun prove_bex_tac ss = unfold_bex_tac ss THEN Quantifier1.prove_one_point_ex_tac;
   322   val rearrange_bex = Quantifier1.rearrange_bex prove_bex_tac;
   323   val unfold_ball_tac = unfold_tac @{thms "Ball_def"};
   324   fun prove_ball_tac ss = unfold_ball_tac ss THEN Quantifier1.prove_one_point_all_tac;
   325   val rearrange_ball = Quantifier1.rearrange_ball prove_ball_tac;
   326   val defBEX_regroup = Simplifier.simproc_global @{theory}
   327     "defined BEX" ["EX x:A. P x & Q x"] rearrange_bex;
   328   val defBALL_regroup = Simplifier.simproc_global @{theory}
   329     "defined BALL" ["ALL x:A. P x --> Q x"] rearrange_ball;
   330 in
   331   Simplifier.map_simpset (fn ss => ss addsimprocs [defBALL_regroup, defBEX_regroup])
   332 end
   333 *}
   334 
   335 lemma ballI [intro!]: "(!!x. x:A ==> P x) ==> ALL x:A. P x"
   336   by (simp add: Ball_def)
   337 
   338 lemmas strip = impI allI ballI
   339 
   340 lemma bspec [dest?]: "ALL x:A. P x ==> x:A ==> P x"
   341   by (simp add: Ball_def)
   342 
   343 text {*
   344   Gives better instantiation for bound:
   345 *}
   346 
   347 declaration {* fn _ =>
   348   Classical.map_cs (fn cs => cs addbefore ("bspec", datac @{thm bspec} 1))
   349 *}
   350 
   351 ML {*
   352 structure Simpdata =
   353 struct
   354 
   355 open Simpdata;
   356 
   357 val mksimps_pairs = [(@{const_name Ball}, @{thms bspec})] @ mksimps_pairs;
   358 
   359 end;
   360 
   361 open Simpdata;
   362 *}
   363 
   364 declaration {* fn _ =>
   365   Simplifier.map_ss (fn ss => ss setmksimps (mksimps mksimps_pairs))
   366 *}
   367 
   368 lemma ballE [elim]: "ALL x:A. P x ==> (P x ==> Q) ==> (x ~: A ==> Q) ==> Q"
   369   by (unfold Ball_def) blast
   370 
   371 lemma bexI [intro]: "P x ==> x:A ==> EX x:A. P x"
   372   -- {* Normally the best argument order: @{prop "P x"} constrains the
   373     choice of @{prop "x:A"}. *}
   374   by (unfold Bex_def) blast
   375 
   376 lemma rev_bexI [intro?]: "x:A ==> P x ==> EX x:A. P x"
   377   -- {* The best argument order when there is only one @{prop "x:A"}. *}
   378   by (unfold Bex_def) blast
   379 
   380 lemma bexCI: "(ALL x:A. ~P x ==> P a) ==> a:A ==> EX x:A. P x"
   381   by (unfold Bex_def) blast
   382 
   383 lemma bexE [elim!]: "EX x:A. P x ==> (!!x. x:A ==> P x ==> Q) ==> Q"
   384   by (unfold Bex_def) blast
   385 
   386 lemma ball_triv [simp]: "(ALL x:A. P) = ((EX x. x:A) --> P)"
   387   -- {* Trival rewrite rule. *}
   388   by (simp add: Ball_def)
   389 
   390 lemma bex_triv [simp]: "(EX x:A. P) = ((EX x. x:A) & P)"
   391   -- {* Dual form for existentials. *}
   392   by (simp add: Bex_def)
   393 
   394 lemma bex_triv_one_point1 [simp]: "(EX x:A. x = a) = (a:A)"
   395   by blast
   396 
   397 lemma bex_triv_one_point2 [simp]: "(EX x:A. a = x) = (a:A)"
   398   by blast
   399 
   400 lemma bex_one_point1 [simp]: "(EX x:A. x = a & P x) = (a:A & P a)"
   401   by blast
   402 
   403 lemma bex_one_point2 [simp]: "(EX x:A. a = x & P x) = (a:A & P a)"
   404   by blast
   405 
   406 lemma ball_one_point1 [simp]: "(ALL x:A. x = a --> P x) = (a:A --> P a)"
   407   by blast
   408 
   409 lemma ball_one_point2 [simp]: "(ALL x:A. a = x --> P x) = (a:A --> P a)"
   410   by blast
   411 
   412 
   413 text {* Congruence rules *}
   414 
   415 lemma ball_cong:
   416   "A = B ==> (!!x. x:B ==> P x = Q x) ==>
   417     (ALL x:A. P x) = (ALL x:B. Q x)"
   418   by (simp add: Ball_def)
   419 
   420 lemma strong_ball_cong [cong]:
   421   "A = B ==> (!!x. x:B =simp=> P x = Q x) ==>
   422     (ALL x:A. P x) = (ALL x:B. Q x)"
   423   by (simp add: simp_implies_def Ball_def)
   424 
   425 lemma bex_cong:
   426   "A = B ==> (!!x. x:B ==> P x = Q x) ==>
   427     (EX x:A. P x) = (EX x:B. Q x)"
   428   by (simp add: Bex_def cong: conj_cong)
   429 
   430 lemma strong_bex_cong [cong]:
   431   "A = B ==> (!!x. x:B =simp=> P x = Q x) ==>
   432     (EX x:A. P x) = (EX x:B. Q x)"
   433   by (simp add: simp_implies_def Bex_def cong: conj_cong)
   434 
   435 
   436 subsection {* Basic operations *}
   437 
   438 subsubsection {* Subsets *}
   439 
   440 lemma subsetI [intro!]: "(\<And>x. x \<in> A \<Longrightarrow> x \<in> B) \<Longrightarrow> A \<subseteq> B"
   441   unfolding mem_def by (rule le_funI, rule le_boolI)
   442 
   443 text {*
   444   \medskip Map the type @{text "'a set => anything"} to just @{typ
   445   'a}; for overloading constants whose first argument has type @{typ
   446   "'a set"}.
   447 *}
   448 
   449 lemma subsetD [elim, intro?]: "A \<subseteq> B ==> c \<in> A ==> c \<in> B"
   450   unfolding mem_def by (erule le_funE, erule le_boolE)
   451   -- {* Rule in Modus Ponens style. *}
   452 
   453 lemma rev_subsetD [no_atp,intro?]: "c \<in> A ==> A \<subseteq> B ==> c \<in> B"
   454   -- {* The same, with reversed premises for use with @{text erule} --
   455       cf @{text rev_mp}. *}
   456   by (rule subsetD)
   457 
   458 text {*
   459   \medskip Converts @{prop "A \<subseteq> B"} to @{prop "x \<in> A ==> x \<in> B"}.
   460 *}
   461 
   462 lemma subsetCE [no_atp,elim]: "A \<subseteq> B ==> (c \<notin> A ==> P) ==> (c \<in> B ==> P) ==> P"
   463   -- {* Classical elimination rule. *}
   464   unfolding mem_def by (blast dest: le_funE le_boolE)
   465 
   466 lemma subset_eq [no_atp]: "A \<le> B = (\<forall>x\<in>A. x \<in> B)" by blast
   467 
   468 lemma contra_subsetD [no_atp]: "A \<subseteq> B ==> c \<notin> B ==> c \<notin> A"
   469   by blast
   470 
   471 lemma subset_refl [simp]: "A \<subseteq> A"
   472   by (fact order_refl)
   473 
   474 lemma subset_trans: "A \<subseteq> B ==> B \<subseteq> C ==> A \<subseteq> C"
   475   by (fact order_trans)
   476 
   477 lemma set_rev_mp: "x:A ==> A \<subseteq> B ==> x:B"
   478   by (rule subsetD)
   479 
   480 lemma set_mp: "A \<subseteq> B ==> x:A ==> x:B"
   481   by (rule subsetD)
   482 
   483 lemma eq_mem_trans: "a=b ==> b \<in> A ==> a \<in> A"
   484   by simp
   485 
   486 lemmas basic_trans_rules [trans] =
   487   order_trans_rules set_rev_mp set_mp eq_mem_trans
   488 
   489 
   490 subsubsection {* Equality *}
   491 
   492 lemma set_eqI: assumes prem: "(!!x. (x:A) = (x:B))" shows "A = B"
   493   apply (rule prem [THEN ext, THEN arg_cong, THEN box_equals])
   494    apply (rule Collect_mem_eq)
   495   apply (rule Collect_mem_eq)
   496   done
   497 
   498 lemma set_eq_iff [no_atp]: "(A = B) = (ALL x. (x:A) = (x:B))"
   499 by(auto intro:set_eqI)
   500 
   501 lemma subset_antisym [intro!]: "A \<subseteq> B ==> B \<subseteq> A ==> A = B"
   502   -- {* Anti-symmetry of the subset relation. *}
   503   by (iprover intro: set_eqI subsetD)
   504 
   505 text {*
   506   \medskip Equality rules from ZF set theory -- are they appropriate
   507   here?
   508 *}
   509 
   510 lemma equalityD1: "A = B ==> A \<subseteq> B"
   511   by simp
   512 
   513 lemma equalityD2: "A = B ==> B \<subseteq> A"
   514   by simp
   515 
   516 text {*
   517   \medskip Be careful when adding this to the claset as @{text
   518   subset_empty} is in the simpset: @{prop "A = {}"} goes to @{prop "{}
   519   \<subseteq> A"} and @{prop "A \<subseteq> {}"} and then back to @{prop "A = {}"}!
   520 *}
   521 
   522 lemma equalityE: "A = B ==> (A \<subseteq> B ==> B \<subseteq> A ==> P) ==> P"
   523   by simp
   524 
   525 lemma equalityCE [elim]:
   526     "A = B ==> (c \<in> A ==> c \<in> B ==> P) ==> (c \<notin> A ==> c \<notin> B ==> P) ==> P"
   527   by blast
   528 
   529 lemma eqset_imp_iff: "A = B ==> (x : A) = (x : B)"
   530   by simp
   531 
   532 lemma eqelem_imp_iff: "x = y ==> (x : A) = (y : A)"
   533   by simp
   534 
   535 
   536 subsubsection {* The universal set -- UNIV *}
   537 
   538 abbreviation UNIV :: "'a set" where
   539   "UNIV \<equiv> top"
   540 
   541 lemma UNIV_def:
   542   "UNIV = {x. True}"
   543   by (simp add: top_fun_eq top_bool_eq Collect_def)
   544 
   545 lemma UNIV_I [simp]: "x : UNIV"
   546   by (simp add: UNIV_def)
   547 
   548 declare UNIV_I [intro]  -- {* unsafe makes it less likely to cause problems *}
   549 
   550 lemma UNIV_witness [intro?]: "EX x. x : UNIV"
   551   by simp
   552 
   553 lemma subset_UNIV [simp]: "A \<subseteq> UNIV"
   554   by (rule subsetI) (rule UNIV_I)
   555 
   556 text {*
   557   \medskip Eta-contracting these two rules (to remove @{text P})
   558   causes them to be ignored because of their interaction with
   559   congruence rules.
   560 *}
   561 
   562 lemma ball_UNIV [simp]: "Ball UNIV P = All P"
   563   by (simp add: Ball_def)
   564 
   565 lemma bex_UNIV [simp]: "Bex UNIV P = Ex P"
   566   by (simp add: Bex_def)
   567 
   568 lemma UNIV_eq_I: "(\<And>x. x \<in> A) \<Longrightarrow> UNIV = A"
   569   by auto
   570 
   571 
   572 subsubsection {* The empty set *}
   573 
   574 lemma empty_def:
   575   "{} = {x. False}"
   576   by (simp add: bot_fun_eq bot_bool_eq Collect_def)
   577 
   578 lemma empty_iff [simp]: "(c : {}) = False"
   579   by (simp add: empty_def)
   580 
   581 lemma emptyE [elim!]: "a : {} ==> P"
   582   by simp
   583 
   584 lemma empty_subsetI [iff]: "{} \<subseteq> A"
   585     -- {* One effect is to delete the ASSUMPTION @{prop "{} <= A"} *}
   586   by blast
   587 
   588 lemma equals0I: "(!!y. y \<in> A ==> False) ==> A = {}"
   589   by blast
   590 
   591 lemma equals0D: "A = {} ==> a \<notin> A"
   592     -- {* Use for reasoning about disjointness: @{text "A Int B = {}"} *}
   593   by blast
   594 
   595 lemma ball_empty [simp]: "Ball {} P = True"
   596   by (simp add: Ball_def)
   597 
   598 lemma bex_empty [simp]: "Bex {} P = False"
   599   by (simp add: Bex_def)
   600 
   601 lemma UNIV_not_empty [iff]: "UNIV ~= {}"
   602   by (blast elim: equalityE)
   603 
   604 
   605 subsubsection {* The Powerset operator -- Pow *}
   606 
   607 definition Pow :: "'a set => 'a set set" where
   608   Pow_def: "Pow A = {B. B \<le> A}"
   609 
   610 lemma Pow_iff [iff]: "(A \<in> Pow B) = (A \<subseteq> B)"
   611   by (simp add: Pow_def)
   612 
   613 lemma PowI: "A \<subseteq> B ==> A \<in> Pow B"
   614   by (simp add: Pow_def)
   615 
   616 lemma PowD: "A \<in> Pow B ==> A \<subseteq> B"
   617   by (simp add: Pow_def)
   618 
   619 lemma Pow_bottom: "{} \<in> Pow B"
   620   by simp
   621 
   622 lemma Pow_top: "A \<in> Pow A"
   623   by simp
   624 
   625 lemma Pow_not_empty: "Pow A \<noteq> {}"
   626   using Pow_top by blast
   627 
   628 subsubsection {* Set complement *}
   629 
   630 lemma Compl_iff [simp]: "(c \<in> -A) = (c \<notin> A)"
   631   by (simp add: mem_def fun_Compl_def bool_Compl_def)
   632 
   633 lemma ComplI [intro!]: "(c \<in> A ==> False) ==> c \<in> -A"
   634   by (unfold mem_def fun_Compl_def bool_Compl_def) blast
   635 
   636 text {*
   637   \medskip This form, with negated conclusion, works well with the
   638   Classical prover.  Negated assumptions behave like formulae on the
   639   right side of the notional turnstile ... *}
   640 
   641 lemma ComplD [dest!]: "c : -A ==> c~:A"
   642   by (simp add: mem_def fun_Compl_def bool_Compl_def)
   643 
   644 lemmas ComplE = ComplD [elim_format]
   645 
   646 lemma Compl_eq: "- A = {x. ~ x : A}" by blast
   647 
   648 
   649 subsubsection {* Binary union -- Un *}
   650 
   651 abbreviation union :: "'a set \<Rightarrow> 'a set \<Rightarrow> 'a set" (infixl "Un" 65) where
   652   "op Un \<equiv> sup"
   653 
   654 notation (xsymbols)
   655   union  (infixl "\<union>" 65)
   656 
   657 notation (HTML output)
   658   union  (infixl "\<union>" 65)
   659 
   660 lemma Un_def:
   661   "A \<union> B = {x. x \<in> A \<or> x \<in> B}"
   662   by (simp add: sup_fun_eq sup_bool_eq Collect_def mem_def)
   663 
   664 lemma Un_iff [simp]: "(c : A Un B) = (c:A | c:B)"
   665   by (unfold Un_def) blast
   666 
   667 lemma UnI1 [elim?]: "c:A ==> c : A Un B"
   668   by simp
   669 
   670 lemma UnI2 [elim?]: "c:B ==> c : A Un B"
   671   by simp
   672 
   673 text {*
   674   \medskip Classical introduction rule: no commitment to @{prop A} vs
   675   @{prop B}.
   676 *}
   677 
   678 lemma UnCI [intro!]: "(c~:B ==> c:A) ==> c : A Un B"
   679   by auto
   680 
   681 lemma UnE [elim!]: "c : A Un B ==> (c:A ==> P) ==> (c:B ==> P) ==> P"
   682   by (unfold Un_def) blast
   683 
   684 lemma insert_def: "insert a B = {x. x = a} \<union> B"
   685   by (simp add: Collect_def mem_def insert_compr Un_def)
   686 
   687 lemma mono_Un: "mono f \<Longrightarrow> f A \<union> f B \<subseteq> f (A \<union> B)"
   688   by (fact mono_sup)
   689 
   690 
   691 subsubsection {* Binary intersection -- Int *}
   692 
   693 abbreviation inter :: "'a set \<Rightarrow> 'a set \<Rightarrow> 'a set" (infixl "Int" 70) where
   694   "op Int \<equiv> inf"
   695 
   696 notation (xsymbols)
   697   inter  (infixl "\<inter>" 70)
   698 
   699 notation (HTML output)
   700   inter  (infixl "\<inter>" 70)
   701 
   702 lemma Int_def:
   703   "A \<inter> B = {x. x \<in> A \<and> x \<in> B}"
   704   by (simp add: inf_fun_eq inf_bool_eq Collect_def mem_def)
   705 
   706 lemma Int_iff [simp]: "(c : A Int B) = (c:A & c:B)"
   707   by (unfold Int_def) blast
   708 
   709 lemma IntI [intro!]: "c:A ==> c:B ==> c : A Int B"
   710   by simp
   711 
   712 lemma IntD1: "c : A Int B ==> c:A"
   713   by simp
   714 
   715 lemma IntD2: "c : A Int B ==> c:B"
   716   by simp
   717 
   718 lemma IntE [elim!]: "c : A Int B ==> (c:A ==> c:B ==> P) ==> P"
   719   by simp
   720 
   721 lemma mono_Int: "mono f \<Longrightarrow> f (A \<inter> B) \<subseteq> f A \<inter> f B"
   722   by (fact mono_inf)
   723 
   724 
   725 subsubsection {* Set difference *}
   726 
   727 lemma Diff_iff [simp]: "(c : A - B) = (c:A & c~:B)"
   728   by (simp add: mem_def fun_diff_def bool_diff_def)
   729 
   730 lemma DiffI [intro!]: "c : A ==> c ~: B ==> c : A - B"
   731   by simp
   732 
   733 lemma DiffD1: "c : A - B ==> c : A"
   734   by simp
   735 
   736 lemma DiffD2: "c : A - B ==> c : B ==> P"
   737   by simp
   738 
   739 lemma DiffE [elim!]: "c : A - B ==> (c:A ==> c~:B ==> P) ==> P"
   740   by simp
   741 
   742 lemma set_diff_eq: "A - B = {x. x : A & ~ x : B}" by blast
   743 
   744 lemma Compl_eq_Diff_UNIV: "-A = (UNIV - A)"
   745 by blast
   746 
   747 
   748 subsubsection {* Augmenting a set -- @{const insert} *}
   749 
   750 lemma insert_iff [simp]: "(a : insert b A) = (a = b | a:A)"
   751   by (unfold insert_def) blast
   752 
   753 lemma insertI1: "a : insert a B"
   754   by simp
   755 
   756 lemma insertI2: "a : B ==> a : insert b B"
   757   by simp
   758 
   759 lemma insertE [elim!]: "a : insert b A ==> (a = b ==> P) ==> (a:A ==> P) ==> P"
   760   by (unfold insert_def) blast
   761 
   762 lemma insertCI [intro!]: "(a~:B ==> a = b) ==> a: insert b B"
   763   -- {* Classical introduction rule. *}
   764   by auto
   765 
   766 lemma subset_insert_iff: "(A \<subseteq> insert x B) = (if x:A then A - {x} \<subseteq> B else A \<subseteq> B)"
   767   by auto
   768 
   769 lemma set_insert:
   770   assumes "x \<in> A"
   771   obtains B where "A = insert x B" and "x \<notin> B"
   772 proof
   773   from assms show "A = insert x (A - {x})" by blast
   774 next
   775   show "x \<notin> A - {x}" by blast
   776 qed
   777 
   778 lemma insert_ident: "x ~: A ==> x ~: B ==> (insert x A = insert x B) = (A = B)"
   779 by auto
   780 
   781 subsubsection {* Singletons, using insert *}
   782 
   783 lemma singletonI [intro!,no_atp]: "a : {a}"
   784     -- {* Redundant? But unlike @{text insertCI}, it proves the subgoal immediately! *}
   785   by (rule insertI1)
   786 
   787 lemma singletonD [dest!,no_atp]: "b : {a} ==> b = a"
   788   by blast
   789 
   790 lemmas singletonE = singletonD [elim_format]
   791 
   792 lemma singleton_iff: "(b : {a}) = (b = a)"
   793   by blast
   794 
   795 lemma singleton_inject [dest!]: "{a} = {b} ==> a = b"
   796   by blast
   797 
   798 lemma singleton_insert_inj_eq [iff,no_atp]:
   799      "({b} = insert a A) = (a = b & A \<subseteq> {b})"
   800   by blast
   801 
   802 lemma singleton_insert_inj_eq' [iff,no_atp]:
   803      "(insert a A = {b}) = (a = b & A \<subseteq> {b})"
   804   by blast
   805 
   806 lemma subset_singletonD: "A \<subseteq> {x} ==> A = {} | A = {x}"
   807   by fast
   808 
   809 lemma singleton_conv [simp]: "{x. x = a} = {a}"
   810   by blast
   811 
   812 lemma singleton_conv2 [simp]: "{x. a = x} = {a}"
   813   by blast
   814 
   815 lemma diff_single_insert: "A - {x} \<subseteq> B ==> x \<in> A ==> A \<subseteq> insert x B"
   816   by blast
   817 
   818 lemma doubleton_eq_iff: "({a,b} = {c,d}) = (a=c & b=d | a=d & b=c)"
   819   by (blast elim: equalityE)
   820 
   821 
   822 subsubsection {* Image of a set under a function *}
   823 
   824 text {*
   825   Frequently @{term b} does not have the syntactic form of @{term "f x"}.
   826 *}
   827 
   828 definition image :: "('a => 'b) => 'a set => 'b set" (infixr "`" 90) where
   829   image_def [no_atp]: "f ` A = {y. EX x:A. y = f(x)}"
   830 
   831 abbreviation
   832   range :: "('a => 'b) => 'b set" where -- "of function"
   833   "range f == f ` UNIV"
   834 
   835 lemma image_eqI [simp, intro]: "b = f x ==> x:A ==> b : f`A"
   836   by (unfold image_def) blast
   837 
   838 lemma imageI: "x : A ==> f x : f ` A"
   839   by (rule image_eqI) (rule refl)
   840 
   841 lemma rev_image_eqI: "x:A ==> b = f x ==> b : f`A"
   842   -- {* This version's more effective when we already have the
   843     required @{term x}. *}
   844   by (unfold image_def) blast
   845 
   846 lemma imageE [elim!]:
   847   "b : (%x. f x)`A ==> (!!x. b = f x ==> x:A ==> P) ==> P"
   848   -- {* The eta-expansion gives variable-name preservation. *}
   849   by (unfold image_def) blast
   850 
   851 lemma image_Un: "f`(A Un B) = f`A Un f`B"
   852   by blast
   853 
   854 lemma image_iff: "(z : f`A) = (EX x:A. z = f x)"
   855   by blast
   856 
   857 lemma image_subset_iff [no_atp]: "(f`A \<subseteq> B) = (\<forall>x\<in>A. f x \<in> B)"
   858   -- {* This rewrite rule would confuse users if made default. *}
   859   by blast
   860 
   861 lemma subset_image_iff: "(B \<subseteq> f`A) = (EX AA. AA \<subseteq> A & B = f`AA)"
   862   apply safe
   863    prefer 2 apply fast
   864   apply (rule_tac x = "{a. a : A & f a : B}" in exI, fast)
   865   done
   866 
   867 lemma image_subsetI: "(!!x. x \<in> A ==> f x \<in> B) ==> f`A \<subseteq> B"
   868   -- {* Replaces the three steps @{text subsetI}, @{text imageE},
   869     @{text hypsubst}, but breaks too many existing proofs. *}
   870   by blast
   871 
   872 text {*
   873   \medskip Range of a function -- just a translation for image!
   874 *}
   875 
   876 lemma range_eqI: "b = f x ==> b \<in> range f"
   877   by simp
   878 
   879 lemma rangeI: "f x \<in> range f"
   880   by simp
   881 
   882 lemma rangeE [elim?]: "b \<in> range (\<lambda>x. f x) ==> (!!x. b = f x ==> P) ==> P"
   883   by blast
   884 
   885 subsubsection {* Some rules with @{text "if"} *}
   886 
   887 text{* Elimination of @{text"{x. \<dots> & x=t & \<dots>}"}. *}
   888 
   889 lemma Collect_conv_if: "{x. x=a & P x} = (if P a then {a} else {})"
   890   by auto
   891 
   892 lemma Collect_conv_if2: "{x. a=x & P x} = (if P a then {a} else {})"
   893   by auto
   894 
   895 text {*
   896   Rewrite rules for boolean case-splitting: faster than @{text
   897   "split_if [split]"}.
   898 *}
   899 
   900 lemma split_if_eq1: "((if Q then x else y) = b) = ((Q --> x = b) & (~ Q --> y = b))"
   901   by (rule split_if)
   902 
   903 lemma split_if_eq2: "(a = (if Q then x else y)) = ((Q --> a = x) & (~ Q --> a = y))"
   904   by (rule split_if)
   905 
   906 text {*
   907   Split ifs on either side of the membership relation.  Not for @{text
   908   "[simp]"} -- can cause goals to blow up!
   909 *}
   910 
   911 lemma split_if_mem1: "((if Q then x else y) : b) = ((Q --> x : b) & (~ Q --> y : b))"
   912   by (rule split_if)
   913 
   914 lemma split_if_mem2: "(a : (if Q then x else y)) = ((Q --> a : x) & (~ Q --> a : y))"
   915   by (rule split_if [where P="%S. a : S"])
   916 
   917 lemmas split_ifs = if_bool_eq_conj split_if_eq1 split_if_eq2 split_if_mem1 split_if_mem2
   918 
   919 (*Would like to add these, but the existing code only searches for the
   920   outer-level constant, which in this case is just Set.member; we instead need
   921   to use term-nets to associate patterns with rules.  Also, if a rule fails to
   922   apply, then the formula should be kept.
   923   [("uminus", Compl_iff RS iffD1), ("minus", [Diff_iff RS iffD1]),
   924    ("Int", [IntD1,IntD2]),
   925    ("Collect", [CollectD]), ("Inter", [InterD]), ("INTER", [INT_D])]
   926  *)
   927 
   928 
   929 subsection {* Further operations and lemmas *}
   930 
   931 subsubsection {* The ``proper subset'' relation *}
   932 
   933 lemma psubsetI [intro!,no_atp]: "A \<subseteq> B ==> A \<noteq> B ==> A \<subset> B"
   934   by (unfold less_le) blast
   935 
   936 lemma psubsetE [elim!,no_atp]: 
   937     "[|A \<subset> B;  [|A \<subseteq> B; ~ (B\<subseteq>A)|] ==> R|] ==> R"
   938   by (unfold less_le) blast
   939 
   940 lemma psubset_insert_iff:
   941   "(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)"
   942   by (auto simp add: less_le subset_insert_iff)
   943 
   944 lemma psubset_eq: "(A \<subset> B) = (A \<subseteq> B & A \<noteq> B)"
   945   by (simp only: less_le)
   946 
   947 lemma psubset_imp_subset: "A \<subset> B ==> A \<subseteq> B"
   948   by (simp add: psubset_eq)
   949 
   950 lemma psubset_trans: "[| A \<subset> B; B \<subset> C |] ==> A \<subset> C"
   951 apply (unfold less_le)
   952 apply (auto dest: subset_antisym)
   953 done
   954 
   955 lemma psubsetD: "[| A \<subset> B; c \<in> A |] ==> c \<in> B"
   956 apply (unfold less_le)
   957 apply (auto dest: subsetD)
   958 done
   959 
   960 lemma psubset_subset_trans: "A \<subset> B ==> B \<subseteq> C ==> A \<subset> C"
   961   by (auto simp add: psubset_eq)
   962 
   963 lemma subset_psubset_trans: "A \<subseteq> B ==> B \<subset> C ==> A \<subset> C"
   964   by (auto simp add: psubset_eq)
   965 
   966 lemma psubset_imp_ex_mem: "A \<subset> B ==> \<exists>b. b \<in> (B - A)"
   967   by (unfold less_le) blast
   968 
   969 lemma atomize_ball:
   970     "(!!x. x \<in> A ==> P x) == Trueprop (\<forall>x\<in>A. P x)"
   971   by (simp only: Ball_def atomize_all atomize_imp)
   972 
   973 lemmas [symmetric, rulify] = atomize_ball
   974   and [symmetric, defn] = atomize_ball
   975 
   976 lemma image_Pow_mono:
   977   assumes "f ` A \<le> B"
   978   shows "(image f) ` (Pow A) \<le> Pow B"
   979 using assms by blast
   980 
   981 lemma image_Pow_surj:
   982   assumes "f ` A = B"
   983   shows "(image f) ` (Pow A) = Pow B"
   984 using assms unfolding Pow_def proof(auto)
   985   fix Y assume *: "Y \<le> f ` A"
   986   obtain X where X_def: "X = {x \<in> A. f x \<in> Y}" by blast
   987   have "f ` X = Y \<and> X \<le> A" unfolding X_def using * by auto
   988   thus "Y \<in> (image f) ` {X. X \<le> A}" by blast
   989 qed
   990 
   991 subsubsection {* Derived rules involving subsets. *}
   992 
   993 text {* @{text insert}. *}
   994 
   995 lemma subset_insertI: "B \<subseteq> insert a B"
   996   by (rule subsetI) (erule insertI2)
   997 
   998 lemma subset_insertI2: "A \<subseteq> B \<Longrightarrow> A \<subseteq> insert b B"
   999   by blast
  1000 
  1001 lemma subset_insert: "x \<notin> A ==> (A \<subseteq> insert x B) = (A \<subseteq> B)"
  1002   by blast
  1003 
  1004 
  1005 text {* \medskip Finite Union -- the least upper bound of two sets. *}
  1006 
  1007 lemma Un_upper1: "A \<subseteq> A \<union> B"
  1008   by (fact sup_ge1)
  1009 
  1010 lemma Un_upper2: "B \<subseteq> A \<union> B"
  1011   by (fact sup_ge2)
  1012 
  1013 lemma Un_least: "A \<subseteq> C ==> B \<subseteq> C ==> A \<union> B \<subseteq> C"
  1014   by (fact sup_least)
  1015 
  1016 
  1017 text {* \medskip Finite Intersection -- the greatest lower bound of two sets. *}
  1018 
  1019 lemma Int_lower1: "A \<inter> B \<subseteq> A"
  1020   by (fact inf_le1)
  1021 
  1022 lemma Int_lower2: "A \<inter> B \<subseteq> B"
  1023   by (fact inf_le2)
  1024 
  1025 lemma Int_greatest: "C \<subseteq> A ==> C \<subseteq> B ==> C \<subseteq> A \<inter> B"
  1026   by (fact inf_greatest)
  1027 
  1028 
  1029 text {* \medskip Set difference. *}
  1030 
  1031 lemma Diff_subset: "A - B \<subseteq> A"
  1032   by blast
  1033 
  1034 lemma Diff_subset_conv: "(A - B \<subseteq> C) = (A \<subseteq> B \<union> C)"
  1035 by blast
  1036 
  1037 
  1038 subsubsection {* Equalities involving union, intersection, inclusion, etc. *}
  1039 
  1040 text {* @{text "{}"}. *}
  1041 
  1042 lemma Collect_const [simp]: "{s. P} = (if P then UNIV else {})"
  1043   -- {* supersedes @{text "Collect_False_empty"} *}
  1044   by auto
  1045 
  1046 lemma subset_empty [simp]: "(A \<subseteq> {}) = (A = {})"
  1047   by blast
  1048 
  1049 lemma not_psubset_empty [iff]: "\<not> (A < {})"
  1050   by (unfold less_le) blast
  1051 
  1052 lemma Collect_empty_eq [simp]: "(Collect P = {}) = (\<forall>x. \<not> P x)"
  1053 by blast
  1054 
  1055 lemma empty_Collect_eq [simp]: "({} = Collect P) = (\<forall>x. \<not> P x)"
  1056 by blast
  1057 
  1058 lemma Collect_neg_eq: "{x. \<not> P x} = - {x. P x}"
  1059   by blast
  1060 
  1061 lemma Collect_disj_eq: "{x. P x | Q x} = {x. P x} \<union> {x. Q x}"
  1062   by blast
  1063 
  1064 lemma Collect_imp_eq: "{x. P x --> Q x} = -{x. P x} \<union> {x. Q x}"
  1065   by blast
  1066 
  1067 lemma Collect_conj_eq: "{x. P x & Q x} = {x. P x} \<inter> {x. Q x}"
  1068   by blast
  1069 
  1070 
  1071 text {* \medskip @{text insert}. *}
  1072 
  1073 lemma insert_is_Un: "insert a A = {a} Un A"
  1074   -- {* NOT SUITABLE FOR REWRITING since @{text "{a} == insert a {}"} *}
  1075   by blast
  1076 
  1077 lemma insert_not_empty [simp]: "insert a A \<noteq> {}"
  1078   by blast
  1079 
  1080 lemmas empty_not_insert = insert_not_empty [symmetric, standard]
  1081 declare empty_not_insert [simp]
  1082 
  1083 lemma insert_absorb: "a \<in> A ==> insert a A = A"
  1084   -- {* @{text "[simp]"} causes recursive calls when there are nested inserts *}
  1085   -- {* with \emph{quadratic} running time *}
  1086   by blast
  1087 
  1088 lemma insert_absorb2 [simp]: "insert x (insert x A) = insert x A"
  1089   by blast
  1090 
  1091 lemma insert_commute: "insert x (insert y A) = insert y (insert x A)"
  1092   by blast
  1093 
  1094 lemma insert_subset [simp]: "(insert x A \<subseteq> B) = (x \<in> B & A \<subseteq> B)"
  1095   by blast
  1096 
  1097 lemma mk_disjoint_insert: "a \<in> A ==> \<exists>B. A = insert a B & a \<notin> B"
  1098   -- {* use new @{text B} rather than @{text "A - {a}"} to avoid infinite unfolding *}
  1099   apply (rule_tac x = "A - {a}" in exI, blast)
  1100   done
  1101 
  1102 lemma insert_Collect: "insert a (Collect P) = {u. u \<noteq> a --> P u}"
  1103   by auto
  1104 
  1105 lemma insert_inter_insert[simp]: "insert a A \<inter> insert a B = insert a (A \<inter> B)"
  1106   by blast
  1107 
  1108 lemma insert_disjoint [simp,no_atp]:
  1109  "(insert a A \<inter> B = {}) = (a \<notin> B \<and> A \<inter> B = {})"
  1110  "({} = insert a A \<inter> B) = (a \<notin> B \<and> {} = A \<inter> B)"
  1111   by auto
  1112 
  1113 lemma disjoint_insert [simp,no_atp]:
  1114  "(B \<inter> insert a A = {}) = (a \<notin> B \<and> B \<inter> A = {})"
  1115  "({} = A \<inter> insert b B) = (b \<notin> A \<and> {} = A \<inter> B)"
  1116   by auto
  1117 
  1118 text {* \medskip @{text image}. *}
  1119 
  1120 lemma image_empty [simp]: "f`{} = {}"
  1121   by blast
  1122 
  1123 lemma image_insert [simp]: "f ` insert a B = insert (f a) (f`B)"
  1124   by blast
  1125 
  1126 lemma image_constant: "x \<in> A ==> (\<lambda>x. c) ` A = {c}"
  1127   by auto
  1128 
  1129 lemma image_constant_conv: "(%x. c) ` A = (if A = {} then {} else {c})"
  1130 by auto
  1131 
  1132 lemma image_image: "f ` (g ` A) = (\<lambda>x. f (g x)) ` A"
  1133 by blast
  1134 
  1135 lemma insert_image [simp]: "x \<in> A ==> insert (f x) (f`A) = f`A"
  1136 by blast
  1137 
  1138 lemma image_is_empty [iff]: "(f`A = {}) = (A = {})"
  1139 by blast
  1140 
  1141 lemma empty_is_image[iff]: "({} = f ` A) = (A = {})"
  1142 by blast
  1143 
  1144 
  1145 lemma image_Collect [no_atp]: "f ` {x. P x} = {f x | x. P x}"
  1146   -- {* NOT suitable as a default simprule: the RHS isn't simpler than the LHS,
  1147       with its implicit quantifier and conjunction.  Also image enjoys better
  1148       equational properties than does the RHS. *}
  1149   by blast
  1150 
  1151 lemma if_image_distrib [simp]:
  1152   "(\<lambda>x. if P x then f x else g x) ` S
  1153     = (f ` (S \<inter> {x. P x})) \<union> (g ` (S \<inter> {x. \<not> P x}))"
  1154   by (auto simp add: image_def)
  1155 
  1156 lemma image_cong: "M = N ==> (!!x. x \<in> N ==> f x = g x) ==> f`M = g`N"
  1157   by (simp add: image_def)
  1158 
  1159 
  1160 text {* \medskip @{text range}. *}
  1161 
  1162 lemma full_SetCompr_eq [no_atp]: "{u. \<exists>x. u = f x} = range f"
  1163   by auto
  1164 
  1165 lemma range_composition: "range (\<lambda>x. f (g x)) = f`range g"
  1166 by (subst image_image, simp)
  1167 
  1168 
  1169 text {* \medskip @{text Int} *}
  1170 
  1171 lemma Int_absorb [simp]: "A \<inter> A = A"
  1172   by (fact inf_idem)
  1173 
  1174 lemma Int_left_absorb: "A \<inter> (A \<inter> B) = A \<inter> B"
  1175   by (fact inf_left_idem)
  1176 
  1177 lemma Int_commute: "A \<inter> B = B \<inter> A"
  1178   by (fact inf_commute)
  1179 
  1180 lemma Int_left_commute: "A \<inter> (B \<inter> C) = B \<inter> (A \<inter> C)"
  1181   by (fact inf_left_commute)
  1182 
  1183 lemma Int_assoc: "(A \<inter> B) \<inter> C = A \<inter> (B \<inter> C)"
  1184   by (fact inf_assoc)
  1185 
  1186 lemmas Int_ac = Int_assoc Int_left_absorb Int_commute Int_left_commute
  1187   -- {* Intersection is an AC-operator *}
  1188 
  1189 lemma Int_absorb1: "B \<subseteq> A ==> A \<inter> B = B"
  1190   by (fact inf_absorb2)
  1191 
  1192 lemma Int_absorb2: "A \<subseteq> B ==> A \<inter> B = A"
  1193   by (fact inf_absorb1)
  1194 
  1195 lemma Int_empty_left [simp]: "{} \<inter> B = {}"
  1196   by (fact inf_bot_left)
  1197 
  1198 lemma Int_empty_right [simp]: "A \<inter> {} = {}"
  1199   by (fact inf_bot_right)
  1200 
  1201 lemma disjoint_eq_subset_Compl: "(A \<inter> B = {}) = (A \<subseteq> -B)"
  1202   by blast
  1203 
  1204 lemma disjoint_iff_not_equal: "(A \<inter> B = {}) = (\<forall>x\<in>A. \<forall>y\<in>B. x \<noteq> y)"
  1205   by blast
  1206 
  1207 lemma Int_UNIV_left [simp]: "UNIV \<inter> B = B"
  1208   by (fact inf_top_left)
  1209 
  1210 lemma Int_UNIV_right [simp]: "A \<inter> UNIV = A"
  1211   by (fact inf_top_right)
  1212 
  1213 lemma Int_Un_distrib: "A \<inter> (B \<union> C) = (A \<inter> B) \<union> (A \<inter> C)"
  1214   by (fact inf_sup_distrib1)
  1215 
  1216 lemma Int_Un_distrib2: "(B \<union> C) \<inter> A = (B \<inter> A) \<union> (C \<inter> A)"
  1217   by (fact inf_sup_distrib2)
  1218 
  1219 lemma Int_UNIV [simp,no_atp]: "(A \<inter> B = UNIV) = (A = UNIV & B = UNIV)"
  1220   by (fact inf_eq_top_iff)
  1221 
  1222 lemma Int_subset_iff [no_atp, simp]: "(C \<subseteq> A \<inter> B) = (C \<subseteq> A & C \<subseteq> B)"
  1223   by (fact le_inf_iff)
  1224 
  1225 lemma Int_Collect: "(x \<in> A \<inter> {x. P x}) = (x \<in> A & P x)"
  1226   by blast
  1227 
  1228 
  1229 text {* \medskip @{text Un}. *}
  1230 
  1231 lemma Un_absorb [simp]: "A \<union> A = A"
  1232   by (fact sup_idem)
  1233 
  1234 lemma Un_left_absorb: "A \<union> (A \<union> B) = A \<union> B"
  1235   by (fact sup_left_idem)
  1236 
  1237 lemma Un_commute: "A \<union> B = B \<union> A"
  1238   by (fact sup_commute)
  1239 
  1240 lemma Un_left_commute: "A \<union> (B \<union> C) = B \<union> (A \<union> C)"
  1241   by (fact sup_left_commute)
  1242 
  1243 lemma Un_assoc: "(A \<union> B) \<union> C = A \<union> (B \<union> C)"
  1244   by (fact sup_assoc)
  1245 
  1246 lemmas Un_ac = Un_assoc Un_left_absorb Un_commute Un_left_commute
  1247   -- {* Union is an AC-operator *}
  1248 
  1249 lemma Un_absorb1: "A \<subseteq> B ==> A \<union> B = B"
  1250   by (fact sup_absorb2)
  1251 
  1252 lemma Un_absorb2: "B \<subseteq> A ==> A \<union> B = A"
  1253   by (fact sup_absorb1)
  1254 
  1255 lemma Un_empty_left [simp]: "{} \<union> B = B"
  1256   by (fact sup_bot_left)
  1257 
  1258 lemma Un_empty_right [simp]: "A \<union> {} = A"
  1259   by (fact sup_bot_right)
  1260 
  1261 lemma Un_UNIV_left [simp]: "UNIV \<union> B = UNIV"
  1262   by (fact sup_top_left)
  1263 
  1264 lemma Un_UNIV_right [simp]: "A \<union> UNIV = UNIV"
  1265   by (fact sup_top_right)
  1266 
  1267 lemma Un_insert_left [simp]: "(insert a B) \<union> C = insert a (B \<union> C)"
  1268   by blast
  1269 
  1270 lemma Un_insert_right [simp]: "A \<union> (insert a B) = insert a (A \<union> B)"
  1271   by blast
  1272 
  1273 lemma Int_insert_left:
  1274     "(insert a B) Int C = (if a \<in> C then insert a (B \<inter> C) else B \<inter> C)"
  1275   by auto
  1276 
  1277 lemma Int_insert_left_if0[simp]:
  1278     "a \<notin> C \<Longrightarrow> (insert a B) Int C = B \<inter> C"
  1279   by auto
  1280 
  1281 lemma Int_insert_left_if1[simp]:
  1282     "a \<in> C \<Longrightarrow> (insert a B) Int C = insert a (B Int C)"
  1283   by auto
  1284 
  1285 lemma Int_insert_right:
  1286     "A \<inter> (insert a B) = (if a \<in> A then insert a (A \<inter> B) else A \<inter> B)"
  1287   by auto
  1288 
  1289 lemma Int_insert_right_if0[simp]:
  1290     "a \<notin> A \<Longrightarrow> A Int (insert a B) = A Int B"
  1291   by auto
  1292 
  1293 lemma Int_insert_right_if1[simp]:
  1294     "a \<in> A \<Longrightarrow> A Int (insert a B) = insert a (A Int B)"
  1295   by auto
  1296 
  1297 lemma Un_Int_distrib: "A \<union> (B \<inter> C) = (A \<union> B) \<inter> (A \<union> C)"
  1298   by (fact sup_inf_distrib1)
  1299 
  1300 lemma Un_Int_distrib2: "(B \<inter> C) \<union> A = (B \<union> A) \<inter> (C \<union> A)"
  1301   by (fact sup_inf_distrib2)
  1302 
  1303 lemma Un_Int_crazy:
  1304     "(A \<inter> B) \<union> (B \<inter> C) \<union> (C \<inter> A) = (A \<union> B) \<inter> (B \<union> C) \<inter> (C \<union> A)"
  1305   by blast
  1306 
  1307 lemma subset_Un_eq: "(A \<subseteq> B) = (A \<union> B = B)"
  1308   by (fact le_iff_sup)
  1309 
  1310 lemma Un_empty [iff]: "(A \<union> B = {}) = (A = {} & B = {})"
  1311   by (fact sup_eq_bot_iff)
  1312 
  1313 lemma Un_subset_iff [no_atp, simp]: "(A \<union> B \<subseteq> C) = (A \<subseteq> C & B \<subseteq> C)"
  1314   by (fact le_sup_iff)
  1315 
  1316 lemma Un_Diff_Int: "(A - B) \<union> (A \<inter> B) = A"
  1317   by blast
  1318 
  1319 lemma Diff_Int2: "A \<inter> C - B \<inter> C = A \<inter> C - B"
  1320   by blast
  1321 
  1322 
  1323 text {* \medskip Set complement *}
  1324 
  1325 lemma Compl_disjoint [simp]: "A \<inter> -A = {}"
  1326   by (fact inf_compl_bot)
  1327 
  1328 lemma Compl_disjoint2 [simp]: "-A \<inter> A = {}"
  1329   by (fact compl_inf_bot)
  1330 
  1331 lemma Compl_partition: "A \<union> -A = UNIV"
  1332   by (fact sup_compl_top)
  1333 
  1334 lemma Compl_partition2: "-A \<union> A = UNIV"
  1335   by (fact compl_sup_top)
  1336 
  1337 lemma double_complement [simp]: "- (-A) = (A::'a set)"
  1338   by (fact double_compl)
  1339 
  1340 lemma Compl_Un [simp]: "-(A \<union> B) = (-A) \<inter> (-B)"
  1341   by (fact compl_sup)
  1342 
  1343 lemma Compl_Int [simp]: "-(A \<inter> B) = (-A) \<union> (-B)"
  1344   by (fact compl_inf)
  1345 
  1346 lemma subset_Compl_self_eq: "(A \<subseteq> -A) = (A = {})"
  1347   by blast
  1348 
  1349 lemma Un_Int_assoc_eq: "((A \<inter> B) \<union> C = A \<inter> (B \<union> C)) = (C \<subseteq> A)"
  1350   -- {* Halmos, Naive Set Theory, page 16. *}
  1351   by blast
  1352 
  1353 lemma Compl_UNIV_eq [simp]: "-UNIV = {}"
  1354   by (fact compl_top_eq)
  1355 
  1356 lemma Compl_empty_eq [simp]: "-{} = UNIV"
  1357   by (fact compl_bot_eq)
  1358 
  1359 lemma Compl_subset_Compl_iff [iff]: "(-A \<subseteq> -B) = (B \<subseteq> A)"
  1360   by (fact compl_le_compl_iff)
  1361 
  1362 lemma Compl_eq_Compl_iff [iff]: "(-A = -B) = (A = (B::'a set))"
  1363   by (fact compl_eq_compl_iff)
  1364 
  1365 text {* \medskip Bounded quantifiers.
  1366 
  1367   The following are not added to the default simpset because
  1368   (a) they duplicate the body and (b) there are no similar rules for @{text Int}. *}
  1369 
  1370 lemma ball_Un: "(\<forall>x \<in> A \<union> B. P x) = ((\<forall>x\<in>A. P x) & (\<forall>x\<in>B. P x))"
  1371   by blast
  1372 
  1373 lemma bex_Un: "(\<exists>x \<in> A \<union> B. P x) = ((\<exists>x\<in>A. P x) | (\<exists>x\<in>B. P x))"
  1374   by blast
  1375 
  1376 
  1377 text {* \medskip Set difference. *}
  1378 
  1379 lemma Diff_eq: "A - B = A \<inter> (-B)"
  1380   by blast
  1381 
  1382 lemma Diff_eq_empty_iff [simp,no_atp]: "(A - B = {}) = (A \<subseteq> B)"
  1383   by blast
  1384 
  1385 lemma Diff_cancel [simp]: "A - A = {}"
  1386   by blast
  1387 
  1388 lemma Diff_idemp [simp]: "(A - B) - B = A - (B::'a set)"
  1389 by blast
  1390 
  1391 lemma Diff_triv: "A \<inter> B = {} ==> A - B = A"
  1392   by (blast elim: equalityE)
  1393 
  1394 lemma empty_Diff [simp]: "{} - A = {}"
  1395   by blast
  1396 
  1397 lemma Diff_empty [simp]: "A - {} = A"
  1398   by blast
  1399 
  1400 lemma Diff_UNIV [simp]: "A - UNIV = {}"
  1401   by blast
  1402 
  1403 lemma Diff_insert0 [simp,no_atp]: "x \<notin> A ==> A - insert x B = A - B"
  1404   by blast
  1405 
  1406 lemma Diff_insert: "A - insert a B = A - B - {a}"
  1407   -- {* NOT SUITABLE FOR REWRITING since @{text "{a} == insert a 0"} *}
  1408   by blast
  1409 
  1410 lemma Diff_insert2: "A - insert a B = A - {a} - B"
  1411   -- {* NOT SUITABLE FOR REWRITING since @{text "{a} == insert a 0"} *}
  1412   by blast
  1413 
  1414 lemma insert_Diff_if: "insert x A - B = (if x \<in> B then A - B else insert x (A - B))"
  1415   by auto
  1416 
  1417 lemma insert_Diff1 [simp]: "x \<in> B ==> insert x A - B = A - B"
  1418   by blast
  1419 
  1420 lemma insert_Diff_single[simp]: "insert a (A - {a}) = insert a A"
  1421 by blast
  1422 
  1423 lemma insert_Diff: "a \<in> A ==> insert a (A - {a}) = A"
  1424   by blast
  1425 
  1426 lemma Diff_insert_absorb: "x \<notin> A ==> (insert x A) - {x} = A"
  1427   by auto
  1428 
  1429 lemma Diff_disjoint [simp]: "A \<inter> (B - A) = {}"
  1430   by blast
  1431 
  1432 lemma Diff_partition: "A \<subseteq> B ==> A \<union> (B - A) = B"
  1433   by blast
  1434 
  1435 lemma double_diff: "A \<subseteq> B ==> B \<subseteq> C ==> B - (C - A) = A"
  1436   by blast
  1437 
  1438 lemma Un_Diff_cancel [simp]: "A \<union> (B - A) = A \<union> B"
  1439   by blast
  1440 
  1441 lemma Un_Diff_cancel2 [simp]: "(B - A) \<union> A = B \<union> A"
  1442   by blast
  1443 
  1444 lemma Diff_Un: "A - (B \<union> C) = (A - B) \<inter> (A - C)"
  1445   by blast
  1446 
  1447 lemma Diff_Int: "A - (B \<inter> C) = (A - B) \<union> (A - C)"
  1448   by blast
  1449 
  1450 lemma Un_Diff: "(A \<union> B) - C = (A - C) \<union> (B - C)"
  1451   by blast
  1452 
  1453 lemma Int_Diff: "(A \<inter> B) - C = A \<inter> (B - C)"
  1454   by blast
  1455 
  1456 lemma Diff_Int_distrib: "C \<inter> (A - B) = (C \<inter> A) - (C \<inter> B)"
  1457   by blast
  1458 
  1459 lemma Diff_Int_distrib2: "(A - B) \<inter> C = (A \<inter> C) - (B \<inter> C)"
  1460   by blast
  1461 
  1462 lemma Diff_Compl [simp]: "A - (- B) = A \<inter> B"
  1463   by auto
  1464 
  1465 lemma Compl_Diff_eq [simp]: "- (A - B) = -A \<union> B"
  1466   by blast
  1467 
  1468 
  1469 text {* \medskip Quantification over type @{typ bool}. *}
  1470 
  1471 lemma bool_induct: "P True \<Longrightarrow> P False \<Longrightarrow> P x"
  1472   by (cases x) auto
  1473 
  1474 lemma all_bool_eq: "(\<forall>b. P b) \<longleftrightarrow> P True \<and> P False"
  1475   by (auto intro: bool_induct)
  1476 
  1477 lemma bool_contrapos: "P x \<Longrightarrow> \<not> P False \<Longrightarrow> P True"
  1478   by (cases x) auto
  1479 
  1480 lemma ex_bool_eq: "(\<exists>b. P b) \<longleftrightarrow> P True \<or> P False"
  1481   by (auto intro: bool_contrapos)
  1482 
  1483 text {* \medskip @{text Pow} *}
  1484 
  1485 lemma Pow_empty [simp]: "Pow {} = {{}}"
  1486   by (auto simp add: Pow_def)
  1487 
  1488 lemma Pow_insert: "Pow (insert a A) = Pow A \<union> (insert a ` Pow A)"
  1489   by (blast intro: image_eqI [where ?x = "u - {a}", standard])
  1490 
  1491 lemma Pow_Compl: "Pow (- A) = {-B | B. A \<in> Pow B}"
  1492   by (blast intro: exI [where ?x = "- u", standard])
  1493 
  1494 lemma Pow_UNIV [simp]: "Pow UNIV = UNIV"
  1495   by blast
  1496 
  1497 lemma Un_Pow_subset: "Pow A \<union> Pow B \<subseteq> Pow (A \<union> B)"
  1498   by blast
  1499 
  1500 lemma Pow_Int_eq [simp]: "Pow (A \<inter> B) = Pow A \<inter> Pow B"
  1501   by blast
  1502 
  1503 
  1504 text {* \medskip Miscellany. *}
  1505 
  1506 lemma set_eq_subset: "(A = B) = (A \<subseteq> B & B \<subseteq> A)"
  1507   by blast
  1508 
  1509 lemma subset_iff [no_atp]: "(A \<subseteq> B) = (\<forall>t. t \<in> A --> t \<in> B)"
  1510   by blast
  1511 
  1512 lemma subset_iff_psubset_eq: "(A \<subseteq> B) = ((A \<subset> B) | (A = B))"
  1513   by (unfold less_le) blast
  1514 
  1515 lemma all_not_in_conv [simp]: "(\<forall>x. x \<notin> A) = (A = {})"
  1516   by blast
  1517 
  1518 lemma ex_in_conv: "(\<exists>x. x \<in> A) = (A \<noteq> {})"
  1519   by blast
  1520 
  1521 lemma distinct_lemma: "f x \<noteq> f y ==> x \<noteq> y"
  1522   by iprover
  1523 
  1524 
  1525 subsubsection {* Monotonicity of various operations *}
  1526 
  1527 lemma image_mono: "A \<subseteq> B ==> f`A \<subseteq> f`B"
  1528   by blast
  1529 
  1530 lemma Pow_mono: "A \<subseteq> B ==> Pow A \<subseteq> Pow B"
  1531   by blast
  1532 
  1533 lemma insert_mono: "C \<subseteq> D ==> insert a C \<subseteq> insert a D"
  1534   by blast
  1535 
  1536 lemma Un_mono: "A \<subseteq> C ==> B \<subseteq> D ==> A \<union> B \<subseteq> C \<union> D"
  1537   by (fact sup_mono)
  1538 
  1539 lemma Int_mono: "A \<subseteq> C ==> B \<subseteq> D ==> A \<inter> B \<subseteq> C \<inter> D"
  1540   by (fact inf_mono)
  1541 
  1542 lemma Diff_mono: "A \<subseteq> C ==> D \<subseteq> B ==> A - B \<subseteq> C - D"
  1543   by blast
  1544 
  1545 lemma Compl_anti_mono: "A \<subseteq> B ==> -B \<subseteq> -A"
  1546   by (fact compl_mono)
  1547 
  1548 text {* \medskip Monotonicity of implications. *}
  1549 
  1550 lemma in_mono: "A \<subseteq> B ==> x \<in> A --> x \<in> B"
  1551   apply (rule impI)
  1552   apply (erule subsetD, assumption)
  1553   done
  1554 
  1555 lemma conj_mono: "P1 --> Q1 ==> P2 --> Q2 ==> (P1 & P2) --> (Q1 & Q2)"
  1556   by iprover
  1557 
  1558 lemma disj_mono: "P1 --> Q1 ==> P2 --> Q2 ==> (P1 | P2) --> (Q1 | Q2)"
  1559   by iprover
  1560 
  1561 lemma imp_mono: "Q1 --> P1 ==> P2 --> Q2 ==> (P1 --> P2) --> (Q1 --> Q2)"
  1562   by iprover
  1563 
  1564 lemma imp_refl: "P --> P" ..
  1565 
  1566 lemma not_mono: "Q --> P ==> ~ P --> ~ Q"
  1567   by iprover
  1568 
  1569 lemma ex_mono: "(!!x. P x --> Q x) ==> (EX x. P x) --> (EX x. Q x)"
  1570   by iprover
  1571 
  1572 lemma all_mono: "(!!x. P x --> Q x) ==> (ALL x. P x) --> (ALL x. Q x)"
  1573   by iprover
  1574 
  1575 lemma Collect_mono: "(!!x. P x --> Q x) ==> Collect P \<subseteq> Collect Q"
  1576   by blast
  1577 
  1578 lemma Int_Collect_mono:
  1579     "A \<subseteq> B ==> (!!x. x \<in> A ==> P x --> Q x) ==> A \<inter> Collect P \<subseteq> B \<inter> Collect Q"
  1580   by blast
  1581 
  1582 lemmas basic_monos =
  1583   subset_refl imp_refl disj_mono conj_mono
  1584   ex_mono Collect_mono in_mono
  1585 
  1586 lemma eq_to_mono: "a = b ==> c = d ==> b --> d ==> a --> c"
  1587   by iprover
  1588 
  1589 
  1590 subsubsection {* Inverse image of a function *}
  1591 
  1592 definition vimage :: "('a => 'b) => 'b set => 'a set" (infixr "-`" 90) where
  1593   "f -` B == {x. f x : B}"
  1594 
  1595 lemma vimage_eq [simp]: "(a : f -` B) = (f a : B)"
  1596   by (unfold vimage_def) blast
  1597 
  1598 lemma vimage_singleton_eq: "(a : f -` {b}) = (f a = b)"
  1599   by simp
  1600 
  1601 lemma vimageI [intro]: "f a = b ==> b:B ==> a : f -` B"
  1602   by (unfold vimage_def) blast
  1603 
  1604 lemma vimageI2: "f a : A ==> a : f -` A"
  1605   by (unfold vimage_def) fast
  1606 
  1607 lemma vimageE [elim!]: "a: f -` B ==> (!!x. f a = x ==> x:B ==> P) ==> P"
  1608   by (unfold vimage_def) blast
  1609 
  1610 lemma vimageD: "a : f -` A ==> f a : A"
  1611   by (unfold vimage_def) fast
  1612 
  1613 lemma vimage_empty [simp]: "f -` {} = {}"
  1614   by blast
  1615 
  1616 lemma vimage_Compl: "f -` (-A) = -(f -` A)"
  1617   by blast
  1618 
  1619 lemma vimage_Un [simp]: "f -` (A Un B) = (f -` A) Un (f -` B)"
  1620   by blast
  1621 
  1622 lemma vimage_Int [simp]: "f -` (A Int B) = (f -` A) Int (f -` B)"
  1623   by fast
  1624 
  1625 lemma vimage_Collect_eq [simp]: "f -` Collect P = {y. P (f y)}"
  1626   by blast
  1627 
  1628 lemma vimage_Collect: "(!!x. P (f x) = Q x) ==> f -` (Collect P) = Collect Q"
  1629   by blast
  1630 
  1631 lemma vimage_insert: "f-`(insert a B) = (f-`{a}) Un (f-`B)"
  1632   -- {* NOT suitable for rewriting because of the recurrence of @{term "{a}"}. *}
  1633   by blast
  1634 
  1635 lemma vimage_Diff: "f -` (A - B) = (f -` A) - (f -` B)"
  1636   by blast
  1637 
  1638 lemma vimage_UNIV [simp]: "f -` UNIV = UNIV"
  1639   by blast
  1640 
  1641 lemma vimage_mono: "A \<subseteq> B ==> f -` A \<subseteq> f -` B"
  1642   -- {* monotonicity *}
  1643   by blast
  1644 
  1645 lemma vimage_image_eq [no_atp]: "f -` (f ` A) = {y. EX x:A. f x = f y}"
  1646 by (blast intro: sym)
  1647 
  1648 lemma image_vimage_subset: "f ` (f -` A) <= A"
  1649 by blast
  1650 
  1651 lemma image_vimage_eq [simp]: "f ` (f -` A) = A Int range f"
  1652 by blast
  1653 
  1654 lemma vimage_const [simp]: "((\<lambda>x. c) -` A) = (if c \<in> A then UNIV else {})"
  1655   by auto
  1656 
  1657 lemma vimage_if [simp]: "((\<lambda>x. if x \<in> B then c else d) -` A) = 
  1658    (if c \<in> A then (if d \<in> A then UNIV else B)
  1659     else if d \<in> A then -B else {})"  
  1660   by (auto simp add: vimage_def) 
  1661 
  1662 lemma vimage_inter_cong:
  1663   "(\<And> w. w \<in> S \<Longrightarrow> f w = g w) \<Longrightarrow> f -` y \<inter> S = g -` y \<inter> S"
  1664   by auto
  1665 
  1666 lemma image_Int_subset: "f`(A Int B) <= f`A Int f`B"
  1667 by blast
  1668 
  1669 lemma image_diff_subset: "f`A - f`B <= f`(A - B)"
  1670 by blast
  1671 
  1672 
  1673 subsubsection {* Getting the Contents of a Singleton Set *}
  1674 
  1675 definition the_elem :: "'a set \<Rightarrow> 'a" where
  1676   "the_elem X = (THE x. X = {x})"
  1677 
  1678 lemma the_elem_eq [simp]: "the_elem {x} = x"
  1679   by (simp add: the_elem_def)
  1680 
  1681 
  1682 subsubsection {* Least value operator *}
  1683 
  1684 lemma Least_mono:
  1685   "mono (f::'a::order => 'b::order) ==> EX x:S. ALL y:S. x <= y
  1686     ==> (LEAST y. y : f ` S) = f (LEAST x. x : S)"
  1687     -- {* Courtesy of Stephan Merz *}
  1688   apply clarify
  1689   apply (erule_tac P = "%x. x : S" in LeastI2_order, fast)
  1690   apply (rule LeastI2_order)
  1691   apply (auto elim: monoD intro!: order_antisym)
  1692   done
  1693 
  1694 subsection {* Misc *}
  1695 
  1696 text {* Rudimentary code generation *}
  1697 
  1698 lemma insert_code [code]: "insert y A x \<longleftrightarrow> y = x \<or> A x"
  1699   by (auto simp add: insert_compr Collect_def mem_def)
  1700 
  1701 lemma vimage_code [code]: "(f -` A) x = A (f x)"
  1702   by (simp add: vimage_def Collect_def mem_def)
  1703 
  1704 hide_const (open) member
  1705 
  1706 text {* Misc theorem and ML bindings *}
  1707 
  1708 lemmas equalityI = subset_antisym
  1709 
  1710 ML {*
  1711 val Ball_def = @{thm Ball_def}
  1712 val Bex_def = @{thm Bex_def}
  1713 val CollectD = @{thm CollectD}
  1714 val CollectE = @{thm CollectE}
  1715 val CollectI = @{thm CollectI}
  1716 val Collect_conj_eq = @{thm Collect_conj_eq}
  1717 val Collect_mem_eq = @{thm Collect_mem_eq}
  1718 val IntD1 = @{thm IntD1}
  1719 val IntD2 = @{thm IntD2}
  1720 val IntE = @{thm IntE}
  1721 val IntI = @{thm IntI}
  1722 val Int_Collect = @{thm Int_Collect}
  1723 val UNIV_I = @{thm UNIV_I}
  1724 val UNIV_witness = @{thm UNIV_witness}
  1725 val UnE = @{thm UnE}
  1726 val UnI1 = @{thm UnI1}
  1727 val UnI2 = @{thm UnI2}
  1728 val ballE = @{thm ballE}
  1729 val ballI = @{thm ballI}
  1730 val bexCI = @{thm bexCI}
  1731 val bexE = @{thm bexE}
  1732 val bexI = @{thm bexI}
  1733 val bex_triv = @{thm bex_triv}
  1734 val bspec = @{thm bspec}
  1735 val contra_subsetD = @{thm contra_subsetD}
  1736 val distinct_lemma = @{thm distinct_lemma}
  1737 val eq_to_mono = @{thm eq_to_mono}
  1738 val equalityCE = @{thm equalityCE}
  1739 val equalityD1 = @{thm equalityD1}
  1740 val equalityD2 = @{thm equalityD2}
  1741 val equalityE = @{thm equalityE}
  1742 val equalityI = @{thm equalityI}
  1743 val imageE = @{thm imageE}
  1744 val imageI = @{thm imageI}
  1745 val image_Un = @{thm image_Un}
  1746 val image_insert = @{thm image_insert}
  1747 val insert_commute = @{thm insert_commute}
  1748 val insert_iff = @{thm insert_iff}
  1749 val mem_Collect_eq = @{thm mem_Collect_eq}
  1750 val rangeE = @{thm rangeE}
  1751 val rangeI = @{thm rangeI}
  1752 val range_eqI = @{thm range_eqI}
  1753 val subsetCE = @{thm subsetCE}
  1754 val subsetD = @{thm subsetD}
  1755 val subsetI = @{thm subsetI}
  1756 val subset_refl = @{thm subset_refl}
  1757 val subset_trans = @{thm subset_trans}
  1758 val vimageD = @{thm vimageD}
  1759 val vimageE = @{thm vimageE}
  1760 val vimageI = @{thm vimageI}
  1761 val vimageI2 = @{thm vimageI2}
  1762 val vimage_Collect = @{thm vimage_Collect}
  1763 val vimage_Int = @{thm vimage_Int}
  1764 val vimage_Un = @{thm vimage_Un}
  1765 *}
  1766 
  1767 end