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