module Trapezoid

/*
  Trapezoids, for coordinate geometry (#1191).

  * `trapezoid(A, B, C, D)`: a quadrilateral with exactly one pair of
    parallel sides, the bases AB and CD (AB ∥ CD, and the legs BC and
    DA are not parallel). So a parallelogram is not a trapezoid. With
    "at least one pair", a parallelogram whose legs are congruent (that
    is, every parallelogram) would be an isosceles trapezoid, and its
    base angles are not congruent unless it is a rectangle.
  * `isosceles_trapezoid(A, B, C, D)`: a trapezoid whose legs BC and DA
    are congruent.

  Theorems: the base angles of an isosceles trapezoid are congruent
  (`isosceles_trapezoid_base_angles_theorem`), its diagonals are
  congruent (`isosceles_trapezoid_diagonals_theorem`), and the
  midsegment of a trapezoid is parallel to the bases and half as long as
  their sum (`trapezoid_midsegment_theorem`).

  How they are proved: in the frame of AB, with U = AB², P = AB · AC,
  Q = AB · AD, and K = AB × AC = AB × AD (the bases are parallel), the
  congruent legs give (P - U)² = Q², and the legs are not parallel
  exactly when P - U ≠ Q, so P + Q = U. That is the base angle
  condition (BA · BC = AD · AB) and makes AC² = BD². For the
  midsegment MN, 2 MN = AB + DC as vectors, and AB · DC > 0 because the
  quadrilateral is convex.
*/

import Base
public import Quadrilateral
import Linear
import Ring

// ABCD is a trapezoid with bases AB and CD and legs BC and DA: a
// quadrilateral with exactly one pair of parallel sides.
fun trapezoid(A : Point, B : Point, C : Point, D : Point) {
  quadrilateral(A, B, C, D) and parallel(line(A, B), line(C, D))
  and not parallel(line(B, C), line(D, A))
}

// ABCD is an isosceles trapezoid: a trapezoid whose legs are congruent.
fun isosceles_trapezoid(A : Point, B : Point, C : Point, D : Point) {
  trapezoid(A, B, C, D) and seg_congruent(B, C, D, A)
}

// The bases AB and CD of a trapezoid are parallel: AB × AC = AB × AD.
lemma trapezoid_cross: all A:Point, B:Point, C:Point, D:Point.
  if quadrilateral(A, B, C, D) and parallel(line(A, B), line(C, D))
  then seg_cross(A, B, C, D) = real(+0) and cross(A, B, C) = cross(A, B, D)
proof
  arbitrary A:Point, B:Point, C:Point, D:Point
  assume h
  have ds: not (A = B) and not (B = C) and not (C = D) and not (D = A)
           and not (A = C) and not (B = D) by apply quad_distinct to conjunct 0 of h
  have z: seg_cross(A, B, C, D) = real(+0)
    by replace apply line_parallel_seg_cross[A, B, C, D] to (conjunct 0 of ds), (conjunct 2 of ds)
       in conjunct 1 of h
  have e: seg_cross(A, B, C, D) = cross(A, B, D) - cross(A, B, C) by { expand seg_cross | cross  algebra }
  (z, (conclude cross(A, B, C) = cross(A, B, D) by algebra with z | e))
end

/*
  Isosceles trapezoids
*/

// With U = AB², P = AB · AC, Q = AB · AD, K = AB × AC = AB × AD,
// G = AC², V = AD², and X = AC × AD: if U ≠ 0, the legs are congruent
// (G - 2P + U = V), and they are not parallel (K ≠ X), then P + Q = U.
lemma isosceles_scalar: all U:Real, P:Real, Q:Real, K:Real, G:Real, V:Real, X:Real.
  if U * G = P * P + K * K and U * V = Q * Q + K * K and U * X = P * K - K * Q
     and G - real(+2) * P + U = V and not (K - X = real(+0)) and not (U = real(+0))
  then P + Q = U
proof
  arbitrary U:Real, P:Real, Q:Real, K:Real, G:Real, V:Real, X:Real
  assume h
  // (P - U)² - Q² = U (G - 2P + U - V) = 0 ...
  have sq: (P - U - Q) * (P - U + Q) = real(+0) by {
    have e: (P - U - Q) * (P - U + Q)
            = (P * P + K * K - U * G) - (Q * Q + K * K - U * V) + U * (G - real(+2) * P + U - V)
      by algebra
    have l: G - real(+2) * P + U - V = real(+0) by { replace conjunct 3 of h. }
    replace e | conjunct 0 of h | conjunct 1 of h | l.
  }
  // ... and P - U - Q ≠ 0, because U (K - X) = K (U - P + Q) ≠ 0.
  have nz: not (P - U - Q = real(+0)) by {
    assume z
    have e: U * (K - X) = - (K * (P - U - Q)) + (P * K - K * Q - U * X) by algebra
    have ux: U * (K - X) = real(+0) by {
      replace e | z | conjunct 2 of h.
    }
    apply (conjunct 4 of h) to replace apply real_mult_zero_iff[U, K - X] to conjunct 5 of h in ux
  }
  have z: P - U + Q = real(+0)
    by replace apply real_mult_zero_iff[P - U - Q, P - U + Q] to nz in sq
  algebra with z
end

// In an isosceles trapezoid, AB · AC + AB · AD = AB².
lemma isosceles_trapezoid_dot: all A:Point, B:Point, C:Point, D:Point.
  if isosceles_trapezoid(A, B, C, D) then dot(A, B, C) + dot(A, B, D) = dist2(A, B)
proof
  arbitrary A:Point, B:Point, C:Point, D:Point
  assume h
  have t: quadrilateral(A, B, C, D) and parallel(line(A, B), line(C, D))
          and not parallel(line(B, C), line(D, A))
    by expand trapezoid in conjunct 0 of expand isosceles_trapezoid in h
  have leg: dist2(B, C) = dist2(D, A)
    by expand seg_congruent in conjunct 1 of expand isosceles_trapezoid in h
  have ds: not (A = B) and not (B = C) and not (C = D) and not (D = A)
           and not (A = C) and not (B = D) by apply quad_distinct to conjunct 0 of t
  have k: cross(A, B, C) = cross(A, B, D)
    by conjunct 1 of apply trapezoid_cross to conjunct 0 of t, conjunct 1 of t
  // The legs are not parallel: BC × DA = AB × AD - AC × AD ≠ 0.
  have sc: seg_cross(B, C, D, A) = cross(A, B, C) - cross(A, C, D) by {
    replace k
    expand seg_cross | cross
    algebra
  }
  have np: not (cross(A, B, C) - cross(A, C, D) = real(+0)) by {
    assume z
    apply (conjunct 2 of t) to (conclude parallel(line(B, C), line(D, A)) by {
      replace apply line_parallel_seg_cross[B, C, D, A] to (conjunct 1 of ds), (conjunct 3 of ds) | sc
      z
    })
  }
  have cc: dot(A, C, C) = dist2(A, C) by { expand dot | dist2  algebra }
  have dd: dot(A, D, D) = dist2(A, D) by { expand dot | dist2  algebra }
  have u0: not (dist2(A, B) = real(+0)) by { replace point_dist2_zero[A, B]  conjunct 0 of ds }
  apply isosceles_scalar[dist2(A, B), dot(A, B, C), dot(A, B, D), cross(A, B, C), dist2(A, C),
                         dist2(A, D), cross(A, C, D)]
    to (replace cc in point_dot_frame[A, B, C, C]),
       (replace dd | symmetric k in point_dot_frame[A, B, D, D]),
       (replace symmetric k in point_cross_frame[A, B, C, D]),
       (conclude dist2(A, C) - real(+2) * dot(A, B, C) + dist2(A, B) = dist2(A, D)
          by algebra with leg | point_dist2_dot[A, B, C] | point_dist2_symmetric[D, A]),
       np, u0
end

// The base angles at A and B of an isosceles trapezoid are congruent.
lemma isosceles_trapezoid_base: all A:Point, B:Point, C:Point, D:Point.
  if isosceles_trapezoid(A, B, C, D) then m_angle(D, A, B) = m_angle(A, B, C)
proof
  arbitrary A:Point, B:Point, C:Point, D:Point
  assume h
  have t: quadrilateral(A, B, C, D) and parallel(line(A, B), line(C, D))
          and not parallel(line(B, C), line(D, A))
    by expand trapezoid in conjunct 0 of expand isosceles_trapezoid in h
  have k: cross(A, B, C) = cross(A, B, D)
    by conjunct 1 of apply trapezoid_cross to conjunct 0 of t, conjunct 1 of t
  have s: dot(A, B, C) + dot(A, B, D) = dist2(A, B) by apply isosceles_trapezoid_dot to h
  // BA · BC = AB² - AB · AC = AB · AD, and BA × BC = AD × AB.
  have d1: dot(B, A, C) = dist2(A, B) - dot(A, B, C) by { expand dot | dist2  algebra }
  have d2: dot(A, D, B) = dot(A, B, D) by { expand dot  algebra }
  have c1: cross(B, A, C) = - cross(A, B, C) by { expand cross  algebra }
  have c2: cross(A, D, B) = - cross(A, B, D) by { expand cross  algebra }
  apply angle_measure_dot_cross[D, A, B, A, B, C]
    to (conclude dot(A, D, B) = dot(B, A, C) by algebra with s | d1 | d2),
       (conclude abs(cross(A, D, B)) = abs(cross(B, A, C)) by { replace c1 | c2 | k. })
end

// An isosceles trapezoid listed from C instead of A.
lemma isosceles_trapezoid_rotate: all A:Point, B:Point, C:Point, D:Point.
  if isosceles_trapezoid(A, B, C, D) then isosceles_trapezoid(C, D, A, B)
proof
  arbitrary A:Point, B:Point, C:Point, D:Point
  assume h
  have t: quadrilateral(A, B, C, D) and parallel(line(A, B), line(C, D))
          and not parallel(line(B, C), line(D, A))
    by expand trapezoid in conjunct 0 of expand isosceles_trapezoid in h
  have q: quadrilateral(C, D, A, B)
    by replace quad_rotate[A, B, C, D] | quad_rotate[B, C, D, A] in conjunct 0 of t
  have np: not parallel(line(D, A), line(B, C)) by {
    assume p
    apply (conjunct 2 of t) to apply line_parallel_symmetric to p
  }
  have tr: trapezoid(C, D, A, B) by {
    expand trapezoid
    (q, (apply line_parallel_symmetric to conjunct 1 of t), np)
  }
  expand isosceles_trapezoid
  (tr, (apply point_seg_congruent_symm to conjunct 1 of expand isosceles_trapezoid in h))
end

// The Isosceles Trapezoid Base Angles Theorem: each pair of base angles
// of an isosceles trapezoid is congruent.
theorem isosceles_trapezoid_base_angles_theorem: all A:Point, B:Point, C:Point, D:Point.
  if isosceles_trapezoid(A, B, C, D)
  then angle_congruent(D, A, B, A, B, C) and angle_congruent(B, C, D, C, D, A)
proof
  arbitrary A:Point, B:Point, C:Point, D:Point
  assume h
  expand angle_congruent
  ((apply isosceles_trapezoid_base to h),
   (apply isosceles_trapezoid_base to apply isosceles_trapezoid_rotate to h))
end

// The Isosceles Trapezoid Diagonals Theorem: the diagonals of an
// isosceles trapezoid are congruent.
theorem isosceles_trapezoid_diagonals_theorem: all A:Point, B:Point, C:Point, D:Point.
  if isosceles_trapezoid(A, B, C, D) then seg_congruent(A, C, B, D)
proof
  arbitrary A:Point, B:Point, C:Point, D:Point
  assume h
  have leg: dist2(B, C) = dist2(D, A)
    by expand seg_congruent in conjunct 1 of expand isosceles_trapezoid in h
  have s: dot(A, B, C) + dot(A, B, D) = dist2(A, B) by apply isosceles_trapezoid_dot to h
  // AC² = AD² + 2 AB · AC - AB² and BD² = AB² + AD² - 2 AB · AD.
  expand seg_congruent
  algebra with leg | s | point_dist2_dot[A, B, C] | point_dist2_dot[A, B, D]
             | point_dist2_symmetric[D, A]
end

/*
  The midsegment
*/

// With U = AB², K = AB × AC = AB × AD, X = AC × AD, P = AB · AC, and
// Q = AB · AD: if U > 0, K X > 0, and U X = P K - K Q, then P > Q.
lemma trapezoid_pos: all U:Real, K:Real, X:Real, P:Real, Q:Real.
  if real(+0) < U and real(+0) < K * X and U * X = P * K - K * Q then real(+0) < P - Q
proof
  arbitrary U:Real, K:Real, X:Real, P:Real, Q:Real
  assume h
  have p: real(+0) < U * (K * X) by apply real_mult_pos to conjunct 0 of h, conjunct 1 of h
  have e: U * (K * X) = (K * K) * (P - Q) by {
    have i: U * (K * X) = (U * X) * K by algebra
    replace i | conjunct 2 of h
    algebra
  }
  cases real_dichotomy[P - Q, real(+0)]
  case le {
    have n: real(+0) ≤ (K * K) * (Q - P)
      by apply real_mult_nonneg to real_square_nonneg[K], (conclude real(+0) ≤ Q - P by algebra with le)
    have i: (K * K) * (Q - P) = - ((K * K) * (P - Q)) by algebra
    conclude false by algebra with p | e | n | i
  }
  case gt { gt }
end

// The vectors n = MN, u = AB, and w = CD with 2 n = u - w.
lemma midsegment_scalar: all n1:Real, n2:Real, u1:Real, u2:Real, w1:Real, w2:Real.
  if real(+2) * n1 = u1 - w1 and real(+2) * n2 = u2 - w2
  then real(+4) * (n1 * n1 + n2 * n2)
       = (u1 * u1 + u2 * u2) + (w1 * w1 + w2 * w2) - real(+2) * (u1 * w1 + u2 * w2)
   and real(+2) * (n1 * u2 - n2 * u1) = u1 * w2 - u2 * w1
   and (u1 * u1 + u2 * u2) * (w1 * w1 + w2 * w2)
       = (u1 * w1 + u2 * w2) * (u1 * w1 + u2 * w2) + (u1 * w2 - u2 * w1) * (u1 * w2 - u2 * w1)
proof
  arbitrary n1:Real, n2:Real, u1:Real, u2:Real, w1:Real, w2:Real
  assume h
  have e1: real(+4) * (n1 * n1 + n2 * n2) = (real(+2) * n1) * (real(+2) * n1) + (real(+2) * n2) * (real(+2) * n2)
    by algebra
  have e2: real(+2) * (n1 * u2 - n2 * u1) = (real(+2) * n1) * u2 - (real(+2) * n2) * u1 by algebra
  ((conclude real(+4) * (n1 * n1 + n2 * n2)
             = (u1 * u1 + u2 * u2) + (w1 * w1 + w2 * w2) - real(+2) * (u1 * w1 + u2 * w2) by {
      replace e1 | conjunct 0 of h | conjunct 1 of h
      algebra
    }),
   (conclude real(+2) * (n1 * u2 - n2 * u1) = u1 * w2 - u2 * w1 by {
      replace e2 | conjunct 0 of h | conjunct 1 of h
      algebra
    }),
   (conclude (u1 * u1 + u2 * u2) * (w1 * w1 + w2 * w2)
             = (u1 * w1 + u2 * w2) * (u1 * w1 + u2 * w2) + (u1 * w2 - u2 * w1) * (u1 * w2 - u2 * w1)
      by algebra))
end

// The Trapezoid Midsegment Theorem: the segment joining the midpoints of
// the legs of a trapezoid is parallel to the bases, and its length is
// half the sum of their lengths.
theorem trapezoid_midsegment_theorem: all A:Point, B:Point, C:Point, D:Point.
  if trapezoid(A, B, C, D)
  then parallel(line(midpoint(A, D), midpoint(B, C)), line(A, B))
   and parallel(line(midpoint(A, D), midpoint(B, C)), line(C, D))
   and real(+2) * dist(midpoint(A, D), midpoint(B, C)) = dist(A, B) + dist(C, D)
proof
  arbitrary A:Point, B:Point, C:Point, D:Point
  assume h
  have t: quadrilateral(A, B, C, D) and parallel(line(A, B), line(C, D))
          and not parallel(line(B, C), line(D, A)) by expand trapezoid in h
  have ds: not (A = B) and not (B = C) and not (C = D) and not (D = A)
           and not (A = C) and not (B = D) by apply quad_distinct to conjunct 0 of t
  have tc: seg_cross(A, B, C, D) = real(+0) and cross(A, B, C) = cross(A, B, D)
    by apply trapezoid_cross to conjunct 0 of t, conjunct 1 of t
  have k: cross(A, B, C) = cross(A, B, D) by conjunct 1 of tc
  have up: real(+0) < dist2(A, B) by apply point_dist2_pos to conjunct 0 of ds
  // The bases point the same way: AB · CD = AB · AD - AB · AC < 0.
  have sdneg: seg_dot(A, B, C, D) < real(+0) by {
    have bcd: cross(B, C, D) = cross(A, C, D) + cross(A, B, C) - cross(A, B, D)
      by { expand cross  algebra }
    have kx: real(+0) < cross(A, B, C) * cross(A, C, D) by {
      have c: cross(B, C, D) = cross(A, C, D) by algebra with bcd | k
      replace symmetric c
      conjunct 0 of apply quad_cross to conjunct 0 of t
    }
    have p: real(+0) < dot(A, B, C) - dot(A, B, D)
      by apply trapezoid_pos[dist2(A, B), cross(A, B, C), cross(A, C, D), dot(A, B, C), dot(A, B, D)]
         to up, kx, (replace symmetric k in point_cross_frame[A, B, C, D])
    have sd: seg_dot(A, B, C, D) = dot(A, B, D) - dot(A, B, C) by { expand seg_dot | dot  algebra }
    algebra with p | sd
  }
  define M = midpoint(A, D)
  define N = midpoint(B, C)
  // As vectors, 2 MN = AB - CD.
  have hx: real(+2) * (xcoord(N) - xcoord(M)) = (xcoord(B) - xcoord(A)) - (xcoord(D) - xcoord(C))
    by { expand M | N | midpoint  algebra }
  have hy: real(+2) * (ycoord(N) - ycoord(M)) = (ycoord(B) - ycoord(A)) - (ycoord(D) - ycoord(C))
    by { expand M | N | midpoint  algebra }
  have ms: real(+4) * dist2(N, M) = dist2(B, A) + dist2(D, C) - real(+2) * seg_dot(A, B, C, D)
           and real(+2) * seg_cross(M, N, A, B) = seg_cross(A, B, C, D)
           and dist2(B, A) * dist2(D, C)
               = seg_dot(A, B, C, D) * seg_dot(A, B, C, D) + seg_cross(A, B, C, D) * seg_cross(A, B, C, D)
  by {
    expand dist2 | seg_dot | seg_cross
    apply midsegment_scalar[xcoord(N) - xcoord(M), ycoord(N) - ycoord(M),
                            xcoord(B) - xcoord(A), ycoord(B) - ycoord(A),
                            xcoord(D) - xcoord(C), ycoord(D) - ycoord(C)] to hx, hy
  }
  have mn: not (M = N) by {
    assume e
    have z: dist2(N, M) = real(+0) by { replace e  point_dist2_self[N] }
    conclude false
      by algebra with conjunct 0 of ms | z | up | sdneg | point_dist2_nonneg[D, C]
                    | point_dist2_symmetric[B, A]
  }
  have par: parallel(line(M, N), line(A, B)) by {
    replace apply line_parallel_seg_cross[M, N, A, B] to mn, conjunct 0 of ds
    algebra with conjunct 1 of ms | conjunct 0 of tc
  }
  // AB · DC = -(AB · CD), since AB ∥ CD.
  have ab_dc: dist(A, B) * dist(D, C) = - seg_dot(A, B, C, D) by {
    have i: (dist(A, B) * dist(D, C)) * (dist(A, B) * dist(D, C))
            = (dist(A, B) * dist(A, B)) * (dist(D, C) * dist(D, C)) by algebra
    have j: (- seg_dot(A, B, C, D)) * (- seg_dot(A, B, C, D))
            = seg_dot(A, B, C, D) * seg_dot(A, B, C, D) by algebra
    apply real_square_injective[dist(A, B) * dist(D, C), - seg_dot(A, B, C, D)]
      to (apply real_mult_nonneg to point_dist_nonneg[A, B], point_dist_nonneg[D, C]),
         (conclude real(+0) ≤ - seg_dot(A, B, C, D) by algebra with sdneg),
         (conclude (dist(A, B) * dist(D, C)) * (dist(A, B) * dist(D, C))
                   = (- seg_dot(A, B, C, D)) * (- seg_dot(A, B, C, D)) by {
            replace i | point_dist_square[A, B] | point_dist_square[D, C] | j
                  | point_dist2_symmetric[A, B] | conjunct 2 of ms | conjunct 0 of tc.
          })
  }
  have mlen: real(+2) * dist(M, N) = dist(A, B) + dist(C, D) by {
    have i1: (real(+2) * dist(M, N)) * (real(+2) * dist(M, N)) = real(+4) * (dist(M, N) * dist(M, N))
      by algebra
    have i2: (dist(A, B) + dist(D, C)) * (dist(A, B) + dist(D, C))
             = dist(A, B) * dist(A, B) + real(+2) * (dist(A, B) * dist(D, C)) + dist(D, C) * dist(D, C)
      by algebra
    replace point_dist_symmetric[C, D]
    apply real_square_injective[real(+2) * dist(M, N), dist(A, B) + dist(D, C)]
      to (conclude real(+0) ≤ real(+2) * dist(M, N) by algebra with point_dist_nonneg[M, N]),
         (conclude real(+0) ≤ dist(A, B) + dist(D, C)
            by algebra with point_dist_nonneg[A, B] | point_dist_nonneg[D, C]),
         (conclude (real(+2) * dist(M, N)) * (real(+2) * dist(M, N))
                   = (dist(A, B) + dist(D, C)) * (dist(A, B) + dist(D, C))
            by algebra with i1 | i2 | conjunct 0 of ms | ab_dc | point_dist_square[M, N]
                          | point_dist_square[A, B] | point_dist_square[D, C]
                          | point_dist2_symmetric[N, M] | point_dist2_symmetric[B, A])
  }
  (par, (apply line_parallel_trans to par, conjunct 1 of t), mlen)
end