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