module Circle

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

  `circle(O, r)` is the circle with center O and radius r, and
  `center(c)` and `radius(c)` give them back. The representation of
  `Circle` is private.

  * `on_circle(P, c)`: P is on c, that is, its distance from the
    center is the radius. `inside_circle(P, c)` and
    `outside_circle(P, c)`: the distance is less, or greater.
  * `chord(A, B, c)`: A and B are distinct points of c.
    `diameter(A, B, c)`: a chord that contains the center.
  * `tangent_at(L, c, P)`: the line L meets c at P and at no other
    point. `tangent(L, c)`: L meets c at exactly one point.
    `secant(L, c)`: L meets c at two points.

  Theorems:

  * Tangents: a tangent is perpendicular to the radius at the point of
    tangency (`circle_tangent_perpendicular`; in coordinates,
    `circle_tangent_dot`: PO · PX = 0 for each point X of the tangent
    at P), and a line perpendicular to a radius at its endpoint on the
    circle is a tangent (`circle_perpendicular_tangent`). The other points of a tangent are
    outside the circle (`circle_tangent_outside`), the two tangent
    segments from a point are congruent (`tangent_segments_theorem`),
    and a tangent is not a secant (`circle_tangent_not_secant`).
  * Chords: a line through the center perpendicular to a chord bisects
    it (`circle_chord_perpendicular_bisect`), the line from the center
    to the midpoint of a chord is perpendicular to it
    (`circle_chord_midpoint_perpendicular`), a chord whose line passes
    through the center has the center as its midpoint
    (`circle_chord_center`), and two chords are congruent exactly when
    they are equidistant from the center (`equidistant_chords_theorem`;
    the distance from the center to a chord is the distance to its
    midpoint).
  * Thales' Theorem: an angle inscribed in a semicircle is a right
    angle (`thales_theorem`), and conversely, if ∠ACB is a right angle,
    then C is on the circle with diameter AB
    (`thales_theorem_converse`).
  * `circle_dist2_radius` and `circle_dist2_same`: the squared
    distance from the center to a point of the circle is r².

  The Inscribed Angle Theorem and the other theorems about angles in a
  circle are in lib/InscribedAngle.pf and lib/Arc.pf, and the segments
  of chords, secants, and tangents (the power of a point) in
  lib/CircleSegments.pf.

  How they are proved: from coordinates. A point Q of the tangent at P
  satisfies OQ² = OP² + PQ², because PO · PQ = 0; and PO · PQ = 0
  because otherwise line PQ meets the circle again, at
  P + (2 PO · PQ / PQ²)(Q - P). For a chord AB with midpoint M,
  2 OM · AB = OB² - OA² = 0, and 4 OM² = 2 OA² + 2 OB² - AB²
  (Apollonius). Thales and its converse: CA · CB = MC² - MA² for the
  midpoint M of AB.
*/

import Base
public import AngleMeasure
import Linear
import Ring

opaque union Circle {
  circle_of(Point, Real)
}

// The circle with center O and radius r.
opaque fun circle(O : Point, r : Real) { circle_of(O, r) }

opaque fun center(c : Circle) {
  switch c { case circle_of(O, r) { O } }
}

opaque fun radius(c : Circle) {
  switch c { case circle_of(O, r) { r } }
}

// P is on the circle c.
fun on_circle(P : Point, c : Circle) { dist(center(c), P) = radius(c) }

// P is inside the circle c.
fun inside_circle(P : Point, c : Circle) { dist(center(c), P) < radius(c) }

// P is outside the circle c.
fun outside_circle(P : Point, c : Circle) { radius(c) < dist(center(c), P) }

// AB is a chord of c.
fun chord(A : Point, B : Point, c : Circle) {
  not (A = B) and on_circle(A, c) and on_circle(B, c)
}

// AB is a diameter of c: a chord through the center.
fun diameter(A : Point, B : Point, c : Circle) {
  chord(A, B, c) and on_segment(center(c), A, B)
}

// The line L is tangent to c at P: P is the only point of L on c.
fun tangent_at(L : Line, c : Circle, P : Point) {
  on_line(P, L) and on_circle(P, c)
  and (all Q:Point. if on_line(Q, L) and on_circle(Q, c) then Q = P)
}

// The line L is tangent to c: it meets c at exactly one point.
fun tangent(L : Line, c : Circle) { some P:Point. tangent_at(L, c, P) }

// The line L is a secant of c: it meets c at two points.
fun secant(L : Line, c : Circle) {
  some A:Point, B:Point. not (A = B) and on_line(A, L) and on_line(B, L)
                         and on_circle(A, c) and on_circle(B, c)
}

theorem circle_center: all O:Point, r:Real. center(circle(O, r)) = O
proof
  arbitrary O:Point, r:Real
  expand circle | center.
end

theorem circle_radius: all O:Point, r:Real. radius(circle(O, r)) = r
proof
  arbitrary O:Point, r:Real
  expand circle | radius.
end

auto circle_center
auto circle_radius

/*
  Points on a circle
*/

// The squared distance from the center to a point of the circle is r².
theorem circle_dist2_radius: all P:Point, c:Circle.
  if on_circle(P, c) then dist2(center(c), P) = radius(c) * radius(c)
proof
  arbitrary P:Point, c:Circle
  expand on_circle
  assume h
  replace symmetric h
  symmetric point_dist_square[center(c), P]
end

theorem circle_dist2_same: all P:Point, Q:Point, c:Circle.
  if on_circle(P, c) and on_circle(Q, c) then dist2(center(c), P) = dist2(center(c), Q)
proof
  arbitrary P:Point, Q:Point, c:Circle
  assume h
  transitive (apply circle_dist2_radius[P, c] to conjunct 0 of h)
             (symmetric apply circle_dist2_radius[Q, c] to conjunct 1 of h)
end

lemma on_circle_of_dist2: all P:Point, Q:Point, c:Circle.
  if on_circle(P, c) and dist2(center(c), Q) = dist2(center(c), P) then on_circle(Q, c)
proof
  arbitrary P:Point, Q:Point, c:Circle
  assume h
  have e: dist(center(c), Q) = dist(center(c), P) by {
    replace symmetric point_seg_congruent_dist[center(c), Q, center(c), P]
    expand seg_congruent
    conjunct 1 of h
  }
  expand on_circle
  transitive e (expand on_circle in conjunct 0 of h)
end

lemma center_ne: all P:Point, c:Circle.
  if real(+0) < radius(c) and on_circle(P, c) then not (center(c) = P)
proof
  arbitrary P:Point, c:Circle
  assume h
  assume e
  have d: dist(center(c), P) = radius(c) by expand on_circle in conjunct 1 of h
  have z: dist(center(c), P) = real(+0) by { replace e  point_dist_self[P] }
  conclude false by algebra with d | z | conjunct 0 of h
end

/*
  Tangents
*/

// Every line has a point other than P.
lemma line_other_point: all L:Line, P:Point. some Q:Point. not (Q = P) and on_line(Q, L)
proof
  arbitrary L:Line, P:Point
  obtain X, Y where xy from line_points[L]
  have l: L = line(X, Y) by conjunct 1 of xy
  switch X = P {
    case true assume xp {
      have yp: not (Y = P) by { assume e  apply (conjunct 0 of xy) to transitive xp (symmetric e) }
      have yl: on_line(Y, L) by { replace l  line_on_right[X, Y] }
      choose Y
      conclude not (Y = P) and on_line(Y, L) by (yp, yl)
    }
    case false assume xp {
      have xl: on_line(X, L) by { replace l  line_on_left[X, Y] }
      choose X
      conclude not (X = P) and on_line(X, L) by (xp, xl)
    }
  }
end

// The radius OP is perpendicular to every segment PX of the tangent at P.
theorem circle_tangent_dot: all L:Line, c:Circle, P:Point, X:Point.
  if tangent_at(L, c, P) and on_line(X, L) then dot(P, center(c), X) = real(+0)
proof
  arbitrary L:Line, c:Circle, P:Point, X:Point
  assume h
  have t: on_line(P, L) and on_circle(P, c)
          and (all Q:Point. if on_line(Q, L) and on_circle(Q, c) then Q = P)
    by expand tangent_at in conjunct 0 of h
  switch X = P {
    case true assume xp { replace xp  expand dot. }
    case false assume xp {
      have px: not (P = X) by { assume e  apply xp to symmetric e }
      have dz: not (dist2(P, X) = real(+0)) by { replace point_dist2_zero[P, X]  px }
      have l: L = line(P, X) by apply line_unique[P, X, L] to px, conjunct 0 of t, conjunct 1 of h
      // The other point Y where line PX meets the circle: Y = P + s (X - P).
      define s = real(+2) * dot(P, center(c), X) / dist2(P, X)
      define Y = point(xcoord(P) + s * (xcoord(X) - xcoord(P)),
                       ycoord(P) + s * (ycoord(X) - ycoord(P)))
      have yl: on_line(Y, L) by {
        replace l | apply line_on_collinear[P, X, Y] to px
        expand collinear | cross | Y
        algebra
      }
      have ss: s * dist2(P, X) = real(+2) * dot(P, center(c), X) by { expand s  algebra }
      have yc: on_circle(Y, c) by {
        have e: dist2(center(c), Y)
                = dist2(center(c), P) + s * (s * dist2(P, X) - real(+2) * dot(P, center(c), X))
          by { expand dist2 | dot | Y  algebra }
        have z: s * (s * dist2(P, X) - real(+2) * dot(P, center(c), X)) = real(+0)
          by { replace ss. }
        have e2: dist2(center(c), Y) = dist2(center(c), P) by algebra with e | z
        apply on_circle_of_dist2[P, Y, c] to conjunct 1 of t, e2
      }
      have yp: Y = P by apply (conjunct 2 of t)[Y] to yl, yc
      have z: dist2(P, X) * (s * s) = real(+0) by {
        have e: dist2(P, Y) = dist2(P, X) * (s * s) by { expand dist2 | Y  algebra }
        replace symmetric e | yp
        point_dist2_self[P]
      }
      have s0: s * s = real(+0) by replace apply real_mult_zero_iff[dist2(P, X), s * s] to dz in z
      have s1: s = real(+0) by {
        cases apply real_mult_to_zero[s, s] to s0
        case a { a }
        case b { b }
      }
      have d2: s * dist2(P, X) = real(+0) by { replace s1. }
      algebra with ss | d2
    }
  }
end

// A tangent is perpendicular to the radius at the point of tangency.
theorem circle_tangent_perpendicular: all L:Line, c:Circle, P:Point.
  if real(+0) < radius(c) and tangent_at(L, c, P) then perpendicular(L, line(center(c), P))
proof
  arbitrary L:Line, c:Circle, P:Point
  assume h
  have t: on_line(P, L) and on_circle(P, c)
          and (all Q:Point. if on_line(Q, L) and on_circle(Q, c) then Q = P)
    by expand tangent_at in conjunct 1 of h
  have op: not (center(c) = P) by apply center_ne to conjunct 0 of h, conjunct 1 of t
  obtain Q where hq from line_other_point[L, P]
  have pq: not (P = Q) by { assume e  apply (conjunct 0 of hq) to symmetric e }
  have l: L = line(P, Q) by apply line_unique[P, Q, L] to pq, conjunct 0 of t, conjunct 1 of hq
  have d: dot(P, center(c), Q) = real(+0)
    by apply circle_tangent_dot[L, c, P, Q] to conjunct 1 of h, conjunct 1 of hq
  have sd: seg_dot(P, Q, center(c), P) = - dot(P, center(c), Q) by { expand seg_dot | dot  algebra }
  replace l | apply line_perpendicular_seg_dot[P, Q, center(c), P] to pq, op | sd
  algebra with d
end

// A line perpendicular to a radius at its endpoint on the circle is a
// tangent.
theorem circle_perpendicular_tangent: all L:Line, c:Circle, P:Point.
  if real(+0) < radius(c) and on_circle(P, c) and on_line(P, L)
     and perpendicular(L, line(center(c), P))
  then tangent_at(L, c, P)
proof
  arbitrary L:Line, c:Circle, P:Point
  assume h
  have op: not (center(c) = P) by apply center_ne to conjunct 0 of h, conjunct 1 of h
  have only: all Q:Point. if on_line(Q, L) and on_circle(Q, c) then Q = P by {
    arbitrary Q:Point
    assume hq
    switch Q = P {
      case true assume qp { qp }
      case false assume qp {
        have pq: not (P = Q) by { assume e  apply qp to symmetric e }
        have l: L = line(P, Q) by apply line_unique[P, Q, L] to pq, conjunct 2 of h, conjunct 0 of hq
        have sd: seg_dot(P, Q, center(c), P) = - dot(P, center(c), Q)
          by { expand seg_dot | dot  algebra }
        have z: seg_dot(P, Q, center(c), P) = real(+0)
          by replace l | apply line_perpendicular_seg_dot[P, Q, center(c), P] to pq, op
             in conjunct 3 of h
        have d: dot(P, center(c), Q) = real(+0) by algebra with sd | z
        have py: dist2(center(c), Q) = dist2(P, center(c)) + dist2(P, Q)
          by apply point_pythagoras[P, center(c), Q] to d
        have same: dist2(center(c), Q) = dist2(center(c), P)
          by apply circle_dist2_same[Q, P, c] to conjunct 1 of hq, conjunct 1 of h
        have z2: dist2(P, Q) = real(+0)
          by algebra with py | same | point_dist2_symmetric[P, center(c)]
        conclude false by apply pq to replace point_dist2_zero[P, Q] in z2
      }
    }
  }
  expand tangent_at
  (conjunct 2 of h, conjunct 1 of h, only)
end

lemma square_le: all a:Real, b:Real. if real(+0) ≤ a and a ≤ b then a * a ≤ b * b
proof
  arbitrary a:Real, b:Real
  assume h
  have p: real(+0) ≤ (b - a) * (b + a)
    by apply real_mult_nonneg to (conclude real(+0) ≤ b - a by algebra with h),
                                 (conclude real(+0) ≤ b + a by algebra with h)
  have e: (b - a) * (b + a) = b * b - a * a by algebra
  algebra with p | e
end

// The points of a tangent other than the point of tangency are outside
// the circle.
theorem circle_tangent_outside: all L:Line, c:Circle, P:Point, X:Point.
  if tangent_at(L, c, P) and on_line(X, L) and not (X = P) then outside_circle(X, c)
proof
  arbitrary L:Line, c:Circle, P:Point, X:Point
  assume h
  have pc: on_circle(P, c) by conjunct 1 of (expand tangent_at in conjunct 0 of h)
  have py: dist2(center(c), X) = dist2(P, center(c)) + dist2(P, X)
    by apply point_pythagoras[P, center(c), X]
       to apply circle_tangent_dot[L, c, P, X] to conjunct 0 of h, conjunct 1 of h
  have pxn: not (P = X) by { assume e  apply (conjunct 2 of h) to symmetric e }
  have px: real(+0) < dist2(P, X) by apply point_dist2_pos[P, X] to pxn
  have r2: dist2(center(c), P) = radius(c) * radius(c) by apply circle_dist2_radius to pc
  expand outside_circle
  cases real_dichotomy[dist(center(c), X), radius(c)]
  case le {
    have sq: dist(center(c), X) * dist(center(c), X) ≤ radius(c) * radius(c)
      by apply square_le to point_dist_nonneg[center(c), X], le
    conclude false
      by algebra with sq | point_dist_square[center(c), X] | py | px | r2
                    | point_dist2_symmetric[P, center(c)]
  }
  case gt { gt }
end

// The Tangent Segments Theorem: the two tangent segments to a circle
// from a point are congruent.
theorem tangent_segments_theorem: all L:Line, M:Line, c:Circle, A:Point, B:Point, P:Point.
  if tangent_at(L, c, A) and tangent_at(M, c, B) and on_line(P, L) and on_line(P, M)
  then dist(P, A) = dist(P, B)
proof
  arbitrary L:Line, M:Line, c:Circle, A:Point, B:Point, P:Point
  assume h
  have ta: on_circle(A, c) by conjunct 1 of (expand tangent_at in conjunct 0 of h)
  have tb: on_circle(B, c) by conjunct 1 of (expand tangent_at in conjunct 1 of h)
  have pa: dist2(center(c), P) = dist2(A, center(c)) + dist2(A, P)
    by apply point_pythagoras[A, center(c), P]
       to apply circle_tangent_dot[L, c, A, P] to conjunct 0 of h, conjunct 2 of h
  have pb: dist2(center(c), P) = dist2(B, center(c)) + dist2(B, P)
    by apply point_pythagoras[B, center(c), P]
       to apply circle_tangent_dot[M, c, B, P] to conjunct 1 of h, conjunct 3 of h
  have same: dist2(center(c), A) = dist2(center(c), B) by apply circle_dist2_same to ta, tb
  have e: dist2(P, A) = dist2(P, B)
    by algebra with pa | pb | same | point_dist2_symmetric[A, center(c)]
                  | point_dist2_symmetric[B, center(c)] | point_dist2_symmetric[A, P]
                  | point_dist2_symmetric[B, P]
  replace symmetric point_seg_congruent_dist[P, A, P, B]
  expand seg_congruent
  e
end

// A tangent is not a secant.
theorem circle_tangent_not_secant: all L:Line, c:Circle. if tangent(L, c) then not secant(L, c)
proof
  arbitrary L:Line, c:Circle
  assume t
  obtain P where tp from expand tangent in t
  have only: all Q:Point. if on_line(Q, L) and on_circle(Q, c) then Q = P
    by conjunct 2 of (expand tangent_at in tp)
  expand secant
  assume s
  obtain A, B where hab from s
  have a: A = P by apply only[A] to conjunct 1 of hab, conjunct 3 of hab
  have b: B = P by apply only[B] to conjunct 2 of hab, conjunct 4 of hab
  conclude false by apply (conjunct 0 of hab) to transitive a (symmetric b)
end

/*
  Chords
*/

lemma half_of: all k:Real, w:Real, u:Real.
  if not (k = real(+0)) and (real(+2) * k) * w = k * u then real(+2) * w = u
proof
  arbitrary k:Real, w:Real, u:Real
  assume h
  have e: k * (real(+2) * w - u) = (real(+2) * k) * w - k * u by algebra
  have z: k * (real(+2) * w - u) = real(+0) by algebra with e | conjunct 1 of h
  have z2: real(+2) * w - u = real(+0)
    by replace apply real_mult_zero_iff[k, real(+2) * w - u] to conjunct 0 of h in z
  algebra with z2
end

// A point M of line AB with 2 AB · AM = AB² is the midpoint of AB.
lemma midpoint_of_dot: all A:Point, B:Point, M:Point.
  if not (A = B) and collinear(A, B, M) and real(+2) * dot(A, B, M) = dist2(A, B)
  then M = midpoint(A, B)
proof
  arbitrary A:Point, B:Point, M:Point
  assume h
  have cz: cross(A, B, M) = real(+0) by expand collinear in conjunct 1 of h
  have kz: not (dot(A, B, M) = real(+0)) by {
    assume z
    have d0: dist2(A, B) = real(+0) by algebra with z | conjunct 2 of h
    apply (conjunct 0 of h) to replace point_dist2_zero[A, B] in d0
  }
  // In the frame of AB: AB² (AM) = (AB · AM) AB + (AB × AM) AB⊥.
  have fx: dist2(A, B) * (xcoord(M) - xcoord(A))
           = dot(A, B, M) * (xcoord(B) - xcoord(A)) - cross(A, B, M) * (ycoord(B) - ycoord(A))
    by { expand dist2 | dot | cross  algebra }
  have fy: dist2(A, B) * (ycoord(M) - ycoord(A))
           = dot(A, B, M) * (ycoord(B) - ycoord(A)) + cross(A, B, M) * (xcoord(B) - xcoord(A))
    by { expand dist2 | dot | cross  algebra }
  have gx: (real(+2) * dot(A, B, M)) * (xcoord(M) - xcoord(A)) = dot(A, B, M) * (xcoord(B) - xcoord(A))
    by replace symmetric (conjunct 2 of h) | cz in fx
  have gy: (real(+2) * dot(A, B, M)) * (ycoord(M) - ycoord(A)) = dot(A, B, M) * (ycoord(B) - ycoord(A))
    by replace symmetric (conjunct 2 of h) | cz in fy
  have hx: real(+2) * (xcoord(M) - xcoord(A)) = xcoord(B) - xcoord(A)
    by apply half_of to kz, gx
  have hy: real(+2) * (ycoord(M) - ycoord(A)) = ycoord(B) - ycoord(A)
    by apply half_of to kz, gy
  have ex: xcoord(M) = xcoord(midpoint(A, B)) by { expand midpoint  algebra with hx }
  have ey: ycoord(M) = ycoord(midpoint(A, B)) by { expand midpoint  algebra with hy }
  apply point_equal[M, midpoint(A, B)] to ex, ey
end

// A line through the center perpendicular to a chord bisects it: if
// OM ⊥ AB for a point M of chord AB's line, then M is the midpoint.
theorem circle_chord_perpendicular_bisect: all c:Circle, A:Point, B:Point, M:Point.
  if chord(A, B, c) and on_line(M, line(A, B)) and not (center(c) = M)
     and perpendicular(line(center(c), M), line(A, B))
  then M = midpoint(A, B)
proof
  arbitrary c:Circle, A:Point, B:Point, M:Point
  assume h
  have ch: not (A = B) and on_circle(A, c) and on_circle(B, c) by expand chord in conjunct 0 of h
  have ab: not (A = B) by conjunct 0 of ch
  have sd: seg_dot(center(c), M, A, B) = real(+0)
    by replace apply line_perpendicular_seg_dot[center(c), M, A, B] to conjunct 2 of h, ab
       in conjunct 3 of h
  have col: collinear(A, B, M) by replace apply line_on_collinear[A, B, M] to ab in conjunct 1 of h
  have same: dist2(center(c), A) = dist2(center(c), B)
    by apply circle_dist2_same to conjunct 1 of ch, conjunct 2 of ch
  have e: real(+2) * dot(A, B, M) - dist2(A, B)
          = real(+2) * seg_dot(center(c), M, A, B) + (dist2(center(c), A) - dist2(center(c), B))
    by { expand dot | dist2 | seg_dot  algebra }
  have two: real(+2) * dot(A, B, M) = dist2(A, B) by algebra with e | sd | same
  apply midpoint_of_dot to ab, col, two
end

// If the line of a chord passes through the center, the center is the
// midpoint of the chord (so the chord is a diameter).
theorem circle_chord_center: all c:Circle, A:Point, B:Point.
  if chord(A, B, c) and on_line(center(c), line(A, B)) then center(c) = midpoint(A, B)
proof
  arbitrary c:Circle, A:Point, B:Point
  assume h
  have ch: not (A = B) and on_circle(A, c) and on_circle(B, c) by expand chord in conjunct 0 of h
  have ab: not (A = B) by conjunct 0 of ch
  have col: collinear(A, B, center(c))
    by replace apply line_on_collinear[A, B, center(c)] to ab in conjunct 1 of h
  have same: dist2(center(c), A) = dist2(center(c), B)
    by apply circle_dist2_same to conjunct 1 of ch, conjunct 2 of ch
  have two: real(+2) * dot(A, B, center(c)) = dist2(A, B)
    by algebra with point_dist2_dot[A, B, center(c)] | same
                  | point_dist2_symmetric[B, center(c)] | point_dist2_symmetric[A, center(c)]
  apply midpoint_of_dot to ab, col, two
end

// The line from the center to the midpoint of a chord is perpendicular
// to the chord.
theorem circle_chord_midpoint_perpendicular: all c:Circle, A:Point, B:Point.
  if chord(A, B, c) and not (center(c) = midpoint(A, B))
  then perpendicular(line(center(c), midpoint(A, B)), line(A, B))
proof
  arbitrary c:Circle, A:Point, B:Point
  assume h
  have ch: not (A = B) and on_circle(A, c) and on_circle(B, c) by expand chord in conjunct 0 of h
  have same: dist2(center(c), A) = dist2(center(c), B)
    by apply circle_dist2_same to conjunct 1 of ch, conjunct 2 of ch
  have e: real(+2) * seg_dot(center(c), midpoint(A, B), A, B)
          = dist2(center(c), B) - dist2(center(c), A)
    by { expand seg_dot | midpoint | dist2  algebra }
  replace apply line_perpendicular_seg_dot[center(c), midpoint(A, B), A, B]
          to conjunct 1 of h, conjunct 0 of ch
  algebra with e | same
end

// Apollonius's theorem: 4 OM² = 2 OA² + 2 OB² - AB² for the midpoint M of AB.
lemma midpoint_apollonius: all O:Point, A:Point, B:Point.
  real(+4) * dist2(O, midpoint(A, B)) = real(+2) * dist2(O, A) + real(+2) * dist2(O, B) - dist2(A, B)
proof
  arbitrary O:Point, A:Point, B:Point
  expand midpoint | dist2
  algebra
end

lemma equidistant_scalar: all P:Real, Q:Real, X:Real, Y:Real, a:Real, b:Real, c:Real, d:Real,
    r:Real.
  if real(+4) * P = real(+2) * a + real(+2) * b - X and real(+4) * Q = real(+2) * c + real(+2) * d - Y
     and a = r and b = r and c = r and d = r
  then (X = Y) = (P = Q)
proof
  arbitrary P:Real, Q:Real, X:Real, Y:Real, a:Real, b:Real, c:Real, d:Real, r:Real
  assume h
  have fwd: if X = Y then P = Q by { assume e  algebra with h | e }
  have bwd: if P = Q then X = Y by { assume e  algebra with h | e }
  apply iff_equal to fwd, bwd
end

// The Equidistant Chords Theorem and its converse: in a circle, two
// chords are congruent exactly when they are equidistant from the
// center (the distance from the center to a chord is the distance to
// its midpoint).
theorem equidistant_chords_theorem: all c:Circle, A:Point, B:Point, C:Point, D:Point.
  if on_circle(A, c) and on_circle(B, c) and on_circle(C, c) and on_circle(D, c)
  then seg_congruent(A, B, C, D)
     = seg_congruent(center(c), midpoint(A, B), center(c), midpoint(C, D))
proof
  arbitrary c:Circle, A:Point, B:Point, C:Point, D:Point
  assume h
  expand seg_congruent
  apply equidistant_scalar[dist2(center(c), midpoint(A, B)), dist2(center(c), midpoint(C, D)),
                           dist2(A, B), dist2(C, D), dist2(center(c), A), dist2(center(c), B),
                           dist2(center(c), C), dist2(center(c), D), radius(c) * radius(c)]
    to midpoint_apollonius[center(c), A, B], midpoint_apollonius[center(c), C, D],
       (apply circle_dist2_radius to conjunct 0 of h), (apply circle_dist2_radius to conjunct 1 of h),
       (apply circle_dist2_radius to conjunct 2 of h), (apply circle_dist2_radius to conjunct 3 of h)
end

// The center of a circle is the midpoint of each diameter.
lemma diameter_midpoint: all c:Circle, A:Point, B:Point.
  if diameter(A, B, c) then center(c) = midpoint(A, B)
proof
  arbitrary c:Circle, A:Point, B:Point
  assume h
  have dm: chord(A, B, c) and on_segment(center(c), A, B) by expand diameter in h
  have ch: not (A = B) and on_circle(A, c) and on_circle(B, c) by expand chord in conjunct 0 of dm
  replace seg_midpoint_iff[A, B, center(c)]
  have e: dist(A, center(c)) = dist(center(c), B) by {
    replace point_dist_symmetric[A, center(c)]
    transitive (expand on_circle in conjunct 1 of ch) (symmetric expand on_circle in conjunct 2 of ch)
  }
  (conjunct 1 of dm, e)
end

// For the midpoint M of AB: CA · CB = MC² - MA².
lemma dot_midpoint: all A:Point, B:Point, C:Point.
  dot(C, A, B) = dist2(midpoint(A, B), C) - dist2(midpoint(A, B), A)
proof
  arbitrary A:Point, B:Point, C:Point
  expand dot | dist2 | midpoint
  algebra
end

// Thales' Theorem: an angle inscribed in a semicircle is a right angle.
theorem thales_theorem: all c:Circle, A:Point, B:Point, C:Point.
  if diameter(A, B, c) and on_circle(C, c) and not (C = A) and not (C = B)
  then right_angle(A, C, B)
proof
  arbitrary c:Circle, A:Point, B:Point, C:Point
  assume h
  have m: center(c) = midpoint(A, B) by apply diameter_midpoint to conjunct 0 of h
  have ch: not (A = B) and on_circle(A, c) and on_circle(B, c)
    by expand chord in conjunct 0 of expand diameter in conjunct 0 of h
  have ac: not (A = C) by { assume e  apply (conjunct 2 of h) to symmetric e }
  have bc: not (B = C) by { assume e  apply (conjunct 3 of h) to symmetric e }
  have same: dist2(center(c), C) = dist2(center(c), A)
    by apply circle_dist2_same to conjunct 1 of h, conjunct 1 of ch
  replace apply angle_right_dot[A, C, B] to ac, bc | dot_midpoint[A, B, C] | symmetric m
  algebra with same
end

// The converse of Thales' Theorem: if angle ACB is a right angle, then C
// is on the circle with diameter AB.
theorem thales_theorem_converse: all c:Circle, A:Point, B:Point, C:Point.
  if diameter(A, B, c) and not (C = A) and not (C = B) and right_angle(A, C, B)
  then on_circle(C, c)
proof
  arbitrary c:Circle, A:Point, B:Point, C:Point
  assume h
  have m: center(c) = midpoint(A, B) by apply diameter_midpoint to conjunct 0 of h
  have ch: not (A = B) and on_circle(A, c) and on_circle(B, c)
    by expand chord in conjunct 0 of expand diameter in conjunct 0 of h
  have ac: not (A = C) by { assume e  apply (conjunct 1 of h) to symmetric e }
  have bc: not (B = C) by { assume e  apply (conjunct 2 of h) to symmetric e }
  // CA · CB = 0 = MC² - MA².
  have z: dot(C, A, B) = real(+0)
    by replace apply angle_right_dot[A, C, B] to ac, bc in conjunct 3 of h
  have same: dist2(center(c), C) = dist2(center(c), A) by {
    replace m
    algebra with z | dot_midpoint[A, B, C]
  }
  apply on_circle_of_dist2[A, C, c] to conjunct 1 of ch, same
end