(* ========================================================================= *)
(* Inclusion-exclusion principle, the usual and generalized forms. *)
(* ========================================================================= *)
(* ------------------------------------------------------------------------- *)
(* Simple set theory lemmas. *)
(* ------------------------------------------------------------------------- *)
(* ------------------------------------------------------------------------- *)
(* Versions for additive real functions, where the additivity applies only *)
(* to some specific subsets (e.g. cardinality of finite sets, measurable *)
(* sets with bounded measure). *)
(* ------------------------------------------------------------------------- *)
(* ------------------------------------------------------------------------- *)
(* Versions for unrestrictedly additive functions. *)
(* ------------------------------------------------------------------------- *)
(* ------------------------------------------------------------------------- *)
(* Special case of cardinality, the most common case. *)
(* ------------------------------------------------------------------------- *)
(* ------------------------------------------------------------------------- *)
(* A more conventional form. *)
(* ------------------------------------------------------------------------- *)
(* ------------------------------------------------------------------------- *)
(* A combinatorial lemma about subsets of a finite set. *)
(* ------------------------------------------------------------------------- *)
(* ------------------------------------------------------------------------- *)
(* Hence a general "Moebius inversion" inclusion-exclusion principle. *)
(* This "symmetric" form is from Ira Gessel: "Symmetric Inclusion-Exclusion" *)
(* ------------------------------------------------------------------------- *)
(* ------------------------------------------------------------------------- *)
(* The more typical non-symmetric version. *)
(* ------------------------------------------------------------------------- *)
let INCLUSION_EXCLUSION_MOBIUS = prove
(`!f g:(A->bool)->real.
(!s. FINITE s ==> g(s) = sum {t | t SUBSET s} f)
==> !s. FINITE s
==> f(s) = sum {t | t SUBSET s}
(\t. (-- &1) pow (CARD s - CARD t) * g(t))`,
REPEAT STRIP_TAC THEN
MP_TAC(ISPECL [`\t. -- &1 pow CARD(t:A->bool) * f t`; `g:(A->bool)->real`]
INCLUSION_EXCLUSION_SYMMETRIC) THEN
REWRITE_TAC[REAL_MUL_ASSOC; GSYM REAL_POW_ADD] THEN ANTS_TAC THENL
[ASM_SIMP_TAC[EVEN_ADD; REAL_POW_ONE; REAL_POW_NEG; REAL_MUL_LID; ETA_AX];
ALL_TAC] THEN
DISCH_THEN(MP_TAC o SPEC `s:A->bool`) THEN ASM_REWRITE_TAC[] THEN
DISCH_THEN(MP_TAC o AP_TERM `(*) ((-- &1) pow (CARD(s:A->bool)))`) THEN
REWRITE_TAC[REAL_MUL_ASSOC; GSYM REAL_POW_ADD; GSYM MULT_2] THEN
REWRITE_TAC[GSYM REAL_POW_POW] THEN CONV_TAC REAL_RAT_REDUCE_CONV THEN
REWRITE_TAC[REAL_POW_ONE; REAL_MUL_LID] THEN DISCH_THEN SUBST1_TAC THEN
REWRITE_TAC[GSYM SUM_LMUL] THEN MATCH_MP_TAC SUM_EQ THEN
X_GEN_TAC `u:A->bool` THEN REWRITE_TAC[IN_ELIM_THM; REAL_MUL_ASSOC] THEN
ASM_SIMP_TAC[REAL_POW_SUB; REAL_ARITH `~(-- &1 = &0)`; CARD_SUBSET] THEN
REWRITE_TAC[REAL_POW_NEG; REAL_POW_ONE] THEN
REPEAT(COND_CASES_TAC THEN ASM_REWRITE_TAC[]) THEN REAL_ARITH_TAC);;
(* ------------------------------------------------------------------------- *)
(* A related principle for real functions. *)
(* ------------------------------------------------------------------------- *)
(*** Not clear how useful this is
needs "Library/products.ml";;
let INCLUSION_EXCLUSION_REAL_FUNCTION = prove
(`!f s:A->bool.
FINITE s
==> product s (\x. &1 - f x) =
sum {t | t SUBSET s} (\t. (-- &1) pow (CARD t) * product t f)`,
let lemma = prove
(`{t | ?u. u SUBSET s /\ t = x INSERT u} =
IMAGE (\s. x INSERT s) {t | t SUBSET s}`,
GEN_REWRITE_TAC I [EXTENSION] THEN REWRITE_TAC[IN_ELIM_THM; IN_IMAGE] THEN
MESON_TAC[]) in
GEN_TAC THEN MATCH_MP_TAC FINITE_INDUCT_STRONG THEN
SIMP_TAC[PRODUCT_CLAUSES; SUBSET_EMPTY; SUM_SING; CARD_CLAUSES; real_pow;
SET_RULE `{x | x = a} = {a}`; REAL_MUL_RID] THEN
REWRITE_TAC[SUBSET_INSERT_EXISTS] THEN
MAP_EVERY X_GEN_TAC [`x:A`; `s:A->bool`] THEN STRIP_TAC THEN
REWRITE_TAC[SET_RULE `{t | p t \/ q t} = {t | p t} UNION {t | q t}`] THEN
W(MP_TAC o PART_MATCH (lhand o rand) SUM_UNION o rand o snd) THEN
ANTS_TAC THENL
[ASM_SIMP_TAC[FINITE_POWERSET; lemma; FINITE_IMAGE] THEN
REWRITE_TAC[GSYM lemma] THEN ASM SET_TAC[];
ALL_TAC] THEN
DISCH_THEN SUBST1_TAC THEN
REWRITE_TAC[GSYM SUM_LMUL; REAL_SUB_RDISTRIB; REAL_MUL_LID; real_sub] THEN
AP_TERM_TAC THEN REWRITE_TAC[lemma] THEN
W(MP_TAC o PART_MATCH (lhand o rand) SUM_IMAGE o rand o snd) THEN
ANTS_TAC THENL [ASM SET_TAC[]; DISCH_THEN SUBST1_TAC] THEN
REWRITE_TAC[GSYM SUM_NEG] THEN MATCH_MP_TAC SUM_EQ THEN
SIMP_TAC[o_THM; IN_ELIM_THM] THEN X_GEN_TAC `t:A->bool` THEN STRIP_TAC THEN
SUBGOAL_THEN `FINITE(t:A->bool) /\ ~(x IN t)` STRIP_ASSUME_TAC THENL
[ASM_MESON_TAC[SUBSET; FINITE_SUBSET]; ALL_TAC] THEN
ASM_SIMP_TAC[PRODUCT_CLAUSES; CARD_CLAUSES; real_pow] THEN REAL_ARITH_TAC);;
***)