src/HOL/Set.thy
 author haftmann Fri Dec 10 16:10:50 2010 +0100 (2010-12-10) changeset 41107 8795cd75965e parent 41082 9ff94e7cc3b3 child 42163 392fd6c4669c permissions -rw-r--r--
moved most fundamental lemmas upwards
     1 (*  Author:     Tobias Nipkow, Lawrence C Paulson and Markus Wenzel *)

     2

     3 header {* Set theory for higher-order logic *}

     4

     5 theory Set

     6 imports Lattices

     7 begin

     8

     9 subsection {* Sets as predicates *}

    10

    11 types 'a set = "'a \<Rightarrow> bool"

    12

    13 definition Collect :: "('a \<Rightarrow> bool) \<Rightarrow> 'a set" where -- "comprehension"

    14   "Collect P = P"

    15

    16 definition member :: "'a \<Rightarrow> 'a set \<Rightarrow> bool" where -- "membership"

    17   mem_def: "member x A = A x"

    18

    19 notation

    20   member  ("op :") and

    21   member  ("(_/ : _)" [50, 51] 50)

    22

    23 abbreviation not_member where

    24   "not_member x A \<equiv> ~ (x : A)" -- "non-membership"

    25

    26 notation

    27   not_member  ("op ~:") and

    28   not_member  ("(_/ ~: _)" [50, 51] 50)

    29

    30 notation (xsymbols)

    31   member      ("op \<in>") and

    32   member      ("(_/ \<in> _)" [50, 51] 50) and

    33   not_member  ("op \<notin>") and

    34   not_member  ("(_/ \<notin> _)" [50, 51] 50)

    35

    36 notation (HTML output)

    37   member      ("op \<in>") and

    38   member      ("(_/ \<in> _)" [50, 51] 50) and

    39   not_member  ("op \<notin>") and

    40   not_member  ("(_/ \<notin> _)" [50, 51] 50)

    41

    42

    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 setup {*

    79 let

    80   val Coll_perm_tac = rtac @{thm Collect_cong} 1 THEN rtac @{thm iffI} 1 THEN

    81     ALLGOALS(EVERY'[REPEAT_DETERM o (etac @{thm conjE}),

    82                     DEPTH_SOLVE_1 o (ares_tac [@{thm conjI}])])

    83   val defColl_regroup = Simplifier.simproc_global @{theory}

    84     "defined Collect" ["{x. P x & Q x}"]

    85     (Quantifier1.rearrange_Coll Coll_perm_tac)

    86 in

    87   Simplifier.map_simpset (fn ss => ss addsimprocs [defColl_regroup])

    88 end

    89 *}

    90

    91 lemmas CollectE = CollectD [elim_format]

    92

    93 lemma set_eqI:

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

    95   shows "A = B"

    96 proof -

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

    98   then show ?thesis by simp

    99 qed

   100

   101 lemma set_eq_iff [no_atp]:

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

   103   by (auto intro:set_eqI)

   104

   105 text {* Set enumerations *}

   106

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

   108   "{} \<equiv> bot"

   109

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

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

   112

   113 syntax

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

   115 translations

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

   117   "{x}" == "CONST insert x {}"

   118

   119

   120 subsection {* Subsets and bounded quantifiers *}

   121

   122 abbreviation

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

   124   "subset \<equiv> less"

   125

   126 abbreviation

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

   128   "subset_eq \<equiv> less_eq"

   129

   130 notation (output)

   131   subset  ("op <") and

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

   133   subset_eq  ("op <=") and

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

   135

   136 notation (xsymbols)

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

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

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

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

   141

   142 notation (HTML output)

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

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

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

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

   147

   148 abbreviation (input)

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

   150   "supset \<equiv> greater"

   151

   152 abbreviation (input)

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

   154   "supset_eq \<equiv> greater_eq"

   155

   156 notation (xsymbols)

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

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

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

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

   161

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

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

   164

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

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

   167

   168 syntax

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

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

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

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

   173

   174 syntax (HOL)

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

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

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

   178

   179 syntax (xsymbols)

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

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

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

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

   184

   185 syntax (HTML output)

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

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

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

   189

   190 translations

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

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

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

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

   195

   196 syntax (output)

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

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

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

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

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

   202

   203 syntax (xsymbols)

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

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

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

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

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

   209

   210 syntax (HOL output)

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

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

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

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

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

   216

   217 syntax (HTML output)

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

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

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

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

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

   223

   224 translations

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

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

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

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

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

   230

   231 print_translation {*

   232 let

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

   234   val All_binder = Syntax.binder_name @{const_syntax All};

   235   val Ex_binder = Syntax.binder_name @{const_syntax Ex};

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

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

   238   val sbset = @{const_syntax subset};

   239   val sbset_eq = @{const_syntax subset_eq};

   240

   241   val trans =

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

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

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

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

   246

   247   fun mk v v' c n P =

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

   249     then Syntax.const c $Syntax.mark_bound v'$ n $P else raise Match;   250   251 fun tr' q = (q,   252 fn [Const (@{syntax_const "_bound"}, _)$ Free (v, Type (T, _)),

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

   289

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

   291 *}

   292

   293 print_translation {*

   294  [Syntax.preserve_binder_abs2_tr' @{const_syntax Ball} @{syntax_const "_Ball"},

   295   Syntax.preserve_binder_abs2_tr' @{const_syntax Bex} @{syntax_const "_Bex"}]

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

   297

   298 print_translation {*

   299 let

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

   301

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

   303     let

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

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

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

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

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

   320         in

   321           case t of

   322             Const (@{const_syntax HOL.conj}, _) $  323 (Const (@{const_syntax Set.member}, _)$

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

   326           | _ => M

   327         end

   328     end;

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

   330 *}

   331

   332 setup {*

   333 let

   334   val unfold_bex_tac = unfold_tac @{thms "Bex_def"};

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

   336   val rearrange_bex = Quantifier1.rearrange_bex prove_bex_tac;

   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   val rearrange_ball = Quantifier1.rearrange_ball prove_ball_tac;

   340   val defBEX_regroup = Simplifier.simproc_global @{theory}

   341     "defined BEX" ["EX x:A. P x & Q x"] rearrange_bex;

   342   val defBALL_regroup = Simplifier.simproc_global @{theory}

   343     "defined BALL" ["ALL x:A. P x --> Q x"] rearrange_ball;

   344 in

   345   Simplifier.map_simpset (fn ss => ss addsimprocs [defBALL_regroup, defBEX_regroup])

   346 end

   347 *}

   348

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

   350   by (simp add: Ball_def)

   351

   352 lemmas strip = impI allI ballI

   353

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

   355   by (simp add: Ball_def)

   356

   357 text {*

   358   Gives better instantiation for bound:

   359 *}

   360

   361 declaration {* fn _ =>

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

   363 *}

   364

   365 ML {*

   366 structure Simpdata =

   367 struct

   368

   369 open Simpdata;

   370

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

   372

   373 end;

   374

   375 open Simpdata;

   376 *}

   377

   378 declaration {* fn _ =>

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

   380 *}

   381

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

   383   by (unfold Ball_def) blast

   384

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

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

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

   388   by (unfold Bex_def) blast

   389

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

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

   392   by (unfold Bex_def) blast

   393

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

   395   by (unfold Bex_def) blast

   396

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

   398   by (unfold Bex_def) blast

   399

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

   401   -- {* Trival rewrite rule. *}

   402   by (simp add: Ball_def)

   403

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

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

   406   by (simp add: Bex_def)

   407

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

   409   by blast

   410

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

   412   by blast

   413

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

   415   by blast

   416

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

   418   by blast

   419

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

   421   by blast

   422

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

   424   by blast

   425

   426

   427 text {* Congruence rules *}

   428

   429 lemma ball_cong:

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

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

   432   by (simp add: Ball_def)

   433

   434 lemma strong_ball_cong [cong]:

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

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

   437   by (simp add: simp_implies_def Ball_def)

   438

   439 lemma bex_cong:

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

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

   442   by (simp add: Bex_def cong: conj_cong)

   443

   444 lemma strong_bex_cong [cong]:

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

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

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

   448

   449

   450 subsection {* Basic operations *}

   451

   452 subsubsection {* Subsets *}

   453

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

   455   unfolding mem_def by (rule le_funI, rule le_boolI)

   456

   457 text {*

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

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

   460   "'a set"}.

   461 *}

   462

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

   464   unfolding mem_def by (erule le_funE, erule le_boolE)

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

   466

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

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

   469       cf @{text rev_mp}. *}

   470   by (rule subsetD)

   471

   472 text {*

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

   474 *}

   475

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

   477   -- {* Classical elimination rule. *}

   478   unfolding mem_def by (blast dest: le_funE le_boolE)

   479

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

   481

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

   483   by blast

   484

   485 lemma subset_refl [simp]: "A \<subseteq> A"

   486   by (fact order_refl)

   487

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

   489   by (fact order_trans)

   490

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

   492   by (rule subsetD)

   493

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

   495   by (rule subsetD)

   496

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

   498   by simp

   499

   500 lemmas basic_trans_rules [trans] =

   501   order_trans_rules set_rev_mp set_mp eq_mem_trans

   502

   503

   504 subsubsection {* Equality *}

   505

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

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

   508   by (iprover intro: set_eqI subsetD)

   509

   510 text {*

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

   512   here?

   513 *}

   514

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

   516   by simp

   517

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

   519   by simp

   520

   521 text {*

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

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

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

   525 *}

   526

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

   528   by simp

   529

   530 lemma equalityCE [elim]:

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

   532   by blast

   533

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

   535   by simp

   536

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

   538   by simp

   539

   540

   541 subsubsection {* The empty set *}

   542

   543 lemma empty_def:

   544   "{} = {x. False}"

   545   by (simp add: bot_fun_def bot_bool_def Collect_def)

   546

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

   548   by (simp add: empty_def)

   549

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

   551   by simp

   552

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

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

   555   by blast

   556

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

   558   by blast

   559

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

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

   562   by blast

   563

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

   565   by (simp add: Ball_def)

   566

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

   568   by (simp add: Bex_def)

   569

   570

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

   572

   573 abbreviation UNIV :: "'a set" where

   574   "UNIV \<equiv> top"

   575

   576 lemma UNIV_def:

   577   "UNIV = {x. True}"

   578   by (simp add: top_fun_def top_bool_def Collect_def)

   579

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

   581   by (simp add: UNIV_def)

   582

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

   584

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

   586   by simp

   587

   588 lemma subset_UNIV [simp]: "A \<subseteq> UNIV"

   589   by (rule subsetI) (rule UNIV_I)

   590

   591 text {*

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

   593   causes them to be ignored because of their interaction with

   594   congruence rules.

   595 *}

   596

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

   598   by (simp add: Ball_def)

   599

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

   601   by (simp add: Bex_def)

   602

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

   604   by auto

   605

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

   607   by (blast elim: equalityE)

   608

   609

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

   611

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

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

   614

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

   616   by (simp add: Pow_def)

   617

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

   619   by (simp add: Pow_def)

   620

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

   622   by (simp add: Pow_def)

   623

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

   625   by simp

   626

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

   628   by simp

   629

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

   631   using Pow_top by blast

   632

   633

   634 subsubsection {* Set complement *}

   635

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

   637   by (simp add: mem_def fun_Compl_def bool_Compl_def)

   638

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

   640   by (unfold mem_def fun_Compl_def bool_Compl_def) blast

   641

   642 text {*

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

   644   Classical prover.  Negated assumptions behave like formulae on the

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

   646

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

   648   by (simp add: mem_def fun_Compl_def bool_Compl_def)

   649

   650 lemmas ComplE = ComplD [elim_format]

   651

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

   653

   654

   655 subsubsection {* Binary intersection *}

   656

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

   658   "op Int \<equiv> inf"

   659

   660 notation (xsymbols)

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

   662

   663 notation (HTML output)

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

   665

   666 lemma Int_def:

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

   668   by (simp add: inf_fun_def inf_bool_def Collect_def mem_def)

   669

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

   671   by (unfold Int_def) blast

   672

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

   674   by simp

   675

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

   677   by simp

   678

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

   680   by simp

   681

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

   683   by simp

   684

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

   686   by (fact mono_inf)

   687

   688

   689 subsubsection {* Binary union *}

   690

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

   692   "union \<equiv> sup"

   693

   694 notation (xsymbols)

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

   696

   697 notation (HTML output)

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

   699

   700 lemma Un_def:

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

   702   by (simp add: sup_fun_def sup_bool_def Collect_def mem_def)

   703

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

   705   by (unfold Un_def) blast

   706

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

   708   by simp

   709

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

   711   by simp

   712

   713 text {*

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

   715   @{prop B}.

   716 *}

   717

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

   719   by auto

   720

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

   722   by (unfold Un_def) blast

   723

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

   725   by (simp add: Collect_def mem_def insert_compr Un_def)

   726

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

   728   by (fact mono_sup)

   729

   730

   731 subsubsection {* Set difference *}

   732

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

   734   by (simp add: mem_def fun_diff_def bool_diff_def)

   735

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

   737   by simp

   738

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

   740   by simp

   741

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

   743   by simp

   744

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

   746   by simp

   747

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

   749

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

   751 by blast

   752

   753

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

   755

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

   757   by (unfold insert_def) blast

   758

   759 lemma insertI1: "a : insert a B"

   760   by simp

   761

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

   763   by simp

   764

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

   766   by (unfold insert_def) blast

   767

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

   769   -- {* Classical introduction rule. *}

   770   by auto

   771

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

   773   by auto

   774

   775 lemma set_insert:

   776   assumes "x \<in> A"

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

   778 proof

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

   780 next

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

   782 qed

   783

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

   785 by auto

   786

   787 subsubsection {* Singletons, using insert *}

   788

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

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

   791   by (rule insertI1)

   792

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

   794   by blast

   795

   796 lemmas singletonE = singletonD [elim_format]

   797

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

   799   by blast

   800

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

   802   by blast

   803

   804 lemma singleton_insert_inj_eq [iff,no_atp]:

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

   806   by blast

   807

   808 lemma singleton_insert_inj_eq' [iff,no_atp]:

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

   810   by blast

   811

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

   813   by fast

   814

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

   816   by blast

   817

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

   819   by blast

   820

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

   822   by blast

   823

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

   825   by (blast elim: equalityE)

   826

   827

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

   829

   830 text {*

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

   832 *}

   833

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

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

   836

   837 abbreviation

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

   839   "range f == f  UNIV"

   840

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

   842   by (unfold image_def) blast

   843

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

   845   by (rule image_eqI) (rule refl)

   846

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

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

   849     required @{term x}. *}

   850   by (unfold image_def) blast

   851

   852 lemma imageE [elim!]:

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

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

   855   by (unfold image_def) blast

   856

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

   858   by blast

   859

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

   861   by blast

   862

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

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

   865   by blast

   866

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

   868   apply safe

   869    prefer 2 apply fast

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

   871   done

   872

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

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

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

   876   by blast

   877

   878 text {*

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

   880 *}

   881

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

   883   by simp

   884

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

   886   by simp

   887

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

   889   by blast

   890

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

   892

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

   894

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

   896   by auto

   897

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

   899   by auto

   900

   901 text {*

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

   903   "split_if [split]"}.

   904 *}

   905

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

   907   by (rule split_if)

   908

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

   910   by (rule split_if)

   911

   912 text {*

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

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

   915 *}

   916

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

   918   by (rule split_if)

   919

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

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

   922

   923 lemmas split_ifs = if_bool_eq_conj split_if_eq1 split_if_eq2 split_if_mem1 split_if_mem2

   924

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

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

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

   928   apply, then the formula should be kept.

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

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

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

   932  *)

   933

   934

   935 subsection {* Further operations and lemmas *}

   936

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

   938

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

   940   by (unfold less_le) blast

   941

   942 lemma psubsetE [elim!,no_atp]:

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

   944   by (unfold less_le) blast

   945

   946 lemma psubset_insert_iff:

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

   948   by (auto simp add: less_le subset_insert_iff)

   949

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

   951   by (simp only: less_le)

   952

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

   954   by (simp add: psubset_eq)

   955

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

   957 apply (unfold less_le)

   958 apply (auto dest: subset_antisym)

   959 done

   960

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

   962 apply (unfold less_le)

   963 apply (auto dest: subsetD)

   964 done

   965

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

   967   by (auto simp add: psubset_eq)

   968

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

   970   by (auto simp add: psubset_eq)

   971

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

   973   by (unfold less_le) blast

   974

   975 lemma atomize_ball:

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

   977   by (simp only: Ball_def atomize_all atomize_imp)

   978

   979 lemmas [symmetric, rulify] = atomize_ball

   980   and [symmetric, defn] = atomize_ball

   981

   982 lemma image_Pow_mono:

   983   assumes "f  A \<le> B"

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

   985 using assms by blast

   986

   987 lemma image_Pow_surj:

   988   assumes "f  A = B"

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

   990 using assms unfolding Pow_def proof(auto)

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

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

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

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

   995 qed

   996

   997 subsubsection {* Derived rules involving subsets. *}

   998

   999 text {* @{text insert}. *}

  1000

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

  1002   by (rule subsetI) (erule insertI2)

  1003

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

  1005   by blast

  1006

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

  1008   by blast

  1009

  1010

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

  1012

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

  1014   by (fact sup_ge1)

  1015

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

  1017   by (fact sup_ge2)

  1018

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

  1020   by (fact sup_least)

  1021

  1022

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

  1024

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

  1026   by (fact inf_le1)

  1027

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

  1029   by (fact inf_le2)

  1030

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

  1032   by (fact inf_greatest)

  1033

  1034

  1035 text {* \medskip Set difference. *}

  1036

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

  1038   by blast

  1039

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

  1041 by blast

  1042

  1043

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

  1045

  1046 text {* @{text "{}"}. *}

  1047

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

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

  1050   by auto

  1051

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

  1053   by blast

  1054

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

  1056   by (unfold less_le) blast

  1057

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

  1059 by blast

  1060

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

  1062 by blast

  1063

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

  1065   by blast

  1066

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

  1068   by blast

  1069

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

  1071   by blast

  1072

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

  1074   by blast

  1075

  1076

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

  1078

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

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

  1081   by blast

  1082

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

  1084   by blast

  1085

  1086 lemmas empty_not_insert = insert_not_empty [symmetric, standard]

  1087 declare empty_not_insert [simp]

  1088

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

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

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

  1092   by blast

  1093

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

  1095   by blast

  1096

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

  1098   by blast

  1099

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

  1101   by blast

  1102

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

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

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

  1106   done

  1107

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

  1109   by auto

  1110

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

  1112   by blast

  1113

  1114 lemma insert_disjoint [simp,no_atp]:

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

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

  1117   by auto

  1118

  1119 lemma disjoint_insert [simp,no_atp]:

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

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

  1122   by auto

  1123

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

  1125

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

  1127   by blast

  1128

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

  1130   by blast

  1131

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

  1133   by auto

  1134

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

  1136 by auto

  1137

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

  1139 by blast

  1140

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

  1142 by blast

  1143

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

  1145 by blast

  1146

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

  1148 by blast

  1149

  1150

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

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

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

  1154       equational properties than does the RHS. *}

  1155   by blast

  1156

  1157 lemma if_image_distrib [simp]:

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

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

  1160   by (auto simp add: image_def)

  1161

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

  1163   by (simp add: image_def)

  1164

  1165

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

  1167

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

  1169   by auto

  1170

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

  1172 by (subst image_image, simp)

  1173

  1174

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

  1176

  1177 lemma Int_absorb [simp]: "A \<inter> A = A"

  1178   by (fact inf_idem)

  1179

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

  1181   by (fact inf_left_idem)

  1182

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

  1184   by (fact inf_commute)

  1185

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

  1187   by (fact inf_left_commute)

  1188

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

  1190   by (fact inf_assoc)

  1191

  1192 lemmas Int_ac = Int_assoc Int_left_absorb Int_commute Int_left_commute

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

  1194

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

  1196   by (fact inf_absorb2)

  1197

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

  1199   by (fact inf_absorb1)

  1200

  1201 lemma Int_empty_left [simp]: "{} \<inter> B = {}"

  1202   by (fact inf_bot_left)

  1203

  1204 lemma Int_empty_right [simp]: "A \<inter> {} = {}"

  1205   by (fact inf_bot_right)

  1206

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

  1208   by blast

  1209

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

  1211   by blast

  1212

  1213 lemma Int_UNIV_left [simp]: "UNIV \<inter> B = B"

  1214   by (fact inf_top_left)

  1215

  1216 lemma Int_UNIV_right [simp]: "A \<inter> UNIV = A"

  1217   by (fact inf_top_right)

  1218

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

  1220   by (fact inf_sup_distrib1)

  1221

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

  1223   by (fact inf_sup_distrib2)

  1224

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

  1226   by (fact inf_eq_top_iff)

  1227

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

  1229   by (fact le_inf_iff)

  1230

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

  1232   by blast

  1233

  1234

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

  1236

  1237 lemma Un_absorb [simp]: "A \<union> A = A"

  1238   by (fact sup_idem)

  1239

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

  1241   by (fact sup_left_idem)

  1242

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

  1244   by (fact sup_commute)

  1245

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

  1247   by (fact sup_left_commute)

  1248

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

  1250   by (fact sup_assoc)

  1251

  1252 lemmas Un_ac = Un_assoc Un_left_absorb Un_commute Un_left_commute

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

  1254

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

  1256   by (fact sup_absorb2)

  1257

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

  1259   by (fact sup_absorb1)

  1260

  1261 lemma Un_empty_left [simp]: "{} \<union> B = B"

  1262   by (fact sup_bot_left)

  1263

  1264 lemma Un_empty_right [simp]: "A \<union> {} = A"

  1265   by (fact sup_bot_right)

  1266

  1267 lemma Un_UNIV_left [simp]: "UNIV \<union> B = UNIV"

  1268   by (fact sup_top_left)

  1269

  1270 lemma Un_UNIV_right [simp]: "A \<union> UNIV = UNIV"

  1271   by (fact sup_top_right)

  1272

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

  1274   by blast

  1275

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

  1277   by blast

  1278

  1279 lemma Int_insert_left:

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

  1281   by auto

  1282

  1283 lemma Int_insert_left_if0[simp]:

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

  1285   by auto

  1286

  1287 lemma Int_insert_left_if1[simp]:

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

  1289   by auto

  1290

  1291 lemma Int_insert_right:

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

  1293   by auto

  1294

  1295 lemma Int_insert_right_if0[simp]:

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

  1297   by auto

  1298

  1299 lemma Int_insert_right_if1[simp]:

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

  1301   by auto

  1302

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

  1304   by (fact sup_inf_distrib1)

  1305

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

  1307   by (fact sup_inf_distrib2)

  1308

  1309 lemma Un_Int_crazy:

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

  1311   by blast

  1312

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

  1314   by (fact le_iff_sup)

  1315

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

  1317   by (fact sup_eq_bot_iff)

  1318

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

  1320   by (fact le_sup_iff)

  1321

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

  1323   by blast

  1324

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

  1326   by blast

  1327

  1328

  1329 text {* \medskip Set complement *}

  1330

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

  1332   by (fact inf_compl_bot)

  1333

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

  1335   by (fact compl_inf_bot)

  1336

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

  1338   by (fact sup_compl_top)

  1339

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

  1341   by (fact compl_sup_top)

  1342

  1343 lemma double_complement [simp]: "- (-A) = (A::'a set)"

  1344   by (fact double_compl)

  1345

  1346 lemma Compl_Un [simp]: "-(A \<union> B) = (-A) \<inter> (-B)"

  1347   by (fact compl_sup)

  1348

  1349 lemma Compl_Int [simp]: "-(A \<inter> B) = (-A) \<union> (-B)"

  1350   by (fact compl_inf)

  1351

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

  1353   by blast

  1354

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

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

  1357   by blast

  1358

  1359 lemma Compl_UNIV_eq [simp]: "-UNIV = {}"

  1360   by (fact compl_top_eq)

  1361

  1362 lemma Compl_empty_eq [simp]: "-{} = UNIV"

  1363   by (fact compl_bot_eq)

  1364

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

  1366   by (fact compl_le_compl_iff)

  1367

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

  1369   by (fact compl_eq_compl_iff)

  1370

  1371 text {* \medskip Bounded quantifiers.

  1372

  1373   The following are not added to the default simpset because

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

  1375

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

  1377   by blast

  1378

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

  1380   by blast

  1381

  1382

  1383 text {* \medskip Set difference. *}

  1384

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

  1386   by blast

  1387

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

  1389   by blast

  1390

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

  1392   by blast

  1393

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

  1395 by blast

  1396

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

  1398   by (blast elim: equalityE)

  1399

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

  1401   by blast

  1402

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

  1404   by blast

  1405

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

  1407   by blast

  1408

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

  1410   by blast

  1411

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

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

  1414   by blast

  1415

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

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

  1418   by blast

  1419

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

  1421   by auto

  1422

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

  1424   by blast

  1425

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

  1427 by blast

  1428

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

  1430   by blast

  1431

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

  1433   by auto

  1434

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

  1436   by blast

  1437

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

  1439   by blast

  1440

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

  1442   by blast

  1443

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

  1445   by blast

  1446

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

  1448   by blast

  1449

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

  1451   by blast

  1452

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

  1454   by blast

  1455

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

  1457   by blast

  1458

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

  1460   by blast

  1461

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

  1463   by blast

  1464

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

  1466   by blast

  1467

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

  1469   by auto

  1470

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

  1472   by blast

  1473

  1474

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

  1476

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

  1478   by (cases x) auto

  1479

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

  1481   by (auto intro: bool_induct)

  1482

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

  1484   by (cases x) auto

  1485

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

  1487   by (auto intro: bool_contrapos)

  1488

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

  1490

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

  1492   by (auto simp add: Pow_def)

  1493

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

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

  1496

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

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

  1499

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

  1501   by blast

  1502

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

  1504   by blast

  1505

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

  1507   by blast

  1508

  1509

  1510 text {* \medskip Miscellany. *}

  1511

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

  1513   by blast

  1514

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

  1516   by blast

  1517

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

  1519   by (unfold less_le) blast

  1520

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

  1522   by blast

  1523

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

  1525   by blast

  1526

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

  1528   by iprover

  1529

  1530

  1531 subsubsection {* Monotonicity of various operations *}

  1532

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

  1534   by blast

  1535

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

  1537   by blast

  1538

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

  1540   by blast

  1541

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

  1543   by (fact sup_mono)

  1544

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

  1546   by (fact inf_mono)

  1547

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

  1549   by blast

  1550

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

  1552   by (fact compl_mono)

  1553

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

  1555

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

  1557   apply (rule impI)

  1558   apply (erule subsetD, assumption)

  1559   done

  1560

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

  1562   by iprover

  1563

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

  1565   by iprover

  1566

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

  1568   by iprover

  1569

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

  1571

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

  1573   by iprover

  1574

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

  1576   by iprover

  1577

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

  1579   by iprover

  1580

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

  1582   by blast

  1583

  1584 lemma Int_Collect_mono:

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

  1586   by blast

  1587

  1588 lemmas basic_monos =

  1589   subset_refl imp_refl disj_mono conj_mono

  1590   ex_mono Collect_mono in_mono

  1591

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

  1593   by iprover

  1594

  1595

  1596 subsubsection {* Inverse image of a function *}

  1597

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

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

  1600

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

  1602   by (unfold vimage_def) blast

  1603

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

  1605   by simp

  1606

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

  1608   by (unfold vimage_def) blast

  1609

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

  1611   by (unfold vimage_def) fast

  1612

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

  1614   by (unfold vimage_def) blast

  1615

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

  1617   by (unfold vimage_def) fast

  1618

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

  1620   by blast

  1621

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

  1623   by blast

  1624

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

  1626   by blast

  1627

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

  1629   by fast

  1630

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

  1632   by blast

  1633

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

  1635   by blast

  1636

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

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

  1639   by blast

  1640

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

  1642   by blast

  1643

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

  1645   by blast

  1646

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

  1648   -- {* monotonicity *}

  1649   by blast

  1650

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

  1652 by (blast intro: sym)

  1653

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

  1655 by blast

  1656

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

  1658 by blast

  1659

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

  1661   by auto

  1662

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

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

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

  1666   by (auto simp add: vimage_def)

  1667

  1668 lemma vimage_inter_cong:

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

  1670   by auto

  1671

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

  1673 by blast

  1674

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

  1676 by blast

  1677

  1678

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

  1680

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

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

  1683

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

  1685   by (simp add: the_elem_def)

  1686

  1687

  1688 subsubsection {* Least value operator *}

  1689

  1690 lemma Least_mono:

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

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

  1693     -- {* Courtesy of Stephan Merz *}

  1694   apply clarify

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

  1696   apply (rule LeastI2_order)

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

  1698   done

  1699

  1700 subsection {* Misc *}

  1701

  1702 text {* Rudimentary code generation *}

  1703

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

  1705   by (auto simp add: insert_compr Collect_def mem_def)

  1706

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

  1708   by (simp add: vimage_def Collect_def mem_def)

  1709

  1710 hide_const (open) member

  1711

  1712 text {* Misc theorem and ML bindings *}

  1713

  1714 lemmas equalityI = subset_antisym

  1715

  1716 ML {*

  1717 val Ball_def = @{thm Ball_def}

  1718 val Bex_def = @{thm Bex_def}

  1719 val CollectD = @{thm CollectD}

  1720 val CollectE = @{thm CollectE}

  1721 val CollectI = @{thm CollectI}

  1722 val Collect_conj_eq = @{thm Collect_conj_eq}

  1723 val Collect_mem_eq = @{thm Collect_mem_eq}

  1724 val IntD1 = @{thm IntD1}

  1725 val IntD2 = @{thm IntD2}

  1726 val IntE = @{thm IntE}

  1727 val IntI = @{thm IntI}

  1728 val Int_Collect = @{thm Int_Collect}

  1729 val UNIV_I = @{thm UNIV_I}

  1730 val UNIV_witness = @{thm UNIV_witness}

  1731 val UnE = @{thm UnE}

  1732 val UnI1 = @{thm UnI1}

  1733 val UnI2 = @{thm UnI2}

  1734 val ballE = @{thm ballE}

  1735 val ballI = @{thm ballI}

  1736 val bexCI = @{thm bexCI}

  1737 val bexE = @{thm bexE}

  1738 val bexI = @{thm bexI}

  1739 val bex_triv = @{thm bex_triv}

  1740 val bspec = @{thm bspec}

  1741 val contra_subsetD = @{thm contra_subsetD}

  1742 val distinct_lemma = @{thm distinct_lemma}

  1743 val eq_to_mono = @{thm eq_to_mono}

  1744 val equalityCE = @{thm equalityCE}

  1745 val equalityD1 = @{thm equalityD1}

  1746 val equalityD2 = @{thm equalityD2}

  1747 val equalityE = @{thm equalityE}

  1748 val equalityI = @{thm equalityI}

  1749 val imageE = @{thm imageE}

  1750 val imageI = @{thm imageI}

  1751 val image_Un = @{thm image_Un}

  1752 val image_insert = @{thm image_insert}

  1753 val insert_commute = @{thm insert_commute}

  1754 val insert_iff = @{thm insert_iff}

  1755 val mem_Collect_eq = @{thm mem_Collect_eq}

  1756 val rangeE = @{thm rangeE}

  1757 val rangeI = @{thm rangeI}

  1758 val range_eqI = @{thm range_eqI}

  1759 val subsetCE = @{thm subsetCE}

  1760 val subsetD = @{thm subsetD}

  1761 val subsetI = @{thm subsetI}

  1762 val subset_refl = @{thm subset_refl}

  1763 val subset_trans = @{thm subset_trans}

  1764 val vimageD = @{thm vimageD}

  1765 val vimageE = @{thm vimageE}

  1766 val vimageI = @{thm vimageI}

  1767 val vimageI2 = @{thm vimageI2}

  1768 val vimage_Collect = @{thm vimage_Collect}

  1769 val vimage_Int = @{thm vimage_Int}

  1770 val vimage_Un = @{thm vimage_Un}

  1771 *}

  1772

  1773 end