(* ========================================================================= *)
(* Basic notions of cardinal arithmetic. *)
(* ========================================================================= *)
needs "Library/wo.ml";;
let TRANS_CHAIN_TAC th =
MAP_EVERY (fun t -> TRANS_TAC th t THEN ASM_REWRITE_TAC[]);;
(* ------------------------------------------------------------------------- *)
(* We need these a few times, so give them names. *)
(* ------------------------------------------------------------------------- *)
let sum_DISTINCT = distinctness "sum";;
let sum_INJECTIVE = injectivity "sum";;
(* ------------------------------------------------------------------------- *)
(* Special case of Zorn's Lemma for restriction of subset lattice. *)
(* ------------------------------------------------------------------------- *)
(* ------------------------------------------------------------------------- *)
(* Useful lemma to reduce some higher order stuff to first order. *)
(* ------------------------------------------------------------------------- *)
(* ------------------------------------------------------------------------- *)
(* Knaster-Tarski fixpoint theorem (used in Schroeder-Bernstein below). *)
(* ------------------------------------------------------------------------- *)
(* ------------------------------------------------------------------------- *)
(* We need a nonemptiness hypothesis for the nicest total function form. *)
(* ------------------------------------------------------------------------- *)
(* ------------------------------------------------------------------------- *)
(* Now bijectivity. *)
(* ------------------------------------------------------------------------- *)
let BIJECTIVE_INVERSES = prove
(`(!x. x
IN s ==> f(x)
IN t) /\
(!y. y
IN t ==> ?!x. x
IN s /\ (f x = y)) <=>
(!x. x
IN s ==> f(x)
IN t) /\
?g. (!y. y
IN t ==> g(y)
IN s) /\
(!y. y
IN t ==> (f(g(y)) = y)) /\
(!x. x
IN s ==> (g(f(x)) = x))`,
(* ------------------------------------------------------------------------- *)
(* Other variants of cardinal equality. *)
(* ------------------------------------------------------------------------- *)
let EQ_C_BIJECTIONS = prove
(`!s:A->bool t:B->bool.
s =_c t <=> ?f g. (!x. x
IN s ==> f x
IN t /\ g(f x) = x) /\
(!y. y
IN t ==> g y
IN s /\ f(g y) = y)`,
REPEAT GEN_TAC THEN REWRITE_TAC[
eq_c] THEN
AP_TERM_TAC THEN GEN_REWRITE_TAC I [
FUN_EQ_THM] THEN
X_GEN_TAC `f:A->B` THEN REWRITE_TAC[] THEN
EQ_TAC THENL [STRIP_TAC; MESON_TAC[]] THEN
EXISTS_TAC `(\y. @x. x
IN s /\ f x = y):B->A` THEN
ASM_MESON_TAC[]);;
let EQ_C = prove
(`s =_c t <=>
?R:A#B->bool. (!x y. R(x,y) ==> x
IN s /\ y
IN t) /\
(!x. x
IN s ==> ?!y. y
IN t /\ R(x,y)) /\
(!y. y
IN t ==> ?!x. x
IN s /\ R(x,y))`,
REWRITE_TAC[
eq_c] THEN EQ_TAC THENL
[DISCH_THEN(X_CHOOSE_THEN `f:A->B` STRIP_ASSUME_TAC) THEN
EXISTS_TAC `\(x:A,y:B). x
IN s /\ y
IN t /\ (y = f x)` THEN
CONV_TAC(ONCE_DEPTH_CONV GEN_BETA_CONV) THEN ASM_MESON_TAC[];
DISCH_THEN(CHOOSE_THEN (CONJUNCTS_THEN2 ASSUME_TAC MP_TAC)) THEN
DISCH_THEN(CONJUNCTS_THEN2 MP_TAC ASSUME_TAC) THEN
GEN_REWRITE_TAC (LAND_CONV o TOP_DEPTH_CONV)
[
EXISTS_UNIQUE_ALT;
RIGHT_IMP_EXISTS_THM;
SKOLEM_THM] THEN
MATCH_MP_TAC
MONO_EXISTS THEN ASM_MESON_TAC[]]);;
(* ------------------------------------------------------------------------- *)
(* The "easy" ordering properties. *)
(* ------------------------------------------------------------------------- *)
let CARD_LE_REFL = prove
(`!s:A->bool. s <=_c s`,
GEN_TAC THEN REWRITE_TAC[
le_c] THEN EXISTS_TAC `\x:A. x` THEN SIMP_TAC[]);;
let CARD_LE_TRANS = prove
(`!s:A->bool t:B->bool u:C->bool.
s <=_c t /\ t <=_c u ==> s <=_c u`,
REPEAT GEN_TAC THEN REWRITE_TAC[
le_c] THEN
DISCH_THEN(CONJUNCTS_THEN2
(X_CHOOSE_TAC `f:A->B`) (X_CHOOSE_TAC `g:B->C`)) THEN
EXISTS_TAC `(g:B->C) o (f:A->B)` THEN REWRITE_TAC[
o_THM] THEN
ASM_MESON_TAC[]);;
let CARD_LET_TRANS = prove
(`!s:A->bool t:B->bool u:C->bool.
s <=_c t /\ t <_c u ==> s <_c u`,
REPEAT GEN_TAC THEN REWRITE_TAC[
lt_c] THEN
MATCH_MP_TAC(TAUT `(a /\ b ==> c) /\ (c' /\ a ==> b')
==> a /\ b /\ ~b' ==> c /\ ~c'`) THEN
REWRITE_TAC[
CARD_LE_TRANS]);;
let CARD_LTE_TRANS = prove
(`!s:A->bool t:B->bool u:C->bool.
s <_c t /\ t <=_c u ==> s <_c u`,
REPEAT GEN_TAC THEN REWRITE_TAC[
lt_c] THEN
MATCH_MP_TAC(TAUT `(a /\ b ==> c) /\ (b /\ c' ==> a')
==> (a /\ ~a') /\ b ==> c /\ ~c'`) THEN
REWRITE_TAC[
CARD_LE_TRANS]);;
let CARD_EQ_REFL = prove
(`!s:A->bool. s =_c s`,
GEN_TAC THEN REWRITE_TAC[
eq_c] THEN EXISTS_TAC `\x:A. x` THEN
SIMP_TAC[] THEN MESON_TAC[]);;
let CARD_LE_RELATIONAL = prove
(`!R:A->B->bool.
(!x y y'. x
IN s /\ R x y /\ R x y' ==> y = y')
==> {y | ?x. x
IN s /\ R x y} <=_c s`,
REPEAT STRIP_TAC THEN REWRITE_TAC[
le_c] THEN
EXISTS_TAC `\y:B. @x:A. x
IN s /\ R x y` THEN
REWRITE_TAC[
IN_ELIM_THM] THEN ASM_MESON_TAC[]);;
let CARD_LE_RELATIONAL_FULL = prove
(`!R:A->B->bool s t.
(!y. y
IN t ==> ?x. x
IN s /\ R x y) /\
(!x y y'. x
IN s /\ y
IN t /\ y'
IN t /\ R x y /\ R x y' ==> y = y')
==> t <=_c s`,
REPEAT STRIP_TAC THEN REWRITE_TAC[
le_c] THEN
EXISTS_TAC `\y:B. @x:A. x
IN s /\ R x y` THEN
REWRITE_TAC[
IN_ELIM_THM] THEN ASM_MESON_TAC[]);;
(* ------------------------------------------------------------------------- *)
(* Two trivial lemmas. *)
(* ------------------------------------------------------------------------- *)
(* ------------------------------------------------------------------------- *)
(* Antisymmetry (the Schroeder-Bernstein theorem). *)
(* ------------------------------------------------------------------------- *)
let CARD_LE_ANTISYM = prove
(`!s:A->bool t:B->bool. s <=_c t /\ t <=_c s <=> (s =_c t)`,
REPEAT GEN_TAC THEN EQ_TAC THENL
[ALL_TAC;
SIMP_TAC[
CARD_EQ_IMP_LE] THEN ONCE_REWRITE_TAC[
CARD_EQ_SYM] THEN
SIMP_TAC[
CARD_EQ_IMP_LE]] THEN
ASM_CASES_TAC `s:A->bool = {}` THEN ASM_CASES_TAC `t:B->bool = {}` THEN
ASM_SIMP_TAC[
CARD_LE_EMPTY;
CARD_EQ_EMPTY] THEN
RULE_ASSUM_TAC(REWRITE_RULE[
EXTENSION;
NOT_IN_EMPTY;
NOT_FORALL_THM]) THEN
ASM_SIMP_TAC[
le_c;
eq_c;
INJECTIVE_LEFT_INVERSE_NONEMPTY] THEN
DISCH_THEN(CONJUNCTS_THEN2
(X_CHOOSE_THEN `i:A->B`
(CONJUNCTS_THEN2 ASSUME_TAC (X_CHOOSE_THEN `i':B->A` STRIP_ASSUME_TAC)))
(X_CHOOSE_THEN `j:B->A`
(CONJUNCTS_THEN2 ASSUME_TAC
(X_CHOOSE_THEN `j':A->B` STRIP_ASSUME_TAC)))) THEN
MP_TAC(ISPEC
`\a. s
DIFF (
IMAGE (j:B->A) (t
DIFF (
IMAGE (i:A->B) a)))`
TARSKI_SET) THEN
REWRITE_TAC[] THEN ANTS_TAC THENL
[REWRITE_TAC[
SUBSET;
IN_DIFF;
IN_IMAGE] THEN MESON_TAC[];
ALL_TAC] THEN
DISCH_THEN(X_CHOOSE_THEN `a:A->bool` ASSUME_TAC) THEN
REWRITE_TAC[
BIJECTIVE_INVERSES] THEN REWRITE_TAC[
RIGHT_AND_EXISTS_THM] THEN
EXISTS_TAC `\x. if x
IN a then (i:A->B)(x) else j'(x)` THEN
EXISTS_TAC `\y. if y
IN (
IMAGE (i:A->B) a) then i'(y) else (j:B->A)(y)` THEN
REWRITE_TAC[
FUN_EQ_THM;
o_THM;
I_DEF] THEN
ONCE_REWRITE_TAC[TAUT `a /\ b /\ c /\ d <=> (a /\ d) /\ (b /\ c)`] THEN
REWRITE_TAC[
AND_FORALL_THM] THEN
REWRITE_TAC[TAUT `(a ==> b) /\ (a ==> c) <=> a ==> b /\ c`] THEN
CONJ_TAC THENL
[X_GEN_TAC `x:A` THEN ASM_CASES_TAC `x:A
IN a`;
X_GEN_TAC `y:B` THEN ASM_CASES_TAC `y
IN IMAGE (i:A->B) a`] THEN
ASM_REWRITE_TAC[] THEN COND_CASES_TAC THEN ASM_REWRITE_TAC[] THEN
RULE_ASSUM_TAC(REWRITE_RULE[
EXTENSION;
IN_UNIV;
IN_DIFF;
IN_IMAGE]) THEN
TRY(FIRST_X_ASSUM(X_CHOOSE_THEN `x:A` STRIP_ASSUME_TAC)) THEN
TRY(FIRST_X_ASSUM(fun th -> MP_TAC(SPEC `x:A` th) THEN
ASM_REWRITE_TAC[] THEN ASSUME_TAC th)) THEN
ASM_MESON_TAC[]);;
(* ------------------------------------------------------------------------- *)
(* Totality (cardinal comparability). *)
(* ------------------------------------------------------------------------- *)
let CARD_LE_TOTAL = prove
(`!s:A->bool t:B->bool. s <=_c t \/ t <=_c s`,
REPEAT GEN_TAC THEN
ABBREV_TAC
`P = \R. (!x:A y:B. R(x,y) ==> x
IN s /\ y
IN t) /\
(!x y y'. R(x,y) /\ R(x,y') ==> (y = y')) /\
(!x x' y. R(x,y) /\ R(x',y) ==> (x = x'))` THEN
MP_TAC(ISPEC `P:((A#B)->bool)->bool`
ZL_SUBSETS_UNIONS) THEN ANTS_TAC THENL
[GEN_TAC THEN EXPAND_TAC "P" THEN
REWRITE_TAC[
UNIONS;
IN_ELIM_THM] THEN
REWRITE_TAC[
SUBSET;
IN] THEN MESON_TAC[];
ALL_TAC] THEN
FIRST_X_ASSUM(SUBST1_TAC o SYM) THEN REWRITE_TAC[] THEN
DISCH_THEN(X_CHOOSE_THEN `R:A#B->bool` STRIP_ASSUME_TAC) THEN
ASM_CASES_TAC `(!x:A. x
IN s ==> ?y:B. y
IN t /\ R(x,y)) \/
(!y:B. y
IN t ==> ?x:A. x
IN s /\ R(x,y))`
THENL
[FIRST_X_ASSUM(K ALL_TAC o SPEC `\(x:A,y:B). T`) THEN
FIRST_X_ASSUM(DISJ_CASES_THEN MP_TAC) THEN
REWRITE_TAC[
RIGHT_IMP_EXISTS_THM;
SKOLEM_THM;
le_c] THEN ASM_MESON_TAC[];
FIRST_X_ASSUM(MP_TAC o GEN_REWRITE_RULE I [DE_MORGAN_THM]) THEN
REWRITE_TAC[
NOT_FORALL_THM; NOT_IMP] THEN
DISCH_THEN(CONJUNCTS_THEN2 (X_CHOOSE_TAC `a:A`) (X_CHOOSE_TAC `b:B`)) THEN
FIRST_X_ASSUM(MP_TAC o SPEC
`\(x:A,y:B). (x = a) /\ (y = b) \/ R(x,y)`) THEN
REWRITE_TAC[
SUBSET;
FORALL_PAIR_THM;
IN;
EXTENSION] THEN
CONV_TAC(ONCE_DEPTH_CONV GEN_BETA_CONV) THEN
RULE_ASSUM_TAC(REWRITE_RULE[
IN]) THEN ASM_MESON_TAC[]]);;
(* ------------------------------------------------------------------------- *)
(* Other variants like "trichotomy of cardinals" now follow easily. *)
(* ------------------------------------------------------------------------- *)
let CARD_LE_CONG = prove
(`!s:A->bool s':B->bool t:C->bool t':D->bool.
s =_c s' /\ t =_c t' ==> (s <=_c t <=> s' <=_c t')`,
REPEAT GEN_TAC THEN REWRITE_TAC[GSYM
CARD_LE_ANTISYM] THEN
MATCH_MP_TAC(TAUT
`!x y. (b /\ e ==> x) /\ (x /\ c ==> f) /\ (a /\ f ==> y) /\ (y /\ d ==> e)
==> (a /\ b) /\ (c /\ d) ==> (e <=> f)`) THEN
MAP_EVERY EXISTS_TAC
[`(s':B->bool) <=_c (t:C->bool)`;
`(s:A->bool) <=_c (t':D->bool)`] THEN
REWRITE_TAC[
CARD_LE_TRANS]);;
let CARD_LT_CONG = prove
(`!s:A->bool s':B->bool t:C->bool t':D->bool.
s =_c s' /\ t =_c t' ==> (s <_c t <=> s' <_c t')`,
REPEAT STRIP_TAC THEN REWRITE_TAC[GSYM
CARD_NOT_LE] THEN
AP_TERM_TAC THEN MATCH_MP_TAC
CARD_LE_CONG THEN
ASM_REWRITE_TAC[]);;
let CARD_EQ_CONG = prove
(`!s:A->bool s':B->bool t:C->bool t':D->bool.
s =_c s' /\ t =_c t' ==> (s =_c t <=> s' =_c t')`,
REPEAT STRIP_TAC THEN EQ_TAC THEN DISCH_TAC THENL
[TRANS_CHAIN_TAC
CARD_EQ_TRANS [`t:C->bool`; `s:A->bool`];
TRANS_CHAIN_TAC
CARD_EQ_TRANS [`s':B->bool`; `t':D->bool`]] THEN
ASM_MESON_TAC[
CARD_EQ_SYM]);;
(* ------------------------------------------------------------------------- *)
(* Finiteness and infiniteness in terms of cardinality of N. *)
(* ------------------------------------------------------------------------- *)
let CARD_LE_CARD_IMP = prove
(`!s:A->bool t:B->bool.
FINITE t /\ s <=_c t ==>
CARD s <=
CARD t`,
REPEAT STRIP_TAC THEN
SUBGOAL_THEN `
FINITE(s:A->bool)` ASSUME_TAC THENL
[ASM_MESON_TAC[
CARD_LE_FINITE]; ALL_TAC] THEN
FIRST_X_ASSUM(MP_TAC o GEN_REWRITE_RULE I [
le_c]) THEN
DISCH_THEN(X_CHOOSE_THEN `f:A->B` STRIP_ASSUME_TAC) THEN
MATCH_MP_TAC
LE_TRANS THEN EXISTS_TAC `
CARD(
IMAGE (f:A->B) s)` THEN
CONJ_TAC THENL
[MATCH_MP_TAC(ARITH_RULE `(m = n:num) ==> n <= m`) THEN
MATCH_MP_TAC
CARD_IMAGE_INJ THEN ASM_REWRITE_TAC[];
MATCH_MP_TAC
CARD_SUBSET THEN ASM_REWRITE_TAC[] THEN
ASM_MESON_TAC[
SUBSET;
IN_IMAGE]]);;
let CARD_EQ_IMAGE = prove
(`!f:A->B s.
(!x y. x
IN s /\ y
IN s /\ f x = f y ==> x = y)
==>
IMAGE f s =_c s`,
REPEAT STRIP_TAC THEN ONCE_REWRITE_TAC[
CARD_EQ_SYM] THEN
REWRITE_TAC[
eq_c] THEN EXISTS_TAC `f:A->B` THEN ASM SET_TAC[]);;
(* ------------------------------------------------------------------------- *)
(* Cardinal arithmetic operations. *)
(* ------------------------------------------------------------------------- *)
parse_as_infix("+_c",(16,"right"));;
parse_as_infix("*_c",(20,"right"));;
let mul_c = new_definition
`s *_c t = {(x,y) | x IN s /\ y IN t}`;;
(* ------------------------------------------------------------------------- *)
(* Congruence properties for the arithmetic operators. *)
(* ------------------------------------------------------------------------- *)
let CARD_LE_ADD = prove
(`!s:A->bool s':B->bool t:C->bool t':D->bool.
s <=_c s' /\ t <=_c t' ==> s +_c t <=_c s' +_c t'`,
REPEAT GEN_TAC THEN REWRITE_TAC[
le_c;
add_c] THEN
DISCH_THEN(CONJUNCTS_THEN2
(X_CHOOSE_THEN `f:A->B` STRIP_ASSUME_TAC)
(X_CHOOSE_THEN `g:C->D` STRIP_ASSUME_TAC)) THEN
MP_TAC(prove_recursive_functions_exist sum_RECURSION
`(!x. h(INL x) = INL((f:A->B) x)) /\ (!y. h(INR y) = INR((g:C->D) y))`) THEN
MATCH_MP_TAC
MONO_EXISTS THEN X_GEN_TAC `h:(A+C)->(B+D)` THEN STRIP_TAC THEN
REWRITE_TAC[
IN_UNION;
IN_ELIM_THM] THEN
CONJ_TAC THEN REPEAT GEN_TAC THEN
REPEAT(DISCH_THEN(CONJUNCTS_THEN2 STRIP_ASSUME_TAC MP_TAC) THEN
ASM_REWRITE_TAC[]) THEN
ASM_REWRITE_TAC[sum_DISTINCT;
sum_INJECTIVE] THEN
ASM_MESON_TAC[]);;
let CARD_LE_MUL = prove
(`!s:A->bool s':B->bool t:C->bool t':D->bool.
s <=_c s' /\ t <=_c t' ==> s *_c t <=_c s' *_c t'`,
REPEAT GEN_TAC THEN REWRITE_TAC[
le_c;
mul_c] THEN
DISCH_THEN(CONJUNCTS_THEN2
(X_CHOOSE_THEN `f:A->B` STRIP_ASSUME_TAC)
(X_CHOOSE_THEN `g:C->D` STRIP_ASSUME_TAC)) THEN
EXISTS_TAC `\(x,y). (f:A->B) x,(g:C->D) y` THEN
REWRITE_TAC[
FORALL_PAIR_THM;
IN_ELIM_THM] THEN
CONV_TAC(ONCE_DEPTH_CONV GEN_BETA_CONV) THEN
REWRITE_TAC[
PAIR_EQ] THEN ASM_MESON_TAC[]);;
let CARD_FUNSPACE_LE = prove
(`(:A) <=_c (:A') /\ (:B) <=_c (:B') ==> (:A->B) <=_c (:A'->B')`,
REWRITE_TAC[
le_c;
IN_UNIV] THEN DISCH_THEN(CONJUNCTS_THEN2
(X_CHOOSE_TAC `f:A->A'`) (X_CHOOSE_TAC `g:B->B'`)) THEN
SUBGOAL_THEN `?f':A'->A. !x. f'(f x) = x` STRIP_ASSUME_TAC THENL
[ASM_REWRITE_TAC[GSYM
INJECTIVE_LEFT_INVERSE]; ALL_TAC] THEN
EXISTS_TAC `\h. (g:B->B') o (h:A->B) o (f':A'->A)` THEN
ASM_REWRITE_TAC[
o_DEF;
FUN_EQ_THM] THEN ASM_MESON_TAC[]);;
let CARD_ADD_CONG = prove
(`!s:A->bool s':B->bool t:C->bool t':D->bool.
s =_c s' /\ t =_c t' ==> s +_c t =_c s' +_c t'`,
let CARD_MUL_CONG = prove
(`!s:A->bool s':B->bool t:C->bool t':D->bool.
s =_c s' /\ t =_c t' ==> s *_c t =_c s' *_c t'`,
(* ------------------------------------------------------------------------- *)
(* Misc lemmas. *)
(* ------------------------------------------------------------------------- *)
(* ------------------------------------------------------------------------- *)
(* Various "arithmetical" lemmas. *)
(* ------------------------------------------------------------------------- *)
let CARD_ADD_ASSOC = prove
(`!s:A->bool t:B->bool u:C->bool. s +_c (t +_c u) =_c (s +_c t) +_c u`,
REPEAT GEN_TAC THEN REWRITE_TAC[
eq_c] THEN
CHOOSE_TAC(prove_recursive_functions_exist sum_RECURSION
`(!u. (i:B+C->(A+B)+C) (INL u) = INL(INR u)) /\
(!v. i(INR v) = INR v)`) THEN
MP_TAC(prove_recursive_functions_exist sum_RECURSION
`(!x. (h:A+B+C->(A+B)+C) (INL x) = INL(INL x)) /\
(!z. h(INR z) = i(z))`) THEN
MATCH_MP_TAC
MONO_EXISTS THEN GEN_TAC THEN STRIP_TAC THEN
ASM_REWRITE_TAC[
FORALL_SUM_THM;
EXISTS_SUM_THM;
EXISTS_UNIQUE_THM;
sum_DISTINCT; sum_INJECTIVE;
IN_CARD_ADD] THEN
MESON_TAC[]);;
let CARD_LDISTRIB = prove
(`!s:A->bool t:B->bool u:C->bool.
s *_c (t +_c u) =_c (s *_c t) +_c (s *_c u)`,
let CARD_RDISTRIB = prove
(`!s:A->bool t:B->bool u:C->bool.
(s +_c t) *_c u =_c (s *_c u) +_c (t *_c u)`,
let CARD_LE_ADDR = prove
(`!s:A->bool t:B->bool. s <=_c s +_c t`,
REPEAT GEN_TAC THEN REWRITE_TAC[
le_c] THEN
EXISTS_TAC `INL:A->A+B` THEN SIMP_TAC[
IN_CARD_ADD; sum_INJECTIVE]);;
let CARD_LE_ADDL = prove
(`!s:A->bool t:B->bool. t <=_c s +_c t`,
REPEAT GEN_TAC THEN REWRITE_TAC[
le_c] THEN
EXISTS_TAC `INR:B->A+B` THEN SIMP_TAC[
IN_CARD_ADD; sum_INJECTIVE]);;
(* ------------------------------------------------------------------------- *)
(* A rather special lemma but temporarily useful. *)
(* ------------------------------------------------------------------------- *)
let CARD_ADD_LE_MUL_INFINITE = prove
(`!s:A->bool.
INFINITE s ==> s +_c s <=_c s *_c s`,
GEN_TAC THEN REWRITE_TAC[
INFINITE_CARD_LE;
le_c;
IN_UNIV] THEN
DISCH_THEN(X_CHOOSE_THEN `f:num->A` STRIP_ASSUME_TAC) THEN
MP_TAC(prove_recursive_functions_exist sum_RECURSION
`(!x. h(INL x) = (f(0),x):A#A) /\ (!x. h(INR x) = (f(1),x))`) THEN
MATCH_MP_TAC
MONO_EXISTS THEN X_GEN_TAC `h:A+A->A#A` THEN
STRIP_TAC THEN
REPEAT((MATCH_MP_TAC sum_INDUCT THEN
ASM_REWRITE_TAC[
IN_CARD_ADD;
IN_CARD_MUL;
PAIR_EQ])
ORELSE STRIP_TAC) THEN
ASM_REWRITE_TAC[] THEN ASM_MESON_TAC[NUM_REDUCE_CONV `1 = 0`]);;
(* ------------------------------------------------------------------------- *)
(* Relate cardinal addition to the simple union operation. *)
(* ------------------------------------------------------------------------- *)
(* ------------------------------------------------------------------------- *)
(* The key to arithmetic on infinite cardinals: k^2 = k. *)
(* ------------------------------------------------------------------------- *)
let CARD_SQUARE_INFINITE = prove
(`!k:A->bool.
INFINITE k ==> (k *_c k =_c k)`,
let lemma = prove
(`INFINITE(s:A->bool) /\ s SUBSET k /\
(!x y. R(x,y) ==> x IN (s *_c s) /\ y IN s) /\
(!x. x IN (s *_c s) ==> ?!y. y IN s /\ R(x,y)) /\
(!y:A. y IN s ==> ?!x. x IN (s *_c s) /\ R(x,y))
==> (s = {z | ?p. R(p,z)})`,
REWRITE_TAC[EXTENSION; IN_ELIM_THM] THEN MESON_TAC[]) in
REPEAT STRIP_TAC THEN
ABBREV_TAC
`P = \R. ?s. INFINITE(s:A->bool) /\ s SUBSET k /\
(!x y. R(x,y) ==> x IN (s *_c s) /\ y IN s) /\
(!x. x IN (s *_c s) ==> ?!y. y IN s /\ R(x,y)) /\
(!y. y IN s ==> ?!x. x IN (s *_c s) /\ R(x,y))` THEN
MP_TAC(ISPEC `P:((A#A)#A->bool)->bool` ZL_SUBSETS_UNIONS_NONEMPTY) THEN
ANTS_TAC THENL
[CONJ_TAC THENL
[FIRST_X_ASSUM(SUBST1_TAC o SYM) THEN REWRITE_TAC[] THEN
ONCE_REWRITE_TAC[SWAP_EXISTS_THM] THEN
REWRITE_TAC[RIGHT_EXISTS_AND_THM; GSYM EQ_C] THEN
FIRST_X_ASSUM(MP_TAC o GEN_REWRITE_RULE I [INFINITE_CARD_LE]) THEN
REWRITE_TAC[CARD_LE_EQ_SUBSET] THEN
MATCH_MP_TAC MONO_EXISTS THEN X_GEN_TAC `s:A->bool` THEN
STRIP_TAC THEN ASM_REWRITE_TAC[] THEN CONJ_TAC THENL
[ASM_MESON_TAC[num_INFINITE; CARD_INFINITE_CONG]; ALL_TAC] THEN
FIRST_ASSUM(fun th ->
MP_TAC(MATCH_MP CARD_MUL_CONG (CONJ th th))) THEN
GEN_REWRITE_TAC LAND_CONV [CARD_EQ_SYM] THEN
DISCH_THEN(MP_TAC o C CONJ CARD_SQUARE_NUM) THEN
DISCH_THEN(MP_TAC o MATCH_MP CARD_EQ_TRANS) THEN
FIRST_ASSUM(fun th ->
DISCH_THEN(ACCEPT_TAC o MATCH_MP CARD_EQ_TRANS o C CONJ th));
ALL_TAC] THEN
SUBGOAL_THEN
`P = \R. INFINITE {z | ?x y. R((x,y),z)} /\
(!x:A y z. R((x,y),z) ==> x IN k /\ y IN k /\ z IN k) /\
(!x y. (?u v. R((u,v),x)) /\ (?u v. R((u,v),y))
==> ?z. R((x,y),z)) /\
(!x y. (?z. R((x,y),z))
==> (?u v. R((u,v),x)) /\ (?u v. R((u,v),y))) /\
(!x y z1 z2. R((x,y),z1) /\ R((x,y),z2) ==> (z1 = z2)) /\
(!x1 y1 x2 y2 z. R((x1,y1),z) /\ R((x2,y2),z)
==> (x1 = x2) /\ (y1 = y2))`
SUBST1_TAC THENL
[FIRST_X_ASSUM(SUBST1_TAC o SYM) THEN REWRITE_TAC[] THEN
ONCE_REWRITE_TAC[MATCH_MP(TAUT `(a ==> b) ==> (a <=> b /\ a)`) lemma] THEN
REWRITE_TAC[UNWIND_THM2] THEN REWRITE_TAC[FUN_EQ_THM] THEN
REWRITE_TAC[IN_CARD_MUL; EXISTS_PAIR_THM; SUBSET; FUN_EQ_THM;
IN_ELIM_THM; FORALL_PAIR_THM; EXISTS_UNIQUE_THM;
UNIONS; PAIR_EQ] THEN
GEN_TAC THEN AP_TERM_TAC THEN MESON_TAC[];
ALL_TAC] THEN
FIRST_X_ASSUM(K ALL_TAC o SYM) THEN REWRITE_TAC[] THEN GEN_TAC THEN
GEN_REWRITE_TAC (LAND_CONV o ONCE_DEPTH_CONV)
[TAUT `a ==> b /\ c <=> (a ==> b) /\ (a ==> c)`] THEN
GEN_REWRITE_TAC (LAND_CONV o ONCE_DEPTH_CONV) [FORALL_AND_THM] THEN
MATCH_MP_TAC(TAUT
`(c /\ d ==> f) /\ (a /\ b ==> e)
==> (a /\ (b /\ c) /\ d ==> e /\ f)`) THEN
CONJ_TAC THENL
[REWRITE_TAC[UNIONS; IN_ELIM_THM] THEN
REWRITE_TAC[SUBSET; IN] THEN MESON_TAC[];
ALL_TAC] THEN
DISCH_THEN(CONJUNCTS_THEN2 (X_CHOOSE_TAC `s:(A#A)#A->bool`) MP_TAC) THEN
DISCH_THEN(MP_TAC o SPEC `s:(A#A)#A->bool`) THEN
ASM_REWRITE_TAC[INFINITE; CONTRAPOS_THM] THEN
MATCH_MP_TAC(ONCE_REWRITE_RULE[TAUT `a /\ b ==> c <=> b ==> a ==> c`]
FINITE_SUBSET) THEN
REWRITE_TAC[SUBSET; IN_ELIM_THM; UNIONS] THEN ASM_MESON_TAC[IN];
ALL_TAC] THEN
FIRST_X_ASSUM(SUBST1_TAC o SYM) THEN REWRITE_TAC[] THEN
DISCH_THEN(X_CHOOSE_THEN `R:(A#A)#A->bool`
(CONJUNCTS_THEN2 (X_CHOOSE_TAC `s:A->bool`) ASSUME_TAC)) THEN
SUBGOAL_THEN `(s:A->bool) *_c s =_c s` ASSUME_TAC THENL
[REWRITE_TAC[EQ_C] THEN EXISTS_TAC `R:(A#A)#A->bool` THEN ASM_REWRITE_TAC[];
ALL_TAC] THEN
SUBGOAL_THEN `s +_c s <=_c (s:A->bool)` ASSUME_TAC THENL
[TRANS_TAC CARD_LE_TRANS `(s:A->bool) *_c s` THEN
ASM_SIMP_TAC[CARD_EQ_IMP_LE; CARD_ADD_LE_MUL_INFINITE];
ALL_TAC] THEN
SUBGOAL_THEN `(s:A->bool) INTER (k DIFF s) = {}` ASSUME_TAC THENL
[REWRITE_TAC[EXTENSION; IN_INTER; IN_DIFF; NOT_IN_EMPTY] THEN MESON_TAC[];
ALL_TAC] THEN
DISJ_CASES_TAC(ISPECL [`k DIFF (s:A->bool)`; `s:A->bool`] CARD_LE_TOTAL)
THENL
[SUBGOAL_THEN `k = (s:A->bool) UNION (k DIFF s)` SUBST1_TAC THENL
[FIRST_ASSUM(MP_TAC o CONJUNCT1 o CONJUNCT2) THEN
REWRITE_TAC[SUBSET; EXTENSION; IN_INTER; NOT_IN_EMPTY;
IN_UNION; IN_DIFF] THEN
MESON_TAC[];
ALL_TAC] THEN
REWRITE_TAC[GSYM CARD_LE_ANTISYM; CARD_LE_SQUARE] THEN
TRANS_TAC CARD_LE_TRANS
`((s:A->bool) +_c (k DIFF s:A->bool)) *_c (s +_c k DIFF s)` THEN
ASM_SIMP_TAC[CARD_DISJOINT_UNION; CARD_EQ_IMP_LE; CARD_MUL_CONG] THEN
TRANS_TAC CARD_LE_TRANS `((s:A->bool) +_c s) *_c (s +_c s)` THEN
ASM_SIMP_TAC[CARD_LE_ADD; CARD_LE_MUL; CARD_LE_REFL] THEN
TRANS_TAC CARD_LE_TRANS `(s:A->bool) *_c s` THEN
ASM_SIMP_TAC[CARD_LE_MUL] THEN
TRANS_TAC CARD_LE_TRANS `s:A->bool` THEN ASM_SIMP_TAC[CARD_EQ_IMP_LE] THEN
REWRITE_TAC[CARD_LE_EQ_SUBSET] THEN EXISTS_TAC `s:A->bool` THEN
SIMP_TAC[CARD_EQ_REFL; SUBSET; IN_UNION];
ALL_TAC] THEN
UNDISCH_TAC `s:A->bool <=_c k DIFF s` THEN
REWRITE_TAC[CARD_LE_EQ_SUBSET] THEN
DISCH_THEN(X_CHOOSE_THEN `d:A->bool` STRIP_ASSUME_TAC) THEN
SUBGOAL_THEN `(s:A->bool *_c d) UNION (d *_c s) UNION (d *_c d) =_c d`
MP_TAC THENL
[TRANS_TAC CARD_EQ_TRANS
`((s:A->bool) *_c (d:A->bool)) +_c ((d *_c s) +_c (d *_c d))` THEN
CONJ_TAC THENL
[TRANS_TAC CARD_EQ_TRANS
`((s:A->bool) *_c d) +_c ((d *_c s) UNION (d *_c d))` THEN
CONJ_TAC THENL
[ALL_TAC;
MATCH_MP_TAC CARD_ADD_CONG THEN REWRITE_TAC[CARD_EQ_REFL]] THEN
MATCH_MP_TAC CARD_DISJOINT_UNION THEN
UNDISCH_TAC `s INTER (k DIFF s:A->bool) = {}` THEN
UNDISCH_TAC `d SUBSET (k DIFF s:A->bool)` THEN
REWRITE_TAC[EXTENSION; SUBSET; FORALL_PAIR_THM; NOT_IN_EMPTY;
IN_INTER; IN_UNION; IN_CARD_MUL; IN_DIFF] THEN MESON_TAC[];
ALL_TAC] THEN
TRANS_TAC CARD_EQ_TRANS `s:A->bool` THEN ASM_REWRITE_TAC[] THEN
TRANS_TAC CARD_EQ_TRANS
`(s:A->bool *_c s) +_c (s *_c s) +_c (s *_c s)` THEN
CONJ_TAC THENL
[REPEAT(MATCH_MP_TAC CARD_ADD_CONG THEN CONJ_TAC) THEN
MATCH_MP_TAC CARD_MUL_CONG THEN ASM_REWRITE_TAC[CARD_EQ_REFL] THEN
ONCE_REWRITE_TAC[CARD_EQ_SYM] THEN ASM_REWRITE_TAC[];
ALL_TAC] THEN
TRANS_TAC CARD_EQ_TRANS `(s:A->bool) +_c s +_c s` THEN CONJ_TAC THENL
[REPEAT(MATCH_MP_TAC CARD_ADD_CONG THEN ASM_REWRITE_TAC[]);
ALL_TAC] THEN
REWRITE_TAC[GSYM CARD_LE_ANTISYM; CARD_LE_ADDR] THEN
TRANS_TAC CARD_LE_TRANS `(s:A->bool) +_c s` THEN
ASM_SIMP_TAC[CARD_LE_ADD; CARD_LE_REFL];
ALL_TAC] THEN
FIRST_X_ASSUM(CONJUNCTS_THEN ASSUME_TAC) THEN
FIRST_X_ASSUM(CONJUNCTS_THEN ASSUME_TAC) THEN
REWRITE_TAC[EQ_C; IN_UNION] THEN
DISCH_THEN(X_CHOOSE_TAC `S:(A#A)#A->bool`) THEN
FIRST_X_ASSUM(MP_TAC o SPEC `\x:(A#A)#A. R(x) \/ S(x)`) THEN
ONCE_REWRITE_TAC[GSYM CONTRAPOS_THM] THEN DISCH_THEN(K ALL_TAC) THEN
REWRITE_TAC[NOT_IMP] THEN REPEAT CONJ_TAC THENL
[EXISTS_TAC `(s:A->bool) UNION d`;
SIMP_TAC[SUBSET; IN];
SUBGOAL_THEN `~(d:A->bool = {})` MP_TAC THENL
[DISCH_THEN(MP_TAC o AP_TERM `FINITE:(A->bool)->bool`) THEN
REWRITE_TAC[FINITE_RULES; GSYM INFINITE] THEN
ASM_MESON_TAC[CARD_INFINITE_CONG];
ALL_TAC] THEN
REWRITE_TAC[GSYM MEMBER_NOT_EMPTY] THEN DISCH_THEN(X_CHOOSE_TAC `a:A`) THEN
FIRST_ASSUM(MP_TAC o C MATCH_MP
(ASSUME `a:A IN d`) o last o CONJUNCTS) THEN
DISCH_THEN(MP_TAC o EXISTENCE) THEN
DISCH_THEN(X_CHOOSE_THEN `b:A#A` (CONJUNCTS_THEN ASSUME_TAC)) THEN
REWRITE_TAC[EXTENSION; NOT_FORALL_THM] THEN
EXISTS_TAC `(b:A#A,a:A)` THEN ASM_REWRITE_TAC[IN] THEN
DISCH_THEN(fun th -> FIRST_ASSUM
(MP_TAC o CONJUNCT2 o C MATCH_MP th o CONJUNCT1)) THEN
MAP_EVERY UNDISCH_TAC
[`a:A IN d`; `(d:A->bool) SUBSET (k DIFF s)`] THEN
REWRITE_TAC[SUBSET; IN_DIFF] THEN MESON_TAC[]] THEN
REWRITE_TAC[INFINITE; FINITE_UNION; DE_MORGAN_THM] THEN
ASM_REWRITE_TAC[GSYM INFINITE] THEN CONJ_TAC THENL
[MAP_EVERY UNDISCH_TAC
[`(d:A->bool) SUBSET (k DIFF s)`; `(s:A->bool) SUBSET k`] THEN
REWRITE_TAC[SUBSET; IN_UNION; IN_DIFF] THEN MESON_TAC[];
ALL_TAC] THEN
REPEAT(FIRST_ASSUM(UNDISCH_TAC o check is_conj o concl)) THEN
REWRITE_TAC[FORALL_PAIR_THM; EXISTS_UNIQUE_THM; EXISTS_PAIR_THM;
IN_CARD_MUL; IN_UNION; PAIR_EQ] THEN
MAP_EVERY UNDISCH_TAC
[`(s:A->bool) SUBSET k`;
`(d:A->bool) SUBSET (k DIFF s)`] THEN
REWRITE_TAC[SUBSET; EXTENSION; NOT_IN_EMPTY; IN_INTER; IN_DIFF] THEN
POP_ASSUM_LIST(K ALL_TAC) THEN
REPEAT DISCH_TAC THEN REPEAT CONJ_TAC THENL
[ASM_MESON_TAC[]; ASM_MESON_TAC[]; ALL_TAC] THEN
GEN_TAC THEN DISCH_THEN(DISJ_CASES_THEN MP_TAC) THENL
[ASM_MESON_TAC[]; ALL_TAC] THEN
DISCH_THEN(fun th -> CONJ_TAC THEN MP_TAC th) THENL
[ALL_TAC; ASM_MESON_TAC[]] THEN
DISCH_THEN(fun th ->
FIRST_ASSUM(MP_TAC o C MATCH_MP th o last o CONJUNCTS)) THEN
MESON_TAC[]);;
(* ------------------------------------------------------------------------- *)
(* Preservation of finiteness. *)
(* ------------------------------------------------------------------------- *)
(* ------------------------------------------------------------------------- *)
(* Hence the "absorption laws" for arithmetic with an infinite cardinal. *)
(* ------------------------------------------------------------------------- *)
let CARD_LT_ADD = prove
(`!s:A->bool s':B->bool t:C->bool t':D->bool.
s <_c s' /\ t <_c t' ==> s +_c t <_c s' +_c t'`,
(* ------------------------------------------------------------------------- *)
(* Some more ad-hoc but useful theorems. *)
(* ------------------------------------------------------------------------- *)
(* ------------------------------------------------------------------------- *)
(* Cantor's theorem. *)
(* ------------------------------------------------------------------------- *)
(* ------------------------------------------------------------------------- *)
(* Lemmas about countability. *)
(* ------------------------------------------------------------------------- *)
let COUNTABLE_CART = prove
(`!P. (!i. 1 <= i /\ i <= dimindex(:N) ==>
COUNTABLE {x | P i x})
==>
COUNTABLE {v:A^N | !i. 1 <= i /\ i <= dimindex(:N) ==> P i (v$i)}`,
GEN_TAC THEN DISCH_TAC THEN
SUBGOAL_THEN
`!n. n <= dimindex(:N)
==>
COUNTABLE {v:A^N | (!i. 1 <= i /\ i <= dimindex(:N) /\ i <= n
==> P i (v$i)) /\
(!i. 1 <= i /\ i <= dimindex(:N) /\ n < i
==> v$i = @x. F)}`
(MP_TAC o SPEC `dimindex(:N)`) THEN REWRITE_TAC[
LE_REFL;
LET_ANTISYM] THEN
INDUCT_TAC THENL
[REWRITE_TAC[ARITH_RULE `1 <= i /\ i <= n /\ i <= 0 <=> F`] THEN
SIMP_TAC[ARITH_RULE `1 <= i /\ i <= n /\ 0 < i <=> 1 <= i /\ i <= n`] THEN
SUBGOAL_THEN
`{v | !i. 1 <= i /\ i <= dimindex (:N) ==> v$i = (@x. F)} =
{(lambda i. @x. F):A^N}`
(fun th -> SIMP_TAC[
COUNTABLE_SING;th]) THEN
SIMP_TAC[
EXTENSION;
IN_SING;
IN_ELIM_THM;
CART_EQ;
LAMBDA_BETA];
ALL_TAC] THEN
DISCH_TAC THEN
MATCH_MP_TAC
COUNTABLE_SUBSET THEN EXISTS_TAC
`
IMAGE (\(x:A,v:A^N). (lambda i. if i = SUC n then x else v$i):A^N)
{x,v | x
IN {x:A | P (SUC n) x} /\
v
IN {v:A^N | (!i. 1 <= i /\ i <= dimindex(:N) /\ i <= n
==> P i (v$i)) /\
(!i. 1 <= i /\ i <= dimindex (:N) /\ n < i
==> v$i = (@x. F))}}` THEN
CONJ_TAC THENL
[MATCH_MP_TAC
COUNTABLE_IMAGE THEN
ASM_SIMP_TAC[REWRITE_RULE[
CROSS]
COUNTABLE_CROSS; ARITH_RULE `1 <= SUC n`;
ARITH_RULE `SUC n <= m ==> n <= m`];
ALL_TAC] THEN
REWRITE_TAC[
SUBSET;
IN_IMAGE;
IN_ELIM_PAIR_THM;
EXISTS_PAIR_THM] THEN
X_GEN_TAC `v:A^N` THEN REWRITE_TAC[
IN_ELIM_THM] THEN
STRIP_TAC THEN EXISTS_TAC `(v:A^N)$(SUC n)` THEN
EXISTS_TAC `(lambda i. if i = SUC n then @x. F else (v:A^N)$i):A^N` THEN
SIMP_TAC[
CART_EQ;
LAMBDA_BETA; ARITH_RULE `i <= n ==> ~(i = SUC n)`] THEN
ASM_MESON_TAC[
LE; ARITH_RULE `1 <= SUC n`;
ARITH_RULE `n < i /\ ~(i = SUC n) ==> SUC n < i`]);;
(* ------------------------------------------------------------------------- *)
(* Cardinality of infinite list and cartesian product types. *)
(* ------------------------------------------------------------------------- *)
(* ------------------------------------------------------------------------- *)
(* Cardinality of the reals. This is done in a rather laborious way to avoid *)
(* any dependence on the theories of analysis. *)
(* ------------------------------------------------------------------------- *)
(* ------------------------------------------------------------------------- *)
(* More about cardinality of lists and restricted powersets etc. *)
(* ------------------------------------------------------------------------- *)
let CARD_LE_LIST = prove
(`!s:A->bool t:B->bool.
s <=_c t
==> {l | !x.
MEM x l ==> x
IN s} <=_c {l | !x.
MEM x l ==> x
IN t}`,
GEN_TAC THEN GEN_TAC THEN REWRITE_TAC[
le_c;
IN_ELIM_THM] THEN
DISCH_THEN(X_CHOOSE_THEN `f:A->B` STRIP_ASSUME_TAC) THEN
EXISTS_TAC `
MAP (f:A->B)` THEN
MATCH_MP_TAC(TAUT `p /\ (p ==> q) ==> p /\ q`) THEN CONJ_TAC THENL
[REWRITE_TAC[
MEM_MAP] THEN ASM_MESON_TAC[]; DISCH_TAC] THEN
ONCE_REWRITE_TAC[
SWAP_FORALL_THM] THEN
LIST_INDUCT_TAC THEN SIMP_TAC[
MAP_EQ_NIL;
MAP] THEN
LIST_INDUCT_TAC THEN REWRITE_TAC[
MAP;
NOT_CONS_NIL;
MEM;
CONS_11] THEN
ASM_MESON_TAC[]);;
let CARD_LE_SUBPOWERSET = prove
(`!s:A->bool t:B->bool.
s <=_c t /\ (!f s. P s ==> Q(
IMAGE f s))
==> {u | u
SUBSET s /\ P u} <=_c {v | v
SUBSET t /\ Q v}`,
REPEAT GEN_TAC THEN REWRITE_TAC[
le_c;
IN_ELIM_THM] THEN DISCH_THEN
(CONJUNCTS_THEN2 (X_CHOOSE_THEN `f:A->B` STRIP_ASSUME_TAC) ASSUME_TAC) THEN
EXISTS_TAC `
IMAGE (f:A->B)` THEN ASM_SIMP_TAC[] THEN ASM SET_TAC[]);;