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 


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


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


13 
by (rtac prem 1);


14 
qed "CollectI";


15 


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


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


18 
qed "CollectD";


19 


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


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


22 
by (rtac Collect_mem_eq 1);


23 
by (rtac Collect_mem_eq 1);


24 
qed "set_ext";


25 


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


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


28 
qed "Collect_cong";


29 


30 
val CollectE = make_elim CollectD;


31 


32 
(*** Bounded quantifiers ***)


33 


34 
val prems = goalw Set.thy [Ball_def]


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


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


37 
qed "ballI";


38 


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


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


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


42 
qed "bspec";


43 


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


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


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


47 
by (REPEAT (eresolve_tac prems 1));


48 
qed "ballE";


49 


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


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


52 


53 
val prems = goalw Set.thy [Bex_def]


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


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


56 
qed "bexI";


57 


58 
qed_goal "bexCI" Set.thy


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


60 
(fn prems=>


61 
[ (rtac classical 1),


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


63 


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


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


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


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


68 
qed "bexE";


69 


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


71 
val prems = goal Set.thy


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


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


74 
qed "ball_rew";


75 


76 
(** Congruence rules **)


77 


78 
val prems = goal Set.thy


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


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


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


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


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


84 
qed "ball_cong";


85 


86 
val prems = goal Set.thy


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


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


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


90 
by (REPEAT (etac bexE 1


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


92 
qed "bex_cong";


93 


94 
(*** Subsets ***)


95 


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


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


98 
qed "subsetI";


99 


100 
(*Rule in Modus Ponens style*)


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


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


103 
by (resolve_tac prems 1);


104 
qed "subsetD";


105 


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


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


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


109 


110 
(*Classical elimination rule*)


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


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


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


114 
by (REPEAT (eresolve_tac prems 1));


115 
qed "subsetCE";


116 


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


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


119 


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


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


122 


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


124 
by (cut_facts_tac prems 1);


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


126 
qed "subset_trans";


127 


128 


129 
(*** Equality ***)


130 


131 
(*Antisymmetry of the subset relation*)


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


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


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


135 
qed "subset_antisym";


136 
val equalityI = subset_antisym;


137 


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


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


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


141 
by (rtac subset_refl 1);


142 
qed "equalityD1";


143 


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


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


146 
by (rtac subset_refl 1);


147 
qed "equalityD2";


148 


149 
val prems = goal Set.thy


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


151 
by (resolve_tac prems 1);


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


153 
qed "equalityE";


154 


155 
val major::prems = goal Set.thy


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


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


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


159 
qed "equalityCE";


160 


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


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


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


164 
val prems = goal Set.thy


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


166 
by (rtac mp 1);


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


168 
qed "setup_induction";


169 


170 


171 
(*** Set complement  Compl ***)


172 


173 
val prems = goalw Set.thy [Compl_def]


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


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


176 
qed "ComplI";


177 


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


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


180 
turnstile...*)


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


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


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


184 
qed "ComplD";


185 


186 
val ComplE = make_elim ComplD;


187 


188 


189 
(*** Binary union  Un ***)


190 


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


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


193 
qed "UnI1";


194 


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


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


197 
qed "UnI2";


198 


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


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


201 
(fn prems=>


202 
[ (rtac classical 1),


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


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


205 


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


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


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


209 
by (REPEAT (eresolve_tac prems 1));


210 
qed "UnE";


211 


212 


213 
(*** Binary intersection  Int ***)


214 


215 
val prems = goalw Set.thy [Int_def]


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


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


218 
qed "IntI";


219 


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


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


222 
qed "IntD1";


223 


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


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


226 
qed "IntD2";


227 


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


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


230 
by (rtac minor 1);


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


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


233 
qed "IntE";


234 


235 


236 
(*** Set difference ***)


237 


238 
qed_goalw "DiffI" Set.thy [set_diff_def]


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


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


241 


242 
qed_goalw "DiffD1" Set.thy [set_diff_def]


243 
"c : A  B ==> c : A"


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


245 


246 
qed_goalw "DiffD2" Set.thy [set_diff_def]


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


248 
(fn [major,minor]=>


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


250 


251 
qed_goal "DiffE" Set.thy


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


253 
(fn prems=>


254 
[ (resolve_tac prems 1),


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


256 


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


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


259 


260 
(*** The empty set  {} ***)


261 


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


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


264 


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


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


267 


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


269 
(fn prems=>


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


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


272 


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


274 
(fn [major,minor]=>


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


276 


277 


278 
(*** Augmenting a set  insert ***)


279 


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


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


282 


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


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


285 


286 
qed_goalw "insertE" Set.thy [insert_def]


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


288 
(fn major::prems=>


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


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


291 


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


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


294 


295 
(*Classical introduction rule*)


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


297 
(fn [prem]=>


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


299 
(etac prem 1) ]);


300 


301 
(*** Singletons, using insert ***)


302 


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


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


305 


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


307 
(fn major::prems=>


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


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


310 


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


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


313 
qed "singletonD";


314 


315 
val singletonE = make_elim singletonD;


316 


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


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


319 
by (rtac singletonI 1);


320 
qed "singleton_inject";


321 

1531

322 


323 
(*** UNIV  The universal set ***)


324 


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


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


327 


328 

923

329 
(*** Unions of families  UNION x:A. B(x) is Union(B``A) ***)


330 


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


332 
val prems = goalw Set.thy [UNION_def]


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


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


335 
qed "UN_I";


336 


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


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


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


340 
by (REPEAT (ares_tac prems 1));


341 
qed "UN_E";


342 


343 
val prems = goal Set.thy


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


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


346 
by (REPEAT (etac UN_E 1


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

1465

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

923

349 
qed "UN_cong";


350 


351 


352 
(*** Intersections of families  INTER x:A. B(x) is Inter(B``A) *)


353 


354 
val prems = goalw Set.thy [INTER_def]


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


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


357 
qed "INT_I";


358 


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


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


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


362 
by (resolve_tac prems 1);


363 
qed "INT_D";


364 


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


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


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


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


369 
by (REPEAT (eresolve_tac prems 1));


370 
qed "INT_E";


371 


372 
val prems = goal Set.thy


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


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


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


376 
by (REPEAT (dtac INT_D 1


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


378 
qed "INT_cong";


379 


380 


381 
(*** Unions over a type; UNION1(B) = Union(range(B)) ***)


382 


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


384 
val prems = goalw Set.thy [UNION1_def]


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


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


387 
qed "UN1_I";


388 


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


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


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


392 
by (REPEAT (ares_tac prems 1));


393 
qed "UN1_E";


394 


395 


396 
(*** Intersections over a type; INTER1(B) = Inter(range(B)) *)


397 


398 
val prems = goalw Set.thy [INTER1_def]


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


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


401 
qed "INT1_I";


402 


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


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


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


406 
qed "INT1_D";


407 


408 
(*** Unions ***)


409 


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


411 
val prems = goalw Set.thy [Union_def]


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


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


414 
qed "UnionI";


415 


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


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


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


419 
by (REPEAT (ares_tac prems 1));


420 
qed "UnionE";


421 


422 
(*** Inter ***)


423 


424 
val prems = goalw Set.thy [Inter_def]


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


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


427 
qed "InterI";


428 


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


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


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


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


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


434 
by (resolve_tac prems 1);


435 
qed "InterD";


436 


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


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


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


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


441 
by (REPEAT (eresolve_tac prems 1));


442 
qed "InterE";


443 


444 
(*** Powerset ***)


445 


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


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


448 


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


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


451 


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


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