module Parallel

/*
  Parallel lines, for coordinate geometry (#1191).

  A transversal through A and C crosses line AB at A and line CD at C.
  Each angle theorem below comes with its converse, stated as one
  equation between "AB ∥ CD" and a statement about angles:

  * Alternate Interior Angles (`line_parallel_alt_interior`, in
    lib/Triangle.pf): if B and D are on opposite sides of line AC, then
    AB ∥ CD exactly when ∠BAC ≅ ∠DCA.
  * Same-Side Interior Angles (`line_parallel_same_side`): if B and D
    are on the same side of line AC, then AB ∥ CD exactly when ∠BAC
    and ∠ACD are supplementary.
  * Corresponding Angles (`line_parallel_corresponding`): if A is
    between G and C, and B and D are on the same side of line AC, then
    AB ∥ CD exactly when ∠GAB ≅ ∠ACD.
  * Alternate Exterior Angles (`line_parallel_alt_exterior`): if A is
    between G and C, C is between A and H, and B and D are on opposite
    sides of line AC, then AB ∥ CD exactly when ∠GAB ≅ ∠HCD.

           G
            \
         ----A-------- B          (B and D on the same side of AC)
              \
         ------C------ D
                \
                 H

  Also here: the Parallel Postulate in Playfair's form
  (`line_parallel_postulate`: through a point there is exactly one
  line parallel to a given line) and the Midsegment Theorem
  (`tri_midsegment`). Parallel lines are transitive
  (`line_parallel_trans`), and two lines perpendicular to the same line
  are parallel (`line_perpendicular_parallel`); both are in
  lib/Line.pf.

  How they are proved: same-side interior angles from alternate
  interior angles, by extending DC through C to a point E (so ∠ACE and
  ∠ACD are a linear pair, and line CE is line CD); corresponding and
  alternate exterior angles from those two, with the Linear Pair
  Postulate at A and C.
*/

import Base
public import Triangle
import Linear
import Ring

// If B is off line AC and A is between G and C, then B is off line GA.
lemma not_collinear_extend: all G:Point, A:Point, C:Point, B:Point.
  if between(G, A, C) and not collinear(A, C, B) then not collinear(G, A, B)
proof
  arbitrary G:Point, A:Point, C:Point, B:Point
  assume h
  have ga: not (G = A) by conjunct 0 of apply between_distinct to conjunct 0 of h
  have ag: not (A = G) by { assume e  apply ga to symmetric e }
  have agc: collinear(A, G, C) by {
    replace symmetric point_collinear_swap[A, C, G] | symmetric point_collinear_rotate[G, A, C]
    apply between_collinear to conjunct 0 of h
  }
  assume gab
  have agb: collinear(A, G, B) by {
    replace symmetric point_collinear_swap[A, B, G] | symmetric point_collinear_rotate[G, A, B]
    gab
  }
  apply (conjunct 1 of h) to apply point_collinear_trans[A, G, C, B] to ag, agc, agb
end

// A point off line AC is not collinear with A and C.
lemma off_line: all A:Point, C:Point, B:Point.
  if not (A = C) and not on_line(B, line(A, C)) then not collinear(A, C, B)
proof
  arbitrary A:Point, C:Point, B:Point
  assume h
  replace symmetric apply line_on_collinear[A, C, B] to conjunct 0 of h
  conjunct 1 of h
end

// The Same-Side Interior Angles Theorem and its converse: if B and D
// are on the same side of line AC, then AB ∥ CD exactly when ∠BAC and
// ∠ACD are supplementary.
theorem line_parallel_same_side: all A:Point, B:Point, C:Point, D:Point.
  if not (A = C) and same_side(B, D, line(A, C))
  then parallel(line(A, B), line(C, D)) = supplementary(m_angle(B, A, C), m_angle(A, C, D))
proof
  arbitrary A:Point, B:Point, C:Point, D:Point
  assume h
  have ac: not (A = C) by conjunct 0 of h
  have s: real(+0) < cross(A, C, B) * cross(A, C, D)
    by replace apply line_same_side_cross[A, C, B, D] to ac in conjunct 1 of h
  have nd: not (cross(A, C, D) = real(+0)) by { assume z  replace z in s }
  have cd: not (C = D) by {
    assume e
    have z: cross(A, C, D) = real(+0) by { replace symmetric e  expand cross  algebra }
    apply nd to z
  }
  // Extend DC through C to E, with CE = DC.
  define E = point(xcoord(C) + (xcoord(C) - xcoord(D)), ycoord(C) + (ycoord(C) - ycoord(D)))
  have dce: between(D, C, E) by {
    have c: cross(D, C, E) = real(+0) by { expand E | cross  algebra }
    have d: dot(C, D, E) = - dist2(C, D) by { expand E | dot | dist2  algebra }
    have p: real(+0) < dist2(C, D) by apply point_dist2_pos to cd
    expand between | collinear
    (c, conclude dot(C, D, E) < real(+0) by algebra with d | p)
  }
  have ce: not (C = E) by conjunct 1 of apply between_distinct to dce
  // Line CE is line CD.
  have l: line(C, E) = line(C, D) by {
    have de: on_line(D, line(C, E)) by {
      replace apply line_on_collinear[C, E, D] to ce
      expand collinear | E | cross
      algebra
    }
    apply line_unique[C, D, line(C, E)] to cd, line_on_left[C, E], de
  }
  // B and E are on opposite sides of line AC: alternate interior angles.
  have opp: opposite_sides(B, E, line(A, C)) by {
    have e: cross(A, C, E) = - cross(A, C, D) by { expand E | cross  algebra }
    have i: cross(A, C, B) * (- cross(A, C, D)) = - (cross(A, C, B) * cross(A, C, D)) by algebra
    replace apply line_opposite_sides_cross[A, C, B, E] to ac | e | i
    algebra with s
  }
  have alt: parallel(line(A, B), line(C, E)) = angle_congruent(B, A, C, E, C, A)
    by apply line_parallel_alt_interior[A, B, C, E] to ac, opp
  // ∠DCA and ∠ACE are a linear pair.
  have ndca: not collinear(D, C, A) by {
    have e: cross(D, C, A) = - cross(A, C, D) by { expand cross  algebra }
    expand collinear
    replace e
    assume z
    apply nd to (conclude cross(A, C, D) = real(+0) by algebra with z)
  }
  have lp: m_angle(D, C, A) + m_angle(A, C, E) = deg(+180)
    by apply angle_linear_pair[D, C, E, A] to dce, ndca
  replace symmetric l | alt
  expand angle_congruent | supplementary
  have fwd: if m_angle(B, A, C) = m_angle(E, C, A)
            then m_angle(B, A, C) + m_angle(A, C, D) = deg(+180) by {
    assume e
    algebra with e | lp | angle_measure_symmetric[D, C, A] | angle_measure_symmetric[E, C, A]
  }
  have bwd: if m_angle(B, A, C) + m_angle(A, C, D) = deg(+180)
            then m_angle(B, A, C) = m_angle(E, C, A) by {
    assume e
    algebra with e | lp | angle_measure_symmetric[D, C, A] | angle_measure_symmetric[E, C, A]
  }
  apply iff_equal to fwd, bwd
end

// The Corresponding Angles Postulate and its converse: if A is between
// G and C, and B and D are on the same side of line AC, then AB ∥ CD
// exactly when ∠GAB ≅ ∠ACD.
theorem line_parallel_corresponding: all G:Point, A:Point, B:Point, C:Point, D:Point.
  if between(G, A, C) and same_side(B, D, line(A, C))
  then parallel(line(A, B), line(C, D)) = angle_congruent(G, A, B, A, C, D)
proof
  arbitrary G:Point, A:Point, B:Point, C:Point, D:Point
  assume h
  have ac: not (A = C) by conjunct 1 of apply between_distinct to conjunct 0 of h
  have nb: not collinear(A, C, B)
    by apply off_line to ac, conjunct 0 of apply line_same_side_off to conjunct 1 of h
  // ∠GAB and ∠BAC are a linear pair.
  have lp: m_angle(G, A, B) + m_angle(B, A, C) = deg(+180)
    by apply angle_linear_pair[G, A, C, B]
       to (conjunct 0 of h), (apply not_collinear_extend to (conjunct 0 of h), nb)
  replace apply line_parallel_same_side[A, B, C, D] to ac, conjunct 1 of h
  expand supplementary | angle_congruent
  have fwd: if m_angle(B, A, C) + m_angle(A, C, D) = deg(+180)
            then m_angle(G, A, B) = m_angle(A, C, D) by { assume e  algebra with e | lp }
  have bwd: if m_angle(G, A, B) = m_angle(A, C, D)
            then m_angle(B, A, C) + m_angle(A, C, D) = deg(+180) by { assume e  algebra with e | lp }
  apply iff_equal to fwd, bwd
end

// The Alternate Exterior Angles Theorem and its converse: if A is
// between G and C, C is between A and H, and B and D are on opposite
// sides of line AC, then AB ∥ CD exactly when ∠GAB ≅ ∠HCD.
theorem line_parallel_alt_exterior: all G:Point, A:Point, B:Point, C:Point, D:Point, H:Point.
  if between(G, A, C) and between(A, C, H) and opposite_sides(B, D, line(A, C))
  then parallel(line(A, B), line(C, D)) = angle_congruent(G, A, B, H, C, D)
proof
  arbitrary G:Point, A:Point, B:Point, C:Point, D:Point, H:Point
  assume h
  have ac: not (A = C) by conjunct 1 of apply between_distinct to conjunct 0 of h
  have off: not on_line(B, line(A, C)) and not on_line(D, line(A, C))
    by apply line_opposite_sides_off to conjunct 2 of h
  have nb: not collinear(A, C, B) by apply off_line to ac, conjunct 0 of off
  have nd: not collinear(A, C, D) by apply off_line to ac, conjunct 1 of off
  // Linear pairs at A and at C.
  have lp1: m_angle(G, A, B) + m_angle(B, A, C) = deg(+180)
    by apply angle_linear_pair[G, A, C, B]
       to (conjunct 0 of h), (apply not_collinear_extend to (conjunct 0 of h), nb)
  have lp2: m_angle(A, C, D) + m_angle(D, C, H) = deg(+180)
    by apply angle_linear_pair[A, C, H, D] to (conjunct 1 of h), nd
  replace apply line_parallel_alt_interior[A, B, C, D] to ac, conjunct 2 of h
  expand angle_congruent
  have fwd: if m_angle(B, A, C) = m_angle(D, C, A) then m_angle(G, A, B) = m_angle(H, C, D) by {
    assume e
    algebra with e | lp1 | lp2 | angle_measure_symmetric[D, C, A] | angle_measure_symmetric[H, C, D]
  }
  have bwd: if m_angle(G, A, B) = m_angle(H, C, D) then m_angle(B, A, C) = m_angle(D, C, A) by {
    assume e
    algebra with e | lp1 | lp2 | angle_measure_symmetric[D, C, A] | angle_measure_symmetric[H, C, D]
  }
  apply iff_equal to fwd, bwd
end

// The Parallel Postulate (Playfair's form): through a point P there is
// exactly one line parallel to a line L. (When P is not on L, as in
// the textbook, that line is not L; when P is on L, it is L itself.)
theorem line_parallel_postulate: all L:Line, P:Point.
  (some M:Line. on_line(P, M) and parallel(L, M))
  and (all M:Line, N:Line.
         if on_line(P, M) and parallel(L, M) and on_line(P, N) and parallel(L, N) then M = N)
proof
  arbitrary L:Line, P:Point
  have ex: some M:Line. on_line(P, M) and parallel(L, M) by {
    obtain A, B where ab from line_points[L]
    // Q is P moved by the vector from A to B.
    define Q = point(xcoord(P) + (xcoord(B) - xcoord(A)), ycoord(P) + (ycoord(B) - ycoord(A)))
    have pq: not (P = Q) by {
      have l: dist2(P, Q) = dist2(A, B) by { expand Q | dist2  algebra }
      replace symmetric point_dist2_zero[P, Q] | l | point_dist2_zero[A, B]
      conjunct 0 of ab
    }
    have par: parallel(L, line(P, Q)) by {
      have c: seg_cross(A, B, P, Q) = real(+0) by { expand Q | seg_cross  algebra }
      replace conjunct 1 of ab | apply line_parallel_seg_cross[A, B, P, Q] to (conjunct 0 of ab), pq
      c
    }
    choose line(P, Q)
    conclude on_line(P, line(P, Q)) and parallel(L, line(P, Q)) by (line_on_left[P, Q], par)
  }
  have un: all M:Line, N:Line.
      if on_line(P, M) and parallel(L, M) and on_line(P, N) and parallel(L, N) then M = N by {
    arbitrary M:Line, N:Line
    assume h
    have mn: parallel(M, N)
      by apply line_parallel_trans[M, L, N]
         to (apply line_parallel_symmetric to conjunct 1 of h), conjunct 3 of h
    apply line_parallel_unique[M, N, P] to mn, (conjunct 0 of h), (conjunct 2 of h)
  }
  (ex, un)
end

// The Midsegment Theorem: the segment joining the midpoints of two
// sides of a triangle is parallel to the third side and half as long.
theorem tri_midsegment: all A:Point, B:Point, C:Point.
  if not (B = C)
  then parallel(line(midpoint(A, B), midpoint(A, C)), line(B, C))
   and real(+2) * dist(midpoint(A, B), midpoint(A, C)) = dist(B, C)
proof
  arbitrary A:Point, B:Point, C:Point
  assume bc
  define M = midpoint(A, B)
  define N = midpoint(A, C)
  // The vector from M to N is half the vector from B to C.
  have hx: xcoord(N) - xcoord(M) = real(frac(+1, 2)) * (xcoord(C) - xcoord(B))
    by { expand M | N | midpoint  algebra }
  have hy: ycoord(N) - ycoord(M) = real(frac(+1, 2)) * (ycoord(C) - ycoord(B))
    by { expand M | N | midpoint  algebra }
  have four: real(+4) * dist2(M, N) = dist2(B, C) by {
    replace point_dist2_symmetric[M, N]
    expand dist2
    replace hx | hy
    algebra
  }
  have mn: not (M = N) by {
    assume e
    have z: dist2(B, C) = real(+0) by { replace symmetric four | e | point_dist2_self[N]. }
    apply bc to replace point_dist2_zero[B, C] in z
  }
  have par: parallel(line(M, N), line(B, C)) by {
    have c: seg_cross(M, N, B, C) = real(+0) by { expand seg_cross  replace hx | hy  algebra }
    replace apply line_parallel_seg_cross[M, N, B, C] to mn, bc
    c
  }
  have half: real(+2) * dist(M, N) = dist(B, C) by {
    have nn: real(+0) ≤ real(+2) * dist(M, N) by algebra with point_dist_nonneg[M, N]
    have sq: (real(+2) * dist(M, N)) * (real(+2) * dist(M, N)) = dist2(B, C) by {
      have e: (real(+2) * dist(M, N)) * (real(+2) * dist(M, N)) = real(+4) * (dist(M, N) * dist(M, N))
        by algebra
      replace e | point_dist_square[M, N]
      four
    }
    symmetric apply point_dist_unique[B, C, real(+2) * dist(M, N)] to nn, sq
  }
  (par, half)
end