src/HOL/Set.thy
 author wenzelm Thu Feb 11 23:00:22 2010 +0100 (2010-02-11) changeset 35115 446c5063e4fd parent 34999 5312d2ffee3b child 35416 d8d7d1b785af permissions -rw-r--r--
modernized translations;
formal markup of @{syntax_const} and @{const_syntax};
minor tuning;
     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 global

    12

    13 types 'a set = "'a => bool"

    14

    15 consts

    16   Collect       :: "('a => bool) => 'a set"              -- "comprehension"

    17   "op :"        :: "'a => 'a set => bool"                -- "membership"

    18

    19 local

    20

    21 notation

    22   "op :"  ("op :") and

    23   "op :"  ("(_/ : _)" [50, 51] 50)

    24

    25 defs

    26   mem_def [code]: "x : S == S x"

    27   Collect_def [code]: "Collect P == P"

    28

    29 abbreviation

    30   "not_mem x A == ~ (x : A)" -- "non-membership"

    31

    32 notation

    33   not_mem  ("op ~:") and

    34   not_mem  ("(_/ ~: _)" [50, 51] 50)

    35

    36 notation (xsymbols)

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

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

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

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

    41

    42 notation (HTML output)

    43   "op :"  ("op \<in>") and

    44   "op :"  ("(_/ \<in> _)" [50, 51] 50) and

    45   not_mem  ("op \<notin>") and

    46   not_mem  ("(_/ \<notin> _)" [50, 51] 50)

    47

    48 text {* Set comprehensions *}

    49

    50 syntax

    51   "_Coll" :: "pttrn => bool => 'a set"    ("(1{_./ _})")

    52 translations

    53   "{x. P}" == "CONST Collect (%x. P)"

    54

    55 syntax

    56   "_Collect" :: "idt => 'a set => bool => 'a set"    ("(1{_ :/ _./ _})")

    57 syntax (xsymbols)

    58   "_Collect" :: "idt => 'a set => bool => 'a set"    ("(1{_ \<in>/ _./ _})")

    59 translations

    60   "{x:A. P}" => "{x. x:A & P}"

    61

    62 lemma mem_Collect_eq [iff]: "(a : {x. P(x)}) = P(a)"

    63   by (simp add: Collect_def mem_def)

    64

    65 lemma Collect_mem_eq [simp]: "{x. x:A} = A"

    66   by (simp add: Collect_def mem_def)

    67

    68 lemma CollectI: "P(a) ==> a : {x. P(x)}"

    69   by simp

    70

    71 lemma CollectD: "a : {x. P(x)} ==> P(a)"

    72   by simp

    73

    74 lemma Collect_cong: "(!!x. P x = Q x) ==> {x. P(x)} = {x. Q(x)}"

    75   by simp

    76

    77 text {*

    78 Simproc for pulling @{text "x=t"} in @{text "{x. \<dots> & x=t & \<dots>}"}

    79 to the front (and similarly for @{text "t=x"}):

    80 *}

    81

    82 setup {*

    83 let

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

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

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

    87   val defColl_regroup = Simplifier.simproc @{theory}

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

    89     (Quantifier1.rearrange_Coll Coll_perm_tac)

    90 in

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

    92 end

    93 *}

    94

    95 lemmas CollectE = CollectD [elim_format]

    96

    97 text {* Set enumerations *}

    98

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

   100   "{} \<equiv> bot"

   101

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

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

   104

   105 syntax

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

   107 translations

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

   109   "{x}" == "CONST insert x {}"

   110

   111

   112 subsection {* Subsets and bounded quantifiers *}

   113

   114 abbreviation

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

   116   "subset \<equiv> less"

   117

   118 abbreviation

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

   120   "subset_eq \<equiv> less_eq"

   121

   122 notation (output)

   123   subset  ("op <") and

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

   125   subset_eq  ("op <=") and

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

   127

   128 notation (xsymbols)

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

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

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

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

   133

   134 notation (HTML output)

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

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

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

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

   139

   140 abbreviation (input)

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

   142   "supset \<equiv> greater"

   143

   144 abbreviation (input)

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

   146   "supset_eq \<equiv> greater_eq"

   147

   148 notation (xsymbols)

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

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

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

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

   153

   154 global

   155

   156 consts

   157   Ball          :: "'a set => ('a => bool) => bool"      -- "bounded universal quantifiers"

   158   Bex           :: "'a set => ('a => bool) => bool"      -- "bounded existential quantifiers"

   159

   160 local

   161

   162 defs

   163   Ball_def:     "Ball A P       == ALL x. x:A --> P(x)"

   164   Bex_def:      "Bex A P        == EX x. x:A & P(x)"

   165

   166 syntax

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

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

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

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

   171

   172 syntax (HOL)

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

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

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

   176

   177 syntax (xsymbols)

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

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

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

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

   182

   183 syntax (HTML output)

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

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

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

   187

   188 translations

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

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

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

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

   193

   194 syntax (output)

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

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

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

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

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

   200

   201 syntax (xsymbols)

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

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

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

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

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

   207

   208 syntax (HOL output)

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

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

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

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

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

   214

   215 syntax (HTML output)

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

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

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

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

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

   221

   222 translations

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

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

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

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

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

   228

   229 print_translation {*

   230 let

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

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

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

   234   val impl = @{const_syntax "op -->"};

   235   val conj = @{const_syntax "op &"};

   236   val sbset = @{const_syntax subset};

   237   val sbset_eq = @{const_syntax subset_eq};

   238

   239   val trans =

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

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

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

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

   244

   245   fun mk v v' c n P =

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

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

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

   287

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

   289 *}

   290

   291 print_translation {*

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

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

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

   295

   296 print_translation {*

   297 let

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

   299

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

   301     let

   302       fun check (Const (@{const_syntax Ex}, _) $Abs (_, _, P), n) = check (P, n + 1)   303 | check (Const (@{const_syntax "op &"}, _)$

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

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

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

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

   318         in

   319           case t of

   320             Const (@{const_syntax "op &"}, _) $  321 (Const (@{const_syntax "op :"}, _)$

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

   324           | _ => M

   325         end

   326     end;

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

   328 *}

   329

   330 setup {*

   331 let

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

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

   334   val rearrange_bex = Quantifier1.rearrange_bex prove_bex_tac;

   335   val unfold_ball_tac = unfold_tac @{thms "Ball_def"};

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

   337   val rearrange_ball = Quantifier1.rearrange_ball prove_ball_tac;

   338   val defBEX_regroup = Simplifier.simproc @{theory}

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

   340   val defBALL_regroup = Simplifier.simproc @{theory}

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

   342 in

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

   344 end

   345 *}

   346

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

   348   by (simp add: Ball_def)

   349

   350 lemmas strip = impI allI ballI

   351

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

   353   by (simp add: Ball_def)

   354

   355 text {*

   356   Gives better instantiation for bound:

   357 *}

   358

   359 declaration {* fn _ =>

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

   361 *}

   362

   363 ML {*

   364 structure Simpdata =

   365 struct

   366

   367 open Simpdata;

   368

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

   370

   371 end;

   372

   373 open Simpdata;

   374 *}

   375

   376 declaration {* fn _ =>

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

   378 *}

   379

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

   381   by (unfold Ball_def) blast

   382

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

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

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

   386   by (unfold Bex_def) blast

   387

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

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

   390   by (unfold Bex_def) blast

   391

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

   393   by (unfold Bex_def) blast

   394

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

   396   by (unfold Bex_def) blast

   397

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

   399   -- {* Trival rewrite rule. *}

   400   by (simp add: Ball_def)

   401

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

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

   404   by (simp add: Bex_def)

   405

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

   407   by blast

   408

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

   410   by blast

   411

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

   413   by blast

   414

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

   416   by blast

   417

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

   419   by blast

   420

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

   422   by blast

   423

   424

   425 text {* Congruence rules *}

   426

   427 lemma ball_cong:

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

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

   430   by (simp add: Ball_def)

   431

   432 lemma strong_ball_cong [cong]:

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

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

   435   by (simp add: simp_implies_def Ball_def)

   436

   437 lemma bex_cong:

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

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

   440   by (simp add: Bex_def cong: conj_cong)

   441

   442 lemma strong_bex_cong [cong]:

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

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

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

   446

   447

   448 subsection {* Basic operations *}

   449

   450 subsubsection {* Subsets *}

   451

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

   453   unfolding mem_def by (rule le_funI, rule le_boolI)

   454

   455 text {*

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

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

   458   "'a set"}.

   459 *}

   460

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

   462   unfolding mem_def by (erule le_funE, erule le_boolE)

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

   464

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

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

   467       cf @{text rev_mp}. *}

   468   by (rule subsetD)

   469

   470 text {*

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

   472 *}

   473

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

   475   -- {* Classical elimination rule. *}

   476   unfolding mem_def by (blast dest: le_funE le_boolE)

   477

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

   479

   480 lemma contra_subsetD [noatp]: "A \<subseteq> B ==> c \<notin> B ==> c \<notin> A"

   481   by blast

   482

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

   484   by (fact order_refl)

   485

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

   487   by (fact order_trans)

   488

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

   490   by (rule subsetD)

   491

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

   493   by (rule subsetD)

   494

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

   496   by simp

   497

   498 lemmas basic_trans_rules [trans] =

   499   order_trans_rules set_rev_mp set_mp eq_mem_trans

   500

   501

   502 subsubsection {* Equality *}

   503

   504 lemma set_ext: assumes prem: "(!!x. (x:A) = (x:B))" shows "A = B"

   505   apply (rule prem [THEN ext, THEN arg_cong, THEN box_equals])

   506    apply (rule Collect_mem_eq)

   507   apply (rule Collect_mem_eq)

   508   done

   509

   510 (* Due to Brian Huffman *)

   511 lemma expand_set_eq: "(A = B) = (ALL x. (x:A) = (x:B))"

   512 by(auto intro:set_ext)

   513

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

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

   516   by (iprover intro: set_ext subsetD)

   517

   518 text {*

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

   520   here?

   521 *}

   522

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

   524   by simp

   525

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

   527   by simp

   528

   529 text {*

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

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

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

   533 *}

   534

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

   536   by simp

   537

   538 lemma equalityCE [elim]:

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

   540   by blast

   541

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

   543   by simp

   544

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

   546   by simp

   547

   548

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

   550

   551 abbreviation UNIV :: "'a set" where

   552   "UNIV \<equiv> top"

   553

   554 lemma UNIV_def:

   555   "UNIV = {x. True}"

   556   by (simp add: top_fun_eq top_bool_eq Collect_def)

   557

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

   559   by (simp add: UNIV_def)

   560

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

   562

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

   564   by simp

   565

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

   567   by (rule subsetI) (rule UNIV_I)

   568

   569 text {*

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

   571   causes them to be ignored because of their interaction with

   572   congruence rules.

   573 *}

   574

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

   576   by (simp add: Ball_def)

   577

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

   579   by (simp add: Bex_def)

   580

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

   582   by auto

   583

   584

   585 subsubsection {* The empty set *}

   586

   587 lemma empty_def:

   588   "{} = {x. False}"

   589   by (simp add: bot_fun_eq bot_bool_eq Collect_def)

   590

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

   592   by (simp add: empty_def)

   593

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

   595   by simp

   596

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

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

   599   by blast

   600

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

   602   by blast

   603

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

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

   606   by blast

   607

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

   609   by (simp add: Ball_def)

   610

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

   612   by (simp add: Bex_def)

   613

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

   615   by (blast elim: equalityE)

   616

   617

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

   619

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

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

   622

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

   624   by (simp add: Pow_def)

   625

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

   627   by (simp add: Pow_def)

   628

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

   630   by (simp add: Pow_def)

   631

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

   633   by simp

   634

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

   636   by simp

   637

   638

   639 subsubsection {* Set complement *}

   640

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

   642   by (simp add: mem_def fun_Compl_def bool_Compl_def)

   643

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

   645   by (unfold mem_def fun_Compl_def bool_Compl_def) blast

   646

   647 text {*

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

   649   Classical prover.  Negated assumptions behave like formulae on the

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

   651

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

   653   by (simp add: mem_def fun_Compl_def bool_Compl_def)

   654

   655 lemmas ComplE = ComplD [elim_format]

   656

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

   658

   659

   660 subsubsection {* Binary union -- Un *}

   661

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

   663   "op Un \<equiv> sup"

   664

   665 notation (xsymbols)

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

   667

   668 notation (HTML output)

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

   670

   671 lemma Un_def:

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

   673   by (simp add: sup_fun_eq sup_bool_eq Collect_def mem_def)

   674

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

   676   by (unfold Un_def) blast

   677

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

   679   by simp

   680

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

   682   by simp

   683

   684 text {*

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

   686   @{prop B}.

   687 *}

   688

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

   690   by auto

   691

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

   693   by (unfold Un_def) blast

   694

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

   696   by (simp add: Collect_def mem_def insert_compr Un_def)

   697

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

   699   by (fact mono_sup)

   700

   701

   702 subsubsection {* Binary intersection -- Int *}

   703

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

   705   "op Int \<equiv> inf"

   706

   707 notation (xsymbols)

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

   709

   710 notation (HTML output)

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

   712

   713 lemma Int_def:

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

   715   by (simp add: inf_fun_eq inf_bool_eq Collect_def mem_def)

   716

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

   718   by (unfold Int_def) blast

   719

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

   721   by simp

   722

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

   724   by simp

   725

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

   727   by simp

   728

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

   730   by simp

   731

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

   733   by (fact mono_inf)

   734

   735

   736 subsubsection {* Set difference *}

   737

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

   739   by (simp add: mem_def fun_diff_def bool_diff_def)

   740

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

   742   by simp

   743

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

   745   by simp

   746

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

   748   by simp

   749

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

   751   by simp

   752

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

   754

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

   756 by blast

   757

   758

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

   760

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

   762   by (unfold insert_def) blast

   763

   764 lemma insertI1: "a : insert a B"

   765   by simp

   766

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

   768   by simp

   769

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

   771   by (unfold insert_def) blast

   772

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

   774   -- {* Classical introduction rule. *}

   775   by auto

   776

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

   778   by auto

   779

   780 lemma set_insert:

   781   assumes "x \<in> A"

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

   783 proof

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

   785 next

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

   787 qed

   788

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

   790 by auto

   791

   792 subsubsection {* Singletons, using insert *}

   793

   794 lemma singletonI [intro!,noatp]: "a : {a}"

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

   796   by (rule insertI1)

   797

   798 lemma singletonD [dest!,noatp]: "b : {a} ==> b = a"

   799   by blast

   800

   801 lemmas singletonE = singletonD [elim_format]

   802

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

   804   by blast

   805

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

   807   by blast

   808

   809 lemma singleton_insert_inj_eq [iff,noatp]:

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

   811   by blast

   812

   813 lemma singleton_insert_inj_eq' [iff,noatp]:

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

   815   by blast

   816

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

   818   by fast

   819

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

   821   by blast

   822

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

   824   by blast

   825

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

   827   by blast

   828

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

   830   by (blast elim: equalityE)

   831

   832

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

   834

   835 text {*

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

   837 *}

   838

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

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

   841

   842 abbreviation

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

   844   "range f == f  UNIV"

   845

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

   847   by (unfold image_def) blast

   848

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

   850   by (rule image_eqI) (rule refl)

   851

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

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

   854     required @{term x}. *}

   855   by (unfold image_def) blast

   856

   857 lemma imageE [elim!]:

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

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

   860   by (unfold image_def) blast

   861

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

   863   by blast

   864

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

   866   by blast

   867

   868 lemma image_subset_iff: "(fA \<subseteq> B) = (\<forall>x\<in>A. f x \<in> B)"

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

   870   by blast

   871

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

   873   apply safe

   874    prefer 2 apply fast

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

   876   done

   877

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

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

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

   881   by blast

   882

   883 text {*

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

   885 *}

   886

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

   888   by simp

   889

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

   891   by simp

   892

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

   894   by blast

   895

   896

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

   898

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

   900

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

   902   by auto

   903

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

   905   by auto

   906

   907 text {*

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

   909   "split_if [split]"}.

   910 *}

   911

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

   913   by (rule split_if)

   914

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

   916   by (rule split_if)

   917

   918 text {*

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

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

   921 *}

   922

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

   924   by (rule split_if)

   925

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

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

   928

   929 lemmas split_ifs = if_bool_eq_conj split_if_eq1 split_if_eq2 split_if_mem1 split_if_mem2

   930

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

   932   outer-level constant, which in this case is just "op :"; we instead need

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

   934   apply, then the formula should be kept.

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

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

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

   938  *)

   939

   940

   941 subsection {* Further operations and lemmas *}

   942

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

   944

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

   946   by (unfold less_le) blast

   947

   948 lemma psubsetE [elim!,noatp]:

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

   950   by (unfold less_le) blast

   951

   952 lemma psubset_insert_iff:

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

   954   by (auto simp add: less_le subset_insert_iff)

   955

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

   957   by (simp only: less_le)

   958

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

   960   by (simp add: psubset_eq)

   961

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

   963 apply (unfold less_le)

   964 apply (auto dest: subset_antisym)

   965 done

   966

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

   968 apply (unfold less_le)

   969 apply (auto dest: subsetD)

   970 done

   971

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

   973   by (auto simp add: psubset_eq)

   974

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

   976   by (auto simp add: psubset_eq)

   977

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

   979   by (unfold less_le) blast

   980

   981 lemma atomize_ball:

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

   983   by (simp only: Ball_def atomize_all atomize_imp)

   984

   985 lemmas [symmetric, rulify] = atomize_ball

   986   and [symmetric, defn] = atomize_ball

   987

   988 subsubsection {* Derived rules involving subsets. *}

   989

   990 text {* @{text insert}. *}

   991

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

   993   by (rule subsetI) (erule insertI2)

   994

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

   996   by blast

   997

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

   999   by blast

  1000

  1001

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

  1003

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

  1005   by blast

  1006

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

  1008   by blast

  1009

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

  1011   by blast

  1012

  1013

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

  1015

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

  1017   by blast

  1018

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

  1020   by blast

  1021

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

  1023   by blast

  1024

  1025

  1026 text {* \medskip Set difference. *}

  1027

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

  1029   by blast

  1030

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

  1032 by blast

  1033

  1034

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

  1036

  1037 text {* @{text "{}"}. *}

  1038

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

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

  1041   by auto

  1042

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

  1044   by blast

  1045

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

  1047   by (unfold less_le) blast

  1048

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

  1050 by blast

  1051

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

  1053 by blast

  1054

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

  1056   by blast

  1057

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

  1059   by blast

  1060

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

  1062   by blast

  1063

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

  1065   by blast

  1066

  1067

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

  1069

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

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

  1072   by blast

  1073

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

  1075   by blast

  1076

  1077 lemmas empty_not_insert = insert_not_empty [symmetric, standard]

  1078 declare empty_not_insert [simp]

  1079

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

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

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

  1083   by blast

  1084

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

  1086   by blast

  1087

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

  1089   by blast

  1090

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

  1092   by blast

  1093

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

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

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

  1097   done

  1098

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

  1100   by auto

  1101

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

  1103   by blast

  1104

  1105 lemma insert_disjoint [simp,noatp]:

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

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

  1108   by auto

  1109

  1110 lemma disjoint_insert [simp,noatp]:

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

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

  1113   by auto

  1114

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

  1116

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

  1118   by blast

  1119

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

  1121   by blast

  1122

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

  1124   by auto

  1125

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

  1127 by auto

  1128

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

  1130 by blast

  1131

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

  1133 by blast

  1134

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

  1136 by blast

  1137

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

  1139 by blast

  1140

  1141

  1142 lemma image_Collect [noatp]: "f  {x. P x} = {f x | x. P x}"

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

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

  1145       equational properties than does the RHS. *}

  1146   by blast

  1147

  1148 lemma if_image_distrib [simp]:

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

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

  1151   by (auto simp add: image_def)

  1152

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

  1154   by (simp add: image_def)

  1155

  1156

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

  1158

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

  1160   by auto

  1161

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

  1163 by (subst image_image, simp)

  1164

  1165

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

  1167

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

  1169   by blast

  1170

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

  1172   by blast

  1173

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

  1175   by blast

  1176

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

  1178   by blast

  1179

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

  1181   by blast

  1182

  1183 lemmas Int_ac = Int_assoc Int_left_absorb Int_commute Int_left_commute

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

  1185

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

  1187   by blast

  1188

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

  1190   by blast

  1191

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

  1193   by blast

  1194

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

  1196   by blast

  1197

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

  1199   by blast

  1200

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

  1202   by blast

  1203

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

  1205   by blast

  1206

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

  1208   by blast

  1209

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

  1211   by blast

  1212

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

  1214   by blast

  1215

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

  1217   by blast

  1218

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

  1220   by blast

  1221

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

  1223   by blast

  1224

  1225

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

  1227

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

  1229   by blast

  1230

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

  1232   by blast

  1233

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

  1235   by blast

  1236

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

  1238   by blast

  1239

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

  1241   by blast

  1242

  1243 lemmas Un_ac = Un_assoc Un_left_absorb Un_commute Un_left_commute

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

  1245

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

  1247   by blast

  1248

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

  1250   by blast

  1251

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

  1253   by blast

  1254

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

  1256   by blast

  1257

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

  1259   by blast

  1260

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

  1262   by blast

  1263

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

  1265   by blast

  1266

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

  1268   by blast

  1269

  1270 lemma Int_insert_left:

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

  1272   by auto

  1273

  1274 lemma Int_insert_left_if0[simp]:

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

  1276   by auto

  1277

  1278 lemma Int_insert_left_if1[simp]:

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

  1280   by auto

  1281

  1282 lemma Int_insert_right:

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

  1284   by auto

  1285

  1286 lemma Int_insert_right_if0[simp]:

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

  1288   by auto

  1289

  1290 lemma Int_insert_right_if1[simp]:

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

  1292   by auto

  1293

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

  1295   by blast

  1296

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

  1298   by blast

  1299

  1300 lemma Un_Int_crazy:

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

  1302   by blast

  1303

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

  1305   by blast

  1306

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

  1308   by blast

  1309

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

  1311   by blast

  1312

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

  1314   by blast

  1315

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

  1317   by blast

  1318

  1319

  1320 text {* \medskip Set complement *}

  1321

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

  1323   by blast

  1324

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

  1326   by blast

  1327

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

  1329   by blast

  1330

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

  1332   by blast

  1333

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

  1335   by blast

  1336

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

  1338   by blast

  1339

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

  1341   by blast

  1342

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

  1344   by blast

  1345

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

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

  1348   by blast

  1349

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

  1351   by blast

  1352

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

  1354   by blast

  1355

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

  1357   by blast

  1358

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

  1360   by blast

  1361

  1362 text {* \medskip Bounded quantifiers.

  1363

  1364   The following are not added to the default simpset because

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

  1366

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

  1368   by blast

  1369

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

  1371   by blast

  1372

  1373

  1374 text {* \medskip Set difference. *}

  1375

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

  1377   by blast

  1378

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

  1380   by blast

  1381

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

  1383   by blast

  1384

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

  1386 by blast

  1387

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

  1389   by (blast elim: equalityE)

  1390

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

  1392   by blast

  1393

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

  1395   by blast

  1396

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

  1398   by blast

  1399

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

  1401   by blast

  1402

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

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

  1405   by blast

  1406

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

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

  1409   by blast

  1410

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

  1412   by auto

  1413

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

  1415   by blast

  1416

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

  1418 by blast

  1419

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

  1421   by blast

  1422

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

  1424   by auto

  1425

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

  1427   by blast

  1428

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

  1430   by blast

  1431

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

  1433   by blast

  1434

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

  1436   by blast

  1437

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

  1439   by blast

  1440

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

  1442   by blast

  1443

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

  1445   by blast

  1446

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

  1448   by blast

  1449

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

  1451   by blast

  1452

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

  1454   by blast

  1455

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

  1457   by blast

  1458

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

  1460   by auto

  1461

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

  1463   by blast

  1464

  1465

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

  1467

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

  1469   by (cases x) auto

  1470

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

  1472   by (auto intro: bool_induct)

  1473

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

  1475   by (cases x) auto

  1476

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

  1478   by (auto intro: bool_contrapos)

  1479

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

  1481

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

  1483   by (auto simp add: Pow_def)

  1484

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

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

  1487

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

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

  1490

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

  1492   by blast

  1493

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

  1495   by blast

  1496

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

  1498   by blast

  1499

  1500

  1501 text {* \medskip Miscellany. *}

  1502

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

  1504   by blast

  1505

  1506 lemma subset_iff: "(A \<subseteq> B) = (\<forall>t. t \<in> A --> t \<in> B)"

  1507   by blast

  1508

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

  1510   by (unfold less_le) blast

  1511

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

  1513   by blast

  1514

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

  1516   by blast

  1517

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

  1519   by iprover

  1520

  1521

  1522 subsubsection {* Monotonicity of various operations *}

  1523

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

  1525   by blast

  1526

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

  1528   by blast

  1529

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

  1531   by blast

  1532

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

  1534   by blast

  1535

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

  1537   by blast

  1538

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

  1540   by blast

  1541

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

  1543   by blast

  1544

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

  1546

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

  1548   apply (rule impI)

  1549   apply (erule subsetD, assumption)

  1550   done

  1551

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

  1553   by iprover

  1554

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

  1556   by iprover

  1557

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

  1559   by iprover

  1560

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

  1562

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

  1564   by iprover

  1565

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

  1567   by iprover

  1568

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

  1570   by iprover

  1571

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

  1573   by blast

  1574

  1575 lemma Int_Collect_mono:

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

  1577   by blast

  1578

  1579 lemmas basic_monos =

  1580   subset_refl imp_refl disj_mono conj_mono

  1581   ex_mono Collect_mono in_mono

  1582

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

  1584   by iprover

  1585

  1586

  1587 subsubsection {* Inverse image of a function *}

  1588

  1589 constdefs

  1590   vimage :: "('a => 'b) => 'b set => 'a set"    (infixr "-" 90)

  1591   [code del]: "f - B == {x. f x : B}"

  1592

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

  1594   by (unfold vimage_def) blast

  1595

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

  1597   by simp

  1598

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

  1600   by (unfold vimage_def) blast

  1601

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

  1603   by (unfold vimage_def) fast

  1604

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

  1606   by (unfold vimage_def) blast

  1607

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

  1609   by (unfold vimage_def) fast

  1610

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

  1612   by blast

  1613

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

  1615   by blast

  1616

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

  1618   by blast

  1619

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

  1621   by fast

  1622

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

  1624   by blast

  1625

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

  1627   by blast

  1628

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

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

  1631   by blast

  1632

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

  1634   by blast

  1635

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

  1637   by blast

  1638

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

  1640   -- {* monotonicity *}

  1641   by blast

  1642

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

  1644 by (blast intro: sym)

  1645

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

  1647 by blast

  1648

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

  1650 by blast

  1651

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

  1653   by auto

  1654

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

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

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

  1658   by (auto simp add: vimage_def)

  1659

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

  1661 by blast

  1662

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

  1664 by blast

  1665

  1666

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

  1668

  1669 definition contents :: "'a set \<Rightarrow> 'a" where

  1670   [code del]: "contents X = (THE x. X = {x})"

  1671

  1672 lemma contents_eq [simp]: "contents {x} = x"

  1673   by (simp add: contents_def)

  1674

  1675

  1676 subsubsection {* Least value operator *}

  1677

  1678 lemma Least_mono:

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

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

  1681     -- {* Courtesy of Stephan Merz *}

  1682   apply clarify

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

  1684   apply (rule LeastI2_order)

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

  1686   done

  1687

  1688 subsection {* Misc *}

  1689

  1690 text {* Rudimentary code generation *}

  1691

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

  1693   by (auto simp add: insert_compr Collect_def mem_def)

  1694

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

  1696   by (simp add: vimage_def Collect_def mem_def)

  1697

  1698

  1699 text {* Misc theorem and ML bindings *}

  1700

  1701 lemmas equalityI = subset_antisym

  1702

  1703 ML {*

  1704 val Ball_def = @{thm Ball_def}

  1705 val Bex_def = @{thm Bex_def}

  1706 val CollectD = @{thm CollectD}

  1707 val CollectE = @{thm CollectE}

  1708 val CollectI = @{thm CollectI}

  1709 val Collect_conj_eq = @{thm Collect_conj_eq}

  1710 val Collect_mem_eq = @{thm Collect_mem_eq}

  1711 val IntD1 = @{thm IntD1}

  1712 val IntD2 = @{thm IntD2}

  1713 val IntE = @{thm IntE}

  1714 val IntI = @{thm IntI}

  1715 val Int_Collect = @{thm Int_Collect}

  1716 val UNIV_I = @{thm UNIV_I}

  1717 val UNIV_witness = @{thm UNIV_witness}

  1718 val UnE = @{thm UnE}

  1719 val UnI1 = @{thm UnI1}

  1720 val UnI2 = @{thm UnI2}

  1721 val ballE = @{thm ballE}

  1722 val ballI = @{thm ballI}

  1723 val bexCI = @{thm bexCI}

  1724 val bexE = @{thm bexE}

  1725 val bexI = @{thm bexI}

  1726 val bex_triv = @{thm bex_triv}

  1727 val bspec = @{thm bspec}

  1728 val contra_subsetD = @{thm contra_subsetD}

  1729 val distinct_lemma = @{thm distinct_lemma}

  1730 val eq_to_mono = @{thm eq_to_mono}

  1731 val equalityCE = @{thm equalityCE}

  1732 val equalityD1 = @{thm equalityD1}

  1733 val equalityD2 = @{thm equalityD2}

  1734 val equalityE = @{thm equalityE}

  1735 val equalityI = @{thm equalityI}

  1736 val imageE = @{thm imageE}

  1737 val imageI = @{thm imageI}

  1738 val image_Un = @{thm image_Un}

  1739 val image_insert = @{thm image_insert}

  1740 val insert_commute = @{thm insert_commute}

  1741 val insert_iff = @{thm insert_iff}

  1742 val mem_Collect_eq = @{thm mem_Collect_eq}

  1743 val rangeE = @{thm rangeE}

  1744 val rangeI = @{thm rangeI}

  1745 val range_eqI = @{thm range_eqI}

  1746 val subsetCE = @{thm subsetCE}

  1747 val subsetD = @{thm subsetD}

  1748 val subsetI = @{thm subsetI}

  1749 val subset_refl = @{thm subset_refl}

  1750 val subset_trans = @{thm subset_trans}

  1751 val vimageD = @{thm vimageD}

  1752 val vimageE = @{thm vimageE}

  1753 val vimageI = @{thm vimageI}

  1754 val vimageI2 = @{thm vimageI2}

  1755 val vimage_Collect = @{thm vimage_Collect}

  1756 val vimage_Int = @{thm vimage_Int}

  1757 val vimage_Un = @{thm vimage_Un}

  1758 *}

  1759

  1760 end
`