1465

1 
(* Title: HOL/set

923

2 
ID: $Id$

1465

3 
Author: Lawrence C Paulson, Cambridge University Computer Laboratory

923

4 
Copyright 1991 University of Cambridge


5 


6 
For set.thy. Set theory for higherorder logic. A set is simply a predicate.


7 
*)


8 


9 
open Set;


10 

1548

11 
section "Relating predicates and sets";


12 


13 
val [prem] = goal Set.thy "P(a) ==> a : {x.P(x)}";

923

14 
by (rtac (mem_Collect_eq RS ssubst) 1);


15 
by (rtac prem 1);


16 
qed "CollectI";


17 


18 
val prems = goal Set.thy "[ a : {x.P(x)} ] ==> P(a)";


19 
by (resolve_tac (prems RL [mem_Collect_eq RS subst]) 1);


20 
qed "CollectD";


21 


22 
val [prem] = goal Set.thy "[ !!x. (x:A) = (x:B) ] ==> A = B";


23 
by (rtac (prem RS ext RS arg_cong RS box_equals) 1);


24 
by (rtac Collect_mem_eq 1);


25 
by (rtac Collect_mem_eq 1);


26 
qed "set_ext";


27 


28 
val [prem] = goal Set.thy "[ !!x. P(x)=Q(x) ] ==> {x. P(x)} = {x. Q(x)}";


29 
by (rtac (prem RS ext RS arg_cong) 1);


30 
qed "Collect_cong";


31 


32 
val CollectE = make_elim CollectD;


33 

1548

34 
section "Bounded quantifiers";

923

35 


36 
val prems = goalw Set.thy [Ball_def]


37 
"[ !!x. x:A ==> P(x) ] ==> ! x:A. P(x)";


38 
by (REPEAT (ares_tac (prems @ [allI,impI]) 1));


39 
qed "ballI";


40 


41 
val [major,minor] = goalw Set.thy [Ball_def]


42 
"[ ! x:A. P(x); x:A ] ==> P(x)";


43 
by (rtac (minor RS (major RS spec RS mp)) 1);


44 
qed "bspec";


45 


46 
val major::prems = goalw Set.thy [Ball_def]


47 
"[ ! x:A. P(x); P(x) ==> Q; x~:A ==> Q ] ==> Q";


48 
by (rtac (major RS spec RS impCE) 1);


49 
by (REPEAT (eresolve_tac prems 1));


50 
qed "ballE";


51 


52 
(*Takes assumptions ! x:A.P(x) and a:A; creates assumption P(a)*)


53 
fun ball_tac i = etac ballE i THEN contr_tac (i+1);


54 


55 
val prems = goalw Set.thy [Bex_def]


56 
"[ P(x); x:A ] ==> ? x:A. P(x)";


57 
by (REPEAT (ares_tac (prems @ [exI,conjI]) 1));


58 
qed "bexI";


59 


60 
qed_goal "bexCI" Set.thy


61 
"[ ! x:A. ~P(x) ==> P(a); a:A ] ==> ? x:A.P(x)"


62 
(fn prems=>


63 
[ (rtac classical 1),


64 
(REPEAT (ares_tac (prems@[bexI,ballI,notI,notE]) 1)) ]);


65 


66 
val major::prems = goalw Set.thy [Bex_def]


67 
"[ ? x:A. P(x); !!x. [ x:A; P(x) ] ==> Q ] ==> Q";


68 
by (rtac (major RS exE) 1);


69 
by (REPEAT (eresolve_tac (prems @ [asm_rl,conjE]) 1));


70 
qed "bexE";


71 


72 
(*Trival rewrite rule; (! x:A.P)=P holds only if A is nonempty!*)


73 
val prems = goal Set.thy


74 
"(! x:A. True) = True";


75 
by (REPEAT (ares_tac [TrueI,ballI,iffI] 1));


76 
qed "ball_rew";


77 


78 
(** Congruence rules **)


79 


80 
val prems = goal Set.thy


81 
"[ A=B; !!x. x:B ==> P(x) = Q(x) ] ==> \


82 
\ (! x:A. P(x)) = (! x:B. Q(x))";


83 
by (resolve_tac (prems RL [ssubst]) 1);


84 
by (REPEAT (ares_tac [ballI,iffI] 1


85 
ORELSE eresolve_tac ([make_elim bspec, mp] @ (prems RL [iffE])) 1));


86 
qed "ball_cong";


87 


88 
val prems = goal Set.thy


89 
"[ A=B; !!x. x:B ==> P(x) = Q(x) ] ==> \


90 
\ (? x:A. P(x)) = (? x:B. Q(x))";


91 
by (resolve_tac (prems RL [ssubst]) 1);


92 
by (REPEAT (etac bexE 1


93 
ORELSE ares_tac ([bexI,iffI] @ (prems RL [iffD1,iffD2])) 1));


94 
qed "bex_cong";


95 

1548

96 
section "Subsets";

923

97 


98 
val prems = goalw Set.thy [subset_def] "(!!x.x:A ==> x:B) ==> A <= B";


99 
by (REPEAT (ares_tac (prems @ [ballI]) 1));


100 
qed "subsetI";


101 


102 
(*Rule in Modus Ponens style*)


103 
val major::prems = goalw Set.thy [subset_def] "[ A <= B; c:A ] ==> c:B";


104 
by (rtac (major RS bspec) 1);


105 
by (resolve_tac prems 1);


106 
qed "subsetD";


107 


108 
(*The same, with reversed premises for use with etac  cf rev_mp*)


109 
qed_goal "rev_subsetD" Set.thy "[ c:A; A <= B ] ==> c:B"


110 
(fn prems=> [ (REPEAT (resolve_tac (prems@[subsetD]) 1)) ]);


111 


112 
(*Classical elimination rule*)


113 
val major::prems = goalw Set.thy [subset_def]


114 
"[ A <= B; c~:A ==> P; c:B ==> P ] ==> P";


115 
by (rtac (major RS ballE) 1);


116 
by (REPEAT (eresolve_tac prems 1));


117 
qed "subsetCE";


118 


119 
(*Takes assumptions A<=B; c:A and creates the assumption c:B *)


120 
fun set_mp_tac i = etac subsetCE i THEN mp_tac i;


121 


122 
qed_goal "subset_refl" Set.thy "A <= (A::'a set)"


123 
(fn _=> [ (REPEAT (ares_tac [subsetI] 1)) ]);


124 


125 
val prems = goal Set.thy "[ A<=B; B<=C ] ==> A<=(C::'a set)";


126 
by (cut_facts_tac prems 1);


127 
by (REPEAT (ares_tac [subsetI] 1 ORELSE set_mp_tac 1));


128 
qed "subset_trans";


129 


130 

1548

131 
section "Equality";

923

132 


133 
(*Antisymmetry of the subset relation*)


134 
val prems = goal Set.thy "[ A <= B; B <= A ] ==> A = (B::'a set)";


135 
by (rtac (iffI RS set_ext) 1);


136 
by (REPEAT (ares_tac (prems RL [subsetD]) 1));


137 
qed "subset_antisym";


138 
val equalityI = subset_antisym;


139 


140 
(* Equality rules from ZF set theory  are they appropriate here? *)


141 
val prems = goal Set.thy "A = B ==> A<=(B::'a set)";


142 
by (resolve_tac (prems RL [subst]) 1);


143 
by (rtac subset_refl 1);


144 
qed "equalityD1";


145 


146 
val prems = goal Set.thy "A = B ==> B<=(A::'a set)";


147 
by (resolve_tac (prems RL [subst]) 1);


148 
by (rtac subset_refl 1);


149 
qed "equalityD2";


150 


151 
val prems = goal Set.thy


152 
"[ A = B; [ A<=B; B<=(A::'a set) ] ==> P ] ==> P";


153 
by (resolve_tac prems 1);


154 
by (REPEAT (resolve_tac (prems RL [equalityD1,equalityD2]) 1));


155 
qed "equalityE";


156 


157 
val major::prems = goal Set.thy


158 
"[ A = B; [ c:A; c:B ] ==> P; [ c~:A; c~:B ] ==> P ] ==> P";


159 
by (rtac (major RS equalityE) 1);


160 
by (REPEAT (contr_tac 1 ORELSE eresolve_tac ([asm_rl,subsetCE]@prems) 1));


161 
qed "equalityCE";


162 


163 
(*Lemma for creating induction formulae  for "pattern matching" on p


164 
To make the induction hypotheses usable, apply "spec" or "bspec" to


165 
put universal quantifiers over the free variables in p. *)


166 
val prems = goal Set.thy


167 
"[ p:A; !!z. z:A ==> p=z > R ] ==> R";


168 
by (rtac mp 1);


169 
by (REPEAT (resolve_tac (refl::prems) 1));


170 
qed "setup_induction";


171 


172 

1548

173 
section "Set complement  Compl";

923

174 


175 
val prems = goalw Set.thy [Compl_def]


176 
"[ c:A ==> False ] ==> c : Compl(A)";


177 
by (REPEAT (ares_tac (prems @ [CollectI,notI]) 1));


178 
qed "ComplI";


179 


180 
(*This form, with negated conclusion, works well with the Classical prover.


181 
Negated assumptions behave like formulae on the right side of the notional


182 
turnstile...*)


183 
val major::prems = goalw Set.thy [Compl_def]


184 
"[ c : Compl(A) ] ==> c~:A";


185 
by (rtac (major RS CollectD) 1);


186 
qed "ComplD";


187 


188 
val ComplE = make_elim ComplD;


189 


190 

1548

191 
section "Binary union  Un";

923

192 


193 
val prems = goalw Set.thy [Un_def] "c:A ==> c : A Un B";


194 
by (REPEAT (resolve_tac (prems @ [CollectI,disjI1]) 1));


195 
qed "UnI1";


196 


197 
val prems = goalw Set.thy [Un_def] "c:B ==> c : A Un B";


198 
by (REPEAT (resolve_tac (prems @ [CollectI,disjI2]) 1));


199 
qed "UnI2";


200 


201 
(*Classical introduction rule: no commitment to A vs B*)


202 
qed_goal "UnCI" Set.thy "(c~:B ==> c:A) ==> c : A Un B"


203 
(fn prems=>


204 
[ (rtac classical 1),


205 
(REPEAT (ares_tac (prems@[UnI1,notI]) 1)),


206 
(REPEAT (ares_tac (prems@[UnI2,notE]) 1)) ]);


207 


208 
val major::prems = goalw Set.thy [Un_def]


209 
"[ c : A Un B; c:A ==> P; c:B ==> P ] ==> P";


210 
by (rtac (major RS CollectD RS disjE) 1);


211 
by (REPEAT (eresolve_tac prems 1));


212 
qed "UnE";


213 


214 

1548

215 
section "Binary intersection  Int";

923

216 


217 
val prems = goalw Set.thy [Int_def]


218 
"[ c:A; c:B ] ==> c : A Int B";


219 
by (REPEAT (resolve_tac (prems @ [CollectI,conjI]) 1));


220 
qed "IntI";


221 


222 
val [major] = goalw Set.thy [Int_def] "c : A Int B ==> c:A";


223 
by (rtac (major RS CollectD RS conjunct1) 1);


224 
qed "IntD1";


225 


226 
val [major] = goalw Set.thy [Int_def] "c : A Int B ==> c:B";


227 
by (rtac (major RS CollectD RS conjunct2) 1);


228 
qed "IntD2";


229 


230 
val [major,minor] = goal Set.thy


231 
"[ c : A Int B; [ c:A; c:B ] ==> P ] ==> P";


232 
by (rtac minor 1);


233 
by (rtac (major RS IntD1) 1);


234 
by (rtac (major RS IntD2) 1);


235 
qed "IntE";


236 


237 

1548

238 
section "Set difference";

923

239 


240 
qed_goalw "DiffI" Set.thy [set_diff_def]


241 
"[ c : A; c ~: B ] ==> c : A  B"


242 
(fn prems=> [ (REPEAT (resolve_tac (prems @ [CollectI,conjI]) 1)) ]);


243 


244 
qed_goalw "DiffD1" Set.thy [set_diff_def]


245 
"c : A  B ==> c : A"


246 
(fn [major]=> [ (rtac (major RS CollectD RS conjunct1) 1) ]);


247 


248 
qed_goalw "DiffD2" Set.thy [set_diff_def]


249 
"[ c : A  B; c : B ] ==> P"


250 
(fn [major,minor]=>


251 
[rtac (minor RS (major RS CollectD RS conjunct2 RS notE)) 1]);


252 


253 
qed_goal "DiffE" Set.thy


254 
"[ c : A  B; [ c:A; c~:B ] ==> P ] ==> P"


255 
(fn prems=>


256 
[ (resolve_tac prems 1),


257 
(REPEAT (ares_tac (prems RL [DiffD1, DiffD2 RS notI]) 1)) ]);


258 


259 
qed_goal "Diff_iff" Set.thy "(c : AB) = (c:A & c~:B)"


260 
(fn _ => [ (fast_tac (HOL_cs addSIs [DiffI] addSEs [DiffE]) 1) ]);


261 

1548

262 
section "The empty set  {}";

923

263 


264 
qed_goalw "emptyE" Set.thy [empty_def] "a:{} ==> P"


265 
(fn [prem] => [rtac (prem RS CollectD RS FalseE) 1]);


266 


267 
qed_goal "empty_subsetI" Set.thy "{} <= A"


268 
(fn _ => [ (REPEAT (ares_tac [equalityI,subsetI,emptyE] 1)) ]);


269 


270 
qed_goal "equals0I" Set.thy "[ !!y. y:A ==> False ] ==> A={}"


271 
(fn prems=>


272 
[ (REPEAT (ares_tac (prems@[empty_subsetI,subsetI,equalityI]) 1


273 
ORELSE eresolve_tac (prems RL [FalseE]) 1)) ]);


274 


275 
qed_goal "equals0D" Set.thy "[ A={}; a:A ] ==> P"


276 
(fn [major,minor]=>


277 
[ (rtac (minor RS (major RS equalityD1 RS subsetD RS emptyE)) 1) ]);


278 


279 

1548

280 
section "Augmenting a set  insert";

923

281 


282 
qed_goalw "insertI1" Set.thy [insert_def] "a : insert a B"


283 
(fn _ => [rtac (CollectI RS UnI1) 1, rtac refl 1]);


284 


285 
qed_goalw "insertI2" Set.thy [insert_def] "a : B ==> a : insert b B"


286 
(fn [prem]=> [ (rtac (prem RS UnI2) 1) ]);


287 


288 
qed_goalw "insertE" Set.thy [insert_def]


289 
"[ a : insert b A; a=b ==> P; a:A ==> P ] ==> P"


290 
(fn major::prems=>


291 
[ (rtac (major RS UnE) 1),


292 
(REPEAT (eresolve_tac (prems @ [CollectE]) 1)) ]);


293 


294 
qed_goal "insert_iff" Set.thy "a : insert b A = (a=b  a:A)"


295 
(fn _ => [fast_tac (HOL_cs addIs [insertI1,insertI2] addSEs [insertE]) 1]);


296 


297 
(*Classical introduction rule*)


298 
qed_goal "insertCI" Set.thy "(a~:B ==> a=b) ==> a: insert b B"


299 
(fn [prem]=>


300 
[ (rtac (disjCI RS (insert_iff RS iffD2)) 1),


301 
(etac prem 1) ]);


302 

1548

303 
section "Singletons, using insert";

923

304 


305 
qed_goal "singletonI" Set.thy "a : {a}"


306 
(fn _=> [ (rtac insertI1 1) ]);


307 


308 
qed_goal "singletonE" Set.thy "[ a: {b}; a=b ==> P ] ==> P"


309 
(fn major::prems=>


310 
[ (rtac (major RS insertE) 1),


311 
(REPEAT (eresolve_tac (prems @ [emptyE]) 1)) ]);


312 


313 
goalw Set.thy [insert_def] "!!a. b : {a} ==> b=a";


314 
by(fast_tac (HOL_cs addSEs [emptyE,CollectE,UnE]) 1);


315 
qed "singletonD";


316 


317 
val singletonE = make_elim singletonD;


318 


319 
val [major] = goal Set.thy "{a}={b} ==> a=b";


320 
by (rtac (major RS equalityD1 RS subsetD RS singletonD) 1);


321 
by (rtac singletonI 1);


322 
qed "singleton_inject";


323 

1531

324 

1548

325 
section "The universal set  UNIV";

1531

326 


327 
qed_goal "subset_UNIV" Set.thy "A <= UNIV"


328 
(fn _ => [rtac subsetI 1, rtac ComplI 1, etac emptyE 1]);


329 


330 

1548

331 
section "Unions of families  UNION x:A. B(x) is Union(B``A)";

923

332 


333 
(*The order of the premises presupposes that A is rigid; b may be flexible*)


334 
val prems = goalw Set.thy [UNION_def]


335 
"[ a:A; b: B(a) ] ==> b: (UN x:A. B(x))";


336 
by (REPEAT (resolve_tac (prems @ [bexI,CollectI]) 1));


337 
qed "UN_I";


338 


339 
val major::prems = goalw Set.thy [UNION_def]


340 
"[ b : (UN x:A. B(x)); !!x.[ x:A; b: B(x) ] ==> R ] ==> R";


341 
by (rtac (major RS CollectD RS bexE) 1);


342 
by (REPEAT (ares_tac prems 1));


343 
qed "UN_E";


344 


345 
val prems = goal Set.thy


346 
"[ A=B; !!x. x:B ==> C(x) = D(x) ] ==> \


347 
\ (UN x:A. C(x)) = (UN x:B. D(x))";


348 
by (REPEAT (etac UN_E 1


349 
ORELSE ares_tac ([UN_I,equalityI,subsetI] @

1465

350 
(prems RL [equalityD1,equalityD2] RL [subsetD])) 1));

923

351 
qed "UN_cong";


352 


353 

1548

354 
section "Intersections of families  INTER x:A. B(x) is Inter(B``A)";

923

355 


356 
val prems = goalw Set.thy [INTER_def]


357 
"(!!x. x:A ==> b: B(x)) ==> b : (INT x:A. B(x))";


358 
by (REPEAT (ares_tac ([CollectI,ballI] @ prems) 1));


359 
qed "INT_I";


360 


361 
val major::prems = goalw Set.thy [INTER_def]


362 
"[ b : (INT x:A. B(x)); a:A ] ==> b: B(a)";


363 
by (rtac (major RS CollectD RS bspec) 1);


364 
by (resolve_tac prems 1);


365 
qed "INT_D";


366 


367 
(*"Classical" elimination  by the Excluded Middle on a:A *)


368 
val major::prems = goalw Set.thy [INTER_def]


369 
"[ b : (INT x:A. B(x)); b: B(a) ==> R; a~:A ==> R ] ==> R";


370 
by (rtac (major RS CollectD RS ballE) 1);


371 
by (REPEAT (eresolve_tac prems 1));


372 
qed "INT_E";


373 


374 
val prems = goal Set.thy


375 
"[ A=B; !!x. x:B ==> C(x) = D(x) ] ==> \


376 
\ (INT x:A. C(x)) = (INT x:B. D(x))";


377 
by (REPEAT_FIRST (resolve_tac [INT_I,equalityI,subsetI]));


378 
by (REPEAT (dtac INT_D 1


379 
ORELSE ares_tac (prems RL [equalityD1,equalityD2] RL [subsetD]) 1));


380 
qed "INT_cong";


381 


382 

1548

383 
section "Unions over a type; UNION1(B) = Union(range(B))";

923

384 


385 
(*The order of the premises presupposes that A is rigid; b may be flexible*)


386 
val prems = goalw Set.thy [UNION1_def]


387 
"b: B(x) ==> b: (UN x. B(x))";


388 
by (REPEAT (resolve_tac (prems @ [TrueI, CollectI RS UN_I]) 1));


389 
qed "UN1_I";


390 


391 
val major::prems = goalw Set.thy [UNION1_def]


392 
"[ b : (UN x. B(x)); !!x. b: B(x) ==> R ] ==> R";


393 
by (rtac (major RS UN_E) 1);


394 
by (REPEAT (ares_tac prems 1));


395 
qed "UN1_E";


396 


397 

1548

398 
section "Intersections over a type; INTER1(B) = Inter(range(B))";

923

399 


400 
val prems = goalw Set.thy [INTER1_def]


401 
"(!!x. b: B(x)) ==> b : (INT x. B(x))";


402 
by (REPEAT (ares_tac (INT_I::prems) 1));


403 
qed "INT1_I";


404 


405 
val [major] = goalw Set.thy [INTER1_def]


406 
"b : (INT x. B(x)) ==> b: B(a)";


407 
by (rtac (TrueI RS (CollectI RS (major RS INT_D))) 1);


408 
qed "INT1_D";


409 

1548

410 
section "Union";

923

411 


412 
(*The order of the premises presupposes that C is rigid; A may be flexible*)


413 
val prems = goalw Set.thy [Union_def]


414 
"[ X:C; A:X ] ==> A : Union(C)";


415 
by (REPEAT (resolve_tac (prems @ [UN_I]) 1));


416 
qed "UnionI";


417 


418 
val major::prems = goalw Set.thy [Union_def]


419 
"[ A : Union(C); !!X.[ A:X; X:C ] ==> R ] ==> R";


420 
by (rtac (major RS UN_E) 1);


421 
by (REPEAT (ares_tac prems 1));


422 
qed "UnionE";


423 

1548

424 
section "Inter";

923

425 


426 
val prems = goalw Set.thy [Inter_def]


427 
"[ !!X. X:C ==> A:X ] ==> A : Inter(C)";


428 
by (REPEAT (ares_tac ([INT_I] @ prems) 1));


429 
qed "InterI";


430 


431 
(*A "destruct" rule  every X in C contains A as an element, but


432 
A:X can hold when X:C does not! This rule is analogous to "spec". *)


433 
val major::prems = goalw Set.thy [Inter_def]


434 
"[ A : Inter(C); X:C ] ==> A:X";


435 
by (rtac (major RS INT_D) 1);


436 
by (resolve_tac prems 1);


437 
qed "InterD";


438 


439 
(*"Classical" elimination rule  does not require proving X:C *)


440 
val major::prems = goalw Set.thy [Inter_def]


441 
"[ A : Inter(C); A:X ==> R; X~:C ==> R ] ==> R";


442 
by (rtac (major RS INT_E) 1);


443 
by (REPEAT (eresolve_tac prems 1));


444 
qed "InterE";


445 

1548

446 
section "The Powerset operator  Pow";

923

447 


448 
qed_goalw "PowI" Set.thy [Pow_def] "!!A B. A <= B ==> A : Pow(B)"


449 
(fn _ => [ (etac CollectI 1) ]);


450 


451 
qed_goalw "PowD" Set.thy [Pow_def] "!!A B. A : Pow(B) ==> A<=B"


452 
(fn _=> [ (etac CollectD 1) ]);


453 


454 
val Pow_bottom = empty_subsetI RS PowI; (* {}: Pow(B) *)


455 
val Pow_top = subset_refl RS PowI; (* A : Pow(A) *)
