src/HOL/Set.ML
 author paulson Wed Aug 19 10:27:25 1998 +0200 (1998-08-19) changeset 5336 721bf1a13f1a parent 5318 72bf8039b53f child 5450 fe9d103464a4 permissions -rw-r--r--
1 (*  Title:      HOL/set
2     ID:         \$Id\$
3     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
4     Copyright   1991  University of Cambridge
6 Set theory for higher-order logic.  A set is simply a predicate.
7 *)
9 open Set;
11 section "Relating predicates and sets";
16 Goal "P(a) ==> a : {x. P(x)}";
17 by (Asm_simp_tac 1);
18 qed "CollectI";
20 Goal "a : {x. P(x)} ==> P(a)";
21 by (Asm_full_simp_tac 1);
22 qed "CollectD";
24 val [prem] = Goal "[| !!x. (x:A) = (x:B) |] ==> A = B";
25 by (rtac (prem RS ext RS arg_cong RS box_equals) 1);
26 by (rtac Collect_mem_eq 1);
27 by (rtac Collect_mem_eq 1);
28 qed "set_ext";
30 val [prem] = Goal "[| !!x. P(x)=Q(x) |] ==> {x. P(x)} = {x. Q(x)}";
31 by (rtac (prem RS ext RS arg_cong) 1);
32 qed "Collect_cong";
34 val CollectE = make_elim CollectD;
40 section "Bounded quantifiers";
42 val prems = Goalw [Ball_def]
43     "[| !!x. x:A ==> P(x) |] ==> ! x:A. P(x)";
44 by (REPEAT (ares_tac (prems @ [allI,impI]) 1));
45 qed "ballI";
47 Goalw [Ball_def] "[| ! x:A. P(x);  x:A |] ==> P(x)";
48 by (Blast_tac 1);
49 qed "bspec";
51 val major::prems = Goalw [Ball_def]
52     "[| ! x:A. P(x);  P(x) ==> Q;  x~:A ==> Q |] ==> Q";
53 by (rtac (major RS spec RS impCE) 1);
54 by (REPEAT (eresolve_tac prems 1));
55 qed "ballE";
57 (*Takes assumptions ! x:A.P(x) and a:A; creates assumption P(a)*)
58 fun ball_tac i = etac ballE i THEN contr_tac (i+1);
63 Goalw [Bex_def] "[| P(x);  x:A |] ==> ? x:A. P(x)";
64 by (Blast_tac 1);
65 qed "bexI";
67 qed_goal "bexCI" Set.thy
68    "[| ! x:A. ~P(x) ==> P(a);  a:A |] ==> ? x:A. P(x)"
69  (fn prems=>
70   [ (rtac classical 1),
71     (REPEAT (ares_tac (prems@[bexI,ballI,notI,notE]) 1))  ]);
73 val major::prems = Goalw [Bex_def]
74     "[| ? x:A. P(x);  !!x. [| x:A; P(x) |] ==> Q  |] ==> Q";
75 by (rtac (major RS exE) 1);
76 by (REPEAT (eresolve_tac (prems @ [asm_rl,conjE]) 1));
77 qed "bexE";
82 (*Trival rewrite rule*)
83 Goal "(! x:A. P) = ((? x. x:A) --> P)";
84 by (simp_tac (simpset() addsimps [Ball_def]) 1);
85 qed "ball_triv";
87 (*Dual form for existentials*)
88 Goal "(? x:A. P) = ((? x. x:A) & P)";
89 by (simp_tac (simpset() addsimps [Bex_def]) 1);
90 qed "bex_triv";
94 (** Congruence rules **)
96 val prems = Goal
97     "[| A=B;  !!x. x:B ==> P(x) = Q(x) |] ==> \
98 \    (! x:A. P(x)) = (! x:B. Q(x))";
99 by (resolve_tac (prems RL [ssubst]) 1);
100 by (REPEAT (ares_tac [ballI,iffI] 1
101      ORELSE eresolve_tac ([make_elim bspec, mp] @ (prems RL [iffE])) 1));
102 qed "ball_cong";
104 val prems = Goal
105     "[| A=B;  !!x. x:B ==> P(x) = Q(x) |] ==> \
106 \    (? x:A. P(x)) = (? x:B. Q(x))";
107 by (resolve_tac (prems RL [ssubst]) 1);
108 by (REPEAT (etac bexE 1
109      ORELSE ares_tac ([bexI,iffI] @ (prems RL [iffD1,iffD2])) 1));
110 qed "bex_cong";
112 section "Subsets";
114 val prems = Goalw [subset_def] "(!!x. x:A ==> x:B) ==> A <= B";
115 by (REPEAT (ares_tac (prems @ [ballI]) 1));
116 qed "subsetI";
120 (*While (:) is not, its type must be kept
123 seq (fn a => Blast.overloaded (a, HOLogic.dest_setT o domain_type))
124     ["Ball", "Bex"];
125 (*need UNION, INTER also?*)
127 (*Image: retain the type of the set being expressed*)
130 (*Rule in Modus Ponens style*)
131 Goalw [subset_def] "[| A <= B;  c:A |] ==> c:B";
132 by (Blast_tac 1);
133 qed "subsetD";
135 (*The same, with reversed premises for use with etac -- cf rev_mp*)
136 qed_goal "rev_subsetD" Set.thy "[| c:A;  A <= B |] ==> c:B"
137  (fn prems=>  [ (REPEAT (resolve_tac (prems@[subsetD]) 1)) ]);
139 (*Converts A<=B to x:A ==> x:B*)
140 fun impOfSubs th = th RSN (2, rev_subsetD);
142 qed_goal "contra_subsetD" Set.thy "!!c. [| A <= B; c ~: B |] ==> c ~: A"
143  (fn prems=>  [ (REPEAT (eresolve_tac [asm_rl, contrapos, subsetD] 1)) ]);
145 qed_goal "rev_contra_subsetD" Set.thy "!!c. [| c ~: B;  A <= B |] ==> c ~: A"
146  (fn prems=>  [ (REPEAT (eresolve_tac [asm_rl, contrapos, subsetD] 1)) ]);
148 (*Classical elimination rule*)
149 val major::prems = Goalw [subset_def]
150     "[| A <= B;  c~:A ==> P;  c:B ==> P |] ==> P";
151 by (rtac (major RS ballE) 1);
152 by (REPEAT (eresolve_tac prems 1));
153 qed "subsetCE";
155 (*Takes assumptions A<=B; c:A and creates the assumption c:B *)
156 fun set_mp_tac i = etac subsetCE i  THEN  mp_tac i;
161 qed_goal "subset_refl" Set.thy "A <= (A::'a set)"
162  (fn _=> [Fast_tac 1]);		(*Blast_tac would try order_refl and fail*)
164 Goal "[| A<=B;  B<=C |] ==> A<=(C::'a set)";
165 by (Blast_tac 1);
166 qed "subset_trans";
169 section "Equality";
171 (*Anti-symmetry of the subset relation*)
172 Goal "[| A <= B;  B <= A |] ==> A = (B::'a set)";
173 by (rtac set_ext 1);
174 by (blast_tac (claset() addIs [subsetD]) 1);
175 qed "subset_antisym";
176 val equalityI = subset_antisym;
180 (* Equality rules from ZF set theory -- are they appropriate here? *)
181 Goal "A = B ==> A<=(B::'a set)";
182 by (etac ssubst 1);
183 by (rtac subset_refl 1);
184 qed "equalityD1";
186 Goal "A = B ==> B<=(A::'a set)";
187 by (etac ssubst 1);
188 by (rtac subset_refl 1);
189 qed "equalityD2";
191 val prems = Goal
192     "[| A = B;  [| A<=B; B<=(A::'a set) |] ==> P |]  ==>  P";
193 by (resolve_tac prems 1);
194 by (REPEAT (resolve_tac (prems RL [equalityD1,equalityD2]) 1));
195 qed "equalityE";
197 val major::prems = Goal
198     "[| A = B;  [| c:A; c:B |] ==> P;  [| c~:A; c~:B |] ==> P |]  ==>  P";
199 by (rtac (major RS equalityE) 1);
200 by (REPEAT (contr_tac 1 ORELSE eresolve_tac ([asm_rl,subsetCE]@prems) 1));
201 qed "equalityCE";
203 (*Lemma for creating induction formulae -- for "pattern matching" on p
204   To make the induction hypotheses usable, apply "spec" or "bspec" to
205   put universal quantifiers over the free variables in p. *)
206 val prems = Goal
207     "[| p:A;  !!z. z:A ==> p=z --> R |] ==> R";
208 by (rtac mp 1);
209 by (REPEAT (resolve_tac (refl::prems) 1));
210 qed "setup_induction";
213 section "The universal set -- UNIV";
215 qed_goalw "UNIV_I" Set.thy [UNIV_def] "x : UNIV"
216   (fn _ => [rtac CollectI 1, rtac TrueI 1]);
219 AddIs    [UNIV_I];  (*unsafe makes it less likely to cause problems*)
221 qed_goal "subset_UNIV" Set.thy "A <= UNIV"
222   (fn _ => [rtac subsetI 1, rtac UNIV_I 1]);
224 (** Eta-contracting these two rules (to remove P) causes them to be ignored
225     because of their interaction with congruence rules. **)
227 Goalw [Ball_def] "Ball UNIV P = All P";
228 by (Simp_tac 1);
229 qed "ball_UNIV";
231 Goalw [Bex_def] "Bex UNIV P = Ex P";
232 by (Simp_tac 1);
233 qed "bex_UNIV";
237 section "The empty set -- {}";
239 qed_goalw "empty_iff" Set.thy [empty_def] "(c : {}) = False"
240  (fn _ => [ (Blast_tac 1) ]);
244 qed_goal "emptyE" Set.thy "!!a. a:{} ==> P"
245  (fn _ => [Full_simp_tac 1]);
249 qed_goal "empty_subsetI" Set.thy "{} <= A"
250  (fn _ => [ (Blast_tac 1) ]);
252 (*One effect is to delete the ASSUMPTION {} <= A*)
255 qed_goal "equals0I" Set.thy "[| !!y. y:A ==> False |] ==> A={}"
256  (fn [prem]=>
257   [ (blast_tac (claset() addIs [prem RS FalseE]) 1) ]);
259 (*Use for reasoning about disjointness: A Int B = {} *)
260 qed_goal "equals0E" Set.thy "!!a. [| A={};  a:A |] ==> P"
261  (fn _ => [ (Blast_tac 1) ]);
263 AddEs [equals0E, sym RS equals0E];
265 Goalw [Ball_def] "Ball {} P = True";
266 by (Simp_tac 1);
267 qed "ball_empty";
269 Goalw [Bex_def] "Bex {} P = False";
270 by (Simp_tac 1);
271 qed "bex_empty";
274 Goal "UNIV ~= {}";
275 by (blast_tac (claset() addEs [equalityE]) 1);
276 qed "UNIV_not_empty";
281 section "The Powerset operator -- Pow";
283 qed_goalw "Pow_iff" Set.thy [Pow_def] "(A : Pow(B)) = (A <= B)"
284  (fn _ => [ (Asm_simp_tac 1) ]);
288 qed_goalw "PowI" Set.thy [Pow_def] "!!A B. A <= B ==> A : Pow(B)"
289  (fn _ => [ (etac CollectI 1) ]);
291 qed_goalw "PowD" Set.thy [Pow_def] "!!A B. A : Pow(B)  ==>  A<=B"
292  (fn _=> [ (etac CollectD 1) ]);
294 val Pow_bottom = empty_subsetI RS PowI;        (* {}: Pow(B) *)
295 val Pow_top = subset_refl RS PowI;             (* A : Pow(A) *)
298 section "Set complement -- Compl";
300 qed_goalw "Compl_iff" Set.thy [Compl_def] "(c : Compl(A)) = (c~:A)"
301  (fn _ => [ (Blast_tac 1) ]);
305 val prems = Goalw [Compl_def] "[| c:A ==> False |] ==> c : Compl(A)";
306 by (REPEAT (ares_tac (prems @ [CollectI,notI]) 1));
307 qed "ComplI";
309 (*This form, with negated conclusion, works well with the Classical prover.
310   Negated assumptions behave like formulae on the right side of the notional
311   turnstile...*)
312 Goalw [Compl_def] "c : Compl(A) ==> c~:A";
313 by (etac CollectD 1);
314 qed "ComplD";
316 val ComplE = make_elim ComplD;
322 section "Binary union -- Un";
324 qed_goalw "Un_iff" Set.thy [Un_def] "(c : A Un B) = (c:A | c:B)"
325  (fn _ => [ Blast_tac 1 ]);
329 Goal "c:A ==> c : A Un B";
330 by (Asm_simp_tac 1);
331 qed "UnI1";
333 Goal "c:B ==> c : A Un B";
334 by (Asm_simp_tac 1);
335 qed "UnI2";
337 (*Classical introduction rule: no commitment to A vs B*)
338 qed_goal "UnCI" Set.thy "(c~:B ==> c:A) ==> c : A Un B"
339  (fn prems=>
340   [ (Simp_tac 1),
341     (REPEAT (ares_tac (prems@[disjCI]) 1)) ]);
343 val major::prems = Goalw [Un_def]
344     "[| c : A Un B;  c:A ==> P;  c:B ==> P |] ==> P";
345 by (rtac (major RS CollectD RS disjE) 1);
346 by (REPEAT (eresolve_tac prems 1));
347 qed "UnE";
353 section "Binary intersection -- Int";
355 qed_goalw "Int_iff" Set.thy [Int_def] "(c : A Int B) = (c:A & c:B)"
356  (fn _ => [ (Blast_tac 1) ]);
360 Goal "[| c:A;  c:B |] ==> c : A Int B";
361 by (Asm_simp_tac 1);
362 qed "IntI";
364 Goal "c : A Int B ==> c:A";
365 by (Asm_full_simp_tac 1);
366 qed "IntD1";
368 Goal "c : A Int B ==> c:B";
369 by (Asm_full_simp_tac 1);
370 qed "IntD2";
372 val [major,minor] = Goal
373     "[| c : A Int B;  [| c:A; c:B |] ==> P |] ==> P";
374 by (rtac minor 1);
375 by (rtac (major RS IntD1) 1);
376 by (rtac (major RS IntD2) 1);
377 qed "IntE";
382 section "Set difference";
384 qed_goalw "Diff_iff" Set.thy [set_diff_def] "(c : A-B) = (c:A & c~:B)"
385  (fn _ => [ (Blast_tac 1) ]);
389 qed_goal "DiffI" Set.thy "!!c. [| c : A;  c ~: B |] ==> c : A - B"
390  (fn _=> [ Asm_simp_tac 1 ]);
392 qed_goal "DiffD1" Set.thy "!!c. c : A - B ==> c : A"
393  (fn _=> [ (Asm_full_simp_tac 1) ]);
395 qed_goal "DiffD2" Set.thy "!!c. [| c : A - B;  c : B |] ==> P"
396  (fn _=> [ (Asm_full_simp_tac 1) ]);
398 qed_goal "DiffE" Set.thy "[| c : A - B;  [| c:A; c~:B |] ==> P |] ==> P"
399  (fn prems=>
400   [ (resolve_tac prems 1),
401     (REPEAT (ares_tac (prems RL [DiffD1, DiffD2 RS notI]) 1)) ]);
407 section "Augmenting a set -- insert";
409 qed_goalw "insert_iff" Set.thy [insert_def] "a : insert b A = (a=b | a:A)"
410  (fn _ => [Blast_tac 1]);
414 qed_goal "insertI1" Set.thy "a : insert a B"
415  (fn _ => [Simp_tac 1]);
417 qed_goal "insertI2" Set.thy "!!a. a : B ==> a : insert b B"
418  (fn _=> [Asm_simp_tac 1]);
420 qed_goalw "insertE" Set.thy [insert_def]
421     "[| a : insert b A;  a=b ==> P;  a:A ==> P |] ==> P"
422  (fn major::prems=>
423   [ (rtac (major RS UnE) 1),
424     (REPEAT (eresolve_tac (prems @ [CollectE]) 1)) ]);
426 (*Classical introduction rule*)
427 qed_goal "insertCI" Set.thy "(a~:B ==> a=b) ==> a: insert b B"
428  (fn prems=>
429   [ (Simp_tac 1),
430     (REPEAT (ares_tac (prems@[disjCI]) 1)) ]);
435 section "Singletons, using insert";
437 qed_goal "singletonI" Set.thy "a : {a}"
438  (fn _=> [ (rtac insertI1 1) ]);
440 Goal "b : {a} ==> b=a";
441 by (Blast_tac 1);
442 qed "singletonD";
444 bind_thm ("singletonE", make_elim singletonD);
446 qed_goal "singleton_iff" thy "(b : {a}) = (b=a)"
447 (fn _ => [Blast_tac 1]);
449 Goal "{a}={b} ==> a=b";
450 by (blast_tac (claset() addEs [equalityE]) 1);
451 qed "singleton_inject";
453 (*Redundant? But unlike insertCI, it proves the subgoal immediately!*)
458 Goal "{x. x=a} = {a}";
459 by (Blast_tac 1);
460 qed "singleton_conv";
464 section "Unions of families -- UNION x:A. B(x) is Union(B``A)";
466 Goalw [UNION_def] "(b: (UN x:A. B(x))) = (EX x:A. b: B(x))";
467 by (Blast_tac 1);
468 qed "UN_iff";
472 (*The order of the premises presupposes that A is rigid; b may be flexible*)
473 Goal "[| a:A;  b: B(a) |] ==> b: (UN x:A. B(x))";
474 by Auto_tac;
475 qed "UN_I";
477 val major::prems = Goalw [UNION_def]
478     "[| b : (UN x:A. B(x));  !!x.[| x:A;  b: B(x) |] ==> R |] ==> R";
479 by (rtac (major RS CollectD RS bexE) 1);
480 by (REPEAT (ares_tac prems 1));
481 qed "UN_E";
486 val prems = Goal
487     "[| A=B;  !!x. x:B ==> C(x) = D(x) |] ==> \
488 \    (UN x:A. C(x)) = (UN x:B. D(x))";
489 by (REPEAT (etac UN_E 1
490      ORELSE ares_tac ([UN_I,equalityI,subsetI] @
491                       (prems RL [equalityD1,equalityD2] RL [subsetD])) 1));
492 qed "UN_cong";
495 section "Intersections of families -- INTER x:A. B(x) is Inter(B``A)";
497 Goalw [INTER_def] "(b: (INT x:A. B(x))) = (ALL x:A. b: B(x))";
498 by Auto_tac;
499 qed "INT_iff";
503 val prems = Goalw [INTER_def]
504     "(!!x. x:A ==> b: B(x)) ==> b : (INT x:A. B(x))";
505 by (REPEAT (ares_tac ([CollectI,ballI] @ prems) 1));
506 qed "INT_I";
508 Goal "[| b : (INT x:A. B(x));  a:A |] ==> b: B(a)";
509 by Auto_tac;
510 qed "INT_D";
512 (*"Classical" elimination -- by the Excluded Middle on a:A *)
513 val major::prems = Goalw [INTER_def]
514     "[| b : (INT x:A. B(x));  b: B(a) ==> R;  a~:A ==> R |] ==> R";
515 by (rtac (major RS CollectD RS ballE) 1);
516 by (REPEAT (eresolve_tac prems 1));
517 qed "INT_E";
522 val prems = Goal
523     "[| A=B;  !!x. x:B ==> C(x) = D(x) |] ==> \
524 \    (INT x:A. C(x)) = (INT x:B. D(x))";
525 by (REPEAT_FIRST (resolve_tac [INT_I,equalityI,subsetI]));
526 by (REPEAT (dtac INT_D 1
527      ORELSE ares_tac (prems RL [equalityD1,equalityD2] RL [subsetD]) 1));
528 qed "INT_cong";
531 section "Union";
533 Goalw [Union_def] "(A : Union(C)) = (EX X:C. A:X)";
534 by (Blast_tac 1);
535 qed "Union_iff";
539 (*The order of the premises presupposes that C is rigid; A may be flexible*)
540 Goal "[| X:C;  A:X |] ==> A : Union(C)";
541 by Auto_tac;
542 qed "UnionI";
544 val major::prems = Goalw [Union_def]
545     "[| A : Union(C);  !!X.[| A:X;  X:C |] ==> R |] ==> R";
546 by (rtac (major RS UN_E) 1);
547 by (REPEAT (ares_tac prems 1));
548 qed "UnionE";
554 section "Inter";
556 Goalw [Inter_def] "(A : Inter(C)) = (ALL X:C. A:X)";
557 by (Blast_tac 1);
558 qed "Inter_iff";
562 val prems = Goalw [Inter_def]
563     "[| !!X. X:C ==> A:X |] ==> A : Inter(C)";
564 by (REPEAT (ares_tac ([INT_I] @ prems) 1));
565 qed "InterI";
567 (*A "destruct" rule -- every X in C contains A as an element, but
568   A:X can hold when X:C does not!  This rule is analogous to "spec". *)
569 Goal "[| A : Inter(C);  X:C |] ==> A:X";
570 by Auto_tac;
571 qed "InterD";
573 (*"Classical" elimination rule -- does not require proving X:C *)
574 val major::prems = Goalw [Inter_def]
575     "[| A : Inter(C);  X~:C ==> R;  A:X ==> R |] ==> R";
576 by (rtac (major RS INT_E) 1);
577 by (REPEAT (eresolve_tac prems 1));
578 qed "InterE";
584 (*** Image of a set under a function ***)
586 (*Frequently b does not have the syntactic form of f(x).*)
587 Goalw [image_def] "[| b=f(x);  x:A |] ==> b : f``A";
588 by (Blast_tac 1);
589 qed "image_eqI";
592 bind_thm ("imageI", refl RS image_eqI);
594 (*The eta-expansion gives variable-name preservation.*)
595 val major::prems = Goalw [image_def]
596     "[| b : (%x. f(x))``A;  !!x.[| b=f(x);  x:A |] ==> P |] ==> P";
597 by (rtac (major RS CollectD RS bexE) 1);
598 by (REPEAT (ares_tac prems 1));
599 qed "imageE";
604 Goal "f``(A Un B) = f``A Un f``B";
605 by (Blast_tac 1);
606 qed "image_Un";
608 Goal "(z : f``A) = (EX x:A. z = f x)";
609 by (Blast_tac 1);
610 qed "image_iff";
612 (*This rewrite rule would confuse users if made default.*)
613 Goal "(f``A <= B) = (ALL x:A. f(x): B)";
614 by (Blast_tac 1);
615 qed "image_subset_iff";
617 (*Replaces the three steps subsetI, imageE, hyp_subst_tac, but breaks too
618   many existing proofs.*)
619 val prems = Goal "(!!x. x:A ==> f(x) : B) ==> f``A <= B";
620 by (blast_tac (claset() addIs prems) 1);
621 qed "image_subsetI";
624 (*** Range of a function -- just a translation for image! ***)
626 Goal "b=f(x) ==> b : range(f)";
627 by (EVERY1 [etac image_eqI, rtac UNIV_I]);
628 bind_thm ("range_eqI", UNIV_I RSN (2,image_eqI));
630 bind_thm ("rangeI", UNIV_I RS imageI);
632 val [major,minor] = Goal
633     "[| b : range(%x. f(x));  !!x. b=f(x) ==> P |] ==> P";
634 by (rtac (major RS imageE) 1);
635 by (etac minor 1);
636 qed "rangeE";
639 (*** Set reasoning tools ***)
642 (** Rewrite rules for boolean case-splitting: faster than
644 **)
646 bind_thm ("split_if_eq1", read_instantiate [("P", "%x. x = ?b")] split_if);
647 bind_thm ("split_if_eq2", read_instantiate [("P", "%x. ?a = x")] split_if);
649 (*Split ifs on either side of the membership relation.
650 	Not for Addsimps -- can cause goals to blow up!*)
651 bind_thm ("split_if_mem1",
652     read_instantiate_sg (sign_of Set.thy) [("P", "%x. x : ?b")] split_if);
653 bind_thm ("split_if_mem2",
654     read_instantiate_sg (sign_of Set.thy) [("P", "%x. ?a : x")] split_if);
656 val split_ifs = [if_bool_eq_conj, split_if_eq1, split_if_eq2,
657 		  split_if_mem1, split_if_mem2];
661 val mem_simps = [insert_iff, empty_iff, Un_iff, Int_iff, Compl_iff, Diff_iff,
662                  mem_Collect_eq, UN_iff, Union_iff, INT_iff, Inter_iff];
664 val mksimps_pairs = ("Ball",[bspec]) :: mksimps_pairs;
666 simpset_ref() := simpset() addcongs [ball_cong,bex_cong]
667                     setmksimps (mksimps mksimps_pairs);