(`!m n p q r.
~(m = 0) /\ ~(m = 1) /\
((&n / &m) pow 2 + (&p / &m) pow 2 +
(&q / &m) pow 2 + (&r / &m) pow 2 = &N)
==> ?m' n' p' q' r'.
~(m' = 0) /\ m' < m /\
((&n' / &m') pow 2 + (&p' / &m') pow 2 +
(&q' / &m') pow 2 + (&r' / &m') pow 2 = &N)`,
REPEAT STRIP_TAC THEN
MATCH_MP_TAC(TAUT `(~p ==> p) ==> p`) THEN
REWRITE_TAC[
NOT_EXISTS_THM] THEN DISCH_TAC THEN
SUBGOAL_THEN
`?n' p' q' r'.
(&n / &m - &n') pow 2 + (&p / &m - &p') pow 2 +
(&q / &m - &q') pow 2 + (&r / &m - &r') pow 2 < &1 \/
(((&n / &m - &n') pow 2 + (&p / &m - &p') pow 2 +
(&q / &m - &q') pow 2 + (&r / &m - &r') pow 2 = &1) /\
(m = 2) /\ (
EVEN(n' + p' + q' + r') =
EVEN(N)))`
MP_TAC THENL
[ASM_CASES_TAC
`?n' p' q' r'. (&n / &m = &n' + &1 / &2) /\
(&p / &m = &p' + &1 / &2) /\
(&q / &m = &q' + &1 / &2) /\
(&r / &m = &r' + &1 / &2)` THENL
[FIRST_X_ASSUM(CHOOSE_THEN STRIP_ASSUME_TAC) THEN
MAP_EVERY EXISTS_TAC [`n':num`; `p':num`; `q':num`] THEN
SUBGOAL_THEN `m = 2` SUBST_ALL_TAC THENL
[FIRST_X_ASSUM(MP_TAC o SPECL
[`2`; `2 * n' + 1`; `2 * p' + 1`; `2 * q' + 1`; `2 * r' + 1`]) THEN
REWRITE_TAC[
ARITH_EQ; GSYM REAL_OF_NUM_ADD; GSYM REAL_OF_NUM_MUL] THEN
REWRITE_TAC[
real_div;
REAL_ADD_RDISTRIB; GSYM REAL_MUL_ASSOC] THEN
REWRITE_TAC[GSYM
real_div] THEN
SIMP_TAC[
REAL_DIV_LMUL; REAL_OF_NUM_EQ;
ARITH_EQ] THEN
REPEAT(FIRST_X_ASSUM(SUBST1_TAC o SYM)) THEN
REWRITE_TAC[] THEN POP_ASSUM_LIST(MP_TAC o end_itlist CONJ) THEN
ARITH_TAC; ALL_TAC] THEN
SUBGOAL_THEN `(
EVEN(n' + p' + q' + r') <=>
EVEN(N)) \/
(
EVEN(n' + p' + q' + r' + 1) <=>
EVEN(N))`
DISJ_CASES_TAC THENL
[REWRITE_TAC[
EVEN_ADD;
ARITH_EVEN] THEN CONV_TAC TAUT;
EXISTS_TAC `r':num` THEN DISJ2_TAC THEN ASM_REWRITE_TAC[] THEN
REWRITE_TAC[REAL_ARITH `(a + b) - a = b`] THEN
CONV_TAC REAL_RAT_REDUCE_CONV;
EXISTS_TAC `r' + 1` THEN DISJ2_TAC THEN ASM_REWRITE_TAC[] THEN
REWRITE_TAC[REAL_ARITH `(a + b) - a = b`] THEN
REWRITE_TAC[GSYM REAL_OF_NUM_ADD] THEN
REWRITE_TAC[REAL_ARITH `(a + b) - (a + c) = b - c`] THEN
CONV_TAC REAL_RAT_REDUCE_CONV];
ALL_TAC] THEN
MAP_EVERY (fun t -> MP_TAC(SPEC t
REAL_NUM_ROUND))
[`&n / &m`; `&p / &m`; `&q / &m`; `&r / &m`] THEN
SIMP_TAC[
REAL_LE_DIV;
REAL_POS] THEN
MAP_EVERY (fun t -> DISCH_THEN(X_CHOOSE_TAC t))
[`r':num`; `q':num`; `p':num`; `n':num`] THEN
MAP_EVERY EXISTS_TAC [`n':num`; `p':num`; `q':num`; `r':num`] THEN
DISJ1_TAC THEN
MATCH_MP_TAC(REAL_ARITH
`!m. a <= m /\ b <= m /\ c <= m /\ d <= m /\
~((a = m) /\ (b = m) /\ (c = m) /\ (d = m)) /\
&4 * m <= &1
==> a + b + c + d < &1`) THEN
EXISTS_TAC `(&1 / &2) pow 2` THEN
ONCE_REWRITE_TAC[SYM(SPEC `a - b`
REAL_POW2_ABS)] THEN
ASM_SIMP_TAC[
REAL_POW_LE2;
REAL_ABS_POS;
REAL_LE_DIV;
REAL_POS] THEN
CONV_TAC(RAND_CONV REAL_RAT_REDUCE_CONV) THEN REWRITE_TAC[] THEN
REWRITE_TAC[REAL_POW_2; REAL_ARITH
`(a * a = b * b) <=> ((a + b) * (a - b) = &0)`] THEN
REWRITE_TAC[
REAL_ENTIRE] THEN
SIMP_TAC[REAL_ARITH `&0 <= x /\ &0 < y ==> ~(x + y = &0)`;
REAL_ABS_POS;
REAL_LT_DIV;
REAL_OF_NUM_LT; ARITH] THEN
REWRITE_TAC[
REAL_SUB_0] THEN
FIRST_ASSUM(MP_TAC o check (is_neg o concl)) THEN
REWRITE_TAC[TAUT `~b ==> ~a <=> a ==> b`] THEN
DISCH_THEN(REPEAT_TCL CONJUNCTS_THEN
(MP_TAC o MATCH_MP
REAL_RAT_ABS_MIDDLE)) THEN MESON_TAC[];
ALL_TAC] THEN
FIRST_X_ASSUM(K ALL_TAC o check (is_forall o concl)) THEN
REWRITE_TAC[
LEFT_IMP_EXISTS_THM] THEN
MAP_EVERY X_GEN_TAC [`n':num`; `p':num`; `q':num`; `r':num`] THEN
DISCH_TAC THEN
ABBREV_TAC `s = &n - &m * &n'` THEN
ABBREV_TAC `t = &p - &m * &p'` THEN
ABBREV_TAC `u = &q - &m * &q'` THEN
ABBREV_TAC `v = &r - &m * &r'` THEN
ABBREV_TAC `N' = n'
EXP 2 + p'
EXP 2 + q'
EXP 2 + r'
EXP 2` THEN
UNDISCH_TAC `n'
EXP 2 + p'
EXP 2 + q'
EXP 2 + r'
EXP 2 = N'` THEN
DISCH_THEN(ASSUME_TAC o REWRITE_RULE
[GSYM REAL_OF_NUM_EQ; GSYM REAL_OF_NUM_ADD; GSYM
REAL_OF_NUM_POW]) THEN
ABBREV_TAC `M = 2 * (n * n' + p * p' + q * q' + r * r')` THEN
UNDISCH_TAC `2 * (n * n' + p * p' + q * q' + r * r') = M` THEN
DISCH_THEN(ASSUME_TAC o REWRITE_RULE
[GSYM REAL_OF_NUM_EQ; GSYM REAL_OF_NUM_ADD; GSYM REAL_OF_NUM_MUL;
GSYM
REAL_OF_NUM_POW]) THEN
ASM_CASES_TAC `(&n / &m = &n') /\ (&p / &m = &p') /\
(&q / &m = &q') /\ (&r / &m = &r')` THENL
[MAP_EVERY EXISTS_TAC [`1`; `n':num`; `p':num`; `q':num`; `r':num`] THEN
REWRITE_TAC[
ARITH_EQ;
REAL_DIV_1] THEN CONJ_TAC THENL
[UNDISCH_TAC `~(m = 0)` THEN UNDISCH_TAC `~(m = 1)` THEN ARITH_TAC;
UNDISCH_THEN
`(&n / &m) pow 2 + (&p / &m) pow 2 +
(&q / &m) pow 2 + (&r / &m) pow 2 = &N`
(SUBST1_TAC o SYM) THEN
ASM_REWRITE_TAC[]];
ALL_TAC] THEN
SUBGOAL_THEN `&0 < (&n / &m - &n') pow 2 + (&p / &m - &p') pow 2 +
(&q / &m - &q') pow 2 + (&r / &m - &r') pow 2`
MP_TAC THENL
[MATCH_MP_TAC(REAL_ARITH
`&0 <= w /\ &0 <= x /\ &0 <= y /\ &0 <= z /\
~((w = &0) /\ (x = &0) /\ (y = &0) /\ (z = &0))
==> &0 < w + x + y + z`) THEN
REWRITE_TAC[REAL_POW_2;
REAL_ENTIRE;
REAL_LE_SQUARE] THEN
ASM_REWRITE_TAC[
REAL_SUB_0];
ALL_TAC] THEN
FIRST_X_ASSUM(MP_TAC o check (is_disj o concl)) THEN
SUBGOAL_THEN
`(&n / &m - &n') pow 2 + (&p / &m - &p') pow 2 +
(&q / &m - &q') pow 2 + (&r / &m - &r') pow 2 =
(s pow 2 + t pow 2 + u pow 2 + v pow 2) / &m pow 2`
MP_TAC THENL
[MATCH_MP_TAC
REAL_EQ_RCANCEL_IMP THEN EXISTS_TAC `&m pow 2` THEN
ASM_SIMP_TAC[
REAL_POW_EQ_0;
REAL_DIV_RMUL; REAL_OF_NUM_EQ] THEN
REWRITE_TAC[
REAL_ADD_RDISTRIB; GSYM
REAL_POW_MUL;
REAL_SUB_RDISTRIB] THEN
ASM_SIMP_TAC[
REAL_POW_EQ_0;
REAL_DIV_RMUL; REAL_OF_NUM_EQ] THEN
ONCE_REWRITE_TAC[REAL_MUL_SYM] THEN ASM_REWRITE_TAC[]; ALL_TAC] THEN
SUBGOAL_THEN `(&n / &m - &n') pow 2 + (&p / &m - &p') pow 2 +
(&q / &m - &q') pow 2 + (&r / &m - &r') pow 2 =
(&N + &N') - &M / &m`
ASSUME_TAC THENL
[MATCH_MP_TAC
REAL_EQ_RCANCEL_IMP THEN EXISTS_TAC `&m pow 2` THEN
ASM_SIMP_TAC[
REAL_POW_EQ_0;
REAL_DIV_RMUL; REAL_OF_NUM_EQ] THEN
REWRITE_TAC[GSYM(ASSUME `(&n / &m) pow 2 + (&p / &m) pow 2 +
(&q / &m) pow 2 + (&r / &m) pow 2 = &N`);
GSYM(ASSUME `&n' pow 2 + &p' pow 2 + &q' pow 2 + &r' pow 2 = &N'`);
GSYM(ASSUME
`&2 * (&n * &n' + &p * &p' + &q * &q' + &r * &r') = &M`)] THEN
REWRITE_TAC[
REAL_ADD_RDISTRIB; GSYM
REAL_POW_MUL;
REAL_SUB_RDISTRIB] THEN
REWRITE_TAC[REAL_POW_2; REAL_MUL_ASSOC] THEN
SIMP_TAC[
REAL_DIV_RMUL; REAL_OF_NUM_EQ; ASSUME `~(m = 0)`] THEN
REWRITE_TAC[GSYM REAL_MUL_ASSOC] THEN
SIMP_TAC[
REAL_DIV_RMUL; REAL_OF_NUM_EQ; ASSUME `~(m = 0)`] THEN
REAL_ARITH_TAC; ALL_TAC] THEN
ASM_REWRITE_TAC[] THEN
REWRITE_TAC[REAL_ARITH `(a + b) - c < &1 <=> (a + b) - &1 < c`;
REAL_ARITH `((a + b) - c = &1) <=> ((a + b) - &1 = c)`;
REAL_ARITH `&0 < a - b <=> b < a`] THEN
SIMP_TAC[
REAL_LT_LDIV_EQ;
REAL_LT_RDIV_EQ;
REAL_EQ_RDIV_EQ;
REAL_OF_NUM_LT;
ARITH_RULE `0 < n <=> ~(n = 0)`; ASSUME `~(m = 0)`] THEN
REWRITE_TAC[REAL_ARITH `(a - &1) * m < M <=> a * m - M < m`;
REAL_ARITH `((a - &1) * m = M) <=> (a * m - M = m)`] THEN
REPEAT DISCH_TAC THEN
UNDISCH_TAC `(&N + &N') - &M / &m =
(s pow 2 + t pow 2 + u pow 2 + v pow 2) / &m pow 2` THEN
ASM_SIMP_TAC[
REAL_EQ_RDIV_EQ;
REAL_POW_LT;
REAL_OF_NUM_LT;
ARITH_RULE `0 < a <=> ~(a = 0)`] THEN
REWRITE_TAC[REAL_POW_2;
REAL_SUB_RDISTRIB; REAL_MUL_ASSOC] THEN
ASM_SIMP_TAC[
REAL_DIV_RMUL; REAL_OF_NUM_EQ; GSYM REAL_POW_2] THEN
ABBREV_TAC `m':num = (N + N') * m - M` THEN
SUBGOAL_THEN `(&N + &N') * &m - &M = &m'`
(fun th -> SUBST_ALL_TAC th THEN ASSUME_TAC th)
THENL
[EXPAND_TAC "m'" THEN
REWRITE_TAC[REAL_OF_NUM_ADD; REAL_OF_NUM_MUL] THEN
MATCH_MP_TAC
REAL_OF_NUM_SUB THEN
REWRITE_TAC[GSYM REAL_OF_NUM_LE; GSYM REAL_OF_NUM_ADD; GSYM
REAL_OF_NUM_MUL] THEN
ASM_SIMP_TAC[
REAL_LT_IMP_LE]; ALL_TAC] THEN
ASM_REWRITE_TAC[GSYM
REAL_SUB_RDISTRIB] THEN
DISCH_THEN(ASSUME_TAC o GSYM) THEN
SUBGOAL_THEN `~(m' = 0)` ASSUME_TAC THENL
[REWRITE_TAC[GSYM REAL_OF_NUM_EQ] THEN
REWRITE_TAC[GSYM(ASSUME `(&N + &N') * &m - &M = &m'`)] THEN
MATCH_MP_TAC(REAL_ARITH `b < a ==> ~(a - b = &0)`) THEN
ASM_REWRITE_TAC[]; ALL_TAC] THEN
SUBGOAL_THEN
`!z. (&n' + s * z) pow 2 + (&p' + t * z) pow 2 +
(&q' + u * z) pow 2 + (&r' + v * z) pow 2 - &N =
(&m * z - &1) * (&m' * z + &N - &N')`
ASSUME_TAC THENL
[GEN_TAC THEN MATCH_MP_TAC
EQ_TRANS THEN
EXISTS_TAC `&m * &m' * z pow 2 + (&M - &2 * &m * &N') * z + &N' - &N` THEN
CONJ_TAC THENL
[REWRITE_TAC[REAL_POW_2; REAL_ARITH
`(n + s * z) * (n + s * z) + (p + t * z) * (p + t * z) +
(q + u * z) * (q + u * z) + (r + v * z) * (r + v * z) - N =
(s * s + t * t + u * u + v * v) * (z * z) +
(&2 * (n * s + p * t + q * u + r * v)) * z +
((n * n + p * p + q * q + r * r) - N)`] THEN
ASM_REWRITE_TAC[GSYM REAL_POW_2] THEN
MATCH_MP_TAC(REAL_ARITH
`(a = c) /\ (b = d) ==> (a + b + n - m = c + d + n - m)`) THEN
CONJ_TAC THENL [REWRITE_TAC[
REAL_MUL_AC]; ALL_TAC] THEN
AP_THM_TAC THEN AP_TERM_TAC THEN
REWRITE_TAC[GSYM(ASSUME
`&n' pow 2 + &p' pow 2 + &q' pow 2 + &r' pow 2 = &N'`);
GSYM(ASSUME
`&2 * (&n * &n' + &p * &p' + &q * &q' + &r * &r') = &M`)] THEN
MAP_EVERY EXPAND_TAC ["s";