module Point

/*
  Points of the plane, for coordinate geometry (#1191).

  `point(x, y)` is the point with coordinates x and y, and `xcoord(P)`
  and `ycoord(P)` are the coordinates of P. The representation of
  `Point` is private, so it can change without affecting users.

  The functions below are defined from the coordinates, so
  `expand dist2` (or `midpoint`, `dot`, `cross`) gives the coordinate
  formula, and `algebra` finishes most coordinate proofs:

  * `dist2(A, B)`: the square of the distance from A to B. It avoids
    square roots, so segments AB and CD are congruent,
    `seg_congruent(A, B, C, D)`, when their squared lengths are equal.
  * `dist(A, B)`: the distance from A to B, `sqrt(dist2(A, B))`.
    `point_dist_square` turns it back into `dist2`.
  * `midpoint(A, B)`: ((x₁ + x₂) / 2, (y₁ + y₂) / 2).
  * `dot(O, A, B)`: the dot product of the vectors OA and OB, which is
    zero when the angle AOB is right.
  * `cross(O, A, B)`: the cross product of OA and OB, which is zero
    when O, A, and B are collinear, `collinear(O, A, B)`.
  * `slope(A, B)`: (y₁ - y₂) / (x₁ - x₂), meaningful when
    xcoord(A) ≠ xcoord(B). Lines (lib/Line.pf) work without it.
*/

import UInt
import Int
import Base
public import Rat
public import Real
import Linear
import Ring

opaque union Point {
  point_of(Real, Real)
}

opaque fun point(x : Real, y : Real) { point_of(x, y) }

opaque fun xcoord(P : Point) {
  switch P { case point_of(x, y) { x } }
}

opaque fun ycoord(P : Point) {
  switch P { case point_of(x, y) { y } }
}

// The square of the distance from A to B.
fun dist2(A : Point, B : Point) {
  (xcoord(A) - xcoord(B)) * (xcoord(A) - xcoord(B))
  + (ycoord(A) - ycoord(B)) * (ycoord(A) - ycoord(B))
}

// The distance from A to B.
fun dist(A : Point, B : Point) { sqrt(dist2(A, B)) }

fun midpoint(A : Point, B : Point) {
  point(real(frac(+1, 2)) * (xcoord(A) + xcoord(B)),
        real(frac(+1, 2)) * (ycoord(A) + ycoord(B)))
}

// The dot product of the vectors from O to A and from O to B.
fun dot(O : Point, A : Point, B : Point) {
  (xcoord(A) - xcoord(O)) * (xcoord(B) - xcoord(O))
  + (ycoord(A) - ycoord(O)) * (ycoord(B) - ycoord(O))
}

// The cross product of the vectors from O to A and from O to B.
fun cross(O : Point, A : Point, B : Point) {
  (xcoord(A) - xcoord(O)) * (ycoord(B) - ycoord(O))
  - (ycoord(A) - ycoord(O)) * (xcoord(B) - xcoord(O))
}

fun collinear(A : Point, B : Point, C : Point) {
  cross(A, B, C) = real(+0)
}

// The slope of line AB. It is meaningful only when
// xcoord(A) ≠ xcoord(B), that is, when AB is not vertical.
fun slope(A : Point, B : Point) {
  (ycoord(A) - ycoord(B)) / (xcoord(A) - xcoord(B))
}

// Segments AB and CD are congruent.
fun seg_congruent(A : Point, B : Point, C : Point, D : Point) {
  dist2(A, B) = dist2(C, D)
}

/*
  Coordinates
*/

theorem point_xcoord: all x:Real, y:Real. xcoord(point(x, y)) = x
proof
  arbitrary x:Real, y:Real
  expand point | xcoord.
end

theorem point_ycoord: all x:Real, y:Real. ycoord(point(x, y)) = y
proof
  arbitrary x:Real, y:Real
  expand point | ycoord.
end

auto point_xcoord
auto point_ycoord

theorem point_coords: all P:Point. point(xcoord(P), ycoord(P)) = P
proof
  arbitrary P:Point
  switch P { case point_of(x, y) {
    expand point | xcoord | ycoord.
  } }
end

// Two points are equal when their coordinates are.
theorem point_equal: all P:Point, Q:Point.
  if xcoord(P) = xcoord(Q) and ycoord(P) = ycoord(Q) then P = Q
proof
  arbitrary P:Point, Q:Point
  assume prem
  replace symmetric point_coords[P] | symmetric point_coords[Q]
  replace conjunct 0 of prem | conjunct 1 of prem.
end

/*
  Distance
*/

theorem point_dist2_symmetric: all A:Point, B:Point. dist2(A, B) = dist2(B, A)
proof
  arbitrary A:Point, B:Point
  expand dist2
  algebra
end

theorem point_dist2_self: all A:Point. dist2(A, A) = real(+0)
proof
  arbitrary A:Point
  expand dist2.
end

theorem point_dist2_nonneg: all A:Point, B:Point. real(+0) ≤ dist2(A, B)
proof
  arbitrary A:Point, B:Point
  expand dist2
  algebra with real_square_nonneg[xcoord(A) - xcoord(B)]
             | real_square_nonneg[ycoord(A) - ycoord(B)]
end

lemma square_zero: all x:Real. if x * x = real(+0) then x = real(+0)
proof
  arbitrary x:Real
  assume xx
  cases apply real_mult_to_zero[x, x] to xx
  case z { z }
  case z { z }
end

// The sum of two squares is zero only when both are.
lemma squares_zero: all x:Real, y:Real.
  if x * x + y * y = real(+0) then x = real(+0) and y = real(+0)
proof
  arbitrary x:Real, y:Real
  assume s
  have xx: x * x = real(+0)
    by algebra with real_square_nonneg[x] | real_square_nonneg[y]
  have yy: y * y = real(+0) by replace xx in s
  have x0: x = real(+0) by apply square_zero[x] to xx
  have y0: y = real(+0) by apply square_zero[y] to yy
  (x0, y0)
end

theorem point_dist2_zero: all A:Point, B:Point. (dist2(A, B) = real(+0)) = (A = B)
proof
  arbitrary A:Point, B:Point
  have fwd: if dist2(A, B) = real(+0) then A = B by {
    expand dist2
    assume d
    have z: xcoord(A) - xcoord(B) = real(+0) and ycoord(A) - ycoord(B) = real(+0)
      by apply squares_zero[xcoord(A) - xcoord(B), ycoord(A) - ycoord(B)] to d
    apply point_equal[A, B] to replace real_sub_eq_zero_iff in z
  }
  have bwd: if A = B then dist2(A, B) = real(+0) by {
    assume e
    replace e
    point_dist2_self[B]
  }
  apply iff_equal to fwd, bwd
end

// Distinct points are a positive distance apart.
theorem point_dist2_pos: all A:Point, B:Point.
  if not (A = B) then real(+0) < dist2(A, B)
proof
  arbitrary A:Point, B:Point
  assume ne
  replace real_less_def
  have nz: not (real(+0) = dist2(A, B)) by {
    assume z
    apply ne to replace point_dist2_zero[A, B] in symmetric z
  }
  (point_dist2_nonneg[A, B], nz)
end

theorem point_dist_nonneg: all A:Point, B:Point. real(+0) ≤ dist(A, B)
proof
  arbitrary A:Point, B:Point
  expand dist
  apply real_sqrt_nonneg[dist2(A, B)] to point_dist2_nonneg[A, B]
end

theorem point_dist_square: all A:Point, B:Point. dist(A, B) * dist(A, B) = dist2(A, B)
proof
  arbitrary A:Point, B:Point
  expand dist
  apply real_sqrt_square[dist2(A, B)] to point_dist2_nonneg[A, B]
end

// The distance is the nonnegative number whose square is dist2.
theorem point_dist_unique: all A:Point, B:Point, r:Real.
  if real(+0) ≤ r and r * r = dist2(A, B) then dist(A, B) = r
proof
  arbitrary A:Point, B:Point, r:Real
  assume h
  expand dist
  apply real_sqrt_unique[dist2(A, B), r] to h
end

theorem point_dist_symmetric: all A:Point, B:Point. dist(A, B) = dist(B, A)
proof
  arbitrary A:Point, B:Point
  expand dist
  replace point_dist2_symmetric[A, B].
end

theorem point_dist_self: all A:Point. dist(A, A) = real(+0)
proof
  arbitrary A:Point
  expand dist
  replace point_dist2_self[A]
  real_sqrt_zero
end

theorem point_dist_zero: all A:Point, B:Point. (dist(A, B) = real(+0)) = (A = B)
proof
  arbitrary A:Point, B:Point
  have fwd: if dist(A, B) = real(+0) then A = B by {
    assume z
    have d2: dist2(A, B) = real(+0)
      by { replace symmetric point_dist_square[A, B] | z. }
    replace symmetric point_dist2_zero[A, B]
    d2
  }
  have bwd: if A = B then dist(A, B) = real(+0) by {
    assume e
    replace e
    point_dist_self[B]
  }
  apply iff_equal to fwd, bwd
end

// Congruent segments are segments of equal length.
theorem point_seg_congruent_dist: all A:Point, B:Point, C:Point, D:Point.
  seg_congruent(A, B, C, D) = (dist(A, B) = dist(C, D))
proof
  arbitrary A:Point, B:Point, C:Point, D:Point
  expand seg_congruent
  have bwd: if dist(A, B) = dist(C, D) then dist2(A, B) = dist2(C, D) by {
    assume e
    replace symmetric point_dist_square[A, B] | symmetric point_dist_square[C, D] | e.
  }
  have fwd: if dist2(A, B) = dist2(C, D) then dist(A, B) = dist(C, D) by {
    assume e
    expand dist
    replace e.
  }
  apply iff_equal to fwd, bwd
end

/*
  Midpoint
*/

theorem point_midpoint_symmetric: all A:Point, B:Point.
  midpoint(A, B) = midpoint(B, A)
proof
  arbitrary A:Point, B:Point
  expand midpoint
  replace real_add_commute[xcoord(A), xcoord(B)]
        | real_add_commute[ycoord(A), ycoord(B)].
end

theorem point_midpoint_self: all A:Point. midpoint(A, A) = A
proof
  arbitrary A:Point
  expand midpoint
  have hx: xcoord(A) + xcoord(A) = real(+2) * xcoord(A) by algebra
  have hy: ycoord(A) + ycoord(A) = real(+2) * ycoord(A) by algebra
  replace hx | hy
  point_coords[A]
end

// AM is half of AB, so AM² is a quarter of AB².
theorem point_midpoint_dist2: all A:Point, B:Point.
  real(+4) * dist2(A, midpoint(A, B)) = dist2(A, B)
proof
  arbitrary A:Point, B:Point
  expand midpoint | dist2
  algebra
end

// The midpoint is equidistant from the endpoints.
theorem point_midpoint_equidistant: all A:Point, B:Point.
  dist2(A, midpoint(A, B)) = dist2(midpoint(A, B), B)
proof
  arbitrary A:Point, B:Point
  // MB² is a quarter of AB² too. (A direct `algebra` works but is slow, #1338.)
  have mb: real(+4) * dist2(midpoint(A, B), B) = dist2(A, B) by {
    replace point_dist2_symmetric[midpoint(A, B), B] | point_midpoint_symmetric[A, B]
          | point_dist2_symmetric[A, B]
    point_midpoint_dist2[B, A]
  }
  algebra with point_midpoint_dist2[A, B] | mb
end

/*
  Collinearity
*/

lemma real_neg_zero_iff: all x:Real. (- x = real(+0)) = (x = real(+0))
proof
  arbitrary x:Real
  have fwd: if - x = real(+0) then x = real(+0) by { assume n algebra }
  have bwd: if x = real(+0) then - x = real(+0) by { assume n algebra }
  apply iff_equal to fwd, bwd
end

theorem point_collinear_rotate: all A:Point, B:Point, C:Point.
  collinear(A, B, C) = collinear(B, C, A)
proof
  arbitrary A:Point, B:Point, C:Point
  have e: cross(B, C, A) = cross(A, B, C) by { expand cross algebra }
  expand collinear
  replace e.
end

theorem point_collinear_swap: all A:Point, B:Point, C:Point.
  collinear(A, B, C) = collinear(A, C, B)
proof
  arbitrary A:Point, B:Point, C:Point
  have e: cross(A, C, B) = - cross(A, B, C) by { expand cross algebra }
  expand collinear
  replace e | real_neg_zero_iff.
end

theorem point_collinear_same: all A:Point, B:Point. collinear(A, A, B)
proof
  arbitrary A:Point, B:Point
  expand collinear | cross.
end

// The vectors u = (a, b), v = (c, d), w = (e, f) in the frame of u:
// |u|² (v · w) = (u · v)(u · w) + (u × v)(u × w) and
// |u|² (v × w) = (u · v)(u × w) - (u × v)(u · w).
lemma vec_dot_frame: all a:Real, b:Real, c:Real, d:Real, e:Real, f:Real.
  (a * a + b * b) * (c * e + d * f)
  = (a * c + b * d) * (a * e + b * f) + (a * d - b * c) * (a * f - b * e)
proof
  arbitrary a:Real, b:Real, c:Real, d:Real, e:Real, f:Real
  algebra
end

lemma vec_cross_frame: all a:Real, b:Real, c:Real, d:Real, e:Real, f:Real.
  (a * a + b * b) * (c * f - d * e)
  = (a * c + b * d) * (a * f - b * e) - (a * d - b * c) * (a * e + b * f)
proof
  arbitrary a:Real, b:Real, c:Real, d:Real, e:Real, f:Real
  algebra
end

// The dot and cross products of AC and AD, from those with AB.
theorem point_dot_frame: all A:Point, B:Point, C:Point, D:Point.
  dist2(A, B) * dot(A, C, D)
  = dot(A, B, C) * dot(A, B, D) + cross(A, B, C) * cross(A, B, D)
proof
  arbitrary A:Point, B:Point, C:Point, D:Point
  replace point_dist2_symmetric[A, B]
  expand dist2 | dot | cross
  vec_dot_frame[xcoord(B) - xcoord(A), ycoord(B) - ycoord(A), xcoord(C) - xcoord(A),
                ycoord(C) - ycoord(A), xcoord(D) - xcoord(A), ycoord(D) - ycoord(A)]
end

theorem point_cross_frame: all A:Point, B:Point, C:Point, D:Point.
  dist2(A, B) * cross(A, C, D)
  = dot(A, B, C) * cross(A, B, D) - cross(A, B, C) * dot(A, B, D)
proof
  arbitrary A:Point, B:Point, C:Point, D:Point
  replace point_dist2_symmetric[A, B]
  expand dist2 | dot | cross
  vec_cross_frame[xcoord(B) - xcoord(A), ycoord(B) - ycoord(A), xcoord(C) - xcoord(A),
                  ycoord(C) - ycoord(A), xcoord(D) - xcoord(A), ycoord(D) - ycoord(A)]
end

// Points C and D of line AB (A ≠ B) are collinear with A.
theorem point_collinear_trans: all A:Point, B:Point, C:Point, D:Point.
  if not (A = B) and collinear(A, B, C) and collinear(A, B, D) then collinear(A, C, D)
proof
  arbitrary A:Point, B:Point, C:Point, D:Point
  expand collinear
  assume h
  have e: dist2(A, B) * cross(A, C, D) = real(+0)
    by { replace point_cross_frame[A, B, C, D] | conjunct 1 of h | conjunct 2 of h. }
  have nz: not (dist2(A, B) = real(+0)) by { replace point_dist2_zero[A, B]  conjunct 0 of h }
  replace apply real_mult_zero_iff[dist2(A, B), cross(A, C, D)] to nz in e
end

theorem point_midpoint_collinear: all A:Point, B:Point.
  collinear(A, midpoint(A, B), B)
proof
  arbitrary A:Point, B:Point
  expand collinear | midpoint | cross
  algebra
end

/*
  Slope
*/

theorem point_slope_symmetric: all A:Point, B:Point. slope(A, B) = slope(B, A)
proof
  arbitrary A:Point, B:Point
  expand slope
  switch xcoord(A) = xcoord(B) {
    case true assume e { replace e. }
    case false assume ne {
      have nz: not (xcoord(A) - xcoord(B) = real(+0))
        by { replace real_sub_eq_zero_iff  ne }
      have nz2: not (xcoord(B) - xcoord(A) = real(+0))
        by { assume z  apply ne to symmetric (replace real_sub_eq_zero_iff in z) }
      algebra
    }
  }
end

// A, B, and C are collinear exactly when AB and AC have the same slope.
theorem point_slope_collinear: all A:Point, B:Point, C:Point.
  if not (xcoord(A) = xcoord(B)) and not (xcoord(A) = xcoord(C))
  then collinear(A, B, C) = (slope(A, B) = slope(A, C))
proof
  arbitrary A:Point, B:Point, C:Point
  assume ne
  have nb: not (xcoord(A) - xcoord(B) = real(+0))
    by { replace real_sub_eq_zero_iff  conjunct 0 of ne }
  have nc: not (xcoord(A) - xcoord(C) = real(+0))
    by { replace real_sub_eq_zero_iff  conjunct 1 of ne }
  have e: cross(A, B, C)
        = - ((xcoord(A) - xcoord(B))
             * ((xcoord(A) - xcoord(C)) * (slope(A, B) - slope(A, C))))
    by { expand cross | slope  algebra }
  expand collinear
  replace e | real_neg_zero_iff
        | apply real_mult_zero_iff[xcoord(A) - xcoord(B),
                (xcoord(A) - xcoord(C)) * (slope(A, B) - slope(A, C))] to nb
        | apply real_mult_zero_iff[xcoord(A) - xcoord(C), slope(A, B) - slope(A, C)] to nc
  real_sub_eq_zero_iff
end

/*
  Right angles: the Pythagorean theorem and its converse
*/

// The law of cosines, in coordinates: BC² = AB² + AC² - 2 AB·AC.
theorem point_dist2_dot: all A:Point, B:Point, C:Point.
  dist2(B, C) = dist2(A, B) + dist2(A, C) - real(+2) * dot(A, B, C)
proof
  arbitrary A:Point, B:Point, C:Point
  expand dist2 | dot
  algebra
end

theorem point_pythagoras: all A:Point, B:Point, C:Point.
  if dot(A, B, C) = real(+0) then dist2(B, C) = dist2(A, B) + dist2(A, C)
proof
  arbitrary A:Point, B:Point, C:Point
  assume right
  replace point_dist2_dot[A, B, C] | right.
end

theorem point_pythagoras_converse: all A:Point, B:Point, C:Point.
  if dist2(B, C) = dist2(A, B) + dist2(A, C) then dot(A, B, C) = real(+0)
proof
  arbitrary A:Point, B:Point, C:Point
  assume sq
  algebra with point_dist2_dot[A, B, C]
end

/*
  Congruent segments
*/

theorem point_seg_congruent_refl: all A:Point, B:Point. seg_congruent(A, B, A, B)
proof
  arbitrary A:Point, B:Point
  expand seg_congruent.
end

theorem point_seg_congruent_symm: all A:Point, B:Point, C:Point, D:Point.
  if seg_congruent(A, B, C, D) then seg_congruent(C, D, A, B)
proof
  arbitrary A:Point, B:Point, C:Point, D:Point
  expand seg_congruent
  assume e
  symmetric e
end

theorem point_seg_congruent_trans: all A:Point, B:Point, C:Point, D:Point, E:Point, F:Point.
  if seg_congruent(A, B, C, D) and seg_congruent(C, D, E, F)
  then seg_congruent(A, B, E, F)
proof
  arbitrary A:Point, B:Point, C:Point, D:Point, E:Point, F:Point
  expand seg_congruent
  assume prem
  transitive (conjunct 0 of prem) (conjunct 1 of prem)
end

theorem point_seg_congruent_reverse: all A:Point, B:Point. seg_congruent(A, B, B, A)
proof
  arbitrary A:Point, B:Point
  expand seg_congruent
  point_dist2_symmetric[A, B]
end

// The midpoint divides a segment into two congruent segments.
theorem point_midpoint_seg_congruent: all A:Point, B:Point.
  seg_congruent(A, midpoint(A, B), midpoint(A, B), B)
proof
  arbitrary A:Point, B:Point
  expand seg_congruent
  point_midpoint_equidistant[A, B]
end