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 


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


262 


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


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


265 


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


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


268 


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


270 
(fn prems=>


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


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


273 


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


275 
(fn [major,minor]=>


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


277 


278 


279 
(*** Augmenting a set  insert ***)


280 


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


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


283 


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


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


286 


287 
qed_goalw "insertE" Set.thy [insert_def]


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


289 
(fn major::prems=>


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


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


292 


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


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


295 


296 
(*Classical introduction rule*)


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


298 
(fn [prem]=>


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


300 
(etac prem 1) ]);


301 


302 
(*** Singletons, using insert ***)


303 


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


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


306 


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


308 
(fn major::prems=>


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


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


311 


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


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


314 
qed "singletonD";


315 


316 
val singletonE = make_elim singletonD;


317 


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


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


320 
by (rtac singletonI 1);


321 
qed "singleton_inject";


322 


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


324 


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


326 
val prems = goalw Set.thy [UNION_def]


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


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


329 
qed "UN_I";


330 


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


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


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


334 
by (REPEAT (ares_tac prems 1));


335 
qed "UN_E";


336 


337 
val prems = goal Set.thy


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


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


340 
by (REPEAT (etac UN_E 1


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

1465

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

923

343 
qed "UN_cong";


344 


345 


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


347 


348 
val prems = goalw Set.thy [INTER_def]


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


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


351 
qed "INT_I";


352 


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


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


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


356 
by (resolve_tac prems 1);


357 
qed "INT_D";


358 


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


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


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


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


363 
by (REPEAT (eresolve_tac prems 1));


364 
qed "INT_E";


365 


366 
val prems = goal Set.thy


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


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


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


370 
by (REPEAT (dtac INT_D 1


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


372 
qed "INT_cong";


373 


374 


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


376 


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


378 
val prems = goalw Set.thy [UNION1_def]


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


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


381 
qed "UN1_I";


382 


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


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


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


386 
by (REPEAT (ares_tac prems 1));


387 
qed "UN1_E";


388 


389 


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


391 


392 
val prems = goalw Set.thy [INTER1_def]


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


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


395 
qed "INT1_I";


396 


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


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


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


400 
qed "INT1_D";


401 


402 
(*** Unions ***)


403 


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


405 
val prems = goalw Set.thy [Union_def]


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


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


408 
qed "UnionI";


409 


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


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


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


413 
by (REPEAT (ares_tac prems 1));


414 
qed "UnionE";


415 


416 
(*** Inter ***)


417 


418 
val prems = goalw Set.thy [Inter_def]


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


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


421 
qed "InterI";


422 


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


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


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


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


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


428 
by (resolve_tac prems 1);


429 
qed "InterD";


430 


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


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


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


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


435 
by (REPEAT (eresolve_tac prems 1));


436 
qed "InterE";


437 


438 
(*** Powerset ***)


439 


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


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


442 


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


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


445 


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


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