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