(* ========================================================================== *)
(* FLYSPECK - BOOK FORMALIZATION *)
(* Section: Appendix *)
(* Chapter: Local Fan *)
(* Author: John Harrison *)
(* Date: 2013-07-12 *)
(* ========================================================================== *)
module Pqcsxwg = struct
let mk_simplex1 = new_definition `mk_simplex1 v0 v1 v2 x1 x2 x3 x4 x5 x6 =
(let uinv = &1 / ups_x x1 x2 x6 in
let d = delta_x x1 x2 x3 x4 x5 x6 in
let d5 = delta_x5 x1 x2 x3 x4 x5 x6 in
let d6 = delta_x4 x1 x2 x3 x4 x5 x6 in
let vcross = (v1 - v0) cross (v2 - v0) in
v0 + uinv % ((&2 * sqrt d) % vcross + d5 % (v1 - v0) + d6 % (v2 - v0)))`;;
let PQCSXWG1_concl = `!v0 v1 v2 v3 x1 x2 x3 x4 x5 x6.
&0 < x1 /\ &0 < x2 /\ &0 < x3 /\ &0 < x4 /\ &0 < x5 /\ &0 < x6 /\
~collinear {v0,v1,v2} /\
x1 = dist(v1,v0) pow 2 /\
x2 = dist(v2,v0) pow 2 /\
x6 = dist(v1,v2) pow 2 /\
&0 < delta_x x1 x2 x3 x4 x5 x6 /\
v3 = mk_simplex1 v0 v1 v2 x1 x2 x3 x4 x5 x6 ==>
(x3 = dist(v3,v0) pow 2 /\
x5 = dist(v3,v1) pow 2 /\
x4 = dist(v3,v2) pow 2 /\
(v1 - v0) dot ((v2 - v0) cross (v3 - v0)) > &0)`;;
let PQCSXWG2_concl = `!(v0:real^3) v1 v2 v3 x1 x2 x3 x4 x5 x6.
&0 < x1 /\ &0 < x2 /\ &0 < x3 /\ &0 < x4 /\ &0 < x5 /\ &0 < x6 /\
~collinear {v0,v1,v2} /\
x1 = dist(v1,v0) pow 2 /\
x2 = dist(v2,v0) pow 2 /\
x6 = dist(v1,v2) pow 2 /\
&0 < delta_x x1 x2 x3 x4 x5 x6 /\
v3 = mk_simplex1 v0 v1 v2 x1 x2 x3 x4 x5 x6 ==>
(\q. mk_simplex1 v0 v1 v2 x1 x2 x3 x4 q x6) continuous atreal x5`;;
(* ------------------------------------------------------------------------- *)
(* The main result. *)
(* ------------------------------------------------------------------------- *)
add_translation_invariants [MK_SIMPLEX_TRANSLATION];;
"x2"; "x6"] THEN REWRITE_TAC[DIST_SYM];
EXPAND_TAC "uinv" THEN
ASM_SIMP_TAC[REAL_LT_DIV; REAL_OF_NUM_LT; ARITH]] THEN
REWRITE_TAC[NORM_MUL; REAL_POW_MUL; REAL_POW2_ABS] THEN
ASM_SIMP_TAC[SQRT_POW_2; REAL_LT_IMP_LE] THEN
REWRITE_TAC[REAL_ARITH `x * &2 pow 2 * y = &4 * x * y`] THEN
REWRITE_TAC[NORM_POW_2; VECTOR_ARITH
`(a + b) dot (a + b:real^3) = a dot a + b dot b + &2 * a dot b`] THEN
REWRITE_TAC[DOT_LMUL] THEN REWRITE_TAC[DOT_RMUL] THEN
ONCE_REWRITE_TAC[REAL_ARITH
`x3:real = a + b /\ x5 = a + c /\ x4 = a + d <=>
x3 = a + b /\ x3 - x5 = b - c /\ x3 - x4 = b - d`] THEN
REWRITE_TAC[REAL_ARITH
`(b * b * x + c * c * y + &2 * b * c * z) -
((b - &1) * (b - &1) * x + c * c * y + &2 * (b - &1) * c * z) =
(&2 * b - &1) * x + &2 * c * z /\
(b * b * x + c * c * y + &2 * b * c * z) -
(b * b * x + (c - &1) * (c - &1) * y + &2 * b * (c - &1) * z) =
(&2 * c - &1) * y + &2 * b * z`] THEN
RULE_ASSUM_TAC(REWRITE_RULE[DIST_0]) THEN
ASM_REWRITE_TAC[GSYM NORM_POW_2; REAL_ARITH
`x = (&2 * b - &1) * y + &2 * c * z <=>
b * y + c * z = (y + x) / &2`] THEN
EXPAND_TAC "vcross" THEN REWRITE_TAC[NORM_POW_2; DOT_CROSS] THEN
ASM_REWRITE_TAC[GSYM NORM_POW_2] THEN
SUBST1_TAC(VECTOR_ARITH `(v2:real^3) dot v1 = v1 dot v2`) THEN
REWRITE_TAC[GSYM REAL_POW_2] THEN
SUBGOAL_THEN `(v1:real^3) dot v2 = ((x1 + x2) - x6) / &2` SUBST1_TAC THENL
[MAP_EVERY EXPAND_TAC ["x1"; "x2"; "x6"] THEN
REWRITE_TAC[dist; NORM_POW_2; DOT_RSUB; DOT_LSUB] THEN
REWRITE_TAC[DOT_SYM] THEN REAL_ARITH_TAC;
ALL_TAC] THEN
REWRITE_TAC[REAL_ARITH
`(&4 * u pow 2 * d) * x + (u * e) * (u * e) * y + (u * f) * (u * f) * z +
&2 * (u * e) * (u * f) * j =
u pow 2 * (&4 * d * x + e pow 2 * y + f pow 2 * z + &2 * e * f * j)`] THEN
REWRITE_TAC[REAL_ARITH
`(u * d) * x + (u * e) * y:real = z <=> u * (d * x + e * y) = z`] THEN
EXPAND_TAC "uinv" THEN MATCH_MP_TAC(REAL_FIELD
`&0 < u /\
u pow 2 * x = y /\ u * a = b /\ u * c = d
==> x = (&1 / u) pow 2 * y /\
(&1 / u) * b = a /\ (&1 / u) * d = c`) THEN
ASM_REWRITE_TAC[] THEN MAP_EVERY EXPAND_TAC ["uinv"; "d"; "d5"; "d6"] THEN
REPEAT(FIRST_X_ASSUM(MP_TAC o MATCH_MP REAL_LT_IMP_NZ)) THEN
REWRITE_TAC[Nonlin_def.delta_x5; Nonlin_def.delta_x4] THEN
REWRITE_TAC[Sphere.ups_x; Sphere.delta_x] THEN CONV_TAC REAL_RING);;
end;;