src/HOL/Set.thy
 author hoelzl Fri Feb 19 13:40:50 2016 +0100 (2016-02-19) changeset 62378 85ed00c1fe7c parent 62087 44841d07ef1d child 62390 842917225d56 permissions -rw-r--r--
generalize more theorems to support enat and ennreal
1 (*  Author:     Tobias Nipkow, Lawrence C Paulson and Markus Wenzel *)
3 section \<open>Set theory for higher-order logic\<close>
5 theory Set
6 imports Lattices
7 begin
9 subsection \<open>Sets as predicates\<close>
11 typedecl 'a set
13 axiomatization Collect :: "('a \<Rightarrow> bool) \<Rightarrow> 'a set" \<comment> "comprehension"
14   and member :: "'a \<Rightarrow> 'a set \<Rightarrow> bool" \<comment> "membership"
15 where mem_Collect_eq [iff, code_unfold]: "member a (Collect P) = P a"
16   and Collect_mem_eq [simp]: "Collect (\<lambda>x. member x A) = A"
18 notation
19   member  ("op \<in>") and
20   member  ("(_/ \<in> _)" [51, 51] 50)
22 abbreviation not_member
23   where "not_member x A \<equiv> \<not> (x \<in> A)" \<comment> "non-membership"
24 notation
25   not_member  ("op \<notin>") and
26   not_member  ("(_/ \<notin> _)" [51, 51] 50)
28 notation (ASCII)
29   member  ("op :") and
30   member  ("(_/ : _)" [51, 51] 50) and
31   not_member  ("op ~:") and
32   not_member  ("(_/ ~: _)" [51, 51] 50)
35 text \<open>Set comprehensions\<close>
37 syntax
38   "_Coll" :: "pttrn => bool => 'a set"    ("(1{_./ _})")
39 translations
40   "{x. P}" \<rightleftharpoons> "CONST Collect (\<lambda>x. P)"
42 syntax (ASCII)
43   "_Collect" :: "pttrn \<Rightarrow> 'a set \<Rightarrow> bool \<Rightarrow> 'a set"  ("(1{_ :/ _./ _})")
44 syntax
45   "_Collect" :: "pttrn \<Rightarrow> 'a set \<Rightarrow> bool \<Rightarrow> 'a set"  ("(1{_ \<in>/ _./ _})")
46 translations
47   "{p:A. P}" \<rightharpoonup> "CONST Collect (\<lambda>p. p \<in> A \<and> P)"
49 lemma CollectI: "P a \<Longrightarrow> a \<in> {x. P x}"
50   by simp
52 lemma CollectD: "a \<in> {x. P x} \<Longrightarrow> P a"
53   by simp
55 lemma Collect_cong: "(\<And>x. P x = Q x) ==> {x. P x} = {x. Q x}"
56   by simp
58 text \<open>
59 Simproc for pulling \<open>x=t\<close> in \<open>{x. \<dots> & x=t & \<dots>}\<close>
60 to the front (and similarly for \<open>t=x\<close>):
61 \<close>
63 simproc_setup defined_Collect ("{x. P x & Q x}") = \<open>
64   fn _ => Quantifier1.rearrange_Collect
65     (fn ctxt =>
66       resolve_tac ctxt @{thms Collect_cong} 1 THEN
67       resolve_tac ctxt @{thms iffI} 1 THEN
68       ALLGOALS
69         (EVERY' [REPEAT_DETERM o eresolve_tac ctxt @{thms conjE},
70           DEPTH_SOLVE_1 o (assume_tac ctxt ORELSE' resolve_tac ctxt @{thms conjI})]))
71 \<close>
73 lemmas CollectE = CollectD [elim_format]
75 lemma set_eqI:
76   assumes "\<And>x. x \<in> A \<longleftrightarrow> x \<in> B"
77   shows "A = B"
78 proof -
79   from assms have "{x. x \<in> A} = {x. x \<in> B}" by simp
80   then show ?thesis by simp
81 qed
83 lemma set_eq_iff:
84   "A = B \<longleftrightarrow> (\<forall>x. x \<in> A \<longleftrightarrow> x \<in> B)"
85   by (auto intro:set_eqI)
87 text \<open>Lifting of predicate class instances\<close>
89 instantiation set :: (type) boolean_algebra
90 begin
92 definition less_eq_set where
93   "A \<le> B \<longleftrightarrow> (\<lambda>x. member x A) \<le> (\<lambda>x. member x B)"
95 definition less_set where
96   "A < B \<longleftrightarrow> (\<lambda>x. member x A) < (\<lambda>x. member x B)"
98 definition inf_set where
99   "A \<sqinter> B = Collect ((\<lambda>x. member x A) \<sqinter> (\<lambda>x. member x B))"
101 definition sup_set where
102   "A \<squnion> B = Collect ((\<lambda>x. member x A) \<squnion> (\<lambda>x. member x B))"
104 definition bot_set where
105   "\<bottom> = Collect \<bottom>"
107 definition top_set where
108   "\<top> = Collect \<top>"
110 definition uminus_set where
111   "- A = Collect (- (\<lambda>x. member x A))"
113 definition minus_set where
114   "A - B = Collect ((\<lambda>x. member x A) - (\<lambda>x. member x B))"
116 instance proof
117 qed (simp_all add: less_eq_set_def less_set_def inf_set_def sup_set_def
118   bot_set_def top_set_def uminus_set_def minus_set_def
119   less_le_not_le inf_compl_bot sup_compl_top sup_inf_distrib1 diff_eq
120   set_eqI fun_eq_iff
121   del: inf_apply sup_apply bot_apply top_apply minus_apply uminus_apply)
123 end
125 text \<open>Set enumerations\<close>
127 abbreviation empty :: "'a set" ("{}") where
128   "{} \<equiv> bot"
130 definition insert :: "'a \<Rightarrow> 'a set \<Rightarrow> 'a set" where
131   insert_compr: "insert a B = {x. x = a \<or> x \<in> B}"
133 syntax
134   "_Finset" :: "args => 'a set"    ("{(_)}")
135 translations
136   "{x, xs}" == "CONST insert x {xs}"
137   "{x}" == "CONST insert x {}"
140 subsection \<open>Subsets and bounded quantifiers\<close>
142 abbreviation subset :: "'a set \<Rightarrow> 'a set \<Rightarrow> bool"
143   where "subset \<equiv> less"
145 abbreviation subset_eq :: "'a set \<Rightarrow> 'a set \<Rightarrow> bool"
146   where "subset_eq \<equiv> less_eq"
148 notation
149   subset  ("op \<subset>") and
150   subset  ("(_/ \<subset> _)" [51, 51] 50) and
151   subset_eq  ("op \<subseteq>") and
152   subset_eq  ("(_/ \<subseteq> _)" [51, 51] 50)
154 abbreviation (input)
155   supset :: "'a set \<Rightarrow> 'a set \<Rightarrow> bool" where
156   "supset \<equiv> greater"
158 abbreviation (input)
159   supset_eq :: "'a set \<Rightarrow> 'a set \<Rightarrow> bool" where
160   "supset_eq \<equiv> greater_eq"
162 notation
163   supset  ("op \<supset>") and
164   supset  ("(_/ \<supset> _)" [51, 51] 50) and
165   supset_eq  ("op \<supseteq>") and
166   supset_eq  ("(_/ \<supseteq> _)" [51, 51] 50)
168 notation (ASCII output)
169   subset  ("op <") and
170   subset  ("(_/ < _)" [51, 51] 50) and
171   subset_eq  ("op <=") and
172   subset_eq  ("(_/ <= _)" [51, 51] 50)
174 definition Ball :: "'a set \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> bool" where
175   "Ball A P \<longleftrightarrow> (\<forall>x. x \<in> A \<longrightarrow> P x)"   \<comment> "bounded universal quantifiers"
177 definition Bex :: "'a set \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> bool" where
178   "Bex A P \<longleftrightarrow> (\<exists>x. x \<in> A \<and> P x)"   \<comment> "bounded existential quantifiers"
180 syntax (ASCII)
181   "_Ball"       :: "pttrn => 'a set => bool => bool"      ("(3ALL _:_./ _)" [0, 0, 10] 10)
182   "_Bex"        :: "pttrn => 'a set => bool => bool"      ("(3EX _:_./ _)" [0, 0, 10] 10)
183   "_Bex1"       :: "pttrn => 'a set => bool => bool"      ("(3EX! _:_./ _)" [0, 0, 10] 10)
184   "_Bleast"     :: "id => 'a set => bool => 'a"           ("(3LEAST _:_./ _)" [0, 0, 10] 10)
186 syntax (HOL)
187   "_Ball"       :: "pttrn => 'a set => bool => bool"      ("(3! _:_./ _)" [0, 0, 10] 10)
188   "_Bex"        :: "pttrn => 'a set => bool => bool"      ("(3? _:_./ _)" [0, 0, 10] 10)
189   "_Bex1"       :: "pttrn => 'a set => bool => bool"      ("(3?! _:_./ _)" [0, 0, 10] 10)
191 syntax
192   "_Ball"       :: "pttrn => 'a set => bool => bool"      ("(3\<forall>_\<in>_./ _)" [0, 0, 10] 10)
193   "_Bex"        :: "pttrn => 'a set => bool => bool"      ("(3\<exists>_\<in>_./ _)" [0, 0, 10] 10)
194   "_Bex1"       :: "pttrn => 'a set => bool => bool"      ("(3\<exists>!_\<in>_./ _)" [0, 0, 10] 10)
195   "_Bleast"     :: "id => 'a set => bool => 'a"           ("(3LEAST_\<in>_./ _)" [0, 0, 10] 10)
197 translations
198   "\<forall>x\<in>A. P" \<rightleftharpoons> "CONST Ball A (\<lambda>x. P)"
199   "\<exists>x\<in>A. P" \<rightleftharpoons> "CONST Bex A (\<lambda>x. P)"
200   "\<exists>!x\<in>A. P" \<rightharpoonup> "\<exists>!x. x \<in> A \<and> P"
201   "LEAST x:A. P" \<rightharpoonup> "LEAST x. x \<in> A \<and> P"
203 syntax (ASCII output)
204   "_setlessAll" :: "[idt, 'a, bool] => bool"  ("(3ALL _<_./ _)"  [0, 0, 10] 10)
205   "_setlessEx"  :: "[idt, 'a, bool] => bool"  ("(3EX _<_./ _)"  [0, 0, 10] 10)
206   "_setleAll"   :: "[idt, 'a, bool] => bool"  ("(3ALL _<=_./ _)" [0, 0, 10] 10)
207   "_setleEx"    :: "[idt, 'a, bool] => bool"  ("(3EX _<=_./ _)" [0, 0, 10] 10)
208   "_setleEx1"   :: "[idt, 'a, bool] => bool"  ("(3EX! _<=_./ _)" [0, 0, 10] 10)
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)
217 syntax
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)
224 translations
225  "\<forall>A\<subset>B. P"   \<rightharpoonup>  "\<forall>A. A \<subset> B \<longrightarrow> P"
226  "\<exists>A\<subset>B. P"   \<rightharpoonup>  "\<exists>A. A \<subset> B \<and> P"
227  "\<forall>A\<subseteq>B. P"   \<rightharpoonup>  "\<forall>A. A \<subseteq> B \<longrightarrow> P"
228  "\<exists>A\<subseteq>B. P"   \<rightharpoonup>  "\<exists>A. A \<subseteq> B \<and> P"
229  "\<exists>!A\<subseteq>B. P"  \<rightharpoonup>  "\<exists>!A. A \<subseteq> B \<and> P"
231 print_translation \<open>
232   let
233     val All_binder = Mixfix.binder_name @{const_syntax All};
234     val Ex_binder = Mixfix.binder_name @{const_syntax Ex};
235     val impl = @{const_syntax HOL.implies};
236     val conj = @{const_syntax HOL.conj};
237     val sbset = @{const_syntax subset};
238     val sbset_eq = @{const_syntax subset_eq};
240     val trans =
241      [((All_binder, impl, sbset), @{syntax_const "_setlessAll"}),
242       ((All_binder, impl, sbset_eq), @{syntax_const "_setleAll"}),
243       ((Ex_binder, conj, sbset), @{syntax_const "_setlessEx"}),
244       ((Ex_binder, conj, sbset_eq), @{syntax_const "_setleEx"})];
246     fun mk v (v', T) c n P =
247       if v = v' andalso not (Term.exists_subterm (fn Free (x, _) => x = v | _ => false) n)
248       then Syntax.const c $Syntax_Trans.mark_bound_body (v', T)$ n $P 249 else raise Match; 251 fun tr' q = (q, fn _ => 252 (fn [Const (@{syntax_const "_bound"}, _)$ Free (v, Type (@{type_name set}, _)),
253           Const (c, _) $254 (Const (d, _)$ (Const (@{syntax_const "_bound"}, _) $Free (v', T))$ n) $P] => 255 (case AList.lookup (op =) trans (q, c, d) of 256 NONE => raise Match 257 | SOME l => mk v (v', T) l n P) 258 | _ => raise Match)); 259 in 260 [tr' All_binder, tr' Ex_binder] 261 end 262 \<close> 265 text \<open> 266 \medskip Translate between \<open>{e | x1...xn. P}\<close> and \<open>{u. EX x1..xn. u = e & P}\<close>; \<open>{y. EX x1..xn. y = e & P}\<close> is 267 only translated if \<open>[0..n] subset bvs(e)\<close>. 268 \<close> 270 syntax 271 "_Setcompr" :: "'a => idts => bool => 'a set" ("(1{_ |/_./ _})") 273 parse_translation \<open> 274 let 275 val ex_tr = snd (Syntax_Trans.mk_binder_tr ("EX ", @{const_syntax Ex})); 277 fun nvars (Const (@{syntax_const "_idts"}, _)$ _ $idts) = nvars idts + 1 278 | nvars _ = 1; 280 fun setcompr_tr ctxt [e, idts, b] = 281 let 282 val eq = Syntax.const @{const_syntax HOL.eq}$ Bound (nvars idts) $e; 283 val P = Syntax.const @{const_syntax HOL.conj}$ eq $b; 284 val exP = ex_tr ctxt [idts, P]; 285 in Syntax.const @{const_syntax Collect}$ absdummy dummyT exP end;
287   in [(@{syntax_const "_Setcompr"}, setcompr_tr)] end;
288 \<close>
290 print_translation \<open>
291  [Syntax_Trans.preserve_binder_abs2_tr' @{const_syntax Ball} @{syntax_const "_Ball"},
292   Syntax_Trans.preserve_binder_abs2_tr' @{const_syntax Bex} @{syntax_const "_Bex"}]
293 \<close> \<comment> \<open>to avoid eta-contraction of body\<close>
295 print_translation \<open>
296 let
297   val ex_tr' = snd (Syntax_Trans.mk_binder_tr' (@{const_syntax Ex}, "DUMMY"));
299   fun setcompr_tr' ctxt [Abs (abs as (_, _, P))] =
300     let
301       fun check (Const (@{const_syntax Ex}, _) $Abs (_, _, P), n) = check (P, n + 1) 302 | check (Const (@{const_syntax HOL.conj}, _)$
303               (Const (@{const_syntax HOL.eq}, _) $Bound m$ e) $P, n) = 304 n > 0 andalso m = n andalso not (loose_bvar1 (P, n)) andalso 305 subset (op =) (0 upto (n - 1), add_loose_bnos (e, 0, [])) 306 | check _ = false; 308 fun tr' (_$ abs) =
309           let val _ $idts$ (_ $(_$ _ $e)$ Q) = ex_tr' ctxt [abs]
310           in Syntax.const @{syntax_const "_Setcompr"} $e$ idts $Q end; 311 in 312 if check (P, 0) then tr' P 313 else 314 let 315 val (x as _$ Free(xN, _), t) = Syntax_Trans.atomic_abs_tr' abs;
316           val M = Syntax.const @{syntax_const "_Coll"} $x$ t;
317         in
318           case t of
319             Const (@{const_syntax HOL.conj}, _) $320 (Const (@{const_syntax Set.member}, _)$
321                 (Const (@{syntax_const "_bound"}, _) $Free (yN, _))$ A) $P => 322 if xN = yN then Syntax.const @{syntax_const "_Collect"}$ x $A$ P else M
323           | _ => M
324         end
325     end;
326   in [(@{const_syntax Collect}, setcompr_tr')] end;
327 \<close>
329 simproc_setup defined_Bex ("EX x:A. P x & Q x") = \<open>
330   fn _ => Quantifier1.rearrange_bex
331     (fn ctxt =>
332       unfold_tac ctxt @{thms Bex_def} THEN
333       Quantifier1.prove_one_point_ex_tac ctxt)
334 \<close>
336 simproc_setup defined_All ("ALL x:A. P x --> Q x") = \<open>
337   fn _ => Quantifier1.rearrange_ball
338     (fn ctxt =>
339       unfold_tac ctxt @{thms Ball_def} THEN
340       Quantifier1.prove_one_point_all_tac ctxt)
341 \<close>
343 lemma ballI [intro!]: "(!!x. x:A ==> P x) ==> ALL x:A. P x"
346 lemmas strip = impI allI ballI
348 lemma bspec [dest?]: "ALL x:A. P x ==> x:A ==> P x"
351 text \<open>
352   Gives better instantiation for bound:
353 \<close>
355 setup \<open>
356   map_theory_claset (fn ctxt =>
357     ctxt addbefore ("bspec", fn ctxt' => dresolve_tac ctxt' @{thms bspec} THEN' assume_tac ctxt'))
358 \<close>
360 ML \<open>
361 structure Simpdata =
362 struct
364 open Simpdata;
366 val mksimps_pairs = [(@{const_name Ball}, @{thms bspec})] @ mksimps_pairs;
368 end;
370 open Simpdata;
371 \<close>
373 declaration \<open>fn _ =>
374   Simplifier.map_ss (Simplifier.set_mksimps (mksimps mksimps_pairs))
375 \<close>
377 lemma ballE [elim]: "ALL x:A. P x ==> (P x ==> Q) ==> (x ~: A ==> Q) ==> Q"
378   by (unfold Ball_def) blast
380 lemma bexI [intro]: "P x ==> x:A ==> EX x:A. P x"
381   \<comment> \<open>Normally the best argument order: @{prop "P x"} constrains the
382     choice of @{prop "x:A"}.\<close>
383   by (unfold Bex_def) blast
385 lemma rev_bexI [intro?]: "x:A ==> P x ==> EX x:A. P x"
386   \<comment> \<open>The best argument order when there is only one @{prop "x:A"}.\<close>
387   by (unfold Bex_def) blast
389 lemma bexCI: "(ALL x:A. ~P x ==> P a) ==> a:A ==> EX x:A. P x"
390   by (unfold Bex_def) blast
392 lemma bexE [elim!]: "EX x:A. P x ==> (!!x. x:A ==> P x ==> Q) ==> Q"
393   by (unfold Bex_def) blast
395 lemma ball_triv [simp]: "(ALL x:A. P) = ((EX x. x:A) --> P)"
396   \<comment> \<open>Trival rewrite rule.\<close>
399 lemma bex_triv [simp]: "(EX x:A. P) = ((EX x. x:A) & P)"
400   \<comment> \<open>Dual form for existentials.\<close>
403 lemma bex_triv_one_point1 [simp]: "(EX x:A. x = a) = (a:A)"
404   by blast
406 lemma bex_triv_one_point2 [simp]: "(EX x:A. a = x) = (a:A)"
407   by blast
409 lemma bex_one_point1 [simp]: "(EX x:A. x = a & P x) = (a:A & P a)"
410   by blast
412 lemma bex_one_point2 [simp]: "(EX x:A. a = x & P x) = (a:A & P a)"
413   by blast
415 lemma ball_one_point1 [simp]: "(ALL x:A. x = a --> P x) = (a:A --> P a)"
416   by blast
418 lemma ball_one_point2 [simp]: "(ALL x:A. a = x --> P x) = (a:A --> P a)"
419   by blast
421 lemma ball_conj_distrib:
422   "(\<forall>x\<in>A. P x \<and> Q x) \<longleftrightarrow> ((\<forall>x\<in>A. P x) \<and> (\<forall>x\<in>A. Q x))"
423   by blast
425 lemma bex_disj_distrib:
426   "(\<exists>x\<in>A. P x \<or> Q x) \<longleftrightarrow> ((\<exists>x\<in>A. P x) \<or> (\<exists>x\<in>A. Q x))"
427   by blast
430 text \<open>Congruence rules\<close>
432 lemma ball_cong:
433   "A = B ==> (!!x. x:B ==> P x = Q x) ==>
434     (ALL x:A. P x) = (ALL x:B. Q x)"
437 lemma strong_ball_cong [cong]:
438   "A = B ==> (!!x. x:B =simp=> P x = Q x) ==>
439     (ALL x:A. P x) = (ALL x:B. Q x)"
440   by (simp add: simp_implies_def Ball_def)
442 lemma bex_cong:
443   "A = B ==> (!!x. x:B ==> P x = Q x) ==>
444     (EX x:A. P x) = (EX x:B. Q x)"
445   by (simp add: Bex_def cong: conj_cong)
447 lemma strong_bex_cong [cong]:
448   "A = B ==> (!!x. x:B =simp=> P x = Q x) ==>
449     (EX x:A. P x) = (EX x:B. Q x)"
450   by (simp add: simp_implies_def Bex_def cong: conj_cong)
452 lemma bex1_def: "(\<exists>!x\<in>X. P x) \<longleftrightarrow> (\<exists>x\<in>X. P x) \<and> (\<forall>x\<in>X. \<forall>y\<in>X. P x \<longrightarrow> P y \<longrightarrow> x = y)"
453   by auto
455 subsection \<open>Basic operations\<close>
457 subsubsection \<open>Subsets\<close>
459 lemma subsetI [intro!]: "(\<And>x. x \<in> A \<Longrightarrow> x \<in> B) \<Longrightarrow> A \<subseteq> B"
460   by (simp add: less_eq_set_def le_fun_def)
462 text \<open>
463   \medskip Map the type \<open>'a set => anything\<close> to just @{typ
465   "'a set"}.
466 \<close>
468 lemma subsetD [elim, intro?]: "A \<subseteq> B ==> c \<in> A ==> c \<in> B"
469   by (simp add: less_eq_set_def le_fun_def)
470   \<comment> \<open>Rule in Modus Ponens style.\<close>
472 lemma rev_subsetD [intro?]: "c \<in> A ==> A \<subseteq> B ==> c \<in> B"
473   \<comment> \<open>The same, with reversed premises for use with \<open>erule\<close> --
474       cf \<open>rev_mp\<close>.\<close>
475   by (rule subsetD)
477 text \<open>
478   \medskip Converts @{prop "A \<subseteq> B"} to @{prop "x \<in> A ==> x \<in> B"}.
479 \<close>
481 lemma subsetCE [elim]: "A \<subseteq> B ==> (c \<notin> A ==> P) ==> (c \<in> B ==> P) ==> P"
482   \<comment> \<open>Classical elimination rule.\<close>
483   by (auto simp add: less_eq_set_def le_fun_def)
485 lemma subset_eq: "A \<le> B = (\<forall>x\<in>A. x \<in> B)" by blast
487 lemma contra_subsetD: "A \<subseteq> B ==> c \<notin> B ==> c \<notin> A"
488   by blast
490 lemma subset_refl: "A \<subseteq> A"
491   by (fact order_refl) (* already [iff] *)
493 lemma subset_trans: "A \<subseteq> B ==> B \<subseteq> C ==> A \<subseteq> C"
494   by (fact order_trans)
496 lemma set_rev_mp: "x:A ==> A \<subseteq> B ==> x:B"
497   by (rule subsetD)
499 lemma set_mp: "A \<subseteq> B ==> x:A ==> x:B"
500   by (rule subsetD)
502 lemma subset_not_subset_eq [code]:
503   "A \<subset> B \<longleftrightarrow> A \<subseteq> B \<and> \<not> B \<subseteq> A"
504   by (fact less_le_not_le)
506 lemma eq_mem_trans: "a=b ==> b \<in> A ==> a \<in> A"
507   by simp
509 lemmas basic_trans_rules [trans] =
510   order_trans_rules set_rev_mp set_mp eq_mem_trans
513 subsubsection \<open>Equality\<close>
515 lemma subset_antisym [intro!]: "A \<subseteq> B ==> B \<subseteq> A ==> A = B"
516   \<comment> \<open>Anti-symmetry of the subset relation.\<close>
517   by (iprover intro: set_eqI subsetD)
519 text \<open>
520   \medskip Equality rules from ZF set theory -- are they appropriate
521   here?
522 \<close>
524 lemma equalityD1: "A = B ==> A \<subseteq> B"
525   by simp
527 lemma equalityD2: "A = B ==> B \<subseteq> A"
528   by simp
530 text \<open>
531   \medskip Be careful when adding this to the claset as \<open>subset_empty\<close> is in the simpset: @{prop "A = {}"} goes to @{prop "{}
532   \<subseteq> A"} and @{prop "A \<subseteq> {}"} and then back to @{prop "A = {}"}!
533 \<close>
535 lemma equalityE: "A = B ==> (A \<subseteq> B ==> B \<subseteq> A ==> P) ==> P"
536   by simp
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
542 lemma eqset_imp_iff: "A = B ==> (x : A) = (x : B)"
543   by simp
545 lemma eqelem_imp_iff: "x = y ==> (x : A) = (y : A)"
546   by simp
549 subsubsection \<open>The empty set\<close>
551 lemma empty_def:
552   "{} = {x. False}"
553   by (simp add: bot_set_def bot_fun_def)
555 lemma empty_iff [simp]: "(c : {}) = False"
558 lemma emptyE [elim!]: "a : {} ==> P"
559   by simp
561 lemma empty_subsetI [iff]: "{} \<subseteq> A"
562     \<comment> \<open>One effect is to delete the ASSUMPTION @{prop "{} <= A"}\<close>
563   by blast
565 lemma equals0I: "(!!y. y \<in> A ==> False) ==> A = {}"
566   by blast
568 lemma equals0D: "A = {} ==> a \<notin> A"
569     \<comment> \<open>Use for reasoning about disjointness: \<open>A Int B = {}\<close>\<close>
570   by blast
572 lemma ball_empty [simp]: "Ball {} P = True"
575 lemma bex_empty [simp]: "Bex {} P = False"
579 subsubsection \<open>The universal set -- UNIV\<close>
581 abbreviation UNIV :: "'a set" where
582   "UNIV \<equiv> top"
584 lemma UNIV_def:
585   "UNIV = {x. True}"
586   by (simp add: top_set_def top_fun_def)
588 lemma UNIV_I [simp]: "x : UNIV"
591 declare UNIV_I [intro]  \<comment> \<open>unsafe makes it less likely to cause problems\<close>
593 lemma UNIV_witness [intro?]: "EX x. x : UNIV"
594   by simp
596 lemma subset_UNIV: "A \<subseteq> UNIV"
597   by (fact top_greatest) (* already simp *)
599 text \<open>
600   \medskip Eta-contracting these two rules (to remove \<open>P\<close>)
601   causes them to be ignored because of their interaction with
602   congruence rules.
603 \<close>
605 lemma ball_UNIV [simp]: "Ball UNIV P = All P"
608 lemma bex_UNIV [simp]: "Bex UNIV P = Ex P"
611 lemma UNIV_eq_I: "(\<And>x. x \<in> A) \<Longrightarrow> UNIV = A"
612   by auto
614 lemma UNIV_not_empty [iff]: "UNIV ~= {}"
615   by (blast elim: equalityE)
617 lemma empty_not_UNIV[simp]: "{} \<noteq> UNIV"
618 by blast
620 subsubsection \<open>The Powerset operator -- Pow\<close>
622 definition Pow :: "'a set => 'a set set" where
623   Pow_def: "Pow A = {B. B \<le> A}"
625 lemma Pow_iff [iff]: "(A \<in> Pow B) = (A \<subseteq> B)"
628 lemma PowI: "A \<subseteq> B ==> A \<in> Pow B"
631 lemma PowD: "A \<in> Pow B ==> A \<subseteq> B"
634 lemma Pow_bottom: "{} \<in> Pow B"
635   by simp
637 lemma Pow_top: "A \<in> Pow A"
638   by simp
640 lemma Pow_not_empty: "Pow A \<noteq> {}"
641   using Pow_top by blast
644 subsubsection \<open>Set complement\<close>
646 lemma Compl_iff [simp]: "(c \<in> -A) = (c \<notin> A)"
647   by (simp add: fun_Compl_def uminus_set_def)
649 lemma ComplI [intro!]: "(c \<in> A ==> False) ==> c \<in> -A"
650   by (simp add: fun_Compl_def uminus_set_def) blast
652 text \<open>
653   \medskip This form, with negated conclusion, works well with the
654   Classical prover.  Negated assumptions behave like formulae on the
655   right side of the notional turnstile ...\<close>
657 lemma ComplD [dest!]: "c : -A ==> c~:A"
658   by simp
660 lemmas ComplE = ComplD [elim_format]
662 lemma Compl_eq: "- A = {x. ~ x : A}"
663   by blast
666 subsubsection \<open>Binary intersection\<close>
668 abbreviation inter :: "'a set \<Rightarrow> 'a set \<Rightarrow> 'a set"  (infixl "\<inter>" 70)
669   where "op \<inter> \<equiv> inf"
671 notation (ASCII)
672   inter  (infixl "Int" 70)
674 lemma Int_def:
675   "A \<inter> B = {x. x \<in> A \<and> x \<in> B}"
676   by (simp add: inf_set_def inf_fun_def)
678 lemma Int_iff [simp]: "(c : A Int B) = (c:A & c:B)"
679   by (unfold Int_def) blast
681 lemma IntI [intro!]: "c:A ==> c:B ==> c : A Int B"
682   by simp
684 lemma IntD1: "c : A Int B ==> c:A"
685   by simp
687 lemma IntD2: "c : A Int B ==> c:B"
688   by simp
690 lemma IntE [elim!]: "c : A Int B ==> (c:A ==> c:B ==> P) ==> P"
691   by simp
693 lemma mono_Int: "mono f \<Longrightarrow> f (A \<inter> B) \<subseteq> f A \<inter> f B"
694   by (fact mono_inf)
697 subsubsection \<open>Binary union\<close>
699 abbreviation union :: "'a set \<Rightarrow> 'a set \<Rightarrow> 'a set"  (infixl "\<union>" 65)
700   where "union \<equiv> sup"
702 notation (ASCII)
703   union  (infixl "Un" 65)
705 lemma Un_def:
706   "A \<union> B = {x. x \<in> A \<or> x \<in> B}"
707   by (simp add: sup_set_def sup_fun_def)
709 lemma Un_iff [simp]: "(c : A Un B) = (c:A | c:B)"
710   by (unfold Un_def) blast
712 lemma UnI1 [elim?]: "c:A ==> c : A Un B"
713   by simp
715 lemma UnI2 [elim?]: "c:B ==> c : A Un B"
716   by simp
718 text \<open>
719   \medskip Classical introduction rule: no commitment to @{prop A} vs
720   @{prop B}.
721 \<close>
723 lemma UnCI [intro!]: "(c~:B ==> c:A) ==> c : A Un B"
724   by auto
726 lemma UnE [elim!]: "c : A Un B ==> (c:A ==> P) ==> (c:B ==> P) ==> P"
727   by (unfold Un_def) blast
729 lemma insert_def: "insert a B = {x. x = a} \<union> B"
730   by (simp add: insert_compr Un_def)
732 lemma mono_Un: "mono f \<Longrightarrow> f A \<union> f B \<subseteq> f (A \<union> B)"
733   by (fact mono_sup)
736 subsubsection \<open>Set difference\<close>
738 lemma Diff_iff [simp]: "(c : A - B) = (c:A & c~:B)"
739   by (simp add: minus_set_def fun_diff_def)
741 lemma DiffI [intro!]: "c : A ==> c ~: B ==> c : A - B"
742   by simp
744 lemma DiffD1: "c : A - B ==> c : A"
745   by simp
747 lemma DiffD2: "c : A - B ==> c : B ==> P"
748   by simp
750 lemma DiffE [elim!]: "c : A - B ==> (c:A ==> c~:B ==> P) ==> P"
751   by simp
753 lemma set_diff_eq: "A - B = {x. x : A & ~ x : B}" by blast
755 lemma Compl_eq_Diff_UNIV: "-A = (UNIV - A)"
756 by blast
759 subsubsection \<open>Augmenting a set -- @{const insert}\<close>
761 lemma insert_iff [simp]: "(a : insert b A) = (a = b | a:A)"
762   by (unfold insert_def) blast
764 lemma insertI1: "a : insert a B"
765   by simp
767 lemma insertI2: "a : B ==> a : insert b B"
768   by simp
770 lemma insertE [elim!]: "a : insert b A ==> (a = b ==> P) ==> (a:A ==> P) ==> P"
771   by (unfold insert_def) blast
773 lemma insertCI [intro!]: "(a~:B ==> a = b) ==> a: insert b B"
774   \<comment> \<open>Classical introduction rule.\<close>
775   by auto
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
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
789 lemma insert_ident: "x ~: A ==> x ~: B ==> (insert x A = insert x B) = (A = B)"
790 by auto
792 lemma insert_eq_iff: assumes "a \<notin> A" "b \<notin> B"
793 shows "insert a A = insert b B \<longleftrightarrow>
794   (if a=b then A=B else \<exists>C. A = insert b C \<and> b \<notin> C \<and> B = insert a C \<and> a \<notin> C)"
795   (is "?L \<longleftrightarrow> ?R")
796 proof
797   assume ?L
798   show ?R
799   proof cases
800     assume "a=b" with assms \<open>?L\<close> show ?R by (simp add: insert_ident)
801   next
802     assume "a\<noteq>b"
803     let ?C = "A - {b}"
804     have "A = insert b ?C \<and> b \<notin> ?C \<and> B = insert a ?C \<and> a \<notin> ?C"
805       using assms \<open>?L\<close> \<open>a\<noteq>b\<close> by auto
806     thus ?R using \<open>a\<noteq>b\<close> by auto
807   qed
808 next
809   assume ?R thus ?L by (auto split: if_splits)
810 qed
812 lemma insert_UNIV: "insert x UNIV = UNIV"
813 by auto
815 subsubsection \<open>Singletons, using insert\<close>
817 lemma singletonI [intro!]: "a : {a}"
818     \<comment> \<open>Redundant? But unlike \<open>insertCI\<close>, it proves the subgoal immediately!\<close>
819   by (rule insertI1)
821 lemma singletonD [dest!]: "b : {a} ==> b = a"
822   by blast
824 lemmas singletonE = singletonD [elim_format]
826 lemma singleton_iff: "(b : {a}) = (b = a)"
827   by blast
829 lemma singleton_inject [dest!]: "{a} = {b} ==> a = b"
830   by blast
832 lemma singleton_insert_inj_eq [iff]:
833      "({b} = insert a A) = (a = b & A \<subseteq> {b})"
834   by blast
836 lemma singleton_insert_inj_eq' [iff]:
837      "(insert a A = {b}) = (a = b & A \<subseteq> {b})"
838   by blast
840 lemma subset_singletonD: "A \<subseteq> {x} ==> A = {} | A = {x}"
841   by fast
843 lemma singleton_conv [simp]: "{x. x = a} = {a}"
844   by blast
846 lemma singleton_conv2 [simp]: "{x. a = x} = {a}"
847   by blast
849 lemma Diff_single_insert: "A - {x} \<subseteq> B ==> A \<subseteq> insert x B"
850   by blast
852 lemma subset_Diff_insert: "A \<subseteq> B - (insert x C) \<longleftrightarrow> A \<subseteq> B - C \<and> x \<notin> A"
853   by blast
855 lemma doubleton_eq_iff: "({a,b} = {c,d}) = (a=c & b=d | a=d & b=c)"
856   by (blast elim: equalityE)
858 lemma Un_singleton_iff:
859   "(A \<union> B = {x}) = (A = {} \<and> B = {x} \<or> A = {x} \<and> B = {} \<or> A = {x} \<and> B = {x})"
860 by auto
862 lemma singleton_Un_iff:
863   "({x} = A \<union> B) = (A = {} \<and> B = {x} \<or> A = {x} \<and> B = {} \<or> A = {x} \<and> B = {x})"
864 by auto
867 subsubsection \<open>Image of a set under a function\<close>
869 text \<open>
870   Frequently @{term b} does not have the syntactic form of @{term "f x"}.
871 \<close>
873 definition image :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a set \<Rightarrow> 'b set" (infixr "" 90)
874 where
875   "f  A = {y. \<exists>x\<in>A. y = f x}"
877 lemma image_eqI [simp, intro]:
878   "b = f x \<Longrightarrow> x \<in> A \<Longrightarrow> b \<in> f  A"
879   by (unfold image_def) blast
881 lemma imageI:
882   "x \<in> A \<Longrightarrow> f x \<in> f  A"
883   by (rule image_eqI) (rule refl)
885 lemma rev_image_eqI:
886   "x \<in> A \<Longrightarrow> b = f x \<Longrightarrow> b \<in> f  A"
887   \<comment> \<open>This version's more effective when we already have the
888     required @{term x}.\<close>
889   by (rule image_eqI)
891 lemma imageE [elim!]:
892   assumes "b \<in> (\<lambda>x. f x)  A" \<comment> \<open>The eta-expansion gives variable-name preservation.\<close>
893   obtains x where "b = f x" and "x \<in> A"
894   using assms by (unfold image_def) blast
896 lemma Compr_image_eq:
897   "{x \<in> f  A. P x} = f  {x \<in> A. P (f x)}"
898   by auto
900 lemma image_Un:
901   "f  (A \<union> B) = f  A \<union> f  B"
902   by blast
904 lemma image_iff:
905   "z \<in> f  A \<longleftrightarrow> (\<exists>x\<in>A. z = f x)"
906   by blast
908 lemma image_subsetI:
909   "(\<And>x. x \<in> A \<Longrightarrow> f x \<in> B) \<Longrightarrow> f  A \<subseteq> B"
910   \<comment> \<open>Replaces the three steps \<open>subsetI\<close>, \<open>imageE\<close>,
911     \<open>hypsubst\<close>, but breaks too many existing proofs.\<close>
912   by blast
914 lemma image_subset_iff:
915   "f  A \<subseteq> B \<longleftrightarrow> (\<forall>x\<in>A. f x \<in> B)"
916   \<comment> \<open>This rewrite rule would confuse users if made default.\<close>
917   by blast
919 lemma subset_imageE:
920   assumes "B \<subseteq> f  A"
921   obtains C where "C \<subseteq> A" and "B = f  C"
922 proof -
923   from assms have "B = f  {a \<in> A. f a \<in> B}" by fast
924   moreover have "{a \<in> A. f a \<in> B} \<subseteq> A" by blast
925   ultimately show thesis by (blast intro: that)
926 qed
928 lemma subset_image_iff:
929   "B \<subseteq> f  A \<longleftrightarrow> (\<exists>AA\<subseteq>A. B = f  AA)"
930   by (blast elim: subset_imageE)
932 lemma image_ident [simp]:
933   "(\<lambda>x. x)  Y = Y"
934   by blast
936 lemma image_empty [simp]:
937   "f  {} = {}"
938   by blast
940 lemma image_insert [simp]:
941   "f  insert a B = insert (f a) (f  B)"
942   by blast
944 lemma image_constant:
945   "x \<in> A \<Longrightarrow> (\<lambda>x. c)  A = {c}"
946   by auto
948 lemma image_constant_conv:
949   "(\<lambda>x. c)  A = (if A = {} then {} else {c})"
950   by auto
952 lemma image_image:
953   "f  (g  A) = (\<lambda>x. f (g x))  A"
954   by blast
956 lemma insert_image [simp]:
957   "x \<in> A ==> insert (f x) (f  A) = f  A"
958   by blast
960 lemma image_is_empty [iff]:
961   "f  A = {} \<longleftrightarrow> A = {}"
962   by blast
964 lemma empty_is_image [iff]:
965   "{} = f  A \<longleftrightarrow> A = {}"
966   by blast
968 lemma image_Collect:
969   "f  {x. P x} = {f x | x. P x}"
970   \<comment> \<open>NOT suitable as a default simprule: the RHS isn't simpler than the LHS,
971       with its implicit quantifier and conjunction.  Also image enjoys better
972       equational properties than does the RHS.\<close>
973   by blast
975 lemma if_image_distrib [simp]:
976   "(\<lambda>x. if P x then f x else g x)  S
977     = (f  (S \<inter> {x. P x})) \<union> (g  (S \<inter> {x. \<not> P x}))"
978   by auto
980 lemma image_cong:
981   "M = N \<Longrightarrow> (\<And>x. x \<in> N \<Longrightarrow> f x = g x) \<Longrightarrow> f  M = g  N"
984 lemma image_Int_subset:
985   "f  (A \<inter> B) \<subseteq> f  A \<inter> f  B"
986   by blast
988 lemma image_diff_subset:
989   "f  A - f  B \<subseteq> f  (A - B)"
990   by blast
992 lemma Setcompr_eq_image: "{f x | x. x \<in> A} = f  A"
993   by blast
995 lemma setcompr_eq_image: "{f x |x. P x} = f  {x. P x}"
996   by auto
998 lemma ball_imageD:
999   assumes "\<forall>x\<in>f  A. P x"
1000   shows "\<forall>x\<in>A. P (f x)"
1001   using assms by simp
1003 lemma bex_imageD:
1004   assumes "\<exists>x\<in>f  A. P x"
1005   shows "\<exists>x\<in>A. P (f x)"
1006   using assms by auto
1009 text \<open>
1010   \medskip Range of a function -- just a translation for image!
1011 \<close>
1013 abbreviation range :: "('a \<Rightarrow> 'b) \<Rightarrow> 'b set"
1014 where \<comment> "of function"
1015   "range f \<equiv> f  UNIV"
1017 lemma range_eqI:
1018   "b = f x \<Longrightarrow> b \<in> range f"
1019   by simp
1021 lemma rangeI:
1022   "f x \<in> range f"
1023   by simp
1025 lemma rangeE [elim?]:
1026   "b \<in> range (\<lambda>x. f x) \<Longrightarrow> (\<And>x. b = f x \<Longrightarrow> P) \<Longrightarrow> P"
1027   by (rule imageE)
1029 lemma full_SetCompr_eq:
1030   "{u. \<exists>x. u = f x} = range f"
1031   by auto
1033 lemma range_composition:
1034   "range (\<lambda>x. f (g x)) = f  range g"
1035   by auto
1038 subsubsection \<open>Some rules with \<open>if\<close>\<close>
1040 text\<open>Elimination of \<open>{x. \<dots> & x=t & \<dots>}\<close>.\<close>
1042 lemma Collect_conv_if: "{x. x=a & P x} = (if P a then {a} else {})"
1043   by auto
1045 lemma Collect_conv_if2: "{x. a=x & P x} = (if P a then {a} else {})"
1046   by auto
1048 text \<open>
1049   Rewrite rules for boolean case-splitting: faster than \<open>split_if [split]\<close>.
1050 \<close>
1052 lemma split_if_eq1: "((if Q then x else y) = b) = ((Q --> x = b) & (~ Q --> y = b))"
1053   by (rule split_if)
1055 lemma split_if_eq2: "(a = (if Q then x else y)) = ((Q --> a = x) & (~ Q --> a = y))"
1056   by (rule split_if)
1058 text \<open>
1059   Split ifs on either side of the membership relation.  Not for \<open>[simp]\<close> -- can cause goals to blow up!
1060 \<close>
1062 lemma split_if_mem1: "((if Q then x else y) : b) = ((Q --> x : b) & (~ Q --> y : b))"
1063   by (rule split_if)
1065 lemma split_if_mem2: "(a : (if Q then x else y)) = ((Q --> a : x) & (~ Q --> a : y))"
1066   by (rule split_if [where P="%S. a : S"])
1068 lemmas split_ifs = if_bool_eq_conj split_if_eq1 split_if_eq2 split_if_mem1 split_if_mem2
1070 (*Would like to add these, but the existing code only searches for the
1071   outer-level constant, which in this case is just Set.member; we instead need
1072   to use term-nets to associate patterns with rules.  Also, if a rule fails to
1073   apply, then the formula should be kept.
1074   [("uminus", Compl_iff RS iffD1), ("minus", [Diff_iff RS iffD1]),
1075    ("Int", [IntD1,IntD2]),
1076    ("Collect", [CollectD]), ("Inter", [InterD]), ("INTER", [INT_D])]
1077  *)
1080 subsection \<open>Further operations and lemmas\<close>
1082 subsubsection \<open>The proper subset'' relation\<close>
1084 lemma psubsetI [intro!]: "A \<subseteq> B ==> A \<noteq> B ==> A \<subset> B"
1085   by (unfold less_le) blast
1087 lemma psubsetE [elim!]:
1088     "[|A \<subset> B;  [|A \<subseteq> B; ~ (B\<subseteq>A)|] ==> R|] ==> R"
1089   by (unfold less_le) blast
1091 lemma psubset_insert_iff:
1092   "(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)"
1093   by (auto simp add: less_le subset_insert_iff)
1095 lemma psubset_eq: "(A \<subset> B) = (A \<subseteq> B & A \<noteq> B)"
1096   by (simp only: less_le)
1098 lemma psubset_imp_subset: "A \<subset> B ==> A \<subseteq> B"
1101 lemma psubset_trans: "[| A \<subset> B; B \<subset> C |] ==> A \<subset> C"
1102 apply (unfold less_le)
1103 apply (auto dest: subset_antisym)
1104 done
1106 lemma psubsetD: "[| A \<subset> B; c \<in> A |] ==> c \<in> B"
1107 apply (unfold less_le)
1108 apply (auto dest: subsetD)
1109 done
1111 lemma psubset_subset_trans: "A \<subset> B ==> B \<subseteq> C ==> A \<subset> C"
1112   by (auto simp add: psubset_eq)
1114 lemma subset_psubset_trans: "A \<subseteq> B ==> B \<subset> C ==> A \<subset> C"
1115   by (auto simp add: psubset_eq)
1117 lemma psubset_imp_ex_mem: "A \<subset> B ==> \<exists>b. b \<in> (B - A)"
1118   by (unfold less_le) blast
1120 lemma atomize_ball:
1121     "(!!x. x \<in> A ==> P x) == Trueprop (\<forall>x\<in>A. P x)"
1122   by (simp only: Ball_def atomize_all atomize_imp)
1124 lemmas [symmetric, rulify] = atomize_ball
1125   and [symmetric, defn] = atomize_ball
1127 lemma image_Pow_mono:
1128   assumes "f  A \<subseteq> B"
1129   shows "image f  Pow A \<subseteq> Pow B"
1130   using assms by blast
1132 lemma image_Pow_surj:
1133   assumes "f  A = B"
1134   shows "image f  Pow A = Pow B"
1135   using assms by (blast elim: subset_imageE)
1138 subsubsection \<open>Derived rules involving subsets.\<close>
1140 text \<open>\<open>insert\<close>.\<close>
1142 lemma subset_insertI: "B \<subseteq> insert a B"
1143   by (rule subsetI) (erule insertI2)
1145 lemma subset_insertI2: "A \<subseteq> B \<Longrightarrow> A \<subseteq> insert b B"
1146   by blast
1148 lemma subset_insert: "x \<notin> A ==> (A \<subseteq> insert x B) = (A \<subseteq> B)"
1149   by blast
1152 text \<open>\medskip Finite Union -- the least upper bound of two sets.\<close>
1154 lemma Un_upper1: "A \<subseteq> A \<union> B"
1155   by (fact sup_ge1)
1157 lemma Un_upper2: "B \<subseteq> A \<union> B"
1158   by (fact sup_ge2)
1160 lemma Un_least: "A \<subseteq> C ==> B \<subseteq> C ==> A \<union> B \<subseteq> C"
1161   by (fact sup_least)
1164 text \<open>\medskip Finite Intersection -- the greatest lower bound of two sets.\<close>
1166 lemma Int_lower1: "A \<inter> B \<subseteq> A"
1167   by (fact inf_le1)
1169 lemma Int_lower2: "A \<inter> B \<subseteq> B"
1170   by (fact inf_le2)
1172 lemma Int_greatest: "C \<subseteq> A ==> C \<subseteq> B ==> C \<subseteq> A \<inter> B"
1173   by (fact inf_greatest)
1176 text \<open>\medskip Set difference.\<close>
1178 lemma Diff_subset: "A - B \<subseteq> A"
1179   by blast
1181 lemma Diff_subset_conv: "(A - B \<subseteq> C) = (A \<subseteq> B \<union> C)"
1182 by blast
1185 subsubsection \<open>Equalities involving union, intersection, inclusion, etc.\<close>
1187 text \<open>\<open>{}\<close>.\<close>
1189 lemma Collect_const [simp]: "{s. P} = (if P then UNIV else {})"
1190   \<comment> \<open>supersedes \<open>Collect_False_empty\<close>\<close>
1191   by auto
1193 lemma subset_empty [simp]: "(A \<subseteq> {}) = (A = {})"
1194   by (fact bot_unique)
1196 lemma not_psubset_empty [iff]: "\<not> (A < {})"
1197   by (fact not_less_bot) (* FIXME: already simp *)
1199 lemma Collect_empty_eq [simp]: "(Collect P = {}) = (\<forall>x. \<not> P x)"
1200 by blast
1202 lemma empty_Collect_eq [simp]: "({} = Collect P) = (\<forall>x. \<not> P x)"
1203 by blast
1205 lemma Collect_neg_eq: "{x. \<not> P x} = - {x. P x}"
1206   by blast
1208 lemma Collect_disj_eq: "{x. P x | Q x} = {x. P x} \<union> {x. Q x}"
1209   by blast
1211 lemma Collect_imp_eq: "{x. P x --> Q x} = -{x. P x} \<union> {x. Q x}"
1212   by blast
1214 lemma Collect_conj_eq: "{x. P x & Q x} = {x. P x} \<inter> {x. Q x}"
1215   by blast
1217 lemma Collect_mono_iff: "Collect P \<subseteq> Collect Q \<longleftrightarrow> (\<forall>x. P x \<longrightarrow> Q x)"
1218   by blast
1221 text \<open>\medskip \<open>insert\<close>.\<close>
1223 lemma insert_is_Un: "insert a A = {a} Un A"
1224   \<comment> \<open>NOT SUITABLE FOR REWRITING since \<open>{a} == insert a {}\<close>\<close>
1225   by blast
1227 lemma insert_not_empty [simp]: "insert a A \<noteq> {}"
1228   by blast
1230 lemmas empty_not_insert = insert_not_empty [symmetric]
1231 declare empty_not_insert [simp]
1233 lemma insert_absorb: "a \<in> A ==> insert a A = A"
1234   \<comment> \<open>\<open>[simp]\<close> causes recursive calls when there are nested inserts\<close>
1235   \<comment> \<open>with \emph{quadratic} running time\<close>
1236   by blast
1238 lemma insert_absorb2 [simp]: "insert x (insert x A) = insert x A"
1239   by blast
1241 lemma insert_commute: "insert x (insert y A) = insert y (insert x A)"
1242   by blast
1244 lemma insert_subset [simp]: "(insert x A \<subseteq> B) = (x \<in> B & A \<subseteq> B)"
1245   by blast
1247 lemma mk_disjoint_insert: "a \<in> A ==> \<exists>B. A = insert a B & a \<notin> B"
1248   \<comment> \<open>use new \<open>B\<close> rather than \<open>A - {a}\<close> to avoid infinite unfolding\<close>
1249   apply (rule_tac x = "A - {a}" in exI, blast)
1250   done
1252 lemma insert_Collect: "insert a (Collect P) = {u. u \<noteq> a --> P u}"
1253   by auto
1255 lemma insert_inter_insert[simp]: "insert a A \<inter> insert a B = insert a (A \<inter> B)"
1256   by blast
1258 lemma insert_disjoint [simp]:
1259  "(insert a A \<inter> B = {}) = (a \<notin> B \<and> A \<inter> B = {})"
1260  "({} = insert a A \<inter> B) = (a \<notin> B \<and> {} = A \<inter> B)"
1261   by auto
1263 lemma disjoint_insert [simp]:
1264  "(B \<inter> insert a A = {}) = (a \<notin> B \<and> B \<inter> A = {})"
1265  "({} = A \<inter> insert b B) = (b \<notin> A \<and> {} = A \<inter> B)"
1266   by auto
1269 text \<open>\medskip \<open>Int\<close>\<close>
1271 lemma Int_absorb: "A \<inter> A = A"
1272   by (fact inf_idem) (* already simp *)
1274 lemma Int_left_absorb: "A \<inter> (A \<inter> B) = A \<inter> B"
1275   by (fact inf_left_idem)
1277 lemma Int_commute: "A \<inter> B = B \<inter> A"
1278   by (fact inf_commute)
1280 lemma Int_left_commute: "A \<inter> (B \<inter> C) = B \<inter> (A \<inter> C)"
1281   by (fact inf_left_commute)
1283 lemma Int_assoc: "(A \<inter> B) \<inter> C = A \<inter> (B \<inter> C)"
1284   by (fact inf_assoc)
1286 lemmas Int_ac = Int_assoc Int_left_absorb Int_commute Int_left_commute
1287   \<comment> \<open>Intersection is an AC-operator\<close>
1289 lemma Int_absorb1: "B \<subseteq> A ==> A \<inter> B = B"
1290   by (fact inf_absorb2)
1292 lemma Int_absorb2: "A \<subseteq> B ==> A \<inter> B = A"
1293   by (fact inf_absorb1)
1295 lemma Int_empty_left: "{} \<inter> B = {}"
1296   by (fact inf_bot_left) (* already simp *)
1298 lemma Int_empty_right: "A \<inter> {} = {}"
1299   by (fact inf_bot_right) (* already simp *)
1301 lemma disjoint_eq_subset_Compl: "(A \<inter> B = {}) = (A \<subseteq> -B)"
1302   by blast
1304 lemma disjoint_iff_not_equal: "(A \<inter> B = {}) = (\<forall>x\<in>A. \<forall>y\<in>B. x \<noteq> y)"
1305   by blast
1307 lemma Int_UNIV_left: "UNIV \<inter> B = B"
1308   by (fact inf_top_left) (* already simp *)
1310 lemma Int_UNIV_right: "A \<inter> UNIV = A"
1311   by (fact inf_top_right) (* already simp *)
1313 lemma Int_Un_distrib: "A \<inter> (B \<union> C) = (A \<inter> B) \<union> (A \<inter> C)"
1314   by (fact inf_sup_distrib1)
1316 lemma Int_Un_distrib2: "(B \<union> C) \<inter> A = (B \<inter> A) \<union> (C \<inter> A)"
1317   by (fact inf_sup_distrib2)
1319 lemma Int_UNIV [simp]: "(A \<inter> B = UNIV) = (A = UNIV & B = UNIV)"
1320   by (fact inf_eq_top_iff) (* already simp *)
1322 lemma Int_subset_iff [simp]: "(C \<subseteq> A \<inter> B) = (C \<subseteq> A & C \<subseteq> B)"
1323   by (fact le_inf_iff)
1325 lemma Int_Collect: "(x \<in> A \<inter> {x. P x}) = (x \<in> A & P x)"
1326   by blast
1329 text \<open>\medskip \<open>Un\<close>.\<close>
1331 lemma Un_absorb: "A \<union> A = A"
1332   by (fact sup_idem) (* already simp *)
1334 lemma Un_left_absorb: "A \<union> (A \<union> B) = A \<union> B"
1335   by (fact sup_left_idem)
1337 lemma Un_commute: "A \<union> B = B \<union> A"
1338   by (fact sup_commute)
1340 lemma Un_left_commute: "A \<union> (B \<union> C) = B \<union> (A \<union> C)"
1341   by (fact sup_left_commute)
1343 lemma Un_assoc: "(A \<union> B) \<union> C = A \<union> (B \<union> C)"
1344   by (fact sup_assoc)
1346 lemmas Un_ac = Un_assoc Un_left_absorb Un_commute Un_left_commute
1347   \<comment> \<open>Union is an AC-operator\<close>
1349 lemma Un_absorb1: "A \<subseteq> B ==> A \<union> B = B"
1350   by (fact sup_absorb2)
1352 lemma Un_absorb2: "B \<subseteq> A ==> A \<union> B = A"
1353   by (fact sup_absorb1)
1355 lemma Un_empty_left: "{} \<union> B = B"
1356   by (fact sup_bot_left) (* already simp *)
1358 lemma Un_empty_right: "A \<union> {} = A"
1359   by (fact sup_bot_right) (* already simp *)
1361 lemma Un_UNIV_left: "UNIV \<union> B = UNIV"
1362   by (fact sup_top_left) (* already simp *)
1364 lemma Un_UNIV_right: "A \<union> UNIV = UNIV"
1365   by (fact sup_top_right) (* already simp *)
1367 lemma Un_insert_left [simp]: "(insert a B) \<union> C = insert a (B \<union> C)"
1368   by blast
1370 lemma Un_insert_right [simp]: "A \<union> (insert a B) = insert a (A \<union> B)"
1371   by blast
1373 lemma Int_insert_left:
1374     "(insert a B) Int C = (if a \<in> C then insert a (B \<inter> C) else B \<inter> C)"
1375   by auto
1377 lemma Int_insert_left_if0[simp]:
1378     "a \<notin> C \<Longrightarrow> (insert a B) Int C = B \<inter> C"
1379   by auto
1381 lemma Int_insert_left_if1[simp]:
1382     "a \<in> C \<Longrightarrow> (insert a B) Int C = insert a (B Int C)"
1383   by auto
1385 lemma Int_insert_right:
1386     "A \<inter> (insert a B) = (if a \<in> A then insert a (A \<inter> B) else A \<inter> B)"
1387   by auto
1389 lemma Int_insert_right_if0[simp]:
1390     "a \<notin> A \<Longrightarrow> A Int (insert a B) = A Int B"
1391   by auto
1393 lemma Int_insert_right_if1[simp]:
1394     "a \<in> A \<Longrightarrow> A Int (insert a B) = insert a (A Int B)"
1395   by auto
1397 lemma Un_Int_distrib: "A \<union> (B \<inter> C) = (A \<union> B) \<inter> (A \<union> C)"
1398   by (fact sup_inf_distrib1)
1400 lemma Un_Int_distrib2: "(B \<inter> C) \<union> A = (B \<union> A) \<inter> (C \<union> A)"
1401   by (fact sup_inf_distrib2)
1403 lemma Un_Int_crazy:
1404     "(A \<inter> B) \<union> (B \<inter> C) \<union> (C \<inter> A) = (A \<union> B) \<inter> (B \<union> C) \<inter> (C \<union> A)"
1405   by blast
1407 lemma subset_Un_eq: "(A \<subseteq> B) = (A \<union> B = B)"
1408   by (fact le_iff_sup)
1410 lemma Un_empty [iff]: "(A \<union> B = {}) = (A = {} & B = {})"
1411   by (fact sup_eq_bot_iff) (* FIXME: already simp *)
1413 lemma Un_subset_iff [simp]: "(A \<union> B \<subseteq> C) = (A \<subseteq> C & B \<subseteq> C)"
1414   by (fact le_sup_iff)
1416 lemma Un_Diff_Int: "(A - B) \<union> (A \<inter> B) = A"
1417   by blast
1419 lemma Diff_Int2: "A \<inter> C - B \<inter> C = A \<inter> C - B"
1420   by blast
1423 text \<open>\medskip Set complement\<close>
1425 lemma Compl_disjoint [simp]: "A \<inter> -A = {}"
1426   by (fact inf_compl_bot)
1428 lemma Compl_disjoint2 [simp]: "-A \<inter> A = {}"
1429   by (fact compl_inf_bot)
1431 lemma Compl_partition: "A \<union> -A = UNIV"
1432   by (fact sup_compl_top)
1434 lemma Compl_partition2: "-A \<union> A = UNIV"
1435   by (fact compl_sup_top)
1437 lemma double_complement: "- (-A) = (A::'a set)"
1438   by (fact double_compl) (* already simp *)
1440 lemma Compl_Un: "-(A \<union> B) = (-A) \<inter> (-B)"
1441   by (fact compl_sup) (* already simp *)
1443 lemma Compl_Int: "-(A \<inter> B) = (-A) \<union> (-B)"
1444   by (fact compl_inf) (* already simp *)
1446 lemma subset_Compl_self_eq: "(A \<subseteq> -A) = (A = {})"
1447   by blast
1449 lemma Un_Int_assoc_eq: "((A \<inter> B) \<union> C = A \<inter> (B \<union> C)) = (C \<subseteq> A)"
1450   \<comment> \<open>Halmos, Naive Set Theory, page 16.\<close>
1451   by blast
1453 lemma Compl_UNIV_eq: "-UNIV = {}"
1454   by (fact compl_top_eq) (* already simp *)
1456 lemma Compl_empty_eq: "-{} = UNIV"
1457   by (fact compl_bot_eq) (* already simp *)
1459 lemma Compl_subset_Compl_iff [iff]: "(-A \<subseteq> -B) = (B \<subseteq> A)"
1460   by (fact compl_le_compl_iff) (* FIXME: already simp *)
1462 lemma Compl_eq_Compl_iff [iff]: "(-A = -B) = (A = (B::'a set))"
1463   by (fact compl_eq_compl_iff) (* FIXME: already simp *)
1465 lemma Compl_insert: "- insert x A = (-A) - {x}"
1466   by blast
1468 text \<open>\medskip Bounded quantifiers.
1470   The following are not added to the default simpset because
1471   (a) they duplicate the body and (b) there are no similar rules for \<open>Int\<close>.\<close>
1473 lemma ball_Un: "(\<forall>x \<in> A \<union> B. P x) = ((\<forall>x\<in>A. P x) & (\<forall>x\<in>B. P x))"
1474   by blast
1476 lemma bex_Un: "(\<exists>x \<in> A \<union> B. P x) = ((\<exists>x\<in>A. P x) | (\<exists>x\<in>B. P x))"
1477   by blast
1480 text \<open>\medskip Set difference.\<close>
1482 lemma Diff_eq: "A - B = A \<inter> (-B)"
1483   by blast
1485 lemma Diff_eq_empty_iff [simp]: "(A - B = {}) = (A \<subseteq> B)"
1486   by blast
1488 lemma Diff_cancel [simp]: "A - A = {}"
1489   by blast
1491 lemma Diff_idemp [simp]: "(A - B) - B = A - (B::'a set)"
1492 by blast
1494 lemma Diff_triv: "A \<inter> B = {} ==> A - B = A"
1495   by (blast elim: equalityE)
1497 lemma empty_Diff [simp]: "{} - A = {}"
1498   by blast
1500 lemma Diff_empty [simp]: "A - {} = A"
1501   by blast
1503 lemma Diff_UNIV [simp]: "A - UNIV = {}"
1504   by blast
1506 lemma Diff_insert0 [simp]: "x \<notin> A ==> A - insert x B = A - B"
1507   by blast
1509 lemma Diff_insert: "A - insert a B = A - B - {a}"
1510   \<comment> \<open>NOT SUITABLE FOR REWRITING since \<open>{a} == insert a 0\<close>\<close>
1511   by blast
1513 lemma Diff_insert2: "A - insert a B = A - {a} - B"
1514   \<comment> \<open>NOT SUITABLE FOR REWRITING since \<open>{a} == insert a 0\<close>\<close>
1515   by blast
1517 lemma insert_Diff_if: "insert x A - B = (if x \<in> B then A - B else insert x (A - B))"
1518   by auto
1520 lemma insert_Diff1 [simp]: "x \<in> B ==> insert x A - B = A - B"
1521   by blast
1523 lemma insert_Diff_single[simp]: "insert a (A - {a}) = insert a A"
1524 by blast
1526 lemma insert_Diff: "a \<in> A ==> insert a (A - {a}) = A"
1527   by blast
1529 lemma Diff_insert_absorb: "x \<notin> A ==> (insert x A) - {x} = A"
1530   by auto
1532 lemma Diff_disjoint [simp]: "A \<inter> (B - A) = {}"
1533   by blast
1535 lemma Diff_partition: "A \<subseteq> B ==> A \<union> (B - A) = B"
1536   by blast
1538 lemma double_diff: "A \<subseteq> B ==> B \<subseteq> C ==> B - (C - A) = A"
1539   by blast
1541 lemma Un_Diff_cancel [simp]: "A \<union> (B - A) = A \<union> B"
1542   by blast
1544 lemma Un_Diff_cancel2 [simp]: "(B - A) \<union> A = B \<union> A"
1545   by blast
1547 lemma Diff_Un: "A - (B \<union> C) = (A - B) \<inter> (A - C)"
1548   by blast
1550 lemma Diff_Int: "A - (B \<inter> C) = (A - B) \<union> (A - C)"
1551   by blast
1553 lemma Diff_Diff_Int: "A - (A - B) = A \<inter> B"
1554   by blast
1556 lemma Un_Diff: "(A \<union> B) - C = (A - C) \<union> (B - C)"
1557   by blast
1559 lemma Int_Diff: "(A \<inter> B) - C = A \<inter> (B - C)"
1560   by blast
1562 lemma Diff_Int_distrib: "C \<inter> (A - B) = (C \<inter> A) - (C \<inter> B)"
1563   by blast
1565 lemma Diff_Int_distrib2: "(A - B) \<inter> C = (A \<inter> C) - (B \<inter> C)"
1566   by blast
1568 lemma Diff_Compl [simp]: "A - (- B) = A \<inter> B"
1569   by auto
1571 lemma Compl_Diff_eq [simp]: "- (A - B) = -A \<union> B"
1572   by blast
1575 text \<open>\medskip Quantification over type @{typ bool}.\<close>
1577 lemma bool_induct: "P True \<Longrightarrow> P False \<Longrightarrow> P x"
1578   by (cases x) auto
1580 lemma all_bool_eq: "(\<forall>b. P b) \<longleftrightarrow> P True \<and> P False"
1581   by (auto intro: bool_induct)
1583 lemma bool_contrapos: "P x \<Longrightarrow> \<not> P False \<Longrightarrow> P True"
1584   by (cases x) auto
1586 lemma ex_bool_eq: "(\<exists>b. P b) \<longleftrightarrow> P True \<or> P False"
1587   by (auto intro: bool_contrapos)
1589 lemma UNIV_bool: "UNIV = {False, True}"
1590   by (auto intro: bool_induct)
1592 text \<open>\medskip \<open>Pow\<close>\<close>
1594 lemma Pow_empty [simp]: "Pow {} = {{}}"
1595   by (auto simp add: Pow_def)
1597 lemma Pow_singleton_iff [simp]: "Pow X = {Y} \<longleftrightarrow> X = {} \<and> Y = {}"
1598 by blast
1600 lemma Pow_insert: "Pow (insert a A) = Pow A \<union> (insert a  Pow A)"
1601   by (blast intro: image_eqI [where ?x = "u - {a}" for u])
1603 lemma Pow_Compl: "Pow (- A) = {-B | B. A \<in> Pow B}"
1604   by (blast intro: exI [where ?x = "- u" for u])
1606 lemma Pow_UNIV [simp]: "Pow UNIV = UNIV"
1607   by blast
1609 lemma Un_Pow_subset: "Pow A \<union> Pow B \<subseteq> Pow (A \<union> B)"
1610   by blast
1612 lemma Pow_Int_eq [simp]: "Pow (A \<inter> B) = Pow A \<inter> Pow B"
1613   by blast
1616 text \<open>\medskip Miscellany.\<close>
1618 lemma set_eq_subset: "(A = B) = (A \<subseteq> B & B \<subseteq> A)"
1619   by blast
1621 lemma subset_iff: "(A \<subseteq> B) = (\<forall>t. t \<in> A --> t \<in> B)"
1622   by blast
1624 lemma subset_iff_psubset_eq: "(A \<subseteq> B) = ((A \<subset> B) | (A = B))"
1625   by (unfold less_le) blast
1627 lemma all_not_in_conv [simp]: "(\<forall>x. x \<notin> A) = (A = {})"
1628   by blast
1630 lemma ex_in_conv: "(\<exists>x. x \<in> A) = (A \<noteq> {})"
1631   by blast
1633 lemma ball_simps [simp, no_atp]:
1634   "\<And>A P Q. (\<forall>x\<in>A. P x \<or> Q) \<longleftrightarrow> ((\<forall>x\<in>A. P x) \<or> Q)"
1635   "\<And>A P Q. (\<forall>x\<in>A. P \<or> Q x) \<longleftrightarrow> (P \<or> (\<forall>x\<in>A. Q x))"
1636   "\<And>A P Q. (\<forall>x\<in>A. P \<longrightarrow> Q x) \<longleftrightarrow> (P \<longrightarrow> (\<forall>x\<in>A. Q x))"
1637   "\<And>A P Q. (\<forall>x\<in>A. P x \<longrightarrow> Q) \<longleftrightarrow> ((\<exists>x\<in>A. P x) \<longrightarrow> Q)"
1638   "\<And>P. (\<forall>x\<in>{}. P x) \<longleftrightarrow> True"
1639   "\<And>P. (\<forall>x\<in>UNIV. P x) \<longleftrightarrow> (\<forall>x. P x)"
1640   "\<And>a B P. (\<forall>x\<in>insert a B. P x) \<longleftrightarrow> (P a \<and> (\<forall>x\<in>B. P x))"
1641   "\<And>P Q. (\<forall>x\<in>Collect Q. P x) \<longleftrightarrow> (\<forall>x. Q x \<longrightarrow> P x)"
1642   "\<And>A P f. (\<forall>x\<in>fA. P x) \<longleftrightarrow> (\<forall>x\<in>A. P (f x))"
1643   "\<And>A P. (\<not> (\<forall>x\<in>A. P x)) \<longleftrightarrow> (\<exists>x\<in>A. \<not> P x)"
1644   by auto
1646 lemma bex_simps [simp, no_atp]:
1647   "\<And>A P Q. (\<exists>x\<in>A. P x \<and> Q) \<longleftrightarrow> ((\<exists>x\<in>A. P x) \<and> Q)"
1648   "\<And>A P Q. (\<exists>x\<in>A. P \<and> Q x) \<longleftrightarrow> (P \<and> (\<exists>x\<in>A. Q x))"
1649   "\<And>P. (\<exists>x\<in>{}. P x) \<longleftrightarrow> False"
1650   "\<And>P. (\<exists>x\<in>UNIV. P x) \<longleftrightarrow> (\<exists>x. P x)"
1651   "\<And>a B P. (\<exists>x\<in>insert a B. P x) \<longleftrightarrow> (P a | (\<exists>x\<in>B. P x))"
1652   "\<And>P Q. (\<exists>x\<in>Collect Q. P x) \<longleftrightarrow> (\<exists>x. Q x \<and> P x)"
1653   "\<And>A P f. (\<exists>x\<in>fA. P x) \<longleftrightarrow> (\<exists>x\<in>A. P (f x))"
1654   "\<And>A P. (\<not>(\<exists>x\<in>A. P x)) \<longleftrightarrow> (\<forall>x\<in>A. \<not> P x)"
1655   by auto
1658 subsubsection \<open>Monotonicity of various operations\<close>
1660 lemma image_mono: "A \<subseteq> B ==> fA \<subseteq> fB"
1661   by blast
1663 lemma Pow_mono: "A \<subseteq> B ==> Pow A \<subseteq> Pow B"
1664   by blast
1666 lemma insert_mono: "C \<subseteq> D ==> insert a C \<subseteq> insert a D"
1667   by blast
1669 lemma Un_mono: "A \<subseteq> C ==> B \<subseteq> D ==> A \<union> B \<subseteq> C \<union> D"
1670   by (fact sup_mono)
1672 lemma Int_mono: "A \<subseteq> C ==> B \<subseteq> D ==> A \<inter> B \<subseteq> C \<inter> D"
1673   by (fact inf_mono)
1675 lemma Diff_mono: "A \<subseteq> C ==> D \<subseteq> B ==> A - B \<subseteq> C - D"
1676   by blast
1678 lemma Compl_anti_mono: "A \<subseteq> B ==> -B \<subseteq> -A"
1679   by (fact compl_mono)
1681 text \<open>\medskip Monotonicity of implications.\<close>
1683 lemma in_mono: "A \<subseteq> B ==> x \<in> A --> x \<in> B"
1684   apply (rule impI)
1685   apply (erule subsetD, assumption)
1686   done
1688 lemma conj_mono: "P1 --> Q1 ==> P2 --> Q2 ==> (P1 & P2) --> (Q1 & Q2)"
1689   by iprover
1691 lemma disj_mono: "P1 --> Q1 ==> P2 --> Q2 ==> (P1 | P2) --> (Q1 | Q2)"
1692   by iprover
1694 lemma imp_mono: "Q1 --> P1 ==> P2 --> Q2 ==> (P1 --> P2) --> (Q1 --> Q2)"
1695   by iprover
1697 lemma imp_refl: "P --> P" ..
1699 lemma not_mono: "Q --> P ==> ~ P --> ~ Q"
1700   by iprover
1702 lemma ex_mono: "(!!x. P x --> Q x) ==> (EX x. P x) --> (EX x. Q x)"
1703   by iprover
1705 lemma all_mono: "(!!x. P x --> Q x) ==> (ALL x. P x) --> (ALL x. Q x)"
1706   by iprover
1708 lemma Collect_mono: "(!!x. P x --> Q x) ==> Collect P \<subseteq> Collect Q"
1709   by blast
1711 lemma Int_Collect_mono:
1712     "A \<subseteq> B ==> (!!x. x \<in> A ==> P x --> Q x) ==> A \<inter> Collect P \<subseteq> B \<inter> Collect Q"
1713   by blast
1715 lemmas basic_monos =
1716   subset_refl imp_refl disj_mono conj_mono
1717   ex_mono Collect_mono in_mono
1719 lemma eq_to_mono: "a = b ==> c = d ==> b --> d ==> a --> c"
1720   by iprover
1723 subsubsection \<open>Inverse image of a function\<close>
1725 definition vimage :: "('a => 'b) => 'b set => 'a set" (infixr "-" 90) where
1726   "f - B == {x. f x : B}"
1728 lemma vimage_eq [simp]: "(a : f - B) = (f a : B)"
1729   by (unfold vimage_def) blast
1731 lemma vimage_singleton_eq: "(a : f - {b}) = (f a = b)"
1732   by simp
1734 lemma vimageI [intro]: "f a = b ==> b:B ==> a : f - B"
1735   by (unfold vimage_def) blast
1737 lemma vimageI2: "f a : A ==> a : f - A"
1738   by (unfold vimage_def) fast
1740 lemma vimageE [elim!]: "a: f - B ==> (!!x. f a = x ==> x:B ==> P) ==> P"
1741   by (unfold vimage_def) blast
1743 lemma vimageD: "a : f - A ==> f a : A"
1744   by (unfold vimage_def) fast
1746 lemma vimage_empty [simp]: "f - {} = {}"
1747   by blast
1749 lemma vimage_Compl: "f - (-A) = -(f - A)"
1750   by blast
1752 lemma vimage_Un [simp]: "f - (A Un B) = (f - A) Un (f - B)"
1753   by blast
1755 lemma vimage_Int [simp]: "f - (A Int B) = (f - A) Int (f - B)"
1756   by fast
1758 lemma vimage_Collect_eq [simp]: "f - Collect P = {y. P (f y)}"
1759   by blast
1761 lemma vimage_Collect: "(!!x. P (f x) = Q x) ==> f - (Collect P) = Collect Q"
1762   by blast
1764 lemma vimage_insert: "f-(insert a B) = (f-{a}) Un (f-B)"
1765   \<comment> \<open>NOT suitable for rewriting because of the recurrence of @{term "{a}"}.\<close>
1766   by blast
1768 lemma vimage_Diff: "f - (A - B) = (f - A) - (f - B)"
1769   by blast
1771 lemma vimage_UNIV [simp]: "f - UNIV = UNIV"
1772   by blast
1774 lemma vimage_mono: "A \<subseteq> B ==> f - A \<subseteq> f - B"
1775   \<comment> \<open>monotonicity\<close>
1776   by blast
1778 lemma vimage_image_eq: "f - (f  A) = {y. EX x:A. f x = f y}"
1779 by (blast intro: sym)
1781 lemma image_vimage_subset: "f  (f - A) <= A"
1782 by blast
1784 lemma image_vimage_eq [simp]: "f  (f - A) = A Int range f"
1785 by blast
1787 lemma image_subset_iff_subset_vimage: "f  A \<subseteq> B \<longleftrightarrow> A \<subseteq> f - B"
1788   by blast
1790 lemma vimage_const [simp]: "((\<lambda>x. c) - A) = (if c \<in> A then UNIV else {})"
1791   by auto
1793 lemma vimage_if [simp]: "((\<lambda>x. if x \<in> B then c else d) - A) =
1794    (if c \<in> A then (if d \<in> A then UNIV else B)
1795     else if d \<in> A then -B else {})"
1796   by (auto simp add: vimage_def)
1798 lemma vimage_inter_cong:
1799   "(\<And> w. w \<in> S \<Longrightarrow> f w = g w) \<Longrightarrow> f - y \<inter> S = g - y \<inter> S"
1800   by auto
1802 lemma vimage_ident [simp]: "(%x. x) - Y = Y"
1803   by blast
1806 subsubsection \<open>Getting the Contents of a Singleton Set\<close>
1808 definition the_elem :: "'a set \<Rightarrow> 'a" where
1809   "the_elem X = (THE x. X = {x})"
1811 lemma the_elem_eq [simp]: "the_elem {x} = x"
1814 lemma the_elem_image_unique:
1815   assumes "A \<noteq> {}"
1816   assumes *: "\<And>y. y \<in> A \<Longrightarrow> f y = f x"
1817   shows "the_elem (f  A) = f x"
1818 unfolding the_elem_def proof (rule the1_equality)
1819   from \<open>A \<noteq> {}\<close> obtain y where "y \<in> A" by auto
1820   with * have "f x = f y" by simp
1821   with \<open>y \<in> A\<close> have "f x \<in> f  A" by blast
1822   with * show "f  A = {f x}" by auto
1823   then show "\<exists>!x. f  A = {x}" by auto
1824 qed
1827 subsubsection \<open>Least value operator\<close>
1829 lemma Least_mono:
1830   "mono (f::'a::order => 'b::order) ==> EX x:S. ALL y:S. x <= y
1831     ==> (LEAST y. y : f  S) = f (LEAST x. x : S)"
1832     \<comment> \<open>Courtesy of Stephan Merz\<close>
1833   apply clarify
1834   apply (erule_tac P = "%x. x : S" in LeastI2_order, fast)
1835   apply (rule LeastI2_order)
1836   apply (auto elim: monoD intro!: order_antisym)
1837   done
1842 definition bind :: "'a set \<Rightarrow> ('a \<Rightarrow> 'b set) \<Rightarrow> 'b set" where
1843   "bind A f = {x. \<exists>B \<in> fA. x \<in> B}"
1845 hide_const (open) bind
1847 lemma bind_bind:
1848   fixes A :: "'a set"
1849   shows "Set.bind (Set.bind A B) C = Set.bind A (\<lambda>x. Set.bind (B x) C)"
1850   by (auto simp add: bind_def)
1852 lemma empty_bind [simp]:
1853   "Set.bind {} f = {}"
1856 lemma nonempty_bind_const:
1857   "A \<noteq> {} \<Longrightarrow> Set.bind A (\<lambda>_. B) = B"
1858   by (auto simp add: bind_def)
1860 lemma bind_const: "Set.bind A (\<lambda>_. B) = (if A = {} then {} else B)"
1861   by (auto simp add: bind_def)
1863 lemma bind_singleton_conv_image: "Set.bind A (\<lambda>x. {f x}) = f ` A"
1866 subsubsection \<open>Operations for execution\<close>
1868 definition is_empty :: "'a set \<Rightarrow> bool" where
1869   [code_abbrev]: "is_empty A \<longleftrightarrow> A = {}"
1871 hide_const (open) is_empty
1873 definition remove :: "'a \<Rightarrow> 'a set \<Rightarrow> 'a set" where
1874   [code_abbrev]: "remove x A = A - {x}"
1876 hide_const (open) remove
1878 lemma member_remove [simp]:
1879   "x \<in> Set.remove y A \<longleftrightarrow> x \<in> A \<and> x \<noteq> y"
1882 definition filter :: "('a \<Rightarrow> bool) \<Rightarrow> 'a set \<Rightarrow> 'a set" where
1883   [code_abbrev]: "filter P A = {a \<in> A. P a}"
1885 hide_const (open) filter
1887 lemma member_filter [simp]:
1888   "x \<in> Set.filter P A \<longleftrightarrow> x \<in> A \<and> P x"
1891 instantiation set :: (equal) equal
1892 begin
1894 definition
1895   "HOL.equal A B \<longleftrightarrow> A \<subseteq> B \<and> B \<subseteq> A"
1897 instance proof
1898 qed (auto simp add: equal_set_def)
1900 end
1903 text \<open>Misc\<close>
1905 definition "pairwise R S \<longleftrightarrow> (\<forall>x \<in> S. \<forall>y\<in> S. x\<noteq>y \<longrightarrow> R x y)"
1907 hide_const (open) member not_member
1909 lemmas equalityI = subset_antisym
1911 ML \<open>
1912 val Ball_def = @{thm Ball_def}
1913 val Bex_def = @{thm Bex_def}
1914 val CollectD = @{thm CollectD}
1915 val CollectE = @{thm CollectE}
1916 val CollectI = @{thm CollectI}
1917 val Collect_conj_eq = @{thm Collect_conj_eq}
1918 val Collect_mem_eq = @{thm Collect_mem_eq}
1919 val IntD1 = @{thm IntD1}
1920 val IntD2 = @{thm IntD2}
1921 val IntE = @{thm IntE}
1922 val IntI = @{thm IntI}
1923 val Int_Collect = @{thm Int_Collect}
1924 val UNIV_I = @{thm UNIV_I}
1925 val UNIV_witness = @{thm UNIV_witness}
1926 val UnE = @{thm UnE}
1927 val UnI1 = @{thm UnI1}
1928 val UnI2 = @{thm UnI2}
1929 val ballE = @{thm ballE}
1930 val ballI = @{thm ballI}
1931 val bexCI = @{thm bexCI}
1932 val bexE = @{thm bexE}
1933 val bexI = @{thm bexI}
1934 val bex_triv = @{thm bex_triv}
1935 val bspec = @{thm bspec}
1936 val contra_subsetD = @{thm contra_subsetD}
1937 val equalityCE = @{thm equalityCE}
1938 val equalityD1 = @{thm equalityD1}
1939 val equalityD2 = @{thm equalityD2}
1940 val equalityE = @{thm equalityE}
1941 val equalityI = @{thm equalityI}
1942 val imageE = @{thm imageE}
1943 val imageI = @{thm imageI}
1944 val image_Un = @{thm image_Un}
1945 val image_insert = @{thm image_insert}
1946 val insert_commute = @{thm insert_commute}
1947 val insert_iff = @{thm insert_iff}
1948 val mem_Collect_eq = @{thm mem_Collect_eq}
1949 val rangeE = @{thm rangeE}
1950 val rangeI = @{thm rangeI}
1951 val range_eqI = @{thm range_eqI}
1952 val subsetCE = @{thm subsetCE}
1953 val subsetD = @{thm subsetD}
1954 val subsetI = @{thm subsetI}
1955 val subset_refl = @{thm subset_refl}
1956 val subset_trans = @{thm subset_trans}
1957 val vimageD = @{thm vimageD}
1958 val vimageE = @{thm vimageE}
1959 val vimageI = @{thm vimageI}
1960 val vimageI2 = @{thm vimageI2}
1961 val vimage_Collect = @{thm vimage_Collect}
1962 val vimage_Int = @{thm vimage_Int}
1963 val vimage_Un = @{thm vimage_Un}
1964 \<close>
1966 end