(:num)} <= k /\
R z z /\
(!x y. R x y ==> R y x) /\
(!x y z. R x y /\ R y z ==> R x z) /\
(!x y. R (f x) (f y) <=> R x y)
==> ?d. 0 < d /\ d <= k /\ !n. R (
REPEAT STRIP_TAC THEN
SUBGOAL_THEN
`?m. 0 < m /\ m <= k /\ (R:A->A->bool) (
ITER m f z) z`
STRIP_ASSUME_TAC THENL
[MP_TAC(ISPECL [`\n. (R:A->A->bool) (
ITER n f z)`; `0..k`]
CARD_IMAGE_EQ_INJ) THEN
REWRITE_TAC[
FINITE_NUMSEG;
CARD_NUMSEG;
SUB_0] THEN
MATCH_MP_TAC(TAUT `~p /\ (~q ==> r) ==> (p <=> q) ==> r`) THEN
CONJ_TAC THENL
[FIRST_X_ASSUM(MATCH_MP_TAC o MATCH_MP (ARITH_RULE
`c <= k ==> s <= c ==> ~(s = k + 1)`)) THEN
MATCH_MP_TAC
CARD_SUBSET THEN ASM_REWRITE_TAC[] THEN ASM SET_TAC[];
REWRITE_TAC[
NOT_FORALL_THM; NOT_IMP;
LEFT_IMP_EXISTS_THM] THEN
MATCH_MP_TAC
WLOG_LT THEN REWRITE_TAC[] THEN
CONJ_TAC THENL [MESON_TAC[]; ALL_TAC] THEN
MAP_EVERY X_GEN_TAC [`p:num`; `q:num`] THEN
REWRITE_TAC[
IN_NUMSEG] THEN REPEAT STRIP_TAC THEN
EXISTS_TAC `q - p:num` THEN
REPEAT(CONJ_TAC THENL [ASM_ARITH_TAC; ALL_TAC]) THEN
SUBGOAL_THEN
`!d. d <= p
==> (R:A->A->bool) (
ITER (p - d) f z) (
ITER (q - d) f z)`
MP_TAC THENL
[INDUCT_TAC THEN ASM_REWRITE_TAC[
SUB_0] THENL
[SPEC_TAC(`q:num`,`q:num`) THEN INDUCT_TAC THEN
ASM_REWRITE_TAC[
ITER];
DISCH_TAC THEN FIRST_X_ASSUM(MP_TAC o check (is_imp o concl)) THEN
ANTS_TAC THENL [ASM_ARITH_TAC; ALL_TAC] THEN
SUBGOAL_THEN `q - d = SUC(q - SUC d) /\ p - d = SUC(p - SUC d)`
(fun th -> REWRITE_TAC[th]) THENL [ASM_ARITH_TAC; ALL_TAC] THEN
ASM_REWRITE_TAC[
ITER]];
DISCH_THEN(MP_TAC o SPEC `p:num`) THEN
REWRITE_TAC[
LE_REFL;
SUB_REFL;
ITER] THEN ASM_MESON_TAC[]]];
MP_TAC(ISPECL [`R:A->A->bool`; `f:A->A`; `z:A`]
ORDER_EXISTENCE_ITER) THEN
ASM_REWRITE_TAC[] THEN MATCH_MP_TAC
MONO_EXISTS THEN
X_GEN_TAC `d:num` THEN ASM_CASES_TAC `d = 0` THEN ASM_SIMP_TAC[] THEN
DISCH_THEN(MP_TAC o SPEC `m:num`) THEN
ASM_SIMP_TAC[
LE_1; NUMBER_RULE `!n. 0 divides n <=> n = 0`] THEN
DISCH_THEN(MP_TAC o MATCH_MP
DIVIDES_LE) THEN ASM_ARITH_TAC]);;