module Quadrilateral

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

  * `quadrilateral(A, B, C, D)`: ABCD is a convex quadrilateral, with
    sides AB, BC, CD, DA: for each side, the other two vertices are on
    the same side of its line. Its vertices are distinct
    (`quad_distinct`), any three of them form a triangle
    (`quad_triangles`), and its diagonals cross: B and D are on
    opposite sides of line AC, and A and C on opposite sides of line BD
    (`quad_diagonals`). `quad_ccw` gives a quadrilateral from
    coordinates: the vertices go counterclockwise.
  * `parallelogram(A, B, C, D)`: a quadrilateral whose opposite sides
    are parallel, AB ∥ CD and BC ∥ DA.
  * `rectangle(A, B, C, D)`: a quadrilateral with four right angles.
  * `rhombus(A, B, C, D)`: a quadrilateral with four congruent sides.
  * `square(A, B, C, D)`: a rectangle and a rhombus, so four right
    angles and four congruent sides.

  Theorems about parallelograms: opposite sides are congruent
  (`parallelogram_opposite_sides`), opposite angles are congruent
  (`parallelogram_opposite_angles`), consecutive angles are
  supplementary (`parallelogram_consecutive_angles`), and the diagonals
  bisect each other (`parallelogram_diagonals`). Conversely, a
  quadrilateral is a parallelogram if both pairs of opposite sides are
  congruent (`parallelogram_of_opposite_sides`), if one pair of
  opposite sides is parallel and congruent (`parallelogram_of_one_pair`),
  or if its diagonals bisect each other (`parallelogram_of_diagonals`).

  Rectangles and rhombuses are parallelograms
  (`rectangle_parallelogram`, `rhombus_parallelogram`); the diagonals of
  a rectangle are congruent (`rectangle_diagonals`), those of a rhombus
  are perpendicular (`rhombus_diagonals`), and those of a square are
  both (`square_diagonals`).

  How they are proved: in a parallelogram C = B + D - A in coordinates
  (`parallelogram_vec`), which gives the sides, angles, and diagonals.
  The two converses about sides follow the textbook: draw diagonal AC,
  prove triangles ABC and CDA congruent (SSS, or SAS with alternate
  interior angles), and use alternate interior angles to get the
  parallel sides.
*/

import Base
public import Parallel
import Linear
import Ring

// ABCD is a (convex) quadrilateral.
fun quadrilateral(A : Point, B : Point, C : Point, D : Point) {
  same_side(C, D, line(A, B)) and same_side(D, A, line(B, C))
  and same_side(A, B, line(C, D)) and same_side(B, C, line(D, A))
}

// ABCD is a parallelogram.
fun parallelogram(A : Point, B : Point, C : Point, D : Point) {
  quadrilateral(A, B, C, D)
  and parallel(line(A, B), line(C, D)) and parallel(line(B, C), line(D, A))
}

// ABCD is a rectangle.
fun rectangle(A : Point, B : Point, C : Point, D : Point) {
  quadrilateral(A, B, C, D)
  and right_angle(D, A, B) and right_angle(A, B, C)
  and right_angle(B, C, D) and right_angle(C, D, A)
}

// ABCD is a rhombus.
fun rhombus(A : Point, B : Point, C : Point, D : Point) {
  quadrilateral(A, B, C, D)
  and seg_congruent(A, B, B, C) and seg_congruent(B, C, C, D)
  and seg_congruent(C, D, D, A)
}

// ABCD is a square.
fun square(A : Point, B : Point, C : Point, D : Point) {
  rectangle(A, B, C, D) and rhombus(A, B, C, D)
}

/*
  Quadrilaterals
*/

lemma cross_rotate: all A:Point, B:Point, C:Point. cross(B, C, A) = cross(A, B, C)
proof
  arbitrary A:Point, B:Point, C:Point
  expand cross
  algebra
end

// For x y > 0 and x z > 0: y z > 0.
lemma sign_pos: all x:Real, y:Real, z:Real.
  if real(+0) < x * y and real(+0) < x * z then real(+0) < y * z
proof
  arbitrary x:Real, y:Real, z:Real
  assume h
  have nx: not (x = real(+0)) by { assume e  replace e in conjunct 0 of h }
  have xx: real(+0) < x * x by {
    replace real_less_def
    have nz: not (real(+0) = x * x) by {
      assume e
      apply nx to replace apply real_mult_zero_iff[x, x] to nx in symmetric e
    }
    (real_square_nonneg[x], nz)
  }
  have p: real(+0) < (x * y) * (x * z) by apply real_mult_pos to h
  have e: (x * y) * (x * z) = (y * z) * (x * x) by algebra
  have c: (real(+0) * (x * x) < (y * z) * (x * x)) = (real(+0) < y * z)
    by apply real_pos_mult_less_iff[x * x, real(+0), y * z] to xx
  replace symmetric c
  replace e in p
end

lemma quad_sides_distinct: all A:Point, B:Point, C:Point, D:Point.
  if quadrilateral(A, B, C, D)
  then not (A = B) and not (B = C) and not (C = D) and not (D = A)
proof
  arbitrary A:Point, B:Point, C:Point, D:Point
  expand quadrilateral
  assume h
  // Each vertex is off the lines of the two sides that do not contain it.
  have ab: not (A = B) by {
    assume e
    have s: same_side(D, A, line(A, C)) by replace symmetric e in conjunct 1 of h
    apply (conjunct 1 of apply line_same_side_off to s) to line_on_left[A, C]
  }
  have bc: not (B = C) by {
    assume e
    have s: same_side(A, B, line(B, D)) by replace symmetric e in conjunct 2 of h
    apply (conjunct 1 of apply line_same_side_off to s) to line_on_left[B, D]
  }
  have cd: not (C = D) by {
    assume e
    have s: same_side(B, C, line(C, A)) by replace symmetric e in conjunct 3 of h
    apply (conjunct 1 of apply line_same_side_off to s) to line_on_left[C, A]
  }
  have da: not (D = A) by {
    assume e
    have s: same_side(C, D, line(D, B)) by replace symmetric e in conjunct 0 of h
    apply (conjunct 1 of apply line_same_side_off to s) to line_on_left[D, B]
  }
  (ab, bc, cd, da)
end

// The four turns ABC, BCD, CDA, DAB of a quadrilateral have the same
// sign.
lemma quad_cross: all A:Point, B:Point, C:Point, D:Point.
  if quadrilateral(A, B, C, D)
  then real(+0) < cross(A, B, C) * cross(B, C, D)
   and real(+0) < cross(A, B, C) * cross(C, D, A)
   and real(+0) < cross(A, B, C) * cross(D, A, B)
proof
  arbitrary A:Point, B:Point, C:Point, D:Point
  assume q
  have ds: not (A = B) and not (B = C) and not (C = D) and not (D = A)
    by apply quad_sides_distinct to q
  have h: same_side(C, D, line(A, B)) and same_side(D, A, line(B, C))
          and same_side(A, B, line(C, D)) and same_side(B, C, line(D, A))
    by expand quadrilateral in q
  have s14: real(+0) < cross(A, B, C) * cross(D, A, B) by {
    replace symmetric cross_rotate[D, A, B]
    replace apply line_same_side_cross[A, B, C, D] to conjunct 0 of ds in conjunct 0 of h
  }
  have s21: real(+0) < cross(B, C, D) * cross(A, B, C) by {
    replace symmetric cross_rotate[A, B, C]
    replace apply line_same_side_cross[B, C, D, A] to conjunct 1 of ds in conjunct 1 of h
  }
  have s32: real(+0) < cross(C, D, A) * cross(B, C, D) by {
    replace symmetric cross_rotate[B, C, D]
    replace apply line_same_side_cross[C, D, A, B] to conjunct 2 of ds in conjunct 2 of h
  }
  have s43: real(+0) < cross(D, A, B) * cross(C, D, A) by {
    replace symmetric cross_rotate[C, D, A]
    replace apply line_same_side_cross[D, A, B, C] to conjunct 3 of ds in conjunct 3 of h
  }
  have s12: real(+0) < cross(A, B, C) * cross(B, C, D) by algebra with s21
  have s13: real(+0) < cross(A, B, C) * cross(C, D, A) by {
    have s41: real(+0) < cross(D, A, B) * cross(A, B, C) by algebra with s14
    apply sign_pos[cross(D, A, B), cross(A, B, C), cross(C, D, A)] to s41, s43
  }
  (s12, s13, s14)
end

// A quadrilateral listed from B instead of A.
theorem quad_rotate: all A:Point, B:Point, C:Point, D:Point.
  quadrilateral(A, B, C, D) = quadrilateral(B, C, D, A)
proof
  arbitrary A:Point, B:Point, C:Point, D:Point
  expand quadrilateral
  have fwd: if same_side(C, D, line(A, B)) and same_side(D, A, line(B, C))
               and same_side(A, B, line(C, D)) and same_side(B, C, line(D, A))
            then same_side(D, A, line(B, C)) and same_side(A, B, line(C, D))
               and same_side(B, C, line(D, A)) and same_side(C, D, line(A, B)) by {
    assume h
    ((conjunct 1 of h), (conjunct 2 of h), (conjunct 3 of h), (conjunct 0 of h))
  }
  have bwd: if same_side(D, A, line(B, C)) and same_side(A, B, line(C, D))
               and same_side(B, C, line(D, A)) and same_side(C, D, line(A, B))
            then same_side(C, D, line(A, B)) and same_side(D, A, line(B, C))
               and same_side(A, B, line(C, D)) and same_side(B, C, line(D, A)) by {
    assume h
    ((conjunct 3 of h), (conjunct 0 of h), (conjunct 1 of h), (conjunct 2 of h))
  }
  apply iff_equal to fwd, bwd
end

// Any three vertices of a quadrilateral form a triangle.
theorem quad_triangles: all A:Point, B:Point, C:Point, D:Point.
  if quadrilateral(A, B, C, D)
  then triangle(A, B, C) and triangle(B, C, D) and triangle(C, D, A) and triangle(D, A, B)
proof
  arbitrary A:Point, B:Point, C:Point, D:Point
  assume q
  have s: real(+0) < cross(A, B, C) * cross(B, C, D)
          and real(+0) < cross(A, B, C) * cross(C, D, A)
          and real(+0) < cross(A, B, C) * cross(D, A, B) by apply quad_cross to q
  expand triangle | collinear
  have t1: not (cross(A, B, C) = real(+0)) by { assume z  replace z in conjunct 0 of s }
  have t2: not (cross(B, C, D) = real(+0)) by { assume z  replace z in conjunct 0 of s }
  have t3: not (cross(C, D, A) = real(+0)) by { assume z  replace z in conjunct 1 of s }
  have t4: not (cross(D, A, B) = real(+0)) by { assume z  replace z in conjunct 2 of s }
  (t1, t2, t3, t4)
end

// The vertices of a quadrilateral are distinct.
theorem quad_distinct: all A:Point, B:Point, C:Point, D:Point.
  if quadrilateral(A, B, C, D)
  then not (A = B) and not (B = C) and not (C = D) and not (D = A)
   and not (A = C) and not (B = D)
proof
  arbitrary A:Point, B:Point, C:Point, D:Point
  assume q
  have ds: not (A = B) and not (B = C) and not (C = D) and not (D = A)
    by apply quad_sides_distinct to q
  have t: triangle(A, B, C) and triangle(B, C, D) and triangle(C, D, A) and triangle(D, A, B)
    by apply quad_triangles to q
  have ac: not (A = C) by conjunct 2 of apply triangle_distinct to conjunct 0 of t
  have bd: not (B = D) by conjunct 2 of apply triangle_distinct to conjunct 1 of t
  ((conjunct 0 of ds), (conjunct 1 of ds), (conjunct 2 of ds), (conjunct 3 of ds), ac, bd)
end

// The diagonals of a quadrilateral cross: B and D are on opposite sides
// of line AC, and A and C are on opposite sides of line BD.
theorem quad_diagonals: all A:Point, B:Point, C:Point, D:Point.
  if quadrilateral(A, B, C, D)
  then opposite_sides(B, D, line(A, C)) and opposite_sides(A, C, line(B, D))
proof
  arbitrary A:Point, B:Point, C:Point, D:Point
  assume q
  have s: real(+0) < cross(A, B, C) * cross(B, C, D)
          and real(+0) < cross(A, B, C) * cross(C, D, A)
          and real(+0) < cross(A, B, C) * cross(D, A, B) by apply quad_cross to q
  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 q
  have o1: opposite_sides(B, D, line(A, C)) by {
    have e1: cross(A, C, B) = - cross(A, B, C) by { expand cross  algebra }
    have e2: cross(A, C, D) = cross(C, D, A) by { expand cross  algebra }
    have i: (- cross(A, B, C)) * cross(C, D, A) = - (cross(A, B, C) * cross(C, D, A)) by algebra
    replace apply line_opposite_sides_cross[A, C, B, D] to conjunct 4 of ds | e1 | e2 | i
    algebra with conjunct 1 of s
  }
  have o2: opposite_sides(A, C, line(B, D)) by {
    have s24: real(+0) < cross(B, C, D) * cross(D, A, B)
      by apply sign_pos[cross(A, B, C), cross(B, C, D), cross(D, A, B)] to (conjunct 0 of s), (conjunct 2 of s)
    have e1: cross(B, D, A) = cross(D, A, B) by { expand cross  algebra }
    have e2: cross(B, D, C) = - cross(B, C, D) by { expand cross  algebra }
    have i: cross(D, A, B) * (- cross(B, C, D)) = - (cross(B, C, D) * cross(D, A, B)) by algebra
    replace apply line_opposite_sides_cross[B, D, A, C] to conjunct 5 of ds | e1 | e2 | i
    algebra with s24
  }
  (o1, o2)
end

// A quadrilateral from coordinates: the turns ABC, BCD, CDA, DAB are
// all counterclockwise.
theorem quad_ccw: all A:Point, B:Point, C:Point, D:Point.
  if real(+0) < cross(A, B, C) and real(+0) < cross(B, C, D)
     and real(+0) < cross(C, D, A) and real(+0) < cross(D, A, B)
  then quadrilateral(A, B, C, D)
proof
  arbitrary A:Point, B:Point, C:Point, D:Point
  assume h
  have t1: triangle(A, B, C) by { expand triangle | collinear  assume z  replace z in conjunct 0 of h }
  have t3: triangle(C, D, A) by { expand triangle | collinear  assume z  replace z in conjunct 2 of h }
  have d1: not (A = B) and not (B = C) and not (A = C) by apply triangle_distinct to t1
  have d3: not (C = D) and not (D = A) and not (C = A) by apply triangle_distinct to t3
  expand quadrilateral
  replace apply line_same_side_cross[A, B, C, D] to conjunct 0 of d1
        | apply line_same_side_cross[B, C, D, A] to conjunct 1 of d1
        | apply line_same_side_cross[C, D, A, B] to conjunct 0 of d3
        | apply line_same_side_cross[D, A, B, C] to conjunct 1 of d3
  have r4: real(+0) < cross(A, B, D) by { replace cross_rotate[D, A, B]  conjunct 3 of h }
  have r1: real(+0) < cross(B, C, A) by { replace cross_rotate[A, B, C]  conjunct 0 of h }
  have r2: real(+0) < cross(C, D, B) by { replace cross_rotate[B, C, D]  conjunct 1 of h }
  have r3: real(+0) < cross(D, A, C) by { replace cross_rotate[C, D, A]  conjunct 2 of h }
  ((apply real_mult_pos to (conjunct 0 of h), r4),
   (apply real_mult_pos to (conjunct 1 of h), r1),
   (apply real_mult_pos to (conjunct 2 of h), r2),
   (apply real_mult_pos to (conjunct 3 of h), r3))
end

/*
  Parallelograms
*/

// In a parallelogram, C = B + D - A.
lemma parallelogram_vec: all A:Point, B:Point, C:Point, D:Point.
  if parallelogram(A, B, C, D)
  then xcoord(C) = xcoord(B) + xcoord(D) - xcoord(A)
   and ycoord(C) = ycoord(B) + ycoord(D) - ycoord(A)
proof
  arbitrary A:Point, B:Point, C:Point, D:Point
  expand parallelogram
  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 s1: 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 s2: seg_cross(B, C, D, A) = real(+0)
    by replace apply line_parallel_seg_cross[B, C, D, A] to (conjunct 1 of ds), (conjunct 3 of ds)
       in conjunct 2 of h
  // k = AB × AD is not zero.
  have k: not (cross(A, B, D) = real(+0)) by {
    have t: triangle(D, A, B) by conjunct 3 of apply quad_triangles to conjunct 0 of h
    have e: cross(D, A, B) = cross(A, B, D) by symmetric cross_rotate[D, A, B]
    replace symmetric e
    expand triangle | collinear in t
  }
  // Cramer's rule: k (C - (B + D - A)) = (AB × DC) AD + (BC × AD) AB.
  have ix: cross(A, B, D) * (xcoord(C) - (xcoord(B) + xcoord(D) - xcoord(A)))
           = seg_cross(A, B, C, D) * (xcoord(A) - xcoord(D))
             + seg_cross(B, C, D, A) * (xcoord(A) - xcoord(B))
    by { expand cross | seg_cross  algebra }
  have iy: cross(A, B, D) * (ycoord(C) - (ycoord(B) + ycoord(D) - ycoord(A)))
           = seg_cross(A, B, C, D) * (ycoord(A) - ycoord(D))
             + seg_cross(B, C, D, A) * (ycoord(A) - ycoord(B))
    by { expand cross | seg_cross  algebra }
  have zx: cross(A, B, D) * (xcoord(C) - (xcoord(B) + xcoord(D) - xcoord(A))) = real(+0)
    by { replace ix | s1 | s2. }
  have zy: cross(A, B, D) * (ycoord(C) - (ycoord(B) + ycoord(D) - ycoord(A))) = real(+0)
    by { replace iy | s1 | s2. }
  have ex: xcoord(C) - (xcoord(B) + xcoord(D) - xcoord(A)) = real(+0)
    by replace apply real_mult_zero_iff[cross(A, B, D),
                   xcoord(C) - (xcoord(B) + xcoord(D) - xcoord(A))] to k in zx
  have ey: ycoord(C) - (ycoord(B) + ycoord(D) - ycoord(A)) = real(+0)
    by replace apply real_mult_zero_iff[cross(A, B, D),
                   ycoord(C) - (ycoord(B) + ycoord(D) - ycoord(A))] to k in zy
  ((replace real_sub_eq_zero_iff in ex), (replace real_sub_eq_zero_iff in ey))
end

// A quadrilateral with C = B + D - A is a parallelogram.
lemma parallelogram_of_vec: all A:Point, B:Point, C:Point, D:Point.
  if quadrilateral(A, B, C, D)
     and xcoord(C) = xcoord(B) + xcoord(D) - xcoord(A)
     and ycoord(C) = ycoord(B) + ycoord(D) - ycoord(A)
  then parallelogram(A, B, C, D)
proof
  arbitrary A:Point, B:Point, C:Point, D:Point
  assume h
  have cx: xcoord(C) = xcoord(B) + xcoord(D) - xcoord(A) by conjunct 1 of h
  have cy: ycoord(C) = ycoord(B) + ycoord(D) - ycoord(A) by conjunct 2 of 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 p1: parallel(line(A, B), line(C, D)) by {
    replace apply line_parallel_seg_cross[A, B, C, D] to (conjunct 0 of ds), (conjunct 2 of ds)
    expand seg_cross
    replace cx | cy
    algebra
  }
  have p2: 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)
    expand seg_cross
    replace cx | cy
    algebra
  }
  expand parallelogram
  ((conjunct 0 of h), p1, p2)
end

// A parallelogram listed from B instead of A.
theorem parallelogram_rotate: all A:Point, B:Point, C:Point, D:Point.
  if parallelogram(A, B, C, D) then parallelogram(B, C, D, A)
proof
  arbitrary A:Point, B:Point, C:Point, D:Point
  expand parallelogram
  assume h
  have q: quadrilateral(B, C, D, A) by replace quad_rotate[A, B, C, D] in conjunct 0 of h
  have p: parallel(line(C, D), line(A, B))
    by apply line_parallel_symmetric to conjunct 1 of h
  (q, (conjunct 2 of h), p)
end

// The opposite sides of a parallelogram are congruent.
theorem parallelogram_opposite_sides: all A:Point, B:Point, C:Point, D:Point.
  if parallelogram(A, B, C, D) then seg_congruent(A, B, C, D) and seg_congruent(B, C, D, A)
proof
  arbitrary A:Point, B:Point, C:Point, D:Point
  assume h
  have v: xcoord(C) = xcoord(B) + xcoord(D) - xcoord(A)
          and ycoord(C) = ycoord(B) + ycoord(D) - ycoord(A) by apply parallelogram_vec to h
  expand seg_congruent
  have ab: dist2(A, B) = dist2(C, D) by {
    expand dist2
    replace conjunct 0 of v | conjunct 1 of v
    algebra
  }
  have bc: dist2(B, C) = dist2(D, A) by {
    expand dist2
    replace conjunct 0 of v | conjunct 1 of v
    algebra
  }
  (ab, bc)
end

// The opposite angles of a parallelogram are congruent.
theorem parallelogram_opposite_angles: all A:Point, B:Point, C:Point, D:Point.
  if parallelogram(A, B, C, D)
  then angle_congruent(D, A, B, B, C, D) and angle_congruent(A, B, C, C, D, A)
proof
  arbitrary A:Point, B:Point, C:Point, D:Point
  assume h
  have v: xcoord(C) = xcoord(B) + xcoord(D) - xcoord(A)
          and ycoord(C) = ycoord(B) + ycoord(D) - ycoord(A) by apply parallelogram_vec to h
  expand angle_congruent
  // The sides at C are those at A, reversed.
  have a: m_angle(D, A, B) = m_angle(B, C, D) by {
    have d: dot(A, D, B) = dot(C, B, D) by {
      expand dot
      replace conjunct 0 of v | conjunct 1 of v
      algebra
    }
    have c: cross(A, D, B) = cross(C, B, D) by {
      expand cross
      replace conjunct 0 of v | conjunct 1 of v
      algebra
    }
    apply angle_measure_dot_cross[D, A, B, B, C, D] to d, (replace c.)
  }
  have b: m_angle(A, B, C) = m_angle(C, D, A) by {
    have d: dot(B, A, C) = dot(D, C, A) by {
      expand dot
      replace conjunct 0 of v | conjunct 1 of v
      algebra
    }
    have c: cross(B, A, C) = cross(D, C, A) by {
      expand cross
      replace conjunct 0 of v | conjunct 1 of v
      algebra
    }
    apply angle_measure_dot_cross[A, B, C, C, D, A] to d, (replace c.)
  }
  (a, b)
end

// Consecutive angles of a parallelogram are supplementary.
theorem parallelogram_consecutive_angles: all A:Point, B:Point, C:Point, D:Point.
  if parallelogram(A, B, C, D) then supplementary(m_angle(D, A, B), m_angle(A, B, C))
proof
  arbitrary A:Point, B:Point, C:Point, D:Point
  expand parallelogram
  assume h
  have ab: not (A = B) by conjunct 0 of apply quad_distinct to conjunct 0 of h
  have s: same_side(D, C, line(A, B)) by {
    replace line_same_side_symmetric[D, C, line(A, B)]
    conjunct 0 of expand quadrilateral in conjunct 0 of h
  }
  // Same-side interior angles, with transversal AB.
  have p: parallel(line(A, D), line(B, C)) by {
    replace line_symmetric[A, D]
    apply line_parallel_symmetric to conjunct 2 of h
  }
  replace symmetric apply line_parallel_same_side[A, D, B, C] to ab, s
  p
end

// The diagonals of a parallelogram bisect each other.
theorem parallelogram_diagonals: all A:Point, B:Point, C:Point, D:Point.
  if parallelogram(A, B, C, D) then midpoint(A, C) = midpoint(B, D)
proof
  arbitrary A:Point, B:Point, C:Point, D:Point
  assume h
  have v: xcoord(C) = xcoord(B) + xcoord(D) - xcoord(A)
          and ycoord(C) = ycoord(B) + ycoord(D) - ycoord(A) by apply parallelogram_vec to h
  have ex: xcoord(midpoint(A, C)) = xcoord(midpoint(B, D)) by {
    expand midpoint
    replace conjunct 0 of v
    algebra
  }
  have ey: ycoord(midpoint(A, C)) = ycoord(midpoint(B, D)) by {
    expand midpoint
    replace conjunct 1 of v
    algebra
  }
  apply point_equal to ex, ey
end

/*
  Proving that a quadrilateral is a parallelogram
*/

// If triangles ABC and CDA are congruent and B, D are on opposite sides
// of line AC, then BC ∥ DA (alternate interior angles at C and A).
lemma parallel_of_congruent: all A:Point, B:Point, C:Point, D:Point.
  if not (A = C) and opposite_sides(B, D, line(A, C)) and tri_congruent(A, B, C, C, D, A)
  then parallel(line(B, C), line(D, A))
proof
  arbitrary A:Point, B:Point, C:Point, D:Point
  assume h
  have ca: not (C = A) by { assume e  apply (conjunct 0 of h) to symmetric e }
  have opp: opposite_sides(B, D, line(C, A)) by replace line_symmetric[A, C] in conjunct 1 of h
  replace line_symmetric[B, C] | line_symmetric[D, A]
  replace apply line_parallel_alt_interior[C, B, A, D] to ca, opp
  conjunct 5 of apply tri_cpctc to conjunct 2 of h
end

// If both pairs of opposite sides of a quadrilateral are congruent,
// then it is a parallelogram.
theorem parallelogram_of_opposite_sides: all A:Point, B:Point, C:Point, D:Point.
  if quadrilateral(A, B, C, D) and seg_congruent(A, B, C, D) and seg_congruent(B, C, D, A)
  then parallelogram(A, B, C, D)
proof
  arbitrary A:Point, B:Point, C:Point, D:Point
  assume h
  have ac: not (A = C) by conjunct 4 of apply quad_distinct to conjunct 0 of h
  have opp: opposite_sides(B, D, line(A, C)) by conjunct 0 of apply quad_diagonals to conjunct 0 of h
  // Diagonal AC: △ABC ≅ △CDA by SSS.
  have cong: tri_congruent(A, B, C, C, D, A)
    by apply tri_sss[A, B, C, C, D, A]
       to (conjunct 1 of h), (conjunct 2 of h), point_seg_congruent_reverse[C, A]
  // ∠BAC ≅ ∠DCA, so AB ∥ CD by alternate interior angles.
  have p1: parallel(line(A, B), line(C, D)) by {
    replace apply line_parallel_alt_interior[A, B, C, D] to ac, opp
    expand angle_congruent
    replace angle_measure_symmetric[B, A, C] | angle_measure_symmetric[D, C, A]
    expand angle_congruent in conjunct 3 of apply tri_cpctc to cong
  }
  have p2: parallel(line(B, C), line(D, A)) by apply parallel_of_congruent to ac, opp, cong
  expand parallelogram
  ((conjunct 0 of h), p1, p2)
end

// If one pair of opposite sides of a quadrilateral is both parallel and
// congruent, then it is a parallelogram.
theorem parallelogram_of_one_pair: all A:Point, B:Point, C:Point, D:Point.
  if quadrilateral(A, B, C, D) and parallel(line(A, B), line(C, D)) and seg_congruent(A, B, C, D)
  then parallelogram(A, B, C, D)
proof
  arbitrary A:Point, B:Point, C:Point, D:Point
  assume h
  have ac: not (A = C) by conjunct 4 of apply quad_distinct to conjunct 0 of h
  have opp: opposite_sides(B, D, line(A, C)) by conjunct 0 of apply quad_diagonals to conjunct 0 of h
  // ∠BAC ≅ ∠DCA by alternate interior angles.
  have alt: angle_congruent(C, A, B, A, C, D) by {
    have a: angle_congruent(B, A, C, D, C, A)
      by replace apply line_parallel_alt_interior[A, B, C, D] to ac, opp in conjunct 1 of h
    expand angle_congruent
    replace angle_measure_symmetric[C, A, B] | angle_measure_symmetric[A, C, D]
    expand angle_congruent in a
  }
  // Diagonal AC: △ABC ≅ △CDA by SAS.
  have cong: tri_congruent(A, B, C, C, D, A)
    by apply tri_sas[A, B, C, C, D, A]
       to (conjunct 0 of apply quad_triangles to conjunct 0 of h), (conjunct 2 of h), alt,
          point_seg_congruent_reverse[A, C]
  have p2: parallel(line(B, C), line(D, A)) by apply parallel_of_congruent to ac, opp, cong
  expand parallelogram
  ((conjunct 0 of h), (conjunct 1 of h), p2)
end

// If the diagonals of a quadrilateral bisect each other, then it is a
// parallelogram.
theorem parallelogram_of_diagonals: all A:Point, B:Point, C:Point, D:Point.
  if quadrilateral(A, B, C, D) and midpoint(A, C) = midpoint(B, D)
  then parallelogram(A, B, C, D)
proof
  arbitrary A:Point, B:Point, C:Point, D:Point
  assume h
  have mx: xcoord(midpoint(A, C)) = xcoord(midpoint(B, D)) by { replace conjunct 1 of h. }
  have my: ycoord(midpoint(A, C)) = ycoord(midpoint(B, D)) by { replace conjunct 1 of h. }
  have cx: xcoord(C) = xcoord(B) + xcoord(D) - xcoord(A) by {
    have e: real(frac(+1, 2)) * (xcoord(A) + xcoord(C)) = real(frac(+1, 2)) * (xcoord(B) + xcoord(D))
      by expand midpoint in mx
    algebra with e
  }
  have cy: ycoord(C) = ycoord(B) + ycoord(D) - ycoord(A) by {
    have e: real(frac(+1, 2)) * (ycoord(A) + ycoord(C)) = real(frac(+1, 2)) * (ycoord(B) + ycoord(D))
      by expand midpoint in my
    algebra with e
  }
  apply parallelogram_of_vec to (conjunct 0 of h), cx, cy
end

/*
  Rectangles, rhombuses, and squares
*/

// A rectangle is a parallelogram: two lines perpendicular to the same
// line are parallel.
theorem rectangle_parallelogram: all A:Point, B:Point, C:Point, D:Point.
  if rectangle(A, B, C, D) then parallelogram(A, B, C, D)
proof
  arbitrary A:Point, B:Point, C:Point, D:Point
  expand rectangle
  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 ab: not (A = B) by conjunct 0 of ds
  have bc: not (B = C) by conjunct 1 of ds
  have da: not (D = A) by conjunct 3 of ds
  have ba: not (B = A) by { assume e  apply ab to symmetric e }
  have cb: not (C = B) by { assume e  apply bc to symmetric e }
  have dc: not (D = C) by { assume e  apply (conjunct 2 of ds) to symmetric e }
  // AD ⊥ AB, BA ⊥ BC, and CB ⊥ CD.
  have pa: perpendicular(line(D, A), line(A, B)) by {
    replace symmetric line_symmetric[A, D]
    replace symmetric apply angle_right_perpendicular[D, A, B] to da, ba
    conjunct 1 of h
  }
  have pb: perpendicular(line(A, B), line(B, C)) by {
    replace line_symmetric[A, B]
    replace symmetric apply angle_right_perpendicular[A, B, C] to ab, cb
    conjunct 2 of h
  }
  have pc: perpendicular(line(B, C), line(C, D)) by {
    replace line_symmetric[B, C]
    replace symmetric apply angle_right_perpendicular[B, C, D] to bc, dc
    conjunct 3 of h
  }
  have p1: parallel(line(A, B), line(C, D))
    by apply line_perpendicular_parallel[line(A, B), line(C, D), line(B, C)]
       to pb, (apply line_perpendicular_symmetric to pc)
  have p2: parallel(line(B, C), line(D, A))
    by apply line_perpendicular_parallel[line(B, C), line(D, A), line(A, B)]
       to (apply line_perpendicular_symmetric to pb), pa
  expand parallelogram
  ((conjunct 0 of h), p1, p2)
end

// The diagonals of a rectangle are congruent.
theorem rectangle_diagonals: all A:Point, B:Point, C:Point, D:Point.
  if rectangle(A, B, C, D) then seg_congruent(A, C, B, D)
proof
  arbitrary A:Point, B:Point, C:Point, D:Point
  assume h
  have v: xcoord(C) = xcoord(B) + xcoord(D) - xcoord(A)
          and ycoord(C) = ycoord(B) + ycoord(D) - ycoord(A)
    by apply parallelogram_vec to apply rectangle_parallelogram to 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 expand rectangle in h
  have ba: not (B = A) by { assume e  apply (conjunct 0 of ds) to symmetric e }
  // The right angle at A: AD · AB = 0.
  have r: dot(A, D, B) = real(+0)
    by replace apply angle_right_dot[D, A, B] to (conjunct 3 of ds), ba
       in conjunct 1 of expand rectangle in h
  // AC² = BD² + 4 AD · AB.
  have e: dist2(A, C) = dist2(B, D) + real(+4) * dot(A, D, B) by {
    expand dist2 | dot
    replace conjunct 0 of v | conjunct 1 of v
    algebra
  }
  expand seg_congruent
  replace e | r.
end

// A rhombus is a parallelogram.
theorem rhombus_parallelogram: all A:Point, B:Point, C:Point, D:Point.
  if rhombus(A, B, C, D) then parallelogram(A, B, C, D)
proof
  arbitrary A:Point, B:Point, C:Point, D:Point
  expand rhombus
  assume h
  apply parallelogram_of_opposite_sides
    to (conjunct 0 of h),
       (apply point_seg_congruent_trans[A, B, B, C, C, D] to (conjunct 1 of h), (conjunct 2 of h)),
       (apply point_seg_congruent_trans[B, C, C, D, D, A] to (conjunct 2 of h), (conjunct 3 of h))
end

// The diagonals of a rhombus are perpendicular.
theorem rhombus_diagonals: all A:Point, B:Point, C:Point, D:Point.
  if rhombus(A, B, C, D) then perpendicular(line(A, C), line(B, D))
proof
  arbitrary A:Point, B:Point, C:Point, D:Point
  assume h
  have v: xcoord(C) = xcoord(B) + xcoord(D) - xcoord(A)
          and ycoord(C) = ycoord(B) + ycoord(D) - ycoord(A)
    by apply parallelogram_vec to apply rhombus_parallelogram to h
  have hr: quadrilateral(A, B, C, D) and seg_congruent(A, B, B, C) and seg_congruent(B, C, C, D)
           and seg_congruent(C, D, D, A) by expand rhombus 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 hr
  // AB ≅ DA.
  have ad: dist2(A, B) = dist2(D, A) by {
    have s: seg_congruent(A, B, D, A)
      by apply point_seg_congruent_trans[A, B, C, D, D, A]
         to (apply point_seg_congruent_trans[A, B, B, C, C, D] to (conjunct 1 of hr), (conjunct 2 of hr)),
            (conjunct 3 of hr)
    expand seg_congruent in s
  }
  // AC · BD = AD² - AB².
  have e: seg_dot(A, C, B, D) = dist2(D, A) - dist2(A, B) by {
    expand seg_dot | dist2
    replace conjunct 0 of v | conjunct 1 of v
    algebra
  }
  replace apply line_perpendicular_seg_dot[A, C, B, D] to (conjunct 4 of ds), (conjunct 5 of ds)
        | e | ad.
end

// The diagonals of a square are congruent and perpendicular.
theorem square_diagonals: all A:Point, B:Point, C:Point, D:Point.
  if square(A, B, C, D)
  then seg_congruent(A, C, B, D) and perpendicular(line(A, C), line(B, D))
proof
  arbitrary A:Point, B:Point, C:Point, D:Point
  expand square
  assume h
  ((apply rectangle_diagonals to conjunct 0 of h), (apply rhombus_diagonals to conjunct 1 of h))
end