src/HOL/Set.thy
 author haftmann Sun Oct 16 14:48:00 2011 +0200 (2011-10-16) changeset 45152 e877b76c72bd parent 45121 5e495ccf6e56 child 45607 16b4f5774621 permissions -rw-r--r--
hide not_member as also member
     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   fn _ =>

    80     Quantifier1.rearrange_Collect

    81      (rtac @{thm Collect_cong} 1 THEN

    82       rtac @{thm iffI} 1 THEN

    83       ALLGOALS

    84         (EVERY' [REPEAT_DETERM o etac @{thm conjE}, DEPTH_SOLVE_1 o ares_tac @{thms conjI}]))

    85 *}

    86

    87 lemmas CollectE = CollectD [elim_format]

    88

    89 lemma set_eqI:

    90   assumes "\<And>x. x \<in> A \<longleftrightarrow> x \<in> B"

    91   shows "A = B"

    92 proof -

    93   from assms have "{x. x \<in> A} = {x. x \<in> B}" by simp

    94   then show ?thesis by simp

    95 qed

    96

    97 lemma set_eq_iff [no_atp]:

    98   "A = B \<longleftrightarrow> (\<forall>x. x \<in> A \<longleftrightarrow> x \<in> B)"

    99   by (auto intro:set_eqI)

   100

   101 text {* Set enumerations *}

   102

   103 abbreviation empty :: "'a set" ("{}") where

   104   "{} \<equiv> bot"

   105

   106 definition insert :: "'a \<Rightarrow> 'a set \<Rightarrow> 'a set" where

   107   insert_compr: "insert a B = {x. x = a \<or> x \<in> B}"

   108

   109 syntax

   110   "_Finset" :: "args => 'a set"    ("{(_)}")

   111 translations

   112   "{x, xs}" == "CONST insert x {xs}"

   113   "{x}" == "CONST insert x {}"

   114

   115

   116 subsection {* Subsets and bounded quantifiers *}

   117

   118 abbreviation

   119   subset :: "'a set \<Rightarrow> 'a set \<Rightarrow> bool" where

   120   "subset \<equiv> less"

   121

   122 abbreviation

   123   subset_eq :: "'a set \<Rightarrow> 'a set \<Rightarrow> bool" where

   124   "subset_eq \<equiv> less_eq"

   125

   126 notation (output)

   127   subset  ("op <") and

   128   subset  ("(_/ < _)" [50, 51] 50) and

   129   subset_eq  ("op <=") and

   130   subset_eq  ("(_/ <= _)" [50, 51] 50)

   131

   132 notation (xsymbols)

   133   subset  ("op \<subset>") and

   134   subset  ("(_/ \<subset> _)" [50, 51] 50) and

   135   subset_eq  ("op \<subseteq>") and

   136   subset_eq  ("(_/ \<subseteq> _)" [50, 51] 50)

   137

   138 notation (HTML output)

   139   subset  ("op \<subset>") and

   140   subset  ("(_/ \<subset> _)" [50, 51] 50) and

   141   subset_eq  ("op \<subseteq>") and

   142   subset_eq  ("(_/ \<subseteq> _)" [50, 51] 50)

   143

   144 abbreviation (input)

   145   supset :: "'a set \<Rightarrow> 'a set \<Rightarrow> bool" where

   146   "supset \<equiv> greater"

   147

   148 abbreviation (input)

   149   supset_eq :: "'a set \<Rightarrow> 'a set \<Rightarrow> bool" where

   150   "supset_eq \<equiv> greater_eq"

   151

   152 notation (xsymbols)

   153   supset  ("op \<supset>") and

   154   supset  ("(_/ \<supset> _)" [50, 51] 50) and

   155   supset_eq  ("op \<supseteq>") and

   156   supset_eq  ("(_/ \<supseteq> _)" [50, 51] 50)

   157

   158 definition Ball :: "'a set \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> bool" where

   159   "Ball A P \<longleftrightarrow> (\<forall>x. x \<in> A \<longrightarrow> P x)"   -- "bounded universal quantifiers"

   160

   161 definition Bex :: "'a set \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> bool" where

   162   "Bex A P \<longleftrightarrow> (\<exists>x. x \<in> A \<and> P x)"   -- "bounded existential quantifiers"

   163

   164 syntax

   165   "_Ball"       :: "pttrn => 'a set => bool => bool"      ("(3ALL _:_./ _)" [0, 0, 10] 10)

   166   "_Bex"        :: "pttrn => 'a set => bool => bool"      ("(3EX _:_./ _)" [0, 0, 10] 10)

   167   "_Bex1"       :: "pttrn => 'a set => bool => bool"      ("(3EX! _:_./ _)" [0, 0, 10] 10)

   168   "_Bleast"     :: "id => 'a set => bool => 'a"           ("(3LEAST _:_./ _)" [0, 0, 10] 10)

   169

   170 syntax (HOL)

   171   "_Ball"       :: "pttrn => 'a set => bool => bool"      ("(3! _:_./ _)" [0, 0, 10] 10)

   172   "_Bex"        :: "pttrn => 'a set => bool => bool"      ("(3? _:_./ _)" [0, 0, 10] 10)

   173   "_Bex1"       :: "pttrn => 'a set => bool => bool"      ("(3?! _:_./ _)" [0, 0, 10] 10)

   174

   175 syntax (xsymbols)

   176   "_Ball"       :: "pttrn => 'a set => bool => bool"      ("(3\<forall>_\<in>_./ _)" [0, 0, 10] 10)

   177   "_Bex"        :: "pttrn => 'a set => bool => bool"      ("(3\<exists>_\<in>_./ _)" [0, 0, 10] 10)

   178   "_Bex1"       :: "pttrn => 'a set => bool => bool"      ("(3\<exists>!_\<in>_./ _)" [0, 0, 10] 10)

   179   "_Bleast"     :: "id => 'a set => bool => 'a"           ("(3LEAST_\<in>_./ _)" [0, 0, 10] 10)

   180

   181 syntax (HTML output)

   182   "_Ball"       :: "pttrn => 'a set => bool => bool"      ("(3\<forall>_\<in>_./ _)" [0, 0, 10] 10)

   183   "_Bex"        :: "pttrn => 'a set => bool => bool"      ("(3\<exists>_\<in>_./ _)" [0, 0, 10] 10)

   184   "_Bex1"       :: "pttrn => 'a set => bool => bool"      ("(3\<exists>!_\<in>_./ _)" [0, 0, 10] 10)

   185

   186 translations

   187   "ALL x:A. P" == "CONST Ball A (%x. P)"

   188   "EX x:A. P" == "CONST Bex A (%x. P)"

   189   "EX! x:A. P" => "EX! x. x:A & P"

   190   "LEAST x:A. P" => "LEAST x. x:A & P"

   191

   192 syntax (output)

   193   "_setlessAll" :: "[idt, 'a, bool] => bool"  ("(3ALL _<_./ _)"  [0, 0, 10] 10)

   194   "_setlessEx"  :: "[idt, 'a, bool] => bool"  ("(3EX _<_./ _)"  [0, 0, 10] 10)

   195   "_setleAll"   :: "[idt, 'a, bool] => bool"  ("(3ALL _<=_./ _)" [0, 0, 10] 10)

   196   "_setleEx"    :: "[idt, 'a, bool] => bool"  ("(3EX _<=_./ _)" [0, 0, 10] 10)

   197   "_setleEx1"   :: "[idt, 'a, bool] => bool"  ("(3EX! _<=_./ _)" [0, 0, 10] 10)

   198

   199 syntax (xsymbols)

   200   "_setlessAll" :: "[idt, 'a, bool] => bool"   ("(3\<forall>_\<subset>_./ _)"  [0, 0, 10] 10)

   201   "_setlessEx"  :: "[idt, 'a, bool] => bool"   ("(3\<exists>_\<subset>_./ _)"  [0, 0, 10] 10)

   202   "_setleAll"   :: "[idt, 'a, bool] => bool"   ("(3\<forall>_\<subseteq>_./ _)" [0, 0, 10] 10)

   203   "_setleEx"    :: "[idt, 'a, bool] => bool"   ("(3\<exists>_\<subseteq>_./ _)" [0, 0, 10] 10)

   204   "_setleEx1"   :: "[idt, 'a, bool] => bool"   ("(3\<exists>!_\<subseteq>_./ _)" [0, 0, 10] 10)

   205

   206 syntax (HOL output)

   207   "_setlessAll" :: "[idt, 'a, bool] => bool"   ("(3! _<_./ _)"  [0, 0, 10] 10)

   208   "_setlessEx"  :: "[idt, 'a, bool] => bool"   ("(3? _<_./ _)"  [0, 0, 10] 10)

   209   "_setleAll"   :: "[idt, 'a, bool] => bool"   ("(3! _<=_./ _)" [0, 0, 10] 10)

   210   "_setleEx"    :: "[idt, 'a, bool] => bool"   ("(3? _<=_./ _)" [0, 0, 10] 10)

   211   "_setleEx1"   :: "[idt, 'a, bool] => bool"   ("(3?! _<=_./ _)" [0, 0, 10] 10)

   212

   213 syntax (HTML output)

   214   "_setlessAll" :: "[idt, 'a, bool] => bool"   ("(3\<forall>_\<subset>_./ _)"  [0, 0, 10] 10)

   215   "_setlessEx"  :: "[idt, 'a, bool] => bool"   ("(3\<exists>_\<subset>_./ _)"  [0, 0, 10] 10)

   216   "_setleAll"   :: "[idt, 'a, bool] => bool"   ("(3\<forall>_\<subseteq>_./ _)" [0, 0, 10] 10)

   217   "_setleEx"    :: "[idt, 'a, bool] => bool"   ("(3\<exists>_\<subseteq>_./ _)" [0, 0, 10] 10)

   218   "_setleEx1"   :: "[idt, 'a, bool] => bool"   ("(3\<exists>!_\<subseteq>_./ _)" [0, 0, 10] 10)

   219

   220 translations

   221  "\<forall>A\<subset>B. P"   =>  "ALL A. A \<subset> B --> P"

   222  "\<exists>A\<subset>B. P"   =>  "EX A. A \<subset> B & P"

   223  "\<forall>A\<subseteq>B. P"   =>  "ALL A. A \<subseteq> B --> P"

   224  "\<exists>A\<subseteq>B. P"   =>  "EX A. A \<subseteq> B & P"

   225  "\<exists>!A\<subseteq>B. P"  =>  "EX! A. A \<subseteq> B & P"

   226

   227 print_translation {*

   228 let

   229   val Type (set_type, _) = @{typ "'a set"};   (* FIXME 'a => bool (!?!) *)

   230   val All_binder = Mixfix.binder_name @{const_syntax All};

   231   val Ex_binder = Mixfix.binder_name @{const_syntax Ex};

   232   val impl = @{const_syntax HOL.implies};

   233   val conj = @{const_syntax HOL.conj};

   234   val sbset = @{const_syntax subset};

   235   val sbset_eq = @{const_syntax subset_eq};

   236

   237   val trans =

   238    [((All_binder, impl, sbset), @{syntax_const "_setlessAll"}),

   239     ((All_binder, impl, sbset_eq), @{syntax_const "_setleAll"}),

   240     ((Ex_binder, conj, sbset), @{syntax_const "_setlessEx"}),

   241     ((Ex_binder, conj, sbset_eq), @{syntax_const "_setleEx"})];

   242

   243   fun mk v v' c n P =

   244     if v = v' andalso not (Term.exists_subterm (fn Free (x, _) => x = v | _ => false) n)

   245     then Syntax.const c $Syntax_Trans.mark_bound v'$ n $P else raise Match;   246   247 fun tr' q = (q,   248 fn [Const (@{syntax_const "_bound"}, _)$ Free (v, Type (T, _)),

   249             Const (c, _) $  250 (Const (d, _)$ (Const (@{syntax_const "_bound"}, _) $Free (v', _))$ n) $P] =>   251 if T = set_type then   252 (case AList.lookup (op =) trans (q, c, d) of   253 NONE => raise Match   254 | SOME l => mk v v' l n P)   255 else raise Match   256 | _ => raise Match);   257 in   258 [tr' All_binder, tr' Ex_binder]   259 end   260 *}   261   262   263 text {*   264 \medskip Translate between @{text "{e | x1...xn. P}"} and @{text   265 "{u. EX x1..xn. u = e & P}"}; @{text "{y. EX x1..xn. y = e & P}"} is   266 only translated if @{text "[0..n] subset bvs(e)"}.   267 *}   268   269 syntax   270 "_Setcompr" :: "'a => idts => bool => 'a set" ("(1{_ |/_./ _})")   271   272 parse_translation {*   273 let   274 val ex_tr = snd (Syntax_Trans.mk_binder_tr ("EX ", @{const_syntax Ex}));   275   276 fun nvars (Const (@{syntax_const "_idts"}, _)$ _ $idts) = nvars idts + 1   277 | nvars _ = 1;   278   279 fun setcompr_tr [e, idts, b] =   280 let   281 val eq = Syntax.const @{const_syntax HOL.eq}$ Bound (nvars idts) $e;   282 val P = Syntax.const @{const_syntax HOL.conj}$ eq $b;   283 val exP = ex_tr [idts, P];   284 in Syntax.const @{const_syntax Collect}$ absdummy dummyT exP end;

   285

   286   in [(@{syntax_const "_Setcompr"}, setcompr_tr)] end;

   287 *}

   288

   289 print_translation {*

   290  [Syntax_Trans.preserve_binder_abs2_tr' @{const_syntax Ball} @{syntax_const "_Ball"},

   291   Syntax_Trans.preserve_binder_abs2_tr' @{const_syntax Bex} @{syntax_const "_Bex"}]

   292 *} -- {* to avoid eta-contraction of body *}

   293

   294 print_translation {*

   295 let

   296   val ex_tr' = snd (Syntax_Trans.mk_binder_tr' (@{const_syntax Ex}, "DUMMY"));

   297

   298   fun setcompr_tr' [Abs (abs as (_, _, P))] =

   299     let

   300       fun check (Const (@{const_syntax Ex}, _) $Abs (_, _, P), n) = check (P, n + 1)   301 | check (Const (@{const_syntax HOL.conj}, _)$

   302               (Const (@{const_syntax HOL.eq}, _) $Bound m$ e) $P, n) =   303 n > 0 andalso m = n andalso not (loose_bvar1 (P, n)) andalso   304 subset (op =) (0 upto (n - 1), add_loose_bnos (e, 0, []))   305 | check _ = false;   306   307 fun tr' (_$ abs) =

   308           let val _ $idts$ (_ $(_$ _ $e)$ Q) = ex_tr' [abs]

   309           in Syntax.const @{syntax_const "_Setcompr"} $e$ idts $Q end;   310 in   311 if check (P, 0) then tr' P   312 else   313 let   314 val (x as _$ Free(xN, _), t) = Syntax_Trans.atomic_abs_tr' abs;

   315           val M = Syntax.const @{syntax_const "_Coll"} $x$ t;

   316         in

   317           case t of

   318             Const (@{const_syntax HOL.conj}, _) $  319 (Const (@{const_syntax Set.member}, _)$

   320                 (Const (@{syntax_const "_bound"}, _) $Free (yN, _))$ A) $P =>   321 if xN = yN then Syntax.const @{syntax_const "_Collect"}$ x $A$ P else M

   322           | _ => M

   323         end

   324     end;

   325   in [(@{const_syntax Collect}, setcompr_tr')] end;

   326 *}

   327

   328 simproc_setup defined_Bex ("EX x:A. P x & Q x") = {*

   329   let

   330     val unfold_bex_tac = unfold_tac @{thms Bex_def};

   331     fun prove_bex_tac ss = unfold_bex_tac ss THEN Quantifier1.prove_one_point_ex_tac;

   332   in fn _ => fn ss => Quantifier1.rearrange_bex (prove_bex_tac ss) ss end

   333 *}

   334

   335 simproc_setup defined_All ("ALL x:A. P x --> Q x") = {*

   336   let

   337     val unfold_ball_tac = unfold_tac @{thms Ball_def};

   338     fun prove_ball_tac ss = unfold_ball_tac ss THEN Quantifier1.prove_one_point_all_tac;

   339   in fn _ => fn ss => Quantifier1.rearrange_ball (prove_ball_tac ss) ss end

   340 *}

   341

   342 lemma ballI [intro!]: "(!!x. x:A ==> P x) ==> ALL x:A. P x"

   343   by (simp add: Ball_def)

   344

   345 lemmas strip = impI allI ballI

   346

   347 lemma bspec [dest?]: "ALL x:A. P x ==> x:A ==> P x"

   348   by (simp add: Ball_def)

   349

   350 text {*

   351   Gives better instantiation for bound:

   352 *}

   353

   354 declaration {* fn _ =>

   355   Classical.map_cs (fn cs => cs addbefore ("bspec", datac @{thm bspec} 1))

   356 *}

   357

   358 ML {*

   359 structure Simpdata =

   360 struct

   361

   362 open Simpdata;

   363

   364 val mksimps_pairs = [(@{const_name Ball}, @{thms bspec})] @ mksimps_pairs;

   365

   366 end;

   367

   368 open Simpdata;

   369 *}

   370

   371 declaration {* fn _ =>

   372   Simplifier.map_ss (fn ss => ss setmksimps (mksimps mksimps_pairs))

   373 *}

   374

   375 lemma ballE [elim]: "ALL x:A. P x ==> (P x ==> Q) ==> (x ~: A ==> Q) ==> Q"

   376   by (unfold Ball_def) blast

   377

   378 lemma bexI [intro]: "P x ==> x:A ==> EX x:A. P x"

   379   -- {* Normally the best argument order: @{prop "P x"} constrains the

   380     choice of @{prop "x:A"}. *}

   381   by (unfold Bex_def) blast

   382

   383 lemma rev_bexI [intro?]: "x:A ==> P x ==> EX x:A. P x"

   384   -- {* The best argument order when there is only one @{prop "x:A"}. *}

   385   by (unfold Bex_def) blast

   386

   387 lemma bexCI: "(ALL x:A. ~P x ==> P a) ==> a:A ==> EX x:A. P x"

   388   by (unfold Bex_def) blast

   389

   390 lemma bexE [elim!]: "EX x:A. P x ==> (!!x. x:A ==> P x ==> Q) ==> Q"

   391   by (unfold Bex_def) blast

   392

   393 lemma ball_triv [simp]: "(ALL x:A. P) = ((EX x. x:A) --> P)"

   394   -- {* Trival rewrite rule. *}

   395   by (simp add: Ball_def)

   396

   397 lemma bex_triv [simp]: "(EX x:A. P) = ((EX x. x:A) & P)"

   398   -- {* Dual form for existentials. *}

   399   by (simp add: Bex_def)

   400

   401 lemma bex_triv_one_point1 [simp]: "(EX x:A. x = a) = (a:A)"

   402   by blast

   403

   404 lemma bex_triv_one_point2 [simp]: "(EX x:A. a = x) = (a:A)"

   405   by blast

   406

   407 lemma bex_one_point1 [simp]: "(EX x:A. x = a & P x) = (a:A & P a)"

   408   by blast

   409

   410 lemma bex_one_point2 [simp]: "(EX x:A. a = x & P x) = (a:A & P a)"

   411   by blast

   412

   413 lemma ball_one_point1 [simp]: "(ALL x:A. x = a --> P x) = (a:A --> P a)"

   414   by blast

   415

   416 lemma ball_one_point2 [simp]: "(ALL x:A. a = x --> P x) = (a:A --> P a)"

   417   by blast

   418

   419 lemma ball_conj_distrib:

   420   "(\<forall>x\<in>A. P x \<and> Q x) \<longleftrightarrow> ((\<forall>x\<in>A. P x) \<and> (\<forall>x\<in>A. Q x))"

   421   by blast

   422

   423 lemma bex_disj_distrib:

   424   "(\<exists>x\<in>A. P x \<or> Q x) \<longleftrightarrow> ((\<exists>x\<in>A. P x) \<or> (\<exists>x\<in>A. Q x))"

   425   by blast

   426

   427

   428 text {* Congruence rules *}

   429

   430 lemma ball_cong:

   431   "A = B ==> (!!x. x:B ==> P x = Q x) ==>

   432     (ALL x:A. P x) = (ALL x:B. Q x)"

   433   by (simp add: Ball_def)

   434

   435 lemma strong_ball_cong [cong]:

   436   "A = B ==> (!!x. x:B =simp=> P x = Q x) ==>

   437     (ALL x:A. P x) = (ALL x:B. Q x)"

   438   by (simp add: simp_implies_def Ball_def)

   439

   440 lemma bex_cong:

   441   "A = B ==> (!!x. x:B ==> P x = Q x) ==>

   442     (EX x:A. P x) = (EX x:B. Q x)"

   443   by (simp add: Bex_def cong: conj_cong)

   444

   445 lemma strong_bex_cong [cong]:

   446   "A = B ==> (!!x. x:B =simp=> P x = Q x) ==>

   447     (EX x:A. P x) = (EX x:B. Q x)"

   448   by (simp add: simp_implies_def Bex_def cong: conj_cong)

   449

   450

   451 subsection {* Basic operations *}

   452

   453 subsubsection {* Subsets *}

   454

   455 lemma subsetI [intro!]: "(\<And>x. x \<in> A \<Longrightarrow> x \<in> B) \<Longrightarrow> A \<subseteq> B"

   456   unfolding mem_def by (rule le_funI, rule le_boolI)

   457

   458 text {*

   459   \medskip Map the type @{text "'a set => anything"} to just @{typ

   460   'a}; for overloading constants whose first argument has type @{typ

   461   "'a set"}.

   462 *}

   463

   464 lemma subsetD [elim, intro?]: "A \<subseteq> B ==> c \<in> A ==> c \<in> B"

   465   unfolding mem_def by (erule le_funE, erule le_boolE)

   466   -- {* Rule in Modus Ponens style. *}

   467

   468 lemma rev_subsetD [no_atp,intro?]: "c \<in> A ==> A \<subseteq> B ==> c \<in> B"

   469   -- {* The same, with reversed premises for use with @{text erule} --

   470       cf @{text rev_mp}. *}

   471   by (rule subsetD)

   472

   473 text {*

   474   \medskip Converts @{prop "A \<subseteq> B"} to @{prop "x \<in> A ==> x \<in> B"}.

   475 *}

   476

   477 lemma subsetCE [no_atp,elim]: "A \<subseteq> B ==> (c \<notin> A ==> P) ==> (c \<in> B ==> P) ==> P"

   478   -- {* Classical elimination rule. *}

   479   unfolding mem_def by (blast dest: le_funE le_boolE)

   480

   481 lemma subset_eq [no_atp]: "A \<le> B = (\<forall>x\<in>A. x \<in> B)" by blast

   482

   483 lemma contra_subsetD [no_atp]: "A \<subseteq> B ==> c \<notin> B ==> c \<notin> A"

   484   by blast

   485

   486 lemma subset_refl: "A \<subseteq> A"

   487   by (fact order_refl) (* already [iff] *)

   488

   489 lemma subset_trans: "A \<subseteq> B ==> B \<subseteq> C ==> A \<subseteq> C"

   490   by (fact order_trans)

   491

   492 lemma set_rev_mp: "x:A ==> A \<subseteq> B ==> x:B"

   493   by (rule subsetD)

   494

   495 lemma set_mp: "A \<subseteq> B ==> x:A ==> x:B"

   496   by (rule subsetD)

   497

   498 lemma eq_mem_trans: "a=b ==> b \<in> A ==> a \<in> A"

   499   by simp

   500

   501 lemmas basic_trans_rules [trans] =

   502   order_trans_rules set_rev_mp set_mp eq_mem_trans

   503

   504

   505 subsubsection {* Equality *}

   506

   507 lemma subset_antisym [intro!]: "A \<subseteq> B ==> B \<subseteq> A ==> A = B"

   508   -- {* Anti-symmetry of the subset relation. *}

   509   by (iprover intro: set_eqI subsetD)

   510

   511 text {*

   512   \medskip Equality rules from ZF set theory -- are they appropriate

   513   here?

   514 *}

   515

   516 lemma equalityD1: "A = B ==> A \<subseteq> B"

   517   by simp

   518

   519 lemma equalityD2: "A = B ==> B \<subseteq> A"

   520   by simp

   521

   522 text {*

   523   \medskip Be careful when adding this to the claset as @{text

   524   subset_empty} is in the simpset: @{prop "A = {}"} goes to @{prop "{}

   525   \<subseteq> A"} and @{prop "A \<subseteq> {}"} and then back to @{prop "A = {}"}!

   526 *}

   527

   528 lemma equalityE: "A = B ==> (A \<subseteq> B ==> B \<subseteq> A ==> P) ==> P"

   529   by simp

   530

   531 lemma equalityCE [elim]:

   532     "A = B ==> (c \<in> A ==> c \<in> B ==> P) ==> (c \<notin> A ==> c \<notin> B ==> P) ==> P"

   533   by blast

   534

   535 lemma eqset_imp_iff: "A = B ==> (x : A) = (x : B)"

   536   by simp

   537

   538 lemma eqelem_imp_iff: "x = y ==> (x : A) = (y : A)"

   539   by simp

   540

   541

   542 subsubsection {* The empty set *}

   543

   544 lemma empty_def:

   545   "{} = {x. False}"

   546   by (simp add: bot_fun_def Collect_def)

   547

   548 lemma empty_iff [simp]: "(c : {}) = False"

   549   by (simp add: empty_def)

   550

   551 lemma emptyE [elim!]: "a : {} ==> P"

   552   by simp

   553

   554 lemma empty_subsetI [iff]: "{} \<subseteq> A"

   555     -- {* One effect is to delete the ASSUMPTION @{prop "{} <= A"} *}

   556   by blast

   557

   558 lemma equals0I: "(!!y. y \<in> A ==> False) ==> A = {}"

   559   by blast

   560

   561 lemma equals0D: "A = {} ==> a \<notin> A"

   562     -- {* Use for reasoning about disjointness: @{text "A Int B = {}"} *}

   563   by blast

   564

   565 lemma ball_empty [simp]: "Ball {} P = True"

   566   by (simp add: Ball_def)

   567

   568 lemma bex_empty [simp]: "Bex {} P = False"

   569   by (simp add: Bex_def)

   570

   571

   572 subsubsection {* The universal set -- UNIV *}

   573

   574 abbreviation UNIV :: "'a set" where

   575   "UNIV \<equiv> top"

   576

   577 lemma UNIV_def:

   578   "UNIV = {x. True}"

   579   by (simp add: top_fun_def Collect_def)

   580

   581 lemma UNIV_I [simp]: "x : UNIV"

   582   by (simp add: UNIV_def)

   583

   584 declare UNIV_I [intro]  -- {* unsafe makes it less likely to cause problems *}

   585

   586 lemma UNIV_witness [intro?]: "EX x. x : UNIV"

   587   by simp

   588

   589 lemma subset_UNIV: "A \<subseteq> UNIV"

   590   by (fact top_greatest) (* already simp *)

   591

   592 text {*

   593   \medskip Eta-contracting these two rules (to remove @{text P})

   594   causes them to be ignored because of their interaction with

   595   congruence rules.

   596 *}

   597

   598 lemma ball_UNIV [simp]: "Ball UNIV P = All P"

   599   by (simp add: Ball_def)

   600

   601 lemma bex_UNIV [simp]: "Bex UNIV P = Ex P"

   602   by (simp add: Bex_def)

   603

   604 lemma UNIV_eq_I: "(\<And>x. x \<in> A) \<Longrightarrow> UNIV = A"

   605   by auto

   606

   607 lemma UNIV_not_empty [iff]: "UNIV ~= {}"

   608   by (blast elim: equalityE)

   609

   610

   611 subsubsection {* The Powerset operator -- Pow *}

   612

   613 definition Pow :: "'a set => 'a set set" where

   614   Pow_def: "Pow A = {B. B \<le> A}"

   615

   616 lemma Pow_iff [iff]: "(A \<in> Pow B) = (A \<subseteq> B)"

   617   by (simp add: Pow_def)

   618

   619 lemma PowI: "A \<subseteq> B ==> A \<in> Pow B"

   620   by (simp add: Pow_def)

   621

   622 lemma PowD: "A \<in> Pow B ==> A \<subseteq> B"

   623   by (simp add: Pow_def)

   624

   625 lemma Pow_bottom: "{} \<in> Pow B"

   626   by simp

   627

   628 lemma Pow_top: "A \<in> Pow A"

   629   by simp

   630

   631 lemma Pow_not_empty: "Pow A \<noteq> {}"

   632   using Pow_top by blast

   633

   634

   635 subsubsection {* Set complement *}

   636

   637 lemma Compl_iff [simp]: "(c \<in> -A) = (c \<notin> A)"

   638   by (simp add: mem_def fun_Compl_def)

   639

   640 lemma ComplI [intro!]: "(c \<in> A ==> False) ==> c \<in> -A"

   641   by (unfold mem_def fun_Compl_def bool_Compl_def) blast

   642

   643 text {*

   644   \medskip This form, with negated conclusion, works well with the

   645   Classical prover.  Negated assumptions behave like formulae on the

   646   right side of the notional turnstile ... *}

   647

   648 lemma ComplD [dest!]: "c : -A ==> c~:A"

   649   by (simp add: mem_def fun_Compl_def)

   650

   651 lemmas ComplE = ComplD [elim_format]

   652

   653 lemma Compl_eq: "- A = {x. ~ x : A}" by blast

   654

   655

   656 subsubsection {* Binary intersection *}

   657

   658 abbreviation inter :: "'a set \<Rightarrow> 'a set \<Rightarrow> 'a set" (infixl "Int" 70) where

   659   "op Int \<equiv> inf"

   660

   661 notation (xsymbols)

   662   inter  (infixl "\<inter>" 70)

   663

   664 notation (HTML output)

   665   inter  (infixl "\<inter>" 70)

   666

   667 lemma Int_def:

   668   "A \<inter> B = {x. x \<in> A \<and> x \<in> B}"

   669   by (simp add: inf_fun_def Collect_def mem_def)

   670

   671 lemma Int_iff [simp]: "(c : A Int B) = (c:A & c:B)"

   672   by (unfold Int_def) blast

   673

   674 lemma IntI [intro!]: "c:A ==> c:B ==> c : A Int B"

   675   by simp

   676

   677 lemma IntD1: "c : A Int B ==> c:A"

   678   by simp

   679

   680 lemma IntD2: "c : A Int B ==> c:B"

   681   by simp

   682

   683 lemma IntE [elim!]: "c : A Int B ==> (c:A ==> c:B ==> P) ==> P"

   684   by simp

   685

   686 lemma mono_Int: "mono f \<Longrightarrow> f (A \<inter> B) \<subseteq> f A \<inter> f B"

   687   by (fact mono_inf)

   688

   689

   690 subsubsection {* Binary union *}

   691

   692 abbreviation union :: "'a set \<Rightarrow> 'a set \<Rightarrow> 'a set" (infixl "Un" 65) where

   693   "union \<equiv> sup"

   694

   695 notation (xsymbols)

   696   union  (infixl "\<union>" 65)

   697

   698 notation (HTML output)

   699   union  (infixl "\<union>" 65)

   700

   701 lemma Un_def:

   702   "A \<union> B = {x. x \<in> A \<or> x \<in> B}"

   703   by (simp add: sup_fun_def Collect_def mem_def)

   704

   705 lemma Un_iff [simp]: "(c : A Un B) = (c:A | c:B)"

   706   by (unfold Un_def) blast

   707

   708 lemma UnI1 [elim?]: "c:A ==> c : A Un B"

   709   by simp

   710

   711 lemma UnI2 [elim?]: "c:B ==> c : A Un B"

   712   by simp

   713

   714 text {*

   715   \medskip Classical introduction rule: no commitment to @{prop A} vs

   716   @{prop B}.

   717 *}

   718

   719 lemma UnCI [intro!]: "(c~:B ==> c:A) ==> c : A Un B"

   720   by auto

   721

   722 lemma UnE [elim!]: "c : A Un B ==> (c:A ==> P) ==> (c:B ==> P) ==> P"

   723   by (unfold Un_def) blast

   724

   725 lemma insert_def: "insert a B = {x. x = a} \<union> B"

   726   by (simp add: Collect_def mem_def insert_compr Un_def)

   727

   728 lemma mono_Un: "mono f \<Longrightarrow> f A \<union> f B \<subseteq> f (A \<union> B)"

   729   by (fact mono_sup)

   730

   731

   732 subsubsection {* Set difference *}

   733

   734 lemma Diff_iff [simp]: "(c : A - B) = (c:A & c~:B)"

   735   by (simp add: mem_def fun_diff_def)

   736

   737 lemma DiffI [intro!]: "c : A ==> c ~: B ==> c : A - B"

   738   by simp

   739

   740 lemma DiffD1: "c : A - B ==> c : A"

   741   by simp

   742

   743 lemma DiffD2: "c : A - B ==> c : B ==> P"

   744   by simp

   745

   746 lemma DiffE [elim!]: "c : A - B ==> (c:A ==> c~:B ==> P) ==> P"

   747   by simp

   748

   749 lemma set_diff_eq: "A - B = {x. x : A & ~ x : B}" by blast

   750

   751 lemma Compl_eq_Diff_UNIV: "-A = (UNIV - A)"

   752 by blast

   753

   754

   755 subsubsection {* Augmenting a set -- @{const insert} *}

   756

   757 lemma insert_iff [simp]: "(a : insert b A) = (a = b | a:A)"

   758   by (unfold insert_def) blast

   759

   760 lemma insertI1: "a : insert a B"

   761   by simp

   762

   763 lemma insertI2: "a : B ==> a : insert b B"

   764   by simp

   765

   766 lemma insertE [elim!]: "a : insert b A ==> (a = b ==> P) ==> (a:A ==> P) ==> P"

   767   by (unfold insert_def) blast

   768

   769 lemma insertCI [intro!]: "(a~:B ==> a = b) ==> a: insert b B"

   770   -- {* Classical introduction rule. *}

   771   by auto

   772

   773 lemma subset_insert_iff: "(A \<subseteq> insert x B) = (if x:A then A - {x} \<subseteq> B else A \<subseteq> B)"

   774   by auto

   775

   776 lemma set_insert:

   777   assumes "x \<in> A"

   778   obtains B where "A = insert x B" and "x \<notin> B"

   779 proof

   780   from assms show "A = insert x (A - {x})" by blast

   781 next

   782   show "x \<notin> A - {x}" by blast

   783 qed

   784

   785 lemma insert_ident: "x ~: A ==> x ~: B ==> (insert x A = insert x B) = (A = B)"

   786 by auto

   787

   788 lemma insert_eq_iff: assumes "a \<notin> A" "b \<notin> B"

   789 shows "insert a A = insert b B \<longleftrightarrow>

   790   (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)"

   791   (is "?L \<longleftrightarrow> ?R")

   792 proof

   793   assume ?L

   794   show ?R

   795   proof cases

   796     assume "a=b" with assms ?L show ?R by (simp add: insert_ident)

   797   next

   798     assume "a\<noteq>b"

   799     let ?C = "A - {b}"

   800     have "A = insert b ?C \<and> b \<notin> ?C \<and> B = insert a ?C \<and> a \<notin> ?C"

   801       using assms ?L a\<noteq>b by auto

   802     thus ?R using a\<noteq>b by auto

   803   qed

   804 next

   805   assume ?R thus ?L by(auto split: if_splits)

   806 qed

   807

   808 subsubsection {* Singletons, using insert *}

   809

   810 lemma singletonI [intro!,no_atp]: "a : {a}"

   811     -- {* Redundant? But unlike @{text insertCI}, it proves the subgoal immediately! *}

   812   by (rule insertI1)

   813

   814 lemma singletonD [dest!,no_atp]: "b : {a} ==> b = a"

   815   by blast

   816

   817 lemmas singletonE = singletonD [elim_format]

   818

   819 lemma singleton_iff: "(b : {a}) = (b = a)"

   820   by blast

   821

   822 lemma singleton_inject [dest!]: "{a} = {b} ==> a = b"

   823   by blast

   824

   825 lemma singleton_insert_inj_eq [iff,no_atp]:

   826      "({b} = insert a A) = (a = b & A \<subseteq> {b})"

   827   by blast

   828

   829 lemma singleton_insert_inj_eq' [iff,no_atp]:

   830      "(insert a A = {b}) = (a = b & A \<subseteq> {b})"

   831   by blast

   832

   833 lemma subset_singletonD: "A \<subseteq> {x} ==> A = {} | A = {x}"

   834   by fast

   835

   836 lemma singleton_conv [simp]: "{x. x = a} = {a}"

   837   by blast

   838

   839 lemma singleton_conv2 [simp]: "{x. a = x} = {a}"

   840   by blast

   841

   842 lemma diff_single_insert: "A - {x} \<subseteq> B ==> x \<in> A ==> A \<subseteq> insert x B"

   843   by blast

   844

   845 lemma doubleton_eq_iff: "({a,b} = {c,d}) = (a=c & b=d | a=d & b=c)"

   846   by (blast elim: equalityE)

   847

   848

   849 subsubsection {* Image of a set under a function *}

   850

   851 text {*

   852   Frequently @{term b} does not have the syntactic form of @{term "f x"}.

   853 *}

   854

   855 definition image :: "('a => 'b) => 'a set => 'b set" (infixr "" 90) where

   856   image_def [no_atp]: "f  A = {y. EX x:A. y = f(x)}"

   857

   858 abbreviation

   859   range :: "('a => 'b) => 'b set" where -- "of function"

   860   "range f == f  UNIV"

   861

   862 lemma image_eqI [simp, intro]: "b = f x ==> x:A ==> b : fA"

   863   by (unfold image_def) blast

   864

   865 lemma imageI: "x : A ==> f x : f  A"

   866   by (rule image_eqI) (rule refl)

   867

   868 lemma rev_image_eqI: "x:A ==> b = f x ==> b : fA"

   869   -- {* This version's more effective when we already have the

   870     required @{term x}. *}

   871   by (unfold image_def) blast

   872

   873 lemma imageE [elim!]:

   874   "b : (%x. f x)A ==> (!!x. b = f x ==> x:A ==> P) ==> P"

   875   -- {* The eta-expansion gives variable-name preservation. *}

   876   by (unfold image_def) blast

   877

   878 lemma image_Un: "f(A Un B) = fA Un fB"

   879   by blast

   880

   881 lemma image_iff: "(z : fA) = (EX x:A. z = f x)"

   882   by blast

   883

   884 lemma image_subset_iff [no_atp]: "(fA \<subseteq> B) = (\<forall>x\<in>A. f x \<in> B)"

   885   -- {* This rewrite rule would confuse users if made default. *}

   886   by blast

   887

   888 lemma subset_image_iff: "(B \<subseteq> fA) = (EX AA. AA \<subseteq> A & B = fAA)"

   889   apply safe

   890    prefer 2 apply fast

   891   apply (rule_tac x = "{a. a : A & f a : B}" in exI, fast)

   892   done

   893

   894 lemma image_subsetI: "(!!x. x \<in> A ==> f x \<in> B) ==> fA \<subseteq> B"

   895   -- {* Replaces the three steps @{text subsetI}, @{text imageE},

   896     @{text hypsubst}, but breaks too many existing proofs. *}

   897   by blast

   898

   899 text {*

   900   \medskip Range of a function -- just a translation for image!

   901 *}

   902

   903 lemma image_ident [simp]: "(%x. x)  Y = Y"

   904   by blast

   905

   906 lemma range_eqI: "b = f x ==> b \<in> range f"

   907   by simp

   908

   909 lemma rangeI: "f x \<in> range f"

   910   by simp

   911

   912 lemma rangeE [elim?]: "b \<in> range (\<lambda>x. f x) ==> (!!x. b = f x ==> P) ==> P"

   913   by blast

   914

   915 subsubsection {* Some rules with @{text "if"} *}

   916

   917 text{* Elimination of @{text"{x. \<dots> & x=t & \<dots>}"}. *}

   918

   919 lemma Collect_conv_if: "{x. x=a & P x} = (if P a then {a} else {})"

   920   by auto

   921

   922 lemma Collect_conv_if2: "{x. a=x & P x} = (if P a then {a} else {})"

   923   by auto

   924

   925 text {*

   926   Rewrite rules for boolean case-splitting: faster than @{text

   927   "split_if [split]"}.

   928 *}

   929

   930 lemma split_if_eq1: "((if Q then x else y) = b) = ((Q --> x = b) & (~ Q --> y = b))"

   931   by (rule split_if)

   932

   933 lemma split_if_eq2: "(a = (if Q then x else y)) = ((Q --> a = x) & (~ Q --> a = y))"

   934   by (rule split_if)

   935

   936 text {*

   937   Split ifs on either side of the membership relation.  Not for @{text

   938   "[simp]"} -- can cause goals to blow up!

   939 *}

   940

   941 lemma split_if_mem1: "((if Q then x else y) : b) = ((Q --> x : b) & (~ Q --> y : b))"

   942   by (rule split_if)

   943

   944 lemma split_if_mem2: "(a : (if Q then x else y)) = ((Q --> a : x) & (~ Q --> a : y))"

   945   by (rule split_if [where P="%S. a : S"])

   946

   947 lemmas split_ifs = if_bool_eq_conj split_if_eq1 split_if_eq2 split_if_mem1 split_if_mem2

   948

   949 (*Would like to add these, but the existing code only searches for the

   950   outer-level constant, which in this case is just Set.member; we instead need

   951   to use term-nets to associate patterns with rules.  Also, if a rule fails to

   952   apply, then the formula should be kept.

   953   [("uminus", Compl_iff RS iffD1), ("minus", [Diff_iff RS iffD1]),

   954    ("Int", [IntD1,IntD2]),

   955    ("Collect", [CollectD]), ("Inter", [InterD]), ("INTER", [INT_D])]

   956  *)

   957

   958

   959 subsection {* Further operations and lemmas *}

   960

   961 subsubsection {* The proper subset'' relation *}

   962

   963 lemma psubsetI [intro!,no_atp]: "A \<subseteq> B ==> A \<noteq> B ==> A \<subset> B"

   964   by (unfold less_le) blast

   965

   966 lemma psubsetE [elim!,no_atp]:

   967     "[|A \<subset> B;  [|A \<subseteq> B; ~ (B\<subseteq>A)|] ==> R|] ==> R"

   968   by (unfold less_le) blast

   969

   970 lemma psubset_insert_iff:

   971   "(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)"

   972   by (auto simp add: less_le subset_insert_iff)

   973

   974 lemma psubset_eq: "(A \<subset> B) = (A \<subseteq> B & A \<noteq> B)"

   975   by (simp only: less_le)

   976

   977 lemma psubset_imp_subset: "A \<subset> B ==> A \<subseteq> B"

   978   by (simp add: psubset_eq)

   979

   980 lemma psubset_trans: "[| A \<subset> B; B \<subset> C |] ==> A \<subset> C"

   981 apply (unfold less_le)

   982 apply (auto dest: subset_antisym)

   983 done

   984

   985 lemma psubsetD: "[| A \<subset> B; c \<in> A |] ==> c \<in> B"

   986 apply (unfold less_le)

   987 apply (auto dest: subsetD)

   988 done

   989

   990 lemma psubset_subset_trans: "A \<subset> B ==> B \<subseteq> C ==> A \<subset> C"

   991   by (auto simp add: psubset_eq)

   992

   993 lemma subset_psubset_trans: "A \<subseteq> B ==> B \<subset> C ==> A \<subset> C"

   994   by (auto simp add: psubset_eq)

   995

   996 lemma psubset_imp_ex_mem: "A \<subset> B ==> \<exists>b. b \<in> (B - A)"

   997   by (unfold less_le) blast

   998

   999 lemma atomize_ball:

  1000     "(!!x. x \<in> A ==> P x) == Trueprop (\<forall>x\<in>A. P x)"

  1001   by (simp only: Ball_def atomize_all atomize_imp)

  1002

  1003 lemmas [symmetric, rulify] = atomize_ball

  1004   and [symmetric, defn] = atomize_ball

  1005

  1006 lemma image_Pow_mono:

  1007   assumes "f  A \<le> B"

  1008   shows "(image f)  (Pow A) \<le> Pow B"

  1009 using assms by blast

  1010

  1011 lemma image_Pow_surj:

  1012   assumes "f  A = B"

  1013   shows "(image f)  (Pow A) = Pow B"

  1014 using assms unfolding Pow_def proof(auto)

  1015   fix Y assume *: "Y \<le> f  A"

  1016   obtain X where X_def: "X = {x \<in> A. f x \<in> Y}" by blast

  1017   have "f  X = Y \<and> X \<le> A" unfolding X_def using * by auto

  1018   thus "Y \<in> (image f)  {X. X \<le> A}" by blast

  1019 qed

  1020

  1021 subsubsection {* Derived rules involving subsets. *}

  1022

  1023 text {* @{text insert}. *}

  1024

  1025 lemma subset_insertI: "B \<subseteq> insert a B"

  1026   by (rule subsetI) (erule insertI2)

  1027

  1028 lemma subset_insertI2: "A \<subseteq> B \<Longrightarrow> A \<subseteq> insert b B"

  1029   by blast

  1030

  1031 lemma subset_insert: "x \<notin> A ==> (A \<subseteq> insert x B) = (A \<subseteq> B)"

  1032   by blast

  1033

  1034

  1035 text {* \medskip Finite Union -- the least upper bound of two sets. *}

  1036

  1037 lemma Un_upper1: "A \<subseteq> A \<union> B"

  1038   by (fact sup_ge1)

  1039

  1040 lemma Un_upper2: "B \<subseteq> A \<union> B"

  1041   by (fact sup_ge2)

  1042

  1043 lemma Un_least: "A \<subseteq> C ==> B \<subseteq> C ==> A \<union> B \<subseteq> C"

  1044   by (fact sup_least)

  1045

  1046

  1047 text {* \medskip Finite Intersection -- the greatest lower bound of two sets. *}

  1048

  1049 lemma Int_lower1: "A \<inter> B \<subseteq> A"

  1050   by (fact inf_le1)

  1051

  1052 lemma Int_lower2: "A \<inter> B \<subseteq> B"

  1053   by (fact inf_le2)

  1054

  1055 lemma Int_greatest: "C \<subseteq> A ==> C \<subseteq> B ==> C \<subseteq> A \<inter> B"

  1056   by (fact inf_greatest)

  1057

  1058

  1059 text {* \medskip Set difference. *}

  1060

  1061 lemma Diff_subset: "A - B \<subseteq> A"

  1062   by blast

  1063

  1064 lemma Diff_subset_conv: "(A - B \<subseteq> C) = (A \<subseteq> B \<union> C)"

  1065 by blast

  1066

  1067

  1068 subsubsection {* Equalities involving union, intersection, inclusion, etc. *}

  1069

  1070 text {* @{text "{}"}. *}

  1071

  1072 lemma Collect_const [simp]: "{s. P} = (if P then UNIV else {})"

  1073   -- {* supersedes @{text "Collect_False_empty"} *}

  1074   by auto

  1075

  1076 lemma subset_empty [simp]: "(A \<subseteq> {}) = (A = {})"

  1077   by (fact bot_unique)

  1078

  1079 lemma not_psubset_empty [iff]: "\<not> (A < {})"

  1080   by (fact not_less_bot) (* FIXME: already simp *)

  1081

  1082 lemma Collect_empty_eq [simp]: "(Collect P = {}) = (\<forall>x. \<not> P x)"

  1083 by blast

  1084

  1085 lemma empty_Collect_eq [simp]: "({} = Collect P) = (\<forall>x. \<not> P x)"

  1086 by blast

  1087

  1088 lemma Collect_neg_eq: "{x. \<not> P x} = - {x. P x}"

  1089   by blast

  1090

  1091 lemma Collect_disj_eq: "{x. P x | Q x} = {x. P x} \<union> {x. Q x}"

  1092   by blast

  1093

  1094 lemma Collect_imp_eq: "{x. P x --> Q x} = -{x. P x} \<union> {x. Q x}"

  1095   by blast

  1096

  1097 lemma Collect_conj_eq: "{x. P x & Q x} = {x. P x} \<inter> {x. Q x}"

  1098   by blast

  1099

  1100

  1101 text {* \medskip @{text insert}. *}

  1102

  1103 lemma insert_is_Un: "insert a A = {a} Un A"

  1104   -- {* NOT SUITABLE FOR REWRITING since @{text "{a} == insert a {}"} *}

  1105   by blast

  1106

  1107 lemma insert_not_empty [simp]: "insert a A \<noteq> {}"

  1108   by blast

  1109

  1110 lemmas empty_not_insert = insert_not_empty [symmetric, standard]

  1111 declare empty_not_insert [simp]

  1112

  1113 lemma insert_absorb: "a \<in> A ==> insert a A = A"

  1114   -- {* @{text "[simp]"} causes recursive calls when there are nested inserts *}

  1115   -- {* with \emph{quadratic} running time *}

  1116   by blast

  1117

  1118 lemma insert_absorb2 [simp]: "insert x (insert x A) = insert x A"

  1119   by blast

  1120

  1121 lemma insert_commute: "insert x (insert y A) = insert y (insert x A)"

  1122   by blast

  1123

  1124 lemma insert_subset [simp]: "(insert x A \<subseteq> B) = (x \<in> B & A \<subseteq> B)"

  1125   by blast

  1126

  1127 lemma mk_disjoint_insert: "a \<in> A ==> \<exists>B. A = insert a B & a \<notin> B"

  1128   -- {* use new @{text B} rather than @{text "A - {a}"} to avoid infinite unfolding *}

  1129   apply (rule_tac x = "A - {a}" in exI, blast)

  1130   done

  1131

  1132 lemma insert_Collect: "insert a (Collect P) = {u. u \<noteq> a --> P u}"

  1133   by auto

  1134

  1135 lemma insert_inter_insert[simp]: "insert a A \<inter> insert a B = insert a (A \<inter> B)"

  1136   by blast

  1137

  1138 lemma insert_disjoint [simp,no_atp]:

  1139  "(insert a A \<inter> B = {}) = (a \<notin> B \<and> A \<inter> B = {})"

  1140  "({} = insert a A \<inter> B) = (a \<notin> B \<and> {} = A \<inter> B)"

  1141   by auto

  1142

  1143 lemma disjoint_insert [simp,no_atp]:

  1144  "(B \<inter> insert a A = {}) = (a \<notin> B \<and> B \<inter> A = {})"

  1145  "({} = A \<inter> insert b B) = (b \<notin> A \<and> {} = A \<inter> B)"

  1146   by auto

  1147

  1148 text {* \medskip @{text image}. *}

  1149

  1150 lemma image_empty [simp]: "f{} = {}"

  1151   by blast

  1152

  1153 lemma image_insert [simp]: "f  insert a B = insert (f a) (fB)"

  1154   by blast

  1155

  1156 lemma image_constant: "x \<in> A ==> (\<lambda>x. c)  A = {c}"

  1157   by auto

  1158

  1159 lemma image_constant_conv: "(%x. c)  A = (if A = {} then {} else {c})"

  1160 by auto

  1161

  1162 lemma image_image: "f  (g  A) = (\<lambda>x. f (g x))  A"

  1163 by blast

  1164

  1165 lemma insert_image [simp]: "x \<in> A ==> insert (f x) (fA) = fA"

  1166 by blast

  1167

  1168 lemma image_is_empty [iff]: "(fA = {}) = (A = {})"

  1169 by blast

  1170

  1171 lemma empty_is_image[iff]: "({} = f  A) = (A = {})"

  1172 by blast

  1173

  1174

  1175 lemma image_Collect [no_atp]: "f  {x. P x} = {f x | x. P x}"

  1176   -- {* NOT suitable as a default simprule: the RHS isn't simpler than the LHS,

  1177       with its implicit quantifier and conjunction.  Also image enjoys better

  1178       equational properties than does the RHS. *}

  1179   by blast

  1180

  1181 lemma if_image_distrib [simp]:

  1182   "(\<lambda>x. if P x then f x else g x)  S

  1183     = (f  (S \<inter> {x. P x})) \<union> (g  (S \<inter> {x. \<not> P x}))"

  1184   by (auto simp add: image_def)

  1185

  1186 lemma image_cong: "M = N ==> (!!x. x \<in> N ==> f x = g x) ==> fM = gN"

  1187   by (simp add: image_def)

  1188

  1189 lemma image_Int_subset: "f(A Int B) <= fA Int fB"

  1190 by blast

  1191

  1192 lemma image_diff_subset: "fA - fB <= f(A - B)"

  1193 by blast

  1194

  1195

  1196 text {* \medskip @{text range}. *}

  1197

  1198 lemma full_SetCompr_eq [no_atp]: "{u. \<exists>x. u = f x} = range f"

  1199   by auto

  1200

  1201 lemma range_composition: "range (\<lambda>x. f (g x)) = frange g"

  1202 by (subst image_image, simp)

  1203

  1204

  1205 text {* \medskip @{text Int} *}

  1206

  1207 lemma Int_absorb: "A \<inter> A = A"

  1208   by (fact inf_idem) (* already simp *)

  1209

  1210 lemma Int_left_absorb: "A \<inter> (A \<inter> B) = A \<inter> B"

  1211   by (fact inf_left_idem)

  1212

  1213 lemma Int_commute: "A \<inter> B = B \<inter> A"

  1214   by (fact inf_commute)

  1215

  1216 lemma Int_left_commute: "A \<inter> (B \<inter> C) = B \<inter> (A \<inter> C)"

  1217   by (fact inf_left_commute)

  1218

  1219 lemma Int_assoc: "(A \<inter> B) \<inter> C = A \<inter> (B \<inter> C)"

  1220   by (fact inf_assoc)

  1221

  1222 lemmas Int_ac = Int_assoc Int_left_absorb Int_commute Int_left_commute

  1223   -- {* Intersection is an AC-operator *}

  1224

  1225 lemma Int_absorb1: "B \<subseteq> A ==> A \<inter> B = B"

  1226   by (fact inf_absorb2)

  1227

  1228 lemma Int_absorb2: "A \<subseteq> B ==> A \<inter> B = A"

  1229   by (fact inf_absorb1)

  1230

  1231 lemma Int_empty_left: "{} \<inter> B = {}"

  1232   by (fact inf_bot_left) (* already simp *)

  1233

  1234 lemma Int_empty_right: "A \<inter> {} = {}"

  1235   by (fact inf_bot_right) (* already simp *)

  1236

  1237 lemma disjoint_eq_subset_Compl: "(A \<inter> B = {}) = (A \<subseteq> -B)"

  1238   by blast

  1239

  1240 lemma disjoint_iff_not_equal: "(A \<inter> B = {}) = (\<forall>x\<in>A. \<forall>y\<in>B. x \<noteq> y)"

  1241   by blast

  1242

  1243 lemma Int_UNIV_left: "UNIV \<inter> B = B"

  1244   by (fact inf_top_left) (* already simp *)

  1245

  1246 lemma Int_UNIV_right: "A \<inter> UNIV = A"

  1247   by (fact inf_top_right) (* already simp *)

  1248

  1249 lemma Int_Un_distrib: "A \<inter> (B \<union> C) = (A \<inter> B) \<union> (A \<inter> C)"

  1250   by (fact inf_sup_distrib1)

  1251

  1252 lemma Int_Un_distrib2: "(B \<union> C) \<inter> A = (B \<inter> A) \<union> (C \<inter> A)"

  1253   by (fact inf_sup_distrib2)

  1254

  1255 lemma Int_UNIV [simp,no_atp]: "(A \<inter> B = UNIV) = (A = UNIV & B = UNIV)"

  1256   by (fact inf_eq_top_iff) (* already simp *)

  1257

  1258 lemma Int_subset_iff [no_atp, simp]: "(C \<subseteq> A \<inter> B) = (C \<subseteq> A & C \<subseteq> B)"

  1259   by (fact le_inf_iff)

  1260

  1261 lemma Int_Collect: "(x \<in> A \<inter> {x. P x}) = (x \<in> A & P x)"

  1262   by blast

  1263

  1264

  1265 text {* \medskip @{text Un}. *}

  1266

  1267 lemma Un_absorb: "A \<union> A = A"

  1268   by (fact sup_idem) (* already simp *)

  1269

  1270 lemma Un_left_absorb: "A \<union> (A \<union> B) = A \<union> B"

  1271   by (fact sup_left_idem)

  1272

  1273 lemma Un_commute: "A \<union> B = B \<union> A"

  1274   by (fact sup_commute)

  1275

  1276 lemma Un_left_commute: "A \<union> (B \<union> C) = B \<union> (A \<union> C)"

  1277   by (fact sup_left_commute)

  1278

  1279 lemma Un_assoc: "(A \<union> B) \<union> C = A \<union> (B \<union> C)"

  1280   by (fact sup_assoc)

  1281

  1282 lemmas Un_ac = Un_assoc Un_left_absorb Un_commute Un_left_commute

  1283   -- {* Union is an AC-operator *}

  1284

  1285 lemma Un_absorb1: "A \<subseteq> B ==> A \<union> B = B"

  1286   by (fact sup_absorb2)

  1287

  1288 lemma Un_absorb2: "B \<subseteq> A ==> A \<union> B = A"

  1289   by (fact sup_absorb1)

  1290

  1291 lemma Un_empty_left: "{} \<union> B = B"

  1292   by (fact sup_bot_left) (* already simp *)

  1293

  1294 lemma Un_empty_right: "A \<union> {} = A"

  1295   by (fact sup_bot_right) (* already simp *)

  1296

  1297 lemma Un_UNIV_left: "UNIV \<union> B = UNIV"

  1298   by (fact sup_top_left) (* already simp *)

  1299

  1300 lemma Un_UNIV_right: "A \<union> UNIV = UNIV"

  1301   by (fact sup_top_right) (* already simp *)

  1302

  1303 lemma Un_insert_left [simp]: "(insert a B) \<union> C = insert a (B \<union> C)"

  1304   by blast

  1305

  1306 lemma Un_insert_right [simp]: "A \<union> (insert a B) = insert a (A \<union> B)"

  1307   by blast

  1308

  1309 lemma Int_insert_left:

  1310     "(insert a B) Int C = (if a \<in> C then insert a (B \<inter> C) else B \<inter> C)"

  1311   by auto

  1312

  1313 lemma Int_insert_left_if0[simp]:

  1314     "a \<notin> C \<Longrightarrow> (insert a B) Int C = B \<inter> C"

  1315   by auto

  1316

  1317 lemma Int_insert_left_if1[simp]:

  1318     "a \<in> C \<Longrightarrow> (insert a B) Int C = insert a (B Int C)"

  1319   by auto

  1320

  1321 lemma Int_insert_right:

  1322     "A \<inter> (insert a B) = (if a \<in> A then insert a (A \<inter> B) else A \<inter> B)"

  1323   by auto

  1324

  1325 lemma Int_insert_right_if0[simp]:

  1326     "a \<notin> A \<Longrightarrow> A Int (insert a B) = A Int B"

  1327   by auto

  1328

  1329 lemma Int_insert_right_if1[simp]:

  1330     "a \<in> A \<Longrightarrow> A Int (insert a B) = insert a (A Int B)"

  1331   by auto

  1332

  1333 lemma Un_Int_distrib: "A \<union> (B \<inter> C) = (A \<union> B) \<inter> (A \<union> C)"

  1334   by (fact sup_inf_distrib1)

  1335

  1336 lemma Un_Int_distrib2: "(B \<inter> C) \<union> A = (B \<union> A) \<inter> (C \<union> A)"

  1337   by (fact sup_inf_distrib2)

  1338

  1339 lemma Un_Int_crazy:

  1340     "(A \<inter> B) \<union> (B \<inter> C) \<union> (C \<inter> A) = (A \<union> B) \<inter> (B \<union> C) \<inter> (C \<union> A)"

  1341   by blast

  1342

  1343 lemma subset_Un_eq: "(A \<subseteq> B) = (A \<union> B = B)"

  1344   by (fact le_iff_sup)

  1345

  1346 lemma Un_empty [iff]: "(A \<union> B = {}) = (A = {} & B = {})"

  1347   by (fact sup_eq_bot_iff) (* FIXME: already simp *)

  1348

  1349 lemma Un_subset_iff [no_atp, simp]: "(A \<union> B \<subseteq> C) = (A \<subseteq> C & B \<subseteq> C)"

  1350   by (fact le_sup_iff)

  1351

  1352 lemma Un_Diff_Int: "(A - B) \<union> (A \<inter> B) = A"

  1353   by blast

  1354

  1355 lemma Diff_Int2: "A \<inter> C - B \<inter> C = A \<inter> C - B"

  1356   by blast

  1357

  1358

  1359 text {* \medskip Set complement *}

  1360

  1361 lemma Compl_disjoint [simp]: "A \<inter> -A = {}"

  1362   by (fact inf_compl_bot)

  1363

  1364 lemma Compl_disjoint2 [simp]: "-A \<inter> A = {}"

  1365   by (fact compl_inf_bot)

  1366

  1367 lemma Compl_partition: "A \<union> -A = UNIV"

  1368   by (fact sup_compl_top)

  1369

  1370 lemma Compl_partition2: "-A \<union> A = UNIV"

  1371   by (fact compl_sup_top)

  1372

  1373 lemma double_complement: "- (-A) = (A::'a set)"

  1374   by (fact double_compl) (* already simp *)

  1375

  1376 lemma Compl_Un: "-(A \<union> B) = (-A) \<inter> (-B)"

  1377   by (fact compl_sup) (* already simp *)

  1378

  1379 lemma Compl_Int: "-(A \<inter> B) = (-A) \<union> (-B)"

  1380   by (fact compl_inf) (* already simp *)

  1381

  1382 lemma subset_Compl_self_eq: "(A \<subseteq> -A) = (A = {})"

  1383   by blast

  1384

  1385 lemma Un_Int_assoc_eq: "((A \<inter> B) \<union> C = A \<inter> (B \<union> C)) = (C \<subseteq> A)"

  1386   -- {* Halmos, Naive Set Theory, page 16. *}

  1387   by blast

  1388

  1389 lemma Compl_UNIV_eq: "-UNIV = {}"

  1390   by (fact compl_top_eq) (* already simp *)

  1391

  1392 lemma Compl_empty_eq: "-{} = UNIV"

  1393   by (fact compl_bot_eq) (* already simp *)

  1394

  1395 lemma Compl_subset_Compl_iff [iff]: "(-A \<subseteq> -B) = (B \<subseteq> A)"

  1396   by (fact compl_le_compl_iff) (* FIXME: already simp *)

  1397

  1398 lemma Compl_eq_Compl_iff [iff]: "(-A = -B) = (A = (B::'a set))"

  1399   by (fact compl_eq_compl_iff) (* FIXME: already simp *)

  1400

  1401 lemma Compl_insert: "- insert x A = (-A) - {x}"

  1402   by blast

  1403

  1404 text {* \medskip Bounded quantifiers.

  1405

  1406   The following are not added to the default simpset because

  1407   (a) they duplicate the body and (b) there are no similar rules for @{text Int}. *}

  1408

  1409 lemma ball_Un: "(\<forall>x \<in> A \<union> B. P x) = ((\<forall>x\<in>A. P x) & (\<forall>x\<in>B. P x))"

  1410   by blast

  1411

  1412 lemma bex_Un: "(\<exists>x \<in> A \<union> B. P x) = ((\<exists>x\<in>A. P x) | (\<exists>x\<in>B. P x))"

  1413   by blast

  1414

  1415

  1416 text {* \medskip Set difference. *}

  1417

  1418 lemma Diff_eq: "A - B = A \<inter> (-B)"

  1419   by blast

  1420

  1421 lemma Diff_eq_empty_iff [simp,no_atp]: "(A - B = {}) = (A \<subseteq> B)"

  1422   by blast

  1423

  1424 lemma Diff_cancel [simp]: "A - A = {}"

  1425   by blast

  1426

  1427 lemma Diff_idemp [simp]: "(A - B) - B = A - (B::'a set)"

  1428 by blast

  1429

  1430 lemma Diff_triv: "A \<inter> B = {} ==> A - B = A"

  1431   by (blast elim: equalityE)

  1432

  1433 lemma empty_Diff [simp]: "{} - A = {}"

  1434   by blast

  1435

  1436 lemma Diff_empty [simp]: "A - {} = A"

  1437   by blast

  1438

  1439 lemma Diff_UNIV [simp]: "A - UNIV = {}"

  1440   by blast

  1441

  1442 lemma Diff_insert0 [simp,no_atp]: "x \<notin> A ==> A - insert x B = A - B"

  1443   by blast

  1444

  1445 lemma Diff_insert: "A - insert a B = A - B - {a}"

  1446   -- {* NOT SUITABLE FOR REWRITING since @{text "{a} == insert a 0"} *}

  1447   by blast

  1448

  1449 lemma Diff_insert2: "A - insert a B = A - {a} - B"

  1450   -- {* NOT SUITABLE FOR REWRITING since @{text "{a} == insert a 0"} *}

  1451   by blast

  1452

  1453 lemma insert_Diff_if: "insert x A - B = (if x \<in> B then A - B else insert x (A - B))"

  1454   by auto

  1455

  1456 lemma insert_Diff1 [simp]: "x \<in> B ==> insert x A - B = A - B"

  1457   by blast

  1458

  1459 lemma insert_Diff_single[simp]: "insert a (A - {a}) = insert a A"

  1460 by blast

  1461

  1462 lemma insert_Diff: "a \<in> A ==> insert a (A - {a}) = A"

  1463   by blast

  1464

  1465 lemma Diff_insert_absorb: "x \<notin> A ==> (insert x A) - {x} = A"

  1466   by auto

  1467

  1468 lemma Diff_disjoint [simp]: "A \<inter> (B - A) = {}"

  1469   by blast

  1470

  1471 lemma Diff_partition: "A \<subseteq> B ==> A \<union> (B - A) = B"

  1472   by blast

  1473

  1474 lemma double_diff: "A \<subseteq> B ==> B \<subseteq> C ==> B - (C - A) = A"

  1475   by blast

  1476

  1477 lemma Un_Diff_cancel [simp]: "A \<union> (B - A) = A \<union> B"

  1478   by blast

  1479

  1480 lemma Un_Diff_cancel2 [simp]: "(B - A) \<union> A = B \<union> A"

  1481   by blast

  1482

  1483 lemma Diff_Un: "A - (B \<union> C) = (A - B) \<inter> (A - C)"

  1484   by blast

  1485

  1486 lemma Diff_Int: "A - (B \<inter> C) = (A - B) \<union> (A - C)"

  1487   by blast

  1488

  1489 lemma Un_Diff: "(A \<union> B) - C = (A - C) \<union> (B - C)"

  1490   by blast

  1491

  1492 lemma Int_Diff: "(A \<inter> B) - C = A \<inter> (B - C)"

  1493   by blast

  1494

  1495 lemma Diff_Int_distrib: "C \<inter> (A - B) = (C \<inter> A) - (C \<inter> B)"

  1496   by blast

  1497

  1498 lemma Diff_Int_distrib2: "(A - B) \<inter> C = (A \<inter> C) - (B \<inter> C)"

  1499   by blast

  1500

  1501 lemma Diff_Compl [simp]: "A - (- B) = A \<inter> B"

  1502   by auto

  1503

  1504 lemma Compl_Diff_eq [simp]: "- (A - B) = -A \<union> B"

  1505   by blast

  1506

  1507

  1508 text {* \medskip Quantification over type @{typ bool}. *}

  1509

  1510 lemma bool_induct: "P True \<Longrightarrow> P False \<Longrightarrow> P x"

  1511   by (cases x) auto

  1512

  1513 lemma all_bool_eq: "(\<forall>b. P b) \<longleftrightarrow> P True \<and> P False"

  1514   by (auto intro: bool_induct)

  1515

  1516 lemma bool_contrapos: "P x \<Longrightarrow> \<not> P False \<Longrightarrow> P True"

  1517   by (cases x) auto

  1518

  1519 lemma ex_bool_eq: "(\<exists>b. P b) \<longleftrightarrow> P True \<or> P False"

  1520   by (auto intro: bool_contrapos)

  1521

  1522 lemma UNIV_bool [no_atp]: "UNIV = {False, True}"

  1523   by (auto intro: bool_induct)

  1524

  1525 text {* \medskip @{text Pow} *}

  1526

  1527 lemma Pow_empty [simp]: "Pow {} = {{}}"

  1528   by (auto simp add: Pow_def)

  1529

  1530 lemma Pow_insert: "Pow (insert a A) = Pow A \<union> (insert a  Pow A)"

  1531   by (blast intro: image_eqI [where ?x = "u - {a}", standard])

  1532

  1533 lemma Pow_Compl: "Pow (- A) = {-B | B. A \<in> Pow B}"

  1534   by (blast intro: exI [where ?x = "- u", standard])

  1535

  1536 lemma Pow_UNIV [simp]: "Pow UNIV = UNIV"

  1537   by blast

  1538

  1539 lemma Un_Pow_subset: "Pow A \<union> Pow B \<subseteq> Pow (A \<union> B)"

  1540   by blast

  1541

  1542 lemma Pow_Int_eq [simp]: "Pow (A \<inter> B) = Pow A \<inter> Pow B"

  1543   by blast

  1544

  1545

  1546 text {* \medskip Miscellany. *}

  1547

  1548 lemma set_eq_subset: "(A = B) = (A \<subseteq> B & B \<subseteq> A)"

  1549   by blast

  1550

  1551 lemma subset_iff [no_atp]: "(A \<subseteq> B) = (\<forall>t. t \<in> A --> t \<in> B)"

  1552   by blast

  1553

  1554 lemma subset_iff_psubset_eq: "(A \<subseteq> B) = ((A \<subset> B) | (A = B))"

  1555   by (unfold less_le) blast

  1556

  1557 lemma all_not_in_conv [simp]: "(\<forall>x. x \<notin> A) = (A = {})"

  1558   by blast

  1559

  1560 lemma ex_in_conv: "(\<exists>x. x \<in> A) = (A \<noteq> {})"

  1561   by blast

  1562

  1563 lemma distinct_lemma: "f x \<noteq> f y ==> x \<noteq> y"

  1564   by iprover

  1565

  1566 lemma ball_simps [simp, no_atp]:

  1567   "\<And>A P Q. (\<forall>x\<in>A. P x \<or> Q) \<longleftrightarrow> ((\<forall>x\<in>A. P x) \<or> Q)"

  1568   "\<And>A P Q. (\<forall>x\<in>A. P \<or> Q x) \<longleftrightarrow> (P \<or> (\<forall>x\<in>A. Q x))"

  1569   "\<And>A P Q. (\<forall>x\<in>A. P \<longrightarrow> Q x) \<longleftrightarrow> (P \<longrightarrow> (\<forall>x\<in>A. Q x))"

  1570   "\<And>A P Q. (\<forall>x\<in>A. P x \<longrightarrow> Q) \<longleftrightarrow> ((\<exists>x\<in>A. P x) \<longrightarrow> Q)"

  1571   "\<And>P. (\<forall>x\<in>{}. P x) \<longleftrightarrow> True"

  1572   "\<And>P. (\<forall>x\<in>UNIV. P x) \<longleftrightarrow> (\<forall>x. P x)"

  1573   "\<And>a B P. (\<forall>x\<in>insert a B. P x) \<longleftrightarrow> (P a \<and> (\<forall>x\<in>B. P x))"

  1574   "\<And>P Q. (\<forall>x\<in>Collect Q. P x) \<longleftrightarrow> (\<forall>x. Q x \<longrightarrow> P x)"

  1575   "\<And>A P f. (\<forall>x\<in>fA. P x) \<longleftrightarrow> (\<forall>x\<in>A. P (f x))"

  1576   "\<And>A P. (\<not> (\<forall>x\<in>A. P x)) \<longleftrightarrow> (\<exists>x\<in>A. \<not> P x)"

  1577   by auto

  1578

  1579 lemma bex_simps [simp, no_atp]:

  1580   "\<And>A P Q. (\<exists>x\<in>A. P x \<and> Q) \<longleftrightarrow> ((\<exists>x\<in>A. P x) \<and> Q)"

  1581   "\<And>A P Q. (\<exists>x\<in>A. P \<and> Q x) \<longleftrightarrow> (P \<and> (\<exists>x\<in>A. Q x))"

  1582   "\<And>P. (\<exists>x\<in>{}. P x) \<longleftrightarrow> False"

  1583   "\<And>P. (\<exists>x\<in>UNIV. P x) \<longleftrightarrow> (\<exists>x. P x)"

  1584   "\<And>a B P. (\<exists>x\<in>insert a B. P x) \<longleftrightarrow> (P a | (\<exists>x\<in>B. P x))"

  1585   "\<And>P Q. (\<exists>x\<in>Collect Q. P x) \<longleftrightarrow> (\<exists>x. Q x \<and> P x)"

  1586   "\<And>A P f. (\<exists>x\<in>fA. P x) \<longleftrightarrow> (\<exists>x\<in>A. P (f x))"

  1587   "\<And>A P. (\<not>(\<exists>x\<in>A. P x)) \<longleftrightarrow> (\<forall>x\<in>A. \<not> P x)"

  1588   by auto

  1589

  1590

  1591 subsubsection {* Monotonicity of various operations *}

  1592

  1593 lemma image_mono: "A \<subseteq> B ==> fA \<subseteq> fB"

  1594   by blast

  1595

  1596 lemma Pow_mono: "A \<subseteq> B ==> Pow A \<subseteq> Pow B"

  1597   by blast

  1598

  1599 lemma insert_mono: "C \<subseteq> D ==> insert a C \<subseteq> insert a D"

  1600   by blast

  1601

  1602 lemma Un_mono: "A \<subseteq> C ==> B \<subseteq> D ==> A \<union> B \<subseteq> C \<union> D"

  1603   by (fact sup_mono)

  1604

  1605 lemma Int_mono: "A \<subseteq> C ==> B \<subseteq> D ==> A \<inter> B \<subseteq> C \<inter> D"

  1606   by (fact inf_mono)

  1607

  1608 lemma Diff_mono: "A \<subseteq> C ==> D \<subseteq> B ==> A - B \<subseteq> C - D"

  1609   by blast

  1610

  1611 lemma Compl_anti_mono: "A \<subseteq> B ==> -B \<subseteq> -A"

  1612   by (fact compl_mono)

  1613

  1614 text {* \medskip Monotonicity of implications. *}

  1615

  1616 lemma in_mono: "A \<subseteq> B ==> x \<in> A --> x \<in> B"

  1617   apply (rule impI)

  1618   apply (erule subsetD, assumption)

  1619   done

  1620

  1621 lemma conj_mono: "P1 --> Q1 ==> P2 --> Q2 ==> (P1 & P2) --> (Q1 & Q2)"

  1622   by iprover

  1623

  1624 lemma disj_mono: "P1 --> Q1 ==> P2 --> Q2 ==> (P1 | P2) --> (Q1 | Q2)"

  1625   by iprover

  1626

  1627 lemma imp_mono: "Q1 --> P1 ==> P2 --> Q2 ==> (P1 --> P2) --> (Q1 --> Q2)"

  1628   by iprover

  1629

  1630 lemma imp_refl: "P --> P" ..

  1631

  1632 lemma not_mono: "Q --> P ==> ~ P --> ~ Q"

  1633   by iprover

  1634

  1635 lemma ex_mono: "(!!x. P x --> Q x) ==> (EX x. P x) --> (EX x. Q x)"

  1636   by iprover

  1637

  1638 lemma all_mono: "(!!x. P x --> Q x) ==> (ALL x. P x) --> (ALL x. Q x)"

  1639   by iprover

  1640

  1641 lemma Collect_mono: "(!!x. P x --> Q x) ==> Collect P \<subseteq> Collect Q"

  1642   by blast

  1643

  1644 lemma Int_Collect_mono:

  1645     "A \<subseteq> B ==> (!!x. x \<in> A ==> P x --> Q x) ==> A \<inter> Collect P \<subseteq> B \<inter> Collect Q"

  1646   by blast

  1647

  1648 lemmas basic_monos =

  1649   subset_refl imp_refl disj_mono conj_mono

  1650   ex_mono Collect_mono in_mono

  1651

  1652 lemma eq_to_mono: "a = b ==> c = d ==> b --> d ==> a --> c"

  1653   by iprover

  1654

  1655

  1656 subsubsection {* Inverse image of a function *}

  1657

  1658 definition vimage :: "('a => 'b) => 'b set => 'a set" (infixr "-" 90) where

  1659   "f - B == {x. f x : B}"

  1660

  1661 lemma vimage_eq [simp]: "(a : f - B) = (f a : B)"

  1662   by (unfold vimage_def) blast

  1663

  1664 lemma vimage_singleton_eq: "(a : f - {b}) = (f a = b)"

  1665   by simp

  1666

  1667 lemma vimageI [intro]: "f a = b ==> b:B ==> a : f - B"

  1668   by (unfold vimage_def) blast

  1669

  1670 lemma vimageI2: "f a : A ==> a : f - A"

  1671   by (unfold vimage_def) fast

  1672

  1673 lemma vimageE [elim!]: "a: f - B ==> (!!x. f a = x ==> x:B ==> P) ==> P"

  1674   by (unfold vimage_def) blast

  1675

  1676 lemma vimageD: "a : f - A ==> f a : A"

  1677   by (unfold vimage_def) fast

  1678

  1679 lemma vimage_empty [simp]: "f - {} = {}"

  1680   by blast

  1681

  1682 lemma vimage_Compl: "f - (-A) = -(f - A)"

  1683   by blast

  1684

  1685 lemma vimage_Un [simp]: "f - (A Un B) = (f - A) Un (f - B)"

  1686   by blast

  1687

  1688 lemma vimage_Int [simp]: "f - (A Int B) = (f - A) Int (f - B)"

  1689   by fast

  1690

  1691 lemma vimage_Collect_eq [simp]: "f - Collect P = {y. P (f y)}"

  1692   by blast

  1693

  1694 lemma vimage_Collect: "(!!x. P (f x) = Q x) ==> f - (Collect P) = Collect Q"

  1695   by blast

  1696

  1697 lemma vimage_insert: "f-(insert a B) = (f-{a}) Un (f-B)"

  1698   -- {* NOT suitable for rewriting because of the recurrence of @{term "{a}"}. *}

  1699   by blast

  1700

  1701 lemma vimage_Diff: "f - (A - B) = (f - A) - (f - B)"

  1702   by blast

  1703

  1704 lemma vimage_UNIV [simp]: "f - UNIV = UNIV"

  1705   by blast

  1706

  1707 lemma vimage_mono: "A \<subseteq> B ==> f - A \<subseteq> f - B"

  1708   -- {* monotonicity *}

  1709   by blast

  1710

  1711 lemma vimage_image_eq [no_atp]: "f - (f  A) = {y. EX x:A. f x = f y}"

  1712 by (blast intro: sym)

  1713

  1714 lemma image_vimage_subset: "f  (f - A) <= A"

  1715 by blast

  1716

  1717 lemma image_vimage_eq [simp]: "f  (f - A) = A Int range f"

  1718 by blast

  1719

  1720 lemma vimage_const [simp]: "((\<lambda>x. c) - A) = (if c \<in> A then UNIV else {})"

  1721   by auto

  1722

  1723 lemma vimage_if [simp]: "((\<lambda>x. if x \<in> B then c else d) - A) =

  1724    (if c \<in> A then (if d \<in> A then UNIV else B)

  1725     else if d \<in> A then -B else {})"

  1726   by (auto simp add: vimage_def)

  1727

  1728 lemma vimage_inter_cong:

  1729   "(\<And> w. w \<in> S \<Longrightarrow> f w = g w) \<Longrightarrow> f - y \<inter> S = g - y \<inter> S"

  1730   by auto

  1731

  1732 lemma vimage_ident [simp]: "(%x. x) - Y = Y"

  1733   by blast

  1734

  1735

  1736 subsubsection {* Getting the Contents of a Singleton Set *}

  1737

  1738 definition the_elem :: "'a set \<Rightarrow> 'a" where

  1739   "the_elem X = (THE x. X = {x})"

  1740

  1741 lemma the_elem_eq [simp]: "the_elem {x} = x"

  1742   by (simp add: the_elem_def)

  1743

  1744

  1745 subsubsection {* Least value operator *}

  1746

  1747 lemma Least_mono:

  1748   "mono (f::'a::order => 'b::order) ==> EX x:S. ALL y:S. x <= y

  1749     ==> (LEAST y. y : f  S) = f (LEAST x. x : S)"

  1750     -- {* Courtesy of Stephan Merz *}

  1751   apply clarify

  1752   apply (erule_tac P = "%x. x : S" in LeastI2_order, fast)

  1753   apply (rule LeastI2_order)

  1754   apply (auto elim: monoD intro!: order_antisym)

  1755   done

  1756

  1757 subsection {* Misc *}

  1758

  1759 text {* Rudimentary code generation *}

  1760

  1761 lemma insert_code [code]: "insert y A x \<longleftrightarrow> y = x \<or> A x"

  1762   by (auto simp add: insert_compr Collect_def mem_def)

  1763

  1764 lemma vimage_code [code]: "(f - A) x = A (f x)"

  1765   by (simp add: vimage_def Collect_def mem_def)

  1766

  1767 hide_const (open) member not_member

  1768

  1769 text {* Misc theorem and ML bindings *}

  1770

  1771 lemmas equalityI = subset_antisym

  1772

  1773 ML {*

  1774 val Ball_def = @{thm Ball_def}

  1775 val Bex_def = @{thm Bex_def}

  1776 val CollectD = @{thm CollectD}

  1777 val CollectE = @{thm CollectE}

  1778 val CollectI = @{thm CollectI}

  1779 val Collect_conj_eq = @{thm Collect_conj_eq}

  1780 val Collect_mem_eq = @{thm Collect_mem_eq}

  1781 val IntD1 = @{thm IntD1}

  1782 val IntD2 = @{thm IntD2}

  1783 val IntE = @{thm IntE}

  1784 val IntI = @{thm IntI}

  1785 val Int_Collect = @{thm Int_Collect}

  1786 val UNIV_I = @{thm UNIV_I}

  1787 val UNIV_witness = @{thm UNIV_witness}

  1788 val UnE = @{thm UnE}

  1789 val UnI1 = @{thm UnI1}

  1790 val UnI2 = @{thm UnI2}

  1791 val ballE = @{thm ballE}

  1792 val ballI = @{thm ballI}

  1793 val bexCI = @{thm bexCI}

  1794 val bexE = @{thm bexE}

  1795 val bexI = @{thm bexI}

  1796 val bex_triv = @{thm bex_triv}

  1797 val bspec = @{thm bspec}

  1798 val contra_subsetD = @{thm contra_subsetD}

  1799 val distinct_lemma = @{thm distinct_lemma}

  1800 val eq_to_mono = @{thm eq_to_mono}

  1801 val equalityCE = @{thm equalityCE}

  1802 val equalityD1 = @{thm equalityD1}

  1803 val equalityD2 = @{thm equalityD2}

  1804 val equalityE = @{thm equalityE}

  1805 val equalityI = @{thm equalityI}

  1806 val imageE = @{thm imageE}

  1807 val imageI = @{thm imageI}

  1808 val image_Un = @{thm image_Un}

  1809 val image_insert = @{thm image_insert}

  1810 val insert_commute = @{thm insert_commute}

  1811 val insert_iff = @{thm insert_iff}

  1812 val mem_Collect_eq = @{thm mem_Collect_eq}

  1813 val rangeE = @{thm rangeE}

  1814 val rangeI = @{thm rangeI}

  1815 val range_eqI = @{thm range_eqI}

  1816 val subsetCE = @{thm subsetCE}

  1817 val subsetD = @{thm subsetD}

  1818 val subsetI = @{thm subsetI}

  1819 val subset_refl = @{thm subset_refl}

  1820 val subset_trans = @{thm subset_trans}

  1821 val vimageD = @{thm vimageD}

  1822 val vimageE = @{thm vimageE}

  1823 val vimageI = @{thm vimageI}

  1824 val vimageI2 = @{thm vimageI2}

  1825 val vimage_Collect = @{thm vimage_Collect}

  1826 val vimage_Int = @{thm vimage_Int}

  1827 val vimage_Un = @{thm vimage_Un}

  1828 *}

  1829

  1830 end