module InscribedAngle

/*
  Angles in a circle, for coordinate geometry (#1191).

  The circle vocabulary (`circle`, `on_circle`, `chord`, ...) is in
  lib/Circle.pf.

  * The Inscribed Angle Theorem: for a chord AB of a circle with center
    O and a point C of the circle other than A and B, if C is on the
    same side of line AB as O, or O is on line AB, then
    m∠ACB = ½ m∠AOB (`inscribed_angle_theorem`); if C and O are on
    opposite sides of line AB, then m∠ACB = 180° - ½ m∠AOB
    (`inscribed_angle_theorem_opposite`).
  * Inscribed angles on the same side of a chord are congruent
    (`circle_inscribed_congruent`), and the opposite angles of a
    quadrilateral inscribed in a circle are supplementary
    (`inscribed_quadrilateral_theorem`). (An angle inscribed in a
    semicircle is right: `thales_theorem` in lib/Circle.pf.)
  * Three distinct points of a circle are not collinear
    (`circle_not_collinear`), so a line meets a circle in at most two
    points (`circle_line_at_most_two`).

  How they are proved: from coordinates. For A, B, C on a circle with
  center O, read d + i c = (CA · CB) + i (CA × CB) and
  p + i q = (OA · OB) + i (OA × OB) as complex numbers. Then
  (d + i c)² = (CA² CB² / r²)(p + i q): the square of the inscribed
  angle is, up to a positive factor, the central angle
  (`circle_identity`, from the frame identities of lib/Point.pf and
  cofactors found by computer algebra). `angle_measure_double`
  (lib/AngleMeasure.pf) turns that into the measures: m∠AOB = 2 m∠ACB
  when CA · CB ≥ 0, which holds when C and O are on the same side of
  AB (then (CA × CB)(OA × OB) > 0, and 2 d c has the sign of q), and
  2 m∠ACB + m∠AOB = 360° when CA · CB ≤ 0. If C were on line AB
  (c = 0), the identity would give OA · OB = r², so A = B.
*/

import Base
public import Circle
import Linear
import Ring

/*
  The key identity

  For A, B, C on a circle with center O, take the products at C:
  U = CA², V = CB², R = CO², d = CA · CB, c = CA × CB, a = CA · CO,
  b = CB · CO, x = CA × CO, and y = CB × CO. A and B are on the circle
  through C with center O, so U = 2a and V = 2b. In the frame of CA
  (`point_dot_frame` and `point_cross_frame` in lib/Point.pf):
  U y = d x - c a, U b = d a + c x, U R = a² + x², and U V = d² + c².
  The products at O are p = OA · OB = d - a - b + R and
  q = OA × OB = c - x + y. Then, with s = U² + 4 x² and t = 4 U² V:
  s (d² - c²) = t p and s (2 d c) = t q. Each difference is a
  combination of the relations, with cofactors found by computer
  algebra.
*/

// If x - y is a combination of the differences of equations, x = y.
lemma zero_comb5: all x:Real, y:Real, g1:Real, g2:Real, g3:Real, g4:Real, g5:Real,
    a1:Real, a2:Real, a3:Real, a4:Real, a5:Real, b1:Real, b2:Real, b3:Real, b4:Real, b5:Real.
  if x - y = g1 * (a1 - b1) + g2 * (a2 - b2) + g3 * (a3 - b3) + g4 * (a4 - b4) + g5 * (a5 - b5)
     and a1 = b1 and a2 = b2 and a3 = b3 and a4 = b4 and a5 = b5
  then x = y
proof
  arbitrary x:Real, y:Real, g1:Real, g2:Real, g3:Real, g4:Real, g5:Real,
    a1:Real, a2:Real, a3:Real, a4:Real, a5:Real, b1:Real, b2:Real, b3:Real, b4:Real, b5:Real
  assume h
  have z1: g1 * (a1 - b1) = real(+0) by { replace conjunct 1 of h. }
  have z2: g2 * (a2 - b2) = real(+0) by { replace conjunct 2 of h. }
  have z3: g3 * (a3 - b3) = real(+0) by { replace conjunct 3 of h. }
  have z4: g4 * (a4 - b4) = real(+0) by { replace conjunct 4 of h. }
  have z5: g5 * (a5 - b5) = real(+0) by { replace conjunct 5 of h. }
  algebra with conjunct 0 of h | z1 | z2 | z3 | z4 | z5
end

lemma circle_scalar: all U:Real, V:Real, R:Real, d:Real, c:Real, a:Real, b:Real, x:Real,
    y:Real, p:Real, q:Real.
  if U = real(+2) * a and V = real(+2) * b
     and U * y = d * x - c * a and U * b = d * a + c * x
     and U * R = a * a + x * x and U * V = d * d + c * c
     and p = d - a - b + R and q = c - x + y
  then (U * U + real(+4) * (x * x)) * (d * d - c * c) = (real(+4) * (U * (U * V))) * p
   and (U * U + real(+4) * (x * x)) * (real(+2) * (d * c)) = (real(+4) * (U * (U * V))) * q
   and real(+4) * (U * R) = U * U + real(+4) * (x * x)
proof
  arbitrary U:Real, V:Real, R:Real, d:Real, c:Real, a:Real, b:Real, x:Real, y:Real,
    p:Real, q:Real
  assume h
  replace conjunct 6 of h | conjunct 7 of h
  define g1 = real(+3) * U * d * d + U * c * c + real(+2) * d * d * a + real(+2) * c * c * a
              - real(+2) * U * U * V - real(+2) * U * V * d - real(+4) * d * c * x
  define g2 = real(+4) * U * c * x - real(+2) * U * U * d
  define g3 = real(+4) * U * V + real(+8) * c * x - real(+4) * U * d
  define g4 = - (real(+4) * d * d + real(+4) * c * c)
  define g5 = real(+2) * U * U - real(+4) * U * R
  have e1: (U * U + real(+4) * (x * x)) * (d * d - c * c)
           - (real(+4) * (U * (U * V))) * (d - a - b + R)
           = g1 * (U - real(+2) * a) + g2 * (V - real(+2) * b) + g3 * (U * b - (d * a + c * x))
             + g4 * (U * R - (a * a + x * x)) + g5 * (U * V - (d * d + c * c))
    by { expand g1 | g2 | g3 | g4 | g5  algebra }
  define h1 = real(+2) * U * d * c + real(+4) * U * d * y - real(+4) * c * c * x
  define h2 = - (real(+4) * U * U * c + real(+4) * U * U * y)
  define h3 = - (real(+4) * U * d + real(+8) * c * x)
  define h4 = - (real(+8) * U * c + real(+8) * U * y)
  define h5 = real(+4) * U * x
  have e2: (U * U + real(+4) * (x * x)) * (real(+2) * (d * c))
           - (real(+4) * (U * (U * V))) * (c - x + y)
           = h1 * (U - real(+2) * a) + h2 * (V - real(+2) * b) + h3 * (U * y - (d * x - c * a))
             + h4 * (U * b - (d * a + c * x)) + h5 * (U * V - (d * d + c * c))
    by { expand h1 | h2 | h3 | h4 | h5  algebra }
  // 4 U R - U² - 4 x² = 4 (U R - a² - x²) - (U + 2a)(U - 2a).
  have e3: real(+4) * (U * R) - (U * U + real(+4) * (x * x))
           = real(+4) * (U * R - (a * a + x * x)) - (U + real(+2) * a) * (U - real(+2) * a)
    by algebra
  have z5: real(+4) * (U * R - (a * a + x * x)) = real(+0) by { replace conjunct 4 of h. }
  have z1: (U + real(+2) * a) * (U - real(+2) * a) = real(+0) by { replace conjunct 0 of h. }
  ((apply zero_comb5 to e1, (conjunct 0 of h), (conjunct 1 of h), (conjunct 3 of h),
                            (conjunct 4 of h), (conjunct 5 of h)),
   (apply zero_comb5 to e2, (conjunct 0 of h), (conjunct 1 of h), (conjunct 2 of h),
                            (conjunct 3 of h), (conjunct 5 of h)),
   (algebra with e3 | z5 | z1))
end

// For A, B, C on a circle with center O, with U = CA², V = CB²,
// d = CA · CB, c = CA × CB, x = CA × CO, s = U² + 4 x², and t = 4 U² V:
// s (d² - c²) = t (OA · OB), s (2 d c) = t (OA × OB), and s = 4 U · CO².
lemma circle_identity: all O:Point, A:Point, B:Point, C:Point.
  if dist2(O, A) = dist2(O, C) and dist2(O, B) = dist2(O, C)
  then (dist2(C, A) * dist2(C, A) + real(+4) * (cross(C, A, O) * cross(C, A, O)))
       * (dot(C, A, B) * dot(C, A, B) - cross(C, A, B) * cross(C, A, B))
       = (real(+4) * (dist2(C, A) * (dist2(C, A) * dist2(C, B)))) * dot(O, A, B)
   and (dist2(C, A) * dist2(C, A) + real(+4) * (cross(C, A, O) * cross(C, A, O)))
       * (real(+2) * (dot(C, A, B) * cross(C, A, B)))
       = (real(+4) * (dist2(C, A) * (dist2(C, A) * dist2(C, B)))) * cross(O, A, B)
   and real(+4) * (dist2(C, A) * dist2(C, O))
       = dist2(C, A) * dist2(C, A) + real(+4) * (cross(C, A, O) * cross(C, A, O))
proof
  arbitrary O:Point, A:Point, B:Point, C:Point
  assume h
  // CA · CO = CA²/2 and CB · CO = CB²/2.
  have hu: dist2(C, A) = real(+2) * dot(C, A, O)
    by algebra with point_dist2_dot[C, A, O] | conjunct 0 of h
                  | point_dist2_symmetric[A, O] | point_dist2_symmetric[C, O]
  have hv: dist2(C, B) = real(+2) * dot(C, B, O)
    by algebra with point_dist2_dot[C, B, O] | conjunct 1 of h
                  | point_dist2_symmetric[B, O] | point_dist2_symmetric[C, O]
  have oo: dot(C, O, O) = dist2(C, O) by { expand dot | dist2  algebra }
  have bb: dot(C, B, B) = dist2(C, B) by { expand dot | dist2  algebra }
  have hp: dot(O, A, B) = dot(C, A, B) - dot(C, A, O) - dot(C, B, O) + dist2(C, O)
    by { expand dot | dist2  algebra }
  have hq: cross(O, A, B) = cross(C, A, B) - cross(C, A, O) + cross(C, B, O)
    by { expand cross  algebra }
  apply circle_scalar[dist2(C, A), dist2(C, B), dist2(C, O), dot(C, A, B), cross(C, A, B),
                      dot(C, A, O), dot(C, B, O), cross(C, A, O), cross(C, B, O),
                      dot(O, A, B), cross(O, A, B)]
    to hu, hv, point_cross_frame[C, A, B, O], point_dot_frame[C, A, B, O],
       (replace oo in point_dot_frame[C, A, O, O]), (replace bb in point_dot_frame[C, A, B, B]),
       hp, hq
end

/*
  Lines and circles
*/

// With the notation of `circle_identity`: if c = 0, then R = p.
lemma noncol_scalar: all U:Real, V:Real, R:Real, d:Real, c:Real, x:Real, p:Real.
  if real(+0) < U and real(+0) < V and c = real(+0) and U * V = d * d + c * c
     and real(+4) * (U * R) = U * U + real(+4) * (x * x)
     and (U * U + real(+4) * (x * x)) * (d * d - c * c) = (real(+4) * (U * (U * V))) * p
  then R = p
proof
  arbitrary U:Real, V:Real, R:Real, d:Real, c:Real, x:Real, p:Real
  assume h
  have uv: U * V = d * d by { replace conjunct 3 of h | conjunct 2 of h. }
  have h5: (U * U + real(+4) * (x * x)) * (d * d) = (real(+4) * (U * (U * V))) * p
    by { replace symmetric (conjunct 5 of h) | conjunct 2 of h. }
  have e: (real(+4) * (U * R)) * (d * d) = (real(+4) * (U * (d * d))) * p
    by { replace conjunct 4 of h | h5 | uv. }
  have k: real(+0) < real(+4) * (U * (d * d)) by {
    have pp: real(+0) < U * (d * d)
      by { replace symmetric uv  apply real_mult_pos to (conjunct 0 of h),
                                   (apply real_mult_pos to (conjunct 0 of h), (conjunct 1 of h)) }
    algebra with pp
  }
  have kz: not (real(+4) * (U * (d * d)) = real(+0)) by { assume z  conclude false by algebra with k | z }
  have z: (real(+4) * (U * (d * d))) * (R - p) = real(+0) by {
    have i: (real(+4) * (U * (d * d))) * (R - p)
            = (real(+4) * (U * R)) * (d * d) - (real(+4) * (U * (d * d))) * p by algebra
    replace i | e.
  }
  have z2: R - p = real(+0)
    by replace apply real_mult_zero_iff[real(+4) * (U * (d * d)), R - p] to kz in z
  algebra with z2
end

// Three distinct points of a circle are not collinear.
theorem circle_not_collinear: all A:Point, B:Point, C:Point, c:Circle.
  if on_circle(A, c) and on_circle(B, c) and on_circle(C, c)
     and not (A = B) and not (B = C) and not (A = C)
  then not collinear(A, B, C)
proof
  arbitrary A:Point, B:Point, C:Point, c:Circle
  assume h
  have oa: dist2(center(c), A) = dist2(center(c), C)
    by apply circle_dist2_same to conjunct 0 of h, conjunct 2 of h
  have ob: dist2(center(c), B) = dist2(center(c), C)
    by apply circle_dist2_same to conjunct 1 of h, conjunct 2 of h
  have ca: not (C = A) by { assume e  apply (conjunct 5 of h) to symmetric e }
  have cb: not (C = B) by { assume e  apply (conjunct 4 of h) to symmetric e }
  have cyc: cross(C, A, B) = cross(A, B, C) by { expand cross  algebra }
  have bb: dot(C, B, B) = dist2(C, B) by { expand dot | dist2  algebra }
  expand collinear
  assume z
  // With CA × CB = 0, the identity gives OA · OB = CO² ...
  have rp: dist2(C, center(c)) = dot(center(c), A, B)
    by apply noncol_scalar[dist2(C, A), dist2(C, B), dist2(C, center(c)), dot(C, A, B),
                           cross(C, A, B), cross(C, A, center(c)), dot(center(c), A, B)]
       to (apply point_dist2_pos to ca), (apply point_dist2_pos to cb), (transitive cyc z),
          (replace bb in point_dot_frame[C, A, B, B]),
          (conjunct 2 of apply circle_identity[center(c), A, B, C] to oa, ob),
          (conjunct 0 of apply circle_identity[center(c), A, B, C] to oa, ob)
  // ... so AB² = OA² + OB² - 2 OA · OB = 0.
  have ab: dist2(A, B) = real(+0)
    by algebra with point_dist2_dot[center(c), A, B] | rp | oa | ob
                  | point_dist2_symmetric[C, center(c)]
  conclude false by apply (conjunct 3 of h) to replace point_dist2_zero[A, B] in ab
end

// A line meets a circle in at most two points.
theorem circle_line_at_most_two: all L:Line, c:Circle, A:Point, B:Point, C:Point.
  if on_line(A, L) and on_line(B, L) and on_line(C, L)
     and on_circle(A, c) and on_circle(B, c) and on_circle(C, c)
  then A = B or B = C or A = C
proof
  arbitrary L:Line, c:Circle, A:Point, B:Point, C:Point
  assume h
  switch A = B {
    case true assume ab { conclude A = B or B = C or A = C by ab }
    case false assume ab {
      switch B = C {
        case true assume bc { conclude A = B or B = C or A = C by bc }
        case false assume bc {
          switch A = C {
            case true assume ac { conclude A = B or B = C or A = C by ac }
            case false assume ac {
              have l: L = line(A, B)
                by apply line_unique[A, B, L] to ab, conjunct 0 of h, conjunct 1 of h
              have col: collinear(A, B, C)
                by replace apply line_on_collinear[A, B, C] to ab in (replace l inconjunct2of              h)haven :notcollinear(A, B, C)
                by apply circle_not_collinear[A, B, C, c]
                   to conjunct 3 of h, conjunct 4 of h, conjunct 5 of h, ab, bc, ac
              conclude false by apply n to col
            }
          }
        }
      }
    }
  }
end

/*
  Inscribed angles
*/

// From s (2 d c) = t q with s, t > 0, c ≠ 0, and c q ≥ 0: d ≥ 0.
lemma side_sign: all s:Real, t:Real, d:Real, c:Real, q:Real.
  if real(+0) < s and real(+0) < t and not (c = real(+0))
     and s * (real(+2) * (d * c)) = t * q and real(+0) ≤ c * q
  then real(+0) ≤ d
proof
  arbitrary s:Real, t:Real, d:Real, c:Real, q:Real
  assume h
  cases real_dichotomy[real(+0), d]
  case nn { nn }
  case neg {
    have cc: real(+0) < c * c by {
      replace real_less_def
      have nz: not (real(+0) = c * c) by {
        assume e
        apply (conjunct 2 of h) to replace apply real_mult_zero_iff[c, c] to conjunct 2 of h in symmetric e
      }
      (real_square_nonneg[c], nz)
    }
    have sd: real(+0) < s * (real(+2) * (- d))
      by apply real_mult_pos
         to (conjunct 0 of h), (conclude real(+0) < real(+2) * (- d) by algebra with neg)
    have a: real(+0) < (s * (real(+2) * (- d))) * (c * c) by apply real_mult_pos to sd, cc
    have b: real(+0) ≤ t * (c * q)
      by apply real_mult_nonneg
         to (apply real_less_implies_less_equal to conjunct 1 of h), (conjunct 4 of h)
    have e: (s * (real(+2) * (d * c))) * c = (t * q) * c by { replace conjunct 3 of h. }
    have i1: (s * (real(+2) * (d * c))) * c = - ((s * (real(+2) * (- d))) * (c * c)) by algebra
    have i2: (t * q) * c = t * (c * q) by algebra
    conclude false by algebra with a | b | e | i1 | i2
  }
end

// From s (2 d c) = t q with s, t > 0, c ≠ 0, and c q ≤ 0: d ≤ 0.
lemma side_sign_neg: all s:Real, t:Real, d:Real, c:Real, q:Real.
  if real(+0) < s and real(+0) < t and not (c = real(+0))
     and s * (real(+2) * (d * c)) = t * q and c * q ≤ real(+0)
  then d ≤ real(+0)
proof
  arbitrary s:Real, t:Real, d:Real, c:Real, q:Real
  assume h
  have l: s * (real(+2) * ((- d) * c)) = - (s * (real(+2) * (d * c))) by algebra
  have r: t * (- q) = - (t * q) by algebra
  have e: s * (real(+2) * ((- d) * c)) = t * (- q) by { replace l | r | conjunct 3 of h. }
  have i: c * (- q) = - (c * q) by algebra
  have n: real(+0) ≤ c * (- q) by { replace i  algebra with conjunct 4 of h }
  have nd: real(+0) ≤ - d
    by apply side_sign to (conjunct 0 of h), (conjunct 1 of h), (conjunct 2 of h), e, n
  algebra with nd
end

// For A, B, C on a circle with center O: m∠AOB = 2 m∠ACB when
// (CA × CB)(OA × OB) ≥ 0, and 2 m∠ACB + m∠AOB = 360° when it is ≤ 0.
lemma inscribed: all c:Circle, A:Point, B:Point, C:Point.
  if chord(A, B, c) and on_circle(C, c) and not (C = A) and not (C = B)
  then (if real(+0) ≤ cross(C, A, B) * cross(center(c), A, B)
        then m_angle(A, C, B) + m_angle(A, C, B) = m_angle(A, center(c), B))
   and (if cross(C, A, B) * cross(center(c), A, B) ≤ real(+0)
        then m_angle(A, C, B) + m_angle(A, C, B) + m_angle(A, center(c), B) = deg(+360))
proof
  arbitrary c:Circle, A:Point, B:Point, C: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 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 oa: dist2(center(c), A) = dist2(center(c), C)
    by apply circle_dist2_same to conjunct 1 of ch, conjunct 1 of h
  have ob: dist2(center(c), B) = dist2(center(c), C)
    by apply circle_dist2_same to conjunct 2 of ch, conjunct 1 of h
  have n: not collinear(A, B, C)
    by apply circle_not_collinear[A, B, C, c]
       to conjunct 1 of ch, conjunct 2 of ch, conjunct 1 of h, conjunct 0 of ch, bc, ac
  have nc: not collinear(A, C, B) by { replace symmetric point_collinear_swap[A, B, C]  n }
  have kz: not (cross(C, A, B) = real(+0)) by {
    have cyc: cross(C, A, B) = cross(A, B, C) by { expand cross  algebra }
    assume z
    apply n to (conclude collinear(A, B, C) by { expand collinear  transitive (symmetric cyc) z })
  }
  have up: real(+0) < dist2(C, A) by apply point_dist2_pos to conjunct 2 of h
  have sp: real(+0) < dist2(C, A) * dist2(C, A) + real(+4) * (cross(C, A, center(c)) * cross(C, A, center(c)))
    by algebra with apply real_mult_pos to up, up | real_square_nonneg[cross(C, A, center(c))]
  have tp: real(+0) < real(+4) * (dist2(C, A) * (dist2(C, A) * dist2(C, B))) by {
    have p: real(+0) < dist2(C, A) * (dist2(C, A) * dist2(C, B))
      by apply real_mult_pos
         to up, (apply real_mult_pos to up, (apply point_dist2_pos to conjunct 3 of h))
    algebra with p
  }
  have i1: (dist2(C, A) * dist2(C, A) + real(+4) * (cross(C, A, center(c)) * cross(C, A, center(c))))
           * (real(+2) * (dot(C, A, B) * cross(C, A, B)))
           = (real(+4) * (dist2(C, A) * (dist2(C, A) * dist2(C, B)))) * cross(center(c), A, B)
    by conjunct 1 of apply circle_identity[center(c), A, B, C] to oa, ob
  have dbl: (if real(+0) ≤ dot(C, A, B)
             then m_angle(A, C, B) + m_angle(A, C, B) = m_angle(A, center(c), B))
        and (if dot(C, A, B) ≤ real(+0)
             then m_angle(A, C, B) + m_angle(A, C, B) + m_angle(A, center(c), B) = deg(+360))
    by apply angle_measure_double[A, C, B, A, center(c), B,
               dist2(C, A) * dist2(C, A) + real(+4) * (cross(C, A, center(c)) * cross(C, A, center(c))),
               real(+4) * (dist2(C, A) * (dist2(C, A) * dist2(C, B)))]
       to nc, sp, tp, (conjunct 0 of apply circle_identity[center(c), A, B, C] to oa, ob), i1
  have acute: if real(+0) ≤ cross(C, A, B) * cross(center(c), A, B)
              then m_angle(A, C, B) + m_angle(A, C, B) = m_angle(A, center(c), B) by {
    assume s
    apply (conjunct 0 of dbl) to apply side_sign to sp, tp, kz, i1, s
  }
  have obtuse: if cross(C, A, B) * cross(center(c), A, B) ≤ real(+0)
               then m_angle(A, C, B) + m_angle(A, C, B) + m_angle(A, center(c), B) = deg(+360) by {
    assume s
    apply (conjunct 1 of dbl) to apply side_sign_neg to sp, tp, kz, i1, s
  }
  (acute, obtuse)
end

// The Inscribed Angle Theorem: if C is on the same side of chord AB as
// the center O, or O is on AB, then m∠ACB = ½ m∠AOB.
theorem inscribed_angle_theorem: all c:Circle, A:Point, B:Point, C:Point.
  if chord(A, B, c) and on_circle(C, c) and not (C = A) and not (C = B)
     and (same_side(C, center(c), line(A, B)) or on_line(center(c), line(A, B)))
  then m_angle(A, C, B) = frac(+1, 2) * m_angle(A, center(c), B)
proof
  arbitrary c:Circle, A:Point, B:Point, C:Point
  assume h
  have ab: not (A = B) by conjunct 0 of (expand chord in conjunct 0 of h)
  have cyc: cross(C, A, B) = cross(A, B, C) by { expand cross  algebra }
  have cyo: cross(center(c), A, B) = cross(A, B, center(c)) by { expand cross  algebra }
  have sign: real(+0) ≤ cross(C, A, B) * cross(center(c), A, B) by {
    replace cyc | cyo
    cases conjunct 4 of h
    case ss {
      have p: real(+0) < cross(A, B, C) * cross(A, B, center(c))
        by replace apply line_same_side_cross[A, B, C, center(c)] to ab in ss
      apply real_less_implies_less_equal to p
    }
    case ol {
      have z: cross(A, B, center(c)) = real(+0)
        by expand collinear in replace apply line_on_collinear[A, B, center(c)] to ab in ol
      replace z.
    }
  }
  have d: m_angle(A, C, B) + m_angle(A, C, B) = m_angle(A, center(c), B)
    by apply (conjunct 0 of apply inscribed[c, A, B, C]
                to conjunct 0 of h, conjunct 1 of h, conjunct 2 of h, conjunct 3 of h) to sign
  algebra with d
end

// The Inscribed Angle Theorem when C and the center O are on opposite
// sides of chord AB: m∠ACB = 180° - ½ m∠AOB.
theorem inscribed_angle_theorem_opposite: all c:Circle, A:Point, B:Point, C:Point.
  if chord(A, B, c) and on_circle(C, c) and opposite_sides(C, center(c), line(A, B))
  then m_angle(A, C, B) = deg(+180) - frac(+1, 2) * m_angle(A, center(c), B)
proof
  arbitrary c:Circle, A:Point, B:Point, C:Point
  assume h
  have ab: not (A = B) by conjunct 0 of (expand chord in conjunct 0 of h)
  have off: not on_line(C, line(A, B)) and not on_line(center(c), line(A, B))
    by apply line_opposite_sides_off to conjunct 2 of h
  have ca: not (C = A) by { assume e  apply (replace e in conjunct 0 of off) to line_on_left[A, B] }
  have cb: not (C = B) by { assume e  apply (replace e in conjunct 0 of off) to line_on_right[A, B] }
  have cyc: cross(C, A, B) = cross(A, B, C) by { expand cross  algebra }
  have cyo: cross(center(c), A, B) = cross(A, B, center(c)) by { expand cross  algebra }
  have sign: cross(C, A, B) * cross(center(c), A, B) ≤ real(+0) by {
    replace cyc | cyo
    have p: cross(A, B, C) * cross(A, B, center(c)) < real(+0)
      by replace apply line_opposite_sides_cross[A, B, C, center(c)] to ab in conjunct 2 of h
    apply real_less_implies_less_equal to p
  }
  have d: m_angle(A, C, B) + m_angle(A, C, B) + m_angle(A, center(c), B) = deg(+360)
    by apply (conjunct 1 of apply inscribed[c, A, B, C]
                to conjunct 0 of h, conjunct 1 of h, ca, cb) to sign
  algebra with d
end

// Inscribed angles on the same side of a chord are congruent.
theorem circle_inscribed_congruent: all c:Circle, A:Point, B:Point, C:Point, D:Point.
  if chord(A, B, c) and on_circle(C, c) and on_circle(D, c) and same_side(C, D, line(A, B))
  then angle_congruent(A, C, B, A, D, B)
proof
  arbitrary c:Circle, A:Point, B:Point, C:Point, D:Point
  assume h
  have off: not on_line(C, line(A, B)) and not on_line(D, line(A, B))
    by apply line_same_side_off to conjunct 3 of h
  have ca: not (C = A) by { assume e  apply (replace e in conjunct 0 of off) to line_on_left[A, B] }
  have cb: not (C = B) by { assume e  apply (replace e in conjunct 0 of off) to line_on_right[A, B] }
  have da: not (D = A) by { assume e  apply (replace e in conjunct 1 of off) to line_on_left[A, B] }
  have db: not (D = B) by { assume e  apply (replace e in conjunct 1 of off) to line_on_right[A, B] }
  have dc: same_side(D, C, line(A, B))
    by replace line_same_side_symmetric[C, D, line(A, B)] in conjunct 3 of h
  expand angle_congruent
  switch on_line(center(c), line(A, B)) {
    case true assume on {
      have mc: m_angle(A, C, B) = frac(+1, 2) * m_angle(A, center(c), B)
        by apply inscribed_angle_theorem[c, A, B, C] to conjunct 0 of h, conjunct 1 of h, ca, cb, on
      have md: m_angle(A, D, B) = frac(+1, 2) * m_angle(A, center(c), B)
        by apply inscribed_angle_theorem[c, A, B, D] to conjunct 0 of h, conjunct 2 of h, da, db, on
      transitive mc (symmetric md)
    }
    case false assume oo {
      have sep: (same_side(C, center(c), line(A, B)) or opposite_sides(C, center(c), line(A, B)))
                and not (same_side(C, center(c), line(A, B)) and opposite_sides(C, center(c), line(A, B)))
        by apply plane_separation_postulate[C, center(c), line(A, B)] to conjunct 0 of off, oo
      cases conjunct 0 of sep
      case sc {
        have sd: same_side(D, center(c), line(A, B)) by apply line_same_side_trans to dc, sc
        have mc: m_angle(A, C, B) = frac(+1, 2) * m_angle(A, center(c), B)
          by apply inscribed_angle_theorem[c, A, B, C] to conjunct 0 of h, conjunct 1 of h, ca, cb, sc
        have md: m_angle(A, D, B) = frac(+1, 2) * m_angle(A, center(c), B)
          by apply inscribed_angle_theorem[c, A, B, D] to conjunct 0 of h, conjunct 2 of h, da, db, sd
        transitive mc (symmetric md)
      }
      case oc {
        have od: opposite_sides(D, center(c), line(A, B)) by apply line_same_opposite_sides to dc, oc
        have mc: m_angle(A, C, B) = deg(+180) - frac(+1, 2) * m_angle(A, center(c), B)
          by apply inscribed_angle_theorem_opposite[c, A, B, C] to conjunct 0 of h, conjunct 1 of h, oc
        have md: m_angle(A, D, B) = deg(+180) - frac(+1, 2) * m_angle(A, center(c), B)
          by apply inscribed_angle_theorem_opposite[c, A, B, D] to conjunct 0 of h, conjunct 2 of h, od
        transitive mc (symmetric md)
      }
    }
  }
end

// The center is on a chord's line only as its midpoint, where the
// chord is a diameter and its central angle is straight.
lemma center_on_chord: all c:Circle, B:Point, D:Point.
  if chord(B, D, c) and on_line(center(c), line(B, D)) then m_angle(B, center(c), D) = deg(+180)
proof
  arbitrary c:Circle, B:Point, D:Point
  assume h
  have bd: not (B = D) by conjunct 0 of (expand chord in conjunct 0 of h)
  have m: center(c) = midpoint(B, D) by apply circle_chord_center to conjunct 0 of h, conjunct 1 of h
  replace m
  apply angle_measure_straight to apply between_midpoint[B, D] to bd
end

// The Inscribed Quadrilateral Theorem: the opposite angles of a
// quadrilateral inscribed in a circle are supplementary: for A and C on opposite sides of chord BD,
// m∠BAD + m∠BCD = 180°.
theorem inscribed_quadrilateral_theorem: all c:Circle, A:Point, B:Point, C:Point, D:Point.
  if chord(B, D, c) and on_circle(A, c) and on_circle(C, c) and opposite_sides(A, C, line(B, D))
  then supplementary(m_angle(B, A, D), m_angle(B, C, D))
proof
  arbitrary c:Circle, A:Point, B:Point, C:Point, D:Point
  assume h
  have off: not on_line(A, line(B, D)) and not on_line(C, line(B, D))
    by apply line_opposite_sides_off to conjunct 3 of h
  have ab: not (A = B) by { assume e  apply (replace e in conjunct 0 of off) to line_on_left[B, D] }
  have ad: not (A = D) by { assume e  apply (replace e in conjunct 0 of off) to line_on_right[B, D] }
  have cb: not (C = B) by { assume e  apply (replace e in conjunct 1 of off) to line_on_left[B, D] }
  have cd: not (C = D) by { assume e  apply (replace e in conjunct 1 of off) to line_on_right[B, D] }
  have ca: opposite_sides(C, A, line(B, D))
    by replace line_opposite_sides_symmetric[A, C, line(B, D)] in conjunct 3 of h
  expand supplementary
  switch on_line(center(c), line(B, D)) {
    case true assume on {
      have ma: m_angle(B, A, D) = frac(+1, 2) * m_angle(B, center(c), D)
        by apply inscribed_angle_theorem[c, B, D, A] to conjunct 0 of h, conjunct 1 of h, ab, ad, on
      have mc: m_angle(B, C, D) = frac(+1, 2) * m_angle(B, center(c), D)
        by apply inscribed_angle_theorem[c, B, D, C] to conjunct 0 of h, conjunct 2 of h, cb, cd, on
      have st: m_angle(B, center(c), D) = deg(+180) by apply center_on_chord to conjunct 0 of h, on
      algebra with ma | mc | st
    }
    case false assume oo {
      have sep: (same_side(A, center(c), line(B, D)) or opposite_sides(A, center(c), line(B, D)))
                and not (same_side(A, center(c), line(B, D)) and opposite_sides(A, center(c), line(B, D)))
        by apply plane_separation_postulate[A, center(c), line(B, D)] to conjunct 0 of off, oo
      cases conjunct 0 of sep
      case sa {
        have oc: opposite_sides(C, center(c), line(B, D)) by {
          have os: opposite_sides(center(c), C, line(B, D))
            by apply line_same_opposite_sides
               to (replace line_same_side_symmetric[A, center(c), line(B, D)] in sa), conjunct 3 of h
          replace line_opposite_sides_symmetric[C, center(c), line(B, D)]
          os
        }
        have ma: m_angle(B, A, D) = frac(+1, 2) * m_angle(B, center(c), D)
          by apply inscribed_angle_theorem[c, B, D, A] to conjunct 0 of h, conjunct 1 of h, ab, ad, sa
        have mc: m_angle(B, C, D) = deg(+180) - frac(+1, 2) * m_angle(B, center(c), D)
          by apply inscribed_angle_theorem_opposite[c, B, D, C] to conjunct 0 of h, conjunct 2 of h, oc
        algebra with ma | mc
      }
      case oa {
        have sc: same_side(C, center(c), line(B, D)) by apply line_opposite_sides_trans to ca, oa
        have ma: m_angle(B, A, D) = deg(+180) - frac(+1, 2) * m_angle(B, center(c), D)
          by apply inscribed_angle_theorem_opposite[c, B, D, A] to conjunct 0 of h, conjunct 1 of h, oa
        have mc: m_angle(B, C, D) = frac(+1, 2) * m_angle(B, center(c), D)
          by apply inscribed_angle_theorem[c, B, D, C] to conjunct 0 of h, conjunct 2 of h, cb, cd, sc
        algebra with ma | mc
      }
    }
  }
end