↑ Contents

Lean sources, part 2 of 50 (bsd/PtCert.lean – found/OrdArith.lean)

bsd/PtCert.lean

lean_all/bsd/PtCert.lean

/-
  bsd/PtCert.lean — T39 (goal G289e-6f): certificates for explicit rational points of E_42486 without evaluating normQ
  (lib/RatBase normalizes with a unary remainder, so large literals are never reduced): a point is given as a structure
  ⟨num, den, _, red⟩ with a Bezout certificate for red, and the curve equation and the class are integer identities.
    mkQ             ⟨n, d⟩ with u·|n| + v·d = 1
    onE_of_int      r²·q³ = s²·p(p² − n²q²)  ⇒  OnE (p/q) (r/s)
    hc_of_int       x = cls β₁·s², x − n = cls β₂·r² as integer identities  ⇒  HC x β₁ β₂
  Lean 4 core plus bsd/TransStep, bsd/IntModel.
-/
import bsd.TransStep

namespace PtCert
open DetChain LibRing LibRat LibInt QModel QCls B5S TransStep

def mkQ (n : Int) (d : Nat) (hd : 0 < d) (u v : Int)
    (h : u * Int.ofNat (absN n) + v * Int.ofNat d = 1) : Q := ⟨n, d, hd, IntModel.cop_of_bez h⟩

/-- the reverse of QModel.int_id, with a symbolic N -/
theorem int_id_rev (N p q r s P Q' : Int)
    (h : r * r * (q * q * q) = s * s * (p * (p * p - N * N * (q * q))))
    (hQ : Q' = q * 1 * (q * 1) * (q * 1)) (hP : P = s * s) :
    r * r * Q' = (p * 1 + -0 * q) * (p * 1 + -N * q) * (p * 1 + - -N * q) * P := by
  rw [hQ, hP]
  exact of_lin h (c := 1) (by lib_int)

theorem onE_of_int (x y : Q)
    (h : y.num * y.num * (Int.ofNat x.den * Int.ofNat x.den * Int.ofNat x.den)
      = Int.ofNat y.den * Int.ofNat y.den
        * (x.num * (x.num * x.num - 42486 * 42486 * (Int.ofNat x.den * Int.ofNat x.den)))) : OnE x y := by
  show ratR.mul y y = ratR.mul (ratR.mul (LibRing.sub ratR x zeroQ) (LibRing.sub ratR x nQ)) (LibRing.sub ratR x (negQ nQ))
  have hr : ratR.mul (ratR.mul (LibRing.sub ratR x zeroQ) (LibRing.sub ratR x nQ)) (LibRing.sub ratR x (negQ nQ))
      = normQ ((x.num * Int.ofNat 1 + -0 * Int.ofNat x.den) * (x.num * Int.ofNat 1 + -42486 * Int.ofNat x.den)
          * (x.num * Int.ofNat 1 + - -42486 * Int.ofNat x.den))
          (x.den * 1 * (x.den * 1) * (x.den * 1))
          (mul_pos' (mul_pos' (mul_pos' x.den_pos one_pos') (mul_pos' x.den_pos one_pos')) (mul_pos' x.den_pos one_pos')) :=
    (congr (congrArg ratR.mul (congr (congrArg ratR.mul (e_zero x)) (e_n x))) (e_mn x)).trans
      ((congrArg (fun t => ratR.mul t _) (rmul _ _ _ _ _ _)).trans (rmul _ _ _ _ _ _))
  rw [e_yy y, hr]
  apply (normQ_eq_iff _ _ _ _ _ _).2
  exact int_id_rev 42486 x.num (Int.ofNat x.den) y.num (Int.ofNat y.den) _ _ h (by lib_int) (by lib_int)

/-- symbolic n (a literal 42486 inside lib_int overflows the kernel recursion depth) -/
theorem sub_den (n X D : Int) (k : Nat) :
    (X * Int.ofNat 1 + -n * D) * Int.ofNat (1 * (k * k)) = (X - n * D) * (Int.ofNat k * Int.ofNat k) := by lib_int

theorem hc_of_int (x s r : Q) (β1 β2 : B5) (hs : s.num ≠ 0) (hr : r.num ≠ 0)
    (h1 : x.num * (Int.ofNat s.den * Int.ofNat s.den) = β1.cls * (s.num * s.num) * Int.ofNat x.den)
    (h2 : (x.num - 42486 * Int.ofNat x.den) * (Int.ofNat r.den * Int.ofNat r.den)
      = β2.cls * (r.num * r.num) * Int.ofNat x.den) : HC x β1 β2 := by
  unfold HC HasCls
  refine ⟨s, r, ?_, ?_, ?_, ?_⟩
  · intro h0; exact hs (num_eq_zero_of s h0)
  · intro h0; exact hr (num_eq_zero_of r h0)
  · calc x = normQ x.num x.den x.den_pos := self_norm x
      _ = normQ (β1.cls * (s.num * s.num)) (1 * (s.den * s.den)) (mul_pos' (Nat.succ_pos 0) (mul_pos' s.den_pos s.den_pos)) :=
          (normQ_eq_iff _ _ _ _ _ _).2 (by rw [← h1]; lib_int)
      _ = ratR.mul (cq β1) (normQ (s.num * s.num) (s.den * s.den) (mul_pos' s.den_pos s.den_pos)) :=
          (rmul β1.cls (s.num * s.num) 1 (s.den * s.den) (Nat.succ_pos 0) (mul_pos' s.den_pos s.den_pos)).symm
      _ = ratR.mul (cq β1) (ratR.mul s s) := congrArg (ratR.mul (cq β1)) (e_yy s).symm
  · calc LibRing.sub ratR x nQ
        = normQ (x.num * Int.ofNat 1 + -42486 * Int.ofNat x.den) (x.den * 1) (mul_pos' x.den_pos one_pos') := e_n x
      _ = normQ (β2.cls * (r.num * r.num)) (1 * (r.den * r.den)) (mul_pos' (Nat.succ_pos 0) (mul_pos' r.den_pos r.den_pos)) := by
          apply (normQ_eq_iff _ _ _ _ _ _).2
          have e : (x.num * Int.ofNat 1 + -42486 * Int.ofNat x.den) * Int.ofNat (1 * (r.den * r.den))
              = (x.num - 42486 * Int.ofNat x.den) * (Int.ofNat r.den * Int.ofNat r.den) :=
            sub_den 42486 x.num (Int.ofNat x.den) r.den
          rw [e, h2]; lib_int
      _ = ratR.mul (cq β2) (normQ (r.num * r.num) (r.den * r.den) (mul_pos' r.den_pos r.den_pos)) :=
          (rmul β2.cls (r.num * r.num) 1 (r.den * r.den) (Nat.succ_pos 0) (mul_pos' r.den_pos r.den_pos)).symm
      _ = ratR.mul (cq β2) (ratR.mul r r) := congrArg (ratR.mul (cq β2)) (e_yy r).symm

end PtCert

#print axioms PtCert.int_id_rev
#print axioms PtCert.onE_of_int
#print axioms PtCert.sub_den
#print axioms PtCert.hc_of_int

bsd/PtCertZ.lean

lean_all/bsd/PtCertZ.lean

/-
  bsd/PtCertZ.lean — T39 (goal G289f-4h-5e, tool): certificates for explicit rational points of E_N, N an integer
  (bsd/PtCert and bsd/TransPts are the case N = 42486), as integer identities checked by rfl at each instance.
    onE_of_intZ    r²·q³ = s²·p(p² − N²q²)  ⇒  OnEn (nz N) (p/q) (r/s)
    nd_ofZ         p, p − Nq, p + Nq nonzero (pattern-matched zero test)  ⇒  NDn (nz N) (p/q)
    hc_of_intZ     x = cls₁·s², x − N = cls₂·r² as integer identities  ⇒  HCn (nz N) ps x
  Lean 4 core plus bsd/PtCert, bsd/TransPts, bsd/QModelZ, bsd/TransStepL.
-/
import bsd.PtCert
import bsd.TransPts
import bsd.QModelZ
import bsd.TransStepL

namespace PtCertZ
open DetChain LibRing LibRat LibInt QModel QCls B5S TransStep PtCert QModelN QModelZ SqUniqueL TransStepL

theorem onE_of_intZ (N : Int) (x y : Q)
    (h : y.num * y.num * (Int.ofNat x.den * Int.ofNat x.den * Int.ofNat x.den)
      = Int.ofNat y.den * Int.ofNat y.den
        * (x.num * (x.num * x.num - N * N * (Int.ofNat x.den * Int.ofNat x.den)))) : OnEn (nz N) x y := by
  show ratR.mul y y = ratR.mul (ratR.mul (LibRing.sub ratR x zeroQ) (LibRing.sub ratR x (nz N))) (LibRing.sub ratR x (negQ (nz N)))
  have hr : ratR.mul (ratR.mul (LibRing.sub ratR x zeroQ) (LibRing.sub ratR x (nz N))) (LibRing.sub ratR x (negQ (nz N)))
      = normQ ((x.num * Int.ofNat 1 + -0 * Int.ofNat x.den) * (x.num * Int.ofNat 1 + -N * Int.ofNat x.den)
          * (x.num * Int.ofNat 1 + - -N * Int.ofNat x.den))
          (x.den * 1 * (x.den * 1) * (x.den * 1))
          (mul_pos' (mul_pos' (mul_pos' x.den_pos one_pos') (mul_pos' x.den_pos one_pos')) (mul_pos' x.den_pos one_pos')) :=
    (congr (congrArg ratR.mul (congr (congrArg ratR.mul (e_zero x)) (e_nz x N))) (e_mnz x N)).trans
      ((congrArg (fun t => ratR.mul t _) (rmul _ _ _ _ _ _)).trans (rmul _ _ _ _ _ _))
  rw [e_yy y, hr]
  apply (normQ_eq_iff _ _ _ _ _ _).2
  exact int_id_rev N x.num (Int.ofNat x.den) y.num (Int.ofNat y.den) _ _ h (by lib_int) (by lib_int)

theorem nd_ofZ (N : Int) (x : Q) (h0 : TransPts.isZ x.num = false)
    (h1 : TransPts.isZ (x.num * Int.ofNat 1 + -N * Int.ofNat x.den) = false)
    (h2 : TransPts.isZ (x.num * Int.ofNat 1 + - -N * Int.ofNat x.den) = false) : NDn (nz N) x := by
  refine ⟨TransStep2.ne_of_num (TransPts.nz _ h0), ?_, ?_⟩
  · rw [e_nz x N]; exact normQ_ne_zero _ (TransPts.nz _ h1)
  · rw [e_mnz x N]; exact normQ_ne_zero _ (TransPts.nz _ h2)

theorem hc_of_intZ (N : Int) (ps : List Nat) (x s r : Q) (s1 : Bool) (b1 : List Bool) (s2 : Bool) (b2 : List Bool) (hs : s.num ≠ 0) (hr : r.num ≠ 0)
    (h1 : x.num * (Int.ofNat s.den * Int.ofNat s.den) = clsL s1 ps b1 * (s.num * s.num) * Int.ofNat x.den)
    (h2 : (x.num - N * Int.ofNat x.den) * (Int.ofNat r.den * Int.ofNat r.den)
      = clsL s2 ps b2 * (r.num * r.num) * Int.ofNat x.den) : HCn (nz N) ps x s1 b1 s2 b2 := by
  unfold HCn
  refine ⟨s, r, ?_, ?_, ?_, ?_⟩
  · intro h0; exact hs (num_eq_zero_of s h0)
  · intro h0; exact hr (num_eq_zero_of r h0)
  · calc x = normQ x.num x.den x.den_pos := self_norm x
      _ = normQ (clsL s1 ps b1 * (s.num * s.num)) (1 * (s.den * s.den)) (mul_pos' (Nat.succ_pos 0) (mul_pos' s.den_pos s.den_pos)) :=
          (normQ_eq_iff _ _ _ _ _ _).2 (by rw [← h1]; lib_int)
      _ = ratR.mul (cqL ps s1 b1) (normQ (s.num * s.num) (s.den * s.den) (mul_pos' s.den_pos s.den_pos)) :=
          (rmul (clsL s1 ps b1) (s.num * s.num) 1 (s.den * s.den) (Nat.succ_pos 0) (mul_pos' s.den_pos s.den_pos)).symm
      _ = ratR.mul (cqL ps s1 b1) (ratR.mul s s) := congrArg (ratR.mul (cqL ps s1 b1)) (e_yy s).symm
  · calc LibRing.sub ratR x (nz N)
        = normQ (x.num * Int.ofNat 1 + -N * Int.ofNat x.den) (x.den * 1) (mul_pos' x.den_pos one_pos') := e_nz x N
      _ = normQ (clsL s2 ps b2 * (r.num * r.num)) (1 * (r.den * r.den)) (mul_pos' (Nat.succ_pos 0) (mul_pos' r.den_pos r.den_pos)) := by
          apply (normQ_eq_iff _ _ _ _ _ _).2
          have e : (x.num * Int.ofNat 1 + -N * Int.ofNat x.den) * Int.ofNat (1 * (r.den * r.den))
              = (x.num - N * Int.ofNat x.den) * (Int.ofNat r.den * Int.ofNat r.den) :=
            sub_den N x.num (Int.ofNat x.den) r.den
          rw [e, h2]; lib_int
      _ = ratR.mul (cqL ps s2 b2) (normQ (r.num * r.num) (r.den * r.den) (mul_pos' r.den_pos r.den_pos)) :=
          (rmul (clsL s2 ps b2) (r.num * r.num) 1 (r.den * r.den) (Nat.succ_pos 0) (mul_pos' r.den_pos r.den_pos)).symm
      _ = ratR.mul (cqL ps s2 b2) (ratR.mul r r) := congrArg (ratR.mul (cqL ps s2 b2)) (e_yy r).symm

end PtCertZ

#print axioms PtCertZ.onE_of_intZ
#print axioms PtCertZ.nd_ofZ
#print axioms PtCertZ.hc_of_intZ

bsd/PtCls.lean

lean_all/bsd/PtCls.lean

/-
  bsd/PtCls.lean — T39 (goal G289e-7b3): 2-descent classes of the points of E_42486 (bsd/PtAdd) and their additivity
  in the cases without a chord through a point of E[2]:
    ClsPt        O has class (0, 0); an affine point has class (β₁, β₂) if it is away from E[2] with that class
                 (HC) or it is a point of E[2] with that class (StepFull.TorCls)
    rescale, cls_eq, same_x    the class of an abscissa is unique
    cls_add_O, cls_add_O'      O + P, P + O
    cls_chord    x₁ ≠ x₂, both points away from E[2]       (StepFull.step_full)
    cls_vert     x₁ = x₂, y₁ = −y₂: the sum O has class β ⊕ β = 0
    cls_dbl      x₁ = x₂, y₁ ≠ −y₂: 2P has class 0 = β ⊕ β   (DblCls)
  Lean 4 core plus bsd/PtAdd, bsd/StepFull, bsd/DblCls.
-/
import bsd.PtAdd
import bsd.StepFull
import bsd.DblCls

namespace PtCls
open DetChain LibRing LibRat QModel QCls B5S TransStep TransStep2 StepFull PtAdd QDec DblCls

section gen
variable {R : Type} (K : CRing R)

theorem rescale (a c s si t : R) (hsi : K.mul s si = K.one) (h : K.mul a (K.mul s s) = K.mul c (K.mul t t)) :
    K.mul a K.one = K.mul c (K.mul (K.mul t si) (K.mul t si)) := by
  calc K.mul a K.one = K.mul a (K.mul (K.mul s si) (K.mul s si)) := by rw [hsi]; lib_ring
    _ = K.mul (K.mul a (K.mul s s)) (K.mul si si) := by lib_ring
    _ = K.mul (K.mul c (K.mul t t)) (K.mul si si) := by rw [h]
    _ = _ := by lib_ring

theorem neg_ne (a : R) (ha : a ≠ K.zero) : K.neg a ≠ K.zero := fun h => ha (by
  calc a = K.neg (K.neg a) := by lib_ring
    _ = K.neg K.zero := by rw [h]
    _ = K.zero := by lib_ring)

theorem ne_neg (hK : NoZD K) (h2 : TrianglePoint.two K K.one ≠ K.zero) (a : R) (ha : a ≠ K.zero) : a ≠ K.neg a := fun h => by
  have e : K.mul (TrianglePoint.two K K.one) a = K.zero := by
    calc K.mul (TrianglePoint.two K K.one) a = K.add a a := by unfold TrianglePoint.two; lib_ring
      _ = K.add a (K.neg a) := by rw [← h]
      _ = K.zero := by lib_ring
  cases hK _ _ e with
  | inl h' => exact h2 h'
  | inr h' => exact ha h'

theorem sub_self_zero (a b : R) (h : a = b) : sub K a b = K.zero := by
  rw [h]; unfold LibRing.sub; lib_ring

end gen

/-- the class of a point (bits of x and of x − n) -/
def ClsPt : Pt → B5 → B5 → Prop
  | Pt.O, β1, β2 => β1 = z5 ∧ β2 = z5
  | Pt.A x _, β1, β2 => (ND x ∧ HC x β1 β2) ∨ TorCls x β1 β2

theorem xor_self (β : B5) : β.xor β = z5 := by
  cases β with
  | mk s a b c d => cases s <;> cases a <;> cases b <;> cases c <;> cases d <;> rfl

theorem z5_xor (β : B5) : z5.xor β = β := by
  cases β with
  | mk s a b c d => cases s <;> cases a <;> cases b <;> cases c <;> cases d <;> rfl

theorem xor_z5 (β : B5) : β.xor z5 = β := by
  cases β with
  | mk s a b c d => cases s <;> cases a <;> cases b <;> cases c <;> cases d <;> rfl

/-- the class of an abscissa is unique -/
theorem cls_eq (x : Q) (β γ : B5) (s t : Q) (hs : s ≠ zeroQ) (e1 : x = ratR.mul (cq β) (ratR.mul s s))
    (e2 : x = ratR.mul (cq γ) (ratR.mul t t)) : β = γ :=
  (bits_of_eq β γ 1 one_ne _
    ((castQ_mul _ _).trans (rescale ratR (cq β) (cq γ) s (invQ s) t (mul_invQ s hs) (e1.symm.trans e2)))).symm

theorem hc_unique (x : Q) (β1 β2 γ1 γ2 : B5) (h1 : HC x β1 β2) (h2 : HC x γ1 γ2) : β1 = γ1 ∧ β2 = γ2 := by
  obtain ⟨s, r, hs, hr, ex, exn⟩ := h1
  obtain ⟨s', r', _, _, ex', exn'⟩ := h2
  exact ⟨cls_eq x β1 γ1 s s' hs ex ex', cls_eq _ β2 γ2 r r' hr exn exn'⟩

theorem n_ne : nQ ≠ zeroQ := Kernel2.nQ_ne
theorem m_ne : negQ nQ ≠ zeroQ := neg_ne ratR nQ n_ne
theorem n_ne_m : nQ ≠ negQ nQ := ne_neg ratR rat_noZD Kernel2.two_one_ne nQ n_ne

theorem tor_unique (x : Q) (β1 β2 γ1 γ2 : B5) (h1 : TorCls x β1 β2) (h2 : TorCls x γ1 γ2) :
    β1 = γ1 ∧ β2 = γ2 := by
  rcases h1 with ⟨e, b1, b2⟩ | ⟨e, b1, b2⟩ | ⟨e, b1, b2⟩ <;>
  rcases h2 with ⟨e', c1, c2⟩ | ⟨e', c1, c2⟩ | ⟨e', c1, c2⟩
  · exact ⟨b1.trans c1.symm, b2.trans c2.symm⟩
  · exact absurd (e'.symm.trans e).symm (fun h => n_ne h.symm)
  · exact absurd (e'.symm.trans e).symm (fun h => m_ne h.symm)
  · exact absurd (e.symm.trans e') n_ne
  · exact ⟨b1.trans c1.symm, b2.trans c2.symm⟩
  · exact absurd (e.symm.trans e') n_ne_m
  · exact absurd (e.symm.trans e') m_ne
  · exact absurd (e'.symm.trans e) n_ne_m
  · exact ⟨b1.trans c1.symm, b2.trans c2.symm⟩

theorem nd_tor (x : Q) (β1 β2 : B5) (nd : ND x) (h : TorCls x β1 β2) : False := by
  rcases h with ⟨e, _, _⟩ | ⟨e, _, _⟩ | ⟨e, _, _⟩
  · exact nd.1 e
  · exact nd.2.1 (sub_self_zero ratR x nQ e)
  · exact nd.2.2 (sub_self_zero ratR x (negQ nQ) e)

/-- two classes of the same abscissa agree -/
theorem same_x (x : Q) (β1 β2 γ1 γ2 : B5) (c1 : (ND x ∧ HC x β1 β2) ∨ TorCls x β1 β2)
    (c2 : (ND x ∧ HC x γ1 γ2) ∨ TorCls x γ1 γ2) : β1 = γ1 ∧ β2 = γ2 := by
  rcases c1 with ⟨nd, h1⟩ | t1 <;> rcases c2 with ⟨nd', h2⟩ | t2
  · exact hc_unique x β1 β2 γ1 γ2 h1 h2
  · exact (nd_tor x γ1 γ2 nd t2).elim
  · exact (nd_tor x β1 β2 nd' t1).elim
  · exact tor_unique x β1 β2 γ1 γ2 t1 t2

theorem cls_add_O (P : Pt) (β1 β2 γ1 γ2 : B5) (c1 : ClsPt Pt.O β1 β2) (c2 : ClsPt P γ1 γ2) :
    ClsPt (add Pt.O P) (β1.xor γ1) (β2.xor γ2) := by
  obtain ⟨e1, e2⟩ := c1
  rw [e1, e2, z5_xor, z5_xor]
  exact c2

theorem cls_add_O' (x y : Q) (β1 β2 γ1 γ2 : B5) (c1 : ClsPt (Pt.A x y) β1 β2) (c2 : ClsPt Pt.O γ1 γ2) :
    ClsPt (add (Pt.A x y) Pt.O) (β1.xor γ1) (β2.xor γ2) := by
  obtain ⟨e1, e2⟩ := c2
  rw [e1, e2, xor_z5, xor_z5]
  exact c1

theorem qbeq_ne (a b : Q) (h : a ≠ b) : qbeq a b = false := by
  cases e : qbeq a b with
  | false => rfl
  | true => exact absurd (qbeq_true a b e) h

/-- **Chord between two points away from E[2].** -/
theorem cls_chord (x1 y1 x2 y2 : Q) (o1 : OnE x1 y1) (o2 : OnE x2 y2) (h12 : x1 ≠ x2) (β1 β2 γ1 γ2 : B5)
    (nd1 : ND x1) (nd2 : ND x2) (hc1 : HC x1 β1 β2) (hc2 : HC x2 γ1 γ2) :
    ClsPt (addA x1 y1 x2 y2) (β1.xor γ1) (β2.xor γ2) := by
  unfold addA
  cases e : qbeq x1 x2 with
  | true => exact absurd (qbeq_true x1 x2 e) h12
  | false =>
    cases step_full x1 y1 x2 y2 o1 o2 h12 β1 β2 γ1 γ2 hc1 hc2 nd1 nd2 with
    | inl h => exact Or.inr h
    | inr h => exact Or.inl h

/-- **Vertical pair: the sum O.** -/
theorem cls_vert (x y1 y2 : Q) (hy : y1 = ratR.neg y2) (β1 β2 γ1 γ2 : B5)
    (c1 : ClsPt (Pt.A x y1) β1 β2) (c2 : ClsPt (Pt.A x y2) γ1 γ2) :
    ClsPt (addA x y1 x y2) (β1.xor γ1) (β2.xor γ2) := by
  obtain ⟨e1, e2⟩ := same_x x β1 β2 γ1 γ2 c1 c2
  unfold addA
  rw [qbeq_self x]
  cases e : qbeq y1 (ratR.neg y2) with
  | false => exact absurd hy (qbeq_false _ _ e)
  | true =>
    show β1.xor γ1 = z5 ∧ β2.xor γ2 = z5
    rw [← e1, ← e2]
    exact ⟨xor_self β1, xor_self β2⟩

/-- **Tangent: 2P has class 0.** -/
theorem cls_dbl (x y1 y2 : Q) (o1 : OnE x y1) (o2 : OnE x y2) (hne : y1 ≠ ratR.neg y2) (β1 β2 γ1 γ2 : B5)
    (c1 : ClsPt (Pt.A x y1) β1 β2) (c2 : ClsPt (Pt.A x y2) γ1 γ2) :
    ClsPt (addA x y1 x y2) (β1.xor γ1) (β2.xor γ2) := by
  obtain ⟨e1, e2⟩ := same_x x β1 β2 γ1 γ2 c1 c2
  have d := dbl_nd_cls x y1 _ o1 (dbl_w x y1 y2 o1 o2 hne)
  unfold addA
  rw [qbeq_self x, qbeq_ne y1 (ratR.neg y2) hne]
  show (ND (dblX x y1) ∧ HC (dblX x y1) (β1.xor γ1) (β2.xor γ2)) ∨ TorCls (dblX x y1) (β1.xor γ1) (β2.xor γ2)
  rw [← e1, ← e2, xor_self β1, xor_self β2]
  exact Or.inl d

end PtCls

#print axioms PtCls.rescale
#print axioms PtCls.neg_ne
#print axioms PtCls.ne_neg
#print axioms PtCls.sub_self_zero
#print axioms PtCls.xor_self
#print axioms PtCls.z5_xor
#print axioms PtCls.xor_z5
#print axioms PtCls.cls_eq
#print axioms PtCls.hc_unique
#print axioms PtCls.n_ne
#print axioms PtCls.m_ne
#print axioms PtCls.n_ne_m
#print axioms PtCls.tor_unique
#print axioms PtCls.nd_tor
#print axioms PtCls.same_x
#print axioms PtCls.cls_add_O
#print axioms PtCls.cls_add_O'
#print axioms PtCls.qbeq_ne
#print axioms PtCls.cls_chord
#print axioms PtCls.cls_vert
#print axioms PtCls.cls_dbl

bsd/PtCls2.lean

lean_all/bsd/PtCls2.lean

/-
  bsd/PtCls2.lean — T39 (goal G289e-7b3): the 2-descent class is additive on all of E_42486(ℚ):
      ClsPt P β, ClsPt Q γ  ⇒  ClsPt (P + Q) (β ⊕ γ)
  for the chord–tangent sum of bsd/PtAdd.  The cases not in bsd/PtCls are chords through points of E[2]:
  one point of E[2] (bsd/ChordTor, bsd/TorCls0, bsd/TorCls12, with bsd/ChordSym.x3_symm for the order) and two
  points of E[2] (the third one, bsd/ChordSym.third_tor).
    xor_comm        β ⊕ γ = γ ⊕ β
    x_tor_nd        P₁ ∈ E[2], P₂ away from E[2], x₁ ≠ x₂: the chord abscissa is away from E[2] with class β ⊕ γ
    cls_tor_tor     P₁, P₂ ∈ E[2], x₁ ≠ x₂
    cls_add         the additivity
  Lean 4 core plus bsd/PtCls, bsd/ChordSym, bsd/ChordTor, bsd/TorCls12.
-/
import bsd.PtCls
import bsd.ChordSym
import bsd.ChordTor
import bsd.TorCls12

namespace PtCls2
open DetChain LibRing LibRat QModel QCls B5S TransStep TransStep2 StepFull PtAdd QDec DblCls PtCls

theorem bxor_comm : ∀ x y : Bool, Bool.xor x y = Bool.xor y x := by
  intro x y; cases x <;> cases y <;> rfl

theorem xor_comm (a b : B5) : a.xor b = b.xor a := by
  cases a with
  | mk s1 a1 b1 c1 d1 =>
    cases b with
    | mk s2 a2 b2 c2 d2 =>
      show B5.mk (Bool.xor s1 s2) (Bool.xor a1 a2) (Bool.xor b1 b2) (Bool.xor c1 c2) (Bool.xor d1 d2)
        = B5.mk (Bool.xor s2 s1) (Bool.xor a2 a1) (Bool.xor b2 b1) (Bool.xor c2 c1) (Bool.xor d2 d1)
      rw [bxor_comm s1 s2, bxor_comm a1 a2, bxor_comm b1 b2, bxor_comm c1 c2, bxor_comm d1 d2]

/-- **One point of E[2] on the chord.** -/
theorem x_tor_nd (x1 y1 x2 y2 : Q) (o1 : OnE x1 y1) (o2 : OnE x2 y2) (h12 : x1 ≠ x2) (β1 β2 γ1 γ2 : B5)
    (t1 : TorCls x1 β1 β2) (nd2 : ND x2) (hc2 : HC x2 γ1 γ2) :
    ND (TransQ.x3 x1 y1 x2 y2) ∧ HC (TransQ.x3 x1 y1 x2 y2) (β1.xor γ1) (β2.xor γ2) := by
  have hy1 := ChordSym.tor_y x1 y1 o1 β1 β2 t1
  subst hy1
  rcases t1 with ⟨e, b1, b2⟩ | ⟨e, b1, b2⟩ | ⟨e, b1, b2⟩
  · subst e; subst b1; subst b2
    have h0 : sub ratR x2 zeroQ ≠ zeroQ := fun h => nd2.1 ((sub0 x2).symm.trans h)
    rw [ChordTor.x3_T0 x2 y2 o2 h0]
    exact TorCls0.cls_T0 x2 y2 o2 nd2 γ1 γ2 hc2
  · subst e; subst b1; subst b2
    rw [ChordTor.x3_T1 x2 y2 o2 nd2.2.1]
    exact TorCls12.cls_T1 x2 y2 o2 nd2 γ1 γ2 hc2
  · subst e; subst b1; subst b2
    rw [ChordTor.x3_T2 x2 y2 o2 nd2.2.2]
    exact TorCls12.cls_T2 x2 y2 o2 nd2 γ1 γ2 hc2

section gen
variable {R : Type} (K : CRing R)
theorem g_ba (a b c : R) : K.add (K.add a b) c = K.add (K.add b a) c := by lib_ring
theorem g_acb (a b c : R) : K.add (K.add a c) b = K.add (K.add a b) c := by lib_ring
theorem g_cab (a b c : R) : K.add (K.add c a) b = K.add (K.add a b) c := by lib_ring
theorem g_bca (a b c : R) : K.add (K.add b c) a = K.add (K.add a b) c := by lib_ring
theorem g_cba (a b c : R) : K.add (K.add c b) a = K.add (K.add a b) c := by lib_ring
end gen

theorem n0 : ratR.add (ratR.add nQ zeroQ) (negQ nQ) = ratR.add (ratR.add zeroQ nQ) (negQ nQ) :=
  g_ba ratR nQ zeroQ (negQ nQ)
theorem m0 : ratR.add (ratR.add zeroQ (negQ nQ)) nQ = ratR.add (ratR.add zeroQ nQ) (negQ nQ) :=
  g_acb ratR zeroQ nQ (negQ nQ)
theorem m0' : ratR.add (ratR.add (negQ nQ) zeroQ) nQ = ratR.add (ratR.add zeroQ nQ) (negQ nQ) :=
  g_cab ratR zeroQ nQ (negQ nQ)
theorem nm : ratR.add (ratR.add nQ (negQ nQ)) zeroQ = ratR.add (ratR.add zeroQ nQ) (negQ nQ) :=
  g_bca ratR zeroQ nQ (negQ nQ)
theorem mn : ratR.add (ratR.add (negQ nQ) nQ) zeroQ = ratR.add (ratR.add zeroQ nQ) (negQ nQ) :=
  g_cba ratR zeroQ nQ (negQ nQ)

/-- **Two points of E[2] on the chord.** -/
theorem cls_tor_tor (x1 y1 x2 y2 : Q) (o1 : OnE x1 y1) (o2 : OnE x2 y2) (h12 : x1 ≠ x2) (β1 β2 γ1 γ2 : B5)
    (t1 : TorCls x1 β1 β2) (t2 : TorCls x2 γ1 γ2) :
    TorCls (TransQ.x3 x1 y1 x2 y2) (β1.xor γ1) (β2.xor γ2) := by
  have hy1 := ChordSym.tor_y x1 y1 o1 β1 β2 t1
  have hy2 := ChordSym.tor_y x2 y2 o2 γ1 γ2 t2
  subst hy1; subst hy2
  unfold TransQ.x3 TransQ.slope
  rcases t1 with ⟨e, b1, b2⟩ | ⟨e, b1, b2⟩ | ⟨e, b1, b2⟩ <;>
  rcases t2 with ⟨e', c1, c2⟩ | ⟨e', c1, c2⟩ | ⟨e', c1, c2⟩ <;>
  subst e <;> subst e' <;> subst b1 <;> subst b2 <;> subst c1 <;> subst c2
  · exact absurd rfl h12
  · exact Or.inr (Or.inr ⟨ChordSym.third_tor ratR _ _ _ _ _ _ _ _ rfl, rfl, rfl⟩)
  · exact Or.inr (Or.inl ⟨ChordSym.third_tor ratR _ _ _ _ _ _ _ _ m0, rfl, rfl⟩)
  · exact Or.inr (Or.inr ⟨ChordSym.third_tor ratR _ _ _ _ _ _ _ _ n0, rfl, rfl⟩)
  · exact absurd rfl h12
  · exact Or.inl ⟨ChordSym.third_tor ratR _ _ _ _ _ _ _ _ nm, rfl, rfl⟩
  · exact Or.inr (Or.inl ⟨ChordSym.third_tor ratR _ _ _ _ _ _ _ _ m0', rfl, rfl⟩)
  · exact Or.inl ⟨ChordSym.third_tor ratR _ _ _ _ _ _ _ _ mn, rfl, rfl⟩
  · exact absurd rfl h12

/-- the chord case x₁ ≠ x₂ -/
theorem cls_chord_all (x1 y1 x2 y2 : Q) (o1 : OnE x1 y1) (o2 : OnE x2 y2) (h12 : x1 ≠ x2) (β1 β2 γ1 γ2 : B5)
    (c1 : ClsPt (Pt.A x1 y1) β1 β2) (c2 : ClsPt (Pt.A x2 y2) γ1 γ2) :
    ClsPt (addA x1 y1 x2 y2) (β1.xor γ1) (β2.xor γ2) := by
  have hq : qbeq x1 x2 = false := qbeq_ne x1 x2 h12
  rcases c1 with ⟨nd1, hc1⟩ | t1 <;> rcases c2 with ⟨nd2, hc2⟩ | t2
  · exact cls_chord x1 y1 x2 y2 o1 o2 h12 β1 β2 γ1 γ2 nd1 nd2 hc1 hc2
  · unfold addA; rw [hq]
    show (ND (TransQ.x3 x1 y1 x2 y2) ∧ HC (TransQ.x3 x1 y1 x2 y2) (β1.xor γ1) (β2.xor γ2)) ∨
      TorCls (TransQ.x3 x1 y1 x2 y2) (β1.xor γ1) (β2.xor γ2)
    rw [ChordSym.x3_symm x1 y1 x2 y2 h12, xor_comm β1 γ1, xor_comm β2 γ2]
    exact Or.inl (x_tor_nd x2 y2 x1 y1 o2 o1 (fun e => h12 e.symm) γ1 γ2 β1 β2 t2 nd1 hc1)
  · unfold addA; rw [hq]
    exact Or.inl (x_tor_nd x1 y1 x2 y2 o1 o2 h12 β1 β2 γ1 γ2 t1 nd2 hc2)
  · unfold addA; rw [hq]
    exact Or.inr (cls_tor_tor x1 y1 x2 y2 o1 o2 h12 β1 β2 γ1 γ2 t1 t2)

/-- **The 2-descent class is additive.** -/
theorem cls_add : ∀ (P1 P2 : Pt) (β1 β2 γ1 γ2 : B5), OnPt P1 → OnPt P2 → ClsPt P1 β1 β2 → ClsPt P2 γ1 γ2 →
    ClsPt (add P1 P2) (β1.xor γ1) (β2.xor γ2)
  | Pt.O, P, β1, β2, γ1, γ2, _, _, c1, c2 => cls_add_O P β1 β2 γ1 γ2 c1 c2
  | Pt.A x y, Pt.O, β1, β2, γ1, γ2, _, _, c1, c2 => cls_add_O' x y β1 β2 γ1 γ2 c1 c2
  | Pt.A x1 y1, Pt.A x2 y2, β1, β2, γ1, γ2, o1, o2, c1, c2 => by
    show ClsPt (addA x1 y1 x2 y2) (β1.xor γ1) (β2.xor γ2)
    cases e : qbeq x1 x2 with
    | false => exact cls_chord_all x1 y1 x2 y2 o1 o2 (qbeq_false _ _ e) β1 β2 γ1 γ2 c1 c2
    | true =>
      have hx := qbeq_true _ _ e
      subst hx
      cases e2 : qbeq y1 (ratR.neg y2) with
      | true => exact cls_vert x1 y1 y2 (qbeq_true _ _ e2) β1 β2 γ1 γ2 c1 c2
      | false => exact cls_dbl x1 y1 y2 o1 o2 (qbeq_false _ _ e2) β1 β2 γ1 γ2 c1 c2

end PtCls2

#print axioms PtCls2.bxor_comm
#print axioms PtCls2.xor_comm
#print axioms PtCls2.x_tor_nd
#print axioms PtCls2.g_ba
#print axioms PtCls2.g_acb
#print axioms PtCls2.g_cab
#print axioms PtCls2.g_bca
#print axioms PtCls2.g_cba
#print axioms PtCls2.n0
#print axioms PtCls2.m0
#print axioms PtCls2.m0'
#print axioms PtCls2.nm
#print axioms PtCls2.mn
#print axioms PtCls2.cls_tor_tor
#print axioms PtCls2.cls_chord_all
#print axioms PtCls2.cls_add

bsd/PtClsN.lean

lean_all/bsd/PtClsN.lean

/-
  bsd/PtClsN.lean — T39 (goal G289f-4e): the 2-descent class of a point of E_n (n a rational parameter, classes over a
  certified prime list ps) and its additivity in the cases without a chord through E[2]: O, vertical pairs, tangents,
  and chords between points away from E[2] (bsd/PtCls fixes n = 42486).
    CL, cxor, zc, WF      classes (sign, bit list), xor, the trivial class, length condition
    TorClsN               the classes of the points of E[2], from the data StepN.TorData
    ClsPtN                the class of a point
    cls_eqN, hc_uniqueN, tor_uniqueN, same_xN     uniqueness
    cls_add_ON, cls_add_ON', cls_chordN, cls_vertN, cls_dblN   additivity in these cases
  Lean 4 core plus bsd/StepN, bsd/DblClsN, bsd/PtCls.
-/
import bsd.StepN
import bsd.DblClsN
import bsd.PtCls

namespace PtClsN
open DetChain LibRing LibRat QModel QCls B5S SqUniqueL SqClassL QModelN TransStepL PtAdd PtAddN QDec StepN DblClsN

structure CL where
  s : Bool
  b : List Bool

def cxor (c d : CL) : CL := ⟨xor c.s d.s, xorL c.b d.b⟩

def zc (ps : List Nat) : CL := ⟨false, zL ps⟩

def WF (ps : List Nat) (c : CL) : Prop := c.b.length = ps.length

def HCc (n : Q) (ps : List Nat) (x : Q) (β1 β2 : CL) : Prop := HCn n ps x β1.s β1.b β2.s β2.b

/-- classes of the points (0,0), (n,0), (−n,0) -/
def TorClsN (n : Q) (ps : List Nat) (D : TorData n ps) (x : Q) (β1 β2 : CL) : Prop :=
  (x = zeroQ ∧ β1 = ⟨xor D.k0m D.k0n, xorL D.b0m D.b0n⟩ ∧ β2 = ⟨D.k0n, D.b0n⟩) ∨
  (x = n ∧ β1 = ⟨D.kn0, D.bn0⟩ ∧ β2 = ⟨xor D.knm D.kn0, xorL D.bnm D.bn0⟩) ∨
  (x = negQ n ∧ β1 = ⟨D.km0, D.bm0⟩ ∧ β2 = ⟨D.kmn, D.bmn⟩)

def ClsPtN (n : Q) (ps : List Nat) (D : TorData n ps) : Pt → CL → CL → Prop
  | Pt.O, β1, β2 => β1 = zc ps ∧ β2 = zc ps
  | Pt.A x _, β1, β2 => WF ps β1 ∧ WF ps β2 ∧ ((NDn n x ∧ HCc n ps x β1 β2) ∨ TorClsN n ps D x β1 β2)

/-! ## Bit-list algebra -/

theorem bxor_self : ∀ a : Bool, xor a a = false
  | true => rfl
  | false => rfl

theorem bxor_f : ∀ a : Bool, xor false a = a
  | true => rfl
  | false => rfl

theorem bxor_f' : ∀ a : Bool, xor a false = a
  | true => rfl
  | false => rfl

theorem bsolve : ∀ a b : Bool, xor (xor a b) b = a
  | true, true => rfl
  | true, false => rfl
  | false, true => rfl
  | false, false => rfl

theorem bsolve2 : ∀ a b : Bool, xor (xor a b) a = b
  | true, true => rfl
  | true, false => rfl
  | false, true => rfl
  | false, false => rfl

theorem xorL_self : ∀ (ps : List Nat) (b : List Bool), b.length = ps.length → xorL b b = zL ps
  | [], [], _ => rfl
  | [], _ :: _, h => Nat.noConfusion h
  | _ :: _, [], h => Nat.noConfusion h
  | _ :: ps, a :: b, h => by
    show xor a a :: xorL b b = false :: zL ps
    rw [bxor_self a, xorL_self ps b (Nat.succ.inj h)]

theorem zL_xorL : ∀ (ps : List Nat) (b : List Bool), b.length = ps.length → xorL (zL ps) b = b
  | [], [], _ => rfl
  | [], _ :: _, h => Nat.noConfusion h
  | _ :: _, [], h => Nat.noConfusion h
  | _ :: ps, a :: b, h => by
    show xor false a :: xorL (zL ps) b = a :: b
    rw [bxor_f a, zL_xorL ps b (Nat.succ.inj h)]

theorem xorL_zL : ∀ (ps : List Nat) (b : List Bool), b.length = ps.length → xorL b (zL ps) = b
  | [], [], _ => rfl
  | [], _ :: _, h => Nat.noConfusion h
  | _ :: _, [], h => Nat.noConfusion h
  | _ :: ps, a :: b, h => by
    show xor a false :: xorL b (zL ps) = a :: b
    rw [bxor_f' a, xorL_zL ps b (Nat.succ.inj h)]

theorem lsolve : ∀ a b : List Bool, a.length = b.length → xorL (xorL a b) b = a
  | [], _, _ => rfl
  | _ :: _, [], h => Nat.noConfusion h
  | x :: a, y :: b, h => by
    show xor (xor x y) y :: xorL (xorL a b) b = x :: a
    rw [bsolve x y, lsolve a b (Nat.succ.inj h)]

theorem lsolve2 : ∀ a b : List Bool, a.length = b.length → xorL (xorL a b) a = b
  | [], [], _ => rfl
  | [], _ :: _, h => Nat.noConfusion h
  | _ :: _, [], h => Nat.noConfusion h
  | x :: a, y :: b, h => by
    show xor (xor x y) x :: xorL (xorL a b) a = y :: b
    rw [bsolve2 x y, lsolve2 a b (Nat.succ.inj h)]

theorem cxor_self (ps : List Nat) (β : CL) (h : WF ps β) : cxor β β = zc ps := by
  cases β with
  | mk s b =>
    show CL.mk (xor s s) (xorL b b) = CL.mk false (zL ps)
    rw [bxor_self s, xorL_self ps b h]

theorem zc_cxor (ps : List Nat) (β : CL) (h : WF ps β) : cxor (zc ps) β = β := by
  cases β with
  | mk s b =>
    show CL.mk (xor false s) (xorL (zL ps) b) = CL.mk s b
    rw [bxor_f s, zL_xorL ps b h]

theorem cxor_zc (ps : List Nat) (β : CL) (h : WF ps β) : cxor β (zc ps) = β := by
  cases β with
  | mk s b =>
    show CL.mk (xor s false) (xorL b (zL ps)) = CL.mk s b
    rw [bxor_f' s, xorL_zL ps b h]

theorem wf_cxor (ps : List Nat) (β γ : CL) (h1 : WF ps β) (h2 : WF ps γ) : WF ps (cxor β γ) :=
  xorL_lenL ps β.b γ.b h1 h2

theorem wf_zc (ps : List Nat) : WF ps (zc ps) := zL_len ps

theorem wf_of (n : Q) (ps : List Nat) (D : TorData n ps) : ∀ (P : Pt) (β1 β2 : CL), ClsPtN n ps D P β1 β2 →
    WF ps β1 ∧ WF ps β2
  | Pt.O, b1, b2, c => by
    obtain ⟨e1, e2⟩ := c
    rw [e1, e2]
    exact ⟨wf_zc ps, wf_zc ps⟩
  | Pt.A _ _, _, _, c => by
    obtain ⟨w1, w2, _⟩ := c
    exact ⟨w1, w2⟩

/-! ## Uniqueness -/

theorem cls_eqN (ps : List Nat) (hc : chkL ps = true) (hp : posL ps = true) (x : Q) (β γ : CL) (hβ : WF ps β)
    (hγ : WF ps γ) (s t : Q) (hs : s ≠ zeroQ) (e1 : x = ratR.mul (cqL ps β.s β.b) (ratR.mul s s))
    (e2 : x = ratR.mul (cqL ps γ.s γ.b) (ratR.mul t t)) : β = γ := by
  cases β with
  | mk s1 b1 =>
    cases γ with
    | mk s2 b2 =>
      obtain ⟨d1, d2⟩ := bits_of_eqL ps hc hp s1 s2 b1 b2 hβ hγ 1 TransStep2.one_ne _
        ((castQ_mul _ _).trans (PtCls.rescale ratR (cqL ps s1 b1) (cqL ps s2 b2) s (invQ s) t (mul_invQ s hs)
          (e1.symm.trans e2)))
      rw [← d1, ← d2]

theorem hc_uniqueN (n : Q) (ps : List Nat) (hc : chkL ps = true) (hp : posL ps = true) (x : Q) (β1 β2 γ1 γ2 : CL)
    (w1 : WF ps β1) (w2 : WF ps β2) (v1 : WF ps γ1) (v2 : WF ps γ2) (h1 : HCc n ps x β1 β2) (h2 : HCc n ps x γ1 γ2) :
    β1 = γ1 ∧ β2 = γ2 := by
  obtain ⟨s, r, hs, hr, ex, exn⟩ := h1
  obtain ⟨s', r', _, _, ex', exn'⟩ := h2
  exact ⟨cls_eqN ps hc hp x β1 γ1 w1 v1 s s' hs ex ex', cls_eqN ps hc hp _ β2 γ2 w2 v2 r r' hr exn exn'⟩

theorem tor_uniqueN (n : Q) (ps : List Nat) (D : TorData n ps) (hn : n ≠ zeroQ) (x : Q) (β1 β2 γ1 γ2 : CL)
    (h1 : TorClsN n ps D x β1 β2) (h2 : TorClsN n ps D x γ1 γ2) : β1 = γ1 ∧ β2 = γ2 := by
  have m_ne : negQ n ≠ zeroQ := PtCls.neg_ne ratR n hn
  have n_ne_m : n ≠ negQ n := PtCls.ne_neg ratR rat_noZD Kernel2.two_one_ne n hn
  rcases h1 with ⟨e, b1, b2⟩ | ⟨e, b1, b2⟩ | ⟨e, b1, b2⟩ <;>
  rcases h2 with ⟨e', c1, c2⟩ | ⟨e', c1, c2⟩ | ⟨e', c1, c2⟩
  · exact ⟨b1.trans c1.symm, b2.trans c2.symm⟩
  · exact absurd (e'.symm.trans e).symm (fun h => hn h.symm)
  · exact absurd (e'.symm.trans e).symm (fun h => m_ne h.symm)
  · exact absurd (e.symm.trans e') hn
  · exact ⟨b1.trans c1.symm, b2.trans c2.symm⟩
  · exact absurd (e.symm.trans e') n_ne_m
  · exact absurd (e.symm.trans e') m_ne
  · exact absurd (e'.symm.trans e) n_ne_m
  · exact ⟨b1.trans c1.symm, b2.trans c2.symm⟩

theorem nd_torN (n : Q) (ps : List Nat) (D : TorData n ps) (x : Q) (β1 β2 : CL) (nd : NDn n x)
    (h : TorClsN n ps D x β1 β2) : False := by
  rcases h with ⟨e, _, _⟩ | ⟨e, _, _⟩ | ⟨e, _, _⟩
  · exact nd.1 e
  · exact nd.2.1 (PtCls.sub_self_zero ratR x n e)
  · exact nd.2.2 (PtCls.sub_self_zero ratR x (negQ n) e)

theorem same_xN (n : Q) (ps : List Nat) (hc : chkL ps = true) (hp : posL ps = true) (D : TorData n ps)
    (hn : n ≠ zeroQ) (x y y' : Q) (β1 β2 γ1 γ2 : CL) (c1 : ClsPtN n ps D (Pt.A x y) β1 β2)
    (c2 : ClsPtN n ps D (Pt.A x y') γ1 γ2) : β1 = γ1 ∧ β2 = γ2 := by
  obtain ⟨w1, w2, c1⟩ := c1
  obtain ⟨v1, v2, c2⟩ := c2
  rcases c1 with ⟨nd, h1⟩ | t1 <;> rcases c2 with ⟨nd', h2⟩ | t2
  · exact hc_uniqueN n ps hc hp x β1 β2 γ1 γ2 w1 w2 v1 v2 h1 h2
  · exact (nd_torN n ps D x γ1 γ2 nd t2).elim
  · exact (nd_torN n ps D x β1 β2 nd' t1).elim
  · exact tor_uniqueN n ps D hn x β1 β2 γ1 γ2 t1 t2

/-! ## Additivity: O, vertical, tangent, chord away from E[2] -/

theorem cls_add_ON (n : Q) (ps : List Nat) (D : TorData n ps) (P : Pt) (β1 β2 γ1 γ2 : CL)
    (c1 : ClsPtN n ps D Pt.O β1 β2) (c2 : ClsPtN n ps D P γ1 γ2) :
    ClsPtN n ps D (addN n Pt.O P) (cxor β1 γ1) (cxor β2 γ2) := by
  obtain ⟨v1, v2⟩ := wf_of n ps D P γ1 γ2 c2
  obtain ⟨e1, e2⟩ := c1
  rw [e1, e2, zc_cxor ps γ1 v1, zc_cxor ps γ2 v2]
  exact c2

theorem cls_add_ON' (n : Q) (ps : List Nat) (D : TorData n ps) (x y : Q) (β1 β2 γ1 γ2 : CL)
    (c1 : ClsPtN n ps D (Pt.A x y) β1 β2) (c2 : ClsPtN n ps D Pt.O γ1 γ2) :
    ClsPtN n ps D (addN n (Pt.A x y) Pt.O) (cxor β1 γ1) (cxor β2 γ2) := by
  obtain ⟨w1, w2⟩ := wf_of n ps D _ β1 β2 c1
  obtain ⟨e1, e2⟩ := c2
  rw [e1, e2, cxor_zc ps β1 w1, cxor_zc ps β2 w2]
  exact c1

/-- **Vertical pair: the sum O.** -/
theorem cls_vertN (n : Q) (ps : List Nat) (hc : chkL ps = true) (hp : posL ps = true) (D : TorData n ps)
    (hn : n ≠ zeroQ) (x y1 y2 : Q) (hy : y1 = ratR.neg y2) (β1 β2 γ1 γ2 : CL)
    (c1 : ClsPtN n ps D (Pt.A x y1) β1 β2) (c2 : ClsPtN n ps D (Pt.A x y2) γ1 γ2) :
    ClsPtN n ps D (addAN n x y1 x y2) (cxor β1 γ1) (cxor β2 γ2) := by
  obtain ⟨w1, w2⟩ := wf_of n ps D _ β1 β2 c1
  obtain ⟨e1, e2⟩ := same_xN n ps hc hp D hn x y1 y2 β1 β2 γ1 γ2 c1 c2
  unfold addAN
  rw [qbeq_self x]
  cases e : qbeq y1 (ratR.neg y2) with
  | false => exact absurd hy (qbeq_false _ _ e)
  | true =>
    show cxor β1 γ1 = zc ps ∧ cxor β2 γ2 = zc ps
    rw [← e1, ← e2]
    exact ⟨cxor_self ps β1 w1, cxor_self ps β2 w2⟩

/-- **Tangent: 2P has class 0.** -/
theorem cls_dblN (n : Q) (ps : List Nat) (hc : chkL ps = true) (hp : posL ps = true) (D : TorData n ps)
    (hn : n ≠ zeroQ) (h0n : ∀ T : Q, sub ratR zeroQ n ≠ ratR.mul T T) (hn0 : ∀ T : Q, sub ratR n zeroQ ≠ ratR.mul T T)
    (hm0 : ∀ T : Q, sub ratR (negQ n) zeroQ ≠ ratR.mul T T)
    (x y1 y2 : Q) (o1 : OnEn n x y1) (o2 : OnEn n x y2) (hne : y1 ≠ ratR.neg y2) (β1 β2 γ1 γ2 : CL)
    (c1 : ClsPtN n ps D (Pt.A x y1) β1 β2) (c2 : ClsPtN n ps D (Pt.A x y2) γ1 γ2) :
    ClsPtN n ps D (addAN n x y1 x y2) (cxor β1 γ1) (cxor β2 γ2) := by
  obtain ⟨w1, w2⟩ := wf_of n ps D _ β1 β2 c1
  obtain ⟨e1, e2⟩ := same_xN n ps hc hp D hn x y1 y2 β1 β2 γ1 γ2 c1 c2
  have d := dbl_nd_clsN n ps x y1 _ o1 (dbl_wn n x y1 y2 o1 o2 hne) h0n hn0 hm0
  unfold addAN
  rw [qbeq_self x, PtCls.qbeq_ne y1 (ratR.neg y2) hne]
  show WF ps (cxor β1 γ1) ∧ WF ps (cxor β2 γ2) ∧
    ((NDn n (dblXn n x y1) ∧ HCc n ps (dblXn n x y1) (cxor β1 γ1) (cxor β2 γ2)) ∨
      TorClsN n ps D (dblXn n x y1) (cxor β1 γ1) (cxor β2 γ2))
  rw [← e1, ← e2, cxor_self ps β1 w1, cxor_self ps β2 w2]
  exact ⟨wf_zc ps, wf_zc ps, Or.inl d⟩

/-- **Chord between two points away from E[2].** -/
theorem cls_chordN (n : Q) (ps : List Nat) (hc : chkL ps = true) (hp : posL ps = true) (D : TorData n ps)
    (x1 y1 x2 y2 : Q) (o1 : OnEn n x1 y1) (o2 : OnEn n x2 y2) (h12 : x1 ≠ x2) (β1 β2 γ1 γ2 : CL)
    (w1 : WF ps β1) (w2 : WF ps β2) (v1 : WF ps γ1) (v2 : WF ps γ2)
    (nd1 : NDn n x1) (nd2 : NDn n x2) (hc1 : HCc n ps x1 β1 β2) (hc2 : HCc n ps x2 γ1 γ2) :
    ClsPtN n ps D (addAN n x1 y1 x2 y2) (cxor β1 γ1) (cxor β2 γ2) := by
  unfold addAN
  cases e : qbeq x1 x2 with
  | true => exact absurd (qbeq_true x1 x2 e) h12
  | false =>
    show WF ps (cxor β1 γ1) ∧ WF ps (cxor β2 γ2) ∧
      ((NDn n (x3n n x1 y1 x2 y2) ∧ HCc n ps (x3n n x1 y1 x2 y2) (cxor β1 γ1) (cxor β2 γ2)) ∨
        TorClsN n ps D (x3n n x1 y1 x2 y2) (cxor β1 γ1) (cxor β2 γ2))
    refine ⟨wf_cxor ps β1 γ1 w1 v1, wf_cxor ps β2 γ2 w2 v2, ?_⟩
    obtain ⟨s1, b1⟩ := β1
    obtain ⟨s2, b2⟩ := β2
    obtain ⟨t1, c1⟩ := γ1
    obtain ⟨t2, c2⟩ := γ2
    have l1 : (xorL b1 c1).length = (xorL b2 c2).length :=
      (xorL_lenL ps b1 c1 w1 v1).trans (xorL_lenL ps b2 c2 w2 v2).symm
    rcases stepN n ps hc hp D x1 y1 x2 y2 o1 o2 h12 s1 s2 t1 t2 b1 b2 c1 c2 w1 w2 v1 v2 hc1 hc2 nd1 nd2 with
      h | ⟨hz, ⟨d2s, d2b⟩, ⟨ds, db⟩⟩ | ⟨hz, ⟨d1s, d1b⟩, ⟨ds, db⟩⟩ | ⟨hz, ⟨d1s, d1b⟩, ⟨d2s, d2b⟩⟩
    · exact Or.inl h
    · refine Or.inr (Or.inl ⟨hz, ?_, ?_⟩)
      · show CL.mk (xor s1 t1) (xorL b1 c1) = CL.mk (xor D.k0m D.k0n) (xorL D.b0m D.b0n)
        rw [← ds, ← d2s, ← db, ← d2b, bsolve, lsolve _ _ l1]
      · show CL.mk (xor s2 t2) (xorL b2 c2) = CL.mk D.k0n D.b0n
        rw [d2s, d2b]
    · refine Or.inr (Or.inr (Or.inl ⟨hz, ?_, ?_⟩))
      · show CL.mk (xor s1 t1) (xorL b1 c1) = CL.mk D.kn0 D.bn0
        rw [d1s, d1b]
      · show CL.mk (xor s2 t2) (xorL b2 c2) = CL.mk (xor D.knm D.kn0) (xorL D.bnm D.bn0)
        rw [← ds, ← d1s, ← db, ← d1b, bsolve2, lsolve2 _ _ l1]
    · refine Or.inr (Or.inr (Or.inr ⟨hz, ?_, ?_⟩))
      · show CL.mk (xor s1 t1) (xorL b1 c1) = CL.mk D.km0 D.bm0
        rw [d1s, d1b]
      · show CL.mk (xor s2 t2) (xorL b2 c2) = CL.mk D.kmn D.bmn
        rw [d2s, d2b]

end PtClsN

#print axioms PtClsN.bxor_self
#print axioms PtClsN.bxor_f
#print axioms PtClsN.bxor_f'
#print axioms PtClsN.bsolve
#print axioms PtClsN.bsolve2
#print axioms PtClsN.xorL_self
#print axioms PtClsN.zL_xorL
#print axioms PtClsN.xorL_zL
#print axioms PtClsN.lsolve
#print axioms PtClsN.lsolve2
#print axioms PtClsN.cxor_self
#print axioms PtClsN.zc_cxor
#print axioms PtClsN.cxor_zc
#print axioms PtClsN.wf_cxor
#print axioms PtClsN.wf_zc
#print axioms PtClsN.wf_of
#print axioms PtClsN.cls_eqN
#print axioms PtClsN.hc_uniqueN
#print axioms PtClsN.tor_uniqueN
#print axioms PtClsN.nd_torN
#print axioms PtClsN.same_xN
#print axioms PtClsN.cls_add_ON
#print axioms PtClsN.cls_add_ON'
#print axioms PtClsN.cls_vertN
#print axioms PtClsN.cls_dblN
#print axioms PtClsN.cls_chordN

bsd/PtClsN2.lean

lean_all/bsd/PtClsN2.lean

/-
  bsd/PtClsN2.lean — T39 (goal G289f-4f, assembly): the 2-descent class is additive on all of E_n(ℚ), n a rational
  parameter, classes over a certified prime list ps (bsd/PtCls2 fixes n = 42486):
      ClsPtN P β, ClsPtN Q γ  ⇒  ClsPtN (P + Q) (β ⊕ γ)
  given the torsion data D and the three relations δ(Tᵢ) ⊕ δ(Tⱼ) = δ(Tₖ) (TorRel; for n = 42486 by rfl).
    cxor_comm, xorL_comm     commutativity
    TorRel                   the relations between the classes of T0, T1, T2
    tor_yN, x3n_symm         y = 0 on E[2]; the chord abscissa does not depend on the order
    x3_T0N, x3_T1N, x3_T2N   the chord through a point of E[2] is the translate (bsd/ChordTor)
    x_tor_ndN, cls_tor_torN, cls_chord_allN, cls_addN
  Lean 4 core plus bsd/TorClsN12, bsd/ChordSym, bsd/ChordTor, bsd/PtCls2.
-/
import bsd.TorClsN12
import bsd.ChordSym
import bsd.ChordTor
import bsd.PtCls2

namespace PtClsN2
open DetChain LibRing LibRat LibInt QModel QCls B5S SqUniqueL SqClassL QModelN TransStepL StepN PtAdd PtAddN QDec
  PtClsN TorClsN0 TorClsN12

theorem bxor_comm : ∀ x y : Bool, xor x y = xor y x
  | true, true => rfl
  | true, false => rfl
  | false, true => rfl
  | false, false => rfl

theorem xorL_comm : ∀ a b : List Bool, xorL a b = xorL b a
  | [], [] => rfl
  | [], _ :: _ => rfl
  | _ :: _, [] => rfl
  | x :: a, y :: b => by
    show xor x y :: xorL a b = xor y x :: xorL b a
    rw [bxor_comm x y, xorL_comm a b]

theorem cxor_comm (β γ : CL) : cxor β γ = cxor γ β := by
  cases β with
  | mk s b =>
    cases γ with
    | mk t c =>
      show CL.mk (xor s t) (xorL b c) = CL.mk (xor t s) (xorL c b)
      rw [bxor_comm s t, xorL_comm b c]

/-- the relations δ(T0) ⊕ δ(T1) = δ(T2), δ(T0) ⊕ δ(T2) = δ(T1), δ(T1) ⊕ δ(T2) = δ(T0) -/
structure TorRel (n : Q) (ps : List Nat) (D : TorData n ps) : Prop where
  r01a : cxor ⟨xor D.k0m D.k0n, xorL D.b0m D.b0n⟩ ⟨D.kn0, D.bn0⟩ = ⟨D.km0, D.bm0⟩
  r01b : cxor ⟨D.k0n, D.b0n⟩ ⟨xor D.knm D.kn0, xorL D.bnm D.bn0⟩ = ⟨D.kmn, D.bmn⟩
  r02a : cxor ⟨xor D.k0m D.k0n, xorL D.b0m D.b0n⟩ ⟨D.km0, D.bm0⟩ = ⟨D.kn0, D.bn0⟩
  r02b : cxor ⟨D.k0n, D.b0n⟩ ⟨D.kmn, D.bmn⟩ = ⟨xor D.knm D.kn0, xorL D.bnm D.bn0⟩
  r12a : cxor ⟨D.kn0, D.bn0⟩ ⟨D.km0, D.bm0⟩ = ⟨xor D.k0m D.k0n, xorL D.b0m D.b0n⟩
  r12b : cxor ⟨xor D.knm D.kn0, xorL D.bnm D.bn0⟩ ⟨D.kmn, D.bmn⟩ = ⟨D.k0n, D.b0n⟩

theorem tor_yN (n : Q) (ps : List Nat) (D : TorData n ps) (x y : Q) (o : OnEn n x y) (β1 β2 : CL)
    (t : TorClsN n ps D x β1 β2) : y = zeroQ := by
  rcases t with ⟨e, _, _⟩ | ⟨e, _, _⟩ | ⟨e, _, _⟩
  · subst x; exact ChordSym.on_root1 ratR rat_noZD zeroQ n (negQ n) y o
  · subst x; exact ChordSym.on_root2 ratR rat_noZD zeroQ n (negQ n) y o
  · subst x; exact ChordSym.on_root3 ratR rat_noZD zeroQ n (negQ n) y o

theorem x3n_symm (n x1 y1 x2 y2 : Q) (h : x1 ≠ x2) : x3n n x1 y1 x2 y2 = x3n n x2 y2 x1 y1 := by
  unfold x3n TransQ.slope
  rw [ChordSym.slope_symm ratR x1 y1 x2 y2 _ _ (mul_invQ _ (ChordSym.sub_ne_of_ne x2 x1 (fun e => h e.symm)))
    (mul_invQ _ (ChordSym.sub_ne_of_ne x1 x2 h))]
  exact ChordSym.third_symm ratR _ x1 x2

theorem x3_T0N (n x2 y2 : Q) (o : OnEn n x2 y2) (h : sub ratR x2 zeroQ ≠ zeroQ) :
    x3n n zeroQ zeroQ x2 y2 = TorTrans.xT ratR zeroQ n (negQ n) (invQ (sub ratR x2 zeroQ)) :=
  ChordTor.chord_tor1 ratR zeroQ n (negQ n) x2 y2 _ o (mul_invQ _ h)

theorem x3_T1N (n x2 y2 : Q) (o : OnEn n x2 y2) (h : sub ratR x2 n ≠ zeroQ) :
    x3n n n zeroQ x2 y2 = TorTrans.xT ratR n zeroQ (negQ n) (invQ (sub ratR x2 n)) :=
  ChordTor.chord_tor2 ratR zeroQ n (negQ n) x2 y2 _ o (mul_invQ _ h)

theorem x3_T2N (n x2 y2 : Q) (o : OnEn n x2 y2) (h : sub ratR x2 (negQ n) ≠ zeroQ) :
    x3n n (negQ n) zeroQ x2 y2 = TorTrans.xT ratR (negQ n) zeroQ n (invQ (sub ratR x2 (negQ n))) :=
  ChordTor.chord_tor3 ratR zeroQ n (negQ n) x2 y2 _ o (mul_invQ _ h)

/-- **One point of E[2] on the chord.** -/
theorem x_tor_ndN (n : Q) (ps : List Nat) (hp : posL ps = true) (D : TorData n ps) (x1 y1 x2 y2 : Q)
    (o1 : OnEn n x1 y1) (o2 : OnEn n x2 y2) (β1 β2 γ1 γ2 : CL) (v1 : WF ps γ1) (v2 : WF ps γ2)
    (t1 : TorClsN n ps D x1 β1 β2) (nd2 : NDn n x2) (hc2 : HCc n ps x2 γ1 γ2) :
    NDn n (x3n n x1 y1 x2 y2) ∧ HCc n ps (x3n n x1 y1 x2 y2) (cxor β1 γ1) (cxor β2 γ2) := by
  have hy1 := tor_yN n ps D x1 y1 o1 β1 β2 t1
  subst hy1
  rcases t1 with ⟨e, b1, b2⟩ | ⟨e, b1, b2⟩ | ⟨e, b1, b2⟩
  · subst x1; subst b1; subst b2
    have h0 : sub ratR x2 zeroQ ≠ zeroQ := fun h => nd2.1 ((TransStep.sub0 x2).symm.trans h)
    rw [x3_T0N n x2 y2 o2 h0]
    exact cls_T0N n ps hp D x2 y2 o2 nd2 γ1 γ2 v1 v2 hc2
  · subst x1; subst b1; subst b2
    rw [x3_T1N n x2 y2 o2 nd2.2.1]
    exact cls_T1N n ps hp D x2 y2 o2 nd2 γ1 γ2 v1 v2 hc2
  · subst x1; subst b1; subst b2
    rw [x3_T2N n x2 y2 o2 nd2.2.2]
    exact cls_T2N n ps hp D x2 y2 o2 nd2 γ1 γ2 v1 v2 hc2

theorem n0 (n : Q) : ratR.add (ratR.add n zeroQ) (negQ n) = ratR.add (ratR.add zeroQ n) (negQ n) :=
  PtCls2.g_ba ratR n zeroQ (negQ n)
theorem m0 (n : Q) : ratR.add (ratR.add zeroQ (negQ n)) n = ratR.add (ratR.add zeroQ n) (negQ n) :=
  PtCls2.g_acb ratR zeroQ n (negQ n)
theorem m0' (n : Q) : ratR.add (ratR.add (negQ n) zeroQ) n = ratR.add (ratR.add zeroQ n) (negQ n) :=
  PtCls2.g_cab ratR zeroQ n (negQ n)
theorem nm (n : Q) : ratR.add (ratR.add n (negQ n)) zeroQ = ratR.add (ratR.add zeroQ n) (negQ n) :=
  PtCls2.g_bca ratR zeroQ n (negQ n)
theorem mn (n : Q) : ratR.add (ratR.add (negQ n) n) zeroQ = ratR.add (ratR.add zeroQ n) (negQ n) :=
  PtCls2.g_cba ratR zeroQ n (negQ n)

/-- **Two points of E[2] on the chord.** -/
theorem cls_tor_torN (n : Q) (ps : List Nat) (D : TorData n ps) (R : TorRel n ps D) (x1 y1 x2 y2 : Q)
    (o1 : OnEn n x1 y1) (o2 : OnEn n x2 y2) (h12 : x1 ≠ x2) (β1 β2 γ1 γ2 : CL)
    (t1 : TorClsN n ps D x1 β1 β2) (t2 : TorClsN n ps D x2 γ1 γ2) :
    TorClsN n ps D (x3n n x1 y1 x2 y2) (cxor β1 γ1) (cxor β2 γ2) := by
  have hy1 := tor_yN n ps D x1 y1 o1 β1 β2 t1
  have hy2 := tor_yN n ps D x2 y2 o2 γ1 γ2 t2
  subst hy1; subst hy2
  unfold x3n TransQ.slope
  rcases t1 with ⟨e, b1, b2⟩ | ⟨e, b1, b2⟩ | ⟨e, b1, b2⟩ <;>
  rcases t2 with ⟨e', c1, c2⟩ | ⟨e', c1, c2⟩ | ⟨e', c1, c2⟩ <;>
  subst x1 <;> subst x2 <;> subst b1 <;> subst b2 <;> subst c1 <;> subst c2
  · exact absurd rfl h12
  · exact Or.inr (Or.inr ⟨ChordSym.third_tor ratR _ _ _ _ _ _ _ _ rfl, R.r01a, R.r01b⟩)
  · exact Or.inr (Or.inl ⟨ChordSym.third_tor ratR _ _ _ _ _ _ _ _ (m0 n), R.r02a, R.r02b⟩)
  · exact Or.inr (Or.inr ⟨ChordSym.third_tor ratR _ _ _ _ _ _ _ _ (n0 n),
      (cxor_comm _ _).trans R.r01a, (cxor_comm _ _).trans R.r01b⟩)
  · exact absurd rfl h12
  · exact Or.inl ⟨ChordSym.third_tor ratR _ _ _ _ _ _ _ _ (nm n), R.r12a, R.r12b⟩
  · exact Or.inr (Or.inl ⟨ChordSym.third_tor ratR _ _ _ _ _ _ _ _ (m0' n),
      (cxor_comm _ _).trans R.r02a, (cxor_comm _ _).trans R.r02b⟩)
  · exact Or.inl ⟨ChordSym.third_tor ratR _ _ _ _ _ _ _ _ (mn n),
      (cxor_comm _ _).trans R.r12a, (cxor_comm _ _).trans R.r12b⟩
  · exact absurd rfl h12

/-- the chord case x₁ ≠ x₂ -/
theorem cls_chord_allN (n : Q) (ps : List Nat) (hc : chkL ps = true) (hp : posL ps = true) (D : TorData n ps)
    (R : TorRel n ps D) (x1 y1 x2 y2 : Q) (o1 : OnEn n x1 y1) (o2 : OnEn n x2 y2) (h12 : x1 ≠ x2)
    (β1 β2 γ1 γ2 : CL) (c1 : ClsPtN n ps D (Pt.A x1 y1) β1 β2) (c2 : ClsPtN n ps D (Pt.A x2 y2) γ1 γ2) :
    ClsPtN n ps D (addAN n x1 y1 x2 y2) (cxor β1 γ1) (cxor β2 γ2) := by
  have hq : qbeq x1 x2 = false := PtCls.qbeq_ne x1 x2 h12
  obtain ⟨w1, w2, c1⟩ := c1
  obtain ⟨v1, v2, c2⟩ := c2
  rcases c1 with ⟨nd1, hc1⟩ | t1 <;> rcases c2 with ⟨nd2, hc2⟩ | t2
  · exact cls_chordN n ps hc hp D x1 y1 x2 y2 o1 o2 h12 β1 β2 γ1 γ2 w1 w2 v1 v2 nd1 nd2 hc1 hc2
  · unfold addAN; rw [hq]
    show WF ps (cxor β1 γ1) ∧ WF ps (cxor β2 γ2) ∧
      ((NDn n (x3n n x1 y1 x2 y2) ∧ HCc n ps (x3n n x1 y1 x2 y2) (cxor β1 γ1) (cxor β2 γ2)) ∨
        TorClsN n ps D (x3n n x1 y1 x2 y2) (cxor β1 γ1) (cxor β2 γ2))
    refine ⟨wf_cxor ps β1 γ1 w1 v1, wf_cxor ps β2 γ2 w2 v2, ?_⟩
    rw [x3n_symm n x1 y1 x2 y2 h12, cxor_comm β1 γ1, cxor_comm β2 γ2]
    exact Or.inl (x_tor_ndN n ps hp D x2 y2 x1 y1 o2 o1 γ1 γ2 β1 β2 w1 w2 t2 nd1 hc1)
  · unfold addAN; rw [hq]
    show WF ps (cxor β1 γ1) ∧ WF ps (cxor β2 γ2) ∧
      ((NDn n (x3n n x1 y1 x2 y2) ∧ HCc n ps (x3n n x1 y1 x2 y2) (cxor β1 γ1) (cxor β2 γ2)) ∨
        TorClsN n ps D (x3n n x1 y1 x2 y2) (cxor β1 γ1) (cxor β2 γ2))
    exact ⟨wf_cxor ps β1 γ1 w1 v1, wf_cxor ps β2 γ2 w2 v2,
      Or.inl (x_tor_ndN n ps hp D x1 y1 x2 y2 o1 o2 β1 β2 γ1 γ2 v1 v2 t1 nd2 hc2)⟩
  · unfold addAN; rw [hq]
    show WF ps (cxor β1 γ1) ∧ WF ps (cxor β2 γ2) ∧
      ((NDn n (x3n n x1 y1 x2 y2) ∧ HCc n ps (x3n n x1 y1 x2 y2) (cxor β1 γ1) (cxor β2 γ2)) ∨
        TorClsN n ps D (x3n n x1 y1 x2 y2) (cxor β1 γ1) (cxor β2 γ2))
    exact ⟨wf_cxor ps β1 γ1 w1 v1, wf_cxor ps β2 γ2 w2 v2,
      Or.inr (cls_tor_torN n ps D R x1 y1 x2 y2 o1 o2 h12 β1 β2 γ1 γ2 t1 t2)⟩

/-- **The 2-descent class is additive (general n).** -/
theorem cls_addN (n : Q) (ps : List Nat) (hc : chkL ps = true) (hp : posL ps = true) (D : TorData n ps)
    (R : TorRel n ps D) (hn : n ≠ zeroQ) (h0n : ∀ T : Q, sub ratR zeroQ n ≠ ratR.mul T T)
    (hn0 : ∀ T : Q, sub ratR n zeroQ ≠ ratR.mul T T) (hm0 : ∀ T : Q, sub ratR (negQ n) zeroQ ≠ ratR.mul T T) :
    ∀ (P1 P2 : Pt) (β1 β2 γ1 γ2 : CL), OnPtN n P1 → OnPtN n P2 → ClsPtN n ps D P1 β1 β2 →
      ClsPtN n ps D P2 γ1 γ2 → ClsPtN n ps D (addN n P1 P2) (cxor β1 γ1) (cxor β2 γ2)
  | Pt.O, P, β1, β2, γ1, γ2, _, _, c1, c2 => cls_add_ON n ps D P β1 β2 γ1 γ2 c1 c2
  | Pt.A x y, Pt.O, β1, β2, γ1, γ2, _, _, c1, c2 => cls_add_ON' n ps D x y β1 β2 γ1 γ2 c1 c2
  | Pt.A x1 y1, Pt.A x2 y2, β1, β2, γ1, γ2, o1, o2, c1, c2 => by
    show ClsPtN n ps D (addAN n x1 y1 x2 y2) (cxor β1 γ1) (cxor β2 γ2)
    cases e : qbeq x1 x2 with
    | false => exact cls_chord_allN n ps hc hp D R x1 y1 x2 y2 o1 o2 (qbeq_false _ _ e) β1 β2 γ1 γ2 c1 c2
    | true =>
      have hx := qbeq_true _ _ e
      subst hx
      cases e2 : qbeq y1 (ratR.neg y2) with
      | true => exact cls_vertN n ps hc hp D hn x1 y1 y2 (qbeq_true _ _ e2) β1 β2 γ1 γ2 c1 c2
      | false => exact cls_dblN n ps hc hp D hn h0n hn0 hm0 x1 y1 y2 o1 o2 (qbeq_false _ _ e2) β1 β2 γ1 γ2 c1 c2

end PtClsN2

#print axioms PtClsN2.bxor_comm
#print axioms PtClsN2.xorL_comm
#print axioms PtClsN2.cxor_comm
#print axioms PtClsN2.tor_yN
#print axioms PtClsN2.x3n_symm
#print axioms PtClsN2.x3_T0N
#print axioms PtClsN2.x3_T1N
#print axioms PtClsN2.x3_T2N
#print axioms PtClsN2.x_tor_ndN
#print axioms PtClsN2.n0
#print axioms PtClsN2.m0
#print axioms PtClsN2.m0'
#print axioms PtClsN2.nm
#print axioms PtClsN2.mn
#print axioms PtClsN2.cls_tor_torN
#print axioms PtClsN2.cls_chord_allN
#print axioms PtClsN2.cls_addN

bsd/QCls.lean

lean_all/bsd/QCls.lean

/-
  bsd/QCls.lean — T39 (goal G289e-6c): every rational point of E_n (n = 42486) with x(x − n)(x + n) ≠ 0 has a descent
  class supported on {−1, 2, 3, 73, 97}:  x = cls β₁·s²,  x − n = cls β₂·r²  with s, r ∈ Q nonzero.
    castQ i           the integer i as a rational number (normQ i 1)
    HasCls x β₁ β₂    the class statement above
    q_cls             OnE x y, x ≠ 0, x − n ≠ 0  ⇒  ∃ β₁ β₂, HasCls x β₁ β₂
  From QModel.q_model (x = X/t², X(X² − n²t⁴) = Y²) and ClsInt.cls_X, cls_Xm (X = cls β₁·U², X − nt² = cls β₂·V²).
  Lean 4 core plus bsd/QModel, bsd/ClsInt, lib/RatField.
-/
import bsd.QModel
import bsd.ClsInt
import lib.RatField

namespace QCls
open LibRat LibInt QModel

def castQ (i : Int) : Q := normQ i 1 (Nat.succ_pos 0)

/-- x = cls β₁·s², x − n = cls β₂·r² -/
def HasCls (x : Q) (s1 a1 b1 c1 d1 s2 a2 b2 c2 d2 : Bool) : Prop :=
  ∃ s r : Q, s ≠ zeroQ ∧ r ≠ zeroQ ∧
    x = ratR.mul (castQ (SqClass.cls s1 a1 b1 c1 d1)) (ratR.mul s s) ∧
    LibRing.sub ratR x nQ = ratR.mul (castQ (SqClass.cls s2 a2 b2 c2 d2)) (ratR.mul r r)

theorem normQ_ne_zero {U : Int} {t : Nat} (ht : 0 < t) (hU : U ≠ 0) : normQ U t ht ≠ zeroQ := by
  intro h
  have e := (normQ_eq_iff _ _ _ _ _ _).1 (h.trans zero_r)
  apply hU
  have e2 : U = U * Int.ofNat 1 := by lib_int
  rw [e2, e]; lib_int

theorem sub_sq (n X : Int) (t : Nat) :
    X - n * Int.ofNat (t * t) = X - n * (Int.ofNat t * Int.ofNat t) := by lib_int

theorem sub_sq1 (n X : Int) (t : Nat) :
    X * Int.ofNat 1 + -n * Int.ofNat (t * t) = X - n * (Int.ofNat t * Int.ofNat t) := by lib_int

theorem gen3 (n X T : Int) : X * (X * X - n * n * (T * T)) = X * ((X - n * T) * (X + n * T)) := by lib_int

/-- **The integral data of a rational point.** -/
theorem int_data (x y : Q) (h : OnE x y) (hx0 : x.num ≠ 0)
    (hxn : x.num - 42486 * Int.ofNat x.den ≠ 0) :
    ∃ t : Nat, ∃ Y u v U V : Int, ∃ s1 a1 b1 c1 d1 s2 a2 b2 c2 d2 : Bool,
      0 < t ∧ x.den = t * t ∧ u * x.num + v * Int.ofNat t = 1 ∧
      Y * Y = x.num * ((x.num - 42486 * (Int.ofNat t * Int.ofNat t)) * (x.num + 42486 * (Int.ofNat t * Int.ofNat t))) ∧
      x.num = SqClass.cls s1 a1 b1 c1 d1 * (U * U) ∧
      x.num - 42486 * (Int.ofNat t * Int.ofNat t) = SqClass.cls s2 a2 b2 c2 d2 * (V * V) ∧
      HasCls x s1 a1 b1 c1 d1 s2 a2 b2 c2 d2 := by
  obtain ⟨t, Y, hq, hY⟩ := q_model x y h
  have ht : 0 < t := by
    cases t with
    | zero => have h0 := x.den_pos; rw [hq] at h0; exact absurd h0 (Nat.lt_irrefl 0)
    | succ t' => exact Nat.succ_pos t'
  -- Bezout for (X, t)
  have hcop : ∀ d, d ∣ absN x.num → d ∣ t → d = 1 := fun d h1 h2 =>
    x.red d h1 (by rw [hq]; obtain ⟨k, hk⟩ := h2; exact ⟨k * t, by rw [hk]; lib_nat⟩)
  obtain ⟨a, b, hab⟩ := LibGcd.coprime_bezout hcop
  have hu : ∃ u : Int, u * x.num = a * Int.ofNat (absN x.num) := by
    cases IntModel.absN_pm x.num with
    | inl e => exact ⟨a, by rw [e]⟩
    | inr e => exact ⟨-a, by rw [e]; lib_int⟩
  obtain ⟨u, hu⟩ := hu
  have huv : u * x.num + b * Int.ofNat t = 1 := by rw [hu]; exact hab
  have hY' : Y * Y = x.num * ((x.num - 42486 * (Int.ofNat t * Int.ofNat t)) * (x.num + 42486 * (Int.ofNat t * Int.ofNat t))) := by
    rw [hY, show Int.ofNat (t * t) = Int.ofNat t * Int.ofNat t by lib_int]
    exact gen3 42486 _ _
  obtain ⟨s1, a1, b1, c1, d1, U, hU⟩ := ClsInt.cls_X _ _ _ _ _ huv hY'
  obtain ⟨s2, a2, b2, c2, d2, V, hV⟩ := ClsInt.cls_Xm _ _ _ _ _ huv hY'
  refine ⟨t, Y, u, b, U, V, s1, a1, b1, c1, d1, s2, a2, b2, c2, d2, ht, hq, huv, hY', hU, hV, ?_⟩
  unfold HasCls
  refine ⟨normQ U t ht, normQ V t ht, ?_, ?_, ?_, ?_⟩
  · exact normQ_ne_zero ht (fun h0 => hx0 (by rw [hU, h0]; lib_int))
  · refine normQ_ne_zero ht (fun h0 => hxn ?_)
    have e : x.num - 42486 * Int.ofNat x.den = x.num - 42486 * (Int.ofNat t * Int.ofNat t) := by
      rw [hq]; exact sub_sq 42486 x.num t
    rw [e, hV, h0]; lib_int
  · calc x = normQ x.num x.den x.den_pos := self_norm x
      _ = normQ (SqClass.cls s1 a1 b1 c1 d1 * (U * U)) (1 * (t * t)) (mul_pos' (Nat.succ_pos 0) (mul_pos' ht ht)) :=
          (normQ_eq_iff _ _ _ _ _ _).2 (by rw [hq, hU]; lib_int)
      _ = ratR.mul (normQ (SqClass.cls s1 a1 b1 c1 d1) 1 (Nat.succ_pos 0)) (normQ (U * U) (t * t) (mul_pos' ht ht)) :=
          (rmul _ _ _ _ _ _).symm
      _ = ratR.mul (castQ (SqClass.cls s1 a1 b1 c1 d1)) (ratR.mul (normQ U t ht) (normQ U t ht)) :=
          congrArg (ratR.mul _) (rmul U U t t ht ht).symm
  · calc LibRing.sub ratR x nQ
        = normQ (x.num * Int.ofNat 1 + -42486 * Int.ofNat x.den) (x.den * 1) (mul_pos' x.den_pos one_pos') := e_n x
      _ = normQ (SqClass.cls s2 a2 b2 c2 d2 * (V * V)) (1 * (t * t)) (mul_pos' (Nat.succ_pos 0) (mul_pos' ht ht)) := by
          apply (normQ_eq_iff _ _ _ _ _ _).2
          have e : x.num * Int.ofNat 1 + -42486 * Int.ofNat x.den = x.num - 42486 * (Int.ofNat t * Int.ofNat t) := by
            rw [hq]; exact sub_sq1 42486 x.num t
          rw [e, hV, hq]; lib_int
      _ = ratR.mul (normQ (SqClass.cls s2 a2 b2 c2 d2) 1 (Nat.succ_pos 0)) (normQ (V * V) (t * t) (mul_pos' ht ht)) :=
          (rmul _ _ _ _ _ _).symm
      _ = ratR.mul (castQ (SqClass.cls s2 a2 b2 c2 d2)) (ratR.mul (normQ V t ht) (normQ V t ht)) :=
          congrArg (ratR.mul _) (rmul V V t t ht ht).symm


theorem q_cls (x y : Q) (h : OnE x y) (hx0 : x.num ≠ 0)
    (hxn : x.num - 42486 * Int.ofNat x.den ≠ 0) :
    ∃ s1 a1 b1 c1 d1 s2 a2 b2 c2 d2 : Bool, HasCls x s1 a1 b1 c1 d1 s2 a2 b2 c2 d2 := by
  obtain ⟨_, _, _, _, _, _, s1, a1, b1, c1, d1, s2, a2, b2, c2, d2, _, _, _, _, _, _, hc⟩ := int_data x y h hx0 hxn
  exact ⟨s1, a1, b1, c1, d1, s2, a2, b2, c2, d2, hc⟩

end QCls

#print axioms QCls.normQ_ne_zero
#print axioms QCls.sub_sq
#print axioms QCls.sub_sq1
#print axioms QCls.gen3
#print axioms QCls.int_data
#print axioms QCls.q_cls

bsd/QDec.lean

lean_all/bsd/QDec.lean

/-
  bsd/QDec.lean — T39 (goal G289e-7b4): a Boolean equality test on ℚ (lib/RatBase.Q) with hand proofs, for case
  splits in the point addition without decidability instances.
    nbeq, ibeq, qbeq        structural tests on Nat, Int, Q (numerator and denominator)
    nbeq_true/false/self    nbeq a b = true ⇒ a = b; = false ⇒ a ≠ b; nbeq a a = true
    ibeq_true/false/self    the same on Int
    q_ext                   equal numerators and denominators ⇒ equal rationals
    qbeq_true/false/self    the same on Q
  Lean 4 core plus lib/RatBase.
-/
import lib.RatBase

namespace QDec
open LibRat

def nbeq : Nat → Nat → Bool
  | 0, 0 => true
  | 0, _ + 1 => false
  | _ + 1, 0 => false
  | a + 1, b + 1 => nbeq a b

theorem nbeq_true : ∀ a b : Nat, nbeq a b = true → a = b
  | 0, 0, _ => rfl
  | 0, _ + 1, h => Bool.noConfusion h
  | _ + 1, 0, h => Bool.noConfusion h
  | a + 1, b + 1, h => congrArg Nat.succ (nbeq_true a b h)

theorem nbeq_false : ∀ a b : Nat, nbeq a b = false → a ≠ b
  | 0, 0, h => Bool.noConfusion h
  | 0, _ + 1, _ => fun e => Nat.noConfusion e
  | _ + 1, 0, _ => fun e => Nat.noConfusion e
  | a + 1, b + 1, h => fun e => nbeq_false a b h (Nat.succ.inj e)

theorem nbeq_self : ∀ a : Nat, nbeq a a = true
  | 0 => rfl
  | a + 1 => nbeq_self a

def ibeq : Int → Int → Bool
  | Int.ofNat a, Int.ofNat b => nbeq a b
  | Int.ofNat _, Int.negSucc _ => false
  | Int.negSucc _, Int.ofNat _ => false
  | Int.negSucc a, Int.negSucc b => nbeq a b

theorem ibeq_true : ∀ a b : Int, ibeq a b = true → a = b
  | Int.ofNat a, Int.ofNat b, h => congrArg Int.ofNat (nbeq_true a b h)
  | Int.ofNat _, Int.negSucc _, h => Bool.noConfusion h
  | Int.negSucc _, Int.ofNat _, h => Bool.noConfusion h
  | Int.negSucc a, Int.negSucc b, h => congrArg Int.negSucc (nbeq_true a b h)

theorem ibeq_false : ∀ a b : Int, ibeq a b = false → a ≠ b
  | Int.ofNat a, Int.ofNat b, h => fun e => nbeq_false a b h (Int.ofNat.inj e)
  | Int.ofNat _, Int.negSucc _, _ => fun e => Int.noConfusion e
  | Int.negSucc _, Int.ofNat _, _ => fun e => Int.noConfusion e
  | Int.negSucc a, Int.negSucc b, h => fun e => nbeq_false a b h (Int.negSucc.inj e)

theorem ibeq_self : ∀ a : Int, ibeq a a = true
  | Int.ofNat a => nbeq_self a
  | Int.negSucc a => nbeq_self a

theorem q_ext : ∀ a b : Q, a.num = b.num → a.den = b.den → a = b
  | ⟨n1, d1, p1, r1⟩, ⟨n2, d2, p2, r2⟩, hn, hd => by
    cases hn; cases hd; rfl

def qbeq (a b : Q) : Bool := ibeq a.num b.num && nbeq a.den b.den

theorem and_l : ∀ {a b : Bool}, (a && b) = true → a = true
  | true, _, _ => rfl
  | false, _, h => Bool.noConfusion h

theorem and_r : ∀ {a b : Bool}, (a && b) = true → b = true
  | _, true, _ => rfl
  | true, false, h => Bool.noConfusion h
  | false, false, h => Bool.noConfusion h

theorem and_tt : ∀ {a b : Bool}, a = true → b = true → (a && b) = true
  | true, true, _, _ => rfl
  | false, _, h, _ => Bool.noConfusion h
  | true, false, _, h => Bool.noConfusion h

theorem qbeq_self (a : Q) : qbeq a a = true := and_tt (ibeq_self _) (nbeq_self _)

theorem qbeq_true (a b : Q) (h : qbeq a b = true) : a = b :=
  q_ext a b (ibeq_true _ _ (and_l h)) (nbeq_true _ _ (and_r h))

theorem qbeq_false (a b : Q) (h : qbeq a b = false) : a ≠ b := fun e => by
  cases e
  exact Bool.noConfusion ((qbeq_self a).symm.trans h)

end QDec

#print axioms QDec.nbeq_true
#print axioms QDec.nbeq_false
#print axioms QDec.nbeq_self
#print axioms QDec.ibeq_true
#print axioms QDec.ibeq_false
#print axioms QDec.ibeq_self
#print axioms QDec.q_ext
#print axioms QDec.and_l
#print axioms QDec.and_r
#print axioms QDec.and_tt
#print axioms QDec.qbeq_true
#print axioms QDec.qbeq_false
#print axioms QDec.qbeq_self

bsd/QModel.lean

lean_all/bsd/QModel.lean

/-
  bsd/QModel.lean — T39 (goal G289e-6c): rational points of E_n : y² = x(x − n)(x + n), n = 42486, over lib/Rat's Q.
    OnE x y          y² = f(x) with f = (x − 0)(x − n)(x + n) (DescentHomFull.f over ratR)
    eq_int           OnE x y  ⇒  r²·q³ = s²·p(p² − n²q²)   for x = p/q, y = r/s in lowest terms
    q_model          OnE x y  ⇒  x.den = t² and p(p² − n²t⁴) = Y²     (bsd/IntModel)
  Lean 4 core plus lib/RatField, bsd/DescentHomFull, bsd/IntModel.
-/
import bsd.IntModel
import bsd.DescentHomFull
import lib.RatField

namespace QModel
open LibRat LibInt

def nQ : Q := normQ 42486 1 (Nat.succ_pos 0)

/-- the curve y² = x(x − n)(x + n) over Q -/
def OnE (x y : Q) : Prop := ratR.mul y y = DescentHomFull.f ratR zeroQ nQ (negQ nQ) x

theorem rmul (a c : Int) (b d : Nat) (hb : 0 < b) (hd : 0 < d) :
    ratR.mul (normQ a b hb) (normQ c d hd) = normQ (a * c) (b * d) (mul_pos' hb hd) := mul_nn a c b d hb hd

theorem radd (a c : Int) (b d : Nat) (hb : 0 < b) (hd : 0 < d) :
    ratR.add (normQ a b hb) (normQ c d hd) = normQ (a * Int.ofNat d + c * Int.ofNat b) (b * d) (mul_pos' hb hd) :=
  add_nn a c b d hb hd

theorem rneg (a : Int) (b : Nat) (hb : 0 < b) : ratR.neg (normQ a b hb) = normQ (-a) b hb := neg_n a b hb

theorem negQ_r (x : Q) : negQ x = ratR.neg x := rfl
theorem zero_r : zeroQ = normQ 0 1 (Nat.succ_le_succ (Nat.zero_le 0)) := rfl

theorem one_pos' : 0 < 1 := Nat.succ_pos 0

theorem e_zero (x : Q) : LibRing.sub ratR x zeroQ
    = normQ (x.num * Int.ofNat 1 + -0 * Int.ofNat x.den) (x.den * 1) (mul_pos' x.den_pos one_pos') :=
  calc LibRing.sub ratR x zeroQ = ratR.add x (ratR.neg (normQ 0 1 one_pos')) := rfl
    _ = ratR.add (normQ x.num x.den x.den_pos) (ratR.neg (normQ 0 1 one_pos')) := congrArg (fun t => ratR.add t _) (self_norm x)
    _ = ratR.add (normQ x.num x.den x.den_pos) (normQ (-0) 1 one_pos') := congrArg (ratR.add _) (rneg 0 1 one_pos')
    _ = _ := radd x.num (-0) x.den 1 x.den_pos one_pos'

/-- x − N for a symbolic integer N -/
theorem e_N (x : Q) (N : Int) : LibRing.sub ratR x (normQ N 1 one_pos')
    = normQ (x.num * Int.ofNat 1 + -N * Int.ofNat x.den) (x.den * 1) (mul_pos' x.den_pos one_pos') :=
  calc LibRing.sub ratR x (normQ N 1 one_pos') = ratR.add x (ratR.neg (normQ N 1 one_pos')) := rfl
    _ = ratR.add (normQ x.num x.den x.den_pos) (ratR.neg (normQ N 1 one_pos')) := congrArg (fun t => ratR.add t _) (self_norm x)
    _ = ratR.add (normQ x.num x.den x.den_pos) (normQ (-N) 1 one_pos') := congrArg (ratR.add _) (rneg N 1 one_pos')
    _ = _ := radd x.num (-N) x.den 1 x.den_pos one_pos'

theorem e_n (x : Q) : LibRing.sub ratR x nQ
    = normQ (x.num * Int.ofNat 1 + -42486 * Int.ofNat x.den) (x.den * 1) (mul_pos' x.den_pos one_pos') :=
  e_N x 42486

theorem negQ_nQ : negQ nQ = normQ (-42486) 1 one_pos' := rneg 42486 1 one_pos'

theorem e_mn (x : Q) : LibRing.sub ratR x (negQ nQ)
    = normQ (x.num * Int.ofNat 1 + - -42486 * Int.ofNat x.den) (x.den * 1) (mul_pos' x.den_pos one_pos') :=
  (congrArg (LibRing.sub ratR x) negQ_nQ).trans (e_N x (-42486))

theorem e_yy (y : Q) : ratR.mul y y = normQ (y.num * y.num) (y.den * y.den) (mul_pos' y.den_pos y.den_pos) :=
  calc ratR.mul y y = ratR.mul (normQ y.num y.den y.den_pos) (normQ y.num y.den y.den_pos) :=
        congr (congrArg ratR.mul (self_norm y)) (self_norm y)
    _ = _ := rmul y.num y.num y.den y.den y.den_pos y.den_pos

/-- the integer identity with a symbolic N (lib_int never sees a large coefficient) -/
theorem int_id (N p q r s P Q' : Int)
    (h3 : r * r * Q' = (p * 1 + -0 * q) * (p * 1 + -N * q) * (p * 1 + - -N * q) * P)
    (hQ : Q' = q * 1 * (q * 1) * (q * 1)) (hP : P = s * s) :
    r * r * (q * q * q) = s * s * (p * (p * p - N * N * (q * q))) := by
  rw [hQ, hP] at h3
  exact of_lin h3 (c := 1) (by lib_int)

theorem eq_int (x y : Q) (h : OnE x y) :
    y.num * y.num * (Int.ofNat x.den * Int.ofNat x.den * Int.ofNat x.den)
      = Int.ofNat y.den * Int.ofNat y.den
        * (x.num * (x.num * x.num - 42486 * 42486 * (Int.ofNat x.den * Int.ofNat x.den))) := by
  have h1 : ratR.mul y y = ratR.mul (ratR.mul (LibRing.sub ratR x zeroQ) (LibRing.sub ratR x nQ))
      (LibRing.sub ratR x (negQ nQ)) := h
  have h2 := ((e_yy y).symm.trans h1).trans
    ((congr (congrArg ratR.mul (congr (congrArg ratR.mul (e_zero x)) (e_n x))) (e_mn x)).trans
      ((congrArg (fun t => ratR.mul t _) (rmul _ _ _ _ _ _)).trans (rmul _ _ _ _ _ _)))
  have h3 := (normQ_eq_iff _ _ _ _ _ _).1 h2
  exact int_id 42486 x.num (Int.ofNat x.den) y.num (Int.ofNat y.den) _ _ h3 (by lib_int) (by lib_int)

/-- **The coprime integral model of a rational point.** -/
theorem q_model (x y : Q) (h : OnE x y) :
    ∃ t : Nat, ∃ Y : Int, x.den = t * t ∧
      Y * Y = x.num * (x.num * x.num - 42486 * 42486 * (Int.ofNat (t * t) * Int.ofNat (t * t))) :=
  IntModel.int_model (42486 * 42486) x.num y.num x.den y.den x.den_pos y.den_pos x.red (eq_int x y h)

end QModel

#print axioms QModel.rmul
#print axioms QModel.radd
#print axioms QModel.rneg
#print axioms QModel.negQ_r
#print axioms QModel.zero_r
#print axioms QModel.one_pos'
#print axioms QModel.e_zero
#print axioms QModel.e_N
#print axioms QModel.e_n
#print axioms QModel.negQ_nQ
#print axioms QModel.e_mn
#print axioms QModel.e_yy
#print axioms QModel.int_id
#print axioms QModel.eq_int
#print axioms QModel.q_model

bsd/QModelN.lean

lean_all/bsd/QModelN.lean

/-
  bsd/QModelN.lean — T39 (goal G289f, first piece): the congruent-number curve E_n : y² = x(x − n)(x + n) over Q
  with n a parameter (bsd/QModel fixes n = 42486).
    OnEn, NDn           the curve and "away from E[2]" for a rational n
    onE_iff, nd_iff     at n = nQ they are QModel.OnE and TransStep.ND
    kernel_n            x = s², x − n = r²  ⇒  x = x(2Q) for a rational point Q (Kernel2.kernel_gen at n)
  Lean 4 core plus bsd/Kernel2, bsd/TransStep.
-/
import bsd.Kernel2
import bsd.TransStep

namespace QModelN
open DetChain LibRing LibRat QModel

def OnEn (n x y : Q) : Prop := ratR.mul y y = DescentHomFull.f ratR zeroQ n (negQ n) x

def NDn (n x : Q) : Prop := x ≠ zeroQ ∧ sub ratR x n ≠ zeroQ ∧ sub ratR x (negQ n) ≠ zeroQ

theorem onE_iff (x y : Q) : OnEn nQ x y ↔ OnE x y := Iff.rfl

theorem nd_iff (x : Q) : NDn nQ x ↔ TransStep.ND x := Iff.rfl

/-- **The trivial class is 2E (x-level), any rational n ≠ 0.** -/
theorem kernel_n (n x y s r : Q) (hn : n ≠ zeroQ) (o : OnEn n x y) (hs : x = ratR.mul s s)
    (hr : sub ratR x n = ratR.mul r r) (hx : x ≠ zeroQ) (hxn : sub ratR x n ≠ zeroQ) :
    ∃ xq yq w : Q, OnEn n xq yq ∧ ratR.mul (TrianglePoint.two ratR yq) w = ratR.one ∧
      DoublingGen.x2 ratR zeroQ n (negQ n) xq w = x :=
  Kernel2.kernel_gen ratR rat_noZD invQ mul_invQ Kernel2.two_one_ne n x y s r hn o hs hr hx hxn

end QModelN

#print axioms QModelN.onE_iff
#print axioms QModelN.nd_iff
#print axioms QModelN.kernel_n

bsd/QModelZ.lean

lean_all/bsd/QModelZ.lean

/-
  bsd/QModelZ.lean — T39 (goal G289f-4h-5a): the coprime integral model of a rational point of E_n for an integer
  parameter N (n = N/1); bsd/QModel is the case N = 42486.
    nz N               the rational number N/1
    e_nz, e_mnz        x − n and x + n as normalised fractions
    eq_intZ            OnEn (nz N) x y  ⇒  r²·q³ = s²·p(p² − N²q²)   for x = p/q, y = r/s in lowest terms
    q_modelZ           OnEn (nz N) x y  ⇒  x.den = t² and p(p² − N²t⁴) = Y²     (bsd/IntModel)
  Lean 4 core plus bsd/QModel, bsd/QModelN.
-/
import bsd.QModel
import bsd.QModelN

namespace QModelZ
open LibRat LibInt QModel QModelN

/-- the integer N as a rational number -/
def nz (N : Int) : Q := normQ N 1 one_pos'

theorem e_nz (x : Q) (N : Int) : LibRing.sub ratR x (nz N)
    = normQ (x.num * Int.ofNat 1 + -N * Int.ofNat x.den) (x.den * 1) (mul_pos' x.den_pos one_pos') :=
  e_N x N

theorem negQ_nz (N : Int) : negQ (nz N) = normQ (-N) 1 one_pos' := rneg N 1 one_pos'

theorem e_mnz (x : Q) (N : Int) : LibRing.sub ratR x (negQ (nz N))
    = normQ (x.num * Int.ofNat 1 + - -N * Int.ofNat x.den) (x.den * 1) (mul_pos' x.den_pos one_pos') :=
  (congrArg (LibRing.sub ratR x) (negQ_nz N)).trans (e_N x (-N))

theorem eq_intZ (N : Int) (x y : Q) (h : OnEn (nz N) x y) :
    y.num * y.num * (Int.ofNat x.den * Int.ofNat x.den * Int.ofNat x.den)
      = Int.ofNat y.den * Int.ofNat y.den
        * (x.num * (x.num * x.num - N * N * (Int.ofNat x.den * Int.ofNat x.den))) := by
  have h1 : ratR.mul y y = ratR.mul (ratR.mul (LibRing.sub ratR x zeroQ) (LibRing.sub ratR x (nz N)))
      (LibRing.sub ratR x (negQ (nz N))) := h
  have h2 := ((e_yy y).symm.trans h1).trans
    ((congr (congrArg ratR.mul (congr (congrArg ratR.mul (e_zero x)) (e_nz x N))) (e_mnz x N)).trans
      ((congrArg (fun t => ratR.mul t _) (rmul _ _ _ _ _ _)).trans (rmul _ _ _ _ _ _)))
  have h3 := (normQ_eq_iff _ _ _ _ _ _).1 h2
  exact int_id N x.num (Int.ofNat x.den) y.num (Int.ofNat y.den) _ _ h3 (by lib_int) (by lib_int)

/-- **The coprime integral model of a rational point of E_N.** -/
theorem q_modelZ (N : Int) (x y : Q) (h : OnEn (nz N) x y) :
    ∃ t : Nat, ∃ Y : Int, x.den = t * t ∧
      Y * Y = x.num * (x.num * x.num - N * N * (Int.ofNat (t * t) * Int.ofNat (t * t))) :=
  IntModel.int_model (N * N) x.num y.num x.den y.den x.den_pos y.den_pos x.red (eq_intZ N x y h)

end QModelZ

#print axioms QModelZ.e_nz
#print axioms QModelZ.negQ_nz
#print axioms QModelZ.e_mnz
#print axioms QModelZ.eq_intZ
#print axioms QModelZ.q_modelZ

bsd/QuadShift.lean

lean_all/bsd/QuadShift.lean

/-
  bsd/QuadShift.lean — T39 (goal G289e-5, piece a): a binary quadratic form at X = p·a + x, Z = p·b + z differs
  from any representative with coefficients shifted by multiples of p by an explicit multiple of p:
    quad_shift   Q(pa + x, pb + z) − Q′(x, z) = p·K,   Q = (α, β, γ),  Q′ = (α + p·kα, β + p·kβ, γ + p·kγ),
  so G(X, Z) is congruent mod p to the residue polynomial of the tables in bsd/Res97.
  Lean 4 core plus lib/Gcd (IDv), lib/IntRing (lib_int).
-/
import lib.Gcd

namespace QuadShift
open LibGcd LibInt

def Q (α β γ X Z : Int) : Int := α * X * X + β * X * Z + γ * Z * Z

/-- **Q(pa + x, pb + z) − Q′(x, z) = p·K** -/
theorem quad_shift (p α β γ ka kb kc a b x z : Int) :
    Q α β γ (p * a + x) (p * b + z) - Q (α + p * ka) (β + p * kb) (γ + p * kc) x z
      = p * (2 * α * a * x + p * α * a * a + β * (a * z + b * x) + p * β * a * b + 2 * γ * b * z + p * γ * b * b
          - ka * x * x - kb * x * z - kc * z * z) := by
  show α * (p * a + x) * (p * a + x) + β * (p * a + x) * (p * b + z) + γ * (p * b + z) * (p * b + z)
      - ((α + p * ka) * x * x + (β + p * kb) * x * z + (γ + p * kc) * z * z)
    = p * (2 * α * a * x + p * α * a * a + β * (a * z + b * x) + p * β * a * b + 2 * γ * b * z + p * γ * b * b
          - ka * x * x - kb * x * z - kc * z * z)
  lib_int

/-- p ∣ Q(pa + x, pb + z) − Q′(x, z) -/
theorem quad_cong (p α β γ ka kb kc a b x z : Int) :
    IDv p (Q α β γ (p * a + x) (p * b + z) - Q (α + p * ka) (β + p * kb) (γ + p * kc) x z) :=
  ⟨_, quad_shift p α β γ ka kb kc a b x z⟩

end QuadShift

#print axioms QuadShift.quad_shift
#print axioms QuadShift.quad_cong

bsd/QuartGen.lean

lean_all/bsd/QuartGen.lean

/-
  bsd/QuartGen.lean — T39 (goal G289e-6d): generic identities for binary quartics with symbolic coefficients (so lib_int
  never meets large numerals; the numeric instances are matched by rfl).
    q4_negX       Q4(a, b, c, d, e)(−X, Z) = Q4(a, −b, c, −d, e)(X, Z)
    q4_negZ       Q4(a, b, c, d, e)(X, −Z) = Q4(a, −b, c, −d, e)(X, Z)
    q4_smul       k·Q4(a, b, c, d, e)(X, Z) = Q4(ka, kb, kc, kd, ke)(X, Z)
    q4_circ       D·(b₀(X² − Z²) − 2c₀XZ)² − N(X² + Z²)² = Q4(Db₀² − N, −4Db₀c₀, D(4c₀² − 2b₀²) − 2N, 4Db₀c₀, Db₀² − N)(X, Z)
    q4_prod       k·(αX² + βXZ + γZ²)(δX² + εXZ + ζZ²) = Q4(kαδ, k(αε + βδ), k(αζ + βε + γδ), k(βζ + γε), kγζ)(X, Z)
    sq_scale      (2·m·a·X)² = 4·(m·a·X)²
  Lean 4 core plus bsd/CopReduce (Q4).
-/
import bsd.CopReduce

namespace QuartGen
open LibInt CopReduce

theorem q4_negX (a b c d e X Z : Int) : Q4 a b c d e (-X) Z = Q4 a (-b) c (-d) e X Z := by unfold Q4; lib_int
theorem q4_negZ (a b c d e X Z : Int) : Q4 a b c d e X (-Z) = Q4 a (-b) c (-d) e X Z := by unfold Q4; lib_int
theorem q4_smul (k a b c d e X Z : Int) : k * Q4 a b c d e X Z = Q4 (k * a) (k * b) (k * c) (k * d) (k * e) X Z := by
  unfold Q4; lib_int

theorem q4_circ (D N b0 c0 X Z : Int) :
    D * ((b0 * (X * X - Z * Z) - 2 * c0 * (X * Z)) * (b0 * (X * X - Z * Z) - 2 * c0 * (X * Z)))
      - N * ((X * X + Z * Z) * (X * X + Z * Z))
      = Q4 (D * b0 * b0 - N) (-4 * D * b0 * c0) (D * (4 * c0 * c0 - 2 * b0 * b0) - 2 * N) (4 * D * b0 * c0)
          (D * b0 * b0 - N) X Z := by
  unfold Q4; lib_int

theorem q4_prod (k α β γ δ ε ζ X Z : Int) :
    k * (α * X * X + β * X * Z + γ * Z * Z) * (δ * X * X + ε * X * Z + ζ * Z * Z)
      = Q4 (k * α * δ) (k * (α * ε + β * δ)) (k * (α * ζ + β * ε + γ * δ)) (k * (β * ζ + γ * ε)) (k * γ * ζ) X Z := by
  unfold Q4; lib_int

theorem sq_scale (m a X : Int) : (2 * m * a * X) * (2 * m * a * X) = 4 * ((m * a * X) * (m * a * X)) := by lib_int

/-- linear times quadratic, plus linear times quadratic, as a binary cubic -/
theorem cub_lin (p q a b c r s d e f X Z : Int) :
    (p * X + q * Z) * (a * X * X + b * X * Z + c * Z * Z) + (r * X + s * Z) * (d * X * X + e * X * Z + f * Z * Z)
      = (p * a + r * d) * (X * X * X) + (p * b + q * a + r * e + s * d) * (X * X * Z)
        + (p * c + q * b + r * f + s * e) * (X * Z * Z) + (q * c + s * f) * (Z * Z * Z) := by lib_int

end QuartGen

#print axioms QuartGen.q4_negX
#print axioms QuartGen.q4_negZ
#print axioms QuartGen.q4_smul
#print axioms QuartGen.q4_circ
#print axioms QuartGen.q4_prod
#print axioms QuartGen.sq_scale
#print axioms QuartGen.cub_lin

bsd/Rank2Cert.lean

lean_all/bsd/Rank2Cert.lean

/-
  bsd/Rank2Cert.lean — T39 cycle 263 (goal G13): rank ≥ 2 for all 8 rank-2 congruent curves in 801–1000, from two points
  whose 2-descent images are independent of each other and of the torsion images.

  Lean 4 core plus bsd/CongruentPoints5 (onC), bsd/DescentCert (isSqI), rfl. Each row (n, P₁, P₂), P = (±a/b², c/b³),
  found by bsd_rank2_points.py (a2_r2pts, NUMERIC_BSD_RANK2PTS.jsonl). Checked:
    on_curve   both points lie on y² = x³ − n²x
    indep      for each of δ(P₁), δ(P₂), δ(P₁)δ(P₂) and each torsion image (u, v): X·u or Y·v is not a square
               (X = ±a, Y = X − nb²; products coordinatewise) — the images span a group of order 16 in (Q*/Q*²)²,
               so E/2E has order ≥ 16 = 2^{r+2} and r ≥ 2 (DescentHomFull, TorsionShift, TorsionImage; tors = E[2] cited).
  n = 866: second point x = 2·2881²/73² from the C search bsd_r2search.c (a2_r2s866, u, v ≤ 10⁴).
-/
import bsd.CongruentPoints5
import bsd.DescentCert

namespace Rank2Cert
open CongruentPoints5 DescentCert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) :=
  [(866, (false, 578, 1, 15504), (true, 16600322, 73, 64969361856)),
   (889, (true, 436921, 17, 233597400), (true, 112903, 1, 37935408)),
   (890, (true, 1338649, 22, 1466438493), (true, 13690, 1, 1598400)),
   (905, (true, 32761, 6, 622459), (false, 906304, 53, 2261098056)),
   (915, (true, 3721, 2, 40931), (true, 960, 1, 9000)),
   (985, (false, 99225, 13, 42102900), (false, 166464, 13, 235416)),
   (987, (true, 271803, 13, 111881700), (true, 3388, 1, 188650)),
   (995, (true, 198025, 2, 88103325), (true, 2205, 1, 92400))]

theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl

def img (n : Nat) (p : Bool × Nat × Nat × Nat) : Int × Int :=
  let X : Int := if p.1 then p.2.1 else - (p.2.1 : Int)
  (X, X - (n : Int) * p.2.2.1 * p.2.2.1)

def nontriv (n : Nat) (xy : Int × Int) : Bool :=
  let m : Int := n
  [((1 : Int), (1 : Int)), (-1, -m), (m, 2), (-m, -2 * m)].all (fun (u, v) => !(isSqI (xy.1 * u)) || !(isSqI (xy.2 * v)))

def indepRow (r : Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) : Bool :=
  let a := img r.1 r.2.1; let b := img r.1 r.2.2
  nontriv r.1 a && nontriv r.1 b && nontriv r.1 (a.1 * b.1, a.2 * b.2)

theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 8 := rfl

end Rank2Cert

#print axioms Rank2Cert.on_curve
#print axioms Rank2Cert.indep
#print axioms Rank2Cert.count

bsd/Rank2Cert10.lean

lean_all/bsd/Rank2Cert10.lean

/-
  bsd/Rank2Cert10.lean — T39 cycle 333 (goal G68): rank ≥ 2 for 5 of the 5 congruent curves in 2601–2800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert10
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (2611, (true, 139129, 6, 38260475), (false, 668367, 16, 6616617)),
   (2698, (true, 6498, 1, 476520), (true, 357446582, 79, 6750474851520)),
   (2706, (true, 19602, 1, 2718144), (true, 55473, 2, 12814263)),
   (2730, (true, 3150, 1, 88200), (true, 11466, 1, 1192464)),
   (2777, (true, 120495765625, 6424, 12922792024594125), (true, 2925572863759225, 819007, 122019707400536910179160))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 5 := rfl

end Rank2Cert10

#print axioms Rank2Cert10.on_curve
#print axioms Rank2Cert10.indep
#print axioms Rank2Cert10.count

bsd/Rank2Cert100.lean

lean_all/bsd/Rank2Cert100.lean

/-
  bsd/Rank2Cert100.lean — T39 cycle 451 (goal G168): rank ≥ 2 for 1 of the 5 congruent curves in 20601–20800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert100
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (20651, (false, 5038523, 41, 77096924856), (true, 6602050009, 504, 325720762199965))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert100

#print axioms Rank2Cert100.on_curve
#print axioms Rank2Cert100.indep
#print axioms Rank2Cert100.count

bsd/Rank2Cert101.lean

lean_all/bsd/Rank2Cert101.lean

/-
  bsd/Rank2Cert101.lean — T39 cycle 452 (goal G169): rank ≥ 2 for 4 of the 6 congruent curves in 20801–21000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert101
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (20922, (true, 697686, 5, 385662816), (true, 3136716, 5, 5477587236)),
   (20930, (false, 14, 3, 704816), (true, 2579850, 11, 790007400)),
   (20945, (true, 1640384, 7, 1638970248), (true, 90345025, 38, 809181418425)),
   (20962, (true, 19460350, 17, 81581550960), (false, 3177108206, 1155, 1565998810741072))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert101

#print axioms Rank2Cert101.on_curve
#print axioms Rank2Cert101.indep
#print axioms Rank2Cert101.count

bsd/Rank2Cert102.lean

lean_all/bsd/Rank2Cert102.lean

/-
  bsd/Rank2Cert102.lean — T39 cycle 453 (goal G170): rank ≥ 2 for 6 of the 10 congruent curves in 21001–21200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert102
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (21009, (true, 34263, 1, 5010012), (true, 906185611908889, 84623, 26900257023329079073060)),
   (21034, (true, 12703626, 17, 39758169360), (true, 86029056784225, 44398, 699149242537764378255)),
   (21041, (true, 59740424431249, 30805, 435191633204466088932), (true, 3506814277201, 6560, 6344348675930944551)),
   (21049, (true, 14292271, 11, 53167248120), (true, 11377129, 23, 7876224840)),
   (21090, (true, 4942090, 13, 7610818600), (true, 53946, 1, 11532456)),
   (21170, (false, 3069504, 97, 348936866256), (true, 772705, 6, 112042225))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 6 := rfl

end Rank2Cert102

#print axioms Rank2Cert102.on_curve
#print axioms Rank2Cert102.indep
#print axioms Rank2Cert102.count

bsd/Rank2Cert103.lean

lean_all/bsd/Rank2Cert103.lean

/-
  bsd/Rank2Cert103.lean — T39 cycle 454 (goal G171): rank ≥ 2 for 1 of the 6 congruent curves in 21201–21400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert103
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (21385, (true, 54665, 1, 11762400), (true, 562185, 1, 421215600))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert103

#print axioms Rank2Cert103.on_curve
#print axioms Rank2Cert103.indep
#print axioms Rank2Cert103.count

bsd/Rank2Cert104.lean

lean_all/bsd/Rank2Cert104.lean

/-
  bsd/Rank2Cert104.lean — T39 cycle 455 (goal G172): rank ≥ 2 for 1 of the 5 congruent curves in 21401–21600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert104
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (21585, (true, 21660, 1, 265050), (true, 93615, 1, 27871200))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert104

#print axioms Rank2Cert104.on_curve
#print axioms Rank2Cert104.indep
#print axioms Rank2Cert104.count

bsd/Rank2Cert105.lean

lean_all/bsd/Rank2Cert105.lean

/-
  bsd/Rank2Cert105.lean — T39 cycle 456 (goal G173): rank ≥ 2 for 6 of the 9 congruent curves in 21601–21800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert105
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (21641, (true, 347633, 4, 18225207), (true, 1425591049, 208, 40590033629595)),
   (21714, (true, 99372, 1, 30568356), (true, 874764, 1, 817904340)),
   (21715, (true, 123725, 1, 42844200), (true, 133456510489, 598, 48671329657235637)),
   (21754, (true, 2035897, 6, 2681398259), (true, 1349428399201, 3312, 1542857975609628655)),
   (21755, (true, 1050625, 2, 1073190375), (false, 324900, 113, 158339960850)),
   (21777, (true, 26775, 1, 2548980), (true, 152841120601, 655, 59641340992944724))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 6 := rfl

end Rank2Cert105

#print axioms Rank2Cert105.on_curve
#print axioms Rank2Cert105.indep
#print axioms Rank2Cert105.count

bsd/Rank2Cert106.lean

lean_all/bsd/Rank2Cert106.lean

/-
  bsd/Rank2Cert106.lean — T39 cycle 457 (goal G174): rank ≥ 2 for 2 of the 5 congruent curves in 21801–22000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert106
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (21835, (false, 14170475, 27, 27293957900), (true, 37073577025, 476, 7074488131026975)),
   (21945, (true, 43120, 1, 7707700), (true, 69935580, 23, 576739899450))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert106

#print axioms Rank2Cert106.on_curve
#print axioms Rank2Cert106.indep
#print axioms Rank2Cert106.count

bsd/Rank2Cert107.lean

lean_all/bsd/Rank2Cert107.lean

/-
  bsd/Rank2Cert107.lean — T39 cycle 458 (goal G175): rank ≥ 2 for 1 of the 8 congruent curves in 22001–22200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert107
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (22106, (false, 185585407, 116, 3166851118977), (true, 216620330437, 531, 100778748568376291))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert107

#print axioms Rank2Cert107.on_curve
#print axioms Rank2Cert107.indep
#print axioms Rank2Cert107.count

bsd/Rank2Cert108.lean

lean_all/bsd/Rank2Cert108.lean

/-
  bsd/Rank2Cert108.lean — T39 cycle 459 (goal G176): rank ≥ 2 for 7 of the 12 congruent curves in 22201–22400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert108
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (22219, (false, 24531083, 943, 97860159811176), (true, 38421650275, 1299, 1646357020501240)),
   (22265, (false, 3254416, 13, 3407281548), (true, 46104603065, 1189, 7233427748878500)),
   (22274, (false, 6727, 2, 7286643), (true, 65744893, 27, 516566107513)),
   (22321, (true, 1723969, 8, 1267117215), (true, 173417, 2, 61909869)),
   (22330, (true, 1854405, 1, 2525081475), (true, 31581, 1, 3968679)),
   (22345, (true, 93025, 2, 7864425), (true, 58331727025441, 18422, 441729091152214615689)),
   (22386, (true, 70602, 1, 17791704), (true, 172302, 1, 70915104))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 7 := rfl

end Rank2Cert108

#print axioms Rank2Cert108.on_curve
#print axioms Rank2Cert108.indep
#print axioms Rank2Cert108.count

bsd/Rank2Cert109.lean

lean_all/bsd/Rank2Cert109.lean

/-
  bsd/Rank2Cert109.lean — T39 cycle 460 (goal G177): rank ≥ 2 for 2 of the 4 congruent curves in 22401–22600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert109
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (22490, (true, 610090, 3, 449540000), (false, 3124810, 19, 13246287600)),
   (22586, (false, 947807, 24, 12631825423), (true, 1933150, 9, 868427560))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert109

#print axioms Rank2Cert109.on_curve
#print axioms Rank2Cert109.indep
#print axioms Rank2Cert109.count

bsd/Rank2Cert11.lean

lean_all/bsd/Rank2Cert11.lean

/-
  bsd/Rank2Cert11.lean — T39 cycle 334 (goal G69): rank ≥ 2 for 9 of the 10 congruent curves in 2801–3000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert11
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (2810, (true, 78961, 4, 18239991), (true, 70278010, 89, 558825498000)),
   (2818, (true, 1441602, 17, 1428221760), (true, 280654704699846135409, 283788890, 2765999920143882629428621145577)),
   (2833, (true, 37953532961722225, 3641969, 1039297286285335157349360), (true, 2861477172449257080625, 1003647528, 11274387813303264972410700649175)),
   (2849, (true, 231361, 6, 99752185), (true, 3388, 1, 106722)),
   (2865, (true, 93025, 3, 27260900), (true, 4335, 1, 214200)),
   (2914, (true, 4923522, 41, 1101463104), (true, 53687536, 45, 390994820216)),
   (2929, (true, 243049, 9, 26020540), (true, 122758436161, 6003, 21961577651593060)),
   (2945, (true, 21025, 2, 2525175), (true, 44764, 3, 7632262)),
   (2995, (true, 4205, 1, 191400), (true, 144752781255625, 133342, 1619438675769460960125))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 9 := rfl

end Rank2Cert11

#print axioms Rank2Cert11.on_curve
#print axioms Rank2Cert11.indep
#print axioms Rank2Cert11.count

bsd/Rank2Cert110.lean

lean_all/bsd/Rank2Cert110.lean

/-
  bsd/Rank2Cert110.lean — T39 cycle 461 (goal G178): rank ≥ 2 for 3 of the 5 congruent curves in 22601–22800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert110
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (22649, (true, 2100464, 5, 2931502812), (false, 284874975, 206, 15493293893385)),
   (22705, (true, 5426495, 7, 12372408600), (true, 103111128553225, 4262, 1047019659294511674525)),
   (22715, (false, 996611, 1257, 35829944423812), (true, 26019, 1, 2046828))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert110

#print axioms Rank2Cert110.on_curve
#print axioms Rank2Cert110.indep
#print axioms Rank2Cert110.count

bsd/Rank2Cert111.lean

lean_all/bsd/Rank2Cert111.lean

/-
  bsd/Rank2Cert111.lean — T39 cycle 462 (goal G179): rank ≥ 2 for 4 of the 6 congruent curves in 22801–23000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert111
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (22810, (false, 14400, 1, 2122800), (true, 3831610, 1, 7500051600)),
   (22865, (true, 1809025, 7, 1910330400), (true, 9118149121, 297, 849115564314344)),
   (22890, (true, 2851440, 7, 4426860600), (true, 189525, 1, 81904725)),
   (22945, (false, 20736, 1, 1414512), (true, 59636082025, 627, 14395870576341800))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert111

#print axioms Rank2Cert111.on_curve
#print axioms Rank2Cert111.indep
#print axioms Rank2Cert111.count

bsd/Rank2Cert112.lean

lean_all/bsd/Rank2Cert112.lean

/-
  bsd/Rank2Cert112.lean — T39 cycle 463 (goal G180): rank ≥ 2 for 2 of the 6 congruent curves in 23001–23200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert112
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (23001, (true, 485809, 4, 221043095), (true, 1730553, 1, 2276349768)),
   (23010, (true, 34810, 1, 4873400), (true, 473850, 1, 325798200))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert112

#print axioms Rank2Cert112.on_curve
#print axioms Rank2Cert112.indep
#print axioms Rank2Cert112.count

bsd/Rank2Cert113.lean

lean_all/bsd/Rank2Cert113.lean

/-
  bsd/Rank2Cert113.lean — T39 cycle 464 (goal G181): rank ≥ 2 for 3 of the 8 congruent curves in 23201–23400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert113
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (23218, (true, 70356650, 23, 581081602920), (false, 765123246, 545, 189580659910992)),
   (23329, (false, 1089329, 7, 361683960), (false, 1212944, 15, 5624484796)),
   (23385, (true, 4406460, 11, 7090782750), (true, 14263937985, 778, 210558462946425))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert113

#print axioms Rank2Cert113.on_curve
#print axioms Rank2Cert113.indep
#print axioms Rank2Cert113.count

bsd/Rank2Cert114.lean

lean_all/bsd/Rank2Cert114.lean

/-
  bsd/Rank2Cert114.lean — T39 cycle 465 (goal G182): rank ≥ 2 for 4 of the 5 congruent curves in 23401–23600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert114
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (23457, (false, 86247, 2, 10850805), (true, 62573303981299225, 1194, 15652497051620590061207285)),
   (23506, (true, 1421298, 1, 1694214192), (true, 62192, 1, 14359176)),
   (23529, (true, 7161372, 7, 18914377014), (true, 15190000, 11, 58152896900)),
   (23585, (false, 1782225, 389, 4764488628900), (true, 55625, 1, 11881500))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert114

#print axioms Rank2Cert114.on_curve
#print axioms Rank2Cert114.indep
#print axioms Rank2Cert114.count

bsd/Rank2Cert115.lean

lean_all/bsd/Rank2Cert115.lean

/-
  bsd/Rank2Cert115.lean — T39 cycle 466 (goal G183): rank ≥ 2 for 2 of the 7 congruent curves in 23601–23800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert115
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (23705, (true, 931225, 2, 893961525), (true, 1533895, 3, 1881271700)),
   (23730, (false, 160286, 5, 228678688), (true, 41070, 1, 6793200))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert115

#print axioms Rank2Cert115.on_curve
#print axioms Rank2Cert115.indep
#print axioms Rank2Cert115.count

bsd/Rank2Cert116.lean

lean_all/bsd/Rank2Cert116.lean

/-
  bsd/Rank2Cert116.lean — T39 cycle 467 (goal G184): rank ≥ 2 for 4 of the 9 congruent curves in 23801–24000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert116
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (23834, (false, 913051098, 1223, 1076849224611504), (true, 21828345066, 445, 3148722307572864)),
   (23874, (true, 179574, 1, 75421080), (false, 244007, 18, 3819048317)),
   (23938, (false, 146882, 17, 2650770240), (true, 32134044704842369, 183040, 5758545250123995783459903)),
   (23970, (true, 66270, 1, 15904800), (true, 32674, 1, 4013632))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert116

#print axioms Rank2Cert116.on_curve
#print axioms Rank2Cert116.indep
#print axioms Rank2Cert116.count

bsd/Rank2Cert117.lean

lean_all/bsd/Rank2Cert117.lean

/-
  bsd/Rank2Cert117.lean — T39 cycle 468 (goal G185): rank ≥ 2 for 2 of the 2 congruent curves in 24001–24200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert117
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (24081, (true, 60809148, 11, 473646380562), (false, 158447, 24, 5520910087)),
   (24161, (true, 224090271426025, 18031, 3352490227184911215060), (true, 158060439139681, 80475, 281007379810805352796))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert117

#print axioms Rank2Cert117.on_curve
#print axioms Rank2Cert117.indep
#print axioms Rank2Cert117.count

bsd/Rank2Cert118.lean

lean_all/bsd/Rank2Cert118.lean

/-
  bsd/Rank2Cert118.lean — T39 cycle 469 (goal G186): rank ≥ 2 for 5 of the 8 congruent curves in 24201–24400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert118
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (24211, (false, 20339, 1, 1873080), (false, 60499879, 50, 14221282581)),
   (24297, (false, 275625, 13, 2150889300), (true, 24375, 1, 304200)),
   (24305, (true, 477204025, 62, 10222778763675), (true, 22797231497725441, 322710, 3420815365042188622403239)),
   (24361, (false, 518400, 13, 2940653520), (true, 355671061721852881, 3754723, 55147883342540502864450360)),
   (24395, (true, 1050625, 6, 591081625), (true, 38556, 1, 5862654))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 5 := rfl

end Rank2Cert118

#print axioms Rank2Cert118.on_curve
#print axioms Rank2Cert118.indep
#print axioms Rank2Cert118.count

bsd/Rank2Cert119.lean

lean_all/bsd/Rank2Cert119.lean

/-
  bsd/Rank2Cert119.lean — T39 cycle 470 (goal G187): rank ≥ 2 for 4 of the 10 congruent curves in 24401–24600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert119
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (24409, (true, 100489, 2, 7536675), (false, 3906063, 184, 1633241489481)),
   (24490, (true, 204490, 1, 91806000), (true, 17905600, 27, 5791754800)),
   (24521, (true, 29929, 1, 2968680), (true, 37975, 1, 5650680)),
   (24530, (true, 19692684, 19, 78055235148), (true, 919145920, 89, 27236382373200))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert119

#print axioms Rank2Cert119.on_curve
#print axioms Rank2Cert119.indep
#print axioms Rank2Cert119.count

bsd/Rank2Cert12.lean

lean_all/bsd/Rank2Cert12.lean

/-
  bsd/Rank2Cert12.lean — T39 cycle 336 (goal G70): rank ≥ 2 for 9 of the 13 congruent curves in 3001–3200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert12
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (3003, (true, 7203, 1, 555660), (true, 77077, 3, 20040020)),
   (3026, (true, 5202, 1, 305184), (true, 140777, 2, 52624299)),
   (3081, (true, 243399, 5, 113910732), (false, 3079479, 47, 10650832920)),
   (3090, (true, 63654, 1, 16040808), (true, 5198410, 39, 5064736600)),
   (3099, (true, 1067089, 8, 1083095335), (false, 95056923, 193, 638538539184)),
   (3129, (true, 3751969, 33, 3041981020), (true, 54208, 3, 10784312)),
   (3145, (true, 34225, 3, 3559400), (true, 1075369, 15, 839704528)),
   (3161, (false, 30276, 25, 343718382), (true, 5837990841, 1141, 316391180888700)),
   (3171, (false, 15987, 7, 19541808), (true, 638428, 9, 467010082))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 9 := rfl

end Rank2Cert12

#print axioms Rank2Cert12.on_curve
#print axioms Rank2Cert12.indep
#print axioms Rank2Cert12.count

bsd/Rank2Cert120.lean

lean_all/bsd/Rank2Cert120.lean

/-
  bsd/Rank2Cert120.lean — T39 cycle 471 (goal G188): rank ≥ 2 for 1 of the 6 congruent curves in 24601–24800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert120
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (24610, (false, 3726, 5, 37554768), (false, 21234150, 5129, 2983282991299800))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert120

#print axioms Rank2Cert120.on_curve
#print axioms Rank2Cert120.indep
#print axioms Rank2Cert120.count

bsd/Rank2Cert121.lean

lean_all/bsd/Rank2Cert121.lean

/-
  bsd/Rank2Cert121.lean — T39 cycle 472 (goal G189): rank ≥ 2 for 3 of the 9 congruent curves in 24801–25000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert121
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (24817, (true, 7316208, 7, 19513974180), (false, 2347499375, 762, 688844405942225)),
   (24889, (true, 1418053941901249, 23821, 53397089684666465576040), (true, 1393956838411017049, 4554480, 1528747045701029635272936157)),
   (24905, (false, 495616, 13, 2942482752), (true, 50520607606225, 5551, 359048186567682393900))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert121

#print axioms Rank2Cert121.on_curve
#print axioms Rank2Cert121.indep
#print axioms Rank2Cert121.count

bsd/Rank2Cert122.lean

lean_all/bsd/Rank2Cert122.lean

/-
  bsd/Rank2Cert122.lean — T39 cycle 474 (goal G191): rank ≥ 2 for 3 of the 9 congruent curves in 25001–25200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert122
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (25049, (true, 565213013575681, 52438, 13337342391998979769305), (true, 4533343597225, 3116, 9638335582045944555)),
   (25177, (false, 134657, 3, 66874600), (true, 854373379906886401, 4225315, 671589748821833458751496024)),
   (25194, (true, 229957, 3, 18363709), (true, 98838, 1, 30046752))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert122

#print axioms Rank2Cert122.on_curve
#print axioms Rank2Cert122.indep
#print axioms Rank2Cert122.count

bsd/Rank2Cert123.lean

lean_all/bsd/Rank2Cert123.lean

/-
  bsd/Rank2Cert123.lean — T39 cycle 475 (goal G192): rank ≥ 2 for 3 of the 11 congruent curves in 25201–25400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert123
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (25233, (false, 1365952223, 812, 612815874549425), (true, 41755440804025, 39422, 92687114358422273755)),
   (25234, (true, 42284, 1, 6976860), (false, 8435855098, 983, 2101234631543784)),
   (25259, (true, 525625, 2, 373971675), (true, 828269, 5, 487786728))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert123

#print axioms Rank2Cert123.on_curve
#print axioms Rank2Cert123.indep
#print axioms Rank2Cert123.count

bsd/Rank2Cert124.lean

lean_all/bsd/Rank2Cert124.lean

/-
  bsd/Rank2Cert124.lean — T39 cycle 476 (goal G193): rank ≥ 2 for 2 of the 10 congruent curves in 25401–25600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert124
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (25474, (false, 528750, 11, 2208116400), (false, 262437143, 294, 35415842359339)),
   (25530, (false, 1491870, 13, 4944846600), (true, 129214, 1, 45532088))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert124

#print axioms Rank2Cert124.on_curve
#print axioms Rank2Cert124.indep
#print axioms Rank2Cert124.count

bsd/Rank2Cert125.lean

lean_all/bsd/Rank2Cert125.lean

/-
  bsd/Rank2Cert125.lean — T39 cycle 477 (goal G194): rank ≥ 2 for 6 of the 11 congruent curves in 25601–25800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert125
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (25619, (true, 509777, 4, 216387801), (true, 4458774319561, 2884, 9404305526047715205)),
   (25681, (true, 217985338321, 1253, 100018863396314820), (true, 1275514153064281, 2543, 45554147026731150249780)),
   (25690, (false, 18490, 1, 2425200), (true, 458654, 1, 310131192)),
   (25707, (true, 583737, 4, 316468845), (true, 5800377, 8, 13395870675)),
   (25745, (true, 62792055, 49, 87490263300), (true, 236698225, 38, 3596406600225)),
   (25795, (true, 9898245, 19, 10558128000), (true, 29149120, 1, 157375818600))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 6 := rfl

end Rank2Cert125

#print axioms Rank2Cert125.on_curve
#print axioms Rank2Cert125.indep
#print axioms Rank2Cert125.count

bsd/Rank2Cert126.lean

lean_all/bsd/Rank2Cert126.lean

/-
  bsd/Rank2Cert126.lean — T39 cycle 478 (goal G195): rank ≥ 2 for 1 of the 4 congruent curves in 25801–26000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert126
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (25993, (false, 2070617, 9, 548683840), (true, 10241642401, 555, 646319500762024))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert126

#print axioms Rank2Cert126.on_curve
#print axioms Rank2Cert126.indep
#print axioms Rank2Cert126.count

bsd/Rank2Cert127.lean

lean_all/bsd/Rank2Cert127.lean

/-
  bsd/Rank2Cert127.lean — T39 cycle 479 (goal G196): rank ≥ 2 for 3 of the 7 congruent curves in 26001–26200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert127
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (26115, (false, 21564015, 38, 143658155025), (true, 100180261162081, 60582, 291722950373496879529)),
   (26123, (true, 488153404, 135, 2382740023958), (true, 542268766822225, 125414, 8240748134446087869135)),
   (26130, (true, 516906, 1, 371160504), (true, 134670, 1, 48481200))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert127

#print axioms Rank2Cert127.on_curve
#print axioms Rank2Cert127.indep
#print axioms Rank2Cert127.count

bsd/Rank2Cert128.lean

lean_all/bsd/Rank2Cert128.lean

/-
  bsd/Rank2Cert128.lean — T39 cycle 480 (goal G197): rank ≥ 2 for 3 of the 8 congruent curves in 26201–26400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert128
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (26265, (true, 811801, 1, 731049776), (true, 69360, 1, 16906500)),
   (26329, (true, 70344873, 7, 589896235032), (false, 11417, 991, 2762852335152)),
   (26385, (true, 46411215, 17, 311883364800), (true, 7403742025, 489, 333354095164900))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert128

#print axioms Rank2Cert128.on_curve
#print axioms Rank2Cert128.indep
#print axioms Rank2Cert128.count

bsd/Rank2Cert129.lean

lean_all/bsd/Rank2Cert129.lean

/-
  bsd/Rank2Cert129.lean — T39 cycle 481 (goal G198): rank ≥ 2 for 4 of the 10 congruent curves in 26401–26600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert129
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (26426, (true, 93897523850, 1373, 24389492182673280), (true, 608438137, 114, 12388674875219)),
   (26441, (true, 20730977, 28, 1029460881), (true, 73954083025, 819, 19524543468090760)),
   (26474, (true, 7812126, 5, 21756530376), (true, 27342, 1, 1130136)),
   (26593, (true, 4950967, 9, 9919006300), (true, 850720299025, 387, 784649015014834180))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert129

#print axioms Rank2Cert129.on_curve
#print axioms Rank2Cert129.indep
#print axioms Rank2Cert129.count

bsd/Rank2Cert13.lean

lean_all/bsd/Rank2Cert13.lean

/-
  bsd/Rank2Cert13.lean — T39 cycle 338 (goal G71): rank ≥ 2 for 6 of the 6 congruent curves in 3201–3400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert13
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (3265, (false, 156816, 17, 368463348), (true, 93412225, 123, 766274254300)),
   (3281, (true, 18769, 1, 2531760), (false, 81, 1, 29520)),
   (3289, (true, 4225, 1, 172380), (true, 3268967, 19, 5506743996)),
   (3290, (true, 1082410, 3, 1125706400), (true, 191646, 1, 83885256)),
   (3355, (true, 21025, 2, 2346825), (true, 1507005, 11, 1781614800)),
   (3379, (true, 77500, 3, 19844650), (true, 27562847925390625, 727302, 4566369587704126371544375))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 6 := rfl

end Rank2Cert13

#print axioms Rank2Cert13.on_curve
#print axioms Rank2Cert13.indep
#print axioms Rank2Cert13.count

bsd/Rank2Cert130.lean

lean_all/bsd/Rank2Cert130.lean

/-
  bsd/Rank2Cert130.lean — T39 cycle 482 (goal G199): rank ≥ 2 for 1 of the 5 congruent curves in 26601–26800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert130
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (26715, (true, 469225, 2, 312973075), (true, 97500, 1, 29279250))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert130

#print axioms Rank2Cert130.on_curve
#print axioms Rank2Cert130.indep
#print axioms Rank2Cert130.count

bsd/Rank2Cert131.lean

lean_all/bsd/Rank2Cert131.lean

/-
  bsd/Rank2Cert131.lean — T39 cycle 483 (goal G200): rank ≥ 2 for 6 of the 8 congruent curves in 26801–27000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert131
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (26818, (false, 566766, 5, 269637648), (false, 1033272746, 2985, 7681012719371992)),
   (26826, (true, 46818, 1, 8302392), (true, 173793516, 79, 614729131380)),
   (26866, (true, 44541, 1, 7497735), (false, 17749728486, 877, 1409475629771160)),
   (26890, (false, 1288810, 7, 311037600), (true, 1139626706089, 4662, 1044430411979079413)),
   (26945, (false, 104329, 109, 103403129544), (false, 2274064, 41, 68218033884)),
   (26970, (false, 799350, 11, 2828779200), (true, 28830, 1, 1729800))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 6 := rfl

end Rank2Cert131

#print axioms Rank2Cert131.on_curve
#print axioms Rank2Cert131.indep
#print axioms Rank2Cert131.count

bsd/Rank2Cert132.lean

lean_all/bsd/Rank2Cert132.lean

/-
  bsd/Rank2Cert132.lean — T39 cycle 484 (goal G201): rank ≥ 2 for 1 of the 4 congruent curves in 27001–27200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert132
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (27186, (true, 11409846, 19, 19657534680), (false, 470327, 6, 588609307))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert132

#print axioms Rank2Cert132.on_curve
#print axioms Rank2Cert132.indep
#print axioms Rank2Cert132.count

bsd/Rank2Cert133.lean

lean_all/bsd/Rank2Cert133.lean

/-
  bsd/Rank2Cert133.lean — T39 cycle 485 (goal G202): rank ≥ 2 for 6 of the 10 congruent curves in 27201–27400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert133
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (27202, (false, 567, 2, 2590875), (false, 40449843, 47, 282611391825)),
   (27258, (true, 54208, 1, 10909360), (false, 24915275, 37, 138664398565)),
   (27265, (true, 42025, 1, 6555900), (false, 19804025, 47, 253122794700)),
   (27313, (true, 187804188, 1, 2573699893050), (true, 17932691569, 5, 2401420524338628)),
   (27354, (true, 37446, 1, 4948560), (true, 884446, 1, 831379240)),
   (27370, (true, 129430, 1, 45511200), (true, 47600, 1, 8496600))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 6 := rfl

end Rank2Cert133

#print axioms Rank2Cert133.on_curve
#print axioms Rank2Cert133.indep
#print axioms Rank2Cert133.count

bsd/Rank2Cert134.lean

lean_all/bsd/Rank2Cert134.lean

/-
  bsd/Rank2Cert134.lean — T39 cycle 486 (goal G203): rank ≥ 2 for 3 of the 6 congruent curves in 27401–27600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert134
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (27401, (true, 474721, 2, 318245655), (true, 44771072, 37, 163529230416)),
   (27451, (true, 812374777791721, 64236, 22928282467183300047605), (true, 1980872017, 24, 88159839164839)),
   (27555, (true, 1255005, 1, 1405605600), (true, 95139, 1, 28087488))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert134

#print axioms Rank2Cert134.on_curve
#print axioms Rank2Cert134.indep
#print axioms Rank2Cert134.count

bsd/Rank2Cert135.lean

lean_all/bsd/Rank2Cert135.lean

/-
  bsd/Rank2Cert135.lean — T39 cycle 487 (goal G204): rank ≥ 2 for 1 of the 6 congruent curves in 27601–27800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert135
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (27761, (true, 78961, 1, 20771520), (false, 260030657, 137, 7281022219968))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert135

#print axioms Rank2Cert135.on_curve
#print axioms Rank2Cert135.indep
#print axioms Rank2Cert135.count

bsd/Rank2Cert136.lean

lean_all/bsd/Rank2Cert136.lean

/-
  bsd/Rank2Cert136.lean — T39 cycle 488 (goal G205): rank ≥ 2 for 3 of the 9 congruent curves in 27801–28000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert136
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (27811, (false, 1764, 1, 1165710), (false, 10955007, 484, 21563191710207)),
   (27898, (false, 2650384, 25, 28056362664), (false, 231572714, 165, 11007736830784)),
   (27921, (true, 24053737700521, 21957, 97767950707941510320), (false, 246504464, 105, 2894777420284))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert136

#print axioms Rank2Cert136.on_curve
#print axioms Rank2Cert136.indep
#print axioms Rank2Cert136.count

bsd/Rank2Cert137.lean

lean_all/bsd/Rank2Cert137.lean

/-
  bsd/Rank2Cert137.lean — T39 cycle 489 (goal G206): rank ≥ 2 for 1 of the 5 congruent curves in 28001–28200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert137
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (28067, (true, 3610477, 9, 5329529920), (true, 15634577, 16, 54904998855))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert137

#print axioms Rank2Cert137.on_curve
#print axioms Rank2Cert137.indep
#print axioms Rank2Cert137.count

bsd/Rank2Cert138.lean

lean_all/bsd/Rank2Cert138.lean

/-
  bsd/Rank2Cert138.lean — T39 cycle 490 (goal G207): rank ≥ 2 for 1 of the 10 congruent curves in 28201–28400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert138
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (28329, (true, 35287, 1, 3952144), (true, 30153, 1, 1793448))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert138

#print axioms Rank2Cert138.on_curve
#print axioms Rank2Cert138.indep
#print axioms Rank2Cert138.count

bsd/Rank2Cert139.lean

lean_all/bsd/Rank2Cert139.lean

/-
  bsd/Rank2Cert139.lean — T39 cycle 491 (goal G208): rank ≥ 2 for 2 of the 3 congruent curves in 28401–28600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert139
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (28441, (true, 30880249, 31, 79866759960), (false, 1002861671, 191, 8425779139680)),
   (28545, (true, 29929, 1, 1556308), (false, 31152975, 1642, 429562222423425))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert139

#print axioms Rank2Cert139.on_curve
#print axioms Rank2Cert139.indep
#print axioms Rank2Cert139.count

bsd/Rank2Cert14.lean

lean_all/bsd/Rank2Cert14.lean

/-
  bsd/Rank2Cert14.lean — T39 cycle 339 (goal G72): rank ≥ 2 for 8 of the 11 congruent curves in 3401–3600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert14
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (3409, (false, 63, 44, 52384479), (false, 3342367, 281, 492077180952)),
   (3434, (true, 243049, 8, 51166005), (true, 166617, 2, 67779459)),
   (3435, (true, 52441, 2, 11589461), (false, 240360135, 278, 1747272572325)),
   (3458, (false, 448574, 15, 425779424), (true, 796250, 1, 710509800)),
   (3554, (true, 2134178, 17, 2732970912), (false, 417698, 17, 606442656)),
   (3570, (true, 6069, 1, 382347), (true, 3630, 1, 39600)),
   (3594, (true, 6498, 1, 436392), (true, 11221068, 47, 26563261788)),
   (3595, (false, 1680907679, 1092, 161683936062319), (true, 13875625, 58, 25340485875))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 8 := rfl

end Rank2Cert14

#print axioms Rank2Cert14.on_curve
#print axioms Rank2Cert14.indep
#print axioms Rank2Cert14.count

bsd/Rank2Cert140.lean

lean_all/bsd/Rank2Cert140.lean

/-
  bsd/Rank2Cert140.lean — T39 cycle 493 (goal G210): rank ≥ 2 for 4 of the 10 congruent curves in 28601–28800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert140
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (28618, (true, 12357441, 20, 16363390239), (true, 3726980401, 310, 153557822510601)),
   (28633, (true, 11761313, 8, 39842551665), (true, 425037804438025, 120596, 1755506529156340253595)),
   (28658, (false, 10288400, 31, 81941477640), (true, 13446881, 20, 25777112529)),
   (28770, (false, 286286, 5, 353040688), (true, 1188030, 1, 1294534800))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert140

#print axioms Rank2Cert140.on_curve
#print axioms Rank2Cert140.indep
#print axioms Rank2Cert140.count

bsd/Rank2Cert141.lean

lean_all/bsd/Rank2Cert141.lean

/-
  bsd/Rank2Cert141.lean — T39 cycle 494 (goal G211): rank ≥ 2 for 7 of the 11 congruent curves in 28801–29000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert141
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (28826, (false, 89383, 2, 21777021), (true, 55738, 1, 11262672)),
   (28866, (true, 1441602, 7, 334451664), (false, 2403392, 11, 3929072912)),
   (28897, (true, 87479100, 49, 498330525090), (true, 4805148964225, 1086, 10532933924676706585)),
   (28905, (true, 1050625, 6, 148558375), (true, 39940929, 2, 252420965433)),
   (28914, (true, 3669708, 11, 2121615468), (true, 9517525, 13, 25196695685)),
   (28938, (true, 80613, 1, 21362445), (false, 58746896, 255, 14415495616808)),
   (28946, (true, 30258, 1, 1533072), (false, 1388096, 7, 343327440))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 7 := rfl

end Rank2Cert141

#print axioms Rank2Cert141.on_curve
#print axioms Rank2Cert141.indep
#print axioms Rank2Cert141.count

bsd/Rank2Cert142.lean

lean_all/bsd/Rank2Cert142.lean

/-
  bsd/Rank2Cert142.lean — T39 cycle 495 (goal G212): rank ≥ 2 for 3 of the 6 congruent curves in 29001–29200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert142
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (29041, (true, 1651225, 3, 2095074280), (false, 57600, 169, 199065599760)),
   (29154, (true, 33282, 1, 2928816), (false, 394304912, 873, 441137861979688)),
   (29185, (true, 28201144465, 142, 4734842338604625), (true, 192793225, 81, 311584676300))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert142

#print axioms Rank2Cert142.on_curve
#print axioms Rank2Cert142.indep
#print axioms Rank2Cert142.count

bsd/Rank2Cert143.lean

lean_all/bsd/Rank2Cert143.lean

/-
  bsd/Rank2Cert143.lean — T39 cycle 497 (goal G213): rank ≥ 2 for 7 of the 12 congruent curves in 29201–29400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert143
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (29211, (true, 3905643, 11, 3283902972), (true, 1041859, 5, 758473352)),
   (29233, (true, 255025, 1, 127938720), (true, 83025, 1, 22390920)),
   (29265, (false, 45990015, 151, 4514401935000), (true, 105370225, 53, 676646422300)),
   (29274, (true, 485809, 4, 89874665), (true, 30258, 1, 1331352)),
   (29281, (true, 58081, 1, 12088560), (true, 18242063, 23, 41154094128)),
   (29289, (true, 21996039, 7, 102941462520), (false, 731991, 5, 15836652)),
   (29370, (true, 356445, 1, 212084775), (true, 3136716, 1, 5555124036))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 7 := rfl

end Rank2Cert143

#print axioms Rank2Cert143.on_curve
#print axioms Rank2Cert143.indep
#print axioms Rank2Cert143.count

bsd/Rank2Cert144.lean

lean_all/bsd/Rank2Cert144.lean

/-
  bsd/Rank2Cert144.lean — T39 cycle 498 (goal G214): rank ≥ 2 for 3 of the 3 congruent curves in 29401–29600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert144
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (29465, (true, 4402071, 11, 5417482044), (true, 15189330025, 717, 138615275869100)),
   (29523, (false, 14400, 1, 3092760), (true, 138415225, 46, 1453165163515)),
   (29585, (false, 10816, 1, 2863848), (true, 106422682602769, 59946, 49518468596188392047))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert144

#print axioms Rank2Cert144.on_curve
#print axioms Rank2Cert144.indep
#print axioms Rank2Cert144.count

bsd/Rank2Cert145.lean

lean_all/bsd/Rank2Cert145.lean

/-
  bsd/Rank2Cert145.lean — T39 cycle 499 (goal G215): rank ≥ 2 for 0 of the 5 congruent curves in 29601–29800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert145
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert145

#print axioms Rank2Cert145.on_curve
#print axioms Rank2Cert145.indep
#print axioms Rank2Cert145.count

bsd/Rank2Cert146.lean

lean_all/bsd/Rank2Cert146.lean

/-
  bsd/Rank2Cert146.lean — T39 cycle 500 (goal G216): rank ≥ 2 for 6 of the 9 congruent curves in 29801–30000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert146
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (29859, (true, 2677357, 3, 4358737196), (false, 125131077, 883, 260419190084712)),
   (29865, (true, 31740, 1, 1914750), (false, 10761135, 32, 93904878375)),
   (29866, (false, 760384, 149, 578183637360), (true, 1397537, 4, 1552552911)),
   (29915, (true, 931225, 2, 891182325), (true, 6731836, 15, 295117334)),
   (29954, (false, 15842, 1, 3199728), (true, 2733982145125753729, 9468018, 850344775338822938760992545)),
   (29986, (true, 128122, 1, 44586456), (false, 55011150, 43, 51291389520))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 6 := rfl

end Rank2Cert146

#print axioms Rank2Cert146.on_curve
#print axioms Rank2Cert146.indep
#print axioms Rank2Cert146.count

bsd/Rank2Cert147.lean

lean_all/bsd/Rank2Cert147.lean

/-
  bsd/Rank2Cert147.lean — T39 cycle 502 (goal G218): rank ≥ 2 for 4 of the 5 congruent curves in 30001–30200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert147
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (30009, (false, 15876, 5, 94507182), (false, 14847257127, 1589, 9053581321629972)),
   (30019, (true, 1858131, 7, 1547628720), (true, 20919861769, 820, 795049083957147)),
   (30081, (false, 161710239, 113, 4430501490240), (true, 208153362205441, 19558, 2998545100983156584665)),
   (30107, (true, 343101276, 95, 3880515237774), (true, 487025, 2, 329326305))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert147

#print axioms Rank2Cert147.on_curve
#print axioms Rank2Cert147.indep
#print axioms Rank2Cert147.count

bsd/Rank2Cert148.lean

lean_all/bsd/Rank2Cert148.lean

/-
  bsd/Rank2Cert148.lean — T39 cycle 503 (goal G219): rank ≥ 2 for 1 of the 6 congruent curves in 30201–30400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert148
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (30305, (true, 235225, 2, 97768725), (true, 6333745, 9, 14694288400))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert148

#print axioms Rank2Cert148.on_curve
#print axioms Rank2Cert148.indep
#print axioms Rank2Cert148.count

bsd/Rank2Cert149.lean

lean_all/bsd/Rank2Cert149.lean

/-
  bsd/Rank2Cert149.lean — T39 cycle 504 (goal G220): rank ≥ 2 for 4 of the 9 congruent curves in 30401–30600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert149
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (30435, (false, 1368900, 1013, 36540800577450), (false, 88935, 2, 24792075)),
   (30530, (true, 83205, 1, 22326675), (false, 7225031, 18, 18160036147)),
   (30545, (true, 14958513025, 418, 1709096452486425), (true, 53830233674281, 9042, 394522290915774724621)),
   (30595, (true, 18313555100, 757, 716086945449150), (true, 748843225, 2, 20492094578475))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert149

#print axioms Rank2Cert149.on_curve
#print axioms Rank2Cert149.indep
#print axioms Rank2Cert149.count

bsd/Rank2Cert15.lean

lean_all/bsd/Rank2Cert15.lean

/-
  bsd/Rank2Cert15.lean — T39 cycle 343 (goal G73): rank ≥ 2 for 6 of the 10 congruent curves in 3601–3800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert15
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (3601, (true, 1338649, 18, 759344885), (false, 298116, 53, 5520504990)),
   (3634, (true, 5202, 1, 268464), (true, 5888, 1, 355488)),
   (3674, (true, 23276, 1, 3506580), (true, 377542, 7, 203907264)),
   (3705, (true, 105625, 1, 34307000), (true, 3900, 1, 76050)),
   (3746, (false, 853776, 137, 64960424760), (true, 2884802, 23, 3560801664)),
   (3794, (true, 38642, 1, 7559376), (true, 314608, 1, 176450568))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 6 := rfl

end Rank2Cert15

#print axioms Rank2Cert15.on_curve
#print axioms Rank2Cert15.indep
#print axioms Rank2Cert15.count

bsd/Rank2Cert150.lean

lean_all/bsd/Rank2Cert150.lean

/-
  bsd/Rank2Cert150.lean — T39 cycle 505 (goal G221): rank ≥ 2 for 3 of the 6 congruent curves in 30601–30800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert150
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (30617, (true, 11969669397841, 7515, 40977352635630501736), (true, 119652953, 47, 1079690879400)),
   (30705, (true, 423729, 2, 263983167), (true, 206429379025, 2002, 75300616806533425)),
   (30745, (true, 406780, 3, 190169650), (true, 37404643705, 1004, 4050564296653875))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert150

#print axioms Rank2Cert150.on_curve
#print axioms Rank2Cert150.indep
#print axioms Rank2Cert150.count

bsd/Rank2Cert151.lean

lean_all/bsd/Rank2Cert151.lean

/-
  bsd/Rank2Cert151.lean — T39 cycle 506 (goal G222): rank ≥ 2 for 4 of the 11 congruent curves in 30801–31000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert151
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (30810, (false, 936975, 16, 7580702025), (true, 31600, 1, 1248200)),
   (30914, (true, 11654578, 19, 11468104752), (true, 1688634649, 46, 69338979322275)),
   (30970, (true, 1668422034, 31, 68138060117448), (false, 27253471230, 1493, 10470806707708200)),
   (30979, (true, 1028646603295441, 94686, 31765952293547202428815), (true, 204994933, 23, 2925650721708))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert151

#print axioms Rank2Cert151.on_curve
#print axioms Rank2Cert151.indep
#print axioms Rank2Cert151.count

bsd/Rank2Cert152.lean

lean_all/bsd/Rank2Cert152.lean

/-
  bsd/Rank2Cert152.lean — T39 cycle 507 (goal G223): rank ≥ 2 for 3 of the 8 congruent curves in 31001–31200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert152
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (31065, (true, 297025, 3, 54652600), (true, 15210735, 17, 47887985700)),
   (31098, (true, 3330625, 4, 6010179425), (true, 32850, 1, 1918440)),
   (31146, (false, 3716756, 967, 56148382459700), (true, 5575134, 11, 9700733160))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert152

#print axioms Rank2Cert152.on_curve
#print axioms Rank2Cert152.indep
#print axioms Rank2Cert152.count

bsd/Rank2Cert153.lean

lean_all/bsd/Rank2Cert153.lean

/-
  bsd/Rank2Cert153.lean — T39 cycle 508 (goal G224): rank ≥ 2 for 4 of the 7 congruent curves in 31201–31400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert153
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (31234, (true, 33282, 1, 2097024), (true, 123202513, 4, 1367495093223)),
   (31290, (true, 183750, 1, 77616000), (true, 27973260, 17, 140006166300)),
   (31297, (true, 8231111, 5, 23508053016), (false, 81387551, 60, 702891331793)),
   (31369, (true, 5547487, 1, 13065844836), (true, 4031827059481, 10285, 4598508472414917996))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert153

#print axioms Rank2Cert153.on_curve
#print axioms Rank2Cert153.indep
#print axioms Rank2Cert153.count

bsd/Rank2Cert154.lean

lean_all/bsd/Rank2Cert154.lean

/-
  bsd/Rank2Cert154.lean — T39 cycle 509 (goal G225): rank ≥ 2 for 4 of the 7 congruent curves in 31401–31600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert154
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (31449, (true, 336633, 1, 194460552), (false, 3251072777, 697, 851184489558752)),
   (31490, (false, 647566, 13, 4250716848), (false, 7869915, 149, 1961113364925)),
   (31529, (true, 30961601, 20, 157340122401), (true, 2775709225, 128, 143683460467755)),
   (31595, (true, 39605, 1, 4752600), (true, 61437058225, 882, 13956382201059775))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert154

#print axioms Rank2Cert154.on_curve
#print axioms Rank2Cert154.indep
#print axioms Rank2Cert154.count

bsd/Rank2Cert155.lean

lean_all/bsd/Rank2Cert155.lean

/-
  bsd/Rank2Cert155.lean — T39 cycle 510 (goal G226): rank ≥ 2 for 4 of the 8 congruent curves in 31601–31800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert155
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (31609, (false, 484416, 5, 434538552), (true, 12554791156394009329, 13259909, 39889022993526806425212274920)),
   (31681, (false, 435600, 17, 6035991060), (true, 3416985523081, 7055, 5603545532651150604)),
   (31746, (true, 99372, 1, 29683836), (true, 72077850, 1, 611931336120)),
   (31785, (true, 70225, 1, 16594300), (true, 1560000, 7, 111033000))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert155

#print axioms Rank2Cert155.on_curve
#print axioms Rank2Cert155.indep
#print axioms Rank2Cert155.count

bsd/Rank2Cert156.lean

lean_all/bsd/Rank2Cert156.lean

/-
  bsd/Rank2Cert156.lean — T39 cycle 511 (goal G227): rank ≥ 2 for 5 of the 9 congruent curves in 31801–32000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert156
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (31809, (true, 34983, 1, 2723292), (true, 228538495249, 333, 109241413952078620)),
   (31857, (true, 81165609, 10, 730673406477), (false, 5552384, 15, 10681504336)),
   (31858, (false, 41616, 5, 162253848), (true, 11477074, 15, 30366006832)),
   (31930, (true, 4296645, 1, 8905990275), (false, 4642839, 14, 9042174891)),
   (31993, (true, 139932, 1, 50958570), (true, 1599773208037225, 50082, 63905845695937548880915))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 5 := rfl

end Rank2Cert156

#print axioms Rank2Cert156.on_curve
#print axioms Rank2Cert156.indep
#print axioms Rank2Cert156.count

bsd/Rank2Cert157.lean

lean_all/bsd/Rank2Cert157.lean

/-
  bsd/Rank2Cert157.lean — T39 cycle 512 (goal G228): rank ≥ 2 for 3 of the 7 congruent curves in 32001–32200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert157
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (32010, (true, 35574, 1, 2927232), (true, 4201164, 5, 8453353788)),
   (32097, (true, 93799225, 46, 626578445285), (false, 1238375775, 367, 145757471310360)),
   (32154, (true, 977202, 1, 965475576), (false, 561055583, 156, 12920009662351))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert157

#print axioms Rank2Cert157.on_curve
#print axioms Rank2Cert157.indep
#print axioms Rank2Cert157.count

bsd/Rank2Cert158.lean

lean_all/bsd/Rank2Cert158.lean

/-
  bsd/Rank2Cert158.lean — T39 cycle 513 (goal G229): rank ≥ 2 for 3 of the 3 congruent curves in 32201–32400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert158
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (32330, (true, 394426, 3, 167236624), (true, 28805912004072025, 938468, 740315964673886949272925)),
   (32331, (false, 736164, 5, 286334334), (true, 10677477704881, 16218, 21099369512653860545)),
   (32385, (true, 3433609, 3, 6339513248), (true, 277440, 1, 145135800))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert158

#print axioms Rank2Cert158.on_curve
#print axioms Rank2Cert158.indep
#print axioms Rank2Cert158.count

bsd/Rank2Cert159.lean

lean_all/bsd/Rank2Cert159.lean

/-
  bsd/Rank2Cert159.lean — T39 cycle 514 (goal G230): rank ≥ 2 for 4 of the 7 congruent curves in 32401–32600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert159
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (32402, (true, 61823033, 2, 486098752845), (true, 41650, 1, 5340720)),
   (32466, (true, 825846, 5, 138555144), (false, 336350, 19, 6794426240)),
   (32505, (true, 293095, 3, 9716300), (true, 34151920, 3, 199575282500)),
   (32595, (true, 1069116, 1, 1104931386), (true, 132703561225, 1638, 36359337991933475))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert159

#print axioms Rank2Cert159.on_curve
#print axioms Rank2Cert159.indep
#print axioms Rank2Cert159.count

bsd/Rank2Cert16.lean

lean_all/bsd/Rank2Cert16.lean

/-
  bsd/Rank2Cert16.lean — T39 cycle 344 (goal G74): rank ≥ 2 for 8 of the 9 congruent curves in 3801–4000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert16
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (3841, (true, 641447, 11, 354078744), (true, 2599727297725441, 167551, 132439580637819892164000)),
   (3842, (true, 3690241, 30, 2476151711), (true, 6487202, 41, 1555199520)),
   (3865, (false, 2944656, 29, 2361931572), (true, 2297810317609, 10803, 3415373048399212252)),
   (3881, (true, 153649104361, 3160, 58280293402449141), (true, 26570917240586161, 705741, 4319739228536754365706760)),
   (3899, (true, 12682012, 57, 2133677854), (false, 20206207, 92, 117286256577)),
   (3930, (true, 515205, 11, 142307325), (true, 102966, 5, 9884736)),
   (3939, (true, 132613, 1, 48271132), (true, 47873293, 87, 259160811832)),
   (3985, (false, 451584, 73, 14267408736), (true, 247275625, 249, 156998714500))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 8 := rfl

end Rank2Cert16

#print axioms Rank2Cert16.on_curve
#print axioms Rank2Cert16.indep
#print axioms Rank2Cert16.count

bsd/Rank2Cert160.lean

lean_all/bsd/Rank2Cert160.lean

/-
  bsd/Rank2Cert160.lean — T39 cycle 515 (goal G231): rank ≥ 2 for 4 of the 7 congruent curves in 32601–32800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert160
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (32610, (false, 33819210, 247, 11568143802600), (true, 1653360, 7, 546057000)),
   (32745, (false, 2537649, 13, 7834004424), (true, 1545564, 5, 1629797238)),
   (32754, (true, 1124554, 1, 1192027240), (false, 52535150, 161, 6141975308720)),
   (32770, (true, 1851505, 4, 2416214025), (true, 12126693005200225, 246228, 1317362243798839768785775))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert160

#print axioms Rank2Cert160.on_curve
#print axioms Rank2Cert160.indep
#print axioms Rank2Cert160.count

bsd/Rank2Cert161.lean

lean_all/bsd/Rank2Cert161.lean

/-
  bsd/Rank2Cert161.lean — T39 cycle 516 (goal G232): rank ≥ 2 for 6 of the 9 congruent curves in 32801–33000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert161
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (32809, (true, 16762056034201, 21750, 25917663917164864949), (false, 33297327, 352, 23456782289703)),
   (32819, (true, 1940317, 7, 1512328824), (true, 2360113561, 66, 114446139678325)),
   (32857, (true, 2770288, 3, 4584573260), (true, 18682320513025, 19551, 59781208827072175820)),
   (32882, (true, 2894418, 7, 4090777440), (true, 24752889, 10, 122059808787)),
   (32929, (true, 377417, 1, 230979204), (false, 2885648, 13, 8083169316)),
   (32970, (true, 255150, 1, 127801800), (false, 530670, 43, 44407024200))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 6 := rfl

end Rank2Cert161

#print axioms Rank2Cert161.on_curve
#print axioms Rank2Cert161.indep
#print axioms Rank2Cert161.count

bsd/Rank2Cert162.lean

lean_all/bsd/Rank2Cert162.lean

/-
  bsd/Rank2Cert162.lean — T39 cycle 518 (goal G234): rank ≥ 2 for 6 of the 9 congruent curves in 33001–33200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert162
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (33019, (true, 38637, 1, 3943836), (true, 179771025910262060521, 2065246, 2410346127133575190124141544045)),
   (33051, (true, 35643, 1, 2519208), (false, 550602357, 551, 235099677685608)),
   (33065, (true, 151321, 2, 28599669), (false, 13456, 37, 5250854028)),
   (33155, (true, 616225, 2, 472401225), (true, 1242492001, 174, 25810606186199)),
   (33169, (true, 75872151601, 623, 20595864100098960), (true, 132759277176946225, 1998792, 2928406487530470387141545)),
   (33170, (true, 57245, 1, 11162775), (false, 74241094, 989, 279549744173496))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 6 := rfl

end Rank2Cert162

#print axioms Rank2Cert162.on_curve
#print axioms Rank2Cert162.indep
#print axioms Rank2Cert162.count

bsd/Rank2Cert163.lean

lean_all/bsd/Rank2Cert163.lean

/-
  bsd/Rank2Cert163.lean — T39 cycle 519 (goal G235): rank ≥ 2 for 7 of the 13 congruent curves in 33201–33400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert163
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (33218, (false, 4418, 1, 2188320), (true, 1231646180263489, 103800, 41359149895326558236513)),
   (33258, (true, 93750, 1, 26838000), (true, 324826107934, 2795, 111114786516710248)),
   (33299, (false, 271663, 4, 238882791), (true, 337747, 3, 90516196)),
   (33306, (true, 628849, 2, 487357975), (true, 99372, 1, 29513484)),
   (33321, (true, 249658332, 13, 3943742873994), (true, 528862009, 77, 11281780411460)),
   (33361, (false, 99937, 7, 515804568), (true, 1556632398380761, 160765, 51133402146484652196384)),
   (33371, (true, 65774513, 4, 533423509143), (true, 49750855011, 1195, 3187217587509384))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 7 := rfl

end Rank2Cert163

#print axioms Rank2Cert163.on_curve
#print axioms Rank2Cert163.indep
#print axioms Rank2Cert163.count

bsd/Rank2Cert164.lean

lean_all/bsd/Rank2Cert164.lean

/-
  bsd/Rank2Cert164.lean — T39 cycle 520 (goal G236): rank ≥ 2 for 4 of the 8 congruent curves in 33401–33600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert164
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (33441, (false, 181476, 17, 4116329370), (false, 86975, 16, 2524608415)),
   (33546, (true, 927522, 1, 892692936), (true, 192246, 1, 82998720)),
   (33553, (true, 17057305057, 88, 2227487271392175), (true, 275820515544025, 2637, 4580782731046665288280)),
   (33586, (true, 319214, 1, 179351760), (true, 9777042, 17, 3669517104))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert164

#print axioms Rank2Cert164.on_curve
#print axioms Rank2Cert164.indep
#print axioms Rank2Cert164.count

bsd/Rank2Cert165.lean

lean_all/bsd/Rank2Cert165.lean

/-
  bsd/Rank2Cert165.lean — T39 cycle 521 (goal G237): rank ≥ 2 for 3 of the 6 congruent curves in 33601–33800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert165
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (33649, (true, 1792252, 3, 2364876514), (true, 119232, 1, 39497256)),
   (33769, (true, 52993546899912529, 150403, 12198020457371908447805160), (true, 219429383296201, 4577, 3250423257638992595160)),
   (33785, (true, 1357225, 6, 701685325), (true, 39115563334349401, 730450, 6865673598490597954534101))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert165

#print axioms Rank2Cert165.on_curve
#print axioms Rank2Cert165.indep
#print axioms Rank2Cert165.count

bsd/Rank2Cert166.lean

lean_all/bsd/Rank2Cert166.lean

/-
  bsd/Rank2Cert166.lean — T39 cycle 523 (goal G238): rank ≥ 2 for 1 of the 3 congruent curves in 33801–34000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert166
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (33915, (true, 180625, 2, 50683375), (true, 38115, 1, 3395700))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert166

#print axioms Rank2Cert166.on_curve
#print axioms Rank2Cert166.indep
#print axioms Rank2Cert166.count

bsd/Rank2Cert167.lean

lean_all/bsd/Rank2Cert167.lean

/-
  bsd/Rank2Cert167.lean — T39 cycle 525 (goal G239): rank ≥ 2 for 4 of the 5 congruent curves in 34001–34200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert167
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (34026, (true, 62163117, 37, 324545368401), (true, 34668, 1, 1236492)),
   (34041, (false, 5103, 2, 9720081), (false, 7513573287, 1457, 6229947704932512)),
   (34099, (true, 48373, 1, 7546188), (true, 110314986769, 1266, 31827172536148015)),
   (34185, (true, 42135, 1, 5056200), (true, 1424685025, 89, 52794528625900))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert167

#print axioms Rank2Cert167.on_curve
#print axioms Rank2Cert167.indep
#print axioms Rank2Cert167.count

bsd/Rank2Cert168.lean

lean_all/bsd/Rank2Cert168.lean

/-
  bsd/Rank2Cert168.lean — T39 cycle 526 (goal G240): rank ≥ 2 for 3 of the 6 congruent curves in 34201–34400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert168
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (34241, (true, 1238593, 1, 1377929232), (true, 727951946401, 3344, 528231298859882865)),
   (34289, (true, 6265494591409, 12513, 8084788231890894400), (true, 19748146680222138721, 34640, 87758565660761591645569425519)),
   (34314, (true, 45486, 1, 6368040), (true, 2514726, 5, 3748618224))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert168

#print axioms Rank2Cert168.on_curve
#print axioms Rank2Cert168.indep
#print axioms Rank2Cert168.count

bsd/Rank2Cert169.lean

lean_all/bsd/Rank2Cert169.lean

/-
  bsd/Rank2Cert169.lean — T39 cycle 527 (goal G241): rank ≥ 2 for 2 of the 7 congruent curves in 34401–34600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert169
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (34539, (false, 6675423, 14, 2908002231), (true, 79227049729, 558, 22093878064583015)),
   (34546, (true, 299538, 1, 162843408), (true, 6315248, 7, 15289576968))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert169

#print axioms Rank2Cert169.on_curve
#print axioms Rank2Cert169.indep
#print axioms Rank2Cert169.count

bsd/Rank2Cert17.lean

lean_all/bsd/Rank2Cert17.lean

/-
  bsd/Rank2Cert17.lean — T39 cycle 345 (goal G75): rank ≥ 2 for 6 of the 8 congruent curves in 4001–4200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert17
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (4002, (true, 4243686, 5, 8739651816), (true, 44566272, 67, 272272068000)),
   (4017, (true, 413751, 5, 258180624), (true, 405520608025, 6206, 238705068789695755)),
   (4121, (false, 2689600, 29, 3584648040), (true, 10700040481, 1265, 871628245236996)),
   (4123, (true, 451612, 9, 204301090), (true, 1721419, 5, 2254498428)),
   (4154, (true, 8978, 1, 754152), (true, 55663600, 109, 192056119080)),
   (4179, (true, 5821347, 23, 12993246504), (true, 51772, 3, 8095094))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 6 := rfl

end Rank2Cert17

#print axioms Rank2Cert17.on_curve
#print axioms Rank2Cert17.indep
#print axioms Rank2Cert17.count

bsd/Rank2Cert170.lean

lean_all/bsd/Rank2Cert170.lean

/-
  bsd/Rank2Cert170.lean — T39 cycle 528 (goal G242): rank ≥ 2 for 3 of the 11 congruent curves in 34601–34800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert170
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (34611, (false, 447564432, 143, 11599253920548), (false, 1555274475, 1757, 4213230094313640)),
   (34689, (true, 23571904, 25, 44915533208), (false, 1922031, 14, 9041534865)),
   (34697, (false, 16900, 1, 3939390), (true, 232713, 2, 90111645))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert170

#print axioms Rank2Cert170.on_curve
#print axioms Rank2Cert170.indep
#print axioms Rank2Cert170.count

bsd/Rank2Cert171.lean

lean_all/bsd/Rank2Cert171.lean

/-
  bsd/Rank2Cert171.lean — T39 cycle 529 (goal G243): rank ≥ 2 for 5 of the 10 congruent curves in 34801–35000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert171
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (34833, (false, 1221008, 7, 1317845780), (true, 2479342849, 55, 123342378525368)),
   (34874, (true, 314608, 3, 12112408), (false, 86903, 6, 369214469)),
   (34881, (true, 19312447, 19, 64349073404), (true, 36869217, 1, 223869885624)),
   (34891, (true, 640625, 4, 251519625), (true, 6725025, 8, 16450291215)),
   (34945, (true, 8421745, 3, 24423060500), (true, 130301553145, 1449, 38870038362177100))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 5 := rfl

end Rank2Cert171

#print axioms Rank2Cert171.on_curve
#print axioms Rank2Cert171.indep
#print axioms Rank2Cert171.count

bsd/Rank2Cert172.lean

lean_all/bsd/Rank2Cert172.lean

/-
  bsd/Rank2Cert172.lean — T39 cycle 531 (goal G245): rank ≥ 2 for 2 of the 6 congruent curves in 35001–35200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert172
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (35122, (true, 31953897, 26, 120888397995), (true, 875691250, 137, 17058087320400)),
   (35139, (true, 36517, 1, 1898884), (true, 75416200275, 1397, 8617636546082460))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert172

#print axioms Rank2Cert172.on_curve
#print axioms Rank2Cert172.indep
#print axioms Rank2Cert172.count

bsd/Rank2Cert173.lean

lean_all/bsd/Rank2Cert173.lean

/-
  bsd/Rank2Cert173.lean — T39 cycle 532 (goal G246): rank ≥ 2 for 3 of the 12 congruent curves in 35201–35400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert173
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (35282, (false, 23, 6, 6091435), (false, 650511875, 1603, 2312260045952025)),
   (35371, (true, 11902912, 9, 39858388696), (false, 564975, 4, 24779385)),
   (35394, (true, 2167362, 1, 3190356864), (false, 137514018, 1537, 980506055402064))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert173

#print axioms Rank2Cert173.on_curve
#print axioms Rank2Cert173.indep
#print axioms Rank2Cert173.count

bsd/Rank2Cert174.lean

lean_all/bsd/Rank2Cert174.lean

/-
  bsd/Rank2Cert174.lean — T39 cycle 533 (goal G247): rank ≥ 2 for 2 of the 6 congruent curves in 35401–35600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert174
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (35409, (true, 45177, 1, 5963364), (true, 5446235036089, 11361, 6912544832056281340)),
   (35483, (true, 694453, 3, 513895220), (true, 43540252, 9, 286673889950))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert174

#print axioms Rank2Cert174.on_curve
#print axioms Rank2Cert174.indep
#print axioms Rank2Cert174.count

bsd/Rank2Cert175.lean

lean_all/bsd/Rank2Cert175.lean

/-
  bsd/Rank2Cert175.lean — T39 cycle 534 (goal G248): rank ≥ 2 for 2 of the 6 congruent curves in 35601–35800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert175
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (35651, (true, 142373, 1, 52009188), (false, 30387110404, 2039, 25288709564777610)),
   (35689, (true, 14311289, 20, 3821114163), (true, 17183459957975809, 277909, 2223337266994701368055480))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert175

#print axioms Rank2Cert175.on_curve
#print axioms Rank2Cert175.indep
#print axioms Rank2Cert175.count

bsd/Rank2Cert176.lean

lean_all/bsd/Rank2Cert176.lean

/-
  bsd/Rank2Cert176.lean — T39 cycle 535 (goal G249): rank ≥ 2 for 5 of the 7 congruent curves in 35801–36000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert176
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (35826, (false, 23696414, 395, 27210086648416), (true, 30559578, 29, 28237050072)),
   (35841, (false, 47104239, 89, 1921446628920), (true, 439698961, 86, 7356137720815)),
   (35931, (true, 38332, 1, 2614346), (true, 18967851, 11, 80409637980)),
   (35970, (false, 41504034, 41, 283280890464), (true, 329725, 3, 35940025)),
   (35985, (true, 712860, 1, 601107750), (true, 1093708098025, 5031, 633220517582982100))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 5 := rfl

end Rank2Cert176

#print axioms Rank2Cert176.on_curve
#print axioms Rank2Cert176.indep
#print axioms Rank2Cert176.count

bsd/Rank2Cert177.lean

lean_all/bsd/Rank2Cert177.lean

/-
  bsd/Rank2Cert177.lean — T39 cycle 536 (goal G250): rank ≥ 2 for 5 of the 11 congruent curves in 36001–36200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert177
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (36010, (true, 37597210, 3, 230524607600), (true, 102713199121, 1518, 19400931836410969)),
   (36051, (false, 13725, 11, 511042140), (true, 1797200316409, 2390, 2393453047740191173)),
   (36114, (true, 39936, 1, 3407040), (false, 441249588, 313, 73739873873484)),
   (36121, (true, 776161, 3, 620928800), (true, 18869545400281, 33, 81967588090011138160)),
   (36186, (true, 318828, 1, 178862508), (false, 7897428, 23, 49003130484))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 5 := rfl

end Rank2Cert177

#print axioms Rank2Cert177.on_curve
#print axioms Rank2Cert177.indep
#print axioms Rank2Cert177.count

bsd/Rank2Cert178.lean

lean_all/bsd/Rank2Cert178.lean

/-
  bsd/Rank2Cert178.lean — T39 cycle 537 (goal G251): rank ≥ 2 for 1 of the 6 congruent curves in 36201–36400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert178
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (36210, (true, 3690240, 1, 7088608800), (true, 124432044, 47, 1063249191972))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert178

#print axioms Rank2Cert178.on_curve
#print axioms Rank2Cert178.indep
#print axioms Rank2Cert178.count

bsd/Rank2Cert179.lean

lean_all/bsd/Rank2Cert179.lean

/-
  bsd/Rank2Cert179.lean — T39 cycle 538 (goal G252): rank ≥ 2 for 4 of the 9 congruent curves in 36401–36600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert179
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (36411, (true, 1311025, 6, 28055935), (false, 928860351, 878, 854985837190905)),
   (36490, (false, 705600, 13, 5146100400), (false, 51938410, 127, 4225011663600)),
   (36498, (true, 917301, 5, 90233451), (true, 20183394, 5, 90583072272)),
   (36570, (false, 1145814, 11, 4575054384), (true, 5348926, 7, 11656015624))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert179

#print axioms Rank2Cert179.on_curve
#print axioms Rank2Cert179.indep
#print axioms Rank2Cert179.count

bsd/Rank2Cert18.lean

lean_all/bsd/Rank2Cert18.lean

/-
  bsd/Rank2Cert18.lean — T39 cycle 346 (goal G76): rank ≥ 2 for 7 of the 8 congruent curves in 4201–4400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert18
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (4218, (true, 4332, 1, 64980), (true, 71179453, 127, 176598173015)),
   (4242, (true, 3366006, 5, 6172437096), (true, 32133150, 13, 182104987680)),
   (4249, (true, 243049, 7, 61822200), (false, 8100056263, 4673, 8318807309299656)),
   (4290, (true, 59290, 1, 14399000), (true, 5850, 1, 304200)),
   (4305, (true, 42025, 2, 7858675), (true, 18081, 2, 741321)),
   (4370, (true, 23805, 1, 3610425), (true, 1969996, 3, 2764467244)),
   (4379, (false, 2280100, 701, 3249284094210), (true, 593021, 7, 425731020))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 7 := rfl

end Rank2Cert18

#print axioms Rank2Cert18.on_curve
#print axioms Rank2Cert18.indep
#print axioms Rank2Cert18.count

bsd/Rank2Cert180.lean

lean_all/bsd/Rank2Cert180.lean

/-
  bsd/Rank2Cert180.lean — T39 cycle 539 (goal G253): rank ≥ 2 for 4 of the 6 congruent curves in 36601–36800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert180
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (36610, (false, 5915, 3, 25336675), (false, 1824774196, 977, 1490735224008492)),
   (36642, (true, 144150, 1, 52931880), (false, 9381902, 37, 150937197040)),
   (36707, (false, 18163475, 23, 29263520880), (true, 922716, 5, 92522514)),
   (36777, (true, 42025, 1, 4168880), (false, 234623, 24, 10260238015))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert180

#print axioms Rank2Cert180.on_curve
#print axioms Rank2Cert180.indep
#print axioms Rank2Cert180.count

bsd/Rank2Cert181.lean

lean_all/bsd/Rank2Cert181.lean

/-
  bsd/Rank2Cert181.lean — T39 cycle 540 (goal G254): rank ≥ 2 for 3 of the 6 congruent curves in 36801–37000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert181
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (36865, (true, 206236781689, 1573, 84000152733170088), (true, 106016779638025, 46787, 707938396551551415600)),
   (36889, (true, 994009, 5, 369771348), (true, 21248827026025, 23693, 21959376682149905460)),
   (36890, (true, 11212460, 17, 11627479500), (true, 81685, 1, 20829675))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert181

#print axioms Rank2Cert181.on_curve
#print axioms Rank2Cert181.indep
#print axioms Rank2Cert181.count

bsd/Rank2Cert182.lean

lean_all/bsd/Rank2Cert182.lean

/-
  bsd/Rank2Cert182.lean — T39 cycle 542 (goal G256): rank ≥ 2 for 4 of the 6 congruent curves in 37001–37200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert182
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (37009, (false, 17, 1, 152592), (false, 278528, 9, 1575230336)),
   (37065, (true, 3115225, 2, 5492141675), (true, 11068215, 17, 9269469000)),
   (37122, (false, 79350, 11, 1265092920), (true, 14937258, 19, 25500487320)),
   (37185, (true, 42025, 1, 4013900), (true, 7474185, 13, 11061793800))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert182

#print axioms Rank2Cert182.on_curve
#print axioms Rank2Cert182.indep
#print axioms Rank2Cert182.count

bsd/Rank2Cert183.lean

lean_all/bsd/Rank2Cert183.lean

/-
  bsd/Rank2Cert183.lean — T39 cycle 543 (goal G257): rank ≥ 2 for 1 of the 4 congruent curves in 37201–37400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert183
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (37329, (true, 7161372, 1, 19164100986), (false, 2801423, 16, 15291972809))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert183

#print axioms Rank2Cert183.on_curve
#print axioms Rank2Cert183.indep
#print axioms Rank2Cert183.count

bsd/Rank2Cert184.lean

lean_all/bsd/Rank2Cert184.lean

/-
  bsd/Rank2Cert184.lean — T39 cycle 544 (goal G258): rank ≥ 2 for 3 of the 13 congruent curves in 37401–37600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert184
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (37505, (true, 41485949761, 545, 8139563087129616), (true, 92906176276225, 14272, 892470154432640580225)),
   (37513, (false, 139167, 4, 217806225), (true, 11237823, 11, 34462657200)),
   (37522, (true, 4169833, 6, 8055716075), (false, 716308850, 167, 20417250934080))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert184

#print axioms Rank2Cert184.on_curve
#print axioms Rank2Cert184.indep
#print axioms Rank2Cert184.count

bsd/Rank2Cert185.lean

lean_all/bsd/Rank2Cert185.lean

/-
  bsd/Rank2Cert185.lean — T39 cycle 545 (goal G259): rank ≥ 2 for 2 of the 4 congruent curves in 37601–37800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert185
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (37705, (true, 346553755611067225, 2968788, 57866870042071064605677475), (true, 72192263576194569721, 27235018, 565489601594610444508279140219)),
   (37706, (true, 47721656250, 1093, 3442038437556000), (false, 48392858538, 2911, 69477850080512496))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert185

#print axioms Rank2Cert185.on_curve
#print axioms Rank2Cert185.indep
#print axioms Rank2Cert185.count

bsd/Rank2Cert186.lean

lean_all/bsd/Rank2Cert186.lean

/-
  bsd/Rank2Cert186.lean — T39 cycle 546 (goal G260): rank ≥ 2 for 0 of the 5 congruent curves in 37801–38000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert186
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert186

#print axioms Rank2Cert186.on_curve
#print axioms Rank2Cert186.indep
#print axioms Rank2Cert186.count

bsd/Rank2Cert187.lean

lean_all/bsd/Rank2Cert187.lean

/-
  bsd/Rank2Cert187.lean — T39 cycle 547 (goal G261): rank ≥ 2 for 3 of the 6 congruent curves in 38001–38200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert187
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (38049, (true, 1329409, 4, 1362644225), (true, 72897, 1, 16788024)),
   (38090, (true, 2146225, 6, 2418795575), (true, 3146313226225, 1876, 5575815405172996425)),
   (38114, (true, 166617, 2, 27437949), (true, 36187577, 26, 152860715379))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert187

#print axioms Rank2Cert187.on_curve
#print axioms Rank2Cert187.indep
#print axioms Rank2Cert187.count

bsd/Rank2Cert188.lean

lean_all/bsd/Rank2Cert188.lean

/-
  bsd/Rank2Cert188.lean — T39 cycle 548 (goal G262): rank ≥ 2 for 0 of the 3 congruent curves in 38201–38400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert188
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert188

#print axioms Rank2Cert188.on_curve
#print axioms Rank2Cert188.indep
#print axioms Rank2Cert188.count

bsd/Rank2Cert189.lean

lean_all/bsd/Rank2Cert189.lean

/-
  bsd/Rank2Cert189.lean — T39 cycle 549 (goal G263): rank ≥ 2 for 4 of the 8 congruent curves in 38401–38600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert189
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (38409, (true, 170097, 2, 30107169), (true, 184074688, 53, 2023457986648)),
   (38410, (false, 9610, 1, 3645600), (false, 14169150, 41, 237119065800)),
   (38570, (true, 129605, 1, 44544675), (false, 47069204, 57, 796789037044)),
   (38595, (false, 1734605, 9, 3425388400), (true, 9791164, 11, 26927948438))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert189

#print axioms Rank2Cert189.on_curve
#print axioms Rank2Cert189.indep
#print axioms Rank2Cert189.count

bsd/Rank2Cert19.lean

lean_all/bsd/Rank2Cert19.lean

/-
  bsd/Rank2Cert19.lean — T39 cycle 347 (goal G77): rank ≥ 2 for 7 of the 9 congruent curves in 4401–4600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert19
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (4402, (true, 5202, 1, 199920), (false, 33982479, 280, 2002064053431)),
   (4441, (true, 234322796761, 3720, 109457722573419491), (true, 253304940022339921, 4034491, 122185489339884393455384280)),
   (4466, (true, 11397232, 47, 19267020696), (true, 18502, 1, 2442264)),
   (4490, (false, 78400, 37, 1720966800), (false, 123210, 7, 63980400)),
   (4529, (true, 9583, 1, 826728), (true, 10297681, 17, 32777239440)),
   (4587, (true, 57963, 1, 13911120), (false, 132831875, 3763, 748595858496200)),
   (4595, (false, 3286234639, 1012, 193099514069559), (true, 6193696995, 677, 458400262567500))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 7 := rfl

end Rank2Cert19

#print axioms Rank2Cert19.on_curve
#print axioms Rank2Cert19.indep
#print axioms Rank2Cert19.count

bsd/Rank2Cert190.lean

lean_all/bsd/Rank2Cert190.lean

/-
  bsd/Rank2Cert190.lean — T39 cycle 550 (goal G264): rank ≥ 2 for 2 of the 7 congruent curves in 38601–38800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert190
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (38690, (true, 1755625, 6, 1416087125), (true, 15803186, 11, 60002919984)),
   (38731, (true, 135531, 1, 47814360), (false, 915359903, 2808, 9239466782685329))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert190

#print axioms Rank2Cert190.on_curve
#print axioms Rank2Cert190.indep
#print axioms Rank2Cert190.count

bsd/Rank2Cert191.lean

lean_all/bsd/Rank2Cert191.lean

/-
  bsd/Rank2Cert191.lean — T39 cycle 551 (goal G265): rank ≥ 2 for 6 of the 8 congruent curves in 38801–39000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert191
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (38801, (true, 1423249, 1, 1697304960), (true, 14977025, 16, 43379700735)),
   (38883, (true, 43936584627, 341, 9160678696419900), (true, 5411601901225, 10574, 7496553839226078835)),
   (38890, (false, 285610, 7, 1006902000), (true, 20705195449, 240, 2961845192586707)),
   (38922, (false, 15332148, 29, 113242313340), (true, 520147056, 115, 1705501522296)),
   (38985, (true, 319225, 2, 157377925), (true, 2293215, 1, 3472197300)),
   (38995, (false, 1628176, 113, 635351653068), (true, 11127085225, 422, 917098107379725))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 6 := rfl

end Rank2Cert191

#print axioms Rank2Cert191.on_curve
#print axioms Rank2Cert191.indep
#print axioms Rank2Cert191.count

bsd/Rank2Cert192.lean

lean_all/bsd/Rank2Cert192.lean

/-
  bsd/Rank2Cert192.lean — T39 cycle 553 (goal G267): rank ≥ 2 for 4 of the 7 congruent curves in 39001–39200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert192
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (39065, (false, 625, 1, 976500), (true, 38520004185, 209, 7552712259838200)),
   (39130, (true, 142814, 1, 51905112), (false, 9516650, 21, 44407048400)),
   (39169, (true, 14209308, 19, 5283494358), (true, 4704558057627961, 81534, 322189973361010549993445)),
   (39185, (true, 15208834865, 299, 1825181756829900), (true, 217852788266569, 65726, 2024069492201041641603))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert192

#print axioms Rank2Cert192.on_curve
#print axioms Rank2Cert192.indep
#print axioms Rank2Cert192.count

bsd/Rank2Cert193.lean

lean_all/bsd/Rank2Cert193.lean

/-
  bsd/Rank2Cert193.lean — T39 cycle 554 (goal G268): rank ≥ 2 for 2 of the 10 congruent curves in 39201–39400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert193
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (39338, (true, 180625, 2, 37692825), (false, 10946666250, 2273, 21233482839145200)),
   (39353, (false, 44246687, 36, 168723272465), (true, 18899238897, 457, 2339620506960660))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert193

#print axioms Rank2Cert193.on_curve
#print axioms Rank2Cert193.indep
#print axioms Rank2Cert193.count

bsd/Rank2Cert194.lean

lean_all/bsd/Rank2Cert194.lean

/-
  bsd/Rank2Cert194.lean — T39 cycle 555 (goal G269): rank ≥ 2 for 2 of the 8 congruent curves in 39401–39600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert194
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (39410, (true, 31364410, 27, 70467027400), (false, 1105454, 15, 9250344656)),
   (39435, (true, 344619, 1, 200977128), (true, 2696640, 1, 4427800200))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert194

#print axioms Rank2Cert194.on_curve
#print axioms Rank2Cert194.indep
#print axioms Rank2Cert194.count

bsd/Rank2Cert195.lean

lean_all/bsd/Rank2Cert195.lean

/-
  bsd/Rank2Cert195.lean — T39 cycle 556 (goal G270): rank ≥ 2 for 1 of the 5 congruent curves in 39601–39800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert195
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (39746, (true, 703298, 1, 588863232), (false, 20420672, 887, 141310876477968))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert195

#print axioms Rank2Cert195.on_curve
#print axioms Rank2Cert195.indep
#print axioms Rank2Cert195.count

bsd/Rank2Cert196.lean

lean_all/bsd/Rank2Cert196.lean

/-
  bsd/Rank2Cert196.lean — T39 cycle 557 (goal G271): rank ≥ 2 for 3 of the 4 congruent curves in 39801–40000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert196
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (39817, (true, 82194622515625, 4629, 745146575729671314500), (true, 11454467958025, 16296, 14906880042045181595)),
   (39889, (true, 97523633, 49, 181637084112), (true, 798006001, 47, 22405025413680)),
   (39963, (false, 131061, 5, 358562988), (false, 251424173, 461, 134608678684480))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert196

#print axioms Rank2Cert196.on_curve
#print axioms Rank2Cert196.indep
#print axioms Rank2Cert196.count

bsd/Rank2Cert197.lean

lean_all/bsd/Rank2Cert197.lean

/-
  bsd/Rank2Cert197.lean — T39 cycle 558 (goal G272): rank ≥ 2 for 2 of the 6 congruent curves in 40001–40200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert197
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (40001, (true, 2007889, 6, 1982744335), (false, 400, 1, 799980)),
   (40137, (false, 764048, 11, 4192262900), (true, 23662358173609, 18225, 95095852552714645648))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert197

#print axioms Rank2Cert197.on_curve
#print axioms Rank2Cert197.indep
#print axioms Rank2Cert197.count

bsd/Rank2Cert198.lean

lean_all/bsd/Rank2Cert198.lean

/-
  bsd/Rank2Cert198.lean — T39 cycle 559 (goal G273): rank ≥ 2 for 5 of the 10 congruent curves in 40201–40400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert198
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (40299, (false, 106875, 7, 644601600), (false, 13952631, 50, 372697978653)),
   (40330, (false, 3701776, 29, 64867502232), (true, 27085850, 19, 118869722400)),
   (40345, (true, 6609758193025, 12122, 7514397908463660825), (true, 12165022540015921, 406510, 1122296106267008810852031)),
   (40370, (false, 3148475, 9, 1566910675), (true, 45345520, 23, 269368869000)),
   (40386, (false, 2481354, 31, 61011142920), (true, 384960624, 97, 1209620007000))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 5 := rfl

end Rank2Cert198

#print axioms Rank2Cert198.on_curve
#print axioms Rank2Cert198.indep
#print axioms Rank2Cert198.count

bsd/Rank2Cert199.lean

lean_all/bsd/Rank2Cert199.lean

/-
  bsd/Rank2Cert199.lean — T39 cycle 560 (goal G274): rank ≥ 2 for 3 of the 9 congruent curves in 40401–40600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert199
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (40467, (true, 189525, 1, 80605980), (false, 855985823, 184, 31297411628945)),
   (40506, (true, 62694, 1, 11981520), (false, 589667508, 787, 609049274715684)),
   (40593, (true, 6559168, 1, 16798279960), (true, 1005578959048225, 103342, 28772313759306440574985))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert199

#print axioms Rank2Cert199.on_curve
#print axioms Rank2Cert199.indep
#print axioms Rank2Cert199.count

bsd/Rank2Cert2.lean

lean_all/bsd/Rank2Cert2.lean

/-
  bsd/Rank2Cert2.lean — T39 cycle 318 (goal G47): rank ≥ 2 for the 17 congruent curves in 1001–1200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 1200, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert2
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (1003, (true, 67473, 4, 17023545), (true, 327019123, 453, 4595449112000)),
   (1057, (true, 70225, 7, 12567360), (false, 776223, 44, 1668168495)),
   (1073, (true, 21025, 3, 2708020), (true, 2027610841, 95, 91300247263764)),
   (1081, (true, 1369, 1, 31080), (true, 7316208, 11, 19786074852)),
   (1105, (true, 7225, 1, 606900), (true, 142129, 11, 18172908)),
   (1113, (true, 811801, 5, 731002924), (true, 19663, 1, 2752820)),
   (1122, (true, 2178, 1, 87120), (true, 166617, 8, 61370595)),
   (1131, (true, 48841, 6, 5961475), (true, 10933, 1, 1137032)),
   (1145, (true, 555025, 22, 22808175), (false, 2704, 5, 1481844)),
   (1146, (true, 5202, 1, 365976), (true, 2028, 1, 75348)),
   (1154, (true, 665858, 1, 543340128), (true, 511826960229649, 456012, 10227787487748920482055)),
   (1155, (true, 2205, 1, 88200), (true, 2475, 1, 108900)),
   (1169, (true, 12943, 1, 1466472), (true, 6080368, 57, 11708316172)),
   (1178, (true, 1900, 1, 64980), (false, 6935599, 100, 25076325351)),
   (1185, (true, 15625, 1, 1947500), (true, 93615, 7, 22467600)),
   (1186, (true, 206082, 7, 89756736), (true, 13098573601, 1972, 1403114792702415)),
   (1195, (true, 3645, 1, 207900), (true, 4184149225, 418, 270314500063725))]

theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 17 := rfl

end Rank2Cert2

#print axioms Rank2Cert2.on_curve
#print axioms Rank2Cert2.indep
#print axioms Rank2Cert2.count

bsd/Rank2Cert20.lean

lean_all/bsd/Rank2Cert20.lean

/-
  bsd/Rank2Cert20.lean — T39 cycle 348 (goal G78): rank ≥ 2 for 8 of the 12 congruent curves in 4601–4800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert20
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (4633, (true, 7225, 1, 471240), (true, 25625, 1, 4034400)),
   (4634, (true, 219122, 1, 102549096), (false, 3699703, 46, 17466562491)),
   (4641, (true, 48841, 1, 10745020), (true, 7497, 1, 509796)),
   (4674, (true, 6498, 1, 363888), (true, 1198881, 16, 81923535)),
   (4705, (true, 18361225, 38, 73093594275), (true, 316336140077281, 80666, 5599892520190001726121)),
   (4731, (false, 14881275, 271, 1339099652520), (true, 392673, 8, 156676527)),
   (4747, (false, 36978143, 216, 1327883656015), (true, 142458688969, 1370, 53663918198851203)),
   (4794, (true, 5202, 1, 145656), (true, 17603568, 37, 68533624152))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 8 := rfl

end Rank2Cert20

#print axioms Rank2Cert20.on_curve
#print axioms Rank2Cert20.indep
#print axioms Rank2Cert20.count

bsd/Rank2Cert200.lean

lean_all/bsd/Rank2Cert200.lean

/-
  bsd/Rank2Cert200.lean — T39 cycle 561 (goal G275): rank ≥ 2 for 2 of the 3 congruent curves in 40601–40800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert200
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (40651, (true, 2588573, 1, 4164252612), (true, 32954269663205881, 832762, 3098010613172456157260475)),
   (40745, (true, 650905, 3, 433871900), (true, 248454745, 71, 2203499390400))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert200

#print axioms Rank2Cert200.on_curve
#print axioms Rank2Cert200.indep
#print axioms Rank2Cert200.count

bsd/Rank2Cert201.lean

lean_all/bsd/Rank2Cert201.lean

/-
  bsd/Rank2Cert201.lean — T39 cycle 562 (goal G276): rank ≥ 2 for 3 of the 4 congruent curves in 40801–41000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert201
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (40874, (false, 3575138, 27, 55933544744), (true, 18695468, 7, 80370743508)),
   (40890, (true, 126150, 1, 42386400), (true, 473854, 1, 324970408)),
   (40971, (true, 6640732, 1, 17112586834), (false, 585669, 5, 643085604))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert201

#print axioms Rank2Cert201.on_curve
#print axioms Rank2Cert201.indep
#print axioms Rank2Cert201.count

bsd/Rank2Cert202.lean

lean_all/bsd/Rank2Cert202.lean

/-
  bsd/Rank2Cert202.lean — T39 cycle 564 (goal G278): rank ≥ 2 for 2 of the 7 congruent curves in 41001–41200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert202
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (41026, (true, 62658, 1, 11854752), (false, 25538, 1, 5131104)),
   (41041, (true, 284089, 2, 123578715), (true, 63063, 1, 12024012))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert202

#print axioms Rank2Cert202.on_curve
#print axioms Rank2Cert202.indep
#print axioms Rank2Cert202.count

bsd/Rank2Cert203.lean

lean_all/bsd/Rank2Cert203.lean

/-
  bsd/Rank2Cert203.lean — T39 cycle 565 (goal G279): rank ≥ 2 for 5 of the 7 congruent curves in 41201–41400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert203
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (41210, (false, 6400, 1, 3256800), (true, 1515127119025, 3112, 1799113810611506025)),
   (41314, (false, 42350, 3, 76020560), (true, 45508554, 19, 290045999880)),
   (41354, (false, 7187063, 54, 322706752901), (true, 3372149, 5, 5894209893)),
   (41361, (false, 1862537, 11, 6339513248), (false, 1873808, 7, 1057059116)),
   (41385, (true, 180625, 1, 74723500), (true, 173196225, 64, 467572075425))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 5 := rfl

end Rank2Cert203

#print axioms Rank2Cert203.on_curve
#print axioms Rank2Cert203.indep
#print axioms Rank2Cert203.count

bsd/Rank2Cert204.lean

lean_all/bsd/Rank2Cert204.lean

/-
  bsd/Rank2Cert204.lean — T39 cycle 566 (goal G280): rank ≥ 2 for 2 of the 2 congruent curves in 41401–41600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert204
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (41403, (true, 33016780947, 599, 5357758382329500), (true, 98775471802225, 25398, 945122913986458088735)),
   (41474, (false, 576, 1, 995280), (true, 80016659154, 923, 20308274179194000))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert204

#print axioms Rank2Cert204.on_curve
#print axioms Rank2Cert204.indep
#print axioms Rank2Cert204.count

bsd/Rank2Cert205.lean

lean_all/bsd/Rank2Cert205.lean

/-
  bsd/Rank2Cert205.lean — T39 cycle 567 (goal G281): rank ≥ 2 for 2 of the 6 congruent curves in 41601–41800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert205
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (41665, (false, 27225, 1, 5204100), (true, 43433304265, 189, 9046470683364400)),
   (41689, (true, 944517289, 7, 29027781953640), (false, 13650867023, 924, 3840577933875479))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert205

#print axioms Rank2Cert205.on_curve
#print axioms Rank2Cert205.indep
#print axioms Rank2Cert205.count

bsd/Rank2Cert206.lean

lean_all/bsd/Rank2Cert206.lean

/-
  bsd/Rank2Cert206.lean — T39 cycle 568 (goal G282): rank ≥ 2 for 4 of the 9 congruent curves in 41801–42000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert206
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (41810, (true, 115750985, 44, 890207753925), (false, 152525840, 1059, 579083916190600)),
   (41865, (false, 1287735, 17, 13651749000), (true, 5005981134025, 1157, 11199705320154582100)),
   (41881, (true, 260743, 1, 131414472), (true, 1190896, 3, 1232790020)),
   (41979, (false, 1310763, 217, 2263151085336), (true, 240429, 1, 116080020))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert206

#print axioms Rank2Cert206.on_curve
#print axioms Rank2Cert206.indep
#print axioms Rank2Cert206.count

bsd/Rank2Cert207.lean

lean_all/bsd/Rank2Cert207.lean

/-
  bsd/Rank2Cert207.lean — T39 cycle 569 (goal G283): rank ≥ 2 for 5 of the 11 congruent curves in 42001–42200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert207
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (42001, (true, 1853390753550625, 53784, 79618744713056429882575), (true, 99998585191329841, 335279, 31586839194108430491330000)),
   (42074, (true, 2693114666, 35, 139734202429536), (false, 1392088922, 631, 622875601789392)),
   (42090, (true, 78579454, 35, 525648810392), (true, 806725, 3, 639732925)),
   (42161, (true, 6028288, 9, 12196909088), (false, 21856023, 23, 20774032356)),
   (42195, (true, 1364305, 4, 1384769575), (true, 11260313638890625, 476956, 624702598793477349250625))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 5 := rfl

end Rank2Cert207

#print axioms Rank2Cert207.on_curve
#print axioms Rank2Cert207.indep
#print axioms Rank2Cert207.count

bsd/Rank2Cert208.lean

lean_all/bsd/Rank2Cert208.lean

/-
  bsd/Rank2Cert208.lean — T39 cycle 570 (goal G284): rank ≥ 2 for 5 of the 8 congruent curves in 42201–42400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert208
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (42211, (true, 45253, 1, 3469908), (true, 17034017, 2, 70299830601)),
   (42218, (true, 11265100, 9, 36025467940), (false, 2019798, 367, 8081346451200)),
   (42315, (true, 53235, 1, 7452900), (true, 562185, 2, 401962275)),
   (42330, (true, 234226, 1, 111491576), (false, 301300095, 212, 32606464831425)),
   (42347, (true, 137853, 1, 48708060), (true, 3339053, 7, 4780300020))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 5 := rfl

end Rank2Cert208

#print axioms Rank2Cert208.on_curve
#print axioms Rank2Cert208.indep
#print axioms Rank2Cert208.count

bsd/Rank2Cert209.lean

lean_all/bsd/Rank2Cert209.lean

/-
  bsd/Rank2Cert209.lean — T39 cycle 571 (goal G285): rank ≥ 2 for 2 of the 9 congruent curves in 42401–42600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert209
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (42490, (false, 13690, 1, 4706400), (false, 739634315, 141, 11098356093125)),
   (42594, (true, 565068, 1, 423558828), (false, 4965518, 11, 3076445456))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert209

#print axioms Rank2Cert209.on_curve
#print axioms Rank2Cert209.indep
#print axioms Rank2Cert209.count

bsd/Rank2Cert21.lean

lean_all/bsd/Rank2Cert21.lean

/-
  bsd/Rank2Cert21.lean — T39 cycle 351 (goal G79): rank ≥ 2 for 4 of the 9 congruent curves in 4801–5000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert21
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (4810, (true, 34225, 2, 5236425), (true, 105625, 4, 23512125)),
   (4841, (false, 10515263, 48, 12060784351), (false, 103366583, 289, 3974143032216)),
   (4899, (false, 2783, 116, 3477603151), (true, 347829, 5, 192001608)),
   (4921, (true, 855625, 13, 186047100), (true, 9583, 1, 804972))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert21

#print axioms Rank2Cert21.on_curve
#print axioms Rank2Cert21.indep
#print axioms Rank2Cert21.count

bsd/Rank2Cert210.lean

lean_all/bsd/Rank2Cert210.lean

/-
  bsd/Rank2Cert210.lean — T39 cycle 572 (goal G286): rank ≥ 2 for 2 of the 6 congruent curves in 42601–42800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert210
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (42690, (false, 9047687294, 817, 2570177128387904), (false, 434079035, 903, 725190809540275)),
   (42770, (true, 59150, 1, 9937200), (false, 2621566, 11, 7224596352))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert210

#print axioms Rank2Cert210.on_curve
#print axioms Rank2Cert210.indep
#print axioms Rank2Cert210.count

bsd/Rank2Cert211.lean

lean_all/bsd/Rank2Cert211.lean

/-
  bsd/Rank2Cert211.lean — T39 cycle 573 (goal G287): rank ≥ 2 for 3 of the 9 congruent curves in 42801–43000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert211
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (42833, (false, 2894143, 148, 1596099092385), (true, 25301674225, 597, 3209496276105260)),
   (42882, (false, 419006, 15, 6239614528), (false, 9961098, 31, 126205679880)),
   (42889, (true, 2379223, 3, 3621267188), (true, 126440589121, 142, 44959306887389895))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert211

#print axioms Rank2Cert211.on_curve
#print axioms Rank2Cert211.indep
#print axioms Rank2Cert211.count

bsd/Rank2Cert212.lean

lean_all/bsd/Rank2Cert212.lean

/-
  bsd/Rank2Cert212.lean — T39 cycle 576 (goal G290): rank ≥ 2 for 4 of the 7 congruent curves in 43001–43200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert212
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (43010, (true, 9883885, 9, 29079346175), (true, 2376990, 1, 3664122000)),
   (43035, (false, 88935, 2, 43953525), (true, 57475, 1, 9133300)),
   (43043, (true, 629563, 3, 393763040), (true, 117612017600, 1207, 34123393727798760)),
   (43194, (true, 44652, 1, 2391444), (true, 705894, 1, 591963120))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert212

#print axioms Rank2Cert212.on_curve
#print axioms Rank2Cert212.indep
#print axioms Rank2Cert212.count

bsd/Rank2Cert213.lean

lean_all/bsd/Rank2Cert213.lean

/-
  bsd/Rank2Cert213.lean — T39 cycle 577 (goal G291): rank ≥ 2 for 1 of the 8 congruent curves in 43201–43400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert213
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (43355, (true, 44109, 1, 1705548), (true, 1433165728201, 5174, 1006540268944927101))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert213

#print axioms Rank2Cert213.on_curve
#print axioms Rank2Cert213.indep
#print axioms Rank2Cert213.count

bsd/Rank2Cert214.lean

lean_all/bsd/Rank2Cert214.lean

/-
  bsd/Rank2Cert214.lean — T39 cycle 578 (goal G292): rank ≥ 2 for 6 of the 11 congruent curves in 43401–43600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert214
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (43401, (true, 395641, 3, 39564100), (true, 197530716, 55, 2074266643014)),
   (43435, (true, 186515, 1, 78336300), (true, 452965, 3, 154008100)),
   (43491, (true, 53067, 1, 7004844), (true, 6041653744, 279, 388957087276660)),
   (43530, (false, 126150, 11, 1870221600), (false, 20319870, 23, 48834351000)),
   (43554, (false, 18910122, 223, 9418210329528), (true, 379542, 1, 232279704)),
   (43555, (false, 23951871, 28, 119111292033), (true, 81995, 1, 19892700))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 6 := rfl

end Rank2Cert214

#print axioms Rank2Cert214.on_curve
#print axioms Rank2Cert214.indep
#print axioms Rank2Cert214.count

bsd/Rank2Cert215.lean

lean_all/bsd/Rank2Cert215.lean

/-
  bsd/Rank2Cert215.lean — T39 cycle 579 (goal G293): rank ≥ 2 for 3 of the 9 congruent curves in 43601–43800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert215
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (43617, (true, 125692, 1, 41792590), (true, 27275164, 25, 4650415462)),
   (43746, (true, 406836282, 1, 8205962208984), (false, 116334023, 62, 1309669860941)),
   (43795, (true, 212521, 2, 55467981), (true, 1599267683814025, 8458, 63955943332880976263325))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert215

#print axioms Rank2Cert215.on_curve
#print axioms Rank2Cert215.indep
#print axioms Rank2Cert215.count

bsd/Rank2Cert216.lean

lean_all/bsd/Rank2Cert216.lean

/-
  bsd/Rank2Cert216.lean — T39 cycle 580 (goal G294): rank ≥ 2 for 4 of the 10 congruent curves in 43801–44000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert216
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (43882, (true, 7393420225, 236, 599983546657185), (true, 713435850, 71, 18116879225760)),
   (43890, (true, 45486, 1, 2547216), (true, 450604, 3, 145545092)),
   (43945, (true, 2289169, 3, 3411427672), (true, 52855, 1, 6751800)),
   (43955, (true, 180625, 2, 17588625), (true, 5439245, 11, 2657459700))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert216

#print axioms Rank2Cert216.on_curve
#print axioms Rank2Cert216.indep
#print axioms Rank2Cert216.count

bsd/Rank2Cert217.lean

lean_all/bsd/Rank2Cert217.lean

/-
  bsd/Rank2Cert217.lean — T39 cycle 581 (goal G295): rank ≥ 2 for 2 of the 3 congruent curves in 44001–44200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert217
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (44115, (true, 748225, 4, 214740575), (false, 8100, 1, 3902850)),
   (44187, (false, 11892243, 19, 36662274000), (true, 54615132, 31, 253823825970))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert217

#print axioms Rank2Cert217.on_curve
#print axioms Rank2Cert217.indep
#print axioms Rank2Cert217.count

bsd/Rank2Cert218.lean

lean_all/bsd/Rank2Cert218.lean

/-
  bsd/Rank2Cert218.lean — T39 cycle 582 (goal G296): rank ≥ 2 for 1 of the 4 congruent curves in 44201–44400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert218
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (44274, (false, 56560223, 56, 953623353871), (true, 50901, 1, 5666085))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert218

#print axioms Rank2Cert218.on_curve
#print axioms Rank2Cert218.indep
#print axioms Rank2Cert218.count

bsd/Rank2Cert219.lean

lean_all/bsd/Rank2Cert219.lean

/-
  bsd/Rank2Cert219.lean — T39 cycle 583 (goal G297): rank ≥ 2 for 3 of the 8 congruent curves in 44401–44600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert219
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (44426, (true, 4318537, 6, 8336270501), (true, 967620440329, 948, 951015365104187035)),
   (44489, (true, 9167990681161, 2832, 27738443008957033925), (true, 396427097, 89, 3615302821416)),
   (44545, (false, 10286271, 16, 15788164833), (true, 55858959025, 902, 10046028901437825))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert219

#print axioms Rank2Cert219.on_curve
#print axioms Rank2Cert219.indep
#print axioms Rank2Cert219.count

bsd/Rank2Cert22.lean

lean_all/bsd/Rank2Cert22.lean

/-
  bsd/Rank2Cert22.lean — T39 cycle 352 (goal G80): rank ≥ 2 for 7 of the 10 congruent curves in 5001–5200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert22
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (5002, (true, 12821131402, 351, 1450064355824800), (true, 1050162561, 100, 33993164704191)),
   (5066, (true, 1519817, 2, 1873476891), (true, 17790993, 52, 47882692473)),
   (5073, (false, 938961, 65, 20749049712), (true, 46067457, 71, 260072573040)),
   (5105, (false, 476100, 149, 78201222450), (true, 2177432302591225, 226442, 100868570699082691460475)),
   (5115, (true, 95139, 1, 29302812), (true, 922560, 7, 852791400)),
   (5185, (true, 48841, 3, 3185936), (false, 144, 1, 62196)),
   (5195, (true, 9245, 1, 735300), (true, 1418215721232025, 460042, 33736374203559365351925))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 7 := rfl

end Rank2Cert22

#print axioms Rank2Cert22.on_curve
#print axioms Rank2Cert22.indep
#print axioms Rank2Cert22.count

bsd/Rank2Cert220.lean

lean_all/bsd/Rank2Cert220.lean

/-
  bsd/Rank2Cert220.lean — T39 cycle 584 (goal G298): rank ≥ 2 for 0 of the 5 congruent curves in 44601–44800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert220
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert220

#print axioms Rank2Cert220.on_curve
#print axioms Rank2Cert220.indep
#print axioms Rank2Cert220.count

bsd/Rank2Cert221.lean

lean_all/bsd/Rank2Cert221.lean

/-
  bsd/Rank2Cert221.lean — T39 cycle 585 (goal G299): rank ≥ 2 for 2 of the 5 congruent curves in 44801–45000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert221
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (44945, (false, 43264, 1, 2532816), (true, 3236629232087161, 259434, 65480255863798797080891)),
   (44970, (true, 108045, 1, 32292225), (false, 54150, 11, 1266152400))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert221

#print axioms Rank2Cert221.on_curve
#print axioms Rank2Cert221.indep
#print axioms Rank2Cert221.count

bsd/Rank2Cert222.lean

lean_all/bsd/Rank2Cert222.lean

/-
  bsd/Rank2Cert222.lean — T39 cycle 587 (goal G301): rank ≥ 2 for 4 of the 6 congruent curves in 45001–45200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert222
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (45066, (true, 94192, 1, 25384744), (true, 79402, 1, 18421264)),
   (45105, (true, 235225, 1, 111967100), (false, 33621047295, 2834, 66138070985684775)),
   (45115, (true, 1661521, 4, 1929025881), (true, 27785835, 23, 75006162000)),
   (45195, (false, 136207725, 59, 918829198800), (true, 57771, 1, 8649144))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert222

#print axioms Rank2Cert222.on_curve
#print axioms Rank2Cert222.indep
#print axioms Rank2Cert222.count

bsd/Rank2Cert223.lean

lean_all/bsd/Rank2Cert223.lean

/-
  bsd/Rank2Cert223.lean — T39 cycle 588 (goal G302): rank ≥ 2 for 4 of the 7 congruent curves in 45201–45400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert223
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (45201, (true, 19092207, 13, 76454181396), (true, 7124084409, 97, 600230849205780)),
   (45313, (false, 147112553, 963, 509680003598360), (true, 6274699369, 285, 402550267801528)),
   (45339, (false, 1096772, 1059, 53250297015322), (true, 307461, 1, 168620760)),
   (45370, (true, 28501434, 1, 152159655648), (true, 1251142917025, 2608, 1356223652646301425))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert223

#print axioms Rank2Cert223.on_curve
#print axioms Rank2Cert223.indep
#print axioms Rank2Cert223.count

bsd/Rank2Cert224.lean

lean_all/bsd/Rank2Cert224.lean

/-
  bsd/Rank2Cert224.lean — T39 cycle 589 (goal G303): rank ≥ 2 for 3 of the 6 congruent curves in 45401–45600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert224
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (45426, (false, 6226176, 943, 100794903501600), (true, 12404046273, 422, 1047254173122177)),
   (45474, (true, 4418557, 3, 9248039801), (true, 7693533, 13, 998523657)),
   (45539, (true, 3540177, 8, 3781169049), (true, 65440673, 28, 443658611313))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert224

#print axioms Rank2Cert224.on_curve
#print axioms Rank2Cert224.indep
#print axioms Rank2Cert224.count

bsd/Rank2Cert225.lean

lean_all/bsd/Rank2Cert225.lean

/-
  bsd/Rank2Cert225.lean — T39 cycle 590 (goal G304): rank ≥ 2 for 2 of the 6 congruent curves in 45601–45800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert225
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (45746, (false, 42618896, 51, 725235036920), (true, 50723576514, 1003, 4803868560338640)),
   (45771, (true, 53067, 1, 6186096), (false, 1478979, 35, 68164201944))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert225

#print axioms Rank2Cert225.on_curve
#print axioms Rank2Cert225.indep
#print axioms Rank2Cert225.count

bsd/Rank2Cert226.lean

lean_all/bsd/Rank2Cert226.lean

/-
  bsd/Rank2Cert226.lean — T39 cycle 591 (goal G305): rank ≥ 2 for 3 of the 6 congruent curves in 45801–46000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert226
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (45849, (true, 922983, 1, 885633552), (true, 4249167, 1, 8758513764)),
   (45866, (true, 167884, 1, 66171300), (true, 520182, 1, 373712976)),
   (45978, (true, 1664617, 6, 228027115), (true, 4414938025, 188, 272755728391445))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert226

#print axioms Rank2Cert226.on_curve
#print axioms Rank2Cert226.indep
#print axioms Rank2Cert226.count

bsd/Rank2Cert227.lean

lean_all/bsd/Rank2Cert227.lean

/-
  bsd/Rank2Cert227.lean — T39 cycle 592 (goal G306): rank ≥ 2 for 4 of the 12 congruent curves in 46001–46200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert227
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (46002, (false, 17424, 5, 151789176), (true, 80850, 1, 18905040)),
   (46065, (true, 2512225, 3, 3927281300), (true, 11470185, 11, 33951747600)),
   (46081, (true, 909476932225, 3926, 541717907597569305), (false, 27638073783, 3761, 108265900853149836)),
   (46169, (true, 2279817, 7, 426131376), (true, 1473548640675241, 173383, 19001752000011143237160))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert227

#print axioms Rank2Cert227.on_curve
#print axioms Rank2Cert227.indep
#print axioms Rank2Cert227.count

bsd/Rank2Cert228.lean

lean_all/bsd/Rank2Cert228.lean

/-
  bsd/Rank2Cert228.lean — T39 cycle 593 (goal G307): rank ≥ 2 for 2 of the 4 congruent curves in 46201–46400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert228
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (46274, (false, 28224, 1, 6160560), (true, 46818, 1, 1539792)),
   (46330, (true, 19690209, 16, 69743373777), (true, 8686365370, 207, 788149036577600))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert228

#print axioms Rank2Cert228.on_curve
#print axioms Rank2Cert228.indep
#print axioms Rank2Cert228.count

bsd/Rank2Cert229.lean

lean_all/bsd/Rank2Cert229.lean

/-
  bsd/Rank2Cert229.lean — T39 cycle 594 (goal G308): rank ≥ 2 for 4 of the 10 congruent curves in 46401–46600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert229
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (46434, (true, 142572, 1, 50898204), (false, 214775, 6, 768273605)),
   (46506, (true, 272322, 1, 140022216), (true, 68694, 1, 13250880)),
   (46515, (true, 47099760, 11, 320925989700), (true, 57972915, 29, 325764539100)),
   (46561, (false, 32400, 1, 6019020), (true, 821918957618641, 45309, 23403813337293953928940))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert229

#print axioms Rank2Cert229.on_curve
#print axioms Rank2Cert229.indep
#print axioms Rank2Cert229.count

bsd/Rank2Cert23.lean

lean_all/bsd/Rank2Cert23.lean

/-
  bsd/Rank2Cert23.lean — T39 cycle 353 (goal G81): rank ≥ 2 for 3 of the 7 congruent curves in 5201–5400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert23
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (5226, (true, 53868, 1, 12443508), (true, 58357, 3, 8345051)),
   (5306, (true, 287282, 7, 65500296), (false, 131383, 22, 929636379)),
   (5394, (false, 1821606, 101, 74223733320), (true, 55738, 3, 6465608))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert23

#print axioms Rank2Cert23.on_curve
#print axioms Rank2Cert23.indep
#print axioms Rank2Cert23.count

bsd/Rank2Cert230.lean

lean_all/bsd/Rank2Cert230.lean

/-
  bsd/Rank2Cert230.lean — T39 cycle 595 (goal G309): rank ≥ 2 for 3 of the 9 congruent curves in 46601–46800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert230
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (46690, (true, 117740, 1, 37088100), (false, 2158435, 7, 1114272375)),
   (46754, (false, 1354896, 25, 33976952808), (true, 116162, 1, 36242544)),
   (46793, (true, 29994313, 4, 164218863675), (true, 23646584133744274609, 21984195, 33588340974861692386222018648))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert230

#print axioms Rank2Cert230.on_curve
#print axioms Rank2Cert230.indep
#print axioms Rank2Cert230.count

bsd/Rank2Cert231.lean

lean_all/bsd/Rank2Cert231.lean

/-
  bsd/Rank2Cert231.lean — T39 cycle 596 (goal G310): rank ≥ 2 for 0 of the 6 congruent curves in 46801–47000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert231
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert231

#print axioms Rank2Cert231.on_curve
#print axioms Rank2Cert231.indep
#print axioms Rank2Cert231.count

bsd/Rank2Cert232.lean

lean_all/bsd/Rank2Cert232.lean

/-
  bsd/Rank2Cert232.lean — T39 cycle 598 (goal G311): rank ≥ 2 for 4 of the 12 congruent curves in 47001–47200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert232
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (47010, (false, 453690, 19, 11426725800), (true, 16600560, 17, 38867560200)),
   (47019, (false, 2061723, 7, 1476502056), (true, 57980188, 31, 276646947038)),
   (47066, (true, 537352522, 99, 6388660898704), (true, 3368595284239201, 171592, 178201286852700710755695)),
   (47090, (true, 65219650, 37, 79828851600), (true, 46527746265625, 8132, 316659953445220831875))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert232

#print axioms Rank2Cert232.on_curve
#print axioms Rank2Cert232.indep
#print axioms Rank2Cert232.count

bsd/Rank2Cert233.lean

lean_all/bsd/Rank2Cert233.lean

/-
  bsd/Rank2Cert233.lean — T39 cycle 599 (goal G312): rank ≥ 2 for 8 of the 14 congruent curves in 47201–47400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert233
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (47235, (false, 19459815, 46, 432471494475), (false, 15448101, 19, 28375594632)),
   (47242, (false, 4839686150, 387, 359057343310640), (false, 522193239, 110, 5313808949259)),
   (47249, (true, 130479560245225, 52089, 277441595587665931420), (true, 1339494486801025, 20419, 49018997290968115877460)),
   (47299, (false, 15876, 1, 5613930), (true, 58667, 1, 8406636)),
   (47321, (true, 1119638521, 1, 37464224517720), (false, 350050975, 104, 6986195464815)),
   (47355, (true, 1166445, 1, 1258746300), (true, 63525, 1, 10672200)),
   (47362, (false, 98192, 79, 92623725480), (true, 19882818, 17, 64305056640)),
   (47377, (true, 30747673, 24, 78560304515), (true, 1725322369, 25, 71654160149472))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 8 := rfl

end Rank2Cert233

#print axioms Rank2Cert233.on_curve
#print axioms Rank2Cert233.indep
#print axioms Rank2Cert233.count

bsd/Rank2Cert234.lean

lean_all/bsd/Rank2Cert234.lean

/-
  bsd/Rank2Cert234.lean — T39 cycle 600 (goal G313): rank ≥ 2 for 5 of the 10 congruent curves in 47401–47600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert234
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (47410, (true, 10020010, 9, 29295866600), (true, 3500296910, 223, 153066761469600)),
   (47523, (false, 5235123, 151, 2479226010120), (false, 2691072, 31, 74788464960)),
   (47561, (false, 1230848631, 161, 2450373652560), (true, 1432833029433961, 163649, 24839618230322868283080)),
   (47571, (false, 170973, 23, 10405230264), (true, 18039735677041, 13002, 68585848854875649505)),
   (47586, (true, 4201164, 7, 7162984620), (true, 26957469, 5, 139828391703))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 5 := rfl

end Rank2Cert234

#print axioms Rank2Cert234.on_curve
#print axioms Rank2Cert234.indep
#print axioms Rank2Cert234.count

bsd/Rank2Cert235.lean

lean_all/bsd/Rank2Cert235.lean

/-
  bsd/Rank2Cert235.lean — T39 cycle 601 (goal G314): rank ≥ 2 for 1 of the 3 congruent curves in 47601–47800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert235
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (47730, (true, 55470, 1, 6656400), (true, 80586, 1, 18432216))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert235

#print axioms Rank2Cert235.on_curve
#print axioms Rank2Cert235.indep
#print axioms Rank2Cert235.count

bsd/Rank2Cert236.lean

lean_all/bsd/Rank2Cert236.lean

/-
  bsd/Rank2Cert236.lean — T39 cycle 602 (goal G315): rank ≥ 2 for 3 of the 5 congruent curves in 47801–48000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert236
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (47859, (true, 207088, 1, 91688212), (true, 14615227, 17, 18053791316)),
   (47905, (true, 48841, 1, 2103036), (true, 4220095, 3, 8623924100)),
   (47994, (true, 8009868, 11, 15613095492), (true, 12216582, 1, 42699345696))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert236

#print axioms Rank2Cert236.on_curve
#print axioms Rank2Cert236.indep
#print axioms Rank2Cert236.count

bsd/Rank2Cert237.lean

lean_all/bsd/Rank2Cert237.lean

/-
  bsd/Rank2Cert237.lean — T39 cycle 604 (goal G317): rank ≥ 2 for 2 of the 9 congruent curves in 48001–48200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert237
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (48059, (true, 1167067, 1, 1259723256), (false, 88258203, 47, 554274875616)),
   (48146, (false, 350, 3, 8106560), (true, 8864982, 13, 10477123944))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert237

#print axioms Rank2Cert237.on_curve
#print axioms Rank2Cert237.indep
#print axioms Rank2Cert237.count

bsd/Rank2Cert238.lean

lean_all/bsd/Rank2Cert238.lean

/-
  bsd/Rank2Cert238.lean — T39 cycle 605 (goal G318): rank ≥ 2 for 3 of the 10 congruent curves in 48201–48400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert238
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (48202, (true, 6429698, 7, 15163831320), (true, 89100, 1, 22368060)),
   (48290, (true, 48510, 1, 1016400), (true, 93175331110, 331, 28395541549224600)),
   (48345, (true, 75547120, 39, 150622070500), (true, 15762452145, 274, 1925775106365825))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert238

#print axioms Rank2Cert238.on_curve
#print axioms Rank2Cert238.indep
#print axioms Rank2Cert238.count

bsd/Rank2Cert239.lean

lean_all/bsd/Rank2Cert239.lean

/-
  bsd/Rank2Cert239.lean — T39 cycle 606 (goal G319): rank ≥ 2 for 4 of the 9 congruent curves in 48401–48600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert239
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (48410, (true, 90846, 1, 23170056), (false, 14911907915, 1107, 7011712909749275)),
   (48490, (true, 1391290, 3, 1558244800), (true, 1151139922973209, 23034, 39046641442102660673827)),
   (48505, (true, 26435225, 16, 119989486275), (true, 51304515025, 462, 11381660013604175)),
   (48570, (true, 75645, 1, 15950025), (false, 50000430, 47, 671245896600))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert239

#print axioms Rank2Cert239.on_curve
#print axioms Rank2Cert239.indep
#print axioms Rank2Cert239.count

bsd/Rank2Cert24.lean

lean_all/bsd/Rank2Cert24.lean

/-
  bsd/Rank2Cert24.lean — T39 cycle 356 (goal G82): rank ≥ 2 for 10 of the 12 congruent curves in 5401–5600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert24
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (5465, (false, 15376, 13, 114508668), (true, 199842079369, 5251, 58680226848300372)),
   (5466, (true, 58482, 1, 14080824), (true, 29240652, 73, 13846750596)),
   (5467, (false, 5103, 4, 6237945), (true, 12476475, 19, 43514617740)),
   (5473, (true, 601947877609, 9775, 231284941301315148), (true, 1730869140625, 5117, 2269356696260362500)),
   (5474, (true, 15842, 1, 1871136), (true, 440657, 2, 292155591)),
   (5506, (false, 94864, 113, 21654263400), (false, 29506562, 137, 537985639632)),
   (5545, (false, 17424, 13, 123676476), (true, 20295025, 58, 36022948575)),
   (5546, (true, 62658, 1, 15622728), (false, 4903463, 42, 18745969411)),
   (5569, (true, 1124193841, 217, 36653234902320), (true, 19111454872225, 57288, 24418616224651698545)),
   (5593, (true, 319225, 1, 180334440), (false, 2053007, 24, 3557252105))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 10 := rfl

end Rank2Cert24

#print axioms Rank2Cert24.on_curve
#print axioms Rank2Cert24.indep
#print axioms Rank2Cert24.count

bsd/Rank2Cert240.lean

lean_all/bsd/Rank2Cert240.lean

/-
  bsd/Rank2Cert240.lean — T39 cycle 607 (goal G320): rank ≥ 2 for 2 of the 12 congruent curves in 48601–48800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert240
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (48691, (false, 2116, 1, 2237670), (true, 17730332, 19, 9782963106)),
   (48705, (true, 3480975, 7, 4727937600), (false, 1797119, 36, 84584518271))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert240

#print axioms Rank2Cert240.on_curve
#print axioms Rank2Cert240.indep
#print axioms Rank2Cert240.count

bsd/Rank2Cert241.lean

lean_all/bsd/Rank2Cert241.lean

/-
  bsd/Rank2Cert241.lean — T39 cycle 608 (goal G321): rank ≥ 2 for 3 of the 8 congruent curves in 48801–49000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert241
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (48865, (true, 5951805865, 12, 459169287879275), (true, 3399006601, 67, 197752396481976)),
   (48930, (false, 623294, 23, 20429202304), (true, 66270, 1, 11505600)),
   (48994, (false, 3080162, 33, 93483001088), (false, 9922832, 27, 108080083592))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert241

#print axioms Rank2Cert241.on_curve
#print axioms Rank2Cert241.indep
#print axioms Rank2Cert241.count

bsd/Rank2Cert242.lean

lean_all/bsd/Rank2Cert242.lean

/-
  bsd/Rank2Cert242.lean — T39 cycle 609 (goal G322): rank ≥ 2 for 1 of the 5 congruent curves in 49001–49200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert242
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (49042, (true, 337842, 1, 194287920), (true, 1430128, 3, 1626770600))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert242

#print axioms Rank2Cert242.on_curve
#print axioms Rank2Cert242.indep
#print axioms Rank2Cert242.count

bsd/Rank2Cert243.lean

lean_all/bsd/Rank2Cert243.lean

/-
  bsd/Rank2Cert243.lean — T39 cycle 610 (goal G323): rank ≥ 2 for 1 of the 11 congruent curves in 49201–49400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert243
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (49259, (false, 25727551, 100, 2495121240543), (false, 545840932, 131, 15080545237758))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert243

#print axioms Rank2Cert243.on_curve
#print axioms Rank2Cert243.indep
#print axioms Rank2Cert243.count

bsd/Rank2Cert244.lean

lean_all/bsd/Rank2Cert244.lean

/-
  bsd/Rank2Cert244.lean — T39 cycle 611 (goal G324): rank ≥ 2 for 3 of the 6 congruent curves in 49401–49600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert244
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (49449, (false, 177277775, 156, 15847818546455), (true, 1889273770213729, 75145, 81216912892384678815692)),
   (49465, (true, 35406067225, 843, 796289747047300), (true, 164521905769, 1533, 47222233059888172)),
   (49569, (true, 145119, 1, 51957360), (true, 26223600, 23, 1482944580))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert244

#print axioms Rank2Cert244.on_curve
#print axioms Rank2Cert244.indep
#print axioms Rank2Cert244.count

bsd/Rank2Cert245.lean

lean_all/bsd/Rank2Cert245.lean

/-
  bsd/Rank2Cert245.lean — T39 cycle 612 (goal G325): rank ≥ 2 for 3 of the 8 congruent curves in 49601–49800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert245
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (49601, (true, 21989627521, 136, 3257980736343615), (false, 519726033, 5609, 35575296127909824)),
   (49714, (false, 19545687, 1702, 636683046041973), (true, 237917, 1, 113486409)),
   (49794, (true, 9945097, 14, 6028787899), (true, 129005243929, 1472, 25401143967484285))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert245

#print axioms Rank2Cert245.on_curve
#print axioms Rank2Cert245.indep
#print axioms Rank2Cert245.count

bsd/Rank2Cert246.lean

lean_all/bsd/Rank2Cert246.lean

/-
  bsd/Rank2Cert246.lean — T39 cycle 613 (goal G326): rank ≥ 2 for 1 of the 9 congruent curves in 49801–50000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert246
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (49929, (false, 47753, 1, 3185936), (false, 946832, 13, 8158767092))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert246

#print axioms Rank2Cert246.on_curve
#print axioms Rank2Cert246.indep
#print axioms Rank2Cert246.count

bsd/Rank2Cert247.lean

lean_all/bsd/Rank2Cert247.lean

/-
  bsd/Rank2Cert247.lean — T39 cycle 615 (goal G328): rank ≥ 2 for 1 of the 6 congruent curves in 50001–50200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert247
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (50089, (true, 78185666689, 1145, 11865521232915012), (true, 172022692995754225, 1439633, 56891118027078147327501180))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert247

#print axioms Rank2Cert247.on_curve
#print axioms Rank2Cert247.indep
#print axioms Rank2Cert247.count

bsd/Rank2Cert248.lean

lean_all/bsd/Rank2Cert248.lean

/-
  bsd/Rank2Cert248.lean — T39 cycle 616 (goal G329): rank ≥ 2 for 2 of the 9 congruent curves in 50201–50400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert248
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (50235, (false, 41616, 13, 1731881052), (true, 109589109315, 689, 35409265139587500)),
   (50266, (true, 61525625, 2, 482593793325), (true, 25553676154, 513, 3494941523114320))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert248

#print axioms Rank2Cert248.on_curve
#print axioms Rank2Cert248.indep
#print axioms Rank2Cert248.count

bsd/Rank2Cert249.lean

lean_all/bsd/Rank2Cert249.lean

/-
  bsd/Rank2Cert249.lean — T39 cycle 617 (goal G330): rank ≥ 2 for 2 of the 6 congruent curves in 50401–50600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert249
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (50466, (true, 2334462, 1, 3565978416), (true, 15730067158, 289, 1900712847856888)),
   (50585, (true, 456775, 3, 25081100), (true, 3920023809025, 1119, 7760261486704544900))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert249

#print axioms Rank2Cert249.on_curve
#print axioms Rank2Cert249.indep
#print axioms Rank2Cert249.count

bsd/Rank2Cert25.lean

lean_all/bsd/Rank2Cert25.lean

/-
  bsd/Rank2Cert25.lean — T39 cycle 357 (goal G83): rank ≥ 2 for 3 of the 7 congruent curves in 5601–5800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert25
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (5610, (true, 69360, 1, 18207000), (true, 24684, 1, 3776652)),
   (5738, (true, 93100, 1, 28352940), (false, 4750593031, 950, 142078120114797)),
   (5794, (true, 8544355763329, 8854, 24940461176700865185), (false, 50783042, 2161, 192817574695152))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert25

#print axioms Rank2Cert25.on_curve
#print axioms Rank2Cert25.indep
#print axioms Rank2Cert25.count

bsd/Rank2Cert250.lean

lean_all/bsd/Rank2Cert250.lean

/-
  bsd/Rank2Cert250.lean — T39 cycle 618 (goal G331): rank ≥ 2 for 1 of the 5 congruent curves in 50601–50800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert250
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (50609, (true, 52441, 1, 3146460), (true, 1075369, 4, 733807125))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert250

#print axioms Rank2Cert250.on_curve
#print axioms Rank2Cert250.indep
#print axioms Rank2Cert250.count

bsd/Rank2Cert251.lean

lean_all/bsd/Rank2Cert251.lean

/-
  bsd/Rank2Cert251.lean — T39 cycle 619 (goal G332): rank ≥ 2 for 2 of the 9 congruent curves in 50801–51000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert251
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (50915, (true, 112608085, 23, 1160278095900), (true, 19382780435, 581, 1247588212903500)),
   (50955, (false, 11265005, 107, 1957668142600), (true, 545025, 2, 373174425))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert251

#print axioms Rank2Cert251.on_curve
#print axioms Rank2Cert251.indep
#print axioms Rank2Cert251.count

bsd/Rank2Cert252.lean

lean_all/bsd/Rank2Cert252.lean

/-
  bsd/Rank2Cert252.lean — T39 cycle 620 (goal G333): rank ≥ 2 for 3 of the 6 congruent curves in 51001–51200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert252
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (51017, (false, 419033, 7, 1595314680), (true, 189351171025, 1843, 33212529700542120)),
   (51051, (true, 77077, 1, 16032016), (true, 1042899, 1, 1063756980)),
   (51179, (true, 58621, 1, 6921060), (true, 3476229736225, 7974, 2279192872935820105))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert252

#print axioms Rank2Cert252.on_curve
#print axioms Rank2Cert252.indep
#print axioms Rank2Cert252.count

bsd/Rank2Cert253.lean

lean_all/bsd/Rank2Cert253.lean

/-
  bsd/Rank2Cert253.lean — T39 cycle 621 (goal G334): rank ≥ 2 for 3 of the 8 congruent curves in 51201–51400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert253
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (51330, (true, 525625, 2, 350802125), (true, 53100, 1, 3132900)),
   (51339, (false, 20736, 1, 6762960), (true, 42047924475, 251, 8596628744992020)),
   (51395, (true, 46677401555, 757, 7823762376152100), (true, 79910809225, 502, 22290908237938275))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert253

#print axioms Rank2Cert253.on_curve
#print axioms Rank2Cert253.indep
#print axioms Rank2Cert253.count

bsd/Rank2Cert254.lean

lean_all/bsd/Rank2Cert254.lean

/-
  bsd/Rank2Cert254.lean — T39 cycle 622 (goal G335): rank ≥ 2 for 2 of the 6 congruent curves in 51401–51600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert254
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (51569, (true, 18503008769, 374, 2317753445990745), (true, 34374592, 9, 200044194616)),
   (51585, (false, 4335, 2, 13582575), (true, 4453656316, 229, 236107877480414))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert254

#print axioms Rank2Cert254.on_curve
#print axioms Rank2Cert254.indep
#print axioms Rank2Cert254.count

bsd/Rank2Cert255.lean

lean_all/bsd/Rank2Cert255.lean

/-
  bsd/Rank2Cert255.lean — T39 cycle 623 (goal G336): rank ≥ 2 for 3 of the 7 congruent curves in 51601–51800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert255
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (51610, (true, 71983615210, 387, 19201366656690400), (true, 447487413025, 1936, 269930910046655025)),
   (51649, (true, 3553225, 3, 6640266880), (false, 14400, 1, 5952120)),
   (51779, (true, 316771, 1, 175888440), (true, 7682803859076025, 122404, 669968086377166080554835))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert255

#print axioms Rank2Cert255.on_curve
#print axioms Rank2Cert255.indep
#print axioms Rank2Cert255.count

bsd/Rank2Cert256.lean

lean_all/bsd/Rank2Cert256.lean

/-
  bsd/Rank2Cert256.lean — T39 cycle 624 (goal G337): rank ≥ 2 for 1 of the 8 congruent curves in 51801–52000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert256
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (51906, (false, 1778498, 9, 5080627504), (false, 5552384, 19, 42170507360))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert256

#print axioms Rank2Cert256.on_curve
#print axioms Rank2Cert256.indep
#print axioms Rank2Cert256.count

bsd/Rank2Cert257.lean

lean_all/bsd/Rank2Cert257.lean

/-
  bsd/Rank2Cert257.lean — T39 cycle 625 (goal G338): rank ≥ 2 for 6 of the 9 congruent curves in 52001–52200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert257
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (52026, (false, 55223, 2, 47150299), (true, 1628901, 1, 2077879725)),
   (52049, (true, 2563201, 7, 409599840), (false, 3887, 12, 467285273)),
   (52059, (true, 231361, 2, 48496825), (true, 502768, 1, 354577132)),
   (52098, (true, 6295650, 7, 14439595680), (true, 12787954800625, 13166, 32377443636473002025)),
   (52122, (true, 95922, 1, 24939720), (false, 33301079, 30, 190657742219)),
   (52193, (false, 1895225, 27, 52315544080), (false, 11257616, 15, 11215786652))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 6 := rfl

end Rank2Cert257

#print axioms Rank2Cert257.on_curve
#print axioms Rank2Cert257.indep
#print axioms Rank2Cert257.count

bsd/Rank2Cert258.lean

lean_all/bsd/Rank2Cert258.lean

/-
  bsd/Rank2Cert258.lean — T39 cycle 626 (goal G339): rank ≥ 2 for 5 of the 7 congruent curves in 52201–52400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert258
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (52210, (false, 1643166, 11, 7819314048), (false, 24652435, 29, 180407105025)),
   (52297, (true, 2302153, 1, 3492120240), (true, 2737850076025, 6103, 3183445610728131720)),
   (52338, (false, 278784, 25, 17270912736), (true, 81862, 1, 18009640)),
   (52346, (false, 51842, 1, 1649928), (false, 24714487, 106, 2921374804677)),
   (52377, (true, 93799225, 6, 908261924635), (false, 248590592, 77, 2934473146480))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 5 := rfl

end Rank2Cert258

#print axioms Rank2Cert258.on_curve
#print axioms Rank2Cert258.indep
#print axioms Rank2Cert258.count

bsd/Rank2Cert259.lean

lean_all/bsd/Rank2Cert259.lean

/-
  bsd/Rank2Cert259.lean — T39 cycle 627 (goal G340): rank ≥ 2 for 0 of the 4 congruent curves in 52401–52600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert259
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert259

#print axioms Rank2Cert259.on_curve
#print axioms Rank2Cert259.indep
#print axioms Rank2Cert259.count

bsd/Rank2Cert26.lean

lean_all/bsd/Rank2Cert26.lean

/-
  bsd/Rank2Cert26.lean — T39 cycle 358 (goal G84): rank ≥ 2 for 4 of the 5 congruent curves in 5801–6000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert26
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (5809, (false, 3600, 1, 273540), (true, 3311275225, 127, 190466673666780)),
   (5865, (true, 180625, 2, 76115375), (true, 7935, 1, 476100)),
   (5962, (true, 1086338, 7, 1090553640), (true, 121977900, 49, 1337858955180)),
   (5986, (false, 4418, 1, 268464), (true, 600281, 10, 34781571))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert26

#print axioms Rank2Cert26.on_curve
#print axioms Rank2Cert26.indep
#print axioms Rank2Cert26.count

bsd/Rank2Cert260.lean

lean_all/bsd/Rank2Cert260.lean

/-
  bsd/Rank2Cert260.lean — T39 cycle 628 (goal G341): rank ≥ 2 for 2 of the 9 congruent curves in 52601–52800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert260
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (52611, (true, 1482627, 1, 1804156704), (false, 38414493, 47, 679823629056)),
   (52658, (true, 229842, 1, 107259600), (true, 14726273, 16, 22748420415))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert260

#print axioms Rank2Cert260.on_curve
#print axioms Rank2Cert260.indep
#print axioms Rank2Cert260.count

bsd/Rank2Cert261.lean

lean_all/bsd/Rank2Cert261.lean

/-
  bsd/Rank2Cert261.lean — T39 cycle 629 (goal G342): rank ≥ 2 for 1 of the 4 congruent curves in 52801–53000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert261
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (52899, (true, 53067, 1, 971964), (false, 996611, 5, 867839588))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert261

#print axioms Rank2Cert261.on_curve
#print axioms Rank2Cert261.indep
#print axioms Rank2Cert261.count

bsd/Rank2Cert262.lean

lean_all/bsd/Rank2Cert262.lean

/-
  bsd/Rank2Cert262.lean — T39 cycle 630 (goal G343): rank ≥ 2 for 3 of the 8 congruent curves in 53001–53200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert262
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (53074, (true, 58482, 1, 5939856), (true, 29275904, 23, 44882439840)),
   (53105, (true, 64220, 1, 9151350), (true, 156863726475625, 54309, 106871052051068851000)),
   (53193, (true, 1266993, 1, 1424879820), (true, 30131752225, 39, 5230401355000340))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert262

#print axioms Rank2Cert262.on_curve
#print axioms Rank2Cert262.indep
#print axioms Rank2Cert262.count

bsd/Rank2Cert263.lean

lean_all/bsd/Rank2Cert263.lean

/-
  bsd/Rank2Cert263.lean — T39 cycle 631 (goal G344): rank ≥ 2 for 2 of the 2 congruent curves in 53201–53400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert263
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (53265, (true, 3845521, 5, 7074511444), (true, 320589025, 13, 5737883211100)),
   (53395, (true, 8036226025, 278, 618175390806675), (true, 758907549409, 2886, 535712202263572073))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert263

#print axioms Rank2Cert263.on_curve
#print axioms Rank2Cert263.indep
#print axioms Rank2Cert263.count

bsd/Rank2Cert264.lean

lean_all/bsd/Rank2Cert264.lean

/-
  bsd/Rank2Cert264.lean — T39 cycle 632 (goal G345): rank ≥ 2 for 1 of the 7 congruent curves in 53401–53600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert264
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (53570, (false, 2035220, 23, 40323673500), (true, 1097966320, 17, 36378128272200))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert264

#print axioms Rank2Cert264.on_curve
#print axioms Rank2Cert264.indep
#print axioms Rank2Cert264.count

bsd/Rank2Cert265.lean

lean_all/bsd/Rank2Cert265.lean

/-
  bsd/Rank2Cert265.lean — T39 cycle 633 (goal G346): rank ≥ 2 for 5 of the 10 congruent curves in 53601–53800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert265
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (53601, (false, 6346457, 11, 3367714688), (false, 14263952, 31, 186935646772)),
   (53618, (true, 367353, 2, 180765165), (true, 12193260853225, 3232, 42532523339929680885)),
   (53634, (true, 34245309, 25, 40991634873), (false, 92740703699, 1483, 22197421351524545)),
   (53690, (false, 4747210, 11, 9662811600), (true, 8236046, 11, 14528385144)),
   (53737, (false, 1245456, 5, 562036572), (true, 110019121, 45, 170094910456))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 5 := rfl

end Rank2Cert265

#print axioms Rank2Cert265.on_curve
#print axioms Rank2Cert265.indep
#print axioms Rank2Cert265.count

bsd/Rank2Cert266.lean

lean_all/bsd/Rank2Cert266.lean

/-
  bsd/Rank2Cert266.lean — T39 cycle 634 (goal G347): rank ≥ 2 for 3 of the 10 congruent curves in 53801–54000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert266
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (53859, (true, 99940009, 18, 983752135795), (true, 7096546081, 350, 220173134246729)),
   (53970, (true, 1387029, 5, 378658917), (true, 666030, 1, 541764000)),
   (53985, (true, 55815, 1, 3348900), (true, 44160921025, 847, 4458969600763600))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert266

#print axioms Rank2Cert266.on_curve
#print axioms Rank2Cert266.indep
#print axioms Rank2Cert266.count

bsd/Rank2Cert267.lean

lean_all/bsd/Rank2Cert267.lean

/-
  bsd/Rank2Cert267.lean — T39 cycle 636 (goal G349): rank ≥ 2 for 2 of the 7 congruent curves in 54001–54200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert267
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (54033, (true, 54208, 1, 1013320), (true, 148412593, 52, 317576927465)),
   (54131, (false, 3459463, 232, 5419086194739), (true, 376475, 1, 228595620))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert267

#print axioms Rank2Cert267.on_curve
#print axioms Rank2Cert267.indep
#print axioms Rank2Cert267.count

bsd/Rank2Cert268.lean

lean_all/bsd/Rank2Cert268.lean

/-
  bsd/Rank2Cert268.lean — T39 cycle 637 (goal G350): rank ≥ 2 for 3 of the 6 congruent curves in 54201–54400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert268
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (54210, (false, 404010, 7, 1668742200), (false, 975, 8, 108333225)),
   (54385, (true, 10539280921, 58, 1081810088651019), (true, 79605801025, 658, 21455331085304775)),
   (54395, (true, 19932220, 19, 15269504250), (false, 4295345680, 359, 363116564106900))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert268

#print axioms Rank2Cert268.on_curve
#print axioms Rank2Cert268.indep
#print axioms Rank2Cert268.count

bsd/Rank2Cert269.lean

lean_all/bsd/Rank2Cert269.lean

/-
  bsd/Rank2Cert269.lean — T39 cycle 638 (goal G351): rank ≥ 2 for 2 of the 7 congruent curves in 54401–54600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert269
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (54417, (true, 89733633, 32, 666272225025), (false, 577915136, 1185, 1836922822008112)),
   (54426, (true, 58482, 1, 5175144), (true, 112908, 1, 33240348))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert269

#print axioms Rank2Cert269.on_curve
#print axioms Rank2Cert269.indep
#print axioms Rank2Cert269.count

bsd/Rank2Cert27.lean

lean_all/bsd/Rank2Cert27.lean

/-
  bsd/Rank2Cert27.lean — T39 cycle 361 (goal G85): rank ≥ 2 for 7 of the 9 congruent curves in 6001–6200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert27
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (6001, (true, 124609, 4, 28037025), (true, 40817, 1, 8156736)),
   (6010, (false, 114490, 7, 91806000), (true, 717085482481, 8424, 488158700102581321)),
   (6058, (true, 4264650, 23, 5810706720), (true, 198285076587015625, 196238, 88294721998680969423742875)),
   (6065, (true, 1471369, 7, 1747986372), (true, 4779579750625, 27837, 1901689318142850500)),
   (6083, (false, 2630383, 32, 9157530375), (true, 145475, 1, 55437360)),
   (6090, (true, 7350, 1, 352800), (true, 6525, 1, 189225)),
   (6106, (true, 299538, 7, 7854552), (false, 7967975, 114, 222864046685))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 7 := rfl

end Rank2Cert27

#print axioms Rank2Cert27.on_curve
#print axioms Rank2Cert27.indep
#print axioms Rank2Cert27.count

bsd/Rank2Cert270.lean

lean_all/bsd/Rank2Cert270.lean

/-
  bsd/Rank2Cert270.lean — T39 cycle 639 (goal G352): rank ≥ 2 for 3 of the 8 congruent curves in 54601–54800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert270
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (54641, (false, 48400, 1, 5578980), (true, 9962638889, 23, 994396885705860)),
   (54723, (false, 49284, 5, 303515514), (true, 2053056, 5, 2193433704)),
   (54786, (false, 268203, 19, 10241620389), (false, 44246687, 156, 8863798665617))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert270

#print axioms Rank2Cert270.on_curve
#print axioms Rank2Cert270.indep
#print axioms Rank2Cert270.count

bsd/Rank2Cert271.lean

lean_all/bsd/Rank2Cert271.lean

/-
  bsd/Rank2Cert271.lean — T39 cycle 640 (goal G353): rank ≥ 2 for 3 of the 6 congruent curves in 54801–55000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert271
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (54834, (false, 467856, 5, 881362728), (true, 57798, 1, 4392648)),
   (54881, (true, 894616773921025, 121072, 11705853882809793736065), (true, 174935567195041, 47967, 1601304376931596373120)),
   (54969, (true, 14326177, 16, 10167737329), (true, 75700368769, 432, 20635840487700415))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert271

#print axioms Rank2Cert271.on_curve
#print axioms Rank2Cert271.indep
#print axioms Rank2Cert271.count

bsd/Rank2Cert272.lean

lean_all/bsd/Rank2Cert272.lean

/-
  bsd/Rank2Cert272.lean — T39 cycle 642 (goal G354): rank ≥ 2 for 1 of the 4 congruent curves in 55001–55200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert272
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (55139, (false, 100071727, 128, 8981596009113), (true, 125150072241997377481, 14750198, 1393612924649838214138816182525))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert272

#print axioms Rank2Cert272.on_curve
#print axioms Rank2Cert272.indep
#print axioms Rank2Cert272.count

bsd/Rank2Cert273.lean

lean_all/bsd/Rank2Cert273.lean

/-
  bsd/Rank2Cert273.lean — T39 cycle 643 (goal G355): rank ≥ 2 for 2 of the 5 congruent curves in 55201–55400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert273
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (55290, (true, 56454, 1, 2709792), (true, 3230956365, 59, 183326282582625)),
   (55361, (true, 696348, 1, 579245478), (true, 422754721, 10, 8691514482231))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert273

#print axioms Rank2Cert273.on_curve
#print axioms Rank2Cert273.indep
#print axioms Rank2Cert273.count

bsd/Rank2Cert274.lean

lean_all/bsd/Rank2Cert274.lean

/-
  bsd/Rank2Cert274.lean — T39 cycle 644 (goal G356): rank ≥ 2 for 2 of the 7 congruent curves in 55401–55600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert274
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (55426, (false, 219303, 2, 15238461), (true, 21180650, 19, 31974309240)),
   (55555, (true, 75645, 1, 14120400), (true, 230625, 2, 29627625))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert274

#print axioms Rank2Cert274.on_curve
#print axioms Rank2Cert274.indep
#print axioms Rank2Cert274.count

bsd/Rank2Cert275.lean

lean_all/bsd/Rank2Cert275.lean

/-
  bsd/Rank2Cert275.lean — T39 cycle 645 (goal G357): rank ≥ 2 for 3 of the 8 congruent curves in 55601–55800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert275
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (55705, (false, 1002001, 17, 16083599532), (true, 29156684494249, 15763, 138566584049350508532)),
   (55731, (false, 242064, 37, 37537314660), (true, 238918849, 2, 3692967041465)),
   (55794, (false, 305762, 3, 220254992), (false, 32994366096, 1271, 15235477826247000))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert275

#print axioms Rank2Cert275.on_curve
#print axioms Rank2Cert275.indep
#print axioms Rank2Cert275.count

bsd/Rank2Cert276.lean

lean_all/bsd/Rank2Cert276.lean

/-
  bsd/Rank2Cert276.lean — T39 cycle 646 (goal G358): rank ≥ 2 for 7 of the 10 congruent curves in 55801–56000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert276
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (55801, (false, 469409, 3, 122309560), (true, 254370601, 57, 2845707342560)),
   (55859, (true, 803119155173449, 119892, 503786614059967208005), (false, 1162834067, 191, 57065021524728)),
   (55913, (true, 1590121, 5, 955843044), (false, 17408, 3, 66354400)),
   (55946, (true, 11943404, 7, 40173507660), (true, 668619625333, 2983, 364972514348315643)),
   (55970, (false, 334080, 11, 3909640800), (true, 6999832225, 6, 585640938841775)),
   (55986, (true, 74629338, 19, 620617574808), (false, 709838, 7, 2232579824)),
   (55993, (true, 613312, 3, 273763000), (true, 423907126890025, 40067, 8529346362297982634820))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 7 := rfl

end Rank2Cert276

#print axioms Rank2Cert276.on_curve
#print axioms Rank2Cert276.indep
#print axioms Rank2Cert276.count

bsd/Rank2Cert277.lean

lean_all/bsd/Rank2Cert277.lean

/-
  bsd/Rank2Cert277.lean — T39 cycle 647 (goal G359): rank ≥ 2 for 2 of the 7 congruent curves in 56001–56200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert277
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (56155, (true, 320589025, 38, 5553497431575), (true, 324559861647025, 47276, 5392247237089220081175)),
   (56161, (false, 204687, 4, 395849097), (true, 61436269521101281, 170561, 15222417737470335377150880))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert277

#print axioms Rank2Cert277.on_curve
#print axioms Rank2Cert277.indep
#print axioms Rank2Cert277.count

bsd/Rank2Cert278.lean

lean_all/bsd/Rank2Cert278.lean

/-
  bsd/Rank2Cert278.lean — T39 cycle 648 (goal G360): rank ≥ 2 for 0 of the 4 congruent curves in 56201–56400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert278
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert278

#print axioms Rank2Cert278.on_curve
#print axioms Rank2Cert278.indep
#print axioms Rank2Cert278.count

bsd/Rank2Cert279.lean

lean_all/bsd/Rank2Cert279.lean

/-
  bsd/Rank2Cert279.lean — T39 cycle 649 (goal G361): rank ≥ 2 for 2 of the 4 congruent curves in 56401–56600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert279
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (56459, (true, 255025, 2, 59828865), (true, 6769504729, 138, 549904212716885)),
   (56474, (true, 69938, 1, 10910328), (false, 43219975, 38, 454627200915))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert279

#print axioms Rank2Cert279.on_curve
#print axioms Rank2Cert279.indep
#print axioms Rank2Cert279.count

bsd/Rank2Cert28.lean

lean_all/bsd/Rank2Cert28.lean

/-
  bsd/Rank2Cert28.lean — T39 cycle 366 (goal G89): rank ≥ 2 for 6 of the 8 congruent curves in 6201–6400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert28
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (6251, (true, 3895612, 9, 7623665686), (false, 1067211, 17, 1505803200)),
   (6290, (true, 855625, 2, 791110875), (true, 601426, 9, 247841776)),
   (6305, (true, 9409, 1, 677448), (true, 195447809470225, 19239, 2732217378455758684400)),
   (6346, (true, 6498, 1, 112632), (true, 1468396, 1, 1779347340)),
   (6355, (true, 8405, 1, 504300), (true, 49600, 1, 10955400)),
   (6386, (false, 19375, 16, 227540775), (false, 23297678, 1287, 51055351890704))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 6 := rfl

end Rank2Cert28

#print axioms Rank2Cert28.on_curve
#print axioms Rank2Cert28.indep
#print axioms Rank2Cert28.count

bsd/Rank2Cert280.lean

lean_all/bsd/Rank2Cert280.lean

/-
  bsd/Rank2Cert280.lean — T39 cycle 650 (goal G362): rank ≥ 2 for 5 of the 7 congruent curves in 56601–56800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert280
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (56715, (false, 15, 2, 878625), (true, 18810475, 17, 40028690800)),
   (56721, (true, 28984431, 17, 128690873640), (true, 103929509161, 154, 33502092435464435)),
   (56730, (true, 167445, 1, 64466325), (true, 58621, 1, 3575881)),
   (56769, (true, 733552, 1, 626385844), (true, 67293029281, 693, 15959592840264940)),
   (56785, (true, 124025, 1, 38831100), (true, 267787315009707025, 268836, 138558761480874939906112775))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 5 := rfl

end Rank2Cert280

#print axioms Rank2Cert280.on_curve
#print axioms Rank2Cert280.indep
#print axioms Rank2Cert280.count

bsd/Rank2Cert281.lean

lean_all/bsd/Rank2Cert281.lean

/-
  bsd/Rank2Cert281.lean — T39 cycle 651 (goal G363): rank ≥ 2 for 3 of the 7 congruent curves in 56801–57000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert281
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (56865, (true, 25038960, 1, 125291990700), (true, 155002673865, 536, 60685232507432325)),
   (56899, (true, 2459068921, 152, 103055215886835), (false, 100842167987, 3961, 281673989283164808)),
   (56994, (true, 62658, 1, 6516432), (true, 59248, 1, 3939992))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert281

#print axioms Rank2Cert281.on_curve
#print axioms Rank2Cert281.indep
#print axioms Rank2Cert281.count

bsd/Rank2Cert282.lean

lean_all/bsd/Rank2Cert282.lean

/-
  bsd/Rank2Cert282.lean — T39 cycle 652 (goal G364): rank ≥ 2 for 2 of the 5 congruent curves in 57001–57200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert282
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (57001, (true, 121801, 1, 37566360), (false, 31074368, 693, 152598342539656)),
   (57090, (false, 756150, 53, 139447564200), (true, 59290, 1, 3896200))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert282

#print axioms Rank2Cert282.on_curve
#print axioms Rank2Cert282.indep
#print axioms Rank2Cert282.count

bsd/Rank2Cert283.lean

lean_all/bsd/Rank2Cert283.lean

/-
  bsd/Rank2Cert283.lean — T39 cycle 653 (goal G365): rank ≥ 2 for 2 of the 5 congruent curves in 57201–57400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert283
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (57226, (false, 179114, 17, 6998911920), (true, 69778942, 21, 543432825944)),
   (57387, (true, 108288, 1, 30219120), (false, 210286725, 149, 18221923389780))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert283

#print axioms Rank2Cert283.on_curve
#print axioms Rank2Cert283.indep
#print axioms Rank2Cert283.count

bsd/Rank2Cert284.lean

lean_all/bsd/Rank2Cert284.lean

/-
  bsd/Rank2Cert284.lean — T39 cycle 654 (goal G366): rank ≥ 2 for 4 of the 8 congruent curves in 57401–57600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert284
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (57491, (true, 3844243, 1, 7536464832), (true, 22602176, 17, 72849638520)),
   (57545, (false, 34969, 25, 6725568696), (true, 255904009, 33, 3969054318752)),
   (57570, (true, 255025, 2, 55340425), (true, 61206, 1, 5141304)),
   (57585, (true, 3045025, 7, 1997536400), (true, 223260, 1, 101921850))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert284

#print axioms Rank2Cert284.on_curve
#print axioms Rank2Cert284.indep
#print axioms Rank2Cert284.count

bsd/Rank2Cert285.lean

lean_all/bsd/Rank2Cert285.lean

/-
  bsd/Rank2Cert285.lean — T39 cycle 655 (goal G367): rank ≥ 2 for 2 of the 4 congruent curves in 57601–57800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert285
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (57715, (true, 235225, 2, 21875925), (true, 2719201, 4, 4217480751)),
   (57739, (false, 154973071, 230, 37974609989217), (true, 34144592779, 131, 6306667906980540))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert285

#print axioms Rank2Cert285.on_curve
#print axioms Rank2Cert285.indep
#print axioms Rank2Cert285.count

bsd/Rank2Cert286.lean

lean_all/bsd/Rank2Cert286.lean

/-
  bsd/Rank2Cert286.lean — T39 cycle 656 (goal G368): rank ≥ 2 for 2 of the 7 congruent curves in 57801–58000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert286
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (57930, (true, 1507005, 1, 1848633975), (false, 28123350, 79, 1911495646800)),
   (57939, (true, 504525, 1, 355992840), (true, 162288813, 49, 1064749260168))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert286

#print axioms Rank2Cert286.on_curve
#print axioms Rank2Cert286.indep
#print axioms Rank2Cert286.count

bsd/Rank2Cert287.lean

lean_all/bsd/Rank2Cert287.lean

/-
  bsd/Rank2Cert287.lean — T39 cycle 658 (goal G370): rank ≥ 2 for 2 of the 3 congruent curves in 58001–58200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert287
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (58123, (true, 77077, 1, 14054040), (true, 3771297, 8, 1205333745)),
   (58145, (false, 44100, 1, 7957950), (true, 27602652545, 113, 4584258149530800))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert287

#print axioms Rank2Cert287.on_curve
#print axioms Rank2Cert287.indep
#print axioms Rank2Cert287.count

bsd/Rank2Cert288.lean

lean_all/bsd/Rank2Cert288.lean

/-
  bsd/Rank2Cert288.lean — T39 cycle 659 (goal G371): rank ≥ 2 for 5 of the 7 congruent curves in 58201–58400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert288
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (58242, (false, 5105168, 11, 10976763320), (false, 13468318608, 4393, 130432012708034520)),
   (58290, (true, 610566, 1, 474909336), (true, 14413725, 7, 53637150825)),
   (58355, (false, 17424, 1, 7351476), (true, 22515025, 2, 106828052175)),
   (58361, (false, 45968, 1, 7709364), (true, 2311606107097225, 116521, 104406689269417718604720)),
   (58377, (true, 18622263, 13, 68157482580), (true, 29723484025, 259, 5079808936530980))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 5 := rfl

end Rank2Cert288

#print axioms Rank2Cert288.on_curve
#print axioms Rank2Cert288.indep
#print axioms Rank2Cert288.count

bsd/Rank2Cert289.lean

lean_all/bsd/Rank2Cert289.lean

/-
  bsd/Rank2Cert289.lean — T39 cycle 660 (goal G372): rank ≥ 2 for 2 of the 5 congruent curves in 58401–58600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert289
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (58466, (true, 353648, 1, 207414552), (true, 97216, 1, 24217200)),
   (58546, (true, 95922, 1, 23532864), (false, 9251728, 49, 426636627864))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert289

#print axioms Rank2Cert289.on_curve
#print axioms Rank2Cert289.indep
#print axioms Rank2Cert289.count

bsd/Rank2Cert29.lean

lean_all/bsd/Rank2Cert29.lean

/-
  bsd/Rank2Cert29.lean — T39 cycle 367 (goal G90): rank ≥ 2 for 8 of the 9 congruent curves in 6401–6600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert29
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (6402, (true, 235225, 4, 102699235), (true, 41250, 1, 8276400)),
   (6409, (true, 243049, 6, 37672595), (true, 900601, 6, 826147205)),
   (6411, (false, 507, 7, 7073352), (true, 3931505314748089, 627724, 188889164039004662736685)),
   (6441, (true, 319225, 7, 27070280), (true, 15472137, 49, 1868772816)),
   (6531, (true, 5475603, 11, 12678772932), (true, 141148, 3, 48211982)),
   (6545, (true, 7225, 1, 260100), (true, 10780, 1, 889350)),
   (6555, (false, 1734605, 567, 2775485505500), (true, 10051, 1, 763876)),
   (6594, (true, 99372, 1, 31256316), (true, 27951966, 65, 12000710736))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 8 := rfl

end Rank2Cert29

#print axioms Rank2Cert29.on_curve
#print axioms Rank2Cert29.indep
#print axioms Rank2Cert29.count

bsd/Rank2Cert290.lean

lean_all/bsd/Rank2Cert290.lean

/-
  bsd/Rank2Cert290.lean — T39 cycle 661 (goal G373): rank ≥ 2 for 3 of the 9 congruent curves in 58601–58800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert290
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (58674, (true, 63525, 1, 6136515), (true, 52161186, 19, 344263827600)),
   (58729, (true, 727529, 1, 618522960), (false, 179987456, 65, 2291457735072)),
   (58786, (false, 234650, 251, 1794037975080), (false, 40079702, 27, 96047200808))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert290

#print axioms Rank2Cert290.on_curve
#print axioms Rank2Cert290.indep
#print axioms Rank2Cert290.count

bsd/Rank2Cert291.lean

lean_all/bsd/Rank2Cert291.lean

/-
  bsd/Rank2Cert291.lean — T39 cycle 662 (goal G374): rank ≥ 2 for 2 of the 6 congruent curves in 58801–59000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert291
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (58857, (false, 81550847, 52, 1234181553535), (true, 2423392725625, 4278, 3379406345966764475)),
   (58929, (true, 2009631, 5, 1937638404), (true, 16685337783361, 6378, 67448688856840669625))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert291

#print axioms Rank2Cert291.on_curve
#print axioms Rank2Cert291.indep
#print axioms Rank2Cert291.count

bsd/Rank2Cert292.lean

lean_all/bsd/Rank2Cert292.lean

/-
  bsd/Rank2Cert292.lean — T39 cycle 663 (goal G375): rank ≥ 2 for 3 of the 7 congruent curves in 59001–59200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert292
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (59010, (true, 59290, 1, 1401400), (false, 2822414, 13, 16069178384)),
   (59185, (false, 11027456, 15, 24789815072), (false, 26435225, 23, 86252561100)),
   (59187, (true, 4302677881, 250, 144145209438971), (true, 1546054300090609, 10800, 60790089787338654516023))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert292

#print axioms Rank2Cert292.on_curve
#print axioms Rank2Cert292.indep
#print axioms Rank2Cert292.count

bsd/Rank2Cert293.lean

lean_all/bsd/Rank2Cert293.lean

/-
  bsd/Rank2Cert293.lean — T39 cycle 664 (goal G376): rank ≥ 2 for 1 of the 6 congruent curves in 59201–59400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert293
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (59386, (false, 38929823, 72, 1905416356591), (true, 1242405214, 105, 37217679927688))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert293

#print axioms Rank2Cert293.on_curve
#print axioms Rank2Cert293.indep
#print axioms Rank2Cert293.count

bsd/Rank2Cert294.lean

lean_all/bsd/Rank2Cert294.lean

/-
  bsd/Rank2Cert294.lean — T39 cycle 665 (goal G377): rank ≥ 2 for 3 of the 6 congruent curves in 59401–59600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert294
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (59449, (true, 2157961, 6, 406288675), (true, 193390547559049, 53118, 1338518396598490195165)),
   (59451, (true, 320572, 1, 178356374), (false, 1478979, 5, 178876944)),
   (59595, (true, 2721505, 6, 2762327575), (true, 13638285930025, 12784, 35256510076572485675))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert294

#print axioms Rank2Cert294.on_curve
#print axioms Rank2Cert294.indep
#print axioms Rank2Cert294.count

bsd/Rank2Cert295.lean

lean_all/bsd/Rank2Cert295.lean

/-
  bsd/Rank2Cert295.lean — T39 cycle 666 (goal G378): rank ≥ 2 for 2 of the 6 congruent curves in 59601–59800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert295
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (59690, (false, 6871635, 23, 80788928625), (false, 2357963915, 1669, 8073078091311525)),
   (59730, (true, 99405, 1, 25052175), (true, 12973356, 7, 45523506204))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert295

#print axioms Rank2Cert295.on_curve
#print axioms Rank2Cert295.indep
#print axioms Rank2Cert295.count

bsd/Rank2Cert296.lean

lean_all/bsd/Rank2Cert296.lean

/-
  bsd/Rank2Cert296.lean — T39 cycle 667 (goal G379): rank ≥ 2 for 1 of the 4 congruent curves in 59801–60000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert296
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (59865, (true, 33658599, 19, 149705477532), (false, 2148215615, 893, 2210419440775000))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert296

#print axioms Rank2Cert296.on_curve
#print axioms Rank2Cert296.indep
#print axioms Rank2Cert296.count

bsd/Rank2Cert297.lean

lean_all/bsd/Rank2Cert297.lean

/-
  bsd/Rank2Cert297.lean — T39 cycle 669 (goal G381): rank ≥ 2 for 3 of the 7 congruent curves in 60001–60200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert297
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (60065, (true, 555025, 3, 93706100), (true, 33945829953025, 17516, 166096461969606737025)),
   (60114, (true, 4027122, 1, 8080603344), (false, 8376819728, 5251, 151702836787297144)),
   (60130, (true, 108045, 1, 29506575), (false, 413853566, 183, 40090868316352))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert297

#print axioms Rank2Cert297.on_curve
#print axioms Rank2Cert297.indep
#print axioms Rank2Cert297.count

bsd/Rank2Cert298.lean

lean_all/bsd/Rank2Cert298.lean

/-
  bsd/Rank2Cert298.lean — T39 cycle 670 (goal G382): rank ≥ 2 for 2 of the 4 congruent curves in 60201–60400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert298
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (60347, (false, 27916063, 764, 186109846424145), (true, 412773713, 52, 7703134342695)),
   (60385, (true, 181753005625, 519, 77174944241673500), (true, 199674028801, 1419, 70774254437890276))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert298

#print axioms Rank2Cert298.on_curve
#print axioms Rank2Cert298.indep
#print axioms Rank2Cert298.count

bsd/Rank2Cert299.lean

lean_all/bsd/Rank2Cert299.lean

/-
  bsd/Rank2Cert299.lean — T39 cycle 671 (goal G383): rank ≥ 2 for 1 of the 8 congruent curves in 60401–60600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert299
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (60554, (true, 451673, 2, 256214973), (true, 39639393, 16, 229693357167))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert299

#print axioms Rank2Cert299.on_curve
#print axioms Rank2Cert299.indep
#print axioms Rank2Cert299.count

bsd/Rank2Cert3.lean

lean_all/bsd/Rank2Cert3.lean

/-
  bsd/Rank2Cert3.lean — T39 cycle 325 (goal G60): rank ≥ 2 for 11 of the 12 congruent curves in 1201–1400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 5000 / 15000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert3
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (1201, (true, 1442401, 7, 1730881200), (true, 9836813420984386225, 80068128, 19201304550791104102427878295)),
   (1217, (false, 2262016, 65, 6944775648), (true, 215950737025, 5663, 98700852266750880)),
   (1241, (true, 2809, 1, 133560), (true, 1540081, 21, 1786493960)),
   (1249, (false, 101761, 17, 110476080), (true, 7726038890656321, 1464527, 636972589666349845140960)),
   (1321, (true, 2151197161, 721, 94554453112680), (true, 12607921225, 2952, 577398738544955)),
   (1330, (true, 19845, 1, 2789325), (true, 1137150, 13, 1188700800)),
   (1339, (true, 142129, 6, 50406785), (true, 28717, 1, 4861116)),
   (1346, (false, 7056, 17, 32670120), (true, 227138, 1, 108249792)),
   (1379, (true, 38809, 2, 7567755), (true, 10726865212, 2763, 213188912401654)),
   (1387, (true, 3330625, 48, 1712873825), (true, 61218475, 31, 478873488960)),
   (1393, (true, 970225, 1, 955670640), (true, 5286127, 31, 11757465720))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 11 := rfl

end Rank2Cert3

#print axioms Rank2Cert3.on_curve
#print axioms Rank2Cert3.indep
#print axioms Rank2Cert3.count

bsd/Rank2Cert30.lean

lean_all/bsd/Rank2Cert30.lean

/-
  bsd/Rank2Cert30.lean — T39 cycle 369 (goal G91): rank ≥ 2 for 9 of the 12 congruent curves in 6601–6800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert30
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (6641, (true, 5498667409, 693, 332148284350660), (true, 10952250409, 1207, 537181280815380)),
   (6649, (true, 2325625, 18, 1336118075), (true, 490455105625, 8526, 58320041571552725)),
   (6657, (true, 6397552, 31, 119686420), (false, 804447, 11, 36876420)),
   (6674, (true, 6216338, 7, 15477475728), (true, 34117488, 53, 166499027688)),
   (6681, (true, 23630697, 52, 74050727499), (false, 19209473, 227, 1506516093784)),
   (6690, (true, 14620326, 43, 29800401624), (true, 497290, 3, 348103000)),
   (6706, (true, 7406, 1, 270480), (true, 10303799177, 806, 947830962998661)),
   (6745, (true, 102524, 1, 32756418), (true, 198474705025, 5258, 30277570917814425)),
   (6771, (true, 11163, 1, 937692), (true, 295867459969, 856, 160910630236966465))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 9 := rfl

end Rank2Cert30

#print axioms Rank2Cert30.on_curve
#print axioms Rank2Cert30.indep
#print axioms Rank2Cert30.count

bsd/Rank2Cert300.lean

lean_all/bsd/Rank2Cert300.lean

/-
  bsd/Rank2Cert300.lean — T39 cycle 672 (goal G384): rank ≥ 2 for 3 of the 8 congruent curves in 60601–60800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert300
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (60634, (true, 635166, 1, 503898360), (true, 3978250558, 39, 250854253789112)),
   (60697, (true, 77372, 1, 13346670), (true, 98160319792921, 39530, 250567733302392833781)),
   (60721, (false, 21689, 3, 80419040), (true, 3291354295681, 633, 5971041437587206920))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert300

#print axioms Rank2Cert300.on_curve
#print axioms Rank2Cert300.indep
#print axioms Rank2Cert300.count

bsd/Rank2Cert301.lean

lean_all/bsd/Rank2Cert301.lean

/-
  bsd/Rank2Cert301.lean — T39 cycle 673 (goal G385): rank ≥ 2 for 3 of the 10 congruent curves in 60801–61000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert301
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (60819, (true, 10189947, 11, 22499402976), (true, 61875, 1, 2831400)),
   (60970, (true, 583570, 3, 151728200), (true, 226358174, 11, 3403795768632)),
   (60977, (true, 3114223, 1, 5494665960), (true, 1723280832, 29, 71505871917480))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert301

#print axioms Rank2Cert301.on_curve
#print axioms Rank2Cert301.indep
#print axioms Rank2Cert301.count

bsd/Rank2Cert302.lean

lean_all/bsd/Rank2Cert302.lean

/-
  bsd/Rank2Cert302.lean — T39 cycle 674 (goal G386): rank ≥ 2 for 1 of the 6 congruent curves in 61001–61200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert302
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (61161, (true, 69375, 1, 8624700), (true, 71206143801, 1030, 7826154351625701))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert302

#print axioms Rank2Cert302.on_curve
#print axioms Rank2Cert302.indep
#print axioms Rank2Cert302.count

bsd/Rank2Cert303.lean

lean_all/bsd/Rank2Cert303.lean

/-
  bsd/Rank2Cert303.lean — T39 cycle 675 (goal G387): rank ≥ 2 for 1 of the 7 congruent curves in 61201–61400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert303
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (61241, (false, 27218687, 1176, 441865893160063), (true, 2130468649, 119, 89820172369320))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert303

#print axioms Rank2Cert303.on_curve
#print axioms Rank2Cert303.indep
#print axioms Rank2Cert303.count

bsd/Rank2Cert304.lean

lean_all/bsd/Rank2Cert304.lean

/-
  bsd/Rank2Cert304.lean — T39 cycle 676 (goal G388): rank ≥ 2 for 1 of the 7 congruent curves in 61401–61600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert304
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (61594, (true, 62192, 1, 2145624), (false, 197006967, 58, 901037360517))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert304

#print axioms Rank2Cert304.on_curve
#print axioms Rank2Cert304.indep
#print axioms Rank2Cert304.count

bsd/Rank2Cert305.lean

lean_all/bsd/Rank2Cert305.lean

/-
  bsd/Rank2Cert305.lean — T39 cycle 677 (goal G389): rank ≥ 2 for 5 of the 6 congruent curves in 61601–61800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert305
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (61690, (true, 607600, 3, 192392200), (false, 646551, 14, 9708462357)),
   (61745, (true, 347657640625, 2353, 37286477895315000), (true, 48681110524225, 25748, 183835596476560349025)),
   (61754, (false, 1517824, 5, 347771424), (true, 532350, 1, 385792680)),
   (61761, (false, 44100, 1, 9080190), (true, 963900, 1, 944397090)),
   (61770, (true, 92016, 1, 20688264), (true, 214959600, 1, 3151630175400))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 5 := rfl

end Rank2Cert305

#print axioms Rank2Cert305.on_curve
#print axioms Rank2Cert305.indep
#print axioms Rank2Cert305.count

bsd/Rank2Cert306.lean

lean_all/bsd/Rank2Cert306.lean

/-
  bsd/Rank2Cert306.lean — T39 cycle 678 (goal G390): rank ≥ 2 for 4 of the 6 congruent curves in 61801–62000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert306
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (61899, (true, 578163, 1, 437091228), (true, 11373655900, 321, 1004295516432110)),
   (61937, (true, 15917809, 15, 30689535752), (true, 1388680225, 68, 50636672913615)),
   (61945, (true, 68424508945, 522, 17345414124241775), (true, 180470480111057929, 82093, 76666934522859728165491308)),
   (61977, (true, 374229025, 12, 7237401522895), (true, 600037297, 92, 7136085349225))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert306

#print axioms Rank2Cert306.on_curve
#print axioms Rank2Cert306.indep
#print axioms Rank2Cert306.count

bsd/Rank2Cert307.lean

lean_all/bsd/Rank2Cert307.lean

/-
  bsd/Rank2Cert307.lean — T39 cycle 679 (goal G391): rank ≥ 2 for 4 of the 7 congruent curves in 62001–62200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert307
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (62090, (false, 29435, 3, 95739875), (true, 63695646, 13, 501406511256)),
   (62130, (true, 417453541, 1, 8529277002461), (false, 19266, 5, 215577648)),
   (62139, (false, 336875, 3, 259097300), (false, 35301, 275, 882923653332)),
   (62146, (true, 66654, 1, 6221040), (true, 1713454, 5, 945826608))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert307

#print axioms Rank2Cert307.on_curve
#print axioms Rank2Cert307.indep
#print axioms Rank2Cert307.count

bsd/Rank2Cert308.lean

lean_all/bsd/Rank2Cert308.lean

/-
  bsd/Rank2Cert308.lean — T39 cycle 680 (goal G392): rank ≥ 2 for 1 of the 8 congruent curves in 62201–62400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert308
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (62265, (false, 15876, 1, 7586082), (false, 24079335, 53, 850085667900))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert308

#print axioms Rank2Cert308.on_curve
#print axioms Rank2Cert308.indep
#print axioms Rank2Cert308.count

bsd/Rank2Cert309.lean

lean_all/bsd/Rank2Cert309.lean

/-
  bsd/Rank2Cert309.lean — T39 cycle 682 (goal G394): rank ≥ 2 for 3 of the 9 congruent curves in 62401–62600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert309
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (62402, (false, 5333378, 17, 39785366880), (false, 410000, 3, 245762200)),
   (62489, (false, 45927, 1, 9081072), (false, 973903, 4, 223217001)),
   (62491, (true, 567853, 3, 59056712), (true, 88412, 1, 18596466))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert309

#print axioms Rank2Cert309.on_curve
#print axioms Rank2Cert309.indep
#print axioms Rank2Cert309.count

bsd/Rank2Cert31.lean

lean_all/bsd/Rank2Cert31.lean

/-
  bsd/Rank2Cert31.lean — T39 cycle 370 (goal G92): rank ≥ 2 for 4 of the 8 congruent curves in 6801–7000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert31
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (6865, (true, 135295230625, 1677, 49255672411121500), (true, 119715043624969, 111159, 924290113523913473428)),
   (6882, (true, 35557, 1, 6578045), (true, 129099438, 29, 1465377928560)),
   (6923, (true, 7452, 1, 238050), (false, 145302500, 737, 45294074157150)),
   (6953, (false, 1040400, 13, 557116860), (true, 7497, 1, 242760))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert31

#print axioms Rank2Cert31.on_curve
#print axioms Rank2Cert31.indep
#print axioms Rank2Cert31.count

bsd/Rank2Cert310.lean

lean_all/bsd/Rank2Cert310.lean

/-
  bsd/Rank2Cert310.lean — T39 cycle 683 (goal G395): rank ≥ 2 for 5 of the 8 congruent curves in 62601–62800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert310
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (62601, (true, 118754097, 29, 1159990019496), (true, 67859484, 29, 352699668090)),
   (62634, (true, 63948, 1, 3261348), (true, 185328, 1, 75088728)),
   (62641, (true, 2866249, 6, 2995230205), (true, 94697137441, 318, 29075785340130935)),
   (62665, (true, 1452025, 3, 1612314100), (true, 21347776, 3, 98600097176)),
   (62785, (true, 187489, 1, 76495512), (true, 7205739103890625, 314687, 309175892646425485620000))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 5 := rfl

end Rank2Cert310

#print axioms Rank2Cert310.on_curve
#print axioms Rank2Cert310.indep
#print axioms Rank2Cert310.count

bsd/Rank2Cert311.lean

lean_all/bsd/Rank2Cert311.lean

/-
  bsd/Rank2Cert311.lean — T39 cycle 684 (goal G396): rank ≥ 2 for 2 of the 5 congruent curves in 62801–63000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert311
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (62835, (true, 24090939, 17, 77701308588), (false, 21814365, 101, 2992016619900)),
   (62930, (true, 8026445, 1, 22739007375), (true, 6827905, 6, 16830785825))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert311

#print axioms Rank2Cert311.on_curve
#print axioms Rank2Cert311.indep
#print axioms Rank2Cert311.count

bsd/Rank2Cert312.lean

lean_all/bsd/Rank2Cert312.lean

/-
  bsd/Rank2Cert312.lean — T39 cycle 685 (goal G397): rank ≥ 2 for 4 of the 8 congruent curves in 63001–63200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert312
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (63035, (false, 929915, 17, 17544244200), (true, 521574284401, 2856, 63287965443773449)),
   (63042, (true, 830053, 3, 551985245), (false, 73225166, 45, 894834709552)),
   (63058, (true, 193714258, 1, 2696139493440), (true, 434324495089, 1530, 269195680777248937)),
   (63145, (true, 42341686688401, 17343, 246246423318510644276), (true, 112339669626921025, 104601, 37652299124136283184284700))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert312

#print axioms Rank2Cert312.on_curve
#print axioms Rank2Cert312.indep
#print axioms Rank2Cert312.count

bsd/Rank2Cert313.lean

lean_all/bsd/Rank2Cert313.lean

/-
  bsd/Rank2Cert313.lean — T39 cycle 691 (goal G404): rank ≥ 2 for 2 of the 6 congruent curves in 63201–63400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert313
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (63273, (true, 3552327, 7, 3268140840), (false, 10551296, 165, 5595399131392)),
   (63330, (true, 202005, 1, 86213925), (true, 10836030, 7, 34176225600))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert313

#print axioms Rank2Cert313.on_curve
#print axioms Rank2Cert313.indep
#print axioms Rank2Cert313.count

bsd/Rank2Cert314.lean

lean_all/bsd/Rank2Cert314.lean

/-
  bsd/Rank2Cert314.lean — T39 cycle 692 (goal G405): rank ≥ 2 for 4 of the 8 congruent curves in 63401–63600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert314
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (63427, (true, 65642525, 31, 197401646520), (true, 4189289923, 93, 268815812863780)),
   (63481, (false, 222414775, 142, 18799453832445), (true, 154685485801, 230, 60823580509317549)),
   (63546, (true, 2289169, 6, 125904295), (true, 77658, 1, 12439728)),
   (63570, (true, 67626, 1, 5998824), (false, 6940830, 29, 139657003200))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert314

#print axioms Rank2Cert314.on_curve
#print axioms Rank2Cert314.indep
#print axioms Rank2Cert314.count

bsd/Rank2Cert315.lean

lean_all/bsd/Rank2Cert315.lean

/-
  bsd/Rank2Cert315.lean — T39 cycle 693 (goal G406): rank ≥ 2 for 1 of the 6 congruent curves in 63601–63800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert315
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (63745, (true, 103455, 1, 26208600), (true, 131033140225, 1178, 34992700387172775))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert315

#print axioms Rank2Cert315.on_curve
#print axioms Rank2Cert315.indep
#print axioms Rank2Cert315.count

bsd/Rank2Cert316.lean

lean_all/bsd/Rank2Cert316.lean

/-
  bsd/Rank2Cert316.lean — T39 cycle 694 (goal G407): rank ≥ 2 for 3 of the 8 congruent curves in 63801–64000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert316
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (63826, (false, 6112350, 13, 21973011360), (false, 869434247, 534, 536046444773587)),
   (63865, (true, 714025, 3, 357958900), (false, 109805625, 77, 3797364820500)),
   (63921, (true, 75504, 1, 11042460), (true, 3713805481, 119, 219497526594620))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert316

#print axioms Rank2Cert316.on_curve
#print axioms Rank2Cert316.indep
#print axioms Rank2Cert316.count

bsd/Rank2Cert317.lean

lean_all/bsd/Rank2Cert317.lean

/-
  bsd/Rank2Cert317.lean — T39 cycle 696 (goal G409): rank ≥ 2 for 1 of the 3 congruent curves in 64001–64200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert317
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (64010, (false, 1600, 1, 2559600), (false, 13156090, 19, 68903960400))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert317

#print axioms Rank2Cert317.on_curve
#print axioms Rank2Cert317.indep
#print axioms Rank2Cert317.count

bsd/Rank2Cert318.lean

lean_all/bsd/Rank2Cert318.lean

/-
  bsd/Rank2Cert318.lean — T39 cycle 697 (goal G410): rank ≥ 2 for 1 of the 6 congruent curves in 64201–64400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert318
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (64297, (true, 59379629737, 177, 14461261887250360), (true, 576047472978121, 78260, 10090583433986196081669))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert318

#print axioms Rank2Cert318.on_curve
#print axioms Rank2Cert318.indep
#print axioms Rank2Cert318.count

bsd/Rank2Cert319.lean

lean_all/bsd/Rank2Cert319.lean

/-
  bsd/Rank2Cert319.lean — T39 cycle 698 (goal G411): rank ≥ 2 for 4 of the 7 congruent curves in 64401–64600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert319
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (64491, (true, 2301289, 2, 3469052845), (true, 78652, 1, 12626614)),
   (64505, (false, 1432809, 5, 885794364), (false, 132951455, 66, 2854243530775)),
   (64554, (true, 247254, 1, 118681920), (false, 6198055479, 1424, 10293983973847035)),
   (64586, (true, 65478508, 1, 529843915188), (true, 22840225333, 579, 1098914368654277))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert319

#print axioms Rank2Cert319.on_curve
#print axioms Rank2Cert319.indep
#print axioms Rank2Cert319.count

bsd/Rank2Cert32.lean

lean_all/bsd/Rank2Cert32.lean

/-
  bsd/Rank2Cert32.lean — T39 cycle 371 (goal G93): rank ≥ 2 for 2 of the 2 congruent curves in 7001–7200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert32
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (7001, (true, 518268968281, 1009, 373071209065760520), (true, 708215163062592025, 5646592, 565623769503900690285756285)),
   (7170, (true, 999045, 1, 998542125), (true, 7350, 1, 138600))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert32

#print axioms Rank2Cert32.on_curve
#print axioms Rank2Cert32.indep
#print axioms Rank2Cert32.count

bsd/Rank2Cert320.lean

lean_all/bsd/Rank2Cert320.lean

/-
  bsd/Rank2Cert320.lean — T39 cycle 701 (goal G414): rank ≥ 2 for 5 of the 8 congruent curves in 64601–64800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert320
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (64610, (true, 82810, 1, 14905800), (false, 142814, 15, 5493469184)),
   (64722, (false, 10932843, 13, 1112383845), (true, 141772, 1, 47493620)),
   (64770, (true, 16516350, 1, 67122446400), (false, 21192072551, 2254, 47804188661318557)),
   (64779, (true, 68403, 1, 5745852), (false, 5889389429, 401, 659355523001560)),
   (64785, (true, 380689, 1, 231458912), (false, 9345629279, 743, 3337323935987944))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 5 := rfl

end Rank2Cert320

#print axioms Rank2Cert320.on_curve
#print axioms Rank2Cert320.indep
#print axioms Rank2Cert320.count

bsd/Rank2Cert321.lean

lean_all/bsd/Rank2Cert321.lean

/-
  bsd/Rank2Cert321.lean — T39 cycle 702 (goal G415): rank ≥ 2 for 3 of the 10 congruent curves in 64801–65000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert321
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (64889, (false, 387617, 3, 271958248), (true, 134898271225, 321, 49485214521133120)),
   (64930, (true, 14106150, 11, 44002132200), (false, 6470199, 10, 1382903451)),
   (64970, (true, 2029289, 2, 2866982187), (true, 6358653159769, 1034, 16033258306226024253))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert321

#print axioms Rank2Cert321.on_curve
#print axioms Rank2Cert321.indep
#print axioms Rank2Cert321.count

bsd/Rank2Cert322.lean

lean_all/bsd/Rank2Cert322.lean

/-
  bsd/Rank2Cert322.lean — T39 cycle 705 (goal G418): rank ≥ 2 for 1 of the 6 congruent curves in 65001–65200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert322
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (65041, (true, 21918817, 7, 101527960344), (true, 1068570721, 84, 31544923736465))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert322

#print axioms Rank2Cert322.on_curve
#print axioms Rank2Cert322.indep
#print axioms Rank2Cert322.count

bsd/Rank2Cert323.lean

lean_all/bsd/Rank2Cert323.lean

/-
  bsd/Rank2Cert323.lean — T39 cycle 708 (goal G421): rank ≥ 2 for 1 of the 6 congruent curves in 65201–65400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert323
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (65305, (true, 18855958489, 303, 2454865863859288), (true, 929736262732305625, 3543792, 422278879401884859335099875))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert323

#print axioms Rank2Cert323.on_curve
#print axioms Rank2Cert323.indep
#print axioms Rank2Cert323.count

bsd/Rank2Cert324.lean

lean_all/bsd/Rank2Cert324.lean

/-
  bsd/Rank2Cert324.lean — T39 cycle 711 (goal G424): rank ≥ 2 for 3 of the 8 congruent curves in 65401–65600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert324
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (65481, (false, 933156, 5, 1299286422), (false, 158447, 4, 412242823)),
   (65490, (true, 5795865, 2, 13939055325), (false, 15891060, 91, 2160964768500)),
   (65586, (false, 596594192, 281, 125649913891528), (true, 1096699217577, 2984, 972124082165228139))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert324

#print axioms Rank2Cert324.on_curve
#print axioms Rank2Cert324.indep
#print axioms Rank2Cert324.count

bsd/Rank2Cert325.lean

lean_all/bsd/Rank2Cert325.lean

/-
  bsd/Rank2Cert325.lean — T39 cycle 714 (goal G427): rank ≥ 2 for 3 of the 6 congruent curves in 65601–65800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert325
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (65641, (true, 3147322201, 119, 168691409663400), (true, 455110755555625, 19992, 9692880287333009410475)),
   (65705, (true, 339708294025, 299, 197967677953850700), (true, 6600435148504752025, 3698344, 16799464051199869941299979075)),
   (65778, (true, 481650, 1, 331138080), (true, 2364536667025, 1012, 3634483916216399545))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert325

#print axioms Rank2Cert325.on_curve
#print axioms Rank2Cert325.indep
#print axioms Rank2Cert325.count

bsd/Rank2Cert326.lean

lean_all/bsd/Rank2Cert326.lean

/-
  bsd/Rank2Cert326.lean — T39 cycle 717 (goal G430): rank ≥ 2 for 2 of the 6 congruent curves in 65801–66000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert326
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (65858, (true, 61305417, 26, 329997310245), (true, 63379170738, 683, 13955966767379520)),
   (65969, (true, 337477601, 23, 6166418015400), (false, 56142272241, 1231, 19598282017064520))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert326

#print axioms Rank2Cert326.on_curve
#print axioms Rank2Cert326.indep
#print axioms Rank2Cert326.count

bsd/Rank2Cert327.lean

lean_all/bsd/Rank2Cert327.lean

/-
  bsd/Rank2Cert327.lean — T39 cycle 721 (goal G433): rank ≥ 2 for 3 of the 10 congruent curves in 66001–66200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert327
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (66154, (true, 13064833, 12, 32317590977), (true, 912429433, 6, 27561157314643)),
   (66155, (true, 125438413995, 67, 44426757199877100), (true, 366726521511025, 27384, 6958299006598636739575)),
   (66178, (false, 20396943, 88, 2312689219575), (false, 60301556, 55, 1482341631372))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert327

#print axioms Rank2Cert327.on_curve
#print axioms Rank2Cert327.indep
#print axioms Rank2Cert327.count

bsd/Rank2Cert328.lean

lean_all/bsd/Rank2Cert328.lean

/-
  bsd/Rank2Cert328.lean — T39 cycle 724 (goal G437): rank ≥ 2 for 1 of the 7 congruent curves in 66201–66400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert328
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (66234, (true, 48898122, 19, 298263498624), (false, 8758848, 41, 328491522576))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert328

#print axioms Rank2Cert328.on_curve
#print axioms Rank2Cert328.indep
#print axioms Rank2Cert328.count

bsd/Rank2Cert329.lean

lean_all/bsd/Rank2Cert329.lean

/-
  bsd/Rank2Cert329.lean — T39 cycle 727 (goal G440): rank ≥ 2 for 2 of the 3 congruent curves in 66401–66600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert329
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (66409, (false, 3132927, 8, 5083579137), (true, 3380375881, 75, 195334733706596)),
   (66489, (true, 138493479, 17, 1614072684960), (true, 99775055625, 1171, 12802157423703900))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert329

#print axioms Rank2Cert329.on_curve
#print axioms Rank2Cert329.indep
#print axioms Rank2Cert329.count

bsd/Rank2Cert33.lean

lean_all/bsd/Rank2Cert33.lean

/-
  bsd/Rank2Cert33.lean — T39 cycle 373 (goal G94): rank ≥ 2 for 4 of the 7 congruent curves in 7201–7400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert33
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (7210, (true, 5198410, 1, 11852374800), (true, 882014, 11, 121907688)),
   (7298, (false, 98, 1, 72240), (true, 775779414835489, 179550, 20589989509434690558737)),
   (7361, (false, 1089, 1, 240240), (true, 28685817, 43, 135244065216)),
   (7395, (true, 525625, 8, 165782125), (true, 42891, 1, 8749764))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert33

#print axioms Rank2Cert33.on_curve
#print axioms Rank2Cert33.indep
#print axioms Rank2Cert33.count

bsd/Rank2Cert330.lean

lean_all/bsd/Rank2Cert330.lean

/-
  bsd/Rank2Cert330.lean — T39 cycle 730 (goal G443): rank ≥ 2 for 4 of the 10 congruent curves in 66601–66800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert330
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (66633, (true, 95817, 1, 21313440), (false, 107146101975, 3041, 198629465277818520)),
   (66722, (false, 1922, 1, 2923920), (false, 911186, 5, 1333699488)),
   (66729, (true, 720447, 1, 608880636), (true, 55519140625, 131, 13078914535115000)),
   (66794, (true, 8133853, 9, 17321723783), (true, 11721150, 13, 10805952360))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert330

#print axioms Rank2Cert330.on_curve
#print axioms Rank2Cert330.indep
#print axioms Rank2Cert330.count

bsd/Rank2Cert331.lean

lean_all/bsd/Rank2Cert331.lean

/-
  bsd/Rank2Cert331.lean — T39 cycle 733 (goal G446): rank ≥ 2 for 2 of the 7 congruent curves in 66801–67000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert331
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (66810, (false, 31147215, 128, 6106538921175), (true, 3500844, 7, 2321059572)),
   (66890, (true, 106090, 1, 26821200), (true, 29979214221785139529, 9394462, 160932048328967902693365599067))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert331

#print axioms Rank2Cert331.on_curve
#print axioms Rank2Cert331.indep
#print axioms Rank2Cert331.count

bsd/Rank2Cert332.lean

lean_all/bsd/Rank2Cert332.lean

/-
  bsd/Rank2Cert332.lean — T39 cycle 736 (goal G449): rank ≥ 2 for 3 of the 5 congruent curves in 67001–67200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert332
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (67009, (false, 57600, 1, 8217840), (true, 99912839000881, 37972, 254339446274608564905)),
   (67065, (true, 16600560, 7, 66298486500), (false, 55815, 1, 8784000)),
   (67106, (true, 948562, 3, 712383056), (false, 29776018, 149, 8127939103296))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert332

#print axioms Rank2Cert332.on_curve
#print axioms Rank2Cert332.indep
#print axioms Rank2Cert332.count

bsd/Rank2Cert333.lean

lean_all/bsd/Rank2Cert333.lean

/-
  bsd/Rank2Cert333.lean — T39 cycle 739 (goal G452): rank ≥ 2 for 3 of the 10 congruent curves in 67201–67400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert333
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (67210, (true, 1922206, 3, 2529623096), (false, 253109610, 383, 156798748033200)),
   (67235, (true, 428485, 1, 277006500), (true, 150299659225, 1026, 51403947006531925)),
   (67355, (true, 14357072041, 282, 1596073065252211), (true, 1927633658280625, 22852, 84618327799619294363625))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert333

#print axioms Rank2Cert333.on_curve
#print axioms Rank2Cert333.indep
#print axioms Rank2Cert333.count

bsd/Rank2Cert334.lean

lean_all/bsd/Rank2Cert334.lean

/-
  bsd/Rank2Cert334.lean — T39 cycle 742 (goal G455): rank ≥ 2 for 2 of the 7 congruent curves in 67401–67600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert334
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (67490, (true, 62609266, 27, 306378097136), (false, 1319506, 5, 1207890528)),
   (67595, (false, 581405, 3, 136536400), (true, 159061661161225, 45182, 997825608881653554525))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert334

#print axioms Rank2Cert334.on_curve
#print axioms Rank2Cert334.indep
#print axioms Rank2Cert334.count

bsd/Rank2Cert335.lean

lean_all/bsd/Rank2Cert335.lean

/-
  bsd/Rank2Cert335.lean — T39 cycle 745 (goal G458): rank ≥ 2 for 3 of the 13 congruent curves in 67601–67800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert335
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (67691, (false, 3541903, 8, 4694845449), (true, 1140220351588201, 112994, 25114371257775857631405)),
   (67721, (false, 15424, 25, 5256562824), (true, 2655926416133761, 198021, 2460425243163688703240)),
   (67730, (false, 11008530, 31, 212846712000), (true, 735586448159809, 37750, 19777862479421058994623))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert335

#print axioms Rank2Cert335.on_curve
#print axioms Rank2Cert335.indep
#print axioms Rank2Cert335.count

bsd/Rank2Cert336.lean

lean_all/bsd/Rank2Cert336.lean

/-
  bsd/Rank2Cert336.lean — T39 cycle 748 (goal G461): rank ≥ 2 for 0 of the 9 congruent curves in 67801–68000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert336
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert336

#print axioms Rank2Cert336.on_curve
#print axioms Rank2Cert336.indep
#print axioms Rank2Cert336.count

bsd/Rank2Cert337.lean

lean_all/bsd/Rank2Cert337.lean

/-
  bsd/Rank2Cert337.lean — T39 cycle 752 (goal G465): rank ≥ 2 for 2 of the 6 congruent curves in 68001–68200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert337
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (68034, (true, 107916, 1, 27518580), (true, 68208, 1, 1271592)),
   (68057, (false, 11425364775, 1393, 14063020742460360), (true, 7012661337, 229, 505509767875860))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert337

#print axioms Rank2Cert337.on_curve
#print axioms Rank2Cert337.indep
#print axioms Rank2Cert337.count

bsd/Rank2Cert338.lean

lean_all/bsd/Rank2Cert338.lean

/-
  bsd/Rank2Cert338.lean — T39 cycle 755 (goal G468): rank ≥ 2 for 2 of the 9 congruent curves in 68201–68400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert338
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (68289, (true, 69628, 1, 3585842), (true, 18268495921, 158, 2458413679388095)),
   (68322, (true, 150479977, 46, 512275253525), (true, 4981686736534225, 20286, 351607155308668749409415))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert338

#print axioms Rank2Cert338.on_curve
#print axioms Rank2Cert338.indep
#print axioms Rank2Cert338.count

bsd/Rank2Cert339.lean

lean_all/bsd/Rank2Cert339.lean

/-
  bsd/Rank2Cert339.lean — T39 cycle 758 (goal G471): rank ≥ 2 for 4 of the 14 congruent curves in 68401–68600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert339
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (68401, (false, 686857, 11, 6835657512), (true, 48179727152418481, 692609, 7743764210378018679748080)),
   (68482, (true, 249218, 1, 119624640), (true, 13351177, 6, 47945300165)),
   (68561, (false, 67600, 1, 2974140), (true, 13167780296400601, 189805, 1484194579813884296302776)),
   (68586, (true, 112000938, 17, 1166601770208), (true, 231886, 1, 106667560))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert339

#print axioms Rank2Cert339.on_curve
#print axioms Rank2Cert339.indep
#print axioms Rank2Cert339.count

bsd/Rank2Cert34.lean

lean_all/bsd/Rank2Cert34.lean

/-
  bsd/Rank2Cert34.lean — T39 cycle 374 (goal G95): rank ≥ 2 for 6 of the 10 congruent curves in 7401–7600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert34
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (7410, (true, 610090, 9, 85412600), (true, 11466, 1, 936936)),
   (7426, (false, 204711743, 168, 643318766369), (false, 604065150, 737, 98018086595520)),
   (7449, (true, 474721, 2, 326437865), (true, 308919, 5, 136993428)),
   (7473, (true, 20727, 1, 2783340), (false, 65110463, 488, 14350570363745)),
   (7579, (true, 80613, 1, 22786608), (false, 93892733, 449, 14777434266804)),
   (7585, (false, 5184, 1, 398664), (true, 9225, 1, 504300))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 6 := rfl

end Rank2Cert34

#print axioms Rank2Cert34.on_curve
#print axioms Rank2Cert34.indep
#print axioms Rank2Cert34.count

bsd/Rank2Cert340.lean

lean_all/bsd/Rank2Cert340.lean

/-
  bsd/Rank2Cert340.lean — T39 cycle 762 (goal G474): rank ≥ 2 for 2 of the 3 congruent curves in 68601–68800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert340
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (68730, (false, 54150, 1, 9849600), (false, 1236270, 271, 5612309047800)),
   (68746, (true, 112554, 1, 29898960), (true, 899669323081, 580, 853062477210716229))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert340

#print axioms Rank2Cert340.on_curve
#print axioms Rank2Cert340.indep
#print axioms Rank2Cert340.count

bsd/Rank2Cert341.lean

lean_all/bsd/Rank2Cert341.lean

/-
  bsd/Rank2Cert341.lean — T39 cycle 765 (goal G477): rank ≥ 2 for 1 of the 6 congruent curves in 68801–69000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert341
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (68817, (true, 2157961, 5, 1913613416), (true, 236992, 1, 110400920))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert341

#print axioms Rank2Cert341.on_curve
#print axioms Rank2Cert341.indep
#print axioms Rank2Cert341.count

bsd/Rank2Cert342.lean

lean_all/bsd/Rank2Cert342.lean

/-
  bsd/Rank2Cert342.lean — T39 cycle 769 (goal G480): rank ≥ 2 for 3 of the 6 congruent curves in 69001–69200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert342
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (69146, (false, 28322, 1, 10615752), (true, 73997, 1, 7168161)),
   (69179, (false, 5436299, 9, 3167746120), (false, 40326201344, 835, 5313989144445696)),
   (69186, (true, 5621850, 1, 13328644680), (true, 8836458982, 237, 746009727009976))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert342

#print axioms Rank2Cert342.on_curve
#print axioms Rank2Cert342.indep
#print axioms Rank2Cert342.count

bsd/Rank2Cert343.lean

lean_all/bsd/Rank2Cert343.lean

/-
  bsd/Rank2Cert343.lean — T39 cycle 772 (goal G483): rank ≥ 2 for 2 of the 5 congruent curves in 69201–69400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert343
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (69258, (true, 1524258, 1, 1879918200), (false, 6911639, 10, 1163765141)),
   (69394, (true, 291737, 2, 48497379), (false, 11207739218, 1927, 27254183824752144))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert343

#print axioms Rank2Cert343.on_curve
#print axioms Rank2Cert343.indep
#print axioms Rank2Cert343.count

bsd/Rank2Cert344.lean

lean_all/bsd/Rank2Cert344.lean

/-
  bsd/Rank2Cert344.lean — T39 cycle 775 (goal G486): rank ≥ 2 for 2 of the 9 congruent curves in 69401–69600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert344
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (69465, (true, 132540, 1, 41094450), (false, 10769969375, 1069, 8161958507219000)),
   (69513, (false, 24162423, 64, 1394526449115), (false, 98307303, 41, 626288730480))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert344

#print axioms Rank2Cert344.on_curve
#print axioms Rank2Cert344.indep
#print axioms Rank2Cert344.count

bsd/Rank2Cert345.lean

lean_all/bsd/Rank2Cert345.lean

/-
  bsd/Rank2Cert345.lean — T39 cycle 778 (goal G489): rank ≥ 2 for 4 of the 14 congruent curves in 69601–69800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert345
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (69665, (true, 15477290001771049, 457745, 640135785848205574236468), (true, 61970666901025, 28448, 202508679246476861775)),
   (69674, (true, 3661218, 7, 2530467192), (true, 6658124, 1, 17179243620)),
   (69745, (true, 3553225, 7, 1833464100), (false, 646416, 17, 16197333348)),
   (69771, (true, 9601563, 11, 14171906988), (true, 1485088730848801, 73984, 55305954737604400567985))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert345

#print axioms Rank2Cert345.on_curve
#print axioms Rank2Cert345.indep
#print axioms Rank2Cert345.count

bsd/Rank2Cert346.lean

lean_all/bsd/Rank2Cert346.lean

/-
  bsd/Rank2Cert346.lean — T39 cycle 781 (goal G492): rank ≥ 2 for 1 of the 8 congruent curves in 69801–70000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert346
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (69890, (true, 33171481, 18, 139609822429), (true, 4202799241, 12, 272462490805339))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert346

#print axioms Rank2Cert346.on_curve
#print axioms Rank2Cert346.indep
#print axioms Rank2Cert346.count

bsd/Rank2Cert347.lean

lean_all/bsd/Rank2Cert347.lean

/-
  bsd/Rank2Cert347.lean — T39 cycle 785 (goal G496): rank ≥ 2 for 4 of the 4 congruent curves in 70001–70200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert347
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (70057, (true, 235897690249, 1835, 30619483573068), (true, 558413054485225, 72581, 9902942051768652018540)),
   (70081, (true, 132278417401, 265, 48076483813015476), (false, 721022423, 257, 122774185316244)),
   (70161, (true, 19153953, 4, 83683620657), (true, 7569518702886361, 16049, 658568815077965034232340)),
   (70195, (true, 71548225225, 682, 17029294605721725), (true, 338177946096, 1259, 185711875510037844))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert347

#print axioms Rank2Cert347.on_curve
#print axioms Rank2Cert347.indep
#print axioms Rank2Cert347.count

bsd/Rank2Cert348.lean

lean_all/bsd/Rank2Cert348.lean

/-
  bsd/Rank2Cert348.lean — T39 cycle 788 (goal G499): rank ≥ 2 for 4 of the 7 congruent curves in 70201–70400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert348
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (70273, (false, 48223, 1, 11224920), (true, 73017025, 31, 237247618320)),
   (70330, (false, 2704, 1, 3654456), (false, 388393850, 903, 1130166239597200)),
   (70345, (true, 89971255, 17, 831334396200), (true, 1046199025, 82, 30183337881825)),
   (70370, (false, 17113519, 28, 216972311079), (true, 498857580, 41, 10824261267300))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert348

#print axioms Rank2Cert348.on_curve
#print axioms Rank2Cert348.indep
#print axioms Rank2Cert348.count

bsd/Rank2Cert349.lean

lean_all/bsd/Rank2Cert349.lean

/-
  bsd/Rank2Cert349.lean — T39 cycle 791 (goal G502): rank ≥ 2 for 1 of the 8 congruent curves in 70401–70600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert349
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (70577, (true, 371876773489, 175, 226772947902899112), (true, 6530363577742225, 273004, 312728683329185679894585))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert349

#print axioms Rank2Cert349.on_curve
#print axioms Rank2Cert349.indep
#print axioms Rank2Cert349.count

bsd/Rank2Cert35.lean

lean_all/bsd/Rank2Cert35.lean

/-
  bsd/Rank2Cert35.lean — T39 cycle 375 (goal G96): rank ≥ 2 for 7 of the 13 congruent curves in 7601–7800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert35
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (7602, (false, 30211454, 115, 527057550656), (true, 1375962, 13, 577904040)),
   (7611, (true, 271803, 1, 141648192), (true, 149683, 3, 51490952)),
   (7689, (false, 480249, 25, 3313626624), (true, 11913, 1, 993168)),
   (7707, (false, 507, 1, 173160), (true, 158203612, 103, 1703509638670)),
   (7769, (true, 208849, 5, 35086632), (false, 31433, 23, 728619864)),
   (7779, (true, 340707, 1, 198819216), (false, 6627, 1, 331632)),
   (7795, (true, 453005, 1, 304852800), (true, 8812378159225, 31882, 11450736068792420475))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 7 := rfl

end Rank2Cert35

#print axioms Rank2Cert35.on_curve
#print axioms Rank2Cert35.indep
#print axioms Rank2Cert35.count

bsd/Rank2Cert350.lean

lean_all/bsd/Rank2Cert350.lean

/-
  bsd/Rank2Cert350.lean — T39 cycle 794 (goal G505): rank ≥ 2 for 1 of the 5 congruent curves in 70601–70800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert350
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (70626, (false, 186687902, 129, 15854510781152), (false, 62106256139, 1055, 12009060982417141))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert350

#print axioms Rank2Cert350.on_curve
#print axioms Rank2Cert350.indep
#print axioms Rank2Cert350.count

bsd/Rank2Cert351.lean

lean_all/bsd/Rank2Cert351.lean

/-
  bsd/Rank2Cert351.lean — T39 cycle 796 (goal G508): rank ≥ 2 for 0 of the 4 congruent curves in 70801–71000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert351
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert351

#print axioms Rank2Cert351.on_curve
#print axioms Rank2Cert351.indep
#print axioms Rank2Cert351.count

bsd/Rank2Cert352.lean

lean_all/bsd/Rank2Cert352.lean

/-
  bsd/Rank2Cert352.lean — T39 cycle 799 (goal G511): rank ≥ 2 for 2 of the 6 congruent curves in 71001–71200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert352
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (71026, (true, 40025650, 9, 250596130960), (false, 68850, 1, 4577760)),
   (71043, (true, 154275, 1, 53788680), (false, 1906857071, 180, 56298123904967))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert352

#print axioms Rank2Cert352.on_curve
#print axioms Rank2Cert352.indep
#print axioms Rank2Cert352.count

bsd/Rank2Cert353.lean

lean_all/bsd/Rank2Cert353.lean

/-
  bsd/Rank2Cert353.lean — T39 cycle 802 (goal G514): rank ≥ 2 for 4 of the 6 congruent curves in 71201–71400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert353
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (71305, (true, 1203409, 3, 1116763552), (true, 461143198320025, 80409, 219280535420396717800)),
   (71337, (true, 251429824, 35, 3738255099832), (true, 83595225, 29, 532250070660)),
   (71346, (false, 11154, 7, 369214560), (true, 152421, 1, 52585245)),
   (71385, (false, 67335, 1, 6150600), (false, 159135, 2, 94577175))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert353

#print axioms Rank2Cert353.on_curve
#print axioms Rank2Cert353.indep
#print axioms Rank2Cert353.count

bsd/Rank2Cert354.lean

lean_all/bsd/Rank2Cert354.lean

/-
  bsd/Rank2Cert354.lean — T39 cycle 805 (goal G517): rank ≥ 2 for 3 of the 9 congruent curves in 71401–71600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert354
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (71435, (true, 479115, 1, 327927600), (true, 170466369100729, 1020, 2225655922632635274067)),
   (71466, (true, 10012722, 11, 15971765544), (true, 773162281998, 2833, 455869202204482488)),
   (71545, (true, 3045025, 4, 4923805425), (true, 1336299921, 40, 48669464155719))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert354

#print axioms Rank2Cert354.on_curve
#print axioms Rank2Cert354.indep
#print axioms Rank2Cert354.count

bsd/Rank2Cert355.lean

lean_all/bsd/Rank2Cert355.lean

/-
  bsd/Rank2Cert355.lean — T39 cycle 808 (goal G520): rank ≥ 2 for 2 of the 4 congruent curves in 71601–71800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert355
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (71610, (true, 80850, 1, 10672200), (true, 562650, 1, 418611600)),
   (71689, (true, 80937, 1, 10688376), (true, 29243395981456697569, 13483061, 141567440234963505099837703560))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert355

#print axioms Rank2Cert355.on_curve
#print axioms Rank2Cert355.indep
#print axioms Rank2Cert355.count

bsd/Rank2Cert356.lean

lean_all/bsd/Rank2Cert356.lean

/-
  bsd/Rank2Cert356.lean — T39 cycle 812 (goal G523): rank ≥ 2 for 4 of the 6 congruent curves in 71801–72000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert356
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (71810, (true, 33745690, 1, 196031781000), (true, 2398454, 1, 3712806792)),
   (71890, (true, 1221025, 4, 452712975), (true, 87374, 1, 14678832)),
   (71929, (false, 451094927, 168, 42039892706057), (true, 104142025, 19, 1029203926140)),
   (71953, (true, 8195068, 9, 16492574350), (true, 14375281257035521, 333150, 1433121006134894121326681))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert356

#print axioms Rank2Cert356.on_curve
#print axioms Rank2Cert356.indep
#print axioms Rank2Cert356.count

bsd/Rank2Cert357.lean

lean_all/bsd/Rank2Cert357.lean

/-
  bsd/Rank2Cert357.lean — T39 cycle 816 (goal G527): rank ≥ 2 for 2 of the 8 congruent curves in 72001–72200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert357
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (72001, (false, 8388161, 17, 55152021960), (true, 7775888761, 280, 471581186797491)),
   (72041, (false, 1800964, 5, 19892466), (true, 830824417200625, 71682, 21439420960866920028575))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert357

#print axioms Rank2Cert357.on_curve
#print axioms Rank2Cert357.indep
#print axioms Rank2Cert357.count

bsd/Rank2Cert358.lean

lean_all/bsd/Rank2Cert358.lean

/-
  bsd/Rank2Cert358.lean — T39 cycle 819 (goal G530): rank ≥ 2 for 2 of the 8 congruent curves in 72201–72400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert358
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (72289, (true, 80063, 1, 9737832), (false, 16486400, 33, 312553344320)),
   (72345, (true, 105625, 1, 25012000), (true, 294945, 2, 30969225))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert358

#print axioms Rank2Cert358.on_curve
#print axioms Rank2Cert358.indep
#print axioms Rank2Cert358.count

bsd/Rank2Cert359.lean

lean_all/bsd/Rank2Cert359.lean

/-
  bsd/Rank2Cert359.lean — T39 cycle 822 (goal G533): rank ≥ 2 for 2 of the 10 congruent curves in 72401–72600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert359
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (72410, (false, 24336, 1, 10638888), (true, 203732714843436025, 347432, 91873680211351008160915725)),
   (72570, (true, 174951756, 43, 1484915525604), (true, 822929286, 55, 22751711792784))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert359

#print axioms Rank2Cert359.on_curve
#print axioms Rank2Cert359.indep
#print axioms Rank2Cert359.count

bsd/Rank2Cert36.lean

lean_all/bsd/Rank2Cert36.lean

/-
  bsd/Rank2Cert36.lean — T39 cycle 376 (goal G97): rank ≥ 2 for 3 of the 4 congruent curves in 7801–8000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert36
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (7874, (true, 7753922, 1, 21591455424), (true, 11093598, 1, 36949496688)),
   (7881, (true, 3845521, 22, 957389615), (true, 13620924, 23, 47857489650)),
   (7995, (true, 42025, 2, 5589325), (true, 15600, 1, 1673100))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert36

#print axioms Rank2Cert36.on_curve
#print axioms Rank2Cert36.indep
#print axioms Rank2Cert36.count

bsd/Rank2Cert360.lean

lean_all/bsd/Rank2Cert360.lean

/-
  bsd/Rank2Cert360.lean — T39 cycle 825 (goal G536): rank ≥ 2 for 3 of the 9 congruent curves in 72601–72800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert360
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (72610, (true, 4973785, 8, 3954159075), (true, 351417768025, 1584, 178141565735533475)),
   (72611, (true, 78038172, 19, 649329743586), (true, 29413769, 2, 159516217245)),
   (72651, (false, 5905227, 43, 326119498812), (true, 71431790703289, 7456, 602756642002103159725))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert360

#print axioms Rank2Cert360.on_curve
#print axioms Rank2Cert360.indep
#print axioms Rank2Cert360.count

bsd/Rank2Cert361.lean

lean_all/bsd/Rank2Cert361.lean

/-
  bsd/Rank2Cert361.lean — T39 cycle 829 (goal G539): rank ≥ 2 for 2 of the 8 congruent curves in 72801–73000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert361
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (72802, (true, 11470409, 10, 30020296923), (false, 3189455442, 661, 1787352625764960)),
   (72930, (true, 1221025, 2, 1310159825), (true, 94380, 1, 18404100))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert361

#print axioms Rank2Cert361.on_curve
#print axioms Rank2Cert361.indep
#print axioms Rank2Cert361.count

bsd/Rank2Cert362.lean

lean_all/bsd/Rank2Cert362.lean

/-
  bsd/Rank2Cert362.lean — T39 cycle 832 (goal G542): rank ≥ 2 for 0 of the 5 congruent curves in 73001–73200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert362
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert362

#print axioms Rank2Cert362.on_curve
#print axioms Rank2Cert362.indep
#print axioms Rank2Cert362.count

bsd/Rank2Cert363.lean

lean_all/bsd/Rank2Cert363.lean

/-
  bsd/Rank2Cert363.lean — T39 cycle 835 (goal G545): rank ≥ 2 for 4 of the 8 congruent curves in 73201–73400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert363
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (73209, (true, 457263, 1, 305218188), (true, 3564258236585281, 209103, 93591693959400177852500)),
   (73315, (false, 996611, 15, 16437817088), (true, 1583356, 3, 1811114054)),
   (73370, (true, 1928205, 1, 2675562975), (false, 61364, 53, 51053682084)),
   (73371, (false, 694083, 7, 2938854204), (true, 951122685436729, 81524, 25184365259778406295315))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert363

#print axioms Rank2Cert363.on_curve
#print axioms Rank2Cert363.indep
#print axioms Rank2Cert363.count

bsd/Rank2Cert364.lean

lean_all/bsd/Rank2Cert364.lean

/-
  bsd/Rank2Cert364.lean — T39 cycle 838 (goal G548): rank ≥ 2 for 3 of the 8 congruent curves in 73401–73600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert364
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (73474, (false, 51842, 1, 11854752), (true, 366338719560529, 9164, 7010720023926537584295)),
   (73554, (false, 8303, 4, 107234119), (true, 75504, 1, 4684680)),
   (73595, (true, 83205, 1, 11197200), (true, 4192045, 1, 8581673100))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert364

#print axioms Rank2Cert364.on_curve
#print axioms Rank2Cert364.indep
#print axioms Rank2Cert364.count

bsd/Rank2Cert365.lean

lean_all/bsd/Rank2Cert365.lean

/-
  bsd/Rank2Cert365.lean — T39 cycle 841 (goal G551): rank ≥ 2 for 2 of the 6 congruent curves in 73601–73800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert365
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (73730, (false, 767376, 13, 10894564968), (false, 9476943120, 1447, 15000148470348600)),
   (73770, (false, 17035350, 31, 284029531200), (false, 3129870, 11, 14789071800))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert365

#print axioms Rank2Cert365.on_curve
#print axioms Rank2Cert365.indep
#print axioms Rank2Cert365.count

bsd/Rank2Cert366.lean

lean_all/bsd/Rank2Cert366.lean

/-
  bsd/Rank2Cert366.lean — T39 cycle 844 (goal G554): rank ≥ 2 for 1 of the 4 congruent curves in 73801–74000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert366
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (73905, (true, 196599, 1, 80777268), (true, 231275424705, 632, 110313198400593825))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert366

#print axioms Rank2Cert366.on_curve
#print axioms Rank2Cert366.indep
#print axioms Rank2Cert366.count

bsd/Rank2Cert367.lean

lean_all/bsd/Rank2Cert367.lean

/-
  bsd/Rank2Cert367.lean — T39 cycle 848 (goal G558): rank ≥ 2 for 3 of the 8 congruent curves in 74001–74200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert367
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (74074, (true, 105413, 1, 24350403), (true, 333170838, 19, 6061737313632)),
   (74130, (true, 2616789, 1, 4231347813), (false, 32463270, 59, 1458579011400)),
   (74195, (true, 319225, 2, 66435525), (true, 51624881, 26, 87858172479))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert367

#print axioms Rank2Cert367.on_curve
#print axioms Rank2Cert367.indep
#print axioms Rank2Cert367.count

bsd/Rank2Cert368.lean

lean_all/bsd/Rank2Cert368.lean

/-
  bsd/Rank2Cert368.lean — T39 cycle 852 (goal G561): rank ≥ 2 for 1 of the 7 congruent curves in 74201–74400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert368
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (74354, (true, 314608, 1, 171464328), (false, 15840211519, 760, 5024113420182879))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert368

#print axioms Rank2Cert368.on_curve
#print axioms Rank2Cert368.indep
#print axioms Rank2Cert368.count

bsd/Rank2Cert369.lean

lean_all/bsd/Rank2Cert369.lean

/-
  bsd/Rank2Cert369.lean — T39 cycle 855 (goal G564): rank ≥ 2 for 5 of the 8 congruent curves in 74401–74600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert369
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (74451, (true, 9795747, 11, 12041580564), (true, 96564741, 25, 831479722164)),
   (74465, (true, 1974025, 1, 2771531100), (true, 450569850025, 2376, 108839819645528725)),
   (74490, (true, 82810, 1, 10410400), (false, 183551, 12, 4594891393)),
   (74497, (true, 25068528, 7, 124176497220), (true, 5880625, 7, 11180452800)),
   (74555, (true, 112995, 1, 28541700), (true, 127358504195, 997, 36963768301418400))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 5 := rfl

end Rank2Cert369

#print axioms Rank2Cert369.on_curve
#print axioms Rank2Cert369.indep
#print axioms Rank2Cert369.count

bsd/Rank2Cert37.lean

lean_all/bsd/Rank2Cert37.lean

/-
  bsd/Rank2Cert37.lean — T39 cycle 377 (goal G98): rank ≥ 2 for 5 of the 9 congruent curves in 8001–8200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert37
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (8106, (true, 37249, 2, 3538655), (true, 22218, 1, 3083472)),
   (8162, (false, 6727, 2, 2620275), (true, 10011276, 35, 1602916524)),
   (8169, (true, 151321, 3, 51449140), (false, 9241407, 121, 362500294212)),
   (8170, (true, 9245, 1, 416025), (true, 74337196, 5, 640925607756)),
   (8194, (false, 256, 1, 131040), (true, 12791682, 31, 36054071424))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 5 := rfl

end Rank2Cert37

#print axioms Rank2Cert37.on_curve
#print axioms Rank2Cert37.indep
#print axioms Rank2Cert37.count

bsd/Rank2Cert370.lean

lean_all/bsd/Rank2Cert370.lean

/-
  bsd/Rank2Cert370.lean — T39 cycle 858 (goal G567): rank ≥ 2 for 0 of the 0 congruent curves in 74601–74800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert370
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert370

#print axioms Rank2Cert370.on_curve
#print axioms Rank2Cert370.indep
#print axioms Rank2Cert370.count

bsd/Rank2Cert371.lean

lean_all/bsd/Rank2Cert371.lean

/-
  bsd/Rank2Cert371.lean — T39 cycle 861 (goal G570): rank ≥ 2 for 2 of the 11 congruent curves in 74801–75000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert371
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (74802, (false, 25886, 5, 300845888), (true, 103334478, 31, 754609227840)),
   (74930, (true, 80645, 1, 8467725), (true, 34257996, 11, 193363548756))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert371

#print axioms Rank2Cert371.on_curve
#print axioms Rank2Cert371.indep
#print axioms Rank2Cert371.count

bsd/Rank2Cert372.lean

lean_all/bsd/Rank2Cert372.lean

/-
  bsd/Rank2Cert372.lean — T39 cycle 864 (goal G573): rank ≥ 2 for 1 of the 6 congruent curves in 75001–75200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert372
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (75026, (true, 947968, 3, 647817632), (true, 77326, 1, 5205360))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert372

#print axioms Rank2Cert372.on_curve
#print axioms Rank2Cert372.indep
#print axioms Rank2Cert372.count

bsd/Rank2Cert373.lean

lean_all/bsd/Rank2Cert373.lean

/-
  bsd/Rank2Cert373.lean — T39 cycle 867 (goal G576): rank ≥ 2 for 2 of the 4 congruent curves in 75201–75400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert373
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (75210, (true, 1441525, 1, 1728388475), (false, 13041414, 403, 44111088462384)),
   (75354, (true, 78345474, 13, 684236670840), (true, 18880924302, 301, 2418844489461576))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert373

#print axioms Rank2Cert373.on_curve
#print axioms Rank2Cert373.indep
#print axioms Rank2Cert373.count

bsd/Rank2Cert374.lean

lean_all/bsd/Rank2Cert374.lean

/-
  bsd/Rank2Cert374.lean — T39 cycle 870 (goal G579): rank ≥ 2 for 3 of the 13 congruent curves in 75401–75600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert374
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (75426, (false, 41128314422, 1597, 38110209891718328), (false, 31332008750, 899, 9255953406114800)),
   (75545, (true, 39358945, 17, 205453692900), (true, 66994225, 22, 459477948225)),
   (75570, (true, 214245, 1, 92792925), (false, 336875, 3, 342929125))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert374

#print axioms Rank2Cert374.on_curve
#print axioms Rank2Cert374.indep
#print axioms Rank2Cert374.count

bsd/Rank2Cert375.lean

lean_all/bsd/Rank2Cert375.lean

/-
  bsd/Rank2Cert375.lean — T39 cycle 873 (goal G582): rank ≥ 2 for 3 of the 6 congruent curves in 75601–75800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert375
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (75610, (true, 82810, 1, 9718800), (true, 238050087804405361, 767382, 114095836993086519341764841)),
   (75665, (false, 75625, 1, 676500), (true, 3578245140625, 6004, 4381137081081245625)),
   (75777, (false, 1010025, 61, 283374230040), (true, 3449247100, 173, 152628714453410))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert375

#print axioms Rank2Cert375.on_curve
#print axioms Rank2Cert375.indep
#print axioms Rank2Cert375.count

bsd/Rank2Cert376.lean

lean_all/bsd/Rank2Cert376.lean

/-
  bsd/Rank2Cert376.lean — T39 cycle 876 (goal G589): rank ≥ 2 for 5 of the 9 congruent curves in 75801–76000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert376
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (75835, (true, 79431725, 13, 698655535800), (false, 296982935, 1178, 1813528103556525)),
   (75922, (true, 80964268, 9, 726413135140), (true, 24219118, 9, 115283001680)),
   (75946, (true, 343408, 1, 196257672), (false, 673913087, 216, 90305442650753)),
   (75961, (true, 232965502225, 1561, 68280438273119820), (true, 40113285585025, 8687, 251450215259197428060)),
   (75985, (true, 211185, 1, 90550200), (true, 105264224065, 1161, 7882975697597000))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 5 := rfl

end Rank2Cert376

#print axioms Rank2Cert376.on_curve
#print axioms Rank2Cert376.indep
#print axioms Rank2Cert376.count

bsd/Rank2Cert377.lean

lean_all/bsd/Rank2Cert377.lean

/-
  bsd/Rank2Cert377.lean — T39 cycle 880 (goal G591): rank ≥ 2 for 1 of the 6 congruent curves in 76001–76200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert377
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (76153, (false, 4626783, 8, 3295117455), (false, 96950336, 45, 1180792999288))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert377

#print axioms Rank2Cert377.on_curve
#print axioms Rank2Cert377.indep
#print axioms Rank2Cert377.count

bsd/Rank2Cert378.lean

lean_all/bsd/Rank2Cert378.lean

/-
  bsd/Rank2Cert378.lean — T39 cycle 882 (goal G593): rank ≥ 2 for 2 of the 8 congruent curves in 76201–76400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert378
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (76377, (false, 139167, 2, 101458665), (true, 452720394025, 1118, 297761170150035955)),
   (76385, (true, 4527213675625, 3471, 9431549692894785500), (true, 2906989484958472225, 6079316, 1182499843184668622100116175))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert378

#print axioms Rank2Cert378.on_curve
#print axioms Rank2Cert378.indep
#print axioms Rank2Cert378.count

bsd/Rank2Cert379.lean

lean_all/bsd/Rank2Cert379.lean

/-
  bsd/Rank2Cert379.lean — T39 cycle 884 (goal G595): rank ≥ 2 for 1 of the 8 congruent curves in 76401–76600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert379
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (76570, (true, 66087970, 27, 287626339000), (true, 64683980, 11, 514865456100))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert379

#print axioms Rank2Cert379.on_curve
#print axioms Rank2Cert379.indep
#print axioms Rank2Cert379.count

bsd/Rank2Cert38.lean

lean_all/bsd/Rank2Cert38.lean

/-
  bsd/Rank2Cert38.lean — T39 cycle 378 (goal G99): rank ≥ 2 for 5 of the 7 congruent curves in 8201–8400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert38
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (8234, (true, 576738, 7, 312976488), (false, 5059103, 36, 21133491281)),
   (8241, (true, 1413721, 13, 288594080), (false, 6959504, 665, 9614164689044)),
   (8313, (true, 4065057, 19, 5528477520), (true, 160429888369, 1595, 63697336123153528)),
   (8385, (true, 42025, 1, 8441900), (true, 1081665, 1, 1124931600)),
   (8393, (false, 2140369, 25, 7006441596), (true, 102487, 3, 22174460))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 5 := rfl

end Rank2Cert38

#print axioms Rank2Cert38.on_curve
#print axioms Rank2Cert38.indep
#print axioms Rank2Cert38.count

bsd/Rank2Cert380.lean

lean_all/bsd/Rank2Cert380.lean

/-
  bsd/Rank2Cert380.lean — T39 cycle 886 (goal G596): rank ≥ 2 for 2 of the 9 congruent curves in 76601–76800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert380
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (76713, (true, 117975, 1, 30785040), (true, 3955052105532025, 94713, 244936101790864871438680)),
   (76755, (true, 151725, 1, 50979600), (false, 68192831, 36, 598051217953))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert380

#print axioms Rank2Cert380.on_curve
#print axioms Rank2Cert380.indep
#print axioms Rank2Cert380.count

bsd/Rank2Cert381.lean

lean_all/bsd/Rank2Cert381.lean

/-
  bsd/Rank2Cert381.lean — T39 cycle 889 (goal G597): rank ≥ 2 for 2 of the 8 congruent curves in 76801–77000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert381
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (76843, (true, 248768, 1, 118009320), (true, 378223187057521, 38930, 6998296184154336380319)),
   (76994, (false, 7056, 1, 6440280), (true, 1358143609, 56, 49254340195635))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert381

#print axioms Rank2Cert381.on_curve
#print axioms Rank2Cert381.indep
#print axioms Rank2Cert381.count

bsd/Rank2Cert382.lean

lean_all/bsd/Rank2Cert382.lean

/-
  bsd/Rank2Cert382.lean — T39 cycle 891 (goal G598): rank ≥ 2 for 3 of the 8 congruent curves in 77001–77200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert382
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (77010, (true, 32853826, 11, 180579333376), (true, 698224, 1, 579875032)),
   (77066, (true, 317417, 2, 42635691), (true, 22223445354, 145, 3304149409830192)),
   (77129, (true, 347633, 1, 199857372), (true, 32515713, 4, 185279305407))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert382

#print axioms Rank2Cert382.on_curve
#print axioms Rank2Cert382.indep
#print axioms Rank2Cert382.count

bsd/Rank2Cert383.lean

lean_all/bsd/Rank2Cert383.lean

/-
  bsd/Rank2Cert383.lean — T39 cycle 894 (goal G599): rank ≥ 2 for 3 of the 7 congruent curves in 77201–77400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert383
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (77226, (false, 1350723, 31, 86237892279), (true, 2715781, 5, 3147590179)),
   (77330, (true, 31864844, 19, 86729178228), (true, 223850, 1, 99389400)),
   (77361, (true, 961633235641, 1160, 937464041011078061), (true, 1057755025, 116, 6104305259065))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert383

#print axioms Rank2Cert383.on_curve
#print axioms Rank2Cert383.indep
#print axioms Rank2Cert383.count

bsd/Rank2Cert384.lean

lean_all/bsd/Rank2Cert384.lean

/-
  bsd/Rank2Cert384.lean — T39 cycle 896 (goal G600): rank ≥ 2 for 0 of the 5 congruent curves in 77401–77600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert384
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert384

#print axioms Rank2Cert384.on_curve
#print axioms Rank2Cert384.indep
#print axioms Rank2Cert384.count

bsd/Rank2Cert385.lean

lean_all/bsd/Rank2Cert385.lean

/-
  bsd/Rank2Cert385.lean — T39 cycle 898 (goal G601): rank ≥ 2 for 4 of the 7 congruent curves in 77601–77800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert385
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (77610, (true, 610090, 1, 472659200), (true, 98844096, 35, 268880652144)),
   (77649, (true, 819025, 2, 685851535), (true, 141858288, 41, 661669356348)),
   (77770, (true, 714070, 1, 599818800), (true, 295853726, 13, 5083778205576)),
   (77794, (false, 15842, 1, 9586368), (true, 7871958290539249, 274348, 466819268650114421286345))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert385

#print axioms Rank2Cert385.on_curve
#print axioms Rank2Cert385.indep
#print axioms Rank2Cert385.count

bsd/Rank2Cert386.lean

lean_all/bsd/Rank2Cert386.lean

/-
  bsd/Rank2Cert386.lean — T39 cycle 899 (goal G602): rank ≥ 2 for 1 of the 7 congruent curves in 77801–78000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert386
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (77891, (true, 221462737, 24, 3227386725991), (true, 1323689367289, 1236, 1516761091917628685))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert386

#print axioms Rank2Cert386.on_curve
#print axioms Rank2Cert386.indep
#print axioms Rank2Cert386.count

bsd/Rank2Cert387.lean

lean_all/bsd/Rank2Cert387.lean

/-
  bsd/Rank2Cert387.lean — T39 cycle 904 (goal G604): rank ≥ 2 for 2 of the 5 congruent curves in 78001–78200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert387
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (78090, (true, 86640, 1, 11046600), (false, 24046875, 187, 13390325038125)),
   (78195, (true, 28844400, 19, 31844720700), (false, 37838775, 22, 4666846275))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert387

#print axioms Rank2Cert387.on_curve
#print axioms Rank2Cert387.indep
#print axioms Rank2Cert387.count

bsd/Rank2Cert388.lean

lean_all/bsd/Rank2Cert388.lean

/-
  bsd/Rank2Cert388.lean — T39 cycle 907 (goal G605): rank ≥ 2 for 2 of the 3 congruent curves in 78201–78400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert388
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (78329, (false, 976144, 5, 1677212004), (true, 10919672540634169, 333915, 684939957624809280264572)),
   (78385, (true, 16114017481, 114, 2041437941914429), (true, 65891289849025, 21714, 442804115564169395975))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert388

#print axioms Rank2Cert388.on_curve
#print axioms Rank2Cert388.indep
#print axioms Rank2Cert388.count

bsd/Rank2Cert389.lean

lean_all/bsd/Rank2Cert389.lean

/-
  bsd/Rank2Cert389.lean — T39 cycle 909 (goal G606): rank ≥ 2 for 1 of the 5 congruent curves in 78401–78600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert389
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (78554, (true, 78750, 1, 1558200), (true, 60398478, 13, 457916415432))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert389

#print axioms Rank2Cert389.on_curve
#print axioms Rank2Cert389.indep
#print axioms Rank2Cert389.count

bsd/Rank2Cert39.lean

lean_all/bsd/Rank2Cert39.lean

/-
  bsd/Rank2Cert39.lean — T39 cycle 379 (goal G100): rank ≥ 2 for 6 of the 9 congruent curves in 8401–8600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert39
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (8515, (true, 34385, 2, 874575), (true, 110453199025, 812, 36661116370389225)),
   (8547, (true, 34225, 2, 294335), (true, 2213673, 4, 3287304405)),
   (8554, (true, 270494, 5, 86145528), (true, 2814266, 11, 4390254960)),
   (8555, (true, 34225, 2, 108225), (true, 238918849, 106, 3380894910393)),
   (8571, (true, 60777003, 71, 333230598792), (true, 729696935437446001, 8737716, 275809153640303544432621065)),
   (8593, (true, 61104128849569, 8755, 477617909248631367372), (true, 126118854617587225, 1674287, 43964334167458280878606260))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 6 := rfl

end Rank2Cert39

#print axioms Rank2Cert39.on_curve
#print axioms Rank2Cert39.indep
#print axioms Rank2Cert39.count

bsd/Rank2Cert390.lean

lean_all/bsd/Rank2Cert390.lean

/-
  bsd/Rank2Cert390.lean — T39 cycle 912 (goal G607): rank ≥ 2 for 2 of the 6 congruent curves in 78601–78800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert390
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (78657, (true, 2170332, 1, 3195242730), (false, 387361343, 76, 4672456544735)),
   (78793, (true, 883025, 2, 775119345), (true, 2401425828879025, 7664, 117680081269223323238505))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert390

#print axioms Rank2Cert390.on_curve
#print axioms Rank2Cert390.indep
#print axioms Rank2Cert390.count

bsd/Rank2Cert391.lean

lean_all/bsd/Rank2Cert391.lean

/-
  bsd/Rank2Cert391.lean — T39 cycle 914 (goal G609): rank ≥ 2 for 4 of the 9 congruent curves in 78801–79000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert391
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (78873, (true, 7096546081, 90, 595392657166729), (false, 5967557471, 3690, 82960754357622583)),
   (78881, (true, 7270400, 7, 16603844160), (true, 1284429692001481, 121599, 19278727244487396428860)),
   (78937, (false, 14808697, 991, 298322727911160), (true, 82287165217, 828, 17781516141285455)),
   (78995, (true, 93499, 1, 15294468), (false, 234239859, 59, 2204466535236))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert391

#print axioms Rank2Cert391.on_curve
#print axioms Rank2Cert391.indep
#print axioms Rank2Cert391.count

bsd/Rank2Cert392.lean

lean_all/bsd/Rank2Cert392.lean

/-
  bsd/Rank2Cert392.lean — T39 cycle 916 (goal G611): rank ≥ 2 for 2 of the 8 congruent curves in 79001–79200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert392
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (79035, (true, 86640, 1, 10448100), (true, 10066485, 7, 29480536800)),
   (79169, (false, 2128137232, 521, 986484662665788), (true, 14616901158742129, 428172, 209167169753849896497545))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert392

#print axioms Rank2Cert392.on_curve
#print axioms Rank2Cert392.indep
#print axioms Rank2Cert392.count

bsd/Rank2Cert393.lean

lean_all/bsd/Rank2Cert393.lean

/-
  bsd/Rank2Cert393.lean — T39 cycle 919 (goal G613): rank ≥ 2 for 2 of the 7 congruent curves in 79201–79400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert393
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (79339, (true, 5465313, 8, 4725972783), (true, 222278281, 52, 867208448475)),
   (79386, (false, 22095366, 25, 208822141152), (true, 109989, 1, 25247475))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert393

#print axioms Rank2Cert393.on_curve
#print axioms Rank2Cert393.indep
#print axioms Rank2Cert393.count

bsd/Rank2Cert394.lean

lean_all/bsd/Rank2Cert394.lean

/-
  bsd/Rank2Cert394.lean — T39 cycle 921 (goal G615): rank ≥ 2 for 1 of the 6 congruent curves in 79401–79600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert394
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (79417, (true, 106709333860441, 32895, 653487092800157323864), (true, 1248955534519225, 59304, 43020917053290929923885))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert394

#print axioms Rank2Cert394.on_curve
#print axioms Rank2Cert394.indep
#print axioms Rank2Cert394.count

bsd/Rank2Cert395.lean

lean_all/bsd/Rank2Cert395.lean

/-
  bsd/Rank2Cert395.lean — T39 cycle 924 (goal G616): rank ≥ 2 for 1 of the 6 congruent curves in 79601–79800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert395
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (79779, (true, 743932, 3, 167902714), (true, 40311189, 5, 255626686512))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert395

#print axioms Rank2Cert395.on_curve
#print axioms Rank2Cert395.indep
#print axioms Rank2Cert395.count

bsd/Rank2Cert396.lean

lean_all/bsd/Rank2Cert396.lean

/-
  bsd/Rank2Cert396.lean — T39 cycle 926 (goal G617): rank ≥ 2 for 1 of the 7 congruent curves in 79801–80000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert396
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (79898, (true, 26419302, 1, 135793825440), (false, 356965826, 2825, 12047198466928968))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert396

#print axioms Rank2Cert396.on_curve
#print axioms Rank2Cert396.indep
#print axioms Rank2Cert396.count

bsd/Rank2Cert397.lean

lean_all/bsd/Rank2Cert397.lean

/-
  bsd/Rank2Cert397.lean — T39 cycle 930 (goal G619): rank ≥ 2 for 3 of the 5 congruent curves in 80001–80200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert397
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (80065, (false, 623365775, 93, 7529478872300), (false, 789055871, 132, 32316632615233)),
   (80178, (true, 2025366, 5, 413174664), (false, 7399406, 15, 44754457328)),
   (80179, (true, 1435933, 3, 1487626588), (true, 10183840411369, 2706, 32444752384888971155))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert397

#print axioms Rank2Cert397.on_curve
#print axioms Rank2Cert397.indep
#print axioms Rank2Cert397.count

bsd/Rank2Cert398.lean

lean_all/bsd/Rank2Cert398.lean

/-
  bsd/Rank2Cert398.lean — T39 cycle 931 (goal G621): rank ≥ 2 for 2 of the 7 congruent curves in 80201–80400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert398
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (80234, (true, 629442, 1, 495309144), (true, 1441484, 1, 1727988900)),
   (80290, (true, 823690, 3, 358807400), (true, 1777850, 1, 2368096200))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert398

#print axioms Rank2Cert398.on_curve
#print axioms Rank2Cert398.indep
#print axioms Rank2Cert398.count

bsd/Rank2Cert399.lean

lean_all/bsd/Rank2Cert399.lean

/-
  bsd/Rank2Cert399.lean — T39 cycle 933 (goal G622): rank ≥ 2 for 3 of the 8 congruent curves in 80401–80600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert399
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (80410, (false, 86496850, 59, 2475826181400), (true, 2013174, 5, 153895968)),
   (80481, (true, 3112897, 4, 5000268511), (true, 4964592012139321, 48896, 349541635196397019758445)),
   (80569, (true, 688231, 1, 567028440), (true, 4482168601, 223, 134513606766840))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert399

#print axioms Rank2Cert399.on_curve
#print axioms Rank2Cert399.indep
#print axioms Rank2Cert399.count

bsd/Rank2Cert4.lean

lean_all/bsd/Rank2Cert4.lean

/-
  bsd/Rank2Cert4.lean — T39 cycle 326 (goal G61): rank ≥ 2 for 8 of the 8 congruent curves in 1401–1600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert4
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (1411, (true, 395641, 16, 101530035), (true, 106097, 4, 33767049)),
   (1419, (true, 5547, 1, 399384), (true, 52371, 5, 8816544)),
   (1434, (true, 28812, 1, 4884516), (true, 5766, 1, 424080)),
   (1443, (false, 1166400, 277, 119570907240), (true, 4107, 1, 246420)),
   (1482, (true, 212268, 1, 97794900), (true, 4693, 1, 305045)),
   (1513, (true, 2289169, 35, 2032782072), (true, 134657, 8, 34337535)),
   (1561, (true, 94192, 3, 28584836), (true, 3802183, 47, 3123630048)),
   (1595, (true, 3115225, 38, 3702290475), (true, 4205, 1, 252300))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 8 := rfl

end Rank2Cert4

#print axioms Rank2Cert4.on_curve
#print axioms Rank2Cert4.indep
#print axioms Rank2Cert4.count

bsd/Rank2Cert40.lean

lean_all/bsd/Rank2Cert40.lean

/-
  bsd/Rank2Cert40.lean — T39 cycle 380 (goal G101): rank ≥ 2 for 6 of the 7 congruent curves in 8601–8800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert40
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (8601, (true, 15228, 1, 1550718), (true, 4461072015625, 9542, 9276025979487003875)),
   (8690, (true, 21403690, 9, 98968731400), (true, 9900, 1, 471900)),
   (8705, (false, 108900, 29, 2415631350), (true, 3029288035225, 17798, 2182856689982716725)),
   (8729, (true, 12943, 1, 1087212), (true, 698188922929, 999, 583345191485294020)),
   (8762, (false, 234650, 7, 174164640), (true, 1993394345028025, 231546, 86493345622602539427955)),
   (8786, (true, 22898, 1, 3199728), (true, 119232, 1, 41058864))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 6 := rfl

end Rank2Cert40

#print axioms Rank2Cert40.on_curve
#print axioms Rank2Cert40.indep
#print axioms Rank2Cert40.count

bsd/Rank2Cert400.lean

lean_all/bsd/Rank2Cert400.lean

/-
  bsd/Rank2Cert400.lean — T39 cycle 935 (goal G623): rank ≥ 2 for 3 of the 8 congruent curves in 80601–80800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert400
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (80619, (true, 84408093, 19, 727935394032), (true, 112544124, 23, 1104901937370)),
   (80674, (true, 381467009, 46, 6662866265055), (false, 137241522, 509, 244851379794912)),
   (80699, (false, 25385828, 21, 125666683246), (true, 82908, 1, 5473902))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert400

#print axioms Rank2Cert400.on_curve
#print axioms Rank2Cert400.indep
#print axioms Rank2Cert400.count

bsd/Rank2Cert401.lean

lean_all/bsd/Rank2Cert401.lean

/-
  bsd/Rank2Cert401.lean — T39 cycle 937 (goal G625): rank ≥ 2 for 1 of the 3 congruent curves in 80801–81000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert401
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (80905, (true, 106724695, 3, 1102522028300), (true, 11890812025, 382, 154606354736325))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert401

#print axioms Rank2Cert401.on_curve
#print axioms Rank2Cert401.indep
#print axioms Rank2Cert401.count

bsd/Rank2Cert402.lean

lean_all/bsd/Rank2Cert402.lean

/-
  bsd/Rank2Cert402.lean — T39 cycle 950 (goal G626): rank ≥ 2 for 1 of the 9 congruent curves in 81001–81200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert402
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (81185, (true, 101400065, 14, 1008423646425), (true, 1745265577225, 2721, 2164586290659626300))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert402

#print axioms Rank2Cert402.on_curve
#print axioms Rank2Cert402.indep
#print axioms Rank2Cert402.count

bsd/Rank2Cert403.lean

lean_all/bsd/Rank2Cert403.lean

/-
  bsd/Rank2Cert403.lean — T39 cycle 952 (goal G627): rank ≥ 2 for 0 of the 5 congruent curves in 81201–81400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert403
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert403

#print axioms Rank2Cert403.on_curve
#print axioms Rank2Cert403.indep
#print axioms Rank2Cert403.count

bsd/Rank2Cert404.lean

lean_all/bsd/Rank2Cert404.lean

/-
  bsd/Rank2Cert404.lean — T39 cycle 955 (goal G628): rank ≥ 2 for 5 of the 8 congruent curves in 81401–81600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert404
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (81403, (true, 6640732, 9, 2034320750), (true, 6580235648809, 4700, 16237110560923505877)),
   (81426, (true, 40428009, 4, 256919997195), (true, 396062618793, 46, 249256068706028043)),
   (81481, (true, 173417, 1, 63748776), (true, 1251340855865109529, 2656204, 1243326701944567254924162045)),
   (81489, (true, 68916303, 29, 60315261732), (true, 1605828787369, 2489, 1931753275530916220)),
   (81490, (true, 25798329, 14, 102902212083), (true, 71829710008081, 29680, 21539178568010891271))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 5 := rfl

end Rank2Cert404

#print axioms Rank2Cert404.on_curve
#print axioms Rank2Cert404.indep
#print axioms Rank2Cert404.count

bsd/Rank2Cert405.lean

lean_all/bsd/Rank2Cert405.lean

/-
  bsd/Rank2Cert405.lean — T39 cycle 957 (goal G629): rank ≥ 2 for 1 of the 6 congruent curves in 81601–81800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert405
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (81690, (false, 23362115, 29, 312274414025), (true, 31777410, 19, 66732561000))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert405

#print axioms Rank2Cert405.on_curve
#print axioms Rank2Cert405.indep
#print axioms Rank2Cert405.count

bsd/Rank2Cert406.lean

lean_all/bsd/Rank2Cert406.lean

/-
  bsd/Rank2Cert406.lean — T39 cycle 959 (goal G630): rank ≥ 2 for 1 of the 3 congruent curves in 81801–82000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert406
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (81842, (true, 29233600, 7, 156566258640), (false, 28273159, 50, 1077499105389))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert406

#print axioms Rank2Cert406.on_curve
#print axioms Rank2Cert406.indep
#print axioms Rank2Cert406.count

bsd/Rank2Cert407.lean

lean_all/bsd/Rank2Cert407.lean

/-
  bsd/Rank2Cert407.lean — T39 cycle 962 (goal G632): rank ≥ 2 for 2 of the 5 congruent curves in 82001–82200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert407
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (82041, (true, 174087, 1, 64064016), (false, 12903023, 48, 677399628871)),
   (82042, (true, 208658, 1, 87636360), (true, 73232332453, 921, 6171043728291085))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert407

#print axioms Rank2Cert407.on_curve
#print axioms Rank2Cert407.indep
#print axioms Rank2Cert407.count

bsd/Rank2Cert408.lean

lean_all/bsd/Rank2Cert408.lean

/-
  bsd/Rank2Cert408.lean — T39 cycle 964 (goal G633): rank ≥ 2 for 2 of the 7 congruent curves in 82201–82400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert408
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (82290, (false, 2275290, 7, 5021379000), (true, 111124416, 5, 1171223563536)),
   (82297, (true, 1088153, 1, 1131851160), (true, 5030623, 3, 11160243640))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert408

#print axioms Rank2Cert408.on_curve
#print axioms Rank2Cert408.indep
#print axioms Rank2Cert408.count

bsd/Rank2Cert409.lean

lean_all/bsd/Rank2Cert409.lean

/-
  bsd/Rank2Cert409.lean — T39 cycle 966 (goal G634): rank ≥ 2 for 2 of the 7 congruent curves in 82401–82600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert409
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (82410, (true, 1656441, 4, 1290367539), (false, 204938090, 87, 8433837218800)),
   (82555, (true, 3783025, 2, 7329897825), (true, 13803196, 5, 50706040506))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert409

#print axioms Rank2Cert409.on_curve
#print axioms Rank2Cert409.indep
#print axioms Rank2Cert409.count

bsd/Rank2Cert41.lean

lean_all/bsd/Rank2Cert41.lean

/-
  bsd/Rank2Cert41.lean — T39 cycle 381 (goal G102): rank ≥ 2 for 9 of the 12 congruent curves in 8801–9000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert41
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (8809, (true, 15053063, 41, 10497027408), (true, 38800441, 7, 241672991160)),
   (8835, (true, 20815260, 43, 58855147650), (true, 23275, 1, 3285100)),
   (8866, (true, 21142, 1, 2790744), (false, 1193237678, 477, 56185358861744)),
   (8890, (true, 7903210, 13, 21812859600), (true, 225806, 5, 18967704)),
   (8897, (true, 2553439, 15, 2533011488), (true, 169650625, 127, 1178684558400)),
   (8906, (true, 168437353, 54, 2159898125077), (true, 706354521601, 8436, 262063283938273025)),
   (8931, (false, 7056, 1, 459900), (true, 157323, 1, 62299908)),
   (8970, (true, 2682030, 17, 1126452600), (true, 9984, 1, 438048)),
   (8979, (true, 835777, 8, 554807519), (true, 309513649, 184, 1023867298025))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 9 := rfl

end Rank2Cert41

#print axioms Rank2Cert41.on_curve
#print axioms Rank2Cert41.indep
#print axioms Rank2Cert41.count

bsd/Rank2Cert410.lean

lean_all/bsd/Rank2Cert410.lean

/-
  bsd/Rank2Cert410.lean — T39 cycle 968 (goal G635): rank ≥ 2 for 1 of the 6 congruent curves in 82601–82800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert410
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (82786, (true, 1996236, 1, 2818019820), (true, 141334517, 7, 1679551805259))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert410

#print axioms Rank2Cert410.on_curve
#print axioms Rank2Cert410.indep
#print axioms Rank2Cert410.count

bsd/Rank2Cert411.lean

lean_all/bsd/Rank2Cert411.lean

/-
  bsd/Rank2Cert411.lean — T39 cycle 970 (goal G636): rank ≥ 2 for 2 of the 2 congruent curves in 82801–83000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert411
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (82915, (false, 17792915, 39, 526647325400), (false, 254035, 7, 2043738600)),
   (82945, (false, 576, 1, 1990632), (true, 58665422545, 528, 13058941544054425))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert411

#print axioms Rank2Cert411.on_curve
#print axioms Rank2Cert411.indep
#print axioms Rank2Cert411.count

bsd/Rank2Cert412.lean

lean_all/bsd/Rank2Cert412.lean

/-
  bsd/Rank2Cert412.lean — T39 cycle 972 (goal G639): rank ≥ 2 for 2 of the 5 congruent curves in 83001–83200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert412
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (83129, (true, 71241553, 4, 601207465767), (true, 356929012279225, 3201, 6743285363709288660320)),
   (83139, (false, 16058259, 25, 198032771664), (false, 264729339, 143, 27324231183900))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert412

#print axioms Rank2Cert412.on_curve
#print axioms Rank2Cert412.indep
#print axioms Rank2Cert412.count

bsd/Rank2Cert413.lean

lean_all/bsd/Rank2Cert413.lean

/-
  bsd/Rank2Cert413.lean — T39 cycle 974 (goal G640): rank ≥ 2 for 2 of the 7 congruent curves in 83201–83400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert413
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (83265, (true, 93025, 1, 12651400), (true, 145119, 1, 45277128)),
   (83345, (true, 197500, 1, 79572750), (true, 20367033610225, 4202, 91675870522031568225))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert413

#print axioms Rank2Cert413.on_curve
#print axioms Rank2Cert413.indep
#print axioms Rank2Cert413.count

bsd/Rank2Cert414.lean

lean_all/bsd/Rank2Cert414.lean

/-
  bsd/Rank2Cert414.lean — T39 cycle 976 (goal G641): rank ≥ 2 for 6 of the 10 congruent curves in 83401–83600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert414
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (83410, (true, 1187500, 3, 1002677500), (true, 34338570781, 537, 4541327789811479)),
   (83419, (false, 72214079, 180, 22959681333281), (true, 3713517848401, 3770, 6781598938456727601)),
   (83426, (false, 647150, 3, 306273520), (true, 1670998, 1, 2157356712)),
   (83505, (true, 85849, 1, 5837732), (false, 519737495, 631, 757896942878900)),
   (83569, (false, 14400, 1, 9878280), (true, 10992095559744445969, 4555793, 35986988061637752785778074640)),
   (83585, (false, 4624, 1, 5675076), (true, 485412244089855625, 834813, 335750154885361941837596000))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 6 := rfl

end Rank2Cert414

#print axioms Rank2Cert414.on_curve
#print axioms Rank2Cert414.indep
#print axioms Rank2Cert414.count

bsd/Rank2Cert415.lean

lean_all/bsd/Rank2Cert415.lean

/-
  bsd/Rank2Cert415.lean — T39 cycle 978 (goal G642): rank ≥ 2 for 3 of the 6 congruent curves in 83601–83800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert415
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (83681, (false, 82369, 1, 4236120), (true, 1251823084801, 1023, 1397169481908834440)),
   (83762, (true, 114688, 1, 26530560), (false, 764119, 10, 7291432779)),
   (83793, (false, 69632, 1, 12299840), (true, 8739006104761, 2990, 25738992845187926491))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert415

#print axioms Rank2Cert415.on_curve
#print axioms Rank2Cert415.indep
#print axioms Rank2Cert415.count

bsd/Rank2Cert416.lean

lean_all/bsd/Rank2Cert416.lean

/-
  bsd/Rank2Cert416.lean — T39 cycle 980 (goal G644): rank ≥ 2 for 1 of the 3 congruent curves in 83801–84000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert416
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (83930, (false, 2420, 3, 37159100), (true, 212725381, 35, 2716173139779))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert416

#print axioms Rank2Cert416.on_curve
#print axioms Rank2Cert416.indep
#print axioms Rank2Cert416.count

bsd/Rank2Cert417.lean

lean_all/bsd/Rank2Cert417.lean

/-
  bsd/Rank2Cert417.lean — T39 cycle 982 (goal G645): rank ≥ 2 for 0 of the 6 congruent curves in 84001–84200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert417
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert417

#print axioms Rank2Cert417.on_curve
#print axioms Rank2Cert417.indep
#print axioms Rank2Cert417.count

bsd/Rank2Cert418.lean

lean_all/bsd/Rank2Cert418.lean

/-
  bsd/Rank2Cert418.lean — T39 cycle 984 (goal G647): rank ≥ 2 for 2 of the 6 congruent curves in 84201–84400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert418
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (84315, (true, 11015235, 1, 36557621100), (true, 138765, 1, 41055300)),
   (84354, (false, 14323314162, 1781, 31976588071993248), (true, 486706088793, 2014, 241481166904716093))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert418

#print axioms Rank2Cert418.on_curve
#print axioms Rank2Cert418.indep
#print axioms Rank2Cert418.count

bsd/Rank2Cert419.lean

lean_all/bsd/Rank2Cert419.lean

/-
  bsd/Rank2Cert419.lean — T39 cycle 986 (goal G649): rank ≥ 2 for 2 of the 7 congruent curves in 84401–84600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert419
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (84545, (false, 18496, 1, 11219592), (true, 1379604318407823277225, 5142483, 51242610624845348371142216243600)),
   (84570, (false, 54150, 1, 15116400), (false, 98609070, 121, 12256426438200))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert419

#print axioms Rank2Cert419.on_curve
#print axioms Rank2Cert419.indep
#print axioms Rank2Cert419.count

bsd/Rank2Cert42.lean

lean_all/bsd/Rank2Cert42.lean

/-
  bsd/Rank2Cert42.lean — T39 cycle 382 (goal G103): rank ≥ 2 for 4 of the 7 congruent curves in 9001–9200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert42
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (9089, (true, 555025, 2, 412605585), (true, 59360938206025, 41634, 440950359010049701885)),
   (9121, (false, 4626783, 92, 165758689551), (true, 54302161, 71, 212893947120)),
   (9131, (true, 23092092, 49, 34852903674), (false, 83642927, 96, 84468083993)),
   (9170, (true, 12005, 1, 848925), (true, 54057150, 43, 377390983200))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert42

#print axioms Rank2Cert42.on_curve
#print axioms Rank2Cert42.indep
#print axioms Rank2Cert42.count

bsd/Rank2Cert420.lean

lean_all/bsd/Rank2Cert420.lean

/-
  bsd/Rank2Cert420.lean — T39 cycle 988 (goal G650): rank ≥ 2 for 3 of the 5 congruent curves in 84601–84800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert420
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (84651, (false, 302847, 26, 31490847207), (true, 5728051, 5, 12739185424)),
   (84706, (true, 27040272850, 173, 4426897168883280), (true, 173942839234, 633, 71150938457692960)),
   (84714, (false, 70602, 1, 12439728), (true, 15138103369, 352, 1342105779023045))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert420

#print axioms Rank2Cert420.on_curve
#print axioms Rank2Cert420.indep
#print axioms Rank2Cert420.count

bsd/Rank2Cert421.lean

lean_all/bsd/Rank2Cert421.lean

/-
  bsd/Rank2Cert421.lean — T39 cycle 990 (goal G651): rank ≥ 2 for 1 of the 4 congruent curves in 84801–85000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert421
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (84882, (true, 6527094, 5, 15769634328), (true, 13963089, 10, 41428485063))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert421

#print axioms Rank2Cert421.on_curve
#print axioms Rank2Cert421.indep
#print axioms Rank2Cert421.count

bsd/Rank2Cert422.lean

lean_all/bsd/Rank2Cert422.lean

/-
  bsd/Rank2Cert422.lean — T39 cycle 992 (goal G652): rank ≥ 2 for 3 of the 9 congruent curves in 85001–85200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert422
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (85090, (true, 365257334, 7, 6980236233048), (false, 3661971086, 309, 438872550124112)),
   (85115, (true, 1102725, 1, 1154524800), (true, 895646949412795721089, 10358558, 26802956228853227312130702834087)),
   (85195, (false, 1936, 1, 3747612), (true, 5807202025, 38, 442438519197075))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert422

#print axioms Rank2Cert422.on_curve
#print axioms Rank2Cert422.indep
#print axioms Rank2Cert422.count

bsd/Rank2Cert423.lean

lean_all/bsd/Rank2Cert423.lean

/-
  bsd/Rank2Cert423.lean — T39 cycle 994 (goal G653): rank ≥ 2 for 3 of the 7 congruent curves in 85201–85400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert423
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (85210, (true, 3260410, 1, 5885182800), (true, 955046819634570961, 2188734, 843784675171066566435578759)),
   (85314, (true, 3187225, 2, 5657379805), (true, 613110025, 6, 15181062802235)),
   (85386, (true, 1282288, 3, 1162394072), (true, 1390572, 1, 1636703244))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert423

#print axioms Rank2Cert423.on_curve
#print axioms Rank2Cert423.indep
#print axioms Rank2Cert423.count

bsd/Rank2Cert424.lean

lean_all/bsd/Rank2Cert424.lean

/-
  bsd/Rank2Cert424.lean — T39 cycle 996 (goal G654): rank ≥ 2 for 0 of the 6 congruent curves in 85401–85600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert424
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert424

#print axioms Rank2Cert424.on_curve
#print axioms Rank2Cert424.indep
#print axioms Rank2Cert424.count

bsd/Rank2Cert425.lean

lean_all/bsd/Rank2Cert425.lean

/-
  bsd/Rank2Cert425.lean — T39 cycle 998 (goal G657): rank ≥ 2 for 2 of the 4 congruent curves in 85601–85800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert425
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (85690, (false, 1361610, 13, 16823448000), (true, 1790921, 4, 1541982981)),
   (85785, (true, 86640, 1, 3573900), (true, 2281881, 5, 1177450596))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert425

#print axioms Rank2Cert425.on_curve
#print axioms Rank2Cert425.indep
#print axioms Rank2Cert425.count

bsd/Rank2Cert426.lean

lean_all/bsd/Rank2Cert426.lean

/-
  bsd/Rank2Cert426.lean — T39 cycle 1000 (goal G658): rank ≥ 2 for 0 of the 7 congruent curves in 85801–86000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert426
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert426

#print axioms Rank2Cert426.on_curve
#print axioms Rank2Cert426.indep
#print axioms Rank2Cert426.count

bsd/Rank2Cert427.lean

lean_all/bsd/Rank2Cert427.lean

/-
  bsd/Rank2Cert427.lean — T39 cycle 1002 (goal G660): rank ≥ 2 for 2 of the 4 congruent curves in 86001–86200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert427
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (86001, (false, 839856047, 204, 100824724552103), (true, 14669104569, 113, 1771679041119540)),
   (86019, (true, 124656, 1, 31854060), (true, 12226230210121225, 184414, 1312613938184120667320995))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert427

#print axioms Rank2Cert427.on_curve
#print axioms Rank2Cert427.indep
#print axioms Rank2Cert427.count

bsd/Rank2Cert428.lean

lean_all/bsd/Rank2Cert428.lean

/-
  bsd/Rank2Cert428.lean — T39 cycle 1004 (goal G661): rank ≥ 2 for 2 of the 7 congruent curves in 86201–86400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert428
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (86305, (true, 3553225, 2, 6666142275), (true, 3039307576321, 4294, 4514282369515566681)),
   (86385, (false, 1115465, 11, 10976516200), (false, 18813860, 29, 304368214150))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert428

#print axioms Rank2Cert428.on_curve
#print axioms Rank2Cert428.indep
#print axioms Rank2Cert428.count

bsd/Rank2Cert429.lean

lean_all/bsd/Rank2Cert429.lean

/-
  bsd/Rank2Cert429.lean — T39 cycle 1006 (goal G662): rank ≥ 2 for 1 of the 6 congruent curves in 86401–86600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert429
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (86554, (false, 67600, 1, 14054040), (true, 916447194, 17, 27733158529440))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert429

#print axioms Rank2Cert429.on_curve
#print axioms Rank2Cert429.indep
#print axioms Rank2Cert429.count

bsd/Rank2Cert43.lean

lean_all/bsd/Rank2Cert43.lean

/-
  bsd/Rank2Cert43.lean — T39 cycle 383 (goal G104): rank ≥ 2 for 6 of the 10 congruent curves in 9201–9400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert43
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (9226, (false, 1973727, 16, 1822448817), (true, 407484350, 107, 7944411263880)),
   (9339, (true, 2642937, 16, 1831555341), (false, 12601872272, 2099, 4396974516013508)),
   (9345, (true, 198025, 2, 86536925), (true, 981225, 4, 960619275)),
   (9347, (true, 14524237, 23, 52046510880), (true, 4589171759230800625, 3623274, 9827570360903712240291992225)),
   (9354, (true, 11532, 1, 724284), (true, 22372566, 23, 103200672960)),
   (9379, (true, 26683587, 1, 137837151144), (true, 702871350381841, 142252, 17942146697898349474695))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 6 := rfl

end Rank2Cert43

#print axioms Rank2Cert43.on_curve
#print axioms Rank2Cert43.indep
#print axioms Rank2Cert43.count

bsd/Rank2Cert430.lean

lean_all/bsd/Rank2Cert430.lean

/-
  bsd/Rank2Cert430.lean — T39 cycle 1010 (goal G663): rank ≥ 2 for 0 of the 8 congruent curves in 86601–86800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert430
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert430

#print axioms Rank2Cert430.on_curve
#print axioms Rank2Cert430.indep
#print axioms Rank2Cert430.count

bsd/Rank2Cert431.lean

lean_all/bsd/Rank2Cert431.lean

/-
  bsd/Rank2Cert431.lean — T39 cycle 1012 (goal G664): rank ≥ 2 for 5 of the 10 congruent curves in 86801–87000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert431
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (86835, (false, 557949, 11, 7837259724), (true, 91035, 1, 8246700)),
   (86905, (true, 65364975, 11, 521583449400), (true, 4864365025, 141, 317143470831200)),
   (86946, (true, 619458, 1, 482722128), (true, 89573942850, 929, 14640085945663920)),
   (86955, (false, 1093455, 4, 899541225), (true, 403155, 1, 249956100)),
   (86970, (true, 11636586, 11, 16942869216), (false, 3346395, 7, 4826855475))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 5 := rfl

end Rank2Cert431

#print axioms Rank2Cert431.on_curve
#print axioms Rank2Cert431.indep
#print axioms Rank2Cert431.count

bsd/Rank2Cert432.lean

lean_all/bsd/Rank2Cert432.lean

/-
  bsd/Rank2Cert432.lean — T39 cycle 1014 (goal G665): rank ≥ 2 for 1 of the 4 congruent curves in 87001–87200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert432
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (87106, (true, 31820850, 19, 27497614320), (true, 1779376633, 66, 73332476189587))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert432

#print axioms Rank2Cert432.on_curve
#print axioms Rank2Cert432.indep
#print axioms Rank2Cert432.count

bsd/Rank2Cert433.lean

lean_all/bsd/Rank2Cert433.lean

/-
  bsd/Rank2Cert433.lean — T39 cycle 1016 (goal G666): rank ≥ 2 for 0 of the 7 congruent curves in 87201–87400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert433
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert433

#print axioms Rank2Cert433.on_curve
#print axioms Rank2Cert433.indep
#print axioms Rank2Cert433.count

bsd/Rank2Cert434.lean

lean_all/bsd/Rank2Cert434.lean

/-
  bsd/Rank2Cert434.lean — T39 cycle 1018 (goal G668): rank ≥ 2 for 0 of the 6 congruent curves in 87401–87600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert434
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert434

#print axioms Rank2Cert434.on_curve
#print axioms Rank2Cert434.indep
#print axioms Rank2Cert434.count

bsd/Rank2Cert435.lean

lean_all/bsd/Rank2Cert435.lean

/-
  bsd/Rank2Cert435.lean — T39 cycle 1020 (goal G669): rank ≥ 2 for 3 of the 9 congruent curves in 87601–87800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert435
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (87601, (false, 2076481, 41, 212176644240), (false, 2873, 3, 42258736)),
   (87666, (true, 191634, 1, 74597040), (true, 6165790576201, 4300, 14771740756876373051)),
   (87730, (false, 19220, 23, 6433991100), (false, 914339606, 1143, 3465616770020872))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert435

#print axioms Rank2Cert435.on_curve
#print axioms Rank2Cert435.indep
#print axioms Rank2Cert435.count

bsd/Rank2Cert436.lean

lean_all/bsd/Rank2Cert436.lean

/-
  bsd/Rank2Cert436.lean — T39 cycle 1022 (goal G670): rank ≥ 2 for 2 of the 6 congruent curves in 87801–88000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert436
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (87841, (true, 354393, 2, 27524523), (false, 659185892, 179, 70251679469418)),
   (87843, (true, 11342961, 10, 24169180959), (false, 1521141887, 204, 129648534011585))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert436

#print axioms Rank2Cert436.on_curve
#print axioms Rank2Cert436.indep
#print axioms Rank2Cert436.count

bsd/Rank2Cert437.lean

lean_all/bsd/Rank2Cert437.lean

/-
  bsd/Rank2Cert437.lean — T39 cycle 1024 (goal G677): rank ≥ 2 for 0 of the 6 congruent curves in 88001–88200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert437
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert437

#print axioms Rank2Cert437.on_curve
#print axioms Rank2Cert437.indep
#print axioms Rank2Cert437.count

bsd/Rank2Cert438.lean

lean_all/bsd/Rank2Cert438.lean

/-
  bsd/Rank2Cert438.lean — T39 cycle 1026 (goal G679): rank ≥ 2 for 3 of the 4 congruent curves in 88201–88400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert438
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (88242, (true, 889350, 1, 834566040), (false, 43552278, 71, 2921499808920)),
   (88314, (true, 644805606, 73, 11192710152960), (true, 1173207333, 67, 37821059068653)),
   (88395, (false, 3049805, 63, 612672553900), (false, 696024431, 172, 66503123224903))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert438

#print axioms Rank2Cert438.on_curve
#print axioms Rank2Cert438.indep
#print axioms Rank2Cert438.count

bsd/Rank2Cert439.lean

lean_all/bsd/Rank2Cert439.lean

/-
  bsd/Rank2Cert439.lean — T39 cycle 1028 (goal G680): rank ≥ 2 for 2 of the 8 congruent curves in 88401–88600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert439
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (88426, (false, 103580438, 159, 22727224958464), (true, 248395436, 17, 3894079224660)),
   (88465, (false, 624260, 3, 390449150), (false, 9485060, 19, 93917364450))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert439

#print axioms Rank2Cert439.on_curve
#print axioms Rank2Cert439.indep
#print axioms Rank2Cert439.count

bsd/Rank2Cert44.lean

lean_all/bsd/Rank2Cert44.lean

/-
  bsd/Rank2Cert44.lean — T39 cycle 384 (goal G105): rank ≥ 2 for 10 of the 14 congruent curves in 9401–9600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert44
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (9401, (true, 10201, 1, 399960), (true, 46788777, 8, 320019638421)),
   (9434, (true, 23973129, 50, 21035981883), (true, 27551020225, 1408, 3357958039173855)),
   (9435, (true, 116365, 1, 39564100), (true, 2609677225, 322, 123594300349325)),
   (9465, (false, 49195815, 158, 1620971815275), (true, 10060925865, 862, 721635228378675)),
   (9483, (true, 4134588, 17, 6294910230), (true, 24124295899201, 2910, 118489417370416412551)),
   (9490, (true, 346385, 2, 202635225), (false, 3869840, 33, 18851037400)),
   (9546, (true, 68413, 1, 17718967), (false, 2413995662, 2083, 2031558562767448)),
   (9554, (true, 1974025, 14, 877651515), (true, 2684674, 5, 4381387968)),
   (9579, (false, 96123, 7, 142438176), (true, 1315516, 11, 713667430)),
   (9595, (true, 1723969, 10, 1880584953), (true, 18705625, 22, 78368935125))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 10 := rfl

end Rank2Cert44

#print axioms Rank2Cert44.on_curve
#print axioms Rank2Cert44.indep
#print axioms Rank2Cert44.count

bsd/Rank2Cert440.lean

lean_all/bsd/Rank2Cert440.lean

/-
  bsd/Rank2Cert440.lean — T39 cycle 1030 (goal G681): rank ≥ 2 for 3 of the 11 congruent curves in 88601–88800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert440
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (88689, (true, 180625, 1, 66874600), (true, 579087, 1, 435473424)),
   (88770, (true, 1809025, 4, 1506917825), (true, 89100, 1, 2286900)),
   (88795, (true, 453005, 1, 298983300), (false, 11681509, 3935, 4699238192880336))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert440

#print axioms Rank2Cert440.on_curve
#print axioms Rank2Cert440.indep
#print axioms Rank2Cert440.count

bsd/Rank2Cert441.lean

lean_all/bsd/Rank2Cert441.lean

/-
  bsd/Rank2Cert441.lean — T39 cycle 1032 (goal G682): rank ≥ 2 for 4 of the 9 congruent curves in 88801–89000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert441
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (88881, (true, 1338649, 2, 1493195275), (true, 75261696, 25, 440520818256)),
   (88939, (true, 504811, 1, 353058000), (true, 4476400, 3, 9318298060)),
   (88978, (false, 18795424850, 1049, 13173666422701680), (true, 3631870225, 202, 5653856856615)),
   (88995, (true, 123420, 1, 30041550), (true, 97776835439089, 24242, 816901348252808375713))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert441

#print axioms Rank2Cert441.on_curve
#print axioms Rank2Cert441.indep
#print axioms Rank2Cert441.count

bsd/Rank2Cert442.lean

lean_all/bsd/Rank2Cert442.lean

/-
  bsd/Rank2Cert442.lean — T39 cycle 1034 (goal G688): rank ≥ 2 for 3 of the 10 congruent curves in 89001–89200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert442
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (89059, (true, 242984243, 43, 2785192471068), (true, 19325528158561, 10136, 74831245108883472495)),
   (89162, (false, 132971498, 73, 5260114099680), (true, 388908905189625769, 1743360, 173961728706935837724775147)),
   (89194, (false, 60543, 4, 350829759), (false, 431822447, 302, 168806587619943))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert442

#print axioms Rank2Cert442.on_curve
#print axioms Rank2Cert442.indep
#print axioms Rank2Cert442.count

bsd/Rank2Cert443.lean

lean_all/bsd/Rank2Cert443.lean

/-
  bsd/Rank2Cert443.lean — T39 cycle 1040 (goal G689): rank ≥ 2 for 0 of the 10 congruent curves in 89201–89400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert443
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert443

#print axioms Rank2Cert443.on_curve
#print axioms Rank2Cert443.indep
#print axioms Rank2Cert443.count

bsd/Rank2Cert444.lean

lean_all/bsd/Rank2Cert444.lean

/-
  bsd/Rank2Cert444.lean — T39 cycle 1042 (goal G690): rank ≥ 2 for 3 of the 6 congruent curves in 89401–89600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert444
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (89434, (true, 19036153, 6, 81859024493), (true, 1995699533602009, 62568, 87771723102477576623645)),
   (89466, (false, 3459600, 17, 47659103640), (true, 763902, 1, 663066936)),
   (89490, (false, 7290, 7, 374398200), (true, 93100, 1, 7833700))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert444

#print axioms Rank2Cert444.on_curve
#print axioms Rank2Cert444.indep
#print axioms Rank2Cert444.count

bsd/Rank2Cert445.lean

lean_all/bsd/Rank2Cert445.lean

/-
  bsd/Rank2Cert445.lean — T39 cycle 1044 (goal G691): rank ≥ 2 for 3 of the 5 congruent curves in 89601–89800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert445
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (89609, (true, 420473, 2, 142540347), (true, 73956353, 8, 634093817343)),
   (89635, (false, 12740, 27, 7375472650), (false, 10714340, 11, 5511419550)),
   (89754, (true, 146202, 1, 44128224), (true, 6456137237449, 6412, 13460667052426638085))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert445

#print axioms Rank2Cert445.on_curve
#print axioms Rank2Cert445.indep
#print axioms Rank2Cert445.count

bsd/Rank2Cert446.lean

lean_all/bsd/Rank2Cert446.lean

/-
  bsd/Rank2Cert446.lean — T39 cycle 1046 (goal G692): rank ≥ 2 for 2 of the 4 congruent curves in 89801–90000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert446
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (89835, (true, 674160, 1, 548597700), (true, 6741943069, 241, 350572575621772)),
   (89954, (false, 63504, 1, 16054920), (true, 44171057137, 326, 9063357901314519))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert446

#print axioms Rank2Cert446.on_curve
#print axioms Rank2Cert446.indep
#print axioms Rank2Cert446.count

bsd/Rank2Cert447.lean

lean_all/bsd/Rank2Cert447.lean

/-
  bsd/Rank2Cert447.lean — T39 cycle 1048 (goal G693): rank ≥ 2 for 0 of the 4 congruent curves in 90001–90200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert447
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert447

#print axioms Rank2Cert447.on_curve
#print axioms Rank2Cert447.indep
#print axioms Rank2Cert447.count

bsd/Rank2Cert448.lean

lean_all/bsd/Rank2Cert448.lean

/-
  bsd/Rank2Cert448.lean — T39 cycle 1050 (goal G694): rank ≥ 2 for 3 of the 6 congruent curves in 90201–90400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert448
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (90321, (false, 735488, 7, 3742753456), (false, 55473, 1, 16788024)),
   (90345, (true, 1655280, 1, 2126470500), (false, 287291495, 67, 4851915557300)),
   (90370, (false, 19394606, 27, 277272405872), (true, 116667670, 9, 1257677482600))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert448

#print axioms Rank2Cert448.on_curve
#print axioms Rank2Cert448.indep
#print axioms Rank2Cert448.count

bsd/Rank2Cert449.lean

lean_all/bsd/Rank2Cert449.lean

/-
  bsd/Rank2Cert449.lean — T39 cycle 1052 (goal G700): rank ≥ 2 for 1 of the 5 congruent curves in 90401–90600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert449
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (90465, (true, 268468225, 53, 1418997036700), (true, 512620311, 71, 5300755454484))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert449

#print axioms Rank2Cert449.on_curve
#print axioms Rank2Cert449.indep
#print axioms Rank2Cert449.count

bsd/Rank2Cert45.lean

lean_all/bsd/Rank2Cert45.lean

/-
  bsd/Rank2Cert45.lean — T39 cycle 385 (goal G106): rank ≥ 2 for 6 of the 10 congruent curves in 9601–9800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert45
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (9618, (true, 2202522, 11, 2775177720), (false, 1955180703, 1702, 1228924896170895)),
   (9641, (true, 48841, 1, 10581480), (false, 147591, 7, 172403400)),
   (9690, (true, 86640, 1, 25342200), (true, 21964, 1, 2921212)),
   (9730, (true, 19845, 1, 2436525), (false, 30926, 3, 14407568)),
   (9731, (true, 1465237, 7, 1677085236), (true, 5831533, 7, 14035163232)),
   (9779, (false, 48223, 4, 32686479), (false, 12880637, 39, 26693970688))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 6 := rfl

end Rank2Cert45

#print axioms Rank2Cert45.on_curve
#print axioms Rank2Cert45.indep
#print axioms Rank2Cert45.count

bsd/Rank2Cert450.lean

lean_all/bsd/Rank2Cert450.lean

/-
  bsd/Rank2Cert450.lean — T39 cycle 1054 (goal G701): rank ≥ 2 for 1 of the 6 congruent curves in 90601–90800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert450
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (90706, (true, 7632262, 9, 5708931976), (true, 91238, 1, 2971752))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert450

#print axioms Rank2Cert450.on_curve
#print axioms Rank2Cert450.indep
#print axioms Rank2Cert450.count

bsd/Rank2Cert451.lean

lean_all/bsd/Rank2Cert451.lean

/-
  bsd/Rank2Cert451.lean — T39 cycle 1056 (goal G702): rank ≥ 2 for 2 of the 4 congruent curves in 90801–91000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert451
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (90865, (true, 167209855831225, 42798, 207969362376128677925), (true, 93161123304001, 28362, 557526711443967145799)),
   (90915, (true, 50094165, 23, 99186446700), (true, 57605625, 2, 437208589125))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert451

#print axioms Rank2Cert451.on_curve
#print axioms Rank2Cert451.indep
#print axioms Rank2Cert451.count

bsd/Rank2Cert452.lean

lean_all/bsd/Rank2Cert452.lean

/-
  bsd/Rank2Cert452.lean — T39 cycle 1058 (goal G703): rank ≥ 2 for 4 of the 10 congruent curves in 91001–91200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert452
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (91001, (true, 2948089, 5, 3219313188), (true, 2094417, 4, 2178808281)),
   (91065, (false, 40978340, 29, 414175152950), (false, 158386865, 107, 12969061487200)),
   (91154, (true, 126002, 1, 30879024), (true, 616896, 1, 479208240)),
   (91177, (true, 175273, 1, 62669040), (true, 323167387598449, 42060, 5034185454589366534793))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert452

#print axioms Rank2Cert452.on_curve
#print axioms Rank2Cert452.indep
#print axioms Rank2Cert452.count

bsd/Rank2Cert453.lean

lean_all/bsd/Rank2Cert453.lean

/-
  bsd/Rank2Cert453.lean — T39 cycle 1060 (goal G704): rank ≥ 2 for 1 of the 5 congruent curves in 91201–91400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert453
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (91210, (true, 152460, 1, 47701500), (false, 146513150, 247, 67332397528200))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert453

#print axioms Rank2Cert453.on_curve
#print axioms Rank2Cert453.indep
#print axioms Rank2Cert453.count

bsd/Rank2Cert454.lean

lean_all/bsd/Rank2Cert454.lean

/-
  bsd/Rank2Cert454.lean — T39 cycle 1062 (goal G705): rank ≥ 2 for 3 of the 8 congruent curves in 91401–91600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert454
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (91403, (false, 153295471, 80, 6989667008583), (true, 48593795355625, 23002, 33146480001295686675)),
   (91531, (true, 2440251, 5, 1324242876), (true, 1668541866188521, 102440, 55730357510128656510069)),
   (91546, (true, 1789333, 3, 2124681013), (false, 2756581343, 828, 3292048026025039))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert454

#print axioms Rank2Cert454.on_curve
#print axioms Rank2Cert454.indep
#print axioms Rank2Cert454.count

bsd/Rank2Cert455.lean

lean_all/bsd/Rank2Cert455.lean

/-
  bsd/Rank2Cert455.lean — T39 cycle 1064 (goal G706): rank ≥ 2 for 0 of the 4 congruent curves in 91601–91800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert455
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert455

#print axioms Rank2Cert455.on_curve
#print axioms Rank2Cert455.indep
#print axioms Rank2Cert455.count

bsd/Rank2Cert456.lean

lean_all/bsd/Rank2Cert456.lean

/-
  bsd/Rank2Cert456.lean — T39 cycle 1066 (goal G707): rank ≥ 2 for 3 of the 10 congruent curves in 91801–92000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert456
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (91905, (true, 310249, 1, 165052468), (true, 55330495, 13, 395024428700)),
   (91930, (true, 14570905, 12, 23240593475), (true, 332519946173430625, 1766922, 96829526162021225327327375)),
   (91977, (true, 404600889, 50, 6696360149787), (false, 240039975, 242, 83371978373355))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert456

#print axioms Rank2Cert456.on_curve
#print axioms Rank2Cert456.indep
#print axioms Rank2Cert456.count

bsd/Rank2Cert457.lean

lean_all/bsd/Rank2Cert457.lean

/-
  bsd/Rank2Cert457.lean — T39 cycle 1070 (goal G708): rank ≥ 2 for 2 of the 4 congruent curves in 92001–92200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert457
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (92001, (false, 1192464, 17, 29005196220), (true, 64747137, 17, 475037113824)),
   (92026, (true, 22517801, 10, 97522805451), (true, 37718289, 20, 50518926087))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert457

#print axioms Rank2Cert457.on_curve
#print axioms Rank2Cert457.indep
#print axioms Rank2Cert457.count

bsd/Rank2Cert458.lean

lean_all/bsd/Rank2Cert458.lean

/-
  bsd/Rank2Cert458.lean — T39 cycle 1072 (goal G709): rank ≥ 2 for 1 of the 3 congruent curves in 92201–92400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert458
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (92345, (true, 778855, 1, 682512600), (true, 4689520, 3, 9994539500))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert458

#print axioms Rank2Cert458.on_curve
#print axioms Rank2Cert458.indep
#print axioms Rank2Cert458.count

bsd/Rank2Cert459.lean

lean_all/bsd/Rank2Cert459.lean

/-
  bsd/Rank2Cert459.lean — T39 cycle 1074 (goal G710): rank ≥ 2 for 1 of the 8 congruent curves in 92401–92600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert459
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (92411, (true, 95139, 1, 6976860), (true, 36292444, 15, 179198436878))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert459

#print axioms Rank2Cert459.on_curve
#print axioms Rank2Cert459.indep
#print axioms Rank2Cert459.count

bsd/Rank2Cert46.lean

lean_all/bsd/Rank2Cert46.lean

/-
  bsd/Rank2Cert46.lean — T39 cycle 386 (goal G107): rank ≥ 2 for 2 of the 4 congruent curves in 9801–10000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert46
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (9809, (false, 48841, 5, 53108952), (true, 18513, 1, 2136288)),
   (9867, (true, 85683, 1, 24913980), (true, 29808, 1, 4856220))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert46

#print axioms Rank2Cert46.on_curve
#print axioms Rank2Cert46.indep
#print axioms Rank2Cert46.count

bsd/Rank2Cert460.lean

lean_all/bsd/Rank2Cert460.lean

/-
  bsd/Rank2Cert460.lean — T39 cycle 1076 (goal G711): rank ≥ 2 for 2 of the 9 congruent curves in 92601–92800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert460
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (92690, (true, 894010, 3, 303963400), (true, 24011546, 11, 104036606856)),
   (92786, (false, 850, 1, 2705040), (true, 10957933816729, 5020, 35438355457156383933))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert460

#print axioms Rank2Cert460.on_curve
#print axioms Rank2Cert460.indep
#print axioms Rank2Cert460.count

bsd/Rank2Cert461.lean

lean_all/bsd/Rank2Cert461.lean

/-
  bsd/Rank2Cert461.lean — T39 cycle 1078 (goal G712): rank ≥ 2 for 3 of the 7 congruent curves in 92801–93000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert461
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (92865, (true, 8413569, 5, 23457030372), (true, 78548470225, 243, 21960676348972300)),
   (92874, (true, 452929, 2, 174377665), (true, 99372, 1, 11141676)),
   (92939, (true, 665973, 1, 538163472), (true, 107100, 1, 17418030))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert461

#print axioms Rank2Cert461.on_curve
#print axioms Rank2Cert461.indep
#print axioms Rank2Cert461.count

bsd/Rank2Cert462.lean

lean_all/bsd/Rank2Cert462.lean

/-
  bsd/Rank2Cert462.lean — T39 cycle 1080 (goal G713): rank ≥ 2 for 5 of the 11 congruent curves in 93001–93200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert462
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (93010, (false, 36748890, 47, 1225425763800), (false, 3343319, 6, 335826379)),
   (93035, (true, 3592951893090169, 175744, 129290434913494005496653), (false, 1191625515, 977, 3065252702421000)),
   (93041, (false, 38025, 1, 16558620), (false, 41616, 1, 16975860)),
   (93171, (false, 492804, 5, 1598135994), (true, 8956666287361, 1822, 26789245679172401945)),
   (93193, (false, 389376, 5, 1433363568), (true, 631667689, 80, 5227973412363))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 5 := rfl

end Rank2Cert462

#print axioms Rank2Cert462.on_curve
#print axioms Rank2Cert462.indep
#print axioms Rank2Cert462.count

bsd/Rank2Cert463.lean

lean_all/bsd/Rank2Cert463.lean

/-
  bsd/Rank2Cert463.lean — T39 cycle 1082 (goal G714): rank ≥ 2 for 3 of the 7 congruent curves in 93201–93400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert463
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (93203, (true, 3191328572041, 5790, 1160197304105029661), (true, 304861942141872025, 323268, 168241472008323022260688445)),
   (93219, (true, 80791537, 4, 726063803081), (true, 53419054731, 715, 5578283872916496)),
   (93314, (false, 2221106, 5, 1063141728), (true, 39106024617361, 15630, 198700254223707903641))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert463

#print axioms Rank2Cert463.on_curve
#print axioms Rank2Cert463.indep
#print axioms Rank2Cert463.count

bsd/Rank2Cert464.lean

lean_all/bsd/Rank2Cert464.lean

/-
  bsd/Rank2Cert464.lean — T39 cycle 1084 (goal G715): rank ≥ 2 for 1 of the 5 congruent curves in 93401–93600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert464
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (93522, (true, 142572, 1, 40633020), (true, 56508070324225, 2838, 424743877812076731935))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert464

#print axioms Rank2Cert464.on_curve
#print axioms Rank2Cert464.indep
#print axioms Rank2Cert464.count

bsd/Rank2Cert465.lean

lean_all/bsd/Rank2Cert465.lean

/-
  bsd/Rank2Cert465.lean — T39 cycle 1086 (goal G718): rank ≥ 2 for 1 of the 2 congruent curves in 93601–93800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert465
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (93610, (true, 139150, 1, 38405400), (true, 5668041985225, 5018, 12272061314794914075))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert465

#print axioms Rank2Cert465.on_curve
#print axioms Rank2Cert465.indep
#print axioms Rank2Cert465.count

bsd/Rank2Cert466.lean

lean_all/bsd/Rank2Cert466.lean

/-
  bsd/Rank2Cert466.lean — T39 cycle 1088 (goal G719): rank ≥ 2 for 2 of the 7 congruent curves in 93801–94000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert466
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (93930, (false, 1245456, 5, 2221685928), (true, 144150, 1, 41515200)),
   (93995, (true, 945169248396025, 56278, 27578886697102429398075), (true, 7044124721929, 5846, 16638443120111852067))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert466

#print axioms Rank2Cert466.on_curve
#print axioms Rank2Cert466.indep
#print axioms Rank2Cert466.count

bsd/Rank2Cert467.lean

lean_all/bsd/Rank2Cert467.lean

/-
  bsd/Rank2Cert467.lean — T39 cycle 1092 (goal G721): rank ≥ 2 for 3 of the 10 congruent curves in 94001–94200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert467
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (94089, (true, 182521600, 43, 745918827920), (true, 21587874249, 5, 3171865891774932)),
   (94170, (true, 222066, 1, 94770936), (true, 3049356, 5, 3384320004)),
   (94177, (true, 186477186592225, 36576, 1877352186649717776815), (true, 407077438266702025, 1117732, 248640960824053215627658245))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert467

#print axioms Rank2Cert467.on_curve
#print axioms Rank2Cert467.indep
#print axioms Rank2Cert467.count

bsd/Rank2Cert468.lean

lean_all/bsd/Rank2Cert468.lean

/-
  bsd/Rank2Cert468.lean — T39 cycle 1094 (goal G722): rank ≥ 2 for 1 of the 7 congruent curves in 94201–94400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert468
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (94395, (true, 1854405, 1, 2521990800), (true, 250821, 1, 116380944))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert468

#print axioms Rank2Cert468.on_curve
#print axioms Rank2Cert468.indep
#print axioms Rank2Cert468.count

bsd/Rank2Cert469.lean

lean_all/bsd/Rank2Cert469.lean

/-
  bsd/Rank2Cert469.lean — T39 cycle 1096 (goal G723): rank ≥ 2 for 2 of the 5 congruent curves in 94401–94600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert469
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (94402, (false, 476847, 8, 4159046745), (true, 8266918, 3, 23643385480)),
   (94546, (false, 4851776, 9, 13051418480), (true, 13991341225, 124, 1646008224917355))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert469

#print axioms Rank2Cert469.on_curve
#print axioms Rank2Cert469.indep
#print axioms Rank2Cert469.count

bsd/Rank2Cert47.lean

lean_all/bsd/Rank2Cert47.lean

/-
  bsd/Rank2Cert47.lean — T39 cycle 387 (goal G108): rank ≥ 2 for 6 of the 9 congruent curves in 10001–10200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert47
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (10001, (true, 1370137, 3, 1600320016), (true, 4862691289, 308, 332573548550685)),
   (10010, (true, 59290, 1, 14229600), (true, 10206, 1, 201096)),
   (10059, (true, 1217307, 11, 22312836), (true, 3608668, 1, 6855177518)),
   (10074, (true, 95922, 1, 29543976), (true, 13248, 1, 990288)),
   (10145, (false, 900, 1, 303150), (true, 285783927471025, 111302, 4338980904473987858175)),
   (10146, (true, 526338, 7, 125385408), (true, 451497, 4, 283088619))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 6 := rfl

end Rank2Cert47

#print axioms Rank2Cert47.on_curve
#print axioms Rank2Cert47.indep
#print axioms Rank2Cert47.count

bsd/Rank2Cert470.lean

lean_all/bsd/Rank2Cert470.lean

/-
  bsd/Rank2Cert470.lean — T39 cycle 1098 (goal G724): rank ≥ 2 for 2 of the 8 congruent curves in 94601–94800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert470
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (94737, (true, 303130628329, 1785, 15304583136698108), (true, 529257807, 67, 7247879498340)),
   (94785, (true, 133225, 1, 34171300), (true, 5061519, 5, 10062299772))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert470

#print axioms Rank2Cert470.on_curve
#print axioms Rank2Cert470.indep
#print axioms Rank2Cert470.count

bsd/Rank2Cert471.lean

lean_all/bsd/Rank2Cert471.lean

/-
  bsd/Rank2Cert471.lean — T39 cycle 1100 (goal G725): rank ≥ 2 for 1 of the 2 congruent curves in 94801–95000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert471
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (94963, (true, 8383419, 5, 23279823072), (false, 172304, 45, 79822800244))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert471

#print axioms Rank2Cert471.on_curve
#print axioms Rank2Cert471.indep
#print axioms Rank2Cert471.count

bsd/Rank2Cert472.lean

lean_all/bsd/Rank2Cert472.lean

/-
  bsd/Rank2Cert472.lean — T39 cycle 1102 (goal G726): rank ≥ 2 for 2 of the 4 congruent curves in 95001–95200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert472
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (95034, (true, 260709, 1, 123958035), (true, 400330725, 37, 7575138044595)),
   (95090, (true, 109971585, 34, 33907905375), (true, 230521837410, 499, 110094357277494000))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert472

#print axioms Rank2Cert472.on_curve
#print axioms Rank2Cert472.indep
#print axioms Rank2Cert472.count

bsd/Rank2Cert473.lean

lean_all/bsd/Rank2Cert473.lean

/-
  bsd/Rank2Cert473.lean — T39 cycle 1104 (goal G727): rank ≥ 2 for 1 of the 6 congruent curves in 95201–95400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert473
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (95243, (true, 1315739761249, 2970, 1161507459195409193), (true, 138508668, 7, 1629178857510))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert473

#print axioms Rank2Cert473.on_curve
#print axioms Rank2Cert473.indep
#print axioms Rank2Cert473.count

bsd/Rank2Cert474.lean

lean_all/bsd/Rank2Cert474.lean

/-
  bsd/Rank2Cert474.lean — T39 cycle 1106 (goal G728): rank ≥ 2 for 0 of the 2 congruent curves in 95401–95600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert474
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert474

#print axioms Rank2Cert474.on_curve
#print axioms Rank2Cert474.indep
#print axioms Rank2Cert474.count

bsd/Rank2Cert475.lean

lean_all/bsd/Rank2Cert475.lean

/-
  bsd/Rank2Cert475.lean — T39 cycle 1108 (goal G729): rank ≥ 2 for 0 of the 5 congruent curves in 95601–95800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert475
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert475

#print axioms Rank2Cert475.on_curve
#print axioms Rank2Cert475.indep
#print axioms Rank2Cert475.count

bsd/Rank2Cert476.lean

lean_all/bsd/Rank2Cert476.lean

/-
  bsd/Rank2Cert476.lean — T39 cycle 1110 (goal G730): rank ≥ 2 for 4 of the 9 congruent curves in 95801–96000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert476
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (95810, (true, 1656490, 3, 1820348200), (true, 2740166, 5, 2203093464)),
   (95849, (true, 408888841, 65, 1143129204264), (true, 456659212368391201, 459765, 308290725896810210846843824)),
   (95865, (true, 103335, 1, 12400200), (true, 333564, 1, 184522338)),
   (95914, (true, 7204617, 2, 19310775099), (false, 9386, 1, 9247680))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert476

#print axioms Rank2Cert476.on_curve
#print axioms Rank2Cert476.indep
#print axioms Rank2Cert476.count

bsd/Rank2Cert477.lean

lean_all/bsd/Rank2Cert477.lean

/-
  bsd/Rank2Cert477.lean — T39 cycle 1114 (goal G732): rank ≥ 2 for 0 of the 6 congruent curves in 96001–96200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert477
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert477

#print axioms Rank2Cert477.on_curve
#print axioms Rank2Cert477.indep
#print axioms Rank2Cert477.count

bsd/Rank2Cert478.lean

lean_all/bsd/Rank2Cert478.lean

/-
  bsd/Rank2Cert478.lean — T39 cycle 1116 (goal G733): rank ≥ 2 for 3 of the 9 congruent curves in 96201–96400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert478
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (96209, (true, 186543, 1, 69026832), (true, 1082717248369, 3247, 394041508430824800)),
   (96265, (false, 20449, 1, 13451724), (true, 834343225, 93, 1558865102300)),
   (96385, (false, 342065, 7, 2754975600), (true, 1204896910140625, 43309, 41350460442680261632500))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert478

#print axioms Rank2Cert478.on_curve
#print axioms Rank2Cert478.indep
#print axioms Rank2Cert478.count

bsd/Rank2Cert479.lean

lean_all/bsd/Rank2Cert479.lean

/-
  bsd/Rank2Cert479.lean — T39 cycle 1118 (goal G734): rank ≥ 2 for 3 of the 9 congruent curves in 96401–96600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert479
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (96441, (true, 975207, 1, 958321224), (true, 25597308, 13, 99860916078)),
   (96459, (false, 68651, 69, 120327353740), (false, 3550272, 7, 5879023128)),
   (96585, (false, 42135, 1, 17839800), (true, 1307054860225, 418, 1494185518477106425))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert479

#print axioms Rank2Cert479.on_curve
#print axioms Rank2Cert479.indep
#print axioms Rank2Cert479.count

bsd/Rank2Cert48.lean

lean_all/bsd/Rank2Cert48.lean

/-
  bsd/Rank2Cert48.lean — T39 cycle 388 (goal G109): rank ≥ 2 for 5 of the 7 congruent curves in 10201–10400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert48
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (10329, (false, 1115136, 29, 9097244640), (true, 45177, 1, 9347976)),
   (10346, (false, 1251362, 11, 39733512), (false, 360703, 32, 6359096961)),
   (10355, (true, 59405, 1, 14257200), (true, 671457330625, 82, 550208920189266375)),
   (10370, (true, 93025, 2, 25395825), (false, 2608786, 29, 13441218912)),
   (10385, (true, 139159, 1, 51767148), (true, 290544467983225, 44511, 4940003056640375095300))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 5 := rfl

end Rank2Cert48

#print axioms Rank2Cert48.on_curve
#print axioms Rank2Cert48.indep
#print axioms Rank2Cert48.count

bsd/Rank2Cert480.lean

lean_all/bsd/Rank2Cert480.lean

/-
  bsd/Rank2Cert480.lean — T39 cycle 1120 (goal G735): rank ≥ 2 for 4 of the 10 congruent curves in 96601–96800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert480
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (96635, (false, 26411, 3, 141276212), (true, 439332579, 65, 3400359777108)),
   (96657, (true, 33989337393, 274, 6121862832641985), (true, 257754158673, 1094, 116940505556872305)),
   (96690, (false, 93750, 1, 7245000), (true, 204490, 1, 81481400)),
   (96730, (true, 4970970, 7, 3340724400), (true, 36880563805969, 16870, 149044145532066929703))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert480

#print axioms Rank2Cert480.on_curve
#print axioms Rank2Cert480.indep
#print axioms Rank2Cert480.count

bsd/Rank2Cert481.lean

lean_all/bsd/Rank2Cert481.lean

/-
  bsd/Rank2Cert481.lean — T39 cycle 1122 (goal G741): rank ≥ 2 for 4 of the 9 congruent curves in 96801–97000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert481
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (96866, (true, 160333173, 23, 1923706187403), (true, 39424462, 13, 225192526944)),
   (96882, (true, 396707449, 30, 7708208473043), (true, 3123183166009, 4520, 4269477741201736723)),
   (96889, (false, 84100, 1, 13952190), (true, 1686177, 4, 861345009)),
   (96985, (true, 107100, 1, 14869050), (true, 115222464, 19, 1178336696712))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert481

#print axioms Rank2Cert481.on_curve
#print axioms Rank2Cert481.indep
#print axioms Rank2Cert481.count

bsd/Rank2Cert482.lean

lean_all/bsd/Rank2Cert482.lean

/-
  bsd/Rank2Cert482.lean — T39 cycle 1124 (goal G742): rank ≥ 2 for 4 of the 8 congruent curves in 97001–97200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert482
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (97026, (true, 127308, 1, 29408148), (true, 1665613, 3, 1830508687)),
   (97130, (false, 2035220, 63, 549963815500), (true, 165753280, 39, 967612461200)),
   (97185, (true, 279775, 1, 138768400), (false, 883025, 123, 1381643134300)),
   (97194, (true, 2719201, 2, 4437895985), (true, 99372, 1, 6522516))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert482

#print axioms Rank2Cert482.on_curve
#print axioms Rank2Cert482.indep
#print axioms Rank2Cert482.count

bsd/Rank2Cert483.lean

lean_all/bsd/Rank2Cert483.lean

/-
  bsd/Rank2Cert483.lean — T39 cycle 1126 (goal G743): rank ≥ 2 for 4 of the 7 congruent curves in 97201–97400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert483
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (97251, (true, 3882417, 2, 7611359679), (false, 610368, 37, 104013291096)),
   (97305, (true, 7211100, 1, 19362558150), (true, 30823069225, 553, 1411063630589600)),
   (97331, (true, 17802319068, 427, 188346376833042), (true, 237567533281, 618, 114366237707668375)),
   (97370, (false, 343850, 3, 472659200), (false, 34418475, 29, 435919499925))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert483

#print axioms Rank2Cert483.on_curve
#print axioms Rank2Cert483.indep
#print axioms Rank2Cert483.count

bsd/Rank2Cert484.lean

lean_all/bsd/Rank2Cert484.lean

/-
  bsd/Rank2Cert484.lean — T39 cycle 1128 (goal G748): rank ≥ 2 for 5 of the 12 congruent curves in 97401–97600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert484
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (97435, (true, 47073065, 2, 322956350925), (true, 66285046570380625, 45208, 17065593193797694948481625)),
   (97545, (true, 100905, 1, 8202600), (true, 63608041966225, 11426, 497032545422065411025)),
   (97546, (true, 485809, 2, 201729225), (true, 98838, 1, 5007792)),
   (97554, (true, 453409053, 23, 9591882811497), (false, 302759975, 58, 2203216006915)),
   (97585, (false, 3364, 1, 5656566), (false, 69696, 1, 18031992))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 5 := rfl

end Rank2Cert484

#print axioms Rank2Cert484.on_curve
#print axioms Rank2Cert484.indep
#print axioms Rank2Cert484.count

bsd/Rank2Cert485.lean

lean_all/bsd/Rank2Cert485.lean

/-
  bsd/Rank2Cert485.lean — T39 cycle 1130 (goal G750): rank ≥ 2 for 2 of the 8 congruent curves in 97601–97800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert485
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (97665, (false, 75615, 1, 16997400), (true, 76567870681, 549, 19558729680929504)),
   (97674, (true, 32867593, 2, 188417468987), (true, 10701261114, 115, 1098917741271888))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert485

#print axioms Rank2Cert485.on_curve
#print axioms Rank2Cert485.indep
#print axioms Rank2Cert485.count

bsd/Rank2Cert486.lean

lean_all/bsd/Rank2Cert486.lean

/-
  bsd/Rank2Cert486.lean — T39 cycle 1132 (goal G751): rank ≥ 2 for 2 of the 6 congruent curves in 97801–98000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert486
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (97881, (true, 16716400, 13, 9849920660), (true, 5798697, 7, 7848039816)),
   (97905, (true, 31427505, 17, 76683112200), (true, 282847545, 49, 2645567370900))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert486

#print axioms Rank2Cert486.on_curve
#print axioms Rank2Cert486.indep
#print axioms Rank2Cert486.count

bsd/Rank2Cert487.lean

lean_all/bsd/Rank2Cert487.lean

/-
  bsd/Rank2Cert487.lean — T39 cycle 1136 (goal G752): rank ≥ 2 for 3 of the 12 congruent curves in 98001–98200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert487
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (98106, (true, 214533, 1, 88368093), (false, 452896302, 383, 306109871194248)),
   (98161, (true, 4021948, 3, 7868941262), (true, 8519936345449, 4146, 24376210917613249645)),
   (98169, (true, 74706609, 20, 549317695977), (true, 1853488922041, 2783, 2301307593527634400))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert487

#print axioms Rank2Cert487.on_curve
#print axioms Rank2Cert487.indep
#print axioms Rank2Cert487.count

bsd/Rank2Cert488.lean

lean_all/bsd/Rank2Cert488.lean

/-
  bsd/Rank2Cert488.lean — T39 cycle 1138 (goal G753): rank ≥ 2 for 5 of the 12 congruent curves in 98201–98400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert488
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (98209, (true, 60827610121, 734, 7400855518823325), (true, 80791535641, 74, 22963557662500875)),
   (98210, (false, 3486014, 9, 13350637216), (true, 4279150, 3, 8660999600)),
   (98258, (true, 802857650, 89, 5583238489920), (true, 2505250804703405055025, 2806986, 125394011075111896384086500580745)),
   (98274, (true, 3583074, 1, 6779852640), (true, 146329986, 31, 1352089070640)),
   (98345, (true, 1338649, 3, 1161947332), (false, 3136, 5, 137682888))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 5 := rfl

end Rank2Cert488

#print axioms Rank2Cert488.on_curve
#print axioms Rank2Cert488.indep
#print axioms Rank2Cert488.count

bsd/Rank2Cert489.lean

lean_all/bsd/Rank2Cert489.lean

/-
  bsd/Rank2Cert489.lean — T39 cycle 1140 (goal G754): rank ≥ 2 for 3 of the 6 congruent curves in 98401–98600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert489
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (98410, (true, 894010, 3, 115055200), (true, 452259811492801, 8334, 9616840024605232118401)),
   (98427, (true, 28954762725, 241, 4829986625466240), (true, 329982521547, 1781, 61375202721540540)),
   (98571, (true, 789525, 1, 696045240), (true, 23533852, 1, 114165719326))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert489

#print axioms Rank2Cert489.on_curve
#print axioms Rank2Cert489.indep
#print axioms Rank2Cert489.count

bsd/Rank2Cert49.lean

lean_all/bsd/Rank2Cert49.lean

/-
  bsd/Rank2Cert49.lean — T39 cycle 390 (goal G110): rank ≥ 2 for 5 of the 7 congruent curves in 10401–10600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert49
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (10434, (true, 16428, 1, 1626372), (false, 1406663, 14, 1760597299)),
   (10489, (true, 537289, 7, 114787800), (false, 9216, 5, 25158048)),
   (10491, (true, 940693, 9, 391328288), (true, 85047473641, 2074, 21022883246421485)),
   (10505, (true, 2006455, 13, 1324260300), (true, 77697261306025, 69778, 515537082987218606925)),
   (10545, (true, 34225, 1, 6023600), (true, 10534455, 31, 9340550100))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 5 := rfl

end Rank2Cert49

#print axioms Rank2Cert49.on_curve
#print axioms Rank2Cert49.indep
#print axioms Rank2Cert49.count

bsd/Rank2Cert490.lean

lean_all/bsd/Rank2Cert490.lean

/-
  bsd/Rank2Cert490.lean — T39 cycle 1142 (goal G755): rank ≥ 2 for 2 of the 10 congruent curves in 98601–98800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert490
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (98714, (false, 94178, 1, 9077544), (true, 153164, 1, 45832380)),
   (98769, (false, 16400, 1, 12473020), (false, 6204161, 9, 12581209160))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert490

#print axioms Rank2Cert490.on_curve
#print axioms Rank2Cert490.indep
#print axioms Rank2Cert490.count

bsd/Rank2Cert491.lean

lean_all/bsd/Rank2Cert491.lean

/-
  bsd/Rank2Cert491.lean — T39 cycle 1144 (goal G756): rank ≥ 2 for 2 of the 6 congruent curves in 98801–99000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert491
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (98810, (true, 19850929, 4, 88163647383), (false, 9640000, 71, 1546232382000)),
   (98826, (false, 28224, 1, 15911280), (true, 99372, 1, 3279276))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert491

#print axioms Rank2Cert491.on_curve
#print axioms Rank2Cert491.indep
#print axioms Rank2Cert491.count

bsd/Rank2Cert492.lean

lean_all/bsd/Rank2Cert492.lean

/-
  bsd/Rank2Cert492.lean — T39 cycle 1146 (goal G760): rank ≥ 2 for 2 of the 9 congruent curves in 99001–99200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert492
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (99065, (true, 2275794216931225, 47307, 108051019485769565319700), (true, 295252614180825625, 636608, 158941735158493047867679875)),
   (99138, (false, 42608942, 21, 63924720560), (true, 230338, 1, 99784160))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert492

#print axioms Rank2Cert492.on_curve
#print axioms Rank2Cert492.indep
#print axioms Rank2Cert492.count

bsd/Rank2Cert493.lean

lean_all/bsd/Rank2Cert493.lean

/-
  bsd/Rank2Cert493.lean — T39 cycle 1148 (goal G761): rank ≥ 2 for 1 of the 7 congruent curves in 99201–99400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert493
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (99202, (true, 132098, 1, 31703520), (true, 146926990978, 831, 49822413848346880))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert493

#print axioms Rank2Cert493.on_curve
#print axioms Rank2Cert493.indep
#print axioms Rank2Cert493.count

bsd/Rank2Cert494.lean

lean_all/bsd/Rank2Cert494.lean

/-
  bsd/Rank2Cert494.lean — T39 cycle 1150 (goal G763): rank ≥ 2 for 2 of the 3 congruent curves in 99401–99600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert494
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (99426, (true, 33655993, 2, 195237523187), (true, 3385028761, 158, 133914222404765)),
   (99435, (false, 33286701, 31, 516782299068), (false, 41371589, 359, 82428427140416))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert494

#print axioms Rank2Cert494.on_curve
#print axioms Rank2Cert494.indep
#print axioms Rank2Cert494.count

bsd/Rank2Cert495.lean

lean_all/bsd/Rank2Cert495.lean

/-
  bsd/Rank2Cert495.lean — T39 cycle 1152 (goal G765): rank ≥ 2 for 0 of the 8 congruent curves in 99601–99800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert495
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert495

#print axioms Rank2Cert495.on_curve
#print axioms Rank2Cert495.indep
#print axioms Rank2Cert495.count

bsd/Rank2Cert496.lean

lean_all/bsd/Rank2Cert496.lean

/-
  bsd/Rank2Cert496.lean — T39 cycle 1154 (goal G766): rank ≥ 2 for 2 of the 7 congruent curves in 99801–100000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert496
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (99834, (true, 30559578, 17, 55679622768), (false, 41748378, 23, 208990789056)),
   (99905, (false, 30276, 1, 16566018), (false, 73984, 1, 18261264))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert496

#print axioms Rank2Cert496.on_curve
#print axioms Rank2Cert496.indep
#print axioms Rank2Cert496.count

bsd/Rank2Cert497.lean

lean_all/bsd/Rank2Cert497.lean

/-
  bsd/Rank2Cert497.lean — T39 cycle 1158 (goal G767): rank ≥ 2 for 1 of the 3 congruent curves in 100001–100200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert497
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (100137, (true, 121755543, 31, 823063353840), (true, 54194473472614225, 214529, 12570614703740069691611140))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert497

#print axioms Rank2Cert497.on_curve
#print axioms Rank2Cert497.indep
#print axioms Rank2Cert497.count

bsd/Rank2Cert498.lean

lean_all/bsd/Rank2Cert498.lean

/-
  bsd/Rank2Cert498.lean — T39 cycle 1160 (goal G767): rank ≥ 2 for 0 of the 12 congruent curves in 100201–100400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert498
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert498

#print axioms Rank2Cert498.on_curve
#print axioms Rank2Cert498.indep
#print axioms Rank2Cert498.count

bsd/Rank2Cert499.lean

lean_all/bsd/Rank2Cert499.lean

/-
  bsd/Rank2Cert499.lean — T39 cycle 1162 (goal G767): rank ≥ 2 for 5 of the 10 congruent curves in 100401–100600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert499
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (100457, (true, 537993, 1, 387666840), (true, 86120577, 8, 796978858815)),
   (100490, (true, 106402932090, 517, 33583999851292800), (true, 88717655190025, 9336, 831549631830633079525)),
   (100529, (true, 1352572, 3, 1169298494), (false, 817210823, 324, 300775440552301)),
   (100594, (true, 69309266, 25, 242859668064), (true, 391585181026, 1389, 212827843894262480)),
   (100595, (true, 84602224, 25, 520693452132), (true, 211310299225, 898, 89693430491272725))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 5 := rfl

end Rank2Cert499

#print axioms Rank2Cert499.on_curve
#print axioms Rank2Cert499.indep
#print axioms Rank2Cert499.count

bsd/Rank2Cert5.lean

lean_all/bsd/Rank2Cert5.lean

/-
  bsd/Rank2Cert5.lean — T39 cycle 327 (goal G62): rank ≥ 2 for 12 of the 13 congruent curves in 1601–1800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert5
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (1610, (true, 259210, 9, 114052400), (true, 7406, 1, 622104)),
   (1633, (false, 947807, 36, 1842223985), (false, 2083823, 41, 2579509800)),
   (1635, (true, 11881, 2, 1081171), (true, 507840, 11, 333311400)),
   (1649, (true, 351649, 7, 202948320), (true, 2719201, 40, 1084961199)),
   (1651, (true, 3757, 1, 206856), (true, 11672943731761, 31842, 39469201001105229055)),
   (1659, (true, 917427, 13, 836693424), (true, 174748, 3, 72782542)),
   (1705, (true, 7225, 2, 202725), (true, 3100, 1, 144150)),
   (1731, (true, 332929, 4, 191434175), (false, 839523, 79, 9868524624)),
   (1745, (true, 121801, 6, 36418499), (true, 6662781836998225, 567112, 541922557927502671604775)),
   (1770, (true, 3645, 1, 192375), (true, 20886, 1, 3007584)),
   (1785, (true, 7225, 2, 93925), (true, 4335, 1, 260100)),
   (1794, (true, 2028, 1, 42588), (true, 2238912, 11, 3334299696))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 12 := rfl

end Rank2Cert5

#print axioms Rank2Cert5.on_curve
#print axioms Rank2Cert5.indep
#print axioms Rank2Cert5.count

bsd/Rank2Cert50.lean

lean_all/bsd/Rank2Cert50.lean

/-
  bsd/Rank2Cert50.lean — T39 cycle 391 (goal G111): rank ≥ 2 for 6 of the 9 congruent curves in 10601–10800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert50
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (10619, (true, 395641, 6, 64117115), (true, 13357, 1, 936396)),
   (10626, (true, 35574, 1, 6403320), (true, 11638, 1, 512072)),
   (10635, (false, 582135, 38, 11708581725), (true, 9434819689, 418, 898482630826963)),
   (10649, (true, 4930487, 7, 10886515296), (true, 383890528921, 5873, 69154958643573480)),
   (10730, (true, 16650, 1, 1642800), (false, 17158720, 157, 1093265350800)),
   (10785, (true, 57660, 1, 13601250), (true, 59940015, 71, 195403849200))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 6 := rfl

end Rank2Cert50

#print axioms Rank2Cert50.on_curve
#print axioms Rank2Cert50.indep
#print axioms Rank2Cert50.count

bsd/Rank2Cert500.lean

lean_all/bsd/Rank2Cert500.lean

/-
  bsd/Rank2Cert500.lean — T39 cycle 1164 (goal G767): rank ≥ 2 for 1 of the 3 congruent curves in 100601–100800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert500
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (100771, (true, 1219779, 1, 1342562760), (false, 3971, 1, 6345240))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert500

#print axioms Rank2Cert500.on_curve
#print axioms Rank2Cert500.indep
#print axioms Rank2Cert500.count

bsd/Rank2Cert501.lean

lean_all/bsd/Rank2Cert501.lean

/-
  bsd/Rank2Cert501.lean — T39 cycle 1166 (goal G767): rank ≥ 2 for 1 of the 7 congruent curves in 100801–101000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert501
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (100833, (false, 166868697, 79, 7838133809220), (true, 282224407857, 514, 149261720484506175))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert501

#print axioms Rank2Cert501.on_curve
#print axioms Rank2Cert501.indep
#print axioms Rank2Cert501.count

bsd/Rank2Cert502.lean

lean_all/bsd/Rank2Cert502.lean

/-
  bsd/Rank2Cert502.lean — T39 cycle 1168 (goal G767): rank ≥ 2 for 4 of the 11 congruent curves in 101001–101200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert502
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (101010, (false, 86490, 1, 15345000), (true, 112554, 1, 16657992)),
   (101099, (true, 112651, 1, 16678200), (true, 1494368674414225, 20708, 57743591722827822008505)),
   (101122, (true, 108578, 1, 13029360), (false, 1729118, 9, 10527947200)),
   (101145, (false, 131769, 17, 10610710704), (true, 1646095, 1, 2107953100))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert502

#print axioms Rank2Cert502.on_curve
#print axioms Rank2Cert502.indep
#print axioms Rank2Cert502.count

bsd/Rank2Cert503.lean

lean_all/bsd/Rank2Cert503.lean

/-
  bsd/Rank2Cert503.lean — T39 cycle 1170 (goal G767): rank ≥ 2 for 4 of the 10 congruent curves in 101201–101400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert503
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (101235, (true, 13396765, 7, 45549001000), (true, 163841664004225, 17582, 2058573160456089556825)),
   (101346, (false, 30926, 5, 445529168), (true, 8864982, 1, 26392978584)),
   (101361, (true, 90920817, 28, 421236145161), (true, 380872888201, 122, 235053478072472995)),
   (101371, (true, 10907217, 4, 35621758809), (true, 210798369, 40, 1954892162097))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert503

#print axioms Rank2Cert503.on_curve
#print axioms Rank2Cert503.indep
#print axioms Rank2Cert503.count

bsd/Rank2Cert504.lean

lean_all/bsd/Rank2Cert504.lean

/-
  bsd/Rank2Cert504.lean — T39 cycle 1172 (goal G767): rank ≥ 2 for 1 of the 6 congruent curves in 101401–101600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert504
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (101465, (true, 2948985, 1, 5061178200), (true, 229003385, 47, 710684005200))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert504

#print axioms Rank2Cert504.on_curve
#print axioms Rank2Cert504.indep
#print axioms Rank2Cert504.count

bsd/Rank2Cert505.lean

lean_all/bsd/Rank2Cert505.lean

/-
  bsd/Rank2Cert505.lean — T39 cycle 1174 (goal G767): rank ≥ 2 for 1 of the 4 congruent curves in 101601–101800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert505
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (101762, (false, 14400, 1, 12088560), (false, 72962, 1, 19161120))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert505

#print axioms Rank2Cert505.on_curve
#print axioms Rank2Cert505.indep
#print axioms Rank2Cert505.count

bsd/Rank2Cert506.lean

lean_all/bsd/Rank2Cert506.lean

/-
  bsd/Rank2Cert506.lean — T39 cycle 1176 (goal G767): rank ≥ 2 for 1 of the 3 congruent curves in 101801–102000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert506
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (101993, (true, 4349648107561, 1115, 9067687996549517316), (true, 84113877072405625, 329133, 24183664414754370137836300))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert506

#print axioms Rank2Cert506.on_curve
#print axioms Rank2Cert506.indep
#print axioms Rank2Cert506.count

bsd/Rank2Cert507.lean

lean_all/bsd/Rank2Cert507.lean

/-
  bsd/Rank2Cert507.lean — T39 cycle 1180 (goal G772): rank ≥ 2 for 1 of the 2 congruent curves in 102001–102200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert507
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (102074, (false, 1228927, 8, 7112698047), (true, 11477529, 10, 17779810083))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert507

#print axioms Rank2Cert507.on_curve
#print axioms Rank2Cert507.indep
#print axioms Rank2Cert507.count

bsd/Rank2Cert508.lean

lean_all/bsd/Rank2Cert508.lean

/-
  bsd/Rank2Cert508.lean — T39 cycle 1182 (goal G772): rank ≥ 2 for 2 of the 5 congruent curves in 102201–102400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert508
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (102201, (true, 10018833, 8, 24021361287), (true, 3648513, 4, 6229915263)),
   (102290, (true, 9870985, 6, 28773921275), (true, 3274959799225, 264, 5926631581593509075))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert508

#print axioms Rank2Cert508.on_curve
#print axioms Rank2Cert508.indep
#print axioms Rank2Cert508.count

bsd/Rank2Cert509.lean

lean_all/bsd/Rank2Cert509.lean

/-
  bsd/Rank2Cert509.lean — T39 cycle 1184 (goal G772): rank ≥ 2 for 2 of the 4 congruent curves in 102401–102600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert509
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (102466, (true, 9020557, 9, 10612192591), (false, 11466350, 69, 1651469528240)),
   (102490, (true, 1918225, 2, 2595358425), (true, 59354111290, 207, 14420628082084400))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert509

#print axioms Rank2Cert509.on_curve
#print axioms Rank2Cert509.indep
#print axioms Rank2Cert509.count

bsd/Rank2Cert51.lean

lean_all/bsd/Rank2Cert51.lean

/-
  bsd/Rank2Cert51.lean — T39 cycle 392 (goal G112): rank ≥ 2 for 3 of the 3 congruent curves in 10801–11000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert51
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (10810, (false, 68890, 7, 137846400), (true, 101614, 3, 9348488)),
   (10945, (false, 14137695, 43, 54445462500), (true, 428697025, 58, 8843374354425)),
   (10961, (false, 150544, 73, 22663458300), (true, 1238593, 9, 961148168))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert51

#print axioms Rank2Cert51.on_curve
#print axioms Rank2Cert51.indep
#print axioms Rank2Cert51.count

bsd/Rank2Cert510.lean

lean_all/bsd/Rank2Cert510.lean

/-
  bsd/Rank2Cert510.lean — T39 cycle 1186 (goal G772): rank ≥ 2 for 3 of the 6 congruent curves in 102601–102800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert510
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (102641, (true, 15495319, 5, 60153748872), (true, 48549193, 16, 284449721787)),
   (102666, (true, 732108, 1, 620225892), (true, 1308534, 1, 1492233120)),
   (102785, (true, 11126241361, 265, 893123371515984), (true, 697791583905025, 18368, 18409908278744363825025))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert510

#print axioms Rank2Cert510.on_curve
#print axioms Rank2Cert510.indep
#print axioms Rank2Cert510.count

bsd/Rank2Cert511.lean

lean_all/bsd/Rank2Cert511.lean

/-
  bsd/Rank2Cert511.lean — T39 cycle 1188 (goal G772): rank ≥ 2 for 2 of the 8 congruent curves in 102801–103000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert511
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (102810, (true, 3330150, 1, 6074193600), (true, 206494, 1, 81377128)),
   (102986, (false, 2944656, 25, 110336840328), (false, 179114, 7, 2134348320))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert511

#print axioms Rank2Cert511.on_curve
#print axioms Rank2Cert511.indep
#print axioms Rank2Cert511.count

bsd/Rank2Cert512.lean

lean_all/bsd/Rank2Cert512.lean

/-
  bsd/Rank2Cert512.lean — T39 cycle 1190 (goal G772): rank ≥ 2 for 1 of the 9 congruent curves in 103001–103200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert512
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (103170, (false, 7220, 7, 429553900), (true, 93368850, 11, 894100107600))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert512

#print axioms Rank2Cert512.on_curve
#print axioms Rank2Cert512.indep
#print axioms Rank2Cert512.count

bsd/Rank2Cert513.lean

lean_all/bsd/Rank2Cert513.lean

/-
  bsd/Rank2Cert513.lean — T39 cycle 1192 (goal G772): rank ≥ 2 for 6 of the 9 congruent curves in 103201–103400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert513
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (103306, (true, 3627134, 5, 4850326152), (false, 26057458, 103, 5592979594104)),
   (103361, (true, 1304726641, 87, 37715870740760), (true, 1717916891913121, 28720, 71116070754011585892081)),
   (103362, (false, 1983750, 7, 6563514600), (true, 190299286, 25, 2469267837784)),
   (103379, (true, 2367571, 1, 3639486480), (true, 12335908032839689, 336260, 437814710130006998175387)),
   (103394, (false, 98192, 1, 10147368), (false, 3503394, 7, 6850062960)),
   (103395, (true, 3129361, 2, 5487280559), (true, 38166541, 19, 49223933164))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 6 := rfl

end Rank2Cert513

#print axioms Rank2Cert513.on_curve
#print axioms Rank2Cert513.indep
#print axioms Rank2Cert513.count

bsd/Rank2Cert514.lean

lean_all/bsd/Rank2Cert514.lean

/-
  bsd/Rank2Cert514.lean — T39 cycle 1194 (goal G772): rank ≥ 2 for 2 of the 6 congruent curves in 103401–103600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert514
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (103411, (true, 114754556, 5, 1228979576454), (false, 83194243, 41, 1392182191224)),
   (103530, (true, 7291470, 1, 19686969000), (true, 1401106, 3, 1238577704))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert514

#print axioms Rank2Cert514.on_curve
#print axioms Rank2Cert514.indep
#print axioms Rank2Cert514.count

bsd/Rank2Cert515.lean

lean_all/bsd/Rank2Cert515.lean

/-
  bsd/Rank2Cert515.lean — T39 cycle 1196 (goal G772): rank ≥ 2 for 2 of the 6 congruent curves in 103601–103800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert515
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (103610, (true, 6352090, 1, 16007266800), (true, 6235013994001, 6956, 9256620551216898951)),
   (103785, (true, 243049, 1, 108349568), (true, 801975, 1, 712153800))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert515

#print axioms Rank2Cert515.on_curve
#print axioms Rank2Cert515.indep
#print axioms Rank2Cert515.count

bsd/Rank2Cert516.lean

lean_all/bsd/Rank2Cert516.lean

/-
  bsd/Rank2Cert516.lean — T39 cycle 1198 (goal G772): rank ≥ 2 for 0 of the 10 congruent curves in 103801–104000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert516
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert516

#print axioms Rank2Cert516.on_curve
#print axioms Rank2Cert516.indep
#print axioms Rank2Cert516.count

bsd/Rank2Cert517.lean

lean_all/bsd/Rank2Cert517.lean

/-
  bsd/Rank2Cert517.lean — T39 cycle 1202 (goal G779): rank ≥ 2 for 2 of the 6 congruent curves in 104001–104200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert517
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (104115, (true, 39417939, 19, 74578740588), (false, 219615, 16, 12490203825)),
   (104195, (true, 4772131, 1, 10422334104), (true, 98942309582431225, 21244, 31122395443542327427324275))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert517

#print axioms Rank2Cert517.on_curve
#print axioms Rank2Cert517.indep
#print axioms Rank2Cert517.count

bsd/Rank2Cert518.lean

lean_all/bsd/Rank2Cert518.lean

/-
  bsd/Rank2Cert518.lean — T39 cycle 1204 (goal G779): rank ≥ 2 for 0 of the 2 congruent curves in 104201–104400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert518
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert518

#print axioms Rank2Cert518.on_curve
#print axioms Rank2Cert518.indep
#print axioms Rank2Cert518.count

bsd/Rank2Cert519.lean

lean_all/bsd/Rank2Cert519.lean

/-
  bsd/Rank2Cert519.lean — T39 cycle 1206 (goal G779): rank ≥ 2 for 3 of the 10 congruent curves in 104401–104600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert519
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (104531, (true, 1246588, 3, 913161158), (true, 51145525, 3, 365710961960)),
   (104538, (true, 720762, 1, 605440080), (false, 37582608, 103, 6795048922200)),
   (104585, (false, 74529, 1, 20030556), (true, 3766335897025, 5648, 3391912813973658975))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert519

#print axioms Rank2Cert519.on_curve
#print axioms Rank2Cert519.indep
#print axioms Rank2Cert519.count

bsd/Rank2Cert52.lean

lean_all/bsd/Rank2Cert52.lean

/-
  bsd/Rank2Cert52.lean — T39 cycle 393 (goal G113): rank ≥ 2 for 3 of the 6 congruent curves in 11001–11200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert52
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (11010, (false, 126150, 7, 186301800), (false, 8704890, 91, 267770813400)),
   (11186, (true, 13778, 1, 944208), (false, 442223, 12, 1030010311)),
   (11193, (true, 284089, 5, 26136188), (true, 3393975, 7, 6170453640))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert52

#print axioms Rank2Cert52.on_curve
#print axioms Rank2Cert52.indep
#print axioms Rank2Cert52.count

bsd/Rank2Cert520.lean

lean_all/bsd/Rank2Cert520.lean

/-
  bsd/Rank2Cert520.lean — T39 cycle 1208 (goal G779): rank ≥ 2 for 2 of the 5 congruent curves in 104601–104800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert520
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (104601, (true, 607257, 2, 342965259), (true, 593648653081420225, 2323706, 140842079520484581050635655)),
   (104610, (false, 2944656, 17, 51631938072), (false, 54150, 1, 20827800))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert520

#print axioms Rank2Cert520.on_curve
#print axioms Rank2Cert520.indep
#print axioms Rank2Cert520.count

bsd/Rank2Cert521.lean

lean_all/bsd/Rank2Cert521.lean

/-
  bsd/Rank2Cert521.lean — T39 cycle 1210 (goal G779): rank ≥ 2 for 2 of the 10 congruent curves in 104801–105000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert521
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (104834, (false, 1150, 1, 3554880), (false, 6106182922, 353, 902408245050312)),
   (104995, (true, 18221405, 11, 55757499300), (true, 145475, 1, 38405400))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert521

#print axioms Rank2Cert521.on_curve
#print axioms Rank2Cert521.indep
#print axioms Rank2Cert521.count

bsd/Rank2Cert522.lean

lean_all/bsd/Rank2Cert522.lean

/-
  bsd/Rank2Cert522.lean — T39 cycle 1212 (goal G779): rank ≥ 2 for 1 of the 4 congruent curves in 105001–105200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert522
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (105090, (true, 230640, 1, 98598600), (true, 25333696, 3, 127422157456))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert522

#print axioms Rank2Cert522.on_curve
#print axioms Rank2Cert522.indep
#print axioms Rank2Cert522.count

bsd/Rank2Cert523.lean

lean_all/bsd/Rank2Cert523.lean

/-
  bsd/Rank2Cert523.lean — T39 cycle 1214 (goal G779): rank ≥ 2 for 3 of the 10 congruent curves in 105201–105400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert523
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (105266, (true, 188498, 1, 67888752), (true, 37948393, 18, 102498609493)),
   (105315, (true, 1221025, 2, 1266390775), (true, 107100, 1, 6372450)),
   (105369, (true, 1359897, 1, 1581071184), (false, 10435623, 1297, 572600865946824))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert523

#print axioms Rank2Cert523.on_curve
#print axioms Rank2Cert523.indep
#print axioms Rank2Cert523.count

bsd/Rank2Cert524.lean

lean_all/bsd/Rank2Cert524.lean

/-
  bsd/Rank2Cert524.lean — T39 cycle 1216 (goal G779): rank ≥ 2 for 1 of the 5 congruent curves in 105401–105600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert524
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (105481, (true, 5823063481, 151, 404680200844920), (true, 1026462801156450025, 1020864, 1033974724931939285398392715))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert524

#print axioms Rank2Cert524.on_curve
#print axioms Rank2Cert524.indep
#print axioms Rank2Cert524.count

bsd/Rank2Cert525.lean

lean_all/bsd/Rank2Cert525.lean

/-
  bsd/Rank2Cert525.lean — T39 cycle 1218 (goal G779): rank ≥ 2 for 0 of the 6 congruent curves in 105601–105800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert525
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert525

#print axioms Rank2Cert525.on_curve
#print axioms Rank2Cert525.indep
#print axioms Rank2Cert525.count

bsd/Rank2Cert526.lean

lean_all/bsd/Rank2Cert526.lean

/-
  bsd/Rank2Cert526.lean — T39 cycle 1220 (goal G779): rank ≥ 2 for 2 of the 5 congruent curves in 105801–106000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert526
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (105961, (true, 59102119, 23, 144047177400), (true, 84985600, 27, 326671443280)),
   (105995, (true, 2053345, 4, 1658809425), (true, 22167745, 8, 99364507425))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert526

#print axioms Rank2Cert526.on_curve
#print axioms Rank2Cert526.indep
#print axioms Rank2Cert526.count

bsd/Rank2Cert527.lean

lean_all/bsd/Rank2Cert527.lean

/-
  bsd/Rank2Cert527.lean — T39 cycle 1224 (goal G780): rank ≥ 2 for 1 of the 6 congruent curves in 106001–106200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert527
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (106194, (true, 7946994, 1, 22400904240), (true, 840009477828889, 54730, 22532876402691525292963))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert527

#print axioms Rank2Cert527.on_curve
#print axioms Rank2Cert527.indep
#print axioms Rank2Cert527.count

bsd/Rank2Cert528.lean

lean_all/bsd/Rank2Cert528.lean

/-
  bsd/Rank2Cert528.lean — T39 cycle 1226 (goal G780): rank ≥ 2 for 1 of the 5 congruent curves in 106201–106400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert528
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (106210, (true, 1201850, 1, 1312420200), (false, 239689826, 1097, 1978803639204432))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert528

#print axioms Rank2Cert528.on_curve
#print axioms Rank2Cert528.indep
#print axioms Rank2Cert528.count

bsd/Rank2Cert529.lean

lean_all/bsd/Rank2Cert529.lean

/-
  bsd/Rank2Cert529.lean — T39 cycle 1228 (goal G780): rank ≥ 2 for 3 of the 7 congruent curves in 106401–106600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert529
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (106426, (false, 499400703, 148, 50885501309439), (true, 36746822493, 83, 7042765505779527)),
   (106465, (true, 131209456, 33, 703670010596), (true, 1330573785025, 2517, 1323012659159060900)),
   (106491, (false, 2616789, 5, 792686916), (true, 855393, 2, 686062377))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert529

#print axioms Rank2Cert529.on_curve
#print axioms Rank2Cert529.indep
#print axioms Rank2Cert529.count

bsd/Rank2Cert53.lean

lean_all/bsd/Rank2Cert53.lean

/-
  bsd/Rank2Cert53.lean — T39 cycle 394 (goal G114): rank ≥ 2 for 8 of the 9 congruent curves in 11201–11400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert53
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (11201, (false, 8303, 71, 5145066696), (true, 64144081, 41, 491098424880)),
   (11211, (true, 377437, 3, 223442704), (true, 380899588189201, 184254, 290695465453362839855)),
   (11226, (false, 7078039775, 1888, 3313469794707505), (false, 7001282, 2313, 158915070830872)),
   (11234, (false, 10082, 1, 497568), (true, 85792665079489, 54636, 731428285635123394015)),
   (11265, (true, 33840060, 11, 196695348750), (true, 1125267025, 237, 31214180017900)),
   (11297, (true, 238975, 3, 105722540), (true, 3068384449, 85, 169906875559668)),
   (11330, (true, 53045, 1, 11935125), (true, 4201164, 1, 8610985812)),
   (11369, (true, 46824199321, 553, 10104272488227240), (true, 599145901186849, 228123, 2313510525595954854280))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 8 := rfl

end Rank2Cert53

#print axioms Rank2Cert53.on_curve
#print axioms Rank2Cert53.indep
#print axioms Rank2Cert53.count

bsd/Rank2Cert530.lean

lean_all/bsd/Rank2Cert530.lean

/-
  bsd/Rank2Cert530.lean — T39 cycle 1230 (goal G780): rank ≥ 2 for 2 of the 4 congruent curves in 106601–106800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert530
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (106658, (true, 4973858, 1, 11090220960), (false, 99458, 1, 12149040)),
   (106777, (false, 946832, 3, 159938380), (true, 4174067162494441, 14775, 269669839889869247617136))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert530

#print axioms Rank2Cert530.on_curve
#print axioms Rank2Cert530.indep
#print axioms Rank2Cert530.count

bsd/Rank2Cert531.lean

lean_all/bsd/Rank2Cert531.lean

/-
  bsd/Rank2Cert531.lean — T39 cycle 1232 (goal G780): rank ≥ 2 for 3 of the 10 congruent curves in 106801–107000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert531
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (106834, (false, 773150, 3, 502606720), (false, 48680818676, 1705, 67676099665954668)),
   (106945, (true, 6108805345, 166, 418225300025175), (true, 327729440373262225, 1685256, 70469836381883509832219975)),
   (106995, (true, 108045, 1, 4939200), (false, 7882980, 11, 28834013250))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert531

#print axioms Rank2Cert531.on_curve
#print axioms Rank2Cert531.indep
#print axioms Rank2Cert531.count

bsd/Rank2Cert532.lean

lean_all/bsd/Rank2Cert532.lean

/-
  bsd/Rank2Cert532.lean — T39 cycle 1234 (goal G780): rank ≥ 2 for 2 of the 4 congruent curves in 107001–107200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert532
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (107066, (true, 5778113, 4, 13264813023), (true, 134329257, 26, 1311570460299)),
   (107139, (true, 111747, 1, 10616544), (false, 2925839, 20, 73133824841))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert532

#print axioms Rank2Cert532.on_curve
#print axioms Rank2Cert532.indep
#print axioms Rank2Cert532.count

bsd/Rank2Cert533.lean

lean_all/bsd/Rank2Cert533.lean

/-
  bsd/Rank2Cert533.lean — T39 cycle 1236 (goal G780): rank ≥ 2 for 2 of the 6 congruent curves in 107201–107400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert533
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (107322, (true, 33596250, 1, 194730505200), (true, 4475645457070225, 106304, 288219251373763262230585)),
   (107355, (false, 2705295, 8, 10387938825), (true, 2605160916763729, 63630, 131105637052572051952433))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert533

#print axioms Rank2Cert533.on_curve
#print axioms Rank2Cert533.indep
#print axioms Rank2Cert533.count

bsd/Rank2Cert534.lean

lean_all/bsd/Rank2Cert534.lean

/-
  bsd/Rank2Cert534.lean — T39 cycle 1238 (goal G780): rank ≥ 2 for 0 of the 1 congruent curves in 107401–107600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert534
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert534

#print axioms Rank2Cert534.on_curve
#print axioms Rank2Cert534.indep
#print axioms Rank2Cert534.count

bsd/Rank2Cert535.lean

lean_all/bsd/Rank2Cert535.lean

/-
  bsd/Rank2Cert535.lean — T39 cycle 1240 (goal G780): rank ≥ 2 for 3 of the 7 congruent curves in 107601–107800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert535
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (107666, (true, 974242, 3, 99676576), (false, 248548642, 157, 41655322495008)),
   (107715, (false, 12396900, 19, 129767350050), (false, 3171955415, 5068, 155816214780589525)),
   (107729, (true, 440657, 1, 283640784), (true, 217824332230129, 23343, 3095904480839057486800))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert535

#print axioms Rank2Cert535.on_curve
#print axioms Rank2Cert535.indep
#print axioms Rank2Cert535.count

bsd/Rank2Cert536.lean

lean_all/bsd/Rank2Cert536.lean

/-
  bsd/Rank2Cert536.lean — T39 cycle 1242 (goal G780): rank ≥ 2 for 4 of the 6 congruent curves in 107801–108000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert536
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (107835, (true, 1965915, 1, 2752281000), (true, 24339900, 1, 120080896650)),
   (107931, (true, 201243, 1, 76195728), (false, 51483, 1, 21523824)),
   (107954, (false, 26918647, 22, 232342913733), (true, 108403834, 27, 776210213240)),
   (107970, (true, 9555345, 8, 20400661575), (true, 63617445, 11, 496601574525))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert536

#print axioms Rank2Cert536.on_curve
#print axioms Rank2Cert536.indep
#print axioms Rank2Cert536.count

bsd/Rank2Cert537.lean

lean_all/bsd/Rank2Cert537.lean

/-
  bsd/Rank2Cert537.lean — T39 cycle 1246 (goal G781): rank ≥ 2 for 0 of the 11 congruent curves in 108001–108200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert537
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert537

#print axioms Rank2Cert537.on_curve
#print axioms Rank2Cert537.indep
#print axioms Rank2Cert537.count

bsd/Rank2Cert538.lean

lean_all/bsd/Rank2Cert538.lean

/-
  bsd/Rank2Cert538.lean — T39 cycle 1248 (goal G781): rank ≥ 2 for 1 of the 3 congruent curves in 108201–108400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert538
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (108353, (true, 314847, 1, 165873240), (false, 1453025, 17, 37705723440))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert538

#print axioms Rank2Cert538.on_curve
#print axioms Rank2Cert538.indep
#print axioms Rank2Cert538.count

bsd/Rank2Cert539.lean

lean_all/bsd/Rank2Cert539.lean

/-
  bsd/Rank2Cert539.lean — T39 cycle 1250 (goal G781): rank ≥ 2 for 1 of the 4 congruent curves in 108401–108600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert539
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (108570, (true, 889350, 1, 832431600), (true, 154630, 1, 43296400))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert539

#print axioms Rank2Cert539.on_curve
#print axioms Rank2Cert539.indep
#print axioms Rank2Cert539.count

bsd/Rank2Cert54.lean

lean_all/bsd/Rank2Cert54.lean

/-
  bsd/Rank2Cert54.lean — T39 cycle 395 (goal G115): rank ≥ 2 for 3 of the 7 congruent curves in 11401–11600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert54
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (11401, (false, 76176, 5, 75805332), (true, 47694772582386241, 1074505, 10011575939846499043306164)),
   (11571, (true, 409948, 3, 253867922), (true, 47937, 2, 2732409)),
   (11577, (true, 485809, 5, 271958248), (true, 7883937, 13, 21444308640))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert54

#print axioms Rank2Cert54.on_curve
#print axioms Rank2Cert54.indep
#print axioms Rank2Cert54.count

bsd/Rank2Cert540.lean

lean_all/bsd/Rank2Cert540.lean

/-
  bsd/Rank2Cert540.lean — T39 cycle 1252 (goal G781): rank ≥ 2 for 1 of the 5 congruent curves in 108601–108800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert540
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (108721, (true, 5499783, 7, 3204540228), (true, 171039317761, 1197, 29209078597818860))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert540

#print axioms Rank2Cert540.on_curve
#print axioms Rank2Cert540.indep
#print axioms Rank2Cert540.count

bsd/Rank2Cert541.lean

lean_all/bsd/Rank2Cert541.lean

/-
  bsd/Rank2Cert541.lean — T39 cycle 1254 (goal G781): rank ≥ 2 for 2 of the 5 congruent curves in 108801–109000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert541
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (108834, (true, 3661218, 1, 7002391968), (true, 89733633, 4, 849867238143)),
   (108915, (true, 20990207715, 197, 2978762931015300), (true, 7001448300625, 7788, 6137887694759806375))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert541

#print axioms Rank2Cert541.on_curve
#print axioms Rank2Cert541.indep
#print axioms Rank2Cert541.count

bsd/Rank2Cert542.lean

lean_all/bsd/Rank2Cert542.lean

/-
  bsd/Rank2Cert542.lean — T39 cycle 1256 (goal G781): rank ≥ 2 for 3 of the 8 congruent curves in 109001–109200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert542
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (109041, (false, 37582608, 229, 35054588000124), (true, 330902184974089, 46475, 4228172782186887753688)),
   (109065, (true, 11458140, 1, 38783902950), (false, 219615, 4, 811277775)),
   (109130, (true, 7197246, 5, 17867519544), (true, 243670, 1, 107545200))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert542

#print axioms Rank2Cert542.on_curve
#print axioms Rank2Cert542.indep
#print axioms Rank2Cert542.count

bsd/Rank2Cert543.lean

lean_all/bsd/Rank2Cert543.lean

/-
  bsd/Rank2Cert543.lean — T39 cycle 1258 (goal G781): rank ≥ 2 for 1 of the 5 congruent curves in 109201–109400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert543
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (109306, (true, 452025, 2, 77132685), (true, 59533025, 16, 405439019985))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert543

#print axioms Rank2Cert543.on_curve
#print axioms Rank2Cert543.indep
#print axioms Rank2Cert543.count

bsd/Rank2Cert544.lean

lean_all/bsd/Rank2Cert544.lean

/-
  bsd/Rank2Cert544.lean — T39 cycle 1260 (goal G781): rank ≥ 2 for 1 of the 9 congruent curves in 109401–109600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert544
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (109473, (true, 100339824, 5, 1004727742668), (true, 719996415102300625, 143849, 610932672360850011260260600))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert544

#print axioms Rank2Cert544.on_curve
#print axioms Rank2Cert544.indep
#print axioms Rank2Cert544.count

bsd/Rank2Cert545.lean

lean_all/bsd/Rank2Cert545.lean

/-
  bsd/Rank2Cert545.lean — T39 cycle 1262 (goal G781): rank ≥ 2 for 1 of the 5 congruent curves in 109601–109800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert545
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (109649, (true, 8200591, 3, 23313108700), (true, 33943272169, 441, 4865409825791500))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert545

#print axioms Rank2Cert545.on_curve
#print axioms Rank2Cert545.indep
#print axioms Rank2Cert545.count

bsd/Rank2Cert546.lean

lean_all/bsd/Rank2Cert546.lean

/-
  bsd/Rank2Cert546.lean — T39 cycle 1264 (goal G781): rank ≥ 2 for 1 of the 7 congruent curves in 109801–110000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert546
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (109889, (false, 2311175711, 401, 842194724127120), (true, 222792909210001, 14178, 3309076967773918439215))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert546

#print axioms Rank2Cert546.on_curve
#print axioms Rank2Cert546.indep
#print axioms Rank2Cert546.count

bsd/Rank2Cert547.lean

lean_all/bsd/Rank2Cert547.lean

/-
  bsd/Rank2Cert547.lean — T39 cycle 1268 (goal G782): rank ≥ 2 for 2 of the 4 congruent curves in 110001–110200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert547
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (110010, (false, 19548150, 29, 399819340800), (true, 7077310, 1, 18825644600)),
   (110057, (false, 2598544, 5, 1457865396), (true, 48377645433, 38, 10640566430665275))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert547

#print axioms Rank2Cert547.on_curve
#print axioms Rank2Cert547.indep
#print axioms Rank2Cert547.count

bsd/Rank2Cert548.lean

lean_all/bsd/Rank2Cert548.lean

/-
  bsd/Rank2Cert548.lean — T39 cycle 1270 (goal G782): rank ≥ 2 for 2 of the 7 congruent curves in 110201–110400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert548
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (110290, (true, 994077841, 62, 28348991649561), (false, 59245229810, 1847, 90436666479837600)),
   (110346, (true, 722454, 1, 606861360), (true, 20438973, 11, 69962209623))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert548

#print axioms Rank2Cert548.on_curve
#print axioms Rank2Cert548.indep
#print axioms Rank2Cert548.count

bsd/Rank2Cert549.lean

lean_all/bsd/Rank2Cert549.lean

/-
  bsd/Rank2Cert549.lean — T39 cycle 1272 (goal G782): rank ≥ 2 for 3 of the 7 congruent curves in 110401–110600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert549
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (110409, (true, 3751969, 5, 4922583328), (false, 2571139679, 263, 364632230459420)),
   (110505, (true, 194775, 1, 70786800), (true, 415070943734440009, 23765, 267413601942236150486995652)),
   (110523, (true, 8593648, 1, 25190130700), (true, 207137896203, 683, 91306442142557460))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert549

#print axioms Rank2Cert549.on_curve
#print axioms Rank2Cert549.indep
#print axioms Rank2Cert549.count

bsd/Rank2Cert55.lean

lean_all/bsd/Rank2Cert55.lean

/-
  bsd/Rank2Cert55.lean — T39 cycle 396 (goal G116): rank ≥ 2 for 6 of the 7 congruent curves in 11601–11800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert55
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (11635, (true, 47385, 2, 1939275), (true, 7952541000625, 18592, 19347133262974427625)),
   (11641, (true, 582750551161, 1039, 444757250537224680), (false, 195370623, 148, 2290238544321)),
   (11659, (true, 22161089, 20, 101988395937), (true, 319696383889, 3740, 155474485700473287)),
   (11713, (true, 70225, 2, 13862415), (true, 429777, 2, 280071345)),
   (11715, (true, 499059, 5, 285461748), (true, 15360, 1, 1231200)),
   (11778, (false, 268203, 7, 264646395), (false, 8031348, 29, 16430237460))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 6 := rfl

end Rank2Cert55

#print axioms Rank2Cert55.on_curve
#print axioms Rank2Cert55.indep
#print axioms Rank2Cert55.count

bsd/Rank2Cert550.lean

lean_all/bsd/Rank2Cert550.lean

/-
  bsd/Rank2Cert550.lean — T39 cycle 1274 (goal G782): rank ≥ 2 for 2 of the 4 congruent curves in 110601–110800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert550
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (110635, (true, 1413721, 2, 1596435819), (false, 19600, 1, 15243900)),
   (110770, (false, 4864475, 37, 334287416925), (true, 169623850, 9, 2206083543400))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert550

#print axioms Rank2Cert550.on_curve
#print axioms Rank2Cert550.indep
#print axioms Rank2Cert550.count

bsd/Rank2Cert551.lean

lean_all/bsd/Rank2Cert551.lean

/-
  bsd/Rank2Cert551.lean — T39 cycle 1276 (goal G782): rank ≥ 2 for 3 of the 7 congruent curves in 110801–111000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert551
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (110810, (false, 5140890, 7, 3961568400), (false, 36825716835, 1361, 38749491855644025)),
   (110890, (false, 43264, 1, 21237216), (true, 263482023025, 1448, 63626832813609975)),
   (110905, (false, 107584, 1, 8835336), (true, 8713361, 8, 14918389191))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert551

#print axioms Rank2Cert551.on_curve
#print axioms Rank2Cert551.indep
#print axioms Rank2Cert551.count

bsd/Rank2Cert552.lean

lean_all/bsd/Rank2Cert552.lean

/-
  bsd/Rank2Cert552.lean — T39 cycle 1278 (goal G782): rank ≥ 2 for 3 of the 8 congruent curves in 111001–111200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert552
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (111106, (true, 206286565937, 704, 90293053576966569), (true, 27645755797676641, 217042, 4513534718436718751266575)),
   (111155, (false, 996611, 9, 8933051512), (false, 1152020525, 1299, 6366037925242900)),
   (111162, (true, 4051884, 5, 5935460652), (true, 8409900, 1, 24386434380))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert552

#print axioms Rank2Cert552.on_curve
#print axioms Rank2Cert552.indep
#print axioms Rank2Cert552.count

bsd/Rank2Cert553.lean

lean_all/bsd/Rank2Cert553.lean

/-
  bsd/Rank2Cert553.lean — T39 cycle 1280 (goal G782): rank ≥ 2 for 1 of the 7 congruent curves in 111201–111400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert553
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (111274, (true, 1105150, 3, 491314040), (true, 285442, 1, 140437464))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert553

#print axioms Rank2Cert553.on_curve
#print axioms Rank2Cert553.indep
#print axioms Rank2Cert553.count

bsd/Rank2Cert554.lean

lean_all/bsd/Rank2Cert554.lean

/-
  bsd/Rank2Cert554.lean — T39 cycle 1282 (goal G782): rank ≥ 2 for 1 of the 10 congruent curves in 111401–111600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert554
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (111435, (true, 273885, 1, 130934700), (true, 774972285, 13, 21567558585600))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert554

#print axioms Rank2Cert554.on_curve
#print axioms Rank2Cert554.indep
#print axioms Rank2Cert554.count

bsd/Rank2Cert555.lean

lean_all/bsd/Rank2Cert555.lean

/-
  bsd/Rank2Cert555.lean — T39 cycle 1284 (goal G782): rank ≥ 2 for 1 of the 3 congruent curves in 111601–111800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert555
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (111755, (false, 581405, 3, 625803200), (false, 6645439, 20, 113955686559))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert555

#print axioms Rank2Cert555.on_curve
#print axioms Rank2Cert555.indep
#print axioms Rank2Cert555.count

bsd/Rank2Cert556.lean

lean_all/bsd/Rank2Cert556.lean

/-
  bsd/Rank2Cert556.lean — T39 cycle 1286 (goal G782): rank ≥ 2 for 1 of the 5 congruent curves in 111801–112000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert556
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (111954, (true, 478927509, 11, 10476853693605), (true, 103702579702, 229, 33341661877122296))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert556

#print axioms Rank2Cert556.on_curve
#print axioms Rank2Cert556.indep
#print axioms Rank2Cert556.count

bsd/Rank2Cert557.lean

lean_all/bsd/Rank2Cert557.lean

/-
  bsd/Rank2Cert557.lean — T39 cycle 1290 (goal G784): rank ≥ 2 for 3 of the 8 congruent curves in 112001–112200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert557
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (112034, (false, 28922318, 21, 215423587504), (false, 51096122, 2531, 5130126820509768)),
   (112066, (false, 68774274, 25, 109980634224), (true, 2643887513, 98, 124171307305443)),
   (112091, (false, 5887, 4, 137605377), (false, 1005377639, 352, 439217664958275))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert557

#print axioms Rank2Cert557.on_curve
#print axioms Rank2Cert557.indep
#print axioms Rank2Cert557.count

bsd/Rank2Cert558.lean

lean_all/bsd/Rank2Cert558.lean

/-
  bsd/Rank2Cert558.lean — T39 cycle 1292 (goal G784): rank ≥ 2 for 2 of the 7 congruent curves in 112201–112400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert558
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (112290, (true, 549100, 1, 398291300), (true, 2981510157025, 4212, 3830335531632124975)),
   (112299, (true, 122539857, 32, 468652379481), (true, 47814723, 19, 175309563528))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert558

#print axioms Rank2Cert558.on_curve
#print axioms Rank2Cert558.indep
#print axioms Rank2Cert558.count

bsd/Rank2Cert559.lean

lean_all/bsd/Rank2Cert559.lean

/-
  bsd/Rank2Cert559.lean — T39 cycle 1294 (goal G784): rank ≥ 2 for 2 of the 7 congruent curves in 112401–112600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert559
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (112489, (false, 98192, 1, 17197812), (false, 46338617, 21, 120557756852)),
   (112497, (false, 86247, 1, 21212100), (true, 467313, 2, 86178015))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert559

#print axioms Rank2Cert559.on_curve
#print axioms Rank2Cert559.indep
#print axioms Rank2Cert559.count

bsd/Rank2Cert56.lean

lean_all/bsd/Rank2Cert56.lean

/-
  bsd/Rank2Cert56.lean — T39 cycle 397 (goal G117): rank ≥ 2 for 2 of the 4 congruent curves in 11801–12000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert56
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (11914, (false, 567, 2, 1134693), (true, 19573, 1, 2172603)),
   (11978, (true, 319225, 2, 178319085), (false, 262946250, 191, 5659263086400))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert56

#print axioms Rank2Cert56.on_curve
#print axioms Rank2Cert56.indep
#print axioms Rank2Cert56.count

bsd/Rank2Cert560.lean

lean_all/bsd/Rank2Cert560.lean

/-
  bsd/Rank2Cert560.lean — T39 cycle 1296 (goal G784): rank ≥ 2 for 1 of the 4 congruent curves in 112601–112800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert560
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (112665, (true, 543900, 1, 392423850), (false, 2736039, 5, 1106475084))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert560

#print axioms Rank2Cert560.on_curve
#print axioms Rank2Cert560.indep
#print axioms Rank2Cert560.count

bsd/Rank2Cert561.lean

lean_all/bsd/Rank2Cert561.lean

/-
  bsd/Rank2Cert561.lean — T39 cycle 1298 (goal G784): rank ≥ 2 for 4 of the 8 congruent curves in 112801–113000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert561
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (112931, (true, 1221025, 2, 1253504265), (true, 992069, 1, 981704100)),
   (112938, (true, 122955882, 17, 1314488206080), (false, 4062282, 17, 65272903920)),
   (112955, (true, 411845, 1, 254167200), (true, 73702225, 22, 424344402225)),
   (112970, (true, 1017280, 3, 33734800), (false, 146071, 2, 163433067))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert561

#print axioms Rank2Cert561.on_curve
#print axioms Rank2Cert561.indep
#print axioms Rank2Cert561.count

bsd/Rank2Cert562.lean

lean_all/bsd/Rank2Cert562.lean

/-
  bsd/Rank2Cert562.lean — T39 cycle 1300 (goal G784): rank ≥ 2 for 5 of the 12 congruent curves in 113001–113200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert562
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (113033, (false, 26896, 5, 463414308), (true, 1444259557319985025, 3511547, 454758837125397666602636520)),
   (113035, (true, 7207785, 2, 19312859475), (true, 1627219140625, 1588, 2043625815262565625)),
   (113105, (true, 15367321225, 358, 632353450205475), (true, 283771345160321041, 477774, 150538581352308797557254289)),
   (113106, (false, 14253134, 15, 79595263664), (false, 665364, 5, 2241745884)),
   (113113, (true, 1880433, 4, 700060425), (true, 14080817, 4, 52399187625))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 5 := rfl

end Rank2Cert562

#print axioms Rank2Cert562.on_curve
#print axioms Rank2Cert562.indep
#print axioms Rank2Cert562.count

bsd/Rank2Cert563.lean

lean_all/bsd/Rank2Cert563.lean

/-
  bsd/Rank2Cert563.lean — T39 cycle 1302 (goal G784): rank ≥ 2 for 0 of the 3 congruent curves in 113201–113400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert563
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert563

#print axioms Rank2Cert563.on_curve
#print axioms Rank2Cert563.indep
#print axioms Rank2Cert563.count

bsd/Rank2Cert564.lean

lean_all/bsd/Rank2Cert564.lean

/-
  bsd/Rank2Cert564.lean — T39 cycle 1304 (goal G784): rank ≥ 2 for 5 of the 10 congruent curves in 113401–113600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert564
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (113451, (false, 85293, 1, 21847644), (false, 8466237, 19, 116594372232)),
   (113505, (true, 231945, 1, 97416900), (true, 3200841, 2, 5668689411)),
   (113529, (false, 2223081, 5, 2630857572), (true, 786396, 1, 690062490)),
   (113570, (true, 2302929, 4, 2146961433), (true, 121638841, 32, 393245711661)),
   (113594, (true, 1122833, 2, 1088025177), (true, 15215850, 11, 25459836480))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 5 := rfl

end Rank2Cert564

#print axioms Rank2Cert564.on_curve
#print axioms Rank2Cert564.indep
#print axioms Rank2Cert564.count

bsd/Rank2Cert565.lean

lean_all/bsd/Rank2Cert565.lean

/-
  bsd/Rank2Cert565.lean — T39 cycle 1306 (goal G784): rank ≥ 2 for 0 of the 7 congruent curves in 113601–113800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert565
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert565

#print axioms Rank2Cert565.on_curve
#print axioms Rank2Cert565.indep
#print axioms Rank2Cert565.count

bsd/Rank2Cert566.lean

lean_all/bsd/Rank2Cert566.lean

/-
  bsd/Rank2Cert566.lean — T39 cycle 1308 (goal G784): rank ≥ 2 for 2 of the 10 congruent curves in 113801–114000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert566
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (113849, (true, 1221025, 3, 733817240), (true, 63315835615157161, 633895, 11014497394016658210968784)),
   (113945, (true, 230082415745, 332, 110198869225269825), (true, 3998787775368961, 117107, 232760228127918804387216))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert566

#print axioms Rank2Cert566.on_curve
#print axioms Rank2Cert566.indep
#print axioms Rank2Cert566.count

bsd/Rank2Cert567.lean

lean_all/bsd/Rank2Cert567.lean

/-
  bsd/Rank2Cert567.lean — T39 cycle 1312 (goal G786): rank ≥ 2 for 4 of the 9 congruent curves in 114001–114200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert567
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (114019, (true, 19065177, 4, 82863614301), (true, 1006445994068089, 46384, 30966085644940184004915)),
   (114034, (false, 80389012, 251, 64409949231012), (false, 10350, 1, 11553360)),
   (114081, (true, 115521153, 31, 391382949672), (true, 2571578441397289, 95288, 119359597264400176735445)),
   (114114, (true, 117117, 1, 9018009), (true, 198198, 1, 72144072))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert567

#print axioms Rank2Cert567.on_curve
#print axioms Rank2Cert567.indep
#print axioms Rank2Cert567.count

bsd/Rank2Cert568.lean

lean_all/bsd/Rank2Cert568.lean

/-
  bsd/Rank2Cert568.lean — T39 cycle 1314 (goal G786): rank ≥ 2 for 3 of the 9 congruent curves in 114201–114400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert568
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (114226, (true, 118098, 1, 10307088), (false, 3495824, 7, 8172685800)),
   (114289, (false, 11005067407, 598, 4129132242020703), (true, 30165089761, 99, 5235489545716900)),
   (114330, (true, 21726030, 13, 46304782200), (true, 63590346, 13, 483117055344))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert568

#print axioms Rank2Cert568.on_curve
#print axioms Rank2Cert568.indep
#print axioms Rank2Cert568.count

bsd/Rank2Cert569.lean

lean_all/bsd/Rank2Cert569.lean

/-
  bsd/Rank2Cert569.lean — T39 cycle 1322 (goal G786): rank ≥ 2 for 2 of the 7 congruent curves in 114401–114600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert569
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (114483, (false, 9463773, 59, 1225616750820), (false, 64338483, 49, 2143549302840)),
   (114530, (true, 8991970, 7, 21067878000), (false, 25975170, 23, 278965190400))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert569

#print axioms Rank2Cert569.on_curve
#print axioms Rank2Cert569.indep
#print axioms Rank2Cert569.count

bsd/Rank2Cert57.lean

lean_all/bsd/Rank2Cert57.lean

/-
  bsd/Rank2Cert57.lean — T39 cycle 398 (goal G118): rank ≥ 2 for 4 of the 5 congruent curves in 12001–12200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert57
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (12035, (true, 2036525, 13, 147090900), (true, 110314986769, 2410, 28345356010870353)),
   (12121, (true, 139129, 1, 51697800), (true, 8642273, 13, 24682331688)),
   (12155, (true, 48841, 2, 1025661), (true, 63580, 1, 15736050)),
   (12185, (true, 50760851362225, 5031, 361647385661337262300), (true, 1057665198715225, 290524, 8026697617979489816925))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert57

#print axioms Rank2Cert57.on_curve
#print axioms Rank2Cert57.indep
#print axioms Rank2Cert57.count

bsd/Rank2Cert570.lean

lean_all/bsd/Rank2Cert570.lean

/-
  bsd/Rank2Cert570.lean — T39 cycle 1324 (goal G786): rank ≥ 2 for 2 of the 8 congruent curves in 114601–114800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert570
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (114699, (true, 1590121, 2, 1919866195), (true, 1356277, 1, 1573852384)),
   (114730, (true, 46893429, 7, 318804517317), (true, 3418954, 1, 6318226992))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert570

#print axioms Rank2Cert570.on_curve
#print axioms Rank2Cert570.indep
#print axioms Rank2Cert570.count

bsd/Rank2Cert571.lean

lean_all/bsd/Rank2Cert571.lean

/-
  bsd/Rank2Cert571.lean — T39 cycle 1326 (goal G786): rank ≥ 2 for 4 of the 10 congruent curves in 114801–115000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert571
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (114810, (true, 9584410, 9, 7179594400), (true, 1481049, 2, 1713573693)),
   (114835, (true, 116365, 1, 6415800), (true, 11685724435, 67, 1262003290432500)),
   (114915, (false, 95039781, 29, 170927136072), (false, 34204595309, 997, 20156229807176836)),
   (114985, (true, 3129361, 4, 4478115591), (true, 2176785, 1, 3207129900))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert571

#print axioms Rank2Cert571.on_curve
#print axioms Rank2Cert571.indep
#print axioms Rank2Cert571.count

bsd/Rank2Cert572.lean

lean_all/bsd/Rank2Cert572.lean

/-
  bsd/Rank2Cert572.lean — T39 cycle 1328 (goal G786): rank ≥ 2 for 4 of the 11 congruent curves in 115001–115200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert572
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (115010, (true, 1082410, 3, 329329000), (false, 103261914, 163, 31033644291384)),
   (115090, (true, 124855705993225, 10074, 1389005404885326758075), (true, 1019915986, 87, 16940756483216)),
   (115115, (false, 87765860, 29, 382804544850), (true, 1203475, 1, 1314194700)),
   (115185, (true, 18051135, 11, 48738064500), (true, 2582786481025, 3422, 3539812245408799975))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert572

#print axioms Rank2Cert572.on_curve
#print axioms Rank2Cert572.indep
#print axioms Rank2Cert572.count

bsd/Rank2Cert573.lean

lean_all/bsd/Rank2Cert573.lean

/-
  bsd/Rank2Cert573.lean — T39 cycle 1330 (goal G786): rank ≥ 2 for 1 of the 3 congruent curves in 115201–115400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert573
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (115210, (false, 50410, 1, 23259600), (true, 3790999808401, 4902, 5042578901778711449))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert573

#print axioms Rank2Cert573.on_curve
#print axioms Rank2Cert573.indep
#print axioms Rank2Cert573.count

bsd/Rank2Cert574.lean

lean_all/bsd/Rank2Cert574.lean

/-
  bsd/Rank2Cert574.lean — T39 cycle 1332 (goal G786): rank ≥ 2 for 1 of the 6 congruent curves in 115401–115600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert574
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (115514, (true, 509786, 1, 354516240), (false, 652837183, 76, 3519255240159))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert574

#print axioms Rank2Cert574.on_curve
#print axioms Rank2Cert574.indep
#print axioms Rank2Cert574.count

bsd/Rank2Cert575.lean

lean_all/bsd/Rank2Cert575.lean

/-
  bsd/Rank2Cert575.lean — T39 cycle 1334 (goal G786): rank ≥ 2 for 1 of the 10 congruent curves in 115601–115800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert575
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (115721, (true, 199534636249, 391, 88779691982861880), (true, 22510859082721, 8757, 98155054197286033240))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert575

#print axioms Rank2Cert575.on_curve
#print axioms Rank2Cert575.indep
#print axioms Rank2Cert575.count

bsd/Rank2Cert576.lean

lean_all/bsd/Rank2Cert576.lean

/-
  bsd/Rank2Cert576.lean — T39 cycle 1336 (goal G786): rank ≥ 2 for 4 of the 12 congruent curves in 115801–116000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert576
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (115801, (true, 34690671, 5, 203611111656), (true, 485164017, 28, 10497664635081)),
   (115962, (true, 26236672, 9, 125481356000), (true, 24948396, 5, 123768992556)),
   (115994, (true, 5001553150, 183, 222812676702440), (true, 1260990598, 31, 44603017264848)),
   (115995, (true, 6952275, 7, 10556439300), (true, 413820, 1, 255533850))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert576

#print axioms Rank2Cert576.on_curve
#print axioms Rank2Cert576.indep
#print axioms Rank2Cert576.count

bsd/Rank2Cert577.lean

lean_all/bsd/Rank2Cert577.lean

/-
  bsd/Rank2Cert577.lean — T39 cycle 1338 (goal G797): rank ≥ 2 for 4 of the 8 congruent curves in 116001–116200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert577
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (116049, (true, 146652, 1, 34336566), (true, 143840463, 1, 1725128568072)),
   (116090, (false, 2840890, 7, 8306485200), (true, 281007454, 25, 4550875573608)),
   (116153, (true, 452025, 1, 293704320), (true, 9936144187225, 2111, 31277838446646221640)),
   (116186, (true, 1184908, 1, 1283597988), (true, 6406828, 7, 7438327308))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert577

#print axioms Rank2Cert577.on_curve
#print axioms Rank2Cert577.indep
#print axioms Rank2Cert577.count

bsd/Rank2Cert578.lean

lean_all/bsd/Rank2Cert578.lean

/-
  bsd/Rank2Cert578.lean — T39 cycle 1340 (goal G797): rank ≥ 2 for 1 of the 8 congruent curves in 116201–116400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert578
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (116290, (true, 359243010, 17, 6779125399200), (false, 640610, 17, 26894170800))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert578

#print axioms Rank2Cert578.on_curve
#print axioms Rank2Cert578.indep
#print axioms Rank2Cert578.count

bsd/Rank2Cert579.lean

lean_all/bsd/Rank2Cert579.lean

/-
  bsd/Rank2Cert579.lean — T39 cycle 1342 (goal G797): rank ≥ 2 for 2 of the 4 congruent curves in 116401–116600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert579
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (116410, (false, 5372890, 47, 595929879600), (true, 38717966, 11, 224409330936)),
   (116466, (false, 130921367, 34, 359306514197), (true, 1662766, 1, 2138842160))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert579

#print axioms Rank2Cert579.on_curve
#print axioms Rank2Cert579.indep
#print axioms Rank2Cert579.count

bsd/Rank2Cert58.lean

lean_all/bsd/Rank2Cert58.lean

/-
  bsd/Rank2Cert58.lean — T39 cycle 399 (goal G119): rank ≥ 2 for 5 of the 9 congruent curves in 12201–12400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert58
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (12257, (false, 9707647, 52, 98734833345), (false, 126221247, 161, 3275681943960)),
   (12306, (true, 2999094, 5, 5166399672), (false, 8750, 9, 93237200)),
   (12331, (true, 344619, 1, 202176480), (true, 278179, 3, 134537480)),
   (12361, (false, 158604743, 119, 932707784184), (true, 77810041, 41, 661436333880)),
   (12369, (true, 13552, 1, 644644), (true, 2193436, 9, 2889851930))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 5 := rfl

end Rank2Cert58

#print axioms Rank2Cert58.on_curve
#print axioms Rank2Cert58.indep
#print axioms Rank2Cert58.count

bsd/Rank2Cert580.lean

lean_all/bsd/Rank2Cert580.lean

/-
  bsd/Rank2Cert580.lean — T39 cycle 1344 (goal G797): rank ≥ 2 for 2 of the 5 congruent curves in 116601–116800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert580
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (116705, (true, 210135144025, 1001, 80033574356735700), (true, 121282490685889, 23253, 1140639306245713261988)),
   (116729, (false, 1737488, 13, 25902120804), (true, 24685962439009, 6748, 119775220331274597615))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert580

#print axioms Rank2Cert580.on_curve
#print axioms Rank2Cert580.indep
#print axioms Rank2Cert580.count

bsd/Rank2Cert581.lean

lean_all/bsd/Rank2Cert581.lean

/-
  bsd/Rank2Cert581.lean — T39 cycle 1346 (goal G797): rank ≥ 2 for 5 of the 8 congruent curves in 116801–117000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert581
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (116857, (true, 7419331584025, 3009, 20002566072308781320), (true, 6091183267037089, 3885, 475392670338892636212688)),
   (116915, (true, 1934960, 1, 2686666500), (true, 228826154915, 59, 109460595767264100)),
   (116961, (true, 61818159, 5, 485498314548), (true, 11921792452415521, 11006, 1301703368374120157202185)),
   (116985, (true, 502681, 2, 130194379), (false, 104757922775, 2617, 257090826404051200)),
   (116994, (true, 139638, 1, 28486152), (true, 657528702, 73, 5356750703184))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 5 := rfl

end Rank2Cert581

#print axioms Rank2Cert581.on_curve
#print axioms Rank2Cert581.indep
#print axioms Rank2Cert581.count

bsd/Rank2Cert582.lean

lean_all/bsd/Rank2Cert582.lean

/-
  bsd/Rank2Cert582.lean — T39 cycle 1348 (goal G797): rank ≥ 2 for 2 of the 4 congruent curves in 117001–117200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert582
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (117059, (false, 283024, 5, 1549586724), (true, 58940124489, 50, 14309063791857987)),
   (117130, (true, 474721, 2, 52694031), (true, 258570, 1, 117218400))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert582

#print axioms Rank2Cert582.on_curve
#print axioms Rank2Cert582.indep
#print axioms Rank2Cert582.count

bsd/Rank2Cert583.lean

lean_all/bsd/Rank2Cert583.lean

/-
  bsd/Rank2Cert583.lean — T39 cycle 1350 (goal G797): rank ≥ 2 for 1 of the 5 congruent curves in 117201–117400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert583
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (117363, (true, 3470205372, 163, 89708200373610), (false, 327892271, 60, 4824925918183))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert583

#print axioms Rank2Cert583.on_curve
#print axioms Rank2Cert583.indep
#print axioms Rank2Cert583.count

bsd/Rank2Cert584.lean

lean_all/bsd/Rank2Cert584.lean

/-
  bsd/Rank2Cert584.lean — T39 cycle 1352 (goal G797): rank ≥ 2 for 1 of the 4 congruent curves in 117401–117600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert584
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (117546, (true, 412918, 1, 254357488), (false, 12920622, 19, 145287854712))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert584

#print axioms Rank2Cert584.on_curve
#print axioms Rank2Cert584.indep
#print axioms Rank2Cert584.count

bsd/Rank2Cert585.lean

lean_all/bsd/Rank2Cert585.lean

/-
  bsd/Rank2Cert585.lean — T39 cycle 1354 (goal G797): rank ≥ 2 for 3 of the 11 congruent curves in 117601–117800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert585
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (117705, (true, 123120, 1, 12671100), (true, 13683705, 4, 50136374925)),
   (117730, (false, 126494480, 33, 234959568200), (true, 6532680625, 68, 526167414756375)),
   (117746, (false, 817216, 5, 2556455952), (false, 4751650, 37, 351223092480))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert585

#print axioms Rank2Cert585.on_curve
#print axioms Rank2Cert585.indep
#print axioms Rank2Cert585.count

bsd/Rank2Cert586.lean

lean_all/bsd/Rank2Cert586.lean

/-
  bsd/Rank2Cert586.lean — T39 cycle 1356 (goal G797): rank ≥ 2 for 0 of the 3 congruent curves in 117801–118000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert586
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert586

#print axioms Rank2Cert586.on_curve
#print axioms Rank2Cert586.indep
#print axioms Rank2Cert586.count

bsd/Rank2Cert587.lean

lean_all/bsd/Rank2Cert587.lean

/-
  bsd/Rank2Cert587.lean — T39 cycle 1358 (goal G806): rank ≥ 2 for 3 of the 6 congruent curves in 118001–118200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert587
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (118001, (true, 44486377, 8, 292408956021), (false, 1476498257, 497, 1118554122530832)),
   (118066, (true, 37128650, 17, 89197868760), (true, 19535382, 11, 58892664936)),
   (118105, (false, 7252911, 8, 5731690713), (true, 770708380, 3, 21396104417350))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert587

#print axioms Rank2Cert587.on_curve
#print axioms Rank2Cert587.indep
#print axioms Rank2Cert587.count

bsd/Rank2Cert588.lean

lean_all/bsd/Rank2Cert588.lean

/-
  bsd/Rank2Cert588.lean — T39 cycle 1360 (goal G806): rank ≥ 2 for 2 of the 2 congruent curves in 118201–118400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert588
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (118266, (false, 51842, 1, 24202808), (true, 5797150, 7, 377092360)),
   (118371, (false, 128329872, 41, 1722685819932), (true, 3264459, 5, 2490072552))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert588

#print axioms Rank2Cert588.on_curve
#print axioms Rank2Cert588.indep
#print axioms Rank2Cert588.count

bsd/Rank2Cert589.lean

lean_all/bsd/Rank2Cert589.lean

/-
  bsd/Rank2Cert589.lean — T39 cycle 1362 (goal G806): rank ≥ 2 for 2 of the 8 congruent curves in 118401–118600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert589
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (118545, (true, 471345, 1, 313198200), (false, 336054705, 97, 19496981057400)),
   (118594, (false, 86247, 2, 136992093), (true, 1120021, 3, 359232965))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert589

#print axioms Rank2Cert589.on_curve
#print axioms Rank2Cert589.indep
#print axioms Rank2Cert589.count

bsd/Rank2Cert59.lean

lean_all/bsd/Rank2Cert59.lean

/-
  bsd/Rank2Cert59.lean — T39 cycle 400 (goal G120): rank ≥ 2 for 6 of the 7 congruent curves in 12401–12600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert59
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (12426, (true, 16758, 1, 1455552), (false, 365030100, 383, 34119690178860)),
   (12435, (true, 687241, 6, 432274589), (true, 2851440, 11, 4090083300)),
   (12505, (false, 4900, 1, 805350), (true, 152561, 2, 56295009)),
   (12545, (true, 5154502145, 448, 322912632353025), (true, 1376481900738961, 19943, 51068476830739562459784)),
   (12570, (true, 5266830, 13, 11060343000), (false, 23105840895, 3214, 19422295737840225)),
   (12595, (true, 1311025, 8, 1183855575), (true, 50197225, 62, 93921487725))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 6 := rfl

end Rank2Cert59

#print axioms Rank2Cert59.on_curve
#print axioms Rank2Cert59.indep
#print axioms Rank2Cert59.count

bsd/Rank2Cert590.lean

lean_all/bsd/Rank2Cert590.lean

/-
  bsd/Rank2Cert590.lean — T39 cycle 1364 (goal G806): rank ≥ 2 for 2 of the 6 congruent curves in 118601–118800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert590
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (118626, (false, 2154512, 11, 20830100408), (true, 110772850, 11, 1156040436880)),
   (118657, (false, 4903983, 16, 66385487385), (true, 3182832, 1, 5674381980))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert590

#print axioms Rank2Cert590.on_curve
#print axioms Rank2Cert590.indep
#print axioms Rank2Cert590.count

bsd/Rank2Cert591.lean

lean_all/bsd/Rank2Cert591.lean

/-
  bsd/Rank2Cert591.lean — T39 cycle 1366 (goal G806): rank ≥ 2 for 2 of the 5 congruent curves in 118801–119000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert591
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (118929, (true, 86652348, 23, 554672532798), (true, 178467830625, 142, 75387736925853975)),
   (118985, (true, 489985, 2, 81531225), (true, 3303337116862225, 71409, 186628214196436594012300))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert591

#print axioms Rank2Cert591.on_curve
#print axioms Rank2Cert591.indep
#print axioms Rank2Cert591.count

bsd/Rank2Cert592.lean

lean_all/bsd/Rank2Cert592.lean

/-
  bsd/Rank2Cert592.lean — T39 cycle 1368 (goal G806): rank ≥ 2 for 2 of the 5 congruent curves in 119001–119200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert592
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (119051, (false, 79524, 1, 24983790), (true, 430103549, 49, 6665035473300)),
   (119195, (false, 3844, 1, 7386246), (true, 39057817600, 107, 7714303298822400))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert592

#print axioms Rank2Cert592.on_curve
#print axioms Rank2Cert592.indep
#print axioms Rank2Cert592.count

bsd/Rank2Cert593.lean

lean_all/bsd/Rank2Cert593.lean

/-
  bsd/Rank2Cert593.lean — T39 cycle 1370 (goal G806): rank ≥ 2 for 2 of the 8 congruent curves in 119201–119400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert593
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (119210, (true, 54057150, 13, 368813915400), (false, 11466350, 13, 56092877400)),
   (119355, (true, 14521525, 7, 50651079200), (true, 46798583220601, 6538, 318238578927289052549))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert593

#print axioms Rank2Cert593.on_curve
#print axioms Rank2Cert593.indep
#print axioms Rank2Cert593.count

bsd/Rank2Cert594.lean

lean_all/bsd/Rank2Cert594.lean

/-
  bsd/Rank2Cert594.lean — T39 cycle 1372 (goal G806): rank ≥ 2 for 3 of the 5 congruent curves in 119401–119600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert594
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (119474, (true, 868562, 1, 801776304), (false, 1272857582, 273, 314416525581712)),
   (119490, (false, 10115, 1, 11974375), (false, 7307100276, 727, 5362240541651532)),
   (119595, (true, 54772725, 1, 405364289400), (true, 228939, 1, 93407112))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert594

#print axioms Rank2Cert594.on_curve
#print axioms Rank2Cert594.indep
#print axioms Rank2Cert594.count

bsd/Rank2Cert595.lean

lean_all/bsd/Rank2Cert595.lean

/-
  bsd/Rank2Cert595.lean — T39 cycle 1374 (goal G806): rank ≥ 2 for 2 of the 8 congruent curves in 119601–119800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert595
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (119761, (true, 152561, 1, 36914760), (true, 1464561, 1, 1766462040)),
   (119795, (true, 7770620, 7, 14193743850), (true, 59230417129, 222, 14343293359655533))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert595

#print axioms Rank2Cert595.on_curve
#print axioms Rank2Cert595.indep
#print axioms Rank2Cert595.count

bsd/Rank2Cert596.lean

lean_all/bsd/Rank2Cert596.lean

/-
  bsd/Rank2Cert596.lean — T39 cycle 1376 (goal G806): rank ≥ 2 for 1 of the 10 congruent curves in 119801–120000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert596
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (119969, (false, 1071017, 3, 141596128), (true, 4975503272428802689, 5097009, 8650983610204830077953753520))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert596

#print axioms Rank2Cert596.on_curve
#print axioms Rank2Cert596.indep
#print axioms Rank2Cert596.count

bsd/Rank2Cert597.lean

lean_all/bsd/Rank2Cert597.lean

/-
  bsd/Rank2Cert597.lean — T39 cycle 1378 (goal G815): rank ≥ 2 for 0 of the 6 congruent curves in 120001–120200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert597
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert597

#print axioms Rank2Cert597.on_curve
#print axioms Rank2Cert597.indep
#print axioms Rank2Cert597.count

bsd/Rank2Cert598.lean

lean_all/bsd/Rank2Cert598.lean

/-
  bsd/Rank2Cert598.lean — T39 cycle 1380 (goal G815): rank ≥ 2 for 1 of the 9 congruent curves in 120201–120400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert598
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (120241, (true, 47264043763225, 1389, 324930951502608945520), (false, 229096097, 489, 435176421006392))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert598

#print axioms Rank2Cert598.on_curve
#print axioms Rank2Cert598.indep
#print axioms Rank2Cert598.count

bsd/Rank2Cert599.lean

lean_all/bsd/Rank2Cert599.lean

/-
  bsd/Rank2Cert599.lean — T39 cycle 1382 (goal G815): rank ≥ 2 for 4 of the 10 congruent curves in 120401–120600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert599
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (120530, (false, 2238016, 5, 3018422352), (false, 5345803280, 521, 2359939205177400)),
   (120554, (false, 1914703, 4, 322823319), (true, 548381502, 67, 2076207100344)),
   (120594, (true, 237606, 1, 99794520), (true, 7999402, 1, 22622308856)),
   (120595, (true, 525625, 2, 151369125), (true, 2725625, 2, 4428829125))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert599

#print axioms Rank2Cert599.on_curve
#print axioms Rank2Cert599.indep
#print axioms Rank2Cert599.count

bsd/Rank2Cert6.lean

lean_all/bsd/Rank2Cert6.lean

/-
  bsd/Rank2Cert6.lean — T39 cycle 328 (goal G63): rank ≥ 2 for 2 of the 3 congruent curves in 1801–2000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert6
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (1939, (true, 76729, 6, 8823835), (true, 125692, 3, 44130086)),
   (1995, (true, 2205, 1, 44100), (true, 23275, 1, 3537800))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert6

#print axioms Rank2Cert6.on_curve
#print axioms Rank2Cert6.indep
#print axioms Rank2Cert6.count

bsd/Rank2Cert60.lean

lean_all/bsd/Rank2Cert60.lean

/-
  bsd/Rank2Cert60.lean — T39 cycle 403 (goal G121): rank ≥ 2 for 5 of the 7 congruent curves in 12601–12800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert60
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (12673, (true, 13248, 1, 444360), (true, 13957135605625, 31017, 25380652138292478700)),
   (12682, (true, 2339897, 2, 3578434125), (true, 63834086667187225, 101764, 16127908623991279703843715)),
   (12747, (true, 638428, 1, 510013070), (false, 46434549, 125, 1319815025676)),
   (12761, (true, 43687, 1, 8732976), (true, 77338463704441, 44609, 642423513665430413400)),
   (12785, (true, 22242844385, 1238, 1569774577202775), (true, 3067909887025, 2992, 5369841567570309975))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 5 := rfl

end Rank2Cert60

#print axioms Rank2Cert60.on_curve
#print axioms Rank2Cert60.indep
#print axioms Rank2Cert60.count

bsd/Rank2Cert600.lean

lean_all/bsd/Rank2Cert600.lean

/-
  bsd/Rank2Cert600.lean — T39 cycle 1384 (goal G815): rank ≥ 2 for 1 of the 4 congruent curves in 120601–120800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert600
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (120649, (true, 217328, 1, 84268932), (true, 169647777, 16, 2172714738831))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert600

#print axioms Rank2Cert600.on_curve
#print axioms Rank2Cert600.indep
#print axioms Rank2Cert600.count

bsd/Rank2Cert601.lean

lean_all/bsd/Rank2Cert601.lean

/-
  bsd/Rank2Cert601.lean — T39 cycle 1386 (goal G815): rank ≥ 2 for 1 of the 10 congruent curves in 120801–121000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert601
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (120930, (true, 247254, 1, 107237592), (true, 52556192689225, 18318, 242137957558276301275))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert601

#print axioms Rank2Cert601.on_curve
#print axioms Rank2Cert601.indep
#print axioms Rank2Cert601.count

bsd/Rank2Cert602.lean

lean_all/bsd/Rank2Cert602.lean

/-
  bsd/Rank2Cert602.lean — T39 cycle 1388 (goal G815): rank ≥ 2 for 0 of the 5 congruent curves in 121001–121200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert602
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert602

#print axioms Rank2Cert602.on_curve
#print axioms Rank2Cert602.indep
#print axioms Rank2Cert602.count

bsd/Rank2Cert603.lean

lean_all/bsd/Rank2Cert603.lean

/-
  bsd/Rank2Cert603.lean — T39 cycle 1390 (goal G815): rank ≥ 2 for 1 of the 5 congruent curves in 121201–121400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert603
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (121290, (true, 2840890, 3, 4420702000), (true, 3894384, 1, 7681524552))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert603

#print axioms Rank2Cert603.on_curve
#print axioms Rank2Cert603.indep
#print axioms Rank2Cert603.count

bsd/Rank2Cert604.lean

lean_all/bsd/Rank2Cert604.lean

/-
  bsd/Rank2Cert604.lean — T39 cycle 1392 (goal G815): rank ≥ 2 for 2 of the 5 congruent curves in 121401–121600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert604
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (121539, (true, 532257, 2, 158080329), (false, 809032037, 541, 1011534495120512)),
   (121555, (true, 1116796, 3, 237267206), (false, 358414355, 239, 131274974461800))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert604

#print axioms Rank2Cert604.on_curve
#print axioms Rank2Cert604.indep
#print axioms Rank2Cert604.count

bsd/Rank2Cert605.lean

lean_all/bsd/Rank2Cert605.lean

/-
  bsd/Rank2Cert605.lean — T39 cycle 1394 (goal G815): rank ≥ 2 for 2 of the 8 congruent curves in 121601–121800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert605
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (121730, (true, 211688470, 23, 2934002194200), (true, 150790241, 16, 1811680598511)),
   (121737, (false, 6201872, 13, 48852114940), (true, 467313, 1, 308426580))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert605

#print axioms Rank2Cert605.on_curve
#print axioms Rank2Cert605.indep
#print axioms Rank2Cert605.count

bsd/Rank2Cert606.lean

lean_all/bsd/Rank2Cert606.lean

/-
  bsd/Rank2Cert606.lean — T39 cycle 1396 (goal G815): rank ≥ 2 for 0 of the 4 congruent curves in 121801–122000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert606
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert606

#print axioms Rank2Cert606.on_curve
#print axioms Rank2Cert606.indep
#print axioms Rank2Cert606.count

bsd/Rank2Cert607.lean

lean_all/bsd/Rank2Cert607.lean

/-
  bsd/Rank2Cert607.lean — T39 cycle 1398 (goal G820): rank ≥ 2 for 0 of the 4 congruent curves in 122001–122200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert607
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert607

#print axioms Rank2Cert607.on_curve
#print axioms Rank2Cert607.indep
#print axioms Rank2Cert607.count

bsd/Rank2Cert608.lean

lean_all/bsd/Rank2Cert608.lean

/-
  bsd/Rank2Cert608.lean — T39 cycle 1400 (goal G820): rank ≥ 2 for 2 of the 5 congruent curves in 122201–122400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert608
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (122241, (false, 44100, 1, 23941890), (false, 22743, 2, 73659789)),
   (122322, (true, 75636954, 5, 657273010392), (true, 3077886, 5, 612059616))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert608

#print axioms Rank2Cert608.on_curve
#print axioms Rank2Cert608.indep
#print axioms Rank2Cert608.count

bsd/Rank2Cert609.lean

lean_all/bsd/Rank2Cert609.lean

/-
  bsd/Rank2Cert609.lean — T39 cycle 1402 (goal G820): rank ≥ 2 for 1 of the 4 congruent curves in 122401–122600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert609
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (122507, (false, 218986831, 550, 548387694005697), (true, 53756171833, 438, 11209287232334555))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert609

#print axioms Rank2Cert609.on_curve
#print axioms Rank2Cert609.indep
#print axioms Rank2Cert609.count

bsd/Rank2Cert61.lean

lean_all/bsd/Rank2Cert61.lean

/-
  bsd/Rank2Cert61.lean — T39 cycle 404 (goal G122): rank ≥ 2 for 7 of the 11 congruent curves in 12801–13000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert61
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (12833, (true, 77383426336225, 39649, 657185380465160195040), (true, 3795944031780289, 65775, 233847775316818458745712)),
   (12859, (true, 13552, 1, 498036), (true, 11651717, 17, 37695491064)),
   (12889, (true, 43797571343788249, 309120, 9162273740078742581481443), (true, 141187175892841, 90137, 1125225179509651791480)),
   (12929, (true, 3751969, 17, 659664720), (false, 5585551487, 828, 514384096843457)),
   (12930, (true, 178605, 1, 75283425), (true, 9450150, 19, 25259686200)),
   (12937, (false, 314721, 5, 41810208), (true, 28774217, 47, 18002353320)),
   (12994, (false, 12544, 1, 379680), (true, 15842, 1, 1140624))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 7 := rfl

end Rank2Cert61

#print axioms Rank2Cert61.on_curve
#print axioms Rank2Cert61.indep
#print axioms Rank2Cert61.count

bsd/Rank2Cert610.lean

lean_all/bsd/Rank2Cert610.lean

/-
  bsd/Rank2Cert610.lean — T39 cycle 1404 (goal G820): rank ≥ 2 for 2 of the 5 congruent curves in 122601–122800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert610
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (122619, (false, 73947, 1, 26598312), (true, 251548941, 17, 3949853305860)),
   (122785, (true, 491985, 2, 20216625), (true, 190931780065, 642, 80445057991008425))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert610

#print axioms Rank2Cert610.on_curve
#print axioms Rank2Cert610.indep
#print axioms Rank2Cert610.count

bsd/Rank2Cert611.lean

lean_all/bsd/Rank2Cert611.lean

/-
  bsd/Rank2Cert611.lean — T39 cycle 1406 (goal G820): rank ≥ 2 for 1 of the 3 congruent curves in 122801–123000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert611
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (122978, (false, 94178, 1, 24269280), (false, 24807488, 27, 429090068080))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert611

#print axioms Rank2Cert611.on_curve
#print axioms Rank2Cert611.indep
#print axioms Rank2Cert611.count

bsd/Rank2Cert612.lean

lean_all/bsd/Rank2Cert612.lean

/-
  bsd/Rank2Cert612.lean — T39 cycle 1408 (goal G820): rank ≥ 2 for 0 of the 3 congruent curves in 123001–123200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert612
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert612

#print axioms Rank2Cert612.on_curve
#print axioms Rank2Cert612.indep
#print axioms Rank2Cert612.count

bsd/Rank2Cert613.lean

lean_all/bsd/Rank2Cert613.lean

/-
  bsd/Rank2Cert613.lean — T39 cycle 1410 (goal G820): rank ≥ 2 for 3 of the 7 congruent curves in 123201–123400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert613
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (123234, (true, 133215954, 13, 1518661875600), (true, 36885882, 17, 58306519656)),
   (123281, (true, 116737472, 23, 1046117596776), (true, 354189395025, 326, 210647348732534415)),
   (123321, (false, 8448, 1, 11308176), (true, 1111355569, 42, 36332563309055))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert613

#print axioms Rank2Cert613.on_curve
#print axioms Rank2Cert613.indep
#print axioms Rank2Cert613.count

bsd/Rank2Cert614.lean

lean_all/bsd/Rank2Cert614.lean

/-
  bsd/Rank2Cert614.lean — T39 cycle 1412 (goal G820): rank ≥ 2 for 1 of the 8 congruent curves in 123401–123600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert614
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (123481, (true, 235225, 1, 97100880), (false, 1153873297, 251, 261334103425512))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert614

#print axioms Rank2Cert614.on_curve
#print axioms Rank2Cert614.indep
#print axioms Rank2Cert614.count

bsd/Rank2Cert615.lean

lean_all/bsd/Rank2Cert615.lean

/-
  bsd/Rank2Cert615.lean — T39 cycle 1414 (goal G820): rank ≥ 2 for 2 of the 6 congruent curves in 123601–123800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert615
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (123618, (false, 5743650, 7, 4610651760), (true, 244660028959752025, 405808, 120596834198405373837441245)),
   (123682, (true, 107727022, 27, 611889484960), (false, 110241553172, 1611, 100096065856951100))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert615

#print axioms Rank2Cert615.on_curve
#print axioms Rank2Cert615.indep
#print axioms Rank2Cert615.count

bsd/Rank2Cert616.lean

lean_all/bsd/Rank2Cert616.lean

/-
  bsd/Rank2Cert616.lean — T39 cycle 1416 (goal G820): rank ≥ 2 for 0 of the 6 congruent curves in 123801–124000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert616
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert616

#print axioms Rank2Cert616.on_curve
#print axioms Rank2Cert616.indep
#print axioms Rank2Cert616.count

bsd/Rank2Cert617.lean

lean_all/bsd/Rank2Cert617.lean

/-
  bsd/Rank2Cert617.lean — T39 cycle 1418 (goal G821): rank ≥ 2 for 1 of the 9 congruent curves in 124001–124200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert617
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (124066, (true, 308898, 1, 157225152), (false, 37538, 1, 22910784))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert617

#print axioms Rank2Cert617.on_curve
#print axioms Rank2Cert617.indep
#print axioms Rank2Cert617.count

bsd/Rank2Cert618.lean

lean_all/bsd/Rank2Cert618.lean

/-
  bsd/Rank2Cert618.lean — T39 cycle 1420 (goal G821): rank ≥ 2 for 0 of the 3 congruent curves in 124201–124400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert618
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert618

#print axioms Rank2Cert618.on_curve
#print axioms Rank2Cert618.indep
#print axioms Rank2Cert618.count

bsd/Rank2Cert619.lean

lean_all/bsd/Rank2Cert619.lean

/-
  bsd/Rank2Cert619.lean — T39 cycle 1422 (goal G821): rank ≥ 2 for 2 of the 3 congruent curves in 124401–124600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert619
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (124410, (true, 637065, 2, 317470725), (true, 138765, 1, 22896225)),
   (124530, (false, 14466900, 17, 125339652900), (true, 134670, 1, 18813600))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert619

#print axioms Rank2Cert619.on_curve
#print axioms Rank2Cert619.indep
#print axioms Rank2Cert619.count

bsd/Rank2Cert62.lean

lean_all/bsd/Rank2Cert62.lean

/-
  bsd/Rank2Cert62.lean — T39 cycle 405 (goal G123): rank ≥ 2 for 3 of the 7 congruent curves in 13001–13200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert62
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (13019, (true, 334839294409, 3306, 175390168019800195), (true, 979872732, 97, 30432255307566)),
   (13065, (true, 319225, 3, 167680700), (true, 15600, 1, 1064700)),
   (13195, (true, 243049, 2, 116963757), (true, 122525, 1, 42638700))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert62

#print axioms Rank2Cert62.on_curve
#print axioms Rank2Cert62.indep
#print axioms Rank2Cert62.count

bsd/Rank2Cert620.lean

lean_all/bsd/Rank2Cert620.lean

/-
  bsd/Rank2Cert620.lean — T39 cycle 1424 (goal G821): rank ≥ 2 for 0 of the 4 congruent curves in 124601–124800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert620
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert620

#print axioms Rank2Cert620.on_curve
#print axioms Rank2Cert620.indep
#print axioms Rank2Cert620.count

bsd/Rank2Cert621.lean

lean_all/bsd/Rank2Cert621.lean

/-
  bsd/Rank2Cert621.lean — T39 cycle 1426 (goal G821): rank ≥ 2 for 1 of the 4 congruent curves in 124801–125000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert621
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (124962, (false, 191218688, 201, 69762737430080), (true, 2138412225, 92, 85943816072415))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert621

#print axioms Rank2Cert621.on_curve
#print axioms Rank2Cert621.indep
#print axioms Rank2Cert621.count

bsd/Rank2Cert622.lean

lean_all/bsd/Rank2Cert622.lean

/-
  bsd/Rank2Cert622.lean — T39 cycle 1428 (goal G821): rank ≥ 2 for 1 of the 6 congruent curves in 125001–125200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert622
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (125106, (true, 126150, 1, 5752440), (true, 70846101, 23, 212808225285))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert622

#print axioms Rank2Cert622.on_curve
#print axioms Rank2Cert622.indep
#print axioms Rank2Cert622.count

bsd/Rank2Cert623.lean

lean_all/bsd/Rank2Cert623.lean

/-
  bsd/Rank2Cert623.lean — T39 cycle 1430 (goal G821): rank ≥ 2 for 0 of the 8 congruent curves in 125201–125400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert623
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert623

#print axioms Rank2Cert623.on_curve
#print axioms Rank2Cert623.indep
#print axioms Rank2Cert623.count

bsd/Rank2Cert624.lean

lean_all/bsd/Rank2Cert624.lean

/-
  bsd/Rank2Cert624.lean — T39 cycle 1432 (goal G821): rank ≥ 2 for 1 of the 4 congruent curves in 125401–125600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert624
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (125419, (true, 3426908, 1, 6339611118), (true, 725249, 2, 446028135))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert624

#print axioms Rank2Cert624.on_curve
#print axioms Rank2Cert624.indep
#print axioms Rank2Cert624.count

bsd/Rank2Cert625.lean

lean_all/bsd/Rank2Cert625.lean

/-
  bsd/Rank2Cert625.lean — T39 cycle 1436 (goal G821): rank ≥ 2 for 5 of the 10 congruent curves in 125601–125800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert625
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (125643, (false, 544887, 22, 44886882915), (true, 3464157, 1, 6443332020)),
   (125665, (false, 10496, 1, 12829392), (true, 12860694025, 64, 1457298420680475)),
   (125690, (true, 176890, 1, 52348800), (false, 86490, 1, 26821200)),
   (125769, (true, 2379223, 3, 3227956732), (true, 4171888, 3, 8201526508)),
   (125795, (true, 169561722195, 1063, 38067006804283800), (true, 43833703225, 562, 3876328487439525))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 5 := rfl

end Rank2Cert625

#print axioms Rank2Cert625.on_curve
#print axioms Rank2Cert625.indep
#print axioms Rank2Cert625.count

bsd/Rank2Cert626.lean

lean_all/bsd/Rank2Cert626.lean

/-
  bsd/Rank2Cert626.lean — T39 cycle 1442 (goal G821): rank ≥ 2 for 1 of the 4 congruent curves in 125801–126000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert626
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (125994, (false, 1679046, 5, 3453286608), (false, 39019086, 37, 1049506651080))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert626

#print axioms Rank2Cert626.on_curve
#print axioms Rank2Cert626.indep
#print axioms Rank2Cert626.count

bsd/Rank2Cert627.lean

lean_all/bsd/Rank2Cert627.lean

/-
  bsd/Rank2Cert627.lean — T39 cycle 1446 (goal G831): rank ≥ 2 for 1 of the 6 congruent curves in 126001–126200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert627
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (126105, (true, 1537305, 1, 1899651600), (false, 41806905, 31, 735470253000))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert627

#print axioms Rank2Cert627.on_curve
#print axioms Rank2Cert627.indep
#print axioms Rank2Cert627.count

bsd/Rank2Cert628.lean

lean_all/bsd/Rank2Cert628.lean

/-
  bsd/Rank2Cert628.lean — T39 cycle 1448 (goal G831): rank ≥ 2 for 0 of the 1 congruent curves in 126201–126400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert628
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert628

#print axioms Rank2Cert628.on_curve
#print axioms Rank2Cert628.indep
#print axioms Rank2Cert628.count

bsd/Rank2Cert629.lean

lean_all/bsd/Rank2Cert629.lean

/-
  bsd/Rank2Cert629.lean — T39 cycle 1450 (goal G831): rank ≥ 2 for 0 of the 6 congruent curves in 126401–126600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert629
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert629

#print axioms Rank2Cert629.on_curve
#print axioms Rank2Cert629.indep
#print axioms Rank2Cert629.count

bsd/Rank2Cert63.lean

lean_all/bsd/Rank2Cert63.lean

/-
  bsd/Rank2Cert63.lean — T39 cycle 406 (goal G124): rank ≥ 2 for 6 of the 13 congruent curves in 13201–13400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert63
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (13202, (true, 826898, 7, 468361200), (true, 13248, 1, 126960)),
   (13258, (false, 8485407, 64, 156245205585), (true, 10275115725, 317, 1032757518390465)),
   (13299, (false, 11892243, 31, 16142438664), (false, 33813, 7, 119666196)),
   (13345, (true, 24649, 1, 3253668), (false, 51984, 25, 1901625564)),
   (13362, (true, 2625633, 4, 4240397295), (true, 966990226, 145, 28772971326224)),
   (13395, (false, 11265005, 29, 219596300), (true, 12129619, 3, 42242468428))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 6 := rfl

end Rank2Cert63

#print axioms Rank2Cert63.on_curve
#print axioms Rank2Cert63.indep
#print axioms Rank2Cert63.count

bsd/Rank2Cert630.lean

lean_all/bsd/Rank2Cert630.lean

/-
  bsd/Rank2Cert630.lean — T39 cycle 1452 (goal G831): rank ≥ 2 for 1 of the 4 congruent curves in 126601–126800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert630
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (126755, (true, 1311025, 2, 1384322175), (true, 36824989355, 37, 7066577448915600))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert630

#print axioms Rank2Cert630.on_curve
#print axioms Rank2Cert630.indep
#print axioms Rank2Cert630.count

bsd/Rank2Cert631.lean

lean_all/bsd/Rank2Cert631.lean

/-
  bsd/Rank2Cert631.lean — T39 cycle 1454 (goal G831): rank ≥ 2 for 0 of the 8 congruent curves in 126801–127000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert631
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert631

#print axioms Rank2Cert631.on_curve
#print axioms Rank2Cert631.indep
#print axioms Rank2Cert631.count

bsd/Rank2Cert632.lean

lean_all/bsd/Rank2Cert632.lean

/-
  bsd/Rank2Cert632.lean — T39 cycle 1456 (goal G831): rank ≥ 2 for 2 of the 13 congruent curves in 127001–127200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert632
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (127065, (false, 1359015, 62, 569403016875), (true, 221563225, 37, 2042608712300)),
   (127185, (false, 6753615, 53, 928278242100), (true, 6904746465, 184, 448512623686575))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert632

#print axioms Rank2Cert632.on_curve
#print axioms Rank2Cert632.indep
#print axioms Rank2Cert632.count

bsd/Rank2Cert633.lean

lean_all/bsd/Rank2Cert633.lean

/-
  bsd/Rank2Cert633.lean — T39 cycle 1458 (goal G831): rank ≥ 2 for 3 of the 8 congruent curves in 127201–127400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert633
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (127218, (true, 612157, 1, 468497575), (false, 19394606, 15, 92711423728)),
   (127281, (true, 1151329, 3, 123845660), (true, 515823, 1, 359012808)),
   (127361, (true, 123067533329, 781, 33484647645840300), (true, 153332546929, 1070, 18568671136829217))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert633

#print axioms Rank2Cert633.on_curve
#print axioms Rank2Cert633.indep
#print axioms Rank2Cert633.count

bsd/Rank2Cert634.lean

lean_all/bsd/Rank2Cert634.lean

/-
  bsd/Rank2Cert634.lean — T39 cycle 1460 (goal G831): rank ≥ 2 for 0 of the 5 congruent curves in 127401–127600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert634
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert634

#print axioms Rank2Cert634.on_curve
#print axioms Rank2Cert634.indep
#print axioms Rank2Cert634.count

bsd/Rank2Cert635.lean

lean_all/bsd/Rank2Cert635.lean

/-
  bsd/Rank2Cert635.lean — T39 cycle 1462 (goal G831): rank ≥ 2 for 0 of the 9 congruent curves in 127601–127800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert635
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert635

#print axioms Rank2Cert635.on_curve
#print axioms Rank2Cert635.indep
#print axioms Rank2Cert635.count

bsd/Rank2Cert636.lean

lean_all/bsd/Rank2Cert636.lean

/-
  bsd/Rank2Cert636.lean — T39 cycle 1464 (goal G831): rank ≥ 2 for 0 of the 7 congruent curves in 127801–128000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert636
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert636

#print axioms Rank2Cert636.on_curve
#print axioms Rank2Cert636.indep
#print axioms Rank2Cert636.count

bsd/Rank2Cert637.lean

lean_all/bsd/Rank2Cert637.lean

/-
  bsd/Rank2Cert637.lean — T39 cycle 1466 (goal G835): rank ≥ 2 for 0 of the 4 congruent curves in 128001–128200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert637
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert637

#print axioms Rank2Cert637.on_curve
#print axioms Rank2Cert637.indep
#print axioms Rank2Cert637.count

bsd/Rank2Cert638.lean

lean_all/bsd/Rank2Cert638.lean

/-
  bsd/Rank2Cert638.lean — T39 cycle 1468 (goal G835): rank ≥ 2 for 3 of the 6 congruent curves in 128201–128400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert638
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (128345, (true, 226473920, 23, 3251457760200), (false, 5037556111, 416, 1535351300368137)),
   (128369, (true, 42504113, 17, 135237251688), (true, 87869638033865689, 547155, 23423706899757633664440512)),
   (128370, (true, 3783025, 2, 7289889175), (true, 7217100, 7, 9505989900))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert638

#print axioms Rank2Cert638.on_curve
#print axioms Rank2Cert638.indep
#print axioms Rank2Cert638.count

bsd/Rank2Cert639.lean

lean_all/bsd/Rank2Cert639.lean

/-
  bsd/Rank2Cert639.lean — T39 cycle 1470 (goal G835): rank ≥ 2 for 2 of the 6 congruent curves in 128401–128600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert639
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (128441, (true, 4456192, 3, 9084868624), (true, 113106433969, 297, 37847847369108140)),
   (128481, (true, 32297918652791089, 93815, 5800901127263888659465312), (false, 1287903488, 177, 136859347597904))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert639

#print axioms Rank2Cert639.on_curve
#print axioms Rank2Cert639.indep
#print axioms Rank2Cert639.count

bsd/Rank2Cert64.lean

lean_all/bsd/Rank2Cert64.lean

/-
  bsd/Rank2Cert64.lean — T39 cycle 407 (goal G125): rank ≥ 2 for 6 of the 10 congruent curves in 13401–13600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert64
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (13442, (false, 5655078, 47, 69319656120), (true, 12922650, 17, 44305736280)),
   (13515, (true, 811801, 2, 729809099), (true, 17340, 1, 1430550)),
   (13546, (true, 18954, 1, 1825200), (true, 899541136656030625, 973346, 853075246911139578204540975)),
   (13561, (false, 1705775, 12, 1241573095), (true, 578788374436441, 132641, 12686405854280310832680)),
   (13585, (true, 284089, 1, 151246212), (true, 2839265, 14, 1660970025)),
   (13593, (false, 1071225, 29, 11779761420), (false, 164825567, 284, 13915538875855))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 6 := rfl

end Rank2Cert64

#print axioms Rank2Cert64.on_curve
#print axioms Rank2Cert64.indep
#print axioms Rank2Cert64.count

bsd/Rank2Cert640.lean

lean_all/bsd/Rank2Cert640.lean

/-
  bsd/Rank2Cert640.lean — T39 cycle 1472 (goal G835): rank ≥ 2 for 5 of the 12 congruent curves in 128601–128800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert640
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (128611, (false, 204687, 22, 28162179297), (true, 160053397, 23, 1832825286516)),
   (128643, (true, 790300273, 44, 21085094404745), (true, 1188962113, 72, 33940820730655)),
   (128681, (true, 8292105721, 71, 752773425976440), (false, 150996319, 40, 1719916969329)),
   (128715, (false, 39902415, 38, 1146699626625), (true, 1011090904680685921, 2708178, 364109262349067773063958231)),
   (128785, (false, 848856335, 82, 4988231414175), (true, 159588265225, 951, 43583042342145700))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 5 := rfl

end Rank2Cert640

#print axioms Rank2Cert640.on_curve
#print axioms Rank2Cert640.indep
#print axioms Rank2Cert640.count

bsd/Rank2Cert641.lean

lean_all/bsd/Rank2Cert641.lean

/-
  bsd/Rank2Cert641.lean — T39 cycle 1474 (goal G835): rank ≥ 2 for 3 of the 11 congruent curves in 128801–129000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert641
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (128841, (false, 38913600, 37, 1073181459240), (true, 146103715751569, 33197, 416213946488040422680)),
   (128874, (true, 2506188, 1, 3962283228), (false, 6125087, 8, 13670668207)),
   (128945, (true, 485809, 1, 326463648), (true, 34768771215025, 783, 205013703366839441800))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert641

#print axioms Rank2Cert641.on_curve
#print axioms Rank2Cert641.indep
#print axioms Rank2Cert641.count

bsd/Rank2Cert642.lean

lean_all/bsd/Rank2Cert642.lean

/-
  bsd/Rank2Cert642.lean — T39 cycle 1476 (goal G835): rank ≥ 2 for 3 of the 5 congruent curves in 129001–129200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert642
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (129010, (true, 159790, 1, 37688400), (true, 202726314, 25, 2648326465512)),
   (129090, (false, 123210, 1, 13519800), (false, 266240, 3, 583523200)),
   (129129, (false, 8578479, 32, 386467520985), (true, 764841, 2, 493322445))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert642

#print axioms Rank2Cert642.on_curve
#print axioms Rank2Cert642.indep
#print axioms Rank2Cert642.count

bsd/Rank2Cert643.lean

lean_all/bsd/Rank2Cert643.lean

/-
  bsd/Rank2Cert643.lean — T39 cycle 1478 (goal G835): rank ≥ 2 for 1 of the 10 congruent curves in 129201–129400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert643
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (129370, (true, 12119130, 7, 35957958000), (false, 473545370, 79, 14230602991200))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert643

#print axioms Rank2Cert643.on_curve
#print axioms Rank2Cert643.indep
#print axioms Rank2Cert643.count

bsd/Rank2Cert644.lean

lean_all/bsd/Rank2Cert644.lean

/-
  bsd/Rank2Cert644.lean — T39 cycle 1480 (goal G835): rank ≥ 2 for 2 of the 6 congruent curves in 129401–129600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert644
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (129426, (true, 542124, 1, 387618660), (true, 137853, 1, 17620857)),
   (129506, (true, 1211913, 2, 1206134853), (true, 1501880344318201, 33652, 57925849738582605564045))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert644

#print axioms Rank2Cert644.on_curve
#print axioms Rank2Cert644.indep
#print axioms Rank2Cert644.count

bsd/Rank2Cert645.lean

lean_all/bsd/Rank2Cert645.lean

/-
  bsd/Rank2Cert645.lean — T39 cycle 1482 (goal G835): rank ≥ 2 for 2 of the 5 congruent curves in 129601–129800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert645
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (129642, (true, 629442, 1, 488675880), (false, 115153871, 400, 222586224663383)),
   (129721, (false, 1540081, 5, 3541823928), (false, 313099993, 239, 130996460459424))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert645

#print axioms Rank2Cert645.on_curve
#print axioms Rank2Cert645.indep
#print axioms Rank2Cert645.count

bsd/Rank2Cert646.lean

lean_all/bsd/Rank2Cert646.lean

/-
  bsd/Rank2Cert646.lean — T39 cycle 1484 (goal G835): rank ≥ 2 for 2 of the 10 congruent curves in 129801–130000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert646
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (129890, (true, 46909510, 19, 9196193400), (true, 193430642309, 1033, 59340271851839277)),
   (129955, (true, 2973244, 3, 4713465322), (true, 3224529, 4, 4425649767))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert646

#print axioms Rank2Cert646.on_curve
#print axioms Rank2Cert646.indep
#print axioms Rank2Cert646.count

bsd/Rank2Cert647.lean

lean_all/bsd/Rank2Cert647.lean

/-
  bsd/Rank2Cert647.lean — T39 cycle 1486 (goal G836): rank ≥ 2 for 3 of the 10 congruent curves in 130001–130200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert647
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (130011, (true, 222789, 1, 85395240), (true, 4487163, 1, 9501133032)),
   (130065, (true, 142129, 1, 21603608), (true, 111716865, 29, 240014864700)),
   (130074, (true, 409374, 1, 248353560), (false, 38481954, 65, 3400779166344))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert647

#print axioms Rank2Cert647.on_curve
#print axioms Rank2Cert647.indep
#print axioms Rank2Cert647.count

bsd/Rank2Cert648.lean

lean_all/bsd/Rank2Cert648.lean

/-
  bsd/Rank2Cert648.lean — T39 cycle 1488 (goal G836): rank ≥ 2 for 4 of the 4 congruent curves in 130201–130400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert648
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (130219, (true, 257399307, 31, 3608729654376), (true, 1467838231390129, 105772, 6867205075157976449175)),
   (130226, (true, 74337196, 15, 589039297244), (false, 1222174, 5, 3335950176)),
   (130305, (true, 133225, 1, 10125100), (true, 66585855, 1, 543340576800)),
   (130385, (false, 5776, 1, 9899532), (true, 1365599318387137201, 444661, 1595539404275731202230236876))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert648

#print axioms Rank2Cert648.on_curve
#print axioms Rank2Cert648.indep
#print axioms Rank2Cert648.count

bsd/Rank2Cert649.lean

lean_all/bsd/Rank2Cert649.lean

/-
  bsd/Rank2Cert649.lean — T39 cycle 1490 (goal G836): rank ≥ 2 for 2 of the 10 congruent curves in 130401–130600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert649
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (130435, (true, 12312475, 9, 22184785700), (true, 1370698456473025, 76956, 41921210727552406327775)),
   (130515, (false, 539, 9, 245436884), (true, 766140, 1, 660795750))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert649

#print axioms Rank2Cert649.on_curve
#print axioms Rank2Cert649.indep
#print axioms Rank2Cert649.count

bsd/Rank2Cert65.lean

lean_all/bsd/Rank2Cert65.lean

/-
  bsd/Rank2Cert65.lean — T39 cycle 408 (goal G126): rank ≥ 2 for 6 of the 8 congruent curves in 13601–13800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert65
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (13601, (true, 125692, 3, 10118206), (true, 6800146228681, 21950, 4737307130346720579)),
   (13706, (true, 2371842, 7, 3503330424), (true, 21142, 1, 2340624)),
   (13715, (true, 62465, 2, 7465575), (true, 363348597507025, 104636, 6306912490110424335975)),
   (13746, (true, 37446, 1, 6740280), (false, 8686602, 61, 148561364952)),
   (13785, (false, 257415, 17, 2017033200), (true, 65691025, 41, 498199923100)),
   (13795, (true, 19845, 1, 2009700), (true, 51005, 1, 11089800))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 6 := rfl

end Rank2Cert65

#print axioms Rank2Cert65.on_curve
#print axioms Rank2Cert65.indep
#print axioms Rank2Cert65.count

bsd/Rank2Cert650.lean

lean_all/bsd/Rank2Cert650.lean

/-
  bsd/Rank2Cert650.lean — T39 cycle 1492 (goal G836): rank ≥ 2 for 1 of the 10 congruent curves in 130601–130800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert650
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (130730, (false, 106904330, 31, 682305764400), (true, 1727943769, 114, 13099073704003))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert650

#print axioms Rank2Cert650.on_curve
#print axioms Rank2Cert650.indep
#print axioms Rank2Cert650.count

bsd/Rank2Cert651.lean

lean_all/bsd/Rank2Cert651.lean

/-
  bsd/Rank2Cert651.lean — T39 cycle 1494 (goal G836): rank ≥ 2 for 1 of the 5 congruent curves in 130801–131000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert651
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (130913, (false, 150553143, 83, 10910547669840), (true, 37847757025, 207, 7281771067043720))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert651

#print axioms Rank2Cert651.on_curve
#print axioms Rank2Cert651.indep
#print axioms Rank2Cert651.count

bsd/Rank2Cert652.lean

lean_all/bsd/Rank2Cert652.lean

/-
  bsd/Rank2Cert652.lean — T39 cycle 1496 (goal G836): rank ≥ 2 for 1 of the 2 congruent curves in 131001–131200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert652
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (131019, (true, 2828469, 1, 4751827920), (true, 176249931, 1, 2339878535280))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert652

#print axioms Rank2Cert652.on_curve
#print axioms Rank2Cert652.indep
#print axioms Rank2Cert652.count

bsd/Rank2Cert653.lean

lean_all/bsd/Rank2Cert653.lean

/-
  bsd/Rank2Cert653.lean — T39 cycle 1498 (goal G836): rank ≥ 2 for 3 of the 8 congruent curves in 131201–131400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert653
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (131306, (true, 537993, 2, 85462917), (true, 51947982241, 556, 7388755063511025)),
   (131345, (true, 1221025, 2, 1217947575), (false, 8100, 1, 11798550)),
   (131370, (true, 3420150, 1, 6320437200), (true, 110949978570, 617, 32989159155888000))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert653

#print axioms Rank2Cert653.on_curve
#print axioms Rank2Cert653.indep
#print axioms Rank2Cert653.count

bsd/Rank2Cert654.lean

lean_all/bsd/Rank2Cert654.lean

/-
  bsd/Rank2Cert654.lean — T39 cycle 1500 (goal G836): rank ≥ 2 for 0 of the 8 congruent curves in 131401–131600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert654
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert654

#print axioms Rank2Cert654.on_curve
#print axioms Rank2Cert654.indep
#print axioms Rank2Cert654.count

bsd/Rank2Cert655.lean

lean_all/bsd/Rank2Cert655.lean

/-
  bsd/Rank2Cert655.lean — T39 cycle 1502 (goal G836): rank ≥ 2 for 0 of the 2 congruent curves in 131601–131800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert655
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert655

#print axioms Rank2Cert655.on_curve
#print axioms Rank2Cert655.indep
#print axioms Rank2Cert655.count

bsd/Rank2Cert656.lean

lean_all/bsd/Rank2Cert656.lean

/-
  bsd/Rank2Cert656.lean — T39 cycle 1504 (goal G836): rank ≥ 2 for 1 of the 5 congruent curves in 131801–132000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert656
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (131835, (true, 132540, 1, 4970250), (true, 154275, 1, 31472100))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert656

#print axioms Rank2Cert656.on_curve
#print axioms Rank2Cert656.indep
#print axioms Rank2Cert656.count

bsd/Rank2Cert657.lean

lean_all/bsd/Rank2Cert657.lean

/-
  bsd/Rank2Cert657.lean — T39 cycle 1506 (goal G837): rank ≥ 2 for 1 of the 6 congruent curves in 132001–132200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert657
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (132009, (false, 2752281, 5, 3021218172), (false, 1605831, 146, 3565817407845))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert657

#print axioms Rank2Cert657.on_curve
#print axioms Rank2Cert657.indep
#print axioms Rank2Cert657.count

bsd/Rank2Cert658.lean

lean_all/bsd/Rank2Cert658.lean

/-
  bsd/Rank2Cert658.lean — T39 cycle 1508 (goal G837): rank ≥ 2 for 3 of the 8 congruent curves in 132201–132400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert658
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (132314, (true, 3211117, 3, 5343886639), (true, 132678, 1, 3577392)),
   (132379, (true, 132613, 1, 2867592), (true, 39328497, 2, 246616231329)),
   (132385, (true, 2878975, 3, 4446955700), (true, 330587356, 1, 6010759116054))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert658

#print axioms Rank2Cert658.on_curve
#print axioms Rank2Cert658.indep
#print axioms Rank2Cert658.count

bsd/Rank2Cert659.lean

lean_all/bsd/Rank2Cert659.lean

/-
  bsd/Rank2Cert659.lean — T39 cycle 1510 (goal G837): rank ≥ 2 for 2 of the 4 congruent curves in 132401–132600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert659
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (132465, (true, 1759383025, 37, 73404262567900), (true, 60842940, 11, 457822429650)),
   (132553, (false, 93025, 1, 28800540), (true, 9878866074475225, 260563, 404983044397572088855860))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert659

#print axioms Rank2Cert659.on_curve
#print axioms Rank2Cert659.indep
#print axioms Rank2Cert659.count

bsd/Rank2Cert66.lean

lean_all/bsd/Rank2Cert66.lean

/-
  bsd/Rank2Cert66.lean — T39 cycle 411 (goal G128): rank ≥ 2 for 4 of the 6 congruent curves in 13801–14000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert66
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (13801, (true, 65499653041, 2178, 524507102717185), (true, 1419254929, 186, 50351160052655)),
   (13890, (true, 1286214, 5, 1404545688), (false, 32076810, 49, 51689334600)),
   (13921, (true, 332813795761825921, 4832760, 41011205518078176682406719), (true, 41180982052081, 52969, 83748794978187807600)),
   (13930, (true, 2673566, 11, 3393360936), (true, 25270, 1, 3351600))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert66

#print axioms Rank2Cert66.on_curve
#print axioms Rank2Cert66.indep
#print axioms Rank2Cert66.count

bsd/Rank2Cert660.lean

lean_all/bsd/Rank2Cert660.lean

/-
  bsd/Rank2Cert660.lean — T39 cycle 1512 (goal G837): rank ≥ 2 for 2 of the 4 congruent curves in 132601–132800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert660
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (132634, (true, 146412, 1, 23725716), (true, 58768150, 3, 450425967040)),
   (132641, (true, 1103103, 1, 1150168728), (true, 8488513, 1, 24728307912))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert660

#print axioms Rank2Cert660.on_curve
#print axioms Rank2Cert660.indep
#print axioms Rank2Cert660.count

bsd/Rank2Cert661.lean

lean_all/bsd/Rank2Cert661.lean

/-
  bsd/Rank2Cert661.lean — T39 cycle 1514 (goal G837): rank ≥ 2 for 1 of the 5 congruent curves in 132801–133000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert661
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (132810, (true, 346560, 1, 188442000), (false, 14881275, 119, 7254895731075))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert661

#print axioms Rank2Cert661.on_curve
#print axioms Rank2Cert661.indep
#print axioms Rank2Cert661.count

bsd/Rank2Cert662.lean

lean_all/bsd/Rank2Cert662.lean

/-
  bsd/Rank2Cert662.lean — T39 cycle 1516 (goal G837): rank ≥ 2 for 5 of the 8 congruent curves in 133001–133200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert662
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (133081, (true, 1509421102225, 3266, 630362285621998545), (true, 744285971376025, 45528, 18859233268178985834685)),
   (133114, (false, 1922, 1, 5835192), (false, 7595775, 8, 10632258945)),
   (133115, (false, 5217871, 8, 15383359767), (true, 4566640792455625, 166258, 182773815900597786895875)),
   (133161, (true, 1266993, 2, 1293989697), (false, 18813968, 13, 53559700676)),
   (133185, (false, 65559, 1, 29683836), (true, 1768872419941225, 83359, 63400522814576660689300))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 5 := rfl

end Rank2Cert662

#print axioms Rank2Cert662.on_curve
#print axioms Rank2Cert662.indep
#print axioms Rank2Cert662.count

bsd/Rank2Cert663.lean

lean_all/bsd/Rank2Cert663.lean

/-
  bsd/Rank2Cert663.lean — T39 cycle 1518 (goal G837): rank ≥ 2 for 2 of the 7 congruent curves in 133201–133400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert663
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (133297, (false, 26520017, 33, 734959911320), (true, 4854623429041, 3140, 10296867080399725161)),
   (133315, (true, 16237900315, 237, 1836010548499400), (true, 10134322068025, 1308, 32253885135791836525))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert663

#print axioms Rank2Cert663.on_curve
#print axioms Rank2Cert663.indep
#print axioms Rank2Cert663.count

bsd/Rank2Cert664.lean

lean_all/bsd/Rank2Cert664.lean

/-
  bsd/Rank2Cert664.lean — T39 cycle 1520 (goal G837): rank ≥ 2 for 2 of the 7 congruent curves in 133401–133600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert664
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (133529, (true, 290393536, 45, 1804593175784), (false, 87445391, 90, 10081032413023)),
   (133586, (false, 578, 3, 28904624), (false, 2011712, 9, 15079617712))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert664

#print axioms Rank2Cert664.on_curve
#print axioms Rank2Cert664.indep
#print axioms Rank2Cert664.count

bsd/Rank2Cert665.lean

lean_all/bsd/Rank2Cert665.lean

/-
  bsd/Rank2Cert665.lean — T39 cycle 1522 (goal G837): rank ≥ 2 for 0 of the 5 congruent curves in 133601–133800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert665
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert665

#print axioms Rank2Cert665.on_curve
#print axioms Rank2Cert665.indep
#print axioms Rank2Cert665.count

bsd/Rank2Cert666.lean

lean_all/bsd/Rank2Cert666.lean

/-
  bsd/Rank2Cert666.lean — T39 cycle 1524 (goal G837): rank ≥ 2 for 4 of the 9 congruent curves in 133801–134000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert666
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (133835, (true, 707281, 2, 388736271), (true, 29214131440, 359, 4030067428658700)),
   (133841, (false, 419033, 3, 731049776), (true, 11347763267846168641, 5479449, 35749467383499255294776913920)),
   (133889, (true, 2664823, 3, 3879982288), (false, 113432193, 47, 2909106810456)),
   (133986, (true, 2472850, 3, 3394936720), (true, 2652989793, 2, 136648025205903))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert666

#print axioms Rank2Cert666.on_curve
#print axioms Rank2Cert666.indep
#print axioms Rank2Cert666.count

bsd/Rank2Cert667.lean

lean_all/bsd/Rank2Cert667.lean

/-
  bsd/Rank2Cert667.lean — T39 cycle 1526 (goal G839): rank ≥ 2 for 5 of the 8 congruent curves in 134001–134200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert667
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (134090, (true, 15634894, 7, 56097999672), (false, 2915, 117, 99103135175)),
   (134106, (false, 7098, 1, 11282544), (true, 101029434, 17, 937788928560)),
   (134130, (false, 20816670, 13, 40936618800), (true, 1346434, 3, 691972352)),
   (134139, (true, 1187836225, 38, 40390810876295), (true, 462549027344209, 58588, 949046338251086523175)),
   (134178, (true, 6301977, 4, 14874035715), (true, 834537, 2, 583796565))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 5 := rfl

end Rank2Cert667

#print axioms Rank2Cert667.on_curve
#print axioms Rank2Cert667.indep
#print axioms Rank2Cert667.count

bsd/Rank2Cert668.lean

lean_all/bsd/Rank2Cert668.lean

/-
  bsd/Rank2Cert668.lean — T39 cycle 1528 (goal G839): rank ≥ 2 for 3 of the 5 congruent curves in 134201–134400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert668
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (134249, (true, 3433609, 4, 4963770075), (false, 799236, 5, 2914147662)),
   (134274, (true, 347778, 1, 189191232), (false, 234537086, 65, 7910853739712)),
   (134386, (false, 2494863, 8, 13000823031), (false, 142100, 11, 6129426660))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert668

#print axioms Rank2Cert668.on_curve
#print axioms Rank2Cert668.indep
#print axioms Rank2Cert668.count

bsd/Rank2Cert669.lean

lean_all/bsd/Rank2Cert669.lean

/-
  bsd/Rank2Cert669.lean — T39 cycle 1530 (goal G839): rank ≥ 2 for 1 of the 5 congruent curves in 134401–134600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert669
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (134545, (true, 819025, 2, 558733425), (false, 747631775, 358, 471052070925975))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert669

#print axioms Rank2Cert669.on_curve
#print axioms Rank2Cert669.indep
#print axioms Rank2Cert669.count

bsd/Rank2Cert67.lean

lean_all/bsd/Rank2Cert67.lean

/-
  bsd/Rank2Cert67.lean — T39 cycle 414 (goal G130): rank ≥ 2 for 5 of the 8 congruent curves in 14001–14200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert67
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (14035, (true, 53235, 1, 11848200), (false, 3428915, 17, 4012159200)),
   (14091, (true, 226043125, 107, 2380469191100), (true, 2578653, 13, 1588450248)),
   (14154, (true, 4769898, 1, 10417457232), (true, 53913589249, 1712, 7995348117332225)),
   (14170, (true, 16250, 1, 1014000), (true, 727481055625, 1802, 619244403491701125)),
   (14186, (true, 1116225, 4, 1154670495), (true, 49916802485869225, 1366676, 9451551419619485818276245))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 5 := rfl

end Rank2Cert67

#print axioms Rank2Cert67.on_curve
#print axioms Rank2Cert67.indep
#print axioms Rank2Cert67.count

bsd/Rank2Cert670.lean

lean_all/bsd/Rank2Cert670.lean

/-
  bsd/Rank2Cert670.lean — T39 cycle 1532 (goal G839): rank ≥ 2 for 1 of the 4 congruent curves in 134601–134800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert670
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (134745, (true, 135759, 1, 6102252), (true, 35376622230625, 7133, 206424974040340978000))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert670

#print axioms Rank2Cert670.on_curve
#print axioms Rank2Cert670.indep
#print axioms Rank2Cert670.count

bsd/Rank2Cert671.lean

lean_all/bsd/Rank2Cert671.lean

/-
  bsd/Rank2Cert671.lean — T39 cycle 1534 (goal G839): rank ≥ 2 for 4 of the 6 congruent curves in 134801–135000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert671
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (134890, (false, 6544810, 7, 2361956400), (true, 4437881, 2, 9279609171)),
   (134970, (false, 3750, 1, 8262000), (false, 715310670, 169, 101309305167000)),
   (134979, (false, 885573, 7, 6168026124), (true, 623650729, 18, 15536089257715)),
   (134994, (true, 136806, 1, 8208360), (true, 6794698, 7, 4049640008))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert671

#print axioms Rank2Cert671.on_curve
#print axioms Rank2Cert671.indep
#print axioms Rank2Cert671.count

bsd/Rank2Cert672.lean

lean_all/bsd/Rank2Cert672.lean

/-
  bsd/Rank2Cert672.lean — T39 cycle 1536 (goal G839): rank ≥ 2 for 0 of the 6 congruent curves in 135001–135200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert672
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert672

#print axioms Rank2Cert672.on_curve
#print axioms Rank2Cert672.indep
#print axioms Rank2Cert672.count

bsd/Rank2Cert673.lean

lean_all/bsd/Rank2Cert673.lean

/-
  bsd/Rank2Cert673.lean — T39 cycle 1538 (goal G839): rank ≥ 2 for 1 of the 4 congruent curves in 135201–135400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert673
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (135265, (true, 744185, 2, 440773125), (true, 505459855849, 1585, 266029691943303132))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert673

#print axioms Rank2Cert673.on_curve
#print axioms Rank2Cert673.indep
#print axioms Rank2Cert673.count

bsd/Rank2Cert674.lean

lean_all/bsd/Rank2Cert674.lean

/-
  bsd/Rank2Cert674.lean — T39 cycle 1540 (goal G839): rank ≥ 2 for 0 of the 4 congruent curves in 135401–135600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert674
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert674

#print axioms Rank2Cert674.on_curve
#print axioms Rank2Cert674.indep
#print axioms Rank2Cert674.count

bsd/Rank2Cert675.lean

lean_all/bsd/Rank2Cert675.lean

/-
  bsd/Rank2Cert675.lean — T39 cycle 1542 (goal G839): rank ≥ 2 for 4 of the 7 congruent curves in 135601–135800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert675
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (135609, (false, 1127756352, 709, 2288912465092968), (false, 574248848, 69, 7071548411116)),
   (135705, (true, 2247225, 2, 3269002725), (true, 45757671634964041, 191226, 9730313358927976283982211)),
   (135713, (true, 9228484225, 79, 882792597543360), (true, 58190839096625928289, 15935775, 357668048454756497908494721712)),
   (135785, (false, 1219985, 3, 78720200), (true, 73839649, 23, 147014852868))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert675

#print axioms Rank2Cert675.on_curve
#print axioms Rank2Cert675.indep
#print axioms Rank2Cert675.count

bsd/Rank2Cert676.lean

lean_all/bsd/Rank2Cert676.lean

/-
  bsd/Rank2Cert676.lean — T39 cycle 1544 (goal G839): rank ≥ 2 for 2 of the 5 congruent curves in 135801–136000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert676
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (135826, (false, 2784546, 5, 3242917392), (true, 139380430594, 787, 41488548600405360)),
   (135915, (false, 41616, 1, 26394948), (true, 30948891, 7, 168140438136))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert676

#print axioms Rank2Cert676.on_curve
#print axioms Rank2Cert676.indep
#print axioms Rank2Cert676.count

bsd/Rank2Cert677.lean

lean_all/bsd/Rank2Cert677.lean

/-
  bsd/Rank2Cert677.lean — T39 cycle 1546 (goal G840): rank ≥ 2 for 1 of the 4 congruent curves in 136001–136200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert677
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (136034, (false, 99458, 17, 12398346864), (true, 61600278016805089, 453102, 13627236931831813899576655))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert677

#print axioms Rank2Cert677.on_curve
#print axioms Rank2Cert677.indep
#print axioms Rank2Cert677.count

bsd/Rank2Cert678.lean

lean_all/bsd/Rank2Cert678.lean

/-
  bsd/Rank2Cert678.lean — T39 cycle 1548 (goal G840): rank ≥ 2 for 2 of the 9 congruent curves in 136201–136400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert678
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (136218, (true, 212268, 1, 75004020), (false, 8882471, 10, 30779233667)),
   (136290, (false, 54150, 1, 29104200), (true, 152460, 1, 26680500))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert678

#print axioms Rank2Cert678.on_curve
#print axioms Rank2Cert678.indep
#print axioms Rank2Cert678.count

bsd/Rank2Cert679.lean

lean_all/bsd/Rank2Cert679.lean

/-
  bsd/Rank2Cert679.lean — T39 cycle 1550 (goal G840): rank ≥ 2 for 3 of the 11 congruent curves in 136401–136600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert679
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (136401, (false, 14592, 73, 87805290672), (true, 73121027281, 255, 19626584469742304)),
   (136554, (true, 247500, 1, 102692700), (true, 96152034, 25, 434253813048)),
   (136561, (true, 69692304049, 495, 16138995996792968), (true, 209306617609489, 37905, 1054352601053253103888))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert679

#print axioms Rank2Cert679.on_curve
#print axioms Rank2Cert679.indep
#print axioms Rank2Cert679.count

bsd/Rank2Cert68.lean

lean_all/bsd/Rank2Cert68.lean

/-
  bsd/Rank2Cert68.lean — T39 cycle 415 (goal G131): rank ≥ 2 for 5 of the 9 congruent curves in 14201–14400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert68
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (14209, (true, 356794321, 138, 4392652533425), (true, 223983546361, 2898, 89706990617197435)),
   (14289, (true, 10018833, 17, 28893077928), (false, 1306833, 17, 4478116104)),
   (14322, (true, 380556, 5, 79536204), (true, 61047525, 47, 407931771555)),
   (14385, (true, 18769, 1, 1651672), (true, 14415, 1, 111600)),
   (14393, (false, 603729, 65, 47247371556), (true, 16466560525801, 17885, 64154811005190143076))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 5 := rfl

end Rank2Cert68

#print axioms Rank2Cert68.on_curve
#print axioms Rank2Cert68.indep
#print axioms Rank2Cert68.count

bsd/Rank2Cert680.lean

lean_all/bsd/Rank2Cert680.lean

/-
  bsd/Rank2Cert680.lean — T39 cycle 1552 (goal G840): rank ≥ 2 for 1 of the 7 congruent curves in 136601–136800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert680
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (136619, (true, 12682012, 9, 22060359874), (false, 2171743, 4, 366081009))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert680

#print axioms Rank2Cert680.on_curve
#print axioms Rank2Cert680.indep
#print axioms Rank2Cert680.count

bsd/Rank2Cert681.lean

lean_all/bsd/Rank2Cert681.lean

/-
  bsd/Rank2Cert681.lean — T39 cycle 1554 (goal G840): rank ≥ 2 for 0 of the 3 congruent curves in 136801–137000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert681
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert681

#print axioms Rank2Cert681.on_curve
#print axioms Rank2Cert681.indep
#print axioms Rank2Cert681.count

bsd/Rank2Cert682.lean

lean_all/bsd/Rank2Cert682.lean

/-
  bsd/Rank2Cert682.lean — T39 cycle 1556 (goal G840): rank ≥ 2 for 1 of the 5 congruent curves in 137001–137200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert682
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (137170, (true, 37749184, 3, 231808301648), (true, 3772883180, 41, 231311616383100))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert682

#print axioms Rank2Cert682.on_curve
#print axioms Rank2Cert682.indep
#print axioms Rank2Cert682.count

bsd/Rank2Cert683.lean

lean_all/bsd/Rank2Cert683.lean

/-
  bsd/Rank2Cert683.lean — T39 cycle 1558 (goal G840): rank ≥ 2 for 0 of the 5 congruent curves in 137201–137400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert683
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert683

#print axioms Rank2Cert683.on_curve
#print axioms Rank2Cert683.indep
#print axioms Rank2Cert683.count

bsd/Rank2Cert684.lean

lean_all/bsd/Rank2Cert684.lean

/-
  bsd/Rank2Cert684.lean — T39 cycle 1560 (goal G840): rank ≥ 2 for 3 of the 8 congruent curves in 137401–137600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert684
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (137410, (true, 3192140, 1, 5697969900), (false, 80451287735, 5198, 1052823096873673125)),
   (137514, (true, 4615104, 5, 6614597808), (true, 289132, 1, 136759436)),
   (137555, (true, 167445, 1, 39070500), (true, 37511031390625, 998, 229739468249386655625))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert684

#print axioms Rank2Cert684.on_curve
#print axioms Rank2Cert684.indep
#print axioms Rank2Cert684.count

bsd/Rank2Cert685.lean

lean_all/bsd/Rank2Cert685.lean

/-
  bsd/Rank2Cert685.lean — T39 cycle 1562 (goal G840): rank ≥ 2 for 2 of the 8 congruent curves in 137601–137800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert685
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (137609, (true, 139159, 1, 7726440), (true, 351221593, 7, 6580998066168)),
   (137643, (true, 879307, 1, 814373560), (true, 19121025, 8, 74209790655))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert685

#print axioms Rank2Cert685.on_curve
#print axioms Rank2Cert685.indep
#print axioms Rank2Cert685.count

bsd/Rank2Cert686.lean

lean_all/bsd/Rank2Cert686.lean

/-
  bsd/Rank2Cert686.lean — T39 cycle 1564 (goal G840): rank ≥ 2 for 1 of the 7 congruent curves in 137801–138000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert686
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (137977, (true, 8474327, 7, 14873490960), (true, 10992474025, 233, 843496740251880))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert686

#print axioms Rank2Cert686.on_curve
#print axioms Rank2Cert686.indep
#print axioms Rank2Cert686.count

bsd/Rank2Cert687.lean

lean_all/bsd/Rank2Cert687.lean

/-
  bsd/Rank2Cert687.lean — T39 cycle 1566 (goal G841): rank ≥ 2 for 1 of the 3 congruent curves in 138001–138200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert687
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (138065, (false, 137641, 1, 4011252), (true, 585385, 2, 148525875))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert687

#print axioms Rank2Cert687.on_curve
#print axioms Rank2Cert687.indep
#print axioms Rank2Cert687.count

bsd/Rank2Cert688.lean

lean_all/bsd/Rank2Cert688.lean

/-
  bsd/Rank2Cert688.lean — T39 cycle 1568 (goal G841): rank ≥ 2 for 3 of the 10 congruent curves in 138201–138400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert688
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (138201, (false, 60543, 1, 30568356), (true, 41430057756975001, 528699, 3047377178954937885592780)),
   (138210, (true, 18769666, 9, 65270136736), (true, 1253104, 3, 169795592)),
   (138385, (false, 3185, 1, 7807800), (true, 559232870091025, 30044, 12890676592568566076025))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert688

#print axioms Rank2Cert688.on_curve
#print axioms Rank2Cert688.indep
#print axioms Rank2Cert688.count

bsd/Rank2Cert689.lean

lean_all/bsd/Rank2Cert689.lean

/-
  bsd/Rank2Cert689.lean — T39 cycle 1570 (goal G841): rank ≥ 2 for 2 of the 10 congruent curves in 138401–138600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert689
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (138539, (false, 96100, 1, 30934590), (false, 93775, 4, 678182505)),
   (138570, (false, 16434150, 11, 13475804400), (true, 81944893770, 583, 19195884406530000))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert689

#print axioms Rank2Cert689.on_curve
#print axioms Rank2Cert689.indep
#print axioms Rank2Cert689.count

bsd/Rank2Cert69.lean

lean_all/bsd/Rank2Cert69.lean

/-
  bsd/Rank2Cert69.lean — T39 cycle 416 (goal G132): rank ≥ 2 for 2 of the 7 congruent curves in 14401–14600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert69
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (14529, (true, 17052, 1, 1165626), (true, 204404209, 61, 2818298698460)),
   (14539, (true, 8044288, 9, 22569758288), (true, 488791867, 89, 10502298235176))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert69

#print axioms Rank2Cert69.on_curve
#print axioms Rank2Cert69.indep
#print axioms Rank2Cert69.count

bsd/Rank2Cert690.lean

lean_all/bsd/Rank2Cert690.lean

/-
  bsd/Rank2Cert690.lean — T39 cycle 1572 (goal G841): rank ≥ 2 for 0 of the 3 congruent curves in 138601–138800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert690
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert690

#print axioms Rank2Cert690.on_curve
#print axioms Rank2Cert690.indep
#print axioms Rank2Cert690.count

bsd/Rank2Cert691.lean

lean_all/bsd/Rank2Cert691.lean

/-
  bsd/Rank2Cert691.lean — T39 cycle 1574 (goal G841): rank ≥ 2 for 2 of the 5 congruent curves in 138801–139000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert691
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (138827, (false, 780868127, 174, 115407491624425), (true, 447951849106129, 47730, 6714126722289129060367)),
   (138890, (true, 140524, 1, 8009868), (true, 19016965, 9, 66861920125))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert691

#print axioms Rank2Cert691.on_curve
#print axioms Rank2Cert691.indep
#print axioms Rank2Cert691.count

bsd/Rank2Cert692.lean

lean_all/bsd/Rank2Cert692.lean

/-
  bsd/Rank2Cert692.lean — T39 cycle 1576 (goal G841): rank ≥ 2 for 1 of the 5 congruent curves in 139001–139200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert692
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (139146, (true, 147026922, 17, 1714791886416), (true, 359583720409, 1102, 190333295366336515))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert692

#print axioms Rank2Cert692.on_curve
#print axioms Rank2Cert692.indep
#print axioms Rank2Cert692.count

bsd/Rank2Cert693.lean

lean_all/bsd/Rank2Cert693.lean

/-
  bsd/Rank2Cert693.lean — T39 cycle 1578 (goal G841): rank ≥ 2 for 3 of the 6 congruent curves in 139201–139400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert693
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (139305, (true, 2063604075625, 3473, 1720949120881289000), (true, 31287211495, 293, 5113898290600300)),
   (139314, (true, 1064757, 1, 1089246411), (false, 115184244, 55, 4350677983596)),
   (139370, (true, 819025, 2, 543013575), (false, 36354873555, 1601, 67759879929767025))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert693

#print axioms Rank2Cert693.on_curve
#print axioms Rank2Cert693.indep
#print axioms Rank2Cert693.count

bsd/Rank2Cert694.lean

lean_all/bsd/Rank2Cert694.lean

/-
  bsd/Rank2Cert694.lean — T39 cycle 1580 (goal G841): rank ≥ 2 for 2 of the 8 congruent curves in 139401–139600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert694
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (139555, (true, 2157961, 2, 3062146659), (true, 25214575581, 127, 3987862001414304)),
   (139570, (false, 306, 1, 2441472), (true, 51076821708025, 3078, 364913738851215769325))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert694

#print axioms Rank2Cert694.on_curve
#print axioms Rank2Cert694.indep
#print axioms Rank2Cert694.count

bsd/Rank2Cert695.lean

lean_all/bsd/Rank2Cert695.lean

/-
  bsd/Rank2Cert695.lean — T39 cycle 1582 (goal G841): rank ≥ 2 for 0 of the 6 congruent curves in 139601–139800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert695
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert695

#print axioms Rank2Cert695.on_curve
#print axioms Rank2Cert695.indep
#print axioms Rank2Cert695.count

bsd/Rank2Cert696.lean

lean_all/bsd/Rank2Cert696.lean

/-
  bsd/Rank2Cert696.lean — T39 cycle 1584 (goal G841): rank ≥ 2 for 1 of the 8 congruent curves in 139801–140000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert696
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (139971, (false, 1296, 1, 5038740), (true, 694083, 1, 566371728))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert696

#print axioms Rank2Cert696.on_curve
#print axioms Rank2Cert696.indep
#print axioms Rank2Cert696.count

bsd/Rank2Cert697.lean

lean_all/bsd/Rank2Cert697.lean

/-
  bsd/Rank2Cert697.lean — T39 cycle 1586 (goal G842): rank ≥ 2 for 0 of the 4 congruent curves in 140001–140200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert697
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert697

#print axioms Rank2Cert697.on_curve
#print axioms Rank2Cert697.indep
#print axioms Rank2Cert697.count

bsd/Rank2Cert698.lean

lean_all/bsd/Rank2Cert698.lean

/-
  bsd/Rank2Cert698.lean — T39 cycle 1588 (goal G842): rank ≥ 2 for 1 of the 3 congruent curves in 140201–140400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert698
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (140315, (true, 269059, 1, 119082348), (false, 99082340, 113, 17807133333150))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert698

#print axioms Rank2Cert698.on_curve
#print axioms Rank2Cert698.indep
#print axioms Rank2Cert698.count

bsd/Rank2Cert699.lean

lean_all/bsd/Rank2Cert699.lean

/-
  bsd/Rank2Cert699.lean — T39 cycle 1590 (goal G842): rank ≥ 2 for 1 of the 4 congruent curves in 140401–140600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert699
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (140442, (false, 28467383, 26, 483236422235), (true, 38961708, 7, 239372778420))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert699

#print axioms Rank2Cert699.on_curve
#print axioms Rank2Cert699.indep
#print axioms Rank2Cert699.count

bsd/Rank2Cert7.lean

lean_all/bsd/Rank2Cert7.lean

/-
  bsd/Rank2Cert7.lean — T39 cycle 329 (goal G64): rank ≥ 2 for 9 of the 9 congruent curves in 2001–2200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert7
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (2035, (true, 34225, 4, 1950825), (true, 6875, 1, 544500)),
   (2059, (true, 867941, 7, 803121360), (true, 5188941, 7, 11817782460)),
   (2113, (true, 14938225, 71, 40479907440), (true, 44562899683540225, 3282528, 8086756610683950023697535)),
   (2130, (true, 2205, 1, 26775), (true, 3750, 1, 189000)),
   (2139, (true, 2883, 1, 103788), (true, 3182832, 19, 5508686484)),
   (2145, (true, 4225, 1, 236600), (true, 18876, 1, 2576574)),
   (2154, (true, 2178, 1, 15048), (true, 5312886, 47, 5447938320)),
   (2170, (true, 2205, 1, 18375), (true, 2366, 1, 45864)),
   (2195, (true, 2645, 1, 75900), (true, 59031991225, 3322, 13079567697512475))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 9 := rfl

end Rank2Cert7

#print axioms Rank2Cert7.on_curve
#print axioms Rank2Cert7.indep
#print axioms Rank2Cert7.count

bsd/Rank2Cert70.lean

lean_all/bsd/Rank2Cert70.lean

/-
  bsd/Rank2Cert70.lean — T39 cycle 417 (goal G133): rank ≥ 2 for 7 of the 8 congruent curves in 14601–14800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert70
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (14610, (false, 79350, 7, 200417400), (true, 2371690, 1, 3652402600)),
   (14649, (true, 3764793, 16, 643779603), (false, 55331072, 427, 19863467411248)),
   (14658, (true, 3669708, 1, 7029813420), (false, 262766, 5, 130930352)),
   (14705, (false, 1936, 1, 641388), (true, 170903329, 87, 1695451698008)),
   (14722, (true, 801378, 7, 312448800), (true, 3187313, 2, 5689353705)),
   (14763, (true, 8968323, 23, 13204130940), (true, 14812, 1, 146510)),
   (14794, (false, 909194, 49, 33858190080), (true, 2339173225, 344, 75034540168245))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 7 := rfl

end Rank2Cert70

#print axioms Rank2Cert70.on_curve
#print axioms Rank2Cert70.indep
#print axioms Rank2Cert70.count

bsd/Rank2Cert700.lean

lean_all/bsd/Rank2Cert700.lean

/-
  bsd/Rank2Cert700.lean — T39 cycle 1592 (goal G842): rank ≥ 2 for 1 of the 7 congruent curves in 140601–140800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert700
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (140745, (false, 21210255, 146, 13816587066975), (true, 1436318356225, 651, 1719892294290814400))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert700

#print axioms Rank2Cert700.on_curve
#print axioms Rank2Cert700.indep
#print axioms Rank2Cert700.count

bsd/Rank2Cert701.lean

lean_all/bsd/Rank2Cert701.lean

/-
  bsd/Rank2Cert701.lean — T39 cycle 1594 (goal G842): rank ≥ 2 for 0 of the 6 congruent curves in 140801–141000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert701
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert701

#print axioms Rank2Cert701.on_curve
#print axioms Rank2Cert701.indep
#print axioms Rank2Cert701.count

bsd/Rank2Cert702.lean

lean_all/bsd/Rank2Cert702.lean

/-
  bsd/Rank2Cert702.lean — T39 cycle 1596 (goal G842): rank ≥ 2 for 2 of the 6 congruent curves in 141001–141200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert702
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (141105, (true, 3949905, 1, 7845175800), (false, 226913745, 97, 19705100081400)),
   (141122, (true, 79731742609, 610, 16941364741522023), (true, 134632426246344202801, 11190390, 1548640380459994187881563992951))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert702

#print axioms Rank2Cert702.on_curve
#print axioms Rank2Cert702.indep
#print axioms Rank2Cert702.count

bsd/Rank2Cert703.lean

lean_all/bsd/Rank2Cert703.lean

/-
  bsd/Rank2Cert703.lean — T39 cycle 1598 (goal G842): rank ≥ 2 for 1 of the 8 congruent curves in 141201–141400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert703
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (141330, (true, 282534, 1, 130038552), (false, 141204, 1, 2242044))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert703

#print axioms Rank2Cert703.on_curve
#print axioms Rank2Cert703.indep
#print axioms Rank2Cert703.count

bsd/Rank2Cert704.lean

lean_all/bsd/Rank2Cert704.lean

/-
  bsd/Rank2Cert704.lean — T39 cycle 1600 (goal G842): rank ≥ 2 for 2 of the 9 congruent curves in 141401–141600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert704
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (141491, (false, 665783363, 71, 6602023999536), (true, 3848093089, 36, 238437576119665)),
   (141585, (false, 21636015, 17, 161544828600), (true, 243516025, 31, 3151548035900))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert704

#print axioms Rank2Cert704.on_curve
#print axioms Rank2Cert704.indep
#print axioms Rank2Cert704.count

bsd/Rank2Cert705.lean

lean_all/bsd/Rank2Cert705.lean

/-
  bsd/Rank2Cert705.lean — T39 cycle 1602 (goal G842): rank ≥ 2 for 2 of the 5 congruent curves in 141601–141800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert705
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (141666, (true, 212268, 1, 72830268), (false, 18354350, 19, 204503173840)),
   (141729, (false, 5000975, 16, 80363887415), (true, 297527625065543041, 1229877, 112534770574879170407384300))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert705

#print axioms Rank2Cert705.on_curve
#print axioms Rank2Cert705.indep
#print axioms Rank2Cert705.count

bsd/Rank2Cert706.lean

lean_all/bsd/Rank2Cert706.lean

/-
  bsd/Rank2Cert706.lean — T39 cycle 1604 (goal G842): rank ≥ 2 for 3 of the 6 congruent curves in 141801–142000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert706
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (141906, (false, 561912384, 167, 92863660372560), (false, 433365392, 59, 4935334831912)),
   (141954, (false, 21248850, 13, 51334446240), (true, 342592422393, 1078, 175747227929422227)),
   (141985, (true, 73565975397721, 10010, 619068219528631152981), (true, 920694798382980625, 2429258, 366145781728135657964475375))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert706

#print axioms Rank2Cert706.on_curve
#print axioms Rank2Cert706.indep
#print axioms Rank2Cert706.count

bsd/Rank2Cert707.lean

lean_all/bsd/Rank2Cert707.lean

/-
  bsd/Rank2Cert707.lean — T39 cycle 1606 (goal G843): rank ≥ 2 for 1 of the 8 congruent curves in 142001–142200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert707
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (142105, (true, 19139980345, 74, 2645775547064475), (true, 128661584582281, 26877, 879803223592981155004))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert707

#print axioms Rank2Cert707.on_curve
#print axioms Rank2Cert707.indep
#print axioms Rank2Cert707.count

bsd/Rank2Cert708.lean

lean_all/bsd/Rank2Cert708.lean

/-
  bsd/Rank2Cert708.lean — T39 cycle 1608 (goal G843): rank ≥ 2 for 2 of the 8 congruent curves in 142201–142400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert708
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (142265, (true, 1755625, 3, 1591563500), (true, 2316248147751615289, 2045275, 3406810094159665059175171788)),
   (142370, (false, 72220414, 25, 441735787584), (false, 786324230, 287, 328098756373800))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert708

#print axioms Rank2Cert708.on_curve
#print axioms Rank2Cert708.indep
#print axioms Rank2Cert708.count

bsd/Rank2Cert709.lean

lean_all/bsd/Rank2Cert709.lean

/-
  bsd/Rank2Cert709.lean — T39 cycle 1610 (goal G843): rank ≥ 2 for 1 of the 6 congruent curves in 142401–142600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert709
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (142499, (false, 42602623, 1124, 1175066909436609), (true, 580673662396409590009, 12162446, 13983370062622386069613663672635))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert709

#print axioms Rank2Cert709.on_curve
#print axioms Rank2Cert709.indep
#print axioms Rank2Cert709.count

bsd/Rank2Cert71.lean

lean_all/bsd/Rank2Cert71.lean

/-
  bsd/Rank2Cert71.lean — T39 cycle 419 (goal G134): rank ≥ 2 for 4 of the 8 congruent curves in 14801–15000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert71
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (14818, (true, 310000, 1, 172403400), (true, 54434078, 23, 397425701520)),
   (14835, (true, 31740, 1, 4999050), (true, 3189525, 13, 3521235600)),
   (14890, (true, 22090, 1, 2425200), (true, 6363284329418809, 46296, 507594060488510800471277)),
   (14915, (true, 468331, 1, 320338404), (true, 13934103812268025, 679684, 1429649864608258341955725))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert71

#print axioms Rank2Cert71.on_curve
#print axioms Rank2Cert71.indep
#print axioms Rank2Cert71.count

bsd/Rank2Cert710.lean

lean_all/bsd/Rank2Cert710.lean

/-
  bsd/Rank2Cert710.lean — T39 cycle 1612 (goal G843): rank ≥ 2 for 1 of the 5 congruent curves in 142601–142800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert710
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (142715, (true, 157165, 1, 26096700), (true, 111532621225, 798, 21592023194781325))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert710

#print axioms Rank2Cert710.on_curve
#print axioms Rank2Cert710.indep
#print axioms Rank2Cert710.count

bsd/Rank2Cert711.lean

lean_all/bsd/Rank2Cert711.lean

/-
  bsd/Rank2Cert711.lean — T39 cycle 1614 (goal G843): rank ≥ 2 for 1 of the 9 congruent curves in 142801–143000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert711
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (142890, (true, 3725964, 1, 7186846788), (true, 145794, 1, 11055528))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert711

#print axioms Rank2Cert711.on_curve
#print axioms Rank2Cert711.indep
#print axioms Rank2Cert711.count

bsd/Rank2Cert712.lean

lean_all/bsd/Rank2Cert712.lean

/-
  bsd/Rank2Cert712.lean — T39 cycle 1616 (goal G843): rank ≥ 2 for 2 of the 9 congruent curves in 143001–143200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert712
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (143041, (true, 65369737, 13, 491057464344), (true, 34394483231761, 12727, 149078844119063735280)),
   (143065, (false, 260, 1, 2306850), (false, 10244585, 23, 240005079600))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert712

#print axioms Rank2Cert712.on_curve
#print axioms Rank2Cert712.indep
#print axioms Rank2Cert712.count

bsd/Rank2Cert713.lean

lean_all/bsd/Rank2Cert713.lean

/-
  bsd/Rank2Cert713.lean — T39 cycle 1618 (goal G843): rank ≥ 2 for 0 of the 11 congruent curves in 143201–143400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert713
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert713

#print axioms Rank2Cert713.on_curve
#print axioms Rank2Cert713.indep
#print axioms Rank2Cert713.count

bsd/Rank2Cert714.lean

lean_all/bsd/Rank2Cert714.lean

/-
  bsd/Rank2Cert714.lean — T39 cycle 1620 (goal G843): rank ≥ 2 for 0 of the 7 congruent curves in 143401–143600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert714
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert714

#print axioms Rank2Cert714.on_curve
#print axioms Rank2Cert714.indep
#print axioms Rank2Cert714.count

bsd/Rank2Cert715.lean

lean_all/bsd/Rank2Cert715.lean

/-
  bsd/Rank2Cert715.lean — T39 cycle 1622 (goal G843): rank ≥ 2 for 3 of the 10 congruent curves in 143601–143800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert715
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (143715, (true, 59530185, 8, 453794494725), (false, 27885, 41, 40341787200)),
   (143786, (true, 209083833, 38, 356274618483), (true, 829470701084089, 34268, 23389016111435335234515)),
   (143787, (true, 20790525, 1, 94795516620), (false, 103664687, 32, 1064578552345))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert715

#print axioms Rank2Cert715.on_curve
#print axioms Rank2Cert715.indep
#print axioms Rank2Cert715.count

bsd/Rank2Cert716.lean

lean_all/bsd/Rank2Cert716.lean

/-
  bsd/Rank2Cert716.lean — T39 cycle 1624 (goal G843): rank ≥ 2 for 0 of the 4 congruent curves in 143801–144000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert716
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert716

#print axioms Rank2Cert716.on_curve
#print axioms Rank2Cert716.indep
#print axioms Rank2Cert716.count

bsd/Rank2Cert717.lean

lean_all/bsd/Rank2Cert717.lean

/-
  bsd/Rank2Cert717.lean — T39 cycle 1626 (goal G844): rank ≥ 2 for 4 of the 8 congruent curves in 144001–144200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert717
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (144105, (true, 471900, 1, 308686950), (true, 186783874225, 1043, 43889899553086600)),
   (144129, (true, 1133335761, 71, 29282187258360), (true, 334964295121, 57, 193864080967895800)),
   (144155, (true, 216237025, 38, 860957383575), (true, 893561855175025, 6132, 26710310773226672540425)),
   (144194, (false, 82944, 1, 33969600), (false, 4802, 1, 9986592))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert717

#print axioms Rank2Cert717.on_curve
#print axioms Rank2Cert717.indep
#print axioms Rank2Cert717.count

bsd/Rank2Cert718.lean

lean_all/bsd/Rank2Cert718.lean

/-
  bsd/Rank2Cert718.lean — T39 cycle 1628 (goal G844): rank ≥ 2 for 1 of the 10 congruent curves in 144201–144400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert718
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (144337, (false, 47, 12, 142491545), (true, 30823069225, 462, 169940234660365))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert718

#print axioms Rank2Cert718.on_curve
#print axioms Rank2Cert718.indep
#print axioms Rank2Cert718.count

bsd/Rank2Cert719.lean

lean_all/bsd/Rank2Cert719.lean

/-
  bsd/Rank2Cert719.lean — T39 cycle 1630 (goal G844): rank ≥ 2 for 3 of the 8 congruent curves in 144401–144600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert719
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (144449, (true, 75181888221001, 15345, 581355073025556035876), (true, 25672050625, 101, 4106523019470300)),
   (144571, (false, 204687, 2, 244691937), (false, 193819, 17, 18393845280)),
   (144578, (true, 185150, 1, 49768320), (false, 33179202, 97, 7833384263040))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert719

#print axioms Rank2Cert719.on_curve
#print axioms Rank2Cert719.indep
#print axioms Rank2Cert719.count

bsd/Rank2Cert72.lean

lean_all/bsd/Rank2Cert72.lean

/-
  bsd/Rank2Cert72.lean — T39 cycle 420 (goal G135): rank ≥ 2 for 6 of the 9 congruent curves in 15001–15200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert72
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (15035, (true, 1264924, 9, 384555682), (false, 1541599, 16, 4378853199)),
   (15067, (true, 1247389, 5, 1328129088), (true, 1130901099250129, 161130, 35683283508859377509617)),
   (15105, (true, 811801, 7, 300461876), (true, 1655280, 7, 1904888700)),
   (15106, (false, 623294, 15, 2637851104), (true, 103194, 1, 32792760)),
   (15169, (true, 970225, 3, 946163420), (true, 4888760668, 371, 309079384907238)),
   (15170, (true, 1413721, 4, 1655954781), (true, 243089, 4, 6601287))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 6 := rfl

end Rank2Cert72

#print axioms Rank2Cert72.on_curve
#print axioms Rank2Cert72.indep
#print axioms Rank2Cert72.count

bsd/Rank2Cert720.lean

lean_all/bsd/Rank2Cert720.lean

/-
  bsd/Rank2Cert720.lean — T39 cycle 1632 (goal G844): rank ≥ 2 for 2 of the 7 congruent curves in 144601–144800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert720
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (144690, (true, 1376410, 3, 523035800), (true, 1022476, 1, 1023498476)),
   (144705, (false, 28274895, 14, 11822810175), (true, 1350302100625, 1927, 1439497330073189000))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert720

#print axioms Rank2Cert720.on_curve
#print axioms Rank2Cert720.indep
#print axioms Rank2Cert720.count

bsd/Rank2Cert721.lean

lean_all/bsd/Rank2Cert721.lean

/-
  bsd/Rank2Cert721.lean — T39 cycle 1634 (goal G844): rank ≥ 2 for 4 of the 7 congruent curves in 144801–145000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert721
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (144809, (true, 159607, 1, 26813976), (false, 1526623, 4, 2153450481)),
   (144826, (true, 1133638, 1, 1197121728), (true, 7105725, 7, 966220695)),
   (144969, (true, 21999132, 11, 62271242406), (false, 5848953, 7, 9748884816)),
   (144970, (true, 178605, 1, 44088975), (true, 983725, 1, 965034225))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert721

#print axioms Rank2Cert721.on_curve
#print axioms Rank2Cert721.indep
#print axioms Rank2Cert721.count

bsd/Rank2Cert722.lean

lean_all/bsd/Rank2Cert722.lean

/-
  bsd/Rank2Cert722.lean — T39 cycle 1636 (goal G844): rank ≥ 2 for 1 of the 4 congruent curves in 145001–145200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert722
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (145010, (true, 24902418025, 196, 3830137941561525), (false, 14628540560, 2913, 148815692491481000))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert722

#print axioms Rank2Cert722.on_curve
#print axioms Rank2Cert722.indep
#print axioms Rank2Cert722.count

bsd/Rank2Cert723.lean

lean_all/bsd/Rank2Cert723.lean

/-
  bsd/Rank2Cert723.lean — T39 cycle 1638 (goal G844): rank ≥ 2 for 1 of the 7 congruent curves in 145201–145400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert723
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (145299, (true, 734349, 1, 616853160), (true, 583737, 2, 41568219))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert723

#print axioms Rank2Cert723.on_curve
#print axioms Rank2Cert723.indep
#print axioms Rank2Cert723.count

bsd/Rank2Cert724.lean

lean_all/bsd/Rank2Cert724.lean

/-
  bsd/Rank2Cert724.lean — T39 cycle 1640 (goal G844): rank ≥ 2 for 3 of the 6 congruent curves in 145401–145600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert724
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (145410, (true, 171610, 1, 37754200), (false, 8003840, 9, 24445959200)),
   (145435, (true, 721458665, 4, 19378266388875), (true, 36920510250625, 14476, 126628116841789538625)),
   (145515, (true, 329725, 1, 169898300), (true, 14521525, 1, 55334569400))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert724

#print axioms Rank2Cert724.on_curve
#print axioms Rank2Cert724.indep
#print axioms Rank2Cert724.count

bsd/Rank2Cert725.lean

lean_all/bsd/Rank2Cert725.lean

/-
  bsd/Rank2Cert725.lean — T39 cycle 1642 (goal G844): rank ≥ 2 for 2 of the 9 congruent curves in 145601–145800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert725
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (145635, (false, 5357259, 11, 38856532464), (false, 12635, 13, 2766559600)),
   (145761, (false, 52983, 1, 31256316), (true, 71990457, 2, 610798767579))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert725

#print axioms Rank2Cert725.on_curve
#print axioms Rank2Cert725.indep
#print axioms Rank2Cert725.count

bsd/Rank2Cert726.lean

lean_all/bsd/Rank2Cert726.lean

/-
  bsd/Rank2Cert726.lean — T39 cycle 1644 (goal G844): rank ≥ 2 for 1 of the 6 congruent curves in 145801–146000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert726
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (145977, (true, 151355100, 7, 1859987688390), (true, 180699454921132225, 1043059, 36638629210150720559738360))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert726

#print axioms Rank2Cert726.on_curve
#print axioms Rank2Cert726.indep
#print axioms Rank2Cert726.count

bsd/Rank2Cert727.lean

lean_all/bsd/Rank2Cert727.lean

/-
  bsd/Rank2Cert727.lean — T39 cycle 1646 (goal G845): rank ≥ 2 for 2 of the 6 congruent curves in 146001–146200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert727
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (146010, (true, 246490, 1, 98596000), (false, 576975, 218, 5270770233225)),
   (146049, (false, 2692881, 41, 402854997600), (false, 2278400, 17, 63617669920))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert727

#print axioms Rank2Cert727.on_curve
#print axioms Rank2Cert727.indep
#print axioms Rank2Cert727.count

bsd/Rank2Cert728.lean

lean_all/bsd/Rank2Cert728.lean

/-
  bsd/Rank2Cert728.lean — T39 cycle 1648 (goal G845): rank ≥ 2 for 1 of the 4 congruent curves in 146201–146400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert728
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (146305, (false, 17661145, 11, 5107056600), (true, 84730477225, 682, 14694620613787725))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert728

#print axioms Rank2Cert728.on_curve
#print axioms Rank2Cert728.indep
#print axioms Rank2Cert728.count

bsd/Rank2Cert729.lean

lean_all/bsd/Rank2Cert729.lean

/-
  bsd/Rank2Cert729.lean — T39 cycle 1650 (goal G845): rank ≥ 2 for 1 of the 9 congruent curves in 146401–146600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert729
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (146595, (true, 16467505, 8, 54919129175), (false, 661680291, 463, 808181581240152))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert729

#print axioms Rank2Cert729.on_curve
#print axioms Rank2Cert729.indep
#print axioms Rank2Cert729.count

bsd/Rank2Cert73.lean

lean_all/bsd/Rank2Cert73.lean

/-
  bsd/Rank2Cert73.lean — T39 cycle 421 (goal G136): rank ≥ 2 for 3 of the 9 congruent curves in 15201–15400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert73
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (15249, (true, 142129, 2, 48397375), (true, 80937, 2, 15135219)),
   (15281, (true, 1159543, 1, 1248511572), (true, 16343488, 9, 65882193832)),
   (15378, (false, 1776192, 17, 5429405520), (false, 50762514, 65, 288930273984))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert73

#print axioms Rank2Cert73.on_curve
#print axioms Rank2Cert73.indep
#print axioms Rank2Cert73.count

bsd/Rank2Cert730.lean

lean_all/bsd/Rank2Cert730.lean

/-
  bsd/Rank2Cert730.lean — T39 cycle 1652 (goal G845): rank ≥ 2 for 1 of the 7 congruent curves in 146601–146800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert730
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (146705, (false, 92416, 1, 34636848), (false, 110889, 1, 31985316))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert730

#print axioms Rank2Cert730.on_curve
#print axioms Rank2Cert730.indep
#print axioms Rank2Cert730.count

bsd/Rank2Cert731.lean

lean_all/bsd/Rank2Cert731.lean

/-
  bsd/Rank2Cert731.lean — T39 cycle 1654 (goal G845): rank ≥ 2 for 2 of the 5 congruent curves in 146801–147000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert731
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (146890, (true, 18741610, 9, 62688152800), (true, 117711415754086729, 860574, 15426966071571588269253467)),
   (146985, (true, 180625, 1, 44616500), (false, 3592256, 5, 1466356472))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert731

#print axioms Rank2Cert731.on_curve
#print axioms Rank2Cert731.indep
#print axioms Rank2Cert731.count

bsd/Rank2Cert732.lean

lean_all/bsd/Rank2Cert732.lean

/-
  bsd/Rank2Cert732.lean — T39 cycle 1656 (goal G845): rank ≥ 2 for 1 of the 6 congruent curves in 147001–147200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert732
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (147010, (true, 92341801, 6, 885896657099), (true, 896598778321, 1230, 822444110401562119))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert732

#print axioms Rank2Cert732.on_curve
#print axioms Rank2Cert732.indep
#print axioms Rank2Cert732.count

bsd/Rank2Cert733.lean

lean_all/bsd/Rank2Cert733.lean

/-
  bsd/Rank2Cert733.lean — T39 cycle 1658 (goal G845): rank ≥ 2 for 2 of the 9 congruent curves in 147201–147400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert733
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (147259, (true, 1330672, 3, 137391884), (true, 33092509, 7, 185787711780)),
   (147371, (false, 1240687, 4, 2233439943), (true, 2842127584430488561, 1822, 4791432281827831229938917855))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert733

#print axioms Rank2Cert733.on_curve
#print axioms Rank2Cert733.indep
#print axioms Rank2Cert733.count

bsd/Rank2Cert734.lean

lean_all/bsd/Rank2Cert734.lean

/-
  bsd/Rank2Cert734.lean — T39 cycle 1660 (goal G845): rank ≥ 2 for 1 of the 4 congruent curves in 147401–147600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert734
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (147435, (false, 44100, 1, 29543850), (true, 230640, 1, 85178700))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert734

#print axioms Rank2Cert734.on_curve
#print axioms Rank2Cert734.indep
#print axioms Rank2Cert734.count

bsd/Rank2Cert735.lean

lean_all/bsd/Rank2Cert735.lean

/-
  bsd/Rank2Cert735.lean — T39 cycle 1662 (goal G845): rank ≥ 2 for 3 of the 10 congruent curves in 147601–147800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert735
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (147611, (false, 6284288, 93, 3200423112224), (false, 473686283, 71, 12489706172664)),
   (147730, (true, 148720, 1, 6606600), (true, 1060970, 1, 1082189400)),
   (147777, (false, 203391, 5, 1663617852), (false, 2376037377, 5107, 187873321282976700))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert735

#print axioms Rank2Cert735.on_curve
#print axioms Rank2Cert735.indep
#print axioms Rank2Cert735.count

bsd/Rank2Cert736.lean

lean_all/bsd/Rank2Cert736.lean

/-
  bsd/Rank2Cert736.lean — T39 cycle 1664 (goal G845): rank ≥ 2 for 0 of the 4 congruent curves in 147801–148000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert736
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert736

#print axioms Rank2Cert736.on_curve
#print axioms Rank2Cert736.indep
#print axioms Rank2Cert736.count

bsd/Rank2Cert737.lean

lean_all/bsd/Rank2Cert737.lean

/-
  bsd/Rank2Cert737.lean — T39 cycle 1666 (goal G846): rank ≥ 2 for 3 of the 6 congruent curves in 148001–148200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert737
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (148155, (true, 151725, 1, 12744900), (true, 2357475, 1, 3612528900)),
   (148161, (true, 58227273, 19, 175613455368), (false, 633737, 7, 5757361532)),
   (148170, (false, 772475, 3, 955376675), (true, 28829680, 13, 76710442600))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert737

#print axioms Rank2Cert737.on_curve
#print axioms Rank2Cert737.indep
#print axioms Rank2Cert737.count

bsd/Rank2Cert738.lean

lean_all/bsd/Rank2Cert738.lean

/-
  bsd/Rank2Cert738.lean — T39 cycle 1668 (goal G846): rank ≥ 2 for 0 of the 4 congruent curves in 148201–148400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert738
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert738

#print axioms Rank2Cert738.on_curve
#print axioms Rank2Cert738.indep
#print axioms Rank2Cert738.count

bsd/Rank2Cert739.lean

lean_all/bsd/Rank2Cert739.lean

/-
  bsd/Rank2Cert739.lean — T39 cycle 1670 (goal G846): rank ≥ 2 for 1 of the 4 congruent curves in 148401–148600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert739
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (148434, (false, 4774068, 7, 11989197324), (true, 1966822, 1, 2750472296))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert739

#print axioms Rank2Cert739.on_curve
#print axioms Rank2Cert739.indep
#print axioms Rank2Cert739.count

bsd/Rank2Cert74.lean

lean_all/bsd/Rank2Cert74.lean

/-
  bsd/Rank2Cert74.lean — T39 cycle 422 (goal G137): rank ≥ 2 for 5 of the 8 congruent curves in 15401–15600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert74
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (15465, (true, 17340, 1, 1032750), (true, 8673996877225, 19557, 18685221814698472700)),
   (15481, (false, 8281, 1, 1190280), (true, 455779801, 161, 4613912575560)),
   (15515, (true, 18125, 1, 1261500), (true, 6216689835, 437, 430910101437900)),
   (15569, (true, 121198081, 7, 1334243241120), (true, 5635173968339466769, 1934751, 13376354365834262721153070480)),
   (15585, (false, 735, 2, 1689975), (true, 8733473010025, 19311, 19264854108812533100))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 5 := rfl

end Rank2Cert74

#print axioms Rank2Cert74.on_curve
#print axioms Rank2Cert74.indep
#print axioms Rank2Cert74.count

bsd/Rank2Cert740.lean

lean_all/bsd/Rank2Cert740.lean

/-
  bsd/Rank2Cert740.lean — T39 cycle 1672 (goal G846): rank ≥ 2 for 1 of the 10 congruent curves in 148601–148800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert740
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (148610, (true, 4693374, 1, 10162719168), (false, 565543440, 97, 30411291233400))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert740

#print axioms Rank2Cert740.on_curve
#print axioms Rank2Cert740.indep
#print axioms Rank2Cert740.count

bsd/Rank2Cert741.lean

lean_all/bsd/Rank2Cert741.lean

/-
  bsd/Rank2Cert741.lean — T39 cycle 1674 (goal G846): rank ≥ 2 for 3 of the 12 congruent curves in 148801–149000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert741
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (148890, (false, 600635, 3, 928347875), (false, 165969810, 1349, 3490630637459400)),
   (148915, (true, 689585, 2, 288521025), (true, 977500225, 28, 30342779577825)),
   (148971, (true, 1906887381, 31, 83034807055200), (true, 1473306172, 49, 54859068765514))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert741

#print axioms Rank2Cert741.on_curve
#print axioms Rank2Cert741.indep
#print axioms Rank2Cert741.count

bsd/Rank2Cert742.lean

lean_all/bsd/Rank2Cert742.lean

/-
  bsd/Rank2Cert742.lean — T39 cycle 1676 (goal G846): rank ≥ 2 for 3 of the 6 congruent curves in 149001–149200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert742
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (149073, (true, 3399375, 1, 6261532200), (true, 52709272225, 339, 11444242384214740)),
   (149089, (false, 11236, 1, 15758490), (false, 2624400, 17, 69670888020)),
   (149131, (true, 257372, 1, 106416366), (false, 20108303, 12, 33803448119))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert742

#print axioms Rank2Cert742.on_curve
#print axioms Rank2Cert742.indep
#print axioms Rank2Cert742.count

bsd/Rank2Cert743.lean

lean_all/bsd/Rank2Cert743.lean

/-
  bsd/Rank2Cert743.lean — T39 cycle 1678 (goal G846): rank ≥ 2 for 2 of the 6 congruent curves in 149201–149400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert743
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (149226, (true, 917301, 1, 866849445), (false, 2101659, 5, 4468489389)),
   (149370, (true, 34325226, 5, 199910116224), (false, 41070, 1, 29104200))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert743

#print axioms Rank2Cert743.on_curve
#print axioms Rank2Cert743.indep
#print axioms Rank2Cert743.count

bsd/Rank2Cert744.lean

lean_all/bsd/Rank2Cert744.lean

/-
  bsd/Rank2Cert744.lean — T39 cycle 1680 (goal G846): rank ≥ 2 for 0 of the 5 congruent curves in 149401–149600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert744
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert744

#print axioms Rank2Cert744.on_curve
#print axioms Rank2Cert744.indep
#print axioms Rank2Cert744.count

bsd/Rank2Cert745.lean

lean_all/bsd/Rank2Cert745.lean

/-
  bsd/Rank2Cert745.lean — T39 cycle 1682 (goal G846): rank ≥ 2 for 1 of the 6 congruent curves in 149601–149800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert745
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (149739, (false, 9458975, 12, 59594626385), (true, 280224717, 43, 723956036616))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert745

#print axioms Rank2Cert745.on_curve
#print axioms Rank2Cert745.indep
#print axioms Rank2Cert745.count

bsd/Rank2Cert746.lean

lean_all/bsd/Rank2Cert746.lean

/-
  bsd/Rank2Cert746.lean — T39 cycle 1684 (goal G846): rank ≥ 2 for 1 of the 7 congruent curves in 149801–150000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert746
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (149937, (true, 141390591, 25, 1258941822264), (true, 2639406390625, 599, 4287154793624369000))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert746

#print axioms Rank2Cert746.on_curve
#print axioms Rank2Cert746.indep
#print axioms Rank2Cert746.count

bsd/Rank2Cert747.lean

lean_all/bsd/Rank2Cert747.lean

/-
  bsd/Rank2Cert747.lean — T39 cycle 1686 (goal G847): rank ≥ 2 for 0 of the 4 congruent curves in 150001–150200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert747
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert747

#print axioms Rank2Cert747.on_curve
#print axioms Rank2Cert747.indep
#print axioms Rank2Cert747.count

bsd/Rank2Cert748.lean

lean_all/bsd/Rank2Cert748.lean

/-
  bsd/Rank2Cert748.lean — T39 cycle 1688 (goal G847): rank ≥ 2 for 4 of the 11 congruent curves in 150201–150400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert748
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (150202, (false, 307448336, 275, 199098522885288), (true, 371570074479409, 38320, 5764105292514960606423)),
   (150249, (false, 67766852, 171, 36162695744702), (true, 145775336025, 742, 45828685716800085)),
   (150265, (false, 11025, 1, 15735300), (false, 12020177465, 1999, 65819083138848600)),
   (150290, (false, 10620974, 9, 19387191824), (true, 606740110, 31, 14515681628400))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert748

#print axioms Rank2Cert748.on_curve
#print axioms Rank2Cert748.indep
#print axioms Rank2Cert748.count

bsd/Rank2Cert749.lean

lean_all/bsd/Rank2Cert749.lean

/-
  bsd/Rank2Cert749.lean — T39 cycle 1690 (goal G847): rank ≥ 2 for 2 of the 5 congruent curves in 150401–150600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert749
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (150465, (true, 24940085940025, 2438, 124470807617028839525), (false, 193406517311, 1216, 48363669467063713)),
   (150546, (false, 11154, 1, 15855840), (true, 939784830625, 2356, 416852160185349775))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert749

#print axioms Rank2Cert749.on_curve
#print axioms Rank2Cert749.indep
#print axioms Rank2Cert749.count

bsd/Rank2Cert75.lean

lean_all/bsd/Rank2Cert75.lean

/-
  bsd/Rank2Cert75.lean — T39 cycle 423 (goal G138): rank ≥ 2 for 7 of the 14 congruent curves in 15601–15800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert75
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (15626, (false, 2433600, 17, 5934374160), (true, 595799594, 191, 4229014642320)),
   (15635, (false, 501264, 29, 9302749716), (true, 43130475, 41, 224588202300)),
   (15665, (false, 7744, 1, 1198296), (true, 15471628225, 813, 1429964063879300)),
   (15738, (true, 22188, 1, 2329740), (true, 292746150, 121, 3089586697920)),
   (15753, (true, 69707025, 8, 581928186105), (true, 817841314305625, 217607, 9588831920324957274200)),
   (15771, (false, 11163, 1, 1177056), (true, 15792028, 31, 17631799262)),
   (15785, (false, 1713481, 157, 509306625828), (true, 243089, 1, 119599788))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 7 := rfl

end Rank2Cert75

#print axioms Rank2Cert75.on_curve
#print axioms Rank2Cert75.indep
#print axioms Rank2Cert75.count

bsd/Rank2Cert750.lean

lean_all/bsd/Rank2Cert750.lean

/-
  bsd/Rank2Cert750.lean — T39 cycle 1692 (goal G847): rank ≥ 2 for 1 of the 4 congruent curves in 150601–150800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert750
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (150745, (true, 71561817145, 158, 19117043046826725), (false, 11263985775, 5143, 423175268532552900))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert750

#print axioms Rank2Cert750.on_curve
#print axioms Rank2Cert750.indep
#print axioms Rank2Cert750.count

bsd/Rank2Cert751.lean

lean_all/bsd/Rank2Cert751.lean

/-
  bsd/Rank2Cert751.lean — T39 cycle 1694 (goal G847): rank ≥ 2 for 0 of the 6 congruent curves in 150801–151000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert751
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert751

#print axioms Rank2Cert751.on_curve
#print axioms Rank2Cert751.indep
#print axioms Rank2Cert751.count

bsd/Rank2Cert752.lean

lean_all/bsd/Rank2Cert752.lean

/-
  bsd/Rank2Cert752.lean — T39 cycle 1696 (goal G847): rank ≥ 2 for 3 of the 5 congruent curves in 151001–151200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert752
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (151081, (false, 476847, 4, 1636442073), (true, 31734215881, 449, 1587013156332120)),
   (151130, (true, 15035650, 3, 58062932200), (true, 151468870, 31, 529261681200)),
   (151145, (true, 168551280, 23, 1926377261100), (true, 1404825361, 95, 12588827277516))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert752

#print axioms Rank2Cert752.on_curve
#print axioms Rank2Cert752.indep
#print axioms Rank2Cert752.count

bsd/Rank2Cert753.lean

lean_all/bsd/Rank2Cert753.lean

/-
  bsd/Rank2Cert753.lean — T39 cycle 1698 (goal G847): rank ≥ 2 for 3 of the 5 congruent curves in 151201–151400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert753
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (151242, (false, 85683, 1, 36481185), (true, 2217397, 3, 2606568965)),
   (151257, (true, 80065372, 23, 25420755610), (true, 10555023700146025, 28842, 1084321225097443439732845)),
   (151305, (true, 172480, 1, 34388200), (true, 35688345, 2, 213170524875))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert753

#print axioms Rank2Cert753.on_curve
#print axioms Rank2Cert753.indep
#print axioms Rank2Cert753.count

bsd/Rank2Cert754.lean

lean_all/bsd/Rank2Cert754.lean

/-
  bsd/Rank2Cert754.lean — T39 cycle 1700 (goal G847): rank ≥ 2 for 2 of the 9 congruent curves in 151401–151600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert754
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (151409, (false, 76144250983, 3073, 393984055357021284), (true, 5957798543647729, 178010, 272641499980181102385567)),
   (151554, (true, 9924525, 7, 20741595615), (false, 197614352, 43, 2792949690232))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert754

#print axioms Rank2Cert754.on_curve
#print axioms Rank2Cert754.indep
#print axioms Rank2Cert754.count

bsd/Rank2Cert755.lean

lean_all/bsd/Rank2Cert755.lean

/-
  bsd/Rank2Cert755.lean — T39 cycle 1702 (goal G847): rank ≥ 2 for 0 of the 4 congruent curves in 151601–151800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert755
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert755

#print axioms Rank2Cert755.on_curve
#print axioms Rank2Cert755.indep
#print axioms Rank2Cert755.count

bsd/Rank2Cert756.lean

lean_all/bsd/Rank2Cert756.lean

/-
  bsd/Rank2Cert756.lean — T39 cycle 1704 (goal G847): rank ≥ 2 for 2 of the 5 congruent curves in 151801–152000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert756
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (151810, (true, 20313964, 9, 72877269212), (true, 187765, 1, 47880075)),
   (151970, (true, 1478750, 1, 1788696000), (true, 692770, 1, 562567200))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert756

#print axioms Rank2Cert756.on_curve
#print axioms Rank2Cert756.indep
#print axioms Rank2Cert756.count

bsd/Rank2Cert757.lean

lean_all/bsd/Rank2Cert757.lean

/-
  bsd/Rank2Cert757.lean — T39 cycle 1706 (goal G848): rank ≥ 2 for 1 of the 10 congruent curves in 152001–152200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert757
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (152009, (true, 302588, 1, 143920062), (true, 8502499681, 185, 620110914062796))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert757

#print axioms Rank2Cert757.on_curve
#print axioms Rank2Cert757.indep
#print axioms Rank2Cert757.count

bsd/Rank2Cert758.lean

lean_all/bsd/Rank2Cert758.lean

/-
  bsd/Rank2Cert758.lean — T39 cycle 1708 (goal G848): rank ≥ 2 for 2 of the 10 congruent curves in 152201–152400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert758
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (152234, (false, 38452063, 16, 39339858831), (true, 26254494, 5, 133104842472)),
   (152306, (true, 4027122, 1, 8075721984), (false, 8827903, 8, 12280540671))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert758

#print axioms Rank2Cert758.on_curve
#print axioms Rank2Cert758.indep
#print axioms Rank2Cert758.count

bsd/Rank2Cert759.lean

lean_all/bsd/Rank2Cert759.lean

/-
  bsd/Rank2Cert759.lean — T39 cycle 1710 (goal G848): rank ≥ 2 for 2 of the 6 congruent curves in 152401–152600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert759
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (152418, (false, 1834602, 17, 59611222440), (false, 157960334, 75, 10590919959536)),
   (152481, (true, 153328, 1, 6301988), (true, 326164503000625, 17433, 5830783277896069137800))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert759

#print axioms Rank2Cert759.on_curve
#print axioms Rank2Cert759.indep
#print axioms Rank2Cert759.count

bsd/Rank2Cert76.lean

lean_all/bsd/Rank2Cert76.lean

/-
  bsd/Rank2Cert76.lean — T39 cycle 424 (goal G139): rank ≥ 2 for 4 of the 7 congruent curves in 15801–16000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert76
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (15834, (true, 184093, 1, 78694343), (true, 3572829, 5, 6711757767)),
   (15841, (true, 3437497, 12, 4767808339), (true, 71791729, 41, 564899654880)),
   (15946, (true, 80802, 1, 22516824), (true, 96748, 1, 29681268)),
   (15977, (true, 2149679584465459225, 731619, 3151789187282876383184283740), (true, 1713260375152275625, 2728016, 2237106396701038318464741525))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert76

#print axioms Rank2Cert76.on_curve
#print axioms Rank2Cert76.indep
#print axioms Rank2Cert76.count

bsd/Rank2Cert760.lean

lean_all/bsd/Rank2Cert760.lean

/-
  bsd/Rank2Cert760.lean — T39 cycle 1712 (goal G848): rank ≥ 2 for 3 of the 7 congruent curves in 152601–152800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert760
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (152609, (true, 485537, 1, 321178416), (false, 758063, 7, 6477176784)),
   (152691, (true, 201243, 1, 58806468), (false, 4269419, 15, 70436970604)),
   (152698, (false, 17219475, 19, 217297621905), (true, 185800286, 25, 2172985703784))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert760

#print axioms Rank2Cert760.on_curve
#print axioms Rank2Cert760.indep
#print axioms Rank2Cert760.count

bsd/Rank2Cert761.lean

lean_all/bsd/Rank2Cert761.lean

/-
  bsd/Rank2Cert761.lean — T39 cycle 1714 (goal G848): rank ≥ 2 for 2 of the 3 congruent curves in 152801–153000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert761
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (152859, (true, 16443261, 5, 64852221384), (true, 86634268, 9, 798092632162)),
   (152930, (true, 77290289, 20, 415324631337), (true, 1845840152689, 206, 2507774310837854487))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert761

#print axioms Rank2Cert761.on_curve
#print axioms Rank2Cert761.indep
#print axioms Rank2Cert761.count

bsd/Rank2Cert762.lean

lean_all/bsd/Rank2Cert762.lean

/-
  bsd/Rank2Cert762.lean — T39 cycle 1716 (goal G848): rank ≥ 2 for 1 of the 10 congruent curves in 153001–153200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert762
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (153010, (true, 204490, 1, 61347000), (true, 297674, 1, 139311432))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert762

#print axioms Rank2Cert762.on_curve
#print axioms Rank2Cert762.indep
#print axioms Rank2Cert762.count

bsd/Rank2Cert763.lean

lean_all/bsd/Rank2Cert763.lean

/-
  bsd/Rank2Cert763.lean — T39 cycle 1718 (goal G848): rank ≥ 2 for 3 of the 5 congruent curves in 153201–153400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert763
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (153258, (true, 1564458, 1, 1947385440), (false, 20979987, 17, 178669480815)),
   (153265, (true, 507964, 1, 345161538), (true, 723617331649, 794, 610037853529503993)),
   (153330, (true, 183750, 1, 43407000), (false, 36903740394, 731, 14052764432095704))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert763

#print axioms Rank2Cert763.on_curve
#print axioms Rank2Cert763.indep
#print axioms Rank2Cert763.count

bsd/Rank2Cert764.lean

lean_all/bsd/Rank2Cert764.lean

/-
  bsd/Rank2Cert764.lean — T39 cycle 1720 (goal G848): rank ≥ 2 for 0 of the 7 congruent curves in 153401–153600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert764
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert764

#print axioms Rank2Cert764.on_curve
#print axioms Rank2Cert764.indep
#print axioms Rank2Cert764.count

bsd/Rank2Cert765.lean

lean_all/bsd/Rank2Cert765.lean

/-
  bsd/Rank2Cert765.lean — T39 cycle 1722 (goal G848): rank ≥ 2 for 2 of the 8 congruent curves in 153601–153800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert765
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (153665, (false, 784, 1, 4302564), (true, 40439864385, 92, 8128114369286175)),
   (153745, (false, 7056, 1, 12900972), (true, 59062498855369, 2773, 453816943150213605072))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert765

#print axioms Rank2Cert765.on_curve
#print axioms Rank2Cert765.indep
#print axioms Rank2Cert765.count

bsd/Rank2Cert766.lean

lean_all/bsd/Rank2Cert766.lean

/-
  bsd/Rank2Cert766.lean — T39 cycle 1724 (goal G848): rank ≥ 2 for 1 of the 10 congruent curves in 153801–154000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert766
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (153881, (true, 1956487, 3, 1933009156), (true, 588069703, 17, 14219941602672))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert766

#print axioms Rank2Cert766.on_curve
#print axioms Rank2Cert766.indep
#print axioms Rank2Cert766.count

bsd/Rank2Cert767.lean

lean_all/bsd/Rank2Cert767.lean

/-
  bsd/Rank2Cert767.lean — T39 cycle 1726 (goal G851): rank ≥ 2 for 0 of the 8 congruent curves in 154001–154200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert767
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert767

#print axioms Rank2Cert767.on_curve
#print axioms Rank2Cert767.indep
#print axioms Rank2Cert767.count

bsd/Rank2Cert768.lean

lean_all/bsd/Rank2Cert768.lean

/-
  bsd/Rank2Cert768.lean — T39 cycle 1728 (goal G851): rank ≥ 2 for 0 of the 7 congruent curves in 154201–154400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert768
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert768

#print axioms Rank2Cert768.on_curve
#print axioms Rank2Cert768.indep
#print axioms Rank2Cert768.count

bsd/Rank2Cert769.lean

lean_all/bsd/Rank2Cert769.lean

/-
  bsd/Rank2Cert769.lean — T39 cycle 1730 (goal G851): rank ≥ 2 for 1 of the 5 congruent curves in 154401–154600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert769
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (154401, (false, 1648656, 5, 4481459172), (true, 66455025, 19, 294991957260))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert769

#print axioms Rank2Cert769.on_curve
#print axioms Rank2Cert769.indep
#print axioms Rank2Cert769.count

bsd/Rank2Cert77.lean

lean_all/bsd/Rank2Cert77.lean

/-
  bsd/Rank2Cert77.lean — T39 cycle 425 (goal G140): rank ≥ 2 for 4 of the 11 congruent curves in 16001–16200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert77
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (16089, (true, 748225, 1, 647064980), (true, 927799, 5, 805329532)),
   (16107, (true, 24843, 1, 2981160), (false, 48073077, 269, 8074217518380)),
   (16154, (true, 1116225, 8, 444616095), (true, 1441644469225, 1732, 1729982717243027445)),
   (16185, (true, 21025, 1, 1945900), (true, 4298736, 7, 8759749284))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert77

#print axioms Rank2Cert77.on_curve
#print axioms Rank2Cert77.indep
#print axioms Rank2Cert77.count

bsd/Rank2Cert770.lean

lean_all/bsd/Rank2Cert770.lean

/-
  bsd/Rank2Cert770.lean — T39 cycle 1732 (goal G851): rank ≥ 2 for 1 of the 7 congruent curves in 154601–154800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert770
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (154682, (true, 1916882, 1, 2645297160), (true, 7581618, 7, 502871160))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert770

#print axioms Rank2Cert770.on_curve
#print axioms Rank2Cert770.indep
#print axioms Rank2Cert770.count

bsd/Rank2Cert771.lean

lean_all/bsd/Rank2Cert771.lean

/-
  bsd/Rank2Cert771.lean — T39 cycle 1734 (goal G851): rank ≥ 2 for 2 of the 5 congruent curves in 154801–155000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert771
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (154835, (true, 48215619, 5, 333716370972), (true, 181855338025, 144, 77539212330528725)),
   (154882, (true, 978650, 1, 955945320), (true, 8510421682, 99, 772512744611440))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert771

#print axioms Rank2Cert771.on_curve
#print axioms Rank2Cert771.indep
#print axioms Rank2Cert771.count

bsd/Rank2Cert772.lean

lean_all/bsd/Rank2Cert772.lean

/-
  bsd/Rank2Cert772.lean — T39 cycle 1736 (goal G851): rank ≥ 2 for 1 of the 6 congruent curves in 155001–155200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert772
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (155170, (true, 16946405, 1, 69758665725), (false, 17615408094, 2945, 178602488706386304))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert772

#print axioms Rank2Cert772.on_curve
#print axioms Rank2Cert772.indep
#print axioms Rank2Cert772.count

bsd/Rank2Cert773.lean

lean_all/bsd/Rank2Cert773.lean

/-
  bsd/Rank2Cert773.lean — T39 cycle 1738 (goal G851): rank ≥ 2 for 4 of the 5 congruent curves in 155201–155400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert773
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (155210, (true, 2350726, 3, 2898780976), (false, 578531250, 937, 3277618318095000)),
   (155274, (true, 752703203380969, 67488, 7070051406095740593035), (true, 1472446122, 1, 56501387960976)),
   (155290, (false, 32400, 13, 4723918200), (true, 1376410, 1, 1604500800)),
   (155305, (true, 36162727225, 482, 461854042616475), (true, 985037552040573025801, 25917682, 30741837901840680051479753462499))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert773

#print axioms Rank2Cert773.on_curve
#print axioms Rank2Cert773.indep
#print axioms Rank2Cert773.count

bsd/Rank2Cert774.lean

lean_all/bsd/Rank2Cert774.lean

/-
  bsd/Rank2Cert774.lean — T39 cycle 1740 (goal G851): rank ≥ 2 for 1 of the 6 congruent curves in 155401–155600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert774
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (155490, (true, 237705885, 13, 3642418528425), (true, 96931325740, 313, 29803405059019900))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert774

#print axioms Rank2Cert774.on_curve
#print axioms Rank2Cert774.indep
#print axioms Rank2Cert774.count

bsd/Rank2Cert775.lean

lean_all/bsd/Rank2Cert775.lean

/-
  bsd/Rank2Cert775.lean — T39 cycle 1742 (goal G851): rank ≥ 2 for 1 of the 4 congruent curves in 155601–155800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert775
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (155730, (true, 5419404, 1, 12610953108), (true, 948204230055625, 74992, 11190822526644379445125))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert775

#print axioms Rank2Cert775.on_curve
#print axioms Rank2Cert775.indep
#print axioms Rank2Cert775.count

bsd/Rank2Cert776.lean

lean_all/bsd/Rank2Cert776.lean

/-
  bsd/Rank2Cert776.lean — T39 cycle 1744 (goal G851): rank ≥ 2 for 3 of the 9 congruent curves in 155801–156000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert776
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (155930, (false, 19220, 1569, 53217229057300), (false, 1984279, 10, 21786418731)),
   (155946, (false, 13254, 5, 448833456), (false, 372287, 4, 1505373857)),
   (155955, (true, 1974025, 2, 2631375325), (true, 426417600, 31, 8243678003400))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert776

#print axioms Rank2Cert776.on_curve
#print axioms Rank2Cert776.indep
#print axioms Rank2Cert776.count

bsd/Rank2Cert777.lean

lean_all/bsd/Rank2Cert777.lean

/-
  bsd/Rank2Cert777.lean — T39 cycle 1786 (goal G855): rank ≥ 2 for 3 of the 5 congruent curves in 156001–156200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert777
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (156026, (true, 2441897, 2, 3689113611), (true, 1168589106169, 512, 1262486006536592115)),
   (156145, (false, 11587455, 14, 96422646375), (false, 68622895, 49, 3053196630000)),
   (156170, (true, 196630, 1, 52978800), (true, 47470689, 16, 176346965487))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert777

#print axioms Rank2Cert777.on_curve
#print axioms Rank2Cert777.indep
#print axioms Rank2Cert777.count

bsd/Rank2Cert778.lean

lean_all/bsd/Rank2Cert778.lean

/-
  bsd/Rank2Cert778.lean — T39 cycle 1788 (goal G855): rank ≥ 2 for 3 of the 8 congruent curves in 156201–156400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert778
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (156210, (true, 161290, 1, 16129000), (true, 670547046, 23, 17231408504856)),
   (156377, (true, 25260992, 11, 84115126200), (true, 137294158313, 109, 50867257075578060)),
   (156385, (false, 2030625, 13, 37550089500), (true, 2017282217647225, 68679, 84330078155855133562700))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert778

#print axioms Rank2Cert778.on_curve
#print axioms Rank2Cert778.indep
#print axioms Rank2Cert778.count

bsd/Rank2Cert779.lean

lean_all/bsd/Rank2Cert779.lean

/-
  bsd/Rank2Cert779.lean — T39 cycle 1790 (goal G855): rank ≥ 2 for 1 of the 14 congruent curves in 156401–156600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert779
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (156585, (false, 54665, 1, 34307000), (true, 1973031225, 14, 87629107853925))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert779

#print axioms Rank2Cert779.on_curve
#print axioms Rank2Cert779.indep
#print axioms Rank2Cert779.count

bsd/Rank2Cert78.lean

lean_all/bsd/Rank2Cert78.lean

/-
  bsd/Rank2Cert78.lean — T39 cycle 426 (goal G141): rank ≥ 2 for 3 of the 7 congruent curves in 16201–16400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert78
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (16226, (false, 42350, 3, 28761040), (true, 505141, 3, 343690165)),
   (16347, (true, 18871636129, 440, 2555758980779633), (true, 37045883960089, 40700, 153875531074620274813)),
   (16385, (false, 256, 1, 262128), (true, 2604198745009, 4167, 4177378697988551552))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert78

#print axioms Rank2Cert78.on_curve
#print axioms Rank2Cert78.indep
#print axioms Rank2Cert78.count

bsd/Rank2Cert780.lean

lean_all/bsd/Rank2Cert780.lean

/-
  bsd/Rank2Cert780.lean — T39 cycle 1792 (goal G855): rank ≥ 2 for 2 of the 7 congruent curves in 156601–156800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert780
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (156689, (true, 1473715321, 95, 15925952897244), (true, 179431005858156618241, 9169760, 2397023595638188601093986323711)),
   (156769, (true, 148850047, 29, 842962047564), (true, 19094620969, 126, 2616047603381645))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert780

#print axioms Rank2Cert780.on_curve
#print axioms Rank2Cert780.indep
#print axioms Rank2Cert780.count

bsd/Rank2Cert781.lean

lean_all/bsd/Rank2Cert781.lean

/-
  bsd/Rank2Cert781.lean — T39 cycle 1794 (goal G855): rank ≥ 2 for 2 of the 7 congruent curves in 156801–157000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert781
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (156819, (false, 152100, 1, 14890590), (true, 2518441333815889729, 765146, 3994008516539578091882740985)),
   (156954, (true, 214221, 1, 67479615), (true, 533022, 1, 371897064))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert781

#print axioms Rank2Cert781.on_curve
#print axioms Rank2Cert781.indep
#print axioms Rank2Cert781.count

bsd/Rank2Cert782.lean

lean_all/bsd/Rank2Cert782.lean

/-
  bsd/Rank2Cert782.lean — T39 cycle 1838 (goal G855): rank ≥ 2 for 0 of the 8 congruent curves in 157001–157200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert782
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert782

#print axioms Rank2Cert782.on_curve
#print axioms Rank2Cert782.indep
#print axioms Rank2Cert782.count

bsd/Rank2Cert783.lean

lean_all/bsd/Rank2Cert783.lean

/-
  bsd/Rank2Cert783.lean — T39 cycle 1840 (goal G855): rank ≥ 2 for 1 of the 7 congruent curves in 157201–157400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert783
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (157251, (true, 2752387, 3, 3916396484), (false, 15831429, 125, 9776074116744))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert783

#print axioms Rank2Cert783.on_curve
#print axioms Rank2Cert783.indep
#print axioms Rank2Cert783.count

bsd/Rank2Cert784.lean

lean_all/bsd/Rank2Cert784.lean

/-
  bsd/Rank2Cert784.lean — T39 cycle 1842 (goal G855): rank ≥ 2 for 0 of the 8 congruent curves in 157401–157600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert784
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert784

#print axioms Rank2Cert784.on_curve
#print axioms Rank2Cert784.indep
#print axioms Rank2Cert784.count

bsd/Rank2Cert785.lean

lean_all/bsd/Rank2Cert785.lean

/-
  bsd/Rank2Cert785.lean — T39 cycle 1844 (goal G855): rank ≥ 2 for 1 of the 6 congruent curves in 157601–157800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert785
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (157610, (true, 1082410, 1, 1114125600), (false, 810, 1, 4485600))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert785

#print axioms Rank2Cert785.on_curve
#print axioms Rank2Cert785.indep
#print axioms Rank2Cert785.count

bsd/Rank2Cert786.lean

lean_all/bsd/Rank2Cert786.lean

/-
  bsd/Rank2Cert786.lean — T39 cycle 1846 (goal G855): rank ≥ 2 for 3 of the 10 congruent curves in 157801–158000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert786
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (157809, (false, 6959504, 55, 1259215710956), (true, 39581827205281, 2071, 248989052483043638120)),
   (157890, (true, 14578510, 3, 55398338000), (false, 95666490, 133, 27301273223400)),
   (157929, (true, 266603367, 7, 4351263813504), (true, 1112188683357049, 11930, 37083353221386726441157))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert786

#print axioms Rank2Cert786.on_curve
#print axioms Rank2Cert786.indep
#print axioms Rank2Cert786.count

bsd/Rank2Cert787.lean

lean_all/bsd/Rank2Cert787.lean

/-
  bsd/Rank2Cert787.lean — T39 cycle 1888 (goal G856): rank ≥ 2 for 0 of the 5 congruent curves in 158001–158200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert787
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert787

#print axioms Rank2Cert787.on_curve
#print axioms Rank2Cert787.indep
#print axioms Rank2Cert787.count

bsd/Rank2Cert788.lean

lean_all/bsd/Rank2Cert788.lean

/-
  bsd/Rank2Cert788.lean — T39 cycle 1890 (goal G856): rank ≥ 2 for 2 of the 9 congruent curves in 158201–158400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert788
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (158249, (true, 4053609, 5, 1778183148), (true, 422333681, 40, 6946530593079)),
   (158385, (true, 1252815, 1, 1391014800), (true, 13229601550200025, 42081, 1521326843045403198209900))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert788

#print axioms Rank2Cert788.on_curve
#print axioms Rank2Cert788.indep
#print axioms Rank2Cert788.count

bsd/Rank2Cert789.lean

lean_all/bsd/Rank2Cert789.lean

/-
  bsd/Rank2Cert789.lean — T39 cycle 1892 (goal G856): rank ≥ 2 for 1 of the 2 congruent curves in 158401–158600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert789
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (158585, (true, 6248249, 5, 12071617068), (true, 83816771465, 722, 4004079933848925))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert789

#print axioms Rank2Cert789.on_curve
#print axioms Rank2Cert789.indep
#print axioms Rank2Cert789.count

bsd/Rank2Cert79.lean

lean_all/bsd/Rank2Cert79.lean

/-
  bsd/Rank2Cert79.lean — T39 cycle 427 (goal G142): rank ≥ 2 for 7 of the 10 congruent curves in 16401–16600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert79
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (16419, (true, 24843, 1, 2938572), (true, 6255806347921, 18904, 5426651850148289735)),
   (16465, (true, 297025, 3, 140293900), (false, 2025, 1, 735300)),
   (16482, (true, 181138, 3, 44243920), (false, 5230200498, 1933, 4437730494639360)),
   (16505, (true, 218005225, 22, 3216685403475), (true, 1183657146553207225, 4357088, 1241831851480131533428039725)),
   (16530, (true, 37845, 1, 6622875), (true, 18150, 1, 1009800)),
   (16531, (true, 3183780625, 438, 15844409542225), (true, 428695861, 113, 7725578079780)),
   (16539, (true, 821437, 7, 121572676), (true, 699645937340319989881, 5283586, 18506205498603555024639842963845))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 7 := rfl

end Rank2Cert79

#print axioms Rank2Cert79.on_curve
#print axioms Rank2Cert79.indep
#print axioms Rank2Cert79.count

bsd/Rank2Cert790.lean

lean_all/bsd/Rank2Cert790.lean

/-
  bsd/Rank2Cert790.lean — T39 cycle 1894 (goal G856): rank ≥ 2 for 2 of the 6 congruent curves in 158601–158800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert790
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (158610, (true, 8221285, 7, 7686901475), (true, 439995840, 1, 9229386373200)),
   (158730, (false, 30, 1, 869400), (true, 9750906, 5, 27813129696))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert790

#print axioms Rank2Cert790.on_curve
#print axioms Rank2Cert790.indep
#print axioms Rank2Cert790.count

bsd/Rank2Cert791.lean

lean_all/bsd/Rank2Cert791.lean

/-
  bsd/Rank2Cert791.lean — T39 cycle 1896 (goal G856): rank ≥ 2 for 2 of the 8 congruent curves in 158801–159000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert791
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (158802, (true, 63361998, 1, 504361504080), (true, 2566712850, 113, 79723803968640)),
   (158865, (true, 20762721, 2, 94563361431), (false, 172304, 7, 3230468956))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert791

#print axioms Rank2Cert791.on_curve
#print axioms Rank2Cert791.indep
#print axioms Rank2Cert791.count

bsd/Rank2Cert792.lean

lean_all/bsd/Rank2Cert792.lean

/-
  bsd/Rank2Cert792.lean — T39 cycle 1898 (goal G856): rank ≥ 2 for 1 of the 7 congruent curves in 159001–159200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert792
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (159114, (true, 7976454, 5, 19526359392), (true, 61152814, 1, 478215005480))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert792

#print axioms Rank2Cert792.on_curve
#print axioms Rank2Cert792.indep
#print axioms Rank2Cert792.count

bsd/Rank2Cert793.lean

lean_all/bsd/Rank2Cert793.lean

/-
  bsd/Rank2Cert793.lean — T39 cycle 1900 (goal G856): rank ≥ 2 for 2 of the 7 congruent curves in 159201–159400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert793
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (159315, (true, 1164225, 2, 1051295175), (false, 6871823765, 233, 435377242875700)),
   (159321, (true, 535623376118976601, 606309, 389652370522978735452995020), (false, 291984287, 116, 36291327963247))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert793

#print axioms Rank2Cert793.on_curve
#print axioms Rank2Cert793.indep
#print axioms Rank2Cert793.count

bsd/Rank2Cert794.lean

lean_all/bsd/Rank2Cert794.lean

/-
  bsd/Rank2Cert794.lean — T39 cycle 1902 (goal G856): rank ≥ 2 for 0 of the 6 congruent curves in 159401–159600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert794
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert794

#print axioms Rank2Cert794.on_curve
#print axioms Rank2Cert794.indep
#print axioms Rank2Cert794.count

bsd/Rank2Cert795.lean

lean_all/bsd/Rank2Cert795.lean

/-
  bsd/Rank2Cert795.lean — T39 cycle 1904 (goal G856): rank ≥ 2 for 2 of the 6 congruent curves in 159601–159800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert795
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (159666, (true, 9287616, 7, 15253196400), (true, 14735916, 1, 56564012700)),
   (159681, (false, 173417, 23, 35176597948), (false, 6284288, 7, 11685375136))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert795

#print axioms Rank2Cert795.on_curve
#print axioms Rank2Cert795.indep
#print axioms Rank2Cert795.count

bsd/Rank2Cert796.lean

lean_all/bsd/Rank2Cert796.lean

/-
  bsd/Rank2Cert796.lean — T39 cycle 1906 (goal G856): rank ≥ 2 for 2 of the 6 congruent curves in 159801–160000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert796
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (159803, (true, 14085493, 9, 20846529640), (true, 438298285681, 550, 288401193915871329)),
   (159915, (false, 2558535, 4, 37077075), (false, 38829, 1, 30568356))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert796

#print axioms Rank2Cert796.on_curve
#print axioms Rank2Cert796.indep
#print axioms Rank2Cert796.count

bsd/Rank2Cert797.lean

lean_all/bsd/Rank2Cert797.lean

/-
  bsd/Rank2Cert797.lean — T39 cycle 1916 (goal G857): rank ≥ 2 for 1 of the 4 congruent curves in 160001–160200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert797
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (160121, (true, 171873, 1, 25895532), (true, 4332946154329, 5060, 2919472124773263267))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert797

#print axioms Rank2Cert797.on_curve
#print axioms Rank2Cert797.indep
#print axioms Rank2Cert797.count

bsd/Rank2Cert798.lean

lean_all/bsd/Rank2Cert798.lean

/-
  bsd/Rank2Cert798.lean — T39 cycle 1918 (goal G857): rank ≥ 2 for 1 of the 7 congruent curves in 160201–160400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert798
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (160370, (true, 206045, 1, 58722825), (false, 72683280, 23, 373005870600))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert798

#print axioms Rank2Cert798.on_curve
#print axioms Rank2Cert798.indep
#print axioms Rank2Cert798.count

bsd/Rank2Cert799.lean

lean_all/bsd/Rank2Cert799.lean

/-
  bsd/Rank2Cert799.lean — T39 cycle 1920 (goal G857): rank ≥ 2 for 2 of the 7 congruent curves in 160401–160600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert799
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (160491, (true, 408075248194136329, 485012, 259563597038152341317892325), (true, 229450826377, 358, 109466778382014691)),
   (160545, (true, 689920, 1, 557326000), (true, 245473305, 23, 3608457583500))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert799

#print axioms Rank2Cert799.on_curve
#print axioms Rank2Cert799.indep
#print axioms Rank2Cert799.count

bsd/Rank2Cert8.lean

lean_all/bsd/Rank2Cert8.lean

/-
  bsd/Rank2Cert8.lean — T39 cycle 330 (goal G65): rank ≥ 2 for 9 of the 10 congruent curves in 2201–2400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert8
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (2201, (true, 10001344, 55, 23601921672), (true, 2555201401, 799, 107880262449960)),
   (2249, (true, 29929, 2, 4938285), (true, 15345511129, 786, 1893147856099435)),
   (2257, (false, 665856, 25, 1014763728), (false, 2325625, 73, 17995872300)),
   (2282, (true, 6429698, 31, 15326061960), (false, 120127, 8, 28789215)),
   (2298, (true, 5202, 1, 336600), (false, 37666858487, 6566, 17783983145387845)),
   (2306, (true, 722402, 7, 606442656), (true, 17392244500801, 49764, 68511196863395556895)),
   (2329, (true, 469225, 9, 294297920), (true, 2873, 1, 90168)),
   (2337, (true, 133209, 5, 43692552), (true, 157676497225, 7859, 25199134216624160)),
   (2379, (true, 48841, 2, 10587005), (true, 93025, 2, 28223785))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 9 := rfl

end Rank2Cert8

#print axioms Rank2Cert8.on_curve
#print axioms Rank2Cert8.indep
#print axioms Rank2Cert8.count

bsd/Rank2Cert80.lean

lean_all/bsd/Rank2Cert80.lean

/-
  bsd/Rank2Cert80.lean — T39 cycle 428 (goal G143): rank ≥ 2 for 8 of the 10 congruent curves in 16601–16800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert80
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (16610, (true, 80190, 1, 22215600), (true, 28875924790, 569, 4821018883323000)),
   (16666, (false, 9391226, 31, 39758169360), (true, 162561369721, 140, 65542823773035981)),
   (16674, (true, 28812, 1, 3988404), (false, 42350, 13, 579834640)),
   (16705, (true, 1651225, 7, 1842767100), (true, 3021398339089, 5267, 5189703377541575412)),
   (16714, (true, 112201082, 53, 1079438523744), (true, 14855200716539569, 266798, 1804764341606726918346825)),
   (16761, (true, 124014639, 43, 1337232135960), (true, 169818350284969, 91014, 1274256889992068229325)),
   (16771, (true, 139589404, 75, 1215590116958), (true, 78889413498543649, 145950, 22157646935415389154344393)),
   (16786, (false, 52983, 2, 9493701), (false, 76849542, 83, 757492407672))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 8 := rfl

end Rank2Cert80

#print axioms Rank2Cert80.on_curve
#print axioms Rank2Cert80.indep
#print axioms Rank2Cert80.count

bsd/Rank2Cert800.lean

lean_all/bsd/Rank2Cert800.lean

/-
  bsd/Rank2Cert800.lean — T39 cycle 1922 (goal G857): rank ≥ 2 for 3 of the 7 congruent curves in 160601–160800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert800
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (160602, (true, 163125, 1, 11542725), (false, 8963316752, 301, 1085180799533480)),
   (160673, (true, 11645775, 7, 29284782120), (true, 13432225, 1, 49225582560)),
   (160746, (true, 195248213161, 454, 85022891575227125), (true, 640893937, 12, 16214210180279))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert800

#print axioms Rank2Cert800.on_curve
#print axioms Rank2Cert800.indep
#print axioms Rank2Cert800.count

bsd/Rank2Cert801.lean

lean_all/bsd/Rank2Cert801.lean

/-
  bsd/Rank2Cert801.lean — T39 cycle 1924 (goal G857): rank ≥ 2 for 1 of the 2 congruent curves in 160801–161000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert801
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (160906, (true, 48132652, 17, 86188110036), (true, 8186977733, 71, 737128809656547))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert801

#print axioms Rank2Cert801.on_curve
#print axioms Rank2Cert801.indep
#print axioms Rank2Cert801.count

bsd/Rank2Cert802.lean

lean_all/bsd/Rank2Cert802.lean

/-
  bsd/Rank2Cert802.lean — T39 cycle 1926 (goal G857): rank ≥ 2 for 1 of the 5 congruent curves in 161001–161200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert802
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (161130, (true, 680805, 1, 545778675), (false, 292788275, 49, 4326961240225))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert802

#print axioms Rank2Cert802.on_curve
#print axioms Rank2Cert802.indep
#print axioms Rank2Cert802.count

bsd/Rank2Cert803.lean

lean_all/bsd/Rank2Cert803.lean

/-
  bsd/Rank2Cert803.lean — T39 cycle 1928 (goal G857): rank ≥ 2 for 1 of the 5 congruent curves in 161201–161400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert803
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (161265, (false, 37479, 1, 30365244), (false, 18299775, 32, 702068997825))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert803

#print axioms Rank2Cert803.on_curve
#print axioms Rank2Cert803.indep
#print axioms Rank2Cert803.count

bsd/Rank2Cert804.lean

lean_all/bsd/Rank2Cert804.lean

/-
  bsd/Rank2Cert804.lean — T39 cycle 1930 (goal G857): rank ≥ 2 for 0 of the 3 congruent curves in 161401–161600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert804
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert804

#print axioms Rank2Cert804.on_curve
#print axioms Rank2Cert804.indep
#print axioms Rank2Cert804.count

bsd/Rank2Cert805.lean

lean_all/bsd/Rank2Cert805.lean

/-
  bsd/Rank2Cert805.lean — T39 cycle 1932 (goal G857): rank ≥ 2 for 0 of the 7 congruent curves in 161601–161800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert805
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert805

#print axioms Rank2Cert805.on_curve
#print axioms Rank2Cert805.indep
#print axioms Rank2Cert805.count

bsd/Rank2Cert806.lean

lean_all/bsd/Rank2Cert806.lean

/-
  bsd/Rank2Cert806.lean — T39 cycle 1934 (goal G857): rank ≥ 2 for 1 of the 3 congruent curves in 161801–162000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert806
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (161993, (true, 58749977, 19, 43162745340), (true, 24113805757561, 6365, 113942888390954007684))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert806

#print axioms Rank2Cert806.on_curve
#print axioms Rank2Cert806.indep
#print axioms Rank2Cert806.count

bsd/Rank2Cert807.lean

lean_all/bsd/Rank2Cert807.lean

/-
  bsd/Rank2Cert807.lean — T39 cycle 1936 (goal G858): rank ≥ 2 for 1 of the 7 congruent curves in 162001–162200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert807
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (162097, (true, 77968657, 12, 656885935225), (true, 16422148380625, 9372, 33164372972705539175))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert807

#print axioms Rank2Cert807.on_curve
#print axioms Rank2Cert807.indep
#print axioms Rank2Cert807.count

bsd/Rank2Cert808.lean

lean_all/bsd/Rank2Cert808.lean

/-
  bsd/Rank2Cert808.lean — T39 cycle 1938 (goal G858): rank ≥ 2 for 2 of the 7 congruent curves in 162201–162400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert808
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (162305, (false, 15895, 1, 20364300), (true, 47281865144761, 2071, 325083423196383065916)),
   (162370, (true, 1379170, 1, 1608406800), (true, 1626209002441, 2754, 1354368084559273061))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert808

#print axioms Rank2Cert808.on_curve
#print axioms Rank2Cert808.indep
#print axioms Rank2Cert808.count

bsd/Rank2Cert809.lean

lean_all/bsd/Rank2Cert809.lean

/-
  bsd/Rank2Cert809.lean — T39 cycle 1940 (goal G858): rank ≥ 2 for 2 of the 6 congruent curves in 162401–162600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert809
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (162401, (false, 159201, 1, 12799920), (true, 7845601, 1, 21970819920)),
   (162433, (false, 39685087, 17, 157962264720), (true, 7448553025, 31, 642706187565120))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert809

#print axioms Rank2Cert809.on_curve
#print axioms Rank2Cert809.indep
#print axioms Rank2Cert809.count

bsd/Rank2Cert81.lean

lean_all/bsd/Rank2Cert81.lean

/-
  bsd/Rank2Cert81.lean — T39 cycle 429 (goal G144): rank ≥ 2 for 3 of the 7 congruent curves in 16801–17000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert81
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (16835, (false, 1073296, 257, 1151964035292), (true, 88985, 2, 17352075)),
   (16898, (true, 132098, 1, 47616960), (false, 7768592, 23, 12325514760)),
   (16985, (true, 152944, 3, 1922228), (true, 17824375, 27, 54131313500))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert81

#print axioms Rank2Cert81.on_curve
#print axioms Rank2Cert81.indep
#print axioms Rank2Cert81.count

bsd/Rank2Cert810.lean

lean_all/bsd/Rank2Cert810.lean

/-
  bsd/Rank2Cert810.lean — T39 cycle 1942 (goal G858): rank ≥ 2 for 6 of the 9 congruent curves in 162601–162800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert810
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (162609, (false, 823296, 5, 3612172608), (true, 1527654187937089, 32039, 59351204934238721872040)),
   (162610, (false, 13528046, 15, 125033054608), (true, 90195525, 17, 731141628525)),
   (162626, (false, 269125046, 123, 40120233840280), (false, 23717294527, 4438, 493270613512831167)),
   (162690, (true, 171394, 1, 22324672), (true, 286110, 1, 125888400)),
   (162714, (true, 5892486, 1, 14298227280), (true, 1170470874093, 143, 1266307450223539023)),
   (162715, (true, 204474340315, 561, 89514240436766600), (false, 249324635, 47, 4088135591400))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 6 := rfl

end Rank2Cert810

#print axioms Rank2Cert810.on_curve
#print axioms Rank2Cert810.indep
#print axioms Rank2Cert810.count

bsd/Rank2Cert811.lean

lean_all/bsd/Rank2Cert811.lean

/-
  bsd/Rank2Cert811.lean — T39 cycle 1944 (goal G858): rank ≥ 2 for 1 of the 3 congruent curves in 162801–163000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert811
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (162955, (true, 217971, 1, 67587156), (true, 69867705625, 464, 15970655901040125))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert811

#print axioms Rank2Cert811.on_curve
#print axioms Rank2Cert811.indep
#print axioms Rank2Cert811.count

bsd/Rank2Cert812.lean

lean_all/bsd/Rank2Cert812.lean

/-
  bsd/Rank2Cert812.lean — T39 cycle 1946 (goal G858): rank ≥ 2 for 2 of the 6 congruent curves in 163001–163200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert812
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (163137, (false, 111519759, 80, 10962679913289), (true, 400163416687225, 48891, 1796895366132927524840)),
   (163139, (true, 2617317, 1, 4226094516), (false, 45227223, 124, 16866715694379))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert812

#print axioms Rank2Cert812.on_curve
#print axioms Rank2Cert812.indep
#print axioms Rank2Cert812.count

bsd/Rank2Cert813.lean

lean_all/bsd/Rank2Cert813.lean

/-
  bsd/Rank2Cert813.lean — T39 cycle 1948 (goal G858): rank ≥ 2 for 2 of the 7 congruent curves in 163201–163400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert813
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (163306, (false, 28141542, 107, 9917322647232), (false, 2581150, 7, 12168790920)),
   (163385, (true, 230625, 1, 78166500), (true, 7591286842441, 4845, 18050026989935733364))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert813

#print axioms Rank2Cert813.on_curve
#print axioms Rank2Cert813.indep
#print axioms Rank2Cert813.count

bsd/Rank2Cert814.lean

lean_all/bsd/Rank2Cert814.lean

/-
  bsd/Rank2Cert814.lean — T39 cycle 1950 (goal G858): rank ≥ 2 for 0 of the 5 congruent curves in 163401–163600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert814
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert814

#print axioms Rank2Cert814.on_curve
#print axioms Rank2Cert814.indep
#print axioms Rank2Cert814.count

bsd/Rank2Cert815.lean

lean_all/bsd/Rank2Cert815.lean

/-
  bsd/Rank2Cert815.lean — T39 cycle 1952 (goal G858): rank ≥ 2 for 3 of the 5 congruent curves in 163601–163800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert815
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (163618, (true, 294185164, 15, 5006148935788), (false, 708277556, 235, 239734317999372)),
   (163699, (false, 170519443, 287, 176060544868104), (true, 1247713551894121, 5652, 44072584975873479084085)),
   (163785, (false, 23755649375, 693, 11557271269657000), (true, 443577660, 11, 9332986125150))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert815

#print axioms Rank2Cert815.on_curve
#print axioms Rank2Cert815.indep
#print axioms Rank2Cert815.count

bsd/Rank2Cert816.lean

lean_all/bsd/Rank2Cert816.lean

/-
  bsd/Rank2Cert816.lean — T39 cycle 1954 (goal G858): rank ≥ 2 for 0 of the 0 congruent curves in 163801–164000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert816
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert816

#print axioms Rank2Cert816.on_curve
#print axioms Rank2Cert816.indep
#print axioms Rank2Cert816.count

bsd/Rank2Cert817.lean

lean_all/bsd/Rank2Cert817.lean

/-
  bsd/Rank2Cert817.lean — T39 cycle 1956 (goal G859): rank ≥ 2 for 1 of the 7 congruent curves in 164001–164200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert817
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (164065, (true, 417282921, 47, 4224943210356), (true, 658480625, 13, 16882204168500))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert817

#print axioms Rank2Cert817.on_curve
#print axioms Rank2Cert817.indep
#print axioms Rank2Cert817.count

bsd/Rank2Cert818.lean

lean_all/bsd/Rank2Cert818.lean

/-
  bsd/Rank2Cert818.lean — T39 cycle 1958 (goal G859): rank ≥ 2 for 1 of the 4 congruent curves in 164201–164400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert818
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (164265, (true, 9576015, 7, 16053907500), (true, 1045675424979025, 55974, 29434852479215364584975))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert818

#print axioms Rank2Cert818.on_curve
#print axioms Rank2Cert818.indep
#print axioms Rank2Cert818.count

bsd/Rank2Cert819.lean

lean_all/bsd/Rank2Cert819.lean

/-
  bsd/Rank2Cert819.lean — T39 cycle 1960 (goal G859): rank ≥ 2 for 2 of the 5 congruent curves in 164401–164600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert819
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (164410, (false, 6839290, 7, 11133735600), (false, 4198400, 7, 14088124800)),
   (164498, (true, 13424346405625, 4186, 48038606704831917075), (false, 4751861648, 243, 583990401676760))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert819

#print axioms Rank2Cert819.on_curve
#print axioms Rank2Cert819.indep
#print axioms Rank2Cert819.count

bsd/Rank2Cert82.lean

lean_all/bsd/Rank2Cert82.lean

/-
  bsd/Rank2Cert82.lean — T39 cycle 430 (goal G145): rank ≥ 2 for 3 of the 7 congruent curves in 17001–17200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert82
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (17017, (false, 592900, 37, 17932319790), (true, 4938857, 2, 10974845805)),
   (17114, (true, 20812, 1, 1708476), (true, 5078214, 7, 11286591840)),
   (17121, (true, 7516119, 19, 11725145640), (true, 2875510407289, 7222, 4635011918600185915))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert82

#print axioms Rank2Cert82.on_curve
#print axioms Rank2Cert82.indep
#print axioms Rank2Cert82.count

bsd/Rank2Cert820.lean

lean_all/bsd/Rank2Cert820.lean

/-
  bsd/Rank2Cert820.lean — T39 cycle 1962 (goal G859): rank ≥ 2 for 2 of the 3 congruent curves in 164601–164800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert820
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (164697, (false, 18956223, 179, 22975493729760), (true, 3224536225, 109, 145537023531160)),
   (164761, (true, 117643473025, 253, 40188301261413240), (true, 2818305645961, 1085, 4720099880685182916))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert820

#print axioms Rank2Cert820.on_curve
#print axioms Rank2Cert820.indep
#print axioms Rank2Cert820.count

bsd/Rank2Cert821.lean

lean_all/bsd/Rank2Cert821.lean

/-
  bsd/Rank2Cert821.lean — T39 cycle 1964 (goal G859): rank ≥ 2 for 2 of the 8 congruent curves in 164801–165000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert821
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (164905, (true, 92181895, 21, 543873180500), (true, 1154182780, 21, 39133445338450)),
   (164985, (false, 88935, 1, 41441400), (true, 853734358125289, 46695, 22622387328617145614288))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert821

#print axioms Rank2Cert821.on_curve
#print axioms Rank2Cert821.indep
#print axioms Rank2Cert821.count

bsd/Rank2Cert822.lean

lean_all/bsd/Rank2Cert822.lean

/-
  bsd/Rank2Cert822.lean — T39 cycle 1966 (goal G859): rank ≥ 2 for 0 of the 4 congruent curves in 165001–165200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert822
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert822

#print axioms Rank2Cert822.on_curve
#print axioms Rank2Cert822.indep
#print axioms Rank2Cert822.count

bsd/Rank2Cert823.lean

lean_all/bsd/Rank2Cert823.lean

/-
  bsd/Rank2Cert823.lean — T39 cycle 1968 (goal G859): rank ≥ 2 for 2 of the 7 congruent curves in 165201–165400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert823
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (165315, (true, 412064169, 38, 6818051200653), (false, 246259054109, 2191, 374375288353777136)),
   (165394, (false, 656, 1, 4236120), (true, 104937771481, 320, 33547990724809629))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert823

#print axioms Rank2Cert823.on_curve
#print axioms Rank2Cert823.indep
#print axioms Rank2Cert823.count

bsd/Rank2Cert824.lean

lean_all/bsd/Rank2Cert824.lean

/-
  bsd/Rank2Cert824.lean — T39 cycle 1970 (goal G859): rank ≥ 2 for 3 of the 3 congruent curves in 165401–165600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert824
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (165410, (true, 1642285, 1, 2093913375), (false, 53578679, 18, 9017427419)),
   (165570, (true, 167445, 1, 10225125), (true, 1591350, 1, 1996572600)),
   (165594, (true, 446988, 1, 277579548), (true, 15011568, 1, 58158428328))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert824

#print axioms Rank2Cert824.on_curve
#print axioms Rank2Cert824.indep
#print axioms Rank2Cert824.count

bsd/Rank2Cert825.lean

lean_all/bsd/Rank2Cert825.lean

/-
  bsd/Rank2Cert825.lean — T39 cycle 1972 (goal G859): rank ≥ 2 for 0 of the 1 congruent curves in 165601–165800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert825
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert825

#print axioms Rank2Cert825.on_curve
#print axioms Rank2Cert825.indep
#print axioms Rank2Cert825.count

bsd/Rank2Cert826.lean

lean_all/bsd/Rank2Cert826.lean

/-
  bsd/Rank2Cert826.lean — T39 cycle 1974 (goal G859): rank ≥ 2 for 0 of the 3 congruent curves in 165801–166000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert826
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert826

#print axioms Rank2Cert826.on_curve
#print axioms Rank2Cert826.indep
#print axioms Rank2Cert826.count

bsd/Rank2Cert827.lean

lean_all/bsd/Rank2Cert827.lean

/-
  bsd/Rank2Cert827.lean — T39 cycle 1992 (goal G869): rank ≥ 2 for 1 of the 5 congruent curves in 166001–166200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert827
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (166019, (true, 11504668, 1, 39018081522), (true, 14698183460041, 1040, 56345999049572460411))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert827

#print axioms Rank2Cert827.on_curve
#print axioms Rank2Cert827.indep
#print axioms Rank2Cert827.count

bsd/Rank2Cert828.lean

lean_all/bsd/Rank2Cert828.lean

/-
  bsd/Rank2Cert828.lean — T39 cycle 1994 (goal G869): rank ≥ 2 for 0 of the 8 congruent curves in 166201–166400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert828
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert828

#print axioms Rank2Cert828.on_curve
#print axioms Rank2Cert828.indep
#print axioms Rank2Cert828.count

bsd/Rank2Cert829.lean

lean_all/bsd/Rank2Cert829.lean

/-
  bsd/Rank2Cert829.lean — T39 cycle 1996 (goal G869): rank ≥ 2 for 3 of the 13 congruent curves in 166401–166600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert829
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (166515, (true, 525809244435, 961, 364607729259705000), (true, 1305015625, 14, 47128944311875)),
   (166530, (false, 62846, 1, 38660608), (true, 505141, 1, 338949611)),
   (166579, (false, 262401251, 41, 1583543188080), (true, 940914704994889, 69222, 15282055900675189225405))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert829

#print axioms Rank2Cert829.on_curve
#print axioms Rank2Cert829.indep
#print axioms Rank2Cert829.count

bsd/Rank2Cert83.lean

lean_all/bsd/Rank2Cert83.lean

/-
  bsd/Rank2Cert83.lean — T39 cycle 431 (goal G146): rank ≥ 2 for 4 of the 7 congruent curves in 17201–17400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert83
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (17227, (false, 476847, 8, 686449575), (true, 509427, 1, 363391260)),
   (17266, (false, 18818, 67, 10632320544), (true, 768337, 2, 670758201)),
   (17290, (true, 176890, 3, 35378000), (true, 854126, 1, 789212424)),
   (17355, (false, 298116, 5, 172119402), (true, 1235676, 1, 1373453874))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert83

#print axioms Rank2Cert83.on_curve
#print axioms Rank2Cert83.indep
#print axioms Rank2Cert83.count

bsd/Rank2Cert830.lean

lean_all/bsd/Rank2Cert830.lean

/-
  bsd/Rank2Cert830.lean — T39 cycle 1998 (goal G869): rank ≥ 2 for 1 of the 3 congruent curves in 166601–166800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert830
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (166665, (true, 2098624, 3, 2126263832), (true, 762036025, 67, 3996221814100))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert830

#print axioms Rank2Cert830.on_curve
#print axioms Rank2Cert830.indep
#print axioms Rank2Cert830.count

bsd/Rank2Cert831.lean

lean_all/bsd/Rank2Cert831.lean

/-
  bsd/Rank2Cert831.lean — T39 cycle 2000 (goal G869): rank ≥ 2 for 2 of the 9 congruent curves in 166801–167000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert831
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (166810, (false, 84413315, 27, 804225437225), (true, 419965406, 31, 7954723861896)),
   (166866, (true, 272832, 1, 112747824), (true, 295788754, 25, 4760425914992))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert831

#print axioms Rank2Cert831.on_curve
#print axioms Rank2Cert831.indep
#print axioms Rank2Cert831.count

bsd/Rank2Cert832.lean

lean_all/bsd/Rank2Cert832.lean

/-
  bsd/Rank2Cert832.lean — T39 cycle 2002 (goal G869): rank ≥ 2 for 1 of the 10 congruent curves in 167001–167200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert832
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (167195, (true, 2754081, 4, 1086582321), (true, 517883168881, 560, 370774956167339529))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert832

#print axioms Rank2Cert832.on_curve
#print axioms Rank2Cert832.indep
#print axioms Rank2Cert832.count

bsd/Rank2Cert833.lean

lean_all/bsd/Rank2Cert833.lean

/-
  bsd/Rank2Cert833.lean — T39 cycle 2004 (goal G869): rank ≥ 2 for 1 of the 5 congruent curves in 167201–167400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert833
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (167362, (true, 182782018, 33, 187125692480), (true, 121622118768089641, 291480, 42124065541823144041360939))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert833

#print axioms Rank2Cert833.on_curve
#print axioms Rank2Cert833.indep
#print axioms Rank2Cert833.count

bsd/Rank2Cert834.lean

lean_all/bsd/Rank2Cert834.lean

/-
  bsd/Rank2Cert834.lean — T39 cycle 2006 (goal G869): rank ≥ 2 for 1 of the 4 congruent curves in 167401–167600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert834
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (167489, (false, 19663, 4, 375767721), (false, 139167, 1, 34766088))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert834

#print axioms Rank2Cert834.on_curve
#print axioms Rank2Cert834.indep
#print axioms Rank2Cert834.count

bsd/Rank2Cert835.lean

lean_all/bsd/Rank2Cert835.lean

/-
  bsd/Rank2Cert835.lean — T39 cycle 2008 (goal G869): rank ≥ 2 for 3 of the 6 congruent curves in 167601–167800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert835
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (167610, (false, 28851446784, 793, 17219396625471936), (false, 5704610790, 209, 346610138504400)),
   (167739, (false, 7248579, 55, 1365971217156), (true, 291525, 1, 128737440)),
   (167754, (true, 295650, 1, 132372360), (true, 90888066, 23, 187187946120))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert835

#print axioms Rank2Cert835.on_curve
#print axioms Rank2Cert835.indep
#print axioms Rank2Cert835.count

bsd/Rank2Cert836.lean

lean_all/bsd/Rank2Cert836.lean

/-
  bsd/Rank2Cert836.lean — T39 cycle 2010 (goal G869): rank ≥ 2 for 0 of the 8 congruent curves in 167801–168000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert836
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert836

#print axioms Rank2Cert836.on_curve
#print axioms Rank2Cert836.indep
#print axioms Rank2Cert836.count

bsd/Rank2Cert837.lean

lean_all/bsd/Rank2Cert837.lean

/-
  bsd/Rank2Cert837.lean — T39 cycle 2012 (goal G870): rank ≥ 2 for 1 of the 5 congruent curves in 168001–168200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert837
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (168010, (true, 11159644225, 132, 1137612009910175), (true, 292690739163018001, 57204, 158348045050989267769054201))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert837

#print axioms Rank2Cert837.on_curve
#print axioms Rank2Cert837.indep
#print axioms Rank2Cert837.count

bsd/Rank2Cert838.lean

lean_all/bsd/Rank2Cert838.lean

/-
  bsd/Rank2Cert838.lean — T39 cycle 2014 (goal G870): rank ≥ 2 for 1 of the 2 congruent curves in 168201–168400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert838
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (168305, (false, 94864, 1, 42819084), (true, 747225, 2, 280264725))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert838

#print axioms Rank2Cert838.on_curve
#print axioms Rank2Cert838.indep
#print axioms Rank2Cert838.count

bsd/Rank2Cert839.lean

lean_all/bsd/Rank2Cert839.lean

/-
  bsd/Rank2Cert839.lean — T39 cycle 2016 (goal G870): rank ≥ 2 for 1 of the 4 congruent curves in 168401–168600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert839
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (168586, (true, 206998, 1, 54647472), (true, 63355798, 7, 499984096128))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert839

#print axioms Rank2Cert839.on_curve
#print axioms Rank2Cert839.indep
#print axioms Rank2Cert839.count

bsd/Rank2Cert84.lean

lean_all/bsd/Rank2Cert84.lean

/-
  bsd/Rank2Cert84.lean — T39 cycle 432 (goal G147): rank ≥ 2 for 8 of the 14 congruent curves in 17401–17600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert84
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (17401, (true, 8077725035107225, 316032, 708992414482389359561405), (true, 27448179448356241, 1071403, 3118979639687407433700600)),
   (17402, (true, 19602, 1, 1263240), (true, 5056524, 5, 11328299268)),
   (17435, (true, 22275, 1, 2069100), (true, 165929949025, 1212, 66780759338155025)),
   (17458, (false, 22743, 2, 9957045), (false, 472916150, 831, 261971599190840)),
   (17459, (true, 628849, 6, 16030495), (true, 31213, 1, 4571112)),
   (17490, (true, 70225, 2, 1615175), (true, 18150, 1, 653400)),
   (17561, (false, 13689, 1, 1287000), (true, 100793, 1, 31510248)),
   (17570, (true, 6509405, 19, 3733722825), (false, 148526, 3, 20914768))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 8 := rfl

end Rank2Cert84

#print axioms Rank2Cert84.on_curve
#print axioms Rank2Cert84.indep
#print axioms Rank2Cert84.count

bsd/Rank2Cert840.lean

lean_all/bsd/Rank2Cert840.lean

/-
  bsd/Rank2Cert840.lean — T39 cycle 2018 (goal G870): rank ≥ 2 for 1 of the 3 congruent curves in 168601–168800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert840
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (168729, (true, 245977313521, 68, 121994540715829175), (false, 1157703657, 119, 71118185596128))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert840

#print axioms Rank2Cert840.on_curve
#print axioms Rank2Cert840.indep
#print axioms Rank2Cert840.count

bsd/Rank2Cert841.lean

lean_all/bsd/Rank2Cert841.lean

/-
  bsd/Rank2Cert841.lean — T39 cycle 2020 (goal G870): rank ≥ 2 for 4 of the 9 congruent curves in 168801–169000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert841
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (168810, (true, 26294640, 7, 127989160200), (false, 87584850, 23, 163032240600)),
   (168889, (true, 297689, 1, 133752360), (true, 6664599769, 127, 496556043466680)),
   (168929, (true, 3396753, 4, 3791650233), (true, 69449238971641, 17936, 360362845006880139405)),
   (168961, (false, 136161, 1, 36914760), (true, 1148797880754241, 30063, 38591737893866276742520))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert841

#print axioms Rank2Cert841.on_curve
#print axioms Rank2Cert841.indep
#print axioms Rank2Cert841.count

bsd/Rank2Cert842.lean

lean_all/bsd/Rank2Cert842.lean

/-
  bsd/Rank2Cert842.lean — T39 cycle 2022 (goal G870): rank ≥ 2 for 1 of the 4 congruent curves in 169001–169200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert842
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (169162, (true, 207088, 1, 54360600), (true, 1290478169, 10, 46354155883197))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert842

#print axioms Rank2Cert842.on_curve
#print axioms Rank2Cert842.indep
#print axioms Rank2Cert842.count

bsd/Rank2Cert843.lean

lean_all/bsd/Rank2Cert843.lean

/-
  bsd/Rank2Cert843.lean — T39 cycle 2024 (goal G870): rank ≥ 2 for 4 of the 9 congruent curves in 169201–169400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert843
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (169210, (false, 90000, 1, 42987000), (true, 205299610, 31, 1795651611600)),
   (169323, (true, 1383123, 1, 1614403980), (false, 161058975, 38, 2334623524695)),
   (169330, (true, 411845, 1, 240929325), (true, 174951756, 31, 849857313396)),
   (169395, (true, 1166445, 1, 1246429800), (false, 62929725, 137, 25216485100200))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert843

#print axioms Rank2Cert843.on_curve
#print axioms Rank2Cert843.indep
#print axioms Rank2Cert843.count

bsd/Rank2Cert844.lean

lean_all/bsd/Rank2Cert844.lean

/-
  bsd/Rank2Cert844.lean — T39 cycle 2026 (goal G870): rank ≥ 2 for 2 of the 7 congruent curves in 169401–169600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert844
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (169505, (false, 19529905, 11, 27685635900), (false, 32166945, 17, 209542719000)),
   (169554, (false, 996611, 3, 1153635175), (false, 1441524, 5, 4785980724))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert844

#print axioms Rank2Cert844.on_curve
#print axioms Rank2Cert844.indep
#print axioms Rank2Cert844.count

bsd/Rank2Cert845.lean

lean_all/bsd/Rank2Cert845.lean

/-
  bsd/Rank2Cert845.lean — T39 cycle 2028 (goal G870): rank ≥ 2 for 0 of the 9 congruent curves in 169601–169800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert845
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert845

#print axioms Rank2Cert845.on_curve
#print axioms Rank2Cert845.indep
#print axioms Rank2Cert845.count

bsd/Rank2Cert846.lean

lean_all/bsd/Rank2Cert846.lean

/-
  bsd/Rank2Cert846.lean — T39 cycle 2030 (goal G870): rank ≥ 2 for 3 of the 7 congruent curves in 169801–170000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert846
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (169898, (true, 4369833, 2, 9023618955), (true, 424948746061225, 2236, 8759997060576732327915)),
   (169905, (true, 19850929, 8, 73994787433), (true, 65100969705, 149, 16582535830648800)),
   (169945, (true, 54128241, 16, 236931370761), (false, 6959504, 573, 147199649723756))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert846

#print axioms Rank2Cert846.on_curve
#print axioms Rank2Cert846.indep
#print axioms Rank2Cert846.count

bsd/Rank2Cert847.lean

lean_all/bsd/Rank2Cert847.lean

/-
  bsd/Rank2Cert847.lean — T39 cycle 2034 (goal G871): rank ≥ 2 for 5 of the 8 congruent curves in 170001–170200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert847
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (170027, (true, 3553225, 4, 4308640635), (false, 7970384708, 4911, 366097347292246570)),
   (170051, (true, 10882036489, 90, 1126050924601763), (true, 2986911900, 107, 123799635105090)),
   (170065, (true, 134521415, 19, 1388261002800), (true, 10365886969, 3, 1055382038469928)),
   (170138, (true, 102774742225, 534, 29045881797990535), (true, 3407544913, 132, 98082414601255)),
   (170185, (true, 31889840057881, 8903, 163180620848820598296), (true, 74685287464225, 20491, 187698672965051896200))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 5 := rfl

end Rank2Cert847

#print axioms Rank2Cert847.on_curve
#print axioms Rank2Cert847.indep
#print axioms Rank2Cert847.count

bsd/Rank2Cert848.lean

lean_all/bsd/Rank2Cert848.lean

/-
  bsd/Rank2Cert848.lean — T39 cycle 2036 (goal G871): rank ≥ 2 for 0 of the 5 congruent curves in 170201–170400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert848
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert848

#print axioms Rank2Cert848.on_curve
#print axioms Rank2Cert848.indep
#print axioms Rank2Cert848.count

bsd/Rank2Cert849.lean

lean_all/bsd/Rank2Cert849.lean

/-
  bsd/Rank2Cert849.lean — T39 cycle 2038 (goal G871): rank ≥ 2 for 3 of the 6 congruent curves in 170401–170600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert849
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (170409, (true, 11255344041, 1, 1194093025794720), (true, 1774611849, 31, 74438560472760)),
   (170555, (true, 172480, 1, 10672200), (false, 12896190720, 1427, 39413365299042000)),
   (170578, (true, 105262777855225, 24624, 200734589283808359565), (false, 11775019408, 5233, 506878562025357720))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert849

#print axioms Rank2Cert849.on_curve
#print axioms Rank2Cert849.indep
#print axioms Rank2Cert849.count

bsd/Rank2Cert85.lean

lean_all/bsd/Rank2Cert85.lean

/-
  bsd/Rank2Cert85.lean — T39 cycle 433 (goal G148): rank ≥ 2 for 6 of the 7 congruent curves in 17601–17800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert85
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (17641, (true, 80063, 1, 22097388), (true, 17324890921, 366, 2259059571545075)),
   (17731, (true, 457436, 5, 76384434), (true, 16649378533225, 3286, 67931049621826550445)),
   (17753, (false, 41984, 5, 90531936), (true, 491996761, 20, 10911843379491)),
   (17754, (true, 6830934, 5, 17815639248), (true, 934626, 5, 795170376)),
   (17777, (true, 36679847719321, 45290, 24070046663321180619), (true, 4254916935025, 13566, 5611598346625376545)),
   (17794, (true, 33282, 1, 5131104), (false, 6975, 4, 23770335))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 6 := rfl

end Rank2Cert85

#print axioms Rank2Cert85.on_curve
#print axioms Rank2Cert85.indep
#print axioms Rank2Cert85.count

bsd/Rank2Cert850.lean

lean_all/bsd/Rank2Cert850.lean

/-
  bsd/Rank2Cert850.lean — T39 cycle 2040 (goal G871): rank ≥ 2 for 2 of the 6 congruent curves in 170601–170800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert850
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (170601, (false, 81225, 1, 42756840), (true, 303753, 1, 138511368)),
   (170794, (true, 516906, 1, 350762880), (false, 35594, 1, 31515120))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert850

#print axioms Rank2Cert850.on_curve
#print axioms Rank2Cert850.indep
#print axioms Rank2Cert850.count

bsd/Rank2Cert851.lean

lean_all/bsd/Rank2Cert851.lean

/-
  bsd/Rank2Cert851.lean — T39 cycle 2042 (goal G871): rank ≥ 2 for 3 of the 7 congruent curves in 170801–171000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert851
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (170833, (true, 61295051233, 174, 15121207222877015), (true, 57229225, 16, 279248653035)),
   (170913, (true, 174087, 1, 13806900), (true, 1667307867834903121, 3060605, 601134475634381955913908956)),
   (170969, (true, 1660894605049, 127, 2140486598336561160), (true, 5480378270798929, 49797, 404494281607508648438120))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert851

#print axioms Rank2Cert851.on_curve
#print axioms Rank2Cert851.indep
#print axioms Rank2Cert851.count

bsd/Rank2Cert852.lean

lean_all/bsd/Rank2Cert852.lean

/-
  bsd/Rank2Cert852.lean — T39 cycle 2044 (goal G871): rank ≥ 2 for 3 of the 7 congruent curves in 171001–171200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert852
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (171105, (true, 95989905, 7, 936861472800), (false, 3864729, 43, 621908838144)),
   (171145, (true, 2761329975625, 1617, 4527917081606269000), (true, 29115130974349225, 411432, 494070008128007408744725)),
   (171186, (false, 1685268, 31, 213551845884), (false, 1467453566, 115, 66039716613248))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert852

#print axioms Rank2Cert852.on_curve
#print axioms Rank2Cert852.indep
#print axioms Rank2Cert852.count

bsd/Rank2Cert853.lean

lean_all/bsd/Rank2Cert853.lean

/-
  bsd/Rank2Cert853.lean — T39 cycle 2046 (goal G871): rank ≥ 2 for 2 of the 12 congruent curves in 171201–171400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert853
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (171210, (true, 204490, 1, 50564800), (true, 120257904, 7, 1315561340808)),
   (171395, (false, 109409895, 26, 398811643125), (false, 335968479, 4538, 64695845968734519))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert853

#print axioms Rank2Cert853.on_curve
#print axioms Rank2Cert853.indep
#print axioms Rank2Cert853.count

bsd/Rank2Cert854.lean

lean_all/bsd/Rank2Cert854.lean

/-
  bsd/Rank2Cert854.lean — T39 cycle 2048 (goal G871): rank ≥ 2 for 1 of the 4 congruent curves in 171401–171600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert854
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (171419, (true, 3047262364, 13, 168207418278990), (true, 128421159853, 333, 45513949492230128))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert854

#print axioms Rank2Cert854.on_curve
#print axioms Rank2Cert854.indep
#print axioms Rank2Cert854.count

bsd/Rank2Cert855.lean

lean_all/bsd/Rank2Cert855.lean

/-
  bsd/Rank2Cert855.lean — T39 cycle 2050 (goal G871): rank ≥ 2 for 0 of the 5 congruent curves in 171601–171800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert855
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert855

#print axioms Rank2Cert855.on_curve
#print axioms Rank2Cert855.indep
#print axioms Rank2Cert855.count

bsd/Rank2Cert856.lean

lean_all/bsd/Rank2Cert856.lean

/-
  bsd/Rank2Cert856.lean — T39 cycle 2052 (goal G871): rank ≥ 2 for 3 of the 9 congruent curves in 171801–172000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert856
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (171930, (false, 4068020, 7, 14879527300), (true, 845680, 1, 761453000)),
   (171969, (true, 1705953, 1, 2216831688), (false, 40602639, 16, 108426229065)),
   (171995, (true, 3553225, 2, 6571081725), (true, 3208005, 1, 5737563000))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert856

#print axioms Rank2Cert856.on_curve
#print axioms Rank2Cert856.indep
#print axioms Rank2Cert856.count

bsd/Rank2Cert857.lean

lean_all/bsd/Rank2Cert857.lean

/-
  bsd/Rank2Cert857.lean — T39 cycle 2054 (goal G872): rank ≥ 2 for 1 of the 7 congruent curves in 172001–172200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert857
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (172081, (true, 136460233, 24, 1095639210757), (true, 6172586211961, 1188, 15323724022664679245))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert857

#print axioms Rank2Cert857.on_curve
#print axioms Rank2Cert857.indep
#print axioms Rank2Cert857.count

bsd/Rank2Cert858.lean

lean_all/bsd/Rank2Cert858.lean

/-
  bsd/Rank2Cert858.lean — T39 cycle 2056 (goal G872): rank ≥ 2 for 0 of the 3 congruent curves in 172201–172400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert858
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert858

#print axioms Rank2Cert858.on_curve
#print axioms Rank2Cert858.indep
#print axioms Rank2Cert858.count

bsd/Rank2Cert859.lean

lean_all/bsd/Rank2Cert859.lean

/-
  bsd/Rank2Cert859.lean — T39 cycle 2058 (goal G872): rank ≥ 2 for 1 of the 7 congruent curves in 172401–172600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert859
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (172515, (true, 230640, 1, 73516500), (true, 610814400, 59, 2759234070600))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert859

#print axioms Rank2Cert859.on_curve
#print axioms Rank2Cert859.indep
#print axioms Rank2Cert859.count

bsd/Rank2Cert86.lean

lean_all/bsd/Rank2Cert86.lean

/-
  bsd/Rank2Cert86.lean — T39 cycle 436 (goal G151): rank ≥ 2 for 4 of the 8 congruent curves in 17801–18000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert86
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (17810, (true, 1221025, 2, 1346934225), (true, 10979865, 14, 34495075875)),
   (17841, (false, 467856, 29, 10257937740), (true, 2537697, 7, 3795143904)),
   (17906, (true, 21294, 1, 1681680), (false, 7488598718, 1107, 1784862607856896)),
   (17985, (true, 297025, 1, 161581600), (true, 57660, 1, 13154850))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert86

#print axioms Rank2Cert86.on_curve
#print axioms Rank2Cert86.indep
#print axioms Rank2Cert86.count

bsd/Rank2Cert860.lean

lean_all/bsd/Rank2Cert860.lean

/-
  bsd/Rank2Cert860.lean — T39 cycle 2060 (goal G872): rank ≥ 2 for 2 of the 7 congruent curves in 172601–172800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert860
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (172601, (true, 1221025, 2, 1112842185), (true, 10369337, 2, 33316679781)),
   (172777, (true, 286754337, 16, 4797733974705), (true, 2808981450030625, 71008, 141534820346682355803825))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert860

#print axioms Rank2Cert860.on_curve
#print axioms Rank2Cert860.indep
#print axioms Rank2Cert860.count

bsd/Rank2Cert861.lean

lean_all/bsd/Rank2Cert861.lean

/-
  bsd/Rank2Cert861.lean — T39 cycle 2062 (goal G872): rank ≥ 2 for 0 of the 5 congruent curves in 172801–173000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert861
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert861

#print axioms Rank2Cert861.on_curve
#print axioms Rank2Cert861.indep
#print axioms Rank2Cert861.count

bsd/Rank2Cert862.lean

lean_all/bsd/Rank2Cert862.lean

/-
  bsd/Rank2Cert862.lean — T39 cycle 2064 (goal G872): rank ≥ 2 for 1 of the 2 congruent curves in 173001–173200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert862
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (173130, (false, 150, 1, 2120400), (false, 1821606, 5, 5299147728))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert862

#print axioms Rank2Cert862.on_curve
#print axioms Rank2Cert862.indep
#print axioms Rank2Cert862.count

bsd/Rank2Cert863.lean

lean_all/bsd/Rank2Cert863.lean

/-
  bsd/Rank2Cert863.lean — T39 cycle 2066 (goal G872): rank ≥ 2 for 2 of the 9 congruent curves in 173201–173400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert863
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (173201, (true, 292854769, 30, 4242680304353), (false, 660585867527, 2977, 1126154689046448732)),
   (173355, (true, 62903100, 7, 494324011350), (true, 286209105, 8, 4838364920025))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert863

#print axioms Rank2Cert863.on_curve
#print axioms Rank2Cert863.indep
#print axioms Rank2Cert863.count

bsd/Rank2Cert864.lean

lean_all/bsd/Rank2Cert864.lean

/-
  bsd/Rank2Cert864.lean — T39 cycle 2068 (goal G872): rank ≥ 2 for 2 of the 6 congruent curves in 173401–173600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert864
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (173530, (false, 104358870, 67, 7885988695200), (true, 4650604, 5, 3613519308)),
   (173545, (true, 98747105, 23, 361414404300), (true, 2416291687883779225, 3124692, 2677675241144800680698462275))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert864

#print axioms Rank2Cert864.on_curve
#print axioms Rank2Cert864.indep
#print axioms Rank2Cert864.count

bsd/Rank2Cert865.lean

lean_all/bsd/Rank2Cert865.lean

/-
  bsd/Rank2Cert865.lean — T39 cycle 2070 (goal G872): rank ≥ 2 for 2 of the 8 congruent curves in 173601–173800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert865
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (173706, (true, 100391850, 23, 405101193120), (true, 13742817, 8, 29950595631)),
   (173755, (true, 18019228645, 27, 2418764375505500), (true, 122434225, 22, 984583782975))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert865

#print axioms Rank2Cert865.on_curve
#print axioms Rank2Cert865.indep
#print axioms Rank2Cert865.count

bsd/Rank2Cert866.lean

lean_all/bsd/Rank2Cert866.lean

/-
  bsd/Rank2Cert866.lean — T39 cycle 2072 (goal G872): rank ≥ 2 for 4 of the 8 congruent curves in 173801–174000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert866
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (173810, (true, 510734, 1, 343213248), (true, 851764225, 58, 18076612742175)),
   (173859, (true, 257091, 1, 96028920), (false, 25036697759, 380, 293337077549039)),
   (173881, (true, 3616569, 1, 6869776320), (true, 252346219097281, 20493, 3837112861901551284680)),
   (173994, (false, 7557647, 12, 65671221833), (true, 6903454, 5, 14084778392))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert866

#print axioms Rank2Cert866.on_curve
#print axioms Rank2Cert866.indep
#print axioms Rank2Cert866.count

bsd/Rank2Cert867.lean

lean_all/bsd/Rank2Cert867.lean

/-
  bsd/Rank2Cert867.lean — T39 cycle 2074 (goal G873): rank ≥ 2 for 0 of the 4 congruent curves in 174001–174200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert867
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert867

#print axioms Rank2Cert867.on_curve
#print axioms Rank2Cert867.indep
#print axioms Rank2Cert867.count

bsd/Rank2Cert868.lean

lean_all/bsd/Rank2Cert868.lean

/-
  bsd/Rank2Cert868.lean — T39 cycle 2076 (goal G873): rank ≥ 2 for 6 of the 14 congruent curves in 174201–174400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert868
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (174202, (false, 136367, 2, 252340935), (false, 38624607, 172, 32028003684495)),
   (174233, (false, 3579664, 5, 4695522084), (true, 408565369, 15, 8220228014528)),
   (174234, (true, 263538, 1, 101503512), (true, 26541717, 11, 83071138059)),
   (174265, (true, 309465, 1, 142264200), (true, 116407204225, 764, 19312788083455425)),
   (174369, (false, 752043503, 124, 70573155670009), (true, 14207924809, 206, 1445738189379965)),
   (174370, (true, 179564, 1, 18168612), (false, 15933543170, 1799, 71206083692858400))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 6 := rfl

end Rank2Cert868

#print axioms Rank2Cert868.on_curve
#print axioms Rank2Cert868.indep
#print axioms Rank2Cert868.count

bsd/Rank2Cert869.lean

lean_all/bsd/Rank2Cert869.lean

/-
  bsd/Rank2Cert869.lean — T39 cycle 2078 (goal G873): rank ≥ 2 for 3 of the 7 congruent curves in 174401–174600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert869
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (174426, (false, 117978, 1, 44128224), (true, 217629401514, 395, 100728836015987088)),
   (174529, (true, 6061712449, 34, 471685263095865), (true, 445414144796569, 36206, 8065552684038302211915)),
   (174585, (false, 381924, 13, 18232478874), (true, 178215, 1, 15107400))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert869

#print axioms Rank2Cert869.on_curve
#print axioms Rank2Cert869.indep
#print axioms Rank2Cert869.count

bsd/Rank2Cert87.lean

lean_all/bsd/Rank2Cert87.lean

/-
  bsd/Rank2Cert87.lean — T39 cycle 437 (goal G154): rank ≥ 2 for 3 of the 6 congruent curves in 18001–18200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert87
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (18042, (true, 1564513, 8, 1320412015), (true, 37648513, 44, 86197871935)),
   (18051, (false, 1829883, 13, 3301830576), (false, 135168, 7, 321368256)),
   (18161, (true, 105625, 1, 33816900), (true, 2641610972, 211, 129253643184666))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert87

#print axioms Rank2Cert87.on_curve
#print axioms Rank2Cert87.indep
#print axioms Rank2Cert87.count

bsd/Rank2Cert870.lean

lean_all/bsd/Rank2Cert870.lean

/-
  bsd/Rank2Cert870.lean — T39 cycle 2080 (goal G873): rank ≥ 2 for 1 of the 4 congruent curves in 174601–174800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert870
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (174777, (false, 2304324, 5, 5635433826), (false, 18813968, 21, 324207858500))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert870

#print axioms Rank2Cert870.on_curve
#print axioms Rank2Cert870.indep
#print axioms Rank2Cert870.count

bsd/Rank2Cert871.lean

lean_all/bsd/Rank2Cert871.lean

/-
  bsd/Rank2Cert871.lean — T39 cycle 2082 (goal G873): rank ≥ 2 for 3 of the 5 congruent curves in 174801–175000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert871
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (174865, (true, 727609, 1, 602460252), (true, 35690049, 8, 202462838943)),
   (174867, (false, 426730128, 79, 20749531158180), (true, 78775146501501025, 657098, 6305487445667313769160105)),
   (174986, (true, 201898, 1, 45252528), (false, 7399350, 169, 13594806242640))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert871

#print axioms Rank2Cert871.on_curve
#print axioms Rank2Cert871.indep
#print axioms Rank2Cert871.count

bsd/Rank2Cert872.lean

lean_all/bsd/Rank2Cert872.lean

/-
  bsd/Rank2Cert872.lean — T39 cycle 2084 (goal G873): rank ≥ 2 for 2 of the 4 congruent curves in 175001–175200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert872
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (175026, (true, 82349733, 17, 589706438013), (false, 256251518, 83, 18860613892064)),
   (175130, (true, 1687805, 1, 2180885175), (true, 7390486, 3, 19629130816))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert872

#print axioms Rank2Cert872.on_curve
#print axioms Rank2Cert872.indep
#print axioms Rank2Cert872.count

bsd/Rank2Cert873.lean

lean_all/bsd/Rank2Cert873.lean

/-
  bsd/Rank2Cert873.lean — T39 cycle 2086 (goal G873): rank ≥ 2 for 2 of the 5 congruent curves in 175201–175400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert873
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (175234, (false, 73984, 1, 43207200), (true, 4287237529, 100, 256195948313667)),
   (175362, (false, 2938386, 5, 5577195888), (true, 1058802875625, 2182, 670001867091213525))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert873

#print axioms Rank2Cert873.on_curve
#print axioms Rank2Cert873.indep
#print axioms Rank2Cert873.count

bsd/Rank2Cert874.lean

lean_all/bsd/Rank2Cert874.lean

/-
  bsd/Rank2Cert874.lean — T39 cycle 2088 (goal G873): rank ≥ 2 for 2 of the 7 congruent curves in 175401–175600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert874
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (175570, (true, 158890850, 29, 739795797600), (true, 470304306, 47, 5769213557904)),
   (175595, (true, 63014845180, 279, 15441855104025350), (true, 210991573751401, 31694, 1681774841103335033901))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert874

#print axioms Rank2Cert874.on_curve
#print axioms Rank2Cert874.indep
#print axioms Rank2Cert874.count

bsd/Rank2Cert875.lean

lean_all/bsd/Rank2Cert875.lean

/-
  bsd/Rank2Cert875.lean — T39 cycle 2090 (goal G873): rank ≥ 2 for 2 of the 7 congruent curves in 175601–175800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert875
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (175674, (false, 3505683, 7, 14720064561), (true, 559728, 1, 397600632)),
   (175729, (true, 4199153, 1, 8597296848), (true, 1634643678679104025, 2306691, 1714278078863197949443513960))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert875

#print axioms Rank2Cert875.on_curve
#print axioms Rank2Cert875.indep
#print axioms Rank2Cert875.count

bsd/Rank2Cert876.lean

lean_all/bsd/Rank2Cert876.lean

/-
  bsd/Rank2Cert876.lean — T39 cycle 2092 (goal G873): rank ≥ 2 for 4 of the 15 congruent curves in 175801–176000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert876
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (175827, (true, 222163548, 1, 3311380354050), (false, 271222500, 121, 42159481758450)),
   (175849, (true, 11215801, 7, 24044882280), (false, 39428879936, 1155, 45918567076344488)),
   (175890, (false, 156816, 1, 31546152), (true, 206310, 1, 48976200)),
   (175921, (false, 6125087, 98, 4181413464663), (true, 20732185734121, 3818, 93674120691588585645))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert876

#print axioms Rank2Cert876.on_curve
#print axioms Rank2Cert876.indep
#print axioms Rank2Cert876.count

bsd/Rank2Cert877.lean

lean_all/bsd/Rank2Cert877.lean

/-
  bsd/Rank2Cert877.lean — T39 cycle 2100 (goal G874): rank ≥ 2 for 1 of the 5 congruent curves in 176001–176200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert877
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (176154, (true, 907137, 2, 544152609), (true, 10269078922, 197, 776518754777344))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert877

#print axioms Rank2Cert877.on_curve
#print axioms Rank2Cert877.indep
#print axioms Rank2Cert877.count

bsd/Rank2Cert878.lean

lean_all/bsd/Rank2Cert878.lean

/-
  bsd/Rank2Cert878.lean — T39 cycle 2102 (goal G874): rank ≥ 2 for 1 of the 9 congruent curves in 176201–176400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert878
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (176330, (true, 4596606, 5, 2792201496), (true, 18354350, 1, 78630035400))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert878

#print axioms Rank2Cert878.on_curve
#print axioms Rank2Cert878.indep
#print axioms Rank2Cert878.count

bsd/Rank2Cert879.lean

lean_all/bsd/Rank2Cert879.lean

/-
  bsd/Rank2Cert879.lean — T39 cycle 2104 (goal G874): rank ≥ 2 for 1 of the 7 congruent curves in 176401–176600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert879
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (176561, (true, 15208732, 1, 59307585414), (true, 804930039521521, 50394, 18965564650723947961825))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert879

#print axioms Rank2Cert879.on_curve
#print axioms Rank2Cert879.indep
#print axioms Rank2Cert879.count

bsd/Rank2Cert88.lean

lean_all/bsd/Rank2Cert88.lean

/-
  bsd/Rank2Cert88.lean — T39 cycle 438 (goal G155): rank ≥ 2 for 6 of the 8 congruent curves in 18201–18400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert88
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (18219, (false, 2742336, 13, 2318342040), (true, 53067, 1, 11481624)),
   (18265, (false, 451584, 5, 45469536), (true, 68954922985, 1538, 14112063809066925)),
   (18299, (true, 15923349, 7, 63439825800), (true, 16356029, 23, 53318912220)),
   (18330, (true, 25328394, 37, 17310148128), (true, 11199630, 19, 30239001000)),
   (18354, (true, 212268, 1, 97431012), (true, 24062094, 25, 103755749328)),
   (18377, (true, 931225, 7, 229029240), (true, 3080192, 11, 3740808960))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 6 := rfl

end Rank2Cert88

#print axioms Rank2Cert88.on_curve
#print axioms Rank2Cert88.indep
#print axioms Rank2Cert88.count

bsd/Rank2Cert880.lean

lean_all/bsd/Rank2Cert880.lean

/-
  bsd/Rank2Cert880.lean — T39 cycle 2106 (goal G874): rank ≥ 2 for 2 of the 5 congruent curves in 176601–176800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert880
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (176649, (true, 17744892, 7, 65253613986), (true, 674295527774401, 43657, 15170717527254536471380)),
   (176722, (false, 13363406, 15, 136900694672), (false, 182, 9, 193112920))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert880

#print axioms Rank2Cert880.on_curve
#print axioms Rank2Cert880.indep
#print axioms Rank2Cert880.count

bsd/Rank2Cert881.lean

lean_all/bsd/Rank2Cert881.lean

/-
  bsd/Rank2Cert881.lean — T39 cycle 2108 (goal G874): rank ≥ 2 for 2 of the 8 congruent curves in 176801–177000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert881
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (176930, (true, 43280770, 7, 278965190400), (true, 64946427140200321, 13366, 16551328001714489616478431)),
   (176946, (false, 26411, 3, 258771205), (true, 71498039382, 629, 3883866232382136))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert881

#print axioms Rank2Cert881.on_curve
#print axioms Rank2Cert881.indep
#print axioms Rank2Cert881.count

bsd/Rank2Cert882.lean

lean_all/bsd/Rank2Cert882.lean

/-
  bsd/Rank2Cert882.lean — T39 cycle 2110 (goal G874): rank ≥ 2 for 2 of the 7 congruent curves in 177001–177200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert882
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (177123, (true, 728433, 2, 144472545), (true, 7288125, 1, 19669595400)),
   (177177, (true, 252252, 1, 90180090), (false, 414164751, 50, 3196189803807))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert882

#print axioms Rank2Cert882.on_curve
#print axioms Rank2Cert882.indep
#print axioms Rank2Cert882.count

bsd/Rank2Cert883.lean

lean_all/bsd/Rank2Cert883.lean

/-
  bsd/Rank2Cert883.lean — T39 cycle 2112 (goal G874): rank ≥ 2 for 1 of the 7 congruent curves in 177201–177400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert883
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (177385, (true, 12074465, 2, 41884245975), (false, 1694556176, 483, 1702059406241732))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert883

#print axioms Rank2Cert883.on_curve
#print axioms Rank2Cert883.indep
#print axioms Rank2Cert883.count

bsd/Rank2Cert884.lean

lean_all/bsd/Rank2Cert884.lean

/-
  bsd/Rank2Cert884.lean — T39 cycle 2114 (goal G874): rank ≥ 2 for 1 of the 10 congruent curves in 177401–177600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert884
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (177474, (true, 1885026, 1, 2576574000), (true, 2116294653001, 2150, 2837950312103948251))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert884

#print axioms Rank2Cert884.on_curve
#print axioms Rank2Cert884.indep
#print axioms Rank2Cert884.count

bsd/Rank2Cert885.lean

lean_all/bsd/Rank2Cert885.lean

/-
  bsd/Rank2Cert885.lean — T39 cycle 2116 (goal G874): rank ≥ 2 for 1 of the 10 congruent curves in 177601–177800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert885
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (177682, (false, 102335488, 47, 3833232406080), (false, 1855021986, 325, 804364340640912))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert885

#print axioms Rank2Cert885.on_curve
#print axioms Rank2Cert885.indep
#print axioms Rank2Cert885.count

bsd/Rank2Cert886.lean

lean_all/bsd/Rank2Cert886.lean

/-
  bsd/Rank2Cert886.lean — T39 cycle 2118 (goal G874): rank ≥ 2 for 1 of the 4 congruent curves in 177801–178000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert886
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (177905, (true, 3237871, 3, 5064030244), (true, 382867393545, 626, 232943460562469475))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert886

#print axioms Rank2Cert886.on_curve
#print axioms Rank2Cert886.indep
#print axioms Rank2Cert886.count

bsd/Rank2Cert887.lean

lean_all/bsd/Rank2Cert887.lean

/-
  bsd/Rank2Cert887.lean — T39 cycle 2120 (goal G875): rank ≥ 2 for 2 of the 6 congruent curves in 178001–178200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert887
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (178035, (true, 37445265, 4, 228473098425), (true, 192099765, 1, 2662502742900)),
   (178145, (true, 30961601, 10, 140906246151), (true, 1728238890625, 122, 2271983377627494375))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert887

#print axioms Rank2Cert887.on_curve
#print axioms Rank2Cert887.indep
#print axioms Rank2Cert887.count

bsd/Rank2Cert888.lean

lean_all/bsd/Rank2Cert888.lean

/-
  bsd/Rank2Cert888.lean — T39 cycle 2122 (goal G875): rank ≥ 2 for 1 of the 7 congruent curves in 178201–178400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert888
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (178345, (true, 168374941027969, 14247, 2133730407247507114228), (true, 1553482708920025, 18576, 61181284307671591567075))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert888

#print axioms Rank2Cert888.on_curve
#print axioms Rank2Cert888.indep
#print axioms Rank2Cert888.count

bsd/Rank2Cert889.lean

lean_all/bsd/Rank2Cert889.lean

/-
  bsd/Rank2Cert889.lean — T39 cycle 2124 (goal G875): rank ≥ 2 for 2 of the 9 congruent curves in 178401–178600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert889
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (178451, (true, 653013, 1, 507608772), (true, 48344445402289, 3794, 335664928734341468865)),
   (178562, (true, 22941702, 11, 36947064840), (false, 897925687, 82, 23883990860955))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert889

#print axioms Rank2Cert889.on_curve
#print axioms Rank2Cert889.indep
#print axioms Rank2Cert889.count

bsd/Rank2Cert89.lean

lean_all/bsd/Rank2Cert89.lean

/-
  bsd/Rank2Cert89.lean — T39 cycle 439 (goal G156): rank ≥ 2 for 7 of the 11 congruent curves in 18401–18600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert89
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (18410, (true, 33892810, 39, 111168416800), (false, 647150, 19, 5321017800)),
   (18465, (false, 51282015, 79, 739036794600), (true, 13944047865, 362, 1621600421480325)),
   (18497, (true, 3045025, 11, 3602873580), (true, 262901481492169, 7695, 4262709936169114365572)),
   (18506, (true, 27702, 1, 3430944), (true, 96529804, 71, 243693216060)),
   (18530, (true, 297025, 4, 9801825), (true, 2608786, 3, 4205023984)),
   (18546, (true, 19602, 1, 888624), (false, 2441472, 47, 63899767008)),
   (18595, (true, 19845, 1, 976500), (true, 207175177225, 638, 94235935847981475))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 7 := rfl

end Rank2Cert89

#print axioms Rank2Cert89.on_curve
#print axioms Rank2Cert89.indep
#print axioms Rank2Cert89.count

bsd/Rank2Cert890.lean

lean_all/bsd/Rank2Cert890.lean

/-
  bsd/Rank2Cert890.lean — T39 cycle 2126 (goal G875): rank ≥ 2 for 1 of the 5 congruent curves in 178601–178800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert890
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (178689, (false, 668367, 2, 207083457), (false, 392273409, 659, 1536942194173980))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert890

#print axioms Rank2Cert890.on_curve
#print axioms Rank2Cert890.indep
#print axioms Rank2Cert890.count

bsd/Rank2Cert891.lean

lean_all/bsd/Rank2Cert891.lean

/-
  bsd/Rank2Cert891.lean — T39 cycle 2128 (goal G875): rank ≥ 2 for 2 of the 5 congruent curves in 178801–179000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert891
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (178849, (false, 214775, 54, 241693996445), (true, 1497767401, 90, 14718719775149)),
   (178986, (false, 8000067, 7, 10165606263), (true, 77381614, 7, 676315306360))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert891

#print axioms Rank2Cert891.on_curve
#print axioms Rank2Cert891.indep
#print axioms Rank2Cert891.count

bsd/Rank2Cert892.lean

lean_all/bsd/Rank2Cert892.lean

/-
  bsd/Rank2Cert892.lean — T39 cycle 2130 (goal G875): rank ≥ 2 for 0 of the 9 congruent curves in 179001–179200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert892
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert892

#print axioms Rank2Cert892.on_curve
#print axioms Rank2Cert892.indep
#print axioms Rank2Cert892.count

bsd/Rank2Cert893.lean

lean_all/bsd/Rank2Cert893.lean

/-
  bsd/Rank2Cert893.lean — T39 cycle 2132 (goal G875): rank ≥ 2 for 1 of the 5 congruent curves in 179201–179400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert893
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (179265, (true, 1221025, 2, 1092065975), (true, 601065, 1, 444788100))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert893

#print axioms Rank2Cert893.on_curve
#print axioms Rank2Cert893.indep
#print axioms Rank2Cert893.count

bsd/Rank2Cert894.lean

lean_all/bsd/Rank2Cert894.lean

/-
  bsd/Rank2Cert894.lean — T39 cycle 2134 (goal G875): rank ≥ 2 for 1 of the 6 congruent curves in 179401–179600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert894
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (179410, (false, 42350, 9, 2990587600), (true, 264110, 1, 99607200))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert894

#print axioms Rank2Cert894.on_curve
#print axioms Rank2Cert894.indep
#print axioms Rank2Cert894.count

bsd/Rank2Cert895.lean

lean_all/bsd/Rank2Cert895.lean

/-
  bsd/Rank2Cert895.lean — T39 cycle 2136 (goal G875): rank ≥ 2 for 3 of the 9 congruent curves in 179601–179800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert895
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (179745, (false, 23142945, 19, 291627913500), (true, 14169389850625, 3794, 52440037522071601625)),
   (179746, (false, 4903983, 8, 23044261401), (true, 4455133, 1, 9395875497)),
   (179795, (false, 73205, 3, 437366600), (false, 1514051, 3, 702633932))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert895

#print axioms Rank2Cert895.on_curve
#print axioms Rank2Cert895.indep
#print axioms Rank2Cert895.count

bsd/Rank2Cert896.lean

lean_all/bsd/Rank2Cert896.lean

/-
  bsd/Rank2Cert896.lean — T39 cycle 2138 (goal G875): rank ≥ 2 for 4 of the 8 congruent curves in 179801–180000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert896
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (179809, (true, 3898559, 1, 7689423000), (true, 411692400, 47, 2197000698060)),
   (179914, (false, 21602531, 155, 20089783207047), (true, 1934534302, 93, 50553103625704)),
   (179945, (true, 198505, 1, 37340400), (true, 37862545, 2, 232935785775)),
   (179970, (true, 15423429, 1, 60567805683), (false, 2228442531, 947, 7618323215926647))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert896

#print axioms Rank2Cert896.on_curve
#print axioms Rank2Cert896.indep
#print axioms Rank2Cert896.count

bsd/Rank2Cert897.lean

lean_all/bsd/Rank2Cert897.lean

/-
  bsd/Rank2Cert897.lean — T39 cycle 2142 (goal G876): rank ≥ 2 for 2 of the 6 congruent curves in 180001–180200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert897
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (180057, (false, 436357823, 56, 7486122302945), (true, 1974991548529129, 35955, 87158625235851798094492)),
   (180115, (true, 2254421, 1, 3374131644), (true, 5422736570329, 434, 12627554799400341333))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert897

#print axioms Rank2Cert897.on_curve
#print axioms Rank2Cert897.indep
#print axioms Rank2Cert897.count

bsd/Rank2Cert898.lean

lean_all/bsd/Rank2Cert898.lean

/-
  bsd/Rank2Cert898.lean — T39 cycle 2144 (goal G876): rank ≥ 2 for 3 of the 6 congruent curves in 180201–180400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert898
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (180219, (false, 46656, 1, 37600200), (true, 77115049297, 358, 20431389395431729)),
   (180251, (true, 485394493131, 859, 325232383282310100), (true, 1662471004, 15, 67764439345742)),
   (180265, (true, 7876133538025, 3963, 20626548238165849100), (false, 711564111, 64, 5258460711063))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert898

#print axioms Rank2Cert898.on_curve
#print axioms Rank2Cert898.indep
#print axioms Rank2Cert898.count

bsd/Rank2Cert899.lean

lean_all/bsd/Rank2Cert899.lean

/-
  bsd/Rank2Cert899.lean — T39 cycle 2146 (goal G876): rank ≥ 2 for 2 of the 6 congruent curves in 180401–180600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert899
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (180435, (true, 9172860, 7, 7401688650), (false, 855200835, 131, 87029734065300)),
   (180570, (true, 167207820, 17, 2054148068700), (false, 315122430, 2819, 25472724452443800))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert899

#print axioms Rank2Cert899.on_curve
#print axioms Rank2Cert899.indep
#print axioms Rank2Cert899.count

bsd/Rank2Cert9.lean

lean_all/bsd/Rank2Cert9.lean

/-
  bsd/Rank2Cert9.lean — T39 cycle 332 (goal G67): rank ≥ 2 for 5 of the 6 congruent curves in 2401–2600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert9
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (2418, (true, 11532, 1, 1210860), (true, 12005773, 69, 11806322255)),
   (2465, (true, 21025, 1, 3027600), (true, 3690241, 33, 4864325464)),
   (2530, (true, 2645, 1, 39675), (true, 209484, 5, 91404852)),
   (2569, (true, 3703, 1, 162288), (true, 60333605641, 3983, 10927439660092920)),
   (2594, (true, 1682209, 6, 2178460655), (false, 5014210082, 2281, 887296243658832))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 5 := rfl

end Rank2Cert9

#print axioms Rank2Cert9.on_curve
#print axioms Rank2Cert9.indep
#print axioms Rank2Cert9.count

bsd/Rank2Cert90.lean

lean_all/bsd/Rank2Cert90.lean

/-
  bsd/Rank2Cert90.lean — T39 cycle 440 (goal G157): rank ≥ 2 for 5 of the 7 congruent curves in 18601–18800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert90
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (18618, (true, 1717350, 1, 2250415440), (false, 917957387, 1303, 957304085316965)),
   (18705, (true, 158025, 2, 55331325), (true, 13127169, 26, 12777508953)),
   (18722, (false, 376475, 59, 39986840355), (true, 7065853, 1, 18782152935)),
   (18746, (true, 36386077, 9, 219292796359), (true, 28350, 1, 3580920)),
   (18761, (false, 1199025, 29, 17226951720), (true, 236257273, 57, 3508488006128))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 5 := rfl

end Rank2Cert90

#print axioms Rank2Cert90.on_curve
#print axioms Rank2Cert90.indep
#print axioms Rank2Cert90.count

bsd/Rank2Cert900.lean

lean_all/bsd/Rank2Cert900.lean

/-
  bsd/Rank2Cert900.lean — T39 cycle 2148 (goal G876): rank ≥ 2 for 4 of the 6 congruent curves in 180601–180800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert900
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (180635, (false, 176400, 1, 16331700), (true, 78011283025, 652, 3842467567496025)),
   (180642, (true, 629442, 1, 478375920), (true, 855393, 2, 423419535)),
   (180705, (true, 310993305, 41, 1175554692900), (true, 156326634394225, 8074, 1949004734736905877025)),
   (180785, (true, 256025, 1, 91730100), (false, 148878224, 29, 376372793676))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert900

#print axioms Rank2Cert900.on_curve
#print axioms Rank2Cert900.indep
#print axioms Rank2Cert900.count

bsd/Rank2Cert901.lean

lean_all/bsd/Rank2Cert901.lean

/-
  bsd/Rank2Cert901.lean — T39 cycle 2150 (goal G876): rank ≥ 2 for 0 of the 3 congruent curves in 180801–181000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert901
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert901

#print axioms Rank2Cert901.on_curve
#print axioms Rank2Cert901.indep
#print axioms Rank2Cert901.count

bsd/Rank2Cert902.lean

lean_all/bsd/Rank2Cert902.lean

/-
  bsd/Rank2Cert902.lean — T39 cycle 2152 (goal G876): rank ≥ 2 for 2 of the 6 congruent curves in 181001–181200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert902
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (181034, (true, 502768, 1, 332581032), (true, 7687134, 5, 17227603848)),
   (181041, (true, 207088, 1, 45755612), (true, 67586407161, 145, 17542811468443716))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert902

#print axioms Rank2Cert902.on_curve
#print axioms Rank2Cert902.indep
#print axioms Rank2Cert902.count

bsd/Rank2Cert903.lean

lean_all/bsd/Rank2Cert903.lean

/-
  bsd/Rank2Cert903.lean — T39 cycle 2154 (goal G876): rank ≥ 2 for 0 of the 2 congruent curves in 181201–181400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert903
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert903

#print axioms Rank2Cert903.on_curve
#print axioms Rank2Cert903.indep
#print axioms Rank2Cert903.count

bsd/Rank2Cert904.lean

lean_all/bsd/Rank2Cert904.lean

/-
  bsd/Rank2Cert904.lean — T39 cycle 2156 (goal G876): rank ≥ 2 for 0 of the 5 congruent curves in 181401–181600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert904
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert904

#print axioms Rank2Cert904.on_curve
#print axioms Rank2Cert904.indep
#print axioms Rank2Cert904.count

bsd/Rank2Cert905.lean

lean_all/bsd/Rank2Cert905.lean

/-
  bsd/Rank2Cert905.lean — T39 cycle 2158 (goal G876): rank ≥ 2 for 2 of the 10 congruent curves in 181601–181800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert905
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (181649, (true, 15808004225, 61, 1985720896716180), (true, 82217905973281, 11234, 715937636030619787785)),
   (181769, (true, 236992, 1, 74030376), (true, 2028521461299975625, 1453068, 2836965577055055308660158075))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert905

#print axioms Rank2Cert905.on_curve
#print axioms Rank2Cert905.indep
#print axioms Rank2Cert905.count

bsd/Rank2Cert906.lean

lean_all/bsd/Rank2Cert906.lean

/-
  bsd/Rank2Cert906.lean — T39 cycle 2160 (goal G876): rank ≥ 2 for 2 of the 7 congruent curves in 181801–182000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert906
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (181905, (true, 14333621844529, 6606, 45184611946573601983), (false, 6276374544, 205, 345748808888604)),
   (181929, (true, 732633, 2, 72530667), (false, 3243237257, 513, 2720365878461608))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert906

#print axioms Rank2Cert906.on_curve
#print axioms Rank2Cert906.indep
#print axioms Rank2Cert906.count

bsd/Rank2Cert907.lean

lean_all/bsd/Rank2Cert907.lean

/-
  bsd/Rank2Cert907.lean — T39 cycle 2164 (goal G877): rank ≥ 2 for 0 of the 7 congruent curves in 182001–182200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert907
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert907

#print axioms Rank2Cert907.on_curve
#print axioms Rank2Cert907.indep
#print axioms Rank2Cert907.count

bsd/Rank2Cert908.lean

lean_all/bsd/Rank2Cert908.lean

/-
  bsd/Rank2Cert908.lean — T39 cycle 2166 (goal G877): rank ≥ 2 for 1 of the 2 congruent curves in 182201–182400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert908
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (182210, (true, 124813850, 1, 1394420332200), (false, 211740494, 237, 148894239750704))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert908

#print axioms Rank2Cert908.on_curve
#print axioms Rank2Cert908.indep
#print axioms Rank2Cert908.count

bsd/Rank2Cert909.lean

lean_all/bsd/Rank2Cert909.lean

/-
  bsd/Rank2Cert909.lean — T39 cycle 2168 (goal G877): rank ≥ 2 for 2 of the 3 congruent curves in 182401–182600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert909
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (182434, (false, 1533854, 15, 50801541056), (true, 203350, 1, 40507320)),
   (182546, (true, 82841850, 19, 456911462280), (true, 1670998, 3, 394355528))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert909

#print axioms Rank2Cert909.on_curve
#print axioms Rank2Cert909.indep
#print axioms Rank2Cert909.count

bsd/Rank2Cert91.lean

lean_all/bsd/Rank2Cert91.lean

/-
  bsd/Rank2Cert91.lean — T39 cycle 441 (goal G158): rank ≥ 2 for 4 of the 7 congruent curves in 18801–19000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert91
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (18849, (true, 75119548, 13, 650486994542), (true, 237251797225, 3042, 78335345088943955)),
   (18907, (true, 634957, 1, 505735980), (true, 5178961225, 126, 372077415396035)),
   (18921, (true, 70225, 1, 17921420), (true, 67473, 1, 16823268)),
   (18995, (true, 3065445, 11, 3551374800), (true, 7068605, 19, 4561836300))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert91

#print axioms Rank2Cert91.on_curve
#print axioms Rank2Cert91.indep
#print axioms Rank2Cert91.count

bsd/Rank2Cert910.lean

lean_all/bsd/Rank2Cert910.lean

/-
  bsd/Rank2Cert910.lean — T39 cycle 2170 (goal G877): rank ≥ 2 for 0 of the 8 congruent curves in 182601–182800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert910
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert910

#print axioms Rank2Cert910.on_curve
#print axioms Rank2Cert910.indep
#print axioms Rank2Cert910.count

bsd/Rank2Cert911.lean

lean_all/bsd/Rank2Cert911.lean

/-
  bsd/Rank2Cert911.lean — T39 cycle 2172 (goal G877): rank ≥ 2 for 0 of the 2 congruent curves in 182801–183000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert911
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert911

#print axioms Rank2Cert911.on_curve
#print axioms Rank2Cert911.indep
#print axioms Rank2Cert911.count

bsd/Rank2Cert912.lean

lean_all/bsd/Rank2Cert912.lean

/-
  bsd/Rank2Cert912.lean — T39 cycle 2174 (goal G877): rank ≥ 2 for 1 of the 7 congruent curves in 183001–183200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert912
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (183090, (true, 10685040, 7, 18971389800), (true, 4579426, 5, 302067424))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert912

#print axioms Rank2Cert912.on_curve
#print axioms Rank2Cert912.indep
#print axioms Rank2Cert912.count

bsd/Rank2Cert913.lean

lean_all/bsd/Rank2Cert913.lean

/-
  bsd/Rank2Cert913.lean — T39 cycle 2176 (goal G877): rank ≥ 2 for 3 of the 9 congruent curves in 183201–183400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert913
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (183241, (false, 55223, 1, 41058864), (true, 26071856127, 53, 4208945216644656)),
   (183274, (true, 314678, 1, 143493168), (false, 6199011, 31, 438244307865)),
   (183305, (false, 152100, 1, 39898950), (true, 332145, 1, 159630900))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert913

#print axioms Rank2Cert913.on_curve
#print axioms Rank2Cert913.indep
#print axioms Rank2Cert913.count

bsd/Rank2Cert914.lean

lean_all/bsd/Rank2Cert914.lean

/-
  bsd/Rank2Cert914.lean — T39 cycle 2178 (goal G877): rank ≥ 2 for 3 of the 7 congruent curves in 183401–183600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert914
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (183441, (false, 23893343, 28, 693223257329), (true, 47204883277561, 13473, 229883220538480030940)),
   (183489, (true, 2345201932471225, 113033, 3079406754592018782020), (true, 447640000, 41, 6863751645400)),
   (183570, (false, 890420, 27, 126274912300), (true, 161928150, 19, 1880095228200))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert914

#print axioms Rank2Cert914.on_curve
#print axioms Rank2Cert914.indep
#print axioms Rank2Cert914.count

bsd/Rank2Cert915.lean

lean_all/bsd/Rank2Cert915.lean

/-
  bsd/Rank2Cert915.lean — T39 cycle 2180 (goal G877): rank ≥ 2 for 5 of the 7 congruent curves in 183601–183800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert915
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (183610, (true, 240254, 1, 75949608), (true, 72001350, 1, 610955455200)),
   (183651, (true, 184093, 1, 5470192), (true, 61633524121, 214, 15158067511330315)),
   (183666, (true, 183750, 1, 2381400), (false, 404468750, 71, 16749583858000)),
   (183729, (true, 2568657, 1, 4106249784), (true, 43026105113167801, 91316, 8919131335575089349520045)),
   (183770, (true, 449650, 1, 275185800), (false, 25569317551, 2900, 247098856229576793))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 5 := rfl

end Rank2Cert915

#print axioms Rank2Cert915.on_curve
#print axioms Rank2Cert915.indep
#print axioms Rank2Cert915.count

bsd/Rank2Cert916.lean

lean_all/bsd/Rank2Cert916.lean

/-
  bsd/Rank2Cert916.lean — T39 cycle 2182 (goal G877): rank ≥ 2 for 0 of the 9 congruent curves in 183801–184000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert916
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert916

#print axioms Rank2Cert916.on_curve
#print axioms Rank2Cert916.indep
#print axioms Rank2Cert916.count

bsd/Rank2Cert917.lean

lean_all/bsd/Rank2Cert917.lean

/-
  bsd/Rank2Cert917.lean — T39 cycle 2184 (goal G878): rank ≥ 2 for 2 of the 10 congruent curves in 184001–184200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert917
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (184002, (true, 2839225, 2, 4620554765), (true, 198198, 1, 32792760)),
   (184145, (false, 184041, 1, 2654652), (true, 834286772449, 1165, 727035189675943668))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert917

#print axioms Rank2Cert917.on_curve
#print axioms Rank2Cert917.indep
#print axioms Rank2Cert917.count

bsd/Rank2Cert918.lean

lean_all/bsd/Rank2Cert918.lean

/-
  bsd/Rank2Cert918.lean — T39 cycle 2186 (goal G878): rank ≥ 2 for 2 of the 6 congruent curves in 184201–184400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert918
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (184314, (true, 2956113, 4, 351777447), (true, 26323789281, 202, 4092913258242255)),
   (184345, (true, 133617281, 4, 1544145101121), (true, 234973695145, 807, 97912319151462200))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert918

#print axioms Rank2Cert918.on_curve
#print axioms Rank2Cert918.indep
#print axioms Rank2Cert918.count

bsd/Rank2Cert919.lean

lean_all/bsd/Rank2Cert919.lean

/-
  bsd/Rank2Cert919.lean — T39 cycle 2188 (goal G878): rank ≥ 2 for 2 of the 5 congruent curves in 184401–184600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert919
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (184506, (false, 69135738, 71, 7712148369744), (true, 26221150, 9, 110325679640)),
   (184569, (false, 45177, 1, 38036592), (true, 186543, 1, 11690028))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert919

#print axioms Rank2Cert919.on_curve
#print axioms Rank2Cert919.indep
#print axioms Rank2Cert919.count

bsd/Rank2Cert92.lean

lean_all/bsd/Rank2Cert92.lean

/-
  bsd/Rank2Cert92.lean — T39 cycle 442 (goal G159): rank ≥ 2 for 3 of the 6 congruent curves in 19001–19200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert92
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (19065, (true, 1755625, 3, 2315066500), (true, 19375, 1, 480500)),
   (19106, (true, 37538, 1, 6260352), (true, 47511270088581601, 1332186, 7254147050949447719550415)),
   (19194, (true, 208849, 2, 88760825), (true, 2051322, 1, 2937864384))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert92

#print axioms Rank2Cert92.on_curve
#print axioms Rank2Cert92.indep
#print axioms Rank2Cert92.count

bsd/Rank2Cert920.lean

lean_all/bsd/Rank2Cert920.lean

/-
  bsd/Rank2Cert920.lean — T39 cycle 2190 (goal G878): rank ≥ 2 for 2 of the 5 congruent curves in 184601–184800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert920
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (184611, (false, 153503525, 237, 128459388902320), (true, 3929577, 2, 7650886419)),
   (184705, (true, 12390196105, 47, 1378420319558100), (true, 23185848361, 354, 205618551400691))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert920

#print axioms Rank2Cert920.on_curve
#print axioms Rank2Cert920.indep
#print axioms Rank2Cert920.count

bsd/Rank2Cert921.lean

lean_all/bsd/Rank2Cert921.lean

/-
  bsd/Rank2Cert921.lean — T39 cycle 2192 (goal G878): rank ≥ 2 for 1 of the 11 congruent curves in 184801–185000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert921
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (184811, (false, 156816, 5, 1828574748), (true, 128858742961, 826, 9533688677369025))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert921

#print axioms Rank2Cert921.on_curve
#print axioms Rank2Cert921.indep
#print axioms Rank2Cert921.count

bsd/Rank2Cert922.lean

lean_all/bsd/Rank2Cert922.lean

/-
  bsd/Rank2Cert922.lean — T39 cycle 2194 (goal G878): rank ≥ 2 for 1 of the 5 congruent curves in 185001–185200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert922
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (185115, (true, 112843521, 10, 1182472972569), (true, 379260, 1, 203852250))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert922

#print axioms Rank2Cert922.on_curve
#print axioms Rank2Cert922.indep
#print axioms Rank2Cert922.count

bsd/Rank2Cert923.lean

lean_all/bsd/Rank2Cert923.lean

/-
  bsd/Rank2Cert923.lean — T39 cycle 2196 (goal G878): rank ≥ 2 for 1 of the 8 congruent curves in 185201–185400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert923
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (185339, (true, 2849308, 3, 3899277998), (true, 4428963, 1, 9312632868))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert923

#print axioms Rank2Cert923.on_curve
#print axioms Rank2Cert923.indep
#print axioms Rank2Cert923.count

bsd/Rank2Cert924.lean

lean_all/bsd/Rank2Cert924.lean

/-
  bsd/Rank2Cert924.lean — T39 cycle 2198 (goal G878): rank ≥ 2 for 1 of the 4 congruent curves in 185401–185600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert924
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (185426, (false, 3373847, 6, 10579934933), (true, 1120618, 1, 1169925192))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert924

#print axioms Rank2Cert924.on_curve
#print axioms Rank2Cert924.indep
#print axioms Rank2Cert924.count

bsd/Rank2Cert925.lean

lean_all/bsd/Rank2Cert925.lean

/-
  bsd/Rank2Cert925.lean — T39 cycle 2200 (goal G878): rank ≥ 2 for 1 of the 9 congruent curves in 185601–185800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert925
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (185674, (true, 756857, 2, 126775341), (true, 4166573401, 76, 259886317875165))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert925

#print axioms Rank2Cert925.on_curve
#print axioms Rank2Cert925.indep
#print axioms Rank2Cert925.count

bsd/Rank2Cert926.lean

lean_all/bsd/Rank2Cert926.lean

/-
  bsd/Rank2Cert926.lean — T39 cycle 2202 (goal G878): rank ≥ 2 for 1 of the 6 congruent curves in 185801–186000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert926
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (185946, (true, 24847404, 7, 115229658060), (false, 7864292567, 1406, 32590265329618507))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert926

#print axioms Rank2Cert926.on_curve
#print axioms Rank2Cert926.indep
#print axioms Rank2Cert926.count

bsd/Rank2Cert927.lean

lean_all/bsd/Rank2Cert927.lean

/-
  bsd/Rank2Cert927.lean — T39 cycle 2204 (goal G879): rank ≥ 2 for 0 of the 9 congruent curves in 186001–186200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert927
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert927

#print axioms Rank2Cert927.on_curve
#print axioms Rank2Cert927.indep
#print axioms Rank2Cert927.count

bsd/Rank2Cert928.lean

lean_all/bsd/Rank2Cert928.lean

/-
  bsd/Rank2Cert928.lean — T39 cycle 2206 (goal G879): rank ≥ 2 for 2 of the 4 congruent curves in 186201–186400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert928
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (186321, (false, 5609375, 8, 24922363375), (true, 13584863635225, 5263, 46316627426212047980)),
   (186361, (true, 2785561, 1, 4638685080), (true, 6340107712, 129, 440313711753896))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert928

#print axioms Rank2Cert928.on_curve
#print axioms Rank2Cert928.indep
#print axioms Rank2Cert928.count

bsd/Rank2Cert929.lean

lean_all/bsd/Rank2Cert929.lean

/-
  bsd/Rank2Cert929.lean — T39 cycle 2208 (goal G879): rank ≥ 2 for 2 of the 5 congruent curves in 186401–186600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert929
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (186465, (true, 86706225, 8, 799691513175), (true, 313445248228225, 1803, 5549344974165212123300)),
   (186594, (false, 1839362, 17, 73093092688), (false, 439943168, 51, 4298364066304))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert929

#print axioms Rank2Cert929.on_curve
#print axioms Rank2Cert929.indep
#print axioms Rank2Cert929.count

bsd/Rank2Cert93.lean

lean_all/bsd/Rank2Cert93.lean

/-
  bsd/Rank2Cert93.lean — T39 cycle 443 (goal G160): rank ≥ 2 for 0 of the 3 congruent curves in 19201–19400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert93
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert93

#print axioms Rank2Cert93.on_curve
#print axioms Rank2Cert93.indep
#print axioms Rank2Cert93.count

bsd/Rank2Cert930.lean

lean_all/bsd/Rank2Cert930.lean

/-
  bsd/Rank2Cert930.lean — T39 cycle 2210 (goal G879): rank ≥ 2 for 0 of the 9 congruent curves in 186601–186800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert930
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert930

#print axioms Rank2Cert930.on_curve
#print axioms Rank2Cert930.indep
#print axioms Rank2Cert930.count

bsd/Rank2Cert931.lean

lean_all/bsd/Rank2Cert931.lean

/-
  bsd/Rank2Cert931.lean — T39 cycle 2212 (goal G879): rank ≥ 2 for 0 of the 5 congruent curves in 186801–187000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert931
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert931

#print axioms Rank2Cert931.on_curve
#print axioms Rank2Cert931.indep
#print axioms Rank2Cert931.count

bsd/Rank2Cert932.lean

lean_all/bsd/Rank2Cert932.lean

/-
  bsd/Rank2Cert932.lean — T39 cycle 2214 (goal G879): rank ≥ 2 for 3 of the 6 congruent curves in 187001–187200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert932
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (187026, (true, 3133452, 1, 5536809684), (true, 10192368, 7, 14241934008)),
   (187153, (true, 2887705455625, 489, 4906560756800946700), (true, 76229553409, 285, 20624019251723452)),
   (187170, (true, 684030, 1, 544143600), (true, 861900, 1, 781080300))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert932

#print axioms Rank2Cert932.on_curve
#print axioms Rank2Cert932.indep
#print axioms Rank2Cert932.count

bsd/Rank2Cert933.lean

lean_all/bsd/Rank2Cert933.lean

/-
  bsd/Rank2Cert933.lean — T39 cycle 2216 (goal G879): rank ≥ 2 for 2 of the 3 congruent curves in 187201–187400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert933
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (187233, (true, 69075633, 7, 569013496920), (true, 35902925529025, 13279, 84530600888035064120)),
   (187241, (true, 1530913764601, 1319, 1850822358433568520), (true, 13870853814118921, 104200, 1615992321581779117898469))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert933

#print axioms Rank2Cert933.on_curve
#print axioms Rank2Cert933.indep
#print axioms Rank2Cert933.count

bsd/Rank2Cert934.lean

lean_all/bsd/Rank2Cert934.lean

/-
  bsd/Rank2Cert934.lean — T39 cycle 2218 (goal G879): rank ≥ 2 for 1 of the 5 congruent curves in 187401–187600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert934
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (187530, (true, 189525, 1, 11940075), (true, 6295650, 1, 15789490200))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert934

#print axioms Rank2Cert934.on_curve
#print axioms Rank2Cert934.indep
#print axioms Rank2Cert934.count

bsd/Rank2Cert935.lean

lean_all/bsd/Rank2Cert935.lean

/-
  bsd/Rank2Cert935.lean — T39 cycle 2220 (goal G879): rank ≥ 2 for 2 of the 7 congruent curves in 187601–187800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert935
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (187635, (false, 11854815, 16, 160271031375), (false, 6283389, 11, 54688090716)),
   (187746, (false, 341172, 7, 5369756652), (true, 250821, 1, 83296845))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert935

#print axioms Rank2Cert935.on_curve
#print axioms Rank2Cert935.indep
#print axioms Rank2Cert935.count

bsd/Rank2Cert936.lean

lean_all/bsd/Rank2Cert936.lean

/-
  bsd/Rank2Cert936.lean — T39 cycle 2222 (goal G879): rank ≥ 2 for 0 of the 8 congruent curves in 187801–188000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert936
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert936

#print axioms Rank2Cert936.on_curve
#print axioms Rank2Cert936.indep
#print axioms Rank2Cert936.count

bsd/Rank2Cert937.lean

lean_all/bsd/Rank2Cert937.lean

/-
  bsd/Rank2Cert937.lean — T39 cycle 2228 (goal G880): rank ≥ 2 for 3 of the 7 congruent curves in 188001–188200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert937
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (188105, (true, 86595401092225, 14309, 721740165070769838600), (true, 14422159535831904025, 2850548, 54461884482608229035320691325)),
   (188123, (true, 108807101, 5, 1133913121224), (true, 87514797241, 130, 25872325434703461)),
   (188194, (false, 173056, 1, 30763200), (true, 12739110117721, 1746, 45422158952513724355))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert937

#print axioms Rank2Cert937.on_curve
#print axioms Rank2Cert937.indep
#print axioms Rank2Cert937.count

bsd/Rank2Cert938.lean

lean_all/bsd/Rank2Cert938.lean

/-
  bsd/Rank2Cert938.lean — T39 cycle 2230 (goal G880): rank ≥ 2 for 2 of the 4 congruent curves in 188201–188400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert938
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (188354, (true, 272322, 1, 102635136), (true, 40201386041025889, 128472, 8036354760196533998733935)),
   (188355, (false, 20880, 11, 3293271900), (true, 3404486265625, 4248, 357742534702191875))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert938

#print axioms Rank2Cert938.on_curve
#print axioms Rank2Cert938.indep
#print axioms Rank2Cert938.count

bsd/Rank2Cert939.lean

lean_all/bsd/Rank2Cert939.lean

/-
  bsd/Rank2Cert939.lean — T39 cycle 2232 (goal G880): rank ≥ 2 for 1 of the 6 congruent curves in 188401–188600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert939
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (188529, (true, 14681337, 2, 56179093509), (true, 54865098289, 504, 6271087425298345))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert939

#print axioms Rank2Cert939.on_curve
#print axioms Rank2Cert939.indep
#print axioms Rank2Cert939.count

bsd/Rank2Cert94.lean

lean_all/bsd/Rank2Cert94.lean

/-
  bsd/Rank2Cert94.lean — T39 cycle 444 (goal G161): rank ≥ 2 for 1 of the 5 congruent curves in 19401–19600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert94
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (19401, (true, 17305692, 23, 57965415354), (true, 491018134427041, 76153, 10590945798520904427260))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert94

#print axioms Rank2Cert94.on_curve
#print axioms Rank2Cert94.indep
#print axioms Rank2Cert94.count

bsd/Rank2Cert940.lean

lean_all/bsd/Rank2Cert940.lean

/-
  bsd/Rank2Cert940.lean — T39 cycle 2234 (goal G880): rank ≥ 2 for 2 of the 4 congruent curves in 188601–188800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert940
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (188706, (true, 4741926, 5, 1043522424), (false, 24276, 5, 735035868)),
   (188786, (true, 1979650, 3, 1429447040), (false, 36503650, 29, 933556546320))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert940

#print axioms Rank2Cert940.on_curve
#print axioms Rank2Cert940.indep
#print axioms Rank2Cert940.count

bsd/Rank2Cert941.lean

lean_all/bsd/Rank2Cert941.lean

/-
  bsd/Rank2Cert941.lean — T39 cycle 2236 (goal G880): rank ≥ 2 for 1 of the 7 congruent curves in 188801–189000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert941
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (188914, (false, 83283422, 27, 1000957795712), (true, 26164589, 1, 133831872735))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Cert941

#print axioms Rank2Cert941.on_curve
#print axioms Rank2Cert941.indep
#print axioms Rank2Cert941.count

bsd/Rank2Cert942.lean

lean_all/bsd/Rank2Cert942.lean

/-
  bsd/Rank2Cert942.lean — T39 cycle 2238 (goal G880): rank ≥ 2 for 3 of the 9 congruent curves in 189001–189200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert942
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (189051, (true, 345507, 1, 169989444), (true, 68991610444576681, 367288, 16837950893334006737686805)),
   (189105, (true, 3243601, 4, 2105097049), (false, 9896145, 47, 1313741293200)),
   (189145, (true, 1801855, 3, 792816200), (false, 6808012911, 250, 797414853936687))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert942

#print axioms Rank2Cert942.on_curve
#print axioms Rank2Cert942.indep
#print axioms Rank2Cert942.count

bsd/Rank2Cert943.lean

lean_all/bsd/Rank2Cert943.lean

/-
  bsd/Rank2Cert943.lean — T39 cycle 2240 (goal G880): rank ≥ 2 for 2 of the 9 congruent curves in 189201–189400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert943
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (189210, (true, 25038960, 11, 50733545400), (true, 765850, 1, 649440800)),
   (189394, (false, 800665664, 1759, 16581472394348400), (true, 6778722889, 70, 552857623839963))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 2 := rfl

end Rank2Cert943

#print axioms Rank2Cert943.on_curve
#print axioms Rank2Cert943.indep
#print axioms Rank2Cert943.count

bsd/Rank2Cert944.lean

lean_all/bsd/Rank2Cert944.lean

/-
  bsd/Rank2Cert944.lean — T39 cycle 2242 (goal G880): rank ≥ 2 for 0 of the 6 congruent curves in 189401–189600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert944
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   ]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 0 := rfl

end Rank2Cert944

#print axioms Rank2Cert944.on_curve
#print axioms Rank2Cert944.indep
#print axioms Rank2Cert944.count

bsd/Rank2Cert95.lean

lean_all/bsd/Rank2Cert95.lean

/-
  bsd/Rank2Cert95.lean — T39 cycle 445 (goal G162): rank ≥ 2 for 5 of the 7 congruent curves in 19601–19800 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert95
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (19610, (true, 28090, 1, 3370800), (false, 15185984, 655, 32785435365936)),
   (19642, (true, 599886, 5, 266882616), (true, 1137613534, 135, 36420845754952)),
   (19721, (true, 34225, 1, 5174820), (true, 806592591025, 6177, 260904812804466740)),
   (19762, (true, 2381321, 10, 2050317381), (false, 61024400, 57, 155948454440)),
   (19778, (true, 53564218, 9, 391848172840), (false, 230702, 21, 4187884240))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 5 := rfl

end Rank2Cert95

#print axioms Rank2Cert95.on_curve
#print axioms Rank2Cert95.indep
#print axioms Rank2Cert95.count

bsd/Rank2Cert96.lean

lean_all/bsd/Rank2Cert96.lean

/-
  bsd/Rank2Cert96.lean — T39 cycle 446 (goal G163): rank ≥ 2 for 4 of the 6 congruent curves in 19801–20000 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert96
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (19810, (true, 259210, 3, 95795000), (false, 52094, 9, 366044896)),
   (19866, (true, 63525, 1, 15207885), (true, 40678, 1, 7159328)),
   (19947, (false, 14400, 1, 1656360), (true, 22458395862841, 15850, 103747732555851490139)),
   (19995, (true, 83205, 1, 23297400), (false, 114371555, 169, 5983629501500))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 4 := rfl

end Rank2Cert96

#print axioms Rank2Cert96.on_curve
#print axioms Rank2Cert96.indep
#print axioms Rank2Cert96.count

bsd/Rank2Cert97.lean

lean_all/bsd/Rank2Cert97.lean

/-
  bsd/Rank2Cert97.lean — T39 cycle 448 (goal G165): rank ≥ 2 for 9 of the 13 congruent curves in 20001–20200 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert97
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (20001, (true, 3540177, 7, 6400640016), (false, 1851392, 129, 452870052992)),
   (20002, (false, 400, 1, 399960), (true, 863298, 1, 801907920)),
   (20009, (true, 180625, 3, 5953400), (false, 764048, 21, 7684048756)),
   (20026, (true, 786258, 1, 696958152), (true, 182476, 1, 77477820)),
   (20066, (true, 1825216, 7, 2077504080), (false, 802026463, 568, 181925312293809)),
   (20105, (true, 11435631118921, 23718, 5717879482054899581), (true, 15687025669225, 9352, 61739808909425356725)),
   (20130, (true, 83205, 1, 23287725), (true, 184525, 1, 78792175)),
   (20155, (true, 390625, 2, 238884375), (true, 3553225, 12, 3863995525)),
   (20162, (false, 3600, 1, 1190280), (false, 30717122, 41, 79382323440))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 9 := rfl

end Rank2Cert97

#print axioms Rank2Cert97.on_curve
#print axioms Rank2Cert97.indep
#print axioms Rank2Cert97.count

bsd/Rank2Cert98.lean

lean_all/bsd/Rank2Cert98.lean

/-
  bsd/Rank2Cert98.lean — T39 cycle 449 (goal G166): rank ≥ 2 for 3 of the 12 congruent curves in 20201–20400 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert98
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (20235, (true, 21660, 1, 1137150), (true, 1767475, 1, 2349640700)),
   (20306, (true, 2903758, 1, 4948003632), (false, 719652947, 329, 55712632169727)),
   (20355, (true, 23805, 1, 1904400), (false, 2671680, 1741, 100846508855400))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 3 := rfl

end Rank2Cert98

#print axioms Rank2Cert98.on_curve
#print axioms Rank2Cert98.indep
#print axioms Rank2Cert98.count

bsd/Rank2Cert99.lean

lean_all/bsd/Rank2Cert99.lean

/-
  bsd/Rank2Cert99.lean — T39 cycle 450 (goal G167): rank ≥ 2 for 5 of the 6 congruent curves in 20401–20600 with
  Tunnell A = 2B and root number +1 (n ≡ 1, 2, 3 mod 8), from two points (bsd_quartic.c, box 2000 / 6000, Mac) whose
  2-descent images are independent of each other and of the torsion images (as Rank2Cert).
-/
import bsd.Rank2Cert

namespace Rank2Cert99
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (20410, (false, 3600, 1, 1205400), (true, 285610, 3, 116880400)),
   (20474, (true, 4259578, 7, 8543927568), (true, 1790324037378548281, 1563866, 2394569994249938466200812365)),
   (20514, (true, 4869228, 11, 9243682812), (true, 28158, 1, 3236688)),
   (20553, (true, 1221025, 7, 762896420), (false, 13526288, 27, 23702317660)),
   (20586, (true, 33282, 1, 4770936), (true, 127308, 1, 44826012))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 5 := rfl

end Rank2Cert99

#print axioms Rank2Cert99.on_curve
#print axioms Rank2Cert99.indep
#print axioms Rank2Cert99.count

bsd/Rank2CertMiss1.lean

lean_all/bsd/Rank2CertMiss1.lean

/-
  bsd/Rank2CertMiss1.lean — T39 cycle 759 (goal G398): rank ≥ 2 for 19 of the 158 finished n (of the 272 congruent
  curves in 50001–63200 with Tunnell A = 2B, root number +1 and only one point in box 6000) from two points of the
  quartic search in box 30000 (Kamiak a2_miss 31945733) whose 2-descent images are independent of each other and of
  the torsion images (as Rank2Cert). Lean 4 core, rfl.
-/
import bsd.Rank2Cert

namespace Rank2CertMiss1
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (50249, (true, 250821338491042225, 2217594, 21527485819792156565724545), (true, 29569895273574049, 410990, 4870835790503396392843593)),
   (50347, (false, 7565256875, 463, 669520798999800), (true, 203228, 1, 88760910)),
   (50691, (true, 230187, 1, 107727516), (true, 28336772549045427444121, 460887284, 4412282099716574030120927671293245)),
   (50809, (true, 688231, 3, 426703220), (true, 7033354494819409, 278457, 488634101125458245177500)),
   (51267, (true, 2928720525, 151, 145323646498080), (false, 96889389621, 7925, 1001792657800378008)),
   (51555, (true, 126567525, 17, 1414012389300), (false, 16675092420, 2221, 32769244818887850)),
   (52170, (false, 3048796830, 767, 1686251432590200), (true, 7580600256, 365, 263485001154096)),
   (52762, (true, 19900612874, 475, 2249689874418432), (false, 567470056979, 23355, 21675445146585519569)),
   (52915, (true, 10381421875, 93, 1056727068897500), (true, 9447701004511225, 383244, 522132790716759073885325)),
   (53097, (false, 2897916153, 343, 297905731049520), (true, 8422615185819563089, 5233660, 24076713521864786908292785063)),
   (53641, (true, 157609, 1, 58835400), (false, 183047237919, 4180, 393265657983586479)),
   (53761, (true, 3781643843209, 1135, 7352721123846444948), (true, 346584688332226969, 1502886, 191106670264065643222042165)),
   (53833, (true, 35343025, 3, 210094540280), (true, 5049722658934487209, 8708385, 6678665757071278015822939448)),
   (54202, (true, 31506607481, 730, 2233740401164371), (true, 110994674230801, 31550, 1021929139734948054951)),
   (57211, (false, 4897587807, 316, 205830510234993), (true, 444411, 1, 293797680)),
   (58331, (true, 226310539, 41, 3068374829520), (true, 518924484112841689, 1485320, 362137524621112721394219363)),
   (59321, (true, 25694761, 4, 130157853525), (false, 5124547472, 381, 495393264356012)),
   (60729, (true, 16192881775, 417, 1562102321530940), (true, 970835285709487081, 3216355, 729287893099184877470889604)),
   (61098, (false, 1206288962, 787, 1313655152388040), (true, 20604955638, 287, 2868147022679280))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 19 := rfl

end Rank2CertMiss1

#print axioms Rank2CertMiss1.on_curve
#print axioms Rank2CertMiss1.indep
#print axioms Rank2CertMiss1.count

bsd/Rank2CertMiss2.lean

lean_all/bsd/Rank2CertMiss2.lean

/-
  bsd/Rank2CertMiss2.lean — T39 cycle 827 (goal G398): rank ≥ 2 for 12 of the remaining 114 finished n (of the 272 congruent
  curves in 50001–63200 with Tunnell A = 2B, root number +1 and only one point in box 6000) from two points of the
  quartic search in box 30000 (Kamiak a2_miss 31945733) whose 2-descent images are independent of each other and of
  the torsion images (as Rank2Cert). Lean 4 core, rfl.
-/
import bsd.Rank2Cert

namespace Rank2CertMiss2
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (50530, (false, 18427671, 98, 2081724924117), (true, 115740092780, 1453, 15272468949008700)),
   (51186, (true, 11491257, 14, 18995047821), (true, 2381802538137, 6608, 1270249590113016381)),
   (55729, (true, 135031367, 13, 1565283606264), (true, 54955066090650961, 731153, 10825519918664632677909840)),
   (56602, (true, 91499317, 27, 781188093595), (true, 5327322686, 55, 388632466112184)),
   (57043, (true, 22819729, 20, 1622885433), (false, 270209583, 88, 5744432408295)),
   (57507, (true, 303579888, 71, 1570336039980), (true, 1359659358536351041, 4813810, 314747059336622960231211911)),
   (57921, (true, 2637921, 1, 4283393400), (true, 190588023844156103881, 32762819, 2487202591368889049319402709600)),
   (58611, (false, 27018003, 31, 256890809736), (true, 3259270588, 227, 69954283349398)),
   (59754, (true, 62658, 1, 4719528), (true, 11739126016222, 13483, 15248733579133692616)),
   (60177, (false, 711726539247, 27842, 39349336703861934225), (true, 593915665048225, 94062, 6413569065085352843465)),
   (60490, (true, 546526, 3, 35519176), (false, 5331685315, 749, 2447101074621225)),
   (63089, (false, 12440425127, 606, 2180014141307093), (true, 25212893070209, 10285, 122084674373091014052))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 12 := rfl

end Rank2CertMiss2

#print axioms Rank2CertMiss2.on_curve
#print axioms Rank2CertMiss2.indep
#print axioms Rank2CertMiss2.count

bsd/Rank2Heeg1.lean

lean_all/bsd/Rank2Heeg1.lean

/-
  bsd/Rank2Heeg1.lean — T39 cycle 1866 (goal G854): rank >= 2 for 100 congruent curves E_n (n from 1762 to 13634,
  root number +1, missing from earlier Rank2/3/4Cert files) from two points each whose 2-descent images are
  independent of each other and of the torsion images (as Rank2Cert). Points were found by PARI/GP ellrank on Kamiak
  (search only); every check below is by rfl in Lean 4 core. All tested integers are below 2^256 (isqrtGo fuel).
-/
import bsd.Rank2Cert

namespace Rank2Heeg1
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (1762, (false, 1600, 1, 29520), (true, 7133496398540310402, 53127689, 13658705245045808640588436800)),
   (2953, (false, 626631365554366178304, 461445925, 1302897505495924149712322691744), (false, 13875625, 289, 917269657800)),
   (3001, (false, 1990921, 89, 33422921400), (true, 9507946770707401, 82201, 927105430318359148599000)),
   (3018, (true, 7350, 1, 574560), (true, 26391612258, 2687, 2418991466407560)),
   (3122, (true, 3150, 1, 23520), (true, 74303381488, 4561, 9839568167613240)),
   (3443, (false, 17424, 5, 11126676), (true, 2500380687103475, 352961, 123175111140638147331840)),
   (3603, (false, 3600, 1, 8820), (false, 424894287135231507, 20780863, 975670237750470534325524240)),
   (3713, (true, 4225, 1, 131040), (false, 14559040797983, 185521, 484440748649590006560)),
   (3761, (false, 28485675729, 3281, 4855866904914480), (true, 79343988021061201, 4535279, 4967809032619851160894320)),
   (3882, (true, 4332, 1, 126540), (true, 1168452150, 23, 39940705082880)),
   (4010, (false, 400, 1, 79800), (true, 13460827210, 151, 1561699982236800)),
   (4033, (false, 37239945639844840489, 9953403785, 2438237377869019909439653059766116), (true, 231361, 5, 100161516)),
   (4258, (false, 78400, 17, 343857360), (true, 203574319362, 2567, 90974528184173760)),
   (4411, (false, 539, 1, 101640), (true, 70865555843899014584859, 1126155359, 18805955171340464905089888214981320)),
   (4649, (false, 106162165219129600, 4864933, 9422585598087951962405040), (true, 55071241, 97, 248292114840)),
   (4681, (true, 11881, 1, 1190280), (true, 4766150238406759, 457433, 322019024265726120713400)),
   (4763, (false, 1393425275, 1409, 349121890242840), (false, 69696, 5, 25488408)),
   (4834, (false, 784, 1, 133560), (true, 641283363955699938, 7568287, 463205607126281228877352608)),
   (4843, (true, 6525, 1, 353220), (true, 3180898405756668383765525, 25157325361, 1516852344595388865601878108504597720)),
   (4867, (false, 81744681231, 4280, 10175102238530103), (false, 34596, 5, 21697458)),
   (4898, (true, 12400, 1, 1268520), (true, 3601606154499218198142, 856509089, 14749243678927295794609223716560)),
   (5065, (false, 225, 1, 75900), (true, 15018514997793223225, 6386403, 58196839478130606959231180300)),
   (5138, (true, 69938, 1, 18445680), (true, 28029610350, 479, 4688577998045760)),
   (5161, (false, 4225, 1, 192660), (false, 5464583316752400, 1724837, 1060715993522724650512380)),
   (5259, (false, 5184, 1, 63720), (true, 30711570123, 673, 5365903575238128)),
   (5362, (true, 8750, 1, 646800), (true, 1555634058930112, 320657, 57374032819572599694480)),
   (5587, (true, 855625, 6, 769275325), (true, 4329739103267990597893, 767748583, 184963842833541031041601615896780)),
   (5657, (false, 18032896835279368290304, 22956856765, 400346312145675230256577154949440256), (true, 25421295027282113, 2119628, 83508429434686346581665)),
   (5666, (false, 1058, 1, 181056), (false, 21476323425061006295616, 3603460457, 10312332262831450689085700112019920)),
   (6051, (true, 7203, 1, 331632), (false, 555842726024547, 4701487, 3153333068024485103768088)),
   (6073, (false, 2080469501843717505024, 4369393165, 5287571817911368798674398583172224), (true, 17135420715343440577, 42432132, 54613517485648042342839636575)),
   (6242, (false, 2901543842, 1649, 900822724405920), (false, 988473600, 401, 5479358232480)),
   (6274, (false, 127546122496, 11689, 302741300980416480), (true, 481306338, 167, 9836742665568)),
   (6529, (false, 23623279401763545246961, 3416531353, 11136582066621672647058671734405920), (true, 168420411780721, 128743, 1674805875712374153840)),
   (6683, (true, 18791489, 20, 80631059487), (true, 22396242590463193321, 9351640, 105953380589738115491141290869)),
   (6769, (false, 375148991007, 39652, 6514583067987121617), (true, 23049601, 23, 109317617760)),
   (6906, (true, 19602, 1, 2568456), (true, 676219310214, 5087, 536301984490211520)),
   (6914, (false, 24612001712162, 60689, 32430420535343976048), (true, 344741282, 151, 5692424765664)),
   (6963, (false, 2112, 1, 304920), (false, 1322874003, 1087, 295343062054920)),
   (6995, (true, 29939045, 41, 150652736100), (true, 11377146907672605, 601031, 1183219533248308944049200)),
   (7289, (false, 132496, 5, 45537492), (true, 187456706657140102009, 99322062, 2370235764872054819582002967365)),
   (7394, (false, 7056, 1, 185640), (false, 4541895632751778370251778, 29305383697, 9457648609882336456245790916738970864)),
   (7417, (false, 299428416, 205, 1498485960552), (true, 722516773547502015625, 93344167, 19343170437588600695886656379000)),
   (7498, (false, 843774737541218, 342003, 6946816032854863597880), (false, 190463, 12, 463819265)),
   (7561, (false, 4064270582663424, 1444805, 972278535008245445839824), (true, 154526305201321, 124031, 1264588889176363839480)),
   (7563, (false, 140029450849083, 136729, 231457097380374323640), (true, 101299905147, 2663, 27351625264334280)),
   (7633, (false, 3025, 1, 385440), (true, 19879721150566225, 1517951, 1306577470906223062506960)),
   (7658, (false, 12578978167, 5126, 22523972633923845), (false, 2540258, 107, 139681687320)),
   (7721, (false, 2215139647, 596, 76111044067233), (true, 69839449, 41, 573481073880)),
   (7793, (false, 40304937362722816, 5954825, 54884860676774347347250752), (true, 260352885755118349874657, 4835373368, 94890037655136002749701386146251345)),
   (7819, (false, 56968697583, 4664, 38251423839410439), (false, 1695204, 29, 8272276530)),
   (8002, (false, 1009145305596574350400, 960999001, 232559351441234185796397975185040), (true, 1280385300321, 2660, 1447391398307700369)),
   (8090, (false, 577600, 13, 941799600), (true, 928717690, 281, 20544179728800)),
   (8138, (false, 1484715024, 745, 164369781787224), (true, 2294916650, 529, 13578201315120)),
   (8498, (true, 13552, 1, 1228920), (true, 71697871923706350, 2895623, 2136264135506818266025440)),
   (8578, (false, 242276002671001998400, 479924833, 30520924593614038007298669840240), (true, 11606562, 23, 36394401120)),
   (8609, (false, 199098924484283103377889, 8143353377, 238744819900720274075143652308419840), (true, 88976327521, 287, 26539816866488640)),
   (8801, (false, 676, 1, 228150), (true, 6626365108005668561624881, 19589286966, 14675624371665330469142061484380506815)),
   (8859, (true, 13467, 1, 1177056), (false, 14433740551850702043, 40533487, 7123403451854063206995710184)),
   (8962, (false, 49352651138, 2593, 7680455997963840), (false, 6489913600, 2689, 5194180042857120)),
   (9049, (false, 40526521344, 3965, 27452268004065696), (true, 24134895518336202526225, 1626931164, 460916957381739848471373797605895)),
   (9066, (true, 13122, 1, 1086696), (true, 45716744748, 1081, 9508830457798188)),
   (9122, (false, 21935803777685456400, 50477369, 35986117498050227791566923640), (true, 5249337341831784482, 19406591, 9093546425559282009797507040)),
   (9395, (true, 139445, 1, 51953700), (true, 5627388533743431405, 18642751, 10872296256186039430469788200)),
   (9521, (false, 13172406045043976214561, 2540474329, 6888581800008765270902049273465600), (false, 15366400, 41, 17542780080)),
   (9642, (true, 640032642, 193, 13402420911000), (true, 58201769388, 1633, 12596710291745580)),
   (9658, (true, 1866238418, 239, 77018443129320), (true, 36440989222, 1321, 6167723176174560)),
   (9986, (false, 352238882, 233, 7734446666688), (true, 26841151666179362, 1318327, 3354504063899588056338336)),
   (10011, (false, 237021, 5, 39125808), (true, 927274683, 191, 25954326920808)),
   (10033, (true, 4159313344, 585, 151395478674328), (true, 19183697352300625, 321839, 2653140750605507255979600)),
   (10371, (false, 736796736, 397, 39605380303320), (true, 1035655015850879763, 7074121, 912064361891575309701230232)),
   (10553, (false, 88592938235822331228736, 3192684125, 18159846874109692580334998189240088), (true, 23723700625, 167, 3653761790097900)),
   (10777, (false, 258976023223824, 438325, 33059502666147723612924), (true, 6134805625, 606, 367146563887675)),
   (11065, (false, 11025, 1, 98700), (true, 102068403425984814025, 53039667, 982061203394943657517787488900)),
   (11082, (true, 176418, 1, 73953000), (true, 2926483350, 47, 158308375080240)),
   (11195, (true, 22445, 1, 2914500), (true, 30734021661207757605, 25950689, 165178280999787402229839864600)),
   (11377, (true, 1221025, 7, 1200401280), (true, 7226035840137775, 773489, 206208562271827385257680)),
   (11458, (false, 340771600, 233, 9606521937480), (true, 1394870562, 217, 48040493587680)),
   (11513, (true, 3940225, 18, 2519379865), (true, 36961131167546181485301889, 51902506630, 122231695165498337525687316397033309287)),
   (11523, (true, 18723, 1, 2019240), (false, 483126982701, 6745, 141452175955783968)),
   (11539, (false, 588338853971, 7967, 334597258190632800), (false, 499029216400, 9953, 726479749320459420)),
   (11753, (true, 15625, 1, 1287000), (true, 40954582809968, 20891, 260028202436781348660)),
   (11811, (false, 11253, 1, 380556), (true, 228264039100, 3979, 62543356293264470)),
   (11819, (true, 14969161, 34, 23662552515), (true, 7030354371093, 6559, 18592038828060852048)),
   (12161, (false, 47666210181184, 468785, 18448154394557494022136), (true, 9172722449573713060801, 171411679, 877844569496494349886374464285920)),
   (12209, (false, 841, 1, 353220), (false, 84269600860583824, 3044905, 21939187893970352782707924)),
   (12259, (false, 2116, 1, 555450), (true, 34123635452, 77, 6303502876126854)),
   (12378, (true, 95922, 1, 29459880), (true, 26778422175757350, 1117033, 3579747922598557046102640)),
   (12513, (false, 807711586799025, 260519, 7456903269022933898040), (false, 266256, 5, 84735972)),
   (12706, (false, 3650402, 17, 760407648), (true, 4216618924855076322, 10984817, 8065924566967318543060156896)),
   (12898, (false, 98, 1, 127680), (false, 1744969596896848897600, 386544073, 34168846445161868262476897407920)),
   (12962, (false, 212996511360000, 168881, 4409842327937871739200), (true, 89646101235529411845602, 1160031439, 26327925806793689266842816556600800)),
   (12986, (true, 223686, 1, 105614880), (true, 112223211589354828, 2613359, 23034990656302002296776572)),
   (13114, (false, 12482, 1, 449352), (false, 1506635370279, 16910, 4214997863065369731)),
   (13169, (false, 5576587686494880484, 27929669, 20373143932129733420225906070), (true, 13344409, 6, 48716353205)),
   (13179, (true, 559728, 1, 418644252), (true, 2512397163, 73, 125881727392512)),
   (13531, (false, 157703364, 109, 391924066170), (true, 5112710899517788, 451527, 307793237227588531733678)),
   (13539, (false, 2002383504, 433, 69812670614940), (false, 421765347, 511, 72086021330112)),
   (13610, (true, 16810, 1, 1279200), (false, 127607987284349653188810, 6689532721, 212735743902027511205294036118226800)),
   (13634, (false, 5184, 1, 907920), (true, 909645090623522, 25991, 27433783538058126471264))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 100 := rfl

end Rank2Heeg1

#print axioms Rank2Heeg1.on_curve
#print axioms Rank2Heeg1.indep
#print axioms Rank2Heeg1.count

bsd/Rank2Heeg10.lean

lean_all/bsd/Rank2Heeg10.lean

/-
  bsd/Rank2Heeg10.lean — T39 cycle 1866 (goal G854): rank >= 2 for 100 congruent curves E_n (n from 95266 to 101473,
  root number +1, missing from earlier Rank2/3/4Cert files) from two points each whose 2-descent images are
  independent of each other and of the torsion images (as Rank2Cert). Points were found by PARI/GP ellrank on Kamiak
  (search only); every check below is by rfl in Lean 4 core. All tested integers are below 2^256 (isqrtGo fuel).
-/
import bsd.Rank2Cert

namespace Rank2Heeg10
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (95266, (false, 93150, 1, 6094080), (false, 3280487403668, 5883, 599197475029597444)),
   (95321, (false, 64543213227968569279, 41554100, 1216427241210047650363604108769), (true, 9520047249967379161, 2826929, 29279514550399034657081826600)),
   (95394, (true, 12656748, 11, 18472283388), (true, 28701416250, 433, 3802927017403800)),
   (95426, (false, 459652939581325887938, 139868609, 38791980172722290748088248198336), (false, 1936862290944, 20737, 57045795686879280960)),
   (95730, (true, 126150, 1, 29179800), (true, 4543758564345405, 37139, 306153633031211714379975)),
   (95753, (false, 37244731747924332768703, 628614932, 1287426061052938929165855335239905), (false, 49705464504087, 29327, 462938562218884026240)),
   (95786, (false, 662332733785058, 208683, 105991117390565054446904), (true, 245367603382666366, 147405, 121537474490236659902130664)),
   (95858, (true, 103058, 1, 12149040), (false, 50782227982400, 23017, 2910759282133821840)),
   (95906, (true, 11142110704, 103, 1171204833676920), (true, 947672925796946574558, 62931337, 26727782049851486478507039396912)),
   (95921, (true, 142129, 1, 39539760), (false, 160192443736377423, 1649551, 82473677302547244581178024)),
   (96139, (false, 21265103548160173031967, 809677952, 8651961634048682332380781134362193), (false, 9117766950637527, 534514, 2474062696371487555430733)),
   (96169, (true, 5408278681, 224, 179615175317565), (true, 5820679236353, 6956, 8436469135494504447)),
   (96322, (false, 12533893308148802, 380521, 684868075565063010561360), (true, 1415226402, 71, 50008437161760)),
   (96338, (true, 6488141, 5, 15345660561), (true, 301065009102, 1511, 112798391427321600)),
   (96362, (false, 4270173698, 211, 27010695533640), (true, 58529856480606, 11525, 436943661575448467496)),
   (96418, (true, 65489193, 26, 51556953525), (true, 834613912098, 2369, 580515014016508320)),
   (96513, (false, 12689879262192662436275625, 11730753161, 13960103801322953887752145820270757300), (false, 433305236111775, 194527, 75485607949734174377640)),
   (96554, (false, 831094210091438423, 22839906, 45911860759941490059152481269), (true, 320224364350, 1293, 156501406730687480)),
   (96586, (false, 3259312322, 571, 1788177847453368), (true, 1387746853063744773034526, 1413775715, 1618907034019192427203343505799373976)),
   (96594, (false, 1088, 1, 3185936), (false, 2106793707347894852162, 309969123, 414867637398443912150898126861872)),
   (96627, (false, 41392597611027, 22351, 159785115475719848400), (true, 662491173, 7, 17051361137460)),
   (96707, (false, 1249924, 85, 781154377926), (true, 716224240095105625, 1697742, 558353307254230491461083925)),
   (96761, (false, 847249233160270633940209424, 155751527235, 63717569010666889009657735615948388071324), (false, 7071967, 16, 63131794383)),
   (96859, (false, 730362023867, 3649, 908420939995469208), (true, 50493516507, 449, 10463503979196576)),
   (96995, (false, 6418001847310805, 257723, 44984574714714691966800), (false, 6283205, 9, 11823074900)),
   (97034, (false, 1813605243257983, 182744, 114363336447980097883839), (false, 11711, 2, 41983935)),
   (97041, (false, 5336100, 89, 1775565882090), (true, 910085170288, 841, 865734012660363308)),
   (97154, (true, 1353635584, 23, 49766762535840), (true, 1724352257522142, 34057, 71451218112289928224704)),
   (97339, (false, 167731484860270831878400, 1354586237, 25136058213695383972665808597686480), (false, 55599968922971, 46817, 1535886938304547003560)),
   (97347, (false, 52466948444484417600, 53373073, 1972396839860747561170274100120), (true, 19228225, 14, 10448617465)),
   (97433, (true, 929618032, 27, 28260862097300), (true, 30152586025, 89, 5234130457034520)),
   (97458, (false, 481119732, 73, 4289966932140), (true, 244815422042548580790000, 615912247, 119742616873922694431084219958535800)),
   (97482, (false, 87318, 1, 12806640), (false, 21798620382, 607, 4214600356233960)),
   (97530, (false, 1586406655470, 4367, 1222961256057504600), (true, 26894511405, 121, 4404356777847825)),
   (97634, (false, 14968177237795562496, 13064809, 28345486025493829485332295360), (false, 1534247618, 697, 1856893716520704)),
   (97705, (false, 3030304512090591291988224, 29925246149, 152221070189922835371596221223913250224), (true, 81462221358353343654184921, 28874568778, 4585228561336896743678409021779794581)),
   (97745, (true, 13415430625, 83, 1551883664710500), (true, 1059373737074451241, 2893575, 692362329482623235197616164)),
   (97826, (true, 151618, 1, 45104592), (false, 711097293056, 3291, 662341137015862880)),
   (97953, (true, 23337328, 1, 112738660300), (true, 16213036833365199850787209, 4970677985, 64551031633617827388398943101060673052)),
   (98042, (false, 8012900082647, 9078, 2933802578313868085), (false, 1252503, 4, 1056973365)),
   (98115, (false, 3121737975, 436, 1027392996469275), (true, 28588795665, 422, 3825981918687825)),
   (98171, (false, 20181872477398756975701796, 43010874649, 810815745126468531335768709932338856370), (false, 106976485183, 2288, 164407093930460961)),
   (98195, (true, 588245, 1, 444836700), (true, 90856509269805, 30319, 98665209242027003400)),
   (98233, (false, 1471515383893247, 661596, 1648432139498946534114305), (true, 78091904773697968151207927, 13888680887, 669471638058022278020233826749700988000)),
   (98259, (true, 197462307, 11, 2769731371764), (true, 304735228, 29, 5120337777562)),
   (98338, (false, 2209545846405612042250000, 5275307753, 2400097139237794371857407889622477000), (true, 49768302156739484088969, 402067730, 10521067481466279227104236262632453)),
   (98363, (true, 1943951275, 137, 26840212704480), (true, 4497192802622725, 119521, 286487703173756439830160)),
   (98395, (false, 153405410007729605, 2282847, 191640240093872286500469400), (true, 2213232025, 122, 78066397022325)),
   (98434, (true, 213616, 1, 87623640), (true, 50644077282, 391, 10882281993542784)),
   (98617, (false, 5712253475855046250625, 306602739, 551846992397357261390867076624200), (false, 3877996763169, 33485, 217615014215676076896)),
   (98747, (false, 311163726753821042783, 87222042, 12061476726647931057193995988745), (true, 112922540797, 151, 37938889751102340)),
   (98753, (true, 38760553129, 10, 7631061170706867), (true, 516154690118074225, 2025606, 229715846311793949509325985)),
   (98763, (true, 19068543399388, 13543, 26009784673185362770), (true, 74087834247525, 8231, 635099717743579352880)),
   (98922, (false, 2380145378088011900547, 211412299, 181779578514795356044321558715145), (true, 150485463487270598562, 38120473, 546087052454774448494182349160)),
   (98938, (false, 60876207, 44, 1417000648665), (true, 45359674945900, 12641, 286338812985446176620)),
   (98987, (false, 16354975, 44, 772184544495), (true, 27251773928317057, 312708, 4205407165041496990018625)),
   (99003, (false, 250911655203, 1849, 113792051387415060), (true, 753941437748376843, 2737141, 117372178916662888071064260)),
   (99019, (false, 145128663655307, 40159, 802667871051891138048), (true, 1697738453362383618843, 23302313, 69917883763450455015520921109448)),
   (99035, (true, 131151258632641, 34454, 665777837421636232761), (true, 1217682137119725, 29107, 42390394609205471215200)),
   (99130, (true, 162063934, 15, 2043511718248), (true, 21832991270, 41, 3225946879963200)),
   (99170, (false, 1128520199800559669806, 204115571, 133521563843081761488411259188528), (false, 19834, 13, 2360325336)),
   (99235, (true, 107494289, 8, 1112547991737), (true, 100990930952204999281, 9234604, 1011331319766186901033210384521)),
   (99242, (true, 190333, 1, 70855785), (false, 1146716064855198838, 5707783, 3237172929090862310601212160)),
   (99266, (false, 1532857461477224537664, 125462273, 11864373687768485855498672385360), (true, 155683116002, 713, 58110814027437024)),
   (99322, (false, 100344620530281850, 1220311, 34420784713593083056015920), (true, 5207112930389675546248225, 1463574444, 11872240616984622649804516343775924815)),
   (99353, (false, 24950025, 41, 824865468840), (true, 7933016884225225, 254569, 412768722643210820423160)),
   (99442, (true, 126350, 1, 27706560), (true, 107263291259538, 32767, 106578115306371848880)),
   (99610, (false, 59150, 1, 19492200), (true, 26895608888965740, 118967, 4404784057020722866190700)),
   (99619, (false, 45484492251210975, 689168, 2779106064254533594466865), (false, 2022084, 5, 2067356214)),
   (99626, (false, 127437065055424656, 1415009, 54783328602047018913037560), (false, 206386922, 47, 1097572863072)),
   (99665, (true, 28867975, 9, 148916271700), (true, 27430597847830980625, 16258437, 40014770977311609941522058500)),
   (99673, (false, 1799039254314436024612743, 4685604779, 1671045674016638033634960316975616700), (true, 3553225, 3, 6480863740)),
   (99699, (false, 5720844995787, 14911, 51223234119952816908), (false, 1076569007, 112, 20882415099097)),
   (99889, (false, 11017937687, 338, 311952861186387), (true, 1222716860352, 721, 1350818080651460616)),
   (99931, (true, 262835369552427841, 1523658, 63334719412499888929076345), (true, 1648397062374588253, 532841, 2116062091872575102746088508)),
   (99953, (false, 3684651719313663, 558286, 1877796382244969098306905), (true, 3999513938766867361, 6275970, 1409458991874588251465613641)),
   (99977, (false, 4352, 1, 6589200), (true, 58072341514198663009, 13573620, 419689262901083418811909868527)),
   (100066, (false, 619825966087511824, 2488817, 1721674670092183394731080), (true, 8918335458, 289, 293925358997088)),
   (100201, (false, 12943408737393806400, 14479933, 59535688100900761879814621880), (true, 8800875597358225, 294996, 111841222677207311586745)),
   (100289, (true, 1495744852063, 1409, 1813026617441551512), (true, 335537069826050689, 546593, 193585251124222936831299600)),
   (100290, (false, 22476312150, 557, 3225743891206200), (false, 5920948890, 877, 5917922044864200)),
   (100331, (false, 9294988036270475, 319653, 416901778148570958521740), (true, 71111117, 23, 399094531836)),
   (100347, (false, 98397, 1, 6175260), (false, 28044345765174375, 977618, 15358735505208292183329525)),
   (100378, (false, 320959336310498, 69521, 6517581524380588704840), (true, 1918074574754074142, 243919, 2656416246175931694448728840)),
   (100465, (false, 7385470200656656500884615, 13207743538, 43192266012314739613691048264823049275), (true, 1221025, 3, 906740900)),
   (100466, (false, 1082005389262, 25271, 66729352877980365552), (true, 1538016722, 17, 60306508530576)),
   (100786, (true, 96966738, 31, 45755154192), (true, 1321524225, 76, 43128846239745)),
   (100826, (true, 263538, 1, 124996872), (true, 647448744610218531528262, 359756999, 520858625057343236724169963141269696)),
   (100834, (false, 24476053961838117535744, 1626290321, 41546747660555186300028761037592320), (true, 4911719750460324512577, 181465804, 253656590332113126614344797690081)),
   (100955, (false, 920860205, 301, 276150253476000), (true, 1049131179986115224091025, 1994605198, 992728446445088768540226437016625425)),
   (101001, (false, 20067703117594512, 744133, 7395157691490676834742868), (false, 9626964480225, 109973, 3789911034764028874440)),
   (101003, (false, 45040100, 27, 390919948370), (true, 353068828, 57, 2447729192510)),
   (101041, (true, 5770073521, 191, 337206842963760), (true, 862673041035439, 17711, 25320718921832697572160)),
   (101066, (false, 239344301293336742460482, 1890172811, 132268971944462873494532587254407448), (true, 3926740384350, 5213, 5561204040160400520)),
   (101090, (true, 286110, 1, 143167200), (true, 1151188651809900, 101209, 17065834485543659267100)),
   (101146, (true, 12365150, 1, 43479501240), (false, 12423908471219825823, 11648672, 20556538398563571068592114321)),
   (101194, (true, 314678, 1, 167145888), (true, 96621145960288204, 389297, 29652920478715587701612700)),
   (101226, (true, 22724135298, 97, 3422545674997752), (true, 36335648748, 529, 4337789081055948)),
   (101409, (false, 30998919633, 697, 6741585578556576), (false, 39039, 2, 79774695)),
   (101473, (true, 76307696, 25, 370683710244), (true, 1361041562817, 2752, 1310498508982870305))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 100 := rfl

end Rank2Heeg10

#print axioms Rank2Heeg10.on_curve
#print axioms Rank2Heeg10.indep
#print axioms Rank2Heeg10.count

bsd/Rank2Heeg11.lean

lean_all/bsd/Rank2Heeg11.lean

/-
  bsd/Rank2Heeg11.lean — T39 cycle 1866 (goal G854): rank >= 2 for 100 congruent curves E_n (n from 101522 to 110851,
  root number +1, missing from earlier Rank2/3/4Cert files) from two points each whose 2-descent images are
  independent of each other and of the torsion images (as Rank2Cert). Points were found by PARI/GP ellrank on Kamiak
  (search only); every check below is by rfl in Lean 4 core. All tested integers are below 2^256 (isqrtGo fuel).
-/
import bsd.Rank2Cert

namespace Rank2Heeg11
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (101522, (true, 435501798768, 1841, 176172003916777560), (true, 226147317767833262527150, 1048791889, 93518096798150559757417880509248480)),
   (101731, (false, 4072299096969607246444863, 26044807028, 139013796980996976147943502033431602081), (true, 3058932596546437, 25823, 169140667674381083945004)),
   (101795, (false, 418202892549350775, 4394758, 1242326102886728642184581475), (true, 25615184651028025, 264202, 3938756316435085160156325)),
   (102179, (false, 541618705397, 2379, 149156006364541792), (true, 365615251297571, 32257, 6688845100704739510560)),
   (102585, (false, 99225, 1, 8202600), (true, 2782974615, 119, 125223511163400)),
   (102731, (true, 37308903953, 404, 6437633356969977), (true, 2697211590225053055382363, 4169469241, 3319526216613723479087951181724690032)),
   (102786, (true, 513264, 1, 360266040), (false, 246740350107, 2269, 232536374941817763)),
   (102883, (false, 52772313405718167763009475, 79522854959, 4710828456258495738670012866501131004480), (true, 39263612, 19, 79802002650)),
   (102891, (false, 1128033312305214747, 3322537, 141192542525396754820270752), (false, 195771078139585536, 3193177, 456038394032094159823195680)),
   (102905, (false, 4944737021979433272022495, 7451501759, 6366453964366426268935432262218647900), (true, 332880025, 38, 5434704105675)),
   (102963, (false, 309065124904563, 95599, 15625198229975485720920), (true, 174469941982588, 24957, 2143192178484368849530)),
   (102985, (false, 411482106830735, 70162, 6007103197220167541025), (true, 404915207475025, 4401, 8147809421663186564900)),
   (103033, (true, 105625, 1, 7558200), (false, 2841145504536647, 174703, 71848238229355187430600)),
   (103065, (false, 37692845645666670117015, 731647369, 7821824158788327457596321379768800), (false, 55815, 2, 96500475)),
   (103089, (false, 1019234546091783, 108854, 21494051319679302482811), (true, 106251712, 31, 395860634344)),
   (103105, (false, 5656690309184784, 286441, 473092993019260681751964), (false, 1375929, 5, 2556755256)),
   (103195, (true, 18406966968446215771587605, 11407434919, 54011520938772720955578826724205690000), (true, 54285892636911570065154025, 22580834462, 98368858010832276169817176277561085075)),
   (103571, (false, 2054319727756816, 199961, 162976526796722154260940), (true, 1401274156867, 3577, 539170779949631544)),
   (103641, (false, 362878504850697, 176183, 60891915929046667564968), (false, 4613904, 13, 36294122340)),
   (103682, (true, 93305477077922, 3689, 901178960621453180640), (true, 738192575924208, 78809, 9805635677560635252600)),
   (103683, (false, 98192, 1, 10432900), (true, 1162810141210651, 57075, 37942290067647614924024)),
   (103810, (false, 835858952915, 3447, 829273622358073375), (true, 8408890, 9, 198989000)),
   (103849, (true, 232826125166881, 47337, 115068630226027084460), (true, 97755867627275295770929, 970118511, 625391031441560395778308494476020)),
   (103859, (false, 23716, 1, 15571710), (true, 1925515913578012, 117933, 55869238402757896491746)),
   (103881, (false, 389697449109759505764, 346014341, 245400141889614141384187127594490), (true, 11011696, 5, 35510299244)),
   (103883, (true, 26427379225, 504, 234611057237635), (true, 2222812923878089, 143910, 26345824560729359101187)),
   (103915, (false, 439163975145035, 120623, 30318826582926650286000), (true, 618282315, 31, 15171909543000)),
   (103961, (false, 6387555917319863, 272839, 349204505404623335616864), (false, 3174743, 6, 3531358259)),
   (104041, (true, 687241, 1, 563156280), (true, 432040669713527, 2081, 8980214489254363271784)),
   (104234, (false, 6507383715326, 7905, 718774220433587032), (false, 6171542, 9, 14313199472)),
   (104403, (false, 85699454760000, 65041, 4010907377231846821800), (true, 1373441359583331025, 3427222, 724827511295287505337551345)),
   (104410, (false, 28090, 1, 16854000), (false, 48071123781638400, 902369, 15374521761919490785927200)),
   (104539, (false, 8976016, 13, 45590146980), (true, 38140689485470923, 508087, 5263643676572155291168752)),
   (104746, (true, 1451597292, 113, 21492627884364), (true, 610500823975778, 2953, 15084429707168552043192)),
   (104826, (true, 2016204691698, 1753, 2826095804681111352), (true, 38075763216966, 12697, 210539345746130669520)),
   (104873, (false, 141241473949952, 44499, 1809248620103993201680), (true, 2775709225, 49, 145635277079160)),
   (104921, (false, 42945777124828394471, 108037999, 8020630714734138806538393862680), (false, 17179389554975, 398784, 69157853504451521415215)),
   (104979, (false, 7372192820947983, 441074, 1635341869387665935094879), (true, 86794869, 17, 757605216060)),
   (104986, (false, 28322, 1, 17013192), (true, 22098715157501150, 32033, 3285075655035476278436040)),
   (105171, (false, 1801479157957068450282924816, 169575467161, 103103969330873675159796987485454641986260), (false, 144976128, 43, 1560504573936)),
   (105290, (false, 649723551997365160000, 896319437, 2156076906281495737812226230654000), (true, 723010090, 79, 8108831530800)),
   (105346, (false, 33029147710724603559892418, 18713243281, 94433537344849906144909340397057429696), (false, 1076414550016, 8273, 7396722968701322880)),
   (105443, (true, 31013212033409270536564225, 8168764638, 168207476444170594420902814675109317735), (true, 52639958550790966979639197, 22221513089, 56181880132918664520903564400854616560)),
   (105571, (false, 1590744311359015998528, 169203599, 102502496580602961216394348772136), (true, 7857281223877268480089, 102889404, 689398272439680313843853359633885)),
   (105618, (true, 272832, 1, 131397840), (false, 807761081888947507343562, 18911158171, 33940350898613011593922978233386940600)),
   (105763, (true, 8613725, 1, 25278643440), (true, 44243029456694570881201, 211543930, 9252687838704803828759406342516801)),
   (105865, (false, 2734479, 28, 137173408119), (true, 34655919782610025, 452427, 5034835719762401887343900)),
   (105905, (false, 307394208375895, 70391, 7456416154900734159300), (true, 714025, 2, 485693325)),
   (105994, (false, 176967, 2, 162076173), (true, 33115085150, 403, 5147925960776760)),
   (106035, (false, 21801600218535, 27662, 364911459294283528725), (false, 172515622500, 10133, 4521525801075065250)),
   (106099, (false, 1122478476644, 3509, 708133711749060450), (false, 22098852783, 704, 7093214499137319)),
   (106114, (false, 94864, 1, 14645400), (false, 247567812429040501432322, 2234403529, 233046777797591448302430048340878768)),
   (106289, (false, 1072555033012996, 101117, 5737259428089636846750), (true, 1309095250254970238281, 98163486, 29499018144164296884310602693635)),
   (106369, (false, 168446335889753, 44379, 1616524974752739850864), (true, 6435631349537, 2552, 16231399426966403121)),
   (106571, (false, 34634352660424511, 4112520, 335373258897844223216471263), (false, 2494301, 13, 28170519480)),
   (106594, (false, 40849944871700402645898087, 19578374782, 5448941912794075245589710097254473373), (false, 85172174, 165, 26770863999824)),
   (106761, (false, 2793, 1, 5640264), (false, 521876461660161518846016, 2604205141, 362560663677521299572612849832440840)),
   (106826, (false, 8451289245672683311, 20256740, 125040860781413300119492981863), (true, 37431198, 13, 200610602808)),
   (106827, (true, 1588637503803, 517, 2002013745245207820), (true, 18878557761525, 12079, 46282802087582648280)),
   (106889, (false, 106826903186209701449, 3928748529, 17052254092335849895773917598997720), (false, 128373689, 57, 3656082662720)),
   (106922, (true, 24231207791134297, 403926, 2617854524001176696556355), (true, 4066454939710289452999850, 5855942393, 3546069438008919096309901247900531760)),
   (106971, (false, 92488470525, 2117, 143059078656754920), (true, 504703680625, 1914, 225953256690466975)),
   (107051, (false, 565609303981764, 403169, 413612550866132466583350), (true, 2897518555776348, 128297, 123814153341811883577102)),
   (107635, (false, 99275, 1, 13104300), (false, 1133689733648384, 110625, 22582469167443711849664)),
   (107681, (false, 130564769361258229263, 243148921, 72728632520783698302925991896056), (true, 42637707121, 521, 6409936715237040)),
   (107707, (true, 5784025, 6, 10321993045), (true, 5462494936693, 4151, 12007505787388920600)),
   (107746, (false, 12326109852736, 12553, 40993262829760978800), (true, 14018411682, 337, 809814953691744)),
   (107882, (true, 180918, 1, 61774320), (true, 73035128282601100, 782057, 8462320805058497362584060)),
   (108049, (false, 88141971977107865183, 34427484, 872247262320560689164352724849), (true, 17481335089, 17, 2311325994062400)),
   (108114, (false, 5315689504193403, 248839, 296694076462032097050291), (false, 24350, 3, 151788160)),
   (108129, (true, 212097, 1, 84032136), (true, 545535088, 47, 11455639466068)),
   (108177, (false, 6455318495756330625, 23486887, 150726156346116441279387525600), (false, 28407663453456, 20345, 184474149915355294428)),
   (108194, (false, 2302, 7, 254361792), (true, 2235530978, 113, 83099345541408)),
   (108385, (false, 32065, 1, 18539400), (false, 208163303836754548465, 43850729, 146866818698656766191398090000)),
   (108521, (true, 55093996136089, 15246, 363557459725995516085), (true, 120453988028668, 30183, 755219263423845007522)),
   (108626, (true, 269630642, 47, 2019270124656), (true, 26486755314478805934, 6202753, 134607225577888986757623983040)),
   (108699, (false, 1862341392, 131, 4595594224788), (true, 31381754187, 407, 4553116884249696)),
   (108849, (false, 207137316074560599, 1803415, 130658762793402865282876524), (true, 3267379921, 102, 175189788817615)),
   (109114, (true, 33466225, 4, 193338500745), (true, 2695575294088732675625, 128483762, 104117588537707276155664413647925)),
   (109154, (false, 364794351197630037563626658, 59720660017, 2596608558107058279199590267755973942624), (true, 3239964002, 161, 89854859549664)),
   (109298, (false, 20178258506, 515, 2956469064146472), (false, 131383, 2, 151143195)),
   (109378, (true, 16424599113, 314, 1587654424913115), (true, 49606238309058, 16031, 287878894664704351680)),
   (109459, (false, 5491, 1, 8100840), (true, 805370704810874743331077, 1284984761, 704324471991070340753505459433241196)),
   (109499, (true, 99963877, 17, 948056400096), (true, 191043467788026045361, 7642494, 2639093589982026689747472831295)),
   (109571, (true, 216293, 1, 86729412), (false, 17390272543, 452, 1858893483434799)),
   (109585, (false, 47089, 1, 21472584), (false, 2093091759, 188, 149095723267161)),
   (109706, (true, 217533780640476098, 547961, 100288902553158039353881512), (true, 62066411814224648502, 14139161, 457427626456910468198305559184)),
   (109722, (true, 142578, 1, 34378920), (true, 138446661602337885228, 32760193, 856664762102867352241039268340)),
   (109731, (false, 40053, 1, 20445516), (false, 568247969003685779667, 156553897, 62662642002911170750610843137872)),
   (109811, (true, 27329220198397, 15721, 16793238457112887224), (true, 1081409349362105569, 1940898, 1039037300057038209169842665)),
   (109939, (true, 682724641, 66, 12714200646985), (true, 43239176594069053, 560951, 5393944831869250311863208)),
   (109963, (false, 34314134602490574503, 20715388, 189748322906557505634327781725), (false, 18016861023, 1348, 26711190482718735)),
   (109970, (true, 541205, 1, 389840325), (false, 16247700674614323511030, 459217783, 2109234026936449961758716868660200)),
   (110130, (true, 1789341870, 119, 37107674287200), (true, 413019666405, 179, 265424034671806275)),
   (110209, (false, 573485663807830928, 2338117, 139858446737023283490420276), (false, 2013561, 5, 2668639512)),
   (110354, (true, 51312914833392, 17983, 264115143296175061176), (true, 123022356708526, 30737, 724328579693855009280)),
   (110506, (true, 1667138, 1, 2147836152), (true, 11558378887136453878, 46753, 39295732605675526293356450352)),
   (110546, (false, 29954959, 40, 954065230839), (true, 97482299606984882, 94927, 30434486054464151097306576)),
   (110649, (true, 1359897, 2, 1499497461), (true, 26933177887, 439, 2699983824258644)),
   (110851, (true, 8209147555921, 5298, 21765598947802235825), (true, 149680275129853, 16847, 1790336132971160496072))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 100 := rfl

end Rank2Heeg11

#print axioms Rank2Heeg11.on_curve
#print axioms Rank2Heeg11.indep
#print axioms Rank2Heeg11.count

bsd/Rank2Heeg12.lean

lean_all/bsd/Rank2Heeg12.lean

/-
  bsd/Rank2Heeg12.lean — T39 cycle 1866 (goal G854): rank >= 2 for 100 congruent curves E_n (n from 110954 to 117626,
  root number +1, missing from earlier Rank2/3/4Cert files) from two points each whose 2-descent images are
  independent of each other and of the torsion images (as Rank2Cert). Points were found by PARI/GP ellrank on Kamiak
  (search only); every check below is by rfl in Lean 4 core. All tested integers are below 2^256 (isqrtGo fuel).
-/
import bsd.Rank2Cert

namespace Rank2Heeg12
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (110954, (false, 287399830561578, 189113, 67094497296093324425424), (true, 83024490348839722, 862799, 2425612734744364967804736)),
   (111027, (false, 115117107489252107663039, 1018697480, 1632888368682407102926502476284959), (false, 2516612, 9, 13696660810)),
   (111098, (false, 39892533462305892841850, 2524403041, 141181864216801552847760269601742080), (true, 3097570856430625, 166968, 2576824353340416132175)),
   (111129, (false, 4422458897, 283, 513638448979112), (true, 70241161, 25, 87795467216)),
   (111282, (false, 14463127808, 409, 1409434834569760), (true, 78823090834, 305, 21938257410881552)),
   (111361, (false, 15685320455364672, 700703, 6559924199467508569114632), (true, 178102452097, 792, 69140882840146111)),
   (111386, (false, 81862, 1, 21611568), (true, 1434359905430664421, 3551425, 346554532929915219192191469)),
   (111395, (true, 19467225625, 418, 53986131292875), (true, 31282183281845, 12601, 144308651116982999100)),
   (111530, (false, 106875, 1, 10423875), (true, 265165921280, 431, 136127824322318400)),
   (111531, (false, 22148, 1, 16267706), (true, 690145407771611829, 1158479, 559691040447305747023467600)),
   (111538, (false, 82649293502, 861, 703015782418720), (false, 274000599, 50, 856638772149)),
   (111570, (true, 346188270, 7, 6440422575600), (true, 11662830545076150, 273739, 878198176687561970902200)),
   (111754, (false, 536203111346498, 103227, 24621347546458058167064), (false, 27734375, 18, 122587668125)),
   (111874, (false, 375596919101894, 63159, 4670845544438339968520), (true, 109041709, 3, 1138598111105)),
   (112001, (true, 17958412081, 391, 725549930412720), (true, 217776684058537476227115663, 25171529497, 3038373160007216155404175481133461396112)),
   (112009, (false, 439449046041600, 101413, 22322570425984355792160), (true, 46260220519666133122921, 607219413, 4482607204727473947569840824225460)),
   (112042, (false, 606066137570189492, 3212001, 766286168591921509517258980), (false, 434007, 2, 73633035)),
   (112201, (true, 133225, 1, 26218680), (true, 193237541221907810487721, 661678380, 82154283775020121259356438131367019)),
   (112306, (false, 3856, 1, 6969720), (false, 146149550926940133632, 465041679, 293614564678017684939528139374688)),
   (112314, (true, 1075884213658482, 93913, 13772269488824225083224), (true, 3423839616606246, 68881, 197899762754533919567760)),
   (112353, (false, 206943553808, 4433, 999977002594815140), (true, 34392815209, 515, 3184697283238448)),
   (112569, (true, 91718929, 22, 706619168495), (true, 146438588168679, 3647, 1771987274302258201320)),
   (112593, (false, 5530695437243949375, 12491849, 39218901007797402857525874600), (true, 76919653048225, 9106, 669627354067765384265)),
   (112681, (false, 19586956410814464, 438685, 1302270116481142135977216), (true, 445468763727917488225, 37544676, 8784150648083122002615665670545)),
   (112690, (true, 147500, 1, 36550500), (true, 313498894810, 1443, 116404103581381000)),
   (113026, (false, 2179427521159118, 1723683, 15676720797408051926775664), (false, 619856594775, 2702, 428849667529490955)),
   (113082, (false, 4418, 1, 7510600), (true, 67613219238058379371109854, 8020246935, 552738293342990050325185077299948280808)),
   (113099, (false, 2254663663, 736, 2907106761233031), (true, 144485336429, 311, 54763018216474260)),
   (113107, (false, 3422378592204925481875, 1630954031, 17599845308006191959598623611371800), (false, 69627669745037731, 4933465, 726182139559660212111946272)),
   (113162, (false, 41663940261964650, 2552359, 150234472852272790428068160), (true, 769191850, 31, 21118721001840)),
   (113307, (true, 256974034818006531, 700565, 127180033551393152427830304), (true, 45855327013002336161667, 607711787, 4015532374501223642072252401265760)),
   (113331, (false, 97344, 1, 18106920), (false, 399378287910853558668387, 1894771651, 49136440598083330878099442449773052)),
   (113370, (false, 247623870, 179, 57028115125800), (true, 293214592845, 1571, 47471810239526175)),
   (113515, (true, 10758024965, 233, 914611732889400), (true, 10031455380714539695132225, 7010506958, 26405321158603859531864271169084834425)),
   (113531, (false, 3951147752651, 145289, 4763662419923351135400), (true, 5133888648779, 6679, 1905922059806420040)),
   (113602, (false, 31103598850946525439, 63358880, 2537428439776014288340463992191), (true, 2517802722, 119, 97186302301920)),
   (113609, (true, 13344409, 7, 44302853400), (true, 18360686475219179248, 7847403, 72738931172011703387385353308)),
   (113610, (true, 151725, 1, 39171825), (false, 65430335915, 787, 6622618615428725)),
   (113705, (false, 574693239123367126202944, 2388411973, 227994077878286680359217674210549096), (true, 517153081, 66, 3382698302821)),
   (113801, (true, 1046735656201, 1201, 1057666455621648600), (true, 70484895062535587584, 22771485, 323624419789789949196616837552)),
   (113953, (true, 1221025, 1, 1343344080), (true, 11771217469375, 3247, 40175215298135408400)),
   (113971, (false, 81796, 1, 22698390), (false, 25212664570602306221567, 547781538, 3668826716106416168491646361003673)),
   (113977, (false, 197898633565866198997056, 1378285165, 39077863376071753028463337326037128), (true, 120546002246083479025, 20452342, 1215602394397193488864089780255)),
   (113986, (false, 6985370619411869584, 12053537, 39685608179753541170865754200), (true, 61582859526444054002082, 233984807, 15203666444751771342730002546130464)),
   (113993, (true, 161230553556633, 30137, 1569393606092936916600), (true, 9811861254531574225, 5855053, 28191708993590747735991573960)),
   (114017, (true, 1395718610364003361, 3497400, 64995470696256847983501391), (true, 64050731761498783793, 22939448, 179448984267115784163318352215)),
   (114051, (false, 109443, 1, 10616544), (true, 2954388618748, 1147, 5071548719639203802)),
   (114106, (true, 235532050822, 391, 113993623506458256), (true, 16030383397851218, 372511, 316836736948250069575992)),
   (114122, (false, 15171577635444430896506587, 13135286489, 48886516066977903188357813617995667155), (true, 3570034152150, 2471, 6615677259743449920)),
   (114186, (true, 25183772652, 337, 3425928929586036), (true, 1767349421787606, 86039, 65250079547088015609120)),
   (114218, (true, 391379638, 47, 5919111472560), (true, 20533786759850, 13313, 15589390796566947360)),
   (114235, (false, 558024300559, 2558, 371510509650111711), (true, 20468525, 13, 30767952600)),
   (114266, (false, 94178, 1, 19858104), (false, 158903173364479, 109900, 17281472913131089570431)),
   (114353, (false, 190863207827575101477136, 1983310865, 177944555768694001199721247141776612), (true, 334372952632509025, 1228298, 165625937451433196045039895)),
   (114362, (false, 4715065698450136165171, 203085665, 8582453001570538790883232816767), (true, 28115639046991727481798, 108981089, 4708846768933138704257811359941440)),
   (114394, (false, 236697639702377378, 2903041, 454680082354608404590897944), (false, 206622423, 74, 8500479987531)),
   (114545, (true, 269777500, 1, 4431069374250), (true, 4932529287589435504, 1862649, 10919187629279212673881835092)),
   (114554, (true, 406802, 1, 248962824), (true, 11570137710486124, 316391, 165619282461735721712940)),
   (114610, (false, 77234, 1, 23532864), (false, 56387841882843674896, 25497313, 365726475559656133773510713592)),
   (114681, (false, 460277998887, 8143, 5149599168672019152), (false, 52983, 2, 104882589)),
   (114737, (false, 144143690583464795718607, 1167670106, 23080301242616045829547544210410815), (false, 155407, 8, 2894152905)),
   (114907, (true, 260056633840447585288225, 805723626, 127044737477325408035441134146205385), (true, 212055355237081755782719453, 40787479927, 1336608159533125125509345739043549514220)),
   (114929, (false, 73662464, 85, 7098636854784), (true, 414355111054918804849, 60028337, 274696686884700211085120857080)),
   (114931, (false, 396609447379, 3247, 721068821017507200), (true, 23835452, 11, 94508995434)),
   (114945, (false, 213230230352989312401, 432297301, 313659267068019311905508976210732), (true, 36322620, 13, 184972942350)),
   (114979, (false, 206051284656553284, 1955725, 176360564182320676340209014), (true, 2058922065563297500, 1826133, 2902654695656292327172486450)),
   (115041, (false, 85952141036093364516, 51852401, 2754658132799169458084290082730), (true, 266162143600, 1199, 107589898500646220)),
   (115042, (true, 9868113, 8, 20640038055), (true, 3225811842, 151, 106640152301760)),
   (115187, (false, 39764576931121827128355623, 67661545086, 3315867874756228987343652589745857116715), (true, 1345230948982629736913725, 3040685833, 953215084978274537538501677718150840)),
   (115194, (true, 237606, 1, 101298960), (true, 36627459842934761772, 13158841, 185918611400172309172466823156)),
   (115195, (true, 1166445, 1, 1253626500), (true, 6460457937340129905625, 42838202, 518993993842378912534021562071125)),
   (115403, (true, 65817604968025, 19998, 380693143645401860245), (true, 359586948191543574330613, 1742338927, 48601014197067071332553070808062840)),
   (115507, (false, 44100, 1, 22418970), (true, 22169669716261628871772, 298713573, 2922539917444622466516186893325710)),
   (115617, (false, 226145137913065073, 1825241, 148276893756154751242175960), (true, 43731769, 5, 288565820272)),
   (115634, (true, 204404209, 26, 2700231856425), (true, 6182951697, 212, 263381333917689)),
   (115714, (false, 10921031463929543, 1811334, 39658387610262578235923251), (false, 3574350, 29, 183860018160)),
   (115810, (false, 47360, 1, 22999200), (true, 67278651008251225, 437936, 16472427259651846885143075)),
   (115843, (false, 70357, 1, 24410820), (false, 82777928718303, 440252, 204280445945876090070735)),
   (115906, (false, 45968, 1, 22812504), (true, 511552098, 41, 10697959258272)),
   (115913, (false, 2045216333727, 9514, 14716889754032739735), (false, 21011307583, 428, 444003538998495)),
   (115914, (true, 185090134614, 193, 79607999367000720), (true, 7382853560189538, 140111, 603477759262019239674648)),
   (115945, (false, 134374464, 373, 186987679769016), (true, 29757100431076225, 354322, 4477101425049359904879975)),
   (116034, (false, 62673012232556798976, 63108631, 3624699844354376556185491287360), (false, 478578, 13, 13561850736)),
   (116043, (false, 39607849347492963, 999679, 21691884404170845337300380), (true, 19970613822525, 12719, 30445350647373666180)),
   (116137, (false, 5687, 1, 8747640), (false, 11933840937853119449807, 456179808, 2295855300061681631361452741620265)),
   (116209, (false, 1213626678975, 7472, 7021371746841749535), (true, 874325761, 23, 25788955812480)),
   (116337, (false, 2196324, 5, 2825570826), (true, 870192463883548, 63989, 21482160429213424622890)),
   (116385, (false, 4577570361637844472735, 1591493438, 19942144651010255327009867838727575), (true, 1026882025, 79, 23260556062900)),
   (116394, (true, 212268, 1, 81783828), (true, 24011477528593099158, 14092367, 31845908270782109617236859392)),
   (116457, (false, 3787927814188432688400, 323398841, 712450097290449730017181247131260), (true, 1007356199877753, 32423, 31735359989712814133160)),
   (116683, (false, 3975105864303, 24728, 142031554377796424775), (true, 36744796, 15, 155838181906)),
   (116746, (true, 367598, 1, 211335432), (true, 32953192418591137, 14892, 5981995849472705327684111)),
   (116794, (false, 13024514849352962, 380433, 1229644093646275771709512), (false, 52621033690943, 32364, 801118790641749410081)),
   (117065, (false, 18780464901681390625, 20687329, 201282838055685873508500922500), (false, 11587216, 13, 54585396852)),
   (117201, (false, 473577948900, 2221, 228201499356787530), (false, 3367395423, 422, 1195297596073161)),
   (117409, (false, 1786499289000000, 579457, 1664557668380342788347000), (true, 8001123601, 217, 517350625126320)),
   (117521, (true, 856908529, 49, 23685339316080), (true, 69841277888, 151, 18443714228296392)),
   (117530, (true, 136045, 1, 25272975), (true, 630006194686, 183, 500044614165888616)),
   (117602, (false, 1028677140127, 5032, 2834268525456531345), (true, 113682662476936802, 827999, 27022373502521774075828640)),
   (117626, (false, 428371348231422341858, 487270793, 577967216087721183399868220485464), (false, 23044087, 14, 3357080133))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 100 := rfl

end Rank2Heeg12

#print axioms Rank2Heeg12.on_curve
#print axioms Rank2Heeg12.indep
#print axioms Rank2Heeg12.count

bsd/Rank2Heeg13.lean

lean_all/bsd/Rank2Heeg13.lean

/-
  bsd/Rank2Heeg13.lean — T39 cycle 1866 (goal G854): rank >= 2 for 100 congruent curves E_n (n from 117761 to 126242,
  root number +1, missing from earlier Rank2/3/4Cert files) from two points each whose 2-descent images are
  independent of each other and of the torsion images (as Rank2Cert). Points were found by PARI/GP ellrank on Kamiak
  (search only); every check below is by rfl in Lean 4 core. All tested integers are below 2^256 (isqrtGo fuel).
-/
import bsd.Rank2Cert

namespace Rank2Heeg13
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (117761, (false, 15922355743, 508, 3266243363603409), (true, 73718412812254487165945072, 20399444983, 472846096618227802390693676297902083036)),
   (117762, (false, 75853550850, 1471, 66999292088762880), (true, 38574866850, 287, 7332852378861360)),
   (117771, (false, 4158227600330013, 256253, 420466538541621331522224), (true, 44849809, 14, 257523390865)),
   (117929, (false, 2954903552, 213, 242458785648512), (true, 148766809, 23, 1647260956200)),
   (118034, (true, 1509293646, 113, 3104903247600), (true, 449783916342208, 23081, 9445388435347958522352)),
   (118083, (false, 55003668842307, 28927, 608768236426493520000), (true, 605650465523439525, 1890689, 338003401537542033392504580)),
   (118123, (false, 12595939221383424, 394285, 1499709020118816997159824), (false, 38620487275, 2737, 173731633846664760)),
   (118473, (false, 94636377450646916175737, 1370278213, 61931541428066899735528527436679180), (false, 1375929, 5, 3076577244)),
   (118489, (false, 3032377217783487, 278468, 477615585777563687586657), (true, 7195959241, 137, 580542859856520)),
   (118506, (true, 14991153858, 337, 808469423271288), (true, 9931005476992707414, 8098609, 19480588674234514920059345280)),
   (118529, (false, 109658187917487729, 2162393, 179904888856443448440584400), (false, 229620440045824, 255425, 117129194030884914233424)),
   (118826, (true, 172197, 1, 51716499), (false, 38552523118939215668918, 687017471, 7997980172345191674343149583818576)),
   (118851, (false, 586839058720719, 87434, 16803306406060591128705), (true, 189339504, 23, 2457493534620)),
   (119570, (false, 4096094420, 223, 275857838523300), (true, 5658366677188720, 216017, 70800381221035463111400)),
   (119730, (false, 5070, 1, 8517600), (true, 318979496394, 1153, 156122105830819128)),
   (119770, (false, 68614, 1, 25714416), (true, 14552473005, 311, 1062528688088175)),
   (119777, (true, 706762225, 23, 18713613665760), (true, 6226243704513, 6991, 5291390190416489040)),
   (119819, (false, 158407396, 53, 3737586827310), (true, 71204209769308, 2529, 600804742285488701422)),
   (119833, (false, 238137226590288415367494468, 69959421389, 8271075427866024627226972024945773396030), (false, 1007, 2, 15210735)),
   (119841, (false, 16641, 1, 15309720), (false, 2596667146669956000000, 231397781, 299019598411758943302064531566000)),
   (119922, (false, 79350, 1, 25328520), (true, 846989910118, 1063, 769461429848751640)),
   (119930, (true, 202005, 1, 73058475), (false, 187121857955324670670, 484740487, 385477567042383288192785743119000)),
   (120003, (true, 1085773, 3, 116241580), (true, 7085342564053, 5749, 15628184370600461920)),
   (120106, (false, 18652597362, 431, 1671918650999688), (false, 155090943, 88, 11420894044161)),
   (120235, (false, 318457093275, 2077, 231040277331827700), (true, 44465780859475, 5409, 295580360444549223700)),
   (120265, (false, 1348695402182845262615, 121359302, 42168195471143991157687134698925), (false, 281735, 7, 3124344000)),
   (120315, (false, 54756, 1, 25069122), (true, 2445892211100, 1123, 3817848425729287950)),
   (120379, (true, 11576723900, 143, 1217114687127990), (true, 8098939454642056367100625, 5330638846, 20891806208515098389996537557131747825)),
   (120385, (false, 347778422711079187349025, 4146997853, 1203582193873001076316110688034127300), (true, 582655928211487603225, 34341831, 13640370884129315462898868142300)),
   (120561, (false, 7513455090854561121, 30954181, 315969193319042591223080208660), (true, 3561411952, 167, 70065142937524)),
   (120570, (false, 2467478707350, 6971, 8347604706904507200), (false, 25230, 23, 10131028200)),
   (120658, (false, 126151337204, 2685, 305685424861689844), (true, 61957778166, 245, 15316380486541464)),
   (120745, (false, 93775, 2, 145086975), (true, 25737202542394225, 70498, 4127850171415980252312225)),
   (120905, (true, 205570358175625, 35618, 1962340019939216755875), (true, 33959239074642394362551881, 13905474906, 143535034219114934870476620005976718979)),
   (120946, (false, 22743, 2, 72877749), (true, 49636862422, 43, 11058649025722536)),
   (120979, (false, 3522161626682498466436, 434671705, 1340353151606119006136737003901238), (false, 12867946860431, 22980, 224476795692280975753)),
   (120985, (true, 3893292092057812890625, 45082881, 242441814349345587689410046112500), (true, 3049228130925340375880401, 3799423287, 4364655415970988556420892329337065324)),
   (121049, (false, 15636891404131094916, 16960721, 123032918810613876073790194050), (false, 8303, 2, 44113839)),
   (121074, (false, 45968, 1, 24014744), (false, 4179356738, 211, 220075643889856)),
   (121129, (false, 16548372500568668691481, 401016233, 1321884724935201130165539770699160), (true, 1296687112328333401, 2580137, 1156330804427104853493970920)),
   (121193, (false, 29239666267192814423104, 9938900525, 2047100358154937564584496749584085944), (true, 676299225837846203777, 74651108, 915239507547982408754650149825)),
   (121243, (false, 3817888563085362047, 12018864, 33398195047396618053371432255), (true, 1651192686389198123833725, 2393758247, 1924817171844582982860471216176183940)),
   (121323, (false, 2800882033689600, 260233, 408779535242415612754080), (false, 1053890538627, 7999, 7895371718774185380)),
   (121330, (true, 1627120, 1, 2069753400), (false, 49751615727275, 71823, 4400706917505075960475)),
   (121346, (true, 224310529, 26, 3126798388065), (true, 32075348427353, 10634, 164196240072709936707)),
   (121418, (true, 17468247500, 343, 1328837796900900), (true, 122319138508419605302, 30899519, 431584982430476763622437227040)),
   (121465, (false, 68060393626176900, 1087837, 33029465464016270152626450), (false, 100881936, 29, 161292809412)),
   (121513, (true, 548926561295937135472, 47775813, 11098449200036338162211233551500), (true, 193584443134586482577941225, 25647534191, 2453113893842083992334208676596548152360)),
   (121602, (true, 51130061502, 7, 11561507619339600), (true, 90018381063546, 5255, 853482081528691075656)),
   (121659, (false, 2677314624, 367, 836469926911080), (true, 496705308723, 1277, 320928408028638072)),
   (121691, (true, 239419633, 24, 3542265558217), (true, 921330112935623159751718897, 84197057976, 9817987644848332605880376324389027554121)),
   (121857, (false, 58284200779281, 34645, 1024136404269374924172), (true, 3281314608061742958064, 145624745, 115834443962628287190481850980988)),
   (121858, (false, 1498662, 31, 143348685480), (true, 387736519673042095600, 19120417, 7584366239601544614357687298440)),
   (121985, (true, 869037713344, 1143, 796397275260583928), (true, 2594627378125225, 82209, 125314841158509865035700)),
   (122169, (true, 7804318033, 196, 550852452180953), (true, 14737231609, 344, 347233894958195)),
   (122171, (false, 15014039908, 701, 7122432724352946), (true, 551049707414703868, 2123463, 10131600325954493724646042)),
   (122370, (true, 3052830, 1, 5329724400), (true, 63313895515509445935227445, 1245357251, 503786588352522295932360909850941364725)),
   (122546, (false, 3211223, 18, 70917648949), (true, 1758157100531541271152258098, 118483431431, 15207805012271455283750567473105120825392)),
   (122689, (false, 1425049867328, 4239, 2008047519268934984), (true, 2322565249, 97, 97126703277360)),
   (122763, (false, 233794957624755316653, 79199791, 11218450679072373405288009639060), (true, 51635858558316118396, 6265145, 369426237107946049179778271194)),
   (122835, (true, 137275, 1, 22706900), (true, 86773266285, 47, 25560938452595400)),
   (122969, (false, 5582143, 16, 73197891039), (true, 987269875207, 1341, 956042105760906164)),
   (123026, (false, 57998511874, 71285, 150557236674599669376), (false, 403202, 3, 654800048)),
   (123027, (false, 16170853179023, 21024, 208781693249976498295), (true, 4555430578725, 4471, 8184272762218880340)),
   (123162, (false, 52589992085748, 71161, 4506743956534988086620), (true, 10064496, 5, 30398304456)),
   (123315, (false, 3002480124730892100, 5993357, 5643000904631257592036112150), (false, 212415, 2, 205176825)),
   (123473, (false, 4085028672321976920128, 880413183, 6111483865686274299286867764049160), (true, 1073212707952, 2943, 93260210782857580)),
   (123515, (false, 4107852802235, 10337, 25420850347200890400), (true, 21089432315, 401, 1029863744099400)),
   (123602, (false, 52372059502549769018903, 3521331006, 350538174829427720651248745973982805), (true, 36111411648, 449, 4966706753495760)),
   (123635, (false, 711722029618688913183844, 2412216461, 88467559469413719717949895070121854), (true, 1418524, 3, 1047876382)),
   (123747, (false, 1053, 1, 4015440), (true, 6899284605231723, 149029, 525635398005860038191840)),
   (123915, (false, 1711256415, 124, 34655718128625), (true, 511010577165, 2009, 74976884320724100)),
   (123986, (false, 126958983799150, 53737, 3771989432598953665680), (false, 3331360483583, 7212, 10078360413632757631)),
   (124010, (false, 9442714410, 1081, 14051819352370800), (true, 35303666984383210, 181121, 6589114014579496862498400)),
   (124089, (false, 11127711, 10, 18318144663), (true, 432766871152, 1829, 80496879507468116)),
   (124090, (false, 105014883600, 2173, 186806591129736600), (true, 33220056337690, 15161, 98155528148067429600)),
   (124194, (true, 360150, 1, 202877640), (false, 25816584948350, 21337, 255592358816790091040)),
   (124243, (false, 129008943, 232, 75941116250265), (true, 1105453938652, 2931, 302559483637112590)),
   (124321, (false, 295594634200446718401600, 2246915231, 301031785716414567485438672698845240), (false, 1943705727, 164, 119951887023423)),
   (124571, (false, 65403, 1, 27113736), (true, 5912969604712416531481497777, 139381562764, 414856273334000464456134234488260858237161)),
   (124683, (false, 1108082063, 108, 31353387912055), (true, 2656887025, 138, 61444015657505)),
   (124745, (false, 6347627584, 533, 2777809528676664), (true, 4058159643749549650225, 475442, 258519728752464655486389098537025)),
   (124899, (false, 26877, 1, 19996488), (false, 60573296207, 2948, 266733305336709737)),
   (124915, (false, 64715, 1, 27180300), (true, 702263211628860, 65939, 11797508473419559168950)),
   (125041, (true, 3838951521350512, 175047, 14874480932117051751436), (true, 44820810896254687, 218039, 9405151706996447772979224)),
   (125074, (false, 423315798575778766763775, 5729772488, 2657381298156739446199037222781014655), (true, 90050869232, 343, 26659678241254344)),
   (125105, (true, 525625, 2, 116590875), (true, 6939433447469993564171100, 3724660819, 17699434123212441102037922854164713850)),
   (125115, (false, 48165, 1, 25342200), (false, 81694386015, 842, 9876711706079625)),
   (125209, (false, 1393667775, 152, 94635699943455), (true, 48288601, 17, 222194830920)),
   (125274, (true, 66439308, 23, 38643027228), (true, 1077850013305254, 90889, 9893714736401695780560)),
   (125395, (true, 78434055045, 89, 21964543281928500), (true, 97496940025, 722, 22587394379264325)),
   (125466, (true, 476850, 1, 317683080), (false, 8537460779832955045203750, 11637953257, 42931559429900683668621767707483899600)),
   (125491, (false, 335721458831778752187506832, 94827147047, 19739810109574121553435711098405957985828), (false, 2987041793012741505024, 191412469, 191035553671960394867173303084800)),
   (125531, (true, 1365215255356, 975, 1589045118675980246), (true, 850720713149692, 9987, 24810383099860434649034)),
   (125697, (true, 1887600, 1, 2587616460), (false, 1978544488805597480625, 170214757, 135999859326199373155620577994100)),
   (125762, (false, 2507444942275, 9697, 18299995854223982535), (true, 110306773850, 703, 30266422987786440)),
   (125794, (false, 3292777013660176, 193889, 194769737247743279241480), (true, 2769200545930985582217, 101938466, 128466856045549555491685831169541)),
   (125882, (true, 905288548252418623225, 68554486, 20617319046668825408086926192915), (true, 13393907316134697905484633, 5328573134, 47241002809901533684266013827897074115)),
   (126121, (false, 715149485036632996, 5364385, 3009029010015045608200654758), (false, 1616584193005824, 146885, 88005678220043562246576)),
   (126242, (false, 2421863, 6, 5984722205), (true, 445450425477496229282, 51871489, 6082257173886288132420425954400))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 100 := rfl

end Rank2Heeg13

#print axioms Rank2Heeg13.on_curve
#print axioms Rank2Heeg13.indep
#print axioms Rank2Heeg13.count

bsd/Rank2Heeg14.lean

lean_all/bsd/Rank2Heeg14.lean

/-
  bsd/Rank2Heeg14.lean — T39 cycle 1866 (goal G854): rank >= 2 for 100 congruent curves E_n (n from 126410 to 133131,
  root number +1, missing from earlier Rank2/3/4Cert files) from two points each whose 2-descent images are
  independent of each other and of the torsion images (as Rank2Cert). Points were found by PARI/GP ellrank on Kamiak
  (search only); every check below is by rfl in Lean 4 core. All tested integers are below 2^256 (isqrtGo fuel).
-/
import bsd.Rank2Cert

namespace Rank2Heeg14
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (126410, (false, 7939701025000000, 2824729, 89871838776084264655050000), (false, 4811933297610, 6529, 5320198784791027200)),
   (126474, (true, 473232966, 53, 6800870865600), (true, 238463089973058, 14039, 3662232415226592490872)),
   (126497, (true, 39753025, 17, 98455450080), (true, 310430019375, 1481, 77576354797397400)),
   (126498, (false, 1789127792805097413876, 119171665, 6881786513935686352009051150068), (false, 21083, 11, 2222464445)),
   (126673, (false, 492291562256, 5245, 2420520741046436028), (true, 3034853425401818225, 370769, 5286879741098262651739331040)),
   (126737, (false, 16951334778388231021397776, 12541308545, 43182902839644718985590927661046612132), (true, 2407287545804047201, 476510, 3734744707195594931018134551)),
   (126809, (false, 6611425434395097688133316, 27940001401, 253968423585078206840949002752237466190), (true, 919941232064089, 27898, 27741266511212308921515)),
   (126826, (false, 370819530192447, 319484, 249177925168487602750113), (false, 28322, 11, 2582586552)),
   (126874, (true, 1416492, 1, 1679082636), (true, 133987762460882, 18617, 1465044612833584213464)),
   (126906, (false, 4212, 1, 8231652), (false, 10689407815368243, 735853, 7018139066223792022252191)),
   (126987, (true, 248877892611822747, 1394891, 14891749911074348312970060), (true, 4463427326261354332, 3903983, 8497169885473290829432996910)),
   (127010, (false, 50960, 1, 26262600), (false, 1551340621606162908560, 117770817, 32874925769727667629020755501400)),
   (127018, (true, 74611841, 20, 471972216639), (true, 2460532252943441161, 3675860, 2765679015626677341879255909)),
   (127049, (true, 95511529, 14, 901151276115), (true, 422710441537579929721, 40729314, 7533603120611081105247434130235)),
   (127099, (true, 2011072, 3, 2345664104), (true, 468438306277669, 28463, 9890647025215742756880)),
   (127114, (false, 78070491434, 791, 4241776711985280), (true, 59142210933546, 17183, 351512337062839951440)),
   (127131, (true, 460452796, 55, 5433693171094), (true, 235771180238307238565177163, 25586911717, 3387146758554929134455574750462628803212)),
   (127145, (true, 2271952225, 38, 107938430882025), (true, 4676806076695, 6047, 1097087799991134000)),
   (127169, (false, 14101750903684927, 339572, 477462953204828054126943), (false, 3459463, 22, 114299382981)),
   (127195, (false, 35836327788156781205, 30189963, 660005970733237324857510839500), (true, 164919542537922320492205625, 15578402722, 2080483268837730129768013333597254454875)),
   (127259, (true, 145475, 1, 26883780), (true, 135098965595528186652, 25218473, 1257326355764815479123125922846)),
   (127379, (false, 96134751, 134, 22405966190985), (true, 70369252230605614463822113, 14141790108, 550263897049491681315318780102987174577)),
   (127483, (false, 261183312817955918933099, 3343333065, 715919149717448784407547660062219024), (true, 79366457048835747702025, 497837468, 20511012878544907888792533966298395)),
   (127490, (true, 229900, 1, 91730100), (true, 8281221920510, 1961, 23789166261188420400)),
   (127563, (false, 625604924463, 3482, 1118753003327677425), (true, 284333077924992889, 1351210, 86971344734041622084913787)),
   (127571, (true, 525625, 2, 91395675), (true, 434450643382701, 13055, 9044116077647881537824)),
   (127659, (false, 135032043, 97, 13869202684896), (false, 5355525, 11, 33529573980)),
   (127729, (false, 8499923664815168, 277329, 454088351659190925870136), (true, 649598586273, 1432, 479115112183300593)),
   (127730, (false, 581972207570, 3647, 1217615930438622000), (false, 161798400, 37, 843569666400)),
   (127770, (false, 126150, 1, 7203600), (true, 1148676001245, 2761, 652604719315263975)),
   (127803, (false, 98397, 1, 25583220), (true, 1744153660303041496301937157, 87766374063, 60128861524287249922214291843845889811320)),
   (127857, (false, 87975, 1, 27518580), (true, 13338084677006704, 51105, 1539940918369147786180108)),
   (127865, (false, 17636520879398811015, 77472641, 3222108454182089013795069969600), (false, 5399931015, 242, 381234917197125)),
   (127970, (true, 7400150, 1, 20127778200), (true, 8605762266645, 5059, 23345707567700737275)),
   (128002, (true, 9077630912, 113, 850750872076560), (true, 230806836643128086752481, 86139020, 110884112837914434119899645322136879)),
   (128195, (true, 5533480071643225, 206422, 65766580933308941498475), (true, 317155160982713805, 734329, 174316492220308652296189800)),
   (128249, (false, 141062910023711653446656, 1359943875, 71617607876631490626053052967720672), (true, 29230398961, 400, 3559081797931959)),
   (128251, (true, 759966020557, 1967, 501788355603877236), (true, 1078745302882182731680837, 2883557089, 169058133965459168545795552699905744)),
   (128338, (true, 263538, 1, 118163760), (false, 19967274464330002, 394727, 152102784330180486982320)),
   (128451, (false, 365724431430605327, 1717436, 59848002384562963799396407), (false, 191368725, 43, 1945925241840)),
   (128459, (false, 1315786042449941546945600, 5099807931, 3522622850267673858931088082368186840), (false, 175883860827079298310400, 1584234161, 113319822652247274054449052923663280)),
   (128506, (false, 4802, 1, 8898792), (false, 13526755767, 326, 218929549406757)),
   (128699, (true, 554791730625, 1436, 362886444971669025), (true, 422573048857203721, 791320, 269654468505881744748053019)),
   (128731, (false, 146876585692265495217201743, 35653471008, 874351503636222149118595020829801438999), (true, 68960637, 7, 570265949436)),
   (128737, (false, 1580675103, 116, 28176905375535), (true, 448328324275225, 56758, 3605984236253577685515)),
   (128739, (false, 12727139458506002244, 10350485, 18959214250338270204007557846), (true, 481137836881, 1614, 239305357394921935)),
   (128770, (true, 191180, 1, 61785900), (false, 19803677422239, 15560, 107156534640178490991)),
   (128795, (true, 1443182440005, 2911, 1134361875949163400), (true, 2518472231017727351539225, 317155078, 3996685766950974303952405264838620725)),
   (128890, (false, 6309447119852000890, 14619841, 67359749892862959939993667200), (true, 261250110601056066490, 4163279, 4222492092486432551807816796000)),
   (128899, (false, 9351965999512077443, 8550847, 3573945635806543692416262576), (true, 401354883, 47, 5666783435592)),
   (128915, (false, 78660, 1, 28645350), (true, 636947119065619, 66819, 6884549382437689732372)),
   (128954, (true, 493612, 1, 334756044), (false, 19341174757830926, 836313, 12251561135092570240749800)),
   (129217, (false, 6210732358003472, 2213703, 49900957399199802050793820), (false, 260898960713, 1731, 146123750608840160)),
   (129259, (true, 257725, 1, 113192820), (true, 9916860207330372449307115429, 241152821815, 644133047177058780671357108390215498820592)),
   (129290, (false, 8951465610, 383, 1581915879736800), (false, 777035, 3, 763502075)),
   (129305, (true, 18399615434695, 2493, 78849415642742420500), (true, 168535881861655, 17383, 2128347164263896622200)),
   (129363, (false, 106508889747, 913, 5498019943186680), (false, 51300524964, 665, 5734247626586466)),
   (129394, (false, 198524019375, 1244, 11661613225179225), (false, 2560992398, 231, 324491139898064)),
   (129585, (true, 168540, 1, 44241750), (true, 21940004667226781628025, 29416369, 3249745875479023309078931184433100)),
   (129610, (false, 129600, 1, 579600), (false, 11942673210979290, 305459, 207928474441194680222400)),
   (129626, (false, 193491527, 54, 4516765598323), (true, 1157666697369, 2630, 787931752038538653)),
   (129794, (false, 635287975, 74, 8033117304765), (true, 780362018, 23, 21714837619392)),
   (129859, (false, 1569375, 4, 1705799025), (true, 138476443487136309, 615607, 48166466916739604047887300)),
   (129914, (true, 4105349329, 106, 245852776727175), (true, 213518453697, 808, 90544622067808161)),
   (130017, (false, 1491718701153, 5503, 4450368353418025200), (true, 2853546203544898355594913, 2533774417, 4609496592080976608574715591710453720)),
   (130090, (false, 81723408010, 5087, 962083163521225200), (true, 22888606732290072377782090, 8654770559, 99084433387449573775761873878944203600)),
   (130123, (false, 12744900, 53, 1304096877510), (true, 514307965954972, 26319, 11483143772091628462210)),
   (130145, (false, 1283841522627040548900, 149592361, 93665960475178042534127409607350), (true, 3390150625, 38, 197088002531625)),
   (130146, (false, 35643, 1, 23631309), (false, 229776648642900, 48631, 3104271615441078199620)),
   (130177, (true, 12279074418167556099409, 193690355, 1248407609928170050206061593880452), (true, 30618957635601142119781225, 15189888769, 32901249537462185011066114358473303860)),
   (130402, (false, 223384111681600, 43289, 1480630636489907569680), (true, 1101700459761691870242, 58255889, 33487491126637773437647926358560)),
   (130465, (true, 39753025, 14, 191906199225), (true, 135812185630106252089, 12688598, 1563690387336915959934908461413)),
   (130474, (true, 83199925625, 358, 23508803172458925), (true, 887486464225, 316, 835979839233326895)),
   (130561, (false, 49027257197531875584, 25143905, 464964755758167432946737283536), (false, 11053876119169, 592961, 152624063411739280583040)),
   (130594, (false, 1850915427394, 5201, 4093567343624592240), (true, 10923138, 7, 29257594752)),
   (130659, (true, 2286848041, 132, 10340333634475), (true, 2027555724249649415259553, 3482800532, 1800649075986680308756113922071824753)),
   (130690, (false, 165860301107774, 50481, 3719395396709384458976), (false, 617694035, 87, 19202505601825)),
   (130715, (true, 342633251306385, 29242, 5995303413642651668625), (true, 508825338621665, 50042, 8787276226754769813225)),
   (130754, (false, 18083, 1, 17413929), (true, 21789104965579199583199098, 7938976777, 94153551666876080047814347540351019448)),
   (130755, (true, 214245, 1, 78556500), (false, 382803942018176166381, 136678679, 47200681789708717569848894118072)),
   (130954, (true, 472898739969, 460, 324642508193842047), (true, 99133019276221225, 809348, 15644898454496900358245205)),
   (130961, (false, 963362752746833670400, 89287369, 12491674897949295382788680020080), (true, 5075624358997124126512534225, 163959812261, 260475626889068386705164199811674456459980)),
   (131201, (false, 132854532621611084767, 138266524, 28870171259645936424911655407823), (false, 277134273496143, 565399, 698204841127499340577656)),
   (131506, (false, 3375812886606, 6745, 9075523582683187728), (false, 4965918527, 204, 162089841207007)),
   (131537, (false, 90480103603183, 26678, 228584250018329195025), (false, 145302500, 81, 10254395553850)),
   (131577, (true, 3239540719070007, 108697, 161767208224311879669480), (true, 972722297436388801, 2472210, 539742001221414973334878201)),
   (131698, (false, 231903, 8, 4057400655), (true, 70580211560940272, 615481, 13263825378208286164888200)),
   (131818, (true, 10077148225, 274, 190752987308865), (true, 3523813880483810170096650, 599172247, 6614244968083314710336911416567388320)),
   (131826, (false, 54402539150, 1017, 29160706038471280), (true, 324440738214, 265, 184725225188681688)),
   (131945, (false, 5579779214295, 6559, 2462619247646844000), (false, 20834460295, 403, 723576813210900)),
   (132017, (false, 12066837440715041, 353305, 1232794820341273789196448), (true, 3217778265473, 3712, 4761269457192350655)),
   (132131, (false, 848443564198020068100, 89274209, 18169402545363587431977558608490), (false, 161256163674267071, 1143600, 24941054433554282125018783)),
   (132186, (true, 4869228, 1, 10740637908), (true, 26356283879032576241286, 300057143, 3817771401604416970306935631872960)),
   (132321, (false, 1920996, 5, 3732640758), (true, 8243114992, 223, 450769387330556)),
   (132483, (true, 56999175025, 502, 11029565623483745), (true, 72674285853, 703, 8502168124246680)),
   (132730, (false, 132496, 1, 2867592), (true, 329751375904938650, 64489, 189356106876463409410600800)),
   (132745, (false, 801483508295, 2493, 175151790533233900), (false, 295875878855, 1513, 37677466230931800)),
   (132905, (true, 27804695, 13, 86420692500), (true, 51191504252980584025, 15303622, 290781176249769316508859722325)),
   (132985, (false, 101169797184, 877, 4785969097587336), (true, 215576262729166647025, 35976681, 1905674417297422490558334889900)),
   (133131, (false, 195943318727997, 116167, 24998357702654702727228), (false, 123611799908523, 83569, 10245354203675062289664))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 100 := rfl

end Rank2Heeg14

#print axioms Rank2Heeg14.on_curve
#print axioms Rank2Heeg14.indep
#print axioms Rank2Heeg14.count

bsd/Rank2Heeg15.lean

lean_all/bsd/Rank2Heeg15.lean

/-
  bsd/Rank2Heeg15.lean — T39 cycle 1866 (goal G854): rank >= 2 for 100 congruent curves E_n (n from 133219 to 141266,
  root number +1, missing from earlier Rank2/3/4Cert files) from two points each whose 2-descent images are
  independent of each other and of the torsion images (as Rank2Cert). Points were found by PARI/GP ellrank on Kamiak
  (search only); every check below is by rfl in Lean 4 core. All tested integers are below 2^256 (isqrtGo fuel).
-/
import bsd.Rank2Cert

namespace Rank2Heeg15
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (133219, (true, 118735435269, 799, 28550465905409580), (true, 7381786103387549, 197711, 449511757381116635278920)),
   (133289, (false, 1357094998433193616, 4964525, 3485156161681054444371089652), (true, 6480026150964878760537601, 1456975578, 16479754753752438572075466325613929255)),
   (133331, (true, 9134224627, 127, 848447415055416), (true, 24731450557, 119, 3877975454724624)),
   (133458, (false, 37908, 1, 24913980), (true, 1161682609725, 2459, 900663597058939065)),
   (133530, (false, 25230, 1, 20827800), (false, 3598962091350, 55589, 782760114361830178800)),
   (133609, (true, 8205796292848, 7083, 13559065320855882212), (true, 1463740551158289827405143, 1205915041, 1755237446695019693925324214640341488)),
   (133707, (false, 71875800927, 758, 7271686536713865), (true, 5306228977291725, 93529, 377019215551197761004540)),
   (133849, (false, 6193862944299849365001, 2173996105, 49784326025618019642553778575521432), (true, 10020841053633, 7868, 17839842098043922977)),
   (133858, (true, 788629150, 73, 9444141317520), (true, 452745235911152, 42791, 8099627502959918983560)),
   (133921, (false, 4492731485924750916, 5908589, 2742802440210664067945857170), (false, 16474235904, 625, 6372826189277856)),
   (134043, (false, 24059, 5, 519770636), (true, 1256717510670762256291707, 1721569907, 1336576345891600265368306670212182180)),
   (134123, (false, 99275, 1, 28415640), (true, 18847398445112590617275, 335167511, 1554483165588707874205311970817880)),
   (134241, (false, 23068571520543, 23681, 344178229258647525456), (true, 4499018545921, 437, 9542663827387980380)),
   (134297, (true, 17682025, 7, 69012022680), (true, 230959791019256247, 708743, 106155091828220850941491680)),
   (134458, (false, 90650, 1, 29898960), (true, 109403964883834408643205214, 21856853385, 926329193381900089805984601414561092312)),
   (134474, (false, 4369631061867693602, 7115811, 10915959073708291629735839368), (false, 16326857140199, 11850, 38334690243389195299)),
   (134617, (false, 3046491504147847, 22771729, 3852936989245708569157100400), (true, 28981349627299423225, 2605183, 155941602492016040595758778840)),
   (134643, (false, 4107, 1, 8624700), (true, 53499849963419780300760483, 18829269563, 176677572860399871500743190093257143180)),
   (134779, (true, 3234083161, 154, 27987463311765), (true, 4239708812117, 5593, 919806969570622104)),
   (134834, (true, 88897778, 23, 500269172208), (true, 136260208, 31, 492077180952)),
   (135051, (false, 73101, 1, 30702420), (true, 354262972, 9, 6664713686666)),
   (135058, (false, 3992783823, 212, 288897453888855), (true, 831380646250, 2463, 128696974207139800)),
   (135226, (false, 26, 1, 689520), (true, 6511300464850636, 27583, 525348364530923077276860)),
   (135235, (true, 437019025, 52, 5002680536775), (true, 1438474167585, 2384, 1458329369391647025)),
   (135394, (false, 89908, 1, 30354324), (false, 34217601052043150, 657579, 8787541703333643464349520)),
   (135442, (true, 4520572201, 90, 294855202752949), (true, 29068467279282, 6089, 154366888609584284160)),
   (135481, (false, 3991636584360000, 412829, 1436831114384134435572600), (true, 4903181577383149078225, 180068358, 152505752927139194133456254620495)),
   (135505, (false, 51984, 1, 28531236), (false, 32027533739705, 93309, 6674274312826684425400)),
   (135874, (false, 50176, 1, 28284480), (true, 14751092322, 329, 138203173418304)),
   (135889, (false, 494439696, 65, 6488791735908), (true, 16445241969685712949481, 322928214, 1070105418014861773001986440417325)),
   (136002, (false, 3487607726144482255872, 440260831, 1543102298895350967964429987627200), (true, 843443317602, 257, 774567412027713360)),
   (136115, (false, 3544466234576400, 424933, 1447966851024917436038700), (true, 7087468500035, 3383, 18407060897999327400)),
   (136234, (false, 518, 3, 27905696), (true, 50196355953727189194, 10208353, 341117472987414550387268095920)),
   (136241, (true, 13687615460544077168368, 15745959, 1601365143074648243006656606141988), (true, 26560965813573236612449, 434217031, 1100996997767136863026453283546160)),
   (136266, (false, 135252, 1, 6102252), (true, 17663601077616, 7675, 66128860024962369336)),
   (136299, (false, 18891341009501495547, 116893561, 8094367114969424782848197440752), (true, 5520695429819377, 10912, 410193529904837951588681)),
   (136362, (true, 50930993884635340002, 11860873, 336705255610069634166781389240), (true, 86015468099720318988, 433607, 797746332734209897746277902660)),
   (136441, (true, 4214809, 1, 8648467800), (false, 1449377467103, 4392, 2644805707865708849)),
   (136451, (false, 113612629479184596167568, 1146328991, 46757182201854213628907148945975804), (false, 71978256, 37, 1462448207220)),
   (136459, (false, 2081642984100, 4069, 1267099096316491710), (true, 37099050031900, 7353, 221453097042390409790)),
   (136506, (true, 274306256934, 983, 125965464370073280), (true, 20585457395596812, 364297, 1402628198091539032226844)),
   (136577, (true, 318301265501900527, 1273749, 128918561882902343527823540), (true, 35481341421682506025, 5138047, 210254861734636976900356342980)),
   (136618, (true, 36114356258, 263, 6623980122879240), (true, 200631183461702845642188, 608665121, 86959892660063765061775264276112220)),
   (136705, (true, 31992124095, 13, 5722219333287900), (true, 1958792250480025, 58958, 84102996911167500057675)),
   (136785, (false, 1815, 8, 372955275), (true, 1400047581160860, 91129, 30622633583977549081350)),
   (136793, (false, 118336, 5, 1175715288), (true, 502425564404824530625, 2154362, 11261785386659983529720744749575)),
   (136834, (false, 32343238506847447049543, 719627766, 11338987688869211322992003567714291), (true, 77570832577298416, 715607, 9267404929751219508057480)),
   (137057, (false, 103432348881, 965, 24048520383216972), (true, 4211369163737624971804, 159970445, 151276980647295846744663806380842)),
   (137067, (true, 141267, 1, 12850740), (false, 117010304887822500, 943541, 11845010405030144997743550)),
   (137074, (true, 217774589774, 199, 101595822090374640), (true, 1050188858747442, 70711, 25786303118891036300496)),
   (137179, (false, 4091126080177643371898607, 5775398234, 4144779052524480373387612343976154833), (false, 2014929372389766349668543, 4667636156, 3133282346390851327239213621221292129)),
   (137306, (true, 331994, 1, 174164640), (false, 1992824622305114, 237641, 334525002235080208166160)),
   (137361, (false, 2776097819320410450591, 253415462, 441163754428699476200533725650025), (false, 544887, 2, 52127523)),
   (137402, (false, 178290260406, 12475, 9028651212224069328), (false, 29767, 2, 94685325)),
   (137441, (false, 409423173632040654162041823, 60768422801, 6069312739953957384045626567672939813736), (false, 7170689965367738728447, 365470156, 1431038810243412677758183149598593)),
   (137571, (true, 52323292, 19, 119144135726), (true, 522265137746127069, 971167, 365596541543750709621073680)),
   (137618, (true, 51977665350, 311, 11455041546216840), (true, 201947079438, 353, 90424251178313520)),
   (137851, (true, 473208988, 57, 3322796333822), (true, 277594283228044605138225, 1397424352, 35706191548398379687988842619741655)),
   (137922, (false, 4423367299473808962682362, 42391524427, 521193441293233562673632681780971264920), (true, 32709612, 11, 160893549180)),
   (137978, (true, 150822, 1, 23652720), (true, 105236791538, 839, 13142885607061320)),
   (137995, (false, 108900, 1, 27969150), (true, 124376060380, 571, 40893169168115550)),
   (138274, (false, 7134080637033894834846, 6335586895, 468794899749862302436040165279354208), (false, 198868703, 84, 13470007504591)),
   (138345, (false, 5503716, 13, 53308831302), (true, 98422671625545, 1823, 976422966147017764200)),
   (138346, (false, 115113401546, 1057, 34996974909021840), (false, 976144, 5, 3278225496)),
   (138395, (true, 1974025, 2, 2662215075), (true, 777244123195, 2253, 293208380280516500)),
   (138409, (false, 16160400, 13, 67982706420), (true, 41362196279985166481157025, 9513198582, 253522842780740320455303273218094016585)),
   (138481, (true, 167281, 1, 38380560), (false, 7189461648, 473, 2555295703063116)),
   (138491, (false, 466792345872378847, 2266642, 366888527898181528429654857), (true, 1900912, 3, 1978812836)),
   (138521, (true, 21444710524400, 5957, 96663032123411929860), (true, 3156531848686441, 101689, 158032467487322425302360)),
   (138522, (true, 1413125658150, 529, 1679220436581076320), (true, 236153218087935372, 723649, 109211901852597073219656540)),
   (138530, (false, 20230, 1, 19492200), (false, 25596978342446, 109731, 8438120378624092789808)),
   (138603, (true, 66068335673488362094923, 397008529, 16026818735186270141889440638036080), (true, 134593840129500994640832, 819660241, 35653060467991123668580741323500520)),
   (138747, (false, 700200851730134907, 2534641, 461561196030872003845764840), (false, 416768627104221, 55105, 1259977646725969116708)),
   (138785, (false, 2170645903665, 4201, 1671757164046466400), (false, 8467134065, 329, 1141808454618000)),
   (138819, (false, 7465256013376686243, 21184687, 168995406470330111028867602064), (false, 25432810188008976, 8711473, 1680072452398164829975574220)),
   (138882, (false, 41021798187055188, 563647, 3290917356609598004536260), (false, 319040078, 61, 7261495866800)),
   (139001, (true, 19321278001, 357, 1071944503495480), (true, 282681612575683033, 1425889, 3354477800022427227602328)),
   (139018, (false, 1976851153567390129898, 148518919, 104225873165880350517324061508160), (true, 86532064938, 521, 22906719866368800)),
   (139153, (true, 1870835806225, 959, 2552907098615427840), (true, 30702801344896116169240889263, 436005212273, 2730867208859428956141461201393446193663640)),
   (139154, (false, 26896, 1, 22390920), (true, 91152807622793, 24898, 281207263520916650757)),
   (139281, (false, 5859343232245356394812977, 11234018209, 40115233336243946247903503962999138472), (false, 84994832, 49, 2981806903972)),
   (139426, (true, 176418, 1, 45400608), (true, 12122111180025, 1046, 42201997091253992685)),
   (139449, (false, 42527, 1, 27387388), (false, 57752309370955449, 643601, 265531854842530526293560)),
   (139539, (false, 27792, 1, 22796388), (false, 6678850172393755152, 58138643, 1218800692060211088340029774828)),
   (139665, (true, 771994140, 11, 21444549316650), (true, 545980599025, 683, 400545132195644900)),
   (139771, (true, 701506441179, 593, 586109690931827040), (true, 1982405996288, 3509, 1385306099423554128)),
   (139938, (false, 587160326400, 2833, 733637810734345440), (false, 1590297762, 257, 363091089493440)),
   (139978, (false, 39953235938882, 25787, 531396794093334678600), (false, 6506631, 10, 31613803347)),
   (140146, (false, 16001739665999775, 796328, 11058388790241103814170095), (false, 1399253431452398, 343263, 615485955080820903741584)),
   (140194, (true, 1625780158, 49, 64132615639632), (true, 2682138820358866338, 3424097, 3471097859079646915332621408)),
   (140514, (false, 157439256546, 1345, 79185569199916608), (true, 5627938690242, 313, 13351274877577402656)),
   (140554, (false, 14549959090178, 13627, 82652761279205327496), (false, 5315849775, 284, 730040511653145)),
   (140585, (false, 98529349999, 1988, 171639053582633799), (true, 18812620506842686225, 11556609, 5098544352036876995424041300)),
   (140730, (true, 477405, 1, 315203175), (false, 2116429012533750, 172469, 166153247258761615914000)),
   (140857, (false, 865955482911, 2920, 774402873575856687), (true, 478298936273343736638025, 1281463193, 289533703245979274822773182298948680)),
   (140865, (false, 46442600095691102994735, 1150766633, 38935079559995562964261202431932000), (true, 947711985025, 453, 922173084663723100)),
   (140866, (false, 33858, 1, 25160256), (false, 4347067192442908736, 85282961, 2136116777340712997735907728880)),
   (140987, (false, 4027325018034846703, 6886184, 10709172905882451741766917255), (false, 13285811, 15, 104996494988)),
   (141249, (true, 434882183961241, 44135, 7023166595549949581164), (true, 857501806293135015036, 59797033, 20292635038747674391277511948750)),
   (141266, (false, 15318, 1, 17380824), (true, 42930654853, 77, 8893415872475667))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 100 := rfl

end Rank2Heeg15

#print axioms Rank2Heeg15.on_curve
#print axioms Rank2Heeg15.indep
#print axioms Rank2Heeg15.count

bsd/Rank2Heeg16.lean

lean_all/bsd/Rank2Heeg16.lean

/-
  bsd/Rank2Heeg16.lean — T39 cycle 1866 (goal G854): rank >= 2 for 100 congruent curves E_n (n from 141290 to 148738,
  root number +1, missing from earlier Rank2/3/4Cert files) from two points each whose 2-descent images are
  independent of each other and of the torsion images (as Rank2Cert). Points were found by PARI/GP ellrank on Kamiak
  (search only); every check below is by rfl in Lean 4 core. All tested integers are below 2^256 (isqrtGo fuel).
-/
import bsd.Rank2Cert

namespace Rank2Heeg16
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (141290, (true, 161290, 1, 31242000), (false, 360398206639104710, 1621411, 53984863097522433135271200)),
   (141322, (false, 26520212616788067810211, 3274226095, 246688588937022005849165727946745913), (true, 5124139189506722501143382, 6003705737, 1258734535063928209456921468560249120)),
   (141395, (true, 178605, 1, 46116000), (true, 14673161725145045, 287041, 1080608545726091927159700)),
   (141410, (false, 500485976469627210, 26345641, 69436524007258906610843991000), (true, 6198024790, 209, 40223875341000)),
   (141513, (true, 158025, 1, 27956880), (true, 2068642911152577847151309569, 4720441525, 94086603167517279526498724471656663289872)),
   (141554, (false, 3126292515345775, 298688, 684131706052111461151545), (true, 150556553714588947019248, 1027250249, 7307905432713488330299669402611768)),
   (141578, (false, 223060331868355818, 1267009, 20580516747902567891914800), (true, 3384390498173147458858, 24966569, 196821711893213708714438892972000)),
   (141681, (false, 22382361, 13, 40241129040), (true, 149847931839201, 21623, 1645351691273811302760)),
   (141882, (true, 233818, 1, 89867440), (false, 15802301777834, 12115, 53914496394703980464)),
   (142009, (false, 66588438045145023, 828356, 18357827241663502536988191), (true, 325333369, 47, 1555312473000)),
   (142018, (true, 4350148316361, 5390, 2875367844211832859), (true, 16208635040460176588239842, 3230442487, 64982517648787700365596949626748525920)),
   (142042, (false, 72420998844289259, 6796575, 1765642629214035523390288789), (true, 3610393982, 23, 216889145523960)),
   (142059, (false, 1246762722314408649901056, 3107034853, 637816998737894389268502907391695200), (true, 11503013026430523, 237143, 887643703049180508668952)),
   (142194, (true, 146250, 1, 13080600), (true, 1035120629299692, 80147, 15669255104607770173404)),
   (142339, (true, 1349797, 1, 1559460756), (true, 10780631117684153881, 3509814, 34925646670488226629834391115)),
   (142449, (false, 1430474089537129536994329, 17805777161, 53988755176977345125440964464565347020), (false, 67767, 2, 147277053)),
   (142530, (true, 102589350, 19, 898949165400), (true, 8377725630, 137, 726668470155600)),
   (142593, (false, 4183502400, 173, 54549436982040), (true, 836782950993, 1882, 610306766768461455)),
   (142618, (true, 1868125, 3, 1855197575), (true, 2425614405774686, 116545, 71897049808883735055816)),
   (142690, (true, 176890, 1, 43969800), (true, 1598852159601409900, 1788879, 1937479415975541573830872100)),
   (142817, (false, 443695933999, 6160, 3597688455541547751), (true, 1251339643132992512, 982831, 1391257100481469677337099680)),
   (142834, (true, 1152707088338, 1039, 1226472485514873168), (true, 2269738297953522, 93791, 90054061229412968444064)),
   (142986, (true, 435726523734, 1559, 173498689718474400), (true, 16332279113457111344418, 298540897, 1305398314015677883686711582856008)),
   (143066, (true, 22746284, 7, 103203433740), (true, 2340180067698, 1169, 3567409731939534888)),
   (143089, (true, 2289713, 4, 55046697), (true, 5785577836325939629953, 176706652, 279576639418860064850927277830463)),
   (143201, (false, 42849, 1, 28284480), (true, 202557104046517689121, 26297761, 2514790856508664771390610009040)),
   (143202, (true, 235248, 1, 90525240), (false, 530870657468224932150, 61108781, 1482712753866139275669315281160)),
   (143234, (false, 91, 1, 1366365), (false, 42689442556290791831150, 550468533, 1618424162964305641796634740298320)),
   (143265, (true, 5946696802225, 717, 14500413363314478700), (true, 1453567107240735, 5711, 55417941029565474935400)),
   (143273, (false, 16552695733194783343, 17309128, 161135294078001343232342923335), (false, 64771447, 58, 3843744721245)),
   (143323, (false, 23689905410770936314528475, 24849752911, 415047636519664619868460873461195322680), (false, 47324725383531024, 602845, 4733232954848748689348724)),
   (143395, (false, 56644, 1, 31352454), (false, 4676742959, 360, 1229997086772839)),
   (143418, (true, 2773650, 1, 4613134680), (true, 41720748102, 233, 8372025042868080)),
   (143474, (true, 47687378, 17, 162659449008), (true, 33260962032, 73, 6064394390810616)),
   (143553, (false, 2145177227084275725087, 255738623, 423344178069055862320686121806600), (true, 15904488769, 290, 1305742156071353)),
   (143594, (false, 37457994908422, 34957, 1049177303475954287712), (true, 428695861, 25, 8679452891109)),
   (143747, (false, 44569217431190958724, 188805725, 34208196949726001894944070267226), (true, 868896545597870674204, 67058805, 17115250394054770614236214651358)),
   (143801, (false, 4362769063721625777743, 321933648, 941287432603025851848460370981609), (true, 135986092866496338928, 24462051, 1227915482637605883678884459132)),
   (143891, (true, 88193853, 23, 418318301808), (true, 74306008910135475, 610931, 13998333442364043681059580)),
   (143970, (true, 144150, 1, 2734200), (true, 264010894419195045, 13561, 135654051916094799058616925)),
   (144251, (false, 558026852798464, 145517, 70941917341458720473280), (true, 27635184228753379722033, 429424388, 1245222180795759081479922002162007)),
   (144267, (false, 7840121088, 1033, 13613352647933520), (true, 34408060339823207499259227, 15392142313, 23227114032951953618170548889848312840)),
   (144274, (false, 79876232150, 8847, 3191376533710170520), (false, 55133438, 21, 235791974816)),
   (144314, (false, 68113150916034383339, 51652451, 3127530022125309663203042388405), (true, 84531628334022418951078, 651689039, 16925995625727443250590114307099312)),
   (144361, (true, 409549428225, 544, 260665576247001345), (true, 153664589619540574255681, 753698580, 50941750336446857625020134462690079)),
   (144395, (true, 23522805, 1, 114084194400), (true, 84755783834875827102025, 194562622, 24623454898670129993142352658331075)),
   (144530, (false, 769938132814226, 80873, 15217531011966143498832), (false, 330643400256, 1997, 271476281683274928)),
   (144658, (false, 5690999, 150, 7764590661499), (true, 13358971792122607737522, 303484897, 112527943140324107593534407404880)),
   (144771, (true, 2718177, 4, 2345010129), (true, 1053848711067094850457, 60777188, 29479292478772867583608485739581)),
   (144849, (true, 24719560903129, 12945, 23261050120522722508), (true, 161227157869380399, 463249, 63523104973806772947917160)),
   (144866, (false, 3667025842704, 5113, 1812428220276261000), (false, 17880539618, 10729, 2229846769363621296)),
   (145123, (false, 754265028635172639, 6010840, 4506380709520195108200102609), (true, 40275252331875, 9409, 242244548419659693600)),
   (145202, (true, 498002, 1, 336166320), (false, 2573671461807016790207198, 10577760651, 25734621593728025204434046559019684000)),
   (145257, (false, 27174327468599518845641703, 25730442173, 480884937767873735997984874690565140020), (true, 145044928, 27, 1193751780760)),
   (145265, (false, 12080248095897916900, 20030357, 198171455679182808972232396950), (false, 3234879376, 169, 147757674508332)),
   (145266, (false, 45012, 1, 29302812), (true, 15947679378246, 5375, 61441492863109595544)),
   (145371, (true, 490821, 1, 328434120), (true, 3296291870746725984006768, 361874293, 5984546509303860653911748474682099012)),
   (145561, (true, 19050902864567596009, 7714362, 74058792758628821253173174995), (true, 25223337753011858635935721, 13163253235, 1514805658709316681133966892371390644)),
   (145667, (true, 5279453475186725, 97697, 370063105885605009256260), (true, 117119712083622525, 870527, 13392003990627674236220880)),
   (145705, (false, 916151824, 85, 15695128070076), (true, 201770158961, 340, 90316453731091959)),
   (145731, (false, 565051420453616067, 3023167, 906635064772304410338873792), (true, 17251996, 5, 70040999294)),
   (145754, (false, 278516, 15, 17306626148), (true, 37447556972, 281, 6895893575995116)),
   (145826, (false, 76328428176, 857, 20757059211808920), (true, 2722808000480367288217, 116960382, 96699062240713808417094984448739)),
   (145865, (false, 257632824093159843904, 82150069, 15249647358170109376728096274056), (true, 143505185803225, 24147, 1384647285544968538700)),
   (146018, (false, 727914840650447472090000, 20034519377, 50000119349086609946445211005742013400), (true, 71751538562, 553, 15044220766766400)),
   (146066, (true, 32097245027022, 10831, 153764552139782357424), (true, 27359625192108722, 406273, 2139387427379807175760944)),
   (146154, (true, 231852, 1, 86664276), (true, 1018719522, 23, 32421085833096)),
   (146258, (true, 1666350, 1, 2142742560), (true, 1825070082578, 521, 2464997844421576080)),
   (146307, (false, 626469145216563, 65497, 957515037436224467640), (true, 150607051144725, 11929, 1830535515549530572860)),
   (146433, (true, 314608, 1, 156183580), (true, 2053826533906350769839, 82519415, 81372039065445411262074607005312)),
   (146489, (false, 19728937490393, 14623, 108068723449875167496), (true, 2976466249, 103, 138494413174440)),
   (146546, (false, 2329192364377150, 137683, 73068783708522932560320), (true, 7457522, 7, 5496676464)),
   (146562, (false, 34108012692, 1829, 90328236328266300), (true, 126533222400, 217, 44942776398823680)),
   (146659, (true, 228085949537, 1192, 44289104280452079), (true, 4455443806464352257, 3749036, 8337466287691181800130188161)),
   (146746, (false, 33984436857674375, 1075674, 30668308544872751464362925), (false, 114242, 33, 54014074568)),
   (146809, (false, 27376463, 96, 7077744703639), (true, 3000795858869008369, 1452522, 5170454510093640783515791135)),
   (147035, (false, 98432787600, 937, 26203911848207100), (true, 26281144348235, 4471, 133885425604358240400)),
   (147041, (false, 2074218499000921, 221713, 315345310328628534771180), (true, 1750012118639, 2789, 1752180731623243500)),
   (147065, (false, 79027120407323659659815, 1944015199, 154654114599921650754913188303429000), (false, 324494778215, 2122, 328835363905968075)),
   (147298, (false, 62148886686, 1505, 81717999062644512), (true, 4526822846703838779522, 82161367, 297133896437496328122397204023360)),
   (147433, (true, 84488323120487, 23431, 222621975978981664800), (true, 1005678953816398849, 2192935, 715260880446678639234275868)),
   (147570, (true, 63336270, 7, 500760176400), (true, 318349573558245, 46309, 616757268949330745475)),
   (147585, (true, 23354006460, 191, 3472825122792450), (true, 66536730570025, 14911, 472148619810339168100)),
   (147658, (true, 1017460822, 71, 22126462438080), (true, 14025861295837039125389248, 2738383359, 52364480558220338957904005135723993680)),
   (147721, (false, 7224022631667263078587441, 7038014077, 3127875111080693576736113511826356600), (true, 1330672, 3, 64711556)),
   (147729, (false, 5673596080766783, 693776, 5338836215268440803535071), (true, 2654477127, 73, 130609991176236)),
   (147746, (true, 2750001122, 17, 144193920593184), (true, 9697101527901438, 208807, 713764053330666616579392)),
   (147819, (false, 417208791420155307, 1738351, 103079607780584529149068728), (true, 388470621, 47, 4147755283980)),
   (147890, (false, 37315925853435, 23447, 441260713404398394675), (false, 3085726, 5, 3577631232)),
   (148065, (false, 57711178091176215, 1037558, 35693969643767142842734125), (false, 42689535, 17, 19221678000)),
   (148083, (false, 52397618449995140517, 19840543, 184903791775469893816094337660), (false, 302579507739717, 129749, 43043765460707070179640)),
   (148114, (true, 2349250574, 97, 91667353913520), (true, 4625440204338, 5537, 1892868633925222512)),
   (148242, (false, 21238099005807594612479102, 29047797163, 568071826614514314324235717315909708960), (true, 59074437, 19, 192287292435)),
   (148506, (false, 107114602614, 22691, 25025063404775691600), (false, 267438, 71, 387143728344)),
   (148601, (false, 181123518363513308944, 47582765, 3815899891394894825997814719204), (false, 79312258382049, 44201, 2487218707093667320620)),
   (148673, (false, 17473647721835764543, 165226972, 16966089589407388773385142324385), (false, 465437476, 65, 9100344831018)),
   (148699, (true, 27789317937, 356, 3404514894433449), (true, 2667042034255830377474033, 2951882656, 3807020181154344222696119710812564663)),
   (148722, (true, 804972, 1, 709789500), (false, 11931586520060606, 286445, 279435942054351659814272)),
   (148737, (false, 202718660780775700920384, 4799087345, 1539647880587752607688464998823337336), (false, 27262221291072, 20137, 280904765171338981080)),
   (148738, (false, 1087548833605799, 387450, 735464772443471330949949), (true, 2170019830321663600, 3555791, 1595008106610299027053964760))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 100 := rfl

end Rank2Heeg16

#print axioms Rank2Heeg16.on_curve
#print axioms Rank2Heeg16.indep
#print axioms Rank2Heeg16.count

bsd/Rank2Heeg17.lean

lean_all/bsd/Rank2Heeg17.lean

/-
  bsd/Rank2Heeg17.lean — T39 cycle 1866 (goal G854): rank >= 2 for 79 congruent curves E_n (n from 148818 to 155185,
  root number +1, missing from earlier Rank2/3/4Cert files) from two points each whose 2-descent images are
  independent of each other and of the torsion images (as Rank2Cert). Points were found by PARI/GP ellrank on Kamiak
  (search only); every check below is by rfl in Lean 4 core. All tested integers are below 2^256 (isqrtGo fuel).
-/
import bsd.Rank2Cert

namespace Rank2Heeg17
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (148818, (false, 98192, 9, 3777149080), (true, 25729332754, 375, 2400900355217008)),
   (148827, (true, 174748, 1, 38284190), (true, 2393450582829789, 60715, 113976916948267786575288)),
   (148946, (true, 116127355801072, 27919, 27642294752971082184), (true, 3125171297347358162, 4516903, 1289462452786913478783275376)),
   (148962, (false, 14762, 1, 18009640), (false, 8608835153244150, 315971, 1125192269954917857840840)),
   (148969, (true, 280803548281, 857, 137040712547135880), (true, 1684257825634041600, 3354107, 217404211292314396001314320)),
   (148985, (false, 1059571916581815, 126793, 69920880055305225055200), (true, 24416495103025, 12681, 23272518057981777100)),
   (149090, (false, 73984, 1, 35207136), (false, 429265088146, 1741, 92537736014091408)),
   (149186, (false, 82582514450930018, 1151369, 51641037116009668323849216), (false, 18891952704, 433, 2835083095614000)),
   (149506, (true, 289100, 1, 133043820), (false, 1125972723928011093946, 143002955, 95381074943385143731998346554408)),
   (149545, (true, 4593655, 1, 9840276600), (true, 69984304046152775924095, 189125151, 18459874426112024632940711575524700)),
   (149569, (false, 4762789232582927, 233988, 459726368542772827288247), (true, 67396288752, 319, 17044606460094564)),
   (149611, (false, 15631, 2, 74794335), (true, 118898784697, 334, 40592397392475621)),
   (149674, (false, 21008162, 43, 1264801520472), (true, 248231013086, 155, 123662632277278584)),
   (149753, (false, 371641242925014302656267703, 153286984249, 67454685256693368060888934583626759815480), (false, 1901284329167, 4944, 4312997971480385495)),
   (149819, (true, 1139025393, 76, 24996027645897), (true, 160278490289253137, 995624, 24133313764317449101778439)),
   (149843, (true, 1424877912103725, 25231, 53664992962384668470760), (true, 8905450973477809, 243670, 36736374118227082040127)),
   (149929, (true, 23890087, 7, 111110566944), (true, 389795162442193729, 1609625, 20201007735544179809710692)),
   (150186, (true, 4961388, 1, 11046017268), (true, 139501135397736420054, 18636481, 1528136382984380931322205053920)),
   (150226, (true, 3011058, 1, 5218401552), (false, 237951436862, 2421, 413534218401654112)),
   (150259, (false, 11169164024919844, 448621, 2970052767748810541802510), (true, 4243536188, 149, 170859170925162)),
   (150314, (true, 5316305569, 2, 387627785473455), (true, 1012844280156361687817, 22108414, 32149068396108532189939220695749)),
   (150370, (false, 99275, 3, 425257525), (true, 62300996225302424043520, 279123833, 15273021970522524270704575489670400)),
   (150545, (true, 516143999800140625, 481498, 369965397648410323061199375), (true, 2755198497146241721, 258066, 4573266014663238968795789531)),
   (150585, (false, 19929351616929340215, 15282473, 129364933941822521889922374000), (true, 6188182225, 199, 130034329003700)),
   (150955, (false, 708355950448055, 69366, 4275301137787969819075), (false, 651605, 3, 962317700)),
   (150994, (false, 54290194688, 3189, 357567326495768096), (true, 10491969033, 202, 869883255518277)),
   (151097, (false, 565382743023950939025, 432186913, 670939216921301582532997693307460), (true, 482696474438931258025, 27093091, 10321267779873767118211469180820)),
   (151170, (true, 453750, 1, 288189000), (true, 92543931045, 781, 2397285312788925)),
   (151307, (true, 2700549397, 127, 60094522105800), (true, 2267928367456990225, 2533266, 3086547916343370345478968785)),
   (151417, (true, 319774686240025, 31807, 5019460281166439828280), (true, 4239806833897080481, 4579620, 5784204900314689826956646129)),
   (151466, (false, 358802993906, 1985, 285669621633626328), (true, 281613150, 43, 495548977560)),
   (151529, (true, 17929477801, 337, 673730920916040), (true, 11954776282325489335723063, 8351911951, 19311559134432031726772924961293947392)),
   (151530, (false, 151230, 1, 3706200), (false, 456642093750, 8401, 7220251128999330000)),
   (151547, (false, 136367, 6, 2014043185), (true, 4163812986730469093197, 62109551, 266019525333208497386737745010060)),
   (151707, (false, 59124411777600, 24601, 540121973592860338680), (true, 2172386857266944125819569003, 104376666923, 65710497924806954186030579780204681795340)),
   (151715, (false, 137275, 1, 23934300), (false, 108315606363396496, 1044665, 41213188291017313954314492)),
   (151721, (false, 45001181856432400, 545557, 795337017017126332445220), (true, 14596782525951025, 309969, 90682527007634460847660)),
   (151986, (false, 682202610322357961485218, 3032750443, 1007778001695407576579017944748476864), (true, 8958049, 4, 25804643345)),
   (152098, (false, 6424686400772738, 2118241, 54699198728584419602505360), (false, 340771600, 457, 586357667521080)),
   (152106, (false, 29694, 1, 25706520), (true, 143088119233025385429, 29034721, 759548764325810331331328650005)),
   (152115, (false, 3797398815, 158, 36025288425), (true, 10788532598105098024560, 149891951, 1062868439951150364142671437108100)),
   (152122, (false, 5987656906683756159193483, 6273828883, 21274846444568960022311527549874235), (false, 1775344238671975686143, 120431316, 55195898113776587393372197871105)),
   (152186, (false, 146629536578, 1377, 95169209895106984), (false, 107266197863, 4662, 1082735042088170141)),
   (152283, (true, 1459264178794107, 97777, 3795618445565388361920), (true, 51316183732799232, 392339, 10341010340058740303754000)),
   (152313, (false, 259073650935225, 41549, 723191410127245571820), (true, 7831380928, 141, 639133859316680)),
   (152345, (false, 1397555887245607512900, 228341261, 292316508778572541730167332998850), (false, 336662108956231744, 1543117, 78400084365012595722634104)),
   (152354, (true, 18085500698528258, 220007, 2220806084604579099953472), (true, 2190582477898257673, 996522, 3234454910341984921318950203)),
   (152355, (false, 46389, 1, 31256316), (true, 303676767187605, 24331, 5053173018572656391400)),
   (152401, (true, 208849, 1, 65259600), (true, 2592516638435177153, 597287, 4173369175346876569381940112)),
   (152546, (false, 25538, 1, 24033744), (false, 4168085017461264, 172897, 119423538252056790952200)),
   (152706, (false, 379572152628, 5141, 2475536425975546956), (false, 40778702, 29, 777662618768)),
   (152889, (true, 284089, 1, 127621520), (false, 13765025489, 2699, 130658403324457160)),
   (153017, (true, 23707019571864025, 115252, 3636749538354414926269155), (true, 412457197984869200753, 48983828, 3816848169341625719620730144265)),
   (153066, (true, 206082, 1, 62641224), (true, 131189405241274572, 571223, 43938670552270818744950244)),
   (153426, (false, 4368, 1, 10135944), (false, 794867150, 79, 15052552918480)),
   (153537, (false, 23417907187782927, 2622877, 161598073013423875459657620), (true, 88014166401535806721, 2688310, 825646897934607519540065000231)),
   (153681, (true, 467313, 1, 301687848), (false, 6087383037501866399376, 592833161, 4187207442658810286627577485792820)),
   (153835, (false, 43547837419515275, 671397, 11261609740619499082906700), (false, 8998728044944, 7825, 8350107275252021796)),
   (153874, (false, 142100, 1, 22252860), (true, 8991768959594682943085478, 7062561551, 14046644707005071512107291402661729032)),
   (153890, (true, 306140890, 39, 3452478020200), (true, 377181447390, 863, 220692935879354400)),
   (153923, (false, 42442887823, 632, 9163595137646985), (true, 148836364259177759749475, 522305281, 55087495542209474480624102602280040)),
   (153978, (true, 43224558968844, 15865, 125836016528351385828), (true, 259003512552246, 38405, 2003916632658998205456)),
   (154010, (false, 1271025032814451600, 4305041, 2881304512130738122157387400), (false, 7005690, 7, 7425194400)),
   (154049, (false, 219303, 2, 269669589), (true, 11025721376282368, 65607, 1155643164358649820678608)),
   (154122, (true, 381121878, 49, 1780621919280), (true, 18132554178, 343, 12949188262200)),
   (154146, (true, 193821, 1, 51728265), (false, 34234378530143, 16172, 124566809404897429361)),
   (154249, (false, 3050429243205974713, 4722553, 2777747084280087262202855184), (false, 15179328, 137, 11279342769816)),
   (154290, (true, 400710, 1, 234099000), (false, 9095652810, 259, 471003049438200)),
   (154322, (false, 282632772717650, 47543, 3436906481169482795760), (true, 324822836018, 337, 184857355102379280)),
   (154354, (true, 7762283456, 217, 240052795453680), (true, 391193693998, 961, 227851251062049552)),
   (154473, (false, 126852, 1, 31395870), (false, 7618793561997783, 289298, 911692697825774921473395)),
   (154513, (true, 75517788025, 658, 9628055462357445), (true, 45163105856350316641, 17096570, 581922446257433472967891689)),
   (154745, (false, 7268014332022500, 293101, 948961117412373262611750), (true, 920777616981976467025, 63719702, 20425037611978495034055315490575)),
   (154771, (false, 3576584486596, 14701, 62895646299382987530), (true, 3018537212, 139, 22618869223866)),
   (154786, (false, 3219295801922, 11497, 36252446462038174368), (true, 26513057538, 391, 1946904838667712)),
   (154986, (false, 61347, 1, 35252217), (false, 216393376212, 1579, 148925035196739828)),
   (155057, (false, 65702623808, 759, 15511787300372840), (true, 245392225, 7, 3842226233040)),
   (155130, (false, 1530150, 121, 2809528473600), (true, 2909687554355805, 135911, 27240074352526538979225)),
   (155185, (false, 18130995482418225, 825773, 14038213603305625162119900), (false, 1457534656656, 19465, 70963417779419811828))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 79 := rfl

end Rank2Heeg17

#print axioms Rank2Heeg17.on_curve
#print axioms Rank2Heeg17.indep
#print axioms Rank2Heeg17.count

bsd/Rank2Heeg18.lean

lean_all/bsd/Rank2Heeg18.lean

/-
  bsd/Rank2Heeg18.lean — T39 cycle 1868 (goal G854b): rank >= 2 for 100 congruent curves E_n (n from 3017 to 103595,
  root number +1, missing from earlier Rank2/3/4Cert files) from two points each whose 2-descent images are
  independent of each other and of the torsion images (as Rank2Cert). Points were found by PARI/GP ellrank (effort 6) on Kamiak
  (search only); every check below is by rfl in Lean 4 core. All tested integers are below 2^256 (isqrtGo fuel).
-/
import bsd.Rank2Cert

namespace Rank2Heeg18
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (3017, (false, 567, 1, 70560), (true, 62241854627349841777515625, 72551680447, 474795461781680014942913063141428053000)),
   (3433, (false, 7568674612568094377461824, 241772240165, 551680289804613261628112407475301747576), (true, 1021772167374817, 389532, 28097992659543027393745)),
   (5273, (false, 1185065520662770681055625, 16353155369, 831985280207075267696420320988309400), (true, 114169225, 119, 922806776760)),
   (5314, (false, 3136, 1, 240240), (false, 11841054832848957351661538, 95840706217, 162947032282313950585188089891021756544)),
   (5449, (false, 887115932159342827521024, 13204874965, 320487447162549421136019458155543776), (true, 1488493561, 233, 56282038418760)),
   (7609, (false, 6727, 1, 291648), (true, 14341372477897696854537721, 9135076463, 54257525337276303058149306825232066200)),
   (9257, (false, 36028584541420265743936, 1984470725, 1055527862054636653748254998697512), (true, 34778199361350327856374025, 53259872927, 134480837003070599204558129782365743880)),
   (9451, (true, 77077, 1, 21237216), (true, 74058860853450626046544573, 86826791039, 173858030087723792010647485387158982068)),
   (9497, (true, 19706551449957996184433, 1167750908, 2085163071659925710655185071289865), (true, 165726456911609395993455625, 128897598473, 652364434824701333292474798905498074200)),
   (12241, (false, 840536064, 265, 5222821157184), (true, 467845935052798400242164241, 135556184351, 8873058976510021880600128425132029572560)),
   (13441, (false, 3198609597680769918723169, 18257412529, 5610841027823480793135593603705401680), (false, 45951147766542118464, 75482305, 415285714873172928736169616216)),
   (19146, (true, 22188, 1, 1670292), (true, 1233208985010948057634722, 3538894031, 1343341695458990516843392458289221816)),
   (21257, (false, 21025, 1, 454140), (false, 68121720713454764319797824, 68467846805, 600272686823837551159403723873004806424)),
   (23737, (true, 1374728029155625, 18377, 50970371330120319253800), (true, 2671710102266884001769712, 7430185149, 3805599756582342024080670113275100260)),
   (23907, (true, 24843, 1, 1064700), (false, 9639021406513550642150906307, 635218305157, 37358859969997260975476286297965752697820)),
   (26018, (false, 2418857047451697429096608400, 404306833193, 172046593225693088745429090828773536215960), (true, 6026283032761, 12090, 11475799102992790259)),
   (35123, (false, 305408003635380706847808912, 138559867637, 10506424981145520034503867103588106763420), (false, 303831, 10, 1928752947)),
   (35147, (false, 22084932100, 1093, 5307011658432330), (true, 1808814375180730405103224252, 198554591247, 49449085955903819554819350692434736273110)),
   (37201, (false, 59846583695380272146161, 5133179281, 239351537489861269872730622485599600), (false, 4230201600, 409, 296851696884240)),
   (38467, (false, 401687, 6, 839942675), (true, 3416134973912406889007881, 9001217950, 2585176004874511324466195080166983971)),
   (38531, (true, 113162554808521, 40222, 1004694578186401755645), (true, 9413641983067433740249484733, 483930110767, 260214069148932074424032016509217737188392)),
   (40289, (false, 13380559271171313264246784, 806424040385, 95840814667904979460713234384216428859264), (true, 74398257993039435361, 33436543, 510740630569452079997502739200)),
   (44386, (false, 1022407031929261006061139842, 157130878417, 12615807561794469582565369608590848370928), (false, 1410608077686691984, 6291713, 1244189972394129745091301480)),
   (51019, (false, 230090672350770952440877056, 140430131693, 14857226243248863852129413091705365221440), (true, 17810229067, 431, 2012403057028056)),
   (51586, (false, 3746253834376828435095077378, 393270202529, 431148169962723663166477999756243153027776), (false, 3878000701696, 11617, 11385580114165028640)),
   (52985, (false, 12627680036330470120489984, 168009568285, 5314559514279992106630493856977670060064), (true, 43247050918715971650625, 23241231, 8993622435189153098489251928015500)),
   (53539, (true, 2677357, 7, 874988136), (true, 2872832212899129746349209449, 76184784354, 153076931668963734061321986699908231083475)),
   (54121, (true, 103790532124988253477025, 928877796, 29862358799813068347138801925460465), (true, 2280218241582249538030431721, 129758814799, 99811314605522047930411648051168386156120)),
   (54233, (false, 2868294210741703744, 17095685, 26400885287456047103703063096), (true, 99657631643842737046452025, 41466269089, 350950289832162357438433530132731092920)),
   (55681, (false, 227194214361216842510673984, 64272774865, 541557878632762927656477201433740588936), (true, 1553069281, 49, 60977722252320)),
   (56243, (false, 10785181996881875, 470321, 644045744072930361676800), (true, 1897136004389103177119442275, 83250570839, 80868842647477610415225845805491579965920)),
   (59153, (false, 547121437719758678768209936, 100430202665, 5566089087248725996573857307981955396988), (true, 17922993229969140625, 11166939, 69153839559961389486499577500)),
   (59795, (true, 27332355625, 382, 4282277712589875), (true, 174401525467507616840392605, 30603026471, 2181201662339824117711828280370340106400)),
   (61139, (true, 8273903521, 306, 543365519653385), (true, 18554389036777631143186093, 9589227761, 76165615720540605736317839394704636328)),
   (61259, (false, 2345495161855798725926822279424, 6602649712345, 1955695592539769241934812032226407552646136176), (true, 17037239628609704650609, 505713600, 873849130880266534976639300003977)),
   (61402, (false, 1844000791683611, 291615, 209776868775417041522963), (true, 923351454201303229264658738, 114424448513, 13800670269554781297814495892427335215320)),
   (62218, (false, 6589231952293651156111850, 14547955583, 29265297940473544045872289557835306320), (true, 25374038680225, 712, 127815658204740796785)),
   (62266, (true, 762136166, 7, 21039982597920), (true, 4620317889244534375065075148, 270420785929, 53276812266202629527973584006407935039492)),
   (62353, (true, 4710034783142408855618928, 5887312249, 9082412401094598636751754995167934260), (true, 43457039743622304719810927, 25379112721, 109431636039414197297990480085745551480)),
   (64849, (false, 1767629883477182379658158961, 204639964817, 86680299168143878380714207920056779157920), (true, 19678758961, 433, 2170619278290960)),
   (65258, (false, 10804647351549990141048313447, 406907283154, 9046750631767073560153810109455437849315), (true, 3241058, 7, 951847560)),
   (65378, (true, 30466749442328569, 222870, 5287593322868982990085997), (true, 16551890817323758944450722, 10416593743, 60841598695067507845486609583469435360)),
   (65571, (false, 65709225606578261626666627088, 1388308188331, 27673341914917761367622368876920438429822164), (true, 5191659, 5, 11224140048)),
   (65761, (false, 14156314643617344, 496625, 941799051758854825433304), (true, 44033095786134936239374805281, 778615452961, 3923118407012843251707153751287664024035360)),
   (66754, (false, 25688621701989693745727618, 48607055473, 788692498771561330705982974873957502976), (false, 140327485526434816, 2541761, 152762687375529190977151680)),
   (66802, (false, 28556641355690550987899246, 26874462495, 207811376712135019774484360210113661008), (false, 385832223, 76, 69298167855)),
   (67306, (true, 28333417, 18, 96289002901), (true, 521333289730869034232605801, 87749512314, 1291719041615496740474633408490046867435)),
   (67331, (false, 34664044678240420958970851, 231512310511, 21246276315488932019283154823512893152880), (false, 39886480656, 1145, 15727080042560172)),
   (67659, (false, 38475, 1, 10916640), (false, 1201389942396192723643543846307, 5586590205677, 1903417408845823766311450961824911214421834648)),
   (67899, (false, 28224, 1, 10374840), (false, 2713906990359939343475674116, 210707030705, 68365386530906962890917138697702617704902)),
   (68051, (true, 1729501556849740540630673, 4479139244, 1396224140016781697809841970431071047), (true, 169698874577985972155979873, 49765501292, 258414285329606634717459260280808785297)),
   (68066, (false, 12863443978967184063987263042, 505635378977, 1329303569988811644568500079936013837336112), (false, 2048688392976, 5617, 921796573123461960)),
   (68402, (true, 4292352, 7, 5555515680), (true, 13667411666532775777838298578, 370142506783, 1163064783342976923788550568550391977525520)),
   (68969, (false, 403896740111784812986369600, 207191245469, 58945778956302064052100847166831797646760), (true, 4767796321900625, 117764, 322519893739060958388825)),
   (69026, (false, 1587706057892337059033858, 6583135961, 3194603760833643251515637884016227104), (true, 7330077762220038511298, 3283369, 627571005791109322479539215702272)),
   (69409, (false, 106002052255617913825397775, 1143638471576, 934652433319950400965855981654312967475895), (true, 2408944561, 23, 118219673122320)),
   (69657, (false, 60543, 1, 8476020), (false, 136952083687508863150874391, 49732112435, 1223195022109541888096090595519118964148)),
   (69785, (false, 461577957851202713796496, 4355117021, 842807023265141966113358583878952108), (true, 7123561, 9, 11570477984)),
   (69953, (false, 3624650488359814144, 59820785, 476538612803702335145838998496), (true, 12963511550286011102821225, 2878961802, 46628256618122143511984927135705224685)),
   (70505, (false, 35695, 1, 11487300), (true, 108211388829321725290794025, 21246767938, 1075873966770327340209384665262781046925)),
   (71131, (false, 2146081946995130083923981707, 219095447143, 123030278234625590855539829921826232539528), (false, 58308496, 29, 102074726340)),
   (71945, (false, 568214971363428981904, 88949213, 808412336150861623608019904844), (true, 16773351331365138551806380625, 468647665222, 728767137662627106038021920016618814447375)),
   (72049, (false, 520480323262326777350400, 25854501257, 34743781248479228138915957542590185520), (true, 457442559025, 378, 309310630216798015)),
   (73322, (true, 1714710122, 1, 71004531607680), (true, 480707524010061764967780649, 57691268130, 9080409520580921025764470963280827341643)),
   (74969, (false, 21904, 5, 277366356), (true, 54853937570922542247542574049, 851240495110, 1782635672869929212265141093526275606343593)),
   (75443, (true, 98536209893674491404173, 990513233, 20417486571670978222022555226113160), (true, 14692278438255698024592817225, 296486200122, 1589135820982012975783075752431632251336515)),
   (76498, (true, 282411378, 31, 4582334818320), (true, 1591203420399140606341434992, 143642361289, 8037965690856822076269653878249221590040)),
   (76619, (false, 15230607885576363248584242064, 792048004813, 5626283532837018149121260625362075466056700), (true, 7499721638918293755543403, 2078391473, 20518442693971398556077275846709241632)),
   (77738, (false, 4474355303, 3738, 72656338274413795), (true, 1558650217491955437647406498, 134697541223, 26189895503788306514480892321795982091400)),
   (77827, (false, 69751937691582502500, 40491133, 892364928827560683541436272350), (true, 778931361272612338604083525, 46180393073, 21240201052778452518437858770173081688380)),
   (78826, (true, 110902, 1, 25979184), (true, 1413641902945999881766357922, 133870849993, 1967900505278778363465905345729857657224)),
   (78851, (true, 3166266429, 161, 136071555808320), (true, 400526824839942576934271429, 36864035089, 7723617784555107004187531414576735093580)),
   (79177, (true, 66015625, 17, 503125155000), (true, 1274783976158938334632771312, 78282281691, 42089225745159646139089243842389291862820)),
   (80497, (false, 180956817112052731200514704, 52620103705, 1750462244174184942342543627266184750156), (true, 41873533664573310510458041, 22398502545, 71609706005564082474056076054530551964)),
   (82385, (true, 342705599264600966511121, 897573157, 196824964214026161810771923187584844), (true, 15164559839694720500835517225, 166723290201, 1846015863888958684492005723156267769894700)),
   (82394, (true, 251064025, 48, 2603383300835), (true, 54372610204916567954346554, 23075554129, 236825536807328154201278389713060889200)),
   (83433, (false, 52983, 1, 14835240), (true, 1207157448964996082436737008, 9171878501, 41940976434884858769243587105519286489860)),
   (84395, (true, 66234938095225, 2698, 539028863567984545275), (true, 2757010070354938075394464005, 107959161241, 135236048330378672032769807123160945690000)),
   (87209, (false, 3484041799797312263036872809156, 7464383567129, 6321983780819493958431504113958499135437000750), (true, 12185105174089, 6386, 40682702025892146435)),
   (87947, (false, 326543167914653696161156, 21360282665, 22929307616648580407624582791080160422), (true, 1858553534143495079498940453, 79081761049, 76535051818832932858179507188996059262640)),
   (89291, (true, 29570716, 15, 117991079486), (true, 59880363709855113867017664981, 769146946129, 6901133874416885949033963935313183832366320)),
   (91457, (false, 2647568726225458719161805376, 269757447745, 314178782807319736701604878352337635329432), (true, 981058639793344140625, 83389783, 23397384658861889286116901990000)),
   (91705, (false, 403580667132798660224028100, 192482454929, 67772708474355401723831703214808368146450), (true, 6731382025, 58, 551695949544675)),
   (92578, (true, 72231163362, 623, 16840286727809280), (true, 199143073674849168542340098, 14467304921, 2796933700435737729395813195346332098080)),
   (92666, (false, 170617783, 58, 3407779322661), (true, 15879258121450589654392926, 12825912935, 17719670523747792648420143208024158424)),
   (92858, (false, 3166686193217410020382362, 86736703783, 1243149058841264561197053221984467029600), (true, 185475787561, 950, 71259908590608309)),
   (93867, (false, 80400, 1, 13736340), (false, 9148199634097728616797442347, 414478541071, 1270140056915519330030639696327139155104440)),
   (95363, (false, 8346992773132159684868543, 14579002392, 53363949854697816796363977423361992545), (true, 60041679034060042998903612, 16940475299, 414102961503820653501721085509747268970)),
   (95809, (false, 198358457673113392294171903, 68372359343, 5655655698647019511875384411859965937704), (false, 256474112938704927, 5156708, 1283693499133332797579235537)),
   (95961, (true, 146247, 1, 42204744), (true, 7602353277832690949272837249, 266146307553, 296869424715076917229799833767915856200340)),
   (96105, (false, 12900, 1, 10816650), (false, 7029346153635669524154624, 21920682209, 121009962659771974255931804692929376176)),
   (97555, (true, 131225645, 11, 1497145007700), (true, 38801008706354012506473144405, 407453017411, 6945348590913580264598130849300443293062000)),
   (97690, (false, 31734068890, 1801, 56163149628104400), (true, 2665464251517430010630681689, 141891889794, 92877670087601619374145617652423020382787)),
   (98033, (false, 2287826111356012700702454016, 153280214825, 12735655790385620993826322754983615550352), (true, 20337603549390518740441, 379029670, 2092364509839367982141089203125739)),
   (98673, (false, 239525125364559920037190911, 309725490515, 146449765596389363087388092008751978461188), (false, 650285798409, 3265, 666721351314145764)),
   (99290, (false, 10734141690, 329, 54975580108800), (true, 2515226338505324696942303290, 159109259591, 4533484593879085870669615197108038925600)),
   (100219, (false, 25956, 1, 15595230), (false, 2107131929694605086261698303, 356999535644, 578281904705210741029349139979604398339329)),
   (101594, (false, 441327795634804441003605799, 433340487710, 400673850926220889717134663588517386029949), (false, 276079484738, 2393, 269070665249360136)),
   (103353, (false, 526525317641668106520158436, 213271714345, 107190911851797877793291176074139679814762), (false, 34983135554807, 83838, 4291707502613995963235)),
   (103595, (true, 4745026202805, 929, 10334294065659702600), (true, 3362901332022374436972283225, 177570179558, 46365950942493602255673995995570348711275))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 100 := rfl

end Rank2Heeg18

#print axioms Rank2Heeg18.on_curve
#print axioms Rank2Heeg18.indep
#print axioms Rank2Heeg18.count

bsd/Rank2Heeg19.lean

lean_all/bsd/Rank2Heeg19.lean

/-
  bsd/Rank2Heeg19.lean — T39 cycle 1868 (goal G854b): rank >= 2 for 74 congruent curves E_n (n from 104074 to 154178,
  root number +1, missing from earlier Rank2/3/4Cert files) from two points each whose 2-descent images are
  independent of each other and of the torsion images (as Rank2Cert). Points were found by PARI/GP ellrank (effort 6) on Kamiak
  (search only); every check below is by rfl in Lean 4 core. All tested integers are below 2^256 (isqrtGo fuel).
-/
import bsd.Rank2Cert

namespace Rank2Heeg19
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (104074, (false, 430707334928, 2723, 420216911118826536), (true, 5950822022292624357277609, 2154368528, 14468714739253185538131995167550747925)),
   (104482, (false, 21250, 1, 14912400), (true, 6970933616918377385750128049, 225211469440, 378126390200043467726438388416504512015593)),
   (104753, (true, 2683446209, 160, 5047198727073), (true, 2071686839209165284164930529, 134002963420, 39512835753489063092322740528986267038417)),
   (105259, (true, 117117, 1, 17573556), (false, 2682439489297892919201327, 23492036612, 95039034611888628190338651220540399257)),
   (106707, (false, 1929789789623303317514355600, 155459444101, 75148903638737966011267176126344653372380), (false, 1321717180281863518021002627, 321573882919, 398277424131317413320452299276445656165800)),
   (107211, (false, 1719739755656983325898032907, 177701053909, 120931938417422547045255593778609667491468), (false, 22671003, 19, 149356567764)),
   (108115, (false, 513714817860654749110196915, 74254100671, 6853948607700142486093497499562831177400), (false, 333967235, 87, 13652580566800)),
   (108178, (true, 1321699698, 79, 41308798351920), (true, 3322678304743681647548039150, 150732840833, 128882412531729684624087170083744553942880)),
   (108697, (false, 34059461373827136, 2151685, 92660976560905449856410888), (true, 264762950645536268385187225, 40458283663, 3190332520070460528494184049079448573960)),
   (109081, (false, 10060493503063484750108889847, 304868708057, 125925019601229208151006164738903319424912), (true, 35866647721129, 12121, 192165762540724958040)),
   (109921, (false, 56129, 1, 22390920), (false, 45201811959265038238386704, 47948321025, 1671647185147983846072436546993685467844)),
   (110289, (true, 300125857, 36, 4571907609871), (true, 39795865527787215170801799073, 591135515476, 1979030451332361155399877192488419010903153)),
   (111137, (false, 93603106970243525871583, 3140510096, 334129555129024196717799332756114865), (true, 7924283900, 113, 694003745754930)),
   (111139, (false, 1764, 1, 4667250), (false, 7182505081151244969276171663, 265994783296, 271269765151722293741046319010811772174071)),
   (113059, (false, 667787300748993398107196303, 89131330536, 15522099242213007630171698327599422076919), (true, 28132754395888197, 267361, 4519764852409307177960844)),
   (113761, (false, 3437394811752672623101306849, 202725635521, 185799144063773998378844935738267403648640), (false, 2149991424, 145, 48594987481824)),
   (113794, (false, 17749567175169407289697244162, 831737838689, 10217773478854896891573208879162557987550272), (false, 1875139068110161427741552896, 210325769921, 202294306254023915460955970107325801138400)),
   (114514, (false, 194710986227959553114859375, 42974004548, 1151649340390955032858147645512649133625), (true, 5664978, 7, 1853696592)),
   (114721, (false, 1383416790322308067764193921, 110424336049, 7708439419023374747303184196723290573120), (false, 42882257648261160000, 22323769, 247603291236321700691070857400)),
   (115283, (true, 4631200269808, 5793, 5478439882944040460), (true, 2593605164713945142423445043, 149582480111, 13782180607528472799477265572022068415440)),
   (115842, (false, 3879712600803688313172754482, 310397778661, 651835845234158778345573552962328689246400), (false, 51600, 7, 1289344680)),
   (117291, (false, 3819443458058427, 5772823, 241568892353087265169962072), (true, 26245722085184430609927491163, 464879559721, 1102484236426449223629746717515809602703648)),
   (117314, (false, 1147810655416873536, 5768401, 3997232957138791625541852720), (true, 2170183968522657214607192162, 55859314871, 99650000662724929654502264348044209120096)),
   (117323, (false, 356160675272734908900, 665931781, 981873299267265983341437946722990), (true, 71407468489052136126631951548, 375003187007, 18565307918663556387483218372670294887265450)),
   (118641, (false, 30708298581047644120994532471, 1370572919237, 38681612761969489780885819651709612661097920), (true, 362318751, 5, 6896387616876)),
   (118785, (false, 753135386340, 5401, 2935200934458848250), (true, 10890500142869501394365737935, 57329692553, 1135775500385224494915088453208045267199000)),
   (118963, (true, 4649194225, 138, 276820809451615), (true, 2977978446354790781684286973, 146689400777, 83042407121170834201375667606911209099000)),
   (120674, (false, 355791505256606979277386242, 63937099561, 6445512003092699194113708102998524775328), (true, 9285589693879703949878018, 1897895113, 28264277102891847214830017981770423872)),
   (122081, (false, 38482379340818587225730721, 20279509729, 200038245404572846202296866878738592000), (false, 1580857600, 449, 976538533963440)),
   (123715, (false, 454949541, 61, 1498704887952), (true, 209402482943127876374732713321, 1040622096486, 73646522284180810797574644694788239953557331)),
   (125251, (false, 11488180676813024207195844, 12332549737, 51504804439703330255284470249714041670), (false, 1757007, 4, 1277603607)),
   (125354, (true, 1815518122, 1, 77357224870464), (false, 78277455019943471990273418, 41386159297, 1768876205806064047907388468450181651536)),
   (128057, (false, 2787642790302674044645908201, 1348512761765, 12294217345055580715994707008301948019997468), (true, 8399539201, 145, 729190047800676)),
   (129161, (false, 2978511683969862320534244, 53570802277, 639695553931920044070712977104036384550), (false, 2860110400, 149, 11004934653720)),
   (129490, (false, 115, 1, 1388625), (true, 1763784963394588738900592890, 113493793283, 24085931546188487696717067521908466085000)),
   (130602, (true, 147923000886593830460179692, 33292263839, 370288121576080367618552606898707066700), (true, 1970842561329529008868471350, 15430780367, 87483032478460703485935193849423596642960)),
   (130786, (false, 6716508423184599048769922, 9584229553, 25814535217858758769835772234127466832), (false, 2071063756042873648645264, 17095842793, 54928864305702193278802041069181123560)),
   (130921, (true, 1258432, 3, 495736136), (true, 1603758780289583894490811609, 86450421586, 50887317057357560273294554005258610018155)),
   (131305, (false, 24383161870068182137721031744, 509417515381, 3716692388760609656806266485378740668163896), (true, 176904633390105625, 784582, 66185708722346920613524875)),
   (131833, (false, 3155710650624, 13085, 39703962783166200624), (true, 300735472908751119359644225, 9197110674, 5211685692302290836827121615494070220985)),
   (133369, (false, 1374045450160001561575833600, 579669433837, 1660397269039389853081320613365744512115840), (true, 4540943640601, 5835, 63989772126500276)),
   (137569, (false, 70918478593288943, 724104, 3507382735195042993399559), (true, 5135346561835316468270185489, 93570193913, 357740366986049419994200077287409583794000)),
   (137715, (false, 166926274763221263384900, 6875609093, 2659029454296042952607096000287077450), (false, 547215, 2, 46799775)),
   (138378, (true, 6138230136642, 1081, 15202464616697615640), (true, 8267529961204028355958510188, 238958535289, 221187591129333154997287806493536294102900)),
   (140090, (true, 767290, 1, 660811200), (false, 3011734124599782657618841600, 197678951357, 250872910325820269445625423970299431273600)),
   (140691, (false, 52296532992, 647, 6194083526614368), (true, 18181926375408224423787796701, 358601599417, 243299229077916085392203821546689593665200)),
   (141274, (false, 250480620930542333955239007, 937438874152, 1964872352192761967487311729041274702830863), (false, 213080223675179, 69711, 9526681337631406065845)),
   (141843, (false, 32400, 1, 24856740), (false, 363685817301898960512826884, 116911793545, 36316912545741900859244199531572839879914)),
   (142121, (true, 2022400, 3, 2227787360), (true, 2862632020065029772317641, 4400551361, 1332572457515200543616655450755732760)),
   (142330, (false, 105350, 1, 31063200), (true, 12976451705751372454465332605, 301329742691, 133392203684937062235441032328939089219625)),
   (142489, (false, 2663198591220803052215865344, 227194161305, 353800418076423427495093287991365503690304), (false, 1217374784, 315, 491471912266264)),
   (143681, (true, 4097889392, 143, 182863549722924), (true, 28004091229069047576646426287, 207927671431, 4569574719550848703265516271934163498405784)),
   (143777, (false, 19923698713090816263627842704, 382346563625, 945058566091933305646350574213355267216844), (true, 6285908357751717082209171025, 180420065242, 332698042241663527639409902213819222001295)),
   (143995, (false, 4146912005, 483, 2146690468508300), (true, 76005140170621565700837605, 15960809009, 580338628355470449399343832172391767600)),
   (144313, (false, 250362008587420541927424, 4006747205, 1152452000383082676414317337906274176), (true, 104142025, 18, 949630265155)),
   (144490, (false, 4747210, 7, 11444565600), (true, 884116386829127621423311690, 26489761201, 26114964851349106388283849000384090385200)),
   (144579, (false, 6642196381685518328109250227, 239203068601, 401885885522904796927151056333925392552632), (true, 10404070443507, 3911, 32791816051033329576)),
   (144770, (false, 19220, 1, 19892700), (true, 169127686532219453594255207350, 709784609791, 62754269440104113623414433325635917013165400)),
   (145362, (false, 14181204303770878994758218, 128130987931, 8986863146476418609908051985841265927320), (false, 8803886, 25, 268299191888)),
   (145610, (true, 13665610, 1, 50514828000), (false, 788945746944859043992992400, 79632974369, 13475943652190419132055182863699686302200)),
   (145858, (false, 3853345626002892641419459600, 286743204673, 704975321790516463091078258772162233247480), (true, 17516185447267881, 278750, 1767591056265535029151029)),
   (146666, (false, 18829226, 17, 164782057440), (true, 2658384784169443206919470025, 134561566332, 6210880516210597435558590207432119773435)),
   (147362, (false, 2060028787251600, 238729, 369537032451834491440680), (true, 334951385104614651462502562, 25502934751, 5873849640905055840964877950100272095840)),
   (148234, (false, 31436280848, 1033, 27486116805417816), (true, 954787497079136139936969057, 18921958244, 29456970490441761954800980533502505618031)),
   (148441, (false, 64247956636257586160991218599, 2367181333681, 210207013191461895162101898271764033515798760), (false, 3316883155372915839, 4753480, 907579037764579486625958591)),
   (148778, (false, 55300811667638507574537939607476, 20488277511085, 215802070875159753755676971365476083410416902268), (true, 19764645886252, 4247, 87054901222648560300)),
   (149145, (false, 305, 1, 2604700), (true, 1740866457889777404178305769, 98826549302, 39775164759552981304166573273354593869997)),
   (149290, (false, 11463154767129610, 1494983, 35702518142086995115276800), (true, 308714660895463991044049290, 34638551249, 4417793637063438063284613244188618810000)),
   (149362, (false, 3438, 1, 8755440), (false, 16623643534658491995465606431, 1406575280640, 38040035591693243825496355896451108483852753)),
   (149618, (true, 4441461624060692336031150, 2210503577, 9232603941888507554397586327453241280), (true, 273202509341646604467494258, 42654787609, 382703568672553676614745713456603944720)),
   (150754, (false, 1656134961408956640838547344, 269809517881, 441498178020122493022286203055824431152040), (false, 79860761912901222761096162, 37149841321, 1716876705211526782524829982284918980432)),
   (151961, (false, 11634401075097276685848623807, 911373049236, 13556377434281655385120802041556336393862497), (true, 354779600864649207927241, 1518447959, 33198615302148425201398785344559960)),
   (153993, (false, 3722628077795493510237783, 8852026838, 22145901109706272730755102487071941555), (true, 955610992, 37, 28812946892660)),
   (154178, (true, 21922390899669952, 101879, 3237216207660732003231120), (true, 576942936987615751444921933922, 581554416457, 436433669284472624175980356449778342534550880))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 74 := rfl

end Rank2Heeg19

#print axioms Rank2Heeg19.on_curve
#print axioms Rank2Heeg19.indep
#print axioms Rank2Heeg19.count

bsd/Rank2Heeg2.lean

lean_all/bsd/Rank2Heeg2.lean

/-
  bsd/Rank2Heeg2.lean — T39 cycle 1866 (goal G854): rank >= 2 for 100 congruent curves E_n (n from 13826 to 22954,
  root number +1, missing from earlier Rank2/3/4Cert files) from two points each whose 2-descent images are
  independent of each other and of the torsion images (as Rank2Cert). Points were found by PARI/GP ellrank on Kamiak
  (search only); every check below is by rfl in Lean 4 core. All tested integers are below 2^256 (isqrtGo fuel).
-/
import bsd.Rank2Cert

namespace Rank2Heeg2
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (13826, (true, 833776, 1, 761227320), (true, 153510242, 103, 561027245856)),
   (14003, (false, 99275, 3, 24461360), (true, 61093683475, 993, 14709888560703160)),
   (14089, (false, 59225122911067129, 2578217, 17655355358664788094528120), (true, 6416089, 7, 16157601720)),
   (14114, (true, 25807518536579977, 1286166, 1766481981063637043094139), (true, 3938278309358048930978, 473499583, 147131060762851093622446586293152)),
   (14273, (false, 4375637371903, 19492, 6700681720827530625), (true, 924402303880225, 254129, 2118523266781074260640)),
   (14394, (true, 14406, 1, 70560), (true, 12005655474184448268, 25668287, 25508066997671006590988762148)),
   (14442, (false, 4350, 1, 908280), (false, 128290280903196774, 3061205, 15436270405828914906355776)),
   (14449, (false, 972842923584, 8905, 597046315428103464), (true, 21114516626511788725921, 377024377, 3053565550230573649864722451140000)),
   (14531, (false, 13093091, 31, 17568023880), (true, 48505917666892462515299, 1703108239, 5287358502365208495820210602047040)),
   (14554, (true, 58482, 1, 13697784), (true, 109038092582, 2633, 13650175298218704)),
   (14603, (false, 53787289541432893099, 63870815, 187817166756786848595989634024), (true, 99188951203225, 25016, 983657331608525757885)),
   (14929, (true, 118116629761, 697, 40517843515924320), (true, 118748007098734883580625, 2013131304, 35210647784554523281235845663363225)),
   (14938, (true, 81862, 1, 23028720), (true, 695227500, 167, 14675426472300)),
   (14946, (false, 13254, 1, 795240), (false, 151414451807, 5024, 134450957607268337)),
   (14978, (false, 5861433600, 697, 330099518385120), (true, 49271396662515226525922, 1777405607, 3048954890193057657856561320340320)),
   (15185, (false, 62408905716885025, 20598773, 1609534942803054830868396300), (false, 1284314492176, 13481, 2768159747934191268)),
   (15258, (true, 530510412, 167, 7296902629380), (true, 706519245013350, 18743, 18779044687132529661840)),
   (15266, (false, 3154176, 17, 5478551520), (true, 562935458, 161, 9499691980512)),
   (15347, (false, 10825977701788527183843727, 26637172328, 3858112170626797168925416055153481975), (false, 1060900, 17, 4435728990)),
   (15371, (false, 13053313899, 1087, 1442758905724920), (true, 782867659, 223, 4731657497280)),
   (15449, (false, 203747239143, 5153, 160714451027958576), (true, 4481689, 17, 823851720)),
   (15499, (false, 48534208607623211, 1926433, 6800494134675772146914760), (true, 856251, 7, 365947560)),
   (15546, (true, 16428, 1, 680652), (true, 473892012882, 5281, 131694391473594696)),
   (15811, (true, 619415713, 72, 15280494666353), (true, 32951140400823775321, 31689108, 165743474374527793786923988445)),
   (15833, (false, 4421613643708422674375, 1021078577, 1057560739730105217942111988358400), (false, 31587119, 60, 266657653529)),
   (16241, (false, 1850976808080400, 412681, 88425279462764158894980), (true, 2007889, 11, 583832340)),
   (16259, (false, 1393742197979006975, 11252664, 1788844741278400306889716735), (false, 637279293061, 6727, 293551307369000820)),
   (16346, (true, 18502, 1, 1179024), (true, 30376784154742124, 1196537, 3375406062831566473945620)),
   (16378, (true, 4737042, 17, 412882920), (true, 379096138118, 863, 233291605223939520)),
   (16409, (false, 79316836, 325, 15419712823962), (true, 7208940527056350625, 19453902, 9829993984012261129079348425)),
   (16426, (true, 33282, 1, 5280744), (false, 1077210458343842570498142, 8750700743, 674004970472964162343788396180028152)),
   (16795, (false, 3382035251147025387605, 453258123, 39750048728222133061876301253100), (true, 8080602005, 311, 711554057789400)),
   (16827, (false, 15123, 1, 907380), (true, 138514113213, 887, 51315460564139100)),
   (16882, (true, 1116018, 7, 791401680), (true, 124983896995150, 35623, 1376593015631399530320)),
   (16907, (false, 407044, 5, 72657354), (true, 28103058906736, 26965, 133970996147568480516)),
   (17009, (false, 6400, 1, 1260720), (false, 3453375069300268265409, 594827593, 289636611810362585607310209265200)),
   (17059, (false, 3022353022408660836, 24813961, 17488469286978737424916885710), (false, 1757007, 88, 175092379287)),
   (17202, (false, 6627, 1, 1292265), (true, 44113090725865725, 1420331, 5720086780089155575325415)),
   (17259, (false, 528, 1, 396396), (false, 2620848747, 817, 574302452265432)),
   (17283, (false, 342251283, 199, 10965081275280), (false, 22743, 2, 9845325)),
   (17418, (true, 53868, 1, 11830860), (true, 26527048562203350, 1127639, 2377817177844611036086560)),
   (17474, (false, 58120953327458, 75217, 609678658944391438416), (true, 3910021558178, 8857, 7240887637260198432)),
   (17498, (false, 443635156250, 15559, 2805884609397870000), (true, 1236066650, 263, 8823702385680)),
   (17506, (false, 24208182129884944, 1181657, 526854937913423046648120), (false, 479198882, 22073, 186709986736580208)),
   (17521, (false, 9583, 1, 1435896), (true, 254799028258073313169, 28558831, 4060808850553888249466562918720)),
   (17795, (true, 2520081117405, 4001, 3974928613706034600), (true, 14130664633817240645, 20968559, 44232529089693612787791919500)),
   (17953, (false, 364742062066664100, 4646729, 79275260376130351511512170), (true, 9009129424003804225, 1588806, 27040750696344064433242387015)),
   (17963, (false, 1614341239392980188475, 305940871, 18879138570245557715970165679680), (false, 336743, 6, 320365045)),
   (18010, (true, 82810, 1, 23259600), (false, 2927448111853690, 405167, 22380441634500294061200)),
   (18034, (true, 19602, 1, 1075536), (true, 156678410638, 2711, 33072077620322832)),
   (18154, (true, 260362, 1, 132528144), (false, 417041402162855184, 12793577, 1899877498156679153204578200)),
   (18290, (true, 37510, 1, 6342600), (false, 105303430124010, 114517, 2211469219444437102600)),
   (18363, (false, 832856062985870400, 19562497, 6368035754798180296200731160), (false, 59434203, 241, 8209572885360)),
   (18403, (true, 22275, 1, 1873080), (false, 267110393125023, 189428, 9870179345596805690385)),
   (18507, (false, 4557, 1, 1210860), (false, 97905976184835003, 37735927, 8246082493698412454155975680)),
   (18561, (false, 4761, 1, 1237860), (true, 22938633425920334607, 17269349, 106616144416765270833771508764)),
   (18715, (false, 4275, 1, 1191300), (true, 52054103820858475, 1542603, 6148703602683110760060700)),
   (18753, (false, 11172, 1, 1592010), (false, 3021628009071, 16130, 6659088956670414033)),
   (18938, (true, 129179315097, 1534, 43578767043152355), (true, 677055231481225, 32656, 17609330829351321962715)),
   (18970, (true, 20230, 1, 999600), (true, 5119647806, 199, 362354480300904)),
   (19147, (true, 3726001649, 200, 222582211702857), (true, 10582031161, 500, 970826196655341)),
   (19195, (false, 605, 1, 471900), (true, 7440740157075625, 12422, 641836148894239715434125)),
   (19402, (true, 2250009, 10, 1709058723), (true, 107651024904627209, 654830, 35214895885734836151931323)),
   (19561, (true, 1101169600, 237, 2435089316680), (true, 314993311313586334294441, 3343476241, 127248184200594671831509256297692200)),
   (19569, (false, 48366933648, 1877, 10805295141245484), (false, 184041, 5, 194460552)),
   (19714, (false, 1731856, 89, 205486806840), (true, 1163144973762, 1231, 1254028722898695744)),
   (19873, (true, 21025, 1, 995280), (false, 21142104346688, 32913, 18651824610089432840)),
   (19939, (false, 44539045704246564, 8857901, 330034546842017158768194030), (false, 18683305407, 968, 3355618079073)),
   (20033, (false, 21775, 2, 11379615), (true, 19263214387682180599489, 29165125, 2673578666846811912004401663573612)),
   (20131, (true, 1200276025, 244, 2249051647635), (true, 27692552961, 620, 4424760514600959)),
   (20539, (false, 8303, 2, 7447791), (true, 686373658949978582733, 30579343, 17975079671939446662789621802188)),
   (20642, (true, 378583854373680482, 2870161, 208119968261033763051179040), (true, 344378848062853598329, 123902070, 2501978246807917848346993142483)),
   (20643, (true, 24843, 1, 2178540), (true, 168464002462761130972, 64056661, 1890066809811193583510283881230)),
   (20761, (false, 72590400, 61, 225169277880), (true, 43495887766962094852921, 78548075, 9071318505880785681635340927746844)),
   (20810, (false, 89580490000, 58409, 21248975370747327000), (true, 288459652810, 3649, 43068921420252000)),
   (20890, (false, 189834490, 161, 6987165790800), (true, 23512255005769690, 1012399, 1489698274788850195929600)),
   (21058, (false, 9898112855347600, 2081857, 9026636862533096041861080), (true, 108071029426475462417001, 719206010, 35346528364988384689107943009225749)),
   (21115, (true, 41434969, 20, 261117104547), (true, 45576513521, 368, 9710804261096631)),
   (21162, (true, 10601647350, 313, 1070515267950240), (true, 7670355612642, 11447, 19806606600797221560)),
   (21274, (true, 40678, 1, 6992832), (true, 1369542160066322, 20249, 50682064847852054348424)),
   (21314, (false, 20736, 1, 709920), (true, 14225041166898242, 566209, 1488044149602314530484544)),
   (21369, (false, 21097203728, 2821, 24509514188907964), (true, 1540081, 5, 1792569896)),
   (21387, (false, 9541069072816410000, 219494449, 3182561850476446414658118452700), (true, 3363305730123, 1777, 6166829626235209800)),
   (21459, (false, 133614727103, 2864, 41883847174250081), (false, 69, 5, 4456296)),
   (21507, (false, 153557889600, 20269, 3461903455282694280), (true, 10357860243, 457, 949879325090520)),
   (21513, (false, 773691495791, 7340, 759078871670634823), (false, 45369, 5, 114148404)),
   (21553, (true, 3617699100625, 12719, 1834957656425961600), (true, 3751018033271152, 339753, 171929372003350003255220)),
   (21619, (true, 888576481, 174, 17913936304745), (true, 812859101190253, 113039, 21795842561674984666032)),
   (21738, (true, 144150, 1, 54103680), (true, 114469714147466017685538, 1629175993, 33449445351676014006903248007276840)),
   (21865, (false, 23386896, 53, 274645642908), (true, 215015106149797494025, 54846987, 3001710214080226026636370289900)),
   (21867, (false, 3815154149877, 16463, 8858643800127201360), (false, 17462853, 53, 246092014980)),
   (22211, (true, 23275, 1, 1061340), (true, 6775343979435997, 17599, 557695062513443122693656)),
   (22251, (false, 156791376102712464, 2866981, 37285543775873264538112740), (true, 2729419707, 167, 138860960548464)),
   (22289, (true, 84573636544, 1935, 3987013196728072), (true, 725949444015601, 107953, 18264621012761794791120)),
   (22323, (false, 15123, 1, 2019240), (true, 1228195612, 141, 40134549409570)),
   (22466, (false, 5016289367, 474, 39739135164533), (false, 22846606, 35, 73340107728)),
   (22523, (true, 184626992525, 593, 79257946489946820), (true, 93661841034612841, 1655090, 21566588506165963681314861)),
   (22682, (true, 141174912978, 2473, 9855081955737480), (true, 193418822042182, 86807, 1259184192733660908000)),
   (22866, (false, 4107, 1, 1441557), (false, 204511125492, 5113, 254021051040471828)),
   (22954, (false, 1206451564795397892321623, 9118467486, 1624353901834252144393344267105993739), (false, 51842, 3, 45532088))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 100 := rfl

end Rank2Heeg2

#print axioms Rank2Heeg2.on_curve
#print axioms Rank2Heeg2.indep
#print axioms Rank2Heeg2.count

bsd/Rank2Heeg20.lean

lean_all/bsd/Rank2Heeg20.lean

/-
  bsd/Rank2Heeg20.lean — T39 cycle 1884 (goal G854b): rank >= 2 for 90 congruent curves E_n (n from 5747 to 154411,
  root number +1, missing from earlier Rank2/3/4Cert files) from two points each whose 2-descent images are
  independent of each other and of the torsion images (as Rank2Cert). Points were found by PARI/GP ellrank (effort 12) on Kamiak
  (search only); every check below is by rfl in Lean 4 core. All tested integers are below 2^256 (isqrtGo fuel).
-/
import bsd.Rank2Cert

namespace Rank2Heeg20
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (5747, (false, 4900, 1, 210210), (false, 394908651785719029256535503, 265561600424, 1811715593894884787512305076206997258665)),
   (8354, (false, 2304, 1, 385440), (false, 171890098532272098587811938, 173661060497, 2414937669757355468488802960289333750624)),
   (15074, (false, 12482, 1, 944208), (false, 75030185231667715753566864, 78230587681, 464943049152644748301842622421845004520)),
   (24313, (false, 2646227727785495356674583104, 651718668445, 513480190550010749602308086687402055004056), (true, 6153618025, 329, 436350003112920)),
   (25987, (true, 2527161987025, 9846, 317122809548010895), (true, 3061327885254702487998557893, 339097797167, 36806911549457833012054476341268187160460)),
   (31665, (true, 33135, 1, 1776600), (true, 3653216431259624773358554140, 339659368621, 1398751849799385701456794449550616781750)),
   (32081, (true, 38067568, 9, 234324931148), (true, 19816126613188520593254081967, 731112796399, 1397955367725413841982039635820811360565576)),
   (33011, (false, 1702827150343596249918714659, 1525684889191, 3170056389088493744835679391279449864004040), (false, 3920400, 37, 89142948180)),
   (33595, (true, 166135460445, 839, 67026815704770900), (true, 129485698182095600934025524025, 426935626118, 46542159036013330500667713528738904006218075)),
   (37762, (false, 120159, 20, 5235755889), (true, 21543246511020103029617826882, 166151079631, 3158331215405426463355997197257691327688640)),
   (40195, (true, 337273225, 58, 5674454154525), (true, 217845620586640778947985238605, 1908062028199, 75320097051798178442133110335409425713436200)),
   (42691, (false, 16920161409381874797451646051, 783030600031, 2597844414547521259755581570474261538474600), (false, 90849492250000, 53021, 747463260947715526500)),
   (44801, (true, 201601, 1, 88255440), (true, 937117321824864771898791014799, 3366063926743, 762560444692560039279753989032821566671170840)),
   (47787, (false, 34012924926695143923538656144, 1185653611265, 10683930437257070058054621257964029201427996), (true, 7759297, 12, 9986897375)),
   (50689, (false, 3600, 1, 3033660), (false, 15618799336348019008035703089, 750145379365, 2982825287131640228526046845647474120857716)),
   (54866, (true, 94192, 1, 23497656), (true, 966364337608147516760759858, 116117767409, 19328404728833330940853260540958026829872)),
   (55434, (true, 176418, 1, 70346232), (true, 9421361637429367877724647286, 69802037377, 914096347883503486630925948028735105591360)),
   (57257, (false, 2660721694418863864103257519, 390766273960, 429594451399735016918592558336963498183129), (true, 294781903773541676912, 24950021, 5024040841406260836011770076940)),
   (62553, (true, 54434247, 1, 401613259320), (true, 500860857005130045622003957788, 2305666205039, 265067667075963337741631718895749605234328330)),
   (62907, (false, 61893, 1, 2798640), (true, 70244560109923271359420080025, 1054938536238, 1523705485597419807609171218441308949084405)),
   (63929, (false, 518132688833902726944385600, 96791470493, 6838201651899045917324572207239138143640), (true, 4162062985706572416674607481, 142812747217, 254995499215056519824293759906614531789960)),
   (64585, (false, 402591006323739750421880235225, 2682353523733, 147250138009408475783003353479610810786020900), (false, 14333617361932582464, 15115061, 13261583598447547577999068584)),
   (65801, (false, 1352475329535965686975504, 43595394445, 145429661735910253207990377902057221956), (true, 8934126978001, 1490, 26700544349538819249)),
   (66059, (false, 4367653087, 452, 843936000702543), (true, 508086823661570266306510492, 72649017327, 8330774626187449413860597468526345527234)),
   (67403, (false, 28213838188900, 51101, 922819111762082297490), (true, 23201282406798279590015158012, 536649791439, 1935656417656721554360602371015608620511910)),
   (68419, (false, 99997, 13, 3656290308), (true, 3408832238049781795438587769, 159567048174, 171073272329358478638002698005932867364475)),
   (68459, (true, 881741998133073, 42484, 25924220193025842279207), (true, 7261905606533345463439834335713, 1856619934388, 19558992352293892019944364286616830095992923183)),
   (69611, (false, 1618786553416084900, 4858141, 357013590966570965082500130), (true, 81542866349823412763349199900, 799067073399, 19521930145711871303591696724547493243738110)),
   (69851, (false, 188890336362684884342106996287, 2003705563236, 90089345938186062616227313920167464095155167), (false, 2304324, 101, 1081645321350)),
   (70897, (true, 130053149420880625, 1035457, 38055715924834846484677200), (true, 51777386966602099280842299775, 481271400041, 11173522805359516437697537908928671309161520)),
   (73482, (false, 65268, 1, 8624700), (true, 11103815883579375951587970732, 96213697093, 1167863046546039447210010132621441797282420)),
   (76834, (false, 784, 1, 2151240), (false, 27563489522100424544922414242, 725119027849, 4903554305478114126964738288960160017568208)),
   (79865, (false, 783451031072763757303796493025, 12290995029961, 10656607029398457211387322335267440905156790300), (false, 471584656, 109, 17880771616428)),
   (80954, (true, 11870756209, 314, 957340879691625), (true, 104295868738015743241345426833, 1089106730156, 13146214253371022406404078955197800116182457)),
   (83362, (false, 87516494050599278827470400, 32442371281, 58435272780866181655722655108916318640), (true, 4025409360106599148482, 161988631, 214397211742236071182117528536960)),
   (83715, (true, 126960, 1, 34010100), (false, 15574814180717867142659920815, 568151336822, 2755942258647387144495013660100207781176625)),
   (84171, (false, 100775638090042455659952478464, 1503052575421, 51191212732901023981455735540505288346657520), (false, 813431023688833973643, 100077649, 6313892781915444128915466284616)),
   (85171, (true, 287013068813, 623, 152739936744291912), (true, 203866513590271386259355144373, 965318242721, 84787216867156915923322500810706609537660428)),
   (85251, (false, 73947, 1, 11535732), (true, 328890777310060653720927880803, 1705839970573, 123839194331783960894543673358593927911847348)),
   (85467, (false, 20667, 1, 11922120), (false, 319138657944908215669275012975, 2484394069178, 237287075934665238234944007385956913477516305)),
   (87042, (false, 15842, 1, 10772560), (false, 1742044429351003622432387600, 653809982017, 1551261607434338055652295812023889620853880)),
   (90449, (true, 27552001, 7, 142737106800), (true, 337009283591349381428991852400, 1925992189359, 18398980752719272451361291299056009038148940)),
   (90658, (false, 290747411336273414088294400, 757830471793, 887771949069744098629149432857305384151040), (true, 108687290832266800115341602, 21856549537, 1039260264710089493788859613380674910880)),
   (100858, (true, 9733909444909938, 108631, 953147116264179645544680), (true, 15803257788843476461834313278, 201664943103, 1918559436542180350008500590266710779424920)),
   (101995, (true, 237183855205217836508089245, 46921885519, 1175876393115472180153573620204341534700), (true, 102193513214754373851854634025, 868415956978, 21508737892579825551530171511555982826385075)),
   (104866, (false, 2905739968967056, 220097, 224615934390231096999000), (true, 175321607938649577234259380738, 372725699591, 73155771112677848552479518687299286929855808)),
   (110883, (true, 498525, 1, 343172880), (true, 58231566637469369545601842563, 484882884719, 12565108639317871003919157026796367810641600)),
   (110953, (false, 206555717579663534346638105796, 11563274902505, 6741821694791462684020680054058181544635523858), (true, 170454205321, 390, 70028136760611619)),
   (111451, (false, 129121363912126119175613978176, 1418266481545, 65852408480456118862222179779724322531163832), (false, 4771568891, 497, 1872853513193760)),
   (112345, (false, 2605153086811034167425632784, 164467767077, 79859454346091337041718378578897649036036), (true, 4413826945181952740986560856225, 5383625201242, 6260364065795523553329869832194496725469854775)),
   (113955, (false, 25205, 1, 17643500), (true, 114097297708231463599645304395, 176448900641, 38521480651643805675045270991645482212619500)),
   (115793, (false, 106357514089702647540600407104, 1397991248825, 65144510593533207808001801199936453252271944), (true, 390062341672897457223798970225, 160362697169, 243606223975375962726604320324804307335978960)),
   (116002, (true, 8380753711362, 2009, 24223999853721119040), (true, 2198529190074005077169100400, 33601953793, 102902587378331880015904757471575852485720)),
   (119081, (false, 836483675228865450715227849, 721975080161, 1795048484153639455054443380102729377139640), (true, 346709568027138002507209, 1694793281, 33395408975498555989921586366738040)),
   (119905, (true, 667097591439576537601, 33567886, 16872868280383383115633715897049), (true, 1970390434576482758761698702025, 3798869355578, 1322884815583321955173089850703307797679419325)),
   (120290, (true, 894010, 1, 837618600), (false, 1545343436020506046120054329646, 3947371777261, 1318537366947368010972030218848295428197243408)),
   (120346, (false, 1750533615466232501215463792248262, 154561448316223, 95418656497332451861035043888111908053163223887312), (false, 190567891, 61, 5594120440305)),
   (122403, (false, 1949508933988200260813814963, 127677221161, 18775942955590256289929369113567085412360), (true, 28067120620801750101869569, 9347404460, 137477062433030478405107386754614970753)),
   (123451, (true, 19240562202562575625, 4809208, 83462478720321498031594349925), (true, 500173367628020141676570120225, 1242373858916, 327062591204840209516308724619516557286333745)),
   (125771, (true, 4410348891962820413, 1200239, 9254280073992016216332968412), (true, 3793134780292665644843816012641, 4667782415686, 5107943531698420833240657523541410905016787625)),
   (127265, (false, 14193908680694892643763445136, 489129264433, 3209227768096548095296344616869303883068012), (true, 2018552590173025, 125253, 13346783704040373404900)),
   (127371, (false, 1815717659272254666495230976, 268591029709, 383821025911490609398740518673821024556960), (false, 4224466194373422507, 9429217, 21595840978248203230828295928)),
   (127762, (true, 22619538, 7, 103376198640), (true, 1242119475246780028237104871150, 3076877435927, 314955883320612626660130142946291778997338480)),
   (129883, (true, 268730449, 30, 3966688814543), (true, 1419165574921034246674283341468, 726122172777, 1688664539136155239490266410721572496974201730)),
   (130938, (false, 829143062, 93, 22211988864560), (true, 2003282782346997377683187021250, 3823567258117, 835805026355936729000932045424824811996578200)),
   (131539, (false, 22993541514857082699878982975, 545861039948, 4813012343579694451275263974295140462866015), (false, 67095018371461, 35999, 1283598656209074989100)),
   (132243, (false, 326385322668759749994358231875, 1911090524137, 203393591682965190618535673408942556729742800), (true, 1058152479793, 2368, 776489949729417385)),
   (132322, (false, 25600, 1, 20771520), (false, 82365504276696219993889970978, 2215938443057, 184970463951369648991309933075776842511764640)),
   (133457, (false, 591004524095138656896737344, 904566802745, 2654677021724686797700362615138695950583304), (true, 30543762254062196649561051025, 319654431446, 4776525267492934041669143861559382418363055)),
   (135506, (true, 350754098, 49, 2454487496208), (true, 9364423575563309728249689712, 133720543039, 875334698645801003712460354908633615281304)),
   (135651, (false, 632810853517408444865146817523, 2171573509639, 74447566691335283861607929902324563922657776), (true, 10256134635525, 8639, 5256383078580002460)),
   (136771, (false, 16857983147, 651, 7200581125721248), (true, 1373089414475350771310258711937, 1904064785672, 1500392958945150961136955830180118777681654849)),
   (137265, (false, 36015, 1, 25137000), (false, 1459504227179311717365181888215, 3619610099282, 1269393952531878211769755438504028373315600525)),
   (137706, (true, 221250, 1, 81455400), (true, 2548025226721591831452323479974, 4109734811635, 1661089681571131417693566085744688499580552368)),
   (138049, (true, 4196673134715328, 173313, 41858451364452792614488), (true, 366486176484225093819726965281, 1160567978737, 191187509004529468615890088531936782238608000)),
   (139667, (false, 201395344320122801477116567475, 1513787899633, 111630144885236834499376666092187933735771920), (false, 819488425536, 4625, 2600776997125511688)),
   (140233, (false, 13225, 1, 16054920), (false, 3187291794826730235773184, 26203256965, 171804135535660998863413573308794190864)),
   (141035, (true, 343040, 1, 183151200), (false, 647412505305475084776214380900, 3865926933901, 1614014490852412168009268467310879753700173550)),
   (141089, (false, 235191672923061757850460645561, 1520091153025, 109486435865584546298743323179169689437121988), (true, 40679815193329, 7885, 253354859933894636892)),
   (141281, (false, 83377079819796992477799663, 24492173591, 138715409973559279439027352637582765224), (true, 245801691769069901842185119089, 1303957463209, 25820961933796996268839309816516880537095680)),
   (143691, (false, 10128, 1, 14424804), (false, 5448291845789819123361787419, 3333268843495, 117841403326165013427104872773312861561484896)),
   (144097, (true, 42220026172784790560206576, 16832013255, 69935012243256114770679472159087387676), (true, 1475915585479360064680906225, 94805731433, 27189326881497752225391914513004203219040)),
   (145018, (false, 1346899706623263190962133098639, 3753628309960, 1783183924702617670228872719569774491257961609), (true, 2315124167974957865061081182, 94624914551, 92223797283919489472655370516933713515400)),
   (145562, (true, 1500243875337113792481433, 2211377478, 1617553310953521006845142360697506205), (true, 19994133705974482371103996225, 8764659492, 2827182344982140374689668391618226618538465)),
   (148241, (false, 115416937856848758322087840000, 900647094649, 11463065265725484032246424275437710494734800), (true, 10268278190895169, 197795, 858652010276196450991572)),
   (148795, (true, 2748304723164005, 61631, 140998406079701885919000), (true, 959053891568391109413768124845, 1108057263359, 922016291505869694567577613642940188542740900)),
   (148899, (false, 43474780034349724472188176, 132894785257, 17336722590951746188380919036814746934100), (false, 39918782120965821363, 16473607, 39613746336083506585317022704)),
   (153122, (false, 684220536221106449855572682498, 3477469312697, 1423256124310940829127616828191666405627657440), (true, 244794497124482, 391, 3830032632624286939200)),
   (153195, (false, 151725, 1, 8246700), (false, 1431694589202679802118720, 2390560887013, 1047537073027949810038098860648749958886600)),
   (154411, (false, 81374597, 171, 40723428821068), (true, 1201843278531035241104228844533, 859675971793, 1311611280722760887757689311291898119368808512))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 90 := rfl

end Rank2Heeg20

#print axioms Rank2Heeg20.on_curve
#print axioms Rank2Heeg20.indep
#print axioms Rank2Heeg20.count

bsd/Rank2Heeg21.lean

lean_all/bsd/Rank2Heeg21.lean

/-
  bsd/Rank2Heeg21.lean — T39 cycle 1888 (goal G854b): rank >= 2 for 1 congruent curves E_n (n from 149834 to 149834,
  root number +1, missing from earlier Rank2/3/4Cert files) from two points each whose 2-descent images are
  independent of each other and of the torsion images (as Rank2Cert). Points were found by PARI/GP ellrank (effort 8, known point as hint) on Kamiak
  (search only); every check below is by rfl in Lean 4 core. All tested integers are below 2^256 (isqrtGo fuel).
-/
import bsd.Rank2Cert

namespace Rank2Heeg21
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (149834, (true, 714540787682, 2111, 215069997869807736), (true, 296782949827141099831889674902, 97200171287, 161678950047527482555095345727916848665150624))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 1 := rfl

end Rank2Heeg21

#print axioms Rank2Heeg21.on_curve
#print axioms Rank2Heeg21.indep
#print axioms Rank2Heeg21.count

bsd/Rank2Heeg22.lean

lean_all/bsd/Rank2Heeg22.lean

/-
  bsd/Rank2Heeg22.lean — T39 cycle 1902 (goal G854c): rank >= 2 for 5 congruent curves E_n (n from 65458 to 123338,
  root number +1, missing from earlier Rank2/3/4Cert files) from two points each whose 2-descent images are
  independent of each other and of the torsion images (as Rank2Cert). Points were found by PARI/GP ell2cover 2-coverings with hyperellratpoints (height 10^6) on Kamiak
  (search only); every check below is by rfl in Lean 4 core. All tested integers are below 2^256 (isqrtGo fuel).
-/
import bsd.Rank2Cert

namespace Rank2Heeg22
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (65458, (true, 1045458, 1, 1066858800), (false, 105364429963847755749354356724023, 40246725850138, 121645844905268447624030835825535152398052717285)),
   (66027, (false, 57600, 1, 7746480), (true, 3149723712165839022078007167025, 6743386211882, 1689253194627112958546819348320509346378304255)),
   (68209, (true, 150311057070705639559201, 1416930617, 24025227608612251923990496687954080), (true, 2056064469703007471637764873355025, 70903133859264, 91924157173561069300584502966954640315494320580615)),
   (82691, (false, 90253627807, 1604, 57878157151355247), (true, 191964400686390570377627755964761, 21068053096514, 2610622146199962511627892357107003820509919735445)),
   (123338, (true, 215554523350, 1031, 79438619508404640), (false, 1034307231480990022710041408820323, 157856491582111, 93077790884749010167096970102035181348852098871655))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 5 := rfl

end Rank2Heeg22

#print axioms Rank2Heeg22.on_curve
#print axioms Rank2Heeg22.indep
#print axioms Rank2Heeg22.count

bsd/Rank2Heeg3.lean

lean_all/bsd/Rank2Heeg3.lean

/-
  bsd/Rank2Heeg3.lean — T39 cycle 1866 (goal G854): rank >= 2 for 100 congruent curves E_n (n from 23042 to 35457,
  root number +1, missing from earlier Rank2/3/4Cert files) from two points each whose 2-descent images are
  independent of each other and of the torsion images (as Rank2Cert). Points were found by PARI/GP ellrank on Kamiak
  (search only); every check below is by rfl in Lean 4 core. All tested integers are below 2^256 (isqrtGo fuel).
-/
import bsd.Rank2Cert

namespace Rank2Heeg3
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (23042, (false, 15800490000, 1649, 7621295260859400), (true, 328140962, 119, 629145288960)),
   (23178, (true, 189877101282, 241, 82736657957483640), (true, 5355485431350, 6311, 12208108004116310160)),
   (23185, (false, 14726335086860544, 1199705, 3633864810068879347234896), (true, 986362023772801, 165213, 23761606485332413181276)),
   (23195, (true, 43880648805, 719, 8842140147821400), (true, 1495412698820045, 48479, 57790020020278055231700)),
   (23233, (false, 39455886320377098943, 64092239, 545846423701247524251011502600), (false, 10130463, 92, 625055419695)),
   (23234, (false, 17462903478818, 31033, 58460370485772190656), (true, 58371354500258, 46393, 230034314321880271392)),
   (23259, (true, 79707, 1, 21523824), (false, 1404250368594575616, 12201001, 3750433546466066333069876400)),
   (23619, (false, 3001805735184, 11581, 1753178416611235020), (true, 741449523, 73, 19896325800912)),
   (23746, (true, 24304, 1, 807240), (true, 25592721282, 463, 4012448740102656)),
   (23779, (false, 19908, 1, 1834854), (false, 94045946173263, 69764, 654106832860617472791)),
   (23795, (true, 6601005, 1, 16959469800), (true, 242449199657517845, 1778801, 113477715019203961236686700)),
   (23849, (true, 9913698823, 439, 874588319409168), (true, 5595102967609, 9703, 12122197531733674440)),
   (23883, (true, 53067, 1, 10916640), (false, 388561264815251712, 4409317, 158466531420424560571050960)),
   (23929, (true, 4924786880596609, 92460, 345307628139615643969727), (true, 770355646702475756227321, 515833393, 676117334911870145621418413597194920)),
   (24385, (false, 7173117294357478464, 28009169, 47498090113887795077014735416), (true, 202385022511681, 61893, 2554140170733047941004)),
   (24386, (false, 2396304, 17, 10259602200), (true, 525982178, 119, 9099004261248)),
   (24634, (false, 75718943061818, 55487, 37782905389193380416), (false, 1574184976, 1733, 2934685801920600)),
   (24778, (false, 99284172650, 15337, 1836217295013466560), (true, 58823159850, 1223, 11078916036420960)),
   (24785, (false, 3442403584, 377, 43868541782736), (true, 109490505536934924969121, 548666797, 36145479201676137443105645095745844)),
   (25601, (false, 38225254998837760000, 207395201, 6804053310346929265633087742400), (true, 174010591576425288406241, 2448526480, 34200803158247817333644169675345711)),
   (25666, (false, 578, 1, 616896), (true, 7191224250355087938, 14333599, 13112063157551682569466110688)),
   (25683, (true, 19150012, 19, 73325152810), (true, 72767646147, 347, 19611687036492180)),
   (25739, (false, 62341181451591834127, 89207512, 1540542802840570996814444953527), (true, 816782428, 159, 14108882271022)),
   (25754, (false, 36769973056888639, 1640080, 11258084107137995444856609), (false, 323545922, 121, 3483034147992)),
   (25979, (false, 242359781627, 23281, 6930931821351672792), (false, 670602816, 173, 10189928004360)),
   (25985, (false, 2213002277860642120336, 291946577, 4167076822934728538552840126388), (true, 759829225, 129, 17222203486700)),
   (26059, (false, 20493, 1, 2304324), (true, 3765238350428, 10991, 4008384766540590702)),
   (26089, (true, 1576804681, 223, 35585965290840), (true, 3497666274487, 9137, 5118295525391464416)),
   (26243, (false, 522265018583, 5188, 343675159550487765), (false, 31788127, 44, 223457182095)),
   (26315, (false, 23104, 1, 1914744), (true, 15496921727842128475, 18596373, 49378613537798794846979389700)),
   (26331, (false, 25152, 1, 1235592), (false, 503571087630669575654976, 4464586873, 104960034093674108966708222834087880)),
   (26386, (true, 30258, 1, 2576112), (false, 20682725003569358, 43381887, 7141579527700418973234516656)),
   (26619, (false, 123783313275, 2191, 11161578964578120), (false, 178752, 11, 1359667512)),
   (26626, (false, 14104404398333762, 1476473, 6686800371779254096981008), (false, 408407908624, 5857, 522116892052257720)),
   (26681, (false, 4039841449422828451970649, 22107648289, 24920673922171478378653916094778985160), (false, 503229359609745664, 6086125, 603385252887704527251133584)),
   (26697, (true, 27753, 1, 1263240), (false, 2801178052579835634868569, 36938440505, 60785784504612468408579332072527072296)),
   (26921, (false, 1287148046616822784, 11954165, 4113053017754470346705696736), (true, 462637081, 119, 5637576868440)),
   (26929, (false, 382181247, 212, 22450031484993), (true, 60657149234639580529, 36596257, 379837674803268940235147256960)),
   (27003, (false, 214755562214427, 155953, 9095258032743975945840), (true, 3697705393563, 5711, 6905841609995863200)),
   (27241, (false, 100800724437801184011264, 15143168885, 1983041740276648375730799619164453984), (true, 588065798825443357485025, 153619116, 450960624953697161341288262037586735)),
   (27249, (false, 59778783009, 2405, 35655561414946836), (true, 20878695424, 865, 649965012296032)),
   (27314, (true, 599886, 1, 464143680), (true, 53370534708950162, 1387249, 2134496313533473760287824)),
   (27393, (true, 67068, 1, 15854130), (false, 5043988284658020324, 17731645, 15678803934854519555486122326)),
   (27419, (false, 785025022816804, 1675997, 2157830239995613531889310), (true, 125268381532, 2007, 20921145452436754)),
   (27466, (false, 74430851138, 5339, 212627893378984776), (false, 10915881975, 2198, 13816743969859515)),
   (28010, (false, 19600, 1, 2801400), (false, 23048640810, 1081, 3528267230888400)),
   (28065, (true, 9394225, 13, 24854023700), (true, 84359250735, 119, 24501589026568200)),
   (28074, (true, 74892, 1, 19000764), (true, 31191193445046, 33073, 30543635621748075120)),
   (28186, (true, 189145009, 38, 2540372948535), (true, 710096273, 52, 18813076957767)),
   (28587, (false, 101986846923, 3739, 123403935352400820), (true, 12128393667, 503, 1072189229602140)),
   (28891, (false, 7415284502404, 16109, 3009895596578097450), (false, 6445030231967, 17556, 15598741028943589777)),
   (28907, (false, 9724372411651, 75875, 518070081038516794368), (true, 37143233441557011453252353, 2143165948, 226368913807712520071086162725395677185)),
   (28993, (false, 13586087456351695359, 29748680, 80228811333462889044439405311), (true, 203683807585230625, 2502031, 41717952773701287027859200)),
   (29058, (true, 213150, 1, 97488720), (true, 5828343817725, 6599, 13735142897500130535)),
   (29122, (false, 205651265959443241411600, 2953917689, 67687264224064727670475885201977480), (false, 890461272078242, 182081, 11135828058738987648240)),
   (29219, (true, 2715781, 1, 4475246700), (true, 2675249283743855109469, 136209649, 135500777297461336459930911915480)),
   (29226, (true, 35574, 1, 3825360), (true, 6938479670412, 7753, 17681134771205444484)),
   (29323, (false, 24367, 2, 17909745), (true, 145989983221569071017691517, 35102000177, 1709069888051775941346241991491965244500)),
   (29809, (false, 206494483289210959921, 278636845, 33124024219095620187025594872108), (true, 29308412809, 935, 2296027451268252)),
   (29883, (false, 60543, 2, 25359705), (true, 46652075488356840684525, 1139086241, 5603352285793045897895706148912380)),
   (29905, (true, 37290575773201, 562, 227718660002488883601), (true, 6846374328257175625, 9415582, 16516287857010873858722944125)),
   (30178, (true, 330193602, 103, 1467984887040), (true, 77813563680175600, 125207, 21705756729723091145802360)),
   (30601, (false, 302666942351, 5340, 450262733080254857), (true, 1124255175481, 791, 1191885000274789080)),
   (30721, (true, 32951791, 23, 164550569280), (true, 609274614543081601, 4410559, 92587789275917674816959840)),
   (30739, (false, 230649667600, 2917, 59233042501037460), (true, 160045694451, 529, 63934871243371920)),
   (31201, (true, 725249, 1, 617061480), (true, 152413347599113441, 164479, 59501490206975453445781440)),
   (31346, (true, 2522258, 1, 4005444528), (true, 10125019184509501888, 10551193, 30243605355458786119781468208)),
   (31561, (false, 1307416676578424860176, 228577765, 36335651137096261683284951737668), (false, 15273441015625, 27277, 69708163118538457500)),
   (31619, (false, 1520964759076, 7289, 879643451810287710), (true, 108757661311183345468, 14219211, 1132239220149852854675628912458)),
   (31641, (false, 25918208520561591, 1290182, 7381470573737563042684035), (false, 8193111, 23, 41778369360)),
   (31778, (false, 335713850867042, 218233, 27039311185792177175280), (false, 5870624400, 12937, 407507494303218840)),
   (31818, (true, 16708285602, 719, 379247504046840), (true, 2783990946828, 3889, 4575249108583416900)),
   (31906, (false, 18963, 1, 3533439), (false, 2187547001703, 8362, 647813219149950819)),
   (31947, (false, 1326352936649691033137883, 8563654483, 2224201752608599376408590605372252420), (false, 14791461, 85, 885890182212)),
   (32066, (false, 102235869980981136, 1998673, 24675953169098105123131560), (true, 55955579554456220738, 14804671, 415252318035081217108521461952)),
   (32155, (false, 132680421005, 31063, 11301465320712174300), (true, 255025, 2, 111208575)),
   (32194, (false, 28322, 1, 2576112), (false, 26905319141470872117136, 6679401809, 235555334699251533415689635846857560)),
   (32457, (false, 1222727292900, 6521, 707903535752415990), (true, 2299162864, 75, 109895785696612)),
   (32513, (true, 7102225, 14, 8356477935), (true, 1746113289228942180625, 125411476, 69764929545892157011419579682425)),
   (32546, (true, 4416173147926826399618, 353846279, 113109343932601017956673676383552), (true, 9308630848251835881577, 380499258, 774544735556629832701784577988331)),
   (32618, (false, 3398147423, 408, 246868422558065), (true, 4374543563148511144172, 348712577, 122039025720939409333576547602380)),
   (32858, (false, 11630647, 46, 233773703835), (true, 7578531072126, 6185, 20574078976885869624)),
   (32883, (true, 993825625, 132, 25599654597725), (true, 386448791617, 2332, 212967769128900065)),
   (32953, (false, 4126832585167561839, 11258680, 1311855678309532552552779591), (true, 574321225, 89, 12259990351560)),
   (33537, (false, 5802804066087, 39818, 127321079935512027285), (false, 929184179364, 11425, 4123604542881189366)),
   (33594, (true, 89100, 1, 24633180), (true, 38953983750, 431, 7588957744130400)),
   (33745, (false, 24336, 1, 3646812), (false, 91990945978876316152089, 1657536557, 3500055078356793118413115846146984)),
   (33779, (true, 66848913, 4, 546546767943), (true, 3659511410513, 9308, 4202992621134863577)),
   (33787, (true, 1221025, 6, 118180855), (true, 11004829780384453, 363359, 1055349692604808691659920)),
   (33899, (true, 29134501, 7, 157001712120), (true, 141192809642042907481424269, 63331831913, 452251970424044087530408656992335611060)),
   (33994, (false, 11371884632927, 42552, 203993817658770330737), (false, 14982338, 219, 6310473641416)),
   (34097, (true, 59324016367, 383, 14397820574038440), (true, 31038279108592, 9831, 171943085026583061380)),
   (34291, (true, 3707307640969, 9022, 4698033790513013475), (true, 271966513045133, 833, 4485110507492740040448)),
   (34306, (false, 6337095091240073334784, 600550417, 845952916640285766487044915676800), (true, 285443073, 16, 4820294875647)),
   (34827, (false, 6627, 1, 2783340), (true, 2108144683479900, 126949, 93300845886366410177730)),
   (34851, (false, 28224, 1, 3434760), (false, 2678748333719225241391347, 15852068953, 13646543783116945186685274426935357472)),
   (34906, (false, 1893300975, 808, 988163080022265), (true, 5354038691678, 767, 12388509657408621528)),
   (34921, (true, 302986487, 31, 5241486270936), (true, 30720244888854889, 912271, 1744804755942669033991560)),
   (35089, (false, 705510618511406917322304, 6452730905, 1074624288706571925065064463973622744), (false, 1620529, 17, 12743239200)),
   (35457, (false, 114669372975, 6521, 509089879747665720), (true, 4580717761, 315, 198538466409116))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 100 := rfl

end Rank2Heeg3

#print axioms Rank2Heeg3.on_curve
#print axioms Rank2Heeg3.indep
#print axioms Rank2Heeg3.count

bsd/Rank2Heeg4.lean

lean_all/bsd/Rank2Heeg4.lean

/-
  bsd/Rank2Heeg4.lean — T39 cycle 1866 (goal G854): rank >= 2 for 100 congruent curves E_n (n from 35619 to 56491,
  root number +1, missing from earlier Rank2/3/4Cert files) from two points each whose 2-descent images are
  independent of each other and of the torsion images (as Rank2Cert). Points were found by PARI/GP ellrank on Kamiak
  (search only); every check below is by rfl in Lean 4 core. All tested integers are below 2^256 (isqrtGo fuel).
-/
import bsd.Rank2Cert

namespace Rank2Heeg4
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (35619, (true, 23874123, 1, 116651588508), (true, 788675704653, 4649, 152155919178573912)),
   (35627, (false, 44251746113780100, 2275613, 37676851420005326546471370), (true, 3285878084547034428, 9039919, 2761314307396206933498872970)),
   (35641, (true, 129669849409, 1743, 25690606570571300), (true, 70219450138547487649, 40545183, 324317891250800040727827161180)),
   (35795, (true, 4543434025, 242, 271704244497075), (true, 3836983202253959805, 9247639, 4531539901218256932783162000)),
   (36706, (false, 333379114221122, 116537, 6767052146957832991008), (true, 5510675452482, 1817, 12933087449767895424)),
   (36874, (false, 40006681803423, 36076, 167653123332840238191), (true, 72929150, 37, 449490174840)),
   (36883, (true, 54208, 1, 9249240), (true, 3596471506397, 7529, 5549595892672477800)),
   (36939, (false, 34347, 1, 2519208), (true, 16882513570461, 8543, 68477298033764258100)),
   (37770, (true, 37845, 1, 463275), (false, 115651317630, 4103, 212627764639369800)),
   (38066, (true, 216832, 1, 99400224), (true, 21666983392658, 13489, 95563174060978683312)),
   (38081, (true, 11643633, 7, 39217787232), (true, 98977060990817, 16328, 979501266024848682129)),
   (38443, (true, 5950263458943637, 349279, 282483042132609316418160), (true, 367175399557116174025, 58784418, 6559118732642229421318069377155)),
   (39026, (true, 57798, 1, 10249512), (true, 9583298413, 303, 870115686709087)),
   (39067, (false, 6946967204100, 13381, 2156706075124101270), (true, 26498986414012, 11367, 133911497254596412330)),
   (39121, (true, 55100, 1, 9108030), (true, 14567844492204727358209, 534780477, 1126086026219286853076159948693380)),
   (39747, (false, 55213586011640250000, 55356589, 806703034971494633427444901500), (false, 310081767668643, 102463, 4917354825759533199000)),
   (39754, (false, 33462, 1, 3926208), (true, 1100781465173533, 86761, 35146337705292543198183)),
   (40081, (false, 3600, 1, 2395140), (false, 6058629994777974916, 37161745, 135425561295512718962744181198)),
   (40187, (false, 374334288159165840100, 109309853, 5818706616880942560352493905170), (true, 8403946273809908525, 6865921, 23735594957948378182520440620)),
   (40314, (true, 5018216562, 313, 219302166045384), (true, 13727975334, 359, 1488806648088480)),
   (40394, (true, 3287853389142, 9017, 277632546243159744), (true, 13262700267724, 11777, 43778996792343416220)),
   (40754, (false, 10496, 1, 4034400), (false, 13894267698, 727, 1940116195548336)),
   (40961, (true, 3267311250807352340561, 254095120, 109672179133674224350393498798809), (true, 278390075881940545577041, 2261464009, 96741037362259133687047238417632080)),
   (41290, (false, 6969600, 13, 904173600), (true, 13957603049290, 8761, 50783428029600010800)),
   (41378, (false, 1525397378, 449, 320308534427280), (true, 1014479903610511202, 4435247, 609869762938365686490898080)),
   (41658, (true, 252333, 1, 125014545), (false, 1203634150256478, 946429, 1293885025129906941472200)),
   (41794, (false, 18084696114339904, 1872329, 19552396481238051392117520), (true, 3731656307848578, 252433, 159676899912647461091328)),
   (42217, (false, 9583, 1, 4024860), (true, 442355634639774992346961, 943725055, 293145242232651323168148654686148084)),
   (42305, (false, 230560644390297157327504, 2770811641, 109843788236033701779268860156992244), (true, 22225991658025, 15598, 92870369608728594675)),
   (42721, (true, 3157729, 1, 5610770880), (false, 16622962559, 2239, 27529077634231320)),
   (43026, (false, 756150, 7, 1711318680), (true, 3677528513476650, 292273, 7529731940546382916440)),
   (43099, (false, 1316, 3, 14071330), (true, 3013605202454496949400668, 548887239, 5231491259061795414929096586951178862)),
   (43147, (true, 5918019617717077, 259697, 396426696746029084621200), (true, 21267356166689728225, 12217554, 93472271480571044851478821015)),
   (44338, (true, 53149715150, 809, 10265799195289680), (true, 51975811040079808, 279553, 11823190506890034599871600)),
   (44393, (true, 8374529984, 395, 430765077936648), (true, 168825631449142813345225, 1183471711, 64491907276041243754696720615801560)),
   (44986, (true, 13673254, 17, 15658972560), (true, 12140744927719265138, 467287, 42302677860607789488424607688)),
   (45065, (false, 30625, 1, 5785500), (true, 188960338557619225, 1972077, 30704821689722674310031700)),
   (45241, (true, 52441, 1, 6073080), (false, 3194644383, 268, 33570916535919)),
   (45545, (true, 69781033921, 666, 17644059833405831), (true, 957645815504856707553025, 817844138, 936671965993069559676255009293460825)),
   (45626, (false, 25377285029887, 23584, 404217193341313023), (false, 2944955781698, 8353, 2074662222329470536)),
   (45627, (false, 105692536057443600, 1896997, 40850049199341481661910420), (true, 853933972323, 4043, 384321375595634400)),
   (45651, (false, 17424, 1, 5569740), (true, 5015926240705958189043, 330185279, 44184112051973572040327434633032)),
   (45778, (true, 142578, 1, 50986320), (false, 7790699409032021886, 15252475, 20266590395322215240775842112)),
   (45923, (false, 830193311247094602465361875, 154217262073, 20448094815796517117437565487685500504600), (false, 32077812499, 37415, 11513942828582385624)),
   (45929, (false, 9181215220920964, 484721, 543349898636896347312030), (true, 375883026649, 1734, 214334705447285995)),
   (46283, (false, 2609563690189017604, 592214585, 26221869053942602551500789402694), (false, 3641980718471791, 290960, 87211301180745157173177)),
   (47474, (true, 59248, 1, 8628312), (true, 2406129133298, 5497, 2996451104152884048)),
   (47483, (false, 2600912221403713545487, 292845944, 159791886464349017181500004336585), (false, 4258837580452900, 1294357, 5184042886684144407993090)),
   (47539, (true, 16903380169, 136, 2194683850390395), (true, 420026527963253633, 1831124, 251853112280766058809605313)),
   (47585, (false, 1435548775, 1399, 3528278687327700), (true, 87703225, 39, 463870738700)),
   (47697, (true, 22515025, 18, 77694293105), (true, 162009533263313775, 379169, 65151058230925922552094720)),
   (47801, (false, 17535323622724186874049, 637229785, 1102095584352310327262780305332324), (false, 1824314944, 365, 260602604575704)),
   (47978, (false, 1662809069362, 20239, 25251158850890265720), (false, 74263, 2, 48224085)),
   (49153, (false, 516025604439423, 505954, 285589474211411919916185), (true, 1723969, 5, 1587621828)),
   (49186, (false, 7654882961027344, 2952673, 37512177997135842830740680), (true, 940131522, 137, 5450577035904)),
   (49515, (true, 10896601, 14, 16355798101), (true, 119737244778457568296560, 1518488939, 12484640817056628754206327042277500)),
   (49538, (false, 127534, 5, 439922736), (true, 307966562, 7, 5404329956640)),
   (49594, (true, 680676417, 64, 16949436123039), (true, 6660913651886823233, 5515444, 16744234020494081030271441183)),
   (50523, (false, 11029509873723, 18943, 47785339008334568520), (true, 1264131, 5, 58082904)),
   (50537, (true, 1570021530025, 4833, 1297047562178423120), (true, 90875827988928225, 857516, 24999851396221594812021585)),
   (50546, (true, 53138, 1, 3778992), (false, 389711515932357343, 8662868, 2355470088726420112963150641)),
   (50626, (false, 1375824179146816, 197497, 52540426544502058322160), (false, 92552368322, 2273, 74424992127017952)),
   (51562, (false, 541614131019529302644, 160800735, 28352130853622638603137713111996), (false, 261677150, 73, 1355906320440)),
   (51682, (false, 45106020996002, 697369, 168803383337195009358480), (true, 250195949961340482, 1846151, 88875203176979737210640640)),
   (51794, (false, 12267, 1, 5573307), (true, 184511118049776, 22223, 2482103538021641198280)),
   (52001, (false, 67699984, 65, 1719763382436), (true, 21037102035358392873409, 324163371, 2946526821280391930488242090708940)),
   (53409, (false, 51984, 1, 2794140), (false, 2340150369537, 6991, 1769130267171535008)),
   (53490, (false, 42361203750, 4499, 222667103623419000), (false, 561690, 7, 1918704600)),
   (53506, (true, 179056, 1, 72305640), (true, 5511724935422, 8809, 8510277663192943056)),
   (53531, (false, 2629889792302769146102109, 7085022383, 894608295168838138229975469533353500), (false, 58258478743796536804, 32999525, 15438529037514031801509506994)),
   (53635, (true, 151905625, 42, 1464750512875), (true, 200407831489659877804765, 810312241, 88320357703902911659139536611634800)),
   (53762, (false, 116961507422585409600, 92820841, 4847092708497265591666522168080), (true, 71010241060812542179202, 672369311, 17779726144522927287542471559876480)),
   (53890, (false, 41616, 1, 6984552), (true, 2495464710226, 5787, 2722527185858959024)),
   (53905, (false, 74149965099327744, 3967769, 230204136665431784085433104), (true, 532784309858641, 98978, 1629221288127719381361)),
   (53945, (false, 154456978906686572944, 113104597, 8359025259534093560988276424404), (false, 583222500, 109, 6418207724250)),
   (53977, (false, 104254134566463, 545084, 163746896987098373357535), (false, 65486006976775, 152794, 10183765398051223836165)),
   (54410, (false, 15210, 1, 6442800), (true, 10622743844969755210, 350641, 34622225249483423659415448000)),
   (54434, (false, 51842, 1, 3778992), (false, 4031049895260411024, 9943169, 7158791283193369439733760920)),
   (54467, (false, 2149717253155986030863391, 13588804810, 14405641136013487117386014833664055273), (true, 8686972, 3, 25562896730)),
   (54507, (false, 33346036804815507600, 2447950609, 1886165044638555384688346101047420), (true, 185385963, 47, 1919293021800)),
   (54529, (true, 68479, 1, 10840080), (true, 91248643050034944721, 16018807, 861336152710850214349171724400)),
   (54634, (true, 24317676, 17, 91202269620), (true, 1073068110341248686722, 23435969, 35137516194472006239078575521224)),
   (54722, (false, 210708301246178, 213457, 36063447143800506125520), (false, 1132995600, 241, 99953571354120)),
   (54795, (false, 54756, 1, 483678), (true, 3217765886400, 7649, 495200199957910200)),
   (54817, (false, 10747400197904, 17265, 40349286440624147356), (true, 11319566329953, 3572, 38011417455881396625)),
   (54994, (false, 7801113697778139689496542, 16038099987, 32958007910989399843829628833297736512), (false, 75732039, 70, 2250530553459)),
   (55042, (false, 66386903233094288, 1131681, 6107909375567274562916200), (true, 3553225, 8, 876225285)),
   (55074, (true, 80802, 1, 16806816), (false, 13548197391522, 93641, 1776840683528436404688)),
   (55154, (false, 54602423614, 5875, 444651741302796384), (false, 26378, 3, 80505656)),
   (55514, (false, 7949863129279720976, 15218505, 28491041321786427974773711768), (true, 16901907883249, 16790, 26248527305182553943)),
   (55522, (false, 460862541250, 3001, 131702089601392800), (false, 278528, 7, 1428261120)),
   (55627, (false, 272431802500, 3581, 344103028916296650), (true, 220008671168, 1453, 87263131912842600)),
   (55842, (false, 67687714178, 1697, 37952183039901200), (false, 65600, 3, 127621520)),
   (55874, (true, 62426, 1, 6956040), (false, 1119979016611, 4481, 69706828350438825)),
   (55898, (true, 198356884038, 487, 88145216980590720), (true, 846211459300153682, 203303, 778424926981421327561322360)),
   (56019, (false, 25773, 1, 7984944), (false, 18541789662595439, 1119260, 9216357518873459471391559)),
   (56033, (true, 93025, 1, 22648080), (true, 48410761082221457315850625, 15709302337, 322798425609272484788079030150850734600)),
   (56289, (true, 58812, 1, 4132674), (true, 276381740103528540674449, 836597629, 143815746563509131419013449562597220)),
   (56451, (false, 3, 1, 97776), (true, 4730414125005237, 205367, 281135911153806405033876)),
   (56491, (true, 3907375081, 172, 220777780860555), (true, 2337436051484897, 175744, 75200326198488044111151))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 100 := rfl

end Rank2Heeg4

#print axioms Rank2Heeg4.on_curve
#print axioms Rank2Heeg4.indep
#print axioms Rank2Heeg4.count

bsd/Rank2Heeg5.lean

lean_all/bsd/Rank2Heeg5.lean

/-
  bsd/Rank2Heeg5.lean — T39 cycle 1866 (goal G854): rank >= 2 for 100 congruent curves E_n (n from 56729 to 66011,
  root number +1, missing from earlier Rank2/3/4Cert files) from two points each whose 2-descent images are
  independent of each other and of the torsion images (as Rank2Cert). Points were found by PARI/GP ellrank on Kamiak
  (search only); every check below is by rfl in Lean 4 core. All tested integers are below 2^256 (isqrtGo fuel).
-/
import bsd.Rank2Cert

namespace Rank2Heeg5
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (56729, (false, 16967, 1, 7051128), (true, 230821394171488738489, 3448447, 3506809988855914330874988904200)),
   (56978, (false, 550776681283384299439, 242941780, 77856527060466985058082848401191), (true, 48787442, 17, 320773825680)),
   (57185, (false, 40097806885282280069776, 984300865, 7655840011696440051742216053266868), (true, 2247135912025, 4041, 3063929699383300900)),
   (57305, (true, 900601, 1, 852938424), (true, 81414509975998141327098025, 20532825652, 701513395326380756271883399455133115925)),
   (57481, (true, 7465937325552, 10763, 9226254387388259004), (true, 147748267781215841441641, 248457631, 56775153046926507794497369677475320)),
   (57561, (false, 63, 1, 456876), (true, 11055601260594580728487792, 12479374547, 21514689353982322573165284053622397964)),
   (57865, (true, 143775, 1, 49905900), (true, 25083940179976102425625, 364583283, 3781399550933421243534340754654500)),
   (57891, (true, 15177309, 1, 59127430320), (true, 366025182627, 383, 221385684707267184)),
   (57907, (false, 5769284924072623599, 14617460, 26290630236628563867599673351), (false, 5259440484, 845, 2974212430666686)),
   (58091, (false, 7248819600, 6997, 242139050813945340), (true, 20889937126619, 18121, 38922044584962295680)),
   (58297, (false, 831744, 5, 1091466096), (true, 255018276920610139225, 64377793, 1303039095104863931877743403240)),
   (58562, (true, 87362, 1, 19161120), (false, 2090722146287, 15168, 19245443308482649735)),
   (58618, (false, 3742051485471973268, 12536901, 16286138412647476389408797300), (false, 21747278, 23, 103082097720)),
   (58665, (true, 50197225, 27, 186210662300), (true, 9019957299965451233340, 238285751, 796102146710594347174479917168850)),
   (58769, (true, 56135044455292609, 208633, 13286166836165009698504320), (true, 1139584933999224137009, 48411320, 38187793323730472537769908818473)),
   (58801, (false, 1858758833239567, 190433, 45056105029084435244112), (true, 89443396647250321, 831439, 23827924352713696735063440)),
   (58834, (true, 31410738, 17, 148020205872), (true, 2518989035656343552, 1953937, 3982042388116699347294811776)),
   (58881, (false, 951125360154153092496, 127146529, 1172094068685480868778005651860), (true, 2989137, 7, 1351142136)),
   (59034, (true, 904570578, 71, 25691519896488), (true, 11997951183458491692, 12970729, 23315507759638123897918374324)),
   (59074, (false, 1151061603689619928418, 294997873, 169986603243451790803125451139184), (false, 8995384336, 769, 3201559757485320)),
   (59299, (false, 101425837411, 1631, 38476444354609920), (false, 1976691600, 233, 112971066528060)),
   (59370, (false, 28363500150, 1111, 11379785788598400), (false, 184958670, 59, 1253966149800)),
   (59394, (false, 912, 1, 1793448), (false, 472872458850, 17953, 13160006636048022960)),
   (59665, (false, 6108495585394030101504, 867177769, 3474084825768848425358141186386656), (true, 13902761732860729, 270657, 1556157643675052046174908)),
   (59714, (false, 24336, 1, 8506680), (false, 6042911257674722, 784561, 2818399845913339768152912)),
   (59849, (false, 24832, 1, 8581008), (true, 589503000467654072364313, 289700153, 452598660628832693974653825217388712)),
   (60009, (false, 1286741067218187081, 38696447, 101920048529091020420716826280), (true, 851281449, 31, 24780549759840)),
   (60115, (false, 5025376010061275, 306249, 181202162022712524686300), (false, 153453959824, 1621, 14675295034409724)),
   (60346, (true, 77077, 1, 13312299), (false, 1127399480350, 7789, 3698423281285092360)),
   (60410, (false, 1918309781150, 14573, 17569369010894740200), (true, 21184940, 17, 55233059700)),
   (60442, (false, 256601858, 249, 59888978423560), (true, 12443832943275646, 447015, 334264373373204134664056)),
   (60514, (false, 106485759, 260, 42198900147711), (true, 92326030539138, 37417, 352567609008959644608)),
   (60602, (false, 896831319870650, 714679, 926578071771836507841840), (true, 3528178048859783244025, 140889516, 197011149092274575939631871765645)),
   (60722, (false, 6694026515336592, 1449617, 10425523852524714841794840), (false, 1119227904, 565, 647406526889856)),
   (60826, (true, 263767257, 38, 4039339876221), (true, 25218406713106760153, 3977542, 126549385383774996205375729917)),
   (60833, (true, 8865838927616, 10975, 14861764528472855664), (true, 8864802596149905581855775, 11863821791, 6836584308252725865093772904784758880)),
   (60874, (true, 25247618, 7, 125973263592), (true, 55651296002038, 6769, 414635778704457238368)),
   (60963, (true, 391003725, 41, 7461360161640), (true, 10285787064735174748, 10501773, 24964859773633959869552366770)),
   (60971, (false, 12032186748608935478235136, 15373616525, 27507274895907700972160194339178258112), (false, 7906572561308571, 361657, 92524620943449950820600)),
   (60994, (false, 47679082456003216, 17287969, 3980499051652376599071609960), (true, 22089343351253366351215737, 2769921806, 103795069819104692985197745195999669459)),
   (61003, (true, 1693489655477, 1921, 2184250451695398240), (true, 1022912327758077, 12047, 32714580201072716191260)),
   (61210, (false, 57600, 1, 4970400), (false, 1131993265257232873690, 228081287, 100134668394870897076663208312400)),
   (61243, (false, 1192464, 5, 1048672716), (true, 87412870351339412169598897, 20992317948, 777337338050498769479029173695213246615)),
   (61386, (true, 62400, 1, 2798640), (false, 211759181283, 1897, 28946015257428711)),
   (61418, (false, 164738, 3, 214159400), (true, 14433287360463454, 343995, 1498122324096119094981608)),
   (61442, (true, 17949129697001502, 376487, 2102695989607930857341760), (true, 1215310736313816160000, 118332457, 29923616145867106855808560797600)),
   (61459, (true, 5972971225, 148, 449743829427555), (true, 2966648470890506225, 6129736, 3207735559267440014210607945)),
   (61537, (false, 1535121, 5, 124856508), (true, 1309142374063555469170432, 2377626831, 1444039374835557397130468872168886480)),
   (61579, (false, 1528686156608687568091, 402245111, 384946657810883431352418956981640), (false, 22743, 2, 36987699)),
   (61755, (false, 22080, 1, 8569800), (true, 2268071865405, 6011, 612255732050944800)),
   (61953, (false, 80784144, 85, 3957058380996), (true, 36987125527923140625, 24370601, 22867482445299633945715758000)),
   (61978, (false, 283024, 5, 810439224), (true, 673673096250, 1033, 550263660867412800)),
   (62123, (false, 150587459136, 1745, 44427287380194888), (true, 5137891965951087670475, 258248321, 217797086426999558220932513575560)),
   (62185, (false, 2853205819818775205625, 304253333, 267056613114553968235585462045500), (true, 3480304424751025, 221169, 99764994746350103354900)),
   (62186, (false, 555458, 3, 51102136), (true, 1162936028394046, 99645, 33606717144955751187656)),
   (62337, (false, 39132657168, 1717, 35520571607227740), (false, 395970201, 85, 4270368541032)),
   (62395, (true, 290405, 1, 152842200), (true, 1540514875429581168355225, 4572560242, 1016910208859086957128291836777131275)),
   (62403, (false, 61347, 1, 2831400), (false, 9799043491743, 15938, 39004113878452470825)),
   (62457, (true, 690060361, 70, 16246972981891), (true, 35838838064824527, 240433, 6750186254472148575108300)),
   (62537, (true, 9832771275625, 3073, 30777206023650431400), (true, 11424083213128023, 401039, 579018149743242565522320)),
   (62651, (false, 871496683004763, 129841, 17615985625372419566736), (true, 593037037, 97, 1578666370716)),
   (62755, (false, 13475, 1, 7114800), (true, 19975591632839920, 526357, 1390108118062508531771700)),
   (62922, (true, 11819687308578, 12049, 25786738163517326280), (true, 11858614587253687471350, 167053273, 1277135023296370470969189819350160)),
   (63305, (true, 42055225, 22, 186814789275), (true, 92414414455, 1103, 15526915083048600)),
   (63354, (true, 63654, 1, 1557360), (true, 3733379812020355117932, 92189641, 225729786109140676632640830783444)),
   (63395, (true, 855625, 2, 755896125), (true, 152866290087405, 23801, 1837128884028201277800)),
   (63401, (false, 41731600, 37, 491640115260), (true, 513447834614347012225729, 1487405735, 353917501572999710055623924799454692)),
   (63491, (false, 109319655340736127487928143, 93123041548, 5642113468754967677575298254695903536361), (true, 66104802872632524289, 16832114, 517182443397404167139349162855)),
   (63529, (true, 255025, 2, 10864065), (true, 12476221025380359648025, 51701256, 1393427436496283919679160959186685)),
   (63619, (true, 164879184999841, 8792, 2116194049319722606065), (true, 427682410926132833, 2153216, 202532879544251634954831087)),
   (63627, (true, 853948, 1, 786933910), (true, 56761274396163922725, 26761463, 254977845387648776653546029240)),
   (63674, (true, 67518, 1, 5835192), (true, 351373593647557, 7149, 6586202109478320268381)),
   (63785, (false, 155950144, 197, 30851789034696), (true, 4723212183846828765025, 207761559, 263750956469180455309340855023900)),
   (63858, (false, 566287350, 607, 559738654328520), (true, 1428427248, 113, 44326448840520)),
   (63889, (true, 4873916383, 199, 290828205299688), (true, 37272669074032, 21831, 131240361646170212716)),
   (63915, (false, 212415, 2, 65571975), (true, 2201586744615360, 64979, 102521798917140228309000)),
   (64001, (true, 66049, 1, 4194240), (true, 30661041152, 143, 5363945394227616)),
   (64059, (true, 79707, 1, 13390776), (false, 1083283977821573848128, 6950423509, 101852999796018251813434292504546136)),
   (64226, (false, 17408, 1, 8156736), (true, 532750082, 23, 12271575537984)),
   (64265, (false, 1759987858019130916225, 275969329, 191594543808737600074756138124100), (true, 4161344636995225, 231027, 151993424344787915853700)),
   (64355, (true, 297025, 2, 80760825), (true, 166219321205, 1109, 59591605268067600)),
   (64498, (true, 190400, 1, 78168720), (true, 363595766363662, 71191, 3035818801924207257840)),
   (64666, (false, 4780535823, 634, 1766528259889449), (false, 468999, 10, 4416891501)),
   (64769, (false, 40653206321050485848639, 1479669480, 27391854200382297046601425177303391), (true, 40906851198400, 1311, 261632968291921207960)),
   (64834, (false, 4796177771, 309, 271048815775045), (false, 776223, 4, 606290769)),
   (64907, (true, 4491675134347932, 98857, 298014995302973396832930), (true, 1496752635770996942306809, 4801371330, 44435317400028352579248495918445627)),
   (64946, (true, 91854, 1, 19686240), (true, 2677277280484087058, 6399919, 495136869053505787696721232)),
   (64995, (false, 26880, 1, 9702000), (true, 238538666205, 149, 116501155752951900)),
   (65019, (false, 7056, 1, 5429340), (true, 48433990989277227, 80519, 10658815278525257133473064)),
   (65042, (false, 28322, 3, 98398720), (true, 887036941149917633650, 50318801, 25959464406547913432289528004080)),
   (65059, (true, 8649481, 4, 25253234715), (true, 12871766967617, 10592, 38037831367259233761)),
   (65161, (false, 361, 1, 1238040), (false, 588597509282192, 96137, 3092189485101566383092)),
   (65330, (false, 23030, 1, 9277800), (false, 3902151406, 335, 387731877581328)),
   (65410, (false, 9610, 1, 6342600), (true, 660934502856790, 100169, 2005937222957824404600)),
   (65570, (true, 127980, 1, 39318300), (false, 64199549457620, 31549, 94077479674087887900)),
   (65667, (false, 11884069682180708765302389, 38013107375, 324536407005796398401236287173801003484), (true, 321657, 2, 105295365)),
   (65753, (false, 5233603587027316625183, 335846592, 380155216717847193468326361122095), (true, 227363333536864333578025, 1630662841, 69303420049757835255324375763714680)),
   (65897, (true, 2301289, 5, 2437570212), (true, 461410283215946348790025, 2493915608, 143965853075667573994300571948610405)),
   (65962, (false, 87322478932, 2233, 93704835393343980), (true, 12244336, 5, 42454918584)),
   (66011, (false, 52732692496, 5017, 381351666584623020), (true, 82333536043, 809, 20110902566993832))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 100 := rfl

end Rank2Heeg5

#print axioms Rank2Heeg5.on_curve
#print axioms Rank2Heeg5.indep
#print axioms Rank2Heeg5.count

bsd/Rank2Heeg6.lean

lean_all/bsd/Rank2Heeg6.lean

/-
  bsd/Rank2Heeg6.lean — T39 cycle 1866 (goal G854): rank >= 2 for 100 congruent curves E_n (n from 66017 to 72890,
  root number +1, missing from earlier Rank2/3/4Cert files) from two points each whose 2-descent images are
  independent of each other and of the torsion images (as Rank2Cert). Points were found by PARI/GP ellrank on Kamiak
  (search only); every check below is by rfl in Lean 4 core. All tested integers are below 2^256 (isqrtGo fuel).
-/
import bsd.Rank2Cert

namespace Rank2Heeg6
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (66017, (false, 218854976201942143, 1866964, 33248778363858321995958465), (false, 18403950421647, 23321, 132256418522634943320)),
   (66057, (true, 2087471137, 172, 33528262578895), (true, 5587383480086257, 69476, 416969616987121701375305)),
   (66146, (false, 833709158930061550656, 123222817, 16170780881544828203923349984880), (true, 6696175164343928017, 3605148, 17184269019941766996255606169)),
   (66243, (true, 937443, 1, 905378760), (false, 1081260032266799, 814740, 1445479068089164049819801)),
   (66259, (true, 664453729, 62, 15819327758295), (true, 3318698549528612481638277, 5402094311, 4913575397554578973940097257261469804)),
   (66291, (true, 183027, 1, 72985536), (false, 16289802095102191334928, 634933379, 2703997916836780102922338163831916)),
   (66306, (false, 258, 1, 1065024), (true, 48792993593922, 26833, 70393146390623968704)),
   (66394, (true, 20333227025, 176, 2884539588740985), (true, 17317871958735225, 251702, 2210740408702407862450605)),
   (66514, (true, 1691311552, 47, 69293167248816), (true, 1297399531122, 479, 1477680552510685776)),
   (66659, (false, 7307833969656900, 1324453, 9976459218414190264460970), (true, 365180970303900, 32123, 6853590556050721246590)),
   (66723, (false, 305589069647, 2256, 81874462773840535), (false, 107257142667, 1273, 4481596909913580)),
   (66746, (false, 1651534985004649447703, 250814658, 156883865438507907403345506555691), (true, 3234373952350, 6951, 444268158016827320)),
   (66859, (true, 2851849, 6, 2583145565), (true, 13069211211838249, 27906, 1494068875377016671457075)),
   (66865, (false, 437719497815, 5182, 1152092140893595725), (true, 616225, 3, 104106700)),
   (66995, (true, 3904375225, 178, 204760396645275), (true, 17431911386064985999005, 151507351, 2292561076319177729810051800758000)),
   (67137, (true, 448063, 1, 296536240), (false, 398388951296, 2465, 55389087017481392)),
   (67202, (false, 18204578, 17, 28869793680), (true, 17387768162, 391, 1849753283780640)),
   (67209, (true, 764841, 1, 666305640), (false, 16762785276806702668089, 1581517961, 21656025924744270809344227595873560)),
   (67234, (false, 19224623350740544, 658321, 3036007539848308764924240), (true, 6180567577766082, 171241, 460511144416190587133184)),
   (67385, (false, 152103900025, 6253, 1025853542572481700), (true, 23314414747642225, 29481, 3559880394836909010743300)),
   (67457, (false, 125770001189365590733456, 4554595025, 494258177265911536417337515485610572), (true, 635023770547693660225, 68577957, 13862346891108051654709111769140)),
   (67529, (false, 25050025, 29, 255098663820), (true, 904732810286767, 112551, 8860005536583490577596)),
   (67587, (false, 210344557602764637, 2681807, 200984164981251502599974160), (true, 1406625025, 34, 52674027729895)),
   (67609, (false, 6969600, 13, 23902768560), (true, 52635329710441, 27193, 119447671844484405240)),
   (67683, (false, 40733, 1, 10909360), (false, 2390378781, 515, 869846320340172)),
   (67705, (true, 29847745225, 538, 3889527202878525), (true, 224492887396855, 44777, 2678989607671129552800)),
   (67731, (false, 60621357525456027, 1074133, 12141523074539764072152648), (false, 250847376, 61, 385758713340)),
   (67746, (false, 141204, 5, 634209156), (true, 5780112390150, 5443, 13031801550291225240)),
   (67762, (false, 2302413272803906, 406075, 524649260941465790538672), (false, 7944098, 11, 5719750560)),
   (67802, (true, 11397232, 3, 38421623240), (true, 2283926129818, 2153, 3418784016656098320)),
   (67809, (false, 16679722500, 629, 2713777862263650), (true, 19440742398448, 1823, 85711696036836470972)),
   (67859, (false, 258696581, 97, 9388725617700), (true, 504022803100, 1413, 344658500411196770)),
   (67921, (false, 7617168, 23, 96910542924), (true, 68338090950159292284673, 741390156, 14963182562627365118954197506144767)),
   (68011, (false, 130390703, 48, 992347015591), (true, 1207572318160258268, 1107233, 1323829347074009302914541938)),
   (68395, (true, 2886483645, 31, 155039025302700), (true, 971166504147618025, 1746518, 934718897402421430933917075)),
   (68441, (false, 29241, 1, 10581480), (true, 5033566368521, 1913, 11279124424739618880)),
   (68474, (true, 77452, 1, 10073316), (true, 33681317126925938, 627719, 3700027269590079809469768)),
   (68529, (true, 84070561, 35, 41603750684), (true, 73921336398911004, 479699, 19635453686922768789225930)),
   (68578, (false, 78826983068806418113600, 2539291217, 122161597528391516635455358437137520), (true, 86103370242, 617, 24076272283828800)),
   (68754, (false, 4469150, 13, 22674236480), (true, 6574851654, 215, 466703091198792)),
   (68939, (true, 13038032293, 391, 876396306532032), (true, 1059915017551117, 85511, 30354521088493157388924)),
   (68979, (false, 15295656288234812555664, 2256496021, 43396816784975689316207775014730780), (false, 8018769574509123, 618487, 2251075051032184064253384)),
   (68995, (true, 2577482068262171511005, 29065249, 130822488674493022796701366461000), (true, 12320538793254050656416025, 7757770178, 40715796901426045744130921882981951925)),
   (69145, (false, 1956469824, 397, 474203121944376), (true, 33062374489228948068241, 626162026, 3441129703240025095643231036503761)),
   (69338, (false, 3107866086012692058349824, 6876072905, 1839350247610859169664923473775659424), (false, 1850, 31, 2866028880)),
   (69418, (false, 21896615714748720400, 19837313, 76429243696903921533537533160), (true, 14032523888298, 3431, 52476642484882917840)),
   (69569, (false, 1530828907673, 6171, 2675248397482787776), (false, 17953254416, 581, 2028379358435100)),
   (69577, (true, 144312169, 40, 1103179846197), (true, 19116105019616321, 212060, 2607368793178623005286369)),
   (69595, (true, 282408025, 22, 4711987812075), (true, 18899566805, 521, 78511446035400)),
   (69610, (false, 32400, 1, 11089800), (true, 2288975642683210, 181319, 2139632318849759292000)),
   (69667, (false, 52900, 1, 10426590), (false, 92437326692, 1699, 54299749527647070)),
   (69699, (false, 11431408323, 1153, 9831154664371536), (true, 1229935477701, 3407, 1027386063267793740)),
   (69729, (false, 69696, 1, 566280), (true, 50506109367153, 2153, 358927702168396144608)),
   (69738, (true, 1786284, 5, 519685452), (true, 79686056174754, 7705, 710373158470482058008)),
   (69769, (true, 1159543, 1, 1246357728), (true, 285585563122729, 31297, 4685975103296655495720)),
   (69802, (true, 1280566225, 106, 36224793033735), (true, 6691044340980795675473, 270982024, 351809044393266169794355396969305)),
   (69826, (false, 7146655642076669585378, 2126295121, 26681138998158793781811840676657104), (false, 4292712508210881845776, 3556381457, 57862155739876505222698991125762680)),
   (69995, (true, 61095063060627225855625, 220056082, 15077862784012572940015213823380125), (true, 52218432484428231818050005, 18655583279, 333765491124716379750296502987750652200)),
   (70233, (false, 17887482674225, 18667, 70640959666934773720), (false, 2440976, 35, 134364394268)),
   (70257, (false, 30385025088, 697, 2710039681645560), (true, 40192010509663617, 491863, 7301670255781282272790800)),
   (70433, (false, 994338566802707845319744, 11641323625, 9466266128755981435767389265284199096), (true, 687285846225, 1913, 528191402568875760)),
   (70530, (true, 6940830, 7, 15857992800), (true, 1274726081205, 1499, 1428047615466551925)),
   (70546, (true, 114025072, 1, 1217588253336), (true, 25042756337646446, 27551, 3962982905652442838916960)),
   (70561, (false, 65600, 1, 6656760), (false, 116330679998105744, 23660385, 13472667490124788746717132404)),
   (70673, (false, 2611553308850591573776, 223517185, 121433313248095662906008136660132), (true, 258985851187575625, 1708518, 79681292926747759305894275)),
   (70738, (false, 1808, 3, 27070280), (true, 158514674540562, 36151, 1621185263924686790160)),
   (70754, (false, 69696, 1, 3218160), (false, 256429671187202, 592681, 397973384176089315760608)),
   (70882, (false, 533750, 3, 255260600), (true, 5224894424134525, 209577, 303304458249377005518055)),
   (70977, (true, 49117558248753, 24119, 186446718088277323320), (true, 48694472772960771713433, 529334264, 9808309020580339981644377881987005)),
   (70986, (true, 2995300812, 97, 159803368205796), (true, 16428681378, 433, 1234537138885752)),
   (71049, (false, 10483475665009572225, 16673429, 54201562909147843620460389360), (false, 507408, 17, 14621833116)),
   (71299, (true, 103933, 1, 24379152), (true, 6694569991681093, 37423, 547691139595175553886188)),
   (71362, (false, 28036431, 20, 28397956503), (true, 8599769084157497774602722, 34620041, 25219134911920600905388313891189657760)),
   (71490, (true, 126960, 1, 37384200), (false, 661265378096821350, 7992943, 3674907234827172059207862600)),
   (71507, (false, 78487302840527, 50808, 1480165253497996587305), (true, 44184694843721503548, 24134819, 98007101786439113534284375350)),
   (71554, (true, 1565942, 1, 1957535496), (false, 7299114314548, 10909, 11848846845316840716)),
   (71571, (false, 69696, 1, 4296600), (true, 1374095076037960817877843, 372681121, 1610696853879040230200979739711810848)),
   (71659, (true, 26209735101, 383, 3887001255483060), (true, 97459474225, 182, 30416360914857345)),
   (71977, (false, 6704800339791786311903, 305634156, 41067581310736388112166413092785), (true, 351468615324035673685225, 2046935983, 107008314417752930750788737014785560)),
   (72106, (false, 106994795031, 3290, 252886017947884203), (true, 15475720862, 181, 1902641356091784)),
   (72186, (true, 80613, 1, 10188243), (false, 1390328230218243126, 17116253, 24882182894730201708819993120)),
   (72194, (false, 16870403416354001346816, 548170369, 1771424976583815550720350837442080), (true, 7017096578, 287, 312065351092608)),
   (72242, (true, 34654812489, 160, 6442078656851013), (true, 11883506442427380521, 9211460, 35094796441824553494025482069)),
   (72330, (true, 99405, 1, 21498975), (false, 65054564670, 1367, 30218442874212600)),
   (72354, (false, 14614305758, 609, 2720754218331136), (true, 423460806, 67, 5591276476920)),
   (72355, (true, 2387788225, 108, 109148300919775), (true, 140479346485005025, 588142, 51810067206033292594769175)),
   (72385, (false, 3030472538203599, 231460, 133200010855536524445399), (false, 5075072775, 439, 925700786616300)),
   (72395, (true, 1881994005, 79, 79256856554400), (true, 68952074625610750647091225, 22664674558, 482147066588535834408588208553108716725)),
   (72417, (true, 20284372572, 529, 125428487215050), (true, 450312419103, 1279, 291539889658543680)),
   (72449, (false, 1541561167305340921380004, 25374350777, 57884911113919708329806761337373730410), (false, 1628176, 25, 57740713668)),
   (72562, (false, 6995079, 10, 5102043219), (true, 2314147219649273600, 259537, 3520347271902086783320553760)),
   (72571, (false, 13600690884, 505, 1463807157844086), (true, 10188504147100, 6969, 30513235418938565630)),
   (72674, (false, 25578, 1, 10879176), (false, 890230789253434884765251, 18563683407, 23614792156775890131342609042038163385)),
   (72713, (false, 89522396474782215843441, 1469417885, 38590320941597043734120839273024248), (false, 121044746128, 2309, 128132195106860460)),
   (72779, (false, 232227407167, 1892, 56903727780187743), (false, 37429924, 37, 564914554050)),
   (72794, (true, 3783801537, 188, 170665012386849), (true, 20285596459365169, 527774, 86830413406868288560905)),
   (72818, (true, 55105854192, 41, 12935866849336200), (true, 110674767636143750258, 34833241, 701190902600338390385744634000)),
   (72835, (false, 33635, 1, 11848200), (false, 2778397879959097646563600, 11785141693, 16213464615672172199919047665725054900)),
   (72857, (false, 19301190329344, 16285, 3916409591248766304), (true, 110426430257325625, 860863, 32009634151610188685165400)),
   (72890, (false, 1234800331926490000, 4116437, 31465165676730746908563000), (true, 1656490, 3, 1957670000))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 100 := rfl

end Rank2Heeg6

#print axioms Rank2Heeg6.on_curve
#print axioms Rank2Heeg6.indep
#print axioms Rank2Heeg6.count

bsd/Rank2Heeg7.lean

lean_all/bsd/Rank2Heeg7.lean

/-
  bsd/Rank2Heeg7.lean — T39 cycle 1866 (goal G854): rank >= 2 for 100 congruent curves E_n (n from 72898 to 79547,
  root number +1, missing from earlier Rank2/3/4Cert files) from two points each whose 2-descent images are
  independent of each other and of the torsion images (as Rank2Cert). Points were found by PARI/GP ellrank on Kamiak
  (search only); every check below is by rfl in Lean 4 core. All tested integers are below 2^256 (isqrtGo fuel).
-/
import bsd.Rank2Cert

namespace Rank2Heeg7
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (72898, (false, 291450132743, 4122, 649897480262117395), (false, 1492974, 5, 1277048976)),
   (72969, (true, 609375, 1, 472270500), (true, 8173072217327511, 269759, 561702487482910768152600)),
   (73122, (true, 99372, 1, 21212100), (false, 15174796681885414574, 34402595, 331902687295482798158234146576)),
   (73138, (false, 2783366033552, 6223, 874764413909806680), (true, 3553225, 2, 6675088485)),
   (73146, (true, 920748, 1, 880717068), (true, 18943532658, 457, 1541805979435272)),
   (73393, (false, 1068677319751812431, 48105180, 175571098706587213559990580247), (true, 6219674400195525025, 2908199, 15433974904532162361169226880)),
   (73409, (false, 3846438924615303583, 8860076, 8415806342746983760629054831), (true, 432533219389489, 75623, 2165434601597410888800)),
   (73489, (false, 103225978944, 5465, 704395102094188296), (true, 530746001437242331225, 52288817, 11317211630754784997201651860020)),
   (73546, (true, 16968204, 7, 68301666180), (true, 5994526706605557142, 8727569, 5223463249954812950887870224)),
   (73690, (false, 44890, 1, 12381600), (false, 794044280256107342400, 109275413, 10685282660279268129912723574800)),
   (73705, (false, 12100, 1, 7997550), (false, 19102785624114432186482064, 17406205757, 50547033797103319198562197045980595884)),
   (73723, (false, 3197393124671475523600, 2083087033, 18088150186505671843111906061732820), (true, 3553225, 6, 4453367165)),
   (73811, (false, 465124, 5, 1217842626), (true, 36812855659900, 21219, 96076619165195280710)),
   (73834, (false, 9918, 1, 7286424), (true, 3476521744901, 745, 6481675001888134851)),
   (73970, (false, 67600, 1, 7807800), (false, 44594717732370, 39031, 691084496429386768800)),
   (74059, (false, 110230640100, 1409, 32303083327903710), (false, 4124742975, 236, 1265822650785)),
   (74067, (true, 151725, 1, 51579360), (true, 33054395280028563, 252733, 5947703898974374949875260)),
   (74081, (false, 70756, 1, 5837370), (false, 96771382824487, 285098, 59226021142768329701397)),
   (74185, (false, 4511376, 13, 24845398668), (true, 238440076665025, 56631, 243862990026081363100)),
   (74315, (true, 2652260609, 136, 116817321130623), (true, 129985867671309841, 1157036, 30161302727071173869522439)),
   (74329, (false, 7630358224799, 26400, 141538884050912978449), (true, 4036824480520647007581481, 4555855367, 7495053534137529649259538797221083960)),
   (74410, (false, 26872819210, 601, 77396705251200), (false, 18515, 3, 91089775)),
   (74810, (true, 171610, 1, 63980400), (false, 11644890476881599438400, 424225609, 729169985543000016020619905149200)),
   (74939, (false, 8590832507478547, 883373, 5361374283238257635474088), (true, 209860017079905418913, 51257668, 1052167278252387147078635505393)),
   (74971, (true, 102280354969, 990, 22754186437415797), (true, 1690738097269, 1433, 2189306598927728280)),
   (74985, (false, 196328435799578749935, 253816382, 67631081496727094933369301089775), (true, 30006900625, 191, 5176300896149500)),
   (74994, (true, 126150, 1, 36028440), (true, 3800742411435282462414, 195244337, 154407738214258260137871345583200)),
   (75003, (true, 1256572, 1, 1406067110), (false, 796027998614691022812490323, 104731902529, 5861414710625961606093155277439741513260)),
   (75009, (false, 29648025, 533, 116028654853320), (true, 2433701812205697, 119233, 107921879373085142327424)),
   (75010, (false, 66560, 1, 8923200), (true, 433831131594202225, 2371256, 66908880111232398923836425)),
   (75130, (false, 660275, 3, 118431775), (true, 929031140717680, 51489, 27658437266989354225400)),
   (75273, (false, 63554251277939099040000, 1238123617, 24279767741538337056950995652264400), (false, 694236571017, 3073, 127215450265931280)),
   (75418, (false, 9754133722443344124002, 974749969, 7011235201258212728485789323725880), (false, 19969047, 86, 2490990648405)),
   (75523, (false, 76211328608100, 205417, 27812340506933932054770), (true, 5752176892, 261, 195133237168870)),
   (75569, (true, 26144681838966824809, 17300105, 67059053347670221700823147252), (true, 322832746095586350625, 58781763, 3411036422517410924197755079700)),
   (75578, (true, 1105150, 1, 1159081320), (true, 1929425615909832222, 4627849, 1458585902132318834905725240)),
   (75579, (false, 13725, 1, 8707140), (true, 561503369832237, 30433, 13201624807730426917836)),
   (75593, (true, 410368939729225, 34289, 8115765056033377634760), (true, 2133673838780932436220392503, 29727393727, 98509720458881619725227982961512327976960)),
   (75603, (true, 673728, 1, 549509400), (false, 56797044037120068, 3117341, 174569704686349718863703850)),
   (75698, (true, 48352408728750, 23857, 152618850417877443600), (true, 102660567971891400798208, 535796473, 32147759249594235427079663980110720)),
   (75826, (false, 1061410726336877594775, 731227322, 1320431351241646457073013868121195), (true, 3759282, 7, 1109500944)),
   (75866, (false, 138679807, 44, 569736310017), (true, 3948299335655690526, 3813665, 7532814881485970685998742024)),
   (75899, (false, 7470207200095370564515775, 10073251176, 5119948950368058006802149470640215455), (false, 1633284, 5, 1234309014)),
   (76026, (true, 853393900614, 3217, 305309339657969760), (true, 626509040644970802, 2399209, 354864953107370858147022456)),
   (76042, (true, 157377025, 42, 1032545818655), (true, 197908267850573416417, 7953168, 2783348632030566315303549749935)),
   (76043, (false, 8525, 1, 6976860), (false, 11039305048475, 13289, 25406484214355123880)),
   (76066, (false, 220609263485884816, 1710857, 14118112010050663509509160), (true, 327731202, 7, 5932648232736)),
   (76115, (true, 91315774225, 232, 27566472372013575), (true, 211808935993265, 51878, 783652098380620326975)),
   (76241, (true, 295736809, 57, 2778340097260), (true, 2478646053790159, 79951, 120993110784319672446240)),
   (76299, (false, 30276, 1, 12186090), (false, 43342618360178143023, 60547706, 1819258320796970857265806294671)),
   (76322, (true, 23171383801600, 3977, 111387846167720300640), (true, 9221876190709642728600318, 1679786809, 27996934207828440006210675244014747360)),
   (76362, (false, 9438, 1, 7361640), (true, 12626538416349, 5455, 44134417564486256007)),
   (76465, (false, 293931460, 107, 14137795072050), (true, 262374393665, 1193, 122288413016431200)),
   (76499, (false, 658941309504, 3761, 696741428892755400), (true, 5154528661667, 4519, 11152212296150417184)),
   (76555, (true, 911645, 1, 867365100), (true, 58532149871614225, 783098, 8457365167777074342198225)),
   (76601, (true, 85849, 1, 11356680), (true, 2480394536473, 113, 3906439613830497408)),
   (76618, (false, 352631632509180455722, 106089223, 14777411280403285692795541020480), (false, 3748121039610000, 5449841, 139317184606150779310360200)),
   (76642, (false, 16740208590400, 25009, 183779930499948677040), (true, 654123117682695042, 1031401, 524915358557600188445392320)),
   (76738, (false, 24786, 5, 302007312), (true, 181528623218392114, 1533645, 8250080489162620629079888)),
   (76746, (true, 3334117974, 191, 104537035128240), (true, 253476526960962, 54311, 1815548385439176386616)),
   (76787, (false, 547453123931855044606564, 5139580445, 1445081567759544723567418542014112066), (true, 9024979689629946556749724, 1938106995, 27098636742386276148857483336763349118)),
   (76937, (false, 150293977377752383, 1445047, 22006769417893351964175660), (true, 3650640742225, 6831, 1265260902801550460)),
   (76954, (false, 402243136, 97, 12074704972560), (true, 194702935848282, 37489, 2259110730201971525376)),
   (76962, (false, 1530150, 7, 4263609960), (true, 306174012923328, 42791, 4756169140462796070960)),
   (76970, (true, 4077045, 1, 8230777275), (false, 6133418417230, 9557, 8508825705244027800)),
   (77011, (false, 39922269747200870656, 67353565, 2192942917372074839941899257328), (false, 29049281939896691, 942241, 10549078502572513127966280)),
   (77073, (false, 1230759592318753247, 724809956, 44919734290727635144344134765105), (false, 28298641284, 1105, 15098347462761486)),
   (77089, (true, 330581197085267392, 1108809, 182092466858847139156805144), (true, 34512244572523016209, 14329063, 180169600015052823871520575920)),
   (77251, (false, 7874327827, 743, 3719256838382688), (true, 97984313731987, 26327, 812298057577190125656)),
   (77401, (false, 3465638317613270016, 9948965, 12719779273702180940730831648), (false, 24649, 5, 303774276)),
   (77443, (false, 1357906958188470374400, 289055857, 233131282780353955279424702091840), (false, 2349682574400, 52531, 327560745172104411240)),
   (77449, (false, 1780440506566968960000, 271648693, 229153652849630087615908065213600), (true, 4434067793307451489, 346500, 9336896362892649921908849263)),
   (77466, (true, 1175628, 1, 1271920572), (true, 156872510204039160128934, 176951857, 62125278197813940290352836093210880)),
   (77602, (true, 80802, 1, 6399840), (true, 3028275218116162, 79959, 164393468548027173098560)),
   (77691, (false, 1199383931748, 5923, 2680379045436868146), (false, 9076623, 14, 36835605729)),
   (77914, (true, 25666022054, 31, 4111838007537120), (true, 17083425700972, 14759, 8056579832396934804)),
   (77945, (false, 2975, 1, 4248300), (false, 2986219167389759, 208356, 86960899892899035158239)),
   (77961, (false, 1911, 1, 3407040), (false, 13427336579454502076775, 4785731021, 206898160898224648770740997397874820)),
   (78379, (false, 96948967699647, 57332, 2350214159118927531423), (true, 1425562239106213852, 3580527, 1207349909073073886080553614)),
   (78410, (false, 12418873600, 481, 1473656707034400), (true, 294658003540810, 61121, 548197680306741192000)),
   (78442, (false, 168486245160929505059266250, 80677285319, 6256000057503177094465116426324843367200), (false, 16904251, 25, 189209281443)),
   (78521, (false, 119289561713181820688, 77621931, 5000254612416106079797135593764), (true, 1865837233, 96, 74286939195287)),
   (78683, (false, 40713821920475, 203009, 20689414335387071295120), (false, 1611693743, 186, 88068484329265)),
   (78706, (true, 7503750, 1, 20553871800), (false, 72329144099927, 31266, 223118249562833457547)),
   (78715, (false, 78400, 1, 1969800), (false, 16084073915, 657, 3795709181937400)),
   (78819, (false, 7332, 1, 6719778), (false, 354108354431376, 203039, 60780396326400442089540)),
   (78931, (true, 367108353, 4, 7033780796673), (true, 7326935213153, 9308, 7120048944233742447)),
   (79009, (true, 183250369, 47, 756095598000), (true, 19317277168, 177, 2662709993070652)),
   (79049, (true, 871503355833061825025, 104700076, 2740938829814990338669911857535), (true, 224505671488595495551081, 583126337, 105610161748102151624252552206102680)),
   (79051, (false, 5108286407, 1016, 5820760896182547), (false, 439417503, 772, 987556050690321)),
   (79058, (true, 9567944722350, 6913, 27190554300912008160), (true, 124832637550889968, 472873, 43660948408887916687701960)),
   (79163, (false, 946635487, 176, 69597939789105), (true, 204049033227547, 50513, 413091535555037608800)),
   (79211, (false, 274220974682328949451, 59270847, 783313779956545052339356093400), (true, 4250851, 3, 8640101800)),
   (79315, (true, 87725, 1, 11101200), (true, 3910245015198115275525, 202534201, 135633698337935330922852688731900)),
   (79322, (false, 694531627374467471888, 126954939, 28287627828899838666847008443560), (true, 2915778229225, 4628, 4046365149815038965)),
   (79355, (false, 18680492721005, 19447, 101518185426801114300), (true, 143989971286611025, 314138, 54557585503278783415402575)),
   (79410, (false, 13235, 21, 4028800175), (true, 14732524075651440, 242039, 1696707662635935298463400)),
   (79441, (false, 365758950075953, 76977, 5666947682453353280656), (true, 6310872481, 247, 321097191833280)),
   (79489, (false, 6441408732000274704, 15135985, 43230936703729381627431674844), (true, 962446735423427809, 144459, 944200901077128314600116940)),
   (79547, (true, 1538609497477, 4203, 777302532473299820), (true, 8177531968333, 3117, 23280113614914760240))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 100 := rfl

end Rank2Heeg7

#print axioms Rank2Heeg7.on_curve
#print axioms Rank2Heeg7.indep
#print axioms Rank2Heeg7.count

bsd/Rank2Heeg8.lean

lean_all/bsd/Rank2Heeg8.lean

/-
  bsd/Rank2Heeg8.lean — T39 cycle 1866 (goal G854): rank >= 2 for 100 congruent curves E_n (n from 79619 to 87794,
  root number +1, missing from earlier Rank2/3/4Cert files) from two points each whose 2-descent images are
  independent of each other and of the torsion images (as Rank2Cert). Points were found by PARI/GP ellrank on Kamiak
  (search only); every check below is by rfl in Lean 4 core. All tested integers are below 2^256 (isqrtGo fuel).
-/
import bsd.Rank2Cert

namespace Rank2Heeg8
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (79619, (false, 1171801179575484964, 5747713, 2549140751615395452230141250), (true, 2035734277595281948, 4768731, 1327716728938032685230555298)),
   (79715, (true, 136960, 1, 41216400), (false, 18032790184178098815, 18703658, 90329054490776556611040968025)),
   (79745, (false, 22500, 1, 11475750), (false, 19040502500, 761, 5805703891119750)),
   (79778, (true, 108578, 1, 24269280), (true, 16771916649516506914, 7112385, 66668864186891536124755312288)),
   (79841, (false, 7637654395048155040000, 385479601, 793403639110212218744263040266800), (true, 15939517255743601, 247831, 1914788524364696219999760)),
   (79954, (true, 211740494, 31, 2871083495280), (true, 1066619126366032062258, 93758833, 26202191318428673524878510791568)),
   (79970, (false, 68981908820, 2323, 111885169993710300), (false, 42906875, 27, 258524349875)),
   (80121, (false, 1483399545872, 6509, 3718657082532689668), (false, 377417, 9, 3980214352)),
   (80201, (false, 4044869878345703, 509406, 1298370710891595827354869), (false, 246050596, 61, 2648952936630)),
   (80266, (true, 73856716886, 623, 18198721753027920), (true, 400424405872315829187618, 744347713, 251817394702351102966601905222794888)),
   (80274, (false, 17408, 1, 10339264), (true, 1836299438316020412850, 143107777, 35056683923712625109821436287120)),
   (80321, (false, 277141918203759859375, 428459752, 245427687434213992008617557697625), (false, 2734479, 7, 4680838800)),
   (80322, (false, 38088898914, 1865, 54015359186251584), (true, 364338018, 41, 6459169669920)),
   (80427, (false, 526817659275, 2897, 306291615422855760), (false, 30015744, 35, 514114950096)),
   (80539, (false, 201992718096, 4469, 717204839708278740), (true, 582134886187, 2681, 46794286583229744)),
   (80689, (false, 11294669180386622066373567, 11835044972, 1365056910155461637418658447989358623), (true, 31939505504368, 6327, 179580842137774249172)),
   (80738, (true, 195938, 1, 79026240), (false, 1787665848199, 131350, 1862434274494667786451)),
   (80786, (false, 122349345593224958, 1233813, 4351115750177224332744544), (false, 186083251399, 30350, 32100055334447357901)),
   (80787, (false, 15987, 1, 10012680), (false, 4408612604941571684847, 249045938, 158124519023283620864541897502545)),
   (81011, (false, 7738745252, 319, 249925113657282), (true, 13485990204, 155, 1549721779506858)),
   (81035, (false, 1669264, 5, 1483116516), (true, 2193493961875, 3921, 2673796547092946500)),
   (81073, (true, 17790993, 4, 74841559815), (true, 361406071140695295953, 9608092, 6869102883804912686701902912615)),
   (81121, (true, 22323597463485564481, 96719, 105474208225281230117107403520), (true, 88869951943632490608, 29564561, 505063600295818905373886788908)),
   (81169, (false, 122868476676, 1261, 13853642742097950), (true, 278865700848, 527, 146780708736929988)),
   (81265, (false, 15779270170269225, 1215409, 14948835957716553923642100), (false, 1505129616, 137, 9582800664948)),
   (81282, (true, 180918, 1, 68748840), (false, 3218591526, 2545, 29867217876325368)),
   (81322, (true, 7415795593, 138, 624530187506555), (true, 30823069225, 402, 4894928740965835)),
   (81346, (false, 102330818, 41, 917534995056), (true, 777337410978, 2009, 621222161506647168)),
   (81361, (false, 24367, 2, 50658993), (true, 287180515346981872, 1796313, 62380746245988684461212804)),
   (81434, (true, 244328524, 17, 3801343710780), (true, 1898344339408765638, 2881169, 2444095096823508302914800528)),
   (81674, (true, 17393931457, 72, 2293337272948001), (true, 429033185075997690049, 8529774, 8885761801923238429655384364545)),
   (81713, (true, 83025, 1, 4236120), (true, 1569419305872562225, 1895551, 1931401384529194685771708640)),
   (81739, (false, 3802793323872229407, 8246092, 7904696958622293514159151313), (false, 2061917939844, 21277, 53053071768921565110)),
   (81851, (true, 116699, 1, 28415640), (true, 1270661368329683253, 622087, 1431890328057923674164736512)),
   (81978, (false, 61347, 1, 13468455), (false, 286494521945224923252, 174820271, 42128979587175499963068510806940)),
   (82099, (false, 11555232707975, 12346, 16328193324656606085), (false, 1214404, 5, 1823427606)),
   (82121, (true, 42023770009, 102, 8612966138873525), (true, 195455038385965090001855625, 34009777211, 2388184452818813846363397692110391433900)),
   (82131, (true, 118579013207452, 37091, 391715180425354223746), (true, 1560914867447229, 4729, 61669229898106389169800)),
   (82274, (false, 14408123965987975, 846178, 6856388399864319877173165), (false, 5542862, 27, 140603018192)),
   (82346, (true, 7064334178, 201, 523790262655352), (true, 168125794342, 381, 68762407440179024)),
   (82739, (false, 112344068807149244004, 51582581, 2006713001003862427481395435230), (true, 193533372, 37, 2183068252554)),
   (83074, (false, 490884225267521018018, 77099537, 1191249452581175512319335222704), (true, 116873554338, 553, 38999940511005312)),
   (83091, (false, 3988227, 7, 1635664248), (true, 1496365314360323695432563, 4185487441, 424276481318416475977496545638038208)),
   (83266, (false, 34, 1, 485520), (false, 24604880912, 623, 3286756025143032)),
   (83297, (true, 110498464830625, 26207, 993745413527021164800), (true, 32327380680580096, 24105, 5812397260091626148578144)),
   (83834, (false, 18780334993129247, 3465672, 137965728586052685574435537), (true, 9544454816350, 8151, 23945141963063325880)),
   (84009, (false, 41, 1, 537920), (true, 7363220821721673822716881, 5563304221, 18693110450755134110200090127165954920)),
   (84082, (false, 741468293481966352, 7869319, 4437874348225173137645235720), (true, 152893225, 42, 458893725515)),
   (84106, (true, 10605533321196, 10903, 11520604646910157260), (true, 70570074989282, 23857, 435589450070515775496)),
   (84170, (false, 4275, 1, 5496225), (true, 1603665793821680, 51563, 63591710959388187239400)),
   (84323, (false, 1941489783997669375, 5649902, 2597775634634662148141161575), (true, 10182400, 9, 24097795120)),
   (84346, (false, 91887552263658480690006938, 36301730129, 599518811127986201225828094509281735824), (true, 152497801, 38, 1133245687035)),
   (84434, (false, 65268, 1, 13684524), (false, 367107796685837045941221182, 65941672179, 99750778142725807336802497680694784528)),
   (84507, (false, 1275, 1, 3017160), (false, 111929574598618176, 2987725, 249581384459063997182256168)),
   (84585, (true, 10634460, 11, 9418127850), (true, 21359369806833030625, 14442441, 55640960244400773418473018500)),
   (84593, (false, 53122698715024, 1836425, 2079317827428672409695276), (true, 117863010450882972025, 36791727, 303140809944414888455147740820)),
   (84667, (false, 2699416511602241434619, 224882145, 172684211064107936545849465372096), (false, 484352, 9, 4760953120)),
   (84841, (false, 745788736391543213735844, 4669930429, 1462292276215813917092714796541285590), (true, 2848267157761, 198, 4806963087506230105)),
   (84931, (false, 26289233141183014527, 85380164, 3171591872947012758261280679487), (false, 660275, 3, 312932620)),
   (85065, (true, 171735, 1, 61824600), (false, 1533603889963802091681, 151299745, 46992514073914704275860049237628)),
   (85106, (true, 602654094, 1, 14794563712320), (true, 2748745109491922, 171503, 59532448544320100477136)),
   (85129, (false, 29888089, 53, 1297057668060), (true, 201007479744, 1475, 35021282846889528)),
   (85226, (true, 17183574, 7, 69095650800), (true, 6317104802, 71, 500922360367464)),
   (85321, (true, 177555625, 4, 2365858773525), (true, 6372487116487521, 144101, 488646109754766334899600)),
   (85385, (false, 24591513105438284567104, 617129461, 3336655138533894316775012585855256), (true, 6981376645490055638822161, 8488532431, 8718237473387213669212425838667582916)),
   (85561, (false, 884874710919564109584, 128696395, 32927004570760616870807112000036), (true, 23962113, 4, 117105624447)),
   (85610, (false, 25320701315, 2427, 80140810739728375), (true, 4219614, 5, 7470084888)),
   (85899, (true, 112651, 1, 24461360), (false, 28076085851, 999, 13572193189931200)),
   (85930, (true, 187850, 1, 72399600), (false, 8667904100713135850, 10587801, 12372321333777910320775445200)),
   (85962, (true, 576738, 1, 433101240), (true, 192288108, 47, 419795971020)),
   (85994, (true, 98838, 1, 15317952), (true, 773113794338, 2633, 432799229405136312)),
   (86073, (false, 1880207303, 694, 1795732369464475), (true, 3729609170955591702025, 33680418, 227691129576778907456990460128995)),
   (86195, (true, 227205405, 41, 2637966315600), (true, 67300595327672940025, 9089998, 549013210479067919146104857925)),
   (86281, (false, 2143125648296752287744, 1707892325, 11650490600014784943962360035907904), (true, 27531105625, 371, 4121247932894100)),
   (86307, (false, 4256227772966637, 225179, 66405629114488964876940), (true, 366729467886705625, 1187178, 209512202099318275908997675)),
   (86315, (true, 25018697929, 90, 3955737021601283), (true, 384592469027725, 59511, 4576778529582459207700)),
   (86339, (true, 4114307197, 97, 258708056582976), (true, 3947740236828, 5491, 5896729204620986178)),
   (86370, (true, 360150, 1, 209827800), (true, 1980992008830, 791, 2787163416874987200)),
   (86433, (false, 372485543278075727, 4689684, 1137678632858910074542045975), (true, 7550336132892, 2257, 20711413763217581670)),
   (86435, (false, 1652706946880563475, 79175037, 696565711260569429706132341900), (false, 4511376, 17, 52184464668)),
   (86578, (false, 18254878552181312, 483939, 1192406984002550111391440), (false, 703298, 3, 281319200)),
   (86610, (false, 1134864090, 481, 673957470601800), (false, 716133750, 889, 1831660001487000)),
   (86619, (false, 85683, 1, 3717324), (true, 168926829970376221801707, 1234318763, 43342808695300428249113394671600964)),
   (86658, (false, 64350, 1, 14723280), (true, 14152198918, 261, 1530128057967080)),
   (86666, (true, 1333657992201993, 10502, 48702981990037836991173), (true, 22402817080811177, 180442, 3326452040478603302385141)),
   (86674, (false, 2624, 1, 4437840), (true, 99166808830278553, 979242, 17034979817158316176266467)),
   (86705, (false, 1153825024, 137, 38982011622576), (true, 11509405306971588025, 3799202, 38814728356754319250913763675)),
   (86801, (true, 567567, 1, 422558136), (true, 10619435301343725025, 2099597, 34583585680493438331112092060)),
   (86842, (false, 6178372767, 268, 67255459839855), (true, 402463097096861267506188, 1429876567, 229133082747744930364809391250917860)),
   (86899, (false, 59276202402816, 66653, 2937066669376239103200), (true, 172662902318083662346653073, 7858004004, 2267716375906647782235585609495293438777)),
   (87242, (false, 481815700414662894122, 88959071, 10854139441822266754594746000720), (true, 291501166191123655375350361, 54928566300, 2138414033992194306025403736267290018141)),
   (87243, (false, 329296154951054782413, 121778453, 22705030364984402179603231030320), (true, 104142025, 34, 264986795645)),
   (87265, (false, 4435382195775, 19147, 66725623702538928300), (true, 1785013912830625, 68133, 73448095388778858623500)),
   (87289, (false, 212547241, 137, 23683314330600), (true, 180397334075321385961, 42896393, 1103015518233810239708655972120)),
   (87371, (true, 43436933441, 620, 5741027062993761), (true, 272420471677225, 25616, 4395646900161779448405)),
   (87401, (false, 5263568607805775, 889068, 4997609668389414817398295), (false, 2091860628469319, 167113, 57522212713163389311840)),
   (87419, (false, 52288, 1, 16019736), (true, 223865892381827275, 948583, 99166959834424781497416240)),
   (87521, (false, 2667186785738586447, 7104967, 5752267783078161334495655688), (false, 345651103, 148, 35057272757871)),
   (87731, (false, 127808054723, 2281, 156658617548043864), (true, 4144994021, 103, 260046806809020)),
   (87794, (true, 84474002, 31, 38508290736), (true, 820054037085808, 88729, 12637405603282612173672))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 100 := rfl

end Rank2Heeg8

#print axioms Rank2Heeg8.on_curve
#print axioms Rank2Heeg8.indep
#print axioms Rank2Heeg8.count

bsd/Rank2Heeg9.lean

lean_all/bsd/Rank2Heeg9.lean

/-
  bsd/Rank2Heeg9.lean — T39 cycle 1866 (goal G854): rank >= 2 for 100 congruent curves E_n (n from 87801 to 95186,
  root number +1, missing from earlier Rank2/3/4Cert files) from two points each whose 2-descent images are
  independent of each other and of the torsion images (as Rank2Cert). Points were found by PARI/GP ellrank on Kamiak
  (search only); every check below is by rfl in Lean 4 core. All tested integers are below 2^256 (isqrtGo fuel).
-/
import bsd.Rank2Cert

namespace Rank2Heeg9
open CongruentPoints5 DescentCert Rank2Cert

def rows : List (Nat × (Bool × Nat × Nat × Nat) × (Bool × Nat × Nat × Nat)) := [
   (87801, (false, 214456934642173686225, 69911957, 5443527667249640630954820233160), (false, 12543, 4, 157326849)),
   (87819, (true, 7686554929, 228, 542172495944105), (true, 18751125217, 148, 2554134100363439)),
   (87953, (false, 27529289, 165, 12562825859416), (true, 234180229325728046225, 51385976, 460171974686429854482778172985)),
   (88010, (false, 78400, 1, 11197200), (false, 2008142231290, 6251, 3956198460854452800)),
   (88026, (true, 342924, 1, 194086620), (true, 83736024801976006165542, 383254921, 23940240065383687555428507807280656)),
   (88035, (false, 10754005248900, 13141, 35237828909394346950), (false, 108995535735, 1178, 18215499676391325)),
   (88081, (false, 6058862585539622383, 9271921, 11179623964099663808520917496), (true, 295198857622711249, 763361, 157945085969548820499528000)),
   (88090, (false, 41515, 1, 15830325), (false, 1476409209158598490, 4340441, 920874009966768457095579600)),
   (88195, (true, 76825225, 22, 559862490525), (true, 96654882171496109016645, 1010324719, 10936926120579538438534800164383500)),
   (88226, (false, 14560324555838, 13833, 32603548657536561376), (false, 66532975, 76, 4121058784185)),
   (88466, (true, 163584, 1, 55652640), (true, 19982446653190898, 466351, 762887587502174656204752)),
   (88537, (true, 242295687374588, 24557, 3678829998416032423950), (true, 259445918655625, 29859, 3980865553006892340700)),
   (88571, (false, 14839063304701636, 2207221, 52532670085929885178997670), (true, 16369473148, 393, 1150247415205882)),
   (88619, (false, 10309080172127, 10788, 976230028726261903), (false, 3311542116, 317, 475710522919470)),
   (88705, (false, 41616, 1, 15980748), (false, 16557593649938359225, 15191761, 48991167958793097849970976700)),
   (88753, (true, 7960621149313, 7164, 18420228734443113025), (true, 3019133295315697, 163601, 102386217115297059997080)),
   (88754, (true, 2209134791726478, 36761, 103679357394968130811728), (true, 212780473916153138, 347207, 98027494259410720117088592)),
   (88755, (false, 2340336289836789, 194665, 116855972883031820613084), (true, 628849, 2, 411605857)),
   (88834, (false, 71019520261700928064, 28441289, 92263728070813795893488617680), (false, 14575609732373762, 472481, 1623468893790176867986272)),
   (88842, (true, 519792, 1, 369237960), (false, 410945516475, 2299, 145640695481923005)),
   (88889, (true, 4802999645720329, 13817, 332863458457942658852760), (true, 1318773649032473968, 3827043, 241550591563343050678199348)),
   (89018, (false, 137168772863, 1428, 44033843177791615), (false, 60580539362, 921, 11093839818611480)),
   (89090, (true, 327904447500, 1271, 168715386142495500), (true, 2748981737790, 1967, 4521853424380389600)),
   (89105, (false, 5238400114798775, 271562, 287135404172351205497475), (true, 192058680025, 878, 78601501386434325)),
   (89121, (false, 183, 1, 1205604), (true, 96317545109946762001, 32102590, 284675290360307366446703861999)),
   (89155, (false, 1409692426895996100, 8128933, 6791595300842144190176728950), (true, 13140625, 2, 47617220625)),
   (89185, (false, 602800388587275264, 2603185, 33780903075587416853450016), (false, 115210705640625, 40597, 979748243838472342500)),
   (89201, (true, 418139147642608, 11751, 8546587278308889122852), (true, 119568060321603395887, 12885311, 1297374717918803257553954170344)),
   (89219, (true, 3450156853, 23, 202636474286532), (true, 3273565770733453, 80441, 184361711443894969392408)),
   (89265, (false, 5951, 6, 247900807), (true, 75584838907441526751, 23251003, 505767298193662088762647194924)),
   (89267, (true, 7031198991540025, 45292, 589381835747899108203795), (true, 12798254857635953, 377572, 153677384987078065117815)),
   (89281, (true, 919560477967, 3209, 17114909958215784), (true, 982348607838481, 104755, 2244512450595188336004)),
   (89282, (false, 405163787738468533039890818, 318241147993, 181825785459773607597897242862874113049200), (false, 1590776787600, 7769, 6493820229797420520)),
   (89290, (false, 1516000941348010, 140287, 34600975106429483466000), (true, 789289692490, 2969, 52340722973775600)),
   (89369, (false, 6137, 1, 6984552), (true, 16793959204897081, 28543, 2176334061940934780099160)),
   (89386, (true, 629442, 1, 494321784), (true, 4397385592886, 6007, 6267782001014408160)),
   (89489, (false, 5090731799862830121, 10891681, 21018796682896290540147986100), (true, 115033646396256662251489, 448864493, 38533288292896400139977709284419380)),
   (89522, (true, 173317225, 16, 2261685795915), (true, 172661972540034850, 1182803, 49386928737721502365845840)),
   (89674, (false, 781456, 5, 1857497928), (true, 4482673759418242986, 5355481, 7773273752649323828859668160)),
   (89746, (true, 376832, 1, 224668416), (true, 2641013571518926, 38287, 135555284778495333660720)),
   (90074, (false, 5932696576, 257, 34208853207360), (true, 119805472858, 1057, 22500880573626048)),
   (90193, (false, 2812099828368407, 1085518, 5633913535552721252570925), (true, 182942173412478366192, 15095663, 2458739304295572387775065445500)),
   (90211, (false, 5928364731971, 19969, 86389379893935245160), (false, 1120974606400, 12671, 15288840023950573080)),
   (90259, (false, 274576, 5, 1173606468), (true, 186405820785048887446984729, 43881111438, 920062605519794502682306835399921092555)),
   (90338, (false, 23767778390720400, 1453537, 29195991789932262450790440), (true, 91293705561680162, 147343, 27577888726669790908454880)),
   (90514, (true, 35735058, 7, 211968224112), (true, 3135082293651392, 177913, 71271880920312394483056)),
   (90649, (false, 115336373607, 1306, 34970269754497077), (true, 318586801, 25, 5595816357924)),
   (90842, (false, 14648706981036550205995264, 12751163525, 7235774448837367703407646005024970016), (false, 83133682650, 961, 3250289663578560)),
   (91011, (false, 21928333101, 745, 6738552608420232), (true, 806965460643, 1009, 720112819367145168)),
   (91034, (false, 42590856803618, 118689, 8364560096880507554936), (false, 153323350007, 1338, 21632598858810437)),
   (91059, (true, 119055261, 23, 1187962785360), (true, 252569646126652, 42277, 3069447716799882552554)),
   (91083, (false, 489171, 5, 1555420608), (true, 22526030737, 144, 3368953933439815)),
   (91138, (false, 224803903398010000, 2130937, 164746190738381960065597800), (true, 3064187474393922, 182063, 28395250753597652159040)),
   (91169, (false, 19636703143558712289, 16175245, 60008319077427517346638185084), (true, 330582197031754801081, 57919975, 2281257247760969841428354048604)),
   (91417, (true, 17696332161, 260, 2205893726550591), (true, 51182331783765625, 17887, 11579239338229390641177000)),
   (91490, (false, 2749346915, 249, 260160506781175), (false, 138826814, 39, 112760701984)),
   (91721, (true, 4459315131607, 1201, 9412630982873754384), (true, 1261746219355001485168, 109651749, 21775834996082693244725867196812)),
   (91722, (true, 179667432150, 1153, 55931803940512080), (true, 3515981330435954988, 2448073, 6511727297743959217933928340)),
   (91802, (true, 2030973672890777320225, 122319484, 67422349111941653798606052878385), (true, 17419727210624891556156250, 12577411241, 40152247850665987296588154966301526000)),
   (91851, (false, 50987819211, 769, 4227977890382400), (false, 11689934400, 377, 628310103374520)),
   (91882, (false, 5865041147541234687, 42231772, 396612367353233264580175714305), (true, 5220114663806, 5885, 9454420455565024296)),
   (91931, (false, 644, 1, 2332890), (true, 46339666494809426748, 22005113, 87641175946885467968831994678)),
   (91954, (true, 18539412816814, 10129, 68717567196457835760), (true, 41170195954112, 18583, 168135491593793538864)),
   (92059, (false, 48400, 1, 17227980), (false, 14618394266979899, 554383, 2928890860973286681299520)),
   (92298, (true, 704383350, 1, 18694489872960), (true, 445887812628440310877218, 187479647, 297732955023316798183904535673682760)),
   (92355, (false, 124835763525, 2381, 179655048306795600), (true, 29093954805, 251, 4862289283221600)),
   (92410, (false, 48532090, 23, 40840976400), (true, 840037780844755350010, 36017209, 24097967558430350848402371044400)),
   (92418, (true, 6880177, 8, 9218876905), (true, 10378664732113, 5872, 31820833910825760025)),
   (92449, (true, 129982801, 7, 1481033679840), (true, 665164528, 57, 15306425688932)),
   (92514, (false, 578, 1, 2224144), (true, 9061191250, 173, 821281211153200)),
   (92537, (true, 93025, 1, 2902380), (true, 669376042418212816057, 30329617, 17177694767148309326162311827480)),
   (92635, (true, 26008769565, 457, 2803358015612700), (true, 54488334092841250570565, 326022401, 12509682113844199272102581423119800)),
   (92713, (false, 2466395303213087, 3786276, 66007877176049688549755695), (false, 7189403164463, 8818, 1427781598575821775)),
   (92785, (true, 203665, 1, 81820200), (false, 113380150307135481, 34023545, 36166390573359909598434470772)),
   (92851, (false, 30899, 1, 15391200), (true, 8785095310331708, 146147, 802161280826817329695242)),
   (92905, (false, 3180982289050982475774505, 8837052977, 11629988470552283346705053163870465000), (true, 20912329, 15, 2758872608)),
   (93003, (false, 30257911666527, 18746, 67950970592708479095), (true, 26887480732358213568, 624013, 139419902386982144540659467240)),
   (93082, (true, 5483046118, 1, 406006370579040), (true, 1309192700087754909900, 33853009, 47212704557166960955253908140420)),
   (93091, (false, 763016147653020543, 2863208, 12775194229010367259647681), (false, 237389389946077, 143459, 29291000457572118877668)),
   (93130, (false, 3350, 1, 5386800), (false, 1284097064704531763897070, 4663255093, 1774623064412710218230184277497793800)),
   (93297, (false, 1233462829424753, 133649, 39355741507926621778280), (true, 65924797783785046681, 24453325, 285191146364972183166552866704)),
   (93313, (false, 36327419459103751419152, 626246439, 843382513387588187668454174286820), (true, 54184321, 15, 367691548744)),
   (93379, (false, 642454632047959548579047, 9271369986, 6413021252597432678340072875106594997), (true, 426409, 2, 134318835)),
   (93545, (true, 20295025, 13, 57332251800), (true, 196809087115998328686079369, 42026221185, 1500258435820068517287171767051671698472)),
   (93961, (true, 88465796377, 599, 24326975122317504), (true, 1050340069321, 3281, 290072455140950040)),
   (93985, (false, 7127730844259978304, 704203825, 124431674587904003161060893516744), (true, 165523834522902025, 7839, 67342743871698638931336100)),
   (94065, (false, 781286065935, 3713, 914881381275668400), (true, 3121169545414315173025, 182154877, 1037926396797315181530592364900)),
   (94083, (false, 6890248029027, 19759, 94706542928404440000), (false, 742682688, 133, 40587471916200)),
   (94106, (true, 2921151942, 49, 157408489627824), (true, 630938064996291345248524, 2522321473, 158119609480563905310642982045475100)),
   (94154, (true, 14063825656822, 10927, 31690730417215764096), (true, 42032878256322, 17801, 191956386960968866536)),
   (94297, (false, 25777225849606623, 1949978, 57418278750871626344477175), (true, 1946622832, 141, 23127350651020)),
   (94313, (false, 46269653199031139877808900, 22996356497, 126657935507517760079025790447576964610), (true, 8566655803225, 8946, 11858745200795915435)),
   (94385, (false, 294335, 2, 128274375), (true, 18866480779997937675625, 352977609, 2026411591995835006360111690719500)),
   (94642, (false, 105225646249659231, 1153900, 22490899073747901829961103), (false, 1511981151446318, 176961, 99117536816728630189360)),
   (94681, (false, 4892582809, 881, 5128839453029160), (true, 70191725371549345369, 6712399, 586982710022325340780469376600)),
   (94731, (false, 1092, 1, 3130218), (false, 14205680635419, 28375, 282441448938220312104)),
   (94754, (false, 10658, 1, 9720096), (true, 7748692568155233458077369, 7494983508, 15675221580208379358173547139184913325)),
   (94889, (false, 1157972841, 113, 12134794485240), (true, 95822988851422471081, 19467023, 869449789287047759793204359640)),
   (95035, (false, 502208100, 73, 1464392318850), (true, 5115380744491490376640, 215597479, 184483768478144461511724654898200)),
   (95186, (false, 61276815782, 837, 6511593489186968), (false, 20193614, 15, 32059993616))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all (fun (n, p, q) => onC n p.1 p.2.1 p.2.2.1 p.2.2.2 && onC n q.1 q.2.1 q.2.2.1 q.2.2.2) = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow = true := rfl
theorem count : rows.length = 100 := rfl

end Rank2Heeg9

#print axioms Rank2Heeg9.on_curve
#print axioms Rank2Heeg9.indep
#print axioms Rank2Heeg9.count

bsd/Rank3Cert.lean

lean_all/bsd/Rank3Cert.lean

/-
  bsd/Rank3Cert.lean — T39 cycle 410 (goal G127): rank ≥ 3 for the congruent curve n = 1254 (analytic rank 3,
  a2_cong7), from three points whose 2-descent images, with the torsion images, span a group of order 32.

  Lean 4 core plus bsd/CongruentPoints5 (onC), bsd/Rank2Cert (img, nontriv), rfl. Points (±a/b², c/b³) from the C
  search (box 3000): x = −98, 4332, 9196. Checked:
    on_curve   the three points lie on y² = x³ − 1254²x
    indep      each of the 7 nonzero products of δ(P₁), δ(P₂), δ(P₃) avoids every torsion image — so the images
               with the torsion images span order 2⁵ in (Q*/Q*²)², E/2E has order ≥ 32 = 2^{r+2} and r ≥ 3
               (DescentHomFull, TorsionShift, TorsionImage; tors = E[2] cited), as in Rank2Cert.
-/
import bsd.Rank2Cert

namespace Rank3Cert
open CongruentPoints5 DescentCert Rank2Cert

def n : Nat := 1254
def P₁ : Bool × Nat × Nat × Nat := (false, 98, 1, 12376)
def P₂ : Bool × Nat × Nat × Nat := (true, 4332, 1, 272916)
def P₃ : Bool × Nat × Nat × Nat := (true, 9196, 1, 873620)

theorem on_curve : [P₁, P₂, P₃].all (fun p => onC n p.1 p.2.1 p.2.2.1 p.2.2.2) = true := rfl

def mul2 (a b : Int × Int) : Int × Int := (a.1 * b.1, a.2 * b.2)

theorem indep :
    let a := img n P₁; let b := img n P₂; let c := img n P₃
    [a, b, c, mul2 a b, mul2 a c, mul2 b c, mul2 (mul2 a b) c].all (nontriv n) = true := rfl

end Rank3Cert

#print axioms Rank3Cert.on_curve
#print axioms Rank3Cert.indep

bsd/Rank3Cert10.lean

lean_all/bsd/Rank3Cert10.lean

/-
  bsd/Rank3Cert10.lean — T39 cycle 552 (goal G266): rank ≥ 3 for 2 more congruent curves (n = 37862, 38982),
  three points whose 2-descent images with E[2] span order 32 (blocks 37001–39000, tools/rank3scan.py). Lean 4 core, rfl.
-/
import bsd.Rank3Cert2

namespace Rank3Cert10
open Rank3Cert2

def rows : List (Nat × Pt × Pt × Pt) := [
   (37862, (true, 69938, 1, 15550920), (false, 22399619, 25, 36115547721), (true, 55300654746481, 20320, 394465311041893994121)),
   (38982, (false, 3650, 1, 2344760), (false, 18818, 1, 4683160), (true, 83265625, 2, 759797495875))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all onRow = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow3 = true := rfl
theorem count : rows.length = 2 := rfl

end Rank3Cert10

#print axioms Rank3Cert10.on_curve
#print axioms Rank3Cert10.indep
#print axioms Rank3Cert10.count

bsd/Rank3Cert11.lean

lean_all/bsd/Rank3Cert11.lean

/-
  bsd/Rank3Cert11.lean — T39 cycle 563 (goal G277): rank ≥ 3 for 6 more congruent curves (n = 39630, 40397, 40406, 40710, 40885, 40894),
  three points whose 2-descent images with E[2] span order 32 (blocks 39001–41000, tools/rank3scan.py). Lean 4 core, rfl.
-/
import bsd.Rank3Cert2

namespace Rank3Cert11
open Rank3Cert2

def rows : List (Nat × Pt × Pt × Pt) := [
   (39630, (false, 2420, 1, 1945900), (false, 26934, 1, 4770936), (false, 10830, 1, 3967200)),
   (40397, (true, 163125, 1, 63831900), (true, 1507072, 1, 1849459920), (true, 100868525, 49, 278107666080)),
   (40406, (false, 164738, 3, 131586056), (false, 15842, 11, 615366648), (true, 127094225, 44, 1129254949095)),
   (40710, (true, 62974, 1, 12056968), (false, 6900, 1, 3332700), (false, 1035, 1, 1309275)),
   (40885, (false, 39204, 1, 2297394), (false, 221, 3, 5470192), (false, 10829, 1, 4102644)),
   (40894, (false, 1694, 1, 1681680), (true, 374978, 1, 228249888), (false, 136367, 2, 33360327))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all onRow = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow3 = true := rfl
theorem count : rows.length = 6 := rfl

end Rank3Cert11

#print axioms Rank3Cert11.on_curve
#print axioms Rank3Cert11.indep
#print axioms Rank3Cert11.count

bsd/Rank3Cert12.lean

lean_all/bsd/Rank3Cert12.lean

/-
  bsd/Rank3Cert12.lean — T39 cycle 574 (goal G288): rank ≥ 3 for 8 more congruent curves (n = 41151, 41181, 41230, 41309, 41582, 41943, 42029, 42486),
  three points whose 2-descent images with E[2] span order 32 (blocks 41001–43000, tools/rank3scan.py). Lean 4 core, rfl.
-/
import bsd.Rank3Cert2

namespace Rank3Cert12
open Rank3Cert2

def rows : List (Nat × Pt × Pt × Pt) := [
   (41151, (false, 473, 1, 894916), (false, 3225, 1, 2329740), (true, 243049, 1, 118093220)),
   (41181, (true, 613312, 1, 479226664), (true, 103933, 1, 30764168), (true, 1936732, 3, 2645469134)),
   (41230, (false, 7595, 1, 3531675), (true, 126350, 1, 42453600), (true, 492480, 1, 344394000)),
   (41309, (false, 138269, 3, 128328580), (false, 186749, 17, 5158441680), (true, 349286910025, 1272, 202615719633579835)),
   (41582, (true, 41650, 1, 485520), (false, 6780862350, 1351, 6224696883442080), (true, 4930172204584225, 45194, 346121583960002637431535)),
   (41943, (true, 42025, 1, 537920), (true, 1302775, 1, 1486205720), (true, 3442857, 1, 6387725520)),
   (42029, (true, 1789333, 3, 2339425088), (true, 143936393, 2, 1726854022557), (false, 2598544, 17, 19126645980)),
   (42486, (true, 46818, 1, 4255848), (true, 63948, 1, 12086172), (false, 5475, 1, 3117465))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all onRow = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow3 = true := rfl
theorem count : rows.length = 8 := rfl

end Rank3Cert12

#print axioms Rank3Cert12.on_curve
#print axioms Rank3Cert12.indep
#print axioms Rank3Cert12.count

bsd/Rank3Cert13.lean

lean_all/bsd/Rank3Cert13.lean

/-
  bsd/Rank3Cert13.lean — T39 cycle 586 (goal G300): rank ≥ 3 for the congruent curve n = 43870,
  three points whose 2-descent images with E[2] span order 32 (blocks 43001–45000, tools/rank3scan.py). Lean 4 core, rfl.
-/
import bsd.Rank3Cert2

namespace Rank3Cert13
open Rank3Cert2

def rows : List (Nat × Pt × Pt × Pt) := [
   (43870, (false, 1926, 1, 1923432), (true, 485809, 2, 315747273), (false, 386420, 3, 50387500))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all onRow = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow3 = true := rfl
theorem count : rows.length = 1 := rfl

end Rank3Cert13

#print axioms Rank3Cert13.on_curve
#print axioms Rank3Cert13.indep
#print axioms Rank3Cert13.count

bsd/Rank3Cert14.lean

lean_all/bsd/Rank3Cert14.lean

/-
  bsd/Rank3Cert14.lean — T39 cycle 603 (goal G316): rank ≥ 3 for 6 more congruent curves (n = 45118, 46246, 46274, 46754, 47094, 47957),
  three points whose 2-descent images with E[2] span order 32 (blocks 45001–48000, tools/rank3scan.py). Lean 4 core, rfl.
-/
import bsd.Rank3Cert2

namespace Rank3Cert14
open Rank3Cert2

def rows : List (Nat × Pt × Pt × Pt) := [
   (45118, (true, 46818, 1, 2705040), (true, 57492657, 32, 259456170135), (true, 2375266414257025, 114962, 112055502124249252677855)),
   (46246, (true, 57798, 1, 8334768), (false, 51842, 9, 852822152), (true, 1918318500723769, 49858, 83868685832694965951565)),
   (46274, (true, 46818, 1, 1539792), (false, 4802, 1, 3189312), (false, 28224, 1, 6160560)),
   (46754, (true, 116162, 1, 36242544), (false, 28322, 1, 6260352), (false, 13968, 7, 270753384)),
   (47094, (true, 80802, 1, 18664056), (true, 61206, 1, 9671760), (true, 118581, 1, 37475685)),
   (47957, (false, 868, 1, 1412670), (false, 3757, 1, 2930460), (false, 36535652, 33, 225566155970))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all onRow = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow3 = true := rfl
theorem count : rows.length = 6 := rfl

end Rank3Cert14

#print axioms Rank3Cert14.on_curve
#print axioms Rank3Cert14.indep
#print axioms Rank3Cert14.count

bsd/Rank3Cert15.lean

lean_all/bsd/Rank3Cert15.lean

/-
  bsd/Rank3Cert15.lean — T39 cycle 614 (goal G327): rank ≥ 3 for the congruent curve n = 48622,
  three points whose 2-descent images with E[2] span order 32 (blocks 48001–50000, tools/rank3scan.py). Lean 4 core, rfl.
-/
import bsd.Rank3Cert2

namespace Rank3Cert15
open Rank3Cert2

def rows : List (Nat × Pt × Pt × Pt) := [
   (48622, (false, 42350, 1, 4915680), (false, 55223, 4, 182354235), (true, 12353369, 10, 39914270403))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all onRow = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow3 = true := rfl
theorem count : rows.length = 1 := rfl

end Rank3Cert15

#print axioms Rank3Cert15.on_curve
#print axioms Rank3Cert15.indep
#print axioms Rank3Cert15.count

bsd/Rank3Cert16.lean

lean_all/bsd/Rank3Cert16.lean

/-
  bsd/Rank3Cert16.lean — T39 cycle 635 (goal G348): rank ≥ 3 for seven congruent curves n ∈ 50001–54000,
  three points each whose 2-descent images with E[2] span order 32 (tools/rank3scan.py). Lean 4 core, rfl.
-/
import bsd.Rank3Cert2

namespace Rank3Cert16
open Rank3Cert2

def rows : List (Nat × Pt × Pt × Pt) := [
   (50061, (true, 62197, 1, 9205156), (false, 38148, 1, 6331446), (false, 164, 35, 785339666)),
   (50583, (true, 5720871, 5, 13344895044), (false, 1192464, 5, 459652284), (true, 352876225, 58, 5807172188935)),
   (50629, (false, 900, 1, 1518630), (true, 3806237, 7, 5631806844), (true, 4689914315641, 8786, 5614315397238380805)),
   (51359, (true, 174087, 1, 69402684), (true, 559728, 1, 416993772), (false, 41216, 1, 6221040)),
   (51590, (true, 59290, 1, 7114800), (true, 52094, 1, 1649928), (true, 197516, 1, 84734364)),
   (51933, (true, 53067, 1, 2513700), (false, 151725, 23, 10700917920), (true, 30920642834302225, 282124, 5388365280109407205748615)),
   (53605, (true, 60400, 1, 6840300), (true, 680805, 1, 559994400), (true, 16049089225, 458, 1450732718185725))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all onRow = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow3 = true := rfl
theorem count : rows.length = 7 := rfl

end Rank3Cert16

#print axioms Rank3Cert16.on_curve
#print axioms Rank3Cert16.indep
#print axioms Rank3Cert16.count

bsd/Rank3Cert17.lean

lean_all/bsd/Rank3Cert17.lean

/-
  bsd/Rank3Cert17.lean — T39 cycle 657 (goal G369): rank ≥ 3 for eight congruent curves n ∈ 54001–58000,
  three points each whose 2-descent images with E[2] span order 32 (tools/rank3scan.py). Lean 4 core, rfl.
-/
import bsd.Rank3Cert2

namespace Rank3Cert17
open Rank3Cert2

def rows : List (Nat × Pt × Pt × Pt) := [
   (55279, (true, 78652, 1, 15691074), (false, 3420207, 8, 1673264103), (true, 85374411721, 1224, 6059125833583355)),
   (55510, (true, 59150, 1, 4968600), (true, 74725, 1, 13674675), (true, 6840414, 11, 3387012888)),
   (55549, (false, 6877, 1, 4571112), (true, 50820037489, 942, 2788059628529695), (true, 6059258800420081, 55212, 471476006811811532707255)),
   (56406, (true, 70602, 1, 11282544), (true, 73644, 1, 12848940), (true, 68544, 1, 10195920)),
   (56630, (true, 95830, 1, 23931600), (false, 19600, 1, 7438200), (true, 5294205, 1, 12180813225)),
   (56990, (false, 810, 1, 1621800), (true, 193210, 1, 81148200), (false, 4453699, 13, 18022029849)),
   (57310, (true, 89100, 1, 20364300), (false, 48400, 1, 6751800), (true, 95753790, 23, 888784010400)),
   (57715, (true, 91035, 1, 21241500), (false, 19600, 1, 7599900), (false, 56644, 1, 2633946))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all onRow = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow3 = true := rfl
theorem count : rows.length = 8 := rfl

end Rank3Cert17

#print axioms Rank3Cert17.on_curve
#print axioms Rank3Cert17.indep
#print axioms Rank3Cert17.count

bsd/Rank3Cert18.lean

lean_all/bsd/Rank3Cert18.lean

/-
  bsd/Rank3Cert18.lean — T39 cycle 668 (goal G380): rank ≥ 3 for three congruent curves n ∈ 58001–60000,
  three points each whose 2-descent images with E[2] span order 32 (tools/rank3scan.py). Lean 4 core, rfl.
-/
import bsd.Rank3Cert2

namespace Rank3Cert18
open Rank3Cert2

def rows : List (Nat × Pt × Pt × Pt) := [
   (58326, (true, 62658, 1, 5730552), (false, 57600, 1, 2201760), (false, 1554486, 31, 69857318160)),
   (58695, (true, 72111, 1, 11249316), (false, 3225, 1, 3328200), (true, 1221025, 3, 1216428200)),
   (59415, (false, 4335, 1, 3901500), (false, 10404, 1, 5966694), (false, 52215, 1, 6478200))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all onRow = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow3 = true := rfl
theorem count : rows.length = 3 := rfl

end Rank3Cert18

#print axioms Rank3Cert18.on_curve
#print axioms Rank3Cert18.indep
#print axioms Rank3Cert18.count

bsd/Rank3Cert19.lean

lean_all/bsd/Rank3Cert19.lean

/-
  bsd/Rank3Cert19.lean — T39 cycle 681 (goal G393): rank ≥ 3 for four congruent curves n ∈ 60001–62000,
  three points each whose 2-descent images with E[2] span order 32 (tools/rank3scan.py). Lean 4 core, rfl.
-/
import bsd.Rank3Cert2

namespace Rank3Cert19
open Rank3Cert2

def rows : List (Nat × Pt × Pt × Pt) := [
   (60006, (false, 3362, 1, 3473848), (true, 337842, 1, 193245624), (true, 5361193, 2, 12400993333)),
   (60229, (false, 44100, 1, 8614410), (false, 54692, 1, 5899278), (true, 475632481, 56, 9520104481425)),
   (60415, (true, 69660, 1, 9152550), (true, 30525625, 9, 166472780500), (true, 5337968900574025, 98417, 387648994226103089645100)),
   (61815, (true, 62400, 1, 2129400), (true, 255025, 1, 124947100), (false, 24336, 1, 8864388))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all onRow = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow3 = true := rfl
theorem count : rows.length = 4 := rfl

end Rank3Cert19

#print axioms Rank3Cert19.on_curve
#print axioms Rank3Cert19.indep
#print axioms Rank3Cert19.count

bsd/Rank3Cert2.lean

lean_all/bsd/Rank3Cert2.lean

/-
  bsd/Rank3Cert2.lean — T39 cycle 435 (goal G150): rank ≥ 3 for 27 congruent curves y² = x³ − n²x in the block
  searches (n = 2605, 2774, 3502, 4199, 4669, 4895, 6286, 6671, 7230, 7766, 8005, 9015, 9430, 9654, 10199, 10549, 11005, 11029, 12166, 12270, 12534, 12935, 13317, 14965, 15655, 16206, 16887),
  from three points whose 2-descent images with the torsion images span a group of order 32 (as Rank3Cert).
  Points from the box-2000/6000 C search of the blocks (tools/rank3scan.py). Lean 4 core, rfl.
-/
import bsd.Rank3Cert

namespace Rank3Cert2
open CongruentPoints5 DescentCert Rank2Cert Rank3Cert

abbrev Pt := Bool × Nat × Nat × Nat

def rows : List (Nat × Pt × Pt × Pt) := [
   (2605, (true, 99405, 1, 31330200), (false, 1805, 1, 79800), (false, 18605, 3, 1945900)),
   (2774, (true, 3724, 1, 151620), (false, 475, 1, 59565), (true, 526338, 1, 381848472)),
   (3502, (true, 3698, 1, 72240), (true, 5202, 1, 277440), (true, 449650, 1, 301507920)),
   (4199, (true, 4225, 1, 30420), (false, 3249, 1, 151620), (true, 25857, 1, 4102644)),
   (4669, (true, 14812, 1, 1710786), (true, 10469, 1, 958740), (true, 31581, 1, 5550600)),
   (4895, (false, 55, 1, 36300), (false, 4356, 1, 147378), (true, 42025, 2, 7622925)),
   (6286, (false, 14, 1, 23520), (true, 7168, 1, 291648), (true, 45486, 1, 9607920)),
   (6671, (false, 5103, 1, 306936), (true, 271663, 1, 141551592), (false, 99225, 13, 353752560)),
   (7230, (false, 30, 1, 39600), (true, 7350, 1, 113400), (false, 3600, 1, 376200)),
   (7766, (false, 6875, 1, 299475), (true, 11638, 1, 935088), (true, 288684, 1, 155051820)),
   (8005, (false, 400, 1, 159900), (false, 6084, 17, 180448086), (false, 10765124, 1593, 66650336319574)),
   (9015, (false, 3600, 1, 495900), (false, 1815, 1, 376200), (false, 36015, 2, 341775)),
   (9430, (true, 23805, 1, 3372375), (true, 77326, 1, 21341976), (true, 523529, 2, 377816517)),
   (9654, (true, 16854, 1, 1793520), (false, 5046, 1, 584640), (true, 732108, 1, 626361372)),
   (10199, (true, 24649, 1, 3523080), (true, 35287, 1, 6345696), (true, 20727, 1, 2597784)),
   (10549, (true, 54208, 1, 12379752), (true, 24299, 1, 3412200), (false, 13475, 3, 10909360)),
   (11005, (true, 11045, 1, 98700), (true, 23805, 1, 3256800), (true, 625084, 1, 494128902)),
   (11029, (false, 4100, 1, 655590), (false, 6724, 5, 22602726), (true, 9722946025, 904, 359606279780085)),
   (12166, (false, 308, 1, 213444), (true, 19602, 1, 2151864), (true, 42284, 1, 8327220)),
   (12270, (false, 3600, 1, 703800), (true, 18150, 1, 1801800), (false, 5070, 1, 795600)),
   (12534, (false, 4107, 1, 758907), (true, 41334, 1, 8007840), (false, 2166, 1, 574560)),
   (12935, (true, 88985, 1, 26262600), (true, 133225, 3, 23644700), (true, 2640625, 8, 4074695625)),
   (13317, (true, 52992, 1, 11807280), (true, 931225, 4, 874792765), (false, 43125, 7, 135212400)),
   (14965, (false, 164, 1, 191634), (false, 4356, 1, 944922), (true, 284089, 4, 81489837)),
   (15655, (true, 70225, 2, 8423025), (false, 19375, 2, 8288625), (true, 13965169, 26, 34051988703)),
   (16206, (false, 219, 1, 239805), (true, 16428, 1, 344988), (false, 15123, 1, 716319)),
   (16887, (false, 900, 1, 505890), (false, 24336, 5, 65749788), (true, 6716775, 7, 17275092120))]

def onRow (r : Nat × Pt × Pt × Pt) : Bool :=
  [r.2.1, r.2.2.1, r.2.2.2].all (fun p => onC r.1 p.1 p.2.1 p.2.2.1 p.2.2.2)

def indepRow3 (r : Nat × Pt × Pt × Pt) : Bool :=
  let a := img r.1 r.2.1; let b := img r.1 r.2.2.1; let c := img r.1 r.2.2.2
  [a, b, c, mul2 a b, mul2 a c, mul2 b c, mul2 (mul2 a b) c].all (nontriv r.1)

set_option maxRecDepth 100000 in
theorem on_curve : rows.all onRow = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow3 = true := rfl
theorem count : rows.length = 27 := rfl

end Rank3Cert2

#print axioms Rank3Cert2.on_curve
#print axioms Rank3Cert2.indep
#print axioms Rank3Cert2.count

bsd/Rank3Cert20.lean

lean_all/bsd/Rank3Cert20.lean

/-
  bsd/Rank3Cert20.lean — T39 cycle 695 (goal G408): rank ≥ 3 for one congruent curve n ∈ 62001–64000,
  three points each whose 2-descent images with E[2] span order 32 (tools/rank3scan.py). Lean 4 core, rfl.
-/
import bsd.Rank3Cert2

namespace Rank3Cert20
open Rank3Cert2

def rows : List (Nat × Pt × Pt × Pt) := [
   (63005, (true, 1099805, 1, 1151488800), (true, 590839732921, 2268, 379736774111073581), (true, 47986723290025, 16258, 311755409151427890675))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all onRow = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow3 = true := rfl
theorem count : rows.length = 1 := rfl

end Rank3Cert20

#print axioms Rank3Cert20.on_curve
#print axioms Rank3Cert20.indep
#print axioms Rank3Cert20.count

bsd/Rank3Cert21.lean

lean_all/bsd/Rank3Cert21.lean

/-
  bsd/Rank3Cert21.lean — T39 cycle 718 (goal G431): rank ≥ 3 for two congruent curves n ∈ 64001–66000,
  three points each whose 2-descent images with E[2] span order 32 (tools/rank3scan.py). Lean 4 core, rfl.
-/
import bsd.Rank3Cert2

namespace Rank3Cert21
open Rank3Cert2

def rows : List (Nat × Pt × Pt × Pt) := [
   (65310, (true, 82810, 1, 14651000), (true, 78750, 1, 12348000), (true, 69360, 1, 6150600)),
   (65535, (false, 735, 1, 1776600), (true, 66049, 1, 2113568), (true, 418335, 1, 267233400))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all onRow = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow3 = true := rfl
theorem count : rows.length = 2 := rfl

end Rank3Cert21

#print axioms Rank3Cert21.on_curve
#print axioms Rank3Cert21.indep
#print axioms Rank3Cert21.count

bsd/Rank3Cert22.lean

lean_all/bsd/Rank3Cert22.lean

/-
  bsd/Rank3Cert22.lean — T39 cycle 749 (goal G462): rank ≥ 3 for one congruent curve n ∈ 66001–68000,
  three points each whose 2-descent images with E[2] span order 32 (tools/rank3scan.py). Lean 4 core, rfl.
-/
import bsd.Rank3Cert2

namespace Rank3Cert22
open Rank3Cert2

def rows : List (Nat × Pt × Pt × Pt) := [
   (67606, (true, 629442, 1, 496493976), (true, 206998, 1, 89013408), (true, 170093, 1, 64371153))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all onRow = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow3 = true := rfl
theorem count : rows.length = 1 := rfl

end Rank3Cert22

#print axioms Rank3Cert22.on_curve
#print axioms Rank3Cert22.indep
#print axioms Rank3Cert22.count

bsd/Rank3Cert23.lean

lean_all/bsd/Rank3Cert23.lean

/-
  bsd/Rank3Cert23.lean — T39 cycle 782 (goal G493): rank ≥ 3 for five congruent curves n ∈ 68001–70000,
  three points each whose 2-descent images with E[2] span order 32 (tools/rank3scan.py). Lean 4 core, rfl.
-/
import bsd.Rank3Cert2

namespace Rank3Cert23
open Rank3Cert2

def rows : List (Nat × Pt × Pt × Pt) := [
   (68295, (true, 76705, 1, 9671500), (false, 5220, 1, 4919850), (false, 7146180, 241, 10603744757550)),
   (68839, (false, 1127, 1, 2310672), (true, 1825648, 1, 2464998228), (false, 347024, 3, 302344660)),
   (69015, (false, 321, 1, 1236492), (true, 70225, 1, 3439700), (false, 20640000, 47, 686241558000)),
   (69509, (false, 15059, 1, 8327220), (true, 177500, 1, 68809650), (false, 4269419, 771, 85375681528060)),
   (69870, (false, 14400, 1, 8204400), (false, 41070, 1, 11455200), (true, 126514, 1, 37514512))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all onRow = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow3 = true := rfl
theorem count : rows.length = 5 := rfl

end Rank3Cert23

#print axioms Rank3Cert23.on_curve
#print axioms Rank3Cert23.indep
#print axioms Rank3Cert23.count

bsd/Rank3Cert24.lean

lean_all/bsd/Rank3Cert24.lean

/-
  bsd/Rank3Cert24.lean — T39 cycle 813 (goal G524): rank ≥ 3 for five congruent curves n ∈ 70001–72000,
  three points each whose 2-descent images with E[2] span order 32 (tools/rank3scan.py). Lean 4 core, rfl.
-/
import bsd.Rank3Cert2

namespace Rank3Cert24
open Rank3Cert2

def rows : List (Nat × Pt × Pt × Pt) := [
   (70013, (false, 8957, 17, 1914951480), (true, 20999755337401, 4260, 96056106055575821101), (true, 510485478585502225, 1207678, 357361603219319850653481135)),
   (70189, (true, 392533, 1, 241968012), (true, 638428, 3, 73856818), (true, 74132275, 13, 630055609260)),
   (70774, (true, 176418, 1, 67875192), (false, 1768811, 5, 58079637), (true, 2375035212954889, 182742, 11407546731450336429115)),
   (70941, (false, 26741, 1, 10745020), (true, 284089, 2, 7240805), (true, 10553413, 1, 34283060812)),
   (70959, (false, 327, 1, 1283148), (true, 97216, 1, 20719160), (true, 153328, 1, 53222428))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all onRow = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow3 = true := rfl
theorem count : rows.length = 5 := rfl

end Rank3Cert24

#print axioms Rank3Cert24.on_curve
#print axioms Rank3Cert24.indep
#print axioms Rank3Cert24.count

bsd/Rank3Cert25.lean

lean_all/bsd/Rank3Cert25.lean

/-
  bsd/Rank3Cert25.lean — T39 cycle 845 (goal G555): rank ≥ 3 for three congruent curves n ∈ 72001–74000,
  three points each whose 2-descent images with E[2] span order 32 (tools/rank3scan.py). Lean 4 core, rfl.
-/
import bsd.Rank3Cert2

namespace Rank3Cert25
open Rank3Cert2

def rows : List (Nat × Pt × Pt × Pt) := [
   (72854, (false, 12482, 3, 73241848), (false, 410114, 3, 327626920), (false, 1930850, 27, 73751015240)),
   (73055, (false, 70756, 1, 4836678), (false, 1299600, 13, 13996583700), (true, 80233295, 23, 629811285600)),
   (73151, (true, 714025, 1, 600176460), (true, 140777, 1, 45129084), (true, 242730626329, 152, 119584897923721725))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all onRow = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow3 = true := rfl
theorem count : rows.length = 3 := rfl

end Rank3Cert25

#print axioms Rank3Cert25.on_curve
#print axioms Rank3Cert25.indep
#print axioms Rank3Cert25.count

bsd/Rank3Cert26.lean

lean_all/bsd/Rank3Cert26.lean

/-
  bsd/Rank3Cert26.lean — T39 cycle 877 (goal G590): rank ≥ 3 for one congruent curve n ∈ 74001–76000,
  three points each whose 2-descent images with E[2] span order 32 (tools/rank3scan.py). Lean 4 core, rfl.
-/
import bsd.Rank3Cert2

namespace Rank3Cert26
open Rank3Cert2

def rows : List (Nat × Pt × Pt × Pt) := [
   (75174, (false, 98, 1, 744184), (true, 77452, 1, 5189284), (true, 152626, 1, 51892840))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all onRow = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow3 = true := rfl
theorem count : rows.length = 1 := rfl

end Rank3Cert26

#print axioms Rank3Cert26.on_curve
#print axioms Rank3Cert26.indep
#print axioms Rank3Cert26.count

bsd/Rank3Cert27.lean

lean_all/bsd/Rank3Cert27.lean

/-
  bsd/Rank3Cert27.lean — T39 cycle 900 (goal G603): rank ≥ 3 for three congruent curves n ∈ 76001–78000 (76245, 76479, 77486),
  three points each whose 2-descent images with E[2] span order 32 (tools/rank3scan.py). Lean 4 core, rfl.
-/
import bsd.Rank3Cert2

namespace Rank3Cert27
open Rank3Cert2

def rows : List (Nat × Pt × Pt × Pt) := [
   (76245, (false, 195, 1, 1064700), (false, 8619, 1, 7033104), (false, 76176, 1, 895068)),
   (76479, (true, 474721, 1, 322810280), (false, 74529, 1, 4684680), (true, 4381404, 1, 9169671510)),
   (77486, (false, 72283, 1, 7505091), (true, 328457, 2, 62305221), (false, 1006587, 31, 74702064717))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all onRow = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow3 = true := rfl
theorem count : rows.length = 3 := rfl

end Rank3Cert27

#print axioms Rank3Cert27.on_curve
#print axioms Rank3Cert27.indep
#print axioms Rank3Cert27.count

bsd/Rank3Cert28.lean

lean_all/bsd/Rank3Cert28.lean

/-
  bsd/Rank3Cert28.lean — T39 cycle 927 (goal G618): rank ≥ 3 for one congruent curve n ∈ 78001–80000,
  three points each whose 2-descent images with E[2] span order 32 (tools/rank3scan.py). Lean 4 core, rfl.
-/
import bsd.Rank3Cert2

namespace Rank3Cert28
open Rank3Cert2

def rows : List (Nat × Pt × Pt × Pt) := [
   (78526, (true, 87362, 1, 11316096), (false, 8848, 1, 7339416), (false, 52983, 2, 71264571))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all onRow = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow3 = true := rfl
theorem count : rows.length = 1 := rfl

end Rank3Cert28

#print axioms Rank3Cert28.on_curve
#print axioms Rank3Cert28.indep
#print axioms Rank3Cert28.count

bsd/Rank3Cert29.lean

lean_all/bsd/Rank3Cert29.lean

/-
  bsd/Rank3Cert29.lean — T39 cycle 1444 (goal G830): rank ≥ 3 for 10 congruent curves n ∈ 80015–84390,
  three points each whose 2-descent images with E[2] span order 32 (tools/rank3scan.py). Lean 4 core, rfl.
-/
import bsd.Rank3Cert2

namespace Rank3Cert29
open Rank3Cert2

def rows : List (Nat × Pt × Pt × Pt) := [
   (80015, (true, 491985, 2, 262080975), (true, 12229009, 11, 26124981948), (true, 10250550025, 249, 908174735125100)),
   (80189, (true, 419813, 1, 267001068), (true, 429777, 2, 187526031), (true, 1560753, 4, 1110261993)),
   (81669, (false, 86247, 2, 92533707), (false, 1550883, 7, 4594125276), (false, 38773279443, 863, 9228006392052636)),
   (81959, (true, 133209, 1, 38326800), (true, 11518075281241, 49, 39090377907953835240), (true, 205688403269281, 49197, 779943430652973661720)),
   (82205, (false, 1845, 1, 3530100), (false, 60516, 1, 13686702), (true, 111005, 1, 24853200)),
   (82309, (true, 172197, 1, 62764296), (false, 156977573, 51, 1823861216764), (true, 2839741074786841, 150118, 114585411325090541662155)),
   (83159, (true, 5534937, 7, 8812721184), (true, 52722121, 16, 350219123205), (true, 60879147169, 315, 14882524835281928)),
   (83845, (false, 10404, 1, 8486094), (false, 16605, 1, 10590300), (true, 3706605, 1, 7134332100)),
   (84134, (true, 104222, 1, 19858104), (true, 529702, 1, 380626464), (true, 6906304, 9, 2943812080)),
   (84390, (false, 291, 1, 1439577), (false, 63504, 1, 14005656), (true, 126150, 1, 33303600))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all onRow = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow3 = true := rfl
theorem count : rows.length = 10 := rfl

end Rank3Cert29

#print axioms Rank3Cert29.on_curve
#print axioms Rank3Cert29.indep
#print axioms Rank3Cert29.count

bsd/Rank3Cert3.lean

lean_all/bsd/Rank3Cert3.lean

/-
  bsd/Rank3Cert3.lean — T39 cycle 447 (goal G164): rank ≥ 3 for 4 more congruent curves (n = 17958, 18221, 19046, 19726),
  three points whose 2-descent images with E[2] span order 32 (blocks 17801–20000, tools/rank3scan.py). Lean 4 core, rfl.
-/
import bsd.Rank3Cert2

namespace Rank3Cert3
open Rank3Cert2

def rows : List (Nat × Pt × Pt × Pt) := [
   (17958, (true, 30258, 1, 4236120), (false, 15842, 1, 1064440), (true, 1252753, 4, 1364783545)),
   (18221, (false, 171, 1, 238260), (true, 525532, 1, 380747934), (true, 3171961, 4, 5625350835)),
   (19046, (true, 22898, 1, 1923432), (true, 4114025, 2, 8343070485), (true, 6709887219025, 524, 17380917529444250535)),
   (19726, (false, 19600, 1, 311640), (false, 51842, 3, 38660608), (true, 22882048, 1, 109456656288))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all onRow = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow3 = true := rfl
theorem count : rows.length = 4 := rfl

end Rank3Cert3

#print axioms Rank3Cert3.on_curve
#print axioms Rank3Cert3.indep
#print axioms Rank3Cert3.count

bsd/Rank3Cert30.lean

lean_all/bsd/Rank3Cert30.lean

/-
  bsd/Rank3Cert30.lean — T39 cycle 1444 (goal G830): rank ≥ 3 for 10 congruent curves n ∈ 85470–91910,
  three points each whose 2-descent images with E[2] span order 32 (tools/rank3scan.py). Lean 4 core, rfl.
-/
import bsd.Rank3Cert2

namespace Rank3Cert30
open Rank3Cert2

def rows : List (Nat × Pt × Pt × Pt) := [
   (85470, (false, 770, 1, 2371600), (true, 89100, 1, 7514100), (true, 92400, 1, 10672200)),
   (86086, (false, 78650, 1, 9815520), (true, 112112, 1, 24048024), (true, 1137150, 1, 1209145560)),
   (86790, (true, 87846, 1, 4024944), (false, 86490, 1, 2120400), (true, 209484, 1, 87263748)),
   (88206, (false, 85683, 1, 6131151), (true, 1452025, 4, 411503885), (true, 11140225, 8, 32055194305)),
   (88502, (true, 229900, 1, 101737020), (true, 306850, 1, 162753240), (false, 61650, 1, 15765960)),
   (89286, (true, 786258, 1, 692674488), (true, 348486, 1, 198853920), (false, 79764, 1, 11331180)),
   (90174, (false, 798, 1, 2547216), (false, 28322, 1, 14407568), (false, 44688, 1, 16556904)),
   (90597, (true, 1221025, 2, 1288439945), (false, 190463, 2, 134548505), (false, 17887077, 29, 313234283700)),
   (91749, (false, 35301, 1, 15911280), (false, 43197, 1, 16823268), (false, 2286144, 5, 281735496)),
   (91910, (true, 96446, 1, 9077544), (true, 532350, 1, 382582200), (true, 836381, 1, 760270329))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all onRow = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow3 = true := rfl
theorem count : rows.length = 10 := rfl

end Rank3Cert30

#print axioms Rank3Cert30.on_curve
#print axioms Rank3Cert30.indep
#print axioms Rank3Cert30.count

bsd/Rank3Cert31.lean

lean_all/bsd/Rank3Cert31.lean

/-
  bsd/Rank3Cert31.lean — T39 cycle 1444 (goal G830): rank ≥ 3 for 10 congruent curves n ∈ 92045–103309,
  three points each whose 2-descent images with E[2] span order 32 (tools/rank3scan.py). Lean 4 core, rfl.
-/
import bsd.Rank3Cert2

namespace Rank3Cert31
open Rank3Cert2

def rows : List (Nat × Pt × Pt × Pt) := [
   (92045, (false, 57600, 1, 17230800), (false, 16400, 1, 11598900), (false, 59245, 1, 17146200)),
   (92157, (false, 5525, 1, 6837740), (true, 184093, 1, 68377400), (false, 2941952, 419, 27750713816800)),
   (93126, (false, 1922, 1, 4081832), (true, 454674, 1, 300084840), (true, 361548, 1, 210059388)),
   (94655, (false, 48400, 1, 17895900), (true, 191455, 1, 72817800), (true, 51439140399940225, 268398, 11563544039594308100316775)),
   (95095, (false, 4004, 1, 6012006), (true, 455455, 1, 300600300), (false, 228095, 6, 1631368025)),
   (99231, (true, 607600, 1, 467257420), (true, 205953, 1, 81901512), (true, 310497, 1, 163942416)),
   (99309, (true, 271803, 1, 131906628), (true, 5744109, 1, 13764774240), (false, 345744, 5, 1445617404)),
   (99645, (false, 44100, 1, 18764550), (false, 99099, 1, 3279276), (false, 70980, 1, 18632250)),
   (100711, (true, 286273, 1, 143377572), (true, 1269710689, 18, 45228658140055), (true, 53872731025, 6, 12504130206240815)),
   (103309, (true, 5736688, 1, 13737933588), (true, 3771297, 4, 6582850959), (true, 1124998681, 104, 4381088692605))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all onRow = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow3 = true := rfl
theorem count : rows.length = 10 := rfl

end Rank3Cert31

#print axioms Rank3Cert31.on_curve
#print axioms Rank3Cert31.indep
#print axioms Rank3Cert31.count

bsd/Rank3Cert32.lean

lean_all/bsd/Rank3Cert32.lean

/-
  bsd/Rank3Cert32.lean — T39 cycle 1444 (goal G830): rank ≥ 3 for 10 congruent curves n ∈ 103670–112326,
  three points each whose 2-descent images with E[2] span order 32 (tools/rank3scan.py). Lean 4 core, rfl.
-/
import bsd.Rank3Cert2

namespace Rank3Cert32
open Rank3Cert2

def rows : List (Nat × Pt × Pt × Pt) := [
   (103670, (false, 2420, 1, 5098500), (false, 78400, 1, 18992400), (true, 260470, 1, 121951200)),
   (103727, (true, 957177, 1, 930943260), (true, 7102225, 6, 16100120465), (true, 7045738493161, 5090, 17288249815251943341)),
   (104006, (false, 38, 1, 641136), (false, 322, 1, 1866312), (true, 208658, 1, 82628568)),
   (105735, (false, 735, 1, 2866500), (true, 474721, 1, 318866444), (true, 779100, 1, 681322950)),
   (106711, (true, 16265089, 7, 62115056100), (true, 31508225, 16, 88128293505), (false, 54286529, 31, 641024509620)),
   (108717, (true, 141267, 1, 33904080), (true, 323733, 1, 173498940), (true, 41440273, 16, 197650662265)),
   (110589, (true, 111747, 1, 5363856), (true, 710734273, 48, 17688431996767), (true, 415783804969, 252, 268064555999170645)),
   (110805, (true, 310005, 1, 161202600), (true, 427645, 1, 270106100), (true, 299105079025, 322, 163461348425585825)),
   (112119, (true, 552727, 1, 402385256), (true, 303753, 1, 155588112), (true, 108097609, 17, 1072211419000)),
   (112326, (false, 582, 1, 2709792), (true, 169362, 1, 52163496), (true, 142572, 1, 33153876))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all onRow = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow3 = true := rfl
theorem count : rows.length = 10 := rfl

end Rank3Cert32

#print axioms Rank3Cert32.on_curve
#print axioms Rank3Cert32.indep
#print axioms Rank3Cert32.count

bsd/Rank3Cert33.lean

lean_all/bsd/Rank3Cert33.lean

/-
  bsd/Rank3Cert33.lean — T39 cycle 1444 (goal G830): rank ≥ 3 for 10 congruent curves n ∈ 113919–120694,
  three points each whose 2-descent images with E[2] span order 32 (tools/rank3scan.py). Lean 4 core, rfl.
-/
import bsd.Rank3Cert2

namespace Rank3Cert33
open Rank3Cert2

def rows : List (Nat × Pt × Pt × Pt) := [
   (113919, (true, 629031, 1, 490644180), (true, 900601, 2, 737197435), (false, 3268967, 6, 4477470283)),
   (114086, (true, 128122, 1, 20870256), (true, 1507072, 3, 1354292576), (false, 348103, 2, 174099891)),
   (114294, (false, 11094, 1, 11981520), (true, 136482, 1, 27557496), (false, 257762, 3, 505584008)),
   (115102, (false, 96148, 1, 19620900), (true, 4501793, 4, 8715827505), (true, 68699817, 14, 537843023115)),
   (115702, (true, 124002, 1, 15706920), (false, 542882, 3, 654730280), (true, 127093750, 1, 1432801776000)),
   (116645, (false, 33124, 1, 20355426), (false, 86756, 1, 22965822), (false, 1017005, 3, 262572200)),
   (119094, (true, 127308, 1, 16051932), (true, 133206, 1, 21777840), (false, 11052282, 11, 30741434208)),
   (119255, (true, 180625, 1, 57655500), (true, 478975, 1, 321050100), (true, 6063705, 7, 3987423600)),
   (120414, (true, 212268, 1, 80538948), (true, 156282, 1, 39383064), (true, 146461, 1, 31907575)),
   (120694, (true, 153328, 1, 37028712), (false, 176967, 2, 188955123), (false, 62678, 97, 284306450736))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all onRow = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow3 = true := rfl
theorem count : rows.length = 10 := rfl

end Rank3Cert33

#print axioms Rank3Cert33.on_curve
#print axioms Rank3Cert33.indep
#print axioms Rank3Cert33.count

bsd/Rank3Cert34.lean

lean_all/bsd/Rank3Cert34.lean

/-
  bsd/Rank3Cert34.lean — T39 cycle 1444 (goal G830): rank ≥ 3 for 6 congruent curves n ∈ 123222–125749,
  three points each whose 2-descent images with E[2] span order 32 (tools/rank3scan.py). Lean 4 core, rfl.
-/
import bsd.Rank3Cert2

namespace Rank3Cert34
open Rank3Cert2

def rows : List (Nat × Pt × Pt × Pt) := [
   (123222, (false, 528, 1, 2831400), (true, 39436914, 5, 246902427288), (false, 1862982, 13, 28309644000)),
   (123510, (true, 192246, 1, 64594656), (false, 15210, 1, 15116400), (true, 672296076, 5, 17431561160676)),
   (123710, (false, 50179, 1, 25329831), (true, 2289169, 4, 1739903097), (true, 102086649, 22, 835418895507)),
   (124670, (false, 6370, 1, 9937200), (true, 532350, 1, 377613600), (false, 3111696, 5, 312972408)),
   (124815, (false, 85860, 1, 26545050), (true, 59059225, 8, 449699340925), (true, 127358400, 7, 1435621847400)),
   (125749, (true, 320437, 1, 166839504), (false, 33813, 1, 22271496), (false, 45968, 3, 242446724))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all onRow = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow3 = true := rfl
theorem count : rows.length = 6 := rfl

end Rank3Cert34

#print axioms Rank3Cert34.on_curve
#print axioms Rank3Cert34.indep
#print axioms Rank3Cert34.count

bsd/Rank3Cert35.lean

lean_all/bsd/Rank3Cert35.lean

/-
  bsd/Rank3Cert35.lean — T39 cycle 1484 (goal G835): rank ≥ 3 for 4 congruent curves n ∈ 128001–130000,
  three points each whose 2-descent images with E[2] span order 32 (tools/rank3scan.py). Lean 4 core, rfl.
-/
import bsd.Rank3Cert2

namespace Rank3Cert35
open Rank3Cert2

def rows : List (Nat × Pt × Pt × Pt) := [
   (128030, (true, 566636, 1, 415506084), (true, 243670, 1, 102341400), (true, 302080, 1, 150379200)),
   (128935, (true, 35352049, 12, 178872229943), (true, 216119401, 36, 2014909519051), (true, 105069981025, 561, 31415457175183900)),
   (129630, (false, 57600, 1, 27871200), (true, 133206, 1, 11189304), (true, 144150, 1, 23938200)),
   (129935, (true, 637065, 2, 294047325), (true, 649893049, 17, 16540043887068), (true, 2260527025, 123, 53064680671900))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all onRow = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow3 = true := rfl
theorem count : rows.length = 4 := rfl

end Rank3Cert35

#print axioms Rank3Cert35.on_curve
#print axioms Rank3Cert35.indep
#print axioms Rank3Cert35.count

bsd/Rank3Cert36.lean

lean_all/bsd/Rank3Cert36.lean

/-
  bsd/Rank3Cert36.lean — T39 cycle 1524 (goal G837): rank ≥ 3 for 3 congruent curves n ∈ 132001–134000,
  three points each whose 2-descent images with E[2] span order 32 (tools/rank3scan.py). Lean 4 core, rfl.
-/
import bsd.Rank3Cert2

namespace Rank3Cert36
open Rank3Cert2

def rows : List (Nat × Pt × Pt × Pt) := [
   (132046, (true, 323008, 1, 167537328), (true, 7395858, 7, 9742810272), (true, 398647018225, 1694, 78190044575194455)),
   (132405, (false, 44100, 1, 26217450), (false, 66339, 1, 29513484), (false, 56355, 1, 28442700)),
   (132559, (true, 3114223, 3, 5076454292), (false, 3337929, 65, 1023214689756), (true, 320159449, 33, 5113242171820))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all onRow = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow3 = true := rfl
theorem count : rows.length = 3 := rfl

end Rank3Cert36

#print axioms Rank3Cert36.on_curve
#print axioms Rank3Cert36.indep
#print axioms Rank3Cert36.count

bsd/Rank3Cert37.lean

lean_all/bsd/Rank3Cert37.lean

/-
  bsd/Rank3Cert37.lean — T39 cycle 1544 (goal G839): rank ≥ 3 for 2 congruent curves n ∈ 134001–136000,
  three points each whose 2-descent images with E[2] span order 32 (tools/rank3scan.py). Lean 4 core, rfl.
-/
import bsd.Rank3Cert2

namespace Rank3Cert37
open Rank3Cert2

def rows : List (Nat × Pt × Pt × Pt) := [
   (134246, (true, 207088, 1, 71755992), (true, 165292, 1, 39206196), (true, 15098246, 7, 52805664960)),
   (134805, (true, 135531, 1, 5157504), (false, 38475, 1, 25342200), (false, 51600, 1, 28289700))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all onRow = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow3 = true := rfl
theorem count : rows.length = 2 := rfl

end Rank3Cert37

#print axioms Rank3Cert37.on_curve
#print axioms Rank3Cert37.indep
#print axioms Rank3Cert37.count

bsd/Rank3Cert38.lean

lean_all/bsd/Rank3Cert38.lean

/-
  bsd/Rank3Cert38.lean — T39 cycle 1564 (goal G840): rank ≥ 3 for 3 congruent curves n ∈ 136001–138000,
  three points each whose 2-descent images with E[2] span order 32 (tools/rank3scan.py). Lean 4 core, rfl.
-/
import bsd.Rank3Cert2

namespace Rank3Cert38
open Rank3Cert2

def rows : List (Nat × Pt × Pt × Pt) := [
   (136445, (false, 84100, 1, 31159050), (false, 9604, 13, 2259801894), (true, 7078201719121, 994, 18828061885435207119)),
   (136830, (false, 74420, 1, 31323500), (false, 72030, 1, 31222800), (true, 28478925049, 456, 209390141871757)),
   (137111, (true, 155025, 1, 28483260), (true, 526656601, 22, 11989909181355), (true, 25335713168585521, 942, 4032735148330781113777985))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all onRow = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow3 = true := rfl
theorem count : rows.length = 3 := rfl

end Rank3Cert38

#print axioms Rank3Cert38.on_curve
#print axioms Rank3Cert38.indep
#print axioms Rank3Cert38.count

bsd/Rank3Cert39.lean

lean_all/bsd/Rank3Cert39.lean

/-
  bsd/Rank3Cert39.lean — T39 cycle 1604 (goal G842): rank ≥ 3 for 2 congruent curves n ∈ 140001–142000,
  three points each whose 2-descent images with E[2] span order 32 (tools/rank3scan.py). Lean 4 core, rfl.
-/
import bsd.Rank3Cert2

namespace Rank3Cert39
open Rank3Cert2

def rows : List (Nat × Pt × Pt × Pt) := [
   (141141, (false, 4653, 1, 9622404), (true, 141267, 1, 2242044), (false, 13013, 1, 16032016)),
   (141661, (false, 24336, 1, 21770580), (true, 190333, 1, 55457688), (true, 6558721, 6, 10562037785))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all onRow = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow3 = true := rfl
theorem count : rows.length = 2 := rfl

end Rank3Cert39

#print axioms Rank3Cert39.on_curve
#print axioms Rank3Cert39.indep
#print axioms Rank3Cert39.count

bsd/Rank3Cert4.lean

lean_all/bsd/Rank3Cert4.lean

/-
  bsd/Rank3Cert4.lean — T39 cycle 473 (goal G190): rank ≥ 3 for 10 more congruent curves (n = 20366, 20774, 20909, 21414, 22134, 23359, 23405, 23709, 24190, 24414),
  three points whose 2-descent images with E[2] span order 32 (blocks 20001–25000, tools/rank3scan.py). Lean 4 core, rfl.
-/
import bsd.Rank3Cert2

namespace Rank3Cert4
open Rank3Cert2

def rows : List (Nat × Pt × Pt × Pt) := [
   (20366, (true, 62866, 1, 14912400), (true, 169362, 1, 69192816), (true, 2958850, 3, 5079833920)),
   (20774, (true, 28717, 1, 3359889), (false, 4212, 1, 1320228), (true, 194633, 2, 77647653)),
   (20909, (true, 94192, 1, 28186956), (true, 21141, 1, 454140), (true, 1599388, 3, 2008647298)),
   (21414, (true, 22188, 1, 865332), (false, 21218, 1, 421064), (false, 825600, 7, 588425760)),
   (22134, (false, 84, 1, 202860), (true, 46818, 1, 8926632), (true, 24684, 1, 1716660)),
   (23359, (true, 28400, 1, 2722140), (true, 124609, 1, 43207200), (true, 564592, 1, 423867444)),
   (23405, (true, 37845, 1, 5785500), (true, 4890605, 1, 10815308400), (true, 297724, 3, 114802982)),
   (23709, (true, 24843, 1, 1169532), (false, 20667, 1, 1670292), (false, 5440701, 17, 9715018320)),
   (24190, (false, 9610, 1, 2176200), (false, 7139, 1, 1952841), (true, 3616569, 2, 6875268147)),
   (24414, (true, 99372, 1, 30365244), (false, 9386, 1, 2183480), (false, 24336, 1, 304200))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all onRow = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow3 = true := rfl
theorem count : rows.length = 10 := rfl

end Rank3Cert4

#print axioms Rank3Cert4.on_curve
#print axioms Rank3Cert4.indep
#print axioms Rank3Cert4.count

bsd/Rank3Cert40.lean

lean_all/bsd/Rank3Cert40.lean

/-
  bsd/Rank3Cert40.lean — T39 cycle 1624 (goal G843): rank ≥ 3 for 4 congruent curves n ∈ 142001–144000,
  three points each whose 2-descent images with E[2] span order 32 (tools/rank3scan.py). Lean 4 core, rfl.
-/
import bsd.Rank3Cert2

namespace Rank3Cert40
open Rank3Cert2

def rows : List (Nat × Pt × Pt × Pt) := [
   (142071, (true, 142129, 1, 1530620), (false, 19343, 1, 19575116), (true, 768732, 1, 662393466)),
   (142222, (true, 142578, 1, 3802080), (true, 217328, 1, 76608120), (true, 643025, 2, 240362745)),
   (143021, (true, 4400892, 1, 9227448054), (true, 2948089, 4, 3191379315), (true, 550431100, 21, 12828751087130)),
   (143526, (false, 54150, 1, 30930480), (true, 251826, 1, 103838040), (true, 97695325522, 761, 16046430007233736))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all onRow = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow3 = true := rfl
theorem count : rows.length = 4 := rfl

end Rank3Cert40

#print axioms Rank3Cert40.on_curve
#print axioms Rank3Cert40.indep
#print axioms Rank3Cert40.count

bsd/Rank3Cert41.lean

lean_all/bsd/Rank3Cert41.lean

/-
  bsd/Rank3Cert41.lean — T39 cycle 1644 (goal G844): rank ≥ 3 for 1 congruent curve n ∈ 144001–146000,
  three points whose 2-descent images with E[2] span order 32 (tools/rank3scan.py). Lean 4 core, rfl.
-/
import bsd.Rank3Cert2

namespace Rank3Cert41
open Rank3Cert2

def rows : List (Nat × Pt × Pt × Pt) := [
   (145439, (true, 187489, 1, 51232560), (false, 19663, 4, 326294871), (false, 42677775, 23, 418199923680))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all onRow = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow3 = true := rfl
theorem count : rows.length = 1 := rfl

end Rank3Cert41

#print axioms Rank3Cert41.on_curve
#print axioms Rank3Cert41.indep
#print axioms Rank3Cert41.count

bsd/Rank3Cert42.lean

lean_all/bsd/Rank3Cert42.lean

/-
  bsd/Rank3Cert42.lean — T39 cycle 1664 (goal G845): rank ≥ 3 for 1 congruent curve n ∈ 146001–148000,
  three points whose 2-descent images with E[2] span order 32 (tools/rank3scan.py). Lean 4 core, rfl.
-/
import bsd.Rank3Cert2

namespace Rank3Cert42
open Rank3Cert2

def rows : List (Nat × Pt × Pt × Pt) := [
   (147934, (false, 5491, 1, 10954545), (true, 1211913, 2, 1164312507), (false, 911259, 5, 3421594461))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all onRow = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow3 = true := rfl
theorem count : rows.length = 1 := rfl

end Rank3Cert42

#print axioms Rank3Cert42.on_curve
#print axioms Rank3Cert42.indep
#print axioms Rank3Cert42.count

bsd/Rank3Cert43.lean

lean_all/bsd/Rank3Cert43.lean

/-
  bsd/Rank3Cert43.lean — T39 cycle 1684 (goal G846): rank ≥ 3 for 3 congruent curves n ∈ 148001–150000,
  three points each whose 2-descent images with E[2] span order 32 (tools/rank3scan.py). Lean 4 core, rfl.
-/
import bsd.Rank3Cert2

namespace Rank3Cert43
open Rank3Cert2

def rows : List (Nat × Pt × Pt × Pt) := [
   (148215, (true, 1311025, 1, 1491499900), (false, 136161, 1, 21604212), (true, 2175025, 1, 3200263100)),
   (148774, (true, 100020001, 24, 515858630705), (false, 181510850, 57, 6035620923640), (false, 1396169603, 1733, 16695181092364119)),
   (149495, (true, 331052545, 9, 6019420411900), (true, 32638369, 10, 165753377097), (true, 344328745, 17, 6338902267200))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all onRow = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow3 = true := rfl
theorem count : rows.length = 3 := rfl

end Rank3Cert43

#print axioms Rank3Cert43.on_curve
#print axioms Rank3Cert43.indep
#print axioms Rank3Cert43.count

bsd/Rank3Cert44.lean

lean_all/bsd/Rank3Cert44.lean

/-
  bsd/Rank3Cert44.lean — T39 cycle 1706 (goal G847): rank ≥ 3 for 2 congruent curves n ∈ 150001–152000,
  three points each whose 2-descent images with E[2] span order 32 (tools/rank3scan.py). Lean 4 core, rfl.
-/
import bsd.Rank3Cert2

namespace Rank3Cert44
open Rank3Cert2

def rows : List (Nat × Pt × Pt × Pt) := [
   (150854, (false, 1514051, 5, 4250145207), (false, 11309958, 23, 265666591344), (true, 39542515609, 98, 7857868248795165)),
   (150990, (true, 823690, 1, 734892200), (true, 151725, 1, 5810175), (true, 152460, 1, 8246700))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all onRow = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow3 = true := rfl
theorem count : rows.length = 2 := rfl

end Rank3Cert44

#print axioms Rank3Cert44.on_curve
#print axioms Rank3Cert44.indep
#print axioms Rank3Cert44.count

bsd/Rank3Cert45.lean

lean_all/bsd/Rank3Cert45.lean

/-
  bsd/Rank3Cert45.lean — T39 cycle 1726 (goal G848): rank ≥ 3 for 3 congruent curves n ∈ 152001–154000,
  three points each whose 2-descent images with E[2] span order 32 (tools/rank3scan.py). Lean 4 core, rfl.
-/
import bsd.Rank3Cert2

namespace Rank3Cert45
open Rank3Cert2

def rows : List (Nat × Pt × Pt × Pt) := [
   (153439, (true, 231361, 1, 83289960), (true, 633737, 1, 489492276), (true, 48384601225, 291, 10251978547254920)),
   (153454, (true, 160178, 1, 18379152), (true, 74842096, 1, 647467538760), (true, 11643633, 8, 21342706743)),
   (153510, (false, 1806, 1, 6523272), (true, 277440, 1, 121726800), (true, 189525, 1, 48388725))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all onRow = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow3 = true := rfl
theorem count : rows.length = 3 := rfl

end Rank3Cert45

#print axioms Rank3Cert45.on_curve
#print axioms Rank3Cert45.indep
#print axioms Rank3Cert45.count

bsd/Rank3Cert46.lean

lean_all/bsd/Rank3Cert46.lean

/-
  bsd/Rank3Cert46.lean — T39 cycle 1784 (goal G851): rank ≥ 3 for 2 congruent curves n ∈ 154001–156000,
  three points each whose 2-descent images with E[2] span order 32 (tools/rank3scan.py). Lean 4 core, rfl.
-/
import bsd.Rank3Cert2

namespace Rank3Cert46
open Rank3Cert2

def rows : List (Nat × Pt × Pt × Pt) := [
   (154622, (true, 314678, 1, 153742680), (true, 1694173, 3, 1257700535), (true, 608409829381225, 56232, 8931639427663243970485)),
   (155062, (true, 12610862, 1, 44780024520), (true, 128884689, 20, 1282617754137), (true, 138960521, 10, 1627857287931))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all onRow = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow3 = true := rfl
theorem count : rows.length = 2 := rfl

end Rank3Cert46

#print axioms Rank3Cert46.on_curve
#print axioms Rank3Cert46.indep
#print axioms Rank3Cert46.count

bsd/Rank3Cert47.lean

lean_all/bsd/Rank3Cert47.lean

/-
  bsd/Rank3Cert47.lean — T39 cycle 1886 (goal G855): rank ≥ 3 for 1 congruent curve n ∈ 156001–158000,
  three points whose 2-descent images with E[2] span order 32 (tools/rank3scan.py). Lean 4 core, rfl.
-/
import bsd.Rank3Cert2

namespace Rank3Cert47
open Rank3Cert2

def rows : List (Nat × Pt × Pt × Pt) := [
   (157591, (false, 86903, 1, 38754696), (true, 9697303, 1, 30193891728), (true, 260273689, 23, 3977776356840))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all onRow = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow3 = true := rfl
theorem count : rows.length = 1 := rfl

end Rank3Cert47

#print axioms Rank3Cert47.on_curve
#print axioms Rank3Cert47.indep
#print axioms Rank3Cert47.count

bsd/Rank3Cert48.lean

lean_all/bsd/Rank3Cert48.lean

/-
  bsd/Rank3Cert48.lean — T39 cycle 1914 (goal G856): rank ≥ 3 for 4 congruent curves n ∈ 158001–160000,
  three points whose 2-descent images with E[2] span order 32 (tools/rank3scan.py). Lean 4 core, rfl.
-/
import bsd.Rank3Cert2

namespace Rank3Cert48
open Rank3Cert2

def rows : List (Nat × Pt × Pt × Pt) := [
   (158070, (true, 178605, 1, 35139825), (true, 542124, 1, 381816468), (true, 332706, 1, 168864696)),
   (158774, (true, 447458, 1, 279838152), (true, 174262, 1, 29980896), (false, 52052, 1, 34222188)),
   (159310, (false, 890, 1, 4752600), (true, 94603529, 22, 533136948933), (true, 148669249, 16, 1743183840543)),
   (159999, (true, 160801, 1, 6432040), (true, 207088, 1, 59830372), (true, 255873, 1, 101004912))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all onRow = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow3 = true := rfl
theorem count : rows.length = 4 := rfl

end Rank3Cert48

#print axioms Rank3Cert48.on_curve
#print axioms Rank3Cert48.indep
#print axioms Rank3Cert48.count

bsd/Rank3Cert49.lean

lean_all/bsd/Rank3Cert49.lean

/-
  bsd/Rank3Cert49.lean — T39 cycle 1934 (goal G857): rank ≥ 3 for 3 congruent curves n ∈ 160001–162000,
  three points whose 2-descent images with E[2] span order 32 (tools/rank3scan.py). Lean 4 core, rfl.
-/
import bsd.Rank3Cert2

namespace Rank3Cert49
open Rank3Cert2

def rows : List (Nat × Pt × Pt × Pt) := [
   (160701, (false, 8349, 1, 14663880), (false, 1173, 1, 5503716), (true, 22917418225, 304, 2642289119699255)),
   (160805, (false, 129600, 1, 34270200), (true, 345550921, 46, 1119402235581), (true, 5679883225, 142, 351458345709525)),
   (160854, (true, 863532, 1, 788404716), (true, 702678, 1, 573385248), (true, 61934604, 1, 487414655100))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all onRow = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow3 = true := rfl
theorem count : rows.length = 3 := rfl

end Rank3Cert49

#print axioms Rank3Cert49.on_curve
#print axioms Rank3Cert49.indep
#print axioms Rank3Cert49.count

bsd/Rank3Cert5.lean

lean_all/bsd/Rank3Cert5.lean

/-
  bsd/Rank3Cert5.lean — T39 cycle 492 (goal G209): rank ≥ 3 for 6 more congruent curves (n = 26013, 26245, 26565, 27613, 28007, 28221),
  three points whose 2-descent images with E[2] span order 32 (blocks 25001–28600, tools/rank3scan.py). Lean 4 core, rfl.
-/
import bsd.Rank3Cert2

namespace Rank3Cert5
open Rank3Cert2

def rows : List (Nat × Pt × Pt × Pt) := [
   (26013, (true, 58812, 1, 12791610), (true, 89088, 1, 25431840), (false, 136367, 4, 145213145)),
   (26245, (false, 324, 1, 472374), (false, 13456, 1, 2613828), (true, 180625, 2, 62468625)),
   (26565, (false, 539, 1, 616616), (false, 644, 1, 673946), (true, 38115, 1, 5336100)),
   (27613, (true, 59059225, 26, 430604067915), (true, 102254013, 49, 787207193760), (true, 1394241919523401, 25580, 52055992790359971406149)),
   (28007, (true, 10063207, 1, 31922943360), (true, 27888112, 3, 147268862020), (true, 90447270682129, 35960, 788219049824960938617)),
   (28221, (true, 28227, 1, 97776), (true, 29808, 1, 1656828), (false, 4347069, 17, 14387932440))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all onRow = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow3 = true := rfl
theorem count : rows.length = 6 := rfl

end Rank3Cert5

#print axioms Rank3Cert5.on_curve
#print axioms Rank3Cert5.indep
#print axioms Rank3Cert5.count

bsd/Rank3Cert50.lean

lean_all/bsd/Rank3Cert50.lean

/-
  bsd/Rank3Cert50.lean — T39 cycle 1954 (goal G858): rank ≥ 3 for 2 congruent curves n ∈ 162001–164000,
  three points whose 2-descent images with E[2] span order 32 (tools/rank3scan.py). Lean 4 core, rfl.
-/
import bsd.Rank3Cert2

namespace Rank3Cert50
open Rank3Cert2

def rows : List (Nat × Pt × Pt × Pt) := [
   (162285, (true, 230640, 1, 78705900), (true, 1055725, 1, 1071848800), (true, 45769039969, 342, 8909924043120553)),
   (163590, (false, 1140, 1, 5523300), (true, 176890, 1, 28302400), (true, 193914, 1, 45849888))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all onRow = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow3 = true := rfl
theorem count : rows.length = 2 := rfl

end Rank3Cert50

#print axioms Rank3Cert50.on_curve
#print axioms Rank3Cert50.indep
#print axioms Rank3Cert50.count

bsd/Rank3Cert51.lean

lean_all/bsd/Rank3Cert51.lean

/-
  bsd/Rank3Cert51.lean — T39 cycle 2012 (goal G868): rank ≥ 3 for 1 congruent curve n ∈ 164001–166000,
  three points whose 2-descent images with E[2] span order 32 (tools/rank3scan.py). Lean 4 core, rfl.
-/
import bsd.Rank3Cert2

namespace Rank3Cert51
open Rank3Cert2

def rows : List (Nat × Pt × Pt × Pt) := [
   (165990, (false, 7290, 1, 14158800), (true, 288684, 1, 126903348), (true, 185394, 1, 35553672))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all onRow = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow3 = true := rfl
theorem count : rows.length = 1 := rfl

end Rank3Cert51

#print axioms Rank3Cert51.on_curve
#print axioms Rank3Cert51.indep
#print axioms Rank3Cert51.count

bsd/Rank3Cert52.lean

lean_all/bsd/Rank3Cert52.lean

/-
  bsd/Rank3Cert52.lean — T39 cycle 2054 (goal G871): rank ≥ 3 for 4 congruent curves n ∈ 170001–172000,
  three points whose 2-descent images with E[2] span order 32 (tools/rank3scan.py). Lean 4 core, rfl.
-/
import bsd.Rank3Cert2

namespace Rank3Cert52
open Rank3Cert2

def rows : List (Nat × Pt × Pt × Pt) := [
   (170430, (false, 2850, 1, 9097200), (true, 320346, 1, 153523656), (true, 622725, 1, 472648275)),
   (170526, (false, 1172, 1, 5837732), (false, 480534, 5, 2936405736), (true, 26429881, 12, 50254174675)),
   (170877, (true, 174748, 1, 15290450), (false, 112167, 2, 225812565), (false, 7882923, 7, 7924453020)),
   (171390, (true, 789525, 1, 684805275), (false, 121104, 1, 42204744), (false, 167334, 1, 15162264))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all onRow = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow3 = true := rfl
theorem count : rows.length = 4 := rfl

end Rank3Cert52

#print axioms Rank3Cert52.on_curve
#print axioms Rank3Cert52.indep
#print axioms Rank3Cert52.count

bsd/Rank3Cert53.lean

lean_all/bsd/Rank3Cert53.lean

/-
  bsd/Rank3Cert53.lean — T39 cycle 2098 (goal G873): rank ≥ 3 for 2 congruent curves n ∈ 174001–176000,
  three points whose 2-descent images with E[2] span order 32 (tools/rank3scan.py). Lean 4 core, rfl.
-/
import bsd.Rank3Cert2

namespace Rank3Cert53
open Rank3Cert2

def rows : List (Nat × Pt × Pt × Pt) := [
   (174709, (true, 796348, 1, 693334698), (false, 331091099, 45, 2268514203524), (true, 1843296255904059961, 194036, 2502591390898451722338120045)),
   (175630, (false, 3136, 1, 9833712), (true, 365170, 1, 193471200), (true, 324626, 1, 155551824))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all onRow = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow3 = true := rfl
theorem count : rows.length = 2 := rfl

end Rank3Cert53

#print axioms Rank3Cert53.on_curve
#print axioms Rank3Cert53.indep
#print axioms Rank3Cert53.count

bsd/Rank3Cert54.lean

lean_all/bsd/Rank3Cert54.lean

/-
  bsd/Rank3Cert54.lean — T39 cycle 2120 (goal G874): rank ≥ 3 for 2 congruent curves n ∈ 176001–178000,
  three points whose 2-descent images with E[2] span order 32 (tools/rank3scan.py). Lean 4 core, rfl.
-/
import bsd.Rank3Cert2

namespace Rank3Cert54
open Rank3Cert2

def rows : List (Nat × Pt × Pt × Pt) := [
   (176470, (false, 176400, 1, 2087400), (true, 2910845, 1, 4957115625), (false, 13037150, 9, 21162874600)),
   (177926, (false, 170471, 2, 285295395), (true, 9801324, 7, 14020500780), (false, 1059247, 4, 2719566297))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all onRow = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow3 = true := rfl
theorem count : rows.length = 2 := rfl

end Rank3Cert54

#print axioms Rank3Cert54.on_curve
#print axioms Rank3Cert54.indep
#print axioms Rank3Cert54.count

bsd/Rank3Cert55.lean

lean_all/bsd/Rank3Cert55.lean

/-
  bsd/Rank3Cert55.lean — T39 cycle 2140 (goal G875): rank ≥ 3 for 1 congruent curve n ∈ 178001–180000,
  three points whose 2-descent images with E[2] span order 32 (tools/rank3scan.py). Lean 4 core, rfl.
-/
import bsd.Rank3Cert2

namespace Rank3Cert55
open Rank3Cert2

def rows : List (Nat × Pt × Pt × Pt) := [
   (179158, (true, 753750, 1, 635642400), (false, 22743, 2, 108019275), (true, 47214941004, 445, 6769477805274492))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all onRow = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow3 = true := rfl
theorem count : rows.length = 1 := rfl

end Rank3Cert55

#print axioms Rank3Cert55.on_curve
#print axioms Rank3Cert55.indep
#print axioms Rank3Cert55.count

bsd/Rank3Cert56.lean

lean_all/bsd/Rank3Cert56.lean

/-
  bsd/Rank3Cert56.lean — T39 cycle 2162 (goal G876): rank ≥ 3 for 1 congruent curve n ∈ 180001–182000,
  three points whose 2-descent images with E[2] span order 32 (tools/rank3scan.py). Lean 4 core, rfl.
-/
import bsd.Rank3Cert2

namespace Rank3Cert56
open Rank3Cert2

def rows : List (Nat × Pt × Pt × Pt) := [
   (181589, (false, 387, 1, 3572268), (true, 1047320892, 31, 33419944469046), (false, 1992135347, 279, 624598284563468))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all onRow = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow3 = true := rfl
theorem count : rows.length = 1 := rfl

end Rank3Cert56

#print axioms Rank3Cert56.on_curve
#print axioms Rank3Cert56.indep
#print axioms Rank3Cert56.count

bsd/Rank3Cert57.lean

lean_all/bsd/Rank3Cert57.lean

/-
  bsd/Rank3Cert57.lean — T39 cycle 2184 (goal G877): rank ≥ 3 for 1 congruent curve n ∈ 182001–184000,
  three points whose 2-descent images with E[2] span order 32 (tools/rank3scan.py). Lean 4 core, rfl.
-/
import bsd.Rank3Cert2

namespace Rank3Cert57
open Rank3Cert2

def rows : List (Nat × Pt × Pt × Pt) := [
   (182005, (true, 356445, 1, 182975100), (false, 142400, 1, 42773400), (false, 4075844, 5, 4083481446))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all onRow = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow3 = true := rfl
theorem count : rows.length = 1 := rfl

end Rank3Cert57

#print axioms Rank3Cert57.on_curve
#print axioms Rank3Cert57.indep
#print axioms Rank3Cert57.count

bsd/Rank3Cert58.lean

lean_all/bsd/Rank3Cert58.lean

/-
  bsd/Rank3Cert58.lean — T39 cycle 2224 (goal G879): rank ≥ 3 for 2 congruent curves n ∈ 186001–188000,
  three points whose 2-descent images with E[2] span order 32 (tools/rank3scan.py). Lean 4 core, rfl.
-/
import bsd.Rank3Cert2

namespace Rank3Cert58
open Rank3Cert2

def rows : List (Nat × Pt × Pt × Pt) := [
   (186295, (false, 144400, 1, 44727900), (true, 7296881, 1, 19704449604), (false, 1213055, 9, 16566037300)),
   (187726, (true, 252333, 1, 84699777), (true, 216293, 1, 49963683), (false, 204687, 4, 1355747337))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all onRow = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow3 = true := rfl
theorem count : rows.length = 2 := rfl

end Rank3Cert58

#print axioms Rank3Cert58.on_curve
#print axioms Rank3Cert58.indep
#print axioms Rank3Cert58.count

bsd/Rank3Cert6.lean

lean_all/bsd/Rank3Cert6.lean

/-
  bsd/Rank3Cert6.lean — T39 cycle 501 (goal G217): rank ≥ 3 for 4 more congruent curves (n = 28806, 29055, 29294, 29614),
  three points whose 2-descent images with E[2] span order 32 (blocks 28601–30000, tools/rank3scan.py). Lean 4 core, rfl.
-/
import bsd.Rank3Cert2

namespace Rank3Cert6
open Rank3Cert2

def rows : List (Nat × Pt × Pt × Pt) := [
   (28806, (false, 6, 1, 70560), (true, 223494, 1, 104775840), (false, 7738178, 17, 8540100296)),
   (29055, (false, 24336, 1, 2476188), (false, 20631, 1, 2938572), (false, 47775, 2, 23157225)),
   (29294, (true, 169362, 1, 68648064), (true, 1169344, 1, 1264087440), (true, 16409118367489, 20262, 45220576687549079455)),
   (29614, (false, 27508, 1, 1819116), (true, 203814, 1, 91036920), (true, 811801, 4, 593916675))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all onRow = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow3 = true := rfl
theorem count : rows.length = 4 := rfl

end Rank3Cert6

#print axioms Rank3Cert6.on_curve
#print axioms Rank3Cert6.indep
#print axioms Rank3Cert6.count

bsd/Rank3Cert7.lean

lean_all/bsd/Rank3Cert7.lean

/-
  bsd/Rank3Cert7.lean — T39 cycle 517 (goal G233): rank ≥ 3 for 5 more congruent curves (n = 30270, 32039, 32318, 32599, 32893),
  three points whose 2-descent images with E[2] span order 32 (blocks 30001–33000, tools/rank3scan.py). Lean 4 core, rfl.
-/
import bsd.Rank3Cert2

namespace Rank3Cert7
open Rank3Cert2

def rows : List (Nat × Pt × Pt × Pt) := [
   (30270, (true, 32805, 1, 2290275), (false, 1470, 1, 1159200), (true, 383070, 1, 236350800)),
   (32039, (true, 32761, 1, 1238040), (true, 59248, 1, 12131028), (true, 641447, 1, 513096144)),
   (32318, (true, 1322893, 1, 1521098865), (true, 319225, 2, 164911635), (true, 32096350, 1, 181837415760)),
   (32599, (true, 10843849, 1, 35708633400), (true, 27001072, 27, 66605031836), (false, 1030225, 61, 123115951560)),
   (32893, (true, 35557, 1, 2546340), (true, 451612, 3, 229193090), (true, 56406784098025, 40854, 97262725645790177005))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all onRow = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow3 = true := rfl
theorem count : rows.length = 5 := rfl

end Rank3Cert7

#print axioms Rank3Cert7.on_curve
#print axioms Rank3Cert7.indep
#print axioms Rank3Cert7.count

bsd/Rank3Cert8.lean

lean_all/bsd/Rank3Cert8.lean

/-
  bsd/Rank3Cert8.lean — T39 cycle 530 (goal G244): rank ≥ 3 for 2 more congruent curves (n = 33117, 33286),
  three points whose 2-descent images with E[2] span order 32 (blocks 33001–35000, tools/rank3scan.py). Lean 4 core, rfl.
-/
import bsd.Rank3Cert2

namespace Rank3Cert8
open Rank3Cert2

def rows : List (Nat × Pt × Pt × Pt) := [
   (33117, (true, 53067, 1, 9552060), (true, 621628, 3, 430101910), (false, 636291, 5, 422538948)),
   (33286, (false, 1602, 1, 1330728), (false, 1683, 1, 1363791), (true, 17937414, 1, 75969453120))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all onRow = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow3 = true := rfl
theorem count : rows.length = 2 := rfl

end Rank3Cert8

#print axioms Rank3Cert8.on_curve
#print axioms Rank3Cert8.indep
#print axioms Rank3Cert8.count

bsd/Rank3Cert9.lean

lean_all/bsd/Rank3Cert9.lean

/-
  bsd/Rank3Cert9.lean — T39 cycle 541 (goal G255): rank ≥ 3 for 5 more congruent curves (n = 35269, 35286, 35719, 36366, 36519),
  three points whose 2-descent images with E[2] span order 32 (blocks 35001–37000, tools/rank3scan.py). Lean 4 core, rfl.
-/
import bsd.Rank3Cert2

namespace Rank3Cert9
open Rank3Cert2

def rows : List (Nat × Pt × Pt × Pt) := [
   (35269, (true, 229957, 1, 108968496), (true, 8160773569, 318, 663088354164665), (true, 958736412513961, 21588, 29681442157646185021205)),
   (35286, (false, 14400, 1, 3865680), (false, 192027, 13, 2611830573), (false, 41896496883, 1091, 603911412838431)),
   (35719, (false, 33856, 1, 2094840), (true, 11013872, 13, 30572901156), (true, 4866175695481, 11249, 3977018546355334920)),
   (36366, (false, 2166, 1, 1689480), (false, 5684, 1, 2708020), (false, 4598, 1, 2446136)),
   (36519, (true, 38332, 1, 2280754), (true, 82908, 1, 21431718), (true, 5129152, 3, 11592440776))]

set_option maxRecDepth 100000 in
theorem on_curve : rows.all onRow = true := rfl
set_option maxRecDepth 100000 in
theorem indep : rows.all indepRow3 = true := rfl
theorem count : rows.length = 5 := rfl

end Rank3Cert9

#print axioms Rank3Cert9.on_curve
#print axioms Rank3Cert9.indep
#print axioms Rank3Cert9.count

bsd/Rank3Count.lean

lean_all/bsd/Rank3Count.lean

/-
  bsd/Rank3Count.lean — T39 (goal G289h): the 32 points S + T (S a subset sum of P₁, P₂, P₃ of bsd/Rank3Lower,
  T ∈ E[2] ∪ {O}) have 32 pairwise different descent classes.  With Rank3Final.rank_le3 every class of a point lies
  in this set modulo the classes of doubles, so E_42486(ℚ)/2E_42486(ℚ) has exactly 32 = 2^(3+2) elements.
    clsA, clsB          the two class components of S + T (Rank3Lower.clsR)
    fld5_self           the field-wise Boolean equality is reflexive
    distChk             all 2¹² pairs of indices checked (rfl)
    classes_distinct    equal classes ⇒ equal subset and equal T
  Lean 4 core plus bsd/Rank3Lower.
-/
import bsd.Rank3Lower

namespace Rank3Count
open DblCls Rank3Lower

def clsA (c : Dep4.E4) (s t : Bool) : B5S.B5 := (Dep4.sum4 c B1 B2 B3 z5).xor ⟨s, t, t, t, t⟩
def clsB (c : Dep4.E4) (s t : Bool) : B5S.B5 := (Dep4.sum4 c B1' B2' B3' z5).xor ⟨s, Bool.xor s t, s, s, s⟩

theorem fld5_self : ∀ β : B5S.B5, VLin.fld5 β β = true
  | ⟨s, a, b, c, d⟩ => by cases s <;> cases a <;> cases b <;> cases c <;> cases d <;> rfl

def sameIdx (c : Dep4.E4) (s t : Bool) (c' : Dep4.E4) (s' t' : Bool) : Bool :=
  VSet.beqB c.e1 c'.e1 && VSet.beqB c.e2 c'.e2 && VSet.beqB c.e3 c'.e3 && VSet.beqB s s' && VSet.beqB t t'

def prem (c : Dep4.E4) (s t : Bool) (c' : Dep4.E4) (s' t' : Bool) : Bool :=
  !c.e4 && !c'.e4 && VLin.fld5 (clsA c s t) (clsA c' s' t') && VLin.fld5 (clsB c s t) (clsB c' s' t')

def distChk : Bool :=
  VSet.all2 fun e1 => VSet.all2 fun e2 => VSet.all2 fun e3 => VSet.all2 fun e4 =>
  VSet.all2 fun s => VSet.all2 fun t =>
  VSet.all2 fun f1 => VSet.all2 fun f2 => VSet.all2 fun f3 => VSet.all2 fun f4 =>
  VSet.all2 fun s' => VSet.all2 fun t' =>
    !prem ⟨e1, e2, e3, e4⟩ s t ⟨f1, f2, f3, f4⟩ s' t' || sameIdx ⟨e1, e2, e3, e4⟩ s t ⟨f1, f2, f3, f4⟩ s' t'

theorem distChk_true : distChk = true := rfl

theorem imp_of : ∀ (p q : Bool), (!p || q) = true → p = true → q = true
  | true, true, _, _ => rfl
  | true, false, h, _ => h
  | false, _, _, hp => Bool.noConfusion hp

theorem and4 (a b c d : Bool) (ha : a = true) (hb : b = true) (hc : c = true) (hd : d = true) :
    (a && b && c && d) = true := by
  subst ha; subst hb; subst hc; subst hd; rfl

theorem dist_app : ∀ (c : Dep4.E4) (s t : Bool) (c' : Dep4.E4) (s' t' : Bool),
    (!prem c s t c' s' t' || sameIdx c s t c' s' t') = true
  | ⟨e1, e2, e3, e4⟩, s, t, ⟨f1, f2, f3, f4⟩, s', t' =>
    Dep4.all2_app _ (Dep4.all2_app _ (Dep4.all2_app _ (Dep4.all2_app _ (Dep4.all2_app _ (Dep4.all2_app _
      (Dep4.all2_app _ (Dep4.all2_app _ (Dep4.all2_app _ (Dep4.all2_app _ (Dep4.all2_app _ (Dep4.all2_app _
        distChk_true e1) e2) e3) e4) s) t) f1) f2) f3) f4) s') t'

/-- **Different (S, T) have different classes.** -/
theorem classes_distinct (c c' : Dep4.E4) (s t s' t' : Bool) (h4 : c.e4 = false) (h4' : c'.e4 = false)
    (ea : clsA c s t = clsA c' s' t') (eb : clsB c s t = clsB c' s' t') :
    c.e1 = c'.e1 ∧ c.e2 = c'.e2 ∧ c.e3 = c'.e3 ∧ s = s' ∧ t = t' := by
  have hp : prem c s t c' s' t' = true := by
    unfold prem
    rw [h4, h4', ea, eb]
    exact and4 _ _ _ _ rfl rfl (fld5_self _) (fld5_self _)
  have q := imp_of _ _ (dist_app c s t c' s' t') hp
  unfold sameIdx at q
  have q4 := Dep4.and_left q
  have q3 := Dep4.and_left q4
  have q2 := Dep4.and_left q3
  exact ⟨Dep4.beqB_eq (Dep4.and_left q2), Dep4.beqB_eq (Dep4.and_right q2), Dep4.beqB_eq (Dep4.and_right q3),
    Dep4.beqB_eq (Dep4.and_right q4), Dep4.beqB_eq (Dep4.and_right q)⟩

end Rank3Count

#print axioms Rank3Count.fld5_self
#print axioms Rank3Count.distChk_true
#print axioms Rank3Count.imp_of
#print axioms Rank3Count.and4
#print axioms Rank3Count.dist_app
#print axioms Rank3Count.classes_distinct

bsd/Rank3Final.lean

lean_all/bsd/Rank3Final.lean

/-
  bsd/Rank3Final.lean — T39 (goal G289e-7d): rank E_42486(ℚ) ≤ 3 in finite form.  For any four rational points
  P₁, …, P₄ of E : y² = x³ − 42486²x there are a nonempty subset S ⊆ {1, 2, 3, 4} and a point T ∈ E[2] ∪ {O} with
      Σ_{i∈S} Pᵢ + T = O   or   x(Σ_{i∈S} Pᵢ + T) = x(2Q) for a rational point Q,
  the sums being the chord–tangent sums of bsd/PtAdd.  So E(ℚ)/2E(ℚ) has at most 2⁵ elements (with E[2] of order 4),
  i.e. rank ≤ 3; with bsd/Rank3Cert12 (three independent points) the rank is 3.
  Proof: every point has a class in V (O, E[2], or Assemble3.image_in_V); classes are additive (PtCls2.cls_add);
  four classes in V are dependent modulo δ(E[2]) (VLin.dep_V); a point of class 0 is O or has x = x(2Q) (Kernel2).
    sel, sumP, torP          subset sums and the points of E[2]
    on_sumP, cls_sumP        on the curve, class = subset sum of classes
    cls_exists               every point has a class in V
    on_torP, cls_torP        T_{s,t} and its class s·δ(T0) ⊕ t·δ(T1)
    kernel_pt                class 0 ⇒ O or x = x(2Q)
    rank_le3                 the statement above
  Lean 4 core plus bsd/PtCls2, bsd/VLin, bsd/Assemble3, bsd/Kernel2.
-/
import bsd.PtCls2
import bsd.VLin
import bsd.Assemble3

namespace Rank3Final
open DetChain LibRing LibRat QModel QCls B5S TransStep TransStep2 StepFull PtAdd QDec DblCls PtCls PtCls2 Dep4

def sel (e : Bool) (P : Pt) : Pt :=
  match e with
  | true => P
  | false => Pt.O

def sumP (c : E4) (P1 P2 P3 P4 : Pt) : Pt :=
  add (add (sel c.e1 P1) (sel c.e2 P2)) (add (sel c.e3 P3) (sel c.e4 P4))

theorem on_sel (e : Bool) (P : Pt) (o : OnPt P) : OnPt (sel e P) := by
  cases e with
  | true => exact o
  | false => exact trivial

theorem cls_sel (e : Bool) (P : Pt) (β β' : B5) (c : ClsPt P β β') : ClsPt (sel e P) (selB e β) (selB e β') := by
  cases e with
  | true => exact c
  | false => exact ⟨rfl, rfl⟩

theorem on_sumP (c : E4) (P1 P2 P3 P4 : Pt) (o1 : OnPt P1) (o2 : OnPt P2) (o3 : OnPt P3) (o4 : OnPt P4) :
    OnPt (sumP c P1 P2 P3 P4) :=
  on_add _ _ (on_add _ _ (on_sel _ _ o1) (on_sel _ _ o2)) (on_add _ _ (on_sel _ _ o3) (on_sel _ _ o4))

theorem cls_sumP (c : E4) (P1 P2 P3 P4 : Pt) (o1 : OnPt P1) (o2 : OnPt P2) (o3 : OnPt P3) (o4 : OnPt P4)
    (β1 β2 β3 β4 β1' β2' β3' β4' : B5) (c1 : ClsPt P1 β1 β1') (c2 : ClsPt P2 β2 β2') (c3 : ClsPt P3 β3 β3')
    (c4 : ClsPt P4 β4 β4') :
    ClsPt (sumP c P1 P2 P3 P4) (sum4 c β1 β2 β3 β4) (sum4 c β1' β2' β3' β4') :=
  cls_add _ _ _ _ _ _ (on_add _ _ (on_sel _ _ o1) (on_sel _ _ o2)) (on_add _ _ (on_sel _ _ o3) (on_sel _ _ o4))
    (cls_add _ _ _ _ _ _ (on_sel _ _ o1) (on_sel _ _ o2) (cls_sel _ _ _ _ c1) (cls_sel _ _ _ _ c2))
    (cls_add _ _ _ _ _ _ (on_sel _ _ o3) (on_sel _ _ o4) (cls_sel _ _ _ _ c3) (cls_sel _ _ _ _ c4))

/-- every point has a class in V -/
theorem cls_exists : ∀ P : Pt, OnPt P →
    ∃ β β' : B5, ClsPt P β β' ∧ VSet.inV β.s β.a β.b β.c β.d β'.s β'.a β'.b β'.c β'.d = true
  | Pt.O, _ => ⟨z5, z5, ⟨rfl, rfl⟩, rfl⟩
  | Pt.A x y, o => by
    cases Int.decEq x.num 0 with
    | isTrue h0 =>
      exact ⟨⟨true, false, false, false, false⟩, ⟨true, true, true, true, true⟩,
        Or.inr (Or.inl ⟨zero_eq _ h0, rfl, rfl⟩), rfl⟩
    | isFalse h0 =>
      cases Int.decEq (sub ratR x nQ).num 0 with
      | isTrue h1 =>
        exact ⟨⟨false, true, true, true, true⟩, ⟨false, true, false, false, false⟩,
          Or.inr (Or.inr (Or.inl ⟨LibVieta.sub_zero_eq ratR _ _ (zero_eq _ h1), rfl, rfl⟩)), rfl⟩
      | isFalse h1 =>
        cases Int.decEq (sub ratR x (negQ nQ)).num 0 with
        | isTrue h2 =>
          exact ⟨⟨true, true, true, true, true⟩, ⟨true, false, true, true, true⟩,
            Or.inr (Or.inr (Or.inr ⟨LibVieta.sub_zero_eq ratR _ _ (zero_eq _ h2), rfl, rfl⟩)), rfl⟩
        | isFalse h2 =>
          have nd : ND x := ⟨ne_of_num h0, ne_of_num h1, ne_of_num h2⟩
          obtain ⟨β1, β2, hc, hv⟩ := Assemble3.image_in_V x y o nd
          exact ⟨β1, β2, Or.inl ⟨nd, hc⟩, hv⟩

/-- the points of E[2] ∪ {O}: T_{s,t} = s·T0 + t·T1 -/
def torP (s t : Bool) : Pt :=
  match s, t with
  | false, false => Pt.O
  | true, false => Pt.A zeroQ ratR.zero
  | false, true => Pt.A nQ ratR.zero
  | true, true => Pt.A (negQ nQ) ratR.zero

section gen
variable {R : Type} (K : CRing R)
theorem root1 (e1 e2 e3 : R) : K.mul K.zero K.zero = DescentHomFull.f K e1 e2 e3 e1 := by
  unfold DescentHomFull.f LibRing.sub; lib_ring
theorem root2 (e1 e2 e3 : R) : K.mul K.zero K.zero = DescentHomFull.f K e1 e2 e3 e2 := by
  unfold DescentHomFull.f LibRing.sub; lib_ring
theorem root3 (e1 e2 e3 : R) : K.mul K.zero K.zero = DescentHomFull.f K e1 e2 e3 e3 := by
  unfold DescentHomFull.f LibRing.sub; lib_ring
end gen

theorem on_torP : ∀ s t : Bool, OnPt (torP s t)
  | false, false => trivial
  | true, false => root1 ratR zeroQ nQ (negQ nQ)
  | false, true => root2 ratR zeroQ nQ (negQ nQ)
  | true, true => root3 ratR zeroQ nQ (negQ nQ)

theorem cls_torP : ∀ s t : Bool, ClsPt (torP s t) ⟨s, t, t, t, t⟩ ⟨s, Bool.xor s t, s, s, s⟩
  | false, false => ⟨rfl, rfl⟩
  | true, false => Or.inr (Or.inl ⟨rfl, rfl, rfl⟩)
  | false, true => Or.inr (Or.inr (Or.inl ⟨rfl, rfl, rfl⟩))
  | true, true => Or.inr (Or.inr (Or.inr ⟨rfl, rfl, rfl⟩))

theorem tor_ne_z5 (x : Q) (h : TorCls x z5 z5) : False := by
  rcases h with ⟨_, e, _⟩ | ⟨_, e, _⟩ | ⟨_, e, _⟩
  · exact Bool.noConfusion (congrArg B5.s e)
  · exact Bool.noConfusion (congrArg B5.a e)
  · exact Bool.noConfusion (congrArg B5.s e)

/-- **A point of class 0 is O or has the abscissa of a double.** -/
theorem kernel_pt (R : Pt) (o : OnPt R) (c : ClsPt R z5 z5) :
    R = Pt.O ∨ ∃ x y : Q, R = Pt.A x y ∧ ∃ xq yq w : Q, OnE xq yq ∧
      ratR.mul (TrianglePoint.two ratR yq) w = ratR.one ∧ DoublingGen.x2 ratR zeroQ nQ (negQ nQ) xq w = x := by
  cases R with
  | O => exact Or.inl rfl
  | A x y =>
    right
    refine ⟨x, y, rfl, ?_⟩
    rcases c with ⟨nd, hc⟩ | t
    · obtain ⟨s, r, _, _, ex, exn⟩ := hc
      exact Kernel2.kernel_Q x y s r o (ex.trans (cq0 _)) (exn.trans (cq0 _)) nd.1 nd.2.1
    · exact (tor_ne_z5 x t).elim

/-- **rank E_42486(ℚ) ≤ 3, finite form.** -/
theorem rank_le3 (P1 P2 P3 P4 : Pt) (o1 : OnPt P1) (o2 : OnPt P2) (o3 : OnPt P3) (o4 : OnPt P4) :
    ∃ c : E4, nz c = true ∧ ∃ s t : Bool,
      add (sumP c P1 P2 P3 P4) (torP s t) = Pt.O ∨
      ∃ x y : Q, add (sumP c P1 P2 P3 P4) (torP s t) = Pt.A x y ∧ ∃ xq yq w : Q, OnE xq yq ∧
        ratR.mul (TrianglePoint.two ratR yq) w = ratR.one ∧ DoublingGen.x2 ratR zeroQ nQ (negQ nQ) xq w = x := by
  obtain ⟨β1, β1', c1, v1⟩ := cls_exists P1 o1
  obtain ⟨β2, β2', c2, v2⟩ := cls_exists P2 o2
  obtain ⟨β3, β3', c3, v3⟩ := cls_exists P3 o3
  obtain ⟨β4, β4', c4, v4⟩ := cls_exists P4 o4
  obtain ⟨c, hn, s, t, e1, e2⟩ := VLin.dep_V β1 β2 β3 β4 β1' β2' β3' β4' v1 v2 v3 v4
  refine ⟨c, hn, s, t, ?_⟩
  have cS := cls_sumP c P1 P2 P3 P4 o1 o2 o3 o4 β1 β2 β3 β4 β1' β2' β3' β4' c1 c2 c3 c4
  rw [e1, e2] at cS
  have cR := cls_add _ _ _ _ _ _ (on_sumP c P1 P2 P3 P4 o1 o2 o3 o4) (on_torP s t) cS (cls_torP s t)
  rw [xor_self, xor_self] at cR
  exact kernel_pt _ (on_add _ _ (on_sumP c P1 P2 P3 P4 o1 o2 o3 o4) (on_torP s t)) cR

end Rank3Final

#print axioms Rank3Final.on_sel
#print axioms Rank3Final.cls_sel
#print axioms Rank3Final.on_sumP
#print axioms Rank3Final.cls_sumP
#print axioms Rank3Final.cls_exists
#print axioms Rank3Final.root1
#print axioms Rank3Final.root2
#print axioms Rank3Final.root3
#print axioms Rank3Final.on_torP
#print axioms Rank3Final.cls_torP
#print axioms Rank3Final.tor_ne_z5
#print axioms Rank3Final.kernel_pt
#print axioms Rank3Final.rank_le3

bsd/Rank3FinalN.lean

lean_all/bsd/Rank3FinalN.lean

/-
  bsd/Rank3FinalN.lean — T39 (goal G289f-4h, assembly): rank E_n(ℚ) ≤ 3 in finite form for a rational parameter n,
  from two curve-specific inputs (bsd/Rank3Final is the case n = 42486):
    img   every point away from E[2] has a class (β₁, β₂) with inV β₁ β₂ = true      (the 2-Selmer bound)
    dep   any four classes in V are dependent modulo the classes of E[2]              (finite linear algebra)
  For any four rational points P₁, …, P₄ there are a nonempty S ⊆ {1, 2, 3, 4} and T ∈ E[2] ∪ {O} with
      Σ_{i∈S} Pᵢ + T = O   or   x(Σ_{i∈S} Pᵢ + T) = x(2Q) for a rational point Q.
    Hyp                        chkL, posL, TorRel, n ≠ 0, ±n not squares
    selPN, sumPN, selC, sum4C  subset sums of points and of classes
    torPN, tcA, tcB            the points of E[2] ∪ {O} and their classes
    cls_sumPN, cls_torPN, cls_existsN, kernel_ptN, rank_le3N
  Lean 4 core plus bsd/PtClsN2, bsd/Rank3Final.
-/
import bsd.PtClsN2
import bsd.Rank3Final

namespace Rank3FinalN
open DetChain LibRing LibRat QModel QCls B5S SqUniqueL SqClassL QModelN TransStepL StepN PtAdd PtAddN DblClsN
  PtClsN PtClsN2 TorClsN0 Dep4

structure Hyp (n : Q) (ps : List Nat) (D : TorData n ps) : Prop where
  hc : chkL ps = true
  hp : posL ps = true
  R : TorRel n ps D
  hn : n ≠ zeroQ
  h0n : ∀ T : Q, sub ratR zeroQ n ≠ ratR.mul T T
  hn0 : ∀ T : Q, sub ratR n zeroQ ≠ ratR.mul T T
  hm0 : ∀ T : Q, sub ratR (negQ n) zeroQ ≠ ratR.mul T T

def selPN (e : Bool) (P : Pt) : Pt :=
  match e with
  | true => P
  | false => Pt.O

def selC (ps : List Nat) (e : Bool) (β : CL) : CL :=
  match e with
  | true => β
  | false => zc ps

def sumPN (n : Q) (c : E4) (P1 P2 P3 P4 : Pt) : Pt :=
  addN n (addN n (selPN c.e1 P1) (selPN c.e2 P2)) (addN n (selPN c.e3 P3) (selPN c.e4 P4))

def sum4C (ps : List Nat) (c : E4) (β1 β2 β3 β4 : CL) : CL :=
  cxor (cxor (selC ps c.e1 β1) (selC ps c.e2 β2)) (cxor (selC ps c.e3 β3) (selC ps c.e4 β4))

theorem on_selPN (n : Q) (e : Bool) (P : Pt) (o : OnPtN n P) : OnPtN n (selPN e P) := by
  cases e with
  | true => exact o
  | false => exact trivial

theorem cls_selC (n : Q) (ps : List Nat) (D : TorData n ps) (e : Bool) (P : Pt) (β β' : CL)
    (c : ClsPtN n ps D P β β') : ClsPtN n ps D (selPN e P) (selC ps e β) (selC ps e β') := by
  cases e with
  | true => exact c
  | false => exact ⟨rfl, rfl⟩

theorem on_sumPN (n : Q) (c : E4) (P1 P2 P3 P4 : Pt) (o1 : OnPtN n P1) (o2 : OnPtN n P2) (o3 : OnPtN n P3)
    (o4 : OnPtN n P4) : OnPtN n (sumPN n c P1 P2 P3 P4) :=
  on_addN n _ _ (on_addN n _ _ (on_selPN n _ _ o1) (on_selPN n _ _ o2))
    (on_addN n _ _ (on_selPN n _ _ o3) (on_selPN n _ _ o4))

theorem cls_sumPN (n : Q) (ps : List Nat) (D : TorData n ps) (H : Hyp n ps D) (c : E4) (P1 P2 P3 P4 : Pt)
    (o1 : OnPtN n P1) (o2 : OnPtN n P2) (o3 : OnPtN n P3) (o4 : OnPtN n P4)
    (β1 β2 β3 β4 β1' β2' β3' β4' : CL) (c1 : ClsPtN n ps D P1 β1 β1') (c2 : ClsPtN n ps D P2 β2 β2')
    (c3 : ClsPtN n ps D P3 β3 β3') (c4 : ClsPtN n ps D P4 β4 β4') :
    ClsPtN n ps D (sumPN n c P1 P2 P3 P4) (sum4C ps c β1 β2 β3 β4) (sum4C ps c β1' β2' β3' β4') :=
  cls_addN n ps H.hc H.hp D H.R H.hn H.h0n H.hn0 H.hm0 _ _ _ _ _ _
    (on_addN n _ _ (on_selPN n _ _ o1) (on_selPN n _ _ o2)) (on_addN n _ _ (on_selPN n _ _ o3) (on_selPN n _ _ o4))
    (cls_addN n ps H.hc H.hp D H.R H.hn H.h0n H.hn0 H.hm0 _ _ _ _ _ _ (on_selPN n _ _ o1) (on_selPN n _ _ o2)
      (cls_selC n ps D _ _ _ _ c1) (cls_selC n ps D _ _ _ _ c2))
    (cls_addN n ps H.hc H.hp D H.R H.hn H.h0n H.hn0 H.hm0 _ _ _ _ _ _ (on_selPN n _ _ o3) (on_selPN n _ _ o4)
      (cls_selC n ps D _ _ _ _ c3) (cls_selC n ps D _ _ _ _ c4))

/-- the points of E[2] ∪ {O}: T_{s,t} = s·T0 + t·T1 -/
def torPN (n : Q) (s t : Bool) : Pt :=
  match s, t with
  | false, false => Pt.O
  | true, false => Pt.A zeroQ ratR.zero
  | false, true => Pt.A n ratR.zero
  | true, true => Pt.A (negQ n) ratR.zero

def tcA (ps : List Nat) {n : Q} (D : TorData n ps) (s t : Bool) : CL :=
  match s, t with
  | false, false => zc ps
  | true, false => ⟨xor D.k0m D.k0n, xorL D.b0m D.b0n⟩
  | false, true => ⟨D.kn0, D.bn0⟩
  | true, true => ⟨D.km0, D.bm0⟩

def tcB (ps : List Nat) {n : Q} (D : TorData n ps) (s t : Bool) : CL :=
  match s, t with
  | false, false => zc ps
  | true, false => ⟨D.k0n, D.b0n⟩
  | false, true => ⟨xor D.knm D.kn0, xorL D.bnm D.bn0⟩
  | true, true => ⟨D.kmn, D.bmn⟩

theorem on_torPN (n : Q) : ∀ s t : Bool, OnPtN n (torPN n s t)
  | false, false => trivial
  | true, false => Rank3Final.root1 ratR zeroQ n (negQ n)
  | false, true => Rank3Final.root2 ratR zeroQ n (negQ n)
  | true, true => Rank3Final.root3 ratR zeroQ n (negQ n)

theorem wf_tcA (n : Q) (ps : List Nat) (D : TorData n ps) : ∀ s t : Bool, WF ps (tcA ps D s t)
  | false, false => wf_zc ps
  | true, false => xorL_lenL ps _ _ D.l0m D.l0n
  | false, true => D.ln0
  | true, true => D.lm0

theorem wf_tcB (n : Q) (ps : List Nat) (D : TorData n ps) : ∀ s t : Bool, WF ps (tcB ps D s t)
  | false, false => wf_zc ps
  | true, false => D.l0n
  | false, true => xorL_lenL ps _ _ D.lnm D.ln0
  | true, true => D.lmn

theorem cls_torPN (n : Q) (ps : List Nat) (D : TorData n ps) :
    ∀ s t : Bool, ClsPtN n ps D (torPN n s t) (tcA ps D s t) (tcB ps D s t)
  | false, false => ⟨rfl, rfl⟩
  | true, false => ⟨wf_tcA n ps D true false, wf_tcB n ps D true false, Or.inr (Or.inl ⟨rfl, rfl, rfl⟩)⟩
  | false, true => ⟨wf_tcA n ps D false true, wf_tcB n ps D false true, Or.inr (Or.inr (Or.inl ⟨rfl, rfl, rfl⟩))⟩
  | true, true => ⟨wf_tcA n ps D true true, wf_tcB n ps D true true, Or.inr (Or.inr (Or.inr ⟨rfl, rfl, rfl⟩))⟩

/-- **Every point has a class in V**, given the Selmer bound img and the classes of E[2] in V. -/
theorem cls_existsN (n : Q) (ps : List Nat) (D : TorData n ps) (inV : CL → CL → Bool)
    (img : ∀ x y : Q, OnEn n x y → NDn n x →
      ∃ β β' : CL, WF ps β ∧ WF ps β' ∧ HCc n ps x β β' ∧ inV β β' = true)
    (vtor : ∀ s t : Bool, inV (tcA ps D s t) (tcB ps D s t) = true) :
    ∀ P : Pt, OnPtN n P → ∃ β β' : CL, ClsPtN n ps D P β β' ∧ inV β β' = true
  | Pt.O, _ => ⟨zc ps, zc ps, ⟨rfl, rfl⟩, vtor false false⟩
  | Pt.A x y, o => by
    cases Int.decEq x.num 0 with
    | isTrue h0 =>
      have e : x = zeroQ := zero_eq _ h0
      subst e
      exact ⟨_, _, ⟨wf_tcA n ps D true false, wf_tcB n ps D true false,
        Or.inr (Or.inl ⟨rfl, rfl, rfl⟩)⟩, vtor true false⟩
    | isFalse h0 =>
      cases Int.decEq (sub ratR x n).num 0 with
      | isTrue h1 =>
        have e : x = n := LibVieta.sub_zero_eq ratR _ _ (zero_eq _ h1)
        subst x
        exact ⟨_, _, ⟨wf_tcA n ps D false true, wf_tcB n ps D false true,
          Or.inr (Or.inr (Or.inl ⟨rfl, rfl, rfl⟩))⟩, vtor false true⟩
      | isFalse h1 =>
        cases Int.decEq (sub ratR x (negQ n)).num 0 with
        | isTrue h2 =>
          have e : x = negQ n := LibVieta.sub_zero_eq ratR _ _ (zero_eq _ h2)
          subst x
          exact ⟨_, _, ⟨wf_tcA n ps D true true, wf_tcB n ps D true true,
            Or.inr (Or.inr (Or.inr ⟨rfl, rfl, rfl⟩))⟩, vtor true true⟩
        | isFalse h2 =>
          have nd : NDn n x := ⟨ne_of_num' h0, ne_of_num' h1, ne_of_num' h2⟩
          obtain ⟨β, β', w, w', hc, hv⟩ := img x y o nd
          exact ⟨β, β', ⟨w, w', Or.inl ⟨nd, hc⟩⟩, hv⟩

/-- the trivial class times a square is a square -/
theorem triv_sq (ps : List Nat) (m : Int) :
    castQ (clsL false ps (zL ps) * (m * m)) = ratR.mul (castQ m) (castQ m) :=
  (castQ_sq _ _).trans (one_mulQ ps _)

theorem zc_inj (ps : List Nat) (k : Bool) (b : List Bool) (h : CL.mk k b = zc ps) : k = false ∧ b = zL ps :=
  ⟨congrArg CL.s h, congrArg CL.b h⟩

/-- **A point of class 0 is O or has the abscissa of a double.** -/
theorem kernel_ptN (n : Q) (ps : List Nat) (D : TorData n ps) (H : Hyp n ps D) (P : Pt) (o : OnPtN n P)
    (c : ClsPtN n ps D P (zc ps) (zc ps)) :
    P = Pt.O ∨ ∃ x y : Q, P = Pt.A x y ∧ ∃ xq yq w : Q, OnEn n xq yq ∧
      ratR.mul (TrianglePoint.two ratR yq) w = ratR.one ∧ DoublingGen.x2 ratR zeroQ n (negQ n) xq w = x := by
  cases P with
  | O => exact Or.inl rfl
  | A x y =>
    right
    refine ⟨x, y, rfl, ?_⟩
    obtain ⟨_, _, c⟩ := c
    rcases c with ⟨nd, hc⟩ | t
    · obtain ⟨s, r, _, _, ex, exn⟩ := hc
      exact kernel_n n x y s r H.hn o (ex.trans (one_mulQ ps _)) (exn.trans (one_mulQ ps _)) nd.1 nd.2.1
    · rcases t with ⟨e, _, b2⟩ | ⟨e, b1, _⟩ | ⟨e, b1, _⟩
      · obtain ⟨k, b⟩ := zc_inj ps _ _ b2.symm
        have h := D.e0n
        rw [k, b, triv_sq] at h
        exact (H.h0n _ h).elim
      · obtain ⟨k, b⟩ := zc_inj ps _ _ b1.symm
        have h := D.en0
        rw [k, b, triv_sq] at h
        exact (H.hn0 _ h).elim
      · obtain ⟨k, b⟩ := zc_inj ps _ _ b1.symm
        have h := D.em0
        rw [k, b, triv_sq] at h
        exact (H.hm0 _ h).elim

/-- **rank E_n(ℚ) ≤ 3, finite form**, from the Selmer bound img and the dependency dep. -/
theorem rank_le3N (n : Q) (ps : List Nat) (D : TorData n ps) (H : Hyp n ps D) (inV : CL → CL → Bool)
    (img : ∀ x y : Q, OnEn n x y → NDn n x →
      ∃ β β' : CL, WF ps β ∧ WF ps β' ∧ HCc n ps x β β' ∧ inV β β' = true)
    (vtor : ∀ s t : Bool, inV (tcA ps D s t) (tcB ps D s t) = true)
    (dep : ∀ β1 β2 β3 β4 β1' β2' β3' β4' : CL, inV β1 β1' = true → inV β2 β2' = true → inV β3 β3' = true →
      inV β4 β4' = true → ∃ c : E4, nz c = true ∧ ∃ s t : Bool,
        sum4C ps c β1 β2 β3 β4 = tcA ps D s t ∧ sum4C ps c β1' β2' β3' β4' = tcB ps D s t)
    (P1 P2 P3 P4 : Pt) (o1 : OnPtN n P1) (o2 : OnPtN n P2) (o3 : OnPtN n P3) (o4 : OnPtN n P4) :
    ∃ c : E4, nz c = true ∧ ∃ s t : Bool,
      addN n (sumPN n c P1 P2 P3 P4) (torPN n s t) = Pt.O ∨
      ∃ x y : Q, addN n (sumPN n c P1 P2 P3 P4) (torPN n s t) = Pt.A x y ∧ ∃ xq yq w : Q, OnEn n xq yq ∧
        ratR.mul (TrianglePoint.two ratR yq) w = ratR.one ∧ DoublingGen.x2 ratR zeroQ n (negQ n) xq w = x := by
  obtain ⟨β1, β1', c1, v1⟩ := cls_existsN n ps D inV img vtor P1 o1
  obtain ⟨β2, β2', c2, v2⟩ := cls_existsN n ps D inV img vtor P2 o2
  obtain ⟨β3, β3', c3, v3⟩ := cls_existsN n ps D inV img vtor P3 o3
  obtain ⟨β4, β4', c4, v4⟩ := cls_existsN n ps D inV img vtor P4 o4
  obtain ⟨c, hn, s, t, e1, e2⟩ := dep β1 β2 β3 β4 β1' β2' β3' β4' v1 v2 v3 v4
  refine ⟨c, hn, s, t, ?_⟩
  have cS := cls_sumPN n ps D H c P1 P2 P3 P4 o1 o2 o3 o4 β1 β2 β3 β4 β1' β2' β3' β4' c1 c2 c3 c4
  rw [e1, e2] at cS
  have oS := on_sumPN n c P1 P2 P3 P4 o1 o2 o3 o4
  have cR := cls_addN n ps H.hc H.hp D H.R H.hn H.h0n H.hn0 H.hm0 _ _ _ _ _ _ oS (on_torPN n s t) cS
    (cls_torPN n ps D s t)
  rw [cxor_self ps _ (wf_tcA n ps D s t), cxor_self ps _ (wf_tcB n ps D s t)] at cR
  exact kernel_ptN n ps D H _ (on_addN n _ _ oS (on_torPN n s t)) cR

end Rank3FinalN

#print axioms Rank3FinalN.on_selPN
#print axioms Rank3FinalN.cls_selC
#print axioms Rank3FinalN.on_sumPN
#print axioms Rank3FinalN.cls_sumPN
#print axioms Rank3FinalN.on_torPN
#print axioms Rank3FinalN.wf_tcA
#print axioms Rank3FinalN.wf_tcB
#print axioms Rank3FinalN.cls_torPN
#print axioms Rank3FinalN.cls_existsN
#print axioms Rank3FinalN.triv_sq
#print axioms Rank3FinalN.zc_inj
#print axioms Rank3FinalN.kernel_ptN
#print axioms Rank3FinalN.rank_le3N

bsd/Rank3Lower.lean

lean_all/bsd/Rank3Lower.lean

/-
  bsd/Rank3Lower.lean — T39 (goal G289e-8): rank E_42486(ℚ) = 3 in point form.
  The three integral points of bsd/Rank3Cert12 (n = 42486)
      P₁ = (46818, 4255848),  P₂ = (63948, 12086172),  P₃ = (−5475, 3117465)
  lie on y² = x(x − n)(x + n), have descent classes
      P₁ : (2, 3),   P₂ : (3, 2·3·73),   P₃ : (−3·73, −1)        (x = β·s², x − n = β′·r²)
  and for every nonempty subset sum S of them and every T ∈ E[2] ∪ {O}, S + T is neither O nor a point with the
  abscissa of a double 2Q (its class is never (1, 1); a 2Q-abscissa has class (1, 1), bsd/DblCls).
  With Rank3Final.rank_le3 (any four points are dependent in that sense) the rank is exactly 3.
    pt_int                 an integral point with its class from integer identities (generic in the integers)
    o1 … c3                the three points
    no_bad                 the finite check over all subsets and all T (rfl)
    not_O, not_dbl         the lower bound
    rank_eq3               lower bound for P₁, P₂, P₃ together with rank_le3
  Lean 4 core plus bsd/Rank3Final, bsd/TransPts.
-/
import bsd.Rank3Final
import bsd.TransPts

namespace Rank3Lower
open DetChain LibRing LibRat QModel QCls B5S TransStep TransStep2 StepFull PtAdd QDec DblCls PtCls PtCls2 Rank3Final

/-- an integral point (X, Y) with x − n = A, x + n = B, x = β·S², x − n = β′·R² -/
theorem pt_int (X Y A B S R0 : Int) (β β' : B5)
    (hA : (X * Int.ofNat 1 + -42486 * Int.ofNat 1) * Int.ofNat 1 = A * Int.ofNat (1 * 1))
    (hB : (X * Int.ofNat 1 + - -42486 * Int.ofNat 1) * Int.ofNat 1 = B * Int.ofNat (1 * 1))
    (hY : Y * Y = X * A * B) (hX0 : TransPts.isZ X = false) (hA0 : TransPts.isZ A = false)
    (hB0 : TransPts.isZ B = false) (hS : TransPts.isZ S = false) (hR : TransPts.isZ R0 = false)
    (hx : X = β.cls * (S * S)) (hn : A = β'.cls * (R0 * R0)) :
    OnPt (Pt.A (castQ X) (castQ Y)) ∧ ClsPt (Pt.A (castQ X) (castQ Y)) β β' := by
  have eA : sub ratR (castQ X) nQ = castQ A := (const_sub X 42486).trans ((normQ_eq_iff _ _ _ _ _ _).2 hA)
  have eB : sub ratR (castQ X) (negQ nQ) = castQ B :=
    (congrArg (sub ratR (castQ X)) negQ_nQ).trans ((const_sub X (-42486)).trans ((normQ_eq_iff _ _ _ _ _ _).2 hB))
  refine ⟨?_, Or.inl ⟨⟨?_, ?_, ?_⟩, ?_⟩⟩
  · show OnE (castQ X) (castQ Y)
    unfold OnE DescentHomFull.f
    rw [sub0, eA, eB, ← castQ_mul, ← castQ_mul, ← castQ_mul, hY]
  · exact normQ_ne_zero (Nat.succ_pos 0) (TransPts.nz _ hX0)
  · exact fun h => normQ_ne_zero (Nat.succ_pos 0) (TransPts.nz _ hA0) (eA.symm.trans h)
  · exact fun h => normQ_ne_zero (Nat.succ_pos 0) (TransPts.nz _ hB0) (eB.symm.trans h)
  · unfold HC HasCls
    refine ⟨castQ S, castQ R0, normQ_ne_zero (Nat.succ_pos 0) (TransPts.nz _ hS),
      normQ_ne_zero (Nat.succ_pos 0) (TransPts.nz _ hR), ?_, ?_⟩
    · rw [hx, castQ_mul, castQ_mul]
      all_goals rfl
    · rw [eA, hn, castQ_mul, castQ_mul]
      all_goals rfl

def B1 : B5 := ⟨false, true, false, false, false⟩
def B1' : B5 := ⟨false, false, true, false, false⟩
def B2 : B5 := ⟨false, false, true, false, false⟩
def B2' : B5 := ⟨false, true, true, true, false⟩
def B3 : B5 := ⟨true, false, true, true, false⟩
def B3' : B5 := ⟨true, false, false, false, false⟩

def P1 : Pt := Pt.A (castQ 46818) (castQ 4255848)
def P2 : Pt := Pt.A (castQ 63948) (castQ 12086172)
def P3 : Pt := Pt.A (castQ (-5475)) (castQ 3117465)

theorem pc1 : OnPt P1 ∧ ClsPt P1 B1 B1' :=
  pt_int 46818 4255848 4332 89304 153 38 B1 B1' rfl rfl rfl rfl rfl rfl rfl rfl rfl rfl
theorem pc2 : OnPt P2 ∧ ClsPt P2 B2 B2' :=
  pt_int 63948 12086172 21462 106434 146 7 B2 B2' rfl rfl rfl rfl rfl rfl rfl rfl rfl rfl
theorem pc3 : OnPt P3 ∧ ClsPt P3 B3 B3' :=
  pt_int (-5475) 3117465 (-47961) 37011 5 219 B3 B3' rfl rfl rfl rfl rfl rfl rfl rfl rfl rfl

/-- the class is zero -/
def isz (β : B5) : Bool := !(β.s || β.a || β.b || β.c || β.d)

def bad (c : Dep4.E4) (s t : Bool) : Bool :=
  !c.e4 && Dep4.nz c && isz ((Dep4.sum4 c B1 B2 B3 z5).xor ⟨s, t, t, t, t⟩)
    && isz ((Dep4.sum4 c B1' B2' B3' z5).xor ⟨s, Bool.xor s t, s, s, s⟩)

def chk : Bool :=
  VSet.all2 fun e1 => VSet.all2 fun e2 => VSet.all2 fun e3 => VSet.all2 fun e4 =>
  VSet.all2 fun s => VSet.all2 fun t => !bad ⟨e1, e2, e3, e4⟩ s t

theorem chk_true : chk = true := rfl

/-- **No nonempty subset sum of P₁, P₂, P₃ plus a point of E[2] ∪ {O} has class (1, 1).** -/
theorem no_bad : ∀ (c : Dep4.E4) (s t : Bool), bad c s t = false
  | ⟨e1, e2, e3, e4⟩, s, t =>
    Dep4.not_true (Dep4.all2_app _ (Dep4.all2_app _ (Dep4.all2_app _ (Dep4.all2_app _
      (Dep4.all2_app _ (Dep4.all2_app _ chk_true e1) e2) e3) e4) s) t)

theorem bad_of (c : Dep4.E4) (s t : Bool) (hc4 : c.e4 = false) (hnz : Dep4.nz c = true)
    (e1 : (Dep4.sum4 c B1 B2 B3 z5).xor ⟨s, t, t, t, t⟩ = z5)
    (e2 : (Dep4.sum4 c B1' B2' B3' z5).xor ⟨s, Bool.xor s t, s, s, s⟩ = z5) : bad c s t = true := by
  unfold bad
  rw [hc4, hnz, e1, e2]
  all_goals rfl

theorem clsR (c : Dep4.E4) (s t : Bool) :
    ClsPt (add (sumP c P1 P2 P3 Pt.O) (torP s t))
      ((Dep4.sum4 c B1 B2 B3 z5).xor ⟨s, t, t, t, t⟩)
      ((Dep4.sum4 c B1' B2' B3' z5).xor ⟨s, Bool.xor s t, s, s, s⟩) :=
  cls_add _ _ _ _ _ _ (on_sumP c P1 P2 P3 Pt.O pc1.1 pc2.1 pc3.1 trivial) (on_torP s t)
    (cls_sumP c P1 P2 P3 Pt.O pc1.1 pc2.1 pc3.1 trivial B1 B2 B3 z5 B1' B2' B3' z5 pc1.2 pc2.2 pc3.2 ⟨rfl, rfl⟩)
    (cls_torP s t)

/-- **A nonempty subset sum plus T is not O.** -/
theorem not_O (c : Dep4.E4) (hc4 : c.e4 = false) (hnz : Dep4.nz c = true) (s t : Bool) :
    add (sumP c P1 P2 P3 Pt.O) (torP s t) ≠ Pt.O := by
  intro h
  have cR := clsR c s t
  rw [h] at cR
  obtain ⟨e1, e2⟩ := cR
  exact Bool.noConfusion ((no_bad c s t).symm.trans (bad_of c s t hc4 hnz e1 e2))

/-- **A nonempty subset sum plus T does not have the abscissa of a double.** -/
theorem not_dbl (c : Dep4.E4) (hc4 : c.e4 = false) (hnz : Dep4.nz c = true) (s t : Bool) (x y : Q)
    (h : add (sumP c P1 P2 P3 Pt.O) (torP s t) = Pt.A x y) (xq yq w : Q) (o : OnE xq yq)
    (hw : ratR.mul (TrianglePoint.two ratR yq) w = ratR.one) :
    DoublingGen.x2 ratR zeroQ nQ (negQ nQ) xq w ≠ x := by
  intro hx
  have cR := clsR c s t
  rw [h] at cR
  have d := dbl_nd_cls xq yq w o hw
  rw [hx] at d
  obtain ⟨e1, e2⟩ := same_x x _ _ z5 z5 cR (Or.inl d)
  exact Bool.noConfusion ((no_bad c s t).symm.trans (bad_of c s t hc4 hnz e1 e2))

/-- **rank E_42486(ℚ) = 3, point form.** P₁, P₂, P₃ are independent modulo 2E(ℚ) + E[2], and any four points
    are dependent in the same sense. -/
theorem rank_eq3 :
    (∀ c : Dep4.E4, c.e4 = false → Dep4.nz c = true → ∀ s t : Bool,
      add (sumP c P1 P2 P3 Pt.O) (torP s t) ≠ Pt.O ∧
      ∀ x y : Q, add (sumP c P1 P2 P3 Pt.O) (torP s t) = Pt.A x y → ∀ xq yq w : Q, OnE xq yq →
        ratR.mul (TrianglePoint.two ratR yq) w = ratR.one → DoublingGen.x2 ratR zeroQ nQ (negQ nQ) xq w ≠ x) ∧
    (∀ Q1 Q2 Q3 Q4 : Pt, OnPt Q1 → OnPt Q2 → OnPt Q3 → OnPt Q4 →
      ∃ c : Dep4.E4, Dep4.nz c = true ∧ ∃ s t : Bool,
        add (sumP c Q1 Q2 Q3 Q4) (torP s t) = Pt.O ∨
        ∃ x y : Q, add (sumP c Q1 Q2 Q3 Q4) (torP s t) = Pt.A x y ∧ ∃ xq yq w : Q, OnE xq yq ∧
          ratR.mul (TrianglePoint.two ratR yq) w = ratR.one ∧ DoublingGen.x2 ratR zeroQ nQ (negQ nQ) xq w = x) :=
  ⟨fun c hc4 hnz s t => ⟨not_O c hc4 hnz s t, fun x y h xq yq w o hw => not_dbl c hc4 hnz s t x y h xq yq w o hw⟩,
   fun Q1 Q2 Q3 Q4 o1 o2 o3 o4 => rank_le3 Q1 Q2 Q3 Q4 o1 o2 o3 o4⟩

end Rank3Lower

#print axioms Rank3Lower.pt_int
#print axioms Rank3Lower.pc1
#print axioms Rank3Lower.pc2
#print axioms Rank3Lower.pc3
#print axioms Rank3Lower.chk_true
#print axioms Rank3Lower.no_bad
#print axioms Rank3Lower.bad_of
#print axioms Rank3Lower.clsR
#print axioms Rank3Lower.not_O
#print axioms Rank3Lower.not_dbl
#print axioms Rank3Lower.rank_eq3

bsd/Rank3N1254.lean

lean_all/bsd/Rank3N1254.lean

/-
  bsd/Rank3N1254.lean — T39 (goal G289f-4h-5e6): rank E_1254(ℚ) ≤ 3 through the general-n theorem
  Rank3FinalN.rank_le3N, with every curve input proved: the Selmer bound img from AssembleN.img_of_clear (class
  existence IntDataN, translation points TP1254, clearing Clear1254, local obstructions Fin1254) and the dependency
  from DepGraph.dep_graph (V closed under ⊕ and a graph, checked over the 32 classes by rfl, bsd/VList).
    is4_of_len, hex1254, img1254, vtorL, hT1254, dep1254, rank_le3_1254
  With bsd/Rank3Cert (three independent points): rank E_1254(ℚ) = 3.
  Lean 4 core plus bsd/Fin1254, bsd/VList.
-/
import bsd.Fin1254
import bsd.VList

set_option maxRecDepth 200000

namespace Rank3N1254
open DetChain LibRing LibRat LibInt QModel QCls SqUniqueL SqClassL TransStepL StepN DblClsN PtAdd PtAddN PtClsN
  Rank3FinalN QModelN QModelZ Tor1254 AssembleN TP1254 Clear1254 VList Dep4

def inV' : CL → CL → Bool := inVL Vlist

theorem is4_of_len (l : List Bool) (h : l.length = 4) : is4 l = true := by
  obtain ⟨a, b, c, d, e⟩ := DepGraph.len4' l h
  rw [e]; rfl

theorem hex1254 (x y : Q) (o : OnEn n1254 x y) (nd : NDn n1254 x) :
    ∃ β β' : CL, WF P1254 β ∧ WF P1254 β' ∧ HCc n1254 P1254 x β β' := by
  obtain ⟨nd0, nd1, _⟩ := nd
  have hx0 : x.num ≠ 0 := fun h => nd0 (Assemble3.num_zero x h)
  have hxn : x.num - Int.ofNat 1254 * Int.ofNat x.den ≠ 0 := fun h => nd1 (by
    rw [show n1254 = nz (Int.ofNat 1254) from rfl, e_nz x (Int.ofNat 1254)]
    exact Assemble3.normQ_zero _ _ _ ((Assemble3.sub_form _ x.num _).trans h))
  obtain ⟨_, _, _, _, _, _, s1, s2, b1, b2, _, _, _, _, l1, l2, _, _, hcl⟩ :=
    IntDataN.int_dataN [3, 11, 19] rfl 1254 Fin1254.hN x y o hx0 hxn
  exact ⟨⟨s1, b1⟩, ⟨s2, b2⟩, l1, l2, hcl⟩

theorem img1254 (x y : Q) (o : OnEn n1254 x y) (nd : NDn n1254 x) :
    ∃ β β' : CL, WF P1254 β ∧ WF P1254 β' ∧ HCc n1254 P1254 x β β' ∧ inV' β β' = true := by
  obtain ⟨β, β', w, w', hc, hv⟩ := img_of_clear n1254 P1254 td1254 H1254 inV1254 vtor1254 hex1254 comb comb_ok
    hclr1254 Fin1254.hfin1254 x y o nd
  refine ⟨β, β', w, w', hc, ?_⟩
  show ((is4 β.b && is4 β'.b) && inV1254 β β') = true
  rw [is4_of_len β.b w, is4_of_len β'.b w', hv]
  rfl

theorem vtorL : ∀ s t : Bool, inV' (tcA P1254 td1254 s t) (tcB P1254 td1254 s t) = true
  | false, false => rfl
  | true, false => rfl
  | false, true => rfl
  | true, true => rfl

theorem hT1254 : ∀ s t : Bool, tcA P1254 td1254 s t = ⟨s, [t, t, t, t]⟩
  | false, false => rfl
  | true, false => rfl
  | false, true => rfl
  | true, true => rfl

theorem cl1254 : cl_chk Vlist = true := rfl
theorem fn1254 : fn_chk Vlist = true := rfl

theorem dep1254 (β1 β2 β3 β4 β1' β2' β3' β4' : CL) (h1 : inV' β1 β1' = true) (h2 : inV' β2 β2' = true)
    (h3 : inV' β3 β3' = true) (h4 : inV' β4 β4' = true) :
    ∃ c : E4, nz c = true ∧ ∃ s t : Bool,
      sum4C P1254 c β1 β2 β3 β4 = tcA P1254 td1254 s t ∧ sum4C P1254 c β1' β2' β3' β4' = tcB P1254 td1254 s t :=
  DepGraph.dep_graph P1254 rfl n1254 td1254 inV' rfl (wf_of_inVL P1254 rfl Vlist) (cl_of_chk Vlist cl1254)
    (fn_of_chk Vlist fn1254) vtorL hT1254 β1 β2 β3 β4 β1' β2' β3' β4' h1 h2 h3 h4

/-- **rank E_1254(ℚ) ≤ 3**: any four points have a nonempty subset sum plus a point of E[2] ∪ {O} equal to O or
    with the abscissa of a double. -/
theorem rank_le3_1254 (P1 P2 P3 P4 : Pt) (o1 : OnPtN n1254 P1) (o2 : OnPtN n1254 P2) (o3 : OnPtN n1254 P3)
    (o4 : OnPtN n1254 P4) :
    ∃ c : E4, nz c = true ∧ ∃ s t : Bool,
      addN n1254 (sumPN n1254 c P1 P2 P3 P4) (torPN n1254 s t) = Pt.O ∨
      ∃ x y : Q, addN n1254 (sumPN n1254 c P1 P2 P3 P4) (torPN n1254 s t) = Pt.A x y ∧ ∃ xq yq w : Q,
        OnEn n1254 xq yq ∧ ratR.mul (TrianglePoint.two ratR yq) w = ratR.one ∧
        DoublingGen.x2 ratR zeroQ n1254 (negQ n1254) xq w = x :=
  rank_le3N n1254 P1254 td1254 H1254 inV' img1254 vtorL dep1254 P1 P2 P3 P4 o1 o2 o3 o4

end Rank3N1254

#print axioms Rank3N1254.is4_of_len
#print axioms Rank3N1254.hex1254
#print axioms Rank3N1254.img1254
#print axioms Rank3N1254.vtorL
#print axioms Rank3N1254.hT1254
#print axioms Rank3N1254.cl1254
#print axioms Rank3N1254.fn1254
#print axioms Rank3N1254.dep1254
#print axioms Rank3N1254.rank_le3_1254

bsd/Rank3N42486.lean

lean_all/bsd/Rank3N42486.lean

/-
  bsd/Rank3N42486.lean — T39 (goal G289f-4h, check of the general assembly): rank E_42486(ℚ) ≤ 3 obtained from the
  general-n theorem Rank3FinalN.rank_le3N, with the curve inputs translated from the B5 classes of bsd/Assemble3
  (Selmer bound) and bsd/VLin (dependency) into list classes over [2, 3, 73, 97].
    toCL, cls_toCL, hc_toCL      B5 class ↦ list class, same value
    is4, shape4, inVC, shapeV    the set V on list classes
    sum4C_toCL, tcA_toCL, tcB_toCL    subset sums and torsion classes
    img42486, vtor42486, dep42486, H42486    the inputs
    rank_le3_42486               the finite rank bound for n = 42486 through the general theorem
  Lean 4 core plus bsd/Rank3FinalN, bsd/TorGen2, bsd/VLin, bsd/Assemble3.
-/
import bsd.Rank3FinalN
import bsd.TorGen2
import bsd.VLin
import bsd.Assemble3

namespace Rank3N42486
open DetChain LibRing LibRat LibInt QModel QCls B5S SqUniqueL SqClassL QModelN TransStepL StepN PtAdd PtAddN
  PtClsN PtClsN2 Dep4 Rank3FinalN TorGen TorGen2 TransStep TransStep2

def toCL (β : B5) : CL := ⟨β.s, [β.a, β.b, β.c, β.d]⟩

theorem assoc5 (e a b c d : Int) : e * (a * (b * (c * (d * 1)))) = e * a * b * c * d := by lib_int

theorem cls_toCL (β : B5) : clsL β.s P4 [β.a, β.b, β.c, β.d] = β.cls := assoc5 _ _ _ _ _

theorem cq_toCL (β : B5) : cqL P4 β.s [β.a, β.b, β.c, β.d] = cq β := congrArg castQ (cls_toCL β)

theorem hc_toCL (x : Q) (β1 β2 : B5) (h : HC x β1 β2) : HCc nQ P4 x (toCL β1) (toCL β2) := by
  obtain ⟨s, r, hs, hr, ex, exn⟩ := h
  exact ⟨s, r, hs, hr, ex.trans (congrArg (fun z => ratR.mul z (ratR.mul s s)) (cq_toCL β1).symm),
    exn.trans (congrArg (fun z => ratR.mul z (ratR.mul r r)) (cq_toCL β2).symm)⟩

def is4 : List Bool → Bool
  | [] => false
  | [_] => false
  | [_, _] => false
  | [_, _, _] => false
  | [_, _, _, _] => true
  | _ :: _ :: _ :: _ :: _ :: _ => false

def g0 : List Bool → Bool
  | [] => false
  | a :: _ => a

def g1 : List Bool → Bool
  | [] => false
  | [_] => false
  | _ :: a :: _ => a

def g2 : List Bool → Bool
  | [] => false
  | [_] => false
  | [_, _] => false
  | _ :: _ :: a :: _ => a

def g3 : List Bool → Bool
  | [] => false
  | [_] => false
  | [_, _] => false
  | [_, _, _] => false
  | _ :: _ :: _ :: a :: _ => a

/-- the set V on list classes -/
def inVC (β β' : CL) : Bool :=
  (is4 β.b && is4 β'.b) && VSet.inV β.s (g0 β.b) (g1 β.b) (g2 β.b) (g3 β.b) β'.s (g0 β'.b) (g1 β'.b) (g2 β'.b) (g3 β'.b)

theorem shape4 : ∀ l : List Bool, is4 l = true → ∃ a b c d : Bool, l = [a, b, c, d]
  | [], h => Bool.noConfusion h
  | [_], h => Bool.noConfusion h
  | [_, _], h => Bool.noConfusion h
  | [_, _, _], h => Bool.noConfusion h
  | [a, b, c, d], _ => ⟨a, b, c, d, rfl⟩
  | _ :: _ :: _ :: _ :: _ :: _, h => Bool.noConfusion h

theorem shapeV (β β' : CL) (h : inVC β β' = true) :
    ∃ B B' : B5, β = toCL B ∧ β' = toCL B' ∧ inVB B B' = true := by
  cases β with
  | mk s l =>
    cases β' with
    | mk s' l' =>
      obtain ⟨a, b, c, d, e⟩ := shape4 l (SqUniqueL.and_l (SqUniqueL.and_l h))
      obtain ⟨a', b', c', d', e'⟩ := shape4 l' (SqUniqueL.and_r (SqUniqueL.and_l h))
      subst e; subst e'
      exact ⟨⟨s, a, b, c, d⟩, ⟨s', a', b', c', d'⟩, rfl, rfl, SqUniqueL.and_r h⟩

theorem sum4C_toCL (c : E4) (B1 B2 B3 B4 : B5) :
    sum4C P4 c (toCL B1) (toCL B2) (toCL B3) (toCL B4) = toCL (sum4 c B1 B2 B3 B4) := by
  cases c with
  | mk e1 e2 e3 e4 => cases e1 <;> cases e2 <;> cases e3 <;> cases e4 <;> rfl

theorem tcA_toCL : ∀ s t : Bool, tcA P4 td42486 s t = toCL ⟨s, t, t, t, t⟩
  | false, false => rfl
  | true, false => rfl
  | false, true => rfl
  | true, true => rfl

theorem tcB_toCL : ∀ s t : Bool, tcB P4 td42486 s t = toCL ⟨s, Bool.xor s t, s, s, s⟩
  | false, false => rfl
  | true, false => rfl
  | false, true => rfl
  | true, true => rfl

theorem img42486 (x y : Q) (o : OnEn nQ x y) (nd : NDn nQ x) :
    ∃ β β' : CL, WF P4 β ∧ WF P4 β' ∧ HCc nQ P4 x β β' ∧ inVC β β' = true := by
  obtain ⟨β1, β2, hc, hv⟩ := Assemble3.image_in_V x y o nd
  exact ⟨toCL β1, toCL β2, rfl, rfl, hc_toCL x β1 β2 hc, hv⟩

theorem vtor42486 : ∀ s t : Bool, inVC (tcA P4 td42486 s t) (tcB P4 td42486 s t) = true
  | false, false => rfl
  | true, false => rfl
  | false, true => rfl
  | true, true => rfl

theorem dep42486 (β1 β2 β3 β4 β1' β2' β3' β4' : CL) (h1 : inVC β1 β1' = true) (h2 : inVC β2 β2' = true)
    (h3 : inVC β3 β3' = true) (h4 : inVC β4 β4' = true) :
    ∃ c : E4, nz c = true ∧ ∃ s t : Bool,
      sum4C P4 c β1 β2 β3 β4 = tcA P4 td42486 s t ∧ sum4C P4 c β1' β2' β3' β4' = tcB P4 td42486 s t := by
  obtain ⟨B1, B1', e1, e1', v1⟩ := shapeV β1 β1' h1
  obtain ⟨B2, B2', e2, e2', v2⟩ := shapeV β2 β2' h2
  obtain ⟨B3, B3', e3, e3', v3⟩ := shapeV β3 β3' h3
  obtain ⟨B4, B4', e4, e4', v4⟩ := shapeV β4 β4' h4
  subst e1; subst e1'; subst e2; subst e2'; subst e3; subst e3'; subst e4; subst e4'
  obtain ⟨c, hn, s, t, d1, d2⟩ := VLin.dep_V B1 B2 B3 B4 B1' B2' B3' B4' v1 v2 v3 v4
  refine ⟨c, hn, s, t, ?_, ?_⟩
  · rw [sum4C_toCL, d1, tcA_toCL]
  · rw [sum4C_toCL, d2, tcB_toCL]

theorem H42486 : Hyp nQ P4 td42486 :=
  ⟨chk_P4, pos_P4, tr42486, PtCls.n_ne, h0n_42486, hn0_42486, hm0_42486⟩

/-- **rank E_42486(ℚ) ≤ 3 through the general-n assembly.** -/
theorem rank_le3_42486 (P1 P2 P3 P4' : Pt) (o1 : OnPtN nQ P1) (o2 : OnPtN nQ P2) (o3 : OnPtN nQ P3)
    (o4 : OnPtN nQ P4') :
    ∃ c : E4, nz c = true ∧ ∃ s t : Bool,
      addN nQ (sumPN nQ c P1 P2 P3 P4') (torPN nQ s t) = Pt.O ∨
      ∃ x y : Q, addN nQ (sumPN nQ c P1 P2 P3 P4') (torPN nQ s t) = Pt.A x y ∧ ∃ xq yq w : Q, OnEn nQ xq yq ∧
        ratR.mul (TrianglePoint.two ratR yq) w = ratR.one ∧ DoublingGen.x2 ratR zeroQ nQ (negQ nQ) xq w = x :=
  rank_le3N nQ P4 td42486 H42486 inVC img42486 vtor42486 dep42486 P1 P2 P3 P4' o1 o2 o3 o4

end Rank3N42486

#print axioms Rank3N42486.assoc5
#print axioms Rank3N42486.cls_toCL
#print axioms Rank3N42486.cq_toCL
#print axioms Rank3N42486.hc_toCL
#print axioms Rank3N42486.shape4
#print axioms Rank3N42486.shapeV
#print axioms Rank3N42486.sum4C_toCL
#print axioms Rank3N42486.tcA_toCL
#print axioms Rank3N42486.tcB_toCL
#print axioms Rank3N42486.img42486
#print axioms Rank3N42486.vtor42486
#print axioms Rank3N42486.dep42486
#print axioms Rank3N42486.H42486
#print axioms Rank3N42486.rank_le3_42486

bsd/Rank42486.lean

lean_all/bsd/Rank42486.lean

/-
  bsd/Rank42486.lean — T39 (goal G289): the 2-descent of the congruent curve E : y² = x³ − n²x, n = 42486.
    row_ok        the three points G1, G2, G3 of Rank3Cert12 lie on E and their descent images with those of E[2]
                  span 32 = 2³·2² classes (lower side: rank E(Q) ≥ 3)
    card_V        the class set V (bsd/VSet) has exactly 32 elements
    image_in_V    every rational point of E outside E[2] has its descent class in V (bsd/Assemble3; upper side:
                  the descent image of E(Q) is V, so E(Q)/2E(Q) has order 32 and rank E(Q) = 3 once the descent
                  map E(Q)/2E(Q) → classes is known to be an injective homomorphism)
  Lean 4 core plus bsd/Rank3Cert12, bsd/Assemble3.
-/
import bsd.Rank3Cert12
import bsd.Assemble3

namespace Rank42486
open Rank3Cert2

def row : Nat × Pt × Pt × Pt :=
  (42486, (true, 46818, 1, 4255848), (true, 63948, 1, 12086172), (false, 5475, 1, 3117465))

theorem row_mem : row ∈ Rank3Cert12.rows := by
  unfold Rank3Cert12.rows
  repeat (first | exact List.Mem.head _ | apply List.Mem.tail)

set_option maxRecDepth 100000 in
theorem row_ok : onRow row = true ∧ indepRow3 row = true := ⟨rfl, rfl⟩

def b2n (b : Bool) : Nat := match b with
  | true => 1
  | false => 0

/-- f true + f false -/
def sumB (f : Bool → Nat) : Nat := f true + f false

/-- number of classes (β₁, β₂) with inV β₁ β₂ = true -/
def cntV : Nat :=
  sumB fun s1 => sumB fun a1 => sumB fun b1 => sumB fun c1 => sumB fun d1 =>
  sumB fun s2 => sumB fun a2 => sumB fun b2 => sumB fun c2 => sumB fun d2 =>
    b2n (VSet.inV s1 a1 b1 c1 d1 s2 a2 b2 c2 d2)

theorem card_V : cntV = 32 := rfl

/-- **Descent image of E_42486(Q) \ E[2] ⊆ V, |V| = 32, and V is reached by three independent points.** -/
theorem image_in_V (x y : LibRat.Q) (o : QModel.OnE x y) (nd : TransStep.ND x) :
    ∃ β1 β2 : B5S.B5, TransStep.HC x β1 β2 ∧ TransStep2.inVB β1 β2 = true :=
  Assemble3.image_in_V x y o nd

end Rank42486

#print axioms Rank42486.row_mem
#print axioms Rank42486.row_ok
#print axioms Rank42486.card_V
#print axioms Rank42486.image_in_V

bsd/Rank4Cert.lean

lean_all/bsd/Rank4Cert.lean

/-
  bsd/Rank4Cert.lean — T39 cycle 501 (goal G152): rank ≥ 4 for the congruent curve n = 29274, from four points whose
  2-descent images, with the torsion images, span a group of order 64 in (Q*/Q*²)².

  Lean 4 core plus bsd/Rank2Cert (img, nontriv), bsd/Rank3Cert (mul2), rfl. Points (±a/b², c/b³) from the block search
  (box 6000): x = 485809/16, 30258, 35301, 34476. Checked:
    on_curve   the four points lie on y² = x³ − 29274²x
    indep      every one of the 15 nonzero products of δ(P₁), …, δ(P₄) avoids every torsion image, so E/2E has order
               ≥ 64 = 2^{r+2} and r ≥ 4 (DescentHomFull, TorsionShift, TorsionImage; tors = E[2] cited).
  With s₂(29274) = 4 (MonskyCheck147) and Monsky's bound (cited), the rank is exactly 4.
-/
import bsd.Rank3Cert

namespace Rank4Cert
open CongruentPoints5 DescentCert Rank2Cert Rank3Cert

def n : Nat := 29274
def pts : List (Bool × Nat × Nat × Nat) :=
  [(true, 485809, 4, 89874665), (true, 30258, 1, 1331352), (true, 35301, 1, 3706605), (true, 34476, 1, 3381300)]

theorem on_curve : pts.all (fun p => onC n p.1 p.2.1 p.2.2.1 p.2.2.2) = true := rfl

/-- all products of a nonempty subset of the images -/
def prods : List (Int × Int) → List (Int × Int)
  | [] => []
  | a :: as => let r := prods as; a :: r ++ r.map (mul2 a)

theorem nprods : (prods (pts.map (img n))).length = 15 := rfl

theorem indep : (prods (pts.map (img n))).all (nontriv n) = true := rfl

end Rank4Cert

#print axioms Rank4Cert.on_curve
#print axioms Rank4Cert.nprods
#print axioms Rank4Cert.indep

bsd/Rank4Cert2.lean

lean_all/bsd/Rank4Cert2.lean

/-
  bsd/Rank4Cert2.lean — T39 cycle 603 (goal G316b): rank ≥ 4 for n = 46274, 46754, from four points each whose 2-descent
  images with the torsion images span order 64 (block searches, tools/rank4scan.py). Both have s₂ = 4, so with Monsky's
  bound (cited) the rank is 4. Lean 4 core plus bsd/Rank4Cert (prods), rfl.
-/
import bsd.Rank4Cert

namespace Rank4Cert2
open CongruentPoints5 DescentCert Rank2Cert Rank3Cert Rank4Cert

def rows : List (Nat × List (Bool × Nat × Nat × Nat)) := [
   (46274, [(true, 46818, 1, 1539792), (false, 4802, 1, 3189312), (false, 28224, 1, 6160560), (false, 37026, 1, 5340720)]),
   (46754, [(true, 116162, 1, 36242544), (false, 28322, 1, 6260352), (false, 13968, 7, 270753384), (true, 36300625, 6, 218476037575)])]

theorem on_curve : rows.all (fun r => r.2.all (fun p => onC r.1 p.1 p.2.1 p.2.2.1 p.2.2.2)) = true := rfl
theorem nprods : rows.all (fun r => Nat.beq (prods (r.2.map (img r.1))).length 15) = true := rfl
theorem indep : rows.all (fun r => (prods (r.2.map (img r.1))).all (nontriv r.1)) = true := rfl

end Rank4Cert2

#print axioms Rank4Cert2.on_curve
#print axioms Rank4Cert2.nprods
#print axioms Rank4Cert2.indep

bsd/Rank4Cert3.lean

lean_all/bsd/Rank4Cert3.lean

/-
  bsd/Rank4Cert3.lean — T39 cycle 657 (goal G369): rank ≥ 4 for n = 57715, from four points whose 2-descent
  images with the torsion images span order 64 (blocks 54001–58000, tools/rank4scan.py). s₂ = 4 (MonskyCheck289),
  so with Monsky's bound (cited) the rank is 4. Lean 4 core plus bsd/Rank4Cert (prods), rfl.
-/
import bsd.Rank4Cert

namespace Rank4Cert3
open CongruentPoints5 DescentCert Rank2Cert Rank3Cert Rank4Cert

def rows : List (Nat × List (Bool × Nat × Nat × Nat)) := [
   (57715, [(true, 91035, 1, 21241500), (false, 19600, 1, 7599900), (false, 56644, 1, 2633946), (true, 187765, 1, 77423100)])]

theorem on_curve : rows.all (fun r => r.2.all (fun p => onC r.1 p.1 p.2.1 p.2.2.1 p.2.2.2)) = true := rfl
theorem nprods : rows.all (fun r => Nat.beq (prods (r.2.map (img r.1))).length 15) = true := rfl
theorem indep : rows.all (fun r => (prods (r.2.map (img r.1))).all (nontriv r.1)) = true := rfl

end Rank4Cert3

#print axioms Rank4Cert3.on_curve
#print axioms Rank4Cert3.nprods
#print axioms Rank4Cert3.indep

bsd/Reflect.lean

lean_all/bsd/Reflect.lean

/-
  bsd/Reflect.lean — T39 cycle 62: the reflection count (piece 2 of a_p = 0 at p ≡ 3 mod 4).

  Lean 4 core plus FrobChain (sumTo) and lib/Semi (lib_nat). Pairing a ↔ p − a on {1, …, p − 1}:
    sum_shift   Σ_{x<n+1} g x = g 0 + Σ_{x<n} g (x+1)
    pair_sums   (x + y + 1 = n ⇒ f x + g y = 2)  ⇒  Σ_{x<n} f + Σ_{x<n} g = 2n
    pair_count  G 0 = 1 and (a + b = n + 1, a, b > 0 ⇒ G a + G b = 2)  ⇒  Σ_{x<n+1} G x = n + 1
  With leg_pair (Supersingular) for G(x) = legV(f(x)), f odd (f(−x) = −f(x)): Σ_x legV(f(x)) = p.
-/
import FrobChain
import lib.Semi

namespace Reflect
open FrobChain

theorem sum_shift (g : Nat → Nat) : ∀ n, sumTo (n + 1) g = g 0 + sumTo n (fun x => g (x + 1))
  | 0 => by show 0 + g 0 = g 0 + 0; rw [Nat.zero_add, Nat.add_zero]
  | n + 1 => by
    show sumTo (n + 1) g + g (n + 1) = g 0 + (sumTo n (fun x => g (x + 1)) + g (n + 1))
    rw [sum_shift g n, Nat.add_assoc]

theorem pair_sums : ∀ (n : Nat) (f g : Nat → Nat), (∀ x y, x + y + 1 = n → f x + g y = 2) →
    sumTo n f + sumTo n g = 2 * n
  | 0, _, _, _ => rfl
  | n + 1, f, g, h => by
    rw [sum_shift g n]
    show (sumTo n f + f n) + (g 0 + sumTo n (fun x => g (x + 1))) = 2 * (n + 1)
    have ih := pair_sums n f (fun x => g (x + 1)) (fun x y e => h x (y + 1) (by rw [← e]; lib_nat))
    have e0 := h n 0 rfl
    have r : (sumTo n f + f n) + (g 0 + sumTo n (fun x => g (x + 1)))
        = (sumTo n f + sumTo n (fun x => g (x + 1))) + (f n + g 0) := by
      generalize sumTo n f = A; generalize sumTo n (fun x => g (x + 1)) = B
      generalize f n = C; generalize g 0 = D; lib_nat
    rw [r, ih, e0]; lib_nat

theorem double_cancel (a b : Nat) (h : a + a = b + b) : a = b := by
  have e : 2 * a = 2 * b := by
    have l : 2 * a = a + a := by lib_nat
    have r : 2 * b = b + b := by lib_nat
    rw [l, r, h]
  exact Nat.eq_of_mul_eq_mul_left (Nat.zero_lt_succ 1) e

/-- **Pairing count**: Σ_{x<n+1} G x = n + 1. -/
theorem pair_count (n : Nat) (G : Nat → Nat) (h0 : G 0 = 1)
    (hp : ∀ a b, a + b = n + 1 → 0 < a → 0 < b → G a + G b = 2) :
    sumTo (n + 1) G = n + 1 := by
  rw [sum_shift G n, h0]
  have s := pair_sums n (fun x => G (x + 1)) (fun x => G (x + 1))
    (fun x y e => hp (x + 1) (y + 1) (by rw [← e]; lib_nat) (Nat.zero_lt_succ x) (Nat.zero_lt_succ y))
  have t : sumTo n (fun x => G (x + 1)) = n := double_cancel _ _ (by rw [s]; lib_nat)
  rw [t]; lib_nat

end Reflect

#print axioms Reflect.sum_shift
#print axioms Reflect.pair_sums
#print axioms Reflect.double_cancel
#print axioms Reflect.pair_count

bsd/Res97.lean

lean_all/bsd/Res97.lean

/-
  bsd/Res97.lean — T39 (goal G289e-4): residue tables for the second descent on the coverings of E_42486, by rfl.
    sq97          −1, 2, 3, 73 are squares mod 97 (explicit roots),
    tab5_g2root   at every (x, z) mod 97, not both 0, with G2(x, z) ≡ 0: −6·G1(x, z) is not a square mod 97,
    tab5_g1root   at every (x, z) mod 97, not both 0, with G1(x, z) ≡ 0: G2(x, z) is not a square mod 97,
    sq73          −1, 2, 3, 97 are squares mod 73,
    tab7_h2root, tab7_h1root   the same for C7 (H1, H2, c = 438 = 6·73) mod 73,
    zero_sq97, zero_sq73   s² ≡ 0 ⇒ s ≡ 0.
  Residues are natural numbers below p; negative coefficients are taken mod p.
  G1 = 3X² − 16XZ − 3Z², G2 = 4X² + 3XZ − 4Z², H1 = 2X² + 9XZ − 2Z², H2 = 9X² − 8XZ − 9Z².
  Lean 4 core, rfl.
-/

set_option maxRecDepth 100000

namespace Res97

/-- is x a square mod p (x < p) -/
def isSq (p x : Nat) : Bool := (List.range p).any (fun s => Nat.beq (s * s % p) x)

/-- all pairs (x, z) mod p, not both 0 -/
def pairs (p : Nat) : List (Nat × Nat) :=
  (List.range p).flatMap (fun x => (List.range p).filterMap (fun z => if x + z == 0 then none else some (x, z)))

-- C5 mod 97: G2 ≡ 4x² + 3xz + 93z², G1 ≡ 3x² + 81xz + 94z², −6·G1 ≡ 79x² + 96xz + 18z²
def g2_97 (x z : Nat) : Nat := (4 * x * x + 3 * x * z + 93 * z * z) % 97
def g1_97 (x z : Nat) : Nat := (3 * x * x + 81 * x * z + 94 * z * z) % 97
def m6g1_97 (x z : Nat) : Nat := (79 * x * x + 96 * x * z + 18 * z * z) % 97

theorem sq97 : isSq 97 96 = true ∧ isSq 97 2 = true ∧ isSq 97 3 = true ∧ isSq 97 73 = true := ⟨rfl, rfl, rfl, rfl⟩

def tab5_g2root : Bool := (pairs 97).all (fun pr => !(Nat.beq (g2_97 pr.1 pr.2) 0) || !(isSq 97 (m6g1_97 pr.1 pr.2)))
def tab5_g1root : Bool := (pairs 97).all (fun pr => !(Nat.beq (g1_97 pr.1 pr.2) 0) || !(isSq 97 (g2_97 pr.1 pr.2)))

theorem tab5_g2root_ok : tab5_g2root = true := rfl
theorem tab5_g1root_ok : tab5_g1root = true := rfl

-- C7 mod 73: H2 ≡ 9x² + 65xz + 64z², H1 ≡ 2x² + 9xz + 71z², c·H1 = 438·H1 ≡ 0, so the unit part is 6·H1
def h2_73 (x z : Nat) : Nat := (9 * x * x + 65 * x * z + 64 * z * z) % 73
def h1_73 (x z : Nat) : Nat := (2 * x * x + 9 * x * z + 71 * z * z) % 73
def s6h1_73 (x z : Nat) : Nat := (12 * x * x + 54 * x * z + 61 * z * z) % 73

theorem sq73 : isSq 73 72 = true ∧ isSq 73 2 = true ∧ isSq 73 3 = true ∧ isSq 73 24 = true := ⟨rfl, rfl, rfl, rfl⟩

def tab7_h2root : Bool := (pairs 73).all (fun pr => !(Nat.beq (h2_73 pr.1 pr.2) 0) || !(isSq 73 (s6h1_73 pr.1 pr.2)))
def tab7_h1root : Bool := (pairs 73).all (fun pr => !(Nat.beq (h1_73 pr.1 pr.2) 0) || !(isSq 73 (h2_73 pr.1 pr.2)))

theorem tab7_h2root_ok : tab7_h2root = true := rfl
theorem tab7_h1root_ok : tab7_h1root = true := rfl

def zsq (p : Nat) : Bool := (List.range p).all (fun s => !(Nat.beq (s * s % p) 0) || Nat.beq s 0)
theorem zero_sq97 : zsq 97 = true := rfl
theorem zero_sq73 : zsq 73 = true := rfl

end Res97

#print axioms Res97.sq97
#print axioms Res97.tab5_g2root_ok
#print axioms Res97.tab5_g1root_ok
#print axioms Res97.sq73
#print axioms Res97.tab7_h2root_ok
#print axioms Res97.tab7_h1root_ok
#print axioms Res97.zero_sq97
#print axioms Res97.zero_sq73

bsd/Res97b.lean

lean_all/bsd/Res97b.lean

/-
  bsd/Res97b.lean — T39 (goal G289e-5, piece e): the residue tables of bsd/Res97 in a form that can be used in
  proofs without the core List lemmas (which bring propext): bounded quantifiers by structural recursion,
    allBelow n f   (∀ x < n, f x),      anyBelow n f   (∃ x < n, f x),
    all_below      allBelow n f = true, x < n ⇒ f x = true,
    any_below      x < n, f x = true ⇒ anyBelow n f = true,
  and the tables
    t5a   ∀ x z < 97, (x, z) ≠ (0, 0), G2(x, z) ≡ 0 ⇒ −6·G1(x, z) is not a square mod 97,
    t5b   ∀ x z < 97, (x, z) ≠ (0, 0), G1(x, z) ≡ 0 ⇒ G2(x, z) is not a square mod 97,
    t7a, t7b   the same for C7 mod 73,
  by rfl (values by core %, fast in the kernel; ModLink relates % to lib/ModN).
  Lean 4 core.
-/
set_option maxRecDepth 100000

namespace Res97b

def allBelow (f : Nat → Bool) : Nat → Bool
  | 0 => true
  | n + 1 => f n && allBelow f n

def anyBelow (f : Nat → Bool) : Nat → Bool
  | 0 => false
  | n + 1 => f n || anyBelow f n

theorem and_left {a b : Bool} (h : (a && b) = true) : a = true := by
  cases a with
  | false => exact Bool.noConfusion h
  | true => rfl
theorem and_right {a b : Bool} (h : (a && b) = true) : b = true := by
  cases a with
  | false => exact Bool.noConfusion h
  | true => exact h

theorem all_below (f : Nat → Bool) : ∀ n x, allBelow f n = true → x < n → f x = true
  | 0, _, _, hx => absurd hx (Nat.not_lt_zero _)
  | n + 1, x, h, hx => by
    cases Nat.lt_or_ge x n with
    | inl hlt => exact all_below f n x (and_right h) hlt
    | inr hge =>
      have e : x = n := Nat.le_antisymm (Nat.le_of_lt_succ hx) hge
      rw [e]; exact and_left h

theorem any_below (f : Nat → Bool) : ∀ n x, x < n → f x = true → anyBelow f n = true
  | 0, _, hx, _ => absurd hx (Nat.not_lt_zero _)
  | n + 1, x, hx, hf => by
    show (f n || anyBelow f n) = true
    cases Nat.lt_or_ge x n with
    | inl hlt => rw [any_below f n x hlt hf]; cases f n <;> rfl
    | inr hge =>
      have e : x = n := Nat.le_antisymm (Nat.le_of_lt_succ hx) hge
      rw [← e, hf]; rfl

def isSq (p m : Nat) : Bool := anyBelow (fun s => Nat.beq (s * s % p) m) p

def g2_97 (x z : Nat) : Nat := (4 * x * x + 3 * x * z + 93 * z * z) % 97
def g1_97 (x z : Nat) : Nat := (3 * x * x + 81 * x * z + 94 * z * z) % 97
def m6g1_97 (x z : Nat) : Nat := (79 * x * x + 96 * x * z + 18 * z * z) % 97
def h2_73 (x z : Nat) : Nat := (9 * x * x + 65 * x * z + 64 * z * z) % 73
def h1_73 (x z : Nat) : Nat := (2 * x * x + 9 * x * z + 71 * z * z) % 73
def s6h1_73 (x z : Nat) : Nat := (12 * x * x + 54 * x * z + 61 * z * z) % 73

def t5a : Bool := allBelow (fun x => allBelow (fun z =>
  Nat.beq (x + z) 0 || !(Nat.beq (g2_97 x z) 0) || !(isSq 97 (m6g1_97 x z))) 97) 97
def t5b : Bool := allBelow (fun x => allBelow (fun z =>
  Nat.beq (x + z) 0 || !(Nat.beq (g1_97 x z) 0) || !(isSq 97 (g2_97 x z))) 97) 97
def t7a : Bool := allBelow (fun x => allBelow (fun z =>
  Nat.beq (x + z) 0 || !(Nat.beq (h2_73 x z) 0) || !(isSq 73 (s6h1_73 x z))) 73) 73
def t7b : Bool := allBelow (fun x => allBelow (fun z =>
  Nat.beq (x + z) 0 || !(Nat.beq (h1_73 x z) 0) || !(isSq 73 (h2_73 x z))) 73) 73

theorem t5a_ok : t5a = true := rfl
theorem t5b_ok : t5b = true := rfl
theorem t7a_ok : t7a = true := rfl
theorem t7b_ok : t7b = true := rfl

end Res97b

#print axioms Res97b.and_left
#print axioms Res97b.and_right
#print axioms Res97b.all_below
#print axioms Res97b.any_below
#print axioms Res97b.t5a_ok
#print axioms Res97b.t5b_ok
#print axioms Res97b.t7a_ok
#print axioms Res97b.t7b_ok

bsd/ResDiv.lean

lean_all/bsd/ResDiv.lean

/-
  bsd/ResDiv.lean — T39 (goal G289e-3): common divisors of the two quadratic factors of the coverings C5, C7 of
  E_42486. Resultant identities (coefficients from tools/g289e_res.py):
    c5_res_x   (3X − 4Z)·G1 + (16X + 3Z)·G2 = 73·X³,      c5_res_z   (−4X − 3Z)·G1 + (3X − 16Z)·G2 = 73·Z³,
    c7_res_x   (8X + 9Z)·H1 + (9X − 2Z)·H2 = 97·X³,       c7_res_z   (9X − 8Z)·H1 + (−2X − 9Z)·H2 = 97·Z³,
  with G1 = 3X² − 16XZ − 3Z², G2 = 4X² + 3XZ − 4Z², H1 = 2X² + 9XZ − 2Z², H2 = 9X² − 8XZ − 9Z²; and with
  uX + vZ = 1 (X, Z coprime), (uX + vZ)⁵ = 1 splits into multiples of X³ and Z³, so
    c5_gcd     d ∣ G1, d ∣ G2 ⇒ d ∣ 73,      c7_gcd     d ∣ H1, d ∣ H2 ⇒ d ∣ 97.
  Lean 4 core plus lib/Gcd (IDv, idv_lin), lib/IntRing (lib_int).
-/
import lib.Gcd

namespace ResDiv
open LibGcd LibInt

def G1 (X Z : Int) : Int := 3 * X * X - 16 * X * Z - 3 * Z * Z
def G2 (X Z : Int) : Int := 4 * X * X + 3 * X * Z - 4 * Z * Z
def H1 (X Z : Int) : Int := 2 * X * X + 9 * X * Z - 2 * Z * Z
def H2 (X Z : Int) : Int := 9 * X * X - 8 * X * Z - 9 * Z * Z

theorem c5_res_x (X Z : Int) : (3 * X - 4 * Z) * G1 X Z + (16 * X + 3 * Z) * G2 X Z = 73 * (X * X * X) := by
  show (3 * X - 4 * Z) * (3 * X * X - 16 * X * Z - 3 * Z * Z) + (16 * X + 3 * Z) * (4 * X * X + 3 * X * Z - 4 * Z * Z)
    = 73 * (X * X * X)
  lib_int
theorem c5_res_z (X Z : Int) : (-4 * X - 3 * Z) * G1 X Z + (3 * X - 16 * Z) * G2 X Z = 73 * (Z * Z * Z) := by
  show (-4 * X - 3 * Z) * (3 * X * X - 16 * X * Z - 3 * Z * Z) + (3 * X - 16 * Z) * (4 * X * X + 3 * X * Z - 4 * Z * Z)
    = 73 * (Z * Z * Z)
  lib_int
theorem c7_res_x (X Z : Int) : (8 * X + 9 * Z) * H1 X Z + (9 * X - 2 * Z) * H2 X Z = 97 * (X * X * X) := by
  show (8 * X + 9 * Z) * (2 * X * X + 9 * X * Z - 2 * Z * Z) + (9 * X - 2 * Z) * (9 * X * X - 8 * X * Z - 9 * Z * Z)
    = 97 * (X * X * X)
  lib_int
theorem c7_res_z (X Z : Int) : (9 * X - 8 * Z) * H1 X Z + (-2 * X - 9 * Z) * H2 X Z = 97 * (Z * Z * Z) := by
  show (9 * X - 8 * Z) * (2 * X * X + 9 * X * Z - 2 * Z * Z) + (-2 * X - 9 * Z) * (9 * X * X - 8 * X * Z - 9 * Z * Z)
    = 97 * (Z * Z * Z)
  lib_int

/-- (uX + vZ)⁵ = X³·P + Z³·Q -/
theorem fifth (X Z u v : Int) :
    (u * X + v * Z) * (u * X + v * Z) * (u * X + v * Z) * (u * X + v * Z) * (u * X + v * Z)
      = (X * X * X) * (u * u * u * u * u * X * X + 5 * u * u * u * u * v * X * Z + 10 * u * u * u * v * v * Z * Z)
        + (Z * Z * Z) * (10 * u * u * v * v * v * X * X + 5 * u * v * v * v * v * X * Z + v * v * v * v * v * Z * Z) := by
  lib_int

/-- d ∣ R·X³, d ∣ R·Z³, uX + vZ = 1 ⇒ d ∣ R -/
theorem dvd_of_cubes (X Z u v d R : Int) (huv : u * X + v * Z = 1) (hx : IDv d (R * (X * X * X)))
    (hz : IDv d (R * (Z * Z * Z))) : IDv d R := by
  obtain ⟨c, hc⟩ := idv_lin hx hz (u * u * u * u * u * X * X + 5 * u * u * u * u * v * X * Z + 10 * u * u * u * v * v * Z * Z)
    (10 * u * u * v * v * v * X * X + 5 * u * v * v * v * v * X * Z + v * v * v * v * v * Z * Z)
  refine ⟨c, ?_⟩
  rw [← hc]
  have h5 := fifth X Z u v
  rw [huv] at h5
  calc R = R * (1 * 1 * 1 * 1 * 1) := by lib_int
    _ = R * ((X * X * X) * (u * u * u * u * u * X * X + 5 * u * u * u * u * v * X * Z + 10 * u * u * u * v * v * Z * Z)
        + (Z * Z * Z) * (10 * u * u * v * v * v * X * X + 5 * u * v * v * v * v * X * Z + v * v * v * v * v * Z * Z)) := by
          rw [h5]
    _ = _ := by lib_int

/-- **C5: a common divisor of G1(X, Z), G2(X, Z) with X, Z coprime divides 73** -/
theorem c5_gcd (X Z u v d : Int) (huv : u * X + v * Z = 1) (h1 : IDv d (G1 X Z)) (h2 : IDv d (G2 X Z)) :
    IDv d 73 := by
  have hx := idv_lin h1 h2 (3 * X - 4 * Z) (16 * X + 3 * Z)
  have hz := idv_lin h1 h2 (-4 * X - 3 * Z) (3 * X - 16 * Z)
  rw [c5_res_x] at hx
  rw [c5_res_z] at hz
  exact dvd_of_cubes X Z u v d 73 huv hx hz

/-- **C7: a common divisor of H1(X, Z), H2(X, Z) with X, Z coprime divides 97** -/
theorem c7_gcd (X Z u v d : Int) (huv : u * X + v * Z = 1) (h1 : IDv d (H1 X Z)) (h2 : IDv d (H2 X Z)) :
    IDv d 97 := by
  have hx := idv_lin h1 h2 (8 * X + 9 * Z) (9 * X - 2 * Z)
  have hz := idv_lin h1 h2 (9 * X - 8 * Z) (-2 * X - 9 * Z)
  rw [c7_res_x] at hx
  rw [c7_res_z] at hz
  exact dvd_of_cubes X Z u v d 97 huv hx hz

end ResDiv

#print axioms ResDiv.c5_res_x
#print axioms ResDiv.c5_res_z
#print axioms ResDiv.c7_res_x
#print axioms ResDiv.c7_res_z
#print axioms ResDiv.fifth
#print axioms ResDiv.dvd_of_cubes
#print axioms ResDiv.c5_gcd
#print axioms ResDiv.c7_gcd

bsd/ResUnique.lean

lean_all/bsd/ResUnique.lean

/-
  bsd/ResUnique.lean — T39 (goal G289e-5, piece d): residues below p that are congruent mod p are equal,
    res_unique   a, b < p, p ∣ (a − b) in ℤ ⇒ a = b,
    idv_trans    p ∣ (A − B), p ∣ (B − C) ⇒ p ∣ (A − C),
  used to pass from integer congruences (QuadShift) to the residue tables of bsd/Res97.
  Lean 4 core plus lib/Gcd (IDv), lib/ModN (div_unique), lib/IntRing (lib_int).
-/
import lib.Gcd
import lib.ModN

namespace ResUnique
open LibGcd LibInt

theorem idv_trans (p A B C : Int) (h1 : IDv p (A - B)) (h2 : IDv p (B - C)) : IDv p (A - C) := by
  obtain ⟨c1, e1⟩ := h1
  obtain ⟨c2, e2⟩ := h2
  exact ⟨c1 + c2, by
    calc A - C = (A - B) + (B - C) := by lib_int
      _ = p * c1 + p * c2 := by rw [e1, e2]
      _ = p * (c1 + c2) := by lib_int⟩

/-- b = a + p·(k + 1) with b < p is impossible -/
theorem big_ne (a b p k : Nat) (hb : b < p) (h : b = a + p * (k + 1)) : False := by
  have h1 : p ≤ b := by
    rw [h, show a + p * (k + 1) = p + (a + p * k) by lib_nat]
    exact le_add' p (a + p * k)
  exact Nat.lt_irrefl b (Nat.lt_of_lt_of_le hb h1)

/-- **a, b < p, p ∣ (a − b) ⇒ a = b** -/
theorem res_unique (p a b : Nat) (ha : a < p) (hb : b < p) (h : IDv (Int.ofNat p) (Int.ofNat a - Int.ofNat b)) :
    a = b := by
  obtain ⟨c, hc⟩ := h
  cases c with
  | ofNat k =>
    have e : Int.ofNat a = Int.ofNat (p * k + b) := by
      show Int.ofNat a = Int.ofNat p * Int.ofNat k + Int.ofNat b
      calc Int.ofNat a = (Int.ofNat a - Int.ofNat b) + Int.ofNat b := by lib_int
        _ = Int.ofNat p * Int.ofNat k + Int.ofNat b := by rw [hc]
    have e' := Int.ofNat.inj e
    exact ((LibMod.div_unique 0 k a b ha hb (by rw [e']; lib_nat)).2)
  | negSucc k =>
    have e : Int.ofNat b = Int.ofNat (a + p * (k + 1)) := by
      show Int.ofNat b = Int.ofNat a + Int.ofNat p * (Int.ofNat k + 1)
      calc Int.ofNat b = Int.ofNat a - (Int.ofNat a - Int.ofNat b) := by lib_int
        _ = Int.ofNat a - Int.ofNat p * Int.negSucc k := by rw [hc]
        _ = Int.ofNat a - Int.ofNat p * (-(Int.ofNat k + 1)) := rfl
        _ = Int.ofNat a + Int.ofNat p * (Int.ofNat k + 1) := by lib_int
    exact (big_ne a b p k hb (Int.ofNat.inj e)).elim

end ResUnique

#print axioms ResUnique.idv_trans
#print axioms ResUnique.big_ne
#print axioms ResUnique.res_unique

bsd/ShaParity.lean

lean_all/bsd/ShaParity.lean

/-
  bsd/ShaParity.lean — T39 cycle 181: Ш_an against Monsky's 2-Selmer rank, all rank-0 squarefree n < 800.

  Lean 4 core plus bsd/MonskyCheck (s2), rfl. For the 197 squarefree n < 800 of computed analytic rank 0,
  the table lists (n, s₂(n), j) with #Ш_an(E_n) = j² (NUMERIC_BSD_CONGRUENT{,2,3,4}.jsonl, rounded; the float
  values are within 10⁻⁶ of these squares). Checked:
    s2_ok        s₂(n) is Monsky's 2-Selmer rank, recomputed here for every n
    sha_parity   s₂ = 0 ⇒ #Ш_an odd;   2^{s₂} ∣ #Ш_an  (BSD + Cassels–Tate predict Ш[2] ≅ (Z/2)^{s₂} in rank 0)
  Observed: s₂ ∈ {0, 2}; #Ш_an ∈ {1, 9, 25, 49} when s₂ = 0 and ∈ {4, 16, 36} when s₂ = 2.
-/
import bsd.MonskyCheck

namespace ShaParity
open MonskyCheck

def tbl : List (Nat × Nat × Nat) := [(1, 0, 1), (2, 0, 1), (3, 0, 1), (10, 0, 1), (11, 0, 1), (17, 2, 2), (19, 0, 1), (26, 0, 1), (33, 0, 1), (35, 0, 1), (42, 0, 1), (43, 0, 3), (51, 0, 1), (57, 0, 1), (58, 0, 1), (59, 0, 1), (66, 0, 1), (67, 0, 1), (73, 2, 2), (74, 0, 1), (82, 2, 2), (83, 0, 1), (89, 2, 2), (91, 0, 1), (97, 2, 2), (105, 0, 1), (106, 0, 1), (107, 0, 3), (113, 2, 4), (114, 0, 1), (  -- (+2169 chars; full line in the Zenodo source)

set_option maxRecDepth 200000 in
set_option maxHeartbeats 0 in
theorem s2_ok : tbl.all (fun (n, k, _) => Nat.beq (s2 n) k) = true := rfl

set_option maxRecDepth 200000 in
theorem sha_parity : tbl.all (fun (_, k, j) =>
    Nat.beq ((j * j) % (2 ^ k)) 0 && (!(Nat.beq k 0) || Nat.beq ((j * j) % 2) 1)) = true := rfl

set_option maxRecDepth 200000 in
theorem count : tbl.length = 197 := rfl

end ShaParity

#print axioms ShaParity.s2_ok
#print axioms ShaParity.sha_parity
#print axioms ShaParity.count

bsd/ShaParity2.lean

lean_all/bsd/ShaParity2.lean

/-
  bsd/ShaParity2.lean — T39 cycle 187: Ш_an against Monsky's 2-Selmer rank, all rank-0 squarefree 801 ≤ n ≤ 1000.

  Lean 4 core plus bsd/MonskyCheck (s2), rfl. For the 50 squarefree n in 801–1000 of computed analytic rank 0,
  the table lists (n, s₂(n), j) with #Ш_an(E_n) = j² (NUMERIC_BSD_CONGRUENT5.jsonl, rounded; the float values are
  within 10⁻⁶ of these squares). Checked as in ShaParity:
    s2_ok        s₂(n) is Monsky's 2-Selmer rank, recomputed here for every n
    sha_parity   s₂ = 0 ⇒ #Ш_an odd;   2^{s₂} ∣ #Ш_an
  Observed (s₂, #Ш_an): (0, 1) 22, (0, 9) 6, (0, 25) 3, (0, 81) 1, (2, 4) 11, (2, 16) 7.
-/
import bsd.MonskyCheck

namespace ShaParity2
open MonskyCheck

def tbl : List (Nat × Nat × Nat) := [(802, 2, 4), (803, 0, 1), (809, 2, 4), (811, 0, 1), (817, 0, 1), (818, 2, 2), (826, 0, 1), (827, 0, 1), (834, 0, 1), (835, 0, 1), (842, 0, 3), (843, 0, 1), (849, 0, 3), (851, 0, 1), (857, 2, 4), (858, 0, 1), (859, 0, 5), (865, 2, 2), (874, 0, 1), (881, 2, 4), (883, 0, 3), (897, 2, 2), (898, 2, 4), (899, 0, 1), (906, 0, 1), (907, 0, 9), (913, 0, 3), (914, 2, 2),  -- (+286 chars; full line in the Zenodo source)

set_option maxRecDepth 200000 in
set_option maxHeartbeats 0 in
theorem s2_ok : tbl.all (fun (n, k, _) => Nat.beq (s2 n) k) = true := rfl

set_option maxRecDepth 200000 in
theorem sha_parity : tbl.all (fun (_, k, j) =>
    Nat.beq ((j * j) % (2 ^ k)) 0 && (!(Nat.beq k 0) || Nat.beq ((j * j) % 2) 1)) = true := rfl

set_option maxRecDepth 200000 in
theorem count : tbl.length = 50 := rfl

end ShaParity2

#print axioms ShaParity2.s2_ok
#print axioms ShaParity2.sha_parity
#print axioms ShaParity2.count

bsd/SignObs.lean

lean_all/bsd/SignObs.lean

/-
  bsd/SignObs.lean — T39 (goal G289e-6f): the sign obstruction at ∞ for the classes (1, −D) of E_n (n = 42486).
  If X = U², X − nZ² = −D·V² (D ≥ 0) and Y² = X(X − nZ²)(X + nZ²), then Y² = −(natural number), so the product is 0.
    sign_obs    X = U², X − nZ² = −D·V²  ⇒  X(X − nZ²)(X + nZ²) = 0
  Lean 4 core plus bsd/SqRes (PM, pm_exists, pm_sq, neg_zero).
-/
import bsd.SqRes

set_option maxRecDepth 100000

namespace SignObs
open LibInt SqRes

theorem sign_obs (X Z Y U V : Int) (D : Nat) (hX : X = U * U) (hXm : X - 42486 * (Z * Z) = -Int.ofNat D * (V * V))
    (h : Y * Y = X * ((X - 42486 * (Z * Z)) * (X + 42486 * (Z * Z)))) :
    X * ((X - 42486 * (Z * Z)) * (X + 42486 * (Z * Z))) = 0 := by
  obtain ⟨un, hu⟩ := pm_exists U
  obtain ⟨vn, hv⟩ := pm_exists V
  obtain ⟨zn, hz⟩ := pm_exists Z
  obtain ⟨yn, hy⟩ := pm_exists Y
  have eU := pm_sq hu
  have eV := pm_sq hv
  have eZ := pm_sq hz
  -- X + nZ² = U² + nZ² ≥ 0
  have hXp : X + 42486 * (Z * Z) = Int.ofNat (un * un + 42486 * (zn * zn)) := by
    rw [hX, eU, eZ]; lib_int
  have e : Int.ofNat (yn * yn) = -Int.ofNat (un * un * (D * (vn * vn) * (un * un + 42486 * (zn * zn)))) := by
    rw [← pm_sq hy, h, hXp, hXm, hX, eU, eV]; lib_int
  obtain ⟨e1, _⟩ := neg_zero e
  rw [← h, pm_sq hy, e1]; rfl

end SignObs

#print axioms SignObs.sign_obs

bsd/SqClass.lean

lean_all/bsd/SqClass.lean

/-
  bsd/SqClass.lean — T39 (goal G289e-6f): square classes supported on {−1, 2, 3, 73, 97} as bit vectors.
    fb p b            p if b, else 1
    cls s a b c d     (−1)^s · 2^a · 3^b · 73^c · 97^d
    fb_mul            fb p b · fb p b′ = fb p (b xor b′) · (fb p (b ∧ b′))²
    cls_mul           cls(β) · cls(β′) = cls(β xor β′) · (cls(β ∧ β′))²            (the class map is a homomorphism)
    cls_ne_zero       cls β ≠ 0
  Lean 4 core plus lib/IntRing.
-/
import lib.IntRing

namespace SqClass
open LibInt

def fb (p : Int) : Bool → Int
  | true => p
  | false => 1

def cls (s a b c d : Bool) : Int := fb (-1) s * fb 2 a * fb 3 b * fb 73 c * fb 97 d

theorem fb_mul (p : Int) : ∀ b b' : Bool, fb p b * fb p b' = fb p (xor b b') * (fb p (b && b') * fb p (b && b'))
  | true, true => by show p * p = 1 * (p * p); lib_int
  | true, false => by show p * 1 = p * (1 * 1); lib_int
  | false, true => by show 1 * p = p * (1 * 1); lib_int
  | false, false => by show (1 : Int) * 1 = 1 * (1 * 1); lib_int

theorem prod5 (A1 A2 A3 A4 A5 B1 B2 B3 B4 B5 : Int) :
    A1 * A2 * A3 * A4 * A5 * (B1 * B2 * B3 * B4 * B5) = (A1 * B1) * (A2 * B2) * (A3 * B3) * (A4 * B4) * (A5 * B5) := by
  lib_int

theorem prod5sq (C1 C2 C3 C4 C5 D1 D2 D3 D4 D5 : Int) :
    (C1 * (D1 * D1)) * (C2 * (D2 * D2)) * (C3 * (D3 * D3)) * (C4 * (D4 * D4)) * (C5 * (D5 * D5))
      = (C1 * C2 * C3 * C4 * C5) * ((D1 * D2 * D3 * D4 * D5) * (D1 * D2 * D3 * D4 * D5)) := by
  lib_int

/-- **The class map is multiplicative modulo squares.** -/
theorem cls_mul (s a b c d s' a' b' c' d' : Bool) :
    cls s a b c d * cls s' a' b' c' d'
      = cls (xor s s') (xor a a') (xor b b') (xor c c') (xor d d')
        * (cls (s && s') (a && a') (b && b') (c && c') (d && d') * cls (s && s') (a && a') (b && b') (c && c') (d && d')) := by
  unfold cls
  rw [prod5, fb_mul (-1) s s', fb_mul 2 a a', fb_mul 3 b b', fb_mul 73 c c', fb_mul 97 d d', prod5sq]

theorem fb_ne_zero (p : Int) (hp : p ≠ 0) : ∀ b : Bool, fb p b ≠ 0
  | true => hp
  | false => fun h => Nat.noConfusion (Int.ofNat.inj h)

theorem mul_ne_zero' {x y : Int} (hx : x ≠ 0) (hy : y ≠ 0) : x * y ≠ 0 := fun h => by
  cases int_noZD x y h with
  | inl h1 => exact hx h1
  | inr h1 => exact hy h1

theorem cls_ne_zero (s a b c d : Bool) : cls s a b c d ≠ 0 := by
  unfold cls
  refine mul_ne_zero' (mul_ne_zero' (mul_ne_zero' (mul_ne_zero' ?_ ?_) ?_) ?_) ?_
  · exact fb_ne_zero (-1) (fun h => Int.noConfusion h) s
  · exact fb_ne_zero 2 (fun h => Nat.noConfusion (Int.ofNat.inj h)) a
  · exact fb_ne_zero 3 (fun h => Nat.noConfusion (Int.ofNat.inj h)) b
  · exact fb_ne_zero 73 (fun h => Nat.noConfusion (Int.ofNat.inj h)) c
  · exact fb_ne_zero 97 (fun h => Nat.noConfusion (Int.ofNat.inj h)) d

end SqClass

#print axioms SqClass.fb_mul
#print axioms SqClass.prod5
#print axioms SqClass.prod5sq
#print axioms SqClass.cls_mul
#print axioms SqClass.fb_ne_zero
#print axioms SqClass.mul_ne_zero'
#print axioms SqClass.cls_ne_zero

bsd/SqClassL.lean

lean_all/bsd/SqClassL.lean

/-
  bsd/SqClassL.lean — T39 (goal G289f, tool): products of square classes over a list of primes, and the n-free
  bits_eq (bsd/BitsEq fixes the primes 2, 3, 73, 97).
    xorL, andL           bitwise xor / and of bit lists
    rearr                (aA)(a′A′) = (xX)((yY)(yY)) from aa′ = x y², AA′ = X Y²
    prodL_mul, clsL_mul  cls β · cls β′ = cls (β ⊕ β′) · cls (β ∧ β′)²
    posL, prodL_ne, clsL_ne    a class over positive primes is nonzero
    xor_allF             allF (xorL bs bs′) = true, equal lengths  ⇒  bs = bs′
    bits_eqL             chkL ps, cls β · P² = cls β′ · Q², P ≠ 0  ⇒  β = β′
  Lean 4 core plus bsd/SqUniqueL, bsd/SqNat, bsd/BitsEq.
-/
import bsd.SqUniqueL
import bsd.SqNat
import bsd.BitsEq

namespace SqClassL
open LibInt SqClass SqUniqueL

def xorL : List Bool → List Bool → List Bool
  | b :: bs, b' :: bs' => xor b b' :: xorL bs bs'
  | [], _ => []
  | _ :: _, [] => []

def andL : List Bool → List Bool → List Bool
  | b :: bs, b' :: bs' => (b && b') :: andL bs bs'
  | [], _ => []
  | _ :: _, [] => []

theorem rearr (a a' A A' x y X Y : Int) (h1 : a * a' = x * (y * y)) (h2 : A * A' = X * (Y * Y)) :
    (a * A) * (a' * A') = (x * X) * ((y * Y) * (y * Y)) :=
  calc (a * A) * (a' * A') = (a * a') * (A * A') := by lib_int
    _ = (x * (y * y)) * (X * (Y * Y)) := by rw [h1, h2]
    _ = (x * X) * ((y * Y) * (y * Y)) := by lib_int

theorem one_mul_one : (1 : Int) * 1 = 1 * (1 * 1) := rfl

theorem prodL_mul : ∀ (ps : List Nat) (bs bs' : List Bool), bs.length = ps.length → bs'.length = ps.length →
    prodL ps bs * prodL ps bs' = prodL ps (xorL bs bs') * (prodL ps (andL bs bs') * prodL ps (andL bs bs'))
  | [], [], [], _, _ => one_mul_one
  | [], _ :: _, _, h, _ => Nat.noConfusion h
  | [], [], _ :: _, _, h => Nat.noConfusion h
  | _ :: _, [], _, h, _ => Nat.noConfusion h
  | _ :: _, _ :: _, [], _, h => Nat.noConfusion h
  | p :: ps, b :: bs, b' :: bs', h, h' =>
    rearr _ _ _ _ _ _ _ _ (fb_mul (Int.ofNat p) b b') (prodL_mul ps bs bs' (Nat.succ.inj h) (Nat.succ.inj h'))

theorem clsL_mul (ps : List Nat) (s s' : Bool) (bs bs' : List Bool) (h : bs.length = ps.length)
    (h' : bs'.length = ps.length) :
    clsL s ps bs * clsL s' ps bs'
      = clsL (xor s s') ps (xorL bs bs') * (clsL (s && s') ps (andL bs bs') * clsL (s && s') ps (andL bs bs')) :=
  rearr _ _ _ _ _ _ _ _ (fb_mul (-1) s s') (prodL_mul ps bs bs' h h')

def posL : List Nat → Bool
  | [] => true
  | 0 :: _ => false
  | (_ + 1) :: ps => posL ps

theorem one_ne : (1 : Int) ≠ 0 := fun h => Nat.noConfusion (Int.ofNat.inj h)

theorem prodL_ne : ∀ (ps : List Nat) (bs : List Bool), posL ps = true → prodL ps bs ≠ 0
  | [], _, _ => one_ne
  | 0 :: _, _, h => Bool.noConfusion h
  | (_ + 1) :: _, [], _ => one_ne
  | (p + 1) :: ps, b :: bs, h =>
    mul_ne_zero' (fb_ne_zero (Int.ofNat (p + 1)) (fun e => Nat.noConfusion (Int.ofNat.inj e)) b) (prodL_ne ps bs h)

theorem clsL_ne (ps : List Nat) (hp : posL ps = true) (s : Bool) (bs : List Bool) : clsL s ps bs ≠ 0 :=
  mul_ne_zero' (fb_ne_zero (-1) (fun h => Int.noConfusion h) s) (prodL_ne ps bs hp)

theorem xor_allF : ∀ (bs bs' : List Bool), bs.length = bs'.length → allF (xorL bs bs') = true → bs = bs'
  | [], [], _, _ => rfl
  | [], _ :: _, h, _ => Nat.noConfusion h
  | _ :: _, [], h, _ => Nat.noConfusion h
  | b :: bs, b' :: bs', h, ha => by
    have e1 : b = b' := BitsEq.xor_false b b' (by
      cases e : xor b b' with
      | false => rfl
      | true => exact absurd (and_l ha) (by rw [e]; exact fun h => Bool.noConfusion h))
    have e2 : bs = bs' := xor_allF bs bs' (Nat.succ.inj h) (and_r ha)
    rw [e1, e2]

theorem xorL_len : ∀ (bs bs' : List Bool), bs.length = bs'.length → (xorL bs bs').length = bs.length
  | [], _, _ => rfl
  | _ :: _, [], h => Nat.noConfusion h
  | _ :: bs, _ :: bs', h => congrArg Nat.succ (xorL_len bs bs' (Nat.succ.inj h))

/-- **Equal classes have equal bits** (any certified list of positive primes). -/
theorem bits_eqL (ps : List Nat) (hc : chkL ps = true) (hp : posL ps = true) (s s' : Bool) (bs bs' : List Bool)
    (hl : bs.length = ps.length) (hl' : bs'.length = ps.length) (P Q : Int) (hP : P ≠ 0)
    (h : clsL s ps bs * (P * P) = clsL s' ps bs' * (Q * Q)) : s = s' ∧ bs = bs' := by
  have h1 : (clsL s ps bs * clsL s' ps bs') * (P * P) = (clsL s' ps bs' * Q) * (clsL s' ps bs' * Q) := by
    calc (clsL s ps bs * clsL s' ps bs') * (P * P) = clsL s' ps bs' * (clsL s ps bs * (P * P)) := by lib_int
      _ = clsL s' ps bs' * (clsL s' ps bs' * (Q * Q)) := by rw [h]
      _ = _ := by lib_int
  obtain ⟨m, hm⟩ := SqNat.int_sq_of_mul_sq _ _ _ hP h1
  rw [clsL_mul ps s s' bs bs' hl hl'] at hm
  obtain ⟨m', hm'⟩ := SqNat.int_sq_of_mul_sq _ m _ (clsL_ne ps hp _ _) hm
  have hx : (xorL bs bs').length = ps.length := (xorL_len bs bs' (hl.trans hl'.symm)).trans hl
  obtain ⟨e1, e2⟩ := cls_sqL ps hc _ _ hx m' hm'
  exact ⟨BitsEq.xor_false _ _ e1, xor_allF bs bs' (hl.trans hl'.symm) e2⟩

end SqClassL

#print axioms SqClassL.rearr
#print axioms SqClassL.one_mul_one
#print axioms SqClassL.prodL_mul
#print axioms SqClassL.clsL_mul
#print axioms SqClassL.one_ne
#print axioms SqClassL.prodL_ne
#print axioms SqClassL.clsL_ne
#print axioms SqClassL.xor_allF
#print axioms SqClassL.xorL_len
#print axioms SqClassL.bits_eqL

bsd/SqCount.lean

lean_all/bsd/SqCount.lean

/-
  bsd/SqCount.lean — T39 cycle 65: the number of square roots, #{y < p : y² ≡ d} = legV(d) = 1 + (d|p).

  Lean 4 core plus ESChain (legV, eqI, sum_eqI, cntY tools) and QRChain (roots_le, legB_iff), adapting
  ESChain.cnt_sq (which counts y² + y ≡ c) to the pure square. p = h + h + 1 an odd prime.
    sq_zero      y < p, y² ≡ 0 ⇒ y = 0
    poly_root2   y² ≡ d ⇒ y is a root of X² + (p − d)
    partner2     y0 + y1 = p, y0² ≡ d ⇒ y1² ≡ d
    sq_count     Σ_{y<p} [y² ≡ d] = legV(d)
-/
import ESChain

namespace SqCount
open FTAChain FrobChain QRChain ESChain

def sqCnt (p d : Nat) : Nat := sumTo p (fun y => b2n (Nat.beq (rem p (y * y)) (rem p d)))

theorem sq_zero (h y : Nat) (hp : Pr (h + h + 1)) (hy : y < h + h + 1)
    (e : rem (h + h + 1) (y * y) = 0) : y = 0 := by
  have hdv : Dv (h + h + 1) y :=
    match euclid _ _ _ hp (dv_of_rem_zero _ _ e) with
    | Or.inl d => d
    | Or.inr d => d
  match y, hdv with
  | 0, _ => rfl
  | k + 1, d => exact absurd d (fun d' => not_dv_of_lt _ _ (Nat.zero_lt_succ k) hy d')

theorem poly_root2 (h d y : Nat) (e : rem (h + h + 1) (y * y) = rem (h + h + 1) d) :
    rem (h + h + 1) (ev y [1, 0, (h + h + 1) - rem (h + h + 1) d]) = 0 := by
  show rem (h + h + 1) (1 * (1 * y * y) + (0 * (1 * y) + (((h + h + 1) - rem (h + h + 1) d) * 1 + 0))) = 0
  rw [Nat.one_mul, Nat.one_mul, Nat.zero_mul, Nat.zero_add, Nat.mul_one, Nat.add_zero]
  rw [rem_add_congr _ _ (rem (h + h + 1) d) _ (e.trans (rem_idem _ d).symm), Nat.add_zero ((h + h + 1) - rem (h + h + 1) d),
    add_sub_of_le' _ _ (Nat.le_of_lt (rem_lt' _ d))]
  exact rem_zero_of_dv _ _ (dv_refl _)

theorem at_most_two2 (h d y y0 y1 : Nat) (hp : Pr (h + h + 1))
    (hy : y < h + h + 1) (hy0 : y0 < h + h + 1) (hy1 : y1 < h + h + 1) (h01 : y0 ≠ y1)
    (e : rem (h + h + 1) (y * y) = rem (h + h + 1) d)
    (e0 : rem (h + h + 1) (y0 * y0) = rem (h + h + 1) d)
    (e1 : rem (h + h + 1) (y1 * y1) = rem (h + h + 1) d) : y = y0 ∨ y = y1 :=
  match Nat.decEq y y0, Nat.decEq y y1 with
  | isTrue a, _ => Or.inl a
  | _, isTrue b => Or.inr b
  | isFalse a, isFalse b => by
    have hle := roots_le (h + h) hp 1 [0, (h + h + 1) - rem (h + h + 1) d] [y, y0, y1]
      (not_dv_of_lt _ 1 Nat.zero_lt_one (Nat.succ_lt_succ (hh_pos h hp)))
      ⟨fun hm => match hm with
        | Or.inl x => a x
        | Or.inr (Or.inl x) => b x,
       fun hm => match hm with
        | Or.inl x => h01 x,
       fun hm => hm, True.intro⟩
      ⟨⟨hy, poly_root2 h d y e⟩, ⟨hy0, poly_root2 h d y0 e0⟩, ⟨hy1, poly_root2 h d y1 e1⟩, True.intro⟩
    exact absurd hle (Nat.not_succ_le_self 2)

theorem partner2 (h d y0 y1 : Nat) (hs : y0 + y1 = h + h + 1)
    (e0 : rem (h + h + 1) (y0 * y0) = rem (h + h + 1) d) :
    rem (h + h + 1) (y1 * y1) = rem (h + h + 1) d := by
  rw [← e0]
  apply rem_cancel_add (h + h) _ _ (y1 * y0)
  have a1 : y1 * y1 + y1 * y0 = y1 * (h + h + 1) := by rw [← hs, nleft_distrib, Nat.add_comm]
  have a2 : y0 * y0 + y1 * y0 = y0 * (h + h + 1) := by rw [← hs, nleft_distrib, nmul_comm y1 y0]
  rw [a1, a2, rem_zero_of_dv (h + h) (y1 * (h + h + 1)) ⟨y1, rfl⟩,
    rem_zero_of_dv (h + h) (y0 * (h + h + 1)) ⟨y0, rfl⟩]

/-- **Square roots**: #{y < p : y² ≡ d} = legV(d). -/
theorem sq_count (h d : Nat) (hp : Pr (h + h + 1)) :
    sqCnt (h + h + 1) d = legV (h + h + 1) d := by
  unfold sqCnt legV
  match Nat.decEq (rem (h + h + 1) d) 0 with
  | isTrue hd =>
    rw [ite_eq_left hd]
    rw [sum_ext _ _ (fun y => eqI y 0) (fun y hy => by
      rw [b2n_beq]
      match Nat.decEq (rem (h + h + 1) (y * y)) (rem (h + h + 1) d) with
      | isTrue e2 =>
        rw [ite_eq_left e2, show eqI y 0 = 1 from ite_eq_left (sq_zero h y hp hy (e2.trans hd))]
      | isFalse n2 =>
        rw [ite_eq_right n2, show eqI y 0 = 0 from
          ite_eq_right (fun e3 => n2 (by rw [e3, hd]; exact rem_of_lt _ _ (Nat.zero_lt_succ _)))])]
    exact sum_eqI 0 _ (Nat.zero_lt_succ _)
  | isFalse hd =>
    rw [ite_eq_right hd]
    cases hl : legB (h + h + 1) d with
    | false =>
      show _ = 0
      exact sum_zero _ _ (fun y _ => by
        rw [b2n_beq]
        exact ite_eq_right (fun e => Bool.noConfusion (hl.symm.trans ((legB_iff (h + h) d).mpr ⟨y, e⟩))))
    | true =>
      show _ = 2
      have ⟨z0, hz0⟩ := (legB_iff (h + h) _).mp hl
      let y0 := rem (h + h + 1) z0
      have hy0 : y0 < h + h + 1 := rem_lt' _ _
      have e0 : rem (h + h + 1) (y0 * y0) = rem (h + h + 1) d :=
        (rem_mul_congr2 _ _ _ _ _ (rem_idem _ z0) (rem_idem _ z0)).trans hz0
      have y0pos : y0 ≠ 0 := fun z => hd (by rw [← e0, z]; rfl)
      have ⟨y1, hy1s⟩ := Nat.le.dest (Nat.le_of_lt hy0)
      have hs : y0 + y1 = h + h + 1 := hy1s
      have hy1 : y1 < h + h + 1 := by
        cases hk : y0 with
        | zero => exact absurd hk y0pos
        | succ k =>
          rw [hk] at hs
          rw [← hs]
          exact Nat.lt_of_lt_of_le (Nat.lt_succ_self y1) (by rw [Nat.succ_add]; exact Nat.succ_le_succ (Nat.le_add_left y1 k))
      have e1 := partner2 h d y0 y1 hs e0
      have h01 : y0 ≠ y1 := fun e => by
        rw [← e] at hs
        exact absurd (congrArg par hs) (by rw [par_double, par_p]; exact Bool.noConfusion)
      rw [sum_ext _ _ (fun y => eqI y y0 + eqI y y1) (fun y hy => by
        rw [b2n_beq]
        match Nat.decEq (rem (h + h + 1) (y * y)) (rem (h + h + 1) d) with
        | isTrue e2 =>
          rw [ite_eq_left e2]
          match at_most_two2 h d y y0 y1 hp hy hy0 hy1 h01 e2 e0 e1 with
          | Or.inl a =>
            rw [show eqI y y0 = 1 from ite_eq_left a,
              show eqI y y1 = 0 from ite_eq_right (fun b => h01 (a.symm.trans b))]
          | Or.inr b =>
            rw [show eqI y y0 = 0 from ite_eq_right (fun a => h01 (a.symm.trans b)),
              show eqI y y1 = 1 from ite_eq_left b]
        | isFalse n2 =>
          rw [ite_eq_right n2,
            show eqI y y0 = 0 from ite_eq_right (fun a => n2 (by rw [a]; exact e0)),
            show eqI y y1 = 0 from ite_eq_right (fun b => n2 (by rw [b]; exact e1))]),
        sum_add, sum_eqI y0 _ hy0, sum_eqI y1 _ hy1]

end SqCount

#print axioms SqCount.sq_zero
#print axioms SqCount.poly_root2
#print axioms SqCount.at_most_two2
#print axioms SqCount.partner2
#print axioms SqCount.sq_count

bsd/SqDvd.lean

lean_all/bsd/SqDvd.lean

/-
  bsd/SqDvd.lean — T39 (goal G289e-2): square divisibility,
    sq_eq_zero       y·y = 0 ⇒ y = 0,
    dvd_of_sq_dvd    d·d ∣ y·y ⇒ d ∣ y,
  so with d = gcd(A, B) and A·B = y², d ∣ y and (A/d)·(B/d) = (y/d)² (then CoprimeSq.coprime_sq).
  Lean 4 core plus bsd/CoprimeSq, lib/Gcd, lib/Burnside, lib/Semi.
-/
import bsd.CoprimeSq

namespace SqDvd
open LibGcd LibBurnside CoprimeSq

theorem sq_eq_zero : ∀ y : Nat, y * y = 0 → y = 0
  | 0, _ => rfl
  | y + 1, h => by
    rw [show (y + 1) * (y + 1) = (y * y + y + y) + 1 by lib_nat] at h
    exact Nat.noConfusion h

/-- **d² ∣ y² ⇒ d ∣ y** -/
theorem dvd_of_sq_dvd (d y : Nat) (h : d * d ∣ y * y) : d ∣ y := by
  cases d with
  | zero =>
    obtain ⟨k, hk⟩ := h
    have hk' : y * y = 0 := by rw [hk]; lib_nat
    rw [sq_eq_zero y hk']
    exact dvd_zero' 0
  | succ d' =>
    obtain ⟨g, ⟨d1, hd1⟩, ⟨y1, hy1⟩, hgmax, _⟩ := bezout (d' + 1) y
    have hg : 0 < g := pos_of_eq_mul hd1
    have hcz : ∀ e, e ∣ d1 → e ∣ y1 → e = 1 := fun e ⟨k, hk⟩ ⟨m, hm⟩ => by
      have h1 : g * e ∣ d' + 1 := ⟨k, by rw [hd1, hk]; lib_nat⟩
      have h2 : g * e ∣ y := ⟨m, by rw [hy1, hm]; lib_nat⟩
      obtain ⟨q, hq⟩ := hgmax (g * e) h1 h2
      have e' : 1 * g = (e * q) * g := by rw [show (e * q) * g = g * e * q by lib_nat, ← hq]; lib_nat
      exact dvd_one' ⟨q, nat_cancel hg 1 (e * q) e'⟩
    obtain ⟨k, hk⟩ := h
    -- y1·y1 = d1·d1·k
    have e1 : (y1 * y1) * (g * g) = (d1 * d1 * k) * (g * g) := by
      have := hk
      rw [hd1, hy1] at this
      rw [show y1 * y1 * (g * g) = g * y1 * (g * y1) by lib_nat, this]; lib_nat
    have hgg : 0 < g * g := by
      cases g with
      | zero => exact absurd hg (Nat.lt_irrefl 0)
      | succ g' => exact Nat.succ_pos _
    have e2 := nat_cancel hgg _ _ e1
    have hdiv : d1 ∣ y1 * y1 := ⟨d1 * k, by rw [e2]; lib_nat⟩
    have hd1y : d1 ∣ y1 := gauss hcz hdiv
    have one := hcz d1 ⟨1, by lib_nat⟩ hd1y
    exact ⟨y1, by rw [hy1, hd1, one]; lib_nat⟩

end SqDvd

#print axioms SqDvd.sq_eq_zero
#print axioms SqDvd.dvd_of_sq_dvd

bsd/SqMod.lean

lean_all/bsd/SqMod.lean

/-
  bsd/SqMod.lean — T39 (goal G289e-6e): "T is a square modulo p" as an integer statement, closed under products.
    SqM p T           ∃ S, p ∣ T − S²
    sqm_sq            m² is a square mod p
    sqm_mul           SqM p T₁ ∧ SqM p T₂ ⇒ SqM p (T₁T₂)
    sqm_congr         T = T′ ⇒ (SqM p T ⇒ SqM p T′)
    sqm_add           SqM p T ⇒ SqM p (T + p·k)
  Lean 4 core plus lib/Gcd (IDv, idv_lin).
-/
import lib.Gcd

namespace SqMod
open LibGcd LibInt

def SqM (p T : Int) : Prop := ∃ S : Int, IDv p (T - S * S)

theorem sqm_sq (p m : Int) : SqM p (m * m) := ⟨m, ⟨0, by lib_int⟩⟩

theorem sqm_mul {p T1 T2 : Int} (h1 : SqM p T1) (h2 : SqM p T2) : SqM p (T1 * T2) := by
  obtain ⟨S1, c1, e1⟩ := h1
  obtain ⟨S2, c2, e2⟩ := h2
  refine ⟨S1 * S2, T1 * c2 + S2 * S2 * c1, ?_⟩
  have e : T1 * T2 - S1 * S2 * (S1 * S2) = T1 * (T2 - S2 * S2) + S2 * S2 * (T1 - S1 * S1) := by lib_int
  rw [e, e1, e2]; lib_int

theorem sqm_congr {p T T' : Int} (e : T = T') (h : SqM p T) : SqM p T' := e ▸ h

/-- adding a multiple of p keeps the square class -/
theorem sqm_add {p T k : Int} (h : SqM p T) : SqM p (T + p * k) := by
  obtain ⟨S, c, e⟩ := h
  refine ⟨S, c + k, ?_⟩
  have e2 : T + p * k - S * S = (T - S * S) + p * k := by lib_int
  rw [e2, e]; lib_int

end SqMod

#print axioms SqMod.sqm_sq
#print axioms SqMod.sqm_mul
#print axioms SqMod.sqm_congr
#print axioms SqMod.sqm_add

bsd/SqNat.lean

lean_all/bsd/SqNat.lean

/-
  bsd/SqNat.lean — T39 (goal G289e-6b): a number that is a square of a rational number is a square of an integer.
    sq_of_mul_sq      N·b² = a², b > 0 (naturals)  ⇒  N = m² for a natural m
    int_sq_of_mul_sq  N·b² = a², b ≠ 0 (integers)  ⇒  N = m² for an integer m
  (divide a, b by their gcd; b₁ ∣ a₁² with gcd(b₁, a₁) = 1 gives b₁ = 1, lib/Gcd gauss).
  Lean 4 core plus lib/Gcd, lib/Burnside (nat_cancel), bsd/CoprimeSq (pos_of_eq_mul), bsd/SqRes (PM, neg_zero).
-/
import bsd.SqRes
import bsd.CoprimeSq

namespace SqNat
open LibGcd LibInt LibBurnside SqRes

theorem sq_of_mul_sq (N a b : Nat) (hb : 0 < b) (h : N * (b * b) = a * a) : ∃ m, N = m * m := by
  cases b with
  | zero => exact absurd hb (Nat.lt_irrefl 0)
  | succ b' =>
    obtain ⟨g, ⟨b1, hb1⟩, ⟨a1, ha1⟩, hgmax, _⟩ := bezout (b' + 1) a
    have hg : 0 < g := CoprimeSq.pos_of_eq_mul hb1
    have hcz : ∀ e, e ∣ b1 → e ∣ a1 → e = 1 := fun e ⟨k, hk⟩ ⟨m, hm⟩ => by
      have h1 : g * e ∣ b' + 1 := ⟨k, by rw [hb1, hk]; lib_nat⟩
      have h2 : g * e ∣ a := ⟨m, by rw [ha1, hm]; lib_nat⟩
      obtain ⟨q, hq⟩ := hgmax (g * e) h1 h2
      have e' : 1 * g = (e * q) * g := by rw [show (e * q) * g = g * e * q by lib_nat, ← hq]; lib_nat
      exact dvd_one' ⟨q, nat_cancel hg 1 (e * q) e'⟩
    have e1 : (N * (b1 * b1)) * (g * g) = (a1 * a1) * (g * g) := by
      have h' := h
      rw [hb1, ha1] at h'
      rw [show N * (b1 * b1) * (g * g) = N * (g * b1 * (g * b1)) by lib_nat, h']; lib_nat
    have hgg : 0 < g * g := by
      cases g with
      | zero => exact absurd hg (Nat.lt_irrefl 0)
      | succ g' => exact Nat.succ_pos _
    have e2 := nat_cancel hgg _ _ e1
    have hdiv : b1 ∣ a1 * a1 := ⟨N * b1, by rw [← e2]; lib_nat⟩
    have hba : b1 ∣ a1 := gauss hcz hdiv
    have one : b1 = 1 := hcz b1 ⟨1, by lib_nat⟩ hba
    rw [one] at e2
    exact ⟨a1, by rw [← e2]; lib_nat⟩

theorem nat_pos_of_ne {b : Int} {k : Nat} (hk : PM b k) (hb : b ≠ 0) : 0 < k := by
  cases k with
  | zero =>
    cases hk with
    | inl e => exact absurd e hb
    | inr e => exact absurd (e.trans (by lib_int)) hb
  | succ k' => exact Nat.succ_pos k'

theorem int_sq_of_mul_sq (N a b : Int) (hb : b ≠ 0) (h : N * (b * b) = a * a) : ∃ m : Int, N = m * m := by
  obtain ⟨n, hn⟩ := pm_exists N
  obtain ⟨k, hk⟩ := pm_exists b
  obtain ⟨t, ht⟩ := pm_exists a
  have hk0 := nat_pos_of_ne hk hb
  have hbb : PM (b * b) (k * k) := Or.inl (pm_sq hk)
  have hnat : n * (k * k) = t * t := pm_nat_sq hn hbb ht h
  obtain ⟨m, hm⟩ := sq_of_mul_sq n t k hk0 hnat
  cases hn with
  | inl e => exact ⟨Int.ofNat m, by rw [e, hm]; lib_int⟩
  | inr e =>
    -- N = −n and N·b² = a² ≥ 0 force n = 0
    have e2 : Int.ofNat (t * t) = -Int.ofNat (n * (k * k)) := by
      rw [← pm_sq ht, ← h, e, pm_sq hk]; lib_int
    obtain ⟨_, e3⟩ := neg_zero e2
    have hn0 : n = 0 := by
      cases LibInt.nat_mul_zero _ _ e3 with
      | inl h1 => exact h1
      | inr h1 =>
        cases LibInt.nat_mul_zero _ _ h1 with
        | inl h2 => rw [h2] at hk0; exact absurd hk0 (Nat.lt_irrefl 0)
        | inr h2 => rw [h2] at hk0; exact absurd hk0 (Nat.lt_irrefl 0)
    exact ⟨0, by rw [e, hn0]; lib_int⟩

end SqNat

#print axioms SqNat.sq_of_mul_sq
#print axioms SqNat.nat_pos_of_ne
#print axioms SqNat.int_sq_of_mul_sq

bsd/SqRes.lean

lean_all/bsd/SqRes.lean

/-
  bsd/SqRes.lean — T39 (goal G289e-5): tools for the second descent on the coverings of E_42486,
    PM A a            A = a or A = −a (a natural number),  pm_exists, pm_mul, pm_sq, pm_nat_sq, pm_dvd, pm_unique,
    sq_split          a·b = n·n ⇒ a = g·u², b = g·w² (g = gcd(a, b)),
    dvd_tab           a finite table over k ≤ N gives F k for every divisor k of N,
    quad_res          Q(α, β, γ)(p·q + x, p·r + z) ≡ (a x² + b x z + c z²) % p   when α ≡ a, β ≡ b, γ ≡ c (mod p),
    sq_of_pm          T = ±k·w², k and −k squares mod p ⇒ T ≡ S² (mod p),
    isSq_of           T ≡ S² and T ≡ r (mod p), r < p ⇒ r is a square mod p (table form, bsd/Res97b),
    res_eq            two residues below p of the same integer are equal.
  Lean 4 core plus lib/Gcd, lib/ModN, lib/Burnside, lib/Semi and the pieces bsd/Res97b, bsd/ResUnique, bsd/ModLink,
  bsd/QuadShift, bsd/CoprimeSq, bsd/SqDvd.
-/
import lib.Gcd
import lib.ModN
import lib.Burnside
import lib.Semi
import bsd.Res97b
import bsd.ResUnique
import bsd.ModLink
import bsd.QuadShift
import bsd.CoprimeSq
import bsd.SqDvd

namespace SqRes
open LibGcd LibInt LibBurnside Res97b

/-! ## Booleans -/

def imp (a b : Bool) : Bool :=
  match a with
  | true => b
  | false => true

theorem imp_mp {a b : Bool} (h : imp a b = true) (ha : a = true) : b = true := by
  cases a with
  | false => exact Bool.noConfusion ha
  | true => exact h

theorem or_cases {a b : Bool} (h : (a || b) = true) : a = true ∨ b = true := by
  cases a with
  | true => exact Or.inl rfl
  | false => exact Or.inr h

theorem any_below_ex (f : Nat → Bool) : ∀ n, anyBelow f n = true → ∃ x, x < n ∧ f x = true
  | 0, h => Bool.noConfusion (h : false = true)
  | n + 1, h => by
    cases hf : f n with
    | true => exact ⟨n, Nat.lt_succ_self n, hf⟩
    | false =>
      have e : (f n || anyBelow f n) = true := h
      rw [hf] at e
      obtain ⟨x, hx, hfx⟩ := any_below_ex f n e
      exact ⟨x, Nat.lt_succ_of_lt hx, hfx⟩

theorem isSq_ex {p m : Nat} (h : isSq p m = true) : ∃ s, s * s % p = m := by
  obtain ⟨s, _, hs⟩ := any_below_ex (fun s => Nat.beq (s * s % p) m) p h
  exact ⟨s, LibMod.beq_true hs⟩

/-! ## Divisor tables -/

theorem zero_or_pos : ∀ g : Nat, g = 0 ∨ 0 < g
  | 0 => Or.inl rfl
  | g + 1 => Or.inr (Nat.succ_pos g)

theorem pos_of_dvd {k N : Nat} (hN : 0 < N) (h : k ∣ N) : 0 < k := by
  obtain ⟨c, hc⟩ := h
  cases zero_or_pos k with
  | inr hk => exact hk
  | inl hk =>
    have e : N = 0 := by rw [hc, hk]; lib_nat
    rw [e] at hN
    exact absurd hN (Nat.lt_irrefl 0)

theorem le_of_dvd' {k N : Nat} (hN : 0 < N) (h : k ∣ N) : k ≤ N := by
  obtain ⟨c, hc⟩ := h
  cases c with
  | zero =>
    rw [hc] at hN
    exact absurd (hN : 0 < 0) (Nat.lt_irrefl 0)
  | succ c =>
    rw [hc]
    exact (LibMod.le_add_l k (k * c) : k ≤ k * c + k)

/-- a table `∀ k ≤ N, N % k = 0 → F k` gives F k for every divisor k of N -/
theorem dvd_tab (N : Nat) (hN : 0 < N) (F : Nat → Bool)
    (ht : allBelow (fun k => imp (Nat.beq (N % k) 0) (F k)) (N + 1) = true) (k : Nat) (hk : k ∣ N) :
    F k = true := by
  have h1 : imp (Nat.beq (N % k) 0) (F k) = true :=
    all_below (fun k => imp (Nat.beq (N % k) 0) (F k)) (N + 1) k ht (Nat.lt_succ_of_le (le_of_dvd' hN hk))
  have h0 : N % k = 0 := ModLink.mod_of_dvd k (pos_of_dvd hN hk) N hk
  have hb : Nat.beq (N % k) 0 = true := by rw [h0]; exact LibMod.beq_self 0
  exact imp_mp h1 hb

/-! ## Integer divisibility -/

theorem idv_symm {p A B : Int} (h : IDv p (A - B)) : IDv p (B - A) := by
  obtain ⟨c, hc⟩ := h
  exact ⟨-c, by
    have e : B - A = -(A - B) := by lib_int
    rw [e, hc]; lib_int⟩

theorem idv_mul {p A : Int} (h : IDv p A) (c : Int) : IDv p (A * c) := by
  obtain ⟨d, hd⟩ := h
  exact ⟨d * c, by rw [hd]; lib_int⟩

theorem idv_sub_zero {p A : Int} (h : IDv p A) : IDv p (A - Int.ofNat 0) := by
  obtain ⟨c, hc⟩ := h
  exact ⟨c, by
    have e : A - Int.ofNat 0 = A := by show A - 0 = A; lib_int
    rw [e, hc]⟩

/-! ## Signs -/

abbrev PM (A : Int) (a : Nat) : Prop := A = Int.ofNat a ∨ A = -Int.ofNat a

theorem pm_exists : ∀ A : Int, ∃ a : Nat, PM A a
  | Int.ofNat n => ⟨n, Or.inl rfl⟩
  | Int.negSucc n => ⟨n + 1, Or.inr rfl⟩

theorem nat_add_zero {x z : Nat} (h : x + z = 0) : x = 0 ∧ z = 0 := by
  cases z with
  | succ k => exact Nat.noConfusion (h : Nat.succ (x + k) = 0)
  | zero =>
    cases x with
    | zero => exact ⟨rfl, rfl⟩
    | succ k => exact Nat.noConfusion (h : Nat.succ k = 0)

theorem neg_zero {x z : Nat} (h : Int.ofNat x = -Int.ofNat z) : x = 0 ∧ z = 0 := by
  have e : Int.ofNat (x + z) = Int.ofNat 0 := by
    show Int.ofNat x + Int.ofNat z = 0
    rw [h]; lib_int
  exact nat_add_zero (Int.ofNat.inj e)

theorem pm_mul {A B : Int} {a b : Nat} (ha : PM A a) (hb : PM B b) : PM (A * B) (a * b) := by
  cases ha with
  | inl ea =>
    cases hb with
    | inl eb => exact Or.inl (by rw [ea, eb] <;> exact rfl)
    | inr eb =>
      exact Or.inr (by
        rw [ea, eb]
        show Int.ofNat a * -Int.ofNat b = -(Int.ofNat a * Int.ofNat b)
        lib_int)
  | inr ea =>
    cases hb with
    | inl eb =>
      exact Or.inr (by
        rw [ea, eb]
        show -Int.ofNat a * Int.ofNat b = -(Int.ofNat a * Int.ofNat b)
        lib_int)
    | inr eb =>
      exact Or.inl (by
        rw [ea, eb]
        show -Int.ofNat a * -Int.ofNat b = Int.ofNat a * Int.ofNat b
        lib_int)

theorem pm_sq {y : Int} {n : Nat} (hy : PM y n) : y * y = Int.ofNat (n * n) := by
  cases hy with
  | inl e => rw [e] <;> exact rfl
  | inr e =>
    rw [e]
    show -Int.ofNat n * -Int.ofNat n = Int.ofNat n * Int.ofNat n
    lib_int

/-- **A = ±a, B = ±b, y = ±n, A·B = y² ⇒ a·b = n²** -/
theorem pm_nat_sq {A B y : Int} {a b n : Nat} (ha : PM A a) (hb : PM B b) (hy : PM y n) (h : A * B = y * y) :
    a * b = n * n := by
  have hab := pm_mul ha hb
  rw [h, pm_sq hy] at hab
  cases hab with
  | inl e => exact (Int.ofNat.inj e).symm
  | inr e =>
    obtain ⟨e1, e2⟩ := neg_zero e
    rw [e2, e1]

theorem pm_dvd {A : Int} {a p : Nat} (ha : PM A a) (h : p ∣ a) : IDv (Int.ofNat p) A := by
  obtain ⟨c, hc⟩ := h
  cases ha with
  | inl e => exact ⟨Int.ofNat c, by rw [e, hc] <;> exact rfl⟩
  | inr e =>
    exact ⟨-Int.ofNat c, by
      rw [e, hc]
      show -(Int.ofNat p * Int.ofNat c) = Int.ofNat p * -Int.ofNat c
      lib_int⟩

theorem pm_unique {B : Int} {b b' : Nat} (h1 : PM B b) (h2 : PM B b') : b = b' := by
  cases h1 with
  | inl e1 =>
    cases h2 with
    | inl e2 => exact Int.ofNat.inj (e1.symm.trans e2)
    | inr e2 =>
      obtain ⟨x, y⟩ := neg_zero (e1.symm.trans e2)
      rw [x, y]
  | inr e1 =>
    cases h2 with
    | inl e2 =>
      obtain ⟨x, y⟩ := neg_zero (e2.symm.trans e1)
      rw [x, y]
    | inr e2 =>
      have e : -Int.ofNat b = -Int.ofNat b' := e1.symm.trans e2
      have e' : Int.ofNat b = Int.ofNat b' := by
        calc Int.ofNat b = -(-Int.ofNat b) := by lib_int
          _ = -(-Int.ofNat b') := by rw [e]
          _ = Int.ofNat b' := by lib_int
      exact Int.ofNat.inj e'

/-! ## Square split -/

/-- **a·b = n² ⇒ a = g·u², b = g·w²** with g = gcd(a, b) -/
theorem sq_split (a b n : Nat) (h : a * b = n * n) : ∃ g u w : Nat, a = g * (u * u) ∧ b = g * (w * w) := by
  obtain ⟨g, ⟨a1, ha1⟩, ⟨b1, hb1⟩, hgmax, _⟩ := bezout a b
  cases zero_or_pos g with
  | inl hg0 =>
    refine ⟨0, 0, 0, ?_, ?_⟩
    · rw [ha1, hg0]; lib_nat
    · rw [hb1, hg0]; lib_nat
  | inr hg =>
    have hc : ∀ d, d ∣ a1 → d ∣ b1 → d = 1 := fun d ⟨k, hk⟩ ⟨m, hm⟩ => by
      have h1 : g * d ∣ a := ⟨k, by rw [ha1, hk]; lib_nat⟩
      have h2 : g * d ∣ b := ⟨m, by rw [hb1, hm]; lib_nat⟩
      obtain ⟨q, hq⟩ := hgmax (g * d) h1 h2
      have e : 1 * g = (d * q) * g := by rw [show (d * q) * g = g * d * q by lib_nat, ← hq]; lib_nat
      exact dvd_one' ⟨q, nat_cancel hg 1 (d * q) e⟩
    have hgg : g * g ∣ n * n := ⟨a1 * b1, by rw [← h, ha1, hb1]; lib_nat⟩
    obtain ⟨t, ht⟩ := SqDvd.dvd_of_sq_dvd g n hgg
    have hgg0 : 0 < g * g := Nat.mul_pos hg hg
    have e : (a1 * b1) * (g * g) = (t * t) * (g * g) := by
      have h' := h
      rw [ha1, hb1, ht] at h'
      rw [show a1 * b1 * (g * g) = g * a1 * (g * b1) by lib_nat, h']; lib_nat
    have hab := nat_cancel hgg0 _ _ e
    obtain ⟨u, hu⟩ := CoprimeSq.coprime_sq a1 b1 t hc hab
    obtain ⟨w, hw⟩ := CoprimeSq.coprime_sq b1 a1 t (fun d h1 h2 => hc d h2 h1)
      (by rw [show b1 * a1 = a1 * b1 by lib_nat]; exact hab)
    exact ⟨g, u, w, by rw [ha1, hu], by rw [hb1, hw]⟩

/-! ## Residues -/

theorem mod_rel (p : Nat) (hp : 0 < p) (n : Nat) : IDv (Int.ofNat p) (Int.ofNat n - Int.ofNat (n % p)) := by
  have e : Int.ofNat n = Int.ofNat p * Int.ofNat (LibMod.dv p n) + Int.ofNat (n % p) :=
    (congrArg Int.ofNat (ModLink.mod_spec p hp n)).trans rfl
  exact ⟨Int.ofNat (LibMod.dv p n), by rw [e]; lib_int⟩

theorem res_eq (p r r' : Nat) (hr : r < p) (hr' : r' < p) (T : Int)
    (h1 : IDv (Int.ofNat p) (T - Int.ofNat r)) (h2 : IDv (Int.ofNat p) (T - Int.ofNat r')) : r = r' :=
  ResUnique.res_unique p r r' hr hr' (ResUnique.idv_trans _ _ _ _ (idv_symm h1) h2)

theorem sq_rel (p : Nat) (hp : 0 < p) (S : Nat) :
    IDv (Int.ofNat p) (Int.ofNat (S * S) - Int.ofNat ((S % p) * (S % p) % p)) := by
  have e : Int.ofNat S = Int.ofNat p * Int.ofNat (LibMod.dv p S) + Int.ofNat (S % p) :=
    (congrArg Int.ofNat (ModLink.mod_spec p hp S)).trans rfl
  have h1 : IDv (Int.ofNat p) (Int.ofNat (S * S) - Int.ofNat ((S % p) * (S % p))) := by
    refine ⟨Int.ofNat p * Int.ofNat (LibMod.dv p S) * Int.ofNat (LibMod.dv p S)
      + 2 * Int.ofNat (LibMod.dv p S) * Int.ofNat (S % p), ?_⟩
    show Int.ofNat S * Int.ofNat S - Int.ofNat (S % p) * Int.ofNat (S % p) = _
    rw [e]; lib_int
  exact ResUnique.idv_trans _ _ _ _ h1 (mod_rel p hp _)

/-- **T ≡ S² and T ≡ r (mod p), r < p ⇒ isSq p r** -/
theorem isSq_of (p : Nat) (hp : 0 < p) (T : Int) (S r : Nat) (hr : r < p)
    (hS : IDv (Int.ofNat p) (T - Int.ofNat (S * S))) (hT : IDv (Int.ofNat p) (T - Int.ofNat r)) :
    isSq p r = true := by
  have h2 := ResUnique.idv_trans _ _ _ _ hS (sq_rel p hp S)
  have e := res_eq p r ((S % p) * (S % p) % p) hr (Nat.mod_lt _ hp) T hT h2
  have h3 : Nat.beq ((S % p) * (S % p) % p) r = true := by rw [e]; exact LibMod.beq_self _
  exact any_below (fun s => Nat.beq (s * s % p) r) p (S % p) (Nat.mod_lt S hp) h3

/-- **T = ±k·w², k and −k ≡ k·m squares mod p = m + 1 ⇒ T ≡ S² (mod p)** -/
theorem sq_of_pm (p m : Nat) (hm : m + 1 = p) (T : Int) (k w : Nat)
    (h1 : isSq p (k % p) = true) (h2 : isSq p (k * m % p) = true) (hT : PM T (k * (w * w))) :
    ∃ S : Nat, IDv (Int.ofNat p) (T - Int.ofNat (S * S)) := by
  have hp : 0 < p := by rw [← hm]; exact Nat.succ_pos m
  cases hT with
  | inl e =>
    obtain ⟨s, hs⟩ := isSq_ex h1
    have m2 := mod_rel p hp (s * s)
    rw [hs] at m2
    have hk : IDv (Int.ofNat p) (Int.ofNat k - Int.ofNat (s * s)) :=
      ResUnique.idv_trans _ _ _ _ (mod_rel p hp k) (idv_symm m2)
    refine ⟨s * w, ?_⟩
    have e2 : T - Int.ofNat (s * w * (s * w)) = (Int.ofNat k - Int.ofNat (s * s)) * Int.ofNat (w * w) := by
      rw [e]
      show Int.ofNat k * (Int.ofNat w * Int.ofNat w) - Int.ofNat s * Int.ofNat w * (Int.ofNat s * Int.ofNat w)
        = (Int.ofNat k - Int.ofNat s * Int.ofNat s) * (Int.ofNat w * Int.ofNat w)
      lib_int
    rw [e2]
    exact idv_mul hk _
  | inr e =>
    obtain ⟨s, hs⟩ := isSq_ex h2
    have m2 := mod_rel p hp (s * s)
    rw [hs] at m2
    have hk : IDv (Int.ofNat p) (Int.ofNat (k * m) - Int.ofNat (s * s)) :=
      ResUnique.idv_trans _ _ _ _ (mod_rel p hp (k * m)) (idv_symm m2)
    have hpp : IDv (Int.ofNat p) (Int.ofNat p) := ⟨1, by lib_int⟩
    have hk2 := idv_lin hk hpp 1 (-Int.ofNat k)
    refine ⟨s * w, ?_⟩
    have e2 : T - Int.ofNat (s * w * (s * w))
        = (1 * (Int.ofNat (k * m) - Int.ofNat (s * s)) + -Int.ofNat k * Int.ofNat p) * Int.ofNat (w * w) := by
      rw [e, ← hm]
      show -(Int.ofNat k * (Int.ofNat w * Int.ofNat w)) - Int.ofNat s * Int.ofNat w * (Int.ofNat s * Int.ofNat w)
        = (1 * (Int.ofNat k * Int.ofNat m - Int.ofNat s * Int.ofNat s) + -Int.ofNat k * (Int.ofNat m + 1))
          * (Int.ofNat w * Int.ofNat w)
      lib_int
    rw [e2]
    exact idv_mul hk2 _

/-- **Q(α, β, γ)(p·q + x, p·r + z) ≡ (a x² + b x z + c z²) % p** when α + p·kα = a etc. -/
theorem quad_res (p : Nat) (hp : 0 < p) (α β γ ka kb kc : Int) (a b c : Nat)
    (ea : α + Int.ofNat p * ka = Int.ofNat a) (eb : β + Int.ofNat p * kb = Int.ofNat b)
    (ec : γ + Int.ofNat p * kc = Int.ofNat c) (q r : Int) (x z : Nat) :
    IDv (Int.ofNat p) (QuadShift.Q α β γ (Int.ofNat p * q + Int.ofNat x) (Int.ofNat p * r + Int.ofNat z)
      - Int.ofNat ((a * x * x + b * x * z + c * z * z) % p)) := by
  have h1 := QuadShift.quad_cong (Int.ofNat p) α β γ ka kb kc q r (Int.ofNat x) (Int.ofNat z)
  rw [ea, eb, ec] at h1
  have e : QuadShift.Q (Int.ofNat a) (Int.ofNat b) (Int.ofNat c) (Int.ofNat x) (Int.ofNat z)
      = Int.ofNat (a * x * x + b * x * z + c * z * z) := rfl
  rw [e] at h1
  exact ResUnique.idv_trans _ _ _ _ h1 (mod_rel p hp _)

end SqRes

#print axioms SqRes.imp_mp
#print axioms SqRes.or_cases
#print axioms SqRes.any_below_ex
#print axioms SqRes.isSq_ex
#print axioms SqRes.zero_or_pos
#print axioms SqRes.pos_of_dvd
#print axioms SqRes.le_of_dvd'
#print axioms SqRes.dvd_tab
#print axioms SqRes.idv_symm
#print axioms SqRes.idv_mul
#print axioms SqRes.idv_sub_zero
#print axioms SqRes.pm_exists
#print axioms SqRes.nat_add_zero
#print axioms SqRes.neg_zero
#print axioms SqRes.pm_mul
#print axioms SqRes.pm_sq
#print axioms SqRes.pm_nat_sq
#print axioms SqRes.pm_dvd
#print axioms SqRes.pm_unique
#print axioms SqRes.sq_split
#print axioms SqRes.mod_rel
#print axioms SqRes.res_eq
#print axioms SqRes.sq_rel
#print axioms SqRes.isSq_of
#print axioms SqRes.sq_of_pm
#print axioms SqRes.quad_res

bsd/SqUnique.lean

lean_all/bsd/SqUnique.lean

/-
  bsd/SqUnique.lean — T39 (goal G289e-6f): a class cls(s, a, b, c, d) supported on {−1, 2, 3, 73, 97} is a square of an
  integer only for the trivial bit vector (so classes are determined by their bits).
    okNonSq N         some modulus q = 2, …, 61 has N mod q a non-square (finite search, rfl)
    not_sq_mod        isSq q (N mod q) = false  ⇒  m² ≠ N
    ne_sq_pos         T = N with okNonSq N  ⇒  T ≠ m²
    ne_sq_neg         T negative  ⇒  T ≠ m²
    cls_sq            cls β = m²  ⇒  β = 0     (32 cases)
  Lean 4 core plus bsd/SqRes (isSq_of, mod_rel, PM), bsd/SqClass.
-/
import bsd.SqRes
import bsd.SqClass

namespace SqUnique
open LibGcd LibInt SqRes Res97b

def okNonSq (N : Nat) : Bool := anyBelow (fun q => !isSq (q + 2) (N % (q + 2))) 60

theorem not_sq_mod (p N : Nat) (hp : 0 < p) (ht : isSq p (N % p) = false) (m : Int) (h : Int.ofNat N = m * m) :
    False := by
  obtain ⟨mn, hmn⟩ := pm_exists m
  have e := pm_sq hmn
  have hS : IDv (Int.ofNat p) (Int.ofNat N - Int.ofNat (mn * mn)) := ⟨0, by rw [h, e]; lib_int⟩
  have hs := isSq_of p hp (Int.ofNat N) mn (N % p) (Nat.mod_lt _ hp) hS (mod_rel p hp N)
  rw [ht] at hs
  exact Bool.noConfusion hs

theorem isSq_false {p r : Nat} (h : (!isSq p r) = true) : isSq p r = false := by
  cases e : isSq p r with
  | false => rfl
  | true => rw [e] at h; exact Bool.noConfusion h

theorem ne_sq_pos {T : Int} {N : Nat} (e : T = Int.ofNat N) (hN : okNonSq N = true) (m : Int) : T ≠ m * m := by
  intro h
  obtain ⟨q, _, hq⟩ := any_below_ex _ 60 hN
  exact not_sq_mod (q + 2) N (Nat.succ_pos _) (isSq_false hq) m (e.symm.trans h)

theorem ne_sq_neg {T : Int} {k : Nat} (e : T = Int.negSucc k) (m : Int) : T ≠ m * m := by
  intro h
  obtain ⟨mn, hmn⟩ := pm_exists m
  have e2 : Int.ofNat (mn * mn) = -Int.ofNat (k + 1) := by rw [← pm_sq hmn, ← h, e]; rfl
  obtain ⟨_, e3⟩ := neg_zero e2
  exact Nat.noConfusion e3

/-- **Only the trivial class is a square.** -/
theorem cls_sq (s a b c d : Bool) (m : Int) (h : SqClass.cls s a b c d = m * m) :
    s = false ∧ a = false ∧ b = false ∧ c = false ∧ d = false := by
  cases s <;> cases a <;> cases b <;> cases c <;> cases d <;>
    first
    | exact ⟨rfl, rfl, rfl, rfl, rfl⟩
    | exact absurd h (ne_sq_pos rfl rfl m)
    | exact absurd h (ne_sq_neg rfl m)

end SqUnique

#print axioms SqUnique.not_sq_mod
#print axioms SqUnique.isSq_false
#print axioms SqUnique.ne_sq_pos
#print axioms SqUnique.ne_sq_neg
#print axioms SqUnique.cls_sq

bsd/SqUniqueL.lean

lean_all/bsd/SqUniqueL.lean

/-
  bsd/SqUniqueL.lean — T39 (goal G289f, tool): square classes over an arbitrary list of primes.
  A class is a sign bit s and a bit list bs over a list ps of primes, value (−1)^s · Π p^b.  If a Boolean
  certificate chkL ps (run over all 2^(k+1) bit vectors) holds, then only the trivial class is an integer square,
  so classes are determined by their bits — the n-free form of bsd/SqUnique.cls_sq (which fixes ps = [2, 3, 73, 97]).
    prodL, clsL        the value of a class
    allBits, allBits_app   enumeration of bit lists of a given length
    chkL               certificate: trivial, negative, or a non-square modulo some q ≤ 61
    cls_sqL            chkL ps = true, clsL s ps bs = m²  ⇒  s = false ∧ every bit false
  Lean 4 core plus bsd/SqUnique.
-/
import bsd.SqUnique

namespace SqUniqueL
open SqClass

def prodL : List Nat → List Bool → Int
  | p :: ps, b :: bs => fb (Int.ofNat p) b * prodL ps bs
  | [], _ => 1
  | _ :: _, [] => 1

def clsL (s : Bool) (ps : List Nat) (bs : List Bool) : Int := fb (-1) s * prodL ps bs

def allF : List Bool → Bool
  | [] => true
  | b :: l => !b && allF l

def allBits : Nat → (List Bool → Bool) → Bool
  | 0, f => f []
  | k + 1, f => allBits k (fun l => f (true :: l)) && allBits k (fun l => f (false :: l))

theorem and_l {a b : Bool} (h : (a && b) = true) : a = true := by
  cases a with
  | true => rfl
  | false => exact Bool.noConfusion h

theorem and_r {a b : Bool} (h : (a && b) = true) : b = true := by
  cases a with
  | true => exact h
  | false => exact Bool.noConfusion h

theorem or_cases : ∀ {a b : Bool}, (a || b) = true → a = true ∨ b = true
  | true, _, _ => Or.inl rfl
  | false, _, h => Or.inr h

theorem allBits_app : ∀ (k : Nat) (f : List Bool → Bool), allBits k f = true →
    ∀ l : List Bool, l.length = k → f l = true
  | 0, _, h, [], _ => h
  | 0, _, _, _ :: _, hl => Nat.noConfusion hl
  | _ + 1, _, _, [], hl => Nat.noConfusion hl
  | k + 1, f, h, true :: l, hl => allBits_app k (fun l => f (true :: l)) (and_l h) l (Nat.succ.inj hl)
  | k + 1, f, h, false :: l, hl => allBits_app k (fun l => f (false :: l)) (and_r h) l (Nat.succ.inj hl)

/-- the value is negative, or a non-square modulo some q -/
def nonSq : Int → Bool
  | Int.ofNat N => SqUnique.okNonSq N
  | Int.negSucc _ => true

def good (ps : List Nat) (s : Bool) (bs : List Bool) : Bool := (!s && allF bs) || nonSq (clsL s ps bs)

/-- the certificate for a list of primes -/
def chkL (ps : List Nat) : Bool := good ps true [] && allBits ps.length (good ps true)
  && allBits ps.length (good ps false)

theorem nonSq_ne : ∀ (T : Int), nonSq T = true → ∀ m : Int, T ≠ m * m
  | Int.ofNat _, h, m => SqUnique.ne_sq_pos rfl h m
  | Int.negSucc _, _, m => SqUnique.ne_sq_neg rfl m

/-- **Only the trivial class is a square** (for any list of primes with a certificate). -/
theorem cls_sqL (ps : List Nat) (hc : chkL ps = true) (s : Bool) (bs : List Bool) (hl : bs.length = ps.length)
    (m : Int) (h : clsL s ps bs = m * m) : s = false ∧ allF bs = true := by
  have hg : good ps s bs = true := by
    cases s with
    | true => exact allBits_app _ _ (and_r (and_l hc)) bs hl
    | false => exact allBits_app _ _ (and_r hc) bs hl
  rcases or_cases hg with ht | hn
  · cases s with
    | true => exact Bool.noConfusion ht
    | false => exact ⟨rfl, ht⟩
  · exact absurd h (nonSq_ne _ hn m)

/-- the list of bsd/SqUnique -/
theorem chk_42486 : chkL [2, 3, 73, 97] = true := rfl

end SqUniqueL

#print axioms SqUniqueL.and_l
#print axioms SqUniqueL.and_r
#print axioms SqUniqueL.or_cases
#print axioms SqUniqueL.allBits_app
#print axioms SqUniqueL.nonSq_ne
#print axioms SqUniqueL.cls_sqL
#print axioms SqUniqueL.chk_42486

bsd/StepFull.lean

lean_all/bsd/StepFull.lean

/-
  bsd/StepFull.lean — T39 (goal G289e-7b3): TransStep2.step with the torsion case made exact.  For P₁, P₂ away from
  E[2] with x₁ ≠ x₂ and classes (β₁, β₂), (γ₁, γ₂), the third chord point P₃ either is a point of E[2] whose class
  is exactly (β₁ ⊕ γ₁, β₂ ⊕ γ₂) — (0,0): ((−1), (−n)), (n,0): ((n), (2)), (−n,0): ((−n), (−2n)) — or is away from
  E[2] with that class.
    TorCls       x is a 2-torsion abscissa and (δ₁, δ₂) its class
    step_full    the exact dichotomy
  Lean 4 core plus bsd/TransStep2.
-/
import bsd.TransStep2

namespace StepFull
open DetChain LibRing LibRat LibInt QModel QCls B5S TransStep TransQ TransStep2

/-- the class of a point of E[2] (bits sign, 2, 3, 73, 97 of x and of x − n) -/
def TorCls (x : Q) (δ1 δ2 : B5) : Prop :=
  (x = zeroQ ∧ δ1 = ⟨true, false, false, false, false⟩ ∧ δ2 = ⟨true, true, true, true, true⟩) ∨
  (x = nQ ∧ δ1 = ⟨false, true, true, true, true⟩ ∧ δ2 = ⟨false, true, false, false, false⟩) ∨
  (x = negQ nQ ∧ δ1 = ⟨true, true, true, true, true⟩ ∧ δ2 = ⟨true, false, true, true, true⟩)

/-- **The chord step with the torsion classes made explicit.** -/
theorem step_full (x1 y1 x2 y2 : Q) (o1 : OnE x1 y1) (o2 : OnE x2 y2) (h12 : x1 ≠ x2)
    (β1 β2 γ1 γ2 : B5) (hc1 : HC x1 β1 β2) (hc2 : HC x2 γ1 γ2) (nd1 : ND x1) (nd2 : ND x2) :
    TorCls (x3 x1 y1 x2 y2) (β1.xor γ1) (β2.xor γ2) ∨
      (ND (x3 x1 y1 x2 y2) ∧ HC (x3 x1 y1 x2 y2) (β1.xor γ1) (β2.xor γ2)) := by
  obtain ⟨nx1, nxn1, nxm1⟩ := nd1
  obtain ⟨nx2, nxn2, nxm2⟩ := nd2
  obtain ⟨T1, eT1⟩ := cls3 x1 y1 o1 β1 β2 hc1 nx1 nxn1
  obtain ⟨T2, eT2⟩ := cls3 x2 y2 o2 γ1 γ2 hc2 nx2 nxn2
  obtain ⟨s1, r1, _, _, ex1, exn1⟩ := hc1
  obtain ⟨s2, r2, _, _, ex2, exn2⟩ := hc2
  have ex1' : sub ratR x1 zeroQ = ratR.mul (cq β1) (ratR.mul s1 s1) := (sub0 x1).trans ex1
  have ex2' : sub ratR x2 zeroQ = ratR.mul (cq γ1) (ratR.mul s2 s2) := (sub0 x2).trans ex2
  have hab0 : ratR.mul (sub ratR x1 zeroQ) (sub ratR x2 zeroQ) ≠ zeroQ := by
    rw [sub0 x1, sub0 x2]; exact mul_ne _ _ nx1 nx2
  have habn := mul_ne _ _ nxn1 nxn2
  have habm := mul_ne _ _ nxm1 nxm2
  have ch0 := chord_Q x1 y1 x2 y2 h12 o1 o2 zeroQ (Or.inl rfl)
  have chn := chord_Q x1 y1 x2 y2 h12 o1 o2 nQ (Or.inr (Or.inl rfl))
  have chm := chord_Q x1 y1 x2 y2 h12 o1 o2 (negQ nQ) (Or.inr (Or.inr rfl))
  -- the three coordinates of P₃ as classes (whenever defined)
  have c0 := third_cq β1 γ1 _ _ _ s1 s2 _ ex1' ex2' hab0 ch0
  have cn := third_cq β2 γ2 _ _ _ r1 r2 _ exn1 exn2 habn chn
  have cm := third_cq (β1.xor β2) (γ1.xor γ2) _ _ _ T1 T2 _ eT1 eT2 habm chm
  have hx4 := xor4B β1 β2 γ1 γ2
  cases Int.decEq (x3 x1 y1 x2 y2).num 0 with
  | isTrue h0 =>
    have hz : x3 x1 y1 x2 y2 = zeroQ := zero_eq _ h0
    obtain ⟨Tn, eTn⟩ := cn
    obtain ⟨Tm, eTm⟩ := cm
    rw [hz, tor_0n] at eTn
    rw [hz, tor_0m] at eTm
    have d2 := bits_of_eq KT0 _ 1 one_ne Tn eTn
    have d12 := bits_of_eq KN _ 1 one_ne Tm eTm
    rw [hx4] at d12
    exact Or.inl (Or.inl ⟨hz, solve _ _ _ _ d12 d2, d2⟩)
  | isFalse h0 =>
    have hz : x3 x1 y1 x2 y2 ≠ zeroQ := ne_of_num h0
    cases Int.decEq (sub ratR (x3 x1 y1 x2 y2) nQ).num 0 with
    | isTrue h1 =>
      have hn : x3 x1 y1 x2 y2 = nQ := LibVieta.sub_zero_eq ratR _ _ (zero_eq _ h1)
      obtain ⟨T0', eT0⟩ := c0
      obtain ⟨Tm, eTm⟩ := cm
      rw [hn, tor_n0] at eT0
      rw [hn, tor_nm] at eTm
      have d1 := bits_of_eq KN _ 1 one_ne T0' eT0
      have d12 := bits_of_eq K2N _ 2 two_ne Tm eTm
      rw [hx4] at d12
      exact Or.inl (Or.inr (Or.inl ⟨hn, d1, solve2 _ _ _ _ d12 d1⟩))
    | isFalse h1 =>
      have hn : sub ratR (x3 x1 y1 x2 y2) nQ ≠ zeroQ := ne_of_num h1
      cases Int.decEq (sub ratR (x3 x1 y1 x2 y2) (negQ nQ)).num 0 with
      | isTrue h2 =>
        have hm : x3 x1 y1 x2 y2 = negQ nQ := LibVieta.sub_zero_eq ratR _ _ (zero_eq _ h2)
        obtain ⟨T0', eT0⟩ := c0
        obtain ⟨Tn, eTn⟩ := cn
        rw [hm, tor_m0] at eT0
        rw [hm, tor_mn] at eTn
        have d1 := bits_of_eq KT0 _ 1 one_ne T0' eT0
        have d2 := bits_of_eq Km2N _ 2 two_ne Tn eTn
        exact Or.inl (Or.inr (Or.inr ⟨hm, d1, d2⟩))
      | isFalse h2 =>
        right
        have hm : sub ratR (x3 x1 y1 x2 y2) (negQ nQ) ≠ zeroQ := ne_of_num h2
        obtain ⟨A, eA⟩ := c0
        obtain ⟨B, eB⟩ := cn
        have h0' : sub ratR (x3 x1 y1 x2 y2) zeroQ ≠ zeroQ := by rw [sub0]; exact hz
        refine ⟨⟨hz, hn, hm⟩, ?_⟩
        unfold HC HasCls
        exact ⟨A, B, T_ne _ _ A h0' eA, T_ne _ _ B hn eB, (sub0 _).symm.trans eA, eB⟩

end StepFull

#print axioms StepFull.step_full

bsd/StepN.lean

lean_all/bsd/StepN.lean

/-
  bsd/StepN.lean — T39 (goal G289f-4c): the chord step on descent classes of E_n over Q, n a rational parameter and
  classes over a certified list of primes ps (bsd/TransStep2.step fixes n = 42486, ps = [2, 3, 73, 97]).
  P₁ (class (β₁, β₂)), P₂ (class (γ₁, γ₂)), x₁ ≠ x₂, both away from E[2], P₃ the third chord point, δ = β ⊕ γ. Then
  P₃ is away from E[2] with class δ, or P₃ ∈ E[2] and δ is determined by the classes of the differences of the roots
  (supplied as data TorData: e − e′ = cls κ · m²).
    xor4L, xorL_lenL     bit-list algebra
    TorData              the six values e − e′ (e ≠ e′ ∈ {0, n, −n}) as classes times squares
    NDn …                x, x − n, x + n nonzero
    stepN                the dichotomy
  Lean 4 core plus bsd/TransStepL, bsd/PtAddN.
-/
import bsd.TransStepL
import bsd.PtAddN

namespace StepN
open DetChain LibRing LibRat LibInt QModel QCls B5S SqUniqueL SqClassL QModelN TransStepL PtAddN

theorem xorL_nil : ∀ l : List Bool, xorL l [] = []
  | [] => rfl
  | _ :: _ => rfl

theorem xor4 (a b c d : Bool) : xor (xor a b) (xor c d) = xor (xor a c) (xor b d) := TransStep.xor4 a b c d

theorem xor4L : ∀ a b c d : List Bool, xorL (xorL a b) (xorL c d) = xorL (xorL a c) (xorL b d)
  | [], _, _, _ => rfl
  | _ :: _, [], c, _ => (xorL_nil (xorL _ c)).symm
  | _ :: _, _ :: _, [], _ => xorL_nil _
  | _ :: _, _ :: _, _ :: _, [] => rfl
  | a :: as, b :: bs, c :: cs, d :: ds => by
    show (xor (xor a b) (xor c d)) :: xorL (xorL as bs) (xorL cs ds)
      = (xor (xor a c) (xor b d)) :: xorL (xorL as cs) (xorL bs ds)
    rw [xor4 a b c d, xor4L as bs cs ds]

theorem xorL_lenL (ps : List Nat) (a b : List Bool) (ha : a.length = ps.length) (hb : b.length = ps.length) :
    (xorL a b).length = ps.length := (xorL_len a b (ha.trans hb.symm)).trans ha

theorem ne_of_num' {x : Q} (h : x.num ≠ 0) : x ≠ zeroQ := fun h0 => h (num_eq_zero_of x h0)

theorem T_neQ (k c T : Q) (hc : c ≠ zeroQ) (e : c = ratR.mul k (ratR.mul T T)) : T ≠ zeroQ :=
  fun h0 => hc (by rw [e, h0]; show ratR.mul k (ratR.mul ratR.zero ratR.zero) = ratR.zero; lib_ring)

/-- the six root differences as classes times nonzero integer squares -/
structure TorData (n : Q) (ps : List Nat) where
  k0n : Bool
  b0n : List Bool
  m0n : Int
  k0m : Bool
  b0m : List Bool
  m0m : Int
  kn0 : Bool
  bn0 : List Bool
  mn0 : Int
  knm : Bool
  bnm : List Bool
  mnm : Int
  km0 : Bool
  bm0 : List Bool
  mm0 : Int
  kmn : Bool
  bmn : List Bool
  mmn : Int
  l0n : b0n.length = ps.length
  l0m : b0m.length = ps.length
  ln0 : bn0.length = ps.length
  lnm : bnm.length = ps.length
  lm0 : bm0.length = ps.length
  lmn : bmn.length = ps.length
  h0n : m0n ≠ 0
  h0m : m0m ≠ 0
  hn0 : mn0 ≠ 0
  hnm : mnm ≠ 0
  hm0 : mm0 ≠ 0
  hmn : mmn ≠ 0
  e0n : sub ratR zeroQ n = castQ (clsL k0n ps b0n * (m0n * m0n))
  e0m : sub ratR zeroQ (negQ n) = castQ (clsL k0m ps b0m * (m0m * m0m))
  en0 : sub ratR n zeroQ = castQ (clsL kn0 ps bn0 * (mn0 * mn0))
  enm : sub ratR n (negQ n) = castQ (clsL knm ps bnm * (mnm * mnm))
  em0 : sub ratR (negQ n) zeroQ = castQ (clsL km0 ps bm0 * (mm0 * mm0))
  emn : sub ratR (negQ n) n = castQ (clsL kmn ps bmn * (mmn * mmn))

/-- **The chord step, general n.** -/
theorem stepN (n : Q) (ps : List Nat) (hc : chkL ps = true) (hp : posL ps = true) (D : TorData n ps)
    (x1 y1 x2 y2 : Q) (o1 : OnEn n x1 y1) (o2 : OnEn n x2 y2) (h12 : x1 ≠ x2)
    (s1 s2 t1 t2 : Bool) (b1 b2 c1 c2 : List Bool) (lb1 : b1.length = ps.length) (lb2 : b2.length = ps.length)
    (lc1 : c1.length = ps.length) (lc2 : c2.length = ps.length)
    (hc1 : HCn n ps x1 s1 b1 s2 b2) (hc2 : HCn n ps x2 t1 c1 t2 c2) (nd1 : NDn n x1) (nd2 : NDn n x2) :
    (NDn n (x3n n x1 y1 x2 y2) ∧ HCn n ps (x3n n x1 y1 x2 y2) (xor s1 t1) (xorL b1 c1) (xor s2 t2) (xorL b2 c2)) ∨
    (x3n n x1 y1 x2 y2 = zeroQ ∧ (xor s2 t2 = D.k0n ∧ xorL b2 c2 = D.b0n) ∧
      (xor (xor s1 t1) (xor s2 t2) = D.k0m ∧ xorL (xorL b1 c1) (xorL b2 c2) = D.b0m)) ∨
    (x3n n x1 y1 x2 y2 = n ∧ (xor s1 t1 = D.kn0 ∧ xorL b1 c1 = D.bn0) ∧
      (xor (xor s1 t1) (xor s2 t2) = D.knm ∧ xorL (xorL b1 c1) (xorL b2 c2) = D.bnm)) ∨
    (x3n n x1 y1 x2 y2 = negQ n ∧ (xor s1 t1 = D.km0 ∧ xorL b1 c1 = D.bm0) ∧
      (xor s2 t2 = D.kmn ∧ xorL b2 c2 = D.bmn)) := by
  obtain ⟨nx1, nxn1, nxm1⟩ := nd1
  obtain ⟨nx2, nxn2, nxm2⟩ := nd2
  obtain ⟨T1, eT1⟩ := cls3n n x1 y1 ps o1 s1 s2 b1 b2 lb1 lb2 hc1 nx1 nxn1
  obtain ⟨T2, eT2⟩ := cls3n n x2 y2 ps o2 t1 t2 c1 c2 lc1 lc2 hc2 nx2 nxn2
  obtain ⟨u1, r1, _, _, ex1, exn1⟩ := hc1
  obtain ⟨u2, r2, _, _, ex2, exn2⟩ := hc2
  have ex1' : sub ratR x1 zeroQ = ratR.mul (cqL ps s1 b1) (ratR.mul u1 u1) := (TransStep.sub0 x1).trans ex1
  have ex2' : sub ratR x2 zeroQ = ratR.mul (cqL ps t1 c1) (ratR.mul u2 u2) := (TransStep.sub0 x2).trans ex2
  have hab0 : ratR.mul (sub ratR x1 zeroQ) (sub ratR x2 zeroQ) ≠ zeroQ := by
    rw [TransStep.sub0 x1, TransStep.sub0 x2]; exact TransStep.mul_ne _ _ nx1 nx2
  have habn := TransStep.mul_ne _ _ nxn1 nxn2
  have habm := TransStep.mul_ne _ _ nxm1 nxm2
  have ch0 := chord_Qn n x1 y1 x2 y2 h12 o1 o2 zeroQ (Or.inl rfl)
  have chn := chord_Qn n x1 y1 x2 y2 h12 o1 o2 n (Or.inr (Or.inl rfl))
  have chm := chord_Qn n x1 y1 x2 y2 h12 o1 o2 (negQ n) (Or.inr (Or.inr rfl))
  have c0 := third_cqL ps s1 t1 b1 c1 lb1 lc1 _ _ _ u1 u2 _ ex1' ex2' hab0 ch0
  have cn := third_cqL ps s2 t2 b2 c2 lb2 lc2 _ _ _ r1 r2 _ exn1 exn2 habn chn
  have cm := third_cqL ps (xor s1 s2) (xor t1 t2) (xorL b1 b2) (xorL c1 c2)
    (xorL_lenL ps b1 b2 lb1 lb2) (xorL_lenL ps c1 c2 lc1 lc2) _ _ _ T1 T2 _ eT1 eT2 habm chm
  rw [xor4 s1 s2 t1 t2, xor4L b1 b2 c1 c2] at cm
  have l1 := xorL_lenL ps b1 c1 lb1 lc1
  have l2 := xorL_lenL ps b2 c2 lb2 lc2
  have l12 := xorL_lenL ps _ _ l1 l2
  cases Int.decEq (x3n n x1 y1 x2 y2).num 0 with
  | isTrue h0 =>
    right; left
    have hz : x3n n x1 y1 x2 y2 = zeroQ := zero_eq _ h0
    obtain ⟨Tn, eTn⟩ := cn
    obtain ⟨Tm, eTm⟩ := cm
    rw [hz, D.e0n] at eTn
    rw [hz, D.e0m] at eTm
    exact ⟨hz, bits_of_eqL ps hc hp _ _ _ _ D.l0n l2 _ D.h0n Tn eTn,
      bits_of_eqL ps hc hp _ _ _ _ D.l0m l12 _ D.h0m Tm eTm⟩
  | isFalse h0 =>
    have hz : x3n n x1 y1 x2 y2 ≠ zeroQ := ne_of_num' h0
    cases Int.decEq (sub ratR (x3n n x1 y1 x2 y2) n).num 0 with
    | isTrue h1 =>
      right; right; left
      have hn : x3n n x1 y1 x2 y2 = n := LibVieta.sub_zero_eq ratR _ _ (zero_eq _ h1)
      obtain ⟨A, eA⟩ := c0
      obtain ⟨Tm, eTm⟩ := cm
      rw [hn, D.en0] at eA
      rw [hn, D.enm] at eTm
      exact ⟨hn, bits_of_eqL ps hc hp _ _ _ _ D.ln0 l1 _ D.hn0 A eA,
        bits_of_eqL ps hc hp _ _ _ _ D.lnm l12 _ D.hnm Tm eTm⟩
    | isFalse h1 =>
      have hn : sub ratR (x3n n x1 y1 x2 y2) n ≠ zeroQ := ne_of_num' h1
      cases Int.decEq (sub ratR (x3n n x1 y1 x2 y2) (negQ n)).num 0 with
      | isTrue h2 =>
        right; right; right
        have hm : x3n n x1 y1 x2 y2 = negQ n := LibVieta.sub_zero_eq ratR _ _ (zero_eq _ h2)
        obtain ⟨A, eA⟩ := c0
        obtain ⟨B, eB⟩ := cn
        rw [hm, D.em0] at eA
        rw [hm, D.emn] at eB
        exact ⟨hm, bits_of_eqL ps hc hp _ _ _ _ D.lm0 l1 _ D.hm0 A eA,
          bits_of_eqL ps hc hp _ _ _ _ D.lmn l2 _ D.hmn B eB⟩
      | isFalse h2 =>
        left
        have hm : sub ratR (x3n n x1 y1 x2 y2) (negQ n) ≠ zeroQ := ne_of_num' h2
        obtain ⟨A, eA⟩ := c0
        obtain ⟨B, eB⟩ := cn
        have h0' : sub ratR (x3n n x1 y1 x2 y2) zeroQ ≠ zeroQ := by rw [TransStep.sub0]; exact hz
        refine ⟨⟨hz, hn, hm⟩, ?_⟩
        exact ⟨A, B, T_neQ _ _ A h0' eA, T_neQ _ _ B hn eB, (TransStep.sub0 _).symm.trans eA, eB⟩

end StepN

#print axioms StepN.xorL_nil
#print axioms StepN.xor4
#print axioms StepN.xor4L
#print axioms StepN.xorL_lenL
#print axioms StepN.ne_of_num'
#print axioms StepN.T_neQ
#print axioms StepN.stepN

bsd/Supersingular.lean

lean_all/bsd/Supersingular.lean

/-
  bsd/Supersingular.lean — T39 cycle 61: first piece of "a_p = 0 for y² = x³ − Dx at p ≡ 3 (mod 4)".

  Lean 4 core plus ESChain (legV), QRChain (Euler criterion, legB_iff) and bsd/Congruent2pE (mul_pow').
  For p = h + h + 1 with h odd (p ≡ 3 mod 4), writing −1 as p − 1 = h + h:
    neg_flip    p ∤ a ⇒ legB(−a) = ¬ legB(a)        (−1 is a non-residue, Euler multiplicativity)
    leg_pair    legV(a) + legV(−a) = 2   for every a  (values 1+1, 2+0 or 0+2)
  Next: with the reflection x ↦ −x, f(−x) = −f(x) for f = x³ − Dx, Σ_x legV(f(x)) = p, so #E(F_p) = p + 1.
-/
import ESChain
import bsd.Congruent2pE

namespace Supersingular
open FTAChain FrobChain QRChain ESChain Congruent2pE

theorem ite_no {α : Type} (c : Prop) [d : Decidable c] (x y : α) (h : ¬ c) : (if c then x else y) = y := by
  cases d with
  | isFalse _ => rfl
  | isTrue hc => exact absurd hc h

theorem neg_flip (n a : Nat) (hp : Pr ((n + n + 1) + (n + n + 1) + 1))
    (ha : ¬ Dv ((n + n + 1) + (n + n + 1) + 1) a) :
    legB ((n + n + 1) + (n + n + 1) + 1) (((n + n + 1) + (n + n + 1)) * a)
      = !legB ((n + n + 1) + (n + n + 1) + 1) a := by
  let h := n + n + 1
  have hna : ¬ Dv (h + h + 1) ((h + h) * a) := fun hd =>
    match euclid _ _ _ hp hd with
    | Or.inl d => not_dv_of_lt _ _ (hh_pos h hp) (Nat.lt_succ_self _) d
    | Or.inr d => ha d
  have hm : rem (h + h + 1) (((h + h) * a) ^ h) = rem (h + h + 1) ((h + h) * a ^ h) := by
    rw [mul_pow']
    exact rem_mul_congr (h + h) _ _ _ ((neg_pow_odd h n hp).trans (rem_of_lt _ _ (Nat.lt_succ_self _)).symm)
  have iA := (legB_iff (h + h) a).trans (euler_iff h a hp ha).symm
  have iB := (legB_iff (h + h) ((h + h) * a)).trans (euler_iff h _ hp hna).symm
  cases euler_pm h a hp ha with
  | inl e1 =>
    have tB : rem (h + h + 1) (((h + h) * a) ^ h) = h + h := by
      rw [hm, rem_mul_congr' (h + h) _ _ _ (e1.trans (rem_one _ (hh_pos h hp)).symm), Nat.mul_one]
      exact rem_of_lt _ _ (Nat.lt_succ_self _)
    have bA : legB (h + h + 1) a = true := iA.mpr e1
    have bB : legB (h + h + 1) ((h + h) * a) = false := by
      cases hb : legB (h + h + 1) ((h + h) * a) with
      | false => rfl
      | true => exact absurd ((iB.mp hb).symm.trans tB) (fun e => par_one_ne h e.symm)
    show legB (h + h + 1) ((h + h) * a) = !legB (h + h + 1) a
    rw [bA, bB]; rfl
  | inr e2 =>
    have e2' : rem (h + h + 1) (a ^ h) = h + h := e2
    have tB : rem (h + h + 1) (((h + h) * a) ^ h) = 1 := by
      rw [hm, rem_mul_congr' (h + h) _ _ _ (e2'.trans (rem_of_lt _ _ (Nat.lt_succ_self _)).symm)]
      exact neg_sq' h hp
    have bB : legB (h + h + 1) ((h + h) * a) = true := iB.mpr tB
    have bA : legB (h + h + 1) a = false := by
      cases hb : legB (h + h + 1) a with
      | false => rfl
      | true => exact absurd ((iA.mp hb).symm.trans e2') (fun e => par_one_ne h e.symm)
    show legB (h + h + 1) ((h + h) * a) = !legB (h + h + 1) a
    rw [bA, bB]; rfl

/-- **legV(a) + legV(−a) = 2** at p ≡ 3 (mod 4). -/
theorem leg_pair (n a : Nat) (hp : Pr ((n + n + 1) + (n + n + 1) + 1)) :
    legV ((n + n + 1) + (n + n + 1) + 1) a
      + legV ((n + n + 1) + (n + n + 1) + 1) (((n + n + 1) + (n + n + 1)) * a) = 2 := by
  let h := n + n + 1
  show legV (h + h + 1) a + legV (h + h + 1) ((h + h) * a) = 2
  unfold legV
  cases hz : rem (h + h + 1) a with
  | zero =>
    have hz2 : rem (h + h + 1) ((h + h) * a) = 0 :=
      rem_zero_of_dv _ _ (dv_mul_left _ _ _ (dv_of_rem_zero _ _ hz))
    rw [hz2]; rfl
  | succ k =>
    have ha : ¬ Dv (h + h + 1) a := fun d => Nat.noConfusion ((rem_zero_of_dv _ _ d).symm.trans hz)
    have hz2 : rem (h + h + 1) ((h + h) * a) ≠ 0 := fun e =>
      match euclid _ _ _ hp (dv_of_rem_zero _ _ e) with
      | Or.inl d => not_dv_of_lt _ _ (hh_pos h hp) (Nat.lt_succ_self _) d
      | Or.inr d => ha d
    rw [ite_no _ _ _ hz2, neg_flip n a hp ha]
    show (if k + 1 = 0 then 1 else cond (legB (h + h + 1) a) 2 0) + cond (!legB (h + h + 1) a) 2 0 = 2
    rw [ite_no _ _ _ (fun e => Nat.noConfusion e)]
    cases legB (h + h + 1) a <;> rfl

end Supersingular

#print axioms Supersingular.ite_no
#print axioms Supersingular.neg_flip
#print axioms Supersingular.leg_pair

bsd/Supersingular2.lean

lean_all/bsd/Supersingular2.lean

/-
  bsd/Supersingular2.lean — T39 cycle 63: Σ_x (x³ + Dx | p) counts to p at p ≡ 3 (mod 4) (piece 3).

  Lean 4 core plus bsd/Supersingular (leg_pair), bsd/Reflect (pair_count), lib/Semi (lib_nat).
  For every D (D = p − D₀ gives x³ − D₀x) and p = 4n + 3 prime:
    legV_congr   legV depends only on the residue
    neg_cong     a + b = p ⇒ b ≡ −a
    odd_f        a + b = p ⇒ f(b) ≡ −f(a),   f(x) = x³ + Dx
    sum_leg      Σ_{x<p} legV(f(x)) = p,  i.e.  Σ_x (1 + (f(x)|p)) = p,  Σ_x (f(x)|p) = 0
  With the square count #{y : y² ≡ c} = legV(c) this is #E(F_p) = p + 1, a_p = 0.
-/
import bsd.Supersingular
import bsd.Reflect

namespace Supersingular2
open FTAChain FrobChain QRChain ESChain Supersingular Reflect

theorem legV_congr (m' d d' : Nat) (h : rem (m' + 1) d = rem (m' + 1) d') :
    legV (m' + 1) d = legV (m' + 1) d' := by
  have hb : legB (m' + 1) d = legB (m' + 1) d' := by
    have i1 := legB_iff m' d
    have i2 := legB_iff m' d'
    rw [h] at i1
    cases e1 : legB (m' + 1) d with
    | true => exact (i2.mpr (i1.mp e1)).symm
    | false =>
      cases e2 : legB (m' + 1) d' with
      | false => rfl
      | true => rw [i1.mpr (i2.mp e2)] at e1; exact Bool.noConfusion e1
  unfold legV; rw [h, hb]

theorem neg_cong (h a b : Nat) (e : a + b = h + h + 1) :
    rem (h + h + 1) b = rem (h + h + 1) ((h + h) * a) := by
  apply rem_cancel_add (h + h) _ _ a
  have z1 : rem (h + h + 1) (b + a) = 0 :=
    rem_zero_of_dv _ _ ⟨1, by rw [Nat.add_comm b a, e]; lib_nat⟩
  have z2 : rem (h + h + 1) ((h + h) * a + a) = 0 :=
    rem_zero_of_dv _ _ ⟨a, by lib_nat⟩
  rw [z1, z2]

theorem odd_f (h D a b : Nat) (hp : Pr (h + h + 1)) (e : a + b = h + h + 1) :
    rem (h + h + 1) (b * b * b + D * b) = rem (h + h + 1) ((h + h) * (a * a * a + D * a)) := by
  have rb := neg_cong h a b e
  have s1 : rem (h + h + 1) (b * b * b + D * b)
      = rem (h + h + 1) (((h + h) * a) * ((h + h) * a) * ((h + h) * a) + D * ((h + h) * a)) :=
    rem_add_congr2 (h + h) _ _ _ _
      (rem_mul_congr2 (h + h) _ _ _ _ (rem_mul_congr2 (h + h) _ _ _ _ rb rb) rb)
      (rem_mul_congr' (h + h) _ _ D rb)
  have e2 : ((h + h) * a) * ((h + h) * a) * ((h + h) * a) + D * ((h + h) * a)
      = ((h + h) * (h + h)) * ((h + h) * (a * a * a)) + (h + h) * (D * a) := by lib_nat
  have s2 : rem (h + h + 1) (((h + h) * (h + h)) * ((h + h) * (a * a * a)))
      = rem (h + h + 1) ((h + h) * (a * a * a)) := by
    rw [rem_mul_congr (h + h) _ 1 _ ((neg_sq' h hp).trans (rem_one _ (hh_pos h hp)).symm), Nat.one_mul]
  rw [s1, e2, rem_add_congr (h + h) _ _ _ s2]
  have e3 : (h + h) * (a * a * a) + (h + h) * (D * a) = (h + h) * (a * a * a + D * a) := by lib_nat
  rw [e3]

/-- **Σ_x legV(x³ + Dx) = p** at every prime p ≡ 3 (mod 4), every D. -/
theorem sum_leg (n D : Nat) (hp : Pr ((n + n + 1) + (n + n + 1) + 1)) :
    sumTo ((n + n + 1) + (n + n + 1) + 1) (fun x => legV ((n + n + 1) + (n + n + 1) + 1) (x * x * x + D * x))
      = (n + n + 1) + (n + n + 1) + 1 := by
  let h := n + n + 1
  show sumTo (h + h + 1) (fun x => legV (h + h + 1) (x * x * x + D * x)) = h + h + 1
  apply pair_count (h + h)
  · show legV (h + h + 1) (0 * 0 * 0 + D * 0) = 1
    have z : rem (h + h + 1) (0 * 0 * 0 + D * 0) = rem (h + h + 1) 0 := by rw [Nat.mul_zero, Nat.mul_zero]
    rw [legV_congr (h + h) _ _ z]
    unfold legV
    rw [rem_of_lt (h + h) 0 (Nat.zero_lt_succ _)]
    rfl
  · intro a b e _ _
    show legV (h + h + 1) (a * a * a + D * a) + legV (h + h + 1) (b * b * b + D * b) = 2
    rw [legV_congr (h + h) _ _ (odd_f h D a b hp e)]
    exact leg_pair n _ hp

end Supersingular2

#print axioms Supersingular2.legV_congr
#print axioms Supersingular2.neg_cong
#print axioms Supersingular2.odd_f
#print axioms Supersingular2.sum_leg

bsd/SupersingularCount.lean

lean_all/bsd/SupersingularCount.lean

/-
  bsd/SupersingularCount.lean — T39 cycle 65: #E(F_p) = p + 1 for y² = x³ + Dx at every prime p ≡ 3 (mod 4).

  Lean 4 core plus bsd/SqCount (sq_count) and bsd/Supersingular2 (sum_leg). Every D (so every CM curve
  y² = x³ − D₀x, D = p − D₀, including the congruent number curves y² = x³ − n²x), every such prime:
    nE_ss        #{(x, y) ∈ F_p² : y² = x³ + Dx} + 1 = p + 1,   i.e.  a_p = 0
  This is the Hasse bound (with equality to p + 1) for infinitely many primes and a whole family, beyond
  the finite checks of BSDChain.hasse_check: a first infinite case of hasse_all_p.
-/
import bsd.SqCount
import bsd.Supersingular2

namespace SupersingularCount
open FTAChain FrobChain QRChain ESChain SqCount Supersingular2

def affSS (p D : Nat) : Nat :=
  sumTo p (fun x => sumTo p (fun y => b2n (Nat.beq (rem p (y * y)) (rem p (x * x * x + D * x)))))

/-- **a_p = 0**: #E(F_p) = p + 1 for y² = x³ + Dx, p ≡ 3 (mod 4) prime. -/
theorem nE_ss (n D : Nat) (hp : Pr ((n + n + 1) + (n + n + 1) + 1)) :
    affSS ((n + n + 1) + (n + n + 1) + 1) D + 1 = ((n + n + 1) + (n + n + 1) + 1) + 1 := by
  let P := (n + n + 1) + (n + n + 1) + 1
  show affSS P D + 1 = P + 1
  have e : affSS P D = sumTo P (fun x => legV P (x * x * x + D * x)) :=
    sum_ext _ _ _ (fun x _ => sq_count (n + n + 1) (x * x * x + D * x) hp)
  rw [e, sum_leg n D hp]

end SupersingularCount

#print axioms SupersingularCount.nE_ss

bsd/TP1254.lean

lean_all/bsd/TP1254.lean

/-
  bsd/TP1254.lean — T39 (goal G289f-4h-5e2/5e3): the image V of E_1254 (32 classes: three Rank3Cert points and E[2])
  and five translation points away from E[2] whose first coordinates span all 32 sign/prime classes, with their
  certificates (points and square roots from tools/g289f4h_tp.py, a computation tool; everything is rechecked here by rfl).
    beqB, beqL, beqCL, inV1254    V-membership by Boolean comparison (no decidability instances)
    vtor1254                       the classes of E[2] lie in V
    tp1 … tp5, ok1 … ok5           the translation points and AssembleN.TPok for each
  Lean 4 core plus bsd/AssembleN, bsd/Tor1254, bsd/PtCertZ.
-/
import bsd.AssembleN
import bsd.Tor1254
import bsd.PtCertZ

set_option maxRecDepth 100000

namespace TP1254
open DetChain LibRing LibRat LibInt QModel QCls SqUniqueL SqClassL TransStepL StepN DblClsN PtClsN PtClsN2 Rank3FinalN
  QModelZ TorGen Tor1254 AssembleN PtCertZ

def beqB : Bool → Bool → Bool
  | true, true => true
  | true, false => false
  | false, true => false
  | false, false => true

theorem beqB_eq : ∀ a b : Bool, beqB a b = true → a = b
  | true, true, _ => rfl
  | true, false, h => Bool.noConfusion h
  | false, true, h => Bool.noConfusion h
  | false, false, _ => rfl

def beqL : List Bool → List Bool → Bool
  | [], [] => true
  | [], _ :: _ => false
  | _ :: _, [] => false
  | a :: l, b :: m => beqB a b && beqL l m

def beqCL (c d : CL) : Bool := beqB c.s d.s && beqL c.b d.b

def Vlist : List (CL × CL) := [
  (⟨false, [false, false, false, false]⟩, ⟨false, [false, false, false, false]⟩),
  (⟨false, [false, false, false, true]⟩, ⟨false, [true, false, true, false]⟩),
  (⟨false, [false, false, true, false]⟩, ⟨false, [true, true, false, false]⟩),
  (⟨false, [false, false, true, true]⟩, ⟨false, [false, true, true, false]⟩),
  (⟨false, [false, true, false, false]⟩, ⟨false, [true, false, false, true]⟩),
  (⟨false, [false, true, false, true]⟩, ⟨false, [false, false, true, true]⟩),
  (⟨false, [false, true, true, false]⟩, ⟨false, [false, true, false, true]⟩),
  (⟨false, [false, true, true, true]⟩, ⟨false, [true, true, true, true]⟩),
  (⟨false, [true, false, false, false]⟩, ⟨false, [false, true, true, true]⟩),
  (⟨false, [true, false, false, true]⟩, ⟨false, [true, true, false, true]⟩),
  (⟨false, [true, false, true, false]⟩, ⟨false, [true, false, true, true]⟩),
  (⟨false, [true, false, true, true]⟩, ⟨false, [false, false, false, true]⟩),
  (⟨false, [true, true, false, false]⟩, ⟨false, [true, true, true, false]⟩),
  (⟨false, [true, true, false, true]⟩, ⟨false, [false, true, false, false]⟩),
  (⟨false, [true, true, true, false]⟩, ⟨false, [false, false, true, false]⟩),
  (⟨false, [true, true, true, true]⟩, ⟨false, [true, false, false, false]⟩),
  (⟨true, [false, false, false, false]⟩, ⟨true, [true, true, true, true]⟩),
  (⟨true, [false, false, false, true]⟩, ⟨true, [false, true, false, true]⟩),
  (⟨true, [false, false, true, false]⟩, ⟨true, [false, false, true, true]⟩),
  (⟨true, [false, false, true, true]⟩, ⟨true, [true, false, false, true]⟩),
  (⟨true, [false, true, false, false]⟩, ⟨true, [false, true, true, false]⟩),
  (⟨true, [false, true, false, true]⟩, ⟨true, [true, true, false, false]⟩),
  (⟨true, [false, true, true, false]⟩, ⟨true, [true, false, true, false]⟩),
  (⟨true, [false, true, true, true]⟩, ⟨true, [false, false, false, false]⟩),
  (⟨true, [true, false, false, false]⟩, ⟨true, [true, false, false, false]⟩),
  (⟨true, [true, false, false, true]⟩, ⟨true, [false, false, true, false]⟩),
  (⟨true, [true, false, true, false]⟩, ⟨true, [false, true, false, false]⟩),
  (⟨true, [true, false, true, true]⟩, ⟨true, [true, true, true, false]⟩),
  (⟨true, [true, true, false, false]⟩, ⟨true, [false, false, false, true]⟩),
  (⟨true, [true, true, false, true]⟩, ⟨true, [true, false, true, true]⟩),
  (⟨true, [true, true, true, false]⟩, ⟨true, [true, true, false, true]⟩),
  (⟨true, [true, true, true, true]⟩, ⟨true, [false, true, true, true]⟩)]

def inV1254 (β β' : CL) : Bool := Vlist.any (fun p => beqCL p.1 β && beqCL p.2 β')

theorem vtor1254 : ∀ s t : Bool, inV1254 (tcA P1254 td1254 s t) (tcB P1254 td1254 s t) = true
  | false, false => rfl
  | true, false => rfl
  | false, true => rfl
  | true, true => rfl

def invChk (c c' : CL) : Bool :=
  allCL 4 (fun β => allCL 4 (fun β' => beqB (inV1254 (cxor β c) (cxor β' c')) (inV1254 β β')))

theorem inv_of_chk (c c' : CL) (h : invChk c c' = true) :
    ∀ β β' : CL, WF P1254 β → WF P1254 β' → inV1254 (cxor β c) (cxor β' c') = inV1254 β β' := fun β β' w w' =>
  beqB_eq _ _ (allCL_app P1254 _ (allCL_app P1254 _ h β w) β' w')

theorem ne_ofNat (k : Nat) : Int.ofNat (k + 1) ≠ 0 := fun h => Nat.noConfusion (Int.ofNat.inj h)

-- point 1: x = -98
def tp1 : TP := ⟨PtCert.mkQ (-98) 1 (Nat.succ_pos 0) (0) (1) rfl, PtCert.mkQ (12376) 1 (Nat.succ_pos 0) (0) (1) rfl, ⟨true, [true, false, false, false]⟩, ⟨true, [true, false, false, false]⟩⟩
theorem ok1 : TPok n1254 P1254 inV1254 tp1 :=
  ⟨onE_of_intZ 1254 _ _ rfl, nd_ofZ 1254 _ rfl rfl rfl,
   hc_of_intZ 1254 P1254 _ (PtCert.mkQ (7) 1 (Nat.succ_pos 0) (0) (1) rfl) (PtCert.mkQ (26) 1 (Nat.succ_pos 0) (0) (1) rfl) _ _ _ _
     (fun h => Nat.noConfusion (Int.ofNat.inj h)) (fun h => Nat.noConfusion (Int.ofNat.inj h)) rfl rfl,
   rfl, rfl, inv_of_chk _ _ rfl⟩

-- point 2: x = -171
def tp2 : TP := ⟨PtCert.mkQ (-171) 1 (Nat.succ_pos 0) (0) (1) rfl, PtCert.mkQ (16245) 1 (Nat.succ_pos 0) (0) (1) rfl, ⟨true, [false, false, false, true]⟩, ⟨true, [false, true, false, true]⟩⟩
theorem ok2 : TPok n1254 P1254 inV1254 tp2 :=
  ⟨onE_of_intZ 1254 _ _ rfl, nd_ofZ 1254 _ rfl rfl rfl,
   hc_of_intZ 1254 P1254 _ (PtCert.mkQ (3) 1 (Nat.succ_pos 0) (0) (1) rfl) (PtCert.mkQ (5) 1 (Nat.succ_pos 0) (0) (1) rfl) _ _ _ _
     (fun h => Nat.noConfusion (Int.ofNat.inj h)) (fun h => Nat.noConfusion (Int.ofNat.inj h)) rfl rfl,
   rfl, rfl, inv_of_chk _ _ rfl⟩

-- point 3: x = -363
def tp3 : TP := ⟨PtCert.mkQ (-363) 1 (Nat.succ_pos 0) (0) (1) rfl, PtCert.mkQ (22869) 1 (Nat.succ_pos 0) (0) (1) rfl, ⟨true, [false, true, false, false]⟩, ⟨true, [false, true, true, false]⟩⟩
theorem ok3 : TPok n1254 P1254 inV1254 tp3 :=
  ⟨onE_of_intZ 1254 _ _ rfl, nd_ofZ 1254 _ rfl rfl rfl,
   hc_of_intZ 1254 P1254 _ (PtCert.mkQ (11) 1 (Nat.succ_pos 0) (0) (1) rfl) (PtCert.mkQ (7) 1 (Nat.succ_pos 0) (0) (1) rfl) _ _ _ _
     (fun h => Nat.noConfusion (Int.ofNat.inj h)) (fun h => Nat.noConfusion (Int.ofNat.inj h)) rfl rfl,
   rfl, rfl, inv_of_chk _ _ rfl⟩

-- point 4: x = -912
def tp4 : TP := ⟨PtCert.mkQ (-912) 1 (Nat.succ_pos 0) (0) (1) rfl, PtCert.mkQ (25992) 1 (Nat.succ_pos 0) (0) (1) rfl, ⟨true, [false, true, false, true]⟩, ⟨true, [true, true, false, false]⟩⟩
theorem ok4 : TPok n1254 P1254 inV1254 tp4 :=
  ⟨onE_of_intZ 1254 _ _ rfl, nd_ofZ 1254 _ rfl rfl rfl,
   hc_of_intZ 1254 P1254 _ (PtCert.mkQ (4) 1 (Nat.succ_pos 0) (0) (1) rfl) (PtCert.mkQ (19) 1 (Nat.succ_pos 0) (0) (1) rfl) _ _ _ _
     (fun h => Nat.noConfusion (Int.ofNat.inj h)) (fun h => Nat.noConfusion (Int.ofNat.inj h)) rfl rfl,
   rfl, rfl, inv_of_chk _ _ rfl⟩

-- point 5: x = 1650
def tp5 : TP := ⟨PtCert.mkQ (1650) 1 (Nat.succ_pos 0) (0) (1) rfl, PtCert.mkQ (-43560) 1 (Nat.succ_pos 0) (0) (1) rfl, ⟨false, [true, true, true, false]⟩, ⟨false, [false, false, true, false]⟩⟩
theorem ok5 : TPok n1254 P1254 inV1254 tp5 :=
  ⟨onE_of_intZ 1254 _ _ rfl, nd_ofZ 1254 _ rfl rfl rfl,
   hc_of_intZ 1254 P1254 _ (PtCert.mkQ (5) 1 (Nat.succ_pos 0) (0) (1) rfl) (PtCert.mkQ (6) 1 (Nat.succ_pos 0) (0) (1) rfl) _ _ _ _
     (fun h => Nat.noConfusion (Int.ofNat.inj h)) (fun h => Nat.noConfusion (Int.ofNat.inj h)) rfl rfl,
   rfl, rfl, inv_of_chk _ _ rfl⟩

end TP1254

#print axioms TP1254.beqB_eq
#print axioms TP1254.vtor1254
#print axioms TP1254.inv_of_chk
#print axioms TP1254.ne_ofNat
#print axioms TP1254.ok1
#print axioms TP1254.ok2
#print axioms TP1254.ok3
#print axioms TP1254.ok4
#print axioms TP1254.ok5

bsd/TP48622.lean

lean_all/bsd/TP48622.lean

/-
  bsd/TP48622.lean — T39 (goal G289f-4h-5e2/5e3): the image V of E_48622 (32 classes: three Rank3Cert points and E[2])
  and five translation points away from E[2] whose first coordinates span all 32 sign/prime classes, with their
  certificates (points and square roots from tools/g289f4h_tp.py, a computation tool; everything is rechecked here by rfl).
    beqB, beqL, beqCL, inV48622    V-membership by Boolean comparison (no decidability instances)
    vtor48622                       the classes of E[2] lie in V
    tp1 … tp5, ok1 … ok5           the translation points and AssembleN.TPok for each
  Lean 4 core plus bsd/AssembleN, bsd/Tor48622, bsd/PtCertZ.
-/
import bsd.AssembleN
import bsd.Tor48622
import bsd.PtCertZ

set_option maxRecDepth 100000

namespace TP48622
open DetChain LibRing LibRat LibInt QModel QCls SqUniqueL SqClassL TransStepL StepN DblClsN PtClsN PtClsN2 Rank3FinalN
  QModelZ TorGen Tor48622 AssembleN PtCertZ

def beqB : Bool → Bool → Bool
  | true, true => true
  | true, false => false
  | false, true => false
  | false, false => true

theorem beqB_eq : ∀ a b : Bool, beqB a b = true → a = b
  | true, true, _ => rfl
  | true, false, h => Bool.noConfusion h
  | false, true, h => Bool.noConfusion h
  | false, false, _ => rfl

def beqL : List Bool → List Bool → Bool
  | [], [] => true
  | [], _ :: _ => false
  | _ :: _, [] => false
  | a :: l, b :: m => beqB a b && beqL l m

def beqCL (c d : CL) : Bool := beqB c.s d.s && beqL c.b d.b

def Vlist : List (CL × CL) := [
  (⟨false, [false, false, false, false]⟩, ⟨false, [false, false, false, false]⟩),
  (⟨false, [false, false, false, true]⟩, ⟨false, [true, false, false, false]⟩),
  (⟨false, [false, false, true, false]⟩, ⟨false, [true, false, true, true]⟩),
  (⟨false, [false, false, true, true]⟩, ⟨false, [false, false, true, true]⟩),
  (⟨false, [false, true, false, false]⟩, ⟨false, [true, false, true, true]⟩),
  (⟨false, [false, true, false, true]⟩, ⟨false, [false, false, true, true]⟩),
  (⟨false, [false, true, true, false]⟩, ⟨false, [false, false, false, false]⟩),
  (⟨false, [false, true, true, true]⟩, ⟨false, [true, false, false, false]⟩),
  (⟨false, [true, false, false, false]⟩, ⟨false, [false, false, false, false]⟩),
  (⟨false, [true, false, false, true]⟩, ⟨false, [true, false, false, false]⟩),
  (⟨false, [true, false, true, false]⟩, ⟨false, [true, false, true, true]⟩),
  (⟨false, [true, false, true, true]⟩, ⟨false, [false, false, true, true]⟩),
  (⟨false, [true, true, false, false]⟩, ⟨false, [true, false, true, true]⟩),
  (⟨false, [true, true, false, true]⟩, ⟨false, [false, false, true, true]⟩),
  (⟨false, [true, true, true, false]⟩, ⟨false, [false, false, false, false]⟩),
  (⟨false, [true, true, true, true]⟩, ⟨false, [true, false, false, false]⟩),
  (⟨true, [false, false, false, false]⟩, ⟨true, [true, true, true, true]⟩),
  (⟨true, [false, false, false, true]⟩, ⟨true, [false, true, true, true]⟩),
  (⟨true, [false, false, true, false]⟩, ⟨true, [false, true, false, false]⟩),
  (⟨true, [false, false, true, true]⟩, ⟨true, [true, true, false, false]⟩),
  (⟨true, [false, true, false, false]⟩, ⟨true, [false, true, false, false]⟩),
  (⟨true, [false, true, false, true]⟩, ⟨true, [true, true, false, false]⟩),
  (⟨true, [false, true, true, false]⟩, ⟨true, [true, true, true, true]⟩),
  (⟨true, [false, true, true, true]⟩, ⟨true, [false, true, true, true]⟩),
  (⟨true, [true, false, false, false]⟩, ⟨true, [true, true, true, true]⟩),
  (⟨true, [true, false, false, true]⟩, ⟨true, [false, true, true, true]⟩),
  (⟨true, [true, false, true, false]⟩, ⟨true, [false, true, false, false]⟩),
  (⟨true, [true, false, true, true]⟩, ⟨true, [true, true, false, false]⟩),
  (⟨true, [true, true, false, false]⟩, ⟨true, [false, true, false, false]⟩),
  (⟨true, [true, true, false, true]⟩, ⟨true, [true, true, false, false]⟩),
  (⟨true, [true, true, true, false]⟩, ⟨true, [true, true, true, true]⟩),
  (⟨true, [true, true, true, true]⟩, ⟨true, [false, true, true, true]⟩)]

def inV48622 (β β' : CL) : Bool := Vlist.any (fun p => beqCL p.1 β && beqCL p.2 β')

theorem vtor48622 : ∀ s t : Bool, inV48622 (tcA P48622 td48622 s t) (tcB P48622 td48622 s t) = true
  | false, false => rfl
  | true, false => rfl
  | false, true => rfl
  | true, true => rfl

def invChk (c c' : CL) : Bool :=
  allCL 4 (fun β => allCL 4 (fun β' => beqB (inV48622 (cxor β c) (cxor β' c')) (inV48622 β β')))

theorem inv_of_chk (c c' : CL) (h : invChk c c' = true) :
    ∀ β β' : CL, WF P48622 β → WF P48622 β' → inV48622 (cxor β c) (cxor β' c') = inV48622 β β' := fun β β' w w' =>
  beqB_eq _ _ (allCL_app P48622 _ (allCL_app P48622 _ h β w) β' w')

theorem ne_ofNat (k : Nat) : Int.ofNat (k + 1) ≠ 0 := fun h => Nat.noConfusion (Int.ofNat.inj h)

-- point 1: x = -42350
def tp1 : TP := ⟨PtCert.mkQ (-42350) 1 (Nat.succ_pos 0) (0) (1) rfl, PtCert.mkQ (4915680) 1 (Nat.succ_pos 0) (0) (1) rfl, ⟨true, [true, true, false, false]⟩, ⟨true, [false, true, false, false]⟩⟩
theorem ok1 : TPok n48622 P48622 inV48622 tp1 :=
  ⟨onE_of_intZ 48622 _ _ rfl, nd_ofZ 48622 _ rfl rfl rfl,
   hc_of_intZ 48622 P48622 _ (PtCert.mkQ (55) 1 (Nat.succ_pos 0) (0) (1) rfl) (PtCert.mkQ (114) 1 (Nat.succ_pos 0) (0) (1) rfl) _ _ _ _
     (fun h => Nat.noConfusion (Int.ofNat.inj h)) (fun h => Nat.noConfusion (Int.ofNat.inj h)) rfl rfl,
   rfl, rfl, inv_of_chk _ _ rfl⟩

-- point 2: x = -55223/16
def tp2 : TP := ⟨PtCert.mkQ (-55223) 16 (Nat.succ_pos 15) (7) (-24160) rfl, PtCert.mkQ (182354235) 64 (Nat.succ_pos 63) (-13) (37040704) rfl, ⟨true, [false, false, true, false]⟩, ⟨true, [false, true, false, false]⟩⟩
theorem ok2 : TPok n48622 P48622 inV48622 tp2 :=
  ⟨onE_of_intZ 48622 _ _ rfl, nd_ofZ 48622 _ rfl rfl rfl,
   hc_of_intZ 48622 P48622 _ (PtCert.mkQ (49) 4 (Nat.succ_pos 3) (1) (-12) rfl) (PtCert.mkQ (345) 4 (Nat.succ_pos 3) (1) (-86) rfl) _ _ _ _
     (fun h => Nat.noConfusion (Int.ofNat.inj h)) (fun h => Nat.noConfusion (Int.ofNat.inj h)) rfl rfl,
   rfl, rfl, inv_of_chk _ _ rfl⟩

-- point 3: x = 3250969600
def tp3 : TP := ⟨PtCert.mkQ (3250969600) 1 (Nat.succ_pos 0) (0) (1) rfl, PtCert.mkQ (185361426298560) 1 (Nat.succ_pos 0) (0) (1) rfl, ⟨false, [false, false, false, true]⟩, ⟨false, [true, false, false, false]⟩⟩
theorem ok3 : TPok n48622 P48622 inV48622 tp3 :=
  ⟨onE_of_intZ 48622 _ _ rfl, nd_ofZ 48622 _ rfl rfl rfl,
   hc_of_intZ 48622 P48622 _ (PtCert.mkQ (4640) 1 (Nat.succ_pos 0) (0) (1) rfl) (PtCert.mkQ (40317) 1 (Nat.succ_pos 0) (0) (1) rfl) _ _ _ _
     (fun h => Nat.noConfusion (Int.ofNat.inj h)) (fun h => Nat.noConfusion (Int.ofNat.inj h)) rfl rfl,
   rfl, rfl, inv_of_chk _ _ rfl⟩

-- point 4: x = 11283777/16
def tp4 : TP := ⟨PtCert.mkQ (11283777) 16 (Nat.succ_pos 15) (1) (-705236) rfl, PtCert.mkQ (-37813520415) 64 (Nat.succ_pos 63) (31) (-18315923951) rfl, ⟨false, [false, false, true, true]⟩, ⟨false, [false, false, true, true]⟩⟩
theorem ok4 : TPok n48622 P48622 inV48622 tp4 :=
  ⟨onE_of_intZ 48622 _ _ rfl, nd_ofZ 48622 _ rfl rfl rfl,
   hc_of_intZ 48622 P48622 _ (PtCert.mkQ (57) 4 (Nat.succ_pos 3) (1) (-14) rfl) (PtCert.mkQ (55) 4 (Nat.succ_pos 3) (-1) (14) rfl) _ _ _ _
     (fun h => Nat.noConfusion (Int.ofNat.inj h)) (fun h => Nat.noConfusion (Int.ofNat.inj h)) rfl rfl,
   rfl, rfl, inv_of_chk _ _ rfl⟩

-- point 5: x = 12353369/100
def tp5 : TP := ⟨PtCert.mkQ (12353369) 100 (Nat.succ_pos 99) (29) (-3582477) rfl, PtCert.mkQ (39914270403) 1000 (Nat.succ_pos 999) (67) (-2674256117) rfl, ⟨false, [false, true, true, false]⟩, ⟨false, [false, false, false, false]⟩⟩
theorem ok5 : TPok n48622 P48622 inV48622 tp5 :=
  ⟨onE_of_intZ 48622 _ _ rfl, nd_ofZ 48622 _ rfl rfl rfl,
   hc_of_intZ 48622 P48622 _ (PtCert.mkQ (277) 10 (Nat.succ_pos 9) (3) (-83) rfl) (PtCert.mkQ (2737) 10 (Nat.succ_pos 9) (3) (-821) rfl) _ _ _ _
     (fun h => Nat.noConfusion (Int.ofNat.inj h)) (fun h => Nat.noConfusion (Int.ofNat.inj h)) rfl rfl,
   rfl, rfl, inv_of_chk _ _ rfl⟩

end TP48622

#print axioms TP48622.beqB_eq
#print axioms TP48622.vtor48622
#print axioms TP48622.inv_of_chk
#print axioms TP48622.ne_ofNat
#print axioms TP48622.ok1
#print axioms TP48622.ok2
#print axioms TP48622.ok3
#print axioms TP48622.ok4
#print axioms TP48622.ok5

bsd/Tor1254.lean

lean_all/bsd/Tor1254.lean

/-
  bsd/Tor1254.lean — T39 (goal G289f-4h-5e, first piece): the torsion data StepN.TorData, the relations
  PtClsN2.TorRel and the hypotheses Rank3FinalN.Hyp for E_n, n = 1254, ps = [2, 3, 11, 19] (as bsd/TorGen,
  bsd/TorGen2 for n = 42486), all by rfl.
    td1254, tr1254, h0n_1254, hn0_1254, hm0_1254, H1254
  Lean 4 core plus bsd/TorGen, bsd/Rank3FinalN, bsd/QModelZ.
-/
import bsd.TorGen
import bsd.Rank3FinalN
import bsd.QModelZ

namespace Tor1254
open DetChain LibRing LibRat LibInt QModel QCls B5S SqUniqueL SqClassL TransStepL StepN DblClsN PtClsN PtClsN2 Rank3FinalN QModelZ TorGen

def n1254 : Q := nz 1254
def P1254 : List Nat := [2, 3, 11, 19]

theorem negQ_n : negQ n1254 = normQ (-1254) 1 one_pos' := negQ_nz 1254

theorem e0n : sub ratR zeroQ n1254 = castQ (clsL true P1254 tttt * (1 * 1)) := cvalI 0 1254 _ rfl
theorem e0m : sub ratR zeroQ (negQ n1254) = castQ (clsL false P1254 tttt * (1 * 1)) := by
  rw [negQ_n]; exact cvalI 0 (-1254) _ rfl
theorem en0 : sub ratR n1254 zeroQ = castQ (clsL false P1254 tttt * (1 * 1)) := cvalI 1254 0 _ rfl
theorem enm : sub ratR n1254 (negQ n1254) = castQ (clsL false P1254 fttt * (2 * 2)) := by
  rw [negQ_n]; exact cvalI 1254 (-1254) _ rfl
theorem em0 : sub ratR (negQ n1254) zeroQ = castQ (clsL true P1254 tttt * (1 * 1)) := by
  rw [negQ_n]; exact cvalI (-1254) 0 _ rfl
theorem emn : sub ratR (negQ n1254) n1254 = castQ (clsL true P1254 fttt * (2 * 2)) := by
  rw [negQ_n]; exact cvalI (-1254) 1254 _ rfl

/-- **The torsion data of E_1254.** -/
def td1254 : TorData n1254 P1254 where
  k0n := true
  b0n := tttt
  m0n := 1
  k0m := false
  b0m := tttt
  m0m := 1
  kn0 := false
  bn0 := tttt
  mn0 := 1
  knm := false
  bnm := fttt
  mnm := 2
  km0 := true
  bm0 := tttt
  mm0 := 1
  kmn := true
  bmn := fttt
  mmn := 2
  l0n := rfl
  l0m := rfl
  ln0 := rfl
  lnm := rfl
  lm0 := rfl
  lmn := rfl
  h0n := TransStep2.one_ne
  h0m := TransStep2.one_ne
  hn0 := TransStep2.one_ne
  hnm := two_ne'
  hm0 := TransStep2.one_ne
  hmn := two_ne'
  e0n := e0n
  e0m := e0m
  en0 := en0
  enm := enm
  em0 := em0
  emn := emn

theorem chk_1254 : chkL P1254 = true := rfl
theorem pos_1254 : posL P1254 = true := rfl

theorem tttt_ne' (b : List Bool) (h : tttt = b) (h' : b = zL P1254) : False :=
  Bool.noConfusion (congrArg (fun l => l.headD false) (h.trans h'))

theorem h0n_1254 : ∀ T : Q, sub ratR zeroQ n1254 ≠ ratR.mul T T :=
  nonsq_of P1254 chk_1254 pos_1254 true tttt rfl 1 TransStep2.one_ne (fun h _ => Bool.noConfusion h) _ e0n

theorem hn0_1254 : ∀ T : Q, sub ratR n1254 zeroQ ≠ ratR.mul T T :=
  nonsq_of P1254 chk_1254 pos_1254 false tttt rfl 1 TransStep2.one_ne (fun _ h => tttt_ne' tttt rfl h) _ en0

theorem hm0_1254 : ∀ T : Q, sub ratR (negQ n1254) zeroQ ≠ ratR.mul T T :=
  nonsq_of P1254 chk_1254 pos_1254 true tttt rfl 1 TransStep2.one_ne (fun h _ => Bool.noConfusion h) _ em0

theorem tr1254 : TorRel n1254 P1254 td1254 where
  r01a := rfl
  r01b := rfl
  r02a := rfl
  r02b := rfl
  r12a := rfl
  r12b := rfl

theorem n_ne : n1254 ≠ zeroQ :=
  normQ_ne_zero one_pos' (fun h => Nat.noConfusion (Int.ofNat.inj h))

theorem H1254 : Hyp n1254 P1254 td1254 :=
  ⟨chk_1254, pos_1254, tr1254, n_ne, h0n_1254, hn0_1254, hm0_1254⟩

end Tor1254

#print axioms Tor1254.negQ_n
#print axioms Tor1254.e0n
#print axioms Tor1254.e0m
#print axioms Tor1254.en0
#print axioms Tor1254.enm
#print axioms Tor1254.em0
#print axioms Tor1254.emn
#print axioms Tor1254.chk_1254
#print axioms Tor1254.pos_1254
#print axioms Tor1254.tttt_ne'
#print axioms Tor1254.h0n_1254
#print axioms Tor1254.hn0_1254
#print axioms Tor1254.hm0_1254
#print axioms Tor1254.tr1254
#print axioms Tor1254.n_ne
#print axioms Tor1254.H1254

bsd/Tor48622.lean

lean_all/bsd/Tor48622.lean

/-
  bsd/Tor48622.lean — T39 (goal G289f-4h-5e, first piece): the torsion data StepN.TorData, the relations
  PtClsN2.TorRel and the hypotheses Rank3FinalN.Hyp for E_n, n = 48622, ps = [2, 7, 23, 151] (as bsd/TorGen,
  bsd/TorGen2 for n = 42486), all by rfl.
    td48622, tr48622, h0n_48622, hn0_48622, hm0_48622, H48622
  Lean 4 core plus bsd/TorGen, bsd/Rank3FinalN, bsd/QModelZ.
-/
import bsd.TorGen
import bsd.Rank3FinalN
import bsd.QModelZ

namespace Tor48622
open DetChain LibRing LibRat LibInt QModel QCls B5S SqUniqueL SqClassL TransStepL StepN DblClsN PtClsN PtClsN2 Rank3FinalN QModelZ TorGen

def n48622 : Q := nz 48622
def P48622 : List Nat := [2, 7, 23, 151]

theorem negQ_n : negQ n48622 = normQ (-48622) 1 one_pos' := negQ_nz 48622

theorem e0n : sub ratR zeroQ n48622 = castQ (clsL true P48622 tttt * (1 * 1)) := cvalI 0 48622 _ rfl
theorem e0m : sub ratR zeroQ (negQ n48622) = castQ (clsL false P48622 tttt * (1 * 1)) := by
  rw [negQ_n]; exact cvalI 0 (-48622) _ rfl
theorem en0 : sub ratR n48622 zeroQ = castQ (clsL false P48622 tttt * (1 * 1)) := cvalI 48622 0 _ rfl
theorem enm : sub ratR n48622 (negQ n48622) = castQ (clsL false P48622 fttt * (2 * 2)) := by
  rw [negQ_n]; exact cvalI 48622 (-48622) _ rfl
theorem em0 : sub ratR (negQ n48622) zeroQ = castQ (clsL true P48622 tttt * (1 * 1)) := by
  rw [negQ_n]; exact cvalI (-48622) 0 _ rfl
theorem emn : sub ratR (negQ n48622) n48622 = castQ (clsL true P48622 fttt * (2 * 2)) := by
  rw [negQ_n]; exact cvalI (-48622) 48622 _ rfl

/-- **The torsion data of E_48622.** -/
def td48622 : TorData n48622 P48622 where
  k0n := true
  b0n := tttt
  m0n := 1
  k0m := false
  b0m := tttt
  m0m := 1
  kn0 := false
  bn0 := tttt
  mn0 := 1
  knm := false
  bnm := fttt
  mnm := 2
  km0 := true
  bm0 := tttt
  mm0 := 1
  kmn := true
  bmn := fttt
  mmn := 2
  l0n := rfl
  l0m := rfl
  ln0 := rfl
  lnm := rfl
  lm0 := rfl
  lmn := rfl
  h0n := TransStep2.one_ne
  h0m := TransStep2.one_ne
  hn0 := TransStep2.one_ne
  hnm := two_ne'
  hm0 := TransStep2.one_ne
  hmn := two_ne'
  e0n := e0n
  e0m := e0m
  en0 := en0
  enm := enm
  em0 := em0
  emn := emn

theorem chk_48622 : chkL P48622 = true := rfl
theorem pos_48622 : posL P48622 = true := rfl

theorem tttt_ne' (b : List Bool) (h : tttt = b) (h' : b = zL P48622) : False :=
  Bool.noConfusion (congrArg (fun l => l.headD false) (h.trans h'))

theorem h0n_48622 : ∀ T : Q, sub ratR zeroQ n48622 ≠ ratR.mul T T :=
  nonsq_of P48622 chk_48622 pos_48622 true tttt rfl 1 TransStep2.one_ne (fun h _ => Bool.noConfusion h) _ e0n

theorem hn0_48622 : ∀ T : Q, sub ratR n48622 zeroQ ≠ ratR.mul T T :=
  nonsq_of P48622 chk_48622 pos_48622 false tttt rfl 1 TransStep2.one_ne (fun _ h => tttt_ne' tttt rfl h) _ en0

theorem hm0_48622 : ∀ T : Q, sub ratR (negQ n48622) zeroQ ≠ ratR.mul T T :=
  nonsq_of P48622 chk_48622 pos_48622 true tttt rfl 1 TransStep2.one_ne (fun h _ => Bool.noConfusion h) _ em0

theorem tr48622 : TorRel n48622 P48622 td48622 where
  r01a := rfl
  r01b := rfl
  r02a := rfl
  r02b := rfl
  r12a := rfl
  r12b := rfl

theorem n_ne : n48622 ≠ zeroQ :=
  normQ_ne_zero one_pos' (fun h => Nat.noConfusion (Int.ofNat.inj h))

theorem H48622 : Hyp n48622 P48622 td48622 :=
  ⟨chk_48622, pos_48622, tr48622, n_ne, h0n_48622, hn0_48622, hm0_48622⟩

end Tor48622

#print axioms Tor48622.negQ_n
#print axioms Tor48622.e0n
#print axioms Tor48622.e0m
#print axioms Tor48622.en0
#print axioms Tor48622.enm
#print axioms Tor48622.em0
#print axioms Tor48622.emn
#print axioms Tor48622.chk_48622
#print axioms Tor48622.pos_48622
#print axioms Tor48622.tttt_ne'
#print axioms Tor48622.h0n_48622
#print axioms Tor48622.hn0_48622
#print axioms Tor48622.hm0_48622
#print axioms Tor48622.tr48622
#print axioms Tor48622.n_ne
#print axioms Tor48622.H48622

bsd/TorCls0.lean

lean_all/bsd/TorCls0.lean

/-
  bsd/TorCls0.lean — T39 (goal G289e-7b3, case 3b for T0 = (0, 0)): for a point P = (x, y) of E_42486 away from E[2]
  with class (γ₁, γ₂), the translate P + T0 (abscissa TorTrans.xT with e₁ = 0, v = x⁻¹) is away from E[2] with class
  (κ₁ ⊕ γ₁, κ₂ ⊕ γ₂), (κ₁, κ₂) = ((−1), (−n)) the class of T0.
      x' = (−1)·x·(nv)²,   x' − n = (−n)(x + n)·x·v²,   x' + n = n(x − n)·x·v²     (TorTrans.tor_d1..d3)
    gen_x, gen_xn, gen_xm      the three products, any commutative ring, class constants abstract
    sq_mul, prod2, prod3       c²w² = (cw)²; cq κ·cq γ and cq κ·cq γ·cq δ as one class times a square
    bxor3, xor3                (a ⊕ (b ⊕ c)) ⊕ b = a ⊕ c
    cls_T0                     ND and the class of P + T0
  Lean 4 core plus bsd/StepFull, bsd/DblCls, bsd/TorTrans.
-/
import bsd.StepFull
import bsd.DblCls
import bsd.TorTrans
import bsd.Kernel3

namespace TorCls0
open DetChain LibRing

section gen
variable {R : Type} (K : CRing R)

theorem gen_x (e1 e2 e3 x2 v s c0 cm c1 : R) (hA : TorTrans.A K e1 e2 e3 = K.mul c0 cm)
    (hx2 : sub K x2 e1 = K.mul c1 (K.mul s s)) (hv : K.mul (sub K x2 e1) v = K.one) :
    sub K (TorTrans.xT K e1 e2 e3 v) e1
      = K.mul (K.mul (K.mul c0 cm) c1) (K.mul (K.mul v s) (K.mul v s)) := by
  rw [TorTrans.tor_d1 K e1 e2 e3 x2 v hv, hA, hx2]; lib_ring

theorem gen_xn (e1 n e3 x2 v s T c1 c12 c0 : R) (h0 : sub K e1 n = c0) (hx2 : sub K x2 e1 = K.mul c1 (K.mul s s))
    (hm : sub K x2 e3 = K.mul c12 (K.mul T T)) (hv : K.mul (sub K x2 e1) v = K.one) :
    sub K (TorTrans.xT K e1 n e3 v) n
      = K.mul (K.mul (K.mul c0 c12) c1) (K.mul (K.mul (K.mul T s) v) (K.mul (K.mul T s) v)) := by
  rw [TorTrans.tor_d2 K e1 n e3 x2 v hv, hm, hx2, h0]; lib_ring

theorem gen_xm (n x2 v : R) (hv : K.mul (sub K x2 K.zero) v = K.one) :
    sub K (TorTrans.xT K K.zero n (K.neg n) v) (K.neg n)
      = K.mul (K.mul (K.mul (sub K K.zero (K.neg n)) (sub K x2 n)) (sub K x2 K.zero)) (K.mul v v) :=
  TorTrans.tor_d3 K K.zero n (K.neg n) x2 v hv

theorem sub0' (x : R) : sub K x K.zero = x := by unfold LibRing.sub; lib_ring

theorem sub_zero_neg (n : R) : n = sub K K.zero (K.neg n) := by unfold LibRing.sub; lib_ring

theorem sq_mul (c w : R) : K.mul (K.mul c c) (K.mul w w) = K.mul (K.mul c w) (K.mul c w) := by lib_ring

end gen

open LibRat QModel QCls B5S TransStep TransStep2 StepFull DblCls

theorem ne_of_eqQ {a b : Q} (h : a = b) (hb : b ≠ zeroQ) : a ≠ zeroQ := fun e => hb (h.symm.trans e)

theorem prod2 (κ γ : B5) (w : Q) :
    ratR.mul (ratR.mul (cq κ) (cq γ)) (ratR.mul w w)
      = ratR.mul (cq (κ.xor γ)) (ratR.mul (ratR.mul (cq (κ.and γ)) w) (ratR.mul (cq (κ.and γ)) w)) := by
  rw [cq_mul κ γ]; lib_ring

theorem prod3 (κ γ δ : B5) (w : Q) :
    ratR.mul (ratR.mul (ratR.mul (cq κ) (cq γ)) (cq δ)) (ratR.mul w w)
      = ratR.mul (cq ((κ.xor γ).xor δ))
          (ratR.mul (ratR.mul (ratR.mul (cq ((κ.xor γ).and δ)) (cq (κ.and γ))) w)
            (ratR.mul (ratR.mul (cq ((κ.xor γ).and δ)) (cq (κ.and γ))) w)) := by
  calc ratR.mul (ratR.mul (ratR.mul (cq κ) (cq γ)) (cq δ)) (ratR.mul w w)
      = ratR.mul (ratR.mul (cq (κ.xor γ)) (cq δ))
          (ratR.mul (ratR.mul (cq (κ.and γ)) (cq (κ.and γ))) (ratR.mul w w)) := by
        rw [cq_mul κ γ]; lib_ring
    _ = _ := by rw [cq_mul (κ.xor γ) δ]; lib_ring

theorem bxor3 : ∀ x y z : Bool, Bool.xor (Bool.xor x (Bool.xor y z)) y = Bool.xor x z := by
  intro x y z; cases x <;> cases y <;> cases z <;> rfl

theorem xor3 (a b c : B5) : (a.xor (b.xor c)).xor b = a.xor c := by
  cases a with
  | mk s1 a1 b1 c1 d1 =>
    cases b with
    | mk s2 a2 b2 c2 d2 =>
      cases c with
      | mk s3 a3 b3 c3 d3 =>
        show B5.mk (Bool.xor (Bool.xor s1 (Bool.xor s2 s3)) s2) (Bool.xor (Bool.xor a1 (Bool.xor a2 a3)) a2)
            (Bool.xor (Bool.xor b1 (Bool.xor b2 b3)) b2) (Bool.xor (Bool.xor c1 (Bool.xor c2 c3)) c2)
            (Bool.xor (Bool.xor d1 (Bool.xor d2 d3)) d2)
          = B5.mk (Bool.xor s1 s3) (Bool.xor a1 a3) (Bool.xor b1 b3) (Bool.xor c1 c3) (Bool.xor d1 d3)
        rw [bxor3 s1 s2 s3, bxor3 a1 a2 a3, bxor3 b1 b2 b3, bxor3 c1 c2 c3, bxor3 d1 d2 d3]

/-- the class of (−1) -/
def K1 : B5 := ⟨true, false, false, false, false⟩

theorem cqK1 : cq K1 = ratR.neg ratR.one := rfl

/-- **The translate by T0 is away from E[2] with class (K1 ⊕ γ₁, KT0 ⊕ γ₂).** -/
theorem cls_T0 (x2 y2 : Q) (o : OnE x2 y2) (nd : ND x2) (γ1 γ2 : B5) (hc : HC x2 γ1 γ2) :
    ND (TorTrans.xT ratR zeroQ nQ (negQ nQ) (invQ (sub ratR x2 zeroQ))) ∧
      HC (TorTrans.xT ratR zeroQ nQ (negQ nQ) (invQ (sub ratR x2 zeroQ))) (K1.xor γ1) (KT0.xor γ2) := by
  obtain ⟨T, eT⟩ := cls3 x2 y2 o γ1 γ2 hc nd.1 nd.2.1
  have hT : T ≠ zeroQ := T_ne _ _ T nd.2.2 eT
  obtain ⟨s, r, hs, _, ex, _⟩ := hc
  have h0 : sub ratR x2 zeroQ ≠ zeroQ := fun h => nd.1 ((sub0 x2).symm.trans h)
  have hv := mul_invQ _ h0
  have hv0 : invQ (sub ratR x2 zeroQ) ≠ zeroQ := Kernel3.ne_of_inv_r ratR Kernel2.two_one_ne _ _ hv
  have hx2 : sub ratR x2 zeroQ = ratR.mul (cq γ1) (ratR.mul s s) := (sub0 x2).trans ex
  have t0 : sub ratR zeroQ nQ = cq KT0 := tor_0n
  have tm : sub ratR zeroQ (negQ nQ) = cq KN := tor_0m
  have hA : TorTrans.A ratR zeroQ nQ (negQ nQ) = ratR.mul (cq KT0) (cq KN) := by
    unfold TorTrans.A; rw [t0, tm]
  have A := gen_x ratR zeroQ nQ (negQ nQ) x2 _ s (cq KT0) (cq KN) (cq γ1) hA hx2 hv
  have B := gen_xn ratR zeroQ nQ (negQ nQ) x2 _ s T (cq γ1) (cq (γ1.xor γ2)) (cq KT0) t0 hx2 eT hv
  have C := TorTrans.tor_d3 ratR zeroQ nQ (negQ nQ) x2 _ hv
  generalize invQ (sub ratR x2 zeroQ) = v at hv hv0 A B C ⊢
  generalize TorTrans.xT ratR zeroQ nQ (negQ nQ) v = X at A B C ⊢
  have A' := A.trans (prod3 KT0 KN γ1 _)
  rw [show KT0.xor KN = K1 from rfl] at A'
  have B' := B.trans (prod3 KT0 (γ1.xor γ2) γ1 _)
  rw [xor3] at B'
  have nx0 := ne_of_eqQ A' (mul_ne _ _ (cq_ne _)
    (mul_ne _ _ (mul_ne _ _ (mul_ne _ _ (cq_ne _) (cq_ne _)) (mul_ne _ _ hv0 hs))
      (mul_ne _ _ (mul_ne _ _ (cq_ne _) (cq_ne _)) (mul_ne _ _ hv0 hs))))
  have nxn := ne_of_eqQ B' (mul_ne _ _ (cq_ne _)
    (mul_ne _ _ (mul_ne _ _ (mul_ne _ _ (cq_ne _) (cq_ne _)) (mul_ne _ _ (mul_ne _ _ hT hs) hv0))
      (mul_ne _ _ (mul_ne _ _ (cq_ne _) (cq_ne _)) (mul_ne _ _ (mul_ne _ _ hT hs) hv0))))
  have k : sub ratR zeroQ (negQ nQ) ≠ zeroQ := fun h => cq_ne KN (tor_0m.symm.trans h)
  have nxm := ne_of_eqQ C (mul_ne _ _ (mul_ne _ _ (mul_ne _ _ k nd.2.1) h0) (mul_ne _ _ hv0 hv0))
  refine ⟨⟨ne_of_eqQ (sub0 X).symm nx0, nxn, nxm⟩, ?_⟩
  unfold HC HasCls
  refine ⟨_, _, ?_, ?_, (sub0 X).symm.trans A', B'⟩
  · exact mul_ne _ _ (mul_ne _ _ (cq_ne _) (cq_ne _)) (mul_ne _ _ hv0 hs)
  · exact mul_ne _ _ (mul_ne _ _ (cq_ne _) (cq_ne _)) (mul_ne _ _ (mul_ne _ _ hT hs) hv0)

end TorCls0

#print axioms TorCls0.gen_x
#print axioms TorCls0.gen_xn
#print axioms TorCls0.gen_xm
#print axioms TorCls0.sub0'
#print axioms TorCls0.sub_zero_neg
#print axioms TorCls0.sq_mul
#print axioms TorCls0.ne_of_eqQ
#print axioms TorCls0.prod2
#print axioms TorCls0.prod3
#print axioms TorCls0.bxor3
#print axioms TorCls0.xor3
#print axioms TorCls0.cqK1
#print axioms TorCls0.cls_T0

bsd/TorCls12.lean

lean_all/bsd/TorCls12.lean

/-
  bsd/TorCls12.lean — T39 (goal G289e-7b3, case 3b for T1 = (n, 0) and T2 = (−n, 0)): for P = (x, y) of E_42486 away
  from E[2] with class (γ₁, γ₂), the translates P + T1 and P + T2 are away from E[2] with classes
  (KN ⊕ γ₁, K2 ⊕ γ₂) and (KT0 ⊕ γ₁, Km2N ⊕ γ₂), the classes of T1 = ((n), (2)) and T2 = ((−n), (−2n)).
      P + T1:  x' − n = 2n²(x − n)v²,  x' = n(x + n)(x − n)v²,  x' + n = 2n·x(x − n)v²          (v = (x − n)⁻¹)
      P + T2:  x' + n = 2n²(x + n)v²,  x' = (−n)(x − n)(x + n)v²,  x' − n = (−2n)·x(x + n)v²   (v = (x + n)⁻¹)
    g1_xn, g1_x0, g2_x0, g2_xn     the products, any commutative ring
    xor3b, xor3c, xor3d            B5 identities
    cls_T1, cls_T2                 ND and the classes of the translates
  Lean 4 core plus bsd/TorCls0.
-/
import bsd.TorCls0

namespace TorCls12
open DetChain LibRing

section gen
variable {R : Type} (K : CRing R)

theorem g1_xn (e1 e2 e3 x2 v r w c0 c1 cg2 : R) (hA : TorTrans.A K e1 e2 e3 = K.mul c0 (K.mul c1 (K.mul w w)))
    (hr : sub K x2 e1 = K.mul cg2 (K.mul r r)) (hv : K.mul (sub K x2 e1) v = K.one) :
    sub K (TorTrans.xT K e1 e2 e3 v) e1
      = K.mul (K.mul (K.mul c0 c1) cg2) (K.mul (K.mul (K.mul w r) v) (K.mul (K.mul w r) v)) := by
  rw [TorTrans.tor_d1 K e1 e2 e3 x2 v hv, hA, hr]; lib_ring

theorem g1_x0 (e1 e2 e3 x2 v r T c0 cg12 cg2 : R) (h0 : sub K e1 e2 = c0)
    (hm : sub K x2 e3 = K.mul cg12 (K.mul T T)) (hr : sub K x2 e1 = K.mul cg2 (K.mul r r))
    (hv : K.mul (sub K x2 e1) v = K.one) :
    sub K (TorTrans.xT K e1 e2 e3 v) e2
      = K.mul (K.mul (K.mul c0 cg12) cg2) (K.mul (K.mul (K.mul T r) v) (K.mul (K.mul T r) v)) := by
  rw [TorTrans.tor_d2 K e1 e2 e3 x2 v hv, hm, hr, h0]; lib_ring

theorem g2_x0 (e1 e2 e3 x2 v r T c0 cg2 cg12 : R) (h0 : sub K e1 e2 = c0)
    (hr : sub K x2 e3 = K.mul cg2 (K.mul r r)) (hm : sub K x2 e1 = K.mul cg12 (K.mul T T))
    (hv : K.mul (sub K x2 e1) v = K.one) :
    sub K (TorTrans.xT K e1 e2 e3 v) e2
      = K.mul (K.mul (K.mul c0 cg2) cg12) (K.mul (K.mul (K.mul r T) v) (K.mul (K.mul r T) v)) := by
  rw [TorTrans.tor_d2 K e1 e2 e3 x2 v hv, hr, hm, h0]; lib_ring

theorem g2_xn (e1 e2 e3 x2 v s T c0 c2 cg1 cg12 : R) (h0 : sub K e1 e3 = K.mul c0 (K.mul c2 c2))
    (hx2 : sub K x2 e2 = K.mul cg1 (K.mul s s)) (hm : sub K x2 e1 = K.mul cg12 (K.mul T T))
    (hv : K.mul (sub K x2 e1) v = K.one) :
    sub K (TorTrans.xT K e1 e2 e3 v) e3
      = K.mul (K.mul (K.mul c0 cg1) cg12) (K.mul (K.mul (K.mul (K.mul c2 s) T) v) (K.mul (K.mul (K.mul c2 s) T) v)) := by
  rw [TorTrans.tor_d3 K e1 e2 e3 x2 v hv, hm, h0, hx2]; lib_ring

theorem two_n_ne (hK : NoZD K) (h2 : TrianglePoint.two K K.one ≠ K.zero) (n : R) (hn : n ≠ K.zero) :
    sub K n (K.neg n) ≠ K.zero := fun h =>
  Kernel2.mul_ne_gen K hK _ _ h2 hn
    ((show K.mul (TrianglePoint.two K K.one) n = sub K n (K.neg n) by
      unfold TrianglePoint.two LibRing.sub; lib_ring).trans h)

theorem two_n_ne' (hK : NoZD K) (h2 : TrianglePoint.two K K.one ≠ K.zero) (n : R) (hn : n ≠ K.zero) :
    sub K (K.neg n) n ≠ K.zero := fun h =>
  Kernel2.mul_ne_gen K hK _ _ h2 hn
    ((show K.mul (TrianglePoint.two K K.one) n = K.neg (sub K (K.neg n) n) by
      unfold TrianglePoint.two LibRing.sub; lib_ring).trans (by rw [h]; lib_ring))

end gen

open LibRat QModel QCls B5S TransStep TransStep2 StepFull DblCls TorCls0

theorem bxor3b : ∀ x y z : Bool, Bool.xor (Bool.xor x (Bool.xor y z)) z = Bool.xor x y := by
  intro x y z; cases x <;> cases y <;> cases z <;> rfl

theorem bxor3c : ∀ x y z : Bool, Bool.xor (Bool.xor x y) (Bool.xor z y) = Bool.xor x z := by
  intro x y z; cases x <;> cases y <;> cases z <;> rfl

theorem bxor3d : ∀ x y z : Bool, Bool.xor (Bool.xor x y) (Bool.xor y z) = Bool.xor x z := by
  intro x y z; cases x <;> cases y <;> cases z <;> rfl

theorem xor3b (a b c : B5) : (a.xor (b.xor c)).xor c = a.xor b := by
  cases a with
  | mk s1 a1 b1 c1 d1 =>
    cases b with
    | mk s2 a2 b2 c2 d2 =>
      cases c with
      | mk s3 a3 b3 c3 d3 =>
        show B5.mk (Bool.xor (Bool.xor s1 (Bool.xor s2 s3)) s3) (Bool.xor (Bool.xor a1 (Bool.xor a2 a3)) a3) (Bool.xor (Bool.xor b1 (Bool.xor b2 b3)) b3) (Bool.xor (Bool.xor c1 (Bool.xor c2 c3)) c3) (Bool.xor (Bool.xor d1 (Bool.xor d2 d3)) d3)
          = B5.mk (Bool.xor s1 s2) (Bool.xor a1 a2) (Bool.xor b1 b2) (Bool.xor c1 c2) (Bool.xor d1 d2)
        rw [bxor3b s1 s2 s3, bxor3b a1 a2 a3, bxor3b b1 b2 b3, bxor3b c1 c2 c3, bxor3b d1 d2 d3]

theorem xor3c (a b c : B5) : (a.xor b).xor (c.xor b) = a.xor c := by
  cases a with
  | mk s1 a1 b1 c1 d1 =>
    cases b with
    | mk s2 a2 b2 c2 d2 =>
      cases c with
      | mk s3 a3 b3 c3 d3 =>
        show B5.mk (Bool.xor (Bool.xor s1 s2) (Bool.xor s3 s2)) (Bool.xor (Bool.xor a1 a2) (Bool.xor a3 a2)) (Bool.xor (Bool.xor b1 b2) (Bool.xor b3 b2)) (Bool.xor (Bool.xor c1 c2) (Bool.xor c3 c2)) (Bool.xor (Bool.xor d1 d2) (Bool.xor d3 d2))
          = B5.mk (Bool.xor s1 s3) (Bool.xor a1 a3) (Bool.xor b1 b3) (Bool.xor c1 c3) (Bool.xor d1 d3)
        rw [bxor3c s1 s2 s3, bxor3c a1 a2 a3, bxor3c b1 b2 b3, bxor3c c1 c2 c3, bxor3c d1 d2 d3]

theorem xor3d (a b c : B5) : (a.xor b).xor (b.xor c) = a.xor c := by
  cases a with
  | mk s1 a1 b1 c1 d1 =>
    cases b with
    | mk s2 a2 b2 c2 d2 =>
      cases c with
      | mk s3 a3 b3 c3 d3 =>
        show B5.mk (Bool.xor (Bool.xor s1 s2) (Bool.xor s2 s3)) (Bool.xor (Bool.xor a1 a2) (Bool.xor a2 a3)) (Bool.xor (Bool.xor b1 b2) (Bool.xor b2 b3)) (Bool.xor (Bool.xor c1 c2) (Bool.xor c2 c3)) (Bool.xor (Bool.xor d1 d2) (Bool.xor d2 d3))
          = B5.mk (Bool.xor s1 s3) (Bool.xor a1 a3) (Bool.xor b1 b3) (Bool.xor c1 c3) (Bool.xor d1 d3)
        rw [bxor3d s1 s2 s3, bxor3d a1 a2 a3, bxor3d b1 b2 b3, bxor3d c1 c2 c3, bxor3d d1 d2 d3]

/-- the class of 2 -/
def K2 : B5 := ⟨false, true, false, false, false⟩

theorem cqK2 : cq K2 = TrianglePoint.two ratR ratR.one := rfl

theorem c2_ne : castQ 2 ≠ zeroQ := normQ_ne_zero (Nat.succ_pos 0) two_ne

/-- **The translate by T1 is away from E[2] with class (KN ⊕ γ₁, K2 ⊕ γ₂).** -/
theorem cls_T1 (x2 y2 : Q) (o : OnE x2 y2) (nd : ND x2) (γ1 γ2 : B5) (hc : HC x2 γ1 γ2) :
    ND (TorTrans.xT ratR nQ zeroQ (negQ nQ) (invQ (sub ratR x2 nQ))) ∧
      HC (TorTrans.xT ratR nQ zeroQ (negQ nQ) (invQ (sub ratR x2 nQ))) (KN.xor γ1) (K2.xor γ2) := by
  obtain ⟨T, eT⟩ := cls3 x2 y2 o γ1 γ2 hc nd.1 nd.2.1
  have hT : T ≠ zeroQ := T_ne _ _ T nd.2.2 eT
  obtain ⟨s, r, _, hr, _, exn⟩ := hc
  have h0x : sub ratR x2 zeroQ ≠ zeroQ := fun h => nd.1 ((sub0 x2).symm.trans h)
  have hv := mul_invQ _ nd.2.1
  have hv0 : invQ (sub ratR x2 nQ) ≠ zeroQ := Kernel3.ne_of_inv_r ratR Kernel2.two_one_ne _ _ hv
  have tn0 : sub ratR nQ zeroQ = cq KN := tor_n0
  have hnm : sub ratR nQ (negQ nQ) = ratR.mul (cq K2N) (ratR.mul (castQ 2) (castQ 2)) :=
    tor_nm.trans ((castQ_mul _ _).trans (congrArg (ratR.mul (cq K2N)) (castQ_mul 2 2)))
  have hA1 : TorTrans.A ratR nQ zeroQ (negQ nQ) = ratR.mul (cq KN) (ratR.mul (cq K2N) (ratR.mul (castQ 2) (castQ 2))) := by
    unfold TorTrans.A; rw [tn0, hnm]
  have A := g1_xn ratR nQ zeroQ (negQ nQ) x2 _ r (castQ 2) (cq KN) (cq K2N) (cq γ2) hA1 exn hv
  have B := g1_x0 ratR nQ zeroQ (negQ nQ) x2 _ r T (cq KN) (cq (γ1.xor γ2)) (cq γ2) tn0 eT exn hv
  have C := TorTrans.tor_d3 ratR nQ zeroQ (negQ nQ) x2 _ hv
  generalize invQ (sub ratR x2 nQ) = v at hv hv0 A B C ⊢
  generalize TorTrans.xT ratR nQ zeroQ (negQ nQ) v = X at A B C ⊢
  have A' := A.trans (prod3 KN K2N γ2 _)
  rw [show KN.xor K2N = K2 from rfl] at A'
  have B' := B.trans (prod3 KN (γ1.xor γ2) γ2 _)
  rw [xor3b] at B'
  have k : sub ratR nQ (negQ nQ) ≠ zeroQ := fun h =>
    mul_ne _ _ (cq_ne _) (mul_ne _ _ c2_ne c2_ne) (hnm.symm.trans h)
  have nx0 := ne_of_eqQ B' (mul_ne _ _ (cq_ne _)
      (mul_ne _ _ (mul_ne _ _ (mul_ne _ _ (cq_ne _) (cq_ne _)) (mul_ne _ _ (mul_ne _ _ hT hr) hv0))
        (mul_ne _ _ (mul_ne _ _ (cq_ne _) (cq_ne _)) (mul_ne _ _ (mul_ne _ _ hT hr) hv0))))
  have nxn := ne_of_eqQ A' (mul_ne _ _ (cq_ne _)
      (mul_ne _ _ (mul_ne _ _ (mul_ne _ _ (cq_ne _) (cq_ne _)) (mul_ne _ _ (mul_ne _ _ c2_ne hr) hv0))
        (mul_ne _ _ (mul_ne _ _ (cq_ne _) (cq_ne _)) (mul_ne _ _ (mul_ne _ _ c2_ne hr) hv0))))
  have nxm := ne_of_eqQ C (mul_ne _ _ (mul_ne _ _ (mul_ne _ _ k h0x) nd.2.1) (mul_ne _ _ hv0 hv0))
  refine ⟨⟨ne_of_eqQ (sub0 X).symm nx0, nxn, nxm⟩, ?_⟩
  unfold HC HasCls
  refine ⟨_, _, ?_, ?_, (sub0 X).symm.trans B', A'⟩
  · exact mul_ne _ _ (mul_ne _ _ (cq_ne _) (cq_ne _)) (mul_ne _ _ (mul_ne _ _ hT hr) hv0)
  · exact mul_ne _ _ (mul_ne _ _ (cq_ne _) (cq_ne _)) (mul_ne _ _ (mul_ne _ _ c2_ne hr) hv0)

/-- **The translate by T2 is away from E[2] with class (KT0 ⊕ γ₁, Km2N ⊕ γ₂).** -/
theorem cls_T2 (x2 y2 : Q) (o : OnE x2 y2) (nd : ND x2) (γ1 γ2 : B5) (hc : HC x2 γ1 γ2) :
    ND (TorTrans.xT ratR (negQ nQ) zeroQ nQ (invQ (sub ratR x2 (negQ nQ)))) ∧
      HC (TorTrans.xT ratR (negQ nQ) zeroQ nQ (invQ (sub ratR x2 (negQ nQ)))) (KT0.xor γ1) (Km2N.xor γ2) := by
  obtain ⟨T, eT⟩ := cls3 x2 y2 o γ1 γ2 hc nd.1 nd.2.1
  have hT : T ≠ zeroQ := T_ne _ _ T nd.2.2 eT
  obtain ⟨s, r, hs, hr, ex, exn⟩ := hc
  have hv := mul_invQ _ nd.2.2
  have hv0 : invQ (sub ratR x2 (negQ nQ)) ≠ zeroQ := Kernel3.ne_of_inv_r ratR Kernel2.two_one_ne _ _ hv
  have hmn : sub ratR (negQ nQ) nQ = ratR.mul (cq Km2N) (ratR.mul (castQ 2) (castQ 2)) :=
    tor_mn.trans ((castQ_mul _ _).trans (congrArg (ratR.mul (cq Km2N)) (castQ_mul 2 2)))
  have tm0 : sub ratR (negQ nQ) zeroQ = cq KT0 := tor_m0
  have ex0 : sub ratR x2 zeroQ = ratR.mul (cq γ1) (ratR.mul s s) := (sub0 x2).trans ex
  have A := g2_x0 ratR (negQ nQ) zeroQ nQ x2 _ r T (cq KT0) (cq γ2) (cq (γ1.xor γ2)) tm0 exn eT hv
  have B := g2_xn ratR (negQ nQ) zeroQ nQ x2 _ s T (cq Km2N) (castQ 2) (cq γ1) (cq (γ1.xor γ2)) hmn ex0 eT hv
  have C := TorTrans.tor_d1 ratR (negQ nQ) zeroQ nQ x2 _ hv
  generalize invQ (sub ratR x2 (negQ nQ)) = v at hv hv0 A B C ⊢
  generalize TorTrans.xT ratR (negQ nQ) zeroQ nQ v = X at A B C ⊢
  have A' := A.trans (prod3 KT0 γ2 (γ1.xor γ2) _)
  rw [xor3c] at A'
  have B' := B.trans (prod3 Km2N γ1 (γ1.xor γ2) _)
  rw [xor3d] at B'
  have k1 : sub ratR (negQ nQ) zeroQ ≠ zeroQ := fun h => cq_ne KT0 (tor_m0.symm.trans h)
  have k2 : sub ratR (negQ nQ) nQ ≠ zeroQ := fun h =>
    mul_ne _ _ (cq_ne _) (mul_ne _ _ c2_ne c2_ne) (hmn.symm.trans h)
  have hA : TorTrans.A ratR (negQ nQ) zeroQ nQ ≠ zeroQ := mul_ne _ _ k1 k2
  have nx0 := ne_of_eqQ A' (mul_ne _ _ (cq_ne _)
      (mul_ne _ _ (mul_ne _ _ (mul_ne _ _ (cq_ne _) (cq_ne _)) (mul_ne _ _ (mul_ne _ _ hr hT) hv0))
        (mul_ne _ _ (mul_ne _ _ (cq_ne _) (cq_ne _)) (mul_ne _ _ (mul_ne _ _ hr hT) hv0))))
  have nxn := ne_of_eqQ B' (mul_ne _ _ (cq_ne _)
      (mul_ne _ _ (mul_ne _ _ (mul_ne _ _ (cq_ne _) (cq_ne _)) (mul_ne _ _ (mul_ne _ _ (mul_ne _ _ c2_ne hs) hT) hv0))
        (mul_ne _ _ (mul_ne _ _ (cq_ne _) (cq_ne _)) (mul_ne _ _ (mul_ne _ _ (mul_ne _ _ c2_ne hs) hT) hv0))))
  have nxm := ne_of_eqQ C (mul_ne _ _ (mul_ne _ _ hA nd.2.2) (mul_ne _ _ hv0 hv0))
  refine ⟨⟨ne_of_eqQ (sub0 X).symm nx0, nxn, nxm⟩, ?_⟩
  unfold HC HasCls
  refine ⟨_, _, ?_, ?_, (sub0 X).symm.trans A', B'⟩
  · exact mul_ne _ _ (mul_ne _ _ (cq_ne _) (cq_ne _)) (mul_ne _ _ (mul_ne _ _ hr hT) hv0)
  · exact mul_ne _ _ (mul_ne _ _ (cq_ne _) (cq_ne _)) (mul_ne _ _ (mul_ne _ _ (mul_ne _ _ c2_ne hs) hT) hv0)

end TorCls12

#print axioms TorCls12.g1_xn
#print axioms TorCls12.g1_x0
#print axioms TorCls12.g2_x0
#print axioms TorCls12.g2_xn
#print axioms TorCls12.two_n_ne
#print axioms TorCls12.two_n_ne'
#print axioms TorCls12.bxor3b
#print axioms TorCls12.bxor3c
#print axioms TorCls12.bxor3d
#print axioms TorCls12.xor3b
#print axioms TorCls12.xor3c
#print axioms TorCls12.xor3d
#print axioms TorCls12.cqK2
#print axioms TorCls12.cls_T1
#print axioms TorCls12.c2_ne
#print axioms TorCls12.cls_T2

bsd/TorClsN0.lean

lean_all/bsd/TorClsN0.lean

/-
  bsd/TorClsN0.lean — T39 (goal G289f-4f, first piece): translation by T0 = (0, 0) on E_n, n a rational parameter,
  classes over a certified prime list (bsd/TorCls0 fixes n = 42486).  For P = (x, y) away from E[2] with class (γ₁, γ₂)
  the translate P + T0 (abscissa TorTrans.xT with e₁ = 0, v = x⁻¹) is away from E[2] with class
  (δ₁(T0) ⊕ γ₁, δ₂(T0) ⊕ γ₂), where δ(T0) is the class of T0 in PtClsN.TorClsN.
    castQ_sq         castQ (c·m²) = castQ c · (castQ m)²
    prod3L           three classes times a square = one class times a square
    xor3B, xor3L     (a ⊕ (b ⊕ c)) ⊕ b = a ⊕ c
    cls_T0N          the statement above
  Lean 4 core plus bsd/PtClsN, bsd/TorCls0.
-/
import bsd.PtClsN
import bsd.TorCls0

namespace TorClsN0
open DetChain LibRing LibRat LibInt QModel QCls B5S SqUniqueL SqClassL QModelN TransStepL StepN PtClsN

theorem castQ_sq (c m : Int) : castQ (c * (m * m)) = ratR.mul (castQ c) (ratR.mul (castQ m) (castQ m)) :=
  (castQ_mul c (m * m)).trans (congrArg (ratR.mul (castQ c)) (castQ_mul m m))

theorem castQ_ne (m : Int) (hm : m ≠ 0) : castQ m ≠ zeroQ := normQ_ne_zero (Nat.succ_pos 0) hm

theorem prod3L (ps : List Nat) (k1 k2 k3 : Bool) (b1 b2 b3 : List Bool) (l1 : b1.length = ps.length)
    (l2 : b2.length = ps.length) (l3 : b3.length = ps.length) (w : Q) :
    ∃ W : Q, ratR.mul (ratR.mul (ratR.mul (cqL ps k1 b1) (cqL ps k2 b2)) (cqL ps k3 b3)) (ratR.mul w w)
      = ratR.mul (cqL ps (xor (xor k1 k2) k3) (xorL (xorL b1 b2) b3)) (ratR.mul W W) := by
  refine ⟨ratR.mul (ratR.mul (cqL ps (xor k1 k2 && k3) (andL (xorL b1 b2) b3)) (cqL ps (k1 && k2) (andL b1 b2))) w, ?_⟩
  calc ratR.mul (ratR.mul (ratR.mul (cqL ps k1 b1) (cqL ps k2 b2)) (cqL ps k3 b3)) (ratR.mul w w)
      = ratR.mul (ratR.mul (cqL ps (xor k1 k2) (xorL b1 b2)) (cqL ps k3 b3))
          (ratR.mul (ratR.mul (cqL ps (k1 && k2) (andL b1 b2)) (cqL ps (k1 && k2) (andL b1 b2))) (ratR.mul w w)) := by
        rw [cqL_mul ps k1 k2 b1 b2 l1 l2]; lib_ring
    _ = _ := by rw [cqL_mul ps (xor k1 k2) k3 (xorL b1 b2) b3 (xorL_lenL ps b1 b2 l1 l2) l3]; lib_ring

theorem xor3B : ∀ x y z : Bool, xor (xor x (xor y z)) y = xor x z
  | true, true, true => rfl
  | true, true, false => rfl
  | true, false, true => rfl
  | true, false, false => rfl
  | false, true, true => rfl
  | false, true, false => rfl
  | false, false, true => rfl
  | false, false, false => rfl

theorem xor3L : ∀ a b c : List Bool, a.length = b.length → b.length = c.length →
    xorL (xorL a (xorL b c)) b = xorL a c
  | [], _, _, _, _ => rfl
  | _ :: _, [], _, h, _ => Nat.noConfusion h
  | _ :: _, _ :: _, [], _, h => Nat.noConfusion h
  | x :: a, y :: b, z :: c, h1, h2 => by
    show xor (xor x (xor y z)) y :: xorL (xorL a (xorL b c)) b = xor x z :: xorL a c
    rw [xor3B x y z, xor3L a b c (Nat.succ.inj h1) (Nat.succ.inj h2)]

theorem ne_of_eqQ {a b : Q} (h : a = b) (hb : b ≠ zeroQ) : a ≠ zeroQ := fun e => hb (h.symm.trans e)

/-- **The translate by T0 (general n).** -/
theorem cls_T0N (n : Q) (ps : List Nat) (hp : posL ps = true) (D : TorData n ps) (x2 y2 : Q) (o : OnEn n x2 y2)
    (nd : NDn n x2) (g1 g2 : CL) (w1 : WF ps g1) (w2 : WF ps g2) (hc : HCc n ps x2 g1 g2) :
    NDn n (TorTrans.xT ratR zeroQ n (negQ n) (invQ (sub ratR x2 zeroQ))) ∧
      HCc n ps (TorTrans.xT ratR zeroQ n (negQ n) (invQ (sub ratR x2 zeroQ)))
        (cxor ⟨xor D.k0m D.k0n, xorL D.b0m D.b0n⟩ g1) (cxor ⟨D.k0n, D.b0n⟩ g2) := by
  obtain ⟨T, eT⟩ := cls3n n x2 y2 ps o g1.s g2.s g1.b g2.b w1 w2 hc nd.1 nd.2.1
  have hT : T ≠ zeroQ := T_neQ _ _ T nd.2.2 eT
  obtain ⟨s, r, hs, _, ex, _⟩ := hc
  have h0 : sub ratR x2 zeroQ ≠ zeroQ := fun h => nd.1 ((TransStep.sub0 x2).symm.trans h)
  have hv := mul_invQ _ h0
  have hv0 : invQ (sub ratR x2 zeroQ) ≠ zeroQ := Kernel3.ne_of_inv_r ratR Kernel2.two_one_ne _ _ hv
  have hx2 : sub ratR x2 zeroQ = ratR.mul (cqL ps g1.s g1.b) (ratR.mul s s) := (TransStep.sub0 x2).trans ex
  have t0 : sub ratR zeroQ n = ratR.mul (cqL ps D.k0n D.b0n) (ratR.mul (castQ D.m0n) (castQ D.m0n)) :=
    D.e0n.trans (castQ_sq _ _)
  have tm : sub ratR zeroQ (negQ n) = ratR.mul (cqL ps D.k0m D.b0m) (ratR.mul (castQ D.m0m) (castQ D.m0m)) :=
    D.e0m.trans (castQ_sq _ _)
  have M0 := castQ_ne _ D.h0n
  have M1 := castQ_ne _ D.h0m
  have c0ne : sub ratR zeroQ n ≠ zeroQ := ne_of_eqQ t0 (TransStep.mul_ne _ _ (cqL_ne ps hp _ _) (TransStep.mul_ne _ _ M0 M0))
  have cmne : sub ratR zeroQ (negQ n) ≠ zeroQ :=
    ne_of_eqQ tm (TransStep.mul_ne _ _ (cqL_ne ps hp _ _) (TransStep.mul_ne _ _ M1 M1))
  have hA : TorTrans.A ratR zeroQ n (negQ n) = ratR.mul (sub ratR zeroQ n) (sub ratR zeroQ (negQ n)) := rfl
  have A := TorCls0.gen_x ratR zeroQ n (negQ n) x2 _ s (sub ratR zeroQ n) (sub ratR zeroQ (negQ n))
    (cqL ps g1.s g1.b) hA hx2 hv
  have B := TorCls0.gen_xn ratR zeroQ n (negQ n) x2 _ s T (cqL ps g1.s g1.b) (cqL ps (xor g1.s g2.s) (xorL g1.b g2.b))
    (sub ratR zeroQ n) rfl hx2 eT hv
  have C := TorTrans.tor_d3 ratR zeroQ n (negQ n) x2 _ hv
  generalize invQ (sub ratR x2 zeroQ) = v at hv hv0 A B C ⊢
  generalize TorTrans.xT ratR zeroQ n (negQ n) v = X at A B C ⊢
  have nx0 : sub ratR X zeroQ ≠ zeroQ := ne_of_eqQ A (TransStep.mul_ne _ _
    (TransStep.mul_ne _ _ (TransStep.mul_ne _ _ c0ne cmne) (cqL_ne ps hp _ _))
    (TransStep.mul_ne _ _ (TransStep.mul_ne _ _ hv0 hs) (TransStep.mul_ne _ _ hv0 hs)))
  have nxn : sub ratR X n ≠ zeroQ := ne_of_eqQ B (TransStep.mul_ne _ _
    (TransStep.mul_ne _ _ (TransStep.mul_ne _ _ c0ne (cqL_ne ps hp _ _)) (cqL_ne ps hp _ _))
    (TransStep.mul_ne _ _ (TransStep.mul_ne _ _ (TransStep.mul_ne _ _ hT hs) hv0)
      (TransStep.mul_ne _ _ (TransStep.mul_ne _ _ hT hs) hv0)))
  have nxm : sub ratR X (negQ n) ≠ zeroQ := ne_of_eqQ C (TransStep.mul_ne _ _
    (TransStep.mul_ne _ _ (TransStep.mul_ne _ _ cmne nd.2.1) h0) (TransStep.mul_ne _ _ hv0 hv0))
  -- X − 0 as a class times a square
  rw [tm, t0] at A
  rw [t0] at B
  have A1 : sub ratR X zeroQ = ratR.mul (ratR.mul (ratR.mul (cqL ps D.k0m D.b0m) (cqL ps D.k0n D.b0n))
      (cqL ps g1.s g1.b)) (ratR.mul (ratR.mul (ratR.mul (castQ D.m0n) (castQ D.m0m)) (ratR.mul v s))
        (ratR.mul (ratR.mul (castQ D.m0n) (castQ D.m0m)) (ratR.mul v s))) := by
    rw [A]; lib_ring
  have B1 : sub ratR X n = ratR.mul (ratR.mul (ratR.mul (cqL ps D.k0n D.b0n) (cqL ps (xor g1.s g2.s) (xorL g1.b g2.b)))
      (cqL ps g1.s g1.b)) (ratR.mul (ratR.mul (castQ D.m0n) (ratR.mul (ratR.mul T s) v))
        (ratR.mul (castQ D.m0n) (ratR.mul (ratR.mul T s) v))) := by
    rw [B]; lib_ring
  obtain ⟨WA, eA⟩ := prod3L ps D.k0m D.k0n g1.s D.b0m D.b0n g1.b D.l0m D.l0n w1 (ratR.mul (ratR.mul (castQ D.m0n) (castQ D.m0m)) (ratR.mul v s))
  obtain ⟨WB, eB⟩ := prod3L ps D.k0n (xor g1.s g2.s) g1.s D.b0n (xorL g1.b g2.b) g1.b D.l0n (xorL_lenL ps g1.b g2.b w1 w2) w1
    (ratR.mul (castQ D.m0n) (ratR.mul (ratR.mul T s) v))
  have A2 := A1.trans eA
  have B2 := B1.trans eB
  rw [xor3B, xor3L _ _ _ (D.l0n.trans (Eq.symm w1)) (Eq.trans w1 (Eq.symm w2))] at B2
  have A3 : sub ratR X zeroQ = ratR.mul (cqL ps (xor (xor D.k0m D.k0n) g1.s) (xorL (xorL D.b0m D.b0n) g1.b))
      (ratR.mul WA WA) := A2
  refine ⟨⟨fun h => nx0 ((TransStep.sub0 X).trans h), nxn, nxm⟩, ?_⟩
  exact ⟨WA, WB, T_neQ _ _ WA nx0 A3, T_neQ _ _ WB nxn B2, (TransStep.sub0 X).symm.trans A3, B2⟩

end TorClsN0

#print axioms TorClsN0.castQ_sq
#print axioms TorClsN0.castQ_ne
#print axioms TorClsN0.prod3L
#print axioms TorClsN0.xor3B
#print axioms TorClsN0.xor3L
#print axioms TorClsN0.ne_of_eqQ
#print axioms TorClsN0.cls_T0N

bsd/TorClsN12.lean

lean_all/bsd/TorClsN12.lean

/-
  bsd/TorClsN12.lean — T39 (goal G289f-4f): translation by T1 = (n, 0) and T2 = (−n, 0) on E_n, n a rational
  parameter, classes over a certified prime list (bsd/TorCls12 fixes n = 42486).  For P = (x, y) away from E[2] with
  class (γ₁, γ₂) the translates are away from E[2] with classes δ(T1) ⊕ γ and δ(T2) ⊕ γ (PtClsN.TorClsN).
      P + T1:  x' − n = (n − 0)(n + n)(x − n)v²,  x' − 0 = (n − 0)(x + n)(x − n)v²     (v = (x − n)⁻¹)
      P + T2:  x' − 0 = (−n − 0)(x − n)(x + n)v²,  x' − n = (−n − n)·x(x + n)v²        (v = (x + n)⁻¹)
    xor3bB, xor3cB, xor3dB and list forms     bit identities
    cls_T1N, cls_T2N                          the statements above
  Lean 4 core plus bsd/TorClsN0.
-/
import bsd.TorClsN0

namespace TorClsN12
open DetChain LibRing LibRat LibInt QModel QCls B5S SqUniqueL SqClassL QModelN TransStepL StepN PtClsN TorClsN0

theorem xor3bB : ∀ x y z : Bool, xor (xor x (xor y z)) z = xor x y
  | true, true, true => rfl
  | true, true, false => rfl
  | true, false, true => rfl
  | true, false, false => rfl
  | false, true, true => rfl
  | false, true, false => rfl
  | false, false, true => rfl
  | false, false, false => rfl

theorem xor3cB : ∀ x y z : Bool, xor (xor x y) (xor z y) = xor x z
  | true, true, true => rfl
  | true, true, false => rfl
  | true, false, true => rfl
  | true, false, false => rfl
  | false, true, true => rfl
  | false, true, false => rfl
  | false, false, true => rfl
  | false, false, false => rfl

theorem xor3dB : ∀ x y z : Bool, xor (xor x y) (xor y z) = xor x z
  | true, true, true => rfl
  | true, true, false => rfl
  | true, false, true => rfl
  | true, false, false => rfl
  | false, true, true => rfl
  | false, true, false => rfl
  | false, false, true => rfl
  | false, false, false => rfl

theorem xor3bL : ∀ a b c : List Bool, a.length = b.length → b.length = c.length →
    xorL (xorL a (xorL b c)) c = xorL a b
  | [], _, _, _, _ => rfl
  | _ :: _, [], _, h, _ => Nat.noConfusion h
  | _ :: _, _ :: _, [], _, h => Nat.noConfusion h
  | x :: a, y :: b, z :: c, h1, h2 => by
    show xor (xor x (xor y z)) z :: xorL (xorL a (xorL b c)) c = xor x y :: xorL a b
    rw [xor3bB x y z, xor3bL a b c (Nat.succ.inj h1) (Nat.succ.inj h2)]

theorem xor3cL : ∀ a b c : List Bool, a.length = b.length → b.length = c.length →
    xorL (xorL a b) (xorL c b) = xorL a c
  | [], _, _, _, _ => rfl
  | _ :: _, [], _, h, _ => Nat.noConfusion h
  | _ :: _, _ :: _, [], _, h => Nat.noConfusion h
  | x :: a, y :: b, z :: c, h1, h2 => by
    show xor (xor x y) (xor z y) :: xorL (xorL a b) (xorL c b) = xor x z :: xorL a c
    rw [xor3cB x y z, xor3cL a b c (Nat.succ.inj h1) (Nat.succ.inj h2)]

theorem xor3dL : ∀ a b c : List Bool, a.length = b.length → b.length = c.length →
    xorL (xorL a b) (xorL b c) = xorL a c
  | [], _, _, _, _ => rfl
  | _ :: _, [], _, h, _ => Nat.noConfusion h
  | _ :: _, _ :: _, [], _, h => Nat.noConfusion h
  | x :: a, y :: b, z :: c, h1, h2 => by
    show xor (xor x y) (xor y z) :: xorL (xorL a b) (xorL b c) = xor x z :: xorL a c
    rw [xor3dB x y z, xor3dL a b c (Nat.succ.inj h1) (Nat.succ.inj h2)]

/-- **The translate by T1 (general n).** -/
theorem cls_T1N (n : Q) (ps : List Nat) (hp : posL ps = true) (D : TorData n ps) (x2 y2 : Q) (o : OnEn n x2 y2)
    (nd : NDn n x2) (g1 g2 : CL) (w1 : WF ps g1) (w2 : WF ps g2) (hc : HCc n ps x2 g1 g2) :
    NDn n (TorTrans.xT ratR n zeroQ (negQ n) (invQ (sub ratR x2 n))) ∧
      HCc n ps (TorTrans.xT ratR n zeroQ (negQ n) (invQ (sub ratR x2 n)))
        (cxor ⟨D.kn0, D.bn0⟩ g1) (cxor ⟨xor D.knm D.kn0, xorL D.bnm D.bn0⟩ g2) := by
  obtain ⟨T, eT⟩ := cls3n n x2 y2 ps o g1.s g2.s g1.b g2.b w1 w2 hc nd.1 nd.2.1
  obtain ⟨_, r, _, _, _, exn⟩ := hc
  have h0x : sub ratR x2 zeroQ ≠ zeroQ := fun h => nd.1 ((TransStep.sub0 x2).symm.trans h)
  have hv := mul_invQ _ nd.2.1
  have hv0 : invQ (sub ratR x2 n) ≠ zeroQ := Kernel3.ne_of_inv_r ratR Kernel2.two_one_ne _ _ hv
  have t1 : sub ratR n zeroQ = ratR.mul (cqL ps D.kn0 D.bn0) (ratR.mul (castQ D.mn0) (castQ D.mn0)) :=
    D.en0.trans (castQ_sq _ _)
  have t2 : sub ratR n (negQ n) = ratR.mul (cqL ps D.knm D.bnm) (ratR.mul (castQ D.mnm) (castQ D.mnm)) :=
    D.enm.trans (castQ_sq _ _)
  have M1 := castQ_ne _ D.hn0
  have M2 := castQ_ne _ D.hnm
  have c1ne : sub ratR n zeroQ ≠ zeroQ :=
    ne_of_eqQ t1 (TransStep.mul_ne _ _ (cqL_ne ps hp _ _) (TransStep.mul_ne _ _ M1 M1))
  have c2ne : sub ratR n (negQ n) ≠ zeroQ :=
    ne_of_eqQ t2 (TransStep.mul_ne _ _ (cqL_ne ps hp _ _) (TransStep.mul_ne _ _ M2 M2))
  have A := TorTrans.tor_d1 ratR n zeroQ (negQ n) x2 _ hv
  have B := TorTrans.tor_d2 ratR n zeroQ (negQ n) x2 _ hv
  have C := TorTrans.tor_d3 ratR n zeroQ (negQ n) x2 _ hv
  generalize invQ (sub ratR x2 n) = v at hv hv0 A B C ⊢
  generalize TorTrans.xT ratR n zeroQ (negQ n) v = X at A B C ⊢
  have hA : TorTrans.A ratR n zeroQ (negQ n) = ratR.mul (sub ratR n zeroQ) (sub ratR n (negQ n)) := rfl
  rw [hA] at A
  have nxn : sub ratR X n ≠ zeroQ := ne_of_eqQ A (TransStep.mul_ne _ _
    (TransStep.mul_ne _ _ (TransStep.mul_ne _ _ c1ne c2ne) nd.2.1) (TransStep.mul_ne _ _ hv0 hv0))
  have nx0 : sub ratR X zeroQ ≠ zeroQ := ne_of_eqQ B (TransStep.mul_ne _ _
    (TransStep.mul_ne _ _ (TransStep.mul_ne _ _ c1ne nd.2.2) nd.2.1) (TransStep.mul_ne _ _ hv0 hv0))
  have nxm : sub ratR X (negQ n) ≠ zeroQ := ne_of_eqQ C (TransStep.mul_ne _ _
    (TransStep.mul_ne _ _ (TransStep.mul_ne _ _ c2ne h0x) nd.2.1) (TransStep.mul_ne _ _ hv0 hv0))
  rw [t1, t2, exn] at A
  rw [t1, eT, exn] at B
  have A1 : sub ratR X n = ratR.mul (ratR.mul (ratR.mul (cqL ps D.knm D.bnm) (cqL ps D.kn0 D.bn0))
      (cqL ps g2.s g2.b)) (ratR.mul (ratR.mul (ratR.mul (castQ D.mn0) (castQ D.mnm)) (ratR.mul r v))
        (ratR.mul (ratR.mul (castQ D.mn0) (castQ D.mnm)) (ratR.mul r v))) := by
    rw [A]; lib_ring
  have B1 : sub ratR X zeroQ = ratR.mul (ratR.mul (ratR.mul (cqL ps D.kn0 D.bn0)
      (cqL ps (xor g1.s g2.s) (xorL g1.b g2.b))) (cqL ps g2.s g2.b))
        (ratR.mul (ratR.mul (castQ D.mn0) (ratR.mul (ratR.mul T r) v))
          (ratR.mul (castQ D.mn0) (ratR.mul (ratR.mul T r) v))) := by
    rw [B]; lib_ring
  obtain ⟨WA, eA⟩ := prod3L ps D.knm D.kn0 g2.s D.bnm D.bn0 g2.b D.lnm D.ln0 w2
    (ratR.mul (ratR.mul (castQ D.mn0) (castQ D.mnm)) (ratR.mul r v))
  obtain ⟨WB, eB⟩ := prod3L ps D.kn0 (xor g1.s g2.s) g2.s D.bn0 (xorL g1.b g2.b) g2.b D.ln0
    (xorL_lenL ps g1.b g2.b w1 w2) w2 (ratR.mul (castQ D.mn0) (ratR.mul (ratR.mul T r) v))
  have A2 := A1.trans eA
  have B2 := B1.trans eB
  rw [xor3bB, xor3bL _ _ _ (D.ln0.trans (Eq.symm w1)) (Eq.trans w1 (Eq.symm w2))] at B2
  have h0' : sub ratR X zeroQ = ratR.mul (cqL ps (xor D.kn0 g1.s) (xorL D.bn0 g1.b)) (ratR.mul WB WB) := B2
  refine ⟨⟨fun h => nx0 ((TransStep.sub0 X).trans h), nxn, nxm⟩, ?_⟩
  exact ⟨WB, WA, T_neQ _ _ WB nx0 h0', T_neQ _ _ WA nxn A2, (TransStep.sub0 X).symm.trans h0', A2⟩

/-- **The translate by T2 (general n).** -/
theorem cls_T2N (n : Q) (ps : List Nat) (hp : posL ps = true) (D : TorData n ps) (x2 y2 : Q) (o : OnEn n x2 y2)
    (nd : NDn n x2) (g1 g2 : CL) (w1 : WF ps g1) (w2 : WF ps g2) (hc : HCc n ps x2 g1 g2) :
    NDn n (TorTrans.xT ratR (negQ n) zeroQ n (invQ (sub ratR x2 (negQ n)))) ∧
      HCc n ps (TorTrans.xT ratR (negQ n) zeroQ n (invQ (sub ratR x2 (negQ n))))
        (cxor ⟨D.km0, D.bm0⟩ g1) (cxor ⟨D.kmn, D.bmn⟩ g2) := by
  obtain ⟨T, eT⟩ := cls3n n x2 y2 ps o g1.s g2.s g1.b g2.b w1 w2 hc nd.1 nd.2.1
  obtain ⟨s, r, _, _, ex, exn⟩ := hc
  have h0x : sub ratR x2 zeroQ ≠ zeroQ := fun h => nd.1 ((TransStep.sub0 x2).symm.trans h)
  have ex' : sub ratR x2 zeroQ = ratR.mul (cqL ps g1.s g1.b) (ratR.mul s s) := (TransStep.sub0 x2).trans ex
  have hv := mul_invQ _ nd.2.2
  have hv0 : invQ (sub ratR x2 (negQ n)) ≠ zeroQ := Kernel3.ne_of_inv_r ratR Kernel2.two_one_ne _ _ hv
  have t1 : sub ratR (negQ n) zeroQ = ratR.mul (cqL ps D.km0 D.bm0) (ratR.mul (castQ D.mm0) (castQ D.mm0)) :=
    D.em0.trans (castQ_sq _ _)
  have t2 : sub ratR (negQ n) n = ratR.mul (cqL ps D.kmn D.bmn) (ratR.mul (castQ D.mmn) (castQ D.mmn)) :=
    D.emn.trans (castQ_sq _ _)
  have M1 := castQ_ne _ D.hm0
  have M2 := castQ_ne _ D.hmn
  have c1ne : sub ratR (negQ n) zeroQ ≠ zeroQ :=
    ne_of_eqQ t1 (TransStep.mul_ne _ _ (cqL_ne ps hp _ _) (TransStep.mul_ne _ _ M1 M1))
  have c2ne : sub ratR (negQ n) n ≠ zeroQ :=
    ne_of_eqQ t2 (TransStep.mul_ne _ _ (cqL_ne ps hp _ _) (TransStep.mul_ne _ _ M2 M2))
  have A := TorTrans.tor_d1 ratR (negQ n) zeroQ n x2 _ hv
  have B := TorTrans.tor_d2 ratR (negQ n) zeroQ n x2 _ hv
  have C := TorTrans.tor_d3 ratR (negQ n) zeroQ n x2 _ hv
  generalize invQ (sub ratR x2 (negQ n)) = v at hv hv0 A B C ⊢
  generalize TorTrans.xT ratR (negQ n) zeroQ n v = X at A B C ⊢
  have hA : TorTrans.A ratR (negQ n) zeroQ n = ratR.mul (sub ratR (negQ n) zeroQ) (sub ratR (negQ n) n) := rfl
  rw [hA] at A
  have nxm : sub ratR X (negQ n) ≠ zeroQ := ne_of_eqQ A (TransStep.mul_ne _ _
    (TransStep.mul_ne _ _ (TransStep.mul_ne _ _ c1ne c2ne) nd.2.2) (TransStep.mul_ne _ _ hv0 hv0))
  have nx0 : sub ratR X zeroQ ≠ zeroQ := ne_of_eqQ B (TransStep.mul_ne _ _
    (TransStep.mul_ne _ _ (TransStep.mul_ne _ _ c1ne nd.2.1) nd.2.2) (TransStep.mul_ne _ _ hv0 hv0))
  have nxn : sub ratR X n ≠ zeroQ := ne_of_eqQ C (TransStep.mul_ne _ _
    (TransStep.mul_ne _ _ (TransStep.mul_ne _ _ c2ne h0x) nd.2.2) (TransStep.mul_ne _ _ hv0 hv0))
  rw [t1, exn, eT] at B
  rw [t2, ex', eT] at C
  have B1 : sub ratR X zeroQ = ratR.mul (ratR.mul (ratR.mul (cqL ps D.km0 D.bm0) (cqL ps g2.s g2.b))
      (cqL ps (xor g1.s g2.s) (xorL g1.b g2.b)))
        (ratR.mul (ratR.mul (castQ D.mm0) (ratR.mul (ratR.mul r T) v))
          (ratR.mul (castQ D.mm0) (ratR.mul (ratR.mul r T) v))) := by
    rw [B]; lib_ring
  have C1 : sub ratR X n = ratR.mul (ratR.mul (ratR.mul (cqL ps D.kmn D.bmn) (cqL ps g1.s g1.b))
      (cqL ps (xor g1.s g2.s) (xorL g1.b g2.b)))
        (ratR.mul (ratR.mul (castQ D.mmn) (ratR.mul (ratR.mul s T) v))
          (ratR.mul (castQ D.mmn) (ratR.mul (ratR.mul s T) v))) := by
    rw [C]; lib_ring
  obtain ⟨WB, eB⟩ := prod3L ps D.km0 g2.s (xor g1.s g2.s) D.bm0 g2.b (xorL g1.b g2.b) D.lm0 w2
    (xorL_lenL ps g1.b g2.b w1 w2) (ratR.mul (castQ D.mm0) (ratR.mul (ratR.mul r T) v))
  obtain ⟨WC, eC⟩ := prod3L ps D.kmn g1.s (xor g1.s g2.s) D.bmn g1.b (xorL g1.b g2.b) D.lmn w1
    (xorL_lenL ps g1.b g2.b w1 w2) (ratR.mul (castQ D.mmn) (ratR.mul (ratR.mul s T) v))
  have B2 := B1.trans eB
  have C2 := C1.trans eC
  rw [xor3cB, xor3cL _ _ _ (D.lm0.trans (Eq.symm w2)) (Eq.trans w2 (Eq.symm w1))] at B2
  rw [xor3dB, xor3dL _ _ _ (D.lmn.trans (Eq.symm w1)) (Eq.trans w1 (Eq.symm w2))] at C2
  have h0' : sub ratR X zeroQ = ratR.mul (cqL ps (xor D.km0 g1.s) (xorL D.bm0 g1.b)) (ratR.mul WB WB) := B2
  refine ⟨⟨fun h => nx0 ((TransStep.sub0 X).trans h), nxn, nxm⟩, ?_⟩
  exact ⟨WB, WC, T_neQ _ _ WB nx0 h0', T_neQ _ _ WC nxn C2, (TransStep.sub0 X).symm.trans h0', C2⟩

end TorClsN12

#print axioms TorClsN12.xor3bB
#print axioms TorClsN12.xor3cB
#print axioms TorClsN12.xor3dB
#print axioms TorClsN12.xor3bL
#print axioms TorClsN12.xor3cL
#print axioms TorClsN12.xor3dL
#print axioms TorClsN12.cls_T1N
#print axioms TorClsN12.cls_T2N

bsd/TorGen.lean

lean_all/bsd/TorGen.lean

/-
  bsd/TorGen.lean — T39 (goal G289f-4g): building the torsion data StepN.TorData and the non-square hypotheses of
  bsd/DblClsN for an integer n from integer identities checked by rfl; the instance n = 42486, ps = [2, 3, 73, 97].
    cvalI          (a/1) − (b/1) = castQ c   from   (a·1 + (−b)·1)·1 = c·(1·1)
    nonsq_of       x = castQ (clsL κ·m²) with κ nontrivial  ⇒  x is not a rational square
    td42486        the torsion data of E_42486
    h0n_42486, hn0_42486, hm0_42486    −n, n, −n are not rational squares
  Lean 4 core plus bsd/DblClsN, bsd/TransStep2.
-/
import bsd.DblClsN
import bsd.TransStep2

namespace TorGen
open DetChain LibRing LibRat LibInt QModel QCls B5S SqUniqueL SqClassL TransStepL StepN DblClsN

theorem cvalI (a b c : Int) (h : (a * Int.ofNat 1 + -b * Int.ofNat 1) * Int.ofNat 1 = c * Int.ofNat (1 * 1)) :
    sub ratR (normQ a 1 one_pos') (normQ b 1 one_pos') = castQ c :=
  (TransStep2.const_sub a b).trans ((normQ_eq_iff _ _ _ _ _ _).2 h)

/-- a class times a square that is not the trivial class is not a rational square -/
theorem nonsq_of (ps : List Nat) (hc : chkL ps = true) (hp : posL ps = true) (k : Bool) (b : List Bool)
    (hl : b.length = ps.length) (m : Int) (hm : m ≠ 0) (hk : k = false → b = zL ps → False) (x : Q)
    (hx : x = castQ (clsL k ps b * (m * m))) : ∀ T : Q, x ≠ ratR.mul T T := fun T h => by
  obtain ⟨d1, d2⟩ := bits_of_eqL ps hc hp k false b (zL ps) hl (zL_len ps) m hm T
    (hx.symm.trans (h.trans (one_mulQ ps _).symm))
  exact hk d1.symm d2.symm

def P4 : List Nat := [2, 3, 73, 97]
def tttt : List Bool := [true, true, true, true]
def fttt : List Bool := [false, true, true, true]

theorem two_ne' : (2 : Int) ≠ 0 := TransStep2.two_ne

theorem e0n : sub ratR zeroQ nQ = castQ (clsL true P4 tttt * (1 * 1)) := cvalI 0 42486 _ rfl
theorem e0m : sub ratR zeroQ (negQ nQ) = castQ (clsL false P4 tttt * (1 * 1)) := by
  rw [negQ_nQ]; exact cvalI 0 (-42486) _ rfl
theorem en0 : sub ratR nQ zeroQ = castQ (clsL false P4 tttt * (1 * 1)) := cvalI 42486 0 _ rfl
theorem enm : sub ratR nQ (negQ nQ) = castQ (clsL false P4 fttt * (2 * 2)) := by
  rw [negQ_nQ]; exact cvalI 42486 (-42486) _ rfl
theorem em0 : sub ratR (negQ nQ) zeroQ = castQ (clsL true P4 tttt * (1 * 1)) := by
  rw [negQ_nQ]; exact cvalI (-42486) 0 _ rfl
theorem emn : sub ratR (negQ nQ) nQ = castQ (clsL true P4 fttt * (2 * 2)) := by
  rw [negQ_nQ]; exact cvalI (-42486) 42486 _ rfl

/-- **The torsion data of E_42486.** -/
def td42486 : TorData nQ P4 where
  k0n := true
  b0n := tttt
  m0n := 1
  k0m := false
  b0m := tttt
  m0m := 1
  kn0 := false
  bn0 := tttt
  mn0 := 1
  knm := false
  bnm := fttt
  mnm := 2
  km0 := true
  bm0 := tttt
  mm0 := 1
  kmn := true
  bmn := fttt
  mmn := 2
  l0n := rfl
  l0m := rfl
  ln0 := rfl
  lnm := rfl
  lm0 := rfl
  lmn := rfl
  h0n := TransStep2.one_ne
  h0m := TransStep2.one_ne
  hn0 := TransStep2.one_ne
  hnm := two_ne'
  hm0 := TransStep2.one_ne
  hmn := two_ne'
  e0n := e0n
  e0m := e0m
  en0 := en0
  enm := enm
  em0 := em0
  emn := emn

theorem chk_P4 : chkL P4 = true := SqUniqueL.chk_42486
theorem pos_P4 : posL P4 = true := rfl

theorem tttt_ne (b : List Bool) (h : tttt = b) (h' : b = zL P4) : False :=
  Bool.noConfusion (congrArg (fun l => l.headD false) (h.trans h'))

theorem h0n_42486 : ∀ T : Q, sub ratR zeroQ nQ ≠ ratR.mul T T :=
  nonsq_of P4 chk_P4 pos_P4 true tttt rfl 1 TransStep2.one_ne (fun h _ => Bool.noConfusion h) _ e0n

theorem hn0_42486 : ∀ T : Q, sub ratR nQ zeroQ ≠ ratR.mul T T :=
  nonsq_of P4 chk_P4 pos_P4 false tttt rfl 1 TransStep2.one_ne (fun _ h => tttt_ne tttt rfl h) _ en0

theorem hm0_42486 : ∀ T : Q, sub ratR (negQ nQ) zeroQ ≠ ratR.mul T T :=
  nonsq_of P4 chk_P4 pos_P4 true tttt rfl 1 TransStep2.one_ne (fun h _ => Bool.noConfusion h) _ em0

end TorGen

#print axioms TorGen.cvalI
#print axioms TorGen.nonsq_of
#print axioms TorGen.two_ne'
#print axioms TorGen.e0n
#print axioms TorGen.e0m
#print axioms TorGen.en0
#print axioms TorGen.enm
#print axioms TorGen.em0
#print axioms TorGen.emn
#print axioms TorGen.td42486
#print axioms TorGen.chk_P4
#print axioms TorGen.pos_P4
#print axioms TorGen.tttt_ne
#print axioms TorGen.h0n_42486
#print axioms TorGen.hn0_42486
#print axioms TorGen.hm0_42486

bsd/TorGen2.lean

lean_all/bsd/TorGen2.lean

/-
  bsd/TorGen2.lean — T39 (goal G289f-4f, instance): the relations PtClsN2.TorRel for the torsion data of E_42486
  (by rfl), and the general additivity PtClsN2.cls_addN specialised to n = 42486, ps = [2, 3, 73, 97].
    tr42486          δ(Tᵢ) ⊕ δ(Tⱼ) = δ(Tₖ) for E_42486
    cls_add_42486    the 2-descent class is additive on E_42486(ℚ) (general machinery at n = 42486)
  Lean 4 core plus bsd/PtClsN2, bsd/TorGen.
-/
import bsd.PtClsN2
import bsd.TorGen

namespace TorGen2
open DetChain LibRing LibRat QModel QModelN PtAdd PtAddN PtClsN PtClsN2 TorGen

theorem tr42486 : TorRel nQ P4 td42486 where
  r01a := rfl
  r01b := rfl
  r02a := rfl
  r02b := rfl
  r12a := rfl
  r12b := rfl

/-- **Additivity of the class on E_42486, from the general-n theorem.** -/
theorem cls_add_42486 : ∀ (P1 P2 : Pt) (β1 β2 γ1 γ2 : CL), OnPtN nQ P1 → OnPtN nQ P2 →
    ClsPtN nQ P4 td42486 P1 β1 β2 → ClsPtN nQ P4 td42486 P2 γ1 γ2 →
    ClsPtN nQ P4 td42486 (addN nQ P1 P2) (cxor β1 γ1) (cxor β2 γ2) :=
  cls_addN nQ P4 chk_P4 pos_P4 td42486 tr42486 PtCls.n_ne h0n_42486 hn0_42486 hm0_42486

end TorGen2

#print axioms TorGen2.tr42486
#print axioms TorGen2.cls_add_42486

bsd/TorTrans.lean

lean_all/bsd/TorTrans.lean

/-
  bsd/TorTrans.lean — T39 (goal G289e-7b): translation by a point T₁ = (e₁, 0) of E[2] on y² = (x − e₁)(x − e₂)(x − e₃),
  over any commutative ring, with v an inverse of x − e₁ and A = (e₁ − e₂)(e₁ − e₃):
      x(P + T₁) = e₁ + A·v,   y(P + T₁) = −A·y·v².
    tor_on    P + T₁ is on the curve
    tor_d1    x(P + T₁) − e₁ = A·(x − e₁)·v²
    tor_d2    x(P + T₁) − e₂ = (e₁ − e₂)(x − e₃)(x − e₁)·v²
    tor_d3    x(P + T₁) − e₃ = (e₁ − e₃)(x − e₂)(x − e₁)·v²
  so the 2-descent class of P + T₁ is the class of P times the class of T₁ (with (x − e₂)(x − e₃) ≡ x − e₁ mod squares).
  Lean 4 core plus bsd/DescentHomFull, lib/RingTac.
-/
import bsd.DescentHomFull

namespace TorTrans
open DetChain LibRing

variable {R : Type} (K : CRing R)

def A (e1 e2 e3 : R) : R := K.mul (sub K e1 e2) (sub K e1 e3)
def xT (e1 e2 e3 v : R) : R := K.add e1 (K.mul (A K e1 e2 e3) v)
def yT (e1 e2 e3 y v : R) : R := K.neg (K.mul (K.mul (A K e1 e2 e3) y) (K.mul v v))

theorem tor_on (e1 e2 e3 x y v : R) (o : K.mul y y = DescentHomFull.f K e1 e2 e3 x)
    (hv : K.mul (sub K x e1) v = K.one) :
    K.mul (yT K e1 e2 e3 y v) (yT K e1 e2 e3 y v) = DescentHomFull.f K e1 e2 e3 (xT K e1 e2 e3 v) := by
  have l : K.mul (yT K e1 e2 e3 y v) (yT K e1 e2 e3 y v)
      = K.mul (K.mul (K.mul (A K e1 e2 e3) (A K e1 e2 e3)) (K.mul (K.mul v v) (K.mul v v))) (K.mul y y) := by
    unfold yT; lib_ring
  have r : DescentHomFull.f K e1 e2 e3 (xT K e1 e2 e3 v)
      = K.mul (K.mul (K.mul (A K e1 e2 e3) v) K.one)
          (K.mul (K.add (K.mul (sub K e1 e2) K.one) (K.mul (A K e1 e2 e3) v))
            (K.add (K.mul (sub K e1 e3) K.one) (K.mul (A K e1 e2 e3) v))) := by
    unfold DescentHomFull.f xT LibRing.sub; lib_ring
  rw [l, r, o, ← hv]
  unfold DescentHomFull.f A LibRing.sub; lib_ring

theorem tor_d1 (e1 e2 e3 x v : R) (hv : K.mul (sub K x e1) v = K.one) :
    sub K (xT K e1 e2 e3 v) e1 = K.mul (K.mul (A K e1 e2 e3) (sub K x e1)) (K.mul v v) := by
  have l : sub K (xT K e1 e2 e3 v) e1 = K.mul (K.mul (A K e1 e2 e3) v) K.one := by
    unfold xT LibRing.sub; lib_ring
  rw [l, ← hv]; lib_ring

theorem tor_d2 (e1 e2 e3 x v : R) (hv : K.mul (sub K x e1) v = K.one) :
    sub K (xT K e1 e2 e3 v) e2
      = K.mul (K.mul (K.mul (sub K e1 e2) (sub K x e3)) (sub K x e1)) (K.mul v v) := by
  have l : sub K (xT K e1 e2 e3 v) e2
      = K.mul (K.add (K.mul (sub K e1 e2) K.one) (K.mul (A K e1 e2 e3) v)) K.one := by
    unfold xT LibRing.sub; lib_ring
  rw [l, ← hv]
  unfold A LibRing.sub; lib_ring

theorem tor_d3 (e1 e2 e3 x v : R) (hv : K.mul (sub K x e1) v = K.one) :
    sub K (xT K e1 e2 e3 v) e3
      = K.mul (K.mul (K.mul (sub K e1 e3) (sub K x e2)) (sub K x e1)) (K.mul v v) := by
  have l : sub K (xT K e1 e2 e3 v) e3
      = K.mul (K.add (K.mul (sub K e1 e3) K.one) (K.mul (A K e1 e2 e3) v)) K.one := by
    unfold xT LibRing.sub; lib_ring
  rw [l, ← hv]
  unfold A LibRing.sub; lib_ring

end TorTrans

#print axioms TorTrans.tor_on
#print axioms TorTrans.tor_d1
#print axioms TorTrans.tor_d2
#print axioms TorTrans.tor_d3

bsd/TorsionImage.lean

lean_all/bsd/TorsionImage.lean

/-
  bsd/TorsionImage.lean — T39 cycle 245 (goal G7): the 2-torsion of E_n : y² = x³ − n²x maps injectively under the
  2-descent map, for every n ≥ 1.

  Lean 4 core plus lib/Semi (lib_nat). With e = (0, n, −n), the descent map in the coordinates (x − e₁, x − e₂)
  sends T₀ = (0, 0) ↦ (−1, −n), T₁ = (n, 0) ↦ (n, 2), T₂ = (−n, 0) ↦ (−n, −2n) modulo squares (TorsionShift,
  DescentHomFull). Each image is non-trivial because one coordinate is not a rational square:
    neg_not_sq     a² + n·b² = 0, n, b > 0 is impossible        (−1, −n: negative)
    no_sqrt2       a² = 2b², b > 0 is impossible                 (2: by descent, hand parity)
  so the image of E_n[2] ≅ (Z/2)² has order 4 — the "+2" in dim Sel₂ = r + 2 used with Monsky's matrix
  (MonskyCheck: s₂ = dim Sel₂ − 2).
-/
import lib.Semi

namespace TorsionImage

theorem parity : ∀ n : Nat, ∃ k, n = 2 * k ∨ n = 2 * k + 1
  | 0 => ⟨0, Or.inl rfl⟩
  | n + 1 => by
    have ⟨k, hk⟩ := parity n
    cases hk with
    | inl h => exact ⟨k, Or.inr (by rw [h])⟩
    | inr h => exact ⟨k + 1, Or.inl (by rw [h]; lib_nat)⟩

theorem odd_ne_even : ∀ x y : Nat, 2 * x + 1 ≠ 2 * y
  | 0, 0, h => Nat.noConfusion h
  | 0, y + 1, h => by
    have h' : 0 + 1 = (2 * y + 1) + 1 := by rw [show (2 * y + 1) + 1 = 2 * (y + 1) by lib_nat]; exact h
    exact Nat.noConfusion (Nat.succ.inj h')
  | x + 1, 0, h => Nat.noConfusion h
  | x + 1, y + 1, h => by
    have h' : (2 * x + 1) + 1 + 1 = (2 * y) + 1 + 1 := by
      rw [show (2 * x + 1) + 1 + 1 = 2 * (x + 1) + 1 by lib_nat, show 2 * y + 1 + 1 = 2 * (y + 1) by lib_nat]; exact h
    exact odd_ne_even x y (Nat.succ.inj (Nat.succ.inj h'))

theorem half_cancel : ∀ x y : Nat, 2 * x = 2 * y → x = y
  | 0, 0, _ => rfl
  | 0, y + 1, h => by
    have h' : 0 = 2 * y + 1 + 1 := by rw [show 2 * y + 1 + 1 = 2 * (y + 1) by lib_nat]; exact h
    exact Nat.noConfusion h'
  | x + 1, 0, h => by
    have h' : 2 * x + 1 + 1 = 0 := by rw [show 2 * x + 1 + 1 = 2 * (x + 1) by lib_nat]; exact h
    exact Nat.noConfusion h'
  | x + 1, y + 1, h => by
    have h' : 2 * x + 1 + 1 = 2 * y + 1 + 1 := by
      rw [show 2 * x + 1 + 1 = 2 * (x + 1) by lib_nat, show 2 * y + 1 + 1 = 2 * (y + 1) by lib_nat]; exact h
    rw [half_cancel x y (Nat.succ.inj (Nat.succ.inj h'))]

theorem sq_even (a X : Nat) (h : a * a = 2 * X) : ∃ c, a = 2 * c := by
  have ⟨k, hk⟩ := parity a
  cases hk with
  | inl e => exact ⟨k, e⟩
  | inr e =>
    rw [e, show (2 * k + 1) * (2 * k + 1) = 2 * (2 * k * k + 2 * k) + 1 by lib_nat] at h
    exact absurd h (odd_ne_even _ _)

/-- **√2 is irrational.** -/
theorem no_sqrt2 : ∀ F b, b ≤ F → 0 < b → ∀ a, a * a ≠ 2 * (b * b)
  | 0, b, hb, hpos, _, _ => absurd (Nat.lt_of_lt_of_le hpos hb) (Nat.lt_irrefl 0)
  | F + 1, b, hb, hpos, a, h => by
    have ⟨c, hc⟩ := sq_even a (b * b) h
    rw [hc, show 2 * c * (2 * c) = 2 * (2 * (c * c)) by lib_nat] at h
    have h1 := half_cancel _ _ h
    have ⟨d, hd⟩ := sq_even b (c * c) h1.symm
    rw [hd, show 2 * d * (2 * d) = 2 * (2 * (d * d)) by lib_nat] at h1
    have h2 := half_cancel _ _ h1
    -- c² = 2d², and 0 < d < b
    have dpos : 0 < d := by
      cases d with
      | zero => rw [hd] at hpos; exact absurd hpos (Nat.lt_irrefl 0)
      | succ d => exact Nat.succ_pos d
    have dlt : d < b := by
      rw [hd, Nat.two_mul]; exact Nat.lt_add_of_pos_right dpos
    exact no_sqrt2 F d (Nat.le_of_lt_succ (Nat.lt_of_lt_of_le dlt hb)) dpos c h2

theorem neg_not_sq (a n b : Nat) (hn : 0 < n) (hb : 0 < b) : a * a + n * (b * b) ≠ 0 := by
  cases n with
  | zero => exact absurd hn (Nat.lt_irrefl 0)
  | succ n =>
    cases b with
    | zero => exact absurd hb (Nat.lt_irrefl 0)
    | succ b =>
      rw [show a * a + (n + 1) * ((b + 1) * (b + 1)) = (a * a + n * ((b + 1) * (b + 1)) + b * b + 2 * b) + 1 by lib_nat]
      exact fun h => Nat.noConfusion h

/-- **The three non-trivial torsion images are non-trivial**, for every n ≥ 1: −1 (T₀), 2 (T₁) and −n (T₂) are not
    rational squares (a rational square q = a/b, b > 0, means a² = q·b²). -/
theorem torsion_image (n : Nat) (hn : 0 < n) :
    (∀ a b, 0 < b → a * a + 1 * (b * b) ≠ 0) ∧ (∀ a b, 0 < b → a * a ≠ 2 * (b * b)) ∧
      (∀ a b, 0 < b → a * a + n * (b * b) ≠ 0) :=
  ⟨fun a b hb => neg_not_sq a 1 b (Nat.lt_succ_self 0) hb,
   fun a b hb => no_sqrt2 b b (Nat.le_refl b) hb a,
   fun a b hb => neg_not_sq a n b hn hb⟩

end TorsionImage

#print axioms TorsionImage.parity
#print axioms TorsionImage.odd_ne_even
#print axioms TorsionImage.half_cancel
#print axioms TorsionImage.sq_even
#print axioms TorsionImage.no_sqrt2
#print axioms TorsionImage.neg_not_sq
#print axioms TorsionImage.torsion_image

bsd/TorsionShift.lean

lean_all/bsd/TorsionShift.lean

/-
  bsd/TorsionShift.lean — T39 cycle 184: translation by a 2-torsion point and the descent map, every
  commutative ring (the torsion half of the homomorphism property).

  Lean 4 core plus bsd/DescentHomFull (f), lib/RingTac. On y² = f(x) = (x − e₁)(x − e₂)(x − e₃), T₁ = (e₁, 0),
  D = (e₁ − e₂)(e₁ − e₃), w(x − e₁) = 1 and v·y = 1 (P not 2-torsion), the point P + T₁ is
      x′ = e₁ + D w,   y′ = −y D w².
    shift_on   y′² = f(x′)
    shift_c1   x′ − e₁ = D (x − e₁) · w²                    (δ₁(P + T₁) = δ₁(T₁) δ₁(P) mod squares, δ₁(T₁) := D)
    shift_c2   x′ − e₂ = (e₁ − e₂)(x − e₂) · ((x − e₃) v)²    (δ₂(P + T₁) = δ₂(T₁) δ₂(P) mod squares)
  (e₃ by symmetry.) With DescentHomFull (collinear generic points) this is the homomorphism property of
  E(K) → (K*/K*²)³ with the standard convention at the 2-torsion points.
-/
import bsd.DescentHomFull

namespace TorsionShift
open DetChain LibRing DescentHomFull

variable {R : Type} (K : CRing R)

def D (e1 e2 e3 : R) : R := K.mul (sub K e1 e2) (sub K e1 e3)
def xs (e1 e2 e3 w : R) : R := K.add e1 (K.mul (D K e1 e2 e3) w)
def ys (e1 e2 e3 y w : R) : R := K.neg (K.mul (K.mul y (D K e1 e2 e3)) (K.mul w w))

theorem shift_on (e1 e2 e3 x y w : R) (hy : K.mul y y = f K e1 e2 e3 x) (hw : K.mul w (sub K x e1) = K.one) :
    K.mul (ys K e1 e2 e3 y w) (ys K e1 e2 e3 y w) = f K e1 e2 e3 (xs K e1 e2 e3 w) := by
  -- both sides equal D² w³ (x − e₂)(x − e₃) · [w (x − e₁)]-normalised; replace 1 by w(x − e₁)
  have e1' : f K e1 e2 e3 (xs K e1 e2 e3 w)
      = K.mul (K.mul (K.mul (D K e1 e2 e3) w) (K.add (sub K e1 e2) (K.mul (D K e1 e2 e3) w)))
          (K.add (sub K e1 e3) (K.mul (D K e1 e2 e3) w)) := by unfold f xs; lib_ring
  have e2' : K.add (sub K e1 e2) (K.mul (D K e1 e2 e3) w) = K.mul (K.mul (sub K e1 e2) (sub K x e3)) w := by
    rw [show K.add (sub K e1 e2) (K.mul (D K e1 e2 e3) w)
        = K.add (K.mul (sub K e1 e2) (K.mul w (sub K x e1))) (K.mul (D K e1 e2 e3) w) by rw [hw]; lib_ring]
    unfold D; lib_ring
  have e3' : K.add (sub K e1 e3) (K.mul (D K e1 e2 e3) w) = K.mul (K.mul (sub K e1 e3) (sub K x e2)) w := by
    rw [show K.add (sub K e1 e3) (K.mul (D K e1 e2 e3) w)
        = K.add (K.mul (sub K e1 e3) (K.mul w (sub K x e1))) (K.mul (D K e1 e2 e3) w) by rw [hw]; lib_ring]
    unfold D; lib_ring
  rw [e1', e2', e3']
  have l : K.mul (ys K e1 e2 e3 y w) (ys K e1 e2 e3 y w)
      = K.mul (K.mul (K.mul (D K e1 e2 e3) (D K e1 e2 e3)) (K.mul (K.mul w w) (K.mul w w))) (K.mul y y) := by
    unfold ys; lib_ring
  rw [l, hy]
  rw [show K.mul (K.mul (K.mul (D K e1 e2 e3) (D K e1 e2 e3)) (K.mul (K.mul w w) (K.mul w w))) (f K e1 e2 e3 x)
      = K.mul (K.mul (K.mul (D K e1 e2 e3) (D K e1 e2 e3)) (K.mul (K.mul w w) w))
          (K.mul (K.mul (sub K x e2) (sub K x e3)) (K.mul w (sub K x e1))) by unfold f; lib_ring, hw]
  unfold D; lib_ring

theorem shift_c1 (e1 e2 e3 x w : R) (hw : K.mul w (sub K x e1) = K.one) :
    sub K (xs K e1 e2 e3 w) e1 = K.mul (K.mul (D K e1 e2 e3) (sub K x e1)) (K.mul w w) := by
  rw [show K.mul (K.mul (D K e1 e2 e3) (sub K x e1)) (K.mul w w)
      = K.mul (K.mul (D K e1 e2 e3) w) (K.mul w (sub K x e1)) by lib_ring, hw]
  unfold xs; lib_ring

theorem shift_c2 (e1 e2 e3 x y w v : R) (hy : K.mul y y = f K e1 e2 e3 x) (hw : K.mul w (sub K x e1) = K.one)
    (hv : K.mul v y = K.one) :
    sub K (xs K e1 e2 e3 w) e2 = K.mul (K.mul (sub K e1 e2) (sub K x e2)) (K.mul (K.mul (sub K x e3) v) (K.mul (sub K x e3) v)) := by
  -- (x − e₂)(x − e₃) v² = w
  have s1 : K.mul (K.mul (K.mul (sub K x e2) (sub K x e3)) (K.mul v v)) (sub K x e1) = K.one := by
    rw [show K.mul (K.mul (K.mul (sub K x e2) (sub K x e3)) (K.mul v v)) (sub K x e1)
        = K.mul (K.mul v v) (f K e1 e2 e3 x) by unfold f; lib_ring, ← hy,
      show K.mul (K.mul v v) (K.mul y y) = K.mul (K.mul v y) (K.mul v y) by lib_ring, hv]
    lib_ring
  have s2 : K.mul (K.mul (sub K x e2) (sub K x e3)) (K.mul v v) = w := by
    rw [show K.mul (K.mul (sub K x e2) (sub K x e3)) (K.mul v v)
        = K.mul (K.mul (K.mul (K.mul (sub K x e2) (sub K x e3)) (K.mul v v)) (sub K x e1)) w by
          rw [show K.mul (K.mul (K.mul (K.mul (sub K x e2) (sub K x e3)) (K.mul v v)) (sub K x e1)) w
              = K.mul (K.mul (K.mul (sub K x e2) (sub K x e3)) (K.mul v v)) (K.mul w (sub K x e1)) by lib_ring, hw]
          lib_ring, s1]
    lib_ring
  rw [show K.mul (K.mul (sub K e1 e2) (sub K x e2)) (K.mul (K.mul (sub K x e3) v) (K.mul (sub K x e3) v))
      = K.mul (K.mul (sub K e1 e2) (sub K x e3)) (K.mul (K.mul (sub K x e2) (sub K x e3)) (K.mul v v)) by lib_ring, s2]
  rw [show sub K (xs K e1 e2 e3 w) e2
      = K.add (K.mul (sub K e1 e2) (K.mul w (sub K x e1))) (K.mul (D K e1 e2 e3) w) by rw [hw]; unfold xs; lib_ring]
  unfold D; lib_ring

end TorsionShift

#print axioms TorsionShift.shift_on
#print axioms TorsionShift.shift_c1
#print axioms TorsionShift.shift_c2

bsd/TorsionTwo.lean

lean_all/bsd/TorsionTwo.lean

/-
  bsd/TorsionTwo.lean — T39 (goal G289g): E_42486(ℚ) has no point of order 4.
  If 2P = T for a point T of E[2], then T has the class of a double, (1, 1) (bsd/PtCls2.cls_add with β ⊕ β = 0),
  while the three points of E[2] have the nonzero classes of Rank3Final.cls_torP; the class of an abscissa is unique
  (bsd/PtCls.same_x).  So 2P ∈ E[2] forces 2P = O, i.e. the 2-power torsion of E(ℚ) is E[2].
    no_four_torsion    add P P = torP s t  ⇒  s = false ∧ t = false
  Lean 4 core plus bsd/Rank3Final.
-/
import bsd.Rank3Final

namespace TorsionTwo
open DetChain LibRing LibRat QModel QCls B5S TransStep TransStep2 StepFull PtAdd QDec DblCls PtCls PtCls2 Rank3Final

/-- **No rational point of order 4.** -/
theorem no_four_torsion (P : Pt) (o : OnPt P) (s t : Bool) (h : add P P = torP s t) : s = false ∧ t = false := by
  obtain ⟨β, β', c, _⟩ := cls_exists P o
  have cR := cls_add P P β β' β β' o o c c
  rw [xor_self, xor_self, h] at cR
  have cT := cls_torP s t
  cases s with
  | false =>
    cases t with
    | false => exact ⟨rfl, rfl⟩
    | true => exact Bool.noConfusion (congrArg B5.a (same_x nQ _ _ _ _ cR cT).1)
  | true =>
    cases t with
    | false => exact Bool.noConfusion (congrArg B5.s (same_x zeroQ _ _ _ _ cR cT).1)
    | true => exact Bool.noConfusion (congrArg B5.s (same_x (negQ nQ) _ _ _ _ cR cT).1)

end TorsionTwo

#print axioms TorsionTwo.no_four_torsion

bsd/TransCls.lean

lean_all/bsd/TransCls.lean

/-
  bsd/TransCls.lean — T39 (goal G289e-6b): square classes along a chord, over a field given as an integral domain
  with an inverse on nonzero elements.
    third_cls     a = c_a·s_a², b = c_b·s_b², a·b ≠ 0, a·b·c = w²  ⇒  c = (c_a·c_b)·t²,  t = w·(c_a c_b s_a s_b)⁻¹
    t_ne_zero     … and t ≠ 0 when c ≠ 0
  With ChordClass.chord_sq ((x₁ − e)(x₂ − e)(x₃ − e) = (λe + ν)²) this moves the descent class of P₁ by that of P₂.
  Lean 4 core plus lib/RingTac.
-/
import lib.RingTac

namespace TransCls
open DetChain LibRing

variable {R : Type} (K : CRing R)

theorem ne_of_mul_ne (a b : R) (h : K.mul a b ≠ K.zero) : a ≠ K.zero := fun h0 => h (by rw [h0]; lib_ring)

/-- **The third class.** -/
theorem third_cls (inv : R → R) (hinv : ∀ a, a ≠ K.zero → K.mul a (inv a) = K.one)
    (a b c ca cb sa sb w : R) (ha : a = K.mul ca (K.mul sa sa)) (hb : b = K.mul cb (K.mul sb sb))
    (hab : K.mul a b ≠ K.zero) (hc : K.mul (K.mul a b) c = K.mul w w) :
    c = K.mul (K.mul ca cb)
      (K.mul (K.mul w (inv (K.mul (K.mul ca cb) (K.mul sa sb)))) (K.mul w (inv (K.mul (K.mul ca cb) (K.mul sa sb))))) := by
  have hD : K.mul (K.mul ca cb) (K.mul sa sb) ≠ K.zero := by
    have e : K.mul a b = K.mul (K.mul (K.mul ca cb) (K.mul sa sb)) (K.mul sa sb) := by rw [ha, hb]; lib_ring
    rw [e] at hab
    exact ne_of_mul_ne K _ _ hab
  have hDi := hinv _ hD
  have e1 : K.mul (K.mul ca cb)
      (K.mul (K.mul w (inv (K.mul (K.mul ca cb) (K.mul sa sb)))) (K.mul w (inv (K.mul (K.mul ca cb) (K.mul sa sb)))))
      = K.mul (K.mul (K.mul ca cb) (K.mul (inv (K.mul (K.mul ca cb) (K.mul sa sb))) (inv (K.mul (K.mul ca cb) (K.mul sa sb)))))
          (K.mul w w) := by lib_ring
  rw [e1, ← hc, ha, hb]
  have e2 : K.mul (K.mul (K.mul ca cb) (K.mul (inv (K.mul (K.mul ca cb) (K.mul sa sb))) (inv (K.mul (K.mul ca cb) (K.mul sa sb)))))
        (K.mul (K.mul (K.mul ca (K.mul sa sa)) (K.mul cb (K.mul sb sb))) c)
      = K.mul (K.mul (K.mul (K.mul (K.mul ca cb) (K.mul sa sb)) (inv (K.mul (K.mul ca cb) (K.mul sa sb))))
          (K.mul (K.mul (K.mul ca cb) (K.mul sa sb)) (inv (K.mul (K.mul ca cb) (K.mul sa sb))))) c := by lib_ring
  rw [e2, hDi]; lib_ring

theorem t_ne_zero (c ca cb t : R) (hc : c ≠ K.zero) (e : c = K.mul (K.mul ca cb) (K.mul t t)) : t ≠ K.zero :=
  fun h0 => hc (by rw [e, h0]; lib_ring)

end TransCls

#print axioms TransCls.ne_of_mul_ne
#print axioms TransCls.third_cls
#print axioms TransCls.t_ne_zero

bsd/TransPts.lean

lean_all/bsd/TransPts.lean

/-
  bsd/TransPts.lean — T39 (goal G289e-6f): the five translation points of E_42486, one per first-coordinate bit of a
  descent class: Q0 ~ (−1, −42486) (T0 + 2G1), Q1 = G1 ~ (2, 3), Q2 = G2 ~ (3, 438), Q3 ~ (73, 97) (G2 + G3 + T0),
  Q4 ~ (97, 7081) (G1 + G3 + T0 + T1)  (exact coordinates from tools/g289e6_pts.py, a computing tool only).
  Each point is a reduced fraction with a Bezout certificate; the curve equation and the class (x = c₁·s², x − n = c₂·r²)
  are integer identities checked by rfl.  Also: the points are nondegenerate (x, x − n, x + n ≠ 0).
  Lean 4 core plus bsd/PtCert.
-/
import bsd.PtCert
import bsd.TransStep2

namespace TransPts
open LibRat QModel QCls B5S TransStep PtCert

/-- zero test by pattern matching (Int's `==` brings propext, Classical.choice, Quot.sound) -/
def isZ : Int → Bool
  | Int.ofNat 0 => true
  | Int.ofNat (_ + 1) => false
  | Int.negSucc _ => false

theorem nz : ∀ (m : Int), isZ m = false → m ≠ 0
  | Int.ofNat 0, h, _ => Bool.noConfusion h
  | Int.ofNat (_ + 1), _, h0 => Nat.noConfusion (Int.ofNat.inj h0)
  | Int.negSucc _, _, h0 => Int.noConfusion h0

theorem nd_of (x : Q) (h0 : isZ x.num = false)
    (h1 : isZ (x.num * Int.ofNat 1 + -42486 * Int.ofNat x.den) = false)
    (h2 : isZ (x.num * Int.ofNat 1 + - -42486 * Int.ofNat x.den) = false) : ND x := by
  refine ⟨TransStep2.ne_of_num (nz _ h0), ?_, ?_⟩
  · rw [e_n x]; exact normQ_ne_zero _ (nz _ h1)
  · rw [e_mn x]; exact normQ_ne_zero _ (nz _ h2)

-- Q0: x = -25226610689748562704/3081769222279225, class (-1, -42486)
def Q0x : Q := PtCert.mkQ (-25226610689748562704) 3081769222279225 (Nat.succ_pos 3081769222279224) (-1435139023492231) (11747697773595383545) rfl
def Q0y : Q := PtCert.mkQ (-645299232094282440608603314344) 171080365848303878694125 (Nat.succ_pos 171080365848303878694124) (51954696241947787453504) (-195968283223968771814891198747) rfl
def Q0s : Q := PtCert.mkQ (5022609948) 55513685 (Nat.succ_pos 55513684) (-22183233) (2007031721) rfl
def Q0r : Q := PtCert.mkQ (60626167) 55513685 (Nat.succ_pos 55513684) (6072453) (-6631690) rfl
def Q0b1 : B5 := ⟨true, false, false, false, false⟩
def Q0b2 : B5 := ⟨true, true, true, true, true⟩
theorem Q0_on : OnE Q0x Q0y := onE_of_int _ _ rfl
theorem Q0_nd : ND Q0x := nd_of _ rfl rfl rfl
theorem Q0_hc : HC Q0x Q0b1 Q0b2 :=
  hc_of_int Q0x Q0s Q0r Q0b1 Q0b2 (fun h => Nat.noConfusion (Int.ofNat.inj h)) (fun h => Nat.noConfusion (Int.ofNat.inj h)) rfl rfl

-- Q1: x = 46818/1, class (2, 3)
def Q1x : Q := PtCert.mkQ (46818) 1 (Nat.succ_pos 0) (0) (1) rfl
def Q1y : Q := PtCert.mkQ (4255848) 1 (Nat.succ_pos 0) (0) (1) rfl
def Q1s : Q := PtCert.mkQ (153) 1 (Nat.succ_pos 0) (0) (1) rfl
def Q1r : Q := PtCert.mkQ (38) 1 (Nat.succ_pos 0) (0) (1) rfl
def Q1b1 : B5 := ⟨false, true, false, false, false⟩
def Q1b2 : B5 := ⟨false, false, true, false, false⟩
theorem Q1_on : OnE Q1x Q1y := onE_of_int _ _ rfl
theorem Q1_nd : ND Q1x := nd_of _ rfl rfl rfl
theorem Q1_hc : HC Q1x Q1b1 Q1b2 :=
  hc_of_int Q1x Q1s Q1r Q1b1 Q1b2 (fun h => Nat.noConfusion (Int.ofNat.inj h)) (fun h => Nat.noConfusion (Int.ofNat.inj h)) rfl rfl

-- Q2: x = 63948/1, class (3, 438)
def Q2x : Q := PtCert.mkQ (63948) 1 (Nat.succ_pos 0) (0) (1) rfl
def Q2y : Q := PtCert.mkQ (12086172) 1 (Nat.succ_pos 0) (0) (1) rfl
def Q2s : Q := PtCert.mkQ (146) 1 (Nat.succ_pos 0) (0) (1) rfl
def Q2r : Q := PtCert.mkQ (7) 1 (Nat.succ_pos 0) (0) (1) rfl
def Q2b1 : B5 := ⟨false, false, true, false, false⟩
def Q2b2 : B5 := ⟨false, true, true, true, false⟩
theorem Q2_on : OnE Q2x Q2y := onE_of_int _ _ rfl
theorem Q2_nd : ND Q2x := nd_of _ rfl rfl rfl
theorem Q2_hc : HC Q2x Q2b1 Q2b2 :=
  hc_of_int Q2x Q2s Q2r Q2b1 Q2b2 (fun h => Nat.noConfusion (Int.ofNat.inj h)) (fun h => Nat.noConfusion (Int.ofNat.inj h)) rfl rfl

-- Q3: x = 69021573073/1597696, class (73, 97)
def Q3x : Q := PtCert.mkQ (69021573073) 1597696 (Nat.succ_pos 1597695) (700465) (-30260572839) rfl
def Q3y : Q := PtCert.mkQ (3284753428036583) 2019487744 (Nat.succ_pos 2019487743) (6330327) (-10296454323935) rfl
def Q3s : Q := PtCert.mkQ (30749) 1264 (Nat.succ_pos 1263) (101) (-2457) rfl
def Q3r : Q := PtCert.mkQ (3431) 1264 (Nat.succ_pos 1263) (7) (-19) rfl
def Q3b1 : B5 := ⟨false, false, false, true, false⟩
def Q3b2 : B5 := ⟨false, false, false, false, true⟩
theorem Q3_on : OnE Q3x Q3y := onE_of_int _ _ rfl
theorem Q3_nd : ND Q3x := nd_of _ rfl rfl rfl
theorem Q3_hc : HC Q3x Q3b1 Q3b2 :=
  hc_of_int Q3x Q3s Q3r Q3b1 Q3b2 (fun h => Nat.noConfusion (Int.ofNat.inj h)) (fun h => Nat.noConfusion (Int.ofNat.inj h)) rfl rfl

-- Q4: x = 204723290784313/93470224, class (97, 7081)
def Q4x : Q := PtCert.mkQ (204723290784313) 93470224 (Nat.succ_pos 93470223) (21854585) (-47867035815871) rfl
def Q4y : Q := PtCert.mkQ (2928661369751278591507) 903670125632 (Nat.succ_pos 903670125631) (-196060776165) (635404009705767798308) rfl
def Q4s : Q := PtCert.mkQ (1452773) 9668 (Nat.succ_pos 9667) (3641) (-547119) rfl
def Q4r : Q := PtCert.mkQ (168377) 9668 (Nat.succ_pos 9667) (-2527) (44010) rfl
def Q4b1 : B5 := ⟨false, false, false, false, true⟩
def Q4b2 : B5 := ⟨false, false, false, true, true⟩
theorem Q4_on : OnE Q4x Q4y := onE_of_int _ _ rfl
theorem Q4_nd : ND Q4x := nd_of _ rfl rfl rfl
theorem Q4_hc : HC Q4x Q4b1 Q4b2 :=
  hc_of_int Q4x Q4s Q4r Q4b1 Q4b2 (fun h => Nat.noConfusion (Int.ofNat.inj h)) (fun h => Nat.noConfusion (Int.ofNat.inj h)) rfl rfl

end TransPts

#print axioms TransPts.isZ
#print axioms TransPts.nz
#print axioms TransPts.nd_of
#print axioms TransPts.Q0_on
#print axioms TransPts.Q0_nd
#print axioms TransPts.Q0_hc
#print axioms TransPts.Q1_on
#print axioms TransPts.Q1_nd
#print axioms TransPts.Q1_hc
#print axioms TransPts.Q2_on
#print axioms TransPts.Q2_nd
#print axioms TransPts.Q2_hc
#print axioms TransPts.Q3_on
#print axioms TransPts.Q3_nd
#print axioms TransPts.Q3_hc
#print axioms TransPts.Q4_on
#print axioms TransPts.Q4_nd
#print axioms TransPts.Q4_hc

bsd/TransQ.lean

lean_all/bsd/TransQ.lean

/-
  bsd/TransQ.lean — T39 (goal G289e-6b): the chord step over Q on E_n : y² = x(x − n)(x + n), n = 42486.
  For two rational points P₁, P₂ with x₁ ≠ x₂, the slope λ = (y₂ − y₁)(x₂ − x₁)⁻¹ and ν = y₁ − λx₁ give the third point
  P₃ = (x₃, λx₃ + ν) on the curve, and for each root e ∈ {0, n, −n}
      (x₁ − e)(x₂ − e)(x₃ − e) = (λe + ν)².
    slope_line     λ(x₂ − x₁) = y₂ − y₁  ⇒  y₂ = λx₂ + ν
    chord_Q        the statement above (ChordClass.third_on, chord_sq with lib/RatField)
  Lean 4 core plus bsd/ChordClass, bsd/QModel, lib/RatField.
-/
import bsd.ChordClass
import bsd.QModel
import lib.RatField

namespace TransQ
open DetChain LibRing LibRat QModel

/-- slope through two points -/
def slope (x1 y1 x2 y2 : Q) : Q := ratR.mul (sub ratR y2 y1) (invQ (sub ratR x2 x1))

def icpt (x1 y1 x2 y2 : Q) : Q := sub ratR y1 (ratR.mul (slope x1 y1 x2 y2) x1)

theorem line1 (x1 y1 x2 y2 : Q) : y1 = ratR.add (ratR.mul (slope x1 y1 x2 y2) x1) (icpt x1 y1 x2 y2) := by
  unfold icpt; lib_ring

theorem line2 (x1 y1 x2 y2 : Q) (h12 : x1 ≠ x2) :
    y2 = ratR.add (ratR.mul (slope x1 y1 x2 y2) x2) (icpt x1 y1 x2 y2) := by
  have hne : sub ratR x2 x1 ≠ ratR.zero := LibVieta.sub_ne_zero ratR x2 x1 (fun h => h12 h.symm)
  have hinv : ratR.mul (sub ratR x2 x1) (invQ (sub ratR x2 x1)) = ratR.one := mul_invQ _ hne
  unfold icpt slope
  have e : ratR.add (ratR.mul (ratR.mul (sub ratR y2 y1) (invQ (sub ratR x2 x1))) x2)
        (sub ratR y1 (ratR.mul (ratR.mul (sub ratR y2 y1) (invQ (sub ratR x2 x1))) x1))
      = ratR.add y1 (ratR.mul (sub ratR y2 y1) (ratR.mul (sub ratR x2 x1) (invQ (sub ratR x2 x1)))) := by lib_ring
  rw [e, hinv]; lib_ring

/-- the third x-coordinate -/
def x3 (x1 y1 x2 y2 : Q) : Q :=
  ChordClass.third ratR (DescentHomFull.c2 ratR zeroQ nQ (negQ nQ) (slope x1 y1 x2 y2)) x1 x2

def y3 (x1 y1 x2 y2 : Q) : Q :=
  ratR.add (ratR.mul (slope x1 y1 x2 y2) (x3 x1 y1 x2 y2)) (icpt x1 y1 x2 y2)

/-- **The third point is on the curve.** -/
theorem third_onE (x1 y1 x2 y2 : Q) (h12 : x1 ≠ x2) (o1 : OnE x1 y1) (o2 : OnE x2 y2) :
    OnE (x3 x1 y1 x2 y2) (y3 x1 y1 x2 y2) :=
  ChordClass.third_on ratR rat_noZD zeroQ nQ (negQ nQ) _ _ x1 x2 y1 y2 h12
    (line1 x1 y1 x2 y2) (line2 x1 y1 x2 y2 h12) o1 o2

/-- **Chord: product of the three descent coordinates is a square.** -/
theorem chord_Q (x1 y1 x2 y2 : Q) (h12 : x1 ≠ x2) (o1 : OnE x1 y1) (o2 : OnE x2 y2) (e : Q)
    (he : e = zeroQ ∨ e = nQ ∨ e = negQ nQ) :
    ratR.mul (ratR.mul (sub ratR x1 e) (sub ratR x2 e)) (sub ratR (x3 x1 y1 x2 y2) e)
      = ratR.mul (ratR.add (ratR.mul (slope x1 y1 x2 y2) e) (icpt x1 y1 x2 y2))
          (ratR.add (ratR.mul (slope x1 y1 x2 y2) e) (icpt x1 y1 x2 y2)) :=
  ChordClass.chord_sq ratR rat_noZD zeroQ nQ (negQ nQ) _ _ x1 x2 y1 y2 h12
    (line1 x1 y1 x2 y2) (line2 x1 y1 x2 y2 h12) o1 o2 e he

end TransQ

#print axioms TransQ.line1
#print axioms TransQ.line2
#print axioms TransQ.third_onE
#print axioms TransQ.chord_Q

bsd/TransStep.lean

lean_all/bsd/TransStep.lean

/-
  bsd/TransStep.lean — T39 (goal G289e-6b): translating a descent class along a chord, over Q.
  For rational points P₁ (class (β₁, β₂)) and P₂ (class (γ₁, γ₂)) of E_n (n = 42486) with x₁ ≠ x₂ and both away from
  E[2], the third chord point P₃ either is away from E[2] with class (β₁ ⊕ γ₁, β₂ ⊕ γ₂), or is a 2-torsion point and then
  (β₁ ⊕ γ₁, β₂ ⊕ γ₂) is the class of that torsion point, which lies in V.
    HC x β₁ β₂        x = cq β₁·s², x − n = cq β₂·r²   (QCls.HasCls)
    third_cq          a = cq β·s², b = cq γ·t², a·b ≠ 0, a·b·c = w²  ⇒  c = cq (β ⊕ γ)·T²
    cls3              the third coordinate: x + n = cq (β₁ ⊕ β₂)·T²
    bits_of_eq        castQ (cls κ·m²) = cq δ·T²  ⇒  δ = κ
    step              the dichotomy above
  Lean 4 core plus bsd/B5, bsd/TransQ, bsd/TransCls, bsd/VSet.
-/
import bsd.B5
import bsd.TransQ
import bsd.TransCls
import bsd.VSet

namespace TransStep
open DetChain LibRing LibRat LibInt QModel QCls B5S TransQ

def HC (x : Q) (β1 β2 : B5) : Prop := HasCls x β1.s β1.a β1.b β1.c β1.d β2.s β2.a β2.b β2.c β2.d

/-- nondegenerate: x, x − n, x + n all nonzero -/
def ND (x : Q) : Prop := x ≠ zeroQ ∧ sub ratR x nQ ≠ zeroQ ∧ sub ratR x (negQ nQ) ≠ zeroQ

theorem sub0 (x : Q) : sub ratR x zeroQ = x := by
  show sub ratR x ratR.zero = x; lib_ring

theorem mul_ne (a b : Q) (ha : a ≠ zeroQ) (hb : b ≠ zeroQ) : ratR.mul a b ≠ zeroQ := fun h => by
  cases rat_noZD a b h with
  | inl h1 => exact ha h1
  | inr h1 => exact hb h1

theorem third_cq (β γ : B5) (a b c sa sb w : Q) (ha : a = ratR.mul (cq β) (ratR.mul sa sa))
    (hb : b = ratR.mul (cq γ) (ratR.mul sb sb)) (hab : ratR.mul a b ≠ zeroQ)
    (hc : ratR.mul (ratR.mul a b) c = ratR.mul w w) : ∃ T : Q, c = ratR.mul (cq (β.xor γ)) (ratR.mul T T) := by
  have e := TransCls.third_cls ratR invQ (fun a h => mul_invQ a h) a b c (cq β) (cq γ) sa sb w ha hb hab hc
  refine ⟨ratR.mul (cq (β.and γ)) (ratR.mul w (invQ (ratR.mul (ratR.mul (cq β) (cq γ)) (ratR.mul sa sb)))), ?_⟩
  rw [e, cq_mul β γ]; lib_ring

theorem T_ne (δ : B5) (c T : Q) (hc : c ≠ zeroQ) (e : c = ratR.mul (cq δ) (ratR.mul T T)) : T ≠ zeroQ :=
  fun h0 => hc (by rw [e, h0]; show ratR.mul (cq δ) (ratR.mul ratR.zero ratR.zero) = ratR.zero; lib_ring)

/-- the third coordinate x + n -/
theorem cls3 (x y : Q) (o : OnE x y) (β1 β2 : B5) (h : HC x β1 β2) (hx : x ≠ zeroQ) (hxn : sub ratR x nQ ≠ zeroQ) :
    ∃ T : Q, sub ratR x (negQ nQ) = ratR.mul (cq (β1.xor β2)) (ratR.mul T T) := by
  obtain ⟨s, r, _, _, hs, hr⟩ := h
  have o' : ratR.mul (ratR.mul x (sub ratR x nQ)) (sub ratR x (negQ nQ)) = ratR.mul y y := by
    have o2 : ratR.mul y y = ratR.mul (ratR.mul (sub ratR x zeroQ) (sub ratR x nQ)) (sub ratR x (negQ nQ)) := o
    rw [o2, sub0]
  exact third_cq β1 β2 x (sub ratR x nQ) (sub ratR x (negQ nQ)) s r y hs hr (mul_ne _ _ hx hxn) o'

theorem bits_of_eq (κ δ : B5) (m : Int) (hm : m ≠ 0) (T : Q)
    (h : castQ (κ.cls * (m * m)) = ratR.mul (cq δ) (ratR.mul T T)) : δ = κ := by
  have h2 : ratR.mul (cq δ) (ratR.mul T T)
      = normQ (δ.cls * (T.num * T.num)) (1 * (T.den * T.den)) (mul_pos' (Nat.succ_pos 0) (mul_pos' T.den_pos T.den_pos)) :=
    (congrArg (ratR.mul (cq δ)) (e_yy T)).trans (rmul δ.cls (T.num * T.num) 1 (T.den * T.den) (Nat.succ_pos 0) (mul_pos' T.den_pos T.den_pos))
  have e := (normQ_eq_iff _ _ _ _ _ _).1 (h.trans h2)
  have hq : Int.ofNat T.den ≠ 0 := CopReduce.ofNat_ne_zero T.den_pos
  have hmq : m * Int.ofNat T.den ≠ 0 := fun h0 => by
    cases int_noZD m _ h0 with
    | inl h1 => exact hm h1
    | inr h1 => exact hq h1
  have e2 : κ.cls * ((m * Int.ofNat T.den) * (m * Int.ofNat T.den)) = δ.cls * (T.num * T.num) := by
    calc κ.cls * ((m * Int.ofNat T.den) * (m * Int.ofNat T.den))
        = κ.cls * (m * m) * Int.ofNat (1 * (T.den * T.den)) := by lib_int
      _ = δ.cls * (T.num * T.num) * Int.ofNat 1 := e
      _ = δ.cls * (T.num * T.num) := by lib_int
  exact (eq_of_cls κ δ _ _ hmq e2).symm

/-! ## Bit algebra -/

theorem xor4 : ∀ a b c d : Bool, xor (xor a b) (xor c d) = xor (xor a c) (xor b d) := by
  intro a b c d; cases a <;> cases b <;> cases c <;> cases d <;> rfl

theorem xor_cancel : ∀ x k : Bool, x = xor (xor x k) k := by
  intro x k; cases x <;> cases k <;> rfl

theorem xor4B (β1 β2 γ1 γ2 : B5) : (β1.xor β2).xor (γ1.xor γ2) = (β1.xor γ1).xor (β2.xor γ2) := by
  obtain ⟨s1, a1, b1, c1, d1⟩ := β1
  obtain ⟨s2, a2, b2, c2, d2⟩ := β2
  obtain ⟨s3, a3, b3, c3, d3⟩ := γ1
  obtain ⟨s4, a4, b4, c4, d4⟩ := γ2
  show B5.mk (xor (xor s1 s2) (xor s3 s4)) (xor (xor a1 a2) (xor a3 a4)) (xor (xor b1 b2) (xor b3 b4))
      (xor (xor c1 c2) (xor c3 c4)) (xor (xor d1 d2) (xor d3 d4))
    = B5.mk (xor (xor s1 s3) (xor s2 s4)) (xor (xor a1 a3) (xor a2 a4)) (xor (xor b1 b3) (xor b2 b4))
      (xor (xor c1 c3) (xor c2 c4)) (xor (xor d1 d3) (xor d2 d4))
  rw [xor4 s1 s2 s3 s4, xor4 a1 a2 a3 a4, xor4 b1 b2 b3 b4, xor4 c1 c2 c3 c4, xor4 d1 d2 d3 d4]

theorem solve (δ1 δ2 K12 K2 : B5) (h12 : δ1.xor δ2 = K12) (h2 : δ2 = K2) : δ1 = K12.xor K2 := by
  rw [← h12, h2]
  obtain ⟨s1, a1, b1, c1, d1⟩ := δ1
  obtain ⟨s2, a2, b2, c2, d2⟩ := K2
  show B5.mk s1 a1 b1 c1 d1
    = B5.mk (xor (xor s1 s2) s2) (xor (xor a1 a2) a2) (xor (xor b1 b2) b2) (xor (xor c1 c2) c2) (xor (xor d1 d2) d2)
  rw [← xor_cancel s1 s2, ← xor_cancel a1 a2, ← xor_cancel b1 b2, ← xor_cancel c1 c2, ← xor_cancel d1 d2]

end TransStep

#print axioms TransStep.sub0
#print axioms TransStep.mul_ne
#print axioms TransStep.third_cq
#print axioms TransStep.T_ne
#print axioms TransStep.cls3
#print axioms TransStep.bits_of_eq
#print axioms TransStep.xor4
#print axioms TransStep.xor_cancel
#print axioms TransStep.xor4B
#print axioms TransStep.solve

bsd/TransStep2.lean

lean_all/bsd/TransStep2.lean

/-
  bsd/TransStep2.lean — T39 (goal G289e-6b): the chord step on descent classes of E_42486 over Q.
  P₁ (class (β₁, β₂)), P₂ (class (γ₁, γ₂)), x₁ ≠ x₂, both away from E[2]; P₃ the third chord point. Then either P₃ is away
  from E[2] with class (β₁ ⊕ γ₁, β₂ ⊕ γ₂), or P₃ ∈ E[2] and (β₁ ⊕ γ₁, β₂ ⊕ γ₂) is the class of that torsion point
  ((−1, −n), (n, 2), (−n, −2n)), which lies in V.
    const_sub     (a/1) − (b/1) = (a − b)/1 without evaluating normQ
    tor_*         the values x − e at torsion points as castQ (cls κ · m²)
    step          the dichotomy
  Lean 4 core plus bsd/TransStep, bsd/TransQ.
-/
import bsd.TransStep

namespace TransStep2
open DetChain LibRing LibRat LibInt QModel QCls B5S TransStep TransQ

def inVB (δ1 δ2 : B5) : Bool := VSet.inV δ1.s δ1.a δ1.b δ1.c δ1.d δ2.s δ2.a δ2.b δ2.c δ2.d

theorem const_sub (a b : Int) : sub ratR (normQ a 1 one_pos') (normQ b 1 one_pos')
    = normQ (a * Int.ofNat 1 + -b * Int.ofNat 1) (1 * 1) (mul_pos' one_pos' one_pos') :=
  calc sub ratR (normQ a 1 one_pos') (normQ b 1 one_pos')
      = ratR.add (normQ a 1 one_pos') (ratR.neg (normQ b 1 one_pos')) := rfl
    _ = ratR.add (normQ a 1 one_pos') (normQ (-b) 1 one_pos') := congrArg (ratR.add _) (rneg b 1 one_pos')
    _ = _ := radd a (-b) 1 1 one_pos' one_pos'

theorem zero_n1 : zeroQ = normQ 0 1 one_pos' := rfl
theorem n_n1 : nQ = normQ 42486 1 one_pos' := rfl

def KT0 : B5 := ⟨true, true, true, true, true⟩      -- −42486
def KN : B5 := ⟨false, true, true, true, true⟩      -- 42486
def K2N : B5 := ⟨false, false, true, true, true⟩    -- 21243 (2n = 21243·2²)
def Km2N : B5 := ⟨true, false, true, true, true⟩    -- −21243 (−2n)

theorem cval (a b : Int) (κ : B5) (m : Int) (h : (a * Int.ofNat 1 + -b * Int.ofNat 1) * Int.ofNat 1 = κ.cls * (m * m) * Int.ofNat (1 * 1)) :
    sub ratR (normQ a 1 one_pos') (normQ b 1 one_pos') = castQ (κ.cls * (m * m)) :=
  (const_sub a b).trans ((normQ_eq_iff _ _ _ _ _ _).2 h)

theorem tor_0n : sub ratR zeroQ nQ = castQ (KT0.cls * (1 * 1)) := cval 0 42486 KT0 1 rfl
theorem tor_0m : sub ratR zeroQ (negQ nQ) = castQ (KN.cls * (1 * 1)) := by
  rw [negQ_nQ]; exact cval 0 (-42486) KN 1 rfl
theorem tor_n0 : sub ratR nQ zeroQ = castQ (KN.cls * (1 * 1)) := cval 42486 0 KN 1 rfl
theorem tor_nm : sub ratR nQ (negQ nQ) = castQ (K2N.cls * (2 * 2)) := by
  rw [negQ_nQ]; exact cval 42486 (-42486) K2N 2 rfl
theorem tor_m0 : sub ratR (negQ nQ) zeroQ = castQ (KT0.cls * (1 * 1)) := by
  rw [negQ_nQ]; exact cval (-42486) 0 KT0 1 rfl
theorem tor_mn : sub ratR (negQ nQ) nQ = castQ (Km2N.cls * (2 * 2)) := by
  rw [negQ_nQ]; exact cval (-42486) 42486 Km2N 2 rfl

theorem one_ne : (1 : Int) ≠ 0 := fun h => Nat.noConfusion (Int.ofNat.inj h)
theorem two_ne : (2 : Int) ≠ 0 := fun h => Nat.noConfusion (Int.ofNat.inj h)

theorem solve2 (δ1 δ2 K12 K1 : B5) (h12 : δ1.xor δ2 = K12) (h1 : δ1 = K1) : δ2 = K12.xor K1 := by
  rw [← h12, h1]
  obtain ⟨s1, a1, b1, c1, d1⟩ := K1
  obtain ⟨s2, a2, b2, c2, d2⟩ := δ2
  show B5.mk s2 a2 b2 c2 d2
    = B5.mk (xor (xor s1 s2) s1) (xor (xor a1 a2) a1) (xor (xor b1 b2) b1) (xor (xor c1 c2) c1) (xor (xor d1 d2) d1)
  have e : ∀ x k : Bool, x = xor (xor k x) k := by intro x k; cases x <;> cases k <;> rfl
  rw [← e s2 s1, ← e a2 a1, ← e b2 b1, ← e c2 c1, ← e d2 d1]

theorem ne_of_num {x : Q} (h : x.num ≠ 0) : x ≠ zeroQ := fun h0 => h (num_eq_zero_of x h0)

/-- **The chord step.** -/
theorem step (x1 y1 x2 y2 : Q) (o1 : OnE x1 y1) (o2 : OnE x2 y2) (h12 : x1 ≠ x2)
    (β1 β2 γ1 γ2 : B5) (hc1 : HC x1 β1 β2) (hc2 : HC x2 γ1 γ2) (nd1 : ND x1) (nd2 : ND x2) :
    (ND (x3 x1 y1 x2 y2) ∧ HC (x3 x1 y1 x2 y2) (β1.xor γ1) (β2.xor γ2)) ∨ inVB (β1.xor γ1) (β2.xor γ2) = true := by
  obtain ⟨nx1, nxn1, nxm1⟩ := nd1
  obtain ⟨nx2, nxn2, nxm2⟩ := nd2
  obtain ⟨T1, eT1⟩ := cls3 x1 y1 o1 β1 β2 hc1 nx1 nxn1
  obtain ⟨T2, eT2⟩ := cls3 x2 y2 o2 γ1 γ2 hc2 nx2 nxn2
  obtain ⟨s1, r1, _, _, ex1, exn1⟩ := hc1
  obtain ⟨s2, r2, _, _, ex2, exn2⟩ := hc2
  have ex1' : sub ratR x1 zeroQ = ratR.mul (cq β1) (ratR.mul s1 s1) := (sub0 x1).trans ex1
  have ex2' : sub ratR x2 zeroQ = ratR.mul (cq γ1) (ratR.mul s2 s2) := (sub0 x2).trans ex2
  have hab0 : ratR.mul (sub ratR x1 zeroQ) (sub ratR x2 zeroQ) ≠ zeroQ := by
    rw [sub0 x1, sub0 x2]; exact mul_ne _ _ nx1 nx2
  have habn := mul_ne _ _ nxn1 nxn2
  have habm := mul_ne _ _ nxm1 nxm2
  have ch0 := chord_Q x1 y1 x2 y2 h12 o1 o2 zeroQ (Or.inl rfl)
  have chn := chord_Q x1 y1 x2 y2 h12 o1 o2 nQ (Or.inr (Or.inl rfl))
  have chm := chord_Q x1 y1 x2 y2 h12 o1 o2 (negQ nQ) (Or.inr (Or.inr rfl))
  -- the three coordinates of P₃ as classes (whenever defined)
  have c0 := third_cq β1 γ1 _ _ _ s1 s2 _ ex1' ex2' hab0 ch0
  have cn := third_cq β2 γ2 _ _ _ r1 r2 _ exn1 exn2 habn chn
  have cm := third_cq (β1.xor β2) (γ1.xor γ2) _ _ _ T1 T2 _ eT1 eT2 habm chm
  have hx4 := xor4B β1 β2 γ1 γ2
  cases Int.decEq (x3 x1 y1 x2 y2).num 0 with
  | isTrue h0 =>
    -- P₃ = (0, 0)
    right
    have hz : x3 x1 y1 x2 y2 = zeroQ := zero_eq _ h0
    obtain ⟨Tn, eTn⟩ := cn
    obtain ⟨Tm, eTm⟩ := cm
    rw [hz, tor_0n] at eTn
    rw [hz, tor_0m] at eTm
    have d2 := bits_of_eq KT0 _ 1 one_ne Tn eTn
    have d12 := bits_of_eq KN _ 1 one_ne Tm eTm
    rw [hx4] at d12
    rw [solve _ _ _ _ d12 d2, d2]; rfl
  | isFalse h0 =>
    have hz : x3 x1 y1 x2 y2 ≠ zeroQ := ne_of_num h0
    cases Int.decEq (sub ratR (x3 x1 y1 x2 y2) nQ).num 0 with
    | isTrue h1 =>
      -- P₃ = (n, 0)
      right
      have hn : x3 x1 y1 x2 y2 = nQ := LibVieta.sub_zero_eq ratR _ _ (zero_eq _ h1)
      obtain ⟨T0', eT0⟩ := c0
      obtain ⟨Tm, eTm⟩ := cm
      rw [hn, tor_n0] at eT0
      rw [hn, tor_nm] at eTm
      have d1 := bits_of_eq KN _ 1 one_ne T0' eT0
      have d12 := bits_of_eq K2N _ 2 two_ne Tm eTm
      rw [hx4] at d12
      rw [solve2 _ _ _ _ d12 d1, d1]; rfl
    | isFalse h1 =>
      have hn : sub ratR (x3 x1 y1 x2 y2) nQ ≠ zeroQ := ne_of_num h1
      cases Int.decEq (sub ratR (x3 x1 y1 x2 y2) (negQ nQ)).num 0 with
      | isTrue h2 =>
        -- P₃ = (−n, 0)
        right
        have hm : x3 x1 y1 x2 y2 = negQ nQ := LibVieta.sub_zero_eq ratR _ _ (zero_eq _ h2)
        obtain ⟨T0', eT0⟩ := c0
        obtain ⟨Tn, eTn⟩ := cn
        rw [hm, tor_m0] at eT0
        rw [hm, tor_mn] at eTn
        have d1 := bits_of_eq KT0 _ 1 one_ne T0' eT0
        have d2 := bits_of_eq Km2N _ 2 two_ne Tn eTn
        rw [d1, d2]; rfl
      | isFalse h2 =>
        -- P₃ away from E[2]
        left
        have hm : sub ratR (x3 x1 y1 x2 y2) (negQ nQ) ≠ zeroQ := ne_of_num h2
        obtain ⟨A, eA⟩ := c0
        obtain ⟨B, eB⟩ := cn
        have h0' : sub ratR (x3 x1 y1 x2 y2) zeroQ ≠ zeroQ := by rw [sub0]; exact hz
        refine ⟨⟨hz, hn, hm⟩, ?_⟩
        unfold HC HasCls
        exact ⟨A, B, T_ne _ _ A h0' eA, T_ne _ _ B hn eB, (sub0 _).symm.trans eA, eB⟩

end TransStep2

#print axioms TransStep2.const_sub
#print axioms TransStep2.zero_n1
#print axioms TransStep2.n_n1
#print axioms TransStep2.cval
#print axioms TransStep2.tor_0n
#print axioms TransStep2.tor_0m
#print axioms TransStep2.tor_n0
#print axioms TransStep2.tor_nm
#print axioms TransStep2.tor_m0
#print axioms TransStep2.tor_mn
#print axioms TransStep2.one_ne
#print axioms TransStep2.two_ne
#print axioms TransStep2.solve2
#print axioms TransStep2.ne_of_num
#print axioms TransStep2.step

bsd/TransStepL.lean

lean_all/bsd/TransStepL.lean

/-
  bsd/TransStepL.lean — T39 (goal G289f-4b): descent classes over an arbitrary certified list of primes and the chord
  step on E_n with n a parameter (bsd/TransStep fixes n = 42486 and the primes 2, 3, 73, 97).
  A class is a sign s and a bit list bs of length |ps|, with value cqL ps s bs = (−1)^s Π p^b ∈ Q.
    cqL, cqL_ne, cqL_mul      the value, nonzero, product = xor-class · square
    HCn n ps x …              x = cqL β₁·s², x − n = cqL β₂·r²
    third_cqL                 a = cqL β·s², b = cqL γ·t², a·b ≠ 0, a·b·c = w²  ⇒  c = cqL (β ⊕ γ)·T²
    cls3n                     x + n = cqL (β₁ ⊕ β₂)·T²
    bits_of_eqL               castQ (clsL κ·m²) = cqL δ·T², m ≠ 0  ⇒  δ = κ
  Lean 4 core plus bsd/SqClassL, bsd/TransStep, bsd/QModelN.
-/
import bsd.SqClassL
import bsd.TransStep
import bsd.QModelN

namespace TransStepL
open DetChain LibRing LibRat LibInt QModel QCls B5S SqUniqueL SqClassL QModelN

def cqL (ps : List Nat) (s : Bool) (bs : List Bool) : Q := castQ (clsL s ps bs)

theorem cqL_ne (ps : List Nat) (hp : posL ps = true) (s : Bool) (bs : List Bool) : cqL ps s bs ≠ zeroQ :=
  normQ_ne_zero (Nat.succ_pos 0) (clsL_ne ps hp s bs)

theorem cqL_mul (ps : List Nat) (s s' : Bool) (bs bs' : List Bool) (h : bs.length = ps.length)
    (h' : bs'.length = ps.length) :
    ratR.mul (cqL ps s bs) (cqL ps s' bs')
      = ratR.mul (cqL ps (xor s s') (xorL bs bs'))
          (ratR.mul (cqL ps (s && s') (andL bs bs')) (cqL ps (s && s') (andL bs bs'))) :=
  calc ratR.mul (cqL ps s bs) (cqL ps s' bs') = castQ (clsL s ps bs * clsL s' ps bs') := (castQ_mul _ _).symm
    _ = castQ (clsL (xor s s') ps (xorL bs bs')
          * (clsL (s && s') ps (andL bs bs') * clsL (s && s') ps (andL bs bs'))) :=
        congrArg castQ (clsL_mul ps s s' bs bs' h h')
    _ = ratR.mul (cqL ps (xor s s') (xorL bs bs'))
          (castQ (clsL (s && s') ps (andL bs bs') * clsL (s && s') ps (andL bs bs'))) := castQ_mul _ _
    _ = _ := congrArg (ratR.mul (cqL ps (xor s s') (xorL bs bs'))) (castQ_mul _ _)

/-- x = cqL β₁·s², x − n = cqL β₂·r² -/
def HCn (n : Q) (ps : List Nat) (x : Q) (s1 : Bool) (b1 : List Bool) (s2 : Bool) (b2 : List Bool) : Prop :=
  ∃ s r : Q, s ≠ zeroQ ∧ r ≠ zeroQ ∧ x = ratR.mul (cqL ps s1 b1) (ratR.mul s s) ∧
    sub ratR x n = ratR.mul (cqL ps s2 b2) (ratR.mul r r)

/-- the third factor of a square product of two classes -/
theorem third_cqL (ps : List Nat) (s1 s2 : Bool) (b1 b2 : List Bool) (h1 : b1.length = ps.length)
    (h2 : b2.length = ps.length) (a b c sa sb w : Q) (ha : a = ratR.mul (cqL ps s1 b1) (ratR.mul sa sa))
    (hb : b = ratR.mul (cqL ps s2 b2) (ratR.mul sb sb)) (hab : ratR.mul a b ≠ zeroQ)
    (hc : ratR.mul (ratR.mul a b) c = ratR.mul w w) :
    ∃ T : Q, c = ratR.mul (cqL ps (xor s1 s2) (xorL b1 b2)) (ratR.mul T T) := by
  have e := TransCls.third_cls ratR invQ (fun a h => mul_invQ a h) a b c (cqL ps s1 b1) (cqL ps s2 b2) sa sb w
    ha hb hab hc
  refine ⟨ratR.mul (cqL ps (s1 && s2) (andL b1 b2))
    (ratR.mul w (invQ (ratR.mul (ratR.mul (cqL ps s1 b1) (cqL ps s2 b2)) (ratR.mul sa sb)))), ?_⟩
  rw [e, cqL_mul ps s1 s2 b1 b2 h1 h2]; lib_ring

/-- the third coordinate x + n -/
theorem cls3n (n x y : Q) (ps : List Nat) (o : OnEn n x y) (s1 s2 : Bool) (b1 b2 : List Bool)
    (h1 : b1.length = ps.length) (h2 : b2.length = ps.length) (h : HCn n ps x s1 b1 s2 b2) (hx : x ≠ zeroQ)
    (hxn : sub ratR x n ≠ zeroQ) :
    ∃ T : Q, sub ratR x (negQ n) = ratR.mul (cqL ps (xor s1 s2) (xorL b1 b2)) (ratR.mul T T) := by
  obtain ⟨s, r, _, _, hs, hr⟩ := h
  have o' : ratR.mul (ratR.mul x (sub ratR x n)) (sub ratR x (negQ n)) = ratR.mul y y := by
    have o2 : ratR.mul y y = ratR.mul (ratR.mul (sub ratR x zeroQ) (sub ratR x n)) (sub ratR x (negQ n)) := o
    rw [o2, TransStep.sub0]
  exact third_cqL ps s1 s2 b1 b2 h1 h2 x (sub ratR x n) (sub ratR x (negQ n)) s r y hs hr
    (TransStep.mul_ne _ _ hx hxn) o'

theorem bits_of_eqL (ps : List Nat) (hc : chkL ps = true) (hp : posL ps = true) (k d : Bool) (kb db : List Bool)
    (hk : kb.length = ps.length) (hd : db.length = ps.length) (m : Int) (hm : m ≠ 0) (T : Q)
    (h : castQ (clsL k ps kb * (m * m)) = ratR.mul (cqL ps d db) (ratR.mul T T)) : d = k ∧ db = kb := by
  have h2 : ratR.mul (cqL ps d db) (ratR.mul T T)
      = normQ (clsL d ps db * (T.num * T.num)) (1 * (T.den * T.den))
          (mul_pos' (Nat.succ_pos 0) (mul_pos' T.den_pos T.den_pos)) :=
    (congrArg (ratR.mul (cqL ps d db)) (e_yy T)).trans
      (rmul (clsL d ps db) (T.num * T.num) 1 (T.den * T.den) (Nat.succ_pos 0) (mul_pos' T.den_pos T.den_pos))
  have e := (normQ_eq_iff _ _ _ _ _ _).1 (h.trans h2)
  have hq : Int.ofNat T.den ≠ 0 := CopReduce.ofNat_ne_zero T.den_pos
  have hmq : m * Int.ofNat T.den ≠ 0 := fun h0 => by
    cases int_noZD m _ h0 with
    | inl h1 => exact hm h1
    | inr h1 => exact hq h1
  have e2 : clsL k ps kb * ((m * Int.ofNat T.den) * (m * Int.ofNat T.den)) = clsL d ps db * (T.num * T.num) := by
    calc clsL k ps kb * ((m * Int.ofNat T.den) * (m * Int.ofNat T.den))
        = clsL k ps kb * (m * m) * Int.ofNat (1 * (T.den * T.den)) := by lib_int
      _ = clsL d ps db * (T.num * T.num) * Int.ofNat 1 := e
      _ = clsL d ps db * (T.num * T.num) := by lib_int
  obtain ⟨e3, e4⟩ := bits_eqL ps hc hp k d kb db hk hd _ _ hmq e2
  exact ⟨e3.symm, e4.symm⟩

end TransStepL

#print axioms TransStepL.cqL_ne
#print axioms TransStepL.cqL_mul
#print axioms TransStepL.third_cqL
#print axioms TransStepL.cls3n
#print axioms TransStepL.bits_of_eqL

bsd/TrianglePoint.lean

lean_all/bsd/TrianglePoint.lean

/-
  bsd/TrianglePoint.lean — T39 cycle 166: a right triangle gives a point on the congruent-number curve,
  every commutative ring.

  Lean 4 core plus lib/RingTac. If a² + b² = c² (area n = ab/2, N = 2ab = 4n), then on E_N : Y² = X³ − N²X
      X = c²,  Y = c(b² − a²)        (the point (c²/4, c(b² − a²)/8) of E_n, scaled by 4 and 8)
  and X, X − N, X + N are the squares c², (a − b)², (a + b)²: by Doubling (x₂, x₂ ∓ n squares) this is the
  shape of a doubled point — the classical fact that the triangle point lies in 2E(Q).
-/
import lib.RingTac

namespace TrianglePoint
open DetChain LibRing

variable {R : Type} (K : CRing R)

def sq (x : R) : R := K.mul x x
def two (x : R) : R := K.add x x

theorem triangle_point (a b c : R) (h : K.add (sq K a) (sq K b) = sq K c) :
    sq K (K.mul c (K.add (sq K b) (K.neg (sq K a))))
      = K.add (K.mul (sq K c) (sq K (sq K c))) (K.neg (K.mul (sq K (two K (K.mul a b))) (sq K c))) := by
  rw [show sq K (K.mul c (K.add (sq K b) (K.neg (sq K a))))
      = K.mul (sq K c) (sq K (K.add (sq K b) (K.neg (sq K a)))) by unfold sq; lib_ring,
    show K.add (K.mul (sq K c) (sq K (sq K c))) (K.neg (K.mul (sq K (two K (K.mul a b))) (sq K c)))
      = K.mul (sq K c) (K.add (sq K (sq K c)) (K.neg (sq K (two K (K.mul a b))))) by unfold sq; lib_ring,
    ← h]
  unfold sq two; lib_ring

theorem shifts (a b c : R) (h : K.add (sq K a) (sq K b) = sq K c) :
    K.add (sq K c) (K.neg (two K (K.mul a b))) = sq K (K.add a (K.neg b)) ∧
    K.add (sq K c) (two K (K.mul a b)) = sq K (K.add a b) := by
  rw [← h]; unfold sq two
  exact ⟨by lib_ring, by lib_ring⟩

end TrianglePoint

#print axioms TrianglePoint.triangle_point
#print axioms TrianglePoint.shifts

bsd/TrivialModel.lean

lean_all/bsd/TrivialModel.lean

/-
  bsd/TrivialModel.lean — T39 cycle 151: the trivial bound |a_p| ≤ p for every Weierstrass model
  y² + B(x) y + Q(x) = P(x) at every odd prime (generalises BSDChain.trivial_bound from 11a).

  Lean 4 core plus bsd/CountModel, BSDChain (legV_le_two, sum_le_two). Hand proofs.

    model_upper    #E(F_p) = affM + 1 ≤ 2p + 1
    model_lower    1 ≤ #E(F_p)
    model_trivial  #E(F_p) ≤ p + 1 + p and p + 1 ≤ #E(F_p) + p, i.e. −p ≤ a_p ≤ p
-/
import bsd.CountModel
import BSDChain

namespace TrivialModel
open FTAChain FrobChain QRChain ESChain CountModel BSDChain

theorem model_upper (h : Nat) (hp : Pr (h + h + 1)) (B Q P : Nat → Nat) :
    affM B Q P (h + h + 1) + 1 ≤ 2 * (h + h + 1) + 1 := by
  rw [count_model h hp B Q P, Nat.add_comm 1]
  exact Nat.add_le_add_right (sum_le_two _ _ (fun x => legV_le_two _ _)) 1

theorem model_lower (B Q P : Nat → Nat) (p : Nat) : 1 ≤ affM B Q P p + 1 :=
  Nat.le_add_left 1 _

/-- **|a_p| ≤ p for every model y² + B(x)y + Q(x) = P(x) and every odd prime p.** -/
theorem model_trivial (h : Nat) (hp : Pr (h + h + 1)) (B Q P : Nat → Nat) :
    affM B Q P (h + h + 1) + 1 ≤ (h + h + 1) + 1 + (h + h + 1) ∧
    (h + h + 1) + 1 ≤ (affM B Q P (h + h + 1) + 1) + (h + h + 1) := by
  refine ⟨?_, ?_⟩
  · have e : (h + h + 1) + 1 + (h + h + 1) = 2 * (h + h + 1) + 1 := by
      rw [Nat.add_right_comm, Nat.two_mul]
    rw [e]; exact model_upper h hp B Q P
  · rw [Nat.add_comm (affM B Q P (h + h + 1) + 1)]
    exact Nat.add_le_add_left (model_lower B Q P _) _

end TrivialModel

#print axioms TrivialModel.model_upper
#print axioms TrivialModel.model_lower
#print axioms TrivialModel.model_trivial

bsd/Tunnell.lean

lean_all/bsd/Tunnell.lean

/-
  bsd/Tunnell.lean — T39 cycle 75: Tunnell's counts agree with the computed analytic ranks for every
  squarefree n ≤ 200 (122 curves y² = x³ − n²x).

  Lean 4 core, rfl. For odd n: A = #{x² + 2y² + 8z² = n}, B = #{x² + 2y² + 32z² = n}; for even n = 2m:
  A = #{x² + 4y² + 8z² = m}, B = #{x² + 4y² + 32z² = m} (x, y, z ∈ ℤ). Tunnell (1983): A ≠ 2B ⇒ n is not
  congruent (unconditional, via Coates–Wiles); A = 2B ⇔ L(E_n, 1) = 0.
    rank0_tunnell   A ≠ 2B for the 49 n with computed analytic rank 0
    pos_tunnell     A = 2B for the 73 n with computed analytic rank ≥ 1
  (the analytic ranks from NUMERIC_BSD_CONGRUENT.jsonl agree with Tunnell's criterion for all 122).
-/
namespace Tunnell

def w (x : Nat) : Nat := if x = 0 then 1 else 2

def isqrt (n : Nat) : Nat := go n (n + 1)
where go (n : Nat) : Nat → Nat
  | 0 => 0
  | f + 1 => if (f + 1) * (f + 1) ≤ n then f + 1 else go n f

/-- #{(x, y, z) ∈ ℤ³ : x² + b y² + c z² = n} -/
def cnt (n b c : Nat) : Nat :=
  let r := isqrt n + 1
  (List.range r).foldl (fun s x => (List.range r).foldl (fun s y => (List.range r).foldl (fun s z =>
    if x * x + b * (y * y) + c * (z * z) = n then s + w x * w y * w z else s) s) s) 0

def tA (n : Nat) : Nat := if n % 2 = 1 then cnt n 2 8 else cnt (n / 2) 4 8
def tB (n : Nat) : Nat := if n % 2 = 1 then cnt n 2 32 else cnt (n / 2) 4 32

def rank0 : List Nat := [1, 2, 3, 10, 11, 17, 19, 26, 33, 35, 42, 43, 51, 57, 58, 59, 66, 67, 73, 74, 82, 83, 89, 91, 97, 105, 106, 107, 113, 114, 115, 122, 123, 129, 130, 131, 139, 146, 155, 163, 170, 177, 178, 179, 185, 186, 187, 193, 195]
def rankPos : List Nat := [5, 6, 7, 13, 14, 15, 21, 22, 23, 29, 30, 31, 34, 37, 38, 39, 41, 46, 47, 53, 55, 61, 62, 65, 69, 70, 71, 77, 78, 79, 85, 86, 87, 93, 94, 95, 101, 102, 103, 109, 110, 111, 118, 119, 127, 133, 134, 137, 138, 141, 142, 143, 145, 149, 151, 154, 157, 158, 159, 161, 165, 166, 167, 173, 174, 181, 182, 183, 190, 191, 194, 197, 199]

set_option maxRecDepth 100000 in
theorem rank0_tunnell : rank0.all (fun n => !(Nat.beq (tA n) (2 * tB n))) = true := rfl
set_option maxRecDepth 100000 in
theorem pos_tunnell : rankPos.all (fun n => Nat.beq (tA n) (2 * tB n)) = true := rfl

end Tunnell

#print axioms Tunnell.rank0_tunnell
#print axioms Tunnell.pos_tunnell

bsd/Tunnell2.lean

lean_all/bsd/Tunnell2.lean

/-
  bsd/Tunnell2.lean — T39 cycle 126 (n = 201–400): Tunnell's counts agree with the computed analytic ranks for every
  squarefree 201 ≤ n ≤ 400 (118 curves y² = x³ − n²x).

  Lean 4 core, rfl. For odd n: A = #{x² + 2y² + 8z² = n}, B = #{x² + 2y² + 32z² = n}; for even n = 2m:
  A = #{x² + 4y² + 8z² = m}, B = #{x² + 4y² + 32z² = m} (x, y, z ∈ ℤ). Tunnell (1983): A ≠ 2B ⇒ n is not
  congruent (unconditional, via Coates–Wiles); A = 2B ⇔ L(E_n, 1) = 0.
    rank0_tunnell   A ≠ 2B for the 45 n with computed analytic rank 0
    pos_tunnell     A = 2B for the 73 n with computed analytic rank ≥ 1
  (the analytic ranks from NUMERIC_BSD_CONGRUENT.jsonl agree with Tunnell's criterion for all 122).
-/
namespace Tunnell2

def w (x : Nat) : Nat := if x = 0 then 1 else 2

def isqrt (n : Nat) : Nat := go n (n + 1)
where go (n : Nat) : Nat → Nat
  | 0 => 0
  | f + 1 => if (f + 1) * (f + 1) ≤ n then f + 1 else go n f

/-- #{(x, y, z) ∈ ℤ³ : x² + b y² + c z² = n} -/
def cnt (n b c : Nat) : Nat :=
  let r := isqrt n + 1
  (List.range r).foldl (fun s x => (List.range r).foldl (fun s y => (List.range r).foldl (fun s z =>
    if x * x + b * (y * y) + c * (z * z) = n then s + w x * w y * w z else s) s) s) 0

def tA (n : Nat) : Nat := if n % 2 = 1 then cnt n 2 8 else cnt (n / 2) 4 8
def tB (n : Nat) : Nat := if n % 2 = 1 then cnt n 2 32 else cnt (n / 2) 4 32

def rank0 : List Nat := [201, 202, 203, 209, 211, 217, 218, 227, 233, 235, 241, 249, 251, 258, 259, 266, 267, 273, 274, 281, 282, 283, 290, 298, 305, 307, 314, 321, 322, 329, 331, 337, 339, 345, 346, 347, 354, 355, 362, 370, 377, 379, 385, 393, 394]
def rankPos : List Nat := [205, 206, 210, 213, 214, 215, 219, 221, 222, 223, 226, 229, 230, 231, 237, 238, 239, 246, 247, 253, 254, 255, 257, 262, 263, 265, 269, 271, 277, 278, 285, 286, 287, 291, 293, 295, 299, 301, 302, 303, 309, 310, 311, 313, 317, 318, 319, 323, 326, 327, 330, 334, 335, 341, 349, 353, 357, 358, 359, 365, 366, 367, 371, 373, 374, 381, 382, 383, 386, 389, 390, 391, 395]

set_option maxRecDepth 100000 in
set_option maxHeartbeats 0 in
theorem rank0_tunnell : rank0.all (fun n => !(Nat.beq (tA n) (2 * tB n))) = true := rfl
set_option maxRecDepth 100000 in
set_option maxHeartbeats 0 in
theorem pos_tunnell : rankPos.all (fun n => Nat.beq (tA n) (2 * tB n)) = true := rfl

end Tunnell2

#print axioms Tunnell2.rank0_tunnell
#print axioms Tunnell2.pos_tunnell

bsd/Tunnell3.lean

lean_all/bsd/Tunnell3.lean

/-
  bsd/Tunnell3.lean — T39 cycle 142 (n = 401–600): Tunnell's counts agree with the computed analytic ranks for every
  squarefree 401 ≤ n ≤ 600 (123 curves y² = x³ − n²x).

  Lean 4 core, rfl. For odd n: A = #{x² + 2y² + 8z² = n}, B = #{x² + 2y² + 32z² = n}; for even n = 2m:
  A = #{x² + 4y² + 8z² = m}, B = #{x² + 4y² + 32z² = m} (x, y, z ∈ ℤ). Tunnell (1983): A ≠ 2B ⇒ n is not
  congruent (unconditional, via Coates–Wiles); A = 2B ⇔ L(E_n, 1) = 0.
    rank0_tunnell   A ≠ 2B for the 50 n with computed analytic rank 0
    pos_tunnell     A = 2B for the 73 n with computed analytic rank ≥ 1
  (the analytic ranks from NUMERIC_BSD_CONGRUENT.jsonl agree with Tunnell's criterion for all 122).
-/
namespace Tunnell3

def w (x : Nat) : Nat := if x = 0 then 1 else 2

def isqrt (n : Nat) : Nat := go n (n + 1)
where go (n : Nat) : Nat → Nat
  | 0 => 0
  | f + 1 => if (f + 1) * (f + 1) ≤ n then f + 1 else go n f

/-- #{(x, y, z) ∈ ℤ³ : x² + b y² + c z² = n} -/
def cnt (n b c : Nat) : Nat :=
  let r := isqrt n + 1
  (List.range r).foldl (fun s x => (List.range r).foldl (fun s y => (List.range r).foldl (fun s z =>
    if x * x + b * (y * y) + c * (z * z) = n then s + w x * w y * w z else s) s) s) 0

def tA (n : Nat) : Nat := if n % 2 = 1 then cnt n 2 8 else cnt (n / 2) 4 8
def tB (n : Nat) : Nat := if n % 2 = 1 then cnt n 2 32 else cnt (n / 2) 4 32

def rank0 : List Nat := [401, 402, 403, 409, 411, 417, 418, 419, 427, 433, 435, 443, 449, 451, 458, 466, 467, 473, 474, 481, 482, 483, 489, 491, 497, 498, 499, 506, 515, 521, 523, 530, 537, 538, 545, 547, 553, 554, 555, 562, 563, 569, 570, 571, 577, 579, 586, 587, 593, 595]
def rankPos : List Nat := [406, 407, 410, 413, 415, 421, 422, 426, 429, 430, 431, 434, 437, 438, 439, 442, 445, 446, 447, 453, 454, 455, 457, 461, 462, 463, 465, 469, 470, 471, 478, 479, 485, 487, 493, 494, 501, 502, 503, 505, 509, 510, 511, 514, 517, 518, 519, 526, 527, 533, 534, 535, 541, 542, 543, 546, 551, 557, 559, 561, 565, 566, 573, 574, 581, 582, 583, 589, 590, 591, 597, 598, 599]

set_option maxRecDepth 100000 in
set_option maxHeartbeats 0 in
theorem rank0_tunnell : rank0.all (fun n => !(Nat.beq (tA n) (2 * tB n))) = true := rfl
set_option maxRecDepth 100000 in
set_option maxHeartbeats 0 in
theorem pos_tunnell : rankPos.all (fun n => Nat.beq (tA n) (2 * tB n)) = true := rfl

end Tunnell3

#print axioms Tunnell3.rank0_tunnell
#print axioms Tunnell3.pos_tunnell

bsd/Tunnell4.lean

lean_all/bsd/Tunnell4.lean

/-
  bsd/Tunnell4.lean — T39 cycle 154 (n = 601–800): Tunnell's counts agree with the computed analytic ranks for every
  squarefree 601 ≤ n ≤ 800 (123 curves y² = x³ − n²x).

  Lean 4 core, rfl. For odd n: A = #{x² + 2y² + 8z² = n}, B = #{x² + 2y² + 32z² = n}; for even n = 2m:
  A = #{x² + 4y² + 8z² = m}, B = #{x² + 4y² + 32z² = m} (x, y, z ∈ ℤ). Tunnell (1983): A ≠ 2B ⇒ n is not
  congruent (unconditional, via Coates–Wiles); A = 2B ⇔ L(E_n, 1) = 0.
    rank0_tunnell   A ≠ 2B for the 53 n with computed analytic rank 0
    pos_tunnell     A = 2B for the 70 n with computed analytic rank ≥ 1
  (the analytic ranks from NUMERIC_BSD_CONGRUENT4.jsonl agree with Tunnell's criterion for all 123).
-/
namespace Tunnell4

def w (x : Nat) : Nat := if x = 0 then 1 else 2

def isqrt (n : Nat) : Nat := go n (n + 1)
where go (n : Nat) : Nat → Nat
  | 0 => 0
  | f + 1 => if (f + 1) * (f + 1) ≤ n then f + 1 else go n f

/-- #{(x, y, z) ∈ ℤ³ : x² + b y² + c z² = n} -/
def cnt (n b c : Nat) : Nat :=
  let r := isqrt n + 1
  (List.range r).foldl (fun s x => (List.range r).foldl (fun s y => (List.range r).foldl (fun s z =>
    if x * x + b * (y * y) + c * (z * z) = n then s + w x * w y * w z else s) s) s) 0

def tA (n : Nat) : Nat := if n % 2 = 1 then cnt n 2 8 else cnt (n / 2) 4 8
def tB (n : Nat) : Nat := if n % 2 = 1 then cnt n 2 32 else cnt (n / 2) 4 32

def rank0 : List Nat := [601, 610, 611, 617, 618, 619, 626, 627, 633, 634, 635, 641, 642, 643, 649, 659, 665, 667, 673, 681, 682, 683, 690, 691, 697, 698, 699, 705, 706, 707, 713, 714, 715, 730, 737, 739, 745, 746, 753, 754, 755, 762, 763, 769, 770, 771, 778, 779, 785, 786, 787, 794, 795]
def rankPos : List Nat := [602, 606, 607, 609, 613, 614, 615, 622, 623, 629, 631, 638, 645, 646, 647, 651, 653, 654, 655, 658, 661, 662, 663, 669, 670, 671, 674, 677, 678, 679, 685, 687, 689, 694, 695, 701, 703, 709, 710, 717, 718, 719, 721, 723, 727, 731, 733, 734, 741, 742, 743, 749, 751, 757, 758, 759, 761, 766, 767, 773, 777, 781, 782, 789, 790, 791, 793, 797, 798, 799]

set_option maxRecDepth 100000 in
set_option maxHeartbeats 0 in
theorem rank0_tunnell : rank0.all (fun n => !(Nat.beq (tA n) (2 * tB n))) = true := rfl
set_option maxRecDepth 100000 in
set_option maxHeartbeats 0 in
theorem pos_tunnell : rankPos.all (fun n => Nat.beq (tA n) (2 * tB n)) = true := rfl

end Tunnell4

#print axioms Tunnell4.rank0_tunnell
#print axioms Tunnell4.pos_tunnell

bsd/Tunnell5.lean

lean_all/bsd/Tunnell5.lean

/-
  bsd/Tunnell5.lean — T39 cycle 187 (n = 801–1000): Tunnell's counts agree with the computed analytic ranks for every
  squarefree 801 ≤ n ≤ 1000 (119 curves y² = x³ − n²x).

  Lean 4 core, rfl. For odd n: A = #{x² + 2y² + 8z² = n}, B = #{x² + 2y² + 32z² = n}; for even n = 2m:
  A = #{x² + 4y² + 8z² = m}, B = #{x² + 4y² + 32z² = m} (x, y, z ∈ ℤ). Tunnell (1983): A ≠ 2B ⇒ n is not
  congruent (unconditional, via Coates–Wiles); A = 2B ⇔ L(E_n, 1) = 0.
    rank0_tunnell   A ≠ 2B for the 50 n with computed analytic rank 0
    pos_tunnell     A = 2B for the 69 n with computed analytic rank ≥ 1
  (the analytic ranks from NUMERIC_BSD_CONGRUENT5.jsonl agree with Tunnell's criterion for all 119).
-/
namespace Tunnell5

def w (x : Nat) : Nat := if x = 0 then 1 else 2

def isqrt (n : Nat) : Nat := go n (n + 1)
where go (n : Nat) : Nat → Nat
  | 0 => 0
  | f + 1 => if (f + 1) * (f + 1) ≤ n then f + 1 else go n f

/-- #{(x, y, z) ∈ ℤ³ : x² + b y² + c z² = n} -/
def cnt (n b c : Nat) : Nat :=
  let r := isqrt n + 1
  (List.range r).foldl (fun s x => (List.range r).foldl (fun s y => (List.range r).foldl (fun s z =>
    if x * x + b * (y * y) + c * (z * z) = n then s + w x * w y * w z else s) s) s) 0

def tA (n : Nat) : Nat := if n % 2 = 1 then cnt n 2 8 else cnt (n / 2) 4 8
def tB (n : Nat) : Nat := if n % 2 = 1 then cnt n 2 32 else cnt (n / 2) 4 32

def rank0 : List Nat := [802, 803, 809, 811, 817, 818, 826, 827, 834, 835, 842, 843, 849, 851, 857, 858, 859, 865, 874, 881, 883, 897, 898, 899, 906, 907, 913, 914, 921, 922, 923, 929, 930, 937, 938, 939, 946, 947, 953, 955, 962, 969, 970, 971, 977, 978, 979, 986, 993, 994]
def rankPos : List Nat := [805, 806, 807, 813, 814, 815, 821, 822, 823, 829, 830, 831, 838, 839, 853, 854, 861, 862, 863, 866, 869, 870, 871, 877, 878, 879, 885, 886, 887, 889, 890, 893, 894, 895, 901, 902, 903, 905, 910, 911, 915, 917, 919, 926, 933, 934, 935, 941, 942, 943, 949, 951, 957, 958, 959, 965, 966, 967, 973, 974, 982, 983, 985, 987, 989, 991, 995, 997, 998]

set_option maxRecDepth 4000000 in
set_option maxHeartbeats 0 in
theorem rank0_tunnell : rank0.all (fun n => !(Nat.beq (tA n) (2 * tB n))) = true := rfl
set_option maxRecDepth 4000000 in
set_option maxHeartbeats 0 in
theorem pos_tunnell : rankPos.all (fun n => Nat.beq (tA n) (2 * tB n)) = true := rfl

end Tunnell5

#print axioms Tunnell5.rank0_tunnell
#print axioms Tunnell5.pos_tunnell

bsd/Tunnell6.lean

lean_all/bsd/Tunnell6.lean

/-
  bsd/Tunnell6.lean — T39 cycle 314 (goal G52, n = 1001–1200): Tunnell's counts for every squarefree 1001 ≤ n ≤ 1200
  (122 curves y² = x³ − n²x); the comparison with analytic ranks follows a2_cong6.

  Lean 4 core, rfl. For odd n: A = #{x² + 2y² + 8z² = n}, B = #{x² + 2y² + 32z² = n}; for even n = 2m:
  A = #{x² + 4y² + 8z² = m}, B = #{x² + 4y² + 32z² = m} (x, y, z ∈ ℤ). Tunnell (1983): A ≠ 2B ⇒ n is not
  congruent (unconditional, via Coates–Wiles); A = 2B ⇔ L(E_n, 1) = 0.
    tun_ne    A ≠ 2B for 44 n — not congruent (Tunnell's unconditional direction, cited)
    tun_eq    A = 2B for 78 n
-/
namespace Tunnell6

def w (x : Nat) : Nat := if x = 0 then 1 else 2

def isqrt (n : Nat) : Nat := go n (n + 1)
where go (n : Nat) : Nat → Nat
  | 0 => 0
  | f + 1 => if (f + 1) * (f + 1) ≤ n then f + 1 else go n f

/-- #{(x, y, z) ∈ ℤ³ : x² + b y² + c z² = n} -/
def cnt (n b c : Nat) : Nat :=
  let r := isqrt n + 1
  (List.range r).foldl (fun s x => (List.range r).foldl (fun s y => (List.range r).foldl (fun s z =>
    if x * x + b * (y * y) + c * (z * z) = n then s + w x * w y * w z else s) s) s) 0

def tA (n : Nat) : Nat := if n % 2 = 1 then cnt n 2 8 else cnt (n / 2) 4 8
def tB (n : Nat) : Nat := if n % 2 = 1 then cnt n 2 32 else cnt (n / 2) 4 32

def neList : List Nat := [1001, 1002, 1009, 1010, 1011, 1018, 1019, 1027, 1033, 1034, 1041, 1042, 1043, 1049, 1051, 1059, 1065, 1066, 1067, 1074, 1082, 1090, 1091, 1097, 1099, 1106, 1114, 1115, 1121, 1123, 1129, 1130, 1137, 1138, 1139, 1147, 1153, 1162, 1163, 1171, 1177, 1187, 1193, 1194]
def eqList : List Nat := [1003, 1005, 1006, 1007, 1013, 1015, 1021, 1022, 1023, 1030, 1031, 1037, 1038, 1039, 1045, 1046, 1047, 1054, 1055, 1057, 1061, 1063, 1069, 1070, 1073, 1077, 1079, 1081, 1085, 1086, 1087, 1093, 1094, 1095, 1101, 1102, 1103, 1105, 1109, 1110, 1111, 1113, 1117, 1118, 1119, 1122, 1126, 1131, 1133, 1135, 1141, 1142, 1145, 1146, 1149, 1151, 1154, 1155, 1157, 1158, 1159, 1165, 11  -- (+93 chars; full line in the Zenodo source)

set_option maxRecDepth 4000000 in
set_option maxHeartbeats 0 in
theorem tun_ne : neList.all (fun n => !(Nat.beq (tA n) (2 * tB n))) = true := rfl

set_option maxRecDepth 4000000 in
set_option maxHeartbeats 0 in
theorem tun_eq : eqList.all (fun n => Nat.beq (tA n) (2 * tB n)) = true := rfl

end Tunnell6

#print axioms Tunnell6.tun_ne
#print axioms Tunnell6.tun_eq

bsd/TunnellMod8.lean

lean_all/bsd/TunnellMod8.lean

/-
  bsd/TunnellMod8.lean — T39 cycle 132: Tunnell's counts vanish for every odd n ≡ 5, 7 (mod 8).

  Lean 4 core plus bsd/Tunnell (cnt, tA, tB), bsd/Mod16 (Z16, r16). x² + 2y² + 8z² and x² + 2y² + 32z² never
  take a value ≡ 5, 7 (mod 8) (a residue table mod 16), so for every odd n ≡ 5, 7 (mod 8), and every even n = 2m with m ≡ 3 (mod 4) (forms x² + 4y² + 8z², x² + 4y² + 32z²),
      A(n) = B(n) = 0,  hence  A(n) = 2B(n)
  — Tunnell's condition holds (as the root number −1 of E_n predicts; these n are expected congruent).
-/
import bsd.Tunnell
import bsd.Mod16

namespace TunnellMod8
open Mod16 Tunnell

def bad : Z16 → Bool
  | Z16.r0 => false | Z16.r1 => false | Z16.r2 => false | Z16.r3 => false
  | Z16.r4 => false | Z16.r5 => true | Z16.r6 => false | Z16.r7 => true
  | Z16.r8 => false | Z16.r9 => false | Z16.r10 => false | Z16.r11 => false
  | Z16.r12 => false | Z16.r13 => true | Z16.r14 => false | Z16.r15 => true

theorem ite_no {α : Type} (c : Prop) [d : Decidable c] (x y : α) (h : ¬ c) : (if c then x else y) = y := by
  cases d with
  | isFalse _ => rfl
  | isTrue hc => exact absurd hc h

set_option maxHeartbeats 0 in
theorem form_tbl (c : Z16) (hc : c = Z16.r8 ∨ c = Z16.r0) (x y z : Z16) :
    bad (((x.mul x).add (Z16.r2.mul (y.mul y))).add (c.mul (z.mul z))) = false := by
  cases hc with
  | inl h => subst h; cases x <;> cases y <;> cases z <;> rfl
  | inr h => subst h; cases x <;> cases y <;> cases z <;> rfl

theorem never (n x y z c : Nat) (hc : r16 c = Z16.r8 ∨ r16 c = Z16.r0) (hn : bad (r16 n) = true) :
    x * x + 2 * (y * y) + c * (z * z) ≠ n := by
  intro e
  have k := congrArg r16 e
  rw [r16_add, r16_add, r16_mul, r16_mul, r16_mul, r16_mul, r16_mul] at k
  have t := form_tbl (r16 c) hc (r16 x) (r16 y) (r16 z)
  rw [show Z16.r2 = r16 2 from rfl, k, hn] at t
  exact Bool.noConfusion t

theorem foldl_const {α : Type} (g : Nat → α → Nat) (h : ∀ s a, g s a = s) :
    ∀ (l : List α) (s : Nat), l.foldl g s = s
  | [], _ => rfl
  | a :: l, s => by show l.foldl g (g s a) = s; rw [h s a]; exact foldl_const g h l s

theorem cnt_zero (n b c : Nat) (hb : b = 2) (hc : r16 c = Z16.r8 ∨ r16 c = Z16.r0) (hn : bad (r16 n) = true) :
    cnt n b c = 0 := by
  subst hb
  unfold cnt
  apply foldl_const; intro s x
  apply foldl_const; intro s y
  apply foldl_const; intro s z
  exact ite_no _ _ _ (never n x y z c hc hn)

/-- **A(n) = 2B(n) = 0** for odd n ≡ 5, 7 (mod 8). -/
theorem a_eq_2b (n : Nat) (hodd : n % 2 = 1) (hn : bad (r16 n) = true) : tA n = 2 * tB n := by
  unfold tA tB
  rw [ite_eq_left hodd, ite_eq_left hodd, cnt_zero n 2 8 rfl (Or.inl rfl) hn, cnt_zero n 2 32 rfl (Or.inr rfl) hn]

def bad4 : Z16 → Bool
  | Z16.r0 => false | Z16.r1 => false | Z16.r2 => false | Z16.r3 => true
  | Z16.r4 => false | Z16.r5 => false | Z16.r6 => false | Z16.r7 => true
  | Z16.r8 => false | Z16.r9 => false | Z16.r10 => false | Z16.r11 => true
  | Z16.r12 => false | Z16.r13 => false | Z16.r14 => false | Z16.r15 => true

set_option maxHeartbeats 0 in
theorem form_tbl4 (c : Z16) (hc : c = Z16.r8 ∨ c = Z16.r0) (x y z : Z16) :
    bad4 (((x.mul x).add (Z16.r4.mul (y.mul y))).add (c.mul (z.mul z))) = false := by
  cases hc with
  | inl h => subst h; cases x <;> cases y <;> cases z <;> rfl
  | inr h => subst h; cases x <;> cases y <;> cases z <;> rfl

theorem never4 (m x y z c : Nat) (hc : r16 c = Z16.r8 ∨ r16 c = Z16.r0) (hm : bad4 (r16 m) = true) :
    x * x + 4 * (y * y) + c * (z * z) ≠ m := by
  intro e
  have k := congrArg r16 e
  rw [r16_add, r16_add, r16_mul, r16_mul, r16_mul, r16_mul, r16_mul] at k
  have t := form_tbl4 (r16 c) hc (r16 x) (r16 y) (r16 z)
  rw [show Z16.r4 = r16 4 from rfl, k, hm] at t
  exact Bool.noConfusion t

theorem cnt_zero4 (m c : Nat) (hc : r16 c = Z16.r8 ∨ r16 c = Z16.r0) (hm : bad4 (r16 m) = true) :
    cnt m 4 c = 0 := by
  unfold cnt
  apply foldl_const; intro s x
  apply foldl_const; intro s y
  apply foldl_const; intro s z
  exact ite_no _ _ _ (never4 m x y z c hc hm)

/-- **A(n) = 2B(n) = 0** for even n = 2m with m ≡ 3 (mod 4)  (n ≡ 6 mod 8). -/
theorem a_eq_2b_even (n : Nat) (hev : ¬ n % 2 = 1) (hm : bad4 (r16 (n / 2)) = true) : tA n = 2 * tB n := by
  unfold tA tB
  rw [ite_no _ _ _ hev, ite_no _ _ _ hev, cnt_zero4 (n / 2) 8 (Or.inl rfl) hm, cnt_zero4 (n / 2) 32 (Or.inr rfl) hm]

end TunnellMod8

#print axioms TunnellMod8.form_tbl
#print axioms TunnellMod8.never
#print axioms TunnellMod8.foldl_const
#print axioms TunnellMod8.cnt_zero
#print axioms TunnellMod8.a_eq_2b
#print axioms TunnellMod8.form_tbl4
#print axioms TunnellMod8.never4
#print axioms TunnellMod8.cnt_zero4
#print axioms TunnellMod8.a_eq_2b_even

bsd/VLin.lean

lean_all/bsd/VLin.lean

/-
  bsd/VLin.lean — T39 (goal G289e-7a): the classes in V are determined by their first halves through the linear map
      L(s, a, b, c, d) = (s, s ⊕ b, s ⊕ a ⊕ b, s ⊕ b ⊕ d, s ⊕ c ⊕ d)          (bsd/VSet)
  so the dependency of four first halves modulo E[2] (bsd/Dep4) lifts to the full 10-bit classes:
    inV_snd     inV β β′ ⇒ β′ = L β
    vsnd_xor    L (β ⊕ γ) = L β ⊕ L γ                      (all 2¹⁰ cases, rfl)
    vsnd_sum    L commutes with subset sums
    dep_V       for any four classes (β_j, β′_j) ∈ V some nonempty subset sum is
                ((s, t, t, t, t), (s, s ⊕ t, s, s, s)) = s·δ(T0) ⊕ t·δ(T1), a class of E[2]
  Lean 4 core plus bsd/Dep4.
-/
import bsd.Dep4

namespace VLin
open B5S Dep4

/-- the second half of a class in V -/
def vsnd (β : B5) : B5 :=
  ⟨β.s, Bool.xor β.s β.b, Bool.xor β.s (Bool.xor β.a β.b), Bool.xor β.s (Bool.xor β.b β.d),
    Bool.xor β.s (Bool.xor β.c β.d)⟩

theorem inV_snd (β β' : B5)
    (h : VSet.inV β.s β.a β.b β.c β.d β'.s β'.a β'.b β'.c β'.d = true) : β' = vsnd β := by
  have h4 := and_left h
  have hd := beqB_eq (and_right h)
  have h3 := and_left h4
  have hc := beqB_eq (and_right h4)
  have h2 := and_left h3
  have hb := beqB_eq (and_right h3)
  have hs := beqB_eq (and_left h2)
  have ha := beqB_eq (and_right h2)
  calc β' = ⟨β'.s, β'.a, β'.b, β'.c, β'.d⟩ := rfl
    _ = ⟨β.s, xor β.s β.b, xor β.s (xor β.a β.b), xor β.s (xor β.b β.d), xor β.s (xor β.c β.d)⟩ := by
        rw [hs, ha, hb, hc, hd]
    _ = vsnd β := rfl

def fld5 (β γ : B5) : Bool :=
  VSet.beqB β.s γ.s && VSet.beqB β.a γ.a && VSet.beqB β.b γ.b && VSet.beqB β.c γ.c && VSet.beqB β.d γ.d

theorem eq_of_fld5 (β γ : B5) (h : fld5 β γ = true) : β = γ := by
  have h4 := and_left h
  have hd := beqB_eq (and_right h)
  have h3 := and_left h4
  have hc := beqB_eq (and_right h4)
  have h2 := and_left h3
  have hb := beqB_eq (and_right h3)
  have hs := beqB_eq (and_left h2)
  have ha := beqB_eq (and_right h2)
  calc β = ⟨β.s, β.a, β.b, β.c, β.d⟩ := rfl
    _ = ⟨γ.s, γ.a, γ.b, γ.c, γ.d⟩ := by rw [hs, ha, hb, hc, hd]

def xorChk : Bool :=
  VSet.all2 fun s1 => VSet.all2 fun a1 => VSet.all2 fun b1 => VSet.all2 fun c1 => VSet.all2 fun d1 =>
  VSet.all2 fun s2 => VSet.all2 fun a2 => VSet.all2 fun b2 => VSet.all2 fun c2 => VSet.all2 fun d2 =>
    fld5 (vsnd ((⟨s1, a1, b1, c1, d1⟩ : B5).xor ⟨s2, a2, b2, c2, d2⟩))
      ((vsnd ⟨s1, a1, b1, c1, d1⟩).xor (vsnd ⟨s2, a2, b2, c2, d2⟩))

theorem xorChk_true : xorChk = true := rfl

/-- L is linear -/
theorem vsnd_xor (β γ : B5) : vsnd (β.xor γ) = (vsnd β).xor (vsnd γ) := by
  cases β with
  | mk s1 a1 b1 c1 d1 =>
    cases γ with
    | mk s2 a2 b2 c2 d2 =>
      exact eq_of_fld5 _ _ (all2_app _ (all2_app _ (all2_app _ (all2_app _ (all2_app _ (all2_app _ (all2_app _
        (all2_app _ (all2_app _ (all2_app _ xorChk_true s1) a1) b1) c1) d1) s2) a2) b2) c2) d2)

theorem vsnd_sel (e : Bool) (β : B5) : vsnd (selB e β) = selB e (vsnd β) := by
  cases e with
  | true => rfl
  | false => rfl

theorem vsnd_sum (c : E4) (β1 β2 β3 β4 : B5) :
    vsnd (sum4 c β1 β2 β3 β4) = sum4 c (vsnd β1) (vsnd β2) (vsnd β3) (vsnd β4) := by
  unfold sum4
  rw [vsnd_xor, vsnd_xor, vsnd_xor, vsnd_sel, vsnd_sel, vsnd_sel, vsnd_sel]

theorem vsnd_tor (s t : Bool) : vsnd ⟨s, t, t, t, t⟩ = ⟨s, Bool.xor s t, s, s, s⟩ := by
  cases s <;> cases t <;> rfl

/-- **Four classes in V are dependent modulo the classes of E[2].** -/
theorem dep_V (β1 β2 β3 β4 β1' β2' β3' β4' : B5)
    (h1 : VSet.inV β1.s β1.a β1.b β1.c β1.d β1'.s β1'.a β1'.b β1'.c β1'.d = true)
    (h2 : VSet.inV β2.s β2.a β2.b β2.c β2.d β2'.s β2'.a β2'.b β2'.c β2'.d = true)
    (h3 : VSet.inV β3.s β3.a β3.b β3.c β3.d β3'.s β3'.a β3'.b β3'.c β3'.d = true)
    (h4 : VSet.inV β4.s β4.a β4.b β4.c β4.d β4'.s β4'.a β4'.b β4'.c β4'.d = true) :
    ∃ c : E4, nz c = true ∧ ∃ s t : Bool,
      sum4 c β1 β2 β3 β4 = ⟨s, t, t, t, t⟩ ∧ sum4 c β1' β2' β3' β4' = ⟨s, Bool.xor s t, s, s, s⟩ := by
  obtain ⟨c, hn, s, t, hst⟩ := dep_B5 β1 β2 β3 β4
  refine ⟨c, hn, s, t, hst, ?_⟩
  rw [inV_snd β1 β1' h1, inV_snd β2 β2' h2, inV_snd β3 β3' h3, inV_snd β4 β4' h4, ← vsnd_sum, hst]
  exact vsnd_tor s t

end VLin

#print axioms VLin.inV_snd
#print axioms VLin.eq_of_fld5
#print axioms VLin.xorChk_true
#print axioms VLin.vsnd_xor
#print axioms VLin.vsnd_sel
#print axioms VLin.vsnd_sum
#print axioms VLin.vsnd_tor
#print axioms VLin.dep_V

bsd/VList.lean

lean_all/bsd/VList.lean

/-
  bsd/VList.lean — T39 (goal G289f-4h-5e2, tool): a set V of classes given as an explicit list of pairs, and the
  properties used by DepGraph.dep_graph proved from Boolean checks over the list (rfl at each instance):
    any_ex, all_app        List.any / List.all by hand (no decidability instances)
    inVL                   membership, with the four-bit guard (so V-membership gives well-formed classes)
    mem_of_inVL, wf_of_inVL
    cl_chk, cl_of_chk      closure under ⊕ from a check over all pairs of the list
    fn_chk, fn_of_chk      "graph of a function" from a check over all pairs of the list
  Lean 4 core plus bsd/TP1254 (beqB, beqL, beqCL), bsd/Clear1254 (beqCL_eq), bsd/DepGraph.
-/
import bsd.Clear1254
import bsd.DepGraph

namespace VList
open PtClsN TP1254 Clear1254

theorem any_ex {α : Type} (f : α → Bool) : ∀ l : List α, l.any f = true → ∃ x, x ∈ l ∧ f x = true
  | [], h => Bool.noConfusion h
  | a :: l, h => by
    cases hf : f a with
    | true => exact ⟨a, List.Mem.head l, hf⟩
    | false =>
      have h' : l.any f = true := by
        have e : (a :: l).any f = (f a || l.any f) := rfl
        rw [e, hf] at h; exact h
      obtain ⟨x, hx, fx⟩ := any_ex f l h'
      exact ⟨x, List.Mem.tail a hx, fx⟩

theorem all_app {α : Type} (f : α → Bool) : ∀ l : List α, l.all f = true → ∀ x, x ∈ l → f x = true
  | [], _, _, hx => nomatch hx
  | a :: l, h, x, hx => by
    have e : (a :: l).all f = (f a && l.all f) := rfl
    rw [e] at h
    cases hx with
    | head => exact SqUniqueL.and_l h
    | tail _ hx' => exact all_app f l (SqUniqueL.and_r h) x hx'

def is4 : List Bool → Bool
  | [_, _, _, _] => true
  | [] => false
  | [_] => false
  | [_, _] => false
  | [_, _, _] => false
  | _ :: _ :: _ :: _ :: _ :: _ => false

theorem is4_len : ∀ l : List Bool, is4 l = true → l.length = 4
  | [_, _, _, _], _ => rfl
  | [], h => Bool.noConfusion h
  | [_], h => Bool.noConfusion h
  | [_, _], h => Bool.noConfusion h
  | [_, _, _], h => Bool.noConfusion h
  | _ :: _ :: _ :: _ :: _ :: _, h => Bool.noConfusion h

def inVL (V : List (CL × CL)) (β β' : CL) : Bool :=
  (is4 β.b && is4 β'.b) && V.any (fun p => beqCL p.1 β && beqCL p.2 β')

theorem mem_of_inVL (V : List (CL × CL)) (β β' : CL) (h : inVL V β β' = true) : (β, β') ∈ V := by
  obtain ⟨q, hq, hb⟩ := any_ex _ V (SqUniqueL.and_r h)
  have e1 := beqCL_eq _ _ (SqUniqueL.and_l hb)
  have e2 := beqCL_eq _ _ (SqUniqueL.and_r hb)
  cases q with
  | mk a b => rw [← e1, ← e2]; exact hq

theorem wf_of_inVL (ps : List Nat) (hl : ps.length = 4) (V : List (CL × CL)) (β β' : CL) (h : inVL V β β' = true) :
    WF ps β ∧ WF ps β' :=
  ⟨(is4_len _ (SqUniqueL.and_l (SqUniqueL.and_l h))).trans hl.symm,
   (is4_len _ (SqUniqueL.and_r (SqUniqueL.and_l h))).trans hl.symm⟩

def cl_chk (V : List (CL × CL)) : Bool := V.all (fun p => V.all (fun q => inVL V (cxor p.1 q.1) (cxor p.2 q.2)))

theorem cl_of_chk (V : List (CL × CL)) (hc : cl_chk V = true) (β β' γ γ' : CL) (h1 : inVL V β β' = true)
    (h2 : inVL V γ γ' = true) : inVL V (cxor β γ) (cxor β' γ') = true :=
  all_app _ V (all_app _ V hc _ (mem_of_inVL V β β' h1)) _ (mem_of_inVL V γ γ' h2)

def fn_chk (V : List (CL × CL)) : Bool := V.all (fun p => V.all (fun q => !(beqCL p.1 q.1) || beqCL p.2 q.2))

theorem fn_of_chk (V : List (CL × CL)) (hc : fn_chk V = true) (β β1 β2 : CL) (h1 : inVL V β β1 = true)
    (h2 : inVL V β β2 = true) : β1 = β2 := by
  have h := all_app _ V (all_app _ V hc _ (mem_of_inVL V β β1 h1)) _ (mem_of_inVL V β β2 h2)
  have hb : beqCL β β = true := by
    cases β with
    | mk s b =>
      have r : ∀ l : List Bool, beqL l l = true := by
        intro l
        induction l with
        | nil => rfl
        | cons a l ih => cases a <;> exact ih
      cases s <;> exact r b
  have h' : (!(beqCL β β) || beqCL β1 β2) = true := h
  rw [hb] at h'
  exact beqCL_eq _ _ h'

end VList

#print axioms VList.any_ex
#print axioms VList.all_app
#print axioms VList.is4_len
#print axioms VList.mem_of_inVL
#print axioms VList.wf_of_inVL
#print axioms VList.cl_of_chk
#print axioms VList.fn_of_chk

bsd/VSet.lean

lean_all/bsd/VSet.lean

/-
  bsd/VSet.lean — T39 (goal G289e-6f): the subgroup V of descent classes of E_42486 spanned by G1, G2, G3 (Rank3Cert12)
  and E[2], in bits (sign, 2, 3, 73, 97 of x; the same of x − n).  The first five bits are pivots:
      β ∈ V  ⇔  β₂ = L(β₁),   L(s, a, b, c, d) = (s, s ⊕ b, s ⊕ a ⊕ b, s ⊕ b ⊕ d, s ⊕ c ⊕ d)
  (computed by tools/g289e6_cosets.py; the generators' classes are checked here by rfl).
    inV          the Boolean test
    inV_gen      the classes of G1 (2, 3), G2 (3, 438), G3 (−219, −1), T0 (−1, −42486), T1 (42486, 2) are in V
    inV_xor      β, γ ∈ V ⇒ β ⊕ γ ∈ V; and inV (β ⊕ γ) = inV β for γ ∈ V (all 1024 β, rfl)
  Lean 4 core.
-/

namespace VSet

def beqB (x y : Bool) : Bool := (x && y) || (!x && !y)

def inV (s1 a1 b1 c1 d1 s2 a2 b2 c2 d2 : Bool) : Bool :=
  beqB s2 s1 && beqB a2 (xor s1 b1) && beqB b2 (xor s1 (xor a1 b1)) && beqB c2 (xor s1 (xor b1 d1))
    && beqB d2 (xor s1 (xor c1 d1))

theorem inV_G1 : inV false true false false false false false true false false = true := rfl
theorem inV_G2 : inV false false true false false false true true true false = true := rfl
theorem inV_G3 : inV true false true true false true false false false false = true := rfl
theorem inV_T0 : inV true false false false false true true true true true = true := rfl
theorem inV_T1 : inV false true true true true false true false false false = true := rfl

def all2 (f : Bool → Bool) : Bool := f true && f false

/-- translating by a class γ ∈ V does not change membership (checked for all 2¹⁰ classes β) -/
def xorInv (g1 g2 g3 g4 g5 g6 g7 g8 g9 g10 : Bool) : Bool :=
  all2 fun s1 => all2 fun a1 => all2 fun b1 => all2 fun c1 => all2 fun d1 =>
  all2 fun s2 => all2 fun a2 => all2 fun b2 => all2 fun c2 => all2 fun d2 =>
    beqB (inV (xor s1 g1) (xor a1 g2) (xor b1 g3) (xor c1 g4) (xor d1 g5)
              (xor s2 g6) (xor a2 g7) (xor b2 g8) (xor c2 g9) (xor d2 g10))
         (inV s1 a1 b1 c1 d1 s2 a2 b2 c2 d2)

theorem xorInv_G1 : xorInv false true false false false false false true false false = true := rfl
theorem xorInv_G2 : xorInv false false true false false false true true true false = true := rfl
theorem xorInv_G3 : xorInv true false true true false true false false false false = true := rfl
theorem xorInv_T0 : xorInv true false false false false true true true true true = true := rfl
theorem xorInv_T1 : xorInv false true true true true false true false false false = true := rfl

end VSet

#print axioms VSet.inV_G1
#print axioms VSet.inV_G2
#print axioms VSet.inV_G3
#print axioms VSet.inV_T0
#print axioms VSet.inV_T1
#print axioms VSet.xorInv_G1
#print axioms VSet.xorInv_G2
#print axioms VSet.xorInv_G3
#print axioms VSet.xorInv_T0
#print axioms VSet.xorInv_T1

bsd/Zeta2.lean

lean_all/bsd/Zeta2.lean

/-
  bsd/Zeta2.lean — T39 cycle 2: points over F_{p²} and the Frobenius polynomial.

  Lean 4 core plus BSDChain (cntG, hasseOK). Concrete counts by `rfl`.

  For good p the zeta function of E/F_p is (1 − aT + pT²)/((1 − T)(1 − pT)) with a = p + 1 − N₁,
  so N₂ = #E(F_{p²}) = p² + 1 − (a² − 2p) = (p + 1)² − a². At a multiplicative prime the
  numerator is 1 − aT (a = ±1) and N₂ = p² + 1 − a². These identities are where "placing points"
  (counting N₁, N₂) and "reading the L-factor" (the polynomial 1 − aT + pT²) meet; Hasse's bound
  |a| ≤ 2√p is the statement that this polynomial has complex roots of absolute value √p.

  F_{p²} = F_p[t]/(t² − c t − d) with (c, d) an irreducible choice per p; an element is u + v t.

  Proved here (rfl):
    zeta2_11a     11a, p = 2, 3, 5, 7, 13 (good): N₂ + a² = (p + 1)²;  p = 11 (bad): N₂ + a² = p² + 1
    zeta2_37a     37a, p = 2, 3, 5, 7, 11, 13 (good): N₂ + a² = (p + 1)²
-/
import BSDChain

namespace Zeta2
open BSDChain

structure F2 where
  u : Nat
  v : Nat

def F2.add (p : Nat) (x y : F2) : F2 := ⟨(x.u + y.u) % p, (x.v + y.v) % p⟩
def F2.mul (p c d : Nat) (x y : F2) : F2 :=
  ⟨(x.u * y.u + x.v * y.v * d) % p, (x.u * y.v + x.v * y.u + x.v * y.v * c) % p⟩
def F2.beq (x y : F2) : Bool := Nat.beq x.u y.u && Nat.beq x.v y.v

def elems (p : Nat) : List F2 :=
  (List.range p).flatMap (fun u => (List.range p).map (fun v => ⟨u, v⟩))

/-- Irreducible t² − c t − d over F_p, for the primes used. -/
def irr : Nat → Nat × Nat
  | 2 => (1, 1)
  | 3 => (0, 2)
  | 5 => (0, 2)
  | 7 => (0, 6)
  | 11 => (0, 10)
  | 13 => (0, 2)
  | _ => (0, 0)

/-- #E(F_{p²}) for y² + y + Q(x) = P(x), the point at infinity included. -/
def cnt2 (lhs rhs : (F2 → F2 → F2) → (F2 → F2 → F2) → F2 → F2 → F2) (p : Nat) : Nat :=
  let c := (irr p).1
  let d := (irr p).2
  let m := F2.mul p c d
  let a := F2.add p
  let E := elems p
  (E.flatMap (fun x => E.filter (fun y => F2.beq (lhs m a x y) (rhs m a x y)))).length + 1

/-- 11a: y² + y + x² = x³. -/
def L11 (m a : F2 → F2 → F2) (x y : F2) : F2 := a (a (m y y) y) (m x x)
def R11 (m _ : F2 → F2 → F2) (x _ : F2) : F2 := m (m x x) x
/-- 37a: y² + y + x = x³. -/
def L37 (m a : F2 → F2 → F2) (x y : F2) : F2 := a (a (m y y) y) x
def R37 (m _ : F2 → F2 → F2) (x _ : F2) : F2 := m (m x x) x

/-- a² from N₁ in ℕ: (p+1−N₁)² + (N₁−(p+1))². -/
def asq (p n1 : Nat) : Nat := (p + 1 - n1) * (p + 1 - n1) + (n1 - (p + 1)) * (n1 - (p + 1))

def goodOK (L R : Nat → Nat → Nat) (L2 R2 : (F2 → F2 → F2) → (F2 → F2 → F2) → F2 → F2 → F2)
    (p : Nat) : Bool :=
  Nat.beq (cnt2 L2 R2 p + asq p (cntG L R p + 1)) ((p + 1) * (p + 1))

def badOK (L R : Nat → Nat → Nat) (L2 R2 : (F2 → F2 → F2) → (F2 → F2 → F2) → F2 → F2 → F2)
    (p : Nat) : Bool :=
  Nat.beq (cnt2 L2 R2 p + asq p (cntG L R p + 1)) (p * p + 1)

set_option maxHeartbeats 0 in
set_option maxRecDepth 1000000 in
theorem zeta2_11a : [2, 3, 5, 7, 13].all (goodOK BSDChain.L11 BSDChain.R11 L11 R11) = true ∧
    badOK BSDChain.L11 BSDChain.R11 L11 R11 11 = true := ⟨rfl, rfl⟩

set_option maxHeartbeats 0 in
set_option maxRecDepth 1000000 in
theorem zeta2_37a : [2, 3, 5, 7, 11, 13].all (goodOK BSDChain.L37 BSDChain.R37 L37 R37) = true := rfl

end Zeta2

#print axioms Zeta2.zeta2_11a
#print axioms Zeta2.zeta2_37a

cert/A/n2/u6.lean

lean_all/cert/A/n2/u6.lean

include hD in
theorem u6core (a w : Nat) : O.le K.zero (K.add (K.mul (p2v K x a) (A1C.p3v K x (a + w + 1))) (K.neg (K.mul (p2v K x (a + w + 1)) (A1C.p3v K x a)))) := by
  cases a with
  | zero =>
    cases w with
    | zero =>
      rw [show 0 + 0 + 1 = 1 by lib_nat]
      show O.le K.zero (K.add (K.mul (Zs K x (3)) (K.add (Zs K x (3)) (Zs K x (5)))) (K.neg (K.mul (K.add (K.add K.zero (Zs K x (2))) (Zs K x (4))) (Zs K x (4)))))
      have d0 := (Dn K O x hD (2) (1) (2) (3) (4) (3) (4) (5) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (3)) (Zs K x (5))) (Zs K x (3))) (K.neg (K.mul (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (4))))))
      exact A1.nn_of_eq K O _ _ d0 (by lib_ring)
    | succ w =>
      cases w with
      | zero =>
        rw [show 0 + (0 + 1) + 1 = 2 by lib_nat]
        show O.le K.zero (K.add (K.mul (Zs K x (3)) (K.add (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (6)))) (K.neg (K.mul (K.add (K.add (Zs K x (1)) (Zs K x (3))) (Zs K x (5))) (Zs K x (4)))))
        have d0 := (Dn K O x hD (1) (2) (1) (2) (3) (3) (4) (5) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (3)) (Zs K x (5))) (Zs K x (2))) (K.neg (K.mul (K.add (Zs K x (1)) (Zs K x (3))) (Zs K x (4))))))
        have d1 := (Dn K O x hD (2) (2) (2) (3) (4) (4) (5) (6) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (4)) (Zs K x (6))) (Zs K x (3))) (K.neg (K.mul (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (5))))))
        exact A1.nn_of_eq K O _ _ (A1.nn_add K O _ _ d0 d1) (by lib_ring)
      | succ w =>
        cases w with
        | zero =>
          rw [show 0 + (0 + 1 + 1) + 1 = 3 by lib_nat]
          show O.le K.zero (K.add (K.mul (Zs K x (3)) (K.add (K.add (K.add (Zs K x (1)) (Zs K x (3))) (Zs K x (5))) (Zs K x (7)))) (K.neg (K.mul (K.add (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (6))) (Zs K x (4)))))
          have d0 := (Dn K O x hD (0) (3) (0) (1) (2) (3) (4) (5) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (3)) (Zs K x (5))) (Zs K x (1))) (K.neg (K.mul (K.add K.zero (Zs K x (2))) (Zs K x (4))))))
          have d1 := (Dn K O x hD (1) (3) (1) (2) (3) (4) (5) (6) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (4)) (Zs K x (6))) (Zs K x (2))) (K.neg (K.mul (K.add (Zs K x (1)) (Zs K x (3))) (Zs K x (5))))))
          have d2 := (Dn K O x hD (2) (1) (2) (3) (4) (3) (4) (5) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (3)) (Zs K x (5))) (Zs K x (3))) (K.neg (K.mul (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (4))))))
          have d3 := (Dn K O x hD (2) (3) (2) (3) (4) (5) (6) (7) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (5)) (Zs K x (7))) (Zs K x (3))) (K.neg (K.mul (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (6))))))
          exact A1.nn_of_eq K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ d0 d1) d2) d3) (by lib_ring)
        | succ w =>
          rw [show 0 + (w + 1 + 1 + 1) + 1 = w + 4 by lib_nat]
          show O.le K.zero (K.add (K.mul (Zs K x (3)) (K.add (K.add (K.add (Zs K x (w + 2)) (Zs K x (w + 4))) (Zs K x (w + 6))) (Zs K x (w + 8)))) (K.neg (K.mul (K.add (K.add (Zs K x (w + 3)) (Zs K x (w + 5))) (Zs K x (w + 7))) (Zs K x (4)))))
          have d0 := (Dn K O x hD (0) (w + 4) (0) (1) (2) (w + 4) (w + 5) (w + 6) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (w + 4)) (Zs K x (w + 6))) (Zs K x (1))) (K.neg (K.mul (K.add K.zero (Zs K x (2))) (Zs K x (w + 5))))))
          have d1 := (Dn K O x hD (1) (w + 2) (1) (2) (3) (w + 3) (w + 4) (w + 5) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (w + 3)) (Zs K x (w + 5))) (Zs K x (2))) (K.neg (K.mul (K.add (Zs K x (1)) (Zs K x (3))) (Zs K x (w + 4))))))
          have d2 := (Dn K O x hD (1) (w + 4) (1) (2) (3) (w + 5) (w + 6) (w + 7) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (w + 5)) (Zs K x (w + 7))) (Zs K x (2))) (K.neg (K.mul (K.add (Zs K x (1)) (Zs K x (3))) (Zs K x (w + 6))))))
          have d3 := (Dn K O x hD (2) (w) (2) (3) (4) (w + 2) (w + 3) (w + 4) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (w + 2)) (Zs K x (w + 4))) (Zs K x (3))) (K.neg (K.mul (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (w + 3))))))
          have d4 := (Dn K O x hD (2) (w + 2) (2) (3) (4) (w + 4) (w + 5) (w + 6) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (w + 4)) (Zs K x (w + 6))) (Zs K x (3))) (K.neg (K.mul (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (w + 5))))))
          have d5 := (Dn K O x hD (2) (w + 4) (2) (3) (4) (w + 6) (w + 7) (w + 8) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (w + 6)) (Zs K x (w + 8))) (Zs K x (3))) (K.neg (K.mul (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (w + 7))))))
          exact A1.nn_of_eq K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ d0 d1) d2) d3) d4) d5) (by lib_ring)
  | succ a =>
    cases a with
    | zero =>
      cases w with
      | zero =>
        rw [show 0 + 1 + 0 + 1 = 2 by lib_nat]
        show O.le K.zero (K.add (K.mul (K.add (K.add K.zero (Zs K x (2))) (Zs K x (4))) (K.add (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (6)))) (K.neg (K.mul (K.add (K.add (Zs K x (1)) (Zs K x (3))) (Zs K x (5))) (K.add (Zs K x (3)) (Zs K x (5))))))
        have d0 := (Dn K O x hD (1) (1) (1) (2) (3) (2) (3) (4) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (2))) (K.neg (K.mul (K.add (Zs K x (1)) (Zs K x (3))) (Zs K x (3))))))
        have d1 := (Dn K O x hD (1) (3) (1) (2) (3) (4) (5) (6) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (4)) (Zs K x (6))) (Zs K x (2))) (K.neg (K.mul (K.add (Zs K x (1)) (Zs K x (3))) (Zs K x (5))))))
        have d2 := (Dn K O x hD (3) (1) (3) (4) (5) (4) (5) (6) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (4)) (Zs K x (6))) (Zs K x (4))) (K.neg (K.mul (K.add (Zs K x (3)) (Zs K x (5))) (Zs K x (5))))))
        exact A1.nn_of_eq K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ d0 d1) d2) (by lib_ring)
      | succ w =>
        cases w with
        | zero =>
          rw [show 0 + 1 + (0 + 1) + 1 = 3 by lib_nat]
          show O.le K.zero (K.add (K.mul (K.add (K.add K.zero (Zs K x (2))) (Zs K x (4))) (K.add (K.add (K.add (Zs K x (1)) (Zs K x (3))) (Zs K x (5))) (Zs K x (7)))) (K.neg (K.mul (K.add (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (6))) (K.add (Zs K x (3)) (Zs K x (5))))))
          have d0 := (Dn K O x hD (0) (2) (0) (1) (2) (2) (3) (4) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (1))) (K.neg (K.mul (K.add K.zero (Zs K x (2))) (Zs K x (3))))))
          have d1 := (Dn K O x hD (0) (4) (0) (1) (2) (4) (5) (6) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (4)) (Zs K x (6))) (Zs K x (1))) (K.neg (K.mul (K.add K.zero (Zs K x (2))) (Zs K x (5))))))
          have d2 := (Dn K O x hD (1) (2) (1) (2) (3) (3) (4) (5) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (3)) (Zs K x (5))) (Zs K x (2))) (K.neg (K.mul (K.add (Zs K x (1)) (Zs K x (3))) (Zs K x (4))))))
          have d3 := (Dn K O x hD (1) (4) (1) (2) (3) (5) (6) (7) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (5)) (Zs K x (7))) (Zs K x (2))) (K.neg (K.mul (K.add (Zs K x (1)) (Zs K x (3))) (Zs K x (6))))))
          have d4 := (Dn K O x hD (2) (2) (2) (3) (4) (4) (5) (6) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (4)) (Zs K x (6))) (Zs K x (3))) (K.neg (K.mul (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (5))))))
          have d5 := (Dn K O x hD (3) (2) (3) (4) (5) (5) (6) (7) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (5)) (Zs K x (7))) (Zs K x (4))) (K.neg (K.mul (K.add (Zs K x (3)) (Zs K x (5))) (Zs K x (6))))))
          exact A1.nn_of_eq K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ d0 d1) d2) d3) d4) d5) (by lib_ring)
        | succ w =>
          cases w with
          | zero =>
            rw [show 0 + 1 + (0 + 1 + 1) + 1 = 4 by lib_nat]
            show O.le K.zero (K.add (K.mul (K.add (K.add K.zero (Zs K x (2))) (Zs K x (4))) (K.add (K.add (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (6))) (Zs K x (8)))) (K.neg (K.mul (K.add (K.add (Zs K x (3)) (Zs K x (5))) (Zs K x (7))) (K.add (Zs K x (3)) (Zs K x (5))))))
            have d0 := (Dn K O x hD (0) (3) (0) (1) (2) (3) (4) (5) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (3)) (Zs K x (5))) (Zs K x (1))) (K.neg (K.mul (K.add K.zero (Zs K x (2))) (Zs K x (4))))))
            have d1 := (Dn K O x hD (0) (5) (0) (1) (2) (5) (6) (7) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (5)) (Zs K x (7))) (Zs K x (1))) (K.neg (K.mul (K.add K.zero (Zs K x (2))) (Zs K x (6))))))
            have d2 := (Dn K O x hD (1) (1) (1) (2) (3) (2) (3) (4) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (2))) (K.neg (K.mul (K.add (Zs K x (1)) (Zs K x (3))) (Zs K x (3))))))
            have d3 := (Dn K O x hD (1) (3) (1) (2) (3) (4) (5) (6) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (4)) (Zs K x (6))) (Zs K x (2))) (K.neg (K.mul (K.add (Zs K x (1)) (Zs K x (3))) (Zs K x (5))))))
            have d4 := (Dn K O x hD (1) (3) (1) (2) (3) (4) (5) (6) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (4)) (Zs K x (6))) (Zs K x (2))) (K.neg (K.mul (K.add (Zs K x (1)) (Zs K x (3))) (Zs K x (5))))))
            have d5 := (Dn K O x hD (1) (5) (1) (2) (3) (6) (7) (8) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (6)) (Zs K x (8))) (Zs K x (2))) (K.neg (K.mul (K.add (Zs K x (1)) (Zs K x (3))) (Zs K x (7))))))
            have d6 := (Dn K O x hD (2) (3) (2) (3) (4) (5) (6) (7) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (5)) (Zs K x (7))) (Zs K x (3))) (K.neg (K.mul (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (6))))))
            have d7 := (Dn K O x hD (3) (1) (3) (4) (5) (4) (5) (6) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (4)) (Zs K x (6))) (Zs K x (4))) (K.neg (K.mul (K.add (Zs K x (3)) (Zs K x (5))) (Zs K x (5))))))
            have d8 := (Dn K O x hD (3) (3) (3) (4) (5) (6) (7) (8) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (6)) (Zs K x (8))) (Zs K x (4))) (K.neg (K.mul (K.add (Zs K x (3)) (Zs K x (5))) (Zs K x (7))))))
            exact A1.nn_of_eq K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ d0 d1) d2) d3) d4) d5) d6) d7) d8) (by lib_ring)
          | succ w =>
            rw [show 0 + 1 + (w + 1 + 1 + 1) + 1 = w + 5 by lib_nat]
            show O.le K.zero (K.add (K.mul (K.add (K.add K.zero (Zs K x (2))) (Zs K x (4))) (K.add (K.add (K.add (Zs K x (w + 3)) (Zs K x (w + 5))) (Zs K x (w + 7))) (Zs K x (w + 9)))) (K.neg (K.mul (K.add (K.add (Zs K x (w + 4)) (Zs K x (w + 6))) (Zs K x (w + 8))) (K.add (Zs K x (3)) (Zs K x (5))))))
            have d0 := (Dn K O x hD (0) (w + 4) (0) (1) (2) (w + 4) (w + 5) (w + 6) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (w + 4)) (Zs K x (w + 6))) (Zs K x (1))) (K.neg (K.mul (K.add K.zero (Zs K x (2))) (Zs K x (w + 5))))))
            have d1 := (Dn K O x hD (0) (w + 6) (0) (1) (2) (w + 6) (w + 7) (w + 8) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (w + 6)) (Zs K x (w + 8))) (Zs K x (1))) (K.neg (K.mul (K.add K.zero (Zs K x (2))) (Zs K x (w + 7))))))
            have d2 := (Dn K O x hD (1) (w + 2) (1) (2) (3) (w + 3) (w + 4) (w + 5) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (w + 3)) (Zs K x (w + 5))) (Zs K x (2))) (K.neg (K.mul (K.add (Zs K x (1)) (Zs K x (3))) (Zs K x (w + 4))))))
            have d3 := (Dn K O x hD (1) (w + 4) (1) (2) (3) (w + 5) (w + 6) (w + 7) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (w + 5)) (Zs K x (w + 7))) (Zs K x (2))) (K.neg (K.mul (K.add (Zs K x (1)) (Zs K x (3))) (Zs K x (w + 6))))))
            have d4 := (Dn K O x hD (1) (w + 4) (1) (2) (3) (w + 5) (w + 6) (w + 7) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (w + 5)) (Zs K x (w + 7))) (Zs K x (2))) (K.neg (K.mul (K.add (Zs K x (1)) (Zs K x (3))) (Zs K x (w + 6))))))
            have d5 := (Dn K O x hD (1) (w + 6) (1) (2) (3) (w + 7) (w + 8) (w + 9) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (w + 7)) (Zs K x (w + 9))) (Zs K x (2))) (K.neg (K.mul (K.add (Zs K x (1)) (Zs K x (3))) (Zs K x (w + 8))))))
            have d6 := (Dn K O x hD (2) (w + 2) (2) (3) (4) (w + 4) (w + 5) (w + 6) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (w + 4)) (Zs K x (w + 6))) (Zs K x (3))) (K.neg (K.mul (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (w + 5))))))
            have d7 := (Dn K O x hD (2) (w + 4) (2) (3) (4) (w + 6) (w + 7) (w + 8) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (w + 6)) (Zs K x (w + 8))) (Zs K x (3))) (K.neg (K.mul (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (w + 7))))))
            have d8 := (Dn K O x hD (3) (w) (3) (4) (5) (w + 3) (w + 4) (w + 5) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (w + 3)) (Zs K x (w + 5))) (Zs K x (4))) (K.neg (K.mul (K.add (Zs K x (3)) (Zs K x (5))) (Zs K x (w + 4))))))
            have d9 := (Dn K O x hD (3) (w + 2) (3) (4) (5) (w + 5) (w + 6) (w + 7) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (w + 5)) (Zs K x (w + 7))) (Zs K x (4))) (K.neg (K.mul (K.add (Zs K x (3)) (Zs K x (5))) (Zs K x (w + 6))))))
            have d10 := (Dn K O x hD (3) (w + 4) (3) (4) (5) (w + 7) (w + 8) (w + 9) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (w + 7)) (Zs K x (w + 9))) (Zs K x (4))) (K.neg (K.mul (K.add (Zs K x (3)) (Zs K x (5))) (Zs K x (w + 8))))))
            exact A1.nn_of_eq K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ d0 d1) d2) d3) d4) d5) d6) d7) d8) d9) d10) (by lib_ring)
    | succ a =>
      cases a with
      | zero =>
        cases w with
        | zero =>
          rw [show 0 + 1 + 1 + 0 + 1 = 3 by lib_nat]
          show O.le K.zero (K.add (K.mul (K.add (K.add (Zs K x (1)) (Zs K x (3))) (Zs K x (5))) (K.add (K.add (K.add (Zs K x (1)) (Zs K x (3))) (Zs K x (5))) (Zs K x (7)))) (K.neg (K.mul (K.add (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (6))) (K.add (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (6))))))
          have d0 := (Dn K O x hD (0) (1) (0) (1) (2) (1) (2) (3) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (1)) (Zs K x (3))) (Zs K x (1))) (K.neg (K.mul (K.add K.zero (Zs K x (2))) (Zs K x (2))))))
          have d1 := (Dn K O x hD (0) (3) (0) (1) (2) (3) (4) (5) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (3)) (Zs K x (5))) (Zs K x (1))) (K.neg (K.mul (K.add K.zero (Zs K x (2))) (Zs K x (4))))))
          have d2 := (Dn K O x hD (0) (5) (0) (1) (2) (5) (6) (7) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (5)) (Zs K x (7))) (Zs K x (1))) (K.neg (K.mul (K.add K.zero (Zs K x (2))) (Zs K x (6))))))
          have d3 := (Dn K O x hD (2) (1) (2) (3) (4) (3) (4) (5) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (3)) (Zs K x (5))) (Zs K x (3))) (K.neg (K.mul (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (4))))))
          have d4 := (Dn K O x hD (2) (3) (2) (3) (4) (5) (6) (7) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (5)) (Zs K x (7))) (Zs K x (3))) (K.neg (K.mul (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (6))))))
          have d5 := (Dn K O x hD (4) (1) (4) (5) (6) (5) (6) (7) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (5)) (Zs K x (7))) (Zs K x (5))) (K.neg (K.mul (K.add (Zs K x (4)) (Zs K x (6))) (Zs K x (6))))))
          exact A1.nn_of_eq K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ d0 d1) d2) d3) d4) d5) (by lib_ring)
        | succ w =>
          cases w with
          | zero =>
            rw [show 0 + 1 + 1 + (0 + 1) + 1 = 4 by lib_nat]
            show O.le K.zero (K.add (K.mul (K.add (K.add (Zs K x (1)) (Zs K x (3))) (Zs K x (5))) (K.add (K.add (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (6))) (Zs K x (8)))) (K.neg (K.mul (K.add (K.add (Zs K x (3)) (Zs K x (5))) (Zs K x (7))) (K.add (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (6))))))
            have d0 := (Dn K O x hD (0) (2) (0) (1) (2) (2) (3) (4) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (1))) (K.neg (K.mul (K.add K.zero (Zs K x (2))) (Zs K x (3))))))
            have d1 := (Dn K O x hD (0) (4) (0) (1) (2) (4) (5) (6) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (4)) (Zs K x (6))) (Zs K x (1))) (K.neg (K.mul (K.add K.zero (Zs K x (2))) (Zs K x (5))))))
            have d2 := (Dn K O x hD (0) (6) (0) (1) (2) (6) (7) (8) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (6)) (Zs K x (8))) (Zs K x (1))) (K.neg (K.mul (K.add K.zero (Zs K x (2))) (Zs K x (7))))))
            have d3 := (Dn K O x hD (1) (2) (1) (2) (3) (3) (4) (5) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (3)) (Zs K x (5))) (Zs K x (2))) (K.neg (K.mul (K.add (Zs K x (1)) (Zs K x (3))) (Zs K x (4))))))
            have d4 := (Dn K O x hD (1) (4) (1) (2) (3) (5) (6) (7) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (5)) (Zs K x (7))) (Zs K x (2))) (K.neg (K.mul (K.add (Zs K x (1)) (Zs K x (3))) (Zs K x (6))))))
            have d5 := (Dn K O x hD (2) (2) (2) (3) (4) (4) (5) (6) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (4)) (Zs K x (6))) (Zs K x (3))) (K.neg (K.mul (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (5))))))
            have d6 := (Dn K O x hD (2) (4) (2) (3) (4) (6) (7) (8) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (6)) (Zs K x (8))) (Zs K x (3))) (K.neg (K.mul (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (7))))))
            have d7 := (Dn K O x hD (3) (2) (3) (4) (5) (5) (6) (7) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (5)) (Zs K x (7))) (Zs K x (4))) (K.neg (K.mul (K.add (Zs K x (3)) (Zs K x (5))) (Zs K x (6))))))
            have d8 := (Dn K O x hD (4) (2) (4) (5) (6) (6) (7) (8) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (6)) (Zs K x (8))) (Zs K x (5))) (K.neg (K.mul (K.add (Zs K x (4)) (Zs K x (6))) (Zs K x (7))))))
            exact A1.nn_of_eq K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ d0 d1) d2) d3) d4) d5) d6) d7) d8) (by lib_ring)
          | succ w =>
            cases w with
            | zero =>
              rw [show 0 + 1 + 1 + (0 + 1 + 1) + 1 = 5 by lib_nat]
              show O.le K.zero (K.add (K.mul (K.add (K.add (Zs K x (1)) (Zs K x (3))) (Zs K x (5))) (K.add (K.add (K.add (Zs K x (3)) (Zs K x (5))) (Zs K x (7))) (Zs K x (9)))) (K.neg (K.mul (K.add (K.add (Zs K x (4)) (Zs K x (6))) (Zs K x (8))) (K.add (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (6))))))
              have d0 := (Dn K O x hD (0) (3) (0) (1) (2) (3) (4) (5) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (3)) (Zs K x (5))) (Zs K x (1))) (K.neg (K.mul (K.add K.zero (Zs K x (2))) (Zs K x (4))))))
              have d1 := (Dn K O x hD (0) (5) (0) (1) (2) (5) (6) (7) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (5)) (Zs K x (7))) (Zs K x (1))) (K.neg (K.mul (K.add K.zero (Zs K x (2))) (Zs K x (6))))))
              have d2 := (Dn K O x hD (0) (7) (0) (1) (2) (7) (8) (9) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (7)) (Zs K x (9))) (Zs K x (1))) (K.neg (K.mul (K.add K.zero (Zs K x (2))) (Zs K x (8))))))
              have d3 := (Dn K O x hD (1) (3) (1) (2) (3) (4) (5) (6) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (4)) (Zs K x (6))) (Zs K x (2))) (K.neg (K.mul (K.add (Zs K x (1)) (Zs K x (3))) (Zs K x (5))))))
              have d4 := (Dn K O x hD (1) (5) (1) (2) (3) (6) (7) (8) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (6)) (Zs K x (8))) (Zs K x (2))) (K.neg (K.mul (K.add (Zs K x (1)) (Zs K x (3))) (Zs K x (7))))))
              have d5 := (Dn K O x hD (2) (1) (2) (3) (4) (3) (4) (5) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (3)) (Zs K x (5))) (Zs K x (3))) (K.neg (K.mul (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (4))))))
              have d6 := (Dn K O x hD (2) (3) (2) (3) (4) (5) (6) (7) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (5)) (Zs K x (7))) (Zs K x (3))) (K.neg (K.mul (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (6))))))
              have d7 := (Dn K O x hD (2) (3) (2) (3) (4) (5) (6) (7) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (5)) (Zs K x (7))) (Zs K x (3))) (K.neg (K.mul (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (6))))))
              have d8 := (Dn K O x hD (2) (5) (2) (3) (4) (7) (8) (9) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (7)) (Zs K x (9))) (Zs K x (3))) (K.neg (K.mul (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (8))))))
              have d9 := (Dn K O x hD (3) (3) (3) (4) (5) (6) (7) (8) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (6)) (Zs K x (8))) (Zs K x (4))) (K.neg (K.mul (K.add (Zs K x (3)) (Zs K x (5))) (Zs K x (7))))))
              have d10 := (Dn K O x hD (4) (1) (4) (5) (6) (5) (6) (7) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (5)) (Zs K x (7))) (Zs K x (5))) (K.neg (K.mul (K.add (Zs K x (4)) (Zs K x (6))) (Zs K x (6))))))
              have d11 := (Dn K O x hD (4) (3) (4) (5) (6) (7) (8) (9) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (7)) (Zs K x (9))) (Zs K x (5))) (K.neg (K.mul (K.add (Zs K x (4)) (Zs K x (6))) (Zs K x (8))))))
              exact A1.nn_of_eq K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ d0 d1) d2) d3) d4) d5) d6) d7) d8) d9) d10) d11) (by lib_ring)
            | succ w =>
              rw [show 0 + 1 + 1 + (w + 1 + 1 + 1) + 1 = w + 6 by lib_nat]
              show O.le K.zero (K.add (K.mul (K.add (K.add (Zs K x (1)) (Zs K x (3))) (Zs K x (5))) (K.add (K.add (K.add (Zs K x (w + 4)) (Zs K x (w + 6))) (Zs K x (w + 8))) (Zs K x (w + 10)))) (K.neg (K.mul (K.add (K.add (Zs K x (w + 5)) (Zs K x (w + 7))) (Zs K x (w + 9))) (K.add (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (6))))))
              have d0 := (Dn K O x hD (0) (w + 4) (0) (1) (2) (w + 4) (w + 5) (w + 6) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (w + 4)) (Zs K x (w + 6))) (Zs K x (1))) (K.neg (K.mul (K.add K.zero (Zs K x (2))) (Zs K x (w + 5))))))
              have d1 := (Dn K O x hD (0) (w + 6) (0) (1) (2) (w + 6) (w + 7) (w + 8) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (w + 6)) (Zs K x (w + 8))) (Zs K x (1))) (K.neg (K.mul (K.add K.zero (Zs K x (2))) (Zs K x (w + 7))))))
              have d2 := (Dn K O x hD (0) (w + 8) (0) (1) (2) (w + 8) (w + 9) (w + 10) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (w + 8)) (Zs K x (w + 10))) (Zs K x (1))) (K.neg (K.mul (K.add K.zero (Zs K x (2))) (Zs K x (w + 9))))))
              have d3 := (Dn K O x hD (1) (w + 4) (1) (2) (3) (w + 5) (w + 6) (w + 7) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (w + 5)) (Zs K x (w + 7))) (Zs K x (2))) (K.neg (K.mul (K.add (Zs K x (1)) (Zs K x (3))) (Zs K x (w + 6))))))
              have d4 := (Dn K O x hD (1) (w + 6) (1) (2) (3) (w + 7) (w + 8) (w + 9) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (w + 7)) (Zs K x (w + 9))) (Zs K x (2))) (K.neg (K.mul (K.add (Zs K x (1)) (Zs K x (3))) (Zs K x (w + 8))))))
              have d5 := (Dn K O x hD (2) (w + 2) (2) (3) (4) (w + 4) (w + 5) (w + 6) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (w + 4)) (Zs K x (w + 6))) (Zs K x (3))) (K.neg (K.mul (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (w + 5))))))
              have d6 := (Dn K O x hD (2) (w + 4) (2) (3) (4) (w + 6) (w + 7) (w + 8) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (w + 6)) (Zs K x (w + 8))) (Zs K x (3))) (K.neg (K.mul (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (w + 7))))))
              have d7 := (Dn K O x hD (2) (w + 4) (2) (3) (4) (w + 6) (w + 7) (w + 8) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (w + 6)) (Zs K x (w + 8))) (Zs K x (3))) (K.neg (K.mul (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (w + 7))))))
              have d8 := (Dn K O x hD (2) (w + 6) (2) (3) (4) (w + 8) (w + 9) (w + 10) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (w + 8)) (Zs K x (w + 10))) (Zs K x (3))) (K.neg (K.mul (K.add (Zs K x (2)) (Zs K x (4))) (Zs K x (w + 9))))))
              have d9 := (Dn K O x hD (3) (w + 2) (3) (4) (5) (w + 5) (w + 6) (w + 7) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (w + 5)) (Zs K x (w + 7))) (Zs K x (4))) (K.neg (K.mul (K.add (Zs K x (3)) (Zs K x (5))) (Zs K x (w + 6))))))
              have d10 := (Dn K O x hD (3) (w + 4) (3) (4) (5) (w + 7) (w + 8) (w + 9) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (w + 7)) (Zs K x (w + 9))) (Zs K x (4))) (K.neg (K.mul (K.add (Zs K x (3)) (Zs K x (5))) (Zs K x (w + 8))))))
              have d11 := (Dn K O x hD (4) (w) (4) (5) (6) (w + 4) (w + 5) (w + 6) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (w + 4)) (Zs K x (w + 6))) (Zs K x (5))) (K.neg (K.mul (K.add (Zs K x (4)) (Zs K x (6))) (Zs K x (w + 5))))))
              have d12 := (Dn K O x hD (4) (w + 2) (4) (5) (6) (w + 6) (w + 7) (w + 8) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (w + 6)) (Zs K x (w + 8))) (Zs K x (5))) (K.neg (K.mul (K.add (Zs K x (4)) (Zs K x (6))) (Zs K x (w + 7))))))
              have d13 := (Dn K O x hD (4) (w + 4) (4) (5) (6) (w + 8) (w + 9) (w + 10) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (w + 8)) (Zs K x (w + 10))) (Zs K x (5))) (K.neg (K.mul (K.add (Zs K x (4)) (Zs K x (6))) (Zs K x (w + 9))))))
              exact A1.nn_of_eq K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ d0 d1) d2) d3) d4) d5) d6) d7) d8) d9) d10) d11) d12) d13) (by lib_ring)
      | succ a =>
        cases w with
        | zero =>
          rw [show a + 1 + 1 + 1 + 0 + 1 = a + 4 by lib_nat]
          show O.le K.zero (K.add (K.mul (K.add (K.add (Zs K x (a + 2)) (Zs K x (a + 4))) (Zs K x (a + 6))) (K.add (K.add (K.add (Zs K x (a + 2)) (Zs K x (a + 4))) (Zs K x (a + 6))) (Zs K x (a + 8)))) (K.neg (K.mul (K.add (K.add (Zs K x (a + 3)) (Zs K x (a + 5))) (Zs K x (a + 7))) (K.add (K.add (K.add (Zs K x (a + 1)) (Zs K x (a + 3))) (Zs K x (a + 5))) (Zs K x (a + 7))))))
          have d0 := (Dn K O x hD (a + 1) (1) (a + 1) (a + 2) (a + 3) (a + 2) (a + 3) (a + 4) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + 2)) (Zs K x (a + 4))) (Zs K x (a + 2))) (K.neg (K.mul (K.add (Zs K x (a + 1)) (Zs K x (a + 3))) (Zs K x (a + 3))))))
          have d1 := (Dn K O x hD (a + 1) (3) (a + 1) (a + 2) (a + 3) (a + 4) (a + 5) (a + 6) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + 4)) (Zs K x (a + 6))) (Zs K x (a + 2))) (K.neg (K.mul (K.add (Zs K x (a + 1)) (Zs K x (a + 3))) (Zs K x (a + 5))))))
          have d2 := (Dn K O x hD (a + 1) (5) (a + 1) (a + 2) (a + 3) (a + 6) (a + 7) (a + 8) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + 6)) (Zs K x (a + 8))) (Zs K x (a + 2))) (K.neg (K.mul (K.add (Zs K x (a + 1)) (Zs K x (a + 3))) (Zs K x (a + 7))))))
          have d3 := (Dn K O x hD (a + 3) (1) (a + 3) (a + 4) (a + 5) (a + 4) (a + 5) (a + 6) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + 4)) (Zs K x (a + 6))) (Zs K x (a + 4))) (K.neg (K.mul (K.add (Zs K x (a + 3)) (Zs K x (a + 5))) (Zs K x (a + 5))))))
          have d4 := (Dn K O x hD (a + 3) (3) (a + 3) (a + 4) (a + 5) (a + 6) (a + 7) (a + 8) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + 6)) (Zs K x (a + 8))) (Zs K x (a + 4))) (K.neg (K.mul (K.add (Zs K x (a + 3)) (Zs K x (a + 5))) (Zs K x (a + 7))))))
          have d5 := (Dn K O x hD (a + 5) (1) (a + 5) (a + 6) (a + 7) (a + 6) (a + 7) (a + 8) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + 6)) (Zs K x (a + 8))) (Zs K x (a + 6))) (K.neg (K.mul (K.add (Zs K x (a + 5)) (Zs K x (a + 7))) (Zs K x (a + 7))))))
          exact A1.nn_of_eq K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ d0 d1) d2) d3) d4) d5) (by lib_ring)
        | succ w =>
          cases w with
          | zero =>
            rw [show a + 1 + 1 + 1 + (0 + 1) + 1 = a + 5 by lib_nat]
            show O.le K.zero (K.add (K.mul (K.add (K.add (Zs K x (a + 2)) (Zs K x (a + 4))) (Zs K x (a + 6))) (K.add (K.add (K.add (Zs K x (a + 3)) (Zs K x (a + 5))) (Zs K x (a + 7))) (Zs K x (a + 9)))) (K.neg (K.mul (K.add (K.add (Zs K x (a + 4)) (Zs K x (a + 6))) (Zs K x (a + 8))) (K.add (K.add (K.add (Zs K x (a + 1)) (Zs K x (a + 3))) (Zs K x (a + 5))) (Zs K x (a + 7))))))
            have d0 := (Dn K O x hD (a + 1) (2) (a + 1) (a + 2) (a + 3) (a + 3) (a + 4) (a + 5) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + 3)) (Zs K x (a + 5))) (Zs K x (a + 2))) (K.neg (K.mul (K.add (Zs K x (a + 1)) (Zs K x (a + 3))) (Zs K x (a + 4))))))
            have d1 := (Dn K O x hD (a + 1) (4) (a + 1) (a + 2) (a + 3) (a + 5) (a + 6) (a + 7) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + 5)) (Zs K x (a + 7))) (Zs K x (a + 2))) (K.neg (K.mul (K.add (Zs K x (a + 1)) (Zs K x (a + 3))) (Zs K x (a + 6))))))
            have d2 := (Dn K O x hD (a + 1) (6) (a + 1) (a + 2) (a + 3) (a + 7) (a + 8) (a + 9) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + 7)) (Zs K x (a + 9))) (Zs K x (a + 2))) (K.neg (K.mul (K.add (Zs K x (a + 1)) (Zs K x (a + 3))) (Zs K x (a + 8))))))
            have d3 := (Dn K O x hD (a + 2) (2) (a + 2) (a + 3) (a + 4) (a + 4) (a + 5) (a + 6) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + 4)) (Zs K x (a + 6))) (Zs K x (a + 3))) (K.neg (K.mul (K.add (Zs K x (a + 2)) (Zs K x (a + 4))) (Zs K x (a + 5))))))
            have d4 := (Dn K O x hD (a + 2) (4) (a + 2) (a + 3) (a + 4) (a + 6) (a + 7) (a + 8) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + 6)) (Zs K x (a + 8))) (Zs K x (a + 3))) (K.neg (K.mul (K.add (Zs K x (a + 2)) (Zs K x (a + 4))) (Zs K x (a + 7))))))
            have d5 := (Dn K O x hD (a + 3) (2) (a + 3) (a + 4) (a + 5) (a + 5) (a + 6) (a + 7) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + 5)) (Zs K x (a + 7))) (Zs K x (a + 4))) (K.neg (K.mul (K.add (Zs K x (a + 3)) (Zs K x (a + 5))) (Zs K x (a + 6))))))
            have d6 := (Dn K O x hD (a + 3) (4) (a + 3) (a + 4) (a + 5) (a + 7) (a + 8) (a + 9) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + 7)) (Zs K x (a + 9))) (Zs K x (a + 4))) (K.neg (K.mul (K.add (Zs K x (a + 3)) (Zs K x (a + 5))) (Zs K x (a + 8))))))
            have d7 := (Dn K O x hD (a + 4) (2) (a + 4) (a + 5) (a + 6) (a + 6) (a + 7) (a + 8) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + 6)) (Zs K x (a + 8))) (Zs K x (a + 5))) (K.neg (K.mul (K.add (Zs K x (a + 4)) (Zs K x (a + 6))) (Zs K x (a + 7))))))
            have d8 := (Dn K O x hD (a + 5) (2) (a + 5) (a + 6) (a + 7) (a + 7) (a + 8) (a + 9) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + 7)) (Zs K x (a + 9))) (Zs K x (a + 6))) (K.neg (K.mul (K.add (Zs K x (a + 5)) (Zs K x (a + 7))) (Zs K x (a + 8))))))
            exact A1.nn_of_eq K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ d0 d1) d2) d3) d4) d5) d6) d7) d8) (by lib_ring)
          | succ w =>
            cases w with
            | zero =>
              rw [show a + 1 + 1 + 1 + (0 + 1 + 1) + 1 = a + 6 by lib_nat]
              show O.le K.zero (K.add (K.mul (K.add (K.add (Zs K x (a + 2)) (Zs K x (a + 4))) (Zs K x (a + 6))) (K.add (K.add (K.add (Zs K x (a + 4)) (Zs K x (a + 6))) (Zs K x (a + 8))) (Zs K x (a + 10)))) (K.neg (K.mul (K.add (K.add (Zs K x (a + 5)) (Zs K x (a + 7))) (Zs K x (a + 9))) (K.add (K.add (K.add (Zs K x (a + 1)) (Zs K x (a + 3))) (Zs K x (a + 5))) (Zs K x (a + 7))))))
              have d0 := (Dn K O x hD (a + 1) (3) (a + 1) (a + 2) (a + 3) (a + 4) (a + 5) (a + 6) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + 4)) (Zs K x (a + 6))) (Zs K x (a + 2))) (K.neg (K.mul (K.add (Zs K x (a + 1)) (Zs K x (a + 3))) (Zs K x (a + 5))))))
              have d1 := (Dn K O x hD (a + 1) (5) (a + 1) (a + 2) (a + 3) (a + 6) (a + 7) (a + 8) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + 6)) (Zs K x (a + 8))) (Zs K x (a + 2))) (K.neg (K.mul (K.add (Zs K x (a + 1)) (Zs K x (a + 3))) (Zs K x (a + 7))))))
              have d2 := (Dn K O x hD (a + 1) (7) (a + 1) (a + 2) (a + 3) (a + 8) (a + 9) (a + 10) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + 8)) (Zs K x (a + 10))) (Zs K x (a + 2))) (K.neg (K.mul (K.add (Zs K x (a + 1)) (Zs K x (a + 3))) (Zs K x (a + 9))))))
              have d3 := (Dn K O x hD (a + 2) (3) (a + 2) (a + 3) (a + 4) (a + 5) (a + 6) (a + 7) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + 5)) (Zs K x (a + 7))) (Zs K x (a + 3))) (K.neg (K.mul (K.add (Zs K x (a + 2)) (Zs K x (a + 4))) (Zs K x (a + 6))))))
              have d4 := (Dn K O x hD (a + 2) (5) (a + 2) (a + 3) (a + 4) (a + 7) (a + 8) (a + 9) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + 7)) (Zs K x (a + 9))) (Zs K x (a + 3))) (K.neg (K.mul (K.add (Zs K x (a + 2)) (Zs K x (a + 4))) (Zs K x (a + 8))))))
              have d5 := (Dn K O x hD (a + 3) (1) (a + 3) (a + 4) (a + 5) (a + 4) (a + 5) (a + 6) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + 4)) (Zs K x (a + 6))) (Zs K x (a + 4))) (K.neg (K.mul (K.add (Zs K x (a + 3)) (Zs K x (a + 5))) (Zs K x (a + 5))))))
              have d6 := (Dn K O x hD (a + 3) (3) (a + 3) (a + 4) (a + 5) (a + 6) (a + 7) (a + 8) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + 6)) (Zs K x (a + 8))) (Zs K x (a + 4))) (K.neg (K.mul (K.add (Zs K x (a + 3)) (Zs K x (a + 5))) (Zs K x (a + 7))))))
              have d7 := (Dn K O x hD (a + 3) (3) (a + 3) (a + 4) (a + 5) (a + 6) (a + 7) (a + 8) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + 6)) (Zs K x (a + 8))) (Zs K x (a + 4))) (K.neg (K.mul (K.add (Zs K x (a + 3)) (Zs K x (a + 5))) (Zs K x (a + 7))))))
              have d8 := (Dn K O x hD (a + 3) (5) (a + 3) (a + 4) (a + 5) (a + 8) (a + 9) (a + 10) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + 8)) (Zs K x (a + 10))) (Zs K x (a + 4))) (K.neg (K.mul (K.add (Zs K x (a + 3)) (Zs K x (a + 5))) (Zs K x (a + 9))))))
              have d9 := (Dn K O x hD (a + 4) (3) (a + 4) (a + 5) (a + 6) (a + 7) (a + 8) (a + 9) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + 7)) (Zs K x (a + 9))) (Zs K x (a + 5))) (K.neg (K.mul (K.add (Zs K x (a + 4)) (Zs K x (a + 6))) (Zs K x (a + 8))))))
              have d10 := (Dn K O x hD (a + 5) (1) (a + 5) (a + 6) (a + 7) (a + 6) (a + 7) (a + 8) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + 6)) (Zs K x (a + 8))) (Zs K x (a + 6))) (K.neg (K.mul (K.add (Zs K x (a + 5)) (Zs K x (a + 7))) (Zs K x (a + 7))))))
              have d11 := (Dn K O x hD (a + 5) (3) (a + 5) (a + 6) (a + 7) (a + 8) (a + 9) (a + 10) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + 8)) (Zs K x (a + 10))) (Zs K x (a + 6))) (K.neg (K.mul (K.add (Zs K x (a + 5)) (Zs K x (a + 7))) (Zs K x (a + 9))))))
              exact A1.nn_of_eq K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ d0 d1) d2) d3) d4) d5) d6) d7) d8) d9) d10) d11) (by lib_ring)
            | succ w =>
              rw [show a + 1 + 1 + 1 + (w + 1 + 1 + 1) + 1 = a + w + 7 by lib_nat]
              show O.le K.zero (K.add (K.mul (K.add (K.add (Zs K x (a + 2)) (Zs K x (a + 4))) (Zs K x (a + 6))) (K.add (K.add (K.add (Zs K x (a + w + 5)) (Zs K x (a + w + 7))) (Zs K x (a + w + 9))) (Zs K x (a + w + 11)))) (K.neg (K.mul (K.add (K.add (Zs K x (a + w + 6)) (Zs K x (a + w + 8))) (Zs K x (a + w + 10))) (K.add (K.add (K.add (Zs K x (a + 1)) (Zs K x (a + 3))) (Zs K x (a + 5))) (Zs K x (a  -- (+10 chars; full line in the Zenodo source)
              have d0 := (Dn K O x hD (a + 1) (w + 4) (a + 1) (a + 2) (a + 3) (a + w + 5) (a + w + 6) (a + w + 7) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + w + 5)) (Zs K x (a + w + 7))) (Zs K x (a + 2))) (K.neg (K.mul (K.add (Zs K x (a + 1)) (Zs K x (a + 3))) (Zs K x (a + w + 6))))))
              have d1 := (Dn K O x hD (a + 1) (w + 6) (a + 1) (a + 2) (a + 3) (a + w + 7) (a + w + 8) (a + w + 9) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + w + 7)) (Zs K x (a + w + 9))) (Zs K x (a + 2))) (K.neg (K.mul (K.add (Zs K x (a + 1)) (Zs K x (a + 3))) (Zs K x (a + w + 8))))))
              have d2 := (Dn K O x hD (a + 1) (w + 8) (a + 1) (a + 2) (a + 3) (a + w + 9) (a + w + 10) (a + w + 11) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + w + 9)) (Zs K x (a + w + 11))) (Zs K x (a + 2))) (K.neg (K.mul (K.add (Zs K x (a + 1)) (Zs K x (a + 3))) (Zs K x (a + w + 10))))))
              have d3 := (Dn K O x hD (a + 2) (w + 4) (a + 2) (a + 3) (a + 4) (a + w + 6) (a + w + 7) (a + w + 8) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + w + 6)) (Zs K x (a + w + 8))) (Zs K x (a + 3))) (K.neg (K.mul (K.add (Zs K x (a + 2)) (Zs K x (a + 4))) (Zs K x (a + w + 7))))))
              have d4 := (Dn K O x hD (a + 2) (w + 6) (a + 2) (a + 3) (a + 4) (a + w + 8) (a + w + 9) (a + w + 10) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + w + 8)) (Zs K x (a + w + 10))) (Zs K x (a + 3))) (K.neg (K.mul (K.add (Zs K x (a + 2)) (Zs K x (a + 4))) (Zs K x (a + w + 9))))))
              have d5 := (Dn K O x hD (a + 3) (w + 2) (a + 3) (a + 4) (a + 5) (a + w + 5) (a + w + 6) (a + w + 7) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + w + 5)) (Zs K x (a + w + 7))) (Zs K x (a + 4))) (K.neg (K.mul (K.add (Zs K x (a + 3)) (Zs K x (a + 5))) (Zs K x (a + w + 6))))))
              have d6 := (Dn K O x hD (a + 3) (w + 4) (a + 3) (a + 4) (a + 5) (a + w + 7) (a + w + 8) (a + w + 9) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + w + 7)) (Zs K x (a + w + 9))) (Zs K x (a + 4))) (K.neg (K.mul (K.add (Zs K x (a + 3)) (Zs K x (a + 5))) (Zs K x (a + w + 8))))))
              have d7 := (Dn K O x hD (a + 3) (w + 4) (a + 3) (a + 4) (a + 5) (a + w + 7) (a + w + 8) (a + w + 9) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + w + 7)) (Zs K x (a + w + 9))) (Zs K x (a + 4))) (K.neg (K.mul (K.add (Zs K x (a + 3)) (Zs K x (a + 5))) (Zs K x (a + w + 8))))))
              have d8 := (Dn K O x hD (a + 3) (w + 6) (a + 3) (a + 4) (a + 5) (a + w + 9) (a + w + 10) (a + w + 11) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + w + 9)) (Zs K x (a + w + 11))) (Zs K x (a + 4))) (K.neg (K.mul (K.add (Zs K x (a + 3)) (Zs K x (a + 5))) (Zs K x (a + w + 10))))))
              have d9 := (Dn K O x hD (a + 4) (w + 2) (a + 4) (a + 5) (a + 6) (a + w + 6) (a + w + 7) (a + w + 8) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + w + 6)) (Zs K x (a + w + 8))) (Zs K x (a + 5))) (K.neg (K.mul (K.add (Zs K x (a + 4)) (Zs K x (a + 6))) (Zs K x (a + w + 7))))))
              have d10 := (Dn K O x hD (a + 4) (w + 4) (a + 4) (a + 5) (a + 6) (a + w + 8) (a + w + 9) (a + w + 10) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + w + 8)) (Zs K x (a + w + 10))) (Zs K x (a + 5))) (K.neg (K.mul (K.add (Zs K x (a + 4)) (Zs K x (a + 6))) (Zs K x (a + w + 9))))))
              have d11 := (Dn K O x hD (a + 5) (w) (a + 5) (a + 6) (a + 7) (a + w + 5) (a + w + 6) (a + w + 7) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + w + 5)) (Zs K x (a + w + 7))) (Zs K x (a + 6))) (K.neg (K.mul (K.add (Zs K x (a + 5)) (Zs K x (a + 7))) (Zs K x (a + w + 6))))))
              have d12 := (Dn K O x hD (a + 5) (w + 2) (a + 5) (a + 6) (a + 7) (a + w + 7) (a + w + 8) (a + w + 9) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + w + 7)) (Zs K x (a + w + 9))) (Zs K x (a + 6))) (K.neg (K.mul (K.add (Zs K x (a + 5)) (Zs K x (a + 7))) (Zs K x (a + w + 8))))))
              have d13 := (Dn K O x hD (a + 5) (w + 4) (a + 5) (a + 6) (a + 7) (a + w + 9) (a + w + 10) (a + w + 11) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) (by lib_nat) : O.le K.zero (K.add (K.mul (K.add (Zs K x (a + w + 9)) (Zs K x (a + w + 11))) (Zs K x (a + 6))) (K.neg (K.mul (K.add (Zs K x (a + 5)) (Zs K x (a + 7))) (Zs K x (a + w + 10))))))
              exact A1.nn_of_eq K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ (A1.nn_add K O _ _ d0 d1) d2) d3) d4) d5) d6) d7) d8) d9) d10) d11) d12) d13) (by lib_ring)

include hD in
/-- (u6) minor(S⁻²ψ₂, S⁻²ψ₃)(i, i+w+1) ≥ 0 -/
theorem u6 (i w : Nat) : O.le K.zero (Lagrange.minor K (Zs K (Zs K (p2v K x))) (Zs K (Zs K (A1C.p3v K x))) i (i + w + 1)) := by
  unfold Lagrange.minor
  cases i with
  | zero =>
    show O.le K.zero (K.add (K.mul K.zero (Zs K (Zs K (A1C.p3v K x)) (0 + w + 1))) (K.neg (K.mul (Zs K (Zs K (p2v K x)) (0 + w + 1)) K.zero)))
    exact A1.nn_of_eq K O _ _ (O.le_refl K.zero) (by lib_ring)
  | succ i =>
    cases i with
    | zero =>
      show O.le K.zero (K.add (K.mul K.zero (Zs K (Zs K (A1C.p3v K x)) (0 + 1 + w + 1))) (K.neg (K.mul (Zs K (Zs K (p2v K x)) (0 + 1 + w + 1)) K.zero)))
      exact A1.nn_of_eq K O _ _ (O.le_refl K.zero) (by lib_ring)
    | succ a =>
      rw [show a + 1 + 1 + w + 1 = a + w + 1 + 2 by lib_nat]
      exact u6core K O x hD a w

found/AD.lean

lean_all/found/AD.lean

/-
  T168. found/AD — determinacy as a point: games are a parameter (moves,
  length, payoff); the axiom of determinacy is the condition "every game
  is determined".
  * Finite games on Bool (alternating moves, decidable payoff) are
    determined, constructively, by induction on the length (Zermelo).
  * Full AD (infinite games on ℕ) is the condition that contradicts choice
    (ZF + AD ⊢ ¬AC, Mycielski–Steinhaus) — a line to T87/T89 (cited).

  Lean 4 core only, hand-written proofs, no axioms.
-/

namespace Found

namespace Det

/-- I wins from history h with n moves left; `turnI` says whose move it is -/
def WinI (P : List Bool → Bool) : Nat → Bool → List Bool → Prop
  | 0, _, h => P h = true
  | n + 1, true, h => WinI P n false (true :: h) ∨ WinI P n false (false :: h)
  | n + 1, false, h => WinI P n true (true :: h) ∧ WinI P n true (false :: h)

def WinII (P : List Bool → Bool) : Nat → Bool → List Bool → Prop
  | 0, _, h => P h = false
  | n + 1, true, h => WinII P n false (true :: h) ∧ WinII P n false (false :: h)
  | n + 1, false, h => WinII P n true (true :: h) ∨ WinII P n true (false :: h)

/-- Zermelo: every finite game on Bool is determined -/
theorem determined (P : List Bool → Bool) : ∀ n t h, WinI P n t h ∨ WinII P n t h
  | 0, _, h => by
    cases e : P h
    · exact Or.inr e
    · exact Or.inl e
  | n + 1, true, h =>
    (determined P n false (true :: h)).elim (fun w => Or.inl (Or.inl w))
      (fun l1 => (determined P n false (false :: h)).elim (fun w => Or.inl (Or.inr w))
        (fun l2 => Or.inr ⟨l1, l2⟩))
  | n + 1, false, h =>
    (determined P n true (true :: h)).elim
      (fun w1 => (determined P n true (false :: h)).elim (fun w2 => Or.inl ⟨w1, w2⟩)
        (fun l2 => Or.inr (Or.inr l2)))
      (fun l1 => Or.inr (Or.inl l1))

/-- and not both -/
theorem not_both (P : List Bool → Bool) : ∀ n t h, ¬ (WinI P n t h ∧ WinII P n t h)
  | 0, _, _, ⟨a, b⟩ => Bool.noConfusion (a.symm.trans b)
  | n + 1, true, _, ⟨a, b⟩ => a.elim (fun w => not_both P n false _ ⟨w, b.1⟩) (fun w => not_both P n false _ ⟨w, b.2⟩)
  | n + 1, false, _, ⟨a, b⟩ => b.elim (fun l => not_both P n true _ ⟨a.1, l⟩) (fun l => not_both P n true _ ⟨a.2, l⟩)

/-- the AD condition for a class of (infinite) games, as a parameter -/
def AD (Game : Type) (WinsI WinsII : Game → Prop) : Prop := ∀ g, WinsI g ∨ WinsII g

end Det

end Found

#print axioms Found.Det.determined
#print axioms Found.Det.not_both

found/ADChoice.lean

lean_all/found/ADChoice.lean

/-
  T168, the line AC ⟂ AD. found/ADChoice — in the classical point (choice
  and excluded middle as data), there is a non-determined infinite game on
  Bool (Mycielski–Steinhaus direction: choice refutes determinacy).
  * Plays are sequences of rounds (I's bit, II's bit); a strategy maps the
    opponent's previous bits (for II: and I's current bit) to a bit.
  * Strategies are well-ordered so that every initial segment is smaller
    than the set of strategies (least segment of Zermelo's well-order that
    is not, or the whole order); strategies are compared up to pointwise
    equality, so no extensionality is used.
  * Along this order each strategy receives a fresh play consistent with it
    (fewer earlier plays than plays consistent with it); plays of
    II-strategies form the payoff set A.  No strategy of I wins A and no
    strategy of II wins its complement.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Cardinal
import found.EffectiveK1

namespace Found

namespace ADC

open CP WO Card

abbrev Strat : Type := List Bool → Bool
abbrev Cd : Type := Bool × Strat
abbrev Play : Type := Nat → Bool × Bool

def CEq (c d : Cd) : Prop := c.1 = d.1 ∧ ∀ l, c.2 l = d.2 l
def PEq (p q : Play) : Prop := ∀ k, p k = q k

/-- the first k bits of a sequence, most recent first -/
def pre (y : Nat → Bool) : Nat → List Bool
  | 0 => []
  | k + 1 => y k :: pre y k

def playI (σ : Strat) (y : Nat → Bool) : Play := fun k => (σ (pre y k), y k)
def playII (τ : Strat) (x : Nat → Bool) : Play := fun k => (x k, τ (pre x (k + 1)))

/-- the play of a tagged strategy against an opponent sequence -/
def playC (c : Cd) (y : Nat → Bool) : Play := cond c.1 (playI c.2 y) (playII c.2 y)

def SEq (a b : Nat → Bool) : Prop := ∀ k, a k = b k

/-- S is small: the bit sequences do not inject into S (up to pointwise equality) -/
def Small (S : Type) : Prop := ¬ ∃ f : (Nat → Bool) → S, ∀ a b, f a = f b → SEq a b

theorem pre_ext {σ τ : Strat} (h : ∀ l, σ l = τ l) (c : Bool) (y : Nat → Bool) :
    PEq (playC (c, σ) y) (playC (c, τ) y) := fun k => by
  cases c with
  | true => show (σ (pre y k), y k) = (τ (pre y k), y k); rw [h]
  | false => show (y k, σ (pre y (k + 1))) = (y k, τ (pre y (k + 1))); rw [h]

/-- the opponent's bits are recovered from the play -/
theorem opp (c : Cd) (y : Nat → Bool) (k : Nat) : cond c.1 (playC c y k).2 (playC c y k).1 = y k := by
  cases h : c.1 with
  | true => show cond true _ _ = y k; show ((cond c.1 (playI c.2 y) (playII c.2 y)) k).2 = y k; rw [h]; rfl
  | false => show ((cond c.1 (playI c.2 y) (playII c.2 y)) k).1 = y k; rw [h]; rfl

/-- a strategy's consistent plays are not covered by a small family -/
theorem fresh (K : Classical') (S : Type) (hS : Small S) (prev : S → Play) (c : Cd) :
    ∃ y : Nat → Bool, ∀ i, ¬ PEq (playC c y) (prev i) := by
  refine dne K _ (fun hn => hS ?_)
  have hall : ∀ y : Nat → Bool, ∃ i, PEq (playC c y) (prev i) := fun y =>
    dne K _ (fun hy => hn ⟨y, fun i hi => hy ⟨i, hi⟩⟩)
  refine ⟨fun y => K.ch _ (hall y), fun a b e k => ?_⟩
  have ha := K.ch_spec _ (hall a)
  have hb := K.ch_spec _ (hall b)
  have e' : K.ch _ (hall a) = K.ch _ (hall b) := e
  rw [e'] at ha
  have hp : playC c a k = playC c b k := (ha k).trans (hb k).symm
  rw [← opp c a k, ← opp c b k, hp]

/-! ## coding strategies by bit sequences -/

def enc : List Bool → Nat
  | [] => 0
  | b :: l => K1.pr (cond b 1 0) (enc l)

def decF : Nat → Nat → List Bool
  | 0, _ => []
  | _ + 1, 0 => []
  | f + 1, m + 1 => ((K1.unpS (m + 1)).1 == 1) :: decF f (K1.unpS (m + 1)).2

theorem len_enc : (l : List Bool) → l.length ≤ enc l
  | [] => Nat.le_refl 0
  | b :: l => Nat.succ_le_of_lt (Nat.lt_of_le_of_lt (len_enc l)
      (Nat.lt_of_le_of_lt (Nat.le_add_left _ _) (K1.pr_ge (cond b 1 0) (enc l))))

theorem decF_enc : (l : List Bool) → (f : Nat) → l.length ≤ f → decF f (enc l) = l
  | [], 0, _ => rfl
  | [], _ + 1, _ => rfl
  | _ :: _, 0, h => absurd h (Nat.not_succ_le_zero _)
  | b :: l, f + 1, h => by
    have hpos : 0 < K1.pr (cond b 1 0) (enc l) :=
      Nat.lt_of_le_of_lt (Nat.zero_le _) (K1.pr_ge (cond b 1 0) (enc l))
    show decF (f + 1) (K1.pr (cond b 1 0) (enc l)) = b :: l
    match hm : K1.pr (cond b 1 0) (enc l) with
    | 0 => rw [hm] at hpos; exact absurd hpos (Nat.lt_irrefl 0)
    | m + 1 =>
      show ((K1.unpS (m + 1)).1 == 1) :: decF f (K1.unpS (m + 1)).2 = b :: l
      have hm' : K1.pr (cond b 1 0) (enc l) = m + 1 := hm
      rw [← hm', K1.unpS_pr, decF_enc l f (Nat.le_of_succ_le_succ h)]
      cases b <;> rfl

def dec (k : Nat) : List Bool := decF k k

theorem dec_enc (l : List Bool) : dec (enc l) = l := decF_enc l (enc l) (len_enc l)

def code (c : Cd) : Nat → Bool
  | 0 => c.1
  | k + 1 => c.2 (dec k)

theorem code_inj (c d : Cd) (h : SEq (code c) (code d)) : CEq c d :=
  ⟨h 0, fun l => by
    have := h (enc l + 1)
    show c.2 l = d.2 l
    have e1 : code c (enc l + 1) = c.2 l := by show c.2 (dec (enc l)) = c.2 l; rw [dec_enc]
    have e2 : code d (enc l + 1) = d.2 l := by show d.2 (dec (enc l)) = d.2 l; rw [dec_enc]
    rw [← e1, ← e2, this]⟩

/-! ## a well-order of strategies with small initial segments -/

structure Stages where
  St : Type
  lt : St → St → Prop
  wf : WellFounded lt
  tri : ∀ a b, lt a b ∨ a = b ∨ lt b a
  st : Cd → St
  st_inj : ∀ c d, st c = st d → CEq c d
  small : ∀ ξ, Small {η // lt η ξ}

theorem triLt (K : Classical') (x y : Cd) : Lt' K x y ∨ x = y ∨ Lt' K y x :=
  (le_total K x y).elim
    (fun h => (K.lem (Le K y x)).elim (fun h' => Or.inr (Or.inl (le_antisymm K x y h h'))) (fun h' => Or.inl ⟨h, h'⟩))
    (fun h => (K.lem (Le K x y)).elim (fun h' => Or.inr (Or.inl (le_antisymm K x y h' h))) (fun h' => Or.inr (Or.inr ⟨h, h'⟩)))

theorem stages (K : Classical') : Nonempty Stages :=
  (K.lem (∃ ξ : Cd, ¬ Small {η // Lt' K η ξ})).elim
    (fun hb => match least K (fun ξ : Cd => ¬ Small {η // Lt' K η ξ}) hb with
      | ⟨ξ0, hbig, hmin⟩ =>
        match dne K _ hbig with
        | ⟨g, hg⟩ => ⟨{
            St := {η : Cd // Lt' K η ξ0}
            lt := fun a b => Lt' K a.val b.val
            wf := InvImage.wf (fun a : {η : Cd // Lt' K η ξ0} => a.val) (wf K Cd)
            tri := fun a b => (triLt K a.val b.val).elim Or.inl
              (fun h => h.elim (fun e => Or.inr (Or.inl (Subtype.ext e))) (fun h => Or.inr (Or.inr h)))
            st := fun c => g (code c)
            st_inj := fun c d e => code_inj c d (hg _ _ e)
            small := fun ξ ⟨f, hf⟩ => ξ.property.2 (hmin ξ.val (fun hs => hs
                ⟨fun a => ⟨(f a).val.val, (f a).property⟩, fun a b e => hf a b (Subtype.ext (Subtype.ext
                  (by have e' := congrArg Subtype.val e; exact e')))⟩)) }⟩)
    (fun hn => ⟨{
        St := Cd
        lt := Lt' K
        wf := wf K Cd
        tri := triLt K
        st := fun c => c
        st_inj := fun c d e => e ▸ ⟨rfl, fun _ => rfl⟩
        small := fun ξ => dne K _ (fun h => hn ⟨ξ, h⟩) }⟩)

/-! ## the diagonal construction -/

section Diag

variable (K : Classical') (S : Stages)

include K in
theorem code_ex (η : S.St) : ∃ c : Cd, (∃ c', S.st c' = η) → S.st c = η :=
  (K.lem (∃ c', S.st c' = η)).elim (fun ⟨c, h⟩ => ⟨c, fun _ => h⟩) (fun hn => ⟨(true, fun _ => true), fun h => (hn h).elim⟩)

/-- the strategy handled at stage η -/
def stCode (η : S.St) : Cd := K.ch _ (code_ex K S η)

def stepP (η : S.St) (rec : ∀ η', S.lt η' η → Play) : Play :=
  playC (stCode K S η) (K.ch (fun y => ∀ i : {η' // S.lt η' η}, ¬ PEq (playC (stCode K S η) y) (rec i.val i.property))
    (fresh K _ (S.small η) (fun i => rec i.val i.property) _))

/-- the plays chosen along the well-order -/
def plays : S.St → Play := S.wf.fix (stepP K S)

theorem plays_eq (η : S.St) : plays K S η = stepP K S η (fun η' _ => plays K S η') := S.wf.fix_eq _ η

theorem opp_seq (η : S.St) : ∃ y, plays K S η = playC (stCode K S η) y ∧
    ∀ η', S.lt η' η → ¬ PEq (playC (stCode K S η) y) (plays K S η') := by
  rw [plays_eq]
  exact ⟨_, rfl, fun η' h => K.ch_spec (fun y => ∀ i : {η' // S.lt η' η}, ¬ PEq (playC (stCode K S η) y) (plays K S i.val))
    (fresh K _ (S.small η) (fun i => plays K S i.val) _) ⟨η', h⟩⟩

theorem peq_symm {p q : Play} (h : PEq p q) : PEq q p := fun k => (h k).symm
theorem peq_trans {p q r : Play} (h : PEq p q) (h' : PEq q r) : PEq p r := fun k => (h k).trans (h' k)

theorem fresh_later (η η' : S.St) (h : S.lt η' η) : ¬ PEq (plays K S η) (plays K S η') :=
  match opp_seq K S η with
  | ⟨_, e, hn⟩ => by rw [e]; exact hn η' h

theorem distinct (η η' : S.St) (ne : η ≠ η') : ¬ PEq (plays K S η) (plays K S η') := fun hp =>
  (S.tri η η').elim (fun h => fresh_later K S η' η h (peq_symm hp))
    (fun h => h.elim (fun e => ne e) (fun h => fresh_later K S η η' h hp))

theorem stCode_of (c : Cd) : CEq (stCode K S (S.st c)) c :=
  S.st_inj _ _ (K.ch_spec _ (code_ex K S (S.st c)) ⟨c, rfl⟩)

/-- the payoff set: plays (up to pointwise equality) chosen for strategies of player II -/
def A (q : Play) : Prop := ∃ η, (stCode K S η).1 = false ∧ PEq q (plays K S η)

theorem playC_ceq {c d : Cd} (h : CEq c d) (y : Nat → Bool) : PEq (playC c y) (playC d y) := by
  have e : c = (d.1, c.2) := by
    cases c with
    | mk a b => cases h.1; rfl
  rw [e]
  exact pre_ext h.2 d.1 y

end Diag

/-- with choice, the game with payoff A is not determined:
  no strategy of I wins A, and no strategy of II wins the complement of A -/
theorem not_determined (K : Classical') : ∃ A : Play → Prop,
    (∀ σ : Strat, ∃ y, ¬ A (playI σ y)) ∧ (∀ τ : Strat, ∃ x, A (playII τ x)) :=
  match stages K with
  | ⟨S⟩ => ⟨A K S,
    fun σ => match opp_seq K S (S.st (true, σ)) with
      | ⟨y, e, _⟩ => ⟨y, fun ⟨η', tag, hq⟩ => by
          have hc := stCode_of K S (true, σ)
          have p1 : PEq (playI σ y) (plays K S (S.st (true, σ))) := by
            rw [e]; exact peq_symm (playC_ceq hc y)
          refine distinct K S (S.st (true, σ)) η' (fun eq => ?_) (peq_trans (peq_symm p1) hq)
          rw [← eq] at tag
          exact Bool.noConfusion (hc.1.symm.trans tag)⟩,
    fun τ => match opp_seq K S (S.st (false, τ)) with
      | ⟨x, e, _⟩ => ⟨x, S.st (false, τ), (stCode_of K S (false, τ)).1, by
          rw [e]; exact peq_symm (playC_ceq (stCode_of K S (false, τ)) x)⟩⟩

end ADC

end Found

#print axioms Found.ADC.fresh
#print axioms Found.ADC.dec_enc
#print axioms Found.ADC.code_inj
#print axioms Found.ADC.stages
#print axioms Found.ADC.distinct
#print axioms Found.ADC.not_determined

found/AFA.lean

lean_all/found/AFA.lean

/-
  T105. found/AFA — non-well-founded sets: graphs, bisimulation, Aczel's
  anti-foundation axiom, final coalgebras, streams.  No quotient: hypersets
  are pointed graphs with bisimilarity as the setoid relation.

  * Hypersets: pointed graphs; x ≈ y : there is a bisimulation relating the
    roots; membership z ∈ x : z ≈ (x re-rooted at a child of its root).
  * ≈ is an equivalence; membership respects ≈.
  * Coinduction: every "set bisimulation" R (members of x matched by
    R-related members of y, and back) is contained in ≈.
  * Strong extensionality: x ≈ y ⟺ x and y have the same members.
  * AFA: every graph has a decoration n ↦ (graph rooted at n), and any two
    decorations agree up to ≈ (final coalgebra of the powerset functor).
  * Foundation fails: Ω = {Ω} exists (a one-node loop), Ω ∈ Ω.
  * Streams: functions ℕ → A with the unfold map form the final coalgebra
    of A × (−): unfold commutes and is unique up to pointwise equality.

  Lean 4 core only, hand-written proofs, no axioms.

  CIC dependence: bisimilarity ∃ S (quantification over relations = impredicative Prop).
-/

namespace Found

namespace AFA

structure HS where
  N : Type
  E : N → N → Prop
  root : N

def at' (x : HS) (n : x.N) : HS := ⟨x.N, x.E, n⟩

def Bisim (x y : HS) (S : x.N → y.N → Prop) : Prop :=
  ∀ a b, S a b → (∀ a', x.E a a' → ∃ b', y.E b b' ∧ S a' b') ∧ (∀ b', y.E b b' → ∃ a', x.E a a' ∧ S a' b')

def Eqv (x y : HS) : Prop := ∃ S, Bisim x y S ∧ S x.root y.root

def Mem (z x : HS) : Prop := ∃ c, x.E x.root c ∧ Eqv z (at' x c)

/-! ## ≈ is an equivalence -/

theorem eqv_refl (x : HS) : Eqv x x :=
  ⟨fun a b => a = b, fun a b e => by
    cases e
    exact ⟨fun a' h => ⟨a', h, rfl⟩, fun b' h => ⟨b', h, rfl⟩⟩, rfl⟩

theorem eqv_symm {x y : HS} (h : Eqv x y) : Eqv y x :=
  match h with
  | ⟨S, hS, r⟩ => ⟨fun b a => S a b, fun b a s =>
      ⟨fun b' e => match (hS a b s).2 b' e with | ⟨a', e', s'⟩ => ⟨a', e', s'⟩,
       fun a' e => match (hS a b s).1 a' e with | ⟨b', e', s'⟩ => ⟨b', e', s'⟩⟩, r⟩

theorem eqv_trans {x y z : HS} (h : Eqv x y) (k : Eqv y z) : Eqv x z :=
  match h, k with
  | ⟨S, hS, r⟩, ⟨T, hT, r'⟩ =>
    ⟨fun a c => ∃ b, S a b ∧ T b c, fun a c ⟨b, s, t⟩ =>
      ⟨fun a' e => match (hS a b s).1 a' e with
        | ⟨b', e', s'⟩ => match (hT b c t).1 b' e' with
          | ⟨c', e'', t'⟩ => ⟨c', e'', b', s', t'⟩,
       fun c' e => match (hT b c t).2 c' e with
        | ⟨b', e', t'⟩ => match (hS a b s).2 b' e' with
          | ⟨a', e'', s'⟩ => ⟨a', e'', b', s', t'⟩⟩,
     ⟨y.root, r, r'⟩⟩

/-- a bisimulation relates re-rooted graphs at related nodes -/
theorem eqv_at {x y : HS} {S : x.N → y.N → Prop} (hS : Bisim x y S) {a : x.N} {b : y.N} (s : S a b) :
    Eqv (at' x a) (at' y b) := ⟨S, hS, s⟩

/-- membership respects ≈ on the right -/
theorem mem_eqv {z x y : HS} (m : Mem z x) (h : Eqv x y) : Mem z y :=
  match m, h with
  | ⟨c, e, hz⟩, ⟨_, hS, r⟩ =>
    match (hS x.root y.root r).1 c e with
    | ⟨d, e', s⟩ => ⟨d, e', eqv_trans hz (eqv_at hS s)⟩

/-- membership respects ≈ on the left -/
theorem eqv_mem {z z' x : HS} (h : Eqv z z') (m : Mem z x) : Mem z' x :=
  match m with
  | ⟨c, e, hz⟩ => ⟨c, e, eqv_trans (eqv_symm h) hz⟩

theorem child_mem (x : HS) {c : x.N} (e : x.E x.root c) : Mem (at' x c) x := ⟨c, e, eqv_refl _⟩

/-! ## Coinduction -/

def SetBisim (R : HS → HS → Prop) : Prop :=
  ∀ x y, R x y → (∀ z, Mem z x → ∃ w, Mem w y ∧ R z w) ∧ (∀ w, Mem w y → ∃ z, Mem z x ∧ R z w)

theorem coinduction {R : HS → HS → Prop} (hR : SetBisim R) {x y : HS} (r : R x y) : Eqv x y := by
  refine ⟨fun a b => ∃ x' y', Eqv (at' x a) x' ∧ R x' y' ∧ Eqv y' (at' y b), ?_, ⟨x, y, eqv_refl x, r, eqv_refl y⟩⟩
  intro a b ⟨x', y', h1, r', h2⟩
  constructor
  · intro a' e
    have m : Mem (at' x a') x' := mem_eqv (child_mem (at' x a) e) h1
    match (hR x' y' r').1 _ m with
    | ⟨w, mw, rw⟩ =>
      match mem_eqv mw h2 with
      | ⟨b', e', hw⟩ => exact ⟨b', e', at' x a', w, eqv_refl _, rw, hw⟩
  · intro b' e
    have m : Mem (at' y b') y' := mem_eqv (child_mem (at' y b) e) (eqv_symm h2)
    match (hR x' y' r').2 _ m with
    | ⟨z, mz, rz⟩ =>
      match mem_eqv mz (eqv_symm h1) with
      | ⟨a', e', hz⟩ => exact ⟨a', e', z, at' y b', eqv_symm hz, rz, eqv_refl _⟩

/-- strong extensionality: bisimilar ⟺ same members -/
theorem ext_iff (x y : HS) : Eqv x y ↔ ∀ z, Mem z x ↔ Mem z y :=
  ⟨fun h _ => ⟨fun m => mem_eqv m h, fun m => mem_eqv m (eqv_symm h)⟩,
   fun h => coinduction (R := fun a b => ∀ z, Mem z a ↔ Mem z b)
     (fun _ _ k => ⟨fun z m => ⟨z, (k z).mp m, fun _ => Iff.rfl⟩,
                    fun w m => ⟨w, (k w).mpr m, fun _ => Iff.rfl⟩⟩) h⟩

/-! ## AFA: unique decorations -/

structure Graph where
  N : Type
  E : N → N → Prop

def dec (G : Graph) (n : G.N) : HS := ⟨G.N, G.E, n⟩

/-- the decoration: dec n has exactly the members dec c for children c -/
theorem dec_spec (G : Graph) (n : G.N) (z : HS) : Mem z (dec G n) ↔ ∃ c, G.E n c ∧ Eqv z (dec G c) :=
  Iff.rfl

def IsDecoration (G : Graph) (d : G.N → HS) : Prop :=
  ∀ n z, Mem z (d n) ↔ ∃ c, G.E n c ∧ Eqv z (d c)

theorem dec_is_decoration (G : Graph) : IsDecoration G (dec G) := dec_spec G

/-- AFA: any decoration agrees with dec up to ≈ -/
theorem decoration_unique (G : Graph) (d : G.N → HS) (hd : IsDecoration G d) (n : G.N) :
    Eqv (d n) (dec G n) :=
  coinduction (R := fun x y => ∃ m, Eqv x (d m) ∧ Eqv y (dec G m))
    (fun _ _ ⟨m, hx, hy⟩ =>
      ⟨fun z mz => match (hd m z).mp (mem_eqv mz hx) with
         | ⟨c, e, hz⟩ => ⟨dec G c, mem_eqv (child_mem (dec G m) e) (eqv_symm hy), c, hz, eqv_refl _⟩,
       fun _ mw => match mem_eqv mw hy with
         | ⟨c, e, hw⟩ => ⟨d c, mem_eqv ((hd m (d c)).mpr ⟨c, e, eqv_refl _⟩) (eqv_symm hx), c,
             eqv_refl _, hw⟩⟩)
    ⟨n, eqv_refl _, eqv_refl _⟩

/-! ## Foundation fails: Ω = {Ω} -/

def Omega : HS := ⟨Unit, fun _ _ => True, ()⟩

theorem omega_mem_self : Mem Omega Omega := ⟨(), trivial, eqv_refl _⟩

theorem omega_singleton (z : HS) : Mem z Omega ↔ Eqv z Omega :=
  ⟨fun ⟨_, _, h⟩ => h, fun h => ⟨(), trivial, h⟩⟩

theorem not_foundation : ¬ ∀ x : HS, ¬ Mem x x := fun h => h Omega omega_mem_self

/-! ## Streams: the final coalgebra of A × (−) -/

section Streams
variable {A : Type}

def Stream' (A : Type) : Type := Nat → A

def hd (s : Stream' A) : A := s 0
def tl (s : Stream' A) : Stream' A := fun n => s (n + 1)

def unfold {S : Type} (c : S → A × S) (x : S) : Stream' A
  | 0 => (c x).1
  | n + 1 => unfold c (c x).2 n

/-- unfold is a coalgebra map -/
theorem unfold_hd {S : Type} (c : S → A × S) (x : S) : hd (unfold c x) = (c x).1 := rfl
theorem unfold_tl {S : Type} (c : S → A × S) (x : S) (n : Nat) :
    tl (unfold c x) n = unfold c (c x).2 n := rfl

/-- and the unique one, up to pointwise equality (bisimilarity of streams) -/
theorem unfold_unique {S : Type} (c : S → A × S) (f : S → Stream' A)
    (hh : ∀ x, hd (f x) = (c x).1) (ht : ∀ x n, tl (f x) n = f (c x).2 n) :
    ∀ n x, f x n = unfold c x n
  | 0, x => hh x
  | n + 1, x => (ht x n).trans (unfold_unique c f hh ht n (c x).2)

end Streams

end AFA

end Found

#print axioms Found.AFA.eqv_refl
#print axioms Found.AFA.eqv_symm
#print axioms Found.AFA.eqv_trans
#print axioms Found.AFA.mem_eqv
#print axioms Found.AFA.eqv_mem
#print axioms Found.AFA.coinduction
#print axioms Found.AFA.ext_iff
#print axioms Found.AFA.dec_spec
#print axioms Found.AFA.decoration_unique
#print axioms Found.AFA.omega_mem_self
#print axioms Found.AFA.omega_singleton
#print axioms Found.AFA.not_foundation
#print axioms Found.AFA.unfold_hd
#print axioms Found.AFA.unfold_tl
#print axioms Found.AFA.unfold_unique

found/AST.lean

lean_all/found/AST.lean

/-
  T166. found/AST — Vopěnka's alternative set theory as a point: classes,
  sets, and semisets (subclasses of sets); AST's characteristic condition is
  that some semiset is not a set (e.g. the class of "small" naturals).
  * In the V point with full separation (T88, T91) every semiset is a set:
    AST's condition contradicts separation — the line AST ⟂ ZF-style
    separation; AST lives where separation is restricted (to set
    formulas), like NF/GPK (T106, T164).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.NBG

namespace Found

namespace AST

open WF

/-- a semiset: a respecting class contained in some set -/
def Semiset (A : NBG.Cls.{0}) : Prop := ∃ a : V.{0}, ∀ x, A.p x → Mem x a

def IsSet (A : NBG.Cls.{0}) : Prop := ∃ s : V.{0}, ∀ x, Mem x s ↔ A.p x

/-- AST's condition: a proper semiset -/
def ProperSemiset : Prop := ∃ A : NBG.Cls.{0}, Semiset A ∧ ¬ IsSet A

/-- with full separation (V, T91) every semiset is a set -/
theorem semiset_is_set (A : NBG.Cls.{0}) (h : Semiset A) : IsSet A :=
  match h with
  | ⟨a, ha⟩ => NBG.subclass_set A a ha

theorem ast_contradicts_separation : ¬ ProperSemiset :=
  fun ⟨A, hs, hn⟩ => hn (semiset_is_set A hs)

end AST

end Found

#print axioms Found.AST.semiset_is_set
#print axioms Found.AST.ast_contradicts_separation

found/ATR.lean

lean_all/found/ATR.lean

/-
  T84, the classes ATR₀ and Π¹₁-CA₀. found/ATR — further comprehension
  values on ω-models (ℕ, D) (found/SecondOrder):
  * Π¹₁ predicates over D in normal form ∀X ∈ D ∃m R(X, n, m) with R
    decidable; Π¹₁-comprehension for D; it implies Σ⁰₁-comprehension
    (ACA direction) as soon as D is inhabited;
  * arithmetical transfinite recursion along a well-founded relation:
    for every operator Θ in a class C, a hierarchy H with
    H_a = Θ(H restricted below a), stored in D2 (binary relations in the
    model); ATR for C implies comprehension for every predicate obtained
    by Θ ∈ C from the empty hierarchy;
  * the full ω-model satisfies Π¹₁-CA and ATR for every class C (the
    hierarchy built by well-founded recursion), and hierarchies are unique
    up to pointwise equivalence (well-founded induction).

  Lean 4 core only, hand-written proofs, no axioms.
  CIC dependence: D ⊆ (ℕ → Prop) (Prop-valued predicates).
-/
import found.SecondOrder

namespace Found

namespace ATR

open RM

/-- Π¹₁ over D, normal form ∀ X ∈ D, ∃ m, R X n m with R decidable -/
def Pi11 (D : (Nat → Prop) → Prop) (P : Nat → Prop) : Prop :=
  ∃ R : (Nat → Prop) → Nat → Nat → Prop, (∀ X n m, R X n m ∨ ¬ R X n m) ∧
    ∀ n, P n ↔ ∀ X, D X → ∃ m, R X n m

def Pi11CA (D : (Nat → Prop) → Prop) : Prop := Comp (Pi11 D) D

/-- every Σ⁰₁ predicate is Π¹₁ over an inhabited D -/
theorem sigma1_pi11 (D : (Nat → Prop) → Prop) (X0 : Nat → Prop) (h0 : D X0) (P : Nat → Prop) (hP : Sigma1 P) :
    Pi11 D P :=
  match hP with
  | ⟨Q, dq, e⟩ => ⟨fun _ n m => Q n m, fun _ n m => dq n m, fun n =>
      ⟨fun p _ _ => (e n).1 p, fun k => (e n).2 (k X0 h0)⟩⟩

/-- Π¹₁-CA ⇒ Σ⁰₁-CA (ACA direction) on inhabited models -/
theorem pi11_aca (D : (Nat → Prop) → Prop) (X0 : Nat → Prop) (h0 : D X0) (h : Pi11CA D) : Comp Sigma1 D :=
  fun P hP => h P (sigma1_pi11 D X0 h0 P hP)

theorem full_pi11 : Pi11CA (fun _ => True) := fun P _ => ⟨P, trivial, fun _ => Iff.rfl⟩

/-! ## arithmetical transfinite recursion -/

/-- an operator respects pointwise equivalence -/
def Resp (Θ : (Nat → Nat → Prop) → Nat → Prop) : Prop :=
  ∀ H H' : Nat → Nat → Prop, (∀ b m, H b m ↔ H' b m) → ∀ n, Θ H n ↔ Θ H' n

/-- H is a Θ-hierarchy along lt -/
def Hier (lt : Nat → Nat → Prop) (Θ : (Nat → Nat → Prop) → Nat → Prop) (H : Nat → Nat → Prop) : Prop :=
  ∀ a n, H a n ↔ Θ (fun b m => lt b a ∧ H b m) n

/-- ATR for a class C of operators in a model with sets of pairs D2 -/
def ATRc (C : ((Nat → Nat → Prop) → Nat → Prop) → Prop) (D2 : (Nat → Nat → Prop) → Prop) : Prop :=
  ∀ lt : Nat → Nat → Prop, WellFounded lt → ∀ Θ, C Θ → Resp Θ → ∃ H, D2 H ∧ Hier lt Θ H

/-- the hierarchy by well-founded recursion -/
def hstep (Θ : (Nat → Nat → Prop) → Nat → Prop) (lt : Nat → Nat → Prop) (a : Nat)
    (rec : ∀ b, lt b a → Nat → Prop) : Nat → Prop :=
  Θ (fun b m => ∃ h : lt b a, rec b h m)

def hier (lt : Nat → Nat → Prop) (w : WellFounded lt) (Θ : (Nat → Nat → Prop) → Nat → Prop) : Nat → Nat → Prop :=
  w.fix (hstep Θ lt)

theorem hier_spec (lt : Nat → Nat → Prop) (w : WellFounded lt) (Θ : (Nat → Nat → Prop) → Nat → Prop) (hΘ : Resp Θ) :
    Hier lt Θ (hier lt w Θ) := fun a n => by
  have e : hier lt w Θ a = hstep Θ lt a (fun b _ => hier lt w Θ b) := w.fix_eq (hstep Θ lt) a
  rw [e]
  exact hΘ _ _ (fun _ _ => ⟨fun ⟨h, x⟩ => ⟨h, x⟩, fun ⟨h, x⟩ => ⟨h, x⟩⟩) n

/-- the full ω-model satisfies ATR for every class of operators -/
theorem full_atr (C : ((Nat → Nat → Prop) → Nat → Prop) → Prop) : ATRc C (fun _ => True) :=
  fun lt w Θ _ hΘ => ⟨hier lt w Θ, trivial, hier_spec lt w Θ hΘ⟩

/-- hierarchies are unique up to pointwise equivalence -/
theorem hier_unique (lt : Nat → Nat → Prop) (w : WellFounded lt) (Θ : (Nat → Nat → Prop) → Nat → Prop) (hΘ : Resp Θ)
    (H H' : Nat → Nat → Prop) (h : Hier lt Θ H) (h' : Hier lt Θ H') : ∀ a n, H a n ↔ H' a n := by
  intro a
  induction w.apply a with
  | intro a _ ih =>
    intro n
    have e := hΘ (fun b m => lt b a ∧ H b m) (fun b m => lt b a ∧ H' b m)
      (fun b m => ⟨fun ⟨l, x⟩ => ⟨l, (ih b l m).1 x⟩, fun ⟨l, x⟩ => ⟨l, (ih b l m).2 x⟩⟩) n
    exact ⟨fun x => (h' a n).2 (e.1 ((h a n).1 x)), fun x => (h a n).2 (e.2 ((h' a n).1 x))⟩

/-- the empty relation is well-founded -/
theorem wf_empty : WellFounded (fun _ _ : Nat => False) := ⟨fun a => ⟨a, fun _ f => f.elim⟩⟩

/-- ATR for C gives comprehension (inside D2) for every Θ ∈ C applied to the empty hierarchy -/
theorem atr_comp (C : ((Nat → Nat → Prop) → Nat → Prop) → Prop) (D2 : (Nat → Nat → Prop) → Prop) (h : ATRc C D2)
    (Θ : (Nat → Nat → Prop) → Nat → Prop) (hC : C Θ) (hΘ : Resp Θ) :
    ∃ H, D2 H ∧ ∀ n, H 0 n ↔ Θ (fun _ _ => False) n :=
  match h _ wf_empty Θ hC hΘ with
  | ⟨H, d, hh⟩ => ⟨H, d, fun n => (hh 0 n).trans (hΘ _ _ (fun _ _ => ⟨fun x => x.1, fun x => x.elim⟩) n)⟩

/-- ATR on a model whose D2 is the full relations of D: the chain ATR ⇒ arithmetical comprehension
    for operators ignoring the hierarchy -/
theorem atr_aca (D2 : (Nat → Nat → Prop) → Prop) (h : ATRc (fun _ => True) D2) (P : Nat → Prop) :
    ∃ H, D2 H ∧ ∀ n, H 0 n ↔ P n :=
  atr_comp _ D2 h (fun _ => P) trivial (fun _ _ _ _ => Iff.rfl)

end ATR

end Found

#print axioms Found.ATR.sigma1_pi11
#print axioms Found.ATR.pi11_aca
#print axioms Found.ATR.full_pi11
#print axioms Found.ATR.hier_spec
#print axioms Found.ATR.full_atr
#print axioms Found.ATR.hier_unique
#print axioms Found.ATR.atr_comp
#print axioms Found.ATR.atr_aca

found/Ack1.lean

lean_all/found/Ack1.lean

/-
  T167, Ackermann set theory, part 1. found/Ack1 — the theory A* and the
  class structures over a level of the meta universe:
  * A* in the language ∈ (symbol 0) and M (symbol 1, "is a set"): class
    extensionality, heredity (members and subclasses of sets are sets),
    class comprehension for every formula, Ackermann's schema (for an
    M-free formula ψ with set parameters: if every x with ψ is a set, the
    collection of these x is a set), class foundation;
  * the structure on Aczel's V.{0}: classes are all trees, membership is
    Mem, sets are the members of a level V_α;
  * invariance of truth under bisimilarity; relativisation of L0 formulas
    to the sets; class extensionality, heredity, comprehension and
    foundation hold in every such structure.

  Lean 4 core only, hand-written proofs, no axioms.
  CIC dependence: impredicative Prop (separation by arbitrary formulas).
-/
import found.Refl3

namespace Found

namespace AK

open WF FO FOCT NBGc RF

/-! ## the theory -/

/-- members of sets are sets: ∀x∀y (M y → x ∈ y → M x) -/
def her1 : Fm := .all (.all (.imp (Mv 0) (.imp (m 1 0) (Mv 1))))

/-- subclasses of sets are sets: ∀x∀y (M y → ∀z (z ∈ x → z ∈ y) → M x) -/
def her2 : Fm := .all (.all (.imp (Mv 0) (.imp (.all (.imp (m 0 2) (m 0 1))) (Mv 1))))

/-- class comprehension: ∃Y ∀x (x ∈ Y ↔ M x ∧ ψ) -/
def compA (ψ : Fm) : Fm := .ex (.all (iffF (m 0 1) (.conj (Mv 0) (ren σc ψ))))

/-- the free variables 0, …, k−1 are sets -/
def guardImp : Nat → Fm → Fm
  | 0, φ => φ
  | k + 1, φ => guardImp k (.imp (Mv k) φ)

/-- Ackermann's schema for ψ(x, p₀ … p_{k−1}) without M:
  (all pᵢ sets) → ∀x (ψ → M x) → ∃y (M y ∧ ∀x (x ∈ y ↔ ψ)) -/
def ackAx (ψ : Fm) (k : Nat) : Fm :=
  guardImp k (.imp (.all (.imp ψ (Mv 0))) (.ex (.conj (Mv 0) (.all (iffF (m 0 1) (ren σc ψ))))))

inductive Ack : Fm → Prop where
  | ext : Ack extAx
  | h1 : Ack her1
  | h2 : Ack her2
  | comp (ψ : Fm) : Ack (compA ψ)
  | ack (ψ : Fm) (k : Nat) : L0 ψ → FV (k + 1) ψ → Ack (ackAx ψ k)
  | found : Ack cfoundAx

/-! ## the class structures on V -/

/-- ∈ is Mem, M is membership in the class S -/
def RA (S : V.{0} → Prop) : Nat → List V.{0} → Prop
  | 0, [] => False
  | 0, [_] => False
  | 0, [a, b] => Mem a b
  | 0, _ :: _ :: _ :: _ => False
  | 1, [] => False
  | 1, [a] => S a
  | 1, _ :: _ :: _ => False
  | _ + 2, _ => False

section S

variable (S : V.{0} → Prop) (hS : ∀ x y, Eq' x y → S x → S y)

def EqL : List V.{0} → List V.{0} → Prop
  | [], [] => True
  | a :: l, b :: k => Eq' a b ∧ EqL l k
  | [], _ :: _ => False
  | _ :: _, [] => False

include hS in
theorem RA_resp : (r : Nat) → (l k : List V.{0}) → EqL l k → RA S r l → RA S r k
  | 0, [], [], _, h => h
  | 0, [_], [_], _, h => h
  | 0, [_, _], [_, _], e, h => mem_resp_right (mem_resp_left e.1 h) e.2.1
  | 0, _ :: _ :: _ :: _, _ :: _ :: _ :: _, _, h => h
  | 1, [], [], _, h => h
  | 1, [_], [_], e, h => hS _ _ e.1 h
  | 1, _ :: _ :: _, _ :: _ :: _, _, h => h
  | _ + 2, _, _, _, h => h
  | 0, [], _ :: _, e, _ => e.elim
  | 0, _ :: _, [], e, _ => e.elim
  | 0, [_], _ :: _ :: _, e, _ => e.2.elim
  | 0, _ :: _ :: _, [_], e, _ => e.2.elim
  | 0, [_, _], _ :: _ :: _ :: _, e, _ => e.2.2.elim
  | 0, _ :: _ :: _ :: _, [_, _], e, _ => e.2.2.elim
  | 1, [], _ :: _, e, _ => e.elim
  | 1, _ :: _, [], e, _ => e.elim
  | 1, [_], _ :: _ :: _, e, _ => e.2.elim
  | 1, _ :: _ :: _, [_], e, _ => e.2.elim

theorem eqL_mapD {v w : Nat → V.{0}} (h : ∀ n, Eq' (v n) (w n)) : (l : List Nat) → EqL (mapD v l) (mapD w l)
  | [] => trivial
  | a :: l => ⟨h a, eqL_mapD h l⟩

include hS in
/-- truth in the class structure is invariant under bisimilarity -/
theorem satA_eq : (φ : Fm) → {v w : Nat → V.{0}} → (∀ n, Eq' (v n) (w n)) → Sat (RA S) φ v → Sat (RA S) φ w
  | .atom r l, _, _, h, s => RA_resp S hS r _ _ (eqL_mapD h l) s
  | .bot, _, _, _, s => s
  | .conj a b, _, _, h, s => ⟨satA_eq a h s.1, satA_eq b h s.2⟩
  | .disj a b, _, _, h, s => s.elim (fun x => Or.inl (satA_eq a h x)) (fun y => Or.inr (satA_eq b h y))
  | .imp a b, _, _, h, s => fun x => satA_eq b h (s (satA_eq a (fun n => eq_symm _ _ (h n)) x))
  | .all a, _, _, h, s => fun d => satA_eq a (ext_eq h d) (s d)
  | .ex a, _, _, h, s => match s with
    | ⟨d, x⟩ => ⟨d, satA_eq a (ext_eq h d) x⟩

/-- on L0 formulas the class structure is V -/
theorem satA_L0 : (φ : Fm) → L0 φ → ∀ v, (Sat (RA S) φ v ↔ Sat RV φ v)
  | .atom _ _, hl, v => match hl with
    | ⟨e0, a, b, e⟩ => by subst e0; subst e; exact Iff.rfl
  | .bot, _, _ => Iff.rfl
  | .conj a b, hl, v => ⟨fun s => ⟨(satA_L0 a hl.1 v).1 s.1, (satA_L0 b hl.2 v).1 s.2⟩,
      fun s => ⟨(satA_L0 a hl.1 v).2 s.1, (satA_L0 b hl.2 v).2 s.2⟩⟩
  | .disj a b, hl, v => ⟨fun s => s.elim (fun x => Or.inl ((satA_L0 a hl.1 v).1 x)) (fun y => Or.inr ((satA_L0 b hl.2 v).1 y)),
      fun s => s.elim (fun x => Or.inl ((satA_L0 a hl.1 v).2 x)) (fun y => Or.inr ((satA_L0 b hl.2 v).2 y))⟩
  | .imp a b, hl, v => ⟨fun s x => (satA_L0 b hl.2 v).1 (s ((satA_L0 a hl.1 v).2 x)),
      fun s x => (satA_L0 b hl.2 v).2 (s ((satA_L0 a hl.1 v).1 x))⟩
  | .all a, hl, v => ⟨fun s d => (satA_L0 a hl (ext v d)).1 (s d), fun s d => (satA_L0 a hl (ext v d)).2 (s d)⟩
  | .ex a, hl, v => ⟨fun ⟨d, s⟩ => ⟨d, (satA_L0 a hl (ext v d)).1 s⟩, fun ⟨d, s⟩ => ⟨d, (satA_L0 a hl (ext v d)).2 s⟩⟩

/-- the relativisation of an L0 formula to the sets is relativised truth -/
theorem satA_rel : (φ : Fm) → L0 φ → ∀ v, (Sat (RA S) (relE φ) v ↔ SatC S φ v)
  | .atom _ _, hl, v => match hl with
    | ⟨e0, a, b, e⟩ => by subst e0; subst e; exact Iff.rfl
  | .bot, _, _ => Iff.rfl
  | .conj a b, hl, v => ⟨fun s => ⟨(satA_rel a hl.1 v).1 s.1, (satA_rel b hl.2 v).1 s.2⟩,
      fun s => ⟨(satA_rel a hl.1 v).2 s.1, (satA_rel b hl.2 v).2 s.2⟩⟩
  | .disj a b, hl, v => ⟨fun s => s.elim (fun x => Or.inl ((satA_rel a hl.1 v).1 x)) (fun y => Or.inr ((satA_rel b hl.2 v).1 y)),
      fun s => s.elim (fun x => Or.inl ((satA_rel a hl.1 v).2 x)) (fun y => Or.inr ((satA_rel b hl.2 v).2 y))⟩
  | .imp a b, hl, v => ⟨fun s x => (satA_rel b hl.2 v).1 (s ((satA_rel a hl.1 v).2 x)),
      fun s x => (satA_rel b hl.2 v).2 (s ((satA_rel a hl.1 v).1 x))⟩
  | .all a, hl, v => ⟨fun s d hd => (satA_rel a hl (ext v d)).1 (s d hd), fun s d hd => (satA_rel a hl (ext v d)).2 (s d hd)⟩
  | .ex a, hl, v => ⟨fun ⟨d, hd, s⟩ => ⟨d, hd, (satA_rel a hl (ext v d)).1 s⟩,
      fun ⟨d, hd, s⟩ => ⟨d, hd, (satA_rel a hl (ext v d)).2 s⟩⟩

/-! ## the axioms other than Ackermann's schema -/

theorem ext_A (v : Nat → V.{0}) : Sat (RA S) extAx v := fun X Y h _ hm =>
  have e : Eq' X Y := extensionality X Y (fun z => ⟨(h z).1, (h z).2⟩)
  mem_resp_left e hm

theorem ext_eq0 (v : Nat → V.{0}) {x y : V.{0}} (e : Eq' x y) : ∀ n, Eq' (ext v x n) (ext v y n)
  | 0 => e
  | _ + 1 => eq_refl _

include hS in
theorem comp_A (ψ : Fm) (v : Nat → V.{0}) (lev : V.{0}) (hlev : ∀ x, S x ↔ Mem x lev) :
    Sat (RA S) (compA ψ) v :=
  let P : V.{0} → Prop := fun x => Sat (RA S) ψ (ext v x)
  have hP : IZF.Resp P := fun _ _ e h => satA_eq S hS ψ (ext_eq0 v e) h
  ⟨IZF.sep lev P, fun x =>
    have e : Sat (RA S) (ren σc ψ) (ext (ext v (IZF.sep lev P)) x) ↔ P x :=
      (sat_ren (RA S) ψ σc _).trans (sat_ext (RA S) ψ (fun n => match n with
        | 0 => rfl
        | _ + 1 => rfl))
    ⟨fun hm => match (IZF.sep_spec lev P hP x).1 hm with
      | ⟨h1, h2⟩ => ⟨(hlev x).2 h1, e.2 h2⟩,
     fun ⟨h1, h2⟩ => (IZF.sep_spec lev P hP x).2 ⟨(hlev x).1 h1, e.1 h2⟩⟩⟩

theorem her1_A (lev : V.{0}) (hlev : ∀ x, S x ↔ Mem x (Vlev lev)) (v : Nat → V.{0}) : Sat (RA S) her1 v :=
  fun x y hy hxy => (hlev x).2 (trans_Vlev lev y x hxy ((hlev y).1 hy))

theorem her2_A (lev : V.{0}) (hlev : ∀ x, S x ↔ Mem x (Vlev lev)) (v : Nat → V.{0}) : Sat (RA S) her2 v :=
  fun x y hy hsub => (hlev x).2 (match lev, (hlev y).1 hy with
    | .sup I f, hy' => match (mem_Vlev I f y).1 hy' with
      | ⟨i, hi⟩ => (mem_Vlev I f x).2 ⟨i, fun z hz => hi z (hsub z hz)⟩)

include hS in
theorem found_A (K : CP.Classical') (v : Nat → V.{0}) : Sat (RA S) cfoundAx v := fun X ⟨x0, hx0, hm0⟩ =>
  CP.dne K _ (fun hno =>
    have all_out : ∀ x, ¬ (S x ∧ Mem x X) := mem_induction (fun x => ¬ (S x ∧ Mem x X))
      (fun x y e h hy => h ⟨hS y x (eq_symm _ _ e) hy.1, mem_resp_left (eq_symm _ _ e) hy.2⟩)
      (fun x ih hx => hno ⟨x, hx.1, hx.2, fun y hy hyx hyX => ih y hyx ⟨hy, hyX⟩⟩)
    all_out x0 ⟨hx0, hm0⟩)

end S

end AK

end Found

#print axioms Found.AK.RA_resp
#print axioms Found.AK.satA_eq
#print axioms Found.AK.satA_L0
#print axioms Found.AK.satA_rel
#print axioms Found.AK.ext_A
#print axioms Found.AK.comp_A
#print axioms Found.AK.her1_A
#print axioms Found.AK.her2_A
#print axioms Found.AK.found_A

found/Ack2.lean

lean_all/found/Ack2.lean

/-
  T167, Ackermann set theory, part 2. found/Ack2 — Ackermann's schema in a
  reflecting level, and the theorem in the universe-hierarchy point:
  * the formula θ_ψ = ∃y ∀x (x ∈ y ↔ ψ) and its free variables;
  * if V_α reflects θ_ψ and ψ (found/Refl3), Ackermann's schema for ψ holds
    in the class structure whose sets are V_α: the class {x | ψ} of sets
    is a set of V by separation, so θ_ψ holds in V and hence in V_α;
  * every derivation from A* uses finitely many instances of the schema;
    reflecting them (with the conclusion) gives a model of the instances
    used, so: if A* derives φ relativised to sets, φ is true in Aczel's
    universe V.{0} (for every sentence φ of set theory).

  Lean 4 core only, hand-written proofs, no axioms.
  Classical point: K (Type) and K1 (choice on Type 1), as parameters.
-/
import found.Ack1

namespace Found

namespace AK

open WF FO FOCT NBGc RF

/-! ## free variables of renamings -/

theorem allLt_ren {k k' : Nat} {ρ : Nat → Nat} (h : ∀ n, n < k → ρ n < k') : (l : List Nat) → AllLt k l →
    AllLt k' (mapL ρ l)
  | [], _ => trivial
  | _ :: l, hl => ⟨h _ hl.1, allLt_ren h l hl.2⟩

theorem up_lt {k k' : Nat} {ρ : Nat → Nat} (h : ∀ n, n < k → ρ n < k') : ∀ n, n < k + 1 → up ρ n < k' + 1
  | 0, _ => Nat.zero_lt_succ _
  | n + 1, hn => Nat.succ_lt_succ (h n (Nat.lt_of_succ_lt_succ hn))

theorem fv_ren : (φ : Fm) → {k k' : Nat} → {ρ : Nat → Nat} → (∀ n, n < k → ρ n < k') → FV k φ → FV k' (ren ρ φ)
  | .atom _ l, _, _, _, h, hf => allLt_ren h l hf
  | .bot, _, _, _, _, _ => trivial
  | .conj a b, _, _, _, h, hf => ⟨fv_ren a h hf.1, fv_ren b h hf.2⟩
  | .disj a b, _, _, _, h, hf => ⟨fv_ren a h hf.1, fv_ren b h hf.2⟩
  | .imp a b, _, _, _, h, hf => ⟨fv_ren a h hf.1, fv_ren b h hf.2⟩
  | .all a, _, _, _, h, hf => fv_ren a (up_lt h) hf
  | .ex a, _, _, _, h, hf => fv_ren a (up_lt h) hf

theorem σc_lt (k : Nat) : ∀ n, n < k + 1 → σc n < k + 2
  | 0, _ => Nat.zero_lt_succ _
  | _ + 1, hn => Nat.succ_lt_succ (Nat.succ_lt_succ (Nat.lt_of_succ_lt_succ hn))

/-- θ_ψ = ∃y ∀x (x ∈ y ↔ ψ) -/
def thetaA (ψ : Fm) : Fm := .ex (.all (iffF (m 0 1) (ren σc ψ)))

theorem l0_thetaA {ψ : Fm} (h : L0 ψ) : L0 (thetaA ψ) := l0_iff (l0_m 0 1) (l0_ren σc ψ h)

/-! ## guards -/

theorem guard_intro {D : Type _} (R : Nat → List D → Prop) : (k : Nat) → (φ : Fm) → (v : Nat → D) →
    ((∀ i, i < k → R 1 [v i]) → Sat R φ v) → Sat R (guardImp k φ) v
  | 0, _, _, h => h (fun i hi => absurd hi (Nat.not_lt_zero i))
  | k + 1, φ, v, h => guard_intro R k (.imp (Mv k) φ) v (fun hk hkk => h (fun i hi =>
      (Nat.lt_or_ge i k).elim (hk i) (fun hge => by
        have e : i = k := Nat.le_antisymm (Nat.le_of_lt_succ hi) hge
        rw [e]
        exact hkk)))

/-! ## Ackermann's schema in a reflecting level -/

section Lev

variable (α : V.{0})

def SL : V.{0} → Prop := fun x => Mem x (Vlev α)

theorem SL_resp : ∀ x y, Eq' x y → SL α x → SL α y := fun _ _ e h => mem_resp_left e h

/-- α reflects a formula -/
def Refl (φ : Fm) : Prop := ∀ v : Nat → V.{0}, (∀ n, SL α (v n)) → (SatC (SL α) φ v ↔ Sat RV φ v)

theorem ack_A (hE : SL α vEmpty) {ψ : Fm} {k : Nat} (hl : L0 ψ) (hf : FV (k + 1) ψ)
    (r1 : Refl α (thetaA ψ)) (r2 : Refl α (ren σc ψ)) (v : Nat → V.{0}) : Sat (RA (SL α)) (ackAx ψ k) v :=
  guard_intro (RA (SL α)) k _ v (fun hg hall => by
    let v' : Nat → V.{0} := fun n => match Nat.decLt n k with
      | isTrue _ => v n
      | isFalse _ => vEmpty
    have hv' : ∀ n, SL α (v' n) := fun n => by
      show SL α (match Nat.decLt n k with
        | isTrue _ => v n
        | isFalse _ => vEmpty)
      match Nat.decLt n k with
      | isTrue hn => exact hg n hn
      | isFalse _ => exact hE
    have hag : ∀ n, n < k → v n = v' n := fun n hn => by
      show v n = (match Nat.decLt n k with
        | isTrue _ => v n
        | isFalse _ => vEmpty)
      match Nat.decLt n k with
      | isTrue _ => rfl
      | isFalse h' => exact absurd hn h'
    -- ψ under v and v'
    have psi_iff : ∀ x, Sat (RA (SL α)) ψ (ext v x) ↔ Sat RV ψ (ext v' x) := fun x =>
      (satA_L0 (SL α) ψ hl _).trans
        ⟨sat_agreeU RV ψ (k + 1) hf (ext_agreeU hag x),
         sat_agreeU RV ψ (k + 1) hf (ext_agreeU (fun n hn => (hag n hn).symm) x)⟩
    have ren_iff : ∀ {D : Type _} (R : Nat → List D → Prop) (w : Nat → D) (y x : D),
        Sat R (ren σc ψ) (ext (ext w y) x) ↔ Sat R ψ (ext w x) := fun R w y x =>
      (sat_ren R ψ σc _).trans (sat_ext R ψ (fun n => match n with
        | 0 => rfl
        | _ + 1 => rfl))
    -- in V, θ_ψ holds (with the parameters v')
    let P : V.{0} → Prop := fun x => Sat RV ψ (ext v' x)
    have hP : IZF.Resp P := fun _ _ e h => sat_eqV ψ hl (ext_eq0 v' e) h
    have inS : ∀ x, P x → SL α x := fun x h => hall x ((psi_iff x).2 h)
    have thV : Sat RV (thetaA ψ) v' := ⟨IZF.sep (Vlev α) P, fun x =>
      ⟨fun hm => (ren_iff RV v' _ x).2 ((IZF.sep_spec (Vlev α) P hP x).1 hm).2,
       fun h => (IZF.sep_spec (Vlev α) P hP x).2 ⟨inS x ((ren_iff RV v' _ x).1 h), (ren_iff RV v' _ x).1 h⟩⟩⟩
    -- so θ_ψ holds in V_α
    match (r1 v' hv').2 thV with
    | ⟨y, hy, hyx⟩ =>
      have key : ∀ x, SL α x → (Mem x y ↔ Sat RV ψ (ext v' x)) := fun x hx =>
        have rr : SatC (SL α) (ren σc ψ) (ext (ext v' y) x) ↔ Sat RV ψ (ext v' x) :=
          (r2 _ (C_ext (C_ext hv' hy) hx)).trans (ren_iff RV v' y x)
        ⟨fun hm => rr.1 ((hyx x hx).1 hm), fun h => (hyx x hx).2 (rr.2 h)⟩
      exact ⟨y, hy, fun x =>
        ⟨fun hm => (ren_iff (RA (SL α)) v y x).2 ((psi_iff x).2
            ((key x (trans_Vlev α y x hm hy)).1 hm)),
         fun h =>
          have h' := (ren_iff (RA (SL α)) v y x).1 h
          (key x (hall x h')).2 ((psi_iff x).1 h')⟩⟩)

end Lev

/-! ## finitely many instances, and the theorem -/

def gapp {α : Type} : List α → List α → List α
  | [], k => k
  | a :: l, k => a :: gapp l k

theorem mem_gapp_l {α : Type} {x : α} : {l : List α} → (k : List α) → x ∈ l → x ∈ gapp l k
  | _ :: _, _, .head _ => .head _
  | _ :: _, k, .tail _ m => .tail _ (mem_gapp_l k m)

theorem mem_gapp_r {α : Type} {x : α} : (l : List α) → {k : List α} → x ∈ k → x ∈ gapp l k
  | [], _, m => m
  | _ :: l, _, m => .tail _ (mem_gapp_r l m)

theorem mem_gapp_inv {α : Type} {x : α} : (l : List α) → {k : List α} → x ∈ gapp l k → x ∈ l ∨ x ∈ k
  | [], _, m => .inr m
  | _ :: l, _, m => by
    cases m with
    | head => exact .inl (.head _)
    | tail _ m' => exact (mem_gapp_inv l m').elim (fun h => .inl (.tail _ h)) .inr

/-- the axioms of A* with the schema restricted to the listed instances -/
def AckIn (L : List (Fm × Nat)) (b : Fm) : Prop :=
  b = extAx ∨ b = her1 ∨ b = her2 ∨ (∃ ψ, b = compA ψ) ∨ b = cfoundAx ∨
    (∃ ψ k, (ψ, k) ∈ L ∧ L0 ψ ∧ FV (k + 1) ψ ∧ b = ackAx ψ k)

theorem ackIn_mono {L L' : List (Fm × Nat)} (hs : ∀ p, p ∈ L → p ∈ L') {b : Fm} (h : AckIn L b) : AckIn L' b :=
  match h with
  | .inl e => .inl e
  | .inr (.inl e) => .inr (.inl e)
  | .inr (.inr (.inl e)) => .inr (.inr (.inl e))
  | .inr (.inr (.inr (.inl e))) => .inr (.inr (.inr (.inl e)))
  | .inr (.inr (.inr (.inr (.inl e)))) => .inr (.inr (.inr (.inr (.inl e))))
  | .inr (.inr (.inr (.inr (.inr ⟨ψ, k, m, h1, h2, e⟩)))) => .inr (.inr (.inr (.inr (.inr ⟨ψ, k, hs _ m, h1, h2, e⟩))))

def L0L (L : List (Fm × Nat)) : Prop := ∀ p, p ∈ L → L0 p.1

theorem ack_in {b : Fm} (h : Ack b) : ∃ L, L0L L ∧ AckIn L b := by
  cases h with
  | ext => exact ⟨[], (fun _ m => nomatch m), .inl rfl⟩
  | h1 => exact ⟨[], (fun _ m => nomatch m), .inr (.inl rfl)⟩
  | h2 => exact ⟨[], (fun _ m => nomatch m), .inr (.inr (.inl rfl))⟩
  | comp ψ => exact ⟨[], (fun _ m => nomatch m), .inr (.inr (.inr (.inl ⟨ψ, rfl⟩)))⟩
  | ack ψ k hl hf =>
    exact ⟨[(ψ, k)], fun p m => by cases m with
      | head => exact hl
      | tail _ m' => exact (nomatch m'), .inr (.inr (.inr (.inr (.inr ⟨ψ, k, .head _, hl, hf, rfl⟩))))⟩
  | found => exact ⟨[], (fun _ m => nomatch m), .inr (.inr (.inr (.inr (.inl rfl))))⟩

theorem extract : (Δ : List Fm) → CtxT Ack Δ → ∃ L, L0L L ∧ ∀ δ, δ ∈ Δ → InT (AckIn L) δ
  | [], _ => ⟨[], (fun _ m => nomatch m), fun _ m => nomatch m⟩
  | δ :: Δ, h =>
    match extract Δ (fun x m => h x (.tail _ m)), h δ (.head _) with
    | ⟨L1, h1, hΔ⟩, ⟨j, a, ha, e⟩ =>
      match ha with
      | .inl hl => ⟨L1, h1, fun x m => by
          cases m with
          | head => exact ⟨j, a, .inl hl, e⟩
          | tail _ m' => exact hΔ x m'⟩
      | .inr ⟨b, σ, hb, ea⟩ => match ack_in hb with
        | ⟨L2, h2, hin⟩ => ⟨gapp L2 L1, fun p m => (mem_gapp_inv L2 m).elim (h2 p) (h1 p), fun x m => by
            cases m with
            | head => exact ⟨j, a, .inr ⟨b, σ, ackIn_mono (fun _ m => mem_gapp_l L1 m) hin, ea⟩, e⟩
            | tail _ m' => exact match hΔ x m' with
              | ⟨j', a', ha', e'⟩ => ⟨j', a', ha'.elim .inl (fun ⟨b', σ', hb', ea'⟩ =>
                  .inr ⟨b', σ', ackIn_mono (fun _ m => mem_gapp_r L2 m) hb', ea'⟩), e'⟩⟩

/-- the formulas to reflect for the listed instances -/
def rlist : List (Fm × Nat) → List Fm
  | [] => []
  | p :: l => thetaA p.1 :: ren σc p.1 :: rlist l

theorem rlist_mem {ψ : Fm} {k : Nat} : {L : List (Fm × Nat)} → (ψ, k) ∈ L → thetaA ψ ∈ rlist L ∧ ren σc ψ ∈ rlist L
  | _ :: _, .head _ => ⟨.head _, .tail _ (.head _)⟩
  | _ :: _, .tail _ m => match rlist_mem m with | ⟨h1, h2⟩ => ⟨.tail _ (.tail _ h1), .tail _ (.tail _ h2)⟩

theorem rlist_l0 : (L : List (Fm × Nat)) → L0L L → ∀ φ, φ ∈ rlist L → L0 φ
  | [], _, _, m => nomatch m
  | p :: l, h, φ, m => by
    cases m with
    | head => exact l0_thetaA (h p (.head _))
    | tail _ m' =>
      cases m' with
      | head => exact l0_ren σc p.1 (h p (.head _))
      | tail _ m'' => exact rlist_l0 l (fun q mq => h q (.tail _ mq)) φ m''

theorem clo_sat {D : Type _} (R : Nat → List D → Prop) {a : Fm} (h : ∀ w, Sat R a w) :
    (j : Nat) → ∀ w, Sat R (clo j a) w
  | 0, w => h w
  | j + 1, w => fun d => clo_sat R h j (ext w d)

/-- Ackermann's A* proves only truths of Aczel's universe (universe-hierarchy point) -/
theorem ack_meta (K : CP.Classical') (K1 : Classical1) {φ : Fm} (hl : L0 φ) (hf : FV 0 φ)
    (h : PC Ack [] (relE φ)) (v : Nat → V.{0}) : Sat RV φ v :=
  match h with
  | ⟨Δ, cΔ, d⟩ => match extract Δ cΔ with
    | ⟨L, hL, hΔ⟩ =>
      match reflection K K1 (φ :: rlist L) (fun ψ m => by
          cases m with
          | head => exact hl
          | tail _ m' => exact rlist_l0 L hL ψ m') (sgl vEmpty) with
      | ⟨α, hsub, hR⟩ =>
        have hE : SL α vEmpty := hsub _ (mem_sgl_lev vEmpty)
        have hb : ∀ b, AckIn L b → ∀ w, Sat (RA (SL α)) b w := fun b hb w => match hb with
          | .inl e => by rw [e]; exact ext_A (SL α) w
          | .inr (.inl e) => by rw [e]; exact her1_A (SL α) α (fun _ => Iff.rfl) w
          | .inr (.inr (.inl e)) => by rw [e]; exact her2_A (SL α) α (fun _ => Iff.rfl) w
          | .inr (.inr (.inr (.inl ⟨ψ, e⟩))) => by
            rw [e]; exact comp_A (SL α) (SL_resp α) ψ w (Vlev α) (fun _ => Iff.rfl)
          | .inr (.inr (.inr (.inr (.inl e)))) => by rw [e]; exact found_A (SL α) (SL_resp α) K w
          | .inr (.inr (.inr (.inr (.inr ⟨ψ, k, m, h1, h2, e⟩)))) => by
            rw [e]
            exact ack_A α hE h1 h2 (hR _ (.tail _ (rlist_mem m).1)) (hR _ (.tail _ (rlist_mem m).2)) w
        have hmod : ∀ δ, δ ∈ Δ → ∀ w, Sat (RA (SL α)) δ w := fun δ m w => match hΔ δ m with
          | ⟨j, a, ha, e⟩ => by
            rw [e]
            exact clo_sat _ (fun w' => match ha with
              | .inl ⟨b, eb⟩ => by rw [eb]; exact K.lem (Sat (RA (SL α)) b w')
              | .inr ⟨b, σ, hbb, eb⟩ => by rw [eb]; exact (sat_ren _ b σ w').2 (hb b hbb _)) j w
        let w0 : Nat → V.{0} := fun _ => vEmpty
        have s : Sat (RA (SL α)) (relE φ) w0 := sound (RA (SL α)) d w0 (fun γ m =>
          (mem_app_inv' Δ [] m).elim (fun mΔ => hmod γ mΔ w0) (fun m' => nomatch m'))
        have s2 : Sat RV φ w0 := (hR φ (.head _) w0 (fun _ => hE)).1 ((satA_rel (SL α) φ hl w0).1 s)
        sat_agreeU RV φ 0 hf (fun n hn => absurd hn (Nat.not_lt_zero n)) s2

end AK

end Found

#print axioms Found.AK.fv_ren
#print axioms Found.AK.l0_thetaA
#print axioms Found.AK.guard_intro
#print axioms Found.AK.ack_A
#print axioms Found.AK.extract
#print axioms Found.AK.ack_meta

found/AckD.lean

lean_all/found/AckD.lean

/-
  T167, Ackermann set theory, part 3. found/AckD — the class structure over
  an arbitrary model D of ZF:
  * classes are the elements of D, membership is D's, sets are the members
    of a set Y of D (later a reflecting level V_α);
  * on L0 formulas it is D; relativised to the sets it is truth relativised
    to Y;
  * formulas of the class language translate into L0 formulas with Y as an
    extra variable; hence class comprehension (separation in D);
  * extensionality, heredity (Y a level), comprehension and class
    foundation hold.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.IZ10
import found.Ack2

namespace Found

namespace AKD

open FO FOCT NBGc RF IZ AK

variable (M : IZ.ZM)

/-- the class structure: 0 = membership, 1 = membership in Y -/
def RAD (Y : M.D) : Nat → List M.D → Prop
  | 0, [] => False
  | 0, [_] => False
  | 0, [a, b] => In M a b
  | 0, _ :: _ :: _ :: _ => False
  | 1, [] => False
  | 1, [a] => In M a Y
  | 1, _ :: _ :: _ => False
  | _ + 2, _ => False

variable (Y : M.D)

theorem satD_L0 : (φ : Fm) → L0 φ → ∀ v, (Sat (RAD M Y) φ v ↔ Sat M.R φ v)
  | .atom _ _, hl, v => match hl with
    | ⟨e0, a, b, e⟩ => by subst e0; subst e; exact Iff.rfl
  | .bot, _, _ => Iff.rfl
  | .conj a b, hl, v => ⟨fun s => ⟨(satD_L0 a hl.1 v).1 s.1, (satD_L0 b hl.2 v).1 s.2⟩,
      fun s => ⟨(satD_L0 a hl.1 v).2 s.1, (satD_L0 b hl.2 v).2 s.2⟩⟩
  | .disj a b, hl, v => ⟨fun s => s.elim (fun x => Or.inl ((satD_L0 a hl.1 v).1 x)) (fun y => Or.inr ((satD_L0 b hl.2 v).1 y)),
      fun s => s.elim (fun x => Or.inl ((satD_L0 a hl.1 v).2 x)) (fun y => Or.inr ((satD_L0 b hl.2 v).2 y))⟩
  | .imp a b, hl, v => ⟨fun s x => (satD_L0 b hl.2 v).1 (s ((satD_L0 a hl.1 v).2 x)),
      fun s x => (satD_L0 b hl.2 v).2 (s ((satD_L0 a hl.1 v).1 x))⟩
  | .all a, hl, v => ⟨fun s d => (satD_L0 a hl (ext v d)).1 (s d), fun s d => (satD_L0 a hl (ext v d)).2 (s d)⟩
  | .ex a, hl, v => ⟨fun ⟨d, s⟩ => ⟨d, (satD_L0 a hl (ext v d)).1 s⟩, fun ⟨d, s⟩ => ⟨d, (satD_L0 a hl (ext v d)).2 s⟩⟩

theorem satD_rel : (φ : Fm) → L0 φ → ∀ v, (Sat (RAD M Y) (relE φ) v ↔ SatG M.R (fun x => In M x Y) φ v)
  | .atom _ _, hl, v => match hl with
    | ⟨e0, a, b, e⟩ => by subst e0; subst e; exact Iff.rfl
  | .bot, _, _ => Iff.rfl
  | .conj a b, hl, v => ⟨fun s => ⟨(satD_rel a hl.1 v).1 s.1, (satD_rel b hl.2 v).1 s.2⟩,
      fun s => ⟨(satD_rel a hl.1 v).2 s.1, (satD_rel b hl.2 v).2 s.2⟩⟩
  | .disj a b, hl, v => ⟨fun s => s.elim (fun x => Or.inl ((satD_rel a hl.1 v).1 x)) (fun y => Or.inr ((satD_rel b hl.2 v).1 y)),
      fun s => s.elim (fun x => Or.inl ((satD_rel a hl.1 v).2 x)) (fun y => Or.inr ((satD_rel b hl.2 v).2 y))⟩
  | .imp a b, hl, v => ⟨fun s x => (satD_rel b hl.2 v).1 (s ((satD_rel a hl.1 v).2 x)),
      fun s x => (satD_rel b hl.2 v).2 (s ((satD_rel a hl.1 v).1 x))⟩
  | .all a, hl, v => ⟨fun s d hd => (satD_rel a hl (ext v d)).1 (s d hd), fun s d hd => (satD_rel a hl (ext v d)).2 (s d hd)⟩
  | .ex a, hl, v => ⟨fun ⟨d, hd, s⟩ => ⟨d, hd, (satD_rel a hl (ext v d)).1 s⟩,
      fun ⟨d, hd, s⟩ => ⟨d, hd, (satD_rel a hl (ext v d)).2 s⟩⟩

/-! ## translating the class language into L0 (Y as variable p) -/

def trA : Fm → Nat → Fm
  | .atom 0 [], _ => .bot
  | .atom 0 [_], _ => .bot
  | .atom 0 [a, b], _ => m a b
  | .atom 0 (_ :: _ :: _ :: _), _ => .bot
  | .atom 1 [], _ => .bot
  | .atom 1 [i], p => m i p
  | .atom 1 (_ :: _ :: _), _ => .bot
  | .atom (_ + 2) _, _ => .bot
  | .bot, _ => .bot
  | .conj a b, p => .conj (trA a p) (trA b p)
  | .disj a b, p => .disj (trA a p) (trA b p)
  | .imp a b, p => .imp (trA a p) (trA b p)
  | .all a, p => .all (trA a (p + 1))
  | .ex a, p => .ex (trA a (p + 1))

theorem l0_trA : (ψ : Fm) → (p : Nat) → L0 (trA ψ p)
  | .atom 0 [], _ => trivial
  | .atom 0 [_], _ => trivial
  | .atom 0 [a, b], _ => l0_m a b
  | .atom 0 (_ :: _ :: _ :: _), _ => trivial
  | .atom 1 [], _ => trivial
  | .atom 1 [i], p => l0_m i p
  | .atom 1 (_ :: _ :: _), _ => trivial
  | .atom (_ + 2) _, _ => trivial
  | .bot, _ => trivial
  | .conj a b, p => ⟨l0_trA a p, l0_trA b p⟩
  | .disj a b, p => ⟨l0_trA a p, l0_trA b p⟩
  | .imp a b, p => ⟨l0_trA a p, l0_trA b p⟩
  | .all a, p => l0_trA a (p + 1)
  | .ex a, p => l0_trA a (p + 1)

theorem sat_trA : (ψ : Fm) → (p : Nat) → ∀ v : Nat → M.D, v p = Y → (Sat (RAD M Y) ψ v ↔ Sat M.R (trA ψ p) v)
  | .atom 0 [], _, _, _ => Iff.rfl
  | .atom 0 [_], _, _, _ => Iff.rfl
  | .atom 0 [_, _], _, _, _ => Iff.rfl
  | .atom 0 (_ :: _ :: _ :: _), _, _, _ => Iff.rfl
  | .atom 1 [], _, _, _ => Iff.rfl
  | .atom 1 [i], p, v, hp => by
    show In M (v i) Y ↔ In M (v i) (v p)
    rw [hp]
  | .atom 1 (_ :: _ :: _), _, _, _ => Iff.rfl
  | .atom (_ + 2) _, _, _, _ => Iff.rfl
  | .bot, _, _, _ => Iff.rfl
  | .conj a b, p, v, hp => ⟨fun s => ⟨(sat_trA a p v hp).1 s.1, (sat_trA b p v hp).1 s.2⟩,
      fun s => ⟨(sat_trA a p v hp).2 s.1, (sat_trA b p v hp).2 s.2⟩⟩
  | .disj a b, p, v, hp => ⟨fun s => s.elim (fun x => Or.inl ((sat_trA a p v hp).1 x)) (fun y => Or.inr ((sat_trA b p v hp).1 y)),
      fun s => s.elim (fun x => Or.inl ((sat_trA a p v hp).2 x)) (fun y => Or.inr ((sat_trA b p v hp).2 y))⟩
  | .imp a b, p, v, hp => ⟨fun s x => (sat_trA b p v hp).1 (s ((sat_trA a p v hp).2 x)),
      fun s x => (sat_trA b p v hp).2 (s ((sat_trA a p v hp).1 x))⟩
  | .all a, p, v, hp => ⟨fun s d => (sat_trA a (p + 1) (ext v d) hp).1 (s d),
      fun s d => (sat_trA a (p + 1) (ext v d) hp).2 (s d)⟩
  | .ex a, p, v, hp => ⟨fun ⟨d, s⟩ => ⟨d, (sat_trA a (p + 1) (ext v d) hp).1 s⟩,
      fun ⟨d, s⟩ => ⟨d, (sat_trA a (p + 1) (ext v d) hp).2 s⟩⟩

/-! ## the axioms other than the schema -/

theorem ext_AD (v : Nat → M.D) : Sat (RAD M Y) extAx v := fun X Y' h Z hm =>
  IZ.extD M X Y' Z (fun u => ⟨(h u).1, (h u).2⟩) hm

theorem her1_AD (K : CP.Classical') {α : M.D} (hα : Ord M α) (hY : Vl M α Y) (v : Nat → M.D) :
    Sat (RAD M Y) her1 v := fun x y hy hxy => vl_trans M K hα hY y x hy hxy

theorem her2_AD (K : CP.Classical') {α : M.D} (hY : Vl M α Y) (v : Nat → M.D) : Sat (RAD M Y) her2 v :=
  fun _ _ hy hsub => vl_sub M K hY hy hsub

theorem comp_AD (ψ : Fm) (v : Nat → M.D) : Sat (RAD M Y) (compA ψ) v :=
  let p := bound ψ + 1
  let v' := upd M v (bound ψ) Y
  have hp : ∀ x, ext v' x p = Y := fun _ => upd_eq M v (bound ψ) Y
  have agree : ∀ x, Sat (RAD M Y) ψ (ext v x) ↔ Sat (RAD M Y) ψ (ext v' x) := fun x =>
    have h : ∀ n, n < bound ψ → ext v x n = ext v' x n := fun n hn => match n with
      | 0 => rfl
      | _ + 1 => (upd_ne M v Y (Nat.ne_of_lt (Nat.lt_of_succ_lt hn))).symm
    ⟨sat_agreeU _ ψ _ (fv_bound ψ) h, sat_agreeU _ ψ _ (fv_bound ψ) (fun n hn => (h n hn).symm)⟩
  match IZ.sep M (P := fun u => Sat M.R (trA ψ p) u) ⟨trA ψ p, l0_trA ψ p, fun _ => Iff.rfl⟩ v' Y with
  | ⟨Yc, hYc⟩ => ⟨Yc, fun x =>
    have e : Sat (RAD M Y) (ren σc ψ) (ext (ext v Yc) x) ↔ Sat (RAD M Y) ψ (ext v x) :=
      (sat_ren _ ψ σc _).trans (sat_ext _ ψ (fun n => match n with
        | 0 => rfl
        | _ + 1 => rfl))
    have e2 : Sat (RAD M Y) ψ (ext v x) ↔ Sat M.R (trA ψ p) (ext v' x) :=
      (agree x).trans (sat_trA M Y ψ p (ext v' x) (hp x))
    ⟨fun hm => match (hYc x).1 hm with
      | ⟨h1, h2⟩ => ⟨h1, e.2 (e2.2 h2)⟩,
     fun ⟨h1, h2⟩ => (hYc x).2 ⟨h1, e2.1 (e.1 h2)⟩⟩⟩

theorem found_AD (v : Nat → M.D) : Sat (RAD M Y) cfoundAx v := fun X ⟨x0, hx0, hm0⟩ =>
  let par : Nat → M.D := fun n => match n with
    | 0 => Y
    | _ + 1 => X
  match IZ.found M (P := fun u => In M (u 0) (u 1) ∧ In M (u 0) (u 2))
      (def_and M (def_mem M 0 1) (def_mem M 0 2)) par ⟨x0, hx0, hm0⟩ with
  | ⟨x, ⟨h1, h2⟩, hmin⟩ => ⟨x, h1, h2, fun y hy hyx hyX => hmin y hyx ⟨hy, hyX⟩⟩

/-! ## Ackermann's schema in a reflecting level -/

theorem ack_AD (hEY : ∃ e, In M e Y) (htr : ∀ x y, In M y Y → In M x y → In M x Y) {ψ : Fm} {k : Nat}
    (hl : L0 ψ) (hf : FV (k + 1) ψ)
    (r1 : ∀ v : Nat → M.D, (∀ n, In M (v n) Y) → (SatG M.R (fun x => In M x Y) (thetaA ψ) v ↔ Sat M.R (thetaA ψ) v))
    (r2 : ∀ v : Nat → M.D, (∀ n, In M (v n) Y) → (SatG M.R (fun x => In M x Y) (ren σc ψ) v ↔ Sat M.R (ren σc ψ) v))
    (v : Nat → M.D) : Sat (RAD M Y) (ackAx ψ k) v :=
  match hEY with
  | ⟨e0, he0⟩ => guard_intro (RAD M Y) k _ v (fun hg hall => by
    let v' : Nat → M.D := mixk M k v (fun _ => e0)
    have hv' : ∀ n, In M (v' n) Y := fun n => (Nat.lt_or_ge n k).elim
      (fun h => by show In M (mixk M k v (fun _ => e0) n) Y; rw [mixk_lt M k v _ h]; exact hg n h)
      (fun h => by show In M (mixk M k v (fun _ => e0) n) Y; rw [mixk_ge M k v _ h]; exact he0)
    have hag : ∀ n, n < k → v n = v' n := fun n hn => (mixk_lt M k v (fun _ => e0) hn).symm
    have psi_iff : ∀ x, Sat (RAD M Y) ψ (ext v x) ↔ Sat M.R ψ (ext v' x) := fun x =>
      (satD_L0 M Y ψ hl _).trans
        ⟨sat_agreeU M.R ψ (k + 1) hf (ext_agreeU hag x),
         sat_agreeU M.R ψ (k + 1) hf (ext_agreeU (fun n hn => (hag n hn).symm) x)⟩
    have ren_iff : ∀ {D : Type} (R : Nat → List D → Prop) (w : Nat → D) (y x : D),
        Sat R (ren σc ψ) (ext (ext w y) x) ↔ Sat R ψ (ext w x) := fun R w y x =>
      (sat_ren R ψ σc _).trans (sat_ext R ψ (fun n => match n with
        | 0 => rfl
        | _ + 1 => rfl))
    have inY : ∀ x, Sat M.R ψ (ext v' x) → In M x Y := fun x h => hall x ((psi_iff x).2 h)
    match IZ.sep M (P := fun u => Sat M.R ψ u) ⟨ψ, hl, fun _ => Iff.rfl⟩ v' Y with
    | ⟨y0, hy0⟩ =>
      have thV : Sat M.R (thetaA ψ) v' := ⟨y0, fun x =>
        ⟨fun hm => (ren_iff M.R v' _ x).2 ((hy0 x).1 hm).2,
         fun h => (hy0 x).2 ⟨inY x ((ren_iff M.R v' _ x).1 h), (ren_iff M.R v' _ x).1 h⟩⟩⟩
      match (r1 v' hv').2 thV with
      | ⟨y, hy, hyx⟩ =>
        have key : ∀ x, In M x Y → (In M x y ↔ Sat M.R ψ (ext v' x)) := fun x hx =>
          have rr : SatG M.R (fun x => In M x Y) (ren σc ψ) (ext (ext v' y) x) ↔ Sat M.R ψ (ext v' x) :=
            (r2 _ (CG_ext (fun x => In M x Y) (CG_ext (fun x => In M x Y) hv' hy) hx)).trans (ren_iff M.R v' y x)
          ⟨fun hm => rr.1 ((hyx x hx).1 hm), fun h => (hyx x hx).2 (rr.2 h)⟩
        exact ⟨y, hy, fun x =>
          ⟨fun hm => (ren_iff (RAD M Y) v y x).2 ((psi_iff x).2 ((key x (htr x y hy hm)).1 hm)),
           fun h =>
            have h' := (ren_iff (RAD M Y) v y x).1 h
            (key x (hall x h')).2 ((psi_iff x).1 h')⟩⟩)

/-- Ackermann's set theory A* is conservative over ZF -/
theorem ack_zf (K : CP.Classical') {φ : Fm} (hl : L0 φ) (hf : FV 0 φ) (h : PC Ack [] (relE φ)) :
    PC (ZF B0) [] φ :=
  completenessT (ZF B0) K (fun D R v hM _ =>
    let M : IZ.ZM := ⟨D, R, hM, v 0⟩
    match h with
    | ⟨Δ, cΔ, d⟩ => match AK.extract Δ cΔ with
      | ⟨L, hL, hΔ⟩ =>
        match reflection_D M K (φ :: rlist L) (fun ψ m => by
            cases m with
            | head => exact hl
            | tail _ m' => exact rlist_l0 L hL ψ m') with
        | ⟨α, Y, hαo, hY, hEY, hR⟩ =>
          have htr : ∀ x y, In M y Y → In M x y → In M x Y := fun x y hy hxy => vl_trans M K hαo hY y x hy hxy
          have hb : ∀ b, AckIn L b → ∀ w, Sat (RAD M Y) b w := fun b hb w => match hb with
            | .inl e => by rw [e]; exact ext_AD M Y w
            | .inr (.inl e) => by rw [e]; exact her1_AD M Y K hαo hY w
            | .inr (.inr (.inl e)) => by rw [e]; exact her2_AD M Y K hY w
            | .inr (.inr (.inr (.inl ⟨ψ, e⟩))) => by rw [e]; exact comp_AD M Y ψ w
            | .inr (.inr (.inr (.inr (.inl e)))) => by rw [e]; exact found_AD M Y w
            | .inr (.inr (.inr (.inr (.inr ⟨ψ, k, m, h1, h2, e⟩)))) => by
              rw [e]
              exact ack_AD M Y hEY htr h1 h2 (hR _ (.tail _ (rlist_mem m).1)) (hR _ (.tail _ (rlist_mem m).2)) w
          have hmod : ∀ δ, δ ∈ Δ → ∀ w, Sat (RAD M Y) δ w := fun δ m w => match hΔ δ m with
            | ⟨j, a, ha, e⟩ => by
              rw [e]
              exact clo_sat _ (fun w' => match ha with
                | .inl ⟨b, eb⟩ => by rw [eb]; exact K.lem (Sat (RAD M Y) b w')
                | .inr ⟨b, σ, hbb, eb⟩ => by rw [eb]; exact (sat_ren _ b σ w').2 (hb b hbb _)) j w
          match hEY with
          | ⟨e0, he0⟩ =>
            let w0 : Nat → D := fun _ => e0
            have s : Sat (RAD M Y) (relE φ) w0 := sound (RAD M Y) d w0 (fun γ m =>
              (mem_app_inv' Δ [] m).elim (fun mΔ => hmod γ mΔ w0) (fun m' => nomatch m'))
            have s2 : Sat R φ w0 := (hR φ (.head _) w0 (fun _ => he0)).1 ((satD_rel M Y φ hl w0).1 s)
            sat_agreeU R φ 0 hf (fun n hn => absurd hn (Nat.not_lt_zero n)) s2)

end AKD

end Found

#print axioms Found.AKD.satD_L0
#print axioms Found.AKD.satD_rel
#print axioms Found.AKD.sat_trA
#print axioms Found.AKD.ext_AD
#print axioms Found.AKD.her1_AD
#print axioms Found.AKD.her2_AD
#print axioms Found.AKD.comp_AD
#print axioms Found.AKD.found_AD
#print axioms Found.AKD.ack_AD
#print axioms Found.AKD.ack_zf

found/Ackermann.lean

lean_all/found/Ackermann.lean

/-
  T167. found/Ackermann — Ackermann's set theory as a point: classes of the
  sets of one universe level become sets one level up (the reflection-style
  principle of Ackermann, realized by the universe U of T104).
  * For every class φ of V₀-sets (no respect needed: membership is via ≈ to a lift), the set
    {x ∈ U | x ≈ lift a for some a with φ a} ∈ V₁ has exactly the lifted
    members of φ (separation in V₁, T88; lift, T104).
  Ackermann's theory is conservative over ZF (Lévy/Reinhardt, cited line).

  Lean 4 core only, hand-written proofs, no axioms.
  CIC dependence: universe hierarchy, impredicative Prop (separation).
-/
import found.Grothendieck

namespace Found

namespace Ack

open WF

def classSet (φ : V.{0} → Prop) : V.{1} :=
  IZF.sep Groth.U (fun x => ∃ a : V.{0}, Eq' x (Groth.lift a) ∧ φ a)

theorem classSet_spec (φ : V.{0} → Prop) (x : V.{1}) :
    Mem x (classSet φ) ↔ ∃ a : V.{0}, Eq' x (Groth.lift a) ∧ φ a :=
  ⟨fun m => ((IZF.sep_spec _ _ (fun _ _ e ⟨a, ea, pa⟩ => ⟨a, eq_trans _ _ _ (eq_symm _ _ e) ea, pa⟩) x).mp m).2,
   fun h => (IZF.sep_spec _ _ (fun _ _ e ⟨a, ea, pa⟩ => ⟨a, eq_trans _ _ _ (eq_symm _ _ e) ea, pa⟩) x).mpr
     ⟨match h with | ⟨a, ea, _⟩ => ⟨a, ea⟩, h⟩⟩

/-- in particular, a class's lifted members are exactly the lifts of its members -/
theorem lift_mem (φ : V.{0} → Prop) (a : V.{0}) (h : φ a) :
    Mem (Groth.lift a) (classSet φ) := (classSet_spec φ _).mpr ⟨a, eq_refl _, h⟩

end Ack

end Found

#print axioms Found.Ack.classSet_spec
#print axioms Found.Ack.lift_mem

found/AlgebraicLogic.lean

lean_all/found/AlgebraicLogic.lean

/-
  T191. found/AlgebraicLogic — algebraic logic as a point: the
  Lindenbaum–Tarski algebra of a theory Γ (formulas, a ≤ b :⟺ Γ ⊢ a → b)
  is a Heyting algebra (preorder form, so no quotient is needed).
  * Algebraic completeness: a formula valid in every Heyting algebra is
    intuitionistically derivable (soundness is T65) — Heyting-valued
    semantics ↔ intuitionistic derivability.
  * The same construction with the DNE hypotheses (T66) gives the Boolean
    case; Stone duality and cylindric/polyadic algebras are cited lines.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Classical

namespace Found

namespace Prp

def LT (Γ : Form → Prop) : HA.{0} where
  car := Form
  le a b := PD true Γ (.imp a b)
  le_refl _ := .impI h0
  le_trans h k := .impI (.impE (wk k) (.impE (wk h) h0))
  top := .imp .bot .bot
  bot := .bot
  meet := .and
  join := .or
  imp := .imp
  le_top _ := .impI (.impI h0)
  bot_le _ := .impI (.botE rfl h0)
  meet_l _ _ := .impI (.andE1 h0)
  meet_r _ _ := .impI (.andE2 h0)
  meet_univ h k := .impI (.andI (.impE (wk h) h0) (.impE (wk k) h0))
  join_l _ _ := .impI (.orI1 h0)
  join_r _ _ := .impI (.orI2 h0)
  join_univ h k := .impI (.orE h0 (.impE (wk (wk h)) h0) (.impE (wk (wk k)) h0))
  imp_adj := ⟨fun h => .impI (.impI (.impE (wk (wk h)) (.andI h1 h0))),
              fun h => .impI (.impE (.impE (wk h) (.andE1 h0)) (.andE2 h0))⟩

theorem interp_LT (Γ : Form → Prop) : (φ : Form) → interp (LT Γ) Form.var Form.bot φ = φ
  | .var _ => rfl
  | .bot => rfl
  | .and a b => by
    show Form.and (interp (LT Γ) Form.var Form.bot a) (interp (LT Γ) Form.var Form.bot b) = _
    rw [interp_LT Γ a, interp_LT Γ b]
  | .or a b => by
    show Form.or (interp (LT Γ) Form.var Form.bot a) (interp (LT Γ) Form.var Form.bot b) = _
    rw [interp_LT Γ a, interp_LT Γ b]
  | .imp a b => by
    show Form.imp (interp (LT Γ) Form.var Form.bot a) (interp (LT Γ) Form.var Form.bot b) = _
    rw [interp_LT Γ a, interp_LT Γ b]

/-- algebraic completeness: valid in every Heyting algebra ⇒ derivable -/
theorem completeness (φ : Form)
    (h : ∀ (H : HA.{0}) (v : Nat → H.car), H.le H.top (interp H v H.bot φ)) :
    PD true (fun _ => False) φ := by
  have k : PD true (fun _ => False) (.imp (.imp .bot .bot) (interp (LT (fun _ => False)) Form.var Form.bot φ)) :=
    h (LT (fun _ => False)) Form.var
  rw [interp_LT] at k
  exact .impE k (.impI h0)

/-- and soundness (T65) closes the circle -/
theorem heyting_iff (φ : Form) :
    PD true (fun _ => False) φ ↔ ∀ (H : HA.{0}) (v : Nat → H.car), H.le H.top (interp H v H.bot φ) :=
  ⟨fun d H v => valid H d v H.bot (fun _ => H.bot_le), completeness φ⟩

end Prp

end Found

#print axioms Found.Prp.LT
#print axioms Found.Prp.interp_LT
#print axioms Found.Prp.completeness
#print axioms Found.Prp.heyting_iff

found/ArithBridge.lean

lean_all/found/ArithBridge.lean

/-
  found/ArithBridge — bridge from the existing coding file (BetaChain:
  Chinese remainder theorem and Gödel's β function, copied unchanged into
  found/BetaChain.lean) to the language of first-order arithmetic used by
  the PA/HA items (T83–T85).

  * First-order arithmetic: terms 0, S, +, ·, variables; formulas with =,
    ⊥, →, ∧, ∨, ∀, ∃; the standard model ℕ with Lean core operations.
  * BetaChain's hand-built operations agree with the core operations of
    the standard model: pre = pred, sb = monus, up = q·m + r (re-exported).
  * The remainder `BetaChain.rem` is defined in arithmetic by the Δ₀-shaped
    formula  Rem(m,n,r) :≡ ∃q ∃k (n = q·m + r ∧ r + S k = m), and β by
    Beta(c,d,i,y) :≡ Rem((i+1)·d+1, c, y): both are exact (↔) bridges.
  * Hence the β-lemma holds in the standard model of the arithmetic
    language: every finite sequence is coded by two numbers via the formula
    Beta.  Gödel's incompleteness itself is not in any existing file; it
    stays open as T85 (representability and diagonal lemma remain).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.BetaChain

namespace Found

namespace Arith

inductive Tm where
  | var : Nat → Tm
  | zero : Tm
  | succ : Tm → Tm
  | add : Tm → Tm → Tm
  | mul : Tm → Tm → Tm

inductive Fm where
  | eq : Tm → Tm → Fm
  | bot : Fm
  | imp : Fm → Fm → Fm
  | and : Fm → Fm → Fm
  | or : Fm → Fm → Fm
  | all : Fm → Fm
  | ex : Fm → Fm

def cons (x : Nat) (ρ : Nat → Nat) : Nat → Nat
  | 0 => x
  | n + 1 => ρ n

def eval (ρ : Nat → Nat) : Tm → Nat
  | .var n => ρ n
  | .zero => 0
  | .succ t => Nat.succ (eval ρ t)
  | .add a b => eval ρ a + eval ρ b
  | .mul a b => eval ρ a * eval ρ b

/-- truth in the standard model -/
def sat : (Nat → Nat) → Fm → Prop
  | ρ, .eq a b => eval ρ a = eval ρ b
  | _, .bot => False
  | ρ, .imp a b => sat ρ a → sat ρ b
  | ρ, .and a b => sat ρ a ∧ sat ρ b
  | ρ, .or a b => sat ρ a ∨ sat ρ b
  | ρ, .all a => ∀ x, sat (cons x ρ) a
  | ρ, .ex a => ∃ x, sat (cons x ρ) a

/-! ## BetaChain operations = core operations of the standard model -/

theorem pre_is_pred (k : Nat) : BetaChain.pre k = Nat.pred k := BetaChain.pre_eq k
theorem sb_is_sub (m n : Nat) : BetaChain.sb m n = m - n := BetaChain.sb_eq m n
theorem up_is_affine (m r q : Nat) : BetaChain.up m q r = q * m + r := BetaChain.up_eq m r q

/-! ## Rem and Beta as arithmetic formulas -/

/-- Rem(m, n, r) with m, n, r the variables 0, 1, 2:
    ∃q ∃k (n = q·m + r ∧ r + S k = m); under the two binders k = 0, q = 1,
    m = 2, n = 3, r = 4 -/
def RemF : Fm :=
  .ex (.ex (.and (.eq (.var 3) (.add (.mul (.var 1) (.var 2)) (.var 4)))
                 (.eq (.add (.var 4) (.succ (.var 0))) (.var 2))))

def env3 (a b c : Nat) : Nat → Nat := cons a (cons b (cons c (fun _ => 0)))

/-- the bridge for the remainder -/
theorem rem_bridge (m' n r : Nat) : sat (env3 (m' + 1) n r) RemF ↔ BetaChain.rem (m' + 1) n = r :=
  ⟨fun ⟨q, k, hn, hk⟩ => Eq.trans (congrArg (BetaChain.rem (m' + 1)) hn) (BetaChain.rem_div m' q r k hk),
   fun h => match BetaChain.rem_spec m' n with
     | ⟨q, r', k, hn, hk, hr⟩ =>
       have e : r' = r := Eq.trans (Eq.symm hr) h
       by cases e; exact ⟨q, k, hn, hk⟩⟩

/-- the modulus (i+1)·d+1 of the β function, as an arithmetic term in d, i -/
def mdT (d i : Tm) : Tm := .succ (.mul (.succ i) d)

theorem mdT_eval (ρ : Nat → Nat) (d i : Tm) :
    eval ρ (mdT d i) = BetaChain.md (eval ρ d) (eval ρ i) := rfl

/-- Beta(c, d, i, y) with variables 0..3:  Rem((i+1)·d+1, c, y), written out -/
def BetaF : Fm :=
  .ex (.ex (.and (.eq (.var 2) (.add (.mul (.var 1) (mdT (.var 3) (.var 4))) (.var 5)))
                 (.eq (.add (.var 5) (.succ (.var 0))) (mdT (.var 3) (.var 4)))))

def env4 (a b c e : Nat) : Nat → Nat := cons a (cons b (cons c (cons e (fun _ => 0))))

/-- the bridge for Gödel's β function -/
theorem beta_bridge (c d i y : Nat) : sat (env4 c d i y) BetaF ↔ BetaChain.beta c d i = y :=
  ⟨fun ⟨q, k, hn, hk⟩ =>
     Eq.trans (congrArg (BetaChain.rem (BetaChain.md d i)) hn)
       (BetaChain.rem_div ((i + 1) * d) q y k hk),
   fun h => match BetaChain.rem_spec ((i + 1) * d) c with
     | ⟨q, r', k, hn, hk, hr⟩ =>
       have e : r' = y := Eq.trans (Eq.symm hr) h
       by cases e; exact ⟨q, k, hn, hk⟩⟩

/-- the β-lemma in the standard model of arithmetic: every finite sequence
  a 0, …, a n is coded by c, d through the formula Beta -/
theorem beta_lemma_arith (a : Nat → Nat) (n : Nat) :
    ∃ c d, ∀ i e, i + e = n → sat (env4 c d i (a i)) BetaF :=
  match BetaChain.beta_lemma a n with
  | ⟨c, d, h⟩ => ⟨c, d, fun i e k => (beta_bridge c d i (a i)).mpr (h i e k)⟩

end Arith

end Found

#print axioms Found.Arith.pre_is_pred
#print axioms Found.Arith.sb_is_sub
#print axioms Found.Arith.up_is_affine
#print axioms Found.Arith.rem_bridge
#print axioms Found.Arith.mdT_eval
#print axioms Found.Arith.beta_bridge
#print axioms Found.Arith.beta_lemma_arith

found/Automata.lean

lean_all/found/Automata.lean

/-
  T182. found/Automata — finite automata as a point on the relation
  coordinate (transition relations labelled by letters).
  * NFA (any state type) and its subset construction (states = predicates on
    states, i.e. the core's power Ω^Q): the DFA accepts exactly the NFA's
    language (by induction on words).
  * Pushdown and cellular automata are further values (stack / grid state
    types) — recorded as lines, not re-proved here.

  Lean 4 core only, hand-written proofs, no axioms.
  CIC dependence: subset states = Q → Prop (Prop-valued predicates, not impredicative Prop).
-/

namespace Found

namespace Auto

universe u v

structure NFA (α : Type u) where
  Q : Type v
  start : Q → Prop
  δ : Q → α → Q → Prop
  acc : Q → Prop

variable {α : Type u}

/-- NFA runs: reach q' from q reading w -/
inductive Run (N : NFA.{u, v} α) : N.Q → List α → N.Q → Prop where
  | nil (q : N.Q) : Run N q [] q
  | cons {q q' q''} (a : α) (w : List α) : N.δ q a q' → Run N q' w q'' → Run N q (a :: w) q''

def accepts (N : NFA.{u, v} α) (w : List α) : Prop := ∃ q q', N.start q ∧ Run N q w q' ∧ N.acc q'

/-- the subset DFA: a state is a set of NFA states -/
def step (N : NFA.{u, v} α) (S : N.Q → Prop) (a : α) : N.Q → Prop := fun q' => ∃ q, S q ∧ N.δ q a q'

def run (N : NFA.{u, v} α) : (N.Q → Prop) → List α → (N.Q → Prop)
  | S, [] => S
  | S, a :: w => run N (step N S a) w

def daccepts (N : NFA.{u, v} α) (w : List α) : Prop := ∃ q, run N N.start w q ∧ N.acc q

theorem run_spec (N : NFA.{u, v} α) : (w : List α) → ∀ (S : N.Q → Prop) (q' : N.Q),
    run N S w q' ↔ ∃ q, S q ∧ Run N q w q'
  | [], S, q' => ⟨fun h => ⟨q', h, .nil q'⟩, fun ⟨_, hs, r⟩ => by cases r; exact hs⟩
  | a :: w, S, q' =>
    ⟨fun h => match (run_spec N w (step N S a) q').mp h with
       | ⟨q1, ⟨q0, hs, d⟩, r⟩ => ⟨q0, hs, .cons a w d r⟩,
     fun ⟨q0, hs, r⟩ => by
       cases r with
       | cons _ _ d r' => exact (run_spec N w (step N S a) q').mpr ⟨_, ⟨q0, hs, d⟩, r'⟩⟩

/-- the subset construction accepts exactly the NFA language -/
theorem subset_construction (N : NFA.{u, v} α) (w : List α) : daccepts N w ↔ accepts N w :=
  ⟨fun ⟨q', h, a⟩ => match (run_spec N w N.start q').mp h with
     | ⟨q, s, r⟩ => ⟨q, q', s, r, a⟩,
   fun ⟨q, q', s, r, a⟩ => ⟨q', (run_spec N w N.start q').mpr ⟨q, s, r⟩, a⟩⟩

end Auto

end Found

#print axioms Found.Auto.run_spec
#print axioms Found.Auto.subset_construction

found/BarInd.lean

lean_all/found/BarInd.lean

/-
  T197 line. found/BarInd — bar induction on Baire space, in the classical
  point: if B bars every infinite sequence (membership at depth n depends on
  the first n values), B ⊆ Q at every node, and Q is inductive
  (Q at all children ⇒ Q at the node), then Q holds at the root.
  Proof: otherwise dependent choice (the choice operator) builds a path
  through nodes outside Q, which never meets the bar.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.ClassicalPoint

namespace Found

namespace BI

open CP

/-- a node: the first n values of a sequence s -/
abbrev Node := (Nat → Nat) × Nat

def Agree (α β : Nat → Nat) (k : Nat) : Prop := ∀ i, i < k → α i = β i

def ext (s : Nat → Nat) (k v : Nat) : Nat → Nat := fun i =>
  match Nat.decEq i k with
  | .isTrue _ => v
  | .isFalse _ => s i

theorem ext_lt (s : Nat → Nat) (k v i : Nat) (h : i < k) : ext s k v i = s i := by
  unfold ext
  cases Nat.decEq i k with
  | isTrue e => exact absurd (e ▸ h) (Nat.lt_irrefl k)
  | isFalse _ => rfl

theorem ext_eq (s : Nat → Nat) (k v : Nat) : ext s k v k = v := by
  unfold ext
  cases Nat.decEq k k with
  | isTrue _ => rfl
  | isFalse h => exact absurd rfl h

variable (K : Classical') (B Q : Node → Prop)
  (locB : ∀ α β n, Agree α β n → B (α, n) → B (β, n))
  (bar : ∀ α : Nat → Nat, ∃ n, B (α, n))
  (sub : ∀ s, B s → Q s)
  (ind : ∀ s : Node, (∀ v, Q (ext s.1 s.2 v, s.2 + 1)) → Q s)

include K ind in
theorem child (s : Node) (h : ¬ Q s) : ∃ v, ¬ Q (ext s.1 s.2 v, s.2 + 1) :=
  dne K _ (fun hn => h (ind s (fun v => dne K _ (fun hq => hn ⟨v, hq⟩))))

/-- the path through ¬Q nodes, carried with its invariant -/
def path (h0 : ¬ Q (fun _ => 0, 0)) : (k : Nat) → {s : Nat → Nat // ¬ Q (s, k)}
  | 0 => ⟨fun _ => 0, h0⟩
  | k + 1 =>
    let p := path h0 k
    ⟨ext p.1 k (K.ch (fun v => ¬ Q (ext p.1 k v, k + 1)) (child K Q ind (p.1, k) p.2)),
     K.ch_spec (fun v => ¬ Q (ext p.1 k v, k + 1)) (child K Q ind (p.1, k) p.2)⟩

def branch (h0 : ¬ Q (fun _ => 0, 0)) (i : Nat) : Nat := (path K Q ind h0 (i + 1)).1 i

theorem stable (h0 : ¬ Q (fun _ => 0, 0)) : ∀ k i, i < k → (path K Q ind h0 k).1 i = branch K Q ind h0 i
  | 0, _, h => absurd h (Nat.not_lt_zero _)
  | k + 1, i, h => by
    cases Nat.lt_or_ge i k with
    | inl hl =>
      show ext (path K Q ind h0 k).1 k _ i = _
      rw [ext_lt _ k _ i hl]
      exact stable h0 k i hl
    | inr hg =>
      have e : i = k := Nat.le_antisymm (Nat.le_of_lt_succ h) hg
      subst e
      rfl

include K locB bar sub ind in
/-- bar induction -/
theorem bar_induction : Q (fun _ => 0, 0) := by
  refine dne K _ (fun h0 => ?_)
  match bar (branch K Q ind h0) with
  | ⟨n, hb⟩ =>
    have hq := sub _ (locB _ (path K Q ind h0 n).1 n (fun i hi => (stable K Q ind h0 n i hi).symm) hb)
    exact (path K Q ind h0 n).2 hq

end BI

end Found

#print axioms Found.BI.bar_induction

found/Base.lean

lean_all/found/Base.lean

/-
  T60. found/Base — the base of the one frame.

  * The A = A chain (OneChain C0–C6): rule, join, rev, ap, Iso.
  * E-categories: categories whose hom-sets carry an equivalence relation
    (a setoid), so that no quotient (Quot.sound) and no funext is needed.
  * Three instances: the path groupoid of a type (the A = A chain is a
    groupoid), the category of setoids, the category of types and functions
    (pointwise equality), the category of types and relations.
  * RelFunCat: the graph functor Fun → Rel, faithful, and the functions are
    exactly the total single-valued relations (one direction constructive,
    the other with a unique-choice witness given as data).

  Lean 4 core only, hand-written proofs, no axioms.

  CIC dependence: proof irrelevance of Eq (path equivalence in PathCat set to True = UIP), structure eta.
-/

namespace Found

universe u v w

/-! ## The A = A chain -/

section Chain
variable {A : Type u} {B : Type v}

theorem rule {a : A} (G : (x : A) → a = x → Prop) (h : G a rfl) :
    ∀ (x : A) (p : a = x), G x p := by
  intro x p
  cases p
  exact h

theorem join {a x y : A} (p : a = x) (q : x = y) : a = y := by
  cases q
  exact p

theorem rev {a x : A} (p : a = x) : x = a := by
  cases p
  rfl

theorem ap (f : A → B) {a x : A} (p : a = x) : f a = f x := by
  cases p
  rfl

theorem join_id {a x : A} (p : a = x) : join p rfl = p := rfl
theorem id_join {a x : A} (p : a = x) : join rfl p = p := rfl
theorem join_assoc {a x y z : A} (p : a = x) (q : x = y) (r : y = z) :
    join (join p q) r = join p (join q r) := rfl
theorem join_rev {a x : A} (p : a = x) : join p (rev p) = rfl := rfl
theorem ap_join (f : A → B) {a x y : A} (p : a = x) (q : x = y) :
    ap f (join p q) = join (ap f p) (ap f q) := rfl

structure Iso (α : Type u) (β : Type v) where
  fwd : α → β
  bwd : β → α
  left : ∀ a, bwd (fwd a) = a
  right : ∀ b, fwd (bwd b) = b

def Iso.refl (α : Type u) : Iso α α := ⟨fun a => a, fun a => a, fun _ => rfl, fun _ => rfl⟩

def Iso.symm {α : Type u} {β : Type v} (e : Iso α β) : Iso β α :=
  ⟨e.bwd, e.fwd, e.right, e.left⟩

def Iso.trans {α : Type u} {β : Type v} {γ : Type w} (e : Iso α β) (f : Iso β γ) : Iso α γ :=
  ⟨fun a => f.fwd (e.fwd a), fun c => e.bwd (f.bwd c),
   fun a => join (ap e.bwd (f.left (e.fwd a))) (e.left a),
   fun c => join (ap f.fwd (e.right (f.bwd c))) (f.right c)⟩

end Chain

/-! ## Setoids and E-categories -/

structure Setd where
  car : Type u
  eqv : car → car → Prop
  refl : ∀ a, eqv a a
  symm : ∀ {a b}, eqv a b → eqv b a
  trans : ∀ {a b c}, eqv a b → eqv b c → eqv a c

/-- A category whose hom-sets are setoids. -/
structure ECat where
  Ob : Type u
  Hom : Ob → Ob → Type v
  heq : {a b : Ob} → Hom a b → Hom a b → Prop
  hrefl : ∀ {a b} (f : Hom a b), heq f f
  hsymm : ∀ {a b} {f g : Hom a b}, heq f g → heq g f
  htrans : ∀ {a b} {f g h : Hom a b}, heq f g → heq g h → heq f h
  id : (a : Ob) → Hom a a
  comp : {a b c : Ob} → Hom a b → Hom b c → Hom a c
  comp_resp : ∀ {a b c} {f f' : Hom a b} {g g' : Hom b c},
    heq f f' → heq g g' → heq (comp f g) (comp f' g')
  id_comp : ∀ {a b} (f : Hom a b), heq (comp (id a) f) f
  comp_id : ∀ {a b} (f : Hom a b), heq (comp f (id b)) f
  assoc : ∀ {a b c d} (f : Hom a b) (g : Hom b c) (h : Hom c d),
    heq (comp (comp f g) h) (comp f (comp g h))

/-- A functor between E-categories, respecting the hom equivalences. -/
structure EFun (C : ECat.{u, v}) (D : ECat.{u', v'}) where
  obj : C.Ob → D.Ob
  map : {a b : C.Ob} → C.Hom a b → D.Hom (obj a) (obj b)
  map_resp : ∀ {a b} {f g : C.Hom a b}, C.heq f g → D.heq (map f) (map g)
  map_id : ∀ a, D.heq (map (C.id a)) (D.id (obj a))
  map_comp : ∀ {a b c} (f : C.Hom a b) (g : C.Hom b c),
    D.heq (map (C.comp f g)) (D.comp (map f) (map g))

def EFun.Faithful {C : ECat.{u, v}} {D : ECat.{u', v'}} (F : EFun C D) : Prop :=
  ∀ {a b} (f g : C.Hom a b), D.heq (F.map f) (F.map g) → C.heq f g

/-! ## Instance 1: the A = A chain is a groupoid -/

def PathCat (A : Type u) : ECat.{u, 0} where
  Ob := A
  Hom a b := PLift (a = b)
  heq _ _ := True
  hrefl _ := trivial
  hsymm _ := trivial
  htrans _ _ := trivial
  id _ := ⟨rfl⟩
  comp p q := ⟨join p.down q.down⟩
  comp_resp _ _ := trivial
  id_comp _ := trivial
  comp_id _ := trivial
  assoc _ _ _ := trivial

/-- every path has an inverse: the groupoid law, from `rev` -/
theorem PathCat.inv {A : Type u} {a b : A} (p : (PathCat A).Hom a b) :
    ∃ q : (PathCat A).Hom b a, (PathCat A).heq ((PathCat A).comp p q) ((PathCat A).id a) :=
  ⟨⟨rev p.down⟩, trivial⟩

/-- `ap` is a functor between path groupoids -/
def PathCat.apF {A : Type u} {B : Type v} (f : A → B) : EFun (PathCat A) (PathCat B) where
  obj := f
  map p := ⟨ap f p.down⟩
  map_resp _ := trivial
  map_id _ := trivial
  map_comp _ _ := trivial

/-! ## Instance 2: setoids -/

structure SHom (X Y : Setd.{u}) where
  fn : X.car → Y.car
  resp : ∀ {a a'}, X.eqv a a' → Y.eqv (fn a) (fn a')

def SetdCat : ECat.{u+1, u} where
  Ob := Setd.{u}
  Hom := SHom
  heq {_ b} f g := ∀ x, b.eqv (f.fn x) (g.fn x)
  hrefl {_ b} f x := b.refl (f.fn x)
  hsymm {_ b} _ _ h x := b.symm (h x)
  htrans {_ b} _ _ _ h k x := b.trans (h x) (k x)
  id _ := ⟨fun x => x, fun h => h⟩
  comp f g := ⟨fun x => g.fn (f.fn x), fun h => g.resp (f.resp h)⟩
  comp_resp {_ _ c} _ _ g _ hf hg x := c.trans (g.resp (hf x)) (hg _)
  id_comp {_ b} f x := b.refl (f.fn x)
  comp_id {_ b} f x := b.refl (f.fn x)
  assoc {_ _ _ d} _ _ h _ := d.refl (h.fn _)

/-! ## Instance 3: types and functions (pointwise equality, no funext) -/

def FunCat : ECat.{u+1, u} where
  Ob := Type u
  Hom A B := A → B
  heq f g := ∀ x, f x = g x
  hrefl _ _ := rfl
  hsymm h x := rev (h x)
  htrans h k x := join (h x) (k x)
  id _ := fun x => x
  comp f g := fun x => g (f x)
  comp_resp {_ _ _} _ f' g _ hf hg x := join (ap g (hf x)) (hg (f' x))
  id_comp _ _ := rfl
  comp_id _ _ := rfl
  assoc _ _ _ _ := rfl

/-! ## Instance 4: types and relations (pointwise ↔, no propext) -/

def RelCat : ECat.{u+1, u} where
  Ob := Type u
  Hom A B := A → B → Prop
  heq R S := ∀ a b, R a b ↔ S a b
  hrefl _ _ _ := Iff.rfl
  hsymm h a b := (h a b).symm
  htrans h k a b := (h a b).trans (k a b)
  id _ := fun a b => a = b
  comp R S := fun a c => ∃ b, R a b ∧ S b c
  comp_resp hR hS a c :=
    ⟨fun ⟨b, r, s⟩ => ⟨b, (hR a b).mp r, (hS b c).mp s⟩,
     fun ⟨b, r, s⟩ => ⟨b, (hR a b).mpr r, (hS b c).mpr s⟩⟩
  id_comp _ a b :=
    ⟨fun ⟨_, e, r⟩ => by cases e; exact r, fun r => ⟨a, rfl, r⟩⟩
  comp_id _ a b :=
    ⟨fun ⟨_, r, e⟩ => by cases e; exact r, fun r => ⟨b, r, rfl⟩⟩
  assoc _ _ _ a d :=
    ⟨fun ⟨c, ⟨b, r, s⟩, t⟩ => ⟨b, r, c, s, t⟩,
     fun ⟨b, r, c, s, t⟩ => ⟨c, ⟨b, r, s⟩, t⟩⟩

/-! ## RelFunCat: functions are the total single-valued relations -/

def graph {A : Type u} {B : Type u} (f : A → B) : A → B → Prop := fun a b => f a = b

def Graph : EFun FunCat.{u} RelCat.{u} where
  obj A := A
  map f := graph f
  map_resp {_ _} _ _ h a _ :=
    ⟨fun e => join (rev (h a)) e, fun e => join (h a) e⟩
  map_id _ _ _ := Iff.rfl
  map_comp f g a _ :=
    ⟨fun e => ⟨f a, rfl, e⟩, fun ⟨_, e1, e2⟩ => join (ap g e1) e2⟩

theorem Graph_faithful : EFun.Faithful Graph.{u} :=
  fun _ g h x => (h x (g x)).mpr rfl

def Total {A B : Type u} (R : A → B → Prop) : Prop := ∀ a, ∃ b, R a b
def SingleValued {A B : Type u} (R : A → B → Prop) : Prop :=
  ∀ a b b', R a b → R a b' → b = b'

theorem graph_total {A B : Type u} (f : A → B) : Total (graph f) := fun a => ⟨f a, rfl⟩

theorem graph_single {A B : Type u} (f : A → B) : SingleValued (graph f) :=
  fun _ _ _ e e' => join (rev e) e'

/-- the converse: a single-valued relation with a chosen witness (data, i.e. a
  `Σ`-type rather than `∃`, so no choice) is the graph of a function -/
theorem rel_is_graph {A B : Type u} (R : A → B → Prop) (hs : SingleValued R)
    (w : (a : A) → {b : B // R a b}) :
    RelCat.heq (graph (fun a => (w a).val)) R :=
  fun a b => ⟨fun e => by cases e; exact (w a).property,
              fun r => hs a _ _ (w a).property r⟩

/-- the map back and forth is the identity up to the hom equivalence -/
theorem graph_witness_roundtrip {A B : Type u} (f : A → B) :
    FunCat.heq (fun a => (⟨f a, rfl⟩ : {b : B // graph f a b}).val) f :=
  fun _ => rfl

end Found

#print axioms Found.rule
#print axioms Found.join
#print axioms Found.rev
#print axioms Found.ap
#print axioms Found.join_assoc
#print axioms Found.Iso.trans
#print axioms Found.PathCat
#print axioms Found.PathCat.inv
#print axioms Found.PathCat.apF
#print axioms Found.SetdCat
#print axioms Found.FunCat
#print axioms Found.RelCat
#print axioms Found.Graph
#print axioms Found.Graph_faithful
#print axioms Found.graph_total
#print axioms Found.graph_single
#print axioms Found.rel_is_graph
#print axioms Found.graph_witness_roundtrip

found/BetaChain.lean

lean_all/found/BetaChain.lean

-- found/BetaChain.lean: copy of ~/Downloads/The_Totality_Theorem/BetaChain.lean (original untouched).
-- Only change: `import Mathlib` and the trailing `#check` block of Mathlib names are dropped,
-- so the file builds with Lean core alone; every theorem below is the original text.

/-
  BetaChain.lean  (second version: the general theorems proved by hand)
  One connected chain from `id` to the Chinese remainder theorem and
  Goedel's beta function (links 1-29).

  Everything in the namespace `BetaChain` is proved by hand: no `omega`,
  no `simp`, no `ring`. Every `#print axioms` line should report no
  axioms. Mathlib is imported only for the last block.

  General theorems proved here:

    rem_lt, rem_idem   the remainder is always below the modulus and is
                       idempotent                              (link 17)
    rem_add_mul        adding a multiple does not change it    (link 18)
    crt_pair           the Chinese remainder theorem for two   (link 24)
    crt_list           ... and for any list of moduli in which each
                       later modulus is invertible along each earlier
                       one                                     (link 25)
    dvd_fact           1 <= k <= s  =>  k divides s!           (link 26)
    md_inv             the moduli (i+1) d + 1 are invertible along
                       each other, with explicit coefficients  (link 27)
    beta_lemma         for every sequence a 0, ..., a n there are c, d
                       with  beta c d i = a i  for all i <= n  (link 28)

  Coprimality is used in the form "y b = 1 + s a" (b is invertible
  along a), which is the form in which the chain states it.
-/

universe u

namespace BetaChain

/-! ## Links 1-8. Places -/

def diag {A : Type u} (x : A) : A × A := (x, x)

theorem tri1 {A : Type u} (x : A) : (diag x).1 = x := rfl

def e1 {A : Type u} (p : A × A) : A × A := diag p.1

theorem e1_idem {A : Type u} (p : A × A) : e1 (e1 p) = e1 p := rfl

theorem split_idem {A B : Type u} (r : B → A) (s : A → B)
    (h : ∀ x, r (s x) = x) (y : B) : s (r (s (r y))) = s (r y) :=
  congrArg s (h (r y))

theorem read_inj {A B : Type u} (read : A → B) (place : B → A)
    (h : ∀ n, place (read n) = n) {m n : A} (e : read m = read n) : m = n :=
  Eq.trans (Eq.symm (h m)) (Eq.trans (congrArg place e) (h n))

/-! ## Links 9-12. "Do it n times"; + and * are composition -/

def iter {A : Type u} : Nat → (A → A) → A → A
  | 0, _, a => a
  | n + 1, f, a => f (iter n f a)

theorem iter_zero {A : Type u} (f : A → A) (a : A) : iter 0 f a = a := rfl

theorem iter_succ {A : Type u} (n : Nat) (f : A → A) (a : A) :
    iter (n + 1) f a = f (iter n f a) := rfl

theorem iter_one {A : Type u} (f : A → A) (a : A) : iter 1 f a = f a := rfl

theorem iter_add {A : Type u} (f : A → A) (a : A) (m : Nat) :
    ∀ n : Nat, iter (m + n) f a = iter n f (iter m f a)
  | 0 => rfl
  | n + 1 => congrArg f (iter_add f a m n)

theorem iter_mul {A : Type u} (f : A → A) (m : Nat) :
    ∀ (n : Nat) (a : A), iter (m * n) f a = iter n (iter m f) a
  | 0, _ => rfl
  | n + 1, a =>
    Eq.trans (iter_add f a (m * n) m) (congrArg (iter m f) (iter_mul f m n a))

theorem iter_fix {A : Type u} (f : A → A) (r : A) (h : f r = r) :
    ∀ k : Nat, iter k f r = r
  | 0 => rfl
  | k + 1 => Eq.trans (congrArg f (iter_fix f r h k)) h

theorem iter_succ' {A : Type u} (f : A → A) (a : A) :
    ∀ q : Nat, iter (q + 1) f a = iter q f (f a)
  | 0 => rfl
  | q + 1 => congrArg f (iter_succ' f a q)

/-! ### The laws of + and *, from the defining equations -/

theorem nzero_add : ∀ (a : Nat), 0 + a = a
  | 0 => rfl
  | a + 1 => congrArg Nat.succ (nzero_add a)

theorem nsucc_add : ∀ (a b : Nat), Nat.succ a + b = Nat.succ (a + b)
  | _, 0 => rfl
  | a, b + 1 => congrArg Nat.succ (nsucc_add a b)

theorem nadd_assoc : ∀ (a b c : Nat), a + b + c = a + (b + c)
  | _, _, 0 => rfl
  | a, b, c + 1 => congrArg Nat.succ (nadd_assoc a b c)

theorem nadd_comm : ∀ (a b : Nat), a + b = b + a
  | a, 0 => Eq.symm (nzero_add a)
  | a, b + 1 =>
    Eq.trans (congrArg Nat.succ (nadd_comm a b)) (Eq.symm (nsucc_add b a))

theorem nadd_left_comm (a b c : Nat) : a + (b + c) = b + (a + c) :=
  Eq.trans (Eq.symm (nadd_assoc a b c))
    (Eq.trans (congrArg (fun t => t + c) (nadd_comm a b)) (nadd_assoc b a c))

theorem nadd_right_comm (a b c : Nat) : a + b + c = a + c + b :=
  Eq.trans (nadd_assoc a b c)
    (Eq.trans (congrArg (fun t => a + t) (nadd_comm b c)) (Eq.symm (nadd_assoc a c b)))

theorem mid4 (a b c d : Nat) : (a + b) + (c + d) = (a + c) + (b + d) :=
  Eq.trans (nadd_assoc a b (c + d))
    (Eq.trans
      (congrArg (fun t => a + t)
        (Eq.trans (Eq.symm (nadd_assoc b c d))
          (Eq.trans (congrArg (fun t => t + d) (nadd_comm b c)) (nadd_assoc c b d))))
      (Eq.symm (nadd_assoc a c (b + d))))

theorem nzero_mul : ∀ a : Nat, 0 * a = 0
  | 0 => rfl
  | a + 1 => nzero_mul a

theorem nsucc_mul : ∀ (a b : Nat), (a + 1) * b = a * b + b
  | _, 0 => rfl
  | a, b + 1 =>
    Eq.trans (congrArg (fun t => t + (a + 1)) (nsucc_mul a b)) (mid4 (a * b) b a 1)

theorem nmul_comm : ∀ (a b : Nat), a * b = b * a
  | a, 0 => Eq.symm (nzero_mul a)
  | a, b + 1 =>
    Eq.trans (congrArg (fun t => t + a) (nmul_comm a b)) (Eq.symm (nsucc_mul b a))

theorem nmul_one (a : Nat) : a * 1 = a :=
  Eq.trans (show a * 1 = 0 + a from rfl) (nzero_add a)

theorem nleft_distrib (a b : Nat) : ∀ c : Nat, a * (b + c) = a * b + a * c
  | 0 => rfl
  | c + 1 =>
    Eq.trans (congrArg (fun t => t + a) (nleft_distrib a b c))
      (nadd_assoc (a * b) (a * c) a)

theorem nright_distrib (a b c : Nat) : (a + b) * c = a * c + b * c :=
  Eq.trans (nmul_comm (a + b) c)
    (Eq.trans (nleft_distrib c a b)
      (Eq.trans (congrArg (fun t => t + c * b) (nmul_comm c a))
        (congrArg (fun t => a * c + t) (nmul_comm c b))))

theorem nmul_assoc (a b : Nat) : ∀ c : Nat, a * b * c = a * (b * c)
  | 0 => rfl
  | c + 1 =>
    Eq.trans (congrArg (fun t => t + a * b) (nmul_assoc a b c))
      (Eq.symm (nleft_distrib a (b * c) b))

theorem mmid4 (a b c d : Nat) : (a * b) * (c * d) = (a * c) * (b * d) :=
  Eq.trans (nmul_assoc a b (c * d))
    (Eq.trans
      (congrArg (fun t => a * t)
        (Eq.trans (Eq.symm (nmul_assoc b c d))
          (Eq.trans (congrArg (fun t => t * d) (nmul_comm b c)) (nmul_assoc c b d))))
      (Eq.symm (nmul_assoc a c (b * d))))

/-! ## Link 13. Choosing -/

def sel {A : Type u} (n : Nat) (x y : A) : A := iter n (fun _ => y) x

theorem sel_zero {A : Type u} (x y : A) : sel 0 x y = x := rfl

theorem sel_succ {A : Type u} (n : Nat) (x y : A) : sel (n + 1) x y = y := rfl

/-- A successor is not zero: choosing tells them apart.
    `sel` reads `True` at `0` and `False` at a successor, so an equation
    `n + 1 = 0` would carry `True` to `False`.
    (Proved here from `sel`, so that the library lemma
    `Nat.succ_ne_zero` is not used.) -/
theorem succ_ne_zero' (n : Nat) : Nat.succ n ≠ 0 :=
  fun h =>
    have e1 : True = sel 0 True False := Eq.symm (sel_zero True False)
    have e2 : sel 0 True False = sel (Nat.succ n) True False :=
      Eq.symm (congrArg (fun t => sel t True False) h)
    have e3 : sel (Nat.succ n) True False = False := sel_succ n True False
    Eq.mp (Eq.trans e1 (Eq.trans e2 e3)) True.intro

/-! ## Link 14. One-before, from a step on pairs -/

def pstep (p : Nat × Nat) : Nat × Nat := (p.1 + 1, p.1)

def pre (n : Nat) : Nat := (iter n pstep (0, 0)).2

theorem pstep_fst : ∀ n : Nat, (iter n pstep (0, 0)).1 = n
  | 0 => rfl
  | n + 1 => congrArg Nat.succ (pstep_fst n)

theorem pre_zero : pre 0 = 0 := rfl

theorem pre_succ (n : Nat) : pre (n + 1) = n := pstep_fst n

theorem succ_inj' {m n : Nat} (h : m + 1 = n + 1) : m = n :=
  Eq.trans (Eq.symm (pre_succ m)) (Eq.trans (congrArg pre h) (pre_succ n))

theorem pre_eq : ∀ k : Nat, pre k = Nat.pred k
  | 0 => rfl
  | k + 1 => pre_succ k

/-! ## Link 15. Subtraction: one-before, n times -/

def sb (m n : Nat) : Nat := iter n pre m

theorem sb_zero (m : Nat) : sb m 0 = m := rfl

theorem sb_succ (m n : Nat) : sb m (n + 1) = pre (sb m n) := rfl

theorem sb_add_cancel : ∀ (n m : Nat), sb (m + n) n = m
  | 0, _ => rfl
  | n + 1, m =>
    Eq.trans
      (congrArg (fun t => pre (sb t n)) (Eq.symm (nsucc_add m n)))
      (Eq.trans (congrArg pre (sb_add_cancel n (m + 1))) (pre_succ m))

theorem sb_self (a : Nat) : sb a a = 0 :=
  Eq.trans (congrArg (fun t => sb t a) (Eq.symm (nzero_add a))) (sb_add_cancel a 0)

theorem sb_eq (m : Nat) : ∀ n : Nat, sb m n = m - n
  | 0 => rfl
  | n + 1 => Eq.trans (congrArg pre (sb_eq m n)) (pre_eq (m - n))

/-- `m - (m + x) = 0`. -/
theorem sb_le_zero (m x : Nat) : sb m (m + x) = 0 :=
  Eq.trans (iter_add pre m m x)
    (Eq.trans (congrArg (iter x pre) (sb_self m)) (iter_fix pre 0 pre_zero x))

/-! ## Links 16-17. The subtract-step and the remainder -/

def tstep (m n : Nat) : Nat := sel (sb m n) (sb n m) n

def rem (m n : Nat) : Nat := iter n (tstep m) n

theorem tstep_small (m r k : Nat) (h : sb m r = k + 1) : tstep m r = r :=
  congrArg (fun t => sel t (sb r m) r) h

theorem tstep_big (m n : Nat) (h : sb m n = 0) : tstep m n = sb n m :=
  congrArg (fun t => sel t (sb n m) n) h

theorem rem_of_fix (m r : Nat) (h : tstep m r = r) : rem m r = r :=
  iter_fix (tstep m) r h r

theorem rem_small (m r k : Nat) (h : sb m r = k + 1) : rem m r = r :=
  rem_of_fix m r (tstep_small m r k h)

theorem rem_idem_of (m n : Nat) (h : tstep m (rem m n) = rem m n) :
    rem m (rem m n) = rem m n :=
  rem_of_fix m (rem m n) h

theorem rem_values :
    rem 3 10 = 1 ∧ rem 3 (rem 3 10) = rem 3 10 ∧ rem 6 11 = 5 ∧ rem 5 12 = 2 :=
  ⟨rfl, rfl, rfl, rfl⟩

/-! ### The remainder in general -/

/-- From `m` up, one step takes `m` away. -/
theorem tstep_add (m x : Nat) : tstep m (m + x) = x :=
  Eq.trans (tstep_big m (m + x) (sb_le_zero m x))
    (Eq.trans (congrArg (fun t => sb t m) (nadd_comm m x)) (sb_add_cancel m x))

/-- Below `m` (that is, `r + (k+1) = m`) the step changes nothing. -/
theorem fix_of_lt (m r k : Nat) (h : r + (k + 1) = m) : tstep m r = r :=
  tstep_small m r k
    (Eq.trans (congrArg (fun t => sb t r) (Eq.symm h))
      (Eq.trans (congrArg (fun t => sb t r) (nadd_comm r (k + 1)))
        (sb_add_cancel r (k + 1))))

/-- `r` with `m` added `q` times. -/
def up (m q r : Nat) : Nat := iter q (fun x => m + x) r

theorem up_eq (m r : Nat) : ∀ q : Nat, up m q r = q * m + r
  | 0 => Eq.symm (Eq.trans (congrArg (fun t => t + r) (nzero_mul m)) (nzero_add r))
  | q + 1 =>
    Eq.trans (congrArg (fun t => m + t) (up_eq m r q))
      (Eq.trans (Eq.symm (nadd_assoc m (q * m) r))
        (Eq.trans (congrArg (fun t => t + r) (nadd_comm m (q * m)))
          (congrArg (fun t => t + r) (Eq.symm (nsucc_mul q m)))))

/-- Taking `m` away `q` times undoes adding it `q` times. -/
theorem down_up (m r : Nat) : ∀ q : Nat, iter q (tstep m) (up m q r) = r
  | 0 => rfl
  | q + 1 =>
    Eq.trans (iter_succ' (tstep m) (up m (q + 1) r) q)
      (Eq.trans (congrArg (iter q (tstep m)) (tstep_add m (up m q r)))
        (down_up m r q))

theorem up_ge (m' r : Nat) : ∀ q : Nat, ∃ j, up (m' + 1) q r = q + j
  | 0 => ⟨r, Eq.symm (nzero_add r)⟩
  | q + 1 =>
    match up_ge m' r q with
    | ⟨j, hj⟩ =>
      ⟨m' + j,
        Eq.trans (congrArg (fun t => (m' + 1) + t) hj)
          (Eq.trans (nsucc_add m' (q + j))
            (Eq.trans (congrArg Nat.succ (nadd_left_comm m' q j))
              (Eq.symm (nsucc_add q (m' + j)))))⟩

theorem rem_up (m' r q : Nat) (hfix : tstep (m' + 1) r = r) :
    rem (m' + 1) (up (m' + 1) q r) = r :=
  match up_ge m' r q with
  | ⟨j, hj⟩ =>
    Eq.trans
      (congrArg (fun t => iter t (tstep (m' + 1)) (up (m' + 1) q r)) hj)
      (Eq.trans (iter_add (tstep (m' + 1)) (up (m' + 1) q r) q j)
        (Eq.trans (congrArg (iter j (tstep (m' + 1))) (down_up (m' + 1) r q))
          (iter_fix (tstep (m' + 1)) r hfix j)))

/-- The remainder of `q m + r` is `r` when `r` is below `m`. -/
theorem rem_div (m' q r k : Nat) (h : r + (k + 1) = m' + 1) :
    rem (m' + 1) (q * (m' + 1) + r) = r :=
  Eq.trans (congrArg (rem (m' + 1)) (Eq.symm (up_eq (m' + 1) r q)))
    (rem_up m' r q (fix_of_lt (m' + 1) r k h))

/-- Every number is `q m + r` with `r` below `m`. -/
theorem div_exists (m' : Nat) :
    ∀ n : Nat, ∃ q r k, n = q * (m' + 1) + r ∧ r + (k + 1) = m' + 1
  | 0 => ⟨0, 0, m', Eq.symm (nzero_mul (m' + 1)), nzero_add (m' + 1)⟩
  | n + 1 =>
    match div_exists m' n with
    | ⟨q, r, k, hn, hr⟩ =>
      match k, hr with
      | 0, hr =>
        ⟨q + 1, 0, m',
          Eq.trans (congrArg Nat.succ hn)
            (Eq.trans (congrArg (fun t => q * (m' + 1) + t) hr)
              (Eq.symm (nsucc_mul q (m' + 1)))),
          nzero_add (m' + 1)⟩
      | k + 1, hr =>
        ⟨q, r + 1, k, congrArg Nat.succ hn, Eq.trans (nsucc_add r (k + 1)) hr⟩

theorem rem_spec (m' n : Nat) :
    ∃ q r k, n = q * (m' + 1) + r ∧ r + (k + 1) = m' + 1 ∧ rem (m' + 1) n = r :=
  match div_exists m' n with
  | ⟨q, r, k, hn, hr⟩ =>
    ⟨q, r, k, hn, hr, Eq.trans (congrArg (rem (m' + 1)) hn) (rem_div m' q r k hr)⟩

/-- The remainder is always below the modulus. -/
theorem rem_lt (m' n : Nat) : ∃ k, rem (m' + 1) n + (k + 1) = m' + 1 :=
  match rem_spec m' n with
  | ⟨_, _, k, _, hr, hrem⟩ => ⟨k, Eq.trans (congrArg (fun t => t + (k + 1)) hrem) hr⟩

/-- The remainder is idempotent. -/
theorem rem_idem (m' n : Nat) : rem (m' + 1) (rem (m' + 1) n) = rem (m' + 1) n :=
  match rem_spec m' n with
  | ⟨_, r, k, _, hr, hrem⟩ =>
    Eq.trans (congrArg (rem (m' + 1)) hrem)
      (Eq.trans (rem_of_fix (m' + 1) r (fix_of_lt (m' + 1) r k hr)) (Eq.symm hrem))

/-- Adding a multiple of the modulus does not change the remainder. -/
theorem rem_add_mul (m' x p : Nat) :
    rem (m' + 1) (p * (m' + 1) + x) = rem (m' + 1) x :=
  match rem_spec m' x with
  | ⟨q, r, k, hx, hr, hrem⟩ =>
    Eq.trans (congrArg (fun t => rem (m' + 1) (p * (m' + 1) + t)) hx)
      (Eq.trans
        (congrArg (rem (m' + 1)) (Eq.symm (nadd_assoc (p * (m' + 1)) (q * (m' + 1)) r)))
        (Eq.trans
          (congrArg (fun t => rem (m' + 1) (t + r)) (Eq.symm (nright_distrib p q (m' + 1))))
          (Eq.trans (rem_div m' (p + q) r k hr) (Eq.symm hrem))))

/-! ## Link 19. The Euclid step -/

def ustep (p : Nat × Nat) : Nat × Nat :=
  sel (sb p.1 p.2) (sel (sb p.2 p.1) p (p.1, sb p.2 p.1)) (sb p.1 p.2, p.2)

theorem ustep_diag (a : Nat) : ustep (a, a) = (a, a) :=
  congrArg (fun t => sel t (sel t (a, a) (a, t)) (t, a)) (sb_self a)

theorem ustep_reaches :
    iter 10 ustep (6, 4) = (2, 2) ∧ iter 5 ustep (2, 3) = (1, 1) ∧
    iter 8 ustep (3, 5) = (1, 1) :=
  ⟨rfl, rfl, rfl⟩

/-! ## Link 22. Euclid's lemma line -/

theorem euclid_line {R : Type u} (add mul : R → R → R)
    (mul_assoc' : ∀ x y z, mul (mul x y) z = mul x (mul y z))
    (right_distrib' : ∀ x y z, mul (add x y) z = add (mul x z) (mul y z))
    (p a b x y c : R)
    (hb : b = add (mul (mul x b) p) (mul y (mul a b)))
    (hab : mul a b = mul c p) :
    b = mul (add (mul x b) (mul y c)) p :=
  Eq.trans hb
    (Eq.trans
      (congrArg (fun w => add (mul (mul x b) p) (mul y w)) hab)
      (Eq.trans
        (congrArg (fun w => add (mul (mul x b) p) w) (Eq.symm (mul_assoc' y c p)))
        (Eq.symm (right_distrib' (mul x b) (mul y c) p))))

/-! ## Link 23. Two pieces that split 1 -/

theorem piece_idem {R : Type u} (add mul : R → R → R) (zero one : R)
    (mul_one' : ∀ x, mul x one = x)
    (add_zero' : ∀ x, add x zero = x)
    (left_distrib' : ∀ x y z, mul x (add y z) = add (mul x y) (mul x z))
    (ea eb : R) (hsum : add ea eb = one) (horth : mul ea eb = zero) :
    mul ea ea = ea :=
  Eq.symm
    (Eq.trans (Eq.symm (mul_one' ea))
      (Eq.trans (congrArg (fun w => mul ea w) (Eq.symm hsum))
        (Eq.trans (left_distrib' ea ea eb)
          (Eq.trans (congrArg (fun w => add (mul ea ea) w) horth)
            (add_zero' (mul ea ea))))))

/-! ## Link 24. The Chinese remainder theorem for two moduli -/

theorem swap_ends (p v x u : Nat) : ((p + v) + x) + u = ((u + p) + x) + v :=
  Eq.trans (congrArg (fun t => t + u) (nadd_right_comm p v x))
    (Eq.trans (nadd_assoc (p + x) v u)
      (Eq.trans (congrArg (fun t => (p + x) + t) (nadd_comm v u))
        (Eq.trans (Eq.symm (nadd_assoc (p + x) u v))
          (congrArg (fun t => t + v)
            (Eq.trans (nadd_comm (p + x) u) (Eq.symm (nadd_assoc u p x)))))))

theorem crtL (v s t w u : Nat) :
    (v + s * t) * (w + 1) + u = ((v * w + v) + (s * t) * (w + 1)) + u :=
  congrArg (fun z => z + u) (nright_distrib v (s * t) (w + 1))

theorem crtR (y t b s w v : Nat) (hy : y * b = 1 + s * (w + 1)) :
    (y * t) * b + v = (t + (s * t) * (w + 1)) + v :=
  congrArg (fun z => z + v)
    (Eq.trans (congrArg (fun z => z * b) (nmul_comm y t))
      (Eq.trans (nmul_assoc t y b)
        (Eq.trans (congrArg (fun z => t * z) hy)
          (Eq.trans (nleft_distrib t 1 (s * (w + 1)))
            (Eq.trans (congrArg (fun z => z + t * (s * (w + 1))) (nmul_one t))
              (Eq.trans
                (congrArg (fun z => t + z) (Eq.symm (nmul_assoc t s (w + 1))))
                (congrArg (fun z => t + z * (w + 1)) (nmul_comm t s))))))))

/-- If `y b = 1 + s (w+1)` then the number on the left is
    `K (w+1) + u` and also `L b + v`: it reads as `u` along `w + 1`
    and as `v` along `b`. -/
theorem crt_pair (w b y s u v : Nat) (hy : y * b = 1 + s * (w + 1)) :
    (v + s * (u + w * v)) * (w + 1) + u = (y * (u + w * v)) * b + v :=
  Eq.trans (crtL v s (u + w * v) w u)
    (Eq.trans (swap_ends (v * w) v ((s * (u + w * v)) * (w + 1)) u)
      (Eq.trans
        (congrArg (fun z => ((u + z) + (s * (u + w * v)) * (w + 1)) + v) (nmul_comm v w))
        (Eq.symm (crtR y (u + w * v) b s w v hy))))

/-- The algebra of placing, over any addition and multiplication. -/
theorem crt_read {R : Type u} (add mul : R → R → R) (one : R)
    (mul_one' : ∀ x, mul x one = x)
    (add_assoc' : ∀ x y z, add (add x y) z = add x (add y z))
    (mul_assoc' : ∀ x y z, mul (mul x y) z = mul x (mul y z))
    (left_distrib' : ∀ x y z, mul x (add y z) = add (mul x y) (mul x z))
    (right_distrib' : ∀ x y z, mul (add x y) z = add (mul x z) (mul y z))
    (a s t u v ea eb : R)
    (hea : ea = add one (mul s a)) (heb : eb = mul t a) :
    add (mul u ea) (mul v eb) = add u (mul (add (mul u s) (mul v t)) a) :=
  have s1 : mul u ea = add u (mul (mul u s) a) :=
    Eq.trans (congrArg (fun w => mul u w) hea)
      (Eq.trans (left_distrib' u one (mul s a))
        (Eq.trans (congrArg (fun w => add w (mul u (mul s a))) (mul_one' u))
          (congrArg (fun w => add u w) (Eq.symm (mul_assoc' u s a)))))
  have s2 : mul v eb = mul (mul v t) a :=
    Eq.trans (congrArg (fun w => mul v w) heb) (Eq.symm (mul_assoc' v t a))
  Eq.trans (congrArg (fun w => add w (mul v eb)) s1)
    (Eq.trans (congrArg (fun w => add (add u (mul (mul u s) a)) w) s2)
      (Eq.trans (add_assoc' u (mul (mul u s) a) (mul (mul v t) a))
        (congrArg (fun w => add u w)
          (Eq.symm (right_distrib' (mul u s) (mul v t) a)))))

/-! ### The moduli 2 and 3, computed -/

theorem pieces23 :
    3 = 1 + 1 * 2 ∧ 3 = 1 * 3 ∧ 4 = 2 * 2 ∧ 4 = 1 + 1 * 3 ∧ 3 + 4 = 1 + 1 * 6 :=
  ⟨rfl, rfl, rfl, rfl, rfl⟩

theorem pieces23_idem :
    rem 6 (3 * 3) = 3 ∧ rem 6 (4 * 4) = 4 ∧ rem 6 (3 * 4) = 0 :=
  ⟨rfl, rfl, rfl⟩

def rd (n : Nat) : Nat × Nat := (rem 2 n, rem 3 n)

def pl (u v : Nat) : Nat := rem 6 (3 * u + 4 * v)

theorem crt23_read_place :
    rd (pl 0 0) = (0, 0) ∧ rd (pl 0 1) = (0, 1) ∧ rd (pl 0 2) = (0, 2) ∧
    rd (pl 1 0) = (1, 0) ∧ rd (pl 1 1) = (1, 1) ∧ rd (pl 1 2) = (1, 2) :=
  ⟨rfl, rfl, rfl, rfl, rfl, rfl⟩

theorem crt23_place_read :
    pl (rd 0).1 (rd 0).2 = 0 ∧ pl (rd 1).1 (rd 1).2 = 1 ∧
    pl (rd 2).1 (rd 2).2 = 2 ∧ pl (rd 3).1 (rd 3).2 = 3 ∧
    pl (rd 4).1 (rd 4).2 = 4 ∧ pl (rd 5).1 (rd 5).2 = 5 :=
  ⟨rfl, rfl, rfl, rfl, rfl, rfl⟩

/-! ## Link 25. The Chinese remainder theorem for a list of moduli

  An entry `(w, a)` stands for the modulus `w + 1` and the value `a`.
-/

/-- A product of invertibles is invertible. -/
theorem inv_mul (a b1 b2 y1 s1 y2 s2 : Nat)
    (h1 : y1 * b1 = 1 + s1 * a) (h2 : y2 * b2 = 1 + s2 * a) :
    (y1 * y2) * (b1 * b2) = 1 + (s1 + (1 + s1 * a) * s2) * a :=
  Eq.trans (mmid4 y1 y2 b1 b2)
    (Eq.trans (congrArg (fun z => z * (y2 * b2)) h1)
      (Eq.trans (congrArg (fun z => (1 + s1 * a) * z) h2)
        (Eq.trans (nleft_distrib (1 + s1 * a) 1 (s2 * a))
          (Eq.trans
            (congrArg (fun z => z + (1 + s1 * a) * (s2 * a)) (nmul_one (1 + s1 * a)))
            (Eq.trans
              (congrArg (fun z => (1 + s1 * a) + z)
                (Eq.symm (nmul_assoc (1 + s1 * a) s2 a)))
              (Eq.trans (nadd_assoc 1 (s1 * a) (((1 + s1 * a) * s2) * a))
                (congrArg (fun z => 1 + z)
                  (Eq.symm (nright_distrib s1 ((1 + s1 * a) * s2) a)))))))))

/-- Every modulus in the list is invertible along `a`. -/
def AllInv (a : Nat) : List (Nat × Nat) → Prop
  | [] => True
  | p :: rest => (∃ y s, y * (p.1 + 1) = 1 + s * a) ∧ AllInv a rest

/-- Each later modulus is invertible along each earlier one. -/
def Good : List (Nat × Nat) → Prop
  | [] => True
  | p :: rest => AllInv (p.1 + 1) rest ∧ Good rest

def prodl : List (Nat × Nat) → Nat
  | [] => 1
  | p :: rest => (p.1 + 1) * prodl rest

/-- `c` reads as the listed value along every listed modulus. -/
def Sol (c : Nat) : List (Nat × Nat) → Prop
  | [] => True
  | p :: rest => rem (p.1 + 1) c = rem (p.1 + 1) p.2 ∧ Sol c rest

theorem prod_inv (a : Nat) :
    ∀ l : List (Nat × Nat), AllInv a l → ∃ y s, y * prodl l = 1 + s * a
  | [], _ => ⟨1, 0, Eq.symm (congrArg (fun z => 1 + z) (nzero_mul a))⟩
  | p :: rest, h =>
    have h' : (∃ y s, y * (p.1 + 1) = 1 + s * a) ∧ AllInv a rest := h
    match h'.1, prod_inv a rest h'.2 with
    | ⟨y1, s1, h1⟩, ⟨y2, s2, h2⟩ =>
      ⟨y1 * y2, s1 + (1 + s1 * a) * s2,
        inv_mul a (p.1 + 1) (prodl rest) y1 s1 y2 s2 h1 h2⟩

theorem sol_add :
    ∀ (l : List (Nat × Nat)) (c x : Nat), Sol c l → Sol (x * prodl l + c) l
  | [], _, _, _ => True.intro
  | p :: rest, c, x, h =>
    have h' : rem (p.1 + 1) c = rem (p.1 + 1) p.2 ∧ Sol c rest := h
    have e1 : x * ((p.1 + 1) * prodl rest) = (x * prodl rest) * (p.1 + 1) :=
      Eq.trans (congrArg (fun z => x * z) (nmul_comm (p.1 + 1) (prodl rest)))
        (Eq.symm (nmul_assoc x (prodl rest) (p.1 + 1)))
    have e2 : (x * (p.1 + 1)) * prodl rest = x * ((p.1 + 1) * prodl rest) :=
      nmul_assoc x (p.1 + 1) (prodl rest)
    show rem (p.1 + 1) (x * ((p.1 + 1) * prodl rest) + c) = rem (p.1 + 1) p.2
        ∧ Sol (x * ((p.1 + 1) * prodl rest) + c) rest from
      And.intro
        (Eq.trans
          (congrArg (fun z => rem (p.1 + 1) (z + c)) e1)
          (Eq.trans (rem_add_mul p.1 c (x * prodl rest)) h'.1))
        (Eq.mp (congrArg (fun z => Sol (z + c) rest) e2)
          (sol_add rest c (x * (p.1 + 1)) h'.2))

/-- The Chinese remainder theorem. -/
theorem crt_list : ∀ l : List (Nat × Nat), Good l → ∃ c, Sol c l
  | [], _ => ⟨0, True.intro⟩
  | p :: rest, h =>
    have h' : AllInv (p.1 + 1) rest ∧ Good rest := h
    match crt_list rest h'.2, prod_inv (p.1 + 1) rest h'.1 with
    | ⟨c0, hc0⟩, ⟨y, s, hy⟩ =>
      ⟨(c0 + s * (p.2 + p.1 * c0)) * (p.1 + 1) + p.2,
        show rem (p.1 + 1) ((c0 + s * (p.2 + p.1 * c0)) * (p.1 + 1) + p.2)
              = rem (p.1 + 1) p.2
            ∧ Sol ((c0 + s * (p.2 + p.1 * c0)) * (p.1 + 1) + p.2) rest from
          And.intro
            (rem_add_mul p.1 p.2 (c0 + s * (p.2 + p.1 * c0)))
            (Eq.mpr
              (congrArg (fun z => Sol z rest)
                (crt_pair p.1 (prodl rest) y s p.2 c0 hy))
              (sol_add rest c0 (y * (p.2 + p.1 * c0)) hc0))⟩

/-! ## Link 26. The factorial, from a step on pairs -/

def fstep (p : Nat × Nat) : Nat × Nat := (p.1 + 1, (p.1 + 1) * p.2)

def fact (n : Nat) : Nat := (iter n fstep (0, 1)).2

theorem fstep_fst : ∀ n : Nat, (iter n fstep (0, 1)).1 = n
  | 0 => rfl
  | n + 1 => congrArg Nat.succ (fstep_fst n)

theorem fact_zero : fact 0 = 1 := rfl

theorem fact_succ (n : Nat) : fact (n + 1) = (n + 1) * fact n :=
  congrArg (fun t => (t + 1) * fact n) (fstep_fst n)

theorem fact_values : fact 1 = 1 ∧ fact 2 = 2 ∧ fact 3 = 6 := ⟨rfl, rfl, rfl⟩

def mx (m n : Nat) : Nat := sel (sb m n) n m

/-- The factorial is positive. -/
theorem fact_pos : ∀ s : Nat, ∃ f, fact s = f + 1
  | 0 => ⟨0, rfl⟩
  | s + 1 =>
    match fact_pos s with
    | ⟨f, hf⟩ =>
      ⟨(s + 1) * f + s,
        Eq.trans (fact_succ s) (congrArg (fun t => (s + 1) * t) hf)⟩

/-- `s ≤ s!`. -/
theorem fact_ge : ∀ s : Nat, ∃ j, s + j = fact s
  | 0 => ⟨1, rfl⟩
  | s + 1 =>
    match fact_pos s with
    | ⟨f, hf⟩ =>
      ⟨(s + 1) * f,
        Eq.trans (nadd_comm (s + 1) ((s + 1) * f))
          (Eq.symm (Eq.trans (fact_succ s) (congrArg (fun t => (s + 1) * t) hf)))⟩

/-- If `1 ≤ k ≤ s` then `k` divides `s!`.
    In the middle case (`j = 0`, so `k + 1 = s + 1`) the number `k`
    enters only through `h`, so its binder is `_`. -/
theorem dvd_fact : ∀ (s k j : Nat), (k + 1) + j = s → ∃ e, fact s = (k + 1) * e
  | 0, k, j, h =>
    absurd (Eq.trans (Eq.symm (nsucc_add k j)) h) (succ_ne_zero' (k + j))
  | s + 1, _, 0, h =>
    ⟨fact s, Eq.trans (fact_succ s) (congrArg (fun t => t * fact s) (Eq.symm h))⟩
  | s + 1, k, j + 1, h =>
    match dvd_fact s k j (@succ_inj' ((k + 1) + j) s h) with
    | ⟨e, he⟩ =>
      ⟨(s + 1) * e,
        Eq.trans (fact_succ s)
          (Eq.trans (congrArg (fun t => (s + 1) * t) he)
            (Eq.trans (Eq.symm (nmul_assoc (s + 1) (k + 1) e))
              (Eq.trans (congrArg (fun t => t * e) (nmul_comm (s + 1) (k + 1)))
                (nmul_assoc (k + 1) (s + 1) e))))⟩

/-! ## Link 27. The moduli `(i+1) d + 1` -/

def md (d i : Nat) : Nat := (i + 1) * d + 1

theorem sq_lemma (w : Nat) : (w + 1) * (w + 1) = 1 + w * (w + 1 + 1) :=
  Eq.trans (congrArg (fun z => z + (w + 1)) (nsucc_mul w w))
    (nadd_comm ((w * w + w) + w) 1)

/-- With `d = k e` and `(i+1) d = w + 1`, the number
    `y = (i+1)(i+1) e` satisfies `y m(i+k) = 1 + (y + w) m(i)`. -/
theorem md_inv (i k d e w y : Nat) (he : d = k * e) (hw : (i + 1) * d = w + 1)
    (hy : y = ((i + 1) * (i + 1)) * e) :
    y * md d (i + k) = 1 + (y + w) * md d i :=
  have h_md : md d (i + k) = md d i + k * d :=
    Eq.trans (congrArg (fun z => z * d + 1) (Eq.symm (nsucc_add i k)))
      (Eq.trans (congrArg (fun z => z + 1) (nright_distrib (i + 1) k d))
        (nadd_right_comm ((i + 1) * d) (k * d) 1))
  have h_sq : y * (k * d) = (w + 1) * (w + 1) :=
    Eq.trans (congrArg (fun z => z * (k * d)) hy)
      (Eq.trans (nmul_assoc ((i + 1) * (i + 1)) e (k * d))
        (Eq.trans
          (congrArg (fun z => ((i + 1) * (i + 1)) * z) (Eq.symm (nmul_assoc e k d)))
          (Eq.trans
            (congrArg (fun z => ((i + 1) * (i + 1)) * (z * d)) (nmul_comm e k))
            (Eq.trans (Eq.symm (mmid4 (i + 1) (k * e) (i + 1) d))
              (Eq.trans
                (congrArg (fun z => ((i + 1) * z) * ((i + 1) * d)) (Eq.symm he))
                (Eq.trans (congrArg (fun z => z * ((i + 1) * d)) hw)
                  (congrArg (fun z => (w + 1) * z) hw)))))))
  have h_m : md d i = w + 1 + 1 := congrArg (fun z => z + 1) hw
  Eq.trans (congrArg (fun z => y * z) h_md)
    (Eq.trans (nleft_distrib y (md d i) (k * d))
      (Eq.trans (congrArg (fun z => y * md d i + z) h_sq)
        (Eq.trans (congrArg (fun z => y * md d i + z) (sq_lemma w))
          (Eq.trans
            (congrArg (fun z => y * md d i + (1 + w * z)) (Eq.symm h_m))
            (Eq.trans (nadd_left_comm (y * md d i) 1 (w * md d i))
              (congrArg (fun z => 1 + z)
                (Eq.symm (nright_distrib y w (md d i)))))))))

/-! ## Link 28. The beta function -/

def beta (c d i : Nat) : Nat := rem (md d i) c

theorem beta_seq_1_0 : beta 3 (fact 1) 0 = 1 ∧ beta 3 (fact 1) 1 = 0 := ⟨rfl, rfl⟩

theorem beta_seq_0_2 : beta 12 (fact 2) 0 = 0 ∧ beta 12 (fact 2) 1 = 2 := ⟨rfl, rfl⟩

/-- The entries for the indices `t, t+1, ..., t+h-1`. -/
def blist (a : Nat → Nat) (d : Nat) : Nat → Nat → List (Nat × Nat)
  | _, 0 => []
  | t, h + 1 => ((t + 1) * d, a t) :: blist a d (t + 1) h

theorem allinv (a : Nat → Nat) (s : Nat) :
    ∀ (h i k j : Nat), (k + h) + j = s →
      AllInv (md (fact s) i) (blist a (fact s) (i + (k + 1)) h)
  | 0, _, _, _, _ => True.intro
  | h + 1, i, k, j, hyp =>
    have hk : (k + 1) + (h + j) = s :=
      Eq.trans (nsucc_add k (h + j))
        (Eq.trans (congrArg Nat.succ (Eq.symm (nadd_assoc k h j)))
          (Eq.trans (Eq.symm (nsucc_add (k + h) j)) hyp))
    have hrec : ((k + 1) + h) + j = s :=
      Eq.trans (congrArg (fun z => z + j) (nsucc_add k h)) hyp
    match dvd_fact s k (h + j) hk, fact_pos s with
    | ⟨e, he⟩, ⟨f, hf⟩ =>
      have hw : (i + 1) * fact s = ((i + 1) * f + i) + 1 :=
        congrArg (fun z => (i + 1) * z) hf
      show (∃ y s', y * (((i + (k + 1)) + 1) * fact s + 1) = 1 + s' * md (fact s) i)
          ∧ AllInv (md (fact s) i) (blist a (fact s) ((i + (k + 1)) + 1) h) from
        And.intro
          ⟨((i + 1) * (i + 1)) * e, ((i + 1) * (i + 1)) * e + ((i + 1) * f + i),
            md_inv i (k + 1) (fact s) e ((i + 1) * f + i)
              (((i + 1) * (i + 1)) * e) he hw rfl⟩
          (allinv a s h i (k + 1) j hrec)

theorem good_blist (a : Nat → Nat) (s : Nat) :
    ∀ (h t j : Nat), h + j = s + 1 → Good (blist a (fact s) t h)
  | 0, _, _, _ => True.intro
  | h + 1, t, j, hj =>
    have hj' : h + j = s :=
      @succ_inj' (h + j) s (Eq.trans (Eq.symm (nsucc_add h j)) hj)
    show AllInv (((t + 1) * fact s) + 1) (blist a (fact s) (t + 1) h)
        ∧ Good (blist a (fact s) (t + 1) h) from
      And.intro
        (allinv a s h t 0 j (Eq.trans (congrArg (fun z => z + j) (nzero_add h)) hj'))
        (good_blist a s h (t + 1) (j + 1) (congrArg Nat.succ hj'))

theorem sol_get (a : Nat → Nat) (d c : Nat) :
    ∀ (h t e j : Nat), Sol c (blist a d t h) → e + (j + 1) = h →
      rem (md d (t + e)) c = rem (md d (t + e)) (a (t + e))
  | 0, _, e, j, _, hh => absurd hh (succ_ne_zero' (e + j))
  | h + 1, t, 0, _, hs, _ =>
    (show rem (md d t) c = rem (md d t) (a t) ∧ Sol c (blist a d (t + 1) h) from hs).1
  | h + 1, t, e + 1, j, hs, hh =>
    have hs' : rem (md d t) c = rem (md d t) (a t) ∧ Sol c (blist a d (t + 1) h) := hs
    have hh' : e + (j + 1) = h :=
      @succ_inj' (e + (j + 1)) h (Eq.trans (Eq.symm (nsucc_add e (j + 1))) hh)
    Eq.mp
      (congrArg (fun x => rem (md d x) c = rem (md d x) (a x)) (nsucc_add t e))
      (sol_get a d c h (t + 1) e j hs'.2 hh')

/-- A bound for the sequence: the sum of its first terms. -/
def bsum (a : Nat → Nat) : Nat → Nat
  | 0 => a 0
  | n + 1 => bsum a n + a (n + 1)

theorem bsum_ge (a : Nat → Nat) : ∀ (n i e : Nat), i + e = n → ∃ j, a i + j = bsum a n
  | 0, 0, _, _ => ⟨0, rfl⟩
  | 0, i + 1, e, h =>
    absurd (Eq.trans (Eq.symm (nsucc_add i e)) h) (succ_ne_zero' (i + e))
  | n + 1, i, 0, h =>
    ⟨bsum a n,
      Eq.trans (nadd_comm (a i) (bsum a n)) (congrArg (fun t => bsum a n + a t) h)⟩
  | n + 1, i, e + 1, h =>
    match bsum_ge a n i e (@succ_inj' (i + e) n h) with
    | ⟨j, hj⟩ =>
      ⟨j + a (n + 1),
        Eq.trans (Eq.symm (nadd_assoc (a i) j (a (n + 1))))
          (congrArg (fun t => t + a (n + 1)) hj)⟩

/-- If `x ≤ d` then `x` is below `(i+1) d + 1`. -/
theorem lt_md (i d x g : Nat) (hd : x + g = d) : x + ((i * d + g) + 1) = md d i :=
  Eq.trans (congrArg Nat.succ (nadd_left_comm x (i * d) g))
    (Eq.trans (congrArg (fun z => (i * d + z) + 1) hd)
      (congrArg (fun z => z + 1) (Eq.symm (nsucc_mul i d))))

/-- Goedel's beta function lemma. -/
theorem beta_lemma (a : Nat → Nat) (n : Nat) :
    ∃ c d, ∀ (i e : Nat), i + e = n → beta c d i = a i :=
  match crt_list (blist a (fact (n + bsum a n)) 0 (n + 1))
      (good_blist a (n + bsum a n) (n + 1) 0 (bsum a n) (nsucc_add n (bsum a n))) with
  | ⟨c, hc⟩ =>
    ⟨c, fact (n + bsum a n), fun i e hie =>
      match bsum_ge a n i e hie, fact_ge (n + bsum a n) with
      | ⟨j1, hj1⟩, ⟨j2, hj2⟩ =>
        have hd : a i + ((j1 + n) + j2) = fact (n + bsum a n) :=
          Eq.trans (Eq.symm (nadd_assoc (a i) (j1 + n) j2))
            (Eq.trans (congrArg (fun z => z + j2) (Eq.symm (nadd_assoc (a i) j1 n)))
              (Eq.trans (congrArg (fun z => (z + n) + j2) hj1)
                (Eq.trans (congrArg (fun z => z + j2) (nadd_comm (bsum a n) n)) hj2)))
        have hlt : a i + ((i * fact (n + bsum a n) + ((j1 + n) + j2)) + 1)
            = md (fact (n + bsum a n)) i :=
          lt_md i (fact (n + bsum a n)) (a i) ((j1 + n) + j2) hd
        have hsol : rem (md (fact (n + bsum a n)) (0 + i)) c
            = rem (md (fact (n + bsum a n)) (0 + i)) (a (0 + i)) :=
          sol_get a (fact (n + bsum a n)) c (n + 1) 0 i e hc (congrArg Nat.succ hie)
        have hsol' : rem (md (fact (n + bsum a n)) i) c
            = rem (md (fact (n + bsum a n)) i) (a i) :=
          Eq.mp
            (congrArg
              (fun x => rem (md (fact (n + bsum a n)) x) c
                        = rem (md (fact (n + bsum a n)) x) (a x))
              (nzero_add i))
            hsol
        Eq.trans hsol'
          (rem_of_fix (md (fact (n + bsum a n)) i) (a i)
            (fix_of_lt (md (fact (n + bsum a n)) i) (a i)
              (i * fact (n + bsum a n) + ((j1 + n) + j2)) hlt))⟩

/-! ## Link 29. A correct record of the steps is "n times" -/

theorem trace_iter {A : Type u} (f : A → A) (x : A) (tr : Nat → A)
    (h0 : tr 0 = x) (hs : ∀ i, tr (i + 1) = f (tr i)) :
    ∀ n : Nat, tr n = iter n f x
  | 0 => h0
  | n + 1 => Eq.trans (hs n) (congrArg f (trace_iter f x tr h0 hs n))

/-! ## Axiom check (all should report no axioms) -/

#print axioms e1_idem
#print axioms split_idem
#print axioms read_inj
#print axioms iter_add
#print axioms iter_mul
#print axioms nmul_comm
#print axioms nmul_assoc
#print axioms nright_distrib
#print axioms succ_inj'
#print axioms sb_add_cancel
#print axioms sb_eq
#print axioms rem_values
#print axioms rem_lt
#print axioms rem_idem
#print axioms rem_add_mul
#print axioms ustep_diag
#print axioms ustep_reaches
#print axioms euclid_line
#print axioms piece_idem
#print axioms crt_pair
#print axioms crt_read
#print axioms crt23_read_place
#print axioms crt23_place_read
#print axioms crt_list
#print axioms dvd_fact
#print axioms md_inv
#print axioms beta_seq_1_0
#print axioms beta_seq_0_2
#print axioms beta_lemma
#print axioms trace_iter

end BetaChain

found/Beth.lean

lean_all/found/Beth.lean

/-
  T148. found/Beth — Beth semantics as a point: on the binary tree of finite
  bit strings (T108's conditions), "U holds on a bar above w" (every infinite
  path through w eventually enters U) is a nucleus on upsets; Beth truth
  values are its fixed points (T108 fixHA), a Heyting algebra — so
  intuitionistic logic is sound for Beth models (T65), and Beth = sheaves for
  the bar topology (T95 line).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Forcing

namespace Found

namespace Beth

open Forcing

def shift (α : Nat → Bool) : Nat → Bool := fun k => α (k + 1)

def pfx : (Nat → Bool) → Nat → List Bool
  | _, 0 => []
  | α, n + 1 => α 0 :: pfx (shift α) n

theorem pre_pfx : (w : List Bool) → ∀ (α : Nat → Bool) (n : Nat), Pre w (pfx α n) → ∃ n', pfx α n' = w
  | [], _, _, _ => ⟨0, rfl⟩
  | b :: w, α, n, h => by
    cases n with
    | zero => cases h
    | succ n =>
      cases h with
      | cons _ h' =>
        match pre_pfx w (shift α) n h' with
        | ⟨n', e⟩ => exact ⟨n' + 1, congrArg (List.cons (α 0)) e⟩

theorem pfx_mono : (m k : Nat) → ∀ α, Pre (pfx α m) (pfx α (m + k))
  | 0, _, _ => .nil _
  | m + 1, k, α => by
    rw [Nat.add_right_comm]
    exact .cons (α 0) (pfx_mono m k (shift α))

def bar (U : Modal.Up Pre) (w : List Bool) : Prop :=
  ∀ α : Nat → Bool, (∃ n, pfx α n = w) → ∃ m, U.p (pfx α m)

def barUp (U : Modal.Up Pre) : Modal.Up Pre :=
  ⟨bar U, fun w _ r h α ⟨n, e⟩ =>
    match pre_pfx w α n (e ▸ r) with
    | ⟨n', e'⟩ => h α ⟨n', e'⟩⟩

def barNucleus : Nucleus H2 where
  j := barUp
  infl U w hU α := fun ⟨n, e⟩ => ⟨n, e ▸ hU⟩
  idem U w h α hα := match h α hα with
    | ⟨m, hm⟩ => hm α ⟨m, rfl⟩
  mono {U V} hUV w h α hα := match h α hα with
    | ⟨m, hm⟩ => ⟨m, hUV _ hm⟩
  meet U V w h α hα := match h.1 α hα, h.2 α hα with
    | ⟨m1, h1⟩, ⟨m2, h2⟩ =>
      ⟨m1 + m2, U.up _ _ (pfx_mono m1 m2 α) h1,
        V.up _ _ (by rw [Nat.add_comm]; exact pfx_mono m2 m1 α) h2⟩

/-- Beth truth values: a Heyting algebra -/
def bethHA : HA.{0} := barNucleus.fixHA

/-- soundness of intuitionistic logic in Beth models -/
theorem beth_sound {φ : Prp.Form} (d : Prp.PD true (fun _ => False) φ) (v : Nat → bethHA.car) :
    bethHA.le bethHA.top (Prp.interp bethHA v bethHA.bot φ) :=
  Prp.valid bethHA d v bethHA.bot (fun _ => bethHA.bot_le)

end Beth

end Found

#print axioms Found.Beth.pre_pfx
#print axioms Found.Beth.pfx_mono
#print axioms Found.Beth.barNucleus
#print axioms Found.Beth.bethHA
#print axioms Found.Beth.beth_sound

found/BethStages.lean

lean_all/found/BethStages.lean

/-
  T106 (b), slice N3c(v). found/BethStages — the beth stages up to ω₁ in the
  classical point.  B α = P(ℕ) ⊕ Σ (β < α), (B β → Prop) (each stage contains
  P(ℕ) and the power sets of all earlier stages), by transfinite recursion along
  ω₁ (found/Omega1); μ = Σ α, B α.
  * ω₁ has no largest element;
  * power sets of earlier stages embed in later ones, stages embed in μ;
  * stage-form strong limit: for every α, B α → Prop is strictly smaller than μ;
  * cofinality: countably many elements of ω₁ are bounded;
  * general strong limit: every ν smaller than μ injects into some stage
    (via Hessenberg, found/Hessenberg), whose power set is smaller than μ.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.CountUnion
import found.Hessenberg

namespace Found

namespace BethS

open CP WO Card NFP W1 CU

variable (K : Classical')

/-- the order of ω₁, inherited from P(ℕ) -/
def Lt (x y : W1 K) : Prop := Lt' K x.val y.val

theorem acc_back (x : PN) (h : Acc (Lt' K) x) : ∀ y : W1 K, y.val = x → Acc (Lt K) y := by
  induction h with
  | intro x _ ih => exact fun y e => .intro _ (fun z hz => ih z.val (e ▸ hz) z rfl)

theorem wfW : WellFounded (Lt K) := ⟨fun y => acc_back K y.val ((wf K PN).apply y.val) y rfl⟩

def B : W1 K → Type :=
  (wfW K).fix (fun α rec => PN ⊕ ((β : {β : W1 K // Lt K β α}) × (rec β.val β.property → Prop)))

theorem B_eq (α : W1 K) : B K α = (PN ⊕ ((β : {β : W1 K // Lt K β α}) × (B K β.val → Prop))) :=
  (wfW K).fix_eq _ α

def mu : Type := (α : W1 K) × B K α

theorem cast_inj {A C : Type} (h : A = C) {x y : A} (e : cast h x = cast h y) : x = y := by
  cases h; exact e

/-- power sets of earlier stages embed in later stages -/
def up (α β : W1 K) (h : Lt K β α) (P : B K β → Prop) : B K α :=
  cast (B_eq K α).symm (.inr ⟨⟨β, h⟩, P⟩)

theorem up_inj (α β : W1 K) (h : Lt K β α) : Inj (up K α β h) := fun P Q e => by
  have e1 := cast_inj _ e
  have e2 : (⟨⟨β, h⟩, P⟩ : (γ : {γ : W1 K // Lt K γ α}) × (B K γ.val → Prop)) = ⟨⟨β, h⟩, Q⟩ :=
    Sum.inr.inj e1
  exact eq_of_heq (Sigma.mk.inj e2).2

theorem stage_inj (α : W1 K) : Inj (fun x : B K α => (⟨α, x⟩ : mu K)) := fun _ _ e =>
  eq_of_heq (Sigma.mk.inj e).2

theorem sub_eq {x y : W1 K} (e : x.val = y.val) : x = y := by
  cases x with
  | mk a ha => cases y with
    | mk b hb => cases e; rfl

def OSpec (y : W1 K) (x : W1 K) : Option {z : W1 K // Lt' K z.val y.val} → Prop
  | none => x = y
  | some z => z.val = x

include K in
theorem ospec_ex (y : W1 K) (x : {x : W1 K // Le K x.val y.val}) : ∃ o, OSpec K y x.val o :=
  (K.lem (Lt' K x.val.val y.val)).elim (fun h => ⟨some ⟨x.val, h⟩, rfl⟩)
    (fun hn => ⟨none, sub_eq K (le_antisymm K _ _ x.property (dne K _ (fun hn' => hn ⟨x.property, hn'⟩)))⟩)

def onat {A : Type} (f : A → Nat) : Option A → Nat
  | none => 0
  | some a => f a + 1

theorem onat_inj {A : Type} (f : A → Nat) (hf : Inj f) : Inj (onat f) := fun a b e => by
  cases a with
  | none => cases b with
    | none => rfl
    | some _ => exact Nat.noConfusion e
  | some a => cases b with
    | none => exact Nat.noConfusion e
    | some b => exact congrArg some (hf a b (Nat.succ.inj e))

/-- elements up to y form a countable set -/
theorem upto_countable (y : W1 K) : Countable {x : W1 K // Le K x.val y.val} :=
  match seg_countable K y with
  | ⟨f, hf⟩ =>
    let h : {x : W1 K // Le K x.val y.val} → Option {z : W1 K // Lt' K z.val y.val} :=
      fun x => K.ch (OSpec K y x.val) (ospec_ex K y x)
    have hs : ∀ x, OSpec K y x.val (h x) := fun x => K.ch_spec (OSpec K y x.val) (ospec_ex K y x)
    ⟨fun x => onat f (h x), fun a b e => by
      have e' : h a = h b := onat_inj f hf _ _ e
      have sa := hs a
      have sb := hs b
      rw [e'] at sa
      cases hb : h b with
      | none =>
        rw [hb] at sa sb
        have : a.val = b.val := sa.trans sb.symm
        cases a with
        | mk a' _ => cases b with
          | mk b' _ => cases this; rfl
      | some z =>
        rw [hb] at sa sb
        have : a.val = b.val := sa.symm.trans sb
        cases a with
        | mk a' _ => cases b with
          | mk b' _ => cases this; rfl⟩

/-- ω₁ has no largest element -/
theorem no_max (α : W1 K) : ∃ α', Lt K α α' :=
  dne K _ (fun hn => uncountable K (by
    match upto_countable K α with
    | ⟨f, hf⟩ =>
      have hle : ∀ x : W1 K, Le K x.val α.val := fun x => (le_total K x.val α.val).elim id
        (fun hax => dne K _ (fun hn' => hn ⟨x, hax, hn'⟩))
      exact ⟨fun x => f ⟨x, hle x⟩, fun a b e => congrArg Subtype.val (hf _ _ e)⟩))

/-- cofinality: countably many elements of ω₁ are bounded -/
theorem bounded (I : Type) (hI : Countable I) (s : I → W1 K) : ∃ b, ∀ i, Lt K (s i) b :=
  match omega1_regular K I hI (fun i x => Le K x.val (s i).val) (fun i => upto_countable K (s i)) with
  | ⟨x, hx⟩ => ⟨x, fun i => ⟨(le_total K (s i).val x.val).elim id (fun h => (hx i h).elim), hx i⟩⟩

/-- the stage-form strong limit: B α → Prop is strictly smaller than μ -/
theorem strong_stage (α : W1 K) : Smaller (B K α → Prop) (mu K) :=
  match no_max K α with
  | ⟨α', h'⟩ => match no_max K α' with
    | ⟨α'', h''⟩ =>
      ⟨⟨fun P => ⟨α', up K α' α h' P⟩, fun P Q e => up_inj K α' α h' P Q (stage_inj K α' _ _ e)⟩,
       fun ⟨g, hg⟩ =>
        -- B α' → Prop ↪ B α'' ↪ μ ↪ (B α → Prop) ↪ B α' : Cantor
        (cantor (B K α')).2 ⟨fun Q => up K α' α h' (g ⟨α'', up K α'' α' h'' Q⟩),
          fun Q R e => up_inj K α'' α' h'' Q R (stage_inj K α'' _ _ (hg _ _ (up_inj K α' α h' _ _ e)))⟩⟩

def low (α : W1 K) (x : PN) : B K α := cast (B_eq K α).symm (.inl x)

theorem low_inj (α : W1 K) : Inj (low K α) := fun _ _ e => Sum.inl.inj (cast_inj _ e)

def nat_pn (n : Nat) : PN := fun m => m = n

theorem nat_pn_inj : Inj nat_pn := fun a _ e => cast (congrFun e a) rfl

theorem w1_ne : ∃ _ : W1 K, True :=
  match least K (fun _ : PN => True) ⟨fun _ => True, trivial⟩ with
  | ⟨m, _, hl⟩ => ⟨⟨m, ⟨fun _ => 0, fun y _ _ => (y.property.2 (hl y.val trivial)).elim⟩⟩, trivial⟩

include K in
/-- every type smaller than μ injects into some stage -/
theorem below_stage (ν : Type) (hν : ¬ ∃ g : mu K → ν, Inj g) : ∃ α, ∃ f : ν → B K α, Inj f := by
  refine dne K _ (fun hn => hν ?_)
  have hall : ∀ α : W1 K, ∃ g : B K α → ν, Inj g := fun α =>
    (comparable K (α := ν) (β := B K α)).elim (fun hf => (hn ⟨α, hf⟩).elim) id
  let c : (α : W1 K) → B K α → ν := fun α => K.ch (fun g : B K α → ν => Inj g) (hall α)
  have hc : ∀ α, Inj (c α) := fun α => K.ch_spec (fun g : B K α → ν => Inj g) (hall α)
  match w1_ne K with
  | ⟨α0, _⟩ =>
    let w : W1 K → ν := fun x => c α0 (low K α0 x.val)
    have hw : Inj w := fun x y e => Subtype.ext (low_inj K α0 _ _ (hc α0 _ _ e))
    let n : Nat → ν := fun k => c α0 (low K α0 (nat_pn k))
    have hnn : Inj n := fun a b e => nat_pn_inj _ _ (low_inj K α0 _ _ (hc α0 _ _ e))
    match Hess.hessenberg K hnn with
    | ⟨F, hF⟩ =>
      exact ⟨fun p => F (w p.1, c p.1 p.2), fun p q e => by
        have e' := hF _ _ e
        have e1 : w p.1 = w q.1 := congrArg Prod.fst e'
        have e2 : c p.1 p.2 = c q.1 q.2 := congrArg Prod.snd e'
        cases p with
        | mk a x => cases q with
          | mk b y =>
            have hab : a = b := hw _ _ e1
            subst hab
            have hxy : x = y := hc a _ _ e2
            subst hxy
            rfl⟩

/-- the general strong limit: ν < μ gives a stage B α ⊇ ν with P(B α) < μ -/
theorem strong_limit (ν : Type) (hν : ¬ ∃ g : mu K → ν, Inj g) :
    ∃ α, (∃ f : ν → B K α, Inj f) ∧ Smaller (B K α → Prop) (mu K) :=
  match below_stage K ν hν with
  | ⟨α, hf⟩ => ⟨α, hf, strong_stage K α⟩

end BethS

end Found

#print axioms Found.BethS.no_max
#print axioms Found.BethS.bounded
#print axioms Found.BethS.strong_stage
#print axioms Found.BethS.below_stage
#print axioms Found.BethS.strong_limit

found/Bishop.lean

lean_all/found/Bishop.lean

/-
  T118. found/Bishop — Bishop's constructive reals as a point: the setoid
  coordinate applied to regular sequences.
  * x is a sequence of integers p n read as p n / (n+1), regular:
      |p m (n+1) − p n (m+1)| ≤ (m+1) + (n+1)   (Bishop: |x_m − x_n| ≤ 1/m + 1/n);
  * x ≈ y  :⟺  ∀ n, |p n − q n| ≤ 2   (|x_n − y_n| ≤ 2/n);
  * ≈ is an equivalence (transitivity by Bishop's argument: for every m,
    |x_n − z_n|·(m+1) ≤ 2(m+1) + 6(n+1), then an Archimedean step);
  * negation and the integers embed, respecting ≈.
  Order is ∃k, b = a + k (no core Int order lemmas: they use propext);
  ring identities by lib_int (lib_shared). Arithmetic of sums/products and
  the approximate intermediate value theorem are the continuation (cited).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import lib.IntRing

namespace Found

namespace Bishop

def LE' (a b : Int) : Prop := ∃ k : Nat, b = a + Int.ofNat k

theorem le_refl' (a : Int) : LE' a a := ⟨0, by show a = a + 0; lib_int⟩

theorem le_trans' {a b c : Int} (h1 : LE' a b) (h2 : LE' b c) : LE' a c :=
  match h1, h2 with
  | ⟨k, e1⟩, ⟨l, e2⟩ => ⟨k + l, by
      rw [e2, e1]
      show a + Int.ofNat k + Int.ofNat l = a + (Int.ofNat k + Int.ofNat l)
      lib_int⟩

theorem le_add' {a b c d : Int} (h1 : LE' a b) (h2 : LE' c d) : LE' (a + c) (b + d) :=
  match h1, h2 with
  | ⟨k, e1⟩, ⟨l, e2⟩ => ⟨k + l, by
      rw [e1, e2]
      show a + Int.ofNat k + (c + Int.ofNat l) = a + c + (Int.ofNat k + Int.ofNat l)
      lib_int⟩

theorem le_mulN {a b : Int} (m : Nat) (h : LE' a b) : LE' (a * Int.ofNat m) (b * Int.ofNat m) :=
  match h with
  | ⟨k, e⟩ => ⟨k * m, by
      rw [e]
      show (a + Int.ofNat k) * Int.ofNat m = a * Int.ofNat m + Int.ofNat k * Int.ofNat m
      lib_int⟩

theorem le_neg' {a b : Int} (h : LE' a b) : LE' (-b) (-a) :=
  match h with
  | ⟨k, e⟩ => ⟨k, by rw [e]; lib_int⟩

theorem le_eq {a b c : Int} (e : a = b) (h : LE' b c) : LE' a c := e ▸ h
theorem le_eq' {a b c : Int} (h : LE' a b) (e : b = c) : LE' a c := e ▸ h

/-- |a| ≤ c -/
def Bnd (a c : Int) : Prop := LE' (-c) a ∧ LE' a c

theorem bnd_neg {a c : Int} (h : Bnd a c) : Bnd (-a) c :=
  ⟨le_neg' h.2, le_eq' (le_neg' h.1) (by lib_int)⟩

theorem bnd_eq {a b c : Int} (e : a = b) (h : Bnd b c) : Bnd a c := e ▸ h

def N (n : Nat) : Int := Int.ofNat (n + 1)

structure Real where
  p : Nat → Int
  reg : ∀ m n, Bnd (p m * N n - p n * N m) (N m + N n)

def Eqv (x y : Real) : Prop := ∀ n, Bnd (x.p n - y.p n) 2

theorem eqv_refl (x : Real) : Eqv x x := fun n =>
  ⟨⟨2, by show x.p n - x.p n = -2 + 2; lib_int⟩, ⟨2, by show (2 : Int) = x.p n - x.p n + 2; lib_int⟩⟩

theorem eqv_symm {x y : Real} (h : Eqv x y) : Eqv y x := fun n =>
  match bnd_neg (h n) with
  | ⟨h1, h2⟩ => ⟨le_eq' h1 (by lib_int), le_eq (by lib_int) h2⟩

/-- the four-term estimate: (x_n − z_n)(m+1) ≤ 2(m+1) + 6(n+1) -/
theorem four_upper {x y z : Real} (h1 : Eqv x y) (h2 : Eqv y z) (n m : Nat) :
    LE' ((x.p n - z.p n) * N m) (2 * N m + 6 * N n) := by
  have a1 : LE' (-(x.p m * N n - x.p n * N m)) (N m + N n) := le_neg' (x.reg m n).1 |> fun h => le_eq' h (by lib_int)
  have a2 : LE' ((x.p m - y.p m) * N n) (2 * N n) := le_mulN (n + 1) (h1 m).2
  have a3 : LE' ((y.p m - z.p m) * N n) (2 * N n) := le_mulN (n + 1) (h2 m).2
  have a4 : LE' (z.p m * N n - z.p n * N m) (N m + N n) := (z.reg m n).2
  have s := le_add' (le_add' (le_add' a1 a2) a3) a4
  exact le_eq (by lib_int) (le_eq' s (by lib_int))

/-- Archimedean step: if d(m+1) ≤ 2(m+1) + c for all m then d ≤ 2 -/
theorem arch (d : Int) (c : Nat) (h : ∀ m : Nat, LE' (d * N m) (2 * N m + Int.ofNat c)) : LE' d 2 := by
  have hd : d = 2 + (d - 2) := by lib_int
  generalize he : d - 2 = e at hd
  cases e with
  | ofNat k =>
    cases k with
    | zero => exact ⟨0, by rw [hd]; show (2 : Int) = 2 + 0 + 0; lib_int⟩
    | succ k =>
      exfalso
      match h c with
      | ⟨j, ej⟩ =>
        have e2 : Int.ofNat (2 * (c + 1) + c) = Int.ofNat (2 * (c + 1) + (k + 1) * (c + 1) + j) := by
          show 2 * N c + Int.ofNat c = _
          rw [ej, hd]
          show (2 + Int.ofNat (k + 1)) * N c + Int.ofNat j = Int.ofNat (2 * (c + 1) + (k + 1) * (c + 1) + j)
          show (2 + Int.ofNat (k + 1)) * Int.ofNat (c + 1) + Int.ofNat j =
            2 * Int.ofNat (c + 1) + Int.ofNat (k + 1) * Int.ofNat (c + 1) + Int.ofNat j
          lib_int
        have e3 := Int.ofNat.inj e2
        have l1 : c + 1 ≤ (k + 1) * (c + 1) := Nat.le_mul_of_pos_left _ (Nat.succ_pos k)
        have l2 : 2 * (c + 1) + (c + 1) ≤ 2 * (c + 1) + (k + 1) * (c + 1) + j :=
          Nat.le_trans (Nat.add_le_add_left l1 _) (Nat.le_add_right _ _)
        rw [← e3] at l2
        exact Nat.lt_irrefl _ (Nat.lt_of_lt_of_le (Nat.add_lt_add_left (Nat.lt_succ_self c) _) l2)
  | negSucc k =>
    exact ⟨k + 1, by
      rw [hd]
      show (2 : Int) = 2 + Int.negSucc k + Int.ofNat (k + 1)
      show (2 : Int) = 2 + -Int.ofNat (k + 1) + Int.ofNat (k + 1)
      lib_int⟩

theorem eqv_trans {x y z : Real} (h1 : Eqv x y) (h2 : Eqv y z) : Eqv x z := fun n =>
  ⟨le_eq' (le_neg' (arch (-(x.p n - z.p n)) (6 * (n + 1)) (fun m =>
      le_eq (by lib_int) (le_eq' (four_upper (eqv_symm h2) (eqv_symm h1) n m) (by show _ = _ + Int.ofNat (6 * (n + 1)); show 2 * N m + 6 * Int.ofNat (n + 1) = 2 * N m + Int.ofNat 6 * Int.ofNat (n + 1); rfl))))) (by lib_int),
   arch (x.p n - z.p n) (6 * (n + 1)) (fun m => le_eq' (four_upper h1 h2 n m) rfl)⟩

/-! ## negation and the integers -/

def neg (x : Real) : Real :=
  ⟨fun n => -(x.p n), fun m n => match bnd_neg (x.reg m n) with
    | ⟨h1, h2⟩ => ⟨le_eq' h1 (by lib_int), le_eq (by lib_int) h2⟩⟩

theorem neg_resp {x y : Real} (h : Eqv x y) : Eqv (neg x) (neg y) := fun n =>
  match bnd_neg (h n) with
  | ⟨h1, h2⟩ => ⟨le_eq' h1 (by show -(x.p n - y.p n) = -(x.p n) - -(y.p n); lib_int),
                 le_eq (by show -(x.p n) - -(y.p n) = -(x.p n - y.p n); lib_int) h2⟩

theorem neg_neg (x : Real) : Eqv (neg (neg x)) x := fun n =>
  bnd_eq (by show -(-(x.p n)) - x.p n = x.p n - x.p n; lib_int) (eqv_refl x n)

def ofInt (k : Int) : Real :=
  ⟨fun n => k * N n, fun m n => by
    have e : k * N m * N n - k * N n * N m = 0 := by lib_int
    rw [e]
    exact ⟨⟨m + 1 + (n + 1), by show (0 : Int) = -(N m + N n) + (N m + N n); lib_int⟩,
           ⟨m + 1 + (n + 1), by show N m + N n = 0 + (N m + N n); lib_int⟩⟩⟩

theorem ofInt_neg (k : Int) : Eqv (neg (ofInt k)) (ofInt (-k)) := fun n =>
  bnd_eq (by show -(k * N n) - -k * N n = 0 - 0; lib_int)
    (⟨⟨2, by show (0 : Int) - 0 = -2 + 2; lib_int⟩, ⟨2, by show (2 : Int) = 0 - 0 + 2; lib_int⟩⟩ :
      Bnd (0 - 0) 2)

end Bishop

end Found

#print axioms Found.Bishop.four_upper
#print axioms Found.Bishop.arch
#print axioms Found.Bishop.eqv_trans
#print axioms Found.Bishop.neg_resp
#print axioms Found.Bishop.neg_neg
#print axioms Found.Bishop.ofInt
#print axioms Found.Bishop.ofInt_neg

found/Bishop2.lean

lean_all/found/Bishop2.lean

/-
  T118, Bishop's reals with arithmetic, part 1. found/Bishop2 — regular
  sequences of rationals with a modulus constant (Bishop–Bridges):
  x_n = a_n / (b_n + 1) with |x_m − x_n| ≤ C (1/(m+1) + 1/(n+1)), all
  inequalities cross-multiplied into integers (LE' and Bnd of
  found/Bishop, ring identities by lib_int).  The reals of found/Bishop
  (numerators over n+1, C = 1) embed.  Sums are exact: the moduli add.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Bishop

namespace Found

namespace Bishop2

open Bishop

def D (b : Nat → Nat) (n : Nat) : Int := Int.ofNat (b n + 1)

structure R where
  a : Nat → Int
  b : Nat → Nat
  C : Nat
  reg : ∀ m n, Bnd ((a m * D b n - a n * D b m) * N m * N n) (Int.ofNat C * (N m + N n) * D b m * D b n)

theorem bnd_add {a a' c c' : Int} (h : Bnd a c) (h' : Bnd a' c') : Bnd (a + a') (c + c') :=
  ⟨le_eq (by lib_int) (le_add' h.1 h'.1), le_add' h.2 h'.2⟩

theorem bnd_mul {a c : Int} (h : Bnd a c) (k : Nat) : Bnd (a * Int.ofNat k) (c * Int.ofNat k) :=
  ⟨le_eq (by lib_int) (le_mulN k h.1), le_mulN k h.2⟩

theorem bnd_eq2 {a a' c c' : Int} (ea : a = a') (ec : c = c') (h : Bnd a c) : Bnd a' c' := ea ▸ ec ▸ h

/-- the reals of found/Bishop embed (denominator n + 1, modulus 1) -/
def ofReal (x : Real) : R where
  a := x.p
  b := fun n => n
  C := 1
  reg m n := bnd_eq2 (by show _ = (x.p m * N n - x.p n * N m) * N m * N n; rfl)
    (by show (N m + N n) * N m * N n = (1 : Int) * (N m + N n) * N m * N n; lib_int)
    (bnd_mul (bnd_mul (x.reg m n) (m + 1)) (n + 1))

/-- the sum: x_n + y_n, moduli added -/
def add (x y : R) : R where
  a := fun n => x.a n * D y.b n + y.a n * D x.b n
  b := fun n => x.b n * y.b n + x.b n + y.b n
  C := x.C + y.C
  reg m n := by
    have hx := bnd_mul (bnd_mul (x.reg m n) (y.b m + 1)) (y.b n + 1)
    have hy := bnd_mul (bnd_mul (y.reg m n) (x.b m + 1)) (x.b n + 1)
    have hD : ∀ k, D (fun n => x.b n * y.b n + x.b n + y.b n) k = D x.b k * D y.b k := fun k => by
      show Int.ofNat (x.b k * y.b k + x.b k + y.b k + 1) = Int.ofNat (x.b k + 1) * Int.ofNat (y.b k + 1)
      show Int.ofNat (x.b k) * Int.ofNat (y.b k) + Int.ofNat (x.b k) + Int.ofNat (y.b k) + 1 =
        (Int.ofNat (x.b k) + 1) * (Int.ofNat (y.b k) + 1)
      lib_int
    rw [hD m, hD n]
    refine bnd_eq2 ?_ ?_ (bnd_add hx hy)
    · show (x.a m * D x.b n - x.a n * D x.b m) * N m * N n * D y.b m * D y.b n +
          (y.a m * D y.b n - y.a n * D y.b m) * N m * N n * D x.b m * D x.b n = _
      lib_int
    · show Int.ofNat x.C * (N m + N n) * D x.b m * D x.b n * D y.b m * D y.b n +
          Int.ofNat y.C * (N m + N n) * D y.b m * D y.b n * D x.b m * D x.b n = _
      show _ = (Int.ofNat x.C + Int.ofNat y.C) * (N m + N n) * (D x.b m * D y.b m) * (D x.b n * D y.b n)
      lib_int

/-- equivalence: |x_n − y_n| ≤ (C_x + C_y) / (n + 1) -/
def Eqv (x y : R) : Prop :=
  ∀ n, Bnd ((x.a n * D y.b n - y.a n * D x.b n) * N n) ((Int.ofNat x.C + Int.ofNat y.C) * D x.b n * D y.b n)

theorem bnd_zero (k : Nat) : Bnd 0 (Int.ofNat k) :=
  ⟨⟨k, by show (0 : Int) = -Int.ofNat k + Int.ofNat k; lib_int⟩, ⟨k, by show Int.ofNat k = 0 + Int.ofNat k; lib_int⟩⟩

theorem eqv_refl (x : R) : Eqv x x := fun n =>
  bnd_eq2 (by lib_int) (show Int.ofNat ((x.C + x.C) * (x.b n + 1) * (x.b n + 1)) = _ from rfl)
    (bnd_zero ((x.C + x.C) * (x.b n + 1) * (x.b n + 1)))

theorem eqv_symm {x y : R} (h : Eqv x y) : Eqv y x := fun n =>
  bnd_eq2 (by lib_int) (by lib_int) (bnd_neg (h n))

/-! ## equality as eventual closeness -/

/-- cancel a positive natural factor in an integer inequality -/
theorem le_cancel {a b : Int} (k : Nat) (h : LE' (a * Int.ofNat (k + 1)) (b * Int.ofNat (k + 1))) : LE' a b :=
  match h with
  | ⟨j, e⟩ => by
    have hd : (b - a) * Int.ofNat (k + 1) = Int.ofNat j := by
      have : b * Int.ofNat (k + 1) - a * Int.ofNat (k + 1) = Int.ofNat j := by rw [e]; lib_int
      rw [← this]; lib_int
    have hb : b = a + (b - a) := by lib_int
    generalize b - a = d at hd hb
    cases d with
    | ofNat t => exact ⟨t, hb⟩
    | negSucc t =>
      exfalso
      have e2 : Int.negSucc t * Int.ofNat (k + 1) = Int.negSucc (t * (k + 1) + k) := by
        show -Int.ofNat (t + 1) * Int.ofNat (k + 1) = -Int.ofNat (t * (k + 1) + k + 1)
        show -Int.ofNat (t + 1) * Int.ofNat (k + 1) = -(Int.ofNat t * Int.ofNat (k + 1) + Int.ofNat k + 1)
        show -(Int.ofNat t + 1) * (Int.ofNat k + 1) = -(Int.ofNat t * (Int.ofNat k + 1) + Int.ofNat k + 1)
        lib_int
      rw [e2] at hd
      exact Int.noConfusion hd

theorem bnd_cancel {a c : Int} (k : Nat) (h : Bnd (a * Int.ofNat (k + 1)) (c * Int.ofNat (k + 1))) : Bnd a c :=
  ⟨le_cancel k (le_eq (by lib_int) h.1), le_cancel k h.2⟩

/-- x and y are equal: for every k, eventually |x_n − y_n| ≤ 1/(k+1) -/
def EqC (x y : R) : Prop :=
  ∀ k, ∃ M, ∀ n, M ≤ n → Bnd ((x.a n * D y.b n - y.a n * D x.b n) * N k) (D x.b n * D y.b n)

theorem eqC_refl (x : R) : EqC x x := fun _ => ⟨0, fun n _ =>
  bnd_eq2 (by lib_int) (show Int.ofNat ((x.b n + 1) * (x.b n + 1)) = _ from rfl) (bnd_zero _)⟩

theorem eqC_symm {x y : R} (h : EqC x y) : EqC y x := fun k => match h k with
  | ⟨M, hM⟩ => ⟨M, fun n hn => bnd_eq2 (by lib_int) (by lib_int) (bnd_neg (hM n hn))⟩

theorem le_max_l (a b : Nat) : a ≤ a + b := Nat.le_add_right a b
theorem le_max_r (a b : Nat) : b ≤ a + b := Nat.le_add_left b a

theorem eqC_trans {x y z : R} (h1 : EqC x y) (h2 : EqC y z) : EqC x z := fun k =>
  match h1 (2 * k + 1), h2 (2 * k + 1) with
  | ⟨M1, hM1⟩, ⟨M2, hM2⟩ => ⟨M1 + M2, fun n hn => by
      have b1 := bnd_mul (hM1 n (Nat.le_trans (le_max_l M1 M2) hn)) (z.b n + 1)
      have b2 := bnd_mul (hM2 n (Nat.le_trans (le_max_r M1 M2) hn)) (x.b n + 1)
      have s := bnd_add b1 b2
      apply bnd_cancel (2 * y.b n + 1)
      refine bnd_eq2 ?_ ?_ s
      · show (x.a n * D y.b n - y.a n * D x.b n) * N (2 * k + 1) * D z.b n +
            (y.a n * D z.b n - z.a n * D y.b n) * N (2 * k + 1) * D x.b n =
          (x.a n * D z.b n - z.a n * D x.b n) * N k * Int.ofNat (2 * y.b n + 1 + 1)
        show (x.a n * D y.b n - y.a n * D x.b n) * (2 * N k) * D z.b n +
            (y.a n * D z.b n - z.a n * D y.b n) * (2 * N k) * D x.b n =
          (x.a n * D z.b n - z.a n * D x.b n) * N k * (2 * D y.b n)
        lib_int
      · show D x.b n * D y.b n * D z.b n + D y.b n * D z.b n * D x.b n = D x.b n * D z.b n * (2 * D y.b n)
        lib_int⟩

theorem D_add (x y : R) (k : Nat) : D (add x y).b k = D x.b k * D y.b k := by
  show Int.ofNat (x.b k * y.b k + x.b k + y.b k + 1) = Int.ofNat (x.b k + 1) * Int.ofNat (y.b k + 1)
  show Int.ofNat (x.b k) * Int.ofNat (y.b k) + Int.ofNat (x.b k) + Int.ofNat (y.b k) + 1 =
    (Int.ofNat (x.b k) + 1) * (Int.ofNat (y.b k) + 1)
  lib_int

/-- the sum respects equality -/
theorem add_resp {x x' y y' : R} (hx : EqC x x') (hy : EqC y y') : EqC (add x y) (add x' y') := fun k =>
  match hx (2 * k + 1), hy (2 * k + 1) with
  | ⟨M1, hM1⟩, ⟨M2, hM2⟩ => ⟨M1 + M2, fun n hn => by
      have b1 := bnd_mul (bnd_mul (hM1 n (Nat.le_trans (le_max_l M1 M2) hn)) (y.b n + 1)) (y'.b n + 1)
      have b2 := bnd_mul (bnd_mul (hM2 n (Nat.le_trans (le_max_r M1 M2) hn)) (x.b n + 1)) (x'.b n + 1)
      have s := bnd_add b1 b2
      apply bnd_cancel 1
      rw [D_add, D_add]
      refine bnd_eq2 ?_ ?_ s
      · show (x.a n * D x'.b n - x'.a n * D x.b n) * N (2 * k + 1) * D y.b n * D y'.b n +
            (y.a n * D y'.b n - y'.a n * D y.b n) * N (2 * k + 1) * D x.b n * D x'.b n =
          ((x.a n * D y.b n + y.a n * D x.b n) * (D x'.b n * D y'.b n) -
            (x'.a n * D y'.b n + y'.a n * D x'.b n) * (D x.b n * D y.b n)) * N k * Int.ofNat (1 + 1)
        show (x.a n * D x'.b n - x'.a n * D x.b n) * (2 * N k) * D y.b n * D y'.b n +
            (y.a n * D y'.b n - y'.a n * D y.b n) * (2 * N k) * D x.b n * D x'.b n =
          ((x.a n * D y.b n + y.a n * D x.b n) * (D x'.b n * D y'.b n) -
            (x'.a n * D y'.b n + y'.a n * D x'.b n) * (D x.b n * D y.b n)) * N k * 2
        lib_int
      · show D x.b n * D x'.b n * D y.b n * D y'.b n + D y.b n * D y'.b n * D x.b n * D x'.b n =
          D x.b n * D y.b n * (D x'.b n * D y'.b n) * 2
        lib_int⟩

/-! ## bounds and products -/

theorem bnd_natAbs (a : Int) : Bnd a (Int.ofNat a.natAbs) := by
  cases a with
  | ofNat t => exact ⟨⟨t + t, by show Int.ofNat t = -Int.ofNat t + Int.ofNat (t + t); show Int.ofNat t = -Int.ofNat t + (Int.ofNat t + Int.ofNat t); lib_int⟩,
      ⟨0, by show Int.ofNat t = Int.ofNat t + 0; lib_int⟩⟩
  | negSucc t => exact ⟨⟨0, by show Int.negSucc t = -Int.ofNat (t + 1) + 0; show -Int.ofNat (t + 1) = -Int.ofNat (t + 1) + 0; lib_int⟩,
      ⟨t + 1 + (t + 1), (by
        show Int.ofNat (t + 1) = -Int.ofNat (t + 1) + (Int.ofNat (t + 1) + Int.ofNat (t + 1))
        lib_int)⟩⟩

/-- product of two bounded quantities -/
theorem bnd_mul2 {A B : Int} {c e : Nat} (hA : Bnd A (Int.ofNat c)) (hB : Bnd B (Int.ofNat e)) :
    Bnd (A * B) (Int.ofNat c * Int.ofNat e) := by
  cases B with
  | ofNat t =>
    have h1 := le_mulN t hA.1
    have h2 := le_mulN t hA.2
    have he : LE' (Int.ofNat c * Int.ofNat t) (Int.ofNat c * Int.ofNat e) :=
      le_eq (by lib_int) (le_eq' (le_mulN c hB.2) (by lib_int))
    exact ⟨le_trans' (le_eq' (le_neg' he) (by lib_int)) (le_eq' h1 rfl), le_trans' h2 he⟩
  | negSucc t =>
    have hB' : LE' (Int.ofNat (t + 1)) (Int.ofNat e) := le_eq (by show Int.ofNat (t + 1) = -Int.negSucc t; rfl) (le_eq' (le_neg' hB.1) (by lib_int))
    have h1 := le_mulN (t + 1) hA.1
    have h2 := le_mulN (t + 1) hA.2
    have he : LE' (Int.ofNat c * Int.ofNat (t + 1)) (Int.ofNat c * Int.ofNat e) :=
      le_eq (by lib_int) (le_eq' (le_mulN c hB') (by lib_int))
    have eB : A * Int.negSucc t = -(A * Int.ofNat (t + 1)) := by
      show A * -Int.ofNat (t + 1) = -(A * Int.ofNat (t + 1)); lib_int
    rw [eB]
    exact ⟨le_neg' (le_trans' h2 he), le_trans' (le_eq' (le_neg' h1) (by lib_int)) he⟩

theorem bnd_weaken {a c c' : Int} (h : Bnd a c) (hc : LE' c c') : Bnd a c' :=
  ⟨le_trans' (le_neg' hc) h.1, le_trans' h.2 hc⟩

/-- divide by a positive factor when the bound is a natural number -/
theorem bnd_div {A : Int} {c d : Nat} (h : Bnd (A * Int.ofNat (d + 1)) (Int.ofNat c)) : Bnd A (Int.ofNat c) := by
  cases A with
  | ofNat t =>
    have up : LE' (Int.ofNat t) (Int.ofNat t * Int.ofNat (d + 1)) := ⟨t * d, (by
      show Int.ofNat t * (Int.ofNat d + 1) = Int.ofNat t + Int.ofNat t * Int.ofNat d
      lib_int)⟩
    exact ⟨⟨c + t, (by show Int.ofNat t = -Int.ofNat c + (Int.ofNat c + Int.ofNat t); lib_int)⟩, le_trans' up h.2⟩
  | negSucc t =>
    have dn : LE' (Int.negSucc t * Int.ofNat (d + 1)) (Int.negSucc t) := ⟨t * d + d, (by
      show -(Int.ofNat t + 1) = -(Int.ofNat t + 1) * (Int.ofNat d + 1) + (Int.ofNat t * Int.ofNat d + Int.ofNat d)
      lib_int)⟩
    exact ⟨le_trans' h.1 dn, ⟨c + t + 1, (by
      show Int.ofNat c = -(Int.ofNat t + 1) + (Int.ofNat c + Int.ofNat t + 1)
      lib_int)⟩⟩

/-- a bound for all terms: |x_n| ≤ K -/
def K (x : R) : Nat := (x.a 0).natAbs + 2 * x.C * (x.b 0 + 1)

theorem bound (x : R) (n : Nat) : Bnd (x.a n) (Int.ofNat (K x * (x.b n + 1))) := by
  have r := x.reg n 0
  have w : LE' (Int.ofNat x.C * (N n + N 0) * D x.b n * D x.b 0)
      ((2 * Int.ofNat x.C * D x.b n * D x.b 0) * N n) := ⟨x.C * n * (x.b n + 1) * (x.b 0 + 1), (by
    show 2 * Int.ofNat x.C * Int.ofNat (x.b n + 1) * Int.ofNat (x.b 0 + 1) * Int.ofNat (n + 1) =
      Int.ofNat x.C * (Int.ofNat (n + 1) + Int.ofNat (0 + 1)) * Int.ofNat (x.b n + 1) * Int.ofNat (x.b 0 + 1) +
        Int.ofNat x.C * Int.ofNat n * Int.ofNat (x.b n + 1) * Int.ofNat (x.b 0 + 1)
    show 2 * Int.ofNat x.C * (Int.ofNat (x.b n) + 1) * (Int.ofNat (x.b 0) + 1) * (Int.ofNat n + 1) =
      Int.ofNat x.C * ((Int.ofNat n + 1) + 1) * (Int.ofNat (x.b n) + 1) * (Int.ofNat (x.b 0) + 1) +
        Int.ofNat x.C * Int.ofNat n * (Int.ofNat (x.b n) + 1) * (Int.ofNat (x.b 0) + 1)
    lib_int)⟩
  have r1 := bnd_weaken r w
  have r2 : Bnd (x.a n * D x.b 0 - x.a 0 * D x.b n) (2 * Int.ofNat x.C * D x.b n * D x.b 0) :=
    bnd_cancel n (bnd_eq2 (by show _ = (x.a n * D x.b 0 - x.a 0 * D x.b n) * N n; show _ * N n * (1 : Int) = _; lib_int)
      rfl r1)
  have r3 := bnd_add r2 (bnd_mul (bnd_natAbs (x.a 0)) (x.b n + 1))
  have r4 : Bnd (x.a n * Int.ofNat (x.b 0 + 1)) (Int.ofNat (K x * (x.b n + 1))) := bnd_eq2 (by
      show x.a n * D x.b 0 - x.a 0 * D x.b n + x.a 0 * Int.ofNat (x.b n + 1) = x.a n * D x.b 0
      show x.a n * D x.b 0 - x.a 0 * D x.b n + x.a 0 * D x.b n = x.a n * D x.b 0
      lib_int) (by
      show 2 * Int.ofNat x.C * Int.ofNat (x.b n + 1) * Int.ofNat (x.b 0 + 1) +
          Int.ofNat (x.a 0).natAbs * Int.ofNat (x.b n + 1) =
        (Int.ofNat (x.a 0).natAbs + 2 * Int.ofNat x.C * Int.ofNat (x.b 0 + 1)) * Int.ofNat (x.b n + 1)
      lib_int) r3
  exact bnd_div r4

/-- the product: x_n y_n, modulus K_x C_y + K_y C_x -/
def mul (x y : R) : R where
  a := fun n => x.a n * y.a n
  b := fun n => x.b n * y.b n + x.b n + y.b n
  C := K x * y.C + K y * x.C
  reg m n := by
    have hD : ∀ k, D (fun n => x.b n * y.b n + x.b n + y.b n) k = D x.b k * D y.b k := fun k => by
      show Int.ofNat (x.b k * y.b k + x.b k + y.b k + 1) = Int.ofNat (x.b k + 1) * Int.ofNat (y.b k + 1)
      show Int.ofNat (x.b k) * Int.ofNat (y.b k) + Int.ofNat (x.b k) + Int.ofNat (y.b k) + 1 =
        (Int.ofNat (x.b k) + 1) * (Int.ofNat (y.b k) + 1)
      lib_int
    rw [hD m, hD n]
    -- the two factors
    have f1 : Bnd (x.a m * D x.b n) (Int.ofNat (K x * (x.b m + 1) * (x.b n + 1))) := bnd_mul (bound x m) (x.b n + 1)
    have g1 : Bnd ((y.a m * D y.b n - y.a n * D y.b m) * N m * N n)
        (Int.ofNat (y.C * ((m + 1) + (n + 1)) * (y.b m + 1) * (y.b n + 1))) := y.reg m n
    have f2 : Bnd (y.a n * D y.b m) (Int.ofNat (K y * (y.b n + 1) * (y.b m + 1))) := bnd_mul (bound y n) (y.b m + 1)
    have g2 : Bnd ((x.a m * D x.b n - x.a n * D x.b m) * N m * N n)
        (Int.ofNat (x.C * ((m + 1) + (n + 1)) * (x.b m + 1) * (x.b n + 1))) := x.reg m n
    have t := bnd_add (bnd_mul2 f1 g1) (bnd_mul2 f2 g2)
    refine bnd_eq2 ?_ ?_ t
    · show x.a m * D x.b n * ((y.a m * D y.b n - y.a n * D y.b m) * N m * N n) +
          y.a n * D y.b m * ((x.a m * D x.b n - x.a n * D x.b m) * N m * N n) =
        (x.a m * y.a m * (D x.b n * D y.b n) - x.a n * y.a n * (D x.b m * D y.b m)) * N m * N n
      lib_int
    · show Int.ofNat (K x) * D x.b m * D x.b n * (Int.ofNat y.C * (N m + N n) * D y.b m * D y.b n) +
          Int.ofNat (K y) * D y.b n * D y.b m * (Int.ofNat x.C * (N m + N n) * D x.b m * D x.b n) =
        (Int.ofNat (K x) * Int.ofNat y.C + Int.ofNat (K y) * Int.ofNat x.C) * (N m + N n) *
          (D x.b m * D y.b m) * (D x.b n * D y.b n)
      lib_int

theorem D_mul (x y : R) (k : Nat) : D (mul x y).b k = D x.b k * D y.b k := D_add x y k

/-- the product respects equality -/
theorem mul_resp {x x' y y' : R} (hx : EqC x x') (hy : EqC y y') : EqC (mul x y) (mul x' y') := fun k =>
  match hx ((K x + K y' + 1) * k + (K x + K y')), hy ((K x + K y' + 1) * k + (K x + K y')) with
  | ⟨M1, hM1⟩, ⟨M2, hM2⟩ => ⟨M1 + M2, fun n hn => by
      have ex := hM1 n (Nat.le_trans (le_max_l M1 M2) hn)
      have ey := hM2 n (Nat.le_trans (le_max_r M1 M2) hn)
      have f1 : Bnd (x.a n * D x'.b n) (Int.ofNat (K x * (x.b n + 1) * (x'.b n + 1))) := bnd_mul (bound x n) (x'.b n + 1)
      have f2 : Bnd (y'.a n * D y.b n) (Int.ofNat (K y' * (y'.b n + 1) * (y.b n + 1))) := bnd_mul (bound y' n) (y.b n + 1)
      have g1 : Bnd ((y.a n * D y'.b n - y'.a n * D y.b n) * N ((K x + K y' + 1) * k + (K x + K y')))
          (Int.ofNat ((y.b n + 1) * (y'.b n + 1))) := ey
      have g2 : Bnd ((x.a n * D x'.b n - x'.a n * D x.b n) * N ((K x + K y' + 1) * k + (K x + K y')))
          (Int.ofNat ((x.b n + 1) * (x'.b n + 1))) := ex
      have t := bnd_add (bnd_mul2 f1 g1) (bnd_mul2 f2 g2)
      have w : LE' (Int.ofNat (K x) * D x.b n * D x'.b n * (D y.b n * D y'.b n) +
            Int.ofNat (K y') * D y'.b n * D y.b n * (D x.b n * D x'.b n))
          (D x.b n * D y.b n * (D x'.b n * D y'.b n) * Int.ofNat (K x + K y' + 1)) :=
        ⟨(x.b n + 1) * (y.b n + 1) * (x'.b n + 1) * (y'.b n + 1), (by
          show D x.b n * D y.b n * (D x'.b n * D y'.b n) * (Int.ofNat (K x) + Int.ofNat (K y') + 1) =
            Int.ofNat (K x) * D x.b n * D x'.b n * (D y.b n * D y'.b n) +
              Int.ofNat (K y') * D y'.b n * D y.b n * (D x.b n * D x'.b n) +
              D x.b n * D y.b n * D x'.b n * D y'.b n
          lib_int)⟩
      have t2 := bnd_weaken t w
      apply bnd_cancel (K x + K y')
      rw [D_mul, D_mul]
      refine bnd_eq2 ?_ rfl t2
      show x.a n * D x'.b n * ((y.a n * D y'.b n - y'.a n * D y.b n) * (Int.ofNat (K x + K y' + 1) * N k)) +
          y'.a n * D y.b n * ((x.a n * D x'.b n - x'.a n * D x.b n) * (Int.ofNat (K x + K y' + 1) * N k)) =
        (x.a n * y.a n * (D x'.b n * D y'.b n) - x'.a n * y'.a n * (D x.b n * D y.b n)) * N k *
          Int.ofNat (K x + K y' + 1)
      lib_int⟩

end Bishop2

end Found

#print axioms Found.Bishop2.ofReal
#print axioms Found.Bishop2.add
#print axioms Found.Bishop2.eqv_refl
#print axioms Found.Bishop2.eqv_symm
#print axioms Found.Bishop2.le_cancel
#print axioms Found.Bishop2.eqC_trans
#print axioms Found.Bishop2.add_resp
#print axioms Found.Bishop2.bnd_mul2
#print axioms Found.Bishop2.bnd_div
#print axioms Found.Bishop2.bound
#print axioms Found.Bishop2.mul
#print axioms Found.Bishop2.mul_resp

found/BishopIVT.lean

lean_all/found/BishopIVT.lean

/-
  T118, the approximate intermediate value theorem. found/BishopIVT — for
  Bishop's reals (found/Bishop2), in grid form: comparisons of a real with
  a rational c/(q+1) (with arbitrarily small slack), a step lemma, totality
  of the comparison in the classical point (from regularity), and the
  theorem: if f(0) ≤ 0 ≤ f(1) and f is uniformly continuous on grids (for
  every k a grid on which consecutive values differ by at most 1/(k+1)),
  then for every k some grid point has |f| ≤ 1/(k+1).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Bishop2
import found.ClassicalPoint

namespace Found

namespace BIVT

open Bishop Bishop2 CP

/-- integers are totally ordered -/
theorem int_total (u v : Int) : LE' u v ∨ LE' v u := by
  have h : v = u + (v - u) := by lib_int
  generalize v - u = d at h
  cases d with
  | ofNat t => exact Or.inl ⟨t, h⟩
  | negSucc t => exact Or.inr ⟨t + 1, by rw [h]; show u = u + -Int.ofNat (t + 1) + Int.ofNat (t + 1); lib_int⟩

/-- x ≤ c/(q+1), with slack 1/(j+1) eventually -/
def LeC (x : R) (c : Int) (q : Nat) : Prop :=
  ∀ j, ∃ M, ∀ n, M ≤ n → LE' (x.a n * N q * N j) ((c * N j + N q) * D x.b n)

/-- x ≥ c/(q+1) -/
def GeC (x : R) (c : Int) (q : Nat) : Prop :=
  ∀ j, ∃ M, ∀ n, M ≤ n → LE' ((c * N j - N q) * D x.b n) (x.a n * N q * N j)

/-- |x − y| ≤ 1/(q+1) -/
def DifLe (x y : R) (q : Nat) : Prop :=
  ∀ j, ∃ M, ∀ n, M ≤ n → Bnd ((x.a n * D y.b n - y.a n * D x.b n) * N q * N j) ((N j + N q) * D x.b n * D y.b n)

theorem le_mulI {u v : Int} (h : LE' u v) (w : Int) (k : Nat) (hw : w = Int.ofNat k) : LE' (u * w) (v * w) := by
  rw [hw]; exact le_mulN k h

/-- step: if y ≤ c/Q and |x − y| ≤ 1/Q then x ≤ (c+1)/Q -/
theorem step_le {x y : R} {c : Int} {q : Nat} (hy : LeC y c q) (hd : DifLe x y q) : LeC x (c + 1) q := fun j =>
  match hy (2 * j + 1), hd (2 * j + 1) with
  | ⟨M1, h1⟩, ⟨M2, h2⟩ => ⟨M1 + M2, fun n hn => by
      have a1 := le_mulN (x.b n + 1) (h1 n (Nat.le_trans (Nat.le_add_right M1 M2) hn))
      have a2 := (h2 n (Nat.le_trans (Nat.le_add_left M2 M1) hn)).2
      have s := le_add' a1 a2
      apply le_cancel (2 * y.b n + 1)
      refine le_eq ?_ (le_eq' s ?_)
      · show x.a n * N q * N j * Int.ofNat (2 * y.b n + 1 + 1) =
          y.a n * N q * N (2 * j + 1) * Int.ofNat (x.b n + 1) + (x.a n * D y.b n - y.a n * D x.b n) * N q * N (2 * j + 1)
        show x.a n * N q * N j * (2 * D y.b n) =
          y.a n * N q * (2 * N j) * D x.b n + (x.a n * D y.b n - y.a n * D x.b n) * N q * (2 * N j)
        lib_int
      · show (c * N (2 * j + 1) + N q) * D y.b n * Int.ofNat (x.b n + 1) + (N (2 * j + 1) + N q) * D x.b n * D y.b n =
          ((c + 1) * N j + N q) * D x.b n * Int.ofNat (2 * y.b n + 1 + 1)
        show (c * (2 * N j) + N q) * D y.b n * D x.b n + (2 * N j + N q) * D x.b n * D y.b n =
          ((c + 1) * N j + N q) * D x.b n * (2 * D y.b n)
        lib_int⟩

theorem le_mono {x : R} {c c' : Int} {q : Nat} (h : LeC x c q) (hc : LE' c c') : LeC x c' q := fun j =>
  match h j with
  | ⟨M, hM⟩ => ⟨M, fun n hn => le_trans' (hM n hn)
      (le_mulN (x.b n + 1) (le_add' (le_mulN (j + 1) hc) (le_refl' (N q))))⟩

theorem ge_mono {x : R} {c c' : Int} {q : Nat} (h : GeC x c q) (hc : LE' c' c) : GeC x c' q := fun j =>
  match h j with
  | ⟨M, hM⟩ => ⟨M, fun n hn => le_trans'
      (le_mulN (x.b n + 1) (le_add' (le_mulN (j + 1) hc) (le_refl' (-N q)))) (hM n hn)⟩

/-- least index with a property (classical point) -/
theorem least_nat (K : Classical') (P : Nat → Prop) : (m : Nat) → ∀ i, i ≤ m → P i →
    ∃ i0, i0 ≤ i ∧ P i0 ∧ ∀ i', i' < i0 → ¬ P i'
  | 0, i, hi, hp => ⟨i, Nat.le_refl i, hp, fun _ h' => absurd (Nat.lt_of_lt_of_le h' hi) (Nat.not_lt_zero _)⟩
  | m + 1, i, hi, hp => (K.lem (∃ i', i' < i ∧ P i')).elim
      (fun ⟨i', h', hp'⟩ => match least_nat K P m i' (Nat.le_of_lt_succ (Nat.lt_of_lt_of_le h' hi)) hp' with
        | ⟨i0, h0, p0, l0⟩ => ⟨i0, Nat.le_trans h0 (Nat.le_of_lt h'), p0, l0⟩)
      (fun hn => ⟨i, Nat.le_refl i, hp, fun _ h' p' => hn ⟨_, h', p'⟩⟩)

theorem le_nat {a b : Nat} (h : a ≤ b) : LE' (Int.ofNat a) (Int.ofNat b) :=
  match Nat.le.dest h with
  | ⟨k, e⟩ => ⟨k, by rw [← e]; rfl⟩

theorem le_cancel' {u v : Int} (P : Nat) (hP : 0 < P) (h : LE' (u * Int.ofNat P) (v * Int.ofNat P)) : LE' u v :=
  match P, hP, h with
  | k + 1, _, h => le_cancel k h

theorem pos_prod (a b c d : Nat) : 0 < (a + 1) * (b + 1) * (c + 1) * (d + 1) :=
  Nat.mul_pos (Nat.mul_pos (Nat.mul_pos (Nat.succ_pos a) (Nat.succ_pos b)) (Nat.succ_pos c)) (Nat.succ_pos d)

/-- totality in the classical point: if not x ≥ c/Q then x ≤ c/Q -/
theorem total_le (K : Classical') {x : R} {c : Int} {q : Nat} (h : ¬ GeC x c q) : LeC x c q := by
  have h1 : ∃ j0, ¬ ∃ M, ∀ n, M ≤ n → LE' ((c * N j0 - N q) * D x.b n) (x.a n * N q * N j0) :=
    dne K _ (fun hn => h (fun j => dne K _ (fun hj => hn ⟨j, hj⟩)))
  match h1 with
  | ⟨j0, hj0⟩ =>
    have bad : ∀ M, ∃ n, M ≤ n ∧ LE' (x.a n * N q * N j0) ((c * N j0 - N q) * D x.b n) := fun M =>
      dne K _ (fun hn => hj0 ⟨M, fun n hn' => (int_total _ _).elim id (fun hle => (hn ⟨n, hn', hle⟩).elim)⟩)
    intro j
    refine ⟨2 * x.C * (j0 + 1), fun m hm => ?_⟩
    match bad (2 * x.C * (j0 + 1)) with
    | ⟨n, hn, hb⟩ =>
      have reg := (x.reg m n).2
      have E1 := le_mulN ((x.b m + 1) * (m + 1) * (n + 1)) hb
      have E2 := le_mulN ((q + 1) * (j0 + 1)) reg
      have S := le_add' E1 E2
      -- key: C J0 (Nm + Nn) ≤ Nm Nn
      have Am := le_nat (Nat.le_trans hm (Nat.le_succ m))
      have An := le_nat (Nat.le_trans hn (Nat.le_succ n))
      have k2 := le_add' (le_mulN (m + 1) An) (le_mulN (n + 1) Am)
      have key : LE' (Int.ofNat x.C * N j0 * (N m + N n)) (N m * N n) := le_cancel 1 (le_eq (by
          show Int.ofNat x.C * N j0 * (N m + N n) * (1 + 1) =
            Int.ofNat (2 * x.C * (j0 + 1)) * Int.ofNat (m + 1) + Int.ofNat (2 * x.C * (j0 + 1)) * Int.ofNat (n + 1)
          show Int.ofNat x.C * N j0 * (N m + N n) * 2 = 2 * Int.ofNat x.C * N j0 * N m + 2 * Int.ofNat x.C * N j0 * N n
          lib_int) (le_eq' k2 (by
          show Int.ofNat (n + 1) * Int.ofNat (m + 1) + Int.ofNat (m + 1) * Int.ofNat (n + 1) = N m * N n * (1 + 1)
          show N n * N m + N m * N n = N m * N n * 2
          lib_int)))
      have key2 := le_mulN ((q + 1) * (x.b m + 1) * (x.b n + 1)) key
      have T := le_trans' S (le_eq (by
          show (c * N j0 - N q) * D x.b n * (D x.b m * N m * N n) + Int.ofNat x.C * (N m + N n) * D x.b m * D x.b n * (N q * N j0) =
            (c * N j0 - N q) * D x.b n * (D x.b m * N m * N n) + Int.ofNat x.C * N j0 * (N m + N n) * (N q * D x.b m * D x.b n)
          lib_int) (le_add' (le_refl' _) key2))
      have T2 : LE' (x.a m * N q * Int.ofNat ((x.b n + 1) * (m + 1) * (n + 1) * (j0 + 1)))
          (c * D x.b m * Int.ofNat ((x.b n + 1) * (m + 1) * (n + 1) * (j0 + 1))) := le_eq (by
          show x.a m * N q * (D x.b n * N m * N n * N j0) =
            x.a n * N q * N j0 * (D x.b m * N m * N n) + (x.a m * D x.b n - x.a n * D x.b m) * N m * N n * (N q * N j0)
          lib_int) (le_eq' T (by
          show (c * N j0 - N q) * D x.b n * (D x.b m * N m * N n) + N m * N n * (N q * D x.b m * D x.b n) =
            c * D x.b m * (D x.b n * N m * N n * N j0)
          lib_int))
      have T3 := le_cancel' _ (pos_prod _ _ _ _) T2
      have T4 := le_mulN (j + 1) T3
      exact le_trans' T4 (le_eq (by show c * D x.b m * N j = c * D x.b m * N j + 0; lib_int)
        (le_eq' (le_add' (le_refl' (c * D x.b m * N j)) (⟨(q + 1) * (x.b m + 1), by
            show N q * D x.b m = 0 + Int.ofNat ((q + 1) * (x.b m + 1)); show N q * D x.b m = 0 + N q * D x.b m; lib_int⟩ :
            LE' 0 (N q * D x.b m))) (by lib_int)))

/-- the approximate intermediate value theorem (grid form): F N i is the value at i/(N+1) -/
theorem approx_ivt (K : Classical') (F : Nat → Nat → R)
    (h0 : ∀ N q, LeC (F N 0) 0 q) (h1 : ∀ N q, GeC (F N (N + 1)) 0 q)
    (hc : ∀ k, ∃ N, ∀ i, i ≤ N → DifLe (F N (i + 1)) (F N i) k) (k : Nat) :
    ∃ N i, i ≤ N + 1 ∧ LeC (F N i) 2 (4 * k + 3) ∧ GeC (F N i) (-2) (4 * k + 3) :=
  match hc (4 * k + 3) with
  | ⟨N, hN⟩ =>
    have hP : GeC (F N (N + 1)) (-2) (4 * k + 3) := ge_mono (h1 N _) ⟨2, rfl⟩
    match least_nat K (fun i => GeC (F N i) (-2) (4 * k + 3)) (N + 1) (N + 1) (Nat.le_refl _) hP with
    | ⟨0, _, p0, _⟩ => ⟨N, 0, Nat.zero_le _, le_mono (h0 N _) ⟨2, rfl⟩, p0⟩
    | ⟨i + 1, hi, p0, lmin⟩ =>
      have hle : LeC (F N i) (-2) (4 * k + 3) := total_le K (lmin i (Nat.lt_succ_self i))
      have hst := step_le hle (hN i (Nat.le_of_succ_le_succ hi))
      ⟨N, i + 1, hi, le_mono hst ⟨3, rfl⟩, p0⟩

end BIVT

end Found

#print axioms Found.BIVT.int_total
#print axioms Found.BIVT.step_le
#print axioms Found.BIVT.le_mono
#print axioms Found.BIVT.ge_mono
#print axioms Found.BIVT.least_nat
#print axioms Found.BIVT.total_le
#print axioms Found.BIVT.approx_ivt

found/BishopProb.lean

lean_all/found/BishopProb.lean

/-
  T174, line real-valued probability ↔ Bishop's analysis. found/BishopProb —
  finite probability spaces with weights in Bishop's reals (found/Bishop2):
  the measure of an event is the sum of the weights of its outcomes; up to
  equality of reals, inclusion–exclusion μ(A ∨ B) + μ(A ∧ B) = μ(A) + μ(B)
  and complementation μ(A) + μ(¬A) = μ(Ω) hold, as for the counting
  measure of found/ProbLogic.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Bishop2

namespace Found

namespace BProb

open Bishop Bishop2

def zero : R := ⟨fun _ => 0, fun _ => 0, 0, fun m n => bnd_eq2 (by lib_int)
  (show Int.ofNat (0 * ((m + 1) + (n + 1)) * (0 + 1) * (0 + 1)) = _ from rfl) (bnd_zero _)⟩

/-- equality from an exact identity of cross products -/
theorem eqC_of (x y : R) (h : ∀ n, x.a n * D y.b n - y.a n * D x.b n = 0) : EqC x y := fun _ => ⟨0, fun n _ =>
  bnd_eq2 (by rw [h n]; lib_int) (show Int.ofNat ((x.b n + 1) * (y.b n + 1)) = _ from rfl) (bnd_zero _)⟩

theorem add_comm' (x y : R) : EqC (add x y) (add y x) := eqC_of _ _ (fun n => by
  show (x.a n * D y.b n + y.a n * D x.b n) * D (add y x).b n - (y.a n * D x.b n + x.a n * D y.b n) * D (add x y).b n = 0
  rw [D_add, D_add]
  show (x.a n * D y.b n + y.a n * D x.b n) * (D y.b n * D x.b n) - (y.a n * D x.b n + x.a n * D y.b n) * (D x.b n * D y.b n) = 0
  lib_int)

theorem add_assoc' (x y z : R) : EqC (add (add x y) z) (add x (add y z)) := eqC_of _ _ (fun n => by
  show ((x.a n * D y.b n + y.a n * D x.b n) * D z.b n + z.a n * D (add x y).b n) * D (add x (add y z)).b n -
      (x.a n * D (add y z).b n + (y.a n * D z.b n + z.a n * D y.b n) * D x.b n) * D (add (add x y) z).b n = 0
  rw [D_add x (add y z), D_add (add x y) z, D_add x y, D_add y z]
  lib_int)

theorem zero_add' (x : R) : EqC (add zero x) x := eqC_of _ _ (fun n => by
  show (zero.a n * D x.b n + x.a n * D zero.b n) * D x.b n - x.a n * D (add zero x).b n = 0
  rw [D_add]
  show (0 * D x.b n + x.a n * Int.ofNat (0 + 1)) * D x.b n - x.a n * (Int.ofNat (0 + 1) * D x.b n) = 0
  lib_int)

/-- (p + X) + (q + Y) = (p + q) + (X + Y) -/
theorem rearr (p q X Y : R) : EqC (add (add p X) (add q Y)) (add (add p q) (add X Y)) :=
  eqC_trans (add_assoc' p X (add q Y))
    (eqC_trans (add_resp (eqC_refl p) (eqC_trans (eqC_symm (add_assoc' X q Y))
        (eqC_trans (add_resp (add_comm' X q) (eqC_refl Y)) (add_assoc' q X Y))))
      (eqC_symm (add_assoc' p q (add X Y))))

variable {Ω : Type}

/-- the measure of an event: the sum of the weights of its outcomes in the list -/
def muR (w : Ω → R) (A : Ω → Bool) : List Ω → R
  | [] => zero
  | o :: l => add (cond (A o) (w o) zero) (muR w A l)

theorem incl_excl (w : Ω → R) (A B : Ω → Bool) : (l : List Ω) →
    EqC (add (muR w (fun o => A o || B o) l) (muR w (fun o => A o && B o) l)) (add (muR w A l) (muR w B l))
  | [] => eqC_refl _
  | o :: l => by
    have ih := incl_excl w A B l
    have loc : EqC (add (cond (A o || B o) (w o) zero) (cond (A o && B o) (w o) zero))
        (add (cond (A o) (w o) zero) (cond (B o) (w o) zero)) := by
      cases A o <;> cases B o
      · exact eqC_refl _
      · exact add_comm' _ _
      · exact eqC_refl _
      · exact eqC_refl _
    exact eqC_trans (rearr _ _ _ _) (eqC_trans (add_resp loc ih) (eqC_symm (rearr _ _ _ _)))

theorem complement (w : Ω → R) (A : Ω → Bool) : (l : List Ω) →
    EqC (add (muR w A l) (muR w (fun o => !A o) l)) (muR w (fun _ => true) l)
  | [] => zero_add' _
  | o :: l => by
    have ih := complement w A l
    have loc : EqC (add (cond (A o) (w o) zero) (cond (!A o) (w o) zero)) (w o) := by
      cases A o
      · exact zero_add' _
      · exact eqC_trans (add_comm' _ _) (zero_add' _)
    exact eqC_trans (rearr _ _ _ _) (add_resp loc ih)

end BProb

end Found

#print axioms Found.BProb.zero
#print axioms Found.BProb.add_assoc'
#print axioms Found.BProb.rearr
#print axioms Found.BProb.incl_excl
#print axioms Found.BProb.complement

found/Brouwer.lean

lean_all/found/Brouwer.lean

/-
  T197. found/Brouwer — Brouwer's intuitionism as a point: choice sequences
  (Baire space ℕ → ℕ, the core's function space on the NNO) with the weak
  continuity condition WC-N as a parameter condition.
  * WC-N: if ∀α ∃n P(α,n) then for each α there are m, n such that every β
    agreeing with α below m has P(β,n).
  * WC-N refutes excluded middle for "α is identically 0" (Π⁰₁ LEM):
    the clash Brouwer ⟂ classical, as a condition clash.
  Fan theorem / bar induction are further conditions (cited); Markov's
  principle (T198) is the Russian point that Brouwer rejects.

  Lean 4 core only, hand-written proofs, no axioms (WC-N is a hypothesis).
-/

namespace Found

namespace Brouwer

def Agree (α β : Nat → Nat) (m : Nat) : Prop := ∀ k, k < m → α k = β k

def WCN : Prop :=
  ∀ P : (Nat → Nat) → Nat → Prop, (∀ α, ∃ n, P α n) →
    ∀ α, ∃ m n, ∀ β, Agree α β m → P β n

def zero : Nat → Nat := fun _ => 0

/-- β: zero below m, one from m on -/
def bump : Nat → Nat → Nat
  | 0, _ => 1
  | _ + 1, 0 => 0
  | m + 1, k + 1 => bump m k

theorem bump_lt : (m k : Nat) → k < m → bump m k = 0
  | 0, _, h => (Nat.not_lt_zero _ h).elim
  | _ + 1, 0, _ => rfl
  | m + 1, k + 1, h => bump_lt m k (Nat.lt_of_succ_lt_succ h)

theorem bump_self : (m : Nat) → bump m m = 1
  | 0 => rfl
  | m + 1 => bump_self m

theorem bump_agree (m : Nat) : Agree zero (bump m) m := fun k h => (bump_lt m k h).symm

theorem bump_not_zero (m : Nat) : ¬ ∀ k, bump m k = 0 := fun h =>
  Nat.noConfusion ((bump_self m).symm.trans (h m))

/-- WC-N refutes ∀α (α ≡ 0 ∨ ¬ α ≡ 0) -/
theorem wcn_not_lem (hw : WCN) : ¬ ∀ α : Nat → Nat, (∀ k, α k = 0) ∨ ¬ (∀ k, α k = 0) := by
  intro lem
  let P : (Nat → Nat) → Nat → Prop := fun α n =>
    (n = 0 ∧ ∀ k, α k = 0) ∨ (n = 1 ∧ ¬ ∀ k, α k = 0)
  have hP : ∀ α, ∃ n, P α n := fun α =>
    (lem α).elim (fun h => ⟨0, Or.inl ⟨rfl, h⟩⟩) (fun h => ⟨1, Or.inr ⟨rfl, h⟩⟩)
  match hw P hP zero with
  | ⟨m, n, h⟩ =>
    have hz : P zero n := h zero (fun _ _ => rfl)
    have hb : P (bump m) n := h (bump m) (bump_agree m)
    exact hz.elim
      (fun ⟨e0, _⟩ => hb.elim (fun ⟨_, hk⟩ => bump_not_zero m hk)
        (fun ⟨e1, _⟩ => Nat.noConfusion (e0.symm.trans e1)))
      (fun ⟨_, nz⟩ => nz (fun _ => rfl))

end Brouwer

end Found

#print axioms Found.Brouwer.bump_lt
#print axioms Found.Brouwer.bump_agree
#print axioms Found.Brouwer.bump_not_zero
#print axioms Found.Brouwer.wcn_not_lem

found/Buchi1.lean

lean_all/found/Buchi1.lean

/-
  Line automata ↔ MSO (Büchi–Elgot–Trakhtenbrot), part 1. found/Buchi1 —
  finite automata with Boolean start, transition and acceptance, and a list
  of states closed under the transitions (Good: finitely many reachable
  states); closure of the recognised languages under intersection
  (product), projection of a Boolean track, and complement through the
  subset construction on Boolean vectors over the state list.

  Lean 4 core only, hand-written proofs, no axioms.
-/

namespace Found

namespace Bu

/-! ## Boolean and list utilities (proved here: the core list lemmas use propext) -/

theorem or_t {a b : Bool} (h : (a || b) = true) : a = true ∨ b = true := by
  cases a with
  | true => exact Or.inl rfl
  | false => exact Or.inr h

theorem and_t {a b : Bool} (h : (a && b) = true) : a = true ∧ b = true := by
  cases a with
  | true => exact ⟨rfl, h⟩
  | false => exact Bool.noConfusion h

theorem or_l {a : Bool} (b : Bool) (h : a = true) : (a || b) = true := by rw [h]; rfl
theorem or_r (a : Bool) {b : Bool} (h : b = true) : (a || b) = true := by
  cases a with
  | true => rfl
  | false => exact h
theorem and_i {a b : Bool} (ha : a = true) (hb : b = true) : (a && b) = true := by rw [ha, hb]; rfl

theorem not_t {a : Bool} : (!a) = true ↔ ¬ a = true := by
  cases a with
  | true => exact ⟨fun h => Bool.noConfusion h, fun h => absurd rfl h⟩
  | false => exact ⟨fun _ h => Bool.noConfusion h, fun _ => rfl⟩

/-- ∃ i < n, p i, as a Boolean -/
def anyI (p : Nat → Bool) : Nat → Bool
  | 0 => false
  | n + 1 => anyI p n || p n

theorem anyI_t (p : Nat → Bool) : (n : Nat) → anyI p n = true → ∃ i, i < n ∧ p i = true
  | 0, h => Bool.noConfusion h
  | n + 1, h => (or_t h).elim (fun h => match anyI_t p n h with
      | ⟨i, hi, hp⟩ => ⟨i, Nat.lt_trans hi (Nat.lt_succ_self n), hp⟩) (fun h => ⟨n, Nat.lt_succ_self n, h⟩)

theorem t_anyI (p : Nat → Bool) : (n i : Nat) → i < n → p i = true → anyI p n = true
  | 0, _, h, _ => absurd h (Nat.not_lt_zero _)
  | n + 1, i, h, hp => (TT'.le_cases (Nat.le_of_lt_succ h)).elim
      (fun e => or_r _ (e ▸ hp)) (fun hl => or_l _ (t_anyI p n i hl hp))
where
  TT'.le_cases {a b : Nat} (h : a ≤ b) : a = b ∨ a < b := by
    induction h with
    | refl => exact Or.inl rfl
    | step h _ => exact Or.inr (Nat.lt_succ_of_le h)

theorem le_cases {a b : Nat} (h : a ≤ b) : a = b ∨ a < b := t_anyI.TT'.le_cases h

def nthO {Q : Type} : List Q → Nat → Option Q
  | [], _ => none
  | q :: _, 0 => some q
  | _ :: l, i + 1 => nthO l i

def nthB : List Bool → Nat → Bool
  | [], _ => false
  | b :: _, 0 => b
  | _ :: l, i + 1 => nthB l i

def len {Q : Type} : List Q → Nat
  | [] => 0
  | _ :: l => len l + 1

inductive Mem {Q : Type} (q : Q) : List Q → Prop where
  | head (l : List Q) : Mem q (q :: l)
  | tail (p : Q) {l : List Q} : Mem q l → Mem q (p :: l)

theorem mem_idx {Q : Type} {q : Q} : {l : List Q} → Mem q l → ∃ i, i < len l ∧ nthO l i = some q
  | _, .head l => ⟨0, Nat.succ_pos _, rfl⟩
  | _, .tail _ m => match mem_idx m with
    | ⟨i, hi, e⟩ => ⟨i + 1, Nat.succ_lt_succ hi, e⟩

theorem idx_mem {Q : Type} {q : Q} : (l : List Q) → (i : Nat) → nthO l i = some q → Mem q l
  | [], _, h => nomatch h
  | _ :: l, 0, h => (Option.some.inj h) ▸ .head l
  | p :: l, i + 1, h => .tail p (idx_mem l i h)

/-- the vector [f i, f (i+1), …] of length n -/
def vecFrom (f : Nat → Bool) (i : Nat) : Nat → List Bool
  | 0 => []
  | n + 1 => f i :: vecFrom f (i + 1) n

theorem nth_vecFrom (f : Nat → Bool) : (n i j : Nat) → j < n → nthB (vecFrom f i n) j = f (i + j)
  | 0, _, _, h => absurd h (Nat.not_lt_zero _)
  | n + 1, i, 0, _ => rfl
  | n + 1, i, j + 1, h => by
    show nthB (vecFrom f (i + 1) n) j = f (i + (j + 1))
    rw [nth_vecFrom f n (i + 1) j (Nat.lt_of_succ_lt_succ h), Nat.add_right_comm, Nat.add_assoc]

theorem len_vecFrom (f : Nat → Bool) : (n i : Nat) → len (vecFrom f i n) = n
  | 0, _ => rfl
  | n + 1, i => congrArg (· + 1) (len_vecFrom f n (i + 1))

/-- all Boolean vectors of length n -/
def mapCons (b : Bool) : List (List Bool) → List (List Bool)
  | [] => []
  | l :: ls => (b :: l) :: mapCons b ls

def app2 : List (List Bool) → List (List Bool) → List (List Bool)
  | [], r => r
  | l :: ls, r => l :: app2 ls r

def allVec : Nat → List (List Bool)
  | 0 => [[]]
  | n + 1 => app2 (mapCons false (allVec n)) (mapCons true (allVec n))

theorem mem_mapCons (b : Bool) {l : List Bool} : {ls : List (List Bool)} → Mem l ls → Mem (b :: l) (mapCons b ls)
  | _, .head _ => .head _
  | _, .tail _ m => .tail _ (mem_mapCons b m)

theorem mem_app2_l {l : List Bool} : {a : List (List Bool)} → (b : List (List Bool)) → Mem l a → Mem l (app2 a b)
  | _, _, .head _ => .head _
  | _, b, .tail _ m => .tail _ (mem_app2_l b m)

theorem mem_app2_r {l : List Bool} : (a : List (List Bool)) → {b : List (List Bool)} → Mem l b → Mem l (app2 a b)
  | [], _, m => m
  | _ :: a, _, m => .tail _ (mem_app2_r a m)

theorem mem_allVec : (l : List Bool) → Mem l (allVec (len l))
  | [] => .head _
  | false :: l => mem_app2_l _ (mem_mapCons false (mem_allVec l))
  | true :: l => mem_app2_r _ (mem_mapCons true (mem_allVec l))

def beqL : List Bool → List Bool → Bool
  | [], [] => true
  | [], _ :: _ => false
  | _ :: _, [] => false
  | a :: l, b :: r => (a == b) && beqL l r

theorem beqL_t : (l r : List Bool) → beqL l r = true → l = r
  | [], [], _ => rfl
  | [], _ :: _, h => Bool.noConfusion h
  | _ :: _, [], h => Bool.noConfusion h
  | a :: l, b :: r, h => by
    have h' := and_t h
    have e : a = b := by cases a <;> cases b <;> first | rfl | exact Bool.noConfusion h'.1
    rw [e, beqL_t l r h'.2]

theorem beqL_self : (l : List Bool) → beqL l l = true
  | [] => rfl
  | a :: l => and_i (by cases a <;> rfl) (beqL_self l)

/-! ## finite automata -/

structure FA (α : Type) where
  Q : Type
  enum : List Q
  start : Q → Bool
  δ : Q → α → Q → Bool
  acc : Q → Bool

variable {α β : Type}

inductive Run (A : FA α) : A.Q → List α → A.Q → Prop where
  | nil (q : A.Q) : Run A q [] q
  | cons {q q' q'' : A.Q} {a : α} {w : List α} : A.δ q a q' = true → Run A q' w q'' → Run A q (a :: w) q''

def accepts (A : FA α) (w : List α) : Prop := ∃ q q', A.start q = true ∧ Run A q w q' ∧ A.acc q' = true

/-- finitely many reachable states: the state list contains the start states and is closed -/
structure Good (A : FA α) : Prop where
  st : ∀ q, A.start q = true → Mem q A.enum
  cl : ∀ q a q', Mem q A.enum → A.δ q a q' = true → Mem q' A.enum

/-! ## intersection -/

def prod (A B : FA α) : FA α where
  Q := A.Q × B.Q
  enum := A.enum.flatMap (fun p => B.enum.map (fun q => (p, q)))
  start p := A.start p.1 && B.start p.2
  δ p a p' := A.δ p.1 a p'.1 && B.δ p.2 a p'.2
  acc p := A.acc p.1 && B.acc p.2

theorem run_prod {A B : FA α} : {p : (prod A B).Q} → {w : List α} → {p' : (prod A B).Q} →
    Run (prod A B) p w p' → Run A p.1 w p'.1 ∧ Run B p.2 w p'.2
  | _, _, _, .nil _ => ⟨.nil _, .nil _⟩
  | _, _, _, .cons h r => match run_prod r with
    | ⟨r1, r2⟩ => ⟨.cons (and_t h).1 r1, .cons (and_t h).2 r2⟩

theorem prod_run {A B : FA α} : {p : A.Q} → {q : B.Q} → {w : List α} → {p' : A.Q} → {q' : B.Q} →
    Run A p w p' → Run B q w q' → Run (prod A B) (p, q) w (p', q')
  | _, _, _, _, _, .nil _, .nil _ => .nil _
  | _, _, _, _, _, .cons h1 r1, .cons h2 r2 => .cons (and_i h1 h2) (prod_run r1 r2)

theorem accepts_prod (A B : FA α) (w : List α) : accepts (prod A B) w ↔ accepts A w ∧ accepts B w :=
  ⟨fun ⟨_, _, hs, r, ha⟩ => match run_prod r with
    | ⟨r1, r2⟩ => ⟨⟨_, _, (and_t hs).1, r1, (and_t ha).1⟩, ⟨_, _, (and_t hs).2, r2, (and_t ha).2⟩⟩,
   fun ⟨⟨p, p', hs1, r1, ha1⟩, ⟨q, q', hs2, r2, ha2⟩⟩ =>
    ⟨(p, q), (p', q'), and_i hs1 hs2, prod_run r1 r2, and_i ha1 ha2⟩⟩

theorem mem_flatMap_map {A B : Type} {p : A} {q : B} : {la : List A} → (lb : List B) → Mem p la → Mem q lb →
    Mem (p, q) (la.flatMap (fun p => lb.map (fun q => (p, q))))
  | _, lb, .head la, mq => by
    show Mem (p, q) (lb.map (fun q => (p, q)) ++ la.flatMap _)
    exact memApp_l (memMap mq)
  | _, lb, .tail x m, mq => by
    show Mem (p, q) (lb.map (fun q => (x, q)) ++ _)
    exact memApp_r _ (mem_flatMap_map lb m mq)
where
  memMap {C D : Type} {f : C → D} {c : C} : {l : List C} → Mem c l → Mem (f c) (l.map f)
    | _, .head _ => .head _
    | _, .tail _ m => .tail _ (memMap m)
  memApp_l {C : Type} {c : C} : {a : List C} → {b : List C} → Mem c a → Mem c (a ++ b)
    | _, _, .head _ => .head _
    | _, _, .tail _ m => .tail _ (memApp_l m)
  memApp_r {C : Type} {c : C} : (a : List C) → {b : List C} → Mem c b → Mem c (a ++ b)
    | [], _, m => m
    | _ :: a, _, m => .tail _ (memApp_r a m)

theorem good_prod {A B : FA α} (ga : Good A) (gb : Good B) : Good (prod A B) :=
  ⟨fun p h => mem_flatMap_map _ (ga.st _ (and_t h).1) (gb.st _ (and_t h).2),
   fun p _ _ m h => by
    have hm : Mem p.1 A.enum ∧ Mem p.2 B.enum := memPair m
    exact mem_flatMap_map _ (ga.cl _ _ _ hm.1 (and_t h).1) (gb.cl _ _ _ hm.2 (and_t h).2)⟩
where
  memPair {p : A.Q × B.Q} : {la : List A.Q} → Mem p (la.flatMap (fun x => B.enum.map (fun q => (x, q)))) →
      Mem p.1 la ∧ Mem p.2 B.enum := fun {la} m => by
    induction la with
    | nil => exact nomatch m
    | cons x la ih =>
      have split : ∀ {l1 l2 : List (A.Q × B.Q)}, Mem p (l1 ++ l2) → Mem p l1 ∨ Mem p l2 := by
        intro l1
        induction l1 with
        | nil => intro l2 m; exact Or.inr m
        | cons y l1 ih1 =>
          intro l2 m
          cases m with
          | head => exact Or.inl (.head _)
          | tail _ m => exact (ih1 m).elim (fun h => Or.inl (.tail _ h)) Or.inr
      have inMap : ∀ {l : List B.Q}, Mem p (l.map (fun q => (x, q))) → p.1 = x ∧ Mem p.2 l := by
        intro l
        induction l with
        | nil => intro m; exact nomatch m
        | cons q l ih2 =>
          intro m
          cases m with
          | head => exact ⟨rfl, .head _⟩
          | tail _ m => exact ⟨(ih2 m).1, .tail _ (ih2 m).2⟩
      cases split m with
      | inl h => exact ⟨(inMap h).1 ▸ .head _, (inMap h).2⟩
      | inr h => exact ⟨.tail _ (ih h).1, (ih h).2⟩

/-! ## projection of the first Boolean track -/

def proj (A : FA (α × List Bool)) : FA (α × List Bool) where
  Q := A.Q
  enum := A.enum
  start := A.start
  δ q x q' := A.δ q (x.1, false :: x.2) q' || A.δ q (x.1, true :: x.2) q'
  acc := A.acc

theorem good_proj {A : FA (α × List Bool)} (g : Good A) : Good (proj A) :=
  ⟨g.st, fun _ _ _ m h => (or_t h).elim (g.cl _ _ _ m) (g.cl _ _ _ m)⟩

/-- add a track: each letter gets one more bit, chosen by the track -/
def addTrack (bs : List Bool) : List (α × List Bool) → List (α × List Bool)
  | [] => []
  | x :: w => match bs with
    | [] => (x.1, false :: x.2) :: addTrack [] w
    | b :: bs => (x.1, b :: x.2) :: addTrack bs w

theorem proj_run {A : FA (α × List Bool)} : {q : A.Q} → {w : List (α × List Bool)} → {q' : A.Q} →
    Run (proj A) q w q' → ∃ bs, Run A q (addTrack bs w) q'
  | _, _, _, .nil _ => ⟨[], .nil _⟩
  | _, _, _, .cons h r => match proj_run r with
    | ⟨bs, r'⟩ => (or_t h).elim (fun h => ⟨false :: bs, .cons h r'⟩) (fun h => ⟨true :: bs, .cons h r'⟩)

theorem run_proj {A : FA (α × List Bool)} : (bs : List Bool) → {q : A.Q} → {w : List (α × List Bool)} →
    {q' : A.Q} → Run A q (addTrack bs w) q' → Run (proj A) q w q'
  | _, _, [], _, r => by cases r; exact .nil _
  | [], _, x :: w, _, r => by
    cases r with
    | cons h r => exact .cons (or_l _ h) (run_proj [] r)
  | b :: bs, _, x :: w, _, r => by
    cases r with
    | cons h r =>
      cases b with
      | false => exact .cons (or_l _ h) (run_proj bs r)
      | true => exact .cons (or_r _ h) (run_proj bs r)

theorem accepts_proj (A : FA (α × List Bool)) (w : List (α × List Bool)) :
    accepts (proj A) w ↔ ∃ bs, accepts A (addTrack bs w) :=
  ⟨fun ⟨q, q', hs, r, ha⟩ => match proj_run r with
    | ⟨bs, r'⟩ => ⟨bs, q, q', hs, r', ha⟩,
   fun ⟨bs, q, q', hs, r, ha⟩ => ⟨q, q', hs, run_proj bs r, ha⟩⟩

/-! ## subset construction on Boolean vectors, complement -/

section
variable (A : FA α)

def stAt (i : Nat) : Bool := match nthO A.enum i with
  | some q => A.start q
  | none => false

def accAt (i : Nat) : Bool := match nthO A.enum i with
  | some q => A.acc q
  | none => false

def δAt (a : α) (i j : Nat) : Bool := match nthO A.enum i, nthO A.enum j with
  | some p, some q => A.δ p a q
  | some _, none => false
  | none, _ => false

def dstep (S : List Bool) (a : α) : List Bool :=
  vecFrom (fun j => anyI (fun i => nthB S i && δAt A a i j) (len A.enum)) 0 (len A.enum)

def S0 : List Bool := vecFrom (stAt A) 0 (len A.enum)

def dacc (S : List Bool) : Bool := anyI (fun i => nthB S i && accAt A i) (len A.enum)

/-- the subset automaton, deterministic; with `flip` the complement -/
def det (flip : Bool) : FA α where
  Q := List Bool
  enum := allVec (len A.enum)
  start S := beqL S (S0 A)
  δ S a T := beqL T (dstep A S a)
  acc S := cond flip (!dacc A S) (dacc A S)

end

/-- the meaning of a vector: the states it marks -/
def mean (A : FA α) (S : List Bool) (q : A.Q) : Prop := ∃ i, i < len A.enum ∧ nthO A.enum i = some q ∧ nthB S i = true

theorem nthO_lt {Q : Type} : (l : List Q) → (i : Nat) → (q : Q) → nthO l i = some q → i < len l
  | [], _, _, h => nomatch h
  | _ :: _, 0, _, _ => Nat.succ_pos _
  | _ :: l, i + 1, q, h => Nat.succ_lt_succ (nthO_lt l i q h)

theorem mean_step (A : FA α) (g : Good A) (S : List Bool) (a : α) (q' : A.Q) :
    mean A (dstep A S a) q' ↔ ∃ q, mean A S q ∧ A.δ q a q' = true := by
  constructor
  · intro ⟨j, hj, ej, hb⟩
    unfold dstep at hb
    rw [nth_vecFrom _ _ 0 j hj, Nat.zero_add] at hb
    match anyI_t _ _ hb with
    | ⟨i, hi, hp⟩ =>
      have hp' := and_t hp
      have hd : δAt A a i j = true := hp'.2
      unfold δAt at hd
      match ei : nthO A.enum i with
      | some p => rw [ei, ej] at hd; exact ⟨p, ⟨i, hi, ei, hp'.1⟩, hd⟩
      | none => rw [ei] at hd; exact Bool.noConfusion hd
  · intro ⟨q, ⟨i, hi, ei, hb⟩, hd⟩
    match mem_idx (g.cl q a q' (idx_mem _ i ei) hd) with
    | ⟨j, hj, ej⟩ =>
      refine ⟨j, hj, ej, ?_⟩
      unfold dstep
      rw [nth_vecFrom _ _ 0 j hj, Nat.zero_add]
      exact t_anyI _ _ i hi (and_i hb (by unfold δAt; rw [ei, ej]; exact hd))

theorem mean_S0 (A : FA α) (g : Good A) (q : A.Q) : mean A (S0 A) q ↔ A.start q = true := by
  constructor
  · intro ⟨i, hi, ei, hb⟩
    unfold S0 at hb
    rw [nth_vecFrom _ _ 0 i hi, Nat.zero_add] at hb
    unfold stAt at hb
    rw [ei] at hb
    exact hb
  · intro h
    match mem_idx (g.st q h) with
    | ⟨i, hi, ei⟩ => exact ⟨i, hi, ei, by unfold S0; rw [nth_vecFrom _ _ 0 i hi, Nat.zero_add]; unfold stAt; rw [ei]; exact h⟩

theorem mean_acc (A : FA α) (S : List Bool) : dacc A S = true ↔ ∃ q, mean A S q ∧ A.acc q = true := by
  constructor
  · intro h
    match anyI_t _ _ h with
    | ⟨i, hi, hp⟩ =>
      have hp' := and_t hp
      have ha : accAt A i = true := hp'.2
      unfold accAt at ha
      revert ha
      match ei : nthO A.enum i with
      | some q => intro ha; exact ⟨q, ⟨i, hi, ei, hp'.1⟩, ha⟩
      | none => intro ha; exact Bool.noConfusion ha
  · intro ⟨q, ⟨i, hi, ei, hb⟩, ha⟩
    exact t_anyI _ _ i hi (and_i hb (by unfold accAt; rw [ei]; exact ha))

/-- the deterministic run -/
def drun (A : FA α) (S : List Bool) : List α → List Bool
  | [] => S
  | a :: w => drun A (dstep A S a) w

theorem det_run (A : FA α) (fl : Bool) : {S : List Bool} → {w : List α} → {T : List Bool} →
    Run (det A fl) S w T → T = drun A S w
  | _, _, _, .nil _ => rfl
  | _, _, _, .cons h r => (det_run A fl r).trans (congrArg (fun S => drun A S _) (beqL_t _ _ h))

theorem run_det (A : FA α) (fl : Bool) : (S : List Bool) → (w : List α) → Run (det A fl) S w (drun A S w)
  | S, [] => Run.nil (A := det A fl) S
  | S, a :: w => .cons (beqL_self (dstep A S a)) (run_det A fl _ w)

theorem mean_drun (A : FA α) (g : Good A) : (w : List α) → (S : List Bool) → (q' : A.Q) →
    mean A (drun A S w) q' ↔ ∃ q, mean A S q ∧ Run A q w q'
  | [], S, q' => ⟨fun h => ⟨q', h, .nil _⟩, fun ⟨_, h, r⟩ => by cases r; exact h⟩
  | a :: w, S, q' => by
    show mean A (drun A (dstep A S a) w) q' ↔ _
    refine (mean_drun A g w (dstep A S a) q').trans ?_
    constructor
    · intro ⟨q1, h1, r⟩
      match (mean_step A g S a q1).1 h1 with
      | ⟨q, hq, hd⟩ => exact ⟨q, hq, .cons hd r⟩
    · intro ⟨q, hq, r⟩
      cases r with
      | cons hd r => exact ⟨_, (mean_step A g S a _).2 ⟨q, hq, hd⟩, r⟩

theorem det_accepts (A : FA α) (g : Good A) (w : List α) :
    dacc A (drun A (S0 A) w) = true ↔ accepts A w := by
  refine (mean_acc A _).trans ?_
  constructor
  · intro ⟨q', hm, ha⟩
    match (mean_drun A g w (S0 A) q').1 hm with
    | ⟨q, hq, r⟩ => exact ⟨q, q', (mean_S0 A g q).1 hq, r, ha⟩
  · intro ⟨q, q', hs, r, ha⟩
    exact ⟨q', (mean_drun A g w (S0 A) q').2 ⟨q, (mean_S0 A g q).2 hs, r⟩, ha⟩

theorem accepts_det (A : FA α) (fl : Bool) (w : List α) :
    accepts (det A fl) w ↔ (det A fl).acc (drun A (S0 A) w) = true :=
  ⟨fun ⟨S, T, hs, r, ha⟩ => by
    have e1 : S = S0 A := beqL_t _ _ hs
    rw [e1] at r
    rw [det_run A fl r] at ha
    exact ha,
   fun h => ⟨S0 A, _, beqL_self _, run_det A fl _ w, h⟩⟩

def compl (A : FA α) : FA α := det A true

theorem accepts_compl (A : FA α) (g : Good A) (w : List α) : accepts (compl A) w ↔ ¬ accepts A w := by
  unfold compl
  refine (accepts_det A true w).trans (not_t.trans ⟨fun h a => h ((det_accepts A g w).2 a), fun h a => h ((det_accepts A g w).1 a)⟩)

theorem len_dstep (A : FA α) (S : List Bool) (a : α) : len (dstep A S a) = len A.enum := by
  unfold dstep
  exact len_vecFrom _ _ _

theorem good_det (A : FA α) (fl : Bool) : Good (det A fl) :=
  ⟨fun S h => by
    have e : S = S0 A := beqL_t _ _ h
    rw [e]
    have := mem_allVec (S0 A)
    unfold S0 at this ⊢
    rw [len_vecFrom] at this
    exact this,
   fun S a T _ h => by
    have e : T = dstep A S a := beqL_t _ _ h
    rw [e]
    show Mem (dstep A S a) (allVec (len A.enum))
    rw [← len_dstep A S a]
    exact mem_allVec _⟩

end Bu

end Found

#print axioms Found.Bu.accepts_prod
#print axioms Found.Bu.good_prod
#print axioms Found.Bu.accepts_proj
#print axioms Found.Bu.good_proj
#print axioms Found.Bu.mean_step
#print axioms Found.Bu.det_accepts
#print axioms Found.Bu.accepts_compl
#print axioms Found.Bu.good_det
#print axioms Found.Bu.nth_vecFrom
#print axioms Found.Bu.anyI_t
#print axioms Found.Bu.t_anyI
#print axioms Found.Bu.mem_idx
#print axioms Found.Bu.idx_mem
#print axioms Found.Bu.mem_allVec
#print axioms Found.Bu.len_vecFrom
#print axioms Found.Bu.len_dstep
#print axioms Found.Bu.mean_S0
#print axioms Found.Bu.mean_acc
#print axioms Found.Bu.le_cases
#print axioms Found.Bu.mean_drun

found/Buchi2.lean

lean_all/found/Buchi2.lean

/-
  Line automata ↔ MSO, part 2. found/Buchi2 — monadic second-order logic of
  words with set variables only (atoms X ⊆ Y, Sing X, X < Y, every position
  of X has a letter in p; ¬, ∧, ∃X; de Bruijn indices), words with k
  Boolean tracks for the free variables, automata for the atoms, and the
  theorem MSO → automata: every formula with free variables below k is
  recognised on the tracked words by a finite automaton (mso_to_fa).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Buchi1

namespace Found

namespace Bu

variable {α : Type}

inductive Fm (α : Type) where
  | sub : Nat → Nat → Fm α
  | sing : Nat → Fm α
  | lt : Nat → Nat → Fm α
  | lab : (α → Bool) → Nat → Fm α
  | neg : Fm α → Fm α
  | and : Fm α → Fm α → Fm α
  | ex : Fm α → Fm α

abbrev Val := Nat → Nat → Bool

def cons (S : Nat → Bool) (ν : Val) : Val
  | 0 => S
  | j + 1 => ν j

def sat : List α → Val → Fm α → Prop
  | w, ν, .sub X Y => ∀ i, i < len w → ν X i = true → ν Y i = true
  | w, ν, .sing X => ∃ i, i < len w ∧ ν X i = true ∧ ∀ j, j < len w → ν X j = true → j = i
  | w, ν, .lt X Y => ∀ i j, i < len w → j < len w → ν X i = true → ν Y j = true → i < j
  | w, ν, .lab p X => ∀ i a, nthO w i = some a → ν X i = true → p a = true
  | w, ν, .neg φ => ¬ sat w ν φ
  | w, ν, .and φ ψ => sat w ν φ ∧ sat w ν ψ
  | w, ν, .ex φ => ∃ S, sat w (cons S ν) φ

/-- the free variables are below k -/
def Bnd : Fm α → Nat → Prop
  | .sub X Y, k => X < k ∧ Y < k
  | .sing X, k => X < k
  | .lt X Y, k => X < k ∧ Y < k
  | .lab _ X, k => X < k
  | .neg φ, k => Bnd φ k
  | .and φ ψ, k => Bnd φ k ∧ Bnd ψ k
  | .ex φ, k => Bnd φ (k + 1)

def shv (ν : Val) : Val := fun j i => ν j (i + 1)

/-- the word with k tracks -/
def extW : List α → Val → Nat → List (α × List Bool)
  | [], _, _ => []
  | a :: w, ν, k => (a, vecFrom (fun j => ν j 0) 0 k) :: extW w (shv ν) k

theorem vecFrom_congr {f g : Nat → Bool} : (n i : Nat) → (∀ j, i ≤ j → j < i + n → f j = g j) →
    vecFrom f i n = vecFrom g i n
  | 0, _, _ => rfl
  | n + 1, i, h => by
    show f i :: vecFrom f (i + 1) n = g i :: vecFrom g (i + 1) n
    rw [h i (Nat.le_refl i) (Nat.lt_add_of_pos_right (Nat.succ_pos n)),
      vecFrom_congr n (i + 1) (fun j h1 h2 => h j (Nat.le_trans (Nat.le_succ i) h1)
        (by rw [Nat.add_assoc, Nat.add_comm 1 n] at h2; exact h2))]

theorem extW_congr : (w : List α) → (ν μ : Val) → (k : Nat) → (∀ j, j < k → ∀ i, ν j i = μ j i) →
    extW w ν k = extW w μ k
  | [], _, _, _, _ => rfl
  | a :: w, ν, μ, k, h => by
    show (a, vecFrom _ 0 k) :: extW w (shv ν) k = (a, vecFrom _ 0 k) :: extW w (shv μ) k
    rw [vecFrom_congr k 0 (fun j _ hj => h j (by rw [Nat.zero_add] at hj; exact hj) 0),
      extW_congr w (shv ν) (shv μ) k (fun j hj i => h j hj (i + 1))]

theorem vecFrom_shift (f : Nat → Bool) : (k i : Nat) → vecFrom f (i + 1) k = vecFrom (fun j => f (j + 1)) i k
  | 0, _ => rfl
  | k + 1, i => by
    show f (i + 1) :: vecFrom f (i + 1 + 1) k = f (i + 1) :: vecFrom (fun j => f (j + 1)) (i + 1) k
    rw [vecFrom_shift f k (i + 1)]

/-- adding the track of S in front -/
theorem addTrack_extW : (w : List α) → (S : Nat → Bool) → (ν : Val) → (k : Nat) →
    addTrack (vecFrom S 0 (len w)) (extW w ν k) = extW w (cons S ν) (k + 1)
  | [], _, _, _ => rfl
  | a :: w, S, ν, k => by
    show (a, S 0 :: vecFrom (fun j => ν j 0) 0 k) :: addTrack (vecFrom S 1 (len w)) (extW w (shv ν) k) =
      (a, vecFrom (fun j => cons S ν j 0) 0 (k + 1)) :: extW w (shv (cons S ν)) (k + 1)
    have e1 : vecFrom (fun j => cons S ν j 0) 0 (k + 1) = S 0 :: vecFrom (fun j => ν j 0) 0 k := by
      show S 0 :: vecFrom (fun j => cons S ν j 0) 1 k = _
      rw [vecFrom_shift]; rfl
    rw [e1, vecFrom_shift, addTrack_extW w (fun j => S (j + 1)) (shv ν) k]
    exact congrArg _ (extW_congr w _ _ (k + 1) (fun j _ i => by cases j <;> rfl))

/-- and every track list of the right length comes from a set -/
theorem addTrack_any : (w : List α) → (bs : List Bool) → (ν : Val) → (k : Nat) →
    addTrack bs (extW w ν k) = extW w (cons (nthB bs) ν) (k + 1)
  | [], _, _, _ => rfl
  | a :: w, [], ν, k => by
    show (a, false :: vecFrom (fun j => ν j 0) 0 k) :: addTrack [] (extW w (shv ν) k) = _
    rw [addTrack_any w [] (shv ν) k]
    show _ = (a, vecFrom (fun j => cons (nthB []) ν j 0) 0 (k + 1)) :: extW w (shv (cons (nthB []) ν)) (k + 1)
    have e1 : vecFrom (fun j => cons (nthB []) ν j 0) 0 (k + 1) = false :: vecFrom (fun j => ν j 0) 0 k := by
      show nthB [] 0 :: vecFrom (fun j => cons (nthB []) ν j 0) 1 k = _
      rw [vecFrom_shift]; rfl
    rw [e1]
    exact congrArg _ (extW_congr w _ _ (k + 1) (fun j _ i => by cases j <;> rfl))
  | a :: w, b :: bs, ν, k => by
    show (a, b :: vecFrom (fun j => ν j 0) 0 k) :: addTrack bs (extW w (shv ν) k) = _
    rw [addTrack_any w bs (shv ν) k]
    show _ = (a, vecFrom (fun j => cons (nthB (b :: bs)) ν j 0) 0 (k + 1)) :: extW w (shv (cons (nthB (b :: bs)) ν)) (k + 1)
    have e1 : vecFrom (fun j => cons (nthB (b :: bs)) ν j 0) 0 (k + 1) = b :: vecFrom (fun j => ν j 0) 0 k := by
      show nthB (b :: bs) 0 :: vecFrom (fun j => cons (nthB (b :: bs)) ν j 0) 1 k = _
      rw [vecFrom_shift]; rfl
    rw [e1]
    exact congrArg _ (extW_congr w _ _ (k + 1) (fun j _ i => by cases j <;> rfl))

/-! ## semantics depends only on the positions of the word -/

theorem nth_vec0 (ν : Val) (k X : Nat) (h : X < k) : nthB (vecFrom (fun j => ν j 0) 0 k) X = ν X 0 := by
  rw [nth_vecFrom _ k 0 X h, Nat.zero_add]

/-! ## runs from a state -/

def AF (A : FA α) (q : A.Q) (u : List α) : Prop := ∃ q', Run A q u q' ∧ A.acc q' = true

theorem af_nil (A : FA α) (q : A.Q) : AF A q [] ↔ A.acc q = true :=
  ⟨fun ⟨_, r, h⟩ => by cases r; exact h, fun h => ⟨q, .nil q, h⟩⟩

theorem af_cons (A : FA α) (q : A.Q) (x : α) (u : List α) :
    AF A q (x :: u) ↔ ∃ q', A.δ q x q' = true ∧ AF A q' u :=
  ⟨fun ⟨_, r, h⟩ => by cases r with
    | cons hd r => exact ⟨_, hd, _, r, h⟩,
   fun ⟨_, hd, q'', r, h⟩ => ⟨q'', .cons hd r, h⟩⟩

theorem accepts_af (A : FA α) (u : List α) : accepts A u ↔ ∃ q, A.start q = true ∧ AF A q u :=
  ⟨fun ⟨q, q', hs, r, h⟩ => ⟨q, hs, q', r, h⟩, fun ⟨q, hs, q', r, h⟩ => ⟨q, q', hs, r, h⟩⟩

def bitX (X : Nat) (x : α × List Bool) : Bool := nthB x.2 X

theorem bit_head (a : α) (ν : Val) {k X : Nat} (h : X < k) :
    bitX X (a, vecFrom (fun j => ν j 0) 0 k) = ν X 0 := nth_vec0 ν k X h

theorem all_cons {P : Nat → Prop} (n : Nat) : (∀ i, i < n + 1 → P i) ↔ P 0 ∧ ∀ i, i < n → P (i + 1) :=
  ⟨fun h => ⟨h 0 (Nat.succ_pos n), fun i hi => h (i + 1) (Nat.succ_lt_succ hi)⟩,
   fun ⟨h0, h⟩ i hi => match i with
    | 0 => h0
    | i + 1 => h i (Nat.lt_of_succ_lt_succ hi)⟩

/-! ## one-state automata: a test on every letter -/

def autT (t : (α × List Bool) → Bool) : FA (α × List Bool) where
  Q := Unit
  enum := [()]
  start _ := true
  δ _ x _ := t x
  acc _ := true

def AllB (t : (α × List Bool) → Bool) : List (α × List Bool) → Prop
  | [] => True
  | x :: u => t x = true ∧ AllB t u

theorem af_autT (t : (α × List Bool) → Bool) : (u : List (α × List Bool)) → AF (autT t) () u ↔ AllB t u
  | [] => ⟨fun _ => trivial, fun _ => (af_nil (autT t) ()).2 rfl⟩
  | x :: u => (af_cons (autT t) () x u).trans
      ⟨fun ⟨(), h, a⟩ => ⟨h, (af_autT t u).1 a⟩, fun ⟨h, a⟩ => ⟨(), h, (af_autT t u).2 a⟩⟩

theorem accepts_autT (t : (α × List Bool) → Bool) (u : List (α × List Bool)) : accepts (autT t) u ↔ AllB t u :=
  (accepts_af _ u).trans ⟨fun ⟨(), _, a⟩ => (af_autT t u).1 a, fun h => ⟨(), rfl, (af_autT t u).2 h⟩⟩

theorem good_autT (t : (α × List Bool) → Bool) : Good (autT t) :=
  ⟨fun () _ => .head _, fun () _ () _ _ => .head _⟩

theorem sub_atom (X Y : Nat) {k : Nat} (hX : X < k) (hY : Y < k) : (w : List α) → (ν : Val) →
    (AllB (fun x => !bitX X x || bitX Y x) (extW w ν k) ↔ sat w ν (.sub X Y))
  | [], _ => ⟨fun _ _ h => absurd h (Nat.not_lt_zero _), fun _ => trivial⟩
  | a :: w, ν => by
    show (!bitX X (a, _) || bitX Y (a, _)) = true ∧ AllB _ (extW w (shv ν) k) ↔
      ∀ i, i < len w + 1 → ν X i = true → ν Y i = true
    rw [bit_head a ν hX, bit_head a ν hY]
    refine Iff.trans ?_ (all_cons (P := fun i => ν X i = true → ν Y i = true) (len w)).symm
    refine ⟨fun ⟨h0, h⟩ => ⟨fun hx => ?_, (sub_atom X Y hX hY w (shv ν)).1 h⟩,
      fun ⟨h0, h⟩ => ⟨?_, (sub_atom X Y hX hY w (shv ν)).2 h⟩⟩
    · rw [hx] at h0; exact h0
    · cases e : ν X 0 with
      | false => rfl
      | true => rw [h0 e]; rfl

theorem lab_atom (p : α → Bool) (X : Nat) {k : Nat} (hX : X < k) : (w : List α) → (ν : Val) →
    (AllB (fun x => !bitX X x || p x.1) (extW w ν k) ↔ sat w ν (.lab p X))
  | [], _ => ⟨fun _ _ _ h => (nomatch h), fun _ => trivial⟩
  | a :: w, ν => by
    show (!bitX X (a, _) || p a) = true ∧ AllB _ (extW w (shv ν) k) ↔
      ∀ i b, nthO (a :: w) i = some b → ν X i = true → p b = true
    rw [bit_head a ν hX]
    constructor
    · intro ⟨h0, h⟩ i b hb hx
      match i, hb with
      | 0, hb =>
        have e : a = b := Option.some.inj hb
        rw [hx] at h0; rw [← e]; exact h0
      | i + 1, hb => exact (lab_atom p X hX w (shv ν)).1 h i b hb hx
    · intro h
      refine ⟨?_, (lab_atom p X hX w (shv ν)).2 (fun i b hb hx => h (i + 1) b hb hx)⟩
      cases e : ν X 0 with
      | false => rfl
      | true => rw [h 0 a rfl e]; rfl

/-! ## two-state atoms -/

inductive S2 where
  | a : S2
  | b : S2

def isA : S2 → Bool
  | .a => true
  | .b => false

def isB : S2 → Bool
  | .a => false
  | .b => true

/-- Sing X: exactly one position in X -/
def autSing (X : Nat) : FA (α × List Bool) where
  Q := S2
  enum := [.a, .b]
  start := isA
  δ q x q' := match q, q' with
    | .a, .a => !bitX X x
    | .a, .b => bitX X x
    | .b, .b => !bitX X x
    | .b, .a => false
  acc := isB

/-- X < Y: every position of X is before every position of Y -/
def autLt (X Y : Nat) : FA (α × List Bool) where
  Q := S2
  enum := [.a, .b]
  start := isA
  δ q x q' := match q, q' with
    | .a, .a => !bitX Y x
    | .a, .b => bitX Y x && !bitX X x
    | .b, .b => !bitX X x
    | .b, .a => false
  acc _ := true

theorem good_S2 (A : FA (α × List Bool)) (hq : A.Q = S2) (he : HEq A.enum [S2.a, S2.b]) : Good A := by
  cases A with
  | mk Q enum st δ acc =>
    cases hq
    cases he
    exact ⟨fun q _ => match q with
      | .a => .head _
      | .b => .tail _ (.head _),
     fun _ _ q' _ _ => match q' with
      | .a => .head _
      | .b => .tail _ (.head _)⟩

theorem bne {b : Bool} : (!b) = true ↔ b = false := by
  cases b with
  | true => exact ⟨fun h => Bool.noConfusion h, fun h => Bool.noConfusion h⟩
  | false => exact ⟨fun _ => rfl, fun _ => rfl⟩

theorem sing_b (X : Nat) {k : Nat} (hX : X < k) : (w : List α) → (ν : Val) →
    (AF (autSing X) .b (extW w ν k) ↔ ∀ i, i < len w → ν X i = false)
  | [], _ => ⟨fun _ _ h => absurd h (Nat.not_lt_zero _), fun _ => (af_nil (autSing X) .b).2 rfl⟩
  | a :: w, ν => by
    refine (af_cons (autSing X) .b _ _).trans ?_
    refine Iff.trans ?_ (all_cons (P := fun i => ν X i = false) (len w)).symm
    constructor
    · intro ⟨q', h, r⟩
      match q', h with
      | .b, h =>
        have h' : (!bitX X (a, vecFrom (fun j => ν j 0) 0 k)) = true := h
        rw [bit_head a ν hX] at h'
        exact ⟨bne.1 h', (sing_b X hX w (shv ν)).1 r⟩
      | .a, h => exact Bool.noConfusion h
    · intro ⟨h0, h⟩
      refine ⟨.b, ?_, (sing_b X hX w (shv ν)).2 h⟩
      show (!bitX X (a, vecFrom (fun j => ν j 0) 0 k)) = true
      rw [bit_head a ν hX, h0]; rfl

theorem sing_a (X : Nat) {k : Nat} (hX : X < k) : (w : List α) → (ν : Val) →
    (AF (autSing X) .a (extW w ν k) ↔ ∃ i, i < len w ∧ ν X i = true ∧ ∀ j, j < len w → ν X j = true → j = i)
  | [], _ => ⟨fun h => Bool.noConfusion ((af_nil (autSing X) .a).1 h), fun ⟨_, h, _⟩ => absurd h (Nat.not_lt_zero _)⟩
  | a :: w, ν => by
    refine (af_cons (autSing X) .a _ _).trans ?_
    constructor
    · intro ⟨q', h, r⟩
      match q', h with
      | .a, h =>
        have h' : (!bitX X (a, vecFrom (fun j => ν j 0) 0 k)) = true := h
        rw [bit_head a ν hX] at h'
        match (sing_a X hX w (shv ν)).1 r with
        | ⟨i, hi, hx, hu⟩ =>
          refine ⟨i + 1, Nat.succ_lt_succ hi, hx, fun j hj hxj => ?_⟩
          match j, hj, hxj with
          | 0, _, hxj => exact absurd hxj (by rw [bne.1 h']; exact fun e => Bool.noConfusion e)
          | j + 1, hj, hxj => exact congrArg (· + 1) (hu j (Nat.lt_of_succ_lt_succ hj) hxj)
      | .b, h =>
        have h' : bitX X (a, vecFrom (fun j => ν j 0) 0 k) = true := h
        rw [bit_head a ν hX] at h'
        have hn := (sing_b X hX w (shv ν)).1 r
        refine ⟨0, Nat.succ_pos _, h', fun j hj hxj => ?_⟩
        match j, hj, hxj with
        | 0, _, _ => rfl
        | j + 1, hj, hxj => exact absurd ((hn j (Nat.lt_of_succ_lt_succ hj)).symm.trans hxj) (fun e => Bool.noConfusion e)
    · intro ⟨i, hi, hx, hu⟩
      match i, hi, hx, hu with
      | 0, _, hx, hu =>
        refine ⟨.b, ?_, (sing_b X hX w (shv ν)).2 (fun j hj => ?_)⟩
        · show bitX X (a, vecFrom (fun j => ν j 0) 0 k) = true
          rw [bit_head a ν hX]; exact hx
        · cases e : ν X (j + 1) with
          | false => exact e
          | true => exact Nat.noConfusion (hu (j + 1) (Nat.succ_lt_succ hj) e)
      | i + 1, hi, hx, hu =>
        refine ⟨.a, ?_, (sing_a X hX w (shv ν)).2 ⟨i, Nat.lt_of_succ_lt_succ hi, hx,
          fun j hj hxj => Nat.succ.inj (hu (j + 1) (Nat.succ_lt_succ hj) hxj)⟩⟩
        show (!bitX X (a, vecFrom (fun j => ν j 0) 0 k)) = true
        rw [bit_head a ν hX]
        cases e : ν X 0 with
        | false => rfl
        | true => exact Nat.noConfusion (hu 0 (Nat.succ_pos _) e)

theorem lt_b (X Y : Nat) {k : Nat} (hX : X < k) : (w : List α) → (ν : Val) →
    (AF (autLt X Y) .b (extW w ν k) ↔ ∀ i, i < len w → ν X i = false)
  | [], _ => ⟨fun _ _ h => absurd h (Nat.not_lt_zero _), fun _ => (af_nil (autLt X Y) .b).2 rfl⟩
  | a :: w, ν => by
    refine (af_cons (autLt X Y) .b _ _).trans ?_
    refine Iff.trans ?_ (all_cons (P := fun i => ν X i = false) (len w)).symm
    constructor
    · intro ⟨q', h, r⟩
      match q', h with
      | .b, h =>
        have h' : (!bitX X (a, vecFrom (fun j => ν j 0) 0 k)) = true := h
        rw [bit_head a ν hX] at h'
        exact ⟨bne.1 h', (lt_b X Y hX w (shv ν)).1 r⟩
      | .a, h => exact Bool.noConfusion h
    · intro ⟨h0, h⟩
      refine ⟨.b, ?_, (lt_b X Y hX w (shv ν)).2 h⟩
      show (!bitX X (a, vecFrom (fun j => ν j 0) 0 k)) = true
      rw [bit_head a ν hX, h0]; rfl

theorem lt_a (X Y : Nat) {k : Nat} (hX : X < k) (hY : Y < k) : (w : List α) → (ν : Val) →
    (AF (autLt X Y) .a (extW w ν k) ↔ sat w ν (.lt X Y))
  | [], _ => ⟨fun _ _ _ h => absurd h (Nat.not_lt_zero _), fun _ => (af_nil (autLt X Y) .a).2 rfl⟩
  | a :: w, ν => by
    refine (af_cons (autLt X Y) .a _ _).trans ?_
    show _ ↔ ∀ i j, i < len w + 1 → j < len w + 1 → ν X i = true → ν Y j = true → i < j
    have ih := lt_a X Y hX hY w (shv ν)
    have hb := lt_b X Y hX w (shv ν)
    constructor
    · intro ⟨q', h, r⟩
      match q', h with
      | .a, h =>
        have h' : (!bitX Y (a, vecFrom (fun j => ν j 0) 0 k)) = true := h
        rw [bit_head a ν hY] at h'
        have hr := ih.1 r
        intro i j hi hj hx hy
        match i, j, hi, hj, hx, hy with
        | _, 0, _, _, _, hy => exact absurd hy (by rw [bne.1 h']; exact fun e => Bool.noConfusion e)
        | 0, j + 1, _, _, _, _ => exact Nat.succ_pos j
        | i + 1, j + 1, hi, hj, hx, hy =>
          exact Nat.succ_lt_succ (hr i j (Nat.lt_of_succ_lt_succ hi) (Nat.lt_of_succ_lt_succ hj) hx hy)
      | .b, h =>
        have h' := and_t (show (bitX Y (a, vecFrom (fun j => ν j 0) 0 k) && !bitX X (a, vecFrom (fun j => ν j 0) 0 k)) = true from h)
        rw [bit_head a ν hY] at h'
        rw [bit_head a ν hX] at h'
        have hn := hb.1 r
        intro i j hi hj hx hy
        match i, hi, hx with
        | 0, _, hx => exact absurd hx (by rw [bne.1 h'.2]; exact fun e => Bool.noConfusion e)
        | i + 1, hi, hx => exact absurd ((hn i (Nat.lt_of_succ_lt_succ hi)).symm.trans hx) (fun e => Bool.noConfusion e)
    · intro h
      cases ey : ν Y 0 with
      | false =>
        refine ⟨.a, ?_, ih.2 (fun i j hi hj hx hy =>
          Nat.lt_of_succ_lt_succ (h (i + 1) (j + 1) (Nat.succ_lt_succ hi) (Nat.succ_lt_succ hj) hx hy))⟩
        show (!bitX Y (a, vecFrom (fun j => ν j 0) 0 k)) = true
        rw [bit_head a ν hY, ey]; rfl
      | true =>
        have x0 : ν X 0 = false := by
          cases e : ν X 0 with
          | false => rfl
          | true => exact absurd (h 0 0 (Nat.succ_pos _) (Nat.succ_pos _) e ey) (Nat.lt_irrefl 0)
        refine ⟨.b, ?_, hb.2 (fun i hi => ?_)⟩
        · show (bitX Y (a, vecFrom (fun j => ν j 0) 0 k) && !bitX X (a, vecFrom (fun j => ν j 0) 0 k)) = true
          rw [bit_head a ν hY, bit_head a ν hX, ey, x0]; rfl
        · cases e : ν X (i + 1) with
          | false => exact e
          | true => exact absurd (h (i + 1) 0 (Nat.succ_lt_succ hi) (Nat.succ_pos _) e ey) (Nat.not_lt_zero _)

/-! ## MSO → automata -/

theorem mso_to_fa : (φ : Fm α) → (k : Nat) → Bnd φ k →
    ∃ A : FA (α × List Bool), Good A ∧ ∀ w ν, (sat w ν φ ↔ accepts A (extW w ν k))
  | .sub X Y, k, ⟨hX, hY⟩ => ⟨autT _, good_autT _, fun w ν =>
      (sub_atom X Y hX hY w ν).symm.trans (accepts_autT _ _).symm⟩
  | .lab p X, k, hX => ⟨autT _, good_autT _, fun w ν =>
      (lab_atom p X hX w ν).symm.trans (accepts_autT _ _).symm⟩
  | .sing X, k, hX => ⟨autSing X, good_S2 _ rfl HEq.rfl, fun w ν =>
      (sing_a X hX w ν).symm.trans (Iff.trans ⟨fun h => ⟨.a, rfl, h⟩, fun ⟨q, hs, h⟩ => by
        cases q with
        | a => exact h
        | b => exact Bool.noConfusion hs⟩ (accepts_af _ _).symm)⟩
  | .lt X Y, k, ⟨hX, hY⟩ => ⟨autLt X Y, good_S2 _ rfl HEq.rfl, fun w ν =>
      (lt_a X Y hX hY w ν).symm.trans (Iff.trans ⟨fun h => ⟨.a, rfl, h⟩, fun ⟨q, hs, h⟩ => by
        cases q with
        | a => exact h
        | b => exact Bool.noConfusion hs⟩ (accepts_af _ _).symm)⟩
  | .neg φ, k, h => match mso_to_fa φ k h with
    | ⟨A, g, hA⟩ => ⟨compl A, good_det A true, fun w ν =>
        Iff.trans ⟨fun hn ha => hn ((hA w ν).2 ha), fun hn hs => hn ((hA w ν).1 hs)⟩ (accepts_compl A g _).symm⟩
  | .and φ ψ, k, ⟨h1, h2⟩ => match mso_to_fa φ k h1, mso_to_fa ψ k h2 with
    | ⟨A, ga, hA⟩, ⟨B, gb, hB⟩ => ⟨prod A B, good_prod ga gb, fun w ν =>
        Iff.trans ⟨fun ⟨a, b⟩ => ⟨(hA w ν).1 a, (hB w ν).1 b⟩, fun ⟨a, b⟩ => ⟨(hA w ν).2 a, (hB w ν).2 b⟩⟩
          (accepts_prod A B _).symm⟩
  | .ex φ, k, h => match mso_to_fa φ (k + 1) h with
    | ⟨A, g, hA⟩ => ⟨proj A, good_proj g, fun w ν =>
        Iff.trans ⟨fun ⟨S, hs⟩ => ⟨vecFrom S 0 (len w), by rw [addTrack_extW]; exact (hA w _).1 hs⟩,
          fun ⟨bs, ha⟩ => ⟨nthB bs, (hA w _).2 (by rw [← addTrack_any]; exact ha)⟩⟩ (accepts_proj A _).symm⟩

end Bu

end Found

#print axioms Found.Bu.accepts_autT
#print axioms Found.Bu.sub_atom
#print axioms Found.Bu.lab_atom
#print axioms Found.Bu.addTrack_extW
#print axioms Found.Bu.addTrack_any
#print axioms Found.Bu.sing_a
#print axioms Found.Bu.lt_a
#print axioms Found.Bu.mso_to_fa

found/Buchi3.lean

lean_all/found/Buchi3.lean

/-
  Line automata ↔ MSO, part 3. found/Buchi3 — semantic tools for the
  converse: truth depends only on the positions of the word (sat_congr);
  stable formulas (¬¬φ → φ) — atoms, negations, conjunctions — so the
  derived ∀, → behave classically without any axiom; position quantifiers
  ∀Z (Sing Z → φ) read as ∀ i < |w|; first / last / successor positions;
  finite conjunctions and disjunctions over state indices; ∃X₀…X_{m−1}.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Buchi2
import found.RegComp1

namespace Found

namespace Bu

variable {α : Type}

theorem sat_congr : (φ : Fm α) → (w : List α) → (ν μ : Val) →
    (∀ j i, i < len w → ν j i = μ j i) → (sat w ν φ ↔ sat w μ φ)
  | .sub X Y, w, ν, μ, h => ⟨fun s i hi hx => (h Y i hi) ▸ s i hi ((h X i hi).symm ▸ hx),
      fun s i hi hx => (h Y i hi).symm ▸ s i hi ((h X i hi) ▸ hx)⟩
  | .sing X, w, ν, μ, h => ⟨fun ⟨i, hi, hx, hu⟩ => ⟨i, hi, (h X i hi) ▸ hx, fun j hj hxj => hu j hj ((h X j hj).symm ▸ hxj)⟩,
      fun ⟨i, hi, hx, hu⟩ => ⟨i, hi, (h X i hi).symm ▸ hx, fun j hj hxj => hu j hj ((h X j hj) ▸ hxj)⟩⟩
  | .lt X Y, w, ν, μ, h => ⟨fun s i j hi hj hx hy => s i j hi hj ((h X i hi).symm ▸ hx) ((h Y j hj).symm ▸ hy),
      fun s i j hi hj hx hy => s i j hi hj ((h X i hi) ▸ hx) ((h Y j hj) ▸ hy)⟩
  | .lab p X, w, ν, μ, h => ⟨fun s i a ha hx => s i a ha ((h X i (nthO_lt w i a ha)).symm ▸ hx),
      fun s i a ha hx => s i a ha ((h X i (nthO_lt w i a ha)) ▸ hx)⟩
  | .neg φ, w, ν, μ, h => ⟨fun s t => s ((sat_congr φ w ν μ h).2 t), fun s t => s ((sat_congr φ w ν μ h).1 t)⟩
  | .and φ ψ, w, ν, μ, h => ⟨fun ⟨a, b⟩ => ⟨(sat_congr φ w ν μ h).1 a, (sat_congr ψ w ν μ h).1 b⟩,
      fun ⟨a, b⟩ => ⟨(sat_congr φ w ν μ h).2 a, (sat_congr ψ w ν μ h).2 b⟩⟩
  | .ex φ, w, ν, μ, h => ⟨fun ⟨S, s⟩ => ⟨S, (sat_congr φ w _ _ (fun j i hi => by cases j <;> first | rfl | exact h _ i hi)).1 s⟩,
      fun ⟨S, s⟩ => ⟨S, (sat_congr φ w _ _ (fun j i hi => by cases j <;> first | rfl | exact h _ i hi)).2 s⟩⟩

/-! ## stability -/

def Stable (P : Prop) : Prop := ¬ ¬ P → P

theorem stable_neg (P : Prop) : Stable (¬ P) := fun h p => h (fun np => np p)
theorem stable_and {P R : Prop} (hp : Stable P) (hr : Stable R) : Stable (P ∧ R) :=
  fun h => ⟨hp (fun np => h (fun pr => np pr.1)), hr (fun nr => h (fun pr => nr pr.2))⟩
theorem stable_bool (b : Bool) : Stable (b = true) := fun h => by
  cases b with
  | true => rfl
  | false => exact absurd (fun e => Bool.noConfusion e) h
theorem stable_imp {P R : Prop} (hr : Stable R) : Stable (P → R) := fun h p => hr (fun nr => h (fun f => nr (f p)))
theorem stable_all {β : Type} {P : β → Prop} (h : ∀ b, Stable (P b)) : Stable (∀ b, P b) :=
  fun nn b => h b (fun np => nn (fun f => np (f b)))
theorem stable_eq_false (b : Bool) : Stable (b = false) := fun h => by
  cases b with
  | false => rfl
  | true => exact absurd (fun e => Bool.noConfusion e) h

/-- formulas built from atoms, negations and conjunctions are stable -/
def SF : Fm α → Prop
  | .sub _ _ => True
  | .sing _ => True
  | .lt _ _ => True
  | .lab _ _ => True
  | .neg _ => True
  | .and φ ψ => SF φ ∧ SF ψ
  | .ex _ => False

/-- decidable facts about Booleans at positions are stable -/
theorem stable_sing (w : List α) (ν : Val) (X : Nat) : Stable (sat w ν (.sing X)) := by
  intro nn
  -- search the first position in X
  have key : ∀ n, n ≤ len w → (∃ i, i < n ∧ ν X i = true) ∨ (∀ i, i < n → ν X i = false) := by
    intro n
    induction n with
    | zero => intro _; exact Or.inr (fun i h => absurd h (Nat.not_lt_zero _))
    | succ n ih =>
      intro hn
      cases ih (Nat.le_of_succ_le hn) with
      | inl h => match h with
        | ⟨i, hi, hx⟩ => exact Or.inl ⟨i, Nat.lt_trans hi (Nat.lt_succ_self n), hx⟩
      | inr h =>
        cases e : ν X n with
        | true => exact Or.inl ⟨n, Nat.lt_succ_self n, e⟩
        | false => exact Or.inr (fun i hi => (le_cases (Nat.le_of_lt_succ hi)).elim (fun e' => e' ▸ e) (h i))
  cases key (len w) (Nat.le_refl _) with
  | inr h => exact absurd (fun ⟨i, hi, hx, _⟩ => Bool.noConfusion ((h i hi).symm.trans hx)) nn
  | inl h =>
    match h with
    | ⟨i0, hi0, hx0⟩ =>
      -- the least position i in X, then check uniqueness among the rest
      have least : ∀ n, n ≤ len w → ∀ i, i < n → ν X i = true → ∃ i', i' < len w ∧ ν X i' = true ∧ ∀ j, j < i' → ν X j = false := by
        intro n
        induction n with
        | zero => intro _ i h; exact absurd h (Nat.not_lt_zero _)
        | succ n ih =>
          intro hn i hi hx
          cases key n (Nat.le_of_succ_le hn) with
          | inl h' => match h' with
            | ⟨i', hi', hx'⟩ => exact ih (Nat.le_of_succ_le hn) i' hi' hx'
          | inr h' =>
            have e : i = n := (le_cases (Nat.le_of_lt_succ hi)).elim id (fun hl => absurd ((h' i hl).symm.trans hx) (fun e => Bool.noConfusion e))
            exact ⟨n, Nat.lt_of_lt_of_le (Nat.lt_succ_self n) hn, e ▸ hx, h'⟩
      match least (len w) (Nat.le_refl _) i0 hi0 hx0 with
      | ⟨i, hi, hx, hb⟩ =>
        refine ⟨i, hi, hx, fun j hj hxj => ?_⟩
        cases Nat.lt_or_ge j i with
        | inl hl => exact absurd ((hb j hl).symm.trans hxj) (fun e => Bool.noConfusion e)
        | inr hg =>
          cases le_cases hg with
          | inl e => exact e.symm
          | inr hl =>
            -- a second position j > i in X contradicts ¬¬Sing
            exact absurd (fun ⟨k, _, _, hu⟩ => Nat.lt_irrefl _ ((hu i hi hx) ▸ (hu j hj hxj) ▸ hl)) nn

theorem stable_sat : (φ : Fm α) → SF φ → (w : List α) → (ν : Val) → Stable (sat w ν φ)
  | .sub _ _, _, _, _ => stable_all (fun _ => stable_imp (stable_imp (stable_bool _)))
  | .sing X, _, w, ν => stable_sing w ν X
  | .lt _ _, _, _, _ => stable_all (fun _ => stable_all (fun _ => stable_imp (stable_imp (stable_imp (stable_imp
      (fun nn => match Nat.lt_or_ge _ _ with
        | Or.inl h => h
        | Or.inr h => absurd (Nat.not_lt_of_le h) nn))))))
  | .lab _ _, _, _, _ => stable_all (fun _ => stable_all (fun _ => stable_imp (stable_imp (stable_bool _))))
  | .neg _, _, _, _ => stable_neg _
  | .and φ ψ, ⟨h1, h2⟩, w, ν => stable_and (stable_sat φ h1 w ν) (stable_sat ψ h2 w ν)

/-! ## derived connectives -/

def tt : Fm α := .ex (.sub 0 0)
def imp (a b : Fm α) : Fm α := .neg (.and a (.neg b))
def all (φ : Fm α) : Fm α := .neg (.ex (.neg φ))
/-- ∀ position Z (Z is variable 0 in φ) -/
def pos (φ : Fm α) : Fm α := all (imp (.sing 0) φ)

theorem sat_tt (w : List α) (ν : Val) : sat w ν (tt : Fm α) := ⟨fun _ => true, fun _ _ h => h⟩

theorem sat_imp (w : List α) (ν : Val) (a b : Fm α) (hb : Stable (sat w ν b)) :
    sat w ν (imp a b) ↔ (sat w ν a → sat w ν b) :=
  ⟨fun h ha => hb (fun nb => h ⟨ha, nb⟩), fun h ⟨ha, nb⟩ => nb (h ha)⟩

/-- the singleton {i} -/
def pt (i : Nat) : Nat → Bool := fun j => RC.bq j i

theorem pt_self (i : Nat) : pt i i = true := RC.bq_self i
theorem pt_ne {i j : Nat} (h : j ≠ i) : pt i j = false := RC.bq_ne j i h
theorem pt_eq {i j : Nat} (h : pt i j = true) : j = i := RC.bq_true j i h

theorem sat_pos (w : List α) (ν : Val) (φ : Fm α) (hs : ∀ μ, Stable (sat w μ φ)) :
    sat w ν (pos φ) ↔ ∀ i, i < len w → sat w (cons (pt i) ν) φ := by
  constructor
  · intro h i hi
    exact hs _ (fun nφ => h ⟨pt i, fun nimp => nimp ⟨⟨i, hi, pt_self i, fun j _ hj => pt_eq hj⟩, nφ⟩⟩)
  · intro h ⟨S, hS⟩
    exact hS (fun ⟨⟨i, hi, hx, hu⟩, nφ⟩ => nφ ((sat_congr φ w _ _ (fun j k hk => by
      cases j with
      | zero =>
        show pt i k = S k
        cases e : S k with
        | true => rw [hu k hk e]; exact pt_self i
        | false => exact pt_ne (fun e' => Bool.noConfusion (e.symm.trans (e' ▸ hx)))
      | succ j => rfl)).1 (h i hi)))

theorem stable_pos (w : List α) (ν : Val) (φ : Fm α) : Stable (sat w ν (pos φ)) := stable_neg _

/-! ## finite conjunctions and disjunctions -/

def bigAnd (f : Nat → Fm α) : Nat → Fm α
  | 0 => tt
  | n + 1 => .and (bigAnd f n) (f n)

def bigOr (f : Nat → Fm α) (n : Nat) : Fm α := .neg (bigAnd (fun j => .neg (f j)) n)

theorem sat_bigAnd (w : List α) (ν : Val) (f : Nat → Fm α) : (n : Nat) → (sat w ν (bigAnd f n) ↔ ∀ j, j < n → sat w ν (f j))
  | 0 => ⟨fun _ _ h => absurd h (Nat.not_lt_zero _), fun _ => sat_tt w ν⟩
  | n + 1 => ⟨fun ⟨a, b⟩ j hj => (le_cases (Nat.le_of_lt_succ hj)).elim (fun e => e ▸ b) ((sat_bigAnd w ν f n).1 a j),
      fun h => ⟨(sat_bigAnd w ν f n).2 (fun j hj => h j (Nat.lt_trans hj (Nat.lt_succ_self n))), h n (Nat.lt_succ_self n)⟩⟩

theorem stable_bigAnd (w : List α) (ν : Val) (f : Nat → Fm α) (hf : ∀ j, Stable (sat w ν (f j))) :
    (n : Nat) → Stable (sat w ν (bigAnd f n))
  | 0 => fun _ => sat_tt w ν
  | n + 1 => stable_and (stable_bigAnd w ν f hf n) (hf n)

/-- a disjunction of Boolean facts -/
theorem sat_bigOr_bool (w : List α) (ν : Val) (f : Nat → Fm α) (b : Nat → Bool)
    (hb : ∀ j, sat w ν (f j) ↔ b j = true) (n : Nat) :
    sat w ν (bigOr f n) ↔ ∃ j, j < n ∧ b j = true := by
  unfold bigOr
  show ¬ sat w ν (bigAnd _ n) ↔ _
  constructor
  · intro h
    cases e : anyI b n with
    | true => exact anyI_t b n e
    | false =>
      exact absurd ((sat_bigAnd w ν (fun j => .neg (f j)) n).2 (fun j hj (hf : sat w ν (f j)) => by
        have := t_anyI b n j hj ((hb j).1 hf)
        rw [e] at this
        exact Bool.noConfusion this)) h
  · intro ⟨j, hj, hbj⟩ h
    exact ((sat_bigAnd w ν _ n).1 h j hj) ((hb j).2 hbj)

/-! ## ∃ X₀ … X_{m−1} -/

def consN : Nat → (Nat → Nat → Bool) → Val → Val
  | 0, _, ν => ν
  | n + 1, T, ν => consN n T (cons (T n) ν)

theorem consN_lt (T : Nat → Nat → Bool) : (n : Nat) → (ν : Val) → (j : Nat) → j < n → consN n T ν j = T j
  | 0, _, _, h => absurd h (Nat.not_lt_zero _)
  | n + 1, ν, j, h => (le_cases (Nat.le_of_lt_succ h)).elim
      (fun e => by
        subst e
        show consN j T (cons (T j) ν) j = T j
        exact consN_ge T j _ 0 ▸ rfl)
      (fun hl => consN_lt T n _ j hl)
where
  consN_ge (T : Nat → Nat → Bool) : (n : Nat) → (μ : Val) → (d : Nat) → consN n T μ (n + d) = μ d
    | 0, _, d => by rw [Nat.zero_add]; rfl
    | n + 1, μ, d => by
      show consN n T (cons (T n) μ) (n + 1 + d) = μ d
      rw [show n + 1 + d = n + (d + 1) by rw [Nat.add_assoc, Nat.add_comm 1 d]]
      exact consN_ge T n _ (d + 1)

def exs : Nat → Fm α → Fm α
  | 0, φ => φ
  | n + 1, φ => .ex (exs n φ)

theorem le_dest' {a b : Nat} (h : a ≤ b) : ∃ d, b = a + d := by
  induction h with
  | refl => exact ⟨0, rfl⟩
  | step _ ih => match ih with
    | ⟨d, e⟩ => exact ⟨d + 1, by rw [e]; rfl⟩

theorem sat_exs (w : List α) (φ : Fm α) : (n : Nat) → (ν : Val) →
    (sat w ν (exs n φ) ↔ ∃ T : Nat → Nat → Bool, sat w (consN n T ν) φ)
  | 0, ν => ⟨fun h => ⟨fun _ _ => false, h⟩, fun ⟨_, h⟩ => h⟩
  | n + 1, ν => by
    show (∃ S, sat w (cons S ν) (exs n φ)) ↔ _
    constructor
    · intro ⟨S, h⟩
      match (sat_exs w φ n (cons S ν)).1 h with
      | ⟨T, hT⟩ =>
        let T' : Nat → Nat → Bool := fun j i => cond (RC.bq j n) (S i) (T j i)
        refine ⟨T', ?_⟩
        show sat w (consN n T' (cons (T' n) ν)) φ
        refine (sat_congr φ w _ _ (fun j i _ => ?_)).1 hT
        cases Nat.lt_or_ge j n with
        | inl hl =>
          rw [consN_lt T n _ j hl, consN_lt T' n _ j hl]
          show T j i = cond (RC.bq j n) (S i) (T j i)
          rw [RC.bq_ne j n (fun e => Nat.lt_irrefl _ (e ▸ hl))]; rfl
        | inr hg =>
          obtain ⟨d, hd⟩ := le_dest' hg
          rw [hd, consN_lt.consN_ge, consN_lt.consN_ge]
          cases d with
          | zero => show S i = cond (RC.bq n n) (S i) (T n i); rw [RC.bq_self]; rfl
          | succ d => rfl
    · intro ⟨T, h⟩
      exact ⟨T n, (sat_exs w φ n _).2 ⟨T, h⟩⟩

end Bu

end Found

#print axioms Found.Bu.sat_congr
#print axioms Found.Bu.stable_sat
#print axioms Found.Bu.sat_pos
#print axioms Found.Bu.sat_bigAnd
#print axioms Found.Bu.sat_bigOr_bool
#print axioms Found.Bu.consN_lt
#print axioms Found.Bu.sat_exs

found/Buchi4.lean

lean_all/found/Buchi4.lean

/-
  Line automata ↔ MSO, part 4. found/Buchi4 — automata → MSO: for a finite
  automaton with m listed states, the sentence ∃X₀…X_{m−1} (every position in
  some X_j ∧ the first position's sets are reachable from a start state ∧
  consecutive positions follow transitions ∧ the last position's sets
  accept ∧ the empty word is accepted iff a start state accepts) holds in a
  word exactly when the automaton accepts it (fa_to_mso).  With
  found/Buchi2 (mso_to_fa) this is the Büchi–Elgot–Trakhtenbrot theorem:
  MSO-definable = recognised by a finite automaton.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Buchi3

namespace Found

namespace Bu

variable {α : Type} (A : FA α)

def initP (j : Nat) (a : α) : Bool := anyI (fun i => stAt A i && δAt A a i j) (len A.enum)
def cE : Bool := anyI (fun i => stAt A i && accAt A i) (len A.enum)

def partF : Fm α := pos (bigOr (fun j => .sub 0 (j + 1)) (len A.enum))
def firstF : Fm α := .neg (.ex (.and (.sing 0) (.lt 0 1)))
def lastF : Fm α := .neg (.ex (.and (.sing 0) (.lt 1 0)))
def initF : Fm α := pos (imp (firstF) (bigAnd (fun j => imp (.sub 0 (j + 1)) (.lab (initP A j) 0)) (len A.enum)))
def finF : Fm α := pos (imp (lastF) (bigAnd (fun j => imp (.sub 0 (j + 1)) (.lab (fun _ => accAt A j) 0)) (len A.enum)))
def succF : Fm α := .and (.lt 1 0) (.neg (.ex (.and (.sing 0) (.and (.lt 2 0) (.lt 0 1)))))
def transF : Fm α := pos (pos (imp succF (bigAnd (fun j => bigAnd (fun j' =>
  imp (.and (.sub 1 (j + 2)) (.sub 0 (j' + 2))) (.lab (fun a => δAt A a j j') 0)) (len A.enum)) (len A.enum))))
def emptyF : Fm α := imp (.neg (.ex (.sing 0))) (cond (cE A) tt (.neg tt))
def bodyF : Fm α := .and (partF A) (.and (initF A) (.and (transF A) (.and (finF A) (emptyF A))))

/-- the sentence describing accepting runs -/
def sentence : Fm α := exs (len A.enum) (bodyF A)

/-! ## atoms at singletons -/

variable {A}

theorem sub_pt (w : List α) (μ : Val) (i j : Nat) (hi : i < len w) :
    sat w (cons (pt i) μ) (.sub 0 (j + 1)) ↔ μ j i = true :=
  ⟨fun h => h i hi (pt_self i), fun h k _ hk => by rw [pt_eq hk]; exact h⟩

theorem lab_pt (w : List α) (μ : Val) (p : α → Bool) (i : Nat) :
    sat w (cons (pt i) μ) (.lab p 0) ↔ ∀ a, nthO w i = some a → p a = true :=
  ⟨fun h a ha => h i a ha (pt_self i), fun h k a ha hk => h a (by rw [← pt_eq hk]; exact ha)⟩

theorem first_pt (w : List α) (μ : Val) (i : Nat) (hi : i < len w) :
    sat w (cons (pt i) μ) firstF ↔ i = 0 := by
  constructor
  · intro h
    cases i with
    | zero => rfl
    | succ i => exact absurd ⟨pt 0, ⟨0, Nat.lt_trans (Nat.succ_pos i) hi, pt_self 0, fun j _ hj => pt_eq hj⟩,
        fun k l hk hl hx hy => (pt_eq hx) ▸ (pt_eq hy) ▸ Nat.succ_pos i⟩ h
  · intro e ⟨S, ⟨k, hk, hx, _⟩, hlt⟩
    exact Nat.not_lt_zero k (e ▸ hlt k i hk hi hx (pt_self i))

theorem last_pt (w : List α) (μ : Val) (i : Nat) (hi : i < len w) :
    sat w (cons (pt i) μ) lastF ↔ i + 1 = len w := by
  constructor
  · intro h
    cases le_cases (Nat.succ_le_of_lt hi) with
    | inl e => exact e
    | inr hl => exact absurd ⟨pt (i + 1), ⟨i + 1, hl, pt_self _, fun j _ hj => pt_eq hj⟩,
        fun k l hk hl' hx hy => (pt_eq hx) ▸ (pt_eq hy) ▸ Nat.lt_succ_self i⟩ h
  · intro e ⟨S, ⟨k, hk, hx, _⟩, hlt⟩
    have := hlt i k hi hk (pt_self i) hx
    rw [← e] at hk
    exact Nat.lt_irrefl _ (Nat.lt_of_lt_of_le this (Nat.le_of_lt_succ hk))

theorem succ_pt (w : List α) (μ : Val) (i i' : Nat) (hi : i < len w) (hi' : i' < len w) :
    sat w (cons (pt i') (cons (pt i) μ)) succF ↔ i' = i + 1 := by
  constructor
  · intro ⟨hlt, hn⟩
    have h1 : i < i' := hlt i i' hi hi' (pt_self i) (pt_self i')
    cases le_cases (Nat.succ_le_of_lt h1) with
    | inl e => exact e.symm
    | inr hl => exact absurd ⟨pt (i + 1), ⟨i + 1, Nat.lt_trans hl hi', pt_self _, fun j _ hj => pt_eq hj⟩,
        fun k l _ _ hx hy => (pt_eq hx) ▸ (pt_eq hy) ▸ Nat.lt_succ_self i,
        fun k l _ _ hx hy => (pt_eq hx) ▸ (pt_eq hy) ▸ hl⟩ hn
  · intro e
    refine ⟨fun k l _ _ hx hy => (pt_eq hx) ▸ (pt_eq hy) ▸ (e ▸ Nat.lt_succ_self i), ?_⟩
    intro ⟨S, ⟨k, hk, hx, _⟩, h1, h2⟩
    have a := h1 i k hi hk (pt_self i) hx
    have b := h2 k i' hk hi' hx (pt_self i')
    rw [e] at b
    exact Nat.lt_irrefl _ (Nat.lt_of_lt_of_le b (Nat.succ_le_of_lt a))

theorem empty_iff (w : List α) (ν : Val) : sat w ν (.neg (.ex (.sing 0))) ↔ len w = 0 := by
  constructor
  · intro h
    cases e : len w with
    | zero => rfl
    | succ n => exact absurd ⟨pt 0, 0, e ▸ Nat.succ_pos n, pt_self 0, fun j _ hj => pt_eq hj⟩ h
  · intro e ⟨_, k, hk, _⟩
    rw [e] at hk
    exact Nat.not_lt_zero k hk

theorem sat_cond (w : List α) (ν : Val) (c : Bool) : sat w ν (cond c tt (.neg tt) : Fm α) ↔ c = true := by
  cases c with
  | true => exact ⟨fun _ => rfl, fun _ => sat_tt w ν⟩
  | false => exact ⟨fun h => absurd (sat_tt w ν) h, fun h => Bool.noConfusion h⟩

/-! ## the body, read as conditions on the sets T j -/

def Meta (A : FA α) (w : List α) (T : Nat → Nat → Bool) : Prop :=
  (∀ i, i < len w → ∃ j, j < len A.enum ∧ T j i = true) ∧
  (∀ j, j < len A.enum → T j 0 = true → ∀ a, nthO w 0 = some a → initP A j a = true) ∧
  (∀ i, i + 1 < len w → ∀ j j', j < len A.enum → j' < len A.enum → T j i = true → T j' (i + 1) = true →
    ∀ a, nthO w (i + 1) = some a → δAt A a j j' = true) ∧
  (∀ j, j < len A.enum → ∀ i, i + 1 = len w → T j i = true → accAt A j = true) ∧
  (len w = 0 → cE A = true)

theorem nthO_some : (w : List α) → (i : Nat) → i < len w → ∃ a, nthO w i = some a
  | [], _, h => absurd h (Nat.not_lt_zero _)
  | a :: _, 0, _ => ⟨a, rfl⟩
  | _ :: w, i + 1, h => nthO_some w i (Nat.lt_of_succ_lt_succ h)

theorem stab_lab (w : List α) (μ : Val) (p : α → Bool) (X : Nat) : Stable (sat w μ (.lab p X)) :=
  stable_sat (.lab p X) trivial w μ

theorem stab_imp (w : List α) (μ : Val) (a b : Fm α) : Stable (sat w μ (imp a b)) := stable_neg _

theorem body_meta (w : List α) (T : Nat → Nat → Bool) (ν : Val) :
    sat w (consN (len A.enum) T ν) (bodyF A) ↔ Meta A w T := by
  let m := len A.enum
  let μ := consN m T ν
  have hT : ∀ j, j < m → ∀ i, μ j i = T j i := fun j hj i => by
    show consN m T ν j i = T j i
    rw [consN_lt T m ν j hj]
  -- part
  have hP : sat w μ (partF A) ↔ ∀ i, i < len w → ∃ j, j < m ∧ T j i = true := by
    refine (sat_pos w μ (bigOr (fun j => (.sub 0 (j + 1) : Fm α)) m) (fun _ => stable_neg _)).trans ⟨fun h i hi => ?_, fun h i hi => ?_⟩
    · match (sat_bigOr_bool w _ _ (fun j => μ j i) (fun j => sub_pt w μ i j hi) m).1 (h i hi) with
      | ⟨j, hj, hb⟩ => exact ⟨j, hj, (hT j hj i) ▸ hb⟩
    · match h i hi with
      | ⟨j, hj, hb⟩ => exact (sat_bigOr_bool w _ _ (fun j => μ j i) (fun j => sub_pt w μ i j hi) m).2 ⟨j, hj, (hT j hj i).symm ▸ hb⟩
  -- init
  have hI : sat w μ (initF A) ↔ ∀ j, j < m → T j 0 = true → ∀ a, nthO w 0 = some a → initP A j a = true := by
    refine (sat_pos w μ _ (fun _ => stab_imp _ _ _ _)).trans ⟨fun h j hj h0 a ha => ?_, fun h i hi => ?_⟩
    · have hl : 0 < len w := nthO_lt w 0 a ha
      have h1 := (sat_imp w _ _ _ (stable_bigAnd w _ _ (fun _ => stab_imp _ _ _ _) m)).1 (h 0 hl) ((first_pt w μ 0 hl).2 rfl)
      have h2 := (sat_bigAnd w _ _ m).1 h1 j hj
      exact (lab_pt w μ _ 0).1 ((sat_imp w _ _ _ (stab_lab _ _ _ _)).1 h2 ((sub_pt w μ 0 j hl).2 ((hT j hj 0).symm ▸ h0))) a ha
    · refine (sat_imp w _ _ _ (stable_bigAnd w _ _ (fun _ => stab_imp _ _ _ _) m)).2 (fun hf => ?_)
      have e := (first_pt w μ i hi).1 hf
      subst e
      exact (sat_bigAnd w _ _ m).2 (fun j hj => (sat_imp w _ _ _ (stab_lab _ _ _ _)).2 (fun hs =>
        (lab_pt w μ _ 0).2 (h j hj ((hT j hj 0) ▸ (sub_pt w μ 0 j hi).1 hs))))
  -- final
  have hF : sat w μ (finF A) ↔ ∀ j, j < m → ∀ i, i + 1 = len w → T j i = true → accAt A j = true := by
    refine (sat_pos w μ _ (fun _ => stab_imp _ _ _ _)).trans ⟨fun h j hj i e hti => ?_, fun h i hi => ?_⟩
    · have hi : i < len w := e ▸ Nat.lt_succ_self i
      have h1 := (sat_imp w _ _ _ (stable_bigAnd w _ _ (fun _ => stab_imp _ _ _ _) m)).1 (h i hi) ((last_pt w μ i hi).2 e)
      have h2 := (sat_bigAnd w _ _ m).1 h1 j hj
      have h3 := (lab_pt w μ _ i).1 ((sat_imp w _ _ _ (stab_lab _ _ _ _)).1 h2 ((sub_pt w μ i j hi).2 ((hT j hj i).symm ▸ hti)))
      match nthO_some w i hi with
      | ⟨a, ha⟩ => exact h3 a ha
    · refine (sat_imp w _ _ _ (stable_bigAnd w _ _ (fun _ => stab_imp _ _ _ _) m)).2 (fun hl => ?_)
      have e := (last_pt w μ i hi).1 hl
      exact (sat_bigAnd w _ _ m).2 (fun j hj => (sat_imp w _ _ _ (stab_lab _ _ _ _)).2 (fun hs =>
        (lab_pt w μ _ i).2 (fun _ _ => h j hj i e ((hT j hj i) ▸ (sub_pt w μ i j hi).1 hs))))
  -- transitions
  have hTr : sat w μ (transF A) ↔ ∀ i, i + 1 < len w → ∀ j j', j < m → j' < m → T j i = true → T j' (i + 1) = true →
      ∀ a, nthO w (i + 1) = some a → δAt A a j j' = true := by
    refine (sat_pos w μ (pos (imp succF (bigAnd (fun j => bigAnd (fun j' =>
      imp (.and (.sub 1 (j + 2)) (.sub 0 (j' + 2))) (.lab (fun a => δAt A a j j') 0)) m) m)))
      (fun _ => stable_neg _)).trans ⟨fun h i hi1 j j' hj hj' h1 h2 a ha => ?_, fun h i hi => ?_⟩
    · have hi : i < len w := Nat.lt_trans (Nat.lt_succ_self i) hi1
      have hin := (sat_pos w _ _ (fun _ => stab_imp _ _ _ _)).1 (h i hi) (i + 1) hi1
      have hb := (sat_imp w _ _ _ (stable_bigAnd w _ _ (fun _ => stable_bigAnd w _ _ (fun _ => stab_imp _ _ _ _) m) m)).1 hin
        ((succ_pt w μ i (i + 1) hi hi1).2 rfl)
      have hb2 := (sat_bigAnd w _ _ m).1 ((sat_bigAnd w _ _ m).1 hb j hj) j' hj'
      have h1' : μ j i = true := by rw [hT j hj i]; exact h1
      have h2' : μ j' (i + 1) = true := by rw [hT j' hj' (i + 1)]; exact h2
      have h3 := (sat_imp w _ _ _ (stab_lab _ _ _ _)).1 hb2
        ⟨fun k _ hk => by rw [pt_eq hk]; exact h1', fun k _ hk => by rw [pt_eq hk]; exact h2'⟩
      exact (lab_pt w _ _ (i + 1)).1 h3 a ha
    · refine (sat_pos w _ _ (fun _ => stab_imp _ _ _ _)).2 (fun i' hi' => ?_)
      refine (sat_imp w _ _ _ (stable_bigAnd w _ _ (fun _ => stable_bigAnd w _ _ (fun _ => stab_imp _ _ _ _) m) m)).2 (fun hs => ?_)
      have e := (succ_pt w μ i i' hi hi').1 hs
      subst e
      exact (sat_bigAnd w _ _ m).2 (fun j hj => (sat_bigAnd w _ _ m).2 (fun j' hj' =>
        (sat_imp w _ _ _ (stab_lab _ _ _ _)).2 (fun ⟨s1, s2⟩ => (lab_pt w _ _ (i + 1)).2
          (h i hi' j j' hj hj' (by rw [← hT j hj i]; exact s1 i hi (pt_self i))
            (by rw [← hT j' hj' (i + 1)]; exact s2 (i + 1) hi' (pt_self (i + 1)))))))
  -- empty
  have hE : sat w μ (emptyF A) ↔ (len w = 0 → cE A = true) :=
    (sat_imp w _ _ _ (fun nn => (sat_cond w μ _).2 (stable_bool _ (fun nc => nn (fun s => nc ((sat_cond w μ _).1 s)))))).trans
      ⟨fun h e => (sat_cond w μ _).1 (h ((empty_iff w μ).2 e)), fun h s => (sat_cond w μ _).2 (h ((empty_iff w μ).1 s))⟩
  exact ⟨fun ⟨a, b, c, d, e⟩ => ⟨hP.1 a, hI.1 b, hTr.1 c, hF.1 d, hE.1 e⟩,
    fun ⟨a, b, c, d, e⟩ => ⟨hP.2 a, hI.2 b, hTr.2 c, hF.2 d, hE.2 e⟩⟩

end Bu

end Found

#print axioms Found.Bu.first_pt
#print axioms Found.Bu.last_pt
#print axioms Found.Bu.succ_pt
#print axioms Found.Bu.body_meta

found/Buchi5.lean

lean_all/found/Buchi5.lean

/-
  Line automata ↔ MSO, part 5. found/Buchi5 — the run conditions on the sets
  are equivalent to acceptance (meta_acc, acc_meta), so the sentence of an
  automaton defines its language (fa_to_mso); the sentence is closed; and
  the Büchi–Elgot–Trakhtenbrot theorem for languages L of finite words:
  L is defined by an MSO sentence iff L is recognised by a finite automaton
  (buchi).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Buchi4

namespace Found

namespace Bu

variable {α : Type} {A : FA α}

theorem δAt_eq {a : α} {j j' : Nat} {p q : A.Q} (hj : nthO A.enum j = some p) (hj' : nthO A.enum j' = some q) :
    δAt A a j j' = A.δ p a q := by unfold δAt; rw [hj, hj']
theorem stAt_eq {j : Nat} {p : A.Q} (hj : nthO A.enum j = some p) : stAt A j = A.start p := by unfold stAt; rw [hj]
theorem accAt_eq {j : Nat} {p : A.Q} (hj : nthO A.enum j = some p) : accAt A j = A.acc p := by unfold accAt; rw [hj]

/-- the first index below n satisfying p -/
def firstJ (p : Nat → Bool) : Nat → Nat
  | 0 => 0
  | n + 1 => cond (anyI p n) (firstJ p n) n

theorem firstJ_spec (p : Nat → Bool) : (n : Nat) → (∃ j, j < n ∧ p j = true) → firstJ p n < n ∧ p (firstJ p n) = true
  | 0, ⟨_, h, _⟩ => absurd h (Nat.not_lt_zero _)
  | n + 1, ⟨j, hj, hp⟩ => by
    show cond (anyI p n) (firstJ p n) n < n + 1 ∧ p (cond (anyI p n) (firstJ p n) n) = true
    cases e : anyI p n with
    | true =>
      have := firstJ_spec p n (anyI_t p n e)
      exact ⟨Nat.lt_trans this.1 (Nat.lt_succ_self n), this.2⟩
    | false =>
      refine ⟨Nat.lt_succ_self n, ?_⟩
      cases le_cases (Nat.le_of_lt_succ hj) with
      | inl e' => exact e' ▸ hp
      | inr hl => have := t_anyI p n j hl hp; rw [e] at this; exact Bool.noConfusion this

theorem enum_some (i : Nat) (h : i < len A.enum) : ∃ p, nthO A.enum i = some p := nthO_some A.enum i h

/-- conditions ⟹ acceptance -/
theorem meta_acc (w : List α) (T : Nat → Nat → Bool) (h : Meta A w T) : accepts A w := by
  let m := len A.enum
  let Idx : Nat → Nat := fun i => firstJ (fun j => T j i) m
  have hIdx : ∀ i, i < len w → Idx i < m ∧ T (Idx i) i = true := fun i hi =>
    firstJ_spec (fun j => T j i) m (h.1 i hi)
  have suff : ∀ (u : List α) (o : Nat), (∀ k, nthO u k = nthO w (o + 1 + k)) → len u + o + 1 = len w →
      ∀ p, nthO A.enum (Idx o) = some p → AF A p u := by
    intro u
    induction u with
    | nil =>
      intro o _ hl p hp
      have ho : o + 1 = len w := by rw [← hl]; show o + 1 = 0 + o + 1; rw [Nat.zero_add]
      have hi := hIdx o (ho ▸ Nat.lt_succ_self o)
      exact (af_nil A p).2 ((accAt_eq hp) ▸ h.2.2.2.1 (Idx o) hi.1 o ho hi.2)
    | cons a u ih =>
      intro o hk hl p hp
      have ha : nthO w (o + 1) = some a := by rw [← Nat.add_zero (o + 1), ← hk 0]; rfl
      have hlt : o + 1 < len w := nthO_lt w (o + 1) a ha
      have h0 := hIdx o (Nat.lt_trans (Nat.lt_succ_self o) hlt)
      have h1 := hIdx (o + 1) hlt
      match enum_some (Idx (o + 1)) h1.1 with
      | ⟨q, hq⟩ =>
        refine (af_cons A p a u).2 ⟨q, ?_, ih (o + 1) (fun k => ?_) ?_ q hq⟩
        · rw [← δAt_eq hp hq]
          exact h.2.2.1 o hlt (Idx o) (Idx (o + 1)) h0.1 h1.1 h0.2 h1.2 a ha
        · rw [show o + 1 + 1 + k = o + 1 + (k + 1) by rw [Nat.add_assoc, Nat.add_comm 1 k]]
          exact hk (k + 1)
        · rw [← hl]; show len u + (o + 1) + 1 = len u + 1 + o + 1; rw [Nat.add_assoc (len u) 1 o, Nat.add_comm 1 o]
  match w, h with
  | [], h =>
    match anyI_t _ _ (h.2.2.2.2 rfl) with
    | ⟨i, hi, hb⟩ =>
      match enum_some i hi with
      | ⟨p, hp⟩ =>
        have hb' := and_t hb
        exact ⟨p, p, (stAt_eq hp) ▸ hb'.1, .nil p, (accAt_eq hp) ▸ hb'.2⟩
  | a :: u, h =>
    have h0 := hIdx 0 (Nat.succ_pos _)
    match anyI_t _ _ (h.2.1 (Idx 0) h0.1 h0.2 a rfl) with
    | ⟨i, hi, hb⟩ =>
      match enum_some i hi, enum_some (Idx 0) h0.1 with
      | ⟨s, hs⟩, ⟨q, hq⟩ =>
        have hb' := and_t hb
        have haf := suff u 0 (fun k => by rw [Nat.zero_add, Nat.add_comm]; rfl) (by rw [Nat.add_zero]; rfl) q hq
        match haf with
        | ⟨q', r, hac⟩ => exact ⟨s, q', (stAt_eq hs) ▸ hb'.1, .cons ((δAt_eq hs hq) ▸ hb'.2) r, hac⟩

/-- the states of a run -/
theorem run_seq : {q : A.Q} → {w : List α} → {q' : A.Q} → Run A q w q' →
    ∃ st : Nat → A.Q, st 0 = q ∧ st (len w) = q' ∧ ∀ i a, nthO w i = some a → A.δ (st i) a (st (i + 1)) = true
  | _, _, _, .nil q => ⟨fun _ => q, rfl, rfl, fun _ _ h => nomatch h⟩
  | q, _, _, @Run.cons _ _ _ q1 _ a w hd r => match run_seq r with
    | ⟨st, h0, hn, hs⟩ => ⟨fun i => match i with
        | 0 => q
        | i + 1 => st i, rfl, hn, fun i b hb => by
          match i, hb with
          | 0, hb =>
            have e : a = b := Option.some.inj hb
            show A.δ q b (st 0) = true
            rw [h0, ← e]; exact hd
          | i + 1, hb => exact hs i b hb⟩

/-- acceptance ⟹ conditions -/
theorem acc_meta (g : Good A) (w : List α) (h : accepts A w) : ∃ T, Meta A w T := by
  match h with
  | ⟨q, q', hs, r, ha⟩ =>
    match run_seq r with
    | ⟨st, h0, hn, hδ⟩ =>
      have inE : ∀ i, i ≤ len w → Mem (st i) A.enum := by
        intro i
        induction i with
        | zero => intro _; rw [h0]; exact g.st q hs
        | succ i ih =>
          intro hi
          match nthO_some w i (Nat.lt_of_succ_le hi) with
          | ⟨a, ha⟩ => exact g.cl _ a _ (ih (Nat.le_of_succ_le hi)) (hδ i a ha)
      have idx : ∀ n, ∃ Id : Nat → Nat, ∀ i, i < n → i ≤ len w → Id i < len A.enum ∧ nthO A.enum (Id i) = some (st i) := by
        intro n
        induction n with
        | zero => exact ⟨fun _ => 0, fun _ h => absurd h (Nat.not_lt_zero _)⟩
        | succ n ih =>
          match ih with
          | ⟨Id, hId⟩ =>
            cases Nat.lt_or_ge (len w) n with
            | inl hl => exact ⟨Id, fun i hi hiw => hId i (Nat.lt_of_le_of_lt hiw hl) hiw⟩
            | inr hg =>
              match mem_idx (inE n hg) with
              | ⟨jn, hjn, ejn⟩ =>
                refine ⟨fun i => cond (RC.bq i n) jn (Id i), fun i hi hiw => ?_⟩
                cases le_cases (Nat.le_of_lt_succ hi) with
                | inl e => subst e; show cond (RC.bq i i) jn (Id i) < _ ∧ nthO A.enum (cond (RC.bq i i) jn (Id i)) = _; rw [RC.bq_self]; exact ⟨hjn, ejn⟩
                | inr hl =>
                  show cond (RC.bq i n) jn (Id i) < _ ∧ nthO A.enum (cond (RC.bq i n) jn (Id i)) = _
                  rw [RC.bq_ne i n (fun e => Nat.lt_irrefl _ (e ▸ hl))]
                  exact hId i hl hiw
      match idx (len w + 1) with
      | ⟨Id, hId⟩ =>
        have hI : ∀ i, i ≤ len w → Id i < len A.enum ∧ nthO A.enum (Id i) = some (st i) :=
          fun i hi => hId i (Nat.lt_succ_of_le hi) hi
        refine ⟨fun j i => RC.bq (Id (i + 1)) j, fun i hi => ⟨Id (i + 1), (hI (i + 1) hi).1, RC.bq_self _⟩,
          fun j _ hT a ha => ?_, fun i hi j j' _ _ h1 h2 a ha => ?_, fun j _ i hi hT => ?_, fun he => ?_⟩
        · have e : Id 1 = j := RC.bq_true _ _ hT
          have hl : 0 < len w := nthO_lt w 0 a ha
          have i0 := hI 0 (Nat.zero_le _)
          have i1 := hI 1 hl
          rw [← e]
          refine t_anyI _ _ (Id 0) i0.1 (and_i ?_ ?_)
          · rw [stAt_eq i0.2, h0]; exact hs
          · rw [δAt_eq i0.2 i1.2]; exact hδ 0 a ha
        · have e1 : Id (i + 1) = j := RC.bq_true _ _ h1
          have e2 : Id (i + 1 + 1) = j' := RC.bq_true _ _ h2
          have a1 := hI (i + 1) (Nat.le_of_lt hi)
          have a2 := hI (i + 1 + 1) (Nat.succ_le_of_lt hi)
          rw [← e1, ← e2, δAt_eq a1.2 a2.2]
          exact hδ (i + 1) a ha
        · have e : Id (i + 1) = j := RC.bq_true _ _ hT
          have a1 := hI (i + 1) (Nat.le_of_eq hi)
          rw [← e, accAt_eq a1.2, hi, hn]; exact ha
        · have i0 := hI 0 (Nat.zero_le _)
          have e : st 0 = st (len w) := by rw [he]
          refine t_anyI _ _ (Id 0) i0.1 (and_i ?_ ?_)
          · rw [stAt_eq i0.2, h0]; exact hs
          · rw [accAt_eq i0.2, e, hn]; exact ha

/-- automata → MSO: the sentence of a finite automaton defines its language -/
theorem fa_to_mso (g : Good A) (w : List α) (ν : Val) : sat w ν (sentence A) ↔ accepts A w :=
  (sat_exs w (bodyF A) (len A.enum) ν).trans
    ⟨fun ⟨T, h⟩ => meta_acc w T ((body_meta w T ν).1 h),
     fun h => match acc_meta g w h with
      | ⟨T, hm⟩ => ⟨T, (body_meta w T ν).2 hm⟩⟩

/-! ## the sentence is closed -/

theorem bnd_exs (φ : Fm α) : (n k : Nat) → Bnd φ (k + n) → Bnd (exs n φ) k
  | 0, _, h => h
  | n + 1, k, h => bnd_exs φ n (k + 1) (by rw [Nat.add_assoc, Nat.add_comm 1 n]; exact h)

theorem bnd_bigAnd (f : Nat → Fm α) (k : Nat) : (n : Nat) → (∀ j, j < n → Bnd (f j) k) → Bnd (bigAnd f n) k
  | 0, _ => ⟨Nat.succ_pos k, Nat.succ_pos k⟩
  | n + 1, h => ⟨bnd_bigAnd f k n (fun j hj => h j (Nat.lt_trans hj (Nat.lt_succ_self n))), h n (Nat.lt_succ_self n)⟩

theorem bnd_pos {φ : Fm α} {k : Nat} (h : Bnd φ (k + 1)) : Bnd (pos φ) k := ⟨Nat.succ_pos k, h⟩
theorem bnd_imp {a b : Fm α} {k : Nat} (ha : Bnd a k) (hb : Bnd b k) : Bnd (imp a b) k := ⟨ha, hb⟩
theorem bnd_tt (k : Nat) : Bnd (tt : Fm α) k := ⟨Nat.succ_pos k, Nat.succ_pos k⟩
theorem s2 (k : Nat) : 1 < k + 2 := Nat.succ_lt_succ (Nat.succ_pos k)
theorem s3 (k : Nat) : 2 < k + 3 := Nat.succ_lt_succ (s2 k)
theorem bnd_first (k : Nat) : Bnd (firstF : Fm α) (k + 1) := ⟨Nat.succ_pos _, Nat.succ_pos _, s2 k⟩
theorem bnd_last (k : Nat) : Bnd (lastF : Fm α) (k + 1) := ⟨Nat.succ_pos _, s2 k, Nat.succ_pos _⟩
theorem bnd_succ (k : Nat) : Bnd (succF : Fm α) (k + 2) :=
  ⟨⟨s2 k, Nat.succ_pos _⟩, Nat.succ_pos _, ⟨s3 k, Nat.succ_pos _⟩, ⟨Nat.succ_pos _, Nat.succ_lt_succ (Nat.succ_pos (k + 1))⟩⟩

theorem bnd_sentence (A : FA α) : Bnd (sentence A) 0 := by
  apply bnd_exs
  rw [Nat.zero_add]
  let m := len A.enum
  refine ⟨?_, ?_, ?_, ?_, ?_⟩
  · exact bnd_pos (bnd_bigAnd _ _ m (fun j hj => ⟨Nat.succ_pos m, Nat.succ_lt_succ hj⟩))
  · exact bnd_pos (bnd_imp (bnd_first m) (bnd_bigAnd _ _ m (fun j hj =>
      bnd_imp ⟨Nat.succ_pos m, Nat.succ_lt_succ hj⟩ (Nat.succ_pos m))))
  · exact bnd_pos (bnd_pos (bnd_imp (bnd_succ m) (bnd_bigAnd _ _ m (fun j hj => bnd_bigAnd _ _ m (fun j' hj' =>
      bnd_imp ⟨⟨s2 m, Nat.succ_lt_succ (Nat.succ_lt_succ hj)⟩, ⟨Nat.succ_pos _, Nat.succ_lt_succ (Nat.succ_lt_succ hj')⟩⟩
        (Nat.succ_pos _))))))
  · exact bnd_pos (bnd_imp (bnd_last m) (bnd_bigAnd _ _ m (fun j hj =>
      bnd_imp ⟨Nat.succ_pos m, Nat.succ_lt_succ hj⟩ (Nat.succ_pos m))))
  · refine ⟨Nat.succ_pos m, ?_⟩
    cases cE A with
    | true => exact bnd_tt (α := α) m
    | false => exact bnd_tt (α := α) m

/-! ## plain alphabets and the theorem -/

def plain (A : FA (α × List Bool)) : FA α where
  Q := A.Q
  enum := A.enum
  start := A.start
  δ q a q' := A.δ q (a, []) q'
  acc := A.acc

def tag0 : List α → List (α × List Bool)
  | [] => []
  | a :: w => (a, []) :: tag0 w

theorem extW_zero : (w : List α) → (ν : Val) → extW w ν 0 = tag0 w
  | [], _ => rfl
  | a :: w, ν => congrArg ((a, []) :: ·) (extW_zero w (shv ν))

theorem run_plain1 (B : FA (α × List Bool)) : {q : B.Q} → {w : List α} → {q' : B.Q} →
    Run (plain B) q w q' → Run B q (tag0 w) q'
  | _, _, _, .nil q => Run.nil (A := B) q
  | _, _, _, .cons h r => Run.cons (A := B) h (run_plain1 B r)

theorem run_plain2 (B : FA (α × List Bool)) : (w : List α) → (q q' : B.Q) →
    Run B q (tag0 w) q' → Run (plain B) q w q'
  | [], q, q', r => by
    have e : q = q' := by
      have r' : Run B q [] q' := r
      cases r'; rfl
    rw [← e]; exact Run.nil (A := plain B) q
  | a :: w, q, q', r => by
    have r' : Run B q ((a, []) :: tag0 w) q' := r
    cases r' with
    | cons h r => exact Run.cons (A := plain B) h (run_plain2 B w _ q' r)

theorem accepts_plain (B : FA (α × List Bool)) (w : List α) : accepts (plain B) w ↔ accepts B (tag0 w) :=
  ⟨fun ⟨q, q', hs, r, ha⟩ => ⟨q, q', hs, run_plain1 B r, ha⟩,
   fun ⟨q, q', hs, r, ha⟩ => ⟨q, q', hs, run_plain2 B w q q' r, ha⟩⟩

/-- Büchi–Elgot–Trakhtenbrot: a language of finite words is MSO-definable iff it is recognised by a
  finite automaton -/
theorem buchi (L : List α → Prop) :
    (∃ φ : Fm α, Bnd φ 0 ∧ ∀ w, (sat w (fun _ _ => false) φ ↔ L w)) ↔
    (∃ A : FA α, Good A ∧ ∀ w, (accepts A w ↔ L w)) :=
  ⟨fun ⟨φ, hb, hφ⟩ => match mso_to_fa φ 0 hb with
    | ⟨A, g, hA⟩ => ⟨plain A, ⟨g.st, fun q a q' m h => g.cl q (a, []) q' m h⟩, fun w =>
        (accepts_plain A w).trans (by rw [← extW_zero w (fun _ _ => false)]; exact (hA w _).symm.trans (hφ w))⟩,
   fun ⟨A, g, hA⟩ => ⟨sentence A, bnd_sentence A, fun w => (fa_to_mso g w _).trans (hA w)⟩⟩

end Bu

end Found

#print axioms Found.Bu.firstJ_spec
#print axioms Found.Bu.meta_acc
#print axioms Found.Bu.run_seq
#print axioms Found.Bu.acc_meta
#print axioms Found.Bu.fa_to_mso
#print axioms Found.Bu.bnd_sentence
#print axioms Found.Bu.accepts_plain
#print axioms Found.Bu.buchi

found/CCC.lean

lean_all/found/CCC.lean

/-
  T62. found/CCC — cartesian closed and locally cartesian closed categories.

  * CCC as an E-category with terminal, products, exponentials (curry/eval).
  * In any CCC: (- × b) and (b ⇒ -) are functors and (- × b) ⊣ (b ⇒ -);
    hence a monad b ⇒ (- × b) (the state monad).
  * Condition: setoids form a CCC (no axioms); for types with pointwise
    equality the standard currying respects equality iff funext holds,
    so "Fun is a CCC" is exactly the funext condition.
  * LCCC in family form: for f : I → J, the reindexing f* on families of
    setoids has a right adjoint Π_f (the dependent product ∀).

  Lean 4 core only, hand-written proofs, no axioms.

  CIC dependence: definitional structure eta (beta/eta of setdCCC are refl); not impredicative Prop.
-/
import found.Cat

namespace Found

universe u v

/-! ## Cartesian closed E-categories -/

structure CCC (C : ECat.{u, v}) where
  term : Terminal C
  prod : (a b : C.Ob) → Product C a b
  exp : C.Ob → C.Ob → C.Ob
  ev : (b c : C.Ob) → C.Hom (prod (exp b c) b).P c
  curry : {a b c : C.Ob} → C.Hom (prod a b).P c → C.Hom a (exp b c)
  curry_resp : ∀ {a b c} {h h' : C.Hom (prod a b).P c}, C.heq h h' → C.heq (curry h) (curry h')
  beta : ∀ {a b c} (h : C.Hom (prod a b).P c),
    C.heq (C.comp ((prod (exp b c) b).pair (C.comp (prod a b).p1 (curry h)) (prod a b).p2) (ev b c)) h
  eta : ∀ {a b c} (g : C.Hom a (exp b c)),
    C.heq (curry (C.comp ((prod (exp b c) b).pair (C.comp (prod a b).p1 g) (prod a b).p2) (ev b c))) g

namespace CCC
variable {C : ECat.{u, v}} (K : CCC C)

/-- `f × b` -/
def times {a a' : C.Ob} (b : C.Ob) (f : C.Hom a a') : C.Hom (K.prod a b).P (K.prod a' b).P :=
  (K.prod a' b).pair (C.comp (K.prod a b).p1 f) (K.prod a b).p2

theorem times_resp {a a' : C.Ob} (b : C.Ob) {f f' : C.Hom a a'} (h : C.heq f f') :
    C.heq (K.times b f) (K.times b f') :=
  (K.prod a' b).pair_resp (C.rcong _ h) (C.hrefl _)

theorem times_id (a b : C.Ob) : C.heq (K.times b (C.id a)) (C.id (K.prod a b).P) :=
  C.htrans ((K.prod a b).pair_resp
      (C.htrans (C.comp_id _) (C.hsymm (C.id_comp _))) (C.hsymm (C.id_comp _)))
    (C.hsymm ((K.prod a b).eta (C.id _)))

theorem times_comp {a a' a'' : C.Ob} (b : C.Ob) (f : C.Hom a a') (g : C.Hom a' a'') :
    C.heq (K.times b (C.comp f g)) (C.comp (K.times b f) (K.times b g)) := by
  have r1 : C.heq (C.comp (C.comp (K.times b f) (K.times b g)) (K.prod a'' b).p1)
      (C.comp (K.prod a b).p1 (C.comp f g)) :=
    C.htrans (C.assoc _ _ _) (C.htrans (C.rcong _ ((K.prod a'' b).beta1 _ _))
      (C.htrans (C.hsymm (C.assoc _ _ _)) (C.htrans (C.lcong ((K.prod a' b).beta1 _ _) g)
        (C.assoc _ _ _))))
  have r2 : C.heq (C.comp (C.comp (K.times b f) (K.times b g)) (K.prod a'' b).p2)
      (K.prod a b).p2 :=
    C.htrans (C.assoc _ _ _) (C.htrans (C.rcong _ ((K.prod a'' b).beta2 _ _))
      ((K.prod a' b).beta2 _ _))
  exact C.htrans ((K.prod a'' b).pair_resp (C.hsymm r1) (C.hsymm r2))
    (C.hsymm ((K.prod a'' b).eta _))

/-- the exponential transpose is unique -/
theorem curry_uniq {a b c : C.Ob} (k : C.Hom a (K.exp b c)) (h : C.Hom (K.prod a b).P c)
    (e : C.heq (C.comp (K.times b k) (K.ev b c)) h) : C.heq k (K.curry h) :=
  C.htrans (C.hsymm (K.eta k)) (K.curry_resp e)

/-- `b ⇒ g` -/
def expMap (b : C.Ob) {c c' : C.Ob} (g : C.Hom c c') : C.Hom (K.exp b c) (K.exp b c') :=
  K.curry (C.comp (K.ev b c) g)

theorem expMap_id (b c : C.Ob) : C.heq (K.expMap b (C.id c)) (C.id (K.exp b c)) :=
  C.hsymm (K.curry_uniq _ _
    (C.htrans (C.lcong (K.times_id _ b) _)
      (C.htrans (C.id_comp _) (C.hsymm (C.comp_id _)))))

theorem expMap_comp (b : C.Ob) {c c' c'' : C.Ob} (g : C.Hom c c') (g' : C.Hom c' c'') :
    C.heq (K.expMap b (C.comp g g')) (C.comp (K.expMap b g) (K.expMap b g')) :=
  C.hsymm (K.curry_uniq _ _
    (C.htrans (C.lcong (K.times_comp b _ _) _)
    (C.htrans (C.assoc _ _ _)
    (C.htrans (C.rcong _ (K.beta _))
    (C.htrans (C.hsymm (C.assoc _ _ _))
    (C.htrans (C.lcong (K.beta _) g') (C.assoc _ _ _)))))))

def prodF (b : C.Ob) : EFun C C where
  obj a := (K.prod a b).P
  map f := K.times b f
  map_resp h := K.times_resp b h
  map_id a := K.times_id a b
  map_comp f g := K.times_comp b f g

def expF (b : C.Ob) : EFun C C where
  obj c := K.exp b c
  map g := K.expMap b g
  map_resp h := K.curry_resp (C.rcong _ h)
  map_id c := K.expMap_id b c
  map_comp g g' := K.expMap_comp b g g'

/-- the defining adjunction of a CCC: `(- × b) ⊣ (b ⇒ -)` -/
def adj (b : C.Ob) : Adj (K.prodF b) (K.expF b) where
  phi h := K.curry h
  psi g := C.comp (K.times b g) (K.ev b _)
  phi_resp h := K.curry_resp h
  psi_resp h := C.lcong (K.times_resp b h) _
  phi_psi g := K.eta g
  psi_phi h := K.beta h
  nat_l _ h := C.hsymm (K.curry_uniq _ _
    (C.htrans (C.lcong (K.times_comp b _ _) _)
    (C.htrans (C.assoc _ _ _) (C.rcong _ (K.beta h)))))
  nat_r h g := C.hsymm (K.curry_uniq _ _
    (C.htrans (C.lcong (K.times_comp b _ _) _)
    (C.htrans (C.assoc _ _ _)
    (C.htrans (C.rcong _ (K.beta _))
    (C.htrans (C.hsymm (C.assoc _ _ _)) (C.lcong (K.beta h) g))))))

/-- the state monad `b ⇒ (- × b)` of any CCC -/
def stateMonad (b : C.Ob) : KMonad C := (K.adj b).monad

end CCC

/-! ## Setoids form a CCC -/

def unitS : Setd.{u} := ⟨PUnit, fun _ _ => True, fun _ => trivial, fun _ => trivial, fun _ _ => trivial⟩

def prodS (X Y : Setd.{u}) : Setd.{u} where
  car := X.car × Y.car
  eqv p q := X.eqv p.1 q.1 ∧ Y.eqv p.2 q.2
  refl p := ⟨X.refl p.1, Y.refl p.2⟩
  symm h := ⟨X.symm h.1, Y.symm h.2⟩
  trans h k := ⟨X.trans h.1 k.1, Y.trans h.2 k.2⟩

def expS (X Y : Setd.{u}) : Setd.{u} where
  car := SHom X Y
  eqv f g := ∀ x, Y.eqv (f.fn x) (g.fn x)
  refl f x := Y.refl (f.fn x)
  symm h x := Y.symm (h x)
  trans h k x := Y.trans (h x) (k x)

def setdTerminal : Terminal SetdCat.{u} where
  one := unitS
  bang _ := ⟨fun _ => PUnit.unit, fun _ => trivial⟩
  uniq _ _ := trivial

def setdProduct (X Y : Setd.{u}) : Product SetdCat.{u} X Y where
  P := prodS X Y
  p1 := ⟨Prod.fst, fun h => h.1⟩
  p2 := ⟨Prod.snd, fun h => h.2⟩
  pair f g := ⟨fun z => (f.fn z, g.fn z), fun h => ⟨f.resp h, g.resp h⟩⟩
  beta1 f _ z := X.refl (f.fn z)
  beta2 _ g z := Y.refl (g.fn z)
  eta h z := ⟨X.refl (h.fn z).1, Y.refl (h.fn z).2⟩
  pair_resp hf hg z := ⟨hf z, hg z⟩

def setdCCC : CCC SetdCat.{u} where
  term := setdTerminal
  prod := setdProduct
  exp := expS
  ev _ Z := ⟨fun p => p.1.fn p.2, fun {p q} h => Z.trans (p.1.resp h.2) (h.1 q.2)⟩
  curry {X Y _} h :=
    ⟨fun x => ⟨fun y => h.fn (x, y), fun e => h.resp ⟨X.refl x, e⟩⟩,
     fun e y => h.resp ⟨e, Y.refl y⟩⟩
  curry_resp hh x y := hh (x, y)
  beta {_ _ c} h p := c.refl (h.fn p)
  eta {_ _ c} g x y := c.refl ((g.fn x).fn y)

/-- the state monad on setoids -/
def setdState (S : Setd.{u}) : KMonad SetdCat.{u} := setdCCC.stateMonad S

/-! ## Types with pointwise equality: CCC ⇔ funext -/

def FunExt : Prop := ∀ {B C : Type u} (f g : B → C), (∀ x, f x = g x) → f = g

/-- the standard currying on `FunCat` respects the hom equivalence -/
def FunCurryResp : Prop :=
  ∀ {A B C : Type u} (h h' : A × B → C), (∀ p, h p = h' p) →
    ∀ a, (fun b => h (a, b)) = (fun b => h' (a, b))

theorem funCurryResp_iff_funext : FunCurryResp.{u} ↔ FunExt.{u} :=
  ⟨fun H _ _ f g e => H (fun p => f p.2) (fun p => g p.2) (fun p => e p.2) PUnit.unit,
   fun F _ _ _ _ _ e a => F _ _ (fun b => e (a, b))⟩

/-! ## LCCC in family form: reindexing has a right adjoint Π -/

/-- families of setoids over a type, with pointwise maps -/
def Fam (I : Type u) : ECat.{u+1, u} where
  Ob := I → Setd.{u}
  Hom X Y := (i : I) → SHom (X i) (Y i)
  heq {_ Y} f g := ∀ i x, (Y i).eqv ((f i).fn x) ((g i).fn x)
  hrefl {_ Y} f i x := (Y i).refl ((f i).fn x)
  hsymm {_ Y _ _} h i x := (Y i).symm (h i x)
  htrans {_ Y _ _ _} h k i x := (Y i).trans (h i x) (k i x)
  id _ _ := ⟨fun x => x, fun h => h⟩
  comp f g i := ⟨fun x => (g i).fn ((f i).fn x), fun h => (g i).resp ((f i).resp h)⟩
  comp_resp {_ _ Z} _ _ g _ hf hg i x := (Z i).trans ((g i).resp (hf i x)) (hg i _)
  id_comp {_ Y} f i x := (Y i).refl ((f i).fn x)
  comp_id {_ Y} f i x := (Y i).refl ((f i).fn x)
  assoc {_ _ _ W} _ _ h i _ := (W i).refl ((h i).fn _)

section Reindex
variable {I J : Type u} (f : I → J)

/-- reindexing `f*` -/
def pullF : EFun (Fam J) (Fam I) where
  obj Y i := Y (f i)
  map g i := g (f i)
  map_resp h i x := h (f i) x
  map_id Y i x := (Y (f i)).refl x
  map_comp {_ _ Z} _ g i _ := (Z (f i)).refl ((g (f i)).fn _)

/-- the dependent product `Π_f X` at `j`: sections over the fibre of `j` -/
def piS (X : I → Setd.{u}) (j : J) : Setd.{u} where
  car := (i : I) → f i = j → (X i).car
  eqv s t := ∀ i e, (X i).eqv (s i e) (t i e)
  refl s i e := (X i).refl (s i e)
  symm h i e := (X i).symm (h i e)
  trans h k i e := (X i).trans (h i e) (k i e)

def piF : EFun (Fam I) (Fam J) where
  obj X j := piS f X j
  map g _ := ⟨fun s i e => (g i).fn (s i e), fun h i e => (g i).resp (h i e)⟩
  map_resp h _ s i e := h i (s i e)
  map_id X _ s i e := (X i).refl (s i e)
  map_comp {_ _ Z} _ g _ _ i _ := (Z i).refl ((g i).fn _)

/-- transport along a fibre equation -/
def tr (Y : J → Setd.{u}) {j j' : J} (e : j = j') (y : (Y j).car) : (Y j').car := by
  cases e
  exact y

theorem tr_resp (Y : J → Setd.{u}) {j j' : J} (e : j = j') {y y' : (Y j).car}
    (h : (Y j).eqv y y') : (Y j').eqv (tr Y e y) (tr Y e y') := by
  cases e
  exact h

/-- `f* ⊣ Π_f` : reindexing has a right adjoint, the dependent product -/
def pullPiAdj : Adj (pullF f) (piF f) where
  phi {Y _} k j := ⟨fun y i e => (k i).fn (tr Y e.symm y),
                    fun h i e => (k i).resp (tr_resp Y e.symm h)⟩
  psi g i := ⟨fun y => (g (f i)).fn y i rfl, fun h => (g (f i)).resp h i rfl⟩
  phi_resp hk _ _ i _ := hk i _
  psi_resp hg i y := hg (f i) y i rfl
  phi_psi {Y X} g j y i e := by
    cases e
    exact (X i).refl _
  psi_phi {_ X} k i y := (X i).refl _
  nat_l {_ _ X} a k j y i e := by
    cases e
    exact (X i).refl _
  nat_r {_ _ X'} k g j y i e := by
    cases e
    exact (X' i).refl _

end Reindex

end Found

#print axioms Found.CCC.times_id
#print axioms Found.CCC.times_comp
#print axioms Found.CCC.curry_uniq
#print axioms Found.CCC.expMap_id
#print axioms Found.CCC.expMap_comp
#print axioms Found.CCC.prodF
#print axioms Found.CCC.expF
#print axioms Found.CCC.adj
#print axioms Found.CCC.stateMonad
#print axioms Found.setdTerminal
#print axioms Found.setdProduct
#print axioms Found.setdCCC
#print axioms Found.setdState
#print axioms Found.funCurryResp_iff_funext
#print axioms Found.Fam
#print axioms Found.pullF
#print axioms Found.piF
#print axioms Found.tr_resp
#print axioms Found.pullPiAdj

found/CHL.lean

lean_all/found/CHL.lean

/-
  T76. found/CHL — Curry–Howard–Lambek.

  * Curry–Howard: a simply typed term is a natural deduction derivation;
    erasing it gives an intuitionistic derivation (T65) of its type read as
    a formula.
  * Lambek: every cartesian closed E-category (T62) is a model of the simply
    typed λ-calculus with βη: terms denote morphisms ⟦Γ⟧ → ⟦A⟧, renaming
    and substitution are composition, and βη-convertible terms denote
    equivalent morphisms.  With the setoid CCC this needs no funext.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Sequent
import found.CCC
import found.Minimal

namespace Found

namespace ND

/-! ## Curry–Howard: terms are derivations -/

def toForm : Ty → Prp.Form
  | .base n => .var n
  | .arr A B => .imp (toForm A) (toForm B)
  | .prod A B => .and (toForm A) (toForm B)

def ctxP (Γ : List Ty) : Prp.Form → Prop := fun x => ∃ B, Nonempty (Var Γ B) ∧ x = toForm B

theorem ctxP_cons (Γ : List Ty) (A : Ty) : ∀ x, ctxP (A :: Γ) x → Prp.ext (ctxP Γ) (toForm A) x :=
  fun _ ⟨B, ⟨v⟩, e⟩ => by
    cases v with
    | z => exact Or.inl e
    | s v => exact Or.inr ⟨B, ⟨v⟩, e⟩

theorem erase {Γ : List Ty} {A : Ty} : Tm Γ A → Prp.PD true (ctxP Γ) (toForm A)
  | .var v => .hyp ⟨_, ⟨v⟩, rfl⟩
  | @Tm.lam _ A' _ t => .impI (Prp.mono (erase t) (ctxP_cons _ A'))
  | .app t u => .impE (erase t) (erase u)
  | .pair t u => .andI (erase t) (erase u)
  | .fst t => .andE1 (erase t)
  | .snd t => .andE2 (erase t)

/-! ## Renaming and substitution of terms -/

def renTm {Γ Δ : List Ty} {A : Ty} (r : Ren Γ Δ) : Tm Γ A → Tm Δ A
  | .var v => .var (r _ v)
  | .lam t => .lam (renTm r.lift t)
  | .app t u => .app (renTm r t) (renTm r u)
  | .pair t u => .pair (renTm r t) (renTm r u)
  | .fst t => .fst (renTm r t)
  | .snd t => .snd (renTm r t)

def Sub (Γ Δ : List Ty) : Type := (X : Ty) → Var Δ X → Tm Γ X

def Sub.lift {Γ Δ : List Ty} {A : Ty} (σ : Sub Γ Δ) : Sub (A :: Γ) (A :: Δ) :=
  fun _ v => match v with
    | .z => .var .z
    | .s v => renTm wk1 (σ _ v)

def subTm {Γ Δ : List Ty} {A : Ty} (σ : Sub Γ Δ) : Tm Δ A → Tm Γ A
  | .var v => σ _ v
  | .lam t => .lam (subTm σ.lift t)
  | .app t u => .app (subTm σ t) (subTm σ u)
  | .pair t u => .pair (subTm σ t) (subTm σ u)
  | .fst t => .fst (subTm σ t)
  | .snd t => .snd (subTm σ t)

def single {Γ : List Ty} {A : Ty} (u : Tm Γ A) : Sub Γ (A :: Γ) :=
  fun _ v => match v with
    | .z => u
    | .s v => .var v

/-- βη-conversion -/
inductive Conv : {Γ : List Ty} → {A : Ty} → Tm Γ A → Tm Γ A → Prop where
  | refl {Γ A} (t : Tm Γ A) : Conv t t
  | symm {Γ A} {t u : Tm Γ A} : Conv t u → Conv u t
  | trans {Γ A} {t u w : Tm Γ A} : Conv t u → Conv u w → Conv t w
  | lam {Γ A B} {t t' : Tm (A :: Γ) B} : Conv t t' → Conv (.lam t) (.lam t')
  | app {Γ A B} {t t' : Tm Γ (.arr A B)} {u u' : Tm Γ A} : Conv t t' → Conv u u' → Conv (.app t u) (.app t' u')
  | pair {Γ A B} {t t' : Tm Γ A} {u u' : Tm Γ B} : Conv t t' → Conv u u' → Conv (.pair t u) (.pair t' u')
  | fst {Γ A B} {t t' : Tm Γ (.prod A B)} : Conv t t' → Conv (.fst t) (.fst t')
  | snd {Γ A B} {t t' : Tm Γ (.prod A B)} : Conv t t' → Conv (.snd t) (.snd t')
  | beta {Γ A B} (t : Tm (A :: Γ) B) (u : Tm Γ A) : Conv (.app (.lam t) u) (subTm (single u) t)
  | eta {Γ A B} (t : Tm Γ (.arr A B)) : Conv (.lam (.app (renTm wk1 t) (.var .z))) t
  | fstβ {Γ A B} (t : Tm Γ A) (u : Tm Γ B) : Conv (.fst (.pair t u)) t
  | sndβ {Γ A B} (t : Tm Γ A) (u : Tm Γ B) : Conv (.snd (.pair t u)) u
  | pairη {Γ A B} (t : Tm Γ (.prod A B)) : Conv (.pair (.fst t) (.snd t)) t

end ND

/-! ## Lambek: interpretation in a CCC -/

namespace CHL

open ND

universe u v

variable {C : ECat.{u, v}} (K : CCC C) (ι : Nat → C.Ob)

theorem pair_comp {x y a b : C.Ob} (P : Product C a b) (f : C.Hom x y) (g : C.Hom y a) (h : C.Hom y b) :
    C.heq (C.comp f (P.pair g h)) (P.pair (C.comp f g) (C.comp f h)) :=
  C.htrans (P.eta _) (P.pair_resp
    (C.htrans (C.assoc _ _ _) (C.rcong f (P.beta1 g h)))
    (C.htrans (C.assoc _ _ _) (C.rcong f (P.beta2 g h))))

def obj : Ty → C.Ob
  | .base n => ι n
  | .arr A B => K.exp (obj A) (obj B)
  | .prod A B => (K.prod (obj A) (obj B)).P

def ctxO : List Ty → C.Ob
  | [] => K.term.one
  | A :: Γ => (K.prod (ctxO Γ) (obj K ι A)).P

def denV : {Γ : List Ty} → {A : Ty} → Var Γ A → C.Hom (ctxO K ι Γ) (obj K ι A)
  | A :: Γ, _, .z => (K.prod (ctxO K ι Γ) (obj K ι A)).p2
  | B :: Γ, _, .s v => C.comp (K.prod (ctxO K ι Γ) (obj K ι B)).p1 (denV v)

def den : {Γ : List Ty} → {A : Ty} → Tm Γ A → C.Hom (ctxO K ι Γ) (obj K ι A)
  | _, _, .var v => denV K ι v
  | _, _, .lam t => K.curry (den t)
  | _, _, @Tm.app _ A B t u =>
    C.comp ((K.prod (K.exp (obj K ι A) (obj K ι B)) (obj K ι A)).pair (den t) (den u)) (K.ev _ _)
  | _, _, .pair t u => (K.prod _ _).pair (den t) (den u)
  | _, _, .fst t => C.comp (den t) (K.prod _ _).p1
  | _, _, .snd t => C.comp (den t) (K.prod _ _).p2

def denR : {Γ Δ : List Ty} → Ren Γ Δ → C.Hom (ctxO K ι Δ) (ctxO K ι Γ)
  | [], _, _ => K.term.bang _
  | A :: Γ, _, r => (K.prod (ctxO K ι Γ) (obj K ι A)).pair (denR (fun X v => r X (.s v))) (denV K ι (r A .z))

def denS : {Γ Δ : List Ty} → Sub Γ Δ → C.Hom (ctxO K ι Γ) (ctxO K ι Δ)
  | _, [], _ => K.term.bang _
  | _, A :: Δ, σ => (K.prod (ctxO K ι Δ) (obj K ι A)).pair (denS (fun X v => σ X (.s v))) (den K ι (σ A .z))

theorem var_ren : {Γ Δ : List Ty} → {X : Ty} → (r : Ren Γ Δ) → (v : Var Γ X) →
    C.heq (denV K ι (r X v)) (C.comp (denR K ι r) (denV K ι v))
  | _ :: _, _, _, _, .z => C.hsymm ((K.prod _ _).beta2 _ _)
  | _ :: _, _, _, r, .s v =>
    C.htrans (var_ren (fun X w => r X (.s w)) v)
      (C.hsymm (C.htrans (C.hsymm (C.assoc _ _ _)) (C.lcong ((K.prod _ _).beta1 _ _) _)))

theorem wk_ren {B : Ty} : {Γ Δ : List Ty} → (r : Ren Γ Δ) →
    C.heq (denR K ι (Γ := Γ) (Δ := B :: Δ) (fun X v => .s (r X v)))
      (C.comp (K.prod (ctxO K ι Δ) (obj K ι B)).p1 (denR K ι r))
  | [], _, _ => C.htrans (K.term.uniq _) (C.hsymm (K.term.uniq _))
  | _ :: _, _, r =>
    C.htrans ((K.prod _ _).pair_resp (wk_ren (fun X v => r X (.s v))) (C.hrefl _))
      (C.hsymm (pair_comp _ _ _ _))

theorem lift_ren {A : Ty} {Γ Δ : List Ty} (r : Ren Γ Δ) :
    C.heq (denR K ι (Ren.lift (A := A) r)) (K.times (obj K ι A) (denR K ι r)) :=
  (K.prod _ _).pair_resp (wk_ren K ι r) (C.hrefl _)

theorem id_ren : (Γ : List Ty) → C.heq (denR K ι (Γ := Γ) (fun _ v => v)) (C.id _)
  | [] => C.htrans (K.term.uniq _) (C.hsymm (K.term.uniq _))
  | _ :: Γ =>
    C.htrans ((K.prod _ _).pair_resp
        (C.htrans (wk_ren K ι (Γ := Γ) (fun _ v => v))
          (C.htrans (C.rcong _ (id_ren Γ)) (C.htrans (C.comp_id _) (C.hsymm (C.id_comp _)))))
        (C.hsymm (C.id_comp _)))
      (C.hsymm ((K.prod _ _).eta _))

theorem ren_den : {Γ Δ : List Ty} → {A : Ty} → (r : Ren Γ Δ) → (t : Tm Γ A) →
    C.heq (den K ι (renTm r t)) (C.comp (denR K ι r) (den K ι t))
  | _, _, _, r, .var v => var_ren K ι r v
  | _, _, _, r, @Tm.lam _ A _ t =>
    C.htrans (K.curry_resp (C.htrans (ren_den r.lift t) (C.lcong (lift_ren K ι (A := A) r) _)))
      ((K.adj (obj K ι A)).nat_l (denR K ι r) (den K ι t))
  | _, _, _, r, .app t u =>
    C.htrans (C.lcong ((K.prod _ _).pair_resp (ren_den r t) (ren_den r u)) _)
      (C.htrans (C.lcong (C.hsymm (pair_comp _ _ _ _)) _) (C.assoc _ _ _))
  | _, _, _, r, .pair t u =>
    C.htrans ((K.prod _ _).pair_resp (ren_den r t) (ren_den r u)) (C.hsymm (pair_comp _ _ _ _))
  | _, _, _, r, .fst t => C.htrans (C.lcong (ren_den r t) _) (C.assoc _ _ _)
  | _, _, _, r, .snd t => C.htrans (C.lcong (ren_den r t) _) (C.assoc _ _ _)

theorem wk1_den {B : Ty} {Γ : List Ty} :
    C.heq (denR K ι (wk1 (Γ := Γ) (B := B))) (K.prod (ctxO K ι Γ) (obj K ι B)).p1 :=
  C.htrans (wk_ren K ι (B := B) (fun _ v => v)) (C.htrans (C.rcong _ (id_ren K ι Γ)) (C.comp_id _))

theorem var_sub : {Γ Δ : List Ty} → {X : Ty} → (σ : Sub Γ Δ) → (v : Var Δ X) →
    C.heq (den K ι (σ X v)) (C.comp (denS K ι σ) (denV K ι v))
  | _, _ :: _, _, _, .z => C.hsymm ((K.prod _ _).beta2 _ _)
  | _, _ :: _, _, σ, .s v =>
    C.htrans (var_sub (fun X w => σ X (.s w)) v)
      (C.hsymm (C.htrans (C.hsymm (C.assoc _ _ _)) (C.lcong ((K.prod _ _).beta1 _ _) _)))

theorem wk_sub {B : Ty} : {Γ Δ : List Ty} → (σ : Sub Γ Δ) →
    C.heq (denS K ι (Γ := B :: Γ) (Δ := Δ) (fun X v => renTm wk1 (σ X v)))
      (C.comp (K.prod (ctxO K ι Γ) (obj K ι B)).p1 (denS K ι σ))
  | _, [], _ => C.htrans (K.term.uniq _) (C.hsymm (K.term.uniq _))
  | _, _ :: _, σ =>
    C.htrans ((K.prod _ _).pair_resp (wk_sub (fun X v => σ X (.s v)))
        (C.htrans (ren_den K ι wk1 _) (C.lcong (wk1_den K ι) _)))
      (C.hsymm (pair_comp _ _ _ _))

theorem lift_sub {A : Ty} {Γ Δ : List Ty} (σ : Sub Γ Δ) :
    C.heq (denS K ι (Sub.lift (A := A) σ)) (K.times (obj K ι A) (denS K ι σ)) :=
  (K.prod _ _).pair_resp (wk_sub K ι σ) (C.hrefl _)

theorem sub_den : {Γ Δ : List Ty} → {A : Ty} → (σ : Sub Γ Δ) → (t : Tm Δ A) →
    C.heq (den K ι (subTm σ t)) (C.comp (denS K ι σ) (den K ι t))
  | _, _, _, σ, .var v => var_sub K ι σ v
  | _, _, _, σ, @Tm.lam _ A _ t =>
    C.htrans (K.curry_resp (C.htrans (sub_den σ.lift t) (C.lcong (lift_sub K ι (A := A) σ) _)))
      ((K.adj (obj K ι A)).nat_l (denS K ι σ) (den K ι t))
  | _, _, _, σ, .app t u =>
    C.htrans (C.lcong ((K.prod _ _).pair_resp (sub_den σ t) (sub_den σ u)) _)
      (C.htrans (C.lcong (C.hsymm (pair_comp _ _ _ _)) _) (C.assoc _ _ _))
  | _, _, _, σ, .pair t u =>
    C.htrans ((K.prod _ _).pair_resp (sub_den σ t) (sub_den σ u)) (C.hsymm (pair_comp _ _ _ _))
  | _, _, _, σ, .fst t => C.htrans (C.lcong (sub_den σ t) _) (C.assoc _ _ _)
  | _, _, _, σ, .snd t => C.htrans (C.lcong (sub_den σ t) _) (C.assoc _ _ _)

theorem varsub_ren : {Γ Δ : List Ty} → (r : Ren Δ Γ) →
    C.heq (denS K ι (Γ := Γ) (Δ := Δ) (fun X v => .var (r X v))) (denR K ι r)
  | _, [], _ => C.hrefl _
  | _, _ :: _, r => (K.prod _ _).pair_resp (varsub_ren (fun X v => r X (.s v))) (C.hrefl _)

theorem single_den {Γ : List Ty} {A : Ty} (u : Tm Γ A) :
    C.heq (denS K ι (single u)) ((K.prod (ctxO K ι Γ) (obj K ι A)).pair (C.id _) (den K ι u)) :=
  (K.prod _ _).pair_resp (C.htrans (varsub_ren K ι (fun _ v => v)) (id_ren K ι Γ)) (C.hrefl _)

/-- Lambek soundness: βη-convertible terms denote equivalent morphisms in every CCC -/
theorem conv_sound {Γ : List Ty} {A : Ty} {t t' : Tm Γ A} (h : Conv t t') :
    C.heq (den K ι t) (den K ι t') := by
  induction h with
  | refl t => exact C.hrefl _
  | symm _ ih => exact C.hsymm ih
  | trans _ _ ih₁ ih₂ => exact C.htrans ih₁ ih₂
  | lam _ ih => exact K.curry_resp ih
  | app _ _ ih₁ ih₂ => exact C.lcong ((K.prod _ _).pair_resp ih₁ ih₂) _
  | pair _ _ ih₁ ih₂ => exact (K.prod _ _).pair_resp ih₁ ih₂
  | fst _ ih => exact C.lcong ih _
  | snd _ ih => exact C.lcong ih _
  | @beta Γ A B t u =>
    have e1 : C.heq ((K.prod (K.exp (obj K ι A) (obj K ι B)) (obj K ι A)).pair (K.curry (den K ι t)) (den K ι u))
        (C.comp ((K.prod (ctxO K ι Γ) (obj K ι A)).pair (C.id _) (den K ι u)) (K.times (obj K ι A) (K.curry (den K ι t)))) :=
      C.hsymm (C.htrans (pair_comp _ _ _ _) ((K.prod _ _).pair_resp
        (C.htrans (C.hsymm (C.assoc _ _ _)) (C.htrans (C.lcong ((K.prod _ _).beta1 _ _) _) (C.id_comp _)))
        ((K.prod _ _).beta2 _ _)))
    exact C.htrans (C.lcong e1 _) (C.htrans (C.assoc _ _ _)
      (C.htrans (C.rcong _ (K.beta (den K ι t)))
        (C.hsymm (C.htrans (sub_den K ι (single u) t) (C.lcong (single_den K ι u) _)))))
  | @eta Γ A B t =>
    exact C.htrans (K.curry_resp (C.lcong ((K.prod _ _).pair_resp
        (C.htrans (ren_den K ι wk1 t) (C.lcong (wk1_den K ι) _)) (C.hrefl _)) _)) (K.eta (den K ι t))
  | fstβ t u => exact (K.prod _ _).beta1 _ _
  | sndβ t u => exact (K.prod _ _).beta2 _ _
  | pairη t => exact C.hsymm ((K.prod _ _).eta _)

end CHL

end Found

#print axioms Found.ND.erase
#print axioms Found.ND.renTm
#print axioms Found.ND.subTm
#print axioms Found.CHL.pair_comp
#print axioms Found.CHL.var_ren
#print axioms Found.CHL.wk_ren
#print axioms Found.CHL.lift_ren
#print axioms Found.CHL.id_ren
#print axioms Found.CHL.ren_den
#print axioms Found.CHL.wk1_den
#print axioms Found.CHL.var_sub
#print axioms Found.CHL.wk_sub
#print axioms Found.CHL.lift_sub
#print axioms Found.CHL.sub_den
#print axioms Found.CHL.varsub_ren
#print axioms Found.CHL.single_den
#print axioms Found.CHL.conv_sound

found/CIC.lean

lean_all/found/CIC.lean

/-
  T99. found/CIC — the calculus of inductive constructions (Lean itself) as
  one point among the others; we use it as the meta-language (marked on the
  map). Its distinguishing conditions, each shown here:
  * impredicative Prop (∀ over Prop stays in Prop);
  * definitional proof irrelevance (any two proofs of P are equal by rfl);
  * subsingleton (large) elimination only: Eq can transport data; this is
    the CIC fact used for UIP;
  * universes are a hierarchy Type u : Type (u+1) (T104 uses it).
  These are exactly the "CIC dependence" marks in the other files; Cubical Agda
  (predicative, proof-relevant) is the contrasting point (agda/Core.agda).

  Lean 4 core only, no axioms.
  CIC dependence: this whole file is about the properties of CIC themselves.
-/

namespace Found

namespace CICPoint

universe u

/-- impredicativity: a proposition quantifying over all propositions -/
def ImpProp : Prop := ∀ P : Prop, P → P

theorem impProp_holds : ImpProp := fun _ p => p

/-- definitional proof irrelevance -/
theorem proof_irrel (P : Prop) (p q : P) : p = q := rfl

/-- uniqueness of identity proofs follows -/
theorem uip {A : Sort u} {a b : A} (p q : a = b) : p = q := rfl

/-- subsingleton elimination: Eq transports data -/
def transport {A : Sort u} (B : A → Sort u) {a b : A} (e : a = b) (x : B a) : B b := e ▸ x

theorem transport_refl {A : Sort u} (B : A → Sort u) (a : A) (x : B a) : transport B rfl x = x := rfl

/-- the universe hierarchy -/
def tower : Type 1 := Type

end CICPoint

end Found

#print axioms Found.CICPoint.impProp_holds
#print axioms Found.CICPoint.proof_irrel
#print axioms Found.CICPoint.uip
#print axioms Found.CICPoint.transport_refl

found/CZF.lean

lean_all/found/CZF.lean

/-
  T88, line IZF -> CZF. found/CZF — Aczel's interpretation of constructive
  Zermelo–Fraenkel set theory CZF in type theory, predicatively: the tree
  universe V (W-type over a universe) with equality and membership valued
  in types (Σ, Π, ×, ⊕), never in the impredicative Prop.
  Verified in V:
  * equality is an equivalence, membership respects it;
  * extensionality, ∈-induction (for type-valued properties);
  * empty set, pairing, union, infinity (ω with ∅ and successor);
  * separation for type-valued (small) properties;
  * strong collection;
  * subset collection (the image sets of all functions a → b form the
    collecting set), the CZF replacement of the power set axiom.
  Choice in the metatheory is the projection of Σ (type-theoretic choice).

  Lean 4 core only, hand-written proofs, no axioms.
-/

universe u

namespace Found

namespace CZF

inductive V : Type (u + 1) where
  | sup (A : Type u) (f : A → V) : V

def V.I : V.{u} → Type u
  | .sup A _ => A

def V.f : (x : V.{u}) → x.I → V.{u}
  | .sup _ f => f

/-- type-valued bisimilarity -/
def EqT : V.{u} → V.{u} → Type u
  | .sup A f, .sup B g => (∀ a : A, (b : B) × EqT (f a) (g b)) × (∀ b : B, (a : A) × EqT (f a) (g b))

def MemT (x : V.{u}) : V.{u} → Type u
  | .sup A f => (a : A) × EqT x (f a)

def eq_refl : (x : V.{u}) → EqT x x
  | .sup _ f => (fun a => ⟨a, eq_refl (f a)⟩, fun a => ⟨a, eq_refl (f a)⟩)

def eq_symm : (x y : V.{u}) → EqT x y → EqT y x
  | .sup _ f, .sup _ g, e =>
    (fun b => ⟨(e.2 b).1, eq_symm (f (e.2 b).1) (g b) (e.2 b).2⟩,
     fun a => ⟨(e.1 a).1, eq_symm (f a) (g (e.1 a).1) (e.1 a).2⟩)

def eq_trans : (x y z : V.{u}) → EqT x y → EqT y z → EqT x z
  | .sup _ f, .sup _ g, .sup _ h, p, q =>
    (fun a => ⟨(q.1 (p.1 a).1).1, eq_trans (f a) (g (p.1 a).1) (h (q.1 (p.1 a).1).1) (p.1 a).2 (q.1 (p.1 a).1).2⟩,
     fun c => ⟨(p.2 (q.2 c).1).1, eq_trans (f (p.2 (q.2 c).1).1) (g (q.2 c).1) (h c) (p.2 (q.2 c).1).2 (q.2 c).2⟩)

def mem_resp_l {x x' : V.{u}} (e : EqT x x') : (y : V.{u}) → MemT x y → MemT x' y
  | .sup _ f, m => ⟨m.1, eq_trans x' x (f m.1) (eq_symm x x' e) m.2⟩

def mem_resp_r (x : V.{u}) : (y y' : V.{u}) → EqT y y' → MemT x y → MemT x y'
  | .sup _ f, .sup _ g, e, m => ⟨(e.1 m.1).1, eq_trans x (f m.1) (g (e.1 m.1).1) m.2 (e.1 m.1).2⟩

/-- the elements of a set are its members -/
def mem_f : (y : V.{u}) → (i : y.I) → MemT (y.f i) y
  | .sup _ f, i => ⟨i, eq_refl (f i)⟩

/-! ## extensionality and ∈-induction -/

def ext : (x y : V.{u}) → (∀ z, MemT z x → MemT z y) → (∀ z, MemT z y → MemT z x) → EqT x y
  | .sup _ f, .sup _ g, h1, h2 =>
    (fun a => match h1 (f a) ⟨a, eq_refl (f a)⟩ with
      | ⟨b, e⟩ => ⟨b, e⟩,
     fun b => match h2 (g b) ⟨b, eq_refl (g b)⟩ with
      | ⟨a, e⟩ => ⟨a, eq_symm (g b) (f a) e⟩)

def ind {P : V.{u} → Sort _} (resp : ∀ x y, EqT x y → P x → P y)
    (step : ∀ x, (∀ y, MemT y x → P y) → P x) : (x : V.{u}) → P x
  | .sup A f => step (.sup A f) (fun y m => resp (f m.1) y (eq_symm y (f m.1) m.2) (ind resp step (f m.1)))

/-! ## constructions -/

def empty : V.{u} := .sup PEmpty.{u + 1} (fun e => nomatch e)

def empty_spec (x : V.{u}) : MemT x empty → PEmpty.{1} := fun m => nomatch m.1

def pair (a b : V.{u}) : V.{u} := .sup (ULift.{u} Bool) (fun t => match t.down with | true => a | false => b)

def pair_in (x a b : V.{u}) : MemT x (pair a b) → EqT x a ⊕ EqT x b := fun ⟨t, e⟩ =>
  match t, e with
  | ⟨true⟩, e => .inl e
  | ⟨false⟩, e => .inr e

def pair_out (x a b : V.{u}) : EqT x a ⊕ EqT x b → MemT x (pair a b)
  | .inl e => ⟨⟨true⟩, e⟩
  | .inr e => ⟨⟨false⟩, e⟩

def union (a : V.{u}) : V.{u} := .sup ((i : a.I) × (a.f i).I) (fun p => (a.f p.1).f p.2)

def union_in (x a : V.{u}) (m : MemT x (union a)) : (y : V.{u}) × MemT y a × MemT x y :=
  match a, m with
  | .sup _ g, ⟨⟨i, j⟩, e⟩ => ⟨g i, mem_f (.sup _ g) i, mem_resp_l (eq_symm _ _ e) (g i) (mem_f (g i) j)⟩

def mem_idx (x : V.{u}) : (y : V.{u}) → MemT x y → (j : y.I) × EqT x (y.f j)
  | .sup _ _, m => m

def union_out (x a y : V.{u}) (my : MemT y a) (mx : MemT x y) : MemT x (union a) :=
  match a, my with
  | .sup _ g, ⟨i, e⟩ =>
    let m := mem_idx x (g i) (mem_resp_r x y (g i) e mx)
    ⟨⟨i, m.1⟩, m.2⟩

def succ (x : V.{u}) : V.{u} := union (pair x (pair x x))

def natV : Nat → V.{u}
  | 0 => empty
  | n + 1 => succ (natV n)

def omega : V.{u} := .sup (ULift.{u} Nat) (fun n => natV n.down)

def infinity_zero : MemT empty.{u} omega := ⟨⟨0⟩, eq_refl _⟩

def infinity_succ (n : Nat) : MemT (succ (natV.{u} n)) omega := ⟨⟨n + 1⟩, eq_refl _⟩

/-- separation for small type-valued properties -/
def sep (a : V.{u}) (P : V.{u} → Type u) : V.{u} := .sup ((i : a.I) × P (a.f i)) (fun p => a.f p.1)

def sep_in (P : V.{u} → Type u) (resp : ∀ x y, EqT x y → P x → P y) (x : V.{u}) :
    (a : V.{u}) → MemT x (sep a P) → MemT x a × P x
  | .sup _ g, ⟨⟨i, p⟩, e⟩ => (⟨i, e⟩, resp (g i) x (eq_symm x (g i) e) p)

def sep_out (P : V.{u} → Type u) (resp : ∀ x y, EqT x y → P x → P y) (x : V.{u}) :
    (a : V.{u}) → MemT x a → P x → MemT x (sep a P)
  | .sup _ g, ⟨i, e⟩, p => ⟨⟨i, resp x (g i) e p⟩, e⟩

/-! ## collection -/

/-- a relation respecting equality on both sides -/
structure Resp2 (φ : V.{u} → V.{u} → Type u) where
  rx : ∀ x x' y, EqT x x' → φ x y → φ x' y
  ry : ∀ x y y', EqT y y' → φ x y → φ x y'

/-- strong collection -/
def strong_collection (φ : V.{u} → V.{u} → Type u) (r : Resp2 φ) :
    (a : V.{u}) → (F : ∀ x, MemT x a → (y : V.{u}) × φ x y) →
    (b : V.{u}) × (∀ x, MemT x a → (y : V.{u}) × MemT y b × φ x y) ×
      (∀ y, MemT y b → (x : V.{u}) × MemT x a × φ x y)
  | .sup A h, F =>
    ⟨.sup A (fun i => (F (h i) ⟨i, eq_refl (h i)⟩).1),
     fun x m => ⟨(F (h m.1) ⟨m.1, eq_refl _⟩).1, ⟨m.1, eq_refl _⟩,
       r.rx (h m.1) x _ (eq_symm x (h m.1) m.2) (F (h m.1) ⟨m.1, eq_refl _⟩).2⟩,
     fun y m => ⟨h m.1, ⟨m.1, eq_refl _⟩,
       r.ry (h m.1) _ y (eq_symm y _ m.2) (F (h m.1) ⟨m.1, eq_refl _⟩).2⟩⟩

/-- subset collection: one set c collects, for every total relation from a to b,
  a subset of b on which the relation is total in both directions -/
def subset_collection : (a b : V.{u}) → (c : V.{u}) ×
    (∀ (φ : V.{u} → V.{u} → Type u), Resp2 φ → (∀ x, MemT x a → (y : V.{u}) × MemT y b × φ x y) →
      (d : V.{u}) × MemT d c × (∀ x, MemT x a → (y : V.{u}) × MemT y d × φ x y) ×
        (∀ y, MemT y d → (x : V.{u}) × MemT x a × φ x y))
  | .sup A h, .sup B k =>
    ⟨.sup (A → B) (fun g => .sup A (fun i => k (g i))), fun φ r H =>
      let g : A → B := fun i => (H (h i) ⟨i, eq_refl _⟩).2.1.1
      let yi : (i : A) → φ (h i) (k (g i)) := fun i =>
        r.ry (h i) _ (k (g i)) (H (h i) ⟨i, eq_refl _⟩).2.1.2 (H (h i) ⟨i, eq_refl _⟩).2.2
      ⟨.sup A (fun i => k (g i)), ⟨g, eq_refl _⟩,
       fun x m => ⟨k (g m.1), ⟨m.1, eq_refl _⟩, r.rx (h m.1) x _ (eq_symm x (h m.1) m.2) (yi m.1)⟩,
       fun y m => ⟨h m.1, ⟨m.1, eq_refl _⟩, r.ry (h m.1) _ y (eq_symm y _ m.2) (yi m.1)⟩⟩⟩

end CZF

end Found

#print axioms Found.CZF.eq_refl
#print axioms Found.CZF.eq_symm
#print axioms Found.CZF.eq_trans
#print axioms Found.CZF.mem_resp_l
#print axioms Found.CZF.mem_resp_r
#print axioms Found.CZF.ext
#print axioms Found.CZF.ind
#print axioms Found.CZF.pair_in
#print axioms Found.CZF.pair_out
#print axioms Found.CZF.union_in
#print axioms Found.CZF.union_out
#print axioms Found.CZF.infinity_succ
#print axioms Found.CZF.sep_in
#print axioms Found.CZF.sep_out
#print axioms Found.CZF.strong_collection
#print axioms Found.CZF.subset_collection

found/Cardinal.lean

lean_all/found/Cardinal.lean

/-
  T106 (b), slice N3a. found/Cardinal — in the classical point:
  * the strict order of the Zermelo well-order is well-founded (recursion
    along it is WellFounded.fix);
  * cardinal comparability: for any two types one injects into the other
    (transfinite recursion assigning to each x the first unused element).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.WellOrder

namespace Found

namespace Card

open CP WO

variable (K : Classical')

def Lt' {α : Type} (x y : α) : Prop := Le K x y ∧ ¬ Le K y x

theorem wf (α : Type) : WellFounded (Lt' K (α := α)) := by
  refine ⟨fun x => dne K _ (fun hx => ?_)⟩
  match least K (fun z => ¬ Acc (Lt' K) z) ⟨x, hx⟩ with
  | ⟨m, hm, hl⟩ =>
    exact hm (.intro m (fun y hy => dne K _ (fun hny => hy.2 (hl y hny))))

def Inj {α β : Type} (f : α → β) : Prop := ∀ x y, f x = f y → x = y

section Comp
variable {α β : Type}

def isSomeB : Option β → Bool
  | some _ => true
  | none => false

theorem some_ne_none {b : β} (e : some b = none) : False := Bool.noConfusion (congrArg isSomeB e)

def Used (x : α) (rec : ∀ y, Lt' K y x → Option β) (b : β) : Prop := ∃ y, ∃ h : Lt' K y x, rec y h = some b

def Good (x : α) (rec : ∀ y, Lt' K y x → Option β) (o : Option β) : Prop :=
  (o = none ∧ ∀ b, Used K x rec b) ∨ (∃ b, o = some b ∧ ¬ Used K x rec b)

theorem good_ex (x : α) (rec : ∀ y, Lt' K y x → Option β) : ∃ o, Good K x rec o :=
  (K.lem (∀ b, Used K x rec b)).elim (fun h => ⟨none, Or.inl ⟨rfl, h⟩⟩)
    (fun h => dne K _ (fun hn => h (fun b => dne K _ (fun hb => hn ⟨some b, Or.inr ⟨b, rfl, hb⟩⟩))))

def step (x : α) (rec : ∀ y, Lt' K y x → Option β) : Option β := K.ch (Good K x rec) (good_ex K x rec)

def F : α → Option β := (wf K α).fix (step K)

theorem F_good (x : α) : Good K x (fun y _ => F K y) (F K (β := β) x) := by
  have e : F K (β := β) x = step K x (fun y _ => F K y) := (wf K α).fix_eq (step K) x
  rw [e]
  exact K.ch_spec _ _

theorem F_inj (x x' : α) (b : β) (h : F K x = some b) (h' : F K x' = some b) : x = x' := by
  cases le_total K x x' with
  | inl hle =>
    exact dne K _ (fun hne => by
      have hlt : Lt' K x x' := ⟨hle, fun hge => hne (le_antisymm K x x' hle hge)⟩
      rcases F_good K x' with ⟨e, _⟩ | ⟨b', e, hu⟩
      · rw [h'] at e; exact (some_ne_none e).elim
      · rw [h'] at e
        have : b = b' := Option.some.inj e
        subst this
        exact hu ⟨x, hlt, h⟩)
  | inr hle =>
    exact dne K _ (fun hne => by
      have hlt : Lt' K x' x := ⟨hle, fun hge => hne (le_antisymm K x x' hge hle)⟩
      rcases F_good K x with ⟨e, _⟩ | ⟨b', e, hu⟩
      · rw [h] at e; exact (some_ne_none e).elim
      · rw [h] at e
        have : b = b' := Option.some.inj e
        subst this
        exact hu ⟨x', hlt, h'⟩)

include K in
/-- cardinal comparability: one of the two types injects into the other -/
theorem comparable : (∃ f : α → β, Inj f) ∨ (∃ g : β → α, Inj g) :=
  (K.lem (∀ x : α, ∃ b, F K (β := β) x = some b)).elim
    (fun hall => Or.inl ⟨fun x => K.ch (fun b => F K x = some b) (hall x), fun x y e => by
      have hx := K.ch_spec (fun b => F K (β := β) x = some b) (hall x)
      have hy := K.ch_spec (fun b => F K (β := β) y = some b) (hall y)
      have e' : K.ch (fun b => F K (β := β) x = some b) (hall x) = K.ch (fun b => F K (β := β) y = some b) (hall y) := e
      rw [e'] at hx
      exact F_inj K x y _ hx hy⟩)
    (fun hn => by
      have hx0 : ∃ x0, F K (β := β) x0 = none := dne K _ (fun hno => hn (fun x =>
        match h : F K (β := β) x with
        | some b => ⟨b, rfl⟩
        | none => (hno ⟨x, h⟩).elim))
      match hx0 with
      | ⟨x0, h0⟩ =>
        have hused : ∀ b : β, Used K x0 (fun y _ => F K y) b := by
          rcases F_good K (β := β) x0 with ⟨_, hu⟩ | ⟨b, e, _⟩
          · exact hu
          · rw [h0] at e; exact (some_ne_none e.symm).elim
        refine Or.inr ⟨fun b => K.ch (fun y => F K (β := β) y = some b)
          (match hused b with | ⟨y, _, e⟩ => ⟨y, e⟩), fun b b' e => ?_⟩
        have h1 := K.ch_spec (fun y => F K (β := β) y = some b) (match hused b with | ⟨y, _, e⟩ => ⟨y, e⟩)
        have h2 := K.ch_spec (fun y => F K (β := β) y = some b') (match hused b' with | ⟨y, _, e⟩ => ⟨y, e⟩)
        have e' : K.ch (fun y => F K (β := β) y = some b) (match hused b with | ⟨y, _, e⟩ => ⟨y, e⟩) =
            K.ch (fun y => F K (β := β) y = some b') (match hused b' with | ⟨y, _, e⟩ => ⟨y, e⟩) := e
        rw [e'] at h1
        exact Option.some.inj (h1.symm.trans h2))

end Comp

end Card

end Found

#print axioms Found.Card.wf
#print axioms Found.Card.F_inj
#print axioms Found.Card.comparable

found/Cat.lean

lean_all/found/Cat.lean

/-
  T61. found/Cat — category theory inside the frame (setoid hom-sets).

  * identity and composite functors, natural transformations, vertical composite
  * setoid-valued presheaves, representables, the Yoneda lemma, Yoneda is
    full (every natural map between representables is composition with a map)
  * terminal objects (unique up to iso), binary products; Fun has both
  * adjunctions in hom-set form: unit, counit, triangle identities
  * Kleisli monads; every adjunction gives a monad; Option is a monad on Fun

  Lean 4 core only, hand-written proofs, no axioms.

  CIC dependence: definitional structure eta (Prod, PUnit: the rfl proofs in funProduct, funTerminal).
-/
import found.Base

namespace Found

universe u v u' v' u'' v'' w

namespace ECat
variable (C : ECat.{u, v})

theorem lcong {a b c : C.Ob} {f f' : C.Hom a b} (h : C.heq f f') (g : C.Hom b c) :
    C.heq (C.comp f g) (C.comp f' g) := C.comp_resp h (C.hrefl g)

theorem rcong {a b c : C.Ob} (f : C.Hom a b) {g g' : C.Hom b c} (h : C.heq g g') :
    C.heq (C.comp f g) (C.comp f g') := C.comp_resp (C.hrefl f) h

/-- the hom-set of `a` and `b` as a setoid -/
def homS (a b : C.Ob) : Setd.{v} :=
  ⟨C.Hom a b, C.heq, C.hrefl, fun h => C.hsymm h, fun h k => C.htrans h k⟩

end ECat

/-! ## Functors and natural transformations -/

def EFun.idF (C : ECat.{u, v}) : EFun C C where
  obj a := a
  map f := f
  map_resp h := h
  map_id a := C.hrefl (C.id a)
  map_comp f g := C.hrefl (C.comp f g)

def EFun.compF {C : ECat.{u, v}} {D : ECat.{u', v'}} {E : ECat.{u'', v''}}
    (F : EFun C D) (G : EFun D E) : EFun C E where
  obj a := G.obj (F.obj a)
  map f := G.map (F.map f)
  map_resp h := G.map_resp (F.map_resp h)
  map_id a := E.htrans (G.map_resp (F.map_id a)) (G.map_id _)
  map_comp f g := E.htrans (G.map_resp (F.map_comp f g)) (G.map_comp _ _)

structure NatTr {C : ECat.{u, v}} {D : ECat.{u', v'}} (F G : EFun C D) where
  app : (a : C.Ob) → D.Hom (F.obj a) (G.obj a)
  nat : ∀ {a b} (f : C.Hom a b),
    D.heq (D.comp (F.map f) (app b)) (D.comp (app a) (G.map f))

section NT
variable {C : ECat.{u, v}} {D : ECat.{u', v'}}

def NatTr.idT (F : EFun C D) : NatTr F F where
  app a := D.id (F.obj a)
  nat f := D.htrans (D.comp_id (F.map f)) (D.hsymm (D.id_comp (F.map f)))

def NatTr.vcomp {F G H : EFun C D} (α : NatTr F G) (β : NatTr G H) : NatTr F H where
  app a := D.comp (α.app a) (β.app a)
  nat {a b} f :=
    D.htrans (D.hsymm (D.assoc (F.map f) (α.app b) (β.app b)))
    (D.htrans (D.lcong (α.nat f) (β.app b))
    (D.htrans (D.assoc (α.app a) (G.map f) (β.app b))
    (D.htrans (D.rcong (α.app a) (β.nat f))
      (D.hsymm (D.assoc (α.app a) (β.app a) (H.map f))))))

theorem NatTr.idT_vcomp {F G : EFun C D} (α : NatTr F G) (a : C.Ob) :
    D.heq ((NatTr.vcomp (NatTr.idT F) α).app a) (α.app a) := D.id_comp (α.app a)

theorem NatTr.vcomp_assoc {F G H K : EFun C D} (α : NatTr F G) (β : NatTr G H)
    (γ : NatTr H K) (a : C.Ob) :
    D.heq ((NatTr.vcomp (NatTr.vcomp α β) γ).app a) ((NatTr.vcomp α (NatTr.vcomp β γ)).app a) :=
  D.assoc _ _ _

end NT

/-! ## Presheaves of setoids and the Yoneda lemma -/

structure Psh (C : ECat.{u, v}) where
  obj : C.Ob → Setd.{w}
  map : {a b : C.Ob} → C.Hom a b → (obj b).car → (obj a).car
  map_eqv : ∀ {a b} (f : C.Hom a b) {x y}, (obj b).eqv x y → (obj a).eqv (map f x) (map f y)
  map_resp : ∀ {a b} {f g : C.Hom a b} (x : (obj b).car), C.heq f g → (obj a).eqv (map f x) (map g x)
  map_id : ∀ a x, (obj a).eqv (map (C.id a) x) x
  map_comp : ∀ {a b c} (f : C.Hom a b) (g : C.Hom b c) x,
    (obj a).eqv (map (C.comp f g) x) (map f (map g x))

structure PNat {C : ECat.{u, v}} (P Q : Psh.{u, v, w} C) where
  app : (a : C.Ob) → (P.obj a).car → (Q.obj a).car
  app_eqv : ∀ a {x y}, (P.obj a).eqv x y → (Q.obj a).eqv (app a x) (app a y)
  nat : ∀ {a b} (f : C.Hom a b) x, (Q.obj a).eqv (app a (P.map f x)) (Q.map f (app b x))

section Yoneda
variable {C : ECat.{u, v}}

/-- the representable presheaf `Hom(-, c)` -/
def yo (C : ECat.{u, v}) (c : C.Ob) : Psh.{u, v, v} C where
  obj a := C.homS a c
  map f g := C.comp f g
  map_eqv f _ _ h := C.rcong f h
  map_resp x h := C.lcong h x
  map_id _ x := C.id_comp x
  map_comp f g x := C.assoc f g x

variable {c : C.Ob} {P : Psh.{u, v, v} C}

def yoFwd (α : PNat (yo C c) P) : (P.obj c).car := α.app c (C.id c)

def yoBwd (x : (P.obj c).car) : PNat (yo C c) P where
  app _ g := P.map g x
  app_eqv _ _ _ h := P.map_resp x h
  nat f g := P.map_comp f g x

/-- Yoneda, one way round -/
theorem yoneda_fwd_bwd (x : (P.obj c).car) : (P.obj c).eqv (yoFwd (yoBwd (P := P) x)) x :=
  P.map_id c x

/-- Yoneda, the other way round -/
theorem yoneda_bwd_fwd (α : PNat (yo C c) P) (a : C.Ob) (g : C.Hom a c) :
    (P.obj a).eqv ((yoBwd (yoFwd α)).app a g) (α.app a g) :=
  (P.obj a).trans ((P.obj a).symm (α.nat g (C.id c))) (α.app_eqv a (C.comp_id g))

theorem yoFwd_resp {α β : PNat (yo C c) P}
    (h : ∀ a g, (P.obj a).eqv (α.app a g) (β.app a g)) :
    (P.obj c).eqv (yoFwd α) (yoFwd β) := h c (C.id c)

theorem yoBwd_resp {x y : (P.obj c).car} (h : (P.obj c).eqv x y) (a : C.Ob) (g : C.Hom a c) :
    (P.obj a).eqv ((yoBwd (P := P) x).app a g) ((yoBwd (P := P) y).app a g) :=
  P.map_eqv g h

/-- the Yoneda embedding is full -/
theorem yoneda_full {a c : C.Ob} (α : PNat (yo C a) (yo C c)) :
    ∃ f : C.Hom a c, ∀ b (g : C.Hom b a), C.heq (α.app b g) (C.comp g f) :=
  ⟨yoFwd α, fun b g => C.hsymm (yoneda_bwd_fwd α b g)⟩

/-- the Yoneda embedding is faithful -/
theorem yoneda_faithful {a c : C.Ob} (f f' : C.Hom a c)
    (h : ∀ b (g : C.Hom b a), C.heq (C.comp g f) (C.comp g f')) : C.heq f f' :=
  C.htrans (C.hsymm (C.id_comp f)) (C.htrans (h a (C.id a)) (C.id_comp f'))

end Yoneda

/-! ## Limits: terminal objects and binary products -/

structure Terminal (C : ECat.{u, v}) where
  one : C.Ob
  bang : (a : C.Ob) → C.Hom a one
  uniq : ∀ {a} (f : C.Hom a one), C.heq f (bang a)

theorem Terminal.unique_iso {C : ECat.{u, v}} (T S : Terminal C) :
    C.heq (C.comp (S.bang T.one) (T.bang S.one)) (C.id T.one) :=
  C.htrans (T.uniq _) (C.hsymm (T.uniq (C.id T.one)))

structure Product (C : ECat.{u, v}) (a b : C.Ob) where
  P : C.Ob
  p1 : C.Hom P a
  p2 : C.Hom P b
  pair : {x : C.Ob} → C.Hom x a → C.Hom x b → C.Hom x P
  beta1 : ∀ {x} (f : C.Hom x a) (g : C.Hom x b), C.heq (C.comp (pair f g) p1) f
  beta2 : ∀ {x} (f : C.Hom x a) (g : C.Hom x b), C.heq (C.comp (pair f g) p2) g
  eta : ∀ {x} (h : C.Hom x P), C.heq h (pair (C.comp h p1) (C.comp h p2))
  pair_resp : ∀ {x} {f f' : C.Hom x a} {g g' : C.Hom x b},
    C.heq f f' → C.heq g g' → C.heq (pair f g) (pair f' g')

/-- two products of the same pair are isomorphic -/
theorem Product.unique_iso {C : ECat.{u, v}} {a b : C.Ob} (X Y : Product C a b) :
    C.heq (C.comp (Y.pair X.p1 X.p2) (X.pair Y.p1 Y.p2)) (C.id X.P) := by
  have k1 : C.heq (C.comp (C.comp (Y.pair X.p1 X.p2) (X.pair Y.p1 Y.p2)) X.p1) X.p1 :=
    C.htrans (C.assoc _ _ _) (C.htrans (C.rcong _ (X.beta1 _ _)) (Y.beta1 _ _))
  have k2 : C.heq (C.comp (C.comp (Y.pair X.p1 X.p2) (X.pair Y.p1 Y.p2)) X.p2) X.p2 :=
    C.htrans (C.assoc _ _ _) (C.htrans (C.rcong _ (X.beta2 _ _)) (Y.beta2 _ _))
  have e1 := X.eta (C.comp (Y.pair X.p1 X.p2) (X.pair Y.p1 Y.p2))
  have e2 := X.eta (C.id X.P)
  exact C.htrans e1 (C.htrans (X.pair_resp k1 k2)
    (C.hsymm (C.htrans e2 (X.pair_resp (C.id_comp X.p1) (C.id_comp X.p2)))))

def funTerminal : Terminal FunCat.{u} where
  one := PUnit
  bang _ := fun _ => PUnit.unit
  uniq _ _ := rfl

def funProduct (A B : Type u) : Product FunCat.{u} A B where
  P := A × B
  p1 := Prod.fst
  p2 := Prod.snd
  pair f g := fun x => (f x, g x)
  beta1 _ _ _ := rfl
  beta2 _ _ _ := rfl
  eta _ _ := rfl
  pair_resp hf hg x := join (ap (fun y => (y, _)) (hf x)) (ap (fun z => (_, z)) (hg x))

/-! ## Adjunctions -/

structure Adj {C : ECat.{u, v}} {D : ECat.{u', v'}} (F : EFun C D) (G : EFun D C) where
  phi : {a : C.Ob} → {b : D.Ob} → D.Hom (F.obj a) b → C.Hom a (G.obj b)
  psi : {a : C.Ob} → {b : D.Ob} → C.Hom a (G.obj b) → D.Hom (F.obj a) b
  phi_resp : ∀ {a b} {k k' : D.Hom (F.obj a) b}, D.heq k k' → C.heq (phi k) (phi k')
  psi_resp : ∀ {a b} {g g' : C.Hom a (G.obj b)}, C.heq g g' → D.heq (psi g) (psi g')
  phi_psi : ∀ {a b} (g : C.Hom a (G.obj b)), C.heq (phi (psi g)) g
  psi_phi : ∀ {a b} (k : D.Hom (F.obj a) b), D.heq (psi (phi k)) k
  nat_l : ∀ {a a' b} (f : C.Hom a' a) (k : D.Hom (F.obj a) b),
    C.heq (phi (D.comp (F.map f) k)) (C.comp f (phi k))
  nat_r : ∀ {a b b'} (k : D.Hom (F.obj a) b) (g : D.Hom b b'),
    C.heq (phi (D.comp k g)) (C.comp (phi k) (G.map g))

namespace Adj
variable {C : ECat.{u, v}} {D : ECat.{u', v'}} {F : EFun C D} {G : EFun D C} (A : Adj F G)

def unit (a : C.Ob) : C.Hom a (G.obj (F.obj a)) := A.phi (D.id (F.obj a))
def counit (b : D.Ob) : D.Hom (F.obj (G.obj b)) b := A.psi (C.id (G.obj b))

theorem phi_eq {a b} (k : D.Hom (F.obj a) b) : C.heq (A.phi k) (C.comp (A.unit a) (G.map k)) :=
  C.htrans (A.phi_resp (D.hsymm (D.id_comp k))) (A.nat_r _ k)

theorem psi_nat_l {a a' b} (f : C.Hom a' a) (h : C.Hom a (G.obj b)) :
    D.heq (A.psi (C.comp f h)) (D.comp (F.map f) (A.psi h)) := by
  have e : C.heq (A.phi (D.comp (F.map f) (A.psi h))) (C.comp f h) :=
    C.htrans (A.nat_l f (A.psi h)) (C.rcong f (A.phi_psi h))
  exact D.htrans (A.psi_resp (C.hsymm e)) (A.psi_phi _)

theorem psi_nat_r {a b b'} (g : C.Hom a (G.obj b)) (k : D.Hom b b') :
    D.heq (A.psi (C.comp g (G.map k))) (D.comp (A.psi g) k) := by
  have e : C.heq (A.phi (D.comp (A.psi g) k)) (C.comp g (G.map k)) :=
    C.htrans (A.nat_r (A.psi g) k) (C.lcong (A.phi_psi g) (G.map k))
  exact D.htrans (A.psi_resp (C.hsymm e)) (A.psi_phi _)

theorem psi_eq {a b} (g : C.Hom a (G.obj b)) : D.heq (A.psi g) (D.comp (F.map g) (A.counit b)) :=
  D.htrans (A.psi_resp (C.hsymm (C.comp_id g))) (A.psi_nat_l g _)

/-- first triangle identity -/
theorem triangle_F (a : C.Ob) :
    D.heq (D.comp (F.map (A.unit a)) (A.counit (F.obj a))) (D.id (F.obj a)) :=
  D.htrans (D.hsymm (A.psi_eq (A.unit a))) (A.psi_phi _)

/-- second triangle identity -/
theorem triangle_G (b : D.Ob) :
    C.heq (C.comp (A.unit (G.obj b)) (G.map (A.counit b))) (C.id (G.obj b)) :=
  C.htrans (C.hsymm (A.phi_eq (A.counit b))) (A.phi_psi _)

end Adj

/-! ## Monads -/

structure KMonad (C : ECat.{u, v}) where
  T : C.Ob → C.Ob
  ret : (a : C.Ob) → C.Hom a (T a)
  bind : {a b : C.Ob} → C.Hom a (T b) → C.Hom (T a) (T b)
  bind_resp : ∀ {a b} {k k' : C.Hom a (T b)}, C.heq k k' → C.heq (bind k) (bind k')
  left : ∀ {a b} (k : C.Hom a (T b)), C.heq (C.comp (ret a) (bind k)) k
  right : ∀ a, C.heq (bind (ret a)) (C.id (T a))
  assoc : ∀ {a b c} (k : C.Hom a (T b)) (l : C.Hom b (T c)),
    C.heq (C.comp (bind k) (bind l)) (bind (C.comp k (bind l)))

/-- every adjunction gives a monad `G ∘ F` -/
def Adj.monad {C : ECat.{u, v}} {D : ECat.{u', v'}} {F : EFun C D} {G : EFun D C}
    (A : Adj F G) : KMonad C where
  T a := G.obj (F.obj a)
  ret := A.unit
  bind k := G.map (A.psi k)
  bind_resp h := G.map_resp (A.psi_resp h)
  left k := C.htrans (C.hsymm (A.phi_eq (A.psi k))) (A.phi_psi k)
  right _ := C.htrans (G.map_resp (A.psi_phi _)) (G.map_id _)
  assoc k l := C.htrans (C.hsymm (G.map_comp _ _)) (G.map_resp (D.hsymm (A.psi_nat_r k (A.psi l))))

def optionBind {A B : Type u} (k : A → Option B) : Option A → Option B
  | none => none
  | some a => k a

/-- `Option` is a monad on the category of functions -/
def optionMonad : KMonad FunCat.{u} where
  T := Option
  ret _ := some
  bind := optionBind
  bind_resp h x := by
    cases x with
    | none => rfl
    | some a => exact h a
  left _ _ := rfl
  right _ x := by
    cases x with
    | none => rfl
    | some _ => rfl
  assoc _ _ x := by
    cases x with
    | none => rfl
    | some _ => rfl

end Found

#print axioms Found.EFun.idF
#print axioms Found.EFun.compF
#print axioms Found.NatTr.idT
#print axioms Found.NatTr.vcomp
#print axioms Found.NatTr.idT_vcomp
#print axioms Found.NatTr.vcomp_assoc
#print axioms Found.yo
#print axioms Found.yoneda_fwd_bwd
#print axioms Found.yoneda_bwd_fwd
#print axioms Found.yoFwd_resp
#print axioms Found.yoBwd_resp
#print axioms Found.yoneda_full
#print axioms Found.yoneda_faithful
#print axioms Found.Terminal.unique_iso
#print axioms Found.Product.unique_iso
#print axioms Found.funTerminal
#print axioms Found.funProduct
#print axioms Found.Adj.phi_eq
#print axioms Found.Adj.psi_nat_l
#print axioms Found.Adj.psi_nat_r
#print axioms Found.Adj.psi_eq
#print axioms Found.Adj.triangle_F
#print axioms Found.Adj.triangle_G
#print axioms Found.Adj.monad
#print axioms Found.optionMonad

found/ChurchNum.lean

lean_all/found/ChurchNum.lean

/-
  T207 slice L1a. found/ChurchNum — Church numerals in the untyped
  λ-calculus (T78): the line arithmetic (ℕ, T86) → λ.
  * church k = λf.λx. fᵏ x;  succ = λn.λf.λx. f (n f x).
  * For every k: succ (church k) →β* church (k+1).
  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Lambda

namespace Found

namespace UL

def iterApp {Γ : List Unit} (f x : T Γ) : Nat → T Γ
  | 0 => x
  | k + 1 => .app f (iterApp f x k)

theorem sub_iter {Γ Δ : List Unit} (σ : V Γ → T Δ) (f x : T Γ) :
    ∀ k, sub σ (iterApp f x k) = iterApp (sub σ f) (sub σ x) k
  | 0 => rfl
  | k + 1 => congrArg (T.app (sub σ f)) (sub_iter σ f x k)

theorem ren_iter {Γ Δ : List Unit} (r : V Γ → V Δ) (f x : T Γ) :
    ∀ k, ren r (iterApp f x k) = iterApp (ren r f) (ren r x) k
  | 0 => rfl
  | k + 1 => congrArg (T.app (ren r f)) (ren_iter r f x k)

def church {Γ : List Unit} (k : Nat) : T Γ := .lam (.lam (iterApp (.var (.s .z)) (.var .z) k))

theorem ren_church {Γ Δ : List Unit} (r : V Γ → V Δ) (k : Nat) : ren r (church k) = church k :=
  congrArg T.lam (congrArg T.lam (ren_iter _ _ _ k))

def succT {Γ : List Unit} : T Γ :=
  .lam (.lam (.lam (.app (.var (.s .z)) (.app (.app (.var (.s (.s .z))) (.var (.s .z))) (.var .z)))))

theorem star_lam {Γ : List Unit} {a b : T (() :: Γ)} (h : Star Beta a b) : Star Beta (.lam a) (.lam b) :=
  Star.map T.lam (fun r => .lam r) h

theorem star_appR {Γ : List Unit} (f : T Γ) {a b : T Γ} (h : Star Beta a b) : Star Beta (.app f a) (.app f b) :=
  Star.map (T.app f) (fun r => .appR f r) h

/-- applying a Church numeral to f and x unfolds to fᵏ x -/
theorem church_app {Γ : List Unit} (k : Nat) (f x : T Γ) :
    Star Beta (.app (.app (church k) f) x) (iterApp f x k) := by
  have s1 : Beta (.app (church k) f) (inst (.lam (iterApp (.var (.s .z)) (.var .z) k)) f) := .beta _ _
  have e1 : inst (.lam (iterApp (.var (.s .z)) (.var .z) k)) f = .lam (iterApp (ren V.s f) (.var .z) k) := by
    show T.lam (sub (liftS (single f)) (iterApp (.var (.s .z)) (.var .z) k)) = _
    rw [sub_iter]
    rfl
  have s2 : Beta (.app (.lam (iterApp (ren V.s f) (.var .z) k)) x)
      (inst (iterApp (ren V.s f) (.var .z) k) x) := .beta _ _
  have e2 : inst (iterApp (ren V.s f) (.var .z) k) x = iterApp f x k := by
    show sub (single x) (iterApp (ren V.s f) (.var .z) k) = _
    rw [sub_iter, sub_ren]
    have hf : sub (fun v => single x (V.s v)) f = f :=
      (sub_ext f (τ := T.var) (fun _ => rfl)).trans (sub_var f)
    rw [hf]
    rfl
  rw [e1] at s1
  rw [e2] at s2
  exact .step (.appL x s1) (.step s2 (.refl _))

/-- succ (church k) →β* church (k+1) -/
theorem succ_church {Γ : List Unit} (k : Nat) : Star Beta (.app succT (church (Γ := Γ) k)) (church (k + 1)) := by
  have s1 : Beta (.app (succT (Γ := Γ)) (church k))
      (.lam (.lam (.app (.var (.s .z)) (.app (.app (church k) (.var (.s .z))) (.var .z))))) := by
    have b := Beta.beta (.lam (.lam (.app (.var (.s .z)) (.app (.app (.var (.s (.s .z))) (.var (.s .z))) (.var .z)))))
      (church (Γ := Γ) k)
    have e : inst (.lam (.lam (.app (.var (.s .z)) (.app (.app (.var (.s (.s .z))) (.var (.s .z))) (.var .z)))))
        (church (Γ := Γ) k) =
        .lam (.lam (.app (.var (.s .z)) (.app (.app (church k) (.var (.s .z))) (.var .z)))) := by
      show T.lam (T.lam (T.app (T.var (V.s V.z)) (T.app (T.app (ren V.s (ren V.s (church k))) (T.var (V.s V.z))) (T.var V.z)))) = _
      rw [ren_church, ren_church]
    rw [e] at b
    exact b
  have inner := church_app (Γ := () :: () :: Γ) k (.var (.s .z)) (.var .z)
  exact .step s1 (star_lam (star_lam (star_appR _ inner)))

theorem iter_add {Γ : List Unit} (f x : T Γ) (b : Nat) : ∀ a, iterApp f (iterApp f x b) a = iterApp f x (a + b)
  | 0 => by rw [Nat.zero_add]; rfl
  | a + 1 => by
    rw [Nat.add_right_comm]
    exact congrArg (T.app f) (iter_add f x b a)

theorem star_appL {Γ : List Unit} {a b : T Γ} (u : T Γ) (h : Star Beta a b) : Star Beta (.app a u) (.app b u) :=
  Star.map (fun t => T.app t u) (fun r => .appL u r) h

def addT {Γ : List Unit} : T Γ :=
  .lam (.lam (.lam (.lam (.app (.app (.var (.s (.s (.s .z)))) (.var (.s .z)))
    (.app (.app (.var (.s (.s .z))) (.var (.s .z))) (.var .z))))))

/-- add (church a) (church b) →β* church (a + b) -/
theorem add_church {Γ : List Unit} (a b : Nat) :
    Star Beta (.app (.app addT (church (Γ := Γ) a)) (church b)) (church (a + b)) := by
  have s1 : Beta (.app (addT (Γ := Γ)) (church a))
      (.lam (.lam (.lam (.app (.app (church a) (.var (.s .z)))
        (.app (.app (.var (.s (.s .z))) (.var (.s .z))) (.var .z)))))) := by
    have bt := Beta.beta (.lam (.lam (.lam (.app (.app (.var (.s (.s (.s .z)))) (.var (.s .z)))
      (.app (.app (.var (.s (.s .z))) (.var (.s .z))) (.var .z)))))) (church (Γ := Γ) a)
    have e : inst (.lam (.lam (.lam (.app (.app (.var (.s (.s (.s .z)))) (.var (.s .z)))
        (.app (.app (.var (.s (.s .z))) (.var (.s .z))) (.var .z)))))) (church (Γ := Γ) a) =
        .lam (.lam (.lam (.app (.app (church a) (.var (.s .z)))
          (.app (.app (.var (.s (.s .z))) (.var (.s .z))) (.var .z))))) := by
      show T.lam (T.lam (T.lam (T.app (T.app (ren V.s (ren V.s (ren V.s (church a)))) (T.var (V.s V.z)))
        (T.app (T.app (T.var (V.s (V.s V.z))) (T.var (V.s V.z))) (T.var V.z))))) = _
      rw [ren_church, ren_church, ren_church]
    rw [e] at bt
    exact bt
  have s2 : Beta (.app (.lam (.lam (.lam (.app (.app (church a) (.var (.s .z)))
        (.app (.app (.var (.s (.s .z))) (.var (.s .z))) (.var .z)))))) (church (Γ := Γ) b))
      (.lam (.lam (.app (.app (church a) (.var (.s .z)))
        (.app (.app (church b) (.var (.s .z))) (.var .z))))) := by
    have bt := Beta.beta (.lam (.lam (.app (.app (church (Γ := () :: () :: () :: Γ) a) (.var (.s .z)))
        (.app (.app (.var (.s (.s .z))) (.var (.s .z))) (.var .z))))) (church (Γ := Γ) b)
    have e : inst (.lam (.lam (.app (.app (church (Γ := () :: () :: () :: Γ) a) (.var (.s .z)))
        (.app (.app (.var (.s (.s .z))) (.var (.s .z))) (.var .z))))) (church (Γ := Γ) b) =
        .lam (.lam (.app (.app (church a) (.var (.s .z)))
          (.app (.app (church b) (.var (.s .z))) (.var .z)))) := by
      show T.lam (T.lam (T.app (T.app (sub (liftS (liftS (single (church b)))) (church a)) (T.var (V.s V.z)))
        (T.app (T.app (ren V.s (ren V.s (church b))) (T.var (V.s V.z))) (T.var V.z)))) = _
      rw [ren_church, ren_church]
      have h : sub (liftS (liftS (single (church (Γ := Γ) b)))) (church (Γ := () :: () :: () :: Γ) a) = church a :=
        congrArg T.lam (congrArg T.lam ((sub_iter _ _ _ a).trans rfl))
      rw [h]
    rw [e] at bt
    exact bt
  have inner_b := church_app (Γ := () :: () :: Γ) b (.var (.s .z)) (.var .z)
  have inner_a := church_app (Γ := () :: () :: Γ) a (.var (.s .z)) (iterApp (.var (.s .z)) (.var .z) b)
  have body : Star Beta (.app (.app (church a) (.var (.s .z))) (.app (.app (church b) (.var (.s .z))) (.var .z)))
      (iterApp (T.var (n := () :: () :: Γ) (.s .z)) (.var .z) (a + b)) :=
    (star_appR _ inner_b).trans' ((iter_add (.var (.s .z)) (.var .z) b a) ▸ inner_a)
  exact (star_appL _ (.one s1)).trans' (.step s2 (star_lam (star_lam body)))

theorem iter_church {Γ : List Unit} (b : Nat) (f x : T Γ) :
    ∀ a, Star Beta (iterApp (.app (church b) f) x a) (iterApp f x (a * b))
  | 0 => by rw [Nat.zero_mul]; exact .refl _
  | a + 1 => by
    have h1 := star_appR (.app (church b) f) (iter_church b f x a)
    have h2 := church_app b f (iterApp f x (a * b))
    have e : iterApp f (iterApp f x (a * b)) b = iterApp f x ((a + 1) * b) := by
      rw [iter_add, Nat.succ_mul, Nat.add_comm]
    exact h1.trans' (e ▸ h2)

/-- one-argument application of a Church numeral -/
theorem church_app1 {Γ : List Unit} (k : Nat) (g : T Γ) :
    Beta (.app (church k) g) (.lam (iterApp (ren V.s g) (.var .z) k)) := by
  have s1 : Beta (.app (church k) g) (inst (.lam (iterApp (.var (.s .z)) (.var .z) k)) g) := .beta _ _
  have e1 : inst (.lam (iterApp (.var (.s .z)) (.var .z) k)) g = .lam (iterApp (ren V.s g) (.var .z) k) := by
    show T.lam (sub (liftS (single g)) (iterApp (.var (.s .z)) (.var .z) k)) = _
    rw [sub_iter]
    rfl
  rw [e1] at s1
  exact s1

def mulT {Γ : List Unit} : T Γ := .lam (.lam (.lam (.app (.var (.s (.s .z))) (.app (.var (.s .z)) (.var .z)))))

/-- mul (church a) (church b) →β* church (a * b) -/
theorem mul_church {Γ : List Unit} (a b : Nat) :
    Star Beta (.app (.app mulT (church (Γ := Γ) a)) (church b)) (church (a * b)) := by
  have s1 : Beta (.app (mulT (Γ := Γ)) (church a))
      (.lam (.lam (.app (church a) (.app (.var (.s .z)) (.var .z))))) := by
    have bt := Beta.beta (.lam (.lam (.app (.var (.s (.s .z))) (.app (.var (.s .z)) (.var .z))))) (church (Γ := Γ) a)
    have e : inst (.lam (.lam (.app (.var (.s (.s .z))) (.app (.var (.s .z)) (.var .z))))) (church (Γ := Γ) a) =
        .lam (.lam (.app (church a) (.app (.var (.s .z)) (.var .z)))) := by
      show T.lam (T.lam (T.app (ren V.s (ren V.s (church a))) (T.app (T.var (V.s V.z)) (T.var V.z)))) = _
      rw [ren_church, ren_church]
    rw [e] at bt
    exact bt
  have s2 : Beta (.app (.lam (.lam (.app (church a) (.app (.var (.s .z)) (.var .z))))) (church (Γ := Γ) b))
      (.lam (.app (church a) (.app (church b) (.var .z)))) := by
    have bt := Beta.beta (.lam (.app (church (Γ := () :: () :: Γ) a) (.app (.var (.s .z)) (.var .z)))) (church (Γ := Γ) b)
    have e : inst (.lam (.app (church (Γ := () :: () :: Γ) a) (.app (.var (.s .z)) (.var .z)))) (church (Γ := Γ) b) =
        .lam (.app (church a) (.app (church b) (.var .z))) := by
      show T.lam (T.app (sub (liftS (single (church b))) (church a)) (T.app (ren V.s (church b)) (T.var V.z))) = _
      rw [ren_church]
      have h : sub (liftS (single (church (Γ := Γ) b))) (church (Γ := () :: () :: Γ) a) = church a :=
        congrArg T.lam (congrArg T.lam ((sub_iter _ _ _ a).trans rfl))
      rw [h]
    rw [e] at bt
    exact bt
  have s3 := church_app1 (Γ := () :: Γ) a (.app (church b) (.var .z))
  have e3 : ren V.s (T.app (church (Γ := () :: Γ) b) (.var .z)) = .app (church b) (.var (.s .z)) := by
    show T.app (ren V.s (church b)) (T.var (V.s V.z)) = _
    rw [ren_church]
  rw [e3] at s3
  have s4 := iter_church (Γ := () :: () :: Γ) b (.var (.s .z)) (.var .z) a
  exact (star_appL _ (.one s1)).trans' (.step s2 (star_lam (.step s3 (star_lam s4))))

def tru {Γ : List Unit} : T Γ := .lam (.lam (.var (.s .z)))
def fls {Γ : List Unit} : T Γ := .lam (.lam (.var .z))

/-- iszero = λn. n (λ_. fls) tru -/
def iszero {Γ : List Unit} : T Γ := .lam (.app (.app (.var .z) (.lam fls)) tru)

theorem iszero_church {Γ : List Unit} (k : Nat) :
    Star Beta (.app iszero (church (Γ := Γ) k)) (match k with | 0 => tru | _ + 1 => fls) := by
  have s1 : Beta (.app (iszero (Γ := Γ)) (church k)) (.app (.app (church k) (.lam fls)) tru) := .beta _ _
  have s2 := church_app (Γ := Γ) k (.lam fls) tru
  refine .step s1 (s2.trans' ?_)
  cases k with
  | zero => exact .refl _
  | succ k => exact .one (.beta _ _)

/-- Turing's fixed-point combinator Θ = A A, A = λx.λy. y (x x y) -/
def Aθ {Γ : List Unit} : T Γ := .lam (.lam (.app (.var .z) (.app (.app (.var (.s .z)) (.var (.s .z))) (.var .z))))
def Θ {Γ : List Unit} : T Γ := .app Aθ Aθ

theorem theta_fix {Γ : List Unit} (g : T Γ) : Star Beta (.app Θ g) (.app g (.app Θ g)) := by
  have s1 : Beta (.app (Aθ (Γ := Γ)) Aθ) (.lam (.app (.var .z) (.app (.app Aθ Aθ) (.var .z)))) := .beta _ _
  have s2 : Beta (.app (.lam (.app (.var .z) (.app (.app (Aθ (Γ := () :: Γ)) Aθ) (.var .z)))) g)
      (.app g (.app (.app Aθ Aθ) g)) := .beta _ _
  exact .step (.appL g s1) (.one s2)

end UL

end Found

#print axioms Found.UL.sub_iter
#print axioms Found.UL.ren_church
#print axioms Found.UL.church_app
#print axioms Found.UL.succ_church
#print axioms Found.UL.iter_add
#print axioms Found.UL.add_church
#print axioms Found.UL.iter_church
#print axioms Found.UL.mul_church
#print axioms Found.UL.iszero_church
#print axioms Found.UL.theta_fix

found/ChurchTuring2.lean

lean_all/found/ChurchTuring2.lean

/-
  T207 (L1, Church–Turing line). found/ChurchTuring2 — every primitive
  recursive function (T81's PR syntax and eval) is λ-definable on Church
  numerals (T78/ChurchNum): a compiler PR → λ-terms and, for every f and
  every argument list of the right length, a β-reduction to the Church
  numeral of eval f args.
  Part 1 (this block): substitution congruence for →β*, Church pairs.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.ChurchNum
import found.Recursive

namespace Found

namespace CT

open UL

theorem star_trans {Γ : List Unit} {a b c : T Γ} (h : Star Beta a b) (k : Star Beta b c) : Star Beta a c :=
  h.trans' k

theorem star_ren {m n : List Unit} (r : V m → V n) {a b : T m} (h : Star Beta a b) :
    Star Beta (ren r a) (ren r b) := by
  induction h with
  | refl => exact .refl _
  | step s _ ih => exact star_trans (par_betas (par_ren (beta_par s) r)) ih

theorem star_app {Γ : List Unit} {a a' b b' : T Γ} (h : Star Beta a a') (k : Star Beta b b') :
    Star Beta (.app a b) (.app a' b') :=
  star_trans (star_appL _ h) (star_appR _ k)

theorem star_sub : {m : List Unit} → (t : T m) → ∀ {n : List Unit} {σ τ : V m → T n},
    (∀ v, Star Beta (σ v) (τ v)) → Star Beta (sub σ t) (sub τ t)
  | _, .var v, _, _, _, h => h v
  | _, .app a b, _, _, _, h => star_app (star_sub a h) (star_sub b h)
  | _, .lam t, _, _, _, h => star_lam (star_sub t (fun v => match v with
      | .z => .refl _
      | .s w => star_ren V.s (h w)))

/-- weakening then instantiating is the identity -/
theorem inst_ren {Γ : List Unit} (a u : T Γ) : inst (ren V.s a) u = a := by
  show sub (single u) (ren V.s a) = a
  rw [sub_ren]
  exact (sub_ext a (τ := T.var) (fun _ => rfl)).trans (sub_var a)

/-- K-reductions: tru a b →β* a, fls a b →β* b -/
theorem tru_ab {Γ : List Unit} (a b : T Γ) : Star Beta (.app (.app tru a) b) a := by
  have s1 : Beta (.app (tru (Γ := Γ)) a) (.lam (ren V.s a)) := .beta _ _
  have s2 : Beta (.app (.lam (ren V.s a)) b) (inst (ren V.s a) b) := .beta _ _
  rw [inst_ren] at s2
  exact .step (.appL b s1) (.one s2)

theorem fls_ab {Γ : List Unit} (a b : T Γ) : Star Beta (.app (.app fls a) b) b :=
  .step (.appL b (.beta _ _)) (.one (.beta _ _))

/-- Church pairs -/
def pairT {Γ : List Unit} (a b : T Γ) : T Γ := .lam (.app (.app (.var .z) (ren V.s a)) (ren V.s b))
def fstT {Γ : List Unit} (p : T Γ) : T Γ := .app p tru
def sndT {Γ : List Unit} (p : T Γ) : T Γ := .app p fls

theorem pair_sel {Γ : List Unit} (a b s : T Γ) : Star Beta (.app (pairT a b) s) (.app (.app s a) b) := by
  have s1 : Beta (.app (pairT a b) s) (inst (.app (.app (.var .z) (ren V.s a)) (ren V.s b)) s) := .beta _ _
  have e : inst (.app (.app (.var .z) (ren V.s a)) (ren V.s b)) s = .app (.app s a) b := by
    show T.app (T.app s (inst (ren V.s a) s)) (inst (ren V.s b) s) = _
    rw [inst_ren, inst_ren]
  rw [e] at s1
  exact .one s1

theorem fst_pair {Γ : List Unit} (a b : T Γ) : Star Beta (fstT (pairT a b)) a :=
  star_trans (pair_sel a b tru) (tru_ab a b)

theorem snd_pair {Γ : List Unit} (a b : T Γ) : Star Beta (sndT (pairT a b)) b :=
  star_trans (pair_sel a b fls) (fls_ab a b)

theorem star_pair {Γ : List Unit} {a a' b b' : T Γ} (h : Star Beta a a') (k : Star Beta b b') :
    Star Beta (pairT a b) (pairT a' b') :=
  star_lam (star_app (star_appR _ (star_ren V.s h)) (star_ren V.s k))

theorem sub_church {Γ Δ : List Unit} (σ : V Γ → T Δ) (k : Nat) : sub σ (church k) = church k :=
  congrArg T.lam (congrArg T.lam (sub_iter _ _ _ k))

theorem sub_succT {Γ Δ : List Unit} (σ : V Γ → T Δ) : sub σ succT = succT := rfl
theorem sub_tru {Γ Δ : List Unit} (σ : V Γ → T Δ) : sub σ tru = tru := rfl
theorem sub_fls {Γ Δ : List Unit} (σ : V Γ → T Δ) : sub σ fls = fls := rfl

theorem sub_pair {Γ Δ : List Unit} (σ : V Γ → T Δ) (a b : T Γ) :
    sub σ (pairT a b) = pairT (sub σ a) (sub σ b) := by
  show T.lam (T.app (T.app (T.var V.z) (sub (liftS σ) (ren V.s a))) (sub (liftS σ) (ren V.s b))) = _
  rw [sub_ren, sub_ren]
  show T.lam (T.app (T.app (T.var V.z) (sub (fun v => ren V.s (σ v)) a)) (sub (fun v => ren V.s (σ v)) b)) = _
  rw [← ren_sub, ← ren_sub]
  rfl

/-! ## Part 2: the compiler PR → λ and its correctness -/

open Rec

@[reducible] def ctx : Nat → List Unit
  | 0 => []
  | n + 1 => () :: ctx n

def varAt : (n : Nat) → Nat → T (ctx n)
  | 0, _ => church 0
  | _ + 1, 0 => .var .z
  | n + 1, i + 1 => ren V.s (varAt n i)

def tl : List Nat → List Nat
  | [] => []
  | _ :: l => l

def env {Δ : List Unit} : (n : Nat) → List Nat → V (ctx n) → T Δ
  | _ + 1, args, .z => church (nth args 0)
  | n + 1, args, .s v => env n (tl args) v

theorem nth_nil : (i : Nat) → nth [] i = 0
  | 0 => rfl
  | _ + 1 => rfl

theorem env_var {Δ : List Unit} : (n : Nat) → (args : List Nat) → args.length = n → (i : Nat) →
    sub (env (Δ := Δ) n args) (varAt n i) = church (nth args i)
  | 0, [], _, i => by rw [nth_nil]; exact sub_church _ 0
  | n + 1, _ :: _, _, 0 => rfl
  | n + 1, a :: rest, h, i + 1 => by
    exact (sub_ren (varAt n i) (env (Δ := Δ) (n + 1) (a :: rest)) V.s).trans (env_var n rest (Nat.succ.inj h) i)

/-- body of the iteration step for primitive recursion -/
def rho (m : Nat) : V (ctx (m + 2)) → T (() :: ctx (m + 1))
  | .z => fstT (.var .z)
  | .s .z => sndT (.var .z)
  | .s (.s v) => .var (.s (.s v))

mutual
def comp : PR → (n : Nat) → T (ctx n)
  | .zero, _ => church 0
  | .succ, n => .app succT (varAt n 0)
  | .proj i, n => varAt n i
  | .comp f gs, n => sub (compL gs n) (comp f gs.length)
  | .prec f _, 0 => comp f 0
  | .prec f g, m + 1 => sndT (.app (.app (.var .z)
      (.lam (pairT (.app succT (fstT (.var .z))) (sub (rho m) (comp g (m + 2))))))
      (pairT (church 0) (ren V.s (comp f m))))
def compL : (gs : List PR) → (n : Nat) → V (ctx gs.length) → T (ctx n)
  | g :: _, n, .z => comp g n
  | _ :: gs, n, .s v => compL gs n v
end

theorem len_evalList : (gs : List PR) → (args : List Nat) → (evalList gs args).length = gs.length
  | [], _ => rfl
  | _ :: gs, args => congrArg (· + 1) (len_evalList gs args)

/-- one iteration step of the compiled primitive recursion -/
theorem step_red {Δ : List Unit} (m : Nat) (gc : T (ctx (m + 2))) (G : List Nat → Nat)
    (hg : ∀ args' : List Nat, args'.length = m + 2 →
      Star Beta (sub (env (Δ := Δ) (m + 2) args') gc) (church (G args')))
    (a : Nat) (rest : List Nat) (hlen : rest.length = m) (k v : Nat) :
    Star Beta (T.app (sub (env (Δ := Δ) (m + 1) (a :: rest))
        (T.lam (pairT (.app succT (fstT (.var .z))) (sub (rho m) gc)))) (pairT (church k) (church v)))
      (pairT (church (k + 1)) (church (G (k :: v :: rest)))) := by
  refine .step (.beta _ _) ?_
  show Star Beta (sub (single (pairT (church k) (church v))) (sub (liftS (env (m + 1) (a :: rest)))
      (pairT (.app succT (fstT (.var .z))) (sub (rho m) gc)))) _
  rw [sub_sub, sub_pair, sub_sub]
  refine star_pair (star_trans (star_appR succT (fst_pair _ _)) (succ_church k)) ?_
  refine star_trans (star_sub gc (σ := fun w => sub (fun u => sub (single (pairT (church k) (church v)))
      (liftS (env (m + 1) (a :: rest)) u)) (rho m w)) (τ := env (m + 2) (k :: v :: rest)) ?_) ?_
  · intro w
    match w with
    | .z => exact fst_pair _ _
    | .s .z => exact snd_pair _ _
    | .s (.s w) =>
      show Star Beta (inst (ren V.s (env m rest w)) (pairT (church k) (church v))) (env m rest w)
      rw [inst_ren]
      exact .refl _
  · exact hg (k :: v :: rest) (congrArg (· + 2) hlen)

theorem iter_red {Δ : List Unit} (m : Nat) (gc : T (ctx (m + 2))) (F G : List Nat → Nat)
    (hg : ∀ args' : List Nat, args'.length = m + 2 →
      Star Beta (sub (env (Δ := Δ) (m + 2) args') gc) (church (G args')))
    (a : Nat) (rest : List Nat) (hlen : rest.length = m) (B0 : T Δ) (hB : Star Beta B0 (church (F rest))) :
    ∀ k, Star Beta (iterApp (sub (env (Δ := Δ) (m + 1) (a :: rest))
        (T.lam (pairT (.app succT (fstT (.var .z))) (sub (rho m) gc)))) (pairT (church 0) B0) k)
      (pairT (church k) (church (precIter F G k rest)))
  | 0 => star_pair (.refl _) hB
  | k + 1 => star_trans (star_appR _ (iter_red m gc F G hg a rest hlen B0 hB k))
      (step_red m gc G hg a rest hlen k _)

mutual
theorem correct : (f : PR) → (n : Nat) → (args : List Nat) → args.length = n → ∀ {Δ : List Unit},
    Star Beta (sub (env (Δ := Δ) n args) (comp f n)) (church (eval f args))
  | .zero, n, args, _, Δ => by
    show Star Beta (sub (env (Δ := Δ) n args) (church 0)) (church 0)
    rw [sub_church]
    exact .refl _
  | .succ, n, args, h, Δ => by
    show Star Beta (T.app succT (sub (env (Δ := Δ) n args) (varAt n 0))) (church (nth args 0 + 1))
    rw [env_var n args h 0]
    exact succ_church _
  | .proj i, n, args, h, Δ => by
    show Star Beta (sub (env (Δ := Δ) n args) (varAt n i)) (church (nth args i))
    rw [env_var n args h i]
    exact .refl _
  | .comp f gs, n, args, h, Δ => by
    show Star Beta (sub (env (Δ := Δ) n args) (sub (compL gs n) (comp f gs.length)))
      (church (eval f (evalList gs args)))
    rw [sub_sub]
    exact star_trans (star_sub _ (fun v => correctL gs n args h v))
      (correct f gs.length (evalList gs args) (len_evalList gs args))
  | .prec f _, 0, [], _, Δ => correct f 0 [] rfl
  | .prec _ _, 0, _ :: _, h, _ => Nat.noConfusion h
  | .prec _ _, _ + 1, [], h, _ => Nat.noConfusion h
  | .prec f g, m + 1, a :: rest, h, Δ => by
    have hlen : rest.length = m := Nat.succ.inj h
    show Star Beta (T.app (T.app (T.app (church a)
        (sub (env (Δ := Δ) (m + 1) (a :: rest))
          (T.lam (pairT (.app succT (fstT (.var .z))) (sub (rho m) (comp g (m + 2)))))))
        (sub (env (m + 1) (a :: rest)) (pairT (church 0) (ren V.s (comp f m))))) fls)
      (church (precIter (eval f) (eval g) a rest))
    rw [sub_pair, sub_church, sub_ren]
    refine star_trans (star_appL _ (church_app a _ _)) ?_
    exact star_trans (star_appL _ (iter_red m (comp g (m + 2)) (eval f) (eval g)
      (fun args' h' => correct g (m + 2) args' h') a rest hlen _ (correct f m rest hlen) a)) (snd_pair _ _)
theorem correctL : (gs : List PR) → (n : Nat) → (args : List Nat) → args.length = n → ∀ {Δ : List Unit}
    (v : V (ctx gs.length)), Star Beta (sub (env (Δ := Δ) n args) (compL gs n v))
      (env (Δ := Δ) gs.length (evalList gs args) v)
  | g :: _, n, args, h, _, .z => correct g n args h
  | _ :: gs, n, args, h, _, .s v => correctL gs n args h v
end

/-- closing: λ-abstract the n argument variables -/
def lams : (n : Nat) → T (ctx n) → T []
  | 0, t => t
  | n + 1, t => lams n (.lam t)

end CT

end Found

#print axioms Found.CT.env_var
#print axioms Found.CT.comp
#print axioms Found.CT.step_red
#print axioms Found.CT.iter_red
#print axioms Found.CT.correct

#print axioms Found.CT.star_sub
#print axioms Found.CT.fst_pair
#print axioms Found.CT.snd_pair
#print axioms Found.CT.star_pair
#print axioms Found.CT.sub_church
#print axioms Found.CT.sub_pair

found/ChurchTuring3.lean

lean_all/found/ChurchTuring3.lean

/-
  T207 (L1, Church–Turing line), part 3. found/ChurchTuring3 — every
  μ-recursive function is λ-definable.
  * MR: zero, succ, proj, comp, prec, mu (nested μ allowed); relational
    semantics Ev (partial functions), with μ by an explicit search
    derivation MuS (no choice: each skipped value carries its own nonzero
    evaluation).
  * Compiler mr : MR → λ-terms (μ via Turing's Θ and iszero), and
    correctness: Ev h args n ⟹ sub env (mr h m) →β* church n.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.ChurchTuring2

namespace Found

namespace CT3

open UL CT Rec

inductive MR where
  | zero : MR
  | succ : MR
  | proj : Nat → MR
  | comp : MR → List MR → MR
  | prec : MR → MR → MR
  | mu : MR → MR

mutual
inductive Ev : MR → List Nat → Nat → Prop where
  | zero (args) : Ev .zero args 0
  | succ (args) : Ev .succ args (nth args 0 + 1)
  | proj (i args) : Ev (.proj i) args (nth args i)
  | comp {f gs args bs n} : EvL gs args bs → Ev f bs n → Ev (.comp f gs) args n
  | precNil {f g n} : Ev f [] n → Ev (.prec f g) [] n
  | prec0 {f g rest n} : Ev f rest n → Ev (.prec f g) (0 :: rest) n
  | precS {f g k rest v n} : Ev (.prec f g) (k :: rest) v → Ev g (k :: v :: rest) n →
      Ev (.prec f g) ((k + 1) :: rest) n
  | mu {f args n} : MuS f args 0 n → Ev (.mu f) args n
inductive EvL : List MR → List Nat → List Nat → Prop where
  | nil (args) : EvL [] args []
  | cons {g gs args b bs} : Ev g args b → EvL gs args bs → EvL (g :: gs) args (b :: bs)
inductive MuS : MR → List Nat → Nat → Nat → Prop where
  | stop {f args j} : Ev f (j :: args) 0 → MuS f args j j
  | next {f args j i n} : Ev f (j :: args) (i + 1) → MuS f args (j + 1) n → MuS f args j n
end

def rhoMu (n : Nat) : V (ctx (n + 1)) → T (() :: () :: ctx n)
  | .z => .var .z
  | .s v => .var (.s (.s v))

def muBody {n : Nat} (F : T (() :: () :: ctx n)) : T (ctx n) :=
  .lam (.lam (.app (.app (.app iszero F) (.var .z)) (.app (.var (.s .z)) (.app succT (.var .z)))))

mutual
def mr : MR → (n : Nat) → T (ctx n)
  | .zero, _ => church 0
  | .succ, n => .app succT (varAt n 0)
  | .proj i, n => varAt n i
  | .comp f gs, n => sub (mrL gs n) (mr f gs.length)
  | .prec f _, 0 => mr f 0
  | .prec f g, m + 1 => sndT (.app (.app (.var .z)
      (.lam (pairT (.app succT (fstT (.var .z))) (sub (rho m) (mr g (m + 2))))))
      (pairT (church 0) (ren V.s (mr f m))))
  | .mu f, n => .app (.app Θ (muBody (sub (rhoMu n) (mr f (n + 1))))) (church 0)
def mrL : (gs : List MR) → (n : Nat) → V (ctx gs.length) → T (ctx n)
  | g :: _, n, .z => mr g n
  | _ :: gs, n, .s v => mrL gs n v
end

/-! ## correctness -/

def envIdx : (n : Nat) → List Nat → V (ctx n) → Nat
  | _ + 1, args, .z => nth args 0
  | n + 1, args, .s v => envIdx n (tl args) v

theorem env_eq {Δ : List Unit} : (n : Nat) → (args : List Nat) → (v : V (ctx n)) →
    env (Δ := Δ) n args v = church (envIdx n args v)
  | _ + 1, _, .z => rfl
  | n + 1, args, .s v => env_eq n (tl args) v

theorem ren_env {Δ Δ' : List Unit} (r : V Δ → V Δ') (n : Nat) (args : List Nat) (v : V (ctx n)) :
    ren r (env (Δ := Δ) n args v) = env n args v := by
  rw [env_eq, env_eq, ren_church]

theorem sub_env {Δ Δ' : List Unit} (σ : V Δ → T Δ') (n : Nat) (args : List Nat) (v : V (ctx n)) :
    sub σ (env (Δ := Δ) n args v) = env n args v := by
  rw [env_eq, env_eq, sub_church]

/-- one unfolding of the compiled μ-search at the numeral j -/
theorem mu_step {Δ : List Unit} (n : Nat) (args : List Nat) (M : T (ctx (n + 1))) (j w : Nat)
    (hw : Star Beta (sub (env (Δ := Δ) (n + 1) (j :: args)) M) (church w)) :
    Star Beta (.app (.app Θ (sub (env (Δ := Δ) n args) (muBody (sub (rhoMu n) M)))) (church j))
      (.app (.app (.app iszero (church w)) (church j))
        (.app (.app Θ (sub (env (Δ := Δ) n args) (muBody (sub (rhoMu n) M)))) (.app succT (church j)))) := by
  refine star_trans (star_appL _ (theta_fix _)) ?_
  refine .step (.appL _ (.beta _ _)) (.step (.beta _ _) ?_)
  generalize hG : sub (env (Δ := Δ) n args) (muBody (sub (rhoMu n) M)) = G
  show Star Beta (sub (single (church j)) (sub (liftS (single (T.app Θ G)))
    (sub (liftS (liftS (env n args))) (.app (.app (.app iszero (sub (rhoMu n) M)) (.var .z))
      (.app (.var (.s .z)) (.app succT (.var .z))))))) _
  rw [sub_sub, sub_sub]
  have hsz : inst (ren V.s (T.app Θ G)) (church j) = T.app Θ G := inst_ren _ _
  have hF : sub (fun v => sub (fun u => sub (single (church j)) (liftS (single (T.app Θ G)) u))
      (liftS (liftS (env (Δ := Δ) n args)) v)) (sub (rhoMu n) M) = sub (env (n + 1) (j :: args)) M := by
    rw [sub_sub]
    refine sub_ext M (fun v => ?_)
    match v with
    | .z => rfl
    | .s w =>
      show sub (fun u => sub (single (church j)) (liftS (single (T.app Θ G)) u))
        (ren V.s (ren V.s (env n args w))) = env n args w
      rw [ren_env, ren_env, sub_env]
  show Star Beta (.app (.app (.app iszero (sub (fun v => sub (fun u => sub (single (church j))
      (liftS (single (T.app Θ G)) u)) (liftS (liftS (env (Δ := Δ) n args)) v)) (sub (rhoMu n) M)))
      (church j)) (.app (inst (ren V.s (T.app Θ G)) (church j)) (.app succT (church j)))) _
  rw [hF, hsz]
  exact star_appL _ (star_appL _ (star_appR _ hw))

theorem step_red' {Δ : List Unit} (m : Nat) (gc : T (ctx (m + 2))) (a : Nat) (rest : List Nat)
    (k v w : Nat) (hg : Star Beta (sub (env (Δ := Δ) (m + 2) (k :: v :: rest)) gc) (church w)) :
    Star Beta (T.app (sub (env (Δ := Δ) (m + 1) (a :: rest))
        (T.lam (pairT (.app succT (fstT (.var .z))) (sub (rho m) gc)))) (pairT (church k) (church v)))
      (pairT (church (k + 1)) (church w)) := by
  refine .step (.beta _ _) ?_
  show Star Beta (sub (single (pairT (church k) (church v))) (sub (liftS (env (m + 1) (a :: rest)))
      (pairT (.app succT (fstT (.var .z))) (sub (rho m) gc)))) _
  rw [sub_sub, sub_pair, sub_sub]
  refine star_pair (star_trans (star_appR succT (fst_pair _ _)) (succ_church k)) ?_
  refine star_trans (star_sub gc (σ := fun x => sub (fun u => sub (single (pairT (church k) (church v)))
      (liftS (env (m + 1) (a :: rest)) u)) (rho m x)) (τ := env (m + 2) (k :: v :: rest)) ?_) hg
  intro x
  match x with
  | .z => exact fst_pair _ _
  | .s .z => exact snd_pair _ _
  | .s (.s x) =>
    show Star Beta (inst (ren V.s (env m rest x)) (pairT (church k) (church v))) (env m rest x)
    rw [inst_ren]
    exact .refl _

theorem evL_len : {gs : List MR} → {args bs : List Nat} → EvL gs args bs → bs.length = gs.length
  | _, _, _, .nil _ => rfl
  | _, _, _, .cons _ dl => congrArg (· + 1) (evL_len dl)

abbrev Sx {Δ : List Unit} (_f g : MR) (m a : Nat) (rest : List Nat) : T Δ :=
  sub (env (Δ := Δ) (m + 1) (a :: rest)) (T.lam (pairT (.app succT (fstT (.var .z))) (sub (rho m) (mr g (m + 2)))))

abbrev P0 {Δ : List Unit} (f : MR) (m : Nat) (rest : List Nat) : T Δ :=
  pairT (church 0) (sub (env (Δ := Δ) m rest) (mr f m))

theorem fin_prec {Δ : List Unit} (f g : MR) (m a n : Nat) (rest : List Nat)
    (hit : Star Beta (iterApp (Sx (Δ := Δ) f g m a rest) (P0 f m rest) a) (pairT (church a) (church n))) :
    Star Beta (sub (env (Δ := Δ) (m + 1) (a :: rest)) (mr (.prec f g) (m + 1))) (church n) := by
  show Star Beta (T.app (T.app (T.app (church a) (Sx (Δ := Δ) f g m a rest))
      (sub (env (m + 1) (a :: rest)) (pairT (church 0) (ren V.s (mr f m))))) fls) (church n)
  rw [sub_pair, sub_church, sub_ren]
  exact star_trans (star_appL _ (star_trans (church_app a _ _) hit)) (snd_pair _ _)

/-- the iteration claim for a derivation about `prec f g` at `k :: rest` -/
def ItP {Δ : List Unit} (m a : Nat) (v : Nat) : MR → List Nat → Prop
  | .prec f g, k :: rest => rest.length = m →
      Star Beta (iterApp (Sx (Δ := Δ) f g m a rest) (P0 f m rest) k) (pairT (church k) (church v))
  | .prec _ _, [] => True
  | .zero, _ => True
  | .succ, _ => True
  | .proj _, _ => True
  | .comp _ _, _ => True
  | .mu _, _ => True

abbrev Gm {Δ : List Unit} (f : MR) (m : Nat) (args : List Nat) : T Δ :=
  sub (env (Δ := Δ) m args) (muBody (sub (rhoMu m) (mr f (m + 1))))

mutual
theorem cor : {h : MR} → {args : List Nat} → {n : Nat} → Ev h args n → (m : Nat) → args.length = m →
    {Δ : List Unit} → Star Beta (sub (env (Δ := Δ) m args) (mr h m)) (church n)
  | _, args, _, .zero _, m, _, Δ => by
    show Star Beta (sub (env (Δ := Δ) m args) (church 0)) (church 0)
    rw [sub_church]
    exact .refl _
  | _, args, _, .succ _, m, hl, Δ => by
    show Star Beta (T.app succT (sub (env (Δ := Δ) m args) (varAt m 0))) (church (nth args 0 + 1))
    rw [env_var m args hl 0]
    exact succ_church _
  | _, args, _, .proj i _, m, hl, Δ => by
    show Star Beta (sub (env (Δ := Δ) m args) (varAt m i)) (church (nth args i))
    rw [env_var m args hl i]
    exact .refl _
  | .comp f gs, args, n, .comp (bs := bs) dl d, m, hl, Δ => by
    show Star Beta (sub (env (Δ := Δ) m args) (sub (mrL gs m) (mr f gs.length))) (church n)
    rw [sub_sub]
    exact star_trans (star_sub _ (fun v => corL dl m hl v)) (cor d gs.length (evL_len dl))
  | _, _, _, .precNil d, 0, _, _ => cor d 0 rfl
  | _, _, _, .precNil _, _ + 1, hl, _ => Nat.noConfusion hl
  | _, _, _, .prec0 (f := f) (g := g) (rest := rest) d, m + 1, hl, Δ =>
    fin_prec f g m 0 _ rest (star_pair (.refl _) (cor d m (Nat.succ.inj hl)))
  | _, _, _, .precS (f := f) (g := g) (k := k) (rest := rest) d1 d2, m + 1, hl, Δ =>
    fin_prec f g m (k + 1) _ rest (star_trans (star_appR _ (corIt d1 m (k + 1) (Nat.succ.inj hl)))
      (step_red' m (mr g (m + 2)) (k + 1) rest k _ _ (cor d2 (m + 2) (congrArg (· + 2) (Nat.succ.inj hl)))))
  | _, _, _, .prec0 _, 0, hl, _ => Nat.noConfusion hl
  | _, _, _, .precS _ _, 0, hl, _ => Nat.noConfusion hl
  | .mu f, args, _, .mu ms, m, hl, Δ => corMu ms m hl
theorem corIt : {h : MR} → {args : List Nat} → {v : Nat} → Ev h args v →
    (m a : Nat) → {Δ : List Unit} → ItP (Δ := Δ) m a v h args
  | _, _, _, .zero _, _, _, _ => trivial
  | _, _, _, .succ _, _, _, _ => trivial
  | _, _, _, .proj _ _, _, _, _ => trivial
  | _, _, _, .comp _ _, _, _, _ => trivial
  | _, _, _, .mu _, _, _, _ => trivial
  | _, _, _, .precNil _, _, _, _ => trivial
  | _, _, _, .prec0 d, m, _, _ => fun hl => star_pair (.refl _) (cor d m hl)
  | _, _, _, .precS (g := g) (k := k) (rest := rest) d1 d2, m, a, _ => fun hl =>
    star_trans (star_appR _ (corIt d1 m a hl))
      (step_red' m (mr g (m + 2)) a rest k _ _ (cor d2 (m + 2) (congrArg (· + 2) hl)))
theorem corL : {gs : List MR} → {args bs : List Nat} → EvL gs args bs → (m : Nat) → args.length = m →
    {Δ : List Unit} → (v : V (ctx gs.length)) →
    Star Beta (sub (env (Δ := Δ) m args) (mrL gs m v)) (env (Δ := Δ) gs.length bs v)
  | _, _, _, .cons d _, m, hl, _, .z => cor d m hl
  | _, _, _, .cons _ dl, m, hl, _, .s v => corL dl m hl v
theorem corMu : {f : MR} → {args : List Nat} → {j n : Nat} → MuS f args j n → (m : Nat) → args.length = m →
    {Δ : List Unit} → Star Beta (.app (.app Θ (Gm (Δ := Δ) f m args)) (church j)) (church n)
  | _, args, j, _, .stop (f := f) d, m, hl, _ =>
    star_trans (mu_step m args (mr f (m + 1)) j 0 (cor d (m + 1) (congrArg (· + 1) hl)))
      (star_trans (star_appL _ (star_appL _ (iszero_church 0))) (tru_ab _ _))
  | f, args, j, _, .next (i := i) d ms, m, hl, _ =>
    star_trans (mu_step m args (mr f (m + 1)) j (i + 1) (cor d (m + 1) (congrArg (· + 1) hl)))
      (star_trans (star_appL _ (star_appL _ (iszero_church (i + 1))))
        (star_trans (fls_ab _ _) (star_trans (star_appR _ (succ_church j)) (corMu ms m hl))))
end

end CT3

end Found

#print axioms Found.CT3.mr
#print axioms Found.CT3.mu_step
#print axioms Found.CT3.cor
#print axioms Found.CT3.corMu
#print axioms Found.CT3.corL

found/Classical.lean

lean_all/found/Classical.lean

/-
  T66. found/Classical — classical logic as a condition, and the
  Gödel–Gentzen double-negation translation (propositional).

  * Classical derivability: intuitionistic derivability from Γ plus all
    instances of ¬¬a → a.
  * Condition in the frame: classical logic is sound in every Boolean
    Heyting algebra (¬¬a ≤ a); Ω = (Prop, ↔) is Boolean exactly when
    ¬¬p → p holds for all p; the three-element algebra is not Boolean.
  * Gödel–Gentzen: Γ ⊢_c φ ⟺ gg Γ ⊢_i gg φ (sound and faithful).
  * Classical logic proves p ∨ ¬p (which intuitionistic logic does not, T65).

  No classical axiom is assumed: excluded middle only appears through the
  translation, as a hypothesis set or as a Boolean condition.
  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Minimal


universe u

namespace Found

namespace Prp

def DNE (Γ : Form → Prop) : Form → Prop :=
  fun x => Γ x ∨ ∃ a, x = .imp (neg (neg a)) a

/-- classical derivability -/
def CD (Γ : Form → Prop) (φ : Form) : Prop := PD true (DNE Γ) φ

def HasDNE (Δ : Form → Prop) : Prop := ∀ a, Δ (.imp (neg (neg a)) a)

theorem hasDNE_ext {Δ : Form → Prop} (h : HasDNE Δ) (b : Form) : HasDNE (ext Δ b) :=
  fun a => Or.inr (h a)

section Hyps
variable {I : Bool} {Δ : Form → Prop} {a b c d : Form}
theorem h0 : PD I (ext Δ a) a := .hyp (Or.inl rfl)
theorem h1 : PD I (ext (ext Δ a) b) a := .hyp (Or.inr (Or.inl rfl))
theorem h2 : PD I (ext (ext (ext Δ a) b) c) a := .hyp (Or.inr (Or.inr (Or.inl rfl)))
theorem wk {φ : Form} (d : PD I Δ φ) : PD I (ext Δ a) φ := mono d (fun _ k => Or.inr k)
end Hyps

/-! ## Gödel–Gentzen -/

def gg : Form → Form
  | .var n => neg (neg (.var n))
  | .bot => .bot
  | .and a b => .and (gg a) (gg b)
  | .or a b => neg (.and (neg (gg a)) (neg (gg b)))
  | .imp a b => .imp (gg a) (gg b)

/-- ¬¬¬A → ¬A -/
theorem tn (Θ : Form → Prop) (A : Form) : PD true Θ (.imp (neg (neg (neg A))) (neg A)) :=
  .impI (.impI (.impE h1 (.impI (.impE h0 h1))))

/-- every translated formula is ¬¬-stable, intuitionistically -/
theorem stable : (φ : Form) → ∀ (Θ : Form → Prop), PD true Θ (.imp (neg (neg (gg φ))) (gg φ))
  | .var _, Θ => tn Θ _
  | .bot, _ => .impI (.impE h0 (.impI h0))
  | .and a b, _ =>
    .impI (.andI
      (.impE (stable a _) (.impI (.impE h1 (.impI (.impE h1 (.andE1 h0))))))
      (.impE (stable b _) (.impI (.impE h1 (.impI (.impE h1 (.andE2 h0)))))))
  | .or _ _, Θ => tn Θ _
  | .imp _ b, _ =>
    .impI (.impI (.impE (stable b _)
      (.impI (.impE h2 (.impI (.impE h1 (.impE h0 h2)))))))

/-- soundness of the translation, in cut form -/
theorem gg_sound {Δ : Form → Prop} {φ : Form} (d : PD true Δ φ) :
    ∀ {Θ : Form → Prop}, (∀ x, Δ x → PD true Θ (gg x)) → PD true Θ (gg φ) := by
  induction d with
  | hyp g => exact fun h => h _ g
  | botE _ _ ih => exact fun h => .botE rfl (ih h)
  | andI _ _ iha ihb => exact fun h => .andI (iha h) (ihb h)
  | andE1 _ ih => exact fun h => .andE1 (ih h)
  | andE2 _ ih => exact fun h => .andE2 (ih h)
  | orI1 _ ih => exact fun h => .impI (.impE (.andE1 h0) (wk (ih h)))
  | orI2 _ ih => exact fun h => .impI (.impE (.andE2 h0) (wk (ih h)))
  | @orE _ a b c _ _ _ ih iha ihb =>
    exact fun {Θ} h =>
      have ext_h : ∀ (e : Form), ∀ x, ext _ e x → PD true (ext Θ (gg e)) (gg x) :=
        fun _ x k => k.elim (fun q => q ▸ h0) (fun g => wk (h x g))
      have ca := iha (Θ := ext Θ (gg a)) (ext_h a)
      have cb := ihb (Θ := ext Θ (gg b)) (ext_h b)
      have inc : ∀ (e : Form), ∀ x, ext Θ (gg e) x → ext (ext Θ (neg (gg c))) (gg e) x :=
        fun _ x k => k.elim Or.inl (fun g => Or.inr (Or.inr g))
      .impE (stable c Θ) (.impI (.impE (wk (ih h))
        (.andI (.impI (.impE h1 (mono ca (inc a)))) (.impI (.impE h1 (mono cb (inc b)))))))
  | @impI _ a _ _ ih =>
    exact fun {Θ} h => .impI (ih (Θ := ext Θ (gg a))
      (fun x k => k.elim (fun q => q ▸ h0) (fun g => wk (h x g))))
  | impE _ _ ihf iha => exact fun h => .impE (ihf h) (iha h)

/-- classically, every formula is equivalent to its translation -/
theorem gg_eqv : (φ : Form) → ∀ {Δ : Form → Prop}, HasDNE Δ →
    PD true Δ (.imp φ (gg φ)) ∧ PD true Δ (.imp (gg φ) φ)
  | .var _, _, hD => ⟨.impI (.impI (.impE h0 h1)), .hyp (hD _)⟩
  | .bot, _, _ => ⟨.impI h0, .impI h0⟩
  | .and a b, _, hD =>
    ⟨.impI (.andI (.impE (gg_eqv a (hasDNE_ext hD _)).1 (.andE1 h0))
                  (.impE (gg_eqv b (hasDNE_ext hD _)).1 (.andE2 h0))),
     .impI (.andI (.impE (gg_eqv a (hasDNE_ext hD _)).2 (.andE1 h0))
                  (.impE (gg_eqv b (hasDNE_ext hD _)).2 (.andE2 h0)))⟩
  | .or a b, _, hD =>
    ⟨.impI (.impI (.orE h1
        (.impE (.andE1 h1) (.impE (gg_eqv a (hasDNE_ext (hasDNE_ext (hasDNE_ext hD _) _) _)).1 h0))
        (.impE (.andE2 h1) (.impE (gg_eqv b (hasDNE_ext (hasDNE_ext (hasDNE_ext hD _) _) _)).1 h0)))),
     .impI (.impE (.hyp (hasDNE_ext hD _ (.or a b)))
       (.impI (.impE h1 (.andI
         (.impI (.impE h1 (.orI1 (.impE (gg_eqv a (hasDNE_ext (hasDNE_ext (hasDNE_ext hD _) _) _)).2 h0))))
         (.impI (.impE h1 (.orI2 (.impE (gg_eqv b (hasDNE_ext (hasDNE_ext (hasDNE_ext hD _) _) _)).2 h0))))))))⟩
  | .imp a b, _, hD =>
    ⟨.impI (.impI (.impE (gg_eqv b (hasDNE_ext (hasDNE_ext hD _) _)).1
        (.impE h1 (.impE (gg_eqv a (hasDNE_ext (hasDNE_ext hD _) _)).2 h0)))),
     .impI (.impI (.impE (gg_eqv b (hasDNE_ext (hasDNE_ext hD _) _)).2
        (.impE h1 (.impE (gg_eqv a (hasDNE_ext (hasDNE_ext hD _) _)).1 h0))))⟩

def GG (Γ : Form → Prop) : Form → Prop := fun x => ∃ y, Γ y ∧ x = gg y

theorem dne_has (Γ : Form → Prop) : HasDNE (DNE Γ) := fun a => Or.inr ⟨a, rfl⟩

/-- Gödel–Gentzen: classical ⊢ φ from Γ iff intuitionistic ⊢ gg φ from gg Γ -/
theorem gg_iff (Γ : Form → Prop) (φ : Form) : CD Γ φ ↔ PD true (GG Γ) (gg φ) :=
  ⟨fun d => gg_sound d (fun x k => k.elim (fun g => .hyp ⟨x, g, rfl⟩)
      (fun ⟨a, q⟩ => q ▸ stable a (GG Γ))),
   fun d => .impE (gg_eqv φ (dne_has Γ)).2
      (cut d (fun _ ⟨y, g, q⟩ => q ▸ .impE (gg_eqv y (dne_has Γ)).1 (.hyp (Or.inl g))))⟩

/-- classical logic proves excluded middle -/
theorem cd_lem (Γ : Form → Prop) (p : Form) : CD Γ (.or p (neg p)) :=
  .impE (.hyp (dne_has Γ _)) (.impI (.impE h0 (.orI2 (.impI (.impE h1 (.orI1 h0))))))

/-! ## The Boolean condition -/

def Boolean (H : HA.{u}) : Prop := ∀ a, H.le (H.neg (H.neg a)) a

/-- classical logic is sound in every Boolean Heyting algebra -/
theorem cd_sound (H : HA.{u}) (hB : Boolean H) {φ : Form} (d : CD (fun _ => False) φ)
    (v : Nat → H.car) : H.le H.top (interp H v H.bot φ) :=
  soundness H d v H.bot (fun _ => H.bot_le) H.top (fun _ k => k.elim (fun g => g.elim)
    (fun ⟨_, q⟩ => q ▸ H.imp_adj.mp (H.le_trans (H.meet_r _ _) (hB _))))

/-- Ω is Boolean exactly when double negation elimination holds for all propositions -/
theorem omega_boolean_iff : Boolean OmegaHA ↔ ∀ p : Prop, ¬¬p → p := Iff.rfl

theorem three_not_boolean : ¬ Boolean Three.HA3 := fun h => h Three.h

end Prp

end Found

#print axioms Found.Prp.tn
#print axioms Found.Prp.stable
#print axioms Found.Prp.gg_sound
#print axioms Found.Prp.gg_eqv
#print axioms Found.Prp.gg_iff
#print axioms Found.Prp.cd_lem
#print axioms Found.Prp.cd_sound
#print axioms Found.Prp.omega_boolean_iff
#print axioms Found.Prp.three_not_boolean

found/ClassicalPoint.lean

lean_all/found/ClassicalPoint.lean

/-
  T106 (b), slice 1. found/ClassicalPoint — the classical point of the
  truth-value coordinate (Boolean + choice), taken as hypotheses, never as
  Lean's built-in axioms: excluded middle `lem` and a choice operator
  `ch` with its specification.  Inside this point:
  * every proposition gets a Boolean decision (via ch), double-negation
    elimination;
  * Cantor–Schröder–Bernstein: injections both ways give a bijection
    (the first cardinal tool needed for Holmes' NF model).
  #print axioms stays empty: lem and ch are ordinary variables.

  Lean 4 core only, hand-written proofs, no axioms.
-/

namespace Found

namespace CP

structure Classical' where
  lem : ∀ p : Prop, p ∨ ¬ p
  ch : ∀ {α : Type} (P : α → Prop), (∃ x, P x) → α
  ch_spec : ∀ {α : Type} (P : α → Prop) (h : ∃ x, P x), P (ch P h)
  /-- the choice operator is extensional (Hilbert's ε with extensionality) -/
  ch_ext : ∀ {α : Type} (P Q : α → Prop) (h : ∃ x, P x) (h' : ∃ x, Q x), (∀ x, P x ↔ Q x) → ch P h = ch Q h'

variable (K : Classical')

include K in
theorem dne (p : Prop) (h : ¬ ¬ p) : p := (K.lem p).elim id (fun n => (h n).elim)

/-- a Boolean decision for every proposition -/
def dec (p : Prop) : Bool :=
  K.ch (fun b : Bool => (b = true ↔ p))
    ((K.lem p).elim (fun hp => ⟨true, ⟨fun _ => hp, fun _ => rfl⟩⟩)
      (fun hn => ⟨false, ⟨fun e => Bool.noConfusion e, fun hp => (hn hp).elim⟩⟩))

theorem dec_spec (p : Prop) : dec K p = true ↔ p :=
  K.ch_spec (fun b : Bool => (b = true ↔ p)) _

/-! ## Cantor–Schröder–Bernstein -/

variable {α β : Type} (f : α → β) (g : β → α)
  (hf : ∀ x y, f x = f y → x = y) (hg : ∀ x y, g x = g y → x = y)

def iter (n : Nat) (a : α) : α := match n with
  | 0 => a
  | k + 1 => g (f (iter k a))

/-- the classical CSB set C = ⋃ₙ (g∘f)ⁿ (α ∖ g(β)) -/
def InC (a : α) : Prop := ∃ n a0, (¬ ∃ b, g b = a0) ∧ iter f g n a0 = a

include K in
theorem not_inC_range (a : α) (h : ¬ InC f g a) : ∃ b, g b = a :=
  dne K _ (fun hn => h ⟨0, a, hn, rfl⟩)

def Pcsb (a : α) (b : β) : Prop := (InC f g a ∧ b = f a) ∨ (¬ InC f g a ∧ g b = a)

include K in
theorem pcsb_ex (a : α) : ∃ b, Pcsb f g a b :=
  (K.lem (InC f g a)).elim (fun hc => ⟨f a, Or.inl ⟨hc, rfl⟩⟩)
    (fun hn => match not_inC_range K f g a hn with
      | ⟨b, hb⟩ => ⟨b, Or.inr ⟨hn, hb⟩⟩)

/-- the Cantor–Schröder–Bernstein map: f on C, g⁻¹ outside C -/
def csb (a : α) : β := K.ch (Pcsb f g a) (pcsb_ex K f g a)

theorem csb_spec (a : α) : Pcsb f g a (csb K f g a) := K.ch_spec (Pcsb f g a) _

include hf in
theorem csb_inj (a a' : α) (h : csb K f g a = csb K f g a') : a = a' := by
  rcases csb_spec K f g a with ⟨c, e⟩ | ⟨c, e⟩ <;> rcases csb_spec K f g a' with ⟨c', e'⟩ | ⟨c', e'⟩
  · exact hf _ _ (e.symm.trans (h.trans e'))
  · exfalso
    apply c'
    match c with
    | ⟨n, a0, hr, hi⟩ => exact ⟨n + 1, a0, hr, by
        show g (f (iter f g n a0)) = a'
        rw [hi, ← e, h, e']⟩
  · exfalso
    apply c
    match c' with
    | ⟨n, a0, hr, hi⟩ => exact ⟨n + 1, a0, hr, by
        show g (f (iter f g n a0)) = a
        rw [hi, ← e', ← h, e]⟩
  · rw [← e, ← e', h]

include hg in
theorem csb_surj (b : β) : ∃ a, csb K f g a = b := by
  rcases csb_spec K f g (g b) with ⟨c, e⟩ | ⟨c, e⟩
  · match c with
    | ⟨0, a0, hr, hi⟩ => exact (hr ⟨b, hi.symm ▸ rfl⟩).elim
    | ⟨k + 1, a0, hr, hi⟩ =>
      have hb : f (iter f g k a0) = b := hg _ _ hi
      refine ⟨iter f g k a0, ?_⟩
      rcases csb_spec K f g (iter f g k a0) with ⟨_, e'⟩ | ⟨c', _⟩
      · rw [e', hb]
      · exact (c' ⟨k, a0, hr, rfl⟩).elim
  · exact ⟨g b, hg _ _ e⟩

/-- a two-sided inverse, so the two types are in bijection -/
def csbInv (b : β) : α := K.ch (fun a => csb K f g a = b) (csb_surj K f g hg b)

theorem csb_inv_right (b : β) : csb K f g (csbInv K f g hg b) = b :=
  K.ch_spec (fun a => csb K f g a = b) (csb_surj K f g hg b)

include hf in
theorem csb_inv_left (a : α) : csbInv K f g hg (csb K f g a) = a :=
  csb_inj K f g hf _ _ (csb_inv_right K f g hg _)

end CP

end Found

#print axioms Found.CP.dec_spec
#print axioms Found.CP.csb_inj
#print axioms Found.CP.csb_surj
#print axioms Found.CP.csb_inv_left
#print axioms Found.CP.csb_inv_right

found/Classifier.lean

lean_all/found/Classifier.lean

/-
  T97. found/Classifier — subobject classifiers in the setoid stem, in
  general: any two are isomorphic (core's Iso', T59).

  A classifier is a setoid Ω with a point t such that every ≈-respecting
  predicate φ on any setoid X has a characteristic map χ : X → Ω with
  φ x ⟺ χ x ≈ t, unique up to pointwise ≈.
  * The core's OmegaU (ULift Prop, ↔) with t = True is one (T59 chiU).
  * Any two classifiers are isomorphic.
  The Heyting structure on Ω (T63), Lawvere–Tierney topologies as nuclei
  (T108) and sheaf truth values (T95) are reused, not re-proved;
  sheafification stays OPEN (L95a).

  Lean 4 core only, hand-written proofs, no axioms.
  CIC dependence: OmegaU = ULift Prop.
-/
import found.Core

namespace Found

namespace Classif

open Core

universe u

structure Classifier where
  Om : Setd.{u}
  t : Om.car
  chi : {X : Setd.{u}} → (φ : X.car → Prop) → RespP X φ → SHom X Om
  spec : ∀ {X : Setd.{u}} (φ : X.car → Prop) (h : RespP X φ) (x : X.car), φ x ↔ Om.eqv ((chi φ h).fn x) t
  uniq : ∀ {X : Setd.{u}} (φ : X.car → Prop) (h : RespP X φ) (g : SHom X Om),
    (∀ x, φ x ↔ Om.eqv (g.fn x) t) → ∀ x, Om.eqv (g.fn x) ((chi φ h).fn x)

/-- the core's Ω is a classifier -/
def coreOmega : Classifier.{u} where
  Om := OmegaU
  t := ⟨True⟩
  chi φ h := chiU φ h
  spec _ _ _ := ⟨fun p => ⟨fun _ => trivial, fun _ => p⟩, fun e => e.mpr trivial⟩
  uniq _ _ _ hg x := ⟨fun p => (hg x).mpr ⟨fun _ => trivial, fun _ => p⟩,
                     fun p => ((hg x).mp p).mpr trivial⟩

def isT (A : Classifier.{u}) : A.Om.car → Prop := fun y => A.Om.eqv y A.t

theorem isT_resp (A : Classifier.{u}) : RespP A.Om (isT A) :=
  fun e h => A.Om.trans (A.Om.symm e) h

/-- any two classifiers are isomorphic -/
theorem unique (A B : Classifier.{u}) : Iso' A.Om B.Om := by
  let f := B.chi (isT A) (isT_resp A)
  let g := A.chi (isT B) (isT_resp B)
  refine ⟨f, g, ?_, ?_⟩
  · intro y
    have key : ∀ z, isT A z ↔ A.Om.eqv ((SetdCat.comp f g).fn z) A.t := fun z =>
      ((B.spec (isT A) (isT_resp A) z).trans (A.spec (isT B) (isT_resp B) _))
    have k1 := A.uniq (isT A) (isT_resp A) (SetdCat.comp f g) key y
    have k2 := A.uniq (isT A) (isT_resp A) (SetdCat.id A.Om) (fun _ => Iff.rfl) y
    exact A.Om.trans k1 (A.Om.symm k2)
  · intro y
    have key : ∀ z, isT B z ↔ B.Om.eqv ((SetdCat.comp g f).fn z) B.t := fun z =>
      ((A.spec (isT B) (isT_resp B) z).trans (B.spec (isT A) (isT_resp A) _))
    have k1 := B.uniq (isT B) (isT_resp B) (SetdCat.comp g f) key y
    have k2 := B.uniq (isT B) (isT_resp B) (SetdCat.id B.Om) (fun _ => Iff.rfl) y
    exact B.Om.trans k1 (B.Om.symm k2)

end Classif

end Found

#print axioms Found.Classif.coreOmega
#print axioms Found.Classif.isT_resp
#print axioms Found.Classif.unique

found/Classifying.lean

lean_all/found/Classifying.lean

/-
  T194, classifying locales. found/Classifying — a propositional geometric
  theory T (sequents φ ⊢ ψ of geometric formulas, found/GeometricLogic)
  has a classifying locale, presented by its syntax:
  * geometric derivability ⊢_T (order, ⊤, ∧, ⋁, frame distributivity, the
    axioms of T);
  * soundness: in every frame, an interpretation of the atoms satisfying
    the axioms satisfies every derivable sequent (a model of T);
  * universal property: models of T in a frame L correspond to the maps
    from the syntactic frame to L preserving ⊢_T, ⊤, ∧ and ⋁ (geometric
    morphisms into the classifying locale): every model v gives such a map
    (interp v), every such map h is (up to ≤ both ways) interp of the model
    h ∘ atom, and the round trip on models is the identity.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.GeometricLogic

namespace Found

namespace Cls

open Loc Geo

universe u

/-- a propositional geometric theory: a set of sequents -/
abbrev Theory : Type (u + 1) := GF.{u} → GF.{u} → Prop

/-- geometric derivability from T -/
inductive GD (T : Theory.{u}) : GF.{u} → GF.{u} → Prop where
  | refl (a) : GD T a a
  | trans {a b c} : GD T a b → GD T b c → GD T a c
  | top (a) : GD T a .top
  | andL (a b) : GD T (.and a b) a
  | andR (a b) : GD T (.and a b) b
  | andI {a b c} : GD T c a → GD T c b → GD T c (.and a b)
  | supI {I : Type u} (f : I → GF.{u}) (i : I) : GD T (f i) (.sup f)
  | supE {I : Type u} (f : I → GF.{u}) (c) : (∀ i, GD T (f i) c) → GD T (.sup f) c
  | dist {I : Type u} (a) (f : I → GF.{u}) : GD T (.and a (.sup f)) (.sup (fun i => .and a (f i)))
  | ax {a b} : T a b → GD T a b

/-- a model of T in a frame -/
def Model (T : Theory.{u}) (L : Frame.{u}) (v : Nat → L.car) : Prop :=
  ∀ a b, T a b → L.le (interp L v a) (interp L v b)

/-- soundness -/
theorem sound (T : Theory.{u}) (L : Frame.{u}) (v : Nat → L.car) (hv : Model T L v) {a b : GF.{u}}
    (d : GD T a b) : L.le (interp L v a) (interp L v b) := by
  induction d with
  | refl => exact L.le_refl _
  | trans _ _ ih1 ih2 => exact L.le_trans ih1 ih2
  | top => exact L.le_top _
  | andL => exact L.meet_l _ _
  | andR => exact L.meet_r _ _
  | andI _ _ ih1 ih2 => exact L.meet_univ ih1 ih2
  | supI f i => exact L.sup_ub (fun i => interp L v (f i)) i
  | supE f c _ ih => exact L.sup_least _ _ ih
  | dist a f => exact L.distrib _ _
  | ax h => exact hv _ _ h

/-- maps out of the syntactic frame preserving the structure (geometric morphisms into it) -/
structure SHom (T : Theory.{u}) (L : Frame.{u}) where
  h : GF.{u} → L.car
  mono : ∀ {a b}, GD T a b → L.le (h a) (h b)
  top : L.le L.top (h .top)
  meet : ∀ a b, L.le (L.meet (h a) (h b)) (h (.and a b))
  sup : ∀ {I : Type u} (f : I → GF.{u}), L.le (h (.sup f)) (L.sup (fun i => h (f i)))

/-- a model gives a structure-preserving map -/
def ofModel (T : Theory.{u}) (L : Frame.{u}) (v : Nat → L.car) (hv : Model T L v) : SHom T L where
  h := interp L v
  mono := sound T L v hv
  top := L.le_refl _
  meet _ _ := L.le_refl _
  sup _ := L.le_refl _

/-- a structure-preserving map gives a model -/
theorem toModel (T : Theory.{u}) (L : Frame.{u}) (φ : SHom T L) : Model T L (fun n => φ.h (.atom n)) :=
  fun a b hab => match unique T L φ a, unique T L φ b with
  | ⟨_, ha⟩, ⟨hb, _⟩ => L.le_trans ha (L.le_trans (φ.mono (.ax hab)) hb)
where
  /-- every structure-preserving map is interp of its atoms -/
  unique (T : Theory.{u}) (L : Frame.{u}) (φ : SHom T L) : (g : GF.{u}) →
      L.le (φ.h g) (interp L (fun n => φ.h (.atom n)) g) ∧ L.le (interp L (fun n => φ.h (.atom n)) g) (φ.h g)
    | .atom _ => ⟨L.le_refl _, L.le_refl _⟩
    | .top => ⟨L.le_top _, φ.top⟩
    | .and a b =>
      ⟨L.meet_univ (L.le_trans (φ.mono (.andL a b)) (unique T L φ a).1)
          (L.le_trans (φ.mono (.andR a b)) (unique T L φ b).1),
       L.le_trans (L.meet_univ (L.le_trans (L.meet_l _ _) (unique T L φ a).2)
          (L.le_trans (L.meet_r _ _) (unique T L φ b).2)) (φ.meet a b)⟩
    | .sup f =>
      ⟨L.le_trans (φ.sup f) (L.sup_least _ _ (fun i =>
          L.le_trans (unique T L φ (f i)).1 (L.sup_ub (fun i => interp L (fun n => φ.h (.atom n)) (f i)) i))),
       L.sup_least _ _ (fun i => L.le_trans (unique T L φ (f i)).2 (φ.mono (.supI f i)))⟩

/-- the universal property: the map of the model of a map is the map (both inequalities) -/
theorem classify (T : Theory.{u}) (L : Frame.{u}) (φ : SHom T L) (g : GF.{u}) :
    L.le (φ.h g) ((ofModel T L _ (toModel T L φ)).h g) ∧ L.le ((ofModel T L _ (toModel T L φ)).h g) (φ.h g) :=
  toModel.unique T L φ g

/-- and the model of the map of a model is the model (on atoms, definitionally) -/
theorem classify_model (T : Theory.{u}) (L : Frame.{u}) (v : Nat → L.car) (hv : Model T L v) (n : Nat) :
    (ofModel T L v hv).h (.atom n) = v n := rfl

end Cls

end Found

#print axioms Found.Cls.sound
#print axioms Found.Cls.ofModel
#print axioms Found.Cls.toModel
#print axioms Found.Cls.classify
#print axioms Found.Cls.classify_model

found/Coalgebra.lean

lean_all/found/Coalgebra.lean

/-
  T156. found/Coalgebra — coalgebras of an endofunctor F on the category
  parameter C (any E-category), generally.

  * Coalgebras, homomorphisms (identity and composites are homomorphisms).
  * Final coalgebras are unique up to isomorphism.
  * Lambek's lemma: the structure map of a final coalgebra is an isomorphism.
  Instances proved elsewhere and not re-proved: streams (T59 core, unfold
  uniqueness) and hypersets (T105, unique decorations = finality of the
  powerset coalgebra); the dual (initial algebras) is the core's W (T59) and
  the NNO (T86).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Cat

namespace Found

namespace Coalg

universe u v

variable {C : ECat.{u, v}} (F : EFun C C)

structure Co where
  X : C.Ob
  str : C.Hom X (F.obj X)

def IsHom (A B : Co F) (h : C.Hom A.X B.X) : Prop :=
  C.heq (C.comp A.str (F.map h)) (C.comp h B.str)

theorem id_hom (A : Co F) : IsHom F A A (C.id A.X) :=
  C.htrans (C.rcong _ (F.map_id _)) (C.htrans (C.comp_id _) (C.hsymm (C.id_comp _)))

theorem comp_hom {A B D : Co F} {h : C.Hom A.X B.X} {k : C.Hom B.X D.X}
    (hh : IsHom F A B h) (hk : IsHom F B D k) : IsHom F A D (C.comp h k) :=
  C.htrans (C.rcong _ (F.map_comp h k))
  (C.htrans (C.hsymm (C.assoc _ _ _))
  (C.htrans (C.lcong hh _)
  (C.htrans (C.assoc _ _ _)
  (C.htrans (C.rcong h hk) (C.hsymm (C.assoc _ _ _))))))

structure Final where
  Z : Co F
  out : (A : Co F) → C.Hom A.X Z.X
  out_hom : ∀ A, IsHom F A Z (out A)
  uniq : ∀ A (h : C.Hom A.X Z.X), IsHom F A Z h → C.heq h (out A)

variable {F}

theorem endo_id (T : Final F) (h : C.Hom T.Z.X T.Z.X) (hh : IsHom F T.Z T.Z h) :
    C.heq h (C.id T.Z.X) :=
  C.htrans (T.uniq _ h hh) (C.hsymm (T.uniq _ _ (id_hom F T.Z)))

/-- final coalgebras are unique up to isomorphism -/
theorem final_iso (T S : Final F) :
    C.heq (C.comp (S.out T.Z) (T.out S.Z)) (C.id T.Z.X) :=
  endo_id T _ (comp_hom F (S.out_hom T.Z) (T.out_hom S.Z))

/-- Lambek's lemma: the structure map of a final coalgebra is invertible -/
theorem lambek (T : Final F) :
    C.heq (C.comp T.Z.str (T.out ⟨F.obj T.Z.X, F.map T.Z.str⟩)) (C.id T.Z.X) ∧
    C.heq (C.comp (T.out ⟨F.obj T.Z.X, F.map T.Z.str⟩) T.Z.str) (C.id (F.obj T.Z.X)) := by
  let A : Co F := ⟨F.obj T.Z.X, F.map T.Z.str⟩
  have hu : IsHom F A T.Z (T.out A) := T.out_hom A
  have hz : IsHom F T.Z A T.Z.str := C.hrefl _
  have h1 : C.heq (C.comp T.Z.str (T.out A)) (C.id T.Z.X) := endo_id T _ (comp_hom F hz hu)
  refine ⟨h1, ?_⟩
  exact C.htrans (C.hsymm hu)
    (C.htrans (C.hsymm (F.map_comp _ _)) (C.htrans (F.map_resp h1) (F.map_id _)))

end Coalg

end Found

#print axioms Found.Coalg.id_hom
#print axioms Found.Coalg.comp_hom
#print axioms Found.Coalg.endo_id
#print axioms Found.Coalg.final_iso
#print axioms Found.Coalg.lambek

found/Compactness.lean

lean_all/found/Compactness.lean

/-
  T94. found/Compactness — compactness as a point (propositional, known
  range; Löwenheim–Skolem and first-order compactness are cited).
  * Finiteness: a derivation from Γ uses a finite list of hypotheses from Γ
    (constructive extraction by Prop-elimination on the cases).
  * Strong completeness: Γ ⊢ φ ⟺ φ is ⊤ in every Heyting algebra under every
    valuation that makes all of Γ ⊤ (Lindenbaum algebra of Γ, T191).
  * Compactness: semantic consequence from Γ is consequence from a finite
    subset.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.AlgebraicLogic

namespace Found

namespace Prp

theorem mem_app_l {α : Type} {x : α} {l₁ l₂ : List α} (h : x ∈ l₁) : x ∈ l₁ ++ l₂ := by
  induction h with
  | head => exact .head _
  | tail _ _ ih => exact .tail _ ih

theorem mem_app_r {α : Type} {x : α} (l₁ : List α) {l₂ : List α} (h : x ∈ l₂) : x ∈ l₁ ++ l₂ := by
  induction l₁ with
  | nil => exact h
  | cons _ _ ih => exact .tail _ ih

theorem mem_app_inv {α : Type} {x : α} : (l₁ l₂ : List α) → x ∈ l₁ ++ l₂ → x ∈ l₁ ∨ x ∈ l₂
  | [], _, h => Or.inr h
  | _ :: l, l₂, h => by
    cases h with
    | head => exact Or.inl (.head _)
    | tail _ h' => exact (mem_app_inv l l₂ h').elim (fun k => Or.inl (.tail _ k)) Or.inr

/-- drop the discharged hypothesis -/
theorem prune {Γ : Form → Prop} (a : Form) : (l : List Form) → (∀ x, x ∈ l → x = a ∨ Γ x) →
    ∃ l' : List Form, (∀ x, x ∈ l' → Γ x) ∧ ∀ x, x ∈ l → x = a ∨ x ∈ l'
  | [], _ => ⟨[], (fun _ (h : _ ∈ ([] : List Form)) => nomatch h), (fun _ (h : _ ∈ ([] : List Form)) => nomatch h)⟩
  | y :: l, h =>
    match prune a l (fun x m => h x (.tail _ m)) with
    | ⟨l', h1, h2⟩ =>
      (h y (.head _)).elim
        (fun e => ⟨l', h1, fun x m => by
          cases m with
          | head => exact Or.inl e
          | tail _ m' => exact h2 x m'⟩)
        (fun g => ⟨y :: l', fun x m => by
            cases m with
            | head => exact g
            | tail _ m' => exact h1 x m',
          fun x m => by
            cases m with
            | head => exact Or.inr (.head _)
            | tail _ m' => exact (h2 x m').elim Or.inl (fun k => Or.inr (.tail _ k))⟩)

/-- finiteness of derivations -/
theorem finite_hyps {I : Bool} {Γ : Form → Prop} {φ : Form} (d : PD I Γ φ) :
    ∃ l : List Form, (∀ x, x ∈ l → Γ x) ∧ PD I (fun x => x ∈ l) φ := by
  induction d with
  | @hyp _ a g =>
    refine ⟨[a], ⟨fun x (m : x ∈ [a]) => ?_, .hyp (.head _)⟩⟩
    cases m with
    | head => exact g
    | tail _ m' => exact nomatch m'
  | botE e _ ih => match ih with | ⟨l, h, d⟩ => exact ⟨l, h, .botE e d⟩
  | andI _ _ iha ihb => match iha, ihb with
    | ⟨l₁, h₁, d₁⟩, ⟨l₂, h₂, d₂⟩ =>
      exact ⟨l₁ ++ l₂, fun x m => (mem_app_inv l₁ l₂ m).elim (h₁ x) (h₂ x),
        .andI (mono d₁ (fun _ k => mem_app_l k)) (mono d₂ (fun _ k => mem_app_r l₁ k))⟩
  | andE1 _ ih => match ih with | ⟨l, h, d⟩ => exact ⟨l, h, .andE1 d⟩
  | andE2 _ ih => match ih with | ⟨l, h, d⟩ => exact ⟨l, h, .andE2 d⟩
  | orI1 _ ih => match ih with | ⟨l, h, d⟩ => exact ⟨l, h, .orI1 d⟩
  | orI2 _ ih => match ih with | ⟨l, h, d⟩ => exact ⟨l, h, .orI2 d⟩
  | @orE _ a b _ _ _ _ ih iha ihb =>
    match ih, iha, ihb with
    | ⟨l, h, d⟩, ⟨la, ha, da⟩, ⟨lb, hb, db⟩ =>
      match prune a la ha, prune b lb hb with
      | ⟨la', ha1, ha2⟩, ⟨lb', hb1, hb2⟩ =>
        exact ⟨l ++ (la' ++ lb'),
          fun x m => (mem_app_inv _ _ m).elim (h x) (fun m' => (mem_app_inv _ _ m').elim (ha1 x) (hb1 x)),
          .orE (mono d (fun _ k => mem_app_l k))
            (mono da (fun x k => (ha2 x k).elim Or.inl (fun k' => Or.inr (mem_app_r l (mem_app_l k')))))
            (mono db (fun x k => (hb2 x k).elim Or.inl (fun k' => Or.inr (mem_app_r l (mem_app_r la' k')))))⟩
  | @impI _ a _ _ ih =>
    match ih with
    | ⟨l, h, d⟩ => match prune a l h with
      | ⟨l', h1, h2⟩ => exact ⟨l', h1, .impI (mono d (fun x k => (h2 x k).elim Or.inl Or.inr))⟩
  | impE _ _ ihf iha => match ihf, iha with
    | ⟨l₁, h₁, d₁⟩, ⟨l₂, h₂, d₂⟩ =>
      exact ⟨l₁ ++ l₂, fun x m => (mem_app_inv l₁ l₂ m).elim (h₁ x) (h₂ x),
        .impE (mono d₁ (fun _ k => mem_app_l k)) (mono d₂ (fun _ k => mem_app_r l₁ k))⟩

/-- semantic consequence in all Heyting algebras -/
def Cons (Γ : Form → Prop) (φ : Form) : Prop :=
  ∀ (H : HA.{0}) (v : Nat → H.car), (∀ ψ, Γ ψ → H.le H.top (interp H v H.bot ψ)) →
    H.le H.top (interp H v H.bot φ)

/-- strong completeness -/
theorem strong_completeness (Γ : Form → Prop) (φ : Form) : Cons Γ φ → PD true Γ φ := by
  intro h
  have k : PD true Γ (.imp (.imp .bot .bot) (interp (LT Γ) Form.var Form.bot φ)) :=
    h (LT Γ) Form.var (fun ψ g => by
      show PD true Γ (.imp (.imp .bot .bot) (interp (LT Γ) Form.var Form.bot ψ))
      rw [interp_LT]
      exact .impI (.hyp (Or.inr g)))
  rw [interp_LT] at k
  exact .impE k (.impI h0)

/-- compactness: consequence from Γ is consequence from a finite part of Γ -/
theorem compactness (Γ : Form → Prop) (φ : Form) (h : Cons Γ φ) :
    ∃ l : List Form, (∀ x, x ∈ l → Γ x) ∧ Cons (fun x => x ∈ l) φ :=
  match finite_hyps (strong_completeness Γ φ h) with
  | ⟨l, hl, d⟩ => ⟨l, hl, fun H v hv => soundness H d v H.bot (fun _ => H.bot_le) H.top hv⟩

end Prp

end Found

#print axioms Found.Prp.prune
#print axioms Found.Prp.finite_hyps
#print axioms Found.Prp.strong_completeness
#print axioms Found.Prp.compactness

found/Completeness.lean

lean_all/found/Completeness.lean

/-
  T93. found/Completeness — completeness as a point of model theory,
  propositional and algebraic (known range; first-order Gödel/Kripke
  completeness is cited and needs prime filters / LEM in the metatheory).
  * Intuitionistic: ⊢ φ ⟺ valid in every Heyting algebra (T191, reused).
  * Classical: ⊢_c φ ⟺ valid in every Boolean Heyting algebra: soundness
    T66, completeness via the Lindenbaum algebra of the DNE theory, which is
    Boolean.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.AlgebraicLogic

namespace Found

namespace Prp

/-- the Lindenbaum algebra of the DNE theory is Boolean -/
theorem lt_dne_boolean : Boolean (LT (DNE (fun _ => False))) :=
  fun a => .hyp (dne_has _ a)

theorem interp_LT' (φ : Form) : interp (LT (DNE (fun _ => False))) Form.var Form.bot φ = φ :=
  interp_LT _ φ

/-- classical completeness for Boolean algebras -/
theorem classical_completeness (φ : Form)
    (h : ∀ (H : HA.{0}), Boolean H → ∀ v : Nat → H.car, H.le H.top (interp H v H.bot φ)) :
    CD (fun _ => False) φ := by
  have k : PD true (DNE (fun _ => False)) (.imp (.imp .bot .bot) (interp (LT (DNE (fun _ => False))) Form.var Form.bot φ)) :=
    h _ lt_dne_boolean Form.var
  rw [interp_LT'] at k
  exact .impE k (.impI h0)

theorem classical_iff (φ : Form) :
    CD (fun _ => False) φ ↔ ∀ (H : HA.{0}), Boolean H → ∀ v : Nat → H.car, H.le H.top (interp H v H.bot φ) :=
  ⟨fun d H hB v => cd_sound H hB d v, classical_completeness φ⟩

theorem intuitionistic_iff (φ : Form) :
    PD true (fun _ => False) φ ↔ ∀ (H : HA.{0}) (v : Nat → H.car), H.le H.top (interp H v H.bot φ) :=
  heyting_iff φ

end Prp

end Found

#print axioms Found.Prp.lt_dne_boolean
#print axioms Found.Prp.classical_completeness
#print axioms Found.Prp.classical_iff
#print axioms Found.Prp.intuitionistic_iff

found/CondCont.lean

lean_all/found/CondCont.lean

/-
  T202, continuity over profinite sets. found/CondCont — in the classical
  point, for the Cantor set 2^ℕ = lim 2^N and a discrete set X, the
  condensed value X(2^ℕ) = C(2^ℕ, X) is the colimit of the finite values
  X^(2^N):
  * a continuous (locally constant) map 2^ℕ → X is uniformly locally
    constant (fan theorem, found/Fan);
  * so it factors through the first N bits, 2^ℕ → 2^N (every element of
    C(2^ℕ, X) comes from some stage);
  * composites of finite-stage maps with the projections are continuous;
  * two finite-stage maps have the same composite iff they agree at a
    common later stage after the transition maps (the colimit relation);
  * the same for every closed subset of 2^ℕ (the branches of a pruned tree,
    i.e. every light profinite set): a non-expanding retraction onto it
    makes each continuous map on it factor through a finite stage.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Fan

namespace Found

namespace CC

open CP Fan

variable (K : Classical') {X : Type}

/-- locally constant (= continuous into a discrete set) -/
def Cont (f : (Nat → Bool) → X) : Prop := ∀ α, ∃ n, ∀ β, Agree α β n → f β = f α

/-- uniformly locally constant at depth N -/
def Unif (f : (Nat → Bool) → X) (N : Nat) : Prop := ∀ α β, Agree α β N → f α = f β

theorem agree_symm {α β : Nat → Bool} {n : Nat} (h : Agree α β n) : Agree β α n := fun i hi => (h i hi).symm

theorem agree_trans {α β γ : Nat → Bool} {n : Nat} (h : Agree α β n) (h' : Agree β γ n) : Agree α γ n :=
  fun i hi => (h i hi).trans (h' i hi)

theorem agree_le {α β : Nat → Bool} {n N : Nat} (hn : n ≤ N) (h : Agree α β N) : Agree α β n :=
  fun i hi => h i (Nat.lt_of_lt_of_le hi hn)

include K in
/-- continuous maps on Cantor space are uniformly continuous -/
theorem uniform (f : (Nat → Bool) → X) (hf : Cont f) : ∃ N, Unif f N := by
  let B : (Nat → Bool) → Nat → Prop := fun α n => ∀ β, Agree α β n → f β = f α
  have loc : ∀ α β n, (∀ i, i < n → α i = β i) → B α n → B β n := fun α β n hab hα γ hβγ =>
    (hα γ (agree_trans hab hβγ)).trans (hα β hab).symm
  match fan K B loc hf with
  | ⟨N, hN⟩ => exact ⟨N, fun α β h => match hN α with
    | ⟨_, hn, hb⟩ => (hb β (agree_le hn h)).symm⟩

/-! ## finite stages 2^N as lists of bits -/

/-- the first n bits -/
def pre : Nat → (Nat → Bool) → List Bool
  | 0, _ => []
  | n + 1, α => α 0 :: pre n (fun i => α (i + 1))

/-- read a list as a point, padded with false -/
def nth : List Bool → Nat → Bool
  | [], _ => false
  | b :: _, 0 => b
  | _ :: l, i + 1 => nth l i

def take' : Nat → List Bool → List Bool
  | 0, _ => []
  | _ + 1, [] => []
  | n + 1, b :: l => b :: take' n l

def len : List Bool → Nat
  | [] => 0
  | _ :: l => len l + 1

theorem nth_pre : (n : Nat) → (α : Nat → Bool) → Agree α (nth (pre n α)) n
  | 0, _ => fun _ hi => absurd hi (Nat.not_lt_zero _)
  | n + 1, α => fun i hi => match i, hi with
    | 0, _ => rfl
    | i + 1, hi => nth_pre n (fun j => α (j + 1)) i (Nat.lt_of_succ_lt_succ hi)

theorem pre_congr : (n : Nat) → (α β : Nat → Bool) → Agree α β n → pre n α = pre n β
  | 0, _, _, _ => rfl
  | n + 1, α, β, h => by
    show α 0 :: pre n (fun i => α (i + 1)) = β 0 :: pre n (fun i => β (i + 1))
    rw [h 0 (Nat.succ_pos n), pre_congr n _ _ (fun i hi => h (i + 1) (Nat.succ_lt_succ hi))]

theorem len_pre : (n : Nat) → (α : Nat → Bool) → len (pre n α) = n
  | 0, _ => rfl
  | n + 1, α => congrArg (· + 1) (len_pre n (fun i => α (i + 1)))

theorem pre_nth : (l : List Bool) → pre (len l) (nth l) = l
  | [] => rfl
  | b :: l => congrArg (List.cons b) (pre_nth l)

theorem take_pre : (m L : Nat) → m ≤ L → (α : Nat → Bool) → take' m (pre L α) = pre m α
  | 0, _, _, _ => rfl
  | _ + 1, 0, h, _ => absurd h (Nat.not_succ_le_zero _)
  | m + 1, L + 1, h, α => congrArg (List.cons (α 0)) (take_pre m L (Nat.le_of_succ_le_succ h) _)

include K in
/-- every continuous map comes from a finite stage: f = h ∘ pre N -/
theorem factor (f : (Nat → Bool) → X) (hf : Cont f) :
    ∃ N, ∃ h : List Bool → X, ∀ α, f α = h (pre N α) :=
  match uniform K f hf with
  | ⟨N, hN⟩ => ⟨N, fun l => f (nth l), fun α => hN α _ (nth_pre N α)⟩

/-- conversely, every finite-stage composite is continuous -/
theorem stage_cont (N : Nat) (h : List Bool → X) : Cont (fun α => h (pre N α)) :=
  fun α => ⟨N, fun β hab => congrArg h (pre_congr N β α (agree_symm hab))⟩

/-- two finite-stage maps with the same composite agree on 2^(N+M) after the transition maps -/
theorem colim_eq (N M : Nat) (h h' : List Bool → X) (e : ∀ α, h (pre N α) = h' (pre M α))
    (l : List Bool) (hl : len l = N + M) : h (take' N l) = h' (take' M l) := by
  have hl' : pre (N + M) (nth l) = l := hl ▸ pre_nth l
  rw [← hl', take_pre N (N + M) (Nat.le_add_right N M), take_pre M (N + M) (Nat.le_add_left M N)]
  exact e (nth l)

/-- and conversely: agreement at a common stage gives the same composite -/
theorem colim_eq' (N M : Nat) (h h' : List Bool → X)
    (e : ∀ l : List Bool, len l = N + M → h (take' N l) = h' (take' M l)) (α : Nat → Bool) :
    h (pre N α) = h' (pre M α) := by
  have l1 := len_pre (N + M) α
  have := e (pre (N + M) α) l1
  rw [take_pre N (N + M) (Nat.le_add_right N M), take_pre M (N + M) (Nat.le_add_left M N)] at this
  exact this

/-! ## closed subsets of 2^ℕ (light profinite sets): retraction and continuity -/

section Closed

/-- reversed prefixes: rpre (n+1) α = α n :: rpre n α -/
def rpre : Nat → (Nat → Bool) → List Bool
  | 0, _ => []
  | n + 1, α => α n :: rpre n α

variable (Tr : List Bool → Prop) (tr0 : Tr []) (prune : ∀ l, Tr l → Tr (true :: l) ∨ Tr (false :: l))

/-- the closed set of branches of a pruned tree -/
def InC (α : Nat → Bool) : Prop := ∀ n, Tr (rpre n α)

/-- the retraction: follow α while staying in the tree, else take the other bit -/
def pr (α : Nat → Bool) : Nat → List Bool
  | 0 => []
  | n + 1 => cond (dec K (Tr (α n :: pr α n))) (α n :: pr α n) ((!α n) :: pr α n)

def r (α : Nat → Bool) (n : Nat) : Bool := cond (dec K (Tr (α n :: pr K Tr α n))) (α n) (!α n)

theorem pr_succ (α : Nat → Bool) (n : Nat) : pr K Tr α (n + 1) = r K Tr α n :: pr K Tr α n := by
  show cond (dec K (Tr (α n :: pr K Tr α n))) (α n :: pr K Tr α n) ((!α n) :: pr K Tr α n) =
    cond (dec K (Tr (α n :: pr K Tr α n))) (α n) (!α n) :: pr K Tr α n
  cases dec K (Tr (α n :: pr K Tr α n)) <;> rfl

include tr0 prune in
theorem pr_tree (α : Nat → Bool) : (n : Nat) → Tr (pr K Tr α n)
  | 0 => tr0
  | n + 1 => by
    rw [pr_succ]
    show Tr (cond (dec K (Tr (α n :: pr K Tr α n))) (α n) (!α n) :: pr K Tr α n)
    have ih := pr_tree α n
    cases hd : dec K (Tr (α n :: pr K Tr α n)) with
    | true => exact (dec_spec K _).1 hd
    | false =>
      have hn : ¬ Tr (α n :: pr K Tr α n) := fun h => Bool.noConfusion (hd.symm.trans ((dec_spec K _).2 h))
      show Tr ((!α n) :: pr K Tr α n)
      cases ha : α n with
      | true => rw [ha] at hn; exact (prune _ ih).elim (fun h => (hn h).elim) id
      | false => rw [ha] at hn; exact (prune _ ih).elim id (fun h => (hn h).elim)

theorem rpre_r (α : Nat → Bool) : (n : Nat) → rpre n (r K Tr α) = pr K Tr α n
  | 0 => rfl
  | n + 1 => by
    rw [pr_succ]
    exact congrArg (List.cons (r K Tr α n)) (rpre_r α n)

include tr0 prune in
/-- the retraction lands in the closed set -/
theorem r_in (α : Nat → Bool) : InC Tr (r K Tr α) := fun n => by
  rw [rpre_r]
  exact pr_tree K Tr tr0 prune α n

theorem pr_on (α : Nat → Bool) (h : InC Tr α) : (n : Nat) → pr K Tr α n = rpre n α
  | 0 => rfl
  | n + 1 => by
    rw [pr_succ]
    show r K Tr α n :: pr K Tr α n = α n :: rpre n α
    have ih := pr_on α h n
    have ht : dec K (Tr (α n :: pr K Tr α n)) = true := (dec_spec K _).2 (by rw [ih]; exact h (n + 1))
    show cond (dec K (Tr (α n :: pr K Tr α n))) (α n) (!α n) :: pr K Tr α n = α n :: rpre n α
    rw [ht, ih]
    rfl

/-- the retraction is the identity on the closed set (pointwise) -/
theorem r_on (α : Nat → Bool) (h : InC Tr α) (n : Nat) : r K Tr α n = α n := by
  show cond (dec K (Tr (α n :: pr K Tr α n))) (α n) (!α n) = α n
  have ht : dec K (Tr (α n :: pr K Tr α n)) = true := (dec_spec K _).2 (by rw [pr_on K Tr α h n]; exact h (n + 1))
  rw [ht]
  rfl

theorem pr_agree (α β : Nat → Bool) (n : Nat) (h : Agree α β n) : (k : Nat) → k ≤ n → pr K Tr α k = pr K Tr β k
  | 0, _ => rfl
  | k + 1, hk => by
    have ih := pr_agree α β n h k (Nat.le_of_succ_le hk)
    show cond (dec K (Tr (α k :: pr K Tr α k))) (α k :: pr K Tr α k) ((!α k) :: pr K Tr α k) =
      cond (dec K (Tr (β k :: pr K Tr β k))) (β k :: pr K Tr β k) ((!β k) :: pr K Tr β k)
    rw [ih, h k hk]

/-- the retraction is non-expanding: it preserves agreement on the first n bits -/
theorem r_agree (α β : Nat → Bool) (n : Nat) (h : Agree α β n) : Agree (r K Tr α) (r K Tr β) n := fun k hk => by
  show cond (dec K (Tr (α k :: pr K Tr α k))) (α k) (!α k) = cond (dec K (Tr (β k :: pr K Tr β k))) (β k) (!β k)
  rw [pr_agree K Tr α β n h k (Nat.le_of_lt hk), h k hk]

/-- continuity on the closed set -/
def ContC (f : {α // InC Tr α} → X) : Prop :=
  ∀ a : {α // InC Tr α}, ∃ n, ∀ b : {α // InC Tr α}, Agree a.val b.val n → f b = f a

include K tr0 prune in
/-- every continuous map on a closed subset of 2^ℕ factors through a finite stage 2^N -/
theorem factorC (f : {α // InC Tr α} → X) (hf : ContC Tr f) :
    ∃ N, ∃ h : List Bool → X, ∀ a : {α // InC Tr α}, f a = h (pre N a.val) := by
  let g : (Nat → Bool) → X := fun α => f ⟨r K Tr α, r_in K Tr tr0 prune α⟩
  have hg : Cont g := fun α => match hf ⟨r K Tr α, r_in K Tr tr0 prune α⟩ with
    | ⟨n, hn⟩ => ⟨n, fun β hab => hn ⟨r K Tr β, r_in K Tr tr0 prune β⟩ (r_agree K Tr α β n hab)⟩
  match factor K g hg with
  | ⟨N, h, hh⟩ => exact ⟨N, h, fun a => by
      rw [← hh a.val]
      match hf a with
      | ⟨n, hn⟩ => exact (hn ⟨r K Tr a.val, r_in K Tr tr0 prune a.val⟩
          (fun i _ => (r_on K Tr a.val a.property i).symm)).symm⟩

end Closed

end CC

end Found

#print axioms Found.CC.uniform
#print axioms Found.CC.nth_pre
#print axioms Found.CC.pre_congr
#print axioms Found.CC.pre_nth
#print axioms Found.CC.take_pre
#print axioms Found.CC.factor
#print axioms Found.CC.stage_cont
#print axioms Found.CC.colim_eq
#print axioms Found.CC.colim_eq'
#print axioms Found.CC.pr_tree
#print axioms Found.CC.r_in
#print axioms Found.CC.r_on
#print axioms Found.CC.r_agree
#print axioms Found.CC.factorC

found/CondDescent.lean

lean_all/found/CondDescent.lean

/-
  T202 line. found/CondDescent — the descent half of the condensed sheaf
  condition, at the level of sets, in the classical point: for a surjection
  f : T → S, maps g : T → X constant on the fibres of f (agreeing on the
  kernel pair T ×_S T) are exactly the maps that factor through f, uniquely
  (pointwise).  With found/Condensed (finite coproducts to products) this is
  the sheaf condition for S ↦ (S → X) in the coherent topology; continuity
  on profinite sets is the remaining part (cited).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.ClassicalPoint

namespace Found

namespace CD

open CP

variable (K : Classical') {S T X : Type} (f : T → S) (surj : ∀ s, ∃ t, f t = s)

/-- the descended map: choose a preimage -/
def desc (g : T → X) (s : S) : X := g (K.ch (fun t => f t = s) (surj s))

/-- existence: a fibre-constant map factors through f -/
theorem desc_factor (g : T → X) (hg : ∀ t t', f t = f t' → g t = g t') (t : T) :
    desc K f surj g (f t) = g t :=
  hg _ _ (K.ch_spec (fun t' => f t' = f t) (surj (f t)))

include surj in
/-- uniqueness: two maps agreeing after f agree -/
theorem desc_unique (h h' : S → X) (e : ∀ t, h (f t) = h' (f t)) (s : S) : h s = h' s :=
  match surj s with
  | ⟨t, ht⟩ => by rw [← ht]; exact e t

/-- and every map factoring through f is fibre-constant: the equalizer description -/
theorem factor_const (h : S → X) (t t' : T) (e : f t = f t') : h (f t) = h (f t') := by rw [e]

end CD

end Found

#print axioms Found.CD.desc_factor
#print axioms Found.CD.desc_unique

found/Condensed.lean

lean_all/found/Condensed.lean

/-
  T202. found/Condensed — condensed (and pyknotic) sets as a point, at the
  level reachable without topology: a condensed set is a presheaf on
  profinite sets sending finite disjoint unions to products (plus a descent
  condition for surjections, cited).
  * For any type X, S ↦ (S → X) sends S ⊕ T to the product: the setoid
    isomorphism (S ⊕ T → X) ≅ (S → X) × (T → X), pointwise (no funext),
    and the empty union to the point.
  Profinite sets, the descent condition and the comparison with
  topological spaces are cited.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Core

namespace Found

namespace Cond2

open Core

def funS (S X : Type) : Setd.{0} := ⟨S → X, fun f g => ∀ s, f s = g s, fun _ _ => rfl,
  fun h s => (h s).symm, fun h k s => (h s).trans (k s)⟩

def split {S T X : Type} : SHom (funS (S ⊕ T) X) (prodS (funS S X) (funS T X)) :=
  ⟨fun f => (fun s => f (.inl s), fun t => f (.inr t)), fun h => ⟨fun _ => h _, fun _ => h _⟩⟩

def join {S T X : Type} : SHom (prodS (funS S X) (funS T X)) (funS (S ⊕ T) X) :=
  ⟨fun p u => match u with | .inl s => p.1 s | .inr t => p.2 t,
   fun h u => match u with | .inl s => h.1 s | .inr t => h.2 t⟩

theorem sum_to_product (S T X : Type) : Iso' (funS (S ⊕ T) X) (prodS (funS S X) (funS T X)) :=
  ⟨split, join, fun _ u => match u with | .inl _ => rfl | .inr _ => rfl,
   fun _ => ⟨fun _ => rfl, fun _ => rfl⟩⟩

theorem empty_to_point (X : Type) : Iso' (funS Empty X) unitS :=
  ⟨⟨fun _ => PUnit.unit, fun _ => trivial⟩, ⟨(fun _ (e : Empty) => nomatch e), (fun _ (e : Empty) => nomatch e)⟩,
   (fun _ (e : Empty) => nomatch e), (fun _ => trivial)⟩

end Cond2

end Found

#print axioms Found.Cond2.sum_to_product
#print axioms Found.Cond2.empty_to_point

found/Conditional.lean

lean_all/found/Conditional.lean

/-
  T172. found/Conditional — conditional / counterfactual logic as a point:
  a selection function on the relation-coordinate worlds,
  A > B at w :⟺ f(w, A) ⊆ B, with success f(w, A) ⊆ A.
  * Identity A > A holds; antecedent strengthening fails (countermodel),
    so > is not strict implication (□(A → B) would strengthen).

  Lean 4 core only, hand-written proofs, no axioms.
-/

namespace Found

namespace Cond

universe u

variable {W : Type u}

structure Sel (W : Type u) where
  f : W → (W → Prop) → (W → Prop)
  success : ∀ w A x, f w A x → A x

def cond (S : Sel W) (A B : W → Prop) (w : W) : Prop := ∀ x, S.f w A x → B x

theorem identity (S : Sel W) (A : W → Prop) (w : W) : cond S A A w := fun x h => S.success w A x h

/-- prefer the world `true` whenever the antecedent allows it -/
def sel : Sel Bool where
  f _ P x := P x ∧ (P true → x = true)
  success _ _ _ h := h.1

theorem no_strengthening :
    cond sel (fun _ => True) (fun x => x = true) false ∧
    ¬ cond sel (fun x => True ∧ x = false) (fun x => x = true) false :=
  ⟨fun _ h => h.2 trivial,
   fun h => Bool.noConfusion (h false ⟨⟨trivial, rfl⟩, fun hp => Bool.noConfusion hp.2⟩)⟩

end Cond

end Found

#print axioms Found.Cond.identity
#print axioms Found.Cond.no_strengthening

found/Core.lean

lean_all/found/Core.lean

/-
  T59. found/Core — the core: the stem (setoids, any universe u, maps
  respecting ≈, equality of maps pointwise) is closed under the seven
  operations, each with its universal property.

    1. products, sums            (prodS from CCC; sumS here)
    2. function spaces           (expS / setdCCC from CCC, universe-polymorphic)
    3. subobjects, power (Ω)     (subU, OmegaU = ULift Prop with ↔; power = X ⇒ Ω)
    4. quotients                 (by any coarser equivalence)
    5. induction, coinduction    (W-types: initial algebras; streams: final coalgebras)
    6. universes                 (the setoid of small setoids, ≈ = isomorphism)
    7. sheaves, localization     (localization at any relation; nuclei: T108)

  The same statements are checked in Cubical Agda: work/agda/Core.agda.
  Lean 4 core only, hand-written proofs, no axioms.
  CIC dependence: Ω = ULift Prop (impredicative Prop) in 3; W-types are Lean inductives.
-/
import found.CCC

namespace Found

namespace Core

universe u

/-! ## 1. Sums (products: prodS, CCC) -/

def sumEqv (X Y : Setd.{u}) : X.car ⊕ Y.car → X.car ⊕ Y.car → Prop
  | .inl a, .inl b => X.eqv a b
  | .inl _, .inr _ => False
  | .inr _, .inl _ => False
  | .inr a, .inr b => Y.eqv a b

def sumS (X Y : Setd.{u}) : Setd.{u} where
  car := X.car ⊕ Y.car
  eqv := sumEqv X Y
  refl p := by cases p with
    | inl a => exact X.refl a
    | inr b => exact Y.refl b
  symm {p q} h := by
    cases p <;> cases q
    · exact X.symm h
    · exact h.elim
    · exact h.elim
    · exact Y.symm h
  trans {p q r} h k := by
    cases p <;> cases q <;> cases r
    · exact X.trans h k
    · exact k.elim
    · exact h.elim
    · exact h.elim
    · exact h.elim
    · exact h.elim
    · exact k.elim
    · exact Y.trans h k

def inl (X Y : Setd.{u}) : SHom X (sumS X Y) := ⟨Sum.inl, fun h => h⟩
def inr (X Y : Setd.{u}) : SHom Y (sumS X Y) := ⟨Sum.inr, fun h => h⟩

def copair {X Y Z : Setd.{u}} (f : SHom X Z) (g : SHom Y Z) : SHom (sumS X Y) Z where
  fn p := match p with
    | .inl a => f.fn a
    | .inr b => g.fn b
  resp {p q} h := by
    cases p <;> cases q
    · exact f.resp h
    · exact h.elim
    · exact h.elim
    · exact g.resp h

theorem copair_inl {X Y Z : Setd.{u}} (f : SHom X Z) (g : SHom Y Z) :
    SetdCat.heq (SetdCat.comp (inl X Y) (copair f g)) f := fun a => Z.refl (f.fn a)

theorem copair_inr {X Y Z : Setd.{u}} (f : SHom X Z) (g : SHom Y Z) :
    SetdCat.heq (SetdCat.comp (inr X Y) (copair f g)) g := fun b => Z.refl (g.fn b)

theorem copair_uniq {X Y Z : Setd.{u}} (f : SHom X Z) (g : SHom Y Z) (h : SHom (sumS X Y) Z)
    (hf : SetdCat.heq (SetdCat.comp (inl X Y) h) f) (hg : SetdCat.heq (SetdCat.comp (inr X Y) h) g) :
    SetdCat.heq h (copair f g)
  | .inl a => hf a
  | .inr b => hg b

/-- products are the CCC's (universe-polymorphic) -/
def prod (X Y : Setd.{u}) : Product SetdCat.{u} X Y := setdProduct X Y

/-! ## 2. Function spaces -/

def ccc : CCC SetdCat.{u} := setdCCC

/-! ## 3. Subobjects and power -/

def OmegaU : Setd.{u} :=
  ⟨ULift.{u} Prop, fun p q => (p.down ↔ q.down), fun _ => Iff.rfl, fun h => h.symm, fun h k => h.trans k⟩

def RespP (X : Setd.{u}) (φ : X.car → Prop) : Prop := ∀ {x y}, X.eqv x y → φ x → φ y

def subU (X : Setd.{u}) (φ : X.car → Prop) : Setd.{u} :=
  ⟨{x // φ x}, fun a b => X.eqv a.val b.val, fun a => X.refl a.val, fun h => X.symm h, fun h k => X.trans h k⟩

def chiU {X : Setd.{u}} (φ : X.car → Prop) (h : RespP X φ) : SHom X OmegaU :=
  ⟨fun x => ⟨φ x⟩, fun e => ⟨h e, h (X.symm e)⟩⟩

/-- classification: membership in the subobject is truth of χ -/
theorem chiU_spec {X : Setd.{u}} (φ : X.car → Prop) (h : RespP X φ) (x : X.car) :
    (∃ s : (subU X φ).car, X.eqv s.val x) ↔ ((chiU φ h).fn x).down :=
  ⟨fun ⟨s, e⟩ => h e s.property, fun p => ⟨⟨x, p⟩, X.refl x⟩⟩

/-- uniqueness of χ: predicates with equivalent characteristic maps agree -/
theorem chiU_uniq {X : Setd.{u}} (φ ψ : X.car → Prop) (hφ : RespP X φ) (hψ : RespP X ψ)
    (e : SetdCat.heq (chiU φ hφ) (chiU ψ hψ)) (x : X.car) : φ x ↔ ψ x := e x

def power (X : Setd.{u}) : Setd.{u} := expS X OmegaU

/-! ## 4. Quotients -/

structure Coarser (X : Setd.{u}) where
  R : X.car → X.car → Prop
  refl : ∀ a, R a a
  symm : ∀ {a b}, R a b → R b a
  trans : ∀ {a b c}, R a b → R b c → R a c
  coarser : ∀ {a b}, X.eqv a b → R a b

def quot {X : Setd.{u}} (C : Coarser X) : Setd.{u} := ⟨X.car, C.R, C.refl, C.symm, C.trans⟩

def proj {X : Setd.{u}} (C : Coarser X) : SHom X (quot C) := ⟨fun x => x, fun h => C.coarser h⟩

def qlift {X Y : Setd.{u}} (C : Coarser X) (f : SHom X Y) (hf : ∀ {a b}, C.R a b → Y.eqv (f.fn a) (f.fn b)) :
    SHom (quot C) Y := ⟨f.fn, hf⟩

theorem qlift_comm {X Y : Setd.{u}} (C : Coarser X) (f : SHom X Y)
    (hf : ∀ {a b}, C.R a b → Y.eqv (f.fn a) (f.fn b)) :
    SetdCat.heq (SetdCat.comp (proj C) (qlift C f hf)) f := fun x => Y.refl (f.fn x)

theorem qlift_uniq {X Y : Setd.{u}} (C : Coarser X) (f : SHom X Y)
    (hf : ∀ {a b}, C.R a b → Y.eqv (f.fn a) (f.fn b)) (g : SHom (quot C) Y)
    (hg : SetdCat.heq (SetdCat.comp (proj C) g) f) : SetdCat.heq g (qlift C f hf) := hg

/-! ## 5a. Induction: W-types are initial algebras -/

inductive W (A : Type u) (B : A → Type u) : Type u where
  | sup (a : A) (f : B a → W A B) : W A B

def WS (A : Type u) (B : A → Type u) : Setd.{u} :=
  ⟨W A B, Eq, fun _ => rfl, fun h => h.symm, fun h k => h.trans k⟩

/-- an algebra for the polynomial functor on a setoid -/
structure Alg (A : Type u) (B : A → Type u) where
  X : Setd.{u}
  alg : (a : A) → (B a → X.car) → X.car

def fold {A : Type u} {B : A → Type u} (M : Alg A B) : W A B → M.X.car
  | .sup a f => M.alg a (fun b => fold M (f b))

theorem fold_eq {A : Type u} {B : A → Type u} (M : Alg A B) (a : A) (f : B a → W A B) :
    fold M (.sup a f) = M.alg a (fun b => fold M (f b)) := rfl

/-- uniqueness of the fold (algebra map), up to ≈, when alg respects pointwise ≈ -/
theorem fold_uniq {A : Type u} {B : A → Type u} (M : Alg A B)
    (hM : ∀ a (g g' : B a → M.X.car), (∀ b, M.X.eqv (g b) (g' b)) → M.X.eqv (M.alg a g) (M.alg a g'))
    (h : W A B → M.X.car) (hh : ∀ a f, M.X.eqv (h (.sup a f)) (M.alg a (fun b => h (f b)))) :
    ∀ w, M.X.eqv (h w) (fold M w)
  | .sup a f => M.X.trans (hh a f) (hM a _ _ (fun b => fold_uniq M hM h hh (f b)))

/-! ## 5b. Coinduction: streams are final coalgebras -/

def StreamS (A : Setd.{u}) : Setd.{u} :=
  ⟨Nat → A.car, fun s t => ∀ n, A.eqv (s n) (t n), fun s n => A.refl (s n),
   fun h n => A.symm (h n), fun h k n => A.trans (h n) (k n)⟩

def unfold {A : Setd.{u}} {S : Type u} (c : S → A.car × S) (x : S) : Nat → A.car
  | 0 => (c x).1
  | n + 1 => unfold c (c x).2 n

theorem unfold_uniq {A : Setd.{u}} {S : Type u} (c : S → A.car × S) (f : S → Nat → A.car)
    (hh : ∀ x, A.eqv (f x 0) (c x).1) (ht : ∀ x n, A.eqv (f x (n + 1)) (f (c x).2 n)) :
    ∀ n x, A.eqv (f x n) (unfold c x n)
  | 0, x => hh x
  | n + 1, x => A.trans (ht x n) (unfold_uniq c f hh ht n (c x).2)

/-! ## 6. Universes: the setoid of small setoids -/

def Iso' (X Y : Setd.{u}) : Prop :=
  ∃ f : SHom X Y, ∃ g : SHom Y X, (∀ x, X.eqv (g.fn (f.fn x)) x) ∧ (∀ y, Y.eqv (f.fn (g.fn y)) y)

theorem iso_refl (X : Setd.{u}) : Iso' X X :=
  ⟨⟨fun x => x, fun h => h⟩, ⟨fun x => x, fun h => h⟩, fun x => X.refl x, fun x => X.refl x⟩

theorem iso_symm {X Y : Setd.{u}} (h : Iso' X Y) : Iso' Y X :=
  match h with
  | ⟨f, g, a, b⟩ => ⟨g, f, b, a⟩

theorem iso_trans {X Y Z : Setd.{u}} (h : Iso' X Y) (k : Iso' Y Z) : Iso' X Z :=
  match h, k with
  | ⟨f, g, a, b⟩, ⟨f', g', a', b'⟩ =>
    ⟨SetdCat.comp f f', SetdCat.comp g' g,
     fun x => X.trans (g.resp (a' (f.fn x))) (a x),
     fun z => Z.trans (f'.resp (b (g'.fn z))) (b' z)⟩

def USetd : Setd.{u + 1} := ⟨Setd.{u}, Iso', iso_refl, iso_symm, iso_trans⟩

/-- the universe is closed under products, up to its own equality -/
theorem prod_resp {X X' Y Y' : Setd.{u}} (h : Iso' X X') (k : Iso' Y Y') :
    Iso' (prodS X Y) (prodS X' Y') :=
  match h, k with
  | ⟨f, g, a, b⟩, ⟨f', g', a', b'⟩ =>
    ⟨⟨fun p => (f.fn p.1, f'.fn p.2), fun e => ⟨f.resp e.1, f'.resp e.2⟩⟩,
     ⟨fun p => (g.fn p.1, g'.fn p.2), fun e => ⟨g.resp e.1, g'.resp e.2⟩⟩,
     fun p => ⟨a p.1, a' p.2⟩, fun p => ⟨b p.1, b' p.2⟩⟩

/-! ## 7. Localization at a relation -/

inductive Gen (X : Setd.{u}) (R : X.car → X.car → Prop) : X.car → X.car → Prop where
  | base {a b} : X.eqv a b → Gen X R a b
  | rel {a b} : R a b → Gen X R a b
  | symm {a b} : Gen X R a b → Gen X R b a
  | trans {a b c} : Gen X R a b → Gen X R b c → Gen X R a c

def loc (X : Setd.{u}) (R : X.car → X.car → Prop) : Setd.{u} :=
  ⟨X.car, Gen X R, fun a => .base (X.refl a), fun h => .symm h, fun h k => .trans h k⟩

def locMap (X : Setd.{u}) (R : X.car → X.car → Prop) : SHom X (loc X R) := ⟨fun x => x, fun h => .base h⟩

theorem gen_resp {X Y : Setd.{u}} (R : X.car → X.car → Prop) (f : SHom X Y)
    (hf : ∀ {a b}, R a b → Y.eqv (f.fn a) (f.fn b)) {a b : X.car} (h : Gen X R a b) :
    Y.eqv (f.fn a) (f.fn b) := by
  induction h with
  | base e => exact f.resp e
  | rel r => exact hf r
  | symm _ ih => exact Y.symm ih
  | trans _ _ ih₁ ih₂ => exact Y.trans ih₁ ih₂

/-- a map inverting R (sending R-related points to ≈-related ones) factors through the localization -/
def locLift {X Y : Setd.{u}} (R : X.car → X.car → Prop) (f : SHom X Y)
    (hf : ∀ {a b}, R a b → Y.eqv (f.fn a) (f.fn b)) : SHom (loc X R) Y :=
  ⟨f.fn, fun h => gen_resp R f hf h⟩

theorem locLift_comm {X Y : Setd.{u}} (R : X.car → X.car → Prop) (f : SHom X Y)
    (hf : ∀ {a b}, R a b → Y.eqv (f.fn a) (f.fn b)) :
    SetdCat.heq (SetdCat.comp (locMap X R) (locLift R f hf)) f := fun x => Y.refl (f.fn x)

theorem locLift_uniq {X Y : Setd.{u}} (R : X.car → X.car → Prop) (f : SHom X Y)
    (hf : ∀ {a b}, R a b → Y.eqv (f.fn a) (f.fn b)) (g : SHom (loc X R) Y)
    (hg : SetdCat.heq (SetdCat.comp (locMap X R) g) f) : SetdCat.heq g (locLift R f hf) := hg

/-- the localization makes R invertible (R-related points become equal) -/
theorem loc_inverts {X : Setd.{u}} (R : X.car → X.car → Prop) {a b : X.car} (r : R a b) :
    (loc X R).eqv ((locMap X R).fn a) ((locMap X R).fn b) := .rel r

end Core

end Found

#print axioms Found.Core.sumS
#print axioms Found.Core.copair_inl
#print axioms Found.Core.copair_inr
#print axioms Found.Core.copair_uniq
#print axioms Found.Core.prod
#print axioms Found.Core.ccc
#print axioms Found.Core.chiU_spec
#print axioms Found.Core.chiU_uniq
#print axioms Found.Core.power
#print axioms Found.Core.qlift_comm
#print axioms Found.Core.qlift_uniq
#print axioms Found.Core.fold_eq
#print axioms Found.Core.fold_uniq
#print axioms Found.Core.unfold_uniq
#print axioms Found.Core.iso_trans
#print axioms Found.Core.USetd
#print axioms Found.Core.prod_resp
#print axioms Found.Core.locLift
#print axioms Found.Core.locLift_comm
#print axioms Found.Core.locLift_uniq
#print axioms Found.Core.loc_inverts

found/CoreBridges.lean

lean_all/found/CoreBridges.lean

/-
  found/CoreBridges — the points already proved come from the core's seven
  operations (T59): explicit maps/equalities, no re-proofs.

  * induction:  Aczel's V (T109, T88–T91) is a retract up to ≈ of the core
                W-type of (Type, ULift ∘ id): toW then fromW is ≈ the identity
                (the other round trip would need funext on W, so it is not
                claimed); ℕ (T86) is the core W-type of (Bool, b ↦ if b then
                Unit else Empty) with the NNO iteration as the core fold.
  * power/Ω:    the Ω of T63 is the core's OmegaU at universe 0 (same ↔).
  * quotients:  the quotients of T87 are the core's quotients at universe 0.
  * coinduction: the streams of T105 are the core's StreamS on a discrete setoid.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Core
import found.Mostowski
import found.NNO
import found.Setoid
import found.Topos

namespace Found

namespace CoreBridges

open Core

/-! ## V as a W-type (induction) -/

abbrev VW : Type 1 := Core.W Type (fun I => ULift.{1} I)

def toW : WF.V → VW
  | .sup I f => .sup I (fun i => toW (f i.down))

def fromW : VW → WF.V
  | .sup I f => .sup I (fun i => fromW (f ⟨i⟩))

/-- V → W → V is the identity up to bisimilarity -/
theorem from_to : (x : WF.V) → WF.Eq' (fromW (toW x)) x
  | .sup _ f => ⟨fun i => ⟨i, from_to (f i)⟩, fun i => ⟨i, from_to (f i)⟩⟩

/-! ## ℕ as a W-type, iteration as fold -/

def NB : Bool → Type
  | true => Unit
  | false => Empty

abbrev NW : Type := Core.W Bool NB

def nz : NW := .sup false (fun e => nomatch e)
def ns (n : NW) : NW := .sup true (fun _ => n)

def toNW : Nat → NW
  | 0 => nz
  | n + 1 => ns (toNW n)

/-- the NNO algebra on a setoid X with a point and an endomap -/
def natAlg (X : Setd.{0}) (x0 : X.car) (f : SHom X X) : Core.Alg Bool NB where
  X := X
  alg b k := match b, k with
    | false, _ => x0
    | true, k => f.fn (k ())

/-- the NNO iteration of T86 is the core fold -/
theorem iter_is_fold (X : Setd.{0}) (x0 : X.car) (f : SHom X X) :
    ∀ n, NNOs.iterF x0 f n = Core.fold (natAlg X x0 f) (toNW n)
  | 0 => rfl
  | n + 1 => congrArg f.fn (iter_is_fold X x0 f n)

/-! ## Ω and quotients are the core's -/

theorem omega_is_core (p q : Prop) : Setd.eqv OmegaS p q ↔ (OmegaU.{0}).eqv ⟨p⟩ ⟨q⟩ := Iff.rfl

theorem quot_is_core {X : Setd.{0}} (C : SetoidModel.Coarser X) (a b : X.car) :
    (SetoidModel.quot C).eqv a b ↔
      (Core.quot (⟨C.R, C.refl, C.symm, C.trans, C.coarser⟩ : Core.Coarser X)).eqv a b := Iff.rfl

/-! ## streams are the core's -/

def discrete (A : Type) : Setd.{0} := ⟨A, Eq, fun _ => rfl, fun h => h.symm, fun h k => h.trans k⟩

theorem stream_is_core {A S : Type} (c : S → A × S) (x : S) (n : Nat) :
    AFA.unfold c x n = Core.unfold (A := discrete A) c x n := by
  induction n generalizing x with
  | zero => rfl
  | succ n ih => exact ih (c x).2

end CoreBridges

end Found

#print axioms Found.CoreBridges.from_to
#print axioms Found.CoreBridges.iter_is_fold
#print axioms Found.CoreBridges.omega_is_core
#print axioms Found.CoreBridges.quot_is_core
#print axioms Found.CoreBridges.stream_is_core

found/CountUnion.lean

lean_all/found/CountUnion.lean

/-
  T106 (b), slice N3c(iii). found/CountUnion — in the classical point: a
  countable union of countable sets is countable (choice picks the
  injections, Cantor pairing from found/RegToMu combines them), hence ω₁
  (found/Omega1) is regular in the cardinal sense: countably many countable
  subsets never cover it.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Omega1
import found.RegToMu

namespace Found

namespace CU

open CP Card W1

variable (K : Classical')

theorem pair_inj {a b a' b' : Nat} (e : RM.pair a b = RM.pair a' b') : a = a' ∧ b = b' :=
  ⟨(RM.fstU_pair a b).symm.trans ((congrArg RM.fstU e).trans (RM.fstU_pair a' b')),
   (RM.sndU_pair a b).symm.trans ((congrArg RM.sndU e).trans (RM.sndU_pair a' b'))⟩

include K in
/-- a countable union of countable types is countable -/
theorem sigma_countable (I : Type) (A : I → Type) (hI : Countable I) (hA : ∀ i, Countable (A i)) :
    Countable ((i : I) × A i) :=
  match hI with
  | ⟨gI, hgI⟩ =>
    let g : (i : I) → A i → Nat := fun i => K.ch (fun f : A i → Nat => Inj f) (hA i)
    have hg : ∀ i, Inj (g i) := fun i => K.ch_spec (fun f : A i → Nat => Inj f) (hA i)
    ⟨fun p => RM.pair (gI p.1) (g p.1 p.2), fun p q e => by
      have h := pair_inj e
      cases p with
      | mk i a => cases q with
        | mk j b =>
          have hij : i = j := hgI _ _ h.1
          subst hij
          have hab : a = b := hg i a b h.2
          subst hab
          rfl⟩

include K in
/-- ω₁ is regular: countably many countable subsets never cover it -/
theorem omega1_regular (I : Type) (hI : Countable I) (S : I → W1 K → Prop) (hS : ∀ i, Countable {x // S i x}) :
    ∃ x : W1 K, ∀ i, ¬ S i x := by
  refine dne K _ (fun hn => ?_)
  have cover : ∀ x : W1 K, ∃ i, S i x := fun x => dne K _ (fun h => hn ⟨x, fun i hi => h ⟨i, hi⟩⟩)
  match sigma_countable K I (fun i => {x // S i x}) hI hS with
  | ⟨f, hf⟩ =>
    let pick : W1 K → (i : I) × {x // S i x} := fun x =>
      ⟨K.ch (fun i => S i x) (cover x), ⟨x, K.ch_spec (fun i => S i x) (cover x)⟩⟩
    exact uncountable K ⟨fun x => f (pick x), fun x y e => by
      have := hf _ _ e
      exact congrArg (fun p : (i : I) × {x // S i x} => p.2.val) this⟩

end CU

end Found

#print axioms Found.CU.sigma_countable
#print axioms Found.CU.omega1_regular

found/CwF.lean

lean_all/found/CwF.lean

/-
  T152 / T98. found/CwF — semantics of dependent type theory as points:
  categories with families (a free parameter) and the family model of MLTT.
  * CwF: contexts, substitutions, types and terms with substitution actions
    and context extension (p, q, pairing) satisfying the CwF laws.
  * The family model: contexts = types, substitutions = functions,
    types over Γ = families Γ → Type, terms = sections; all laws hold
    definitionally (rfl), no funext.
  * MLTT type formers in the family model: Π and Σ with β and η, Id as
    PLift of Lean's Eq (UIP — the extensional point, T120 line), so MLTT is
    an instance of the CwF parameter.

  Lean 4 core only, hand-written proofs, no axioms.
  CIC dependence: definitional eta (functions, structures), Id = PLift Eq (UIP).
-/

namespace Found

namespace CwF

universe u v

structure CwF where
  Ctx : Type u
  Sub : Ctx → Ctx → Type v
  id : (Γ : Ctx) → Sub Γ Γ
  comp : {Γ Δ Θ : Ctx} → Sub Γ Δ → Sub Δ Θ → Sub Γ Θ
  Ty : Ctx → Type u
  tySub : {Γ Δ : Ctx} → Ty Δ → Sub Γ Δ → Ty Γ
  Tm : (Γ : Ctx) → Ty Γ → Type v
  tmSub : {Γ Δ : Ctx} → {A : Ty Δ} → Tm Δ A → (σ : Sub Γ Δ) → Tm Γ (tySub A σ)
  ext : (Γ : Ctx) → Ty Γ → Ctx
  p : {Γ : Ctx} → (A : Ty Γ) → Sub (ext Γ A) Γ
  ty_id : ∀ {Γ} (A : Ty Γ), tySub A (id Γ) = A
  ty_comp : ∀ {Γ Δ Θ} (A : Ty Θ) (σ : Sub Γ Δ) (τ : Sub Δ Θ), tySub A (comp σ τ) = tySub (tySub A τ) σ

/-! ## the family model -/

def Fam : CwF.{1, 0} where
  Ctx := Type
  Sub Γ Δ := Γ → Δ
  id _ := fun γ => γ
  comp σ τ := fun γ => τ (σ γ)
  Ty Γ := Γ → Type
  tySub A σ := fun γ => A (σ γ)
  Tm Γ A := (γ : Γ) → A γ
  tmSub t σ := fun γ => t (σ γ)
  ext Γ A := (γ : Γ) × A γ
  p {Γ} A := fun (x : (γ : Γ) × A γ) => x.1
  ty_id _ := rfl
  ty_comp _ _ _ := rfl

/-! ## MLTT type formers in the family model -/

variable {Γ : Type}

def Pi (A : Γ → Type) (B : ((γ : Γ) × A γ) → Type) : Γ → Type := fun γ => (x : A γ) → B ⟨γ, x⟩
def lam {A : Γ → Type} {B : ((γ : Γ) × A γ) → Type} (t : (g : (γ : Γ) × A γ) → B g) : (γ : Γ) → Pi A B γ :=
  fun γ x => t ⟨γ, x⟩
def app {A : Γ → Type} {B : ((γ : Γ) × A γ) → Type} (t : (γ : Γ) → Pi A B γ) (u : (γ : Γ) → A γ) :
    (γ : Γ) → B ⟨γ, u γ⟩ := fun γ => t γ (u γ)

theorem pi_beta {A : Γ → Type} {B : ((γ : Γ) × A γ) → Type} (t : (g : (γ : Γ) × A γ) → B g) (u : (γ : Γ) → A γ)
    (γ : Γ) : app (lam t) u γ = t ⟨γ, u γ⟩ := rfl

theorem pi_eta {A : Γ → Type} {B : ((γ : Γ) × A γ) → Type} (t : (γ : Γ) → Pi A B γ) (γ : Γ) :
    lam (fun g => t g.1 g.2) γ = t γ := rfl

def Sig (A : Γ → Type) (B : ((γ : Γ) × A γ) → Type) : Γ → Type := fun γ => (x : A γ) × B ⟨γ, x⟩

theorem sig_beta {A : Γ → Type} {B : ((γ : Γ) × A γ) → Type} (a : (γ : Γ) → A γ) (b : (γ : Γ) → B ⟨γ, a γ⟩) (γ : Γ) :
    (⟨a γ, b γ⟩ : Sig A B γ).1 = a γ := rfl

theorem sig_eta {A : Γ → Type} {B : ((γ : Γ) × A γ) → Type} (s : (γ : Γ) → Sig A B γ) (γ : Γ) :
    (⟨(s γ).1, (s γ).2⟩ : Sig A B γ) = s γ := rfl

def Id (A : Γ → Type) (a b : (γ : Γ) → A γ) : Γ → Type := fun γ => PLift (a γ = b γ)

def refl' (A : Γ → Type) (a : (γ : Γ) → A γ) : (γ : Γ) → Id A a a γ := fun _ => ⟨rfl⟩

/-- the extensional point: Id reflects into judgmental (Lean) equality, and UIP -/
theorem id_reflect (A : Γ → Type) (a b : (γ : Γ) → A γ) (h : (γ : Γ) → Id A a b γ) (γ : Γ) : a γ = b γ := (h γ).down

theorem plift_eq {P : Prop} (x y : PLift P) : x = y := by
  cases x
  cases y
  rfl

theorem id_uip (A : Γ → Type) (a b : (γ : Γ) → A γ) (h k : (γ : Γ) → Id A a b γ) (γ : Γ) : h γ = k γ :=
  plift_eq _ _

end CwF

end Found

#print axioms Found.CwF.Fam
#print axioms Found.CwF.pi_beta
#print axioms Found.CwF.pi_eta
#print axioms Found.CwF.sig_eta
#print axioms Found.CwF.id_reflect
#print axioms Found.CwF.id_uip

found/DInf1.lean

lean_all/found/DInf1.lean

/-
  T116, Scott's D∞, part 1. found/DInf1 — ω-chain complete pointed
  preorders with sups of chains (the sup takes the chain proof, so the
  function space is constructive), continuous maps, the function space
  CPO [A → B] (pointwise order and sups), and the tower D_0 = Sierpinski
  (Prop), D_{n+1} = [D_n → D_n] with embedding–projection pairs
  e_0 x = const x, p_0 f = f ⊥, e_{n+1} f = e_n ∘ f ∘ p_n,
  p_{n+1} g = p_n ∘ g ∘ e_n:  p_n ∘ e_n ≈ id and e_n ∘ p_n ≤ id.

  Lean 4 core only, hand-written proofs, no axioms.
-/

namespace Found

namespace DI

universe u

structure CPO where
  D : Type u
  le : D → D → Prop
  le_refl : ∀ a, le a a
  le_trans : ∀ {a b c}, le a b → le b c → le a c
  bot : D
  bot_le : ∀ a, le bot a
  sup : (c : Nat → D) → (∀ n, le (c n) (c (n + 1))) → D
  sup_ub : ∀ (c : Nat → D) (hc : ∀ n, le (c n) (c (n + 1))) n, le (c n) (sup c hc)
  sup_least : ∀ (c : Nat → D) (hc : ∀ n, le (c n) (c (n + 1))) x, (∀ n, le (c n) x) → le (sup c hc) x

def Chain (A : CPO.{u}) (c : Nat → A.D) : Prop := ∀ n, A.le (c n) (c (n + 1))

def Eqv (A : CPO.{u}) (a b : A.D) : Prop := A.le a b ∧ A.le b a

theorem eqv_refl (A : CPO.{u}) (a : A.D) : Eqv A a a := ⟨A.le_refl a, A.le_refl a⟩
theorem eqv_symm (A : CPO.{u}) {a b : A.D} (h : Eqv A a b) : Eqv A b a := ⟨h.2, h.1⟩
theorem eqv_trans (A : CPO.{u}) {a b c : A.D} (h : Eqv A a b) (k : Eqv A b c) : Eqv A a c :=
  ⟨A.le_trans h.1 k.1, A.le_trans k.2 h.2⟩

theorem sup_mono (A : CPO.{u}) (c d : Nat → A.D) (hc : Chain A c) (hd : Chain A d) (h : ∀ n, A.le (c n) (d n)) :
    A.le (A.sup c hc) (A.sup d hd) :=
  A.sup_least c hc _ (fun n => A.le_trans (h n) (A.sup_ub d hd n))

/-- continuous maps -/
structure CMap (A B : CPO.{u}) where
  f : A.D → B.D
  mono : ∀ {a b}, A.le a b → B.le (f a) (f b)
  cont : ∀ (c : Nat → A.D) (hc : Chain A c), B.le (f (A.sup c hc)) (B.sup (fun n => f (c n)) (fun n => mono (hc n)))

theorem cmap_sup {A B : CPO.{u}} (g : CMap A B) (c : Nat → A.D) (hc : Chain A c) :
    Eqv B (g.f (A.sup c hc)) (B.sup (fun n => g.f (c n)) (fun n => g.mono (hc n))) :=
  ⟨g.cont c hc, B.sup_least _ _ _ (fun n => g.mono (A.sup_ub c hc n))⟩

def cid (A : CPO.{u}) : CMap A A := ⟨fun x => x, fun h => h, fun _ _ => A.le_refl _⟩

def ccomp {A B C : CPO.{u}} (g : CMap B C) (h : CMap A B) : CMap A C :=
  ⟨fun x => g.f (h.f x), fun e => g.mono (h.mono e), fun c hc =>
    C.le_trans (g.mono (h.cont c hc)) (g.cont _ (fun n => h.mono (hc n)))⟩

def cconst (A : CPO.{u}) {B : CPO.{u}} (b : B.D) : CMap A B :=
  ⟨fun _ => b, fun _ => B.le_refl _, fun _ _ => B.sup_ub (fun _ => b) (fun _ => B.le_refl _) 0⟩

/-- the function space CPO [A → B] -/
def fn (A B : CPO.{u}) : CPO.{u} where
  D := CMap A B
  le g h := ∀ x, B.le (g.f x) (h.f x)
  le_refl _ _ := B.le_refl _
  le_trans h1 h2 x := B.le_trans (h1 x) (h2 x)
  bot := cconst A B.bot
  bot_le _ _ := B.bot_le _
  sup c hc := ⟨fun x => B.sup (fun n => (c n).f x) (fun n => hc n x),
    fun e => sup_mono B _ _ _ _ (fun n => (c n).mono e),
    fun d hd => B.sup_least _ _ _ (fun n =>
      B.le_trans ((c n).cont d hd) (B.sup_least _ _ _ (fun m =>
        B.le_trans (B.sup_ub (fun k => (c k).f (d m)) (fun k => hc k (d m)) n)
          (B.sup_ub (fun m => B.sup (fun k => (c k).f (d m)) (fun k => hc k (d m)))
            (fun m => sup_mono B _ _ _ _ (fun k => (c k).mono (hd m))) m))))⟩
  sup_ub c hc n x := B.sup_ub (fun n => (c n).f x) (fun n => hc n x) n
  sup_least _ _ _ h x := B.sup_least _ _ _ (fun n => h n x)

/-! ## the tower -/

/-- the Sierpinski space: propositions ordered by implication -/
def sier : CPO.{0} where
  D := Prop
  le a b := a → b
  le_refl _ h := h
  le_trans h1 h2 x := h2 (h1 x)
  bot := False
  bot_le _ h := h.elim
  sup c _ := ∃ n, c n
  sup_ub _ _ n h := ⟨n, h⟩
  sup_least _ _ _ h := fun ⟨n, hn⟩ => h n hn

def D : Nat → CPO.{0}
  | 0 => sier
  | n + 1 => fn (D n) (D n)

/-- conjugation g ∘ f ∘ h, continuous in f -/
def conj {A B C E : CPO.{0}} (g : CMap B C) (h : CMap E A) : CMap (fn A B) (fn E C) :=
  ⟨fun f => ccomp g (ccomp f h), fun e x => g.mono (e (h.f x)), fun _ hc x => g.cont _ (fun n => hc n (h.f x))⟩

/-- the embedding–projection pairs -/
def ep : (n : Nat) → CMap (D n) (D (n + 1)) × CMap (D (n + 1)) (D n)
  | 0 => (⟨fun x => cconst sier x, fun e _ => e, fun _ _ _ => fun h => h⟩,
          ⟨fun f => f.f False, fun e => e False, fun _ _ => fun h => h⟩)
  | n + 1 => (conj (ep n).1 (ep n).2, conj (ep n).2 (ep n).1)

def e (n : Nat) : CMap (D n) (D (n + 1)) := (ep n).1
def p (n : Nat) : CMap (D (n + 1)) (D n) := (ep n).2

/-- p ∘ e ≈ id and e ∘ p ≤ id -/
theorem pe : (n : Nat) → ∀ x, Eqv (D n) ((p n).f ((e n).f x)) x
  | 0, _ => ⟨fun h => h, fun h => h⟩
  | n + 1, f => ⟨fun x => (D n).le_trans ((pe n _).1) (f.mono ((pe n x).1)),
                 fun x => (D n).le_trans (f.mono ((pe n x).2)) ((pe n _).2)⟩

theorem ep_le : (n : Nat) → ∀ y, (D (n + 1)).le ((e n).f ((p n).f y)) y
  | 0, f => fun _ => f.mono (fun h => h.elim)
  | n + 1, g => fun x => (D (n + 1)).le_trans (ep_le n _) (g.mono (ep_le n x))

end DI

end Found

#print axioms Found.DI.fn
#print axioms Found.DI.pe
#print axioms Found.DI.ep_le

found/DInf2.lean

lean_all/found/DInf2.lean

/-
  T116, Scott's D∞, part 2. found/DInf2 — transfer maps between all levels
  of the tower (found/DInf1): Θ n m = (θ_{n,m}, θ'_{n,m}) with
  θ_{n,0} = p-iterate, θ'_{n,0} = e-iterate, θ_{n+1,m+1} f = θ_{n,m} ∘ f ∘ θ'_{n,m}
  (and the reverse), by structural recursion on n.  Compatibility with the
  projections and embeddings:  p_m (θ_{n,m+1} x) ≈ θ_{n,m} x  and
  θ'_{n,m+1} (e_m y) ≈ θ'_{n,m} y;  θ_{n,n} ≈ id ≈ θ'_{n,n}.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.DInf1

namespace Found

namespace DI

def up0 : (m : Nat) → CMap (D 0) (D m)
  | 0 => cid _
  | m + 1 => ccomp (e m) (up0 m)

def dn0 : (m : Nat) → CMap (D m) (D 0)
  | 0 => cid _
  | m + 1 => ccomp (dn0 m) (p m)

def Θ : (n m : Nat) → CMap (D n) (D m) × CMap (D m) (D n)
  | 0, m => (up0 m, dn0 m)
  | n + 1, 0 => (ccomp (Θ n 0).1 (p n), ccomp (e n) (Θ n 0).2)
  | n + 1, m + 1 => (conj (Θ n m).1 (Θ n m).2, conj (Θ n m).2 (Θ n m).1)

def th (n m : Nat) : CMap (D n) (D m) := (Θ n m).1
def ht (n m : Nat) : CMap (D m) (D n) := (Θ n m).2

theorem eqv_map {A B : CPO} (g : CMap A B) {a b : A.D} (h : Eqv A a b) : Eqv B (g.f a) (g.f b) :=
  ⟨g.mono h.1, g.mono h.2⟩

theorem eqv_fn {A B : CPO} {f g : CMap A B} (h : ∀ x, Eqv B (f.f x) (g.f x)) : Eqv (fn A B) f g :=
  ⟨fun x => (h x).1, fun x => (h x).2⟩

/-- the projection to level n is the evaluation at the embedded bottom, below level 0 -/
theorem lemG : (n : Nat) → ∀ x : (D (n + 1)).D,
    Eqv (D 0) ((th n 0).f ((p n).f x)) ((th n 0).f (x.f ((ht n 0).f False)))
  | 0, _ => eqv_refl _ _
  | n + 1, x => lemG n (ccomp (p n) (ccomp x (e n)))

/-- embeddings of embedded points of level 0 are constant maps -/
theorem lemH : (n : Nat) → ∀ z : (D 0).D,
    Eqv (D (n + 1)) ((e n).f ((ht n 0).f z)) (cconst (D n) ((ht n 0).f z))
  | 0, _ => eqv_refl _ _
  | n + 1, z => eqv_fn (fun y => eqv_map (e n) (⟨(lemH n z).1 _, (lemH n z).2 _⟩ :
      Eqv (D n) (((e n).f ((ht n 0).f z)).f ((p n).f y)) ((ht n 0).f z)))

/-- compatibility of the transfer maps with p and e -/
theorem compat : (n : Nat) → ∀ m,
    (∀ x : (D n).D, Eqv (D m) ((p m).f ((th n (m + 1)).f x)) ((th n m).f x)) ∧
    (∀ y : (D m).D, Eqv (D n) ((ht n (m + 1)).f ((e m).f y)) ((ht n m).f y))
  | 0, m => ⟨fun _ => pe m _, fun y => eqv_map (dn0 m) (pe m y)⟩
  | n + 1, 0 => ⟨fun x => eqv_symm _ (lemG n x), fun y => eqv_symm _ (lemH n y)⟩
  | n + 1, m + 1 =>
    ⟨fun x => eqv_fn (fun y => eqv_trans _ ((compat n m).1 _)
        (eqv_map (th n m) (eqv_map x ((compat n m).2 y)))),
     fun y => eqv_fn (fun z => eqv_trans _ ((compat n m).2 _)
        (eqv_map (ht n m) (eqv_map y ((compat n m).1 z))))⟩

/-- θ_{n,n} ≈ id ≈ θ'_{n,n} -/
theorem ident : (n : Nat) → (∀ x, Eqv (D n) ((th n n).f x) x) ∧ (∀ x, Eqv (D n) ((ht n n).f x) x)
  | 0 => ⟨fun x => eqv_refl _ x, fun x => eqv_refl _ x⟩
  | n + 1 => ⟨fun f => eqv_fn (fun y => eqv_trans _ ((ident n).1 _) (eqv_map f ((ident n).2 y))),
              fun f => eqv_fn (fun y => eqv_trans _ ((ident n).2 _) (eqv_map f ((ident n).1 y)))⟩

theorem th0 : (n : Nat) → th n 0 = dn0 n
  | 0 => rfl
  | n + 1 => congrArg (fun g => ccomp g (p n)) (th0 n)

theorem ht0 : (n : Nat) → ht n 0 = up0 n
  | 0 => rfl
  | n + 1 => congrArg (fun g => ccomp (e n) g) (ht0 n)

/-- moving the source up by e_n does not change the transfer -/
theorem src_up : (n : Nat) → ∀ m,
    (∀ x, Eqv (D m) ((th (n + 1) m).f ((e n).f x)) ((th n m).f x)) ∧
    (∀ u, Eqv (D n) ((p n).f ((ht (n + 1) m).f u)) ((ht n m).f u))
  | 0, 0 => ⟨fun x => pe 0 x, fun u => pe 0 u⟩
  | 0, m + 1 => ⟨fun x => by
        have h := lemH m x
        rw [ht0] at h
        exact eqv_fn (fun _ => ⟨(h.2 _), (h.1 _)⟩),
      fun u => by
        have h := lemG m u
        rw [th0, ht0] at h
        exact eqv_symm _ h⟩
  | n + 1, 0 => ⟨fun x => eqv_map (th (n + 1) 0) (pe (n + 1) x), fun u => pe (n + 1) _⟩
  | n + 1, m + 1 =>
    ⟨fun x => eqv_fn (fun u => eqv_trans _ ((src_up n m).1 _)
        (eqv_map (th n m) (eqv_map x ((src_up n m).2 u)))),
     fun u => eqv_fn (fun w => eqv_trans _ ((src_up n m).2 _)
        (eqv_map (ht n m) (eqv_map u ((src_up n m).1 w))))⟩

/-- upward: below the target, θ_{n,m+1} = e_m ∘ θ_{n,m} -/
theorem upw : (n : Nat) → ∀ m, n ≤ m →
    (∀ x, Eqv (D (m + 1)) ((th n (m + 1)).f x) ((e m).f ((th n m).f x))) ∧
    (∀ v, Eqv (D n) ((ht n (m + 1)).f v) ((ht n m).f ((p m).f v)))
  | 0, _, _ => ⟨fun _ => eqv_refl _ _, fun _ => eqv_refl _ _⟩
  | _ + 1, 0, h => absurd h (Nat.not_succ_le_zero _)
  | n + 1, m + 1, h =>
    have ih := upw n m (Nat.le_of_succ_le_succ h)
    ⟨fun x => eqv_fn (fun u => eqv_trans _ (ih.1 _) (eqv_map (e m) (eqv_map (th n m) (eqv_map x (ih.2 u))))),
     fun v => eqv_fn (fun w => eqv_trans _ (ih.2 _) (eqv_map (ht n m) (eqv_map (p m) (eqv_map v (ih.1 w)))))⟩

/-- downward: above the target, θ_{n+1,m} = θ_{n,m} ∘ p_n -/
theorem dnw : (m : Nat) → ∀ n, m ≤ n →
    (∀ y, Eqv (D m) ((th (n + 1) m).f y) ((th n m).f ((p n).f y))) ∧
    (∀ v, Eqv (D (n + 1)) ((ht (n + 1) m).f v) ((e n).f ((ht n m).f v)))
  | 0, _, _ => ⟨fun _ => eqv_refl _ _, fun _ => eqv_refl _ _⟩
  | _ + 1, 0, h => absurd h (Nat.not_succ_le_zero _)
  | m + 1, n + 1, h =>
    have ih := dnw m n (Nat.le_of_succ_le_succ h)
    ⟨fun y => eqv_fn (fun u => eqv_trans _ (ih.1 _) (eqv_map (th n m) (eqv_map (p n) (eqv_map y (ih.2 u))))),
     fun v => eqv_fn (fun w => eqv_trans _ (ih.2 _) (eqv_map (e n) (eqv_map (ht n m) (eqv_map v (ih.1 w)))))⟩

/-! ## the inverse limit -/

def Compat (z : (n : Nat) → (D n).D) : Prop := ∀ n, Eqv (D n) ((p n).f (z (n + 1))) (z n)

theorem pbot : (n : Nat) → Eqv (D n) ((p n).f (D (n + 1)).bot) (D n).bot
  | 0 => eqv_refl _ _
  | n + 1 => eqv_fn (fun _ => pbot n)

theorem sup_eqv (A : CPO) (c d : Nat → A.D) (hc : Chain A c) (hd : Chain A d) (h : ∀ n, Eqv A (c n) (d n)) :
    Eqv A (A.sup c hc) (A.sup d hd) :=
  ⟨sup_mono A c d hc hd (fun n => (h n).1), sup_mono A d c hd hc (fun n => (h n).2)⟩

def Dinf : CPO.{0} where
  D := {z : (n : Nat) → (D n).D // Compat z}
  le z w := ∀ n, (D n).le (z.val n) (w.val n)
  le_refl _ _ := (D _).le_refl _
  le_trans h1 h2 n := (D n).le_trans (h1 n) (h2 n)
  bot := ⟨fun n => (D n).bot, pbot⟩
  bot_le _ n := (D n).bot_le _
  sup c hc := ⟨fun n => (D n).sup (fun k => (c k).val n) (fun k => hc k n), fun n =>
    eqv_trans _ (cmap_sup (p n) _ (fun k => hc k (n + 1)))
      (sup_eqv _ _ _ _ _ (fun k => (c k).property n))⟩
  sup_ub c hc k n := (D n).sup_ub (fun k => (c k).val n) (fun k => hc k n) k
  sup_least _ _ _ h n := (D n).sup_least _ _ _ (fun k => h k n)

/-- the embedding of level n into D∞ -/
def iota (n : Nat) (x : (D n).D) : Dinf.D := ⟨fun m => (th n m).f x, fun m => (compat n m).1 x⟩

theorem iota_mono (n : Nat) {x y : (D n).D} (h : (D n).le x y) : Dinf.le (iota n x) (iota n y) :=
  fun m => (th n m).mono h

theorem pi_iota (n : Nat) (x : (D n).D) : Eqv (D n) ((iota n x).val n) x := (ident n).1 x

theorem total : (a b : Nat) → a ≤ b ∨ b ≤ a
  | 0, _ => Or.inl (Nat.zero_le _)
  | _ + 1, 0 => Or.inr (Nat.zero_le _)
  | a + 1, b + 1 => (total a b).elim (fun h => Or.inl (Nat.succ_le_succ h)) (fun h => Or.inr (Nat.succ_le_succ h))

theorem ep_compat (z : Dinf.D) (m : Nat) : (D (m + 1)).le ((e m).f (z.val m)) (z.val (m + 1)) :=
  (D (m + 1)).le_trans ((e m).mono (z.property m).2) (ep_le m _)

theorem down_eq (z : Dinf.D) (m : Nat) : ∀ n, m ≤ n → Eqv (D m) ((th n m).f (z.val n)) (z.val m) := by
  intro n h
  induction h with
  | refl => exact (ident m).1 _
  | step h' ih => exact eqv_trans _ ((dnw m _ h').1 _) (eqv_trans _ (eqv_map (th _ m) (z.property _)) ih)

theorem up_le (z : Dinf.D) (n : Nat) : ∀ m, n ≤ m → (D m).le ((th n m).f (z.val n)) (z.val m) := by
  intro m h
  induction h with
  | refl => exact ((ident n).1 _).1
  | step h' ih => exact (D _).le_trans ((upw n _ h').1 _).1 ((D _).le_trans ((e _).mono ih) (ep_compat z _))

/-- ι_n ∘ π_n ≤ id -/
theorem iota_pi (z : Dinf.D) (n : Nat) : Dinf.le (iota n (z.val n)) z := fun m =>
  (total m n).elim (fun h => (down_eq z m n h).1) (fun h => up_le z n m h)

/-- ι_n ∘ π_n increases with n -/
theorem iota_chain (z : Dinf.D) : Chain Dinf (fun n => iota n (z.val n)) := fun n m =>
  (D m).le_trans ((src_up n m).1 _).2 ((th (n + 1) m).mono (ep_compat z n))

/-- ⊔ ι_n ∘ π_n = id -/
theorem sup_iota (z : Dinf.D) : Eqv Dinf (Dinf.sup (fun n => iota n (z.val n)) (iota_chain z)) z :=
  ⟨Dinf.sup_least _ _ _ (fun n => iota_pi z n),
   fun m => (D m).le_trans ((ident m).1 _).2 (Dinf.sup_ub _ (iota_chain z) m m)⟩

end DI

end Found

#print axioms Found.DI.lemG
#print axioms Found.DI.lemH
#print axioms Found.DI.compat
#print axioms Found.DI.ident
#print axioms Found.DI.src_up
#print axioms Found.DI.upw
#print axioms Found.DI.dnw
#print axioms Found.DI.Dinf
#print axioms Found.DI.iota_pi
#print axioms Found.DI.iota_chain
#print axioms Found.DI.sup_iota

found/DInf3.lean

lean_all/found/DInf3.lean

/-
  T116, Scott's D∞, part 3. found/DInf3 — D∞ ≅ [D∞ → D∞]: with ι_n, π_n
  continuous (found/DInf2),
    Ψ(F)_{n+1} = π_n ∘ F ∘ ι_n,  Ψ(F)_0 = p_0 (Ψ(F)_1),
    Φ(z)(y) = ⊔_n ι_n (z_{n+1} (y_n)),
  are continuous, and Φ ∘ Ψ ≈ id, Ψ ∘ Φ ≈ id: an extensional model of the
  untyped λ-calculus in domain theory; it is not trivial (⊥ ≠ ι_0 ⊤).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.DInf2

namespace Found

namespace DI

/-- θ' and θ are the same transfer, read in both orders -/
theorem sym : (k : Nat) → ∀ m,
    (∀ x, Eqv (D k) ((ht k m).f x) ((th m k).f x)) ∧ (∀ x, Eqv (D m) ((ht m k).f x) ((th k m).f x))
  | 0, m => ⟨fun x => by rw [th0]; exact eqv_refl _ _, fun x => by rw [ht0]; exact eqv_refl _ _⟩
  | k + 1, 0 => ⟨fun x => by rw [ht0]; exact eqv_refl _ _, fun x => by rw [th0]; exact eqv_refl _ _⟩
  | k + 1, m + 1 =>
    ⟨fun f => eqv_fn (fun u => eqv_trans _ ((sym k m).1 _) (eqv_map (th m k) (eqv_map f (eqv_symm _ ((sym k m).2 u))))),
     fun f => eqv_fn (fun u => eqv_trans _ ((sym k m).2 _) (eqv_map (th k m) (eqv_map f (eqv_symm _ ((sym k m).1 u)))))⟩

def iotaC (n : Nat) : CMap (D n) Dinf :=
  ⟨iota n, iota_mono n, fun c hc m => (th n m).cont c hc⟩

def piC (n : Nat) : CMap Dinf (D n) :=
  ⟨fun z => z.val n, fun h => h n, fun _ _ => (D n).le_refl _⟩

theorem eqv_dinf {z w : Dinf.D} (h : ∀ n, Eqv (D n) (z.val n) (w.val n)) : Eqv Dinf z w :=
  ⟨fun n => (h n).1, fun n => (h n).2⟩

/-- ι_{n+1} ∘ e_n ≈ ι_n -/
theorem iota_e (n : Nat) (x : (D n).D) : Eqv Dinf (iota (n + 1) ((e n).f x)) (iota n x) :=
  eqv_dinf (fun m => (src_up n m).1 x)

/-! ## Ψ : [D∞ → D∞] → D∞ -/

def psiSeq (F : CMap Dinf Dinf) : (n : Nat) → (D n).D
  | 0 => (p 0).f (ccomp (piC 0) (ccomp F (iotaC 0)))
  | n + 1 => ccomp (piC n) (ccomp F (iotaC n))

theorem psi_compat (F : CMap Dinf Dinf) : Compat (psiSeq F)
  | 0 => eqv_refl _ _
  | n + 1 => eqv_fn (fun x =>
      eqv_trans _ ((F.f (iota (n + 1) ((e n).f x))).property n)
        ⟨(F.mono (iota_e n x).1) n, (F.mono (iota_e n x).2) n⟩)

def psi (F : CMap Dinf Dinf) : Dinf.D := ⟨psiSeq F, psi_compat F⟩

theorem chain_mono (A : CPO) (c : Nat → A.D) (hc : Chain A c) {a b : Nat} (h : a ≤ b) : A.le (c a) (c b) := by
  induction h with
  | refl => exact A.le_refl _
  | step _ ih => exact A.le_trans ih (hc _)

/-! ## Φ : D∞ → [D∞ → D∞] -/

def phiTerm (z y : Dinf.D) (n : Nat) : Dinf.D := iota n ((z.val (n + 1)).f (y.val n))

theorem phi_chain (z y : Dinf.D) : Chain Dinf (phiTerm z y) := fun n =>
  Dinf.le_trans (iota_e n _).2 (iota_mono (n + 1) ((D (n + 1)).le_trans
    ((e n).mono ((D n).le_trans ((z.property (n + 1)).2 (y.val n))
      ((p n).mono ((z.val (n + 2)).mono (ep_compat y n)))))
    (ep_le n _)))

def phiF (z : Dinf.D) : CMap Dinf Dinf where
  f y := Dinf.sup (phiTerm z y) (phi_chain z y)
  mono h := sup_mono Dinf _ _ _ _ (fun n => iota_mono n ((z.val (n + 1)).mono (h n)))
  cont c hc := Dinf.sup_least _ _ _ (fun n =>
    Dinf.le_trans (iota_mono n ((z.val (n + 1)).cont _ (fun k => hc k n)))
      (Dinf.le_trans ((iotaC n).cont _ (fun k => (z.val (n + 1)).mono (hc k n)))
        (sup_mono Dinf _ _ _ _ (fun k => Dinf.sup_ub (phiTerm z (c k)) (phi_chain z (c k)) n))))

def phi : CMap Dinf (fn Dinf Dinf) where
  f := phiF
  mono h y := sup_mono Dinf _ _ _ _ (fun n => iota_mono n (h (n + 1) (y.val n)))
  cont c hc y := Dinf.sup_least _ _ _ (fun n =>
    Dinf.le_trans ((iotaC n).cont _ (fun k => hc k (n + 1) (y.val n)))
      (sup_mono Dinf _ _ _ _ (fun k => Dinf.sup_ub (phiTerm (c k) y) (phi_chain (c k) y) n)))

/-! ## Φ ∘ Ψ ≈ id and Ψ ∘ Φ ≈ id -/

theorem phi_psi (F : CMap Dinf Dinf) (y : Dinf.D) : Eqv Dinf ((phiF (psi F)).f y) (F.f y) :=
  ⟨Dinf.sup_least _ _ _ (fun n => Dinf.le_trans (iota_pi (F.f (iota n (y.val n))) n) (F.mono (iota_pi y n))),
   Dinf.le_trans (F.mono (sup_iota y).2)
    (Dinf.le_trans (F.cont _ (iota_chain y))
      (Dinf.sup_least _ _ _ (fun n => Dinf.le_trans (sup_iota (F.f (iota n (y.val n)))).2
        (Dinf.sup_least _ _ _ (fun m =>
          Dinf.le_trans (chain_mono Dinf _ (iota_chain (F.f (iota n (y.val n)))) (Nat.le_add_right m n))
            (Dinf.le_trans
              (iota_mono (m + n) (F.mono (chain_mono Dinf _ (iota_chain y) (Nat.le_add_left n m)) (m + n)))
              (Dinf.sup_ub (phiTerm (psi F) y) (phi_chain (psi F) y) (m + n))))))))⟩

theorem psi_phi_succ (z : Dinf.D) (n : Nat) : Eqv (D (n + 1)) ((psi (phiF z)).val (n + 1)) (z.val (n + 1)) :=
  eqv_fn (fun x =>
    ⟨(D n).sup_least _ _ _ (fun k => (D n).le_trans
        ((th k n).mono ((z.val (k + 1)).mono ((sym k n).1 x).2))
        ((iota_pi z (k + 1)) (n + 1) x)),
     (D n).le_trans
        ((D n).le_trans ((z.val (n + 1)).mono ((ident n).1 x).2) ((ident n).1 _).2)
        ((D n).sup_ub (fun k => ((phiTerm z (iota n x)) k).val n) (fun k => phi_chain z (iota n x) k n) n)⟩)

theorem psi_phi (z : Dinf.D) : Eqv Dinf (psi (phiF z)) z :=
  eqv_dinf (fun n => match n with
    | 0 => eqv_trans _ (eqv_map (p 0) (psi_phi_succ z 0)) (z.property 0)
    | n + 1 => psi_phi_succ z n)

def psiC : CMap (fn Dinf Dinf) Dinf where
  f := psi
  mono {a b} h n := match n with
    | 0 => show (D 0).le ((p 0).f (ccomp (piC 0) (ccomp a (iotaC 0)))) ((p 0).f (ccomp (piC 0) (ccomp b (iotaC 0)))) from
        (p 0).mono (fun x => (h (iota 0 x)) 0)
    | n + 1 => fun x => (h (iota n x)) n
  cont c hc n := match n with
    | 0 => show (D 0).le ((p 0).f (ccomp (piC 0) (ccomp ((fn Dinf Dinf).sup c hc) (iotaC 0)))) _ from
        (D 0).le_trans ((p 0).mono (fun _ => (D 0).le_refl _))
          ((p 0).cont (fun k => ccomp (piC 0) (ccomp (c k) (iotaC 0))) (fun k x => hc k (iota 0 x) 0))
    | n + 1 => fun _ => (D n).le_refl _

/-- D∞ ≅ [D∞ → D∞] (both composites equivalent to identities) -/
theorem iso : (∀ F : CMap Dinf Dinf, Eqv (fn Dinf Dinf) (phi.f (psiC.f F)) F) ∧
    (∀ z : Dinf.D, Eqv Dinf (psiC.f (phi.f z)) z) :=
  ⟨fun F => eqv_fn (fun y => by show Eqv Dinf ((phiF (psi F)).f y) (F.f y); exact phi_psi F y), psi_phi⟩

/-- the model is not trivial -/
theorem nontrivial : ¬ Dinf.le (iota 0 True) Dinf.bot := fun h => h 0 trivial

end DI

end Found

#print axioms Found.DI.sym
#print axioms Found.DI.psi
#print axioms Found.DI.phi
#print axioms Found.DI.phi_psi
#print axioms Found.DI.psi_phi
#print axioms Found.DI.psiC
#print axioms Found.DI.iso
#print axioms Found.DI.nontrivial

found/DNTrans.lean

lean_all/found/DNTrans.lean

/-
  T77. found/DNTrans — double-negation translations compared (propositional).

  * Intuitionistically gg φ ↔ ¬¬φ for every formula; propositional Kuroda
    is φ ↦ ¬¬φ, so Gödel–Gentzen and Kuroda agree up to provable equivalence.
  * Glivenko: Γ ⊢_c φ ⟺ gg Γ ⊢_i ¬¬φ; in particular ⊢_c φ ⟺ ⊢_i ¬¬φ.
  * Friedman's A-translation (⊥ ↦ A, p ↦ p ∨ A) preserves intuitionistic
    derivability; first-order Π⁰₂-conservativity uses it in T83.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Classical

namespace Found

namespace Prp

def N (x : Form) : Form := neg (neg x)

/-- intuitionistically, gg φ ↔ ¬¬φ (both directions, in any context) -/
theorem gg_nn : (φ : Form) → ∀ (Θ : Form → Prop),
    PD true Θ (.imp (gg φ) (N φ)) ∧ PD true Θ (.imp (N φ) (gg φ))
  | .var _, _ => ⟨.impI h0, .impI h0⟩
  | .bot, _ => ⟨.impI (.impI (.impE h0 h1)), .impI (.impE h0 (.impI h0))⟩
  | .and a b, _ =>
    ⟨.impI (.impI (.impE (.impE (gg_nn a _).1 (.andE1 h1))
        (.impI (.impE (.impE (gg_nn b _).1 (.andE2 h2)) (.impI (.impE h2 (.andI h1 h0))))))),
     .impI (.andI
        (.impE (gg_nn a _).2 (.impI (.impE h1 (.impI (.impE h1 (.andE1 h0))))))
        (.impE (gg_nn b _).2 (.impI (.impE h1 (.impI (.impE h1 (.andE2 h0)))))))⟩
  | .or a b, _ =>
    ⟨.impI (.impI (.impE h1 (.andI
        (.impI (.impE (.impE (gg_nn a _).1 h0) (.impI (.impE h2 (.orI1 h0)))))
        (.impI (.impE (.impE (gg_nn b _).1 h0) (.impI (.impE h2 (.orI2 h0)))))))),
     .impI (.impI (.impE h1 (.impI (.orE h0
        (.impE (.andE1 h2) (.impE (gg_nn a _).2 (.impI (.impE h0 h1))))
        (.impE (.andE2 h2) (.impE (gg_nn b _).2 (.impI (.impE h0 h1))))))))⟩
  | .imp a b, _ =>
    ⟨.impI (.impI (.impE
        (.impE (gg_nn b _).1 (.impE h1 (.impE (gg_nn a _).2
          (.impI (.impE h1 (.impI (.botE rfl (.impE h1 h0))))))))
        (.impI (.impE h1 (.impI h1))))),
     .impI (.impI (.impE (gg_nn b _).2 (.impI (.impE h2 (.impI
        (.impE (.impE (gg_nn a _).1 h2) (.impI (.impE h2 (.impE h1 h0)))))))))⟩

/-- Glivenko: Γ ⊢_c φ ⟺ gg Γ ⊢_i ¬¬φ -/
theorem glivenko (Γ : Form → Prop) (φ : Form) : CD Γ φ ↔ PD true (GG Γ) (N φ) :=
  ⟨fun d => .impE (gg_nn φ _).1 ((gg_iff Γ φ).mp d),
   fun d => (gg_iff Γ φ).mpr (.impE (gg_nn φ _).2 d)⟩

/-- Glivenko for theorems: ⊢_c φ ⟺ ⊢_i ¬¬φ -/
theorem glivenko0 (φ : Form) : CD (fun _ => False) φ ↔ PD true (fun _ => False) (N φ) :=
  ⟨fun d => mono ((glivenko _ φ).mp d) (fun _ ⟨_, g, _⟩ => g.elim),
   fun d => (glivenko _ φ).mpr (mono d (fun _ g => g.elim))⟩

/-- propositional Kuroda: φ ↦ ¬¬φ; it agrees with Gödel–Gentzen -/
def kuroda (φ : Form) : Form := N φ

theorem kuroda_gg (φ : Form) (Θ : Form → Prop) :
    PD true Θ (.imp (kuroda φ) (gg φ)) ∧ PD true Θ (.imp (gg φ) (kuroda φ)) :=
  ⟨(gg_nn φ Θ).2, (gg_nn φ Θ).1⟩

/-! ## Friedman's A-translation -/

def atr (A : Form) : Form → Form
  | .var n => .or (.var n) A
  | .bot => A
  | .and a b => .and (atr A a) (atr A b)
  | .or a b => .or (atr A a) (atr A b)
  | .imp a b => .imp (atr A a) (atr A b)

theorem efqA (A : Form) : (φ : Form) → ∀ (Θ : Form → Prop), PD true Θ (.imp A (atr A φ))
  | .var _, _ => .impI (.orI2 h0)
  | .bot, _ => .impI h0
  | .and a b, _ => .impI (.andI (.impE (efqA A a _) h0) (.impE (efqA A b _) h0))
  | .or a _, _ => .impI (.orI1 (.impE (efqA A a _) h0))
  | .imp _ b, _ => .impI (.impI (.impE (efqA A b _) h1))

/-- the A-translation preserves intuitionistic derivability -/
theorem atr_sound (A : Form) {Δ : Form → Prop} {φ : Form} (d : PD true Δ φ) :
    ∀ {Θ : Form → Prop}, (∀ x, Δ x → PD true Θ (atr A x)) → PD true Θ (atr A φ) := by
  induction d with
  | hyp g => exact fun h => h _ g
  | @botE _ a _ _ ih => exact fun h => .impE (efqA A a _) (ih h)
  | andI _ _ iha ihb => exact fun h => .andI (iha h) (ihb h)
  | andE1 _ ih => exact fun h => .andE1 (ih h)
  | andE2 _ ih => exact fun h => .andE2 (ih h)
  | orI1 _ ih => exact fun h => .orI1 (ih h)
  | orI2 _ ih => exact fun h => .orI2 (ih h)
  | @orE _ a b _ _ _ _ ih iha ihb =>
    exact fun {Θ} h => .orE (ih h)
      (iha (Θ := ext Θ (atr A a)) (fun x k => k.elim (fun q => q ▸ h0) (fun g => wk (h x g))))
      (ihb (Θ := ext Θ (atr A b)) (fun x k => k.elim (fun q => q ▸ h0) (fun g => wk (h x g))))
  | @impI _ a _ _ ih =>
    exact fun {Θ} h => .impI (ih (Θ := ext Θ (atr A a))
      (fun x k => k.elim (fun q => q ▸ h0) (fun g => wk (h x g))))
  | impE _ _ ihf iha => exact fun h => .impE (ihf h) (iha h)

/-- Friedman's trick, propositional form: from ⊢_i ¬¬p get ⊢_i p via A := p -/
theorem friedman_atom (n : Nat) (d : PD true (fun _ => False) (N (.var n))) :
    PD true (fun _ => False) (.var n) :=
  .impE (atr_sound (.var n) d (fun _ g => g.elim))
    (.impI (.orE h0 h0 h0))

end Prp

end Found

#print axioms Found.Prp.gg_nn
#print axioms Found.Prp.glivenko
#print axioms Found.Prp.glivenko0
#print axioms Found.Prp.kuroda_gg
#print axioms Found.Prp.efqA
#print axioms Found.Prp.atr_sound
#print axioms Found.Prp.friedman_atom

found/DNeg1.lean

lean_all/found/DNeg1.lean

/-
  T89 (Friedman), part 1. found/DNeg1 — the Gödel–Gentzen negative
  translation for first-order logic: atoms are double negated, ∨ and ∃ are
  replaced by ¬(¬a ∧ ¬b) and ¬∀¬. Translated formulas are stable
  (¬¬a* → a* is intuitionistically derivable); the translation commutes with
  renaming; intuitionistic derivations translate to intuitionistic
  derivations; translated excluded-middle closures are derivable. Hence a
  classical derivation from a theory T gives an intuitionistic derivation of
  the translation from any hypotheses that derive the translated axioms of T.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.FOClassicalT

namespace Found

namespace DN

open FO FOCT

def neg (a : Fm) : Fm := .imp a .bot

/-- the Gödel–Gentzen translation -/
def gg : Fm → Fm
  | .atom r l => neg (neg (.atom r l))
  | .bot => .bot
  | .conj a b => .conj (gg a) (gg b)
  | .disj a b => neg (.conj (neg (gg a)) (neg (gg b)))
  | .imp a b => .imp (gg a) (gg b)
  | .all a => .all (gg a)
  | .ex a => neg (.all (neg (gg a)))

def ggL : List Fm → List Fm
  | [] => []
  | a :: l => gg a :: ggL l

theorem ren_gg : (a : Fm) → (ρ : Nat → Nat) → ren ρ (gg a) = gg (ren ρ a)
  | .atom _ _, _ => rfl
  | .bot, _ => rfl
  | .conj a b, ρ => by show Fm.conj _ _ = Fm.conj _ _; rw [ren_gg a ρ, ren_gg b ρ]
  | .disj a b, ρ => by
      show Fm.imp (Fm.conj (Fm.imp (ren ρ (gg a)) .bot) (Fm.imp (ren ρ (gg b)) .bot)) .bot = _
      rw [ren_gg a ρ, ren_gg b ρ]; rfl
  | .imp a b, ρ => by show Fm.imp _ _ = Fm.imp _ _; rw [ren_gg a ρ, ren_gg b ρ]
  | .all a, ρ => by show Fm.all _ = Fm.all _; rw [ren_gg a (up ρ)]
  | .ex a, ρ => by
      show Fm.imp (Fm.all (Fm.imp (ren (up ρ) (gg a)) .bot)) .bot = _
      rw [ren_gg a (up ρ)]; rfl

theorem inst_gg (y : Nat) (a : Fm) : inst y (gg a) = gg (inst y a) := ren_gg a _
theorem shift_gg (a : Fm) : shift (gg a) = gg (shift a) := ren_gg a _

theorem ggL_sh : (Γ : List Fm) → ggL (sh Γ) = sh (ggL Γ)
  | [] => rfl
  | a :: Γ => by
      show gg (shift a) :: ggL (sh Γ) = shift (gg a) :: sh (ggL Γ)
      rw [shift_gg, ggL_sh Γ]

theorem mem_ggL {a : Fm} : (Γ : List Fm) → a ∈ Γ → gg a ∈ ggL Γ
  | _ :: _, .head _ => .head _
  | _ :: Γ, .tail _ m => .tail _ (mem_ggL Γ m)

theorem ggL_app : (A B : List Fm) → ggL (A ++ B) = ggL A ++ ggL B
  | [], _ => rfl
  | a :: A, B => by show gg a :: ggL (A ++ B) = gg a :: (ggL A ++ ggL B); rw [ggL_app A B]

theorem sub_cons2 (x y : Fm) (Γ : List Fm) : Sub (x :: Γ) (x :: y :: Γ) := fun a m => by
  cases m with
  | head => exact .head _
  | tail _ m' => exact .tail _ (.tail _ m')

theorem sub_tl1 (y : Fm) (Γ : List Fm) : Sub Γ (y :: Γ) := fun _ m => .tail _ m

/-- translated formulas are stable -/
theorem stab : (a : Fm) → (Γ : List Fm) → Pf Γ (.imp (neg (neg (gg a))) (gg a))
  | .atom _ _, _ => .impI (.impI (.impE (.hyp (.tail _ (.head _)))
      (.impI (.impE (.hyp (.head _)) (.hyp (.tail _ (.head _)))))))
  | .bot, _ => .impI (.impE (.hyp (.head _)) (.impI (.hyp (.head _))))
  | .conj a b, Γ => .impI (.conjI
      (.impE (stab a _) (.impI (.impE (.hyp (.tail _ (.head _))) (.impI (.impE (.hyp (.tail _ (.head _)))
        (.conjL (.hyp (.head _))))))))
      (.impE (stab b _) (.impI (.impE (.hyp (.tail _ (.head _))) (.impI (.impE (.hyp (.tail _ (.head _)))
        (.conjR (.hyp (.head _)))))))))
  | .disj _ _, _ => .impI (.impI (.impE (.hyp (.tail _ (.head _))) (.impI (.impE (.hyp (.head _)) (.hyp (.tail _ (.head _)))))))
  | .imp a b, _ => .impI (.impI (.impE (stab b _) (.impI (.impE (.hyp (.tail _ (.tail _ (.head _))))
      (.impI (.impE (.hyp (.tail _ (.head _))) (.impE (.hyp (.head _)) (.hyp (.tail _ (.tail _ (.head _)))))))))))
  | .all a, Γ => .impI (.allI (.impE (stab a _) (.impI (.impE (.hyp (.tail _ (.head _)))
      (.impI (.impE (.hyp (.tail _ (.head _))) (by
        have h := Pf.allE (Γ := Fm.all (ren (up Nat.succ) (gg a)) :: neg (gg a) ::
          shift (neg (neg (Fm.all (gg a)))) :: sh Γ) 0 (Pf.hyp (.head _))
        rw [inst0_shift] at h
        exact h)))))))
  | .ex _, _ => .impI (.impI (.impE (.hyp (.tail _ (.head _))) (.impI (.impE (.hyp (.head _)) (.hyp (.tail _ (.head _)))))))

/-- intuitionistic derivations translate -/
theorem gg_pf {Γ : List Fm} {φ : Fm} (d : Pf Γ φ) : Pf (ggL Γ) (gg φ) := by
  induction d with
  | hyp m => exact .hyp (mem_ggL _ m)
  | botE _ ih => exact .botE ih
  | conjI _ _ iha ihb => exact .conjI iha ihb
  | conjL _ ih => exact .conjL ih
  | conjR _ ih => exact .conjR ih
  | disjL _ ih => exact .impI (.impE (.conjL (.hyp (.head _))) (weaken ih (sub_tl1 _ _)))
  | disjR _ ih => exact .impI (.impE (.conjR (.hyp (.head _))) (weaken ih (sub_tl1 _ _)))
  | @disjE Γ a b c _ _ _ ih ih1 ih2 =>
    exact .impE (stab c _) (.impI (.impE (weaken ih (sub_tl1 _ _)) (.conjI
      (.impI (.impE (.hyp (.tail _ (.head _))) (weaken ih1 (sub_cons2 _ _ _))))
      (.impI (.impE (.hyp (.tail _ (.head _))) (weaken ih2 (sub_cons2 _ _ _)))))))
  | impI _ ih => exact .impI ih
  | impE _ _ ih1 ih2 => exact .impE ih1 ih2
  | @allI Γ a _ ih =>
    rw [ggL_sh] at ih
    exact .allI ih
  | @allE Γ a y _ ih =>
    rw [← inst_gg]
    exact .allE y ih
  | @exI Γ a y _ ih =>
    rw [← inst_gg] at ih
    exact .impI (.impE (.allE y (.hyp (.head _))) (weaken ih (sub_tl1 _ _)))
  | @exE Γ a c _ _ ih1 ih2 =>
    have h2 : Pf (gg a :: sh (ggL Γ)) (shift (gg c)) := by
      have h : Pf (gg a :: ggL (sh Γ)) (gg (shift c)) := ih2
      rw [ggL_sh, ← shift_gg] at h
      exact h
    exact .impE (stab c _) (.impI (.impE (weaken ih1 (sub_tl1 _ _))
      (.allI (.impI (.impE (.hyp (.tail _ (.head _))) (weaken h2 (sub_cons2 _ _ _)))))))

def clo : Nat → Fm → Fm
  | 0, a => a
  | j + 1, a => .all (clo j a)

theorem gg_clo : (j : Nat) → (a : Fm) → gg (FOCT.clo j a) = clo j (gg a)
  | 0, _ => rfl
  | j + 1, a => congrArg Fm.all (gg_clo j a)

/-- translated excluded middle and its closures are derivable -/
theorem gg_lem (a : Fm) (Γ : List Fm) : Pf Γ (gg (FOCT.lemF a)) :=
  .impI (.impE (.conjR (.hyp (.head _))) (.conjL (.hyp (.head _))))

theorem clo_pf {a : Fm} (h : ∀ Γ, Pf Γ a) : (j : Nat) → ∀ Γ, Pf Γ (clo j a)
  | 0, Γ => h Γ
  | j + 1, Γ => .allI (clo_pf h j (sh Γ))

/-- discharging derivable hypotheses -/
theorem cut_list {E : List Fm} : (D : List Fm) → (∀ δ, δ ∈ D → Pf E δ) → ∀ {c : Fm}, Pf (D ++ E) c → Pf E c
  | [], _, _, d => d
  | δ :: D, h, _, d => .impE (cut_list D (fun x m => h x (.tail _ m)) (.impI d)) (h δ (.head _))

/-- classical derivations from a theory give intuitionistic derivations of the translation -/
theorem gg_sound {T : Fm → Prop} {S : List Fm} {φ : Fm} (h : PC T S φ) (E : List Fm)
    (hT : ∀ b σ j, T b → Pf (E ++ ggL S) (clo j (gg (ren σ b)))) : Pf (E ++ ggL S) (gg φ) :=
  match h with
  | ⟨Δ, cΔ, d⟩ =>
    have d' : Pf (ggL Δ ++ (E ++ ggL S)) (gg φ) := weaken (by have := gg_pf d; rw [ggL_app] at this; exact this)
      (fun x m => (mem_app_inv' _ _ m).elim (fun m1 => mem_app_l' _ _ m1)
        (fun m2 => mem_app_r' _ _ (mem_app_r' _ _ m2)))
    cut_list (ggL Δ) (fun δ m => by
      have aux : ∀ (L : List Fm), (∀ x, x ∈ L → InT T x) → δ ∈ ggL L → Pf (E ++ ggL S) δ := by
        intro L
        induction L with
        | nil => intro _ m; exact nomatch m
        | cons x L ih =>
          intro hL m
          cases m with
          | head =>
            match hL x (.head _) with
            | ⟨j, a, ha, e⟩ =>
              rw [e, gg_clo]
              exact ha.elim (fun ⟨b, eb⟩ => by rw [eb]; exact clo_pf (fun Γ => gg_lem b Γ) j _)
                (fun ⟨b, σ, hb, eb⟩ => by rw [eb]; exact hT b σ j hb)
          | tail _ m' => exact ih (fun y my => hL y (.tail _ my)) m'
      exact aux Δ cΔ m) d'

end DN

end Found

#print axioms Found.DN.ren_gg
#print axioms Found.DN.inst_gg
#print axioms Found.DN.shift_gg
#print axioms Found.DN.ggL_sh
#print axioms Found.DN.mem_ggL
#print axioms Found.DN.ggL_app
#print axioms Found.DN.stab
#print axioms Found.DN.gg_pf
#print axioms Found.DN.gg_clo
#print axioms Found.DN.gg_lem
#print axioms Found.DN.clo_pf
#print axioms Found.DN.cut_list
#print axioms Found.DN.gg_sound

found/DNeg2.lean

lean_all/found/DNeg2.lean

/-
  T89 (Friedman), part 2. found/DNeg2 — set theory without extensionality
  in the language with ∈ only (Friedman 1973; the ε-axioms of Matthews,
  "A Guide to Krivine Realizability for Set Theory", §7): ∈-induction,
  separation, collection (schemes with parameters), pairing ∃c (a ∈ c ∧
  b ∈ c), union ∃b ∀x ∈ a ∀y ∈ x (y ∈ b), weak power set
  ∃b ∀x ∃y ∈ b ∀z (z ∈ y ↔ z ∈ a ∧ z ∈ x), infinity with successors up to
  coextension. With classical logic this is
  ZF∖Ext, with intuitionistic logic IZF∖Ext; the Gödel–Gentzen
  translation commutes with the parameter shifts of the schemes.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.DNeg1
import found.NbgZ4

namespace Found

namespace DN

open FO FOCT NBGc

/-- parameters of a scheme: keep the first k variables, shift the rest by j -/
def keep : Nat → Nat → Nat → Nat
  | 0, j, n => n + j
  | _ + 1, _, 0 => 0
  | k + 1, j, n + 1 => keep k j n + 1

def indAx (φ : Fm) : Fm :=
  .imp (.all (.imp (.all (.imp (m 0 1) (ren (keep 1 1) φ))) φ)) (.all φ)

def sepAx (φ : Fm) : Fm :=
  .all (.ex (.all (iffF (m 0 1) (.conj (m 0 2) (ren (keep 1 2) φ)))))

def collAx (φ : Fm) : Fm :=
  .all (.ex (.all (.imp (m 0 2) (.imp (.ex (ren (keep 2 2) φ)) (.ex (.conj (m 0 2) (ren (keep 2 2) φ)))))))

def pairW : Fm := .all (.all (.ex (.conj (m 2 0) (m 1 0))))
def unionW : Fm := .all (.ex (.all (.imp (m 0 2) (.all (.imp (m 0 1) (m 0 2))))))
def wpowAx : Fm := .all (.ex (.all (.ex (.conj (m 0 2) (.all (iffF (m 0 1) (.conj (m 0 4) (m 0 2))))))))
/-- infinity with successors up to coextension: an empty member, and every member x has a member y
  with x ∈ y, x ⊆ y and every member of y in x or coextensive with x -/
def infW : Fm := .ex (.conj (.ex (.conj (m 0 1) (.all (.imp (m 0 1) .bot))))
  (.all (.imp (m 0 1) (.ex (.conj (m 0 2) (.conj (m 1 0) (.conj (.all (.imp (m 0 2) (m 0 1)))
    (.all (.imp (m 0 1) (.disj (m 0 2) (EqF 0 2)))))))))))

/-- ZF without extensionality (classical) / IZF without extensionality (intuitionistic) -/
def ZFE (φ : Fm) : Prop :=
  (∃ θ, L0 θ ∧ φ = indAx θ) ∨ (∃ θ, L0 θ ∧ φ = sepAx θ) ∨ (∃ θ, L0 θ ∧ φ = collAx θ) ∨
  φ = pairW ∨ φ = unionW ∨ φ = wpowAx ∨ φ = infW

/-- intuitionistic derivability from closures of renamed axioms -/
def IPC (T : Fm → Prop) (S : List Fm) (φ : Fm) : Prop :=
  ∃ Δ, (∀ δ, δ ∈ Δ → ∃ j b σ, T b ∧ δ = clo j (ren σ b)) ∧ Pf (Δ ++ S) φ

/-- the translation of a scheme instance is the scheme instance of the translation -/
theorem gg_ren_keep (k j : Nat) (φ : Fm) : gg (ren (keep k j) φ) = ren (keep k j) (gg φ) := (ren_gg φ _).symm

end DN

end Found

#print axioms Found.DN.gg_ren_keep

found/DNeg3.lean

lean_all/found/DNeg3.lean

/-
  T89 (Friedman), part 3. found/DNeg3 — formulas whose Gödel–Gentzen
  translation follows from them intuitionistically: the class Up (closed
  under atoms, ⊥, ∧, ∨, ∃, ∀, and A → B with A in Dn and B in Up) satisfies
  ⊢ A → A*, and the class Dn (atoms, ⊥, ∧, ∃, and A → B with A in Up and B
  in Dn) satisfies ⊢ A* → ¬¬A. Pairing, union, weak power set and infinity
  of IZF∖Ext are in Up, so their translations are derivable from them.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.DNeg2

namespace Found

namespace DN

open FO FOCT NBGc

/-- (Up, Dn) -/
def UD : Fm → Prop × Prop
  | .atom _ _ => (True, True)
  | .bot => (True, True)
  | .conj a b => ((UD a).1 ∧ (UD b).1, (UD a).2 ∧ (UD b).2)
  | .disj a b => ((UD a).1 ∧ (UD b).1, False)
  | .imp a b => ((UD a).2 ∧ (UD b).1, (UD a).1 ∧ (UD b).2)
  | .all a => ((UD a).1, False)
  | .ex a => ((UD a).1, (UD a).2)

theorem ud : (a : Fm) → ((UD a).1 → ∀ Γ, Pf Γ (.imp a (gg a))) ∧ ((UD a).2 → ∀ Γ, Pf Γ (.imp (gg a) (neg (neg a))))
  | .atom _ _ => ⟨fun _ _ => .impI (.impI (.impE (.hyp (.head _)) (.hyp (.tail _ (.head _))))),
      fun _ _ => .impI (.hyp (.head _))⟩
  | .bot => ⟨fun _ _ => .impI (.hyp (.head _)), fun _ _ => .impI (.botE (.hyp (.head _)))⟩
  | .conj a b =>
    have ha := ud a
    have hb := ud b
    ⟨fun ⟨ua, ub⟩ _ => .impI (.conjI (.impE (ha.1 ua _) (.conjL (.hyp (.head _)))) (.impE (hb.1 ub _) (.conjR (.hyp (.head _))))),
     fun ⟨da, db⟩ _ => .impI (.impI (.impE (.impE (ha.2 da _) (.conjL (.hyp (.tail _ (.head _)))))
      (.impI (.impE (.impE (hb.2 db _) (.conjR (.hyp (.tail _ (.tail _ (.head _))))))
        (.impI (.impE (.hyp (.tail _ (.tail _ (.head _)))) (.conjI (.hyp (.tail _ (.head _))) (.hyp (.head _)))))))))⟩
  | .disj a b =>
    have ha := ud a
    have hb := ud b
    ⟨fun ⟨ua, ub⟩ _ => .impI (.impI (.disjE (.hyp (.tail _ (.head _)))
      (.impE (.conjL (.hyp (.tail _ (.head _)))) (.impE (ha.1 ua _) (.hyp (.head _))))
      (.impE (.conjR (.hyp (.tail _ (.head _)))) (.impE (hb.1 ub _) (.hyp (.head _)))))),
     fun h => h.elim⟩
  | .imp a b =>
    have ha := ud a
    have hb := ud b
    ⟨fun ⟨da, ub⟩ _ => .impI (.impI (.impE (stab b _) (.impI (.impE (.impE (ha.2 da _) (.hyp (.tail _ (.head _))))
      (.impI (.impE (.hyp (.tail _ (.head _))) (.impE (hb.1 ub _)
        (.impE (.hyp (.tail _ (.tail _ (.tail _ (.head _))))) (.hyp (.head _)))))))))),
     fun ⟨ua, db⟩ Γ =>
      have na : Pf (neg (Fm.imp a b) :: Fm.imp (gg a) (gg b) :: Γ) (neg a) :=
        .impI (.impE (.impE (hb.2 db _) (.impE (.hyp (.tail _ (.tail _ (.head _)))) (.impE (ha.1 ua _) (.hyp (.head _)))))
          (.impI (.impE (.hyp (.tail _ (.tail _ (.head _)))) (.impI (.hyp (.tail _ (.head _)))))))
      .impI (.impI (.impE (.hyp (.head _)) (.impI (.botE (.impE (weaken na (sub_tl1 _ _)) (.hyp (.head _)))))))⟩
  | .all a =>
    have ha := ud a
    ⟨fun ua Γ => .impI (.allI (.impE (ha.1 ua _) (by
        have h := Pf.allE (Γ := sh (Fm.all a :: Γ)) 0 (Pf.hyp (a := Fm.all (ren (up Nat.succ) a)) (.head _))
        rw [inst0_shift] at h
        exact h))),
     fun h => h.elim⟩
  | .ex a =>
    have ha := ud a
    ⟨fun ua Γ => .impI (.impI (.exE (.hyp (.tail _ (.head _))) (.impE (by
        have h := Pf.allE (Γ := a :: sh (Fm.all (neg (gg a)) :: Fm.ex a :: Γ)) 0
          (Pf.hyp (a := Fm.all (ren (up Nat.succ) (neg (gg a)))) (.tail _ (.head _)))
        rw [inst0_shift] at h
        exact h) (.impE (ha.1 ua _) (.hyp (.head _)))))),
     fun da Γ => .impI (.impI (.impE (.hyp (.tail _ (.head _))) (.allI (.impI (.impE (.impE (ha.2 da _) (.hyp (.head _)))
      (.impI (.impE (.hyp (.tail _ (.tail _ (.head _)))) (.exI 0 (by
        show Pf _ (inst 0 (ren (up Nat.succ) a))
        rw [inst0_shift]
        exact .hyp (.head _))))))))))⟩

theorem up_gg {a : Fm} (hu : (UD a).1) {Γ : List Fm} (d : Pf Γ a) : Pf Γ (gg a) := .impE ((ud a).1 hu Γ) d

theorem ud_pair : (UD pairW).1 := ⟨trivial, trivial⟩
theorem ud_union : (UD unionW).1 := ⟨trivial, trivial, trivial⟩
theorem ud_wpow : (UD wpowAx).1 := ⟨trivial, ⟨trivial, ⟨trivial, trivial⟩⟩, ⟨⟨trivial, trivial⟩, trivial⟩⟩
theorem ud_inf : (UD infW).1 := by
  repeat (first | exact trivial | constructor)

end DN

end Found

#print axioms Found.DN.ud
#print axioms Found.DN.up_gg
#print axioms Found.DN.ud_pair
#print axioms Found.DN.ud_union
#print axioms Found.DN.ud_wpow
#print axioms Found.DN.ud_inf

found/DNeg4.lean

lean_all/found/DNeg4.lean

/-
  T89 (Friedman), part 4. found/DNeg4 — the translations of the schemes of
  IZF∖Ext. A relation TT m a b between a formula a and a partial
  translation b (translated outside, with stable "holes" kept equal) in
  three modes: U (⊢ a → b), D (⊢ b → ¬¬a), E (⊢ b → a). Atoms are U and D,
  ∀ is U and E, ∃ is U and D, implications switch mode in the antecedent.
  Targets are stable. The separation, ∈-induction and collection schemes
  for a translated formula imply the translations of the schemes (the
  collection hypothesis ∃y φ* sits in D-mode).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.DNeg3

namespace Found

namespace DN

open FO FOCT NBGc

inductive Md where
  | U | D | E

def Stab (s : Fm) : Prop := ∀ Γ, Pf Γ (.imp (neg (neg s)) s)

inductive TT : Md → Fm → Fm → Prop where
  | atomU (r : Nat) (l : List Nat) : TT .U (.atom r l) (neg (neg (.atom r l)))
  | atomD (r : Nat) (l : List Nat) : TT .D (.atom r l) (neg (neg (.atom r l)))
  | bot (m : Md) : TT m .bot .bot
  | hole (m : Md) (s : Fm) : Stab s → TT m s s
  | conj (m : Md) {a a' b b' : Fm} : TT m a a' → TT m b b' → TT m (.conj a b) (.conj a' b')
  | disj {a a' b b' : Fm} : TT .U a a' → TT .U b b' → TT .U (.disj a b) (neg (.conj (neg a') (neg b')))
  | impU {a a' b b' : Fm} : TT .D a a' → TT .U b b' → TT .U (.imp a b) (.imp a' b')
  | impD {a a' b b' : Fm} : TT .U a a' → TT .D b b' → TT .D (.imp a b) (.imp a' b')
  | impE {a a' b b' : Fm} : TT .U a a' → TT .E b b' → TT .E (.imp a b) (.imp a' b')
  | allU {a a' : Fm} : TT .U a a' → TT .U (.all a) (.all a')
  | allE {a a' : Fm} : TT .E a a' → TT .E (.all a) (.all a')
  | exU {a a' : Fm} : TT .U a a' → TT .U (.ex a) (neg (.all (neg a')))
  | exD {a a' : Fm} : TT .D a a' → TT .D (.ex a) (neg (.all (neg a')))
  | eD {a a' : Fm} : TT .E a a' → TT .D a a'

def Concl : Md → Fm → Fm → Prop
  | .U, a, b => ∀ Γ, Pf Γ (.imp a b)
  | .D, a, b => ∀ Γ, Pf Γ (.imp b (neg (neg a)))
  | .E, a, b => ∀ Γ, Pf Γ (.imp b a)

/-! ## stability -/

theorem st_atom (r : Nat) (l : List Nat) : Stab (neg (neg (.atom r l))) := fun _ =>
  .impI (.impI (.impE (.hyp (.tail _ (.head _))) (.impI (.impE (.hyp (.head _)) (.hyp (.tail _ (.head _)))))))

theorem st_bot : Stab .bot := fun _ => .impI (.impE (.hyp (.head _)) (.impI (.hyp (.head _))))

theorem st_neg (x : Fm) : Stab (neg x) := fun _ =>
  .impI (.impI (.impE (.hyp (.tail _ (.head _))) (.impI (.impE (.hyp (.head _)) (.hyp (.tail _ (.head _)))))))

theorem st_conj {a b : Fm} (sa : Stab a) (sb : Stab b) : Stab (.conj a b) := fun _ => .impI (.conjI
  (.impE (sa _) (.impI (.impE (.hyp (.tail _ (.head _))) (.impI (.impE (.hyp (.tail _ (.head _)))
    (.conjL (.hyp (.head _))))))))
  (.impE (sb _) (.impI (.impE (.hyp (.tail _ (.head _))) (.impI (.impE (.hyp (.tail _ (.head _)))
    (.conjR (.hyp (.head _)))))))))

theorem st_imp (a : Fm) {b : Fm} (sb : Stab b) : Stab (.imp a b) := fun _ =>
  .impI (.impI (.impE (sb _) (.impI (.impE (.hyp (.tail _ (.tail _ (.head _))))
    (.impI (.impE (.hyp (.tail _ (.head _))) (.impE (.hyp (.head _)) (.hyp (.tail _ (.tail _ (.head _)))))))))))

theorem st_all {a : Fm} (sa : Stab a) : Stab (.all a) := fun Γ =>
  .impI (.allI (.impE (sa _) (.impI (.impE (.hyp (.tail _ (.head _)))
    (.impI (.impE (.hyp (.tail _ (.head _))) (by
      have h := Pf.allE (Γ := Fm.all (ren (up Nat.succ) a) :: neg a ::
        shift (neg (neg (Fm.all a))) :: sh Γ) 0 (Pf.hyp (.head _))
      rw [inst0_shift] at h
      exact h)))))))

theorem tstab {m : Md} {a b : Fm} (h : TT m a b) : Stab b := by
  induction h with
  | atomU r l => exact st_atom r l
  | atomD r l => exact st_atom r l
  | bot _ => exact st_bot
  | hole _ _ hs => exact hs
  | conj _ _ _ iha ihb => exact st_conj iha ihb
  | disj _ _ _ _ => exact st_neg _
  | impU _ _ _ ihb => exact st_imp _ ihb
  | impD _ _ _ ihb => exact st_imp _ ihb
  | impE _ _ _ ihb => exact st_imp _ ihb
  | allU _ ih => exact st_all ih
  | allE _ ih => exact st_all ih
  | exU _ _ => exact st_neg _
  | exD _ _ => exact st_neg _
  | eD _ ih => exact ih

/-! ## soundness of the three modes -/

theorem tt_sound {m : Md} {a b : Fm} (h : TT m a b) : Concl m a b := by
  induction h with
  | atomU _ _ => exact fun _ => .impI (.impI (.impE (.hyp (.head _)) (.hyp (.tail _ (.head _)))))
  | atomD _ _ => exact fun _ => .impI (.hyp (.head _))
  | bot m =>
    cases m with
    | U => exact fun _ => .impI (.hyp (.head _))
    | D => exact fun _ => .impI (.botE (.hyp (.head _)))
    | E => exact fun _ => .impI (.hyp (.head _))
  | hole m _ _ =>
    cases m with
    | U => exact fun _ => .impI (.hyp (.head _))
    | D => exact fun _ => .impI (.impI (.impE (.hyp (.head _)) (.hyp (.tail _ (.head _)))))
    | E => exact fun _ => .impI (.hyp (.head _))
  | conj m _ _ iha ihb =>
    cases m with
    | U => exact fun _ => .impI (.conjI (.impE (iha _) (.conjL (.hyp (.head _)))) (.impE (ihb _) (.conjR (.hyp (.head _)))))
    | D => exact fun _ => .impI (.impI (.impE (.impE (iha _) (.conjL (.hyp (.tail _ (.head _)))))
      (.impI (.impE (.impE (ihb _) (.conjR (.hyp (.tail _ (.tail _ (.head _))))))
        (.impI (.impE (.hyp (.tail _ (.tail _ (.head _)))) (.conjI (.hyp (.tail _ (.head _))) (.hyp (.head _)))))))))
    | E => exact fun _ => .impI (.conjI (.impE (iha _) (.conjL (.hyp (.head _)))) (.impE (ihb _) (.conjR (.hyp (.head _)))))
  | disj _ _ iha ihb =>
    exact fun _ => .impI (.impI (.disjE (.hyp (.tail _ (.head _)))
      (.impE (.conjL (.hyp (.tail _ (.head _)))) (.impE (iha _) (.hyp (.head _))))
      (.impE (.conjR (.hyp (.tail _ (.head _)))) (.impE (ihb _) (.hyp (.head _))))))
  | impU _ hb iha ihb =>
    exact fun _ => .impI (.impI (.impE (tstab hb _) (.impI (.impE (.impE (iha _) (.hyp (.tail _ (.head _))))
      (.impI (.impE (.hyp (.tail _ (.head _))) (.impE (ihb _)
        (.impE (.hyp (.tail _ (.tail _ (.tail _ (.head _))))) (.hyp (.head _))))))))))
  | @impD a a' b b' _ _ iha ihb =>
    exact fun Γ =>
      have na : Pf (neg (Fm.imp a b) :: Fm.imp a' b' :: Γ) (neg a) :=
        .impI (.impE (.impE (ihb _) (.impE (.hyp (.tail _ (.tail _ (.head _)))) (.impE (iha _) (.hyp (.head _)))))
          (.impI (.impE (.hyp (.tail _ (.tail _ (.head _)))) (.impI (.hyp (.tail _ (.head _)))))))
      .impI (.impI (.impE (.hyp (.head _)) (.impI (.botE (.impE (weaken na (sub_tl1 _ _)) (.hyp (.head _)))))))
  | impE _ _ iha ihb =>
    exact fun _ => .impI (.impI (.impE (ihb _) (.impE (.hyp (.tail _ (.head _))) (.impE (iha _) (.hyp (.head _))))))
  | @allU a a' _ ih =>
    exact fun Γ => .impI (.allI (.impE (ih _) (by
        have h := Pf.allE (Γ := sh (Fm.all a :: Γ)) 0 (Pf.hyp (a := Fm.all (ren (up Nat.succ) a)) (.head _))
        rw [inst0_shift] at h
        exact h)))
  | @allE a a' _ ih =>
    exact fun Γ => .impI (.allI (.impE (ih _) (by
        have h := Pf.allE (Γ := sh (Fm.all a' :: Γ)) 0 (Pf.hyp (a := Fm.all (ren (up Nat.succ) a')) (.head _))
        rw [inst0_shift] at h
        exact h)))
  | @exU a a' _ ih =>
    exact fun Γ => .impI (.impI (.exE (.hyp (.tail _ (.head _))) (.impE (by
        have h := Pf.allE (Γ := a :: sh (Fm.all (neg a') :: Fm.ex a :: Γ)) 0
          (Pf.hyp (a := Fm.all (ren (up Nat.succ) (neg a'))) (.tail _ (.head _)))
        rw [inst0_shift] at h
        exact h) (.impE (ih _) (.hyp (.head _))))))
  | @exD a a' _ ih =>
    exact fun _ => .impI (.impI (.impE (.hyp (.tail _ (.head _))) (.allI (.impI (.impE (.impE (ih _) (.hyp (.head _)))
      (.impI (.impE (.hyp (.tail _ (.tail _ (.head _)))) (.exI 0 (by
        show Pf _ (inst 0 (ren (up Nat.succ) a))
        rw [inst0_shift]
        exact .hyp (.head _))))))))))
  | eD _ ih =>
    exact fun _ => .impI (.impI (.impE (.hyp (.head _)) (.impE (ih _) (.hyp (.tail _ (.head _))))))

/-! ## the schemes -/

theorem l0_gg : (a : Fm) → L0 a → L0 (gg a)
  | .atom _ _, h => ⟨⟨h, trivial⟩, trivial⟩
  | .bot, _ => trivial
  | .conj a b, h => ⟨l0_gg a h.1, l0_gg b h.2⟩
  | .disj a b, h => ⟨⟨⟨l0_gg a h.1, trivial⟩, ⟨l0_gg b h.2, trivial⟩⟩, trivial⟩
  | .imp a b, h => ⟨l0_gg a h.1, l0_gg b h.2⟩
  | .all a, h => l0_gg a h
  | .ex a, h => ⟨⟨l0_gg a h, trivial⟩, trivial⟩

theorem stab_ren (k j : Nat) (θ : Fm) : Stab (gg (ren (keep k j) θ)) := fun Γ => stab _ Γ

theorem sep_gg (θ : Fm) : ∀ Γ, Pf Γ (.imp (sepAx (gg θ)) (gg (sepAx θ))) := by
  have e : ren (keep 1 2) (gg θ) = gg (ren (keep 1 2) θ) := ren_gg θ _
  unfold sepAx
  rw [e]
  have hs := stab_ren 1 2 θ
  exact tt_sound (m := .U) (.allU (.exU (.allU (.conj .U
    (.impU (.atomD 0 _) (.conj .U (.atomU 0 _) (.hole .U _ hs)))
    (.impU (.conj .D (.atomD 0 _) (.hole .D _ hs)) (.atomU 0 _))))))

theorem ind_gg (θ : Fm) : ∀ Γ, Pf Γ (.imp (indAx (gg θ)) (gg (indAx θ))) := by
  have e : ren (keep 1 1) (gg θ) = gg (ren (keep 1 1) θ) := ren_gg θ _
  unfold indAx
  rw [e]
  have hs := stab_ren 1 1 θ
  have h0 : Stab (gg θ) := fun Γ => stab θ Γ
  exact tt_sound (m := .U) (.impU (.eD (.allE (.impE (.allU (.impU (.atomD 0 _) (.hole .U _ hs))) (.hole .E _ h0))))
    (.allU (.hole .U _ h0)))

theorem coll_gg (θ : Fm) : ∀ Γ, Pf Γ (.imp (collAx (gg θ)) (gg (collAx θ))) := by
  have e : ren (keep 2 2) (gg θ) = gg (ren (keep 2 2) θ) := ren_gg θ _
  unfold collAx
  rw [e]
  have hs := stab_ren 2 2 θ
  exact tt_sound (m := .U) (.allU (.exU (.allU (.impU (.atomD 0 _)
    (.impU (.exD (.hole .D _ hs)) (.exU (.conj .U (.atomU 0 _) (.hole .U _ hs))))))))

/-- every axiom of IZF∖Ext implies the translation of the corresponding axiom -/
theorem ax_gg {b : Fm} (hb : ZFE b) : ∃ b', ZFE b' ∧ ∀ Γ, Pf Γ (.imp b' (gg b)) :=
  match hb with
  | .inl ⟨θ, hl, e⟩ => ⟨indAx (gg θ), .inl ⟨gg θ, l0_gg θ hl, rfl⟩, by rw [e]; exact ind_gg θ⟩
  | .inr (.inl ⟨θ, hl, e⟩) => ⟨sepAx (gg θ), .inr (.inl ⟨gg θ, l0_gg θ hl, rfl⟩), by rw [e]; exact sep_gg θ⟩
  | .inr (.inr (.inl ⟨θ, hl, e⟩)) => ⟨collAx (gg θ), .inr (.inr (.inl ⟨gg θ, l0_gg θ hl, rfl⟩)), by rw [e]; exact coll_gg θ⟩
  | .inr (.inr (.inr (.inl e))) => ⟨b, hb, fun Γ => by rw [e]; exact (ud pairW).1 ud_pair Γ⟩
  | .inr (.inr (.inr (.inr (.inl e)))) => ⟨b, hb, fun Γ => by rw [e]; exact (ud unionW).1 ud_union Γ⟩
  | .inr (.inr (.inr (.inr (.inr (.inl e))))) => ⟨b, hb, fun Γ => by rw [e]; exact (ud wpowAx).1 ud_wpow Γ⟩
  | .inr (.inr (.inr (.inr (.inr (.inr e))))) => ⟨b, hb, fun Γ => by rw [e]; exact (ud infW).1 ud_inf Γ⟩

end DN

end Found

#print axioms Found.DN.tstab
#print axioms Found.DN.tt_sound
#print axioms Found.DN.l0_gg
#print axioms Found.DN.stab_ren
#print axioms Found.DN.sep_gg
#print axioms Found.DN.ind_gg
#print axioms Found.DN.coll_gg
#print axioms Found.DN.ax_gg

found/DNeg5.lean

lean_all/found/DNeg5.lean

/-
  T89 (Friedman), part 5. found/DNeg5 — the double-negation interpretation
  of ZF∖Ext in IZF∖Ext: if ZF∖Ext proves φ classically from hypotheses S,
  then IZF∖Ext proves the Gödel–Gentzen translation φ* intuitionistically
  from the translated hypotheses. (Each axiom closure used is replaced by
  the closure of the axiom of IZF∖Ext that implies its translation.)

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.DNeg4

namespace Found

namespace DN

open FO FOCT NBGc

/-- closures are monotone -/
theorem clo_mono {X Y : Fm} (h : ∀ Γ, Pf Γ (.imp X Y)) : (j : Nat) → ∀ Γ, Pf Γ (.imp (clo j X) (clo j Y))
  | 0, Γ => h Γ
  | j + 1, Γ => .impI (.allI (.impE (clo_mono h j _) (by
      have d := Pf.allE (Γ := sh (Fm.all (clo j X) :: Γ)) 0 (Pf.hyp (a := Fm.all (ren (up Nat.succ) (clo j X))) (.head _))
      rw [inst0_shift] at d
      exact d)))

theorem ren_imp_pf {X Y : Fm} (h : ∀ Γ, Pf Γ (.imp X Y)) (σ : Nat → Nat) : ∀ Γ, Pf Γ (.imp (ren σ X) (ren σ Y)) :=
  fun _ => weaken (pf_ren (h []) σ) (fun _ m => nomatch m)

theorem gg_cloT (j : Nat) (a : Fm) : gg (FOCT.clo j a) = clo j (gg a) := gg_clo j a

/-- the IZF∖Ext closures that derive the translated axiom closures of a context -/
theorem exists_E : (Δ : List Fm) → CtxT ZFE Δ →
    ∃ E : List Fm, (∀ δ, δ ∈ E → ∃ j b σ, ZFE b ∧ δ = clo j (ren σ b)) ∧ ∀ δ, δ ∈ Δ → Pf E (gg δ)
  | [], _ => ⟨[], (fun _ m => nomatch m), (fun _ m => nomatch m)⟩
  | δ :: Δ, h => by
    obtain ⟨E0, hE0, dE0⟩ := exists_E Δ (fun x m => h x (.tail _ m))
    obtain ⟨j, a, ha, e⟩ := h δ (.head _)
    cases ha with
    | inl hl =>
      obtain ⟨b, eb⟩ := hl
      refine ⟨E0, hE0, fun x m => ?_⟩
      cases m with
      | head => rw [e, gg_cloT, eb]; exact clo_pf (fun Γ => gg_lem b Γ) j _
      | tail _ m' => exact dE0 x m'
    | inr hr =>
      obtain ⟨b, σ, hb, eb⟩ := hr
      obtain ⟨b', hb', imp⟩ := ax_gg hb
      refine ⟨clo j (ren σ b') :: E0, fun x m => ?_, fun x m => ?_⟩
      · cases m with
        | head => exact ⟨j, b', σ, hb', rfl⟩
        | tail _ m' => exact hE0 x m'
      · cases m with
        | head =>
          rw [e, gg_cloT, eb, ← ren_gg]
          exact .impE (clo_mono (ren_imp_pf imp σ) j _) (.hyp (.head _))
        | tail _ m' => exact weaken (dE0 x m') (sub_tl1 _ _)

/-- Friedman's second interpretation: ZF∖Ext ⊢ φ ⇒ IZF∖Ext ⊢ φ* -/
theorem zfe_izfe {S : List Fm} {φ : Fm} (h : PC ZFE S φ) : IPC ZFE (ggL S) (gg φ) :=
  match h with
  | ⟨Δ, cΔ, d⟩ =>
  match exists_E Δ cΔ with
  | ⟨E, hE, dE⟩ =>
    have d' : Pf (ggL Δ ++ (E ++ ggL S)) (gg φ) := weaken (by have := gg_pf d; rw [ggL_app] at this; exact this)
      (fun x m => (mem_app_inv' _ _ m).elim (fun m1 => mem_app_l' _ _ m1)
        (fun m2 => mem_app_r' _ _ (mem_app_r' _ _ m2)))
    have hD : ∀ δ, δ ∈ ggL Δ → Pf (E ++ ggL S) δ := by
      have aux : ∀ (L : List Fm), (∀ x, x ∈ L → Pf E (gg x)) → ∀ δ, δ ∈ ggL L → Pf (E ++ ggL S) δ := by
        intro L
        induction L with
        | nil => intro _ _ m; exact nomatch m
        | cons x L ih =>
          intro hL δ m
          cases m with
          | head => exact weaken (hL x (.head _)) (fun _ m => mem_app_l' _ _ m)
          | tail _ m' => exact ih (fun y my => hL y (.tail _ my)) δ m'
      exact aux Δ dE
    ⟨E, hE, cut_list (ggL Δ) hD d'⟩

end DN

end Found

#print axioms Found.DN.clo_mono
#print axioms Found.DN.ren_imp_pf
#print axioms Found.DN.exists_E
#print axioms Found.DN.zfe_izfe

found/DRSound.lean

lean_all/found/DRSound.lean

/-
  T179, Danos–Regnier soundness. found/DRSound — the proof structure of an
  MLL derivation (found/ProofNets) and its switching graphs:
  * vertices are numbered while the structure is built (one vertex per
    link conclusion); links ax (i, j), ⊗ (i, j ↦ k), ⅋ (i, j ↦ k);
  * a switching s chooses, for each ⅋-vertex k, one premise (s k = true:
    left); the switching graph has the edges of ax, both ⊗-edges and the
    chosen ⅋-edge;
  * Danos–Regnier soundness: for every derivation and every switching, the
    switching graph is connected and has exactly one edge fewer than
    vertices — the characterisation of finite trees.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.ProofNets

namespace Found

namespace DR

open PN

/-! ## lists of edges, membership, connectivity -/

abbrev E := List (Nat × Nat)

theorem mem_l {x : Nat × Nat} : {A : E} → (B : E) → x ∈ A → x ∈ A ++ B
  | _ :: _, _, .head _ => .head _
  | _ :: _, B, .tail _ m => .tail _ (mem_l B m)

theorem mem_r {x : Nat × Nat} : (A : E) → {B : E} → x ∈ B → x ∈ A ++ B
  | [], _, m => m
  | _ :: A, _, m => .tail _ (mem_r A m)

def len : E → Nat
  | [] => 0
  | _ :: l => len l + 1

theorem len_app : (A B : E) → len (A ++ B) = len A + len B
  | [], B => (Nat.zero_add _).symm
  | _ :: A, B => by
    show len (A ++ B) + 1 = len A + 1 + len B
    rw [len_app A B, Nat.add_right_comm]

/-- connectivity in the undirected graph with edge list G -/
inductive Conn (G : E) : Nat → Nat → Prop where
  | refl (x : Nat) : Conn G x x
  | step {x y z : Nat} : ((x, y) ∈ G ∨ (y, x) ∈ G) → Conn G y z → Conn G x z

theorem conn_trans {G : E} {x y z : Nat} (h1 : Conn G x y) (h2 : Conn G y z) : Conn G x z := by
  induction h1 with
  | refl => exact h2
  | step e _ ih => exact .step e (ih h2)

theorem conn_symm {G : E} {x y : Nat} (h : Conn G x y) : Conn G y x := by
  induction h with
  | refl => exact .refl _
  | step e _ ih => exact conn_trans ih (.step (e.elim Or.inr Or.inl) (.refl _))

theorem conn_mono {G H : E} (hs : ∀ e, e ∈ G → e ∈ H) {x y : Nat} (h : Conn G x y) : Conn H x y := by
  induction h with
  | refl => exact .refl _
  | step e _ ih => exact .step (e.elim (fun m => Or.inl (hs _ m)) (fun m => Or.inr (hs _ m))) ih

theorem conn_edge {G : E} {x y : Nat} (m : (x, y) ∈ G) : Conn G x y := .step (Or.inl m) (.refl _)

theorem conn_edge' {G : E} {x y : Nat} (m : (x, y) ∈ G) : Conn G y x := .step (Or.inr m) (.refl _)

/-! ## vertex tuples along a sequent -/

def Vs : List F → Type
  | [] => PUnit
  | _ :: l => Nat × Vs l

def join : (Γ : List F) → {Δ : List F} → Vs Γ → Vs Δ → Vs (Γ ++ Δ)
  | [], _, _, w => w
  | _ :: Γ, _, v, w => (v.1, join Γ v.2 w)

def split : (Γ : List F) → {Δ : List F} → Vs (Γ ++ Δ) → Vs Γ × Vs Δ
  | [], _, w => (PUnit.unit, w)
  | _ :: Γ, _, v => ((v.1, (split Γ v.2).1), (split Γ v.2).2)

def InR (lo hi v : Nat) : Prop := lo ≤ v ∧ v < hi

def AllIn (lo hi : Nat) : (Γ : List F) → Vs Γ → Prop
  | [], _ => True
  | _ :: l, v => InR lo hi v.1 ∧ AllIn lo hi l v.2

theorem allIn_join (lo hi : Nat) : (Γ : List F) → {Δ : List F} → (v : Vs Γ) → (w : Vs Δ) →
    AllIn lo hi Γ v → AllIn lo hi Δ w → AllIn lo hi (Γ ++ Δ) (join Γ v w)
  | [], _, _, _, _, hw => hw
  | _ :: Γ, _, v, w, hv, hw => ⟨hv.1, allIn_join lo hi Γ v.2 w hv.2 hw⟩

theorem allIn_split (lo hi : Nat) : (Γ : List F) → {Δ : List F} → (v : Vs (Γ ++ Δ)) →
    AllIn lo hi (Γ ++ Δ) v → AllIn lo hi Γ (split Γ v).1 ∧ AllIn lo hi Δ (split Γ v).2
  | [], _, _, h => ⟨trivial, h⟩
  | _ :: Γ, _, v, h => ⟨⟨h.1, (allIn_split lo hi Γ v.2 h.2).1⟩, (allIn_split lo hi Γ v.2 h.2).2⟩

theorem inR_widen {lo hi lo' hi' v : Nat} (h1 : lo' ≤ lo) (h2 : hi ≤ hi') (h : InR lo hi v) : InR lo' hi' v :=
  ⟨Nat.le_trans h1 h.1, Nat.lt_of_lt_of_le h.2 h2⟩

theorem allIn_widen {lo hi lo' hi' : Nat} (h1 : lo' ≤ lo) (h2 : hi ≤ hi') : (Γ : List F) → (v : Vs Γ) →
    AllIn lo hi Γ v → AllIn lo' hi' Γ v
  | [], _, _ => trivial
  | _ :: Γ, v, h => ⟨inR_widen h1 h2 h.1, allIn_widen h1 h2 Γ v.2 h.2⟩

/-! ## the proof structure and its switching graph -/

structure R (Γ : List F) where
  G : E
  vs : Vs Γ
  nx : Nat

/-- the switching graph of the proof structure of d, vertices numbered from o -/
def build (s : Nat → Bool) : {Γ : List F} → D Γ → Nat → R Γ
  | _, .ax _, o => ⟨[(o, o + 1)], (o, (o + 1, PUnit.unit)), o + 2⟩
  | _, @D.tensor Γ Δ a b d1 d2, o =>
    let r1 := build s d1 o
    let r2 := build s d2 r1.nx
    let p1 := split Γ (Δ := [a]) r1.vs
    let p2 := split Δ (Δ := [b]) r2.vs
    ⟨r1.G ++ (r2.G ++ [(p1.2.1, r2.nx), (p2.2.1, r2.nx)]),
     join (Γ ++ Δ) (Δ := [F.tensor a b]) (join Γ p1.1 p2.1) (r2.nx, PUnit.unit), r2.nx + 1⟩
  | _, @D.par Γ a b d, o =>
    let r := build s d o
    let p := split Γ (Δ := [a, b]) r.vs
    ⟨r.G ++ [(cond (s r.nx) p.2.1 p.2.2.1, r.nx)],
     join Γ (Δ := [F.par a b]) p.1 (r.nx, PUnit.unit), r.nx + 1⟩
  | _, @D.exch Γ Δ a b d, o =>
    let r := build s d o
    let q := split (Γ ++ [a, b]) (Δ := Δ) r.vs
    let p := split Γ (Δ := [a, b]) q.1
    ⟨r.G, join (Γ ++ [b, a]) (Δ := Δ) (join Γ (Δ := [b, a]) p.1 (p.2.2.1, (p.2.1, PUnit.unit))) q.2, r.nx⟩


/-- a switching graph is a tree: connected, vertices in [o, nx), one edge fewer than vertices -/
def Tree {Γ : List F} (o : Nat) (r : R Γ) : Prop :=
  o + (len r.G + 1) = r.nx ∧ AllIn o r.nx Γ r.vs ∧ ∀ v, InR o r.nx v → Conn r.G v o

theorem arith (o a : Nat) : (b : Nat) → o + (a + (b + 2) + 1) = o + (a + 1) + (b + 1) + 1
  | 0 => rfl
  | b + 1 => congrArg Nat.succ (arith o a b)

theorem le_of_cnt {o l n : Nat} (h : o + (l + 1) = n) : o ≤ n := h ▸ Nat.le_add_right o (l + 1)

/-- Danos–Regnier soundness: every switching graph of a derivation is a tree -/
theorem dr_sound (s : Nat → Bool) : {Γ : List F} → (d : D Γ) → (o : Nat) → Tree o (build s d o)
  | _, .ax _, o => by
    refine ⟨rfl, ⟨⟨Nat.le_refl o, Nat.lt_succ_of_le (Nat.le_succ o)⟩,
      ⟨Nat.le_succ o, Nat.lt_succ_self _⟩, trivial⟩, fun v hv => ?_⟩
    show Conn [(o, o + 1)] v o
    cases Nat.lt_or_ge v (o + 1) with
    | inl h =>
      have e : v = o := Nat.le_antisymm (Nat.le_of_lt_succ h) hv.1
      rw [e]; exact .refl _
    | inr h =>
      have e : v = o + 1 := Nat.le_antisymm (Nat.le_of_lt_succ hv.2) h
      rw [e]; exact conn_edge' (.head _)
  | _, @D.tensor Γ Δ a b d1 d2, o => by
    let r1 := build s d1 o
    let r2 := build s d2 r1.nx
    have t1 : Tree o r1 := dr_sound s d1 o
    have t2 : Tree r1.nx r2 := dr_sound s d2 r1.nx
    let p1 := split Γ (Δ := [a]) r1.vs
    let p2 := split Δ (Δ := [b]) r2.vs
    let k := r2.nx
    let G := r1.G ++ (r2.G ++ [(p1.2.1, k), (p2.2.1, k)])
    have h1 : o ≤ r1.nx := le_of_cnt t1.1
    have h2 : r1.nx ≤ k := le_of_cnt t2.1
    have hk : k ≤ k + 1 := Nat.le_succ k
    have s1 := allIn_split o r1.nx Γ r1.vs t1.2.1
    have s2 := allIn_split r1.nx k Δ r2.vs t2.2.1
    have c1 : ∀ v, Conn r1.G v o → Conn G v o := fun _ c => conn_mono (fun _ m => mem_l _ m) c
    have c2 : ∀ v w, Conn r2.G v w → Conn G v w := fun _ _ c =>
      conn_mono (fun _ m => mem_r r1.G (mem_l _ m)) c
    have ek : Conn G k o :=
      .step (Or.inr (mem_r r1.G (mem_r r2.G (.head _)))) (c1 _ (t1.2.2 _ s1.2.1))
    have e1 : Conn G r1.nx k :=
      conn_trans (c2 _ _ (conn_symm (t2.2.2 _ s2.2.1)))
        (conn_edge (mem_r r1.G (mem_r r2.G (.tail _ (.head _)))))
    refine ⟨?_, ?_, fun v hv => ?_⟩
    · show o + (len G + 1) = k + 1
      have e : len G = len r1.G + (len r2.G + 2) := by rw [len_app, len_app]; rfl
      rw [e, arith o (len r1.G) (len r2.G), t1.1, t2.1]
    · exact allIn_join o (k + 1) (Γ ++ Δ) _ _
        (allIn_join o (k + 1) Γ p1.1 p2.1 (allIn_widen (Nat.le_refl o) (Nat.le_trans h2 hk) Γ p1.1 s1.1)
          (allIn_widen h1 hk Δ p2.1 s2.1))
        ⟨⟨Nat.le_trans h1 h2, Nat.lt_succ_self k⟩, trivial⟩
    · show Conn G v o
      cases Nat.lt_or_ge v r1.nx with
      | inl h => exact c1 _ (t1.2.2 v ⟨hv.1, h⟩)
      | inr h =>
        cases Nat.lt_or_ge v k with
        | inl h' => exact conn_trans (c2 _ _ (t2.2.2 v ⟨h, h'⟩)) (conn_trans e1 ek)
        | inr h' =>
          have e : v = k := Nat.le_antisymm (Nat.le_of_lt_succ hv.2) h'
          rw [e]; exact ek
  | _, @D.par Γ a b d, o => by
    let r := build s d o
    have t : Tree o r := dr_sound s d o
    let p := split Γ (Δ := [a, b]) r.vs
    let k := r.nx
    let c := cond (s k) p.2.1 p.2.2.1
    let G := r.G ++ [(c, k)]
    have h1 : o ≤ k := le_of_cnt t.1
    have sp := allIn_split o k Γ r.vs t.2.1
    have hc : InR o k c := by
      show InR o k (cond (s k) p.2.1 p.2.2.1)
      cases s k with
      | true => exact sp.2.1
      | false => exact sp.2.2.1
    have ek : Conn G k o :=
      .step (Or.inr (mem_r r.G (.head _))) (conn_mono (fun _ m => mem_l _ m) (t.2.2 c hc))
    refine ⟨?_, ?_, fun v hv => ?_⟩
    · show o + (len G + 1) = k + 1
      have e : len G = len r.G + 1 := len_app _ _
      rw [e]
      exact congrArg Nat.succ t.1
    · exact allIn_join o (k + 1) Γ p.1 _ (allIn_widen (Nat.le_refl o) (Nat.le_succ k) Γ p.1 sp.1)
        ⟨⟨h1, Nat.lt_succ_self k⟩, trivial⟩
    · show Conn G v o
      cases Nat.lt_or_ge v k with
      | inl h => exact conn_mono (fun _ m => mem_l _ m) (t.2.2 v ⟨hv.1, h⟩)
      | inr h =>
        have e : v = k := Nat.le_antisymm (Nat.le_of_lt_succ hv.2) h
        rw [e]; exact ek
  | _, @D.exch Γ Δ a b d, o => by
    let r := build s d o
    have t : Tree o r := dr_sound s d o
    let q := split (Γ ++ [a, b]) (Δ := Δ) r.vs
    let p := split Γ (Δ := [a, b]) q.1
    have sq := allIn_split o r.nx (Γ ++ [a, b]) r.vs t.2.1
    have sp := allIn_split o r.nx Γ q.1 sq.1
    exact ⟨t.1, allIn_join o r.nx (Γ ++ [b, a]) _ q.2
      (allIn_join o r.nx Γ p.1 _ sp.1 ⟨sp.2.2.1, sp.2.1, trivial⟩) sq.2, t.2.2⟩

/-- every switching graph of every derivation is a tree -/
theorem danos_regnier_sound {Γ : List F} (d : D Γ) (s : Nat → Bool) :
    (∀ v, InR 0 (build s d 0).nx v → Conn (build s d 0).G v 0) ∧ len (build s d 0).G + 1 = (build s d 0).nx :=
  ⟨(dr_sound s d 0).2.2, (Nat.zero_add _).symm.trans (dr_sound s d 0).1⟩

end DR

end Found

#print axioms Found.DR.mem_l
#print axioms Found.DR.mem_r
#print axioms Found.DR.len_app
#print axioms Found.DR.conn_trans
#print axioms Found.DR.conn_symm
#print axioms Found.DR.conn_mono
#print axioms Found.DR.allIn_join
#print axioms Found.DR.allIn_split
#print axioms Found.DR.allIn_widen
#print axioms Found.DR.arith
#print axioms Found.DR.dr_sound
#print axioms Found.DR.danos_regnier_sound

found/DaCosta.lean

lean_all/found/DaCosta.lean

/-
  T139. found/DaCosta — da Costa's Cω as a point: negation is not
  truth-functional; semantics by bivaluations v : formulas → Bool with
    classical ∧, ∨, →;   v a = 0 ⇒ v(¬a) = 1;   v(¬¬a) = 1 ⇒ v a = 1.
  * Excluded middle is valid (every bivaluation), explosion is not:
    an explicit bivaluation with p, ¬p true and q false.
  * Line to LP (T70) / classical (T66): restricting bivaluations to
    truth-functional ¬ gives classical logic.

  Lean 4 core only, hand-written proofs, no axioms.
-/

namespace Found

namespace DaCosta

inductive DF where
  | var : Nat → DF
  | neg : DF → DF
  | and : DF → DF → DF
  | or : DF → DF → DF
  | imp : DF → DF → DF

structure Biv where
  v : DF → Bool
  and_ : ∀ a b, v (.and a b) = (v a && v b)
  or_ : ∀ a b, v (.or a b) = (v a || v b)
  imp_ : ∀ a b, v (.imp a b) = (!v a || v b)
  neg0 : ∀ a, v a = false → v (.neg a) = true
  negneg : ∀ a, v (.neg (.neg a)) = true → v a = true

/-- excluded middle is valid in Cω -/
theorem lem (B : Biv) (a : DF) : B.v (.or a (.neg a)) = true := by
  rw [B.or_]
  cases h : B.v a with
  | true => rfl
  | false => rw [B.neg0 a h]; rfl

def isP0 : DF → Bool
  | .var 0 => true
  | .var (_ + 1) => false
  | .neg _ => false
  | .and _ _ => false
  | .or _ _ => false
  | .imp _ _ => false

def v0 : DF → Bool
  | .var 0 => true
  | .var (_ + 1) => false
  | .neg a => isP0 a || !(v0 a)
  | .and a b => v0 a && v0 b
  | .or a b => v0 a || v0 b
  | .imp a b => !(v0 a) || v0 b

theorem isP0_v0 : (a : DF) → isP0 a = true → v0 a = true
  | .var 0, _ => rfl
  | .var (_ + 1), h => Bool.noConfusion h
  | .neg _, h => Bool.noConfusion h
  | .and _ _, h => Bool.noConfusion h
  | .or _ _, h => Bool.noConfusion h
  | .imp _ _, h => Bool.noConfusion h

def B0 : Biv where
  v := v0
  and_ _ _ := rfl
  or_ _ _ := rfl
  imp_ _ _ := rfl
  neg0 a h := by
    show (isP0 a || !(v0 a)) = true
    rw [h]
    cases isP0 a <;> rfl
  negneg a h := by
    have h' : (isP0 (.neg a) || !(isP0 a || !(v0 a))) = true := h
    cases hp : isP0 a with
    | true => exact isP0_v0 a hp
    | false =>
      rw [hp] at h'
      cases hv : v0 a with
      | true => rfl
      | false => rw [hv] at h'; exact Bool.noConfusion h'

/-- explosion fails: p, ¬p true and q false under B0 -/
theorem no_explosion :
    B0.v (.var 0) = true ∧ B0.v (.neg (.var 0)) = true ∧ B0.v (.var 1) = false :=
  ⟨rfl, rfl, rfl⟩

end DaCosta

end Found

#print axioms Found.DaCosta.lem
#print axioms Found.DaCosta.isP0_v0
#print axioms Found.DaCosta.B0
#print axioms Found.DaCosta.no_explosion

found/DepE1.lean

lean_all/found/DepE1.lean

/-
  Line dependence logic ↔ ESO (Väänänen), part 1. found/DepE1 — both
  logics over a relational vocabulary (I r : List D → Prop), with variables
  as de Bruijn levels, so that a team of a formula with k free variables is
  a relation on k-tuples:
  * dependence logic in negation normal form: R ī, ¬R ī, xᵢ = xⱼ, xᵢ ≠ xⱼ,
    =(ī, j), ∧, ∨ (team split), ∃ (a supplementing function), ∀ (duplication);
  * existential second-order logic: a first-order matrix with relation
    variables Xᵢ, true when some interpretation J of the variables makes it
    true (∃ J, sat J [] θ);
  * congruence of both semantics under pointwise equivalent teams and
    relation interpretations.

  Lean 4 core only, hand-written proofs, no axioms.
-/

namespace Found

namespace DE

variable {D : Type}

/-! ## tuples -/

def get (d0 : D) : List D → Nat → D
  | [], _ => d0
  | a :: _, 0 => a
  | _ :: u, i + 1 => get d0 u i

def args (d0 : D) (u : List D) : List Nat → List D
  | [] => []
  | i :: is => get d0 u i :: args d0 u is

def range' (b : Nat) : Nat → List Nat
  | 0 => []
  | k + 1 => b :: range' (b + 1) k

def len : List D → Nat
  | [] => 0
  | _ :: u => len u + 1

/-! ## dependence logic -/

inductive DF where
  | rel : Nat → List Nat → DF
  | nrel : Nat → List Nat → DF
  | eq : Nat → Nat → DF
  | neq : Nat → Nat → DF
  | dep : List Nat → Nat → DF
  | and : DF → DF → DF
  | or : DF → DF → DF
  | ex : DF → DF
  | all : DF → DF

abbrev Team (D : Type) := List D → Prop

def TS (d0 : D) (I : Nat → List D → Prop) : Team D → DF → Prop
  | T, .rel r is => ∀ u, T u → I r (args d0 u is)
  | T, .nrel r is => ∀ u, T u → ¬ I r (args d0 u is)
  | T, .eq i j => ∀ u, T u → get d0 u i = get d0 u j
  | T, .neq i j => ∀ u, T u → get d0 u i ≠ get d0 u j
  | T, .dep is j => ∀ u v, T u → T v → args d0 u is = args d0 v is → get d0 u j = get d0 v j
  | T, .and φ ψ => TS d0 I T φ ∧ TS d0 I T ψ
  | T, .or φ ψ => ∃ T1 T2 : Team D, (∀ u, T u → T1 u ∨ T2 u) ∧ (∀ u, T1 u → T u) ∧ (∀ u, T2 u → T u) ∧
      TS d0 I T1 φ ∧ TS d0 I T2 ψ
  | T, .ex φ => ∃ F : List D → D, TS d0 I (fun u' => ∃ u, T u ∧ u' = u ++ [F u]) φ
  | T, .all φ => TS d0 I (fun u' => ∃ u a, T u ∧ u' = u ++ [a]) φ

/-- truth of a sentence: the team of the empty tuple -/
def DTrue (d0 : D) (I : Nat → List D → Prop) (φ : DF) : Prop := TS d0 I (fun u => u = []) φ

theorem ts_congr (d0 : D) (I : Nat → List D → Prop) : (φ : DF) → (T T' : Team D) → (∀ u, T u ↔ T' u) →
    (TS d0 I T φ ↔ TS d0 I T' φ)
  | .rel _ _, _, _, h => ⟨fun s u hu => s u ((h u).2 hu), fun s u hu => s u ((h u).1 hu)⟩
  | .nrel _ _, _, _, h => ⟨fun s u hu => s u ((h u).2 hu), fun s u hu => s u ((h u).1 hu)⟩
  | .eq _ _, _, _, h => ⟨fun s u hu => s u ((h u).2 hu), fun s u hu => s u ((h u).1 hu)⟩
  | .neq _ _, _, _, h => ⟨fun s u hu => s u ((h u).2 hu), fun s u hu => s u ((h u).1 hu)⟩
  | .dep _ _, _, _, h => ⟨fun s u v hu hv => s u v ((h u).2 hu) ((h v).2 hv),
      fun s u v hu hv => s u v ((h u).1 hu) ((h v).1 hv)⟩
  | .and φ ψ, T, T', h => ⟨fun ⟨a, b⟩ => ⟨(ts_congr d0 I φ T T' h).1 a, (ts_congr d0 I ψ T T' h).1 b⟩,
      fun ⟨a, b⟩ => ⟨(ts_congr d0 I φ T T' h).2 a, (ts_congr d0 I ψ T T' h).2 b⟩⟩
  | .or _ _, _, _, h => ⟨fun ⟨T1, T2, c, s1, s2, a, b⟩ => ⟨T1, T2, fun u hu => c u ((h u).2 hu),
        fun u hu => (h u).1 (s1 u hu), fun u hu => (h u).1 (s2 u hu), a, b⟩,
      fun ⟨T1, T2, c, s1, s2, a, b⟩ => ⟨T1, T2, fun u hu => c u ((h u).1 hu),
        fun u hu => (h u).2 (s1 u hu), fun u hu => (h u).2 (s2 u hu), a, b⟩⟩
  | .ex φ, _, _, h => ⟨fun ⟨F, a⟩ => ⟨F, (ts_congr d0 I φ _ _ (fun _ => ⟨fun ⟨u, hu, e⟩ => ⟨u, (h u).2 hu, e⟩,
        fun ⟨u, hu, e⟩ => ⟨u, (h u).1 hu, e⟩⟩)).2 a⟩,
      fun ⟨F, a⟩ => ⟨F, (ts_congr d0 I φ _ _ (fun _ => ⟨fun ⟨u, hu, e⟩ => ⟨u, (h u).2 hu, e⟩,
        fun ⟨u, hu, e⟩ => ⟨u, (h u).1 hu, e⟩⟩)).1 a⟩⟩
  | .all φ, _, _, h => (ts_congr d0 I φ _ _ (fun _ => ⟨fun ⟨u, a, hu, e⟩ => ⟨u, a, (h u).1 hu, e⟩,
        fun ⟨u, a, hu, e⟩ => ⟨u, a, (h u).2 hu, e⟩⟩))

/-! ## existential second-order logic -/

inductive EF where
  | rel : Nat → List Nat → EF
  | svar : Nat → List Nat → EF
  | eq : Nat → Nat → EF
  | neg : EF → EF
  | and : EF → EF → EF
  | or : EF → EF → EF
  | ex : EF → EF
  | all : EF → EF

abbrev SVal (D : Type) := Nat → List D → Prop

def sat (d0 : D) (I : Nat → List D → Prop) (J : SVal D) : List D → EF → Prop
  | e, .rel r is => I r (args d0 e is)
  | e, .svar i is => J i (args d0 e is)
  | e, .eq i j => get d0 e i = get d0 e j
  | e, .neg φ => ¬ sat d0 I J e φ
  | e, .and φ ψ => sat d0 I J e φ ∧ sat d0 I J e ψ
  | e, .or φ ψ => sat d0 I J e φ ∨ sat d0 I J e ψ
  | e, .ex φ => ∃ a, sat d0 I J (e ++ [a]) φ
  | e, .all φ => ∀ a, sat d0 I J (e ++ [a]) φ

/-- truth of an ESO sentence: some interpretation of the relation variables -/
def ETrue (d0 : D) (I : Nat → List D → Prop) (θ : EF) : Prop := ∃ J, sat d0 I J [] θ

/-- the relation variables of θ are below m -/
def SB : EF → Nat → Prop
  | .rel _ _, _ => True
  | .svar i _, m => i < m
  | .eq _ _, _ => True
  | .neg φ, m => SB φ m
  | .and φ ψ, m => SB φ m ∧ SB ψ m
  | .or φ ψ, m => SB φ m ∧ SB ψ m
  | .ex φ, m => SB φ m
  | .all φ, m => SB φ m

theorem sat_congrJ (d0 : D) (I : Nat → List D → Prop) : (θ : EF) → (m : Nat) → SB θ m → (J J' : SVal D) →
    (∀ i, i < m → ∀ u, J i u ↔ J' i u) → (e : List D) → (sat d0 I J e θ ↔ sat d0 I J' e θ)
  | .rel _ _, _, _, _, _, _, _ => Iff.rfl
  | .svar i _, _, hb, _, _, h, _ => h i hb _
  | .eq _ _, _, _, _, _, _, _ => Iff.rfl
  | .neg φ, m, hb, J, J', h, e => ⟨fun s t => s ((sat_congrJ d0 I φ m hb J J' h e).2 t),
      fun s t => s ((sat_congrJ d0 I φ m hb J J' h e).1 t)⟩
  | .and φ ψ, m, ⟨h1, h2⟩, J, J', h, e => ⟨fun ⟨a, b⟩ => ⟨(sat_congrJ d0 I φ m h1 J J' h e).1 a, (sat_congrJ d0 I ψ m h2 J J' h e).1 b⟩,
      fun ⟨a, b⟩ => ⟨(sat_congrJ d0 I φ m h1 J J' h e).2 a, (sat_congrJ d0 I ψ m h2 J J' h e).2 b⟩⟩
  | .or φ ψ, m, ⟨h1, h2⟩, J, J', h, e => ⟨fun s => s.elim (fun a => Or.inl ((sat_congrJ d0 I φ m h1 J J' h e).1 a))
        (fun b => Or.inr ((sat_congrJ d0 I ψ m h2 J J' h e).1 b)),
      fun s => s.elim (fun a => Or.inl ((sat_congrJ d0 I φ m h1 J J' h e).2 a))
        (fun b => Or.inr ((sat_congrJ d0 I ψ m h2 J J' h e).2 b))⟩
  | .ex φ, m, hb, J, J', h, _ => ⟨fun ⟨a, s⟩ => ⟨a, (sat_congrJ d0 I φ m hb J J' h _).1 s⟩,
      fun ⟨a, s⟩ => ⟨a, (sat_congrJ d0 I φ m hb J J' h _).2 s⟩⟩
  | .all φ, m, hb, J, J', h, _ => ⟨fun s a => (sat_congrJ d0 I φ m hb J J' h _).1 (s a),
      fun s a => (sat_congrJ d0 I φ m hb J J' h _).2 (s a)⟩

theorem sb_mono : (θ : EF) → {m m' : Nat} → m ≤ m' → SB θ m → SB θ m'
  | .rel _ _, _, _, _, _ => trivial
  | .svar _ _, _, _, hm, hb => Nat.lt_of_lt_of_le hb hm
  | .eq _ _, _, _, _, _ => trivial
  | .neg φ, _, _, hm, hb => sb_mono φ hm hb
  | .and φ ψ, _, _, hm, ⟨a, b⟩ => ⟨sb_mono φ hm a, sb_mono ψ hm b⟩
  | .or φ ψ, _, _, hm, ⟨a, b⟩ => ⟨sb_mono φ hm a, sb_mono ψ hm b⟩
  | .ex φ, _, _, hm, hb => sb_mono φ hm hb
  | .all φ, _, _, hm, hb => sb_mono φ hm hb

end DE

end Found

#print axioms Found.DE.ts_congr
#print axioms Found.DE.sat_congrJ
#print axioms Found.DE.sb_mono

found/DepE2.lean

lean_all/found/DepE2.lean

/-
  Line dependence logic ↔ ESO, part 2. found/DepE2 — dependence logic →
  ESO (Väänänen), in the classical point: a formula with k free variables
  whose team is the relation variable X_t (restricted to k-tuples) is
  translated into a first-order formula with fresh relation variables (one
  per ∨-branch, ∃ and ∀); the team satisfies the formula iff the translation
  is true for some values of the fresh variables (tau_ok; downward closure
  and a choice of supplementing functions); for sentences,
  DTrue φ ↔ ETrue (toESO φ) (d_to_eso).

  Lean 4 core only, hand-written proofs, no axioms (the classical point K
  is a parameter: excluded middle for the matrix, choice for supplementing
  functions).
-/
import found.DepE1
import found.ClassicalPoint

namespace Found

namespace DE

open CP

variable {D : Type} (d0 : D) (I : Nat → List D → Prop)

/-! ## lists -/

theorem app_assoc'' : (u v w : List D) → (u ++ v) ++ w = u ++ (v ++ w)
  | [], _, _ => rfl
  | a :: u, v, w => congrArg (a :: ·) (app_assoc'' u v w)

theorem app_nil' : (u : List D) → u ++ [] = u
  | [] => rfl
  | a :: u => congrArg (a :: ·) (app_nil' u)

theorem len_app : (u v : List D) → len (u ++ v) = len u + len v
  | [], v => (Nat.zero_add _).symm
  | _ :: u, v => by show len (u ++ v) + 1 = len u + 1 + len v; rw [len_app u v, Nat.add_right_comm]

theorem get_app_l : (u v : List D) → (i : Nat) → i < len u → get d0 (u ++ v) i = get d0 u i
  | [], _, _, h => absurd h (Nat.not_lt_zero _)
  | _ :: _, _, 0, _ => rfl
  | _ :: u, v, i + 1, h => get_app_l u v i (Nat.lt_of_succ_lt_succ h)

theorem get_app_r : (u v : List D) → (i : Nat) → get d0 (u ++ v) (len u + i) = get d0 v i
  | [], v, i => by show get d0 v (0 + i) = get d0 v i; rw [Nat.zero_add]
  | a :: u, v, i => by
    show get d0 (a :: (u ++ v)) (len u + 1 + i) = get d0 v i
    rw [Nat.add_right_comm]
    exact get_app_r u v i

/-- reading a segment by its positions -/
theorem args_seg : (seg pre post : List D) →
    args d0 (pre ++ seg ++ post) (range' (len pre) (len seg)) = seg
  | [], _, _ => rfl
  | a :: seg, pre, post => by
    show get d0 (pre ++ (a :: seg) ++ post) (len pre) :: args d0 (pre ++ (a :: seg) ++ post) (range' (len pre + 1) (len seg)) = a :: seg
    have e1 : get d0 (pre ++ (a :: seg) ++ post) (len pre) = a := by
      rw [app_assoc'']
      exact get_app_r d0 pre _ 0
    have e2 : pre ++ (a :: seg) ++ post = (pre ++ [a]) ++ seg ++ post := by
      rw [app_assoc'' pre [a] seg]; rfl
    have e3 : len (pre ++ [a]) = len pre + 1 := len_app pre [a]
    rw [e1, e2, ← e3, args_seg seg (pre ++ [a]) post]

theorem args_all (u : List D) : args d0 u (range' 0 (len u)) = u := by
  have := args_seg d0 u [] []
  rw [app_nil'] at this
  exact this

theorem args_init (u : List D) (x : D) : args d0 (u ++ [x]) (range' 0 (len u)) = u :=
  args_seg d0 u [] [x]

theorem args_second (u v : List D) : args d0 (u ++ v) (range' (len u) (len v)) = v := by
  have := args_seg d0 v u []
  rw [app_nil'] at this
  exact this

theorem snoc_dec : (u' : List D) → (k : Nat) → len u' = k + 1 → ∃ u x, u' = u ++ [x] ∧ len u = k
  | [], _, h => Nat.noConfusion h
  | [x], k, h => ⟨[], x, rfl, Nat.succ.inj h⟩
  | a :: b :: w, k, h => by
    have h' : len (b :: w) + 1 = k + 1 := h
    match k, h' with
    | 0, h' => exact Nat.noConfusion (Nat.succ.inj h')
    | k + 1, h' =>
      match snoc_dec (b :: w) k (Nat.succ.inj h') with
      | ⟨u, x, e, hl⟩ => exact ⟨a :: u, x, by rw [e]; rfl, congrArg (· + 1) hl⟩

theorem app_dec : (e : List D) → (k : Nat) → k ≤ len e → ∃ u v, e = u ++ v ∧ len u = k
  | e, 0, _ => ⟨[], e, rfl, rfl⟩
  | [], _ + 1, h => absurd h (Nat.not_succ_le_zero _)
  | a :: e, k + 1, h => match app_dec e k (Nat.le_of_succ_le_succ h) with
    | ⟨u, v, he, hl⟩ => ⟨a :: u, v, by rw [he]; rfl, congrArg (· + 1) hl⟩

/-! ## the translation -/

def tv (t k : Nat) : EF := .svar t (range' 0 k)
def imp' (a b : EF) : EF := .or (.neg a) b

def allK : Nat → EF → EF
  | 0, φ => φ
  | k + 1, φ => allK k (.all φ)

def eqs (k : Nat) : List Nat → EF
  | [] => .eq 0 0
  | i :: is => .and (.eq i (k + i)) (eqs k is)

def τ : DF → Nat → Nat → Nat → EF × Nat
  | .rel r is, k, t, n => (allK k (imp' (tv t k) (.rel r is)), n)
  | .nrel r is, k, t, n => (allK k (imp' (tv t k) (.neg (.rel r is))), n)
  | .eq i j, k, t, n => (allK k (imp' (tv t k) (.eq i j)), n)
  | .neq i j, k, t, n => (allK k (imp' (tv t k) (.neg (.eq i j))), n)
  | .dep is j, k, t, n =>
      (allK (k + k) (imp' (.and (tv t k) (.and (.svar t (range' k k)) (eqs k is))) (.eq j (k + j))), n)
  | .and φ ψ, k, t, n => (.and (τ φ k t n).1 (τ ψ k t (τ φ k t n).2).1, (τ ψ k t (τ φ k t n).2).2)
  | .or φ ψ, k, t, n =>
      (.and (allK k (imp' (tv t k) (.or (tv n k) (tv (n + 1) k))))
        (.and (allK k (imp' (tv n k) (tv t k)))
          (.and (allK k (imp' (tv (n + 1) k) (tv t k)))
            (.and (τ φ k n (n + 2)).1 (τ ψ k (n + 1) (τ φ k n (n + 2)).2).1))),
       (τ ψ k (n + 1) (τ φ k n (n + 2)).2).2)
  | .ex φ, k, t, n =>
      (.and (allK k (imp' (tv t k) (.ex (tv n (k + 1)))))
        (.and (allK (k + 1) (imp' (tv n (k + 1)) (tv t k))) (τ φ (k + 1) n (n + 1)).1),
       (τ φ (k + 1) n (n + 1)).2)
  | .all φ, k, t, n =>
      (.and (allK (k + 1) (.and (imp' (tv n (k + 1)) (tv t k)) (imp' (tv t k) (tv n (k + 1)))))
        (τ φ (k + 1) n (n + 1)).1,
       (τ φ (k + 1) n (n + 1)).2)

/-- the sentence: the team of the empty tuple is X₀ -/
def toESO (φ : DF) : EF := .and (.svar 0 []) (τ φ 0 0 1).1

/-! ## bookkeeping: fresh variables -/

theorem sb_allK (m : Nat) : (k : Nat) → (φ : EF) → SB φ m → SB (allK k φ) m
  | 0, _, h => h
  | k + 1, φ, h => sb_allK m k (.all φ) h

theorem tau_mono : (φ : DF) → (k t n : Nat) → n ≤ (τ φ k t n).2
  | .rel _ _, _, _, _ => Nat.le_refl _
  | .nrel _ _, _, _, _ => Nat.le_refl _
  | .eq _ _, _, _, _ => Nat.le_refl _
  | .neq _ _, _, _, _ => Nat.le_refl _
  | .dep _ _, _, _, _ => Nat.le_refl _
  | .and φ ψ, k, t, n => Nat.le_trans (tau_mono φ k t n) (tau_mono ψ k t _)
  | .or φ ψ, k, _, n => Nat.le_trans (Nat.le_add_right n 2) (Nat.le_trans (tau_mono φ k n (n + 2)) (tau_mono ψ k (n + 1) _))
  | .ex φ, k, _, n => Nat.le_trans (Nat.le_succ n) (tau_mono φ (k + 1) n (n + 1))
  | .all φ, k, _, n => Nat.le_trans (Nat.le_succ n) (tau_mono φ (k + 1) n (n + 1))

theorem sb_eqs (m k : Nat) : (is : List Nat) → SB (eqs k is) m
  | [] => trivial
  | _ :: is => ⟨trivial, sb_eqs m k is⟩

theorem tau_sb : (φ : DF) → (k t n : Nat) → t < n → SB (τ φ k t n).1 (τ φ k t n).2
  | .rel _ _, k, _, n, ht => sb_allK n k _ ⟨ht, trivial⟩
  | .nrel _ _, k, _, n, ht => sb_allK n k _ ⟨ht, trivial⟩
  | .eq _ _, k, _, n, ht => sb_allK n k _ ⟨ht, trivial⟩
  | .neq _ _, k, _, n, ht => sb_allK n k _ ⟨ht, trivial⟩
  | .dep is _, k, _, n, ht => sb_allK n (k + k) _ ⟨⟨ht, ht, sb_eqs n k is⟩, trivial⟩
  | .and φ ψ, k, t, n, ht =>
    ⟨sb_mono _ (tau_mono ψ k t _) (tau_sb φ k t n ht),
     tau_sb ψ k t _ (Nat.lt_of_lt_of_le ht (tau_mono φ k t n))⟩
  | .or φ ψ, k, t, n, ht =>
    have h1 := tau_mono φ k n (n + 2)
    have h2 := tau_mono ψ k (n + 1) (τ φ k n (n + 2)).2
    have l0 : n < (τ ψ k (n + 1) (τ φ k n (n + 2)).2).2 := Nat.lt_of_lt_of_le (Nat.lt_succ_of_le (Nat.le_succ n)) (Nat.le_trans h1 h2)
    have l1 : n + 1 < (τ ψ k (n + 1) (τ φ k n (n + 2)).2).2 := Nat.lt_of_lt_of_le (Nat.lt_succ_self _) (Nat.le_trans h1 h2)
    have lt : t < (τ ψ k (n + 1) (τ φ k n (n + 2)).2).2 := Nat.lt_trans ht l0
    ⟨sb_allK _ k _ ⟨lt, l0, l1⟩, sb_allK _ k _ ⟨l0, lt⟩, sb_allK _ k _ ⟨l1, lt⟩,
     sb_mono _ h2 (tau_sb φ k n (n + 2) (Nat.lt_succ_of_le (Nat.le_succ n))),
     tau_sb ψ k (n + 1) _ (Nat.lt_of_lt_of_le (Nat.lt_succ_self _) h1)⟩
  | .ex φ, k, _, n, ht =>
    have h1 := tau_mono φ (k + 1) n (n + 1)
    have l0 : n < (τ φ (k + 1) n (n + 1)).2 := Nat.lt_of_lt_of_le (Nat.lt_succ_self n) h1
    ⟨sb_allK _ k _ ⟨Nat.lt_trans ht l0, l0⟩, sb_allK _ (k + 1) _ ⟨l0, Nat.lt_trans ht l0⟩,
     tau_sb φ (k + 1) n (n + 1) (Nat.lt_succ_self n)⟩
  | .all φ, k, _, n, ht =>
    have h1 := tau_mono φ (k + 1) n (n + 1)
    have l0 : n < (τ φ (k + 1) n (n + 1)).2 := Nat.lt_of_lt_of_le (Nat.lt_succ_self n) h1
    ⟨sb_allK _ (k + 1) _ ⟨⟨l0, Nat.lt_trans ht l0⟩, ⟨Nat.lt_trans ht l0, l0⟩⟩,
     tau_sb φ (k + 1) n (n + 1) (Nat.lt_succ_self n)⟩

/-! ## semantics of the pieces -/

theorem sat_allK (J : SVal D) (φ : EF) : (k : Nat) → (e : List D) →
    (sat d0 I J e (allK k φ) ↔ ∀ u, len u = k → sat d0 I J (e ++ u) φ)
  | 0, e => ⟨fun h u hu => by
      cases u with
      | nil => rw [app_nil']; exact h
      | cons _ _ => exact Nat.noConfusion hu, fun h => by have := h [] rfl; rw [app_nil'] at this; exact this⟩
  | k + 1, e => by
    show sat d0 I J e (allK k (.all φ)) ↔ _
    refine (sat_allK J (.all φ) k e).trans ⟨fun h u' hu' => ?_, fun h u hu a => ?_⟩
    · match snoc_dec u' k hu' with
      | ⟨u, x, eu, hl⟩ =>
        have := h u hl x
        rw [eu, ← app_assoc'']
        exact this
    · have := h (u ++ [a]) (by rw [len_app, hu]; rfl)
      rw [← app_assoc''] at this
      exact this

theorem sat_imp' (K : Classical') (J : SVal D) (e : List D) (a b : EF) :
    sat d0 I J e (imp' a b) ↔ (sat d0 I J e a → sat d0 I J e b) :=
  ⟨fun h ha => h.elim (fun na => absurd ha na) id,
   fun h => (K.lem (sat d0 I J e a)).elim (fun ha => Or.inr (h ha)) Or.inl⟩

/-- a guarded clause: for every tuple of the team -/
theorem guard (K : Classical') (J : SVal D) (t k : Nat) (P : EF) :
    sat d0 I J [] (allK k (imp' (tv t k) P)) ↔ ∀ u, len u = k → J t u → sat d0 I J u P :=
  (sat_allK d0 I J _ k []).trans ⟨fun h u hu ht => (sat_imp' d0 I K J u _ _).1 (h u hu)
      (show J t (args d0 u (range' 0 k)) from by rw [← hu, args_all]; exact ht),
    fun h u hu => (sat_imp' d0 I K J u _ _).2 (fun ht => h u hu (by
      have e : args d0 u (range' 0 k) = u := by rw [← hu, args_all]
      show J t u
      have ht' : J t (args d0 u (range' 0 k)) := ht
      rw [e] at ht'
      exact ht'))⟩

def Jk (J : SVal D) (t k : Nat) : Team D := fun u => J t u ∧ len u = k

/-- well-formed dependence atoms: their variables are below k -/
def AllLt : List Nat → Nat → Prop
  | [], _ => True
  | i :: is, k => i < k ∧ AllLt is k

def WFd : DF → Nat → Prop
  | .dep is j, k => AllLt is k ∧ j < k
  | .and φ ψ, k => WFd φ k ∧ WFd ψ k
  | .or φ ψ, k => WFd φ k ∧ WFd ψ k
  | .ex φ, k => WFd φ (k + 1)
  | .all φ, k => WFd φ (k + 1)
  | .rel _ _, _ => True
  | .nrel _ _, _ => True
  | .eq _ _, _ => True
  | .neq _ _, _ => True

theorem sat_eqs (J : SVal D) (u v : List D) (k : Nat) (hu : len u = k) :
    (is : List Nat) → AllLt is k → (sat d0 I J (u ++ v) (eqs k is) ↔ args d0 u is = args d0 v is)
  | [], _ => ⟨fun _ => rfl, fun _ => rfl⟩
  | i :: is, ⟨hi, hs⟩ => by
    show (get d0 (u ++ v) i = get d0 (u ++ v) (k + i) ∧ sat d0 I J (u ++ v) (eqs k is)) ↔
      get d0 u i :: args d0 u is = get d0 v i :: args d0 v is
    rw [get_app_l d0 u v i (hu ▸ hi), ← hu, get_app_r]
    rw [hu]
    refine ⟨fun ⟨a, b⟩ => by rw [a, (sat_eqs J u v k hu is hs).1 b], fun h => ⟨?_, (sat_eqs J u v k hu is hs).2 ?_⟩⟩
    · exact (List.cons.inj h).1
    · exact (List.cons.inj h).2

/-- downward closure of dependence logic -/
theorem ts_down : (φ : DF) → (T T' : Team D) → (∀ u, T' u → T u) → TS d0 I T φ → TS d0 I T' φ
  | .rel _ _, _, _, h, s => fun u hu => s u (h u hu)
  | .nrel _ _, _, _, h, s => fun u hu => s u (h u hu)
  | .eq _ _, _, _, h, s => fun u hu => s u (h u hu)
  | .neq _ _, _, _, h, s => fun u hu => s u (h u hu)
  | .dep _ _, _, _, h, s => fun u v hu hv => s u v (h u hu) (h v hv)
  | .and φ ψ, T, T', h, ⟨a, b⟩ => ⟨ts_down φ T T' h a, ts_down ψ T T' h b⟩
  | .or φ ψ, _, T', h, ⟨T1, T2, c, _, _, a, b⟩ =>
    ⟨fun u => T1 u ∧ T' u, fun u => T2 u ∧ T' u,
     fun u hu => (c u (h u hu)).elim (fun h1 => Or.inl ⟨h1, hu⟩) (fun h2 => Or.inr ⟨h2, hu⟩),
     fun _ hu => hu.2, fun _ hu => hu.2,
     ts_down φ T1 _ (fun _ hu => hu.1) a, ts_down ψ T2 _ (fun _ hu => hu.1) b⟩
  | .ex φ, _, _, h, ⟨F, a⟩ => ⟨F, ts_down φ _ _ (fun _ ⟨u, hu, e⟩ => ⟨u, h u hu, e⟩) a⟩
  | .all φ, _, _, h, a => ts_down φ _ _ (fun _ ⟨u, x, hu, e⟩ => ⟨u, x, h u hu, e⟩) a

/-! ## updating one relation variable -/

def updJ (J : SVal D) (i : Nat) (R : List D → Prop) : SVal D := fun j => match Nat.decEq j i with
  | isTrue _ => R
  | isFalse _ => J j

theorem updJ_eq (J : SVal D) (i : Nat) (R : List D → Prop) (u : List D) : updJ J i R i u ↔ R u := by
  unfold updJ
  match Nat.decEq i i with
  | isTrue _ => exact Iff.rfl
  | isFalse h => exact absurd rfl h

theorem updJ_ne (J : SVal D) {i j : Nat} (R : List D → Prop) (h : j ≠ i) (u : List D) : updJ J i R j u ↔ J j u := by
  unfold updJ
  match Nat.decEq j i with
  | isTrue e => exact absurd e h
  | isFalse _ => exact Iff.rfl

theorem snoc_inj : (u v : List D) → (x y : D) → u ++ [x] = v ++ [y] → u = v ∧ x = y
  | [], [], _, _, h => ⟨rfl, List.cons.inj h |>.1⟩
  | [], _ :: v, _, _, h => by
    have := (List.cons.inj h).2
    cases v with
    | nil => exact nomatch this
    | cons _ _ => exact nomatch this
  | _ :: u, [], _, _, h => by
    have := (List.cons.inj h).2
    cases u with
    | nil => exact nomatch this
    | cons _ _ => exact nomatch this
  | a :: u, b :: v, x, y, h =>
    have h' := List.cons.inj h
    match snoc_inj u v x y h'.2 with
    | ⟨e1, e2⟩ => ⟨by rw [h'.1, e1], e2⟩

theorem len_snoc (u : List D) (x : D) : len (u ++ [x]) = len u + 1 := len_app u [x]

theorem addc : (k : Nat) → {a b : Nat} → k + a = k + b → a = b
  | 0, a, b, h => by rw [Nat.zero_add, Nat.zero_add] at h; exact h
  | k + 1, a, b, h => addc k (Nat.succ.inj (by rw [Nat.succ_add, Nat.succ_add] at h; exact h))

/-- an atom guarded by the team -/
theorem guard_case (K : Classical') (J : SVal D) (t k n : Nat) (ht : t < n) (P : EF) (Q : List D → Prop)
    (hP : ∀ (J' : SVal D) u, sat d0 I J' u P ↔ Q u) :
    (∀ u, Jk J t k u → Q u) ↔ ∃ J' : SVal D, (∀ i, i < n → ∀ u, J' i u ↔ J i u) ∧ sat d0 I J' [] (allK k (imp' (tv t k) P)) :=
  ⟨fun h => ⟨J, fun _ _ _ => Iff.rfl, (guard d0 I K J t k P).2 (fun u hu ht' => (hP J u).2 (h u ⟨ht', hu⟩))⟩,
   fun ⟨J', hag, hs⟩ u ⟨htu, hu⟩ => (hP J' u).1 ((guard d0 I K J' t k P).1 hs u hu ((hag t ht u).2 htu))⟩

theorem dep_case (K : Classical') (J : SVal D) (t k n : Nat) (ht : t < n) (is : List Nat) (j : Nat)
    (hw : AllLt is k ∧ j < k) :
    TS d0 I (Jk J t k) (.dep is j) ↔ ∃ J' : SVal D, (∀ i, i < n → ∀ u, J' i u ↔ J i u) ∧
      sat d0 I J' [] (allK (k + k) (imp' (.and (tv t k) (.and (.svar t (range' k k)) (eqs k is))) (.eq j (k + j)))) := by
  have key : ∀ (J' : SVal D), (∀ u, J' t u ↔ J t u) →
      (sat d0 I J' [] (allK (k + k) (imp' (.and (tv t k) (.and (.svar t (range' k k)) (eqs k is))) (.eq j (k + j))))
        ↔ TS d0 I (Jk J t k) (.dep is j)) := by
    intro J' hJ
    refine (sat_allK d0 I J' _ (k + k) []).trans ⟨fun h u v ⟨hu, lu⟩ ⟨hv, lv⟩ ha => ?_, fun h e he => ?_⟩
    · have := (sat_imp' d0 I K J' _ _ _).1 (h (u ++ v) (by rw [len_app, lu, lv]))
      have e1 : args d0 (u ++ v) (range' 0 k) = u := by have := args_seg d0 u [] v; rw [lu] at this; exact this
      have e2 : args d0 (u ++ v) (range' k k) = v := by have := args_second d0 u v; rw [lu, lv] at this; exact this
      have hj := this ⟨show J' t (args d0 (u ++ v) (range' 0 k)) by rw [e1]; exact (hJ u).2 hu,
        show J' t (args d0 (u ++ v) (range' k k)) by rw [e2]; exact (hJ v).2 hv,
        (sat_eqs d0 I J' u v k lu is hw.1).2 ha⟩
      have g := hj
      show get d0 u j = get d0 v j
      have g' : get d0 (u ++ v) j = get d0 (u ++ v) (k + j) := g
      rw [get_app_l d0 u v j (lu ▸ hw.2), ← lu, get_app_r] at g'
      exact g'
    · show sat d0 I J' ([] ++ e) _
      match app_dec e k (by rw [he]; exact Nat.le_add_right k k) with
      | ⟨u, v, eu, lu⟩ =>
        have lv : len v = k := by
          have := len_app u v; rw [← eu, he, lu] at this; exact (addc k this).symm
        refine (sat_imp' d0 I K J' _ _ _).2 (fun ⟨h1, h2, h3⟩ => ?_)
        rw [show ([] : List D) ++ e = u ++ v from eu] at h1 h2 h3 ⊢
        have e1 : args d0 (u ++ v) (range' 0 k) = u := by have := args_seg d0 u [] v; rw [lu] at this; exact this
        have e2 : args d0 (u ++ v) (range' k k) = v := by have := args_second d0 u v; rw [lu, lv] at this; exact this
        have h1' : J' t (args d0 (u ++ v) (range' 0 k)) := h1
        have h2' : J' t (args d0 (u ++ v) (range' k k)) := h2
        rw [e1] at h1'
        rw [e2] at h2'
        have g := h u v ⟨(hJ u).1 h1', lu⟩ ⟨(hJ v).1 h2', lv⟩ ((sat_eqs d0 I J' u v k lu is hw.1).1 h3)
        show get d0 (u ++ v) j = get d0 (u ++ v) (k + j)
        rw [get_app_l d0 u v j (lu ▸ hw.2), ← lu, get_app_r]
        exact g
  exact ⟨fun h => ⟨J, fun _ _ _ => Iff.rfl, (key J (fun _ => Iff.rfl)).2 h⟩,
    fun ⟨J', hag, hs⟩ => (key J' (hag t ht)).1 hs⟩

/-! ## correctness of the translation -/

theorem ag_trans {J1 J2 J3 : SVal D} {n m : Nat} (h1 : ∀ i, i < n → ∀ u, J1 i u ↔ J2 i u)
    (h2 : ∀ i, i < m → ∀ u, J2 i u ↔ J3 i u) (hnm : n ≤ m) : ∀ i, i < n → ∀ u, J1 i u ↔ J3 i u :=
  fun i hi u => (h1 i hi u).trans (h2 i (Nat.lt_of_lt_of_le hi hnm) u)

theorem jk_congr {J J' : SVal D} {t k : Nat} (h : ∀ u, J' t u ↔ J t u) : ∀ u, Jk J' t k u ↔ Jk J t k u :=
  fun u => ⟨fun ⟨a, b⟩ => ⟨(h u).1 a, b⟩, fun ⟨a, b⟩ => ⟨(h u).2 a, b⟩⟩

theorem tau_ok (K : Classical') : (φ : DF) → (k t n : Nat) → WFd φ k → t < n → (J : SVal D) →
    (TS d0 I (Jk J t k) φ ↔ ∃ J' : SVal D, (∀ i, i < n → ∀ u, J' i u ↔ J i u) ∧ sat d0 I J' [] (τ φ k t n).1)
  | .rel r is, k, t, n, _, ht, J => guard_case d0 I K J t k n ht _ _ (fun _ _ => Iff.rfl)
  | .nrel r is, k, t, n, _, ht, J => guard_case d0 I K J t k n ht _ _ (fun _ _ => Iff.rfl)
  | .eq i j, k, t, n, _, ht, J => guard_case d0 I K J t k n ht _ _ (fun _ _ => Iff.rfl)
  | .neq i j, k, t, n, _, ht, J => guard_case d0 I K J t k n ht _ _ (fun _ _ => Iff.rfl)
  | .dep is j, k, t, n, hw, ht, J => dep_case d0 I K J t k n ht is j hw
  | .and φ ψ, k, t, n, ⟨w1, w2⟩, ht, J => by
    have m1 := tau_mono φ k t n
    have sa := tau_sb φ k t n ht
    constructor
    · intro ⟨ha, hb⟩
      match (tau_ok K φ k t n w1 ht J).1 ha with
      | ⟨J1, ag1, s1⟩ =>
        have hb' := (ts_congr d0 I ψ _ _ (jk_congr (ag1 t ht))).2 hb
        match (tau_ok K ψ k t _ w2 (Nat.lt_of_lt_of_le ht m1) J1).1 hb' with
        | ⟨J2, ag2, s2⟩ =>
          exact ⟨J2, fun i hi u => (ag2 i (Nat.lt_of_lt_of_le hi m1) u).trans (ag1 i hi u), (sat_congrJ d0 I _ _ sa J2 J1 ag2 []).2 s1, s2⟩
    · intro ⟨J', ag, s1, s2⟩
      have hJ : ∀ u, Jk J' t k u ↔ Jk J t k u := jk_congr (ag t ht)
      exact ⟨(ts_congr d0 I φ _ _ hJ).1 ((tau_ok K φ k t n w1 ht J').2 ⟨J', fun _ _ _ => Iff.rfl, s1⟩),
        (ts_congr d0 I ψ _ _ hJ).1 ((tau_ok K ψ k t _ w2 (Nat.lt_of_lt_of_le ht m1) J').2 ⟨J', fun _ _ _ => Iff.rfl, s2⟩)⟩
  | .or φ ψ, k, t, n, ⟨w1, w2⟩, ht, J => by
    have m1 := tau_mono φ k n (n + 2)
    have sa := tau_sb φ k n (n + 2) (Nat.lt_succ_of_le (Nat.le_succ n))
    have hA : n < n + 2 := Nat.lt_succ_of_le (Nat.le_succ n)
    have hB : n + 1 < (τ φ k n (n + 2)).2 := Nat.lt_of_lt_of_le (Nat.lt_succ_self _) m1
    have rng : ∀ u, len u = k → args d0 u (range' 0 k) = u := fun u hu => by rw [← hu, args_all]
    constructor
    · intro ⟨T1, T2, c, s1, s2, a, b⟩
      let J0 := updJ (updJ J n T1) (n + 1) T2
      have l1 : ∀ u, T1 u → len u = k := fun u h => (s1 u h).2
      have l2 : ∀ u, T2 u → len u = k := fun u h => (s2 u h).2
      have j0n : ∀ u, J0 n u ↔ T1 u := fun u =>
        (updJ_ne _ _ (fun e => Nat.lt_irrefl _ (Nat.lt_of_lt_of_le (Nat.lt_succ_self n) (Nat.le_of_eq e.symm))) u).trans (updJ_eq _ _ _ u)
      have j0n1 : ∀ u, J0 (n + 1) u ↔ T2 u := fun u => updJ_eq _ _ _ u
      have j0lo : ∀ i, i < n → ∀ u, J0 i u ↔ J i u := fun i hi u =>
        (updJ_ne _ _ (fun (e : i = n + 1) => by rw [e] at hi; exact Nat.lt_irrefl n (Nat.lt_trans (Nat.lt_succ_self n) hi)) u).trans
          (updJ_ne _ _ (fun (e : i = n) => by rw [e] at hi; exact Nat.lt_irrefl n hi) u)
      have a' : TS d0 I (Jk J0 n k) φ := (ts_congr d0 I φ _ _ (fun u => ⟨fun h => ⟨(j0n u).2 h, l1 u h⟩,
        fun ⟨h, _⟩ => (j0n u).1 h⟩)).1 a
      match (tau_ok K φ k n (n + 2) w1 hA J0).1 a' with
      | ⟨J1, ag1, sat1⟩ =>
        have b' : TS d0 I (Jk J1 (n + 1) k) ψ := (ts_congr d0 I ψ _ _ (fun u => ⟨fun h => ⟨(ag1 (n + 1) (Nat.lt_succ_self _) u).2 ((j0n1 u).2 h), l2 u h⟩,
          fun ⟨h, _⟩ => (j0n1 u).1 ((ag1 (n + 1) (Nat.lt_succ_self _) u).1 h)⟩)).1 b
        match (tau_ok K ψ k (n + 1) _ w2 hB J1).1 b' with
        | ⟨J2, ag2, sat2⟩ =>
          have J2n : ∀ u, J2 n u ↔ T1 u := fun u => (ag2 n (Nat.lt_trans (Nat.lt_succ_self n) hB) u).trans
            ((ag1 n hA u).trans (j0n u))
          have J2n1 : ∀ u, J2 (n + 1) u ↔ T2 u := fun u => (ag2 (n + 1) hB u).trans
            ((ag1 (n + 1) (Nat.lt_succ_self _) u).trans (j0n1 u))
          have J2t : ∀ u, J2 t u ↔ J t u := fun u => (ag2 t (Nat.lt_trans ht (Nat.lt_trans (Nat.lt_succ_self n) hB)) u).trans
            ((ag1 t (Nat.lt_trans ht hA) u).trans (j0lo t ht u))
          refine ⟨J2, fun i hi u => (ag2 i (Nat.lt_trans hi (Nat.lt_trans (Nat.lt_succ_self n) hB)) u).trans
              ((ag1 i (Nat.lt_trans hi hA) u).trans (j0lo i hi u)), ?_, ?_, ?_, ?_, sat2⟩
          · refine (guard d0 I K J2 t k _).2 (fun u hu h2 => ?_)
            show J2 n (args d0 u (range' 0 k)) ∨ J2 (n + 1) (args d0 u (range' 0 k))
            rw [rng u hu]
            exact (c u ⟨(J2t u).1 h2, hu⟩).elim (fun h => Or.inl ((J2n u).2 h)) (fun h => Or.inr ((J2n1 u).2 h))
          · refine (guard d0 I K J2 n k _).2 (fun u hu h2 => ?_)
            show J2 t (args d0 u (range' 0 k))
            rw [rng u hu]
            exact (J2t u).2 (s1 u ((J2n u).1 h2)).1
          · refine (guard d0 I K J2 (n + 1) k _).2 (fun u hu h2 => ?_)
            show J2 t (args d0 u (range' 0 k))
            rw [rng u hu]
            exact (J2t u).2 (s2 u ((J2n1 u).1 h2)).1
          · exact (sat_congrJ d0 I _ _ sa J2 J1 ag2 []).2 sat1
    · intro ⟨J', ag, cov, sub1, sub2, sat1, sat2⟩
      have hJt : ∀ u, J' t u ↔ J t u := ag t ht
      refine ⟨Jk J' n k, Jk J' (n + 1) k, fun u ⟨h, hu⟩ => ?_, fun u ⟨h, hu⟩ => ?_, fun u ⟨h, hu⟩ => ?_,
        (tau_ok K φ k n (n + 2) w1 hA J').2 ⟨J', fun _ _ _ => Iff.rfl, sat1⟩,
        (tau_ok K ψ k (n + 1) _ w2 hB J').2 ⟨J', fun _ _ _ => Iff.rfl, sat2⟩⟩
      · have := (guard d0 I K J' t k _).1 cov u hu ((hJt u).2 h)
        have this' : J' n (args d0 u (range' 0 k)) ∨ J' (n + 1) (args d0 u (range' 0 k)) := this
        rw [rng u hu] at this'
        exact this'.elim (fun h => Or.inl ⟨h, hu⟩) (fun h => Or.inr ⟨h, hu⟩)
      · have := (guard d0 I K J' n k _).1 sub1 u hu h
        have this' : J' t (args d0 u (range' 0 k)) := this
        rw [rng u hu] at this'
        exact ⟨(hJt u).1 this', hu⟩
      · have := (guard d0 I K J' (n + 1) k _).1 sub2 u hu h
        have this' : J' t (args d0 u (range' 0 k)) := this
        rw [rng u hu] at this'
        exact ⟨(hJt u).1 this', hu⟩
  | .ex φ, k, t, n, w, ht, J => by
    have hS : n < n + 1 := Nat.lt_succ_self n
    have rng : ∀ u, len u = k → args d0 u (range' 0 k) = u := fun u hu => by rw [← hu, args_all]
    have rng1 : ∀ u x, len u = k → args d0 (u ++ [x]) (range' 0 (k + 1)) = u ++ [x] := fun u x hu => by
      rw [← hu, ← len_snoc, args_all]
    have rngi : ∀ u x, len u = k → args d0 (u ++ [x]) (range' 0 k) = u := fun u x hu => by rw [← hu, args_init]
    constructor
    · intro ⟨F, hF⟩
      let T' : Team D := fun u' => ∃ u, Jk J t k u ∧ u' = u ++ [F u]
      let J0 := updJ J n T'
      have j0n : ∀ u, J0 n u ↔ T' u := fun u => updJ_eq _ _ _ u
      have j0lo : ∀ i, i < n → ∀ u, J0 i u ↔ J i u := fun i hi u => updJ_ne _ _ (fun (e : i = n) => by rw [e] at hi; exact Nat.lt_irrefl n hi) u
      have hF' : TS d0 I (Jk J0 n (k + 1)) φ := (ts_congr d0 I φ _ _ (fun u' => ⟨fun h => ⟨(j0n u').2 h,
          match h with
          | ⟨u, ⟨_, hu⟩, e⟩ => by rw [e, len_snoc, hu]⟩, fun ⟨h, _⟩ => (j0n u').1 h⟩)).1
        ((ts_congr d0 I φ _ _ (fun _ => Iff.rfl)).1 hF)
      match (tau_ok K φ (k + 1) n (n + 1) w hS J0).1 hF' with
      | ⟨J1, ag1, sat1⟩ =>
        have J1n : ∀ u, J1 n u ↔ T' u := fun u => (ag1 n hS u).trans (j0n u)
        have J1t : ∀ u, J1 t u ↔ J t u := fun u => (ag1 t (Nat.lt_trans ht hS) u).trans (j0lo t ht u)
        refine ⟨J1, fun i hi u => (ag1 i (Nat.lt_trans hi hS) u).trans (j0lo i hi u), ?_, ?_, sat1⟩
        · refine (guard d0 I K J1 t k _).2 (fun u hu h1 => ⟨F u, ?_⟩)
          show J1 n (args d0 (u ++ [F u]) (range' 0 (k + 1)))
          rw [rng1 u (F u) hu]
          exact (J1n _).2 ⟨u, ⟨(J1t u).1 h1, hu⟩, rfl⟩
        · refine (guard d0 I K J1 n (k + 1) _).2 (fun u' hu' h1 => ?_)
          match (J1n u').1 h1 with
          | ⟨u, ⟨hj, hu⟩, e⟩ =>
            show J1 t (args d0 u' (range' 0 k))
            rw [e, rngi u (F u) hu]
            exact (J1t u).2 hj
    · intro ⟨J', ag, tot, sub, sat1⟩
      have hJt : ∀ u, J' t u ↔ J t u := ag t ht
      let F : List D → D := fun u => K.ch (fun x => (∃ y, J' n (u ++ [y])) → J' n (u ++ [x]))
        ((K.lem (∃ y, J' n (u ++ [y]))).elim (fun ⟨y, hy⟩ => ⟨y, fun _ => hy⟩) (fun hn => ⟨d0, fun h => absurd h hn⟩))
      have hFs : ∀ u, (∃ y, J' n (u ++ [y])) → J' n (u ++ [F u]) := fun u =>
        K.ch_spec (fun x => (∃ y, J' n (u ++ [y])) → J' n (u ++ [x])) _
      refine ⟨F, ts_down d0 I φ (Jk J' n (k + 1)) _ (fun u' ⟨u, ⟨hj, hu⟩, e⟩ => ?_)
        ((tau_ok K φ (k + 1) n (n + 1) w hS J').2 ⟨J', fun _ _ _ => Iff.rfl, sat1⟩)⟩
      have := (guard d0 I K J' t k _).1 tot u hu ((hJt u).2 hj)
      match this with
      | ⟨y, hy⟩ =>
        have hy' : J' n (args d0 (u ++ [y]) (range' 0 (k + 1))) := hy
        rw [rng1 u y hu] at hy'
        rw [e]
        exact ⟨hFs u ⟨y, hy'⟩, by rw [len_snoc, hu]⟩
  | .all φ, k, t, n, w, ht, J => by
    have hS : n < n + 1 := Nat.lt_succ_self n
    have rng1 : ∀ u x, len u = k → args d0 (u ++ [x]) (range' 0 (k + 1)) = u ++ [x] := fun u x hu => by
      rw [← hu, ← len_snoc, args_all]
    have rngi : ∀ u x, len u = k → args d0 (u ++ [x]) (range' 0 k) = u := fun u x hu => by rw [← hu, args_init]
    let T' : Team D := fun u' => ∃ u x, Jk J t k u ∧ u' = u ++ [x]
    -- the clause, read on tuples
    have eqv_iff : ∀ J' : SVal D, (∀ u, J' t u ↔ J t u) →
        (sat d0 I J' [] (allK (k + 1) (.and (imp' (tv n (k + 1)) (tv t k)) (imp' (tv t k) (tv n (k + 1)))))
          ↔ ∀ u x, len u = k → (J' n (u ++ [x]) ↔ J t u)) := by
      intro J' hJt
      refine (sat_allK d0 I J' _ (k + 1) []).trans ⟨fun h u x hu => ?_, fun h u' hu' => ?_⟩
      · have := h (u ++ [x]) (by rw [len_snoc, hu])
        have a := (sat_imp' d0 I K J' _ _ _).1 this.1
        have b := (sat_imp' d0 I K J' _ _ _).1 this.2
        constructor
        · intro hn
          have := a (show J' n (args d0 ([] ++ (u ++ [x])) (range' 0 (k + 1))) by
            show J' n (args d0 (u ++ [x]) (range' 0 (k + 1))); rw [rng1 u x hu]; exact hn)
          have this' : J' t (args d0 (u ++ [x]) (range' 0 k)) := this
          rw [rngi u x hu] at this'
          exact (hJt u).1 this'
        · intro hj
          have := b (show J' t (args d0 ([] ++ (u ++ [x])) (range' 0 k)) by
            show J' t (args d0 (u ++ [x]) (range' 0 k)); rw [rngi u x hu]; exact (hJt u).2 hj)
          have this' : J' n (args d0 (u ++ [x]) (range' 0 (k + 1))) := this
          rw [rng1 u x hu] at this'
          exact this'
      · match snoc_dec u' k hu' with
        | ⟨u, x, e, hu⟩ =>
          show sat d0 I J' ([] ++ u') _
          rw [show ([] : List D) ++ u' = u ++ [x] from e]
          refine ⟨(sat_imp' d0 I K J' _ _ _).2 (fun hn => ?_), (sat_imp' d0 I K J' _ _ _).2 (fun hj => ?_)⟩
          · have hn' : J' n (args d0 (u ++ [x]) (range' 0 (k + 1))) := hn
            rw [rng1 u x hu] at hn'
            show J' t (args d0 (u ++ [x]) (range' 0 k))
            rw [rngi u x hu]
            exact (hJt u).2 ((h u x hu).1 hn')
          · have hj' : J' t (args d0 (u ++ [x]) (range' 0 k)) := hj
            rw [rngi u x hu] at hj'
            show J' n (args d0 (u ++ [x]) (range' 0 (k + 1)))
            rw [rng1 u x hu]
            exact (h u x hu).2 ((hJt u).1 hj')
    constructor
    · intro hT
      let J0 := updJ J n T'
      have j0n : ∀ u, J0 n u ↔ T' u := fun u => updJ_eq _ _ _ u
      have j0lo : ∀ i, i < n → ∀ u, J0 i u ↔ J i u := fun i hi u => updJ_ne _ _ (fun (e : i = n) => by rw [e] at hi; exact Nat.lt_irrefl n hi) u
      have hT' : TS d0 I (Jk J0 n (k + 1)) φ := (ts_congr d0 I φ _ _ (fun u' => ⟨fun h => ⟨(j0n u').2 h,
          match h with
          | ⟨u, _, ⟨_, hu⟩, e⟩ => by rw [e, len_snoc, hu]⟩, fun ⟨h, _⟩ => (j0n u').1 h⟩)).1
        ((ts_congr d0 I φ _ _ (fun _ => Iff.rfl)).1 hT)
      match (tau_ok K φ (k + 1) n (n + 1) w hS J0).1 hT' with
      | ⟨J1, ag1, sat1⟩ =>
        have J1n : ∀ u, J1 n u ↔ T' u := fun u => (ag1 n hS u).trans (j0n u)
        have J1t : ∀ u, J1 t u ↔ J t u := fun u => (ag1 t (Nat.lt_trans ht hS) u).trans (j0lo t ht u)
        refine ⟨J1, fun i hi u => (ag1 i (Nat.lt_trans hi hS) u).trans (j0lo i hi u), (eqv_iff J1 J1t).2
          (fun u x hu => ⟨fun h => ?_, fun h => (J1n _).2 ⟨u, x, ⟨h, hu⟩, rfl⟩⟩), sat1⟩
        match (J1n _).1 h with
        | ⟨u2, x2, ⟨hj, _⟩, e⟩ =>
          have := snoc_inj u u2 x x2 e
          rw [this.1]; exact hj
    · intro ⟨J', ag, heqv, sat1⟩
      have hJt : ∀ u, J' t u ↔ J t u := ag t ht
      have h := (eqv_iff J' hJt).1 heqv
      have hφ := (tau_ok K φ (k + 1) n (n + 1) w hS J').2 ⟨J', fun _ _ _ => Iff.rfl, sat1⟩
      refine (ts_congr d0 I φ _ _ (fun u' => ⟨fun ⟨hn, hl⟩ => ?_, fun ⟨u, x, ⟨hj, hu⟩, e⟩ => ?_⟩)).1 hφ
      · match snoc_dec u' k hl with
        | ⟨u, x, e, hu⟩ => exact ⟨u, x, ⟨(h u x hu).1 (e ▸ hn), hu⟩, e⟩
      · rw [e]; exact ⟨(h u x hu).2 hj, by rw [len_snoc, hu]⟩

/-- dependence logic → ESO, for sentences -/
theorem d_to_eso (K : Classical') (φ : DF) (hw : WFd φ 0) : DTrue d0 I φ ↔ ETrue d0 I (toESO φ) := by
  constructor
  · intro h
    let J0 : SVal D := fun _ u => u = []
    have hT : TS d0 I (Jk J0 0 0) φ := (ts_congr d0 I φ _ _ (fun u => ⟨fun e => ⟨e, by rw [e]; rfl⟩,
      fun ⟨e, _⟩ => e⟩)).1 h
    match (tau_ok d0 I K φ 0 0 1 hw (Nat.succ_pos 0) J0).1 hT with
    | ⟨J', ag, s⟩ => exact ⟨J', (ag 0 (Nat.succ_pos 0) []).2 rfl, s⟩
  · intro ⟨J, h0, s⟩
    have := (tau_ok d0 I K φ 0 0 1 hw (Nat.succ_pos 0) J).2 ⟨J, fun _ _ _ => Iff.rfl, s⟩
    refine (ts_congr d0 I φ _ _ (fun u => ⟨fun ⟨_, hl⟩ => ?_, fun e => ⟨by rw [e]; exact h0, by rw [e]; rfl⟩⟩)).1 this
    cases u with
    | nil => rfl
    | cons _ _ => exact Nat.noConfusion hl

end DE

end Found

#print axioms Found.DE.args_seg
#print axioms Found.DE.snoc_dec
#print axioms Found.DE.tau_mono
#print axioms Found.DE.tau_sb
#print axioms Found.DE.sat_allK
#print axioms Found.DE.guard
#print axioms Found.DE.sat_eqs
#print axioms Found.DE.ts_down
#print axioms Found.DE.guard_case
#print axioms Found.DE.dep_case
#print axioms Found.DE.tau_ok
#print axioms Found.DE.d_to_eso
#print axioms Found.DE.snoc_inj
#print axioms Found.DE.updJ_eq
#print axioms Found.DE.updJ_ne
#print axioms Found.DE.app_dec
#print axioms Found.DE.addc
#print axioms Found.DE.len_snoc
#print axioms Found.DE.ts_congr

found/DepE3.lean

lean_all/found/DepE3.lean

/-
  Line dependence logic ↔ ESO, part 3. found/DepE3 — tools for ESO → D, in
  the classical point:
  * negation normal form NF of the ESO matrix (literals R ī, ¬R ī, X ī, ¬X ī,
    xᵢ = xⱼ, xᵢ ≠ xⱼ; ∧, ∨, ∃, ∀) with nnf θ ↔ θ (and the dual ↔ ¬θ);
  * first-order formulas are flat in dependence logic: a NF formula without
    relation variables, read as a dependence formula, holds in a team iff it
    holds at every tuple of the team (flat).

  Lean 4 core only, hand-written proofs, no axioms (classical point K as a
  parameter).
-/
import found.DepE2

namespace Found

namespace DE

open CP

variable {D : Type} (d0 : D) (I : Nat → List D → Prop)

inductive NF where
  | rel : Nat → List Nat → NF
  | nrel : Nat → List Nat → NF
  | svar : Nat → List Nat → NF
  | nsvar : Nat → List Nat → NF
  | eq : Nat → Nat → NF
  | neq : Nat → Nat → NF
  | and : NF → NF → NF
  | or : NF → NF → NF
  | ex : NF → NF
  | all : NF → NF

def semN (J : SVal D) : List D → NF → Prop
  | e, .rel r is => I r (args d0 e is)
  | e, .nrel r is => ¬ I r (args d0 e is)
  | e, .svar i is => J i (args d0 e is)
  | e, .nsvar i is => ¬ J i (args d0 e is)
  | e, .eq i j => get d0 e i = get d0 e j
  | e, .neq i j => get d0 e i ≠ get d0 e j
  | e, .and φ ψ => semN J e φ ∧ semN J e ψ
  | e, .or φ ψ => semN J e φ ∨ semN J e ψ
  | e, .ex φ => ∃ a, semN J (e ++ [a]) φ
  | e, .all φ => ∀ a, semN J (e ++ [a]) φ

/-- negation normal form; the flag false gives the negation -/
def nnf : EF → Bool → NF
  | .rel r is, true => .rel r is
  | .rel r is, false => .nrel r is
  | .svar i is, true => .svar i is
  | .svar i is, false => .nsvar i is
  | .eq i j, true => .eq i j
  | .eq i j, false => .neq i j
  | .neg φ, true => nnf φ false
  | .neg φ, false => nnf φ true
  | .and φ ψ, true => .and (nnf φ true) (nnf ψ true)
  | .and φ ψ, false => .or (nnf φ false) (nnf ψ false)
  | .or φ ψ, true => .or (nnf φ true) (nnf ψ true)
  | .or φ ψ, false => .and (nnf φ false) (nnf ψ false)
  | .ex φ, true => .ex (nnf φ true)
  | .ex φ, false => .all (nnf φ false)
  | .all φ, true => .all (nnf φ true)
  | .all φ, false => .ex (nnf φ false)

theorem nnf_ok (K : Classical') (J : SVal D) : (θ : EF) → (e : List D) →
    (semN d0 I J e (nnf θ true) ↔ sat d0 I J e θ) ∧ (semN d0 I J e (nnf θ false) ↔ ¬ sat d0 I J e θ)
  | .rel _ _, _ => ⟨Iff.rfl, Iff.rfl⟩
  | .svar _ _, _ => ⟨Iff.rfl, Iff.rfl⟩
  | .eq _ _, _ => ⟨Iff.rfl, Iff.rfl⟩
  | .neg φ, e => ⟨(nnf_ok K J φ e).2, (nnf_ok K J φ e).1.trans ⟨fun h n => n h, fun h => dne K _ h⟩⟩
  | .and φ ψ, e =>
    have h1 := nnf_ok K J φ e
    have h2 := nnf_ok K J ψ e
    ⟨⟨fun ⟨a, b⟩ => ⟨h1.1.1 a, h2.1.1 b⟩, fun ⟨a, b⟩ => ⟨h1.1.2 a, h2.1.2 b⟩⟩,
     ⟨fun h ⟨a, b⟩ => h.elim (fun x => h1.2.1 x a) (fun y => h2.2.1 y b),
      fun h => (K.lem (sat d0 I J e φ)).elim
        (fun a => Or.inr (h2.2.2 (fun b => h ⟨a, b⟩))) (fun na => Or.inl (h1.2.2 na))⟩⟩
  | .or φ ψ, e =>
    have h1 := nnf_ok K J φ e
    have h2 := nnf_ok K J ψ e
    ⟨⟨fun h => h.elim (fun a => Or.inl (h1.1.1 a)) (fun b => Or.inr (h2.1.1 b)),
      fun h => h.elim (fun a => Or.inl (h1.1.2 a)) (fun b => Or.inr (h2.1.2 b))⟩,
     ⟨fun ⟨a, b⟩ h => h.elim (h1.2.1 a) (h2.2.1 b),
      fun h => ⟨h1.2.2 (fun a => h (Or.inl a)), h2.2.2 (fun b => h (Or.inr b))⟩⟩⟩
  | .ex φ, _ =>
    ⟨⟨fun ⟨a, h⟩ => ⟨a, (nnf_ok K J φ _).1.1 h⟩, fun ⟨a, h⟩ => ⟨a, (nnf_ok K J φ _).1.2 h⟩⟩,
     ⟨fun h ⟨a, s⟩ => (nnf_ok K J φ _).2.1 (h a) s, fun h a => (nnf_ok K J φ _).2.2 (fun s => h ⟨a, s⟩)⟩⟩
  | .all φ, _ =>
    ⟨⟨fun h a => (nnf_ok K J φ _).1.1 (h a), fun h a => (nnf_ok K J φ _).1.2 (h a)⟩,
     ⟨fun ⟨a, s⟩ h => (nnf_ok K J φ _).2.1 s (h a),
      fun h => dne K _ (fun nex => h (fun a => dne K _ (fun ns => nex ⟨a, (nnf_ok K J φ _).2.2 ns⟩)))⟩⟩

/-! ## first-order formulas are flat -/

/-- the dependence-logic reading of a relation-variable-free NF formula -/
def toD : NF → DF
  | .rel r is => .rel r is
  | .nrel r is => .nrel r is
  | .svar _ _ => .eq 0 0
  | .nsvar _ _ => .eq 0 0
  | .eq i j => .eq i j
  | .neq i j => .neq i j
  | .and φ ψ => .and (toD φ) (toD ψ)
  | .or φ ψ => .or (toD φ) (toD ψ)
  | .ex φ => .ex (toD φ)
  | .all φ => .all (toD φ)

/-- no relation variables -/
def NoS : NF → Prop
  | .svar _ _ => False
  | .nsvar _ _ => False
  | .and φ ψ => NoS φ ∧ NoS ψ
  | .or φ ψ => NoS φ ∧ NoS ψ
  | .ex φ => NoS φ
  | .all φ => NoS φ
  | .rel _ _ => True
  | .nrel _ _ => True
  | .eq _ _ => True
  | .neq _ _ => True

theorem flat (K : Classical') (J : SVal D) : (φ : NF) → NoS φ → (T : Team D) →
    (TS d0 I T (toD φ) ↔ ∀ u, T u → semN d0 I J u φ)
  | .rel _ _, _, _ => Iff.rfl
  | .nrel _ _, _, _ => Iff.rfl
  | .eq _ _, _, _ => Iff.rfl
  | .neq _ _, _, _ => Iff.rfl
  | .and φ ψ, ⟨h1, h2⟩, T => ⟨fun ⟨a, b⟩ u hu => ⟨(flat K J φ h1 T).1 a u hu, (flat K J ψ h2 T).1 b u hu⟩,
      fun h => ⟨(flat K J φ h1 T).2 (fun u hu => (h u hu).1), (flat K J ψ h2 T).2 (fun u hu => (h u hu).2)⟩⟩
  | .or φ ψ, ⟨h1, h2⟩, T => ⟨fun ⟨T1, T2, c, _, _, a, b⟩ u hu => (c u hu).elim
        (fun h => Or.inl ((flat K J φ h1 T1).1 a u h)) (fun h => Or.inr ((flat K J ψ h2 T2).1 b u h)),
      fun h => ⟨fun u => T u ∧ semN d0 I J u φ, fun u => T u ∧ ¬ semN d0 I J u φ,
        fun u hu => (K.lem (semN d0 I J u φ)).elim (fun a => Or.inl ⟨hu, a⟩) (fun na => Or.inr ⟨hu, na⟩),
        fun _ hu => hu.1, fun _ hu => hu.1,
        (flat K J φ h1 _).2 (fun _ hu => hu.2),
        (flat K J ψ h2 _).2 (fun u hu => (h u hu.1).elim (fun a => absurd a hu.2) id)⟩⟩
  | .ex φ, h, T => ⟨fun ⟨F, a⟩ u hu => ⟨F u, (flat K J φ h _).1 a _ ⟨u, hu, rfl⟩⟩,
      fun hall =>
        let F : List D → D := fun u => K.ch (fun x => (∃ y, semN d0 I J (u ++ [y]) φ) → semN d0 I J (u ++ [x]) φ)
          ((K.lem (∃ y, semN d0 I J (u ++ [y]) φ)).elim (fun ⟨y, hy⟩ => ⟨y, fun _ => hy⟩)
            (fun hn => ⟨d0, fun h' => absurd h' hn⟩))
        ⟨F, (flat K J φ h _).2 (fun u' ⟨u, hu, e⟩ => by
          rw [e]
          exact K.ch_spec (fun x => (∃ y, semN d0 I J (u ++ [y]) φ) → semN d0 I J (u ++ [x]) φ) _ (hall u hu))⟩⟩
  | .all φ, h, T => ((flat K J φ h _)).trans ⟨fun s u hu a => s _ ⟨u, a, hu, rfl⟩,
      fun s u' ⟨u, a, hu, e⟩ => by rw [e]; exact s u hu a⟩

end DE

end Found

#print axioms Found.DE.nnf_ok
#print axioms Found.DE.flat

found/DepE4.lean

lean_all/found/DepE4.lean

/-
  Line dependence logic ↔ ESO, part 4. found/DepE4 — prenex normal form of
  the negation normal form, with variables as levels: shifting the levels
  ≥ d by k is inserting k values into the environment at position d
  (sh_ok); formulas only read their scope (locality); quantifier prefixes
  compose and commute with constant conjuncts and disjuncts (classically,
  over a nonempty domain); every scoped NF formula is equivalent to a
  quantifier prefix over a quantifier-free matrix (pre_ok).

  Lean 4 core only, hand-written proofs, no axioms (classical point K as a
  parameter).
-/
import found.DepE3

namespace Found

namespace DE

open CP

variable {D : Type} (d0 : D) (I : Nat → List D → Prop)

/-! ## shifting levels -/

def shi (d k i : Nat) : Nat := cond (Nat.blt i d) i (i + k)

def shL (d k : Nat) : List Nat → List Nat
  | [] => []
  | i :: is => shi d k i :: shL d k is

def shN (d k : Nat) : NF → NF
  | .rel r is => .rel r (shL d k is)
  | .nrel r is => .nrel r (shL d k is)
  | .svar i is => .svar i (shL d k is)
  | .nsvar i is => .nsvar i (shL d k is)
  | .eq i j => .eq (shi d k i) (shi d k j)
  | .neq i j => .neq (shi d k i) (shi d k j)
  | .and φ ψ => .and (shN d k φ) (shN d k ψ)
  | .or φ ψ => .or (shN d k φ) (shN d k ψ)
  | .ex φ => .ex (shN d k φ)
  | .all φ => .all (shN d k φ)

theorem blt_t' {i k : Nat} (h : i < k) : Nat.blt i k = true := Nat.ble_eq_true_of_le h
theorem blt_f' {i k : Nat} (h : k ≤ i) : Nat.blt i k = false := by
  cases e : Nat.blt i k with
  | false => rfl
  | true => exact absurd (Nat.lt_of_lt_of_le (Nat.le_of_ble_eq_true e) h) (Nat.lt_irrefl _)

theorem get_sh (e u v : List D) (i : Nat) :
    get d0 (e ++ (u ++ v)) (shi (len e) (len u) i) = get d0 (e ++ v) i := by
  cases Nat.lt_or_ge i (len e) with
  | inl hl =>
    unfold shi; rw [blt_t' hl]
    show get d0 (e ++ (u ++ v)) i = _
    rw [get_app_l d0 e _ i hl, get_app_l d0 e _ i hl]
  | inr hg =>
    unfold shi; rw [blt_f' hg]
    obtain ⟨j, hj⟩ := le_dest'' hg
    show get d0 (e ++ (u ++ v)) (i + len u) = _
    rw [hj, Nat.add_assoc, get_app_r, Nat.add_comm, get_app_r, get_app_r]
where
  le_dest'' {a b : Nat} (h : a ≤ b) : ∃ d, b = a + d := by
    induction h with
    | refl => exact ⟨0, rfl⟩
    | step _ ih => match ih with
      | ⟨d, e⟩ => exact ⟨d + 1, by rw [e]; rfl⟩

theorem args_sh (e u v : List D) : (is : List Nat) →
    args d0 (e ++ (u ++ v)) (shL (len e) (len u) is) = args d0 (e ++ v) is
  | [] => rfl
  | i :: is => by
    show get d0 _ (shi _ _ i) :: args d0 _ (shL _ _ is) = get d0 _ i :: args d0 _ is
    rw [get_sh d0 e u v i, args_sh e u v is]

/-- shifting = inserting values -/
theorem sh_ok (J : SVal D) : (m : NF) → (e u v : List D) →
    (semN d0 I J (e ++ (u ++ v)) (shN (len e) (len u) m) ↔ semN d0 I J (e ++ v) m)
  | .rel r is, e, u, v => by show I r _ ↔ I r _; rw [args_sh]
  | .nrel r is, e, u, v => by show ¬ I r _ ↔ ¬ I r _; rw [args_sh]
  | .svar i is, e, u, v => by show J i _ ↔ J i _; rw [args_sh]
  | .nsvar i is, e, u, v => by show ¬ J i _ ↔ ¬ J i _; rw [args_sh]
  | .eq i j, e, u, v => by show get d0 _ _ = get d0 _ _ ↔ _; rw [get_sh, get_sh]; exact Iff.rfl
  | .neq i j, e, u, v => by show get d0 _ _ ≠ get d0 _ _ ↔ _; rw [get_sh, get_sh]; exact Iff.rfl
  | .and φ ψ, e, u, v => ⟨fun ⟨a, b⟩ => ⟨(sh_ok J φ e u v).1 a, (sh_ok J ψ e u v).1 b⟩,
      fun ⟨a, b⟩ => ⟨(sh_ok J φ e u v).2 a, (sh_ok J ψ e u v).2 b⟩⟩
  | .or φ ψ, e, u, v => ⟨fun h => h.elim (fun a => Or.inl ((sh_ok J φ e u v).1 a)) (fun b => Or.inr ((sh_ok J ψ e u v).1 b)),
      fun h => h.elim (fun a => Or.inl ((sh_ok J φ e u v).2 a)) (fun b => Or.inr ((sh_ok J ψ e u v).2 b))⟩
  | .ex φ, e, u, v => by
    show (∃ a, semN d0 I J (e ++ (u ++ v) ++ [a]) _) ↔ ∃ a, semN d0 I J (e ++ v ++ [a]) φ
    constructor
    · intro ⟨a, h⟩
      rw [app_assoc'', app_assoc''] at h
      exact ⟨a, by rw [app_assoc'']; exact (sh_ok J φ e u (v ++ [a])).1 h⟩
    · intro ⟨a, h⟩
      rw [app_assoc''] at h
      exact ⟨a, by rw [app_assoc'', app_assoc'']; exact (sh_ok J φ e u (v ++ [a])).2 h⟩
  | .all φ, e, u, v => by
    show (∀ a, semN d0 I J (e ++ (u ++ v) ++ [a]) _) ↔ ∀ a, semN d0 I J (e ++ v ++ [a]) φ
    constructor
    · intro h a
      have := h a
      rw [app_assoc'', app_assoc''] at this
      rw [app_assoc'']; exact (sh_ok J φ e u (v ++ [a])).1 this
    · intro h a
      have := h a
      rw [app_assoc''] at this
      rw [app_assoc'', app_assoc'']; exact (sh_ok J φ e u (v ++ [a])).2 this

/-! ## scope and locality -/

def ScN : NF → Nat → Prop
  | .rel _ is, n => AllLt is n
  | .nrel _ is, n => AllLt is n
  | .svar _ is, n => AllLt is n
  | .nsvar _ is, n => AllLt is n
  | .eq i j, n => i < n ∧ j < n
  | .neq i j, n => i < n ∧ j < n
  | .and φ ψ, n => ScN φ n ∧ ScN ψ n
  | .or φ ψ, n => ScN φ n ∧ ScN ψ n
  | .ex φ, n => ScN φ (n + 1)
  | .all φ, n => ScN φ (n + 1)

theorem shi_lt {d k i : Nat} (h : i < d) : shi d k i = i := by unfold shi; rw [blt_t' h]; rfl

theorem allLt_mono {a b : Nat} (h : a ≤ b) : (is : List Nat) → AllLt is a → AllLt is b
  | [], _ => trivial
  | _ :: is, ⟨hi, hs⟩ => ⟨Nat.lt_of_lt_of_le hi h, allLt_mono h is hs⟩

/-- quantifier-free -/
def QF : NF → Prop
  | .and φ ψ => QF φ ∧ QF ψ
  | .or φ ψ => QF φ ∧ QF ψ
  | .ex _ => False
  | .all _ => False
  | .rel _ _ => True
  | .nrel _ _ => True
  | .svar _ _ => True
  | .nsvar _ _ => True
  | .eq _ _ => True
  | .neq _ _ => True

theorem args_loc (e w : List D) : (is : List Nat) → AllLt is (len e) → args d0 (e ++ w) is = args d0 e is
  | [], _ => rfl
  | i :: is, ⟨hi, hs⟩ => by
    show get d0 (e ++ w) i :: args d0 (e ++ w) is = get d0 e i :: args d0 e is
    rw [get_app_l d0 e w i hi, args_loc e w is hs]

/-- a quantifier-free formula only reads its scope -/
theorem loc_qf (J : SVal D) (e w : List D) : (m : NF) → QF m → ScN m (len e) →
    (semN d0 I J (e ++ w) m ↔ semN d0 I J e m)
  | .rel r is, _, h => by show I r _ ↔ I r _; rw [args_loc d0 e w is h]
  | .nrel r is, _, h => by show ¬ I r _ ↔ ¬ I r _; rw [args_loc d0 e w is h]
  | .svar i is, _, h => by show J i _ ↔ J i _; rw [args_loc d0 e w is h]
  | .nsvar i is, _, h => by show ¬ J i _ ↔ ¬ J i _; rw [args_loc d0 e w is h]
  | .eq i j, _, ⟨hi, hj⟩ => by show get d0 _ i = get d0 _ j ↔ _; rw [get_app_l d0 e w i hi, get_app_l d0 e w j hj]; exact Iff.rfl
  | .neq i j, _, ⟨hi, hj⟩ => by show get d0 _ i ≠ get d0 _ j ↔ _; rw [get_app_l d0 e w i hi, get_app_l d0 e w j hj]; exact Iff.rfl
  | .and φ ψ, ⟨q1, q2⟩, ⟨h1, h2⟩ => ⟨fun ⟨a, b⟩ => ⟨(loc_qf J e w φ q1 h1).1 a, (loc_qf J e w ψ q2 h2).1 b⟩,
      fun ⟨a, b⟩ => ⟨(loc_qf J e w φ q1 h1).2 a, (loc_qf J e w ψ q2 h2).2 b⟩⟩
  | .or φ ψ, ⟨q1, q2⟩, ⟨h1, h2⟩ => ⟨fun h => h.elim (fun a => Or.inl ((loc_qf J e w φ q1 h1).1 a))
        (fun b => Or.inr ((loc_qf J e w ψ q2 h2).1 b)),
      fun h => h.elim (fun a => Or.inl ((loc_qf J e w φ q1 h1).2 a)) (fun b => Or.inr ((loc_qf J e w ψ q2 h2).2 b))⟩

theorem shi_bnd {d k n i : Nat} (_ : d ≤ n) (h : i < n) : shi d k i < n + k := by
  unfold shi
  cases Nat.lt_or_ge i d with
  | inl hl => rw [blt_t' hl]; exact Nat.lt_of_lt_of_le h (Nat.le_add_right n k)
  | inr hg => rw [blt_f' hg]; exact Nat.add_lt_add_right h k

theorem shL_bnd {d k n : Nat} (hd : d ≤ n) : (is : List Nat) → AllLt is n → AllLt (shL d k is) (n + k)
  | [], _ => trivial
  | _ :: is, ⟨hi, hs⟩ => ⟨shi_bnd hd hi, shL_bnd hd is hs⟩

theorem sc_sh {d k : Nat} : (m : NF) → (n : Nat) → d ≤ n → ScN m n → ScN (shN d k m) (n + k)
  | .rel _ is, n, hd, h => shL_bnd hd is h
  | .nrel _ is, n, hd, h => shL_bnd hd is h
  | .svar _ is, n, hd, h => shL_bnd hd is h
  | .nsvar _ is, n, hd, h => shL_bnd hd is h
  | .eq _ _, n, hd, ⟨hi, hj⟩ => ⟨shi_bnd hd hi, shi_bnd hd hj⟩
  | .neq _ _, n, hd, ⟨hi, hj⟩ => ⟨shi_bnd hd hi, shi_bnd hd hj⟩
  | .and φ ψ, n, hd, ⟨a, b⟩ => ⟨sc_sh φ n hd a, sc_sh ψ n hd b⟩
  | .or φ ψ, n, hd, ⟨a, b⟩ => ⟨sc_sh φ n hd a, sc_sh ψ n hd b⟩
  | .ex φ, n, hd, h => by
    have := sc_sh (k := k) φ (n + 1) (Nat.le_succ_of_le hd) h
    show ScN (shN d k φ) (n + k + 1)
    rw [Nat.add_right_comm]; exact this
  | .all φ, n, hd, h => by
    have := sc_sh (k := k) φ (n + 1) (Nat.le_succ_of_le hd) h
    show ScN (shN d k φ) (n + k + 1)
    rw [Nat.add_right_comm]; exact this

theorem qf_sh {d k : Nat} : (m : NF) → QF m → QF (shN d k m)
  | .rel _ _, _ => trivial
  | .nrel _ _, _ => trivial
  | .svar _ _, _ => trivial
  | .nsvar _ _, _ => trivial
  | .eq _ _, _ => trivial
  | .neq _ _, _ => trivial
  | .and φ ψ, ⟨a, b⟩ => ⟨qf_sh φ a, qf_sh ψ b⟩
  | .or φ ψ, ⟨a, b⟩ => ⟨qf_sh φ a, qf_sh ψ b⟩

theorem scN_mono {a b : Nat} (h : a ≤ b) : (m : NF) → ScN m a → ScN m b
  | .rel _ is, hs => allLt_mono h is hs
  | .nrel _ is, hs => allLt_mono h is hs
  | .svar _ is, hs => allLt_mono h is hs
  | .nsvar _ is, hs => allLt_mono h is hs
  | .eq _ _, ⟨hi, hj⟩ => ⟨Nat.lt_of_lt_of_le hi h, Nat.lt_of_lt_of_le hj h⟩
  | .neq _ _, ⟨hi, hj⟩ => ⟨Nat.lt_of_lt_of_le hi h, Nat.lt_of_lt_of_le hj h⟩
  | .and φ ψ, ⟨x, y⟩ => ⟨scN_mono h φ x, scN_mono h ψ y⟩
  | .or φ ψ, ⟨x, y⟩ => ⟨scN_mono h φ x, scN_mono h ψ y⟩
  | .ex φ, x => scN_mono (Nat.succ_le_succ h) φ x
  | .all φ, x => scN_mono (Nat.succ_le_succ h) φ x

/-! ## quantifier prefixes -/

def lenB : List Bool → Nat
  | [] => 0
  | _ :: p => lenB p + 1

/-- true = ∀, false = ∃ -/
def QP : List Bool → List D → (List D → Prop) → Prop
  | [], e, P => P e
  | true :: p, e, P => ∀ a, QP p (e ++ [a]) P
  | false :: p, e, P => ∃ a, QP p (e ++ [a]) P

theorem lenB_app : (p q : List Bool) → lenB (p ++ q) = lenB p + lenB q
  | [], q => (Nat.zero_add _).symm
  | _ :: p, q => by show lenB (p ++ q) + 1 = lenB p + 1 + lenB q; rw [lenB_app p q, Nat.add_right_comm]

theorem qp_app : (p q : List Bool) → (e : List D) → (P : List D → Prop) →
    (QP (p ++ q) e P ↔ QP p e (fun e1 => QP q e1 P))
  | [], _, _, _ => Iff.rfl
  | true :: p, q, _, P => ⟨fun h a => (qp_app p q _ P).1 (h a), fun h a => (qp_app p q _ P).2 (h a)⟩
  | false :: p, q, _, P => ⟨fun ⟨a, h⟩ => ⟨a, (qp_app p q _ P).1 h⟩, fun ⟨a, h⟩ => ⟨a, (qp_app p q _ P).2 h⟩⟩

theorem qp_congr : (p : List Bool) → (e : List D) → (P P' : List D → Prop) →
    (∀ u, len u = lenB p → (P (e ++ u) ↔ P' (e ++ u))) → (QP p e P ↔ QP p e P')
  | [], e, P, P', h => by have := h [] rfl; rw [app_nil'] at this; exact this
  | true :: p, e, P, P', h => ⟨fun s a => (qp_congr p _ P P' (fun u hu => by
        rw [app_assoc'']; exact h _ (by show len u + 1 = lenB p + 1; rw [hu]))).1 (s a),
      fun s a => (qp_congr p _ P P' (fun u hu => by
        rw [app_assoc'']; exact h _ (by show len u + 1 = lenB p + 1; rw [hu]))).2 (s a)⟩
  | false :: p, e, P, P', h => ⟨fun ⟨a, s⟩ => ⟨a, (qp_congr p _ P P' (fun u hu => by
        rw [app_assoc'']; exact h _ (by show len u + 1 = lenB p + 1; rw [hu]))).1 s⟩,
      fun ⟨a, s⟩ => ⟨a, (qp_congr p _ P P' (fun u hu => by
        rw [app_assoc'']; exact h _ (by show len u + 1 = lenB p + 1; rw [hu]))).2 s⟩⟩

include d0 in
theorem qp_const_and (A : Prop) : (p : List Bool) → (e : List D) → (P : List D → Prop) →
    (QP p e (fun e' => A ∧ P e') ↔ A ∧ QP p e P)
  | [], _, _ => Iff.rfl
  | true :: p, _, P => ⟨fun h => ⟨((qp_const_and A p _ P).1 (h d0)).1, fun a => ((qp_const_and A p _ P).1 (h a)).2⟩,
      fun ⟨ha, h⟩ a => (qp_const_and A p _ P).2 ⟨ha, h a⟩⟩
  | false :: p, _, P => ⟨fun ⟨a, h⟩ => ⟨((qp_const_and A p _ P).1 h).1, a, ((qp_const_and A p _ P).1 h).2⟩,
      fun ⟨ha, a, h⟩ => ⟨a, (qp_const_and A p _ P).2 ⟨ha, h⟩⟩⟩

include d0 in
theorem qp_const_or (K : Classical') (A : Prop) : (p : List Bool) → (e : List D) → (P : List D → Prop) →
    (QP p e (fun e' => A ∨ P e') ↔ A ∨ QP p e P)
  | [], _, _ => Iff.rfl
  | true :: p, _, P => ⟨fun h => (K.lem A).elim Or.inl (fun na => Or.inr (fun a =>
        ((qp_const_or K A p _ P).1 (h a)).elim (fun x => absurd x na) id)),
      fun h a => (qp_const_or K A p _ P).2 (h.elim Or.inl (fun x => Or.inr (x a)))⟩
  | false :: p, _, P => ⟨fun ⟨a, h⟩ => ((qp_const_or K A p _ P).1 h).elim Or.inl (fun x => Or.inr ⟨a, x⟩),
      fun h => h.elim (fun x => ⟨d0, (qp_const_or K A p _ P).2 (Or.inl x)⟩)
        (fun ⟨a, x⟩ => ⟨a, (qp_const_or K A p _ P).2 (Or.inr x)⟩)⟩

theorem qp_sh (J : SVal D) (m : NF) (e u : List D) : (p : List Bool) → (v : List D) →
    (QP p (e ++ (u ++ v)) (fun e' => semN d0 I J e' (shN (len e) (len u) m)) ↔
      QP p (e ++ v) (fun e' => semN d0 I J e' m))
  | [], v => sh_ok d0 I J m e u v
  | true :: p, v => ⟨fun h a => by
        have := h a; rw [app_assoc'', app_assoc''] at this
        have := (qp_sh J m e u p (v ++ [a])).1 this
        rw [app_assoc'']; exact this,
      fun h a => by
        have := h a; rw [app_assoc''] at this
        have := (qp_sh J m e u p (v ++ [a])).2 this
        rw [app_assoc'', app_assoc'']; exact this⟩
  | false :: p, v => ⟨fun ⟨a, h⟩ => by
        rw [app_assoc'', app_assoc''] at h
        exact ⟨a, by rw [app_assoc'']; exact (qp_sh J m e u p (v ++ [a])).1 h⟩,
      fun ⟨a, h⟩ => by
        rw [app_assoc''] at h
        exact ⟨a, by rw [app_assoc'', app_assoc'']; exact (qp_sh J m e u p (v ++ [a])).2 h⟩⟩

/-! ## the prenex form -/

def pre : NF → Nat → List Bool × NF
  | .rel r is, _ => ([], .rel r is)
  | .nrel r is, _ => ([], .nrel r is)
  | .svar i is, _ => ([], .svar i is)
  | .nsvar i is, _ => ([], .nsvar i is)
  | .eq i j, _ => ([], .eq i j)
  | .neq i j, _ => ([], .neq i j)
  | .and φ ψ, d => ((pre φ d).1 ++ (pre ψ d).1, .and (pre φ d).2 (shN d (lenB (pre φ d).1) (pre ψ d).2))
  | .or φ ψ, d => ((pre φ d).1 ++ (pre ψ d).1, .or (pre φ d).2 (shN d (lenB (pre φ d).1) (pre ψ d).2))
  | .ex φ, d => (false :: (pre φ (d + 1)).1, (pre φ (d + 1)).2)
  | .all φ, d => (true :: (pre φ (d + 1)).1, (pre φ (d + 1)).2)

theorem pre_qf : (φ : NF) → (d : Nat) → QF (pre φ d).2
  | .rel _ _, _ => trivial
  | .nrel _ _, _ => trivial
  | .svar _ _, _ => trivial
  | .nsvar _ _, _ => trivial
  | .eq _ _, _ => trivial
  | .neq _ _, _ => trivial
  | .and φ ψ, d => ⟨pre_qf φ d, qf_sh _ (pre_qf ψ d)⟩
  | .or φ ψ, d => ⟨pre_qf φ d, qf_sh _ (pre_qf ψ d)⟩
  | .ex φ, d => pre_qf φ (d + 1)
  | .all φ, d => pre_qf φ (d + 1)

theorem pre_sc : (φ : NF) → (d : Nat) → ScN φ d → ScN (pre φ d).2 (d + lenB (pre φ d).1)
  | .rel _ _, _, h => h
  | .nrel _ _, _, h => h
  | .svar _ _, _, h => h
  | .nsvar _ _, _, h => h
  | .eq _ _, _, h => h
  | .neq _ _, _, h => h
  | .and φ ψ, d, ⟨h1, h2⟩ => by
    show ScN (pre φ d).2 (d + lenB ((pre φ d).1 ++ (pre ψ d).1)) ∧ ScN (shN d (lenB (pre φ d).1) (pre ψ d).2) (d + lenB ((pre φ d).1 ++ (pre ψ d).1))
    rw [lenB_app]
    refine ⟨scN_mono (by rw [← Nat.add_assoc]; exact Nat.le_add_right _ _) _ (pre_sc φ d h1), ?_⟩
    have := sc_sh (k := lenB (pre φ d).1) _ _ (Nat.le_add_right d _) (pre_sc ψ d h2)
    rw [Nat.add_assoc, Nat.add_comm (lenB (pre ψ d).1)] at this
    exact this
  | .or φ ψ, d, ⟨h1, h2⟩ => by
    show ScN (pre φ d).2 (d + lenB ((pre φ d).1 ++ (pre ψ d).1)) ∧ ScN (shN d (lenB (pre φ d).1) (pre ψ d).2) (d + lenB ((pre φ d).1 ++ (pre ψ d).1))
    rw [lenB_app]
    refine ⟨scN_mono (by rw [← Nat.add_assoc]; exact Nat.le_add_right _ _) _ (pre_sc φ d h1), ?_⟩
    have := sc_sh (k := lenB (pre φ d).1) _ _ (Nat.le_add_right d _) (pre_sc ψ d h2)
    rw [Nat.add_assoc, Nat.add_comm (lenB (pre ψ d).1)] at this
    exact this
  | .ex φ, d, h => by
    have := pre_sc φ (d + 1) h
    show ScN (pre φ (d + 1)).2 (d + (lenB (pre φ (d + 1)).1 + 1))
    rw [← Nat.add_assoc, Nat.add_right_comm]; exact this
  | .all φ, d, h => by
    have := pre_sc φ (d + 1) h
    show ScN (pre φ (d + 1)).2 (d + (lenB (pre φ (d + 1)).1 + 1))
    rw [← Nat.add_assoc, Nat.add_right_comm]; exact this

theorem len_snoc' (u : List D) (x : D) : len (u ++ [x]) = len u + 1 := len_app u [x]

/-- a scoped NF formula is equivalent to its prenex form -/
theorem pre_ok (K : Classical') (J : SVal D) : (φ : NF) → (e : List D) → ScN φ (len e) →
    (QP (pre φ (len e)).1 e (fun e' => semN d0 I J e' (pre φ (len e)).2) ↔ semN d0 I J e φ)
  | .rel _ _, _, _ => Iff.rfl
  | .nrel _ _, _, _ => Iff.rfl
  | .svar _ _, _, _ => Iff.rfl
  | .nsvar _ _, _, _ => Iff.rfl
  | .eq _ _, _, _ => Iff.rfl
  | .neq _ _, _, _ => Iff.rfl
  | .and φ ψ, e, ⟨h1, h2⟩ => by
    show QP ((pre φ (len e)).1 ++ (pre ψ (len e)).1) e
      (fun e' => semN d0 I J e' (pre φ (len e)).2 ∧ semN d0 I J e' (shN (len e) (lenB (pre φ (len e)).1) (pre ψ (len e)).2)) ↔ _
    have sc1 := pre_sc φ (len e) h1
    refine (qp_app _ _ e _).trans ?_
    refine Iff.trans (qp_congr _ e _ (fun e1 => semN d0 I J e1 (pre φ (len e)).2 ∧ semN d0 I J e ψ) (fun u hu => ?_)) ?_
    · refine (qp_congr _ (e ++ u) _ (fun e2 => semN d0 I J (e ++ u) (pre φ (len e)).2 ∧
          semN d0 I J e2 (shN (len e) (len u) (pre ψ (len e)).2)) (fun v _ => ?_)).trans ?_
      · rw [hu]
        refine ⟨fun ⟨a, b⟩ => ⟨(loc_qf d0 I J (e ++ u) v _ (pre_qf φ _) (by rw [len_app, hu]; exact sc1)).1 a, b⟩,
          fun ⟨a, b⟩ => ⟨(loc_qf d0 I J (e ++ u) v _ (pre_qf φ _) (by rw [len_app, hu]; exact sc1)).2 a, b⟩⟩
      · refine (qp_const_and d0 _ _ _ _).trans ⟨fun ⟨a, b⟩ => ⟨a, ?_⟩, fun ⟨a, b⟩ => ⟨a, ?_⟩⟩
        · have b' := (qp_sh d0 I J (pre ψ (len e)).2 e u _ []).1 (by rw [app_nil']; exact b)
          rw [app_nil'] at b'
          exact (pre_ok K J ψ e h2).1 b'
        · have := (pre_ok K J ψ e h2).2 b
          have := (qp_sh d0 I J (pre ψ (len e)).2 e u _ []).2 (by rw [app_nil']; exact this)
          rw [app_nil'] at this
          exact this
    · refine Iff.trans (qp_congr _ e _ (fun e1 => semN d0 I J e ψ ∧ semN d0 I J e1 (pre φ (len e)).2)
        (fun _ _ => ⟨fun ⟨a, b⟩ => ⟨b, a⟩, fun ⟨a, b⟩ => ⟨b, a⟩⟩)) ?_
      refine (qp_const_and d0 _ _ _ _).trans ⟨fun ⟨a, b⟩ => ⟨(pre_ok K J φ e h1).1 b, a⟩,
        fun ⟨a, b⟩ => ⟨b, (pre_ok K J φ e h1).2 a⟩⟩
  | .or φ ψ, e, ⟨h1, h2⟩ => by
    show QP ((pre φ (len e)).1 ++ (pre ψ (len e)).1) e
      (fun e' => semN d0 I J e' (pre φ (len e)).2 ∨ semN d0 I J e' (shN (len e) (lenB (pre φ (len e)).1) (pre ψ (len e)).2)) ↔ _
    have sc1 := pre_sc φ (len e) h1
    refine (qp_app _ _ e _).trans ?_
    refine Iff.trans (qp_congr _ e _ (fun e1 => semN d0 I J e1 (pre φ (len e)).2 ∨ semN d0 I J e ψ) (fun u hu => ?_)) ?_
    · refine (qp_congr _ (e ++ u) _ (fun e2 => semN d0 I J (e ++ u) (pre φ (len e)).2 ∨
          semN d0 I J e2 (shN (len e) (len u) (pre ψ (len e)).2)) (fun v _ => ?_)).trans ?_
      · rw [hu]
        exact ⟨fun h => h.elim (fun a => Or.inl ((loc_qf d0 I J (e ++ u) v _ (pre_qf φ _) (by rw [len_app, hu]; exact sc1)).1 a)) Or.inr,
          fun h => h.elim (fun a => Or.inl ((loc_qf d0 I J (e ++ u) v _ (pre_qf φ _) (by rw [len_app, hu]; exact sc1)).2 a)) Or.inr⟩
      · refine (qp_const_or d0 K _ _ _ _).trans ⟨fun h => h.elim Or.inl (fun b => Or.inr ?_), fun h => h.elim Or.inl (fun b => Or.inr ?_)⟩
        · have b' := (qp_sh d0 I J (pre ψ (len e)).2 e u _ []).1 (by rw [app_nil']; exact b)
          rw [app_nil'] at b'
          exact (pre_ok K J ψ e h2).1 b'
        · have := (pre_ok K J ψ e h2).2 b
          have := (qp_sh d0 I J (pre ψ (len e)).2 e u _ []).2 (by rw [app_nil']; exact this)
          rw [app_nil'] at this
          exact this
    · refine Iff.trans (qp_congr _ e _ (fun e1 => semN d0 I J e ψ ∨ semN d0 I J e1 (pre φ (len e)).2)
        (fun _ _ => ⟨fun h => h.elim Or.inr Or.inl, fun h => h.elim Or.inr Or.inl⟩)) ?_
      refine (qp_const_or d0 K _ _ _ _).trans ⟨fun h => h.elim Or.inr (fun a => Or.inl ((pre_ok K J φ e h1).1 a)),
        fun h => h.elim (fun a => Or.inr ((pre_ok K J φ e h1).2 a)) Or.inl⟩
  | .ex φ, e, h => by
    show (∃ a, QP (pre φ (len e + 1)).1 (e ++ [a]) (fun e' => semN d0 I J e' (pre φ (len e + 1)).2)) ↔
      ∃ a, semN d0 I J (e ++ [a]) φ
    refine ⟨fun ⟨a, s⟩ => ⟨a, ?_⟩, fun ⟨a, s⟩ => ⟨a, ?_⟩⟩
    · have ih := pre_ok K J φ (e ++ [a]) (by rw [len_snoc']; exact h)
      rw [len_snoc'] at ih
      exact ih.1 s
    · have ih := pre_ok K J φ (e ++ [a]) (by rw [len_snoc']; exact h)
      rw [len_snoc'] at ih
      exact ih.2 s
  | .all φ, e, h => by
    show (∀ a, QP (pre φ (len e + 1)).1 (e ++ [a]) (fun e' => semN d0 I J e' (pre φ (len e + 1)).2)) ↔
      ∀ a, semN d0 I J (e ++ [a]) φ
    refine ⟨fun s a => ?_, fun s a => ?_⟩
    · have ih := pre_ok K J φ (e ++ [a]) (by rw [len_snoc']; exact h)
      rw [len_snoc'] at ih
      exact ih.1 (s a)
    · have ih := pre_ok K J φ (e ++ [a]) (by rw [len_snoc']; exact h)
      rw [len_snoc'] at ih
      exact ih.2 (s a)

end DE

end Found

#print axioms Found.DE.sh_ok
#print axioms Found.DE.loc_qf
#print axioms Found.DE.sc_sh
#print axioms Found.DE.qp_app
#print axioms Found.DE.qp_congr
#print axioms Found.DE.qp_const_and
#print axioms Found.DE.qp_const_or
#print axioms Found.DE.qp_sh
#print axioms Found.DE.pre_qf
#print axioms Found.DE.pre_sc
#print axioms Found.DE.pre_ok

found/DepE5.lean

lean_all/found/DepE5.lean

/-
  Line dependence logic ↔ ESO, part 5. found/DepE5 — quantifier blocks in
  team semantics (classical point):
  * a prefix of ∀/∃ over a team whose tuples satisfy the prefix (in the
    Tarski sense, QP) yields teams of tuples satisfying the matrix
    (prefix_intro), and conversely a team satisfying the prefix gives a
    team of extended tuples from which the prefix holds of every tuple
    of the original team (prefix_elim);
  * a block of k universal quantifiers duplicates the team over all
    k-tuples (allN_ok); a block of k existential quantifiers is the same as
    one function choosing k values (exN_intro, exN_elim).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.DepE4

namespace Found

namespace DE

open CP

variable {D : Type} (d0 : D) (I : Nat → List D → Prop)

def prefixD : List Bool → DF → DF
  | [], χ => χ
  | true :: p, χ => .all (prefixD p χ)
  | false :: p, χ => .ex (prefixD p χ)

theorem prefix_intro (K : Classical') (χ : DF) (P : List D → Prop) : (p : List Bool) → (T : Team D) → (d : Nat) →
    (∀ T' : Team D, (∀ v, T' v → P v ∧ len v = d + lenB p) → TS d0 I T' χ) →
    (∀ u, T u → QP p u P ∧ len u = d) → TS d0 I T (prefixD p χ)
  | [], T, d, hχ, hT => hχ T (fun v hv => ⟨(hT v hv).1, (hT v hv).2⟩)
  | true :: p, T, d, hχ, hT => prefix_intro K χ P p _ (d + 1)
      (fun T' h => hχ T' (fun v hv => ⟨(h v hv).1, by rw [(h v hv).2]; show d + 1 + lenB p = d + (lenB p + 1); rw [Nat.add_right_comm, Nat.add_assoc]⟩))
      (fun _ ⟨u, a, hu, e⟩ => by rw [e]; exact ⟨(hT u hu).1 a, by rw [len_snoc', (hT u hu).2]⟩)
  | false :: p, T, d, hχ, hT =>
    let F : List D → D := fun u => K.ch (fun a => (∃ b, QP p (u ++ [b]) P) → QP p (u ++ [a]) P)
      ((K.lem (∃ b, QP p (u ++ [b]) P)).elim (fun ⟨b, hb⟩ => ⟨b, fun _ => hb⟩) (fun hn => ⟨d0, fun h => absurd h hn⟩))
    ⟨F, prefix_intro K χ P p _ (d + 1)
      (fun T' h => hχ T' (fun v hv => ⟨(h v hv).1, by rw [(h v hv).2]; show d + 1 + lenB p = d + (lenB p + 1); rw [Nat.add_right_comm, Nat.add_assoc]⟩))
      (fun _ ⟨u, hu, e⟩ => by
        rw [e]
        exact ⟨K.ch_spec (fun a => (∃ b, QP p (u ++ [b]) P) → QP p (u ++ [a]) P) _ (hT u hu).1,
          by rw [len_snoc', (hT u hu).2]⟩)⟩

theorem prefix_elim (χ : DF) : (p : List Bool) → (T : Team D) → (d : Nat) → TS d0 I T (prefixD p χ) →
    (∀ u, T u → len u = d) →
    ∃ T' : Team D, TS d0 I T' χ ∧ (∀ v, T' v → len v = d + lenB p) ∧
      ∀ P : List D → Prop, (∀ v, T' v → P v) → ∀ u, T u → QP p u P
  | [], T, d, h, hl => ⟨T, h, hl, fun _ hP u hu => hP u hu⟩
  | true :: p, T, d, h, hl =>
    match prefix_elim χ p _ (d + 1) h (fun _ ⟨u, a, hu, e⟩ => by rw [e, len_snoc', hl u hu]) with
    | ⟨T', h1, h2, h3⟩ => ⟨T', h1, fun v hv => by rw [h2 v hv]; show d + 1 + lenB p = d + (lenB p + 1); rw [Nat.add_right_comm, Nat.add_assoc],
        fun P hP u hu a => h3 P hP _ ⟨u, a, hu, rfl⟩⟩
  | false :: p, T, d, ⟨F, h⟩, hl =>
    match prefix_elim χ p _ (d + 1) h (fun _ ⟨u, hu, e⟩ => by rw [e, len_snoc', hl u hu]) with
    | ⟨T', h1, h2, h3⟩ => ⟨T', h1, fun v hv => by rw [h2 v hv]; show d + 1 + lenB p = d + (lenB p + 1); rw [Nat.add_right_comm, Nat.add_assoc],
        fun P hP u hu => ⟨F u, h3 P hP _ ⟨u, hu, rfl⟩⟩⟩

/-! ## blocks -/

def allN : Nat → DF → DF
  | 0, ψ => ψ
  | k + 1, ψ => .all (allN k ψ)

def exN : Nat → DF → DF
  | 0, ψ => ψ
  | k + 1, ψ => .ex (exN k ψ)

def dupT (T : Team D) (k : Nat) : Team D := fun w => ∃ u z, T u ∧ len z = k ∧ w = u ++ z

theorem allN_ok (ψ : DF) : (k : Nat) → (T : Team D) → (TS d0 I T (allN k ψ) ↔ TS d0 I (dupT T k) ψ)
  | 0, T => ts_congr d0 I ψ T (dupT T 0) (fun w => ⟨fun h => ⟨w, [], h, rfl, (app_nil' w).symm⟩,
      fun ⟨u, z, hu, hz, e⟩ => by
        cases z with
        | nil => rw [e, app_nil']; exact hu
        | cons _ _ => exact Nat.noConfusion hz⟩)
  | k + 1, T => (allN_ok ψ k _).trans (ts_congr d0 I ψ (dupT (fun u' => ∃ u a, T u ∧ u' = u ++ [a]) k) (dupT T (k + 1))
      (fun w => ⟨fun ⟨u1, z, ⟨u, a, hu, e1⟩, hz, e⟩ =>
        ⟨u, a :: z, hu, by show len z + 1 = k + 1; rw [hz], by rw [e, e1, app_assoc'']; rfl⟩,
      fun ⟨u, z, hu, hz, e⟩ => by
        cases z with
        | nil => exact Nat.noConfusion hz
        | cons a z => exact ⟨u ++ [a], z, ⟨u, a, hu, rfl⟩, Nat.succ.inj hz, by rw [e, app_assoc'']; rfl⟩⟩))

def img (T : Team D) (G : List D → List D) : Team D := fun x => ∃ w, T w ∧ x = w ++ G w

/-- the first L entries -/
def firstL : Nat → List D → List D
  | 0, _ => []
  | _ + 1, [] => []
  | L + 1, a :: u => a :: firstL L u

theorem firstL_app : (w v : List D) → firstL (len w) (w ++ v) = w
  | [], v => by cases v <;> rfl
  | a :: w, v => congrArg (a :: ·) (firstL_app w v)

def hd' : List D → D
  | [] => d0
  | a :: _ => a

def tl' : List D → List D
  | [] => []
  | _ :: u => u

theorem hd_tl {l : List D} {k : Nat} (h : len l = k + 1) : hd' d0 l :: tl' l = l := by
  cases l with
  | nil => exact Nat.noConfusion h
  | cons _ _ => rfl

theorem exN_elim (ψ : DF) : (k : Nat) → (T : Team D) → TS d0 I T (exN k ψ) →
    ∃ G : List D → List D, (∀ w, len (G w) = k) ∧ TS d0 I (img T G) ψ
  | 0, T, h => ⟨fun _ => [], fun _ => rfl, (ts_congr d0 I ψ T (img T (fun _ => [])) (fun x => ⟨fun h => ⟨x, h, (app_nil' x).symm⟩,
      fun ⟨w, hw, e⟩ => by rw [e, app_nil']; exact hw⟩)).1 h⟩
  | k + 1, T, ⟨F, h⟩ => match exN_elim ψ k _ h with
    | ⟨G1, hl, h1⟩ => ⟨fun w => F w :: G1 (w ++ [F w]), fun w => by show len _ + 1 = k + 1; rw [hl],
        (ts_congr d0 I ψ (img (fun u' => ∃ u, T u ∧ u' = u ++ [F u]) G1) (img T (fun w => F w :: G1 (w ++ [F w])))
          (fun x => ⟨fun ⟨x1, ⟨w, hw, e1⟩, e⟩ => ⟨w, hw, by rw [e, e1, app_assoc'']; rfl⟩,
            fun ⟨w, hw, e⟩ => ⟨w ++ [F w], ⟨w, hw, rfl⟩, by rw [e, app_assoc'']; rfl⟩⟩)).1 h1⟩

theorem exN_intro (ψ : DF) : (k : Nat) → (T : Team D) → (L : Nat) → (∀ w, T w → len w = L) →
    (G : List D → List D) → (∀ w, len (G w) = k) → TS d0 I (img T G) ψ → TS d0 I T (exN k ψ)
  | 0, T, _, _, G, hG, h => (ts_congr d0 I ψ (img T G) T (fun x => ⟨fun ⟨w, hw, e⟩ => by
        have : G w = [] := by
          have := hG w
          cases e' : G w with
          | nil => rfl
          | cons _ _ => rw [e'] at this; exact Nat.noConfusion this
        rw [e, this, app_nil']; exact hw,
      fun h => ⟨x, h, by
        have : G x = [] := by
          have := hG x
          cases e' : G x with
          | nil => rfl
          | cons _ _ => rw [e'] at this; exact Nat.noConfusion this
        rw [this, app_nil']⟩⟩)).1 h
  | k + 1, T, L, hl, G, hG, h => by
    let F : List D → D := fun w => hd' d0 (G (firstL L w))
    let T1 : Team D := fun u' => ∃ u, T u ∧ u' = u ++ [F u]
    let G' : List D → List D := fun x => tl' (G (firstL L x))
    have fw : ∀ w, T w → firstL L w = w := fun w hw => by
      have := firstL_app w []; rw [app_nil'] at this; rw [← hl w hw]; exact this
    have fw1 : ∀ w a, T w → firstL L (w ++ [a]) = w := fun w a hw => by rw [← hl w hw, firstL_app]
    have key : ∀ w, T w → w ++ [F w] ++ G' (w ++ [F w]) = w ++ G w := fun w hw => by
      show w ++ [hd' d0 (G (firstL L w))] ++ tl' (G (firstL L (w ++ [hd' d0 (G (firstL L w))]))) = w ++ G w
      rw [fw1 w _ hw, fw w hw, app_assoc'']
      exact congrArg (w ++ ·) (hd_tl d0 (hG w))
    refine ⟨F, exN_intro ψ k T1 (L + 1) (fun _ ⟨w, hw, e⟩ => by rw [e, len_snoc', hl w hw]) G'
      (fun x => by
        show len (tl' (G (firstL L x))) = k
        have := hG (firstL L x)
        cases e : G (firstL L x) with
        | nil => rw [e] at this; exact Nat.noConfusion this
        | cons _ t => rw [e] at this; exact Nat.succ.inj this) ?_⟩
    exact (ts_congr d0 I ψ (img T G) (img T1 G') (fun x => ⟨fun ⟨w, hw, e⟩ => ⟨w ++ [F w], ⟨w, hw, rfl⟩, by rw [e, key w hw]⟩,
      fun ⟨x1, ⟨w, hw, e1⟩, e⟩ => ⟨w, hw, by rw [e, e1, key w hw]⟩⟩)).1 h

end DE

end Found

#print axioms Found.DE.prefix_intro
#print axioms Found.DE.prefix_elim
#print axioms Found.DE.allN_ok
#print axioms Found.DE.exN_elim
#print axioms Found.DE.exN_intro

found/DepE6.lean

lean_all/found/DepE6.lean

/-
  Line dependence logic ↔ ESO, part 6. found/DepE6 — bookkeeping for ESO →
  D: the occurrences of relation variables in a quantifier-free matrix;
  replacing occurrence number c by the equation a_c = b_c (resp. a_c ≠ b_c)
  between two fresh positions preserves truth when each equation holds
  exactly when the relation holds at the occurrence's arguments (rep_ok);
  formulas for equal and different tuples; reading blocks of an extended
  tuple u ++ z₀ ++ … ++ z_{q−1} ++ g (get_zcat).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.DepE5

namespace Found

namespace DE

open CP

variable {D : Type} (d0 : D) (I : Nat → List D → Prop)

/-! ## occurrences and their replacement -/

def occ : NF → List (Nat × List Nat)
  | .svar i is => [(i, is)]
  | .nsvar i is => [(i, is)]
  | .and a b => occ a ++ occ b
  | .or a b => occ a ++ occ b
  | .rel _ _ => []
  | .nrel _ _ => []
  | .eq _ _ => []
  | .neq _ _ => []
  | .ex _ => []
  | .all _ => []

def nthP : List (Nat × List Nat) → Nat → Nat × List Nat
  | [], _ => (0, [])
  | x :: _, 0 => x
  | _ :: l, j + 1 => nthP l j

theorem nthP_app_l : (a b : List (Nat × List Nat)) → (j : Nat) → j < len a → nthP (a ++ b) j = nthP a j
  | [], _, _, h => absurd h (Nat.not_lt_zero _)
  | _ :: _, _, 0, _ => rfl
  | _ :: a, b, j + 1, h => nthP_app_l a b j (Nat.lt_of_succ_lt_succ h)

theorem nthP_app_r : (a b : List (Nat × List Nat)) → (j : Nat) → nthP (a ++ b) (len a + j) = nthP b j
  | [], _, j => by show nthP _ (0 + j) = _; rw [Nat.zero_add]; rfl
  | x :: a, b, j => by
    show nthP (x :: (a ++ b)) (len a + 1 + j) = nthP b j
    rw [Nat.add_right_comm]
    exact nthP_app_r a b j

def rep (B : Nat) : NF → Nat → NF × Nat
  | .svar _ _, c => (.eq (B + 2 * c) (B + 2 * c + 1), c + 1)
  | .nsvar _ _, c => (.neq (B + 2 * c) (B + 2 * c + 1), c + 1)
  | .and a b, c => (.and (rep B a c).1 (rep B b (rep B a c).2).1, (rep B b (rep B a c).2).2)
  | .or a b, c => (.or (rep B a c).1 (rep B b (rep B a c).2).1, (rep B b (rep B a c).2).2)
  | .rel r is, c => (.rel r is, c)
  | .nrel r is, c => (.nrel r is, c)
  | .eq i j, c => (.eq i j, c)
  | .neq i j, c => (.neq i j, c)
  | .ex φ, c => (.ex φ, c)
  | .all φ, c => (.all φ, c)

theorem rep_cnt (B : Nat) : (M : NF) → (c : Nat) → (rep B M c).2 = c + len (occ M)
  | .svar _ _, _ => rfl
  | .nsvar _ _, _ => rfl
  | .and a b, c => by
    show (rep B b (rep B a c).2).2 = c + len (occ a ++ occ b)
    rw [rep_cnt B b, rep_cnt B a, len_app, Nat.add_assoc]
  | .or a b, c => by
    show (rep B b (rep B a c).2).2 = c + len (occ a ++ occ b)
    rw [rep_cnt B b, rep_cnt B a, len_app, Nat.add_assoc]
  | .rel _ _, _ => rfl
  | .nrel _ _, _ => rfl
  | .eq _ _, _ => rfl
  | .neq _ _, _ => rfl
  | .ex _, _ => rfl
  | .all _, _ => rfl

theorem rep_noS (B : Nat) : (M : NF) → QF M → (c : Nat) → NoS (rep B M c).1
  | .svar _ _, _, _ => trivial
  | .nsvar _ _, _, _ => trivial
  | .and a b, ⟨qa, qb⟩, c => ⟨rep_noS B a qa c, rep_noS B b qb _⟩
  | .or a b, ⟨qa, qb⟩, c => ⟨rep_noS B a qa c, rep_noS B b qb _⟩
  | .rel _ _, _, _ => trivial
  | .nrel _ _, _, _ => trivial
  | .eq _ _, _, _ => trivial
  | .neq _ _, _, _ => trivial

/-- the replacement is faithful when every equation tracks its relation -/
theorem rep_ok (J J' : SVal D) (x : List D) (B : Nat) : (M : NF) → QF M → (c : Nat) →
    (∀ j, j < len (occ M) → (get d0 x (B + 2 * (c + j)) = get d0 x (B + 2 * (c + j) + 1) ↔
      J (nthP (occ M) j).1 (args d0 x (nthP (occ M) j).2))) →
    (semN d0 I J' x (rep B M c).1 ↔ semN d0 I J x M)
  | .svar _ _, _, c, h => by
    have := h 0 (Nat.succ_pos 0)
    rw [Nat.add_zero] at this
    exact this
  | .nsvar _ _, _, c, h => by
    have := h 0 (Nat.succ_pos 0)
    rw [Nat.add_zero] at this
    exact ⟨fun ne e => ne (this.2 e), fun ne e => ne (this.1 e)⟩
  | .and a b, ⟨qa, qb⟩, c, h => by
    have ha : ∀ j, j < len (occ a) → (get d0 x (B + 2 * (c + j)) = get d0 x (B + 2 * (c + j) + 1) ↔
        J (nthP (occ a) j).1 (args d0 x (nthP (occ a) j).2)) := fun j hj => by
      have := h j (by show j < len (occ a ++ occ b); rw [len_app]; exact Nat.lt_of_lt_of_le hj (Nat.le_add_right _ _))
      have e := nthP_app_l (occ a) (occ b) j hj
      have e' : nthP (occ (.and a b)) j = nthP (occ a) j := e
      rw [e'] at this; exact this
    have hb : ∀ j, j < len (occ b) → (get d0 x (B + 2 * ((rep B a c).2 + j)) = get d0 x (B + 2 * ((rep B a c).2 + j) + 1) ↔
        J (nthP (occ b) j).1 (args d0 x (nthP (occ b) j).2)) := fun j hj => by
      have := h (len (occ a) + j) (by show _ < len (occ a ++ occ b); rw [len_app]; exact Nat.add_lt_add_left hj _)
      have e := nthP_app_r (occ a) (occ b) j
      have e' : nthP (occ (.and a b)) (len (occ a) + j) = nthP (occ b) j := e
      rw [e', ← Nat.add_assoc] at this
      rw [rep_cnt]; exact this
    exact ⟨fun ⟨x1, x2⟩ => ⟨(rep_ok J J' x B a qa c ha).1 x1, (rep_ok J J' x B b qb _ hb).1 x2⟩,
      fun ⟨x1, x2⟩ => ⟨(rep_ok J J' x B a qa c ha).2 x1, (rep_ok J J' x B b qb _ hb).2 x2⟩⟩
  | .or a b, ⟨qa, qb⟩, c, h => by
    have ha : ∀ j, j < len (occ a) → (get d0 x (B + 2 * (c + j)) = get d0 x (B + 2 * (c + j) + 1) ↔
        J (nthP (occ a) j).1 (args d0 x (nthP (occ a) j).2)) := fun j hj => by
      have := h j (by show j < len (occ a ++ occ b); rw [len_app]; exact Nat.lt_of_lt_of_le hj (Nat.le_add_right _ _))
      have e := nthP_app_l (occ a) (occ b) j hj
      have e' : nthP (occ (.or a b)) j = nthP (occ a) j := e
      rw [e'] at this; exact this
    have hb : ∀ j, j < len (occ b) → (get d0 x (B + 2 * ((rep B a c).2 + j)) = get d0 x (B + 2 * ((rep B a c).2 + j) + 1) ↔
        J (nthP (occ b) j).1 (args d0 x (nthP (occ b) j).2)) := fun j hj => by
      have := h (len (occ a) + j) (by show _ < len (occ a ++ occ b); rw [len_app]; exact Nat.add_lt_add_left hj _)
      have e := nthP_app_r (occ a) (occ b) j
      have e' : nthP (occ (.or a b)) (len (occ a) + j) = nthP (occ b) j := e
      rw [e', ← Nat.add_assoc] at this
      rw [rep_cnt]; exact this
    exact ⟨fun hh => hh.elim (fun x1 => Or.inl ((rep_ok J J' x B a qa c ha).1 x1)) (fun x2 => Or.inr ((rep_ok J J' x B b qb _ hb).1 x2)),
      fun hh => hh.elim (fun x1 => Or.inl ((rep_ok J J' x B a qa c ha).2 x1)) (fun x2 => Or.inr ((rep_ok J J' x B b qb _ hb).2 x2))⟩
  | .rel _ _, _, _, _ => Iff.rfl
  | .nrel _ _, _, _, _ => Iff.rfl
  | .eq _ _, _, _, _ => Iff.rfl
  | .neq _ _, _, _, _ => Iff.rfl

/-! ## tuple formulas -/

def eqTup : List Nat → List Nat → NF
  | [], [] => .eq 0 0
  | [], _ :: _ => .eq 0 0
  | _ :: _, [] => .eq 0 0
  | x :: xs, y :: ys => .and (.eq x y) (eqTup xs ys)

def neqTup : List Nat → List Nat → NF
  | [], [] => .neq 0 0
  | [], _ :: _ => .neq 0 0
  | _ :: _, [] => .neq 0 0
  | x :: xs, y :: ys => .or (.neq x y) (neqTup xs ys)

theorem sem_eqTup (J : SVal D) (x : List D) : (xs ys : List Nat) → len xs = len ys →
    (semN d0 I J x (eqTup xs ys) ↔ args d0 x xs = args d0 x ys)
  | [], [], _ => ⟨fun _ => rfl, fun _ => rfl⟩
  | [], _ :: _, h => Nat.noConfusion h
  | _ :: _, [], h => Nat.noConfusion h
  | a :: xs, b :: ys, h => ⟨fun ⟨e, r⟩ => by
        show get d0 x a :: args d0 x xs = get d0 x b :: args d0 x ys
        rw [e, (sem_eqTup J x xs ys (Nat.succ.inj h)).1 r],
      fun e => ⟨(List.cons.inj e).1, (sem_eqTup J x xs ys (Nat.succ.inj h)).2 (List.cons.inj e).2⟩⟩

theorem sem_neqTup (K : Classical') (J : SVal D) (x : List D) : (xs ys : List Nat) → len xs = len ys →
    (semN d0 I J x (neqTup xs ys) ↔ args d0 x xs ≠ args d0 x ys)
  | [], [], _ => ⟨fun h => absurd rfl h, fun h => absurd rfl h⟩
  | [], _ :: _, h => Nat.noConfusion h
  | _ :: _, [], h => Nat.noConfusion h
  | a :: xs, b :: ys, h => ⟨fun hh e => hh.elim (fun ne => ne (List.cons.inj e).1)
        (fun r => (sem_neqTup K J x xs ys (Nat.succ.inj h)).1 r (List.cons.inj e).2),
      fun ne => (K.lem (get d0 x a = get d0 x b)).elim
        (fun e => Or.inr ((sem_neqTup K J x xs ys (Nat.succ.inj h)).2 (fun e' => ne (by
          show get d0 x a :: args d0 x xs = get d0 x b :: args d0 x ys
          rw [e, e']))))
        (fun ne' => Or.inl ne')⟩

theorem qf_eqTup : (xs ys : List Nat) → QF (eqTup xs ys) ∧ NoS (eqTup xs ys)
  | [], [] => ⟨trivial, trivial⟩
  | [], _ :: _ => ⟨trivial, trivial⟩
  | _ :: _, [] => ⟨trivial, trivial⟩
  | _ :: xs, _ :: ys => ⟨⟨trivial, (qf_eqTup xs ys).1⟩, ⟨trivial, (qf_eqTup xs ys).2⟩⟩

theorem qf_neqTup : (xs ys : List Nat) → QF (neqTup xs ys) ∧ NoS (neqTup xs ys)
  | [], [] => ⟨trivial, trivial⟩
  | [], _ :: _ => ⟨trivial, trivial⟩
  | _ :: _, [] => ⟨trivial, trivial⟩
  | _ :: xs, _ :: ys => ⟨⟨trivial, (qf_neqTup xs ys).1⟩, ⟨trivial, (qf_neqTup xs ys).2⟩⟩

def bigAndN (f : Nat → NF) : Nat → NF
  | 0 => .eq 0 0
  | n + 1 => .and (bigAndN f n) (f n)

def bigOrN (f : Nat → NF) : Nat → NF
  | 0 => .neq 0 0
  | n + 1 => .or (bigOrN f n) (f n)

theorem sem_bigAndN (J : SVal D) (x : List D) (f : Nat → NF) : (n : Nat) →
    (semN d0 I J x (bigAndN f n) ↔ ∀ j, j < n → semN d0 I J x (f j))
  | 0 => ⟨fun _ _ h => absurd h (Nat.not_lt_zero _), fun _ => rfl⟩
  | n + 1 => ⟨fun ⟨a, b⟩ j hj => (Mk'.le_cases (Nat.le_of_lt_succ hj)).elim (fun e => e ▸ b) ((sem_bigAndN J x f n).1 a j),
      fun h => ⟨(sem_bigAndN J x f n).2 (fun j hj => h j (Nat.lt_trans hj (Nat.lt_succ_self n))), h n (Nat.lt_succ_self n)⟩⟩
where
  Mk'.le_cases {a b : Nat} (h : a ≤ b) : a = b ∨ a < b := by
    induction h with
    | refl => exact Or.inl rfl
    | step h _ => exact Or.inr (Nat.lt_succ_of_le h)

theorem le_cases' {a b : Nat} (h : a ≤ b) : a = b ∨ a < b := sem_bigAndN.Mk'.le_cases h

theorem sem_bigOrN (J : SVal D) (x : List D) (f : Nat → NF) : (n : Nat) →
    (semN d0 I J x (bigOrN f n) ↔ ∃ j, j < n ∧ semN d0 I J x (f j))
  | 0 => ⟨fun h => absurd rfl h, fun ⟨_, h, _⟩ => absurd h (Nat.not_lt_zero _)⟩
  | n + 1 => ⟨fun h => h.elim (fun a => match (sem_bigOrN J x f n).1 a with
        | ⟨j, hj, s⟩ => ⟨j, Nat.lt_trans hj (Nat.lt_succ_self n), s⟩) (fun b => ⟨n, Nat.lt_succ_self n, b⟩),
      fun ⟨j, hj, s⟩ => (le_cases' (Nat.le_of_lt_succ hj)).elim (fun e => Or.inr (e ▸ s))
        (fun hl => Or.inl ((sem_bigOrN J x f n).2 ⟨j, hl, s⟩))⟩

theorem qfn_bigAndN (f : Nat → NF) (hf : ∀ j, QF (f j) ∧ NoS (f j)) : (n : Nat) → QF (bigAndN f n) ∧ NoS (bigAndN f n)
  | 0 => ⟨trivial, trivial⟩
  | n + 1 => ⟨⟨(qfn_bigAndN f hf n).1, (hf n).1⟩, ⟨(qfn_bigAndN f hf n).2, (hf n).2⟩⟩

theorem qfn_bigOrN (f : Nat → NF) (hf : ∀ j, QF (f j) ∧ NoS (f j)) : (n : Nat) → QF (bigOrN f n) ∧ NoS (bigOrN f n)
  | 0 => ⟨trivial, trivial⟩
  | n + 1 => ⟨⟨(qfn_bigOrN f hf n).1, (hf n).1⟩, ⟨(qfn_bigOrN f hf n).2, (hf n).2⟩⟩

/-! ## blocks of an extended tuple -/

def padC : List D → Nat → List D
  | _, 0 => []
  | [], A + 1 => d0 :: padC [] A
  | a :: l, A + 1 => a :: padC l A

theorem len_padC : (l : List D) → (A : Nat) → len (padC d0 l A) = A
  | _, 0 => rfl
  | [], A + 1 => congrArg (· + 1) (len_padC [] A)
  | _ :: l, A + 1 => congrArg (· + 1) (len_padC l A)

theorem get_padC : (l : List D) → (A j : Nat) → j < len l → j < A → get d0 (padC d0 l A) j = get d0 l j
  | [], _, _, h, _ => absurd h (Nat.not_lt_zero _)
  | _ :: _, 0, _, _, h => absurd h (Nat.not_lt_zero _)
  | _ :: _, _ + 1, 0, _, _ => rfl
  | _ :: l, A + 1, j + 1, h1, h2 => get_padC l A j (Nat.lt_of_succ_lt_succ h1) (Nat.lt_of_succ_lt_succ h2)

def zcat (bl : Nat → List D) : Nat → List D
  | 0 => []
  | q + 1 => zcat bl q ++ bl q

theorem len_zcat (bl : Nat → List D) (A : Nat) (hb : ∀ o, len (bl o) = A) : (q : Nat) → len (zcat bl q) = q * A
  | 0 => by rw [Nat.zero_mul]; rfl
  | q + 1 => by show len (zcat bl q ++ bl q) = (q + 1) * A; rw [len_app, len_zcat bl A hb q, hb q, Nat.succ_mul]

theorem get_zcat (bl : Nat → List D) (A : Nat) (hb : ∀ o, len (bl o) = A) : (q : Nat) → (rest : List D) →
    (o j : Nat) → o < q → j < A → get d0 (zcat bl q ++ rest) (o * A + j) = get d0 (bl o) j
  | 0, _, _, _, h, _ => absurd h (Nat.not_lt_zero _)
  | q + 1, rest, o, j, ho, hj => by
    show get d0 ((zcat bl q ++ bl q) ++ rest) _ = _
    rw [app_assoc'']
    cases le_cases' (Nat.le_of_lt_succ ho) with
    | inl e =>
      rw [e, ← len_zcat bl A hb q, get_app_r, get_app_l d0 _ _ j (by rw [hb q]; exact hj)]
    | inr hl => exact get_zcat bl A hb q _ o j hl hj

/-- reading consecutive positions -/
theorem args_range_eq (x : List D) : (m b : Nat) → (l : List D) → len l = m →
    (∀ j, j < m → get d0 x (b + j) = get d0 l j) → args d0 x (range' b m) = l
  | 0, _, [], _, _ => rfl
  | 0, _, _ :: _, h, _ => Nat.noConfusion h
  | _ + 1, _, [], h, _ => Nat.noConfusion h
  | m + 1, b, a :: l, h, hg => by
    show get d0 x b :: args d0 x (range' (b + 1) m) = a :: l
    rw [show get d0 x b = a from hg 0 (Nat.succ_pos m),
      args_range_eq x m (b + 1) l (Nat.succ.inj h) (fun j hj => by
        rw [Nat.add_assoc, Nat.add_comm 1 j]; exact hg (j + 1) (Nat.succ_lt_succ hj))]

end DE

end Found

#print axioms Found.DE.rep_cnt
#print axioms Found.DE.rep_noS
#print axioms Found.DE.rep_ok
#print axioms Found.DE.sem_eqTup
#print axioms Found.DE.sem_neqTup
#print axioms Found.DE.sem_bigOrN
#print axioms Found.DE.get_zcat
#print axioms Found.DE.args_range_eq
#print axioms Found.DE.get_padC

found/DepE7.lean

lean_all/found/DepE7.lean

/-
  Line dependence logic ↔ ESO, part 7. found/DepE7 — ESO → dependence
  logic over domains with two distinct elements d0 ≠ d1: for a prenex
  matrix M with q occurrences of relation variables, the sentence
  φ₂ = prefix ∀z̄₀…∀z̄_{q−1} ∃a₀∃b₀…∃a_{q−1}∃b_{q−1}
        (⋀ =(z̄_o, a_o) ∧ =(z̄_o, b_o)) ∧ consistency ∧ (some z̄_o ≠ ī_o ∨ M[X ī_o ↦ a_o = b_o])
  holds exactly when some interpretation of the relation variables makes
  the prefix over M true (phi2_intro: a relation gives a_o = d0, b_o = d0
  or d1; phi2_elim: the values a_o = b_o at the blocks z̄_o define a
  relation, well defined by the dependence atoms and the consistency
  clause).

  Lean 4 core only, hand-written proofs, no axioms (classical point K).
-/
import found.DepE6

namespace Found

namespace DE

open CP

variable {D : Type} (d0 : D) (I : Nat → List D → Prop)

def beqN : Nat → Nat → Bool
  | 0, 0 => true
  | 0, _ + 1 => false
  | _ + 1, 0 => false
  | a + 1, b + 1 => beqN a b

theorem beqN_t : (a b : Nat) → beqN a b = true → a = b
  | 0, 0, _ => rfl
  | 0, _ + 1, h => Bool.noConfusion h
  | _ + 1, 0, h => Bool.noConfusion h
  | a + 1, b + 1, h => congrArg (· + 1) (beqN_t a b h)

theorem beqN_self : (a : Nat) → beqN a a = true
  | 0 => rfl
  | a + 1 => beqN_self a

def maxN (a b : Nat) : Nat := cond (Nat.ble a b) b a

theorem le_maxN_l (a b : Nat) : a ≤ maxN a b := by
  unfold maxN
  cases e : Nat.ble a b with
  | true => exact Nat.le_of_ble_eq_true e
  | false => exact Nat.le_refl a

theorem le_maxN_r (a b : Nat) : b ≤ maxN a b := by
  unfold maxN
  cases e : Nat.ble a b with
  | true => exact Nat.le_refl b
  | false =>
    cases Nat.lt_or_ge a b with
    | inl hl => exact absurd (Nat.ble_eq_true_of_le (Nat.le_of_lt hl)) (by rw [e]; exact fun h => Bool.noConfusion h)
    | inr hg => exact hg

def amax : List (Nat × List Nat) → Nat
  | [] => 0
  | x :: l => maxN (len x.2) (amax l)

theorem amax_ge : (l : List (Nat × List Nat)) → (j : Nat) → j < len l → len (nthP l j).2 ≤ amax l
  | [], _, h => absurd h (Nat.not_lt_zero _)
  | x :: _, 0, _ => le_maxN_l (len x.2) _
  | _ :: l, j + 1, h => Nat.le_trans (amax_ge l j (Nat.lt_of_succ_lt_succ h)) (le_maxN_r _ _)

/-! ## the sentence -/

section
variable (n : Nat) (M : NF)

def qO : Nat := len (occ M)
def AO : Nat := amax (occ M)
def BO : Nat := n + qO M * AO M
def iO (o : Nat) : Nat := (nthP (occ M) o).1
def isO (o : Nat) : List Nat := (nthP (occ M) o).2
def zb (o : Nat) : List Nat := range' (n + o * AO M) (len (isO M o))
def aL (o : Nat) : Nat := BO n M + 2 * o
def bL (o : Nat) : Nat := BO n M + 2 * o + 1

def depsD : Nat → DF
  | 0 => .eq 0 0
  | o + 1 => .and (depsD o) (.and (.dep (zb n M o) (aL n M o)) (.dep (zb n M o) (bL n M o)))

def cpair (o o' : Nat) : NF :=
  cond (beqN (iO M o) (iO M o') && beqN (len (isO M o)) (len (isO M o')))
    (.or (neqTup (zb n M o) (zb n M o')) (.and (.eq (aL n M o) (aL n M o')) (.eq (bL n M o) (bL n M o'))))
    (.eq 0 0)

def consF : NF := bigAndN (fun o => bigAndN (fun o' => cpair n M o o') (qO M)) (qO M)
def escF : NF := bigOrN (fun o => neqTup (zb n M o) (isO M o)) (qO M)
def flatF : NF := .and (consF n M) (.or (escF n M) (rep (BO n M) M 0).1)
def bodyD : DF := .and (depsD n M (qO M)) (toD (flatF n M))

def phi2 (p : List Bool) : DF := prefixD p (allN (qO M * AO M) (exN (2 * qO M) (bodyD n M)))

end

/-! ## helpers -/

theorem len_range' : (b m : Nat) → len (range' b m) = m
  | _, 0 => rfl
  | b, m + 1 => congrArg (· + 1) (len_range' (b + 1) m)

theorem allLt_range' : (b m N : Nat) → b + m ≤ N → AllLt (range' b m) N
  | _, 0, _, _ => trivial
  | b, m + 1, N, h => ⟨Nat.lt_of_lt_of_le (Nat.lt_add_of_pos_right (Nat.succ_pos m)) h,
      allLt_range' (b + 1) m N (by rw [Nat.add_assoc, Nat.add_comm 1 m]; exact h)⟩

theorem occ_sc : (M : NF) → (n : Nat) → QF M → ScN M n → ∀ j, j < len (occ M) → AllLt (nthP (occ M) j).2 n
  | .svar _ _, _, _, h, 0, _ => h
  | .svar _ _, _, _, _, _ + 1, hj => absurd (Nat.lt_of_succ_lt_succ hj) (Nat.not_lt_zero _)
  | .nsvar _ _, _, _, h, 0, _ => h
  | .nsvar _ _, _, _, _, _ + 1, hj => absurd (Nat.lt_of_succ_lt_succ hj) (Nat.not_lt_zero _)
  | .and a b, n, ⟨qa, qb⟩, ⟨sa, sb⟩, j, hj => by
    cases Nat.lt_or_ge j (len (occ a)) with
    | inl hl => show AllLt (nthP (occ a ++ occ b) j).2 n; rw [nthP_app_l _ _ j hl]; exact occ_sc a n qa sa j hl
    | inr hg =>
      obtain ⟨k, hk⟩ := le_dest_ hg
      show AllLt (nthP (occ a ++ occ b) j).2 n
      have hj' : j < len (occ a ++ occ b) := hj
      rw [len_app, hk] at hj'
      rw [hk, nthP_app_r]; exact occ_sc b n qb sb k (Nat.lt_of_add_lt_add_left hj')
  | .or a b, n, ⟨qa, qb⟩, ⟨sa, sb⟩, j, hj => by
    cases Nat.lt_or_ge j (len (occ a)) with
    | inl hl => show AllLt (nthP (occ a ++ occ b) j).2 n; rw [nthP_app_l _ _ j hl]; exact occ_sc a n qa sa j hl
    | inr hg =>
      obtain ⟨k, hk⟩ := le_dest_ hg
      show AllLt (nthP (occ a ++ occ b) j).2 n
      have hj' : j < len (occ a ++ occ b) := hj
      rw [len_app, hk] at hj'
      rw [hk, nthP_app_r]; exact occ_sc b n qb sb k (Nat.lt_of_add_lt_add_left hj')
  | .rel _ _, _, _, _, _, hj => absurd hj (Nat.not_lt_zero _)
  | .nrel _ _, _, _, _, _, hj => absurd hj (Nat.not_lt_zero _)
  | .eq _ _, _, _, _, _, hj => absurd hj (Nat.not_lt_zero _)
  | .neq _ _, _, _, _, _, hj => absurd hj (Nat.not_lt_zero _)
where
  le_dest_ {a b : Nat} (h : a ≤ b) : ∃ d, b = a + d := by
    induction h with
    | refl => exact ⟨0, rfl⟩
    | step _ ih => match ih with
      | ⟨d, e⟩ => exact ⟨d + 1, by rw [e]; rfl⟩

theorem zb_lt (n : Nat) (M : NF) (o : Nat) (ho : o < qO M) : AllLt (zb n M o) (BO n M) := by
  apply allLt_range'
  have h1 : len (isO M o) ≤ AO M := amax_ge (occ M) o ho
  have h2 : (o + 1) * AO M ≤ qO M * AO M := Nat.mul_le_mul_right _ (Nat.succ_le_of_lt ho)
  rw [Nat.succ_mul] at h2
  show n + o * AO M + len (isO M o) ≤ n + qO M * AO M
  rw [Nat.add_assoc]
  exact Nat.add_le_add_left (Nat.le_trans (Nat.add_le_add_left h1 _) h2) n

theorem ts_depsD (n : Nat) (M : NF) (T : Team D) : (k : Nat) → (TS d0 I T (depsD n M k) ↔
    ∀ o, o < k → TS d0 I T (.dep (zb n M o) (aL n M o)) ∧ TS d0 I T (.dep (zb n M o) (bL n M o)))
  | 0 => ⟨fun _ _ h => absurd h (Nat.not_lt_zero _), fun _ _ _ => rfl⟩
  | k + 1 => ⟨fun ⟨a, b⟩ o ho => (le_cases' (Nat.le_of_lt_succ ho)).elim (fun e => e ▸ b) ((ts_depsD n M T k).1 a o),
      fun h => ⟨(ts_depsD n M T k).2 (fun o ho => h o (Nat.lt_trans ho (Nat.lt_succ_self k))), h k (Nat.lt_succ_self k)⟩⟩

theorem nos_cpair (n : Nat) (M : NF) (o o' : Nat) : QF (cpair n M o o') ∧ NoS (cpair n M o o') := by
  unfold cpair
  cases (beqN (iO M o) (iO M o') && beqN (len (isO M o)) (len (isO M o'))) with
  | true => exact ⟨⟨(qf_neqTup _ _).1, trivial, trivial⟩, ⟨(qf_neqTup _ _).2, trivial, trivial⟩⟩
  | false => exact ⟨trivial, trivial⟩

theorem nos_flat (n : Nat) (M : NF) (hq : QF M) : NoS (flatF n M) :=
  ⟨(qfn_bigAndN _ (fun o => qfn_bigAndN _ (fun o' => nos_cpair n M o o') _) _).2,
   (qfn_bigOrN _ (fun _ => qf_neqTup _ _) _).2, rep_noS _ M hq 0⟩

/-! ## the values a, b -/

def gv (f : Nat → D) : Nat → List D
  | 0 => []
  | o + 1 => gv f o ++ [d0, f o]

theorem len_gv (f : Nat → D) : (q : Nat) → len (gv d0 f q) = 2 * q
  | 0 => rfl
  | q + 1 => by show len (gv d0 f q ++ _) = _; rw [len_app, len_gv f q, Nat.mul_succ]; rfl

theorem get_gv (f : Nat → D) : (q o : Nat) → o < q → get d0 (gv d0 f q) (2 * o) = d0 ∧ get d0 (gv d0 f q) (2 * o + 1) = f o
  | 0, _, h => absurd h (Nat.not_lt_zero _)
  | q + 1, o, h => by
    show get d0 (gv d0 f q ++ [d0, f q]) _ = d0 ∧ get d0 (gv d0 f q ++ [d0, f q]) _ = f o
    cases le_cases' (Nat.le_of_lt_succ h) with
    | inl e =>
      subst e
      rw [← len_gv d0 f o, show len (gv d0 f o) + 1 = len (gv d0 f o) + 1 from rfl]
      exact ⟨get_app_r d0 _ _ 0, get_app_r d0 _ _ 1⟩
    | inr hl =>
      have l2 : 2 * o + 1 < len (gv d0 f q) := by
        rw [len_gv]
        have := Nat.mul_le_mul_left 2 (Nat.succ_le_of_lt hl)
        rw [Nat.mul_succ] at this
        exact Nat.lt_of_lt_of_le (Nat.lt_succ_self _) this
      rw [get_app_l d0 _ _ _ (Nat.lt_trans (Nat.lt_succ_self _) l2), get_app_l d0 _ _ _ l2]
      exact get_gv f q o hl

theorem andB_t {a b : Bool} (h : (a && b) = true) : a = true ∧ b = true := by
  cases a with
  | true => exact ⟨rfl, h⟩
  | false => exact Bool.noConfusion h

/-- the values at a_o and b_o -/
theorem val_ab (n : Nat) (M : NF) (f : Nat → D) (w : List D) (hw : len w = BO n M) (o : Nat) (ho : o < qO M) :
    get d0 (w ++ gv d0 f (qO M)) (aL n M o) = d0 ∧ get d0 (w ++ gv d0 f (qO M)) (bL n M o) = f o := by
  unfold aL bL
  rw [← hw, get_app_r, Nat.add_assoc, get_app_r]
  exact get_gv d0 f (qO M) o ho

/-- ESO → D, first direction: a relation interpretation gives the dependence-logic truth -/
theorem phi2_intro (K : Classical') (d1 : D) (hne : d1 ≠ d0) (p : List Bool) (M : NF) (hq : QF M)
    (hs : ScN M (lenB p)) (J : SVal D) (h : QP p [] (fun v => semN d0 I J v M)) :
    DTrue d0 I (phi2 (lenB p) M p) := by
  let n := lenB p
  let bv : List D → Nat → D := fun w o => cond (dec K (J (iO M o) (args d0 w (zb n M o)))) d0 d1
  refine prefix_intro d0 I K _ (fun v => semN d0 I J v M) p (fun u => u = []) 0 (fun T' hT' => ?_)
    (fun u hu => by rw [hu]; exact ⟨h, rfl⟩)
  have lenT : ∀ u, T' u → len u = n := fun u hu => by rw [(hT' u hu).2, Nat.zero_add]
  have lenW : ∀ w, dupT T' (qO M * AO M) w → len w = BO n M := fun w ⟨u, z, hu, hz, e⟩ => by
    rw [e, len_app, lenT u hu, hz]; rfl
  refine (allN_ok d0 I _ _ T').2 (exN_intro d0 I _ (2 * qO M) _ (BO n M) lenW (fun w => gv d0 (bv w) (qO M))
    (fun w => len_gv d0 _ _) ?_)
  show TS d0 I _ (depsD n M (qO M)) ∧ TS d0 I _ (toD (flatF n M))
  refine ⟨(ts_depsD d0 I n M _ _).2 (fun o ho => ⟨?_, ?_⟩), (flat d0 I K J (flatF n M) (nos_flat n M hq) _).2 (fun x hx => ?_)⟩
  · intro x x' ⟨w, hw, e⟩ ⟨w', hw', e'⟩ _
    rw [e, e', (val_ab d0 n M (bv w) w (lenW w hw) o ho).1, (val_ab d0 n M (bv w') w' (lenW w' hw') o ho).1]
  · intro x x' ⟨w, hw, e⟩ ⟨w', hw', e'⟩ ha
    rw [e, e', (val_ab d0 n M (bv w) w (lenW w hw) o ho).2, (val_ab d0 n M (bv w') w' (lenW w' hw') o ho).2]
    rw [e, e', args_loc d0 w _ _ (by rw [lenW w hw]; exact zb_lt n M o ho),
      args_loc d0 w' _ _ (by rw [lenW w' hw']; exact zb_lt n M o ho)] at ha
    show cond (dec K (J (iO M o) (args d0 w (zb n M o)))) d0 d1 = cond (dec K (J (iO M o) (args d0 w' (zb n M o)))) d0 d1
    rw [ha]
  · match hx with
    | ⟨w, hw, e⟩ =>
      match hw with
      | ⟨u, z, hu, hz, ew⟩ =>
        have lw := lenW w hw
        have argsW : ∀ o, o < qO M → args d0 x (zb n M o) = args d0 w (zb n M o) := fun o ho => by
          rw [e]; exact args_loc d0 w _ _ (by rw [lw]; exact zb_lt n M o ho)
        refine ⟨(sem_bigAndN d0 I J x _ _).2 (fun o ho => (sem_bigAndN d0 I J x _ _).2 (fun o' ho' => ?_)), ?_⟩
        · unfold cpair
          cases hb : (beqN (iO M o) (iO M o') && beqN (len (isO M o)) (len (isO M o'))) with
          | false => rfl
          | true =>
            have hb' := andB_t hb
            have ei := beqN_t _ _ hb'.1
            have el := beqN_t _ _ hb'.2
            refine (K.lem (args d0 x (zb n M o) = args d0 x (zb n M o'))).elim (fun eq => Or.inr ⟨?_, ?_⟩)
              (fun ne => Or.inl ((sem_neqTup d0 I K J x _ _ (by unfold zb; rw [len_range', len_range', el])).2 ne))
            · show get d0 x (aL n M o) = get d0 x (aL n M o')
              rw [e, (val_ab d0 n M (bv w) w lw o ho).1, (val_ab d0 n M (bv w) w lw o' ho').1]
            · show get d0 x (bL n M o) = get d0 x (bL n M o')
              rw [e, (val_ab d0 n M (bv w) w lw o ho).2, (val_ab d0 n M (bv w) w lw o' ho').2]
              show cond (dec K (J (iO M o) (args d0 w (zb n M o)))) d0 d1 = cond (dec K (J (iO M o') (args d0 w (zb n M o')))) d0 d1
              rw [← argsW o ho, ← argsW o' ho', eq, ei]
        · refine (K.lem (∃ o, o < qO M ∧ args d0 x (zb n M o) ≠ args d0 x (isO M o))).elim
            (fun ⟨o, ho, ne⟩ => Or.inl ((sem_bigOrN d0 I J x _ _).2 ⟨o, ho,
              (sem_neqTup d0 I K J x _ _ (by unfold zb; rw [len_range'])).2 ne⟩)) (fun hn => Or.inr ?_)
          have allEq : ∀ o, o < qO M → args d0 x (zb n M o) = args d0 x (isO M o) := fun o ho =>
            dne K _ (fun ne => hn ⟨o, ho, ne⟩)
          refine (rep_ok d0 I J J x (BO n M) M hq 0 (fun j hj => ?_)).2 ?_
          · rw [Nat.zero_add]
            show get d0 x (aL n M j) = get d0 x (bL n M j) ↔ J (iO M j) (args d0 x (isO M j))
            rw [e, (val_ab d0 n M (bv w) w lw j hj).1, (val_ab d0 n M (bv w) w lw j hj).2]
            show d0 = cond (dec K (J (iO M j) (args d0 w (zb n M j)))) d0 d1 ↔ _
            rw [← argsW j hj, ← e, allEq j hj]
            cases hd : dec K (J (iO M j) (args d0 x (isO M j))) with
            | true => exact ⟨fun _ => (dec_spec K _).1 hd, fun _ => rfl⟩
            | false => exact ⟨fun h' => absurd h'.symm hne, fun hj' => by rw [(dec_spec K _).2 hj'] at hd; exact Bool.noConfusion hd⟩
          · rw [e, ew, app_assoc'']
            exact (loc_qf d0 I J u _ M hq (by rw [lenT u hu]; exact hs)).2 (hT' u hu).1

end DE

end Found
#print axioms Found.DE.phi2_intro

found/DepE8.lean

lean_all/found/DepE8.lean

/-
  Line dependence logic ↔ ESO, part 8. found/DepE8 — ESO → D, second
  direction: if φ₂ holds then the relation "a_o = b_o at a tuple whose block
  z̄_o is ū" (for an occurrence o of the variable) makes the prefix over the
  matrix true (phi2_elim); it is well defined by the dependence atoms
  =(z̄_o, a_o), =(z̄_o, b_o) and the consistency clause, read at tuples whose
  blocks are chosen to be the occurrence's arguments.

  Lean 4 core only, hand-written proofs, no axioms (classical point K).
-/
import found.DepE7

namespace Found

namespace DE

open CP

variable {D : Type} (d0 : D) (I : Nat → List D → Prop)

theorem len_args' (u : List D) : (is : List Nat) → len (args d0 u is) = len is
  | [] => rfl
  | _ :: is => congrArg (· + 1) (len_args' u is)

theorem len_args_range (x : List D) (b m : Nat) : len (args d0 x (range' b m)) = m := by
  rw [len_args', len_range']

theorem beqN_f {a b : Nat} (h : a ≠ b) : beqN a b = false := by
  cases e : beqN a b with
  | false => rfl
  | true => exact absurd (beqN_t a b e) h

def setAt (l0 : Nat → List D) (o' : Nat) (v : List D) : Nat → List D := fun o => cond (beqN o o') v (l0 o)

theorem setAt_eq (l0 : Nat → List D) (o' : Nat) (v : List D) : setAt l0 o' v o' = v := by
  unfold setAt; rw [beqN_self]; rfl

theorem setAt_ne (l0 : Nat → List D) {o o' : Nat} (v : List D) (h : o ≠ o') : setAt l0 o' v o = l0 o := by
  unfold setAt; rw [beqN_f h]; rfl

/-- reading block o of u ++ z₀ ++ … ++ g -/
theorem blockArgs (n : Nat) (M : NF) (u : List D) (hu : len u = n) (l : Nat → List D)
    (hl : ∀ o, o < qO M → len (l o) = len (isO M o)) (g : List D) (o : Nat) (ho : o < qO M) :
    args d0 (u ++ zcat (fun o => padC d0 (l o) (AO M)) (qO M) ++ g) (zb n M o) = l o := by
  have hA : len (isO M o) ≤ AO M := amax_ge (occ M) o ho
  apply args_range_eq d0 _ _ _ _ (hl o ho)
  intro j hj
  have lz := len_zcat (fun o => padC d0 (l o) (AO M)) (AO M) (fun o => len_padC d0 _ _) (qO M)
  have pos : n + o * AO M + j < len (u ++ zcat (fun o => padC d0 (l o) (AO M)) (qO M)) := by
    rw [len_app, hu, lz, Nat.add_assoc]
    apply Nat.add_lt_add_left
    have h2 : (o + 1) * AO M ≤ qO M * AO M := Nat.mul_le_mul_right _ (Nat.succ_le_of_lt ho)
    rw [Nat.succ_mul] at h2
    exact Nat.lt_of_lt_of_le (Nat.add_lt_add_left (Nat.lt_of_lt_of_le hj hA) _) h2
  rw [get_app_l d0 _ g _ pos, ← hu, Nat.add_assoc, get_app_r]
  have := get_zcat d0 (fun o => padC d0 (l o) (AO M)) (AO M) (fun o => len_padC d0 _ _) (qO M) [] o j ho
    (Nat.lt_of_lt_of_le hj hA)
  rw [app_nil'] at this
  rw [this]
  exact get_padC d0 (l o) (AO M) j (by rw [hl o ho]; exact hj) (Nat.lt_of_lt_of_le hj hA)

/-- ESO → D, second direction -/
theorem phi2_elim (K : Classical') (p : List Bool) (M : NF) (hq : QF M) (hs : ScN M (lenB p))
    (h : DTrue d0 I (phi2 (lenB p) M p)) : ∃ J : SVal D, QP p [] (fun v => semN d0 I J v M) := by
  let n := lenB p
  match prefix_elim d0 I _ p (fun u => u = []) 0 h (fun u hu => by rw [hu]; rfl) with
  | ⟨T', hT', lenT', prop⟩ =>
    have lenT : ∀ u, T' u → len u = n := fun u hu => by rw [lenT' u hu, Nat.zero_add]
    match exN_elim d0 I _ (2 * qO M) _ ((allN_ok d0 I _ _ T').1 hT') with
    | ⟨G, _, hbody⟩ =>
      let Tf : Team D := img (dupT T' (qO M * AO M)) G
      have hdeps := (ts_depsD d0 I n M Tf (qO M)).1 hbody.1
      have hflat : ∀ x, Tf x → semN d0 I (fun _ _ => True) x (flatF n M) :=
        (flat d0 I K (fun _ _ => True) (flatF n M) (nos_flat n M hq) Tf).1 hbody.2
      let J : SVal D := fun i ū => ∃ x, Tf x ∧ ∃ o, o < qO M ∧ iO M o = i ∧ args d0 x (zb n M o) = ū ∧
        get d0 x (aL n M o) = get d0 x (bL n M o)
      refine ⟨J, prop _ (fun u hu => ?_) [] rfl⟩
      -- the tuple whose blocks are the arguments of the occurrences, possibly one block changed
      have lu := lenT u hu
      have isLt : ∀ o, o < qO M → AllLt (isO M o) n := fun o ho => occ_sc M n hq hs o ho
      let mk : (Nat → List D) → List D := fun l => u ++ zcat (fun o => padC d0 (l o) (AO M)) (qO M)
      have mkT : ∀ l, Tf (mk l ++ G (mk l)) := fun l =>
        ⟨mk l, ⟨u, _, hu, by rw [len_zcat _ (AO M) (fun o => len_padC d0 _ _)], rfl⟩, rfl⟩
      have isArgs : ∀ l g o, o < qO M → args d0 (mk l ++ g) (isO M o) = args d0 u (isO M o) := fun l g o ho => by
        show args d0 (u ++ _ ++ g) _ = _
        rw [app_assoc'']; exact args_loc d0 u _ _ (by rw [lu]; exact isLt o ho)
      let l0 : Nat → List D := fun o => args d0 u (isO M o)
      have hl0 : ∀ o, o < qO M → len (l0 o) = len (isO M o) := fun o _ => len_args' d0 u _
      let xs := mk l0 ++ G (mk l0)
      have zs : ∀ o, o < qO M → args d0 xs (zb n M o) = args d0 u (isO M o) := fun o ho =>
        blockArgs d0 n M u lu l0 hl0 _ o ho
      have fl := hflat xs (mkT l0)
      -- no escape at xs
      have hrep : semN d0 I (fun _ _ => True) xs (rep (BO n M) M 0).1 := fl.2.elim
        (fun hesc => match (sem_bigOrN d0 I _ xs _ _).1 hesc with
          | ⟨o, ho, hne⟩ => absurd (by rw [zs o ho, isArgs l0 _ o ho])
              ((sem_neqTup d0 I K _ xs _ _ (by unfold zb; rw [len_range'])).1 hne))
        id
      refine (loc_qf d0 I J u (zcat (fun o => padC d0 (l0 o) (AO M)) (qO M) ++ G (mk l0)) M hq (by rw [lu]; exact hs)).1 ?_
      rw [← app_assoc'']
      refine (rep_ok d0 I J (fun _ _ => True) xs (BO n M) M hq 0 (fun j hj => ?_)).1 hrep
      rw [Nat.zero_add]
      show get d0 xs (aL n M j) = get d0 xs (bL n M j) ↔ J (iO M j) (args d0 xs (isO M j))
      rw [isArgs l0 _ j hj]
      constructor
      · intro heq
        exact ⟨xs, mkT l0, j, hj, rfl, zs j hj, heq⟩
      · intro ⟨x, hx, o', ho', ei, ea, eqx⟩
        have lenU : len (args d0 u (isO M j)) = len (isO M o') := by
          rw [← ea]; unfold zb; exact len_args_range d0 x _ _
        let l' : Nat → List D := setAt l0 o' (args d0 u (isO M j))
        have hl' : ∀ o, o < qO M → len (l' o) = len (isO M o) := fun o ho => by
          match Nat.decEq o o' with
          | isTrue e => rw [e]; show len (setAt l0 o' _ o') = _; rw [setAt_eq]; exact lenU
          | isFalse ne => show len (setAt l0 o' _ o) = _; rw [setAt_ne l0 _ ne]; exact hl0 o ho
        let y := mk l' ++ G (mk l')
        have yo' : args d0 y (zb n M o') = args d0 u (isO M j) := by
          rw [blockArgs d0 n M u lu l' hl' (G (mk l')) o' ho']
          exact setAt_eq l0 o' _
        have yj : args d0 y (zb n M j) = args d0 u (isO M j) := by
          rw [blockArgs d0 n M u lu l' hl' (G (mk l')) j hj]
          match Nat.decEq j o' with
          | isTrue e =>
            show setAt l0 o' (args d0 u (isO M j)) j = _
            unfold setAt
            rw [show beqN j o' = true from e ▸ beqN_self j]
            rfl
          | isFalse ne => exact setAt_ne l0 _ ne
        -- consistency at y for (j, o')
        have cons := (sem_bigAndN d0 I _ y _ _).1 (hflat y (mkT l')).1 j hj
        have cp := (sem_bigAndN d0 I _ y _ _).1 cons o' ho'
        unfold cpair at cp
        have cond1 : (beqN (iO M j) (iO M o') && beqN (len (isO M j)) (len (isO M o'))) = true := by
          rw [ei, beqN_self, show len (isO M j) = len (isO M o') from by
            rw [← lenU, len_args'], beqN_self]; rfl
        rw [cond1] at cp
        have ab : get d0 y (aL n M j) = get d0 y (aL n M o') ∧ get d0 y (bL n M j) = get d0 y (bL n M o') :=
          cp.elim (fun hne => absurd (by rw [yj, yo']) ((sem_neqTup d0 I K _ y _ _ (by unfold zb; rw [len_range', len_range',
            show len (isO M j) = len (isO M o') from by rw [← lenU, len_args']])).1 hne)) id
        -- dependence between x and y at o', between y and xs at j
        have dxa := (hdeps o' ho').1 x y hx (mkT l') (by rw [ea, yo'])
        have dxb := (hdeps o' ho').2 x y hx (mkT l') (by rw [ea, yo'])
        have dya := (hdeps j hj).1 y xs (mkT l') (mkT l0) (by rw [yj, zs j hj])
        have dyb := (hdeps j hj).2 y xs (mkT l') (mkT l0) (by rw [yj, zs j hj])
        rw [← dya, ← dyb, ab.1, ab.2, ← dxa, ← dxb]
        exact eqx

end DE

end Found

#print axioms Found.DE.blockArgs
#print axioms Found.DE.phi2_elim

found/DepE9.lean

lean_all/found/DepE9.lean

/-
  Line dependence logic ↔ ESO, part 9. found/DepE9 — one-element domains
  and the theorem.  On a one-element domain a relation variable is one truth
  value per arity, so ESO is a finite disjunction over consistent truth
  vectors of first-order sentences, which dependence logic expresses
  flatly (phi1).  The uniform translation
      eso2d θ = (∀x∀y x = y ∧ φ₁) ∨ (∃x∃y x ≠ y ∧ φ₂)
  satisfies ETrue θ ↔ DTrue (eso2d θ) on every structure (eso_to_d); with
  found/DepE2 (d_to_eso) this is Väänänen's theorem: dependence logic and
  existential second-order logic define the same classes of structures.

  Lean 4 core only, hand-written proofs, no axioms (classical point K).
-/
import found.DepE8

namespace Found

namespace DE

open CP

variable {D : Type} (d0 : D) (I : Nat → List D → Prop)

/-! ## truth values for the occurrences -/

def substB (β : Nat → Bool) : NF → Nat → NF × Nat
  | .svar _ _, c => (cond (β c) (.eq 0 0) (.neq 0 0), c + 1)
  | .nsvar _ _, c => (cond (β c) (.neq 0 0) (.eq 0 0), c + 1)
  | .and a b, c => (.and (substB β a c).1 (substB β b (substB β a c).2).1, (substB β b (substB β a c).2).2)
  | .or a b, c => (.or (substB β a c).1 (substB β b (substB β a c).2).1, (substB β b (substB β a c).2).2)
  | .rel r is, c => (.rel r is, c)
  | .nrel r is, c => (.nrel r is, c)
  | .eq i j, c => (.eq i j, c)
  | .neq i j, c => (.neq i j, c)
  | .ex φ, c => (.ex φ, c)
  | .all φ, c => (.all φ, c)

theorem substB_cnt (β : Nat → Bool) : (M : NF) → (c : Nat) → (substB β M c).2 = c + len (occ M)
  | .svar _ _, _ => rfl
  | .nsvar _ _, _ => rfl
  | .and a b, c => by
    show (substB β b (substB β a c).2).2 = c + len (occ a ++ occ b)
    rw [substB_cnt β b, substB_cnt β a, len_app, Nat.add_assoc]
  | .or a b, c => by
    show (substB β b (substB β a c).2).2 = c + len (occ a ++ occ b)
    rw [substB_cnt β b, substB_cnt β a, len_app, Nat.add_assoc]
  | .rel _ _, _ => rfl
  | .nrel _ _, _ => rfl
  | .eq _ _, _ => rfl
  | .neq _ _, _ => rfl
  | .ex _, _ => rfl
  | .all _, _ => rfl

theorem substB_ok (J J' : SVal D) (x : List D) (β : Nat → Bool) : (M : NF) → QF M → (c : Nat) →
    (∀ j, j < len (occ M) → (β (c + j) = true ↔ J (nthP (occ M) j).1 (args d0 x (nthP (occ M) j).2))) →
    (semN d0 I J' x (substB β M c).1 ↔ semN d0 I J x M)
  | .svar _ _, _, c, h => by
    have := h 0 (Nat.succ_pos 0)
    rw [Nat.add_zero] at this
    show semN d0 I J' x (cond (β c) (.eq 0 0) (.neq 0 0)) ↔ _
    cases hb : β c with
    | true => exact ⟨fun _ => this.1 hb, fun _ => rfl⟩
    | false => exact ⟨fun hn => absurd rfl hn, fun hj => by rw [this.2 hj] at hb; exact Bool.noConfusion hb⟩
  | .nsvar _ _, _, c, h => by
    have := h 0 (Nat.succ_pos 0)
    rw [Nat.add_zero] at this
    show semN d0 I J' x (cond (β c) (.neq 0 0) (.eq 0 0)) ↔ _
    cases hb : β c with
    | true => exact ⟨fun hn => absurd rfl hn, fun hn => absurd (this.1 hb) hn⟩
    | false => exact ⟨fun _ hj => by rw [this.2 hj] at hb; exact Bool.noConfusion hb, fun _ => rfl⟩
  | .and a b, ⟨qa, qb⟩, c, h => by
    have ha : ∀ j, j < len (occ a) → (β (c + j) = true ↔ J (nthP (occ a) j).1 (args d0 x (nthP (occ a) j).2)) := fun j hj => by
      have := h j (by show j < len (occ a ++ occ b); rw [len_app]; exact Nat.lt_of_lt_of_le hj (Nat.le_add_right _ _))
      have e' : nthP (occ (.and a b)) j = nthP (occ a) j := nthP_app_l (occ a) (occ b) j hj
      rw [e'] at this; exact this
    have hb : ∀ j, j < len (occ b) → (β ((substB β a c).2 + j) = true ↔ J (nthP (occ b) j).1 (args d0 x (nthP (occ b) j).2)) := fun j hj => by
      have := h (len (occ a) + j) (by show _ < len (occ a ++ occ b); rw [len_app]; exact Nat.add_lt_add_left hj _)
      have e' : nthP (occ (.and a b)) (len (occ a) + j) = nthP (occ b) j := nthP_app_r (occ a) (occ b) j
      rw [e', ← Nat.add_assoc] at this
      rw [substB_cnt]; exact this
    exact ⟨fun ⟨x1, x2⟩ => ⟨(substB_ok J J' x β a qa c ha).1 x1, (substB_ok J J' x β b qb _ hb).1 x2⟩,
      fun ⟨x1, x2⟩ => ⟨(substB_ok J J' x β a qa c ha).2 x1, (substB_ok J J' x β b qb _ hb).2 x2⟩⟩
  | .or a b, ⟨qa, qb⟩, c, h => by
    have ha : ∀ j, j < len (occ a) → (β (c + j) = true ↔ J (nthP (occ a) j).1 (args d0 x (nthP (occ a) j).2)) := fun j hj => by
      have := h j (by show j < len (occ a ++ occ b); rw [len_app]; exact Nat.lt_of_lt_of_le hj (Nat.le_add_right _ _))
      have e' : nthP (occ (.or a b)) j = nthP (occ a) j := nthP_app_l (occ a) (occ b) j hj
      rw [e'] at this; exact this
    have hb : ∀ j, j < len (occ b) → (β ((substB β a c).2 + j) = true ↔ J (nthP (occ b) j).1 (args d0 x (nthP (occ b) j).2)) := fun j hj => by
      have := h (len (occ a) + j) (by show _ < len (occ a ++ occ b); rw [len_app]; exact Nat.add_lt_add_left hj _)
      have e' : nthP (occ (.or a b)) (len (occ a) + j) = nthP (occ b) j := nthP_app_r (occ a) (occ b) j
      rw [e', ← Nat.add_assoc] at this
      rw [substB_cnt]; exact this
    exact ⟨fun hh => hh.elim (fun x1 => Or.inl ((substB_ok J J' x β a qa c ha).1 x1)) (fun x2 => Or.inr ((substB_ok J J' x β b qb _ hb).1 x2)),
      fun hh => hh.elim (fun x1 => Or.inl ((substB_ok J J' x β a qa c ha).2 x1)) (fun x2 => Or.inr ((substB_ok J J' x β b qb _ hb).2 x2))⟩
  | .rel _ _, _, _, _ => Iff.rfl
  | .nrel _ _, _, _, _ => Iff.rfl
  | .eq _ _, _, _, _ => Iff.rfl
  | .neq _ _, _, _, _ => Iff.rfl

theorem substB_noS (β : Nat → Bool) : (M : NF) → QF M → (c : Nat) → NoS (substB β M c).1
  | .svar _ _, _, c => by show NoS (cond (β c) _ _); cases β c <;> exact trivial
  | .nsvar _ _, _, c => by show NoS (cond (β c) _ _); cases β c <;> exact trivial
  | .and a b, ⟨qa, qb⟩, c => ⟨substB_noS β a qa c, substB_noS β b qb _⟩
  | .or a b, ⟨qa, qb⟩, c => ⟨substB_noS β a qa c, substB_noS β b qb _⟩
  | .rel _ _, _, _ => trivial
  | .nrel _ _, _, _ => trivial
  | .eq _ _, _, _ => trivial
  | .neq _ _, _, _ => trivial

/-! ## prefixes of NF formulas -/

def pfxN : List Bool → NF → NF
  | [], M => M
  | true :: p, M => .all (pfxN p M)
  | false :: p, M => .ex (pfxN p M)

theorem toD_pfx : (p : List Bool) → (M : NF) → toD (pfxN p M) = prefixD p (toD M)
  | [], _ => rfl
  | true :: p, M => congrArg DF.all (toD_pfx p M)
  | false :: p, M => congrArg DF.ex (toD_pfx p M)

theorem sem_pfx (J : SVal D) (M : NF) : (p : List Bool) → (e : List D) →
    (semN d0 I J e (pfxN p M) ↔ QP p e (fun e' => semN d0 I J e' M))
  | [], _ => Iff.rfl
  | true :: p, _ => ⟨fun h a => (sem_pfx J M p _).1 (h a), fun h a => (sem_pfx J M p _).2 (h a)⟩
  | false :: p, _ => ⟨fun ⟨a, h⟩ => ⟨a, (sem_pfx J M p _).1 h⟩, fun ⟨a, h⟩ => ⟨a, (sem_pfx J M p _).2 h⟩⟩

theorem nos_pfx : (p : List Bool) → (M : NF) → NoS M → NoS (pfxN p M)
  | [], _, h => h
  | true :: p, M, h => nos_pfx p M h
  | false :: p, M, h => nos_pfx p M h

theorem qp_iff (P P' : List D → Prop) (h : ∀ e, P e ↔ P' e) : (p : List Bool) → (e : List D) → (QP p e P ↔ QP p e P')
  | [], e => h e
  | true :: p, _ => ⟨fun s a => (qp_iff P P' h p _).1 (s a), fun s a => (qp_iff P P' h p _).2 (s a)⟩
  | false :: p, _ => ⟨fun ⟨a, s⟩ => ⟨a, (qp_iff P P' h p _).1 s⟩, fun ⟨a, s⟩ => ⟨a, (qp_iff P P' h p _).2 s⟩⟩

/-! ## Boolean vectors -/

def nthBo : List Bool → Nat → Bool
  | [], _ => false
  | b :: _, 0 => b
  | _ :: l, i + 1 => nthBo l i

def vecB (f : Nat → Bool) (i : Nat) : Nat → List Bool
  | 0 => []
  | q + 1 => f i :: vecB f (i + 1) q

theorem nth_vecB (f : Nat → Bool) : (q i j : Nat) → j < q → nthBo (vecB f i q) j = f (i + j)
  | 0, _, _, h => absurd h (Nat.not_lt_zero _)
  | _ + 1, _, 0, _ => rfl
  | q + 1, i, j + 1, h => by
    show nthBo (vecB f (i + 1) q) j = f (i + (j + 1))
    rw [nth_vecB f q (i + 1) j (Nat.lt_of_succ_lt_succ h), Nat.add_right_comm, Nat.add_assoc]

def allBV : Nat → List (List Bool)
  | 0 => [[]]
  | q + 1 => (allBV q).map (false :: ·) ++ (allBV q).map (true :: ·)

inductive MemB (x : List Bool) : List (List Bool) → Prop where
  | head (l : List (List Bool)) : MemB x (x :: l)
  | tail (y : List Bool) {l : List (List Bool)} : MemB x l → MemB x (y :: l)

theorem memB_map (b : Bool) {x : List Bool} : {l : List (List Bool)} → MemB x l → MemB (b :: x) (l.map (b :: ·))
  | _, .head _ => .head _
  | _, .tail _ m => .tail _ (memB_map b m)

theorem memB_app_l {x : List Bool} : {a : List (List Bool)} → (b : List (List Bool)) → MemB x a → MemB x (a ++ b)
  | _, _, .head _ => .head _
  | _, b, .tail _ m => .tail _ (memB_app_l b m)

theorem memB_app_r {x : List Bool} : (a : List (List Bool)) → {b : List (List Bool)} → MemB x b → MemB x (a ++ b)
  | [], _, m => m
  | _ :: a, _, m => .tail _ (memB_app_r a m)

theorem memB_all : (q : Nat) → (x : List Bool) → len x = q → MemB x (allBV q)
  | 0, [], _ => .head _
  | 0, _ :: _, h => Nat.noConfusion h
  | _ + 1, [], h => Nat.noConfusion h
  | q + 1, false :: x, h => memB_app_l _ (memB_map false (memB_all q x (Nat.succ.inj h)))
  | q + 1, true :: x, h => memB_app_r _ (memB_map true (memB_all q x (Nat.succ.inj h)))

theorem len_vecB (f : Nat → Bool) : (q i : Nat) → len (vecB f i q) = q
  | 0, _ => rfl
  | q + 1, i => congrArg (· + 1) (len_vecB f q (i + 1))

/-! ## consistency of a truth vector -/

def allI (f : Nat → Bool) : Nat → Bool
  | 0 => true
  | n + 1 => allI f n && f n

theorem allI_t (f : Nat → Bool) : (n : Nat) → allI f n = true → ∀ i, i < n → f i = true
  | 0, _, _, h => absurd h (Nat.not_lt_zero _)
  | n + 1, h, i, hi => (le_cases' (Nat.le_of_lt_succ hi)).elim (fun e => e ▸ (andB_t h).2)
      (fun hl => allI_t f n (andB_t h).1 i hl)

theorem t_allI (f : Nat → Bool) : (n : Nat) → (∀ i, i < n → f i = true) → allI f n = true
  | 0, _ => rfl
  | n + 1, h => by
    show (allI f n && f n) = true
    rw [t_allI f n (fun i hi => h i (Nat.lt_trans hi (Nat.lt_succ_self n))), h n (Nat.lt_succ_self n)]; rfl

def beqB : Bool → Bool → Bool
  | true, true => true
  | false, false => true
  | true, false => false
  | false, true => false

theorem beqB_t : (a b : Bool) → beqB a b = true → a = b
  | true, true, _ => rfl
  | false, false, _ => rfl
  | true, false, h => Bool.noConfusion h
  | false, true, h => Bool.noConfusion h

theorem beqB_self : (a : Bool) → beqB a a = true
  | true => rfl
  | false => rfl

def consB (M : NF) (β : List Bool) : Bool :=
  allI (fun o => allI (fun o' =>
    !(beqN (iO M o) (iO M o') && beqN (len (isO M o)) (len (isO M o'))) || beqB (nthBo β o) (nthBo β o')) (qO M)) (qO M)

theorem consB_t (M : NF) (β : List Bool) (h : consB M β = true) (o o' : Nat) (ho : o < qO M) (ho' : o' < qO M)
    (ei : iO M o = iO M o') (el : len (isO M o) = len (isO M o')) : nthBo β o = nthBo β o' := by
  have := allI_t _ _ (allI_t _ _ h o ho) o' ho'
  rw [ei, el, beqN_self, beqN_self] at this
  exact beqB_t _ _ this

theorem t_consB (M : NF) (β : List Bool)
    (h : ∀ o o', o < qO M → o' < qO M → iO M o = iO M o' → len (isO M o) = len (isO M o') → nthBo β o = nthBo β o') :
    consB M β = true :=
  t_allI _ _ (fun o ho => t_allI _ _ (fun o' ho' => by
    cases e1 : beqN (iO M o) (iO M o') with
    | false => rfl
    | true =>
      cases e2 : beqN (len (isO M o)) (len (isO M o')) with
      | false => rfl
      | true =>
        show (!(true && true) || beqB (nthBo β o) (nthBo β o')) = true
        rw [h o o' ho ho' (beqN_t _ _ e1) (beqN_t _ _ e2), beqB_self]; rfl))

/-! ## one-element domains -/

theorem lists_eq (hD : ∀ a b : D, a = b) : (u v : List D) → len u = len v → u = v
  | [], [], _ => rfl
  | [], _ :: _, h => Nat.noConfusion h
  | _ :: _, [], h => Nat.noConfusion h
  | a :: u, b :: v, h => by rw [hD a b, lists_eq hD u v (Nat.succ.inj h)]

theorem one_elem (K : Classical') (hD : ∀ a b : D, a = b) (p : List Bool) (M : NF) (hq : QF M) :
    (∃ J : SVal D, QP p [] (fun v => semN d0 I J v M)) ↔
      ∃ β, MemB β (allBV (qO M)) ∧ consB M β = true ∧
        QP p [] (fun v => semN d0 I (fun _ _ => True) v (substB (nthBo β) M 0).1) := by
  constructor
  · intro ⟨J, h⟩
    let f : Nat → Bool := fun o => dec K (J (iO M o) (args d0 [] (isO M o)))
    let β := vecB f 0 (qO M)
    have hβ : ∀ o, o < qO M → nthBo β o = f o := fun o ho => by rw [nth_vecB f _ 0 o ho, Nat.zero_add]
    have argsE : ∀ x o, args d0 x (isO M o) = args d0 [] (isO M o) := fun x o =>
      lists_eq hD _ _ (by rw [len_args', len_args'])
    refine ⟨β, memB_all _ β (len_vecB _ _ _), t_consB M β (fun o o' ho ho' ei el => by
      rw [hβ o ho, hβ o' ho']
      show dec K (J (iO M o) (args d0 [] (isO M o))) = dec K (J (iO M o') (args d0 [] (isO M o')))
      rw [ei, lists_eq hD (args d0 [] (isO M o)) (args d0 [] (isO M o')) (by rw [len_args', len_args', el])]), ?_⟩
    refine (qp_iff _ _ (fun x => substB_ok d0 I J _ x (nthBo β) M hq 0 (fun j hj => ?_)) p []).2 h
    rw [Nat.zero_add, hβ j hj]
    show dec K (J (iO M j) (args d0 [] (isO M j))) = true ↔ J (iO M j) (args d0 x (isO M j))
    rw [argsE x j]
    exact dec_spec K _
  · intro ⟨β, _, hc, h⟩
    let J : SVal D := fun i u => ∃ o, o < qO M ∧ iO M o = i ∧ len (isO M o) = len u ∧ nthBo β o = true
    refine ⟨J, (qp_iff _ _ (fun x => substB_ok d0 I J _ x (nthBo β) M hq 0 (fun j hj => ?_)) p []).1 h⟩
    rw [Nat.zero_add]
    exact ⟨fun hb => ⟨j, hj, rfl, (len_args' d0 x _).symm, hb⟩,
      fun ⟨o', ho', ei, el, hb⟩ => by
        rw [← consB_t M β hc o' j ho' hj ei (by rw [el, len_args']; rfl)]; exact hb⟩

/-! ## the sentences -/

def orL : List DF → DF
  | [] => .neq 0 0
  | φ :: l => .or φ (orL l)

def phi1 (p : List Bool) (M : NF) : DF :=
  orL ((allBV (qO M)).map (fun β => cond (consB M β) (toD (pfxN p (substB (nthBo β) M 0).1)) (.neq 0 0)))

theorem ts_empty : (φ : DF) → TS d0 I (fun _ => False) φ
  | .rel _ _ => fun _ h => h.elim
  | .nrel _ _ => fun _ h => h.elim
  | .eq _ _ => fun _ h => h.elim
  | .neq _ _ => fun _ h => h.elim
  | .dep _ _ => fun _ _ h => h.elim
  | .and φ ψ => ⟨ts_empty φ, ts_empty ψ⟩
  | .or φ ψ => ⟨fun _ => False, fun _ => False, fun _ h => h.elim, fun _ h => h, fun _ h => h, ts_empty φ, ts_empty ψ⟩
  | .ex φ => ⟨fun _ => d0, (ts_congr d0 I φ _ _ (fun _ => ⟨fun ⟨_, h, _⟩ => h.elim, fun h => h.elim⟩)).2 (ts_empty φ)⟩
  | .all φ => (ts_congr d0 I φ _ _ (fun _ => ⟨fun ⟨_, _, h, _⟩ => h.elim, fun h => h.elim⟩)).2 (ts_empty φ)

theorem dtrue_or (φ ψ : DF) : DTrue d0 I (.or φ ψ) ↔ DTrue d0 I φ ∨ DTrue d0 I ψ := by
  constructor
  · intro ⟨T1, T2, c, s1, s2, a, b⟩
    exact (c [] rfl).elim
      (fun h1 => Or.inl ((ts_congr d0 I φ _ _ (fun u => ⟨fun h => s1 u h, fun e => by rw [e]; exact h1⟩)).1 a))
      (fun h2 => Or.inr ((ts_congr d0 I ψ _ _ (fun u => ⟨fun h => s2 u h, fun e => by rw [e]; exact h2⟩)).1 b))
  · intro h
    exact h.elim (fun a => ⟨fun u => u = [], fun _ => False, fun _ e => Or.inl e, fun _ e => e, fun _ h => h.elim, a, ts_empty d0 I ψ⟩)
      (fun b => ⟨fun _ => False, fun u => u = [], fun _ e => Or.inr e, fun _ h => h.elim, fun _ e => e, ts_empty d0 I φ, b⟩)

theorem dtrue_orL : (l : List DF) → DTrue d0 I (orL l) → ∃ φ, (∃ l1 l2, l = l1 ++ φ :: l2) ∧ DTrue d0 I φ
  | [], h => absurd (h [] rfl) (fun ne => ne rfl)
  | φ :: l, h => ((dtrue_or d0 I φ (orL l)).1 h).elim (fun a => ⟨φ, ⟨[], l, rfl⟩, a⟩)
      (fun b => match dtrue_orL l b with
        | ⟨ψ, ⟨l1, l2, e⟩, hψ⟩ => ⟨ψ, ⟨φ :: l1, l2, by rw [e]; rfl⟩, hψ⟩)

theorem orL_true (φ : DF) (h : DTrue d0 I φ) : (l1 l2 : List DF) → DTrue d0 I (orL (l1 ++ φ :: l2))
  | [], l2 => (dtrue_or d0 I φ (orL l2)).2 (Or.inl h)
  | ψ :: l1, l2 => (dtrue_or d0 I ψ _).2 (Or.inr (orL_true φ h l1 l2))

theorem map_mem {f : List Bool → DF} {β : List Bool} : {L : List (List Bool)} → MemB β L →
    ∃ l1 l2, L.map f = l1 ++ f β :: l2
  | _, .head l => ⟨[], l.map f, rfl⟩
  | _, .tail y m => match map_mem (f := f) m with
    | ⟨l1, l2, e⟩ => ⟨f y :: l1, l2, by show f y :: _ = _; rw [e]; rfl⟩

theorem map_split {f : List Bool → DF} {φ : DF} : (L : List (List Bool)) → (l1 l2 : List DF) → L.map f = l1 ++ φ :: l2 →
    ∃ β, MemB β L ∧ f β = φ
  | [], [], _, h => nomatch h
  | [], _ :: _, _, h => nomatch h
  | β :: L, [], _, h => ⟨β, .head L, (List.cons.inj h).1⟩
  | β :: L, _ :: l1, l2, h => match map_split L l1 l2 (List.cons.inj h).2 with
    | ⟨γ, m, e⟩ => ⟨γ, .tail β m, e⟩

theorem dtrue_flat (K : Classical') (N : NF) (hN : NoS N) : DTrue d0 I (toD N) ↔ semN d0 I (fun _ _ => True) [] N :=
  (flat d0 I K (fun _ _ => True) N hN _).trans ⟨fun h => h [] rfl, fun h u e => by rw [e]; exact h⟩

theorem phi1_ok (K : Classical') (p : List Bool) (M : NF) (hq : QF M) :
    DTrue d0 I (phi1 p M) ↔ ∃ β, MemB β (allBV (qO M)) ∧ consB M β = true ∧
      QP p [] (fun v => semN d0 I (fun _ _ => True) v (substB (nthBo β) M 0).1) := by
  have one : ∀ β, DTrue d0 I (toD (pfxN p (substB (nthBo β) M 0).1)) ↔
      QP p [] (fun v => semN d0 I (fun _ _ => True) v (substB (nthBo β) M 0).1) := fun β =>
    (dtrue_flat d0 I K _ (nos_pfx p _ (substB_noS _ M hq 0))).trans (sem_pfx d0 I _ _ p [])
  constructor
  · intro h
    match dtrue_orL d0 I _ h with
    | ⟨φ, ⟨l1, l2, e⟩, hφ⟩ =>
      match map_split _ l1 l2 e with
      | ⟨β, m, ef⟩ =>
        rw [← ef] at hφ
        cases hc : consB M β with
        | false => rw [hc] at hφ; exact absurd (hφ [] rfl) (fun ne => ne rfl)
        | true => rw [hc] at hφ; exact ⟨β, m, hc, (one β).1 hφ⟩
  · intro ⟨β, m, hc, hq'⟩
    match map_mem (f := fun β => cond (consB M β) (toD (pfxN p (substB (nthBo β) M 0).1)) (.neq 0 0)) m with
    | ⟨l1, l2, e⟩ =>
      show DTrue d0 I (orL _)
      rw [e]
      refine orL_true d0 I _ ?_ l1 l2
      show DTrue d0 I (cond (consB M β) _ _)
      rw [hc]
      exact (one β).2 hq'

/-! ## closed ESO sentences -/

def ScE : EF → Nat → Prop
  | .rel _ is, n => AllLt is n
  | .svar _ is, n => AllLt is n
  | .eq i j, n => i < n ∧ j < n
  | .neg φ, n => ScE φ n
  | .and φ ψ, n => ScE φ n ∧ ScE ψ n
  | .or φ ψ, n => ScE φ n ∧ ScE ψ n
  | .ex φ, n => ScE φ (n + 1)
  | .all φ, n => ScE φ (n + 1)

theorem sc_nnf : (θ : EF) → (n : Nat) → ScE θ n → (b : Bool) → ScN (nnf θ b) n
  | .rel _ _, _, h, true => h
  | .rel _ _, _, h, false => h
  | .svar _ _, _, h, true => h
  | .svar _ _, _, h, false => h
  | .eq _ _, _, h, true => h
  | .eq _ _, _, h, false => h
  | .neg φ, n, h, true => sc_nnf φ n h false
  | .neg φ, n, h, false => sc_nnf φ n h true
  | .and φ ψ, n, ⟨a, b⟩, true => ⟨sc_nnf φ n a true, sc_nnf ψ n b true⟩
  | .and φ ψ, n, ⟨a, b⟩, false => ⟨sc_nnf φ n a false, sc_nnf ψ n b false⟩
  | .or φ ψ, n, ⟨a, b⟩, true => ⟨sc_nnf φ n a true, sc_nnf ψ n b true⟩
  | .or φ ψ, n, ⟨a, b⟩, false => ⟨sc_nnf φ n a false, sc_nnf ψ n b false⟩
  | .ex φ, n, h, true => sc_nnf φ (n + 1) h true
  | .ex φ, n, h, false => sc_nnf φ (n + 1) h false
  | .all φ, n, h, true => sc_nnf φ (n + 1) h true
  | .all φ, n, h, false => sc_nnf φ (n + 1) h false

/-! ## the translation and the theorem -/

def allEqD : DF := .all (.all (.eq 0 1))
def someNeqD : DF := .ex (.ex (.neq 0 1))

def eso2d (θ : EF) : DF :=
  .or (.and allEqD (phi1 (pre (nnf θ true) 0).1 (pre (nnf θ true) 0).2))
      (.and someNeqD (phi2 (lenB (pre (nnf θ true) 0).1) (pre (nnf θ true) 0).2 (pre (nnf θ true) 0).1))

theorem dtrue_allEq : DTrue d0 I (allEqD : DF) ↔ ∀ a b : D, a = b :=
  ⟨fun h a b => h [a, b] ⟨[a], b, ⟨[], a, rfl, rfl⟩, rfl⟩,
   fun h _ ⟨_, b, ⟨_, a, e1, e2⟩, e⟩ => by rw [e, e2, e1]; exact h a b⟩

theorem dtrue_someNeq : DTrue d0 I (someNeqD : DF) ↔ ∃ a b : D, a ≠ b :=
  ⟨fun ⟨F1, F2, h⟩ => ⟨F1 [], F2 ([] ++ [F1 []]), h _ ⟨[] ++ [F1 []], ⟨[], rfl, rfl⟩, rfl⟩⟩,
   fun ⟨a, b, hne⟩ => ⟨fun _ => a, fun _ => b, fun _ ⟨_, ⟨_, e1, e2⟩, e⟩ => by rw [e, e2, e1]; exact hne⟩⟩

/-- ESO → dependence logic, uniformly in the structure -/
theorem eso_to_d (K : Classical') (θ : EF) (hc : ScE θ 0) : ETrue d0 I θ ↔ DTrue d0 I (eso2d θ) := by
  let N := nnf θ true
  let p := (pre N 0).1
  let M := (pre N 0).2
  have hN : ScN N 0 := sc_nnf θ 0 hc true
  have hq : QF M := pre_qf N 0
  have hsM : ScN M (lenB p) := by have := pre_sc N 0 hN; rw [Nat.zero_add] at this; exact this
  have E1 : ETrue d0 I θ ↔ ∃ J : SVal D, QP p [] (fun v => semN d0 I J v M) :=
    ⟨fun ⟨J, h⟩ => ⟨J, (pre_ok d0 I K J N [] hN).2 ((nnf_ok d0 I K J θ []).1.2 h)⟩,
     fun ⟨J, h⟩ => ⟨J, (nnf_ok d0 I K J θ []).1.1 ((pre_ok d0 I K J N [] hN).1 h)⟩⟩
  refine E1.trans (Iff.trans ?_ (dtrue_or d0 I _ _).symm)
  refine (K.lem (∀ a b : D, a = b)).elim (fun hD => ?_) (fun hD => ?_)
  · refine (one_elem d0 I K hD p M hq).trans ⟨fun h => Or.inl ⟨(dtrue_allEq d0 I).2 hD, (phi1_ok d0 I K p M hq).2 h⟩,
      fun h => h.elim (fun ⟨_, h1⟩ => (phi1_ok d0 I K p M hq).1 h1)
        (fun ⟨hn, _⟩ => match (dtrue_someNeq d0 I).1 hn with
          | ⟨a, b, ne⟩ => absurd (hD a b) ne)⟩
  · have ex2 : ∃ a b : D, a ≠ b := dne K _ (fun hn => hD (fun a b => dne K _ (fun ne => hn ⟨a, b, ne⟩)))
    match ex2 with
    | ⟨a, b, hab⟩ =>
      have hd1 : ∃ d1 : D, d1 ≠ d0 := (K.lem (a = d0)).elim
        (fun e => ⟨b, fun eb => hab (e.trans eb.symm)⟩) (fun ne => ⟨a, ne⟩)
      match hd1 with
      | ⟨d1, hne⟩ =>
        exact ⟨fun ⟨J, h⟩ => Or.inr ⟨(dtrue_someNeq d0 I).2 ex2, phi2_intro d0 I K d1 hne p M hq hsM J h⟩,
          fun h => h.elim (fun ⟨ha, _⟩ => absurd ((dtrue_allEq d0 I).1 ha) hD)
            (fun ⟨_, h2⟩ => phi2_elim d0 I K p M hq hsM h2)⟩

/-- Väänänen: dependence logic and existential second-order logic define the same classes of
  structures — every dependence sentence has an ESO sentence with the same models and conversely,
  uniformly in the structure (D, d0, I) -/
theorem vaananen (K : Classical') :
    (∀ φ : DF, WFd φ 0 → ∀ (D : Type) (d0 : D) (I : Nat → List D → Prop), DTrue d0 I φ ↔ ETrue d0 I (toESO φ)) ∧
    (∀ θ : EF, ScE θ 0 → ∀ (D : Type) (d0 : D) (I : Nat → List D → Prop), ETrue d0 I θ ↔ DTrue d0 I (eso2d θ)) :=
  ⟨fun φ hw _ d0 I => d_to_eso d0 I K φ hw, fun θ hc _ d0 I => eso_to_d d0 I K θ hc⟩

end DE

end Found

#print axioms Found.DE.substB_ok
#print axioms Found.DE.substB_noS
#print axioms Found.DE.sem_pfx
#print axioms Found.DE.one_elem
#print axioms Found.DE.dtrue_or
#print axioms Found.DE.dtrue_orL
#print axioms Found.DE.phi1_ok
#print axioms Found.DE.dtrue_allEq
#print axioms Found.DE.dtrue_someNeq
#print axioms Found.DE.eso_to_d
#print axioms Found.DE.vaananen

found/Dependence.lean

lean_all/found/Dependence.lean

/-
  T175. found/Dependence — dependence (and IF) logic as a point: team
  semantics, truth of a formula on a set of assignments (a team).
  * Dependence atom =(x, y): on the team, the value of x determines y.
  * Downward closure for the atom, conjunction and the split disjunction.
  * Not flat: =(x, y) holds on every singleton team but not on their union
    (countermodel) — the line to first-order logic is "flat formulas".

  Lean 4 core only, hand-written proofs, no axioms.
-/

namespace Found

namespace Team

universe u

variable {D : Type u}

def Team (D : Type u) : Type u := (Nat → D) → Prop

def dep (x y : Nat) (T : Team D) : Prop := ∀ s s', T s → T s' → s x = s' x → s y = s' y

def sub (T T' : Team D) : Prop := ∀ s, T s → T' s

theorem dep_down {x y : Nat} {T T' : Team D} (h : sub T T') (d : dep x y T') : dep x y T :=
  fun s s' hs hs' => d s s' (h s hs) (h s' hs')

/-- split (tensor) disjunction -/
def tensor (P Q : Team D → Prop) (T : Team D) : Prop :=
  ∃ T₁ T₂ : Team D, (∀ s, T s → T₁ s ∨ T₂ s) ∧ P T₁ ∧ Q T₂

def Down (P : Team D → Prop) : Prop := ∀ T T', sub T T' → P T' → P T

theorem tensor_down (P Q : Team D → Prop) (hP : Down P) (hQ : Down Q) : Down (tensor P Q) :=
  fun T _ h ⟨T₁, T₂, cov, p, q⟩ =>
    ⟨fun s => T s ∧ T₁ s, fun s => T s ∧ T₂ s,
     fun s hs => (cov s (h s hs)).elim (fun a => Or.inl ⟨hs, a⟩) (fun b => Or.inr ⟨hs, b⟩),
     hP _ _ (fun _ a => a.2) p, hQ _ _ (fun _ a => a.2) q⟩

def single (s0 : Nat → Bool) : Team Bool := fun s => ∀ n, s n = s0 n

theorem dep_single (s0 : Nat → Bool) (x y : Nat) : dep x y (single s0) :=
  fun _ _ hs hs' _ => (hs y).trans (hs' y).symm

/-- not flat: two singletons satisfy =(0,1), their union does not -/
theorem not_flat :
    ¬ dep 0 1 (fun s => single (fun _ => false) s ∨ single (fun n => match n with | 1 => true | _ => false) s) :=
  fun h => Bool.noConfusion (h (fun _ => false) (fun n => match n with | 1 => true | _ => false)
    (Or.inl (fun _ => rfl)) (Or.inr (fun _ => rfl)) rfl)

end Team

end Found

#print axioms Found.Team.dep_down
#print axioms Found.Team.tensor_down
#print axioms Found.Team.dep_single
#print axioms Found.Team.not_flat

found/Description.lean

lean_all/found/Description.lean

/-
  T176. found/Description — description logic ALC as a point: concepts are
  predicates on a domain, roles are values of the relation coordinate.
  The line ALC ↔ modal K (Schild): the translation ∃R.C ↦ ◇C, ∀R.C ↦ □C,
  ¬C ↦ C → ⊥ preserves extensions exactly (one role; several roles are the
  multimodal case).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Modal

namespace Found

namespace DL

universe u

inductive Concept where
  | atom : Nat → Concept
  | top : Concept
  | neg : Concept → Concept
  | and : Concept → Concept → Concept
  | or : Concept → Concept → Concept
  | some : Concept → Concept     -- ∃R.C
  | all : Concept → Concept      -- ∀R.C

variable {D : Type u} (R : D → D → Prop) (A : Nat → D → Prop)

def ext : Concept → D → Prop
  | .atom n, x => A n x
  | .top, _ => True
  | .neg c, x => ¬ ext c x
  | .and c d, x => ext c x ∧ ext d x
  | .or c d, x => ext c x ∨ ext d x
  | .some c, x => ∃ y, R x y ∧ ext c y
  | .all c, x => ∀ y, R x y → ext c y

def tr : Concept → Modal.MForm
  | .atom n => .var n
  | .top => .imp .bot .bot
  | .neg c => .imp (tr c) .bot
  | .and c d => .and (tr c) (tr d)
  | .or c d => .or (tr c) (tr d)
  | .some c => .dia (tr c)
  | .all c => .box (tr c)

theorem tr_correct : (c : Concept) → ∀ x, ext R A c x ↔ Modal.sat R (fun w n => A n w) x (tr c)
  | .atom _, _ => Iff.rfl
  | .top, _ => ⟨fun _ h => h, fun _ => trivial⟩
  | .neg c, x => ⟨fun h hc => h ((tr_correct c x).mpr hc), fun h hc => h ((tr_correct c x).mp hc)⟩
  | .and c d, x => ⟨fun h => ⟨(tr_correct c x).mp h.1, (tr_correct d x).mp h.2⟩,
                    fun h => ⟨(tr_correct c x).mpr h.1, (tr_correct d x).mpr h.2⟩⟩
  | .or c d, x => ⟨fun h => h.elim (fun a => Or.inl ((tr_correct c x).mp a)) (fun b => Or.inr ((tr_correct d x).mp b)),
                   fun h => h.elim (fun a => Or.inl ((tr_correct c x).mpr a)) (fun b => Or.inr ((tr_correct d x).mpr b))⟩
  | .some c, _ => ⟨fun ⟨y, r, h⟩ => ⟨y, r, (tr_correct c y).mp h⟩, fun ⟨y, r, h⟩ => ⟨y, r, (tr_correct c y).mpr h⟩⟩
  | .all c, _ => ⟨fun h y r => (tr_correct c y).mp (h y r), fun h y r => (tr_correct c y).mpr (h y r)⟩

/-- subsumption C ⊑ D is K-validity of the translated implication on the frame -/
theorem subsumption_iff (c d : Concept) :
    (∀ x, ext R A c x → ext R A d x) ↔ ∀ x, Modal.sat R (fun w n => A n w) x (.imp (tr c) (tr d)) :=
  ⟨fun h x hc => (tr_correct R A d x).mp (h x ((tr_correct R A c x).mpr hc)),
   fun h x hc => (tr_correct R A d x).mpr (h x ((tr_correct R A c x).mp hc))⟩

end DL

end Found

#print axioms Found.DL.tr_correct
#print axioms Found.DL.subsumption_iff

found/DialLL.lean

lean_all/found/DialLL.lean

/-
  T180, line Dialectica <-> linear logic. found/DialLL — de Paiva's
  Dialectica category Dial (found/Dialectica) is a model of intuitionistic
  linear logic:
  * tensor A ⊗ B = (U × V, X × Y, α ∧ β), unit I = (1, 1, True), a
    bifunctor (identities and composites preserved);
  * associator, unitors and symmetry are isomorphisms (both composites are
    identities), the symmetry is involutive;
  * closed: A ⊸ B = ((U → V) × (U → Y → X), U × Y, α(u, F u y) → β(f u, y)),
    and Hom(A ⊗ B, C) ≅ Hom(A, B ⊸ C), both round trips identities,
    natural in A; evaluation (A ⊸ B) ⊗ A → B;
  * additive conjunction A & B = (U × V, X ⊕ Y) is a cartesian product
    (projections, pairing, uniqueness);
  * the exponential !A = (U, U → X, α(u, φ u)) is a comonad (counit,
    comultiplication, the three comonad laws) and !A → !A ⊗ !A (copy) and
    !A → I (discard) exist — contraction and weakening for ! formulas.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Dialectica

namespace Found

namespace DialLL

open Dial

universe u

abbrev H (A B : Obj.{u}) : Type u := Hom A B

/-- equality of Dial morphisms (pointwise) -/
def E {A B : Obj.{u}} (h k : H A B) : Prop := Dial.{u}.heq h k

def cmp {A B C : Obj.{u}} (h : H A B) (k : H B C) : H A C := Dial.{u}.comp h k
def idh (A : Obj.{u}) : H A A := Dial.{u}.id A

/-! ## tensor and unit -/

def tens (A B : Obj.{u}) : Obj.{u} := ⟨A.U × B.U, A.X × B.X, fun p x => A.α p.1 x.1 ∧ B.α p.2 x.2⟩

def unit : Obj.{u} := ⟨PUnit, PUnit, fun _ _ => True⟩

def tmap {A A' B B' : Obj.{u}} (h : H A A') (k : H B B') : H (tens A B) (tens A' B') :=
  ⟨fun p => (h.f p.1, k.f p.2), fun p x => (h.F p.1 x.1, k.F p.2 x.2),
   fun p x c => ⟨h.cond p.1 x.1 c.1, k.cond p.2 x.2 c.2⟩⟩

theorem tmap_id (A B : Obj.{u}) : E (tmap (idh A) (idh B)) (idh (tens A B)) :=
  ⟨fun _ => rfl, fun _ _ => rfl⟩

theorem tmap_comp {A A' A'' B B' B'' : Obj.{u}} (h : H A A') (h' : H A' A'') (k : H B B') (k' : H B' B'') :
    E (tmap (cmp h h') (cmp k k')) (cmp (tmap h k) (tmap h' k')) :=
  ⟨fun _ => rfl, fun _ _ => rfl⟩

theorem tmap_resp {A A' B B' : Obj.{u}} {h h' : H A A'} {k k' : H B B'} (eh : E h h') (ek : E k k') :
    E (tmap h k) (tmap h' k') :=
  ⟨fun p => by
    show (h.f p.1, k.f p.2) = (h'.f p.1, k'.f p.2)
    rw [eh.1, ek.1],
   fun p x => by
    show (h.F p.1 x.1, k.F p.2 x.2) = (h'.F p.1 x.1, k'.F p.2 x.2)
    rw [eh.2, ek.2]⟩

/-! ## coherence isomorphisms -/

def assoc (A B C : Obj.{u}) : H (tens (tens A B) C) (tens A (tens B C)) :=
  ⟨fun p => (p.1.1, (p.1.2, p.2)), fun _ x => ((x.1, x.2.1), x.2.2), fun _ _ c => ⟨c.1.1, c.1.2, c.2⟩⟩

def assocInv (A B C : Obj.{u}) : H (tens A (tens B C)) (tens (tens A B) C) :=
  ⟨fun p => ((p.1, p.2.1), p.2.2), fun _ x => (x.1.1, (x.1.2, x.2)), fun _ _ c => ⟨⟨c.1, c.2.1⟩, c.2.2⟩⟩

theorem assoc_iso1 (A B C : Obj.{u}) : E (cmp (assoc A B C) (assocInv A B C)) (idh _) := ⟨fun _ => rfl, fun _ _ => rfl⟩
theorem assoc_iso2 (A B C : Obj.{u}) : E (cmp (assocInv A B C) (assoc A B C)) (idh _) := ⟨fun _ => rfl, fun _ _ => rfl⟩

def lunit (A : Obj.{u}) : H (tens unit A) A := ⟨fun p => p.2, fun _ x => (PUnit.unit, x), fun _ _ c => c.2⟩
def lunitInv (A : Obj.{u}) : H A (tens unit A) := ⟨fun u => (PUnit.unit, u), fun _ x => x.2, fun _ _ c => ⟨trivial, c⟩⟩

theorem lunit_iso1 (A : Obj.{u}) : E (cmp (lunit A) (lunitInv A)) (idh _) := ⟨fun _ => rfl, fun _ _ => rfl⟩
theorem lunit_iso2 (A : Obj.{u}) : E (cmp (lunitInv A) (lunit A)) (idh _) := ⟨fun _ => rfl, fun _ _ => rfl⟩

def runit (A : Obj.{u}) : H (tens A unit) A := ⟨fun p => p.1, fun _ x => (x, PUnit.unit), fun _ _ c => c.1⟩
def runitInv (A : Obj.{u}) : H A (tens A unit) := ⟨fun u => (u, PUnit.unit), fun _ x => x.1, fun _ _ c => ⟨c, trivial⟩⟩

theorem runit_iso1 (A : Obj.{u}) : E (cmp (runit A) (runitInv A)) (idh _) := ⟨fun _ => rfl, fun _ _ => rfl⟩
theorem runit_iso2 (A : Obj.{u}) : E (cmp (runitInv A) (runit A)) (idh _) := ⟨fun _ => rfl, fun _ _ => rfl⟩

def swap (A B : Obj.{u}) : H (tens A B) (tens B A) :=
  ⟨fun p => (p.2, p.1), fun _ x => (x.2, x.1), fun _ _ c => ⟨c.2, c.1⟩⟩

theorem swap_invol (A B : Obj.{u}) : E (cmp (swap A B) (swap B A)) (idh _) := ⟨fun _ => rfl, fun _ _ => rfl⟩

/-- the symmetry is natural -/
theorem swap_nat {A A' B B' : Obj.{u}} (h : H A A') (k : H B B') :
    E (cmp (tmap h k) (swap A' B')) (cmp (swap A B) (tmap k h)) := ⟨fun _ => rfl, fun _ _ => rfl⟩

/-- the pentagon-free part of coherence: the triangle identity -/
theorem triangle (A B : Obj.{u}) :
    E (cmp (assoc A unit B) (tmap (idh A) (lunit B))) (tmap (runit A) (idh B)) := ⟨fun _ => rfl, fun _ _ => rfl⟩

theorem pentagon (A B C D : Obj.{u}) :
    E (cmp (cmp (tmap (assoc A B C) (idh D)) (assoc A (tens B C) D)) (tmap (idh A) (assoc B C D)))
      (cmp (assoc (tens A B) C D) (assoc A B (tens C D))) := ⟨fun _ => rfl, fun _ _ => rfl⟩

/-! ## closed structure -/

def lolli (A B : Obj.{u}) : Obj.{u} :=
  ⟨(A.U → B.U) × (A.U → B.X → A.X), A.U × B.X, fun g p => A.α p.1 (g.2 p.1 p.2) → B.α (g.1 p.1) p.2⟩

def curry {A B C : Obj.{u}} (h : H (tens A B) C) : H A (lolli B C) :=
  ⟨fun u => (fun v => h.f (u, v), fun v z => (h.F (u, v) z).2), fun u p => (h.F (u, p.1) p.2).1,
   fun u p a b => h.cond (u, p.1) p.2 ⟨a, b⟩⟩

def uncurry {A B C : Obj.{u}} (g : H A (lolli B C)) : H (tens A B) C :=
  ⟨fun p => (g.f p.1).1 p.2, fun p z => (g.F p.1 (p.2, z), (g.f p.1).2 p.2 z),
   fun p z c => g.cond p.1 (p.2, z) c.1 c.2⟩

theorem uncurry_curry {A B C : Obj.{u}} (h : H (tens A B) C) : E (uncurry (curry h)) h :=
  ⟨fun _ => rfl, fun _ _ => rfl⟩

theorem curry_uncurry {A B C : Obj.{u}} (g : H A (lolli B C)) : E (curry (uncurry g)) g :=
  ⟨fun _ => rfl, fun _ _ => rfl⟩

/-- naturality of currying in A -/
theorem curry_nat {A' A B C : Obj.{u}} (a : H A' A) (h : H (tens A B) C) :
    E (curry (cmp (tmap a (idh B)) h)) (cmp a (curry h)) := ⟨fun _ => rfl, fun _ _ => rfl⟩

def ev (A B : Obj.{u}) : H (tens (lolli A B) A) B := uncurry (idh (lolli A B))

/-- every morphism factors through evaluation -/
theorem ev_universal {A B C : Obj.{u}} (h : H (tens C A) B) : E (cmp (tmap (curry h) (idh A)) (ev A B)) h :=
  ⟨fun _ => rfl, fun _ _ => rfl⟩

/-! ## with (&): cartesian product -/

def wth (A B : Obj.{u}) : Obj.{u} :=
  ⟨A.U × B.U, A.X ⊕ B.X, fun p x => match x with | .inl a => A.α p.1 a | .inr b => B.α p.2 b⟩

def fst (A B : Obj.{u}) : H (wth A B) A := ⟨fun p => p.1, fun _ x => .inl x, fun _ _ c => c⟩
def snd (A B : Obj.{u}) : H (wth A B) B := ⟨fun p => p.2, fun _ x => .inr x, fun _ _ c => c⟩

def pairH {C A B : Obj.{u}} (h : H C A) (k : H C B) : H C (wth A B) :=
  ⟨fun w => (h.f w, k.f w), fun w x => match x with | .inl a => h.F w a | .inr b => k.F w b,
   fun w x c => match x, c with
     | .inl a, c => h.cond w a c
     | .inr b, c => k.cond w b c⟩

theorem fst_pair {C A B : Obj.{u}} (h : H C A) (k : H C B) : E (cmp (pairH h k) (fst A B)) h :=
  ⟨fun _ => rfl, fun _ _ => rfl⟩
theorem snd_pair {C A B : Obj.{u}} (h : H C A) (k : H C B) : E (cmp (pairH h k) (snd A B)) k :=
  ⟨fun _ => rfl, fun _ _ => rfl⟩

theorem pair_unique {C A B : Obj.{u}} (m : H C (wth A B)) :
    E (pairH (cmp m (fst A B)) (cmp m (snd A B))) m :=
  ⟨fun _ => rfl, fun _ x => match x with
    | .inl _ => rfl
    | .inr _ => rfl⟩

/-! ## the exponential ! (finite lists of counterexamples) -/

def All {X : Type u} (p : X → Prop) : List X → Prop
  | [] => True
  | x :: l => p x ∧ All p l

def app {X : Type u} : List X → List X → List X
  | [], m => m
  | x :: l, m => x :: app l m

theorem app_nil {X : Type u} : (l : List X) → app l [] = l
  | [] => rfl
  | x :: l => congrArg (List.cons x) (app_nil l)

theorem app_assoc {X : Type u} : (l m n : List X) → app (app l m) n = app l (app m n)
  | [], _, _ => rfl
  | x :: l, m, n => congrArg (List.cons x) (app_assoc l m n)

theorem all_app {X : Type u} (p : X → Prop) : (l m : List X) → All p (app l m) → All p l ∧ All p m
  | [], _, h => ⟨trivial, h⟩
  | _ :: l, m, h => match all_app p l m h.2 with
    | ⟨a, b⟩ => ⟨⟨h.1, a⟩, b⟩

def lmap {X Y : Type u} (g : X → Y) : List X → List Y
  | [] => []
  | x :: l => g x :: lmap g l

theorem all_map {X Y : Type u} (p : Y → Prop) (q : X → Prop) (g : X → Y) (hg : ∀ x, p (g x) → q x) :
    (l : List X) → All p (lmap g l) → All q l
  | [], _ => trivial
  | _ :: l, h => ⟨hg _ h.1, all_map p q g hg l h.2⟩

theorem lmap_id {X : Type u} : (l : List X) → lmap (fun x => x) l = l
  | [] => rfl
  | x :: l => congrArg (List.cons x) (lmap_id l)

theorem lmap_comp {X Y Z : Type u} (g : X → Y) (k : Y → Z) : (l : List X) → lmap k (lmap g l) = lmap (fun x => k (g x)) l
  | [] => rfl
  | x :: l => congrArg (List.cons (k (g x))) (lmap_comp g k l)

/-- !A = (U, U → X*, α(u, x) for every x in the list at u) -/
def bang (A : Obj.{u}) : Obj.{u} := ⟨A.U, A.U → List A.X, fun u φ => All (A.α u) (φ u)⟩

def bmap {A B : Obj.{u}} (h : H A B) : H (bang A) (bang B) :=
  ⟨h.f, fun _ ψ v => lmap (h.F v) (ψ (h.f v)), fun u _ c => all_map _ _ (h.F u) (h.cond u) _ c⟩

/-- equality of morphisms out of a ! object, pointwise in the list component -/
def EB {A B : Obj.{u}} (h k : H (bang A) B) : Prop := (∀ u, h.f u = k.f u) ∧ ∀ u y v, h.F u y v = k.F u y v

theorem bmap_id (A : Obj.{u}) : (∀ u, (bmap (idh A)).f u = u) ∧ ∀ u ψ v, (bmap (idh A)).F u ψ v = ψ v :=
  ⟨fun _ => rfl, fun _ ψ v => lmap_id (ψ v)⟩

theorem bmap_comp {A B C : Obj.{u}} (h : H A B) (k : H B C) :
    (∀ u, (bmap (cmp h k)).f u = (cmp (bmap h) (bmap k)).f u) ∧
    ∀ u ψ v, (bmap (cmp h k)).F u ψ v = (cmp (bmap h) (bmap k)).F u ψ v :=
  ⟨fun _ => rfl, fun _ ψ v => (lmap_comp (k.F (h.f v)) (h.F v) (ψ (k.f (h.f v)))).symm⟩

def counit (A : Obj.{u}) : H (bang A) A := ⟨fun u => u, fun _ x _ => [x], fun _ _ c => c.1⟩

def joinMap {U X : Type u} (v : U) : List (U → List X) → List X
  | [] => []
  | φ :: L => app (φ v) (joinMap v L)

theorem all_join {U X : Type u} (p : X → Prop) (v : U) :
    (L : List (U → List X)) → All p (joinMap v L) → All (fun φ => All p (φ v)) L
  | [], _ => trivial
  | φ :: L, h => match all_app p (φ v) (joinMap v L) h with
    | ⟨a, b⟩ => ⟨a, all_join p v L b⟩

theorem join_app {U X : Type u} (v : U) : (L M : List (U → List X)) →
    joinMap v (app L M) = app (joinMap v L) (joinMap v M)
  | [], _ => rfl
  | φ :: L, M => by
    show app (φ v) (joinMap v (app L M)) = app (app (φ v) (joinMap v L)) (joinMap v M)
    rw [join_app v L M, app_assoc]

def comult (A : Obj.{u}) : H (bang A) (bang (bang A)) :=
  ⟨fun u => u, fun _ Φ v => joinMap v (Φ v), fun u Φ c => all_join (A.α u) u (Φ u) c⟩

theorem comonad_left (A : Obj.{u}) : EB (cmp (comult A) (counit (bang A))) (idh (bang A)) :=
  ⟨fun _ => rfl, fun _ x v => app_nil (x v)⟩

theorem join_single {U X : Type u} (v : U) : (l : List X) → joinMap v (lmap (fun x (_ : U) => [x]) l) = l
  | [] => rfl
  | x :: l => congrArg (List.cons x) (join_single v l)

theorem comonad_right (A : Obj.{u}) : EB (cmp (comult A) (bmap (counit A))) (idh (bang A)) :=
  ⟨fun _ => rfl, fun _ x v => join_single v (x v)⟩

theorem join_join {U X : Type u} (v : U) : (L : List (U → List (U → List X))) →
    joinMap v (joinMap v L) = joinMap v (lmap (fun Φ (w : U) => joinMap w (Φ w)) L)
  | [] => rfl
  | Φ :: L => by
    show joinMap v (app (Φ v) (joinMap v L)) = app (joinMap v (Φ v)) (joinMap v (lmap (fun Φ (w : U) => joinMap w (Φ w)) L))
    rw [join_app, join_join v L]

theorem comonad_assoc (A : Obj.{u}) :
    (∀ u, (cmp (comult A) (comult (bang A))).f u = (cmp (comult A) (bmap (comult A))).f u) ∧
    ∀ u Ψ v, (cmp (comult A) (comult (bang A))).F u Ψ v = (cmp (comult A) (bmap (comult A))).F u Ψ v :=
  ⟨fun _ => rfl, fun _ Ψ v => join_join v (Ψ v)⟩

/-- contraction for ! formulas -/
def copy (A : Obj.{u}) : H (bang A) (tens (bang A) (bang A)) :=
  ⟨fun u => (u, u), fun _ x v => app (x.1 v) (x.2 v), fun u x c => all_app (A.α u) (x.1 u) (x.2 u) c⟩

/-- weakening for ! formulas -/
def discard (A : Obj.{u}) : H (bang A) unit := ⟨fun _ => PUnit.unit, fun _ _ _ => [], fun _ _ _ => trivial⟩

/-- dereliction then copying: both copies of !A are derelicted to A ⊗ A -/
theorem copy_counit (A : Obj.{u}) :
    (∀ u, (cmp (copy A) (tmap (counit A) (counit A))).f u = (u, u)) := fun _ => rfl

end DialLL

end Found

#print axioms Found.DialLL.tmap_id
#print axioms Found.DialLL.tmap_comp
#print axioms Found.DialLL.tmap_resp
#print axioms Found.DialLL.assoc_iso1
#print axioms Found.DialLL.assoc_iso2
#print axioms Found.DialLL.lunit_iso1
#print axioms Found.DialLL.lunit_iso2
#print axioms Found.DialLL.runit_iso1
#print axioms Found.DialLL.runit_iso2
#print axioms Found.DialLL.swap_invol
#print axioms Found.DialLL.swap_nat
#print axioms Found.DialLL.triangle
#print axioms Found.DialLL.pentagon
#print axioms Found.DialLL.uncurry_curry
#print axioms Found.DialLL.curry_uncurry
#print axioms Found.DialLL.curry_nat
#print axioms Found.DialLL.ev_universal
#print axioms Found.DialLL.fst_pair
#print axioms Found.DialLL.snd_pair
#print axioms Found.DialLL.pair_unique
#print axioms Found.DialLL.all_app
#print axioms Found.DialLL.all_map
#print axioms Found.DialLL.bmap_id
#print axioms Found.DialLL.bmap_comp
#print axioms Found.DialLL.all_join
#print axioms Found.DialLL.join_app
#print axioms Found.DialLL.comonad_left
#print axioms Found.DialLL.comonad_right
#print axioms Found.DialLL.comonad_assoc
#print axioms Found.DialLL.copy
#print axioms Found.DialLL.discard

found/Dialectica.lean

lean_all/found/Dialectica.lean

/-
  T180. found/Dialectica — Gödel's Dialectica interpretation as a point:
  de Paiva's Dialectica category Dial is an instance of the category
  parameter (setoid homs, pointwise).
  * Objects (U, X, α ⊆ U × X): a formula ∃u ∀x α(u, x) in Dialectica form.
  * Morphisms (f : U → V, F : U → Y → X) with α(u, F u y) → β(f u, y):
    exactly the Dialectica translation of implication (∃f,F ∀u,y).
  * Dial is an E-category; it is a model of linear logic (cited), the line
    Dialectica ↔ linear logic (T68); modified realizability is the
    analogous construction with realizers (T96).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Base

namespace Found

namespace Dial

universe u

structure Obj where
  U : Type u
  X : Type u
  α : U → X → Prop

structure Hom (A B : Obj.{u}) where
  f : A.U → B.U
  F : A.U → B.X → A.X
  cond : ∀ u y, A.α u (F u y) → B.α (f u) y

def Dial : ECat.{u + 1, u} where
  Ob := Obj.{u}
  Hom := Hom
  heq h k := (∀ u, h.f u = k.f u) ∧ ∀ u y, h.F u y = k.F u y
  hrefl _ := ⟨fun _ => rfl, fun _ _ => rfl⟩
  hsymm h := ⟨fun u => (h.1 u).symm, fun u y => (h.2 u y).symm⟩
  htrans h k := ⟨fun u => (h.1 u).trans (k.1 u), fun u y => (h.2 u y).trans (k.2 u y)⟩
  id _ := ⟨fun u => u, fun _ x => x, fun _ _ h => h⟩
  comp h k := ⟨fun u => k.f (h.f u), fun u z => h.F u (k.F (h.f u) z),
    fun u z a => k.cond (h.f u) z (h.cond u (k.F (h.f u) z) a)⟩
  comp_resp {_ _ _ h h' k k'} e e' :=
    ⟨fun u => (congrArg k.f (e.1 u)).trans (e'.1 (h'.f u)),
     fun u z => (congrArg (h.F u) ((congrArg (fun v => k.F v z) (e.1 u)).trans (e'.2 (h'.f u) z))).trans
       (e.2 u (k'.F (h'.f u) z))⟩
  id_comp _ := ⟨fun _ => rfl, fun _ _ => rfl⟩
  comp_id _ := ⟨fun _ => rfl, fun _ _ => rfl⟩
  assoc _ _ _ := ⟨fun _ => rfl, fun _ _ => rfl⟩

/-- the Dialectica reading of a morphism: ∃ f F, ∀ u y, α(u, F u y) → β(f u, y) -/
theorem hom_is_dialectica (A B : Obj.{u}) :
    Nonempty (Hom A B) ↔ ∃ (f : A.U → B.U) (F : A.U → B.X → A.X), ∀ u y, A.α u (F u y) → B.α (f u) y :=
  ⟨fun ⟨h⟩ => ⟨h.f, h.F, h.cond⟩, fun ⟨f, F, c⟩ => ⟨⟨f, F, c⟩⟩⟩

end Dial

end Found

#print axioms Found.Dial.Dial
#print axioms Found.Dial.hom_is_dialectica

found/Domain.lean

lean_all/found/Domain.lean

/-
  T116. found/Domain — Scott domain theory as a point: ω-chain complete
  pointed preorders (a free parameter) and the Kleene fixed point theorem.

  * ωCPO: preorder with ⊥ and a sup for every increasing chain ℕ → D.
  * Scott-continuous maps: monotone and preserving sups of chains.
  * Kleene: for continuous f, fix f := sup (fⁿ ⊥) is a fixed point (up to ≤
    both ways) and below every pre-fixed point — the least fixed point.
  * This is the semantics of recursion used by PCF (T144) and by the λ-models
    of T78 (D ≅ D^D) — lines to computation.

  Lean 4 core only, hand-written proofs, no axioms.
-/

namespace Found

namespace Dom

universe u

structure CPO where
  D : Type u
  le : D → D → Prop
  le_refl : ∀ a, le a a
  le_trans : ∀ {a b c}, le a b → le b c → le a c
  bot : D
  bot_le : ∀ a, le bot a
  sup : (Nat → D) → D
  sup_ub : ∀ (c : Nat → D), (∀ n, le (c n) (c (n + 1))) → ∀ n, le (c n) (sup c)
  sup_least : ∀ (c : Nat → D), (∀ n, le (c n) (c (n + 1))) → ∀ x, (∀ n, le (c n) x) → le (sup c) x

variable (P : CPO.{u})

structure Cont where
  f : P.D → P.D
  mono : ∀ {a b}, P.le a b → P.le (f a) (f b)
  cont : ∀ (c : Nat → P.D), (∀ n, P.le (c n) (c (n + 1))) → P.le (f (P.sup c)) (P.sup (fun n => f (c n)))

variable {P}

def iter (g : Cont P) : Nat → P.D
  | 0 => P.bot
  | n + 1 => g.f (iter g n)

theorem iter_chain (g : Cont P) : ∀ n, P.le (iter g n) (iter g (n + 1))
  | 0 => P.bot_le _
  | n + 1 => g.mono (iter_chain g n)

def fix (g : Cont P) : P.D := P.sup (iter g)

theorem fix_le (g : Cont P) : P.le (g.f (fix g)) (fix g) :=
  P.le_trans (g.cont _ (iter_chain g))
    (P.sup_least _ (fun n => g.mono (iter_chain g n)) _ (fun n => P.sup_ub _ (iter_chain g) (n + 1)))

theorem le_fix (g : Cont P) : P.le (fix g) (g.f (fix g)) :=
  P.sup_least _ (iter_chain g) _ (fun n => match n with
    | 0 => P.bot_le _
    | n + 1 => g.mono (P.sup_ub _ (iter_chain g) n))

/-- least among pre-fixed points -/
theorem fix_least (g : Cont P) (x : P.D) (hx : P.le (g.f x) x) : P.le (fix g) x :=
  P.sup_least _ (iter_chain g) x (fun n => below n)
where
  below : ∀ n, P.le (iter g n) x
    | 0 => P.bot_le x
    | n + 1 => P.le_trans (g.mono (below n)) hx

end Dom

end Found

#print axioms Found.Dom.iter_chain
#print axioms Found.Dom.fix_le
#print axioms Found.Dom.le_fix
#print axioms Found.Dom.fix_least

found/DoubleCat.lean

lean_all/found/DoubleCat.lean

/-
  T193 line. found/DoubleCat — double categories as a point: objects,
  horizontal and vertical arrows (two category structures on the same
  objects) and squares with horizontal and vertical pasting, unital and
  satisfying the interchange law, up to a square equivalence.
  * Sq(C): every E-category C gives the double category of commutative
    squares (Ehresmann's quintets of a 1-category), whose horizontal and
    vertical arrows are both arrows of C; pasting is the commutativity of
    the composite rectangle, proved by rewriting along associativity.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Base

namespace Found

namespace Dbl

universe u v w

structure DoubleCat where
  Ob : Type u
  H : Ob → Ob → Type v
  V : Ob → Ob → Type v
  hc : {a b c : Ob} → H a b → H b c → H a c
  vc : {a b c : Ob} → V a b → V b c → V a c
  hid : (a : Ob) → H a a
  vid : (a : Ob) → V a a
  /-- squares with top f, bottom g, left u, right v -/
  Sq : {a b c d : Ob} → H a b → H c d → V a c → V b d → Type w
  sqeq : {a b c d : Ob} → {f : H a b} → {g : H c d} → {u : V a c} → {v : V b d} →
    Sq f g u v → Sq f g u v → Prop
  hpaste : {a b c d e f' : Ob} → {f : H a b} → {f₂ : H b e} → {g : H c d} → {g₂ : H d f'} →
    {u : V a c} → {v : V b d} → {w' : V e f'} →
    Sq f g u v → Sq f₂ g₂ v w' → Sq (hc f f₂) (hc g g₂) u w'
  vpaste : {a b c d c' d' : Ob} → {f : H a b} → {g : H c d} → {k : H c' d'} →
    {u : V a c} → {v : V b d} → {u₂ : V c c'} → {v₂ : V d d'} →
    Sq f g u v → Sq g k u₂ v₂ → Sq f k (vc u u₂) (vc v v₂)
  interchange : ∀ {a b e c d f' c' d' e' : Ob}
    {f : H a b} {f₂ : H b e} {g : H c d} {g₂ : H d f'} {k : H c' d'} {k₂ : H d' e'}
    {u : V a c} {v : V b d} {w' : V e f'} {u₂ : V c c'} {v₂ : V d d'} {w₂ : V f' e'}
    (α : Sq f g u v) (β : Sq f₂ g₂ v w') (γ : Sq g k u₂ v₂) (δ : Sq g₂ k₂ v₂ w₂),
    sqeq (vpaste (hpaste α β) (hpaste γ δ)) (hpaste (vpaste α γ) (vpaste β δ))

variable (C : ECat.{u, v})

/-- a commutative square f ; v ≈ u ; g -/
structure CSq {a b c d : C.Ob} (f : C.Hom a b) (g : C.Hom c d) (u : C.Hom a c) (v : C.Hom b d) : Type where
  comm : C.heq (C.comp f v) (C.comp u g)

theorem h_comm {a b c d e f' : C.Ob} {f : C.Hom a b} {f₂ : C.Hom b e} {g : C.Hom c d} {g₂ : C.Hom d f'}
    {u : C.Hom a c} {v : C.Hom b d} {w : C.Hom e f'}
    (α : C.heq (C.comp f v) (C.comp u g)) (β : C.heq (C.comp f₂ w) (C.comp v g₂)) :
    C.heq (C.comp (C.comp f f₂) w) (C.comp u (C.comp g g₂)) :=
  C.htrans (C.assoc f f₂ w)
    (C.htrans (C.comp_resp (C.hrefl f) β)
      (C.htrans (C.hsymm (C.assoc f v g₂))
        (C.htrans (C.comp_resp α (C.hrefl g₂)) (C.assoc u g g₂))))

theorem v_comm {a b c d c' d' : C.Ob} {f : C.Hom a b} {g : C.Hom c d} {k : C.Hom c' d'}
    {u : C.Hom a c} {v : C.Hom b d} {u₂ : C.Hom c c'} {v₂ : C.Hom d d'}
    (α : C.heq (C.comp f v) (C.comp u g)) (γ : C.heq (C.comp g v₂) (C.comp u₂ k)) :
    C.heq (C.comp f (C.comp v v₂)) (C.comp (C.comp u u₂) k) :=
  C.htrans (C.hsymm (C.assoc f v v₂))
    (C.htrans (C.comp_resp α (C.hrefl v₂))
      (C.htrans (C.assoc u g v₂)
        (C.htrans (C.comp_resp (C.hrefl u) γ) (C.hsymm (C.assoc u u₂ k)))))

/-- the double category of commutative squares of C -/
def Sq' : DoubleCat.{u, v, 0} where
  Ob := C.Ob
  H := C.Hom
  V := C.Hom
  hc := C.comp
  vc := C.comp
  hid := C.id
  vid := C.id
  Sq f g u v := CSq C f g u v
  sqeq _ _ := True
  hpaste α β := ⟨h_comm C α.comm β.comm⟩
  vpaste α γ := ⟨v_comm C α.comm γ.comm⟩
  interchange _ _ _ _ := trivial

/-- identity squares: the horizontal identity square on a vertical arrow -/
def hidSq {a c : C.Ob} (u : C.Hom a c) : CSq C (C.id a) (C.id c) u u :=
  ⟨C.htrans (C.id_comp u) (C.hsymm (C.comp_id u))⟩

def vidSq {a b : C.Ob} (f : C.Hom a b) : CSq C f f (C.id a) (C.id b) :=
  ⟨C.htrans (C.comp_id f) (C.hsymm (C.id_comp f))⟩

end Dbl

end Found

#print axioms Found.Dbl.h_comm
#print axioms Found.Dbl.v_comm
#print axioms Found.Dbl.Sq'
#print axioms Found.Dbl.hidSq
#print axioms Found.Dbl.vidSq

found/Dynamic.lean

lean_all/found/Dynamic.lean

/-
  T138. found/Dynamic — dynamic and temporal logics as points: programs are
  values of the relation coordinate (closed under ;, ∪, *), time is the
  core's stream (coinduction, T59).

  * PDL: [α;β]p ⟺ [α][β]p, [α∪β]p ⟺ [α]p ∧ [β]p, [α*]p ⟺ p ∧ [α][α*]p
    (fixed point) and the induction rule for *.
  * LTL on paths ℕ → S: G p ⟺ p ∧ X G p, F p ⟺ p ∨ X F p, p U q unfolds;
    CTL's A/E over paths are the relation-coordinate box/diamond on paths
    (stated for the path relation).

  Lean 4 core only, hand-written proofs, no axioms.
-/

namespace Found

namespace Dyn

universe u

variable {S : Type u}

def Rel (S : Type u) : Type u := S → S → Prop

def seq (a b : Rel S) : Rel S := fun x z => ∃ y, a x y ∧ b y z
def alt (a b : Rel S) : Rel S := fun x y => a x y ∨ b x y

inductive star (a : Rel S) : Rel S where
  | refl (x : S) : star a x x
  | step {x y z : S} : a x y → star a y z → star a x z

def box (a : Rel S) (p : S → Prop) : S → Prop := fun x => ∀ y, a x y → p y

theorem box_seq (a b : Rel S) (p : S → Prop) (x : S) :
    box (seq a b) p x ↔ box a (box b p) x :=
  ⟨fun h y r z r' => h z ⟨y, r, r'⟩, fun h z ⟨y, r, r'⟩ => h y r z r'⟩

theorem box_alt (a b : Rel S) (p : S → Prop) (x : S) :
    box (alt a b) p x ↔ box a p x ∧ box b p x :=
  ⟨fun h => ⟨fun y r => h y (Or.inl r), fun y r => h y (Or.inr r)⟩,
   fun h y r => r.elim (h.1 y) (h.2 y)⟩

theorem box_star (a : Rel S) (p : S → Prop) (x : S) :
    box (star a) p x ↔ p x ∧ box a (box (star a) p) x :=
  ⟨fun h => ⟨h x (.refl x), fun y r z r' => h z (.step r r')⟩,
   fun h y r => by
     cases r with
     | refl => exact h.1
     | step r₁ r₂ => exact h.2 _ r₁ y r₂⟩

/-- the induction rule: an α-invariant true at x holds along α* -/
theorem star_induction (a : Rel S) (p : S → Prop) (inv : ∀ x, p x → box a p x) :
    ∀ x, p x → box (star a) p x := by
  intro x hx y r
  induction r with
  | refl => exact hx
  | step r₁ _ ih => exact ih (inv _ hx _ r₁)

/-! ## LTL on paths -/

def Path (S : Type u) : Type u := Nat → S

def shift (π : Path S) : Path S := fun n => π (n + 1)
def drop (π : Path S) : Nat → Path S
  | 0 => π
  | k + 1 => drop (shift π) k

def G (p : Path S → Prop) (π : Path S) : Prop := ∀ k, p (drop π k)
def F (p : Path S → Prop) (π : Path S) : Prop := ∃ k, p (drop π k)
def X (p : Path S → Prop) (π : Path S) : Prop := p (shift π)

theorem G_unfold (p : Path S → Prop) (π : Path S) : G p π ↔ p π ∧ X (G p) π :=
  ⟨fun h => ⟨h 0, fun k => h (k + 1)⟩,
   fun h k => match k with
     | 0 => h.1
     | k + 1 => h.2 k⟩

theorem F_unfold (p : Path S → Prop) (π : Path S) : F p π ↔ p π ∨ X (F p) π :=
  ⟨fun ⟨k, h⟩ => match k, h with
     | 0, h => Or.inl h
     | k + 1, h => Or.inr ⟨k, h⟩,
   fun h => h.elim (fun h0 => ⟨0, h0⟩) (fun ⟨k, hk⟩ => ⟨k + 1, hk⟩)⟩

/-- duality G ¬p ⟺ ¬ F p (constructive direction) -/
theorem G_not_F (p : Path S → Prop) (π : Path S) : G (fun σ => ¬ p σ) π ↔ ¬ F p π :=
  ⟨fun h ⟨k, hk⟩ => h k hk, fun h k hk => h ⟨k, hk⟩⟩

end Dyn

end Found

#print axioms Found.Dyn.box_seq
#print axioms Found.Dyn.box_alt
#print axioms Found.Dyn.box_star
#print axioms Found.Dyn.star_induction
#print axioms Found.Dyn.G_unfold
#print axioms Found.Dyn.F_unfold
#print axioms Found.Dyn.G_not_F

found/ETCC.lean

lean_all/found/ETCC.lean

/-
  T213. found/ETCC — Lawvere's elementary theory of the category of
  categories as a point of the chart: the arrows of the theory are functors
  between (small) categories, and the primitive notions of a category are
  read off from three test objects:
  * 1 (terminal): objects of C = functors 1 → C;
  * 2 (one arrow 0 → 1): arrows of C = functors 2 → C, with domain and
    codomain the restrictions to the two objects;
  * 3 (0 → 1 → 2): composable pairs = functors 3 → C, and composition is the
    image of the long arrow 0 → 2 (3 is the pushout 2 ⊔_1 2);
  * products of categories with pairing (finite limits), and discrete
    categories: functors between discrete categories are functions (ETCS,
    T107, inside ETCC as the discrete objects).

  Lean 4 core only, hand-written proofs, no axioms.
-/

namespace Found

namespace ETCC

universe u v

/-- small categories -/
structure Cat where
  Ob : Type u
  Hom : Ob → Ob → Type v
  id : (a : Ob) → Hom a a
  comp : {a b c : Ob} → Hom a b → Hom b c → Hom a c
  id_comp : ∀ {a b} (f : Hom a b), comp (id a) f = f
  comp_id : ∀ {a b} (f : Hom a b), comp f (id b) = f
  assoc : ∀ {a b c d} (f : Hom a b) (g : Hom b c) (h : Hom c d), comp (comp f g) h = comp f (comp g h)

/-- functors: the arrows of ETCC -/
structure Fun (C D : Cat.{u, v}) where
  obj : C.Ob → D.Ob
  map : {a b : C.Ob} → C.Hom a b → D.Hom (obj a) (obj b)
  map_id : ∀ a, map (C.id a) = D.id (obj a)
  map_comp : ∀ {a b c} (f : C.Hom a b) (g : C.Hom b c), map (C.comp f g) = D.comp (map f) (map g)

/-! ## the terminal category 1 -/

def one : Cat.{u, v} where
  Ob := PUnit
  Hom _ _ := PUnit
  id _ := PUnit.unit
  comp _ _ := PUnit.unit
  id_comp _ := rfl
  comp_id _ := rfl
  assoc _ _ _ := rfl

variable {C : Cat.{u, v}}

/-- objects of C are functors 1 → C -/
def ofObj (a : C.Ob) : Fun one.{u, v} C where
  obj _ := a
  map _ := C.id a
  map_id _ := rfl
  map_comp _ _ := (C.id_comp (C.id a)).symm

def toObj (F : Fun one.{u, v} C) : C.Ob := F.obj PUnit.unit

theorem toObj_ofObj (a : C.Ob) : toObj (ofObj a) = a := rfl

theorem ofObj_toObj (F : Fun one.{u, v} C) : ∀ x, (ofObj (toObj F)).obj x = F.obj x := fun _ => rfl

/-- a functor out of 1 sends its arrow to an identity -/
theorem one_map (F : Fun one.{u, v} C) (f : one.{u, v}.Hom PUnit.unit PUnit.unit) :
    F.map f = C.id (F.obj PUnit.unit) := F.map_id PUnit.unit

/-! ## the arrow category 2 -/

inductive Two where
  | z : Two
  | o : Two

def Hom2 : Two → Two → Type v
  | .z, .z => PUnit
  | .z, .o => PUnit
  | .o, .z => PEmpty
  | .o, .o => PUnit

def comp2 : {a b c : Two} → Hom2.{v} a b → Hom2.{v} b c → Hom2.{v} a c
  | .z, .z, .z, _, _ => PUnit.unit
  | .z, .z, .o, _, _ => PUnit.unit
  | .z, .o, .z, _, g => PEmpty.elim g
  | .z, .o, .o, _, _ => PUnit.unit
  | .o, .z, _, f, _ => PEmpty.elim f
  | .o, .o, .z, _, g => g
  | .o, .o, .o, _, _ => PUnit.unit

def id2 : (a : Two) → Hom2.{v} a a
  | .z => PUnit.unit
  | .o => PUnit.unit

def two : Cat.{0, v} where
  Ob := Two
  Hom := Hom2
  id := id2
  comp := comp2
  id_comp {a b} f := match a, b, f with
    | .z, .z, _ => rfl
    | .z, .o, _ => rfl
    | .o, .z, f => PEmpty.elim f
    | .o, .o, _ => rfl
  comp_id {a b} f := match a, b, f with
    | .z, .z, _ => rfl
    | .z, .o, _ => rfl
    | .o, .z, f => PEmpty.elim f
    | .o, .o, _ => rfl
  assoc {a b c d} f g h := match a, b, c, d, f, g, h with
    | .z, .z, .z, .z, _, _, _ => rfl
    | .z, .z, .z, .o, _, _, _ => rfl
    | .z, .z, .o, .z, _, _, h => PEmpty.elim h
    | .z, .z, .o, .o, _, _, _ => rfl
    | .z, .o, .z, _, _, g, _ => PEmpty.elim g
    | .z, .o, .o, .z, _, _, h => PEmpty.elim h
    | .z, .o, .o, .o, _, _, _ => rfl
    | .o, .z, _, _, f, _, _ => PEmpty.elim f
    | .o, .o, .z, _, _, g, _ => PEmpty.elim g
    | .o, .o, .o, .z, _, _, h => PEmpty.elim h
    | .o, .o, .o, .o, _, _, _ => rfl

/-- the generic arrow 0 → 1 of 2 -/
def arr : Hom2.{v} .z .o := PUnit.unit

variable {C : Cat.{0, v}}

/-- arrows of C are functors 2 → C -/
def ofArr {a b : C.Ob} (f : C.Hom a b) : Fun two.{v} C where
  obj x := match x with
    | .z => a
    | .o => b
  map {x y} g := match x, y, g with
    | .z, .z, _ => C.id a
    | .z, .o, _ => f
    | .o, .z, g => PEmpty.elim g
    | .o, .o, _ => C.id b
  map_id x := match x with
    | .z => rfl
    | .o => rfl
  map_comp {x y w} g h := match x, y, w, g, h with
    | .z, .z, .z, _, _ => (C.id_comp _).symm
    | .z, .z, .o, _, _ => (C.id_comp f).symm
    | .z, .o, .z, _, h => PEmpty.elim h
    | .z, .o, .o, _, _ => (C.comp_id f).symm
    | .o, .z, _, g, _ => PEmpty.elim g
    | .o, .o, .z, _, h => PEmpty.elim h
    | .o, .o, .o, _, _ => (C.id_comp _).symm

/-- the arrow named by a functor 2 → C -/
def toArr (F : Fun two.{v} C) : C.Hom (F.obj .z) (F.obj .o) := F.map arr

theorem toArr_ofArr {a b : C.Ob} (f : C.Hom a b) : toArr (ofArr f) = f := rfl

theorem ofArr_obj (F : Fun two.{v} C) : ∀ x, (ofArr (toArr F)).obj x = F.obj x
  | .z => rfl
  | .o => rfl

/-! ## composition from 3 -/

inductive Three where
  | a0 : Three
  | a1 : Three
  | a2 : Three

/-- the composable pair named by a functor out of 3: its long arrow is the composite -/
structure Pair3 (C : Cat.{0, v}) where
  x0 : C.Ob
  x1 : C.Ob
  x2 : C.Ob
  f : C.Hom x0 x1
  g : C.Hom x1 x2

def long (P : Pair3 C) : C.Hom P.x0 P.x2 := C.comp P.f P.g

/-- composition is functorial along the two arrows of 3: restricting to 0 → 1 and 1 → 2 gives f and g,
  and the long arrow is their composite (3 = 2 ⊔_1 2) -/
theorem long_comp (P : Pair3 C) (F : Fun C C) : F.map (long P) = C.comp (F.map P.f) (F.map P.g) := F.map_comp _ _

/-! ## products of categories -/

def prod (C D : Cat.{u, v}) : Cat.{u, v} where
  Ob := C.Ob × D.Ob
  Hom x y := C.Hom x.1 y.1 × D.Hom x.2 y.2
  id x := (C.id x.1, D.id x.2)
  comp f g := (C.comp f.1 g.1, D.comp f.2 g.2)
  id_comp f := by
    show (C.comp (C.id _) f.1, D.comp (D.id _) f.2) = f
    rw [C.id_comp, D.id_comp]
  comp_id f := by
    show (C.comp f.1 (C.id _), D.comp f.2 (D.id _)) = f
    rw [C.comp_id, D.comp_id]
  assoc f g h := by
    show (C.comp (C.comp f.1 g.1) h.1, D.comp (D.comp f.2 g.2) h.2) = (C.comp f.1 (C.comp g.1 h.1), D.comp f.2 (D.comp g.2 h.2))
    rw [C.assoc, D.assoc]

def pairF {B C D : Cat.{u, v}} (F : Fun B C) (G : Fun B D) : Fun B (prod C D) where
  obj b := (F.obj b, G.obj b)
  map f := (F.map f, G.map f)
  map_id b := by
    show (F.map (B.id b), G.map (B.id b)) = (C.id (F.obj b), D.id (G.obj b))
    rw [F.map_id, G.map_id]
  map_comp f g := by
    show (F.map (B.comp f g), G.map (B.comp f g)) = (C.comp (F.map f) (F.map g), D.comp (G.map f) (G.map g))
    rw [F.map_comp, G.map_comp]

def fstF {C D : Cat.{u, v}} : Fun (prod C D) C := ⟨fun x => x.1, fun f => f.1, fun _ => rfl, fun _ _ => rfl⟩
def sndF {C D : Cat.{u, v}} : Fun (prod C D) D := ⟨fun x => x.2, fun f => f.2, fun _ => rfl, fun _ _ => rfl⟩

theorem pair_fst {B C D : Cat.{u, v}} (F : Fun B C) (G : Fun B D) (b : B.Ob) : fstF.obj ((pairF F G).obj b) = F.obj b := rfl
theorem pair_snd {B C D : Cat.{u, v}} (F : Fun B C) (G : Fun B D) (b : B.Ob) : sndF.obj ((pairF F G).obj b) = G.obj b := rfl

/-! ## discrete categories: sets inside ETCC -/

def disc (X : Type u) : Cat.{u, u} where
  Ob := X
  Hom a b := ULift.{u} (PLift (a = b))
  id _ := ⟨⟨rfl⟩⟩
  comp f g := ⟨⟨f.down.down.trans g.down.down⟩⟩
  id_comp _ := rfl
  comp_id _ := rfl
  assoc _ _ _ := rfl

/-- every function is a functor between discrete categories -/
def discF {X Y : Type u} (f : X → Y) : Fun (disc X) (disc Y) where
  obj := f
  map e := ⟨⟨congrArg f e.down.down⟩⟩
  map_id _ := rfl
  map_comp _ _ := rfl

/-- and every functor between discrete categories is a function on objects -/
theorem discF_obj {X Y : Type u} (F : Fun (disc X) (disc Y)) (x : X) :
    (discF (X := X) (Y := Y) F.obj).obj x = F.obj x := rfl

theorem disc_round {X Y : Type u} (f : X → Y) : (discF f).obj = f := rfl

end ETCC

end Found

#print axioms Found.ETCC.toObj_ofObj
#print axioms Found.ETCC.one_map
#print axioms Found.ETCC.two
#print axioms Found.ETCC.ofArr
#print axioms Found.ETCC.toArr_ofArr
#print axioms Found.ETCC.ofArr_obj
#print axioms Found.ETCC.long_comp
#print axioms Found.ETCC.prod
#print axioms Found.ETCC.pairF
#print axioms Found.ETCC.pair_fst
#print axioms Found.ETCC.disc
#print axioms Found.ETCC.discF_obj
#print axioms Found.ETCC.disc_round

found/ETCS.lean

lean_all/found/ETCS.lean

/-
  T107. found/ETCS — Lawvere's ETCS as a point: the setoid category with
  conditions on the coordinates.

  ETCS = topos (T63) + well-pointed + NNO (T86) + Boolean Ω (T66) + choice (T87).
  * Well-pointed: in setoids, maps are determined by global elements (proved).
  * NNO: the discrete ℕ (T86), reused.
  * Boolean Ω ⟺ excluded middle (T66, reused); choice ⇒ excluded middle (T87,
    reused): so the point ETCS sits over the condition "LEM + AC" on the
    setoid coordinate, and without them the same coordinate gives the
    constructive (IZF/CZF-like) point.
  * ETCS ↔ ZFC (bounded Zermelo with choice) is the known translation (cited,
    Lawvere/Mitchell); drawn as a line in the map, not re-proved.

  Lean 4 core only, hand-written proofs, no axioms.
  CIC dependence: Ω = Prop (T63).
-/
import found.NNO
import found.Setoid
import found.Classical

namespace Found

namespace ETCS

/-- global elements of a setoid -/
def pt {X : Setd.{0}} (x : X.car) : SHom unitS X := ⟨fun _ => x, fun _ => X.refl x⟩

/-- well-pointedness: maps agreeing on all global elements are equal -/
theorem well_pointed {X Y : Setd.{0}} (f g : SHom X Y)
    (h : ∀ x : SHom unitS X, SetdCat.heq (SetdCat.comp x f) (SetdCat.comp x g)) :
    SetdCat.heq f g :=
  fun x => h (pt x) PUnit.unit

/-- the terminal object is a generator and not initial: 1 has a point -/
theorem one_has_point : ∃ _ : SHom (unitS.{0}) (unitS.{0}), True := ⟨pt (X := unitS.{0}) PUnit.unit, trivial⟩

/-- the ETCS conditions on the setoid coordinate -/
structure Conditions : Prop where
  lem : ∀ p : Prop, p ∨ ¬ p
  ac : SetoidModel.SetoidAC

/-- under the conditions Ω is Boolean (reusing T66's condition) -/
theorem boolean_of (c : Conditions) : Prp.Boolean OmegaHA :=
  fun p hp => (c.lem p).elim id (fun np => (hp np).elim)

/-- and choice alone already forces the classical condition (reusing T87) -/
theorem lem_of_ac (ac : SetoidModel.SetoidAC) : ∀ p : Prop, p ∨ ¬ p := SetoidModel.ac_lem ac

/-- so the conditions reduce to choice -/
theorem conditions_of_ac (ac : SetoidModel.SetoidAC) : Conditions := ⟨lem_of_ac ac, ac⟩

/-- the NNO of the point (reused from T86) -/
def nno : NNOs.NNO SetdCat.{0} setdTerminal := NNOs.natNNO

end ETCS

end Found

#print axioms Found.ETCS.well_pointed
#print axioms Found.ETCS.one_has_point
#print axioms Found.ETCS.boolean_of
#print axioms Found.ETCS.lem_of_ac
#print axioms Found.ETCS.conditions_of_ac
#print axioms Found.ETCS.nno

found/ETCSZF.lean

lean_all/found/ETCSZF.lean

/-
  T209 slice L3a. found/ETCSZF — the line between the set-theoretic point
  (Aczel's V, Mac Lane / Z, T163) and the topos point (setoids, T92/T107).
  * El : V → setoids (the elements of a set, with bisimilarity).
  * Power sets agree: El (P a) ≅ Ω^(El a) (the core's power object) as
    setoids, by explicit maps both ways — the set-theoretic power set is
    the topos power object.  (Products, exponentials of El's and the
    converse interpretation ETCS → Mac Lane are the next slices.)

  Lean 4 core only, hand-written proofs, no axioms.
  CIC dependence: impredicative Prop (the index type a.I → Prop of P a).
-/
import found.IZF
import found.Core

namespace Found

namespace ETCSZF

open WF Core

def El (a : V.{0}) : Setd.{0} :=
  ⟨a.I, fun i j => Eq' (a.f i) (a.f j), fun _ => eq_refl _, fun h => eq_symm _ _ h,
   fun h k => eq_trans _ _ _ h k⟩

/-- the predicate on El a named by an element of P a -/
def F (a : V.{0}) : SHom (El (IZF.power a)) (power (El a)) where
  fn P := ⟨fun i => ⟨Mem (a.f i) ((IZF.power a).f P)⟩,
    fun e => ⟨fun m => mem_resp_left e m, fun m => mem_resp_left (eq_symm _ _ e) m⟩⟩
  resp e _ := ⟨fun m => mem_resp_right m e, fun m => mem_resp_right m (eq_symm _ _ e)⟩

def G (a : V.{0}) : SHom (power (El a)) (El (IZF.power a)) where
  fn φ := fun i => (φ.fn i).down
  resp e :=
    ⟨fun ⟨i, hi⟩ => ⟨⟨i, (e i).mp hi⟩, eq_refl _⟩,
     fun ⟨i, hi⟩ => ⟨⟨i, (e i).mpr hi⟩, eq_refl _⟩⟩

/-- G ∘ F ≈ id on El (P a) -/
theorem GF (a : V.{0}) (P : (El (IZF.power a)).car) : (El (IZF.power a)).eqv ((G a).fn ((F a).fn P)) P := by
  show Eq' (V.sup {i // Mem (a.f i) ((IZF.power a).f P)} (fun p => a.f p.val)) (V.sup {i // P i} (fun p => a.f p.val))
  exact ⟨fun ⟨i, ⟨⟨j, hj⟩, e⟩⟩ => ⟨⟨j, hj⟩, e⟩,
         fun ⟨j, hj⟩ => ⟨⟨j, ⟨⟨j, hj⟩, eq_refl _⟩⟩, eq_refl _⟩⟩

/-- F ∘ G ≈ id on Ω^(El a) -/
theorem FG (a : V.{0}) (φ : (power (El a)).car) : (power (El a)).eqv ((F a).fn ((G a).fn φ)) φ := fun i =>
  ⟨fun ⟨⟨_, hj⟩, e⟩ => (φ.resp (eq_symm _ _ e)).mp hj,
   fun hi => ⟨⟨i, hi⟩, eq_refl _⟩⟩

theorem power_iso (a : V.{0}) : Iso' (El (IZF.power a)) (power (El a)) :=
  ⟨F a, G a, GF a, FG a⟩

end ETCSZF

end Found

#print axioms Found.ETCSZF.GF
#print axioms Found.ETCSZF.FG
#print axioms Found.ETCSZF.power_iso

found/ETCSZF2.lean

lean_all/found/ETCSZF2.lean

/-
  T209 slice L3b. found/ETCSZF2 — products agree between the set point
  (Aczel's V) and the topos point (setoids):
  * Kuratowski pairs ⟨x,y⟩ = {{x},{x,y}} are injective up to ≈
    (opair_inj), and respect ≈ (opair_resp);
  * the set product a × b = {⟨x,y⟩ | x ∈ a, y ∈ b} satisfies
    El (a × b) ≅ El a × El b (the topos product) as setoids.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.ETCSZF

namespace Found

namespace ETCSZF2

open WF Core ETCSZF

def single (x : V.{0}) : V.{0} := IZF.pair x x
def opair (x y : V.{0}) : V.{0} := IZF.pair (single x) (IZF.pair x y)

theorem in_pair_l (a b : V.{0}) : Mem a (IZF.pair a b) := (IZF.pair_spec a b a).mpr (Or.inl (eq_refl _))
theorem in_pair_r (a b : V.{0}) : Mem b (IZF.pair a b) := (IZF.pair_spec a b b).mpr (Or.inr (eq_refl _))

/-- if a ≈ pair c d then a's members are ≈ c or ≈ d -/
theorem of_eq_pair {z a c d : V.{0}} (e : Eq' a (IZF.pair c d)) (m : Mem z a) : Eq' z c ∨ Eq' z d :=
  (IZF.pair_spec c d z).mp (mem_resp_right m e)

theorem single_eq {x c d : V.{0}} (e : Eq' (single x) (IZF.pair c d)) : Eq' x c ∨ Eq' x d :=
  of_eq_pair e (in_pair_l x x)

theorem eq_single {x y : V.{0}} (e : Eq' (single x) (single y)) : Eq' x y :=
  (single_eq e).elim id id

theorem pair_resp {a a' b b' : V.{0}} (ha : Eq' a a') (hb : Eq' b b') : Eq' (IZF.pair a b) (IZF.pair a' b') :=
  extensionality _ _ (fun z =>
    ⟨fun m => (IZF.pair_spec a' b' z).mpr (((IZF.pair_spec a b z).mp m).elim
        (fun h => Or.inl (eq_trans _ _ _ h ha)) (fun h => Or.inr (eq_trans _ _ _ h hb))),
     fun m => (IZF.pair_spec a b z).mpr (((IZF.pair_spec a' b' z).mp m).elim
        (fun h => Or.inl (eq_trans _ _ _ h (eq_symm _ _ ha))) (fun h => Or.inr (eq_trans _ _ _ h (eq_symm _ _ hb))))⟩)

theorem opair_resp {x x' y y' : V.{0}} (hx : Eq' x x') (hy : Eq' y y') : Eq' (opair x y) (opair x' y') :=
  pair_resp (pair_resp hx hx) (pair_resp hx hy)

theorem opair_inj {x y x' y' : V.{0}} (e : Eq' (opair x y) (opair x' y')) : Eq' x x' ∧ Eq' y y' := by
  -- first components
  have hx : Eq' x x' := by
    rcases of_eq_pair e (in_pair_l (single x) (IZF.pair x y)) with h | h
    · exact eq_single h
    · exact eq_symm _ _ ((of_eq_pair (eq_symm _ _ h) (in_pair_l x' y')).elim id id)
  refine ⟨hx, ?_⟩
  rcases of_eq_pair e (in_pair_r (single x) (IZF.pair x y)) with h | h
  · -- pair x y ≈ single x'  ⇒ y ≈ x'
    have hyx : Eq' y x' := (of_eq_pair h (in_pair_r x y)).elim id id
    rcases of_eq_pair (eq_symm _ _ e) (in_pair_r (single x') (IZF.pair x' y')) with k | k
    · have : Eq' y' x := (of_eq_pair k (in_pair_r x' y')).elim id id
      exact eq_trans _ _ _ hyx (eq_trans _ _ _ (eq_symm _ _ hx) (eq_symm _ _ this))
    · have k' : Eq' (IZF.pair x' y') (single x') := eq_trans _ _ _ k h
      have : Eq' y' x' := (of_eq_pair k' (in_pair_r x' y')).elim id id
      exact eq_trans _ _ _ hyx (eq_symm _ _ this)
  · rcases of_eq_pair h (in_pair_r x y) with k | k
    · have hyx : Eq' y x' := k
      rcases of_eq_pair (eq_symm _ _ h) (in_pair_r x' y') with l | l
      · exact eq_trans _ _ _ hyx (eq_trans _ _ _ (eq_symm _ _ hx) (eq_symm _ _ l))
      · exact eq_symm _ _ l
    · exact k

/-- the set product -/
def prodV (a b : V.{0}) : V.{0} := .sup (a.I × b.I) (fun p => opair (a.f p.1) (b.f p.2))

theorem prod_iso (a b : V.{0}) : Iso' (El (prodV a b)) (prodS (El a) (El b)) :=
  ⟨⟨fun p => p, fun h => opair_inj h⟩, ⟨fun p => p, fun h => opair_resp h.1 h.2⟩,
   fun _ => eq_refl _, fun _ => ⟨eq_refl _, eq_refl _⟩⟩

/-! ## exponentials: the set of (graphs of) functions -/

def graph (a b : V.{0}) (h : a.I → b.I) : V.{0} := .sup a.I (fun i => opair (a.f i) (b.f (h i)))

def Resp' (a b : V.{0}) (h : a.I → b.I) : Prop :=
  ∀ i j, Eq' (a.f i) (a.f j) → Eq' (b.f (h i)) (b.f (h j))

/-- b^a: graphs of ≈-respecting index functions (CZF-style exponentiation) -/
def expV (a b : V.{0}) : V.{0} := .sup {h : a.I → b.I // Resp' a b h} (fun h => graph a b h.val)

theorem graph_eq_iff (a b : V.{0}) (h k : {h : a.I → b.I // Resp' a b h}) :
    Eq' (graph a b h.val) (graph a b k.val) ↔ ∀ i, Eq' (b.f (h.val i)) (b.f (k.val i)) :=
  ⟨fun e i => match e.1 i with
    | ⟨j, ej⟩ => match opair_inj ej with
      | ⟨ea, eb⟩ => eq_trans _ _ _ eb (k.property j i (eq_symm _ _ ea)),
   fun hp => ⟨fun i => ⟨i, opair_resp (eq_refl _) (hp i)⟩, fun j => ⟨j, opair_resp (eq_refl _) (hp j)⟩⟩⟩

theorem exp_iso (a b : V.{0}) : Iso' (El (expV a b)) (expS (El a) (El b)) :=
  ⟨⟨fun h => ⟨h.val, fun e => h.property _ _ e⟩, fun e => (graph_eq_iff a b _ _).mp e⟩,
   ⟨fun F => ⟨F.fn, fun _ _ e => F.resp e⟩, fun e => (graph_eq_iff a b _ _).mpr e⟩,
   fun h => (graph_eq_iff a b h h).mpr (fun _ => eq_refl _), fun _ _ => eq_refl _⟩

end ETCSZF2

end Found

#print axioms Found.ETCSZF2.opair_inj
#print axioms Found.ETCSZF2.opair_resp
#print axioms Found.ETCSZF2.prod_iso
#print axioms Found.ETCSZF2.graph_eq_iff
#print axioms Found.ETCSZF2.exp_iso

found/ETCSZF3.lean

lean_all/found/ETCSZF3.lean

/-
  T209 slice L3c. found/ETCSZF3 — the converse line, topos → Mac Lane:
  inside the core (T59), the W-type of the universe coordinate
  VW = W(Type, ULift) (operation 5, induction) carries membership and
  bisimilarity transported along fromW : VW → V, and with the operations
  transported along toW it satisfies the Mac Lane axioms (pairing, union,
  power, infinity, Δ₀-separation) — built only from the core's operations
  plus the setoid topos' power objects.  Together with L3a/L3b
  (El : V → setoids preserves power, products, exponentials) this gives
  both directions at the instance level.

  Lean 4 core only, hand-written proofs, no axioms.
  CIC dependence: impredicative Prop (power, Δ₀-separation).
-/
import found.CoreBridges
import found.MacLane

namespace Found

namespace ETCSZF3

open WF CoreBridges

def memW (x y : VW) : Prop := Mem (fromW x) (fromW y)
def eqW (x y : VW) : Prop := Eq' (fromW x) (fromW y)

def pairW (a b : VW) : VW := toW (IZF.pair (fromW a) (fromW b))
def unionW (a : VW) : VW := toW (IZF.union (fromW a))
def powerW (a : VW) : VW := toW (IZF.power (fromW a))
def emptyW : VW := toW IZF.empty
def omegaW : VW := toW IZF.omega
def succW (a : VW) : VW := toW (IZF.succ (fromW a))
def sepW (a : VW) (φ : KP.D0) (ρ : Nat → VW) : VW :=
  toW (IZF.sep (fromW a) (fun y => KP.sat φ (KP.cons y (fun n => fromW (ρ n)))))

theorem memW_toW (x : VW) (P : V) : memW x (toW P) ↔ Mem (fromW x) P :=
  ⟨fun m => mem_resp_right m (from_to P), fun m => mem_resp_right m (eq_symm _ _ (from_to P))⟩

theorem maclaneW (a b x : VW) (φ : KP.D0) (ρ : Nat → VW) :
    (memW x (pairW a b) ↔ eqW x a ∨ eqW x b) ∧
    (memW x (unionW a) ↔ ∃ y : V, Mem y (fromW a) ∧ Mem (fromW x) y) ∧
    (memW x (powerW a) ↔ ∀ z : V, Mem z (fromW x) → Mem z (fromW a)) ∧
    (memW emptyW omegaW ∧ ∀ y : VW, memW y omegaW → memW (succW y) omegaW) ∧
    (memW x (sepW a φ ρ) ↔ memW x a ∧ KP.sat φ (KP.cons (fromW x) (fun n => fromW (ρ n)))) :=
  ⟨(memW_toW _ _).trans (IZF.pair_spec _ _ _),
   (memW_toW _ _).trans (IZF.union_spec _ _),
   (memW_toW _ _).trans (IZF.power_spec _ _),
   ⟨(memW_toW _ _).mpr (mem_resp_left (eq_symm _ _ (from_to _)) IZF.infinity.{0,0}.1),
    fun _ m => (memW_toW _ _).mpr (mem_resp_left (eq_symm _ _ (from_to _))
      (IZF.infinity.{0,0}.2 _ ((memW_toW _ _).mp m)))⟩,
   (memW_toW _ _).trans (KP.d0_separation φ _ _ _)⟩

end ETCSZF3

end Found

#print axioms Found.ETCSZF3.memW_toW
#print axioms Found.ETCSZF3.maclaneW

found/Eff1.lean

lean_all/found/Eff1.lean

/-
  T96b, the effective topos, part 1. found/Eff1 — partial combinatory
  algebras and combinatory completeness:
  * a PCA: a deterministic partial application a·b ↓ c with k and s,
    k·a ↓, k·a·b = a, s·a ↓, s·a·b ↓, and s·a·b·x = (a·x)·(b·x) whenever the
    right side is defined (the direction used by realizability);
  * terms with variables and constants, their (relational) values, and
    bracket abstraction [x]t: always defined, and ([x]t)·a ↓ v whenever
    t[x := a] ↓ v;
  * pairing ⟨a, b⟩ with projections, and the combinators used to build
    realizers.

  Lean 4 core only, hand-written proofs, no axioms.
-/

namespace Found

namespace Eff

structure PCA where
  A : Type
  ap : A → A → A → Prop
  det : ∀ {a b c c'}, ap a b c → ap a b c' → c = c'
  k : A
  s : A
  k1 : ∀ a, ∃ c, ap k a c ∧ ∀ b, ap c b a
  s1 : ∀ a, ∃ c, ap s a c ∧ ∀ b, ∃ d, ap c b d ∧ ∀ x p q v, ap a x p → ap b x q → ap p q v → ap d x v

variable {P : PCA}

/-- terms: variables (de Bruijn), constants, application -/
inductive CT (P : PCA) where
  | v : Nat → CT P
  | c : P.A → CT P
  | ap : CT P → CT P → CT P

def ext (ρ : Nat → P.A) (a : P.A) : Nat → P.A
  | 0 => a
  | n + 1 => ρ n

/-- the value of a term (partial) -/
def Ev (ρ : Nat → P.A) : CT P → P.A → Prop
  | .v n, x => x = ρ n
  | .c a, x => x = a
  | .ap t u, x => ∃ p q, Ev ρ t p ∧ Ev ρ u q ∧ P.ap p q x

theorem ev_det (ρ : Nat → P.A) : (t : CT P) → {x y : P.A} → Ev ρ t x → Ev ρ t y → x = y
  | .v _, _, _, h1, h2 => h1.trans h2.symm
  | .c _, _, _, h1, h2 => h1.trans h2.symm
  | .ap t u, _, _, ⟨p, q, h1, h2, h3⟩, ⟨p', q', h1', h2', h3'⟩ => by
    have e1 := ev_det ρ t h1 h1'
    have e2 := ev_det ρ u h2 h2'
    subst e1; subst e2
    exact P.det h3 h3'

def kT : CT P := .c P.k
def sT : CT P := .c P.s
def iT : CT P := .ap (.ap sT kT) kT

/-- bracket abstraction of variable 0 -/
def lam : CT P → CT P
  | .v 0 => iT
  | .v (n + 1) => .ap kT (.v n)
  | .c a => .ap kT (.c a)
  | .ap t u => .ap (.ap sT (lam t)) (lam u)

theorem k_def (a : P.A) : ∃ c, P.ap P.k a c := match P.k1 a with | ⟨c, h, _⟩ => ⟨c, h⟩

theorem s_def2 (a b : P.A) : ∃ c d, P.ap P.s a c ∧ P.ap c b d := match P.s1 a with
  | ⟨c, h, h2⟩ => match h2 b with
    | ⟨d, hd, _⟩ => ⟨c, d, h, hd⟩

/-- abstractions are defined -/
theorem lam_def (ρ : Nat → P.A) : (t : CT P) → ∃ f, Ev ρ (lam t) f
  | .v 0 => match s_def2 P.k P.k with
    | ⟨c, d, h1, h2⟩ => ⟨d, c, P.k, ⟨P.s, P.k, rfl, rfl, h1⟩, rfl, h2⟩
  | .v (n + 1) => match k_def (ρ n) with
    | ⟨c, h⟩ => ⟨c, P.k, ρ n, rfl, rfl, h⟩
  | .c a => match k_def a with
    | ⟨c, h⟩ => ⟨c, P.k, a, rfl, rfl, h⟩
  | .ap t u => match lam_def ρ t, lam_def ρ u with
    | ⟨F, hF⟩, ⟨G, hG⟩ => match s_def2 F G with
      | ⟨c, d, h1, h2⟩ => ⟨d, c, G, ⟨P.s, F, rfl, hF, h1⟩, hG, h2⟩

/-- the s-law with the values of s·F and s·F·G given -/
theorem s_law {F G c d x p q v : P.A} (h1 : P.ap P.s F c) (h2 : P.ap c G d)
    (hp : P.ap F x p) (hq : P.ap G x q) (hv : P.ap p q v) : P.ap d x v :=
  match P.s1 F with
  | ⟨c', h1', h2'⟩ => by
    have e := P.det h1 h1'
    subst e
    exact match h2' G with
    | ⟨d', hd', law⟩ => by
      have e' := P.det h2 hd'
      subst e'
      exact law x p q v hp hq hv

theorem k_law {a c b : P.A} (h : P.ap P.k a c) : P.ap c b a :=
  match P.k1 a with
  | ⟨c', h', law⟩ => by
    have e := P.det h h'
    subst e
    exact law b

/-- β for bracket abstraction (the defined direction) -/
theorem lam_beta (ρ : Nat → P.A) : (t : CT P) → {f : P.A} → Ev ρ (lam t) f → ∀ {a v : P.A}, Ev (ext ρ a) t v → P.ap f a v
  | .v 0, f, ⟨c, k', ⟨s', k'', es, ek, h1⟩, ek2, h2⟩, a, v, hv => by
    show P.ap f a v
    have hv' : v = a := hv
    rw [hv']
    subst es; subst ek; subst ek2
    exact match k_def a with
    | ⟨ka, hka⟩ => s_law h1 h2 hka hka (k_law hka)
  | .v (n + 1), f, ⟨k', x, ek, ex, h⟩, a, v, hv => by
    have hv' : v = ρ n := hv
    subst ek; subst ex; subst hv'
    exact k_law h
  | .c b, f, ⟨k', x, ek, ex, h⟩, a, v, hv => by
    have hv' : v = b := hv
    subst ek; subst ex; subst hv'
    exact k_law h
  | .ap t u, f, ⟨c, G, ⟨s', F, es, hF, h1⟩, hG, h2⟩, a, v, ⟨p, q, hp, hq, hpq⟩ => by
    subst es
    exact s_law h1 h2 (lam_beta ρ t hF hp) (lam_beta ρ u hG hq) hpq

end Eff

end Found

#print axioms Found.Eff.ev_det
#print axioms Found.Eff.lam_def
#print axioms Found.Eff.s_law
#print axioms Found.Eff.k_law
#print axioms Found.Eff.lam_beta

found/Eff10.lean

lean_all/found/Eff10.lean

/-
  T96b, the effective topos, part 10. found/Eff10 — the classifying arrow
  of a relation: for a strict relational R ⊆ B × A, the arrow
  χ_R : B → PA, χ_R(b, φ) = E(b, b) ∧ "R(b, -) and φ are good and
  equivalent", is a functional relation.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Eff9

namespace Found

namespace Eff

variable {P : PCA}

/-- a relation between B and A: strict and relational -/
structure Rel (B A : Obj P) where
  R : B.X → A.X → Pr P
  st : Ent (fun p : B.X × A.X => R p.1 p.2) (andP (fun p => B.E p.1 p.1) (fun p => A.E p.2 p.2))
  rel : Ent (andP (andP (fun p : (B.X × A.X) × (B.X × A.X) => R p.1.1 p.1.2) (fun p => B.E p.1.1 p.2.1)) (fun p => A.E p.1.2 p.2.2))
    (fun p => R p.2.1 p.2.2)

section C

variable {B A : Obj P} (R : Rel B A)

/-- the sections R(b, -) are good when b exists -/
theorem goodR {Γ : Type} (b : Γ → B.X) : Ent (fun γ => B.E (b γ) (b γ)) (GoodF A (fun γ => R.R (b γ))) := by
  refine and_i (all_intro (curry ?_)) (all_intro (curry ?_))
  · exact ent_trans (and_r _ _) (ent_trans (ent_re (fun q : Γ × A.X => (b q.1, q.2)) R.st) (and_r _ _))
  · exact ent_trans (and_i (and_i (ent_trans (and_r _ _) (and_l _ _)) (and_l _ _)) (ent_trans (and_r _ _) (and_r _ _)))
      (ent_re (fun q : Γ × (A.X × A.X) => ((b q.1, q.2.1), (b q.1, q.2.2))) R.rel)

/-- equal arguments give equivalent sections -/
theorem iffR {Γ : Type} (b b' : Γ → B.X) :
    Ent (fun γ => B.E (b γ) (b' γ)) (IffF A (fun γ => R.R (b γ)) (fun γ => R.R (b' γ))) := by
  refine all_intro (and_i (curry ?_) (curry ?_))
  · exact ent_trans (and_i (and_i (and_r _ _) (and_l _ _))
      (ent_trans (and_r _ _) (ent_trans (ent_re (fun q : Γ × A.X => (b q.1, q.2)) R.st) (and_r _ _))))
      (ent_re (fun q : Γ × A.X => ((b q.1, q.2), (b' q.1, q.2))) R.rel)
  · exact ent_trans (and_i (and_i (and_r _ _) (ent_trans (and_l _ _) (sym' B (fun q : Γ × A.X => b q.1) (fun q => b' q.1))))
      (ent_trans (and_r _ _) (ent_trans (ent_re (fun q : Γ × A.X => (b' q.1, q.2)) R.st) (and_r _ _))))
      (ent_re (fun q : Γ × A.X => ((b' q.1, q.2), (b q.1, q.2))) R.rel)

/-- the classifying relation -/
def chiR (b : B.X) (φ : A.X → Pr P) : Pr P :=
  andP (fun _ : Unit => B.E b b) (EPF A (fun _ : Unit => R.R b) (fun _ => φ)) ()

theorem chi_un {Γ : Type} (b : Γ → B.X) (φ : Γ → A.X → Pr P) :
    Ent (fun γ => chiR R (b γ) (φ γ)) (andP (fun γ => B.E (b γ) (b γ)) (EPF A (fun γ => R.R (b γ)) φ)) := ent_of (fun _ _ h => h)

theorem un_chi {Γ : Type} (b : Γ → B.X) (φ : Γ → A.X → Pr P) :
    Ent (andP (fun γ => B.E (b γ) (b γ)) (EPF A (fun γ => R.R (b γ)) φ)) (fun γ => chiR R (b γ) (φ γ)) := ent_of (fun _ _ h => h)

theorem pE_un {Γ : Type} (φ ψ : Γ → A.X → Pr P) :
    Ent (fun γ => (powO A).E (φ γ) (ψ γ)) (EPF A φ ψ) := ent_of (fun _ _ h => h)

theorem un_pE {Γ : Type} (φ ψ : Γ → A.X → Pr P) :
    Ent (EPF A φ ψ) (fun γ => (powO A).E (φ γ) (ψ γ)) := ent_of (fun _ _ h => h)

/-- the classifying arrow -/
def chiF : Fn B (powO A) where
  F := chiR R
  st := by
    let b := fun p : B.X × (A.X → Pr P) => p.1
    let φ := fun p : B.X × (A.X → Pr P) => p.2
    refine ent_trans (chi_un R b φ) (and_i (and_l _ _) (ent_trans ?_ (un_pE φ φ)))
    have gφ : Ent (andP (fun p : B.X × (A.X → Pr P) => B.E (b p) (b p)) (EPF A (fun p => R.R (b p)) φ)) (GoodF A φ) :=
      ent_trans (and_r _ _) (ent_trans (and_r _ _) (and_l _ _))
    exact and_i gφ (and_i gφ (iff_refl A _ φ))
  rel := by
    let b := fun p : (B.X × B.X) × ((A.X → Pr P) × (A.X → Pr P)) => p.1.1
    let b' := fun p : (B.X × B.X) × ((A.X → Pr P) × (A.X → Pr P)) => p.1.2
    let φ := fun p : (B.X × B.X) × ((A.X → Pr P) × (A.X → Pr P)) => p.2.1
    let φ' := fun p : (B.X × B.X) × ((A.X → Pr P) × (A.X → Pr P)) => p.2.2
    let L := andP (fun p => B.E (b p) (b' p)) (andP (andP (fun p => B.E (b p) (b p)) (EPF A (fun p => R.R (b p)) φ)) (EPF A φ φ'))
    have pull : Ent (andP (fun p : (B.X × B.X) × ((A.X → Pr P) × (A.X → Pr P)) => B.E p.1.1 p.1.2)
        (andP (fun p => chiR R p.1.1 p.2.1) (fun p => (powO A).E p.2.1 p.2.2))) L := ent_of (fun _ _ h => h)
    have eb' : Ent L (fun p => B.E (b' p) (b' p)) := ent_trans (and_l _ _) (refl_r B b b')
    have ir : Ent L (IffF A (fun p => R.R (b' p)) (fun p => R.R (b p))) :=
      ent_trans (ent_trans (and_l _ _) (sym' B b b')) (iffR R b' b)
    have i1 : Ent L (IffF A (fun p => R.R (b p)) φ) := ent_trans (and_r _ _) (ent_trans (and_l _ _) (ent_trans (and_r _ _) (ent_trans (and_r _ _) (and_r _ _))))
    have i2 : Ent L (IffF A φ φ') := ent_trans (and_r _ _) (ent_trans (and_r _ _) (ent_trans (and_r _ _) (and_r _ _)))
    have g' : Ent L (GoodF A φ') := ent_trans (and_r _ _) (ent_trans (and_r _ _) (ent_trans (and_r _ _) (and_l _ _)))
    have ifin : Ent L (IffF A (fun p => R.R (b' p)) φ') :=
      ent_trans (and_i (ent_trans (and_i ir i1) (iff_trans A _ _ _)) i2) (iff_trans A _ _ _)
    exact ent_trans pull (ent_trans (and_i eb' (and_i (ent_trans eb' (goodR R b')) (and_i g' ifin))) (un_chi R b' φ'))
  sv := by
    let b := fun p : B.X × (A.X → Pr P) × (A.X → Pr P) => p.1
    let φ := fun p : B.X × (A.X → Pr P) × (A.X → Pr P) => p.2.1
    let φ' := fun p : B.X × (A.X → Pr P) × (A.X → Pr P) => p.2.2
    let L := andP (andP (fun p => B.E (b p) (b p)) (EPF A (fun p => R.R (b p)) φ))
      (andP (fun p => B.E (b p) (b p)) (EPF A (fun p => R.R (b p)) φ'))
    have pull : Ent (andP (fun p : B.X × (A.X → Pr P) × (A.X → Pr P) => chiR R p.1 p.2.1) (fun p => chiR R p.1 p.2.2)) L :=
      ent_of (fun _ _ h => h)
    have g1 : Ent L (GoodF A φ) := ent_trans (and_l _ _) (ent_trans (and_r _ _) (ent_trans (and_r _ _) (and_l _ _)))
    have g2 : Ent L (GoodF A φ') := ent_trans (and_r _ _) (ent_trans (and_r _ _) (ent_trans (and_r _ _) (and_l _ _)))
    have i1 : Ent L (IffF A φ (fun p => R.R (b p))) :=
      ent_trans (and_l _ _) (ent_trans (and_r _ _) (ent_trans (and_r _ _) (ent_trans (and_r _ _) (iff_symm A _ _))))
    have i2 : Ent L (IffF A (fun p => R.R (b p)) φ') := ent_trans (and_r _ _) (ent_trans (and_r _ _) (ent_trans (and_r _ _) (and_r _ _)))
    exact ent_trans pull (ent_trans (and_i g1 (and_i g2 (ent_trans (and_i i1 i2) (iff_trans A _ _ _)))) (un_pE φ φ'))
  tot := ex_intro (fun b => R.R b) (ent_trans (and_i (ent_refl _) (ent_trans (goodR R (fun b => b))
      (and_i (ent_refl _) (and_i (ent_refl _) (iff_refl A _ _))))) (un_chi R (fun b => b) (fun b => R.R b)))

end C

end Eff

end Found

#print axioms Found.Eff.goodR
#print axioms Found.Eff.iffR
#print axioms Found.Eff.chiF

found/Eff11.lean

lean_all/found/Eff11.lean

/-
  T96b, the effective topos, part 11. found/Eff11 — the power object is a
  power object: pulling membership back along χ_R gives R
  (R(b, x) ⊣⊢ ∃φ. χ_R(b, φ) ∧ x ∈ φ), and any arrow with this property is
  equal to χ_R. With Eff5–Eff10: the realizability category over any PCA
  has finite limits and power objects — it is a topos (in the form with
  morphisms up to equality of functional relations).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Eff10

namespace Found

namespace Eff

variable {P : PCA}

section U

variable {B A : Obj P}

/-- the relation obtained by pulling membership back along an arrow into PA -/
def pbR (χ : Fn B (powO A)) : B.X × A.X → Pr P :=
  exP (andP (fun q : (B.X × A.X) × (A.X → Pr P) => χ.F q.1.1 q.2) (memF A (fun q => q.1.2) (fun q => q.2)))

/-- φ(x) from "x ∈ φ" -/
theorem mem_at {Γ : Type} (x : Γ → A.X) (φ : Γ → A.X → Pr P) : Ent (memF A x φ) (fun γ => φ γ (x γ)) :=
  ent_trans (and_r _ _) (and_r _ _)

/-- the instance of an equivalence of predicates at a point, one direction -/
theorem iff_bwd {Γ : Type} (φ ψ : Γ → A.X → Pr P) (x : Γ → A.X) :
    Ent (andP (IffF A φ ψ) (fun γ => ψ γ (x γ))) (fun γ => φ γ (x γ)) :=
  ent_trans (and_i (ent_trans (and_l _ _) (ent_trans (all_elim _ x) (and_r _ _))) (and_r _ _)) (eval_ent _ _)

theorem iff_fwd {Γ : Type} (φ ψ : Γ → A.X → Pr P) (x : Γ → A.X) :
    Ent (andP (IffF A φ ψ) (fun γ => φ γ (x γ))) (fun γ => ψ γ (x γ)) :=
  ent_trans (and_i (ent_trans (and_l _ _) (ent_trans (all_elim _ x) (and_l _ _))) (and_r _ _)) (eval_ent _ _)

/-- strictness of a good predicate at a point -/
theorem good_str {Γ : Type} (φ : Γ → A.X → Pr P) (x : Γ → A.X) :
    Ent (andP (GoodF A φ) (fun γ => φ γ (x γ))) (fun γ => A.E (x γ) (x γ)) :=
  ent_trans (and_i (ent_trans (and_l _ _) (ent_trans (and_l _ _) (all_elim _ x))) (and_r _ _)) (eval_ent _ _)

variable (R : Rel B A)

/-- membership pulled back along χ_R is R -/
theorem chi_pb : Ent (fun p : B.X × A.X => R.R p.1 p.2) (pbR (chiF R)) ∧ Ent (pbR (chiF R)) (fun p : B.X × A.X => R.R p.1 p.2) := by
  constructor
  · refine ex_intro (fun p : B.X × A.X => R.R p.1) ?_
    have ebb : Ent (fun p : B.X × A.X => R.R p.1 p.2) (fun p => B.E p.1 p.1) := ent_trans R.st (and_l _ _)
    have exx : Ent (fun p : B.X × A.X => R.R p.1 p.2) (fun p => A.E p.2 p.2) := ent_trans R.st (and_r _ _)
    have gR : Ent (fun p : B.X × A.X => R.R p.1 p.2) (GoodF A (fun p => R.R p.1)) := ent_trans ebb (goodR R (fun p : B.X × A.X => p.1))
    exact ent_trans (and_i (and_i ebb (and_i gR (and_i gR (iff_refl A _ (fun p : B.X × A.X => R.R p.1))))) (and_i exx (and_i gR (ent_refl _))))
      (ent_of (fun _ _ h => h))
  · refine ex_elim ?_
    let L := andP (andP (fun q : (B.X × A.X) × (A.X → Pr P) => B.E q.1.1 q.1.1) (EPF A (fun q => R.R q.1.1) (fun q => q.2)))
      (memF A (fun q : (B.X × A.X) × (A.X → Pr P) => q.1.2) (fun q => q.2))
    have h : Ent (andP (fun q : (B.X × A.X) × (A.X → Pr P) => (chiF R).F q.1.1 q.2) (memF A (fun q => q.1.2) (fun q => q.2))) L :=
      ent_of (fun _ _ h => h)
    exact ent_trans h (ent_trans (and_i (ent_trans (and_l _ _) (ent_trans (and_r _ _) (ent_trans (and_r _ _) (and_r _ _))))
      (ent_trans (and_r _ _) (mem_at _ _))) (iff_bwd _ _ (fun q : (B.X × A.X) × (A.X → Pr P) => q.1.2)))

/-- an arrow pulling membership back to R is χ_R -/
theorem chi_uniq (χ : Fn B (powO A)) (h1 : Ent (fun p : B.X × A.X => R.R p.1 p.2) (pbR χ))
    (h2 : Ent (pbR χ) (fun p : B.X × A.X => R.R p.1 p.2)) : FEq χ (chiF R) := by
  -- χ(b, φ) ⊢ χ_R(b, φ)
  have fwd : Ent (fun p : B.X × (A.X → Pr P) => χ.F p.1 p.2) (fun p => (chiF R).F p.1 p.2) := by
    have ebb : Ent (fun p : B.X × (A.X → Pr P) => χ.F p.1 p.2) (fun p => B.E p.1 p.1) := ent_trans χ.st (and_l _ _)
    have gφ : Ent (fun p : B.X × (A.X → Pr P) => χ.F p.1 p.2) (GoodF A (fun p => p.2)) :=
      ent_trans χ.st (ent_trans (and_r _ _) (ent_trans (pE_un (fun p : B.X × (A.X → Pr P) => p.2) (fun p => p.2)) (and_l _ _)))
    -- the equivalence R(b, -) ⇔ φ, pointwise
    have iff : Ent (fun p : B.X × (A.X → Pr P) => χ.F p.1 p.2) (IffF A (fun p => R.R p.1) (fun p => p.2)) := by
      refine all_intro (and_i (curry ?_) (curry ?_))
      · -- χ(b, φ) ∧ R(b, x) ⊢ φ(x)
        let L := andP (fun r : ((B.X × (A.X → Pr P)) × A.X) × (A.X → Pr P) => χ.F r.1.1.1 r.1.1.2)
          (andP (fun r => χ.F r.1.1.1 r.2) (memF A (fun r => r.1.2) (fun r => r.2)))
        have pull : Ent (andP (fun q : (B.X × (A.X → Pr P)) × A.X => χ.F q.1.1 q.1.2) (fun q => pbR χ (q.1.1, q.2))) (exP L) :=
          ent_of (fun _ _ ⟨a, b, hc, ha, ⟨φ', hb⟩⟩ => ⟨φ', a, b, hc, ha, hb⟩)
        have e : Ent L (fun r => (powO A).E r.2 r.1.1.2) := ent_and2 (ent_trans (and_r _ _) (and_l _ _)) (and_l _ _)
          (ent_re (fun r : ((B.X × (A.X → Pr P)) × A.X) × (A.X → Pr P) => (r.1.1.1, r.2, r.1.1.2)) χ.sv)
        have i : Ent L (IffF A (fun r => r.2) (fun r => r.1.1.2)) :=
          ent_trans e (ent_trans (pE_un (fun r : ((B.X × (A.X → Pr P)) × A.X) × (A.X → Pr P) => r.2) (fun r => r.1.1.2))
            (ent_trans (and_r _ _) (and_r _ _)))
        exact ent_trans (and_i (and_l _ _) (ent_trans (and_r _ _) (ent_re (fun q : (B.X × (A.X → Pr P)) × A.X => (q.1.1, q.2)) h1)))
          (ent_trans pull (ex_elim (ent_trans (and_i i (ent_trans (and_r _ _) (ent_trans (and_r _ _) (mem_at _ _))))
            (iff_fwd _ _ (fun r : ((B.X × (A.X → Pr P)) × A.X) × (A.X → Pr P) => r.1.2)))))
      · -- χ(b, φ) ∧ φ(x) ⊢ R(b, x)
        have gq : Ent (andP (fun q : (B.X × (A.X → Pr P)) × A.X => χ.F q.1.1 q.1.2) (fun q => q.1.2 q.2)) (GoodF A (fun q => q.1.2)) :=
          ent_trans (and_l _ _) (ent_re (fun q : (B.X × (A.X → Pr P)) × A.X => q.1) gφ)
        have exx : Ent (andP (fun q : (B.X × (A.X → Pr P)) × A.X => χ.F q.1.1 q.1.2) (fun q => q.1.2 q.2)) (fun q => A.E q.2 q.2) :=
          ent_trans (and_i gq (and_r _ _)) (good_str _ (fun q : (B.X × (A.X → Pr P)) × A.X => q.2))
        have inm : Ent (andP (fun q : (B.X × (A.X → Pr P)) × A.X => χ.F q.1.1 q.1.2) (fun q => q.1.2 q.2)) (fun q => pbR χ (q.1.1, q.2)) :=
          ex_intro (φ := fun r : ((B.X × (A.X → Pr P)) × A.X) × (A.X → Pr P) =>
              andP (fun r : ((B.X × (A.X → Pr P)) × A.X) × (A.X → Pr P) => χ.F r.1.1.1 r.2) (memF A (fun r => r.1.2) (fun r => r.2)) r)
            (fun q => q.1.2) (ent_trans (and_i (and_l _ _) (and_i exx (and_i gq (and_r _ _)))) (ent_of (fun _ _ h => h)))
        exact ent_trans inm (ent_re (fun q : (B.X × (A.X → Pr P)) × A.X => (q.1.1, q.2)) h2)
    exact ent_trans (and_i ebb (and_i (ent_trans ebb (goodR R (fun p : B.X × (A.X → Pr P) => p.1))) (and_i gφ iff)))
      (un_chi R (fun p : B.X × (A.X → Pr P) => p.1) (fun p => p.2))
  refine ⟨fwd, ?_⟩
  -- χ_R(b, φ) ⊢ χ(b, φ): χ is total, its value is χ_R-related, so equal to φ
  let L := andP (fun r : (B.X × (A.X → Pr P)) × (A.X → Pr P) => (chiF R).F r.1.1 r.1.2) (fun r => χ.F r.1.1 r.2)
  have pull : Ent (fun p : B.X × (A.X → Pr P) => (chiF R).F p.1 p.2) (exP L) :=
    ent_trans (and_i (ent_refl _) (ent_trans (ent_trans (chi_un R (fun p : B.X × (A.X → Pr P) => p.1) (fun p => p.2)) (and_l _ _))
      (ent_re (fun p : B.X × (A.X → Pr P) => p.1) χ.tot)))
      (ent_of (fun _ _ ⟨a, b, hc, ha, ⟨φ', hb⟩⟩ => ⟨φ', a, b, hc, ha, hb⟩))
  have e : Ent L (fun r => (powO A).E r.2 r.1.2) := ent_and2 (ent_trans (and_r _ _) (ent_re (fun r : (B.X × (A.X → Pr P)) × (A.X → Pr P) => (r.1.1, r.2)) fwd))
    (and_l _ _) (ent_re (fun r : (B.X × (A.X → Pr P)) × (A.X → Pr P) => (r.1.1, r.2, r.1.2)) (chiF R).sv)
  have ebb : Ent L (fun r => B.E r.1.1 r.1.1) := ent_trans (and_l _ _)
    (ent_trans (chi_un R (fun r : (B.X × (A.X → Pr P)) × (A.X → Pr P) => r.1.1) (fun r => r.1.2)) (and_l _ _))
  exact ent_trans pull (ex_elim (ent_and2 ebb (and_i (and_r _ _) e)
    (ent_re (fun r : (B.X × (A.X → Pr P)) × (A.X → Pr P) => ((r.1.1, r.1.1), (r.2, r.1.2))) χ.rel)))

end U

end Eff

end Found

#print axioms Found.Eff.iff_bwd
#print axioms Found.Eff.chi_pb
#print axioms Found.Eff.chi_uniq

found/Eff2.lean

lean_all/found/Eff2.lean

/-
  T96b, the effective topos, part 2. found/Eff2 — the realizability tripos
  over a PCA:
  * predicates on a type Γ: families Γ → (sets of realizers);
  * entailment φ ⊢ ψ: one realizer turns, uniformly in γ, every realizer of
    φ γ into a realizer of ψ γ;
  * ⊢ is a preorder; ⊤, ∧ (pairs), ⇒ (realizers of implications are
    functions), ∃ and ∀ over a variable, reindexing — with their rules
    (introduction, elimination, currying, evaluation), each realized by an
    explicit combinator.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Eff1

namespace Found

namespace Eff

variable {P : PCA}

def ρ0 (P : PCA) : Nat → P.A := fun _ => P.k

/-- a closed term denotes a defined element -/
theorem val_ex (t : CT P) : ∃ f, Ev (ρ0 P) (lam t) f := lam_def _ t

/-- two-argument abstraction: defined on the first argument -/
theorem lam2 {ρ : Nat → P.A} {b : CT P} {f : P.A} (hf : Ev ρ (lam (lam b)) f) (a : P.A) :
    ∃ g, P.ap f a g ∧ Ev (ext ρ a) (lam b) g :=
  match lam_def (ext ρ a) b with
  | ⟨g, hg⟩ => ⟨g, lam_beta ρ (lam b) hf hg, hg⟩

/-! ## combinators -/

/-- λx y z. z x y -/
def pairT : CT P := lam (lam (lam (.ap (.ap (.v 0) (.v 2)) (.v 1))))

/-- c is the pair of a and b -/
def IsPair (c a b : P.A) : Prop := ∃ pr g, Ev (ρ0 P) pairT pr ∧ P.ap pr a g ∧ P.ap g b c

theorem pair_ex (a b : P.A) : ∃ c, IsPair c a b :=
  match val_ex (P := P) (lam (lam (.ap (.ap (.v 0) (.v 2)) (.v 1)))) with
  | ⟨pr, hpr⟩ => match lam2 hpr a with
    | ⟨g, hg, hgv⟩ => match lam_def (ext (ext (ρ0 P) a) b) (.ap (.ap (.v 0) (.v 2)) (.v 1)) with
      | ⟨c, hc⟩ => ⟨c, pr, g, hpr, hg, lam_beta _ _ hgv hc⟩

theorem pair_use {c a b : P.A} (h : IsPair c a b) {z p v : P.A} (h1 : P.ap z a p) (h2 : P.ap p b v) : P.ap c z v :=
  match h with
  | ⟨pr, g, hpr, hg, hc⟩ => by
    -- g = value of lam (lam body) at a, c = value of lam body at a, b
    match lam2 hpr a with
    | ⟨g', hg', hgv⟩ =>
      have e := P.det hg hg'
      subst e
      match lam2 hgv b with
      | ⟨c', hc', hcv⟩ =>
        have e2 := P.det hc hc'
        subst e2
        exact lam_beta _ _ hcv ⟨p, b, ⟨z, a, rfl, rfl, h1⟩, rfl, h2⟩

def p0T : CT P := lam (.ap (.v 0) kT)
def p1T : CT P := lam (.ap (.v 0) (.ap kT iT))

theorem i_law (ρ : Nat → P.A) {i : P.A} (hi : Ev ρ iT i) (a : P.A) : P.ap i a a :=
  match hi with
  | ⟨c, k', ⟨s', k'', es, ek, h1⟩, ek2, h2⟩ => by
    subst es; subst ek; subst ek2
    exact match k_def a with
    | ⟨_, hka⟩ => s_law h1 h2 hka hka (k_law hka)

theorem p0_law {p0 c a b : P.A} (hp : Ev (ρ0 P) p0T p0) (h : IsPair c a b) : P.ap p0 c a :=
  match k_def a with
  | ⟨_, hka⟩ => lam_beta _ _ hp ⟨c, P.k, rfl, rfl, pair_use h hka (k_law hka)⟩

theorem p1_law {p1 c a b : P.A} (hp : Ev (ρ0 P) p1T p1) (h : IsPair c a b) : P.ap p1 c b :=
  match lam_def (P := P) (ρ0 P) (.v 0) with
  | ⟨i, hi⟩ => match k_def i with
    | ⟨ki, hki⟩ => lam_beta _ _ hp ⟨c, ki, rfl, ⟨P.k, i, rfl, hi, hki⟩, pair_use h (k_law hki) (i_law _ hi b)⟩

/-! ## predicates and entailment -/

abbrev Pr (P : PCA) : Type := P.A → Prop

def Ent {Γ : Type} (φ ψ : Γ → Pr P) : Prop := ∃ r, ∀ γ a, φ γ a → ∃ b, P.ap r a b ∧ ψ γ b

theorem ent_refl {Γ : Type} (φ : Γ → Pr P) : Ent φ φ :=
  match lam_def (P := P) (ρ0 P) (.v 0) with
  | ⟨i, hi⟩ => ⟨i, fun _ a h => ⟨a, i_law _ hi a, h⟩⟩

theorem ent_trans {Γ : Type} {φ ψ χ : Γ → Pr P} (h1 : Ent φ ψ) (h2 : Ent ψ χ) : Ent φ χ :=
  match h1, h2 with
  | ⟨r1, h1⟩, ⟨r2, h2⟩ => match val_ex (P := P) (.ap (.c r2) (.ap (.c r1) (.v 0))) with
    | ⟨f, hf⟩ => ⟨f, fun γ a ha => match h1 γ a ha with
      | ⟨b, hb, hψ⟩ => match h2 γ b hψ with
        | ⟨c, hc, hχ⟩ => ⟨c, lam_beta _ _ hf ⟨r2, b, rfl, ⟨r1, a, rfl, rfl, hb⟩, hc⟩, hχ⟩⟩

/-- reindexing -/
theorem ent_re {Γ Δ : Type} (f : Δ → Γ) {φ ψ : Γ → Pr P} (h : Ent φ ψ) :
    Ent (fun d => φ (f d)) (fun d => ψ (f d)) :=
  match h with
  | ⟨r, hr⟩ => ⟨r, fun d a ha => hr (f d) a ha⟩

def topP {Γ : Type} : Γ → Pr P := fun _ _ => True

theorem top_intro {Γ : Type} (φ : Γ → Pr P) : Ent φ topP :=
  match lam_def (P := P) (ρ0 P) (.v 0) with
  | ⟨i, hi⟩ => ⟨i, fun _ a _ => ⟨a, i_law _ hi a, trivial⟩⟩

/-- conjunction: realized by pairs -/
def andP {Γ : Type} (φ ψ : Γ → Pr P) : Γ → Pr P := fun γ c => ∃ a b, IsPair c a b ∧ φ γ a ∧ ψ γ b

theorem and_l {Γ : Type} (φ ψ : Γ → Pr P) : Ent (andP φ ψ) φ :=
  match val_ex (P := P) (.ap (.v 0) kT) with
  | ⟨p0, hp⟩ => ⟨p0, fun _ _ ⟨a, _, hc, ha, _⟩ => ⟨a, p0_law hp hc, ha⟩⟩

theorem and_r {Γ : Type} (φ ψ : Γ → Pr P) : Ent (andP φ ψ) ψ :=
  match val_ex (P := P) (.ap (.v 0) (.ap kT iT)) with
  | ⟨p1, hp⟩ => ⟨p1, fun _ _ ⟨_, b, hc, _, hb⟩ => ⟨b, p1_law hp hc, hb⟩⟩

/-- the pair of two applications, inside a term -/
theorem pair_app {r1 r2 a x y pr : P.A} (hpr : Ev (ρ0 P) pairT pr) (h1 : P.ap r1 a x) (h2 : P.ap r2 a y) :
    ∃ c, IsPair c x y ∧
      Ev (ext (ρ0 P) a) (.ap (.ap (.c pr) (.ap (.c r1) (.v 0))) (.ap (.c r2) (.v 0))) c :=
  match lam2 hpr x with
  | ⟨g, hg, hgv⟩ => match lam_def (ext (ext (ρ0 P) x) y) (.ap (.ap (.v 0) (.v 2)) (.v 1)) with
    | ⟨c, hc⟩ =>
      have hgc : P.ap g y c := lam_beta _ _ hgv hc
      ⟨c, ⟨pr, g, hpr, hg, hgc⟩, g, y, ⟨pr, x, rfl, ⟨r1, a, rfl, rfl, h1⟩, hg⟩, ⟨r2, a, rfl, rfl, h2⟩, hgc⟩

theorem and_i {Γ : Type} {χ φ ψ : Γ → Pr P} (h1 : Ent χ φ) (h2 : Ent χ ψ) : Ent χ (andP φ ψ) :=
  match h1, h2, val_ex (P := P) (lam (lam (.ap (.ap (.v 0) (.v 2)) (.v 1)))) with
  | ⟨r1, h1⟩, ⟨r2, h2⟩, ⟨pr, hpr⟩ =>
    match val_ex (P := P) (.ap (.ap (.c pr) (.ap (.c r1) (.v 0))) (.ap (.c r2) (.v 0))) with
    | ⟨f, hf⟩ => ⟨f, fun γ a ha => match h1 γ a ha, h2 γ a ha with
      | ⟨x, hx, hφ⟩, ⟨y, hy, hψ⟩ => match pair_app hpr hx hy with
        | ⟨c, hc, hev⟩ => ⟨c, lam_beta _ _ hf hev, x, y, hc, hφ, hψ⟩⟩

/-- implication: realized by functions -/
def impP {Γ : Type} (φ ψ : Γ → Pr P) : Γ → Pr P := fun γ f => ∀ a, φ γ a → ∃ b, P.ap f a b ∧ ψ γ b

theorem curry {Γ : Type} {χ φ ψ : Γ → Pr P} (h : Ent (andP χ φ) ψ) : Ent χ (impP φ ψ) :=
  match h, val_ex (P := P) (lam (lam (.ap (.ap (.v 0) (.v 2)) (.v 1)))) with
  | ⟨r, hr⟩, ⟨pr, hpr⟩ =>
    match val_ex (P := P) (lam (.ap (.c r) (.ap (.ap (.c pr) (.v 1)) (.v 0)))) with
    | ⟨f, hf⟩ => ⟨f, fun γ x hx => match lam2 hf x with
      | ⟨g, hg, hgv⟩ => ⟨g, hg, fun a ha => match pair_ex x a with
        | ⟨c, hc⟩ => match hr γ c ⟨x, a, hc, hx, ha⟩ with
          | ⟨b, hb, hψ⟩ => match hc with
            | ⟨_, g', hpr', hg', hc'⟩ =>
              ⟨b, lam_beta _ _ hgv ⟨r, c, rfl, ⟨g', a, ⟨pr, x, rfl, rfl, (ev_det _ _ hpr' hpr) ▸ hg'⟩, rfl, hc'⟩, hb⟩, hψ⟩⟩⟩

theorem eval_ent {Γ : Type} (φ ψ : Γ → Pr P) : Ent (andP (impP φ ψ) φ) ψ :=
  match val_ex (P := P) (.ap (.v 0) kT), val_ex (P := P) (.ap (.v 0) (.ap kT iT)) with
  | ⟨p0, hp0⟩, ⟨p1, hp1⟩ =>
    match val_ex (P := P) (.ap (.ap (.c p0) (.v 0)) (.ap (.c p1) (.v 0))) with
    | ⟨f, hf⟩ => ⟨f, fun _ c ⟨g, a, hc, hg, ha⟩ => match hg a ha with
      | ⟨b, hb, hψ⟩ => ⟨b, lam_beta _ _ hf ⟨g, a, ⟨p0, c, rfl, rfl, p0_law hp0 hc⟩, ⟨p1, c, rfl, rfl, p1_law hp1 hc⟩, hb⟩, hψ⟩⟩

/-! ## quantifiers -/

def exP {Γ Y : Type} (φ : Γ × Y → Pr P) : Γ → Pr P := fun γ a => ∃ y, φ (γ, y) a
theorem ex_intro {Γ Y : Type} {ψ : Γ → Pr P} {φ : Γ × Y → Pr P} (t : Γ → Y) (h : Ent ψ (fun γ => φ (γ, t γ))) :
    Ent ψ (exP φ) :=
  match h with
  | ⟨r, hr⟩ => ⟨r, fun γ a ha => match hr γ a ha with | ⟨b, hb, h'⟩ => ⟨b, hb, t γ, h'⟩⟩

theorem ex_elim {Γ Y : Type} {ψ : Γ → Pr P} {φ : Γ × Y → Pr P} (h : Ent φ (fun p => ψ p.1)) : Ent (exP φ) ψ :=
  match h with
  | ⟨r, hr⟩ => ⟨r, fun γ a ⟨y, hy⟩ => hr (γ, y) a hy⟩

/-- universal quantifier: realizers are thunks (applied to k they realize every instance) -/
def allP {Γ Y : Type} (φ : Γ × Y → Pr P) : Γ → Pr P := fun γ a => ∀ y, ∃ b, P.ap a P.k b ∧ φ (γ, y) b

theorem all_intro {Γ Y : Type} {ψ : Γ → Pr P} {φ : Γ × Y → Pr P} (h : Ent (fun p => ψ p.1) φ) : Ent ψ (allP φ) :=
  match h with
  | ⟨r, hr⟩ => match val_ex (P := P) (lam (.ap (.c r) (.v 1))) with
    | ⟨f, hf⟩ => ⟨f, fun γ a ha => match lam2 hf a with
      | ⟨g, hg, hgv⟩ => ⟨g, hg, fun y => match hr (γ, y) a ha with
        | ⟨b, hb, hφ⟩ => ⟨b, lam_beta _ _ hgv ⟨r, a, rfl, rfl, hb⟩, hφ⟩⟩⟩

theorem all_elim {Γ Y : Type} (φ : Γ × Y → Pr P) (t : Γ → Y) : Ent (allP φ) (fun γ => φ (γ, t γ)) :=
  match val_ex (P := P) (.ap (.v 0) kT) with
  | ⟨f, hf⟩ => ⟨f, fun γ a ha => match ha (t γ) with
    | ⟨b, hb, hφ⟩ => ⟨b, lam_beta _ _ hf ⟨a, P.k, rfl, rfl, hb⟩, hφ⟩⟩

end Eff

end Found

#print axioms Found.Eff.lam2
#print axioms Found.Eff.pair_ex
#print axioms Found.Eff.pair_use
#print axioms Found.Eff.p0_law
#print axioms Found.Eff.p1_law
#print axioms Found.Eff.ent_refl
#print axioms Found.Eff.ent_trans
#print axioms Found.Eff.ent_re
#print axioms Found.Eff.top_intro
#print axioms Found.Eff.and_l
#print axioms Found.Eff.and_r
#print axioms Found.Eff.and_i
#print axioms Found.Eff.curry
#print axioms Found.Eff.eval_ent
#print axioms Found.Eff.ex_intro
#print axioms Found.Eff.ex_elim
#print axioms Found.Eff.all_intro
#print axioms Found.Eff.all_elim

found/Eff3.lean

lean_all/found/Eff3.lean

/-
  T96b, the effective topos, part 3. found/Eff3 — objects and morphisms of
  the realizability topos (tripos-to-topos construction):
  * an object is a type X with a realizability equality E(x, x'),
    symmetric and transitive (uniformly realized);
  * a morphism is a functional relation F(x, y): strict, relational,
    single-valued and total; two morphisms are equal when they entail each
    other;
  * identities (E itself) and composition (G ∘ F)(x, z) = ∃y (F(x, y) ∧ G(y, z))
    are morphisms.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Eff2

namespace Found

namespace Eff

variable {P : PCA}

/-! ## derived rules -/

theorem and_mono {Γ : Type} {φ φ' ψ ψ' : Γ → Pr P} (h1 : Ent φ φ') (h2 : Ent ψ ψ') : Ent (andP φ ψ) (andP φ' ψ') :=
  and_i (ent_trans (and_l φ ψ) h1) (ent_trans (and_r φ ψ) h2)

theorem and_comm' {Γ : Type} (φ ψ : Γ → Pr P) : Ent (andP φ ψ) (andP ψ φ) := and_i (and_r φ ψ) (and_l φ ψ)

theorem and_dup {Γ : Type} (φ : Γ → Pr P) : Ent φ (andP φ φ) := and_i (ent_refl φ) (ent_refl φ)

/-- from φ ∧ ψ ⊢ χ and an entailment into φ and into ψ -/
theorem ent_and2 {Γ : Type} {α φ ψ χ : Γ → Pr P} (h1 : Ent α φ) (h2 : Ent α ψ) (h : Ent (andP φ ψ) χ) : Ent α χ :=
  ent_trans (and_i h1 h2) h

/-! ## objects -/

structure Obj (P : PCA) where
  X : Type
  E : X → X → Pr P
  sym : Ent (fun p : X × X => E p.1 p.2) (fun p => E p.2 p.1)
  tr : Ent (andP (fun p : X × X × X => E p.1 p.2.1) (fun p => E p.2.1 p.2.2)) (fun p => E p.1 p.2.2)

section O

variable (A : Obj P)

theorem sym' {Γ : Type} (f g : Γ → A.X) : Ent (fun γ => A.E (f γ) (g γ)) (fun γ => A.E (g γ) (f γ)) :=
  ent_re (fun γ => (f γ, g γ)) A.sym

theorem tr' {Γ : Type} (f g h : Γ → A.X) :
    Ent (andP (fun γ => A.E (f γ) (g γ)) (fun γ => A.E (g γ) (h γ))) (fun γ => A.E (f γ) (h γ)) :=
  ent_re (fun γ => (f γ, g γ, h γ)) A.tr

/-- E(x, y) ⊢ E(x, x) -/
theorem refl_l {Γ : Type} (f g : Γ → A.X) : Ent (fun γ => A.E (f γ) (g γ)) (fun γ => A.E (f γ) (f γ)) :=
  ent_and2 (ent_refl _) (sym' A f g) (tr' A f g f)

/-- E(x, y) ⊢ E(y, y) -/
theorem refl_r {Γ : Type} (f g : Γ → A.X) : Ent (fun γ => A.E (f γ) (g γ)) (fun γ => A.E (g γ) (g γ)) :=
  ent_trans (sym' A f g) (refl_l A g f)

end O

/-! ## morphisms: functional relations -/

structure Fn (A B : Obj P) where
  F : A.X → B.X → Pr P
  st : Ent (fun p : A.X × B.X => F p.1 p.2) (andP (fun p => A.E p.1 p.1) (fun p => B.E p.2 p.2))
  rel : Ent (andP (fun p : (A.X × A.X) × (B.X × B.X) => A.E p.1.1 p.1.2)
      (andP (fun p => F p.1.1 p.2.1) (fun p => B.E p.2.1 p.2.2))) (fun p => F p.1.2 p.2.2)
  sv : Ent (andP (fun p : A.X × B.X × B.X => F p.1 p.2.1) (fun p => F p.1 p.2.2)) (fun p => B.E p.2.1 p.2.2)
  tot : Ent (fun x : A.X => A.E x x) (exP (fun p : A.X × B.X => F p.1 p.2))

/-- equality of morphisms -/
def FEq {A B : Obj P} (F G : Fn A B) : Prop :=
  Ent (fun p : A.X × B.X => F.F p.1 p.2) (fun p => G.F p.1 p.2) ∧
  Ent (fun p : A.X × B.X => G.F p.1 p.2) (fun p => F.F p.1 p.2)

theorem feq_refl {A B : Obj P} (F : Fn A B) : FEq F F := ⟨ent_refl _, ent_refl _⟩
theorem feq_symm {A B : Obj P} {F G : Fn A B} (h : FEq F G) : FEq G F := ⟨h.2, h.1⟩
theorem feq_trans {A B : Obj P} {F G H : Fn A B} (h1 : FEq F G) (h2 : FEq G H) : FEq F H :=
  ⟨ent_trans h1.1 h2.1, ent_trans h2.2 h1.2⟩

/-- the identity -/
def idF (A : Obj P) : Fn A A where
  F := A.E
  st := and_i (refl_l A (fun p : A.X × A.X => p.1) (fun p => p.2)) (refl_r A (fun p : A.X × A.X => p.1) (fun p => p.2))
  rel :=
    -- E(x, x') ∧ E(x, y) ∧ E(y, y') ⊢ E(x', y')
    ent_and2 (ent_trans (and_l _ _) (sym' A (fun p : (A.X × A.X) × (A.X × A.X) => p.1.1) (fun p => p.1.2)))
      (ent_trans (and_r _ _) (tr' A (fun p : (A.X × A.X) × (A.X × A.X) => p.1.1) (fun p => p.2.1) (fun p => p.2.2)))
      (tr' A (fun p : (A.X × A.X) × (A.X × A.X) => p.1.2) (fun p => p.1.1) (fun p => p.2.2))
  sv := ent_and2 (ent_trans (and_l _ _) (sym' A (fun p : A.X × A.X × A.X => p.1) (fun p => p.2.1)))
      (and_r _ _) (tr' A (fun p : A.X × A.X × A.X => p.2.1) (fun p => p.1) (fun p => p.2.2))
  tot := ex_intro (fun x => x) (ent_refl _)

/-! ## structural entailments (identity realizer) -/

theorem ent_of {Γ : Type} {φ ψ : Γ → Pr P} (h : ∀ γ a, φ γ a → ψ γ a) : Ent φ ψ :=
  match ent_refl φ with
  | ⟨i, hi⟩ => ⟨i, fun γ a ha => match hi γ a ha with
    | ⟨b, hb, hφ⟩ => ⟨b, hb, h γ b hφ⟩⟩

/-- composition of functional relations -/
def compR {A B C : Obj P} (F : Fn A B) (G : Fn B C) (x : A.X) (z : C.X) : Pr P :=
  fun c => ∃ y, andP (fun _ : Unit => F.F x y) (fun _ => G.F y z) () c

/-- the body of the composite, over ((x, z), y) -/
def compB {A B C : Obj P} (F : Fn A B) (G : Fn B C) : (A.X × C.X) × B.X → Pr P :=
  andP (fun q => F.F q.1.1 q.2) (fun q => G.F q.2 q.1.2)

theorem compR_ex {A B C : Obj P} (F : Fn A B) (G : Fn B C) :
    Ent (fun p : A.X × C.X => compR F G p.1 p.2) (exP (compB F G)) := ent_of (fun _ _ h => h)

theorem ex_compR {A B C : Obj P} (F : Fn A B) (G : Fn B C) :
    Ent (exP (compB F G)) (fun p : A.X × C.X => compR F G p.1 p.2) := ent_of (fun _ _ h => h)

/-- the composite of functional relations is functional -/
def compF {A B C : Obj P} (F : Fn A B) (G : Fn B C) : Fn A C where
  F := compR F G
  st := ent_trans (compR_ex F G) (ex_elim (and_mono
      (ent_trans (ent_re (fun q : (A.X × C.X) × B.X => (q.1.1, q.2)) F.st) (and_l _ _))
      (ent_trans (ent_re (fun q : (A.X × C.X) × B.X => (q.2, q.1.2)) G.st) (and_r _ _))))
  rel := by
    let L : ((A.X × A.X) × (C.X × C.X)) × B.X → Pr P :=
      andP (fun q => A.E q.1.1.1 q.1.1.2)
        (andP (andP (fun q => F.F q.1.1.1 q.2) (fun q => G.F q.2 q.1.2.1)) (fun q => C.E q.1.2.1 q.1.2.2))
    have pull : Ent (andP (fun p : (A.X × A.X) × (C.X × C.X) => A.E p.1.1 p.1.2)
        (andP (fun p => compR F G p.1.1 p.2.1) (fun p => C.E p.2.1 p.2.2))) (exP L) :=
      ent_of (fun _ c ⟨a, d, hc, ha, ⟨e, b, hd, ⟨y, hy⟩, hb⟩⟩ => ⟨y, a, d, hc, ha, e, b, hd, hy, hb⟩)
    have exx : Ent L (fun q => A.E q.1.1.1 q.1.1.2) := and_l _ _
    have fxy : Ent L (fun q => F.F q.1.1.1 q.2) := ent_trans (and_r _ _) (ent_trans (and_l _ _) (and_l _ _))
    have gyz : Ent L (fun q => G.F q.2 q.1.2.1) := ent_trans (and_r _ _) (ent_trans (and_l _ _) (and_r _ _))
    have ezz : Ent L (fun q => C.E q.1.2.1 q.1.2.2) := ent_trans (and_r _ _) (and_r _ _)
    have eyy : Ent L (fun q => B.E q.2 q.2) :=
      ent_trans fxy (ent_trans (ent_re (fun q : ((A.X × A.X) × (C.X × C.X)) × B.X => (q.1.1.1, q.2)) F.st) (and_r _ _))
    have fx'y : Ent L (fun q => F.F q.1.1.2 q.2) := ent_and2 exx (and_i fxy eyy)
      (ent_re (fun q : ((A.X × A.X) × (C.X × C.X)) × B.X => ((q.1.1.1, q.1.1.2), (q.2, q.2))) F.rel)
    have gyz' : Ent L (fun q => G.F q.2 q.1.2.2) := ent_and2 eyy (and_i gyz ezz)
      (ent_re (fun q : ((A.X × A.X) × (C.X × C.X)) × B.X => ((q.2, q.2), (q.1.2.1, q.1.2.2))) G.rel)
    exact ent_trans pull (ex_elim (ent_trans (and_i fx'y gyz')
      (ent_of (fun q _ h => ⟨q.2, h⟩))))
  sv := by
    let L : (A.X × C.X × C.X) × (B.X × B.X) → Pr P :=
      andP (andP (fun q => F.F q.1.1 q.2.1) (fun q => G.F q.2.1 q.1.2.1))
        (andP (fun q => F.F q.1.1 q.2.2) (fun q => G.F q.2.2 q.1.2.2))
    have pull : Ent (andP (fun p : A.X × C.X × C.X => compR F G p.1 p.2.1) (fun p => compR F G p.1 p.2.2))
        (exP L) :=
      ent_of (fun _ c ⟨a, b, hc, ⟨y, ha⟩, ⟨y', hb⟩⟩ => ⟨(y, y'), a, b, hc, ha, hb⟩)
    have fxy : Ent L (fun q => F.F q.1.1 q.2.1) := ent_trans (and_l _ _) (and_l _ _)
    have gyz : Ent L (fun q => G.F q.2.1 q.1.2.1) := ent_trans (and_l _ _) (and_r _ _)
    have fxy' : Ent L (fun q => F.F q.1.1 q.2.2) := ent_trans (and_r _ _) (and_l _ _)
    have gyz' : Ent L (fun q => G.F q.2.2 q.1.2.2) := ent_trans (and_r _ _) (and_r _ _)
    have eyy' : Ent L (fun q => B.E q.2.1 q.2.2) := ent_and2 fxy fxy'
      (ent_re (fun q : (A.X × C.X × C.X) × (B.X × B.X) => (q.1.1, q.2.1, q.2.2)) F.sv)
    have ezz : Ent L (fun q => C.E q.1.2.1 q.1.2.1) :=
      ent_trans gyz (ent_trans (ent_re (fun q : (A.X × C.X × C.X) × (B.X × B.X) => (q.2.1, q.1.2.1)) G.st) (and_r _ _))
    have gy'z : Ent L (fun q => G.F q.2.2 q.1.2.1) := ent_and2 eyy' (and_i gyz ezz)
      (ent_re (fun q : (A.X × C.X × C.X) × (B.X × B.X) => ((q.2.1, q.2.2), (q.1.2.1, q.1.2.1))) G.rel)
    exact ent_trans pull (ex_elim (ent_and2 gy'z gyz'
      (ent_re (fun q : (A.X × C.X × C.X) × (B.X × B.X) => (q.2.2, q.1.2.1, q.1.2.2)) G.sv)))
  tot := ent_trans F.tot (ex_elim (ent_trans
      (and_i (ent_refl (fun q : A.X × B.X => F.F q.1 q.2))
        (ent_trans (ent_trans (ent_re (fun q : A.X × B.X => q) F.st) (and_r _ _))
          (ent_re (fun q : A.X × B.X => q.2) G.tot)))
      (ent_of (fun q _ ⟨a, b, hc, ha, ⟨z, hb⟩⟩ => ⟨z, q.2, a, b, hc, ha, hb⟩))))

end Eff

end Found


#print axioms Found.Eff.and_mono
#print axioms Found.Eff.refl_l
#print axioms Found.Eff.refl_r
#print axioms Found.Eff.idF
#print axioms Found.Eff.compF

found/Eff4.lean

lean_all/found/Eff4.lean

/-
  T96b, the effective topos, part 4. found/Eff4 — the category of the
  realizability topos: composition of functional relations respects
  equality of morphisms, the identity relation is a unit on both sides, and
  composition is associative (up to equality of morphisms).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Eff3

namespace Found

namespace Eff

variable {P : PCA}

/-- entailment passes under an existential -/
theorem ex_mono {Γ Y : Type} {φ ψ : Γ × Y → Pr P} (h : Ent φ ψ) : Ent (exP φ) (exP ψ) :=
  match h with
  | ⟨r, hr⟩ => ⟨r, fun γ a ⟨y, hy⟩ => match hr (γ, y) a hy with
    | ⟨b, h1, h2⟩ => ⟨b, h1, y, h2⟩⟩

section C

variable {A B C D : Obj P}

/-- composition respects equality of morphisms -/
theorem comp_resp {F F' : Fn A B} {G G' : Fn B C} (hF : FEq F F') (hG : FEq G G') : FEq (compF F G) (compF F' G') :=
  ⟨ent_trans (compR_ex F G) (ent_trans (ex_mono (and_mono
      (ent_re (fun q : (A.X × C.X) × B.X => (q.1.1, q.2)) hF.1) (ent_re (fun q : (A.X × C.X) × B.X => (q.2, q.1.2)) hG.1)))
      (ex_compR F' G')),
   ent_trans (compR_ex F' G') (ent_trans (ex_mono (and_mono
      (ent_re (fun q : (A.X × C.X) × B.X => (q.1.1, q.2)) hF.2) (ent_re (fun q : (A.X × C.X) × B.X => (q.2, q.1.2)) hG.2)))
      (ex_compR F G))⟩

/-- the identity is a left unit -/
theorem id_l (F : Fn A B) : FEq (compF (idF A) F) F := by
  refine ⟨ent_trans (compR_ex (idF A) F) (ex_elim ?_), ent_trans (ex_intro (fun p : A.X × B.X => p.1) ?_) (ex_compR (idF A) F)⟩
  · -- E(x, x') ∧ F(x', y) ⊢ F(x, y)
    have eyy : Ent (compB (idF A) F) (fun q => B.E q.1.2 q.1.2) :=
      ent_trans (and_r _ _) (ent_trans (ent_re (fun q : (A.X × B.X) × A.X => (q.2, q.1.2)) F.st) (and_r _ _))
    exact ent_and2 (ent_trans (and_l _ _) (sym' A (fun q : (A.X × B.X) × A.X => q.1.1) (fun q => q.2)))
      (and_i (and_r _ _) eyy) (ent_re (fun q : (A.X × B.X) × A.X => ((q.2, q.1.1), (q.1.2, q.1.2))) F.rel)
  · -- F(x, y) ⊢ E(x, x) ∧ F(x, y)
    exact and_i (ent_trans (F.st) (and_l _ _)) (ent_refl _)

/-- the identity is a right unit -/
theorem id_r (F : Fn A B) : FEq (compF F (idF B)) F := by
  refine ⟨ent_trans (compR_ex F (idF B)) (ex_elim ?_), ent_trans (ex_intro (fun p : A.X × B.X => p.2) ?_) (ex_compR F (idF B))⟩
  · -- F(x, y') ∧ E(y', y) ⊢ F(x, y)
    have exx : Ent (compB F (idF B)) (fun q => A.E q.1.1 q.1.1) :=
      ent_trans (and_l _ _) (ent_trans (ent_re (fun q : (A.X × B.X) × B.X => (q.1.1, q.2)) F.st) (and_l _ _))
    exact ent_and2 exx (and_i (and_l _ _) (and_r _ _))
      (ent_re (fun q : (A.X × B.X) × B.X => ((q.1.1, q.1.1), (q.2, q.1.2))) F.rel)
  · -- F(x, y) ⊢ F(x, y) ∧ E(y, y)
    exact and_i (ent_refl _) (ent_trans (F.st) (and_r _ _))

/-- composition is associative -/
theorem assoc (F : Fn A B) (G : Fn B C) (H : Fn C D) : FEq (compF (compF F G) H) (compF F (compF G H)) := by
  let L1 : (A.X × D.X) × (B.X × C.X) → Pr P :=
    andP (andP (fun q => F.F q.1.1 q.2.1) (fun q => G.F q.2.1 q.2.2)) (fun q => H.F q.2.2 q.1.2)
  let L2 : (A.X × D.X) × (B.X × C.X) → Pr P :=
    andP (fun q => F.F q.1.1 q.2.1) (andP (fun q => G.F q.2.1 q.2.2) (fun q => H.F q.2.2 q.1.2))
  have pull1 : Ent (fun p : A.X × D.X => compR (compF F G) H p.1 p.2) (exP L1) :=
    ent_of (fun _ c ⟨z, a, b, hc, ⟨y, ha⟩, hb⟩ => ⟨(y, z), a, b, hc, ha, hb⟩)
  have push1 : Ent (exP L1) (fun p : A.X × D.X => compR (compF F G) H p.1 p.2) :=
    ent_of (fun _ c ⟨(y, z), a, b, hc, ha, hb⟩ => ⟨z, a, b, hc, ⟨y, ha⟩, hb⟩)
  have pull2 : Ent (fun p : A.X × D.X => compR F (compF G H) p.1 p.2) (exP L2) :=
    ent_of (fun _ c ⟨y, a, b, hc, ha, ⟨z, hb⟩⟩ => ⟨(y, z), a, b, hc, ha, hb⟩)
  have push2 : Ent (exP L2) (fun p : A.X × D.X => compR F (compF G H) p.1 p.2) :=
    ent_of (fun _ c ⟨(y, z), a, b, hc, ha, hb⟩ => ⟨y, a, b, hc, ha, ⟨z, hb⟩⟩)
  have r12 : Ent L1 L2 := and_i (ent_trans (and_l _ _) (and_l _ _)) (and_i (ent_trans (and_l _ _) (and_r _ _)) (and_r _ _))
  have r21 : Ent L2 L1 := and_i (and_i (and_l _ _) (ent_trans (and_r _ _) (and_l _ _))) (ent_trans (and_r _ _) (and_r _ _))
  exact ⟨ent_trans pull1 (ent_trans (ex_mono r12) push2), ent_trans pull2 (ent_trans (ex_mono r21) push1)⟩

end C

end Eff

end Found

#print axioms Found.Eff.ex_mono
#print axioms Found.Eff.comp_resp
#print axioms Found.Eff.id_l
#print axioms Found.Eff.id_r
#print axioms Found.Eff.assoc

found/Eff5.lean

lean_all/found/Eff5.lean

/-
  T96b, the effective topos, part 5. found/Eff5 — finite limits, part 1:
  the terminal object (one point, every realizer) with its unique arrows,
  and binary products (pairs, equality realized by pairs of equalities)
  with projections, pairing and the universal property.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Eff4

namespace Found

namespace Eff

variable {P : PCA}

/-! ## the terminal object -/

def one (P : PCA) : Obj P where
  X := Unit
  E := fun _ _ _ => True
  sym := ent_of (fun _ _ h => h)
  tr := top_intro _

/-- the arrow to the terminal object -/
def bang (A : Obj P) : Fn A (one P) where
  F := fun x _ => A.E x x
  st := and_i (ent_refl _) (top_intro _)
  rel := ent_trans (and_l _ _) (refl_r A (fun p : (A.X × A.X) × (Unit × Unit) => p.1.1) (fun p => p.1.2))
  sv := top_intro _
  tot := ex_intro (fun _ => ()) (ent_refl _)

/-- every arrow to the terminal object is the bang -/
theorem bang_uniq {A : Obj P} (G : Fn A (one P)) : FEq G (bang A) :=
  ⟨ent_trans G.st (and_l _ _), ent_trans (ent_re (fun p : A.X × Unit => p.1) G.tot) (ent_of (fun _ _ ⟨_, h⟩ => h))⟩

/-! ## binary products -/

def prodO (A B : Obj P) : Obj P where
  X := A.X × B.X
  E := fun u v => andP (fun _ : Unit => A.E u.1 v.1) (fun _ => B.E u.2 v.2) ()
  sym := ent_trans (ent_of (fun _ _ h => h) : Ent (fun p : (A.X × B.X) × (A.X × B.X) => andP (fun _ : Unit => A.E p.1.1 p.2.1) (fun _ => B.E p.1.2 p.2.2) ())
      (andP (fun p : (A.X × B.X) × (A.X × B.X) => A.E p.1.1 p.2.1) (fun p => B.E p.1.2 p.2.2)))
    (ent_trans (and_mono (sym' A (fun p : (A.X × B.X) × (A.X × B.X) => p.1.1) (fun p => p.2.1))
      (sym' B (fun p : (A.X × B.X) × (A.X × B.X) => p.1.2) (fun p => p.2.2))) (ent_of (fun _ _ h => h)))
  tr := by
    let L : (A.X × B.X) × (A.X × B.X) × (A.X × B.X) → Pr P :=
      andP (andP (fun p => A.E p.1.1 p.2.1.1) (fun p => B.E p.1.2 p.2.1.2))
        (andP (fun p => A.E p.2.1.1 p.2.2.1) (fun p => B.E p.2.1.2 p.2.2.2))
    have pull : Ent (andP (fun p : (A.X × B.X) × (A.X × B.X) × (A.X × B.X) =>
        andP (fun _ : Unit => A.E p.1.1 p.2.1.1) (fun _ => B.E p.1.2 p.2.1.2) ())
        (fun p => andP (fun _ : Unit => A.E p.2.1.1 p.2.2.1) (fun _ => B.E p.2.1.2 p.2.2.2) ())) L := ent_of (fun _ _ h => h)
    exact ent_trans pull (ent_trans (and_i
      (ent_and2 (ent_trans (and_l _ _) (and_l _ _)) (ent_trans (and_r _ _) (and_l _ _))
        (tr' A (fun p : (A.X × B.X) × (A.X × B.X) × (A.X × B.X) => p.1.1) (fun p => p.2.1.1) (fun p => p.2.2.1)))
      (ent_and2 (ent_trans (and_l _ _) (and_r _ _)) (ent_trans (and_r _ _) (and_r _ _))
        (tr' B (fun p : (A.X × B.X) × (A.X × B.X) × (A.X × B.X) => p.1.2) (fun p => p.2.1.2) (fun p => p.2.2.2))))
      (ent_of (fun _ _ h => h)))

/-- unfolding the product equality into a pair -/
theorem pE (A B : Obj P) {Γ : Type} (f g : Γ → A.X × B.X) :
    Ent (fun γ => (prodO A B).E (f γ) (g γ)) (andP (fun γ => A.E (f γ).1 (g γ).1) (fun γ => B.E (f γ).2 (g γ).2)) :=
  ent_of (fun _ _ h => h)

theorem Ep (A B : Obj P) {Γ : Type} (f g : Γ → A.X × B.X) :
    Ent (andP (fun γ => A.E (f γ).1 (g γ).1) (fun γ => B.E (f γ).2 (g γ).2)) (fun γ => (prodO A B).E (f γ) (g γ)) :=
  ent_of (fun _ _ h => h)

/-- witnesses mapped into another existential -/
theorem ex_into {Γ Y Z : Type} {φ : Γ × Y → Pr P} {ψ : Γ × Z → Pr P} (t : Γ × Y → Z) (h : Ent φ (fun q => ψ (q.1, t q))) :
    Ent (exP φ) (exP ψ) :=
  match h with
  | ⟨r, hr⟩ => ⟨r, fun γ a ⟨y, hy⟩ => match hr (γ, y) a hy with
    | ⟨b, h1, h2⟩ => ⟨b, h1, t (γ, y), h2⟩⟩

section Pr

variable (A B : Obj P)

/-- the first projection -/
def pi1 : Fn (prodO A B) A where
  F := fun u a => andP (fun _ : Unit => A.E u.1 a) (fun _ => B.E u.2 u.2) ()
  st := by
    have h : Ent (fun p : (A.X × B.X) × A.X => andP (fun _ : Unit => A.E p.1.1 p.2) (fun _ => B.E p.1.2 p.1.2) ())
        (andP (fun p : (A.X × B.X) × A.X => A.E p.1.1 p.2) (fun p => B.E p.1.2 p.1.2)) := ent_of (fun _ _ h => h)
    exact ent_trans h (and_i (ent_trans (and_i (ent_trans (and_l _ _) (refl_l A (fun p : (A.X × B.X) × A.X => p.1.1) (fun p => p.2)))
      (and_r _ _)) (Ep A B (fun p : (A.X × B.X) × A.X => p.1) (fun p => p.1)))
      (ent_trans (and_l _ _) (refl_r A (fun p : (A.X × B.X) × A.X => p.1.1) (fun p => p.2))))
  rel := by
    let L : ((A.X × B.X) × (A.X × B.X)) × (A.X × A.X) → Pr P :=
      andP (andP (fun p => A.E p.1.1.1 p.1.2.1) (fun p => B.E p.1.1.2 p.1.2.2))
        (andP (andP (fun p => A.E p.1.1.1 p.2.1) (fun p => B.E p.1.1.2 p.1.1.2)) (fun p => A.E p.2.1 p.2.2))
    have pull : Ent (andP (fun p : ((A.X × B.X) × (A.X × B.X)) × (A.X × A.X) => (prodO A B).E p.1.1 p.1.2)
        (andP (fun p => andP (fun _ : Unit => A.E p.1.1.1 p.2.1) (fun _ => B.E p.1.1.2 p.1.1.2) ()) (fun p => A.E p.2.1 p.2.2))) L :=
      ent_of (fun _ _ h => h)
    have a1 : Ent L (fun p => A.E p.1.2.1 p.2.2) := ent_and2
      (ent_trans (ent_trans (and_l _ _) (and_l _ _)) (sym' A (fun p : ((A.X × B.X) × (A.X × B.X)) × (A.X × A.X) => p.1.1.1) (fun p => p.1.2.1)))
      (ent_and2 (ent_trans (ent_trans (and_r _ _) (and_l _ _)) (and_l _ _)) (ent_trans (and_r _ _) (and_r _ _))
        (tr' A (fun p : ((A.X × B.X) × (A.X × B.X)) × (A.X × A.X) => p.1.1.1) (fun p => p.2.1) (fun p => p.2.2)))
      (tr' A (fun p : ((A.X × B.X) × (A.X × B.X)) × (A.X × A.X) => p.1.2.1) (fun p => p.1.1.1) (fun p => p.2.2))
    have b1 : Ent L (fun p => B.E p.1.2.2 p.1.2.2) :=
      ent_trans (ent_trans (and_l _ _) (and_r _ _)) (refl_r B (fun p : ((A.X × B.X) × (A.X × B.X)) × (A.X × A.X) => p.1.1.2) (fun p => p.1.2.2))
    exact ent_trans pull (ent_trans (and_i a1 b1) (ent_of (fun _ _ h => h)))
  sv := by
    have h : Ent (andP (fun p : (A.X × B.X) × A.X × A.X => andP (fun _ : Unit => A.E p.1.1 p.2.1) (fun _ => B.E p.1.2 p.1.2) ())
        (fun p => andP (fun _ : Unit => A.E p.1.1 p.2.2) (fun _ => B.E p.1.2 p.1.2) ()))
        (andP (andP (fun p : (A.X × B.X) × A.X × A.X => A.E p.1.1 p.2.1) (fun p => B.E p.1.2 p.1.2))
          (andP (fun p => A.E p.1.1 p.2.2) (fun p => B.E p.1.2 p.1.2))) := ent_of (fun _ _ h => h)
    exact ent_trans h (ent_and2 (ent_trans (ent_trans (and_l _ _) (and_l _ _)) (sym' A (fun p : (A.X × B.X) × A.X × A.X => p.1.1) (fun p => p.2.1)))
      (ent_trans (and_r _ _) (and_l _ _)) (tr' A (fun p : (A.X × B.X) × A.X × A.X => p.2.1) (fun p => p.1.1) (fun p => p.2.2)))
  tot := ex_intro (fun u => u.1) (ent_of (fun _ _ h => h))

/-- the second projection -/
def pi2 : Fn (prodO A B) B where
  F := fun u b => andP (fun _ : Unit => A.E u.1 u.1) (fun _ => B.E u.2 b) ()
  st := by
    have h : Ent (fun p : (A.X × B.X) × B.X => andP (fun _ : Unit => A.E p.1.1 p.1.1) (fun _ => B.E p.1.2 p.2) ())
        (andP (fun p : (A.X × B.X) × B.X => A.E p.1.1 p.1.1) (fun p => B.E p.1.2 p.2)) := ent_of (fun _ _ h => h)
    exact ent_trans h (and_i (ent_trans (and_i (and_l _ _) (ent_trans (and_r _ _) (refl_l B (fun p : (A.X × B.X) × B.X => p.1.2) (fun p => p.2))))
      (Ep A B (fun p : (A.X × B.X) × B.X => p.1) (fun p => p.1)))
      (ent_trans (and_r _ _) (refl_r B (fun p : (A.X × B.X) × B.X => p.1.2) (fun p => p.2))))
  rel := by
    let L : ((A.X × B.X) × (A.X × B.X)) × (B.X × B.X) → Pr P :=
      andP (andP (fun p => A.E p.1.1.1 p.1.2.1) (fun p => B.E p.1.1.2 p.1.2.2))
        (andP (andP (fun p => A.E p.1.1.1 p.1.1.1) (fun p => B.E p.1.1.2 p.2.1)) (fun p => B.E p.2.1 p.2.2))
    have pull : Ent (andP (fun p : ((A.X × B.X) × (A.X × B.X)) × (B.X × B.X) => (prodO A B).E p.1.1 p.1.2)
        (andP (fun p => andP (fun _ : Unit => A.E p.1.1.1 p.1.1.1) (fun _ => B.E p.1.1.2 p.2.1) ()) (fun p => B.E p.2.1 p.2.2))) L :=
      ent_of (fun _ _ h => h)
    have a1 : Ent L (fun p => A.E p.1.2.1 p.1.2.1) :=
      ent_trans (ent_trans (and_l _ _) (and_l _ _)) (refl_r A (fun p : ((A.X × B.X) × (A.X × B.X)) × (B.X × B.X) => p.1.1.1) (fun p => p.1.2.1))
    have b1 : Ent L (fun p => B.E p.1.2.2 p.2.2) := ent_and2
      (ent_trans (ent_trans (and_l _ _) (and_r _ _)) (sym' B (fun p : ((A.X × B.X) × (A.X × B.X)) × (B.X × B.X) => p.1.1.2) (fun p => p.1.2.2)))
      (ent_and2 (ent_trans (ent_trans (and_r _ _) (and_l _ _)) (and_r _ _)) (ent_trans (and_r _ _) (and_r _ _))
        (tr' B (fun p : ((A.X × B.X) × (A.X × B.X)) × (B.X × B.X) => p.1.1.2) (fun p => p.2.1) (fun p => p.2.2)))
      (tr' B (fun p : ((A.X × B.X) × (A.X × B.X)) × (B.X × B.X) => p.1.2.2) (fun p => p.1.1.2) (fun p => p.2.2))
    exact ent_trans pull (ent_trans (and_i a1 b1) (ent_of (fun _ _ h => h)))
  sv := by
    have h : Ent (andP (fun p : (A.X × B.X) × B.X × B.X => andP (fun _ : Unit => A.E p.1.1 p.1.1) (fun _ => B.E p.1.2 p.2.1) ())
        (fun p => andP (fun _ : Unit => A.E p.1.1 p.1.1) (fun _ => B.E p.1.2 p.2.2) ()))
        (andP (andP (fun p : (A.X × B.X) × B.X × B.X => A.E p.1.1 p.1.1) (fun p => B.E p.1.2 p.2.1))
          (andP (fun p => A.E p.1.1 p.1.1) (fun p => B.E p.1.2 p.2.2))) := ent_of (fun _ _ h => h)
    exact ent_trans h (ent_and2 (ent_trans (ent_trans (and_l _ _) (and_r _ _)) (sym' B (fun p : (A.X × B.X) × B.X × B.X => p.1.2) (fun p => p.2.1)))
      (ent_trans (and_r _ _) (and_r _ _)) (tr' B (fun p : (A.X × B.X) × B.X × B.X => p.2.1) (fun p => p.1.2) (fun p => p.2.2)))
  tot := ex_intro (fun u => u.2) (ent_of (fun _ _ h => h))

end Pr

end Eff

end Found

#print axioms Found.Eff.bang
#print axioms Found.Eff.bang_uniq
#print axioms Found.Eff.prodO
#print axioms Found.Eff.pi1
#print axioms Found.Eff.pi2

found/Eff6.lean

lean_all/found/Eff6.lean

/-
  T96b, the effective topos, part 6. found/Eff6 — finite limits, part 2:
  pairing of two arrows into a product, the two projection equations, and
  uniqueness: the product is a product in the realizability category.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Eff5

namespace Found

namespace Eff

variable {P : PCA}

section Pa

variable {A B C : Obj P} (F : Fn C A) (G : Fn C B)

/-- pairing -/
def pairF : Fn C (prodO A B) where
  F := fun c u => andP (fun _ : Unit => F.F c u.1) (fun _ => G.F c u.2) ()
  st := by
    have h : Ent (fun p : C.X × (A.X × B.X) => andP (fun _ : Unit => F.F p.1 p.2.1) (fun _ => G.F p.1 p.2.2) ())
        (andP (fun p : C.X × (A.X × B.X) => F.F p.1 p.2.1) (fun p => G.F p.1 p.2.2)) := ent_of (fun _ _ h => h)
    have f := ent_re (fun p : C.X × (A.X × B.X) => (p.1, p.2.1)) F.st
    have g := ent_re (fun p : C.X × (A.X × B.X) => (p.1, p.2.2)) G.st
    exact ent_trans h (and_i (ent_trans (and_l _ _) (ent_trans f (and_l _ _)))
      (ent_trans (and_mono (ent_trans f (and_r _ _)) (ent_trans g (and_r _ _)))
        (Ep A B (fun p : C.X × (A.X × B.X) => p.2) (fun p => p.2))))
  rel := by
    let L : (C.X × C.X) × ((A.X × B.X) × (A.X × B.X)) → Pr P :=
      andP (fun p => C.E p.1.1 p.1.2) (andP (andP (fun p => F.F p.1.1 p.2.1.1) (fun p => G.F p.1.1 p.2.1.2))
        (andP (fun p => A.E p.2.1.1 p.2.2.1) (fun p => B.E p.2.1.2 p.2.2.2)))
    have pull : Ent (andP (fun p : (C.X × C.X) × ((A.X × B.X) × (A.X × B.X)) => C.E p.1.1 p.1.2)
        (andP (fun p => andP (fun _ : Unit => F.F p.1.1 p.2.1.1) (fun _ => G.F p.1.1 p.2.1.2) ())
          (fun p => (prodO A B).E p.2.1 p.2.2))) L := ent_of (fun _ _ h => h)
    have f : Ent L (fun p => F.F p.1.2 p.2.2.1) := ent_and2 (and_l _ _)
      (and_i (ent_trans (and_r _ _) (ent_trans (and_l _ _) (and_l _ _))) (ent_trans (and_r _ _) (ent_trans (and_r _ _) (and_l _ _))))
      (ent_re (fun p : (C.X × C.X) × ((A.X × B.X) × (A.X × B.X)) => ((p.1.1, p.1.2), (p.2.1.1, p.2.2.1))) F.rel)
    have g : Ent L (fun p => G.F p.1.2 p.2.2.2) := ent_and2 (and_l _ _)
      (and_i (ent_trans (and_r _ _) (ent_trans (and_l _ _) (and_r _ _))) (ent_trans (and_r _ _) (ent_trans (and_r _ _) (and_r _ _))))
      (ent_re (fun p : (C.X × C.X) × ((A.X × B.X) × (A.X × B.X)) => ((p.1.1, p.1.2), (p.2.1.2, p.2.2.2))) G.rel)
    exact ent_trans pull (ent_trans (and_i f g) (ent_of (fun _ _ h => h)))
  sv := by
    have h : Ent (andP (fun p : C.X × (A.X × B.X) × (A.X × B.X) => andP (fun _ : Unit => F.F p.1 p.2.1.1) (fun _ => G.F p.1 p.2.1.2) ())
        (fun p => andP (fun _ : Unit => F.F p.1 p.2.2.1) (fun _ => G.F p.1 p.2.2.2) ()))
        (andP (andP (fun p : C.X × (A.X × B.X) × (A.X × B.X) => F.F p.1 p.2.1.1) (fun p => G.F p.1 p.2.1.2))
          (andP (fun p => F.F p.1 p.2.2.1) (fun p => G.F p.1 p.2.2.2))) := ent_of (fun _ _ h => h)
    exact ent_trans h (ent_trans (and_i
      (ent_and2 (ent_trans (and_l _ _) (and_l _ _)) (ent_trans (and_r _ _) (and_l _ _))
        (ent_re (fun p : C.X × (A.X × B.X) × (A.X × B.X) => (p.1, p.2.1.1, p.2.2.1)) F.sv))
      (ent_and2 (ent_trans (and_l _ _) (and_r _ _)) (ent_trans (and_r _ _) (and_r _ _))
        (ent_re (fun p : C.X × (A.X × B.X) × (A.X × B.X) => (p.1, p.2.1.2, p.2.2.2)) G.sv)))
      (Ep A B (fun p : C.X × (A.X × B.X) × (A.X × B.X) => p.2.1) (fun p => p.2.2)))
  tot := ent_trans (and_i F.tot G.tot) (ent_of (fun _ _ ⟨a, b, hc, ⟨x, ha⟩, ⟨y, hb⟩⟩ => ⟨(x, y), a, b, hc, ha, hb⟩))

/-- the first projection equation -/
theorem beta1 : FEq (compF (pairF F G) (pi1 A B)) F := by
  refine ⟨ent_trans (compR_ex _ _) (ex_elim ?_), ?_⟩
  · -- (F(c, u.1) ∧ G(c, u.2)) ∧ (E(u.1, a) ∧ E(u.2, u.2)) ⊢ F(c, a)
    let L : (C.X × A.X) × (A.X × B.X) → Pr P :=
      andP (andP (fun q => F.F q.1.1 q.2.1) (fun q => G.F q.1.1 q.2.2)) (andP (fun q => A.E q.2.1 q.1.2) (fun q => B.E q.2.2 q.2.2))
    have h : Ent (compB (pairF F G) (pi1 A B)) L := ent_of (fun _ _ h => h)
    have fcu : Ent L (fun q => F.F q.1.1 q.2.1) := ent_trans (and_l _ _) (and_l _ _)
    have ecc : Ent L (fun q => C.E q.1.1 q.1.1) := ent_trans fcu (ent_trans (ent_re (fun q : (C.X × A.X) × (A.X × B.X) => (q.1.1, q.2.1)) F.st) (and_l _ _))
    exact ent_trans h (ent_and2 ecc (and_i fcu (ent_trans (and_r _ _) (and_l _ _)))
      (ent_re (fun q : (C.X × A.X) × (A.X × B.X) => ((q.1.1, q.1.1), (q.2.1, q.1.2))) F.rel))
  · -- F(c, a) ⊢ ∃ b. G(c, b), then the witness (a, b)
    have ecc : Ent (fun p : C.X × A.X => F.F p.1 p.2) (fun p => C.E p.1 p.1) := ent_trans F.st (and_l _ _)
    have exb : Ent (fun p : C.X × A.X => F.F p.1 p.2) (exP (fun q : (C.X × A.X) × B.X => andP (fun q => F.F q.1.1 q.1.2) (fun q => G.F q.1.1 q.2) q)) :=
      ent_trans (and_i (ent_refl _) (ent_trans ecc (ent_re (fun p : C.X × A.X => p.1) G.tot)))
        (ent_of (fun _ _ ⟨a, b, hc, ha, ⟨y, hb⟩⟩ => ⟨y, a, b, hc, ha, hb⟩))
    refine ent_trans exb (ent_trans (ex_into (ψ := compB (pairF F G) (pi1 A B)) (fun q : (C.X × A.X) × B.X => (q.1.2, q.2)) ?_) (ex_compR (pairF F G) (pi1 A B)))
    have fa := and_l (fun q : (C.X × A.X) × B.X => F.F q.1.1 q.1.2) (fun q => G.F q.1.1 q.2)
    have gb := and_r (fun q : (C.X × A.X) × B.X => F.F q.1.1 q.1.2) (fun q => G.F q.1.1 q.2)
    have eaa : Ent (andP (fun q : (C.X × A.X) × B.X => F.F q.1.1 q.1.2) (fun q => G.F q.1.1 q.2)) (fun q => A.E q.1.2 q.1.2) :=
      ent_trans fa (ent_trans (ent_re (fun q : (C.X × A.X) × B.X => q.1) F.st) (and_r _ _))
    have ebb : Ent (andP (fun q : (C.X × A.X) × B.X => F.F q.1.1 q.1.2) (fun q => G.F q.1.1 q.2)) (fun q => B.E q.2 q.2) :=
      ent_trans gb (ent_trans (ent_re (fun q : (C.X × A.X) × B.X => (q.1.1, q.2)) G.st) (and_r _ _))
    exact ent_trans (and_i (and_i fa gb) (and_i eaa ebb)) (ent_of (fun _ _ h => h))

/-- the second projection equation -/
theorem beta2 : FEq (compF (pairF F G) (pi2 A B)) G := by
  refine ⟨ent_trans (compR_ex _ _) (ex_elim ?_), ?_⟩
  · let L : (C.X × B.X) × (A.X × B.X) → Pr P :=
      andP (andP (fun q => F.F q.1.1 q.2.1) (fun q => G.F q.1.1 q.2.2)) (andP (fun q => A.E q.2.1 q.2.1) (fun q => B.E q.2.2 q.1.2))
    have h : Ent (compB (pairF F G) (pi2 A B)) L := ent_of (fun _ _ h => h)
    have gcu : Ent L (fun q => G.F q.1.1 q.2.2) := ent_trans (and_l _ _) (and_r _ _)
    have ecc : Ent L (fun q => C.E q.1.1 q.1.1) := ent_trans gcu (ent_trans (ent_re (fun q : (C.X × B.X) × (A.X × B.X) => (q.1.1, q.2.2)) G.st) (and_l _ _))
    exact ent_trans h (ent_and2 ecc (and_i gcu (ent_trans (and_r _ _) (and_r _ _)))
      (ent_re (fun q : (C.X × B.X) × (A.X × B.X) => ((q.1.1, q.1.1), (q.2.2, q.1.2))) G.rel))
  · have ecc : Ent (fun p : C.X × B.X => G.F p.1 p.2) (fun p => C.E p.1 p.1) := ent_trans G.st (and_l _ _)
    have exa : Ent (fun p : C.X × B.X => G.F p.1 p.2) (exP (fun q : (C.X × B.X) × A.X => andP (fun q => F.F q.1.1 q.2) (fun q => G.F q.1.1 q.1.2) q)) :=
      ent_trans (and_i (ent_trans ecc (ent_re (fun p : C.X × B.X => p.1) F.tot)) (ent_refl _))
        (ent_of (fun _ _ ⟨a, b, hc, ⟨x, ha⟩, hb⟩ => ⟨x, a, b, hc, ha, hb⟩))
    refine ent_trans exa (ent_trans (ex_into (ψ := compB (pairF F G) (pi2 A B)) (fun q : (C.X × B.X) × A.X => (q.2, q.1.2)) ?_)
      (ex_compR (pairF F G) (pi2 A B)))
    have fa := and_l (fun q : (C.X × B.X) × A.X => F.F q.1.1 q.2) (fun q => G.F q.1.1 q.1.2)
    have gb := and_r (fun q : (C.X × B.X) × A.X => F.F q.1.1 q.2) (fun q => G.F q.1.1 q.1.2)
    have eaa : Ent (andP (fun q : (C.X × B.X) × A.X => F.F q.1.1 q.2) (fun q => G.F q.1.1 q.1.2)) (fun q => A.E q.2 q.2) :=
      ent_trans fa (ent_trans (ent_re (fun q : (C.X × B.X) × A.X => (q.1.1, q.2)) F.st) (and_r _ _))
    have ebb : Ent (andP (fun q : (C.X × B.X) × A.X => F.F q.1.1 q.2) (fun q => G.F q.1.1 q.1.2)) (fun q => B.E q.1.2 q.1.2) :=
      ent_trans gb (ent_trans (ent_re (fun q : (C.X × B.X) × A.X => q.1) G.st) (and_r _ _))
    exact ent_trans (and_i (and_i fa gb) (and_i eaa ebb)) (ent_of (fun _ _ h => h))

/-- uniqueness of the pairing -/
theorem prod_uniq (K : Fn C (prodO A B)) (h1 : FEq (compF K (pi1 A B)) F) (h2 : FEq (compF K (pi2 A B)) G) :
    FEq K (pairF F G) := by
  have euu : Ent (fun p : C.X × (A.X × B.X) => K.F p.1 p.2) (fun p => (prodO A B).E p.2 p.2) := ent_trans K.st (and_r _ _)
  have k1 : Ent (fun p : C.X × (A.X × B.X) => K.F p.1 p.2) (fun p => compR K (pi1 A B) p.1 p.2.1) :=
    ent_trans (ex_intro (φ := fun q : (C.X × (A.X × B.X)) × (A.X × B.X) => compB K (pi1 A B) ((q.1.1, q.1.2.1), q.2)) (fun p => p.2)
      (and_i (ent_refl _) (ent_trans euu (ent_of (fun _ _ h => h)))))
      (ent_re (fun p : C.X × (A.X × B.X) => (p.1, p.2.1)) (ex_compR K (pi1 A B)))
  have k2 : Ent (fun p : C.X × (A.X × B.X) => K.F p.1 p.2) (fun p => compR K (pi2 A B) p.1 p.2.2) :=
    ent_trans (ex_intro (φ := fun q : (C.X × (A.X × B.X)) × (A.X × B.X) => compB K (pi2 A B) ((q.1.1, q.1.2.2), q.2)) (fun p => p.2)
      (and_i (ent_refl _) (ent_trans euu (ent_of (fun _ _ h => h)))))
      (ent_re (fun p : C.X × (A.X × B.X) => (p.1, p.2.2)) (ex_compR K (pi2 A B)))
  refine ⟨ent_trans (and_i (ent_trans k1 (ent_re (fun p : C.X × (A.X × B.X) => (p.1, p.2.1)) h1.1))
      (ent_trans k2 (ent_re (fun p : C.X × (A.X × B.X) => (p.1, p.2.2)) h2.1))) (ent_of (fun _ _ h => h)), ?_⟩
  -- F(c, w.1) ∧ G(c, w.2) ⊢ K(c, w)
  let L : (C.X × (A.X × B.X)) × ((A.X × B.X) × (A.X × B.X)) → Pr P :=
    andP (andP (fun q => K.F q.1.1 q.2.1) (andP (fun q => A.E q.2.1.1 q.1.2.1) (fun q => B.E q.2.1.2 q.2.1.2)))
      (andP (fun q => K.F q.1.1 q.2.2) (andP (fun q => A.E q.2.2.1 q.2.2.1) (fun q => B.E q.2.2.2 q.1.2.2)))
  have pull : Ent (andP (fun p : C.X × (A.X × B.X) => compR K (pi1 A B) p.1 p.2.1) (fun p => compR K (pi2 A B) p.1 p.2.2)) (exP L) :=
    ent_of (fun _ _ ⟨a, b, hc, ⟨u, ha⟩, ⟨u', hb⟩⟩ => ⟨(u, u'), a, b, hc, ha, hb⟩)
  have into : Ent (fun p : C.X × (A.X × B.X) => (pairF F G).F p.1 p.2)
      (andP (fun p : C.X × (A.X × B.X) => compR K (pi1 A B) p.1 p.2.1) (fun p => compR K (pi2 A B) p.1 p.2.2)) :=
    ent_trans (ent_of (fun _ _ h => h) : Ent (fun p : C.X × (A.X × B.X) => (pairF F G).F p.1 p.2)
      (andP (fun p : C.X × (A.X × B.X) => F.F p.1 p.2.1) (fun p => G.F p.1 p.2.2)))
      (and_mono (ent_re (fun p : C.X × (A.X × B.X) => (p.1, p.2.1)) h1.2) (ent_re (fun p : C.X × (A.X × B.X) => (p.1, p.2.2)) h2.2))
  have kcu : Ent L (fun q => K.F q.1.1 q.2.1) := ent_trans (and_l _ _) (and_l _ _)
  have kcu' : Ent L (fun q => K.F q.1.1 q.2.2) := ent_trans (and_r _ _) (and_l _ _)
  have euu' : Ent L (fun q => (prodO A B).E q.2.1 q.2.2) := ent_and2 kcu kcu'
    (ent_re (fun q : (C.X × (A.X × B.X)) × ((A.X × B.X) × (A.X × B.X)) => (q.1.1, q.2.1, q.2.2)) K.sv)
  have e2 : Ent L (fun q => B.E q.2.1.2 q.1.2.2) := ent_and2
    (ent_trans euu' (ent_trans (pE A B (fun q : (C.X × (A.X × B.X)) × ((A.X × B.X) × (A.X × B.X)) => q.2.1) (fun q => q.2.2)) (and_r _ _)))
    (ent_trans (and_r _ _) (ent_trans (and_r _ _) (and_r _ _)))
    (tr' B (fun q : (C.X × (A.X × B.X)) × ((A.X × B.X) × (A.X × B.X)) => q.2.1.2) (fun q => q.2.2.2) (fun q => q.1.2.2))
  have e1 : Ent L (fun q => A.E q.2.1.1 q.1.2.1) := ent_trans (and_l _ _) (ent_trans (and_r _ _) (and_l _ _))
  have euw : Ent L (fun q => (prodO A B).E q.2.1 q.1.2) :=
    ent_trans (and_i e1 e2) (Ep A B (fun q : (C.X × (A.X × B.X)) × ((A.X × B.X) × (A.X × B.X)) => q.2.1) (fun q => q.1.2))
  have ecc : Ent L (fun q => C.E q.1.1 q.1.1) :=
    ent_trans kcu (ent_trans (ent_re (fun q : (C.X × (A.X × B.X)) × ((A.X × B.X) × (A.X × B.X)) => (q.1.1, q.2.1)) K.st) (and_l _ _))
  exact ent_trans into (ent_trans pull (ex_elim (ent_and2 ecc (and_i kcu euw)
    (ent_re (fun q : (C.X × (A.X × B.X)) × ((A.X × B.X) × (A.X × B.X)) => ((q.1.1, q.1.1), (q.2.1, q.1.2))) K.rel))))

end Pa

end Eff

end Found

#print axioms Found.Eff.pairF
#print axioms Found.Eff.beta1
#print axioms Found.Eff.beta2
#print axioms Found.Eff.prod_uniq

found/Eff7.lean

lean_all/found/Eff7.lean

/-
  T96b, the effective topos, part 7. found/Eff7 — finite limits, part 3:
  the equalizer of two arrows F, G : A → B, the subobject of A where
  E(x, x') holds together with some y with F(x, y) and G(x, y); its
  inclusion equalizes F and G.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Eff6

namespace Found

namespace Eff

variable {P : PCA}

section Q

variable {A B : Obj P} (F G : Fn A B)

/-- the agreement of F and G at x -/
def agree (x : A.X) : Pr P := exP (fun q : Unit × B.X => andP (fun q : Unit × B.X => F.F x q.2) (fun q => G.F x q.2) q) ()

/-- a relation transports along the equality of its first argument -/
theorem transport (H : Fn A B) {Γ : Type} (f g : Γ → A.X) (y : Γ → B.X) :
    Ent (andP (fun γ => A.E (f γ) (g γ)) (fun γ => H.F (f γ) (y γ))) (fun γ => H.F (g γ) (y γ)) :=
  ent_and2 (and_l _ _) (and_i (and_r _ _) (ent_trans (and_r _ _) (ent_trans (ent_re (fun γ => (f γ, y γ)) H.st) (and_r _ _))))
    (ent_re (fun γ => ((f γ, g γ), (y γ, y γ))) H.rel)

/-- agreement transports along equality -/
theorem agree_tr {Γ : Type} (f g : Γ → A.X) :
    Ent (andP (fun γ => A.E (f γ) (g γ)) (fun γ => agree F G (f γ))) (fun γ => agree F G (g γ)) := by
  let L : Γ × B.X → Pr P := andP (fun q => A.E (f q.1) (g q.1)) (andP (fun q => F.F (f q.1) q.2) (fun q => G.F (f q.1) q.2))
  have pull : Ent (andP (fun γ => A.E (f γ) (g γ)) (fun γ => agree F G (f γ))) (exP L) :=
    ent_of (fun _ _ ⟨a, b, hc, ha, ⟨y, hb⟩⟩ => ⟨y, a, b, hc, ha, hb⟩)
  have fg : Ent L (fun q => F.F (g q.1) q.2) :=
    ent_trans (and_i (and_l _ _) (ent_trans (and_r _ _) (and_l _ _))) (transport F (fun q : Γ × B.X => f q.1) (fun q => g q.1) (fun q => q.2))
  have gg : Ent L (fun q => G.F (g q.1) q.2) :=
    ent_trans (and_i (and_l _ _) (ent_trans (and_r _ _) (and_r _ _))) (transport G (fun q : Γ × B.X => f q.1) (fun q => g q.1) (fun q => q.2))
  exact ent_trans pull (ent_trans (ex_mono (Γ := Γ) (and_i fg gg)) (ent_of (fun _ _ ⟨y, h⟩ => ⟨y, h⟩)))

/-- the equalizer object -/
def eqO : Obj P where
  X := A.X
  E := fun x x' => andP (fun _ : Unit => A.E x x') (fun _ => agree F G x) ()
  sym := by
    have h1 : Ent (fun p : A.X × A.X => andP (fun _ : Unit => A.E p.1 p.2) (fun _ => agree F G p.1) ())
        (andP (fun p : A.X × A.X => A.E p.1 p.2) (fun p => agree F G p.1)) := ent_of (fun _ _ h => h)
    exact ent_trans h1 (ent_trans (and_i (ent_trans (and_l _ _) (sym' A (fun p : A.X × A.X => p.1) (fun p => p.2)))
      (agree_tr F G (fun p : A.X × A.X => p.1) (fun p => p.2))) (ent_of (fun _ _ h => h)))
  tr := by
    have h1 : Ent (andP (fun p : A.X × A.X × A.X => andP (fun _ : Unit => A.E p.1 p.2.1) (fun _ => agree F G p.1) ())
        (fun p => andP (fun _ : Unit => A.E p.2.1 p.2.2) (fun _ => agree F G p.2.1) ()))
        (andP (andP (fun p : A.X × A.X × A.X => A.E p.1 p.2.1) (fun p => agree F G p.1))
          (andP (fun p => A.E p.2.1 p.2.2) (fun p => agree F G p.2.1))) := ent_of (fun _ _ h => h)
    exact ent_trans h1 (ent_trans (and_i (ent_and2 (ent_trans (and_l _ _) (and_l _ _)) (ent_trans (and_r _ _) (and_l _ _))
        (tr' A (fun p : A.X × A.X × A.X => p.1) (fun p => p.2.1) (fun p => p.2.2)))
      (ent_trans (and_l _ _) (and_r _ _))) (ent_of (fun _ _ h => h)))

theorem eqE (Γ : Type) (f g : Γ → A.X) :
    Ent (fun γ => (eqO F G).E (f γ) (g γ)) (andP (fun γ => A.E (f γ) (g γ)) (fun γ => agree F G (f γ))) := ent_of (fun _ _ h => h)

theorem Eeq (Γ : Type) (f g : Γ → A.X) :
    Ent (andP (fun γ => A.E (f γ) (g γ)) (fun γ => agree F G (f γ))) (fun γ => (eqO F G).E (f γ) (g γ)) := ent_of (fun _ _ h => h)

/-- the inclusion of the equalizer -/
def incl : Fn (eqO F G) A where
  F := (eqO F G).E
  st := ent_trans (eqE F G _ (fun p : A.X × A.X => p.1) (fun p => p.2)) (and_i
      (ent_trans (and_i (ent_trans (and_l _ _) (refl_l A (fun p : A.X × A.X => p.1) (fun p => p.2))) (and_r _ _))
        (Eeq F G _ (fun p : A.X × A.X => p.1) (fun p => p.1)))
      (ent_trans (and_l _ _) (refl_r A (fun p : A.X × A.X => p.1) (fun p => p.2))))
  rel := by
    let L : (A.X × A.X) × (A.X × A.X) → Pr P :=
      andP (andP (fun p => A.E p.1.1 p.1.2) (fun p => agree F G p.1.1))
        (andP (andP (fun p => A.E p.1.1 p.2.1) (fun p => agree F G p.1.1)) (fun p => A.E p.2.1 p.2.2))
    have pull : Ent (andP (fun p : (A.X × A.X) × (A.X × A.X) => (eqO F G).E p.1.1 p.1.2)
        (andP (fun p => (eqO F G).E p.1.1 p.2.1) (fun p => A.E p.2.1 p.2.2))) L := ent_of (fun _ _ h => h)
    have e01 : Ent L (fun p => A.E p.1.1 p.1.2) := ent_trans (and_l _ _) (and_l _ _)
    have e10 : Ent L (fun p => A.E p.1.2 p.1.1) := ent_trans e01 (sym' A (fun p : (A.X × A.X) × (A.X × A.X) => p.1.1) (fun p => p.1.2))
    have e0a : Ent L (fun p => A.E p.1.1 p.2.2) := ent_and2 (ent_trans (and_r _ _) (ent_trans (and_l _ _) (and_l _ _)))
      (ent_trans (and_r _ _) (and_r _ _)) (tr' A (fun p : (A.X × A.X) × (A.X × A.X) => p.1.1) (fun p => p.2.1) (fun p => p.2.2))
    have e1a : Ent L (fun p => A.E p.1.2 p.2.2) := ent_and2 e10 e0a
      (tr' A (fun p : (A.X × A.X) × (A.X × A.X) => p.1.2) (fun p => p.1.1) (fun p => p.2.2))
    have ag1 : Ent L (fun p => agree F G p.1.2) := ent_trans (and_l _ _) (agree_tr F G (fun p : (A.X × A.X) × (A.X × A.X) => p.1.1) (fun p => p.1.2))
    exact ent_trans pull (ent_trans (and_i e1a ag1) (Eeq F G _ (fun p : (A.X × A.X) × (A.X × A.X) => p.1.2) (fun p => p.2.2)))
  sv := by
    have h : Ent (andP (fun p : A.X × A.X × A.X => (eqO F G).E p.1 p.2.1) (fun p => (eqO F G).E p.1 p.2.2))
        (andP (fun p : A.X × A.X × A.X => A.E p.1 p.2.1) (fun p => A.E p.1 p.2.2)) :=
      and_mono (ent_trans (eqE F G _ (fun p : A.X × A.X × A.X => p.1) (fun p => p.2.1)) (and_l _ _))
        (ent_trans (eqE F G _ (fun p : A.X × A.X × A.X => p.1) (fun p => p.2.2)) (and_l _ _))
    exact ent_trans h (ent_and2 (ent_trans (and_l _ _) (sym' A (fun p : A.X × A.X × A.X => p.1) (fun p => p.2.1))) (and_r _ _)
      (tr' A (fun p : A.X × A.X × A.X => p.2.1) (fun p => p.1) (fun p => p.2.2)))
  tot := ex_intro (fun x => x) (ent_refl _)

/-- from F(a, y), E(x, a) and the agreement at x, G(a, y) -/
theorem swapFG (F G : Fn A B) {Γ : Type} (x a : Γ → A.X) (y : Γ → B.X) :
    Ent (andP (andP (fun γ => A.E (x γ) (a γ)) (fun γ => agree F G (x γ))) (fun γ => F.F (a γ) (y γ))) (fun γ => G.F (a γ) (y γ)) := by
  let L : Γ × B.X → Pr P :=
    andP (andP (fun q => A.E (x q.1) (a q.1)) (andP (fun q => F.F (x q.1) q.2) (fun q => G.F (x q.1) q.2))) (fun q => F.F (a q.1) (y q.1))
  have pull : Ent (andP (andP (fun γ => A.E (x γ) (a γ)) (fun γ => agree F G (x γ))) (fun γ => F.F (a γ) (y γ))) (exP L) :=
    ent_of (fun _ _ ⟨c1, c2, hc, ⟨d1, d2, hd, h1, ⟨y', h2⟩⟩, h3⟩ => ⟨y', c1, c2, hc, ⟨d1, d2, hd, h1, h2⟩, h3⟩)
  have exa : Ent L (fun q => A.E (x q.1) (a q.1)) := ent_trans (and_l _ _) (and_l _ _)
  have eax : Ent L (fun q => A.E (a q.1) (x q.1)) := ent_trans exa (sym' A (fun q : Γ × B.X => x q.1) (fun q => a q.1))
  have fxy : Ent L (fun q => F.F (x q.1) (y q.1)) := ent_trans (and_i eax (and_r _ _)) (transport F (fun q : Γ × B.X => a q.1) (fun q => x q.1) (fun q => y q.1))
  have fxy' : Ent L (fun q => F.F (x q.1) q.2) := ent_trans (and_l _ _) (ent_trans (and_r _ _) (and_l _ _))
  have gxy' : Ent L (fun q => G.F (x q.1) q.2) := ent_trans (and_l _ _) (ent_trans (and_r _ _) (and_r _ _))
  have eyy : Ent L (fun q => B.E q.2 (y q.1)) := ent_and2 fxy' fxy (ent_re (fun q : Γ × B.X => (x q.1, q.2, y q.1)) F.sv)
  have exx : Ent L (fun q => A.E (x q.1) (x q.1)) := ent_trans exa (refl_l A (fun q : Γ × B.X => x q.1) (fun q => a q.1))
  have gxy : Ent L (fun q => G.F (x q.1) (y q.1)) := ent_and2 exx (and_i gxy' eyy)
    (ent_re (fun q : Γ × B.X => ((x q.1, x q.1), (q.2, y q.1))) G.rel)
  exact ent_trans pull (ex_elim (ent_trans (and_i exa gxy) (transport G (fun q : Γ × B.X => x q.1) (fun q => a q.1) (fun q => y q.1))))

theorem agree_symm {Γ : Type} (x : Γ → A.X) : Ent (fun γ => agree F G (x γ)) (fun γ => agree G F (x γ)) :=
  ent_trans (ent_of (fun _ _ h => h) : Ent (fun γ => agree F G (x γ))
      (exP (fun q : Γ × B.X => andP (fun q : Γ × B.X => F.F (x q.1) q.2) (fun q => G.F (x q.1) q.2) q)))
    (ent_trans (ex_mono (and_comm' _ _)) (ent_of (fun _ _ h => h)))

/-- the inclusion equalizes F and G -/
theorem incl_eq : FEq (compF (incl F G) F) (compF (incl F G) G) := by
  have hF : Ent (compB (incl F G) F) (andP (andP (fun q : (A.X × B.X) × A.X => A.E q.1.1 q.2) (fun q => agree F G q.1.1))
      (fun q => F.F q.2 q.1.2)) := ent_of (fun _ _ h => h)
  have hG : Ent (compB (incl F G) G) (andP (andP (fun q : (A.X × B.X) × A.X => A.E q.1.1 q.2) (fun q => agree F G q.1.1))
      (fun q => G.F q.2 q.1.2)) := ent_of (fun _ _ h => h)
  have d1 : Ent (compB (incl F G) F) (compB (incl F G) G) :=
    ent_trans hF (ent_trans (and_i (and_l _ _)
      (swapFG F G (fun q : (A.X × B.X) × A.X => q.1.1) (fun q => q.2) (fun q => q.1.2))) (ent_of (fun _ _ h => h)))
  have d2 : Ent (compB (incl F G) G) (compB (incl F G) F) :=
    ent_trans hG (ent_trans (and_i (and_l _ _)
      (ent_trans (and_mono (and_mono (ent_refl _) (agree_symm F G (fun q : (A.X × B.X) × A.X => q.1.1))) (ent_refl _))
        (swapFG G F (fun q : (A.X × B.X) × A.X => q.1.1) (fun q => q.2) (fun q => q.1.2)))) (ent_of (fun _ _ h => h)))
  exact ⟨ent_trans (compR_ex _ _) (ent_trans (ex_mono d1) (ex_compR (incl F G) G)),
    ent_trans (compR_ex _ _) (ent_trans (ex_mono d2) (ex_compR (incl F G) F))⟩

end Q

end Eff

end Found

#print axioms Found.Eff.transport
#print axioms Found.Eff.agree_tr
#print axioms Found.Eff.eqO
#print axioms Found.Eff.incl
#print axioms Found.Eff.swapFG
#print axioms Found.Eff.incl_eq

found/Eff8.lean

lean_all/found/Eff8.lean

/-
  T96b, the effective topos, part 8. found/Eff8 — finite limits, part 4:
  the universal property of the equalizer. An arrow H : C → A with
  H ; F = H ; G lands in the agreement of F and G, so it lifts through the
  inclusion, and the lift is unique. With Eff5–Eff6: the realizability
  category over any PCA has all finite limits.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Eff7

namespace Found

namespace Eff

variable {P : PCA}

section L

variable {A B C : Obj P} (F G : Fn A B) (H : Fn C A) (heq : FEq (compF H F) (compF H G))
include heq

/-- an arrow equalizing F and G lands where they agree -/
theorem agreeH : Ent (fun p : C.X × A.X => H.F p.1 p.2) (fun p => agree F G p.2) := by
  -- H(c, x) ⊢ ∃ y. H(c, x) ∧ F(x, y)
  have s1 : Ent (fun p : C.X × A.X => H.F p.1 p.2) (exP (andP (fun q : (C.X × A.X) × B.X => H.F q.1.1 q.1.2) (fun q => F.F q.1.2 q.2))) :=
    ent_trans (and_i (ent_refl _) (ent_trans (ent_trans H.st (and_r _ _)) (ent_re (fun p : C.X × A.X => p.2) F.tot)))
      (ent_of (fun _ _ ⟨a, b, hc, ha, ⟨y, hb⟩⟩ => ⟨y, a, b, hc, ha, hb⟩))
  -- H(c, x) ∧ F(x, y) ⊢ ∃ x'. H(c, x') ∧ G(x', y)
  have s2 : Ent (andP (fun q : (C.X × A.X) × B.X => H.F q.1.1 q.1.2) (fun q => F.F q.1.2 q.2)) (fun q => compR H G q.1.1 q.2) :=
    ent_trans (ex_intro (φ := fun r : ((C.X × A.X) × B.X) × A.X => compB H F ((r.1.1.1, r.1.2), r.2)) (fun q => q.1.2) (ent_of (fun _ _ h => h)))
      (ent_trans (ent_re (fun q : (C.X × A.X) × B.X => (q.1.1, q.2)) (ex_compR H F))
        (ent_re (fun q : (C.X × A.X) × B.X => (q.1.1, q.2)) heq.1))
  let L : ((C.X × A.X) × B.X) × A.X → Pr P :=
    andP (andP (fun r => H.F r.1.1.1 r.1.1.2) (fun r => F.F r.1.1.2 r.1.2)) (andP (fun r => H.F r.1.1.1 r.2) (fun r => G.F r.2 r.1.2))
  have pull : Ent (andP (andP (fun q : (C.X × A.X) × B.X => H.F q.1.1 q.1.2) (fun q => F.F q.1.2 q.2)) (fun q => compR H G q.1.1 q.2)) (exP L) :=
    ent_of (fun _ _ ⟨a, b, hc, ha, ⟨x', hb⟩⟩ => ⟨x', a, b, hc, ha, hb⟩)
  have exx : Ent L (fun r => A.E r.2 r.1.1.2) := ent_and2 (ent_trans (and_r _ _) (and_l _ _)) (ent_trans (and_l _ _) (and_l _ _))
    (ent_re (fun r : ((C.X × A.X) × B.X) × A.X => (r.1.1.1, r.2, r.1.1.2)) H.sv)
  have gxy : Ent L (fun r => G.F r.1.1.2 r.1.2) := ent_trans (and_i exx (ent_trans (and_r _ _) (and_r _ _)))
    (transport G (fun r : ((C.X × A.X) × B.X) × A.X => r.2) (fun r => r.1.1.2) (fun r => r.1.2))
  have fg : Ent (andP (fun q : (C.X × A.X) × B.X => H.F q.1.1 q.1.2) (fun q => F.F q.1.2 q.2))
      (andP (fun q : (C.X × A.X) × B.X => F.F q.1.2 q.2) (fun q => G.F q.1.2 q.2)) :=
    ent_trans (and_i (ent_refl _) s2) (ent_trans pull (ex_elim (and_i (ent_trans (and_l _ _) (and_r _ _)) gxy)))
  exact ent_trans s1 (ent_trans (ex_mono fg) (ent_of (fun _ _ ⟨y, h⟩ => ⟨y, h⟩)))

/-- the lift through the equalizer -/
def liftK : Fn C (eqO F G) where
  F := fun c x => andP (fun _ : Unit => H.F c x) (fun _ => agree F G x) ()
  st := by
    have h : Ent (fun p : C.X × A.X => andP (fun _ : Unit => H.F p.1 p.2) (fun _ => agree F G p.2) ())
        (andP (fun p : C.X × A.X => H.F p.1 p.2) (fun p => agree F G p.2)) := ent_of (fun _ _ h => h)
    exact ent_trans h (and_i (ent_trans (and_l _ _) (ent_trans H.st (and_l _ _)))
      (ent_trans (and_mono (ent_trans H.st (and_r _ _)) (ent_refl _)) (Eeq F G _ (fun p : C.X × A.X => p.2) (fun p => p.2))))
  rel := by
    let L : (C.X × C.X) × (A.X × A.X) → Pr P :=
      andP (fun p => C.E p.1.1 p.1.2) (andP (andP (fun p => H.F p.1.1 p.2.1) (fun p => agree F G p.2.1))
        (andP (fun p => A.E p.2.1 p.2.2) (fun p => agree F G p.2.1)))
    have pull : Ent (andP (fun p : (C.X × C.X) × (A.X × A.X) => C.E p.1.1 p.1.2)
        (andP (fun p => andP (fun _ : Unit => H.F p.1.1 p.2.1) (fun _ => agree F G p.2.1) ()) (fun p => (eqO F G).E p.2.1 p.2.2))) L :=
      ent_of (fun _ _ h => h)
    have h' : Ent L (fun p => H.F p.1.2 p.2.2) := ent_and2 (and_l _ _)
      (and_i (ent_trans (and_r _ _) (ent_trans (and_l _ _) (and_l _ _))) (ent_trans (and_r _ _) (ent_trans (and_r _ _) (and_l _ _))))
      (ent_re (fun p : (C.X × C.X) × (A.X × A.X) => ((p.1.1, p.1.2), (p.2.1, p.2.2))) H.rel)
    have ag : Ent L (fun p => agree F G p.2.2) := ent_trans (and_i (ent_trans (and_r _ _) (ent_trans (and_r _ _) (and_l _ _)))
      (ent_trans (and_r _ _) (ent_trans (and_l _ _) (and_r _ _)))) (agree_tr F G (fun p : (C.X × C.X) × (A.X × A.X) => p.2.1) (fun p => p.2.2))
    exact ent_trans pull (ent_trans (and_i h' ag) (ent_of (fun _ _ h => h)))
  sv := by
    have h : Ent (andP (fun p : C.X × A.X × A.X => andP (fun _ : Unit => H.F p.1 p.2.1) (fun _ => agree F G p.2.1) ())
        (fun p => andP (fun _ : Unit => H.F p.1 p.2.2) (fun _ => agree F G p.2.2) ()))
        (andP (andP (fun p : C.X × A.X × A.X => H.F p.1 p.2.1) (fun p => agree F G p.2.1))
          (andP (fun p => H.F p.1 p.2.2) (fun p => agree F G p.2.2))) := ent_of (fun _ _ h => h)
    exact ent_trans h (ent_trans (and_i (ent_and2 (ent_trans (and_l _ _) (and_l _ _)) (ent_trans (and_r _ _) (and_l _ _)) H.sv)
      (ent_trans (and_l _ _) (and_r _ _))) (Eeq F G _ (fun p : C.X × A.X × A.X => p.2.1) (fun p => p.2.2)))
  tot := ent_trans H.tot (ent_trans (ex_mono (and_i (ent_refl _) (agreeH F G H heq))) (ent_of (fun _ _ ⟨x, h⟩ => ⟨x, h⟩)))

/-- the lift commutes with the inclusion -/
theorem lift_comm : FEq (compF (liftK F G H heq) (incl F G)) H := by
  refine ⟨ent_trans (compR_ex _ _) (ex_elim ?_), ?_⟩
  · let L : (C.X × A.X) × A.X → Pr P :=
      andP (andP (fun q => H.F q.1.1 q.2) (fun q => agree F G q.2)) (andP (fun q => A.E q.2 q.1.2) (fun q => agree F G q.2))
    have h : Ent (compB (liftK F G H heq) (incl F G)) L := ent_of (fun _ _ h => h)
    have hcx : Ent L (fun q => H.F q.1.1 q.2) := ent_trans (and_l _ _) (and_l _ _)
    have ecc : Ent L (fun q => C.E q.1.1 q.1.1) := ent_trans hcx (ent_trans (ent_re (fun q : (C.X × A.X) × A.X => (q.1.1, q.2)) H.st) (and_l _ _))
    exact ent_trans h (ent_and2 ecc (and_i hcx (ent_trans (and_r _ _) (and_l _ _)))
      (ent_re (fun q : (C.X × A.X) × A.X => ((q.1.1, q.1.1), (q.2, q.1.2))) H.rel))
  · have ag := agreeH F G H heq
    have eaa : Ent (fun p : C.X × A.X => H.F p.1 p.2) (fun p => A.E p.2 p.2) := ent_trans H.st (and_r _ _)
    refine ent_trans (ex_intro (φ := compB (liftK F G H heq) (incl F G)) (fun p => p.2) ?_) (ex_compR (liftK F G H heq) (incl F G))
    exact ent_trans (and_i (and_i (ent_refl _) ag) (and_i eaa ag)) (ent_of (fun _ _ h => h))

/-- the lift is unique -/
theorem lift_uniq (K : Fn C (eqO F G)) (hK : FEq (compF K (incl F G)) H) : FEq K (liftK F G H heq) := by
  have exx : Ent (fun p : C.X × A.X => K.F p.1 p.2) (fun p => (eqO F G).E p.2 p.2) := ent_trans K.st (and_r _ _)
  have k1 : Ent (fun p : C.X × A.X => K.F p.1 p.2) (fun p => compR K (incl F G) p.1 p.2) :=
    ent_trans (ex_intro (φ := compB K (incl F G)) (fun p => p.2) (and_i (ent_refl _) exx)) (ex_compR K (incl F G))
  refine ⟨ent_trans (and_i (ent_trans k1 hK.1) (ent_trans exx (ent_trans (eqE F G _ (fun p : C.X × A.X => p.2) (fun p => p.2)) (and_r _ _))))
    (ent_of (fun _ _ h => h)), ?_⟩
  have into : Ent (fun p : C.X × A.X => (liftK F G H heq).F p.1 p.2) (fun p => compR K (incl F G) p.1 p.2) :=
    ent_trans (ent_trans (ent_of (fun _ _ h => h) : Ent (fun p : C.X × A.X => (liftK F G H heq).F p.1 p.2)
      (andP (fun p : C.X × A.X => H.F p.1 p.2) (fun p => agree F G p.2))) (and_l _ _)) hK.2
  have ecc : Ent (compB K (incl F G)) (fun q => C.E q.1.1 q.1.1) :=
    ent_trans (and_l _ _) (ent_trans (ent_re (fun q : (C.X × A.X) × A.X => (q.1.1, q.2)) K.st) (and_l _ _))
  exact ent_trans into (ent_trans (compR_ex _ _) (ex_elim (ent_and2 ecc (and_i (and_l _ _) (and_r _ _))
    (ent_re (fun q : (C.X × A.X) × A.X => ((q.1.1, q.1.1), (q.2, q.1.2))) K.rel))))

end L

end Eff

end Found

#print axioms Found.Eff.agreeH
#print axioms Found.Eff.liftK
#print axioms Found.Eff.lift_comm
#print axioms Found.Eff.lift_uniq

found/Eff9.lean

lean_all/found/Eff9.lean

/-
  T96b, the effective topos, part 9. found/Eff9 — the power object PA:
  predicates φ on A that are strict (φ(x) ⊢ E(x, x)) and extensional
  (φ(x) ∧ E(x, x') ⊢ φ(x')), with equality "both good and equivalent";
  membership x ∈ φ. Derived rules for implication and the universal
  quantifier.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Eff8

namespace Found

namespace Eff

variable {P : PCA}

/-! ## derived rules -/

theorem imp_comp {Γ : Type} (φ ψ χ : Γ → Pr P) : Ent (andP (impP φ ψ) (impP ψ χ)) (impP φ χ) :=
  curry (ent_trans (and_i (ent_trans (and_l _ _) (and_r _ _))
    (ent_trans (and_i (ent_trans (and_l _ _) (and_l _ _)) (and_r _ _)) (eval_ent φ ψ))) (eval_ent ψ χ))

theorem imp_id {Γ : Type} (χ φ : Γ → Pr P) : Ent χ (impP φ φ) := curry (and_r _ _)

/-- the instance at the context variable -/
theorem all_at {Γ Y : Type} (φ : Γ × Y → Pr P) : Ent (fun p : Γ × Y => allP φ p.1) φ :=
  ent_trans (ent_of (fun _ _ h => h) : Ent (fun p : Γ × Y => allP φ p.1) (allP (fun q : (Γ × Y) × Y => φ (q.1.1, q.2))))
    (all_elim (fun q : (Γ × Y) × Y => φ (q.1.1, q.2)) (fun p => p.2))

theorem all_mono {Γ Y : Type} {φ ψ : Γ × Y → Pr P} (h : Ent φ ψ) : Ent (allP φ) (allP ψ) :=
  all_intro (ent_trans (all_at φ) h)

theorem all_and {Γ Y : Type} (φ ψ : Γ × Y → Pr P) : Ent (andP (allP φ) (allP ψ)) (allP (andP φ ψ)) :=
  all_intro (and_i (ent_trans (ent_re (fun p : Γ × Y => p.1) (and_l _ _)) (all_at φ))
    (ent_trans (ent_re (fun p : Γ × Y => p.1) (and_r _ _)) (all_at ψ)))

/-! ## good predicates -/

section G

variable (A : Obj P)

def StrF {Γ : Type} (φ : Γ → A.X → Pr P) : Γ → Pr P :=
  allP (impP (fun q : Γ × A.X => φ q.1 q.2) (fun q => A.E q.2 q.2))

def ExtF {Γ : Type} (φ : Γ → A.X → Pr P) : Γ → Pr P :=
  allP (impP (andP (fun q : Γ × (A.X × A.X) => φ q.1 q.2.1) (fun q => A.E q.2.1 q.2.2)) (fun q => φ q.1 q.2.2))

def IffF {Γ : Type} (φ ψ : Γ → A.X → Pr P) : Γ → Pr P :=
  allP (andP (impP (fun q : Γ × A.X => φ q.1 q.2) (fun q => ψ q.1 q.2)) (impP (fun q => ψ q.1 q.2) (fun q => φ q.1 q.2)))

def GoodF {Γ : Type} (φ : Γ → A.X → Pr P) : Γ → Pr P := andP (StrF A φ) (ExtF A φ)

def EPF {Γ : Type} (φ ψ : Γ → A.X → Pr P) : Γ → Pr P := andP (GoodF A φ) (andP (GoodF A ψ) (IffF A φ ψ))

theorem iff_refl {Γ : Type} (χ : Γ → Pr P) (φ : Γ → A.X → Pr P) : Ent χ (IffF A φ φ) :=
  all_intro (and_i (imp_id _ _) (imp_id _ _))

theorem iff_symm {Γ : Type} (φ ψ : Γ → A.X → Pr P) : Ent (IffF A φ ψ) (IffF A ψ φ) := all_mono (and_comm' _ _)

theorem iff_trans {Γ : Type} (φ ψ χ : Γ → A.X → Pr P) : Ent (andP (IffF A φ ψ) (IffF A ψ χ)) (IffF A φ χ) :=
  ent_trans (all_and _ _) (all_mono (and_i
    (ent_trans (and_i (ent_trans (and_l _ _) (and_l _ _)) (ent_trans (and_r _ _) (and_l _ _))) (imp_comp _ _ _))
    (ent_trans (and_i (ent_trans (and_r _ _) (and_r _ _)) (ent_trans (and_l _ _) (and_r _ _))) (imp_comp _ _ _))))

/-- the power object -/
def powO : Obj P where
  X := A.X → Pr P
  E := fun φ ψ => EPF A (fun _ : Unit => φ) (fun _ => ψ) ()
  sym := ent_trans (ent_of (fun _ _ h => h) : Ent (fun p : (A.X → Pr P) × (A.X → Pr P) => EPF A (fun _ : Unit => p.1) (fun _ => p.2) ())
      (EPF A (fun p : (A.X → Pr P) × (A.X → Pr P) => p.1) (fun p => p.2)))
    (ent_trans (and_i (ent_trans (and_r _ _) (and_l _ _)) (and_i (and_l _ _) (ent_trans (and_r _ _) (ent_trans (and_r _ _) (iff_symm A _ _)))))
      (ent_of (fun _ _ h => h)))
  tr := ent_trans (ent_of (fun _ _ h => h) : Ent (andP (fun p : (A.X → Pr P) × (A.X → Pr P) × (A.X → Pr P) =>
        EPF A (fun _ : Unit => p.1) (fun _ => p.2.1) ()) (fun p => EPF A (fun _ : Unit => p.2.1) (fun _ => p.2.2) ()))
      (andP (EPF A (fun p : (A.X → Pr P) × (A.X → Pr P) × (A.X → Pr P) => p.1) (fun p => p.2.1)) (EPF A (fun p => p.2.1) (fun p => p.2.2))))
    (ent_trans (and_i (ent_trans (and_l _ _) (and_l _ _)) (and_i (ent_trans (and_r _ _) (ent_trans (and_r _ _) (and_l _ _)))
      (ent_trans (and_i (ent_trans (and_l _ _) (ent_trans (and_r _ _) (and_r _ _))) (ent_trans (and_r _ _) (ent_trans (and_r _ _) (and_r _ _))))
        (iff_trans A _ _ _)))) (ent_of (fun _ _ h => h)))

/-- membership x ∈ φ -/
def memF {Γ : Type} (x : Γ → A.X) (φ : Γ → A.X → Pr P) : Γ → Pr P :=
  andP (fun γ => A.E (x γ) (x γ)) (andP (GoodF A φ) (fun γ => φ γ (x γ)))

end G

end Eff

end Found

#print axioms Found.Eff.imp_comp
#print axioms Found.Eff.all_mono
#print axioms Found.Eff.iff_trans
#print axioms Found.Eff.powO

found/Effective.lean

lean_all/found/Effective.lean

/-
  T96. found/Effective — realizability (the logic of the effective topos).

  * Combinatory algebras (A, ·, K, S) with K x y = x, S x y z = x z (y z).
  * Bracket abstraction on expressions with de Bruijn variables:
    (λ e) · x = e[x/0]  (combinatory completeness, by induction).
  * Realizability predicates A → Prop with  φ ⊢ ψ :⟺ ∃ e, ∀ x ∈ φ, e·x ∈ ψ
    form a Heyting (pre)algebra: ∧ by pairing, ∨ by tagged pairs, → by
    application.  Hence every intuitionistic theorem is realized (T65).
  * Condition: over the one-point algebra realizability collapses to
    Ω = (Prop, ↔) (the ordinary topos of sets); the effective topos is the
    case of Kleene's first algebra K1 (its construction needs T81: OPEN).

  Lean 4 core only, hand-written proofs, no axioms.

  CIC dependence: realizability predicates are Prop-valued.
-/
import found.Minimal


universe u

namespace Found

namespace Real

structure CA where
  A : Type u
  app : A → A → A
  k : A
  s : A
  k_ax : ∀ x y, app (app k x) y = x
  s_ax : ∀ x y z, app (app (app s x) y) z = app (app x z) (app y z)

namespace CA
variable (M : CA)

def i : M.A := M.app (M.app M.s M.k) M.k

theorem i_ax (x : M.A) : M.app M.i x = x :=
  (M.s_ax M.k M.k x).trans (M.k_ax x _)

/-! ## Bracket abstraction -/

inductive Ex (M : CA) where
  | var : Nat → Ex M
  | c : M.A → Ex M
  | ap : Ex M → Ex M → Ex M

def cons (x : M.A) (ρ : Nat → M.A) : Nat → M.A
  | 0 => x
  | n + 1 => ρ n

def eval (ρ : Nat → M.A) : Ex M → M.A
  | .var n => ρ n
  | .c a => a
  | .ap e f => M.app (eval ρ e) (eval ρ f)

def lam : Ex M → Ex M
  | .var 0 => .c M.i
  | .var (n + 1) => .ap (.c M.k) (.var n)
  | .c a => .ap (.c M.k) (.c a)
  | .ap e f => .ap (.ap (.c M.s) (lam e)) (lam f)

/-- combinatory completeness -/
theorem lam_beta (ρ : Nat → M.A) (x : M.A) : (e : Ex M) →
    M.app (M.eval ρ (M.lam e)) x = M.eval (M.cons x ρ) e
  | .var 0 => M.i_ax x
  | .var (_ + 1) => M.k_ax _ x
  | .c a => M.k_ax a x
  | .ap e f => (M.s_ax _ _ x).trans
      (congrArg₂' M.app (lam_beta ρ x e) (lam_beta ρ x f))
where
  congrArg₂' {α β γ : Sort _} (g : α → β → γ) {a a' : α} {b b' : β} (h1 : a = a') (h2 : b = b') :
      g a b = g a' b' := by
    cases h1
    cases h2
    rfl

def z0 : Nat → M.A := fun _ => M.k

/-- closed λ-terms -/
def L1 (e : Ex M) : M.A := M.eval M.z0 (M.lam e)
def L2 (e : Ex M) : M.A := M.eval M.z0 (M.lam (M.lam e))
def L3 (e : Ex M) : M.A := M.eval M.z0 (M.lam (M.lam (M.lam e)))

/-- the pair combinator λx y f. f x y -/
def P : M.A := M.L3 (.ap (.ap (.var 0) (.var 2)) (.var 1))

def ki : M.A := M.app M.k M.i

theorem pair_k (x y : M.A) : M.app (M.app (M.app M.P x) y) M.k = x := by
  unfold P L3
  rw [lam_beta, lam_beta, lam_beta]
  exact M.k_ax x y

theorem pair_ki (x y : M.A) : M.app (M.app (M.app M.P x) y) M.ki = y := by
  unfold P L3
  rw [lam_beta, lam_beta, lam_beta]
  show M.app (M.app (M.app M.k M.i) x) y = y
  rw [M.k_ax]
  exact M.i_ax y

end CA

/-! ## The realizability Heyting algebra -/

section RHA
variable (M : CA)

def Pr : Type := M.A → Prop

def rle (φ ψ : M.A → Prop) : Prop := ∃ e, ∀ x, φ x → ψ (M.app e x)

theorem mem_eq {φ : M.A → Prop} {x y : M.A} (h : x = y) (p : φ y) : φ x := h ▸ p

def rmeet (a b : M.A → Prop) : M.A → Prop := fun t => a (M.app t M.k) ∧ b (M.app t M.ki)

def rjoin (a b : M.A → Prop) : M.A → Prop :=
  fun t => (M.app t M.k = M.k ∧ a (M.app t M.ki)) ∨ (M.app t M.k = M.ki ∧ b (M.app t M.ki))

def rimp (a b : M.A → Prop) : M.A → Prop := fun f => ∀ x, a x → b (M.app f x)

def RHA : HA.{u} where
  car := M.A → Prop
  le := rle M
  le_refl _ := ⟨M.i, fun x h => mem_eq M (M.i_ax x) h⟩
  le_trans := fun ⟨e₁, h₁⟩ ⟨e₂, h₂⟩ =>
    ⟨M.L1 (.ap (.c e₂) (.ap (.c e₁) (.var 0))),
     fun x hx => mem_eq M (M.lam_beta _ x _) (h₂ _ (h₁ x hx))⟩
  top := fun _ => True
  bot := fun _ => False
  meet := rmeet M
  join := rjoin M
  imp := rimp M
  le_top _ := ⟨M.i, fun _ _ => trivial⟩
  bot_le _ := ⟨M.i, fun _ h => h.elim⟩
  meet_l _ _ := ⟨M.L1 (.ap (.var 0) (.c M.k)), fun x h => mem_eq M (M.lam_beta _ x _) h.1⟩
  meet_r _ _ := ⟨M.L1 (.ap (.var 0) (.c M.ki)), fun x h => mem_eq M (M.lam_beta _ x _) h.2⟩
  meet_univ := fun ⟨e₁, h₁⟩ ⟨e₂, h₂⟩ =>
    ⟨M.L1 (.ap (.ap (.c M.P) (.ap (.c e₁) (.var 0))) (.ap (.c e₂) (.var 0))),
     fun x hx => by
      have e : M.app (M.L1 (.ap (.ap (.c M.P) (.ap (.c e₁) (.var 0))) (.ap (.c e₂) (.var 0)))) x =
          M.app (M.app M.P (M.app e₁ x)) (M.app e₂ x) := M.lam_beta _ x _
      rw [e]
      exact ⟨mem_eq M (M.pair_k _ _) (h₁ x hx), mem_eq M (M.pair_ki _ _) (h₂ x hx)⟩⟩
  join_l _ _ := ⟨M.L1 (.ap (.ap (.c M.P) (.c M.k)) (.var 0)), fun x hx => by
      have e : M.app (M.L1 (.ap (.ap (.c M.P) (.c M.k)) (.var 0))) x = M.app (M.app M.P M.k) x :=
        M.lam_beta _ x _
      rw [e]
      exact Or.inl ⟨M.pair_k _ _, mem_eq M (M.pair_ki _ _) hx⟩⟩
  join_r _ _ := ⟨M.L1 (.ap (.ap (.c M.P) (.c M.ki)) (.var 0)), fun x hx => by
      have e : M.app (M.L1 (.ap (.ap (.c M.P) (.c M.ki)) (.var 0))) x = M.app (M.app M.P M.ki) x :=
        M.lam_beta _ x _
      rw [e]
      exact Or.inr ⟨M.pair_k _ _, mem_eq M (M.pair_ki _ _) hx⟩⟩
  join_univ := fun ⟨e₁, h₁⟩ ⟨e₂, h₂⟩ =>
    ⟨M.L1 (.ap (.ap (.ap (.var 0) (.c M.k)) (.ap (.c e₁) (.ap (.var 0) (.c M.ki))))
               (.ap (.c e₂) (.ap (.var 0) (.c M.ki)))),
     fun t ht => by
      have e : M.app (M.L1 (.ap (.ap (.ap (.var 0) (.c M.k)) (.ap (.c e₁) (.ap (.var 0) (.c M.ki))))
               (.ap (.c e₂) (.ap (.var 0) (.c M.ki))))) t =
          M.app (M.app (M.app t M.k) (M.app e₁ (M.app t M.ki))) (M.app e₂ (M.app t M.ki)) :=
        M.lam_beta _ t _
      rw [e]
      cases ht with
      | inl h =>
        rw [h.1, M.k_ax]
        exact h₁ _ h.2
      | inr h =>
        rw [h.1]
        unfold CA.ki
        rw [M.k_ax, M.i_ax]
        exact h₂ _ h.2⟩
  imp_adj := ⟨fun ⟨e, h⟩ =>
      ⟨M.L2 (.ap (.c e) (.ap (.ap (.c M.P) (.var 1)) (.var 0))), fun z hz x hx => by
        have q : M.app (M.app (M.L2 (.ap (.c e) (.ap (.ap (.c M.P) (.var 1)) (.var 0)))) z) x =
            M.app e (M.app (M.app M.P z) x) := by
          unfold CA.L2
          rw [M.lam_beta, M.lam_beta]
          rfl
        rw [q]
        exact h _ ⟨mem_eq M (M.pair_k _ _) hz, mem_eq M (M.pair_ki _ _) hx⟩⟩,
    fun ⟨e, h⟩ =>
      ⟨M.L1 (.ap (.ap (.c e) (.ap (.var 0) (.c M.k))) (.ap (.var 0) (.c M.ki))), fun t ht => by
        have q : M.app (M.L1 (.ap (.ap (.c e) (.ap (.var 0) (.c M.k))) (.ap (.var 0) (.c M.ki)))) t =
            M.app (M.app e (M.app t M.k)) (M.app t M.ki) := M.lam_beta _ t _
        rw [q]
        exact h _ ht.1 _ ht.2⟩⟩

/-- every intuitionistic propositional theorem is uniformly realized -/
theorem realized {φ : Prp.Form} (d : Prp.PD true (fun _ => False) φ) (v : Nat → M.A → Prop) :
    ∃ e, ∀ x : M.A, Prp.interp (RHA M) v (fun _ => False) φ (M.app e x) :=
  match Prp.valid (RHA M) d v (fun _ => False) (fun _ _ => ⟨M.i, fun _ h => h.elim⟩) with
  | ⟨e, h⟩ => ⟨e, fun x => h x trivial⟩

end RHA

/-! ## The one-point algebra: realizability collapses to Ω -/

def unitCA : CA where
  A := Unit
  app _ _ := ()
  k := ()
  s := ()
  k_ax _ _ := rfl
  s_ax _ _ _ := rfl

theorem unit_le_iff (φ ψ : Unit → Prop) : (RHA unitCA).le φ ψ ↔ (φ () → ψ ()) :=
  ⟨fun ⟨_, h⟩ p => h () p, fun h => ⟨(), fun _ p => h p⟩⟩

end Real

end Found

#print axioms Found.Real.CA.i_ax
#print axioms Found.Real.CA.lam_beta
#print axioms Found.Real.CA.pair_k
#print axioms Found.Real.CA.pair_ki
#print axioms Found.Real.RHA
#print axioms Found.Real.realized
#print axioms Found.Real.unit_le_iff

found/EffectiveK1.lean

lean_all/found/EffectiveK1.lean

/-
  T96b. found/EffectiveK1 — realizability over Kleene's first algebra
  (codes = μ-recursive programs MR of T207 with their relational semantics
  Ev), the effective-topos point:
  * a numbering of programs: enc : MR → ℕ with a decoder (dec_enc), built
    on the pairing ⟨a,b⟩ = 2^a(2b+1) with hand-written parity/halving;
  * Ev is deterministic;
  * Church's thesis is realized (a realizer of ∀n∃m φ is a program);
  * ∀n (H n ∨ ¬H n) has no realizer, for H n = "program n halts on n"
    (diagonal: the program μt.d(n) halts on n exactly when d says no).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.ChurchTuring3

namespace Found

namespace K1

open CT3 Rec

/-! ## pairing ⟨a,b⟩ = 2^a (2b+1) -/

def dbl (n : Nat) : Nat := n + n

def pr : Nat → Nat → Nat
  | 0, b => dbl b + 1
  | a + 1, b => dbl (pr a b)

/-- parity: n = 2q + r -/
def par : Nat → Bool × Nat
  | 0 => (false, 0)
  | 1 => (true, 0)
  | n + 2 => ((par n).1, (par n).2 + 1)

theorem par_dbl : (n : Nat) → par (dbl n) = (false, n)
  | 0 => rfl
  | n + 1 => by
    show par (n + 1 + (n + 1)) = _
    have e : n + 1 + (n + 1) = (n + n) + 2 := by
      rw [Nat.add_succ, Nat.succ_add]
    rw [e]
    show ((par (dbl n)).1, (par (dbl n)).2 + 1) = _
    rw [par_dbl n]

theorem par_dbl1 : (n : Nat) → par (dbl n + 1) = (true, n)
  | 0 => rfl
  | n + 1 => by
    have e : dbl (n + 1) + 1 = (dbl n + 1) + 2 := by
      show n + 1 + (n + 1) + 1 = n + n + 1 + 2
      rw [Nat.add_succ (n + 1) n, Nat.succ_add n n]
    rw [e]
    show ((par (dbl n + 1)).1, (par (dbl n + 1)).2 + 1) = _
    rw [par_dbl1 n]

def unp : Nat → Nat → Nat × Nat
  | 0, _ => (0, 0)
  | f + 1, w => match par w with
    | (true, q) => (0, q)
    | (false, q) => ((unp f q).1 + 1, (unp f q).2)

theorem unp_pr : (a b f : Nat) → a < f → unp f (pr a b) = (a, b)
  | 0, b, f + 1, _ => by
    show (match par (dbl b + 1) with
      | (true, q) => (0, q)
      | (false, q) => ((unp f q).1 + 1, (unp f q).2)) = _
    rw [par_dbl1]
  | a + 1, b, f + 1, h => by
    show (match par (dbl (pr a b)) with
      | (true, q) => (0, q)
      | (false, q) => ((unp f q).1 + 1, (unp f q).2)) = _
    rw [par_dbl]
    show ((unp f (pr a b)).1 + 1, (unp f (pr a b)).2) = _
    rw [unp_pr a b f (Nat.lt_of_succ_lt_succ h)]
  | _, _, 0, h => absurd h (Nat.not_lt_zero _)

theorem pr_ge : (a b : Nat) → a + b < pr a b
  | 0, b => by
    show 0 + b < b + b + 1
    rw [Nat.zero_add]
    exact Nat.lt_succ_of_le (Nat.le_add_left b b)
  | a + 1, b => by
    show a + 1 + b < pr a b + pr a b
    have h := pr_ge a b
    have h1 : a + 1 + b ≤ pr a b := by
      rw [Nat.add_right_comm]
      exact h
    exact Nat.lt_of_le_of_lt h1 (Nat.lt_add_of_pos_right (Nat.lt_of_le_of_lt (Nat.zero_le _) h))

def unpS (w : Nat) : Nat × Nat := unp w w

theorem unpS_pr (a b : Nat) : unpS (pr a b) = (a, b) :=
  unp_pr a b _ (Nat.lt_of_le_of_lt (Nat.le_add_right a b) (pr_ge a b))

/-! ## numbering of programs -/

mutual
def enc : MR → Nat
  | .zero => pr 0 0
  | .succ => pr 1 0
  | .proj i => pr 2 i
  | .comp f gs => pr 3 (pr (enc f) (encL gs))
  | .prec f g => pr 4 (pr (enc f) (enc g))
  | .mu f => pr 5 (enc f)
def encL : List MR → Nat
  | [] => 0
  | g :: gs => pr (enc g) (encL gs) + 1
end

mutual
def dec : Nat → Nat → MR
  | 0, _ => .zero
  | f + 1, n => match (unpS n).1 with
    | 0 => .zero
    | 1 => .succ
    | 2 => .proj (unpS n).2
    | 3 => .comp (dec f (unpS (unpS n).2).1) (decL f (unpS (unpS n).2).2)
    | 4 => .prec (dec f (unpS (unpS n).2).1) (dec f (unpS (unpS n).2).2)
    | 5 => .mu (dec f (unpS n).2)
    | _ + 6 => .zero
def decL : Nat → Nat → List MR
  | 0, _ => []
  | _ + 1, 0 => []
  | f + 1, n + 1 => dec f (unpS n).1 :: decL f (unpS n).2
end

theorem lt_pr_l (a b : Nat) : a < pr a b := Nat.lt_of_le_of_lt (Nat.le_add_right a b) (pr_ge a b)
theorem lt_pr_r (a b : Nat) : b < pr a b := Nat.lt_of_le_of_lt (Nat.le_add_left b a) (pr_ge a b)

mutual
theorem dec_enc : (g : MR) → (f : Nat) → enc g < f → dec f (enc g) = g
  | .zero, f + 1, _ => by unfold enc dec; rw [unpS_pr]
  | .succ, f + 1, _ => by unfold enc dec; rw [unpS_pr]
  | .proj i, f + 1, _ => by unfold enc dec; rw [unpS_pr]
  | .comp g gs, f + 1, h => by
    unfold enc dec
    rw [unpS_pr]
    show MR.comp (dec f (unpS (pr (enc g) (encL gs))).1) (decL f (unpS (pr (enc g) (encL gs))).2) = _
    rw [unpS_pr]
    have h1 : pr (enc g) (encL gs) < f + 1 := Nat.lt_trans (lt_pr_r 3 _) h
    rw [dec_enc g f (Nat.lt_of_lt_of_le (lt_pr_l _ _) (Nat.le_of_lt_succ h1)),
        decL_encL gs f (Nat.lt_of_lt_of_le (lt_pr_r _ _) (Nat.le_of_lt_succ h1))]
  | .prec g k, f + 1, h => by
    unfold enc dec
    rw [unpS_pr]
    show MR.prec (dec f (unpS (pr (enc g) (enc k))).1) (dec f (unpS (pr (enc g) (enc k))).2) = _
    rw [unpS_pr]
    have h1 : pr (enc g) (enc k) < f + 1 := Nat.lt_trans (lt_pr_r 4 _) h
    rw [dec_enc g f (Nat.lt_of_lt_of_le (lt_pr_l _ _) (Nat.le_of_lt_succ h1)),
        dec_enc k f (Nat.lt_of_lt_of_le (lt_pr_r _ _) (Nat.le_of_lt_succ h1))]
  | .mu g, f + 1, h => by
    unfold enc dec
    rw [unpS_pr]
    show MR.mu (dec f (enc g)) = _
    rw [dec_enc g f (Nat.lt_of_lt_of_le (lt_pr_r 5 _) (Nat.le_of_lt_succ h))]
  | _, 0, h => absurd h (Nat.not_lt_zero _)
theorem decL_encL : (gs : List MR) → (f : Nat) → encL gs < f → decL f (encL gs) = gs
  | [], f + 1, _ => rfl
  | g :: gs, f + 1, h => by
    show dec f (unpS (pr (enc g) (encL gs))).1 :: decL f (unpS (pr (enc g) (encL gs))).2 = _
    rw [unpS_pr]
    have h1 : pr (enc g) (encL gs) < f := Nat.lt_of_succ_lt_succ h
    rw [dec_enc g f (Nat.lt_trans (lt_pr_l _ _) h1), decL_encL gs f (Nat.lt_trans (lt_pr_r _ _) h1)]
  | _, 0, h => absurd h (Nat.not_lt_zero _)
end

/-- the program numbered n -/
def prog (n : Nat) : MR := dec (n + 1) n

theorem prog_enc (g : MR) : prog (enc g) = g := dec_enc g _ (Nat.lt_succ_self _)

/-! ## determinism -/

def hdN : List Nat → Nat
  | [] => 0
  | a :: _ => a

def tlN : List Nat → List Nat
  | [] => []
  | _ :: l => l

def isNilB : List Nat → Bool
  | [] => true
  | _ :: _ => false

def getF : MR → MR
  | .zero => .zero
  | .succ => .zero
  | .proj _ => .zero
  | .comp f _ => f
  | .prec f _ => f
  | .mu f => f

def getG : MR → MR
  | .zero => .zero
  | .succ => .zero
  | .proj _ => .zero
  | .comp _ _ => .zero
  | .prec _ g => g
  | .mu _ => .zero

def getGs : MR → List MR
  | .zero => []
  | .succ => []
  | .proj _ => []
  | .comp _ gs => gs
  | .prec _ _ => []
  | .mu _ => []

def tag : MR → Nat
  | .zero => 0
  | .succ => 1
  | .proj _ => 2
  | .comp _ _ => 3
  | .prec _ _ => 4
  | .mu _ => 5

def hdM : List MR → MR
  | [] => .zero
  | g :: _ => g

def tlM : List MR → List MR
  | [] => []
  | _ :: l => l

def isNilM : List MR → Bool
  | [] => true
  | _ :: _ => false

/-! Inversion lemmas with variable indices: `cases` on a derivation whose index is a
  constructor application unifies through injEq lemmas (propext), so every inversion
  goes through equations and projection functions instead. -/

theorem beq_refl' : (n : Nat) → Nat.beq n n = true
  | 0 => rfl
  | n + 1 => beq_refl' n

theorem tag_ne' {m m' : MR} (e : m = m') (h : Nat.beq (tag m) (tag m') = false) : False :=
  Bool.noConfusion (h.symm.trans ((congrArg (fun x => Nat.beq (tag x) (tag m')) e).trans (beq_refl' (tag m'))))

theorem inv_zero {h a v} (d : Ev h a v) (eh : h = .zero) : v = 0 := by
  cases d with
  | zero => rfl
  | succ => exact (tag_ne' eh rfl).elim
  | proj => exact (tag_ne' eh rfl).elim
  | comp => exact (tag_ne' eh rfl).elim
  | precNil => exact (tag_ne' eh rfl).elim
  | prec0 => exact (tag_ne' eh rfl).elim
  | precS => exact (tag_ne' eh rfl).elim
  | mu => exact (tag_ne' eh rfl).elim

theorem inv_succ {h a v} (d : Ev h a v) (eh : h = .succ) : v = nth a 0 + 1 := by
  cases d with
  | succ => rfl
  | zero => exact (tag_ne' eh rfl).elim
  | proj => exact (tag_ne' eh rfl).elim
  | comp => exact (tag_ne' eh rfl).elim
  | precNil => exact (tag_ne' eh rfl).elim
  | prec0 => exact (tag_ne' eh rfl).elim
  | precS => exact (tag_ne' eh rfl).elim
  | mu => exact (tag_ne' eh rfl).elim

def projIdx : MR → Nat
  | .zero => 0
  | .succ => 0
  | .proj i => i
  | .comp _ _ => 0
  | .prec _ _ => 0
  | .mu _ => 0

theorem inv_proj {h a v i} (d : Ev h a v) (eh : h = .proj i) : v = nth a i := by
  cases d with
  | proj j => exact congrArg (nth a) (congrArg projIdx eh)
  | zero => exact (tag_ne' eh rfl).elim
  | succ => exact (tag_ne' eh rfl).elim
  | comp => exact (tag_ne' eh rfl).elim
  | precNil => exact (tag_ne' eh rfl).elim
  | prec0 => exact (tag_ne' eh rfl).elim
  | precS => exact (tag_ne' eh rfl).elim
  | mu => exact (tag_ne' eh rfl).elim

theorem inv_comp {h a v f gs} (d : Ev h a v) (eh : h = .comp f gs) : ∃ bs, EvL gs a bs ∧ Ev f bs v := by
  cases d with
  | @comp f' gs' _ bs _ l e =>
    have hf : f' = f := congrArg getF eh
    have hg : gs' = gs := congrArg getGs eh
    subst hf hg
    exact ⟨bs, l, e⟩
  | zero => exact (tag_ne' eh rfl).elim
  | succ => exact (tag_ne' eh rfl).elim
  | proj => exact (tag_ne' eh rfl).elim
  | precNil => exact (tag_ne' eh rfl).elim
  | prec0 => exact (tag_ne' eh rfl).elim
  | precS => exact (tag_ne' eh rfl).elim
  | mu => exact (tag_ne' eh rfl).elim

theorem inv_mu {h a v f} (d : Ev h a v) (eh : h = .mu f) : MuS f a 0 v := by
  cases d with
  | @mu f' _ _ m =>
    have hf : f' = f := congrArg getF eh
    subst hf
    exact m
  | zero => exact (tag_ne' eh rfl).elim
  | succ => exact (tag_ne' eh rfl).elim
  | proj => exact (tag_ne' eh rfl).elim
  | comp => exact (tag_ne' eh rfl).elim
  | precNil => exact (tag_ne' eh rfl).elim
  | prec0 => exact (tag_ne' eh rfl).elim
  | precS => exact (tag_ne' eh rfl).elim

/-- prec-shaped derivations: the three cases, by the shape of the arguments -/
theorem inv_prec {h a v f g} (d : Ev h a v) (eh : h = .prec f g) :
    (a = [] ∧ Ev f [] v) ∨ (∃ rest, a = 0 :: rest ∧ Ev f rest v) ∨
    (∃ k rest v0, a = (k + 1) :: rest ∧ Ev (.prec f g) (k :: rest) v0 ∧ Ev g (k :: v0 :: rest) v) := by
  cases d with
  | @precNil f' g' _ e =>
    have hf : f' = f := congrArg getF eh
    subst hf
    exact Or.inl ⟨rfl, e⟩
  | @prec0 f' g' rest _ e =>
    have hf : f' = f := congrArg getF eh
    subst hf
    exact Or.inr (Or.inl ⟨rest, rfl, e⟩)
  | @precS f' g' k rest v0 _ p e =>
    have hf : f' = f := congrArg getF eh
    have hg : g' = g := congrArg getG eh
    subst hf hg
    exact Or.inr (Or.inr ⟨k, rest, v0, rfl, p, e⟩)
  | zero => exact (tag_ne' eh rfl).elim
  | succ => exact (tag_ne' eh rfl).elim
  | proj => exact (tag_ne' eh rfl).elim
  | comp => exact (tag_ne' eh rfl).elim
  | mu => exact (tag_ne' eh rfl).elim

theorem invL {gs a bs} (d : EvL gs a bs) :
    (gs = [] ∧ bs = []) ∨ (∃ b bs', bs = b :: bs' ∧ Ev (hdM gs) a b ∧ EvL (tlM gs) a bs') := by
  cases d with
  | nil => exact Or.inl ⟨rfl, rfl⟩
  | @cons g gs' _ b bs' e l => exact Or.inr ⟨b, bs', rfl, e, l⟩

mutual
theorem ev_det : {h : MR} → {a : List Nat} → {v v' : Nat} → Ev h a v → Ev h a v' → v = v'
  | _, _, _, _, .zero _, d => (inv_zero d rfl).symm
  | _, _, _, _, .succ _, d => (inv_succ d rfl).symm
  | _, _, _, _, .proj _ _, d => (inv_proj d rfl).symm
  | _, _, _, _, .comp l1 e1, d => match inv_comp d rfl with
    | ⟨_, l2, e2⟩ => by
      have hb := evL_det l1 l2
      subst hb
      exact ev_det e1 e2
  | _, _, _, _, .precNil e1, d => match inv_prec d rfl with
    | Or.inl ⟨_, e2⟩ => ev_det e1 e2
    | Or.inr (Or.inl ⟨_, ea, _⟩) => Bool.noConfusion (congrArg isNilB ea)
    | Or.inr (Or.inr ⟨_, _, _, ea, _, _⟩) => Bool.noConfusion (congrArg isNilB ea)
  | _, _, _, _, .prec0 (rest := rest) e1, d => match inv_prec d rfl with
    | Or.inl ⟨ea, _⟩ => Bool.noConfusion (congrArg isNilB ea)
    | Or.inr (Or.inl ⟨rest', ea, e2⟩) => by
      have hr : rest = rest' := congrArg tlN ea
      subst hr
      exact ev_det e1 e2
    | Or.inr (Or.inr ⟨_, _, _, ea, _, _⟩) => Nat.noConfusion (congrArg hdN ea)
  | _, _, _, _, .precS (k := k) (rest := rest) p1 e1, d => match inv_prec d rfl with
    | Or.inl ⟨ea, _⟩ => Bool.noConfusion (congrArg isNilB ea)
    | Or.inr (Or.inl ⟨_, ea, _⟩) => Nat.noConfusion (congrArg hdN ea)
    | Or.inr (Or.inr ⟨k', rest', _, ea, p2, e2⟩) => by
      have hk : k = k' := Nat.succ.inj (congrArg hdN ea)
      have hr : rest = rest' := congrArg tlN ea
      subst hk hr
      have hv := ev_det p1 p2
      subst hv
      exact ev_det e1 e2
  | _, _, _, _, .mu m1, d => muS_det m1 (inv_mu d rfl)
theorem evL_det : {gs : List MR} → {a bs bs' : List Nat} → EvL gs a bs → EvL gs a bs' → bs = bs'
  | _, _, _, _, .nil _, d => match invL d with
    | Or.inl ⟨_, e⟩ => e.symm
    | Or.inr ⟨_, _, _, _, _⟩ => by
      cases d with
      | nil => rfl
  | _, _, _, _, .cons e1 l1, d => match invL d with
    | Or.inl ⟨eg, _⟩ => Bool.noConfusion (congrArg isNilM eg)
    | Or.inr ⟨_, _, eb, e2, l2⟩ => by
      rw [eb, ev_det e1 e2, evL_det l1 l2]
theorem muS_det : {f : MR} → {a : List Nat} → {j n n' : Nat} → MuS f a j n → MuS f a j n' → n = n'
  | _, _, _, _, _, .stop e1, d => by
    cases d with
    | stop _ => rfl
    | next e2 _ => exact Nat.noConfusion (ev_det e1 e2)
  | _, _, _, _, _, .next e1 m1, d => by
    cases d with
    | stop e2 => exact Nat.noConfusion (ev_det e1 e2)
    | next _ m2 => exact muS_det m1 m2
end

/-! ## realizability: Church's thesis, and the failure of decidability of halting -/

/-- e realizes ∀n ∃m φ n m -/
def RealizesAE (e : MR) (φ : Nat → Nat → Prop) : Prop := ∀ n, ∃ m, Ev e [n] m ∧ φ n m

/-- Church's thesis: a realized ∀∃ statement has a program computing its witnesses -/
theorem church_thesis (φ : Nat → Nat → Prop) (e : MR) (h : RealizesAE e φ) :
    ∃ g : MR, ∀ n, ∃ m, Ev g [n] m ∧ φ n m := ⟨e, h⟩

/-- e realizes ∀n (P n ∨ ¬P n): it answers 1 for P and 0 for ¬P -/
def RealizesDec (e : MR) (P : Nat → Prop) : Prop :=
  ∀ n, (Ev e [n] 1 ∧ P n) ∨ (Ev e [n] 0 ∧ ¬ P n)

def Halts (n : Nat) : Prop := ∃ v, Ev (prog n) [n] v

/-- the diagonal program for a candidate decider d: μt. d(n) -/
def diag (d : MR) : MR := .mu (.comp d [.proj 1])

theorem diag_halts (d : MR) (n : Nat) (h : Ev d [n] 0) : Ev (diag d) [n] 0 :=
  .mu (.stop (.comp (.cons (.proj 1 [0, n]) (.nil _)) h))

theorem diag_mu (d : MR) (n : Nat) (h : Ev d [n] 1) : (f : MR) → (a : List Nat) → (j w : Nat) →
    MuS f a j w → f = .comp d [.proj 1] → a = [n] → False
  | _, _, _, _, .stop e, hf, ha => by
    subst hf ha
    match inv_comp e rfl with
    | ⟨bs, l, e'⟩ => match invL l with
      | Or.inl ⟨eg, _⟩ => exact Bool.noConfusion (congrArg isNilM eg)
      | Or.inr ⟨b, bs', eb, ep, l'⟩ =>
        have eb' : bs' = [] := evL_det l' (EvL.nil _)
        have hb : b = n := inv_proj ep rfl
        have e'' : Ev d [n] 0 := by rw [eb, eb', hb] at e'; exact e'
        exact Nat.noConfusion (ev_det h e'')
  | _, _, _, _, .next _ ms, hf, ha => diag_mu d n h _ _ _ _ ms hf ha

theorem diag_diverges (d : MR) (n : Nat) (h : Ev d [n] 1) : ∀ v, ¬ Ev (diag d) [n] v := by
  intro v hv
  cases hv with
  | mu m =>
    exact diag_mu d n h _ _ _ _ m rfl rfl

/-- ∀n (Halts n ∨ ¬Halts n) has no realizer -/
theorem no_halting_realizer : ¬ ∃ d : MR, RealizesDec d Halts := fun ⟨d, hd⟩ => by
  have hp : prog (enc (diag d)) = diag d := prog_enc (diag d)
  cases hd (enc (diag d)) with
  | inl h =>
    match h.2 with
    | ⟨v, hv⟩ =>
      rw [hp] at hv
      exact diag_diverges d _ h.1 v hv
  | inr h =>
    exact h.2 ⟨0, by rw [hp]; exact diag_halts d _ h.1⟩

end K1

end Found

#print axioms Found.K1.unpS_pr
#print axioms Found.K1.pr_ge
#print axioms Found.K1.dec_enc
#print axioms Found.K1.prog_enc
#print axioms Found.K1.ev_det
#print axioms Found.K1.church_thesis
#print axioms Found.K1.diag_diverges
#print axioms Found.K1.no_halting_realizer

found/Enriched.lean

lean_all/found/Enriched.lean

/-
  T193 line. found/Enriched — enriched categories as a point: a category
  enriched in a monoidal preorder V (Lawvere).  Hom objects live in V,
  identities are I ≤ C(a,a), composition is C(a,b) ⊗ C(b,c) ≤ C(a,c).
  * V = (Prop, ∧, True): V-categories are exactly preorders (both ways).
  * V = (ℕ, +, 0) ordered by ≥: V-categories are Lawvere (generalized)
    metric spaces: d(a,a) = 0 and the triangle inequality.
  * Change of base along a lax monoidal monotone map V → W; the map
    ℕ → Prop, d ↦ (d = 0), turns a metric space into its specialization
    preorder.
  * V-functors compose; non-expansive maps are the ℕ-functors.

  Lean 4 core only, hand-written proofs, no axioms.
-/

namespace Found

namespace Enr

universe u v

structure MonPre where
  car : Type u
  le : car → car → Prop
  refl : ∀ a, le a a
  trans : ∀ {a b c}, le a b → le b c → le a c
  ten : car → car → car
  unit : car
  ten_mono : ∀ {a a' b b'}, le a a' → le b b' → le (ten a b) (ten a' b')

structure VCat (V : MonPre.{u}) where
  Ob : Type v
  hom : Ob → Ob → V.car
  id : ∀ a, V.le V.unit (hom a a)
  comp : ∀ a b c, V.le (V.ten (hom a b) (hom b c)) (hom a c)

structure VFun {V : MonPre.{u}} (C D : VCat.{u, v} V) where
  obj : C.Ob → D.Ob
  map : ∀ a b, V.le (C.hom a b) (D.hom (obj a) (obj b))

def VFun.id' {V : MonPre.{u}} (C : VCat.{u, v} V) : VFun C C := ⟨fun a => a, fun _ _ => V.refl _⟩

def VFun.comp' {V : MonPre.{u}} {C D E : VCat.{u, v} V} (F : VFun C D) (G : VFun D E) : VFun C E :=
  ⟨fun a => G.obj (F.obj a), fun a b => V.trans (F.map a b) (G.map _ _)⟩

/-! ## V = truth values -/

def PropV : MonPre.{0} where
  car := Prop
  le p q := p → q
  refl _ h := h
  trans h k x := k (h x)
  ten p q := p ∧ q
  unit := True
  ten_mono h k x := ⟨h x.1, k x.2⟩

structure Preord where
  P : Type
  le : P → P → Prop
  refl : ∀ a, le a a
  trans : ∀ {a b c}, le a b → le b c → le a c

def ofPreord (Q : Preord) : VCat.{0, 0} PropV where
  Ob := Q.P
  hom := Q.le
  id a _ := Q.refl a
  comp _ _ _ h := Q.trans h.1 h.2

def toPreord (C : VCat.{0, 0} PropV) : Preord where
  P := C.Ob
  le := C.hom
  refl a := C.id a trivial
  trans {a b c} h k := C.comp a b c ⟨h, k⟩

theorem round_le (Q : Preord) (a b : Q.P) : (toPreord (ofPreord Q)).le a b ↔ Q.le a b := Iff.rfl
theorem round_hom (C : VCat.{0, 0} PropV) (a b : C.Ob) : (ofPreord (toPreord C)).hom a b ↔ C.hom a b := Iff.rfl

/-- monotone maps are exactly the Prop-functors -/
def monoFun (Q R : Preord) (f : Q.P → R.P) (hf : ∀ a b, Q.le a b → R.le (f a) (f b)) :
    VFun (ofPreord Q) (ofPreord R) := ⟨f, hf⟩

/-! ## V = distances (Lawvere metric spaces) -/

def NatV : MonPre.{0} where
  car := Nat
  le x y := y ≤ x
  refl x := Nat.le_refl x
  trans h k := Nat.le_trans k h
  ten x y := x + y
  unit := 0
  ten_mono h k := Nat.add_le_add h k

structure LMetric where
  X : Type
  d : X → X → Nat
  zero : ∀ a, d a a = 0
  tri : ∀ a b c, d a c ≤ d a b + d b c

def ofMetric (M : LMetric) : VCat.{0, 0} NatV where
  Ob := M.X
  hom := M.d
  id a := by
    show M.d a a ≤ 0
    rw [M.zero a]
    exact Nat.le_refl 0
  comp a b c := M.tri a b c

def toMetric (C : VCat.{0, 0} NatV) : LMetric where
  X := C.Ob
  d := C.hom
  zero a := Nat.le_antisymm (C.id a) (Nat.zero_le _)
  tri a b c := C.comp a b c

/-- non-expansive maps are the ℕ-functors -/
def nonexp (M N : LMetric) (f : M.X → N.X) (hf : ∀ a b, N.d (f a) (f b) ≤ M.d a b) :
    VFun (ofMetric M) (ofMetric N) := ⟨f, hf⟩

/-! ## change of base -/

structure LaxMon (V W : MonPre.{u}) where
  f : V.car → W.car
  mono : ∀ {a b}, V.le a b → W.le (f a) (f b)
  unit : W.le W.unit (f V.unit)
  ten : ∀ a b, W.le (W.ten (f a) (f b)) (f (V.ten a b))

def base {V W : MonPre.{u}} (φ : LaxMon V W) (C : VCat.{u, v} V) : VCat.{u, v} W where
  Ob := C.Ob
  hom a b := φ.f (C.hom a b)
  id a := W.trans φ.unit (φ.mono (C.id a))
  comp a b c := W.trans (φ.ten _ _) (φ.mono (C.comp a b c))

theorem le_zero {x : Nat} (h : x ≤ 0) : x = 0 := Nat.le_antisymm h (Nat.zero_le x)

def zf (d : Nat) : Prop := d = 0

theorem zf_mono {a b : Nat} (h : b ≤ a) (ha : zf a) : zf b := le_zero (show b ≤ 0 from ha ▸ h)

theorem zf_ten (a b : Nat) (h : zf a ∧ zf b) : zf (a + b) := by
  show a + b = 0
  rw [h.1, h.2]

/-- d ↦ (d = 0): distance zero is a lax monoidal map ℕ → Prop -/
def isZero : LaxMon NatV PropV where
  f := zf
  mono h ha := zf_mono h ha
  unit _ := rfl
  ten a b h := zf_ten a b h

/-- the specialization preorder of a metric space -/
theorem spec_preorder (M : LMetric) (a b : M.X) :
    (base isZero (ofMetric M)).hom a b ↔ M.d a b = 0 := Iff.rfl

end Enr

end Found

#print axioms Found.Enr.VFun.comp'
#print axioms Found.Enr.ofPreord
#print axioms Found.Enr.toPreord
#print axioms Found.Enr.round_le
#print axioms Found.Enr.round_hom
#print axioms Found.Enr.ofMetric
#print axioms Found.Enr.toMetric
#print axioms Found.Enr.nonexp
#print axioms Found.Enr.base
#print axioms Found.Enr.isZero
#print axioms Found.Enr.spec_preorder

found/Epsilon.lean

lean_all/found/Epsilon.lean

/-
  T214. found/Epsilon — Hilbert's ε-calculus as a point of the chart: an
  ε-operator on a domain D (a value of the choice parameter, T89) with the
  critical axiom A(t) → A(εx.A(x)).
  * ∃ is definable: (∃ x, A x) ↔ A(εA);
  * ∀ is definable at the classical point: (∀ x, A x) ↔ A(ε¬A) (with
    excluded middle as a hypothesis variable);
  * ε gives the axiom of choice: ∀ a ∃ b R a b ⇒ a choice function a ↦ ε(R a);
  * extensional ε (coextensive predicates get the same witness) on Bool
    gives excluded middle (Diaconescu's argument, no propext): the line from
    the ε-point to the classical point (T66).

  Lean 4 core only, hand-written proofs, no axioms.
-/

namespace Found

namespace Epsilon

/-- an ε-operator with the critical axiom -/
structure Eps (D : Type) where
  eps : (D → Prop) → D
  crit : ∀ (A : D → Prop) (t : D), A t → A (eps A)

variable {D : Type} (E : Eps D)

/-- ∃ from ε -/
theorem ex_iff (A : D → Prop) : (∃ x, A x) ↔ A (E.eps A) :=
  ⟨fun ⟨t, h⟩ => E.crit A t h, fun h => ⟨_, h⟩⟩

/-- ∀ from ε, at the classical point -/
theorem all_iff (lem : ∀ p : Prop, p ∨ ¬ p) (A : D → Prop) : (∀ x, A x) ↔ A (E.eps (fun x => ¬ A x)) :=
  ⟨fun h => h _, fun h x => (lem (A x)).elim id
    (fun hn => absurd h (E.crit (fun x => ¬ A x) x hn))⟩

/-- ε gives choice functions -/
theorem choice (E : Eps D) {A : Type} (R : A → D → Prop) (h : ∀ a, ∃ b, R a b) : ∃ f : A → D, ∀ a, R a (f a) :=
  ⟨fun a => E.eps (R a), fun a => (h a).elim (fun b hb => E.crit (R a) b hb)⟩

/-- extensional ε -/
def Extensional : Prop := ∀ A B : D → Prop, (∀ x, A x ↔ B x) → E.eps A = E.eps B

/-- Diaconescu: an extensional ε on Bool decides every proposition -/
theorem diaconescu (E : Eps Bool) (hE : Extensional E) (p : Prop) : p ∨ ¬ p :=
  let U : Bool → Prop := fun b => b = true ∨ p
  let V : Bool → Prop := fun b => b = false ∨ p
  have hu : U (E.eps U) := E.crit U true (.inl rfl)
  have hv : V (E.eps V) := E.crit V false (.inl rfl)
  hu.elim
    (fun eu => hv.elim
      (fun ev => .inr (fun hp =>
        have e : E.eps U = E.eps V := hE U V (fun _ => ⟨fun _ => .inr hp, fun _ => .inr hp⟩)
        Bool.noConfusion (eu.symm.trans (e.trans ev))))
      (fun h => .inl h))
    (fun h => .inl h)

end Epsilon

end Found

#print axioms Found.Epsilon.ex_iff
#print axioms Found.Epsilon.all_iff
#print axioms Found.Epsilon.choice
#print axioms Found.Epsilon.diaconescu

found/EtB1.lean

lean_all/found/EtB1.lean

/-
  T209 (Lawvere–Mitchell), direction B, part 1. found/EtB1 — an abstract
  model of Lawvere's ETCS: a category with terminal and initial objects,
  binary products and coproducts, equalizers, a subobject classifier,
  exponentials, a natural numbers object, in which 1 is a generator
  (well-pointed), which is nondegenerate and satisfies choice (epis split).
  Arrows are compared with the equality of the arrow types (no
  extensionality is used). Basic consequences: points of products, monos
  from injectivity on points, epis from surjectivity on points, splitting
  of point-surjections, 0 has no points.

  Lean 4 core only, hand-written proofs, no axioms.
-/

namespace Found

namespace ETB

structure ETCS where
  Obj : Type
  Hom : Obj → Obj → Type
  comp : {A B C : Obj} → Hom B C → Hom A B → Hom A C
  id : (A : Obj) → Hom A A
  assoc : ∀ {A B C D : Obj} (h : Hom C D) (g : Hom B C) (f : Hom A B), comp h (comp g f) = comp (comp h g) f
  id_l : ∀ {A B : Obj} (f : Hom A B), comp (id B) f = f
  id_r : ∀ {A B : Obj} (f : Hom A B), comp f (id A) = f
  one : Obj
  bang : (A : Obj) → Hom A one
  bang_u : ∀ {A : Obj} (f : Hom A one), f = bang A
  zero : Obj
  ini : (A : Obj) → Hom zero A
  ini_u : ∀ {A : Obj} (f : Hom zero A), f = ini A
  prod : Obj → Obj → Obj
  p1 : {A B : Obj} → Hom (prod A B) A
  p2 : {A B : Obj} → Hom (prod A B) B
  pair : {A B C : Obj} → Hom C A → Hom C B → Hom C (prod A B)
  p1_pair : ∀ {A B C : Obj} (f : Hom C A) (g : Hom C B), comp p1 (pair f g) = f
  p2_pair : ∀ {A B C : Obj} (f : Hom C A) (g : Hom C B), comp p2 (pair f g) = g
  pair_u : ∀ {A B C : Obj} (h : Hom C (prod A B)), h = pair (comp p1 h) (comp p2 h)
  coprod : Obj → Obj → Obj
  i1 : {A B : Obj} → Hom A (coprod A B)
  i2 : {A B : Obj} → Hom B (coprod A B)
  copair : {A B C : Obj} → Hom A C → Hom B C → Hom (coprod A B) C
  copair_i1 : ∀ {A B C : Obj} (f : Hom A C) (g : Hom B C), comp (copair f g) i1 = f
  copair_i2 : ∀ {A B C : Obj} (f : Hom A C) (g : Hom B C), comp (copair f g) i2 = g
  copair_u : ∀ {A B C : Obj} (h : Hom (coprod A B) C), h = copair (comp h i1) (comp h i2)
  eqz : {A B : Obj} → Hom A B → Hom A B → Obj
  eqm : {A B : Obj} → (f g : Hom A B) → Hom (eqz f g) A
  eq_comm : ∀ {A B : Obj} (f g : Hom A B), comp f (eqm f g) = comp g (eqm f g)
  eq_lift : ∀ {A B C : Obj} (f g : Hom A B) (h : Hom C A), comp f h = comp g h → ∃ k : Hom C (eqz f g), comp (eqm f g) k = h
  eqm_mono : ∀ {A B C : Obj} (f g : Hom A B) (k k' : Hom C (eqz f g)), comp (eqm f g) k = comp (eqm f g) k' → k = k'
  Ω : Obj
  tru : Hom one Ω
  chi : {S A : Obj} → (m : Hom S A) → (∀ {X : Obj} (f g : Hom X S), comp m f = comp m g → f = g) → Hom A Ω
  chi_comm : ∀ {S A : Obj} (m : Hom S A) (hm : ∀ {X : Obj} (f g : Hom X S), comp m f = comp m g → f = g),
    comp (chi m hm) m = comp tru (bang S)
  chi_pb : ∀ {S A : Obj} (m : Hom S A) (hm : ∀ {X : Obj} (f g : Hom X S), comp m f = comp m g → f = g)
    {X : Obj} (x : Hom X A), comp (chi m hm) x = comp tru (bang X) → ∃ y : Hom X S, comp m y = x
  chi_u : ∀ {S A : Obj} (m : Hom S A) (hm : ∀ {X : Obj} (f g : Hom X S), comp m f = comp m g → f = g)
    (χ : Hom A Ω), comp χ m = comp tru (bang S) →
    (∀ {X : Obj} (x : Hom X A), comp χ x = comp tru (bang X) → ∃ y : Hom X S, comp m y = x) → χ = chi m hm
  exp : Obj → Obj → Obj
  ev : {A B : Obj} → Hom (prod (exp B A) A) B
  cur : {A B C : Obj} → Hom (prod C A) B → Hom C (exp B A)
  ev_cur : ∀ {A B C : Obj} (f : Hom (prod C A) B), comp ev (pair (comp (cur f) p1) p2) = f
  cur_u : ∀ {A B C : Obj} (f : Hom (prod C A) B) (g : Hom C (exp B A)), comp ev (pair (comp g p1) p2) = f → g = cur f
  N : Obj
  zN : Hom one N
  sN : Hom N N
  nrec : {X : Obj} → Hom one X → Hom X X → Hom N X
  rec_z : ∀ {X : Obj} (x : Hom one X) (f : Hom X X), comp (nrec x f) zN = x
  rec_s : ∀ {X : Obj} (x : Hom one X) (f : Hom X X), comp (nrec x f) sN = comp f (nrec x f)
  rec_u : ∀ {X : Obj} (x : Hom one X) (f : Hom X X) (u : Hom N X), comp u zN = x → comp u sN = comp f u → u = nrec x f
  wp : ∀ {A B : Obj} (f g : Hom A B), (∀ x : Hom one A, comp f x = comp g x) → f = g
  nondeg : ∃ A : Obj, ∃ x y : Hom one A, x = y → False
  choice : ∀ {A B : Obj} (f : Hom A B), (∀ {C : Obj} (g h : Hom B C), comp g f = comp h f → g = h) →
    ∃ s : Hom B A, comp f s = id B

variable (E : ETCS)

namespace ETCS

def Mono {S A : E.Obj} (m : E.Hom S A) : Prop := ∀ {X : E.Obj} (f g : E.Hom X S), E.comp m f = E.comp m g → f = g

def Pt (A : E.Obj) : Type := E.Hom E.one A

theorem congrArg' {α β : Type} (f : α → β) {a b : α} (h : a = b) : f a = f b := h ▸ rfl

/-- composing with a pairing -/
theorem pair_comp {A B C D : E.Obj} (f : E.Hom C A) (g : E.Hom C B) (h : E.Hom D C) :
    E.comp (E.pair f g) h = E.pair (E.comp f h) (E.comp g h) := by
  have e := E.pair_u (E.comp (E.pair f g) h)
  rw [E.assoc, E.assoc, E.p1_pair, E.p2_pair] at e
  exact e

/-- pairs are equal when their components are -/
theorem pair_ext {A B C : E.Obj} {h h' : E.Hom C (E.prod A B)} (e1 : E.comp E.p1 h = E.comp E.p1 h')
    (e2 : E.comp E.p2 h = E.comp E.p2 h') : h = h' := by
  rw [E.pair_u h, E.pair_u h', e1, e2]

/-- the only point of 1 -/
theorem one_pt (x : E.Hom E.one E.one) : x = E.id E.one := (E.bang_u x).trans (E.bang_u (E.id E.one)).symm

theorem bang_comp {A B : E.Obj} (f : E.Hom A B) : E.comp (E.bang B) f = E.bang A := E.bang_u _

/-- injective on points gives mono -/
theorem mono_of_inj {S A : E.Obj} (m : E.Hom S A) (h : ∀ x y : E.Hom E.one S, E.comp m x = E.comp m y → x = y) :
    Mono E m := fun f g e => E.wp f g (fun x => h _ _ (by rw [E.assoc, E.assoc, e]))

/-- monos are injective on points -/
theorem inj_of_mono {S A : E.Obj} {m : E.Hom S A} (hm : Mono E m) (x y : E.Hom E.one S) (e : E.comp m x = E.comp m y) :
    x = y := hm x y e

/-- surjective on points gives epi -/
theorem epi_of_surj {A B : E.Obj} (f : E.Hom A B) (h : ∀ b : E.Hom E.one B, ∃ a, E.comp f a = b) :
    ∀ {C : E.Obj} (g k : E.Hom B C), E.comp g f = E.comp k f → g = k := fun g k e =>
  E.wp g k (fun b => match h b with
    | ⟨a, ha⟩ => by rw [← ha, E.assoc, E.assoc, e])

/-- surjections on points split -/
theorem split_of_surj {A B : E.Obj} (f : E.Hom A B) (h : ∀ b : E.Hom E.one B, ∃ a, E.comp f a = b) :
    ∃ s : E.Hom B A, E.comp f s = E.id B := E.choice f (epi_of_surj E f h)

/-- 0 has no points -/
theorem zero_no_pt (p : E.Hom E.one E.zero) : False :=
  match E.nondeg with
  | ⟨A, x, y, hne⟩ =>
    have hx : x = E.comp (E.ini A) p := by
      have e : E.comp x (E.bang E.zero) = E.ini A := E.ini_u _
      have e2 : E.comp (E.bang E.zero) p = E.id E.one := one_pt E _
      rw [← e, ← E.assoc, e2, E.id_r]
    have hy : y = E.comp (E.ini A) p := by
      have e : E.comp y (E.bang E.zero) = E.ini A := E.ini_u _
      have e2 : E.comp (E.bang E.zero) p = E.id E.one := one_pt E _
      rw [← e, ← E.assoc, e2, E.id_r]
    hne (hx.trans hy.symm)

/-- an object without points has at most one arrow to anything -/
theorem no_pt_eq {A B : E.Obj} (h : E.Hom E.one A → False) (f g : E.Hom A B) : f = g :=
  E.wp f g (fun x => (h x).elim)

/-- points of a product are pairs -/
theorem pt_pair {A B : E.Obj} (x : E.Hom E.one (E.prod A B)) : x = E.pair (E.comp E.p1 x) (E.comp E.p2 x) := E.pair_u x

end ETCS

end ETB

end Found

#print axioms Found.ETB.ETCS.pair_comp
#print axioms Found.ETB.ETCS.pair_ext
#print axioms Found.ETB.ETCS.one_pt
#print axioms Found.ETB.ETCS.bang_comp
#print axioms Found.ETB.ETCS.mono_of_inj
#print axioms Found.ETB.ETCS.inj_of_mono
#print axioms Found.ETB.ETCS.epi_of_surj
#print axioms Found.ETB.ETCS.split_of_surj
#print axioms Found.ETB.ETCS.zero_no_pt
#print axioms Found.ETB.ETCS.no_pt_eq
#print axioms Found.ETB.ETCS.pt_pair

found/EtB10.lean

lean_all/found/EtB10.lean

/-
  T209 (Lawvere–Mitchell), direction B, part 10. found/EtB10 — Δ₀-separation
  for candidate sets of an ETCS model. Bounded formulas (KP.D0) are read on
  environments of nodes (bounded quantifiers range over members); the
  reading respects equivalence. Each variable of a formula lives in a known
  graph (a bound variable in the graph of its bound), so the formula
  translates into the internal language, with membership and equality read
  through the largest bisimulations; the translation is correct, and
  collecting the members that satisfy it gives the separated set.

  Lean 4 core only, hand-written proofs, no axioms (classical logic only as
  the parameter K).
-/
import found.EtB9
import found.KP

namespace Found

namespace ETB

namespace ETCS

variable (E : ETCS)

def consN (d : Nd E) (ρ : Nat → Nd E) : Nat → Nd E
  | 0 => d
  | n + 1 => ρ n

/-- bounded formulas read on nodes -/
def satN : KP.D0 → (Nat → Nd E) → Prop
  | .mem i j, ρ => MemN E (ρ i) (ρ j)
  | .eq i j, ρ => Eqv E (ρ i) (ρ j)
  | .bot, _ => False
  | .and a b, ρ => satN a ρ ∧ satN b ρ
  | .or a b, ρ => satN a ρ ∨ satN b ρ
  | .imp a b, ρ => satN a ρ → satN b ρ
  | .ball n a, ρ => ∀ d, MemN E d (ρ n) → satN a (consN E d ρ)
  | .bex n a, ρ => ∃ d, MemN E d (ρ n) ∧ satN a (consN E d ρ)

theorem eqv_r (K : CP.Classical') {a b c : Nd E} (h1 : Eqv E a b) (h2 : Eqv E c b) : Eqv E a c :=
  eqv_trans E K h1 (eqv_symm E K h2)

/-- the reading respects equivalence of environments -/
theorem satN_resp (K : CP.Classical') : (φ : KP.D0) → {ρ σ : Nat → Nd E} → (∀ n, Eqv E (ρ n) (σ n)) →
    (satN E φ ρ ↔ satN E φ σ)
  | .mem i j, _, _, h => ⟨fun m => memN_r E K (h j) (memN_l E K (h i) m),
      fun m => memN_r E K (eqv_symm E K (h j)) (memN_l E K (eqv_symm E K (h i)) m)⟩
  | .eq i j, _, _, h => ⟨fun e => eqv_trans E K (eqv_symm E K (h i)) (eqv_trans E K e (h j)),
      fun e => eqv_trans E K (h i) (eqv_trans E K e (eqv_symm E K (h j)))⟩
  | .bot, _, _, _ => Iff.rfl
  | .and a b, _, _, h => ⟨fun ⟨x, y⟩ => ⟨(satN_resp K a h).1 x, (satN_resp K b h).1 y⟩,
      fun ⟨x, y⟩ => ⟨(satN_resp K a h).2 x, (satN_resp K b h).2 y⟩⟩
  | .or a b, _, _, h => ⟨fun o => o.elim (fun x => .inl ((satN_resp K a h).1 x)) (fun y => .inr ((satN_resp K b h).1 y)),
      fun o => o.elim (fun x => .inl ((satN_resp K a h).2 x)) (fun y => .inr ((satN_resp K b h).2 y))⟩
  | .imp a b, _, _, h => ⟨fun f x => (satN_resp K b h).1 (f ((satN_resp K a h).2 x)),
      fun f x => (satN_resp K b h).2 (f ((satN_resp K a h).1 x))⟩
  | .ball n a, ρ, σ, h =>
    have hc : ∀ d, ∀ k, Eqv E (consN E d ρ k) (consN E d σ k) := fun _ k => match k with
      | 0 => eqv_refl E K _ _
      | k + 1 => h k
    ⟨fun f d m => (satN_resp K a (hc d)).1 (f d (memN_r E K (eqv_symm E K (h n)) m)),
     fun f d m => (satN_resp K a (hc d)).2 (f d (memN_r E K (h n) m))⟩
  | .bex n a, ρ, σ, h =>
    have hc : ∀ d, ∀ k, Eqv E (consN E d ρ k) (consN E d σ k) := fun _ k => match k with
      | 0 => eqv_refl E K _ _
      | k + 1 => h k
    ⟨fun ⟨d, m, s⟩ => ⟨d, memN_r E K (h n) m, (satN_resp K a (hc d)).1 s⟩,
     fun ⟨d, m, s⟩ => ⟨d, memN_r E K (eqv_symm E K (h n)) m, (satN_resp K a (hc d)).2 s⟩⟩

theorem satN_cons (K : CP.Classical') (φ : KP.D0) {d d' : Nd E} (e : Eqv E d d') (ρ : Nat → Nd E)
    (h : satN E φ (consN E d ρ)) : satN E φ (consN E d' ρ) :=
  (satN_resp E K φ (ρ := consN E d ρ) (σ := consN E d' ρ) (fun k => match k with
    | 0 => e
    | _ + 1 => eqv_refl E K _ _)).1 h

/-! ## weakening of terms -/

def wk {Γ : List E.Obj} {B : E.Obj} : {A : E.Obj} → Tm E Γ A → Tm E (B :: Γ) A
  | _, .var i => .var (.s i)
  | _, .ap f t => .ap f (wk t)
  | _, .pr t u => .pr (wk t) (wk u)
  | _, .cst a => .cst a

theorem eval_wk {Γ : List E.Obj} {B : E.Obj} (b : E.Hom E.one B) (ρ : Env E Γ) :
    {A : E.Obj} → (t : Tm E Γ A) → eval E (wk E (B := B) t) (b, ρ) = eval E t ρ
  | _, .var _ => rfl
  | _, .ap f t => by
      show E.comp f (eval E (wk E t) (b, ρ)) = E.comp f (eval E t ρ)
      rw [eval_wk b ρ t]
  | _, .pr t u => by
      show E.pair (eval E (wk E t) (b, ρ)) (eval E (wk E u) (b, ρ)) = E.pair (eval E t ρ) (eval E u ρ)
      rw [eval_wk b ρ t, eval_wk b ρ u]
  | _, .cst _ => rfl

/-! ## the translation -/

/-- a variable: its graph and its term -/
structure Slot (Γ : List E.Obj) where
  G : Gr E
  t : Tm E Γ G.X

/-- the chosen largest bisimulation -/
def Lc (K : CP.Classical') (G H : Gr E) : E.Hom E.one (P E (E.prod G.X H.X)) :=
  K.ch (fun L => IsBis G H L ∧ ∀ x y, Mem E (E.pair x y) L ↔ EqvR G H x y) (largest E K G H)

theorem Lc_spec (K : CP.Classical') (G H : Gr E) (x : E.Hom E.one G.X) (y : E.Hom E.one H.X) :
    Mem E (E.pair x y) (Lc E K G H) ↔ EqvR G H x y :=
  (K.ch_spec (fun L => IsBis G H L ∧ ∀ x y, Mem E (E.pair x y) L ↔ EqvR G H x y) (largest E K G H)).2 x y

def liftS {Γ : List E.Obj} (G : Gr E) (σ : Nat → Slot E Γ) : Nat → Slot E (G.X :: Γ)
  | 0 => ⟨G, .var .z⟩
  | n + 1 => ⟨(σ n).G, wk E (σ n).t⟩

def tr (K : CP.Classical') : KP.D0 → {Γ : List E.Obj} → (Nat → Slot E Γ) → Fm E Γ
  | .mem i j, _, σ => .ex (σ j).G.X (.and (edgeT E (σ j).G (.var .z) (wk E (σ j).t))
      (memT E (.pr (wk E (σ i).t) (.var .z)) (.cst (Lc E K (σ i).G (σ j).G))))
  | .eq i j, _, σ => memT E (.pr (σ i).t (σ j).t) (.cst (Lc E K (σ i).G (σ j).G))
  | .bot, _, _ => .bot
  | .and a b, _, σ => .and (tr K a σ) (tr K b σ)
  | .or a b, _, σ => .or (tr K a σ) (tr K b σ)
  | .imp a b, _, σ => .imp (tr K a σ) (tr K b σ)
  | .ball n a, _, σ => .all (σ n).G.X (.imp (edgeT E (σ n).G (.var .z) (wk E (σ n).t)) (tr K a (liftS E (σ n).G σ)))
  | .bex n a, _, σ => .ex (σ n).G.X (.and (edgeT E (σ n).G (.var .z) (wk E (σ n).t)) (tr K a (liftS E (σ n).G σ)))

/-- the nodes named by the slots in an environment -/
def nodes {Γ : List E.Obj} (σ : Nat → Slot E Γ) (ρ : Env E Γ) : Nat → Nd E := fun i => ⟨(σ i).G, eval E (σ i).t ρ⟩

theorem nodes_lift (K : CP.Classical') {Γ : List E.Obj} (G : Gr E) (σ : Nat → Slot E Γ) (ρ : Env E Γ)
    (y : E.Hom E.one G.X) : ∀ k, Eqv E (nodes E (liftS E G σ) (y, ρ) k) (consN E ⟨G, y⟩ (nodes E σ ρ) k)
  | 0 => eqv_refl E K _ _
  | k + 1 => by
      show EqvR (σ k).G (σ k).G (eval E (wk E (σ k).t) (y, ρ)) (eval E (σ k).t ρ)
      rw [eval_wk]
      exact eqv_refl E K _ _

theorem tr_ok (K : CP.Classical') : (φ : KP.D0) → {Γ : List E.Obj} → (σ : Nat → Slot E Γ) → (ρ : Env E Γ) →
    (semE E (tr E K φ σ) ρ ↔ satN E φ (nodes E σ ρ))
  | .mem i j, _, σ, ρ => ⟨fun ⟨y, h1, h2⟩ => by
        have h1' : Edge E (σ j).G y (eval E (wk E (σ j).t) (y, ρ)) := h1
        have h2' : Mem E (E.pair (eval E (wk E (σ i).t) (y, ρ)) y) (Lc E K (σ i).G (σ j).G) := h2
        rw [eval_wk] at h1' h2'
        exact ⟨y, h1', (Lc_spec E K _ _ _ _).1 h2'⟩,
      fun ⟨y, h1, h2⟩ => by
        have h1' : Edge E (σ j).G y (eval E (wk E (σ j).t) (y, ρ)) := by rw [eval_wk]; exact h1
        have h2' : Mem E (E.pair (eval E (wk E (σ i).t) (y, ρ)) y) (Lc E K (σ i).G (σ j).G) := by
          rw [eval_wk]; exact (Lc_spec E K _ _ _ _).2 h2
        exact ⟨y, h1', h2'⟩⟩
  | .eq i j, _, σ, ρ => Lc_spec E K _ _ _ _
  | .bot, _, _, _ => Iff.rfl
  | .and a b, _, σ, ρ => ⟨fun ⟨x, y⟩ => ⟨(tr_ok K a σ ρ).1 x, (tr_ok K b σ ρ).1 y⟩,
      fun ⟨x, y⟩ => ⟨(tr_ok K a σ ρ).2 x, (tr_ok K b σ ρ).2 y⟩⟩
  | .or a b, _, σ, ρ => ⟨fun o => o.elim (fun x => .inl ((tr_ok K a σ ρ).1 x)) (fun y => .inr ((tr_ok K b σ ρ).1 y)),
      fun o => o.elim (fun x => .inl ((tr_ok K a σ ρ).2 x)) (fun y => .inr ((tr_ok K b σ ρ).2 y))⟩
  | .imp a b, _, σ, ρ => ⟨fun f x => (tr_ok K b σ ρ).1 (f ((tr_ok K a σ ρ).2 x)),
      fun f x => (tr_ok K b σ ρ).2 (f ((tr_ok K a σ ρ).1 x))⟩
  | .ball n a, _, σ, ρ =>
    ⟨fun f d ⟨y, hy, ed⟩ =>
      have hy' : Edge E (σ n).G y (eval E (wk E (σ n).t) (y, ρ)) := by rw [eval_wk]; exact hy
      have s := (tr_ok K a (liftS E (σ n).G σ) (y, ρ)).1 (f y hy')
      have s' := (satN_resp E K a (nodes_lift E K (σ n).G σ ρ y)).1 s
      satN_cons E K a (eqv_symm E K ed) _ s',
     fun f y hy =>
      have hy' : Edge E (σ n).G y (eval E (σ n).t ρ) := by
        have h : Edge E (σ n).G y (eval E (wk E (σ n).t) (y, ρ)) := hy
        rw [eval_wk] at h; exact h
      (tr_ok K a (liftS E (σ n).G σ) (y, ρ)).2 ((satN_resp E K a (nodes_lift E K (σ n).G σ ρ y)).2
        (f ⟨(σ n).G, y⟩ ⟨y, hy', eqv_refl E K _ _⟩))⟩
  | .bex n a, _, σ, ρ =>
    ⟨fun ⟨y, hy, s⟩ =>
      have hy' : Edge E (σ n).G y (eval E (σ n).t ρ) := by
        have h : Edge E (σ n).G y (eval E (wk E (σ n).t) (y, ρ)) := hy
        rw [eval_wk] at h; exact h
      ⟨⟨(σ n).G, y⟩, ⟨y, hy', eqv_refl E K _ _⟩,
        (satN_resp E K a (nodes_lift E K (σ n).G σ ρ y)).1 ((tr_ok K a (liftS E (σ n).G σ) (y, ρ)).1 s)⟩,
     fun ⟨d, ⟨y, hy, ed⟩, s⟩ =>
      have s' := satN_cons E K a ed _ s
      have hy' : Edge E (σ n).G y (eval E (wk E (σ n).t) (y, ρ)) := by rw [eval_wk]; exact hy
      ⟨y, hy', (tr_ok K a (liftS E (σ n).G σ) (y, ρ)).2 ((satN_resp E K a (nodes_lift E K (σ n).G σ ρ y)).2 s')⟩⟩

/-- Δ₀-separation -/
theorem separation (K : CP.Classical') (φ : KP.D0) (ρ : Nat → Nd E) (a : Nd E) :
    ∃ c : Nd E, (∀ d, MemN E d c ↔ MemN E d a ∧ satN E φ (consN E d ρ)) ∧ (WFG E a.G → WFG E c.G) := by
  let σ : Nat → Slot E [a.G.X] := fun i => match i with
    | 0 => ⟨a.G, .var .z⟩
    | k + 1 => ⟨(ρ k).G, .cst (ρ k).x⟩
  match comprE E K (Γ := []) (A := a.G.X) (.and (edgeT E a.G (.var .z) (.cst a.x)) (tr E K φ σ)) () with
  | ⟨S, hS⟩ =>
  match collect E K a.G S with
  | ⟨c, hc, wc⟩ =>
  have nd : ∀ y k, Eqv E (nodes E σ (y, ()) k) (consN E ⟨a.G, y⟩ ρ k) := fun y k => match k with
    | 0 => eqv_refl E K _ _
    | _ + 1 => eqv_refl E K _ _
  refine ⟨c, fun d => (hc d).trans ⟨fun ⟨y, hy, ed⟩ => ?_, fun ⟨⟨y, hy, ed⟩, s⟩ => ?_⟩, wc⟩
  · have h := (hS y).1 hy
    have s := (satN_resp E K φ (nd y)).1 ((tr_ok E K φ σ (y, ())).1 h.2)
    exact ⟨⟨y, h.1, ed⟩, satN_cons E K φ (eqv_symm E K ed) ρ s⟩
  · have s' := satN_cons E K φ ed ρ s
    exact ⟨y, (hS y).2 ⟨hy, (tr_ok E K φ σ (y, ())).2 ((satN_resp E K φ (nd y)).2 s')⟩, ed⟩

end ETCS

end ETB

end Found

#print axioms Found.ETB.ETCS.satN_resp
#print axioms Found.ETB.ETCS.satN_cons
#print axioms Found.ETB.ETCS.eval_wk
#print axioms Found.ETB.ETCS.Lc_spec
#print axioms Found.ETB.ETCS.nodes_lift
#print axioms Found.ETB.ETCS.tr_ok
#print axioms Found.ETB.ETCS.separation

found/EtB11.lean

lean_all/found/EtB11.lean

/-
  T209 (Lawvere–Mitchell), direction B, part 11. found/EtB11 — every model
  of ETCS gives a model of Mac Lane set theory without foundation and
  choice (extensionality, empty set, pairing, union, power set, infinity,
  Δ₀-separation): the candidate sets are the nodes of internal graphs,
  equality is bisimilarity and membership is "equivalent to a node below".

  Lean 4 core only, hand-written proofs, no axioms (classical logic only as
  the parameter K).
-/
import found.EtB10

namespace Found

namespace ETB

namespace ETCS

variable (E : ETCS)

theorem maclane_of_etcs (K : CP.Classical') :
    -- equality is an equivalence respected by membership
    (∀ a, Eqv E a a) ∧ (∀ a b, Eqv E a b → Eqv E b a) ∧ (∀ a b c, Eqv E a b → Eqv E b c → Eqv E a c) ∧
    (∀ a a' b b', Eqv E a a' → Eqv E b b' → MemN E a b → MemN E a' b') ∧
    -- extensionality
    (∀ a b, (∀ c, MemN E c a ↔ MemN E c b) → Eqv E a b) ∧
    -- empty set, pairing, union, power set
    (∃ c, ∀ d, MemN E d c → False) ∧
    (∀ a b, ∃ c, ∀ d, MemN E d c ↔ Eqv E d a ∨ Eqv E d b) ∧
    (∀ a, ∃ c, ∀ d, MemN E d c ↔ ∃ y, MemN E y a ∧ MemN E d y) ∧
    (∀ a, ∃ c, ∀ d, MemN E d c ↔ ∀ e, MemN E e d → MemN E e a) ∧
    -- infinity
    (∃ w, (∃ e, MemN E e w ∧ ∀ d, MemN E d e → False) ∧
      ∀ y, MemN E y w → ∃ z, MemN E z w ∧ ∀ d, MemN E d z ↔ MemN E d y ∨ Eqv E d y) ∧
    -- Δ₀-separation
    (∀ (φ : KP.D0) (ρ : Nat → Nd E) a, ∃ c, ∀ d, MemN E d c ↔ MemN E d a ∧ satN E φ (consN E d ρ)) :=
  ⟨fun a => eqv_refl E K a.G a.x, fun _ _ h => eqv_symm E K h, fun _ _ _ h1 h2 => eqv_trans E K h1 h2,
   fun _ _ _ _ ea eb m => memN_r E K eb (memN_l E K ea m),
   fun _ _ h => ext E K h,
   match empty E K with | ⟨c, h, _⟩ => ⟨c, h⟩,
   fun a b => match pairing E K a b with | ⟨c, h, _⟩ => ⟨c, h⟩,
   fun a => match union E K a with | ⟨c, h, _⟩ => ⟨c, h⟩,
   fun a => match power E K a with | ⟨c, h, _⟩ => ⟨c, h⟩,
   match infinity E K with | ⟨w, h, _⟩ => ⟨w, h⟩,
   fun φ ρ a => match separation E K φ ρ a with | ⟨c, h, _⟩ => ⟨c, h⟩⟩

end ETCS

end ETB

end Found

#print axioms Found.ETB.ETCS.maclane_of_etcs

found/EtB12.lean

lean_all/found/EtB12.lean

/-
  T209 (Lawvere–Mitchell), direction B, part 12. found/EtB12 — the axiom of
  choice for candidate sets of an ETCS model: a set a of nonempty, pairwise
  disjoint sets has a choice set. The members of a are replaced by their
  bisimilarity classes (points of P X); the projection from the object of
  pairs (class, element) onto the object of classes is onto on points, so
  it splits by the choice axiom of ETCS; the chosen elements are collected.

  Lean 4 core only, hand-written proofs, no axioms (classical logic only as
  the parameter K).
-/
import found.EtB11

namespace Found

namespace ETB

namespace ETCS

variable (E : ETCS)

theorem choice_model (K : CP.Classical') (a : Nd E) (hne : ∀ u, MemN E u a → ∃ z, MemN E z u)
    (hdis : ∀ u v, MemN E u a → MemN E v a → (∃ z, MemN E z u ∧ MemN E z v) → Eqv E u v) :
    ∃ c : Nd E, (∀ u, MemN E u a → ∃ z, MemN E z u ∧ MemN E z c ∧ ∀ z', MemN E z' u → MemN E z' c → Eqv E z' z) ∧
      (WFG E a.G → WFG E c.G) := by
  let G := a.G
  let X := G.X
  let L := Lc E K G G
  -- the class map
  match compr E (C := X) (A := X) (int_of_test E (C := E.prod X X) (φ := memT E (.pr (.ap E.p2 (.var .z)) (.ap E.p1 (.var .z))) (.cst L)) K) with
  | ⟨cls, hcls⟩ =>
  have clsm : ∀ y y', Mem E y' (E.comp cls y) ↔ EqvR G G y' y := fun y y' => (hcls y y').trans (by
    show E.comp E.ev (E.pair L (E.pair (E.comp E.p2 (E.pair y y')) (E.comp E.p1 (E.pair y y')))) = E.tru ↔ _
    rw [E.p1_pair, E.p2_pair]
    exact Lc_spec E K G G y' y)
  have cls_eq : ∀ y y', EqvR G G y y' → E.comp cls y = E.comp cls y' := fun y y' e =>
    pext E K (fun w => (clsm y w).trans ((Iff.intro (fun h => eqv_trans E K h e)
      (fun h => eqv_trans E K h (eqv_symm E K e))).trans (clsm y' w).symm))
  have cls_inv : ∀ y y', E.comp cls y = E.comp cls y' → EqvR G G y y' := fun y y' e =>
    (clsm y' y).1 (by rw [← e]; exact (clsm y y).2 (eqv_refl E K G y))
  -- the object of classes of members
  match sub_obj E (int_of_test E (C := P E X) (φ := .ex X (.and (edgeT E G (.var .z) (.cst a.x))
      (.eq (.var (.s .z)) (.ap cls (.var .z))))) K) with
  | ⟨SQ, mQ, mQm, hQ, fQ⟩ =>
  -- the object of pairs (class, element)
  match sub_obj E (int_of_test E (C := E.prod (P E X) X) (φ := .ex X (.and (edgeT E G (.var .z) (.cst a.x))
      (.and (.eq (.ap E.p1 (.var (.s .z))) (.ap cls (.var .z))) (edgeT E G (.ap E.p2 (.var (.s .z))) (.var .z))))) K) with
  | ⟨SD, mD, _, hD, _⟩ =>
  have dpt : ∀ d, ∃ y, Edge E G y a.x ∧ E.comp E.p1 (E.comp mD d) = E.comp cls y ∧
      Edge E G (E.comp E.p2 (E.comp mD d)) y := fun d => (hD _).2 ⟨d, rfl⟩
  -- the projection onto classes, onto on points, split
  match fQ (E.comp E.p1 mD) (fun d => match dpt d with
      | ⟨y, h1, h2, _⟩ => ⟨y, h1, by rw [← E.assoc]; exact h2⟩) with
  | ⟨π, hπ⟩ =>
  have onto : ∀ t : E.Hom E.one SQ, ∃ d, E.comp π d = t := by
    intro t
    match (hQ (E.comp mQ t)).2 ⟨t, rfl⟩ with
    | ⟨y, hy, ey⟩ =>
    match hne ⟨G, y⟩ ⟨y, hy, eqv_refl E K G y⟩ with
    | ⟨_, z, hz, _⟩ =>
    have hzD : semE E (Γ := [E.prod (P E X) X]) (.ex X (.and (edgeT E G (.var .z) (.cst a.x))
        (.and (.eq (.ap E.p1 (.var (.s .z))) (.ap cls (.var .z))) (edgeT E G (.ap E.p2 (.var (.s .z))) (.var .z)))))
        (E.pair (E.comp mQ t) z, ()) := by
      refine ⟨y, hy, ?_, ?_⟩
      · show E.comp E.p1 (E.pair (E.comp mQ t) z) = E.comp cls y
        rw [E.p1_pair]; exact ey
      · show E.comp G.R (E.pair (E.comp E.p2 (E.pair (E.comp mQ t) z)) y) = E.tru
        rw [E.p2_pair]; exact hz
    obtain ⟨d, hd⟩ := (hD _).1 hzD
    exact ⟨d, mQm _ _ (by rw [E.assoc, hπ, ← E.assoc, hd, E.p1_pair])⟩
  match split_of_surj E π onto with
  | ⟨sec, hsec⟩ =>
  have qt : ∀ t : E.Hom E.one SQ, E.comp mQ t = E.comp E.p1 (E.comp mD (E.comp sec t)) := fun t => by
    have : E.comp mQ t = E.comp mQ (E.comp (E.comp π sec) t) := by rw [hsec, E.id_l]
    rw [this, ← E.assoc π, E.assoc mQ, hπ, ← E.assoc]
  let ch : E.Hom SQ X := E.comp E.p2 (E.comp mD sec)
  have chp : ∀ t : E.Hom E.one SQ, ∃ y, Edge E G y a.x ∧ E.comp mQ t = E.comp cls y ∧ Edge E G (E.comp ch t) y := fun t =>
    match dpt (E.comp sec t) with
    | ⟨y, h1, h2, h3⟩ => ⟨y, h1, by rw [qt]; exact h2, by
        show Edge E G (E.comp (E.comp E.p2 (E.comp mD sec)) t) y
        rw [← E.assoc, ← E.assoc]; exact h3⟩
  -- the choice set
  match comprE E K (Γ := []) (A := X) (.ex SQ (.eq (.var (.s .z)) (.ap ch (.var .z)))) () with
  | ⟨S, hS⟩ =>
  match collect E K G S with
  | ⟨c, hc, wc⟩ =>
  refine ⟨c, fun u ⟨y0, hy0, eu⟩ => ?_, wc⟩
  match (hQ (E.comp cls y0)).1 ⟨y0, hy0, rfl⟩ with
  | ⟨t0, ht0⟩ =>
  match chp t0 with
  | ⟨y, _, e1, hz⟩ =>
  have eyy : EqvR G G y y0 := cls_inv y y0 (e1.symm.trans ht0)
  have z_in_y0 : MemN E ⟨G, E.comp ch t0⟩ ⟨G, y0⟩ := memN_r E K (b := ⟨G, y⟩) eyy ⟨_, hz, eqv_refl E K G _⟩
  refine ⟨⟨G, E.comp ch t0⟩, memN_r E K (eqv_symm E K eu) z_in_y0, (hc _).2 ⟨E.comp ch t0, (hS (E.comp ch t0)).2 ⟨t0, rfl⟩, eqv_refl E K G _⟩,
    fun z' hz'u hz'c => ?_⟩
  match (hc z').1 hz'c with
  | ⟨z1, hz1S, ez1⟩ =>
  match (hS z1).1 hz1S with
  | ⟨t1, e⟩ =>
  have e' : z1 = E.comp ch t1 := e
  match chp t1 with
  | ⟨y1, hy1, e2, hz1⟩ =>
  have z'y1 : MemN E z' ⟨G, y1⟩ := ⟨E.comp ch t1, hz1, by rw [← e']; exact ez1⟩
  have euv : Eqv E u ⟨G, y1⟩ := hdis u ⟨G, y1⟩ ⟨y0, hy0, eu⟩ ⟨y1, hy1, eqv_refl E K G y1⟩ ⟨z', hz'u, z'y1⟩
  have ey : EqvR G G y0 y1 := eqv_trans E K (eqv_symm E K eu) euv
  have et : t1 = t0 := mQm _ _ (by rw [e2, ht0]; exact (cls_eq y0 y1 ey).symm)
  have : Eqv E z' ⟨G, E.comp ch t1⟩ := by rw [← e']; exact ez1
  rw [et] at this
  exact this

end ETCS

end ETB

end Found

#print axioms Found.ETB.ETCS.choice_model

found/EtB13.lean

lean_all/found/EtB13.lean

/-
  T209 (Lawvere–Mitchell), direction B, part 13. found/EtB13 — every model
  of ETCS gives a model of bounded Zermelo set theory with choice (BZC,
  Mac Lane set theory): the candidate sets are the nodes of well-founded
  internal graphs, equality is bisimilarity and membership is "equivalent
  to a node below". Members of such nodes are again such nodes; all the
  constructions (empty set, pairing, union, power set, ω, Δ₀-separation,
  choice sets) stay among them, and foundation holds (a minimal node of the
  internal set of nodes equivalent to members).

  Lean 4 core only, hand-written proofs, no axioms (classical logic only as
  the parameter K).
-/
import found.EtB12

namespace Found

namespace ETB

namespace ETCS

variable (E : ETCS)

/-- foundation: a nonempty well-founded node has a member disjoint from it -/
theorem foundation (K : CP.Classical') (a : Nd E) (wa : WFG E a.G) (hne : ∃ u, MemN E u a) :
    ∃ m, MemN E m a ∧ ∀ d, MemN E d m → MemN E d a → False := by
  let G := a.G
  let L := Lc E K G G
  match comprE E K (Γ := []) (A := G.X) (.ex G.X (.and (edgeT E G (.var .z) (.cst a.x))
      (memT E (.pr (.var (.s .z)) (.var .z)) (.cst L)))) () with
  | ⟨S, hS⟩ =>
  have sm : ∀ y, Mem E y S ↔ ∃ y', Edge E G y' a.x ∧ EqvR G G y y' := fun y =>
    (hS y).trans ⟨fun ⟨y', h1, h2⟩ => ⟨y', h1, (Lc_spec E K G G y y').1 h2⟩,
      fun ⟨y', h1, h2⟩ => ⟨y', h1, (Lc_spec E K G G y y').2 h2⟩⟩
  match hne with
  | ⟨_, y0, hy0, _⟩ =>
  match wa S ⟨y0, (sm y0).2 ⟨y0, hy0, eqv_refl E K G y0⟩⟩ with
  | ⟨y, hy, hmin⟩ =>
  match (sm y).1 hy with
  | ⟨y', hy', eyy⟩ =>
  exact ⟨⟨G, y⟩, ⟨y', hy', eyy⟩, fun d ⟨z, hz, ed⟩ ⟨z', hz', ed'⟩ =>
    hmin z hz ((sm z).2 ⟨z', hz', eqv_trans E K (eqv_symm E K ed) ed'⟩)⟩

/-- the BZC (Mac Lane) model of an ETCS model, on well-founded nodes -/
theorem bzc_of_etcs (K : CP.Classical') :
    -- members of well-founded nodes are well-founded nodes
    (∀ a, WFG E a.G → ∀ d, MemN E d a → ∃ d', WFG E d'.G ∧ Eqv E d d') ∧
    -- equality is an equivalence respected by membership; extensionality
    (∀ a, Eqv E a a) ∧ (∀ a b, Eqv E a b → Eqv E b a) ∧ (∀ a b c, Eqv E a b → Eqv E b c → Eqv E a c) ∧
    (∀ a a' b b', Eqv E a a' → Eqv E b b' → MemN E a b → MemN E a' b') ∧
    (∀ a b, (∀ c, MemN E c a ↔ MemN E c b) → Eqv E a b) ∧
    -- empty set, pairing, union, power set
    (∃ c, WFG E c.G ∧ ∀ d, MemN E d c → False) ∧
    (∀ a b, WFG E a.G → WFG E b.G → ∃ c, WFG E c.G ∧ ∀ d, MemN E d c ↔ Eqv E d a ∨ Eqv E d b) ∧
    (∀ a, WFG E a.G → ∃ c, WFG E c.G ∧ ∀ d, MemN E d c ↔ ∃ y, MemN E y a ∧ MemN E d y) ∧
    (∀ a, WFG E a.G → ∃ c, WFG E c.G ∧ ∀ d, MemN E d c ↔ ∀ e, MemN E e d → MemN E e a) ∧
    -- infinity
    (∃ w, WFG E w.G ∧ (∃ e, MemN E e w ∧ ∀ d, MemN E d e → False) ∧
      ∀ y, MemN E y w → ∃ z, MemN E z w ∧ ∀ d, MemN E d z ↔ MemN E d y ∨ Eqv E d y) ∧
    -- Δ₀-separation
    (∀ (φ : KP.D0) (ρ : Nat → Nd E) a, WFG E a.G →
      ∃ c, WFG E c.G ∧ ∀ d, MemN E d c ↔ MemN E d a ∧ satN E φ (consN E d ρ)) ∧
    -- foundation
    (∀ a, WFG E a.G → (∃ u, MemN E u a) → ∃ m, MemN E m a ∧ ∀ d, MemN E d m → MemN E d a → False) ∧
    -- choice
    (∀ a, WFG E a.G → (∀ u, MemN E u a → ∃ z, MemN E z u) →
      (∀ u v, MemN E u a → MemN E v a → (∃ z, MemN E z u ∧ MemN E z v) → Eqv E u v) →
      ∃ c, WFG E c.G ∧ ∀ u, MemN E u a → ∃ z, MemN E z u ∧ MemN E z c ∧ ∀ z', MemN E z' u → MemN E z' c → Eqv E z' z) :=
  ⟨fun a wa _ ⟨y, _, ed⟩ => ⟨⟨a.G, y⟩, wa, ed⟩,
   fun a => eqv_refl E K a.G a.x, fun _ _ h => eqv_symm E K h, fun _ _ _ h1 h2 => eqv_trans E K h1 h2,
   fun _ _ _ _ ea eb m => memN_r E K eb (memN_l E K ea m),
   fun _ _ h => ext E K h,
   match empty E K with | ⟨c, h, w⟩ => ⟨c, w, h⟩,
   fun a b wa wb => match pairing E K a b with | ⟨c, h, w⟩ => ⟨c, w wa wb, h⟩,
   fun a wa => match union E K a with | ⟨c, h, w⟩ => ⟨c, w wa, h⟩,
   fun a wa => match power E K a with | ⟨c, h, w⟩ => ⟨c, w wa, h⟩,
   match infinity E K with | ⟨w, h, ww⟩ => ⟨w, ww, h⟩,
   fun φ ρ a wa => match separation E K φ ρ a with | ⟨c, h, w⟩ => ⟨c, w wa, h⟩,
   fun a wa hne => foundation E K a wa hne,
   fun a wa hne hdis => match choice_model E K a hne hdis with | ⟨c, h, w⟩ => ⟨c, w wa, h⟩⟩

end ETCS

end ETB

end Found

#print axioms Found.ETB.ETCS.foundation
#print axioms Found.ETB.ETCS.bzc_of_etcs

found/EtB2.lean

lean_all/found/EtB2.lean

/-
  T209 (Lawvere–Mitchell), direction B, part 2. found/EtB2 — internal
  predicates in an ETCS model: a predicate on the points of A is internal
  when some χ : A → Ω tests it (χ ∘ x = true). Internal predicates are the
  ones cut out by monos, they are stable under substitution, and they are
  closed under equality of arrows, ∧, ¬ (Ω has exactly the two points true
  and false, classically), ∨, →, ∀ and ∃ over a factor of a product.

  Lean 4 core only, hand-written proofs, no axioms (classical logic only as
  the parameter K).
-/
import found.EtB1
import found.ClassicalPoint

namespace Found

namespace ETB

namespace ETCS

variable (E : ETCS)

def Int {A : E.Obj} (Q : E.Hom E.one A → Prop) : Prop :=
  ∃ χ : E.Hom A E.Ω, ∀ x, Q x ↔ E.comp χ x = E.tru

theorem bang_one : E.bang E.one = E.id E.one := one_pt E _

theorem tru_one {B : E.Obj} (t : E.Hom E.one B) : E.comp t (E.bang E.one) = t := by
  rw [bang_one, E.id_r]

theorem tru_bang_pt {X : E.Obj} (x : E.Hom E.one X) : E.comp (E.comp E.tru (E.bang X)) x = E.tru := by
  rw [← E.assoc, bang_comp, tru_one]

theorem int_resp {A : E.Obj} {Q Q' : E.Hom E.one A → Prop} (h : ∀ x, Q x ↔ Q' x) (hQ : Int E Q) : Int E Q' :=
  match hQ with
  | ⟨χ, hχ⟩ => ⟨χ, fun x => (h x).symm.trans (hχ x)⟩

/-- the points through a mono form an internal predicate -/
theorem int_of_mono {S A : E.Obj} (m : E.Hom S A) (hm : Mono E m) : Int E (fun x => ∃ y, E.comp m y = x) :=
  ⟨E.chi m hm, fun x => ⟨fun ⟨y, hy⟩ => by rw [← hy, E.assoc, E.chi_comm m hm]; exact tru_bang_pt E y,
    fun h => E.chi_pb m hm x (by rw [h, tru_one])⟩⟩

/-- an internal predicate is cut out by a mono -/
theorem mono_of_int {A : E.Obj} {Q : E.Hom E.one A → Prop} (hQ : Int E Q) :
    ∃ (S : E.Obj) (m : E.Hom S A), Mono E m ∧ ∀ x, Q x ↔ ∃ y, E.comp m y = x :=
  match hQ with
  | ⟨χ, hχ⟩ => ⟨_, E.eqm χ (E.comp E.tru (E.bang A)), fun f g e => E.eqm_mono _ _ f g e, fun x =>
    ⟨fun hx => E.eq_lift _ _ x (by rw [tru_bang_pt E x]; exact (hχ x).1 hx),
     fun ⟨y, hy⟩ => (hχ x).2 (by
        rw [← hy, E.assoc, E.eq_comm, ← E.assoc, ← E.assoc, bang_comp, tru_one])⟩⟩

/-- substitution -/
theorem int_pre {A B : E.Obj} {Q : E.Hom E.one B → Prop} (hQ : Int E Q) (f : E.Hom A B) :
    Int E (fun x => Q (E.comp f x)) :=
  match hQ with
  | ⟨χ, hχ⟩ => ⟨E.comp χ f, fun x => (hχ _).trans (by rw [E.assoc])⟩

/-- equality of two arrows at a point -/
theorem int_eq {A B : E.Obj} (f g : E.Hom A B) : Int E (fun x => E.comp f x = E.comp g x) :=
  int_resp E (fun x => ⟨fun ⟨y, hy⟩ => by rw [← hy, E.assoc, E.assoc, E.eq_comm],
      fun h => E.eq_lift f g x h⟩)
    (int_of_mono E (E.eqm f g) (fun a b e => E.eqm_mono f g a b e))

theorem mono_from_one {A : E.Obj} (a : E.Hom E.one A) : Mono E a := fun f g _ => (E.bang_u f).trans (E.bang_u g).symm

/-- equality with a fixed point -/
theorem int_is {A : E.Obj} (a : E.Hom E.one A) : Int E (fun x => x = a) :=
  int_resp E (fun x => ⟨fun ⟨y, hy⟩ => by rw [← hy, one_pt E y, E.id_r], fun h => ⟨E.id E.one, by rw [E.id_r, h]⟩⟩)
    (int_of_mono E a (mono_from_one E a))

/-! ## the two truth values -/

theorem ini_mono (A : E.Obj) : Mono E (E.ini A) := fun f g _ =>
  no_pt_eq E (fun p => zero_no_pt E (E.comp f p)) f g

def fls : E.Hom E.one E.Ω := E.chi (E.ini E.one) (ini_mono E E.one)

theorem fls_ne : E.fls = E.tru → False := fun h =>
  match E.chi_pb (E.ini E.one) (ini_mono E E.one) (E.id E.one)
      (by show E.comp (fls E) (E.id E.one) = _; rw [h, E.id_r, tru_one]) with
  | ⟨y, _⟩ => zero_no_pt E y

/-- a truth value other than true is false -/
theorem eq_fls {ω : E.Hom E.one E.Ω} (h : ω = E.tru → False) : ω = fls E :=
  E.chi_u (E.ini E.one) (ini_mono E E.one) ω (E.wp _ _ (fun p => (zero_no_pt E p).elim))
    (fun {X} x hx =>
      have nopt : E.Hom E.one X → False := fun p => h (by
        have e : E.comp (E.comp ω x) p = E.comp (E.comp E.tru (E.bang X)) p := by rw [hx]
        rw [tru_bang_pt, ← E.assoc, one_pt E (E.comp x p), E.id_r] at e
        exact e)
      E.chi_pb (E.ini E.one) (ini_mono E E.one) x (no_pt_eq E nopt _ _))

theorem const_pt {X B : E.Obj} (t : E.Hom E.one B) (x : E.Hom E.one X) : E.comp (E.comp t (E.bang X)) x = t := by
  rw [← E.assoc, bang_comp, tru_one]

/-- the test of a single point -/
theorem chi_pt {A : E.Obj} (a w : E.Hom E.one A) : E.comp (E.chi a (mono_from_one E a)) w = E.tru ↔ w = a :=
  ⟨fun h => match E.chi_pb a (mono_from_one E a) w (by rw [h, tru_one]) with
    | ⟨y, hy⟩ => by rw [← hy, one_pt E y, E.id_r],
   fun h => by rw [h, E.chi_comm a (mono_from_one E a), tru_one]⟩

/-- Ω has the two points true and false (classically) -/
theorem two_valued (K : CP.Classical') (ω : E.Hom E.one E.Ω) : ω = E.tru ∨ ω = fls E :=
  (K.lem (ω = E.tru)).elim .inl (fun h => .inr (eq_fls E h))

theorem int_and {A : E.Obj} {Q1 Q2 : E.Hom E.one A → Prop} (h1 : Int E Q1) (h2 : Int E Q2) :
    Int E (fun x => Q1 x ∧ Q2 x) :=
  match h1, h2 with
  | ⟨χ1, e1⟩, ⟨χ2, e2⟩ =>
    ⟨E.comp (E.chi (E.pair E.tru E.tru) (mono_from_one E _)) (E.pair χ1 χ2), fun x => by
      show Q1 x ∧ Q2 x ↔ _
      rw [← E.assoc, pair_comp]
      refine (Iff.trans (b := E.comp χ1 x = E.tru ∧ E.comp χ2 x = E.tru) ⟨fun ⟨a, b⟩ => ⟨(e1 x).1 a, (e2 x).1 b⟩, fun ⟨a, b⟩ => ⟨(e1 x).2 a, (e2 x).2 b⟩⟩ ?_)
      refine Iff.trans ?_ (chi_pt E _ _).symm
      exact ⟨fun ⟨a, b⟩ => by rw [a, b], fun h => ⟨by rw [← E.p1_pair (E.comp χ1 x) (E.comp χ2 x), h, E.p1_pair],
        by rw [← E.p2_pair (E.comp χ1 x) (E.comp χ2 x), h, E.p2_pair]⟩⟩⟩

theorem int_not {A : E.Obj} {Q : E.Hom E.one A → Prop} (h : Int E Q) : Int E (fun x => ¬ Q x) :=
  match h with
  | ⟨χ, e⟩ => ⟨E.comp (E.chi (fls E) (mono_from_one E _)) χ, fun x => by
      rw [← E.assoc]
      refine Iff.trans ?_ (chi_pt E _ _).symm
      exact ⟨fun hn => eq_fls E (fun ht => hn ((e x).2 ht)), fun hf hq => fls_ne E (hf.symm.trans ((e x).1 hq))⟩⟩

theorem int_true (A : E.Obj) : Int E (fun (_ : E.Hom E.one A) => True) :=
  ⟨E.comp E.tru (E.bang A), fun x => ⟨fun _ => const_pt E _ x, fun _ => trivial⟩⟩

theorem int_false (A : E.Obj) : Int E (fun (_ : E.Hom E.one A) => False) :=
  int_resp E (fun _ => ⟨fun h => h trivial, fun h => h.elim⟩) (int_not E (int_true E A))

theorem int_or (K : CP.Classical') {A : E.Obj} {Q1 Q2 : E.Hom E.one A → Prop} (h1 : Int E Q1) (h2 : Int E Q2) :
    Int E (fun x => Q1 x ∨ Q2 x) :=
  int_resp E (fun x => ⟨fun hn => (K.lem (Q1 x)).elim .inl (fun n1 => (K.lem (Q2 x)).elim .inr (fun n2 => (hn ⟨n1, n2⟩).elim)),
      fun ho ⟨n1, n2⟩ => ho.elim n1 n2⟩)
    (int_not E (int_and E (int_not E h1) (int_not E h2)))

theorem int_imp (K : CP.Classical') {A : E.Obj} {Q1 Q2 : E.Hom E.one A → Prop} (h1 : Int E Q1) (h2 : Int E Q2) :
    Int E (fun x => Q1 x → Q2 x) :=
  int_resp E (fun x => ⟨fun hn q1 => (K.lem (Q2 x)).elim (fun h => h) (fun n2 => (hn ⟨q1, n2⟩).elim),
      fun hi ⟨q1, n2⟩ => n2 (hi q1)⟩)
    (int_not E (int_and E h1 (int_not E h2)))

theorem int_iff (K : CP.Classical') {A : E.Obj} {Q1 Q2 : E.Hom E.one A → Prop} (h1 : Int E Q1) (h2 : Int E Q2) :
    Int E (fun x => Q1 x ↔ Q2 x) :=
  int_resp E (fun _ => ⟨fun ⟨a, b⟩ => ⟨a, b⟩, fun ⟨a, b⟩ => ⟨a, b⟩⟩) (int_and E (int_imp E K h1 h2) (int_imp E K h2 h1))

/-- a fixed proposition, classically -/
theorem int_const (K : CP.Classical') (A : E.Obj) (P : Prop) : Int E (fun (_ : E.Hom E.one A) => P) :=
  (K.lem P).elim (fun hp => int_resp E (fun _ => ⟨fun _ => hp, fun _ => trivial⟩) (int_true E A))
    (fun hn => int_resp E (fun _ => ⟨fun h => h.elim, fun h => (hn h).elim⟩) (int_false E A))

/-! ## quantifiers -/

/-- naturality of currying -/
theorem cur_nat {C C' A B : E.Obj} (χ : E.Hom (E.prod C A) B) (c : E.Hom C' C) :
    E.comp (E.cur χ) c = E.cur (E.comp χ (E.pair (E.comp c E.p1) E.p2)) := by
  apply E.cur_u
  have h1 : E.comp χ (E.pair (E.comp c E.p1) E.p2) =
      E.comp (E.comp E.ev (E.pair (E.comp (E.cur χ) E.p1) E.p2)) (E.pair (E.comp c E.p1) E.p2) := by rw [E.ev_cur]
  rw [h1, ← E.assoc E.ev, pair_comp, E.p2_pair, ← E.assoc (E.cur χ) E.p1, E.p1_pair, ← E.assoc (E.cur χ) c E.p1]

theorem cur_inj {C A B : E.Obj} {g g' : E.Hom (E.prod C A) B} (h : E.cur g = E.cur g') : g = g' := by
  rw [← E.ev_cur g, ← E.ev_cur g', h]

/-- the point ⟨c, a⟩ -/
theorem pt1 {A : E.Obj} (w : E.Hom E.one (E.prod E.one A)) : w = E.pair (E.id E.one) (E.comp E.p2 w) := by
  rw [← one_pt E (E.comp E.p1 w)]; exact E.pair_u w

theorem int_all {C A : E.Obj} {Q : E.Hom E.one (E.prod C A) → Prop} (h : Int E Q) :
    Int E (fun c => ∀ a, Q (E.pair c a)) :=
  match h with
  | ⟨χ, e⟩ =>
    int_resp E (fun c => by
      show E.comp (E.cur χ) c = E.comp (E.comp (E.cur (E.comp E.tru (E.bang (E.prod E.one A)))) (E.bang C)) c ↔ _
      rw [const_pt, cur_nat]
      have kw : ∀ a : E.Hom E.one A, E.comp (E.pair (E.comp c E.p1) E.p2) (E.pair (E.id E.one) a) = E.pair c a := fun a => by
        rw [pair_comp, ← E.assoc, E.p1_pair, E.p2_pair, E.id_r]
      exact ⟨fun hc a => (e (E.pair c a)).2 (by
          have e3 : E.comp (E.comp χ (E.pair (E.comp c E.p1) E.p2)) (E.pair (E.id E.one) a) =
              E.comp (E.comp E.tru (E.bang (E.prod E.one A))) (E.pair (E.id E.one) a) := by rw [cur_inj E hc]
          rw [← E.assoc, kw, const_pt] at e3
          exact e3),
        fun ha => congrArg E.cur (E.wp _ _ (fun w => by
          rw [pt1 E w, ← E.assoc, kw, const_pt]
          exact (e (E.pair c (E.comp E.p2 w))).1 (ha (E.comp E.p2 w))))⟩)
      (int_eq E (E.cur χ) (E.comp (E.cur (E.comp E.tru (E.bang (E.prod E.one A)))) (E.bang C)))

theorem int_ex (K : CP.Classical') {C A : E.Obj} {Q : E.Hom E.one (E.prod C A) → Prop} (h : Int E Q) :
    Int E (fun c => ∃ a, Q (E.pair c a)) :=
  int_resp E (fun c => ⟨fun hn => (K.lem (∃ a, Q (E.pair c a))).elim (fun h => h) (fun hne => (hn (fun a q => hne ⟨a, q⟩)).elim),
      fun ⟨a, q⟩ hall => hall a q⟩)
    (int_not E (int_all E (int_not E h)))

end ETCS

end ETB

end Found

#print axioms Found.ETB.ETCS.bang_one
#print axioms Found.ETB.ETCS.tru_one
#print axioms Found.ETB.ETCS.tru_bang_pt
#print axioms Found.ETB.ETCS.int_resp
#print axioms Found.ETB.ETCS.int_of_mono
#print axioms Found.ETB.ETCS.mono_of_int
#print axioms Found.ETB.ETCS.int_pre
#print axioms Found.ETB.ETCS.int_eq
#print axioms Found.ETB.ETCS.mono_from_one
#print axioms Found.ETB.ETCS.int_is
#print axioms Found.ETB.ETCS.ini_mono
#print axioms Found.ETB.ETCS.fls_ne
#print axioms Found.ETB.ETCS.eq_fls
#print axioms Found.ETB.ETCS.const_pt
#print axioms Found.ETB.ETCS.chi_pt
#print axioms Found.ETB.ETCS.two_valued
#print axioms Found.ETB.ETCS.int_and
#print axioms Found.ETB.ETCS.int_not
#print axioms Found.ETB.ETCS.int_true
#print axioms Found.ETB.ETCS.int_false
#print axioms Found.ETB.ETCS.int_or
#print axioms Found.ETB.ETCS.int_imp
#print axioms Found.ETB.ETCS.int_iff
#print axioms Found.ETB.ETCS.int_const
#print axioms Found.ETB.ETCS.cur_nat
#print axioms Found.ETB.ETCS.cur_inj
#print axioms Found.ETB.ETCS.pt1
#print axioms Found.ETB.ETCS.int_all
#print axioms Found.ETB.ETCS.int_ex

found/EtB3.lean

lean_all/found/EtB3.lean

/-
  T209 (Lawvere–Mitchell), direction B, part 3. found/EtB3 — the internal
  language of an ETCS model and its power objects:
  * typed contexts (lists of objects, read as iterated products), variables,
    terms (variables, arrows applied to terms, pairs, constant points) and
    formulas (equations, truth of an Ω-term, ⊥, ∧, ∨, →, ∀ and ∃ over an
    object); every formula is an internal predicate of its context;
  * power objects P A = Ω^A: membership, comprehension (every internal
    predicate on C × A is a family C → P A), extensionality of their points.

  Lean 4 core only, hand-written proofs, no axioms (classical logic only as
  the parameter K).
-/
import found.EtB2

namespace Found

namespace ETB

namespace ETCS

variable (E : ETCS)

inductive Ix : List E.Obj → E.Obj → Type where
  | z {Γ : List E.Obj} {A : E.Obj} : Ix (A :: Γ) A
  | s {Γ : List E.Obj} {A B : E.Obj} : Ix Γ A → Ix (B :: Γ) A

def ctx : List E.Obj → E.Obj
  | [] => E.one
  | A :: Γ => E.prod (ctx Γ) A

def vden : {Γ : List E.Obj} → {A : E.Obj} → Ix E Γ A → E.Hom (ctx E Γ) A
  | _ :: _, _, .z => E.p2
  | _ :: _, _, .s i => E.comp (vden i) E.p1

inductive Tm (Γ : List E.Obj) : E.Obj → Type where
  | var {A : E.Obj} : Ix E Γ A → Tm Γ A
  | ap {A B : E.Obj} : E.Hom A B → Tm Γ A → Tm Γ B
  | pr {A B : E.Obj} : Tm Γ A → Tm Γ B → Tm Γ (E.prod A B)
  | cst {A : E.Obj} : E.Hom E.one A → Tm Γ A

def den {Γ : List E.Obj} : {A : E.Obj} → Tm E Γ A → E.Hom (ctx E Γ) A
  | _, .var i => vden E i
  | _, .ap f t => E.comp f (den t)
  | _, .pr t u => E.pair (den t) (den u)
  | _, .cst a => E.comp a (E.bang _)

inductive Fm : List E.Obj → Type where
  | eq {Γ : List E.Obj} {A : E.Obj} : Tm E Γ A → Tm E Γ A → Fm Γ
  | tr {Γ : List E.Obj} : Tm E Γ E.Ω → Fm Γ
  | bot {Γ : List E.Obj} : Fm Γ
  | and {Γ : List E.Obj} : Fm Γ → Fm Γ → Fm Γ
  | or {Γ : List E.Obj} : Fm Γ → Fm Γ → Fm Γ
  | imp {Γ : List E.Obj} : Fm Γ → Fm Γ → Fm Γ
  | all {Γ : List E.Obj} (A : E.Obj) : Fm (A :: Γ) → Fm Γ
  | ex {Γ : List E.Obj} (A : E.Obj) : Fm (A :: Γ) → Fm Γ

def sem : {Γ : List E.Obj} → Fm E Γ → E.Hom E.one (ctx E Γ) → Prop
  | _, .eq t u, e => E.comp (den E t) e = E.comp (den E u) e
  | _, .tr t, e => E.comp (den E t) e = E.tru
  | _, .bot, _ => False
  | _, .and φ ψ, e => sem φ e ∧ sem ψ e
  | _, .or φ ψ, e => sem φ e ∨ sem ψ e
  | _, .imp φ ψ, e => sem φ e → sem ψ e
  | _, .all A φ, e => ∀ a : E.Hom E.one A, sem φ (E.pair e a)
  | _, .ex A φ, e => ∃ a : E.Hom E.one A, sem φ (E.pair e a)

/-- every formula is internal -/
theorem int_sem (K : CP.Classical') : {Γ : List E.Obj} → (φ : Fm E Γ) → Int E (sem E φ)
  | _, .eq t u => int_eq E (den E t) (den E u)
  | _, .tr t => int_resp E (fun x => by
      show E.comp (den E t) x = E.comp (E.comp E.tru (E.bang _)) x ↔ _
      rw [const_pt]; exact Iff.rfl) (int_eq E (den E t) (E.comp E.tru (E.bang _)))
  | _, .bot => int_false E _
  | _, .and φ ψ => int_and E (int_sem K φ) (int_sem K ψ)
  | _, .or φ ψ => int_or E K (int_sem K φ) (int_sem K ψ)
  | _, .imp φ ψ => int_imp E K (int_sem K φ) (int_sem K ψ)
  | _, .all _ φ => int_all E (int_sem K φ)
  | _, .ex _ φ => int_ex E K (int_sem K φ)

/-! ## computing terms at a point -/

theorem vz_pt {Γ : List E.Obj} {A : E.Obj} (e : E.Hom E.one (ctx E Γ)) (a : E.Hom E.one A) :
    E.comp (vden E (Γ := A :: Γ) .z) (E.pair e a) = a := E.p2_pair e a

theorem vs_pt {Γ : List E.Obj} {A B : E.Obj} (i : Ix E Γ A) (e : E.Hom E.one (ctx E Γ)) (b : E.Hom E.one B) :
    E.comp (vden E (Γ := B :: Γ) (.s i)) (E.pair e b) = E.comp (vden E i) e := by
  show E.comp (E.comp (vden E i) E.p1) (E.pair e b) = _
  rw [← E.assoc, E.p1_pair]

theorem ap_pt {Γ : List E.Obj} {A B : E.Obj} (f : E.Hom A B) (t : Tm E Γ A) (e : E.Hom E.one (ctx E Γ)) :
    E.comp (den E (.ap f t)) e = E.comp f (E.comp (den E t) e) := (E.assoc _ _ _).symm

theorem pr_pt {Γ : List E.Obj} {A B : E.Obj} (t : Tm E Γ A) (u : Tm E Γ B) (e : E.Hom E.one (ctx E Γ)) :
    E.comp (den E (.pr t u)) e = E.pair (E.comp (den E t) e) (E.comp (den E u) e) := pair_comp E _ _ _

theorem cst_pt {Γ : List E.Obj} {A : E.Obj} (a : E.Hom E.one A) (e : E.Hom E.one (ctx E Γ)) :
    E.comp (den E (Γ := Γ) (.cst a)) e = a := const_pt E a e

theorem var_pt {Γ : List E.Obj} {A : E.Obj} (i : Ix E Γ A) (e : E.Hom E.one (ctx E Γ)) :
    E.comp (den E (.var i)) e = E.comp (vden E i) e := rfl

/-! ## power objects -/

abbrev P (A : E.Obj) : E.Obj := E.exp E.Ω A

/-- membership a ∈ s for points -/
def Mem {A : E.Obj} (a : E.Hom E.one A) (s : E.Hom E.one (P E A)) : Prop := E.comp E.ev (E.pair s a) = E.tru

theorem ev_cur_pt {C A B : E.Obj} (χ : E.Hom (E.prod C A) B) (c : E.Hom E.one C) (a : E.Hom E.one A) :
    E.comp E.ev (E.pair (E.comp (E.cur χ) c) a) = E.comp χ (E.pair c a) := by
  have h : E.comp χ (E.pair c a) = E.comp (E.comp E.ev (E.pair (E.comp (E.cur χ) E.p1) E.p2)) (E.pair c a) := by
    rw [E.ev_cur]
  rw [h, ← E.assoc, pair_comp, E.p2_pair, ← E.assoc (E.cur χ), E.p1_pair]

/-- comprehension: an internal family of subsets -/
theorem compr {C A : E.Obj} {Q : E.Hom E.one (E.prod C A) → Prop} (h : Int E Q) :
    ∃ f : E.Hom C (P E A), ∀ c a, Mem E a (E.comp f c) ↔ Q (E.pair c a) :=
  match h with
  | ⟨χ, e⟩ => ⟨E.cur χ, fun c a => by
      show E.comp E.ev (E.pair (E.comp (E.cur χ) c) a) = E.tru ↔ _
      rw [ev_cur_pt]
      exact (e _).symm⟩

/-- comprehension for a single subset -/
theorem compr1 {A : E.Obj} {Q : E.Hom E.one A → Prop} (h : Int E Q) : ∃ s : E.Hom E.one (P E A), ∀ a, Mem E a s ↔ Q a :=
  match compr E (Q := fun w => Q (E.comp E.p2 w)) (int_pre E h E.p2) with
  | ⟨f, hf⟩ => ⟨E.comp f (E.id E.one), fun a => (hf (E.id E.one) a).trans (by rw [E.p2_pair])⟩

/-- extensionality of points of a power object -/
theorem pext (K : CP.Classical') {A : E.Obj} {s t : E.Hom E.one (P E A)} (h : ∀ a, Mem E a s ↔ Mem E a t) : s = t := by
  have es : s = E.cur (E.comp E.ev (E.pair (E.comp s E.p1) E.p2)) := E.cur_u _ s rfl
  have et : t = E.cur (E.comp E.ev (E.pair (E.comp t E.p1) E.p2)) := E.cur_u _ t rfl
  rw [es, et]
  apply congrArg E.cur
  apply E.wp
  intro w
  rw [pt1 E w, ← E.assoc, ← E.assoc, pair_comp, pair_comp, ← E.assoc s E.p1, ← E.assoc t E.p1, E.p1_pair, E.p2_pair,
    E.id_r, E.id_r]
  have hs := two_valued E K (E.comp E.ev (E.pair s (E.comp E.p2 w)))
  have ht := two_valued E K (E.comp E.ev (E.pair t (E.comp E.p2 w)))
  have ha := h (E.comp E.p2 w)
  match hs, ht with
  | .inl a, .inl b => exact a.trans b.symm
  | .inr a, .inr b => exact a.trans b.symm
  | .inl a, .inr b => exact ((fls_ne E) (b.symm.trans (ha.1 a))).elim
  | .inr a, .inl b => exact ((fls_ne E) (a.symm.trans (ha.2 b))).elim

end ETCS

end ETB

end Found

#print axioms Found.ETB.ETCS.int_sem
#print axioms Found.ETB.ETCS.vz_pt
#print axioms Found.ETB.ETCS.vs_pt
#print axioms Found.ETB.ETCS.ap_pt
#print axioms Found.ETB.ETCS.pr_pt
#print axioms Found.ETB.ETCS.cst_pt
#print axioms Found.ETB.ETCS.var_pt
#print axioms Found.ETB.ETCS.ev_cur_pt
#print axioms Found.ETB.ETCS.compr
#print axioms Found.ETB.ETCS.compr1
#print axioms Found.ETB.ETCS.pext

found/EtB4.lean

lean_all/found/EtB4.lean

/-
  T209 (Lawvere–Mitchell), direction B, part 4. found/EtB4 — the internal
  language read on environments of points: an environment of a context is a
  list of points; terms evaluate to points and formulas to propositions by
  plain recursion (so concrete predicates unfold definitionally). The
  reading agrees with the arrow semantics at the tuple of the environment,
  hence every formula is internal on environments, and comprehension
  gives, for every formula and environment, the point of P A of its
  instances.

  Lean 4 core only, hand-written proofs, no axioms (classical logic only as
  the parameter K).
-/
import found.EtB3

namespace Found

namespace ETB

namespace ETCS

variable (E : ETCS)

def Env : List E.Obj → Type
  | [] => Unit
  | A :: Γ => E.Hom E.one A × Env Γ

def tup : {Γ : List E.Obj} → Env E Γ → E.Hom E.one (ctx E Γ)
  | [], _ => E.id E.one
  | _ :: _, ρ => E.pair (tup ρ.2) ρ.1

def get : {Γ : List E.Obj} → {A : E.Obj} → Ix E Γ A → Env E Γ → E.Hom E.one A
  | _ :: _, _, .z, ρ => ρ.1
  | _ :: _, _, .s i, ρ => get i ρ.2

def eval {Γ : List E.Obj} : {A : E.Obj} → Tm E Γ A → Env E Γ → E.Hom E.one A
  | _, .var i, ρ => get E i ρ
  | _, .ap f t, ρ => E.comp f (eval t ρ)
  | _, .pr t u, ρ => E.pair (eval t ρ) (eval u ρ)
  | _, .cst a, _ => a

def semE : {Γ : List E.Obj} → Fm E Γ → Env E Γ → Prop
  | _, .eq t u, ρ => eval E t ρ = eval E u ρ
  | _, .tr t, ρ => eval E t ρ = E.tru
  | _, .bot, _ => False
  | _, .and φ ψ, ρ => semE φ ρ ∧ semE ψ ρ
  | _, .or φ ψ, ρ => semE φ ρ ∨ semE ψ ρ
  | _, .imp φ ψ, ρ => semE φ ρ → semE ψ ρ
  | _, .all A φ, ρ => ∀ a : E.Hom E.one A, semE φ (a, ρ)
  | _, .ex A φ, ρ => ∃ a : E.Hom E.one A, semE φ (a, ρ)

theorem get_tup : {Γ : List E.Obj} → {A : E.Obj} → (i : Ix E Γ A) → (ρ : Env E Γ) →
    E.comp (vden E i) (tup E ρ) = get E i ρ
  | _ :: _, _, .z, ρ => E.p2_pair _ _
  | _ :: _, _, .s i, ρ => by
      show E.comp (E.comp (vden E i) E.p1) (E.pair (tup E ρ.2) ρ.1) = get E i ρ.2
      rw [← E.assoc, E.p1_pair]
      exact get_tup i ρ.2

theorem eval_tup {Γ : List E.Obj} : {A : E.Obj} → (t : Tm E Γ A) → (ρ : Env E Γ) →
    E.comp (den E t) (tup E ρ) = eval E t ρ
  | _, .var i, ρ => get_tup E i ρ
  | _, .ap f t, ρ => by
      show E.comp (E.comp f (den E t)) (tup E ρ) = E.comp f (eval E t ρ)
      rw [← E.assoc, eval_tup t ρ]
  | _, .pr t u, ρ => by
      show E.comp (E.pair (den E t) (den E u)) (tup E ρ) = E.pair (eval E t ρ) (eval E u ρ)
      rw [pair_comp, eval_tup t ρ, eval_tup u ρ]
  | _, .cst a, ρ => const_pt E a _

theorem semE_tup : {Γ : List E.Obj} → (φ : Fm E Γ) → (ρ : Env E Γ) → (semE E φ ρ ↔ sem E φ (tup E ρ))
  | _, .eq t u, ρ => by
      show eval E t ρ = eval E u ρ ↔ E.comp (den E t) (tup E ρ) = E.comp (den E u) (tup E ρ)
      rw [eval_tup, eval_tup]
  | _, .tr t, ρ => by
      show eval E t ρ = E.tru ↔ E.comp (den E t) (tup E ρ) = E.tru
      rw [eval_tup]
  | _, .bot, _ => Iff.rfl
  | _, .and φ ψ, ρ => ⟨fun ⟨a, b⟩ => ⟨(semE_tup φ ρ).1 a, (semE_tup ψ ρ).1 b⟩,
      fun ⟨a, b⟩ => ⟨(semE_tup φ ρ).2 a, (semE_tup ψ ρ).2 b⟩⟩
  | _, .or φ ψ, ρ => ⟨fun h => h.elim (fun a => .inl ((semE_tup φ ρ).1 a)) (fun b => .inr ((semE_tup ψ ρ).1 b)),
      fun h => h.elim (fun a => .inl ((semE_tup φ ρ).2 a)) (fun b => .inr ((semE_tup ψ ρ).2 b))⟩
  | _, .imp φ ψ, ρ => ⟨fun h a => (semE_tup ψ ρ).1 (h ((semE_tup φ ρ).2 a)),
      fun h a => (semE_tup ψ ρ).2 (h ((semE_tup φ ρ).1 a))⟩
  | _, .all _ φ, ρ => ⟨fun h a => (semE_tup φ (a, ρ)).1 (h a), fun h a => (semE_tup φ (a, ρ)).2 (h a)⟩
  | _, .ex _ φ, ρ => ⟨fun ⟨a, h⟩ => ⟨a, (semE_tup φ (a, ρ)).1 h⟩, fun ⟨a, h⟩ => ⟨a, (semE_tup φ (a, ρ)).2 h⟩⟩

/-- internal predicates of environments -/
def IntE {Γ : List E.Obj} (Q : Env E Γ → Prop) : Prop :=
  ∃ χ : E.Hom (ctx E Γ) E.Ω, ∀ ρ, Q ρ ↔ E.comp χ (tup E ρ) = E.tru

theorem int_semE (K : CP.Classical') {Γ : List E.Obj} (φ : Fm E Γ) : IntE E (semE E φ) :=
  match int_sem E K φ with
  | ⟨χ, e⟩ => ⟨χ, fun ρ => (semE_tup E φ ρ).trans (e _)⟩

/-- comprehension for a formula in an environment -/
theorem comprE (K : CP.Classical') {Γ : List E.Obj} {A : E.Obj} (φ : Fm E (A :: Γ)) (ρ : Env E Γ) :
    ∃ s : E.Hom E.one (P E A), ∀ a, Mem E a s ↔ semE E φ (a, ρ) :=
  match compr E (int_sem E K φ) with
  | ⟨f, hf⟩ => ⟨E.comp f (tup E ρ), fun a => (hf (tup E ρ) a).trans (semE_tup E φ (a, ρ)).symm⟩

end ETCS

end ETB

end Found

#print axioms Found.ETB.ETCS.get_tup
#print axioms Found.ETB.ETCS.eval_tup
#print axioms Found.ETB.ETCS.semE_tup
#print axioms Found.ETB.ETCS.int_semE
#print axioms Found.ETB.ETCS.comprE

found/EtB5.lean

lean_all/found/EtB5.lean

/-
  T209 (Lawvere–Mitchell), direction B, part 5. found/EtB5 — graphs in an
  ETCS model and bisimulation:
  * a graph is an object X with an internal edge relation R : X × X → Ω;
    a node of a graph is a candidate set; its members are the nodes below;
  * a bisimulation between two graphs is a point of P(X × Y) (internal),
    two nodes are equivalent when a bisimulation relates them;
  * equivalence is reflexive (diagonal), symmetric (transpose) and
    transitive (composite), membership respects it, the largest
    bisimulation is internal, and extensionality holds.

  Lean 4 core only, hand-written proofs, no axioms (classical logic only as
  the parameter K).
-/
import found.EtB4

namespace Found

namespace ETB

namespace ETCS

variable (E : ETCS)

structure Gr where
  X : E.Obj
  R : E.Hom (E.prod X X) E.Ω

def Edge (G : Gr E) (y x : E.Hom E.one G.X) : Prop := E.comp G.R (E.pair y x) = E.tru

def memT {Γ : List E.Obj} {A : E.Obj} (a : Tm E Γ A) (s : Tm E Γ (P E A)) : Fm E Γ := .tr (.ap E.ev (.pr s a))

def edgeT (G : Gr E) {Γ : List E.Obj} (a b : Tm E Γ G.X) : Fm E Γ := .tr (.ap G.R (.pr a b))

section B

variable {E} (G H : Gr E)

/-- B is a bisimulation (B = variable 0) -/
def bisF {Γ : List E.Obj} : Fm E (P E (E.prod G.X H.X) :: Γ) :=
  .all G.X (.all H.X (.imp (memT E (.pr (.var (.s .z)) (.var .z)) (.var (.s (.s .z))))
    (.and (.all G.X (.imp (edgeT E G (.var .z) (.var (.s (.s .z))))
        (.ex H.X (.and (edgeT E H (.var .z) (.var (.s (.s .z))))
          (memT E (.pr (.var (.s .z)) (.var .z)) (.var (.s (.s (.s (.s .z))))))))))
      (.all H.X (.imp (edgeT E H (.var .z) (.var (.s .z)))
        (.ex G.X (.and (edgeT E G (.var .z) (.var (.s (.s (.s .z)))))
          (memT E (.pr (.var .z) (.var (.s .z))) (.var (.s (.s (.s (.s .z)))))))))))))

def IsBis (B : E.Hom E.one (P E (E.prod G.X H.X))) : Prop :=
  ∀ x y, Mem E (E.pair x y) B →
    (∀ x', Edge E G x' x → ∃ y', Edge E H y' y ∧ Mem E (E.pair x' y') B) ∧
    (∀ y', Edge E H y' y → ∃ x', Edge E G x' x ∧ Mem E (E.pair x' y') B)

theorem bisF_iff {Γ : List E.Obj} (B : E.Hom E.one (P E (E.prod G.X H.X))) (ρ : Env E Γ) :
    semE E (bisF G H) (B, ρ) ↔ IsBis G H B := Iff.rfl

def EqvR (x : E.Hom E.one G.X) (y : E.Hom E.one H.X) : Prop :=
  ∃ B, IsBis G H B ∧ Mem E (E.pair x y) B

end B

theorem mem_pair_iff {A B : E.Obj} {Q : E.Hom E.one (E.prod A B) → Prop} {s : E.Hom E.one (P E (E.prod A B))}
    (h : ∀ w, Mem E w s ↔ Q w) (x : E.Hom E.one A) (y : E.Hom E.one B) : Mem E (E.pair x y) s ↔ Q (E.pair x y) := h _

/-- the diagonal is a bisimulation -/
theorem eqv_refl (K : CP.Classical') (G : Gr E) (x : E.Hom E.one G.X) : EqvR G G x x :=
  match comprE E K (Γ := []) (A := E.prod G.X G.X) (.eq (.ap E.p1 (.var .z)) (.ap E.p2 (.var .z))) () with
  | ⟨Δ, hΔ⟩ =>
    have dia : ∀ a b, Mem E (E.pair a b) Δ ↔ a = b := fun a b => (hΔ _).trans (by
      show E.comp E.p1 (E.pair a b) = E.comp E.p2 (E.pair a b) ↔ a = b
      rw [E.p1_pair, E.p2_pair])
    ⟨Δ, fun a b hab => have e := (dia a b).1 hab
      ⟨fun a' ha' => ⟨a', by rw [← e]; exact ha', (dia a' a').2 rfl⟩,
       fun b' hb' => ⟨b', by rw [e]; exact hb', (dia b' b').2 rfl⟩⟩, (dia x x).2 rfl⟩

/-- the transpose of a bisimulation -/
theorem eqv_symm (K : CP.Classical') {G H : Gr E} {x : E.Hom E.one G.X} {y : E.Hom E.one H.X} (h : EqvR G H x y) :
    EqvR H G y x :=
  match h with
  | ⟨B, hB, hxy⟩ =>
  match comprE E K (Γ := []) (A := E.prod H.X G.X) (memT E (.pr (.ap E.p2 (.var .z)) (.ap E.p1 (.var .z))) (.cst B)) () with
  | ⟨T, hT⟩ =>
    have tr : ∀ b a, Mem E (E.pair b a) T ↔ Mem E (E.pair a b) B := fun b a => (hT _).trans (by
      show E.comp E.ev (E.pair B (E.pair (E.comp E.p2 (E.pair b a)) (E.comp E.p1 (E.pair b a)))) = E.tru ↔ _
      rw [E.p1_pair, E.p2_pair]; exact Iff.rfl)
    ⟨T, fun b a hba => match hB a b ((tr b a).1 hba) with
      | ⟨f, g⟩ => ⟨fun b' hb' => match g b' hb' with
          | ⟨a', ha', m⟩ => ⟨a', ha', (tr b' a').2 m⟩,
        fun a' ha' => match f a' ha' with
          | ⟨b', hb', m⟩ => ⟨b', hb', (tr b' a').2 m⟩⟩, (tr y x).2 hxy⟩

/-- the composite of two bisimulations -/
theorem eqv_trans (K : CP.Classical') {G H L : Gr E} {x : E.Hom E.one G.X} {y : E.Hom E.one H.X} {z : E.Hom E.one L.X}
    (h1 : EqvR G H x y) (h2 : EqvR H L y z) : EqvR G L x z :=
  match h1, h2 with
  | ⟨B1, hB1, m1⟩, ⟨B2, hB2, m2⟩ =>
  match comprE E K (Γ := []) (A := E.prod G.X L.X) (.ex H.X (.and
      (memT E (.pr (.ap E.p1 (.var (.s .z))) (.var .z)) (.cst B1))
      (memT E (.pr (.var .z) (.ap E.p2 (.var (.s .z)))) (.cst B2)))) () with
  | ⟨C, hC⟩ =>
    have cm : ∀ a c, Mem E (E.pair a c) C ↔ ∃ b, Mem E (E.pair a b) B1 ∧ Mem E (E.pair b c) B2 := fun a c =>
      (hC _).trans (by
        show (∃ b, E.comp E.ev (E.pair B1 (E.pair (E.comp E.p1 (E.pair a c)) b)) = E.tru ∧
          E.comp E.ev (E.pair B2 (E.pair b (E.comp E.p2 (E.pair a c)))) = E.tru) ↔ _
        rw [E.p1_pair, E.p2_pair]; exact Iff.rfl)
    ⟨C, fun a c hac => match (cm a c).1 hac with
      | ⟨b, mab, mbc⟩ => match hB1 a b mab, hB2 b c mbc with
        | ⟨f1, g1⟩, ⟨f2, g2⟩ =>
          ⟨fun a' ha' => match f1 a' ha' with
            | ⟨b', hb', mab'⟩ => match f2 b' hb' with
              | ⟨c', hc', mbc'⟩ => ⟨c', hc', (cm a' c').2 ⟨b', mab', mbc'⟩⟩,
           fun c' hc' => match g2 c' hc' with
            | ⟨b', hb', mbc'⟩ => match g1 b' hb' with
              | ⟨a', ha', mab'⟩ => ⟨a', ha', (cm a' c').2 ⟨b', mab', mbc'⟩⟩⟩,
     (cm x z).2 ⟨y, m1, m2⟩⟩

/-- the largest bisimulation is internal and is a bisimulation -/
theorem largest (K : CP.Classical') (G H : Gr E) :
    ∃ L, IsBis G H L ∧ ∀ x y, Mem E (E.pair x y) L ↔ EqvR G H x y :=
  match comprE E K (Γ := []) (A := E.prod G.X H.X) (.ex (P E (E.prod G.X H.X)) (.and (bisF G H) (memT E (.var (.s .z)) (.var .z)))) () with
  | ⟨L, hL⟩ =>
    have lm : ∀ w, Mem E w L ↔ ∃ B, IsBis G H B ∧ Mem E w B := fun w => hL w
    ⟨L, fun x y hxy => match (lm _).1 hxy with
      | ⟨B, hB, m⟩ => match hB x y m with
        | ⟨f, g⟩ => ⟨fun x' hx' => match f x' hx' with
            | ⟨y', hy', m'⟩ => ⟨y', hy', (lm _).2 ⟨B, hB, m'⟩⟩,
          fun y' hy' => match g y' hy' with
            | ⟨x', hx', m'⟩ => ⟨x', hx', (lm _).2 ⟨B, hB, m'⟩⟩⟩,
     fun _ _ => lm _⟩

/-! ## nodes: the candidate sets -/

structure Nd where
  G : Gr E
  x : E.Hom E.one G.X

def Eqv (a b : Nd E) : Prop := EqvR a.G b.G a.x b.x

def MemN (a b : Nd E) : Prop := ∃ y, Edge E b.G y b.x ∧ Eqv E a ⟨b.G, y⟩

theorem memN_l (K : CP.Classical') {a a' b : Nd E} (e : Eqv E a a') (h : MemN E a b) : MemN E a' b :=
  match h with
  | ⟨y, hy, ha⟩ => ⟨y, hy, eqv_trans E K (eqv_symm E K e) ha⟩

theorem memN_r (K : CP.Classical') {a b b' : Nd E} (e : Eqv E b b') (h : MemN E a b) : MemN E a b' :=
  match h, e with
  | ⟨y, hy, ha⟩, ⟨B, hB, m⟩ => match (hB b.x b'.x m).1 y hy with
    | ⟨y', hy', m'⟩ => ⟨y', hy', eqv_trans E K ha ⟨B, hB, m'⟩⟩

/-- extensionality -/
theorem ext (K : CP.Classical') {a b : Nd E} (h : ∀ c, MemN E c a ↔ MemN E c b) : Eqv E a b :=
  match largest E K a.G b.G with
  | ⟨L, hL, hLm⟩ =>
  match comprE E K (Γ := []) (A := E.prod a.G.X b.G.X)
      (.or (memT E (.var .z) (.cst L)) (.eq (.var .z) (.cst (E.pair a.x b.x)))) () with
  | ⟨U, hU⟩ =>
    have um : ∀ w, Mem E w U ↔ Mem E w L ∨ w = E.pair a.x b.x := fun w => hU w
    ⟨U, fun x y hxy => (um _).1 hxy |>.elim
      (fun hl => match hL x y hl with
        | ⟨f, g⟩ => ⟨fun x' hx' => match f x' hx' with
            | ⟨y', hy', m⟩ => ⟨y', hy', (um _).2 (.inl m)⟩,
          fun y' hy' => match g y' hy' with
            | ⟨x', hx', m⟩ => ⟨x', hx', (um _).2 (.inl m)⟩⟩)
      (fun e =>
        have ex : x = a.x := by rw [← E.p1_pair x y, e, E.p1_pair]
        have ey : y = b.x := by rw [← E.p2_pair x y, e, E.p2_pair]
        ⟨fun x' hx' => match (h ⟨a.G, x'⟩).1 ⟨x', by rw [← ex]; exact hx', eqv_refl E K a.G x'⟩ with
          | ⟨y', hy', e'⟩ => ⟨y', by rw [ey]; exact hy', (um _).2 (.inl ((hLm x' y').2 e'))⟩,
         fun y' hy' => match (h ⟨b.G, y'⟩).2 ⟨y', by rw [← ey]; exact hy', eqv_refl E K b.G y'⟩ with
          | ⟨x', hx', e'⟩ => ⟨x', by rw [ex]; exact hx', (um _).2 (.inl ((hLm x' y').2 (eqv_symm E K e')))⟩⟩),
     (um _).2 (.inr rfl)⟩

end ETCS

end ETB

end Found

#print axioms Found.ETB.ETCS.bisF_iff
#print axioms Found.ETB.ETCS.eqv_refl
#print axioms Found.ETB.ETCS.eqv_symm
#print axioms Found.ETB.ETCS.eqv_trans
#print axioms Found.ETB.ETCS.largest
#print axioms Found.ETB.ETCS.memN_l
#print axioms Found.ETB.ETCS.memN_r
#print axioms Found.ETB.ETCS.ext

found/EtB6.lean

lean_all/found/EtB6.lean

/-
  T209 (Lawvere–Mitchell), direction B, part 6. found/EtB6 — points of a
  coproduct in an ETCS model: the injections never meet, each is injective
  on points, and every point of A + B comes from A or from B (the internal
  predicate "in the image of i1 or of i2" is true on both summands, so its
  test is the constant true by the uniqueness of copairing).

  Lean 4 core only, hand-written proofs, no axioms (classical logic only as
  the parameter K).
-/
import found.EtB5

namespace Found

namespace ETB

namespace ETCS

variable (E : ETCS)

theorem i1_ne_i2 {A B : E.Obj} (a : E.Hom E.one A) (b : E.Hom E.one B) (h : E.comp E.i1 a = E.comp E.i2 b) : False :=
  fls_ne E (by
    have e : E.comp (E.comp (E.copair (E.comp E.tru (E.bang A)) (E.comp (fls E) (E.bang B))) E.i1) a =
        E.comp (E.comp (E.copair (E.comp E.tru (E.bang A)) (E.comp (fls E) (E.bang B))) E.i2) b := by
      rw [← E.assoc, h, E.assoc]
    rw [E.copair_i1, E.copair_i2, const_pt, const_pt] at e
    exact e.symm)

theorem i1_inj {A B : E.Obj} (a a' : E.Hom E.one A) (h : E.comp (E.i1 (B := B)) a = E.comp E.i1 a') :
    a = a' := by
  have r : E.comp (E.copair (E.id A) (E.comp a (E.bang B))) (E.comp (E.i1 (B := B)) a) =
      E.comp (E.copair (E.id A) (E.comp a (E.bang B))) (E.comp E.i1 a') := by rw [h]
  rw [E.assoc, E.assoc, E.copair_i1, E.id_l, E.id_l] at r
  exact r

theorem i2_inj {A B : E.Obj} (b b' : E.Hom E.one B) (h : E.comp (E.i2 (A := A)) b = E.comp E.i2 b') :
    b = b' := by
  have r : E.comp (E.copair (E.comp b (E.bang A)) (E.id B)) (E.comp (E.i2 (A := A)) b) =
      E.comp (E.copair (E.comp b (E.bang A)) (E.id B)) (E.comp E.i2 b') := by rw [h]
  rw [E.assoc, E.assoc, E.copair_i2, E.id_l, E.id_l] at r
  exact r

/-- every point of A + B comes from A or from B -/
theorem cop_pt (K : CP.Classical') {A B : E.Obj} (p : E.Hom E.one (E.coprod A B)) :
    (∃ a, p = E.comp E.i1 a) ∨ (∃ b, p = E.comp E.i2 b) := by
  have hI : Int E (fun p : E.Hom E.one (E.coprod A B) => (∃ a, p = E.comp E.i1 a) ∨ (∃ b, p = E.comp E.i2 b)) :=
    int_resp E (fun p => ⟨fun h => h, fun h => h⟩) (int_semE E K (Γ := [E.coprod A B])
      (.or (.ex A (.eq (.var (.s .z)) (.ap E.i1 (.var .z)))) (.ex B (.eq (.var (.s .z)) (.ap E.i2 (.var .z)))))
      |> fun ⟨χ, e⟩ => ⟨E.comp χ (E.pair (E.bang _) (E.id _)), fun p => (e (p, ())).trans (by
          have h1 : E.comp (E.comp χ (E.pair (E.bang (E.coprod A B)) (E.id (E.coprod A B)))) p =
              E.comp χ (E.comp (E.pair (E.bang (E.coprod A B)) (E.id (E.coprod A B))) p) := (E.assoc _ _ _).symm
          have h2 : E.comp (E.pair (E.bang (E.coprod A B)) (E.id (E.coprod A B))) p = E.pair (E.id E.one) p := by
            rw [pair_comp, bang_comp, bang_one, E.id_l]
          have h3 : E.comp χ (E.comp (E.pair (E.bang (E.coprod A B)) (E.id (E.coprod A B))) p) =
              E.comp χ (E.pair (E.id E.one) p) := congrArg (fun k => E.comp χ k) h2
          show E.comp χ (E.pair (E.id E.one) p) = E.tru ↔ _
          rw [h1, h3])⟩)
  match hI with
  | ⟨χ, e⟩ =>
    have c1 : E.comp χ E.i1 = E.comp (E.comp E.tru (E.bang _)) (E.i1 (B := B)) := E.wp _ _ (fun a => by
      rw [← E.assoc, ← E.assoc, const_pt]
      exact (e _).1 (.inl ⟨a, rfl⟩))
    have c2 : E.comp χ E.i2 = E.comp (E.comp E.tru (E.bang _)) (E.i2 (A := A)) := E.wp _ _ (fun b => by
      rw [← E.assoc, ← E.assoc, const_pt]
      exact (e _).1 (.inr ⟨b, rfl⟩))
    have all : χ = E.comp E.tru (E.bang _) := by
      rw [E.copair_u χ, E.copair_u (E.comp E.tru (E.bang _)), c1, c2]
    exact (e p).2 (by rw [all, const_pt])

end ETCS

end ETB

end Found

#print axioms Found.ETB.ETCS.i1_ne_i2
#print axioms Found.ETB.ETCS.i1_inj
#print axioms Found.ETB.ETCS.i2_inj
#print axioms Found.ETB.ETCS.cop_pt

found/EtB7.lean

lean_all/found/EtB7.lean

/-
  T209 (Lawvere–Mitchell), direction B, part 7. found/EtB7 — building
  candidate sets in an ETCS model:
  * internal relations from formulas (edge relations of new graphs);
  * an arrow that preserves edges and reflects them below its image keeps
    nodes equivalent;
  * well-founded graphs; a graph covered by two well-founded graphs with
    the right shape of edges is well-founded;
  * collection: for a graph G and an internal subset S of its nodes there
    is a node whose members are exactly the nodes of S (new root on
    G + 1, with edges from S to the root); it keeps well-foundedness.

  Lean 4 core only, hand-written proofs, no axioms (classical logic only as
  the parameter K).
-/
import found.EtB6

namespace Found

namespace ETB

namespace ETCS

variable (E : ETCS)

theorem pair_inj {A B : E.Obj} {a a' : E.Hom E.one A} {b b' : E.Hom E.one B} (h : E.pair a b = E.pair a' b') :
    a = a' ∧ b = b' :=
  ⟨by rw [← E.p1_pair a b, h, E.p1_pair], by rw [← E.p2_pair a b, h, E.p2_pair]⟩

/-- a test arrow for a formula in one variable -/
theorem test1 (K : CP.Classical') {C : E.Obj} (φ : Fm E [C]) :
    ∃ χ : E.Hom C E.Ω, ∀ p, E.comp χ p = E.tru ↔ semE E φ (p, ()) :=
  match int_semE E K φ with
  | ⟨χ, e⟩ => ⟨E.comp χ (E.pair (E.bang C) (E.id C)), fun p => by
      have h1 : E.comp (E.comp χ (E.pair (E.bang C) (E.id C))) p = E.comp χ (E.comp (E.pair (E.bang C) (E.id C)) p) :=
        (E.assoc _ _ _).symm
      have h2 : E.comp (E.pair (E.bang C) (E.id C)) p = E.pair (E.id E.one) p := by
        rw [pair_comp, bang_comp, bang_one, E.id_l]
      have h3 : E.comp χ (E.comp (E.pair (E.bang C) (E.id C)) p) = E.comp χ (E.pair (E.id E.one) p) :=
        congrArg (fun k => E.comp χ k) h2
      rw [h1, h3]
      exact (e (p, ())).symm⟩

/-- the subobject of an internal predicate, with factorization -/
theorem sub_obj {A : E.Obj} {Q : E.Hom E.one A → Prop} (hQ : Int E Q) :
    ∃ (S : E.Obj) (m : E.Hom S A), Mono E m ∧ (∀ x, Q x ↔ ∃ y, E.comp m y = x) ∧
      ∀ {B : E.Obj} (g : E.Hom B A), (∀ b, Q (E.comp g b)) → ∃ k, E.comp m k = g :=
  match hQ with
  | ⟨χ, hχ⟩ => ⟨_, E.eqm χ (E.comp E.tru (E.bang A)), fun f g e => E.eqm_mono _ _ f g e, fun x =>
    ⟨fun hx => E.eq_lift _ _ x (by rw [tru_bang_pt E x]; exact (hχ x).1 hx),
     fun ⟨y, hy⟩ => (hχ x).2 (by
        rw [← hy, E.assoc, E.eq_comm, ← E.assoc, ← E.assoc, bang_comp, tru_one])⟩,
    fun g hg => E.eq_lift _ _ g (E.wp _ _ (fun b => by
      rw [← E.assoc, ← E.assoc, ← E.assoc, bang_comp, tru_one]
      exact (hχ _).1 (hg b)))⟩

theorem int_of_test {C : E.Obj} {φ : Fm E [C]} (K : CP.Classical') : Int E (fun p => semE E φ (p, ())) :=
  match test1 E K φ with
  | ⟨χ, h⟩ => ⟨χ, fun p => (h p).symm⟩

/-- an edge-preserving arrow reflecting edges below its image keeps nodes equivalent -/
theorem emb_eqv (K : CP.Classical') {G G' : Gr E} (ι : E.Hom G.X G'.X)
    (hp : ∀ y x, Edge E G y x → Edge E G' (E.comp ι y) (E.comp ι x))
    (hr : ∀ z x, Edge E G' z (E.comp ι x) → ∃ y, z = E.comp ι y ∧ Edge E G y x) (x : E.Hom E.one G.X) :
    EqvR G G' x (E.comp ι x) :=
  match comprE E K (Γ := []) (A := E.prod G.X G'.X) (.eq (.ap ι (.ap E.p1 (.var .z))) (.ap E.p2 (.var .z))) () with
  | ⟨B, hB⟩ =>
    have bm : ∀ a b, Mem E (E.pair a b) B ↔ E.comp ι a = b := fun a b => (hB _).trans (by
      show E.comp ι (E.comp E.p1 (E.pair a b)) = E.comp E.p2 (E.pair a b) ↔ _
      rw [E.p1_pair, E.p2_pair])
    ⟨B, fun a b hab =>
      have e := (bm a b).1 hab
      ⟨fun a' ha' => ⟨E.comp ι a', by rw [← e]; exact hp a' a ha', (bm _ _).2 rfl⟩,
       fun b' hb' => match hr b' a (by rw [e]; exact hb') with
        | ⟨y, hy, hya⟩ => ⟨y, hya, (bm _ _).2 hy.symm⟩⟩, (bm _ _).2 rfl⟩

/-- every nonempty internal subset has a minimal node -/
def WFG (G : Gr E) : Prop :=
  ∀ S : E.Hom E.one (P E G.X), (∃ y, Mem E y S) → ∃ y, Mem E y S ∧ ∀ z, Edge E G z y → Mem E z S → False

/-- a graph without edges -/
def flat (Y : E.Obj) : Gr E := ⟨Y, E.comp (fls E) (E.bang _)⟩

theorem flat_edge (Y : E.Obj) (y t : E.Hom E.one Y) : Edge E (flat E Y) y t → False := fun h =>
  fls_ne E (by
    have h' : E.comp (E.comp (fls E) (E.bang (E.prod Y Y))) (E.pair y t) = E.tru := h
    rw [const_pt] at h'; exact h')

theorem flat_wf (Y : E.Obj) : WFG E (flat E Y) := fun _ ⟨y, hy⟩ => ⟨y, hy, fun z hz _ => flat_edge E Y z y hz⟩

/-- well-foundedness of a graph covered by two well-founded graphs -/
theorem wf_transfer (K : CP.Classical') {G H G' : Gr E} (ι1 : E.Hom G.X G'.X) (ι2 : E.Hom H.X G'.X)
    (cover : ∀ p : E.Hom E.one G'.X, (∃ x, p = E.comp ι1 x) ∨ (∃ t, p = E.comp ι2 t))
    (h1 : ∀ (z : E.Hom E.one G'.X) (x : E.Hom E.one G.X), Edge E G' z (E.comp ι1 x) → ∃ y, z = E.comp ι1 y ∧ Edge E G y x)
    (h2 : ∀ (z : E.Hom E.one G'.X) (t : E.Hom E.one H.X), Edge E G' z (E.comp ι2 t) → (∃ y, z = E.comp ι1 y) ∨ (∃ y, z = E.comp ι2 y ∧ Edge E H y t))
    (wG : WFG E G) (wH : WFG E H) : WFG E G' := by
  intro S ⟨p0, hp0⟩
  match comprE E K (Γ := []) (A := G.X) (memT E (.ap ι1 (.var .z)) (.cst S)) () with
  | ⟨S1, hS1⟩ =>
  have s1 : ∀ x, Mem E x S1 ↔ Mem E (E.comp ι1 x) S := fun x => hS1 x
  refine (K.lem (∃ x, Mem E x S1)).elim (fun ne1 => ?_) (fun no1 => ?_)
  · match wG S1 ne1 with
    | ⟨x, hx, hmin⟩ =>
      exact ⟨E.comp ι1 x, (s1 x).1 hx, fun z hz hzS => match h1 z x hz with
        | ⟨y, ez, hyx⟩ => hmin y hyx ((s1 y).2 (by rw [← ez]; exact hzS))⟩
  · match comprE E K (Γ := []) (A := H.X) (memT E (.ap ι2 (.var .z)) (.cst S)) () with
    | ⟨S2, hS2⟩ =>
    have s2 : ∀ t, Mem E t S2 ↔ Mem E (E.comp ι2 t) S := fun t => hS2 t
    have ne2 : ∃ t, Mem E t S2 := (cover p0).elim
      (fun ⟨x, ex⟩ => (no1 ⟨x, (s1 x).2 (by rw [← ex]; exact hp0)⟩).elim)
      (fun ⟨t, et⟩ => ⟨t, (s2 t).2 (by rw [← et]; exact hp0)⟩)
    match wH S2 ne2 with
    | ⟨t, ht, hmin⟩ =>
      exact ⟨E.comp ι2 t, (s2 t).1 ht, fun z hz hzS => (h2 z t hz).elim
        (fun ⟨y, ey⟩ => no1 ⟨y, (s1 y).2 (by rw [← ey]; exact hzS)⟩)
        (fun ⟨y, ey, hyt⟩ => hmin y hyt ((s2 y).2 (by rw [← ey]; exact hzS)))⟩

/-- the root point of 1 -/
def star : E.Hom E.one E.one := E.id E.one

/-- collection: a node whose members are the nodes of an internal subset -/
theorem collect (K : CP.Classical') (G : Gr E) (S : E.Hom E.one (P E G.X)) :
    ∃ c : Nd E, (∀ d, MemN E d c ↔ ∃ y, Mem E y S ∧ Eqv E d ⟨G, y⟩) ∧ (WFG E G → WFG E c.G) := by
  let X' := E.coprod G.X E.one
  let φ : Fm E [E.prod X' X'] :=
    .or (.ex G.X (.ex G.X (.and (.eq (.var (.s (.s .z))) (.pr (.ap E.i1 (.var (.s .z))) (.ap E.i1 (.var .z))))
          (edgeT E G (.var (.s .z)) (.var .z)))))
      (.ex G.X (.and (.eq (.var (.s .z)) (.pr (.ap E.i1 (.var .z)) (.cst (E.comp E.i2 (star E)))))
          (memT E (.var .z) (.cst S))))
  match test1 E K φ with
  | ⟨χ, hχ⟩ =>
  let G' : Gr E := ⟨X', χ⟩
  have espec : ∀ p q, Edge E G' p q ↔ (∃ y x, E.pair p q = E.pair (E.comp E.i1 y) (E.comp E.i1 x) ∧ Edge E G y x) ∨
      (∃ y, E.pair p q = E.pair (E.comp E.i1 y) (E.comp E.i2 (star E)) ∧ Mem E y S) := fun p q => hχ _
  have e11 : ∀ y x, Edge E G' (E.comp E.i1 y) (E.comp E.i1 x) ↔ Edge E G y x := fun y x =>
    ⟨fun h => ((espec _ _).1 h).elim
      (fun ⟨y', x', e, hyx⟩ => by
        have e' := pair_inj E e
        rw [i1_inj E _ _ e'.1, i1_inj E _ _ e'.2]; exact hyx)
      (fun ⟨_, e, _⟩ => (i1_ne_i2 E _ _ (pair_inj E e).2).elim),
     fun h => (espec _ _).2 (.inl ⟨y, x, rfl, h⟩)⟩
  have e1r : ∀ z x, Edge E G' z (E.comp E.i1 x) → ∃ y, z = E.comp E.i1 y ∧ Edge E G y x := fun z x h =>
    ((espec _ _).1 h).elim
      (fun ⟨y', x', e, hyx⟩ =>
        have e' := pair_inj E e
        ⟨y', e'.1, by rw [i1_inj E _ _ e'.2]; exact hyx⟩)
      (fun ⟨_, e, _⟩ => (i1_ne_i2 E _ _ (pair_inj E e).2).elim)
  have e2 : ∀ z, Edge E G' z (E.comp E.i2 (star E)) ↔ ∃ y, z = E.comp E.i1 y ∧ Mem E y S := fun z =>
    ⟨fun h => ((espec _ _).1 h).elim
      (fun ⟨_, _, e, _⟩ => (i1_ne_i2 E _ _ (pair_inj E e).2.symm).elim)
      (fun ⟨y, e, hy⟩ => ⟨y, (pair_inj E e).1, hy⟩),
     fun ⟨y, ez, hy⟩ => (espec _ _).2 (.inr ⟨y, by rw [ez], hy⟩)⟩
  have emb : ∀ y, EqvR G G' y (E.comp E.i1 y) := fun y =>
    emb_eqv E K (G := G) (G' := G') (E.i1 (B := E.one)) (fun y x h => (e11 y x).2 h) e1r y
  refine ⟨⟨G', E.comp E.i2 (star E)⟩, fun d => ⟨fun ⟨z, hz, hd⟩ => ?_, fun ⟨y, hy, hd⟩ => ?_⟩,
    fun wG => wf_transfer E K (G := G) (H := flat E E.one) (G' := G') E.i1 E.i2 (fun p => cop_pt E K p) e1r
      (fun z t hz => .inl (match t, (show t = star E from one_pt E t) with
        | _, rfl => match (e2 z).1 hz with
          | ⟨y, ez, _⟩ => ⟨y, ez⟩)) wG (flat_wf E E.one)⟩
  · match (e2 z).1 hz with
    | ⟨y, ez, hy⟩ =>
      refine ⟨y, hy, eqv_trans E K hd ?_⟩
      have : EqvR G' G z y := by rw [ez]; exact eqv_symm E K (emb y)
      exact this
  · exact ⟨E.comp E.i1 y, (e2 _).2 ⟨y, rfl, hy⟩, eqv_trans E K hd (emb y)⟩

end ETCS

end ETB

end Found

#print axioms Found.ETB.ETCS.pair_inj
#print axioms Found.ETB.ETCS.test1
#print axioms Found.ETB.ETCS.sub_obj
#print axioms Found.ETB.ETCS.int_of_test
#print axioms Found.ETB.ETCS.emb_eqv
#print axioms Found.ETB.ETCS.flat_edge
#print axioms Found.ETB.ETCS.flat_wf
#print axioms Found.ETB.ETCS.wf_transfer
#print axioms Found.ETB.ETCS.collect

found/EtB8.lean

lean_all/found/EtB8.lean

/-
  T209 (Lawvere–Mitchell), direction B, part 8. found/EtB8 — set
  constructions on candidate sets of an ETCS model:
  * the sum of two graphs (both embedded);
  * the extension of a graph G by an object Y of new nodes whose members
    are given by an internal relation τ from G to Y;
  * pairing, union, the empty set and the power set (new nodes for all
    points of P X, then collected).

  Lean 4 core only, hand-written proofs, no axioms (classical logic only as
  the parameter K).
-/
import found.EtB7

namespace Found

namespace ETB

namespace ETCS

variable (E : ETCS)

/-- the sum of two graphs -/
theorem sumG (K : CP.Classical') (G H : Gr E) :
    ∃ (G3 : Gr E) (ι1 : E.Hom G.X G3.X) (ι2 : E.Hom H.X G3.X),
      (∀ x, EqvR G G3 x (E.comp ι1 x)) ∧ (∀ y, EqvR H G3 y (E.comp ι2 y)) ∧ (WFG E G → WFG E H → WFG E G3) := by
  let X3 := E.coprod G.X H.X
  let φ : Fm E [E.prod X3 X3] :=
    .or (.ex G.X (.ex G.X (.and (.eq (.var (.s (.s .z))) (.pr (.ap E.i1 (.var (.s .z))) (.ap E.i1 (.var .z))))
          (edgeT E G (.var (.s .z)) (.var .z)))))
      (.ex H.X (.ex H.X (.and (.eq (.var (.s (.s .z))) (.pr (.ap E.i2 (.var (.s .z))) (.ap E.i2 (.var .z))))
          (edgeT E H (.var (.s .z)) (.var .z)))))
  match test1 E K φ with
  | ⟨χ, hχ⟩ =>
  let G3 : Gr E := ⟨X3, χ⟩
  have espec : ∀ p q, Edge E G3 p q ↔ (∃ y x, E.pair p q = E.pair (E.comp E.i1 y) (E.comp E.i1 x) ∧ Edge E G y x) ∨
      (∃ y x, E.pair p q = E.pair (E.comp E.i2 y) (E.comp E.i2 x) ∧ Edge E H y x) := fun p q => hχ _
  have e1r : ∀ z x, Edge E G3 z (E.comp E.i1 x) → ∃ y, z = E.comp E.i1 y ∧ Edge E G y x := fun z x h =>
    ((espec _ _).1 h).elim
      (fun ⟨y', x', e, hyx⟩ => have e' := pair_inj E e
        ⟨y', e'.1, by rw [i1_inj E _ _ e'.2]; exact hyx⟩)
      (fun ⟨_, _, e, _⟩ => (i1_ne_i2 E _ _ (pair_inj E e).2).elim)
  have e2r : ∀ z x, Edge E G3 z (E.comp E.i2 x) → ∃ y, z = E.comp E.i2 y ∧ Edge E H y x := fun z x h =>
    ((espec _ _).1 h).elim
      (fun ⟨_, _, e, _⟩ => (i1_ne_i2 E _ _ (pair_inj E e).2.symm).elim)
      (fun ⟨y', x', e, hyx⟩ => have e' := pair_inj E e
        ⟨y', e'.1, by rw [i2_inj E _ _ e'.2]; exact hyx⟩)
  exact ⟨G3, E.i1, E.i2,
    fun x => emb_eqv E K (G := G) (G' := G3) (E.i1 (B := H.X)) (fun y x h => (espec _ _).2 (.inl ⟨y, x, rfl, h⟩)) e1r x,
    fun y => emb_eqv E K (G := H) (G' := G3) (E.i2 (A := G.X)) (fun y x h => (espec _ _).2 (.inr ⟨y, x, rfl, h⟩)) e2r y,
    fun wG wH => wf_transfer E K (G := G) (H := H) (G' := G3) E.i1 E.i2 (cop_pt E K) e1r (fun z t hz => .inr (e2r z t hz)) wG wH⟩

/-- extension of a graph by new nodes with members given by τ -/
theorem extG (K : CP.Classical') (G : Gr E) (Y : E.Obj) (τ : E.Hom (E.prod G.X Y) E.Ω) :
    ∃ (G' : Gr E) (ι1 : E.Hom G.X G'.X) (ι2 : E.Hom Y G'.X), (∀ x, EqvR G G' x (E.comp ι1 x)) ∧
      (∀ t d, MemN E d ⟨G', E.comp ι2 t⟩ ↔ ∃ y, E.comp τ (E.pair y t) = E.tru ∧ Eqv E d ⟨G, y⟩) ∧ (WFG E G → WFG E G') := by
  let X' := E.coprod G.X Y
  let φ : Fm E [E.prod X' X'] :=
    .or (.ex G.X (.ex G.X (.and (.eq (.var (.s (.s .z))) (.pr (.ap E.i1 (.var (.s .z))) (.ap E.i1 (.var .z))))
          (edgeT E G (.var (.s .z)) (.var .z)))))
      (.ex G.X (.ex Y (.and (.eq (.var (.s (.s .z))) (.pr (.ap E.i1 (.var (.s .z))) (.ap E.i2 (.var .z))))
          (.tr (.ap τ (.pr (.var (.s .z)) (.var .z)))))))
  match test1 E K φ with
  | ⟨χ, hχ⟩ =>
  let G' : Gr E := ⟨X', χ⟩
  have espec : ∀ p q, Edge E G' p q ↔ (∃ y x, E.pair p q = E.pair (E.comp E.i1 y) (E.comp E.i1 x) ∧ Edge E G y x) ∨
      (∃ y t, E.pair p q = E.pair (E.comp E.i1 y) (E.comp E.i2 t) ∧ E.comp τ (E.pair y t) = E.tru) := fun p q => hχ _
  have e1r : ∀ z x, Edge E G' z (E.comp E.i1 x) → ∃ y, z = E.comp E.i1 y ∧ Edge E G y x := fun z x h =>
    ((espec _ _).1 h).elim
      (fun ⟨y', x', e, hyx⟩ => have e' := pair_inj E e
        ⟨y', e'.1, by rw [i1_inj E _ _ e'.2]; exact hyx⟩)
      (fun ⟨_, _, e, _⟩ => (i1_ne_i2 E _ _ (pair_inj E e).2).elim)
  have e2 : ∀ z t, Edge E G' z (E.comp E.i2 t) ↔ ∃ y, z = E.comp E.i1 y ∧ E.comp τ (E.pair y t) = E.tru := fun z t =>
    ⟨fun h => ((espec _ _).1 h).elim
      (fun ⟨_, _, e, _⟩ => (i1_ne_i2 E _ _ (pair_inj E e).2.symm).elim)
      (fun ⟨y, t', e, hy⟩ => have e' := pair_inj E e
        ⟨y, e'.1, by rw [i2_inj E _ _ e'.2]; exact hy⟩),
     fun ⟨y, ez, hy⟩ => (espec _ _).2 (.inr ⟨y, t, by rw [ez], hy⟩)⟩
  have emb : ∀ y, EqvR G G' y (E.comp E.i1 y) := fun y =>
    emb_eqv E K (G := G) (G' := G') (E.i1 (B := Y)) (fun y x h => (espec _ _).2 (.inl ⟨y, x, rfl, h⟩)) e1r y
  refine ⟨G', E.i1, E.i2, emb, fun t d => ⟨fun ⟨z, hz, hd⟩ => ?_, fun ⟨y, hy, hd⟩ => ?_⟩,
    fun wG => wf_transfer E K (G := G) (H := flat E Y) (G' := G') E.i1 E.i2 (cop_pt E K) e1r
      (fun z t hz => .inl (match (e2 z t).1 hz with
        | ⟨y, ez, _⟩ => ⟨y, ez⟩)) wG (flat_wf E Y)⟩
  · match (e2 z t).1 hz with
    | ⟨y, ez, hy⟩ =>
      refine ⟨y, hy, eqv_trans E K hd ?_⟩
      have : EqvR G' G z y := by rw [ez]; exact eqv_symm E K (emb y)
      exact this
  · exact ⟨E.comp E.i1 y, (e2 _ t).2 ⟨y, rfl, hy⟩, eqv_trans E K hd (emb y)⟩

/-- a test arrow for a formula in two variables -/
theorem test2 (K : CP.Classical') {A B : E.Obj} (φ : Fm E [E.prod A B]) :
    ∃ τ : E.Hom (E.prod A B) E.Ω, ∀ a b, E.comp τ (E.pair a b) = E.tru ↔ semE E φ (E.pair a b, ()) :=
  match test1 E K φ with
  | ⟨τ, h⟩ => ⟨τ, fun _ _ => h _⟩

/-! ## the axioms -/

theorem pairing (K : CP.Classical') (a b : Nd E) :
    ∃ c : Nd E, (∀ d, MemN E d c ↔ Eqv E d a ∨ Eqv E d b) ∧ (WFG E a.G → WFG E b.G → WFG E c.G) :=
  match sumG E K a.G b.G with
  | ⟨G3, ι1, ι2, h1, h2, w3⟩ =>
  match comprE E K (Γ := []) (A := G3.X) (.or (.eq (.var .z) (.cst (E.comp ι1 a.x))) (.eq (.var .z) (.cst (E.comp ι2 b.x)))) () with
  | ⟨S, hS⟩ =>
  match collect E K G3 S with
  | ⟨c, hc, wc⟩ => ⟨c, fun d => (hc d).trans
      ⟨fun ⟨y, hy, hd⟩ => ((hS y).1 hy).elim
        (fun e => .inl (eqv_trans E K hd (by
          have e' : y = E.comp ι1 a.x := e
          have : EqvR G3 a.G y a.x := by rw [e']; exact eqv_symm E K (h1 a.x)
          exact this)))
        (fun e => .inr (eqv_trans E K hd (by
          have e' : y = E.comp ι2 b.x := e
          have : EqvR G3 b.G y b.x := by rw [e']; exact eqv_symm E K (h2 b.x)
          exact this))),
       fun h => h.elim
        (fun ha => ⟨E.comp ι1 a.x, (hS _).2 (.inl rfl), eqv_trans E K ha (h1 a.x)⟩)
        (fun hb => ⟨E.comp ι2 b.x, (hS _).2 (.inr rfl), eqv_trans E K hb (h2 b.x)⟩)⟩,
      fun wa wb => wc (w3 wa wb)⟩

theorem union (K : CP.Classical') (a : Nd E) :
    ∃ c : Nd E, (∀ d, MemN E d c ↔ ∃ y, MemN E y a ∧ MemN E d y) ∧ (WFG E a.G → WFG E c.G) :=
  match comprE E K (Γ := []) (A := a.G.X) (.ex a.G.X (.and (edgeT E a.G (.var (.s .z)) (.var .z))
      (edgeT E a.G (.var .z) (.cst a.x)))) () with
  | ⟨S, hS⟩ =>
  match collect E K a.G S with
  | ⟨c, hc, wc⟩ => ⟨c, fun d => (hc d).trans
      ⟨fun ⟨z, hz, hd⟩ => match (hS z).1 hz with
        | ⟨y, hzy, hya⟩ => ⟨⟨a.G, y⟩, ⟨y, hya, eqv_refl E K a.G y⟩, ⟨z, hzy, hd⟩⟩,
       fun ⟨_, hya, hdy⟩ => match hya with
        | ⟨y0, hy0, ey⟩ => match memN_r E K ey hdy with
          | ⟨z, hz, hd⟩ => ⟨z, (hS z).2 ⟨y0, hz, hy0⟩, hd⟩⟩, wc⟩

theorem empty (K : CP.Classical') : ∃ c : Nd E, (∀ d, MemN E d c → False) ∧ WFG E c.G :=
  let G0 : Gr E := flat E E.one
  match comprE E K (Γ := []) (A := G0.X) .bot () with
  | ⟨S, hS⟩ =>
  match collect E K G0 S with
  | ⟨c, hc, wc⟩ => ⟨c, fun d h => match (hc d).1 h with
    | ⟨y, hy, _⟩ => (hS y).1 hy, wc (flat_wf E E.one)⟩

/-- d ⊆ a -/
def Sub (d a : Nd E) : Prop := ∀ e, MemN E e d → MemN E e a

theorem power (K : CP.Classical') (a : Nd E) : ∃ c : Nd E, (∀ d, MemN E d c ↔ Sub E d a) ∧ (WFG E a.G → WFG E c.G) := by
  match test2 E K (A := a.G.X) (B := P E a.G.X) (.and (memT E (.ap E.p1 (.var .z)) (.ap E.p2 (.var .z)))
      (edgeT E a.G (.ap E.p1 (.var .z)) (.cst a.x))) with
  | ⟨τ, hτ⟩ =>
  match extG E K a.G (P E a.G.X) τ with
  | ⟨G', ι1, ι2, emb, mem2, wG'⟩ =>
  have m2 : ∀ s d, MemN E d ⟨G', E.comp ι2 s⟩ ↔ ∃ y, (Mem E y s ∧ Edge E a.G y a.x) ∧ Eqv E d ⟨a.G, y⟩ :=
    fun s d => (mem2 s d).trans ⟨fun ⟨y, hy, hd⟩ => ⟨y, by
        have h := (hτ y s).1 hy
        show Mem E y s ∧ Edge E a.G y a.x
        have h' : (E.comp E.ev (E.pair (E.comp E.p2 (E.pair y s)) (E.comp E.p1 (E.pair y s))) = E.tru ∧
          E.comp a.G.R (E.pair (E.comp E.p1 (E.pair y s)) a.x) = E.tru) := h
        rw [E.p1_pair, E.p2_pair] at h'
        exact h', hd⟩,
      fun ⟨y, hy, hd⟩ => ⟨y, (hτ y s).2 (by
        show E.comp E.ev (E.pair (E.comp E.p2 (E.pair y s)) (E.comp E.p1 (E.pair y s))) = E.tru ∧
          E.comp a.G.R (E.pair (E.comp E.p1 (E.pair y s)) a.x) = E.tru
        rw [E.p1_pair, E.p2_pair]
        exact hy), hd⟩⟩
  match comprE E K (Γ := []) (A := G'.X) (.ex (P E a.G.X) (.eq (.var (.s .z)) (.ap ι2 (.var .z)))) () with
  | ⟨S, hS⟩ =>
  match collect E K G' S with
  | ⟨c, hc, wc⟩ =>
  refine ⟨c, fun d => (hc d).trans ⟨fun ⟨p, hp, hd⟩ => ?_, fun hsub => ?_⟩, fun wa => wc (wG' wa)⟩
  · match (hS p).1 hp with
    | ⟨s, es⟩ =>
      intro e he
      have hd' : Eqv E d ⟨G', E.comp ι2 s⟩ := by
        have es' : p = E.comp ι2 s := es
        have : EqvR d.G G' d.x (E.comp ι2 s) := by rw [← es']; exact hd
        exact this
      match (m2 s e).1 (memN_r E K hd' he) with
      | ⟨y, ⟨_, hya⟩, he'⟩ => exact ⟨y, hya, he'⟩
  · match largest E K d.G a.G with
    | ⟨L, _, hLm⟩ =>
    match comprE E K (Γ := []) (A := a.G.X) (.and (edgeT E a.G (.var .z) (.cst a.x))
        (.ex d.G.X (.and (edgeT E d.G (.var .z) (.cst d.x)) (memT E (.pr (.var .z) (.var (.s .z))) (.cst L))))) () with
    | ⟨s, hs⟩ =>
    have sm : ∀ y, Mem E y s ↔ Edge E a.G y a.x ∧ ∃ z, Edge E d.G z d.x ∧ EqvR d.G a.G z y := fun y =>
      (hs y).trans ⟨fun ⟨h1, z, h2, h3⟩ => ⟨h1, z, h2, (hLm z y).1 h3⟩, fun ⟨h1, z, h2, h3⟩ => ⟨h1, z, h2, (hLm z y).2 h3⟩⟩
    refine ⟨E.comp ι2 s, (hS _).2 ⟨s, rfl⟩, ext E K (fun e => ⟨fun he => ?_, fun he => ?_⟩)⟩
    · match hsub e he, he with
      | ⟨y0, hy0, ey⟩, ⟨z, hz, ez⟩ =>
        exact (m2 s e).2 ⟨y0, ⟨(sm y0).2 ⟨hy0, z, hz, eqv_trans E K (eqv_symm E K ez) ey⟩, hy0⟩, ey⟩
    · match (m2 s e).1 he with
      | ⟨y, ⟨hys, _⟩, ey⟩ => match (sm y).1 hys with
        | ⟨_, z, hz, ezy⟩ => exact ⟨z, hz, eqv_trans E K ey (eqv_symm E K ezy)⟩

end ETCS

end ETB

end Found

#print axioms Found.ETB.ETCS.sumG
#print axioms Found.ETB.ETCS.extG
#print axioms Found.ETB.ETCS.test2
#print axioms Found.ETB.ETCS.pairing
#print axioms Found.ETB.ETCS.union
#print axioms Found.ETB.ETCS.empty
#print axioms Found.ETB.ETCS.power

found/EtB9.lean

lean_all/found/EtB9.lean

/-
  T209 (Lawvere–Mitchell), direction B, part 9. found/EtB9 — the axiom of
  infinity for candidate sets of an ETCS model: by recursion on the natural
  numbers object, π : N → P N with π(0) = ∅ and π(n + 1) = π(n) ∪ {n}
  (recursion on N × P N; its first component is the identity). The graph
  on N with m → n iff m ∈ π(n) gives the von Neumann numerals; collecting
  all of them gives ω, which contains ∅ and is closed under x ↦ x ∪ {x};
  by induction on N (for internal subsets) the numerals graph is
  well-founded.

  Lean 4 core only, hand-written proofs, no axioms (classical logic only as
  the parameter K).
-/
import found.EtB8

namespace Found

namespace ETB

namespace ETCS

variable (E : ETCS)

theorem pred_map (K : CP.Classical') : ∃ π : E.Hom E.N (P E E.N),
    (∀ m, Mem E m (E.comp π E.zN) → False) ∧
    ∀ n m, Mem E m (E.comp π (E.comp E.sN n)) ↔ Mem E m (E.comp π n) ∨ m = n := by
  match comprE E K (Γ := []) (A := E.N) .bot () with
  | ⟨e, he⟩ =>
  match test1 E K (C := E.prod (E.prod E.N (P E E.N)) E.N)
      (.or (memT E (.ap E.p2 (.var .z)) (.ap E.p2 (.ap E.p1 (.var .z)))) (.eq (.ap E.p2 (.var .z)) (.ap E.p1 (.ap E.p1 (.var .z))))) with
  | ⟨χ, hχ⟩ =>
  match compr E (C := E.prod E.N (P E E.N)) (A := E.N) ⟨χ, fun p => (hχ p).symm⟩ with
  | ⟨g, hg⟩ =>
  have gm : ∀ c m, Mem E m (E.comp g c) ↔ Mem E m (E.comp E.p2 c) ∨ m = E.comp E.p1 c := fun c m => (hg c m).trans (by
    show E.comp E.ev (E.pair (E.comp E.p2 (E.comp E.p1 (E.pair c m))) (E.comp E.p2 (E.pair c m))) = E.tru ∨
      E.comp E.p2 (E.pair c m) = E.comp E.p1 (E.comp E.p1 (E.pair c m)) ↔ _
    rw [E.p1_pair, E.p2_pair]; exact Iff.rfl)
  let f : E.Hom (E.prod E.N (P E E.N)) (E.prod E.N (P E E.N)) := E.pair (E.comp E.sN E.p1) g
  let u := E.nrec (E.pair E.zN e) f
  have p1u : E.comp E.p1 u = E.id E.N := by
    have a : E.comp E.p1 u = E.nrec E.zN E.sN := E.rec_u _ _ _
      (by rw [← E.assoc, E.rec_z, E.p1_pair])
      (by rw [← E.assoc, E.rec_s, E.assoc, E.p1_pair, ← E.assoc])
    have b : E.id E.N = E.nrec E.zN E.sN := E.rec_u _ _ _ (E.id_l _) (by rw [E.id_l, E.id_r])
    rw [a, b]
  have un : ∀ n : E.Hom E.one E.N, E.comp u n = E.pair n (E.comp (E.comp E.p2 u) n) := fun n => by
    rw [E.pair_u (E.comp u n), E.assoc E.p1 u n, p1u, E.id_l, E.assoc E.p2 u n]
  refine ⟨E.comp E.p2 u, fun m hm => (he m).1 (by
      have : E.comp (E.comp E.p2 u) E.zN = e := by rw [← E.assoc, E.rec_z, E.p2_pair]
      rw [this] at hm; exact hm), fun n m => ?_⟩
  have h : E.comp (E.comp E.p2 u) (E.comp E.sN n) = E.comp g (E.comp u n) := by
    rw [← E.assoc, E.assoc u, E.rec_s, ← E.assoc, E.assoc E.p2, E.p2_pair]
  rw [h]
  exact (gm _ m).trans (by rw [un n, E.p1_pair, E.p2_pair])

/-- the numerals graph -/
def omG (π : E.Hom E.N (P E E.N)) : Gr E := ⟨E.N, E.comp E.ev (E.pair (E.comp π E.p2) E.p1)⟩

theorem omG_edge (π : E.Hom E.N (P E E.N)) (m n : E.Hom E.one E.N) : Edge E (omG E π) m n ↔ Mem E m (E.comp π n) := by
  show E.comp (E.comp E.ev (E.pair (E.comp π E.p2) E.p1)) (E.pair m n) = E.tru ↔ _
  rw [← E.assoc, pair_comp, ← E.assoc, E.p2_pair, E.p1_pair]
  exact Iff.rfl

/-- induction on N -/
theorem nno_ind (K : CP.Classical') (T : E.Hom E.one (P E E.N)) (h0 : Mem E E.zN T)
    (hs : ∀ n, Mem E n T → Mem E (E.comp E.sN n) T) : ∀ n, Mem E n T := by
  match sub_obj E (Q := fun n => Mem E n T) (int_of_test E (C := E.N) (φ := memT E (.var .z) (.cst T)) K) with
  | ⟨S, m, mm, hm, fm⟩ =>
  match (hm E.zN).1 h0 with
  | ⟨z', hz'⟩ =>
  match fm (E.comp E.sN m) (fun b => by
      rw [← E.assoc]; exact hs _ ((hm _).2 ⟨b, rfl⟩)) with
  | ⟨s', hs'⟩ =>
  have hid : E.comp m (E.nrec z' s') = E.id E.N := by
    have a : E.comp m (E.nrec z' s') = E.nrec E.zN E.sN := E.rec_u _ _ _
      (by rw [← E.assoc, E.rec_z, hz'])
      (by rw [← E.assoc, E.rec_s, E.assoc, hs', ← E.assoc])
    have b : E.id E.N = E.nrec E.zN E.sN := E.rec_u _ _ _ (E.id_l _) (by rw [E.id_l, E.id_r])
    rw [a, b]
  intro n
  exact (hm n).2 ⟨E.comp (E.nrec z' s') n, by rw [E.assoc, hid, E.id_l]⟩

/-- the numerals graph is well-founded -/
theorem omG_wf (K : CP.Classical') (π : E.Hom E.N (P E E.N)) (h0 : ∀ m, Mem E m (E.comp π E.zN) → False)
    (hs : ∀ n m, Mem E m (E.comp π (E.comp E.sN n)) ↔ Mem E m (E.comp π n) ∨ m = n) : WFG E (omG E π) := by
  intro S ⟨y0, hy0⟩
  refine (K.lem (∃ y, Mem E y S ∧ ∀ z, Edge E (omG E π) z y → Mem E z S → False)).elim (fun h => h) (fun hno => ?_)
  match comprE E K (Γ := []) (A := E.N) (.all E.N (.imp (memT E (.var .z) (.ap π (.var (.s .z))))
      (.imp (memT E (.var .z) (.cst S)) .bot))) () with
  | ⟨T, hT⟩ =>
  have tm : ∀ n, Mem E n T ↔ ∀ m, Mem E m (E.comp π n) → Mem E m S → False := fun n => hT n
  have all := nno_ind E K T ((tm _).2 (fun m hm _ => h0 m hm)) (fun n hn => (tm _).2 (fun m hm hmS =>
    ((hs n m).1 hm).elim (fun h => (tm n).1 hn m h hmS)
      (fun e => hno ⟨n, by rw [← e]; exact hmS, fun z hz hzS => (tm n).1 hn z ((omG_edge E π z n).1 hz) hzS⟩)))
  exact ((tm _).1 (all (E.comp E.sN y0)) y0 ((hs y0 y0).2 (.inr rfl)) hy0).elim

theorem infinity (K : CP.Classical') : ∃ w : Nd E,
    ((∃ e, MemN E e w ∧ ∀ d, MemN E d e → False) ∧
    ∀ y, MemN E y w → ∃ z, MemN E z w ∧ ∀ d, MemN E d z ↔ MemN E d y ∨ Eqv E d y) ∧ WFG E w.G := by
  match pred_map E K with
  | ⟨π, h0, hs⟩ =>
  match comprE E K (Γ := []) (A := E.N) (.eq (.var .z) (.var .z)) () with
  | ⟨S, hS⟩ =>
  match collect E K (omG E π) S with
  | ⟨w, hw, ww⟩ =>
  have inw : ∀ n, MemN E ⟨omG E π, n⟩ w := fun n => (hw _).2 ⟨n, (hS n).2 rfl, eqv_refl E K _ n⟩
  refine ⟨w, ⟨⟨⟨omG E π, E.zN⟩, inw _, fun d ⟨m, hm, _⟩ => h0 m ((omG_edge E π m _).1 hm)⟩, fun y hy => ?_⟩,
    ww (omG_wf E K π h0 hs)⟩
  match (hw y).1 hy with
  | ⟨n, _, ey⟩ =>
  refine ⟨⟨omG E π, E.comp E.sN n⟩, inw _, fun d => ⟨fun ⟨m, hm, ed⟩ => ?_, fun h => ?_⟩⟩
  · match (hs n m).1 ((omG_edge E π m _).1 hm) with
    | .inl hmn => exact .inl (memN_r E K (eqv_symm E K ey) ⟨m, (omG_edge E π m n).2 hmn, ed⟩)
    | .inr e => exact .inr (eqv_trans E K ed (by rw [e]; exact eqv_symm E K ey))
  · match h with
    | .inl hd => match memN_r E K ey hd with
      | ⟨m, hm, ed⟩ => exact ⟨m, (omG_edge E π m _).2 ((hs n m).2 (.inl ((omG_edge E π m n).1 hm))), ed⟩
    | .inr hd => exact ⟨n, (omG_edge E π n _).2 ((hs n n).2 (.inr rfl)), eqv_trans E K hd ey⟩

end ETCS

end ETB

end Found

#print axioms Found.ETB.ETCS.pred_map
#print axioms Found.ETB.ETCS.omG_edge
#print axioms Found.ETB.ETCS.nno_ind
#print axioms Found.ETB.ETCS.omG_wf
#print axioms Found.ETB.ETCS.infinity

found/EtZ1.lean

lean_all/found/EtZ1.lean

/-
  T209 (Lawvere–Mitchell), direction A, part 1. found/EtZ1 — the category of
  sets inside an arbitrary model of ZF:
  * the cartesian product A × B of two sets exists (slices by replacement,
    then union);
  * arrows A → B are set-functions f ⊆ A × B (total, single-valued);
  * composites exist (separation in A × C) and are unique up to
    coextension; identities; unit and associativity laws (up to
    coextension).
  Arrows are compared by coextension: the first-order (relational) form of
  ETCS.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.IZ11

namespace Found

namespace IZ

open FO FOCT NBGc

variable (M : ZM)

/-! ## the product of two sets -/

theorem slice_ex (a B : M.D) : ∃ s, ∀ p, In M p s ↔ ∃ b, In M b B ∧ OPs M.R p a b :=
  match repl M (Θ := fun u => OPs M.R (u 1) (u 2) (u 0)) (def_op M 1 2 0) (fun _ => a) B
      (fun _ _ _ _ h1 h2 => op_uniq M h1 h2) with
  | ⟨w, hw⟩ => ⟨w, fun p => hw p⟩

theorem prod_ex (A B : M.D) : ∃ P, ∀ p, In M p P ↔ ∃ a b, In M a A ∧ In M b B ∧ OPs M.R p a b := by
  let W : (Nat → M.D) → Prop := fun u => ∀ p, In M p (u 1) ↔ ∃ b, In M b (u 2) ∧ OPs M.R p (u 0) b
  have hW : Def M W := def_congr M (fun _ => Iff.rfl) (def_all M (def_iff M (def_mem M 0 2)
    (def_ex M (def_and M (def_mem M 0 4) (def_op M 1 2 0)))))
  match repl M hW (fun _ => B) A (fun _ _ z z' h1 h2 p => (h1 p).trans (h2 p).symm) with
  | ⟨F, hF⟩ => match union_ex M F with
    | ⟨P, hP⟩ => exact ⟨P, fun p => ⟨fun h => match (hP p).1 h with
        | ⟨z, hz, hpz⟩ => match (hF z).1 hz with
          | ⟨a, ha, hw⟩ => match (hw p).1 hpz with
            | ⟨b, hb, hop⟩ => ⟨a, b, ha, hb, hop⟩,
      fun ⟨a, b, ha, hb, hop⟩ => match slice_ex M a B with
        | ⟨s, hs⟩ => (hP p).2 ⟨s, (hF s).2 ⟨a, ha, hs⟩, (hs p).2 ⟨b, hb, hop⟩⟩⟩⟩

/-! ## set-functions -/

/-- f is a function from A to B -/
def IsFn (A B f : M.D) : Prop :=
  (∀ p, In M p f → ∃ a b, In M a A ∧ In M b B ∧ OPs M.R p a b) ∧
  (∀ a, In M a A → ∃ b, InG M f a b) ∧
  (∀ a b b', InG M f a b → InG M f a b' → Ceq M.R b b')

theorem ing_resp {f f' a a' b b' : M.D} (ef : Ceq M.R f f') (ea : Ceq M.R a a') (eb : Ceq M.R b b') (h : InG M f a b) : InG M f' a' b' :=
  match h with
  | ⟨p, hp, hop⟩ => ⟨p, mem_ceq_r M ef hp, op_resp M hop (ceq_refl M.R p) ea eb⟩

/-- the components of a pair in a function lie in its domain and codomain -/
theorem ing_dom {A B f a b : M.D} (hf : IsFn M A B f) (h : InG M f a b) : In M a A ∧ In M b B :=
  match h with
  | ⟨p, hp, hop⟩ => match hf.1 p hp with
    | ⟨_, _, ha', hb', hop'⟩ =>
      have e := op_inj' M hop' hop
      ⟨mem_ceq_l M e.1 ha', mem_ceq_l M e.2 hb'⟩

/-- composite: h relates a to c when f, g relate a to some b and b to c -/
def IsComp (f g h : M.D) : Prop := ∀ a c, InG M h a c ↔ ∃ b, InG M f a b ∧ InG M g b c

theorem comp_ex {A B C f g : M.D} (hf : IsFn M A B f) (hg : IsFn M B C g) : ∃ h, IsFn M A C h ∧ IsComp M f g h := by
  let P : (Nat → M.D) → Prop := fun u => ∃ a c, OPs M.R (u 0) a c ∧ ∃ b, InG M (u 1) a b ∧ InG M (u 2) b c
  have hP : Def M P := def_congr M (fun _ => Iff.rfl) (def_ex M (def_ex M (def_and M (def_op M 2 1 0)
    (def_ex M (def_and M (def_InG M 4 2 0) (def_InG M 5 0 1))))))
  match prod_ex M A C with
  | ⟨AC, hAC⟩ => match sep M hP (fun n => match n with | 0 => f | _ => g) AC with
    | ⟨h, hh⟩ =>
      have ingH : ∀ a c, InG M h a c ↔ ∃ b, InG M f a b ∧ InG M g b c := fun a c =>
        ⟨fun ⟨p, hp, hop⟩ => match ((hh p).1 hp).2 with
          | ⟨a', c', hop', b, h1, h2⟩ =>
            have e := op_inj' M hop' hop
            ⟨b, ing_resp M (ceq_refl M.R f) e.1 (ceq_refl M.R b) h1, ing_resp M (ceq_refl M.R g) (ceq_refl M.R b) e.2 h2⟩,
         fun ⟨b, h1, h2⟩ => match op_exists M a c with
          | ⟨p, hop⟩ => ⟨p, (hh p).2 ⟨(hAC p).2 ⟨a, c, (ing_dom M hf h1).1, (ing_dom M hg h2).2, hop⟩, a, c, hop, b, h1, h2⟩, hop⟩⟩
      refine ⟨h, ⟨fun p hp => ?_, fun a ha => ?_, fun a c c' h1 h2 => ?_⟩, ingH⟩
      · match (hAC p).1 ((hh p).1 hp).1 with
        | ⟨a, c, ha, hc, hop⟩ => exact ⟨a, c, ha, hc, hop⟩
      · match hf.2.1 a ha with
        | ⟨b, hb⟩ => match hg.2.1 b (ing_dom M hf hb).2 with
          | ⟨c, hc⟩ => exact ⟨c, (ingH a c).2 ⟨b, hb, hc⟩⟩
      · match (ingH a c).1 h1, (ingH a c').1 h2 with
        | ⟨b, f1, g1⟩, ⟨b', f2, g2⟩ =>
          exact hg.2.2 b c c' g1 (ing_resp M (ceq_refl M.R g) (ceq_symm M.R (hf.2.2 a b b' f1 f2)) (ceq_refl M.R c') g2)

/-- composites are unique up to coextension -/
theorem comp_uniq {A C f g h h' : M.D} (hh : IsFn M A C h) (hh' : IsFn M A C h') (c1 : IsComp M f g h) (c2 : IsComp M f g h') :
    Ceq M.R h h' := fun p =>
  ⟨fun hp => match hh.1 p hp with
    | ⟨a, c, _, _, hop⟩ => match (c2 a c).2 ((c1 a c).1 ⟨p, hp, hop⟩) with
      | ⟨_, hq, hop'⟩ => mem_ceq_l M (op_uniq M hop' hop) hq,
   fun hp => match hh'.1 p hp with
    | ⟨a, c, _, _, hop⟩ => match (c1 a c).2 ((c2 a c).1 ⟨p, hp, hop⟩) with
      | ⟨_, hq, hop'⟩ => mem_ceq_l M (op_uniq M hop' hop) hq⟩

/-- two sets of pairs with the same pairs are coextensional -/
theorem fn_ext {A B A' B' f g : M.D} (hf : IsFn M A B f) (hg : IsFn M A' B' g) (h : ∀ a b, InG M f a b ↔ InG M g a b) :
    Ceq M.R f g := fun p =>
  ⟨fun hp => match hf.1 p hp with
    | ⟨a, b, _, _, hop⟩ => match (h a b).1 ⟨p, hp, hop⟩ with
      | ⟨_, hq, hop'⟩ => mem_ceq_l M (op_uniq M hop' hop) hq,
   fun hp => match hg.1 p hp with
    | ⟨a, b, _, _, hop⟩ => match (h a b).2 ⟨p, hp, hop⟩ with
      | ⟨_, hq, hop'⟩ => mem_ceq_l M (op_uniq M hop' hop) hq⟩

/-! ## identities -/

theorem id_ex (A : M.D) : ∃ i, IsFn M A A i ∧ ∀ a a', InG M i a a' ↔ In M a A ∧ Ceq M.R a a' := by
  let P : (Nat → M.D) → Prop := fun u => ∃ a, OPs M.R (u 0) a a
  have hP : Def M P := def_congr M (fun _ => Iff.rfl) (def_ex M (def_op M 1 0 0))
  match prod_ex M A A with
  | ⟨AA, hAA⟩ => match sep M hP (fun _ => A) AA with
    | ⟨i, hi⟩ =>
      have ingI : ∀ a a', InG M i a a' ↔ In M a A ∧ Ceq M.R a a' := fun a a' =>
        ⟨fun ⟨p, hp, hop⟩ => match ((hi p).1 hp).2, (hAA p).1 ((hi p).1 hp).1 with
          | ⟨x, hx⟩, ⟨a1, _, ha1, _, hop1⟩ =>
            have e1 := op_inj' M hx hop
            have e2 := op_inj' M hx hop1
            ⟨mem_ceq_l M (ceq_trans M.R (ceq_symm M.R e2.1) e1.1) ha1, ceq_trans M.R (ceq_symm M.R e1.1) e1.2⟩,
         fun ⟨ha, e⟩ => match op_exists M a a with
          | ⟨p, hop⟩ =>
            have hop' : OPs M.R p a a' := op_resp M hop (ceq_refl M.R p) (ceq_refl M.R a) e
            ⟨p, (hi p).2 ⟨(hAA p).2 ⟨a, a, ha, ha, hop⟩, a, hop⟩, hop'⟩⟩
      refine ⟨i, ⟨fun p hp => (hAA p).1 ((hi p).1 hp).1, fun a ha => ⟨a, (ingI a a).2 ⟨ha, ceq_refl M.R a⟩⟩,
        fun a b b' h1 h2 => ceq_trans M.R (ceq_symm M.R ((ingI a b).1 h1).2) ((ingI a b').1 h2).2⟩, ingI⟩

/-! ## the category laws -/

theorem comp_id_l {A B f i : M.D} (hf : IsFn M A B f) (hi : ∀ a a', InG M i a a' ↔ In M a A ∧ Ceq M.R a a') : IsComp M i f f :=
  fun a c => ⟨fun h => ⟨a, (hi a a).2 ⟨(ing_dom M hf h).1, ceq_refl M.R a⟩, h⟩,
    fun ⟨b, h1, h2⟩ => ing_resp M (ceq_refl M.R f) (ceq_symm M.R ((hi a b).1 h1).2) (ceq_refl M.R c) h2⟩

theorem comp_id_r {A B f i : M.D} (hf : IsFn M A B f) (hi : ∀ b b', InG M i b b' ↔ In M b B ∧ Ceq M.R b b') : IsComp M f i f :=
  fun a c => ⟨fun h => ⟨c, h, (hi c c).2 ⟨(ing_dom M hf h).2, ceq_refl M.R c⟩⟩,
    fun ⟨b, h1, h2⟩ => ing_resp M (ceq_refl M.R f) (ceq_refl M.R a) ((hi b c).1 h2).2 h1⟩

theorem comp_assoc {A D f g k h1 h2 h3 h4 : M.D} (hh2 : IsFn M A D h2) (hh4 : IsFn M A D h4)
    (c1 : IsComp M f g h1) (c2 : IsComp M h1 k h2) (c3 : IsComp M g k h3) (c4 : IsComp M f h3 h4) : Ceq M.R h2 h4 :=
  fn_ext M hh2 hh4 (fun a d =>
    ⟨fun h => match (c2 a d).1 h with
      | ⟨c, hac, hcd⟩ => match (c1 a c).1 hac with
        | ⟨b, hab, hbc⟩ => (c4 a d).2 ⟨b, hab, (c3 b d).2 ⟨c, hbc, hcd⟩⟩,
     fun h => match (c4 a d).1 h with
      | ⟨b, hab, hbd⟩ => match (c3 b d).1 hbd with
        | ⟨c, hbc, hcd⟩ => (c2 a d).2 ⟨c, (c1 a c).2 ⟨b, hab, hbc⟩, hcd⟩⟩)

end IZ

end Found

#print axioms Found.IZ.prod_ex
#print axioms Found.IZ.comp_ex
#print axioms Found.IZ.comp_uniq
#print axioms Found.IZ.fn_ext
#print axioms Found.IZ.id_ex
#print axioms Found.IZ.comp_id_l
#print axioms Found.IZ.comp_id_r
#print axioms Found.IZ.comp_assoc

found/EtZ2.lean

lean_all/found/EtZ2.lean

/-
  T209 (Lawvere–Mitchell), direction A, part 2. found/EtZ2 — finite limits
  in the category of sets of a model of ZF:
  * a terminal object (a singleton): every set has exactly one function to
    it (up to coextension);
  * products: A × B with its projections, pairing and uniqueness;
  * equalizers: {a ∈ A | f(a) = g(a)} with its inclusion, the lift and its
    uniqueness.
  Composites are stated relationally (h ; f relates c to b when some a has
  h(c) = a and f(a) = b).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.EtZ1

namespace Found

namespace IZ

open FO FOCT NBGc

variable (M : ZM)

/-- a function into a singleton -/
theorem terminal : ∃ T, (∃ e, ∀ s, In M s T ↔ Ceq M.R s e) ∧ ∀ A, (∃ f, IsFn M A T f) ∧
    ∀ f g, IsFn M A T f → IsFn M A T g → Ceq M.R f g :=
  match sing_exists M M.d0 with
  | ⟨T, hT⟩ => ⟨T, ⟨M.d0, hT⟩, fun A =>
    ⟨match prod_ex M A T with
      | ⟨P, hP⟩ => ⟨P, fun p hp => (hP p).1 hp, fun a ha => match op_exists M a M.d0 with
          | ⟨p, hop⟩ => ⟨M.d0, p, (hP p).2 ⟨a, M.d0, ha, (hT M.d0).2 (ceq_refl M.R _), hop⟩, hop⟩,
        fun _ b b' h1 h2 => match h1, h2 with
          | ⟨p, hp, hop⟩, ⟨q, hq, hoq⟩ => match (hP p).1 hp, (hP q).1 hq with
            | ⟨_, b1, _, hb1, hop1⟩, ⟨_, b2, _, hb2, hop2⟩ =>
              have e1 : Ceq M.R b M.d0 := ceq_trans M.R (ceq_symm M.R (op_inj' M hop1 hop).2) ((hT b1).1 hb1)
              have e2 : Ceq M.R b' M.d0 := ceq_trans M.R (ceq_symm M.R (op_inj' M hop2 hoq).2) ((hT b2).1 hb2)
              ceq_trans M.R e1 (ceq_symm M.R e2)⟩,
     fun f g hf hg => fn_ext M hf hg (fun a b =>
       ⟨fun h => match hg.2.1 a (ing_dom M hf h).1 with
          | ⟨b', h'⟩ => ing_resp M (ceq_refl M.R g) (ceq_refl M.R a)
              (ceq_trans M.R ((hT b').1 (ing_dom M hg h').2) (ceq_symm M.R ((hT b).1 (ing_dom M hf h).2))) h',
        fun h => match hf.2.1 a (ing_dom M hg h).1 with
          | ⟨b', h'⟩ => ing_resp M (ceq_refl M.R f) (ceq_refl M.R a)
              (ceq_trans M.R ((hT b').1 (ing_dom M hf h').2) (ceq_symm M.R ((hT b).1 (ing_dom M hg h).2))) h'⟩)⟩⟩

/-! ## products -/

/-- the relational composite h ; f -/
def RC (h f c b : M.D) : Prop := ∃ a, InG M h c a ∧ InG M f a b

/-- the first projection of A × B -/
theorem proj1_ex {A B P : M.D} (hP : ∀ p, In M p P ↔ ∃ a b, In M a A ∧ In M b B ∧ OPs M.R p a b) :
    ∃ q, IsFn M P A q ∧ ∀ p x, InG M q p x ↔ In M p P ∧ ∃ a b, OPs M.R p a b ∧ Ceq M.R a x := by
  let Q : (Nat → M.D) → Prop := fun u => ∃ p a b, OPs M.R (u 0) p a ∧ OPs M.R p a b
  have hQ : Def M Q := def_congr M (fun _ => Iff.rfl) (def_ex M (def_ex M (def_ex M (def_and M (def_op M 3 2 1) (def_op M 2 1 0)))))
  match prod_ex M P A with
  | ⟨PA, hPA⟩ => match sep M hQ (fun _ => M.d0) PA with
    | ⟨q, hq⟩ =>
      have ingQ : ∀ p x, InG M q p x ↔ In M p P ∧ ∃ a b, OPs M.R p a b ∧ Ceq M.R a x := fun p x =>
        ⟨fun ⟨r, hr, hop⟩ => match ((hq r).1 hr).2, (hPA r).1 ((hq r).1 hr).1 with
          | ⟨p0, a0, b0, hr0, hp0⟩, ⟨p1, _, hp1, _, hr1⟩ =>
            have e := op_inj' M hr0 hop
            have e1 := op_inj' M hr1 hop
            ⟨mem_ceq_l M e1.1 hp1, a0, b0, op_resp M hp0 e.1 (ceq_refl M.R a0) (ceq_refl M.R b0), e.2⟩,
         fun ⟨hp, a, b, hab, e⟩ => match (hP p).1 hp, op_exists M p x with
          | ⟨a1, b1, ha1, _, h1⟩, ⟨r, hr⟩ =>
            have e1 := op_inj' M h1 hab
            have hx : In M x A := mem_ceq_l M (ceq_trans M.R e1.1 e) ha1
            ⟨r, (hq r).2 ⟨(hPA r).2 ⟨p, x, hp, hx, hr⟩, p, x, b, hr, op_resp M hab (ceq_refl M.R p) e (ceq_refl M.R b)⟩, hr⟩⟩
      refine ⟨q, ⟨fun r hr => (hPA r).1 ((hq r).1 hr).1, fun p hp => match (hP p).1 hp with
        | ⟨a, b, _, _, hab⟩ => ⟨a, (ingQ p a).2 ⟨hp, a, b, hab, ceq_refl M.R a⟩⟩,
        fun p x x' h1 h2 => match (ingQ p x).1 h1, (ingQ p x').1 h2 with
          | ⟨_, a, b, hab, e⟩, ⟨_, a', b', hab', e'⟩ =>
            ceq_trans M.R (ceq_symm M.R e) (ceq_trans M.R (op_inj' M hab hab').1 e')⟩, ingQ⟩

/-- the second projection of A × B -/
theorem proj2_ex {A B P : M.D} (hP : ∀ p, In M p P ↔ ∃ a b, In M a A ∧ In M b B ∧ OPs M.R p a b) :
    ∃ q, IsFn M P B q ∧ ∀ p y, InG M q p y ↔ In M p P ∧ ∃ a b, OPs M.R p a b ∧ Ceq M.R b y := by
  let Q : (Nat → M.D) → Prop := fun u => ∃ p a b, OPs M.R (u 0) p b ∧ OPs M.R p a b
  have hQ : Def M Q := def_congr M (fun _ => Iff.rfl) (def_ex M (def_ex M (def_ex M (def_and M (def_op M 3 2 0) (def_op M 2 1 0)))))
  match prod_ex M P B with
  | ⟨PB, hPB⟩ => match sep M hQ (fun _ => M.d0) PB with
    | ⟨q, hq⟩ =>
      have ingQ : ∀ p y, InG M q p y ↔ In M p P ∧ ∃ a b, OPs M.R p a b ∧ Ceq M.R b y := fun p y =>
        ⟨fun ⟨r, hr, hop⟩ => match ((hq r).1 hr).2, (hPB r).1 ((hq r).1 hr).1 with
          | ⟨p0, a0, b0, hr0, hp0⟩, ⟨p1, _, hp1, _, hr1⟩ =>
            have e := op_inj' M hr0 hop
            have e1 := op_inj' M hr1 hop
            ⟨mem_ceq_l M e1.1 hp1, a0, b0, op_resp M hp0 e.1 (ceq_refl M.R a0) (ceq_refl M.R b0), e.2⟩,
         fun ⟨hp, a, b, hab, e⟩ => match (hP p).1 hp, op_exists M p y with
          | ⟨a1, b1, _, hb1, h1⟩, ⟨r, hr⟩ =>
            have e1 := op_inj' M h1 hab
            have hy : In M y B := mem_ceq_l M (ceq_trans M.R e1.2 e) hb1
            ⟨r, (hq r).2 ⟨(hPB r).2 ⟨p, y, hp, hy, hr⟩, p, a, y, hr, op_resp M hab (ceq_refl M.R p) (ceq_refl M.R a) e⟩, hr⟩⟩
      refine ⟨q, ⟨fun r hr => (hPB r).1 ((hq r).1 hr).1, fun p hp => match (hP p).1 hp with
        | ⟨a, b, _, _, hab⟩ => ⟨b, (ingQ p b).2 ⟨hp, a, b, hab, ceq_refl M.R b⟩⟩,
        fun p y y' h1 h2 => match (ingQ p y).1 h1, (ingQ p y').1 h2 with
          | ⟨_, a, b, hab, e⟩, ⟨_, a', b', hab', e'⟩ =>
            ceq_trans M.R (ceq_symm M.R e) (ceq_trans M.R (op_inj' M hab hab').2 e')⟩, ingQ⟩

/-- the pairing of two functions into A × B -/
theorem pair_ex {A B P C f g : M.D} (hP : ∀ p, In M p P ↔ ∃ a b, In M a A ∧ In M b B ∧ OPs M.R p a b)
    (hf : IsFn M C A f) (hg : IsFn M C B g) :
    ∃ h, IsFn M C P h ∧ ∀ c p, InG M h c p ↔ In M c C ∧ ∃ a b, OPs M.R p a b ∧ InG M f c a ∧ InG M g c b := by
  let Q : (Nat → M.D) → Prop := fun u => ∃ c p a b, OPs M.R (u 0) c p ∧ OPs M.R p a b ∧ InG M (u 1) c a ∧ InG M (u 2) c b
  have hQ : Def M Q := def_congr M (fun _ => Iff.rfl) (def_ex M (def_ex M (def_ex M (def_ex M (def_and M (def_op M 4 3 2)
    (def_and M (def_op M 2 1 0) (def_and M (def_InG M 5 3 1) (def_InG M 6 3 0))))))))
  match prod_ex M C P with
  | ⟨CP, hCP⟩ => match sep M hQ (fun n => match n with | 0 => f | _ => g) CP with
    | ⟨h, hh⟩ =>
      have ingH : ∀ c p, InG M h c p ↔ In M c C ∧ ∃ a b, OPs M.R p a b ∧ InG M f c a ∧ InG M g c b := fun c p =>
        ⟨fun ⟨r, hr, hop⟩ => match ((hh r).1 hr).2, (hCP r).1 ((hh r).1 hr).1 with
          | ⟨c0, p0, a, b, hr0, hp0, fa, gb⟩, ⟨c1, _, hc1, _, hr1⟩ =>
            have e := op_inj' M hr0 hop
            have e1 := op_inj' M hr1 hop
            ⟨mem_ceq_l M e1.1 hc1, a, b, op_resp M hp0 e.2 (ceq_refl M.R a) (ceq_refl M.R b),
              ing_resp M (ceq_refl M.R f) e.1 (ceq_refl M.R a) fa, ing_resp M (ceq_refl M.R g) e.1 (ceq_refl M.R b) gb⟩,
         fun ⟨hc, a, b, hab, fa, gb⟩ => match op_exists M c p with
          | ⟨r, hr⟩ => ⟨r, (hh r).2 ⟨(hCP r).2 ⟨c, p, hc, (hP p).2 ⟨a, b, (ing_dom M hf fa).2, (ing_dom M hg gb).2, hab⟩, hr⟩,
              c, p, a, b, hr, hab, fa, gb⟩, hr⟩⟩
      refine ⟨h, ⟨fun r hr => (hCP r).1 ((hh r).1 hr).1, fun c hc => ?_, fun c p p' h1 h2 => ?_⟩, ingH⟩
      · match hf.2.1 c hc, hg.2.1 c hc with
        | ⟨a, fa⟩, ⟨b, gb⟩ => match op_exists M a b with
          | ⟨p, hop⟩ => exact ⟨p, (ingH c p).2 ⟨hc, a, b, hop, fa, gb⟩⟩
      · match (ingH c p).1 h1, (ingH c p').1 h2 with
        | ⟨_, a, b, hab, fa, gb⟩, ⟨_, a', b', hab', fa', gb'⟩ =>
          exact op_uniq M hab (op_resp M hab' (ceq_refl M.R p') (ceq_symm M.R (hf.2.2 c a a' fa fa'))
            (ceq_symm M.R (hg.2.2 c b b' gb gb')))

/-- products: projections, pairing, uniqueness -/
theorem products (A B : M.D) : ∃ P p1 p2, IsFn M P A p1 ∧ IsFn M P B p2 ∧
    ∀ C f g, IsFn M C A f → IsFn M C B g →
      ∃ h, IsFn M C P h ∧ (∀ c a, RC M h p1 c a ↔ InG M f c a) ∧ (∀ c b, RC M h p2 c b ↔ InG M g c b) ∧
      ∀ h', IsFn M C P h' → (∀ c a, RC M h' p1 c a ↔ InG M f c a) → (∀ c b, RC M h' p2 c b ↔ InG M g c b) → Ceq M.R h' h :=
  match prod_ex M A B with
  | ⟨P, hP⟩ => match proj1_ex M hP, proj2_ex M hP with
    | ⟨p1, f1, i1⟩, ⟨p2, f2, i2⟩ => ⟨P, p1, p2, f1, f2, fun C f g hf hg => match pair_ex M hP hf hg with
      | ⟨h, hh, ih⟩ =>
        have r1h : ∀ c a, RC M h p1 c a ↔ InG M f c a := fun c a =>
          ⟨fun ⟨p, hcp, hpa⟩ => match (ih c p).1 hcp, (i1 p a).1 hpa with
            | ⟨_, _, _, hab0, fa0, _⟩, ⟨_, _, _, hab1, e1⟩ =>
              ing_resp M (ceq_refl M.R f) (ceq_refl M.R c) (ceq_trans M.R (op_inj' M hab0 hab1).1 e1) fa0,
           fun fa => match hg.2.1 c (ing_dom M hf fa).1 with
            | ⟨b, gb⟩ => match op_exists M a b with
              | ⟨p, hop⟩ => ⟨p, (ih c p).2 ⟨(ing_dom M hf fa).1, a, b, hop, fa, gb⟩,
                  (i1 p a).2 ⟨(hP p).2 ⟨a, b, (ing_dom M hf fa).2, (ing_dom M hg gb).2, hop⟩, a, b, hop, ceq_refl M.R a⟩⟩⟩
        have r2h : ∀ c b, RC M h p2 c b ↔ InG M g c b := fun c b =>
          ⟨fun ⟨p, hcp, hpb⟩ => match (ih c p).1 hcp, (i2 p b).1 hpb with
            | ⟨_, _, _, hab0, _, gb0⟩, ⟨_, _, _, hab1, e1⟩ =>
              ing_resp M (ceq_refl M.R g) (ceq_refl M.R c) (ceq_trans M.R (op_inj' M hab0 hab1).2 e1) gb0,
           fun gb => match hf.2.1 c (ing_dom M hg gb).1 with
            | ⟨a, fa⟩ => match op_exists M a b with
              | ⟨p, hop⟩ => ⟨p, (ih c p).2 ⟨(ing_dom M hg gb).1, a, b, hop, fa, gb⟩,
                  (i2 p b).2 ⟨(hP p).2 ⟨a, b, (ing_dom M hf fa).2, (ing_dom M hg gb).2, hop⟩, a, b, hop, ceq_refl M.R b⟩⟩⟩
        have char : ∀ h', IsFn M C P h' → (∀ c a, RC M h' p1 c a ↔ InG M f c a) → (∀ c b, RC M h' p2 c b ↔ InG M g c b) →
            ∀ c p, InG M h' c p ↔ InG M h c p := fun h' hh' r1 r2 c p =>
          ⟨fun hcp => match (hP p).1 (ing_dom M hh' hcp).2 with
            | ⟨a, b, _, _, hab⟩ =>
              have fa := (r1 c a).1 ⟨p, hcp, (i1 p a).2 ⟨(ing_dom M hh' hcp).2, a, b, hab, ceq_refl M.R a⟩⟩
              have gb := (r2 c b).1 ⟨p, hcp, (i2 p b).2 ⟨(ing_dom M hh' hcp).2, a, b, hab, ceq_refl M.R b⟩⟩
              (ih c p).2 ⟨(ing_dom M hh' hcp).1, a, b, hab, fa, gb⟩,
           fun hcp => match (ih c p).1 hcp with
            | ⟨hc, a, b, hab, fa, gb⟩ => match hh'.2.1 c hc with
              | ⟨p', hp'⟩ => match (hP p').1 (ing_dom M hh' hp').2 with
                | ⟨a', b', _, _, hab'⟩ =>
                  have fa' := (r1 c a').1 ⟨p', hp', (i1 p' a').2 ⟨(ing_dom M hh' hp').2, a', b', hab', ceq_refl M.R a'⟩⟩
                  have gb' := (r2 c b').1 ⟨p', hp', (i2 p' b').2 ⟨(ing_dom M hh' hp').2, a', b', hab', ceq_refl M.R b'⟩⟩
                  have ep : Ceq M.R p' p := op_uniq M hab' (op_resp M hab (ceq_refl M.R p)
                    (hf.2.2 c a a' fa fa') (hg.2.2 c b b' gb gb'))
                  ing_resp M (ceq_refl M.R h') (ceq_refl M.R c) ep hp'⟩
        ⟨h, hh, r1h, r2h, fun h' hh' r1 r2 => fn_ext M hh' hh (char h' hh' r1 r2)⟩⟩

end IZ

end Found

#print axioms Found.IZ.terminal
#print axioms Found.IZ.proj1_ex
#print axioms Found.IZ.proj2_ex
#print axioms Found.IZ.pair_ex
#print axioms Found.IZ.products

found/EtZ3.lean

lean_all/found/EtZ3.lean

/-
  T209 (Lawvere–Mitchell), direction A, part 3. found/EtZ3 — equalizers in
  the category of sets of a model of ZF: E = {a ∈ A | f(a) = g(a)} with its
  inclusion; an arrow h with h ; f = h ; g factors through E (by h itself)
  and the factorization is unique.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.EtZ2

namespace Found

namespace IZ

open FO FOCT NBGc

variable (M : ZM)

theorem equalizers {A B f g : M.D} (hf : IsFn M A B f) (hg : IsFn M A B g) :
    ∃ E m, IsFn M E A m ∧ (∀ e b, RC M m f e b ↔ RC M m g e b) ∧
      ∀ C h, IsFn M C A h → (∀ c b, RC M h f c b ↔ RC M h g c b) →
        ∃ u, IsFn M C E u ∧ (∀ c a, RC M u m c a ↔ InG M h c a) ∧
        ∀ u', IsFn M C E u' → (∀ c a, RC M u' m c a ↔ InG M h c a) → Ceq M.R u' u := by
  let Q : (Nat → M.D) → Prop := fun u => ∃ b, InG M (u 1) (u 0) b ∧ InG M (u 2) (u 0) b
  have hQ : Def M Q := def_congr M (fun _ => Iff.rfl) (def_ex M (def_and M (def_InG M 2 1 0) (def_InG M 3 1 0)))
  match sep M hQ (fun n => match n with | 0 => f | _ => g) A with
  | ⟨E, hE⟩ => match id_ex M E with
    | ⟨i, hi, ii⟩ =>
      have agree : ∀ a b, In M a E → InG M f a b → InG M g a b := fun a b ha fa => match ((hE a).1 ha).2 with
        | ⟨b0, f0, g0⟩ => ing_resp M (ceq_refl M.R g) (ceq_refl M.R a) (hf.2.2 a b0 b f0 fa) g0
      have agree' : ∀ a b, In M a E → InG M g a b → InG M f a b := fun a b ha ga => match ((hE a).1 ha).2 with
        | ⟨b0, f0, g0⟩ => ing_resp M (ceq_refl M.R f) (ceq_refl M.R a) (hg.2.2 a b0 b g0 ga) f0
      -- the inclusion E → A
      have hm : IsFn M E A i := ⟨fun p hp => match hi.1 p hp with
          | ⟨a, a', ha, ha', hop⟩ => ⟨a, a', ha, ((hE a').1 ha').1, hop⟩, hi.2.1, hi.2.2⟩
      refine ⟨E, i, hm, fun e b => ⟨fun ⟨a, hia, fa⟩ => ⟨a, hia, agree a b (mem_ceq_l M ((ii e a).1 hia).2 ((ii e a).1 hia).1) fa⟩,
        fun ⟨a, hia, ga⟩ => ⟨a, hia, agree' a b (mem_ceq_l M ((ii e a).1 hia).2 ((ii e a).1 hia).1) ga⟩⟩, fun C h hh heq => ?_⟩
      -- the values of h lie in E
      have inE : ∀ c a, InG M h c a → In M a E := fun c a hca => match hf.2.1 a (ing_dom M hh hca).2 with
        | ⟨b, fab⟩ => match (heq c b).1 ⟨a, hca, fab⟩ with
          | ⟨a', hca', gab⟩ => (hE a).2 ⟨(ing_dom M hh hca).2, b, fab,
              ing_resp M (ceq_refl M.R g) (hh.2.2 c a' a hca' hca) (ceq_refl M.R b) gab⟩
      have hu : IsFn M C E h := ⟨fun p hp => match hh.1 p hp with
          | ⟨c, a, hc, _, hop⟩ => ⟨c, a, hc, inE c a ⟨p, hp, hop⟩, hop⟩, hh.2.1, hh.2.2⟩
      have rcm : ∀ (u : M.D) c a, RC M u i c a ↔ ∃ e, InG M u c e ∧ In M e E ∧ Ceq M.R e a := fun u c a =>
        ⟨fun ⟨e, h1, h2⟩ => ⟨e, h1, (ii e a).1 h2⟩, fun ⟨e, h1, h2⟩ => ⟨e, h1, (ii e a).2 h2⟩⟩
      refine ⟨h, hu, fun c a => ⟨fun r => match (rcm h c a).1 r with
          | ⟨e, h1, _, h3⟩ => ing_resp M (ceq_refl M.R h) (ceq_refl M.R c) h3 h1,
        fun hca => (rcm h c a).2 ⟨a, hca, inE c a hca, ceq_refl M.R a⟩⟩, fun u' hu' r => ?_⟩
      exact fn_ext M hu' hu (fun c e => ⟨fun h1 => (r c e).1 ((rcm u' c e).2 ⟨e, h1, (ing_dom M hu' h1).2, ceq_refl M.R e⟩),
        fun h1 => match (rcm u' c e).1 ((r c e).2 h1) with
          | ⟨e', h2, _, h4⟩ => ing_resp M (ceq_refl M.R u') (ceq_refl M.R c) h4 h2⟩)

end IZ

end Found

#print axioms Found.IZ.equalizers

found/EtZ4.lean

lean_all/found/EtZ4.lean

/-
  T209 (Lawvere–Mitchell), direction A, part 4. found/EtZ4 — exponentials
  in the category of sets of a model of ZF: the set B^A of all functions
  A → B (separation in the power set of A × B), evaluation, and for every
  g : C × A → B the transpose C → B^A (c ↦ the function a ↦ g(c, a), by
  replacement), unique with ev ∘ (ĝ × id) = g (stated pointwise).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.EtZ3

namespace Found

namespace IZ

open FO FOCT NBGc

variable (M : ZM)

theorem def_IsFn (i j k : Nat) : Def M (fun v => IsFn M (v i) (v j) (v k)) :=
  def_and M (def_congr M (fun _ => Iff.rfl) (def_all M (def_imp M (def_mem M 0 (k + 1))
      (def_ex M (def_ex M (def_and M (def_mem M 1 (i + 3)) (def_and M (def_mem M 0 (j + 3)) (def_op M 2 1 0))))))))
    (def_and M (def_congr M (fun _ => Iff.rfl) (def_all M (def_imp M (def_mem M 0 (i + 1)) (def_ex M (def_InG M (k + 2) 1 0)))))
      (def_congr M (fun _ => Iff.rfl) (def_all M (def_all M (def_all M (def_imp M (def_InG M (k + 3) 2 1)
        (def_imp M (def_InG M (k + 3) 2 0) (def_ceq M 1 0))))))))

/-- every function A → B is a subset of A × B -/
theorem fn_sub {A B P f : M.D} (hP : ∀ p, In M p P ↔ ∃ a b, In M a A ∧ In M b B ∧ OPs M.R p a b) (hf : IsFn M A B f) :
    ∀ p, In M p f → In M p P := fun p hp => (hP p).2 (hf.1 p hp)

/-- the set of all functions A → B -/
theorem funset_ex (A B : M.D) : ∃ F, ∀ f, In M f F ↔ IsFn M A B f :=
  match prod_ex M A B with
  | ⟨P, hP⟩ => match power_ex M P with
    | ⟨Pw, hPw⟩ => match sep M (def_IsFn M 1 2 0) (fun n => match n with | 0 => A | _ => B) Pw with
      | ⟨F, hF⟩ => ⟨F, fun f => ⟨fun h => ((hF f).1 h).2, fun h => (hF f).2 ⟨(hPw f).2 (fn_sub M hP h), h⟩⟩⟩

/-- evaluation Fun × A → B -/
theorem ev_ex {A B F Q : M.D} (hF : ∀ f, In M f F ↔ IsFn M A B f)
    (hQ : ∀ q, In M q Q ↔ ∃ f a, In M f F ∧ In M a A ∧ OPs M.R q f a) :
    ∃ ev, IsFn M Q B ev ∧ ∀ q b, InG M ev q b ↔ ∃ f a, In M f F ∧ OPs M.R q f a ∧ InG M f a b := by
  let P' : (Nat → M.D) → Prop := fun u => ∃ q b f a, OPs M.R (u 0) q b ∧ OPs M.R q f a ∧ InG M f a b
  have hP' : Def M P' := def_congr M (fun _ => Iff.rfl) (def_ex M (def_ex M (def_ex M (def_ex M (def_and M (def_op M 4 3 2)
    (def_and M (def_op M 3 1 0) (def_InG M 1 0 2)))))))
  match prod_ex M Q B with
  | ⟨QB, hQB⟩ => match sep M hP' (fun _ => M.d0) QB with
    | ⟨ev, hev⟩ =>
      have ingE : ∀ q b, InG M ev q b ↔ ∃ f a, In M f F ∧ OPs M.R q f a ∧ InG M f a b := fun q b =>
        ⟨fun ⟨r, hr, hop⟩ => match ((hev r).1 hr).2, (hQB r).1 ((hev r).1 hr).1 with
          | ⟨q0, b0, f, a, hr0, hq0, fab⟩, ⟨q1, _, hq1, _, hr1⟩ =>
            have e := op_inj' M hr0 hop
            have e1 := op_inj' M hr1 hop
            match (hQ q1).1 hq1 with
            | ⟨f', a', hf', _, hq'⟩ =>
              have e2 := op_inj' M hq0 (op_resp M hq' (ceq_trans M.R e1.1 (ceq_symm M.R e.1)) (ceq_refl M.R f') (ceq_refl M.R a'))
              ⟨f, a, mem_ceq_l M (ceq_symm M.R e2.1) hf', op_resp M hq0 e.1 (ceq_refl M.R f) (ceq_refl M.R a),
                ing_resp M (ceq_refl M.R f) (ceq_refl M.R a) e.2 fab⟩,
         fun ⟨f, a, hf, hq, fab⟩ => match op_exists M q b with
          | ⟨r, hr⟩ => ⟨r, (hev r).2 ⟨(hQB r).2 ⟨q, b, (hQ q).2 ⟨f, a, hf, (ing_dom M ((hF f).1 hf) fab).1, hq⟩,
              (ing_dom M ((hF f).1 hf) fab).2, hr⟩, q, b, f, a, hr, hq, fab⟩, hr⟩⟩
      refine ⟨ev, ⟨fun r hr => (hQB r).1 ((hev r).1 hr).1, fun q hq => ?_, fun q b b' h1 h2 => ?_⟩, ingE⟩
      · match (hQ q).1 hq with
        | ⟨f, a, hf, ha, hop⟩ => match ((hF f).1 hf).2.1 a ha with
          | ⟨b, fab⟩ => exact ⟨b, (ingE q b).2 ⟨f, a, hf, hop, fab⟩⟩
      · match (ingE q b).1 h1, (ingE q b').1 h2 with
        | ⟨f, a, hf, hq, fab⟩, ⟨f', a', _, hq', fab'⟩ =>
          have e := op_inj' M hq hq'
          exact ((hF f).1 hf).2.2 a b b' fab (ing_resp M (ceq_symm M.R e.1) (ceq_symm M.R e.2) (ceq_refl M.R b') fab')

/-- the transpose of g : C × A → B -/
theorem transpose_ex {A B C F P g : M.D} (hF : ∀ f, In M f F ↔ IsFn M A B f)
    (hP : ∀ q, In M q P ↔ ∃ c a, In M c C ∧ In M a A ∧ OPs M.R q c a) (hg : IsFn M P B g) :
    ∃ t, IsFn M C F t ∧ (∀ c a b q, In M c C → OPs M.R q c a → (InG M g q b ↔ ∃ φ, InG M t c φ ∧ InG M φ a b)) ∧
      ∀ t', IsFn M C F t' → (∀ c a b q, In M c C → OPs M.R q c a → (InG M g q b ↔ ∃ φ, InG M t' c φ ∧ InG M φ a b)) →
        Ceq M.R t' t := by
  match prod_ex M A B with
  | ⟨AB, hAB⟩ =>
  let ChP : M.D → M.D → Prop := fun c φ => ∀ p, In M p φ ↔ In M p AB ∧ ∃ a b, OPs M.R p a b ∧ ∃ q, OPs M.R q c a ∧ InG M g q b
  -- sections exist
  have sec : ∀ c, ∃ φ, ChP c φ := fun c => by
    let Pφ : (Nat → M.D) → Prop := fun u => ∃ a b, OPs M.R (u 0) a b ∧ ∃ q, OPs M.R q (u 1) a ∧ InG M (u 2) q b
    have hPφ : Def M Pφ := def_congr M (fun _ => Iff.rfl) (def_ex M (def_ex M (def_and M (def_op M 2 1 0)
      (def_ex M (def_and M (def_op M 0 4 2) (def_InG M 5 0 1))))))
    match sep M hPφ (fun n => match n with | 0 => c | _ => g) AB with
    | ⟨φ, hφ⟩ => exact ⟨φ, fun p => hφ p⟩
  -- sections are unique
  have sec_u : ∀ c φ φ', ChP c φ → ChP c φ' → Ceq M.R φ φ' := fun _ _ _ h1 h2 p => (h1 p).trans (h2 p).symm
  -- the graph of c ↦ section
  let Θ : (Nat → M.D) → Prop := fun u => ∃ φ, OPs M.R (u 1) (u 0) φ ∧
    ∀ p, In M p φ ↔ In M p (u 2) ∧ ∃ a b, OPs M.R p a b ∧ ∃ q, OPs M.R q (u 0) a ∧ InG M (u 3) q b
  have hΘ : Def M Θ := def_congr M (fun _ => Iff.rfl) (def_ex M (def_and M (def_op M 2 1 0)
    (def_all M (def_iff M (def_mem M 0 1) (def_and M (def_mem M 0 4) (def_ex M (def_ex M (def_and M (def_op M 2 1 0)
      (def_ex M (def_and M (def_op M 0 5 2) (def_InG M 8 0 1)))))))))))
  match repl M hΘ (fun n => match n with | 0 => AB | _ => g) C (fun c _ z z' ⟨φ, h1, c1⟩ ⟨φ', h2, c2⟩ =>
      op_uniq M h1 (op_resp M h2 (ceq_refl M.R z') (ceq_refl M.R c) (sec_u c φ' φ c2 c1))) with
  | ⟨t, ht⟩ =>
  have ingT : ∀ c φ, InG M t c φ ↔ In M c C ∧ ChP c φ := fun c φ =>
    ⟨fun ⟨z, hz, hop⟩ => match (ht z).1 hz with
      | ⟨c', hc', φ', hop', ch'⟩ =>
        have e := op_inj' M hop' hop
        ⟨mem_ceq_l M e.1 hc', fun p => (e.2 p).symm.trans ((ch' p).trans
          ⟨fun ⟨h1, a, b, hab, q, hq, gq⟩ => ⟨h1, a, b, hab, q, op_resp M hq (ceq_refl M.R q) e.1 (ceq_refl M.R a), gq⟩,
           fun ⟨h1, a, b, hab, q, hq, gq⟩ => ⟨h1, a, b, hab, q, op_resp M hq (ceq_refl M.R q) (ceq_symm M.R e.1) (ceq_refl M.R a), gq⟩⟩)⟩,
     fun ⟨hc, ch⟩ => match op_exists M c φ with
      | ⟨z, hz⟩ => ⟨z, (ht z).2 ⟨c, hc, φ, hz, ch⟩, hz⟩⟩
  -- sections are functions A → B
  have sec_fn : ∀ c φ, In M c C → ChP c φ → IsFn M A B φ := fun c φ hc ch =>
    ⟨fun p hp => (hAB p).1 ((ch p).1 hp).1,
     fun a ha => match op_exists M c a with
      | ⟨q, hq⟩ => match hg.2.1 q ((hP q).2 ⟨c, a, hc, ha, hq⟩) with
        | ⟨b, gq⟩ => match op_exists M a b with
          | ⟨p, hp⟩ => ⟨b, p, (ch p).2 ⟨(hAB p).2 ⟨a, b, ha, (ing_dom M hg gq).2, hp⟩, a, b, hp, q, hq, gq⟩, hp⟩,
     fun a b b' ⟨p, hp, hop⟩ ⟨p', hp', hop'⟩ => match ((ch p).1 hp).2, ((ch p').1 hp').2 with
      | ⟨a1, b1, hab1, q1, hq1, gq1⟩, ⟨a2, b2, hab2, q2, hq2, gq2⟩ =>
        have e1 := op_inj' M hab1 hop
        have e2 := op_inj' M hab2 hop'
        have eq : Ceq M.R q1 q2 := op_uniq M hq1 (op_resp M hq2 (ceq_refl M.R q2) (ceq_refl M.R c) (ceq_trans M.R e2.1 (ceq_symm M.R e1.1)))
        ceq_trans M.R (ceq_symm M.R e1.2) (ceq_trans M.R (hg.2.2 q1 b1 b2 gq1 (ing_resp M (ceq_refl M.R g) (ceq_symm M.R eq) (ceq_refl M.R b2) gq2)) e2.2)⟩
  have ht_fn : IsFn M C F t :=
    ⟨fun z hz => match (ht z).1 hz with
      | ⟨c, hc, φ, hop, ch⟩ => ⟨c, φ, hc, (hF φ).2 (sec_fn c φ hc ch), hop⟩,
     fun c hc => match sec c with
      | ⟨φ, ch⟩ => ⟨φ, (ingT c φ).2 ⟨hc, ch⟩⟩,
     fun c φ φ' h1 h2 => sec_u c φ φ' ((ingT c φ).1 h1).2 ((ingT c φ').1 h2).2⟩
  have beta : ∀ c a b q, In M c C → OPs M.R q c a → (InG M g q b ↔ ∃ φ, InG M t c φ ∧ InG M φ a b) := fun c a b q hc hq =>
    ⟨fun gq => match sec c, op_exists M a b with
      | ⟨φ, ch⟩, ⟨p, hp⟩ => match (hP q).1 (ing_dom M hg gq).1 with
        | ⟨c', a', _, ha', hq'⟩ =>
          have e := op_inj' M hq' hq
          ⟨φ, (ingT c φ).2 ⟨hc, ch⟩, p, (ch p).2 ⟨(hAB p).2 ⟨a, b, mem_ceq_l M e.2 ha', (ing_dom M hg gq).2, hp⟩, a, b, hp, q, hq, gq⟩, hp⟩,
     fun ⟨φ, tcφ, ⟨p, hp, hop⟩⟩ => match ((ingT c φ).1 tcφ).2 p |>.1 hp with
      | ⟨_, a1, b1, hab1, q1, hq1, gq1⟩ =>
        have e := op_inj' M hab1 hop
        have eq : Ceq M.R q1 q := op_uniq M hq1 (op_resp M hq (ceq_refl M.R q) (ceq_refl M.R c) (ceq_symm M.R e.1))
        ing_resp M (ceq_refl M.R g) eq e.2 gq1⟩
  refine ⟨t, ht_fn, beta, fun t' ht' beta' => ?_⟩
  have key : ∀ c φ, InG M t' c φ → ChP c φ := fun c φ h => by
    have hc := (ing_dom M ht' h).1
    have fφ := (hF φ).1 (ing_dom M ht' h).2
    intro p
    constructor
    · intro hp
      match fφ.1 p hp with
      | ⟨a, b, ha, hb, hab⟩ => match op_exists M c a with
        | ⟨q, hq⟩ => exact ⟨(hAB p).2 ⟨a, b, ha, hb, hab⟩, a, b, hab, q, hq, (beta' c a b q hc hq).2 ⟨φ, h, p, hp, hab⟩⟩
    · intro ⟨_, a, b, hab, q, hq, gq⟩
      match (beta' c a b q hc hq).1 gq with
      | ⟨φ', h', ⟨p', hp', hop'⟩⟩ =>
        have e := ht'.2.2 c φ' φ h' h
        exact mem_ceq_l M (op_uniq M hop' hab) (mem_ceq_r M e hp')
  exact fn_ext M ht' ht_fn (fun c φ => ⟨fun h => (ingT c φ).2 ⟨(ing_dom M ht' h).1, key c φ h⟩,
    fun h => match ht'.2.1 c ((ingT c φ).1 h).1 with
      | ⟨φ', h'⟩ => ing_resp M (ceq_refl M.R t') (ceq_refl M.R c) (sec_u c φ' φ (key c φ' h') ((ingT c φ).1 h).2) h'⟩)

end IZ

end Found

#print axioms Found.IZ.def_IsFn
#print axioms Found.IZ.funset_ex
#print axioms Found.IZ.ev_ex
#print axioms Found.IZ.transpose_ex

found/EtZ5.lean

lean_all/found/EtZ5.lean

/-
  T209 (Lawvere–Mitchell), direction A, part 5. found/EtZ5 — the
  subobject classifier of the category of sets of a model of ZF:
  Ω = {0, 1} (0 = ∅, 1 = {∅}), true picks 1; for every injective function
  m : S → A the characteristic function χ (a ↦ 1 on the image, 0 off it —
  by the classical point) satisfies χ ∘ m = true, every arrow h with
  χ ∘ h = true factors uniquely through m, and χ is the only such arrow.

  Lean 4 core only, hand-written proofs, no axioms (classical logic only as
  the parameter K).
-/
import found.EtZ4

namespace Found

namespace IZ

open FO FOCT NBGc

variable (M : ZM)

theorem empty_ex' : ∃ e, ∀ s, ¬ In M s e :=
  match sep M (def_false M) (fun _ => M.d0) M.d0 with
  | ⟨e, he⟩ => ⟨e, fun s hs => ((he s).1 hs).2⟩

def Mono (S A m : M.D) : Prop := IsFn M S A m ∧ ∀ s s' a, InG M m s a → InG M m s' a → Ceq M.R s s'

theorem classifier (K : CP.Classical') :
    ∃ Ω z o, (∀ v, In M v Ω ↔ Ceq M.R v z ∨ Ceq M.R v o) ∧ ¬ Ceq M.R z o ∧
    ∀ S A m, Mono M S A m → ∃ χ, IsFn M A Ω χ ∧ (∀ s a, InG M m s a → InG M χ a o) ∧
      (∀ C h, IsFn M C A h → (∀ c a, InG M h c a → InG M χ a o) →
        ∃ k, IsFn M C S k ∧ (∀ c a, RC M k m c a ↔ InG M h c a) ∧
          ∀ k', IsFn M C S k' → (∀ c a, RC M k' m c a ↔ InG M h c a) → Ceq M.R k' k) ∧
      ∀ χ', IsFn M A Ω χ' → (∀ s a, InG M m s a → InG M χ' a o) →
        (∀ C h, IsFn M C A h → (∀ c a, InG M h c a → InG M χ' a o) → ∃ k, IsFn M C S k ∧ ∀ c a, RC M k m c a ↔ InG M h c a) →
        Ceq M.R χ' χ := by
  match empty_ex' M with
  | ⟨z, hz⟩ => match sing_exists M z with
    | ⟨o, ho⟩ => match pairing M z o with
      | ⟨Ω, hΩ⟩ =>
      have zo : ¬ Ceq M.R z o := fun e => hz z ((e z).2 ((ho z).2 (ceq_refl M.R z)))
      refine ⟨Ω, z, o, fun v => hΩ v, zo, fun S A m hm => ?_⟩
      let Im : M.D → Prop := fun a => ∃ s, InG M m s a
      -- the characteristic function
      let Pχ : (Nat → M.D) → Prop := fun u => ∃ a v, OPs M.R (u 0) a v ∧
        ((∃ s, InG M (u 1) s a) ∧ Ceq M.R v (u 2) ∨ ¬ (∃ s, InG M (u 1) s a) ∧ Ceq M.R v (u 3))
      have hPχ : Def M Pχ := def_congr M (fun _ => Iff.rfl) (def_ex M (def_ex M (def_and M (def_op M 2 1 0)
        (def_or M (def_and M (def_ex M (def_InG M 4 0 2)) (def_ceq M 0 4))
          (def_and M (def_not M (def_ex M (def_InG M 4 0 2))) (def_ceq M 0 5))))))
      match prod_ex M A Ω with
      | ⟨AΩ, hAΩ⟩ => match sep M hPχ (fun n => match n with | 0 => m | 1 => o | _ => z) AΩ with
        | ⟨χ, hχ⟩ =>
        have ingχ : ∀ a v, InG M χ a v ↔ In M a A ∧ (Im a ∧ Ceq M.R v o ∨ ¬ Im a ∧ Ceq M.R v z) := fun a v =>
          ⟨fun ⟨r, hr, hop⟩ => match ((hχ r).1 hr).2, (hAΩ r).1 ((hχ r).1 hr).1 with
            | ⟨a0, v0, hr0, hc⟩, ⟨a1, _, ha1, _, hr1⟩ =>
              have e := op_inj' M hr0 hop
              have e1 := op_inj' M hr1 hop
              have tr : Im a0 ↔ Im a := ⟨fun ⟨s, h⟩ => ⟨s, ing_resp M (ceq_refl M.R m) (ceq_refl M.R s) e.1 h⟩,
                fun ⟨s, h⟩ => ⟨s, ing_resp M (ceq_refl M.R m) (ceq_refl M.R s) (ceq_symm M.R e.1) h⟩⟩
              ⟨mem_ceq_l M e1.1 ha1, hc.elim (fun ⟨i, ev⟩ => .inl ⟨tr.1 i, ceq_trans M.R (ceq_symm M.R e.2) ev⟩)
                (fun ⟨ni, ev⟩ => .inr ⟨fun i => ni (tr.2 i), ceq_trans M.R (ceq_symm M.R e.2) ev⟩)⟩,
           fun ⟨ha, hc⟩ => match op_exists M a v with
            | ⟨r, hr⟩ =>
              have hv : In M v Ω := (hΩ v).2 (hc.elim (fun h => .inr h.2) (fun h => .inl h.2))
              ⟨r, (hχ r).2 ⟨(hAΩ r).2 ⟨a, v, ha, hv, hr⟩, a, v, hr, hc⟩, hr⟩⟩
        have hχf : IsFn M A Ω χ := ⟨fun r hr => (hAΩ r).1 ((hχ r).1 hr).1,
          fun a ha => (K.lem (Im a)).elim (fun i => ⟨o, (ingχ a o).2 ⟨ha, .inl ⟨i, ceq_refl M.R o⟩⟩⟩)
            (fun ni => ⟨z, (ingχ a z).2 ⟨ha, .inr ⟨ni, ceq_refl M.R z⟩⟩⟩),
          fun a v v' h1 h2 => match ((ingχ a v).1 h1).2, ((ingχ a v').1 h2).2 with
            | .inl ⟨_, e1⟩, .inl ⟨_, e2⟩ => ceq_trans M.R e1 (ceq_symm M.R e2)
            | .inr ⟨_, e1⟩, .inr ⟨_, e2⟩ => ceq_trans M.R e1 (ceq_symm M.R e2)
            | .inl ⟨i, _⟩, .inr ⟨ni, _⟩ => (ni i).elim
            | .inr ⟨ni, _⟩, .inl ⟨i, _⟩ => (ni i).elim⟩
        have χo : ∀ a, InG M χ a o → Im a := fun a h => match ((ingχ a o).1 h).2 with
          | .inl ⟨i, _⟩ => i
          | .inr ⟨_, e⟩ => (zo (ceq_symm M.R e)).elim
        have χI : ∀ a, In M a A → (InG M χ a o ↔ Im a) := fun a ha =>
          ⟨χo a, fun i => (ingχ a o).2 ⟨ha, .inl ⟨i, ceq_refl M.R o⟩⟩⟩
        -- the pullback property of any χ' with χ' ∘ m = true
        have fact : ∀ χ', IsFn M A Ω χ' → (∀ C h, IsFn M C A h → (∀ c a, InG M h c a → InG M χ' a o) →
            ∃ k, IsFn M C S k ∧ ∀ c a, RC M k m c a ↔ InG M h c a) → ∀ a, InG M χ' a o → Im a := by
          intro χ' _ hpb a hχa
          let Q : (Nat → M.D) → Prop := fun u => InG M (u 1) (u 0) (u 2)
          have hQ : Def M Q := def_InG M 1 0 2
          match sep M hQ (fun n => match n with | 0 => χ' | _ => o) A with
          | ⟨D, hD⟩ => match id_ex M D with
            | ⟨i, hi, ii⟩ =>
              have hiA : IsFn M D A i := ⟨fun p hp => match hi.1 p hp with
                | ⟨x, x', hx, hx', hop⟩ => ⟨x, x', hx, ((hD x').1 hx').1, hop⟩, hi.2.1, hi.2.2⟩
              have hiχ : ∀ c x, InG M i c x → InG M χ' x o := fun c x h =>
                have e := (ii c x).1 h
                ((hD x).1 (mem_ceq_l M e.2 e.1)).2
              have haD : In M a D := (hD a).2 ⟨(ing_dom M ‹IsFn M A Ω χ'› hχa).1, hχa⟩
              match hpb D i hiA hiχ with
              | ⟨_, _, hk⟩ => match (hk a a).2 ((ii a a).2 ⟨haD, ceq_refl M.R a⟩) with
                | ⟨s, _, hsa⟩ => exact ⟨s, hsa⟩
        refine ⟨χ, hχf, fun s a h => (ingχ a o).2 ⟨(ing_dom M hm.1 h).2, .inl ⟨⟨s, h⟩, ceq_refl M.R o⟩⟩, ?_, ?_⟩
        · intro C h hh hχh
          let Pk : (Nat → M.D) → Prop := fun u => ∃ c s, OPs M.R (u 0) c s ∧ ∃ a, InG M (u 1) c a ∧ InG M (u 2) s a
          have hPk : Def M Pk := def_congr M (fun _ => Iff.rfl) (def_ex M (def_ex M (def_and M (def_op M 2 1 0)
            (def_ex M (def_and M (def_InG M 4 2 0) (def_InG M 5 1 0))))))
          match prod_ex M C S with
          | ⟨CS, hCS⟩ => match sep M hPk (fun n => match n with | 0 => h | _ => m) CS with
            | ⟨k, hk⟩ =>
            have ingK : ∀ c s, InG M k c s ↔ In M c C ∧ ∃ a, InG M h c a ∧ InG M m s a := fun c s =>
              ⟨fun ⟨r, hr, hop⟩ => match ((hk r).1 hr).2, (hCS r).1 ((hk r).1 hr).1 with
                | ⟨c0, s0, hr0, a, ha, hsa⟩, ⟨c1, _, hc1, _, hr1⟩ =>
                  have e := op_inj' M hr0 hop
                  have e1 := op_inj' M hr1 hop
                  ⟨mem_ceq_l M e1.1 hc1, a, ing_resp M (ceq_refl M.R h) e.1 (ceq_refl M.R a) ha,
                    ing_resp M (ceq_refl M.R m) e.2 (ceq_refl M.R a) hsa⟩,
               fun ⟨hc, a, ha, hsa⟩ => match op_exists M c s with
                | ⟨r, hr⟩ => ⟨r, (hk r).2 ⟨(hCS r).2 ⟨c, s, hc, (ing_dom M hm.1 hsa).1, hr⟩, c, s, hr, a, ha, hsa⟩, hr⟩⟩
            have hkf : IsFn M C S k := ⟨fun r hr => (hCS r).1 ((hk r).1 hr).1,
              fun c hc => match hh.2.1 c hc with
                | ⟨a, ha⟩ => match χo a (hχh c a ha) with
                  | ⟨s, hsa⟩ => ⟨s, (ingK c s).2 ⟨hc, a, ha, hsa⟩⟩,
              fun c s s' h1 h2 => match (ingK c s).1 h1, (ingK c s').1 h2 with
                | ⟨_, a, ha, hsa⟩, ⟨_, a', ha', hsa'⟩ =>
                  hm.2 s s' a hsa (ing_resp M (ceq_refl M.R m) (ceq_refl M.R s') (ceq_symm M.R (hh.2.2 c a a' ha ha')) hsa')⟩
            have rk : ∀ c a, RC M k m c a ↔ InG M h c a := fun c a =>
              ⟨fun ⟨s, hks, hsa⟩ => match (ingK c s).1 hks with
                | ⟨_, a', ha', hsa'⟩ => ing_resp M (ceq_refl M.R h) (ceq_refl M.R c) (hm.1.2.2 s a' a hsa' hsa) ha',
               fun ha => match χo a (hχh c a ha) with
                | ⟨s, hsa⟩ => ⟨s, (ingK c s).2 ⟨(ing_dom M hh ha).1, a, ha, hsa⟩, hsa⟩⟩
            refine ⟨k, hkf, rk, fun k' hk' rk' => fn_ext M hk' hkf (fun c s => ⟨fun h1 => ?_, fun h1 => ?_⟩)⟩
            · match hm.1.2.1 s (ing_dom M hk' h1).2 with
              | ⟨a, hsa⟩ => exact (ingK c s).2 ⟨(ing_dom M hk' h1).1, a, (rk' c a).1 ⟨s, h1, hsa⟩, hsa⟩
            · match (ingK c s).1 h1 with
              | ⟨hc, a, ha, hsa⟩ => match hk'.2.1 c hc with
                | ⟨s', hs'⟩ => match hm.1.2.1 s' (ing_dom M hk' hs').2 with
                  | ⟨a', hsa'⟩ =>
                    have ha' := (rk' c a').1 ⟨s', hs', hsa'⟩
                    have e : Ceq M.R s' s := hm.2 s' s a' hsa'
                      (ing_resp M (ceq_refl M.R m) (ceq_refl M.R s) (hh.2.2 c a a' ha ha') hsa)
                    exact ing_resp M (ceq_refl M.R k') (ceq_refl M.R c) e hs'
        · intro χ' hχ' htr hpb
          have I' : ∀ a, In M a A → (InG M χ' a o ↔ Im a) := fun a _ =>
            ⟨fact χ' hχ' hpb a, fun ⟨s, hsa⟩ => htr s a hsa⟩
          refine fn_ext M hχ' hχf (fun a v => ⟨fun h1 => ?_, fun h1 => ?_⟩)
          · have ha := (ing_dom M hχ' h1).1
            match (hΩ v).1 (ing_dom M hχ' h1).2 with
            | .inr e =>
              have h2 := (χI a ha).2 ((I' a ha).1 (ing_resp M (ceq_refl M.R χ') (ceq_refl M.R a) e h1))
              exact ing_resp M (ceq_refl M.R χ) (ceq_refl M.R a) (ceq_symm M.R e) h2
            | .inl e => exact (ingχ a v).2 ⟨ha, .inr ⟨fun i => zo (ceq_trans M.R (ceq_symm M.R e)
                (hχ'.2.2 a v o h1 ((I' a ha).2 i))), e⟩⟩
          · have ha := (ing_dom M hχf h1).1
            match ((ingχ a v).1 h1).2 with
            | .inl ⟨i, e⟩ => exact ing_resp M (ceq_refl M.R χ') (ceq_refl M.R a) (ceq_symm M.R e) ((I' a ha).2 i)
            | .inr ⟨ni, e⟩ => match hχ'.2.1 a ha with
              | ⟨w, hw⟩ => match (hΩ w).1 (ing_dom M hχ' hw).2 with
                | .inl ew => exact ing_resp M (ceq_refl M.R χ') (ceq_refl M.R a) (ceq_trans M.R ew (ceq_symm M.R e)) hw
                | .inr ew => exact (ni ((I' a ha).1 (ing_resp M (ceq_refl M.R χ') (ceq_refl M.R a) ew hw))).elim

end IZ

end Found

#print axioms Found.IZ.empty_ex'
#print axioms Found.IZ.classifier

found/EtZ6.lean

lean_all/found/EtZ6.lean

/-
  T209 (Lawvere–Mitchell), direction A, part 6. found/EtZ6 — the natural
  numbers object of the category of sets of a model of ZF: ω with zero ∅
  and the successor function n ↦ n ∪ {n}; for every set X, x0 ∈ X and
  f : X → X there is exactly one u : ω → X with u(0) = x0 and
  u(n + 1) = f(u(n)). The function u is the least set of pairs closed under
  the two equations (separation in ω × X); it is a function by induction
  on ω.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.EtZ5
import found.IZ6

namespace Found

namespace IZ

open FO FOCT NBGc

variable (M : ZM)

/-- m = n ∪ {n} -/
def Succ (m n : M.D) : Prop := ∀ s, In M s m ↔ (In M s n ∨ Ceq M.R s n)

theorem def_Succ (i j : Nat) : Def M (fun v => Succ M (v i) (v j)) :=
  def_congr M (fun _ => Iff.rfl) (def_all M (def_iff M (def_mem M 0 (i + 1))
    (def_or M (def_mem M 0 (j + 1)) (def_ceq M 0 (j + 1)))))

theorem succ_resp {m n m' n' : M.D} (em : Ceq M.R m m') (en : Ceq M.R n n') (h : Succ M m n) : Succ M m' n' :=
  fun s => ((em s).symm.trans (h s)).trans
    ⟨fun o => o.elim (fun a => .inl ((en s).1 a)) (fun c => .inr (ceq_trans M.R c en)),
     fun o => o.elim (fun a => .inl ((en s).2 a)) (fun c => .inr (ceq_trans M.R c (ceq_symm M.R en)))⟩

/-- the successor is injective on transitive sets -/
theorem succ_inj {m n n' : M.D} (hn : Trans M n) (h1 : Succ M m n) (h2 : Succ M m n') : Ceq M.R n n' :=
  match (h2 n).1 ((h1 n).2 (.inr (ceq_refl M.R n))), (h1 n').1 ((h2 n').2 (.inr (ceq_refl M.R n'))) with
  | _, .inr b => ceq_symm M.R b
  | .inr a, .inl _ => a
  | .inl a, .inl b => (not_mem_self M n (hn n' n b a)).elim

/-- a set of pairs closed under the recursion equations -/
def Cl (R e x0 f : M.D) : Prop :=
  (∀ p, OPs M.R p e x0 → In M p R) ∧
  ∀ p n x m y q, In M p R → OPs M.R p n x → Succ M m n → InG M f x y → OPs M.R q m y → In M q R

theorem def_Cl (r a b c : Nat) : Def M (fun v => Cl M (v r) (v a) (v b) (v c)) :=
  def_and M (def_congr M (fun _ => Iff.rfl) (def_all M (def_imp M (def_op M 0 (a + 1) (b + 1)) (def_mem M 0 (r + 1)))))
    (def_congr M (fun _ => Iff.rfl) (def_all M (def_all M (def_all M (def_all M (def_all M (def_all M
      (def_imp M (def_mem M 5 (r + 6)) (def_imp M (def_op M 5 4 3) (def_imp M (def_Succ M 2 4)
        (def_imp M (def_InG M (c + 6) 3 1) (def_imp M (def_op M 0 2 1) (def_mem M 0 (r + 6))))))))))))))

/-- ω is a natural numbers object -/
theorem nno : ∃ w e sc, In M e w ∧ (∀ s, ¬ In M s e) ∧ IsFn M w w sc ∧ (∀ n m, InG M sc n m ↔ In M n w ∧ Succ M m n) ∧
    ∀ X x0 f, In M x0 X → IsFn M X X f → ∃ u, IsFn M w X u ∧ InG M u e x0 ∧
      (∀ n m x y, InG M sc n m → InG M u n x → InG M f x y → InG M u m y) ∧
      ∀ u', IsFn M w X u' → InG M u' e x0 → (∀ n m x y, InG M sc n m → InG M u' n x → InG M f x y → InG M u' m y) →
        Ceq M.R u' u := by
  match omega_exists M with
  | ⟨w, hw, hmin⟩ =>
  have word := omega_ord M hw hmin
  match hw.1 with
  | ⟨e, he, hne⟩ =>
  have msucc : ∀ n m, In M n w → Succ M m n → In M m w := fun n m hn hs => match hw.2 n hn with
    | ⟨y, hy, hys⟩ => mem_ceq_l M (fun s => (hys s).trans (hs s).symm) hy
  -- the successor function
  let Qs : (Nat → M.D) → Prop := fun v => ∃ n m, OPs M.R (v 0) n m ∧ Succ M m n
  have hQs : Def M Qs := def_congr M (fun _ => Iff.rfl) (def_ex M (def_ex M (def_and M (def_op M 2 1 0) (def_Succ M 0 1))))
  match prod_ex M w w with
  | ⟨ww, hww⟩ => match sep M hQs (fun _ => M.d0) ww with
  | ⟨sc, hsc⟩ =>
  have ingSc : ∀ n m, InG M sc n m ↔ In M n w ∧ Succ M m n := fun n m =>
    ⟨fun ⟨r, hr, hop⟩ => match ((hsc r).1 hr).2, (hww r).1 ((hsc r).1 hr).1 with
      | ⟨n0, m0, hr0, hs0⟩, ⟨n1, _, hn1, _, hr1⟩ =>
        have e0 := op_inj' M hr0 hop
        have e1 := op_inj' M hr1 hop
        ⟨mem_ceq_l M e1.1 hn1, succ_resp M e0.2 e0.1 hs0⟩,
     fun ⟨hn, hs⟩ => match op_exists M n m with
      | ⟨r, hr⟩ => ⟨r, (hsc r).2 ⟨(hww r).2 ⟨n, m, hn, msucc n m hn hs, hr⟩, n, m, hr, hs⟩, hr⟩⟩
  have scF : IsFn M w w sc := ⟨fun r hr => (hww r).1 ((hsc r).1 hr).1,
    fun n hn => match hw.2 n hn with
      | ⟨y, _, hys⟩ => ⟨y, (ingSc n y).2 ⟨hn, hys⟩⟩,
    fun n m m' h1 h2 => fun s => (((ingSc n m).1 h1).2 s).trans (((ingSc n m').1 h2).2 s).symm⟩
  refine ⟨w, e, sc, he, hne, scF, ingSc, fun X x0 f hx0 hf => ?_⟩
  -- the least closed set of pairs
  let Pu : (Nat → M.D) → Prop := fun v => ∀ R, Cl M R (v 1) (v 2) (v 3) → In M (v 0) R
  have hPu : Def M Pu := def_congr M (fun _ => Iff.rfl) (def_all M (def_imp M (def_Cl M 0 2 3 4) (def_mem M 1 0)))
  match prod_ex M w X with
  | ⟨wX, hwX⟩ => match sep M hPu (fun n => match n with | 0 => e | 1 => x0 | _ => f) wX with
  | ⟨u, hu⟩ =>
  have clU : Cl M u e x0 f :=
    ⟨fun p hp => (hu p).2 ⟨(hwX p).2 ⟨e, x0, he, hx0, hp⟩, fun R hR => hR.1 p hp⟩,
     fun p n x m y q hp hpo hs hxy hq =>
      have hp' := (hu p).1 hp
      match (hwX p).1 hp'.1 with
      | ⟨n1, _, hn1, _, hp1⟩ =>
        have hn : In M n w := mem_ceq_l M (op_inj' M hp1 hpo).1 hn1
        (hu q).2 ⟨(hwX q).2 ⟨m, y, msucc n m hn hs, (ing_dom M hf hxy).2, hq⟩,
          fun R hR => hR.2 p n x m y q (hp'.2 R hR) hpo hs hxy hq⟩⟩
  have uSub : ∀ R, Cl M R e x0 f → ∀ p, In M p u → In M p R := fun R hR p hp => ((hu p).1 hp).2 R hR
  have uDom : ∀ n x, InG M u n x → In M n w ∧ In M x X := fun n x ⟨p, hp, hop⟩ =>
    match (hwX p).1 ((hu p).1 hp).1 with
    | ⟨n1, x1, hn1, hx1, hp1⟩ =>
      have e1 := op_inj' M hp1 hop
      ⟨mem_ceq_l M e1.1 hn1, mem_ceq_l M e1.2 hx1⟩
  -- the value at zero is unique
  let Q0 : (Nat → M.D) → Prop := fun v => ∀ x, OPs M.R (v 0) (v 1) x → Ceq M.R x (v 2)
  have hQ0 : Def M Q0 := def_congr M (fun _ => Iff.rfl) (def_all M (def_imp M (def_op M 1 2 0) (def_ceq M 0 3)))
  have zero_uniq : ∀ x, InG M u e x → Ceq M.R x x0 := match sep M hQ0 (fun n => match n with | 0 => e | _ => x0) u with
    | ⟨R0, hR0⟩ =>
      have cl0 : Cl M R0 e x0 f :=
        ⟨fun p hp => (hR0 p).2 ⟨clU.1 p hp, fun x hx => ceq_symm M.R (op_inj' M hp hx).2⟩,
         fun p n x m y q hp hpo hs hxy hq => (hR0 q).2 ⟨clU.2 p n x m y q ((hR0 p).1 hp).1 hpo hs hxy hq,
          fun x' hq' => (hne n (mem_ceq_r M (op_inj' M hq hq').1 ((hs n).2 (.inr (ceq_refl M.R n))))).elim⟩⟩
      fun x ⟨p, hp, hop⟩ => ((hR0 p).1 (uSub R0 cl0 p hp)).2 x hop
  -- induction on ω: one value at each n
  let QT : (Nat → M.D) → Prop := fun v => (∃ x, InG M (v 1) (v 0) x) ∧ ∀ x x', InG M (v 1) (v 0) x → InG M (v 1) (v 0) x' → Ceq M.R x x'
  have hQT : Def M QT := def_and M (def_congr M (fun _ => Iff.rfl) (def_ex M (def_InG M 2 1 0)))
    (def_congr M (fun _ => Iff.rfl) (def_all M (def_all M (def_imp M (def_InG M 3 2 1) (def_imp M (def_InG M 3 2 0) (def_ceq M 1 0))))))
  match sep M hQT (fun _ => u) w with
  | ⟨T, hT⟩ =>
  have ex0 : InG M u e x0 := match op_exists M e x0 with
    | ⟨p, hp⟩ => ⟨p, clU.1 p hp, hp⟩
  have stepT : ∀ n, In M n T → ∀ m, In M m w → Succ M m n → In M m T := fun n hnT m hm hs => by
    match (hT n).1 hnT with
    | ⟨hn, ⟨x, hx⟩, huniq⟩ =>
    match hf.2.1 x (uDom n x hx).2 with
    | ⟨y, hfy⟩ =>
    -- every value at m comes from a value at n
    let Q1 : (Nat → M.D) → Prop := fun v => ∀ y', OPs M.R (v 0) (v 1) y' → ∃ x', InG M (v 2) (v 3) x' ∧ InG M (v 4) x' y'
    have hQ1 : Def M Q1 := def_congr M (fun _ => Iff.rfl) (def_all M (def_imp M (def_op M 1 2 0)
      (def_ex M (def_and M (def_InG M 4 5 0) (def_InG M 6 0 1)))))
    match sep M hQ1 (fun k => match k with | 0 => m | 1 => u | 2 => n | _ => f) u with
    | ⟨R1, hR1⟩ =>
    have cl1 : Cl M R1 e x0 f :=
      ⟨fun p hp => (hR1 p).2 ⟨clU.1 p hp, fun y' hy' =>
          (hne n (mem_ceq_r M (ceq_symm M.R (op_inj' M hp hy').1) ((hs n).2 (.inr (ceq_refl M.R n))))).elim⟩,
       fun p n1 x1 m1 y1 q hp hpo hs1 hxy1 hq => (hR1 q).2 ⟨clU.2 p n1 x1 m1 y1 q ((hR1 p).1 hp).1 hpo hs1 hxy1 hq,
        fun y' hq' =>
          have eo := op_inj' M hq hq'
          have hun : InG M u n1 x1 := ⟨p, ((hR1 p).1 hp).1, hpo⟩
          have hn1 : Ord M n1 := ord_mem M word (uDom n1 x1 hun).1
          have en : Ceq M.R n1 n := succ_inj M hn1.1 (succ_resp M eo.1 (ceq_refl M.R n1) hs1) hs
          ⟨x1, ing_resp M (ceq_refl M.R u) en (ceq_refl M.R x1) hun, ing_resp M (ceq_refl M.R f) (ceq_refl M.R x1) eo.2 hxy1⟩⟩⟩
    have back : ∀ y', InG M u m y' → InG M f x y' := fun y' ⟨p, hp, hop⟩ =>
      match ((hR1 p).1 (uSub R1 cl1 p hp)).2 y' hop with
      | ⟨x', hx', hfx'⟩ => ing_resp M (ceq_refl M.R f) (huniq x' x hx' hx) (ceq_refl M.R y') hfx'
    match op_exists M m y with
    | ⟨q, hq⟩ =>
    match hx with
    | ⟨p, hp, hop⟩ =>
    exact (hT m).2 ⟨hm, ⟨y, q, clU.2 p n x m y q hp hop hs hfy hq, hq⟩,
      fun y1 y2 h1 h2 => hf.2.2 x y1 y2 (back y1 h1) (back y2 h2)⟩
  have indT : Ind M T :=
    ⟨⟨e, (hT e).2 ⟨he, ⟨x0, ex0⟩, fun x x' h1 h2 => ceq_trans M.R (zero_uniq x h1) (ceq_symm M.R (zero_uniq x' h2))⟩, hne⟩,
     fun n hnT => match hw.2 n ((hT n).1 hnT).1 with
      | ⟨m, hm, hs⟩ => ⟨m, stepT n hnT m hm hs, hs⟩⟩
  have allT : ∀ n, In M n w → In M n T := fun n hn => hmin n hn T indT
  have uF : IsFn M w X u := ⟨fun p hp => (hwX p).1 ((hu p).1 hp).1, fun n hn => ((hT n).1 (allT n hn)).2.1,
    fun n x x' h1 h2 => ((hT n).1 (allT n (uDom n x h1).1)).2.2 x x' h1 h2⟩
  refine ⟨u, uF, ex0, fun n m x y hsc ⟨p, hp, hop⟩ hfy => match op_exists M m y with
    | ⟨q, hq⟩ => ⟨q, clU.2 p n x m y q hp hop ((ingSc n m).1 hsc).2 hfy hq, hq⟩, fun u' hu' h0 hst => ?_⟩
  have clU' : Cl M u' e x0 f :=
    ⟨fun p hp => match h0 with
      | ⟨p', hp', hop'⟩ => mem_ceq_l M (op_uniq M hop' hp) hp',
     fun p n x m y q hp hpo hs hxy hq =>
      match hst n m x y ((ingSc n m).2 ⟨(ing_dom M hu' ⟨p, hp, hpo⟩).1, hs⟩) ⟨p, hp, hpo⟩ hxy with
      | ⟨q', hq', hoq'⟩ => mem_ceq_l M (op_uniq M hoq' hq) hq'⟩
  exact fn_ext M hu' uF (fun n x => ⟨fun h => match uF.2.1 n (ing_dom M hu' h).1 with
      | ⟨x', ⟨p, hp, hop⟩⟩ => ing_resp M (ceq_refl M.R u) (ceq_refl M.R n)
          (hu'.2.2 n x' x ⟨p, uSub u' clU' p hp, hop⟩ h) ⟨p, hp, hop⟩,
    fun ⟨p, hp, hop⟩ => ⟨p, uSub u' clU' p hp, hop⟩⟩)

end IZ

end Found

#print axioms Found.IZ.def_Succ
#print axioms Found.IZ.succ_resp
#print axioms Found.IZ.succ_inj
#print axioms Found.IZ.def_Cl
#print axioms Found.IZ.nno

found/EtZ7.lean

lean_all/found/EtZ7.lean

/-
  T209 (Lawvere–Mitchell), direction A, part 7. found/EtZ7 — the category
  of sets of a model of ZF is well-pointed, and in a model of ZFC every
  surjection splits:
  * points: every a ∈ A is a point {⟨t, a⟩} : 1 → A; two functions A → B
    that agree after every point are equal;
  * choice: for a surjective f : A → B there is s : B → A with f ∘ s = id
    (the choice function of found/IZ11 on the set of fibers).

  Lean 4 core only, hand-written proofs, no axioms (classical logic and
  choice only as the parameters K and hac).
-/
import found.EtZ6

namespace Found

namespace IZ

open FO FOCT NBGc

variable (M : ZM)

/-- the point {⟨t, a⟩} : {t} → A -/
theorem point_ex {T A t a : M.D} (hT : ∀ s, In M s T ↔ Ceq M.R s t) (ha : In M a A) :
    ∃ x, IsFn M T A x ∧ InG M x t a :=
  match op_exists M t a with
  | ⟨q, hq⟩ => match sing_exists M q with
    | ⟨x, hx⟩ =>
      have ing : ∀ s b, InG M x s b → Ceq M.R s t ∧ Ceq M.R b a := fun _ _ ⟨p, hp, hop⟩ =>
        have e := op_inj' M (op_resp M hq (ceq_symm M.R ((hx p).1 hp)) (ceq_refl M.R t) (ceq_refl M.R a)) hop
        ⟨ceq_symm M.R e.1, ceq_symm M.R e.2⟩
      ⟨x, ⟨fun p hp => ⟨t, a, (hT t).2 (ceq_refl M.R t), ha,
          op_resp M hq (ceq_symm M.R ((hx p).1 hp)) (ceq_refl M.R t) (ceq_refl M.R a)⟩,
        fun s hs => ⟨a, q, (hx q).2 (ceq_refl M.R q), op_resp M hq (ceq_refl M.R q) (ceq_symm M.R ((hT s).1 hs)) (ceq_refl M.R a)⟩,
        fun s b b' h1 h2 => ceq_trans M.R (ing s b h1).2 (ceq_symm M.R (ing s b' h2).2)⟩,
       q, (hx q).2 (ceq_refl M.R q), hq⟩

/-- well-pointedness: functions agreeing after every point are equal -/
theorem well_pointed {T A B f g t : M.D} (hT : ∀ s, In M s T ↔ Ceq M.R s t) (hf : IsFn M A B f) (hg : IsFn M A B g)
    (h : ∀ x, IsFn M T A x → ∀ s b, RC M x f s b ↔ RC M x g s b) : Ceq M.R f g :=
  fn_ext M hf hg (fun a b =>
    ⟨fun fab => match point_ex M hT (ing_dom M hf fab).1 with
      | ⟨x, hx, xta⟩ => match (h x hx t b).1 ⟨a, xta, fab⟩ with
        | ⟨a', xta', gab⟩ => ing_resp M (ceq_refl M.R g) (hx.2.2 t a' a xta' xta) (ceq_refl M.R b) gab,
     fun gab => match point_ex M hT (ing_dom M hg gab).1 with
      | ⟨x, hx, xta⟩ => match (h x hx t b).2 ⟨a, xta, gab⟩ with
        | ⟨a', xta', fab⟩ => ing_resp M (ceq_refl M.R f) (hx.2.2 t a' a xta' xta) (ceq_refl M.R b) fab⟩)

/-- choice: every surjection has a section -/
theorem section_ex (K : CP.Classical') (hac : ACsem M) {A B f : M.D} (hf : IsFn M A B f)
    (hs : ∀ b, In M b B → ∃ a, InG M f a b) :
    ∃ s, IsFn M B A s ∧ ∀ b a, InG M s b a → InG M f a b := by
  let Fb : (Nat → M.D) → Prop := fun u => ∀ a, In M a (u 1) ↔ In M a (u 2) ∧ InG M (u 3) a (u 0)
  have hFb : Def M Fb := def_congr M (fun _ => Iff.rfl) (def_all M (def_iff M (def_mem M 0 2)
    (def_and M (def_mem M 0 3) (def_InG M 4 0 1))))
  have fib_ex : ∀ b, ∃ φ, ∀ a, In M a φ ↔ In M a A ∧ InG M f a b := fun b =>
    let Q : (Nat → M.D) → Prop := fun u => InG M (u 1) (u 0) (u 2)
    match sep M (P := Q) (def_InG M 1 0 2) (fun n => match n with | 0 => f | _ => b) A with
    | ⟨φ, hφ⟩ => ⟨φ, hφ⟩
  match repl M hFb (fun n => match n with | 0 => A | _ => f) B (fun _ _ z z' h1 h2 a => (h1 a).trans (h2 a).symm) with
  | ⟨Fib, hFib⟩ =>
  match choice_fn M K hac Fib (fun φ hφ => match (hFib φ).1 hφ with
      | ⟨b, hb, hfb⟩ => match hs b hb with
        | ⟨a, fab⟩ => ⟨a, (hfb a).2 ⟨(ing_dom M hf fab).1, fab⟩⟩) with
  | ⟨c, hc1, hc2⟩ =>
  let Qs : (Nat → M.D) → Prop := fun v => ∃ b a, OPs M.R (v 0) b a ∧
    ∃ φ, (∀ t, In M t φ ↔ In M t (v 1) ∧ InG M (v 2) t b) ∧ InG M (v 3) φ a
  have hQs : Def M Qs := def_congr M (fun _ => Iff.rfl) (def_ex M (def_ex M (def_and M (def_op M 2 1 0)
    (def_ex M (def_and M (def_all M (def_iff M (def_mem M 0 1) (def_and M (def_mem M 0 5) (def_InG M 6 0 3))))
      (def_InG M 6 0 1))))))
  match prod_ex M B A with
  | ⟨BA, hBA⟩ => match sep M hQs (fun n => match n with | 0 => A | 1 => f | _ => c) BA with
  | ⟨s, hsS⟩ =>
  have ingS : ∀ b a, InG M s b a ↔ In M b B ∧ ∃ φ, (∀ t, In M t φ ↔ In M t A ∧ InG M f t b) ∧ InG M c φ a := fun b a =>
    ⟨fun ⟨r, hr, hop⟩ => match ((hsS r).1 hr).2, (hBA r).1 ((hsS r).1 hr).1 with
      | ⟨b0, a0, hr0, φ, hφ, cφ⟩, ⟨b1, _, hb1, _, hr1⟩ =>
        have e := op_inj' M hr0 hop
        have e1 := op_inj' M hr1 hop
        ⟨mem_ceq_l M e1.1 hb1, φ, fun t => (hφ t).trans ⟨fun ⟨h1, h2⟩ => ⟨h1, ing_resp M (ceq_refl M.R f) (ceq_refl M.R t) e.1 h2⟩,
            fun ⟨h1, h2⟩ => ⟨h1, ing_resp M (ceq_refl M.R f) (ceq_refl M.R t) (ceq_symm M.R e.1) h2⟩⟩,
          ing_resp M (ceq_refl M.R c) (ceq_refl M.R φ) e.2 cφ⟩,
     fun ⟨hb, φ, hφ, cφ⟩ =>
      have hφF : In M φ Fib := (hFib φ).2 ⟨b, hb, hφ⟩
      have ha : In M a A := match hc1 φ hφF with
        | ⟨t, ht, ct⟩ => ((hφ a).1 (mem_ceq_l M (hc2 φ t a hφF ct cφ) ht)).1
      match op_exists M b a with
      | ⟨r, hr⟩ => ⟨r, (hsS r).2 ⟨(hBA r).2 ⟨b, a, hb, ha, hr⟩, b, a, hr, φ, hφ, cφ⟩, hr⟩⟩
  have sec : ∀ b a, InG M s b a → InG M f a b := fun b a h => match (ingS b a).1 h with
    | ⟨hb, φ, hφ, cφ⟩ =>
      have hφF : In M φ Fib := (hFib φ).2 ⟨b, hb, hφ⟩
      match hc1 φ hφF with
      | ⟨t, ht, ct⟩ => ((hφ a).1 (mem_ceq_l M (hc2 φ t a hφF ct cφ) ht)).2
  refine ⟨s, ⟨fun r hr => (hBA r).1 ((hsS r).1 hr).1, fun b hb => ?_, fun b a a' h1 h2 => ?_⟩, sec⟩
  · match fib_ex b with
    | ⟨φ, hφ⟩ => match hc1 φ ((hFib φ).2 ⟨b, hb, hφ⟩) with
      | ⟨t, _, ct⟩ => exact ⟨t, (ingS b t).2 ⟨hb, φ, hφ, ct⟩⟩
  · match (ingS b a).1 h1, (ingS b a').1 h2 with
    | ⟨hb, φ, hφ, cφ⟩, ⟨_, φ', hφ', cφ'⟩ =>
      have e : Ceq M.R φ' φ := fun t => (hφ' t).trans (hφ t).symm
      exact hc2 φ a a' ((hFib φ).2 ⟨b, hb, hφ⟩) cφ (ing_resp M (ceq_refl M.R c) e (ceq_refl M.R a') cφ')

end IZ

end Found

#print axioms Found.IZ.point_ex
#print axioms Found.IZ.well_pointed
#print axioms Found.IZ.section_ex

found/FOClassical.lean

lean_all/found/FOClassical.lean

/-
  T208 (L2), the two-valued classical case. found/FOClassical — Gödel's
  completeness theorem in the classical point (excluded middle and choice as
  the data of found/ClassicalPoint, not as Lean axioms):
  * classical derivability: natural deduction (found/FOCompleteness) from
    instances of excluded middle and their universal closures;
  * two-valued Tarski semantics over any domain;
  * soundness: derivable ⟹ true in every two-valued structure;
  * completeness: true in every two-valued structure ⟹ derivable, by a
    Henkin construction: formulas are enumerated (an injective code into ℕ
    through the Hessenberg pairing), a consistent list is extended step by
    step deciding each formula, with a witness variable for every ∃ added and
    a counter-witness for every ∀ refuted (fresh-variable renaming moves the
    used excluded-middle instances under the quantifier), and the term model
    on ℕ satisfies exactly the formulas collected (truth lemma).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.FOCompact
import found.Hessenberg

namespace Found

namespace FOC

open FO CP Card

/-! ## classical derivability -/

def neg (a : Fm) : Fm := .imp a .bot
def lemF (a : Fm) : Fm := .disj a (neg a)

def clo : Nat → Fm → Fm
  | 0, a => a
  | j + 1, a => .all (clo j a)

def InT (δ : Fm) : Prop := ∃ j a, δ = clo j (lemF a)
def CtxT (Δ : List Fm) : Prop := ∀ δ, δ ∈ Δ → InT δ

/-- derivable from finitely many excluded-middle closures -/
def PC (S : List Fm) (φ : Fm) : Prop := ∃ Δ, CtxT Δ ∧ Pf (Δ ++ S) φ

def Con (S : List Fm) : Prop := ¬ PC S .bot

theorem pc_of {S : List Fm} {φ : Fm} (d : Pf S φ) : PC S φ := ⟨[], ⟨(fun _ m => nomatch m), d⟩⟩

/-! ## list and context helpers -/

theorem sub_trans' {A B C : List Fm} (h1 : Sub A B) (h2 : Sub B C) : Sub A C := fun a m => h2 a (h1 a m)
theorem sub_tl (y : Fm) (A : List Fm) : Sub A (y :: A) := fun _ m => .tail _ m
theorem sub_appL' (A B : List Fm) : Sub A (A ++ B) := fun _ m => mem_app_l' A B m
theorem sub_appR' (A B : List Fm) : Sub B (A ++ B) := fun _ m => mem_app_r' A B m
theorem sub_app_of {A B T : List Fm} (h1 : Sub A T) (h2 : Sub B T) : Sub (A ++ B) T := fun a m =>
  (mem_app_inv' A B m).elim (h1 a) (h2 a)
theorem sub_cons_of {x : Fm} {A T : List Fm} (hx : x ∈ T) (h : Sub A T) : Sub (x :: A) T := fun a m => by
  cases m with
  | head => exact hx
  | tail _ m' => exact h a m'
theorem sub_nil (T : List Fm) : Sub [] T := fun _ m => nomatch m

theorem ctx_app' {A B : List Fm} (h1 : CtxT A) (h2 : CtxT B) : CtxT (A ++ B) := fun a m =>
  (mem_app_inv' A B m).elim (h1 a) (h2 a)
theorem ctx_cons {x : Fm} {A : List Fm} (hx : InT x) (h : CtxT A) : CtxT (x :: A) := fun a m => by
  cases m with
  | head => exact hx
  | tail _ m' => exact h a m'
theorem lem_in (a : Fm) : InT (lemF a) := ⟨0, a, rfl⟩

theorem mapF_app (ρ : Nat → Nat) : (A B : List Fm) → mapF ρ (A ++ B) = mapF ρ A ++ mapF ρ B
  | [], _ => rfl
  | a :: A, B => congrArg (List.cons (ren ρ a)) (mapF_app ρ A B)

theorem mem_mapF_inv (ρ : Nat → Nat) {a : Fm} : (Γ : List Fm) → a ∈ mapF ρ Γ → ∃ b, b ∈ Γ ∧ a = ren ρ b
  | [], m => nomatch m
  | b :: Γ, m => by
    cases m with
    | head => exact ⟨b, .head _, rfl⟩
    | tail _ m' => exact match mem_mapF_inv ρ Γ m' with | ⟨c, hc, e⟩ => ⟨c, .tail _ hc, e⟩

def upn : Nat → (Nat → Nat) → (Nat → Nat)
  | 0, ρ => ρ
  | j + 1, ρ => upn j (up ρ)

theorem ren_clo : (j : Nat) → (ρ : Nat → Nat) → (x : Fm) → ren ρ (clo j x) = clo j (ren (upn j ρ) x)
  | 0, _, _ => rfl
  | j + 1, ρ, x => by
    show Fm.all (ren (up ρ) (clo j x)) = Fm.all (clo j (ren (upn j (up ρ)) x))
    rw [ren_clo j (up ρ) x]

theorem inT_ren (ρ : Nat → Nat) {δ : Fm} (h : InT δ) : InT (ren ρ δ) :=
  match h with
  | ⟨j, a, e⟩ => ⟨j, ren (upn j ρ) a, by rw [e, ren_clo]; rfl⟩

theorem ctx_ren (ρ : Nat → Nat) {Δ : List Fm} (h : CtxT Δ) : CtxT (mapF ρ Δ) := fun _ m =>
  match mem_mapF_inv ρ Δ m with
  | ⟨b, hb, e⟩ => e ▸ inT_ren ρ (h b hb)

def allL : List Fm → List Fm
  | [] => []
  | δ :: Δ => .all δ :: allL Δ

theorem ctx_allL : {Δ : List Fm} → CtxT Δ → CtxT (allL Δ)
  | [], _ => fun _ m => nomatch m
  | _ :: Δ, h => ctx_cons (match h _ (.head _) with
      | ⟨j, a, e⟩ => ⟨j + 1, a, by rw [e]; rfl⟩) (ctx_allL (fun b m => h b (.tail _ m)))

theorem mem_allL {δ : Fm} : (Δ : List Fm) → δ ∈ Δ → Fm.all δ ∈ allL Δ
  | [], m => nomatch m
  | _ :: Δ, m => by
    cases m with
    | head => exact .head _
    | tail _ m' => exact .tail _ (mem_allL Δ m')

theorem inst0_shift (δ : Fm) : inst 0 (ren (up Nat.succ) δ) = δ := by
  unfold inst
  rw [ren_ren]
  exact (ren_ext δ (σ := fun n => n) (fun n => match n with
    | 0 => rfl
    | _ + 1 => rfl)).trans (ren_id δ)

/-- the closures, shifted under a binder, give back the instances -/
theorem pf_allL (Δ T : List Fm) (h : Sub (sh (allL Δ)) T) : ∀ δ, δ ∈ Δ → Pf T δ := fun δ m => by
  have m1 : shift (Fm.all δ) ∈ sh (allL Δ) := mem_mapF Nat.succ _ (mem_allL Δ m)
  have p : Pf T (Fm.all (ren (up Nat.succ) δ)) := .hyp (h _ m1)
  have q := Pf.allE 0 p
  rw [inst0_shift] at q
  exact q

/-- discharge hypotheses that are derivable -/
theorem discharge : (Δ : List Fm) → {L T : List Fm} → {φ : Fm} → Pf (Δ ++ L) φ → Sub L T →
    (∀ δ, δ ∈ Δ → Pf T δ) → Pf T φ
  | [], _, _, _, d, hL, _ => weaken d hL
  | δ :: Δ, _, _, _, d, hL, hΔ =>
    .impE (discharge Δ (Pf.impI d) hL (fun b m => hΔ b (.tail _ m))) (hΔ δ (.head _))

def boundL : List Fm → Nat
  | [] => 0
  | a :: S => bound a + boundL S

theorem bound_le : (S : List Fm) → {s : Fm} → s ∈ S → bound s ≤ boundL S
  | [], _, m => nomatch m
  | _ :: S, _, m => by
    cases m with
    | head => exact Nat.le_add_right _ _
    | tail _ m' => exact Nat.le_trans (bound_le S m') (Nat.le_add_left _ _)

theorem mapF_sig (N : Nat) : (S : List Fm) → (∀ s, s ∈ S → bound s ≤ N) → mapF (sig N) S = sh S
  | [], _ => rfl
  | a :: S, h => by
    show ren (sig N) a :: mapF (sig N) S = shift a :: sh S
    rw [sig_shift N a (h a (.head _)), mapF_sig N S (fun s m => h s (.tail _ m))]

/-! ## consistency steps -/

variable (K : Classical')

theorem con_mono {S S' : List Fm} (h : Con S') (hs : Sub S S') : Con S := fun ⟨Δ, c, d⟩ =>
  h ⟨Δ, c, weaken d (sub_app_of (sub_appL' Δ S') (sub_trans' hs (sub_appR' Δ S')))⟩

include K in
/-- deciding a formula keeps consistency -/
theorem decide_con (S : List Fm) (ψ : Fm) (h : Con S) : Con (ψ :: S) ∨ Con (neg ψ :: S) :=
  (K.lem (Con (ψ :: S))).elim Or.inl (fun h1 => (K.lem (Con (neg ψ :: S))).elim Or.inr (fun h2 =>
    (h (match dne K _ h1, dne K _ h2 with
      | ⟨Δ1, c1, d1⟩, ⟨Δ2, c2, d2⟩ =>
        let T := lemF ψ :: ((Δ1 ++ Δ2) ++ S)
        ⟨lemF ψ :: (Δ1 ++ Δ2), ctx_cons (lem_in ψ) (ctx_app' c1 c2),
          .disjE (.hyp (.head _))
            (weaken d1 (sub_app_of (sub_trans' (sub_appL' Δ1 Δ2) (sub_trans' (sub_appL' _ S)
                (sub_trans' (sub_tl _ _) (sub_tl _ T))))
              (sub_cons_of (.head _) (sub_trans' (sub_appR' _ S) (sub_trans' (sub_tl _ _) (sub_tl _ T))))))
            (weaken d2 (sub_app_of (sub_trans' (sub_appR' Δ1 Δ2) (sub_trans' (sub_appL' _ S)
                (sub_trans' (sub_tl _ _) (sub_tl _ T))))
              (sub_cons_of (.head _) (sub_trans' (sub_appR' _ S) (sub_trans' (sub_tl _ _) (sub_tl _ T))))))⟩)).elim))

include K in
/-- a consistent ∃ has a consistent witness -/
theorem henkin_ex (S : List Fm) (a : Fm) (h : Con (.ex a :: S)) : ∃ y, Con (inst y a :: S) := by
  refine dne K _ (fun hn => h ?_)
  have hy : PC (inst (boundL (.ex a :: S)) a :: S) .bot := dne K _ (fun hc => hn ⟨_, hc⟩)
  match hy with
  | ⟨Δ, cΔ, d⟩ =>
    have hbS : ∀ s, s ∈ S → bound s ≤ boundL (.ex a :: S) := fun s m => bound_le _ (.tail _ m)
    have hba : bound a ≤ boundL (.ex a :: S) + 1 := Nat.le_succ_of_le (bound_le (.ex a :: S) (s := .ex a) (.head _))
    have d1 := pf_ren d (sig (boundL (.ex a :: S)))
    rw [mapF_app] at d1
    have e1 : mapF (sig (boundL (.ex a :: S))) (inst (boundL (.ex a :: S)) a :: S) = a :: sh S := by
      show ren (sig _) (inst _ a) :: mapF (sig _) S = a :: sh S
      rw [sig_inst _ a hba, mapF_sig _ S hbS]
    rw [e1] at d1
    let Δ' := mapF (sig (boundL (.ex a :: S))) Δ
    let Γ := allL Δ' ++ .ex a :: S
    have sS : Sub S Γ := sub_trans' (sub_tl _ _) (sub_appR' _ _)
    have d2 : Pf (a :: sh Γ) .bot := discharge Δ' d1 (sub_cons a (sub_sh sS))
      (pf_allL Δ' _ (sub_trans' (sub_sh (sub_appL' _ _)) (sub_tl _ _)))
    exact ⟨allL Δ', ctx_allL (ctx_ren _ cΔ), .exE (.hyp (mem_app_r' _ _ (.head _))) d2⟩

include K in
/-- a consistent refuted ∀ has a consistent counter-witness -/
theorem henkin_all (S : List Fm) (b : Fm) (h : Con (neg (.all b) :: S)) : ∃ y, Con (neg (inst y b) :: S) := by
  refine dne K _ (fun hn => h ?_)
  have hy : PC (neg (inst (boundL (neg (.all b) :: S)) b) :: S) .bot := dne K _ (fun hc => hn ⟨_, hc⟩)
  match hy with
  | ⟨Δ, cΔ, d⟩ =>
    have hbS : ∀ s, s ∈ S → bound s ≤ boundL (neg (.all b) :: S) := fun s m => bound_le _ (.tail _ m)
    have hbb : bound b ≤ boundL (neg (.all b) :: S) + 1 := Nat.le_succ_of_le (bound_le (neg (.all b) :: S) (s := neg (.all b)) (.head _))
    have d1 := pf_ren d (sig (boundL (neg (.all b) :: S)))
    rw [mapF_app] at d1
    have e1 : mapF (sig (boundL (neg (.all b) :: S))) (neg (inst (boundL (neg (.all b) :: S)) b) :: S) =
        neg b :: sh S := by
      show neg (ren (sig _) (inst _ b)) :: mapF (sig _) S = neg b :: sh S
      rw [sig_inst _ b hbb, mapF_sig _ S hbS]
    rw [e1] at d1
    let Δ' := mapF (sig (boundL (neg (.all b) :: S))) Δ
    have d2 : Pf ((lemF b :: Δ') ++ sh S) b :=
      .disjE (.hyp (.head _)) (.hyp (.head _)) (.botE (weaken d1
        (sub_app_of (sub_trans' (sub_appL' Δ' (sh S)) (sub_trans' (sub_tl _ _) (sub_tl _ _)))
          (sub_cons_of (.head _) (sub_trans' (sub_appR' Δ' (sh S)) (sub_trans' (sub_tl _ _) (sub_tl _ _)))))))
    let Δ2 := lemF b :: Δ'
    let Γ := allL Δ2 ++ neg (.all b) :: S
    have sS : Sub S Γ := sub_trans' (sub_tl _ _) (sub_appR' _ _)
    have d3 : Pf (sh Γ) b := discharge Δ2 d2 (sub_sh sS) (pf_allL Δ2 _ (sub_sh (sub_appL' _ _)))
    exact ⟨allL Δ2, ctx_allL (ctx_cons (lem_in b) (ctx_ren _ cΔ)),
      .impE (.hyp (mem_app_r' _ _ (.head _))) (.allI d3)⟩

/-! ## enumeration of formulas -/

def pr (n : Nat × Nat) : Nat :=
  K.ch (fun F : Nat × Nat → Nat => Inj F) (Hess.hessenberg K (e := fun n : Nat => n) (fun _ _ h => h)) n

theorem pr_inj : Inj (pr K) :=
  K.ch_spec (fun F : Nat × Nat → Nat => Inj F) (Hess.hessenberg K (e := fun n : Nat => n) (fun _ _ h => h))

def encL : List Nat → Nat
  | [] => pr K (0, 0)
  | x :: l => pr K (1, pr K (x, encL l))

def enc : Fm → Nat
  | .atom r l => pr K (0, pr K (r, encL K l))
  | .bot => pr K (1, 0)
  | .conj a b => pr K (2, pr K (enc a, enc b))
  | .disj a b => pr K (3, pr K (enc a, enc b))
  | .imp a b => pr K (4, pr K (enc a, enc b))
  | .all a => pr K (5, enc a)
  | .ex a => pr K (6, enc a)

theorem pair_eq {a b c d : Nat} (h : (a, b) = (c, d)) : a = c ∧ b = d :=
  ⟨congrArg Prod.fst h, congrArg Prod.snd h⟩

theorem encL_inj : (l m : List Nat) → encL K l = encL K m → l = m
  | [], [], _ => rfl
  | [], _ :: _, h => Bool.noConfusion (congrArg (fun p : Nat × Nat => Nat.beq p.1 0) (pr_inj K _ _ h))
  | _ :: _, [], h => Bool.noConfusion (congrArg (fun p : Nat × Nat => Nat.beq p.1 1) (pr_inj K _ _ h))
  | x :: l, y :: m, h => by
    have h2 := (pair_eq (pr_inj K _ _ h)).2
    have h3 := pair_eq (pr_inj K _ _ h2)
    rw [h3.1, encL_inj l m h3.2]

theorem enc_inj (φ : Fm) : ∀ ψ, enc K φ = enc K ψ → φ = ψ := by
  induction φ with
  | atom r l =>
    intro ψ h
    cases ψ <;> (try exact Bool.noConfusion (congrArg (fun p : Nat × Nat => Nat.beq p.1 0) (pr_inj K _ _ h)))
    have h3 := pair_eq (pr_inj K _ _ (pair_eq (pr_inj K _ _ h)).2)
    rw [h3.1, encL_inj K _ _ h3.2]
  | bot =>
    intro ψ h
    cases ψ <;> (try exact Bool.noConfusion (congrArg (fun p : Nat × Nat => Nat.beq p.1 1) (pr_inj K _ _ h)))
    rfl
  | conj a b iha ihb =>
    intro ψ h
    cases ψ <;> (try exact Bool.noConfusion (congrArg (fun p : Nat × Nat => Nat.beq p.1 2) (pr_inj K _ _ h)))
    have h3 := pair_eq (pr_inj K _ _ (pair_eq (pr_inj K _ _ h)).2)
    rw [iha _ h3.1, ihb _ h3.2]
  | disj a b iha ihb =>
    intro ψ h
    cases ψ <;> (try exact Bool.noConfusion (congrArg (fun p : Nat × Nat => Nat.beq p.1 3) (pr_inj K _ _ h)))
    have h3 := pair_eq (pr_inj K _ _ (pair_eq (pr_inj K _ _ h)).2)
    rw [iha _ h3.1, ihb _ h3.2]
  | imp a b iha ihb =>
    intro ψ h
    cases ψ <;> (try exact Bool.noConfusion (congrArg (fun p : Nat × Nat => Nat.beq p.1 4) (pr_inj K _ _ h)))
    have h3 := pair_eq (pr_inj K _ _ (pair_eq (pr_inj K _ _ h)).2)
    rw [iha _ h3.1, ihb _ h3.2]
  | all a iha =>
    intro ψ h
    cases ψ <;> (try exact Bool.noConfusion (congrArg (fun p : Nat × Nat => Nat.beq p.1 5) (pr_inj K _ _ h)))
    rw [iha _ (pair_eq (pr_inj K _ _ h)).2]
  | ex a iha =>
    intro ψ h
    cases ψ <;> (try exact Bool.noConfusion (congrArg (fun p : Nat × Nat => Nat.beq p.1 6) (pr_inj K _ _ h)))
    rw [iha _ (pair_eq (pr_inj K _ _ h)).2]

theorem dec_ex (n : Nat) : ∃ ψ, (∃ φ, enc K φ = n) → enc K ψ = n :=
  (K.lem (∃ φ, enc K φ = n)).elim (fun ⟨φ, h⟩ => ⟨φ, fun _ => h⟩) (fun hn => ⟨.bot, fun h => (hn h).elim⟩)

def decF (n : Nat) : Fm := K.ch (fun ψ => (∃ φ, enc K φ = n) → enc K ψ = n) (dec_ex K n)

theorem decF_enc (φ : Fm) : decF K (enc K φ) = φ :=
  enc_inj K _ _ (K.ch_spec (fun ψ => (∃ φ', enc K φ' = enc K φ) → enc K ψ = enc K φ) (dec_ex K _) ⟨φ, rfl⟩)

/-! ## the Henkin sequence -/

theorem con_dup {x : Fm} {S : List Fm} (h : Con (x :: S)) : Con (x :: x :: S) :=
  con_mono h (sub_cons_of (.head _) (fun _ m => m))

include K in
theorem wEx_ex (S : List Fm) (a : Fm) : ∃ y, Con (.ex a :: S) → Con (inst y a :: .ex a :: S) :=
  (K.lem (Con (.ex a :: S))).elim
    (fun hc => match henkin_ex K (.ex a :: S) a (con_dup hc) with | ⟨y, hy⟩ => ⟨y, fun _ => hy⟩)
    (fun hn => ⟨0, fun h => (hn h).elim⟩)

def wEx (S : List Fm) (a : Fm) : Nat := K.ch _ (wEx_ex K S a)

include K in
theorem wAll_ex (S : List Fm) (b : Fm) : ∃ y, Con (neg (.all b) :: S) → Con (neg (inst y b) :: neg (.all b) :: S) :=
  (K.lem (Con (neg (.all b) :: S))).elim
    (fun hc => match henkin_all K (neg (.all b) :: S) b (con_dup hc) with | ⟨y, hy⟩ => ⟨y, fun _ => hy⟩)
    (fun hn => ⟨0, fun h => (hn h).elim⟩)

def wAll (S : List Fm) (b : Fm) : Nat := K.ch _ (wAll_ex K S b)

def stepT (S : List Fm) : Fm → List Fm
  | .atom r l => .atom r l :: S
  | .bot => .bot :: S
  | .conj a b => .conj a b :: S
  | .disj a b => .disj a b :: S
  | .imp a b => .imp a b :: S
  | .all a => .all a :: S
  | .ex a => inst (wEx K S a) a :: .ex a :: S

def stepF (S : List Fm) : Fm → List Fm
  | .atom r l => neg (.atom r l) :: S
  | .bot => neg .bot :: S
  | .conj a b => neg (.conj a b) :: S
  | .disj a b => neg (.disj a b) :: S
  | .imp a b => neg (.imp a b) :: S
  | .all b => neg (inst (wAll K S b) b) :: neg (.all b) :: S
  | .ex a => neg (.ex a) :: S

def step (S : List Fm) (ψ : Fm) : List Fm := cond (dec K (Con (ψ :: S))) (stepT K S ψ) (stepF K S ψ)

theorem stepT_spec (S : List Fm) : (ψ : Fm) → ψ ∈ stepT K S ψ ∧ Sub S (stepT K S ψ)
  | .atom _ _ => ⟨.head _, sub_tl _ _⟩
  | .bot => ⟨.head _, sub_tl _ _⟩
  | .conj _ _ => ⟨.head _, sub_tl _ _⟩
  | .disj _ _ => ⟨.head _, sub_tl _ _⟩
  | .imp _ _ => ⟨.head _, sub_tl _ _⟩
  | .all _ => ⟨.head _, sub_tl _ _⟩
  | .ex _ => ⟨.tail _ (.head _), sub_trans' (sub_tl _ _) (sub_tl _ _)⟩

theorem stepF_spec (S : List Fm) : (ψ : Fm) → neg ψ ∈ stepF K S ψ ∧ Sub S (stepF K S ψ)
  | .atom _ _ => ⟨.head _, sub_tl _ _⟩
  | .bot => ⟨.head _, sub_tl _ _⟩
  | .conj _ _ => ⟨.head _, sub_tl _ _⟩
  | .disj _ _ => ⟨.head _, sub_tl _ _⟩
  | .imp _ _ => ⟨.head _, sub_tl _ _⟩
  | .all _ => ⟨.tail _ (.head _), sub_trans' (sub_tl _ _) (sub_tl _ _)⟩
  | .ex _ => ⟨.head _, sub_tl _ _⟩

theorem con_stepT (S : List Fm) : (ψ : Fm) → Con (ψ :: S) → Con (stepT K S ψ)
  | .atom _ _, h => h
  | .bot, h => h
  | .conj _ _, h => h
  | .disj _ _, h => h
  | .imp _ _, h => h
  | .all _, h => h
  | .ex a, h => K.ch_spec (fun y => Con (.ex a :: S) → Con (inst y a :: .ex a :: S)) (wEx_ex K S a) h

theorem con_stepF (S : List Fm) : (ψ : Fm) → Con (neg ψ :: S) → Con (stepF K S ψ)
  | .atom _ _, h => h
  | .bot, h => h
  | .conj _ _, h => h
  | .disj _ _, h => h
  | .imp _ _, h => h
  | .all b, h => K.ch_spec (fun y => Con (neg (.all b) :: S) → Con (neg (inst y b) :: neg (.all b) :: S))
      (wAll_ex K S b) h
  | .ex _, h => h

theorem step_sub (S : List Fm) (ψ : Fm) : Sub S (step K S ψ) := by
  unfold step
  cases dec K (Con (ψ :: S)) with
  | true => exact (stepT_spec K S ψ).2
  | false => exact (stepF_spec K S ψ).2

theorem con_step (S : List Fm) (ψ : Fm) (h : Con S) : Con (step K S ψ) := by
  unfold step
  cases hd : dec K (Con (ψ :: S)) with
  | true => exact con_stepT K S ψ ((dec_spec K _).1 hd)
  | false =>
    have hn : ¬ Con (ψ :: S) := fun hc => Bool.noConfusion (hd.symm.trans ((dec_spec K _).2 hc))
    exact con_stepF K S ψ ((decide_con K S ψ h).elim (fun hc => (hn hc).elim) id)

section Henkin

variable (S0 : List Fm) (c0 : Con S0)

def seq : Nat → List Fm
  | 0 => S0
  | n + 1 => step K (seq n) (decF K n)

include c0 in
theorem con_seq : (n : Nat) → Con (seq K S0 n)
  | 0 => c0
  | n + 1 => con_step K _ _ (con_seq n)

theorem sub_seq (n : Nat) : (k : Nat) → Sub (seq K S0 n) (seq K S0 (n + k))
  | 0 => fun _ m => m
  | k + 1 => sub_trans' (sub_seq n k) (step_sub K _ _)

theorem seq_mono {n N : Nat} (h : n ≤ N) : Sub (seq K S0 n) (seq K S0 N) := by
  induction h with
  | refl => exact fun _ m => m
  | step _ ih => exact sub_trans' ih (step_sub K _ _)

def UU (χ : Fm) : Prop := ∃ n, χ ∈ seq K S0 n

theorem common {a b : Fm} (ha : UU K S0 a) (hb : UU K S0 b) : ∃ n, a ∈ seq K S0 n ∧ b ∈ seq K S0 n :=
  match ha, hb with
  | ⟨n, hn⟩, ⟨m, hm⟩ => ⟨n + m, seq_mono K S0 (Nat.le_add_right n m) _ hn,
      seq_mono K S0 (Nat.le_add_left m n) _ hm⟩

theorem common3 {a b c : Fm} (ha : UU K S0 a) (hb : UU K S0 b) (hc : UU K S0 c) :
    ∃ n, a ∈ seq K S0 n ∧ b ∈ seq K S0 n ∧ c ∈ seq K S0 n :=
  match ha, hb, hc with
  | ⟨n, hn⟩, ⟨m, hm⟩, ⟨k, hk⟩ =>
    ⟨n + m + k, seq_mono K S0 (Nat.le_trans (Nat.le_add_right n m) (Nat.le_add_right _ k)) _ hn,
      seq_mono K S0 (Nat.le_trans (Nat.le_add_left m n) (Nat.le_add_right _ k)) _ hm,
      seq_mono K S0 (Nat.le_add_left k (n + m)) _ hk⟩

theorem decided (ψ : Fm) : UU K S0 ψ ∨ UU K S0 (neg ψ) := by
  have : ψ ∈ step K (seq K S0 (enc K ψ)) (decF K (enc K ψ)) ∨ neg ψ ∈ step K (seq K S0 (enc K ψ)) (decF K (enc K ψ)) := by
    rw [decF_enc]
    unfold step
    cases dec K (Con (ψ :: seq K S0 (enc K ψ))) with
    | true => exact Or.inl (stepT_spec K _ ψ).1
    | false => exact Or.inr (stepF_spec K _ ψ).1
  exact this.elim (fun h => Or.inl ⟨enc K ψ + 1, h⟩) (fun h => Or.inr ⟨enc K ψ + 1, h⟩)

include c0 in
theorem not_both {χ : Fm} (h1 : UU K S0 χ) (h2 : UU K S0 (neg χ)) : False :=
  match common K S0 h1 h2 with
  | ⟨n, m1, m2⟩ => con_seq K S0 c0 n (pc_of <| .impE (.hyp m2) (.hyp m1))

include c0 in
theorem closed2 {a b c : Fm} (ha : UU K S0 a) (hb : UU K S0 b) (d : Pf [a, b] c) : UU K S0 c :=
  (decided K S0 c).elim id (fun h => match common3 K S0 ha hb h with
    | ⟨n, ma, mb, mc⟩ => (con_seq K S0 c0 n (pc_of <|
        .impE (.hyp mc) (weaken d (sub_cons_of ma (sub_cons_of mb (sub_nil _)))))).elim)

include c0 in
theorem closed1 {a c : Fm} (ha : UU K S0 a) (d : Pf [a] c) : UU K S0 c :=
  closed2 K S0 c0 ha ha (weaken d (sub_cons_of (.head _) (sub_nil _)))

include c0 in
theorem bot_not (h : UU K S0 .bot) : False :=
  match h with
  | ⟨n, m⟩ => con_seq K S0 c0 n (pc_of <| .hyp m)

include c0 in
theorem henkin_ex_U (a : Fm) (h : UU K S0 (.ex a)) : ∃ y, UU K S0 (inst y a) := by
  have e : seq K S0 (enc K (.ex a) + 1) = step K (seq K S0 (enc K (.ex a))) (.ex a) := by
    show step K (seq K S0 (enc K (.ex a))) (decF K (enc K (.ex a))) = _
    rw [decF_enc]
  revert e
  unfold step
  cases hd : dec K (Con (.ex a :: seq K S0 (enc K (.ex a)))) with
  | true => exact fun e => ⟨_, enc K (.ex a) + 1, by rw [e]; exact .head _⟩
  | false =>
    intro _
    have hn : ¬ Con (.ex a :: seq K S0 (enc K (.ex a))) :=
      fun hc => Bool.noConfusion (hd.symm.trans ((dec_spec K _).2 hc))
    match h with
    | ⟨m, hm⟩ => exact (hn (con_mono (con_seq K S0 c0 (enc K (.ex a) + m))
        (sub_cons_of (seq_mono K S0 (Nat.le_add_left m _) _ hm) (seq_mono K S0 (Nat.le_add_right _ m))))).elim

include c0 in
theorem henkin_all_U (b : Fm) (h : ¬ UU K S0 (.all b)) : ∃ y, ¬ UU K S0 (inst y b) := by
  have e : seq K S0 (enc K (.all b) + 1) = step K (seq K S0 (enc K (.all b))) (.all b) := by
    show step K (seq K S0 (enc K (.all b))) (decF K (enc K (.all b))) = _
    rw [decF_enc]
  revert e
  unfold step
  cases hd : dec K (Con (.all b :: seq K S0 (enc K (.all b)))) with
  | true => exact fun e => (h ⟨enc K (.all b) + 1, by rw [e]; exact .head _⟩).elim
  | false => exact fun e => ⟨_, fun hu => not_both K S0 c0 hu ⟨enc K (.all b) + 1, by rw [e]; exact .head _⟩⟩

/-! ## the term model and the truth lemma -/

/-- two-valued Tarski semantics -/
def Sat {D : Type} (R : Nat → List D → Prop) : Fm → (Nat → D) → Prop
  | .atom r l, v => R r (mapD v l)
  | .bot, _ => False
  | .conj a b, v => Sat R a v ∧ Sat R b v
  | .disj a b, v => Sat R a v ∨ Sat R b v
  | .imp a b, v => Sat R a v → Sat R b v
  | .all a, v => ∀ d, Sat R a (ext v d)
  | .ex a, v => ∃ d, Sat R a (ext v d)

include c0 in
/-- the truth lemma for the term model on ℕ -/
theorem truth : (φ : Fm) → (v : Nat → Nat) → (Sat (fun r l => UU K S0 (.atom r l)) φ v ↔ UU K S0 (ren v φ))
  | .atom r l, v => by
    show UU K S0 (.atom r (mapD v l)) ↔ UU K S0 (.atom r (mapL v l))
    rw [mapD_mapL']
  | .bot, _ => ⟨False.elim, bot_not K S0 c0⟩
  | .conj a b, v =>
    ⟨fun ⟨ha, hb⟩ => closed2 K S0 c0 ((truth a v).1 ha) ((truth b v).1 hb)
        (.conjI (.hyp (.head _)) (.hyp (.tail _ (.head _)))),
     fun h => ⟨(truth a v).2 (closed1 K S0 c0 h (.conjL (.hyp (.head _)))),
       (truth b v).2 (closed1 K S0 c0 h (.conjR (.hyp (.head _))))⟩⟩
  | .disj a b, v =>
    ⟨fun h => h.elim (fun ha => closed1 K S0 c0 ((truth a v).1 ha) (.disjL (.hyp (.head _))))
        (fun hb => closed1 K S0 c0 ((truth b v).1 hb) (.disjR (.hyp (.head _)))),
     fun h => (decided K S0 (ren v a)).elim (fun ha => Or.inl ((truth a v).2 ha))
       (fun na => (decided K S0 (ren v b)).elim (fun hb => Or.inr ((truth b v).2 hb))
         (fun nb => (not_both K S0 c0 h (closed2 K S0 c0 na nb
           (.impI (.disjE (.hyp (.head _))
             (.impE (.hyp (.tail _ (.tail _ (.head _)))) (.hyp (.head _)))
             (.impE (.hyp (.tail _ (.tail _ (.tail _ (.head _))))) (.hyp (.head _))))))).elim))⟩
  | .imp a b, v =>
    ⟨fun h => (K.lem (UU K S0 (ren v a))).elim
        (fun ha => closed1 K S0 c0 ((truth b v).1 (h ((truth a v).2 ha))) (.impI (.hyp (.tail _ (.head _)))))
        (fun na => (decided K S0 (ren v a)).elim (fun ha => (na ha).elim)
          (fun hn => closed1 K S0 c0 hn (.impI (.botE (.impE (.hyp (.tail _ (.head _))) (.hyp (.head _))))))),
     fun h ha => (truth b v).2 (closed2 K S0 c0 h ((truth a v).1 ha)
        (.impE (.hyp (.head _)) (.hyp (.tail _ (.head _)))))⟩
  | .all a, v =>
    ⟨fun h => (K.lem (UU K S0 (.all (ren (up v) a)))).elim id (fun hn =>
        match henkin_all_U K S0 c0 _ hn with
        | ⟨y, hy⟩ => (hy (by
            have := (truth a (ext v y)).1 (h y)
            rw [inst_ext] at this
            exact this)).elim),
     fun h d => (truth a (ext v d)).2 (by
        rw [inst_ext]
        exact closed1 K S0 c0 h (.allE d (.hyp (.head _))))⟩
  | .ex a, v =>
    ⟨fun ⟨d, hd⟩ => closed1 K S0 c0 (by
        have := (truth a (ext v d)).1 hd
        rw [inst_ext] at this
        exact this) (.exI d (.hyp (.head _))),
     fun h => match henkin_ex_U K S0 c0 _ h with
       | ⟨y, hy⟩ => ⟨y, (truth a (ext v y)).2 (by rw [inst_ext]; exact hy)⟩⟩

end Henkin

/-! ## soundness and completeness -/

theorem sat_ext {D : Type} (R : Nat → List D → Prop) : (φ : Fm) → {v w : Nat → D} → (∀ n, v n = w n) →
    (Sat R φ v ↔ Sat R φ w)
  | .atom r l, _, _, h => by
    show R r (mapD _ l) ↔ R r (mapD _ l)
    rw [mapD_ext h l]
  | .bot, _, _, _ => Iff.rfl
  | .conj a b, _, _, h => ⟨fun ⟨x, y⟩ => ⟨(sat_ext R a h).1 x, (sat_ext R b h).1 y⟩,
      fun ⟨x, y⟩ => ⟨(sat_ext R a h).2 x, (sat_ext R b h).2 y⟩⟩
  | .disj a b, _, _, h => ⟨fun x => x.elim (fun x => Or.inl ((sat_ext R a h).1 x)) (fun y => Or.inr ((sat_ext R b h).1 y)),
      fun x => x.elim (fun x => Or.inl ((sat_ext R a h).2 x)) (fun y => Or.inr ((sat_ext R b h).2 y))⟩
  | .imp a b, _, _, h => ⟨fun f x => (sat_ext R b h).1 (f ((sat_ext R a h).2 x)),
      fun f x => (sat_ext R b h).2 (f ((sat_ext R a h).1 x))⟩
  | .all a, _, _, h => ⟨fun f d => (sat_ext R a (ext_ext h d)).1 (f d), fun f d => (sat_ext R a (ext_ext h d)).2 (f d)⟩
  | .ex a, _, _, h => ⟨fun ⟨d, x⟩ => ⟨d, (sat_ext R a (ext_ext h d)).1 x⟩, fun ⟨d, x⟩ => ⟨d, (sat_ext R a (ext_ext h d)).2 x⟩⟩

theorem sat_ren {D : Type} (R : Nat → List D → Prop) : (φ : Fm) → (ρ : Nat → Nat) → (v : Nat → D) →
    (Sat R (ren ρ φ) v ↔ Sat R φ (fun n => v (ρ n)))
  | .atom r l, ρ, v => by
    show R r (mapD v (mapL ρ l)) ↔ R r (mapD _ l)
    rw [mapD_mapL]
  | .bot, _, _ => Iff.rfl
  | .conj a b, ρ, v => ⟨fun ⟨x, y⟩ => ⟨(sat_ren R a ρ v).1 x, (sat_ren R b ρ v).1 y⟩,
      fun ⟨x, y⟩ => ⟨(sat_ren R a ρ v).2 x, (sat_ren R b ρ v).2 y⟩⟩
  | .disj a b, ρ, v => ⟨fun x => x.elim (fun x => Or.inl ((sat_ren R a ρ v).1 x)) (fun y => Or.inr ((sat_ren R b ρ v).1 y)),
      fun x => x.elim (fun x => Or.inl ((sat_ren R a ρ v).2 x)) (fun y => Or.inr ((sat_ren R b ρ v).2 y))⟩
  | .imp a b, ρ, v => ⟨fun f x => (sat_ren R b ρ v).1 (f ((sat_ren R a ρ v).2 x)),
      fun f x => (sat_ren R b ρ v).2 (f ((sat_ren R a ρ v).1 x))⟩
  | .all a, ρ, v => ⟨fun f d => (sat_ext R a (ext_up v ρ d)).1 ((sat_ren R a (up ρ) (ext v d)).1 (f d)),
      fun f d => (sat_ren R a (up ρ) (ext v d)).2 ((sat_ext R a (ext_up v ρ d)).2 (f d))⟩
  | .ex a, ρ, v => ⟨fun ⟨d, x⟩ => ⟨d, (sat_ext R a (ext_up v ρ d)).1 ((sat_ren R a (up ρ) (ext v d)).1 x)⟩,
      fun ⟨d, x⟩ => ⟨d, (sat_ren R a (up ρ) (ext v d)).2 ((sat_ext R a (ext_up v ρ d)).2 x)⟩⟩

theorem sat_shift {D : Type} (R : Nat → List D → Prop) (c : Fm) (v : Nat → D) (d : D) :
    Sat R (shift c) (ext v d) ↔ Sat R c v :=
  (sat_ren R c Nat.succ (ext v d)).trans (sat_ext R c (fun _ => rfl))

theorem sat_inst {D : Type} (R : Nat → List D → Prop) (y : Nat) (a : Fm) (v : Nat → D) :
    Sat R (inst y a) v ↔ Sat R a (ext v (v y)) :=
  (sat_ren R a (cons0 y) v).trans (sat_ext R a (fun n => match n with
    | 0 => rfl
    | _ + 1 => rfl))

/-- soundness of natural deduction for two-valued semantics -/
theorem sound {D : Type} (R : Nat → List D → Prop) {Γ : List Fm} {φ : Fm} (d : Pf Γ φ) :
    ∀ v, (∀ γ, γ ∈ Γ → Sat R γ v) → Sat R φ v := by
  induction d with
  | hyp m => exact fun v h => h _ m
  | botE _ ih => exact fun v h => (ih v h).elim
  | conjI _ _ ih1 ih2 => exact fun v h => ⟨ih1 v h, ih2 v h⟩
  | conjL _ ih => exact fun v h => (ih v h).1
  | conjR _ ih => exact fun v h => (ih v h).2
  | disjL _ ih => exact fun v h => Or.inl (ih v h)
  | disjR _ ih => exact fun v h => Or.inr (ih v h)
  | disjE _ _ _ ih ih1 ih2 => exact (fun v h => (ih v h).elim
      (fun x => ih1 v (fun γ m => by cases m with | head => exact x | tail _ m' => exact h γ m'))
      (fun y => ih2 v (fun γ m => by cases m with | head => exact y | tail _ m' => exact h γ m')))
  | impI _ ih => exact fun v h x => ih v (fun γ m => by cases m with | head => exact x | tail _ m' => exact h γ m')
  | impE _ _ ih1 ih2 => exact fun v h => ih1 v h (ih2 v h)
  | allI _ ih => exact (fun v h d => ih (ext v d) (fun γ m => match mem_sh _ m with
      | ⟨b, hb, e⟩ => by rw [e]; exact (sat_shift R b v d).2 (h b hb)))
  | allE y _ ih => exact fun v h => (sat_inst R y _ v).2 (ih v h (v y))
  | exI y _ ih => exact fun v h => ⟨v y, (sat_inst R y _ v).1 (ih v h)⟩
  | exE _ _ ih1 ih2 => exact (fun v h => match ih1 v h with
      | ⟨d, x⟩ => (sat_shift R _ v d).1 (ih2 (ext v d) (fun γ m => by
          cases m with
          | head => exact x
          | tail _ m' => exact match mem_sh _ m' with
            | ⟨b, hb, e⟩ => by rw [e]; exact (sat_shift R b v d).2 (h b hb))))

include K in
theorem sat_clo {D : Type} (R : Nat → List D → Prop) (a : Fm) : (j : Nat) → ∀ v, Sat R (clo j (lemF a)) v
  | 0, v => K.lem (Sat R a v)
  | j + 1, v => fun d => sat_clo R a j (ext v d)

/-- truth in every two-valued structure -/
def Valid2 (Γ : List Fm) (φ : Fm) : Prop :=
  ∀ (D : Type) (R : Nat → List D → Prop) (v : Nat → D), (∀ γ, γ ∈ Γ → Sat R γ v) → Sat R φ v

include K in
theorem soundness2 {Γ : List Fm} {φ : Fm} (h : PC Γ φ) : Valid2 Γ φ := fun _ R v hΓ =>
  match h with
  | ⟨Δ, cΔ, d⟩ => sound R d v (fun γ m => (mem_app_inv' Δ Γ m).elim
      (fun mΔ => match cΔ γ mΔ with | ⟨j, a, e⟩ => e ▸ sat_clo K R a j v) (hΓ γ))

include K in
/-- Gödel's completeness theorem (two-valued, in the classical point) -/
theorem completeness2 {Γ : List Fm} {φ : Fm} (h : Valid2 Γ φ) : PC Γ φ := dne K _ (fun hn => by
  have c0 : Con (neg φ :: Γ) := fun ⟨Δ, cΔ, d⟩ => hn ⟨lemF φ :: Δ, ctx_cons (lem_in φ) cΔ,
    .disjE (.hyp (.head _)) (.hyp (.head _)) (.botE (weaken d (sub_app_of
      (sub_trans' (sub_appL' Δ Γ) (sub_trans' (sub_tl _ _) (sub_tl _ _)))
      (sub_cons_of (.head _) (sub_trans' (sub_appR' Δ Γ) (sub_trans' (sub_tl _ _) (sub_tl _ _)))))))⟩
  have hv := h Nat (fun r l => UU K (neg φ :: Γ) (.atom r l)) (fun n => n) (fun γ m =>
    (truth K _ c0 γ (fun n => n)).2 (by rw [ren_id]; exact ⟨0, .tail _ m⟩))
  have u := (truth K _ c0 φ (fun n => n)).1 hv
  rw [ren_id] at u
  exact not_both K _ c0 u ⟨0, .head _⟩)

include K in
theorem complete_iff2 (Γ : List Fm) (φ : Fm) : PC Γ φ ↔ Valid2 Γ φ := ⟨soundness2 K, completeness2 K⟩

end FOC

end Found

#print axioms Found.FOC.inst0_shift
#print axioms Found.FOC.pf_allL
#print axioms Found.FOC.discharge
#print axioms Found.FOC.decide_con
#print axioms Found.FOC.henkin_ex
#print axioms Found.FOC.henkin_all
#print axioms Found.FOC.enc_inj
#print axioms Found.FOC.decF_enc
#print axioms Found.FOC.con_step
#print axioms Found.FOC.con_seq
#print axioms Found.FOC.decided
#print axioms Found.FOC.closed2
#print axioms Found.FOC.henkin_ex_U
#print axioms Found.FOC.henkin_all_U
#print axioms Found.FOC.truth
#print axioms Found.FOC.sat_ren
#print axioms Found.FOC.sound
#print axioms Found.FOC.soundness2
#print axioms Found.FOC.completeness2
#print axioms Found.FOC.complete_iff2

found/FOClassicalT.lean

lean_all/found/FOClassicalT.lean

/-
  T208 (L2), infinite theories. found/FOClassicalT — Gödel's
  completeness theorem for an arbitrary, possibly infinite, theory T (a
  predicate on formulas, no closedness condition: a member stands for its
  universal closure) in the classical point (excluded middle and choice as
  the data of found/ClassicalPoint, not as Lean axioms):
  * classical derivability: natural deduction (found/FOCompleteness) from
    instances of excluded middle and their universal closures;
  * two-valued Tarski semantics over any domain;
  * soundness: derivable ⟹ true in every two-valued structure;
  * the axioms are the excluded-middle instances and all renamings of
    members of T, with their universal closures; a model of T makes every
    member true under every valuation;
  * completeness (completenessT): true in every model of T ⟹ derivable from
    finitely many axioms (so also compactness), by a
    Henkin construction: formulas are enumerated (an injective code into ℕ
    through the Hessenberg pairing), a consistent list is extended step by
    step deciding each formula, with a witness variable for every ∃ added and
    a counter-witness for every ∀ refuted (fresh-variable renaming moves the
    used excluded-middle instances under the quantifier), and the term model
    on ℕ satisfies exactly the formulas collected (truth lemma).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.FOCompact
import found.Hessenberg

namespace Found

namespace FOCT

open FO CP Card

/-! ## classical derivability -/

def neg (a : Fm) : Fm := .imp a .bot
def lemF (a : Fm) : Fm := .disj a (neg a)

def clo : Nat → Fm → Fm
  | 0, a => a
  | j + 1, a => .all (clo j a)

/-- the axioms: excluded-middle instances and renamings of members of the theory -/
def Ax (T : Fm → Prop) (a : Fm) : Prop := (∃ b, a = lemF b) ∨ (∃ b σ, T b ∧ a = ren σ b)
def InT (T : Fm → Prop) (δ : Fm) : Prop := ∃ j a, Ax T a ∧ δ = clo j a
def CtxT (T : Fm → Prop) (Δ : List Fm) : Prop := ∀ δ, δ ∈ Δ → InT T δ

/-- derivable from finitely many closures of axioms -/
def PC (T : Fm → Prop) (S : List Fm) (φ : Fm) : Prop := ∃ Δ, CtxT T Δ ∧ Pf (Δ ++ S) φ

def Con (T : Fm → Prop) (S : List Fm) : Prop := ¬ PC T S .bot

variable {T : Fm → Prop}

theorem pc_of {S : List Fm} {φ : Fm} (d : Pf S φ) : PC T S φ := ⟨[], ⟨(fun _ m => nomatch m), d⟩⟩

/-! ## list and context helpers -/

theorem sub_trans' {A B C : List Fm} (h1 : Sub A B) (h2 : Sub B C) : Sub A C := fun a m => h2 a (h1 a m)
theorem sub_tl (y : Fm) (A : List Fm) : Sub A (y :: A) := fun _ m => .tail _ m
theorem sub_appL' (A B : List Fm) : Sub A (A ++ B) := fun _ m => mem_app_l' A B m
theorem sub_appR' (A B : List Fm) : Sub B (A ++ B) := fun _ m => mem_app_r' A B m
theorem sub_app_of {A B T : List Fm} (h1 : Sub A T) (h2 : Sub B T) : Sub (A ++ B) T := fun a m =>
  (mem_app_inv' A B m).elim (h1 a) (h2 a)
theorem sub_cons_of {x : Fm} {A T : List Fm} (hx : x ∈ T) (h : Sub A T) : Sub (x :: A) T := fun a m => by
  cases m with
  | head => exact hx
  | tail _ m' => exact h a m'
theorem sub_nil (T : List Fm) : Sub [] T := fun _ m => nomatch m

theorem ctx_app' {A B : List Fm} (h1 : CtxT T A) (h2 : CtxT T B) : CtxT T (A ++ B) := fun a m =>
  (mem_app_inv' A B m).elim (h1 a) (h2 a)
theorem ctx_cons {x : Fm} {A : List Fm} (hx : InT T x) (h : CtxT T A) : CtxT T (x :: A) := fun a m => by
  cases m with
  | head => exact hx
  | tail _ m' => exact h a m'
theorem lem_in (a : Fm) : InT T (lemF a) := ⟨0, lemF a, Or.inl ⟨a, rfl⟩, rfl⟩

theorem mapF_app (ρ : Nat → Nat) : (A B : List Fm) → mapF ρ (A ++ B) = mapF ρ A ++ mapF ρ B
  | [], _ => rfl
  | a :: A, B => congrArg (List.cons (ren ρ a)) (mapF_app ρ A B)

theorem mem_mapF_inv (ρ : Nat → Nat) {a : Fm} : (Γ : List Fm) → a ∈ mapF ρ Γ → ∃ b, b ∈ Γ ∧ a = ren ρ b
  | [], m => nomatch m
  | b :: Γ, m => by
    cases m with
    | head => exact ⟨b, .head _, rfl⟩
    | tail _ m' => exact match mem_mapF_inv ρ Γ m' with | ⟨c, hc, e⟩ => ⟨c, .tail _ hc, e⟩

def upn : Nat → (Nat → Nat) → (Nat → Nat)
  | 0, ρ => ρ
  | j + 1, ρ => upn j (up ρ)

theorem ren_clo : (j : Nat) → (ρ : Nat → Nat) → (x : Fm) → ren ρ (clo j x) = clo j (ren (upn j ρ) x)
  | 0, _, _ => rfl
  | j + 1, ρ, x => by
    show Fm.all (ren (up ρ) (clo j x)) = Fm.all (clo j (ren (upn j (up ρ)) x))
    rw [ren_clo j (up ρ) x]

theorem ax_ren (σ : Nat → Nat) {a : Fm} (h : Ax T a) : Ax T (ren σ a) :=
  match h with
  | .inl ⟨b, e⟩ => .inl ⟨ren σ b, by rw [e]; rfl⟩
  | .inr ⟨b, τ, hb, e⟩ => .inr ⟨b, fun n => σ (τ n), hb, by rw [e, ren_ren]⟩

theorem inT_ren (ρ : Nat → Nat) {δ : Fm} (h : InT T δ) : InT T (ren ρ δ) :=
  match h with
  | ⟨j, a, ha, e⟩ => ⟨j, ren (upn j ρ) a, ax_ren (upn j ρ) ha, by rw [e, ren_clo]⟩

theorem ctx_ren (ρ : Nat → Nat) {Δ : List Fm} (h : CtxT T Δ) : CtxT T (mapF ρ Δ) := fun _ m =>
  match mem_mapF_inv ρ Δ m with
  | ⟨b, hb, e⟩ => e ▸ inT_ren ρ (h b hb)

def allL : List Fm → List Fm
  | [] => []
  | δ :: Δ => .all δ :: allL Δ

theorem ctx_allL : {Δ : List Fm} → CtxT T Δ → CtxT T (allL Δ)
  | [], _ => fun _ m => nomatch m
  | _ :: Δ, h => ctx_cons (match h _ (.head _) with
      | ⟨j, a, ha, e⟩ => ⟨j + 1, a, ha, by rw [e]; rfl⟩) (ctx_allL (fun b m => h b (.tail _ m)))

theorem mem_allL {δ : Fm} : (Δ : List Fm) → δ ∈ Δ → Fm.all δ ∈ allL Δ
  | [], m => nomatch m
  | _ :: Δ, m => by
    cases m with
    | head => exact .head _
    | tail _ m' => exact .tail _ (mem_allL Δ m')

theorem inst0_shift (δ : Fm) : inst 0 (ren (up Nat.succ) δ) = δ := by
  unfold inst
  rw [ren_ren]
  exact (ren_ext δ (σ := fun n => n) (fun n => match n with
    | 0 => rfl
    | _ + 1 => rfl)).trans (ren_id δ)

/-- the closures, shifted under a binder, give back the instances -/
theorem pf_allL (Δ T : List Fm) (h : Sub (sh (allL Δ)) T) : ∀ δ, δ ∈ Δ → Pf T δ := fun δ m => by
  have m1 : shift (Fm.all δ) ∈ sh (allL Δ) := mem_mapF Nat.succ _ (mem_allL Δ m)
  have p : Pf T (Fm.all (ren (up Nat.succ) δ)) := .hyp (h _ m1)
  have q := Pf.allE 0 p
  rw [inst0_shift] at q
  exact q

/-- discharge hypotheses that are derivable -/
theorem discharge : (Δ : List Fm) → {L T : List Fm} → {φ : Fm} → Pf (Δ ++ L) φ → Sub L T →
    (∀ δ, δ ∈ Δ → Pf T δ) → Pf T φ
  | [], _, _, _, d, hL, _ => weaken d hL
  | δ :: Δ, _, _, _, d, hL, hΔ =>
    .impE (discharge Δ (Pf.impI d) hL (fun b m => hΔ b (.tail _ m))) (hΔ δ (.head _))

def boundL : List Fm → Nat
  | [] => 0
  | a :: S => bound a + boundL S

theorem bound_le : (S : List Fm) → {s : Fm} → s ∈ S → bound s ≤ boundL S
  | [], _, m => nomatch m
  | _ :: S, _, m => by
    cases m with
    | head => exact Nat.le_add_right _ _
    | tail _ m' => exact Nat.le_trans (bound_le S m') (Nat.le_add_left _ _)

theorem mapF_sig (N : Nat) : (S : List Fm) → (∀ s, s ∈ S → bound s ≤ N) → mapF (sig N) S = sh S
  | [], _ => rfl
  | a :: S, h => by
    show ren (sig N) a :: mapF (sig N) S = shift a :: sh S
    rw [sig_shift N a (h a (.head _)), mapF_sig N S (fun s m => h s (.tail _ m))]

/-! ## consistency steps -/

variable (K : Classical') (T)

theorem con_mono {S S' : List Fm} (h : Con T S') (hs : Sub S S') : Con T S := fun ⟨Δ, c, d⟩ =>
  h ⟨Δ, c, weaken d (sub_app_of (sub_appL' Δ S') (sub_trans' hs (sub_appR' Δ S')))⟩

include K in
/-- deciding a formula keeps consistency -/
theorem decide_con (S : List Fm) (ψ : Fm) (h : Con T S) : Con T (ψ :: S) ∨ Con T (neg ψ :: S) :=
  (K.lem (Con T (ψ :: S))).elim Or.inl (fun h1 => (K.lem (Con T (neg ψ :: S))).elim Or.inr (fun h2 =>
    (h (match dne K _ h1, dne K _ h2 with
      | ⟨Δ1, c1, d1⟩, ⟨Δ2, c2, d2⟩ =>
        let T := lemF ψ :: ((Δ1 ++ Δ2) ++ S)
        ⟨lemF ψ :: (Δ1 ++ Δ2), ctx_cons (lem_in ψ) (ctx_app' c1 c2),
          .disjE (.hyp (.head _))
            (weaken d1 (sub_app_of (sub_trans' (sub_appL' Δ1 Δ2) (sub_trans' (sub_appL' _ S)
                (sub_trans' (sub_tl _ _) (sub_tl _ T))))
              (sub_cons_of (.head _) (sub_trans' (sub_appR' _ S) (sub_trans' (sub_tl _ _) (sub_tl _ T))))))
            (weaken d2 (sub_app_of (sub_trans' (sub_appR' Δ1 Δ2) (sub_trans' (sub_appL' _ S)
                (sub_trans' (sub_tl _ _) (sub_tl _ T))))
              (sub_cons_of (.head _) (sub_trans' (sub_appR' _ S) (sub_trans' (sub_tl _ _) (sub_tl _ T))))))⟩)).elim))

include K in
/-- a consistent ∃ has a consistent witness -/
theorem henkin_ex (S : List Fm) (a : Fm) (h : Con T (.ex a :: S)) : ∃ y, Con T (inst y a :: S) := by
  refine dne K _ (fun hn => h ?_)
  have hy : PC T (inst (boundL (.ex a :: S)) a :: S) .bot := dne K _ (fun hc => hn ⟨_, hc⟩)
  match hy with
  | ⟨Δ, cΔ, d⟩ =>
    have hbS : ∀ s, s ∈ S → bound s ≤ boundL (.ex a :: S) := fun s m => bound_le _ (.tail _ m)
    have hba : bound a ≤ boundL (.ex a :: S) + 1 := Nat.le_succ_of_le (bound_le (.ex a :: S) (s := .ex a) (.head _))
    have d1 := pf_ren d (sig (boundL (.ex a :: S)))
    rw [mapF_app] at d1
    have e1 : mapF (sig (boundL (.ex a :: S))) (inst (boundL (.ex a :: S)) a :: S) = a :: sh S := by
      show ren (sig _) (inst _ a) :: mapF (sig _) S = a :: sh S
      rw [sig_inst _ a hba, mapF_sig _ S hbS]
    rw [e1] at d1
    let Δ' := mapF (sig (boundL (.ex a :: S))) Δ
    let Γ := allL Δ' ++ .ex a :: S
    have sS : Sub S Γ := sub_trans' (sub_tl _ _) (sub_appR' _ _)
    have d2 : Pf (a :: sh Γ) .bot := discharge Δ' d1 (sub_cons a (sub_sh sS))
      (pf_allL Δ' _ (sub_trans' (sub_sh (sub_appL' _ _)) (sub_tl _ _)))
    exact ⟨allL Δ', ctx_allL (ctx_ren _ cΔ), .exE (.hyp (mem_app_r' _ _ (.head _))) d2⟩

include K in
/-- a consistent refuted ∀ has a consistent counter-witness -/
theorem henkin_all (S : List Fm) (b : Fm) (h : Con T (neg (.all b) :: S)) : ∃ y, Con T (neg (inst y b) :: S) := by
  refine dne K _ (fun hn => h ?_)
  have hy : PC T (neg (inst (boundL (neg (.all b) :: S)) b) :: S) .bot := dne K _ (fun hc => hn ⟨_, hc⟩)
  match hy with
  | ⟨Δ, cΔ, d⟩ =>
    have hbS : ∀ s, s ∈ S → bound s ≤ boundL (neg (.all b) :: S) := fun s m => bound_le _ (.tail _ m)
    have hbb : bound b ≤ boundL (neg (.all b) :: S) + 1 := Nat.le_succ_of_le (bound_le (neg (.all b) :: S) (s := neg (.all b)) (.head _))
    have d1 := pf_ren d (sig (boundL (neg (.all b) :: S)))
    rw [mapF_app] at d1
    have e1 : mapF (sig (boundL (neg (.all b) :: S))) (neg (inst (boundL (neg (.all b) :: S)) b) :: S) =
        neg b :: sh S := by
      show neg (ren (sig _) (inst _ b)) :: mapF (sig _) S = neg b :: sh S
      rw [sig_inst _ b hbb, mapF_sig _ S hbS]
    rw [e1] at d1
    let Δ' := mapF (sig (boundL (neg (.all b) :: S))) Δ
    have d2 : Pf ((lemF b :: Δ') ++ sh S) b :=
      .disjE (.hyp (.head _)) (.hyp (.head _)) (.botE (weaken d1
        (sub_app_of (sub_trans' (sub_appL' Δ' (sh S)) (sub_trans' (sub_tl _ _) (sub_tl _ _)))
          (sub_cons_of (.head _) (sub_trans' (sub_appR' Δ' (sh S)) (sub_trans' (sub_tl _ _) (sub_tl _ _)))))))
    let Δ2 := lemF b :: Δ'
    let Γ := allL Δ2 ++ neg (.all b) :: S
    have sS : Sub S Γ := sub_trans' (sub_tl _ _) (sub_appR' _ _)
    have d3 : Pf (sh Γ) b := discharge Δ2 d2 (sub_sh sS) (pf_allL Δ2 _ (sub_sh (sub_appL' _ _)))
    exact ⟨allL Δ2, ctx_allL (ctx_cons (lem_in b) (ctx_ren _ cΔ)),
      .impE (.hyp (mem_app_r' _ _ (.head _))) (.allI d3)⟩

/-! ## enumeration of formulas -/

def pr (n : Nat × Nat) : Nat :=
  K.ch (fun F : Nat × Nat → Nat => Inj F) (Hess.hessenberg K (e := fun n : Nat => n) (fun _ _ h => h)) n

theorem pr_inj : Inj (pr K) :=
  K.ch_spec (fun F : Nat × Nat → Nat => Inj F) (Hess.hessenberg K (e := fun n : Nat => n) (fun _ _ h => h))

def encL : List Nat → Nat
  | [] => pr K (0, 0)
  | x :: l => pr K (1, pr K (x, encL l))

def enc : Fm → Nat
  | .atom r l => pr K (0, pr K (r, encL K l))
  | .bot => pr K (1, 0)
  | .conj a b => pr K (2, pr K (enc a, enc b))
  | .disj a b => pr K (3, pr K (enc a, enc b))
  | .imp a b => pr K (4, pr K (enc a, enc b))
  | .all a => pr K (5, enc a)
  | .ex a => pr K (6, enc a)

theorem pair_eq {a b c d : Nat} (h : (a, b) = (c, d)) : a = c ∧ b = d :=
  ⟨congrArg Prod.fst h, congrArg Prod.snd h⟩

theorem encL_inj : (l m : List Nat) → encL K l = encL K m → l = m
  | [], [], _ => rfl
  | [], _ :: _, h => Bool.noConfusion (congrArg (fun p : Nat × Nat => Nat.beq p.1 0) (pr_inj K _ _ h))
  | _ :: _, [], h => Bool.noConfusion (congrArg (fun p : Nat × Nat => Nat.beq p.1 1) (pr_inj K _ _ h))
  | x :: l, y :: m, h => by
    have h2 := (pair_eq (pr_inj K _ _ h)).2
    have h3 := pair_eq (pr_inj K _ _ h2)
    rw [h3.1, encL_inj l m h3.2]

theorem enc_inj (φ : Fm) : ∀ ψ, enc K φ = enc K ψ → φ = ψ := by
  induction φ with
  | atom r l =>
    intro ψ h
    cases ψ <;> (try exact Bool.noConfusion (congrArg (fun p : Nat × Nat => Nat.beq p.1 0) (pr_inj K _ _ h)))
    have h3 := pair_eq (pr_inj K _ _ (pair_eq (pr_inj K _ _ h)).2)
    rw [h3.1, encL_inj K _ _ h3.2]
  | bot =>
    intro ψ h
    cases ψ <;> (try exact Bool.noConfusion (congrArg (fun p : Nat × Nat => Nat.beq p.1 1) (pr_inj K _ _ h)))
    rfl
  | conj a b iha ihb =>
    intro ψ h
    cases ψ <;> (try exact Bool.noConfusion (congrArg (fun p : Nat × Nat => Nat.beq p.1 2) (pr_inj K _ _ h)))
    have h3 := pair_eq (pr_inj K _ _ (pair_eq (pr_inj K _ _ h)).2)
    rw [iha _ h3.1, ihb _ h3.2]
  | disj a b iha ihb =>
    intro ψ h
    cases ψ <;> (try exact Bool.noConfusion (congrArg (fun p : Nat × Nat => Nat.beq p.1 3) (pr_inj K _ _ h)))
    have h3 := pair_eq (pr_inj K _ _ (pair_eq (pr_inj K _ _ h)).2)
    rw [iha _ h3.1, ihb _ h3.2]
  | imp a b iha ihb =>
    intro ψ h
    cases ψ <;> (try exact Bool.noConfusion (congrArg (fun p : Nat × Nat => Nat.beq p.1 4) (pr_inj K _ _ h)))
    have h3 := pair_eq (pr_inj K _ _ (pair_eq (pr_inj K _ _ h)).2)
    rw [iha _ h3.1, ihb _ h3.2]
  | all a iha =>
    intro ψ h
    cases ψ <;> (try exact Bool.noConfusion (congrArg (fun p : Nat × Nat => Nat.beq p.1 5) (pr_inj K _ _ h)))
    rw [iha _ (pair_eq (pr_inj K _ _ h)).2]
  | ex a iha =>
    intro ψ h
    cases ψ <;> (try exact Bool.noConfusion (congrArg (fun p : Nat × Nat => Nat.beq p.1 6) (pr_inj K _ _ h)))
    rw [iha _ (pair_eq (pr_inj K _ _ h)).2]

theorem dec_ex (n : Nat) : ∃ ψ, (∃ φ, enc K φ = n) → enc K ψ = n :=
  (K.lem (∃ φ, enc K φ = n)).elim (fun ⟨φ, h⟩ => ⟨φ, fun _ => h⟩) (fun hn => ⟨.bot, fun h => (hn h).elim⟩)

def decF (n : Nat) : Fm := K.ch (fun ψ => (∃ φ, enc K φ = n) → enc K ψ = n) (dec_ex K n)

theorem decF_enc (φ : Fm) : decF K (enc K φ) = φ :=
  enc_inj K _ _ (K.ch_spec (fun ψ => (∃ φ', enc K φ' = enc K φ) → enc K ψ = enc K φ) (dec_ex K _) ⟨φ, rfl⟩)

/-! ## the Henkin sequence -/

theorem con_dup {x : Fm} {S : List Fm} (h : Con T (x :: S)) : Con T (x :: x :: S) :=
  con_mono T h (sub_cons_of (.head _) (fun _ m => m))

include K in
theorem wEx_ex (S : List Fm) (a : Fm) : ∃ y, Con T (.ex a :: S) → Con T (inst y a :: .ex a :: S) :=
  (K.lem (Con T (.ex a :: S))).elim
    (fun hc => match henkin_ex T K (.ex a :: S) a (con_dup T hc) with | ⟨y, hy⟩ => ⟨y, fun _ => hy⟩)
    (fun hn => ⟨0, fun h => (hn h).elim⟩)

def wEx (S : List Fm) (a : Fm) : Nat := K.ch _ (wEx_ex T K S a)

include K in
theorem wAll_ex (S : List Fm) (b : Fm) : ∃ y, Con T (neg (.all b) :: S) → Con T (neg (inst y b) :: neg (.all b) :: S) :=
  (K.lem (Con T (neg (.all b) :: S))).elim
    (fun hc => match henkin_all T K (neg (.all b) :: S) b (con_dup T hc) with | ⟨y, hy⟩ => ⟨y, fun _ => hy⟩)
    (fun hn => ⟨0, fun h => (hn h).elim⟩)

def wAll (S : List Fm) (b : Fm) : Nat := K.ch _ (wAll_ex T K S b)

def stepT (S : List Fm) : Fm → List Fm
  | .atom r l => .atom r l :: S
  | .bot => .bot :: S
  | .conj a b => .conj a b :: S
  | .disj a b => .disj a b :: S
  | .imp a b => .imp a b :: S
  | .all a => .all a :: S
  | .ex a => inst (wEx T K S a) a :: .ex a :: S

def stepF (S : List Fm) : Fm → List Fm
  | .atom r l => neg (.atom r l) :: S
  | .bot => neg .bot :: S
  | .conj a b => neg (.conj a b) :: S
  | .disj a b => neg (.disj a b) :: S
  | .imp a b => neg (.imp a b) :: S
  | .all b => neg (inst (wAll T K S b) b) :: neg (.all b) :: S
  | .ex a => neg (.ex a) :: S

def step (S : List Fm) (ψ : Fm) : List Fm := cond (dec K (Con T (ψ :: S))) (stepT T K S ψ) (stepF T K S ψ)

theorem stepT_spec (S : List Fm) : (ψ : Fm) → ψ ∈ stepT T K S ψ ∧ Sub S (stepT T K S ψ)
  | .atom _ _ => ⟨.head _, sub_tl _ _⟩
  | .bot => ⟨.head _, sub_tl _ _⟩
  | .conj _ _ => ⟨.head _, sub_tl _ _⟩
  | .disj _ _ => ⟨.head _, sub_tl _ _⟩
  | .imp _ _ => ⟨.head _, sub_tl _ _⟩
  | .all _ => ⟨.head _, sub_tl _ _⟩
  | .ex _ => ⟨.tail _ (.head _), sub_trans' (sub_tl _ _) (sub_tl _ _)⟩

theorem stepF_spec (S : List Fm) : (ψ : Fm) → neg ψ ∈ stepF T K S ψ ∧ Sub S (stepF T K S ψ)
  | .atom _ _ => ⟨.head _, sub_tl _ _⟩
  | .bot => ⟨.head _, sub_tl _ _⟩
  | .conj _ _ => ⟨.head _, sub_tl _ _⟩
  | .disj _ _ => ⟨.head _, sub_tl _ _⟩
  | .imp _ _ => ⟨.head _, sub_tl _ _⟩
  | .all _ => ⟨.tail _ (.head _), sub_trans' (sub_tl _ _) (sub_tl _ _)⟩
  | .ex _ => ⟨.head _, sub_tl _ _⟩

theorem con_stepT (S : List Fm) : (ψ : Fm) → Con T (ψ :: S) → Con T (stepT T K S ψ)
  | .atom _ _, h => h
  | .bot, h => h
  | .conj _ _, h => h
  | .disj _ _, h => h
  | .imp _ _, h => h
  | .all _, h => h
  | .ex a, h => K.ch_spec (fun y => Con T (.ex a :: S) → Con T (inst y a :: .ex a :: S)) (wEx_ex T K S a) h

theorem con_stepF (S : List Fm) : (ψ : Fm) → Con T (neg ψ :: S) → Con T (stepF T K S ψ)
  | .atom _ _, h => h
  | .bot, h => h
  | .conj _ _, h => h
  | .disj _ _, h => h
  | .imp _ _, h => h
  | .all b, h => K.ch_spec (fun y => Con T (neg (.all b) :: S) → Con T (neg (inst y b) :: neg (.all b) :: S))
      (wAll_ex T K S b) h
  | .ex _, h => h

theorem step_sub (S : List Fm) (ψ : Fm) : Sub S (step T K S ψ) := by
  unfold step
  cases dec K (Con T (ψ :: S)) with
  | true => exact (stepT_spec T K S ψ).2
  | false => exact (stepF_spec T K S ψ).2

theorem con_step (S : List Fm) (ψ : Fm) (h : Con T S) : Con T (step T K S ψ) := by
  unfold step
  cases hd : dec K (Con T (ψ :: S)) with
  | true => exact con_stepT T K S ψ ((dec_spec K _).1 hd)
  | false =>
    have hn : ¬ Con T (ψ :: S) := fun hc => Bool.noConfusion (hd.symm.trans ((dec_spec K _).2 hc))
    exact con_stepF T K S ψ ((decide_con T K S ψ h).elim (fun hc => (hn hc).elim) id)

section Henkin

variable (S0 : List Fm) (c0 : Con T S0)

def seq : Nat → List Fm
  | 0 => S0
  | n + 1 => step T K (seq n) (decF K n)

include c0 in
theorem con_seq : (n : Nat) → Con T (seq T K S0 n)
  | 0 => c0
  | n + 1 => con_step T K _ _ (con_seq n)

theorem sub_seq (n : Nat) : (k : Nat) → Sub (seq T K S0 n) (seq T K S0 (n + k))
  | 0 => fun _ m => m
  | k + 1 => sub_trans' (sub_seq n k) (step_sub T K _ _)

theorem seq_mono {n N : Nat} (h : n ≤ N) : Sub (seq T K S0 n) (seq T K S0 N) := by
  induction h with
  | refl => exact fun _ m => m
  | step _ ih => exact sub_trans' ih (step_sub T K _ _)

def UU (χ : Fm) : Prop := ∃ n, χ ∈ seq T K S0 n

theorem common {a b : Fm} (ha : UU T K S0 a) (hb : UU T K S0 b) : ∃ n, a ∈ seq T K S0 n ∧ b ∈ seq T K S0 n :=
  match ha, hb with
  | ⟨n, hn⟩, ⟨m, hm⟩ => ⟨n + m, seq_mono T K S0 (Nat.le_add_right n m) _ hn,
      seq_mono T K S0 (Nat.le_add_left m n) _ hm⟩

theorem common3 {a b c : Fm} (ha : UU T K S0 a) (hb : UU T K S0 b) (hc : UU T K S0 c) :
    ∃ n, a ∈ seq T K S0 n ∧ b ∈ seq T K S0 n ∧ c ∈ seq T K S0 n :=
  match ha, hb, hc with
  | ⟨n, hn⟩, ⟨m, hm⟩, ⟨k, hk⟩ =>
    ⟨n + m + k, seq_mono T K S0 (Nat.le_trans (Nat.le_add_right n m) (Nat.le_add_right _ k)) _ hn,
      seq_mono T K S0 (Nat.le_trans (Nat.le_add_left m n) (Nat.le_add_right _ k)) _ hm,
      seq_mono T K S0 (Nat.le_add_left k (n + m)) _ hk⟩

theorem decided (ψ : Fm) : UU T K S0 ψ ∨ UU T K S0 (neg ψ) := by
  have : ψ ∈ step T K (seq T K S0 (enc K ψ)) (decF K (enc K ψ)) ∨ neg ψ ∈ step T K (seq T K S0 (enc K ψ)) (decF K (enc K ψ)) := by
    rw [decF_enc]
    unfold step
    cases dec K (Con T (ψ :: seq T K S0 (enc K ψ))) with
    | true => exact Or.inl (stepT_spec T K _ ψ).1
    | false => exact Or.inr (stepF_spec T K _ ψ).1
  exact this.elim (fun h => Or.inl ⟨enc K ψ + 1, h⟩) (fun h => Or.inr ⟨enc K ψ + 1, h⟩)

include c0 in
theorem not_both {χ : Fm} (h1 : UU T K S0 χ) (h2 : UU T K S0 (neg χ)) : False :=
  match common T K S0 h1 h2 with
  | ⟨n, m1, m2⟩ => con_seq T K S0 c0 n (pc_of <| .impE (.hyp m2) (.hyp m1))

include c0 in
theorem closed2 {a b c : Fm} (ha : UU T K S0 a) (hb : UU T K S0 b) (d : Pf [a, b] c) : UU T K S0 c :=
  (decided T K S0 c).elim id (fun h => match common3 T K S0 ha hb h with
    | ⟨n, ma, mb, mc⟩ => (con_seq T K S0 c0 n (pc_of <|
        .impE (.hyp mc) (weaken d (sub_cons_of ma (sub_cons_of mb (sub_nil _)))))).elim)

include c0 in
theorem closed1 {a c : Fm} (ha : UU T K S0 a) (d : Pf [a] c) : UU T K S0 c :=
  closed2 T K S0 c0 ha ha (weaken d (sub_cons_of (.head _) (sub_nil _)))

include c0 in
theorem bot_not (h : UU T K S0 .bot) : False :=
  match h with
  | ⟨n, m⟩ => con_seq T K S0 c0 n (pc_of <| .hyp m)

include c0 in
theorem henkin_ex_U (a : Fm) (h : UU T K S0 (.ex a)) : ∃ y, UU T K S0 (inst y a) := by
  have e : seq T K S0 (enc K (.ex a) + 1) = step T K (seq T K S0 (enc K (.ex a))) (.ex a) := by
    show step T K (seq T K S0 (enc K (.ex a))) (decF K (enc K (.ex a))) = _
    rw [decF_enc]
  revert e
  unfold step
  cases hd : dec K (Con T (.ex a :: seq T K S0 (enc K (.ex a)))) with
  | true => exact fun e => ⟨_, enc K (.ex a) + 1, by rw [e]; exact .head _⟩
  | false =>
    intro _
    have hn : ¬ Con T (.ex a :: seq T K S0 (enc K (.ex a))) :=
      fun hc => Bool.noConfusion (hd.symm.trans ((dec_spec K _).2 hc))
    match h with
    | ⟨m, hm⟩ => exact (hn (con_mono T (con_seq T K S0 c0 (enc K (.ex a) + m))
        (sub_cons_of (seq_mono T K S0 (Nat.le_add_left m _) _ hm) (seq_mono T K S0 (Nat.le_add_right _ m))))).elim

include c0 in
theorem henkin_all_U (b : Fm) (h : ¬ UU T K S0 (.all b)) : ∃ y, ¬ UU T K S0 (inst y b) := by
  have e : seq T K S0 (enc K (.all b) + 1) = step T K (seq T K S0 (enc K (.all b))) (.all b) := by
    show step T K (seq T K S0 (enc K (.all b))) (decF K (enc K (.all b))) = _
    rw [decF_enc]
  revert e
  unfold step
  cases hd : dec K (Con T (.all b :: seq T K S0 (enc K (.all b)))) with
  | true => exact fun e => (h ⟨enc K (.all b) + 1, by rw [e]; exact .head _⟩).elim
  | false => exact fun e => ⟨_, fun hu => not_both T K S0 c0 hu ⟨enc K (.all b) + 1, by rw [e]; exact .head _⟩⟩

/-! ## the term model and the truth lemma -/

/-- two-valued Tarski semantics -/
def Sat {D : Type _} (R : Nat → List D → Prop) : Fm → (Nat → D) → Prop
  | .atom r l, v => R r (mapD v l)
  | .bot, _ => False
  | .conj a b, v => Sat R a v ∧ Sat R b v
  | .disj a b, v => Sat R a v ∨ Sat R b v
  | .imp a b, v => Sat R a v → Sat R b v
  | .all a, v => ∀ d, Sat R a (ext v d)
  | .ex a, v => ∃ d, Sat R a (ext v d)

include c0 in
/-- the truth lemma for the term model on ℕ -/
theorem truth : (φ : Fm) → (v : Nat → Nat) → (Sat (fun r l => UU T K S0 (.atom r l)) φ v ↔ UU T K S0 (ren v φ))
  | .atom r l, v => by
    show UU T K S0 (.atom r (mapD v l)) ↔ UU T K S0 (.atom r (mapL v l))
    rw [mapD_mapL']
  | .bot, _ => ⟨False.elim, bot_not T K S0 c0⟩
  | .conj a b, v =>
    ⟨fun ⟨ha, hb⟩ => closed2 T K S0 c0 ((truth a v).1 ha) ((truth b v).1 hb)
        (.conjI (.hyp (.head _)) (.hyp (.tail _ (.head _)))),
     fun h => ⟨(truth a v).2 (closed1 T K S0 c0 h (.conjL (.hyp (.head _)))),
       (truth b v).2 (closed1 T K S0 c0 h (.conjR (.hyp (.head _))))⟩⟩
  | .disj a b, v =>
    ⟨fun h => h.elim (fun ha => closed1 T K S0 c0 ((truth a v).1 ha) (.disjL (.hyp (.head _))))
        (fun hb => closed1 T K S0 c0 ((truth b v).1 hb) (.disjR (.hyp (.head _)))),
     fun h => (decided T K S0 (ren v a)).elim (fun ha => Or.inl ((truth a v).2 ha))
       (fun na => (decided T K S0 (ren v b)).elim (fun hb => Or.inr ((truth b v).2 hb))
         (fun nb => (not_both T K S0 c0 h (closed2 T K S0 c0 na nb
           (.impI (.disjE (.hyp (.head _))
             (.impE (.hyp (.tail _ (.tail _ (.head _)))) (.hyp (.head _)))
             (.impE (.hyp (.tail _ (.tail _ (.tail _ (.head _))))) (.hyp (.head _))))))).elim))⟩
  | .imp a b, v =>
    ⟨fun h => (K.lem (UU T K S0 (ren v a))).elim
        (fun ha => closed1 T K S0 c0 ((truth b v).1 (h ((truth a v).2 ha))) (.impI (.hyp (.tail _ (.head _)))))
        (fun na => (decided T K S0 (ren v a)).elim (fun ha => (na ha).elim)
          (fun hn => closed1 T K S0 c0 hn (.impI (.botE (.impE (.hyp (.tail _ (.head _))) (.hyp (.head _))))))),
     fun h ha => (truth b v).2 (closed2 T K S0 c0 h ((truth a v).1 ha)
        (.impE (.hyp (.head _)) (.hyp (.tail _ (.head _)))))⟩
  | .all a, v =>
    ⟨fun h => (K.lem (UU T K S0 (.all (ren (up v) a)))).elim id (fun hn =>
        match henkin_all_U T K S0 c0 _ hn with
        | ⟨y, hy⟩ => (hy (by
            have := (truth a (ext v y)).1 (h y)
            rw [inst_ext] at this
            exact this)).elim),
     fun h d => (truth a (ext v d)).2 (by
        rw [inst_ext]
        exact closed1 T K S0 c0 h (.allE d (.hyp (.head _))))⟩
  | .ex a, v =>
    ⟨fun ⟨d, hd⟩ => closed1 T K S0 c0 (by
        have := (truth a (ext v d)).1 hd
        rw [inst_ext] at this
        exact this) (.exI d (.hyp (.head _))),
     fun h => match henkin_ex_U T K S0 c0 _ h with
       | ⟨y, hy⟩ => ⟨y, (truth a (ext v y)).2 (by rw [inst_ext]; exact hy)⟩⟩

end Henkin

/-! ## soundness and completeness -/

theorem sat_ext {D : Type _} (R : Nat → List D → Prop) : (φ : Fm) → {v w : Nat → D} → (∀ n, v n = w n) →
    (Sat R φ v ↔ Sat R φ w)
  | .atom r l, _, _, h => by
    show R r (mapD _ l) ↔ R r (mapD _ l)
    rw [mapD_ext h l]
  | .bot, _, _, _ => Iff.rfl
  | .conj a b, _, _, h => ⟨fun ⟨x, y⟩ => ⟨(sat_ext R a h).1 x, (sat_ext R b h).1 y⟩,
      fun ⟨x, y⟩ => ⟨(sat_ext R a h).2 x, (sat_ext R b h).2 y⟩⟩
  | .disj a b, _, _, h => ⟨fun x => x.elim (fun x => Or.inl ((sat_ext R a h).1 x)) (fun y => Or.inr ((sat_ext R b h).1 y)),
      fun x => x.elim (fun x => Or.inl ((sat_ext R a h).2 x)) (fun y => Or.inr ((sat_ext R b h).2 y))⟩
  | .imp a b, _, _, h => ⟨fun f x => (sat_ext R b h).1 (f ((sat_ext R a h).2 x)),
      fun f x => (sat_ext R b h).2 (f ((sat_ext R a h).1 x))⟩
  | .all a, _, _, h => ⟨fun f d => (sat_ext R a (ext_ext h d)).1 (f d), fun f d => (sat_ext R a (ext_ext h d)).2 (f d)⟩
  | .ex a, _, _, h => ⟨fun ⟨d, x⟩ => ⟨d, (sat_ext R a (ext_ext h d)).1 x⟩, fun ⟨d, x⟩ => ⟨d, (sat_ext R a (ext_ext h d)).2 x⟩⟩

theorem sat_ren {D : Type _} (R : Nat → List D → Prop) : (φ : Fm) → (ρ : Nat → Nat) → (v : Nat → D) →
    (Sat R (ren ρ φ) v ↔ Sat R φ (fun n => v (ρ n)))
  | .atom r l, ρ, v => by
    show R r (mapD v (mapL ρ l)) ↔ R r (mapD _ l)
    rw [mapD_mapL]
  | .bot, _, _ => Iff.rfl
  | .conj a b, ρ, v => ⟨fun ⟨x, y⟩ => ⟨(sat_ren R a ρ v).1 x, (sat_ren R b ρ v).1 y⟩,
      fun ⟨x, y⟩ => ⟨(sat_ren R a ρ v).2 x, (sat_ren R b ρ v).2 y⟩⟩
  | .disj a b, ρ, v => ⟨fun x => x.elim (fun x => Or.inl ((sat_ren R a ρ v).1 x)) (fun y => Or.inr ((sat_ren R b ρ v).1 y)),
      fun x => x.elim (fun x => Or.inl ((sat_ren R a ρ v).2 x)) (fun y => Or.inr ((sat_ren R b ρ v).2 y))⟩
  | .imp a b, ρ, v => ⟨fun f x => (sat_ren R b ρ v).1 (f ((sat_ren R a ρ v).2 x)),
      fun f x => (sat_ren R b ρ v).2 (f ((sat_ren R a ρ v).1 x))⟩
  | .all a, ρ, v => ⟨fun f d => (sat_ext R a (ext_up v ρ d)).1 ((sat_ren R a (up ρ) (ext v d)).1 (f d)),
      fun f d => (sat_ren R a (up ρ) (ext v d)).2 ((sat_ext R a (ext_up v ρ d)).2 (f d))⟩
  | .ex a, ρ, v => ⟨fun ⟨d, x⟩ => ⟨d, (sat_ext R a (ext_up v ρ d)).1 ((sat_ren R a (up ρ) (ext v d)).1 x)⟩,
      fun ⟨d, x⟩ => ⟨d, (sat_ren R a (up ρ) (ext v d)).2 ((sat_ext R a (ext_up v ρ d)).2 x)⟩⟩

theorem sat_shift {D : Type _} (R : Nat → List D → Prop) (c : Fm) (v : Nat → D) (d : D) :
    Sat R (shift c) (ext v d) ↔ Sat R c v :=
  (sat_ren R c Nat.succ (ext v d)).trans (sat_ext R c (fun _ => rfl))

theorem sat_inst {D : Type _} (R : Nat → List D → Prop) (y : Nat) (a : Fm) (v : Nat → D) :
    Sat R (inst y a) v ↔ Sat R a (ext v (v y)) :=
  (sat_ren R a (cons0 y) v).trans (sat_ext R a (fun n => match n with
    | 0 => rfl
    | _ + 1 => rfl))

/-- soundness of natural deduction for two-valued semantics -/
theorem sound {D : Type _} (R : Nat → List D → Prop) {Γ : List Fm} {φ : Fm} (d : Pf Γ φ) :
    ∀ v, (∀ γ, γ ∈ Γ → Sat R γ v) → Sat R φ v := by
  induction d with
  | hyp m => exact fun v h => h _ m
  | botE _ ih => exact fun v h => (ih v h).elim
  | conjI _ _ ih1 ih2 => exact fun v h => ⟨ih1 v h, ih2 v h⟩
  | conjL _ ih => exact fun v h => (ih v h).1
  | conjR _ ih => exact fun v h => (ih v h).2
  | disjL _ ih => exact fun v h => Or.inl (ih v h)
  | disjR _ ih => exact fun v h => Or.inr (ih v h)
  | disjE _ _ _ ih ih1 ih2 => exact (fun v h => (ih v h).elim
      (fun x => ih1 v (fun γ m => by cases m with | head => exact x | tail _ m' => exact h γ m'))
      (fun y => ih2 v (fun γ m => by cases m with | head => exact y | tail _ m' => exact h γ m')))
  | impI _ ih => exact fun v h x => ih v (fun γ m => by cases m with | head => exact x | tail _ m' => exact h γ m')
  | impE _ _ ih1 ih2 => exact fun v h => ih1 v h (ih2 v h)
  | allI _ ih => exact (fun v h d => ih (ext v d) (fun γ m => match mem_sh _ m with
      | ⟨b, hb, e⟩ => by rw [e]; exact (sat_shift R b v d).2 (h b hb)))
  | allE y _ ih => exact fun v h => (sat_inst R y _ v).2 (ih v h (v y))
  | exI y _ ih => exact fun v h => ⟨v y, (sat_inst R y _ v).1 (ih v h)⟩
  | exE _ _ ih1 ih2 => exact (fun v h => match ih1 v h with
      | ⟨d, x⟩ => (sat_shift R _ v d).1 (ih2 (ext v d) (fun γ m => by
          cases m with
          | head => exact x
          | tail _ m' => exact match mem_sh _ m' with
            | ⟨b, hb, e⟩ => by rw [e]; exact (sat_shift R b v d).2 (h b hb))))

/-- the models of T: every member is true under every valuation -/
def ModT {D : Type _} (R : Nat → List D → Prop) : Prop := ∀ b, T b → ∀ w : Nat → D, Sat R b w

include K in
theorem sat_ax {D : Type _} (R : Nat → List D → Prop) (hM : ModT T R) {a : Fm} (ha : Ax T a) (v : Nat → D) :
    Sat R a v :=
  match ha with
  | .inl ⟨b, e⟩ => e ▸ K.lem (Sat R b v)
  | .inr ⟨b, σ, hb, e⟩ => e ▸ (sat_ren R b σ v).2 (hM b hb _)

include K in
theorem sat_clo {D : Type _} (R : Nat → List D → Prop) (hM : ModT T R) {a : Fm} (ha : Ax T a) :
    (j : Nat) → ∀ v, Sat R (clo j a) v
  | 0, v => sat_ax T K R hM ha v
  | j + 1, v => fun d => sat_clo R hM ha j (ext v d)

/-- truth in every two-valued model of T -/
def ValidT (Γ : List Fm) (φ : Fm) : Prop :=
  ∀ (D : Type) (R : Nat → List D → Prop) (v : Nat → D), ModT T R → (∀ γ, γ ∈ Γ → Sat R γ v) → Sat R φ v

include K in
theorem soundnessT {Γ : List Fm} {φ : Fm} (h : PC T Γ φ) : ValidT T Γ φ := fun _ R v hM hΓ =>
  match h with
  | ⟨Δ, cΔ, d⟩ => sound R d v (fun γ m => (mem_app_inv' Δ Γ m).elim
      (fun mΔ => match cΔ γ mΔ with | ⟨j, _, ha, e⟩ => e ▸ sat_clo T K R hM ha j v) (hΓ γ))

include K in
/-- soundness for models in any universe -/
theorem soundnessU {Γ : List Fm} {φ : Fm} (h : PC T Γ φ) {D : Type _} (R : Nat → List D → Prop) (v : Nat → D)
    (hM : ModT T R) (hΓ : ∀ γ, γ ∈ Γ → Sat R γ v) : Sat R φ v :=
  match h with
  | ⟨Δ, cΔ, d⟩ => sound R d v (fun γ m => (mem_app_inv' Δ Γ m).elim
      (fun mΔ => match cΔ γ mΔ with | ⟨j, _, ha, e⟩ => e ▸ sat_clo T K R hM ha j v) (hΓ γ))

include K in
/-- every axiom lies in the Henkin set -/
theorem uu_ax (S0 : List Fm) (c0 : Con T S0) {c : Fm} (hc : InT T c) : UU T K S0 c :=
  (decided T K S0 c).elim id (fun h => match h with
    | ⟨n, m⟩ =>
      have cc : CtxT T [c] := fun _ m' => by
        cases m' with
        | head => exact hc
        | tail _ m'' => exact nomatch m''
      have d : Pf ([c] ++ seq T K S0 n) Fm.bot := .impE (.hyp (.tail _ m)) (.hyp (.head _))
      (con_seq T K S0 c0 n ⟨[c], cc, d⟩).elim)

include K in
/-- Gödel's completeness theorem for an arbitrary (possibly infinite) theory T
  (two-valued, in the classical point): true in every model of T ⟹ derivable
  from finitely many axioms of T and excluded-middle instances -/
theorem completenessT {Γ : List Fm} {φ : Fm} (h : ValidT T Γ φ) : PC T Γ φ := dne K _ (fun hn => by
  have c0 : Con T (neg φ :: Γ) := fun ⟨Δ, cΔ, d⟩ => hn ⟨lemF φ :: Δ, ctx_cons (lem_in φ) cΔ,
    .disjE (.hyp (.head _)) (.hyp (.head _)) (.botE (weaken d (sub_app_of
      (sub_trans' (sub_appL' Δ Γ) (sub_trans' (sub_tl _ _) (sub_tl _ _)))
      (sub_cons_of (.head _) (sub_trans' (sub_appR' Δ Γ) (sub_trans' (sub_tl _ _) (sub_tl _ _)))))))⟩
  have hM : ModT T (fun r l => UU T K (neg φ :: Γ) (.atom r l)) := fun b hb w =>
    (truth T K _ c0 b w).2 (uu_ax T K _ c0 ⟨0, ren w b, .inr ⟨b, w, hb, rfl⟩, rfl⟩)
  have hv := h Nat (fun r l => UU T K (neg φ :: Γ) (.atom r l)) (fun n => n) hM (fun γ m =>
    (truth T K _ c0 γ (fun n => n)).2 (by rw [ren_id]; exact ⟨0, .tail _ m⟩))
  have u := (truth T K _ c0 φ (fun n => n)).1 hv
  rw [ren_id] at u
  exact not_both T K _ c0 u ⟨0, .head _⟩)

include K in
theorem complete_iffT (Γ : List Fm) (φ : Fm) : PC T Γ φ ↔ ValidT T Γ φ := ⟨soundnessT T K, completenessT T K⟩

end FOCT

end Found

#print axioms Found.FOCT.inst0_shift
#print axioms Found.FOCT.pf_allL
#print axioms Found.FOCT.discharge
#print axioms Found.FOCT.decide_con
#print axioms Found.FOCT.henkin_ex
#print axioms Found.FOCT.henkin_all
#print axioms Found.FOCT.enc_inj
#print axioms Found.FOCT.decF_enc
#print axioms Found.FOCT.con_step
#print axioms Found.FOCT.con_seq
#print axioms Found.FOCT.decided
#print axioms Found.FOCT.closed2
#print axioms Found.FOCT.henkin_ex_U
#print axioms Found.FOCT.henkin_all_U
#print axioms Found.FOCT.truth
#print axioms Found.FOCT.sat_ren
#print axioms Found.FOCT.sound
#print axioms Found.FOCT.soundnessT
#print axioms Found.FOCT.soundnessU
#print axioms Found.FOCT.completenessT
#print axioms Found.FOCT.complete_iffT

found/FOCompact.lean

lean_all/found/FOCompact.lean

/-
  T208 (L2) slices L2b, L2c. found/FOCompact —
  * Löwenheim–Skolem (Heyting-valued form): validity in all safe structures
    equals validity in the single Lindenbaum structure, whose domain is ℕ.
  * Compactness / strong completeness for a theory T of sentences (possibly
    infinite): if every safe structure in which c lies below the values of
    all of T also has c below φ's value, then φ is derivable from finitely
    many members of T.  Built from the T-relative Lindenbaum structure
    (a ≤ b iff some finite part Γ of T gives a, Γ ⊢ b).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.FOCompleteness

namespace Found

namespace FO

/-! ## Löwenheim–Skolem -/

def ValidIn (M : Str) (Γ : List Fm) (φ : Fm) : Prop :=
  ∀ (v : Nat → M.D) (c : M.H.car), (∀ γ x, γ ∈ Γ → Val M γ v x → M.H.le c x) → ∀ y, Val M φ v y → M.H.le c y

theorem complete_LS {Γ : List Fm} {φ : Fm} (h : ValidIn LS Γ φ) : Pf Γ φ := by
  have key : Pf [bigConj Γ] (ren (fun n => n) φ) :=
    h (fun n => n) (bigConj Γ) (fun γ x m hx =>
      cut (big_mem Γ m) (by
        have e := val_le (M := LS) γ (val_lind γ (fun n => n)) hx
        rw [ren_id] at e
        exact e)) _ (val_lind φ (fun n => n))
  rw [ren_id] at key
  exact cut (big_intro Γ) key

/-- a consequence valid in the countable (domain ℕ) Lindenbaum structure is valid everywhere -/
theorem lowenheim_skolem (Γ : List Fm) (φ : Fm) : ValidIn LS Γ φ ↔ Valid Γ φ :=
  ⟨fun h => soundness (complete_LS h), fun h => h LS lind_safe⟩

/-! ## T-relative Lindenbaum structure -/

def Ctx (T : Fm → Prop) (Γ : List Fm) : Prop := ∀ γ, γ ∈ Γ → T γ

theorem mem_app_inv' {a : Fm} : (l k : List Fm) → a ∈ l ++ k → a ∈ l ∨ a ∈ k
  | [], _, m => Or.inr m
  | _ :: l, k, m => by
    cases m with
    | head => exact Or.inl (.head _)
    | tail _ m' => exact (mem_app_inv' l k m').elim (fun h => Or.inl (.tail _ h)) Or.inr

theorem mem_app_l' {a : Fm} : (l k : List Fm) → a ∈ l → a ∈ l ++ k
  | _ :: _, _, .head _ => .head _
  | _ :: l, k, .tail _ m => .tail _ (mem_app_l' l k m)

theorem mem_app_r' {a : Fm} : (l k : List Fm) → a ∈ k → a ∈ l ++ k
  | [], _, m => m
  | _ :: l, k, m => .tail _ (mem_app_r' l k m)

theorem ctx_app {T : Fm → Prop} {Γ Δ : List Fm} (h1 : Ctx T Γ) (h2 : Ctx T Δ) : Ctx T (Γ ++ Δ) :=
  fun γ m => (mem_app_inv' Γ Δ m).elim (h1 γ) (h2 γ)

theorem sub_l (a : Fm) (Γ Δ : List Fm) : Sub (a :: Γ) (a :: (Γ ++ Δ)) := fun b m => by
  cases m with
  | head => exact .head _
  | tail _ m' => exact .tail _ (mem_app_l' Γ Δ m')

theorem sub_r (a : Fm) (Γ Δ : List Fm) : Sub (a :: Δ) (a :: (Γ ++ Δ)) := fun b m => by
  cases m with
  | head => exact .head _
  | tail _ m' => exact .tail _ (mem_app_r' Γ Δ m')

theorem cutC {Γ : List Fm} {a b c : Fm} (h1 : Pf (a :: Γ) b) (h2 : Pf (b :: Γ) c) : Pf (a :: Γ) c :=
  .impE (weaken (Pf.impI h2) (fun _ m => .tail _ m)) h1

theorem ctx_nil (T : Fm → Prop) : Ctx T [] := fun _ m => nomatch m

def LT (T : Fm → Prop) : HA.{0} where
  car := Fm
  le a b := ∃ Γ, Ctx T Γ ∧ Pf (a :: Γ) b
  le_refl _ := ⟨[], ctx_nil T, .hyp (.head _)⟩
  le_trans := fun ⟨Γ, hΓ, h1⟩ ⟨Δ, hΔ, h2⟩ =>
    ⟨Γ ++ Δ, ctx_app hΓ hΔ, cutC (weaken h1 (sub_l _ Γ Δ)) (weaken h2 (sub_r _ Γ Δ))⟩
  top := .imp .bot .bot
  bot := .bot
  meet := .conj
  join := .disj
  imp := .imp
  le_top _ := ⟨[], ctx_nil T, .impI (.hyp (.head _))⟩
  bot_le _ := ⟨[], ctx_nil T, .botE (.hyp (.head _))⟩
  meet_l _ _ := ⟨[], ctx_nil T, .conjL (.hyp (.head _))⟩
  meet_r _ _ := ⟨[], ctx_nil T, .conjR (.hyp (.head _))⟩
  meet_univ := fun ⟨Γ, hΓ, h1⟩ ⟨Δ, hΔ, h2⟩ =>
    ⟨Γ ++ Δ, ctx_app hΓ hΔ, .conjI (weaken h1 (sub_l _ Γ Δ)) (weaken h2 (sub_r _ Γ Δ))⟩
  join_l _ _ := ⟨[], ctx_nil T, .disjL (.hyp (.head _))⟩
  join_r _ _ := ⟨[], ctx_nil T, .disjR (.hyp (.head _))⟩
  join_univ := fun ⟨Γ, hΓ, h1⟩ ⟨Δ, hΔ, h2⟩ =>
    ⟨Γ ++ Δ, ctx_app hΓ hΔ, .disjE (.hyp (.head _))
      (weaken h1 (fun b m => by
        cases m with
        | head => exact .head _
        | tail _ m' => exact .tail _ (.tail _ (mem_app_l' Γ Δ m'))))
      (weaken h2 (fun b m => by
        cases m with
        | head => exact .head _
        | tail _ m' => exact .tail _ (.tail _ (mem_app_r' Γ Δ m'))))⟩
  imp_adj := ⟨fun ⟨Γ, hΓ, h⟩ => ⟨Γ, hΓ, .impI (.impE (weaken (Pf.impI h) (fun _ m => .tail _ (.tail _ m)))
      (.conjI (.hyp (.tail _ (.head _))) (.hyp (.head _))))⟩,
    fun ⟨Γ, hΓ, h⟩ => ⟨Γ, hΓ, .impE (cutC (.conjL (.hyp (.head _))) (weaken h (fun b m => by
        cases m with
        | head => exact .head _
        | tail _ m' => exact .tail _ m'))) (.conjR (.hyp (.head _)))⟩⟩

/-! ## sentences, quantifier lemmas relative to T -/

def Sent (T : Fm → Prop) : Prop := ∀ γ, T γ → bound γ = 0

theorem ren_closed (ρ : Nat → Nat) (γ : Fm) (h : bound γ = 0) : ren ρ γ = γ :=
  (ren_bound γ (σ := fun n => n) (fun n hn => absurd (h ▸ hn) (Nat.not_lt_zero n))).trans (ren_id γ)

theorem mapF_closed {T : Fm → Prop} (hT : Sent T) (ρ : Nat → Nat) : (Γ : List Fm) → Ctx T Γ → mapF ρ Γ = Γ
  | [], _ => rfl
  | γ :: Γ, h => by
    show ren ρ γ :: mapF ρ Γ = γ :: Γ
    rw [ren_closed ρ γ (hT γ (h γ (.head _))), mapF_closed hT ρ Γ (fun g m => h g (.tail _ m))]

theorem all_inf_T {T : Fm → Prop} (hT : Sent T) (ψ a : Fm)
    (h : ∀ y, ∃ Γ, Ctx T Γ ∧ Pf (ψ :: Γ) (inst y a)) : ∃ Γ, Ctx T Γ ∧ Pf (ψ :: Γ) (.all a) :=
  match h (bound ψ + bound a) with
  | ⟨Γ, hΓ, d⟩ => by
    have hN := pf_ren d (sig (bound ψ + bound a))
    rw [sig_inst _ a (Nat.le_succ_of_le (Nat.le_add_left _ _))] at hN
    have e : mapF (sig (bound ψ + bound a)) (ψ :: Γ) = sh (ψ :: Γ) := by
      show ren (sig (bound ψ + bound a)) ψ :: mapF (sig (bound ψ + bound a)) Γ = shift ψ :: mapF Nat.succ Γ
      rw [sig_shift _ ψ (Nat.le_add_right _ _), mapF_closed hT _ Γ hΓ, mapF_closed hT _ Γ hΓ]
    rw [e] at hN
    exact ⟨Γ, hΓ, .allI hN⟩

theorem ex_sup_T {T : Fm → Prop} (hT : Sent T) (ψ a : Fm)
    (h : ∀ y, ∃ Γ, Ctx T Γ ∧ Pf (inst y a :: Γ) ψ) : ∃ Γ, Ctx T Γ ∧ Pf (.ex a :: Γ) ψ :=
  match h (bound ψ + bound a) with
  | ⟨Γ, hΓ, d⟩ => by
    have hN := pf_ren d (sig (bound ψ + bound a))
    have e : mapF (sig (bound ψ + bound a)) (inst (bound ψ + bound a) a :: Γ) = a :: Γ := by
      show ren (sig (bound ψ + bound a)) (inst (bound ψ + bound a) a) :: mapF (sig (bound ψ + bound a)) Γ = a :: Γ
      rw [sig_inst _ a (Nat.le_succ_of_le (Nat.le_add_left _ _)), mapF_closed hT _ Γ hΓ]
    rw [e, sig_shift _ ψ (Nat.le_add_right _ _)] at hN
    have e2 : sh (Fm.ex a :: Γ) = shift (.ex a) :: Γ := by
      show shift (.ex a) :: mapF Nat.succ Γ = _
      rw [mapF_closed hT _ Γ hΓ]
    refine ⟨Γ, hΓ, .exE (.hyp (.head _)) ?_⟩
    rw [e2]
    exact weaken hN (fun b m => by
      cases m with
      | head => exact .head _
      | tail _ m' => exact .tail _ (.tail _ m'))

def LST (T : Fm → Prop) : Str := ⟨LT T, Nat, fun r l => .atom r l⟩

theorem val_lindT {T : Fm → Prop} (hT : Sent T) : (φ : Fm) → (v : Nat → Nat) → Val (LST T) φ v (ren v φ)
  | .atom r l, v => by
    show Val (LST T) (.atom r l) v (Fm.atom r (mapL v l))
    rw [← mapD_mapL']
    exact .atom
  | .bot, _ => .bot
  | .conj a b, v => .conj (val_lindT hT a v) (val_lindT hT b v)
  | .disj a b, v => .disj (val_lindT hT a v) (val_lindT hT b v)
  | .imp a b, v => .imp (val_lindT hT a v) (val_lindT hT b v)
  | .all a, v => .all (fun d => ren (ext v d) a) (fun d => val_lindT hT a (ext v d))
      (fun (d : Nat) => ⟨[], ctx_nil T, by
        show Pf [Fm.all (ren (up v) a)] (ren (ext (D := Nat) v d) a)
        rw [inst_ext]
        exact .allE d (.hyp (.head _))⟩)
      (fun c hc => all_inf_T hT c (ren (up v) a) (fun (y : Nat) =>
        match hc y with
        | ⟨Γ, hΓ, h⟩ => ⟨Γ, hΓ, by
          have h' : Pf (c :: Γ) (ren (ext (D := Nat) v y) a) := h
          rw [inst_ext] at h'
          exact h'⟩))
  | .ex a, v => .ex (fun d => ren (ext v d) a) (fun d => val_lindT hT a (ext v d))
      (fun (d : Nat) => ⟨[], ctx_nil T, by
        show Pf [ren (ext (D := Nat) v d) a] (Fm.ex (ren (up v) a))
        rw [inst_ext]
        exact .exI d (.hyp (.head _))⟩)
      (fun c hc => ex_sup_T hT c (ren (up v) a) (fun (y : Nat) =>
        match hc y with
        | ⟨Γ, hΓ, h⟩ => ⟨Γ, hΓ, by
          have h' : Pf (ren (ext (D := Nat) v y) a :: Γ) c := h
          rw [inst_ext] at h'
          exact h'⟩))

theorem lindT_safe {T : Fm → Prop} (hT : Sent T) : Safe (LST T) := fun φ v => ⟨ren v φ, val_lindT hT φ v⟩

/-- semantic consequence from a (possibly infinite) theory -/
def TValid (T : Fm → Prop) (φ : Fm) : Prop :=
  ∀ M : Str, Safe M → ∀ (v : Nat → M.D) (c : M.H.car),
    (∀ γ x, T γ → Val M γ v x → M.H.le c x) → ∀ y, Val M φ v y → M.H.le c y

/-- compactness (strong completeness): a semantic consequence of a theory of
  sentences follows from finitely many of its members -/
theorem compactness {T : Fm → Prop} (hT : Sent T) {φ : Fm} (h : TValid T φ) :
    ∃ Γ, Ctx T Γ ∧ Pf Γ φ := by
  have key := h (LST T) (lindT_safe hT) (fun n => n) (.imp .bot .bot) (fun γ x hγ hx =>
    match val_le (M := LST T) γ (val_lindT hT γ (fun n => n)) hx with
    | ⟨Γ', hΓ', d⟩ => ⟨γ :: Γ', fun g m => by
        cases m with
        | head => exact hγ
        | tail _ m' => exact hΓ' g m', by
        rw [ren_id] at d
        exact weaken d (fun b m => by
          cases m with
          | head => exact .tail _ (.head _)
          | tail _ m' => exact .tail _ (.tail _ m'))⟩) _ (val_lindT hT φ (fun n => n))
  match key with
  | ⟨Γ, hΓ, d⟩ => exact ⟨Γ, hΓ, .impE (.impI (by rw [ren_id] at d; exact d)) (.impI (.hyp (.head _)))⟩

/-- and conversely (soundness for theories) -/
theorem compactness_converse {T : Fm → Prop} {φ : Fm} (h : ∃ Γ, Ctx T Γ ∧ Pf Γ φ) : TValid T φ :=
  fun M hM v c hc y hy => match h with
  | ⟨_, hΓ, d⟩ => sound d M hM v c (fun γ x m hx => hc γ x (hΓ γ m) hx) y hy

end FO

end Found

#print axioms Found.FO.lowenheim_skolem
#print axioms Found.FO.LT
#print axioms Found.FO.all_inf_T
#print axioms Found.FO.ex_sup_T
#print axioms Found.FO.val_lindT
#print axioms Found.FO.compactness
#print axioms Found.FO.compactness_converse

found/FOCompleteness.lean

lean_all/found/FOCompleteness.lean

/-
  T208 (L2). found/FOCompleteness — first-order intuitionistic logic:
  syntax (pure predicate logic, de Bruijn variables), natural deduction,
  admissible renaming; (later parts) Heyting-valued semantics with
  quantifiers as existing infima/suprema, soundness and completeness via the
  Lindenbaum structure, compactness, and Löwenheim–Skolem (the term model
  has domain ℕ).
  Part 1: syntax, renaming lemmas, derivations, renaming admissible.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Compactness

namespace Found

namespace FO

inductive Fm where
  | atom : Nat → List Nat → Fm
  | bot : Fm
  | conj : Fm → Fm → Fm
  | disj : Fm → Fm → Fm
  | imp : Fm → Fm → Fm
  | all : Fm → Fm
  | ex : Fm → Fm

def up (ρ : Nat → Nat) : Nat → Nat
  | 0 => 0
  | n + 1 => ρ n + 1

def mapL (ρ : Nat → Nat) : List Nat → List Nat
  | [] => []
  | a :: l => ρ a :: mapL ρ l

def ren (ρ : Nat → Nat) : Fm → Fm
  | .atom r l => .atom r (mapL ρ l)
  | .bot => .bot
  | .conj a b => .conj (ren ρ a) (ren ρ b)
  | .disj a b => .disj (ren ρ a) (ren ρ b)
  | .imp a b => .imp (ren ρ a) (ren ρ b)
  | .all a => .all (ren (up ρ) a)
  | .ex a => .ex (ren (up ρ) a)

theorem mapL_ext {ρ σ : Nat → Nat} (h : ∀ n, ρ n = σ n) : (l : List Nat) → mapL ρ l = mapL σ l
  | [] => rfl
  | a :: l => by show ρ a :: mapL ρ l = σ a :: mapL σ l; rw [h a, mapL_ext h l]

theorem mapL_comp (ρ σ : Nat → Nat) : (l : List Nat) → mapL ρ (mapL σ l) = mapL (fun n => ρ (σ n)) l
  | [] => rfl
  | a :: l => congrArg (List.cons (ρ (σ a))) (mapL_comp ρ σ l)

theorem up_ext {ρ σ : Nat → Nat} (h : ∀ n, ρ n = σ n) : ∀ n, up ρ n = up σ n
  | 0 => rfl
  | n + 1 => congrArg (· + 1) (h n)

theorem up_comp (ρ σ : Nat → Nat) : ∀ n, up ρ (up σ n) = up (fun n => ρ (σ n)) n
  | 0 => rfl
  | _ + 1 => rfl

theorem ren_ext : (φ : Fm) → {ρ σ : Nat → Nat} → (∀ n, ρ n = σ n) → ren ρ φ = ren σ φ
  | .atom r l, _, _, h => congrArg (Fm.atom r) (mapL_ext h l)
  | .bot, _, _, _ => rfl
  | .conj a b, _, _, h => by show Fm.conj _ _ = Fm.conj _ _; rw [ren_ext a h, ren_ext b h]
  | .disj a b, _, _, h => by show Fm.disj _ _ = Fm.disj _ _; rw [ren_ext a h, ren_ext b h]
  | .imp a b, _, _, h => by show Fm.imp _ _ = Fm.imp _ _; rw [ren_ext a h, ren_ext b h]
  | .all a, _, _, h => congrArg Fm.all (ren_ext a (up_ext h))
  | .ex a, _, _, h => congrArg Fm.ex (ren_ext a (up_ext h))

theorem ren_ren : (φ : Fm) → (ρ σ : Nat → Nat) → ren ρ (ren σ φ) = ren (fun n => ρ (σ n)) φ
  | .atom r l, ρ, σ => congrArg (Fm.atom r) (mapL_comp ρ σ l)
  | .bot, _, _ => rfl
  | .conj a b, ρ, σ => by show Fm.conj _ _ = Fm.conj _ _; rw [ren_ren a ρ σ, ren_ren b ρ σ]
  | .disj a b, ρ, σ => by show Fm.disj _ _ = Fm.disj _ _; rw [ren_ren a ρ σ, ren_ren b ρ σ]
  | .imp a b, ρ, σ => by show Fm.imp _ _ = Fm.imp _ _; rw [ren_ren a ρ σ, ren_ren b ρ σ]
  | .all a, ρ, σ => congrArg Fm.all ((ren_ren a (up ρ) (up σ)).trans (ren_ext a (up_comp ρ σ)))
  | .ex a, ρ, σ => congrArg Fm.ex ((ren_ren a (up ρ) (up σ)).trans (ren_ext a (up_comp ρ σ)))

def shift (φ : Fm) : Fm := ren Nat.succ φ

def cons0 (y : Nat) : Nat → Nat
  | 0 => y
  | n + 1 => n

/-- instantiate the bound variable 0 of a body by the variable y -/
def inst (y : Nat) (a : Fm) : Fm := ren (cons0 y) a

theorem ren_shift (ρ : Nat → Nat) (φ : Fm) : ren (up ρ) (shift φ) = shift (ren ρ φ) :=
  (ren_ren φ (up ρ) Nat.succ).trans ((ren_ext φ (fun _ => rfl)).trans (ren_ren φ Nat.succ ρ).symm)

theorem ren_inst (ρ : Nat → Nat) (y : Nat) (a : Fm) : ren ρ (inst y a) = inst (ρ y) (ren (up ρ) a) := by
  unfold inst
  rw [ren_ren, ren_ren]
  exact ren_ext a (fun n => match n with
    | 0 => rfl
    | _ + 1 => rfl)

def mapF (ρ : Nat → Nat) : List Fm → List Fm
  | [] => []
  | a :: l => ren ρ a :: mapF ρ l

def sh (Γ : List Fm) : List Fm := mapF Nat.succ Γ

theorem mapF_sh (ρ : Nat → Nat) : (Γ : List Fm) → mapF (up ρ) (sh Γ) = sh (mapF ρ Γ)
  | [] => rfl
  | a :: Γ => by
    show ren (up ρ) (shift a) :: mapF (up ρ) (sh Γ) = shift (ren ρ a) :: sh (mapF ρ Γ)
    rw [ren_shift, mapF_sh ρ Γ]

theorem mem_mapF (ρ : Nat → Nat) {a : Fm} : (Γ : List Fm) → a ∈ Γ → ren ρ a ∈ mapF ρ Γ
  | [], m => nomatch m
  | _ :: Γ, m => by
    cases m with
    | head => exact .head _
    | tail _ m' => exact .tail _ (mem_mapF ρ Γ m')

/-! ## natural deduction -/

inductive Pf : List Fm → Fm → Prop where
  | hyp {Γ a} : a ∈ Γ → Pf Γ a
  | botE {Γ a} : Pf Γ .bot → Pf Γ a
  | conjI {Γ a b} : Pf Γ a → Pf Γ b → Pf Γ (.conj a b)
  | conjL {Γ a b} : Pf Γ (.conj a b) → Pf Γ a
  | conjR {Γ a b} : Pf Γ (.conj a b) → Pf Γ b
  | disjL {Γ a b} : Pf Γ a → Pf Γ (.disj a b)
  | disjR {Γ a b} : Pf Γ b → Pf Γ (.disj a b)
  | disjE {Γ a b c} : Pf Γ (.disj a b) → Pf (a :: Γ) c → Pf (b :: Γ) c → Pf Γ c
  | impI {Γ a b} : Pf (a :: Γ) b → Pf Γ (.imp a b)
  | impE {Γ a b} : Pf Γ (.imp a b) → Pf Γ a → Pf Γ b
  | allI {Γ a} : Pf (sh Γ) a → Pf Γ (.all a)
  | allE {Γ a} (y : Nat) : Pf Γ (.all a) → Pf Γ (inst y a)
  | exI {Γ a} (y : Nat) : Pf Γ (inst y a) → Pf Γ (.ex a)
  | exE {Γ a c} : Pf Γ (.ex a) → Pf (a :: sh Γ) (shift c) → Pf Γ c

/-- renaming is admissible -/
theorem pf_ren {Γ : List Fm} {φ : Fm} (d : Pf Γ φ) : ∀ ρ : Nat → Nat, Pf (mapF ρ Γ) (ren ρ φ) := by
  induction d with
  | hyp m => exact fun ρ => .hyp (mem_mapF ρ _ m)
  | botE _ ih => exact fun ρ => .botE (ih ρ)
  | conjI _ _ iha ihb => exact fun ρ => .conjI (iha ρ) (ihb ρ)
  | conjL _ ih => exact fun ρ => .conjL (ih ρ)
  | conjR _ ih => exact fun ρ => .conjR (ih ρ)
  | disjL _ ih => exact fun ρ => .disjL (ih ρ)
  | disjR _ ih => exact fun ρ => .disjR (ih ρ)
  | disjE _ _ _ ih ih1 ih2 => exact fun ρ => .disjE (ih ρ) (ih1 ρ) (ih2 ρ)
  | impI _ ih => exact fun ρ => .impI (ih ρ)
  | impE _ _ ih1 ih2 => exact fun ρ => .impE (ih1 ρ) (ih2 ρ)
  | @allI Γ a _ ih =>
    exact fun ρ => .allI (by have h := ih (up ρ); rw [mapF_sh] at h; exact h)
  | @allE Γ a y _ ih =>
    exact fun ρ => by rw [ren_inst]; exact .allE (ρ y) (ih ρ)
  | @exI Γ a y _ ih =>
    exact fun ρ => .exI (ρ y) (by have h := ih ρ; rw [ren_inst] at h; exact h)
  | @exE Γ a c _ _ ih1 ih2 =>
    exact fun ρ => .exE (ih1 ρ) (by
      have h := ih2 (up ρ)
      show Pf (ren (up ρ) a :: sh (mapF ρ Γ)) (shift (ren ρ c))
      rw [← mapF_sh, ← ren_shift]
      exact h)

/-! ## Part 2: freshness, weakening, quantifiers as infima/suprema of instances -/

def sumN : List Nat → Nat
  | [] => 0
  | a :: l => a + sumN l

theorem le_sumN : (l : List Nat) → {a : Nat} → a ∈ l → a ≤ sumN l
  | [], _, m => nomatch m
  | _ :: l, _, m => by
    cases m with
    | head => exact Nat.le_add_right _ _
    | tail _ m' => exact Nat.le_trans (le_sumN l m') (Nat.le_add_left _ _)

/-- an upper bound for the free variables -/
def bound : Fm → Nat
  | .atom _ l => sumN l + 1
  | .bot => 0
  | .conj a b => bound a + bound b
  | .disj a b => bound a + bound b
  | .imp a b => bound a + bound b
  | .all a => bound a
  | .ex a => bound a

theorem mapL_bound {ρ σ : Nat → Nat} : (l : List Nat) → (∀ n, n < sumN l + 1 → ρ n = σ n) → mapL ρ l = mapL σ l
  | [], _ => rfl
  | a :: l, h => by
    show ρ a :: mapL ρ l = σ a :: mapL σ l
    rw [h a (Nat.lt_succ_of_le (Nat.le_add_right _ _)),
      mapL_bound l (fun n hn => h n (Nat.lt_of_lt_of_le hn (Nat.succ_le_succ (Nat.le_add_left _ _))))]

theorem up_bound {ρ σ : Nat → Nat} {B : Nat} (h : ∀ n, n < B → ρ n = σ n) : ∀ n, n < B + 1 → up ρ n = up σ n
  | 0, _ => rfl
  | n + 1, hn => congrArg (· + 1) (h n (Nat.lt_of_succ_lt_succ hn))

theorem ren_bound : (φ : Fm) → {ρ σ : Nat → Nat} → (∀ n, n < bound φ → ρ n = σ n) → ren ρ φ = ren σ φ
  | .atom r l, _, _, h => congrArg (Fm.atom r) (mapL_bound l h)
  | .bot, _, _, _ => rfl
  | .conj a b, _, _, h => by
    show Fm.conj _ _ = Fm.conj _ _
    rw [ren_bound a (fun n hn => h n (Nat.lt_of_lt_of_le hn (Nat.le_add_right _ _))),
        ren_bound b (fun n hn => h n (Nat.lt_of_lt_of_le hn (Nat.le_add_left _ _)))]
  | .disj a b, _, _, h => by
    show Fm.disj _ _ = Fm.disj _ _
    rw [ren_bound a (fun n hn => h n (Nat.lt_of_lt_of_le hn (Nat.le_add_right _ _))),
        ren_bound b (fun n hn => h n (Nat.lt_of_lt_of_le hn (Nat.le_add_left _ _)))]
  | .imp a b, _, _, h => by
    show Fm.imp _ _ = Fm.imp _ _
    rw [ren_bound a (fun n hn => h n (Nat.lt_of_lt_of_le hn (Nat.le_add_right _ _))),
        ren_bound b (fun n hn => h n (Nat.lt_of_lt_of_le hn (Nat.le_add_left _ _)))]
  | .all a, _, _, h => congrArg Fm.all (ren_bound a (fun n hn =>
      match n with
      | 0 => rfl
      | m + 1 => congrArg (· + 1) (h m (Nat.lt_of_succ_lt hn))))
  | .ex a, _, _, h => congrArg Fm.ex (ren_bound a (fun n hn =>
      match n with
      | 0 => rfl
      | m + 1 => congrArg (· + 1) (h m (Nat.lt_of_succ_lt hn))))

theorem ren_id (φ : Fm) : ren (fun n => n) φ = φ := by
  induction φ with
  | atom r l =>
    show Fm.atom r (mapL (fun n => n) l) = _
    have : ∀ l : List Nat, mapL (fun n => n) l = l := fun l => by
      induction l with
      | nil => rfl
      | cons a l ih => exact congrArg (List.cons a) ih
    rw [this]
  | bot => rfl
  | conj a b iha ihb => show Fm.conj _ _ = _; rw [iha, ihb]
  | disj a b iha ihb => show Fm.disj _ _ = _; rw [iha, ihb]
  | imp a b iha ihb => show Fm.imp _ _ = _; rw [iha, ihb]
  | all a ih =>
    show Fm.all (ren (up fun n => n) a) = _
    rw [ren_ext a (σ := fun n => n) (fun n => match n with | 0 => rfl | _ + 1 => rfl), ih]
  | ex a ih =>
    show Fm.ex (ren (up fun n => n) a) = _
    rw [ren_ext a (σ := fun n => n) (fun n => match n with | 0 => rfl | _ + 1 => rfl), ih]

def ltb : Nat → Nat → Bool
  | _, 0 => false
  | 0, _ + 1 => true
  | a + 1, b + 1 => ltb a b

theorem ltb_true : (a b : Nat) → a < b → ltb a b = true
  | _, 0, h => absurd h (Nat.not_lt_zero _)
  | 0, _ + 1, _ => rfl
  | a + 1, b + 1, h => ltb_true a b (Nat.lt_of_succ_lt_succ h)

theorem ltb_false : (a b : Nat) → ltb a b = false → b ≤ a
  | _, 0, _ => Nat.zero_le _
  | 0, _ + 1, h => Bool.noConfusion h
  | a + 1, b + 1, h => Nat.succ_le_succ (ltb_false a b h)

def eqb : Nat → Nat → Bool
  | 0, 0 => true
  | 0, _ + 1 => false
  | _ + 1, 0 => false
  | a + 1, b + 1 => eqb a b

theorem eqb_refl : (a : Nat) → eqb a a = true
  | 0 => rfl
  | a + 1 => eqb_refl a

theorem ltb_irrefl : (a : Nat) → ltb a a = false
  | 0 => rfl
  | a + 1 => ltb_irrefl a

/-- the renaming that sends N to 0 and shifts everything below N -/
def sig (N n : Nat) : Nat :=
  match ltb n N with
  | true => n + 1
  | false => match eqb n N with
    | true => 0
    | false => n

theorem sig_lt (N n : Nat) (h : n < N) : sig N n = n + 1 := by
  unfold sig
  rw [ltb_true n N h]

theorem sig_N (N : Nat) : sig N N = 0 := by
  unfold sig
  rw [ltb_irrefl, eqb_refl]

theorem sig_shift (N : Nat) (ψ : Fm) (h : bound ψ ≤ N) : ren (sig N) ψ = shift ψ :=
  ren_bound ψ (fun n hn => sig_lt N n (Nat.lt_of_lt_of_le hn h))

theorem sig_inst (N : Nat) (a : Fm) (h : bound a ≤ N + 1) : ren (sig N) (inst N a) = a := by
  unfold inst
  rw [ren_ren]
  refine (ren_bound a (σ := fun n => n) (fun n hn => ?_)).trans (ren_id a)
  match n with
  | 0 => exact sig_N N
  | m + 1 => exact sig_lt N m (Nat.lt_of_succ_lt_succ (Nat.lt_of_lt_of_le hn h))

/-- weakening -/
def Sub (Γ Δ : List Fm) : Prop := ∀ a, a ∈ Γ → a ∈ Δ

theorem mem_sh {a : Fm} : (Γ : List Fm) → a ∈ sh Γ → ∃ b, b ∈ Γ ∧ a = shift b
  | [], m => nomatch m
  | b :: Γ, m => by
    cases m with
    | head => exact ⟨b, .head _, rfl⟩
    | tail _ m' => exact match mem_sh Γ m' with | ⟨c, hc, e⟩ => ⟨c, .tail _ hc, e⟩

theorem sub_sh {Γ Δ : List Fm} (h : Sub Γ Δ) : Sub (sh Γ) (sh Δ) := fun _ m =>
  match mem_sh Γ m with
  | ⟨b, hb, e⟩ => e ▸ mem_mapF Nat.succ Δ (h b hb)

theorem sub_cons {Γ Δ : List Fm} (a : Fm) (h : Sub Γ Δ) : Sub (a :: Γ) (a :: Δ) := fun b m => by
  cases m with
  | head => exact .head _
  | tail _ m' => exact .tail _ (h b m')

theorem weaken {Γ : List Fm} {φ : Fm} (d : Pf Γ φ) : ∀ {Δ : List Fm}, Sub Γ Δ → Pf Δ φ := by
  induction d with
  | hyp m => exact fun h => .hyp (h _ m)
  | botE _ ih => exact fun h => .botE (ih h)
  | conjI _ _ iha ihb => exact fun h => .conjI (iha h) (ihb h)
  | conjL _ ih => exact fun h => .conjL (ih h)
  | conjR _ ih => exact fun h => .conjR (ih h)
  | disjL _ ih => exact fun h => .disjL (ih h)
  | disjR _ ih => exact fun h => .disjR (ih h)
  | disjE _ _ _ ih ih1 ih2 => exact fun h => .disjE (ih h) (ih1 (sub_cons _ h)) (ih2 (sub_cons _ h))
  | impI _ ih => exact fun h => .impI (ih (sub_cons _ h))
  | impE _ _ ih1 ih2 => exact fun h => .impE (ih1 h) (ih2 h)
  | allI _ ih => exact fun h => .allI (ih (sub_sh h))
  | allE y _ ih => exact fun h => .allE y (ih h)
  | exI y _ ih => exact fun h => .exI y (ih h)
  | exE _ _ ih1 ih2 => exact fun h => .exE (ih1 h) (ih2 (sub_cons _ (sub_sh h)))

/-- ∀ is the infimum of its instances (Lindenbaum, one hypothesis) -/
theorem all_inf (ψ a : Fm) (h : ∀ y, Pf [ψ] (inst y a)) : Pf [ψ] (.all a) := by
  have hN := pf_ren (h (bound ψ + bound a)) (sig (bound ψ + bound a))
  rw [sig_inst _ a (Nat.le_succ_of_le (Nat.le_add_left _ _))] at hN
  have e : mapF (sig (bound ψ + bound a)) [ψ] = sh [ψ] := by
    show [ren (sig (bound ψ + bound a)) ψ] = [shift ψ]
    rw [sig_shift _ ψ (Nat.le_add_right _ _)]
  rw [e] at hN
  exact .allI hN

/-- ∃ is the supremum of its instances -/
theorem ex_sup (ψ a : Fm) (h : ∀ y, Pf [inst y a] ψ) : Pf [.ex a] ψ := by
  have hN := pf_ren (h (bound ψ + bound a)) (sig (bound ψ + bound a))
  have e : mapF (sig (bound ψ + bound a)) [inst (bound ψ + bound a) a] = [a] := by
    show [ren (sig (bound ψ + bound a)) (inst (bound ψ + bound a) a)] = [a]
    rw [sig_inst _ a (Nat.le_succ_of_le (Nat.le_add_left _ _))]
  rw [e, sig_shift _ ψ (Nat.le_add_right _ _)] at hN
  exact .exE (.hyp (.head _)) (weaken hN (fun b m => by
    cases m with
    | head => exact .head _
    | tail _ m' => exact nomatch m'))

/-! ## Part 3: Heyting-valued structures, values, soundness -/

theorem distrib (H : HA.{0}) (c a b : H.car) :
    H.le (H.meet c (H.join a b)) (H.join (H.meet c a) (H.meet c b)) := by
  have ha : H.le a (H.imp c (H.join (H.meet c a) (H.meet c b))) :=
    H.imp_adj.mp (H.le_trans (H.meet_univ (H.meet_r _ _) (H.meet_l _ _)) (H.join_l _ _))
  have hb : H.le b (H.imp c (H.join (H.meet c a) (H.meet c b))) :=
    H.imp_adj.mp (H.le_trans (H.meet_univ (H.meet_r _ _) (H.meet_l _ _)) (H.join_r _ _))
  have hj := H.imp_adj.mpr (H.join_univ ha hb)
  exact H.le_trans (H.meet_univ (H.meet_r _ _) (H.meet_l _ _)) hj

structure Str where
  H : HA.{0}
  D : Type
  R : Nat → List D → H.car

def mapD {D : Type _} (v : Nat → D) : List Nat → List D
  | [] => []
  | a :: l => v a :: mapD v l

def ext {D : Type _} (v : Nat → D) (d : D) : Nat → D
  | 0 => d
  | n + 1 => v n

inductive Val (M : Str) : Fm → (Nat → M.D) → M.H.car → Prop where
  | atom {r l v} : Val M (.atom r l) v (M.R r (mapD v l))
  | bot {v} : Val M .bot v M.H.bot
  | conj {a b v x y} : Val M a v x → Val M b v y → Val M (.conj a b) v (M.H.meet x y)
  | disj {a b v x y} : Val M a v x → Val M b v y → Val M (.disj a b) v (M.H.join x y)
  | imp {a b v x y} : Val M a v x → Val M b v y → Val M (.imp a b) v (M.H.imp x y)
  | all {a v h} (f : M.D → M.H.car) : (∀ d, Val M a (ext v d) (f d)) → (∀ d, M.H.le h (f d)) →
      (∀ c, (∀ d, M.H.le c (f d)) → M.H.le c h) → Val M (.all a) v h
  | ex {a v h} (f : M.D → M.H.car) : (∀ d, Val M a (ext v d) (f d)) → (∀ d, M.H.le (f d) h) →
      (∀ c, (∀ d, M.H.le (f d) c) → M.H.le h c) → Val M (.ex a) v h

/-- every formula has a value under every valuation -/
def Safe (M : Str) : Prop := ∀ φ v, ∃ x, Val M φ v x

theorem mapD_ext {D : Type _} {v w : Nat → D} (h : ∀ n, v n = w n) : (l : List Nat) → mapD v l = mapD w l
  | [] => rfl
  | a :: l => by show v a :: mapD v l = w a :: mapD w l; rw [h a, mapD_ext h l]

theorem ext_ext {D : Type _} {v w : Nat → D} (h : ∀ n, v n = w n) (d : D) : ∀ n, ext v d n = ext w d n
  | 0 => rfl
  | n + 1 => h n

theorem val_ext {M : Str} {φ : Fm} {v : Nat → M.D} {x : M.H.car} (hv : Val M φ v x) :
    ∀ {w : Nat → M.D}, (∀ n, v n = w n) → Val M φ w x := by
  induction hv with
  | @atom r l v => exact fun h => by rw [mapD_ext h l]; exact .atom
  | bot => exact fun _ => .bot
  | conj _ _ ih1 ih2 => exact fun h => .conj (ih1 h) (ih2 h)
  | disj _ _ ih1 ih2 => exact fun h => .disj (ih1 h) (ih2 h)
  | imp _ _ ih1 ih2 => exact fun h => .imp (ih1 h) (ih2 h)
  | all f _ h1 h2 ih => exact fun h => .all f (fun d => ih d (ext_ext h d)) h1 h2
  | ex f _ h1 h2 ih => exact fun h => .ex f (fun d => ih d (ext_ext h d)) h1 h2

theorem mapD_mapL {D : Type _} (v : Nat → D) (ρ : Nat → Nat) : (l : List Nat) →
    mapD v (mapL ρ l) = mapD (fun n => v (ρ n)) l
  | [] => rfl
  | a :: l => congrArg (List.cons (v (ρ a))) (mapD_mapL v ρ l)

theorem ext_up {D : Type _} (v : Nat → D) (ρ : Nat → Nat) (d : D) :
    ∀ n, ext v d (up ρ n) = ext (fun m => v (ρ m)) d n
  | 0 => rfl
  | _ + 1 => rfl

theorem val_ren_mp (M : Str) : (φ : Fm) → (ρ : Nat → Nat) → (v : Nat → M.D) → (x : M.H.car) →
    Val M (ren ρ φ) v x → Val M φ (fun n => v (ρ n)) x
  | .atom r l, ρ, v, _, h => by
    cases h
    rw [mapD_mapL]
    exact .atom
  | .bot, _, _, _, h => by cases h; exact .bot
  | .conj a b, ρ, v, _, h => by
    cases h with
    | conj h1 h2 => exact .conj (val_ren_mp M a ρ v _ h1) (val_ren_mp M b ρ v _ h2)
  | .disj a b, ρ, v, _, h => by
    cases h with
    | disj h1 h2 => exact .disj (val_ren_mp M a ρ v _ h1) (val_ren_mp M b ρ v _ h2)
  | .imp a b, ρ, v, _, h => by
    cases h with
    | imp h1 h2 => exact .imp (val_ren_mp M a ρ v _ h1) (val_ren_mp M b ρ v _ h2)
  | .all a, ρ, v, _, h => by
    cases h with
    | all f hf h1 h2 =>
      exact .all f (fun d => val_ext (val_ren_mp M a (up ρ) (ext v d) _ (hf d)) (ext_up v ρ d)) h1 h2
  | .ex a, ρ, v, _, h => by
    cases h with
    | ex f hf h1 h2 =>
      exact .ex f (fun d => val_ext (val_ren_mp M a (up ρ) (ext v d) _ (hf d)) (ext_up v ρ d)) h1 h2

theorem val_ren_mpr (M : Str) : (φ : Fm) → (ρ : Nat → Nat) → (v : Nat → M.D) → (x : M.H.car) →
    Val M φ (fun n => v (ρ n)) x → Val M (ren ρ φ) v x
  | .atom r l, ρ, v, _, h => by
    cases h
    show Val M (.atom r (mapL ρ l)) v _
    rw [← mapD_mapL]
    exact .atom
  | .bot, _, _, _, h => by cases h; exact .bot
  | .conj a b, ρ, v, _, h => by
    cases h with
    | conj h1 h2 => exact .conj (val_ren_mpr M a ρ v _ h1) (val_ren_mpr M b ρ v _ h2)
  | .disj a b, ρ, v, _, h => by
    cases h with
    | disj h1 h2 => exact .disj (val_ren_mpr M a ρ v _ h1) (val_ren_mpr M b ρ v _ h2)
  | .imp a b, ρ, v, _, h => by
    cases h with
    | imp h1 h2 => exact .imp (val_ren_mpr M a ρ v _ h1) (val_ren_mpr M b ρ v _ h2)
  | .all a, ρ, v, _, h => by
    cases h with
    | all f hf h1 h2 =>
      exact .all f (fun d => val_ren_mpr M a (up ρ) (ext v d) _
        (val_ext (hf d) (fun n => (ext_up v ρ d n).symm))) h1 h2
  | .ex a, ρ, v, _, h => by
    cases h with
    | ex f hf h1 h2 =>
      exact .ex f (fun d => val_ren_mpr M a (up ρ) (ext v d) _
        (val_ext (hf d) (fun n => (ext_up v ρ d n).symm))) h1 h2

/-- values are unique up to ≤ both ways -/
theorem val_le {M : Str} : (φ : Fm) → {v : Nat → M.D} → {x y : M.H.car} →
    Val M φ v x → Val M φ v y → M.H.le x y
  | .atom _ _, _, _, _, hx, hy => by cases hx; cases hy; exact M.H.le_refl _
  | .bot, _, _, _, hx, hy => by cases hx; cases hy; exact M.H.le_refl _
  | .conj a b, _, _, _, hx, hy => by
    cases hx with
    | conj x1 x2 => cases hy with
      | conj y1 y2 => exact (M.H.meet_univ (M.H.le_trans (M.H.meet_l _ _) (val_le a x1 y1)) (M.H.le_trans (M.H.meet_r _ _) (val_le b x2 y2)))
  | .disj a b, _, _, _, hx, hy => by
    cases hx with
    | disj x1 x2 => cases hy with
      | disj y1 y2 => exact (M.H.join_univ (M.H.le_trans (val_le a x1 y1) (M.H.join_l _ _)) (M.H.le_trans (val_le b x2 y2) (M.H.join_r _ _)))
  | .imp a b, _, _, _, hx, hy => by
    cases hx with
    | imp x1 x2 => cases hy with
      | imp y1 y2 =>
        exact (M.H.imp_adj.mp (M.H.le_trans (M.H.le_trans (M.H.meet_univ (M.H.meet_l _ _) (M.H.le_trans (M.H.meet_r _ _) (val_le a y1 x1))) (HA.mp M.H _ _)) (val_le b x2 y2)))
  | .all a, _, _, _, hx, hy => by
    cases hx with
    | all f hf h1 _ => cases hy with
      | all g hg _ k2 => exact k2 _ (fun d => M.H.le_trans (h1 d) (val_le a (hf d) (hg d)))
  | .ex a, _, _, _, hx, hy => by
    cases hx with
    | ex f hf _ h2 => cases hy with
      | ex g hg k1 _ => exact h2 _ (fun d => M.H.le_trans (val_le a (hf d) (hg d)) (k1 d))

theorem val_inst {M : Str} (y : Nat) (a : Fm) (v : Nat → M.D) (z : M.H.car) :
    Val M (inst y a) v z ↔ Val M a (ext v (v y)) z :=
  ⟨fun h => val_ext (val_ren_mp M a (cons0 y) v z h) (fun n => match n with | 0 => rfl | _ + 1 => rfl),
   fun h => val_ren_mpr M a (cons0 y) v z (val_ext h (fun n => match n with | 0 => rfl | _ + 1 => rfl))⟩

theorem val_shift {M : Str} (c : Fm) (v : Nat → M.D) (d : M.D) (z : M.H.car) :
    Val M (shift c) (ext v d) z ↔ Val M c v z :=
  ⟨fun h => val_ext (val_ren_mp M c Nat.succ (ext v d) z h) (fun _ => rfl),
   fun h => val_ren_mpr M c Nat.succ (ext v d) z (val_ext h (fun _ => rfl))⟩

/-- soundness: derivable ⟹ in every safe structure, any lower bound of the
  hypotheses' values is below the conclusion's value -/
theorem sound {Γ : List Fm} {φ : Fm} (d : Pf Γ φ) (M : Str) (hM : Safe M) :
    ∀ (v : Nat → M.D) (c : M.H.car), (∀ γ x, γ ∈ Γ → Val M γ v x → M.H.le c x) →
      ∀ y, Val M φ v y → M.H.le c y := by
  induction d with
  | hyp m =>
    intro v c hc y hy
    exact hc _ y m hy
  | botE _ ih => exact fun v c hc y _ => M.H.le_trans (ih v c hc _ .bot) (M.H.bot_le y)
  | conjI _ _ ih1 ih2 =>
    intro v c hc y hy
    cases hy with
    | conj h1 h2 => exact M.H.meet_univ (ih1 v c hc _ h1) (ih2 v c hc _ h2)
  | @conjL Γ a b _ ih =>
    intro v c hc y hy
    exact
      match hM b v with
      | ⟨xb, hb⟩ => M.H.le_trans (ih v c hc _ (.conj hy hb)) (M.H.meet_l _ _)
  | @conjR Γ a b _ ih =>
    intro v c hc y hy
    exact
      match hM a v with
      | ⟨xa, ha⟩ => M.H.le_trans (ih v c hc _ (.conj ha hy)) (M.H.meet_r _ _)
  | @disjL Γ a b _ ih =>
    intro v c hc y hy
    cases hy with
    | disj h1 _ => exact M.H.le_trans (ih v c hc _ h1) (M.H.join_l _ _)
  | @disjR Γ a b _ ih =>
    intro v c hc y hy
    cases hy with
    | disj _ h2 => exact M.H.le_trans (ih v c hc _ h2) (M.H.join_r _ _)
  | @disjE Γ a b e _ _ _ ih ih1 ih2 =>
    intro v c hc y hy
    exact
      match hM a v, hM b v with
      | ⟨xa, ha⟩, ⟨xb, hb⟩ =>
        have k := ih v c hc _ (.disj ha hb)
        have k1 : M.H.le (M.H.meet c xa) y := ih1 v _ (fun γ x m hx => by
          cases m with
          | head => exact M.H.le_trans (M.H.meet_r _ _) (val_le a ha hx)
          | tail _ m' => exact M.H.le_trans (M.H.meet_l _ _) (hc γ x m' hx)) y hy
        have k2 : M.H.le (M.H.meet c xb) y := ih2 v _ (fun γ x m hx => by
          cases m with
          | head => exact M.H.le_trans (M.H.meet_r _ _) (val_le b hb hx)
          | tail _ m' => exact M.H.le_trans (M.H.meet_l _ _) (hc γ x m' hx)) y hy
        M.H.le_trans (M.H.meet_univ (M.H.le_refl c) k)
          (M.H.le_trans (distrib M.H c xa xb) (M.H.join_univ k1 k2))
  | @impI Γ a b _ ih =>
    intro v c hc y hy
    cases hy with
    | imp ha hb =>
      exact M.H.imp_adj.mp (ih v _ (fun γ x m hx => by
        cases m with
        | head => exact M.H.le_trans (M.H.meet_r _ _) (val_le a ha hx)
        | tail _ m' => exact M.H.le_trans (M.H.meet_l _ _) (hc γ x m' hx)) _ hb)
  | @impE Γ a b _ _ ih1 ih2 =>
    intro v c hc y hy
    exact
      match hM a v with
      | ⟨xa, ha⟩ => HA.imp_elim M.H (ih1 v c hc _ (.imp ha hy)) (ih2 v c hc _ ha)
  | @allI Γ a _ ih =>
    intro v c hc y hy
    cases hy with
    | all f hf _ h2 =>
      exact h2 c (fun d => ih (ext v d) c (fun γ x m hx =>
        match mem_sh Γ m with
        | ⟨g, hg, e⟩ => hc g x hg ((val_shift g v d x).mp (e ▸ hx))) _ (hf d))
  | @allE Γ a t _ ih =>
    intro v c hc y hy
    exact
      match hM (.all a) v with
      | ⟨h, hh⟩ => by
        cases hh with
        | all f hf h1 _ =>
          exact M.H.le_trans (ih v c hc _ (.all f hf h1 (by assumption)))
            (M.H.le_trans (h1 (v t)) (val_le a (hf (v t)) ((val_inst t a v y).mp hy)))
  | @exI Γ a t _ ih =>
    intro v c hc y hy
    cases hy with
    | ex f hf h1 _ =>
      exact match hM (inst t a) v with
      | ⟨z, hz⟩ => M.H.le_trans (ih v c hc _ hz)
          (M.H.le_trans (val_le a ((val_inst t a v z).mp hz) (hf (v t))) (h1 (v t)))
  | @exE Γ a e _ _ ih1 ih2 =>
    intro v c hc y hy
    exact
      match hM (.ex a) v with
      | ⟨h, hh⟩ => by
        cases hh with
        | ex f hf h1 h2 =>
          have k1 := ih1 v c hc _ (.ex f hf h1 h2)
          have k2 : M.H.le h (M.H.imp c y) := h2 _ (fun d => M.H.imp_adj.mp (ih2 (ext v d) _
            (fun γ x m hx => by
              cases m with
              | head => exact M.H.le_trans (M.H.meet_l _ _) (val_le a (hf d) hx)
              | tail _ m' =>
                exact match mem_sh Γ m' with
                | ⟨g, hg, e⟩ => M.H.le_trans (M.H.meet_r _ _) (hc g x hg ((val_shift g v d x).mp (e ▸ hx))))
            _ ((val_shift e v d y).mpr hy)))
          exact HA.imp_elim M.H (M.H.le_trans k1 k2) (M.H.le_refl c)

/-! ## Part 4: the Lindenbaum structure (domain ℕ), completeness -/

theorem cut {Γ : List Fm} {x y : Fm} (hx : Pf Γ x) (hy : Pf [x] y) : Pf Γ y :=
  .impE (weaken (Pf.impI hy) (fun _ m => nomatch m)) hx

theorem sub_one {a : Fm} {Γ : List Fm} (h : a ∈ Γ) : Sub [a] Γ := fun _ m => by
  cases m with
  | head => exact h
  | tail _ m' => exact nomatch m'

def LH : HA.{0} where
  car := Fm
  le a b := Pf [a] b
  le_refl _ := .hyp (.head _)
  le_trans h1 h2 := cut h1 h2
  top := .imp .bot .bot
  bot := .bot
  meet := .conj
  join := .disj
  imp := .imp
  le_top _ := .impI (.hyp (.head _))
  bot_le _ := .botE (.hyp (.head _))
  meet_l _ _ := .conjL (.hyp (.head _))
  meet_r _ _ := .conjR (.hyp (.head _))
  meet_univ h1 h2 := .conjI h1 h2
  join_l _ _ := .disjL (.hyp (.head _))
  join_r _ _ := .disjR (.hyp (.head _))
  join_univ h1 h2 := .disjE (.hyp (.head _)) (weaken h1 (sub_one (.head _))) (weaken h2 (sub_one (.head _)))
  imp_adj := ⟨fun h => .impI (cut (.conjI (.hyp (.tail _ (.head _))) (.hyp (.head _))) h),
    fun h => .impE (cut (.conjL (.hyp (.head _))) h) (.conjR (.hyp (.head _)))⟩

def LS : Str := ⟨LH, Nat, fun r l => .atom r l⟩

theorem mapD_mapL' (v : Nat → Nat) : (l : List Nat) → mapD v l = mapL v l
  | [] => rfl
  | a :: l => congrArg (List.cons (v a)) (mapD_mapL' v l)

theorem inst_ext (v : Nat → Nat) (d : Nat) (a : Fm) : ren (ext v d) a = inst d (ren (up v) a) := by
  unfold inst
  rw [ren_ren]
  exact ren_ext a (fun n => match n with | 0 => rfl | _ + 1 => rfl)

/-- in the Lindenbaum structure every formula has its own (renamed) value -/
theorem val_lind : (φ : Fm) → (v : Nat → Nat) → Val LS φ v (ren v φ)
  | .atom r l, v => by
    show Val LS (.atom r l) v (Fm.atom r (mapL v l))
    rw [← mapD_mapL']
    exact .atom
  | .bot, _ => .bot
  | .conj a b, v => .conj (val_lind a v) (val_lind b v)
  | .disj a b, v => .disj (val_lind a v) (val_lind b v)
  | .imp a b, v => .imp (val_lind a v) (val_lind b v)
  | .all a, v => .all (fun d => ren (ext v d) a) (fun d => val_lind a (ext v d))
      (fun (d : Nat) => by
        show Pf [Fm.all (ren (up v) a)] (ren (ext (D := Nat) v d) a)
        rw [inst_ext]
        exact .allE d (.hyp (.head _)))
      (fun c hc => all_inf c (ren (up v) a) (fun y => by
        have := hc y
        rw [inst_ext] at this
        exact this))
  | .ex a, v => .ex (fun d => ren (ext v d) a) (fun d => val_lind a (ext v d))
      (fun (d : Nat) => by
        show Pf [ren (ext (D := Nat) v d) a] (Fm.ex (ren (up v) a))
        rw [inst_ext]
        exact .exI d (.hyp (.head _)))
      (fun c hc => ex_sup c (ren (up v) a) (fun y => by
        have := hc y
        rw [inst_ext] at this
        exact this))

theorem lind_safe : Safe LS := fun φ v => ⟨ren v φ, val_lind φ v⟩

def Valid (Γ : List Fm) (φ : Fm) : Prop :=
  ∀ M : Str, Safe M → ∀ (v : Nat → M.D) (c : M.H.car),
    (∀ γ x, γ ∈ Γ → Val M γ v x → M.H.le c x) → ∀ y, Val M φ v y → M.H.le c y

def bigConj : List Fm → Fm
  | [] => .imp .bot .bot
  | a :: Γ => .conj a (bigConj Γ)

theorem big_mem {γ : Fm} : (Γ : List Fm) → γ ∈ Γ → Pf [bigConj Γ] γ
  | [], m => nomatch m
  | _ :: Γ, m => by
    cases m with
    | head => exact .conjL (.hyp (.head _))
    | tail _ m' => exact cut (.conjR (.hyp (.head _))) (big_mem Γ m')

theorem big_intro : (Γ : List Fm) → Pf Γ (bigConj Γ)
  | [] => .impI (.hyp (.head _))
  | _ :: Γ => .conjI (.hyp (.head _)) (weaken (big_intro Γ) (fun _ m => .tail _ m))

theorem soundness {Γ : List Fm} {φ : Fm} (d : Pf Γ φ) : Valid Γ φ := fun M hM => sound d M hM

/-- completeness: valid in every safe Heyting-valued structure ⟹ derivable -/
theorem completeness {Γ : List Fm} {φ : Fm} (h : Valid Γ φ) : Pf Γ φ := by
  have key : Pf [bigConj Γ] (ren (fun n => n) φ) :=
    h LS lind_safe (fun n => n) (bigConj Γ) (fun γ x m hx =>
      cut (big_mem Γ m) (by
        have e := val_le (M := LS) γ (val_lind γ (fun n => n)) hx
        rw [ren_id] at e
        exact e)) _ (val_lind φ (fun n => n))
  rw [ren_id] at key
  exact cut (big_intro Γ) key

theorem complete_iff (Γ : List Fm) (φ : Fm) : Pf Γ φ ↔ Valid Γ φ := ⟨soundness, completeness⟩

end FO

end Found

#print axioms Found.FO.ren_ren
#print axioms Found.FO.ren_inst
#print axioms Found.FO.pf_ren
#print axioms Found.FO.weaken
#print axioms Found.FO.all_inf
#print axioms Found.FO.ex_sup
#print axioms Found.FO.val_le
#print axioms Found.FO.sound
#print axioms Found.FO.val_lind
#print axioms Found.FO.completeness
#print axioms Found.FO.complete_iff

found/Fagin.lean

lean_all/found/Fagin.lean

/-
  T185. found/Fagin — descriptive complexity as a point (Fagin: NP =
  existential second-order logic on finite structures), certificate side:
  * An ESO property: "there is a colouring c with no monochromatic edge"
    (2-colourability) — the second-order ∃ guesses c, the first-order part
    is a check over the finite edge list.
  * The check is a Boolean function and is correct: check c E = true ⟺ c
    is proper on E — the polynomial-time verifier of the NP side.
  The converse (every NP property is ESO) is cited.

  Lean 4 core only, hand-written proofs, no axioms.
-/

namespace Found

namespace Fagin

def neq : Bool → Bool → Bool
  | true, true => false
  | true, false => true
  | false, true => true
  | false, false => false

theorem neq_true : (a b : Bool) → (neq a b = true ↔ a ≠ b)
  | true, true => ⟨fun h => Bool.noConfusion h, fun h => (h rfl).elim⟩
  | true, false => ⟨fun _ e => Bool.noConfusion e, fun _ => rfl⟩
  | false, true => ⟨fun _ e => Bool.noConfusion e, fun _ => rfl⟩
  | false, false => ⟨fun h => Bool.noConfusion h, fun h => (h rfl).elim⟩

def check (c : Nat → Bool) : List (Nat × Nat) → Bool
  | [] => true
  | (u, v) :: E => neq (c u) (c v) && check c E

def Proper (c : Nat → Bool) (E : List (Nat × Nat)) : Prop := ∀ e, e ∈ E → c e.1 ≠ c e.2

theorem check_correct (c : Nat → Bool) : (E : List (Nat × Nat)) → (check c E = true ↔ Proper c E)
  | [] => ⟨(fun _ _ m => nomatch m), (fun _ => rfl)⟩
  | (u, v) :: E => by
    show (neq (c u) (c v) && check c E) = true ↔ _
    cases h1 : neq (c u) (c v) with
    | false =>
      exact ⟨fun h => Bool.noConfusion h, fun hp => by
        have := (neq_true _ _).mpr (hp (u, v) (.head _))
        rw [h1] at this
        exact Bool.noConfusion this⟩
    | true =>
      cases h2 : check c E with
      | false =>
        exact ⟨fun h => Bool.noConfusion h, fun hp => by
          have := (check_correct c E).mpr (fun e m => hp e (.tail _ m))
          rw [h2] at this
          exact Bool.noConfusion this⟩
      | true =>
        refine ⟨fun _ e m => ?_, fun _ => rfl⟩
        cases m with
        | head => exact (neq_true _ _).mp h1
        | tail _ m' => exact (check_correct c E).mp h2 e m'

/-- the ESO property: some colouring is proper -/
def TwoColourable (E : List (Nat × Nat)) : Prop := ∃ c : Nat → Bool, check c E = true

theorem eso_iff (E : List (Nat × Nat)) : TwoColourable E ↔ ∃ c, Proper c E :=
  ⟨fun ⟨c, h⟩ => ⟨c, (check_correct c E).mp h⟩, fun ⟨c, h⟩ => ⟨c, (check_correct c E).mpr h⟩⟩

end Fagin

end Found

#print axioms Found.Fagin.neq_true
#print axioms Found.Fagin.check_correct
#print axioms Found.Fagin.eso_iff

found/Fan.lean

lean_all/found/Fan.lean

/-
  T197 line. found/Fan — the fan theorem, in the classical point
  (found/ClassicalPoint), by König's lemma: a bar on the binary fan
  (every infinite binary sequence meets it at some depth, and membership
  at depth n depends only on the first n bits) is uniform: one depth bounds
  every sequence.  Constructively (Brouwer) this is an axiom of the
  intuitionistic point (T197); here it is a theorem of the classical point.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.ClassicalPoint

namespace Found

namespace Fan

open CP

variable (K : Classical') (B : (Nat → Bool) → Nat → Prop)
  (loc : ∀ α β n, (∀ i, i < n → α i = β i) → B α n → B β n)

def Agree (α β : Nat → Bool) (k : Nat) : Prop := ∀ i, i < k → α i = β i

/-- s is good at depth k: its first k bits extend arbitrarily far avoiding the bar -/
def Good (s : Nat → Bool) (k : Nat) : Prop :=
  ∀ N, ∃ β, Agree s β k ∧ ∀ n, n ≤ k + N → ¬ B β n

def upd (s : Nat → Bool) (k : Nat) (c : Bool) : Nat → Bool := fun i =>
  match Nat.decEq i k with
  | .isTrue _ => c
  | .isFalse _ => s i

theorem upd_lt (s : Nat → Bool) (k : Nat) (c : Bool) (i : Nat) (h : i < k) : upd s k c i = s i := by
  unfold upd
  cases Nat.decEq i k with
  | isTrue e => exact absurd (e ▸ h) (Nat.lt_irrefl k)
  | isFalse _ => rfl

theorem upd_eq (s : Nat → Bool) (k : Nat) (c : Bool) : upd s k c k = c := by
  unfold upd
  cases Nat.decEq k k with
  | isTrue _ => rfl
  | isFalse h => exact absurd rfl h

include K in
/-- one of the two children of a good node is good -/
theorem child (s : Nat → Bool) (k : Nat) (hs : Good B s k) : ∃ c, Good B (upd s k c) (k + 1) := by
  refine dne K _ (fun hno => ?_)
  have nb : ∀ c, ¬ Good B (upd s k c) (k + 1) := fun c h => hno ⟨c, h⟩
  have bad : ∀ c, ∃ N, ∀ β, Agree (upd s k c) β (k + 1) → ∃ n, n ≤ k + 1 + N ∧ B β n := fun c =>
    dne K _ (fun hn => nb c (fun N => dne K _ (fun hno2 =>
      hn ⟨N, fun β hβ => dne K _ (fun hno3 => hno2 ⟨β, hβ, fun n hn' hb => hno3 ⟨n, hn', hb⟩⟩)⟩)))
  match bad false, bad true with
  | ⟨N0, h0⟩, ⟨N1, h1⟩ =>
    match hs (N0 + N1 + 1) with
    | ⟨β, hβ, hav⟩ =>
      have hext : Agree (upd s k (β k)) β (k + 1) := fun i hi => by
        cases Nat.lt_or_ge i k with
        | inl hl => rw [upd_lt s k _ i hl]; exact hβ i hl
        | inr hg =>
          have e : i = k := Nat.le_antisymm (Nat.le_of_lt_succ hi) hg
          rw [e, upd_eq]
      have hk : ∀ c, β k = c → ∃ n, n ≤ k + 1 + (N0 + N1) ∧ B β n := fun c e => by
        cases c with
        | false =>
          match h0 β (e ▸ hext) with
          | ⟨n, hn, hb⟩ => exact ⟨n, Nat.le_trans hn (Nat.add_le_add_left (Nat.le_add_right _ _) _), hb⟩
        | true =>
          match h1 β (e ▸ hext) with
          | ⟨n, hn, hb⟩ => exact ⟨n, Nat.le_trans hn (Nat.add_le_add_left (Nat.le_add_left _ _) _), hb⟩
      match hk (β k) rfl with
      | ⟨n, hn, hb⟩ => exact hav n (by
          have : k + 1 + (N0 + N1) = k + (N0 + N1 + 1) := by
            rw [Nat.add_right_comm k 1, Nat.add_assoc k (N0 + N1) 1]
          rw [← this]; exact hn) hb

/-- the branch through good nodes (König's lemma), by recursion with the
  goodness invariant carried along -/
def approx (h0 : Good B (fun _ => false) 0) : (k : Nat) → {s : Nat → Bool // Good B s k}
  | 0 => ⟨fun _ => false, h0⟩
  | k + 1 =>
    let p := approx h0 k
    ⟨upd p.1 k (K.ch (fun c => Good B (upd p.1 k c) (k + 1)) (child K B p.1 k p.2)),
     K.ch_spec (fun c => Good B (upd p.1 k c) (k + 1)) (child K B p.1 k p.2)⟩

def branch (h0 : Good B (fun _ => false) 0) (i : Nat) : Bool := (approx K B h0 (i + 1)).1 i

theorem approx_stable (h0 : Good B (fun _ => false) 0) :
    ∀ k i, i < k → (approx K B h0 k).1 i = branch K B h0 i
  | 0, _, h => absurd h (Nat.not_lt_zero _)
  | k + 1, i, h => by
    cases Nat.lt_or_ge i k with
    | inl hl =>
      show upd (approx K B h0 k).1 k _ i = _
      rw [upd_lt _ k _ i hl]
      exact approx_stable h0 k i hl
    | inr hg =>
      have e : i = k := Nat.le_antisymm (Nat.le_of_lt_succ h) hg
      subst e
      rfl

include loc in
/-- the branch never meets the bar -/
theorem branch_avoids (h0 : Good B (fun _ => false) 0) (n : Nat) : ¬ B (branch K B h0) n := fun hb =>
  match (approx K B h0 n).2 0 with
  | ⟨β, hβ, hav⟩ => hav n (Nat.le_refl _) (loc _ β n (fun i hi => by
      rw [← approx_stable K B h0 n i hi]; exact hβ i hi) hb)

include K loc in
/-- the fan theorem: a bar on the binary fan is uniform -/
theorem fan (bar : ∀ α, ∃ n, B α n) : ∃ N, ∀ α, ∃ n, n ≤ N ∧ B α n := by
  refine dne K _ (fun hno => ?_)
  have h0 : Good B (fun _ => false) 0 := fun N => dne K _ (fun hn => hno ⟨N, fun α => dne K _ (fun hα =>
    hn ⟨α, fun _ hi => absurd hi (Nat.not_lt_zero _), fun n hle hb => hα ⟨n, by rw [Nat.zero_add] at hle; exact hle, hb⟩⟩)⟩)
  match bar (branch K B h0) with
  | ⟨n, hb⟩ => exact branch_avoids K B loc h0 n hb

end Fan

end Found

#print axioms Found.Fan.child
#print axioms Found.Fan.branch_avoids
#print axioms Found.Fan.fan

found/Fg1.lean

lean_all/found/Fg1.lean

/-
  T185 (Fagin: NP = ESO), part 1. found/Fg1 — finite relational
  structures with universe {0, …, n-1} and relations given as Boolean
  functions on argument lists; first-order formulas (de Bruijn variables)
  and their satisfaction; existential second-order sentences (expansions by
  new relations); isomorphisms and isomorphism-closed classes.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt88

namespace Found

namespace Fg

open Gt

/-- a finite structure with m relation symbols -/
structure FStr where
  n : Nat
  rel : Nat → List Nat → Bool

/-- first-order formulas over relation symbols -/
inductive FFm : Type where
  | rel (i : Nat) (args : List Nat) : FFm
  | eq (a b : Nat) : FFm
  | neg : FFm → FFm
  | conj : FFm → FFm → FFm
  | disj : FFm → FFm → FFm
  | ex : FFm → FFm
  | all : FFm → FFm

def consV (v : Nat → Nat) (x : Nat) : Nat → Nat
  | 0 => x
  | k + 1 => v k

/-- satisfaction -/
def Sat (A : FStr) : (Nat → Nat) → FFm → Prop
  | v, .rel i args => A.rel i (lmapG v args) = true
  | v, .eq a b => v a = v b
  | v, .neg φ => ¬ Sat A v φ
  | v, .conj φ ψ => Sat A v φ ∧ Sat A v ψ
  | v, .disj φ ψ => Sat A v φ ∨ Sat A v ψ
  | v, .ex φ => ∃ x, x < A.n ∧ Sat A (consV v x) φ
  | v, .all φ => ∀ x, x < A.n → Sat A (consV v x) φ

/-- expansion of A (with m relations) by new relations S (indices ≥ m) -/
def expand (A : FStr) (m : Nat) (S : Nat → List Nat → Bool) : FStr where
  n := A.n
  rel i := match Nat.decLt i m with
    | isTrue _ => A.rel i
    | isFalse _ => S (subN i m)

/-- an existential second-order sentence ∃S̄ φ over m base relations (φ closed) -/
def ESO (m : Nat) (φ : FFm) (A : FStr) : Prop :=
  ∃ S : Nat → List Nat → Bool, Sat (expand A m S) (fun _ => 0) φ

/-- an isomorphism of finite structures on the first m relations (arities ar) -/
structure FIsoS (m : Nat) (A B : FStr) where
  f : Nat → Nat
  g : Nat → Nat
  f_lt : ∀ x, x < A.n → f x < B.n
  g_lt : ∀ y, y < B.n → g y < A.n
  gf : ∀ x, x < A.n → g (f x) = x
  fg : ∀ y, y < B.n → f (g y) = y
  rel_pres : ∀ i, i < m → ∀ args, AllLtG A.n args → B.rel i (lmapG f args) = A.rel i args

def IsoClosed (m : Nat) (Kl : FStr → Prop) : Prop := ∀ A B, FIsoS m A B → Kl A → Kl B

end Fg

end Found

#print axioms Found.Fg.Sat
#print axioms Found.Fg.ESO

found/Fg10.lean

lean_all/found/Fg10.lean

/-
  T185 (Fagin: NP = ESO), part 10. found/Fg10 — reading the encoding of a
  structure by position: the unary block, the separators at the block
  offsets, the bit of R_i at the r-th tuple, and blanks after the end.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Fg9

namespace Found

namespace Fg

open Gt

theorem len_appG {X : Type} : (l l' : List X) → lenG (appG l l') = lenG l + lenG l'
  | [], l' => (Nat.zero_add _).symm
  | _ :: l, l' => by
    show lenG (appG l l') + 1 = lenG l + 1 + lenG l'
    rw [len_appG l l', Nat.add_right_comm]

theorem wAt_app_lt : (l l' : List Nat) → ∀ p, p < lenG l → wAt (appG l l') p = wAt l p
  | [], _, p, h => absurd h (Nat.not_lt_zero p)
  | _ :: _, _, 0, _ => rfl
  | _ :: l, l', p + 1, h => wAt_app_lt l l' p (Nat.lt_of_succ_lt_succ h)

theorem wAt_app_ge : (l l' : List Nat) → ∀ j, wAt (appG l l') (j + lenG l) = wAt l' j
  | [], _, j => rfl
  | _ :: l, l', j => by
    show wAt (appG l l') (j + lenG l) = wAt l' j
    exact wAt_app_ge l l' j

theorem wAt_repl (x : Nat) : (d : Nat) → ∀ p, p < d → wAt (replG x d) p = x
  | 0, p, h => absurd h (Nat.not_lt_zero p)
  | _ + 1, 0, _ => rfl
  | d + 1, p + 1, h => wAt_repl x d p (Nat.lt_of_succ_lt_succ h)

theorem wAt_beyond : (l : List Nat) → ∀ p, lenG l ≤ p → wAt l p = 0
  | [], _, _ => rfl
  | _ :: _, 0, h => absurd h (Nat.not_succ_le_zero _)
  | _ :: l, p + 1, h => wAt_beyond l p (Nat.le_of_succ_le_succ h)

theorem len_upFrom : (k N : Nat) → lenG (upFrom k N) = N
  | _, 0 => rfl
  | k, N + 1 => congrArg (· + 1) (len_upFrom (k + 1) N)

theorem wAt_map_up (f : Nat → Nat) : (k N : Nat) → ∀ j, j < N → wAt (lmapG f (upFrom k N)) j = f (k + j)
  | _, 0, j, h => absurd h (Nat.not_lt_zero j)
  | k, N + 1, 0, _ => by rw [Nat.add_zero]; rfl
  | k, N + 1, j + 1, h => by
    show wAt (lmapG f (upFrom (k + 1) N)) j = f (k + (j + 1))
    rw [wAt_map_up f (k + 1) N j (Nat.lt_of_succ_lt_succ h), Nat.add_assoc, Nat.add_comm 1 j]

theorem relBits_eq (A : FStr) (i a : Nat) :
    relBits A i a = lmapG (fun j => bitOf (A.rel i (iterL A.n a j))) (upFrom 0 (A.n ^ a)) :=
  lmap_lmap _ _ _

theorem len_relBits (A : FStr) (i a : Nat) : lenG (relBits A i a) = A.n ^ a := by
  rw [relBits_eq, len_lmapG, len_upFrom]

theorem wAt_relBits (A : FStr) (i a r : Nat) (hr : r < A.n ^ a) :
    wAt (relBits A i a) r = bitOf (A.rel i (iterL A.n a r)) := by
  rw [relBits_eq, wAt_map_up _ 0 _ r hr, Nat.zero_add]

/-- the block offsets of the encoding -/
def offE (n : Nat) (ar : Nat → Nat) : Nat → Nat
  | 0 => n
  | i + 1 => offE n ar i + 1 + n ^ ar i

theorem len_relsEnc (ar : Nat → Nat) (A : FStr) : (i : Nat) → lenG (relsEnc ar A i) + A.n = offE A.n ar i
  | 0 => Nat.zero_add _
  | i + 1 => by
    show lenG (appG (relsEnc ar A i) (4 :: relBits A i (ar i))) + A.n = offE A.n ar i + 1 + A.n ^ ar i
    rw [len_appG, ← len_relsEnc ar A i]
    show lenG (relsEnc ar A i) + (lenG (relBits A i (ar i)) + 1) + A.n = lenG (relsEnc ar A i) + A.n + 1 + A.n ^ ar i
    rw [len_relBits, Nat.add_right_comm _ _ A.n, Nat.add_assoc (lenG (relsEnc ar A i) + A.n), Nat.add_comm (A.n ^ ar i) 1]

theorem len_relsEnc_mono (ar : Nat → Nat) (A : FStr) (i : Nat) : (M : Nat) → i ≤ M →
    lenG (relsEnc ar A i) ≤ lenG (relsEnc ar A M)
  | 0, h => by rw [Nat.le_antisymm h (Nat.zero_le _)]; exact Nat.le_refl _
  | M + 1, h => match Nat.decEq i (M + 1) with
    | isTrue e => by rw [e]; exact Nat.le_refl _
    | isFalse ne => by
      have q := len_relsEnc_mono ar A i M (Nat.le_of_lt_succ (Nat.lt_of_le_of_ne h ne))
      show _ ≤ lenG (appG (relsEnc ar A M) (4 :: relBits A M (ar M)))
      rw [len_appG]
      exact Nat.le_trans q (Nat.le_add_right _ _)

theorem relsEnc_block (ar : Nat → Nat) (A : FStr) : (M : Nat) → ∀ i, i < M → ∀ j, j < lenG (4 :: relBits A i (ar i)) →
    wAt (relsEnc ar A M) (j + lenG (relsEnc ar A i)) = wAt (4 :: relBits A i (ar i)) j
  | 0, i, h, _, _ => absurd h (Nat.not_lt_zero i)
  | M + 1, i, h, j, hj => by
    show wAt (appG (relsEnc ar A M) (4 :: relBits A M (ar M))) (j + lenG (relsEnc ar A i)) = _
    match Nat.decEq i M with
    | isTrue e =>
      subst e
      exact wAt_app_ge _ _ j
    | isFalse ne =>
      have hi : i < M := Nat.lt_of_le_of_ne (Nat.le_of_lt_succ h) ne
      have hlt : j + lenG (relsEnc ar A i) < lenG (relsEnc ar A M) := by
        have q := len_relsEnc_mono ar A (i + 1) M hi
        have e : lenG (relsEnc ar A (i + 1)) = lenG (relsEnc ar A i) + lenG (4 :: relBits A i (ar i)) := len_appG _ _
        rw [e, Nat.add_comm (lenG (relsEnc ar A i))] at q
        exact Nat.lt_of_lt_of_le (Nat.add_lt_add_right hj _) q
      rw [wAt_app_lt _ _ _ hlt]
      exact relsEnc_block ar A M i hi j hj

variable (m : Nat) (ar : Nat → Nat) (A : FStr)

theorem wAt_enc_unary {p : Nat} (hp : p < A.n) : wAt (enc m ar A) p = 3 := by
  show wAt (appG (replG 3 A.n) (relsEnc ar A m)) p = 3
  rw [wAt_app_lt _ _ p (by rw [len_replG]; exact hp)]
  exact wAt_repl 3 A.n p hp

theorem wAt_enc_shift (j : Nat) : wAt (enc m ar A) (j + A.n) = wAt (relsEnc ar A m) j := by
  show wAt (appG (replG 3 A.n) (relsEnc ar A m)) (j + A.n) = _
  have q := wAt_app_ge (replG 3 A.n) (relsEnc ar A m) j
  rw [len_replG] at q
  exact q

theorem wAt_enc_sep {i : Nat} (hi : i < m) : wAt (enc m ar A) (offE A.n ar i) = 4 := by
  rw [← len_relsEnc ar A i, wAt_enc_shift]
  have q := relsEnc_block ar A m i hi 0 (Nat.zero_lt_succ _)
  rw [Nat.zero_add] at q
  exact q

theorem wAt_enc_bit {i r : Nat} (hi : i < m) (hr : r < A.n ^ ar i) :
    wAt (enc m ar A) (offE A.n ar i + 1 + r) = bitOf (A.rel i (iterL A.n (ar i) r)) := by
  rw [← len_relsEnc ar A i]
  have e : lenG (relsEnc ar A i) + A.n + 1 + r = (r + 1 + lenG (relsEnc ar A i)) + A.n := by
    rw [Nat.add_right_comm _ A.n 1, Nat.add_right_comm _ A.n r, Nat.add_assoc (lenG (relsEnc ar A i)) 1 r,
      Nat.add_comm (lenG (relsEnc ar A i)) (1 + r), Nat.add_comm 1 r]
  rw [e, wAt_enc_shift, relsEnc_block ar A m i hi (r + 1) (Nat.succ_lt_succ (by rw [len_relBits]; exact hr))]
  exact wAt_relBits A i (ar i) r hr

theorem wAt_enc_end {p : Nat} (hp : offE A.n ar m ≤ p) : wAt (enc m ar A) p = 0 := by
  apply wAt_beyond
  show lenG (appG (replG 3 A.n) (relsEnc ar A m)) ≤ p
  rw [len_appG, len_replG, Nat.add_comm, len_relsEnc]
  exact hp

end Fg

end Found

#print axioms Found.Fg.wAt_app_ge
#print axioms Found.Fg.wAt_relBits
#print axioms Found.Fg.len_relsEnc
#print axioms Found.Fg.wAt_enc_unary
#print axioms Found.Fg.wAt_enc_sep
#print axioms Found.Fg.wAt_enc_bit
#print axioms Found.Fg.wAt_enc_end

found/Fg11.lean

lean_all/found/Fg11.lean

/-
  T185 (Fagin: NP = ESO), part 11. found/Fg11 — finite disjunctions and
  conjunctions of definable predicates; the variable index lists of the
  last d-tuple (rangeG d) and the one before (shifted by d), read back as
  the pushed tuples.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Fg10

namespace Found

namespace Fg

open Gt

/-- ⋁_{a < k} P a -/
theorem defF_bigOr {P : Nat → FStr → (Nat → Nat) → Prop} (hP : ∀ a, DefF (P a)) :
    (k : Nat) → DefF (fun A v => ∃ a, a < k ∧ P a A v)
  | 0 => defF_congr (fun _ _ => ⟨fun h => h.elim, fun ⟨a, ha, _⟩ => absurd ha (Nat.not_lt_zero a)⟩) defF_false
  | k + 1 => defF_congr (fun _ _ => ⟨fun h => h.elim (fun ⟨a, ha, h⟩ => ⟨a, Nat.lt_succ_of_lt ha, h⟩)
        (fun h => ⟨k, Nat.lt_succ_self k, h⟩),
      fun ⟨a, ha, h⟩ => match Nat.decEq a k with
        | isTrue e => .inr (e ▸ h)
        | isFalse ne => .inl ⟨a, Nat.lt_of_le_of_ne (Nat.le_of_lt_succ ha) ne, h⟩⟩)
      (defF_or (defF_bigOr hP k) (hP k))

/-- ⋀_{a < k} P a -/
theorem defF_bigAnd {P : Nat → FStr → (Nat → Nat) → Prop} (hP : ∀ a, DefF (P a)) :
    (k : Nat) → DefF (fun A v => ∀ a, a < k → P a A v)
  | 0 => defF_congr (fun _ _ => ⟨fun _ a ha => absurd ha (Nat.not_lt_zero a), fun _ => trivial⟩) defF_true
  | k + 1 => defF_congr (fun _ _ => ⟨fun ⟨h1, h2⟩ a ha => match Nat.decEq a k with
        | isTrue e => e ▸ h2
        | isFalse ne => h1 a (Nat.lt_of_le_of_ne (Nat.le_of_lt_succ ha) ne),
      fun h => ⟨fun a ha => h a (Nat.lt_succ_of_lt ha), h k (Nat.lt_succ_self k)⟩⟩)
      (defF_and (defF_bigAnd hP k) (hP k))

/-- ⋁ over the members of a list -/
theorem defF_listOr {X : Type} {P : X → FStr → (Nat → Nat) → Prop} (hP : ∀ x, DefF (P x)) :
    (l : List X) → DefF (fun A v => ∃ x, x ∈ l ∧ P x A v)
  | [] => defF_congr (fun _ _ => ⟨fun h => h.elim, fun ⟨_, hx, _⟩ => nomatch hx⟩) defF_false
  | x :: l => defF_congr (fun A v => ⟨fun h => h.elim (fun h => ⟨x, .head _, h⟩)
        (fun ⟨y, hy, h⟩ => ⟨y, .tail _ hy, h⟩),
      fun ⟨y, hy, h⟩ => by
        cases hy with
        | head => exact .inl h
        | tail _ hy' => exact .inr ⟨y, hy', h⟩⟩)
      (defF_or (hP x) (defF_listOr hP l))

/-- the variables of the tuple pushed before the last d-tuple -/
def prevIdx (d : Nat) : List Nat := lmapG (· + d) (rangeG d)

theorem extW_prev (v : Nat → Nat) (ts ss : List Nat) (hs : lenG ss = lenG ts) :
    lmapG (extW (extW v ts) ss) (prevIdx (lenG ts)) = ts := by
  show lmapG (extW (extW v ts) ss) (lmapG (· + lenG ts) (rangeG (lenG ts))) = ts
  rw [lmap_lmap]
  have e : ∀ k, extW (extW v ts) ss (k + lenG ts) = extW v ts k := fun k => by
    have q := extW_off (extW v ts) ss k
    rw [hs] at q
    exact q
  rw [lmapG_congr (f := fun k => extW (extW v ts) ss (k + lenG ts)) (g := extW v ts) _ (fun k _ => e k)]
  exact extW_range v ts

end Fg

end Found

#print axioms Found.Fg.defF_bigOr
#print axioms Found.Fg.defF_bigAnd
#print axioms Found.Fg.defF_listOr
#print axioms Found.Fg.extW_prev

found/Fg12.lean

lean_all/found/Fg12.lean

/-
  T185 (Fagin: NP = ESO), part 12. found/Fg12 — predicates over an
  environment of quantified tuples of given lengths (most recent first):
  the environment is read from the valuation in consecutive blocks;
  quantifying a new tuple pushes it on the environment; atoms on
  concatenations of environment tuples and the tuple predicates are
  definable.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Fg11

namespace Found

namespace Fg

open Gt

/-- the environment tuples of lengths lens read from w -/
def readEnvL (w : Nat → Nat) : List Nat → List (List Nat)
  | [] => []
  | d :: lens => lmapG w (rangeG d) :: readEnvL (fun k => w (k + d)) lens

theorem readEnvL_congr {w w' : Nat → Nat} (h : ∀ k, w k = w' k) : (lens : List Nat) → readEnvL w lens = readEnvL w' lens
  | [] => rfl
  | d :: lens => by
    show lmapG w (rangeG d) :: readEnvL (fun k => w (k + d)) lens = lmapG w' (rangeG d) :: readEnvL (fun k => w' (k + d)) lens
    rw [lmapG_congr _ (fun k _ => h k), readEnvL_congr (w := fun k => w (k + d)) (w' := fun k => w' (k + d)) (fun k => h (k + d)) lens]

theorem readEnvL_push (w : Nat → Nat) (hs : List Nat) (lens : List Nat) :
    readEnvL (extW w hs) (lenG hs :: lens) = hs :: readEnvL w lens := by
  show lmapG (extW w hs) (rangeG (lenG hs)) :: readEnvL (fun k => extW w hs (k + lenG hs)) lens = hs :: readEnvL w lens
  rw [extW_range, readEnvL_congr (extW_off w hs) lens]

/-- the j-th environment tuple -/
def envAt : List (List Nat) → Nat → List Nat
  | [], _ => []
  | e :: _, 0 => e
  | _ :: env, j + 1 => envAt env j

/-- the variables of the j-th environment tuple -/
def idxL : List Nat → Nat → List Nat
  | [], _ => []
  | d :: _, 0 => rangeG d
  | d :: lens, j + 1 => lmapG (· + d) (idxL lens j)

theorem read_idx (w : Nat → Nat) : (lens : List Nat) → ∀ j, lmapG w (idxL lens j) = envAt (readEnvL w lens) j
  | [], _ => rfl
  | _ :: _, 0 => rfl
  | d :: lens, j + 1 => by
    show lmapG w (lmapG (· + d) (idxL lens j)) = envAt (readEnvL (fun k => w (k + d)) lens) j
    rw [lmap_lmap]
    exact read_idx (fun k => w (k + d)) lens j

/-- the concatenation of selected environment tuples -/
def selE (env : List (List Nat)) : List Nat → List Nat
  | [] => []
  | j :: sel => appG (envAt env j) (selE env sel)

def selI (lens : List Nat) : List Nat → List Nat
  | [] => []
  | j :: sel => appG (idxL lens j) (selI lens sel)

theorem lmap_appG {X Y : Type} (f : X → Y) : (l l' : List X) → lmapG f (appG l l') = appG (lmapG f l) (lmapG f l')
  | [], _ => rfl
  | x :: l, l' => by
    show f x :: lmapG f (appG l l') = f x :: appG (lmapG f l) (lmapG f l')
    rw [lmap_appG f l l']

theorem read_sel (w : Nat → Nat) (lens : List Nat) : (sel : List Nat) →
    lmapG w (selI lens sel) = selE (readEnvL w lens) sel
  | [] => rfl
  | j :: sel => by
    show lmapG w (appG (idxL lens j) (selI lens sel)) = appG (envAt (readEnvL w lens) j) (selE (readEnvL w lens) sel)
    rw [lmap_appG, read_idx, read_sel w lens sel]

/-- definable predicates of an environment of tuples of lengths lens -/
def DefFE (lens : List Nat) (P : FStr → List (List Nat) → Prop) : Prop := DefF (fun B w => P B (readEnvL w lens))

theorem defFE_congr {lens : List Nat} {P Q : FStr → List (List Nat) → Prop} (h : ∀ B env, P B env ↔ Q B env)
    (hP : DefFE lens P) : DefFE lens Q :=
  defF_congr (fun B _ => h B _) hP

theorem defFE_and {lens : List Nat} {P Q : FStr → List (List Nat) → Prop} (hP : DefFE lens P) (hQ : DefFE lens Q) :
    DefFE lens (fun B env => P B env ∧ Q B env) := defF_and hP hQ

theorem defFE_or {lens : List Nat} {P Q : FStr → List (List Nat) → Prop} (hP : DefFE lens P) (hQ : DefFE lens Q) :
    DefFE lens (fun B env => P B env ∨ Q B env) := defF_or hP hQ

theorem defFE_neg {lens : List Nat} {P : FStr → List (List Nat) → Prop} (hP : DefFE lens P) :
    DefFE lens (fun B env => ¬ P B env) := defF_neg hP

theorem defFE_ex {lens : List Nat} (d : Nat) {P : FStr → List (List Nat) → Prop} (hP : DefFE (d :: lens) P) :
    DefFE lens (fun B env => ∃ hs, lenG hs = d ∧ DigitsLt B.n hs ∧ P B (hs :: env)) :=
  defF_congr (fun B w => ⟨fun ⟨hs, hl, hd, h⟩ => ⟨hs, hl, hd, by
      have q := readEnvL_push w hs lens
      rw [hl] at q
      rw [← q]; exact h⟩,
    fun ⟨hs, hl, hd, h⟩ => ⟨hs, hl, hd, by
      have q := readEnvL_push w hs lens
      rw [hl] at q
      rw [q]; exact h⟩⟩) (defF_exT d hP)

theorem defFE_all {lens : List Nat} (d : Nat) {P : FStr → List (List Nat) → Prop} (hP : DefFE (d :: lens) P) :
    DefFE lens (fun B env => ∀ hs, lenG hs = d → DigitsLt B.n hs → P B (hs :: env)) :=
  defF_congr (fun B w => ⟨fun h hs hl hd => by
      have q := readEnvL_push w hs lens
      rw [hl] at q
      rw [← q]; exact h hs hl hd,
    fun h hs hl hd => by
      have q := readEnvL_push w hs lens
      rw [hl] at q
      rw [q]; exact h hs hl hd⟩) (defF_allT d hP)

theorem defFE_rel (lens : List Nat) (j : Nat) (sel : List Nat) :
    DefFE lens (fun B env => B.rel j (selE env sel) = true) :=
  defF_congr (fun B w => by rw [read_sel]) (defF_rel j (selI lens sel))

section TP

variable (ℓ : Nat) (B : FStr) (w : Nat → Nat)

theorem tupEq_map : (I J : List Nat) → (TupEq B w I J ↔ TupEq B (fun x => x) (lmapG w I) (lmapG w J))
  | [], [] => Iff.rfl
  | _ :: I, _ :: J => ⟨fun ⟨h1, h2⟩ => ⟨h1, (tupEq_map I J).1 h2⟩, fun ⟨h1, h2⟩ => ⟨h1, (tupEq_map I J).2 h2⟩⟩
  | [], _ :: _ => Iff.rfl
  | _ :: _, [] => Iff.rfl

theorem allMin_map : (I : List Nat) → (AllMin ℓ B w I ↔ AllMin ℓ B (fun x => x) (lmapG w I))
  | [] => Iff.rfl
  | _ :: I => ⟨fun ⟨h1, h2⟩ => ⟨h1, (allMin_map I).1 h2⟩, fun ⟨h1, h2⟩ => ⟨h1, (allMin_map I).2 h2⟩⟩

theorem allMax_map : (I : List Nat) → (AllMax ℓ B w I ↔ AllMax ℓ B (fun x => x) (lmapG w I))
  | [] => Iff.rfl
  | _ :: I => ⟨fun ⟨h1, h2⟩ => ⟨h1, (allMax_map I).1 h2⟩, fun ⟨h1, h2⟩ => ⟨h1, (allMax_map I).2 h2⟩⟩

theorem tupSucc_map : (I J : List Nat) → (TupSucc ℓ B w I J ↔ TupSucc ℓ B (fun x => x) (lmapG w I) (lmapG w J))
  | [], [] => Iff.rfl
  | _ :: I, _ :: J => ⟨fun h => h.elim (fun ⟨h1, h2⟩ => .inl ⟨h1, (tupSucc_map I J).1 h2⟩)
        (fun ⟨h1, h2, h3⟩ => .inr ⟨h1, (allMax_map ℓ B w I).1 h2, (allMin_map ℓ B w J).1 h3⟩),
      fun h => h.elim (fun ⟨h1, h2⟩ => .inl ⟨h1, (tupSucc_map I J).2 h2⟩)
        (fun ⟨h1, h2, h3⟩ => .inr ⟨h1, (allMax_map ℓ B w I).2 h2, (allMin_map ℓ B w J).2 h3⟩)⟩
  | [], _ :: _ => Iff.rfl
  | _ :: _, [] => Iff.rfl

end TP

theorem defFE_tupEq (lens : List Nat) (a b : Nat) :
    DefFE lens (fun B env => TupEq B (fun x => x) (envAt env a) (envAt env b)) :=
  defF_congr (fun B w => by
    show _ ↔ TupEq B (fun x => x) (envAt (readEnvL w lens) a) (envAt (readEnvL w lens) b)
    rw [← read_idx, ← read_idx]; exact tupEq_map B w _ _) (defF_tupEq (idxL lens a) (idxL lens b))

theorem defFE_allMin (ℓ : Nat) (lens : List Nat) (a : Nat) :
    DefFE lens (fun B env => AllMin ℓ B (fun x => x) (envAt env a)) :=
  defF_congr (fun B w => by
    show _ ↔ AllMin ℓ B (fun x => x) (envAt (readEnvL w lens) a)
    rw [← read_idx]; exact allMin_map ℓ B w _) (defF_allMin ℓ (idxL lens a))

theorem defFE_allMax (ℓ : Nat) (lens : List Nat) (a : Nat) :
    DefFE lens (fun B env => AllMax ℓ B (fun x => x) (envAt env a)) :=
  defF_congr (fun B w => by
    show _ ↔ AllMax ℓ B (fun x => x) (envAt (readEnvL w lens) a)
    rw [← read_idx]; exact allMax_map ℓ B w _) (defF_allMax ℓ (idxL lens a))

theorem defFE_tupSucc (ℓ : Nat) (lens : List Nat) (a b : Nat) :
    DefFE lens (fun B env => TupSucc ℓ B (fun x => x) (envAt env a) (envAt env b)) :=
  defF_congr (fun B w => by
    show _ ↔ TupSucc ℓ B (fun x => x) (envAt (readEnvL w lens) a) (envAt (readEnvL w lens) b)
    rw [← read_idx, ← read_idx]; exact tupSucc_map ℓ B w _ _)
    (defF_tupSucc ℓ (idxL lens a) (idxL lens b))

theorem defFE_bigOr {lens : List Nat} {P : Nat → FStr → List (List Nat) → Prop} (hP : ∀ a, DefFE lens (P a)) (k : Nat) :
    DefFE lens (fun B env => ∃ a, a < k ∧ P a B env) := defF_bigOr hP k

theorem defFE_bigAnd {lens : List Nat} {P : Nat → FStr → List (List Nat) → Prop} (hP : ∀ a, DefFE lens (P a)) (k : Nat) :
    DefFE lens (fun B env => ∀ a, a < k → P a B env) := defF_bigAnd hP k

theorem defFE_listOr {X : Type} {lens : List Nat} {P : X → FStr → List (List Nat) → Prop} (hP : ∀ x, DefFE lens (P x))
    (l : List X) : DefFE lens (fun B env => ∃ x, x ∈ l ∧ P x B env) := defF_listOr hP l

theorem defFE_true (lens : List Nat) : DefFE lens (fun _ _ => True) := defF_true
theorem defFE_false (lens : List Nat) : DefFE lens (fun _ _ => False) := defF_false

end Fg

end Found

#print axioms Found.Fg.readEnvL_push
#print axioms Found.Fg.defFE_ex
#print axioms Found.Fg.defFE_all
#print axioms Found.Fg.defFE_rel
#print axioms Found.Fg.defFE_tupSucc
#print axioms Found.Fg.defFE_bigOr

found/Fg13.lean

lean_all/found/Fg13.lean

/-
  T185 (Fagin: NP = ESO), part 13. found/Fg13 — the tableau sentence for a
  finite nondeterministic machine, as constraints on an expansion B of the
  input structure: a linear order (symbol m); states St_q(t̄), head
  Hd(t̄, s̄), tape symbols Tb_a(t̄, s̄) over d-tuples of times and cells
  (d = e + 1); the layout of the encoding (separators Sp_i, bit positions
  Bp_i); the initial row; uniqueness; transitions; acceptance.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Fg12

namespace Found

namespace Fg

open Gt

structure TabP where
  m : Nat
  ar : Nat → Nat
  e : Nat
  M : NTM
  Qn : Nat
  G : Nat

namespace TabP

variable (P : TabP)

def d : Nat := P.e + 1
def iLT : Nat := P.m
def iSt (q : Nat) : Nat := P.m + 1 + q
def iHd : Nat := P.m + 1 + P.Qn
def iTb (a : Nat) : Nat := P.m + 2 + P.Qn + a
def iSp (i : Nat) : Nat := P.m + 2 + P.Qn + P.G + i
def iBp (i : Nat) : Nat := P.m + 2 + P.Qn + P.G + P.m + i

/-! closed forms -/

def R1 (B : FStr) (j : Nat) (xs : List Nat) : Prop := B.rel j (appG xs []) = true
def R2 (B : FStr) (j : Nat) (xs ys : List Nat) : Prop := B.rel j (appG xs (appG ys [])) = true

/-- tuples of length k in the universe -/
def Tu (B : FStr) (k : Nat) (xs : List Nat) : Prop := lenG xs = k ∧ DigitsLt B.n xs

def Sc (B : FStr) (xs ys : List Nat) : Prop := TupSucc P.iLT B (fun x => x) xs ys
def Mn (B : FStr) (xs : List Nat) : Prop := AllMin P.iLT B (fun x => x) xs
def Mx (B : FStr) (xs : List Nat) : Prop := AllMax P.iLT B (fun x => x) xs
def Eqt (B : FStr) (xs ys : List Nat) : Prop := TupEq B (fun x => x) xs ys

/-- the order axioms -/
def COrd (B : FStr) : Prop :=
  ∀ x, Tu B 1 x → ∀ y, Tu B 1 y → ∀ z, Tu B 1 z →
    ¬ R2 B P.iLT x x ∧ (R2 B P.iLT x y → R2 B P.iLT y z → R2 B P.iLT x z) ∧
    (R2 B P.iLT x y ∨ Eqt B x y ∨ R2 B P.iLT y x)

/-- s is a position of the unary block: (min, …, min, x) -/
def Unary (B : FStr) (s : List Nat) : Prop :=
  ∃ p, Tu B P.e p ∧ ∃ x, Tu B 1 x ∧ Mn P B p ∧ Eqt B s (appG p (appG x []))

/-- the last unary position (min, …, min, max) -/
def UnaryLast (B : FStr) (s : List Nat) : Prop :=
  ∃ p, Tu B P.e p ∧ ∃ x, Tu B 1 x ∧ Mn P B p ∧ Mx P B x ∧ Eqt B s (appG p (appG x []))

/-- the layout of the encoding -/
def CLay (B : FStr) : Prop :=
  (∀ s, Tu B P.d s → (R1 B (P.iSp 0) s ↔ ∃ r, Tu B P.d r ∧ UnaryLast P B r ∧ Sc P B r s)) ∧
  (∀ i, i < P.m → ∀ u, Tu B (P.ar i) u → ∀ s, Tu B P.d s →
    (R2 B (P.iBp i) u s ↔ ((Mn P B u ∧ ∃ r, Tu B P.d r ∧ R1 B (P.iSp i) r ∧ Sc P B r s) ∨
      (∃ u', Tu B (P.ar i) u' ∧ ∃ r, Tu B P.d r ∧ Sc P B u' u ∧ R2 B (P.iBp i) u' r ∧ Sc P B r s)))) ∧
  (∀ i, i + 1 < P.m → ∀ s, Tu B P.d s →
    (R1 B (P.iSp (i + 1)) s ↔ ∃ u, Tu B (P.ar i) u ∧ ∃ r, Tu B P.d r ∧ Mx P B u ∧ R2 B (P.iBp i) u r ∧ Sc P B r s))

/-- the symbol a belongs at position s of the initial tape -/
def InitSym (B : FStr) (a : Nat) (s : List Nat) : Prop :=
  (a = 3 ∧ Unary P B s) ∨ (a = 4 ∧ ∃ i, i < P.m ∧ R1 B (P.iSp i) s) ∨
  (∃ i, i < P.m ∧ ∃ u, Tu B (P.ar i) u ∧ R2 B (P.iBp i) u s ∧ ((a = 2 ∧ R1 B i u) ∨ (a = 1 ∧ ¬ R1 B i u))) ∨
  (a = 0 ∧ ¬ Unary P B s ∧ (∀ i, i < P.m → ¬ R1 B (P.iSp i) s) ∧
    (∀ i, i < P.m → ¬ ∃ u, Tu B (P.ar i) u ∧ R2 B (P.iBp i) u s))

/-- the initial row -/
def CInit (B : FStr) : Prop :=
  ∀ z, Tu B P.d z → Mn P B z →
    R1 B (P.iSt P.M.q0) z ∧ R2 B P.iHd z z ∧
    ∀ s, Tu B P.d s → ∀ a, a < P.G → (R2 B (P.iTb a) z s ↔ InitSym P B a s)

/-- uniqueness of state, head and symbols -/
def CUniq (B : FStr) : Prop :=
  ∀ t, Tu B P.d t →
    (∃ q, q < P.Qn ∧ R1 B (P.iSt q) t) ∧
    (∀ q, q < P.Qn → ∀ q', q' < P.Qn → R1 B (P.iSt q) t → R1 B (P.iSt q') t → q = q') ∧
    (∃ h, Tu B P.d h ∧ R2 B P.iHd t h) ∧
    (∀ h, Tu B P.d h → ∀ h', Tu B P.d h' → R2 B P.iHd t h → R2 B P.iHd t h' → Eqt B h h') ∧
    ∀ s, Tu B P.d s → (∃ a, a < P.G ∧ R2 B (P.iTb a) t s) ∧
      (∀ a, a < P.G → ∀ a', a' < P.G → R2 B (P.iTb a) t s → R2 B (P.iTb a') t s → a = a')

/-- the head moves right (true) or left (false; staying at the first cell) -/
def MoveOK (B : FStr) (t' h : List Nat) : Bool → Prop
  | true => ∃ r, Tu B P.d r ∧ Sc P B h r ∧ R2 B P.iHd t' r
  | false => (Mn P B h ∧ R2 B P.iHd t' h) ∨ ∃ r, Tu B P.d r ∧ Sc P B r h ∧ R2 B P.iHd t' r

/-- a step from row t to row t' -/
def Trans (B : FStr) (t t' : List Nat) : Prop :=
  ∃ h, Tu B P.d h ∧ R2 B P.iHd t h ∧
    (∃ q, q < P.Qn ∧ ∃ a, a < P.G ∧ R1 B (P.iSt q) t ∧ R2 B (P.iTb a) t h ∧
      ∃ mv, mv ∈ P.M.δ q a ∧ R1 B (P.iSt mv.1) t' ∧ R2 B (P.iTb mv.2.1) t' h ∧ MoveOK P B t' h mv.2.2) ∧
    ∀ s, Tu B P.d s → Eqt B s h ∨ ∀ a, a < P.G → (R2 B (P.iTb a) t s ↔ R2 B (P.iTb a) t' s)

def CTrans (B : FStr) : Prop :=
  ∀ t, Tu B P.d t → ∀ t', Tu B P.d t' → Sc P B t t' → R1 B (P.iSt P.M.acc) t ∨ Trans P B t t'

def CAcc (B : FStr) : Prop := ∃ t, Tu B P.d t ∧ R1 B (P.iSt P.M.acc) t

/-- the tableau sentence -/
def Tab (B : FStr) : Prop :=
  COrd P B ∧ CLay P B ∧ CInit P B ∧ CUniq P B ∧ CTrans P B ∧ CAcc P B

end TabP

end Fg

end Found

#print axioms Found.Fg.TabP.Tab

found/Fg14.lean

lean_all/found/Fg14.lean

/-
  T185 (Fagin: NP = ESO), part 14. found/Fg14 — the tableau sentence is
  first-order definable over the expanded structure: it is built from
  definable pieces by the environment combinators.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Fg13

namespace Found

namespace Fg

open Gt

section H

variable {lens : List Nat}

theorem defFE_of {P Q : FStr → List (List Nat) → Prop} (hP : DefFE lens P) (h : ∀ B env, P B env ↔ Q B env) :
    DefFE lens Q := defFE_congr h hP

theorem defFE_allTu (k : Nat) {P : FStr → List (List Nat) → Prop} (hP : DefFE (k :: lens) P) :
    DefFE lens (fun B env => ∀ x, TabP.Tu B k x → P B (x :: env)) :=
  defFE_congr (fun _ _ => ⟨fun h x hx => h x hx.1 hx.2, fun h x hl hd => h x ⟨hl, hd⟩⟩) (defFE_all k hP)

theorem defFE_exTu (k : Nat) {P : FStr → List (List Nat) → Prop} (hP : DefFE (k :: lens) P) :
    DefFE lens (fun B env => ∃ x, TabP.Tu B k x ∧ P B (x :: env)) :=
  defFE_congr (fun _ _ => ⟨fun ⟨x, hl, hd, h⟩ => ⟨x, ⟨hl, hd⟩, h⟩, fun ⟨x, hx, h⟩ => ⟨x, hx.1, hx.2, h⟩⟩) (defFE_ex k hP)

theorem defFE_imp (Kc : CP.Classical') {P Q : FStr → List (List Nat) → Prop} (hP : DefFE lens P) (hQ : DefFE lens Q) :
    DefFE lens (fun B env => P B env → Q B env) := defF_imp hP hQ Kc

theorem defFE_iff (Kc : CP.Classical') {P Q : FStr → List (List Nat) → Prop} (hP : DefFE lens P) (hQ : DefFE lens Q) :
    DefFE lens (fun B env => P B env ↔ Q B env) :=
  defFE_congr (fun _ _ => ⟨fun ⟨h1, h2⟩ => ⟨h1, h2⟩, fun h => ⟨h.1, h.2⟩⟩)
    (defFE_and (defFE_imp Kc hP hQ) (defFE_imp Kc hQ hP))

theorem defFE_dec (p : Prop) [h : Decidable p] : DefFE lens (fun _ _ => p) :=
  match h with
  | isTrue hp => defFE_congr (fun _ _ => ⟨fun _ => hp, fun _ => trivial⟩) (defFE_true lens)
  | isFalse hn => defFE_congr (fun _ _ => ⟨fun h => h.elim, fun h => hn h⟩) (defFE_false lens)

theorem defFE_R1 (j a : Nat) : DefFE lens (fun B env => TabP.R1 B j (envAt env a)) := defFE_rel lens j [a]

theorem defFE_R2 (j a b : Nat) : DefFE lens (fun B env => TabP.R2 B j (envAt env a) (envAt env b)) :=
  defFE_rel lens j [a, b]

theorem defFE_eqtSel (a b c : Nat) :
    DefFE lens (fun B env => TabP.Eqt B (envAt env a) (appG (envAt env b) (appG (envAt env c) []))) :=
  defF_congr (fun B w => by
    show _ ↔ TupEq B (fun x => x) (envAt (readEnvL w lens) a) (selE (readEnvL w lens) [b, c])
    rw [← read_idx, ← read_sel]; exact tupEq_map B w _ _) (defF_tupEq (idxL lens a) (selI lens [b, c]))

end H

namespace TabP

variable (P : TabP) (Kc : CP.Classical')

include Kc in
theorem def_COrd : DefFE [] (fun B _ => P.COrd B) := by
  have h := defFE_allTu (lens := []) 1 (defFE_allTu 1 (defFE_allTu 1
    (defFE_and (defFE_neg (defFE_R2 P.iLT 2 2))
      (defFE_and (defFE_imp Kc (defFE_R2 P.iLT 2 1) (defFE_imp Kc (defFE_R2 P.iLT 1 0) (defFE_R2 P.iLT 2 0)))
        (defFE_or (defFE_R2 P.iLT 2 1) (defFE_or (defFE_tupEq _ 2 1) (defFE_R2 P.iLT 1 2)))))))
  exact defFE_of h (fun _ _ => Iff.rfl)

/-- Unary at environment index a, in any environment -/
theorem def_Unary {lens : List Nat} (a : Nat) : DefFE lens (fun B env => P.Unary B (envAt env a)) :=
  defFE_exTu P.e (defFE_exTu 1 (defFE_and (defFE_allMin P.iLT _ 1) (defFE_eqtSel (a + 2) 1 0)))

theorem def_UnaryLast {lens : List Nat} (a : Nat) : DefFE lens (fun B env => P.UnaryLast B (envAt env a)) :=
  defFE_exTu P.e (defFE_exTu 1 (defFE_and (defFE_allMin P.iLT _ 1)
    (defFE_and (defFE_allMax P.iLT _ 0) (defFE_eqtSel (a + 2) 1 0))))

include Kc in
theorem def_CLay : DefFE [] (fun B _ => P.CLay B) := by
  have h1 := defFE_allTu (lens := []) P.d (defFE_iff Kc (defFE_R1 (P.iSp 0) 0)
    (defFE_exTu P.d (defFE_and (P.def_UnaryLast 0) (defFE_tupSucc P.iLT _ 0 1))))
  have h2 := defFE_bigAnd (lens := []) (fun i => defFE_allTu (P.ar i) (defFE_allTu P.d (defFE_iff Kc
    (defFE_R2 (P.iBp i) 1 0)
    (defFE_or (defFE_and (defFE_allMin P.iLT _ 1) (defFE_exTu P.d (defFE_and (defFE_R1 (P.iSp i) 0)
        (defFE_tupSucc P.iLT _ 0 1))))
      (defFE_exTu (P.ar i) (defFE_exTu P.d (defFE_and (defFE_tupSucc P.iLT _ 1 3)
        (defFE_and (defFE_R2 (P.iBp i) 1 0) (defFE_tupSucc P.iLT _ 0 2))))))))) P.m
  have h3 := defFE_bigAnd (lens := []) (fun i => defFE_imp Kc (defFE_dec (i + 1 < P.m))
    (defFE_allTu P.d (defFE_iff Kc (defFE_R1 (P.iSp (i + 1)) 0)
      (defFE_exTu (P.ar i) (defFE_exTu P.d (defFE_and (defFE_allMax P.iLT _ 1)
        (defFE_and (defFE_R2 (P.iBp i) 1 0) (defFE_tupSucc P.iLT _ 0 2)))))))) P.m
  exact defFE_of (defFE_and h1 (defFE_and h2 h3)) (fun _ _ => ⟨fun ⟨a, b, c⟩ => ⟨a, b, fun i hi => c i
      (Nat.lt_of_succ_lt hi) hi⟩, fun ⟨a, b, c⟩ => ⟨a, b, fun i _ hi => c i hi⟩⟩)

theorem def_InitSym {lens : List Nat} (a k : Nat) : DefFE lens (fun B env => P.InitSym B a (envAt env k)) :=
  defFE_or (defFE_and (defFE_dec (a = 3)) (P.def_Unary k))
    (defFE_or (defFE_and (defFE_dec (a = 4)) (defFE_bigOr (fun i => defFE_R1 (P.iSp i) k) P.m))
      (defFE_or (defFE_bigOr (fun i => defFE_exTu (P.ar i) (defFE_and (defFE_R2 (P.iBp i) 0 (k + 1))
          (defFE_or (defFE_and (defFE_dec (a = 2)) (defFE_R1 i 0))
            (defFE_and (defFE_dec (a = 1)) (defFE_neg (defFE_R1 i 0)))))) P.m)
        (defFE_and (defFE_dec (a = 0)) (defFE_and (defFE_neg (P.def_Unary k))
          (defFE_and (defFE_bigAnd (fun i => defFE_neg (defFE_R1 (P.iSp i) k)) P.m)
            (defFE_bigAnd (fun i => defFE_neg (defFE_exTu (P.ar i) (defFE_R2 (P.iBp i) 0 (k + 1)))) P.m))))))

include Kc in
theorem def_CInit : DefFE [] (fun B _ => P.CInit B) := by
  have h := defFE_allTu (lens := []) P.d (defFE_imp Kc (defFE_allMin P.iLT _ 0)
    (defFE_and (defFE_R1 (P.iSt P.M.q0) 0) (defFE_and (defFE_R2 P.iHd 0 0)
      (defFE_allTu P.d (defFE_bigAnd (fun a => defFE_iff Kc (defFE_R2 (P.iTb a) 1 0) (P.def_InitSym a 0)) P.G)))))
  exact defFE_of h (fun _ _ => Iff.rfl)

include Kc in
theorem def_CUniq : DefFE [] (fun B _ => P.CUniq B) := by
  have hq1 := defFE_bigOr (lens := [P.d]) (fun q => defFE_R1 (P.iSt q) 0) P.Qn
  have hq2 := defFE_bigAnd (lens := [P.d]) (fun q => defFE_bigAnd (fun q' => defFE_imp Kc (defFE_R1 (P.iSt q) 0)
    (defFE_imp Kc (defFE_R1 (P.iSt q') 0) (defFE_dec (q = q')))) P.Qn) P.Qn
  have hh1 := defFE_exTu (lens := [P.d]) P.d (defFE_R2 P.iHd 1 0)
  have hh2 := defFE_allTu (lens := [P.d]) P.d (defFE_allTu P.d (defFE_imp Kc (defFE_R2 P.iHd 2 1)
    (defFE_imp Kc (defFE_R2 P.iHd 2 0) (defFE_tupEq _ 1 0))))
  have hs := defFE_allTu (lens := [P.d]) P.d (defFE_and (defFE_bigOr (fun a => defFE_R2 (P.iTb a) 1 0) P.G)
    (defFE_bigAnd (fun a => defFE_bigAnd (fun a' => defFE_imp Kc (defFE_R2 (P.iTb a) 1 0)
      (defFE_imp Kc (defFE_R2 (P.iTb a') 1 0) (defFE_dec (a = a')))) P.G) P.G))
  have h := defFE_allTu (lens := []) P.d (defFE_and hq1 (defFE_and hq2 (defFE_and hh1 (defFE_and hh2 hs))))
  exact defFE_of h (fun _ _ => Iff.rfl)

theorem def_MoveOK {lens : List Nat} (t' hh : Nat) : (b : Bool) →
    DefFE lens (fun B env => P.MoveOK B (envAt env t') (envAt env hh) b)
  | true => defFE_exTu P.d (defFE_and (defFE_tupSucc P.iLT _ (hh + 1) 0) (defFE_R2 P.iHd (t' + 1) 0))
  | false => defFE_or (defFE_and (defFE_allMin P.iLT _ hh) (defFE_R2 P.iHd t' hh))
      (defFE_exTu P.d (defFE_and (defFE_tupSucc P.iLT _ 0 (hh + 1)) (defFE_R2 P.iHd (t' + 1) 0)))

include Kc in
theorem def_Trans {lens : List Nat} (t t' : Nat) : DefFE lens (fun B env => P.Trans B (envAt env t) (envAt env t')) :=
  defFE_exTu P.d (defFE_and (defFE_R2 P.iHd (t + 1) 0)
    (defFE_and (defFE_bigOr (fun q => defFE_bigOr (fun a => defFE_and (defFE_R1 (P.iSt q) (t + 1))
        (defFE_and (defFE_R2 (P.iTb a) (t + 1) 0) (defFE_listOr (fun mv => defFE_and (defFE_R1 (P.iSt mv.1) (t' + 1))
          (defFE_and (defFE_R2 (P.iTb mv.2.1) (t' + 1) 0) (P.def_MoveOK (t' + 1) 0 mv.2.2))) (P.M.δ q a)))) P.G) P.Qn)
      (defFE_allTu P.d (defFE_or (defFE_tupEq _ 0 1) (defFE_bigAnd (fun a => defFE_iff Kc
        (defFE_R2 (P.iTb a) (t + 2) 0) (defFE_R2 (P.iTb a) (t' + 2) 0)) P.G)))))

include Kc in
theorem def_CTrans : DefFE [] (fun B _ => P.CTrans B) := by
  have h := defFE_allTu (lens := []) P.d (defFE_allTu P.d (defFE_imp Kc (defFE_tupSucc P.iLT _ 1 0)
    (defFE_or (defFE_R1 (P.iSt P.M.acc) 1) (P.def_Trans Kc 1 0))))
  exact defFE_of h (fun _ _ => Iff.rfl)

theorem def_CAcc : DefFE [] (fun B _ => P.CAcc B) := by
  have h := defFE_exTu (lens := []) P.d (defFE_R1 (P.iSt P.M.acc) 0)
  exact defFE_of h (fun _ _ => Iff.rfl)

include Kc in
/-- the tableau sentence is first-order -/
theorem def_Tab : DefFE [] (fun B _ => P.Tab B) :=
  defFE_and (P.def_COrd Kc) (defFE_and (P.def_CLay Kc) (defFE_and (P.def_CInit Kc) (defFE_and (P.def_CUniq Kc)
    (defFE_and (P.def_CTrans Kc) P.def_CAcc))))

end TabP

end Fg

end Found

#print axioms Found.Fg.defFE_iff
#print axioms Found.Fg.defFE_eqtSel
#print axioms Found.Fg.TabP.def_COrd
#print axioms Found.Fg.TabP.def_Unary
#print axioms Found.Fg.TabP.def_CLay
#print axioms Found.Fg.TabP.def_CInit
#print axioms Found.Fg.TabP.def_CUniq
#print axioms Found.Fg.TabP.def_CTrans
#print axioms Found.Fg.TabP.def_Tab

found/Fg15.lean

lean_all/found/Fg15.lean

/-
  T185 (Fagin: NP = ESO), part 15. found/Fg15 — base-n numerals of digit
  lists: the value is injective on digit lists of equal length; the value
  of a list with a digit appended; all-zero lists are exactly those of
  value 0.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Fg14

namespace Found

namespace Fg

open Gt

theorem addMulR (a b c : Nat) : (a + b) * c = a * c + b * c := by
  rw [Nat.mul_comm, Nat.left_distrib, Nat.mul_comm c a, Nat.mul_comm c b]

theorem mulAssoc (a b : Nat) : (c : Nat) → a * b * c = a * (b * c)
  | 0 => by rw [Nat.mul_zero, Nat.mul_zero, Nat.mul_zero]
  | c + 1 => by rw [Nat.mul_succ, Nat.mul_succ, Nat.left_distrib, mulAssoc a b c]

theorem addLeftCancel : (a : Nat) → ∀ {b c : Nat}, a + b = a + c → b = c
  | 0, b, c, h => by rw [Nat.zero_add, Nat.zero_add] at h; exact h
  | a + 1, b, c, h => by
    rw [Nat.succ_add, Nat.succ_add] at h
    exact addLeftCancel a (Nat.succ.inj h)

variable (n : Nat)

theorem lt_mul_pow {a b k : Nat} (ha : a < b) (r : Nat) (hr : r < n ^ k) : a * n ^ k + r < b * n ^ k :=
  Nat.lt_of_lt_of_le (Nat.add_lt_add_left hr _) (by rw [← Nat.succ_mul]; exact Nat.mul_le_mul_right _ ha)

/-- equal values of equal-length digit lists -/
theorem num_inj : (l l' : List Nat) → lenG l = lenG l' → DigitsLt n l → DigitsLt n l' → num n l = num n l' → l = l'
  | [], [], _, _, _, _ => rfl
  | a :: as, b :: bs, hl, ⟨ha, hs⟩, ⟨hb, ht⟩, he => by
    have hl' : lenG as = lenG bs := Nat.succ.inj hl
    have e : a * n ^ lenG as + num n as = b * n ^ lenG as + num n bs := by
      have q : a * n ^ lenG as + num n as = b * n ^ lenG bs + num n bs := he
      rw [← hl'] at q; exact q
    have hab : a = b := match Nat.lt_or_ge a b with
      | .inl h => absurd e (Nat.ne_of_lt (Nat.lt_of_lt_of_le (lt_mul_pow n h _ (num_lt n as hs)) (Nat.le_add_right _ _)))
      | .inr h => match Nat.lt_or_ge b a with
        | .inl h' => absurd e.symm (Nat.ne_of_lt (Nat.lt_of_lt_of_le (lt_mul_pow n h' _
            (by rw [hl']; exact num_lt n bs ht)) (Nat.le_add_right _ _)))
        | .inr h' => Nat.le_antisymm h' h
    subst hab
    have e2 : num n as = num n bs := addLeftCancel _ e
    rw [num_inj as bs hl' hs ht e2]
  | [], _ :: _, hl, _, _, _ => Nat.noConfusion hl
  | _ :: _, [], hl, _, _, _ => Nat.noConfusion hl

theorem num_app1 : (p : List Nat) → (x : Nat) → num n (appG p [x]) = num n p * n + x
  | [], x => by show x * n ^ 0 + 0 = 0 * n + x; rw [Nat.pow_zero, Nat.mul_one, Nat.zero_mul, Nat.zero_add, Nat.add_zero]
  | a :: p, x => by
    show a * n ^ lenG (appG p [x]) + num n (appG p [x]) = (a * n ^ lenG p + num n p) * n + x
    rw [len_appG, num_app1 p x, addMulR, mulAssoc, ← Nat.pow_succ, Nat.add_assoc]
    rfl

theorem digits_app {p : List Nat} {x : Nat} (hp : DigitsLt n p) (hx : x < n) : DigitsLt n (appG p [x]) := by
  induction p with
  | nil => exact ⟨hx, trivial⟩
  | cons a p ih => exact ⟨hp.1, ih hp.2⟩

theorem num_zero_iff (hn : 1 ≤ n) (l : List Nat) (hl : DigitsLt n l) : num n l = 0 ↔ l = replG 0 (lenG l) :=
  ⟨fun h => num_inj n l _ (len_replG 0 _).symm hl (digits_zero n hn _) (h.trans (num_zero n _).symm),
   fun h => by rw [h, num_zero]⟩

end Fg

end Found

#print axioms Found.Fg.num_inj
#print axioms Found.Fg.num_app1
#print axioms Found.Fg.num_zero_iff

found/Fg16.lean

lean_all/found/Fg16.lean

/-
  T185 (Fagin: NP = ESO), part 16. found/Fg16 — under the natural order the
  tableau's tuple predicates are conditions on base-n values: least tuple =
  value 0, greatest = value n^d - 1, successor = value + 1, equality =
  equality, unary positions = values < n, the last unary position =
  value n - 1.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Fg15

namespace Found

namespace Fg

open Gt

theorem lmap_id' : (l : List Nat) → lmapG (fun x => x) l = l
  | [] => rfl
  | x :: l => by show x :: lmapG (fun x => x) l = x :: l; rw [lmap_id' l]

theorem tupEq_id (B : FStr) : (xs ys : List Nat) → lenG xs = lenG ys → (TupEq B (fun x => x) xs ys ↔ xs = ys)
  | [], [], _ => ⟨fun _ => rfl, fun _ => trivial⟩
  | x :: xs, y :: ys, hl => ⟨fun ⟨h1, h2⟩ => by
        show x :: xs = y :: ys
        rw [show x = y from h1, (tupEq_id B xs ys (Nat.succ.inj hl)).1 h2],
      fun h => have e := List.cons.inj h; ⟨e.1, (tupEq_id B xs ys (Nat.succ.inj hl)).2 e.2⟩⟩
  | [], _ :: _, hl => Nat.noConfusion hl
  | _ :: _, [], hl => Nat.noConfusion hl

theorem split_last : (e : Nat) → (l : List Nat) → lenG l = e + 1 → ∃ p x, lenG p = e ∧ l = appG p [x]
  | 0, [x], _ => ⟨[], x, rfl, rfl⟩
  | 0, [], h => Nat.noConfusion h
  | 0, _ :: _ :: _, h => Nat.noConfusion (Nat.succ.inj h)
  | _ + 1, [], h => Nat.noConfusion h
  | e + 1, a :: l, h => match split_last e l (Nat.succ.inj h) with
    | ⟨p, x, hp, hl⟩ => ⟨a :: p, x, congrArg (· + 1) hp, by rw [hl]; rfl⟩

theorem pow_pos1 (n : Nat) (hn : 1 ≤ n) : (k : Nat) → 1 ≤ n ^ k
  | 0 => by rw [Nat.pow_zero]; exact Nat.le_refl 1
  | k + 1 => by rw [Nat.pow_succ]; exact Nat.le_trans (pow_pos1 n hn k) (Nat.le_mul_of_pos_right _ hn)

theorem allMaxL_of_num (n : Nat) (hn : 1 ≤ n) : (l : List Nat) → DigitsLt n l → num n l + 1 = n ^ lenG l → AllMaxL n l
  | [], _, _ => trivial
  | a :: as, ⟨ha, hs⟩, h => by
    have hk := num_lt n as hs
    have h' : a * n ^ lenG as + num n as + 1 = n ^ (lenG as + 1) := h
    have hpos : 1 ≤ n ^ lenG as := pow_pos1 n hn (lenG as)
    have ha' : a + 1 = n := match Nat.lt_or_ge (a + 1) n with
      | .inl hl => by
        have h1 : a * n ^ lenG as + num n as + 1 ≤ (a + 1) * n ^ lenG as := by
          rw [Nat.add_assoc, Nat.succ_mul]; exact Nat.add_le_add_left hk _
        have h2 : (a + 1) * n ^ lenG as < (a + 1 + 1) * n ^ lenG as := by
          rw [Nat.succ_mul (a + 1)]; exact Nat.lt_of_lt_of_le (Nat.lt_succ_self _) (Nat.add_le_add_left hpos _)
        have h3 : (a + 1 + 1) * n ^ lenG as ≤ n * n ^ lenG as := Nat.mul_le_mul_right _ hl
        have q := Nat.lt_of_le_of_lt h1 (Nat.lt_of_lt_of_le h2 h3)
        rw [h', Nat.pow_succ, Nat.mul_comm] at q
        exact absurd q (Nat.lt_irrefl _)
      | .inr hg => Nat.le_antisymm ha hg
    refine ⟨ha', allMaxL_of_num n hn as hs ?_⟩
    have q : a * n ^ lenG as + (num n as + 1) = a * n ^ lenG as + n ^ lenG as := by
      rw [← Nat.add_assoc, h', Nat.pow_succ, ← ha', Nat.mul_succ, Nat.mul_comm]
    exact addLeftCancel _ q

theorem digits_prefix (n : Nat) : (p l : List Nat) → DigitsLt n (appG p l) → DigitsLt n p ∧ DigitsLt n l
  | [], _, h => ⟨trivial, h⟩
  | _ :: p, l, ⟨ha, h⟩ => ⟨⟨ha, (digits_prefix n p l h).1⟩, (digits_prefix n p l h).2⟩

namespace TabP

variable (P : TabP) {B : FStr} (hN : ∀ x y, x < B.n → y < B.n → (ltR P.iLT B x y ↔ x < y)) (hn : 1 ≤ B.n)

include hN hn in
theorem mn_iff {xs : List Nat} (hx : DigitsLt B.n xs) : P.Mn B xs ↔ num B.n xs = 0 := by
  have q := allMin_nat P.iLT hN (fun x => x) xs (by rw [lmap_id']; exact hx)
  rw [lmap_id'] at q
  exact q.trans (num_zero_iff B.n hn xs hx).symm

include hN hn in
theorem mx_iff {xs : List Nat} (hx : DigitsLt B.n xs) : P.Mx B xs ↔ num B.n xs + 1 = B.n ^ lenG xs := by
  have q := allMax_nat P.iLT hN (fun x => x) xs (by rw [lmap_id']; exact hx)
  rw [lmap_id'] at q
  exact q.trans ⟨num_max B.n hn xs, allMaxL_of_num B.n hn xs hx⟩

include hN hn in
theorem sc_iff {xs ys : List Nat} (hl : lenG xs = lenG ys) (hx : DigitsLt B.n xs) (hy : DigitsLt B.n ys) :
    P.Sc B xs ys ↔ num B.n ys = num B.n xs + 1 := by
  have q := tupSucc_nat P.iLT hN (fun x => x) xs ys hl (by rw [lmap_id']; exact hx) (by rw [lmap_id']; exact hy)
  rw [lmap_id', lmap_id'] at q
  refine q.trans ⟨fun ⟨h1, h2⟩ => by rw [h2]; exact (inc_spec B.n hn xs hx h1).2, fun h => ?_⟩
  have hnm : ¬ AllMaxL B.n xs := fun hm => by
    have e := num_max B.n hn xs hm
    have q2 := num_lt B.n ys hy
    rw [h, e, hl] at q2
    exact Nat.lt_irrefl _ q2
  have r := inc_spec B.n hn xs hx hnm
  exact ⟨hnm, num_inj B.n ys _ (by rw [incL_len]; exact hl.symm) hy r.1 (h.trans r.2.symm)⟩

omit hN hn in
theorem eqt_iff {xs ys : List Nat} (hl : lenG xs = lenG ys) : TabP.Eqt B xs ys ↔ xs = ys := tupEq_id B xs ys hl

theorem one_tuple : (x : List Nat) → lenG x = 1 → ∃ x0, x = [x0]
  | [x0], _ => ⟨x0, rfl⟩
  | [], h => Nat.noConfusion h
  | _ :: _ :: _, h => Nat.noConfusion (Nat.succ.inj h)

include hN hn in
theorem unary_iff {s : List Nat} (hs : TabP.Tu B P.d s) : P.Unary B s ↔ num B.n s < B.n := by
  constructor
  · intro ⟨p, ⟨hpl, hpd⟩, x, ⟨hxl, hxd⟩, hmn, he⟩
    match one_tuple x hxl with
    | ⟨x0, hx0⟩ =>
    subst hx0
    have hs' : s = appG p [x0] := (eqt_iff (B := B) (by rw [len_appG, hpl, hs.1]; rfl)).1 he
    rw [hs', num_app1, (P.mn_iff hN hn hpd).1 hmn, Nat.zero_mul, Nat.zero_add]
    exact hxd.1
  · intro h
    match split_last P.e s hs.1 with
    | ⟨p, x0, hpl, hsp⟩ =>
    have hd := digits_prefix B.n p [x0] (hsp ▸ hs.2)
    rw [hsp, num_app1] at h
    have hp0 : num B.n p = 0 := match num B.n p, h with
      | 0, _ => rfl
      | k + 1, h => absurd (Nat.lt_of_le_of_lt (Nat.le_trans (Nat.le_mul_of_pos_left B.n (Nat.succ_pos k))
          (Nat.le_add_right _ _)) h) (Nat.lt_irrefl _)
    exact ⟨p, ⟨hpl, hd.1⟩, [x0], ⟨rfl, hd.2⟩, (P.mn_iff hN hn hd.1).2 hp0,
      (eqt_iff (B := B) (by rw [hsp, len_appG, len_appG]; rfl)).2 (by rw [hsp]; rfl)⟩

theorem num_single (n x0 : Nat) : num n [x0] = x0 := by
  show x0 * n ^ 0 + 0 = x0
  rw [Nat.pow_zero, Nat.mul_one, Nat.add_zero]

include hN hn in
theorem unaryLast_iff {s : List Nat} (hs : TabP.Tu B P.d s) : P.UnaryLast B s ↔ num B.n s + 1 = B.n := by
  constructor
  · intro ⟨p, ⟨hpl, hpd⟩, x, ⟨hxl, hxd⟩, hmn, hmx, he⟩
    match one_tuple x hxl with
    | ⟨x0, hx0⟩ =>
    subst hx0
    have hs' : s = appG p [x0] := (eqt_iff (B := B) (by rw [len_appG, hpl, hs.1]; rfl)).1 he
    have hm := (P.mx_iff hN hn hxd).1 hmx
    rw [num_single] at hm
    have hp1 : B.n ^ lenG [x0] = B.n := by show B.n ^ (0 + 1) = B.n; rw [Nat.zero_add, Nat.pow_succ, Nat.pow_zero, Nat.one_mul]
    rw [hs', num_app1, (P.mn_iff hN hn hpd).1 hmn, Nat.zero_mul, Nat.zero_add, hm, hp1]
  · intro h
    match split_last P.e s hs.1 with
    | ⟨p, x0, hpl, hsp⟩ =>
    have hd := digits_prefix B.n p [x0] (hsp ▸ hs.2)
    rw [hsp, num_app1] at h
    have hp0 : num B.n p = 0 := match num B.n p, h with
      | 0, _ => rfl
      | k + 1, h => absurd (Nat.lt_of_le_of_lt (Nat.le_trans (Nat.le_mul_of_pos_left B.n (Nat.succ_pos k))
          (Nat.le_add_right _ _)) (Nat.lt_of_succ_le (Nat.le_of_eq h))) (Nat.lt_irrefl _)
    rw [hp0, Nat.zero_mul, Nat.zero_add] at h
    exact ⟨p, ⟨hpl, hd.1⟩, [x0], ⟨rfl, hd.2⟩, (P.mn_iff hN hn hd.1).2 hp0,
      (P.mx_iff hN hn hd.2).2 (by
        rw [num_single]
        show x0 + 1 = B.n ^ (0 + 1)
        rw [Nat.zero_add, Nat.pow_succ, Nat.pow_zero, Nat.one_mul]; exact h),
      (eqt_iff (B := B) (by rw [hsp, len_appG, len_appG]; rfl)).2 (by rw [hsp]; rfl)⟩

end TabP

end Fg

end Found

#print axioms Found.Fg.tupEq_id
#print axioms Found.Fg.split_last
#print axioms Found.Fg.allMaxL_of_num
#print axioms Found.Fg.TabP.sc_iff
#print axioms Found.Fg.TabP.unary_iff
#print axioms Found.Fg.TabP.unaryLast_iff

found/Fg17.lean

lean_all/found/Fg17.lean

/-
  T185 (Fagin: NP = ESO), part 17. found/Fg17 — the layout constraints
  under the natural order: if the encoding fits (offE m ≤ n^d), the layout
  constraints hold exactly when the separator of block i is at position
  offE i and the bit of tuple u in block i is at position offE i + 1 + num u.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Fg16

namespace Found

namespace Fg

open Gt

namespace TabP

variable (P : TabP) {B : FStr} (hN : ∀ x y, x < B.n → y < B.n → (ltR P.iLT B x y ↔ x < y)) (hn : 1 ≤ B.n)

theorem subOneAdd : (n : Nat) → 1 ≤ n → n - 1 + 1 = n
  | 0, h => absurd h (Nat.not_succ_le_zero 0)
  | _ + 1, _ => rfl

theorem subOneLe : (n : Nat) → n - 1 ≤ n
  | 0 => Nat.le_refl 0
  | k + 1 => Nat.le_succ k

include hn in
/-- a tuple of given value -/
theorem tup_of (len k : Nat) (hk : k < B.n ^ len) : ∃ xs, TabP.Tu B len xs ∧ num B.n xs = k :=
  ⟨iterL B.n len k, ⟨(iter_spec B.n hn len k hk).2.2, (iter_spec B.n hn len k hk).1⟩, (iter_spec B.n hn len k hk).2.1⟩

/-- the numeric layout -/
def SpOK (B : FStr) (i : Nat) : Prop := ∀ s, TabP.Tu B P.d s → (TabP.R1 B (P.iSp i) s ↔ num B.n s = offE B.n P.ar i)
def BpOK (B : FStr) (i : Nat) : Prop := ∀ u, TabP.Tu B (P.ar i) u → ∀ s, TabP.Tu B P.d s →
  (TabP.R2 B (P.iBp i) u s ↔ num B.n s = offE B.n P.ar i + 1 + num B.n u)

include hN hn in
theorem sp0_num (hfit : B.n < B.n ^ P.d) (s : List Nat) (hs : TabP.Tu B P.d s) :
    (∃ r, TabP.Tu B P.d r ∧ P.UnaryLast B r ∧ P.Sc B r s) ↔ num B.n s = offE B.n P.ar 0 :=
  ⟨fun ⟨r, hr, hu, hsc⟩ => by
      have e1 := (P.unaryLast_iff hN hn hr).1 hu
      have e2 := (P.sc_iff hN hn (hr.1.trans hs.1.symm) hr.2 hs.2).1 hsc
      rw [e2, e1]; rfl,
    fun h => by
      match tup_of (B := B) hn P.d (B.n - 1) (Nat.lt_of_le_of_lt (subOneLe _) hfit) with
      | ⟨r, hr, hrv⟩ =>
      have e1 : num B.n r + 1 = B.n := by rw [hrv]; exact subOneAdd _ hn
      refine ⟨r, hr, (P.unaryLast_iff hN hn hr).2 e1, (P.sc_iff hN hn (hr.1.trans hs.1.symm) hr.2 hs.2).2 ?_⟩
      rw [e1]; exact h⟩

include hN hn in
theorem sp0_ok (hfit : B.n < B.n ^ P.d)
    (hc : ∀ s, TabP.Tu B P.d s → (TabP.R1 B (P.iSp 0) s ↔ ∃ r, TabP.Tu B P.d r ∧ P.UnaryLast B r ∧ P.Sc B r s)) :
    P.SpOK B 0 := fun s hs => (hc s hs).trans (P.sp0_num hN hn hfit s hs)

include hN hn in
theorem bp_ok (i : Nat) (hsp : P.SpOK B i)
    (hc : ∀ u, TabP.Tu B (P.ar i) u → ∀ s, TabP.Tu B P.d s →
      (TabP.R2 B (P.iBp i) u s ↔ ((P.Mn B u ∧ ∃ r, TabP.Tu B P.d r ∧ TabP.R1 B (P.iSp i) r ∧ P.Sc B r s) ∨
        (∃ u', TabP.Tu B (P.ar i) u' ∧ ∃ r, TabP.Tu B P.d r ∧ P.Sc B u' u ∧ TabP.R2 B (P.iBp i) u' r ∧ P.Sc B r s)))) :
    P.BpOK B i := by
  have main : ∀ k u, TabP.Tu B (P.ar i) u → num B.n u = k → ∀ s, TabP.Tu B P.d s →
      (TabP.R2 B (P.iBp i) u s ↔ num B.n s = offE B.n P.ar i + 1 + k) := by
    intro k
    induction k with
    | zero =>
      intro u hu hk s hs
      refine (hc u hu s hs).trans ⟨fun h => ?_, fun h => ?_⟩
      · match h with
        | .inl ⟨_, r, hr, hsr, hsc⟩ =>
          rw [(P.sc_iff hN hn (hr.1.trans hs.1.symm) hr.2 hs.2).1 hsc, (hsp r hr).1 hsr]
        | .inr ⟨u', hu', _, _, hsc, _, _⟩ =>
          rw [(P.sc_iff hN hn (hu'.1.trans hu.1.symm) hu'.2 hu.2).1 hsc] at hk
          exact Nat.noConfusion hk
      · have hlt : offE B.n P.ar i < B.n ^ P.d := Nat.lt_of_lt_of_le (Nat.lt_succ_self _)
          (Nat.le_of_lt (h ▸ num_lt B.n s hs.2 |> fun q => by rw [hs.1] at q; exact q))
        match tup_of (B := B) hn P.d _ hlt with
        | ⟨r, hr, hrv⟩ =>
        exact .inl ⟨(P.mn_iff hN hn hu.2).2 hk, r, hr, (hsp r hr).2 hrv,
          (P.sc_iff hN hn (hr.1.trans hs.1.symm) hr.2 hs.2).2 (by rw [h, hrv])⟩
    | succ k ih =>
      intro u hu hk s hs
      refine (hc u hu s hs).trans ⟨fun h => ?_, fun h => ?_⟩
      · match h with
        | .inl ⟨hm, _⟩ =>
          rw [(P.mn_iff hN hn hu.2).1 hm] at hk
          exact Nat.noConfusion hk
        | .inr ⟨u', hu', r, hr, hsc1, hb, hsc2⟩ =>
          have e1 := (P.sc_iff hN hn (hu'.1.trans hu.1.symm) hu'.2 hu.2).1 hsc1
          rw [hk] at e1
          have e2 := (ih u' hu' (Nat.succ.inj e1).symm r hr).1 hb
          rw [(P.sc_iff hN hn (hr.1.trans hs.1.symm) hr.2 hs.2).1 hsc2, e2]
          rfl
      · have hu' := num_lt B.n u hu.2
        rw [hu.1, hk] at hu'
        have hk' : k < B.n ^ P.ar i := Nat.lt_of_succ_lt hu'
        have hs' := num_lt B.n s hs.2
        rw [hs.1, h] at hs'
        have hr' : offE B.n P.ar i + 1 + k < B.n ^ P.d := Nat.lt_of_lt_of_le (Nat.lt_succ_self _) (Nat.le_of_lt hs')
        match tup_of (B := B) hn (P.ar i) k hk', tup_of (B := B) hn P.d _ hr' with
        | ⟨u', hu', hu'v⟩, ⟨r, hr, hrv⟩ =>
        exact .inr ⟨u', hu', r, hr, (P.sc_iff hN hn (hu'.1.trans hu.1.symm) hu'.2 hu.2).2 (by rw [hk, hu'v]),
          (ih u' hu' hu'v r hr).2 hrv, (P.sc_iff hN hn (hr.1.trans hs.1.symm) hr.2 hs.2).2 (by rw [h, hrv]; rfl)⟩
  exact fun u hu s hs => main (num B.n u) u hu rfl s hs

include hN hn in
theorem spS_num (i : Nat) (hbp : P.BpOK B i) (s : List Nat) (hs : TabP.Tu B P.d s) :
    (∃ u, TabP.Tu B (P.ar i) u ∧ ∃ r, TabP.Tu B P.d r ∧ P.Mx B u ∧ TabP.R2 B (P.iBp i) u r ∧ P.Sc B r s) ↔
    num B.n s = offE B.n P.ar (i + 1) :=
  ⟨fun ⟨u, hu, r, hr, hmx, hb, hsc⟩ => by
      have e1 := (P.mx_iff hN hn hu.2).1 hmx
      rw [hu.1] at e1
      have e2 := (hbp u hu r hr).1 hb
      rw [(P.sc_iff hN hn (hr.1.trans hs.1.symm) hr.2 hs.2).1 hsc, e2, Nat.add_assoc _ (num B.n u) 1, e1]
      rfl,
    fun h => by
      have hpos := pow_pos1 B.n hn (P.ar i)
      have hs' := num_lt B.n s hs.2
      rw [hs.1, h] at hs'
      have hu'' : B.n ^ P.ar i - 1 < B.n ^ P.ar i := Nat.lt_of_lt_of_le (Nat.lt_succ_self _)
        (Nat.le_of_eq (subOneAdd _ hpos))
      have hr' : offE B.n P.ar i + 1 + (B.n ^ P.ar i - 1) < B.n ^ P.d := by
        refine Nat.lt_of_lt_of_le ?_ (Nat.le_of_lt hs')
        show offE B.n P.ar i + 1 + (B.n ^ P.ar i - 1) < offE B.n P.ar i + 1 + B.n ^ P.ar i
        exact Nat.add_lt_add_left hu'' _
      match tup_of (B := B) hn (P.ar i) _ hu'', tup_of (B := B) hn P.d _ hr' with
      | ⟨u, hu, huv⟩, ⟨r, hr, hrv⟩ =>
      refine ⟨u, hu, r, hr, (P.mx_iff hN hn hu.2).2 (by rw [huv, hu.1]; exact subOneAdd _ hpos),
        (hbp u hu r hr).2 (by rw [hrv, huv]), (P.sc_iff hN hn (hr.1.trans hs.1.symm) hr.2 hs.2).2 ?_⟩
      rw [h, hrv, Nat.add_assoc _ (B.n ^ P.ar i - 1) 1, subOneAdd _ hpos]
      rfl⟩

include hN hn in
theorem sp_succ_ok (i : Nat) (hbp : P.BpOK B i)
    (hc : ∀ s, TabP.Tu B P.d s → (TabP.R1 B (P.iSp (i + 1)) s ↔
      ∃ u, TabP.Tu B (P.ar i) u ∧ ∃ r, TabP.Tu B P.d r ∧ P.Mx B u ∧ TabP.R2 B (P.iBp i) u r ∧ P.Sc B r s)) :
    P.SpOK B (i + 1) := fun s hs => (hc s hs).trans (P.spS_num hN hn i hbp s hs)

include hN hn in
theorem lay_ok (hfit : B.n < B.n ^ P.d) (hL : P.CLay B) : ∀ i, i < P.m → P.SpOK B i ∧ P.BpOK B i
  | 0, h0 => have sp := P.sp0_ok hN hn hfit hL.1
      ⟨sp, P.bp_ok hN hn 0 sp (hL.2.1 0 h0)⟩
  | i + 1, h => match lay_ok hfit hL i (Nat.lt_of_succ_lt h) with
    | ⟨_, bpi⟩ => have sp := P.sp_succ_ok hN hn i bpi (hL.2.2 i h)
      ⟨sp, P.bp_ok hN hn (i + 1) sp (hL.2.1 (i + 1) h)⟩

include hN hn in
theorem bp_rhs (i : Nat) (hsp : P.SpOK B i) (hbp : P.BpOK B i) (u : List Nat) (hu : TabP.Tu B (P.ar i) u)
    (s : List Nat) (hs : TabP.Tu B P.d s) :
    ((P.Mn B u ∧ ∃ r, TabP.Tu B P.d r ∧ TabP.R1 B (P.iSp i) r ∧ P.Sc B r s) ∨
      (∃ u', TabP.Tu B (P.ar i) u' ∧ ∃ r, TabP.Tu B P.d r ∧ P.Sc B u' u ∧ TabP.R2 B (P.iBp i) u' r ∧ P.Sc B r s)) ↔
    num B.n s = offE B.n P.ar i + 1 + num B.n u := by
  constructor
  · intro h
    match h with
    | .inl ⟨hm, r, hr, hsr, hsc⟩ =>
      rw [(P.sc_iff hN hn (hr.1.trans hs.1.symm) hr.2 hs.2).1 hsc, (hsp r hr).1 hsr, (P.mn_iff hN hn hu.2).1 hm]
    | .inr ⟨u', hu', r, hr, hsc1, hb, hsc2⟩ =>
      rw [(P.sc_iff hN hn (hr.1.trans hs.1.symm) hr.2 hs.2).1 hsc2, (hbp u' hu' r hr).1 hb,
        (P.sc_iff hN hn (hu'.1.trans hu.1.symm) hu'.2 hu.2).1 hsc1]
      rfl
  · intro h
    have hs' := num_lt B.n s hs.2
    rw [hs.1, h] at hs'
    have hu' := num_lt B.n u hu.2
    rw [hu.1] at hu'
    match hk : num B.n u with
    | 0 =>
      have hlt : offE B.n P.ar i < B.n ^ P.d := by
        rw [hk] at hs'; exact Nat.lt_of_lt_of_le (Nat.lt_succ_self _) (Nat.le_of_lt hs')
      match tup_of (B := B) hn P.d _ hlt with
      | ⟨r, hr, hrv⟩ =>
      exact .inl ⟨(P.mn_iff hN hn hu.2).2 hk, r, hr, (hsp r hr).2 hrv,
        (P.sc_iff hN hn (hr.1.trans hs.1.symm) hr.2 hs.2).2 (by rw [h, hrv, hk])⟩
    | k + 1 =>
      rw [hk] at hs' hu' h
      have hk' : k < B.n ^ P.ar i := Nat.lt_of_succ_lt hu'
      have hr' : offE B.n P.ar i + 1 + k < B.n ^ P.d := Nat.lt_of_lt_of_le (Nat.lt_succ_self _) (Nat.le_of_lt hs')
      match tup_of (B := B) hn (P.ar i) k hk', tup_of (B := B) hn P.d _ hr' with
      | ⟨u', hu'', hu'v⟩, ⟨r, hr, hrv⟩ =>
      exact .inr ⟨u', hu'', r, hr, (P.sc_iff hN hn (hu''.1.trans hu.1.symm) hu''.2 hu.2).2 (by rw [hk, hu'v]),
        (hbp u' hu'' r hr).2 (by rw [hrv, hu'v]), (P.sc_iff hN hn (hr.1.trans hs.1.symm) hr.2 hs.2).2 (by rw [h, hrv]; rfl)⟩

include hN hn in
/-- numeric layout relations satisfy the layout constraints -/
theorem num_lay (hfit : B.n < B.n ^ P.d) (hsp : ∀ i, i < P.m → P.SpOK B i) (hbp : ∀ i, i < P.m → P.BpOK B i)
    (h0 : 0 < P.m) : P.CLay B :=
  ⟨fun s hs => (hsp 0 h0 s hs).trans (P.sp0_num hN hn hfit s hs).symm,
   fun i hi u hu s hs => (hbp i hi u hu s hs).trans (P.bp_rhs hN hn i (hsp i hi) (hbp i hi) u hu s hs).symm,
   fun i hi s hs => (hsp (i + 1) hi s hs).trans (P.spS_num hN hn i (hbp i (Nat.lt_of_succ_lt hi)) s hs).symm⟩

end TabP

end Fg

end Found

#print axioms Found.Fg.TabP.sp0_ok
#print axioms Found.Fg.TabP.bp_ok
#print axioms Found.Fg.TabP.sp_succ_ok
#print axioms Found.Fg.TabP.lay_ok
#print axioms Found.Fg.TabP.bp_rhs
#print axioms Found.Fg.TabP.num_lay

found/Fg18.lean

lean_all/found/Fg18.lean

/-
  T185 (Fagin: NP = ESO), part 18. found/Fg18 — the regions of the
  encoding: the block offsets increase; every position is in the unary
  block, at a separator, at a bit of some block, or past the end; and these
  regions do not overlap.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Fg17

namespace Found

namespace Fg

open Gt

variable (n : Nat) (ar : Nat → Nat)

theorem le_split' {a b : Nat} (h : a ≤ b) : ∃ j, b = a + j := by
  induction h with
  | refl => exact ⟨0, rfl⟩
  | step _ ih => match ih with
    | ⟨j, e⟩ => exact ⟨j + 1, by rw [e]; rfl⟩

theorem offE_ge (i : Nat) : n ≤ offE n ar i := by
  induction i with
  | zero => exact Nat.le_refl n
  | succ i ih => exact Nat.le_trans ih (Nat.le_trans (Nat.le_add_right _ 1) (Nat.le_add_right _ _))

theorem offE_succ_le {i j : Nat} (h : i < j) : offE n ar (i + 1) ≤ offE n ar j := by
  induction h with
  | refl => exact Nat.le_refl _
  | step _ ih => exact Nat.le_trans ih (Nat.le_trans (Nat.le_add_right _ 1) (Nat.le_add_right _ _))

theorem bit_lt_next {i r : Nat} (hr : r < n ^ ar i) : offE n ar i + 1 + r < offE n ar (i + 1) :=
  Nat.add_lt_add_left hr _

theorem classify (p : Nat) : (m : Nat) → p < n ∨ (∃ i, i < m ∧ p = offE n ar i) ∨
    (∃ i, i < m ∧ ∃ r, r < n ^ ar i ∧ p = offE n ar i + 1 + r) ∨ offE n ar m ≤ p
  | 0 => match Nat.lt_or_ge p n with
    | .inl h => .inl h
    | .inr h => .inr (.inr (.inr h))
  | m + 1 => match classify p m with
    | .inl h => .inl h
    | .inr (.inl ⟨i, hi, e⟩) => .inr (.inl ⟨i, Nat.lt_succ_of_lt hi, e⟩)
    | .inr (.inr (.inl ⟨i, hi, r, hr, e⟩)) => .inr (.inr (.inl ⟨i, Nat.lt_succ_of_lt hi, r, hr, e⟩))
    | .inr (.inr (.inr h)) => match Nat.decEq p (offE n ar m) with
      | isTrue e => .inr (.inl ⟨m, Nat.lt_succ_self m, e⟩)
      | isFalse ne =>
        have h' : offE n ar m + 1 ≤ p := Nat.lt_of_le_of_ne h (fun e => ne e.symm)
        match le_split' h' with
        | ⟨r, e⟩ => match Nat.lt_or_ge r (n ^ ar m) with
          | .inl hr => .inr (.inr (.inl ⟨m, Nat.lt_succ_self m, r, hr, e⟩))
          | .inr hr => .inr (.inr (.inr (by rw [e]; exact Nat.add_le_add_left hr _)))

theorem offE_lt {i j : Nat} (h : i < j) : offE n ar i < offE n ar j :=
  Nat.lt_of_lt_of_le (Nat.lt_of_lt_of_le (Nat.lt_succ_self _) (Nat.le_add_right _ _)) (offE_succ_le n ar h)

theorem offE_mono {i j : Nat} (h : i ≤ j) : offE n ar i ≤ offE n ar j :=
  match Nat.eq_or_lt_of_le h with
  | .inl e => by rw [e]; exact Nat.le_refl _
  | .inr hl => Nat.le_of_lt (offE_lt n ar hl)

theorem offE_inj {i j : Nat} (h : offE n ar i = offE n ar j) : i = j :=
  match Nat.lt_or_ge i j with
  | .inl hl => absurd h (Nat.ne_of_lt (offE_lt n ar hl))
  | .inr hg => match Nat.eq_or_lt_of_le hg with
    | .inl e => e.symm
    | .inr hl => absurd h.symm (Nat.ne_of_lt (offE_lt n ar hl))

theorem sep_ne_bit {i j r : Nat} (hr : r < n ^ ar i) : offE n ar j ≠ offE n ar i + 1 + r := fun e =>
  match Nat.lt_or_ge i j with
  | .inl hl => absurd e (Nat.ne_of_gt (Nat.lt_of_lt_of_le (bit_lt_next n ar hr) (offE_succ_le n ar hl)))
  | .inr hg => absurd e (Nat.ne_of_lt (Nat.lt_of_le_of_lt (offE_mono n ar hg)
      (Nat.lt_of_lt_of_le (Nat.lt_succ_self _) (Nat.le_add_right _ _))))

theorem bit_inj {i j r r' : Nat} (hr : r < n ^ ar i) (hr' : r' < n ^ ar j)
    (e : offE n ar i + 1 + r = offE n ar j + 1 + r') : i = j ∧ r = r' := by
  have hij : i = j := match Nat.lt_or_ge i j with
    | .inl hl => absurd e (Nat.ne_of_lt (Nat.lt_of_lt_of_le (bit_lt_next n ar hr)
        (Nat.le_trans (offE_succ_le n ar hl) (Nat.le_trans (Nat.le_add_right _ 1) (Nat.le_add_right _ _)))))
    | .inr hg => match Nat.eq_or_lt_of_le hg with
      | .inl e' => e'.symm
      | .inr hl => absurd e.symm (Nat.ne_of_lt (Nat.lt_of_lt_of_le (bit_lt_next n ar hr')
          (Nat.le_trans (offE_succ_le n ar hl) (Nat.le_trans (Nat.le_add_right _ 1) (Nat.le_add_right _ _)))))
  subst hij
  exact ⟨rfl, addLeftCancel _ e⟩

theorem region_lt_end {m i : Nat} (hi : i < m) : offE n ar (i + 1) ≤ offE n ar m := offE_mono n ar hi

end Fg

end Found

#print axioms Found.Fg.classify
#print axioms Found.Fg.offE_inj
#print axioms Found.Fg.sep_ne_bit
#print axioms Found.Fg.bit_inj
#print axioms Found.Fg.region_lt_end

found/Fg19.lean

lean_all/found/Fg19.lean

/-
  T185 (Fagin: NP = ESO), part 19. found/Fg19 — the initial symbols: under
  the natural order and the numeric layout, the symbol prescribed at
  position s by the tableau sentence is the symbol of the encoding at
  position num s.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Fg18

namespace Found

namespace Fg

open Gt

theorem appG_nil {X : Type} : (l : List X) → appG l [] = l
  | [] => rfl
  | x :: l => by show x :: appG l [] = x :: l; rw [appG_nil l]

theorem bitOf_iff (a : Nat) : (b : Bool) → ((a = 2 ∧ b = true) ∨ (a = 1 ∧ ¬ b = true) ↔ bitOf b = a)
  | true => ⟨fun h => h.elim (fun ⟨e, _⟩ => e.symm) (fun ⟨_, h⟩ => (h rfl).elim), fun h => .inl ⟨h.symm, rfl⟩⟩
  | false => ⟨fun h => h.elim (fun ⟨_, h⟩ => Bool.noConfusion h) (fun ⟨e, _⟩ => e.symm),
      fun h => .inr ⟨h.symm, fun h' => Bool.noConfusion h'⟩⟩

namespace TabP

variable (P : TabP) (A : FStr) {B : FStr} (hN : ∀ x y, x < B.n → y < B.n → (ltR P.iLT B x y ↔ x < y))
  (hn : 1 ≤ B.n) (hBn : B.n = A.n) (hbase : ∀ i, i < P.m → ∀ l, B.rel i l = A.rel i l)
  (hsp : ∀ i, i < P.m → P.SpOK B i) (hbp : ∀ i, i < P.m → P.BpOK B i)

include hN hn hBn hbase hsp hbp in
theorem initSym_iff (s : List Nat) (hs : TabP.Tu B P.d s) (a : Nat) :
    P.InitSym B a s ↔ wAt (enc P.m P.ar A) (num B.n s) = a := by
  have hun := P.unary_iff hN hn hs
  have sp := fun i hi => hsp i hi s hs
  have bp := fun i hi u hu => hbp i hi u hu s hs
  have hBn' : offE B.n P.ar = offE A.n P.ar := by rw [hBn]
  have notU : ∀ k, B.n ≤ k → num B.n s = k → ¬ P.Unary B s := fun k hk e hU =>
    Nat.lt_irrefl _ (Nat.lt_of_lt_of_le (hun.1 hU) (e ▸ hk))
  rcases classify B.n P.ar (num B.n s) P.m with h | ⟨i, hi, e⟩ | ⟨i, hi, r, hr, e⟩ | h
  · -- unary block
    have hw : wAt (enc P.m P.ar A) (num B.n s) = 3 :=
      wAt_enc_unary P.m P.ar A (Nat.lt_of_lt_of_le h (Nat.le_of_eq hBn))
    rw [hw]
    refine ⟨fun hI => ?_, fun ha => .inl ⟨ha.symm, hun.2 h⟩⟩
    rcases hI with ⟨ha, _⟩ | ⟨_, j, hj, hS⟩ | ⟨j, hj, u, hu, hB, _⟩ | ⟨_, hnu, _⟩
    · exact ha.symm
    · exact absurd (sp j hj |>.1 hS) (fun e' => Nat.lt_irrefl _ (Nat.lt_of_lt_of_le h (e' ▸ offE_ge B.n P.ar j)))
    · exact absurd (bp j hj u hu |>.1 hB) (fun e' => Nat.lt_irrefl _ (Nat.lt_of_lt_of_le h
        (e' ▸ Nat.le_trans (offE_ge B.n P.ar j) (Nat.le_trans (Nat.le_add_right _ 1) (Nat.le_add_right _ _)))))
    · exact (hnu (hun.2 h)).elim
  · -- separator of block i
    have hw : wAt (enc P.m P.ar A) (num B.n s) = 4 := by rw [e, hBn']; exact wAt_enc_sep P.m P.ar A hi
    rw [hw]
    refine ⟨fun hI => ?_, fun ha => .inr (.inl ⟨ha.symm, i, hi, (sp i hi).2 e⟩)⟩
    rcases hI with ⟨_, hU⟩ | ⟨ha, _⟩ | ⟨j, hj, u, hu, hB, _⟩ | ⟨_, _, hns, _⟩
    · exact (notU _ (offE_ge B.n P.ar i) e hU).elim
    · exact ha.symm
    · have q := (bp j hj u hu).1 hB
      rw [e] at q
      have hu' := num_lt B.n u hu.2
      rw [hu.1] at hu'
      exact (sep_ne_bit B.n P.ar hu' q).elim
    · exact (hns i hi ((sp i hi).2 e)).elim
  · -- bit r of block i
    have hw : wAt (enc P.m P.ar A) (num B.n s) = bitOf (A.rel i (iterL A.n (P.ar i) r)) := by
      rw [e, hBn']; exact wAt_enc_bit P.m P.ar A hi (hBn ▸ hr)
    rw [hw]
    have hr' : r < B.n ^ P.ar i := hr
    match tup_of (B := B) hn (P.ar i) r hr' with
    | ⟨u0, hu0, hu0v⟩ =>
    have hu0e : u0 = iterL A.n (P.ar i) r := by
      have it := iter_spec B.n hn (P.ar i) r hr'
      rw [← hBn]
      exact num_inj B.n u0 _ (hu0.1.trans it.2.2.symm) hu0.2 it.1 (hu0v.trans it.2.1.symm)
    have hB0 : TabP.R2 B (P.iBp i) u0 s := (bp i hi u0 hu0).2 (by rw [e, hu0v])
    have hR : TabP.R1 B i u0 ↔ A.rel i (iterL A.n (P.ar i) r) = true := by
      show B.rel i (appG u0 []) = true ↔ _
      rw [appG_nil, hbase i hi, hu0e]
    refine ⟨fun hI => ?_, fun ha => .inr (.inr (.inl ⟨i, hi, u0, hu0, hB0, ?_⟩))⟩
    · rcases hI with ⟨_, hU⟩ | ⟨_, j, hj, hS⟩ | ⟨j, hj, u, hu, hB, hbits⟩ | ⟨_, _, _, hnb⟩
      · exact (notU _ (Nat.le_trans (offE_ge B.n P.ar i) (Nat.le_trans (Nat.le_add_right _ 1)
          (Nat.le_add_right _ _))) e hU).elim
      · have q := (sp j hj).1 hS
        rw [e] at q
        exact (sep_ne_bit B.n P.ar hr' q.symm).elim
      · have q := (bp j hj u hu).1 hB
        rw [e] at q
        have hu' := num_lt B.n u hu.2
        rw [hu.1] at hu'
        have bi := bit_inj B.n P.ar hr' hu' q
        have hij := bi.1
        subst hij
        have hue : u = u0 := num_inj B.n u u0 (hu.1.trans hu0.1.symm) hu.2 hu0.2 (bi.2.symm.trans hu0v.symm)
        subst hue
        exact (bitOf_iff a _).1 (hbits.elim (fun ⟨x, y⟩ => .inl ⟨x, hR.1 y⟩) (fun ⟨x, y⟩ => .inr ⟨x, fun z => y (hR.2 z)⟩))
      · exact (hnb i hi ⟨u0, hu0, hB0⟩).elim
    · exact (bitOf_iff a _).2 ha |> fun h => h.elim (fun ⟨x, y⟩ => .inl ⟨x, hR.2 y⟩) (fun ⟨x, y⟩ => .inr ⟨x, fun z => y (hR.1 z)⟩)
  · -- past the end
    have hw : wAt (enc P.m P.ar A) (num B.n s) = 0 := by rw [hBn'] at h; exact wAt_enc_end P.m P.ar A h
    rw [hw]
    have hge : B.n ≤ num B.n s := Nat.le_trans (offE_ge B.n P.ar P.m) h
    have nsp : ∀ j, j < P.m → ¬ TabP.R1 B (P.iSp j) s := fun j hj hS => by
      have q := (sp j hj).1 hS
      exact Nat.lt_irrefl _ (Nat.lt_of_lt_of_le (q ▸ offE_lt B.n P.ar hj) h)
    have nbp : ∀ j, j < P.m → ¬ ∃ u, TabP.Tu B (P.ar j) u ∧ TabP.R2 B (P.iBp j) u s := fun j hj ⟨u, hu, hB⟩ => by
      have q := (bp j hj u hu).1 hB
      have hu' := num_lt B.n u hu.2
      rw [hu.1] at hu'
      exact Nat.lt_irrefl _ (Nat.lt_of_lt_of_le (q ▸ Nat.lt_of_lt_of_le (bit_lt_next B.n P.ar hu')
        (region_lt_end B.n P.ar hj)) h)
    refine ⟨fun hI => ?_, fun ha => .inr (.inr (.inr ⟨ha.symm, notU _ hge rfl, nsp, nbp⟩))⟩
    rcases hI with ⟨_, hU⟩ | ⟨_, j, hj, hS⟩ | ⟨j, hj, u, hu, hB, _⟩ | ⟨ha, _⟩
    · exact (notU _ hge rfl hU).elim
    · exact (nsp j hj hS).elim
    · exact (nbp j hj ⟨u, hu, hB⟩).elim
    · exact ha.symm

end TabP

end Fg

end Found

#print axioms Found.Fg.TabP.initSym_iff

found/Fg2.lean

lean_all/found/Fg2.lean

/-
  T185 (Fagin: NP = ESO), part 2. found/Fg2 — nondeterministic Turing
  machines with a one-way infinite tape (Sipser's convention: moving left
  at cell 0 stays at cell 0): symbols are natural numbers (0 = blank), the
  transition relation is a finite list of moves for each (state, symbol);
  configurations (state, tape as a function, head), steps stated
  relationally (the new tape agrees pointwise with the updated tape),
  runs with step counts; polynomial bounds.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Fg1

namespace Found

namespace Fg

structure NTM where
  δ : Nat → Nat → List (Nat × Nat × Bool)
  q0 : Nat
  acc : Nat

structure Cfg where
  q : Nat
  tape : Nat → Nat
  h : Nat

/-- the head position after a move right (true) or left (false) -/
def newH (h : Nat) : Bool → Nat
  | true => h + 1
  | false => match h with
    | 0 => 0
    | k + 1 => k

/-- the tape after writing w at the head -/
def wrt (tape : Nat → Nat) (h w : Nat) (i : Nat) : Nat :=
  match Nat.decEq i h with
  | isTrue _ => w
  | isFalse _ => tape i

/-- one step: some move (q', w, d) of (state, scanned symbol) -/
def Step (M : NTM) (c c' : Cfg) : Prop :=
  ∃ q' w d, (q', w, d) ∈ M.δ c.q (c.tape c.h) ∧ c'.q = q' ∧ (∀ i, c'.tape i = wrt c.tape c.h w i) ∧ c'.h = newH c.h d

inductive Steps (M : NTM) : Nat → Cfg → Cfg → Prop
  | zero (c : Cfg) : Steps M 0 c c
  | succ {t : Nat} {c c' c'' : Cfg} : Step M c c' → Steps M t c' c'' → Steps M (t + 1) c c''

theorem steps_trans {M : NTM} {s t : Nat} {c c' c'' : Cfg} (h1 : Steps M s c c') (h2 : Steps M t c' c'') :
    Steps M (s + t) c c'' := by
  induction h1 with
  | zero => rw [Nat.zero_add]; exact h2
  | succ st _ ih => rw [Nat.succ_add]; exact .succ st (ih h2)

/-- the symbol at position i of a word (blank beyond) -/
def wAt : List Nat → Nat → Nat
  | [], _ => 0
  | x :: _, 0 => x
  | _ :: l, i + 1 => wAt l i

def initC (M : NTM) (w : List Nat) : Cfg := ⟨M.q0, wAt w, 0⟩

/-- a polynomial bound c·(len + 1)^k -/
def pbound (c k len : Nat) : Nat := c * (len + 1) ^ k

end Fg

end Found

#print axioms Found.Fg.steps_trans

found/Fg20.lean

lean_all/found/Fg20.lean

/-
  T185 (Fagin: NP = ESO), part 20. found/Fg20 — reading machine rows off a
  model of the tableau sentence (natural order): at time k (the k-th
  d-tuple) the state is the unique q with St_q, the head is the value of
  the unique head cell, and the symbol at cell i is the unique a with Tb_a;
  row 0 is the initial configuration on the encoding.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Fg19

namespace Found

namespace Fg

open Gt

/-- Steps only depend on the state, head and tape values -/
def CfgEq (c c' : Cfg) : Prop := c.q = c'.q ∧ c.h = c'.h ∧ ∀ i, c.tape i = c'.tape i

theorem step_resp {M : NTM} {c c' d : Cfg} (e : CfgEq c c') (h : Step M c d) : Step M c' d :=
  match h with
  | ⟨q', w, dd, hm, h1, h2, h3⟩ => ⟨q', w, dd, by rw [← e.1, ← e.2.1, ← e.2.2]; exact hm, h1,
      fun i => by rw [h2 i]; unfold wrt; rw [e.2.1]; match Nat.decEq i c'.h with
        | isTrue _ => rfl
        | isFalse _ => exact e.2.2 i, by rw [h3, e.2.1]⟩

theorem steps_resp {M : NTM} {s : Nat} {c c' d : Cfg} (e : CfgEq c c') (h : Steps M s c d) :
    ∃ d', Steps M s c' d' ∧ d'.q = d.q := by
  cases h with
  | zero => exact ⟨c', .zero c', e.1.symm⟩
  | succ st rest => exact ⟨d, .succ (step_resp e st) rest, rfl⟩

/-- choose an x with Q x when p holds -/
def chooseIf (Kc : CP.Classical') {X : Type} (p : Prop) (Q : X → Prop) (h : p → ∃ x, Q x) (x0 : X) : X :=
  Kc.ch (fun x => p → Q x) (match Kc.lem p with
    | .inl hp => match h hp with | ⟨x, hx⟩ => ⟨x, fun _ => hx⟩
    | .inr hn => ⟨x0, fun hp => absurd hp hn⟩)

theorem chooseIf_spec (Kc : CP.Classical') {X : Type} {p : Prop} {Q : X → Prop} (h : p → ∃ x, Q x) (x0 : X)
    (hp : p) : Q (chooseIf Kc p Q h x0) :=
  Kc.ch_spec (fun x => p → Q x) _ hp

namespace TabP

variable (P : TabP) (Kc : CP.Classical') {B : FStr} (hn : 1 ≤ B.n) (hU : P.CUniq B)

/-- the k-th d-tuple -/
def tk (B : FStr) (k : Nat) : List Nat := iterL B.n P.d k

theorem tk_spec (hn : 1 ≤ B.n) {k : Nat} (hk : k < B.n ^ P.d) : TabP.Tu B P.d (P.tk B k) ∧ num B.n (P.tk B k) = k :=
  match iter_spec B.n hn P.d k hk with
  | ⟨h1, h2, h3⟩ => ⟨⟨h3, h1⟩, h2⟩

/-- the state at time k -/
def rowQ (k : Nat) : Nat :=
  chooseIf Kc (k < B.n ^ P.d) (fun q => q < P.Qn ∧ TabP.R1 B (P.iSt q) (P.tk B k))
    (fun hk => match (hU _ (P.tk_spec hn hk).1).1 with | ⟨q, hq, h⟩ => ⟨q, hq, h⟩) 0

/-- the head cell at time k -/
def rowHT (k : Nat) : List Nat :=
  chooseIf Kc (k < B.n ^ P.d) (fun h => TabP.Tu B P.d h ∧ TabP.R2 B P.iHd (P.tk B k) h)
    (fun hk => (hU _ (P.tk_spec hn hk).1).2.2.1) []

/-- the symbol at cell i, time k -/
def rowA (k i : Nat) : Nat :=
  if hi : i < B.n ^ P.d then
    chooseIf Kc (k < B.n ^ P.d) (fun a => a < P.G ∧ TabP.R2 B (P.iTb a) (P.tk B k) (P.tk B i))
      (fun hk => (hU _ (P.tk_spec hn hk).1).2.2.2.2 _ (P.tk_spec hn hi).1 |>.1) 0
  else 0

/-- the machine row at time k -/
def row (k : Nat) : Cfg := ⟨P.rowQ Kc hn hU k, P.rowA Kc hn hU k, num B.n (P.rowHT Kc hn hU k)⟩

theorem rowQ_spec {k : Nat} (hk : k < B.n ^ P.d) :
    P.rowQ Kc hn hU k < P.Qn ∧ TabP.R1 B (P.iSt (P.rowQ Kc hn hU k)) (P.tk B k) :=
  chooseIf_spec Kc (Q := fun q => q < P.Qn ∧ TabP.R1 B (P.iSt q) (P.tk B k)) _ 0 hk

theorem rowQ_uniq {k q : Nat} (hk : k < B.n ^ P.d) (hq : q < P.Qn) (h : TabP.R1 B (P.iSt q) (P.tk B k)) :
    q = P.rowQ Kc hn hU k :=
  (hU _ (P.tk_spec hn hk).1).2.1 q hq _ (P.rowQ_spec Kc hn hU hk).1 h (P.rowQ_spec Kc hn hU hk).2

theorem rowHT_spec {k : Nat} (hk : k < B.n ^ P.d) :
    TabP.Tu B P.d (P.rowHT Kc hn hU k) ∧ TabP.R2 B P.iHd (P.tk B k) (P.rowHT Kc hn hU k) :=
  chooseIf_spec Kc (Q := fun h => TabP.Tu B P.d h ∧ TabP.R2 B P.iHd (P.tk B k) h) _ [] hk

theorem rowHT_uniq {k : Nat} {h : List Nat} (hk : k < B.n ^ P.d) (hh : TabP.Tu B P.d h)
    (hr : TabP.R2 B P.iHd (P.tk B k) h) : h = P.rowHT Kc hn hU k :=
  (eqt_iff (B := B) (hh.1.trans (P.rowHT_spec Kc hn hU hk).1.1.symm)).1
    ((hU _ (P.tk_spec hn hk).1).2.2.2.1 h hh _ (P.rowHT_spec Kc hn hU hk).1 hr (P.rowHT_spec Kc hn hU hk).2)

theorem rowA_spec {k i : Nat} (hk : k < B.n ^ P.d) (hi : i < B.n ^ P.d) :
    P.rowA Kc hn hU k i < P.G ∧ TabP.R2 B (P.iTb (P.rowA Kc hn hU k i)) (P.tk B k) (P.tk B i) := by
  unfold rowA
  rw [dite_eq_left hi]
  exact chooseIf_spec Kc (Q := fun a => a < P.G ∧ TabP.R2 B (P.iTb a) (P.tk B k) (P.tk B i)) _ 0 hk

theorem rowA_beyond {k i : Nat} (hi : ¬ i < B.n ^ P.d) : P.rowA Kc hn hU k i = 0 := by
  unfold rowA
  rw [dite_eq_right hi]

theorem rowA_uniq {k i a : Nat} (hk : k < B.n ^ P.d) (hi : i < B.n ^ P.d) (ha : a < P.G)
    (h : TabP.R2 B (P.iTb a) (P.tk B k) (P.tk B i)) : a = P.rowA Kc hn hU k i :=
  (hU _ (P.tk_spec hn hk).1).2.2.2.2 _ (P.tk_spec hn hi).1 |>.2 a ha _ (P.rowA_spec Kc hn hU hk hi).1 h
    (P.rowA_spec Kc hn hU hk hi).2

end TabP

end Fg

end Found

#print axioms Found.Fg.steps_resp
#print axioms Found.Fg.TabP.row
#print axioms Found.Fg.TabP.rowA_uniq
#print axioms Found.Fg.TabP.rowHT_uniq

found/Fg21.lean

lean_all/found/Fg21.lean

/-
  T185 (Fagin: NP = ESO), part 21. found/Fg21 — row 0 of the machine rows
  read off a model of the tableau sentence (natural order, numeric layout)
  is the initial configuration of the machine on the encoding.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Fg20

namespace Found

namespace Fg

open Gt

namespace TabP

variable (P : TabP) (A : FStr) (Kc : CP.Classical') {B : FStr}
  (hN : ∀ x y, x < B.n → y < B.n → (ltR P.iLT B x y ↔ x < y))
  (hn : 1 ≤ B.n) (hBn : B.n = A.n) (hbase : ∀ i, i < P.m → ∀ l, B.rel i l = A.rel i l)
  (hsp : ∀ i, i < P.m → P.SpOK B i) (hbp : ∀ i, i < P.m → P.BpOK B i) (hU : P.CUniq B)

/-- initial symbols are at most 4 -/
theorem initSym_le {a : Nat} {s : List Nat} (h : P.InitSym B a s) : a ≤ 4 :=
  match h with
  | .inl ⟨e, _⟩ => e ▸ Nat.le.step Nat.le.refl
  | .inr (.inl ⟨e, _⟩) => e ▸ Nat.le.refl
  | .inr (.inr (.inl ⟨_, _, _, _, _, hb⟩)) => hb.elim
      (fun ⟨e, _⟩ => e ▸ Nat.le.step (Nat.le.step Nat.le.refl))
      (fun ⟨e, _⟩ => e ▸ Nat.le.step (Nat.le.step (Nat.le.step Nat.le.refl)))
  | .inr (.inr (.inr ⟨e, _⟩)) => e ▸ Nat.zero_le 4

include hN hn hBn hbase hsp hbp in
/-- row 0 is the initial configuration on the encoding -/
theorem row0 (hI : P.CInit B) (hq0 : P.M.q0 < P.Qn) (hG : 4 < P.G)
    (hlen : lenG (enc P.m P.ar A) ≤ B.n ^ P.d) :
    CfgEq (P.row Kc hn hU 0) (initC P.M (enc P.m P.ar A)) := by
  have hpos : 0 < B.n ^ P.d := pow_pos1 B.n hn P.d
  have hz := P.tk_spec hn hpos
  have ci := hI (P.tk B 0) hz.1 ((P.mn_iff hN hn hz.1.2).2 hz.2)
  refine ⟨(P.rowQ_uniq Kc hn hU hpos hq0 ci.1).symm, ?_, fun i => ?_⟩
  · show num B.n (P.rowHT Kc hn hU 0) = 0
    rw [← P.rowHT_uniq Kc hn hU hpos hz.1 ci.2.1]
    exact hz.2
  · show P.rowA Kc hn hU 0 i = wAt (enc P.m P.ar A) i
    match Nat.lt_or_ge i (B.n ^ P.d) with
    | .inl hi =>
      have hs := P.tk_spec hn hi
      have hI' : P.InitSym B (wAt (enc P.m P.ar A) i) (P.tk B i) :=
        (P.initSym_iff A hN hn hBn hbase hsp hbp (P.tk B i) hs.1 _).2 (by rw [hs.2])
      have ha : wAt (enc P.m P.ar A) i < P.G := Nat.lt_of_le_of_lt (P.initSym_le hI') hG
      exact (P.rowA_uniq Kc hn hU hpos hi ha ((ci.2.2 _ hs.1 _ ha).2 hI')).symm
    | .inr hi =>
      rw [P.rowA_beyond Kc hn hU (fun h => Nat.lt_irrefl _ (Nat.lt_of_lt_of_le h hi)),
        wAt_beyond _ i (Nat.le_trans hlen hi)]

end TabP

end Fg

end Found

#print axioms Found.Fg.TabP.initSym_le
#print axioms Found.Fg.TabP.row0

found/Fg3.lean

lean_all/found/Fg3.lean

/-
  T185 (Fagin: NP = ESO), part 3. found/Fg3 — the standard encoding of a
  finite structure as a word: n in unary (symbol 3), then for each relation
  symbol i < m a separator 4 followed by the bits (2 = true, 1 = false) of
  R_i on all ar(i)-tuples in lexicographic order. NP classes of finite
  structures (all runs polynomially bounded); ESO-definable classes.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Fg2

namespace Found

namespace Fg

open Gt

def appG {X : Type} : List X → List X → List X
  | [], l => l
  | x :: k, l => x :: appG k l

def flatMapG {X Y : Type} (f : X → List Y) : List X → List Y
  | [] => []
  | x :: l => appG (f x) (flatMapG f l)

def replG {X : Type} (x : X) : Nat → List X
  | 0 => []
  | n + 1 => x :: replG x n

/-- k, k+1, …, k+n-1 -/
def upFrom (k : Nat) : Nat → List Nat
  | 0 => []
  | n + 1 => k :: upFrom (k + 1) n

section Inc

variable (n : Nat)

def AllMaxL : List Nat → Prop
  | [] => True
  | x :: l => x + 1 = n ∧ AllMaxL l

def decAllMax : (l : List Nat) → Decidable (AllMaxL n l)
  | [] => isTrue trivial
  | x :: l => match Nat.decEq (x + 1) n, decAllMax l with
    | isTrue h1, isTrue h2 => isTrue ⟨h1, h2⟩
    | isFalse h1, _ => isFalse (fun h => h1 h.1)
    | _, isFalse h2 => isFalse (fun h => h2 h.2)

def incStep (a : Nat) (as : List Nat) (r : List Nat) : List Nat :=
  match decAllMax n as with
  | isTrue _ => (a + 1) :: replG 0 (lenG as)
  | isFalse _ => a :: r

/-- the lexicographic successor (wrapping all-max lists to zeros) -/
def incL : List Nat → List Nat
  | [] => []
  | a :: as => incStep n a as (incL as)

/-- the k-th d-digit list -/
def iterL (d : Nat) : Nat → List Nat
  | 0 => replG 0 d
  | k + 1 => incL n (iterL d k)

end Inc

/-- all a-tuples over {0..n-1}: the first n^a lexicographic successors of the zero tuple -/
def allTup (n a : Nat) : List (List Nat) := lmapG (iterL n a) (upFrom 0 (n ^ a))

def bitOf (b : Bool) : Nat := match b with
  | true => 2
  | false => 1

def relBits (A : FStr) (i a : Nat) : List Nat := lmapG (fun t => bitOf (A.rel i t)) (allTup A.n a)

def relsEnc (ar : Nat → Nat) (A : FStr) : Nat → List Nat
  | 0 => []
  | i + 1 => appG (relsEnc ar A i) (4 :: relBits A i (ar i))

/-- the encoding of a structure with m relations of arities ar -/
def enc (m : Nat) (ar : Nat → Nat) (A : FStr) : List Nat := appG (replG 3 A.n) (relsEnc ar A m)

/-- every run of M on w has at most t steps -/
def RunsBounded (M : NTM) (w : List Nat) (t : Nat) : Prop :=
  ∀ s c, Steps M s (initC M w) c → s ≤ t

/-- M accepts w -/
def Accepts (M : NTM) (w : List Nat) : Prop := ∃ s c, Steps M s (initC M w) c ∧ c.q = M.acc

/-- M is a finite machine: states < Qn, symbols < G -/
def FinM (M : NTM) (Qn G : Nat) : Prop :=
  M.q0 < Qn ∧ M.acc < Qn ∧ 4 < G ∧ (∀ q a, (Qn ≤ q ∨ G ≤ a) → M.δ q a = []) ∧
  ∀ q a q' w d, (q', w, d) ∈ M.δ q a → q' < Qn ∧ w < G

/-- NP classes of (nonempty) finite structures: a finite nondeterministic machine all of whose runs
    on the encoding have polynomially many steps accepts exactly the encodings of members -/
def InNP (m : Nat) (ar : Nat → Nat) (Kl : FStr → Prop) : Prop :=
  ∃ (M : NTM) (Qn G c k : Nat), FinM M Qn G ∧ ∀ A : FStr, 1 ≤ A.n →
    RunsBounded M (enc m ar A) (pbound c k (lenG (enc m ar A))) ∧ (Kl A ↔ Accepts M (enc m ar A))

/-- ESO-definable classes (φ uses relation symbols < m with arities ar, and new ones ≥ m) -/
def ESODef (m : Nat) (Kl : FStr → Prop) : Prop :=
  ∃ φ : FFm, ∀ A : FStr, 1 ≤ A.n → (Kl A ↔ ESO m φ A)

end Fg

end Found

#print axioms Found.Fg.enc
#print axioms Found.Fg.InNP

found/Fg4.lean

lean_all/found/Fg4.lean

/-
  T185 (Fagin: NP = ESO), part 4. found/Fg4 — satisfaction of first-order
  formulas is invariant under isomorphisms of finite structures (a
  bijection of universes respecting all relations on tuples from the
  universe).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Fg3

namespace Found

namespace Fg

open Gt

/-- an isomorphism respecting all relations -/
structure FIsoA (A B : FStr) where
  f : Nat → Nat
  g : Nat → Nat
  f_lt : ∀ x, x < A.n → f x < B.n
  g_lt : ∀ y, y < B.n → g y < A.n
  gf : ∀ x, x < A.n → g (f x) = x
  fg : ∀ y, y < B.n → f (g y) = y
  rel_pres : ∀ i args, AllLtG A.n args → B.rel i (lmapG f args) = A.rel i args

theorem lmap_comp_eq {X Y Z : Type} (f : X → Y) (g : Y → Z) (h : X → Z) (e : ∀ x, g (f x) = h x) :
    (l : List X) → lmapG g (lmapG f l) = lmapG h l
  | [] => rfl
  | x :: l => by
    show g (f x) :: lmapG g (lmapG f l) = h x :: lmapG h l
    rw [e x, lmap_comp_eq f g h e l]

theorem allLt_lmap {n : Nat} (v : Nat → Nat) (hv : ∀ k, v k < n) : (l : List Nat) → AllLtG n (lmapG v l)
  | [] => trivial
  | k :: l => ⟨hv k, allLt_lmap v hv l⟩

/-- satisfaction depends only on the values of the valuation -/
theorem sat_congr (A : FStr) : (φ : FFm) → ∀ {v w : Nat → Nat}, (∀ k, v k = w k) → (Sat A v φ ↔ Sat A w φ)
  | .rel i args, v, w, h => by
    show A.rel i (lmapG v args) = true ↔ A.rel i (lmapG w args) = true
    rw [lmapG_congr args (fun k _ => h k)]
  | .eq a b, v, w, h => by
    show v a = v b ↔ w a = w b
    rw [h a, h b]
  | .neg φ, _, _, h => ⟨fun x y => x ((sat_congr A φ h).2 y), fun x y => x ((sat_congr A φ h).1 y)⟩
  | .conj φ ψ, _, _, h => ⟨fun ⟨x, y⟩ => ⟨(sat_congr A φ h).1 x, (sat_congr A ψ h).1 y⟩,
      fun ⟨x, y⟩ => ⟨(sat_congr A φ h).2 x, (sat_congr A ψ h).2 y⟩⟩
  | .disj φ ψ, _, _, h => ⟨fun x => x.elim (fun y => .inl ((sat_congr A φ h).1 y)) (fun y => .inr ((sat_congr A ψ h).1 y)),
      fun x => x.elim (fun y => .inl ((sat_congr A φ h).2 y)) (fun y => .inr ((sat_congr A ψ h).2 y))⟩
  | .ex φ, v, w, h =>
    have hc : ∀ x k, consV v x k = consV w x k := fun x k => match k with
      | 0 => rfl
      | k + 1 => h k
    ⟨fun ⟨x, hx, y⟩ => ⟨x, hx, (sat_congr A φ (hc x)).1 y⟩, fun ⟨x, hx, y⟩ => ⟨x, hx, (sat_congr A φ (hc x)).2 y⟩⟩
  | .all φ, v, w, h =>
    have hc : ∀ x k, consV v x k = consV w x k := fun x k => match k with
      | 0 => rfl
      | k + 1 => h k
    ⟨fun y x hx => (sat_congr A φ (hc x)).1 (y x hx), fun y x hx => (sat_congr A φ (hc x)).2 (y x hx)⟩

/-- isomorphisms preserve satisfaction (valuations into the universe) -/
theorem sat_iso {A B : FStr} (φi : FIsoA A B) : (φ : FFm) → ∀ (v : Nat → Nat), (∀ k, v k < A.n) →
    (Sat A v φ ↔ Sat B (fun k => φi.f (v k)) φ)
  | .rel i args, v, hv => by
    show A.rel i (lmapG v args) = true ↔ B.rel i (lmapG (fun k => φi.f (v k)) args) = true
    rw [← lmap_comp_eq v φi.f _ (fun _ => rfl) args, φi.rel_pres i _ (allLt_lmap v hv args)]
  | .eq a b, v, hv => ⟨fun h => by show φi.f (v a) = φi.f (v b); rw [h],
      fun h => by
        have e : φi.f (v a) = φi.f (v b) := h
        have h' : φi.g (φi.f (v a)) = φi.g (φi.f (v b)) := by rw [e]
        rw [φi.gf _ (hv a), φi.gf _ (hv b)] at h'
        exact h'⟩
  | .neg φ, v, hv => ⟨fun h h' => h ((sat_iso φi φ v hv).2 h'), fun h h' => h ((sat_iso φi φ v hv).1 h')⟩
  | .conj φ ψ, v, hv => ⟨fun ⟨h1, h2⟩ => ⟨(sat_iso φi φ v hv).1 h1, (sat_iso φi ψ v hv).1 h2⟩,
      fun ⟨h1, h2⟩ => ⟨(sat_iso φi φ v hv).2 h1, (sat_iso φi ψ v hv).2 h2⟩⟩
  | .disj φ ψ, v, hv => ⟨fun h => h.elim (fun y => .inl ((sat_iso φi φ v hv).1 y)) (fun y => .inr ((sat_iso φi ψ v hv).1 y)),
      fun h => h.elim (fun y => .inl ((sat_iso φi φ v hv).2 y)) (fun y => .inr ((sat_iso φi ψ v hv).2 y))⟩
  | .all φ, v, hv => ⟨fun h y hy => (sat_congr B φ (v := fun k => φi.f (consV v (φi.g y) k))
        (w := consV (fun k => φi.f (v k)) y) (fun k => match k with
          | 0 => φi.fg y hy
          | _ + 1 => rfl)).1 ((sat_iso φi φ (consV v (φi.g y)) (fun k => match k with
            | 0 => φi.g_lt y hy
            | k + 1 => hv k)).1 (h (φi.g y) (φi.g_lt y hy))),
      fun h x hx => (sat_iso φi φ (consV v x) (fun k => match k with
          | 0 => hx
          | k + 1 => hv k)).2 ((sat_congr B φ (v := consV (fun k => φi.f (v k)) (φi.f x))
            (w := fun k => φi.f (consV v x k)) (fun k => match k with
              | 0 => rfl
              | _ + 1 => rfl)).1 (h (φi.f x) (φi.f_lt x hx)))⟩
  | .ex φ, v, hv => ⟨fun ⟨x, hx, h⟩ => ⟨φi.f x, φi.f_lt x hx,
        (sat_congr B φ (v := fun k => φi.f (consV v x k)) (w := consV (fun k => φi.f (v k)) (φi.f x))
          (fun k => match k with
          | 0 => rfl
          | _ + 1 => rfl)).1 ((sat_iso φi φ (consV v x) (fun k => match k with
            | 0 => hx
            | k + 1 => hv k)).1 h)⟩,
      fun ⟨y, hy, h⟩ => ⟨φi.g y, φi.g_lt y hy, (sat_iso φi φ (consV v (φi.g y)) (fun k => match k with
          | 0 => φi.g_lt y hy
          | k + 1 => hv k)).2 ((sat_congr B φ (v := consV (fun k => φi.f (v k)) y)
            (w := fun k => φi.f (consV v (φi.g y) k)) (fun k => match k with
            | 0 => (φi.fg y hy).symm
            | _ + 1 => rfl)).1 h)⟩⟩

end Fg

end Found

#print axioms Found.Fg.sat_congr
#print axioms Found.Fg.sat_iso

found/Fg5.lean

lean_all/found/Fg5.lean

/-
  T185 (Fagin: NP = ESO), part 5. found/Fg5 — renaming of variables in
  first-order formulas, and first-order definable predicates of finite
  structures with valuations, closed under the connectives, bounded
  quantifiers and renamings (so constraints can be written semantically).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Fg4

namespace Found

namespace Fg

open Gt

def liftR (σ : Nat → Nat) : Nat → Nat
  | 0 => 0
  | k + 1 => σ k + 1

def renF : (Nat → Nat) → FFm → FFm
  | σ, .rel i args => .rel i (lmapG σ args)
  | σ, .eq a b => .eq (σ a) (σ b)
  | σ, .neg φ => .neg (renF σ φ)
  | σ, .conj φ ψ => .conj (renF σ φ) (renF σ ψ)
  | σ, .disj φ ψ => .disj (renF σ φ) (renF σ ψ)
  | σ, .ex φ => .ex (renF (liftR σ) φ)
  | σ, .all φ => .all (renF (liftR σ) φ)

theorem sat_ren (A : FStr) : (φ : FFm) → ∀ (σ : Nat → Nat) (v : Nat → Nat),
    (Sat A v (renF σ φ) ↔ Sat A (fun k => v (σ k)) φ)
  | .rel i args, σ, v => by
    show A.rel i (lmapG v (lmapG σ args)) = true ↔ A.rel i (lmapG (fun k => v (σ k)) args) = true
    rw [lmap_comp_eq σ v _ (fun _ => rfl) args]
  | .eq a b, _, _ => Iff.rfl
  | .neg φ, σ, v => ⟨fun h h' => h ((sat_ren A φ σ v).2 h'), fun h h' => h ((sat_ren A φ σ v).1 h')⟩
  | .conj φ ψ, σ, v => ⟨fun ⟨x, y⟩ => ⟨(sat_ren A φ σ v).1 x, (sat_ren A ψ σ v).1 y⟩,
      fun ⟨x, y⟩ => ⟨(sat_ren A φ σ v).2 x, (sat_ren A ψ σ v).2 y⟩⟩
  | .disj φ ψ, σ, v => ⟨fun h => h.elim (fun x => .inl ((sat_ren A φ σ v).1 x)) (fun y => .inr ((sat_ren A ψ σ v).1 y)),
      fun h => h.elim (fun x => .inl ((sat_ren A φ σ v).2 x)) (fun y => .inr ((sat_ren A ψ σ v).2 y))⟩
  | .ex φ, σ, v =>
    have hc : ∀ x k, consV v x (liftR σ k) = consV (fun k => v (σ k)) x k := fun x k => match k with
      | 0 => rfl
      | _ + 1 => rfl
    ⟨fun ⟨x, hx, h⟩ => ⟨x, hx, (sat_congr A φ (hc x)).1 ((sat_ren A φ (liftR σ) (consV v x)).1 h)⟩,
     fun ⟨x, hx, h⟩ => ⟨x, hx, (sat_ren A φ (liftR σ) (consV v x)).2 ((sat_congr A φ (hc x)).2 h)⟩⟩
  | .all φ, σ, v =>
    have hc : ∀ x k, consV v x (liftR σ k) = consV (fun k => v (σ k)) x k := fun x k => match k with
      | 0 => rfl
      | _ + 1 => rfl
    ⟨fun h x hx => (sat_congr A φ (hc x)).1 ((sat_ren A φ (liftR σ) (consV v x)).1 (h x hx)),
     fun h x hx => (sat_ren A φ (liftR σ) (consV v x)).2 ((sat_congr A φ (hc x)).2 (h x hx))⟩

/-- first-order definable predicates -/
def DefF (P : FStr → (Nat → Nat) → Prop) : Prop := ∃ φ : FFm, ∀ A v, Sat A v φ ↔ P A v

theorem defF_congr {P Q : FStr → (Nat → Nat) → Prop} (h : ∀ A v, P A v ↔ Q A v) (hP : DefF P) : DefF Q :=
  match hP with
  | ⟨φ, hφ⟩ => ⟨φ, fun A v => (hφ A v).trans (h A v)⟩

theorem defF_rel (i : Nat) (args : List Nat) : DefF (fun A v => A.rel i (lmapG v args) = true) :=
  ⟨.rel i args, fun _ _ => Iff.rfl⟩

theorem defF_eq (a b : Nat) : DefF (fun _ v => v a = v b) := ⟨.eq a b, fun _ _ => Iff.rfl⟩

theorem defF_true : DefF (fun _ _ => True) := ⟨.eq 0 0, fun _ _ => ⟨fun _ => trivial, fun _ => rfl⟩⟩

theorem defF_false : DefF (fun _ _ => False) :=
  ⟨.neg (.eq 0 0), fun _ _ => ⟨fun h => h rfl, fun h => h.elim⟩⟩

theorem defF_neg {P : FStr → (Nat → Nat) → Prop} (hP : DefF P) : DefF (fun A v => ¬ P A v) :=
  match hP with
  | ⟨φ, hφ⟩ => ⟨.neg φ, fun A v => ⟨fun h h' => h ((hφ A v).2 h'), fun h h' => h ((hφ A v).1 h')⟩⟩

theorem defF_and {P Q : FStr → (Nat → Nat) → Prop} (hP : DefF P) (hQ : DefF Q) : DefF (fun A v => P A v ∧ Q A v) :=
  match hP, hQ with
  | ⟨φ, hφ⟩, ⟨ψ, hψ⟩ => ⟨.conj φ ψ, fun A v => ⟨fun ⟨x, y⟩ => ⟨(hφ A v).1 x, (hψ A v).1 y⟩,
      fun ⟨x, y⟩ => ⟨(hφ A v).2 x, (hψ A v).2 y⟩⟩⟩

theorem defF_or {P Q : FStr → (Nat → Nat) → Prop} (hP : DefF P) (hQ : DefF Q) : DefF (fun A v => P A v ∨ Q A v) :=
  match hP, hQ with
  | ⟨φ, hφ⟩, ⟨ψ, hψ⟩ => ⟨.disj φ ψ, fun A v => ⟨fun h => h.elim (fun x => .inl ((hφ A v).1 x)) (fun y => .inr ((hψ A v).1 y)),
      fun h => h.elim (fun x => .inl ((hφ A v).2 x)) (fun y => .inr ((hψ A v).2 y))⟩⟩

theorem defF_ex {P : FStr → (Nat → Nat) → Prop} (hP : DefF P) :
    DefF (fun A v => ∃ x, x < A.n ∧ P A (consV v x)) :=
  match hP with
  | ⟨φ, hφ⟩ => ⟨.ex φ, fun A _ => ⟨fun ⟨x, hx, h⟩ => ⟨x, hx, (hφ A _).1 h⟩, fun ⟨x, hx, h⟩ => ⟨x, hx, (hφ A _).2 h⟩⟩⟩

theorem defF_all {P : FStr → (Nat → Nat) → Prop} (hP : DefF P) :
    DefF (fun A v => ∀ x, x < A.n → P A (consV v x)) :=
  match hP with
  | ⟨φ, hφ⟩ => ⟨.all φ, fun A _ => ⟨fun h x hx => (hφ A _).1 (h x hx), fun h x hx => (hφ A _).2 (h x hx)⟩⟩

theorem defF_ren (σ : Nat → Nat) {P : FStr → (Nat → Nat) → Prop} (hP : DefF P) :
    DefF (fun A v => P A (fun k => v (σ k))) :=
  match hP with
  | ⟨φ, hφ⟩ => ⟨renF σ φ, fun A v => (sat_ren A φ σ v).trans (hφ A _)⟩

theorem defF_imp {P Q : FStr → (Nat → Nat) → Prop} (hP : DefF P) (hQ : DefF Q) (Kc : CP.Classical') :
    DefF (fun A v => P A v → Q A v) :=
  defF_congr (fun A v => ⟨fun h hp => h.elim (fun hn => (hn hp).elim) (fun hq => hq),
    fun h => (Kc.lem (P A v)).elim (fun hp => .inr (h hp)) (fun hn => .inl hn)⟩) (defF_or (defF_neg hP) hQ)

end Fg

end Found

#print axioms Found.Fg.sat_ren
#print axioms Found.Fg.defF_ren
#print axioms Found.Fg.defF_imp

found/Fg6.lean

lean_all/found/Fg6.lean

/-
  T185 (Fagin: NP = ESO), part 6. found/Fg6 — tuples of variables (lists of
  variable indices) under an order relation symbol ℓ: equality, all-least,
  all-greatest, lexicographic comparison and lexicographic successor; all
  first-order definable.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Fg5

namespace Found

namespace Fg

open Gt

variable (ℓ : Nat)

def ltR (A : FStr) (x y : Nat) : Prop := A.rel ℓ [x, y] = true

/-- x is least -/
def IsMin (A : FStr) (x : Nat) : Prop := ∀ y, y < A.n → ¬ ltR ℓ A y x
/-- x is greatest -/
def IsMax (A : FStr) (x : Nat) : Prop := ∀ y, y < A.n → ¬ ltR ℓ A x y
/-- y is the immediate successor of x -/
def Succ1 (A : FStr) (x y : Nat) : Prop := ltR ℓ A x y ∧ ∀ z, z < A.n → ¬ (ltR ℓ A x z ∧ ltR ℓ A z y)

def TupEq (A : FStr) (v : Nat → Nat) : List Nat → List Nat → Prop
  | [], [] => True
  | a :: as, b :: bs => v a = v b ∧ TupEq A v as bs
  | [], _ :: _ => False
  | _ :: _, [] => False

def AllMin (A : FStr) (v : Nat → Nat) : List Nat → Prop
  | [] => True
  | a :: as => IsMin ℓ A (v a) ∧ AllMin A v as

def AllMax (A : FStr) (v : Nat → Nat) : List Nat → Prop
  | [] => True
  | a :: as => IsMax ℓ A (v a) ∧ AllMax A v as

def LexLt (A : FStr) (v : Nat → Nat) : List Nat → List Nat → Prop
  | [], [] => False
  | a :: as, b :: bs => ltR ℓ A (v a) (v b) ∨ (v a = v b ∧ LexLt A v as bs)
  | [], _ :: _ => False
  | _ :: _, [] => False

def TupSucc (A : FStr) (v : Nat → Nat) : List Nat → List Nat → Prop
  | [], [] => False
  | a :: as, b :: bs => (v a = v b ∧ TupSucc A v as bs) ∨
      (Succ1 ℓ A (v a) (v b) ∧ AllMax ℓ A v as ∧ AllMin ℓ A v bs)
  | [], _ :: _ => False
  | _ :: _, [] => False

theorem defF_isMin (a : Nat) : DefF (fun A v => IsMin ℓ A (v a)) :=
  defF_all (defF_neg (defF_rel ℓ [0, a + 1]))

theorem defF_isMax (a : Nat) : DefF (fun A v => IsMax ℓ A (v a)) :=
  defF_all (defF_neg (defF_rel ℓ [a + 1, 0]))

theorem defF_succ1 (a b : Nat) : DefF (fun A v => Succ1 ℓ A (v a) (v b)) :=
  defF_and (defF_rel ℓ [a, b]) (defF_all (defF_neg (defF_and (defF_rel ℓ [a + 1, 0]) (defF_rel ℓ [0, b + 1]))))

theorem defF_tupEq : (as bs : List Nat) → DefF (fun A v => TupEq A v as bs)
  | [], [] => defF_true
  | a :: as, b :: bs => defF_and (defF_eq a b) (defF_tupEq as bs)
  | [], _ :: _ => defF_false
  | _ :: _, [] => defF_false

theorem defF_allMin : (as : List Nat) → DefF (fun A v => AllMin ℓ A v as)
  | [] => defF_true
  | a :: as => defF_and (defF_isMin ℓ a) (defF_allMin as)

theorem defF_allMax : (as : List Nat) → DefF (fun A v => AllMax ℓ A v as)
  | [] => defF_true
  | a :: as => defF_and (defF_isMax ℓ a) (defF_allMax as)

theorem defF_lexLt : (as bs : List Nat) → DefF (fun A v => LexLt ℓ A v as bs)
  | [], [] => defF_false
  | a :: as, b :: bs => defF_or (defF_rel ℓ [a, b]) (defF_and (defF_eq a b) (defF_lexLt as bs))
  | [], _ :: _ => defF_false
  | _ :: _, [] => defF_false

theorem defF_tupSucc : (as bs : List Nat) → DefF (fun A v => TupSucc ℓ A v as bs)
  | [], [] => defF_false
  | a :: as, b :: bs => defF_or (defF_and (defF_eq a b) (defF_tupSucc as bs))
      (defF_and (defF_succ1 ℓ a b) (defF_and (defF_allMax ℓ as) (defF_allMin ℓ bs)))
  | [], _ :: _ => defF_false
  | _ :: _, [] => defF_false

end Fg

end Found

#print axioms Found.Fg.defF_lexLt
#print axioms Found.Fg.defF_tupSucc

found/Fg7.lean

lean_all/found/Fg7.lean

/-
  T185 (Fagin: NP = ESO), part 7. found/Fg7 — digit lists in base n (most
  significant first): the value num, the lexicographic successor incL, and
  the enumeration of all d-digit lists: the k-th list has value k.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Fg6

namespace Found

namespace Fg

open Gt

variable (n : Nat)

def DigitsLt : List Nat → Prop
  | [] => True
  | x :: l => x < n ∧ DigitsLt l

def num : List Nat → Nat
  | [] => 0
  | a :: as => a * n ^ lenG as + num as

theorem len_replG {X : Type} (x : X) : (d : Nat) → lenG (replG x d) = d
  | 0 => rfl
  | d + 1 => congrArg (· + 1) (len_replG x d)

theorem num_zero : (d : Nat) → num n (replG 0 d) = 0
  | 0 => rfl
  | d + 1 => by
    show 0 * n ^ lenG (replG 0 d) + num n (replG 0 d) = 0
    rw [Nat.zero_mul, Nat.zero_add, num_zero d]

theorem num_lt : (l : List Nat) → DigitsLt n l → num n l < n ^ lenG l
  | [], _ => by
      show 0 < n ^ 0
      rw [Nat.pow_zero]; exact Nat.zero_lt_one
  | a :: as, ⟨ha, hs⟩ => by
    show a * n ^ lenG as + num n as < n ^ (lenG as + 1)
    have h1 : a * n ^ lenG as + num n as < a * n ^ lenG as + n ^ lenG as := Nat.add_lt_add_left (num_lt as hs) _
    have h2 : a * n ^ lenG as + n ^ lenG as = (a + 1) * n ^ lenG as := (Nat.succ_mul a _).symm
    have h3 : (a + 1) * n ^ lenG as ≤ n * n ^ lenG as := Nat.mul_le_mul_right _ ha
    rw [Nat.pow_succ, Nat.mul_comm (n ^ lenG as) n]
    exact Nat.lt_of_lt_of_le h1 (h2 ▸ h3)

theorem num_max (hn : 1 ≤ n) : (l : List Nat) → AllMaxL n l → num n l + 1 = n ^ lenG l
  | [], _ => by show 0 + 1 = n ^ 0; rw [Nat.pow_zero]
  | a :: as, ⟨ha, hs⟩ => by
    show a * n ^ lenG as + num n as + 1 = n ^ (lenG as + 1)
    rw [Nat.add_assoc, num_max hn as hs, ← Nat.succ_mul]
    show (a + 1) * n ^ lenG as = n ^ (lenG as + 1)
    rw [ha, Nat.pow_succ, Nat.mul_comm]

theorem incL_cons_max {a : Nat} {as : List Nat} (h : AllMaxL n as) :
    incL n (a :: as) = (a + 1) :: replG 0 (lenG as) := by
  show incStep n a as (incL n as) = _
  unfold incStep
  match decAllMax n as with
  | isTrue _ => rfl
  | isFalse h' => exact absurd h h'

theorem incL_cons_nmax {a : Nat} {as : List Nat} (h : ¬ AllMaxL n as) :
    incL n (a :: as) = a :: incL n as := by
  show incStep n a as (incL n as) = _
  unfold incStep
  match decAllMax n as with
  | isTrue h' => exact absurd h' h
  | isFalse _ => rfl

theorem incL_len : (l : List Nat) → lenG (incL n l) = lenG l
  | [] => rfl
  | a :: as => match decAllMax n as with
    | isTrue h => by rw [incL_cons_max n h]; show lenG (replG 0 (lenG as)) + 1 = lenG as + 1; rw [len_replG]
    | isFalse h => by rw [incL_cons_nmax n h]; show lenG (incL n as) + 1 = lenG as + 1; rw [incL_len as]

theorem digits_zero (hn : 1 ≤ n) : (d : Nat) → DigitsLt n (replG 0 d)
  | 0 => trivial
  | d + 1 => ⟨hn, digits_zero hn d⟩

theorem inc_spec (hn : 1 ≤ n) : (l : List Nat) → DigitsLt n l → ¬ AllMaxL n l →
    DigitsLt n (incL n l) ∧ num n (incL n l) = num n l + 1
  | [], _, h => (h trivial).elim
  | a :: as, ⟨ha, hs⟩, hm => match decAllMax n as with
    | isTrue h => by
      rw [incL_cons_max n h]
      have ha' : a + 1 < n := Nat.lt_of_le_of_ne ha (fun e => hm ⟨e, h⟩)
      refine ⟨⟨ha', digits_zero n hn _⟩, ?_⟩
      show (a + 1) * n ^ lenG (replG 0 (lenG as)) + num n (replG 0 (lenG as)) = a * n ^ lenG as + num n as + 1
      rw [len_replG, num_zero, Nat.add_zero, Nat.add_assoc, num_max n hn as h, Nat.succ_mul]
    | isFalse h => by
      rw [incL_cons_nmax n h]
      have r := inc_spec hn as hs h
      refine ⟨⟨ha, r.1⟩, ?_⟩
      show a * n ^ lenG (incL n as) + num n (incL n as) = a * n ^ lenG as + num n as + 1
      rw [incL_len, r.2, Nat.add_assoc]

theorem iter_spec (hn : 1 ≤ n) (d : Nat) : (k : Nat) → k < n ^ d →
    DigitsLt n (iterL n d k) ∧ num n (iterL n d k) = k ∧ lenG (iterL n d k) = d
  | 0, _ => ⟨digits_zero n hn d, num_zero n d, len_replG 0 d⟩
  | k + 1, hk => by
    have ih := iter_spec hn d k (Nat.lt_of_succ_lt hk)
    have hnm : ¬ AllMaxL n (iterL n d k) := fun hm => by
      have e := num_max n hn _ hm
      rw [ih.2.1, ih.2.2] at e
      rw [e] at hk
      exact Nat.lt_irrefl _ hk
    have r := inc_spec n hn _ ih.1 hnm
    exact ⟨r.1, by show num n (incL n (iterL n d k)) = k + 1; rw [r.2, ih.2.1],
      by show lenG (incL n (iterL n d k)) = d; rw [incL_len, ih.2.2]⟩

end Fg

end Found

#print axioms Found.Fg.num_lt
#print axioms Found.Fg.num_max
#print axioms Found.Fg.inc_spec
#print axioms Found.Fg.iter_spec

found/Fg8.lean

lean_all/found/Fg8.lean

/-
  T185 (Fagin: NP = ESO), part 8. found/Fg8 — when the order symbol is the
  natural order on the universe, the tuple predicates are digit-list facts:
  least = 0, greatest = n - 1, immediate successor = +1, all-greatest =
  AllMaxL, lexicographic successor = incL; quantifying over whole d-tuples
  of variables is first-order definable.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Fg7

namespace Found

namespace Fg

open Gt

variable (ℓ : Nat) {A : FStr} (hN : ∀ x y, x < A.n → y < A.n → (ltR ℓ A x y ↔ x < y))

include hN in
theorem isMin_nat {x : Nat} (hx : x < A.n) : IsMin ℓ A x ↔ x = 0 :=
  ⟨fun h => match x, hx with
    | 0, _ => rfl
    | k + 1, hx => absurd ((hN k (k + 1) (Nat.lt_of_succ_lt hx) hx).2 (Nat.lt_succ_self k))
        (h k (Nat.lt_of_succ_lt hx)),
   fun h y hy hl => by
    rw [h] at hl
    exact Nat.not_lt_zero y ((hN y 0 hy (h ▸ hx)).1 hl)⟩

include hN in
theorem isMax_nat {x : Nat} (hx : x < A.n) : IsMax ℓ A x ↔ x + 1 = A.n :=
  ⟨fun h => (Nat.lt_or_ge (x + 1) A.n).elim
      (fun hl => absurd ((hN x (x + 1) hx hl).2 (Nat.lt_succ_self x)) (h (x + 1) hl))
      (fun hg => Nat.le_antisymm hx hg),
   fun h y hy hl => by
    have q := (hN x y hx hy).1 hl
    rw [← h] at hy
    exact Nat.lt_irrefl _ (Nat.lt_of_lt_of_le q (Nat.le_of_lt_succ hy))⟩

include hN in
theorem succ1_nat {x y : Nat} (hx : x < A.n) (hy : y < A.n) : Succ1 ℓ A x y ↔ y = x + 1 :=
  ⟨fun ⟨h1, h2⟩ => by
    have q := (hN x y hx hy).1 h1
    refine (Nat.lt_or_ge (x + 1) y).elim (fun hl => ?_) (fun hg => Nat.le_antisymm hg q)
    exact (h2 (x + 1) (Nat.lt_trans hl hy) ⟨(hN x (x + 1) hx (Nat.lt_trans hl hy)).2 (Nat.lt_succ_self x),
      (hN (x + 1) y (Nat.lt_trans hl hy) hy).2 hl⟩).elim,
   fun h => by
    subst h
    exact ⟨(hN x (x + 1) hx hy).2 (Nat.lt_succ_self x), fun z hz ⟨h1, h2⟩ => by
      have q1 := (hN x z hx hz).1 h1
      have q2 := (hN z (x + 1) hz hy).1 h2
      exact Nat.lt_irrefl _ (Nat.lt_of_lt_of_le q2 q1)⟩⟩

include hN in
theorem allMax_nat (v : Nat → Nat) : (as : List Nat) → DigitsLt A.n (lmapG v as) →
    (AllMax ℓ A v as ↔ AllMaxL A.n (lmapG v as))
  | [], _ => ⟨fun _ => trivial, fun _ => trivial⟩
  | _ :: as, ⟨ha, hs⟩ => ⟨fun ⟨h1, h2⟩ => ⟨(isMax_nat ℓ hN ha).1 h1, (allMax_nat v as hs).1 h2⟩,
      fun ⟨h1, h2⟩ => ⟨(isMax_nat ℓ hN ha).2 h1, (allMax_nat v as hs).2 h2⟩⟩

include hN in
theorem allMin_nat (v : Nat → Nat) : (as : List Nat) → DigitsLt A.n (lmapG v as) →
    (AllMin ℓ A v as ↔ lmapG v as = replG 0 (lenG as))
  | [], _ => ⟨fun _ => rfl, fun _ => trivial⟩
  | a :: as, ⟨ha, hs⟩ => ⟨fun ⟨h1, h2⟩ => by
        show v a :: lmapG v as = 0 :: replG 0 (lenG as)
        rw [(isMin_nat ℓ hN ha).1 h1, (allMin_nat v as hs).1 h2],
      fun h => have e := List.cons.inj h
        ⟨(isMin_nat ℓ hN ha).2 e.1, (allMin_nat v as hs).2 e.2⟩⟩

include hN in
theorem tupSucc_nat (v : Nat → Nat) : (as bs : List Nat) → lenG as = lenG bs → DigitsLt A.n (lmapG v as) →
    DigitsLt A.n (lmapG v bs) →
    (TupSucc ℓ A v as bs ↔ (¬ AllMaxL A.n (lmapG v as) ∧ lmapG v bs = incL A.n (lmapG v as)))
  | [], [], _, _, _ => ⟨fun h => h.elim, fun ⟨h, _⟩ => (h trivial).elim⟩
  | a :: as, b :: bs, hl, ⟨ha, hs⟩, ⟨hb, ht⟩ => by
    have hl' : lenG as = lenG bs := Nat.succ.inj hl
    have ih := tupSucc_nat v as bs hl' hs ht
    match decAllMax A.n (lmapG v as) with
    | isTrue hL =>
      show _ ↔ (¬ (v a + 1 = A.n ∧ AllMaxL A.n (lmapG v as)) ∧ v b :: lmapG v bs = incL A.n (v a :: lmapG v as))
      rw [incL_cons_max A.n hL]
      constructor
      · intro h
        match h with
        | .inl ⟨_, h2⟩ => exact ((ih.1 h2).1 hL).elim
        | .inr ⟨h1, _, h3⟩ =>
          have e1 := (succ1_nat ℓ hN ha hb).1 h1
          have e3 := (allMin_nat ℓ hN v bs ht).1 h3
          refine ⟨fun ⟨q, _⟩ => ?_, ?_⟩
          · rw [e1, q] at hb; exact Nat.lt_irrefl _ hb
          · rw [e1, e3, ← hl', len_lmapG]
      · intro ⟨_, h2⟩
        have e := List.cons.inj h2
        refine .inr ⟨(succ1_nat ℓ hN ha hb).2 e.1, (allMax_nat ℓ hN v as hs).2 hL, (allMin_nat ℓ hN v bs ht).2 ?_⟩
        rw [e.2, len_lmapG, hl']
    | isFalse hL =>
      show _ ↔ (¬ (v a + 1 = A.n ∧ AllMaxL A.n (lmapG v as)) ∧ v b :: lmapG v bs = incL A.n (v a :: lmapG v as))
      rw [incL_cons_nmax A.n hL]
      constructor
      · intro h
        match h with
        | .inl ⟨h1, h2⟩ =>
          have r := ih.1 h2
          refine ⟨fun ⟨_, q⟩ => hL q, ?_⟩
          rw [h1, r.2]
        | .inr ⟨_, h2, _⟩ => exact (hL ((allMax_nat ℓ hN v as hs).1 h2)).elim
      · intro ⟨_, h2⟩
        have e := List.cons.inj h2
        exact .inl ⟨e.1.symm, ih.2 ⟨hL, e.2⟩⟩

end Fg

end Found

#print axioms Found.Fg.isMin_nat
#print axioms Found.Fg.isMax_nat
#print axioms Found.Fg.succ1_nat
#print axioms Found.Fg.allMax_nat
#print axioms Found.Fg.allMin_nat
#print axioms Found.Fg.tupSucc_nat

found/Fg9.lean

lean_all/found/Fg9.lean

/-
  T185 (Fagin: NP = ESO), part 9. found/Fg9 — quantifying over whole
  tuples of variables: a list of values pushed in front of a valuation
  (the last pushed value is variable 0); first-order definable bounded
  ∀ and ∃ over d-tuples; reading the pushed tuple back.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Fg8

namespace Found

namespace Fg

open Gt

/-- push the values ws in front of v (the last one becomes variable 0) -/
def extW (v : Nat → Nat) : List Nat → Nat → Nat
  | [] => v
  | x :: ws => extW (consV v x) ws

theorem extW_off (v : Nat → Nat) : (ws : List Nat) → ∀ k, extW v ws (k + lenG ws) = v k
  | [], _ => rfl
  | x :: ws, k => by
    show extW (consV v x) ws (k + (lenG ws + 1)) = v k
    rw [← Nat.add_assoc, Nat.add_right_comm k (lenG ws) 1]
    show extW (consV v x) ws ((k + 1) + lenG ws) = v k
    rw [extW_off (consV v x) ws (k + 1)]
    rfl

theorem extW_range (v : Nat → Nat) : (ws : List Nat) → lmapG (extW v ws) (rangeG (lenG ws)) = ws
  | [] => rfl
  | x :: ws => by
    show extW (consV v x) ws (lenG ws) :: lmapG (extW (consV v x) ws) (rangeG (lenG ws)) = x :: ws
    have h0 := extW_off (consV v x) ws 0
    rw [Nat.zero_add] at h0
    rw [h0, extW_range (consV v x) ws]
    rfl

theorem extW_congr {v w : Nat → Nat} (h : ∀ k, v k = w k) : (ws : List Nat) → ∀ k, extW v ws k = extW w ws k
  | [], k => h k
  | x :: ws, k => extW_congr (v := consV v x) (w := consV w x) (fun j => match j with
    | 0 => rfl
    | j + 1 => h j) ws k

/-- bounded ∀ over d-tuples -/
theorem defF_allT {P : FStr → (Nat → Nat) → Prop} :
    (d : Nat) → DefF P → DefF (fun A v => ∀ ws, lenG ws = d → DigitsLt A.n ws → P A (extW v ws))
  | 0, hP => defF_congr (fun A v => ⟨fun h ws hl _ => match ws, hl with
      | [], _ => h, fun h => h [] rfl trivial⟩) hP
  | d + 1, hP => by
    have ih := defF_allT d hP
    refine defF_congr (fun A v => ⟨fun h ws hl hd => ?_, fun h x hx ws hl hd => h (x :: ws) (congrArg (· + 1) hl) ⟨hx, hd⟩⟩) (defF_all ih)
    match ws, hl, hd with
    | x :: ws, hl, ⟨hx, hd⟩ => exact h x hx ws (Nat.succ.inj hl) hd

/-- bounded ∃ over d-tuples -/
theorem defF_exT {P : FStr → (Nat → Nat) → Prop} :
    (d : Nat) → DefF P → DefF (fun A v => ∃ ws, lenG ws = d ∧ DigitsLt A.n ws ∧ P A (extW v ws))
  | 0, hP => defF_congr (fun A v => ⟨fun h => ⟨[], rfl, trivial, h⟩, fun ⟨ws, hl, _, h⟩ => match ws, hl with
      | [], _ => h⟩) hP
  | d + 1, hP => by
    have ih := defF_exT d hP
    refine defF_congr (fun A v => ⟨fun ⟨x, hx, ws, hl, hd, h⟩ => ⟨x :: ws, congrArg (· + 1) hl, ⟨hx, hd⟩, h⟩,
      fun ⟨ws, hl, hd, h⟩ => ?_⟩) (defF_ex ih)
    match ws, hl, hd with
    | x :: ws, hl, ⟨hx, hd⟩ => exact ⟨x, hx, ws, Nat.succ.inj hl, hd, h⟩

end Fg

end Found

#print axioms Found.Fg.extW_range
#print axioms Found.Fg.defF_allT
#print axioms Found.Fg.defF_exT

found/Fibration.lean

lean_all/found/Fibration.lean

/-
  T151. found/Fibration — Lawvere's hyperdoctrines as a structure on the
  category parameter C: for each object a Heyting algebra of "predicates",
  reindexing along maps, and left/right adjoints ∃_f ⊣ f* ⊣ ∀_f.
  (Fibrations in general are the Grothendieck construction of such indexed
  data — cited; here the indexed form.)
  * Instance: the setoid stem with respecting predicates (T63 SubHA) and
    T73's ∃/∀ (no re-proofs).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.HigherOrder

namespace Found

namespace Hyper

universe u v w

structure Hyperdoctrine (C : ECat.{u, v}) where
  P : C.Ob → HA.{w}
  pull : {a b : C.Ob} → C.Hom a b → (P b).car → (P a).car
  ex : {a b : C.Ob} → C.Hom a b → (P a).car → (P b).car
  all : {a b : C.Ob} → C.Hom a b → (P a).car → (P b).car
  ex_adj : ∀ {a b} (f : C.Hom a b) (φ : (P a).car) (ψ : (P b).car),
    (P b).le (ex f φ) ψ ↔ (P a).le φ (pull f ψ)
  all_adj : ∀ {a b} (f : C.Hom a b) (ψ : (P b).car) (φ : (P a).car),
    (P a).le (pull f ψ) φ ↔ (P b).le ψ (all f φ)

/-- the setoid hyperdoctrine -/
def setoidHD : Hyperdoctrine SetdCat.{0} where
  P X := SubHA X
  pull f ψ := HO.pull f ψ
  ex f φ := HO.exF f φ
  all f φ := HO.allF f φ
  ex_adj f φ ψ := HO.ex_adj f φ ψ
  all_adj f ψ φ := HO.all_adj f ψ φ

/-- in any hyperdoctrine, ∃_f is monotone (from the adjunction alone) -/
theorem ex_mono {C : ECat.{u, v}} (H : Hyperdoctrine.{u, v, w} C) {a b : C.Ob} (f : C.Hom a b)
    {φ φ' : (H.P a).car} (h : (H.P a).le φ φ') : (H.P b).le (H.ex f φ) (H.ex f φ') :=
  (H.ex_adj f φ _).mpr ((H.P a).le_trans h ((H.ex_adj f φ' _).mp ((H.P b).le_refl _)))

/-- and ∀_f is monotone -/
theorem all_mono {C : ECat.{u, v}} (H : Hyperdoctrine.{u, v, w} C) {a b : C.Ob} (f : C.Hom a b)
    {φ φ' : (H.P a).car} (h : (H.P a).le φ φ') : (H.P b).le (H.all f φ) (H.all f φ') :=
  (H.all_adj f _ φ').mp ((H.P a).le_trans ((H.all_adj f _ φ).mpr ((H.P b).le_refl _)) h)

end Hyper

end Found

#print axioms Found.Hyper.setoidHD
#print axioms Found.Hyper.ex_mono
#print axioms Found.Hyper.all_mono

found/Finitism.lean

lean_all/found/Finitism.lean

/-
  T200. found/Finitism — finitism as a point: Tait's thesis identifies
  finitist functions with the primitive recursive ones (PRA); the
  ultrafinitist point drops totality of iteration (cited).
  * Multiplication is primitive recursive, built on addition (T81), with
    the expected values: the finitist arithmetic is closed under the core's
    induction on ℕ (T59) used only through PR schemes.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Recursive

namespace Found

namespace Rec

/-- mul(0, y) = 0, mul(n+1, y) = add(mul(n, y), y) -/
def mulPR : PR := .prec .zero (.comp addPR [.proj 1, .proj 2])

theorem mul_correct (y : Nat) : ∀ n, eval mulPR [n, y] = n * y
  | 0 => (Nat.zero_mul y).symm
  | n + 1 => by
    show precIter (eval .zero) (eval (.comp addPR [.proj 1, .proj 2])) (n + 1) [y] = (n + 1) * y
    show eval addPR [precIter (eval .zero) (eval (.comp addPR [.proj 1, .proj 2])) n [y], y] = _
    have ih : precIter (eval .zero) (eval (.comp addPR [.proj 1, .proj 2])) n [y] = n * y := mul_correct y n
    rw [ih, add_correct, Nat.succ_mul]

end Rec

end Found

#print axioms Found.Rec.mul_correct

found/FixpointLogic.lean

lean_all/found/FixpointLogic.lean

/-
  T177. found/FixpointLogic — least fixed-point logic (LFP) as a point:
  first-order formulas plus least fixed points of positive operators; the
  fixed point is T171's lfp on pairs (reused), and transitive closure is
  LFP-definable: TC = μT. E(x,y) ∨ ∃z (E(x,z) ∧ T(z,y)) equals E⁺.

  Lean 4 core only, hand-written proofs, no axioms.
  CIC dependence: impredicative Prop (the lfp of T171).
-/
import found.ModalMu

namespace Found

namespace LFP

universe u

variable {A : Type u} (E : A → A → Prop)

inductive Plus : A → A → Prop where
  | one {x y} : E x y → Plus x y
  | step {x z y} : E x z → Plus z y → Plus x y

def tcOp (T : A × A → Prop) : A × A → Prop :=
  fun p => E p.1 p.2 ∨ ∃ z, E p.1 z ∧ T (z, p.2)

theorem tcOp_mono : Mu.Mono (tcOp E) :=
  fun h _ x => x.elim Or.inl (fun ⟨z, e, t⟩ => Or.inr ⟨z, e, h _ t⟩)

theorem tc_lfp (x y : A) : Mu.lfp (tcOp E) (x, y) ↔ Plus E x y := by
  constructor
  · intro h
    exact h (fun p => Plus E p.1 p.2) (fun p hp => hp.elim (fun e => .one e)
      (fun ⟨z, e, t⟩ => .step e t))
  · intro h
    induction h with
    | one e => exact Mu.f_lfp_le (tcOp_mono E) _ (Or.inl e)
    | step e _ ih => exact Mu.f_lfp_le (tcOp_mono E) _ (Or.inr ⟨_, e, ih⟩)

end LFP

end Found

#print axioms Found.LFP.tcOp_mono
#print axioms Found.LFP.tc_lfp

found/Forcing.lean

lean_all/found/Forcing.lean

/-
  T108. found/Forcing — forcing as a local operator on Ω.

  * Nuclei (Lawvere–Tierney topologies on a Heyting algebra): j inflationary,
    idempotent, monotone, preserving ∧.  The fixed points form a Heyting
    algebra (the truth values of the sheaf subtopos).
  * j = ¬¬ is a nucleus on every Heyting algebra (the ∧ law is the T77
    Gödel–Gentzen lemma, read through soundness), and in its fixed points
    excluded middle holds: ¬¬-sheaves are Boolean.
  * Kripke/Cohen forcing over a poset of conditions: the ¬¬ nucleus on
    upsets is "densely often"; arbitrary meets of ¬¬-fixed upsets are fixed
    (a complete Boolean algebra of truth values: Boolean-valued model).
  * Cohen-style example: conditions = finite bit strings under extension,
    p = "the generic bit is 1": the empty condition forces neither p nor ¬p,
    but forces ¬¬(p ∨ ¬p) — the generic is undecided while the forced
    logic is classical.

  Lean 4 core only, hand-written proofs, no axioms.

  CIC dependence: truth values = Prop (UpHA), proof irrelevance.
-/
import found.DNTrans
import found.Modal


universe u

namespace Found

namespace Forcing

open Prp

structure Nucleus (H : HA.{u}) where
  j : H.car → H.car
  infl : ∀ a, H.le a (j a)
  idem : ∀ a, H.le (j (j a)) (j a)
  mono : ∀ {a b}, H.le a b → H.le (j a) (j b)
  meet : ∀ a b, H.le (H.meet (j a) (j b)) (j (H.meet a b))

namespace Nucleus
variable {H : HA.{u}} (n : Nucleus H)

/-- the fixed points of a nucleus form a Heyting algebra -/
def fixHA : HA.{u} where
  car := {a : H.car // H.le (n.j a) a}
  le a b := H.le a.val b.val
  le_refl a := H.le_refl a.val
  le_trans h k := H.le_trans h k
  top := ⟨H.top, H.le_top _⟩
  bot := ⟨n.j H.bot, n.idem _⟩
  meet a b := ⟨H.meet a.val b.val, H.le_trans (H.meet_univ (n.mono (H.meet_l _ _)) (n.mono (H.meet_r _ _)))
    (H.meet_univ (H.le_trans (H.meet_l _ _) a.property) (H.le_trans (H.meet_r _ _) b.property))⟩
  join a b := ⟨n.j (H.join a.val b.val), n.idem _⟩
  imp a b := ⟨H.imp a.val b.val, H.imp_adj.mp (H.le_trans
      (H.meet_univ (H.meet_l _ _) (H.le_trans (H.meet_r _ _) (n.infl _)))
      (H.le_trans (n.meet _ _) (H.le_trans (n.mono (H.mp _ _)) b.property)))⟩
  le_top _ := H.le_top _
  bot_le a := H.le_trans (n.mono (H.bot_le _)) a.property
  meet_l _ _ := H.meet_l _ _
  meet_r _ _ := H.meet_r _ _
  meet_univ h k := H.meet_univ h k
  join_l _ _ := H.le_trans (H.join_l _ _) (n.infl _)
  join_r _ _ := H.le_trans (H.join_r _ _) (n.infl _)
  join_univ {_ _ c} h k := H.le_trans (n.mono (H.join_univ h k)) c.property
  imp_adj := H.imp_adj

end Nucleus

/-! ## The double negation nucleus -/

section NegNeg
variable (H : HA.{u})

/-- an intuitionistic theorem `⊢ φ → ψ` gives `⟦φ⟧ ≤ ⟦ψ⟧` in every Heyting algebra -/
theorem le_of_thm {φ ψ : Form} (d : PD true (fun _ => False) (.imp φ ψ)) (v : Nat → H.car) :
    H.le (interp H v H.bot φ) (interp H v H.bot ψ) :=
  H.le_trans (H.meet_univ (H.le_top _) (H.le_refl _))
    (H.imp_adj.mpr (valid H d v H.bot (fun _ => H.bot_le)))

def negneg : Nucleus H where
  j a := H.neg (H.neg a)
  infl := H.le_negneg
  idem a := H.negnegneg (H.neg a)
  mono h := H.neg_antitone (H.neg_antitone h)
  meet a b := le_of_thm H ((gg_nn (.and (.var 0) (.var 1)) _).1)
    (fun k => match k with | 0 => a | _ => b)

/-- ⊢ ¬¬(p ∨ ¬p) intuitionistically -/
theorem nn_lem : PD true (fun _ => False) (N (.or (.var 0) (neg (.var 0)))) :=
  .impI (.impE h0 (.orI2 (.impI (.impE h1 (.orI1 h0)))))

/-- excluded middle holds under ¬¬: ⊤ ≤ ¬¬(a ∨ ¬a) -/
theorem negneg_lem (a : H.car) : H.le H.top (H.neg (H.neg (H.join a (H.neg a)))) :=
  valid H nn_lem (fun _ => a) H.bot (fun _ => H.bot_le)

/-- the ¬¬-fixed algebra is Boolean -/
theorem negneg_boolean (a : (negneg H).fixHA.car) :
    H.le (H.neg (H.neg a.val)) a.val := a.property

end NegNeg

/-! ## Forcing over a poset of conditions -/

section Poset
variable {W : Type} (R : W → W → Prop) (hr : Modal.Refl R) (ht : Modal.Trans R)

/-- ¬¬ on upsets is "densely often": for every extension there is a further one in U -/
theorem negneg_dense (U : Modal.Up R) (w : W) :
    ((Modal.UpHA R hr ht).neg ((Modal.UpHA R hr ht).neg U)).p w ↔
    ∀ v, R w v → ¬ ∀ u, R v u → ¬ U.p u := Iff.rfl

def meetUp {I : Type} (F : I → Modal.Up R) : Modal.Up R :=
  ⟨fun w => ∀ i, (F i).p w, fun w v r h i => (F i).up w v r (h i)⟩

include hr ht in
/-- arbitrary meets of ¬¬-fixed upsets are ¬¬-fixed: the regular upsets form a
  complete Boolean algebra (the truth values of a Boolean-valued model) -/
theorem fixed_meet {I : Type} (F : I → Modal.Up R)
    (hF : ∀ i, (Modal.UpHA R hr ht).le ((negneg (Modal.UpHA R hr ht)).j (F i)) (F i)) :
    ∀ w, ((negneg (Modal.UpHA R hr ht)).j (meetUp R F)).p w → ∀ i, (F i).p w :=
  fun w h i => hF i w ((negneg (Modal.UpHA R hr ht)).mono (a := meetUp R F) (b := F i)
    (fun _ x => x i) w h)

end Poset

/-! ## A Cohen-style example: finite bit strings -/

inductive Pre : List Bool → List Bool → Prop where
  | nil (l : List Bool) : Pre [] l
  | cons (b : Bool) {l l' : List Bool} : Pre l l' → Pre (b :: l) (b :: l')

theorem pre_refl : (l : List Bool) → Pre l l
  | [] => .nil []
  | b :: l => .cons b (pre_refl l)

theorem pre_trans {a b c : List Bool} (h : Pre a b) (k : Pre b c) : Pre a c := by
  induction h generalizing c with
  | nil _ => exact .nil c
  | cons x _ ih =>
    cases k with
    | cons _ k' => exact .cons x (ih k')

def startsTrue : List Bool → Prop
  | [] => False
  | true :: _ => True
  | false :: _ => False

/-- p = "the generic bit is 1" as an upset of conditions -/
def P : Modal.Up Pre :=
  ⟨startsTrue, fun w v r h => by
    cases r with
    | nil => exact h.elim
    | cons b _ => cases b with
      | false => exact h.elim
      | true => exact trivial⟩

abbrev H2 : HA.{0} := Modal.UpHA Pre (fun l => pre_refl l) (fun _ _ _ h k => pre_trans h k)

/-- the empty condition does not force p -/
theorem not_force_p : ¬ ((negneg H2).j P).p [] :=
  fun h => h [false] (.nil _) (fun u r hu => by
    cases r with
    | cons _ _ => exact hu)

/-- the empty condition does not force ¬p -/
theorem not_force_notp : ¬ (H2.neg P).p [] :=
  fun h => h [true] (.nil _) trivial

/-- but it forces p ∨ ¬p in the ¬¬ (Boolean) sense -/
theorem force_lem : ((negneg H2).j (H2.join P (H2.neg P))).p [] :=
  negneg_lem H2 P [] trivial

end Forcing

end Found

#print axioms Found.Forcing.Nucleus.fixHA
#print axioms Found.Forcing.le_of_thm
#print axioms Found.Forcing.negneg
#print axioms Found.Forcing.nn_lem
#print axioms Found.Forcing.negneg_lem
#print axioms Found.Forcing.negneg_boolean
#print axioms Found.Forcing.negneg_dense
#print axioms Found.Forcing.fixed_meet
#print axioms Found.Forcing.pre_trans
#print axioms Found.Forcing.not_force_p
#print axioms Found.Forcing.not_force_notp
#print axioms Found.Forcing.force_lem

found/FreeInfHybrid.lean

lean_all/found/FreeInfHybrid.lean

/-
  T141. found/FreeInfHybrid — free logic, infinitary logic, hybrid logic as
  points on existing coordinates (semantics as predicates on a domain or on
  worlds of the relation coordinate).

  * Free logic: quantifiers range over an existence predicate E; instantiation
    needs E t; plain instantiation fails (countermodel); with E empty
    (inclusive free logic) ∃x (x = x) fails.
  * Infinitary logic L_ω₁ω: countable conjunctions/disjunctions are ∀n/∃n over
    ℕ-indexed families; distributivity and De Morgan (constructive direction).
  * Hybrid logic: nominals true at exactly one world, @_i; laws
    @_i @_j φ ⟺ @_j φ and (@_i j ∧ @_j φ) → @_i φ.

  Lean 4 core only, hand-written proofs, no axioms.
-/

namespace Found

namespace FreeL

universe u

variable {M : Type u} (E : M → Prop)

def fall (φ : M → Prop) : Prop := ∀ x, E x → φ x
def fex (φ : M → Prop) : Prop := ∃ x, E x ∧ φ x

theorem free_inst (φ : M → Prop) (t : M) (h : fall E φ) (ht : E t) : φ t := h t ht

theorem plain_inst_fails :
    ¬ ∀ (φ : Bool → Prop) (t : Bool), fall (fun b => b = true) φ → φ t :=
  fun h => Bool.noConfusion (h (fun b => b = true) false (fun _ e => e))

theorem inclusive_empty : ¬ fex (fun (_ : Bool) => False) (fun x => x = x) :=
  fun ⟨_, e, _⟩ => e

end FreeL

namespace InfL

/-- countable conjunction and disjunction -/
def bigAnd (φ : Nat → Prop) : Prop := ∀ n, φ n
def bigOr (φ : Nat → Prop) : Prop := ∃ n, φ n

theorem and_bigOr (p : Prop) (φ : Nat → Prop) : (p ∧ bigOr φ) ↔ bigOr (fun n => p ∧ φ n) :=
  ⟨fun ⟨hp, n, h⟩ => ⟨n, hp, h⟩, fun ⟨n, hp, h⟩ => ⟨hp, n, h⟩⟩

theorem not_bigOr (φ : Nat → Prop) : ¬ bigOr φ ↔ bigAnd (fun n => ¬ φ n) :=
  ⟨fun h n hn => h ⟨n, hn⟩, fun h ⟨n, hn⟩ => h n hn⟩

end InfL

namespace Hybrid

universe u

variable {W : Type u}

/-- a nominal: true at exactly one world -/
structure Nominal (W : Type u) where
  w : W

def holds (i : Nominal W) (v : W) : Prop := v = i.w

def at_ (i : Nominal W) (φ : W → Prop) : W → Prop := fun _ => φ i.w

theorem at_at (i j : Nominal W) (φ : W → Prop) (v : W) : at_ i (at_ j φ) v ↔ at_ j φ v := Iff.rfl

theorem at_nominal (i j : Nominal W) (φ : W → Prop) (v : W) :
    at_ i (holds j) v → at_ j φ v → at_ i φ v :=
  fun e h => by
    show φ i.w
    rw [show i.w = j.w from e]
    exact h

theorem nominal_unique (i : Nominal W) (v v' : W) (h : holds i v) (h' : holds i v') : v = v' :=
  h.trans h'.symm

end Hybrid

end Found

#print axioms Found.FreeL.free_inst
#print axioms Found.FreeL.plain_inst_fails
#print axioms Found.FreeL.inclusive_empty
#print axioms Found.InfL.and_bigOr
#print axioms Found.InfL.not_bigOr
#print axioms Found.Hybrid.at_at
#print axioms Found.Hybrid.at_nominal
#print axioms Found.Hybrid.nominal_unique

found/Frege.lean

lean_all/found/Frege.lean

/-
  T211. found/Frege — Frege and neo-logicism as a point of the chart: a
  domain D (a type), concepts D → Prop (the power/Ω operation), and an
  abstraction operator (concepts → objects) constrained by
  * Basic Law V: ext F = ext G ↔ ∀ x, F x ↔ G x — inconsistent (Russell's
    paradox), equivalently: ext would invert the map D → Ω^D, against
    Cantor's diagonal (the line to Lawvere's fixed-point theorem);
  * Hume's principle: # F = # G ↔ F and G are equinumerous (a bijective
    relation between them) — the neo-logicist base.  From HP: 0 := #(empty)
    is not a successor, numbers of equinumerous concepts agree, and the
    predecessor relation of Frege's definition is stated.  Frege's theorem
    (all Peano axioms from HP in second-order logic) is a theorem inside this
    point (out of scope).

  Lean 4 core only, hand-written proofs, no axioms.
-/

namespace Found

namespace Frege

/-! ## Basic Law V -/

/-- an extension operator satisfying Basic Law V -/
structure BLV (D : Type) where
  ext : (D → Prop) → D
  law : ∀ F G : D → Prop, ext F = ext G ↔ ∀ x, (F x ↔ G x)

/-- Russell's paradox: Basic Law V is inconsistent -/
theorem russell {D : Type} (B : BLV D) : False :=
  let R : D → Prop := fun x => ∃ F : D → Prop, x = B.ext F ∧ ¬ F x
  have nR : ¬ R (B.ext R) := fun ⟨F, e, nF⟩ => nF (((B.law R F).1 e (B.ext R)).1 ⟨F, e, nF⟩)
  nR ⟨R, rfl, nR⟩

/-- Cantor's diagonal: no map D → (D → Prop) is onto up to ↔ -/
theorem cantor {D : Type} (r : D → D → Prop) : ¬ ∀ F : D → Prop, ∃ d, ∀ x, (r d x ↔ F x) :=
  fun h => match h (fun x => ¬ r x x) with
  | ⟨d, hd⟩ =>
    have n : ¬ r d d := fun hr => (hd d).1 hr hr
    n ((hd d).2 n)

/-- the line to Cantor (and Lawvere's fixed-point theorem): Basic Law V makes
  membership in extensions onto -/
theorem blv_onto {D : Type} (B : BLV D) : ∀ F : D → Prop, ∃ d, ∀ x,
    ((∃ G : D → Prop, d = B.ext G ∧ G x) ↔ F x) :=
  fun F => ⟨B.ext F, fun x =>
    ⟨fun ⟨G, e, hG⟩ => ((B.law F G).1 e x).2 hG, fun hF => ⟨F, rfl, hF⟩⟩⟩

theorem russell' {D : Type} (B : BLV D) : False :=
  cantor (fun d x => ∃ G : D → Prop, d = B.ext G ∧ G x) (blv_onto B)

/-! ## Hume's principle -/

/-- equinumerosity: a relation that is a bijection between F and G -/
def Eqn {D : Type} (F G : D → Prop) : Prop :=
  ∃ R : D → D → Prop,
    (∀ x, F x → ∃ y, G y ∧ R x y) ∧ (∀ y, G y → ∃ x, F x ∧ R x y) ∧
    (∀ x y y', F x → R x y → R x y' → y = y') ∧ (∀ x x' y, G y → R x y → R x' y → x = x') ∧
    (∀ x y, R x y → F x ∧ G y)

theorem eqn_refl {D : Type} (F : D → Prop) : Eqn F F :=
  ⟨fun x y => F x ∧ x = y, fun x hx => ⟨x, hx, hx, rfl⟩, fun y hy => ⟨y, hy, hy, rfl⟩,
    fun _ _ _ _ h h' => h.2.symm.trans h'.2, fun _ _ _ _ h h' => h.2.trans h'.2.symm,
    fun _ _ h => ⟨h.1, h.2 ▸ h.1⟩⟩

theorem eqn_symm {D : Type} {F G : D → Prop} (h : Eqn F G) : Eqn G F :=
  match h with
  | ⟨R, t, s, f, i, d⟩ => ⟨fun y x => R x y, s, t, fun y x x' hy h1 h2 => i x x' y hy h1 h2,
      fun y y' x hx h1 h2 => f x y y' hx h1 h2, fun y x h => ⟨(d x y h).2, (d x y h).1⟩⟩

/-- a number operator satisfying Hume's principle -/
structure HP (D : Type) where
  num : (D → Prop) → D
  law : ∀ F G : D → Prop, num F = num G ↔ Eqn F G

variable {D : Type} (H : HP D)

/-- zero: the number of the empty concept -/
def zero : D := H.num (fun _ => False)

/-- Frege's predecessor: m immediately precedes n -/
def Pred (m n : D) : Prop :=
  ∃ (F : D → Prop) (y : D), F y ∧ n = H.num F ∧ m = H.num (fun x => F x ∧ x ≠ y)

/-- natural numbers: zero and its successors under the ancestral -/
def IsNat (n : D) : Prop :=
  ∀ P : D → Prop, P (zero H) → (∀ a b, P a → Pred H a b → P b) → P n

/-- zero is not a successor -/
theorem zero_not_succ (m : D) : ¬ Pred H m (zero H) :=
  fun ⟨F, y, hy, e, _⟩ => match (H.law F (fun _ => False)).1 e.symm with
    | ⟨_, t, _, _, _, _⟩ => match t y hy with
      | ⟨_, f, _⟩ => f

theorem zero_nat : IsNat H (zero H) := fun _ h0 _ => h0

/-- the successor of a natural number is natural -/
theorem nat_succ {m n : D} (hm : IsNat H m) (hp : Pred H m n) : IsNat H n :=
  fun P h0 hs => hs m n (hm P h0 hs) hp

/-- induction over the natural numbers -/
theorem nat_ind (P : D → Prop) (h0 : P (zero H)) (hs : ∀ a b, IsNat H a → P a → Pred H a b → P b) :
    ∀ n, IsNat H n → P n :=
  fun _ hn => (hn (fun x => IsNat H x ∧ P x) ⟨zero_nat H, h0⟩
    (fun a b ⟨na, pa⟩ hp => ⟨nat_succ H na hp, hs a b na pa hp⟩)).2

/-- equinumerous concepts have the same number -/
theorem num_eq {F G : D → Prop} (h : Eqn F G) : H.num F = H.num G := (H.law F G).2 h

/-- the number of a concept that holds of something is not zero -/
theorem num_ne_zero {F : D → Prop} {y : D} (hy : F y) : H.num F ≠ zero H :=
  fun e => match (H.law F (fun _ => False)).1 e with
    | ⟨_, t, _, _, _, _⟩ => match t y hy with
      | ⟨_, f, _⟩ => f

end Frege

end Found

#print axioms Found.Frege.russell
#print axioms Found.Frege.cantor
#print axioms Found.Frege.blv_onto
#print axioms Found.Frege.russell'
#print axioms Found.Frege.eqn_refl
#print axioms Found.Frege.eqn_symm
#print axioms Found.Frege.zero_not_succ
#print axioms Found.Frege.nat_succ
#print axioms Found.Frege.nat_ind
#print axioms Found.Frege.num_eq
#print axioms Found.Frege.num_ne_zero

found/Fz1.lean

lean_all/found/Fz1.lean

/-
  T89 (Friedman), part 6. found/Fz1 — reasoning inside a model of ZF
  without extensionality (the theory ZFE of found/DNeg2): definable
  predicates (closed under ∈-atoms, connectives, quantifiers, renaming) and
  the axioms read semantically: separation, collection, ∈-induction
  (schemes for definable predicates with parameters), pairing, union, weak
  power set and infinity in their weak forms.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.DNeg5

namespace Found

namespace FZ

open FO FOCT NBGc DN

structure EM where
  D : Type
  R : Nat → List D → Prop
  ax : ∀ φ, ZFE φ → ∀ v : Nat → D, Sat R φ v
  d0 : D

variable (M : EM)

abbrev In (x y : M.D) : Prop := mem M.R x y

/-- a definable predicate of valuations -/
def Def (P : (Nat → M.D) → Prop) : Prop := ∃ φ, L0 φ ∧ ∀ v, Sat M.R φ v ↔ P v

theorem def_mem (i j : Nat) : Def M (fun v => In M (v i) (v j)) := ⟨m i j, l0_m i j, fun _ => Iff.rfl⟩

theorem def_congr {P Q : (Nat → M.D) → Prop} (h : ∀ v, P v ↔ Q v) (hP : Def M P) : Def M Q :=
  match hP with
  | ⟨φ, hl, e⟩ => ⟨φ, hl, fun v => (e v).trans (h v)⟩

theorem def_and {P Q : (Nat → M.D) → Prop} (hP : Def M P) (hQ : Def M Q) : Def M (fun v => P v ∧ Q v) :=
  match hP, hQ with
  | ⟨φ, l1, e1⟩, ⟨ψ, l2, e2⟩ => ⟨.conj φ ψ, ⟨l1, l2⟩, fun v =>
    ⟨fun ⟨a, b⟩ => ⟨(e1 v).1 a, (e2 v).1 b⟩, fun ⟨a, b⟩ => ⟨(e1 v).2 a, (e2 v).2 b⟩⟩⟩

theorem def_or {P Q : (Nat → M.D) → Prop} (hP : Def M P) (hQ : Def M Q) : Def M (fun v => P v ∨ Q v) :=
  match hP, hQ with
  | ⟨φ, l1, e1⟩, ⟨ψ, l2, e2⟩ => ⟨.disj φ ψ, ⟨l1, l2⟩, fun v =>
    ⟨fun h => h.elim (fun a => .inl ((e1 v).1 a)) (fun b => .inr ((e2 v).1 b)),
     fun h => h.elim (fun a => .inl ((e1 v).2 a)) (fun b => .inr ((e2 v).2 b))⟩⟩

theorem def_imp {P Q : (Nat → M.D) → Prop} (hP : Def M P) (hQ : Def M Q) : Def M (fun v => P v → Q v) :=
  match hP, hQ with
  | ⟨φ, l1, e1⟩, ⟨ψ, l2, e2⟩ => ⟨.imp φ ψ, ⟨l1, l2⟩, fun v =>
    ⟨fun h a => (e2 v).1 (h ((e1 v).2 a)), fun h a => (e2 v).2 (h ((e1 v).1 a))⟩⟩

theorem def_false : Def M (fun _ => False) := ⟨.bot, trivial, fun _ => Iff.rfl⟩

theorem def_true : Def M (fun _ => True) :=
  ⟨.imp .bot .bot, ⟨trivial, trivial⟩, fun _ => ⟨fun _ => trivial, fun _ h => h⟩⟩

theorem def_not {P : (Nat → M.D) → Prop} (hP : Def M P) : Def M (fun v => ¬ P v) := def_imp M hP (def_false M)

theorem def_iff {P Q : (Nat → M.D) → Prop} (hP : Def M P) (hQ : Def M Q) : Def M (fun v => P v ↔ Q v) :=
  def_congr M (fun _ => ⟨fun ⟨a, b⟩ => ⟨a, b⟩, fun h => ⟨h.1, h.2⟩⟩) (def_and M (def_imp M hP hQ) (def_imp M hQ hP))

theorem def_all {P : (Nat → M.D) → Prop} (hP : Def M P) : Def M (fun v => ∀ d, P (ext v d)) :=
  match hP with
  | ⟨φ, l, e⟩ => ⟨.all φ, l, fun v => ⟨fun h d => (e (ext v d)).1 (h d), fun h d => (e (ext v d)).2 (h d)⟩⟩

theorem def_ex {P : (Nat → M.D) → Prop} (hP : Def M P) : Def M (fun v => ∃ d, P (ext v d)) :=
  match hP with
  | ⟨φ, l, e⟩ => ⟨.ex φ, l, fun v => ⟨fun ⟨d, h⟩ => ⟨d, (e (ext v d)).1 h⟩, fun ⟨d, h⟩ => ⟨d, (e (ext v d)).2 h⟩⟩⟩

theorem def_ren (σ : Nat → Nat) {P : (Nat → M.D) → Prop} (hP : Def M P) : Def M (fun v => P (fun n => v (σ n))) :=
  match hP with
  | ⟨φ, l, e⟩ => ⟨ren σ φ, l0_ren σ φ l, fun v => (sat_ren M.R φ σ v).trans (e _)⟩

/-- definable predicates respect pointwise equality of valuations -/
theorem def_pw {P : (Nat → M.D) → Prop} (hP : Def M P) {v w : Nat → M.D} (h : ∀ n, v n = w n) (hv : P v) : P w :=
  match hP with
  | ⟨φ, _, e⟩ => (e w).1 ((sat_ext M.R φ h).1 ((e v).2 hv))

/-! ## the axioms -/

theorem sepE {P : (Nat → M.D) → Prop} (hP : Def M P) (v : Nat → M.D) (a : M.D) :
    ∃ b, ∀ x, In M x b ↔ In M x a ∧ P (ext v x) :=
  match hP with
  | ⟨θ, l, e⟩ =>
    match M.ax (sepAx θ) (.inr (.inl ⟨θ, l, rfl⟩)) v a with
    | ⟨b, hb⟩ => ⟨b, fun x =>
      have hx := hb x
      have tr : Sat M.R (ren (keep 1 2) θ) (ext (ext (ext v a) b) x) ↔ P (ext v x) :=
        (sat_ren M.R θ _ _).trans ((sat_ext M.R θ (v := fun n => ext (ext (ext v a) b) x (keep 1 2 n)) (w := ext v x) (fun n => match n with
          | 0 => rfl
          | _ + 1 => rfl)).trans (e _))
      ⟨fun h => ⟨(hx.1 h).1, tr.1 (hx.1 h).2⟩, fun ⟨h1, h2⟩ => hx.2 ⟨h1, tr.2 h2⟩⟩⟩

theorem collE {P : (Nat → M.D) → Prop} (hP : Def M P) (v : Nat → M.D) (a : M.D) :
    ∃ b, ∀ x, In M x a → (∃ y, P (ext (ext v x) y)) → ∃ y, In M y b ∧ P (ext (ext v x) y) :=
  match hP with
  | ⟨θ, l, e⟩ =>
    match M.ax (collAx θ) (.inr (.inr (.inl ⟨θ, l, rfl⟩))) v a with
    | ⟨b, hb⟩ => ⟨b, fun x hx hy =>
      have tr : ∀ y, Sat M.R (ren (keep 2 2) θ) (ext (ext (ext (ext v a) b) x) y) ↔ P (ext (ext v x) y) := fun y =>
        (sat_ren M.R θ _ _).trans ((sat_ext M.R θ (v := fun n => ext (ext (ext (ext v a) b) x) y (keep 2 2 n)) (w := ext (ext v x) y) (fun n => match n with
          | 0 => rfl
          | 1 => rfl
          | _ + 2 => rfl)).trans (e _))
      match hb x hx (match hy with | ⟨y, h⟩ => ⟨y, (tr y).2 h⟩) with
      | ⟨y, h1, h2⟩ => ⟨y, h1, (tr y).1 h2⟩⟩

theorem indE {P : (Nat → M.D) → Prop} (hP : Def M P) (v : Nat → M.D)
    (h : ∀ x, (∀ y, In M y x → P (ext v y)) → P (ext v x)) : ∀ x, P (ext v x) :=
  match hP with
  | ⟨θ, l, e⟩ =>
    fun x => (e _).1 (M.ax (indAx θ) (.inl ⟨θ, l, rfl⟩) v (fun x hx => (e _).2 (h x (fun y hy =>
      (e _).1 ((sat_ext M.R θ (v := fun n => ext (ext v x) y (keep 1 1 n)) (w := ext v y) (fun n => match n with
        | 0 => rfl
        | _ + 1 => rfl)).1 ((sat_ren M.R θ _ _).1 (hx y hy)))))) x)

theorem pairE (a b : M.D) : ∃ c, In M a c ∧ In M b c := M.ax pairW (.inr (.inr (.inr (.inl rfl)))) (fun _ => M.d0) a b

theorem unionE (a : M.D) : ∃ b, ∀ x, In M x a → ∀ y, In M y x → In M y b :=
  M.ax unionW (.inr (.inr (.inr (.inr (.inl rfl))))) (fun _ => M.d0) a

theorem wpowE (a : M.D) : ∃ b, ∀ x, ∃ y, In M y b ∧ ∀ z, In M z y ↔ In M z a ∧ In M z x :=
  match M.ax wpowAx (.inr (.inr (.inr (.inr (.inr (.inl rfl)))))) (fun _ => M.d0) a with
  | ⟨b, hb⟩ => ⟨b, fun x => match hb x with
    | ⟨y, h1, h2⟩ => ⟨y, h1, fun z => ⟨fun h => (h2 z).1 h, fun h => (h2 z).2 h⟩⟩⟩

theorem infE : ∃ w, (∃ e, In M e w ∧ ∀ s, In M s e → False) ∧
    ∀ x, In M x w → ∃ y, In M y w ∧ In M x y ∧ (∀ s, In M s x → In M s y) ∧
      ∀ s, In M s y → In M s x ∨ ∀ z, In M z s ↔ In M z x :=
  match M.ax infW (.inr (.inr (.inr (.inr (.inr (.inr rfl)))))) (fun _ => M.d0) with
  | ⟨w, ⟨e, he, hne⟩, hs⟩ => ⟨w, ⟨e, he, hne⟩, fun x hx => match hs x hx with
    | ⟨y, hy, hxy, hsub, hback⟩ => ⟨y, hy, hxy, hsub, fun s hsy => (hback s hsy).elim .inl
        (fun c => .inr (fun z => ⟨(c z).1, (c z).2⟩))⟩⟩

end FZ

end Found

#print axioms Found.FZ.def_pw
#print axioms Found.FZ.sepE
#print axioms Found.FZ.collE
#print axioms Found.FZ.indE
#print axioms Found.FZ.pairE
#print axioms Found.FZ.unionE
#print axioms Found.FZ.wpowE
#print axioms Found.FZ.infE

found/Fz2.lean

lean_all/found/Fz2.lean

/-
  T89 (Friedman), part 7. found/Fz2 — codes in a model of ZF without
  extensionality, up to coextension Co (same members):
  * singleton, doubleton and ordered-pair codes (every member is of the
    right kind and each kind occurs); they exist, respect Co, and pair codes
    determine their components up to Co;
  * every set is included in a transitive set (∈-induction + collection +
    union);
  * for sets A, B a set of pair codes of all (a, b) with a ∈ A, b ∈ B and
    nothing else.

  Lean 4 core only, hand-written proofs, no axioms (classical logic only as
  the parameter K).
-/
import found.Fz1

namespace Found

namespace FZ

open FO FOCT NBGc DN

variable (M : EM)

def Co (x y : M.D) : Prop := ∀ z, In M z x ↔ In M z y

theorem co_refl (x : M.D) : Co M x x := fun _ => Iff.rfl
theorem co_symm {x y : M.D} (h : Co M x y) : Co M y x := fun z => (h z).symm
theorem co_trans {x y w : M.D} (h1 : Co M x y) (h2 : Co M y w) : Co M x w := fun z => (h1 z).trans (h2 z)

theorem def_co (i j : Nat) : Def M (fun v => Co M (v i) (v j)) :=
  def_congr M (fun _ => Iff.rfl) (def_all M (def_iff M (def_mem M 0 (i + 1)) (def_mem M 0 (j + 1))))

def Sing (s a : M.D) : Prop := (∀ x, In M x s → Co M x a) ∧ ∃ x, In M x s ∧ Co M x a
def Dbl (s a b : M.D) : Prop := (∀ x, In M x s → Co M x a ∨ Co M x b) ∧ (∃ x, In M x s ∧ Co M x a) ∧ ∃ x, In M x s ∧ Co M x b
def OPc (p a b : M.D) : Prop := (∀ z, In M z p → Sing M z a ∨ Dbl M z a b) ∧ (∃ z, In M z p ∧ Sing M z a) ∧ ∃ z, In M z p ∧ Dbl M z a b

theorem def_sing (i j : Nat) : Def M (fun v => Sing M (v i) (v j)) :=
  def_and M (def_congr M (fun _ => Iff.rfl) (def_all M (def_imp M (def_mem M 0 (i + 1)) (def_co M 0 (j + 1)))))
    (def_congr M (fun _ => Iff.rfl) (def_ex M (def_and M (def_mem M 0 (i + 1)) (def_co M 0 (j + 1)))))

theorem def_dbl (i j k : Nat) : Def M (fun v => Dbl M (v i) (v j) (v k)) :=
  def_and M (def_congr M (fun _ => Iff.rfl) (def_all M (def_imp M (def_mem M 0 (i + 1))
      (def_or M (def_co M 0 (j + 1)) (def_co M 0 (k + 1))))))
    (def_and M (def_congr M (fun _ => Iff.rfl) (def_ex M (def_and M (def_mem M 0 (i + 1)) (def_co M 0 (j + 1)))))
      (def_congr M (fun _ => Iff.rfl) (def_ex M (def_and M (def_mem M 0 (i + 1)) (def_co M 0 (k + 1))))))

theorem def_opc (i j k : Nat) : Def M (fun v => OPc M (v i) (v j) (v k)) :=
  def_and M (def_congr M (fun _ => Iff.rfl) (def_all M (def_imp M (def_mem M 0 (i + 1))
      (def_or M (def_sing M 0 (j + 1)) (def_dbl M 0 (j + 1) (k + 1))))))
    (def_and M (def_congr M (fun _ => Iff.rfl) (def_ex M (def_and M (def_mem M 0 (i + 1)) (def_sing M 0 (j + 1)))))
      (def_congr M (fun _ => Iff.rfl) (def_ex M (def_and M (def_mem M 0 (i + 1)) (def_dbl M 0 (j + 1) (k + 1))))))

/-- separation with a fixed list of parameters -/
theorem sep2 {P : M.D → Prop} (hP : Def M (fun v => P (v 0))) (a : M.D) : ∃ b, ∀ x, In M x b ↔ In M x a ∧ P x :=
  match sepE M hP (fun _ => M.d0) a with
  | ⟨b, hb⟩ => ⟨b, fun x => hb x⟩

theorem sing_ex (a : M.D) : ∃ s, Sing M s a :=
  match pairE M a a with
  | ⟨c, hc, _⟩ =>
  match sepE M (P := fun v => Co M (v 0) (v 1)) (def_co M 0 1) (fun _ => a) c with
  | ⟨s, hs⟩ => ⟨s, fun x h => ((hs x).1 h).2, a, (hs a).2 ⟨hc, co_refl M a⟩, co_refl M a⟩

theorem dbl_ex (a b : M.D) : ∃ s, Dbl M s a b :=
  match pairE M a b with
  | ⟨c, ha, hb⟩ =>
  match sepE M (P := fun v => Co M (v 0) (v 1) ∨ Co M (v 0) (v 2)) (def_or M (def_co M 0 1) (def_co M 0 2))
      (fun n => match n with | 0 => a | _ => b) c with
  | ⟨s, hs⟩ => ⟨s, fun x h => ((hs x).1 h).2, ⟨a, (hs a).2 ⟨ha, .inl (co_refl M a)⟩, co_refl M a⟩,
      b, (hs b).2 ⟨hb, .inr (co_refl M b)⟩, co_refl M b⟩

theorem opc_ex (a b : M.D) : ∃ p, OPc M p a b :=
  match sing_ex M a, dbl_ex M a b with
  | ⟨s, hs⟩, ⟨d, hd⟩ =>
  match pairE M s d with
  | ⟨c, h1, h2⟩ =>
  match sepE M (P := fun v => Sing M (v 0) (v 1) ∨ Dbl M (v 0) (v 1) (v 2))
      (def_or M (def_sing M 0 1) (def_dbl M 0 1 2)) (fun n => match n with | 0 => a | _ => b) c with
  | ⟨p, hp⟩ => ⟨p, fun z h => ((hp z).1 h).2, ⟨s, (hp s).2 ⟨h1, .inl hs⟩, hs⟩, d, (hp d).2 ⟨h2, .inr hd⟩, hd⟩

theorem sing_uniq {s a a' : M.D} (h1 : Sing M s a) (h2 : Sing M s a') : Co M a a' :=
  match h1.2 with
  | ⟨x, hx, ex⟩ => co_trans M (co_symm M ex) (h2.1 x hx)

/-- pair codes determine their components -/
theorem opc_inj {p a b a' b' : M.D} (h1 : OPc M p a b) (h2 : OPc M p a' b') :
    Co M a a' ∧ Co M b b' := by
  have ea : Co M a a' := match h1.2.1 with
    | ⟨z, hz, sz⟩ => (h2.1 z hz).elim (fun s' => sing_uniq M sz s')
      (fun d' => match d'.2.1 with
        | ⟨x, hx, ex⟩ => co_trans M (co_symm M (sz.1 x hx)) ex)
  refine ⟨ea, ?_⟩
  match h1.2.2, h2.2.2 with
  | ⟨w, hw, dw⟩, ⟨w', hw', dw'⟩ =>
  -- b' is coextensive with a or b (a member of w')
  have hb'w' : Co M b' a ∨ Co M b' b := match dw'.2.2 with
    | ⟨x, hx, ex⟩ => (h1.1 w' hw').elim (fun s => .inl (co_trans M (co_symm M ex) (s.1 x hx)))
      (fun d => (d.1 x hx).elim (fun e => .inl (co_trans M (co_symm M ex) e)) (fun e => .inr (co_trans M (co_symm M ex) e)))
  refine hb'w'.elim (fun e => ?_) (fun e => co_symm M e)
  -- b' ~ a: every member of w is ~ a, and w has a member ~ b
  have allA : ∀ x, In M x w → Co M x a := fun x hx => (h2.1 w hw).elim
    (fun s => co_trans M (s.1 x hx) (co_symm M ea))
    (fun d => (d.1 x hx).elim (fun e1 => co_trans M e1 (co_symm M ea)) (fun e1 => co_trans M e1 e))
  match dw.2.2 with
  | ⟨x, hx, ex⟩ => exact co_trans M (co_trans M (co_symm M ex) (allA x hx)) (co_symm M e)

theorem opc_resp {p a b a' b' : M.D} (h : OPc M p a b) (ea : Co M a a') (eb : Co M b b') : OPc M p a' b' :=
  have sr : ∀ z, Sing M z a → Sing M z a' := fun _ s => ⟨fun x hx => co_trans M (s.1 x hx) ea,
    match s.2 with | ⟨x, hx, e⟩ => ⟨x, hx, co_trans M e ea⟩⟩
  have dr : ∀ z, Dbl M z a b → Dbl M z a' b' := fun _ d => ⟨fun x hx => (d.1 x hx).elim
      (fun e => .inl (co_trans M e ea)) (fun e => .inr (co_trans M e eb)),
    match d.2.1 with | ⟨x, hx, e⟩ => ⟨x, hx, co_trans M e ea⟩,
    match d.2.2 with | ⟨x, hx, e⟩ => ⟨x, hx, co_trans M e eb⟩⟩
  ⟨fun z hz => (h.1 z hz).elim (fun s => .inl (sr z s)) (fun d => .inr (dr z d)),
   match h.2.1 with | ⟨z, hz, s⟩ => ⟨z, hz, sr z s⟩,
   match h.2.2 with | ⟨z, hz, d⟩ => ⟨z, hz, dr z d⟩⟩

/-! ## transitive supersets -/

def Trans (t : M.D) : Prop := ∀ u, In M u t → ∀ w, In M w u → In M w t

theorem def_trans (i : Nat) : Def M (fun v => Trans M (v i)) :=
  def_congr M (fun _ => Iff.rfl) (def_all M (def_imp M (def_mem M 0 (i + 1))
    (def_all M (def_imp M (def_mem M 0 1) (def_mem M 0 (i + 2))))))

theorem tsup (x : M.D) : ∃ t, Trans M t ∧ ∀ y, In M y x → In M y t := by
  let P : (Nat → M.D) → Prop := fun v => ∃ t, Trans M t ∧ ∀ y, In M y (v 0) → In M y t
  have hP : Def M P := def_congr M (fun _ => Iff.rfl) (def_ex M (def_and M (def_trans M 0)
    (def_all M (def_imp M (def_mem M 0 2) (def_mem M 0 1)))))
  refine indE M hP (fun _ => M.d0) (fun x ih => ?_) x
  -- collect transitive supersets of the members
  let Q : (Nat → M.D) → Prop := fun v => Trans M (v 0) ∧ ∀ w, In M w (v 1) → In M w (v 0)
  have hQ : Def M Q := def_and M (def_trans M 0) (def_congr M (fun _ => Iff.rfl)
    (def_all M (def_imp M (def_mem M 0 2) (def_mem M 0 1))))
  match collE M hQ (fun _ => M.d0) x with
  | ⟨b, hb⟩ =>
  match sepE M (P := fun v => Trans M (v 0)) (def_trans M 0) (fun _ => M.d0) b with
  | ⟨b', hb'⟩ =>
  match unionE M b' with
  | ⟨U, hU⟩ =>
  match pairE M U x with
  | ⟨c, hUc, hxc⟩ =>
  match unionE M c with
  | ⟨W, hW⟩ =>
  match sepE M (P := fun v => (∃ t, In M t (v 1) ∧ In M (v 0) t) ∨ In M (v 0) (v 2))
      (def_or M (def_congr M (fun _ => Iff.rfl) (def_ex M (def_and M (def_mem M 0 2) (def_mem M 1 0)))) (def_mem M 0 2))
      (fun n => match n with | 0 => b' | _ => x) W with
  | ⟨T, hT⟩ =>
  have memb : ∀ y, In M y x → ∃ t, In M t b' ∧ ∀ w, In M w y → In M w t := fun y hy =>
    match ih y hy with
    | ⟨t0, ht0, sub0⟩ => match hb y hy ⟨t0, ht0, sub0⟩ with
      | ⟨t, htb, htr, sub⟩ => ⟨t, (hb' t).2 ⟨htb, htr⟩, sub⟩
  have inT : ∀ t w, In M t b' → In M w t → In M w T := fun t w ht hw =>
    (hT w).2 ⟨hW U hUc w (hU t ht w hw), .inl ⟨t, ht, hw⟩⟩
  refine ⟨T, fun u hu w hw => ?_, fun y hy => (hT y).2 ⟨hW x hxc y hy, .inr hy⟩⟩
  match ((hT u).1 hu).2 with
  | .inl ⟨t, ht, hut⟩ => exact inT t w ht (((hb' t).1 ht).2 u hut w hw)
  | .inr hux => match memb u hux with
    | ⟨t, ht, sub⟩ => exact inT t w ht (sub w hw)

/-! ## sets of pair codes -/

theorem prod_codes (A B : M.D) : ∃ C, (∀ a, In M a A → ∀ b, In M b B → ∃ p, In M p C ∧ OPc M p a b) ∧
    ∀ p, In M p C → ∃ a b, In M a A ∧ In M b B ∧ OPc M p a b := by
  -- for each a ∈ A a set of codes of (a, b), b ∈ B
  let Q : (Nat → M.D) → Prop := fun v => ∀ b, In M b (v 2) → ∃ p, In M p (v 0) ∧ OPc M p (v 1) b
  have hQ : Def M Q := def_congr M (fun _ => Iff.rfl) (def_all M (def_imp M (def_mem M 0 3)
    (def_congr M (fun _ => Iff.rfl) (def_ex M (def_and M (def_mem M 0 2) (def_opc M 0 3 1))))))
  have row : ∀ a, ∃ c, ∀ b, In M b B → ∃ p, In M p c ∧ OPc M p a b := fun a =>
    match collE M (P := fun v => OPc M (v 0) (v 2) (v 1)) (def_opc M 0 2 1) (fun _ => a) B with
    | ⟨c, hc⟩ => ⟨c, fun b hb => hc b hb (opc_ex M a b)⟩
  match collE M hQ (fun _ => B) A with
  | ⟨K0, hK0⟩ =>
  match unionE M K0 with
  | ⟨C0, hC0⟩ =>
  match sepE M (P := fun v => ∃ a b, In M a (v 1) ∧ In M b (v 2) ∧ OPc M (v 0) a b)
      (def_congr M (fun _ => Iff.rfl) (def_ex M (def_ex M (def_and M (def_mem M 1 3)
        (def_and M (def_mem M 0 4) (def_opc M 2 1 0))))))
      (fun n => match n with | 0 => A | _ => B) C0 with
  | ⟨C, hC⟩ =>
  refine ⟨C, fun a ha b hb => ?_, fun p hp => ((hC p).1 hp).2⟩
  match hK0 a ha (row a) with
  | ⟨c, hcK, hc⟩ => match hc b hb with
    | ⟨p, hpc, hp⟩ => exact ⟨p, (hC p).2 ⟨hC0 c hcK p hpc, a, b, ha, hb, hp⟩, hp⟩

end FZ

end Found

#print axioms Found.FZ.def_opc
#print axioms Found.FZ.sing_ex
#print axioms Found.FZ.dbl_ex
#print axioms Found.FZ.opc_ex
#print axioms Found.FZ.opc_inj
#print axioms Found.FZ.opc_resp
#print axioms Found.FZ.tsup
#print axioms Found.FZ.prod_codes

found/Fz3.lean

lean_all/found/Fz3.lean

/-
  T89 (Friedman), part 8. found/Fz3 — Friedman's ∼ in a model of ZF without
  extensionality: x ∼ y when some set E of pair codes is a bisimulation
  relating x and y. ∼ is definable, satisfies the extensionality principle
  (sets whose members match up to ∼ are ∼), hence by ∈-induction it is an
  equivalence and contains coextension; x ∈⋆ y (x ∼ some member of y)
  respects ∼ on both sides and is extensional.

  Lean 4 core only, hand-written proofs, no axioms (classical logic only as
  the parameter K).
-/
import found.Fz2

namespace Found

namespace FZ

open FO FOCT NBGc DN

variable (M : EM)

def RelE (E u v : M.D) : Prop := ∃ p, In M p E ∧ OPc M p u v

def Bis (E : M.D) : Prop := ∀ p, In M p E → ∀ u v, OPc M p u v →
  (∀ u', In M u' u → ∃ v', In M v' v ∧ RelE M E u' v') ∧ (∀ v', In M v' v → ∃ u', In M u' u ∧ RelE M E u' v')

def Sim (x y : M.D) : Prop := ∃ E, Bis M E ∧ RelE M E x y

theorem def_rel (i j k : Nat) : Def M (fun v => RelE M (v i) (v j) (v k)) :=
  def_congr M (fun _ => Iff.rfl) (def_ex M (def_and M (def_mem M 0 (i + 1)) (def_opc M 0 (j + 1) (k + 1))))

theorem def_bis (i : Nat) : Def M (fun v => Bis M (v i)) :=
  def_congr M (fun _ => Iff.rfl) (def_all M (def_imp M (def_mem M 0 (i + 1)) (def_all M (def_all M (def_imp M (def_opc M 2 1 0)
    (def_and M
      (def_all M (def_imp M (def_mem M 0 2) (def_congr M (fun _ => Iff.rfl) (def_ex M (def_and M (def_mem M 0 2) (def_rel M (i + 5) 1 0))))))
      (def_all M (def_imp M (def_mem M 0 1) (def_congr M (fun _ => Iff.rfl) (def_ex M (def_and M (def_mem M 0 3) (def_rel M (i + 5) 0 1))))))))))))

theorem def_sim (i j : Nat) : Def M (fun v => Sim M (v i) (v j)) :=
  def_congr M (fun _ => Iff.rfl) (def_ex M (def_and M (def_bis M 0) (def_rel M 0 (i + 1) (j + 1))))

theorem sim_l {x y u : M.D} (h : Sim M x y) (hu : In M u x) : ∃ v, In M v y ∧ Sim M u v :=
  match h with
  | ⟨E, hE, p, hp, op⟩ => match (hE p hp x y op).1 u hu with
    | ⟨v, hv, r⟩ => ⟨v, hv, E, hE, r⟩

theorem sim_r {x y v : M.D} (h : Sim M x y) (hv : In M v y) : ∃ u, In M u x ∧ Sim M u v :=
  match h with
  | ⟨E, hE, p, hp, op⟩ => match (hE p hp x y op).2 v hv with
    | ⟨u, hu, r⟩ => ⟨u, hu, E, hE, r⟩

/-- a transitive set containing x -/
theorem tsup1 (x : M.D) : ∃ t, Trans M t ∧ In M x t :=
  match pairE M x x with
  | ⟨c, hc, _⟩ => match tsup M c with
    | ⟨t, ht, sub⟩ => ⟨t, ht, sub x hc⟩

/-- extensionality for ∼ -/
theorem sim_ext {x y : M.D} (h1 : ∀ u, In M u x → ∃ v, In M v y ∧ Sim M u v)
    (h2 : ∀ v, In M v y → ∃ u, In M u x ∧ Sim M u v) : Sim M x y := by
  match tsup1 M x, tsup1 M y with
  | ⟨A, hA, hxA⟩, ⟨B, hB, hyB⟩ =>
  match prod_codes M A B with
  | ⟨C, hC1, hC2⟩ =>
  match sepE M (P := fun v => ∃ u w, OPc M (v 0) u w ∧ (Sim M u w ∨ (Co M u (v 1) ∧ Co M w (v 2))))
      (def_congr M (fun _ => Iff.rfl) (def_ex M (def_ex M (def_and M (def_opc M 2 1 0)
        (def_or M (def_sim M 1 0) (def_and M (def_co M 1 3) (def_co M 0 4)))))))
      (fun n => match n with | 0 => x | _ => y) C with
  | ⟨E, hE⟩ =>
  have inE : ∀ u w, In M u A → In M w B → (Sim M u w ∨ (Co M u x ∧ Co M w y)) → RelE M E u w := fun u w hu hw c =>
    match hC1 u hu w hw with
    | ⟨p, hp, op⟩ => ⟨p, (hE p).2 ⟨hp, u, w, op, c⟩, op⟩
  refine ⟨E, fun p hp u v op => ?_, inE x y hxA hyB (.inr ⟨co_refl M x, co_refl M y⟩)⟩
  match (hE p).1 hp with
  | ⟨hpC, u0, v0, op0, c⟩ =>
  match hC2 p hpC with
  | ⟨a, b, ha, hb, opab⟩ =>
  have e0 := opc_inj M op0 op
  have eab := opc_inj M opab op
  have uA : ∀ u', In M u' u → In M u' A := fun u' h => hA a ha u' ((eab.1 u').2 h)
  have vB : ∀ v', In M v' v → In M v' B := fun v' h => hB b hb v' ((eab.2 v').2 h)
  match c with
  | .inl s =>
    exact ⟨fun u' hu' => match sim_l M s ((e0.1 u').2 hu') with
        | ⟨v', hv', s'⟩ => ⟨v', (e0.2 v').1 hv', inE u' v' (uA u' hu') (vB v' ((e0.2 v').1 hv')) (.inl s')⟩,
      fun v' hv' => match sim_r M s ((e0.2 v').2 hv') with
        | ⟨u', hu', s'⟩ => ⟨u', (e0.1 u').1 hu', inE u' v' (uA u' ((e0.1 u').1 hu')) (vB v' hv') (.inl s')⟩⟩
  | .inr ⟨cx, cy⟩ =>
    exact ⟨fun u' hu' => match h1 u' ((cx u').1 ((e0.1 u').2 hu')) with
        | ⟨v', hv', s'⟩ => have hv'' : In M v' v := (e0.2 v').1 ((cy v').2 hv')
          ⟨v', hv'', inE u' v' (uA u' hu') (vB v' hv'') (.inl s')⟩,
      fun v' hv' => match h2 v' ((cy v').1 ((e0.2 v').2 hv')) with
        | ⟨u', hu', s'⟩ => have hu'' : In M u' u := (e0.1 u').1 ((cx u').2 hu')
          ⟨u', hu'', inE u' v' (uA u' hu'') (vB v' hv') (.inl s')⟩⟩

theorem sim_refl (x : M.D) : Sim M x x :=
  indE M (P := fun v => Sim M (v 0) (v 0)) (def_sim M 0 0) (fun _ => M.d0)
    (fun _ ih => sim_ext M (fun u hu => ⟨u, hu, ih u hu⟩) (fun u hu => ⟨u, hu, ih u hu⟩)) x

theorem sim_symm {x y : M.D} (h : Sim M x y) : Sim M y x :=
  indE M (P := fun v => ∀ y, Sim M (v 0) y → Sim M y (v 0))
    (def_congr M (fun _ => Iff.rfl) (def_all M (def_imp M (def_sim M 1 0) (def_sim M 0 1)))) (fun _ => M.d0)
    (fun _ ih _ h => sim_ext M
      (fun v hv => match sim_r M h hv with | ⟨u, hu, s⟩ => ⟨u, hu, ih u hu v s⟩)
      (fun u hu => match sim_l M h hu with | ⟨v, hv, s⟩ => ⟨v, hv, ih u hu v s⟩)) x y h

theorem sim_trans {x y z : M.D} (h1 : Sim M x y) (h2 : Sim M y z) : Sim M x z :=
  indE M (P := fun v => ∀ y z, Sim M (v 0) y → Sim M y z → Sim M (v 0) z)
    (def_congr M (fun _ => Iff.rfl) (def_all M (def_all M (def_imp M (def_sim M 2 1) (def_imp M (def_sim M 1 0) (def_sim M 2 0))))))
    (fun _ => M.d0)
    (fun _ ih _ _ h1 h2 => sim_ext M
      (fun u hu => match sim_l M h1 hu with
        | ⟨v, hv, s1⟩ => match sim_l M h2 hv with
          | ⟨w, hw, s2⟩ => ⟨w, hw, ih u hu v w s1 s2⟩)
      (fun w hw => match sim_r M h2 hw with
        | ⟨v, hv, s2⟩ => match sim_r M h1 hv with
          | ⟨u, hu, s1⟩ => ⟨u, hu, ih u hu v w s1 s2⟩)) x y z h1 h2

theorem sim_of_co {x y : M.D} (h : Co M x y) : Sim M x y :=
  sim_ext M (fun u hu => ⟨u, (h u).1 hu, sim_refl M u⟩) (fun u hu => ⟨u, (h u).2 hu, sim_refl M u⟩)

/-! ## the new membership -/

def MemS (x y : M.D) : Prop := ∃ v, In M v y ∧ Sim M x v

theorem def_memS (i j : Nat) : Def M (fun v => MemS M (v i) (v j)) :=
  def_congr M (fun _ => Iff.rfl) (def_ex M (def_and M (def_mem M 0 (j + 1)) (def_sim M (i + 1) 0)))

theorem memS_l {x x' y : M.D} (e : Sim M x x') (h : MemS M x y) : MemS M x' y :=
  match h with
  | ⟨v, hv, s⟩ => ⟨v, hv, sim_trans M (sim_symm M e) s⟩

theorem memS_r {x y y' : M.D} (e : Sim M y y') (h : MemS M x y) : MemS M x y' :=
  match h with
  | ⟨_, hv, s⟩ => match sim_l M e hv with
    | ⟨v', hv', s'⟩ => ⟨v', hv', sim_trans M s s'⟩

theorem memS_of_mem {x y : M.D} (h : In M x y) : MemS M x y := ⟨x, h, sim_refl M x⟩

theorem memS_ext {x y : M.D} (h : ∀ z, MemS M z x ↔ MemS M z y) : Sim M x y :=
  sim_ext M (fun u hu => match (h u).1 (memS_of_mem M hu) with | ⟨v, hv, s⟩ => ⟨v, hv, s⟩)
    (fun v hv => match (h v).2 (memS_of_mem M hv) with | ⟨u, hu, s⟩ => ⟨u, hu, sim_symm M s⟩)

end FZ

end Found

#print axioms Found.FZ.def_sim
#print axioms Found.FZ.sim_ext
#print axioms Found.FZ.sim_refl
#print axioms Found.FZ.sim_symm
#print axioms Found.FZ.sim_trans
#print axioms Found.FZ.sim_of_co
#print axioms Found.FZ.memS_l
#print axioms Found.FZ.memS_r
#print axioms Found.FZ.memS_ext

found/Fz4.lean

lean_all/found/Fz4.lean

/-
  T89 (Friedman), part 9. found/Fz4 — Friedman's translation φ ↦ φ⋆:
  * uniform definability (one L0 formula for all models of ZF∖Ext) for
    coextension, the codes, bisimulation sets, ∼ and ∈⋆;
  * φ⋆ replaces every atom x ∈ y by the uniform formula of x ∈⋆ y;
  * in every model, φ⋆ holds exactly when φ holds in the structure (D, ∈⋆),
    and truth in (D, ∈⋆) respects ∼.

  Lean 4 core only, hand-written proofs, no axioms (the choice of the uniform
  formula uses the classical point K).
-/
import found.Fz3

namespace Found

namespace FZ

open FO FOCT NBGc DN

/-- uniformly definable predicates -/
def UDef (P : (M : EM) → (Nat → M.D) → Prop) : Prop := ∃ φ, L0 φ ∧ ∀ (M : EM) v, Sat M.R φ v ↔ P M v

theorem udef_mem (i j : Nat) : UDef (fun M v => In M (v i) (v j)) := ⟨m i j, l0_m i j, fun _ _ => Iff.rfl⟩

theorem udef_congr {P Q : (M : EM) → (Nat → M.D) → Prop} (h : ∀ M v, P M v ↔ Q M v) (hP : UDef P) : UDef Q :=
  match hP with
  | ⟨φ, hl, e⟩ => ⟨φ, hl, fun M v => (e M v).trans (h M v)⟩

theorem udef_and {P Q : (M : EM) → (Nat → M.D) → Prop} (hP : UDef P) (hQ : UDef Q) : UDef (fun M v => P M v ∧ Q M v) :=
  match hP, hQ with
  | ⟨φ, l1, e1⟩, ⟨ψ, l2, e2⟩ => ⟨.conj φ ψ, ⟨l1, l2⟩, fun M v =>
    ⟨fun ⟨a, b⟩ => ⟨(e1 M v).1 a, (e2 M v).1 b⟩, fun ⟨a, b⟩ => ⟨(e1 M v).2 a, (e2 M v).2 b⟩⟩⟩

theorem udef_or {P Q : (M : EM) → (Nat → M.D) → Prop} (hP : UDef P) (hQ : UDef Q) : UDef (fun M v => P M v ∨ Q M v) :=
  match hP, hQ with
  | ⟨φ, l1, e1⟩, ⟨ψ, l2, e2⟩ => ⟨.disj φ ψ, ⟨l1, l2⟩, fun M v =>
    ⟨fun h => h.elim (fun a => .inl ((e1 M v).1 a)) (fun b => .inr ((e2 M v).1 b)),
     fun h => h.elim (fun a => .inl ((e1 M v).2 a)) (fun b => .inr ((e2 M v).2 b))⟩⟩

theorem udef_imp {P Q : (M : EM) → (Nat → M.D) → Prop} (hP : UDef P) (hQ : UDef Q) : UDef (fun M v => P M v → Q M v) :=
  match hP, hQ with
  | ⟨φ, l1, e1⟩, ⟨ψ, l2, e2⟩ => ⟨.imp φ ψ, ⟨l1, l2⟩, fun M v =>
    ⟨fun h a => (e2 M v).1 (h ((e1 M v).2 a)), fun h a => (e2 M v).2 (h ((e1 M v).1 a))⟩⟩

theorem udef_iff {P Q : (M : EM) → (Nat → M.D) → Prop} (hP : UDef P) (hQ : UDef Q) : UDef (fun M v => P M v ↔ Q M v) :=
  udef_congr (fun _ _ => ⟨fun ⟨a, b⟩ => ⟨a, b⟩, fun h => ⟨h.1, h.2⟩⟩) (udef_and (udef_imp hP hQ) (udef_imp hQ hP))

theorem udef_all {P : (M : EM) → (Nat → M.D) → Prop} (hP : UDef P) : UDef (fun M v => ∀ d, P M (ext v d)) :=
  match hP with
  | ⟨φ, l, e⟩ => ⟨.all φ, l, fun M v => ⟨fun h d => (e M (ext v d)).1 (h d), fun h d => (e M (ext v d)).2 (h d)⟩⟩

theorem udef_ex {P : (M : EM) → (Nat → M.D) → Prop} (hP : UDef P) : UDef (fun M v => ∃ d, P M (ext v d)) :=
  match hP with
  | ⟨φ, l, e⟩ => ⟨.ex φ, l, fun M v => ⟨fun ⟨d, h⟩ => ⟨d, (e M (ext v d)).1 h⟩, fun ⟨d, h⟩ => ⟨d, (e M (ext v d)).2 h⟩⟩⟩

theorem udef_co (i j : Nat) : UDef (fun M v => Co M (v i) (v j)) :=
  udef_congr (fun _ _ => Iff.rfl) (udef_all (udef_iff (udef_mem 0 (i + 1)) (udef_mem 0 (j + 1))))

theorem udef_sing (i j : Nat) : UDef (fun M v => Sing M (v i) (v j)) :=
  udef_and (udef_congr (fun _ _ => Iff.rfl) (udef_all (udef_imp (udef_mem 0 (i + 1)) (udef_co 0 (j + 1)))))
    (udef_congr (fun _ _ => Iff.rfl) (udef_ex (udef_and (udef_mem 0 (i + 1)) (udef_co 0 (j + 1)))))

theorem udef_dbl (i j k : Nat) : UDef (fun M v => Dbl M (v i) (v j) (v k)) :=
  udef_and (udef_congr (fun _ _ => Iff.rfl) (udef_all (udef_imp (udef_mem 0 (i + 1))
      (udef_or (udef_co 0 (j + 1)) (udef_co 0 (k + 1))))))
    (udef_and (udef_congr (fun _ _ => Iff.rfl) (udef_ex (udef_and (udef_mem 0 (i + 1)) (udef_co 0 (j + 1)))))
      (udef_congr (fun _ _ => Iff.rfl) (udef_ex (udef_and (udef_mem 0 (i + 1)) (udef_co 0 (k + 1))))))

theorem udef_opc (i j k : Nat) : UDef (fun M v => OPc M (v i) (v j) (v k)) :=
  udef_and (udef_congr (fun _ _ => Iff.rfl) (udef_all (udef_imp (udef_mem 0 (i + 1))
      (udef_or (udef_sing 0 (j + 1)) (udef_dbl 0 (j + 1) (k + 1))))))
    (udef_and (udef_congr (fun _ _ => Iff.rfl) (udef_ex (udef_and (udef_mem 0 (i + 1)) (udef_sing 0 (j + 1)))))
      (udef_congr (fun _ _ => Iff.rfl) (udef_ex (udef_and (udef_mem 0 (i + 1)) (udef_dbl 0 (j + 1) (k + 1))))))

theorem udef_rel (i j k : Nat) : UDef (fun M v => RelE M (v i) (v j) (v k)) :=
  udef_congr (fun _ _ => Iff.rfl) (udef_ex (udef_and (udef_mem 0 (i + 1)) (udef_opc 0 (j + 1) (k + 1))))

theorem udef_bis (i : Nat) : UDef (fun M v => Bis M (v i)) :=
  udef_congr (fun _ _ => Iff.rfl) (udef_all (udef_imp (udef_mem 0 (i + 1)) (udef_all (udef_all (udef_imp (udef_opc 2 1 0)
    (udef_and
      (udef_all (udef_imp (udef_mem 0 2) (udef_congr (fun _ _ => Iff.rfl) (udef_ex (udef_and (udef_mem 0 2) (udef_rel (i + 5) 1 0))))))
      (udef_all (udef_imp (udef_mem 0 1) (udef_congr (fun _ _ => Iff.rfl) (udef_ex (udef_and (udef_mem 0 3) (udef_rel (i + 5) 0 1))))))))))))

theorem udef_sim (i j : Nat) : UDef (fun M v => Sim M (v i) (v j)) :=
  udef_congr (fun _ _ => Iff.rfl) (udef_ex (udef_and (udef_bis 0) (udef_rel 0 (i + 1) (j + 1))))

theorem udef_memS : UDef (fun M v => MemS M (v 0) (v 1)) :=
  udef_congr (fun _ _ => Iff.rfl) (udef_ex (udef_and (udef_mem 0 2) (udef_sim 1 0)))

/-! ## the translation -/

/-- the chosen uniform formula for x ∈⋆ y (x = variable 0, y = variable 1) -/
def memSF (K : CP.Classical') : Fm :=
  K.ch (fun φ => L0 φ ∧ ∀ (M : EM) v, Sat M.R φ v ↔ MemS M (v 0) (v 1)) udef_memS

theorem memSF_spec (K : CP.Classical') : L0 (memSF K) ∧ ∀ (M : EM) v, Sat M.R (memSF K) v ↔ MemS M (v 0) (v 1) :=
  K.ch_spec (fun φ => L0 φ ∧ ∀ (M : EM) v, Sat M.R φ v ↔ MemS M (v 0) (v 1)) udef_memS

def hdN : List Nat → Nat
  | [] => 0
  | a :: _ => a

def tlN : List Nat → List Nat
  | [] => []
  | _ :: l => l

def atomσ (l : List Nat) : Nat → Nat
  | 0 => hdN l
  | 1 => hdN (tlN l)
  | _ + 2 => 0

def star (K : CP.Classical') : Fm → Fm
  | .atom _ l => ren (atomσ l) (memSF K)
  | .bot => .bot
  | .conj a b => .conj (star K a) (star K b)
  | .disj a b => .disj (star K a) (star K b)
  | .imp a b => .imp (star K a) (star K b)
  | .all a => .all (star K a)
  | .ex a => .ex (star K a)

theorem l0_star (K : CP.Classical') : (φ : Fm) → L0 (star K φ)
  | .atom _ l => l0_ren (atomσ l) _ (memSF_spec K).1
  | .bot => trivial
  | .conj a b => ⟨l0_star K a, l0_star K b⟩
  | .disj a b => ⟨l0_star K a, l0_star K b⟩
  | .imp a b => ⟨l0_star K a, l0_star K b⟩
  | .all a => l0_star K a
  | .ex a => l0_star K a

variable (M : EM)

def hdD : List M.D → M.D
  | [] => M.d0
  | a :: _ => a

def tlD : List M.D → List M.D
  | [] => []
  | _ :: l => l

/-- the structure (D, ∈⋆) -/
def Rs (_ : Nat) (l : List M.D) : Prop := MemS M (hdD M l) (hdD M (tlD M l))

theorem star_sat (K : CP.Classical') : (φ : Fm) → L0 φ → ∀ v : Nat → M.D, Sat M.R (star K φ) v ↔ Sat (Rs M) φ v
  | .atom _ l, h, v => match h with
    | ⟨_, a, b, e⟩ => by
      subst e
      exact ((sat_ren M.R _ _ v).trans ((memSF_spec K).2 M _))
  | .bot, _, _ => Iff.rfl
  | .conj a b, h, v => ⟨fun ⟨x, y⟩ => ⟨(star_sat K a h.1 v).1 x, (star_sat K b h.2 v).1 y⟩,
      fun ⟨x, y⟩ => ⟨(star_sat K a h.1 v).2 x, (star_sat K b h.2 v).2 y⟩⟩
  | .disj a b, h, v => ⟨fun o => o.elim (fun x => .inl ((star_sat K a h.1 v).1 x)) (fun y => .inr ((star_sat K b h.2 v).1 y)),
      fun o => o.elim (fun x => .inl ((star_sat K a h.1 v).2 x)) (fun y => .inr ((star_sat K b h.2 v).2 y))⟩
  | .imp a b, h, v => ⟨fun f x => (star_sat K b h.2 v).1 (f ((star_sat K a h.1 v).2 x)),
      fun f x => (star_sat K b h.2 v).2 (f ((star_sat K a h.1 v).1 x))⟩
  | .all a, h, v => ⟨fun f d => (star_sat K a h (ext v d)).1 (f d), fun f d => (star_sat K a h (ext v d)).2 (f d)⟩
  | .ex a, h, v => ⟨fun ⟨d, x⟩ => ⟨d, (star_sat K a h (ext v d)).1 x⟩, fun ⟨d, x⟩ => ⟨d, (star_sat K a h (ext v d)).2 x⟩⟩

/-- truth in (D, ∈⋆) respects ∼ -/
theorem sat_sim : (φ : Fm) → L0 φ → ∀ {v w : Nat → M.D}, (∀ n, Sim M (v n) (w n)) → Sat (Rs M) φ v → Sat (Rs M) φ w
  | .atom _ l, h, v, w, e, s => match h with
    | ⟨_, a, b, el⟩ => by
      subst el
      exact memS_r M (e b) (memS_l M (e a) s)
  | .bot, _, _, _, _, s => s
  | .conj a b, h, _, _, e, s => ⟨sat_sim a h.1 e s.1, sat_sim b h.2 e s.2⟩
  | .disj a b, h, _, _, e, s => s.elim (fun x => .inl (sat_sim a h.1 e x)) (fun y => .inr (sat_sim b h.2 e y))
  | .imp a b, h, _, _, e, s => fun x => sat_sim b h.2 e (s (sat_sim a h.1 (fun n => sim_symm M (e n)) x))
  | .all a, h, _, _, e, s => fun d => sat_sim a h (fun n => match n with
      | 0 => sim_refl M d
      | k + 1 => e k) (s d)
  | .ex a, h, _, _, e, s => match s with
    | ⟨d, x⟩ => ⟨d, sat_sim a h (fun n => match n with
      | 0 => sim_refl M d
      | k + 1 => e k) x⟩

end FZ

end Found

#print axioms Found.FZ.udef_memS
#print axioms Found.FZ.memSF_spec
#print axioms Found.FZ.l0_star
#print axioms Found.FZ.star_sat
#print axioms Found.FZ.sat_sim

found/Fz5.lean

lean_all/found/Fz5.lean

/-
  T89 (Friedman), part 10. found/Fz5 — in every model of ZF∖Ext the
  structure (D, ∈⋆) is a model of ZF(B0): extensionality, pairing, union,
  power set (from weak power set), infinity (from the successor form),
  replacement (collection + separation, for the ⋆-translated formula) and
  foundation (∈-induction, classically).

  Lean 4 core only, hand-written proofs, no axioms (classical logic only as
  the parameter K).
-/
import found.Fz4

namespace Found

namespace FZ

open FO FOCT NBGc DN

variable (M : EM)

theorem eqF_sim {v : Nat → M.D} {i j : Nat} : Sat (Rs M) (EqF i j) v ↔ Sim M (v i) (v j) :=
  ⟨fun h => memS_ext M (fun z => ⟨(h z).1, (h z).2⟩),
   fun h _ => ⟨fun hz => memS_r M h hz, fun hz => memS_r M (sim_symm M h) hz⟩⟩

theorem pair_s (a b : M.D) : ∃ c, ∀ u, MemS M u c ↔ Sim M u a ∨ Sim M u b :=
  match pairE M a b with
  | ⟨c0, ha, hb⟩ =>
  match sepE M (P := fun v => Sim M (v 0) (v 1) ∨ Sim M (v 0) (v 2)) (def_or M (def_sim M 0 1) (def_sim M 0 2))
      (fun n => match n with | 0 => a | _ => b) c0 with
  | ⟨c, hc⟩ => ⟨c, fun _ => ⟨fun ⟨w, hw, s⟩ => ((hc w).1 hw).2.elim (fun e => .inl (sim_trans M s e))
        (fun e => .inr (sim_trans M s e)),
      fun h => h.elim (fun e => ⟨a, (hc a).2 ⟨ha, .inl (sim_refl M a)⟩, e⟩)
        (fun e => ⟨b, (hc b).2 ⟨hb, .inr (sim_refl M b)⟩, e⟩)⟩⟩

theorem union_s (a : M.D) : ∃ c, ∀ u, MemS M u c ↔ ∃ y, MemS M y a ∧ MemS M u y :=
  match unionE M a with
  | ⟨U, hU⟩ =>
  match sepE M (P := fun v => ∃ y, In M y (v 1) ∧ In M (v 0) y)
      (def_congr M (fun _ => Iff.rfl) (def_ex M (def_and M (def_mem M 0 2) (def_mem M 1 0)))) (fun _ => a) U with
  | ⟨c, hc⟩ => ⟨c, fun _ => ⟨fun ⟨w, hw, s⟩ => match ((hc w).1 hw).2 with
        | ⟨y, hy, hwy⟩ => ⟨y, memS_of_mem M hy, w, hwy, s⟩,
      fun ⟨_, hya, huy⟩ => match hya with
        | ⟨y0, hy0, ey⟩ => match memS_r M ey huy with
          | ⟨w, hw, s⟩ => ⟨w, (hc w).2 ⟨hU y0 hy0 w hw, y0, hy0, hw⟩, s⟩⟩⟩

theorem power_s (a : M.D) : ∃ c, ∀ u, MemS M u c ↔ ∀ z, MemS M z u → MemS M z a :=
  match wpowE M a with
  | ⟨b, hb⟩ =>
  match sepE M (P := fun v => ∀ z, MemS M z (v 0) → MemS M z (v 1))
      (def_congr M (fun _ => Iff.rfl) (def_all M (def_imp M (def_memS M 0 1) (def_memS M 0 2)))) (fun _ => a) b with
  | ⟨c, hc⟩ => ⟨c, fun u => ⟨fun ⟨y, hy, s⟩ z hz => ((hc y).1 hy).2 z (memS_r M s hz),
      fun hsub =>
        match sepE M (P := fun v => MemS M (v 0) (v 1)) (def_memS M 0 1) (fun _ => u) a with
        | ⟨x, hx⟩ => match hb x with
          | ⟨y, hyb, hy⟩ =>
            have ysub : ∀ z, MemS M z y → MemS M z a := fun _ ⟨w, hw, s⟩ =>
              ⟨w, ((hy w).1 hw).1, s⟩
            have e : Sim M u y := memS_ext M (fun z => ⟨fun hz => match hsub z hz with
                | ⟨w, hw, s⟩ => ⟨w, (hy w).2 ⟨hw, (hx w).2 ⟨hw, memS_l M s hz⟩⟩, s⟩,
              fun ⟨w, hw, s⟩ => memS_l M (sim_symm M s) ((hx w).1 ((hy w).1 hw).2).2⟩)
            ⟨y, (hc y).2 ⟨hyb, ysub⟩, e⟩⟩⟩

theorem inf_s : ∃ w, (∃ e, MemS M e w ∧ ∀ s, MemS M s e → False) ∧
    ∀ x, MemS M x w → ∃ y, MemS M y w ∧ ∀ s, MemS M s y ↔ MemS M s x ∨ Sim M s x :=
  match infE M with
  | ⟨w, ⟨e, he, hne⟩, hs⟩ => ⟨w, ⟨e, memS_of_mem M he, fun _ ⟨t, ht, _⟩ => hne t ht⟩, fun _ ⟨x0, hx0, ex⟩ =>
    match hs x0 hx0 with
    | ⟨y, hy, hxy, hsub, hback⟩ => ⟨y, memS_of_mem M hy, fun _ =>
      ⟨fun ⟨t, ht, st⟩ => (hback t ht).elim (fun htx => .inl (memS_r M (sim_symm M ex) ⟨t, htx, st⟩))
          (fun c => .inr (sim_trans M st (sim_trans M (sim_of_co M c) (sim_symm M ex)))),
       fun h => h.elim (fun hs' => match memS_r M ex hs' with
          | ⟨t, ht, st⟩ => ⟨t, hsub t ht, st⟩)
         (fun e' => ⟨x0, hxy, sim_trans M e' ex⟩)⟩⟩⟩

/-! ## the schemes -/

theorem defθ (K : CP.Classical') {θ : Fm} (hl : L0 θ) (σ : Nat → Nat) :
    Def M (fun u => Sat (Rs M) θ (fun n => u (σ n))) :=
  ⟨ren σ (star K θ), l0_ren σ _ (l0_star K θ), fun u => (sat_ren M.R _ σ u).trans (star_sat M K θ hl _)⟩

def sw : Nat → Nat
  | 0 => 1
  | 1 => 0
  | n + 2 => n + 2

def σq : Nat → Nat
  | 0 => 0
  | 1 => 1
  | n + 2 => n + 3

theorem repl_s (K : CP.Classical') {θ : Fm} (hl : L0 θ) (pF : Nat → M.D) (x : M.D)
    (pre : ∀ w, MemS M w x → ∀ z z', Sat (Rs M) θ (ext (ext pF z) w) → Sat (Rs M) θ (ext (ext pF z') w) → Sim M z z') :
    ∃ y, ∀ z, MemS M z y ↔ ∃ w, MemS M w x ∧ Sat (Rs M) θ (ext (ext pF z) w) := by
  have pc : ∀ w z, Sat (Rs M) θ (fun n => ext (ext pF w) z (sw n)) ↔ Sat (Rs M) θ (ext (ext pF z) w) := fun _ _ =>
    sat_ext (Rs M) θ (fun n => match n with
      | 0 => rfl
      | 1 => rfl
      | _ + 2 => rfl)
  match collE M (defθ M K hl sw) pF x with
  | ⟨b, hb⟩ =>
  have hQ : Def M (fun u => ∃ w, In M w (u 1) ∧ Sat (Rs M) θ (fun n => ext u w (σq n))) :=
    def_congr M (fun _ => Iff.rfl) (def_ex M (def_and M (def_mem M 0 2) (defθ M K hl σq)))
  have qc : ∀ z w, Sat (Rs M) θ (fun n => ext (ext (ext pF x) z) w (σq n)) ↔ Sat (Rs M) θ (ext (ext pF z) w) :=
    fun _ _ => sat_ext (Rs M) θ (fun n => match n with
      | 0 => rfl
      | 1 => rfl
      | _ + 2 => rfl)
  match sepE M hQ (ext pF x) b with
  | ⟨y, hy⟩ =>
  refine ⟨y, fun z => ⟨fun ⟨z0, hz0, s⟩ => ?_, fun ⟨w, hw, ht⟩ => ?_⟩⟩
  · match ((hy z0).1 hz0).2 with
    | ⟨w, hwx, ht⟩ =>
      exact ⟨w, memS_of_mem M hwx, sat_sim M θ hl (v := ext (ext pF z0) w) (fun n => match n with
        | 0 => sim_refl M w
        | 1 => sim_symm M s
        | _ + 2 => sim_refl M _) ((qc z0 w).1 ht)⟩
  · match hw with
    | ⟨w0, hw0, ew⟩ =>
      have ht0 : Sat (Rs M) θ (ext (ext pF z) w0) := sat_sim M θ hl (v := ext (ext pF z) w) (fun n => match n with
        | 0 => ew
        | _ + 1 => sim_refl M _) ht
      match hb w0 hw0 ⟨z, (pc w0 z).2 ht0⟩ with
      | ⟨z1, hz1, ht1⟩ =>
        have ht1' := (pc w0 z1).1 ht1
        exact ⟨z1, (hy z1).2 ⟨hz1, w0, hw0, (qc z1 w0).2 ht1'⟩, pre w0 (memS_of_mem M hw0) z z1 ht0 ht1'⟩

def σid (n : Nat) : Nat := n

theorem σid_eq (v : Nat → M.D) (n : Nat) : v (σid n) = v n := rfl

theorem found_s (K : CP.Classical') {θ : Fm} (hl : L0 θ) (pF : Nat → M.D) (hx : ∃ x, Sat (Rs M) θ (ext pF x)) :
    ∃ x, Sat (Rs M) θ (ext pF x) ∧ ∀ y, MemS M y x → ¬ Sat (Rs M) θ (ext pF y) := by
  refine (K.lem (∃ x, Sat (Rs M) θ (ext pF x) ∧ ∀ y, MemS M y x → ¬ Sat (Rs M) θ (ext pF y))).elim (fun h => h)
    (fun hno => ?_)
  have hP : Def M (fun u => ¬ Sat (Rs M) θ (fun n => u (σid n))) := def_not M (defθ M K hl σid)
  have all : ∀ x, ¬ Sat (Rs M) θ (fun n => ext pF x (σid n)) := indE M hP pF (fun x ih ht =>
    hno ⟨x, (sat_ext (Rs M) θ (fun _ => rfl)).1 ht, fun y ⟨y0, hy0, ey⟩ hty =>
      ih y0 hy0 ((sat_ext (Rs M) θ (fun _ => rfl)).1
        (sat_sim M θ hl (v := ext pF y) (fun n => match n with
          | 0 => ey
          | _ + 1 => sim_refl M _) hty))⟩)
  match hx with
  | ⟨x, h⟩ => exact ((all x) ((sat_ext (Rs M) θ (fun _ => rfl)).1 h)).elim

end FZ

end Found

#print axioms Found.FZ.eqF_sim
#print axioms Found.FZ.pair_s
#print axioms Found.FZ.union_s
#print axioms Found.FZ.power_s
#print axioms Found.FZ.inf_s
#print axioms Found.FZ.defθ
#print axioms Found.FZ.repl_s
#print axioms Found.FZ.found_s

found/Fz6.lean

lean_all/found/Fz6.lean

/-
  T89 (Friedman), part 11. found/Fz6 — Friedman's theorem (1973):
  * every model of ZF∖Ext gives the model (D, ∈⋆) of ZF(B0);
  * hence (soundness and completeness) ZF ⊢ φ implies ZF∖Ext ⊢ φ⋆;
  * with the double-negation interpretation (found/DNeg5): ZF ⊢ φ implies
    IZF∖Ext ⊢ (φ⋆)* intuitionistically — ZF is interpreted in the
    intuitionistic set theory IZF∖Ext.

  Lean 4 core only, hand-written proofs, no axioms (classical logic only as
  the parameter K, for the classical side).
-/
import found.Fz5
import found.NbgZ5

namespace Found

namespace FZ

open FO FOCT NBGc DN

theorem model_s (M : EM) (K : CP.Classical') : ∀ φ, ZF B0 φ → ∀ v : Nat → M.D, Sat (Rs M) φ v := by
  intro φ hφ v
  match hφ with
  | .inl e =>
    rw [e]
    exact fun _ _ hco _ hxw => memS_l M (memS_ext M (fun z => ⟨(hco z).1, (hco z).2⟩)) hxw
  | .inr (.inl hb) =>
    match hb with
    | .inl e =>
      rw [e]
      exact fun a b => match pair_s M a b with
        | ⟨c, hc⟩ => ⟨c, fun u => ⟨fun h => ((hc u).1 h).elim (fun x => .inl ((eqF_sim M).2 x)) (fun x => .inr ((eqF_sim M).2 x)),
            fun h => (hc u).2 (h.elim (fun x => .inl ((eqF_sim M).1 x)) (fun x => .inr ((eqF_sim M).1 x)))⟩⟩
    | .inr (.inl e) =>
      rw [e]
      exact fun a => match union_s M a with
        | ⟨c, hc⟩ => ⟨c, fun u => ⟨fun h => (hc u).1 h, fun h => (hc u).2 h⟩⟩
    | .inr (.inr (.inl e)) =>
      rw [e]
      exact fun a => match power_s M a with
        | ⟨c, hc⟩ => ⟨c, fun u => ⟨fun h => (hc u).1 h, fun h => (hc u).2 h⟩⟩
    | .inr (.inr (.inr e)) =>
      rw [e]
      exact match inf_s M with
        | ⟨w, he, hs⟩ => ⟨w, he, fun x hx => match hs x hx with
          | ⟨y, hy, hy'⟩ => ⟨y, hy, fun s => ⟨fun h => ((hy' s).1 h).elim .inl (fun e' => .inr ((eqF_sim M).2 e')),
              fun h => (hy' s).2 (h.elim .inl (fun e' => .inr ((eqF_sim M).1 e')))⟩⟩⟩
  | .inr (.inr (.inl ⟨θ, hl, e⟩)) =>
    rw [e]
    intro x hpre
    have eA : ∀ w z z', Sat (Rs M) (ren (r3 2 1) θ) (ext (ext (ext (ext v x) w) z) z') ↔ Sat (Rs M) θ (ext (ext v z) w) :=
      fun _ _ _ => (sat_ren (Rs M) θ _ _).trans (sat_ext (Rs M) θ (fun n => match n with
        | 0 => rfl
        | 1 => rfl
        | _ + 2 => rfl))
    have eB : ∀ w z z', Sat (Rs M) (ren (r3 2 0) θ) (ext (ext (ext (ext v x) w) z) z') ↔ Sat (Rs M) θ (ext (ext v z') w) :=
      fun _ _ _ => (sat_ren (Rs M) θ _ _).trans (sat_ext (Rs M) θ (fun n => match n with
        | 0 => rfl
        | 1 => rfl
        | _ + 2 => rfl))
    have eC : ∀ y z w, Sat (Rs M) (ren (r3 0 1) θ) (ext (ext (ext (ext v x) y) z) w) ↔ Sat (Rs M) θ (ext (ext v z) w) :=
      fun _ _ _ => (sat_ren (Rs M) θ _ _).trans (sat_ext (Rs M) θ (fun n => match n with
        | 0 => rfl
        | 1 => rfl
        | _ + 2 => rfl))
    match repl_s M K hl v x (fun w hw z z' h1 h2 =>
        (eqF_sim M (v := ext (ext (ext (ext v x) w) z) z') (i := 1) (j := 0)).1
          (hpre w hw z z' ⟨(eA w z z').2 h1, (eB w z z').2 h2⟩)) with
    | ⟨y, hy⟩ => exact ⟨y, fun z => ⟨fun hz => match (hy z).1 hz with
        | ⟨w, h1, h2⟩ => ⟨w, h1, (eC y z w).2 h2⟩,
      fun ⟨w, h1, h2⟩ => (hy z).2 ⟨w, h1, (eC y z w).1 h2⟩⟩⟩
  | .inr (.inr (.inr ⟨θ, hl, e⟩)) =>
    rw [e]
    intro hx
    have e1 : ∀ x y, Sat (Rs M) (ren (up Nat.succ) θ) (ext (ext v x) y) ↔ Sat (Rs M) θ (ext v y) := fun _ _ =>
      (sat_ren (Rs M) θ _ _).trans (sat_ext (Rs M) θ (fun n => match n with
        | 0 => rfl
        | _ + 1 => rfl))
    match found_s M K hl v hx with
    | ⟨x, h1, h2⟩ => exact ⟨x, h1, fun y hy ht => h2 y hy ((e1 x y).1 ht)⟩

/-- ZF ⊢ φ ⇒ ZF∖Ext ⊢ φ⋆ -/
theorem zf_zfe (K : CP.Classical') {φ : Fm} (hl : L0 φ) (h : PC (ZF B0) [] φ) : PC ZFE [] (star K φ) :=
  completenessT ZFE K (fun D R v hM _ =>
    let M : EM := ⟨D, R, hM, v 0⟩
    (star_sat M K φ hl v).2 (soundnessT (ZF B0) K h D (Rs M) v (model_s M K) (fun _ m => nomatch m)))

/-- Friedman: ZF ⊢ φ ⇒ IZF∖Ext ⊢ (φ⋆)* (intuitionistically) -/
theorem friedman (K : CP.Classical') {φ : Fm} (hl : L0 φ) (h : PC (ZF B0) [] φ) : IPC ZFE [] (gg (star K φ)) :=
  zfe_izfe (zf_zfe K hl h)

end FZ

end Found

#print axioms Found.FZ.model_s
#print axioms Found.FZ.zf_zfe
#print axioms Found.FZ.friedman

found/GL.lean

lean_all/found/GL.lean

/-
  T137. found/GL — provability logic GL as a point on the relation
  coordinate R (T67's semantics reused).

  Löb's axiom □(□p → p) → □p is valid on a frame  ⟺  R is transitive and
  conversely well-founded (every world is accessible for "has a successor
  chain"), both directions constructive:
  * ⇐ by Acc-induction;
  * ⇒ well-foundedness from the valuation p := Acc, transitivity via the
    axiom 4 (derived semantically from Löb) and T67's 4 ⟺ transitive.

  Lean 4 core only, hand-written proofs, no axioms.
  CIC dependence: elimination of Acc from Prop (structural).
-/
import found.Modal

namespace Found

namespace Modal

universe u

variable {W : Type u} (R : W → W → Prop)

def Succ (y x : W) : Prop := R x y

def CWF : Prop := ∀ w, Acc (Succ R) w

def LobValid : Prop :=
  ∀ (V : W → Nat → Prop) w,
    sat R V w (.imp (.box (.imp (.box (.var 0)) (.var 0))) (.box (.var 0)))

theorem lob_of_frame (ht : Trans R) (hw : CWF R) : LobValid R := by
  intro V w h
  have key : ∀ v, Acc (Succ R) v → R w v → V v 0 := by
    intro v acc
    induction acc with
    | intro v _ ih =>
      intro r
      exact h v r (fun u r' => ih u r' (ht w v u r r'))
  exact fun v r => key v (hw v) r

theorem cwf_of_lob (hl : LobValid R) : CWF R := by
  intro w
  have hb := hl (fun v _ => Acc (Succ R) v) w
    (fun v _ hv => Acc.intro v (fun u r => hv u r))
  exact Acc.intro w (fun u r => hb u r)

theorem four_of_lob (hl : LobValid R) :
    ∀ (V : W → Nat → Prop) w, sat R V w (.imp (.box (.var 0)) (.box (.box (.var 0)))) := by
  intro V w hp
  -- apply Löb to q := p ∧ □p, encoded as a valuation of variable 0
  have hq := hl (fun v _ => V v 0 ∧ ∀ u, R v u → V u 0) w
    (fun v r hv => ⟨hp v r, fun u r' => (hv u r').1⟩)
  exact fun v r u r' => (hq v r).2 u r'

theorem trans_of_lob (hl : LobValid R) : Trans R :=
  (four_iff_trans R).mp (four_of_lob R hl)

/-- GL frames: Löb ⟺ transitive and conversely well-founded -/
theorem gl_iff : LobValid R ↔ Trans R ∧ CWF R :=
  ⟨fun hl => ⟨trans_of_lob R hl, cwf_of_lob R hl⟩, fun ⟨ht, hw⟩ => lob_of_frame R ht hw⟩

/-- GL is irreflexive on its frames (contrast with T and S4) -/
theorem gl_irrefl (hw : CWF R) (w : W) : ¬ R w w := by
  induction hw w with
  | intro w _ ih => exact fun r => ih w r r

end Modal

end Found

#print axioms Found.Modal.lob_of_frame
#print axioms Found.Modal.cwf_of_lob
#print axioms Found.Modal.four_of_lob
#print axioms Found.Modal.trans_of_lob
#print axioms Found.Modal.gl_iff
#print axioms Found.Modal.gl_irrefl

found/GPK.lean

lean_all/found/GPK.lean

/-
  T164. found/GPK — positive set theory as a point: comprehension for
  positive formulas (no negation, no implication) on a structure (M, ∈),
  the same free parameter as NF (T106).
  * Positive formulas: ∈, =, ∧, ∨, ∀, ∃.
  * Consequences: a universal set with V ∈ V, binary unions and
    intersections; Russell's formula is not positive (and its comprehension
    is contradictory, T106 reused).
  Models of GPK⁺ (Esser, topological) are cited.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.NF

namespace Found

namespace GPK

universe u

inductive PF where
  | mem : Nat → Nat → PF
  | eq : Nat → Nat → PF
  | and : PF → PF → PF
  | or : PF → PF → PF
  | all : Nat → PF → PF
  | ex : Nat → PF → PF

variable {M : Type u} (mem : M → M → Prop)

def sat : (Nat → M) → PF → Prop
  | ρ, .mem i j => mem (ρ i) (ρ j)
  | ρ, .eq i j => ρ i = ρ j
  | ρ, .and a b => sat ρ a ∧ sat ρ b
  | ρ, .or a b => sat ρ a ∨ sat ρ b
  | ρ, .all n a => ∀ m, sat (NF.upd ρ n m) a
  | ρ, .ex n a => ∃ m, sat (NF.upd ρ n m) a

def PosComp : Prop := ∀ φ : PF, ∀ ρ : Nat → M, ∃ y, ∀ x, mem x y ↔ sat mem (NF.upd ρ 0 x) φ

variable {mem}

theorem univ (h : PosComp mem) (m : M) : ∃ V, (∀ x, mem x V) ∧ mem V V :=
  match h (.eq 0 0) (fun _ => m) with
  | ⟨V, hV⟩ => ⟨V, fun x => (hV x).mpr rfl, (hV V).mpr rfl⟩

theorem union (h : PosComp mem) (a b : M) : ∃ c, ∀ x, mem x c ↔ mem x a ∨ mem x b :=
  match h (.or (.mem 0 1) (.mem 0 2)) (fun k => match k with | 1 => a | _ => b) with
  | ⟨c, hc⟩ => ⟨c, hc⟩

theorem inter (h : PosComp mem) (a b : M) : ∃ c, ∀ x, mem x c ↔ mem x a ∧ mem x b :=
  match h (.and (.mem 0 1) (.mem 0 2)) (fun k => match k with | 1 => a | _ => b) with
  | ⟨c, hc⟩ => ⟨c, hc⟩

end GPK

end Found

#print axioms Found.GPK.univ
#print axioms Found.GPK.union
#print axioms Found.GPK.inter

found/Game.lean

lean_all/found/Game.lean

/-
  T117. found/Game — game semantics as a point: Hintikka's evaluation game
  for propositional formulas under a Bool valuation (verifier chooses at ∨,
  falsifier at ∧, roles swap at ¬).
  * The verifier has a winning strategy iff the formula is true, and the
    falsifier iff it is false (constructive, by induction) — truth = winning,
    the game is determined (line to T168).
  Lorenzen dialogues (intuitionistic) and game models of linear logic
  (Blass, Abramsky–Jagadeesan) are the further values (cited).

  Lean 4 core only, hand-written proofs, no axioms.
-/

namespace Found

namespace GameSem

inductive F where
  | var : Nat → F
  | neg : F → F
  | and : F → F → F
  | or : F → F → F

def val (v : Nat → Bool) : F → Bool
  | .var n => v n
  | .neg a => !val v a
  | .and a b => val v a && val v b
  | .or a b => val v a || val v b

mutual
/-- the verifier wins the game on φ -/
def VWin (v : Nat → Bool) : F → Prop
  | .var n => v n = true
  | .neg a => FWin v a
  | .and a b => VWin v a ∧ VWin v b
  | .or a b => VWin v a ∨ VWin v b
/-- the falsifier wins -/
def FWin (v : Nat → Bool) : F → Prop
  | .var n => v n = false
  | .neg a => VWin v a
  | .and a b => FWin v a ∨ FWin v b
  | .or a b => FWin v a ∧ FWin v b
end

theorem band_t : (a b : Bool) → ((a && b) = true ↔ a = true ∧ b = true)
  | true, true => ⟨fun _ => ⟨rfl, rfl⟩, fun _ => rfl⟩
  | true, false => ⟨fun h => Bool.noConfusion h, fun h => Bool.noConfusion h.2⟩
  | false, _ => ⟨fun h => Bool.noConfusion h, fun h => Bool.noConfusion h.1⟩

theorem band_f : (a b : Bool) → ((a && b) = false ↔ a = false ∨ b = false)
  | true, true => ⟨fun h => Bool.noConfusion h, fun h => h.elim (fun e => Bool.noConfusion e) (fun e => Bool.noConfusion e)⟩
  | true, false => ⟨fun _ => Or.inr rfl, fun _ => rfl⟩
  | false, _ => ⟨fun _ => Or.inl rfl, fun _ => rfl⟩

theorem bor_t : (a b : Bool) → ((a || b) = true ↔ a = true ∨ b = true)
  | true, _ => ⟨fun _ => Or.inl rfl, fun _ => rfl⟩
  | false, true => ⟨fun _ => Or.inr rfl, fun _ => rfl⟩
  | false, false => ⟨fun h => Bool.noConfusion h, fun h => h.elim (fun e => Bool.noConfusion e) (fun e => Bool.noConfusion e)⟩

theorem bor_f : (a b : Bool) → ((a || b) = false ↔ a = false ∧ b = false)
  | true, _ => ⟨fun h => Bool.noConfusion h, fun h => Bool.noConfusion h.1⟩
  | false, true => ⟨fun h => Bool.noConfusion h, fun h => Bool.noConfusion h.2⟩
  | false, false => ⟨fun _ => ⟨rfl, rfl⟩, fun _ => rfl⟩

theorem bnot_t : (a : Bool) → ((!a) = true ↔ a = false)
  | true => ⟨fun h => Bool.noConfusion h, fun h => Bool.noConfusion h⟩
  | false => ⟨fun _ => rfl, fun _ => rfl⟩

theorem bnot_f : (a : Bool) → ((!a) = false ↔ a = true)
  | true => ⟨fun _ => rfl, fun _ => rfl⟩
  | false => ⟨fun h => Bool.noConfusion h, fun h => Bool.noConfusion h⟩

theorem and_iff {p q p' q' : Prop} (h1 : p ↔ p') (h2 : q ↔ q') : (p ∧ q) ↔ (p' ∧ q') :=
  ⟨fun h => ⟨h1.mp h.1, h2.mp h.2⟩, fun h => ⟨h1.mpr h.1, h2.mpr h.2⟩⟩

theorem or_iff {p q p' q' : Prop} (h1 : p ↔ p') (h2 : q ↔ q') : (p ∨ q) ↔ (p' ∨ q') :=
  ⟨fun h => h.elim (fun a => Or.inl (h1.mp a)) (fun b => Or.inr (h2.mp b)),
   fun h => h.elim (fun a => Or.inl (h1.mpr a)) (fun b => Or.inr (h2.mpr b))⟩

theorem win_iff (v : Nat → Bool) : (φ : F) →
    (VWin v φ ↔ val v φ = true) ∧ (FWin v φ ↔ val v φ = false)
  | .var _ => ⟨Iff.rfl, Iff.rfl⟩
  | .neg a => ⟨(win_iff v a).2.trans (bnot_t _).symm, (win_iff v a).1.trans (bnot_f _).symm⟩
  | .and a b => ⟨(and_iff (win_iff v a).1 (win_iff v b).1).trans (band_t _ _).symm,
                 (or_iff (win_iff v a).2 (win_iff v b).2).trans (band_f _ _).symm⟩
  | .or a b => ⟨(or_iff (win_iff v a).1 (win_iff v b).1).trans (bor_t _ _).symm,
                (and_iff (win_iff v a).2 (win_iff v b).2).trans (bor_f _ _).symm⟩

/-- the evaluation game is determined -/
theorem determined (v : Nat → Bool) (φ : F) : VWin v φ ∨ FWin v φ := by
  cases h : val v φ
  · exact Or.inr ((win_iff v φ).2.mpr h)
  · exact Or.inl ((win_iff v φ).1.mpr h)

end GameSem

end Found

#print axioms Found.GameSem.win_iff
#print axioms Found.GameSem.determined

found/GameLL.lean

lean_all/found/GameLL.lean

/-
  T117, line games <-> linear logic. found/GameLL — a game model of linear
  logic through one-move (Blass) games:
  * a one-move game: the proponent picks u : U, the opponent answers x : X,
    the proponent wins iff α u x; the proponent wins the game iff
    ∃ u ∀ x α u x (a winning first move);
  * a strategy for A ⊸ B (the proponent plays B while borrowing A, copying
    the opponent's B-answer back into A) is exactly a Dial morphism; strategies
    compose (the category of games and strategies is Dial, found/Dialectica);
  * winning is preserved: a strategy for A ⊸ B maps winning moves of A to
    winning moves of B;
  * tensor: A ⊗ B is won if both are (and conversely when counter-moves
    exist); the unit is won; the linear
    implication game A ⊸ B is won iff a strategy A → B exists; & is won iff
    both components are;
  * so the symmetric monoidal closed structure with products and the
    exponential of found/DialLL is a game model of intuitionistic linear logic.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.DialLL

namespace Found

namespace GameLL

open Dial DialLL

universe u

/-- the proponent wins the one-move game A -/
def Won (A : Obj.{u}) : Prop := ∃ u, ∀ x, A.α u x

/-- a strategy maps winning first moves to winning first moves -/
theorem strategy_wins {A B : Obj.{u}} (s : H A B) (h : Won A) : Won B :=
  match h with
  | ⟨u, hu⟩ => ⟨s.f u, fun y => s.cond u y (hu (s.F u y))⟩

/-- composition of strategies is composition in Dial -/
theorem comp_wins {A B C : Obj.{u}} (s : H A B) (t : H B C) (h : Won A) : Won C :=
  strategy_wins (cmp s t) h

/-- if both games are won, so is their tensor -/
theorem won_tens {A B : Obj.{u}} (ha : Won A) (hb : Won B) : Won (tens A B) :=
  match ha, hb with
  | ⟨u, hu⟩, ⟨v, hv⟩ => ⟨(u, v), fun x => ⟨hu x.1, hv x.2⟩⟩

/-- conversely, with counter-moves available in both games -/
theorem won_tens_inv {A B : Obj.{u}} (x0 : A.X) (y0 : B.X) (h : Won (tens A B)) : Won A ∧ Won B :=
  match h with
  | ⟨p, hp⟩ => ⟨⟨p.1, fun x => (hp (x, y0)).1⟩, ⟨p.2, fun y => (hp (x0, y)).2⟩⟩

theorem won_unit : Won unit.{u} := ⟨PUnit.unit, fun _ => trivial⟩

/-- the linear implication game is won exactly by the strategies -/
theorem won_lolli (A B : Obj.{u}) : Won (lolli A B) ↔ Nonempty (H A B) :=
  ⟨fun ⟨g, hg⟩ => ⟨⟨g.1, g.2, fun u y => hg (u, y)⟩⟩,
   fun ⟨s⟩ => ⟨(s.f, s.F), fun p => s.cond p.1 p.2⟩⟩

theorem won_wth (A B : Obj.{u}) : Won (wth A B) ↔ Won A ∧ Won B :=
  ⟨fun ⟨p, hp⟩ => ⟨⟨p.1, fun x => hp (.inl x)⟩, ⟨p.2, fun y => hp (.inr y)⟩⟩,
   fun ⟨⟨u, hu⟩, ⟨v, hv⟩⟩ => ⟨(u, v), fun x => match x with
     | .inl a => hu a
     | .inr b => hv b⟩⟩

/-- !A is won iff A is (finite lists of counter-moves are answered one by one) -/
theorem all_of {X : Type u} {p : X → Prop} (h : ∀ x, p x) : (l : List X) → All p l
  | [] => trivial
  | x :: l => ⟨h x, all_of h l⟩

theorem won_bang (A : Obj.{u}) : Won (bang A) ↔ Won A :=
  ⟨fun h => strategy_wins (counit A) h,
   fun ⟨u, hu⟩ => ⟨u, fun φ => all_of hu (φ u)⟩⟩

end GameLL

end Found

#print axioms Found.GameLL.strategy_wins
#print axioms Found.GameLL.comp_wins
#print axioms Found.GameLL.won_tens
#print axioms Found.GameLL.won_tens_inv
#print axioms Found.GameLL.won_unit
#print axioms Found.GameLL.won_lolli
#print axioms Found.GameLL.won_wth
#print axioms Found.GameLL.won_bang

found/GeometricLogic.lean

lean_all/found/GeometricLogic.lean

/-
  T194. found/GeometricLogic — geometric logic as a point: geometric
  (propositional) formulas use ⊤, ∧ and arbitrary disjunctions ⋁ (no →, ∀);
  their interpretation in a frame (T115) is preserved by frame
  homomorphisms (inverse images of geometric morphisms of locales/toposes).
  Classifying toposes (every geometric theory has one) are the known
  continuation (cited).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Locale

namespace Found

namespace Geo

universe u

inductive GF : Type (u + 1) where
  | atom : Nat → GF
  | top : GF
  | and : GF → GF → GF
  | sup : {I : Type u} → (I → GF) → GF

def interp (L : Loc.Frame.{u}) (v : Nat → L.car) : GF.{u} → L.car
  | .atom n => v n
  | .top => L.top
  | .and a b => L.meet (interp L v a) (interp L v b)
  | .sup f => L.sup (fun i => interp L v (f i))

structure FrameHom (L M : Loc.Frame.{u}) where
  h : L.car → M.car
  mono : ∀ {a b}, L.le a b → M.le (h a) (h b)
  top : M.le M.top (h L.top)
  meet : ∀ a b, M.le (M.meet (h a) (h b)) (h (L.meet a b))
  sup : ∀ {I : Type u} (f : I → L.car), M.le (h (L.sup f)) (M.sup (fun i => h (f i)))

/-- geometric formulas are preserved by frame homomorphisms (both inequalities) -/
theorem preserve (L M : Loc.Frame.{u}) (φ : FrameHom L M) (v : Nat → L.car) :
    (g : GF.{u}) → M.le (φ.h (interp L v g)) (interp M (fun n => φ.h (v n)) g) ∧
      M.le (interp M (fun n => φ.h (v n)) g) (φ.h (interp L v g))
  | .atom _ => ⟨M.le_refl _, M.le_refl _⟩
  | .top => ⟨M.le_top _, φ.top⟩
  | .and a b =>
    ⟨M.meet_univ (M.le_trans (φ.mono (L.meet_l _ _)) (preserve L M φ v a).1)
                 (M.le_trans (φ.mono (L.meet_r _ _)) (preserve L M φ v b).1),
     M.le_trans (M.meet_univ (M.le_trans (M.meet_l _ _) (preserve L M φ v a).2)
                             (M.le_trans (M.meet_r _ _) (preserve L M φ v b).2)) (φ.meet _ _)⟩
  | .sup f =>
    ⟨M.le_trans (φ.sup _) (M.sup_least _ _ (fun i =>
        M.le_trans (preserve L M φ v (f i)).1 (M.sup_ub (fun j => interp M (fun n => φ.h (v n)) (f j)) i))),
     M.sup_least _ _ (fun i => M.le_trans (preserve L M φ v (f i)).2
        (φ.mono (L.sup_ub (fun j => interp L v (f j)) i)))⟩

end Geo

end Found

#print axioms Found.Geo.preserve

found/GoedelBridge.lean

lean_all/found/GoedelBridge.lean

/-
  T85. found/GoedelBridge — Gödel's incompleteness theorems, linked from the
  user's existing formalization (found/NoSelfVerify.lean, a copy of
  ~/Downloads/NoSelfVerify.lean; the original is untouched): Löb, Gödel I
  and II for any proof system with K, S, the derivability conditions and a
  diagonal sentence, plus the type-level diagonal results.
  Bridge (short): the same Löb principle appears on both sides of the
  provability-logic line —
    syntax  (NoSelfVerify.loeb: ⊢ □b → b  ⟹  ⊢ b)
    frames  (T137 GL: transitive + conversely well-founded ⟹ Löb valid),
  and Gödel II is Löb at b = ⊥.
  found/NoSelfVerify.lean is the axiom-free version of the user's file (same
  statements; Classical.em in cantor_inj replaced by the constructive
  diagonal, simp by Bool case splits); the verbatim copy is kept, unbuilt, in
  work/imported/NoSelfVerify.lean.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.NoSelfVerify
import found.GL

namespace Found

namespace GoedelBridge

theorem loeb_both :
    (∀ (T : NoSelfVerify.ProvSys) (b : T.S), T.Pr (T.imp (T.box b) b) → T.Pr b) ∧
    (∀ {W : Type} (R : W → W → Prop), Modal.Trans R → Modal.CWF R → Modal.LobValid R) :=
  ⟨NoSelfVerify.loeb, fun R ht hw => Modal.lob_of_frame R ht hw⟩

/-- Gödel II as Löb at ⊥ -/
theorem goedel2_from_loeb (T : NoSelfVerify.ProvSys) (hc : ¬ T.Pr T.bot) :
    ¬ T.Pr (T.imp (T.box T.bot) T.bot) :=
  fun h => hc (NoSelfVerify.loeb T T.bot h)

end GoedelBridge

end Found

#print axioms NoSelfVerify.loeb
#print axioms NoSelfVerify.goedel1
#print axioms NoSelfVerify.goedel2
#print axioms NoSelfVerify.model_consistent
#print axioms NoSelfVerify.model_no_self_verify
#print axioms NoSelfVerify.no_truth_inside
#print axioms NoSelfVerify.tower_never_closes
#print axioms NoSelfVerify.cantor_inj
#print axioms NoSelfVerify.lean_types_not_inside_lean
#print axioms NoSelfVerify.no_code_of_all_types
#print axioms NoSelfVerify.no_self_checker
#print axioms NoSelfVerify.not_every_map_coded
#print axioms Found.GoedelBridge.loeb_both
#print axioms Found.GoedelBridge.goedel2_from_loeb

found/GraphModel.lean

lean_all/found/GraphModel.lean

/-
  T116 line. found/GraphModel — a model of the untyped λ-calculus
  (found/Lambda): Engeler's graph model.  D = sets of Engeler elements
  (atoms and pairs (finite list, element)); application
  F·X = {b | ∃ finite l ⊆ X, (l, b) ∈ F}; abstraction of f = its graph on
  finite sets.  Every term denotes a monotone, finitely determined function
  of its environment; β holds for such functions; hence β-steps preserve
  denotations (soundness).  Equalities are pointwise ↔ (no funext, no
  propext).  The extensional model D ≅ D^D (Scott's D∞) is the remaining part.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Lambda

namespace Found

namespace GM

open UL

inductive Eg where
  | atom : Nat → Eg
  | arr : List Eg → Eg → Eg

abbrev D := Eg → Prop

def Fin (l : List Eg) (X : D) : Prop := ∀ e, e ∈ l → X e
def setL (l : List Eg) : D := fun e => e ∈ l

def app (F X : D) : D := fun b => ∃ l, Fin l X ∧ F (.arr l b)

def lam (f : D → D) : D
  | .atom _ => False
  | .arr l b => f (setL l) b

def Eqv (X Y : D) : Prop := ∀ e, X e ↔ Y e
def Sub (X Y : D) : Prop := ∀ e, X e → Y e

def consD {n : List Unit} (X : D) (ρ : V n → D) : V (() :: n) → D
  | .z => X
  | .s x => ρ x

def den : {n : List Unit} → T n → (V n → D) → D
  | _, .var x, ρ => ρ x
  | _, .app a b, ρ => app (den a ρ) (den b ρ)
  | _, .lam t, ρ => lam (fun X => den t (consD X ρ))

/-! monotonicity -/

theorem den_mono : {n : List Unit} → (t : T n) → {ρ ρ' : V n → D} → (∀ x, Sub (ρ x) (ρ' x)) → Sub (den t ρ) (den t ρ')
  | _, .var x, _, _, h => h x
  | _, .app a b, _, _, h => fun _ ⟨l, hl, ha⟩ => ⟨l, fun e' m => den_mono b h e' (hl e' m), den_mono a h _ ha⟩
  | _, .lam t, ρ, ρ', h => fun e he => match e, he with
    | .arr l b, hb => den_mono t (ρ := consD (setL l) ρ) (ρ' := consD (setL l) ρ')
        (fun x => match x with | .z => fun _ h => h | .s y => h y) b hb

theorem den_ext {n : List Unit} (t : T n) {ρ ρ' : V n → D} (h : ∀ x, Eqv (ρ x) (ρ' x)) : Eqv (den t ρ) (den t ρ') :=
  fun e => ⟨den_mono t (fun x e' => (h x e').mp) e, den_mono t (fun x e' => (h x e').mpr) e⟩

/-! finite determination -/

def FEnv (n : List Unit) := V n → List Eg

def fenv {n : List Unit} (F : FEnv n) : V n → D := fun x => setL (F x)

def FLe {n : List Unit} (F : FEnv n) (ρ : V n → D) : Prop := ∀ x, Fin (F x) (ρ x)

def fempty (n : List Unit) : FEnv n := fun _ => []

def funion {n : List Unit} (F G : FEnv n) : FEnv n := fun x => F x ++ G x

theorem mem_app_l {a : Eg} : (l k : List Eg) → a ∈ l → a ∈ l ++ k
  | _ :: _, _, .head _ => .head _
  | _ :: l, k, .tail _ m => .tail _ (mem_app_l l k m)

theorem mem_app_r {a : Eg} : (l k : List Eg) → a ∈ k → a ∈ l ++ k
  | [], _, m => m
  | _ :: l, k, m => .tail _ (mem_app_r l k m)

theorem mem_app_inv {a : Eg} : (l k : List Eg) → a ∈ l ++ k → a ∈ l ∨ a ∈ k
  | [], _, m => Or.inr m
  | _ :: l, k, m => by
    cases m with
    | head => exact Or.inl (.head _)
    | tail _ m' => exact (mem_app_inv l k m').elim (fun h => Or.inl (.tail _ h)) Or.inr

theorem funion_le {n : List Unit} {F G : FEnv n} {ρ : V n → D} (h1 : FLe F ρ) (h2 : FLe G ρ) : FLe (funion F G) ρ :=
  fun x e m => (mem_app_inv _ _ m).elim (h1 x e) (h2 x e)

theorem sub_union_l {n : List Unit} (F G : FEnv n) : ∀ x, Sub (fenv F x) (fenv (funion F G) x) :=
  fun _ _ m => mem_app_l _ _ m

theorem sub_union_r {n : List Unit} (F G : FEnv n) : ∀ x, Sub (fenv G x) (fenv (funion F G) x) :=
  fun _ _ m => mem_app_r _ _ m

def decV : {n : List Unit} → V n → V n → Bool
  | _, .z, .z => true
  | _, .z, .s _ => false
  | _, .s _, .z => false
  | _, .s x, .s y => decV x y

theorem decV_refl : {n : List Unit} → (x : V n) → decV x x = true
  | _, .z => rfl
  | _, .s x => decV_refl x

theorem decV_eq : {n : List Unit} → (x y : V n) → decV x y = true → x = y
  | _, .z, .z, _ => rfl
  | _, .z, .s _, h => Bool.noConfusion h
  | _, .s _, .z, h => Bool.noConfusion h
  | _, .s x, .s y, h => congrArg V.s (decV_eq x y h)

def single1 {n : List Unit} (y : V n) (b : Eg) : FEnv n := fun x =>
  match decV x y with
  | true => [b]
  | false => []

theorem single1_le {n : List Unit} (y : V n) (b : Eg) (ρ : V n → D) (hb : ρ y b) : FLe (single1 y b) ρ := by
  intro x e m
  unfold single1 at m
  cases h : decV x y with
  | true =>
    rw [h] at m
    have := decV_eq x y h
    subst this
    cases m with
    | head => exact hb
    | tail _ m' => exact nomatch m'
  | false => rw [h] at m; exact nomatch m

theorem single1_mem {n : List Unit} (y : V n) (b : Eg) : fenv (single1 y b) y b := by
  show b ∈ single1 y b y
  unfold single1
  rw [decV_refl]
  exact .head _

/-- every denotation is finitely determined -/
theorem fd : {n : List Unit} → (t : T n) → (ρ : V n → D) → (b : Eg) → den t ρ b →
    ∃ F : FEnv n, FLe F ρ ∧ den t (fenv F) b
  | _, .var y, ρ, b, h => ⟨single1 y b, single1_le y b ρ h, single1_mem y b⟩
  | _, .app a c, ρ, b, h => by
    obtain ⟨l, hl, ha⟩ := (h : ∃ l, Fin l (den c ρ) ∧ den a ρ (.arr l b))
    match fd a ρ _ ha with
    | ⟨F1, h1, d1⟩ =>
      have hall : ∀ k : List Eg, (∀ e, e ∈ k → den c ρ e) → ∃ G : FEnv _, FLe G ρ ∧ ∀ e, e ∈ k → den c (fenv G) e := by
        intro k
        induction k with
        | nil => exact fun _ => ⟨fempty _, ⟨(fun _ _ (m : _ ∈ ([] : List Eg)) => nomatch m), (fun _ (m : _ ∈ ([] : List Eg)) => nomatch m)⟩⟩
        | cons e k ih => exact fun hk => match fd c ρ e (hk e (.head _)), ih (fun e' m => hk e' (.tail _ m)) with
          | ⟨Fe, he, de⟩, ⟨G, hG, dG⟩ => ⟨funion Fe G, funion_le he hG, fun e' m => by
              cases m with
              | head => exact den_mono c (sub_union_l Fe G) _ de
              | tail _ m' => exact den_mono c (sub_union_r Fe G) _ (dG e' m')⟩
      match hall l hl with
      | ⟨G, hG, dG⟩ => exact ⟨funion F1 G, funion_le h1 hG,
          ⟨l, fun e m => den_mono c (sub_union_r F1 G) _ (dG e m), den_mono a (sub_union_l F1 G) _ d1⟩⟩
  | _, .lam t, ρ, .arr l b, h => by
    match fd t (consD (setL l) ρ) b h with
    | ⟨F'', h'', d''⟩ =>
      refine ⟨fun x => F'' (.s x), fun x => h'' (.s x), ?_⟩
      show den t (consD (setL l) (fenv fun x => F'' (.s x))) b
      exact den_mono t (ρ := fenv F'') (ρ' := consD (setL l) (fenv fun x => F'' (.s x))) (fun x => match x with
        | .z => fun e m => h'' .z e m
        | .s _ => fun _ m => m) b d''
  | _, .lam _, _, .atom _, h => h.elim

/-- continuity in the first variable -/
theorem cont {n : List Unit} (t : T (() :: n)) (ρ : V n → D) (X : D) (b : Eg) (h : den t (consD X ρ) b) :
    ∃ l, Fin l X ∧ den t (consD (setL l) ρ) b :=
  match fd t (consD X ρ) b h with
  | ⟨F, hF, dF⟩ => ⟨F .z, hF .z, den_mono t (ρ := fenv F) (ρ' := consD (setL (F .z)) ρ) (fun x => match x with
      | .z => fun _ m => m
      | .s y => fun e m => hF (.s y) e m) b dF⟩

/-- β for denotations -/
theorem beta_den {n : List Unit} (t : T (() :: n)) (ρ : V n → D) (X : D) :
    Eqv (app (lam (fun Y => den t (consD Y ρ))) X) (den t (consD X ρ)) := fun b =>
  ⟨fun ⟨l, hl, hb⟩ => den_mono t (ρ := consD (setL l) ρ) (ρ' := consD X ρ) (fun x => match x with
      | .z => fun e m => hl e m
      | .s _ => fun _ m => m) b hb,
   fun h => match cont t ρ X b h with
    | ⟨l, hl, hb⟩ => ⟨l, hl, hb⟩⟩

theorem den_ren : {m : List Unit} → (t : T m) → {n : List Unit} → (r : V m → V n) → (ρ : V n → D) →
    Eqv (den (ren r t) ρ) (den t (fun x => ρ (r x)))
  | _, .var _, _, _, _ => fun _ => Iff.rfl
  | _, .app a c, _, r, ρ => fun _ =>
    ⟨fun ⟨l, hl, ha⟩ => ⟨l, fun e m => (den_ren c r ρ e).mp (hl e m), (den_ren a r ρ _).mp ha⟩,
     fun ⟨l, hl, ha⟩ => ⟨l, fun e m => (den_ren c r ρ e).mpr (hl e m), (den_ren a r ρ _).mpr ha⟩⟩
  | _, .lam t, _, r, ρ => fun e => match e with
    | .atom _ => Iff.rfl
    | .arr l b => (den_ren t (lift r) (consD (setL l) ρ) b).trans
        (den_ext t (ρ := fun x => consD (setL l) ρ (lift r x)) (ρ' := consD (setL l) fun x => ρ (r x))
          (fun x => match x with | .z => fun _ => Iff.rfl | .s _ => fun _ => Iff.rfl) b)

theorem den_sub : {m : List Unit} → (t : T m) → {n : List Unit} → (σ : V m → T n) → (ρ : V n → D) →
    Eqv (den (sub σ t) ρ) (den t (fun x => den (σ x) ρ))
  | _, .var _, _, _, _ => fun _ => Iff.rfl
  | _, .app a c, _, σ, ρ => fun _ =>
    ⟨fun ⟨l, hl, ha⟩ => ⟨l, fun e m => (den_sub c σ ρ e).mp (hl e m), (den_sub a σ ρ _).mp ha⟩,
     fun ⟨l, hl, ha⟩ => ⟨l, fun e m => (den_sub c σ ρ e).mpr (hl e m), (den_sub a σ ρ _).mpr ha⟩⟩
  | _, .lam t, _, σ, ρ => fun e => match e with
    | .atom _ => Iff.rfl
    | .arr l b => (den_sub t (liftS σ) (consD (setL l) ρ) b).trans
        (den_ext t (ρ := fun x => den (liftS σ x) (consD (setL l) ρ)) (ρ' := consD (setL l) fun x => den (σ x) ρ)
          (fun x => match x with
            | .z => fun _ => Iff.rfl
            | .s y => fun e' => den_ren (σ y) V.s (consD (setL l) ρ) e') b)

/-- soundness: β-steps preserve denotations -/
theorem sound {n : List Unit} {t t' : T n} (h : Beta t t') : ∀ ρ, Eqv (den t ρ) (den t' ρ) := by
  induction h with
  | beta t u =>
    intro ρ e
    refine (beta_den t ρ (den u ρ) e).trans ?_
    refine (den_ext t (ρ := consD (den u ρ) ρ) (ρ' := fun x => den (UL.single u x) ρ)
      (fun x => match x with | .z => fun _ => Iff.rfl | .s _ => fun _ => Iff.rfl) e).trans ?_
    exact (den_sub t (UL.single u) ρ e).symm
  | appL b _ ih => exact fun ρ e =>
      ⟨fun ⟨l, hl, ha⟩ => ⟨l, hl, (ih ρ _).mp ha⟩, fun ⟨l, hl, ha⟩ => ⟨l, hl, (ih ρ _).mpr ha⟩⟩
  | appR a _ ih => exact fun ρ e =>
      ⟨fun ⟨l, hl, ha⟩ => ⟨l, fun e' m => (ih ρ e').mp (hl e' m), ha⟩,
       fun ⟨l, hl, ha⟩ => ⟨l, fun e' m => (ih ρ e').mpr (hl e' m), ha⟩⟩
  | lam _ ih => exact fun ρ e => match e with
      | .atom _ => Iff.rfl
      | .arr l b => ih (consD (setL l) ρ) b

end GM

end Found

#print axioms Found.GM.fd
#print axioms Found.GM.beta_den
#print axioms Found.GM.den_sub
#print axioms Found.GM.sound

found/Grothendieck.lean

lean_all/found/Grothendieck.lean

/-
  T104. found/Grothendieck — Grothendieck universes as a point: the type
  universe levels of the meta-language give a universe set inside the next
  level of Aczel's V (T109, now universe-polymorphic).

  U := { lift a | a ∈ V.{0} } ∈ V.{1}.
  * lift respects ≈; membership in a lifted set is lifting of members.
  * U is transitive, and closed under pairing, union and power set (each via
    extensionality), so U is a Grothendieck universe; an inaccessible-like
    point in the set-theory coordinate.
  Lean 4 core only, hand-written proofs, no axioms.
  CIC dependence: universe levels Type 0 : Type 1 (the meta-language's hierarchy), impredicative Prop (power).
-/
import found.IZF

namespace Found

namespace Groth

open WF

def lift : V.{0} → V.{1}
  | .sup I f => .sup (ULift I) (fun i => lift (f i.down))

theorem lift_eq : (x y : V.{0}) → Eq' x y → Eq' (lift x) (lift y)
  | .sup _ _, .sup _ _, ⟨e1, e2⟩ =>
    ⟨fun i => match e1 i.down with | ⟨j, e⟩ => ⟨⟨j⟩, lift_eq _ _ e⟩,
     fun j => match e2 j.down with | ⟨i, e⟩ => ⟨⟨i⟩, lift_eq _ _ e⟩⟩

theorem mem_lift (z : V.{1}) (w : V.{0}) : Mem z (lift w) ↔ ∃ v : V.{0}, Mem v w ∧ Eq' z (lift v) := by
  cases w with
  | sup I f =>
    exact ⟨fun ⟨i, e⟩ => ⟨f i.down, ⟨i.down, eq_refl _⟩, e⟩,
           fun ⟨_, ⟨i, ev⟩, e⟩ => ⟨⟨i⟩, eq_trans _ _ _ e (lift_eq _ _ ev)⟩⟩

def U : V.{1} := .sup V.{0} lift

theorem mem_U (x : V.{1}) : Mem x U ↔ ∃ a : V.{0}, Eq' x (lift a) := Iff.rfl

/-- U is transitive -/
theorem U_trans (x y : V.{1}) (m : Mem x y) (hy : Mem y U) : Mem x U :=
  match hy with
  | ⟨a, e⟩ => match (mem_lift x a).mp (mem_resp_right m e) with
    | ⟨v, _, ev⟩ => ⟨v, ev⟩

/-- closure under pairing -/
theorem U_pair (x y : V.{1}) (hx : Mem x U) (hy : Mem y U) : Mem (IZF.pair x y) U :=
  match hx, hy with
  | ⟨a, ea⟩, ⟨b, eb⟩ => ⟨IZF.pair a b, extensionality _ _ (fun z =>
      ⟨fun m => match (IZF.pair_spec x y z).mp m with
         | Or.inl e => (mem_lift z _).mpr ⟨a, (IZF.pair_spec a b a).mpr (Or.inl (eq_refl _)), eq_trans _ _ _ e ea⟩
         | Or.inr e => (mem_lift z _).mpr ⟨b, (IZF.pair_spec a b b).mpr (Or.inr (eq_refl _)), eq_trans _ _ _ e eb⟩,
       fun m => match (mem_lift z _).mp m with
         | ⟨v, mv, e⟩ => (IZF.pair_spec x y z).mpr (((IZF.pair_spec a b v).mp mv).elim
             (fun ev => Or.inl (eq_trans _ _ _ e (eq_trans _ _ _ (lift_eq _ _ ev) (eq_symm _ _ ea))))
             (fun ev => Or.inr (eq_trans _ _ _ e (eq_trans _ _ _ (lift_eq _ _ ev) (eq_symm _ _ eb)))))⟩)⟩

/-- closure under union -/
theorem U_union (x : V.{1}) (hx : Mem x U) : Mem (IZF.union x) U :=
  match hx with
  | ⟨a, ea⟩ => ⟨IZF.union a, extensionality _ _ (fun z =>
      ⟨fun m => match (IZF.union_spec x z).mp m with
         | ⟨y, my, mz⟩ => match (mem_lift y a).mp (mem_resp_right my ea) with
           | ⟨v, mv, ev⟩ => match (mem_lift z v).mp (mem_resp_right mz ev) with
             | ⟨u, mu, eu⟩ => (mem_lift z _).mpr ⟨u, (IZF.union_spec a u).mpr ⟨v, mv, mu⟩, eu⟩,
       fun m => match (mem_lift z _).mp m with
         | ⟨u, mu, eu⟩ => match (IZF.union_spec a u).mp mu with
           | ⟨v, mv, muv⟩ => (IZF.union_spec x z).mpr ⟨lift v,
               mem_resp_right ((mem_lift (lift v) a).mpr ⟨v, mv, eq_refl _⟩) (eq_symm _ _ ea),
               mem_resp_left (eq_symm _ _ eu) ((mem_lift (lift u) v).mpr ⟨u, muv, eq_refl _⟩)⟩⟩)⟩

/-- closure under power set -/
theorem U_power (x : V.{1}) (hx : Mem x U) : Mem (IZF.power x) U :=
  match hx with
  | ⟨a, ea⟩ => ⟨IZF.power a, extensionality _ _ (fun z =>
      ⟨fun m =>
        have sub : ∀ w, Mem w z → Mem w x := (IZF.power_spec x z).mp m
        let φ : V.{0} → Prop := fun u => Mem (lift u) z
        have hφ : IZF.Resp φ := fun _ _ e h => mem_resp_left (lift_eq _ _ e) h
        (mem_lift z _).mpr ⟨IZF.sep a φ,
          (IZF.power_spec a _).mpr (fun u mu => ((IZF.sep_spec a φ hφ u).mp mu).1),
          extensionality _ _ (fun w =>
            ⟨fun mw => match (mem_lift w a).mp (mem_resp_right (sub w mw) ea) with
               | ⟨u, mu, eu⟩ => (mem_lift w _).mpr ⟨u, (IZF.sep_spec a φ hφ u).mpr
                   ⟨mu, mem_resp_left eu mw⟩, eu⟩,
             fun mw => match (mem_lift w _).mp mw with
               | ⟨u, mu, eu⟩ => mem_resp_left (eq_symm _ _ eu) ((IZF.sep_spec a φ hφ u).mp mu).2⟩)⟩,
       fun m => match (mem_lift z _).mp m with
         | ⟨v, mv, ev⟩ => (IZF.power_spec x z).mpr (fun w mw =>
             match (mem_lift w v).mp (mem_resp_right mw ev) with
             | ⟨u, mu, eu⟩ => mem_resp_right ((mem_lift w a).mpr
                 ⟨u, (IZF.power_spec a v).mp mv u mu, eu⟩) (eq_symm _ _ ea))⟩)⟩

end Groth

end Found

#print axioms Found.Groth.lift_eq
#print axioms Found.Groth.mem_lift
#print axioms Found.Groth.U_trans
#print axioms Found.Groth.U_pair
#print axioms Found.Groth.U_union
#print axioms Found.Groth.U_power

found/Gt1.lean

lean_all/found/Gt1.lean

/-
  T194 (classifying topos), part 1. found/Gt1 — Grothendieck topologies on
  an E-category (setoid hom-sets) and sheaves of setoids:
  * a topology assigns covering sieves; they respect sieve equality,
    contain the maximal sieve, are stable under pullback and transitive;
    hence the intersection of two covering sieves covers and any sieve
    containing a covering sieve covers;
  * matching families on a sieve, amalgamations, the sheaf condition
    (existence and uniqueness up to the setoid equality);
  * separated presheaves.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Sheaf

namespace Found

namespace Gt

open Sh

variable {C : ECat.{0, 0}}

def maxS (c : C.Ob) : Sieve C c := ⟨fun _ => True, fun _ _ => trivial, fun _ _ => trivial⟩

def pullS {c d : C.Ob} (f : C.Hom d c) (S : Sieve C c) : Sieve C d :=
  ⟨fun g => S.S (C.comp g f), fun e h => S.resp (C.lcong e f) h,
   fun h g => S.resp (C.hsymm (C.assoc _ _ _)) (S.stable h g)⟩

def meetS {c : C.Ob} (S T : Sieve C c) : Sieve C c :=
  ⟨fun f => S.S f ∧ T.S f, fun e h => ⟨S.resp e h.1, T.resp e h.2⟩, fun h g => ⟨S.stable h.1 g, T.stable h.2 g⟩⟩

def SEq {c : C.Ob} (S T : Sieve C c) : Prop := ∀ d (f : C.Hom d c), S.S f ↔ T.S f

structure GTop (C : ECat.{0, 0}) where
  cov : {c : C.Ob} → Sieve C c → Prop
  resp : ∀ {c} {S T : Sieve C c}, SEq S T → cov S → cov T
  top : ∀ c, cov (maxS c)
  pull : ∀ {c d} (f : C.Hom d c) {S : Sieve C c}, cov S → cov (pullS f S)
  trans : ∀ {c} {S R : Sieve C c}, cov S → (∀ d (f : C.Hom d c), S.S f → cov (pullS f R)) → cov R

variable (J : GTop C)

/-- a sieve containing a covering sieve covers -/
theorem cov_mono {c : C.Ob} {S R : Sieve C c} (hS : J.cov S) (h : ∀ d (f : C.Hom d c), S.S f → R.S f) : J.cov R :=
  J.trans hS (fun d f hf => J.resp (fun _ g => ⟨fun _ => R.stable (h d f hf) g, fun _ => trivial⟩) (J.top d))

/-- the intersection of two covering sieves covers -/
theorem cov_meet {c : C.Ob} {S T : Sieve C c} (hS : J.cov S) (hT : J.cov T) : J.cov (meetS S T) :=
  J.trans hS (fun _ f hf => J.resp (fun _ g => ⟨fun h => ⟨S.stable hf g, h⟩, fun h => h.2⟩) (J.pull f hT))

variable (P : Psh.{0, 0, 0} C)

/-- a matching family on S -/
structure MFam {c : C.Ob} (S : Sieve C c) where
  x : {d : C.Ob} → (f : C.Hom d c) → S.S f → (P.obj d).car
  resp : ∀ {d} {f f' : C.Hom d c} (hf : S.S f) (hf' : S.S f'), C.heq f f' → (P.obj d).eqv (x f hf) (x f' hf')
  compat : ∀ {d e} (f : C.Hom d c) (hf : S.S f) (g : C.Hom e d),
    (P.obj e).eqv (x (C.comp g f) (S.stable hf g)) (P.map g (x f hf))

def Amalg {c : C.Ob} {S : Sieve C c} (m : MFam P S) (a : (P.obj c).car) : Prop :=
  ∀ d (f : C.Hom d c) (hf : S.S f), (P.obj d).eqv (P.map f a) (m.x f hf)

def IsSheaf : Prop :=
  ∀ c (S : Sieve C c), J.cov S → ∀ m : MFam P S, (∃ a, Amalg P m a) ∧ ∀ a b, Amalg P m a → Amalg P m b → (P.obj c).eqv a b

def Separated : Prop :=
  ∀ c (S : Sieve C c), J.cov S → ∀ a b : (P.obj c).car,
    (∀ d (f : C.Hom d c), S.S f → (P.obj d).eqv (P.map f a) (P.map f b)) → (P.obj c).eqv a b

theorem sheaf_sep (h : IsSheaf J P) : Separated J P := fun c S hS a b hab =>
  let m : MFam P S := ⟨fun f _ => P.map f b, fun _ _ e => P.map_resp b e,
    fun f _ g => P.map_comp g f b⟩
  (h c S hS m).2 a b (fun d f hf => hab d f hf) (fun d _ _ => (P.obj d).refl _)

end Gt

end Found

#print axioms Found.Gt.cov_mono
#print axioms Found.Gt.cov_meet
#print axioms Found.Gt.sheaf_sep

found/Gt10.lean

lean_all/found/Gt10.lean

/-
  T194 (classifying topos), part 10. found/Gt10 — renaming of variables in
  geometric formulas and its forcing lemma (forcing a renamed formula is
  forcing the formula at the composed valuation); iterated ∃; sheaf models
  of a geometric theory over arbitrary small sites, and semantic entailment
  (truth in every sheaf model at every stage).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt9

namespace Found

namespace Gt

open Sh

def upR (σ : Nat → Nat) : Nat → Nat
  | 0 => 0
  | n + 1 => σ n + 1

def renG (σ : Nat → Nat) : GFm → GFm
  | .atom R l => .atom R (lmapG σ l)
  | .eq i j => .eq (σ i) (σ j)
  | .top => .top
  | .conj a b => .conj (renG σ a) (renG σ b)
  | .sup φ => .sup (fun n => renG σ (φ n))
  | .ex φ => .ex (renG (upR σ) φ)

def exN : Nat → GFm → GFm
  | 0, φ => φ
  | m + 1, φ => .ex (exN m φ)

section F

variable {C : ECat.{0, 0}} (J : GTop C) (M : @GStr C)

theorem force_eqval {c : C.Ob} {ρ ρ' : Nat → (M.P.obj c).car} (h : ∀ n, ρ n = ρ' n) (φ : GFm) :
    force J M c ρ φ → force J M c ρ' φ :=
  force_resp J M φ (fun n => by rw [h n]; exact (M.P.obj c).refl _)

theorem lmapG_lmapG {X Y Z : Type} (f : X → Y) (g : Y → Z) (l : List X) :
    lmapG g (lmapG f l) = lmapG (fun x => g (f x)) l := lmapG_comp f g l

theorem force_ren : (φ : GFm) → (σ : Nat → Nat) → ∀ {c : C.Ob} (ρ : Nat → (M.P.obj c).car),
    force J M c ρ (renG σ φ) ↔ force J M c (fun n => ρ (σ n)) φ
  | .atom R l, σ, c, ρ => by
    show M.rel R c (lmapG ρ (lmapG σ l)) ↔ M.rel R c (lmapG (fun n => ρ (σ n)) l)
    rw [lmapG_lmapG]
  | .eq _ _, _, _, _ => Iff.rfl
  | .top, _, _, _ => Iff.rfl
  | .conj a b, σ, _, ρ => ⟨fun ⟨x, y⟩ => ⟨(force_ren a σ ρ).1 x, (force_ren b σ ρ).1 y⟩,
      fun ⟨x, y⟩ => ⟨(force_ren a σ ρ).2 x, (force_ren b σ ρ).2 y⟩⟩
  | .sup φ, σ, _, ρ => ⟨fun ⟨S, hS, g⟩ => ⟨S, hS, fun d f hf => match g d f hf with
        | ⟨i, fi⟩ => ⟨i, (force_ren (φ i) σ _).1 fi⟩⟩,
      fun ⟨S, hS, g⟩ => ⟨S, hS, fun d f hf => match g d f hf with
        | ⟨i, fi⟩ => ⟨i, (force_ren (φ i) σ _).2 fi⟩⟩⟩
  | .ex φ, σ, _, ρ =>
    have e : ∀ {d : C.Ob} (ρ' : Nat → (M.P.obj d).car) (a : (M.P.obj d).car) n,
        extV M ρ' a (upR σ n) = extV M (fun k => ρ' (σ k)) a n := fun _ _ n => match n with
      | 0 => rfl
      | _ + 1 => rfl
    ⟨fun ⟨S, hS, g⟩ => ⟨S, hS, fun d f hf => match g d f hf with
        | ⟨a, fa⟩ => ⟨a, force_eqval J M (e _ a) φ ((force_ren φ (upR σ) _).1 fa)⟩⟩,
      fun ⟨S, hS, g⟩ => ⟨S, hS, fun d f hf => match g d f hf with
        | ⟨a, fa⟩ => ⟨a, (force_ren φ (upR σ) _).2 (force_eqval J M (fun n => (e _ a n).symm) φ fa)⟩⟩⟩

end F

/-- a sheaf model of a geometric theory over some small site -/
structure SModel (T : Seq → Prop) where
  D : ECat.{0, 0}
  K : GTop D
  M : @GStr D
  sheaf : IsSheaf K M.P
  closed : Closed K M
  model : IsModel K M T

/-- semantic entailment: φ ⊨_T ψ in every sheaf model at every stage -/
def Ent (T : Seq → Prop) (φ ψ : GFm) : Prop :=
  ∀ (N : SModel T) (c : N.D.Ob) (ρ : Nat → (N.M.P.obj c).car), force N.K N.M c ρ φ → force N.K N.M c ρ ψ

theorem ent_refl (T : Seq → Prop) (φ : GFm) : Ent T φ φ := fun _ _ _ h => h

theorem ent_trans {T : Seq → Prop} {φ ψ χ : GFm} (h1 : Ent T φ ψ) (h2 : Ent T ψ χ) : Ent T φ χ :=
  fun N c ρ h => h2 N c ρ (h1 N c ρ h)

theorem ent_ax {T : Seq → Prop} {σ : Seq} (h : T σ) : Ent T σ.lhs σ.rhs := fun N c ρ f => N.model σ h c ρ f

end Gt

end Found

#print axioms Found.Gt.force_ren
#print axioms Found.Gt.ent_trans
#print axioms Found.Gt.ent_ax

found/Gt11.lean

lean_all/found/Gt11.lean

/-
  T194 (classifying topos), part 11. found/Gt11 — forcing iterated
  existentials: extending valuations by lists of witnesses, the sieve of
  stages with witnesses, and (in a closed structure) ∃^m φ is forced at ρ
  iff that sieve covers.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt10

namespace Found

namespace Gt

open Sh

variable {C : ECat.{0, 0}} (J : GTop C) (M : @GStr C)

/-- extending a valuation by a list of values (the last one becomes variable 0) -/
def extLV {c : C.Ob} (ρ : Nat → (M.P.obj c).car) : List (M.P.obj c).car → Nat → (M.P.obj c).car
  | [] => ρ
  | a :: ds => extLV (extV M ρ a) ds

def lenG {X : Type} : List X → Nat
  | [] => 0
  | _ :: l => lenG l + 1

theorem len_lmapG {X Y : Type} (f : X → Y) : (l : List X) → lenG (lmapG f l) = lenG l
  | [] => rfl
  | _ :: l => congrArg (· + 1) (len_lmapG f l)

theorem extLV_eqv {d : C.Ob} {ρ ρ' : Nat → (M.P.obj d).car} (h : ∀ n, (M.P.obj d).eqv (ρ n) (ρ' n)) :
    (ds : List (M.P.obj d).car) → ∀ n, (M.P.obj d).eqv (extLV M ρ ds n) (extLV M ρ' ds n)
  | [] => h
  | a :: ds => extLV_eqv (extV_eqv M h a) ds

theorem res_extLV {d e : C.Ob} (k : C.Hom e d) : (ρ0 : Nat → (M.P.obj d).car) → (ds : List (M.P.obj d).car) →
    ∀ n, (M.P.obj e).eqv (resV M k (extLV M ρ0 ds) n) (extLV M (resV M k ρ0) (lmapG (M.P.map k) ds) n)
  | _, [] => fun _ => (M.P.obj e).refl _
  | ρ0, a :: ds => fun n => (M.P.obj e).trans (res_extLV k (extV M ρ0 a) ds n)
      (extLV_eqv M (ext_res M k ρ0 a) _ n)

/-- the sieve of stages with witnesses -/
def witS {c : C.Ob} (ρ : Nat → (M.P.obj c).car) (m : Nat) (φ : GFm) : Sieve C c where
  S := fun g => ∃ ds, lenG ds = m ∧ force J M _ (extLV M (resV M g ρ) ds) φ
  resp := fun e ⟨ds, hl, f⟩ => ⟨ds, hl, force_resp J M φ (extLV_eqv M (fun n => M.P.map_resp (ρ n) e) ds) f⟩
  stable := fun {_ _} {g} ⟨ds, hl, f⟩ k => ⟨lmapG (M.P.map k) ds, (len_lmapG _ ds).trans hl,
    force_resp J M φ (fun n => (M.P.obj _).trans (res_extLV M k (resV M g ρ) ds n)
      (extLV_eqv M (res_comp M g k ρ) _ n)) (force_stable J M φ k _ f)⟩

/-- witnesses at the identity give ∃^m -/
theorem exN_of (φ : GFm) : (m : Nat) → ∀ {c : C.Ob} (ρ : Nat → (M.P.obj c).car) (ds : List (M.P.obj c).car),
    lenG ds = m → force J M c (extLV M ρ ds) φ → force J M c ρ (exN m φ)
  | 0, _, _, [], _, f => f
  | m + 1, _, ρ, a :: ds, e, f => ⟨maxS _, J.top _, fun d g _ => ⟨M.P.map g a, by
      have h := exN_of φ m (extV M ρ a) ds (Nat.succ.inj e) f
      exact force_resp J M (exN m φ) (ext_res M g ρ a) (force_stable J M (exN m φ) g _ h)⟩⟩
  | 0, _, _, _ :: _, e, _ => Nat.noConfusion e
  | _ + 1, _, _, [], e, _ => Nat.noConfusion e

/-- local witnesses give ∃^m in a closed structure -/
theorem exN_local (hc : Closed J M) (φ : GFm) (m : Nat) {c : C.Ob} (ρ : Nat → (M.P.obj c).car)
    (hW : J.cov (witS J M ρ m φ)) : force J M c ρ (exN m φ) :=
  force_local J M hc (exN m φ) ρ (witS J M ρ m φ) hW (fun _ _ ⟨ds, hl, f⟩ => exN_of J M φ m _ ds hl f)

/-- ∃^m forced gives local witnesses -/
theorem exN_wit (φ : GFm) : (m : Nat) → ∀ {c : C.Ob} (ρ : Nat → (M.P.obj c).car),
    force J M c ρ (exN m φ) → J.cov (witS J M ρ m φ)
  | 0, c, ρ, f => cov_mono J (J.top c) (fun _ g _ => ⟨[], rfl, force_stable J M φ g ρ f⟩)
  | m + 1, _, ρ, ⟨_, hS, g⟩ => J.trans hS (fun d f hf => match g d f hf with
      | ⟨a, fa⟩ => cov_mono J (exN_wit φ m (extV M (resV M f ρ) a) fa) (fun e k ⟨ds, hl, fk⟩ =>
          ⟨M.P.map k a :: ds, congrArg (· + 1) hl, force_resp J M φ (extLV_eqv M (ρ := resV M k (extV M (resV M f ρ) a))
            (ρ' := extV M (resV M (C.comp k f) ρ) (M.P.map k a)) (fun n => match n with
            | 0 => (M.P.obj e).refl _
            | n + 1 => res_comp M f k ρ n) ds) fk⟩))

end Gt

end Found

#print axioms Found.Gt.res_extLV
#print axioms Found.Gt.witS
#print axioms Found.Gt.exN_of
#print axioms Found.Gt.exN_local
#print axioms Found.Gt.exN_wit

found/Gt12.lean

lean_all/found/Gt12.lean

/-
  T194 (classifying topos), part 12. found/Gt12 — free-variable bounds for
  geometric formulas and the agreement lemma: valuations that agree (up to
  the setoid) below the bound force the same formulas.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt11

namespace Found

namespace Gt

open Sh

def AllLtG (k : Nat) : List Nat → Prop
  | [] => True
  | a :: l => a < k ∧ AllLtG k l

/-- all free variables below k -/
def FVg : Nat → GFm → Prop
  | k, .atom _ l => AllLtG k l
  | k, .eq i j => i < k ∧ j < k
  | _, .top => True
  | k, .conj a b => FVg k a ∧ FVg k b
  | k, .sup φ => ∀ n, FVg k (φ n)
  | k, .ex φ => FVg (k + 1) φ

variable {C : ECat.{0, 0}} (J : GTop C) (M : @GStr C)

theorem leq_map_lt {c : C.Ob} {k : Nat} {ρ ρ' : Nat → (M.P.obj c).car} (h : ∀ n, n < k → (M.P.obj c).eqv (ρ n) (ρ' n)) :
    (l : List Nat) → AllLtG k l → LEq M.P c (lmapG ρ l) (lmapG ρ' l)
  | [], _ => trivial
  | n :: l, hl => ⟨h n hl.1, leq_map_lt h l hl.2⟩

theorem force_agree : (φ : GFm) → (k : Nat) → FVg k φ → ∀ {c : C.Ob} {ρ ρ' : Nat → (M.P.obj c).car},
    (∀ n, n < k → (M.P.obj c).eqv (ρ n) (ρ' n)) → force J M c ρ φ → force J M c ρ' φ
  | .atom R l, _, hf, c, _, _, h, f => M.rel_resp R c _ _ (leq_map_lt M h l hf) f
  | .eq i j, _, hf, c, _, _, h, f => (M.P.obj c).trans ((M.P.obj c).symm (h i hf.1)) ((M.P.obj c).trans f (h j hf.2))
  | .top, _, _, _, _, _, _, f => f
  | .conj a b, k, hf, _, _, _, h, f => ⟨force_agree a k hf.1 h f.1, force_agree b k hf.2 h f.2⟩
  | .sup φ, k, hf, _, _, _, h, ⟨S, hS, g⟩ => ⟨S, hS, fun d f hf' => match g d f hf' with
      | ⟨i, fi⟩ => ⟨i, force_agree (φ i) k (hf i) (fun n hn => M.P.map_eqv f (h n hn)) fi⟩⟩
  | .ex φ, k, hf, _, _, _, h, ⟨S, hS, g⟩ => ⟨S, hS, fun d f hf' => match g d f hf' with
      | ⟨a, fa⟩ => ⟨a, force_agree φ (k + 1) hf (fun n hn => match n, hn with
          | 0, _ => (M.P.obj d).refl a
          | n + 1, hn => M.P.map_eqv f (h n (Nat.lt_of_succ_lt_succ hn))) fa⟩⟩

end Gt

end Found

#print axioms Found.Gt.force_agree

found/Gt13.lean

lean_all/found/Gt13.lean

/-
  T194 (classifying topos), part 13. found/Gt13 — blocks of variables:
  * after extending a valuation by a list of length n, variable i + n is the
    old variable i, and the variables below n do not depend on the old
    valuation;
  * for any target function there is a list placing its first n values in
    the newest n variables;
  * the equality block "x_i = x_{i+n} for i < n" and its forcing.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt12

namespace Found

namespace Gt

open Sh

variable {C : ECat.{0, 0}} (J : GTop C) (M : @GStr C)

theorem extLV_off {c : C.Ob} : (ρ : Nat → (M.P.obj c).car) → (ds : List (M.P.obj c).car) → ∀ i,
    extLV M ρ ds (i + lenG ds) = ρ i
  | _, [], _ => rfl
  | ρ, a :: ds, i => by
    show extLV M (extV M ρ a) ds ((i + lenG ds) + 1) = ρ i
    have h := extLV_off (extV M ρ a) ds (i + 1)
    rw [Nat.succ_add] at h
    exact h

theorem extLV_lt {c : C.Ob} : (ρ ρ' : Nat → (M.P.obj c).car) → (ds : List (M.P.obj c).car) → ∀ i, i < lenG ds →
    extLV M ρ ds i = extLV M ρ' ds i
  | _, _, [], _, h => (Nat.not_lt_zero _ h).elim
  | ρ, ρ', a :: ds, i, h => match Nat.decEq i (lenG ds) with
    | isTrue e => by
      show extLV M (extV M ρ a) ds i = extLV M (extV M ρ' a) ds i
      have h1 := extLV_off M (extV M ρ a) ds 0
      have h2 := extLV_off M (extV M ρ' a) ds 0
      rw [Nat.zero_add] at h1 h2
      rw [e, h1, h2]; rfl
    | isFalse ne => extLV_lt (extV M ρ a) (extV M ρ' a) ds i (Nat.lt_of_le_of_ne (Nat.le_of_lt_succ h) ne)

/-- a list placing τ 0, ..., τ (n-1) in the newest variables -/
def witL {c : C.Ob} (τ : Nat → (M.P.obj c).car) : Nat → List (M.P.obj c).car
  | 0 => []
  | n + 1 => τ n :: witL τ n

theorem len_witL {c : C.Ob} (τ : Nat → (M.P.obj c).car) : (n : Nat) → lenG (witL M τ n) = n
  | 0 => rfl
  | n + 1 => congrArg (· + 1) (len_witL τ n)

theorem witL_val {c : C.Ob} (τ : Nat → (M.P.obj c).car) : (n : Nat) → ∀ (β : Nat → (M.P.obj c).car) i, i < n →
    extLV M β (witL M τ n) i = τ i
  | 0, _, _, h => (Nat.not_lt_zero _ h).elim
  | n + 1, β, i, h => match Nat.decEq i n with
    | isTrue e => by
      show extLV M (extV M β (τ n)) (witL M τ n) i = τ i
      have h1 := extLV_off M (extV M β (τ n)) (witL M τ n) 0
      rw [Nat.zero_add, len_witL] at h1
      rw [e, h1]; rfl
    | isFalse ne => by
      show extLV M (extV M β (τ n)) (witL M τ n) i = τ i
      exact witL_val τ n _ i (Nat.lt_of_le_of_ne (Nat.le_of_lt_succ h) ne)

/-- finite conjunction of geometric formulas -/
def conjG : List GFm → GFm
  | [] => .top
  | a :: l => .conj a (conjG l)

theorem force_conjG {c : C.Ob} (ρ : Nat → (M.P.obj c).car) : (l : List GFm) →
    (force J M c ρ (conjG l) ↔ ∀ x, x ∈ l → force J M c ρ x)
  | [] => ⟨(fun _ _ m => nomatch m), (fun _ => trivial)⟩
  | a :: l => ⟨fun ⟨h1, h2⟩ x m => by
      cases m with
      | head => exact h1
      | tail _ m' => exact (force_conjG ρ l).1 h2 x m',
    fun h => ⟨h a (.head _), (force_conjG ρ l).2 (fun x m => h x (.tail _ m))⟩⟩

def rangeG : Nat → List Nat
  | 0 => []
  | k + 1 => k :: rangeG k

theorem mem_rangeG : (k : Nat) → ∀ i, i ∈ rangeG k → i < k
  | 0, _, h => nomatch h
  | k + 1, i, h => by
    cases h with
    | head => exact Nat.lt_succ_self k
    | tail _ h' => exact Nat.lt_succ_of_lt (mem_rangeG k i h')

theorem rangeG_mem : (k : Nat) → ∀ i, i < k → i ∈ rangeG k
  | 0, _, h => (Nat.not_lt_zero _ h).elim
  | k + 1, i, h => match Nat.decEq i k with
    | isTrue e => by rw [e]; exact .head _
    | isFalse ne => .tail _ (rangeG_mem k i (Nat.lt_of_le_of_ne (Nat.le_of_lt_succ h) ne))

/-- x_i = x_{i+n} for i < n -/
def eqBlock (n : Nat) : GFm := conjG (lmapG (fun i => GFm.eq i (i + n)) (rangeG n))

theorem mem_lmapG {X Y : Type} {f : X → Y} {y : Y} : (l : List X) → y ∈ lmapG f l → ∃ x, x ∈ l ∧ y = f x
  | [], h => nomatch h
  | x :: l, h => by
    cases h with
    | head => exact ⟨x, .head _, rfl⟩
    | tail _ m => match mem_lmapG l m with | ⟨x', hx, e⟩ => exact ⟨x', .tail _ hx, e⟩

theorem lmapG_mem {X Y : Type} (f : X → Y) {x : X} : (l : List X) → x ∈ l → f x ∈ lmapG f l
  | _ :: _, .head _ => .head _
  | _ :: l, .tail _ m => .tail _ (lmapG_mem f l m)

theorem force_eqBlock {c : C.Ob} (n : Nat) (ρ : Nat → (M.P.obj c).car) :
    force J M c ρ (eqBlock n) ↔ ∀ i, i < n → (M.P.obj c).eqv (ρ i) (ρ (i + n)) :=
  (force_conjG J M ρ _).trans ⟨fun h i hi => h _ (lmapG_mem _ _ (rangeG_mem n i hi)),
    fun h x hx => match mem_lmapG _ hx with
      | ⟨i, hi, e⟩ => by rw [e]; exact h i (mem_rangeG n i hi)⟩

end Gt

end Found

#print axioms Found.Gt.extLV_off
#print axioms Found.Gt.extLV_lt
#print axioms Found.Gt.witL_val
#print axioms Found.Gt.force_conjG
#print axioms Found.Gt.force_eqBlock

found/Gt14.lean

lean_all/found/Gt14.lean

/-
  T194 (classifying topos), part 14. found/Gt14 — the syntactic category
  of a geometric theory T, read semantically (truth in all sheaf models):
  * objects: formulas in context {x̄ . φ} (free variables below the length);
  * arrows {x̄ . φ} → {ȳ . ψ}: formulas θ(x̄, ȳ) (targets ȳ newest) that are
    T-functional: θ implies φ(x̄) and ψ(ȳ), φ implies ∃ȳ θ, and θ is
    single-valued in ȳ;
  * the identity arrow φ(x̄) ∧ ȳ = x̄.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt13

namespace Found

namespace Gt

open Sh

theorem allLt_ren {k k' : Nat} {σ : Nat → Nat} (hσ : ∀ i, i < k → σ i < k') : (l : List Nat) → AllLtG k l →
    AllLtG k' (lmapG σ l)
  | [], _ => trivial
  | _ :: l, h => ⟨hσ _ h.1, allLt_ren hσ l h.2⟩

theorem fv_ren : (φ : GFm) → {k k' : Nat} → {σ : Nat → Nat} → (∀ i, i < k → σ i < k') → FVg k φ → FVg k' (renG σ φ)
  | .atom _ l, _, _, _, hσ, h => allLt_ren hσ l h
  | .eq _ _, _, _, _, hσ, h => ⟨hσ _ h.1, hσ _ h.2⟩
  | .top, _, _, _, _, _ => trivial
  | .conj a b, _, _, _, hσ, h => ⟨fv_ren a hσ h.1, fv_ren b hσ h.2⟩
  | .sup φ, _, _, _, hσ, h => fun n => fv_ren (φ n) hσ (h n)
  | .ex φ, _, _, σ, hσ, h => fv_ren φ (σ := upR σ) (fun i hi => match i, hi with
      | 0, _ => Nat.zero_lt_succ _
      | i + 1, hi => Nat.succ_lt_succ (hσ i (Nat.lt_of_succ_lt_succ hi))) h

theorem fv_conjG (k : Nat) : (l : List GFm) → (∀ x, x ∈ l → FVg k x) → FVg k (conjG l)
  | [], _ => trivial
  | a :: l, h => ⟨h a (.head _), fv_conjG k l (fun x m => h x (.tail _ m))⟩

theorem fv_eqBlock (n : Nat) : FVg (n + n) (eqBlock n) :=
  fv_conjG _ _ (fun x hx => match mem_lmapG _ hx with
    | ⟨i, hi, e⟩ => by
      rw [e]
      have h := mem_rangeG n i hi
      exact ⟨Nat.lt_of_lt_of_le h (Nat.le_add_right n n), Nat.add_lt_add_right h n⟩)

variable (T : Seq → Prop)

structure SObj where
  n : Nat
  φ : GFm
  fv : FVg n φ

/-- θ is a T-functional relation from A to B -/
def IsArr (A B : SObj) (θ : GFm) : Prop :=
  FVg (B.n + A.n) θ ∧
  (∀ (N : SModel T) c (ρ : Nat → (N.M.P.obj c).car) ys, lenG ys = B.n →
    force N.K N.M c (extLV N.M ρ ys) θ → force N.K N.M c ρ A.φ ∧ force N.K N.M c (extLV N.M ρ ys) B.φ) ∧
  (∀ (N : SModel T) c (ρ : Nat → (N.M.P.obj c).car), force N.K N.M c ρ A.φ → force N.K N.M c ρ (exN B.n θ)) ∧
  (∀ (N : SModel T) c (ρ : Nat → (N.M.P.obj c).car) ys ys', lenG ys = B.n → lenG ys' = B.n →
    force N.K N.M c (extLV N.M ρ ys) θ → force N.K N.M c (extLV N.M ρ ys') θ →
    ∀ i, i < B.n → (N.M.P.obj c).eqv (extLV N.M ρ ys i) (extLV N.M ρ ys' i))

def idF (A : SObj) : GFm := .conj (renG (· + A.n) A.φ) (eqBlock A.n)

theorem shift_val {D : ECat.{0, 0}} (M : @GStr D) {c : D.Ob} (ρ : Nat → (M.P.obj c).car) (ys : List (M.P.obj c).car)
    (n : Nat) (hl : lenG ys = n) : ∀ i, extLV M ρ ys (i + n) = ρ i := fun i => by
  rw [← hl]; exact extLV_off M ρ ys i

theorem id_arr (A : SObj) : IsArr T A A (idF A) := by
  refine ⟨⟨fv_ren A.φ (fun i hi => Nat.add_lt_add_right hi A.n) A.fv, fv_eqBlock A.n⟩, ?_, ?_, ?_⟩
  · intro N c ρ ys hl ⟨h1, h2⟩
    have hφ : force N.K N.M c ρ A.φ :=
      force_eqval N.K N.M (shift_val N.M ρ ys A.n hl) A.φ ((force_ren N.K N.M A.φ _ _).1 h1)
    have eb := (force_eqBlock N.K N.M A.n _).1 h2
    refine ⟨hφ, force_agree N.K N.M A.φ A.n A.fv (fun i hi => ?_) hφ⟩
    have e := eb i hi
    rw [shift_val N.M ρ ys A.n hl i] at e
    exact (N.M.P.obj c).symm e
  · intro N c ρ hφ
    refine exN_of N.K N.M _ A.n ρ (witL N.M ρ A.n) (len_witL N.M ρ A.n) ⟨?_, ?_⟩
    · exact (force_ren N.K N.M A.φ _ _).2 (force_eqval N.K N.M
        (fun i => (shift_val N.M ρ (witL N.M ρ A.n) A.n (len_witL N.M ρ A.n) i).symm) A.φ hφ)
    · refine (force_eqBlock N.K N.M A.n _).2 (fun i hi => ?_)
      rw [witL_val N.M ρ A.n ρ i hi, shift_val N.M ρ (witL N.M ρ A.n) A.n (len_witL N.M ρ A.n) i]
      exact (N.M.P.obj c).refl _
  · intro N c ρ ys ys' hl hl' ⟨_, e1⟩ ⟨_, e2⟩ i hi
    have a1 := (force_eqBlock N.K N.M A.n _).1 e1 i hi
    have a2 := (force_eqBlock N.K N.M A.n _).1 e2 i hi
    rw [shift_val N.M ρ ys A.n hl i] at a1
    rw [shift_val N.M ρ ys' A.n hl' i] at a2
    exact (N.M.P.obj c).trans a1 ((N.M.P.obj c).symm a2)

end Gt

end Found

#print axioms Found.Gt.fv_ren
#print axioms Found.Gt.fv_eqBlock
#print axioms Found.Gt.id_arr

found/Gt15.lean

lean_all/found/Gt15.lean

/-
  T194 (classifying topos), part 15. found/Gt15 — the composite of
  syntactic arrows θ : {x̄.φ} → {ȳ.ψ} (|ȳ| = m) and η : {ȳ.ψ} → {z̄.χ}
  (|z̄| = k) is ∃ȳ (θ(x̄, ȳ) ∧ η(ȳ, z̄)); after the renamings, forcing the
  body at (ρ, z̄, ȳ) is forcing θ at (ρ, ȳ) and η at (ȳ, z̄).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt14

namespace Found

namespace Gt

open Sh

def subK : Nat → Nat → Nat
  | 0, i => i
  | _ + 1, 0 => 0
  | k + 1, i + 1 => subK k i

def subN (i k : Nat) : Nat := subK k i

theorem subN_add (j : Nat) : (k : Nat) → subN (j + k) k = j
  | 0 => rfl
  | k + 1 => subN_add j k

def σθ (m k : Nat) (i : Nat) : Nat :=
  match Nat.decLt i m with
  | isTrue _ => i
  | isFalse _ => i + k

def ση (m k : Nat) (i : Nat) : Nat :=
  match Nat.decLt i k with
  | isTrue _ => i + m
  | isFalse _ => subN i k

def compF (m k : Nat) (θ η : GFm) : GFm := exN m (.conj (renG (σθ m k) θ) (renG (ση m k) η))

theorem not_lt_split {i m : Nat} (h : ¬ i < m) : ∃ j, i = j + m :=
  match le_split (Nat.le_of_not_lt h) with
  | ⟨j, e⟩ => ⟨j, e.trans (Nat.add_comm m j)⟩
where
  le_split {a b : Nat} (hle : a ≤ b) : ∃ j, b = a + j := by
    induction hle with
    | refl => exact ⟨0, rfl⟩
    | step _ ih => match ih with | ⟨j, e⟩ => exact ⟨j + 1, by rw [e]; rfl⟩

variable {C : ECat.{0, 0}} (J : GTop C) (M : @GStr C)

theorem val_θ {c : C.Ob} (ρ : Nat → (M.P.obj c).car) (zs ys : List (M.P.obj c).car) (m k : Nat)
    (hm : lenG ys = m) (hk : lenG zs = k) :
    ∀ i, extLV M (extLV M ρ zs) ys (σθ m k i) = extLV M ρ ys i := fun i => by
  unfold σθ
  match Nat.decLt i m with
  | isTrue h => exact extLV_lt M _ _ ys i (by rw [hm]; exact h)
  | isFalse h =>
    match not_lt_split h with
    | ⟨j, e⟩ =>
      subst e
      show extLV M (extLV M ρ zs) ys (j + m + k) = extLV M ρ ys (j + m)
      rw [Nat.add_right_comm, shift_val M (extLV M ρ zs) ys m hm, shift_val M ρ zs k hk, shift_val M ρ ys m hm]

theorem val_η {c : C.Ob} (ρ : Nat → (M.P.obj c).car) (zs ys : List (M.P.obj c).car) (m k : Nat)
    (hm : lenG ys = m) (hk : lenG zs = k) :
    ∀ i, i < k + m → extLV M (extLV M ρ zs) ys (ση m k i) = extLV M (extLV M ρ ys) zs i := fun i hi => by
  unfold ση
  match Nat.decLt i k with
  | isTrue h =>
    show extLV M (extLV M ρ zs) ys (i + m) = extLV M (extLV M ρ ys) zs i
    rw [shift_val M (extLV M ρ zs) ys m hm]
    exact extLV_lt M _ _ zs i (by rw [hk]; exact h)
  | isFalse h =>
    match not_lt_split h with
    | ⟨j, e⟩ =>
      subst e
      show extLV M (extLV M ρ zs) ys (subN (j + k) k) = extLV M (extLV M ρ ys) zs (j + k)
      rw [subN_add, shift_val M (extLV M ρ ys) zs k hk]
      have hj : j < m := Nat.lt_of_add_lt_add_right (by rw [Nat.add_comm k m] at hi; exact hi)
      exact extLV_lt M _ _ ys j (by rw [hm]; exact hj)

/-- forcing the body of the composite -/
theorem force_body (θ η : GFm) (m k : Nat) (hη : FVg (k + m) η) {c : C.Ob} (ρ : Nat → (M.P.obj c).car)
    (zs ys : List (M.P.obj c).car) (hm : lenG ys = m) (hk : lenG zs = k) :
    force J M c (extLV M (extLV M ρ zs) ys) (.conj (renG (σθ m k) θ) (renG (ση m k) η)) ↔
      force J M c (extLV M ρ ys) θ ∧ force J M c (extLV M (extLV M ρ ys) zs) η := by
  have eθ := val_θ M ρ zs ys m k hm hk
  have eη := val_η M ρ zs ys m k hm hk
  constructor
  · intro ⟨h1, h2⟩
    exact ⟨force_eqval J M eθ θ ((force_ren J M θ _ _).1 h1),
      force_agree J M η (k + m) hη (fun i hi => by rw [eη i hi]; exact (M.P.obj c).refl _)
        ((force_ren J M η _ _).1 h2)⟩
  · intro ⟨h1, h2⟩
    exact ⟨(force_ren J M θ _ _).2 (force_eqval J M (fun i => (eθ i).symm) θ h1),
      (force_ren J M η _ _).2 (force_agree J M η (k + m) hη (fun i hi => by rw [eη i hi]; exact (M.P.obj c).refl _) h2)⟩

end Gt

end Found

#print axioms Found.Gt.val_θ
#print axioms Found.Gt.val_η
#print axioms Found.Gt.force_body

found/Gt16.lean

lean_all/found/Gt16.lean

/-
  T194 (classifying topos), part 16. found/Gt16 — the composite of two
  syntactic arrows is an arrow: its free variables are in range; it implies
  the source and target formulas (locally, then by local character); the
  source formula implies ∃z̄ of it (composing local witnesses); and it is
  single-valued (locally by single-valuedness of θ and η, then by
  separatedness of the carrier).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt15

namespace Found

namespace Gt

open Sh

theorem fv_exN : (m : Nat) → ∀ {k : Nat} {φ : GFm}, FVg (k + m) φ → FVg k (exN m φ)
  | 0, _, _, h => h
  | m + 1, k, φ, h => by
    show FVg (k + 1) (exN m φ)
    apply fv_exN m
    rw [Nat.add_right_comm k 1 m]
    exact h

variable {C : ECat.{0, 0}} (J : GTop C) (M : @GStr C)

/-- restriction of a block valuation -/
theorem res_block {c d : C.Ob} (g : C.Hom d c) (ρ : Nat → (M.P.obj c).car) (ys : List (M.P.obj c).car) :
    ∀ n, (M.P.obj d).eqv (resV M g (extLV M ρ ys) n) (extLV M (resV M g ρ) (lmapG (M.P.map g) ys) n) :=
  res_extLV M g ρ ys

section Comp

variable (T : Seq → Prop) {X Y Z : SObj} {θ η : GFm}

def bodyF (m k : Nat) (θ η : GFm) : GFm := .conj (renG (σθ m k) θ) (renG (ση m k) η)

/-- unpacking the body at a restricted stage -/
theorem body_at (N : SModel T) (hη : FVg (Z.n + Y.n) η) {c d : N.D.Ob} (g : N.D.Hom d c)
    (ρ : Nat → (N.M.P.obj c).car) (zs : List (N.M.P.obj c).car) (hk : lenG zs = Z.n)
    (ys : List (N.M.P.obj d).car) (hl : lenG ys = Y.n)
    (fb : force N.K N.M d (extLV N.M (resV N.M g (extLV N.M ρ zs)) ys) (bodyF Y.n Z.n θ η)) :
    force N.K N.M d (extLV N.M (resV N.M g ρ) ys) θ ∧
      force N.K N.M d (extLV N.M (extLV N.M (resV N.M g ρ) ys) (lmapG (N.M.P.map g) zs)) η := by
  have fb' := force_resp N.K N.M _ (extLV_eqv N.M (res_block N.M g ρ zs) ys) fb
  exact (force_body N.K N.M θ η Y.n Z.n hη (resV N.M g ρ) (lmapG (N.M.P.map g) zs) ys hl
    ((len_lmapG _ zs).trans hk)).1 fb'

theorem comp_a (hθ : IsArr T X Y θ) (hη : IsArr T Y Z η) : ∀ (N : SModel T) c (ρ : Nat → (N.M.P.obj c).car) zs,
    lenG zs = Z.n → force N.K N.M c (extLV N.M ρ zs) (compF Y.n Z.n θ η) →
    force N.K N.M c ρ X.φ ∧ force N.K N.M c (extLV N.M ρ zs) Z.φ := by
  intro N c ρ zs hk hf
  have W := exN_wit N.K N.M (bodyF Y.n Z.n θ η) Y.n (extLV N.M ρ zs) hf
  refine ⟨force_local N.K N.M N.closed X.φ ρ _ W (fun d g ⟨ys, hl, fb⟩ => ?_),
    force_local N.K N.M N.closed Z.φ (extLV N.M ρ zs) _ W (fun d g ⟨ys, hl, fb⟩ => ?_)⟩
  · exact (hθ.2.1 N d (resV N.M g ρ) ys hl (body_at T N hη.1 g ρ zs hk ys hl fb).1).1
  · have hz := (hη.2.1 N d (extLV N.M (resV N.M g ρ) ys) (lmapG (N.M.P.map g) zs) ((len_lmapG _ zs).trans hk)
      (body_at T N hη.1 g ρ zs hk ys hl fb).2).2
    refine force_agree N.K N.M Z.φ Z.n Z.fv (fun i hi => ?_) hz
    rw [extLV_lt N.M _ (resV N.M g ρ) _ i (by rw [len_lmapG, hk]; exact hi)]
    exact (N.M.P.obj d).symm (res_block N.M g ρ zs i)

theorem comp_b (hθ : IsArr T X Y θ) (hη : IsArr T Y Z η) : ∀ (N : SModel T) c (ρ : Nat → (N.M.P.obj c).car),
    force N.K N.M c ρ X.φ → force N.K N.M c ρ (exN Z.n (compF Y.n Z.n θ η)) := by
  intro N c ρ hφ
  have W1 := exN_wit N.K N.M θ Y.n ρ (hθ.2.2.1 N c ρ hφ)
  refine force_local N.K N.M N.closed _ ρ _ W1 (fun d g ⟨ys, hl, fθ⟩ => ?_)
  have hψ := (hθ.2.1 N d (resV N.M g ρ) ys hl fθ).2
  have W2 := exN_wit N.K N.M η Z.n (extLV N.M (resV N.M g ρ) ys) (hη.2.2.1 N d _ hψ)
  refine force_local N.K N.M N.closed _ (resV N.M g ρ) _ W2 (fun e h ⟨zs, hz, fη⟩ => ?_)
  have hl' : lenG (lmapG (N.M.P.map h) ys) = Y.n := (len_lmapG _ ys).trans hl
  have η' := force_resp N.K N.M η (extLV_eqv N.M (res_block N.M h (resV N.M g ρ) ys) zs) fη
  have θ' := force_resp N.K N.M θ (res_block N.M h (resV N.M g ρ) ys) (force_stable N.K N.M θ h _ fθ)
  have hb := (force_body N.K N.M θ η Y.n Z.n hη.1 (resV N.M h (resV N.M g ρ)) zs _ hl' hz).2 ⟨θ', η'⟩
  exact exN_of N.K N.M _ Z.n _ zs hz (exN_of N.K N.M _ Y.n _ _ hl' hb)

theorem comp_c (hθ : IsArr T X Y θ) (hη : IsArr T Y Z η) : ∀ (N : SModel T) c (ρ : Nat → (N.M.P.obj c).car) zs zs',
    lenG zs = Z.n → lenG zs' = Z.n → force N.K N.M c (extLV N.M ρ zs) (compF Y.n Z.n θ η) →
    force N.K N.M c (extLV N.M ρ zs') (compF Y.n Z.n θ η) →
    ∀ i, i < Z.n → (N.M.P.obj c).eqv (extLV N.M ρ zs i) (extLV N.M ρ zs' i) := by
  intro N c ρ zs zs' hk hk' hf hf' i hi
  have W1 := exN_wit N.K N.M (bodyF Y.n Z.n θ η) Y.n _ hf
  have W2 := exN_wit N.K N.M (bodyF Y.n Z.n θ η) Y.n _ hf'
  refine N.closed.sep c _ (cov_meet N.K W1 W2) _ _ (fun d g ⟨⟨ys, hl, fb⟩, ⟨ys', hl', fb'⟩⟩ => ?_)
  have b1 := body_at T N hη.1 g ρ zs hk ys hl fb
  have b2 := body_at T N hη.1 g ρ zs' hk' ys' hl' fb'
  have ey := hθ.2.2.2 N d (resV N.M g ρ) ys ys' hl hl' b1.1 b2.1
  have hzk : lenG (lmapG (N.M.P.map g) zs) = Z.n := (len_lmapG _ zs).trans hk
  have hzk' : lenG (lmapG (N.M.P.map g) zs') = Z.n := (len_lmapG _ zs').trans hk'
  -- move the second η to the source ys
  have η2 : force N.K N.M d (extLV N.M (extLV N.M (resV N.M g ρ) ys) (lmapG (N.M.P.map g) zs')) η := by
    refine force_agree N.K N.M η (Z.n + Y.n) hη.1 (fun j hj => ?_) b2.2
    exact match Nat.decLt j Z.n with
    | isTrue h => by
      rw [extLV_lt N.M _ (extLV N.M (resV N.M g ρ) ys) _ j (by rw [hzk']; exact h)]
      exact (N.M.P.obj d).refl _
    | isFalse h => match not_lt_split h with
      | ⟨j', e⟩ => by
        subst e
        rw [shift_val N.M _ _ Z.n hzk' j', shift_val N.M _ _ Z.n hzk' j']
        exact (N.M.P.obj d).symm (ey j' (Nat.lt_of_add_lt_add_right (by rw [Nat.add_comm Z.n Y.n] at hj; exact hj)))
  have ez := hη.2.2.2 N d (extLV N.M (resV N.M g ρ) ys) _ _ hzk hzk' b1.2 η2 i hi
  refine (N.M.P.obj d).trans (res_block N.M g ρ zs i) ((N.M.P.obj d).trans ?_ ((N.M.P.obj d).symm (res_block N.M g ρ zs' i)))
  rw [extLV_lt N.M (resV N.M g ρ) (extLV N.M (resV N.M g ρ) ys) _ i (by rw [hzk]; exact hi),
    extLV_lt N.M (resV N.M g ρ) (extLV N.M (resV N.M g ρ) ys) _ i (by rw [hzk']; exact hi)]
  exact ez

theorem fv_comp (hθ : IsArr T X Y θ) (hη : IsArr T Y Z η) : FVg (Z.n + X.n) (compF Y.n Z.n θ η) := by
  apply fv_exN Y.n
  refine ⟨fv_ren θ (fun i hi => ?_) hθ.1, fv_ren η (fun i hi => ?_) hη.1⟩
  · unfold σθ
    match Nat.decLt i Y.n with
    | isTrue h => exact Nat.lt_of_lt_of_le h (Nat.le_add_left _ _)
    | isFalse h => match not_lt_split h with
      | ⟨j, e⟩ =>
        subst e
        have hj : j < X.n := Nat.lt_of_add_lt_add_right (by rw [Nat.add_comm Y.n X.n] at hi; exact hi)
        show j + Y.n + Z.n < Z.n + X.n + Y.n
        rw [Nat.add_right_comm, Nat.add_comm (j + Z.n) Y.n, Nat.add_comm Z.n X.n, Nat.add_comm (X.n + Z.n) Y.n,
          ← Nat.add_assoc, ← Nat.add_assoc]
        exact Nat.add_lt_add_right (Nat.add_lt_add_left hj Y.n) Z.n
  · unfold ση
    match Nat.decLt i Z.n with
    | isTrue h => exact Nat.add_lt_add_right (Nat.lt_of_lt_of_le h (Nat.le_add_right _ _)) Y.n
    | isFalse h => match not_lt_split h with
      | ⟨j, e⟩ =>
        subst e
        show subN (j + Z.n) Z.n < Z.n + X.n + Y.n
        rw [subN_add]
        have hj : j < Y.n := Nat.lt_of_add_lt_add_right (by rw [Nat.add_comm Z.n Y.n] at hi; exact hi)
        exact Nat.lt_of_lt_of_le hj (Nat.le_add_left _ _)

theorem comp_arr (hθ : IsArr T X Y θ) (hη : IsArr T Y Z η) : IsArr T X Z (compF Y.n Z.n θ η) :=
  ⟨fv_comp T hθ hη, comp_a T hθ hη, comp_b T hθ hη, comp_c T hθ hη⟩

end Comp

end Gt

end Found

#print axioms Found.Gt.fv_exN
#print axioms Found.Gt.comp_a
#print axioms Found.Gt.comp_b
#print axioms Found.Gt.comp_c
#print axioms Found.Gt.fv_comp
#print axioms Found.Gt.comp_arr

found/Gt17.lean

lean_all/found/Gt17.lean

/-
  T194 (classifying topos), part 17. found/Gt17 — equality of syntactic
  arrows (forcing the same formulas at all (ρ, ȳ) in all sheaf models) and
  the unit laws: id ; θ = θ and θ ; id = θ.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt16

namespace Found

namespace Gt

open Sh

variable (T : Seq → Prop)

def ArrEq (B : SObj) (θ θ' : GFm) : Prop :=
  ∀ (N : SModel T) c (ρ : Nat → (N.M.P.obj c).car) ys, lenG ys = B.n →
    (force N.K N.M c (extLV N.M ρ ys) θ ↔ force N.K N.M c (extLV N.M ρ ys) θ')

variable {X Y : SObj} {θ : GFm}

theorem id_comp_arr (hθ : IsArr T X Y θ) : ArrEq T Y (compF X.n Y.n (idF X) θ) θ := by
  intro N c ρ ys hl
  constructor
  · intro hf
    have W := exN_wit N.K N.M (bodyF X.n Y.n (idF X) θ) X.n _ hf
    refine force_local N.K N.M N.closed θ _ _ W (fun d g ⟨xs, hxl, fb⟩ => ?_)
    have b := body_at T (Y := X) (Z := Y) (θ := idF X) (η := θ) N hθ.1 g ρ ys hl xs hxl fb
    have eb := (force_eqBlock N.K N.M X.n _).1 b.1.2
    have hyl : lenG (lmapG (N.M.P.map g) ys) = Y.n := (len_lmapG _ ys).trans hl
    have θ1 : force N.K N.M d (extLV N.M (resV N.M g ρ) (lmapG (N.M.P.map g) ys)) θ := by
      refine force_agree N.K N.M θ (Y.n + X.n) hθ.1 (fun j hj => ?_) b.2
      exact match Nat.decLt j Y.n with
      | isTrue h => by
        rw [extLV_lt N.M _ (resV N.M g ρ) _ j (by rw [hyl]; exact h)]
        exact (N.M.P.obj d).refl _
      | isFalse h => match not_lt_split h with
        | ⟨j', e⟩ => by
          subst e
          have hj' : j' < X.n := Nat.lt_of_add_lt_add_right (by rw [Nat.add_comm Y.n X.n] at hj; exact hj)
          rw [shift_val N.M _ _ Y.n hyl j', shift_val N.M _ _ Y.n hyl j']
          have q := eb j' hj'
          rw [shift_val N.M _ xs X.n hxl j'] at q
          exact q
    exact force_resp N.K N.M θ (fun n => (N.M.P.obj d).symm (res_block N.M g ρ ys n)) θ1
  · intro hf
    have hφ := (hθ.2.1 N c ρ ys hl hf).1
    have hwl := len_witL N.M ρ X.n
    refine exN_of N.K N.M _ X.n _ (witL N.M ρ X.n) hwl
      ((force_body N.K N.M (idF X) θ X.n Y.n hθ.1 ρ ys _ hwl hl).2 ⟨?_, ?_⟩)
    · refine ⟨(force_ren N.K N.M X.φ _ _).2 (force_eqval N.K N.M
        (fun i => (shift_val N.M ρ _ X.n hwl i).symm) X.φ hφ), (force_eqBlock N.K N.M X.n _).2 (fun i hi => ?_)⟩
      rw [witL_val N.M ρ X.n ρ i hi, shift_val N.M ρ _ X.n hwl i]
      exact (N.M.P.obj c).refl _
    · refine force_agree N.K N.M θ (Y.n + X.n) hθ.1 (fun j hj => ?_) hf
      exact match Nat.decLt j Y.n with
      | isTrue h => by
        rw [extLV_lt N.M ρ (extLV N.M ρ (witL N.M ρ X.n)) ys j (by rw [hl]; exact h)]
        exact (N.M.P.obj c).refl _
      | isFalse h => match not_lt_split h with
        | ⟨j', e⟩ => by
          subst e
          have hj' : j' < X.n := Nat.lt_of_add_lt_add_right (by rw [Nat.add_comm Y.n X.n] at hj; exact hj)
          rw [shift_val N.M _ _ Y.n hl j', shift_val N.M _ _ Y.n hl j', witL_val N.M ρ X.n ρ j' hj']
          exact (N.M.P.obj c).refl _

theorem comp_id_arr (hθ : IsArr T X Y θ) : ArrEq T Y (compF Y.n Y.n θ (idF Y)) θ := by
  intro N c ρ zs hl
  have hid := (id_arr T Y).1
  constructor
  · intro hf
    have W := exN_wit N.K N.M (bodyF Y.n Y.n θ (idF Y)) Y.n _ hf
    refine force_local N.K N.M N.closed θ _ _ W (fun d g ⟨ys, hyl, fb⟩ => ?_)
    have b := body_at T (Y := Y) (Z := Y) (θ := θ) (η := idF Y) N hid g ρ zs hl ys hyl fb
    have eb := (force_eqBlock N.K N.M Y.n _).1 b.2.2
    have hzl : lenG (lmapG (N.M.P.map g) zs) = Y.n := (len_lmapG _ zs).trans hl
    have θ1 : force N.K N.M d (extLV N.M (resV N.M g ρ) (lmapG (N.M.P.map g) zs)) θ := by
      refine force_agree N.K N.M θ (Y.n + X.n) hθ.1 (fun j hj => ?_) b.1
      exact match Nat.decLt j Y.n with
      | isTrue h => by
        have q := eb j h
        rw [extLV_lt N.M _ (resV N.M g ρ) _ j (by rw [hzl]; exact h), shift_val N.M _ _ Y.n hzl j] at q
        exact (N.M.P.obj d).symm q
      | isFalse h => match not_lt_split h with
        | ⟨j', e⟩ => by
          subst e
          rw [shift_val N.M _ _ Y.n hyl j', shift_val N.M _ _ Y.n hzl j']
          exact (N.M.P.obj d).refl _
    exact force_resp N.K N.M θ (fun n => (N.M.P.obj d).symm (res_block N.M g ρ zs n)) θ1
  · intro hf
    have hψ := (hθ.2.1 N c ρ zs hl hf).2
    refine exN_of N.K N.M _ Y.n _ zs hl
      ((force_body N.K N.M θ (idF Y) Y.n Y.n hid ρ zs zs hl hl).2 ⟨hf, ?_, ?_⟩)
    · exact (force_ren N.K N.M Y.φ _ _).2 (force_eqval N.K N.M
        (fun i => (shift_val N.M _ zs Y.n hl i).symm) Y.φ hψ)
    · refine (force_eqBlock N.K N.M Y.n _).2 (fun i hi => ?_)
      rw [extLV_lt N.M (extLV N.M ρ zs) ρ zs i (by rw [hl]; exact hi), shift_val N.M _ zs Y.n hl i]
      exact (N.M.P.obj c).refl _

end Gt

end Found

#print axioms Found.Gt.id_comp_arr
#print axioms Found.Gt.comp_id_arr

found/Gt18.lean

lean_all/found/Gt18.lean

/-
  T194 (classifying topos), part 18. found/Gt18 — composition of syntactic
  arrows respects equality of arrows and is associative; the syntactic
  category C_T of a geometric theory.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt17

namespace Found

namespace Gt

open Sh

variable (T : Seq → Prop) {X Y Z : SObj}

theorem comp_resp_dir {θ θ' η η' : GFm} (hη : IsArr T Y Z η) (hη' : IsArr T Y Z η')
    (eθ : ArrEq T Y θ θ') (eη : ArrEq T Z η η') :
    ∀ (N : SModel T) c (ρ : Nat → (N.M.P.obj c).car) zs, lenG zs = Z.n →
      force N.K N.M c (extLV N.M ρ zs) (compF Y.n Z.n θ η) → force N.K N.M c (extLV N.M ρ zs) (compF Y.n Z.n θ' η') := by
  intro N c ρ zs hl hf
  have W := exN_wit N.K N.M (bodyF Y.n Z.n θ η) Y.n _ hf
  refine exN_local N.K N.M N.closed _ Y.n _ (cov_mono N.K W (fun d g ⟨ys, hyl, fb⟩ => ⟨ys, hyl, ?_⟩))
  have b := body_at T (Y := Y) (Z := Z) N hη.1 g ρ zs hl ys hyl fb
  have hzl : lenG (lmapG (N.M.P.map g) zs) = Z.n := (len_lmapG _ zs).trans hl
  have b' := (force_body N.K N.M θ' η' Y.n Z.n hη'.1 (resV N.M g ρ) _ ys hyl hzl).2
    ⟨(eθ N d _ ys hyl).1 b.1, (eη N d _ _ hzl).1 b.2⟩
  exact force_resp N.K N.M _ (extLV_eqv N.M (fun n => (N.M.P.obj d).symm (res_block N.M g ρ zs n)) ys) b'

theorem comp_resp_arr {θ θ' η η' : GFm} (hη : IsArr T Y Z η) (hη' : IsArr T Y Z η')
    (eθ : ArrEq T Y θ θ') (eη : ArrEq T Z η η') : ArrEq T Z (compF Y.n Z.n θ η) (compF Y.n Z.n θ' η') :=
  fun N c ρ zs hl => ⟨comp_resp_dir T hη hη' eθ eη N c ρ zs hl,
    comp_resp_dir T hη' hη (fun N c ρ ys h => (eθ N c ρ ys h).symm) (fun N c ρ ys h => (eη N c ρ ys h).symm) N c ρ zs hl⟩

section Assoc

variable {V : SObj} {θ η ξ : GFm} (hθ : IsArr T X Y θ) (hη : IsArr T Y Z η) (hξ : IsArr T Z V ξ)

/-- agreement of two valuations below l + k sharing the newest l values and the next k values -/
theorem agree2 {D : ECat.{0, 0}} (M : @GStr D) {c : D.Ob} (A B : Nat → (M.P.obj c).car) (zs vs : List (M.P.obj c).car)
    (l : Nat) (hv : lenG vs = l) (hk : ∀ j, j < lenG zs → extLV M A zs j = extLV M B zs j) :
    ∀ j, j < l + lenG zs → (M.P.obj c).eqv (extLV M (extLV M A zs) vs j) (extLV M (extLV M B zs) vs j) := fun j hj =>
  match Nat.decLt j l with
  | isTrue h => by
    rw [extLV_lt M (extLV M A zs) (extLV M B zs) vs j (by rw [hv]; exact h)]
    exact (M.P.obj c).refl _
  | isFalse h => match not_lt_split h with
    | ⟨j', e⟩ => by
      subst e
      rw [shift_val M _ vs l hv j', shift_val M _ vs l hv j', hk j' (Nat.lt_of_add_lt_add_right
        (by rw [Nat.add_comm l (lenG zs)] at hj; exact hj))]
      exact (M.P.obj c).refl _

include hη hξ in
theorem assoc_lr : ∀ (N : SModel T) c (ρ : Nat → (N.M.P.obj c).car) vs, lenG vs = V.n →
    force N.K N.M c (extLV N.M ρ vs) (compF Z.n V.n (compF Y.n Z.n θ η) ξ) →
    force N.K N.M c (extLV N.M ρ vs) (compF Y.n V.n θ (compF Z.n V.n η ξ)) := by
  intro N c ρ vs hl hL
  have hηξ := comp_arr T hη hξ
  have W1 := exN_wit N.K N.M (bodyF Z.n V.n (compF Y.n Z.n θ η) ξ) Z.n _ hL
  refine force_local N.K N.M N.closed _ _ _ W1 (fun d g ⟨zs, hzl, fb⟩ => ?_)
  have b1 := body_at T (Y := Z) (Z := V) (θ := compF Y.n Z.n θ η) (η := ξ) N hξ.1 g ρ vs hl zs hzl fb
  have hvg : lenG (lmapG (N.M.P.map g) vs) = V.n := (len_lmapG _ vs).trans hl
  refine force_resp N.K N.M _ (fun n => (N.M.P.obj d).symm (res_block N.M g ρ vs n)) ?_
  have W2 := exN_wit N.K N.M (bodyF Y.n Z.n θ η) Y.n _ b1.1
  refine exN_local N.K N.M N.closed _ Y.n _ (cov_mono N.K W2 (fun e h ⟨ys, hyl, fb2⟩ => ⟨ys, hyl, ?_⟩))
  have b2 := body_at T (Y := Y) (Z := Z) (θ := θ) (η := η) N hη.1 h (resV N.M g ρ) zs hzl ys hyl fb2
  have hzh : lenG (lmapG (N.M.P.map h) zs) = Z.n := (len_lmapG _ zs).trans hzl
  have hvh : lenG (lmapG (N.M.P.map h) (lmapG (N.M.P.map g) vs)) = V.n := (len_lmapG _ _).trans hvg
  let ρh := resV N.M h (resV N.M g ρ)
  -- ξ restricted to the stage
  have ξ1 := force_resp N.K N.M ξ (fun n => (N.M.P.obj e).trans (res_block N.M h _ _ n)
      (extLV_eqv N.M (res_block N.M h (resV N.M g ρ) zs) _ n))
    (force_stable N.K N.M ξ h _ b1.2)
  have ξ2 : force N.K N.M e (extLV N.M (extLV N.M (extLV N.M ρh ys) (lmapG (N.M.P.map h) zs))
      (lmapG (N.M.P.map h) (lmapG (N.M.P.map g) vs))) ξ := by
    refine force_agree N.K N.M ξ (V.n + Z.n) hξ.1 ?_ ξ1
    have q := agree2 N.M ρh (extLV N.M ρh ys) (lmapG (N.M.P.map h) zs) _ V.n hvh
      (fun j hj => extLV_lt N.M _ _ _ j hj)
    rw [hzh] at q
    exact q
  have cηξ := exN_of N.K N.M _ Z.n _ _ hzh ((force_body N.K N.M η ξ Z.n V.n hξ.1 (extLV N.M ρh ys) _ _ hzh hvh).2
    ⟨b2.2, ξ2⟩)
  have bR := (force_body N.K N.M θ (compF Z.n V.n η ξ) Y.n V.n hηξ.1 ρh _ ys hyl hvh).2 ⟨b2.1, cηξ⟩
  exact force_resp N.K N.M _ (extLV_eqv N.M (fun n => (N.M.P.obj e).symm
    (res_block N.M h (resV N.M g ρ) (lmapG (N.M.P.map g) vs) n)) ys) bR

include hη hξ in
theorem assoc_rl : ∀ (N : SModel T) c (ρ : Nat → (N.M.P.obj c).car) vs, lenG vs = V.n →
    force N.K N.M c (extLV N.M ρ vs) (compF Y.n V.n θ (compF Z.n V.n η ξ)) →
    force N.K N.M c (extLV N.M ρ vs) (compF Z.n V.n (compF Y.n Z.n θ η) ξ) := by
  intro N c ρ vs hl hR
  have hηξ := comp_arr T hη hξ
  have W1 := exN_wit N.K N.M (bodyF Y.n V.n θ (compF Z.n V.n η ξ)) Y.n _ hR
  refine force_local N.K N.M N.closed _ _ _ W1 (fun d g ⟨ys, hyl, fb⟩ => ?_)
  have b1 := body_at T (Y := Y) (Z := V) (θ := θ) (η := compF Z.n V.n η ξ) N hηξ.1 g ρ vs hl ys hyl fb
  have hvg : lenG (lmapG (N.M.P.map g) vs) = V.n := (len_lmapG _ vs).trans hl
  refine force_resp N.K N.M _ (fun n => (N.M.P.obj d).symm (res_block N.M g ρ vs n)) ?_
  let B := extLV N.M (resV N.M g ρ) ys
  have W2 := exN_wit N.K N.M (bodyF Z.n V.n η ξ) Z.n _ b1.2
  refine exN_local N.K N.M N.closed _ Z.n _ (cov_mono N.K W2 (fun e h ⟨zs, hzl, fb2⟩ => ⟨zs, hzl, ?_⟩))
  have b2 := body_at T (Y := Z) (Z := V) (θ := η) (η := ξ) N hξ.1 h B _ hvg zs hzl fb2
  have hyh : lenG (lmapG (N.M.P.map h) ys) = Y.n := (len_lmapG _ ys).trans hyl
  have hvh : lenG (lmapG (N.M.P.map h) (lmapG (N.M.P.map g) vs)) = V.n := (len_lmapG _ _).trans hvg
  let ρh := resV N.M h (resV N.M g ρ)
  have θ1 : force N.K N.M e (extLV N.M ρh (lmapG (N.M.P.map h) ys)) θ :=
    force_resp N.K N.M θ (res_block N.M h (resV N.M g ρ) ys) (force_stable N.K N.M θ h _ b1.1)
  have η1 : force N.K N.M e (extLV N.M (extLV N.M ρh (lmapG (N.M.P.map h) ys)) zs) η :=
    force_resp N.K N.M η (extLV_eqv N.M (res_block N.M h (resV N.M g ρ) ys) zs) b2.1
  have cθη := exN_of N.K N.M _ Y.n _ _ hyh ((force_body N.K N.M θ η Y.n Z.n hη.1 ρh zs _ hyh hzl).2 ⟨θ1, η1⟩)
  have ξ1 : force N.K N.M e (extLV N.M (extLV N.M ρh zs) (lmapG (N.M.P.map h) (lmapG (N.M.P.map g) vs))) ξ := by
    refine force_agree N.K N.M ξ (V.n + Z.n) hξ.1 ?_ b2.2
    have q := agree2 N.M (resV N.M h B) ρh zs _ V.n hvh (fun j hj => extLV_lt N.M _ _ _ j hj)
    rw [hzl] at q
    exact q
  have bL := (force_body N.K N.M (compF Y.n Z.n θ η) ξ Z.n V.n hξ.1 ρh _ zs hzl hvh).2 ⟨cθη, ξ1⟩
  exact force_resp N.K N.M _ (extLV_eqv N.M (fun n => (N.M.P.obj e).symm
    (res_block N.M h (resV N.M g ρ) (lmapG (N.M.P.map g) vs) n)) zs) bL

include hη hξ in
theorem assoc_arr : ArrEq T V (compF Z.n V.n (compF Y.n Z.n θ η) ξ) (compF Y.n V.n θ (compF Z.n V.n η ξ)) :=
  fun N c ρ vs hl => ⟨assoc_lr T hη hξ N c ρ vs hl, assoc_rl T hη hξ N c ρ vs hl⟩

end Assoc

/-- the syntactic category of T -/
def SynCat : ECat.{0, 0} where
  Ob := SObj
  Hom A B := {θ : GFm // IsArr T A B θ}
  heq {_ B} f g := ArrEq T B f.1 g.1
  hrefl _ _ _ _ _ _ := Iff.rfl
  hsymm h N c ρ ys hl := (h N c ρ ys hl).symm
  htrans h k N c ρ ys hl := (h N c ρ ys hl).trans (k N c ρ ys hl)
  id A := ⟨idF A, id_arr T A⟩
  comp {_ B C} f g := ⟨compF B.n C.n f.1 g.1, comp_arr T f.2 g.2⟩
  comp_resp {_ _ _} {_ _ g g'} h k := comp_resp_arr T g.2 g'.2 h k
  id_comp f := id_comp_arr T f.2
  comp_id f := comp_id_arr T f.2
  assoc _ g h := assoc_arr T g.2 h.2

end Gt

end Found

#print axioms Found.Gt.comp_resp_arr
#print axioms Found.Gt.assoc_lr
#print axioms Found.Gt.assoc_rl
#print axioms Found.Gt.assoc_arr
#print axioms Found.Gt.SynCat

found/Gt19.lean

lean_all/found/Gt19.lean

/-
  T194 (classifying topos), part 19. found/Gt19 — projection arrows in the
  syntactic category: for an object P and a block of q of its variables
  starting at o (o + q ≤ |P|) such that P.φ implies Q.φ of the block, the
  formula P.φ(x̄) ∧ ȳ = (block of x̄) is an arrow P → Q.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt18

namespace Found

namespace Gt

open Sh

/-- x_i = x_{i + off} for i < n -/
def eqOff (n off : Nat) : GFm := conjG (lmapG (fun i => GFm.eq i (i + off)) (rangeG n))

variable {C : ECat.{0, 0}} (J : GTop C) (M : @GStr C)

theorem force_eqOff {c : C.Ob} (n off : Nat) (ρ : Nat → (M.P.obj c).car) :
    force J M c ρ (eqOff n off) ↔ ∀ i, i < n → (M.P.obj c).eqv (ρ i) (ρ (i + off)) :=
  (force_conjG J M ρ _).trans ⟨fun h i hi => h _ (lmapG_mem _ _ (rangeG_mem n i hi)),
    fun h x hx => match mem_lmapG _ hx with
      | ⟨i, hi, e⟩ => by rw [e]; exact h i (mem_rangeG n i hi)⟩

theorem fv_eqOff (n off k : Nat) (h : n + off ≤ k) : FVg k (eqOff n off) :=
  fv_conjG _ _ (fun x hx => match mem_lmapG _ hx with
    | ⟨i, hi, e⟩ => by
      rw [e]
      have hi' := mem_rangeG n i hi
      exact ⟨Nat.lt_of_lt_of_le hi' (Nat.le_trans (Nat.le_add_right n off) h),
        Nat.lt_of_lt_of_le (Nat.add_lt_add_right hi' off) h⟩)

variable (T : Seq → Prop)

def projF (P : SObj) (q o : Nat) : GFm := .conj (renG (· + q) P.φ) (eqOff q (q + o))

theorem proj_arr (P Q : SObj) (o : Nat) (hb : o + Q.n ≤ P.n)
    (hQ : ∀ (N : SModel T) c (ρ : Nat → (N.M.P.obj c).car), force N.K N.M c ρ P.φ →
      force N.K N.M c (fun i => ρ (i + o)) Q.φ) : IsArr T P Q (projF P Q.n o) := by
  refine ⟨⟨fv_ren P.φ (fun i hi => by rw [Nat.add_comm Q.n P.n]; exact Nat.add_lt_add_right hi Q.n) P.fv, fv_eqOff Q.n (Q.n + o) (Q.n + P.n)
    (Nat.add_le_add_left (by rw [Nat.add_comm Q.n o]; exact hb) Q.n)⟩, ?_, ?_, ?_⟩
  · intro N c ρ ys hl ⟨h1, h2⟩
    have hφ : force N.K N.M c ρ P.φ :=
      force_eqval N.K N.M (shift_val N.M ρ ys Q.n hl) P.φ ((force_ren N.K N.M P.φ _ _).1 h1)
    have eb := (force_eqOff N.K N.M Q.n (Q.n + o) _).1 h2
    refine ⟨hφ, force_agree N.K N.M Q.φ Q.n Q.fv (fun i hi => ?_) (hQ N c ρ hφ)⟩
    have e := eb i hi
    rw [← Nat.add_assoc, Nat.add_right_comm i Q.n o, shift_val N.M ρ ys Q.n hl (i + o)] at e
    exact (N.M.P.obj c).symm e
  · intro N c ρ hφ
    have hwl := len_witL N.M (fun i => ρ (i + o)) Q.n
    refine exN_of N.K N.M _ Q.n ρ (witL N.M (fun i => ρ (i + o)) Q.n) hwl ⟨?_, ?_⟩
    · exact (force_ren N.K N.M P.φ _ _).2 (force_eqval N.K N.M
        (fun i => (shift_val N.M ρ _ Q.n hwl i).symm) P.φ hφ)
    · refine (force_eqOff N.K N.M Q.n (Q.n + o) _).2 (fun i hi => ?_)
      rw [witL_val N.M _ Q.n ρ i hi, ← Nat.add_assoc, Nat.add_right_comm i Q.n o, shift_val N.M ρ _ Q.n hwl (i + o)]
      exact (N.M.P.obj c).refl _
  · intro N c ρ ys ys' hl hl' ⟨_, e1⟩ ⟨_, e2⟩ i hi
    have a1 := (force_eqOff N.K N.M Q.n (Q.n + o) _).1 e1 i hi
    have a2 := (force_eqOff N.K N.M Q.n (Q.n + o) _).1 e2 i hi
    rw [← Nat.add_assoc, Nat.add_right_comm i Q.n o, shift_val N.M ρ ys Q.n hl (i + o)] at a1
    rw [← Nat.add_assoc, Nat.add_right_comm i Q.n o, shift_val N.M ρ ys' Q.n hl' (i + o)] at a2
    exact (N.M.P.obj c).trans a1 ((N.M.P.obj c).symm a2)

end Gt

end Found

#print axioms Found.Gt.force_eqOff
#print axioms Found.Gt.proj_arr

found/Gt2.lean

lean_all/found/Gt2.lean

/-
  T194 (classifying topos), part 2. found/Gt2 — the plus construction for a
  presheaf of setoids on a site: elements of P⁺(c) are matching families on
  covering sieves of c, identified when they agree on a common covering
  sieve; restriction pulls sieves back. P⁺ is a presheaf of setoids and
  there is a natural map P → P⁺ (restrictions along the maximal sieve).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt1

namespace Found

namespace Gt

open Sh

variable {C : ECat.{0, 0}} (J : GTop C) (P : Psh.{0, 0, 0} C)

structure PElt (c : C.Ob) where
  S : Sieve C c
  cov : J.cov S
  m : MFam P S

def PEq {c : C.Ob} (a b : PElt J P c) : Prop :=
  ∃ R : Sieve C c, J.cov R ∧ (∀ d (f : C.Hom d c), R.S f → a.S.S f ∧ b.S.S f) ∧
    ∀ d (f : C.Hom d c) (ha : a.S.S f) (hb : b.S.S f), R.S f → (P.obj d).eqv (a.m.x f ha) (b.m.x f hb)

theorem peq_refl {c : C.Ob} (a : PElt J P c) : PEq J P a a :=
  ⟨a.S, a.cov, fun _ _ h => ⟨h, h⟩, fun _ f ha hb _ => a.m.resp ha hb (C.hrefl f)⟩

theorem peq_symm {c : C.Ob} {a b : PElt J P c} (h : PEq J P a b) : PEq J P b a :=
  match h with
  | ⟨R, hR, inc, e⟩ => ⟨R, hR, fun d f r => ⟨(inc d f r).2, (inc d f r).1⟩,
      fun d f hb ha r => (P.obj d).symm (e d f ha hb r)⟩

theorem peq_trans {c : C.Ob} {a b e : PElt J P c} (h1 : PEq J P a b) (h2 : PEq J P b e) : PEq J P a e :=
  match h1, h2 with
  | ⟨R1, hR1, inc1, e1⟩, ⟨R2, hR2, inc2, e2⟩ =>
    ⟨meetS R1 R2, cov_meet J hR1 hR2, fun d f r => ⟨(inc1 d f r.1).1, (inc2 d f r.2).2⟩,
     fun d f ha he r => (P.obj d).trans (e1 d f ha (inc1 d f r.1).2 r.1) (e2 d f (inc1 d f r.1).2 he r.2)⟩

/-- restriction of a matching family along g -/
def resM {c c' : C.Ob} (g : C.Hom c' c) {S : Sieve C c} (m : MFam P S) : MFam P (pullS g S) where
  x := fun f hf => m.x (C.comp f g) hf
  resp := fun hf hf' e => m.resp hf hf' (C.lcong e g)
  compat := fun f hf h => (P.obj _).trans (m.resp _ _ (C.assoc h f g)) (m.compat (C.comp f g) hf h)

def resE {c c' : C.Ob} (g : C.Hom c' c) (a : PElt J P c) : PElt J P c' :=
  ⟨pullS g a.S, J.pull g a.cov, resM P g a.m⟩

def PlusObj (c : C.Ob) : Setd.{0} := ⟨PElt J P c, PEq J P, peq_refl J P, peq_symm J P, peq_trans J P⟩

theorem plus_map_eqv {c c' : C.Ob} (g : C.Hom c' c) {a b : PElt J P c} (h : PEq J P a b) :
    PEq J P (resE J P g a) (resE J P g b) :=
  match h with
  | ⟨R, hR, inc, e⟩ => ⟨pullS g R, J.pull g hR, fun d f r => inc d (C.comp f g) r,
      fun d f ha hb r => e d (C.comp f g) ha hb r⟩

theorem plus_map_resp {c c' : C.Ob} {g g' : C.Hom c' c} (a : PElt J P c) (e : C.heq g g') :
    PEq J P (resE J P g a) (resE J P g' a) :=
  ⟨pullS g a.S, J.pull g a.cov, fun _ f r => ⟨r, a.S.resp (C.rcong f e) r⟩,
    fun _ f ha hb _ => a.m.resp ha hb (C.rcong f e)⟩

theorem plus_map_id {c : C.Ob} (a : PElt J P c) : PEq J P (resE J P (C.id c) a) a :=
  ⟨a.S, a.cov, fun _ f r => ⟨a.S.resp (C.hsymm (C.comp_id f)) r, r⟩,
    fun _ f ha hb _ => a.m.resp ha hb (C.comp_id f)⟩

theorem plus_map_comp {c1 c2 c3 : C.Ob} (f : C.Hom c1 c2) (g : C.Hom c2 c3) (a : PElt J P c3) :
    PEq J P (resE J P (C.comp f g) a) (resE J P f (resE J P g a)) :=
  ⟨pullS (C.comp f g) a.S, J.pull _ a.cov,
    fun _ h r => ⟨r, a.S.resp (C.hsymm (C.assoc h f g)) r⟩,
    fun _ h ha hb _ => a.m.resp ha hb (C.hsymm (C.assoc h f g))⟩

/-- the presheaf P⁺ -/
def Plus : Psh.{0, 0, 0} C where
  obj c := PlusObj J P c
  map g a := resE J P g a
  map_eqv {_ _} g {_ _} h := plus_map_eqv J P g h
  map_resp a e := plus_map_resp J P a e
  map_id _ a := plus_map_id J P a
  map_comp f g a := plus_map_comp J P f g a

/-- the unit P → P⁺ -/
def etaM {c : C.Ob} (x : (P.obj c).car) : MFam P (maxS c) where
  x := fun f _ => P.map f x
  resp := fun _ _ e => P.map_resp x e
  compat := fun f _ g => P.map_comp g f x

def etaE {c : C.Ob} (x : (P.obj c).car) : PElt J P c := ⟨maxS c, J.top c, etaM P x⟩

def eta : PNat P (Plus J P) where
  app _ x := etaE J P x
  app_eqv _ {_ _} h := ⟨maxS _, J.top _, fun _ _ _ => ⟨trivial, trivial⟩, fun _ f _ _ _ => P.map_eqv f h⟩
  nat f x := ⟨maxS _, J.top _, fun _ _ _ => ⟨trivial, trivial⟩, fun _ g _ _ _ => (P.obj _).symm (P.map_comp g f x)⟩

end Gt

end Found

#print axioms Found.Gt.peq_trans
#print axioms Found.Gt.Plus
#print axioms Found.Gt.eta

found/Gt20.lean

lean_all/found/Gt20.lean

/-
  T194 (classifying topos), part 20. found/Gt20 — pullback objects in the
  syntactic category: for θ : B → A and f : A' → A, the object
  P = {x̄' x̄ . ∃ȳ (θ(x̄, ȳ) ∧ f(x̄', ȳ))} (A'-block first, B-block after);
  forcing P at a valuation means: locally there are ȳ with θ(B-part, ȳ)
  and f(A'-part, ȳ).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt19

namespace Found

namespace Gt

open Sh

variable {C : ECat.{0, 0}} (J : GTop C) (M : @GStr C)

/-- θ renamed into the pullback context -/
theorem val_θP {c : C.Ob} (ρ : Nat → (M.P.obj c).car) (ys : List (M.P.obj c).car) (m k : Nat) (hm : lenG ys = m) :
    ∀ i, extLV M ρ ys (σθ m k i) = extLV M (fun j => ρ (j + k)) ys i := fun i => by
  unfold σθ
  match Nat.decLt i m with
  | isTrue h => exact extLV_lt M _ _ ys i (by rw [hm]; exact h)
  | isFalse h =>
    match not_lt_split h with
    | ⟨j, e⟩ =>
      subst e
      show extLV M ρ ys (j + m + k) = extLV M (fun j => ρ (j + k)) ys (j + m)
      rw [Nat.add_right_comm, shift_val M ρ ys m hm, shift_val M _ ys m hm]

theorem allLtG_mono {k j : Nat} (h : k ≤ j) : (l : List Nat) → AllLtG k l → AllLtG j l
  | [], _ => trivial
  | _ :: l, hl => ⟨Nat.lt_of_lt_of_le hl.1 h, allLtG_mono h l hl.2⟩

theorem fvg_mono : (φ : GFm) → {k j : Nat} → FVg k φ → k ≤ j → FVg j φ
  | .atom _ l, _, _, h, hle => allLtG_mono hle l h
  | .eq _ _, _, _, h, hle => ⟨Nat.lt_of_lt_of_le h.1 hle, Nat.lt_of_lt_of_le h.2 hle⟩
  | .top, _, _, _, _ => trivial
  | .conj a b, _, _, h, hle => ⟨fvg_mono a h.1 hle, fvg_mono b h.2 hle⟩
  | .sup φ, _, _, h, hle => fun n => fvg_mono (φ n) (h n) hle
  | .ex φ, _, _, h, hle => fvg_mono φ h (Nat.succ_le_succ hle)

variable (T : Seq → Prop)

def pbF (A A' : SObj) (θ f : GFm) : GFm := exN A.n (.conj (renG (σθ A.n A'.n) θ) f)

theorem fv_pb {A A' B : SObj} {θ f : GFm} (hθ : IsArr T B A θ) (hf : IsArr T A' A f) :
    FVg (A'.n + B.n) (pbF A A' θ f) := by
  apply fv_exN A.n
  refine ⟨fv_ren θ (fun i hi => ?_) hθ.1, fvg_mono f hf.1 ?_⟩
  · unfold σθ
    match Nat.decLt i A.n with
    | isTrue h => exact Nat.lt_of_lt_of_le h (Nat.le_add_left _ _)
    | isFalse h => match not_lt_split h with
      | ⟨j, e⟩ =>
        subst e
        have hj : j < B.n := Nat.lt_of_add_lt_add_right (by rw [Nat.add_comm A.n B.n] at hi; exact hi)
        show j + A.n + A'.n < A'.n + B.n + A.n
        rw [Nat.add_right_comm, Nat.add_comm (j + A'.n) A.n, Nat.add_comm A'.n B.n, Nat.add_comm (B.n + A'.n) A.n,
          ← Nat.add_assoc, ← Nat.add_assoc]
        exact Nat.add_lt_add_right (Nat.add_lt_add_left hj A.n) A'.n
  · show A.n + A'.n ≤ A'.n + B.n + A.n
    rw [Nat.add_comm A.n A'.n, Nat.add_assoc]
    exact Nat.add_le_add_left (Nat.le_add_left _ _) A'.n

section PB

variable {A A' B : SObj} {θ f : GFm} (hθ : IsArr T B A θ) (hf : IsArr T A' A f)

def pbObj : SObj := ⟨A'.n + B.n, pbF A A' θ f, fv_pb T hθ hf⟩

/-- the body of the pullback formula at a stage -/
theorem pb_body (N : SModel T) {d : N.D.Ob} (ρ : Nat → (N.M.P.obj d).car) (ys : List (N.M.P.obj d).car)
    (hl : lenG ys = A.n) :
    force N.K N.M d (extLV N.M ρ ys) (.conj (renG (σθ A.n A'.n) θ) f) ↔
      force N.K N.M d (extLV N.M (fun j => ρ (j + A'.n)) ys) θ ∧ force N.K N.M d (extLV N.M ρ ys) f := by
  have e := val_θP N.M ρ ys A.n A'.n hl
  exact ⟨fun ⟨h1, h2⟩ => ⟨force_eqval N.K N.M e θ ((force_ren N.K N.M θ _ _).1 h1), h2⟩,
    fun ⟨h1, h2⟩ => ⟨(force_ren N.K N.M θ _ _).2 (force_eqval N.K N.M (fun i => (e i).symm) θ h1), h2⟩⟩

include hf in
theorem pb_A' : ∀ (N : SModel T) c (ρ : Nat → (N.M.P.obj c).car), force N.K N.M c ρ (pbF A A' θ f) →
    force N.K N.M c (fun i => ρ (i + 0)) A'.φ := fun N _ ρ h =>
  force_local N.K N.M N.closed A'.φ _ _ (exN_wit N.K N.M _ A.n ρ h) (fun d g ⟨ys, hl, fb⟩ =>
    (hf.2.1 N d (resV N.M g ρ) ys hl ((pb_body T N (resV N.M g ρ) ys hl).1 fb).2).1)

include hθ in
theorem pb_B : ∀ (N : SModel T) c (ρ : Nat → (N.M.P.obj c).car), force N.K N.M c ρ (pbF A A' θ f) →
    force N.K N.M c (fun i => ρ (i + A'.n)) B.φ := fun N _ ρ h =>
  force_local N.K N.M N.closed B.φ _ _ (exN_wit N.K N.M _ A.n ρ h) (fun d g ⟨ys, hl, fb⟩ =>
    (hθ.2.1 N d (fun j => resV N.M g ρ (j + A'.n)) ys hl ((pb_body T N (resV N.M g ρ) ys hl).1 fb).1).1)

theorem pi1_arr : IsArr T (pbObj T hθ hf) A' (projF (pbObj T hθ hf) A'.n 0) :=
  proj_arr T (pbObj T hθ hf) A' 0 (by show 0 + A'.n ≤ A'.n + B.n; rw [Nat.zero_add]; exact Nat.le_add_right _ _)
    (pb_A' T hf)

theorem pi2_arr : IsArr T (pbObj T hθ hf) B (projF (pbObj T hθ hf) B.n A'.n) :=
  proj_arr T (pbObj T hθ hf) B A'.n (Nat.le_refl _) (pb_B T hθ)

end PB

end Gt

end Found

#print axioms Found.Gt.fv_pb
#print axioms Found.Gt.pb_body
#print axioms Found.Gt.pi1_arr
#print axioms Found.Gt.pi2_arr

found/Gt21.lean

lean_all/found/Gt21.lean

/-
  T194 (classifying topos), part 21. found/Gt21 — composing a projection
  arrow P → Q (block at o) with an arrow g : Q → A is forcing P.φ and g at
  the block; hence the pullback square π' ; f = π ; θ commutes.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt20

namespace Found

namespace Gt

open Sh

variable (T : Seq → Prop)

theorem comp_proj (P Q A : SObj) (o : Nat) (hb : o + Q.n ≤ P.n)
    (hQ : ∀ (N : SModel T) c (ρ : Nat → (N.M.P.obj c).car), force N.K N.M c ρ P.φ →
      force N.K N.M c (fun i => ρ (i + o)) Q.φ) {g : GFm} (hg : IsArr T Q A g) :
    ∀ (N : SModel T) c (ρ : Nat → (N.M.P.obj c).car) ys, lenG ys = A.n →
      (force N.K N.M c (extLV N.M ρ ys) (compF Q.n A.n (projF P Q.n o) g) ↔
        force N.K N.M c ρ P.φ ∧ force N.K N.M c (extLV N.M (fun i => ρ (i + o)) ys) g) := by
  intro N c ρ ys hl
  have hu := proj_arr T P Q o hb hQ
  constructor
  · intro h
    have W := exN_wit N.K N.M (bodyF Q.n A.n (projF P Q.n o) g) Q.n _ h
    refine ⟨force_local N.K N.M N.closed P.φ ρ _ W (fun d k ⟨zs, hzl, fb⟩ =>
      (hu.2.1 N d _ zs hzl (body_at T (Y := Q) (Z := A) N hg.1 k ρ ys hl zs hzl fb).1).1), ?_⟩
    refine force_local N.K N.M N.closed g _ _ W (fun d k ⟨zs, hzl, fb⟩ => ?_)
    have b := body_at T (Y := Q) (Z := A) N hg.1 k ρ ys hl zs hzl fb
    have eo := (force_eqOff N.K N.M Q.n (Q.n + o) _).1 b.1.2
    have hyl : lenG (lmapG (N.M.P.map k) ys) = A.n := (len_lmapG _ ys).trans hl
    have g1 : force N.K N.M d (extLV N.M (fun i => resV N.M k ρ (i + o)) (lmapG (N.M.P.map k) ys)) g := by
      refine force_agree N.K N.M g (A.n + Q.n) hg.1 (fun j hj => ?_) b.2
      exact match Nat.decLt j A.n with
      | isTrue h => by
        rw [extLV_lt N.M _ (fun i => resV N.M k ρ (i + o)) _ j (by rw [hyl]; exact h)]
        exact (N.M.P.obj d).refl _
      | isFalse h => match not_lt_split h with
        | ⟨j', e⟩ => by
          subst e
          have hj' : j' < Q.n := Nat.lt_of_add_lt_add_right (by rw [Nat.add_comm A.n Q.n] at hj; exact hj)
          rw [shift_val N.M _ _ A.n hyl j', shift_val N.M _ _ A.n hyl j']
          have q := eo j' hj'
          rw [← Nat.add_assoc, Nat.add_right_comm j' Q.n o, shift_val N.M _ zs Q.n hzl (j' + o)] at q
          exact q
    exact force_resp N.K N.M g (fun n => (N.M.P.obj d).symm (res_block N.M k (fun i => ρ (i + o)) ys n)) g1
  · intro ⟨hP, hg'⟩
    have hwl := len_witL N.M (fun i => ρ (i + o)) Q.n
    refine exN_of N.K N.M _ Q.n _ (witL N.M (fun i => ρ (i + o)) Q.n) hwl
      ((force_body N.K N.M (projF P Q.n o) g Q.n A.n hg.1 ρ ys _ hwl hl).2 ⟨⟨?_, ?_⟩, ?_⟩)
    · exact (force_ren N.K N.M P.φ _ _).2 (force_eqval N.K N.M
        (fun i => (shift_val N.M ρ _ Q.n hwl i).symm) P.φ hP)
    · refine (force_eqOff N.K N.M Q.n (Q.n + o) _).2 (fun i hi => ?_)
      rw [witL_val N.M _ Q.n ρ i hi, ← Nat.add_assoc, Nat.add_right_comm i Q.n o, shift_val N.M ρ _ Q.n hwl (i + o)]
      exact (N.M.P.obj c).refl _
    · refine force_agree N.K N.M g (A.n + Q.n) hg.1 (fun j hj => ?_) hg'
      exact match Nat.decLt j A.n with
      | isTrue h => by
        rw [extLV_lt N.M (fun i => ρ (i + o)) (extLV N.M ρ (witL N.M (fun i => ρ (i + o)) Q.n)) ys j (by rw [hl]; exact h)]
        exact (N.M.P.obj c).refl _
      | isFalse h => match not_lt_split h with
        | ⟨j', e⟩ => by
          subst e
          have hj' : j' < Q.n := Nat.lt_of_add_lt_add_right (by rw [Nat.add_comm A.n Q.n] at hj; exact hj)
          rw [shift_val N.M _ _ A.n hl j', shift_val N.M _ _ A.n hl j', witL_val N.M _ Q.n ρ j' hj']
          exact (N.M.P.obj c).refl _

section Sq

variable {A A' B : SObj} {θ f : GFm} (hθ : IsArr T B A θ) (hf : IsArr T A' A f)

/-- moving a forced arrow along unique values -/
theorem move_vals (N : SModel T) {X Y : SObj} {g : GFm} (hg : IsArr T X Y g) {d : N.D.Ob}
    (σ : Nat → (N.M.P.obj d).car) (ys ys' : List (N.M.P.obj d).car) (hl : lenG ys = Y.n) (hl' : lenG ys' = Y.n)
    (e : ∀ i, i < Y.n → (N.M.P.obj d).eqv (extLV N.M σ ys i) (extLV N.M σ ys' i))
    (h : force N.K N.M d (extLV N.M σ ys) g) : force N.K N.M d (extLV N.M σ ys') g :=
  force_agree N.K N.M g (Y.n + X.n) hg.1 (fun j hj => match Nat.decLt j Y.n with
    | isTrue hj' => e j hj'
    | isFalse hj' => match not_lt_split hj' with
      | ⟨j', eq⟩ => by
        subst eq
        rw [shift_val N.M σ ys Y.n hl j', shift_val N.M σ ys' Y.n hl' j']
        exact (N.M.P.obj d).refl _) h

include hθ hf in
theorem pb_square : ArrEq T A (compF A'.n A.n (projF (pbObj T hθ hf) A'.n 0) f)
    (compF B.n A.n (projF (pbObj T hθ hf) B.n A'.n) θ) := by
  intro N c ρ ys hl
  refine (comp_proj T (pbObj T hθ hf) A' A 0 (by show 0 + A'.n ≤ A'.n + B.n; rw [Nat.zero_add]; exact Nat.le_add_right _ _)
    (pb_A' T hf) hf N c ρ ys hl).trans (Iff.trans ⟨fun ⟨hP, h1⟩ => ⟨hP, ?_⟩, fun ⟨hP, h2⟩ => ⟨hP, ?_⟩⟩
    (comp_proj T (pbObj T hθ hf) B A A'.n (Nat.le_refl _) (pb_B T hθ) hθ N c ρ ys hl).symm)
  · have W := exN_wit N.K N.M _ A.n ρ hP
    refine force_local N.K N.M N.closed θ _ _ W (fun d g ⟨ys', hl', fb⟩ => ?_)
    have b := (pb_body T N (θ := θ) (f := f) (resV N.M g ρ) ys' hl').1 fb
    have hyl : lenG (lmapG (N.M.P.map g) ys) = A.n := (len_lmapG _ ys).trans hl
    have f1 : force N.K N.M d (extLV N.M (resV N.M g ρ) (lmapG (N.M.P.map g) ys)) f :=
      force_resp N.K N.M f (res_block N.M g (fun i => ρ (i + 0)) ys) (force_stable N.K N.M f g _ h1)
    have e := hf.2.2.2 N d (resV N.M g ρ) _ ys' hyl hl' f1 b.2
    have e' : ∀ i, i < A.n → (N.M.P.obj d).eqv (extLV N.M (fun j => resV N.M g ρ (j + A'.n)) ys' i)
        (extLV N.M (fun j => resV N.M g ρ (j + A'.n)) (lmapG (N.M.P.map g) ys) i) := fun i hi => by
      have q1 : extLV N.M (fun j => resV N.M g ρ (j + A'.n)) ys' i = extLV N.M (resV N.M g ρ) ys' i :=
        extLV_lt N.M _ _ ys' i (by rw [hl']; exact hi)
      have q2 : extLV N.M (fun j => resV N.M g ρ (j + A'.n)) (lmapG (N.M.P.map g) ys) i =
          extLV N.M (resV N.M g ρ) (lmapG (N.M.P.map g) ys) i := extLV_lt N.M _ _ _ i (by rw [hyl]; exact hi)
      rw [q1, q2]
      exact (N.M.P.obj d).symm (e i hi)
    exact force_resp N.K N.M θ (fun n => (N.M.P.obj d).symm (res_block N.M g (fun i => ρ (i + A'.n)) ys n))
      (move_vals T N hθ _ ys' _ hl' hyl e' b.1)
  · have W := exN_wit N.K N.M _ A.n ρ hP
    refine force_local N.K N.M N.closed f _ _ W (fun d g ⟨ys', hl', fb⟩ => ?_)
    have b := (pb_body T N (θ := θ) (f := f) (resV N.M g ρ) ys' hl').1 fb
    have hyl : lenG (lmapG (N.M.P.map g) ys) = A.n := (len_lmapG _ ys).trans hl
    have θ1 : force N.K N.M d (extLV N.M (fun j => resV N.M g ρ (j + A'.n)) (lmapG (N.M.P.map g) ys)) θ :=
      force_resp N.K N.M θ (res_block N.M g (fun i => ρ (i + A'.n)) ys) (force_stable N.K N.M θ g _ h2)
    have e := hθ.2.2.2 N d _ _ ys' hyl hl' θ1 b.1
    have e' : ∀ i, i < A.n → (N.M.P.obj d).eqv (extLV N.M (resV N.M g ρ) ys' i)
        (extLV N.M (resV N.M g ρ) (lmapG (N.M.P.map g) ys) i) := fun i hi => by
      have q1 : extLV N.M (resV N.M g ρ) ys' i = extLV N.M (fun j => resV N.M g ρ (j + A'.n)) ys' i :=
        extLV_lt N.M _ _ ys' i (by rw [hl']; exact hi)
      have q2 : extLV N.M (resV N.M g ρ) (lmapG (N.M.P.map g) ys) i =
          extLV N.M (fun j => resV N.M g ρ (j + A'.n)) (lmapG (N.M.P.map g) ys) i :=
        extLV_lt N.M _ _ _ i (by rw [hyl]; exact hi)
      rw [q1, q2]
      exact (N.M.P.obj d).symm (e i hi)
    exact force_resp N.K N.M f (fun n => (N.M.P.obj d).symm (res_block N.M g (fun i => ρ (i + 0)) ys n))
      (move_vals T N hf _ ys' _ hl' hyl e' b.2)

end Sq

end Gt

end Found

#print axioms Found.Gt.comp_proj
#print axioms Found.Gt.move_vals
#print axioms Found.Gt.pb_square

found/Gt22.lean

lean_all/found/Gt22.lean

/-
  T194 (classifying topos), part 22. found/Gt22 — the coverage of the
  syntactic site: a sieve S on A covers when, in every sheaf model, every
  element ρ of A has, locally, an arrow u ∈ S and a source value σ with
  u(σ, ρ). The stages where this happens form a sieve; the maximal sieve
  covers (by the identity arrow).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt21

namespace Found

namespace Gt

open Sh

variable (T : Seq → Prop)

/-- the stages where some arrow of S hits ρ -/
def hitS (N : SModel T) {A : SObj} (S : Sieve (SynCat T) A) {c : N.D.Ob} (ρ : Nat → (N.M.P.obj c).car) :
    Sieve N.D c where
  S := fun {d} g => ∃ (B : SObj) (u : (SynCat T).Hom B A), S.S u ∧ ∃ (σ : Nat → (N.M.P.obj d).car) (ys : List (N.M.P.obj d).car),
    lenG ys = A.n ∧ (∀ i, i < A.n → (N.M.P.obj d).eqv (extLV N.M σ ys i) (resV N.M g ρ i)) ∧
    force N.K N.M d (extLV N.M σ ys) u.1
  resp := fun e ⟨B, u, hu, σ, ys, hl, he, hf⟩ => ⟨B, u, hu, σ, ys, hl,
    fun i hi => (N.M.P.obj _).trans (he i hi) (N.M.P.map_resp (ρ i) e), hf⟩
  stable := fun {_ _} {g} ⟨B, u, hu, σ, ys, hl, he, hf⟩ k => ⟨B, u, hu, resV N.M k σ, lmapG (N.M.P.map k) ys,
    (len_lmapG _ ys).trans hl,
    fun i hi => (N.M.P.obj _).trans ((N.M.P.obj _).symm (res_block N.M k σ ys i))
      ((N.M.P.obj _).trans (N.M.P.map_eqv k (he i hi)) (res_comp N.M g k ρ i)),
    force_resp N.K N.M u.1 (res_block N.M k σ ys) (force_stable N.K N.M u.1 k _ hf)⟩

def CovT {A : SObj} (S : Sieve (SynCat T) A) : Prop :=
  ∀ (N : SModel T) c (ρ : Nat → (N.M.P.obj c).car), force N.K N.M c ρ A.φ → N.K.cov (hitS T N S ρ)

theorem covT_resp {A : SObj} {S S' : Sieve (SynCat T) A} (h : SEq S S') (hS : CovT T S) : CovT T S' :=
  fun N c ρ hφ => cov_mono N.K (hS N c ρ hφ) (fun _ _ ⟨B, u, hu, rest⟩ => ⟨B, u, (h B u).1 hu, rest⟩)

/-- the identity arrow hits every element -/
theorem id_hits (N : SModel T) (A : SObj) {d : N.D.Ob} (ρ : Nat → (N.M.P.obj d).car) (hφ : force N.K N.M d ρ A.φ) :
    force N.K N.M d (extLV N.M ρ (witL N.M ρ A.n)) (idF A) := by
  have hwl := len_witL N.M ρ A.n
  refine ⟨(force_ren N.K N.M A.φ _ _).2 (force_eqval N.K N.M
    (fun i => (shift_val N.M ρ _ A.n hwl i).symm) A.φ hφ), (force_eqBlock N.K N.M A.n _).2 (fun i hi => ?_)⟩
  rw [witL_val N.M ρ A.n ρ i hi, shift_val N.M ρ _ A.n hwl i]
  exact (N.M.P.obj d).refl _

theorem covT_top (A : SObj) : CovT T (maxS A) := fun N c ρ hφ =>
  cov_mono N.K (N.K.top c) (fun d g _ => ⟨A, (SynCat T).id A, trivial, resV N.M g ρ, witL N.M (resV N.M g ρ) A.n,
    len_witL N.M _ A.n, fun i hi => by rw [witL_val N.M _ A.n _ i hi]; exact (N.M.P.obj d).refl _,
    id_hits T N A (resV N.M g ρ) (force_stable N.K N.M A.φ g ρ hφ)⟩)

end Gt

end Found

#print axioms Found.Gt.hitS
#print axioms Found.Gt.covT_resp
#print axioms Found.Gt.covT_top

found/Gt23.lean

lean_all/found/Gt23.lean

/-
  T194 (classifying topos), part 23. found/Gt23 — the coverage is stable
  under pullback: if S covers A and f : A' → A, then f*S covers A'; the
  witnesses are the first projections out of pullback objects.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt22

namespace Found

namespace Gt

open Sh

variable (T : Seq → Prop)

theorem extLV_base {D : ECat.{0, 0}} (M : @GStr D) {c : D.Ob} {β β' : Nat → (M.P.obj c).car} (h : ∀ n, β n = β' n) :
    (ds : List (M.P.obj c).car) → ∀ n, extLV M β ds n = extLV M β' ds n
  | [], n => h n
  | a :: ds, n => extLV_base M (β := extV M β a) (β' := extV M β' a) (fun k => match k with
    | 0 => rfl
    | k + 1 => h k) ds n

theorem covT_pull {A A' : SObj} (f : (SynCat T).Hom A' A) {S : Sieve (SynCat T) A} (hS : CovT T S) :
    CovT T (pullS f S) := by
  intro N c ρ hφ
  have W1 := exN_wit N.K N.M f.1 A.n ρ (f.2.2.2.1 N c ρ hφ)
  refine N.K.trans W1 (fun d g ⟨ys, hl, ff⟩ => ?_)
  have hA := (f.2.2.1 N d (resV N.M g ρ) ys hl ff).2
  refine cov_mono N.K (hS N d _ hA) (fun e h ⟨B, u, hu, σ, ys2, hl2, he2, fu⟩ => ?_)
  let P := pbObj T u.2 f.2
  let π' : (SynCat T).Hom P A' := ⟨projF P A'.n 0, pi1_arr T u.2 f.2⟩
  let π : (SynCat T).Hom P B := ⟨projF P B.n A'.n, pi2_arr T u.2 f.2⟩
  have sq : (SynCat T).heq ((SynCat T).comp π' f) ((SynCat T).comp π u) := pb_square T u.2 f.2
  have hπ' : (pullS f S).S π' := S.resp ((SynCat T).hsymm sq) (S.stable hu π)
  let ρe := resV N.M h (resV N.M g ρ)
  have hwl := len_witL N.M ρe A'.n
  let σ' := extLV N.M σ (witL N.M ρe A'.n)
  have σB : ∀ j, σ' (j + A'.n) = σ j := fun j => shift_val N.M σ _ A'.n hwl j
  have σA : ∀ j, j < A'.n → σ' j = ρe j := fun j hj => witL_val N.M ρe A'.n σ j hj
  refine ⟨P, π', hπ', σ', witL N.M ρe A'.n, hwl, fun i hi => ?_, ?_, ?_⟩
  · rw [witL_val N.M ρe A'.n σ' i hi]
    exact res_comp N.M g h ρ i
  · -- the pullback formula at σ'
    refine (force_ren N.K N.M _ _ _).2 (force_eqval N.K N.M (fun i => (shift_val N.M σ' _ A'.n hwl i).symm) _ ?_)
    refine exN_of N.K N.M _ A.n σ' ys2 hl2 ((pb_body T N (θ := u.1) (f := f.1) σ' ys2 hl2).2 ⟨?_, ?_⟩)
    · exact force_eqval N.K N.M (fun n => (extLV_base N.M (fun j => (σB j).symm) ys2 n)) _ fu
    · have hyh : lenG (lmapG (N.M.P.map h) ys) = A.n := (len_lmapG _ ys).trans hl
      have f1 := force_resp N.K N.M f.1 (res_block N.M h (resV N.M g ρ) ys) (force_stable N.K N.M f.1 h _ ff)
      refine force_agree N.K N.M f.1 (A.n + A'.n) f.2.1 (fun j hj => ?_) f1
      exact match Nat.decLt j A.n with
      | isTrue hj' => by
          rw [extLV_lt N.M σ' σ ys2 j (by rw [hl2]; exact hj')]
          exact (N.M.P.obj e).symm ((N.M.P.obj e).trans (he2 j hj') (res_block N.M h (resV N.M g ρ) ys j))
      | isFalse hj' => match not_lt_split hj' with
        | ⟨j', eq⟩ => by
          subst eq
          have hj'' : j' < A'.n := Nat.lt_of_add_lt_add_right (by rw [Nat.add_comm A.n A'.n] at hj; exact hj)
          rw [shift_val N.M _ _ A.n hyh j', shift_val N.M _ _ A.n hl2 j', σA j' hj'']
          exact (N.M.P.obj e).refl _
  · refine (force_eqOff N.K N.M A'.n (A'.n + 0) _).2 (fun i hi => ?_)
    rw [witL_val N.M ρe A'.n σ' i hi]
    show (N.M.P.obj e).eqv (ρe i) (extLV N.M σ' (witL N.M ρe A'.n) (i + A'.n))
    rw [shift_val N.M σ' _ A'.n hwl i, σA i hi]
    exact (N.M.P.obj e).refl _

end Gt

end Found

#print axioms Found.Gt.covT_pull

found/Gt24.lean

lean_all/found/Gt24.lean

/-
  T194 (classifying topos), part 24. found/Gt24 — the coverage is
  transitive (an element hit by u ∈ S, whose source value is hit by
  v ∈ u*R, is hit by v ; u ∈ R), so (C_T, J_T) is a site: the syntactic
  site of the geometric theory T.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt23

namespace Found

namespace Gt

open Sh

variable (T : Seq → Prop)

theorem covT_trans {A : SObj} {S R : Sieve (SynCat T) A} (hS : CovT T S)
    (h : ∀ B (u : (SynCat T).Hom B A), S.S u → CovT T (pullS u R)) : CovT T R := by
  intro N c ρ hφ
  refine N.K.trans (hS N c ρ hφ) (fun d g ⟨B, u, hu, σ, ys, hl, he, fu⟩ => ?_)
  have hB := (u.2.2.1 N d σ ys hl fu).1
  refine cov_mono N.K (h B u hu N d σ hB) (fun e k ⟨B', v, hv, σ2, ys2, hl2, he2, fv⟩ => ?_)
  have hyk : lenG (lmapG (N.M.P.map k) ys) = A.n := (len_lmapG _ ys).trans hl
  refine ⟨B', (SynCat T).comp v u, hv, σ2, lmapG (N.M.P.map k) ys, hyk, fun i hi => ?_, ?_⟩
  · rw [extLV_lt N.M σ2 (resV N.M k σ) _ i (by rw [hyk]; exact hi)]
    exact (N.M.P.obj e).trans ((N.M.P.obj e).symm (res_block N.M k σ ys i))
      ((N.M.P.obj e).trans (N.M.P.map_eqv k (he i hi)) (res_comp N.M g k ρ i))
  · have u1 := force_resp N.K N.M u.1 (res_block N.M k σ ys) (force_stable N.K N.M u.1 k _ fu)
    have u2 : force N.K N.M e (extLV N.M (extLV N.M σ2 ys2) (lmapG (N.M.P.map k) ys)) u.1 := by
      refine force_agree N.K N.M u.1 (A.n + B.n) u.2.1 (fun j hj => ?_) u1
      exact match Nat.decLt j A.n with
      | isTrue hj' => by
        rw [extLV_lt N.M (resV N.M k σ) (extLV N.M σ2 ys2) _ j (by rw [hyk]; exact hj')]
        exact (N.M.P.obj e).refl _
      | isFalse hj' => match not_lt_split hj' with
        | ⟨j', eq⟩ => by
          subst eq
          have hj'' : j' < B.n := Nat.lt_of_add_lt_add_right (by rw [Nat.add_comm A.n B.n] at hj; exact hj)
          rw [shift_val N.M _ _ A.n hyk j', shift_val N.M _ _ A.n hyk j']
          exact (N.M.P.obj e).symm (he2 j' hj'')
    exact exN_of N.K N.M _ B.n _ ys2 hl2
      ((force_body N.K N.M v.1 u.1 B.n A.n u.2.1 σ2 _ ys2 hl2 hyk).2 ⟨fv, u2⟩)

/-- the syntactic site of T -/
def JT : GTop (SynCat T) where
  cov := CovT T
  resp := covT_resp T
  top := covT_top T
  pull := fun {_ _} f {_} hS => covT_pull T f hS
  trans := fun hS h => covT_trans T hS (fun B u hu => h B u hu)

end Gt

end Found

#print axioms Found.Gt.covT_trans
#print axioms Found.Gt.JT

found/Gt25.lean

lean_all/found/Gt25.lean

/-
  T194 (classifying topos), part 25. found/Gt25 — a diagonal enumeration
  of ℕ × ℕ (onto), used to combine countably many countable families.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt24

namespace Found

namespace Gt

def dg : Nat → Nat × Nat
  | 0 => (0, 0)
  | k + 1 => match dg k with
    | (a, 0) => (0, a + 1)
    | (a, b + 1) => (a + 1, b)

theorem dg_succ_zero {k a : Nat} (h : dg k = (a, 0)) : dg (k + 1) = (0, a + 1) := by
  show (match dg k with
    | (a, 0) => (0, a + 1)
    | (a, b + 1) => (a + 1, b)) = (0, a + 1)
  rw [h]

theorem dg_succ_succ {k a b : Nat} (h : dg k = (a, b + 1)) : dg (k + 1) = (a + 1, b) := by
  show (match dg k with
    | (a, 0) => (0, a + 1)
    | (a, b + 1) => (a + 1, b)) = (a + 1, b)
  rw [h]

theorem dg_onto_aux : (s : Nat) → ∀ a b, a + b = s → ∃ k, dg k = (a, b)
  | 0, a, b, h => match a, b, h with
    | 0, 0, _ => ⟨0, rfl⟩
    | _ + 1, _, h => (Nat.noConfusion (h.symm.trans (Nat.succ_add _ _)).symm)
    | 0, _ + 1, h => Nat.noConfusion h
  | s + 1, a, b, h => by
    induction a generalizing b with
    | zero =>
      match b with
      | 0 => exact Nat.noConfusion h
      | b' + 1 =>
        have hb : b' = s := Nat.succ.inj (by rw [Nat.zero_add] at h; exact h)
        match dg_onto_aux s b' 0 (by rw [Nat.add_zero]; exact hb) with
        | ⟨k, hk⟩ => exact ⟨k + 1, dg_succ_zero hk⟩
    | succ a ih =>
      match ih (b + 1) (by rw [Nat.add_succ, ← Nat.succ_add]; exact h) with
      | ⟨k, hk⟩ => exact ⟨k + 1, dg_succ_succ hk⟩

theorem dg_onto (a b : Nat) : ∃ k, dg k = (a, b) := dg_onto_aux (a + b) a b rfl

end Gt

end Found

#print axioms Found.Gt.dg_onto

found/Gt26.lean

lean_all/found/Gt26.lean

/-
  T194 (classifying topos), part 26. found/Gt26 — the countable syntactic
  coverage: a sieve covers when some countable family of its arrows hits
  every element locally. Hitting by a pullback projection (factored out of
  Gt23), and the countable families.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt25

namespace Found

namespace Gt

open Sh

variable (T : Seq → Prop)

/-- the first projection of a pullback hits the element ρ at the stage where u hits f(ρ) -/
theorem pull_hit {A A' B : SObj} (f : (SynCat T).Hom A' A) (u : (SynCat T).Hom B A) (N : SModel T)
    {c d e : N.D.Ob} (g : N.D.Hom d c) (h : N.D.Hom e d) (ρ : Nat → (N.M.P.obj c).car)
    (ys : List (N.M.P.obj d).car) (hl : lenG ys = A.n) (ff : force N.K N.M d (extLV N.M (resV N.M g ρ) ys) f.1)
    (σ : Nat → (N.M.P.obj e).car) (ys2 : List (N.M.P.obj e).car) (hl2 : lenG ys2 = A.n)
    (he2 : ∀ i, i < A.n → (N.M.P.obj e).eqv (extLV N.M σ ys2 i) (resV N.M h (extLV N.M (resV N.M g ρ) ys) i))
    (fu : force N.K N.M e (extLV N.M σ ys2) u.1) :
    ∃ (σ' : Nat → (N.M.P.obj e).car) (ys' : List (N.M.P.obj e).car), lenG ys' = A'.n ∧
      (∀ i, i < A'.n → (N.M.P.obj e).eqv (extLV N.M σ' ys' i) (resV N.M (N.D.comp h g) ρ i)) ∧
      force N.K N.M e (extLV N.M σ' ys') (projF (pbObj T u.2 f.2) A'.n 0) := by
  let ρe := resV N.M h (resV N.M g ρ)
  have hwl := len_witL N.M ρe A'.n
  let σ' := extLV N.M σ (witL N.M ρe A'.n)
  have σB : ∀ j, σ' (j + A'.n) = σ j := fun j => shift_val N.M σ _ A'.n hwl j
  have σA : ∀ j, j < A'.n → σ' j = ρe j := fun j hj => witL_val N.M ρe A'.n σ j hj
  refine ⟨σ', witL N.M ρe A'.n, hwl, fun i hi => ?_, ?_, ?_⟩
  · rw [witL_val N.M ρe A'.n σ' i hi]
    exact res_comp N.M g h ρ i
  · -- the pullback formula at σ'
    refine (force_ren N.K N.M _ _ _).2 (force_eqval N.K N.M (fun i => (shift_val N.M σ' _ A'.n hwl i).symm) _ ?_)
    refine exN_of N.K N.M _ A.n σ' ys2 hl2 ((pb_body T N (θ := u.1) (f := f.1) σ' ys2 hl2).2 ⟨?_, ?_⟩)
    · exact force_eqval N.K N.M (fun n => (extLV_base N.M (fun j => (σB j).symm) ys2 n)) _ fu
    · have hyh : lenG (lmapG (N.M.P.map h) ys) = A.n := (len_lmapG _ ys).trans hl
      have f1 := force_resp N.K N.M f.1 (res_block N.M h (resV N.M g ρ) ys) (force_stable N.K N.M f.1 h _ ff)
      refine force_agree N.K N.M f.1 (A.n + A'.n) f.2.1 (fun j hj => ?_) f1
      exact match Nat.decLt j A.n with
      | isTrue hj' => by
          rw [extLV_lt N.M σ' σ ys2 j (by rw [hl2]; exact hj')]
          exact (N.M.P.obj e).symm ((N.M.P.obj e).trans (he2 j hj') (res_block N.M h (resV N.M g ρ) ys j))
      | isFalse hj' => match not_lt_split hj' with
        | ⟨j', eq⟩ => by
          subst eq
          have hj'' : j' < A'.n := Nat.lt_of_add_lt_add_right (by rw [Nat.add_comm A.n A'.n] at hj; exact hj)
          rw [shift_val N.M _ _ A.n hyh j', shift_val N.M _ _ A.n hl2 j', σA j' hj'']
          exact (N.M.P.obj e).refl _
  · refine (force_eqOff N.K N.M A'.n (A'.n + 0) _).2 (fun i hi => ?_)
    rw [witL_val N.M ρe A'.n σ' i hi]
    show (N.M.P.obj e).eqv (ρe i) (extLV N.M σ' (witL N.M ρe A'.n) (i + A'.n))
    rw [shift_val N.M σ' _ A'.n hwl i, σA i hi]
    exact (N.M.P.obj e).refl _


/-- a countable family of arrows into A -/
structure CFam (A : SObj) where
  B : Nat → SObj
  u : (n : Nat) → (SynCat T).Hom (B n) A

/-- the stages where some member of the family hits ρ -/
def hitF (N : SModel T) {A : SObj} (F : CFam T A) {c : N.D.Ob} (ρ : Nat → (N.M.P.obj c).car) : Sieve N.D c where
  S := fun {d} g => ∃ (n : Nat) (σ : Nat → (N.M.P.obj d).car) (ys : List (N.M.P.obj d).car),
    lenG ys = A.n ∧ (∀ i, i < A.n → (N.M.P.obj d).eqv (extLV N.M σ ys i) (resV N.M g ρ i)) ∧
    force N.K N.M d (extLV N.M σ ys) (F.u n).1
  resp := fun e ⟨n, σ, ys, hl, he, hf⟩ => ⟨n, σ, ys, hl,
    fun i hi => (N.M.P.obj _).trans (he i hi) (N.M.P.map_resp (ρ i) e), hf⟩
  stable := fun {_ _} {g} ⟨n, σ, ys, hl, he, hf⟩ k => ⟨n, resV N.M k σ, lmapG (N.M.P.map k) ys,
    (len_lmapG _ ys).trans hl,
    fun i hi => (N.M.P.obj _).trans ((N.M.P.obj _).symm (res_block N.M k σ ys i))
      ((N.M.P.obj _).trans (N.M.P.map_eqv k (he i hi)) (res_comp N.M g k ρ i)),
    force_resp N.K N.M (F.u n).1 (res_block N.M k σ ys) (force_stable N.K N.M (F.u n).1 k _ hf)⟩

/-- countable coverage -/
def CovC {A : SObj} (S : Sieve (SynCat T) A) : Prop :=
  ∃ F : CFam T A, (∀ n, S.S (F.u n)) ∧
    ∀ (N : SModel T) c (ρ : Nat → (N.M.P.obj c).car), force N.K N.M c ρ A.φ → N.K.cov (hitF T N F ρ)

theorem covC_resp {A : SObj} {S S' : Sieve (SynCat T) A} (h : SEq S S') (hS : CovC T S) : CovC T S' :=
  match hS with
  | ⟨F, hF, hc⟩ => ⟨F, fun n => (h _ (F.u n)).1 (hF n), hc⟩

theorem covC_top (A : SObj) : CovC T (maxS A) :=
  ⟨⟨fun _ => A, fun _ => (SynCat T).id A⟩, fun _ => trivial, fun N c ρ hφ =>
    cov_mono N.K (N.K.top c) (fun d g _ => ⟨0, resV N.M g ρ, witL N.M (resV N.M g ρ) A.n,
      len_witL N.M _ A.n, fun i hi => by rw [witL_val N.M _ A.n _ i hi]; exact (N.M.P.obj d).refl _,
      id_hits T N A (resV N.M g ρ) (force_stable N.K N.M A.φ g ρ hφ)⟩)⟩

theorem covC_pull {A A' : SObj} (f : (SynCat T).Hom A' A) {S : Sieve (SynCat T) A} (hS : CovC T S) :
    CovC T (pullS f S) :=
  match hS with
  | ⟨F, hF, hc⟩ =>
    ⟨⟨fun n => pbObj T (F.u n).2 f.2, fun n => ⟨projF (pbObj T (F.u n).2 f.2) A'.n 0, pi1_arr T (F.u n).2 f.2⟩⟩,
     fun n =>
      have sq : (SynCat T).heq ((SynCat T).comp ⟨projF (pbObj T (F.u n).2 f.2) A'.n 0, pi1_arr T (F.u n).2 f.2⟩ f)
          ((SynCat T).comp ⟨projF (pbObj T (F.u n).2 f.2) (F.B n).n A'.n, pi2_arr T (F.u n).2 f.2⟩ (F.u n)) :=
        pb_square T (F.u n).2 f.2
      S.resp ((SynCat T).hsymm sq) (S.stable (hF n) _),
     fun N c ρ hφ => by
      have W1 := exN_wit N.K N.M f.1 A.n ρ (f.2.2.2.1 N c ρ hφ)
      refine N.K.trans W1 (fun d g ⟨ys, hl, ff⟩ => ?_)
      have hA := (f.2.2.1 N d (resV N.M g ρ) ys hl ff).2
      refine cov_mono N.K (hc N d _ hA) (fun e h ⟨n, σ, ys2, hl2, he2, fu⟩ => ?_)
      match pull_hit T f (F.u n) N g h ρ ys hl ff σ ys2 hl2 he2 fu with
      | ⟨σ', ys', hl', he', hf'⟩ => exact ⟨n, σ', ys', hl', he', hf'⟩⟩

/-- the hit of a composite v ; u (u hits ρ at σ, v hits σ) -/
theorem comp_hit {A B B' : SObj} (u : (SynCat T).Hom B A) (v : (SynCat T).Hom B' B) (N : SModel T)
    {d e : N.D.Ob} (k : N.D.Hom e d) (σ : Nat → (N.M.P.obj d).car) (ys : List (N.M.P.obj d).car) (hl : lenG ys = A.n)
    (fu : force N.K N.M d (extLV N.M σ ys) u.1) (σ2 : Nat → (N.M.P.obj e).car) (ys2 : List (N.M.P.obj e).car)
    (hl2 : lenG ys2 = B.n) (he2 : ∀ i, i < B.n → (N.M.P.obj e).eqv (extLV N.M σ2 ys2 i) (resV N.M k σ i))
    (fv : force N.K N.M e (extLV N.M σ2 ys2) v.1) :
    force N.K N.M e (extLV N.M σ2 (lmapG (N.M.P.map k) ys)) ((SynCat T).comp v u).1 := by
  have hyk : lenG (lmapG (N.M.P.map k) ys) = A.n := (len_lmapG _ ys).trans hl
  have u1 := force_resp N.K N.M u.1 (res_block N.M k σ ys) (force_stable N.K N.M u.1 k _ fu)
  have u2 : force N.K N.M e (extLV N.M (extLV N.M σ2 ys2) (lmapG (N.M.P.map k) ys)) u.1 := by
    refine force_agree N.K N.M u.1 (A.n + B.n) u.2.1 (fun j hj => ?_) u1
    exact match Nat.decLt j A.n with
    | isTrue hj' => by
      rw [extLV_lt N.M (resV N.M k σ) (extLV N.M σ2 ys2) _ j (by rw [hyk]; exact hj')]
      exact (N.M.P.obj e).refl _
    | isFalse hj' => match not_lt_split hj' with
      | ⟨j', eq⟩ => by
        subst eq
        have hj'' : j' < B.n := Nat.lt_of_add_lt_add_right (by rw [Nat.add_comm A.n B.n] at hj; exact hj)
        rw [shift_val N.M _ _ A.n hyk j', shift_val N.M _ _ A.n hyk j']
        exact (N.M.P.obj e).symm (he2 j' hj'')
  exact exN_of N.K N.M _ B.n _ ys2 hl2 ((force_body N.K N.M v.1 u.1 B.n A.n u.2.1 σ2 _ ys2 hl2 hyk).2 ⟨fv, u2⟩)

theorem covC_trans (K : CP.Classical') {A : SObj} {S R : Sieve (SynCat T) A} (hS : CovC T S)
    (h : ∀ B (u : (SynCat T).Hom B A), S.S u → CovC T (pullS u R)) : CovC T R := by
  match hS with
  | ⟨F, hF, hc⟩ =>
  let G : (n : Nat) → CFam T (F.B n) := fun n => K.ch (fun G' => (∀ m, (pullS (F.u n) R).S (G'.u m)) ∧
    ∀ (N : SModel T) c (ρ : Nat → (N.M.P.obj c).car), force N.K N.M c ρ (F.B n).φ → N.K.cov (hitF T N G' ρ))
    (h _ (F.u n) (hF n))
  have hG : ∀ n, (∀ m, (pullS (F.u n) R).S ((G n).u m)) ∧
      ∀ (N : SModel T) c (ρ : Nat → (N.M.P.obj c).car), force N.K N.M c ρ (F.B n).φ → N.K.cov (hitF T N (G n) ρ) :=
    fun n => K.ch_spec _ (h _ (F.u n) (hF n))
  let uP : (p : Nat × Nat) → (SynCat T).Hom ((G p.1).B p.2) A := fun p => (SynCat T).comp ((G p.1).u p.2) (F.u p.1)
  refine ⟨⟨fun k => (G (dg k).1).B (dg k).2, fun k => uP (dg k)⟩, fun k => (hG (dg k).1).1 (dg k).2, ?_⟩
  intro N c ρ hφ
  refine N.K.trans (hc N c ρ hφ) (fun d g ⟨n, σ, ys, hl, he, fu⟩ => ?_)
  have hB := ((F.u n).2.2.1 N d σ ys hl fu).1
  refine cov_mono N.K ((hG n).2 N d σ hB) (fun e k ⟨m, σ2, ys2, hl2, he2, fv⟩ => ?_)
  match dg_onto n m with
  | ⟨k', hk⟩ =>
  have hyk : lenG (lmapG (N.M.P.map k) ys) = A.n := (len_lmapG _ ys).trans hl
  refine ⟨k', σ2, lmapG (N.M.P.map k) ys, hyk, fun i hi => ?_, ?_⟩
  · rw [extLV_lt N.M σ2 (resV N.M k σ) _ i (by rw [hyk]; exact hi)]
    exact (N.M.P.obj e).trans ((N.M.P.obj e).symm (res_block N.M k σ ys i))
      ((N.M.P.obj e).trans (N.M.P.map_eqv k (he i hi)) (res_comp N.M g k ρ i))
  · have hc' := comp_hit T (F.u n) ((G n).u m) N k σ ys hl fu σ2 ys2 hl2 he2 fv
    show force N.K N.M e (extLV N.M σ2 (lmapG (N.M.P.map k) ys)) (uP (dg k')).1
    rw [hk]
    exact hc'

/-- the countable syntactic site -/
def JTc (K : CP.Classical') : GTop (SynCat T) where
  cov := CovC T
  resp := covC_resp T
  top := covC_top T
  pull := fun {_ _} f {_} hS => covC_pull T f hS
  trans := fun hS h => covC_trans T K hS (fun B u hu => h B u hu)

end Gt

end Found

#print axioms Found.Gt.pull_hit
#print axioms Found.Gt.covC_resp
#print axioms Found.Gt.covC_top
#print axioms Found.Gt.covC_pull
#print axioms Found.Gt.comp_hit
#print axioms Found.Gt.covC_trans
#print axioms Found.Gt.JTc

found/Gt27.lean

lean_all/found/Gt27.lean

/-
  T194 (classifying topos), part 27. found/Gt27 — the gluing formula for
  representable sheaves on the syntactic site: for a covering family
  F_n : B_n → A and arrows m_n : B_n → X, the formula
  A.φ(ȳ) ∧ ⋁_n ∃x̄ (F_n(x̄, ȳ) ∧ m_n(x̄, z̄)); the renamings into its context
  and their valuation lemmas.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt26

namespace Found

namespace Gt

open Sh

/-- F_n (targets ȳ of size a, sources x̄ of size b) into the context x̄, z̄ (size k), ȳ -/
def rF (a b k : Nat) (i : Nat) : Nat :=
  match Nat.decLt i a with
  | isTrue _ => i + k + b
  | isFalse _ => subN i a

/-- m_n (targets z̄ of size k, sources x̄ of size b) into the same context -/
def rM (b k : Nat) (i : Nat) : Nat :=
  match Nat.decLt i k with
  | isTrue _ => i + b
  | isFalse _ => subN i k

def glF (A X : SObj) (B : Nat → SObj) (fu mx : Nat → GFm) : GFm :=
  .conj (renG (· + X.n) A.φ) (.sup (fun n => exN (B n).n
    (.conj (renG (rF A.n (B n).n X.n) (fu n)) (renG (rM (B n).n X.n) (mx n)))))

variable {C : ECat.{0, 0}} (M : @GStr C)

theorem val_rF {c : C.Ob} (ρ : Nat → (M.P.obj c).car) (zs xs : List (M.P.obj c).car) (a b k : Nat)
    (hk : lenG zs = k) (hb : lenG xs = b) :
    ∀ i, i < a + b → extLV M (extLV M ρ zs) xs (rF a b k i) = extLV M (extLV M ρ xs) (witL M ρ a) i := fun i hi => by
  have hw := len_witL M ρ a
  unfold rF
  match Nat.decLt i a with
  | isTrue h =>
    show extLV M (extLV M ρ zs) xs (i + k + b) = extLV M (extLV M ρ xs) (witL M ρ a) i
    rw [shift_val M _ xs b hb, shift_val M ρ zs k hk, witL_val M ρ a _ i h]
  | isFalse h => match not_lt_split h with
    | ⟨j, e⟩ =>
      subst e
      have hj : j < b := Nat.lt_of_add_lt_add_right (by rw [Nat.add_comm a b] at hi; exact hi)
      show extLV M (extLV M ρ zs) xs (subN (j + a) a) = extLV M (extLV M ρ xs) (witL M ρ a) (j + a)
      rw [subN_add, shift_val M _ _ a hw j]
      exact extLV_lt M _ _ xs j (by rw [hb]; exact hj)

theorem val_rM {c : C.Ob} (ρ : Nat → (M.P.obj c).car) (zs xs : List (M.P.obj c).car) (b k : Nat)
    (hk : lenG zs = k) (hb : lenG xs = b) :
    ∀ i, i < k + b → extLV M (extLV M ρ zs) xs (rM b k i) = extLV M (extLV M ρ xs) zs i := fun i hi => by
  unfold rM
  match Nat.decLt i k with
  | isTrue h =>
    show extLV M (extLV M ρ zs) xs (i + b) = extLV M (extLV M ρ xs) zs i
    rw [shift_val M _ xs b hb]
    exact extLV_lt M _ _ zs i (by rw [hk]; exact h)
  | isFalse h => match not_lt_split h with
    | ⟨j, e⟩ =>
      subst e
      have hj : j < b := Nat.lt_of_add_lt_add_right (by rw [Nat.add_comm k b] at hi; exact hi)
      show extLV M (extLV M ρ zs) xs (subN (j + k) k) = extLV M (extLV M ρ xs) zs (j + k)
      rw [subN_add, shift_val M _ zs k hk j]
      exact extLV_lt M _ _ xs j (by rw [hb]; exact hj)

end Gt

end Found

#print axioms Found.Gt.val_rF
#print axioms Found.Gt.val_rM

found/Gt28.lean

lean_all/found/Gt28.lean

/-
  T194 (classifying topos), part 28. found/Gt28 — the disjuncts of the
  gluing formula: forcing F_n(x̄, ȳ) ∧ m_n(x̄, z̄) in the glued context is
  forcing F_n and m_n in their own contexts; free variables of glF.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt27

namespace Found

namespace Gt

open Sh

variable {C : ECat.{0, 0}} (J : GTop C) (M : @GStr C)

theorem disj_iff (a b k : Nat) (φF φM : GFm) (hF : FVg (a + b) φF) (hM : FVg (k + b) φM) {c : C.Ob}
    (ρ : Nat → (M.P.obj c).car) (zs xs : List (M.P.obj c).car) (hk : lenG zs = k) (hb : lenG xs = b) :
    force J M c (extLV M (extLV M ρ zs) xs) (.conj (renG (rF a b k) φF) (renG (rM b k) φM)) ↔
      force J M c (extLV M (extLV M ρ xs) (witL M ρ a)) φF ∧ force J M c (extLV M (extLV M ρ xs) zs) φM := by
  have eF : ∀ i, i < a + b → (M.P.obj c).eqv (extLV M (extLV M ρ zs) xs (rF a b k i))
      (extLV M (extLV M ρ xs) (witL M ρ a) i) := fun i hi => by
    rw [val_rF M ρ zs xs a b k hk hb i hi]; exact (M.P.obj c).refl _
  have eM : ∀ i, i < k + b → (M.P.obj c).eqv (extLV M (extLV M ρ zs) xs (rM b k i))
      (extLV M (extLV M ρ xs) zs i) := fun i hi => by
    rw [val_rM M ρ zs xs b k hk hb i hi]; exact (M.P.obj c).refl _
  constructor
  · intro ⟨h1, h2⟩
    exact ⟨force_agree J M φF (a + b) hF eF ((force_ren J M φF _ _).1 h1),
      force_agree J M φM (k + b) hM eM ((force_ren J M φM _ _).1 h2)⟩
  · intro ⟨h1, h2⟩
    exact ⟨(force_ren J M φF _ _).2 (force_agree J M φF (a + b) hF (fun i hi => (M.P.obj c).symm (eF i hi)) h1),
      (force_ren J M φM _ _).2 (force_agree J M φM (k + b) hM (fun i hi => (M.P.obj c).symm (eM i hi)) h2)⟩

theorem fv_rF (a b k : Nat) : ∀ i, i < a + b → rF a b k i < (k + a) + b := fun i hi => by
  unfold rF
  match Nat.decLt i a with
  | isTrue h =>
    show i + k + b < k + a + b
    rw [Nat.add_comm i k]
    exact Nat.add_lt_add_right (Nat.add_lt_add_left h k) b
  | isFalse h => match not_lt_split h with
    | ⟨j, e⟩ =>
      subst e
      have hj : j < b := Nat.lt_of_add_lt_add_right (by rw [Nat.add_comm a b] at hi; exact hi)
      show subN (j + a) a < k + a + b
      rw [subN_add]
      exact Nat.lt_of_lt_of_le hj (Nat.le_add_left b _)

theorem fv_rM (a b k : Nat) : ∀ i, i < k + b → rM b k i < (k + a) + b := fun i hi => by
  unfold rM
  match Nat.decLt i k with
  | isTrue h =>
    show i + b < k + a + b
    exact Nat.add_lt_add_right (Nat.lt_of_lt_of_le h (Nat.le_add_right k a)) b
  | isFalse h => match not_lt_split h with
    | ⟨j, e⟩ =>
      subst e
      have hj : j < b := Nat.lt_of_add_lt_add_right (by rw [Nat.add_comm k b] at hi; exact hi)
      show subN (j + k) k < k + a + b
      rw [subN_add]
      exact Nat.lt_of_lt_of_le hj (Nat.le_add_left b _)

theorem fv_glF (A X : SObj) (B : Nat → SObj) (fu mx : Nat → GFm) (hF : ∀ n, FVg (A.n + (B n).n) (fu n))
    (hM : ∀ n, FVg (X.n + (B n).n) (mx n)) : FVg (X.n + A.n) (glF A X B fu mx) :=
  ⟨fv_ren A.φ (fun i hi => by rw [Nat.add_comm X.n A.n]; exact Nat.add_lt_add_right hi X.n) A.fv,
   fun n => fv_exN (B n).n ⟨fv_ren (fu n) (fv_rF A.n (B n).n X.n) (hF n),
     fv_ren (mx n) (fv_rM A.n (B n).n X.n) (hM n)⟩⟩

end Gt

end Found

#print axioms Found.Gt.disj_iff
#print axioms Found.Gt.fv_glF

found/Gt29.lean

lean_all/found/Gt29.lean

/-
  T194 (classifying topos), part 29. found/Gt29 — tools for reading the
  gluing formula at an arbitrary valuation: splitting a valuation into a
  block and the rest, agreement on two blocks, the decoded disjunct, and the
  covering sieve of stages where an explicit disjunct and witness exist.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt28

namespace Found

namespace Gt

open Sh

variable {C : ECat.{0, 0}} (J : GTop C) (M : @GStr C)

theorem split_val {c : C.Ob} (W : Nat → (M.P.obj c).car) (k : Nat) :
    ∀ n, extLV M (fun j => W (j + k)) (witL M W k) n = W n := fun n =>
  match Nat.decLt n k with
  | isTrue h => witL_val M W k _ n h
  | isFalse h => match not_lt_split h with
    | ⟨j, e⟩ => by
      subst e
      exact shift_val M (fun j => W (j + k)) (witL M W k) k (len_witL M W k) j

theorem agree_blocks {c : C.Ob} (V V' : Nat → (M.P.obj c).car) (ys ys' : List (M.P.obj c).car) (n m : Nat)
    (hl : lenG ys = n) (hl' : lenG ys' = n)
    (h1 : ∀ i, i < n → (M.P.obj c).eqv (extLV M V ys i) (extLV M V' ys' i))
    (h2 : ∀ j, j < m → (M.P.obj c).eqv (V j) (V' j)) :
    ∀ i, i < n + m → (M.P.obj c).eqv (extLV M V ys i) (extLV M V' ys' i) := fun i hi =>
  match Nat.decLt i n with
  | isTrue h => h1 i h
  | isFalse h => match not_lt_split h with
    | ⟨j, e⟩ => by
      subst e
      have hj : j < m := Nat.lt_of_add_lt_add_right (by rw [Nat.add_comm n m] at hi; exact hi)
      rw [shift_val M V ys n hl j, shift_val M V' ys' n hl' j]
      exact h2 j hj

/-- the disjunct at an arbitrary valuation W of the glued context -/
theorem disj_at (a b k : Nat) (φF φM : GFm) (hF : FVg (a + b) φF) (hM : FVg (k + b) φM) {c : C.Ob}
    (W : Nat → (M.P.obj c).car) (xs : List (M.P.obj c).car) (hb : lenG xs = b) :
    force J M c (extLV M W xs) (.conj (renG (rF a b k) φF) (renG (rM b k) φM)) ↔
      force J M c (extLV M (extLV M (fun j => W (j + k)) xs) (witL M (fun j => W (j + k)) a)) φF ∧
      force J M c (extLV M (extLV M (fun j => W (j + k)) xs) (witL M W k)) φM :=
  ⟨fun h => (disj_iff J M a b k φF φM hF hM _ _ xs (len_witL M W k) hb).1
      (force_eqval J M (fun n => (extLV_base M (fun i => (split_val M W k i).symm) xs n)) _ h),
   fun h => force_eqval J M (fun n => extLV_base M (split_val M W k) xs n) _
      ((disj_iff J M a b k φF φM hF hM _ _ xs (len_witL M W k) hb).2 h)⟩

section D

variable (A X : SObj) (B : Nat → SObj) (fu mx : Nat → GFm)

def bodyG (n : Nat) : GFm := .conj (renG (rF A.n (B n).n X.n) (fu n)) (renG (rM (B n).n X.n) (mx n))

/-- stages where an explicit disjunct with an explicit witness holds -/
def DecS {c : C.Ob} (W : Nat → (M.P.obj c).car) : Sieve C c where
  S := fun {d} g => ∃ (n : Nat) (xs : List (M.P.obj d).car), lenG xs = (B n).n ∧
    force J M d (extLV M (resV M g W) xs) (bodyG A X B fu mx n)
  resp := fun e ⟨n, xs, hl, h⟩ => ⟨n, xs, hl,
    force_resp J M _ (extLV_eqv M (fun i => M.P.map_resp (W i) e) xs) h⟩
  stable := fun {_ _} {g} ⟨n, xs, hl, h⟩ k => ⟨n, lmapG (M.P.map k) xs, (len_lmapG _ xs).trans hl,
    force_resp J M _ (fun i => (M.P.obj _).trans (res_block M k (resV M g W) xs i)
        (extLV_eqv M (res_comp M g k W) _ i))
      (force_stable J M _ k _ h)⟩

theorem cov_decS {c : C.Ob} (W : Nat → (M.P.obj c).car) (h : force J M c W (glF A X B fu mx)) :
    J.cov (DecS J M A X B fu mx W) :=
  match h.2 with
  | ⟨_, hS, hh⟩ => J.trans hS (fun d f hf => match hh d f hf with
    | ⟨n, hn⟩ => cov_mono J (exN_wit J M _ (B n).n _ hn) (fun _ g ⟨xs, hl, hb⟩ => ⟨n, xs, hl,
        force_resp J M _ (extLV_eqv M (res_comp M f g W) xs) hb⟩))

end D

end Gt

end Found

#print axioms Found.Gt.split_val
#print axioms Found.Gt.agree_blocks
#print axioms Found.Gt.disj_at
#print axioms Found.Gt.DecS
#print axioms Found.Gt.cov_decS

found/Gt3.lean

lean_all/found/Gt3.lean

/-
  T194 (classifying topos), part 3. found/Gt3 — P⁺ is separated: two
  elements of P⁺(c) that agree after restriction along a covering sieve
  agree on the sieve of arrows where both families are defined and equal,
  which covers by transitivity.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt2

namespace Found

namespace Gt

open Sh

variable {C : ECat.{0, 0}} (J : GTop C) (P : Psh.{0, 0, 0} C)

/-- the sieve where two elements of P⁺ agree -/
def agreeS {c : C.Ob} (a b : PElt J P c) : Sieve C c where
  S := fun h => ∃ (ha : a.S.S h) (hb : b.S.S h), (P.obj _).eqv (a.m.x h ha) (b.m.x h hb)
  resp := fun e ⟨ha, hb, q⟩ => ⟨a.S.resp e ha, b.S.resp e hb,
    (P.obj _).trans ((P.obj _).symm (a.m.resp ha (a.S.resp e ha) e)) ((P.obj _).trans q (b.m.resp hb (b.S.resp e hb) e))⟩
  stable := fun ⟨ha, hb, q⟩ g => ⟨a.S.stable ha g, b.S.stable hb g,
    (P.obj _).trans (a.m.compat _ ha g) ((P.obj _).trans (P.map_eqv g q) ((P.obj _).symm (b.m.compat _ hb g)))⟩

theorem plus_sep : Separated J (Plus J P) := fun c S hS a b hab => by
  refine ⟨agreeS J P a b, ?_, fun _ _ ⟨ha, hb, _⟩ => ⟨ha, hb⟩, fun _ _ ha hb ⟨ha', hb', q⟩ =>
    (P.obj _).trans (a.m.resp ha ha' (C.hrefl _)) ((P.obj _).trans q (b.m.resp hb' hb (C.hrefl _)))⟩
  have hT : J.cov (meetS S (meetS a.S b.S)) := cov_meet J hS (cov_meet J a.cov b.cov)
  refine J.trans hT (fun d f ⟨hfS, _, _⟩ => ?_)
  match hab d f hfS with
  | ⟨R, hR, inc, e⟩ =>
    exact cov_mono J hR (fun e' g hg => ⟨(inc e' g hg).1, (inc e' g hg).2, e e' g (inc e' g hg).1 (inc e' g hg).2 hg⟩)

end Gt

end Found

#print axioms Found.Gt.plus_sep

found/Gt30.lean

lean_all/found/Gt30.lean

/-
  T194 (classifying topos), part 30. found/Gt30 — the gluing formula of a
  covering family F_n : B_n → A and arrows m_n : B_n → X is typed
  (A.φ and X.φ hold) and total (every element of A has, locally, an image).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt29

namespace Found

namespace Gt

open Sh

variable (T : Seq → Prop) {A X : SObj} (F : CFam T A) (mx : (n : Nat) → (SynCat T).Hom (F.B n) X)

/-- the glued formula -/
def glT : GFm := glF A X F.B (fun n => (F.u n).1) (fun n => (mx n).1)

theorem glT_A (N : SModel T) {c : N.D.Ob} (ρ : Nat → (N.M.P.obj c).car) (ys : List (N.M.P.obj c).car)
    (hl : lenG ys = X.n) (h : force N.K N.M c (extLV N.M ρ ys) (glT T F mx)) : force N.K N.M c ρ A.φ :=
  force_eqval N.K N.M (shift_val N.M ρ ys X.n hl) A.φ ((force_ren N.K N.M A.φ _ _).1 h.1)

theorem glT_X (N : SModel T) {c : N.D.Ob} (ρ : Nat → (N.M.P.obj c).car) (ys : List (N.M.P.obj c).car)
    (h : force N.K N.M c (extLV N.M ρ ys) (glT T F mx)) : force N.K N.M c (extLV N.M ρ ys) X.φ :=
  force_local N.K N.M N.closed X.φ _ _ (cov_decS N.K N.M A X F.B _ _ _ h) (fun d g ⟨n, xs, hl, hb⟩ => by
    let W := resV N.M g (extLV N.M ρ ys)
    have hm := ((disj_at N.K N.M A.n (F.B n).n X.n _ _ (F.u n).2.1 (mx n).2.1 W xs hl).1 hb).2
    have hX := ((mx n).2.2.1 N d _ _ (len_witL N.M W X.n) hm).2
    exact force_agree N.K N.M X.φ X.n X.fv (fun i hi => by
      rw [witL_val N.M W X.n _ i hi]; exact (N.M.P.obj d).refl _) hX)

theorem glT_tot (hcov : ∀ (N : SModel T) c (ρ : Nat → (N.M.P.obj c).car), force N.K N.M c ρ A.φ →
      N.K.cov (hitF T N F ρ))
    (N : SModel T) {c : N.D.Ob} (ρ : Nat → (N.M.P.obj c).car) (hφ : force N.K N.M c ρ A.φ) :
    force N.K N.M c ρ (exN X.n (glT T F mx)) :=
  force_local N.K N.M N.closed _ ρ _ (hcov N c ρ hφ) (fun d g ⟨n, σ, ys, hl, he, hf⟩ => by
    have hB := ((F.u n).2.2.1 N d σ ys hl hf).1
    have hT := (mx n).2.2.2.1 N d σ hB
    refine force_local N.K N.M N.closed _ _ _ (exN_wit N.K N.M _ X.n σ hT) (fun e h ⟨zs, hzl, hm⟩ => ?_)
    let ρe := resV N.M h (resV N.M g ρ)
    let xs := witL N.M (resV N.M h σ) (F.B n).n
    have hxl : lenG xs = (F.B n).n := len_witL N.M _ _
    have hyl : lenG (lmapG (N.M.P.map h) ys) = A.n := (len_lmapG _ ys).trans hl
    have hf' := force_resp N.K N.M _ (res_block N.M h σ ys) (force_stable N.K N.M _ h _ hf)
    have Fp : force N.K N.M e (extLV N.M (extLV N.M ρe xs) (witL N.M ρe A.n)) (F.u n).1 :=
      force_agree N.K N.M _ (A.n + (F.B n).n) (F.u n).2.1
        (agree_blocks N.M _ _ _ _ A.n (F.B n).n hyl (len_witL N.M ρe A.n)
          (fun i hi => by
            rw [witL_val N.M ρe A.n _ i hi]
            exact (N.M.P.obj e).trans ((N.M.P.obj e).symm (res_block N.M h σ ys i))
              (N.M.P.map_eqv h (he i hi)))
          (fun j hj => by rw [witL_val N.M _ _ ρe j hj]; exact (N.M.P.obj e).refl _)) hf'
    have Mp : force N.K N.M e (extLV N.M (extLV N.M ρe xs) zs) (mx n).1 :=
      force_agree N.K N.M _ (X.n + (F.B n).n) (mx n).2.1
        (agree_blocks N.M _ _ _ _ X.n (F.B n).n hzl hzl
          (fun i hi => by
            rw [extLV_lt N.M (resV N.M h σ) (extLV N.M ρe xs) zs i (by rw [hzl]; exact hi)]
            exact (N.M.P.obj e).refl _)
          (fun j hj => by rw [witL_val N.M _ _ ρe j hj]; exact (N.M.P.obj e).refl _)) hm
    have D : force N.K N.M e (extLV N.M ρe zs) (exN (F.B n).n (bodyG A X F.B (fun n => (F.u n).1) (fun n => (mx n).1) n)) :=
      exN_of N.K N.M _ _ _ xs hxl ((disj_iff N.K N.M A.n (F.B n).n X.n _ _ (F.u n).2.1 (mx n).2.1 ρe zs xs hzl hxl).2 ⟨Fp, Mp⟩)
    have hφe := force_stable N.K N.M A.φ h _ (force_stable N.K N.M A.φ g ρ hφ)
    exact exN_of N.K N.M _ X.n ρe zs hzl ⟨(force_ren N.K N.M A.φ _ _).2
      (force_eqval N.K N.M (fun i => (shift_val N.M ρe zs X.n hzl i).symm) A.φ hφe),
      ⟨maxS e, N.K.top e, fun _ f _ => ⟨n, force_stable N.K N.M _ f _ D⟩⟩⟩)

end Gt

end Found

#print axioms Found.Gt.glT_A
#print axioms Found.Gt.glT_X
#print axioms Found.Gt.glT_tot

found/Gt31.lean

lean_all/found/Gt31.lean

/-
  T194 (classifying topos), part 31. found/Gt31 — the glued formula is
  single-valued when the arrows m_n agree on overlaps (semantic
  compatibility), hence an arrow A → X of the syntactic category.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt30

namespace Found

namespace Gt

open Sh

theorem witL_congr {C : ECat.{0, 0}} (M : @GStr C) {c : C.Ob} {τ τ' : Nat → (M.P.obj c).car} (h : ∀ j, τ j = τ' j) :
    (n : Nat) → witL M τ n = witL M τ' n
  | 0 => rfl
  | n + 1 => by
    show τ n :: witL M τ n = τ' n :: witL M τ' n
    rw [h n, witL_congr M h n]

variable (T : Seq → Prop) {A X : SObj} (F : CFam T A) (mx : (n : Nat) → (SynCat T).Hom (F.B n) X)

/-- semantic compatibility of the arrows on overlaps -/
def CmpG : Prop :=
  ∀ n k (N : SModel T) c (ρ : Nat → (N.M.P.obj c).car) (xs xs' zs zs' : List (N.M.P.obj c).car),
    lenG xs = (F.B n).n → lenG xs' = (F.B k).n → lenG zs = X.n → lenG zs' = X.n →
    force N.K N.M c (extLV N.M (extLV N.M ρ xs) (witL N.M ρ A.n)) (F.u n).1 →
    force N.K N.M c (extLV N.M (extLV N.M ρ xs') (witL N.M ρ A.n)) (F.u k).1 →
    force N.K N.M c (extLV N.M (extLV N.M ρ xs) zs) (mx n).1 →
    force N.K N.M c (extLV N.M (extLV N.M ρ xs') zs') (mx k).1 →
    ∀ i, i < X.n → (N.M.P.obj c).eqv (extLV N.M ρ zs i) (extLV N.M ρ zs' i)

theorem glT_fn (hc : CmpG T F mx) (N : SModel T) {c : N.D.Ob} (ρ : Nat → (N.M.P.obj c).car)
    (ys ys' : List (N.M.P.obj c).car) (hl : lenG ys = X.n) (hl' : lenG ys' = X.n)
    (h : force N.K N.M c (extLV N.M ρ ys) (glT T F mx)) (h' : force N.K N.M c (extLV N.M ρ ys') (glT T F mx)) :
    ∀ i, i < X.n → (N.M.P.obj c).eqv (extLV N.M ρ ys i) (extLV N.M ρ ys' i) := by
  let V := extLV N.M (extLV N.M ρ ys') ys
  have hV : force N.K N.M c V (eqOff X.n X.n) := by
    refine force_local N.K N.M N.closed _ V _ (cov_meet N.K (cov_decS N.K N.M A X F.B _ _ _ h)
      (cov_decS N.K N.M A X F.B _ _ _ h')) (fun d g ⟨⟨n, xs, hxl, hb⟩, ⟨k, xs', hxl', hb'⟩⟩ => ?_)
    let W1 := resV N.M g (extLV N.M ρ ys)
    let W2 := resV N.M g (extLV N.M ρ ys')
    have e12 : ∀ j, W2 (j + X.n) = W1 (j + X.n) := fun j => by
      show N.M.P.map g (extLV N.M ρ ys' (j + X.n)) = N.M.P.map g (extLV N.M ρ ys (j + X.n))
      rw [shift_val N.M ρ ys' X.n hl' j, shift_val N.M ρ ys X.n hl j]
    have d1 := (disj_at N.K N.M A.n (F.B n).n X.n _ _ (F.u n).2.1 (mx n).2.1 W1 xs hxl).1 hb
    have d2 := (disj_at N.K N.M A.n (F.B k).n X.n _ _ (F.u k).2.1 (mx k).2.1 W2 xs' hxl').1 hb'
    have wl : witL N.M (fun j => W2 (j + X.n)) A.n = witL N.M (fun j => W1 (j + X.n)) A.n := witL_congr N.M e12 A.n
    have F2 : force N.K N.M d (extLV N.M (extLV N.M (fun j => W1 (j + X.n)) xs')
        (witL N.M (fun j => W1 (j + X.n)) A.n)) (F.u k).1 := by
      rw [← wl]
      exact force_eqval N.K N.M (fun m => extLV_base N.M (extLV_base N.M e12 xs') _ m) _ d2.1
    have M2 : force N.K N.M d (extLV N.M (extLV N.M (fun j => W1 (j + X.n)) xs') (witL N.M W2 X.n)) (mx k).1 :=
      force_eqval N.K N.M (fun m => extLV_base N.M (extLV_base N.M e12 xs') _ m) _ d2.2
    have r := hc n k N d _ xs xs' _ _ hxl hxl' (len_witL N.M W1 X.n) (len_witL N.M W2 X.n) d1.1 F2 d1.2 M2
    refine (force_eqOff N.K N.M X.n X.n _).2 (fun i hi => ?_)
    have q := r i hi
    rw [witL_val N.M W1 X.n _ i hi, witL_val N.M W2 X.n _ i hi] at q
    show (N.M.P.obj d).eqv (N.M.P.map g (V i)) (N.M.P.map g (V (i + X.n)))
    have v1 : V i = extLV N.M ρ ys i := extLV_lt N.M (extLV N.M ρ ys') ρ ys i (by rw [hl]; exact hi)
    have v2 : V (i + X.n) = extLV N.M ρ ys' i := shift_val N.M _ ys X.n hl i
    rw [v1, v2]
    exact q
  intro i hi
  have q := (force_eqOff N.K N.M X.n X.n V).1 hV i hi
  have v1 : V i = extLV N.M ρ ys i := extLV_lt N.M (extLV N.M ρ ys') ρ ys i (by rw [hl]; exact hi)
  have v2 : V (i + X.n) = extLV N.M ρ ys' i := shift_val N.M _ ys X.n hl i
  rw [v1, v2] at q
  exact q

theorem glT_arr (hcov : ∀ (N : SModel T) c (ρ : Nat → (N.M.P.obj c).car), force N.K N.M c ρ A.φ →
      N.K.cov (hitF T N F ρ)) (hc : CmpG T F mx) : IsArr T A X (glT T F mx) :=
  ⟨fv_glF A X F.B _ _ (fun n => (F.u n).2.1) (fun n => (mx n).2.1),
   fun N _ ρ ys hl h => ⟨glT_A T F mx N ρ ys hl h, glT_X T F mx N ρ ys h⟩,
   fun N _ ρ hφ => glT_tot T F mx hcov N ρ hφ,
   fun N _ ρ ys ys' hl hl' h h' => glT_fn T F mx hc N ρ ys ys' hl hl' h h'⟩

end Gt

end Found

#print axioms Found.Gt.witL_congr
#print axioms Found.Gt.glT_fn
#print axioms Found.Gt.glT_arr

found/Gt32.lean

lean_all/found/Gt32.lean

/-
  T194 (classifying topos), part 32. found/Gt32 — the glued arrow is an
  amalgamation: F_n ; glT ≡ m_n for every member of the covering family.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt31

namespace Found

namespace Gt

open Sh

variable (T : Seq → Prop) {A X : SObj} (F : CFam T A) (mx : (n : Nat) → (SynCat T).Hom (F.B n) X)

theorem fv_glT : FVg (X.n + A.n) (glT T F mx) :=
  fv_glF A X F.B _ _ (fun n => (F.u n).2.1) (fun n => (mx n).2.1)

/-- m_n implies the composite F_n ; glT -/
theorem glT_amalg_r (n : Nat) (N : SModel T) {c : N.D.Ob} (ρ : Nat → (N.M.P.obj c).car)
    (zs : List (N.M.P.obj c).car) (hl : lenG zs = X.n) (hm : force N.K N.M c (extLV N.M ρ zs) (mx n).1) :
    force N.K N.M c (extLV N.M ρ zs) (compF A.n X.n (F.u n).1 (glT T F mx)) := by
  have hB := ((mx n).2.2.1 N c ρ zs hl hm).1
  refine force_local N.K N.M N.closed _ _ _ (exN_wit N.K N.M _ A.n ρ ((F.u n).2.2.2.1 N c ρ hB))
    (fun d g ⟨ys, hyl, fF⟩ => ?_)
  let ρd := resV N.M g ρ
  let zsd := lmapG (N.M.P.map g) zs
  have hzl : lenG zsd = X.n := (len_lmapG _ zs).trans hl
  refine force_resp N.K N.M _ (fun i => (N.M.P.obj d).symm (res_block N.M g ρ zs i)) ?_
  have hA := ((F.u n).2.2.1 N d ρd ys hyl fF).2
  let xs := witL N.M ρd (F.B n).n
  have hxl : lenG xs = (F.B n).n := len_witL N.M ρd _
  have Fp : force N.K N.M d (extLV N.M (extLV N.M (extLV N.M ρd ys) xs) (witL N.M (extLV N.M ρd ys) A.n)) (F.u n).1 :=
    force_agree N.K N.M _ (A.n + (F.B n).n) (F.u n).2.1
      (agree_blocks N.M _ _ _ _ A.n (F.B n).n hyl (len_witL N.M _ A.n)
        (fun i hi => by rw [witL_val N.M _ A.n _ i hi]; exact (N.M.P.obj d).refl _)
        (fun j hj => by rw [witL_val N.M ρd _ _ j hj]; exact (N.M.P.obj d).refl _)) fF
  have hm' := force_resp N.K N.M _ (res_block N.M g ρ zs) (force_stable N.K N.M _ g _ hm)
  have Mp : force N.K N.M d (extLV N.M (extLV N.M (extLV N.M ρd ys) xs) zsd) (mx n).1 :=
    force_agree N.K N.M _ (X.n + (F.B n).n) (mx n).2.1
      (agree_blocks N.M _ _ _ _ X.n (F.B n).n hzl hzl
        (fun i hi => by
          rw [extLV_lt N.M ρd (extLV N.M (extLV N.M ρd ys) xs) zsd i (by rw [hzl]; exact hi)]
          exact (N.M.P.obj d).refl _)
        (fun j hj => by rw [witL_val N.M ρd _ _ j hj]; exact (N.M.P.obj d).refl _)) hm'
  have D : force N.K N.M d (extLV N.M (extLV N.M ρd ys) zsd)
      (exN (F.B n).n (bodyG A X F.B (fun n => (F.u n).1) (fun n => (mx n).1) n)) :=
    exN_of N.K N.M _ _ _ xs hxl ((disj_iff N.K N.M A.n (F.B n).n X.n _ _ (F.u n).2.1 (mx n).2.1 _ zsd xs hzl hxl).2 ⟨Fp, Mp⟩)
  have G : force N.K N.M d (extLV N.M (extLV N.M ρd ys) zsd) (glT T F mx) :=
    ⟨(force_ren N.K N.M A.φ _ _).2 (force_eqval N.K N.M (fun i => (shift_val N.M _ zsd X.n hzl i).symm) A.φ hA),
     ⟨maxS d, N.K.top d, fun _ f _ => ⟨n, force_stable N.K N.M _ f _ D⟩⟩⟩
  exact exN_of N.K N.M _ A.n _ ys hyl
    ((force_body N.K N.M (F.u n).1 (glT T F mx) A.n X.n (fv_glT T F mx) ρd zsd ys hyl hzl).2 ⟨fF, G⟩)

/-- the composite F_n ; glT implies m_n -/
theorem glT_amalg_l (hc : CmpG T F mx) (n : Nat) (N : SModel T) {c : N.D.Ob} (ρ : Nat → (N.M.P.obj c).car)
    (zs : List (N.M.P.obj c).car) (hl : lenG zs = X.n)
    (hcomp : force N.K N.M c (extLV N.M ρ zs) (compF A.n X.n (F.u n).1 (glT T F mx))) :
    force N.K N.M c (extLV N.M ρ zs) (mx n).1 := by
  refine force_local N.K N.M N.closed _ _ _ (exN_wit N.K N.M _ A.n _ hcomp) (fun d g ⟨ys, hyl, fb⟩ => ?_)
  have b := body_at T (Y := A) (Z := X) N (fv_glT T F mx) g ρ zs hl ys hyl fb
  let ρd := resV N.M g ρ
  let zsd := lmapG (N.M.P.map g) zs
  have hzl : lenG zsd = X.n := (len_lmapG _ zs).trans hl
  refine force_resp N.K N.M _ (fun i => (N.M.P.obj d).symm (res_block N.M g ρ zs i)) ?_
  let W := extLV N.M (extLV N.M ρd ys) zsd
  have hB := ((F.u n).2.2.1 N d ρd ys hyl b.1).1
  refine force_local N.K N.M N.closed _ _ _ (cov_meet N.K (cov_decS N.K N.M A X F.B _ _ W b.2)
    (exN_wit N.K N.M _ X.n ρd ((mx n).2.2.2.1 N d ρd hB)))
    (fun e h ⟨⟨k, xs', hxl', hb'⟩, ⟨zs2, hzl2, hm2⟩⟩ => ?_)
  let W' := resV N.M h W
  have dk := (disj_at N.K N.M A.n (F.B k).n X.n _ _ (F.u k).2.1 (mx k).2.1 W' xs' hxl').1 hb'
  let ρ' : Nat → (N.M.P.obj e).car := fun j => W' (j + X.n)
  have eρ : ∀ j, ρ' j = N.M.P.map h (extLV N.M ρd ys j) := fun j => by
    have w : W (j + X.n) = extLV N.M ρd ys j := shift_val N.M _ zsd X.n hzl j
    show N.M.P.map h (W (j + X.n)) = _
    rw [w]
  let xs := witL N.M (resV N.M h ρd) (F.B n).n
  have hxl : lenG xs = (F.B n).n := len_witL N.M _ _
  have fF' := force_resp N.K N.M _ (res_block N.M h ρd ys) (force_stable N.K N.M _ h _ b.1)
  have Fn : force N.K N.M e (extLV N.M (extLV N.M ρ' xs) (witL N.M ρ' A.n)) (F.u n).1 :=
    force_agree N.K N.M _ (A.n + (F.B n).n) (F.u n).2.1
      (agree_blocks N.M _ _ _ _ A.n (F.B n).n ((len_lmapG _ ys).trans hyl) (len_witL N.M _ A.n)
        (fun i hi => by
          rw [witL_val N.M ρ' A.n _ i hi, eρ i]
          exact (N.M.P.obj e).symm (res_block N.M h ρd ys i))
        (fun j hj => by rw [witL_val N.M _ _ ρ' j hj]; exact (N.M.P.obj e).refl _)) fF'
  have Mn : force N.K N.M e (extLV N.M (extLV N.M ρ' xs) zs2) (mx n).1 :=
    force_agree N.K N.M _ (X.n + (F.B n).n) (mx n).2.1
      (agree_blocks N.M _ _ _ _ X.n (F.B n).n hzl2 hzl2
        (fun i hi => by
          rw [extLV_lt N.M (resV N.M h ρd) (extLV N.M ρ' xs) zs2 i (by rw [hzl2]; exact hi)]
          exact (N.M.P.obj e).refl _)
        (fun j hj => by rw [witL_val N.M _ _ ρ' j hj]; exact (N.M.P.obj e).refl _)) hm2
  have r := hc n k N e ρ' xs xs' zs2 (witL N.M W' X.n) hxl hxl' hzl2 (len_witL N.M W' X.n) Fn dk.1 Mn dk.2
  refine force_resp N.K N.M _ (fun i => (N.M.P.obj e).symm (res_block N.M h ρd zsd i)) ?_
  refine force_agree N.K N.M _ (X.n + (F.B n).n) (mx n).2.1
    (agree_blocks N.M _ _ _ _ X.n (F.B n).n hzl2 ((len_lmapG _ zsd).trans hzl) (fun i hi => ?_)
      (fun j _ => (N.M.P.obj e).refl _)) hm2
  have q := r i hi
  rw [extLV_lt N.M ρ' (resV N.M h ρd) zs2 i (by rw [hzl2]; exact hi), witL_val N.M W' X.n ρ' i hi] at q
  have wi : W i = extLV N.M ρd zsd i := extLV_lt N.M (extLV N.M ρd ys) ρd zsd i (by rw [hzl]; exact hi)
  have q' : (N.M.P.obj e).eqv (extLV N.M (resV N.M h ρd) zs2 i) (N.M.P.map h (extLV N.M ρd zsd i)) := by
    rw [← wi]; exact q
  exact (N.M.P.obj e).trans q' (res_block N.M h ρd zsd i)

theorem glT_amalg (hc : CmpG T F mx) (n : Nat) :
    ArrEq T X (compF A.n X.n (F.u n).1 (glT T F mx)) (mx n).1 :=
  fun N _ ρ zs hl => ⟨glT_amalg_l T F mx hc n N ρ zs hl, glT_amalg_r T F mx n N ρ zs hl⟩

end Gt

end Found

#print axioms Found.Gt.glT_amalg_r
#print axioms Found.Gt.glT_amalg_l
#print axioms Found.Gt.glT_amalg

found/Gt33.lean

lean_all/found/Gt33.lean

/-
  T194 (classifying topos), part 33. found/Gt33 — semantic compatibility
  of the arrows m_n from their agreement on the pullbacks B_n ×_A B_k
  (π' ; m_k ≡ π ; m_n).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt32

namespace Found

namespace Gt

open Sh

variable (T : Seq → Prop) {A X : SObj} (F : CFam T A) (mx : (n : Nat) → (SynCat T).Hom (F.B n) X)

/-- agreement of the arrows on the pullbacks -/
def PbAgree : Prop :=
  ∀ n k, ArrEq T X (compF (F.B k).n X.n (projF (pbObj T (F.u n).2 (F.u k).2) (F.B k).n 0) (mx k).1)
    (compF (F.B n).n X.n (projF (pbObj T (F.u n).2 (F.u k).2) (F.B n).n (F.B k).n) (mx n).1)

theorem cmp_of (h : PbAgree T F mx) : CmpG T F mx := by
  intro n k N c ρ xs xs' zs zs' hxl hxl' hzl hzl' fn fk mn mk i hi
  let P := pbObj T (F.u n).2 (F.u k).2
  let ρP := extLV N.M (extLV N.M ρ xs) xs'
  have shP : ∀ j, ρP (j + (F.B k).n) = extLV N.M ρ xs j := shift_val N.M _ xs' (F.B k).n hxl'
  have hP : force N.K N.M c ρP P.φ := by
    refine exN_of N.K N.M _ A.n ρP (witL N.M ρ A.n) (len_witL N.M ρ A.n)
      ((pb_body T N (θ := (F.u n).1) (f := (F.u k).1) ρP _ (len_witL N.M ρ A.n)).2 ⟨?_, ?_⟩)
    · exact force_eqval N.K N.M (fun m => extLV_base N.M (fun j => (shP j).symm) _ m) _ fn
    · exact force_agree N.K N.M _ (A.n + (F.B k).n) (F.u k).2.1
        (agree_blocks N.M _ _ _ _ A.n (F.B k).n (len_witL N.M ρ A.n) (len_witL N.M ρ A.n)
          (fun j hj => by
            rw [extLV_lt N.M (extLV N.M ρ xs') ρP _ j (by rw [len_witL]; exact hj)]
            exact (N.M.P.obj c).refl _)
          (fun j hj => by
            rw [extLV_lt N.M ρ (extLV N.M ρ xs) xs' j (by rw [hxl']; exact hj)]
            exact (N.M.P.obj c).refl _)) fk
  have mk' : force N.K N.M c (extLV N.M ρP zs') (mx k).1 :=
    force_agree N.K N.M _ (X.n + (F.B k).n) (mx k).2.1
      (agree_blocks N.M _ _ _ _ X.n (F.B k).n hzl' hzl'
        (fun j hj => by
          rw [extLV_lt N.M (extLV N.M ρ xs') ρP zs' j (by rw [hzl']; exact hj)]
          exact (N.M.P.obj c).refl _)
        (fun j hj => by
          rw [extLV_lt N.M ρ (extLV N.M ρ xs) xs' j (by rw [hxl']; exact hj)]
          exact (N.M.P.obj c).refl _)) mk
  have ck := (comp_proj T P (F.B k) X 0 (by show 0 + (F.B k).n ≤ (F.B k).n + (F.B n).n; rw [Nat.zero_add]; exact Nat.le_add_right _ _)
    (pb_A' T (F.u k).2) (mx k).2 N c ρP zs' hzl').2 ⟨hP, mk'⟩
  have cn := (h n k N c ρP zs' hzl').1 ck
  have mn' := ((comp_proj T P (F.B n) X (F.B k).n (Nat.le_refl _) (pb_B T (F.u n).2) (mx n).2 N c ρP zs' hzl').1 cn).2
  have mn'' : force N.K N.M c (extLV N.M (extLV N.M ρ xs) zs') (mx n).1 :=
    force_eqval N.K N.M (fun m => extLV_base N.M shP zs' m) _ mn'
  have q := (mx n).2.2.2.2 N c (extLV N.M ρ xs) zs zs' hzl hzl' mn mn'' i hi
  rw [extLV_lt N.M (extLV N.M ρ xs) ρ zs i (by rw [hzl]; exact hi),
    extLV_lt N.M (extLV N.M ρ xs) ρ zs' i (by rw [hzl']; exact hi)] at q
  exact q

end Gt

end Found

#print axioms Found.Gt.cmp_of

found/Gt34.lean

lean_all/found/Gt34.lean

/-
  T194 (classifying topos), part 34. found/Gt34 — separation for the
  syntactic site: two arrows B → X that agree after composing with every
  member of a covering family are equal.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt33

namespace Found

namespace Gt

open Sh

variable (T : Seq → Prop) {B X : SObj} (G : CFam T B)

theorem sep_dir (hcov : ∀ (N : SModel T) c (ρ : Nat → (N.M.P.obj c).car), force N.K N.M c ρ B.φ →
      N.K.cov (hitF T N G ρ)) {g h : GFm} (hg : IsArr T B X g) (hh : IsArr T B X h)
    (hgh : ∀ n (N : SModel T) c (ρ : Nat → (N.M.P.obj c).car) zs, lenG zs = X.n →
      force N.K N.M c (extLV N.M ρ zs) (compF B.n X.n (G.u n).1 g) →
      force N.K N.M c (extLV N.M ρ zs) (compF B.n X.n (G.u n).1 h))
    (N : SModel T) {c : N.D.Ob} (ρ : Nat → (N.M.P.obj c).car) (zs : List (N.M.P.obj c).car) (hl : lenG zs = X.n)
    (fg : force N.K N.M c (extLV N.M ρ zs) g) : force N.K N.M c (extLV N.M ρ zs) h := by
  have hB := (hg.2.1 N c ρ zs hl fg).1
  refine force_local N.K N.M N.closed _ _ _ (hcov N c ρ hB) (fun d f ⟨n, σ, ys, hyl, he, hf⟩ => ?_)
  let ρd := resV N.M f ρ
  let zsd := lmapG (N.M.P.map f) zs
  have hzl : lenG zsd = X.n := (len_lmapG _ zs).trans hl
  refine force_resp N.K N.M _ (fun i => (N.M.P.obj d).symm (res_block N.M f ρ zs i)) ?_
  have fg' := force_resp N.K N.M _ (res_block N.M f ρ zs) (force_stable N.K N.M _ f _ fg)
  have g2 : force N.K N.M d (extLV N.M (extLV N.M σ ys) zsd) g :=
    force_agree N.K N.M _ (X.n + B.n) hg.1
      (agree_blocks N.M _ _ _ _ X.n B.n hzl hzl
        (fun i hi => by
          rw [extLV_lt N.M ρd (extLV N.M σ ys) zsd i (by rw [hzl]; exact hi)]
          exact (N.M.P.obj d).refl _)
        (fun j hj => (N.M.P.obj d).symm (he j hj))) fg'
  have cg : force N.K N.M d (extLV N.M σ zsd) (compF B.n X.n (G.u n).1 g) :=
    exN_of N.K N.M _ B.n _ ys hyl ((force_body N.K N.M (G.u n).1 g B.n X.n hg.1 σ zsd ys hyl hzl).2 ⟨hf, g2⟩)
  have ch := hgh n N d σ zsd hzl cg
  refine force_local N.K N.M N.closed _ _ _ (exN_wit N.K N.M _ B.n _ ch) (fun e k ⟨ys', hyl', fb⟩ => ?_)
  have b := body_at T (Y := B) (Z := X) N hh.1 k σ zsd hzl ys' hyl' fb
  have hyk : lenG (lmapG (N.M.P.map k) ys) = B.n := (len_lmapG _ ys).trans hyl
  have hf' := force_resp N.K N.M _ (res_block N.M k σ ys) (force_stable N.K N.M _ k _ hf)
  have fn := (G.u n).2.2.2.2 N e (resV N.M k σ) ys' _ hyl' hyk b.1 hf'
  refine force_resp N.K N.M _ (fun i => (N.M.P.obj e).symm (res_block N.M k ρd zsd i)) ?_
  refine force_agree N.K N.M _ (X.n + B.n) hh.1
    (agree_blocks N.M _ _ _ _ X.n B.n ((len_lmapG _ zsd).trans hzl) ((len_lmapG _ zsd).trans hzl)
      (fun i hi => by
        rw [extLV_lt N.M (extLV N.M (resV N.M k σ) ys') (resV N.M k ρd) _ i
          (by rw [len_lmapG, hzl]; exact hi)]
        exact (N.M.P.obj e).refl _)
      (fun j hj => (N.M.P.obj e).trans (fn j hj) ((N.M.P.obj e).trans
        ((N.M.P.obj e).symm (res_block N.M k σ ys j)) (N.M.P.map_eqv k (he j hj))))) b.2

theorem sep_arr (hcov : ∀ (N : SModel T) c (ρ : Nat → (N.M.P.obj c).car), force N.K N.M c ρ B.φ →
      N.K.cov (hitF T N G ρ)) {g h : GFm} (hg : IsArr T B X g) (hh : IsArr T B X h)
    (hgh : ∀ n, ArrEq T X (compF B.n X.n (G.u n).1 g) (compF B.n X.n (G.u n).1 h)) : ArrEq T X g h :=
  fun N _ ρ zs hl => ⟨sep_dir T G hcov hg hh (fun n N _ ρ zs hl => (hgh n N _ ρ zs hl).1) N ρ zs hl,
    sep_dir T G hcov hh hg (fun n N _ ρ zs hl => (hgh n N _ ρ zs hl).2) N ρ zs hl⟩

end Gt

end Found

#print axioms Found.Gt.sep_dir
#print axioms Found.Gt.sep_arr

found/Gt35.lean

lean_all/found/Gt35.lean

/-
  T194 (classifying topos), part 35. found/Gt35 — subcanonicity: every
  representable presheaf Hom(-, X) on the syntactic site (C_T, J_T^c) is a
  sheaf. A matching family on a covering sieve is glued by the formula glT;
  the amalgamation holds on every arrow of the sieve (checked on the
  pulled-back covering family), and is unique by separation.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt34

namespace Found

namespace Gt

open Sh

variable (T : Seq → Prop)

/-- the pullback of a countable family along u -/
def pbFam {A B : SObj} (F : CFam T A) (u : (SynCat T).Hom B A) : CFam T B :=
  ⟨fun n => pbObj T (F.u n).2 u.2, fun n => ⟨projF (pbObj T (F.u n).2 u.2) B.n 0, pi1_arr T (F.u n).2 u.2⟩⟩

theorem pb_cov {A B : SObj} (F : CFam T A) (u : (SynCat T).Hom B A)
    (hc : ∀ (N : SModel T) c (ρ : Nat → (N.M.P.obj c).car), force N.K N.M c ρ A.φ → N.K.cov (hitF T N F ρ)) :
    ∀ (N : SModel T) c (ρ : Nat → (N.M.P.obj c).car), force N.K N.M c ρ B.φ → N.K.cov (hitF T N (pbFam T F u) ρ) := by
  intro N c ρ hφ
  have W1 := exN_wit N.K N.M u.1 A.n ρ (u.2.2.2.1 N c ρ hφ)
  refine N.K.trans W1 (fun d g ⟨ys, hl, ff⟩ => ?_)
  have hA := (u.2.2.1 N d (resV N.M g ρ) ys hl ff).2
  refine cov_mono N.K (hc N d _ hA) (fun e h ⟨n, σ, ys2, hl2, he2, fu⟩ => ?_)
  match pull_hit T u (F.u n) N g h ρ ys hl ff σ ys2 hl2 he2 fu with
  | ⟨σ', ys', hl', he', hf'⟩ => exact ⟨n, σ', ys', hl', he', hf'⟩

theorem yo_sheaf (K : CP.Classical') (X : SObj) : IsSheaf (JTc T K) (yo (SynCat T) X) := by
  intro A S hS m
  match hS with
  | ⟨F, hF, hcov⟩ =>
  let C := SynCat T
  let mx : (n : Nat) → C.Hom (F.B n) X := fun n => m.x (F.u n) (hF n)
  have hag : PbAgree T F mx := fun n k => by
    let π' : C.Hom (pbObj T (F.u n).2 (F.u k).2) (F.B k) := ⟨_, pi1_arr T (F.u n).2 (F.u k).2⟩
    let π : C.Hom (pbObj T (F.u n).2 (F.u k).2) (F.B n) := ⟨_, pi2_arr T (F.u n).2 (F.u k).2⟩
    have sq : C.heq (C.comp π' (F.u k)) (C.comp π (F.u n)) := pb_square T (F.u n).2 (F.u k).2
    have r : C.heq (C.comp π' (mx k)) (C.comp π (mx n)) :=
      C.htrans (C.hsymm (m.compat (F.u k) (hF k) π'))
        (C.htrans (m.resp (S.stable (hF k) π') (S.stable (hF n) π) sq) (m.compat (F.u n) (hF n) π))
    exact r
  let a : C.Hom A X := ⟨glT T F mx, glT_arr T F mx hcov (cmp_of T F mx hag)⟩
  refine ⟨⟨a, fun B u hu => ?_⟩, fun a1 a2 h1 h2 => ?_⟩
  · refine sep_arr T (pbFam T F u) (pb_cov T F u hcov) (C.comp u a).2 (m.x u hu).2 (fun n => ?_)
    let π' : C.Hom (pbObj T (F.u n).2 u.2) B := (pbFam T F u).u n
    let π : C.Hom (pbObj T (F.u n).2 u.2) (F.B n) := ⟨_, pi2_arr T (F.u n).2 u.2⟩
    have sq : C.heq (C.comp π' u) (C.comp π (F.u n)) := pb_square T (F.u n).2 u.2
    have r : C.heq (C.comp π' (C.comp u a)) (C.comp π' (m.x u hu)) :=
      C.htrans (C.hsymm (C.assoc π' u a))
      (C.htrans (C.comp_resp sq (C.hrefl a))
      (C.htrans (C.assoc π (F.u n) a)
      (C.htrans (C.comp_resp (C.hrefl π) (glT_amalg T F mx (cmp_of T F mx hag) n))
      (C.htrans (C.hsymm (m.compat (F.u n) (hF n) π))
      (C.htrans (m.resp (S.stable (hF n) π) (S.stable hu π') (C.hsymm sq))
        (m.compat u hu π'))))))
    exact r
  · exact sep_arr T F hcov a1.2 a2.2 (fun n => C.htrans (h1 _ (F.u n) (hF n)) (C.hsymm (h2 _ (F.u n) (hF n))))

end Gt

end Found

#print axioms Found.Gt.pb_cov
#print axioms Found.Gt.yo_sheaf

found/Gt36.lean

lean_all/found/Gt36.lean

/-
  T194 (classifying topos), part 36. found/Gt36 — toward the universal
  model: the generic sort [y : ⊤], elements at a stage A are arrows
  A → [y : ⊤]; the graph formula of k elements (context v̄, x̄) and its
  reading; locally, every tuple of elements has values (graph totality).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt35

namespace Found

namespace Gt

open Sh

variable (T : Seq → Prop)

/-- the generic sort -/
def U1 : SObj := ⟨1, .top, trivial⟩

/-- renaming for the i-th element: y ↦ v_i, x_j ↦ position j + k -/
def gσ (k i : Nat) : Nat → Nat
  | 0 => i
  | j + 1 => j + k

/-- the graph of k elements of A -/
def grK {A : SObj} (ρ : Nat → (SynCat T).Hom A U1) (k : Nat) : GFm :=
  conjG (lmapG (fun i => renG (gσ k i) (ρ i).1) (rangeG k))

theorem fv_gσ (A : SObj) (k i : Nat) (hi : i < k) : ∀ j, j < 1 + A.n → gσ k i j < k + A.n
  | 0, _ => Nat.lt_of_lt_of_le hi (Nat.le_add_right k A.n)
  | j + 1, h => by
    show j + k < k + A.n
    have h' : j + 1 < A.n + 1 := by rw [Nat.add_comm A.n 1]; exact h
    rw [Nat.add_comm j k]
    exact Nat.add_lt_add_left (Nat.lt_of_succ_lt_succ h') k

theorem fv_grK {A : SObj} (ρ : Nat → (SynCat T).Hom A U1) (k : Nat) : FVg (k + A.n) (grK T ρ k) :=
  fv_conjG _ _ (fun x hx => match mem_lmapG _ hx with
    | ⟨i, hi, e⟩ => by
      rw [e]
      exact fv_ren _ (fv_gσ A k i (mem_rangeG k i hi)) (ρ i).2.1)

section R

variable {C : ECat.{0, 0}} (J : GTop C) (M : @GStr C)

theorem force_grK {A : SObj} (ρ : Nat → (SynCat T).Hom A U1) (k : Nat) {c : C.Ob} (W : Nat → (M.P.obj c).car) :
    force J M c W (grK T ρ k) ↔ ∀ i, i < k → force J M c (extV M (fun j => W (j + k)) (W i)) (ρ i).1 :=
  (force_conjG J M W _).trans
    ⟨fun h i hi => force_eqval J M (ρ := fun n => W (gσ k i n)) (fun n => match n with
        | 0 => rfl
        | _ + 1 => rfl) _ ((force_ren J M (ρ i).1 (gσ k i) W).1 (h _ (lmapG_mem _ _ (rangeG_mem k i hi)))),
     fun h x hx => match mem_lmapG _ hx with
      | ⟨i, hi, e⟩ => by
        rw [e]
        exact (force_ren J M (ρ i).1 (gσ k i) W).2 (force_eqval J M (ρ' := fun n => W (gσ k i n)) (fun n => match n with
          | 0 => rfl
          | _ + 1 => rfl) _ (h i (mem_rangeG k i hi)))⟩

end R

/-- extend k values by one more -/
def pickV {X : Type} (k : Nat) (w : Nat → X) (y : X) (i : Nat) : X :=
  match Nat.decLt i k with
  | isTrue _ => w i
  | isFalse _ => y

theorem pick_lt {X : Type} {k i : Nat} (w : Nat → X) (y : X) (h : i < k) : pickV k w y i = w i := by
  unfold pickV
  match Nat.decLt i k with
  | isTrue _ => rfl
  | isFalse h' => exact absurd h h'

theorem pick_eq {X : Type} (k : Nat) (w : Nat → X) (y : X) : pickV k w y k = y := by
  unfold pickV
  match Nat.decLt k k with
  | isTrue h' => exact absurd h' (Nat.lt_irrefl k)
  | isFalse _ => rfl

section Tot

variable (N : SModel T) {A : SObj} (ρ : Nat → (SynCat T).Hom A U1)

theorem ext_resR {c d e : N.D.Ob} (g : N.D.Hom d c) (h : N.D.Hom e d) (σ : Nat → (N.M.P.obj c).car)
    (y : (N.M.P.obj d).car) :
    ∀ n, (N.M.P.obj e).eqv (resV N.M h (extV N.M (resV N.M g σ) y) n) (extV N.M (resV N.M (N.D.comp h g) σ) (N.M.P.map h y) n)
  | 0 => (N.M.P.obj e).refl _
  | n + 1 => res_comp N.M g h σ n

theorem ext_comp {c d e : N.D.Ob} (g : N.D.Hom d c) (h : N.D.Hom e d) (σ : Nat → (N.M.P.obj c).car)
    (y : (N.M.P.obj e).car) :
    ∀ n, (N.M.P.obj e).eqv (extV N.M (resV N.M h (resV N.M g σ)) y n) (extV N.M (resV N.M (N.D.comp h g) σ) y n)
  | 0 => (N.M.P.obj e).refl _
  | n + 1 => res_comp N.M g h σ n

/-- stages where all k elements have values -/
def TotS (k : Nat) {c : N.D.Ob} (σ : Nat → (N.M.P.obj c).car) : Sieve N.D c where
  S := fun {d} g => ∃ w : Nat → (N.M.P.obj d).car, ∀ i, i < k → force N.K N.M d (extV N.M (resV N.M g σ) (w i)) (ρ i).1
  resp := fun {_} {g g'} e ⟨w, hw⟩ => ⟨w, fun i hi => force_resp N.K N.M _
    (ρ := extV N.M (resV N.M g σ) (w i)) (ρ' := extV N.M (resV N.M g' σ) (w i)) (fun n => match n with
    | 0 => (N.M.P.obj _).refl _
    | n + 1 => N.M.P.map_resp (σ n) e) (hw i hi)⟩
  stable := fun {_ _} {g} ⟨w, hw⟩ h => ⟨fun i => N.M.P.map h (w i), fun i hi =>
    force_resp N.K N.M _ (ext_resR T N g h σ (w i)) (force_stable N.K N.M _ h _ (hw i hi))⟩

theorem cov_tot {c : N.D.Ob} (σ : Nat → (N.M.P.obj c).car) (hφ : force N.K N.M c σ A.φ) :
    (k : Nat) → N.K.cov (TotS T N ρ k σ)
  | 0 => cov_mono N.K (N.K.top c) (fun _ g _ => ⟨resV N.M g σ, fun _ hi => (Nat.not_lt_zero _ hi).elim⟩)
  | k + 1 => N.K.trans (cov_tot σ hφ k) (fun d g ⟨w, hw⟩ => by
    have hT := (ρ k).2.2.2.1 N d (resV N.M g σ) (force_stable N.K N.M A.φ g σ hφ)
    refine cov_mono N.K (exN_wit N.K N.M _ 1 _ hT) (fun e h ⟨ds, hl, f⟩ => ?_)
    match ds, hl with
    | [], hl => exact Nat.noConfusion hl
    | _ :: _ :: l, hl => exact Nat.noConfusion (Nat.succ.inj hl)
    | [y], _ =>
      refine ⟨pickV k (fun i => N.M.P.map h (w i)) y, fun i hi => ?_⟩
      match Nat.decLt i k with
      | isTrue h' =>
        rw [pick_lt _ y h']
        exact force_resp N.K N.M _ (ext_resR T N g h σ (w i)) (force_stable N.K N.M _ h _ (hw i h'))
      | isFalse h' =>
        have e : i = k := Nat.le_antisymm (Nat.le_of_lt_succ hi) (Nat.le_of_not_lt h')
        subst e
        rw [pick_eq]
        exact force_resp N.K N.M _ (ext_comp T N g h σ y) f)

end Tot

end Gt

end Found

#print axioms Found.Gt.fv_grK
#print axioms Found.Gt.force_grK
#print axioms Found.Gt.TotS
#print axioms Found.Gt.cov_tot

found/Gt37.lean

lean_all/found/Gt37.lean

/-
  T194 (classifying topos), part 37. found/Gt37 — reading composites: a
  composite f ; g holds at (x̄, z̄) iff locally some ȳ has f(x̄, ȳ) and
  g(ȳ, z̄); in particular, given f(x̄, ȳ), the composite gives g(ȳ, z̄).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt36

namespace Found

namespace Gt

open Sh

variable (T : Seq → Prop) {X Y Z : SObj} {f g : GFm}

theorem comp_intro (hg : FVg (Z.n + Y.n) g) (N : SModel T) {c : N.D.Ob} (σ : Nat → (N.M.P.obj c).car)
    (ys zs : List (N.M.P.obj c).car) (hyl : lenG ys = Y.n) (hzl : lenG zs = Z.n)
    (ff : force N.K N.M c (extLV N.M σ ys) f) (fg : force N.K N.M c (extLV N.M (extLV N.M σ ys) zs) g) :
    force N.K N.M c (extLV N.M σ zs) (compF Y.n Z.n f g) :=
  exN_of N.K N.M _ Y.n _ ys hyl ((force_body N.K N.M f g Y.n Z.n hg σ zs ys hyl hzl).2 ⟨ff, fg⟩)

theorem comp_elim (hf : IsArr T X Y f) (hg : IsArr T Y Z g) (N : SModel T) {c : N.D.Ob} (σ : Nat → (N.M.P.obj c).car)
    (ys zs : List (N.M.P.obj c).car) (hyl : lenG ys = Y.n) (hzl : lenG zs = Z.n)
    (fc : force N.K N.M c (extLV N.M σ zs) (compF Y.n Z.n f g)) (ff : force N.K N.M c (extLV N.M σ ys) f) :
    force N.K N.M c (extLV N.M (extLV N.M σ ys) zs) g := by
  refine force_local N.K N.M N.closed _ _ _ (exN_wit N.K N.M _ Y.n _ fc) (fun d k ⟨ys', hyl', fb⟩ => ?_)
  have b := body_at T (Y := Y) (Z := Z) N hg.1 k σ zs hzl ys' hyl' fb
  have hyk : lenG (lmapG (N.M.P.map k) ys) = Y.n := (len_lmapG _ ys).trans hyl
  have hzk : lenG (lmapG (N.M.P.map k) zs) = Z.n := (len_lmapG _ zs).trans hzl
  have ff' := force_resp N.K N.M _ (res_block N.M k σ ys) (force_stable N.K N.M _ k _ ff)
  have fn := hf.2.2.2 N d (resV N.M k σ) ys' _ hyl' hyk b.1 ff'
  have g1 : force N.K N.M d (extLV N.M (extLV N.M (resV N.M k σ) (lmapG (N.M.P.map k) ys)) (lmapG (N.M.P.map k) zs)) g :=
    force_agree N.K N.M _ (Z.n + Y.n) hg.1
      (agree_blocks N.M _ _ _ _ Z.n Y.n hzk hzk
        (fun i hi => by
          rw [extLV_lt N.M (extLV N.M (resV N.M k σ) ys') (extLV N.M (resV N.M k σ) (lmapG (N.M.P.map k) ys)) _ i
            (by rw [hzk]; exact hi)]
          exact (N.M.P.obj d).refl _)
        fn) b.2
  exact force_resp N.K N.M _ (fun n => (N.M.P.obj d).symm ((N.M.P.obj d).trans
    (res_block N.M k (extLV N.M σ ys) zs n) (extLV_eqv N.M (res_block N.M k σ ys) _ n))) g1

end Gt

end Found

#print axioms Found.Gt.comp_intro
#print axioms Found.Gt.comp_elim

found/Gt38.lean

lean_all/found/Gt38.lean

/-
  T194 (classifying topos), part 38. found/Gt38 — the structure of the
  universal model on the syntactic site: the carrier is Hom(-, [y : ⊤]); a
  relation R holds of a list of elements at A when, in every sheaf model,
  values of the elements over A satisfy R.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt37

namespace Found

namespace Gt

open Sh

variable (T : Seq → Prop)

/-- the elements have the values vs over σ -/
def LR (N : SModel T) {A : SObj} {c : N.D.Ob} (σ : Nat → (N.M.P.obj c).car) :
    List ((SynCat T).Hom A U1) → List (N.M.P.obj c).car → Prop
  | [], [] => True
  | a :: l, v :: vs => force N.K N.M c (extV N.M σ v) a.1 ∧ LR N σ l vs
  | [], _ :: _ => False
  | _ :: _, [] => False

theorem lr_res (N : SModel T) {A : SObj} {c d : N.D.Ob} (g : N.D.Hom d c) (σ : Nat → (N.M.P.obj c).car) :
    (l : List ((SynCat T).Hom A U1)) → (vs : List (N.M.P.obj c).car) → LR T N σ l vs →
      LR T N (resV N.M g σ) l (lmapG (N.M.P.map g) vs)
  | [], [], _ => trivial
  | _ :: l, _ :: vs, ⟨h1, h2⟩ => ⟨force_resp N.K N.M _ (ext_res N.M g σ _) (force_stable N.K N.M _ g _ h1),
      lr_res N g σ l vs h2⟩
  | [], _ :: _, h => h.elim
  | _ :: _, [], h => h.elim

theorem lr_leq (N : SModel T) {A : SObj} {c : N.D.Ob} (σ : Nat → (N.M.P.obj c).car) :
    (l l' : List ((SynCat T).Hom A U1)) → (vs : List (N.M.P.obj c).car) →
      LEq (yo (SynCat T) U1) A l l' → LR T N σ l' vs → LR T N σ l vs
  | [], [], [], _, _ => trivial
  | _ :: l, _ :: l', _ :: vs, ⟨e, el⟩, ⟨h1, h2⟩ => ⟨(e N _ σ [_] rfl).2 h1, lr_leq N σ l l' vs el h2⟩
  | [], [], _ :: _, _, h => h.elim
  | _ :: _, _ :: _, [], _, h => h.elim
  | [], _ :: _, _, e, _ => e.elim
  | _ :: _, [], _, e, _ => e.elim

theorem lr_comp (N : SModel T) {A B : SObj} (f : (SynCat T).Hom B A) {c : N.D.Ob} (σ : Nat → (N.M.P.obj c).car)
    (ys : List (N.M.P.obj c).car) (hyl : lenG ys = A.n) (ff : force N.K N.M c (extLV N.M σ ys) f.1) :
    (l : List ((SynCat T).Hom A U1)) → (vs : List (N.M.P.obj c).car) →
      LR T N σ (lmapG (fun a => (SynCat T).comp f a) l) vs → LR T N (extLV N.M σ ys) l vs
  | [], [], _ => trivial
  | a :: l, _ :: vs, ⟨h1, h2⟩ => ⟨comp_elim T f.2 a.2 N σ ys [_] hyl rfl h1 ff, lr_comp N f σ ys hyl ff l vs h2⟩
  | [], _ :: _, h => h.elim
  | _ :: _, [], h => h.elim

/-- the relations of the universal model -/
def relU (R : Nat) (A : SObj) (l : List ((SynCat T).Hom A U1)) : Prop :=
  ∀ (N : SModel T) c (σ : Nat → (N.M.P.obj c).car) (vs : List (N.M.P.obj c).car),
    force N.K N.M c σ A.φ → LR T N σ l vs → N.M.rel R c vs

/-- the universal structure -/
def UStr : @GStr (SynCat T) where
  P := yo (SynCat T) U1
  rel := relU T
  rel_resp := fun _ _ l l' e h N c σ vs hφ hl => h N c σ vs hφ (lr_leq T N σ l l' vs e hl)
  rel_stable := fun R {_ _} f l h N c σ vs hφ hl =>
    N.closed.rel_loc R c vs _ (exN_wit N.K N.M _ _ σ (f.2.2.2.1 N c σ hφ)) (fun d k ⟨ys, hyl, ff⟩ =>
      h N d (extLV N.M (resV N.M k σ) ys) _ (f.2.2.1 N d _ ys hyl ff).2
        (lr_comp T N f (resV N.M k σ) ys hyl ff l _ (lr_res T N k σ _ vs hl)))

end Gt

end Found

#print axioms Found.Gt.lr_res
#print axioms Found.Gt.lr_leq
#print axioms Found.Gt.lr_comp
#print axioms Found.Gt.UStr

found/Gt39.lean

lean_all/found/Gt39.lean

/-
  T194 (classifying topos), part 39. found/Gt39 — the universal structure
  is closed for the countable syntactic coverage: its carrier is a sheaf
  (subcanonicity) and its relations are local.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt38

namespace Found

namespace Gt

open Sh

variable (T : Seq → Prop)

theorem ext_agree1 {C : ECat.{0, 0}} (M : @GStr C) {c : C.Ob} (n : Nat) {τ τ' : Nat → (M.P.obj c).car}
    (h : ∀ j, j < n → (M.P.obj c).eqv (τ j) (τ' j)) (v : (M.P.obj c).car) :
    ∀ m, m < 1 + n → (M.P.obj c).eqv (extV M τ v m) (extV M τ' v m)
  | 0, _ => (M.P.obj c).refl _
  | j + 1, hm => h j (Nat.lt_of_succ_lt_succ (show j + 1 < n + 1 by rw [Nat.add_comm n 1]; exact hm))

theorem lr_compI (N : SModel T) {A B : SObj} (f : (SynCat T).Hom B A) {c : N.D.Ob} (σ τ : Nat → (N.M.P.obj c).car)
    (ys : List (N.M.P.obj c).car) (hyl : lenG ys = A.n) (ff : force N.K N.M c (extLV N.M σ ys) f.1)
    (he : ∀ i, i < A.n → (N.M.P.obj c).eqv (extLV N.M σ ys i) (τ i)) :
    (l : List ((SynCat T).Hom A U1)) → (vs : List (N.M.P.obj c).car) →
      LR T N τ l vs → LR T N σ (lmapG (fun a => (SynCat T).comp f a) l) vs
  | [], [], _ => trivial
  | a :: l, v :: vs, ⟨h1, h2⟩ => ⟨comp_intro T (Y := A) (Z := U1) a.2.1 N σ ys [v] hyl rfl ff
      (force_agree N.K N.M _ (1 + A.n) a.2.1 (ext_agree1 N.M A.n (fun j hj => (N.M.P.obj c).symm (he j hj)) v) h1),
      lr_compI N f σ τ ys hyl ff he l vs h2⟩
  | [], _ :: _, h => h.elim
  | _ :: _, [], h => h.elim

theorem relU_loc (K : CP.Classical') (R : Nat) (A : SObj) (l : List ((SynCat T).Hom A U1)) (S : Sieve (SynCat T) A)
    (hS : (JTc T K).cov S)
    (h : ∀ B (u : (SynCat T).Hom B A), S.S u → relU T R B (lmapG (fun a => (SynCat T).comp u a) l)) :
    relU T R A l := by
  intro N c σ vs hφ hl
  match hS with
  | ⟨F, hF, hcov⟩ =>
  exact N.closed.rel_loc R c vs _ (hcov N c σ hφ) (fun d g ⟨n, σ', ys, hyl, he, hf⟩ =>
    h _ (F.u n) (hF n) N d σ' _ ((F.u n).2.2.1 N d σ' ys hyl hf).1
      (lr_compI T N (F.u n) σ' (resV N.M g σ) ys hyl hf he l _ (lr_res T N g σ l vs hl)))

theorem u_closed (K : CP.Classical') : Closed (JTc T K) (UStr T) :=
  ⟨sheaf_sep (JTc T K) _ (yo_sheaf T K U1), fun R A l S hS h => relU_loc T K R A l S hS h⟩

end Gt

end Found

#print axioms Found.Gt.lr_compI
#print axioms Found.Gt.relU_loc
#print axioms Found.Gt.u_closed

found/Gt4.lean

lean_all/found/Gt4.lean

/-
  T194 (classifying topos), part 4. found/Gt4 — if P is separated then P⁺
  is a sheaf: a matching family of elements of P⁺ on a covering sieve is
  glued on the sieve of composites h ∘ f (h in the sieve of the element at
  f); the value at g is read off any such representation (chosen by the
  classical point), and different representations agree by separatedness.
  Hence P⁺⁺ is a sheaf for every presheaf P (sheafification).

  Lean 4 core only, hand-written proofs, no axioms (classical logic only as
  the parameter K, for choosing representations).
-/
import found.Gt3
import found.ClassicalPoint

namespace Found

namespace Gt

open Sh

variable {C : ECat.{0, 0}} (J : GTop C) (P : Psh.{0, 0, 0} C)

section Glue

variable {c : C.Ob} {S : Sieve C c} (M : MFam (Plus J P) S)

structure Rep {e : C.Ob} (g : C.Hom e c) where
  d : C.Ob
  f : C.Hom d c
  hf : S.S f
  h : C.Hom e d
  eq : C.heq g (C.comp h f)
  mem : (M.x f hf).S.S h

def GS : Sieve C c where
  S := fun g => ∃ _ : Rep J P M g, True
  resp := fun e ⟨r, _⟩ => ⟨⟨r.d, r.f, r.hf, r.h, C.htrans (C.hsymm e) r.eq, r.mem⟩, trivial⟩
  stable := fun ⟨r, _⟩ k => ⟨⟨r.d, r.f, r.hf, C.comp k r.h,
    C.htrans (C.rcong k r.eq) (C.hsymm (C.assoc k r.h r.f)), (M.x r.f r.hf).S.stable r.mem k⟩, trivial⟩

def val {e : C.Ob} {g : C.Hom e c} (r : Rep J P M g) : (P.obj e).car := (M.x r.f r.hf).m.x r.h r.mem

variable (hsep : Separated J P)

include hsep in
/-- two representations of equivalent arrows give equal values -/
theorem wd {e : C.Ob} {g g' : C.Hom e c} (r : Rep J P M g) (r' : Rep J P M g') (eg : C.heq g g') :
    (P.obj e).eqv (val J P M r) (val J P M r') := by
  have e1 : PEq J P (M.x (C.comp r.h r.f) (S.stable r.hf r.h)) (resE J P r.h (M.x r.f r.hf)) :=
    M.compat r.f r.hf r.h
  have e2 : PEq J P (M.x (C.comp r'.h r'.f) (S.stable r'.hf r'.h)) (resE J P r'.h (M.x r'.f r'.hf)) :=
    M.compat r'.f r'.hf r'.h
  have e3 : PEq J P (M.x (C.comp r.h r.f) (S.stable r.hf r.h)) (M.x (C.comp r'.h r'.f) (S.stable r'.hf r'.h)) :=
    M.resp _ _ (C.htrans (C.hsymm r.eq) (C.htrans eg r'.eq))
  match peq_trans J P (peq_symm J P e1) (peq_trans J P e3 e2) with
  | ⟨R, hR, inc, ag⟩ =>
    refine hsep e (meetS R (meetS (pullS r.h (M.x r.f r.hf).S) (pullS r'.h (M.x r'.f r'.hf).S)))
      (cov_meet J hR (cov_meet J (J.pull r.h (M.x r.f r.hf).cov) (J.pull r'.h (M.x r'.f r'.hf).cov))) _ _
      (fun d k ⟨hk, h1, h2⟩ => ?_)
    exact (P.obj d).trans ((P.obj d).symm ((M.x r.f r.hf).m.compat r.h r.mem k))
      ((P.obj d).trans (ag d k h1 h2 hk) ((M.x r'.f r'.hf).m.compat r'.h r'.mem k))

variable (K : CP.Classical')

def rep {e : C.Ob} {g : C.Hom e c} (hg : (GS J P M).S g) : Rep J P M g :=
  K.ch (fun _ => True) (match hg with | ⟨r, _⟩ => ⟨r, trivial⟩)

def glueM : MFam P (GS J P M) where
  x := fun g hg => val J P M (rep J P M K hg)
  resp := fun hg hg' e => wd J P M hsep _ _ e
  compat := fun {_ _} g hg k => by
    let r := rep J P M K hg
    let r2 : Rep J P M (C.comp k g) := ⟨r.d, r.f, r.hf, C.comp k r.h,
      C.htrans (C.rcong k r.eq) (C.hsymm (C.assoc k r.h r.f)), (M.x r.f r.hf).S.stable r.mem k⟩
    exact (P.obj _).trans (wd J P M hsep _ r2 (C.hrefl _)) ((M.x r.f r.hf).m.compat r.h r.mem k)

include hsep K in
theorem glue_amalg (hS : J.cov S) :
    ∃ a : PElt J P c, ∀ d (f : C.Hom d c) (hf : S.S f), PEq J P (resE J P f a) (M.x f hf) := by
  have hT : J.cov (GS J P M) := J.trans hS (fun d f hf => cov_mono J (M.x f hf).cov
    (fun e h hh => ⟨⟨d, f, hf, h, C.hrefl _, hh⟩, trivial⟩))
  refine ⟨⟨GS J P M, hT, glueM J P M hsep K⟩, fun d f hf => ⟨(M.x f hf).S, (M.x f hf).cov,
    fun e h hh => ⟨⟨⟨d, f, hf, h, C.hrefl _, hh⟩, trivial⟩, hh⟩, fun e h h1 h2 _ => ?_⟩⟩
  exact wd J P M hsep _ ⟨d, f, hf, h, C.hrefl _, h2⟩ (C.hrefl _)

end Glue

theorem plus_sheaf (K : CP.Classical') (hsep : Separated J P) : IsSheaf J (Plus J P) := fun c S hS M =>
  ⟨match glue_amalg J P M hsep K hS with
    | ⟨a, ha⟩ => ⟨a, fun d f hf => ha d f hf⟩,
   fun a b ha hb => plus_sep J P c S hS a b (fun d f hf =>
     peq_trans J P (ha d f hf) (peq_symm J P (hb d f hf)))⟩

/-- sheafification: P⁺⁺ is a sheaf -/
theorem sheafify (K : CP.Classical') : IsSheaf J (Plus J (Plus J P)) :=
  plus_sheaf J (Plus J P) K (plus_sep J P)

end Gt

end Found

#print axioms Found.Gt.wd
#print axioms Found.Gt.glueM
#print axioms Found.Gt.glue_amalg
#print axioms Found.Gt.plus_sheaf
#print axioms Found.Gt.sheafify

found/Gt40.lean

lean_all/found/Gt40.lean

/-
  T194 (classifying topos), part 40. found/Gt40 — subobjects of A in the
  syntactic category: a formula ψ in the context of A that implies A.φ
  gives an inclusion arrow ⟨A.n, ψ⟩ → A; composing an element of A with the
  inclusion is restricting it to ψ. The image object of k elements under a
  formula ψ(v̄): A.φ ∧ ∃v̄ (graph ∧ ψ).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt39

namespace Found

namespace Gt

open Sh

variable (T : Seq → Prop)

def subO (A : SObj) (ψ : GFm) (hψ : FVg A.n ψ) : SObj := ⟨A.n, ψ, hψ⟩

theorem incl_arr (A : SObj) (ψ : GFm) (hψ : FVg A.n ψ)
    (hs : ∀ (N : SModel T) c (ρ : Nat → (N.M.P.obj c).car), force N.K N.M c ρ ψ → force N.K N.M c ρ A.φ) :
    IsArr T (subO A ψ hψ) A (idF (subO A ψ hψ)) :=
  have h := id_arr T (subO A ψ hψ)
  ⟨h.1, fun N c ρ ys hl f => ⟨(h.2.1 N c ρ ys hl f).1, hs N c _ (h.2.1 N c ρ ys hl f).2⟩, h.2.2.1, h.2.2.2⟩

def inclH (A : SObj) (ψ : GFm) (hψ : FVg A.n ψ)
    (hs : ∀ (N : SModel T) c (ρ : Nat → (N.M.P.obj c).car), force N.K N.M c ρ ψ → force N.K N.M c ρ A.φ) :
    (SynCat T).Hom (subO A ψ hψ) A := ⟨_, incl_arr T A ψ hψ hs⟩

theorem witL_agree {C : ECat.{0, 0}} (M : @GStr C) {c : C.Ob} (σ : Nat → (M.P.obj c).car) (n : Nat) :
    ∀ j, j < n → (M.P.obj c).eqv (extLV M σ (witL M σ n) j) (σ j) := fun j hj => by
  rw [witL_val M σ n σ j hj]; exact (M.P.obj c).refl _

theorem comp_incl (A : SObj) (ψ : GFm) (hψ : FVg A.n ψ)
    (hs : ∀ (N : SModel T) c (ρ : Nat → (N.M.P.obj c).car), force N.K N.M c ρ ψ → force N.K N.M c ρ A.φ)
    (a : (SynCat T).Hom A U1) (N : SModel T) {c : N.D.Ob} (σ : Nat → (N.M.P.obj c).car) (y : (N.M.P.obj c).car) :
    force N.K N.M c (extV N.M σ y) ((SynCat T).comp (inclH T A ψ hψ hs) a).1 ↔
      force N.K N.M c σ ψ ∧ force N.K N.M c (extV N.M σ y) a.1 := by
  have hwl : lenG (witL N.M σ A.n) = A.n := len_witL N.M σ A.n
  constructor
  · intro h
    have hB : force N.K N.M c σ ψ := (((SynCat T).comp (inclH T A ψ hψ hs) a).2.2.1 N c σ [y] rfl h).1
    have hi : force N.K N.M c (extLV N.M σ (witL N.M σ A.n)) (idF (subO A ψ hψ)) := id_hits T N (subO A ψ hψ) σ hB
    have g := comp_elim T (incl_arr T A ψ hψ hs) a.2 N σ (witL N.M σ A.n) [y] hwl rfl h hi
    exact ⟨hB, force_agree N.K N.M _ (1 + A.n) a.2.1 (ext_agree1 N.M A.n (witL_agree N.M σ A.n) y) g⟩
  · intro ⟨hB, ha⟩
    exact comp_intro T (Y := A) (Z := U1) a.2.1 N σ (witL N.M σ A.n) [y] hwl rfl (id_hits T N (subO A ψ hψ) σ hB)
      (force_agree N.K N.M _ (1 + A.n) a.2.1
        (ext_agree1 N.M A.n (fun j hj => (N.M.P.obj c).symm (witL_agree N.M σ A.n j hj)) y) ha)

/-- the image formula of k elements under ψ(v̄) -/
def imgF {A : SObj} (ρ : Nat → (SynCat T).Hom A U1) (k : Nat) (ψ : GFm) : GFm :=
  .conj A.φ (exN k (.conj (grK T ρ k) ψ))

theorem fv_imgF {A : SObj} (ρ : Nat → (SynCat T).Hom A U1) (k : Nat) (ψ : GFm) (hψ : FVg k ψ) :
    FVg A.n (imgF T ρ k ψ) :=
  ⟨A.fv, fv_exN k ⟨by rw [Nat.add_comm A.n k]; exact fv_grK T ρ k, fvg_mono ψ hψ (Nat.le_add_left k A.n)⟩⟩

theorem imgF_sub {A : SObj} (ρ : Nat → (SynCat T).Hom A U1) (k : Nat) (ψ : GFm) :
    ∀ (N : SModel T) c (σ : Nat → (N.M.P.obj c).car), force N.K N.M c σ (imgF T ρ k ψ) → force N.K N.M c σ A.φ :=
  fun _ _ _ h => h.1

end Gt

end Found

#print axioms Found.Gt.incl_arr
#print axioms Found.Gt.comp_incl
#print axioms Found.Gt.fv_imgF

found/Gt41.lean

lean_all/found/Gt41.lean

/-
  T194 (classifying topos), part 41. found/Gt41 — the truth lemma for the
  universal model, first cases. Sem k A ρ φ: in every sheaf model, values
  of the k elements ρ over A satisfy φ. Forcing in the universal model is
  Sem for atomic formulas, equalities, ⊤ and conjunctions.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt40

namespace Found

namespace Gt

open Sh

variable (T : Seq → Prop)

/-- semantic truth of φ for k elements over A -/
def Sem (k : Nat) (A : SObj) (ρ : Nat → (SynCat T).Hom A U1) (φ : GFm) : Prop :=
  ∀ (N : SModel T) c (σ : Nat → (N.M.P.obj c).car) (v : Nat → (N.M.P.obj c).car), force N.K N.M c σ A.φ →
    (∀ i, i < k → force N.K N.M c (extV N.M σ (v i)) (ρ i).1) → force N.K N.M c v φ

theorem lr_map (N : SModel T) {A : SObj} (ρ : Nat → (SynCat T).Hom A U1) (k : Nat) {c : N.D.Ob}
    (σ v : Nat → (N.M.P.obj c).car) (hv : ∀ i, i < k → force N.K N.M c (extV N.M σ (v i)) (ρ i).1) :
    (l : List Nat) → AllLtG k l → LR T N σ (lmapG ρ l) (lmapG v l)
  | [], _ => trivial
  | j :: l, ⟨hj, hl⟩ => ⟨hv j hj, lr_map N ρ k σ v hv l hl⟩

theorem leq_of_lr (N : SModel T) {A : SObj} (ρ : Nat → (SynCat T).Hom A U1) (k : Nat) {c : N.D.Ob}
    (τ w : Nat → (N.M.P.obj c).car) (hw : ∀ i, i < k → force N.K N.M c (extV N.M τ (w i)) (ρ i).1) :
    (l : List Nat) → AllLtG k l → (vs : List (N.M.P.obj c).car) → LR T N τ (lmapG ρ l) vs →
      LEq N.M.P c (lmapG w l) vs
  | [], _, [], _ => trivial
  | j :: l, ⟨hj, hl⟩, v :: vs, ⟨h1, h2⟩ =>
    ⟨(ρ j).2.2.2.2 N c τ [w j] [v] rfl rfl (hw j hj) h1 0 (Nat.zero_lt_one), leq_of_lr N ρ k τ w hw l hl vs h2⟩
  | [], _, _ :: _, h => h.elim
  | _ :: _, _, [], h => h.elim

variable (K : CP.Classical')

theorem tl_atom (k : Nat) (R : Nat) (l : List Nat) (hl : AllLtG k l) (A : SObj) (ρ : Nat → (SynCat T).Hom A U1) :
    force (JTc T K) (UStr T) A ρ (.atom R l) ↔ Sem T k A ρ (.atom R l) := by
  constructor
  · intro h N c σ v hφ hv
    exact h N c σ (lmapG v l) hφ (lr_map T N ρ k σ v hv l hl)
  · intro h N c σ vs hφ hr
    refine N.closed.rel_loc R c vs _ (cov_tot T N ρ σ hφ k) (fun d g ⟨w, hw⟩ => ?_)
    exact N.M.rel_resp R d _ _ (leq_of_lr T N ρ k (resV N.M g σ) w hw l hl _ (lr_res T N g σ _ vs hr))
      (h N d (resV N.M g σ) w (force_stable N.K N.M A.φ g σ hφ) hw)

theorem tl_eq_dir (k i j : Nat) (hi : i < k) (hj : j < k) (A : SObj) (ρ : Nat → (SynCat T).Hom A U1)
    (h : Sem T k A ρ (.eq i j)) (N : SModel T) {c : N.D.Ob} (σ : Nat → (N.M.P.obj c).car) (y : (N.M.P.obj c).car)
    (fi : force N.K N.M c (extV N.M σ y) (ρ i).1) : force N.K N.M c (extV N.M σ y) (ρ j).1 := by
  have hφ := ((ρ i).2.2.1 N c σ [y] rfl fi).1
  refine force_local N.K N.M N.closed _ _ _ (cov_tot T N ρ σ hφ k) (fun d g ⟨w, hw⟩ => ?_)
  have e : (N.M.P.obj d).eqv (w i) (w j) := h N d (resV N.M g σ) w (force_stable N.K N.M A.φ g σ hφ) hw
  have fi' := force_resp N.K N.M _ (ext_res N.M g σ y) (force_stable N.K N.M _ g _ fi)
  have ey : (N.M.P.obj d).eqv (N.M.P.map g y) (w i) :=
    (ρ i).2.2.2.2 N d (resV N.M g σ) [N.M.P.map g y] [w i] rfl rfl fi' (hw i hi) 0 (Nat.zero_lt_one)
  refine force_resp N.K N.M _ (fun n => (N.M.P.obj d).symm (ext_res N.M g σ y n)) ?_
  exact force_resp N.K N.M _ (ρ := extV N.M (resV N.M g σ) (w j))
    (ρ' := extV N.M (resV N.M g σ) (N.M.P.map g y)) (fun n => match n with
    | 0 => (N.M.P.obj d).symm ((N.M.P.obj d).trans ey e)
    | _ + 1 => (N.M.P.obj d).refl _) (hw j hj)

theorem tl_eq (k i j : Nat) (hi : i < k) (hj : j < k) (A : SObj) (ρ : Nat → (SynCat T).Hom A U1) :
    force (JTc T K) (UStr T) A ρ (.eq i j) ↔ Sem T k A ρ (.eq i j) := by
  constructor
  · intro h N c σ v _ hv
    have fj : force N.K N.M c (extV N.M σ (v i)) (ρ j).1 := (h N c σ [v i] rfl).1 (hv i hi)
    exact (ρ j).2.2.2.2 N c σ [v i] [v j] rfl rfl fj (hv j hj) 0 (Nat.zero_lt_one)
  · intro h N c σ ys hl
    match ys, hl with
    | [y], _ =>
      have h' : Sem T k A ρ (.eq j i) := fun N c σ v hφ hv => (N.M.P.obj c).symm (h N c σ v hφ hv)
      exact ⟨tl_eq_dir T k i j hi hj A ρ h N σ y, tl_eq_dir T k j i hj hi A ρ h' N σ y⟩
    | [], hl => exact Nat.noConfusion hl
    | _ :: _ :: _, hl => exact Nat.noConfusion (Nat.succ.inj hl)

theorem tl_top (k : Nat) (A : SObj) (ρ : Nat → (SynCat T).Hom A U1) :
    force (JTc T K) (UStr T) A ρ .top ↔ Sem T k A ρ .top :=
  ⟨fun _ _ _ _ _ _ _ => trivial, fun _ => trivial⟩

theorem tl_conj (k : Nat) (a b : GFm) (A : SObj) (ρ : Nat → (SynCat T).Hom A U1)
    (ha : force (JTc T K) (UStr T) A ρ a ↔ Sem T k A ρ a) (hb : force (JTc T K) (UStr T) A ρ b ↔ Sem T k A ρ b) :
    force (JTc T K) (UStr T) A ρ (.conj a b) ↔ Sem T k A ρ (.conj a b) :=
  ⟨fun ⟨h1, h2⟩ N c σ v hφ hv => ⟨ha.1 h1 N c σ v hφ hv, hb.1 h2 N c σ v hφ hv⟩,
   fun h => ⟨ha.2 (fun N c σ v hφ hv => (h N c σ v hφ hv).1), hb.2 (fun N c σ v hφ hv => (h N c σ v hφ hv).2)⟩⟩

end Gt

end Found

#print axioms Found.Gt.tl_atom
#print axioms Found.Gt.tl_eq
#print axioms Found.Gt.tl_top
#print axioms Found.Gt.tl_conj

found/Gt42.lean

lean_all/found/Gt42.lean

/-
  T194 (classifying topos), part 42. found/Gt42 — the truth lemma for
  countable disjunctions: forcing ⋁_n φ_n in the universal model is Sem.
  The covering family for ⇐ is the family of image subobjects
  A.φ ∧ ∃v̄ (graph ∧ φ_n).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt41

namespace Found

namespace Gt

open Sh

variable (T : Seq → Prop)

theorem sem_pull (N : SModel T) {A B : SObj} (u : (SynCat T).Hom B A) (ρ : Nat → (SynCat T).Hom A U1) (k : Nat)
    {d : N.D.Ob} (σ' τ : Nat → (N.M.P.obj d).car) (ys : List (N.M.P.obj d).car) (hyl : lenG ys = A.n)
    (hu : force N.K N.M d (extLV N.M σ' ys) u.1) (he : ∀ i, i < A.n → (N.M.P.obj d).eqv (extLV N.M σ' ys i) (τ i))
    (w : Nat → (N.M.P.obj d).car) (hw : ∀ i, i < k → force N.K N.M d (extV N.M τ (w i)) (ρ i).1) :
    ∀ i, i < k → force N.K N.M d (extV N.M σ' (w i)) ((SynCat T).comp u (ρ i)).1 := fun i hi =>
  comp_intro T (Y := A) (Z := U1) (ρ i).2.1 N σ' ys [w i] hyl rfl hu
    (force_agree N.K N.M _ (1 + A.n) (ρ i).2.1 (ext_agree1 N.M A.n (fun j hj => (N.M.P.obj d).symm (he j hj)) (w i))
      (hw i hi))

theorem sem_res (N : SModel T) {A : SObj} (ρ : Nat → (SynCat T).Hom A U1) (k : Nat) {c d : N.D.Ob}
    (g : N.D.Hom d c) (σ v : Nat → (N.M.P.obj c).car) (hv : ∀ i, i < k → force N.K N.M c (extV N.M σ (v i)) (ρ i).1) :
    ∀ i, i < k → force N.K N.M d (extV N.M (resV N.M g σ) (resV N.M g v i)) (ρ i).1 := fun i hi =>
  force_resp N.K N.M _ (ext_res N.M g σ (v i)) (force_stable N.K N.M _ g _ (hv i hi))

variable (K : CP.Classical')

theorem tl_sup_l (k : Nat) (φs : Nat → GFm)
    (IH : ∀ n (B : SObj) (ρ' : Nat → (SynCat T).Hom B U1),
      force (JTc T K) (UStr T) B ρ' (φs n) ↔ Sem T k B ρ' (φs n))
    (A : SObj) (ρ : Nat → (SynCat T).Hom A U1) (h : force (JTc T K) (UStr T) A ρ (.sup φs)) :
    Sem T k A ρ (.sup φs) := by
  intro N c σ v hφ hv
  match h with
  | ⟨S, ⟨F, hF, hcov⟩, hh⟩ =>
  refine force_local N.K N.M N.closed _ v _ (hcov N c σ hφ) (fun d g ⟨n', σ', ys, hyl, he, hf⟩ => ?_)
  match hh _ (F.u n') (hF n') with
  | ⟨i, hi⟩ =>
  have r := (IH i _ _).1 hi N d σ' (resV N.M g v) ((F.u n').2.2.1 N d σ' ys hyl hf).1
    (sem_pull T N (F.u n') ρ k σ' (resV N.M g σ) ys hyl hf he _ (sem_res T N ρ k g σ v hv))
  exact ⟨maxS d, N.K.top d, fun _ h _ => ⟨i, force_stable N.K N.M _ h _ r⟩⟩

/-- the family of image subobjects -/
def imgFam {A : SObj} (ρ : Nat → (SynCat T).Hom A U1) (k : Nat) (φs : Nat → GFm) (hφs : ∀ n, FVg k (φs n)) :
    CFam T A :=
  ⟨fun n => subO A (imgF T ρ k (φs n)) (fv_imgF T ρ k (φs n) (hφs n)),
   fun n => inclH T A _ _ (imgF_sub T ρ k (φs n))⟩

/-- the k elements, restricted to an image subobject, satisfy φ -/
theorem sem_img (k : Nat) (ψ : GFm) (hψ : FVg k ψ) (A : SObj) (ρ : Nat → (SynCat T).Hom A U1) :
    Sem T k (subO A (imgF T ρ k ψ) (fv_imgF T ρ k ψ hψ))
      (fun i => (SynCat T).comp (inclH T A _ _ (imgF_sub T ρ k ψ)) (ρ i)) ψ := by
  intro N c σ v hB hv
  have hv' : ∀ i, i < k → force N.K N.M c (extV N.M σ (v i)) (ρ i).1 := fun i hi =>
    ((comp_incl T A _ _ (imgF_sub T ρ k ψ) (ρ i) N σ (v i)).1 (hv i hi)).2
  refine force_local N.K N.M N.closed ψ v _ (exN_wit N.K N.M _ k σ hB.2) (fun d g ⟨ws, hwl, ⟨hg, hp⟩⟩ => ?_)
  let W := extLV N.M (resV N.M g σ) ws
  have gr := (force_grK T N.K N.M ρ k W).1 hg
  refine force_agree N.K N.M ψ k hψ (fun i hi => ?_) hp
  have fi : force N.K N.M d (extV N.M (resV N.M g σ) (W i)) (ρ i).1 :=
    force_eqval N.K N.M (ρ := extV N.M (fun j => W (j + k)) (W i)) (ρ' := extV N.M (resV N.M g σ) (W i)) (fun m => match m with
      | 0 => rfl
      | j + 1 => shift_val N.M (resV N.M g σ) ws k hwl j) _ (gr i hi)
  exact (ρ i).2.2.2.2 N d (resV N.M g σ) [W i] [resV N.M g v i] rfl rfl fi (sem_res T N ρ k g σ v hv' i hi) 0
    (Nat.zero_lt_one)

/-- the image subobject of ψ contains every stage where values satisfy ψ -/
theorem img_hit (N : SModel T) (k : Nat) (ψ : GFm) (hψ : FVg k ψ) {A : SObj} (ρ : Nat → (SynCat T).Hom A U1)
    {c d e : N.D.Ob} (g : N.D.Hom d c) (h : N.D.Hom e d) (σ : Nat → (N.M.P.obj c).car) (hφ : force N.K N.M c σ A.φ)
    (w : Nat → (N.M.P.obj d).car) (hw : ∀ i, i < k → force N.K N.M d (extV N.M (resV N.M g σ) (w i)) (ρ i).1)
    (hn : force N.K N.M e (resV N.M h w) ψ) :
    force N.K N.M e (resV N.M (N.D.comp h g) σ) (imgF T ρ k ψ) := by
  let σ'' := resV N.M (N.D.comp h g) σ
  have hwl := len_witL N.M (resV N.M h w) k
  let W := extLV N.M σ'' (witL N.M (resV N.M h w) k)
  refine ⟨force_stable N.K N.M A.φ _ σ hφ, exN_of N.K N.M _ k σ'' _ hwl ⟨(force_grK T N.K N.M ρ k W).2 (fun i hi => ?_), ?_⟩⟩
  · have f1 := force_resp N.K N.M _ (ext_resR T N g h σ (w i)) (force_stable N.K N.M _ h _ (hw i hi))
    exact force_eqval N.K N.M (ρ := extV N.M σ'' (N.M.P.map h (w i))) (ρ' := extV N.M (fun j => W (j + k)) (W i))
      (fun m => match m with
        | 0 => (witL_val N.M (resV N.M h w) k σ'' i hi).symm
        | j + 1 => (shift_val N.M σ'' _ k hwl j).symm) _ f1
  · exact force_agree N.K N.M ψ k hψ (fun i hi => by
      rw [witL_val N.M (resV N.M h w) k σ'' i hi]; exact (N.M.P.obj e).refl _) hn

theorem tl_sup_r (k : Nat) (φs : Nat → GFm) (hφs : ∀ n, FVg k (φs n))
    (IH : ∀ n (B : SObj) (ρ' : Nat → (SynCat T).Hom B U1),
      force (JTc T K) (UStr T) B ρ' (φs n) ↔ Sem T k B ρ' (φs n))
    (A : SObj) (ρ : Nat → (SynCat T).Hom A U1) (h : Sem T k A ρ (.sup φs)) :
    force (JTc T K) (UStr T) A ρ (.sup φs) := by
  refine ⟨supS (JTc T K) (UStr T) ρ φs, ⟨imgFam T ρ k φs hφs, fun n => ⟨n, (IH n _ _).2
    (sem_img T k (φs n) (hφs n) A ρ)⟩, fun N c σ hφ => ?_⟩, fun _ _ hf => hf⟩
  refine N.K.trans (cov_tot T N ρ σ hφ k) (fun d g ⟨w, hw⟩ => ?_)
  match h N d (resV N.M g σ) w (force_stable N.K N.M A.φ g σ hφ) hw with
  | ⟨S', hS', hh'⟩ =>
  refine cov_mono N.K hS' (fun e hk hk' => ?_)
  match hh' e hk hk' with
  | ⟨n, hn⟩ =>
  have hB := img_hit T N k (φs n) (hφs n) ρ g hk σ hφ w hw hn
  exact ⟨n, resV N.M (N.D.comp hk g) σ, witL N.M (resV N.M (N.D.comp hk g) σ) A.n, len_witL N.M _ A.n,
    witL_agree N.M _ A.n, id_hits T N (subO A (imgF T ρ k (φs n)) (fv_imgF T ρ k (φs n) (hφs n))) _ hB⟩

theorem tl_sup (k : Nat) (φs : Nat → GFm) (hφs : ∀ n, FVg k (φs n))
    (IH : ∀ n (B : SObj) (ρ' : Nat → (SynCat T).Hom B U1),
      force (JTc T K) (UStr T) B ρ' (φs n) ↔ Sem T k B ρ' (φs n))
    (A : SObj) (ρ : Nat → (SynCat T).Hom A U1) :
    force (JTc T K) (UStr T) A ρ (.sup φs) ↔ Sem T k A ρ (.sup φs) :=
  ⟨tl_sup_l T K k φs IH A ρ, tl_sup_r T K k φs hφs IH A ρ⟩

end Gt

end Found

#print axioms Found.Gt.sem_pull
#print axioms Found.Gt.tl_sup_l
#print axioms Found.Gt.sem_img
#print axioms Found.Gt.img_hit
#print axioms Found.Gt.tl_sup

found/Gt43.lean

lean_all/found/Gt43.lean

/-
  T194 (classifying topos), part 43. found/Gt43 — the witness object for
  ∃: in context (y, x̄), A.φ(x̄) ∧ ∃v̄ (graph(v̄, x̄) ∧ φ(y, v̄)); renamings
  skipping y and moving y in front of v̄; reading the body; reading
  projection arrows.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt42

namespace Found

namespace Gt

open Sh

def skipY (k i : Nat) : Nat :=
  match Nat.decLt i k with
  | isTrue _ => i
  | isFalse _ => i + 1

theorem skip_lt {k i : Nat} (h : i < k) : skipY k i = i := by
  unfold skipY
  match Nat.decLt i k with
  | isTrue _ => rfl
  | isFalse h' => exact absurd h h'

theorem skip_ge (k j : Nat) : skipY k (j + k) = j + k + 1 := by
  unfold skipY
  match Nat.decLt (j + k) k with
  | isTrue h' => exact absurd h' (Nat.not_lt_of_le (Nat.le_add_left k j))
  | isFalse _ => rfl

def swapY (k : Nat) : Nat → Nat
  | 0 => k
  | i + 1 => i

theorem fv_skip (k n : Nat) : ∀ i, i < k + n → skipY k i < (1 + n) + k := fun i hi =>
  match Nat.decLt i k with
  | isTrue h => by rw [skip_lt h]; exact Nat.lt_of_lt_of_le h (Nat.le_add_left k _)
  | isFalse h => match not_lt_split h with
    | ⟨j, e⟩ => by
      subst e
      rw [skip_ge k j, Nat.add_right_comm j k 1, Nat.add_comm 1 n]
      have hj : j < n := Nat.lt_of_add_lt_add_right (by rw [Nat.add_comm k n] at hi; exact hi)
      exact Nat.add_lt_add_right (Nat.succ_lt_succ hj) k

theorem fv_swap (k n : Nat) : ∀ i, i < k + 1 → swapY k i < (1 + n) + k
  | 0, _ => Nat.lt_of_lt_of_le (Nat.lt_succ_self k) (by rw [Nat.add_comm (1 + n) k, ← Nat.add_assoc]; exact Nat.le_add_right _ _)
  | i + 1, h => Nat.lt_of_lt_of_le (Nat.lt_of_succ_lt_succ h) (Nat.le_add_left k _)

variable (T : Seq → Prop)

def exBody {A : SObj} (ρ : Nat → (SynCat T).Hom A U1) (k : Nat) (φ : GFm) : GFm :=
  .conj (renG (skipY k) (grK T ρ k)) (renG (swapY k) φ)

def exF {A : SObj} (ρ : Nat → (SynCat T).Hom A U1) (k : Nat) (φ : GFm) : GFm :=
  .conj (renG (· + 1) A.φ) (exN k (exBody T ρ k φ))

theorem fv_exF {A : SObj} (ρ : Nat → (SynCat T).Hom A U1) (k : Nat) (φ : GFm) (hφ : FVg (k + 1) φ) :
    FVg (1 + A.n) (exF T ρ k φ) :=
  ⟨fv_ren A.φ (fun i hi => by rw [Nat.add_comm 1 A.n]; exact Nat.succ_lt_succ hi) A.fv,
   fv_exN k ⟨fv_ren _ (fv_skip k A.n) (fv_grK T ρ k), fv_ren φ (fv_swap k A.n) hφ⟩⟩

/-- the witness object -/
def exO {A : SObj} (ρ : Nat → (SynCat T).Hom A U1) (k : Nat) (φ : GFm) (hφ : FVg (k + 1) φ) : SObj :=
  ⟨1 + A.n, exF T ρ k φ, fv_exF T ρ k φ hφ⟩

section V

variable {C : ECat.{0, 0}} (J : GTop C) (M : @GStr C)

theorem force_exBody {A : SObj} (ρ : Nat → (SynCat T).Hom A U1) (k : Nat) (φ : GFm) {c : C.Ob}
    (σE : Nat → (M.P.obj c).car) (ws : List (M.P.obj c).car) (hwl : lenG ws = k) :
    force J M c (extLV M σE ws) (exBody T ρ k φ) ↔
      (∀ i, i < k → force J M c (extV M (fun j => σE (j + 1)) (extLV M σE ws i)) (ρ i).1) ∧
      force J M c (extV M (extLV M σE ws) (σE 0)) φ := by
  let W := extLV M σE ws
  have eG : force J M c W (renG (skipY k) (grK T ρ k)) ↔ ∀ i, i < k → force J M c (extV M (fun j => σE (j + 1)) (W i)) (ρ i).1 :=
    (force_ren J M _ _ W).trans ((force_grK T J M ρ k _).trans
      ⟨fun h i hi => force_eqval J M (ρ := extV M (fun j => W (skipY k (j + k))) (W (skipY k i)))
          (ρ' := extV M (fun j => σE (j + 1)) (W i)) (fun m => match m with
            | 0 => by show W (skipY k i) = W i; rw [skip_lt hi]
            | j + 1 => by
              show W (skipY k (j + k)) = σE (j + 1)
              rw [skip_ge k j, Nat.add_right_comm j k 1]
              exact shift_val M σE ws k hwl (j + 1)) _ (h i hi),
       fun h i hi => force_eqval J M (ρ := extV M (fun j => σE (j + 1)) (W i))
          (ρ' := extV M (fun j => W (skipY k (j + k))) (W (skipY k i))) (fun m => match m with
            | 0 => by show W i = W (skipY k i); rw [skip_lt hi]
            | j + 1 => by
              show σE (j + 1) = W (skipY k (j + k))
              rw [skip_ge k j, Nat.add_right_comm j k 1]
              exact (shift_val M σE ws k hwl (j + 1)).symm) _ (h i hi)⟩)
  have eP : force J M c W (renG (swapY k) φ) ↔ force J M c (extV M W (σE 0)) φ :=
    (force_ren J M _ _ W).trans
      ⟨fun h => force_eqval J M (ρ := fun m => W (swapY k m)) (ρ' := extV M W (σE 0)) (fun m => match m with
          | 0 => by
            show W k = σE 0
            have q := shift_val M σE ws k hwl 0
            rw [Nat.zero_add] at q
            exact q
          | _ + 1 => rfl) φ h,
       fun h => force_eqval J M (ρ := extV M W (σE 0)) (ρ' := fun m => W (swapY k m)) (fun m => match m with
          | 0 => by
            show σE 0 = W k
            have q := shift_val M σE ws k hwl 0
            rw [Nat.zero_add] at q
            exact q.symm
          | _ + 1 => rfl) φ h⟩
  exact ⟨fun ⟨h1, h2⟩ => ⟨eG.1 h1, eP.1 h2⟩, fun ⟨h1, h2⟩ => ⟨eG.2 h1, eP.2 h2⟩⟩

theorem force_projF (P : SObj) (q o : Nat) {c : C.Ob} (σ : Nat → (M.P.obj c).car) (ys : List (M.P.obj c).car)
    (hl : lenG ys = q) :
    force J M c (extLV M σ ys) (projF P q o) ↔
      force J M c σ P.φ ∧ ∀ i, i < q → (M.P.obj c).eqv (extLV M σ ys i) (σ (i + o)) := by
  have e : ∀ i, extLV M σ ys (i + (q + o)) = σ (i + o) := fun i => by
    rw [← Nat.add_assoc, Nat.add_right_comm i q o]; exact shift_val M σ ys q hl (i + o)
  constructor
  · intro ⟨h1, h2⟩
    refine ⟨force_eqval J M (shift_val M σ ys q hl) P.φ ((force_ren J M P.φ _ _).1 h1), fun i hi => ?_⟩
    have q' := (force_eqOff J M q (q + o) _).1 h2 i hi
    rw [e i] at q'
    exact q'
  · intro ⟨h1, h2⟩
    refine ⟨(force_ren J M P.φ _ _).2 (force_eqval J M (fun i => (shift_val M σ ys q hl i).symm) P.φ h1),
      (force_eqOff J M q (q + o) _).2 (fun i hi => ?_)⟩
    rw [e i]
    exact h2 i hi

end V

section P

variable {A : SObj} (ρ : Nat → (SynCat T).Hom A U1) (k : Nat) (φ : GFm) (hφ : FVg (k + 1) φ)

def πY : (SynCat T).Hom (exO T ρ k φ hφ) U1 :=
  ⟨projF (exO T ρ k φ hφ) 1 0, proj_arr T (exO T ρ k φ hφ) U1 0
    (show 0 + 1 ≤ 1 + A.n by rw [Nat.zero_add]; exact Nat.le_add_right 1 A.n) (fun _ _ _ _ => trivial)⟩

def πA : (SynCat T).Hom (exO T ρ k φ hφ) A :=
  ⟨projF (exO T ρ k φ hφ) A.n 1, proj_arr T (exO T ρ k φ hφ) A 1 (Nat.le_refl _)
    (fun N _ _ h => (force_ren N.K N.M A.φ _ _).1 h.1)⟩

end P

end Gt

end Found

#print axioms Found.Gt.fv_exF
#print axioms Found.Gt.force_exBody
#print axioms Found.Gt.force_projF
#print axioms Found.Gt.πY
#print axioms Found.Gt.πA

found/Gt44.lean

lean_all/found/Gt44.lean

/-
  T194 (classifying topos), part 44. found/Gt44 — the truth lemma for ∃:
  forcing ∃y φ in the universal model is Sem. For ⇐ the covering arrow is
  the projection of the witness object onto A, and the witness is the
  projection onto y.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt43

namespace Found

namespace Gt

open Sh

variable (T : Seq → Prop) (K : CP.Classical')

theorem tl_ex_l (k : Nat) (φ : GFm)
    (IH : ∀ (B : SObj) (ρ' : Nat → (SynCat T).Hom B U1),
      force (JTc T K) (UStr T) B ρ' φ ↔ Sem T (k + 1) B ρ' φ)
    (A : SObj) (ρ : Nat → (SynCat T).Hom A U1) (h : force (JTc T K) (UStr T) A ρ (.ex φ)) :
    Sem T k A ρ (.ex φ) := by
  intro N c σ v hφ hv
  match h with
  | ⟨S, ⟨F, hF, hcov⟩, hh⟩ =>
  refine force_local N.K N.M N.closed _ v _ (hcov N c σ hφ) (fun d g ⟨n', σ', ys, hyl, he, hf⟩ => ?_)
  match hh _ (F.u n') (hF n') with
  | ⟨a, ha⟩ =>
  have sem := (IH _ _).1 ha
  have hB := ((F.u n').2.2.1 N d σ' ys hyl hf).1
  have hp := sem_pull T N (F.u n') ρ k σ' (resV N.M g σ) ys hyl hf he _ (sem_res T N ρ k g σ v hv)
  refine force_local N.K N.M N.closed _ _ _ (exN_wit N.K N.M a.1 1 σ' (a.2.2.2.1 N d σ' hB))
    (fun e h ⟨ds, hdl, fa⟩ => ?_)
  match ds, hdl with
  | [], hdl => exact Nat.noConfusion hdl
  | _ :: _ :: _, hdl => exact Nat.noConfusion (Nat.succ.inj hdl)
  | [y], _ =>
  have r := sem N e (resV N.M h σ') (extV N.M (resV N.M h (resV N.M g v)) y) (force_stable N.K N.M _ h σ' hB)
    (fun i hi => match i, hi with
      | 0, _ => fa
      | j + 1, hj => sem_res T N (fun i => (SynCat T).comp (F.u n') (ρ i)) k h σ' (resV N.M g v) hp j
          (Nat.lt_of_succ_lt_succ hj))
  exact ⟨maxS e, N.K.top e, fun _ h' _ => ⟨N.M.P.map h' y,
    force_resp N.K N.M φ (ext_res N.M h' _ y) (force_stable N.K N.M φ h' _ r)⟩⟩

theorem sem_exO (k : Nat) (φ : GFm) (hφ : FVg (k + 1) φ) (A : SObj) (ρ : Nat → (SynCat T).Hom A U1) :
    Sem T (k + 1) (exO T ρ k φ hφ)
      (extV (UStr T) (fun i => (SynCat T).comp (πA T ρ k φ hφ) (ρ i)) (πY T ρ k φ hφ)) φ := by
  intro N c σ v hE hv
  have e0 : (N.M.P.obj c).eqv (v 0) (σ 0) :=
    ((force_projF N.K N.M (exO T ρ k φ hφ) 1 0 σ [v 0] rfl).1 (hv 0 (Nat.succ_pos k))).2 0 (Nat.zero_lt_one)
  have hys := len_witL N.M (fun i => σ (i + 1)) A.n
  have hπ : force N.K N.M c (extLV N.M σ (witL N.M (fun i => σ (i + 1)) A.n)) (πA T ρ k φ hφ).1 :=
    (force_projF N.K N.M (exO T ρ k φ hφ) A.n 1 σ _ hys).2 ⟨hE, fun i hi => by
      rw [witL_val N.M _ A.n σ i hi]; exact (N.M.P.obj c).refl _⟩
  have hv' : ∀ i, i < k → force N.K N.M c (extV N.M (fun j => σ (j + 1)) (v (i + 1))) (ρ i).1 := fun i hi =>
    force_agree N.K N.M _ (1 + A.n) (ρ i).2.1
      (ext_agree1 N.M A.n (fun j hj => by rw [witL_val N.M _ A.n σ j hj]; exact (N.M.P.obj c).refl _) _)
      (comp_elim T (πA T ρ k φ hφ).2 (ρ i).2 N σ _ [v (i + 1)] hys rfl (hv (i + 1) (Nat.succ_lt_succ hi)) hπ)
  refine force_local N.K N.M N.closed φ v _ (exN_wit N.K N.M _ k σ hE.2) (fun d g ⟨ws, hwl, hb⟩ => ?_)
  have b := (force_exBody T N.K N.M ρ k φ (resV N.M g σ) ws hwl).1 hb
  refine force_agree N.K N.M φ (k + 1) hφ (fun m hm => ?_) b.2
  match m, hm with
  | 0, _ => exact N.M.P.map_eqv g ((N.M.P.obj c).symm e0)
  | i + 1, hi =>
    have hi' := Nat.lt_of_succ_lt_succ hi
    have r := force_resp N.K N.M _ (ext_res N.M g _ (v (i + 1))) (force_stable N.K N.M _ g _ (hv' i hi'))
    exact (ρ i).2.2.2.2 N d _ [_] [_] rfl rfl (b.1 i hi') r 0 (Nat.zero_lt_one)

theorem exO_hit (N : SModel T) (k : Nat) (φ : GFm) (hφ : FVg (k + 1) φ) {A : SObj} (ρ : Nat → (SynCat T).Hom A U1)
    {c d e : N.D.Ob} (g : N.D.Hom d c) (h : N.D.Hom e d) (σ : Nat → (N.M.P.obj c).car) (hφA : force N.K N.M c σ A.φ)
    (w : Nat → (N.M.P.obj d).car) (hw : ∀ i, i < k → force N.K N.M d (extV N.M (resV N.M g σ) (w i)) (ρ i).1)
    (y : (N.M.P.obj e).car) (hy : force N.K N.M e (extV N.M (resV N.M h w) y) φ) :
    force N.K N.M e (extV N.M (resV N.M (N.D.comp h g) σ) y) (exO T ρ k φ hφ).φ := by
  let σ'' := resV N.M (N.D.comp h g) σ
  let σE := extV N.M σ'' y
  have hwl := len_witL N.M (resV N.M h w) k
  refine ⟨(force_ren N.K N.M A.φ _ σE).2 (force_stable N.K N.M A.φ _ σ hφA),
    exN_of N.K N.M _ k σE _ hwl ((force_exBody T N.K N.M ρ k φ σE _ hwl).2 ⟨fun i hi => ?_, ?_⟩)⟩
  · rw [witL_val N.M (resV N.M h w) k σE i hi]
    exact force_resp N.K N.M _ (ext_resR T N g h σ (w i)) (force_stable N.K N.M _ h _ (hw i hi))
  · refine force_agree N.K N.M φ (k + 1) hφ (fun m hm => ?_) hy
    match m, hm with
    | 0, _ => exact (N.M.P.obj e).refl _
    | i + 1, hi =>
      show (N.M.P.obj e).eqv (resV N.M h w i) (extLV N.M σE (witL N.M (resV N.M h w) k) i)
      rw [witL_val N.M (resV N.M h w) k σE i (Nat.lt_of_succ_lt_succ hi)]
      exact (N.M.P.obj e).refl _

theorem tl_ex_r (k : Nat) (φ : GFm) (hφ : FVg (k + 1) φ)
    (IH : ∀ (B : SObj) (ρ' : Nat → (SynCat T).Hom B U1),
      force (JTc T K) (UStr T) B ρ' φ ↔ Sem T (k + 1) B ρ' φ)
    (A : SObj) (ρ : Nat → (SynCat T).Hom A U1) (h : Sem T k A ρ (.ex φ)) :
    force (JTc T K) (UStr T) A ρ (.ex φ) := by
  refine ⟨exS (JTc T K) (UStr T) ρ φ, ⟨⟨fun _ => exO T ρ k φ hφ, fun _ => πA T ρ k φ hφ⟩,
    fun _ => ⟨πY T ρ k φ hφ, (IH _ _).2 (sem_exO T k φ hφ A ρ)⟩, fun N c σ hφA => ?_⟩, fun _ _ hf => hf⟩
  refine N.K.trans (cov_tot T N ρ σ hφA k) (fun d g ⟨w, hw⟩ => ?_)
  match h N d (resV N.M g σ) w (force_stable N.K N.M A.φ g σ hφA) hw with
  | ⟨S', hS', hh'⟩ =>
  refine cov_mono N.K hS' (fun e hk hk' => ?_)
  match hh' e hk hk' with
  | ⟨y, hy⟩ =>
  let σ'' := resV N.M (N.D.comp hk g) σ
  have hE := exO_hit T N k φ hφ ρ g hk σ hφA w hw y hy
  have hl := len_witL N.M σ'' A.n
  refine ⟨0, extV N.M σ'' y, witL N.M σ'' A.n, hl, fun i hi => by
    rw [witL_val N.M σ'' A.n _ i hi]; exact (N.M.P.obj e).refl _, ?_⟩
  exact (force_projF N.K N.M (exO T ρ k φ hφ) A.n 1 _ _ hl).2 ⟨hE, fun i hi => by
    rw [witL_val N.M σ'' A.n _ i hi]; exact (N.M.P.obj e).refl _⟩

theorem tl_ex (k : Nat) (φ : GFm) (hφ : FVg (k + 1) φ)
    (IH : ∀ (B : SObj) (ρ' : Nat → (SynCat T).Hom B U1),
      force (JTc T K) (UStr T) B ρ' φ ↔ Sem T (k + 1) B ρ' φ)
    (A : SObj) (ρ : Nat → (SynCat T).Hom A U1) :
    force (JTc T K) (UStr T) A ρ (.ex φ) ↔ Sem T k A ρ (.ex φ) :=
  ⟨tl_ex_l T K k φ IH A ρ, tl_ex_r T K k φ hφ IH A ρ⟩

/-- the truth lemma for the universal model -/
theorem truth_lemma : (φ : GFm) → (k : Nat) → FVg k φ → ∀ (A : SObj) (ρ : Nat → (SynCat T).Hom A U1),
    force (JTc T K) (UStr T) A ρ φ ↔ Sem T k A ρ φ
  | .atom R l, k, hf, A, ρ => tl_atom T K k R l hf A ρ
  | .eq i j, k, hf, A, ρ => tl_eq T K k i j hf.1 hf.2 A ρ
  | .top, k, _, A, ρ => tl_top T K k A ρ
  | .conj a b, k, hf, A, ρ => tl_conj T K k a b A ρ (truth_lemma a k hf.1 A ρ) (truth_lemma b k hf.2 A ρ)
  | .sup φs, k, hf, A, ρ => tl_sup T K k φs hf (fun n B ρ' => truth_lemma (φs n) k (hf n) B ρ') A ρ
  | .ex φ, k, hf, A, ρ => tl_ex T K k φ hf (fun B ρ' => truth_lemma φ (k + 1) hf B ρ') A ρ

end Gt

end Found

#print axioms Found.Gt.tl_ex_l
#print axioms Found.Gt.sem_exO
#print axioms Found.Gt.exO_hit
#print axioms Found.Gt.tl_ex
#print axioms Found.Gt.truth_lemma

found/Gt45.lean

lean_all/found/Gt45.lean

/-
  T194 (classifying topos), part 45. found/Gt45 — the universal model:
  for a geometric theory T whose sequents are in finite contexts, the
  universal structure on the countable syntactic site is a sheaf model of
  T, and it is generic: a sequent in a finite context holds in it exactly
  when it holds in every sheaf model of T.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt44

namespace Found

namespace Gt

open Sh

variable (T : Seq → Prop) (K : CP.Classical')

/-- the sequent's semantic consequence transported through Sem -/
theorem sem_ent {k : Nat} {φ ψ : GFm} (h : Ent T φ ψ) {A : SObj} {ρ : Nat → (SynCat T).Hom A U1}
    (hs : Sem T k A ρ φ) : Sem T k A ρ ψ :=
  fun N c σ v hA hv => h N c v (hs N c σ v hA hv)

theorem u_valid {k : Nat} {φ ψ : GFm} (hφ : FVg k φ) (hψ : FVg k ψ) (h : Ent T φ ψ) (A : SObj)
    (ρ : Nat → (SynCat T).Hom A U1) (f : force (JTc T K) (UStr T) A ρ φ) : force (JTc T K) (UStr T) A ρ ψ :=
  (truth_lemma T K ψ k hψ A ρ).2 (sem_ent T h ((truth_lemma T K φ k hφ A ρ).1 f))

/-- the universal model of T -/
def UModel (hT : ∀ σ, T σ → ∃ k, FVg k σ.lhs ∧ FVg k σ.rhs) : SModel T where
  D := SynCat T
  K := JTc T K
  M := UStr T
  sheaf := yo_sheaf T K U1
  closed := u_closed T K
  model := fun σ hσ A ρ f => match hT σ hσ with
    | ⟨_, hl, hr⟩ => u_valid T K hl hr (ent_ax hσ) A ρ f

/-- the context of m+1 variables satisfying φ, and its variables as elements -/
def ctxO (m : Nat) (φ : GFm) (hφ : FVg (m + 1) φ) : SObj := ⟨m + 1, φ, hφ⟩

def varE (m : Nat) (φ : GFm) (hφ : FVg (m + 1) φ) (i : Nat) (hi : i < m + 1) : (SynCat T).Hom (ctxO m φ hφ) U1 :=
  ⟨projF (ctxO m φ hφ) 1 i, proj_arr T (ctxO m φ hφ) U1 i hi (fun _ _ _ _ => trivial)⟩

def varV (m : Nat) (φ : GFm) (hφ : FVg (m + 1) φ) (i : Nat) : (SynCat T).Hom (ctxO m φ hφ) U1 :=
  match Nat.decLt i (m + 1) with
  | isTrue h => varE T m φ hφ i h
  | isFalse _ => varE T m φ hφ 0 (Nat.succ_pos m)

theorem varV_lt (m : Nat) (φ : GFm) (hφ : FVg (m + 1) φ) {i : Nat} (hi : i < m + 1) :
    varV T m φ hφ i = varE T m φ hφ i hi := by
  unfold varV
  match Nat.decLt i (m + 1) with
  | isTrue _ => rfl
  | isFalse h' => exact absurd hi h'

/-- genericity: a sequent in a finite context holds in the universal model iff in every sheaf model -/
theorem u_generic (hT : ∀ σ, T σ → ∃ k, FVg k σ.lhs ∧ FVg k σ.rhs) {k : Nat} {φ ψ : GFm}
    (hφ : FVg k φ) (hψ : FVg k ψ) :
    (∀ (A : SObj) (ρ : Nat → (SynCat T).Hom A U1),
      force (UModel T K hT).K (UModel T K hT).M A ρ φ → force (UModel T K hT).K (UModel T K hT).M A ρ ψ) ↔
    Ent T φ ψ := by
  refine ⟨fun h => ?_, fun h A ρ f => u_valid T K hφ hψ h A ρ f⟩
  have hφ' : FVg (k + 1) φ := fvg_mono φ hφ (Nat.le_succ k)
  have hψ' : FVg (k + 1) ψ := fvg_mono ψ hψ (Nat.le_succ k)
  let A := ctxO k φ hφ'
  let ρ := varV T k φ hφ'
  have hvar : ∀ (N : SModel T) c (σ v : Nat → (N.M.P.obj c).car), ∀ i, i < k + 1 →
      (force N.K N.M c (extV N.M σ (v i)) (ρ i).1 ↔ force N.K N.M c σ φ ∧ (N.M.P.obj c).eqv (v i) (σ i)) :=
    fun N c σ v i hi => by
      show force N.K N.M c (extV N.M σ (v i)) (varV T k φ hφ' i).1 ↔ _
      rw [varV_lt T k φ hφ' hi]
      exact (force_projF N.K N.M A 1 i σ [v i] rfl).trans
        ⟨fun ⟨h1, h2⟩ => ⟨h1, by have q := h2 0 Nat.zero_lt_one; rw [Nat.zero_add] at q; exact q⟩,
         fun ⟨h1, h2⟩ => ⟨h1, fun j hj => match j, hj with
          | 0, _ => by rw [Nat.zero_add]; exact h2
          | j + 1, hj => absurd (Nat.lt_of_succ_lt_succ hj) (Nat.not_lt_zero j)⟩⟩
  have sφ : Sem T (k + 1) A ρ φ := fun N c σ v hA hv =>
    force_agree N.K N.M φ (k + 1) hφ' (fun i hi => (N.M.P.obj c).symm ((hvar N c σ v i hi).1 (hv i hi)).2) hA
  have fψ := h A ρ ((truth_lemma T K φ (k + 1) hφ' A ρ).2 sφ)
  have sψ := (truth_lemma T K ψ (k + 1) hψ' A ρ).1 fψ
  exact fun N c v f => sψ N c v v f (fun i hi => (hvar N c v v i hi).2 ⟨f, (N.M.P.obj c).refl _⟩)

end Gt

end Found

#print axioms Found.Gt.sem_ent
#print axioms Found.Gt.u_valid
#print axioms Found.Gt.UModel
#print axioms Found.Gt.u_generic

found/Gt46.lean

lean_all/found/Gt46.lean

/-
  T194 (classifying topos), part 46. found/Gt46 — the interpretation of an
  object of the syntactic category in a sheaf model N: F_N(A) = [[A.φ]],
  the sheaf of A.n-tuples (bounded functions) that satisfy A.φ at every
  later stage. It is a presheaf and (gluing coordinatewise) a sheaf.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt45

namespace Found

namespace Gt

open Sh

variable {T : Seq → Prop} (N : SModel T)

/-- n-tuples at a stage -/
def Tup (n : Nat) (c : N.D.Ob) : Type := (i : Nat) → i < n → (N.M.P.obj c).car

/-- a tuple in front of a valuation -/
def tv {n : Nat} {c : N.D.Ob} (ρ : Nat → (N.M.P.obj c).car) (t : Tup N n c) (i : Nat) : (N.M.P.obj c).car :=
  match Nat.decLt i n with
  | isTrue h => t i h
  | isFalse _ => ρ (subN i n)

theorem tv_lt {n : Nat} {c : N.D.Ob} (ρ : Nat → (N.M.P.obj c).car) (t : Tup N n c) {i : Nat} (h : i < n) :
    tv N ρ t i = t i h := by
  unfold tv
  match Nat.decLt i n with
  | isTrue _ => rfl
  | isFalse h' => exact absurd h h'

theorem tv_ge {n : Nat} {c : N.D.Ob} (ρ : Nat → (N.M.P.obj c).car) (t : Tup N n c) (j : Nat) :
    tv N ρ t (j + n) = ρ j := by
  unfold tv
  match Nat.decLt (j + n) n with
  | isTrue h' => exact absurd h' (Nat.not_lt_of_le (Nat.le_add_left n j))
  | isFalse _ => show ρ (subN (j + n) n) = ρ j; rw [subN_add]

theorem tv_eqv {n : Nat} {c : N.D.Ob} {ρ ρ' : Nat → (N.M.P.obj c).car} {t t' : Tup N n c}
    (h1 : ∀ i (h : i < n), (N.M.P.obj c).eqv (t i h) (t' i h)) (h2 : ∀ j, (N.M.P.obj c).eqv (ρ j) (ρ' j)) :
    ∀ i, (N.M.P.obj c).eqv (tv N ρ t i) (tv N ρ' t' i) := fun i =>
  match Nat.decLt i n with
  | isTrue h => by rw [tv_lt N ρ t h, tv_lt N ρ' t' h]; exact h1 i h
  | isFalse h => match not_lt_split h with
    | ⟨j, e⟩ => by subst e; rw [tv_ge, tv_ge]; exact h2 j

def resT {n : Nat} {c d : N.D.Ob} (f : N.D.Hom d c) (t : Tup N n c) : Tup N n d := fun i h => N.M.P.map f (t i h)

theorem res_tv {n : Nat} {c d : N.D.Ob} (f : N.D.Hom d c) (ρ : Nat → (N.M.P.obj c).car) (t : Tup N n c) :
    ∀ i, resV N.M f (tv N ρ t) i = tv N (resV N.M f ρ) (resT N f t) i := fun i =>
  match Nat.decLt i n with
  | isTrue h => by
    show N.M.P.map f (tv N ρ t i) = _
    rw [tv_lt N ρ t h, tv_lt N _ _ h]; rfl
  | isFalse h => match not_lt_split h with
    | ⟨j, e⟩ => by
      subst e
      show N.M.P.map f (tv N ρ t (j + n)) = _
      rw [tv_ge, tv_ge]; rfl

/-- t satisfies φ at every later stage -/
def SatT (A : SObj) {c : N.D.Ob} (t : Tup N A.n c) : Prop :=
  ∀ d (f : N.D.Hom d c) (ρ : Nat → (N.M.P.obj d).car), force N.K N.M d (tv N ρ (resT N f t)) A.φ

/-- the presheaf [[A.φ]] -/
def FP (A : SObj) : Psh.{0, 0, 0} N.D where
  obj c := ⟨{t : Tup N A.n c // SatT N A t}, fun x y => ∀ i (h : i < A.n), (N.M.P.obj c).eqv (x.1 i h) (y.1 i h),
    fun _ _ _ => (N.M.P.obj c).refl _, fun h i hi => (N.M.P.obj c).symm (h i hi),
    fun h k i hi => (N.M.P.obj c).trans (h i hi) (k i hi)⟩
  map f x := ⟨resT N f x.1, fun e g ρ => force_resp N.K N.M A.φ
    (tv_eqv N (fun i h => N.M.P.map_comp g f (x.1 i h)) (fun _ => (N.M.P.obj e).refl _))
    (x.2 e (N.D.comp g f) ρ)⟩
  map_eqv f _ _ h i hi := N.M.P.map_eqv f (h i hi)
  map_resp x h i hi := N.M.P.map_resp (x.1 i hi) h
  map_id _ x i hi := N.M.P.map_id _ (x.1 i hi)
  map_comp f g x i hi := N.M.P.map_comp f g (x.1 i hi)

theorem fp_sheaf (Kc : CP.Classical') (A : SObj) : IsSheaf N.K (FP N A) := by
  intro c S hS m
  let mi : (i : Nat) → i < A.n → MFam N.M.P S := fun i h =>
    ⟨fun f hf => (m.x f hf).1 i h, fun hf hf' e => m.resp hf hf' e i h, fun f hf g => m.compat f hf g i h⟩
  have ex : ∀ i (h : i < A.n), ∃ a, Amalg N.M.P (mi i h) a := fun i h => (N.sheaf c S hS (mi i h)).1
  let a : Tup N A.n c := fun i h => Kc.ch _ (ex i h)
  have ha : ∀ i (h : i < A.n), Amalg N.M.P (mi i h) (a i h) := fun i h => Kc.ch_spec _ (ex i h)
  have sat : SatT N A a := fun d f ρ =>
    force_local N.K N.M N.closed A.φ _ (pullS f S) (N.K.pull f hS) (fun e g hg => by
      have hx := (m.x (N.D.comp g f) hg).2 e (N.D.id e) (resV N.M g ρ)
      have h1 : force N.K N.M e (tv N (resV N.M g ρ) (resT N g (resT N f a))) A.φ :=
        force_resp N.K N.M A.φ (tv_eqv N (fun i h => (N.M.P.obj e).trans (N.M.P.map_id e _)
          ((N.M.P.obj e).trans ((N.M.P.obj e).symm (ha i h e (N.D.comp g f) hg)) (N.M.P.map_comp g f (a i h))))
          (fun _ => (N.M.P.obj e).refl _)) hx
      exact force_eqval N.K N.M (fun i => (res_tv N g ρ (resT N f a) i).symm) A.φ h1)
  exact ⟨⟨⟨a, sat⟩, fun d f hf i h => ha i h d f hf⟩,
    fun b b' hb hb' i h => (N.sheaf c S hS (mi i h)).2 (b.1 i h) (b'.1 i h)
      (fun d f hf => hb d f hf i h) (fun d f hf => hb' d f hf i h)⟩

/-- the sheaf [[A.φ]] -/
def FSh (Kc : CP.Classical') (A : SObj) : (ShCat N.K).Ob := ⟨FP N A, fp_sheaf N Kc A⟩

end Gt

end Found

#print axioms Found.Gt.tv_eqv
#print axioms Found.Gt.res_tv
#print axioms Found.Gt.FP
#print axioms Found.Gt.fp_sheaf

found/Gt47.lean

lean_all/found/Gt47.lean

/-
  T194 (classifying topos), part 47. found/Gt47 — arrows of the syntactic
  category in a sheaf model: θ relates tuples x (of A) and y (of B) when
  θ(y, x) holds at every later stage. The relation respects equivalence
  and restriction; in an inhabited model (every stage covered by stages
  with an element) it is single-valued and locally total.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt46

namespace Found

namespace Gt

open Sh

variable {T : Seq → Prop} (N : SModel T)

/-- θ(y, x) at every later stage -/
def RelT (θ : GFm) {a b : Nat} {c : N.D.Ob} (x : Tup N a c) (y : Tup N b c) : Prop :=
  ∀ d (f : N.D.Hom d c) (ρ : Nat → (N.M.P.obj d).car), force N.K N.M d (tv N (tv N ρ (resT N f x)) (resT N f y)) θ

theorem relT_resp (θ : GFm) {a b : Nat} {c : N.D.Ob} {x x' : Tup N a c} {y y' : Tup N b c}
    (hx : ∀ i (h : i < a), (N.M.P.obj c).eqv (x i h) (x' i h)) (hy : ∀ i (h : i < b), (N.M.P.obj c).eqv (y i h) (y' i h))
    (r : RelT N θ x y) : RelT N θ x' y' := fun d f ρ =>
  force_resp N.K N.M θ (tv_eqv N (fun i h => N.M.P.map_eqv f (hy i h))
    (tv_eqv N (fun i h => N.M.P.map_eqv f (hx i h)) (fun _ => (N.M.P.obj d).refl _))) (r d f ρ)

theorem relT_res (θ : GFm) {a b : Nat} {c e : N.D.Ob} (g : N.D.Hom e c) {x : Tup N a c} {y : Tup N b c}
    (r : RelT N θ x y) : RelT N θ (resT N g x) (resT N g y) := fun d f ρ =>
  force_resp N.K N.M θ (tv_eqv N (fun i h => N.M.P.map_comp f g (y i h))
    (tv_eqv N (fun i h => N.M.P.map_comp f g (x i h)) (fun _ => (N.M.P.obj d).refl _))) (r d (N.D.comp f g) ρ)

/-- stages with an element -/
def InhS (c : N.D.Ob) : Sieve N.D c where
  S := fun {d} _ => ∃ _ : (N.M.P.obj d).car, True
  resp := fun _ h => h
  stable := fun {_ _} {_} ⟨p, _⟩ g => ⟨N.M.P.map g p, trivial⟩

/-- the model is inhabited: every stage is covered by stages with an element -/
def Inh : Prop := ∀ c, N.K.cov (InhS N c)

theorem tv_list {n : Nat} {c : N.D.Ob} (β : Nat → (N.M.P.obj c).car) (t : Tup N n c) :
    ∀ i, extLV N.M β (witL N.M (tv N β t) n) i = tv N β t i := fun i =>
  match Nat.decLt i n with
  | isTrue h => witL_val N.M _ n β i h
  | isFalse h => match not_lt_split h with
    | ⟨j, e⟩ => by
      subst e
      rw [shift_val N.M β _ n (len_witL N.M _ n) j, tv_ge]

theorem relT_uniq (hI : Inh N) {A B : SObj} {θ : GFm} (hθ : IsArr T A B θ) {c : N.D.Ob} {x : Tup N A.n c}
    {y y' : Tup N B.n c} (r : RelT N θ x y) (r' : RelT N θ x y') :
    ∀ i (h : i < B.n), (N.M.P.obj c).eqv (y i h) (y' i h) := fun i h =>
  N.closed.sep c _ (hI c) (y i h) (y' i h) (fun d f ⟨p, _⟩ => by
    let β := tv N (fun _ => p) (resT N f x)
    have f1 := force_eqval N.K N.M (fun j => (tv_list N β (resT N f y) j).symm) θ (r d f (fun _ => p))
    have f2 := force_eqval N.K N.M (fun j => (tv_list N β (resT N f y') j).symm) θ (r' d f (fun _ => p))
    have q := hθ.2.2.2 N d β _ _ (len_witL N.M _ B.n) (len_witL N.M _ B.n) f1 f2 i h
    rw [tv_list, tv_list, tv_lt N β _ h, tv_lt N β _ h] at q
    exact q)

/-- stages where x has an image under θ -/
def WitT {A B : SObj} (θ : GFm) {c : N.D.Ob} (x : Tup N A.n c) : Sieve N.D c where
  S := fun {e} g => ∃ y : Tup N B.n e, SatT N B y ∧ RelT N θ (resT N g x) y
  resp := fun {_} {_ _} e ⟨y, hy, r⟩ => ⟨y, hy, relT_resp N θ (fun i h => N.M.P.map_resp (x i h) e)
    (fun _ _ => (N.M.P.obj _).refl _) r⟩
  stable := fun {_ _} {g} ⟨y, hy, r⟩ h => ⟨resT N h y, ((FP N B).map h ⟨y, hy⟩).2,
    relT_resp N θ (fun i hi => (N.M.P.obj _).symm (N.M.P.map_comp h g (x i hi))) (fun _ _ => (N.M.P.obj _).refl _)
      (relT_res N θ h r)⟩

theorem witT_cov (hI : Inh N) {A B : SObj} {θ : GFm} (hθ : IsArr T A B θ) {c : N.D.Ob} (x : Tup N A.n c)
    (hx : SatT N A x) : N.K.cov (WitT N (B := B) θ x) := by
  refine N.K.trans (hI c) (fun d f ⟨p, _⟩ => ?_)
  let β := tv N (fun _ => p) (resT N f x)
  have hT := hθ.2.2.1 N d β (hx d f (fun _ => p))
  refine cov_mono N.K (exN_wit N.K N.M θ B.n β hT) (fun e g ⟨ys, hyl, fθ⟩ => ?_)
  let β' := resV N.M g β
  let y : Tup N B.n e := fun i _ => extLV N.M β' ys i
  refine ⟨y, fun d' f' ρ => ?_, fun d' f' ρ => ?_⟩
  · have hB := (hθ.2.1 N e β' ys hyl fθ).2
    refine force_agree N.K N.M B.φ B.n B.fv (fun m hm => ?_) (force_stable N.K N.M B.φ f' _ hB)
    rw [tv_lt N ρ _ hm]; exact (N.M.P.obj d').refl _
  · refine force_agree N.K N.M θ (B.n + A.n) hθ.1 (fun m hm => ?_) (force_stable N.K N.M θ f' _ fθ)
    match Nat.decLt m B.n with
    | isTrue h => rw [tv_lt N _ _ h]; exact (N.M.P.obj d').refl _
    | isFalse h => match not_lt_split h with
      | ⟨j, eq⟩ =>
        subst eq
        have hj : j < A.n := Nat.lt_of_add_lt_add_right (by rw [Nat.add_comm B.n A.n] at hm; exact hm)
        rw [tv_ge, tv_lt N ρ _ hj]
        show (N.M.P.obj d').eqv (N.M.P.map f' (extLV N.M β' ys (j + B.n))) _
        rw [shift_val N.M β' ys B.n hyl j]
        show (N.M.P.obj d').eqv (N.M.P.map f' (N.M.P.map g (tv N (fun _ => p) (resT N f x) j))) _
        rw [tv_lt N _ _ hj]
        exact N.M.P.map_eqv f' ((N.M.P.obj e).symm (N.M.P.map_comp g f (x j hj)))

end Gt

end Found

#print axioms Found.Gt.relT_resp
#print axioms Found.Gt.relT_res
#print axioms Found.Gt.relT_uniq
#print axioms Found.Gt.WitT
#print axioms Found.Gt.witT_cov

found/Gt48.lean

lean_all/found/Gt48.lean

/-
  T194 (classifying topos), part 48. found/Gt48 — in an inhabited sheaf
  model, every tuple x of [[A.φ]] has an image under an arrow θ : A → B
  (glued from the local images); this gives the natural transformation
  F_N(θ) : [[A.φ]] → [[B.φ]].

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt47

namespace Found

namespace Gt

open Sh

variable {T : Seq → Prop} (N : SModel T) (Kc : CP.Classical') (hI : Inh N)

include Kc hI in
theorem relT_ex {A B : SObj} {θ : GFm} (hθ : IsArr T A B θ) {c : N.D.Ob} (x : Tup N A.n c) (hx : SatT N A x) :
    ∃ y : Tup N B.n c, SatT N B y ∧ RelT N θ x y := by
  let S := WitT N (B := B) θ x
  let pick : {e : N.D.Ob} → (f : N.D.Hom e c) → S.S f → Tup N B.n e := fun f hf =>
    Kc.ch (fun y => SatT N B y ∧ RelT N θ (resT N f x) y) hf
  have pk : ∀ {e} (f : N.D.Hom e c) (hf : S.S f), SatT N B (pick f hf) ∧ RelT N θ (resT N f x) (pick f hf) :=
    fun f hf => Kc.ch_spec (fun y => SatT N B y ∧ RelT N θ (resT N f x) y) hf
  let m : MFam (FP N B) S :=
    ⟨fun f hf => ⟨pick f hf, (pk f hf).1⟩,
     fun {_} {f f'} hf hf' e => relT_uniq N hI hθ (pk f hf).2
       (relT_resp N θ (fun i h => (N.M.P.obj _).symm (N.M.P.map_resp (x i h) e)) (fun _ _ => (N.M.P.obj _).refl _)
         (pk f' hf').2),
     fun f hf g => relT_uniq N hI hθ (pk _ (S.stable hf g)).2
       (relT_resp N θ (fun i h => (N.M.P.obj _).symm (N.M.P.map_comp g f (x i h))) (fun _ _ => (N.M.P.obj _).refl _)
         (relT_res N θ g (pk f hf).2))⟩
  match (fp_sheaf N Kc B c S (witT_cov N hI hθ x hx) m).1 with
  | ⟨a, ha⟩ =>
  refine ⟨a.1, a.2, fun d f ρ => force_local N.K N.M N.closed θ _ (pullS f S) (N.K.pull f (witT_cov N hI hθ x hx))
    (fun e g hg => ?_)⟩
  have r := (pk (N.D.comp g f) hg).2 e (N.D.id e) (resV N.M g ρ)
  refine force_eqval N.K N.M (fun i => (res_tv N g _ (resT N f a.1) i).symm) θ ?_
  refine force_resp N.K N.M θ (tv_eqv N (fun i h => (N.M.P.obj e).trans (N.M.P.map_id e _)
      ((N.M.P.obj e).trans ((N.M.P.obj e).symm (ha e (N.D.comp g f) hg i h)) (N.M.P.map_comp g f (a.1 i h))))
    (fun j => ?_)) r
  rw [res_tv N g ρ (resT N f x) j]
  exact tv_eqv N (fun i h => (N.M.P.obj e).trans (N.M.P.map_id e _) (N.M.P.map_comp g f (x i h)))
    (fun _ => (N.M.P.obj e).refl _) j

/-- F_N on arrows -/
def FMap {A B : SObj} {θ : GFm} (hθ : IsArr T A B θ) : PNat (FP N A) (FP N B) where
  app _ x := ⟨Kc.ch (fun y => SatT N B y ∧ RelT N θ x.1 y) (relT_ex N Kc hI hθ x.1 x.2),
    (Kc.ch_spec (fun y => SatT N B y ∧ RelT N θ x.1 y) (relT_ex N Kc hI hθ x.1 x.2)).1⟩
  app_eqv c {x x'} h := relT_uniq N hI hθ
    (Kc.ch_spec (fun y => SatT N B y ∧ RelT N θ x.1 y) (relT_ex N Kc hI hθ x.1 x.2)).2
    (relT_resp N θ (fun i hi => (N.M.P.obj c).symm (h i hi)) (fun _ _ => (N.M.P.obj c).refl _)
      (Kc.ch_spec (fun y => SatT N B y ∧ RelT N θ x'.1 y) (relT_ex N Kc hI hθ x'.1 x'.2)).2)
  nat {_ _} f x := relT_uniq N hI hθ
    (Kc.ch_spec (fun y => SatT N B y ∧ RelT N θ (resT N f x.1) y) (relT_ex N Kc hI hθ _ ((FP N A).map f x).2)).2
    (relT_res N θ f (Kc.ch_spec (fun y => SatT N B y ∧ RelT N θ x.1 y) (relT_ex N Kc hI hθ x.1 x.2)).2)

theorem fmap_rel {A B : SObj} {θ : GFm} (hθ : IsArr T A B θ) {c : N.D.Ob} (x : ((FP N A).obj c).car) :
    RelT N θ x.1 ((FMap N Kc hI hθ).app c x).1 :=
  (Kc.ch_spec (fun y => SatT N B y ∧ RelT N θ x.1 y) (relT_ex N Kc hI hθ x.1 x.2)).2

end Gt

end Found

#print axioms Found.Gt.relT_ex
#print axioms Found.Gt.FMap
#print axioms Found.Gt.fmap_rel

found/Gt49.lean

lean_all/found/Gt49.lean

/-
  T194 (classifying topos), part 49. found/Gt49 — an inhabited sheaf model
  N of T gives a functor F_N : C_T → Sh(D, K), A ↦ [[A.φ]], θ ↦ F_N(θ):
  provably equal arrows go to equal maps, identities to identities,
  composites to composites.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt48

namespace Found

namespace Gt

open Sh

variable {T : Seq → Prop} (N : SModel T) (Kc : CP.Classical') (hI : Inh N)

theorem tv_tl {a b : Nat} {c : N.D.Ob} (σ : Nat → (N.M.P.obj c).car) (Y : Tup N a c) (Z : Tup N b c) :
    ∀ i, extLV N.M (extLV N.M σ (witL N.M (tv N σ Y) a)) (witL N.M (tv N σ Z) b) i = tv N (tv N σ Y) Z i := fun i => by
  rw [extLV_base N.M (tv_list N σ Y) _ i]
  match Nat.decLt i b with
  | isTrue h => rw [witL_val N.M _ b _ i h, tv_lt N σ Z h, tv_lt N _ Z h]
  | isFalse h => match not_lt_split h with
    | ⟨j, e⟩ =>
      subst e
      rw [shift_val N.M _ _ b (len_witL N.M _ b) j, tv_ge]

theorem arreq_rel {B : SObj} {θ θ' : GFm} (h : ArrEq T B θ θ') {a : Nat} {c : N.D.Ob} {x : Tup N a c}
    {y : Tup N B.n c} (r : RelT N θ x y) : RelT N θ' x y := fun d f ρ => by
  let β := tv N ρ (resT N f x)
  have f1 := force_eqval N.K N.M (fun j => (tv_list N β (resT N f y) j).symm) θ (r d f ρ)
  exact force_eqval N.K N.M (tv_list N β (resT N f y)) θ' ((h N d β _ (len_witL N.M _ B.n)).1 f1)

theorem id_rel (A : SObj) {c : N.D.Ob} (x : ((FP N A).obj c).car) : RelT N (idF A) x.1 x.1 := fun d f ρ => by
  let X := resT N f x.1
  refine ⟨(force_ren N.K N.M A.φ _ _).2 (force_eqval N.K N.M (fun i => (tv_ge N (tv N ρ X) X i).symm) A.φ (x.2 d f ρ)),
    (force_eqBlock N.K N.M A.n _).2 (fun i hi => ?_)⟩
  rw [tv_lt N _ X hi, tv_ge, tv_lt N ρ X hi]
  exact (N.M.P.obj d).refl _

theorem comp_rel {A B C : SObj} {θ η : GFm} (hη : IsArr T B C η) {c : N.D.Ob} {x : Tup N A.n c} {y : Tup N B.n c}
    {z : Tup N C.n c} (r1 : RelT N θ x y) (r2 : RelT N η y z) : RelT N (compF B.n C.n θ η) x z := fun d f ρ => by
  let σ := tv N ρ (resT N f x)
  have f1 := force_eqval N.K N.M (fun j => (tv_list N σ (resT N f y) j).symm) θ (r1 d f ρ)
  have f2 := force_eqval N.K N.M (fun j => (tv_tl N σ (resT N f y) (resT N f z) j).symm) η (r2 d f σ)
  exact force_eqval N.K N.M (tv_list N σ (resT N f z)) _
    (comp_intro T (Y := B) (Z := C) hη.1 N σ _ _ (len_witL N.M _ B.n) (len_witL N.M _ C.n) f1 f2)

/-- the functor F_N : C_T → Sh(D, K) -/
def FN : EFun (SynCat T) (ShCat N.K) where
  obj A := FSh N Kc A
  map θ := FMap N Kc hI θ.2
  map_resp {_ _} {θ θ'} h _ x := relT_uniq N hI θ'.2 (arreq_rel N h (fmap_rel N Kc hI θ.2 x)) (fmap_rel N Kc hI θ'.2 x)
  map_id A _ x := relT_uniq N hI (id_arr T A) (fmap_rel N Kc hI (id_arr T A) x) (id_rel N A x)
  map_comp {_ _ _} θ η _ x := relT_uniq N hI (comp_arr T θ.2 η.2) (fmap_rel N Kc hI (comp_arr T θ.2 η.2) x)
    (comp_rel N η.2 (fmap_rel N Kc hI θ.2 x) (fmap_rel N Kc hI η.2 _))

end Gt

end Found

#print axioms Found.Gt.arreq_rel
#print axioms Found.Gt.id_rel
#print axioms Found.Gt.comp_rel
#print axioms Found.Gt.FN

found/Gt5.lean

lean_all/found/Gt5.lean

/-
  T194 (classifying topos), part 5. found/Gt5 — the universal property of
  the plus construction: a natural map α : P → F into a sheaf F extends to
  P⁺ → F (an element (S, m) goes to the amalgamation of α ∘ m, chosen by
  the classical point), the extension composed with the unit is α, and two
  extensions agreeing with α after the unit agree. Applied twice, maps
  from P into sheaves factor uniquely through the sheafification P⁺⁺.

  Lean 4 core only, hand-written proofs, no axioms (classical logic only as
  the parameter K).
-/
import found.Gt4

namespace Found

namespace Gt

open Sh

variable {C : ECat.{0, 0}} (J : GTop C) (P F : Psh.{0, 0, 0} C) (hF : IsSheaf J F) (α : PNat P F) (K : CP.Classical')

/-- α applied to a matching family -/
def mapM {c : C.Ob} {S : Sieve C c} (m : MFam P S) : MFam F S where
  x := fun f hf => α.app _ (m.x f hf)
  resp := fun hf hf' e => α.app_eqv _ (m.resp hf hf' e)
  compat := fun f hf g => (F.obj _).trans (α.app_eqv _ (m.compat f hf g)) (α.nat g (m.x f hf))

include hF in
theorem ext_ex {c : C.Ob} (a : PElt J P c) : ∃ y, Amalg F (mapM P F α a.m) y :=
  (hF c a.S a.cov (mapM P F α a.m)).1

def extA {c : C.Ob} (a : PElt J P c) : (F.obj c).car := K.ch _ (ext_ex J P F hF α a)

theorem extA_spec {c : C.Ob} (a : PElt J P c) : Amalg F (mapM P F α a.m) (extA J P F hF α K a) :=
  K.ch_spec _ (ext_ex J P F hF α a)

include hF in
/-- an amalgamation is determined by agreement on a covering sieve -/
theorem amalg_eq {c : C.Ob} {y z : (F.obj c).car} (R : Sieve C c) (hR : J.cov R)
    (h : ∀ d (f : C.Hom d c), R.S f → (F.obj d).eqv (F.map f y) (F.map f z)) : (F.obj c).eqv y z :=
  sheaf_sep J F hF c R hR y z h

def extN : PNat (Plus J P) F where
  app _ a := extA J P F hF α K a
  app_eqv _ {a b} h := match h with
    | ⟨R, hR, inc, e⟩ => amalg_eq J F hF R hR (fun d f hf =>
        (F.obj d).trans (extA_spec J P F hF α K a d f (inc d f hf).1)
          ((F.obj d).trans (α.app_eqv d (e d f (inc d f hf).1 (inc d f hf).2 hf))
            ((F.obj d).symm (extA_spec J P F hF α K b d f (inc d f hf).2))))
  nat {_ _} g a := amalg_eq J F hF (pullS g a.S) (J.pull g a.cov) (fun d f hf =>
    (F.obj d).trans (extA_spec J P F hF α K (resE J P g a) d f hf)
      ((F.obj d).trans ((F.obj d).symm (extA_spec J P F hF α K a d (C.comp f g) hf))
        ((F.obj d).trans (F.map_comp f g _) ((F.obj d).refl _))))

theorem ext_unit (c : C.Ob) (x : (P.obj c).car) :
    (F.obj c).eqv (extA J P F hF α K (etaE J P x)) (α.app c x) :=
  amalg_eq J F hF (maxS c) (J.top c) (fun d f _ =>
    (F.obj d).trans (extA_spec J P F hF α K (etaE J P x) d f trivial) (α.nat f x))

include hF in
/-- uniqueness: maps P⁺ → F agreeing after the unit agree -/
theorem ext_uniq (β γ : PNat (Plus J P) F)
    (h : ∀ c x, (F.obj c).eqv (β.app c (etaE J P x)) (γ.app c (etaE J P x))) :
    ∀ c a, (F.obj c).eqv (β.app c a) (γ.app c a) := fun c a =>
  amalg_eq J F hF a.S a.cov (fun d f hf => by
    -- the restriction of a along f agrees with the unit of its value at f
    have e : PEq J P (resE J P f a) (etaE J P (a.m.x f hf)) :=
      ⟨maxS d, J.top d, fun _ _ _ => ⟨a.S.stable hf _, trivial⟩, fun _ g h1 _ _ => a.m.compat f hf g⟩
    exact (F.obj d).trans ((F.obj d).symm (β.nat f a)) ((F.obj d).trans (β.app_eqv d e)
      ((F.obj d).trans (h d _) ((F.obj d).trans ((F.obj d).symm (γ.app_eqv d e)) (γ.nat f a)))))

end Gt

end Found

#print axioms Found.Gt.extN
#print axioms Found.Gt.ext_unit
#print axioms Found.Gt.ext_uniq

found/Gt50.lean

lean_all/found/Gt50.lean

/-
  T194 (classifying topos), part 50. found/Gt50 — continuous and flat
  (filtering) functors from a site into a sheaf category, elementwise:
  continuous = covering sieves go to jointly locally surjective families;
  flat = locally nonempty, locally a common source for two elements,
  locally an equalizing source for two parallel arrows that agree on an
  element. The functor F_N of an inhabited model is continuous.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt49

namespace Found

namespace Gt

open Sh

section Gen

variable {C D : ECat.{0, 0}} (J : GTop C) (K : GTop D) (F : EFun C (ShCat K))

/-- the stages where x is hit by the image of some arrow of S -/
def HitS {A : C.Ob} (S : Sieve C A) {c : D.Ob} (x : ((F.obj A).1.obj c).car) : Sieve D c where
  S := fun {d} g => ∃ (B : C.Ob) (u : C.Hom B A) (_ : S.S u) (y : ((F.obj B).1.obj d).car),
    ((F.obj A).1.obj d).eqv ((F.map u).app d y) ((F.obj A).1.map g x)
  resp := fun e ⟨B, u, hu, y, hy⟩ => ⟨B, u, hu, y, ((F.obj A).1.obj _).trans hy ((F.obj A).1.map_resp x e)⟩
  stable := fun {_ _} {g} ⟨B, u, hu, y, hy⟩ h => ⟨B, u, hu, (F.obj B).1.map h y,
    ((F.obj A).1.obj _).trans ((F.map u).nat h y) (((F.obj A).1.obj _).trans ((F.obj A).1.map_eqv h hy)
      (((F.obj A).1.obj _).symm ((F.obj A).1.map_comp h g x)))⟩

def ContF : Prop :=
  ∀ (A : C.Ob) (S : Sieve C A), J.cov S → ∀ (c : D.Ob) (x : ((F.obj A).1.obj c).car), K.cov (HitS K F S x)

end Gen

section FNc

variable {T : Seq → Prop} (N : SModel T) (Kc : CP.Classical') (hI : Inh N)

/-- a source valuation gives a tuple satisfying B.φ -/
theorem sat_of_val (B : SObj) {e : N.D.Ob} (σ' : Nat → (N.M.P.obj e).car) (hB : force N.K N.M e σ' B.φ) :
    SatT N B (fun i _ => σ' i) := fun _ f' ρ =>
  force_agree N.K N.M B.φ B.n B.fv (fun i hi => by rw [tv_lt N ρ _ hi]; exact (N.M.P.obj _).refl _)
    (force_stable N.K N.M B.φ f' σ' hB)

theorem fn_cont : ContF (JTc T Kc) N.K (FN N Kc hI) := by
  intro A S hS c x
  match hS with
  | ⟨F, hF, hcov⟩ =>
  refine N.K.trans (hI c) (fun d f ⟨p, _⟩ => ?_)
  let σ := tv N (fun _ => p) (resT N f x.1)
  refine cov_mono N.K (hcov N d σ (x.2 d f (fun _ => p))) (fun e h ⟨n, σ', ys, hyl, he, hu⟩ => ?_)
  have hB := ((F.u n).2.2.1 N e σ' ys hyl hu).1
  let y : ((FP N (F.B n)).obj e).car := ⟨fun i _ => σ' i, sat_of_val N (F.B n) σ' hB⟩
  have r : RelT N (F.u n).1 y.1 (resT N (N.D.comp h f) x.1) := fun d' f' ρ => by
    refine force_agree N.K N.M _ (A.n + (F.B n).n) (F.u n).2.1 (fun m hm => ?_) (force_stable N.K N.M _ f' _ hu)
    match Nat.decLt m A.n with
    | isTrue hm' =>
      rw [tv_lt N _ _ hm']
      have q := he m hm'
      have q2 : σ m = N.M.P.map f (x.1 m hm') := tv_lt N _ _ hm'
      show (N.M.P.obj d').eqv (N.M.P.map f' (extLV N.M σ' ys m)) (N.M.P.map f' (N.M.P.map (N.D.comp h f) (x.1 m hm')))
      refine N.M.P.map_eqv f' ((N.M.P.obj e).trans q ?_)
      show (N.M.P.obj e).eqv (N.M.P.map h (σ m)) _
      rw [q2]
      exact (N.M.P.obj e).symm (N.M.P.map_comp h f (x.1 m hm'))
    | isFalse hm' => match not_lt_split hm' with
      | ⟨j, eq⟩ =>
        subst eq
        have hj : j < (F.B n).n := Nat.lt_of_add_lt_add_right (by rw [Nat.add_comm A.n] at hm; exact hm)
        rw [tv_ge, tv_lt N ρ _ hj]
        show (N.M.P.obj d').eqv (N.M.P.map f' (extLV N.M σ' ys (j + A.n))) _
        rw [shift_val N.M σ' ys A.n hyl j]
        exact (N.M.P.obj d').refl _
  exact ⟨F.B n, F.u n, hF n, y, relT_uniq N hI (F.u n).2 (fmap_rel N Kc hI (F.u n).2 y) r⟩

end FNc

end Gt

end Found

#print axioms Found.Gt.HitS
#print axioms Found.Gt.sat_of_val
#print axioms Found.Gt.fn_cont

found/Gt51.lean

lean_all/found/Gt51.lean

/-
  T194 (classifying topos), part 51. found/Gt51 — the first two filtering
  conditions: F_N(1) has an element at every stage, and two elements
  x ∈ F_N(A), y ∈ F_N(B) are the images of one element of F_N(A × B)
  under the projections (A × B = the context x̄, ȳ with A.φ ∧ B.φ).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt50

namespace Found

namespace Gt

open Sh

section Gen

variable {C D : ECat.{0, 0}} (K : GTop D) (F : EFun C (ShCat K))

def NeS (c : D.Ob) : Sieve D c where
  S := fun {d} _ => ∃ (A : C.Ob) (_ : ((F.obj A).1.obj d).car), True
  resp := fun _ h => h
  stable := fun {_ _} {_} ⟨A, x, _⟩ g => ⟨A, (F.obj A).1.map g x, trivial⟩

def PairS {A B : C.Ob} {c : D.Ob} (x : ((F.obj A).1.obj c).car) (y : ((F.obj B).1.obj c).car) : Sieve D c where
  S := fun {d} g => ∃ (E : C.Ob) (u : C.Hom E A) (v : C.Hom E B) (z : ((F.obj E).1.obj d).car),
    ((F.obj A).1.obj d).eqv ((F.map u).app d z) ((F.obj A).1.map g x) ∧
    ((F.obj B).1.obj d).eqv ((F.map v).app d z) ((F.obj B).1.map g y)
  resp := fun e ⟨E, u, v, z, h1, h2⟩ => ⟨E, u, v, z, ((F.obj A).1.obj _).trans h1 ((F.obj A).1.map_resp x e),
    ((F.obj B).1.obj _).trans h2 ((F.obj B).1.map_resp y e)⟩
  stable := fun {_ _} {g} ⟨E, u, v, z, h1, h2⟩ h => ⟨E, u, v, (F.obj E).1.map h z,
    ((F.obj A).1.obj _).trans ((F.map u).nat h z) (((F.obj A).1.obj _).trans ((F.obj A).1.map_eqv h h1)
      (((F.obj A).1.obj _).symm ((F.obj A).1.map_comp h g x))),
    ((F.obj B).1.obj _).trans ((F.map v).nat h z) (((F.obj B).1.obj _).trans ((F.obj B).1.map_eqv h h2)
      (((F.obj B).1.obj _).symm ((F.obj B).1.map_comp h g y)))⟩

end Gen

variable (T : Seq → Prop)

def oneO : SObj := ⟨0, .top, trivial⟩

def prodO (A B : SObj) : SObj :=
  ⟨A.n + B.n, .conj A.φ (renG (· + A.n) B.φ),
   ⟨fvg_mono A.φ A.fv (Nat.le_add_right _ _), fv_ren B.φ (fun i hi => by rw [Nat.add_comm A.n B.n]; exact Nat.add_lt_add_right hi A.n) B.fv⟩⟩

theorem p1_arr (A B : SObj) : IsArr T (prodO A B) A (projF (prodO A B) A.n 0) :=
  proj_arr T (prodO A B) A 0 (by show 0 + A.n ≤ A.n + B.n; rw [Nat.zero_add]; exact Nat.le_add_right _ _)
    (fun _ _ _ h => h.1)

theorem p2_arr (A B : SObj) : IsArr T (prodO A B) B (projF (prodO A B) B.n A.n) :=
  proj_arr T (prodO A B) B A.n (Nat.le_refl _) (fun N _ _ h => (force_ren N.K N.M B.φ _ _).1 h.2)

section FNf

variable {T} (N : SModel T)

/-- a projection relates a tuple to its block -/
theorem relT_proj (P Q : SObj) (o : Nat) (hb : o + Q.n ≤ P.n) {c : N.D.Ob} (x : Tup N P.n c) (hx : SatT N P x)
    (y : Tup N Q.n c) (hy : ∀ i (h : i < Q.n), (N.M.P.obj c).eqv (y i h) (x (i + o) (Nat.lt_of_lt_of_le
      (by rw [Nat.add_comm i o]; exact Nat.add_lt_add_left h o) hb))) :
    RelT N (projF P Q.n o) x y := fun d f ρ => by
  let σ := tv N ρ (resT N f x)
  refine force_eqval N.K N.M (tv_list N σ (resT N f y)) _
    ((force_projF N.K N.M P Q.n o σ _ (len_witL N.M _ Q.n)).2 ⟨hx d f ρ, fun i hi => ?_⟩)
  rw [tv_list, tv_lt N σ _ hi]
  show (N.M.P.obj d).eqv (N.M.P.map f (y i hi)) (tv N ρ (resT N f x) (i + o))
  have hio : i + o < P.n := Nat.lt_of_lt_of_le (by rw [Nat.add_comm i o]; exact Nat.add_lt_add_left hi o) hb
  rw [tv_lt N ρ _ hio]
  exact N.M.P.map_eqv f (hy i hi)

theorem tup_congr {b : Nat} {c : N.D.Ob} (y : Tup N b c) {k j : Nat} (e : k = j) (h1 : k < b) (h2 : j < b) :
    y k h1 = y j h2 := by subst e; rfl

/-- concatenation of tuples -/
def catT {a b : Nat} {c : N.D.Ob} (x : Tup N a c) (y : Tup N b c) : Tup N (a + b) c := fun i h =>
  match Nat.decLt i a with
  | isTrue h' => x i h'
  | isFalse h' => y (subN i a) (match not_lt_split h' with
    | ⟨j, e⟩ => by
      subst e
      rw [subN_add]
      exact Nat.lt_of_add_lt_add_right (by rw [Nat.add_comm a b] at h; exact h))

theorem catT_lt {a b : Nat} {c : N.D.Ob} (x : Tup N a c) (y : Tup N b c) {i : Nat} (h : i < a + b) (h' : i < a) :
    catT N x y i h = x i h' := by
  unfold catT
  match Nat.decLt i a with
  | isTrue _ => rfl
  | isFalse h2 => exact absurd h' h2

theorem catT_ge {a b : Nat} {c : N.D.Ob} (x : Tup N a c) (y : Tup N b c) (j : Nat) (h : j + a < a + b) (hj : j < b) :
    catT N x y (j + a) h = y j hj := by
  unfold catT
  match Nat.decLt (j + a) a with
  | isTrue h2 => exact absurd h2 (Nat.not_lt_of_le (Nat.le_add_left a j))
  | isFalse _ =>
    exact tup_congr N y (subN_add j a) _ hj

variable (Kc : CP.Classical') (hI : Inh N)

theorem fn_ne : ∀ c, N.K.cov (NeS N.K (FN N Kc hI) c) := fun c =>
  cov_mono N.K (N.K.top c) (fun _ _ _ => ⟨oneO, ⟨fun i h => absurd h (Nat.not_lt_zero i), fun _ _ _ => trivial⟩, trivial⟩)

theorem cat_sat (A B : SObj) {c : N.D.Ob} (X : Tup N A.n c) (Y : Tup N B.n c) (hX : SatT N A X) (hY : SatT N B Y) :
    SatT N (prodO A B) (catT N X Y) := fun d f ρ =>
  ⟨force_agree N.K N.M A.φ A.n A.fv (fun i hi => by
      rw [tv_lt N ρ (resT N f X) hi, tv_lt N (n := (prodO A B).n) ρ (resT N f (catT N X Y)) (Nat.lt_of_lt_of_le hi (Nat.le_add_right A.n B.n))]
      have q : catT N X Y i (Nat.lt_of_lt_of_le hi (Nat.le_add_right A.n B.n)) = X i hi := catT_lt N X Y _ hi
      show (N.M.P.obj d).eqv (N.M.P.map f (X i hi)) (N.M.P.map f (catT N X Y i (Nat.lt_of_lt_of_le hi (Nat.le_add_right A.n B.n))))
      exact q ▸ (N.M.P.obj d).refl _) (hX d f ρ),
   (force_ren N.K N.M B.φ _ _).2 (force_agree N.K N.M B.φ B.n B.fv (fun i hi => by
      have h2 : i + A.n < A.n + B.n := by rw [Nat.add_comm A.n B.n]; exact Nat.add_lt_add_right hi A.n
      rw [tv_lt N ρ (resT N f Y) hi, tv_lt N (n := (prodO A B).n) ρ (resT N f (catT N X Y)) h2]
      have q : catT N X Y (i + A.n) h2 = Y i hi := catT_ge N X Y i h2 hi
      show (N.M.P.obj d).eqv (N.M.P.map f (Y i hi)) (N.M.P.map f (catT N X Y (i + A.n) h2))
      exact q ▸ (N.M.P.obj d).refl _) (hY d f ρ))⟩

theorem fn_pair (A B : SObj) {c : N.D.Ob} (x : ((FP N A).obj c).car) (y : ((FP N B).obj c).car) :
    N.K.cov (PairS N.K (FN N Kc hI) (A := A) (B := B) x y) :=
  cov_mono N.K (N.K.top c) (fun d g _ =>
    let xg := (FP N A).map g x
    let yg := (FP N B).map g y
    let z : ((FP N (prodO A B)).obj d).car := ⟨catT N xg.1 yg.1, cat_sat N A B xg.1 yg.1 xg.2 yg.2⟩
    ⟨prodO A B, ⟨_, p1_arr T A B⟩, ⟨_, p2_arr T A B⟩, z,
     relT_uniq N hI (p1_arr T A B) (fmap_rel N Kc hI (p1_arr T A B) z)
       (relT_proj N (prodO A B) A 0 (by show 0 + A.n ≤ A.n + B.n; rw [Nat.zero_add]; exact Nat.le_add_right _ _) z.1 z.2 xg.1 (fun i h => by
          show (N.M.P.obj d).eqv (xg.1 i h) (catT N xg.1 yg.1 i (Nat.lt_of_lt_of_le h (Nat.le_add_right _ _)))
          rw [catT_lt N xg.1 yg.1 _ h]; exact (N.M.P.obj d).refl _)),
     relT_uniq N hI (p2_arr T A B) (fmap_rel N Kc hI (p2_arr T A B) z)
       (relT_proj N (prodO A B) B A.n (Nat.le_refl _) z.1 z.2 yg.1 (fun i h => by
          have h2 : i + A.n < A.n + B.n := by rw [Nat.add_comm A.n B.n]; exact Nat.add_lt_add_right h A.n
          show (N.M.P.obj d).eqv (yg.1 i h) (catT N xg.1 yg.1 (i + A.n) h2)
          rw [catT_ge N xg.1 yg.1 i h2 h]; exact (N.M.P.obj d).refl _))⟩)

end FNf

end Gt

end Found

#print axioms Found.Gt.PairS
#print axioms Found.Gt.relT_proj
#print axioms Found.Gt.catT_ge
#print axioms Found.Gt.fn_ne
#print axioms Found.Gt.cat_sat
#print axioms Found.Gt.fn_pair

found/Gt52.lean

lean_all/found/Gt52.lean

/-
  T194 (classifying topos), part 52. found/Gt52 — the third filtering
  condition: if two arrows u, v : A → B agree on x ∈ F_N(A), then x comes
  from the equalizer subobject [x̄ : A.φ ∧ ∃ȳ (u(ȳ, x̄) ∧ v(ȳ, x̄))], whose
  inclusion equalizes u and v. Hence F_N is flat (filtering).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt51

namespace Found

namespace Gt

open Sh

section Gen

variable {C D : ECat.{0, 0}} (K : GTop D) (F : EFun C (ShCat K))

def EqzS {A B : C.Ob} (u v : C.Hom A B) {c : D.Ob} (x : ((F.obj A).1.obj c).car) : Sieve D c where
  S := fun {d} g => ∃ (E : C.Ob) (w : C.Hom E A) (z : ((F.obj E).1.obj d).car),
    C.heq (C.comp w u) (C.comp w v) ∧ ((F.obj A).1.obj d).eqv ((F.map w).app d z) ((F.obj A).1.map g x)
  resp := fun e ⟨E, w, z, h1, h2⟩ => ⟨E, w, z, h1, ((F.obj A).1.obj _).trans h2 ((F.obj A).1.map_resp x e)⟩
  stable := fun {_ _} {g} ⟨E, w, z, h1, h2⟩ h => ⟨E, w, (F.obj E).1.map h z, h1,
    ((F.obj A).1.obj _).trans ((F.map w).nat h z) (((F.obj A).1.obj _).trans ((F.obj A).1.map_eqv h h2)
      (((F.obj A).1.obj _).symm ((F.obj A).1.map_comp h g x)))⟩

/-- flat (filtering) functors into sheaves -/
structure FlatF : Prop where
  ne : ∀ c, K.cov (NeS K F c)
  pair : ∀ {A B : C.Ob} {c : D.Ob} (x : ((F.obj A).1.obj c).car) (y : ((F.obj B).1.obj c).car), K.cov (PairS K F x y)
  eqz : ∀ {A B : C.Ob} (u v : C.Hom A B) {c : D.Ob} (x : ((F.obj A).1.obj c).car),
    ((F.obj B).1.obj c).eqv ((F.map u).app c x) ((F.map v).app c x) → K.cov (EqzS K F u v x)

end Gen

variable (T : Seq → Prop)

/-- composing with a subobject inclusion restricts to the subobject -/
theorem comp_incl_g (A : SObj) (ψ : GFm) (hψ : FVg A.n ψ)
    (hs : ∀ (N : SModel T) c (ρ : Nat → (N.M.P.obj c).car), force N.K N.M c ρ ψ → force N.K N.M c ρ A.φ)
    {B : SObj} (a : (SynCat T).Hom A B) (N : SModel T) {c : N.D.Ob} (σ : Nat → (N.M.P.obj c).car)
    (ys : List (N.M.P.obj c).car) (hyl : lenG ys = B.n) :
    force N.K N.M c (extLV N.M σ ys) ((SynCat T).comp (inclH T A ψ hψ hs) a).1 ↔
      force N.K N.M c σ ψ ∧ force N.K N.M c (extLV N.M σ ys) a.1 := by
  have hwl : lenG (witL N.M σ A.n) = A.n := len_witL N.M σ A.n
  have ag : ∀ i, i < B.n + A.n → (N.M.P.obj c).eqv (extLV N.M (extLV N.M σ (witL N.M σ A.n)) ys i) (extLV N.M σ ys i) :=
    agree_blocks N.M _ _ _ _ B.n A.n hyl hyl (fun i hi => by
      rw [extLV_lt N.M (extLV N.M σ (witL N.M σ A.n)) σ ys i (by rw [hyl]; exact hi)]
      exact (N.M.P.obj c).refl _) (witL_agree N.M σ A.n)
  constructor
  · intro h
    have hB : force N.K N.M c σ ψ := (((SynCat T).comp (inclH T A ψ hψ hs) a).2.2.1 N c σ ys hyl h).1
    have g := comp_elim T (incl_arr T A ψ hψ hs) a.2 N σ (witL N.M σ A.n) ys hwl hyl h (id_hits T N (subO A ψ hψ) σ hB)
    exact ⟨hB, force_agree N.K N.M _ (B.n + A.n) a.2.1 ag g⟩
  · intro ⟨hB, ha⟩
    exact comp_intro T (Y := A) (Z := B) a.2.1 N σ (witL N.M σ A.n) ys hwl hyl (id_hits T N (subO A ψ hψ) σ hB)
      (force_agree N.K N.M _ (B.n + A.n) a.2.1 (fun i hi => (N.M.P.obj c).symm (ag i hi)) ha)

section Eqz

variable {A B : SObj} (u v : (SynCat T).Hom A B)

def eqzF : GFm := .conj A.φ (exN B.n (.conj u.1 v.1))

theorem fv_eqzF : FVg A.n (eqzF T u v) :=
  ⟨A.fv, fv_exN B.n ⟨by rw [Nat.add_comm A.n B.n]; exact u.2.1, by rw [Nat.add_comm A.n B.n]; exact v.2.1⟩⟩

def eqzW : (SynCat T).Hom (subO A (eqzF T u v) (fv_eqzF T u v)) A :=
  inclH T A _ _ (fun _ _ _ h => h.1)

/-- moving an arrow's value along a local common witness -/
theorem move_val (a b : (SynCat T).Hom A B) (Φ : GFm) (N : SModel T) {c : N.D.Ob} (σ : Nat → (N.M.P.obj c).car)
    (hex : force N.K N.M c σ (exN B.n Φ))
    (hΦ : ∀ {d : N.D.Ob} (V : Nat → (N.M.P.obj d).car), force N.K N.M d V Φ → force N.K N.M d V a.1 ∧ force N.K N.M d V b.1)
    (ys : List (N.M.P.obj c).car) (hyl : lenG ys = B.n) (ha : force N.K N.M c (extLV N.M σ ys) a.1) :
    force N.K N.M c (extLV N.M σ ys) b.1 := by
  refine force_local N.K N.M N.closed _ _ _ (exN_wit N.K N.M _ B.n σ hex) (fun d g ⟨ws, hwl, fw⟩ => ?_)
  have hyk : lenG (lmapG (N.M.P.map g) ys) = B.n := (len_lmapG _ ys).trans hyl
  have ha' := force_resp N.K N.M _ (res_block N.M g σ ys) (force_stable N.K N.M _ g _ ha)
  have fn := a.2.2.2.2 N d (resV N.M g σ) ws _ hwl hyk (hΦ _ fw).1 ha'
  refine force_resp N.K N.M _ (fun n => (N.M.P.obj d).symm (res_block N.M g σ ys n)) ?_
  exact force_agree N.K N.M _ (B.n + A.n) b.2.1
    (agree_blocks N.M _ _ _ _ B.n A.n hwl hyk fn (fun _ _ => (N.M.P.obj d).refl _)) (hΦ _ fw).2

theorem eqz_heq : (SynCat T).heq ((SynCat T).comp (eqzW T u v) u) ((SynCat T).comp (eqzW T u v) v) :=
  fun N _ σ ys hyl =>
    (comp_incl_g T A _ _ _ u N σ ys hyl).trans (Iff.trans
      ⟨fun ⟨hE, hu⟩ => ⟨hE, move_val T u v _ N σ hE.2 (fun _ h => h) ys hyl hu⟩,
       fun ⟨hE, hv⟩ => ⟨hE, move_val T v u _ N σ hE.2 (fun _ h => ⟨h.2, h.1⟩) ys hyl hv⟩⟩
      (comp_incl_g T A _ _ _ v N σ ys hyl).symm)

end Eqz

section FNe

variable {T} (N : SModel T) (Kc : CP.Classical') (hI : Inh N)

theorem fn_eqz {A B : SObj} (u v : (SynCat T).Hom A B) {c : N.D.Ob} (x : ((FP N A).obj c).car)
    (h : ((FP N B).obj c).eqv ((FMap N Kc hI u.2).app c x) ((FMap N Kc hI v.2).app c x)) :
    N.K.cov (EqzS N.K (FN N Kc hI) u v x) :=
  cov_mono N.K (N.K.top c) (fun d g _ => by
    let xg := (FP N A).map g x
    have hg : ((FP N B).obj d).eqv ((FMap N Kc hI u.2).app d xg) ((FMap N Kc hI v.2).app d xg) :=
      ((FP N B).obj d).trans ((FMap N Kc hI u.2).nat g x) (((FP N B).obj d).trans ((FP N B).map_eqv g h)
        (((FP N B).obj d).symm ((FMap N Kc hI v.2).nat g x)))
    have sat : SatT N (subO A (eqzF T u v) (fv_eqzF T u v)) xg.1 := fun d' f ρ => by
      let σ := tv N ρ (resT N f xg.1)
      let Y := resT N f ((FMap N Kc hI u.2).app d xg).1
      have ru := fmap_rel N Kc hI u.2 xg d' f ρ
      have rv := relT_resp N v.1 (fun _ _ => (N.M.P.obj d).refl _) (fun i hi => (N.M.P.obj d).symm (hg i hi))
        (fmap_rel N Kc hI v.2 xg) d' f ρ
      exact ⟨xg.2 d' f ρ, exN_of N.K N.M _ B.n σ _ (len_witL N.M _ B.n)
        ⟨force_eqval N.K N.M (fun j => (tv_list N σ Y j).symm) _ ru,
         force_eqval N.K N.M (fun j => (tv_list N σ Y j).symm) _ rv⟩⟩
    let z : ((FP N (subO A (eqzF T u v) (fv_eqzF T u v))).obj d).car := ⟨xg.1, sat⟩
    exact ⟨_, eqzW T u v, z, eqz_heq T u v,
      relT_uniq N hI (eqzW T u v).2 (fmap_rel N Kc hI (eqzW T u v).2 z) (id_rel N _ z)⟩)

theorem fn_flat : FlatF N.K (FN N Kc hI) :=
  ⟨fn_ne N Kc hI, fun {A B} _ x y => fn_pair N Kc hI A B x y, fun u v _ x h => fn_eqz N Kc hI u v x h⟩

end FNe

end Gt

end Found

#print axioms Found.Gt.EqzS
#print axioms Found.Gt.comp_incl_g
#print axioms Found.Gt.move_val
#print axioms Found.Gt.eqz_heq
#print axioms Found.Gt.fn_eqz
#print axioms Found.Gt.fn_flat

found/Gt53.lean

lean_all/found/Gt53.lean

/-
  T194 (classifying topos), part 53. found/Gt53 — toward models from flat
  continuous functors F : C_T → Sh(D, K): the terminal arrows A → 1, the
  covering U1 → 1, so F(U1) is locally inhabited; locally, any k elements
  of F(U1) are the images of one element e ∈ F(E) under arrows ρ_i : E → U1.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt52

namespace Found

namespace Gt

open Sh

variable (T : Seq → Prop)

theorem bang_arr (A : SObj) : IsArr T A oneO A.φ :=
  ⟨by show FVg (0 + A.n) A.φ; rw [Nat.zero_add]; exact A.fv,
   fun _ _ _ ys hl h => match ys, hl with
     | [], _ => ⟨h, trivial⟩,
   fun _ _ _ h => h,
   fun _ _ _ _ _ _ _ _ _ i hi => absurd hi (Nat.not_lt_zero i)⟩

def bang (A : SObj) : (SynCat T).Hom A oneO := ⟨A.φ, bang_arr T A⟩

/-- the sieve of arrows into 1 whose source has an element-arrow -/
def S1 : Sieve (SynCat T) oneO where
  S := fun {B} _ => ∃ _ : (SynCat T).Hom B U1, True
  resp := fun _ h => h
  stable := fun {_ _} {_} ⟨a, _⟩ g => ⟨(SynCat T).comp g a, trivial⟩

theorem s1_cov (Kc : CP.Classical') : (JTc T Kc).cov (S1 T) :=
  ⟨⟨fun _ => U1, fun _ => bang T U1⟩, fun _ => ⟨(SynCat T).id U1, trivial⟩, fun N c σ _ =>
    cov_mono N.K (N.K.top c) (fun _ g _ => ⟨0, resV N.M g σ, [], rfl, fun i hi => absurd hi (Nat.not_lt_zero i), trivial⟩)⟩

/-- U1 → 1 is a covering family -/
theorem u1_cov (Kc : CP.Classical') : (JTc T Kc).cov (maxS (C := SynCat T) oneO) :=
  ⟨⟨fun _ => U1, fun _ => bang T U1⟩, fun _ => trivial, fun N c σ _ =>
    cov_mono N.K (N.K.top c) (fun _ g _ => ⟨0, resV N.M g σ, [], rfl, fun i hi => absurd hi (Nat.not_lt_zero i), trivial⟩)⟩

section F

variable {D : ECat.{0, 0}} (K : GTop D) (F : EFun (SynCat T) (ShCat K))

/-- e ∈ F(E) represents the k values v through ρ -/
def RepAt (k : Nat) {c : D.Ob} (v : Nat → ((F.obj U1).1.obj c).car) {E : SObj} (ρ : Nat → (SynCat T).Hom E U1)
    (e : ((F.obj E).1.obj c).car) : Prop :=
  ∀ i, i < k → ((F.obj U1).1.obj c).eqv ((F.map (ρ i)).app c e) (v i)

/-- stages where the k values are represented -/
def RepS (k : Nat) {c : D.Ob} (v : Nat → ((F.obj U1).1.obj c).car) : Sieve D c where
  S := fun {d} g => ∃ (E : SObj) (ρ : Nat → (SynCat T).Hom E U1) (e : ((F.obj E).1.obj d).car),
    RepAt T K F k (fun i => (F.obj U1).1.map g (v i)) ρ e
  resp := fun h ⟨E, ρ, e, r⟩ => ⟨E, ρ, e, fun i hi => ((F.obj U1).1.obj _).trans (r i hi) ((F.obj U1).1.map_resp (v i) h)⟩
  stable := fun {_ _} {g} ⟨E, ρ, e, r⟩ h => ⟨E, ρ, (F.obj E).1.map h e, fun i hi =>
    ((F.obj U1).1.obj _).trans ((F.map (ρ i)).nat h e) (((F.obj U1).1.obj _).trans ((F.obj U1).1.map_eqv h (r i hi))
      (((F.obj U1).1.obj _).symm ((F.obj U1).1.map_comp h g (v i))))⟩

def InhU (c : D.Ob) : Sieve D c where
  S := fun {d} _ => ∃ _ : ((F.obj U1).1.obj d).car, True
  resp := fun _ h => h
  stable := fun {_ _} {_} ⟨p, _⟩ g => ⟨(F.obj U1).1.map g p, trivial⟩

theorem inhU_cov (Kc : CP.Classical') (hF : FlatF K F) (hC : ContF (JTc T Kc) K F) (c : D.Ob) :
    K.cov (InhU T K F c) :=
  K.trans (hF.ne c) (fun d _ ⟨A, x, _⟩ =>
    cov_mono K (hC oneO (S1 T) (s1_cov T Kc) d ((F.map (bang T A)).app d x))
      (fun e _ ⟨_, _, ⟨a, _⟩, z, _⟩ => ⟨(F.map a).app e z, trivial⟩))

theorem rep_ex (hF : FlatF K F) (hinh : ∀ c, K.cov (InhU T K F c)) :
    (k : Nat) → ∀ (c : D.Ob) (v : Nat → ((F.obj U1).1.obj c).car), K.cov (RepS T K F k v)
  | 0, c, _ => cov_mono K (hinh c) (fun _ _ ⟨p, _⟩ => ⟨U1, fun _ => (SynCat T).id U1, p,
      fun i hi => absurd hi (Nat.not_lt_zero i)⟩)
  | k + 1, c, v => K.trans (rep_ex hF hinh k c v) (fun d g ⟨E, ρ, e, r⟩ =>
      cov_mono K (hF.pair e ((F.obj U1).1.map g (v k))) (fun d' h ⟨E', u, w, e', h1, h2⟩ => by
        let P := (F.obj U1).1
        refine ⟨E', pickV k (fun i => (SynCat T).comp u (ρ i)) w, e', fun i hi => ?_⟩
        match Nat.decLt i k with
        | isTrue h' =>
          rw [pick_lt _ w h']
          exact (P.obj d').trans (F.map_comp u (ρ i) d' e') ((P.obj d').trans
            ((F.map (ρ i)).app_eqv d' h1) ((P.obj d').trans ((F.map (ρ i)).nat h e) ((P.obj d').trans
              (P.map_eqv h (r i h')) ((P.obj d').symm (P.map_comp h g (v i))))))
        | isFalse h' =>
          have e' : i = k := Nat.le_antisymm (Nat.le_of_lt_succ hi) (Nat.le_of_not_lt h')
          subst e'
          rw [pick_eq]
          exact (P.obj d').trans h2 ((P.obj d').symm (P.map_comp h g (v i)))))

end F

end Gt

end Found

#print axioms Found.Gt.bang_arr
#print axioms Found.Gt.u1_cov
#print axioms Found.Gt.RepS
#print axioms Found.Gt.s1_cov
#print axioms Found.Gt.inhU_cov
#print axioms Found.Gt.rep_ex

found/Gt54.lean

lean_all/found/Gt54.lean

/-
  T194 (classifying topos), part 54. found/Gt54 — Sem along arrows:
  precomposition with u : E' → E preserves Sem, equal element-arrows can be
  exchanged, a disjunct gives the disjunction; list equivalence tools.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt53

namespace Found

namespace Gt

open Sh

variable (T : Seq → Prop)

theorem sem_pre {k : Nat} {E E' : SObj} (u : (SynCat T).Hom E' E) (ρ : Nat → (SynCat T).Hom E U1) {φ : GFm}
    (h : Sem T k E ρ φ) : Sem T k E' (fun i => (SynCat T).comp u (ρ i)) φ := by
  intro N c σ v hE hv
  refine force_local N.K N.M N.closed φ v _ (exN_wit N.K N.M _ E.n σ (u.2.2.2.1 N c σ hE)) (fun d g ⟨ys, hyl, fu⟩ => ?_)
  have hE2 := (u.2.2.1 N d (resV N.M g σ) ys hyl fu).2
  refine h N d (extLV N.M (resV N.M g σ) ys) (resV N.M g v) hE2 (fun i hi => ?_)
  have h1 := force_resp N.K N.M _ (ext_res N.M g σ (v i)) (force_stable N.K N.M _ g _ (hv i hi))
  exact comp_elim T u.2 (ρ i).2 N (resV N.M g σ) ys [_] hyl rfl h1 fu

theorem sem_heq {k : Nat} {E : SObj} {ρ ρ' : Nat → (SynCat T).Hom E U1} {φ : GFm}
    (he : ∀ i, i < k → ArrEq T U1 (ρ i).1 (ρ' i).1) (h : Sem T k E ρ φ) : Sem T k E ρ' φ :=
  fun N c σ v hE hv => h N c σ v hE (fun i hi => (he i hi N c σ [v i] rfl).2 (hv i hi))

theorem sem_sup {k : Nat} {E : SObj} {ρ : Nat → (SynCat T).Hom E U1} {φs : Nat → GFm} (n : Nat)
    (h : Sem T k E ρ (φs n)) : Sem T k E ρ (.sup φs) :=
  fun N c σ v hE hv => ⟨maxS c, N.K.top c, fun _ g _ => ⟨n, force_stable N.K N.M _ g _ (h N c σ v hE hv)⟩⟩

section L

variable {C : ECat.{0, 0}} (P : Psh.{0, 0, 0} C)

theorem leq_refl {c : C.Ob} : (l : List (P.obj c).car) → LEq P c l l
  | [] => trivial
  | _ :: l => ⟨(P.obj c).refl _, leq_refl l⟩

theorem leq_symm {c : C.Ob} : (l l' : List (P.obj c).car) → LEq P c l l' → LEq P c l' l
  | [], [], _ => trivial
  | _ :: l, _ :: l', ⟨h1, h2⟩ => ⟨(P.obj c).symm h1, leq_symm l l' h2⟩
  | [], _ :: _, h => h.elim
  | _ :: _, [], h => h.elim

theorem leq_trans {c : C.Ob} : (l l' l'' : List (P.obj c).car) → LEq P c l l' → LEq P c l' l'' → LEq P c l l''
  | [], [], [], _, _ => trivial
  | _ :: l, _ :: l', _ :: l'', ⟨h1, h2⟩, ⟨k1, k2⟩ => ⟨(P.obj c).trans h1 k1, leq_trans l l' l'' h2 k2⟩
  | [], [], _ :: _, _, h => h.elim
  | [], _ :: _, _, h, _ => h.elim
  | _ :: _, [], _, h, _ => h.elim
  | _ :: _, _ :: _, [], _, h => h.elim

theorem leq_lmap {c d : C.Ob} (f : C.Hom d c) : (l l' : List (P.obj c).car) → LEq P c l l' →
    LEq P d (lmapG (P.map f) l) (lmapG (P.map f) l')
  | [], [], _ => trivial
  | _ :: l, _ :: l', ⟨h1, h2⟩ => ⟨P.map_eqv f h1, leq_lmap f l l' h2⟩
  | [], _ :: _, h => h.elim
  | _ :: _, [], h => h.elim

theorem leq_comp {c d e : C.Ob} (f : C.Hom d c) (g : C.Hom e d) : (l : List (P.obj c).car) →
    LEq P e (lmapG (P.map g) (lmapG (P.map f) l)) (lmapG (P.map (C.comp g f)) l)
  | [] => trivial
  | _ :: l => ⟨(P.obj e).symm (P.map_comp g f _), leq_comp f g l⟩

theorem leq_resp {c d : C.Ob} {f f' : C.Hom d c} (h : C.heq f f') : (l : List (P.obj c).car) →
    LEq P d (lmapG (P.map f) l) (lmapG (P.map f') l)
  | [] => trivial
  | _ :: l => ⟨P.map_resp _ h, leq_resp h l⟩

theorem leq_len {c : C.Ob} : (l l' : List (P.obj c).car) → LEq P c l l' → lenG l = lenG l'
  | [], [], _ => rfl
  | _ :: l, _ :: l', ⟨_, h2⟩ => congrArg (· + 1) (leq_len l l' h2)
  | [], _ :: _, h => h.elim
  | _ :: _, [], h => h.elim

end L

end Gt

end Found

#print axioms Found.Gt.sem_pre
#print axioms Found.Gt.sem_heq
#print axioms Found.Gt.sem_sup
#print axioms Found.Gt.leq_trans
#print axioms Found.Gt.leq_comp
#print axioms Found.Gt.leq_len

found/Gt55.lean

lean_all/found/Gt55.lean

/-
  T194 (classifying topos), part 55. found/Gt55 — the structure N_F of a
  functor F : C_T → Sh(D, K): carrier F(U1); R holds of a list vs at c
  when, locally, vs is the image of one generalized element e ∈ F(E)
  under arrows ρ_i : E → U1 with Sem(E ⊢ R(ρ)). It is closed (separated,
  local relations).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt54

namespace Found

namespace Gt

open Sh

variable (T : Seq → Prop) {D : ECat.{0, 0}} (K : GTop D) (F : EFun (SynCat T) (ShCat K))

/-- the images of e under ρ_{m-1}, …, ρ_0 -/
def elsL {E : SObj} (ρ : Nat → (SynCat T).Hom E U1) (m : Nat) {d : D.Ob} (e : ((F.obj E).1.obj d).car) :
    List (((F.obj U1).1.obj d).car) :=
  lmapG (fun i => (F.map (ρ i)).app d e) (rangeG m)

theorem els_nat {E : SObj} (ρ : Nat → (SynCat T).Hom E U1) {d d' : D.Ob} (h : D.Hom d' d)
    (e : ((F.obj E).1.obj d).car) : (l : List Nat) →
    LEq (F.obj U1).1 d' (lmapG (fun i => (F.map (ρ i)).app d' ((F.obj E).1.map h e)) l)
      (lmapG ((F.obj U1).1.map h) (lmapG (fun i => (F.map (ρ i)).app d e) l))
  | [] => trivial
  | i :: l => ⟨(F.map (ρ i)).nat h e, els_nat ρ h e l⟩

def RelS (R : Nat) {c : D.Ob} (vs : List (((F.obj U1).1.obj c).car)) : Sieve D c where
  S := fun {d} g => ∃ (m : Nat) (E : SObj) (ρ : Nat → (SynCat T).Hom E U1) (e : ((F.obj E).1.obj d).car),
    m = lenG vs ∧ LEq (F.obj U1).1 d (elsL T K F ρ m e) (lmapG ((F.obj U1).1.map g) vs) ∧
    Sem T m E ρ (.atom R (rangeG m))
  resp := fun h ⟨m, E, ρ, e, hm, h1, hs⟩ => ⟨m, E, ρ, e, hm, leq_trans _ _ _ _ h1 (leq_resp _ h vs), hs⟩
  stable := fun {_ _} {g} ⟨m, E, ρ, e, hm, h1, hs⟩ h => ⟨m, E, ρ, (F.obj E).1.map h e, hm,
    leq_trans _ _ _ _ (els_nat T K F ρ h e _) (leq_trans _ _ _ _ (leq_lmap _ h _ _ h1) (leq_comp _ g h vs)), hs⟩

def relF (R : Nat) (c : D.Ob) (vs : List (((F.obj U1).1.obj c).car)) : Prop := K.cov (RelS T K F R vs)

/-- the structure N_F -/
def NFStr : @GStr D where
  P := (F.obj U1).1
  rel := relF T K F
  rel_resp := fun _ _ l l' hl h => cov_mono K h (fun _ g ⟨m, E, ρ, x, hm, h1, hs⟩ =>
    ⟨m, E, ρ, x, hm.trans (leq_len _ l l' hl), leq_trans _ _ _ _ h1 (leq_lmap _ g l l' hl), hs⟩)
  rel_stable := fun _ {_ _} f l h => cov_mono K (K.pull f h) (fun _ g ⟨m, E, ρ, x, hm, h1, hs⟩ =>
    ⟨m, E, ρ, x, hm.trans (len_lmapG _ l).symm, leq_trans _ _ _ _ h1 (leq_symm _ _ _ (leq_comp _ f g l)), hs⟩)

theorem nf_closed : Closed K (NFStr T K F) where
  sep := sheaf_sep K _ (F.obj U1).2
  rel_loc := fun _ _ l _ hS h => K.trans hS (fun _ f hf => cov_mono K (h _ f hf) (fun _ g ⟨m, E, ρ, x, hm, h1, hs⟩ =>
    ⟨m, E, ρ, x, hm.trans (len_lmapG _ l), leq_trans _ _ _ _ h1 (leq_comp _ f g l), hs⟩))

end Gt

end Found

#print axioms Found.Gt.RelS
#print axioms Found.Gt.els_nat
#print axioms Found.Gt.NFStr
#print axioms Found.Gt.nf_closed

found/Gt56.lean

lean_all/found/Gt56.lean

/-
  T194 (classifying topos), part 56. found/Gt56 — tools for the key lemma
  of N_F: representations restrict along stages; the sieve of stages where
  e factors through a generalized element satisfying φ; for a flat F,
  finitely many pairs of arrows agreeing on e are locally equalized by one
  arrow.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt55

namespace Found

namespace Gt

open Sh

variable (T : Seq → Prop) {D : ECat.{0, 0}} (K : GTop D) (F : EFun (SynCat T) (ShCat K))

theorem rep_res {k : Nat} {c d : D.Ob} (g : D.Hom d c) {v : Nat → ((F.obj U1).1.obj c).car} {E : SObj}
    {ρ : Nat → (SynCat T).Hom E U1} {e : ((F.obj E).1.obj c).car} (r : RepAt T K F k v ρ e) :
    RepAt T K F k (fun i => (F.obj U1).1.map g (v i)) ρ ((F.obj E).1.map g e) := fun i hi =>
  ((F.obj U1).1.obj d).trans ((F.map (ρ i)).nat g e) ((F.obj U1).1.map_eqv g (r i hi))

/-- F of a composite, applied -/
theorem fcomp {A B C' : SObj} (u : (SynCat T).Hom A B) (w : (SynCat T).Hom B C') {d : D.Ob}
    (x : ((F.obj A).1.obj d).car) :
    ((F.obj C').1.obj d).eqv ((F.map ((SynCat T).comp u w)).app d x) ((F.map w).app d ((F.map u).app d x)) :=
  F.map_comp u w d x

/-- stages where e factors through a generalized element on which φ holds -/
def FacS (k : Nat) (φ : GFm) {E : SObj} (ρ : Nat → (SynCat T).Hom E U1) {c : D.Ob} (e : ((F.obj E).1.obj c).car) :
    Sieve D c where
  S := fun {d} g => ∃ (E' : SObj) (w : (SynCat T).Hom E' E) (e' : ((F.obj E').1.obj d).car),
    ((F.obj E).1.obj d).eqv ((F.map w).app d e') ((F.obj E).1.map g e) ∧ Sem T k E' (fun i => (SynCat T).comp w (ρ i)) φ
  resp := fun h ⟨E', w, e', h1, hs⟩ => ⟨E', w, e', ((F.obj E).1.obj _).trans h1 ((F.obj E).1.map_resp e h), hs⟩
  stable := fun {_ _} {g} ⟨E', w, e', h1, hs⟩ h => ⟨E', w, (F.obj E').1.map h e',
    ((F.obj E).1.obj _).trans ((F.map w).nat h e') (((F.obj E).1.obj _).trans ((F.obj E).1.map_eqv h h1)
      (((F.obj E).1.obj _).symm ((F.obj E).1.map_comp h g e))), hs⟩

/-- stages where e factors through an arrow equalizing the listed pairs -/
def EqzL (ps : List (Nat × Nat)) {E : SObj} (a b : Nat → (SynCat T).Hom E U1) {c : D.Ob}
    (e : ((F.obj E).1.obj c).car) : Sieve D c where
  S := fun {d} g => ∃ (E' : SObj) (w : (SynCat T).Hom E' E) (e' : ((F.obj E').1.obj d).car),
    ((F.obj E).1.obj d).eqv ((F.map w).app d e') ((F.obj E).1.map g e) ∧
    ∀ p, p ∈ ps → (SynCat T).heq ((SynCat T).comp w (a p.1)) ((SynCat T).comp w (b p.2))
  resp := fun h ⟨E', w, e', h1, hs⟩ => ⟨E', w, e', ((F.obj E).1.obj _).trans h1 ((F.obj E).1.map_resp e h), hs⟩
  stable := fun {_ _} {g} ⟨E', w, e', h1, hs⟩ h => ⟨E', w, (F.obj E').1.map h e',
    ((F.obj E).1.obj _).trans ((F.map w).nat h e') (((F.obj E).1.obj _).trans ((F.obj E).1.map_eqv h h1)
      (((F.obj E).1.obj _).symm ((F.obj E).1.map_comp h g e))), hs⟩

theorem eqz_pairs (hF : FlatF K F) : (ps : List (Nat × Nat)) → ∀ {E : SObj} (a b : Nat → (SynCat T).Hom E U1)
    {c : D.Ob} (e : ((F.obj E).1.obj c).car),
    (∀ p, p ∈ ps → ((F.obj U1).1.obj c).eqv ((F.map (a p.1)).app c e) ((F.map (b p.2)).app c e)) →
    K.cov (EqzL T K F ps a b e)
  | [], E, _, _, c, e, _ => cov_mono K (K.top c) (fun d g _ =>
      ⟨E, (SynCat T).id E, (F.obj E).1.map g e, F.map_id E d _, fun _ m => nomatch m⟩)
  | p :: ps, E, a, b, c, e, h => by
    let C := SynCat T
    let PU := (F.obj U1).1
    refine K.trans (hF.eqz (a p.1) (b p.2) e (h p (.head _))) (fun d g ⟨E1, w1, e1, hw1, h1⟩ => ?_)
    have h' : ∀ q, q ∈ ps → (PU.obj d).eqv ((F.map (C.comp w1 (a q.1))).app d e1) ((F.map (C.comp w1 (b q.2))).app d e1) :=
      fun q hq => (PU.obj d).trans (fcomp T K F w1 (a q.1) e1) ((PU.obj d).trans
        ((F.map (a q.1)).app_eqv d h1) ((PU.obj d).trans ((F.map (a q.1)).nat g e) ((PU.obj d).trans
          (PU.map_eqv g (h q (.tail _ hq))) ((PU.obj d).trans ((PU.obj d).symm ((F.map (b q.2)).nat g e))
            ((PU.obj d).trans ((F.map (b q.2)).app_eqv d (((F.obj E).1.obj d).symm h1))
              ((PU.obj d).symm (fcomp T K F w1 (b q.2) e1)))))))
    refine cov_mono K (eqz_pairs hF ps (fun i => C.comp w1 (a i)) (fun i => C.comp w1 (b i)) e1 h')
      (fun d' g' ⟨E2, w2, e2, hw2, h2⟩ => ⟨E2, C.comp w2 w1, e2, ?_, fun q hq => ?_⟩)
    · exact ((F.obj E).1.obj d').trans (fcomp T K F w2 w1 e2) (((F.obj E).1.obj d').trans
        ((F.map w1).app_eqv d' hw2) (((F.obj E).1.obj d').trans ((F.map w1).nat g' e1) (((F.obj E).1.obj d').trans
          ((F.obj E).1.map_eqv g' h1) (((F.obj E).1.obj d').symm ((F.obj E).1.map_comp g' g e)))))
    · cases hq with
      | head => exact C.htrans (C.assoc w2 w1 (a p.1)) (C.htrans (C.comp_resp (C.hrefl w2) hw1)
          (C.hsymm (C.assoc w2 w1 (b p.2))))
      | tail _ hq' => exact C.htrans (C.assoc w2 w1 (a q.1)) (C.htrans (h2 q hq')
          (C.hsymm (C.assoc w2 w1 (b q.2))))

end Gt

end Found

#print axioms Found.Gt.rep_res
#print axioms Found.Gt.FacS
#print axioms Found.Gt.eqz_pairs

found/Gt57.lean

lean_all/found/Gt57.lean

/-
  T194 (classifying topos), part 57. found/Gt57 — the key lemma for N_F,
  first cases: for a representation e of the values v (through ρ),
  N_F forces φ at v iff, locally, e factors through a generalized element
  on which φ holds. Cases ⊤, equality and conjunction (flatness).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt56

namespace Found

namespace Gt

open Sh

variable (T : Seq → Prop) {D : ECat.{0, 0}} (K : GTop D) (F : EFun (SynCat T) (ShCat K))

/-- the key lemma's statement for φ in k variables -/
def KLst (φ : GFm) (k : Nat) : Prop :=
  ∀ (c : D.Ob) (v : Nat → ((F.obj U1).1.obj c).car) (E : SObj) (ρ : Nat → (SynCat T).Hom E U1)
    (e : ((F.obj E).1.obj c).car), RepAt T K F k v ρ e →
    (force K (NFStr T K F) c v φ ↔ K.cov (FacS T K F k φ ρ e))

/-- chaining two factorizations -/
theorem fac_comp {E E1 E3 : SObj} (w1 : (SynCat T).Hom E1 E) (u : (SynCat T).Hom E3 E1) {c d d' : D.Ob}
    (g : D.Hom d c) (g' : D.Hom d' d) {e : ((F.obj E).1.obj c).car} {e1 : ((F.obj E1).1.obj d).car}
    {e3 : ((F.obj E3).1.obj d').car} (h1 : ((F.obj E).1.obj d).eqv ((F.map w1).app d e1) ((F.obj E).1.map g e))
    (h3 : ((F.obj E1).1.obj d').eqv ((F.map u).app d' e3) ((F.obj E1).1.map g' e1)) :
    ((F.obj E).1.obj d').eqv ((F.map ((SynCat T).comp u w1)).app d' e3) ((F.obj E).1.map (D.comp g' g) e) :=
  ((F.obj E).1.obj d').trans (fcomp T K F u w1 e3) (((F.obj E).1.obj d').trans ((F.map w1).app_eqv d' h3)
    (((F.obj E).1.obj d').trans ((F.map w1).nat g' e1) (((F.obj E).1.obj d').trans ((F.obj E).1.map_eqv g' h1)
      (((F.obj E).1.obj d').symm ((F.obj E).1.map_comp g' g e)))))

theorem kl_top (k : Nat) : KLst T K F .top k := fun c _ E _ e _ =>
  ⟨fun _ => cov_mono K (K.top c) (fun d g _ => ⟨E, (SynCat T).id E, (F.obj E).1.map g e, F.map_id E d _,
    fun _ _ _ _ _ _ => trivial⟩), fun _ => trivial⟩

/-- equal arrows give Sem of an equality -/
theorem sem_eq_of_heq {k i j : Nat} (hi : i < k) (hj : j < k) {E : SObj} (ρ : Nat → (SynCat T).Hom E U1)
    (h : ArrEq T U1 (ρ i).1 (ρ j).1) : Sem T k E ρ (.eq i j) := fun N c σ v _ hv =>
  (ρ j).2.2.2.2 N c σ [v i] [v j] rfl rfl ((h N c σ [v i] rfl).1 (hv i hi)) (hv j hj) 0 Nat.zero_lt_one

/-- Sem of an equality makes the arrows equal -/
theorem heq_of_sem_eq {k i j : Nat} (hi : i < k) (hj : j < k) {E : SObj} (ρ : Nat → (SynCat T).Hom E U1)
    (h : Sem T k E ρ (.eq i j)) : ArrEq T U1 (ρ i).1 (ρ j).1 := fun N _ σ ys hl =>
  match ys, hl with
  | [y], _ =>
    have h' : Sem T k E ρ (.eq j i) := fun N c σ v hφ hv => (N.M.P.obj c).symm (h N c σ v hφ hv)
    ⟨tl_eq_dir T k i j hi hj E ρ h N σ y, tl_eq_dir T k j i hj hi E ρ h' N σ y⟩
  | [], hl => Nat.noConfusion hl
  | _ :: _ :: _, hl => Nat.noConfusion (Nat.succ.inj hl)

theorem kl_eq (hF : FlatF K F) (k i j : Nat) (hi : i < k) (hj : j < k) : KLst T K F (.eq i j) k := by
  intro c v E ρ e r
  let PU := (F.obj U1).1
  constructor
  · intro h
    have he : (PU.obj c).eqv ((F.map (ρ i)).app c e) ((F.map (ρ j)).app c e) :=
      (PU.obj c).trans (r i hi) ((PU.obj c).trans h ((PU.obj c).symm (r j hj)))
    exact cov_mono K (hF.eqz (ρ i) (ρ j) e he) (fun _ _ ⟨E', w, z, hw, hz⟩ =>
      ⟨E', w, z, hz, sem_eq_of_heq T hi hj _ (fun N c σ ys hl =>
        ((SynCat T).htrans ((SynCat T).htrans (show (SynCat T).heq ((SynCat T).comp w (ρ i)) ((SynCat T).comp w (ρ i))
          from (SynCat T).hrefl _) hw) ((SynCat T).hrefl _)) N c σ ys hl)⟩)
  · intro h
    refine (nf_closed T K F).sep c _ h (v i) (v j) (fun d g ⟨E', w, e', h1, hs⟩ => ?_)
    have ae := heq_of_sem_eq T hi hj _ hs
    have q : (PU.obj d).eqv ((F.map ((SynCat T).comp w (ρ i))).app d e') ((F.map ((SynCat T).comp w (ρ j))).app d e') :=
      F.map_resp (a := E') (b := U1) (f := (SynCat T).comp w (ρ i)) (g := (SynCat T).comp w (ρ j)) ae d e'
    have qi : (PU.obj d).eqv ((F.map ((SynCat T).comp w (ρ i))).app d e') (PU.map g (v i)) :=
      (PU.obj d).trans (fcomp T K F w (ρ i) e') ((PU.obj d).trans ((F.map (ρ i)).app_eqv d h1)
        ((PU.obj d).trans ((F.map (ρ i)).nat g e) (PU.map_eqv g (r i hi))))
    have qj : (PU.obj d).eqv ((F.map ((SynCat T).comp w (ρ j))).app d e') (PU.map g (v j)) :=
      (PU.obj d).trans (fcomp T K F w (ρ j) e') ((PU.obj d).trans ((F.map (ρ j)).app_eqv d h1)
        ((PU.obj d).trans ((F.map (ρ j)).nat g e) (PU.map_eqv g (r j hj))))
    exact (PU.obj d).trans ((PU.obj d).symm qi) ((PU.obj d).trans q qj)

/-- two factorizations of e have a common refinement -/
theorem fac_meet (hF : FlatF K F) (k : Nat) {a b : GFm} {E : SObj} (ρ : Nat → (SynCat T).Hom E U1)
    {c : D.Ob} (e : ((F.obj E).1.obj c).car) (ha : K.cov (FacS T K F k a ρ e)) (hb : K.cov (FacS T K F k b ρ e)) :
    K.cov (FacS T K F k (.conj a b) ρ e) := by
  let C := SynCat T
  refine K.trans (cov_meet K ha hb) (fun d g ⟨⟨E1, w1, e1, h1, s1⟩, ⟨E2, w2, e2, h2, s2⟩⟩ => ?_)
  refine K.trans (hF.pair e1 e2) (fun d' g' ⟨E3, u1, u2, e3, k1, k2⟩ => ?_)
  have f1 := fac_comp T K F w1 u1 g g' h1 k1
  have f2 := fac_comp T K F w2 u2 g g' h2 k2
  refine cov_mono K (hF.eqz (C.comp u1 w1) (C.comp u2 w2) e3 (((F.obj E).1.obj d').trans f1 (((F.obj E).1.obj d').symm f2)))
    (fun d'' g'' ⟨E4, w4, e4, hw4, k4⟩ => ⟨E4, C.comp w4 (C.comp u1 w1), e4, ?_, ?_⟩)
  · have q := ((F.obj E).1.obj d'').trans (fcomp T K F w4 (C.comp u1 w1) e4) (((F.obj E).1.obj d'').trans
      ((F.map (C.comp u1 w1)).app_eqv d'' k4) (((F.obj E).1.obj d'').trans ((F.map (C.comp u1 w1)).nat g'' e3)
        (((F.obj E).1.obj d'').trans ((F.obj E).1.map_eqv g'' f1)
          (((F.obj E).1.obj d'').symm ((F.obj E).1.map_comp g'' (D.comp g' g) e)))))
    exact ((F.obj E).1.obj d'').trans q ((F.obj E).1.map_resp e (D.hsymm (D.assoc g'' g' g)))
  · have sa : Sem T k E4 (fun i => C.comp (C.comp w4 u1) (C.comp w1 (ρ i))) a := sem_pre T (C.comp w4 u1) _ s1
    have sb : Sem T k E4 (fun i => C.comp (C.comp w4 u2) (C.comp w2 (ρ i))) b := sem_pre T (C.comp w4 u2) _ s2
    refine fun N c σ v hE hv => ⟨sa N c σ v hE (fun i hi => ?_), sb N c σ v hE (fun i hi => ?_)⟩
    · exact (C.htrans (C.assoc w4 (C.comp u1 w1) (ρ i)) (C.htrans (C.comp_resp (C.hrefl w4) (C.assoc u1 w1 (ρ i)))
        (C.hsymm (C.assoc w4 u1 (C.comp w1 (ρ i)))))
        N c σ [v i] rfl).1 (hv i hi)
    · exact (C.htrans (C.comp_resp hw4 (C.hrefl (ρ i))) (C.htrans (C.assoc w4 (C.comp u2 w2) (ρ i))
        (C.htrans (C.comp_resp (C.hrefl w4) (C.assoc u2 w2 (ρ i)))
          (C.hsymm (C.assoc w4 u2 (C.comp w2 (ρ i))))))
        N c σ [v i] rfl).1 (hv i hi)

theorem kl_conj (hF : FlatF K F) (k : Nat) (a b : GFm) (ha : KLst T K F a k) (hb : KLst T K F b k) :
    KLst T K F (.conj a b) k := fun c v E ρ e r =>
  ⟨fun ⟨h1, h2⟩ => fac_meet T K F hF k ρ e ((ha c v E ρ e r).1 h1) ((hb c v E ρ e r).1 h2),
   fun h => ⟨(ha c v E ρ e r).2 (cov_mono K h (fun _ _ ⟨E', w, e', h1, s⟩ =>
      ⟨E', w, e', h1, fun N c σ v hE hv => (s N c σ v hE hv).1⟩)),
    (hb c v E ρ e r).2 (cov_mono K h (fun _ _ ⟨E', w, e', h1, s⟩ =>
      ⟨E', w, e', h1, fun N c σ v hE hv => (s N c σ v hE hv).2⟩))⟩⟩

end Gt

end Found

#print axioms Found.Gt.fac_comp
#print axioms Found.Gt.kl_top
#print axioms Found.Gt.heq_of_sem_eq
#print axioms Found.Gt.kl_eq
#print axioms Found.Gt.fac_meet
#print axioms Found.Gt.kl_conj

found/Gt58.lean

lean_all/found/Gt58.lean

/-
  T194 (classifying topos), part 58. found/Gt58 — reindexing lists for the
  atomic case: revIdx l j is the j-th entry of l counted from the end, so
  mapping over rangeG (length l) through revIdx is mapping over l; list
  equivalence and membership.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt57

namespace Found

namespace Gt

open Sh

def pickR (n a r j : Nat) : Nat :=
  match Nat.decEq j n with
  | isTrue _ => a
  | isFalse _ => r

def revIdx : List Nat → Nat → Nat
  | [], _ => 0
  | a :: l, j => pickR (lenG l) a (revIdx l j) j

theorem revIdx_top (a : Nat) (l : List Nat) : revIdx (a :: l) (lenG l) = a := by
  show pickR (lenG l) a (revIdx l (lenG l)) (lenG l) = a
  unfold pickR
  match Nat.decEq (lenG l) (lenG l) with
  | isTrue _ => rfl
  | isFalse h => exact absurd rfl h

theorem revIdx_ne (a : Nat) (l : List Nat) {j : Nat} (h : j ≠ lenG l) : revIdx (a :: l) j = revIdx l j := by
  show pickR (lenG l) a (revIdx l j) j = revIdx l j
  unfold pickR
  match Nat.decEq j (lenG l) with
  | isTrue e => exact absurd e h
  | isFalse _ => rfl

theorem lmapG_congr {X Y : Type} {f g : X → Y} : (l : List X) → (∀ x, x ∈ l → f x = g x) → lmapG f l = lmapG g l
  | [], _ => rfl
  | x :: l, h => by
    show f x :: lmapG f l = g x :: lmapG g l
    rw [h x (.head _), lmapG_congr l (fun y hy => h y (.tail _ hy))]

theorem rev_lmap {Y : Type} (f : Nat → Y) : (l : List Nat) →
    lmapG (fun j => f (revIdx l j)) (rangeG (lenG l)) = lmapG f l
  | [] => rfl
  | a :: l => by
    show f (revIdx (a :: l) (lenG l)) :: lmapG (fun j => f (revIdx (a :: l) j)) (rangeG (lenG l)) = f a :: lmapG f l
    rw [revIdx_top, lmapG_congr (f := fun j => f (revIdx (a :: l) j)) (g := fun j => f (revIdx l j)) _
      (fun j hj => by rw [revIdx_ne a l (Nat.ne_of_lt (mem_rangeG _ j hj))]), rev_lmap f l]

theorem revIdx_mem : (l : List Nat) → ∀ j, j < lenG l → revIdx l j ∈ l
  | [], _, h => absurd h (Nat.not_lt_zero _)
  | a :: l, j, h => match Nat.decEq j (lenG l) with
    | isTrue e => by rw [e, revIdx_top]; exact .head _
    | isFalse ne => by
      rw [revIdx_ne a l ne]
      exact .tail _ (revIdx_mem l j (Nat.lt_of_le_of_ne (Nat.le_of_lt_succ h) ne))

theorem allLt_mem {k : Nat} : (l : List Nat) → AllLtG k l → ∀ x, x ∈ l → x < k
  | _ :: _, h, _, .head _ => h.1
  | _ :: l, h, x, .tail _ m => allLt_mem l h.2 x m

theorem lmap_lmap {X Y Z : Type} (f : X → Y) (g : Y → Z) : (l : List X) →
    lmapG g (lmapG f l) = lmapG (fun x => g (f x)) l
  | [] => rfl
  | x :: l => by show g (f x) :: lmapG g (lmapG f l) = g (f x) :: _; rw [lmap_lmap f g l]

section L

variable {C : ECat.{0, 0}} (P : Psh.{0, 0, 0} C)

theorem leq_mem {X : Type} {c : C.Ob} (f f' : X → (P.obj c).car) : (l : List X) →
    LEq P c (lmapG f l) (lmapG f' l) → ∀ x, x ∈ l → (P.obj c).eqv (f x) (f' x)
  | _ :: _, h, _, .head _ => h.1
  | _ :: l, h, x, .tail _ m => leq_mem f f' l h.2 x m

theorem leq_of_mem {X : Type} {c : C.Ob} (f f' : X → (P.obj c).car) : (l : List X) →
    (∀ x, x ∈ l → (P.obj c).eqv (f x) (f' x)) → LEq P c (lmapG f l) (lmapG f' l)
  | [], _ => trivial
  | x :: l, h => ⟨h x (.head _), leq_of_mem f f' l (fun y hy => h y (.tail _ hy))⟩

end L

variable (T : Seq → Prop) {D : ECat.{0, 0}} (K : GTop D) (F : EFun (SynCat T) (ShCat K))

theorem kl_atom_r (k R : Nat) (l : List Nat) (hl : AllLtG k l) {c : D.Ob} (v : Nat → ((F.obj U1).1.obj c).car)
    {E : SObj} (ρ : Nat → (SynCat T).Hom E U1) (e : ((F.obj E).1.obj c).car) (r : RepAt T K F k v ρ e)
    (h : K.cov (FacS T K F k (.atom R l) ρ e)) : force K (NFStr T K F) c v (.atom R l) := by
  let PU := (F.obj U1).1
  let C := SynCat T
  show K.cov (RelS T K F R (lmapG v l))
  refine cov_mono K h (fun d g ⟨E', w, e', h1, s⟩ => ⟨lenG l, E', fun j => C.comp w (ρ (revIdx l j)), e',
    (len_lmapG v l).symm, ?_, ?_⟩)
  · have eq1 : elsL T K F (fun j => C.comp w (ρ (revIdx l j))) (lenG l) e' =
        lmapG (fun i => (F.map (C.comp w (ρ i))).app d e') l := rev_lmap (fun i => (F.map (C.comp w (ρ i))).app d e') l
    rw [eq1, lmap_lmap]
    exact leq_of_mem PU _ _ l (fun x hx => (PU.obj d).trans (fcomp T K F w (ρ x) e') ((PU.obj d).trans
      ((F.map (ρ x)).app_eqv d h1) ((PU.obj d).trans ((F.map (ρ x)).nat g e)
        (PU.map_eqv g (r x (allLt_mem l hl x hx))))))
  · intro N c0 σ v2 hE hv2
    refine N.closed.rel_loc R c0 _ _ (cov_tot T N (fun i => C.comp w (ρ i)) σ hE k) (fun d0 g0 ⟨w0, hw0⟩ => ?_)
    have hr := s N d0 (resV N.M g0 σ) w0 (force_stable N.K N.M _ g0 σ hE) hw0
    refine N.M.rel_resp R d0 _ _ ?_ hr
    show LEq N.M.P d0 (lmapG w0 l) (lmapG (N.M.P.map g0) (lmapG v2 (rangeG (lenG l))))
    rw [← rev_lmap w0 l, lmap_lmap]
    refine leq_of_mem N.M.P _ _ _ (fun j hj => ?_)
    have hjl := mem_rangeG _ j hj
    have hx := allLt_mem l hl _ (revIdx_mem l j hjl)
    have f2 := force_resp N.K N.M _ (ext_res N.M g0 σ (v2 j)) (force_stable N.K N.M _ g0 _ (hv2 j hjl))
    exact (C.comp w (ρ (revIdx l j))).2.2.2.2 N d0 (resV N.M g0 σ) [_] [_] rfl rfl (hw0 _ hx) f2 0 Nat.zero_lt_one

theorem kl_atom_l (hF : FlatF K F) (k R : Nat) (l : List Nat) (hl : AllLtG k l) {c : D.Ob}
    (v : Nat → ((F.obj U1).1.obj c).car) {E : SObj} (ρ : Nat → (SynCat T).Hom E U1) (e : ((F.obj E).1.obj c).car)
    (r : RepAt T K F k v ρ e) (h : force K (NFStr T K F) c v (.atom R l)) :
    K.cov (FacS T K F k (.atom R l) ρ e) := by
  let PU := (F.obj U1).1
  let C := SynCat T
  have h' : K.cov (RelS T K F R (lmapG v l)) := h
  refine K.trans h' (fun d g ⟨m, E2, ρ2, e2, hm, h1, s2⟩ => ?_)
  have hml : m = lenG l := hm.trans (len_lmapG v l)
  subst hml
  rw [lmap_lmap, ← rev_lmap (fun i => PU.map g (v i)) l] at h1
  have hp := leq_mem PU _ _ _ h1
  refine K.trans (hF.pair ((F.obj E).1.map g e) e2) (fun d' g' ⟨E3, u, u2, e3, k1, k2⟩ => ?_)
  let a : Nat → C.Hom E3 U1 := fun j => C.comp u2 (ρ2 j)
  let b : Nat → C.Hom E3 U1 := fun j => C.comp u (ρ (revIdx l j))
  let ps := lmapG (fun j => (j, j)) (rangeG (lenG l))
  have hps : ∀ p, p ∈ ps → (PU.obj d').eqv ((F.map (a p.1)).app d' e3) ((F.map (b p.2)).app d' e3) := by
    intro p hp'
    match mem_lmapG _ hp' with
    | ⟨j, hj, eq⟩ =>
    subst eq
    have hx := allLt_mem l hl _ (revIdx_mem l j (mem_rangeG _ j hj))
    exact (PU.obj d').trans (fcomp T K F u2 (ρ2 j) e3) ((PU.obj d').trans ((F.map (ρ2 j)).app_eqv d' k2)
      ((PU.obj d').trans ((F.map (ρ2 j)).nat g' e2) ((PU.obj d').trans (PU.map_eqv g' (hp j hj))
        ((PU.obj d').symm ((PU.obj d').trans (fcomp T K F u (ρ (revIdx l j)) e3) ((PU.obj d').trans
          ((F.map (ρ (revIdx l j))).app_eqv d' k1) ((PU.obj d').trans ((F.map (ρ (revIdx l j))).nat g' _)
            (PU.map_eqv g' ((PU.obj d).trans ((F.map (ρ (revIdx l j))).nat g e) (PU.map_eqv g (r _ hx)))))))))))
  refine cov_mono K (eqz_pairs T K F hF ps a b e3 hps) (fun d'' g'' ⟨E4, w4, e4, h4, hq⟩ =>
    ⟨E4, C.comp w4 u, e4, ?_, ?_⟩)
  · have k1' : ((F.obj E).1.obj d').eqv ((F.map u).app d' e3) ((F.obj E).1.map (D.comp g' g) e) :=
      ((F.obj E).1.obj d').trans k1 (((F.obj E).1.obj d').symm ((F.obj E).1.map_comp g' g e))
    exact ((F.obj E).1.obj d'').trans (fac_comp T K F u w4 (D.comp g' g) g'' k1' h4)
      ((F.obj E).1.map_resp e (D.hsymm (D.assoc g'' g' g)))
  · have s3 := sem_pre T (C.comp w4 u2) ρ2 s2
    intro N c0 σ v' hE hv'
    have q := s3 N c0 σ (fun j => v' (revIdx l j)) hE (fun j hj => by
      have hx := allLt_mem l hl _ (revIdx_mem l j hj)
      have hpj : (j, j) ∈ ps := lmapG_mem (fun j => (j, j)) (rangeG (lenG l)) (rangeG_mem _ j hj)
      exact (C.htrans (C.assoc w4 u (ρ (revIdx l j))) (C.htrans (C.hsymm (hq (j, j) hpj))
        (C.hsymm (C.assoc w4 u2 (ρ2 j)))) N c0 σ [_] rfl).1 (hv' _ hx))
    have e2' : lmapG (fun j => v' (revIdx l j)) (rangeG (lenG l)) = lmapG v' l := rev_lmap v' l
    show N.M.rel R c0 (lmapG v' l)
    rw [← e2']
    exact q

theorem kl_atom (hF : FlatF K F) (k R : Nat) (l : List Nat) (hl : AllLtG k l) : KLst T K F (.atom R l) k :=
  fun _ v _ ρ e r => ⟨kl_atom_l T K F hF k R l hl v ρ e r, kl_atom_r T K F k R l hl v ρ e r⟩

end Gt

end Found

#print axioms Found.Gt.rev_lmap
#print axioms Found.Gt.revIdx_mem
#print axioms Found.Gt.leq_mem
#print axioms Found.Gt.leq_of_mem
#print axioms Found.Gt.kl_atom

found/Gt59.lean

lean_all/found/Gt59.lean

/-
  T194 (classifying topos), part 59. found/Gt59 — the key lemma for N_F,
  countable disjunctions: a generalized element on which ⋁ φ_n holds is
  covered (in J_T) by the image subobjects of the φ_n, and continuity of F
  brings the cover down to the stages.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt58

namespace Found

namespace Gt

open Sh

variable (T : Seq → Prop)

/-- the arrows into A along which some disjunct holds -/
def SupSv {A : SObj} (ρ : Nat → (SynCat T).Hom A U1) (k : Nat) (φs : Nat → GFm) : Sieve (SynCat T) A where
  S := fun {B} u => ∃ n, Sem T k B (fun i => (SynCat T).comp u (ρ i)) (φs n)
  resp := fun {_} {_ _} e ⟨n, s⟩ => ⟨n, sem_heq T (fun i _ => (SynCat T).comp_resp e ((SynCat T).hrefl (ρ i))) s⟩
  stable := fun {_ _} {u} ⟨n, s⟩ g => ⟨n, sem_heq T (fun i _ => (SynCat T).hsymm ((SynCat T).assoc g u (ρ i)))
    (sem_pre T g _ s)⟩

theorem sup_hit_cov {A : SObj} (ρ : Nat → (SynCat T).Hom A U1) (k : Nat) (φs : Nat → GFm) (hφs : ∀ n, FVg k (φs n))
    (h : Sem T k A ρ (.sup φs)) :
    ∀ (N : SModel T) c (σ : Nat → (N.M.P.obj c).car), force N.K N.M c σ A.φ →
      N.K.cov (hitF T N (imgFam T ρ k φs hφs) σ) := by
  intro N c σ hφ
  refine N.K.trans (cov_tot T N ρ σ hφ k) (fun d g ⟨w, hw⟩ => ?_)
  match h N d (resV N.M g σ) w (force_stable N.K N.M A.φ g σ hφ) hw with
  | ⟨S', hS', hh'⟩ =>
  refine cov_mono N.K hS' (fun e hk hk' => ?_)
  match hh' e hk hk' with
  | ⟨n, hn⟩ =>
  have hB := img_hit T N k (φs n) (hφs n) ρ g hk σ hφ w hw hn
  exact ⟨n, resV N.M (N.D.comp hk g) σ, witL N.M (resV N.M (N.D.comp hk g) σ) A.n, len_witL N.M _ A.n,
    witL_agree N.M _ A.n, id_hits T N (subO A (imgF T ρ k (φs n)) (fv_imgF T ρ k (φs n) (hφs n))) _ hB⟩

theorem supSv_cov (Kc : CP.Classical') {A : SObj} (ρ : Nat → (SynCat T).Hom A U1) (k : Nat) (φs : Nat → GFm)
    (hφs : ∀ n, FVg k (φs n)) (h : Sem T k A ρ (.sup φs)) : (JTc T Kc).cov (SupSv T ρ k φs) :=
  ⟨imgFam T ρ k φs hφs, fun n => ⟨n, sem_img T k (φs n) (hφs n) A ρ⟩, sup_hit_cov T ρ k φs hφs h⟩

variable {D : ECat.{0, 0}} (K : GTop D) (F : EFun (SynCat T) (ShCat K))

theorem kl_sup (Kc : CP.Classical') (hC : ContF (JTc T Kc) K F) (k : Nat) (φs : Nat → GFm)
    (hφs : ∀ n, FVg k (φs n)) (IH : ∀ n, KLst T K F (φs n) k) : KLst T K F (.sup φs) k := by
  intro c v E ρ e r
  let C := SynCat T
  constructor
  · intro ⟨S, hS, hh⟩
    refine K.trans hS (fun d f hf => ?_)
    match hh d f hf with
    | ⟨n, hn⟩ =>
    exact cov_mono K ((IH n d _ E ρ _ (rep_res T K F f r)).1 hn) (fun _ g ⟨E', w, e', h1, s⟩ =>
      ⟨E', w, e', ((F.obj E).1.obj _).trans h1 (((F.obj E).1.obj _).symm ((F.obj E).1.map_comp g f e)),
        sem_sup T n s⟩)
  · intro h
    refine force_local K (NFStr T K F) (nf_closed T K F) _ v _ h (fun d g ⟨E', w, e', h1, s⟩ => ?_)
    refine ⟨HitS K F (SupSv T (fun i => C.comp w (ρ i)) k φs) e',
      hC E' _ (supSv_cov T Kc _ k φs hφs s) d e', fun d' g' ⟨B, u, ⟨n, sn⟩, y, hy⟩ => ⟨n, ?_⟩⟩
    have fac : ((F.obj E).1.obj d').eqv ((F.map (C.comp u w)).app d' y) ((F.obj E).1.map (D.comp g' g) e) :=
      fac_comp T K F w u g g' h1 hy
    have mem : (FacS T K F k (φs n) ρ ((F.obj E).1.map (D.comp g' g) e)).S (D.id d') :=
      ⟨B, C.comp u w, y, ((F.obj E).1.obj d').trans fac (((F.obj E).1.obj d').symm ((F.obj E).1.map_id d' _)),
        sem_heq T (fun i _ => C.hsymm (C.assoc u w (ρ i))) sn⟩
    have hcov : K.cov (FacS T K F k (φs n) ρ ((F.obj E).1.map (D.comp g' g) e)) :=
      cov_mono K (K.top d') (fun _ h' _ => (FacS T K F k (φs n) ρ _).resp (D.comp_id h')
        ((FacS T K F k (φs n) ρ _).stable mem h'))
    have f1 := (IH n d' _ E ρ _ (rep_res T K F (D.comp g' g) r)).2 hcov
    exact force_resp K (NFStr T K F) (φs n) (fun i => ((F.obj U1).1.obj d').symm (res_comp (NFStr T K F) g g' v i)) f1

end Gt

end Found

#print axioms Found.Gt.SupSv
#print axioms Found.Gt.supSv_cov
#print axioms Found.Gt.kl_sup

found/Gt6.lean

lean_all/found/Gt6.lean

/-
  T194 (classifying topos), part 6. found/Gt6 — (single-sorted, relational;
  many sorts reduce to unary sort predicates) geometric logic and its
  Kripke–Joyal semantics in presheaves on a site:
  * formulas: relation atoms, equality, ⊤, ∧, countable disjunctions (over
    ℕ, so that formulas form a small type), ∃ (de Bruijn variables);
  * a structure: a presheaf of setoids and, for each relation symbol, a
    predicate on lists of elements at each stage, stable under restriction
    and respecting the setoid;
  * forcing c ⊩ φ[ρ]: atoms, equality and ∧ pointwise, ⋁ and ∃ locally on
    a covering sieve. Forcing respects the setoid and is stable under
    restriction.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt5

namespace Found

namespace Gt

open Sh

inductive GFm : Type where
  | atom (R : Nat) (l : List Nat) : GFm
  | eq (i j : Nat) : GFm
  | top : GFm
  | conj : GFm → GFm → GFm
  | sup : (Nat → GFm) → GFm
  | ex : GFm → GFm

variable {C : ECat.{0, 0}} (J : GTop C)

def lmapG {X Y : Type} (f : X → Y) : List X → List Y
  | [] => []
  | x :: l => f x :: lmapG f l

/-- pointwise equivalence of lists -/
def LEq (P : Psh.{0, 0, 0} C) (c : C.Ob) : List (P.obj c).car → List (P.obj c).car → Prop
  | [], [] => True
  | x :: l, y :: l' => (P.obj c).eqv x y ∧ LEq P c l l'
  | [], _ :: _ => False
  | _ :: _, [] => False

/-- a structure in presheaves -/
structure GStr where
  P : Psh.{0, 0, 0} C
  rel : Nat → (c : C.Ob) → List (P.obj c).car → Prop
  rel_resp : ∀ R c (l l' : List (P.obj c).car), LEq P c l l' → rel R c l → rel R c l'
  rel_stable : ∀ R {c d} (f : C.Hom d c) (l : List (P.obj c).car), rel R c l → rel R d (lmapG (P.map f) l)

variable (M : @GStr C)

def resV {c d : C.Ob} (f : C.Hom d c) (ρ : Nat → (M.P.obj c).car) : Nat → (M.P.obj d).car := fun n => M.P.map f (ρ n)

def extV {c : C.Ob} (ρ : Nat → (M.P.obj c).car) (a : (M.P.obj c).car) : Nat → (M.P.obj c).car
  | 0 => a
  | n + 1 => ρ n

/-- Kripke–Joyal forcing -/
def force : (c : C.Ob) → (Nat → (M.P.obj c).car) → GFm → Prop
  | c, ρ, .atom R l => M.rel R c (lmapG ρ l)
  | c, ρ, .eq i j => (M.P.obj c).eqv (ρ i) (ρ j)
  | _, _, .top => True
  | c, ρ, .conj a b => force c ρ a ∧ force c ρ b
  | c, ρ, .sup φ => ∃ S : Sieve C c, J.cov S ∧ ∀ d (f : C.Hom d c), S.S f → ∃ i, force d (resV M f ρ) (φ i)
  | c, ρ, .ex φ => ∃ S : Sieve C c, J.cov S ∧ ∀ d (f : C.Hom d c), S.S f → ∃ a, force d (extV M (resV M f ρ) a) φ

theorem leq_map {c : C.Ob} {ρ ρ' : Nat → (M.P.obj c).car} (h : ∀ n, (M.P.obj c).eqv (ρ n) (ρ' n)) :
    (l : List Nat) → LEq M.P c (lmapG ρ l) (lmapG ρ' l)
  | [] => trivial
  | n :: l => ⟨h n, leq_map h l⟩

theorem extV_eqv {c : C.Ob} {ρ ρ' : Nat → (M.P.obj c).car} (h : ∀ n, (M.P.obj c).eqv (ρ n) (ρ' n))
    (a : (M.P.obj c).car) : ∀ n, (M.P.obj c).eqv (extV M ρ a n) (extV M ρ' a n)
  | 0 => (M.P.obj c).refl a
  | n + 1 => h n

/-- forcing respects the setoid -/
theorem force_resp : (φ : GFm) → {c : C.Ob} → {ρ ρ' : Nat → (M.P.obj c).car} →
    (∀ n, (M.P.obj c).eqv (ρ n) (ρ' n)) → force J M c ρ φ → force J M c ρ' φ
  | .atom R l, c, _, _, h, f => M.rel_resp R c _ _ (leq_map M h l) f
  | .eq i j, c, _, _, h, f => (M.P.obj c).trans ((M.P.obj c).symm (h i)) ((M.P.obj c).trans f (h j))
  | .top, _, _, _, _, f => f
  | .conj a b, _, _, _, h, f => ⟨force_resp a h f.1, force_resp b h f.2⟩
  | .sup φ, _, _, _, h, ⟨S, hS, g⟩ => ⟨S, hS, fun d f hf => match g d f hf with
      | ⟨i, fi⟩ => ⟨i, force_resp (φ i) (fun n => M.P.map_eqv f (h n)) fi⟩⟩
  | .ex φ, _, _, _, h, ⟨S, hS, g⟩ => ⟨S, hS, fun d f hf => match g d f hf with
      | ⟨a, fa⟩ => ⟨a, force_resp φ (extV_eqv M (fun n => M.P.map_eqv f (h n)) a) fa⟩⟩

theorem lmapG_comp {X Y Z : Type} (f : X → Y) (g : Y → Z) : (l : List X) → lmapG g (lmapG f l) = lmapG (fun x => g (f x)) l
  | [] => rfl
  | x :: l => congrArg (List.cons (g (f x))) (lmapG_comp f g l)

theorem res_comp {c d e : C.Ob} (f : C.Hom d c) (g : C.Hom e d) (ρ : Nat → (M.P.obj c).car) :
    ∀ n, (M.P.obj e).eqv (resV M g (resV M f ρ) n) (resV M (C.comp g f) ρ n) :=
  fun n => (M.P.obj e).symm (M.P.map_comp g f (ρ n))

theorem ext_res {c d : C.Ob} (f : C.Hom d c) (ρ : Nat → (M.P.obj c).car) (a : (M.P.obj c).car) :
    ∀ n, (M.P.obj d).eqv (resV M f (extV M ρ a) n) (extV M (resV M f ρ) (M.P.map f a) n)
  | 0 => (M.P.obj d).refl _
  | _ + 1 => (M.P.obj d).refl _

/-- forcing is stable under restriction -/
theorem force_stable : (φ : GFm) → {c d : C.Ob} → (f : C.Hom d c) → (ρ : Nat → (M.P.obj c).car) →
    force J M c ρ φ → force J M d (resV M f ρ) φ
  | .atom R l, _, _, f, ρ, h => by
    have h' := M.rel_stable R f _ h
    rw [lmapG_comp] at h'
    exact h'
  | .eq _ _, _, _, f, _, h => M.P.map_eqv f h
  | .top, _, _, _, _, h => h
  | .conj a b, _, _, f, ρ, h => ⟨force_stable a f ρ h.1, force_stable b f ρ h.2⟩
  | .sup φ, _, _, f, ρ, ⟨S, hS, g⟩ => ⟨pullS f S, J.pull f hS, fun e k hk => match g e (C.comp k f) hk with
      | ⟨i, fi⟩ => ⟨i, force_resp J M (φ i) (fun n => (M.P.obj e).symm (res_comp M f k ρ n)) fi⟩⟩
  | .ex φ, _, _, f, ρ, ⟨S, hS, g⟩ => ⟨pullS f S, J.pull f hS, fun e k hk => match g e (C.comp k f) hk with
      | ⟨a, fa⟩ => ⟨a, force_resp J M φ (extV_eqv M (fun n => (M.P.obj e).symm (res_comp M f k ρ n)) a) fa⟩⟩

end Gt

end Found

#print axioms Found.Gt.LEq
#print axioms Found.Gt.force
#print axioms Found.Gt.force_resp
#print axioms Found.Gt.force_stable

found/Gt60.lean

lean_all/found/Gt60.lean

/-
  T194 (classifying topos), part 60. found/Gt60 — the key lemma for N_F,
  existential quantification: a generalized element on which ∃y φ holds is
  covered (in J_T) by the projection from the witness object, and
  continuity of F brings the cover down to the stages; conversely a local
  witness is combined with the representation by flatness.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt59

namespace Found

namespace Gt

open Sh

variable (T : Seq → Prop)

/-- the arrows into A along which a witness exists -/
def ExSv {A : SObj} (ρ : Nat → (SynCat T).Hom A U1) (k : Nat) (φ : GFm) : Sieve (SynCat T) A where
  S := fun {B} u => ∃ a : (SynCat T).Hom B U1, Sem T (k + 1) B (extV (UStr T) (fun i => (SynCat T).comp u (ρ i)) a) φ
  resp := fun {_} {u _} e ⟨a, s⟩ => ⟨a, sem_heq T (ρ := extV (UStr T) (fun i => (SynCat T).comp u (ρ i)) a) (fun i _ => match i with
    | 0 => (SynCat T).hrefl a
    | j + 1 => (SynCat T).comp_resp e ((SynCat T).hrefl (ρ j))) s⟩
  stable := fun {_ _} {u} ⟨a, s⟩ g => ⟨(SynCat T).comp g a, sem_heq T
    (ρ := fun i => (SynCat T).comp g (extV (UStr T) (fun i => (SynCat T).comp u (ρ i)) a i)) (fun i _ => match i with
    | 0 => (SynCat T).hrefl _
    | j + 1 => (SynCat T).hsymm ((SynCat T).assoc g u (ρ j))) (sem_pre T g _ s)⟩

theorem ex_hit_cov (k : Nat) (φ : GFm) (hφ : FVg (k + 1) φ) {A : SObj} (ρ : Nat → (SynCat T).Hom A U1)
    (h : Sem T k A ρ (.ex φ)) :
    ∀ (N : SModel T) c (σ : Nat → (N.M.P.obj c).car), force N.K N.M c σ A.φ →
      N.K.cov (hitF T N (⟨fun _ => exO T ρ k φ hφ, fun _ => πA T ρ k φ hφ⟩ : CFam T A) σ) := by
  intro N c σ hφA
  refine N.K.trans (cov_tot T N ρ σ hφA k) (fun d g ⟨w, hw⟩ => ?_)
  match h N d (resV N.M g σ) w (force_stable N.K N.M A.φ g σ hφA) hw with
  | ⟨S', hS', hh'⟩ =>
  refine cov_mono N.K hS' (fun e hk hk' => ?_)
  match hh' e hk hk' with
  | ⟨y, hy⟩ =>
  let σ'' := resV N.M (N.D.comp hk g) σ
  have hE := exO_hit T N k φ hφ ρ g hk σ hφA w hw y hy
  have hl := len_witL N.M σ'' A.n
  refine ⟨0, extV N.M σ'' y, witL N.M σ'' A.n, hl, fun i hi => by
    rw [witL_val N.M σ'' A.n _ i hi]; exact (N.M.P.obj e).refl _, ?_⟩
  exact (force_projF N.K N.M (exO T ρ k φ hφ) A.n 1 _ _ hl).2 ⟨hE, fun i hi => by
    rw [witL_val N.M σ'' A.n _ i hi]; exact (N.M.P.obj e).refl _⟩

theorem exSv_cov (Kc : CP.Classical') (k : Nat) (φ : GFm) (hφ : FVg (k + 1) φ) {A : SObj}
    (ρ : Nat → (SynCat T).Hom A U1) (h : Sem T k A ρ (.ex φ)) : (JTc T Kc).cov (ExSv T ρ k φ) :=
  ⟨⟨fun _ => exO T ρ k φ hφ, fun _ => πA T ρ k φ hφ⟩, fun _ => ⟨πY T ρ k φ hφ, sem_exO T k φ hφ A ρ⟩,
   ex_hit_cov T k φ hφ ρ h⟩

variable {D : ECat.{0, 0}} (K : GTop D) (F : EFun (SynCat T) (ShCat K))

theorem kl_ex_l (hF : FlatF K F) (k : Nat) (φ : GFm) (IH : KLst T K F φ (k + 1)) {c : D.Ob}
    (v : Nat → ((F.obj U1).1.obj c).car) {E : SObj} (ρ : Nat → (SynCat T).Hom E U1) (e : ((F.obj E).1.obj c).car)
    (r : RepAt T K F k v ρ e) (h : force K (NFStr T K F) c v (.ex φ)) : K.cov (FacS T K F k (.ex φ) ρ e) := by
  let C := SynCat T
  let PU := (F.obj U1).1
  match h with
  | ⟨S, hS, hh⟩ =>
  refine K.trans hS (fun d f hf => ?_)
  match hh d f hf with
  | ⟨a, ha⟩ =>
  refine K.trans (hF.pair ((F.obj E).1.map f e) a) (fun d' g ⟨E3, u, wa, e3, k1, k2⟩ => ?_)
  let ρ' := extV (UStr T) (fun i => C.comp u (ρ i)) wa
  let v' := extV (NFStr T K F) (resV (NFStr T K F) g (resV (NFStr T K F) f v)) (PU.map g a)
  have rep' : RepAt T K F (k + 1) v' ρ' e3 := fun i hi => match i, hi with
    | 0, _ => k2
    | j + 1, hj => (PU.obj d').trans (fcomp T K F u (ρ j) e3) ((PU.obj d').trans ((F.map (ρ j)).app_eqv d' k1)
        ((PU.obj d').trans ((F.map (ρ j)).nat g _) (PU.map_eqv g ((PU.obj d).trans ((F.map (ρ j)).nat f e)
          (PU.map_eqv f (r j (Nat.lt_of_succ_lt_succ hj)))))))
  have hφ' : force K (NFStr T K F) d' v' φ :=
    force_resp K (NFStr T K F) φ (ext_res (NFStr T K F) g _ a) (force_stable K (NFStr T K F) φ g _ ha)
  refine cov_mono K ((IH d' v' E3 ρ' e3 rep').1 hφ') (fun d'' g'' ⟨E4, w4, e4, h4, s4⟩ =>
    ⟨E4, C.comp w4 u, e4, ?_, ?_⟩)
  · have k1' : ((F.obj E).1.obj d').eqv ((F.map u).app d' e3) ((F.obj E).1.map (D.comp g f) e) :=
      ((F.obj E).1.obj d').trans k1 (((F.obj E).1.obj d').symm ((F.obj E).1.map_comp g f e))
    exact ((F.obj E).1.obj d'').trans (fac_comp T K F u w4 (D.comp g f) g'' k1' h4)
      ((F.obj E).1.map_resp e (D.hsymm (D.assoc g'' g f)))
  · intro N c0 σ v'' hE hv
    have hT := (C.comp w4 wa).2.2.2.1 N c0 σ hE
    refine force_local N.K N.M N.closed _ v'' _ (exN_wit N.K N.M _ 1 σ hT) (fun d0 h0 ⟨ys, hyl, fy⟩ => ?_)
    match ys, hyl with
    | [], hyl => exact Nat.noConfusion hyl
    | _ :: _ :: _, hyl => exact Nat.noConfusion (Nat.succ.inj hyl)
    | [y], _ =>
    have q := s4 N d0 (resV N.M h0 σ) (extV N.M (resV N.M h0 v'') y) (force_stable N.K N.M _ h0 σ hE)
      (fun i hi => match i, hi with
        | 0, _ => fy
        | j + 1, hj => (C.htrans (C.assoc w4 u (ρ j)) (C.hrefl _) N d0 (resV N.M h0 σ) [_] rfl).1
            (force_resp N.K N.M _ (ext_res N.M h0 σ (v'' j))
              (force_stable N.K N.M _ h0 _ (hv j (Nat.lt_of_succ_lt_succ hj)))))
    exact ⟨maxS d0, N.K.top d0, fun _ h' _ => ⟨N.M.P.map h' y,
      force_resp N.K N.M φ (ext_res N.M h' _ y) (force_stable N.K N.M φ h' _ q)⟩⟩

theorem kl_ex_r (Kc : CP.Classical') (hC : ContF (JTc T Kc) K F) (k : Nat) (φ : GFm) (hφ : FVg (k + 1) φ)
    (IH : KLst T K F φ (k + 1)) {c : D.Ob} (v : Nat → ((F.obj U1).1.obj c).car) {E : SObj}
    (ρ : Nat → (SynCat T).Hom E U1) (e : ((F.obj E).1.obj c).car) (r : RepAt T K F k v ρ e)
    (h : K.cov (FacS T K F k (.ex φ) ρ e)) : force K (NFStr T K F) c v (.ex φ) := by
  let C := SynCat T
  let PU := (F.obj U1).1
  refine force_local K (NFStr T K F) (nf_closed T K F) _ v _ h (fun d g ⟨E', w, e', h1, s⟩ => ?_)
  refine ⟨HitS K F (ExSv T (fun i => C.comp w (ρ i)) k φ) e',
    hC E' _ (exSv_cov T Kc k φ hφ _ s) d e', fun d' g' ⟨B, u, ⟨a, sa⟩, y, hy⟩ => ⟨(F.map a).app d' y, ?_⟩⟩
  have fac := fac_comp T K F w u g g' h1 hy
  let ρ0 := extV (UStr T) (fun i => C.comp u (C.comp w (ρ i))) a
  let v0 := extV (NFStr T K F) (resV (NFStr T K F) (D.comp g' g) v) ((F.map a).app d' y)
  have rep0 : RepAt T K F (k + 1) v0 ρ0 y := fun i hi => match i, hi with
    | 0, _ => (PU.obj d').refl _
    | j + 1, hj => (PU.obj d').trans (F.map_resp (C.hsymm (C.assoc u w (ρ j))) d' y) ((PU.obj d').trans
        (fcomp T K F (C.comp u w) (ρ j) y) ((PU.obj d').trans ((F.map (ρ j)).app_eqv d' fac)
          ((PU.obj d').trans ((F.map (ρ j)).nat (D.comp g' g) e) (PU.map_eqv _ (r j (Nat.lt_of_succ_lt_succ hj))))))
  have mem : (FacS T K F (k + 1) φ ρ0 y).S (D.id d') :=
    ⟨B, C.id B, y, ((F.obj B).1.obj d').trans (F.map_id B d' y) (((F.obj B).1.obj d').symm ((F.obj B).1.map_id d' y)),
      sem_heq T (fun i _ => C.hsymm (C.id_comp (ρ0 i))) sa⟩
  have hcov : K.cov (FacS T K F (k + 1) φ ρ0 y) :=
    cov_mono K (K.top d') (fun _ h' _ => (FacS T K F (k + 1) φ ρ0 y).resp (D.comp_id h')
      ((FacS T K F (k + 1) φ ρ0 y).stable mem h'))
  have f1 := (IH d' v0 B ρ0 y rep0).2 hcov
  exact force_resp K (NFStr T K F) φ (extV_eqv (NFStr T K F)
    (fun i => ((F.obj U1).1.obj d').symm (res_comp (NFStr T K F) g g' v i)) _) f1

theorem kl_ex (Kc : CP.Classical') (hF : FlatF K F) (hC : ContF (JTc T Kc) K F) (k : Nat) (φ : GFm)
    (hφ : FVg (k + 1) φ) (IH : KLst T K F φ (k + 1)) : KLst T K F (.ex φ) k :=
  fun _ v _ ρ e r => ⟨kl_ex_l T K F hF k φ IH v ρ e r, kl_ex_r T K F Kc hC k φ hφ IH v ρ e r⟩

/-- the key lemma for N_F -/
theorem key_lemma (Kc : CP.Classical') (hF : FlatF K F) (hC : ContF (JTc T Kc) K F) :
    (φ : GFm) → (k : Nat) → FVg k φ → KLst T K F φ k
  | .atom R l, k, hf => kl_atom T K F hF k R l hf
  | .eq i j, k, hf => kl_eq T K F hF k i j hf.1 hf.2
  | .top, k, _ => kl_top T K F k
  | .conj a b, k, hf => kl_conj T K F hF k a b (key_lemma Kc hF hC a k hf.1) (key_lemma Kc hF hC b k hf.2)
  | .sup φs, k, hf => kl_sup T K F Kc hC k φs hf (fun n => key_lemma Kc hF hC (φs n) k (hf n))
  | .ex φ, k, hf => kl_ex T K F Kc hF hC k φ hf (key_lemma Kc hF hC φ (k + 1) hf)

end Gt

end Found

#print axioms Found.Gt.ExSv
#print axioms Found.Gt.exSv_cov
#print axioms Found.Gt.kl_ex
#print axioms Found.Gt.key_lemma

found/Gt61.lean

lean_all/found/Gt61.lean

/-
  T194 (classifying topos), part 61. found/Gt61 — a flat continuous functor
  F : C_T → Sh(D, K) gives a sheaf model N_F of T (for T in finite
  contexts): a sequent's left side, read through a local representation
  and the key lemma, factors through generalized elements on which the
  right side holds. N_F is inhabited.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt60

namespace Found

namespace Gt

open Sh

variable (T : Seq → Prop) {D : ECat.{0, 0}} (K : GTop D) (F : EFun (SynCat T) (ShCat K))

theorem nf_valid (Kc : CP.Classical') (hF : FlatF K F) (hC : ContF (JTc T Kc) K F) {k : Nat} {φ ψ : GFm}
    (hφ : FVg k φ) (hψ : FVg k ψ) (h : Ent T φ ψ) (c : D.Ob) (v : Nat → ((F.obj U1).1.obj c).car)
    (f : force K (NFStr T K F) c v φ) : force K (NFStr T K F) c v ψ :=
  force_local K (NFStr T K F) (nf_closed T K F) ψ v _
    (rep_ex T K F hF (inhU_cov T K F Kc hF hC) k c v) (fun d g ⟨E, ρ, e, r⟩ =>
      (key_lemma T K F Kc hF hC ψ k hψ d _ E ρ e r).2
        (cov_mono K ((key_lemma T K F Kc hF hC φ k hφ d _ E ρ e r).1 (force_stable K (NFStr T K F) φ g v f))
          (fun _ _ ⟨E', w, e', h1, s⟩ => ⟨E', w, e', h1, sem_ent T h s⟩)))

/-- the model of a flat continuous functor -/
def NFModel (Kc : CP.Classical') (hF : FlatF K F) (hC : ContF (JTc T Kc) K F)
    (hT : ∀ σ, T σ → ∃ k, FVg k σ.lhs ∧ FVg k σ.rhs) : SModel T where
  D := D
  K := K
  M := NFStr T K F
  sheaf := (F.obj U1).2
  closed := nf_closed T K F
  model := fun σ hσ c v f => match hT σ hσ with
    | ⟨_, hl, hr⟩ => nf_valid T K F Kc hF hC hl hr (ent_ax hσ) c v f

theorem nf_inh (Kc : CP.Classical') (hF : FlatF K F) (hC : ContF (JTc T Kc) K F)
    (hT : ∀ σ, T σ → ∃ k, FVg k σ.lhs ∧ FVg k σ.rhs) : Inh (NFModel T K F Kc hF hC hT) :=
  fun c => inhU_cov T K F Kc hF hC c

end Gt

end Found

#print axioms Found.Gt.nf_valid
#print axioms Found.Gt.NFModel
#print axioms Found.Gt.nf_inh

found/Gt62.lean

lean_all/found/Gt62.lean

/-
  T194 (classifying topos), part 62. found/Gt62 — isomorphisms of
  structures on a site (inverse natural maps of carriers that preserve
  and reflect relations), list tools, and the carrier isomorphism
  F_N(U1) ≅ N.P (one-element tuples).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt61

namespace Found

namespace Gt

open Sh

/-- an isomorphism of structures on the same site -/
structure SIso {D : ECat.{0, 0}} (M M' : @GStr D) where
  fwd : PNat M.P M'.P
  bwd : PNat M'.P M.P
  fb : ∀ c x, (M.P.obj c).eqv (bwd.app c (fwd.app c x)) x
  bf : ∀ c y, (M'.P.obj c).eqv (fwd.app c (bwd.app c y)) y
  rel_fwd : ∀ R c l, M.rel R c l → M'.rel R c (lmapG (fwd.app c) l)
  rel_bwd : ∀ R c l, M'.rel R c l → M.rel R c (lmapG (bwd.app c) l)

section L

variable {C : ECat.{0, 0}} {P Q : Psh.{0, 0, 0} C}

theorem leq_app (α : PNat P Q) {c : C.Ob} : (l l' : List (P.obj c).car) → LEq P c l l' →
    LEq Q c (lmapG (α.app c) l) (lmapG (α.app c) l')
  | [], [], _ => trivial
  | _ :: l, _ :: l', ⟨h1, h2⟩ => ⟨α.app_eqv c h1, leq_app α l l' h2⟩
  | [], _ :: _, h => h.elim
  | _ :: _, [], h => h.elim

theorem leq_nat (α : PNat P Q) {c d : C.Ob} (f : C.Hom d c) : (l : List (P.obj c).car) →
    LEq Q d (lmapG (α.app d) (lmapG (P.map f) l)) (lmapG (Q.map f) (lmapG (α.app c) l))
  | [] => trivial
  | x :: l => ⟨α.nat f x, leq_nat α f l⟩

end L

theorem extLV_range {C : ECat.{0, 0}} (M : @GStr C) {c : C.Ob} :
    (β : Nat → (M.P.obj c).car) → (ds : List (M.P.obj c).car) → lmapG (extLV M β ds) (rangeG (lenG ds)) = ds
  | _, [] => rfl
  | β, a :: ds => by
    show extLV M (extV M β a) ds (lenG ds) :: lmapG (extLV M (extV M β a) ds) (rangeG (lenG ds)) = a :: ds
    have h0 := extLV_off M (extV M β a) ds 0
    rw [Nat.zero_add] at h0
    rw [h0, extLV_range M (extV M β a) ds]
    rfl

section A

variable {T : Seq → Prop} (N : SModel T) (Kc : CP.Classical') (hI : Inh N)

def u1Fwd : PNat (FP N U1) N.M.P where
  app _ t := t.1 0 Nat.zero_lt_one
  app_eqv _ {_ _} h := h 0 Nat.zero_lt_one
  nat _ _ := (N.M.P.obj _).refl _

def u1Bwd : PNat N.M.P (FP N U1) where
  app _ p := ⟨fun _ _ => p, fun _ _ _ => trivial⟩
  app_eqv _ {_ _} h _ _ := h
  nat _ _ _ _ := (N.M.P.obj _).refl _

theorem u1_fb (c : N.D.Ob) (t : ((FP N U1).obj c).car) :
    ((FP N U1).obj c).eqv ((u1Bwd N).app c ((u1Fwd N).app c t)) t := fun i h => match i, h with
  | 0, _ => (N.M.P.obj c).refl _
  | j + 1, h => absurd (Nat.lt_of_succ_lt_succ h) (Nat.not_lt_zero j)

end A

end Gt

end Found

#print axioms Found.Gt.leq_app
#print axioms Found.Gt.leq_nat
#print axioms Found.Gt.extLV_range
#print axioms Found.Gt.u1_fb

found/Gt63.lean

lean_all/found/Gt63.lean

/-
  T194 (classifying topos), part 63. found/Gt63 — for an inhabited sheaf
  model N, the model of the functor F_N is isomorphic to N: relations of
  N_{F_N} hold exactly when N's relations hold of the corresponding
  elements.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt62

namespace Found

namespace Gt

open Sh

theorem rangeG_allLt : (m : Nat) → AllLtG m (rangeG m)
  | 0 => trivial
  | m + 1 => ⟨Nat.lt_succ_self m, allLtG_mono (Nat.le_succ m) (rangeG m) (rangeG_allLt m)⟩

theorem leq_comm_map {C : ECat.{0, 0}} (P : Psh.{0, 0, 0} C) {c : C.Ob} {X Y : Type} (f : X → (P.obj c).car) (g : Y → X)
    (g' : Z → (P.obj c).car) (f' : Y → Z) (h : ∀ y, (P.obj c).eqv (f (g y)) (g' (f' y))) :
    (l : List Y) → LEq P c (lmapG f (lmapG g l)) (lmapG g' (lmapG f' l))
  | [] => trivial
  | y :: l => ⟨h y, leq_comm_map P f g g' f' h l⟩

variable {T : Seq → Prop} (N : SModel T) (Kc : CP.Classical') (hI : Inh N)

theorem rel_fwd_N (R : Nat) (c : N.D.Ob) (l : List (((FP N U1).obj c).car))
    (h : (NFStr T N.K (FN N Kc hI)).rel R c l) : N.M.rel R c (lmapG ((u1Fwd N).app c) l) := by
  refine N.closed.rel_loc R c _ _ h (fun d g ⟨m, E, ρ, e, _, h1, s⟩ => ?_)
  refine N.closed.rel_loc R d _ _ (hI d) (fun d' h' ⟨p, _⟩ => ?_)
  let σ := tv N (fun _ => p) (resT N h' e.1)
  let y : Nat → ((FP N U1).obj d).car := fun i => (FMap N Kc hI (ρ i).2).app d e
  let v : Nat → (N.M.P.obj d').car := fun i => N.M.P.map h' ((y i).1 0 Nat.zero_lt_one)
  have hv : ∀ i, i < m → force N.K N.M d' (extV N.M σ (v i)) (ρ i).1 := fun i _ =>
    force_eqval N.K N.M (ρ := tv N σ (resT N h' (y i).1)) (fun j => match j with
      | 0 => tv_lt N σ _ Nat.zero_lt_one
      | j + 1 => tv_ge N σ _ j) _ (fmap_rel N Kc hI (ρ i).2 e d' h' (fun _ => p))
  have q := s N d' σ v (e.2 d' h' (fun _ => p)) hv
  refine N.M.rel_resp R d' _ _ ?_ q
  have l1 := leq_lmap N.M.P h' _ _ (leq_app (u1Fwd N) _ _ h1)
  have e1 : lmapG (N.M.P.map h') (lmapG ((u1Fwd N).app d) (elsL T N.K (FN N Kc hI) ρ m e)) = lmapG v (rangeG m) := by
    show lmapG (N.M.P.map h') (lmapG ((u1Fwd N).app d) (lmapG y (rangeG m))) = lmapG v (rangeG m)
    rw [lmap_lmap, lmap_lmap]
    rfl
  rw [e1] at l1
  exact leq_trans N.M.P _ _ _ l1 (leq_lmap N.M.P h' _ _ (leq_comm_map N.M.P ((u1Fwd N).app d) (((FN N Kc hI).obj U1).1.map g) (N.M.P.map g) ((u1Fwd N).app c)
    (fun _ => (N.M.P.obj d).refl _) l))

theorem rel_bwd_N (R : Nat) (c : N.D.Ob) (l : List (N.M.P.obj c).car) (h : N.M.rel R c l) :
    (NFStr T N.K (FN N Kc hI)).rel R c (lmapG ((u1Bwd N).app c) l) := by
  let m := lenG l
  let φR : GFm := .atom R (rangeG m)
  have hφR : FVg (m + 1) φR := allLtG_mono (Nat.le_succ m) (rangeG m) (rangeG_allLt m)
  let E := ctxO m φR hφR
  let ρ := varV T m φR hφR
  refine cov_mono N.K (hI c) (fun d g ⟨p, _⟩ => ?_)
  let lg := lmapG (N.M.P.map g) l
  have hlg : lenG lg = m := len_lmapG _ l
  let et : Tup N E.n d := fun i _ => extLV N.M (fun _ => p) lg i
  have rng : lmapG (extLV N.M (fun _ => p) lg) (rangeG m) = lg := by
    have q := extLV_range N.M (fun _ => p) lg
    rw [hlg] at q
    exact q
  have sat : SatT N E et := fun d' f ρ' => by
    show N.M.rel R d' (lmapG (tv N ρ' (resT N f et)) (rangeG m))
    have e1 : lmapG (tv N ρ' (resT N f et)) (rangeG m) = lmapG (N.M.P.map f) (lmapG (extLV N.M (fun _ => p) lg) (rangeG m)) := by
      rw [lmap_lmap]
      exact lmapG_congr _ (fun i hi => tv_lt N ρ' _ (Nat.lt_succ_of_lt (mem_rangeG m i hi)))
    rw [e1, rng]
    exact N.M.rel_stable R f lg (N.M.rel_stable R g l h)
  let e : ((FP N E).obj d).car := ⟨et, sat⟩
  refine ⟨m, E, ρ, e, (len_lmapG _ l).symm, ?_, ?_⟩
  · have e3 : lmapG (fun i => (u1Bwd N).app d (extLV N.M (fun _ => p) lg i)) (rangeG m) = lmapG ((u1Bwd N).app d) lg := by
      rw [← lmap_lmap (extLV N.M (fun _ => p) lg) ((u1Bwd N).app d) (rangeG m), rng]
    refine leq_trans (FP N U1) _ (lmapG ((u1Bwd N).app d) lg) _ ?_
      (leq_comm_map (FP N U1) ((u1Bwd N).app d) (N.M.P.map g) (((FN N Kc hI).obj U1).1.map g) ((u1Bwd N).app c)
        (fun _ => ((FP N U1).obj d).refl _) l)
    rw [← e3]
    refine leq_of_mem (FP N U1) _ _ _ (fun i hi => ?_)
    have hi1 : i < m + 1 := Nat.lt_succ_of_lt (mem_rangeG m i hi)
    have hr : ρ i = varE T m φR hφR i hi1 := varV_lt T m φR hφR hi1
    show ((FP N U1).obj d).eqv ((FMap N Kc hI (ρ i).2).app d e) _
    rw [hr]
    exact relT_uniq N hI (varE T m φR hφR i hi1).2 (fmap_rel N Kc hI _ e)
      (relT_proj N E U1 i hi1 et sat _ (fun j hj => match j, hj with
        | 0, _ => by
          show (N.M.P.obj d).eqv (extLV N.M (fun _ => p) lg i) (extLV N.M (fun _ => p) lg (0 + i))
          rw [Nat.zero_add]
          exact (N.M.P.obj d).refl _
        | j + 1, hj => absurd (Nat.lt_of_succ_lt_succ hj) (Nat.not_lt_zero j)))
  · intro N' c' σ v hE hv
    refine N'.M.rel_resp R c' _ _ (leq_of_mem N'.M.P _ _ _ (fun i hi => ?_)) hE
    have hi1 : i < m + 1 := Nat.lt_succ_of_lt (mem_rangeG m i hi)
    have f := hv i (mem_rangeG m i hi)
    rw [show ρ i = varE T m φR hφR i hi1 from varV_lt T m φR hφR hi1] at f
    have q := ((force_projF N'.K N'.M E 1 i σ [v i] rfl).1 f).2 0 Nat.zero_lt_one
    rw [Nat.zero_add] at q
    exact (N'.M.P.obj c').symm q

/-- the model of F_N is isomorphic to N -/
def nfn_iso : SIso (NFStr T N.K (FN N Kc hI)) N.M where
  fwd := u1Fwd N
  bwd := u1Bwd N
  fb := u1_fb N
  bf := fun _ _ => (N.M.P.obj _).refl _
  rel_fwd := rel_fwd_N N Kc hI
  rel_bwd := rel_bwd_N N Kc hI

end Gt

end Found

#print axioms Found.Gt.rangeG_allLt
#print axioms Found.Gt.rel_fwd_N
#print axioms Found.Gt.rel_bwd_N
#print axioms Found.Gt.nfn_iso

found/Gt64.lean

lean_all/found/Gt64.lean

/-
  T194 (classifying topos), part 64. found/Gt64 — arrows into an object A
  of the syntactic category are determined by their components: the
  projections π_i : A → [y : ⊤]; elements ρ_i : E → [y : ⊤] whose values
  satisfy A.φ form an arrow ⟨ρ⟩ : E → A with π_i ∘ ⟨ρ⟩ = ρ_i; two arrows
  with equal components are equal.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt63

namespace Found

namespace Gt

open Sh

variable (T : Seq → Prop)

def projA (A : SObj) (i : Nat) (h : i < A.n) : (SynCat T).Hom A U1 :=
  ⟨projF A 1 i, proj_arr T A U1 i h (fun _ _ _ _ => trivial)⟩

/-- reading a projection -/
theorem force_projA (A : SObj) (i : Nat) (h : i < A.n) (N : SModel T) {c : N.D.Ob} (σ : Nat → (N.M.P.obj c).car)
    (y : (N.M.P.obj c).car) :
    force N.K N.M c (extV N.M σ y) (projA T A i h).1 ↔ force N.K N.M c σ A.φ ∧ (N.M.P.obj c).eqv y (σ i) :=
  (force_projF N.K N.M A 1 i σ [y] rfl).trans
    ⟨fun ⟨h1, h2⟩ => ⟨h1, by have q := h2 0 Nat.zero_lt_one; rw [Nat.zero_add] at q; exact q⟩,
     fun ⟨h1, h2⟩ => ⟨h1, fun j hj => match j, hj with
      | 0, _ => by rw [Nat.zero_add]; exact h2
      | j + 1, hj => absurd (Nat.lt_of_succ_lt_succ hj) (Nat.not_lt_zero j)⟩⟩

/-- two arrows into A with equal components are equal -/
theorem arr_ext {A B : SObj} (f g : (SynCat T).Hom B A)
    (h : ∀ i (hi : i < A.n), (SynCat T).heq ((SynCat T).comp f (projA T A i hi)) ((SynCat T).comp g (projA T A i hi))) :
    (SynCat T).heq f g := by
  have dir : ∀ (f g : (SynCat T).Hom B A),
      (∀ i (hi : i < A.n), (SynCat T).heq ((SynCat T).comp f (projA T A i hi)) ((SynCat T).comp g (projA T A i hi))) →
      ∀ (N : SModel T) c (σ : Nat → (N.M.P.obj c).car) ys, lenG ys = A.n →
        force N.K N.M c (extLV N.M σ ys) f.1 → force N.K N.M c (extLV N.M σ ys) g.1 := by
    intro f g h N c σ ys hl ff
    have hB := (f.2.2.1 N c σ ys hl ff).1
    have hA := (f.2.2.1 N c σ ys hl ff).2
    refine force_local N.K N.M N.closed _ _ _ (exN_wit N.K N.M _ A.n σ (g.2.2.2.1 N c σ hB)) (fun d k ⟨ys', hl', fg⟩ => ?_)
    have hyk : lenG (lmapG (N.M.P.map k) ys) = A.n := (len_lmapG _ ys).trans hl
    have ff' := force_resp N.K N.M _ (res_block N.M k σ ys) (force_stable N.K N.M _ k _ ff)
    have hA' := force_resp N.K N.M _ (res_block N.M k σ ys) (force_stable N.K N.M _ k _ hA)
    refine force_resp N.K N.M _ (fun n => (N.M.P.obj d).symm (res_block N.M k σ ys n)) ?_
    refine force_agree N.K N.M _ (A.n + B.n) g.2.1 (agree_blocks N.M _ _ _ _ A.n B.n hl' hyk (fun i hi => ?_)
      (fun _ _ => (N.M.P.obj d).refl _)) fg
    let yi := extLV N.M (resV N.M k σ) (lmapG (N.M.P.map k) ys) i
    have c1 : force N.K N.M d (extV N.M (resV N.M k σ) yi) ((SynCat T).comp f (projA T A i hi)).1 :=
      comp_intro T (Y := A) (Z := U1) (projA T A i hi).2.1 N _ _ [yi] hyk rfl ff'
        ((force_projA T A i hi N _ yi).2 ⟨hA', (N.M.P.obj d).refl _⟩)
    have c2 := (h i hi N d (resV N.M k σ) [yi] rfl).1 c1
    have c3 := comp_elim T g.2 (projA T A i hi).2 N _ ys' [yi] hl' rfl c2 fg
    exact (N.M.P.obj d).symm ((force_projA T A i hi N _ yi).1 c3).2
  exact fun N c σ ys hl => ⟨dir f g h N c σ ys hl, dir g f (fun i hi => (SynCat T).hsymm (h i hi)) N c σ ys hl⟩

/-- the tuple arrow ⟨ρ_0, …, ρ_{n-1}⟩ : E → A -/
def tupF (E A : SObj) (ρ : Nat → (SynCat T).Hom E U1) : GFm := .conj (renG (· + A.n) E.φ) (grK T ρ A.n)

theorem tup_read {E A : SObj} (ρ : Nat → (SynCat T).Hom E U1) (N : SModel T) {c : N.D.Ob}
    (σ : Nat → (N.M.P.obj c).car) (ys : List (N.M.P.obj c).car) (hl : lenG ys = A.n)
    (h : force N.K N.M c (extLV N.M σ ys) (tupF T E A ρ)) :
    force N.K N.M c σ E.φ ∧ ∀ i, i < A.n → force N.K N.M c (extV N.M σ (extLV N.M σ ys i)) (ρ i).1 :=
  ⟨force_eqval N.K N.M (shift_val N.M σ ys A.n hl) E.φ ((force_ren N.K N.M E.φ _ _).1 h.1),
   fun i hi => force_eqval N.K N.M (ρ := extV N.M (fun j => extLV N.M σ ys (j + A.n)) (extLV N.M σ ys i))
     (ρ' := extV N.M σ (extLV N.M σ ys i)) (fun m => match m with
     | 0 => rfl
     | j + 1 => shift_val N.M σ ys A.n hl j) _ ((force_grK T N.K N.M ρ A.n _).1 h.2 i hi)⟩

theorem tup_make {E A : SObj} (ρ : Nat → (SynCat T).Hom E U1) (N : SModel T) {c : N.D.Ob}
    (σ : Nat → (N.M.P.obj c).car) (ys : List (N.M.P.obj c).car) (hl : lenG ys = A.n) (hE : force N.K N.M c σ E.φ)
    (hv : ∀ i, i < A.n → force N.K N.M c (extV N.M σ (extLV N.M σ ys i)) (ρ i).1) :
    force N.K N.M c (extLV N.M σ ys) (tupF T E A ρ) :=
  ⟨(force_ren N.K N.M E.φ _ _).2 (force_eqval N.K N.M (fun i => (shift_val N.M σ ys A.n hl i).symm) E.φ hE),
   (force_grK T N.K N.M ρ A.n _).2 (fun i hi => force_eqval N.K N.M (ρ := extV N.M σ (extLV N.M σ ys i))
     (ρ' := extV N.M (fun j => extLV N.M σ ys (j + A.n)) (extLV N.M σ ys i)) (fun m => match m with
     | 0 => rfl
     | j + 1 => (shift_val N.M σ ys A.n hl j).symm) _ (hv i hi))⟩

theorem tup_arr {E A : SObj} (ρ : Nat → (SynCat T).Hom E U1) (hs : Sem T A.n E ρ A.φ) : IsArr T E A (tupF T E A ρ) := by
  refine ⟨⟨fv_ren E.φ (fun i hi => by rw [Nat.add_comm A.n E.n]; exact Nat.add_lt_add_right hi A.n) E.fv, fv_grK T ρ A.n⟩,
    fun N c σ ys hl h => ?_, fun N c σ hE => ?_, fun N c σ ys ys' hl hl' h h' i hi => ?_⟩
  · have r := tup_read T ρ N σ ys hl h
    exact ⟨r.1, hs N c σ (extLV N.M σ ys) r.1 r.2⟩
  · refine force_local N.K N.M N.closed _ σ _ (cov_tot T N ρ σ hE A.n) (fun d g ⟨w, hw⟩ => ?_)
    have hwl := len_witL N.M w A.n
    exact exN_of N.K N.M _ A.n _ (witL N.M w A.n) hwl (tup_make T ρ N _ _ hwl (force_stable N.K N.M _ g σ hE)
      (fun i hi => by rw [witL_val N.M w A.n _ i hi]; exact hw i hi))
  · exact (ρ i).2.2.2.2 N c σ [_] [_] rfl rfl ((tup_read T ρ N σ ys hl h).2 i hi) ((tup_read T ρ N σ ys' hl' h').2 i hi)
      0 Nat.zero_lt_one

def tupH {E A : SObj} (ρ : Nat → (SynCat T).Hom E U1) (hs : Sem T A.n E ρ A.φ) : (SynCat T).Hom E A :=
  ⟨tupF T E A ρ, tup_arr T ρ hs⟩

theorem tup_proj {E A : SObj} (ρ : Nat → (SynCat T).Hom E U1) (hs : Sem T A.n E ρ A.φ) (i : Nat) (hi : i < A.n) :
    (SynCat T).heq ((SynCat T).comp (tupH T ρ hs) (projA T A i hi)) (ρ i) := by
  intro N c σ ys hl
  match ys, hl with
  | [], hl => exact Nat.noConfusion hl
  | _ :: _ :: _, hl => exact Nat.noConfusion (Nat.succ.inj hl)
  | [y], _ =>
  constructor
  · intro h
    refine force_local N.K N.M N.closed _ _ _ (exN_wit N.K N.M _ A.n _ h) (fun d g ⟨ys', hl', fb⟩ => ?_)
    have b := body_at T (Y := A) (Z := U1) N (projA T A i hi).2.1 g σ [y] rfl ys' hl' fb
    have r := (tup_read T ρ N _ ys' hl' b.1).2 i hi
    have e := ((force_projA T A i hi N _ _).1 b.2).2
    refine force_resp N.K N.M _ (fun n => (N.M.P.obj d).symm (ext_res N.M g σ y n)) ?_
    exact force_resp N.K N.M _ (ρ := extV N.M (resV N.M g σ) (extLV N.M (resV N.M g σ) ys' i))
      (ρ' := extV N.M (resV N.M g σ) (N.M.P.map g y)) (fun n => match n with
        | 0 => (N.M.P.obj d).symm e
        | _ + 1 => (N.M.P.obj d).refl _) r
  · intro h
    have hE := ((ρ i).2.2.1 N c σ [y] rfl h).1
    refine force_local N.K N.M N.closed _ _ _ (exN_wit N.K N.M _ A.n σ ((tup_arr T ρ hs).2.2.1 N c σ hE))
      (fun d g ⟨ys', hl', ft⟩ => ?_)
    have r := tup_read T ρ N _ ys' hl' ft
    have hy := force_resp N.K N.M _ (ext_res N.M g σ y) (force_stable N.K N.M _ g _ h)
    have e := (ρ i).2.2.2.2 N d (resV N.M g σ) [_] [_] rfl rfl hy (r.2 i hi) 0 Nat.zero_lt_one
    have hA := ((tup_arr T ρ hs).2.1 N d _ ys' hl' ft).2
    refine force_resp N.K N.M _ (fun n => (N.M.P.obj d).symm (ext_res N.M g σ y n)) ?_
    exact comp_intro T (Y := A) (Z := U1) (projA T A i hi).2.1 N _ ys' [_] hl' rfl ft
      ((force_projA T A i hi N _ _).2 ⟨hA, e⟩)

end Gt

end Found

#print axioms Found.Gt.force_projA
#print axioms Found.Gt.arr_ext
#print axioms Found.Gt.tup_arr
#print axioms Found.Gt.tup_proj

found/Gt65.lean

lean_all/found/Gt65.lean

/-
  T194 (classifying topos), part 65. found/Gt65 — for a flat F, the maps
  F(π_i) : F(A) → F(U1) are jointly monic: two elements with the same
  components are equal (locally they come from one element on which the
  two arrows have equal components, hence are equal arrows).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt64

namespace Found

namespace Gt

open Sh

variable (T : Seq → Prop) {D : ECat.{0, 0}} (K : GTop D) (F : EFun (SynCat T) (ShCat K))

/-- stages where z factors through an arrow equalizing the first n components of u and v -/
def EqU {E A : SObj} (u v : (SynCat T).Hom E A) (n : Nat) {c : D.Ob} (z : ((F.obj E).1.obj c).car) : Sieve D c where
  S := fun {d} g => ∃ (E' : SObj) (w : (SynCat T).Hom E' E) (z' : ((F.obj E').1.obj d).car),
    ((F.obj E).1.obj d).eqv ((F.map w).app d z') ((F.obj E).1.map g z) ∧
    ∀ i (_ : i < n) (h : i < A.n), (SynCat T).heq ((SynCat T).comp w ((SynCat T).comp u (projA T A i h)))
      ((SynCat T).comp w ((SynCat T).comp v (projA T A i h)))
  resp := fun h ⟨E', w, z', h1, hs⟩ => ⟨E', w, z', ((F.obj E).1.obj _).trans h1 ((F.obj E).1.map_resp z h), hs⟩
  stable := fun {_ _} {g} ⟨E', w, z', h1, hs⟩ h => ⟨E', w, (F.obj E').1.map h z',
    ((F.obj E).1.obj _).trans ((F.map w).nat h z') (((F.obj E).1.obj _).trans ((F.obj E).1.map_eqv h h1)
      (((F.obj E).1.obj _).symm ((F.obj E).1.map_comp h g z))), hs⟩

theorem eqz_upto (hF : FlatF K F) {E A : SObj} (u v : (SynCat T).Hom E A) {c : D.Ob} (z : ((F.obj E).1.obj c).car)
    (hz : ∀ i (h : i < A.n), ((F.obj U1).1.obj c).eqv ((F.map ((SynCat T).comp u (projA T A i h))).app c z)
      ((F.map ((SynCat T).comp v (projA T A i h))).app c z)) :
    (n : Nat) → K.cov (EqU T K F u v n z)
  | 0 => cov_mono K (K.top c) (fun d g _ => ⟨E, (SynCat T).id E, (F.obj E).1.map g z, F.map_id E d _,
      fun _ hi _ => absurd hi (Nat.not_lt_zero _)⟩)
  | n + 1 => by
    let C := SynCat T
    let PU := (F.obj U1).1
    refine K.trans (eqz_upto hF u v z hz n) (fun d g ⟨E1, w1, z1, h1, hq1⟩ => ?_)
    match Nat.decLt n A.n with
    | isFalse hn =>
      exact cov_mono K (K.top d) (fun d' g' _ => ⟨E1, w1, (F.obj E1).1.map g' z1,
        ((F.obj E).1.obj d').trans ((F.map w1).nat g' z1) (((F.obj E).1.obj d').trans ((F.obj E).1.map_eqv g' h1)
          (((F.obj E).1.obj d').symm ((F.obj E).1.map_comp g' g z))),
        fun i hi h => match Nat.decLt i n with
          | isTrue hi' => hq1 i hi' h
          | isFalse hi' => by
            have e : i = n := Nat.le_antisymm (Nat.le_of_lt_succ hi) (Nat.le_of_not_lt hi')
            subst e
            exact absurd h hn⟩)
    | isTrue hn =>
    have hab : (PU.obj d).eqv ((F.map (C.comp w1 (C.comp u (projA T A n hn)))).app d z1)
        ((F.map (C.comp w1 (C.comp v (projA T A n hn)))).app d z1) :=
      (PU.obj d).trans (fcomp T K F w1 _ z1) ((PU.obj d).trans ((F.map _).app_eqv d h1)
        ((PU.obj d).trans ((F.map _).nat g z) ((PU.obj d).trans (PU.map_eqv g (hz n hn))
          ((PU.obj d).trans ((PU.obj d).symm ((F.map _).nat g z)) ((PU.obj d).trans
            ((F.map _).app_eqv d (((F.obj E).1.obj d).symm h1)) ((PU.obj d).symm (fcomp T K F w1 _ z1)))))))
    refine cov_mono K (hF.eqz _ _ z1 hab) (fun d' g' ⟨E2, w2, z2, hw2, h2⟩ => ⟨E2, C.comp w2 w1, z2, ?_, fun i hi h => ?_⟩)
    · exact fac_comp T K F w1 w2 g g' h1 h2
    · match Nat.decLt i n with
      | isTrue hi' => exact C.htrans (C.assoc w2 w1 _) (C.htrans (C.comp_resp (C.hrefl w2) (hq1 i hi' h))
          (C.hsymm (C.assoc w2 w1 _)))
      | isFalse hi' =>
        have e : i = n := Nat.le_antisymm (Nat.le_of_lt_succ hi) (Nat.le_of_not_lt hi')
        subst e
        exact C.htrans (C.assoc w2 w1 _) (C.htrans hw2 (C.hsymm (C.assoc w2 w1 _)))

theorem proj_monic (hF : FlatF K F) {A : SObj} {c : D.Ob} (x y : ((F.obj A).1.obj c).car)
    (h : ∀ i (hi : i < A.n), ((F.obj U1).1.obj c).eqv ((F.map (projA T A i hi)).app c x) ((F.map (projA T A i hi)).app c y)) :
    ((F.obj A).1.obj c).eqv x y := by
  let C := SynCat T
  let PU := (F.obj U1).1
  let PA := (F.obj A).1
  refine sheaf_sep K PA (F.obj A).2 c _ (hF.pair x y) x y (fun d g ⟨E, u, v, z, hu, hv⟩ => ?_)
  have hz : ∀ i (hi : i < A.n), (PU.obj d).eqv ((F.map (C.comp u (projA T A i hi))).app d z)
      ((F.map (C.comp v (projA T A i hi))).app d z) := fun i hi =>
    (PU.obj d).trans (fcomp T K F u _ z) ((PU.obj d).trans ((F.map _).app_eqv d hu) ((PU.obj d).trans
      ((F.map _).nat g x) ((PU.obj d).trans (PU.map_eqv g (h i hi)) ((PU.obj d).trans ((PU.obj d).symm ((F.map _).nat g y))
        ((PU.obj d).trans ((F.map _).app_eqv d ((PA.obj d).symm hv)) ((PU.obj d).symm (fcomp T K F v _ z)))))))
  refine sheaf_sep K PA (F.obj A).2 d _ (eqz_upto T K F hF u v z hz A.n) _ _ (fun d' g' ⟨E', w, z', hw, hq⟩ => ?_)
  have ha : C.heq (C.comp w u) (C.comp w v) := arr_ext T _ _ (fun i hi =>
    C.htrans (C.assoc w u _) (C.htrans (hq i hi hi) (C.hsymm (C.assoc w v _))))
  have q : (PA.obj d').eqv ((F.map (C.comp w u)).app d' z') ((F.map (C.comp w v)).app d' z') := F.map_resp ha d' z'
  have qu : (PA.obj d').eqv ((F.map (C.comp w u)).app d' z') (PA.map g' (PA.map g x)) :=
    (PA.obj d').trans (fcomp T K F w u z') ((PA.obj d').trans ((F.map u).app_eqv d' hw)
      ((PA.obj d').trans ((F.map u).nat g' z) (PA.map_eqv g' hu)))
  have qv : (PA.obj d').eqv ((F.map (C.comp w v)).app d' z') (PA.map g' (PA.map g y)) :=
    (PA.obj d').trans (fcomp T K F w v z') ((PA.obj d').trans ((F.map v).app_eqv d' hw)
      ((PA.obj d').trans ((F.map v).nat g' z) (PA.map_eqv g' hv)))
  exact (PA.obj d').trans ((PA.obj d').symm qu) ((PA.obj d').trans q qv)

end Gt

end Found

#print axioms Found.Gt.eqz_upto
#print axioms Found.Gt.proj_monic

found/Gt66.lean

lean_all/found/Gt66.lean

/-
  T194 (classifying topos), part 66. found/Gt66 — comparing F with the
  functor of its model: an element e ∈ F(A) gives the tuple of its
  components F(π_i)(e), which satisfies A.φ in N_F (read through the key
  lemma, representing the components by a generalized element over A).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt65

namespace Found

namespace Gt

open Sh

variable (T : Seq → Prop)

/-- the components of u : E → A, padded by v -/
def selA {E A : SObj} (u : (SynCat T).Hom E A) (v : (SynCat T).Hom E U1) (i : Nat) : (SynCat T).Hom E U1 :=
  match Nat.decLt i A.n with
  | isTrue h => (SynCat T).comp u (projA T A i h)
  | isFalse _ => v

theorem selA_lt {E A : SObj} (u : (SynCat T).Hom E A) (v : (SynCat T).Hom E U1) {i : Nat} (h : i < A.n) :
    selA T u v i = (SynCat T).comp u (projA T A i h) := by
  unfold selA
  match Nat.decLt i A.n with
  | isTrue _ => rfl
  | isFalse h' => exact absurd h h'

/-- values of the components of an arrow into A satisfy A.φ -/
theorem sem_via {E A : SObj} (u : (SynCat T).Hom E A) (v : (SynCat T).Hom E U1) : Sem T A.n E (selA T u v) A.φ := by
  intro N c σ w hE hw
  refine force_local N.K N.M N.closed A.φ w _ (exN_wit N.K N.M _ A.n σ (u.2.2.2.1 N c σ hE)) (fun d g ⟨ys, hl, fu⟩ => ?_)
  have hA := (u.2.2.1 N d _ ys hl fu).2
  refine force_agree N.K N.M A.φ A.n A.fv (fun i hi => ?_) hA
  have f1 := force_resp N.K N.M _ (ext_res N.M g σ (w i)) (force_stable N.K N.M _ g _ (hw i hi))
  rw [selA_lt T u v hi] at f1
  have f2 := comp_elim T u.2 (projA T A i hi).2 N _ ys [_] hl rfl f1 fu
  exact (N.M.P.obj d).symm ((force_projA T A i hi N _ _).1 f2).2

variable {D : ECat.{0, 0}} (K : GTop D) (F : EFun (SynCat T) (ShCat K)) (Kc : CP.Classical') (hF : FlatF K F)
  (hC : ContF (JTc T Kc) K F) (hT : ∀ σ, T σ → ∃ k, FVg k σ.lhs ∧ FVg k σ.rhs)

/-- stages where X comes from a generalized element with an extra element-arrow -/
def RepA (A : SObj) {d : D.Ob} (X : ((F.obj A).1.obj d).car) : Sieve D d where
  S := fun {d'} g => ∃ (E : SObj) (u : (SynCat T).Hom E A) (_ : (SynCat T).Hom E U1) (z : ((F.obj E).1.obj d').car),
    ((F.obj A).1.obj d').eqv ((F.map u).app d' z) ((F.obj A).1.map g X)
  resp := fun h ⟨E, u, v, z, hu⟩ => ⟨E, u, v, z, ((F.obj A).1.obj _).trans hu ((F.obj A).1.map_resp X h)⟩
  stable := fun {_ _} {g} ⟨E, u, v, z, hu⟩ h => ⟨E, u, v, (F.obj E).1.map h z,
    ((F.obj A).1.obj _).trans ((F.map u).nat h z) (((F.obj A).1.obj _).trans ((F.obj A).1.map_eqv h hu)
      (((F.obj A).1.obj _).symm ((F.obj A).1.map_comp h g X)))⟩

include Kc hF hC in
theorem repA_cov (A : SObj) {d : D.Ob} (X : ((F.obj A).1.obj d).car) : K.cov (RepA T K F A X) :=
  K.trans (inhU_cov T K F Kc hF hC d) (fun _ g1 ⟨p, _⟩ => cov_mono K (hF.pair ((F.obj A).1.map g1 X) p)
    (fun _ g2 ⟨E, u, v, z, hu, _⟩ => ⟨E, u, v, z, ((F.obj A).1.obj _).trans hu
      (((F.obj A).1.obj _).symm ((F.obj A).1.map_comp g2 g1 X))⟩))

/-- the components of an element satisfy A.φ in N_F -/
theorem comp_sat (A : SObj) {d : D.Ob} (X : ((F.obj A).1.obj d).car) (ρ : Nat → ((F.obj U1).1.obj d).car) :
    force K (NFStr T K F) d (tv (NFModel T K F Kc hF hC hT) ρ (fun i h => (F.map (projA T A i h)).app d X)) A.φ := by
  let PU := (F.obj U1).1
  let N := NFModel T K F Kc hF hC hT
  let V := tv N ρ (fun i h => (F.map (projA T A i h)).app d X)
  refine force_local K (NFStr T K F) (nf_closed T K F) A.φ V _ (repA_cov T K F Kc hF hC A X)
    (fun d2 g ⟨E, u, v, z, hu⟩ => ?_)
  have rep : RepAt T K F A.n (resV (NFStr T K F) g V) (selA T u v) z := fun i hi => by
    rw [selA_lt T u v hi]
    have q : V i = (F.map (projA T A i hi)).app d X := tv_lt N ρ _ hi
    show (PU.obj d2).eqv _ (PU.map g (V i))
    rw [q]
    exact (PU.obj d2).trans (fcomp T K F u _ z) ((PU.obj d2).trans ((F.map _).app_eqv d2 hu) ((F.map _).nat g X))
  refine (key_lemma T K F Kc hF hC A.φ A.n A.fv d2 _ E (selA T u v) z rep).2 ?_
  exact cov_mono K (K.top d2) (fun _ h _ => ⟨E, (SynCat T).id E, (F.obj E).1.map h z, F.map_id E _ _,
    sem_heq T (fun i _ => (SynCat T).hsymm ((SynCat T).id_comp _)) (sem_via T u v)⟩)

/-- the comparison map F(A) → F_{N_F}(A) -/
def tauN (A : SObj) : PNat (F.obj A).1 (FP (NFModel T K F Kc hF hC hT) A) where
  app d X := ⟨fun i h => (F.map (projA T A i h)).app d X, fun d' f ρ =>
    force_resp K (NFStr T K F) A.φ (tv_eqv (NFModel T K F Kc hF hC hT) (fun i h => (F.map (projA T A i h)).nat f X)
      (fun _ => ((F.obj U1).1.obj d').refl _)) (comp_sat T K F Kc hF hC hT A ((F.obj A).1.map f X) ρ)⟩
  app_eqv d {_ _} h i hi := (F.map (projA T A i hi)).app_eqv d h
  nat f X i hi := (F.map (projA T A i hi)).nat f X

end Gt

end Found

#print axioms Found.Gt.sem_via
#print axioms Found.Gt.repA_cov
#print axioms Found.Gt.comp_sat
#print axioms Found.Gt.tauN

found/Gt67.lean

lean_all/found/Gt67.lean

/-
  T194 (classifying topos), part 67. found/Gt67 — every tuple of F(U1)
  satisfying A.φ in N_F is the tuple of components of an element of F(A):
  locally (key lemma) the tuple comes from a generalized element on which
  A.φ holds, i.e. from a tuple arrow into A; the local elements glue (F(A)
  is a sheaf, components are jointly monic). Hence F(A) ≅ F_{N_F}(A).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt66

namespace Found

namespace Gt

open Sh

variable (T : Seq → Prop) {D : ECat.{0, 0}} (K : GTop D) (F : EFun (SynCat T) (ShCat K)) (Kc : CP.Classical')
  (hF : FlatF K F) (hC : ContF (JTc T Kc) K F) (hT : ∀ σ, T σ → ∃ k, FVg k σ.lhs ∧ FVg k σ.rhs)

/-- stages where the tuple comes from an element of F(A) -/
def LocA (A : SObj) {c : D.Ob} (t : Tup (NFModel T K F Kc hF hC hT) A.n c) : Sieve D c where
  S := fun {d} g => ∃ X : ((F.obj A).1.obj d).car, ∀ i (h : i < A.n),
    ((F.obj U1).1.obj d).eqv ((F.map (projA T A i h)).app d X) ((F.obj U1).1.map g (t i h))
  resp := fun e ⟨X, hX⟩ => ⟨X, fun i h => ((F.obj U1).1.obj _).trans (hX i h) ((F.obj U1).1.map_resp _ e)⟩
  stable := fun {_ _} {g} ⟨X, hX⟩ k => ⟨(F.obj A).1.map k X, fun i h => ((F.obj U1).1.obj _).trans
    ((F.map (projA T A i h)).nat k X) (((F.obj U1).1.obj _).trans ((F.obj U1).1.map_eqv k (hX i h))
      (((F.obj U1).1.obj _).symm ((F.obj U1).1.map_comp k g _)))⟩

theorem locA_cov (A : SObj) {c : D.Ob} (t : ((FP (NFModel T K F Kc hF hC hT) A).obj c).car) :
    K.cov (LocA T K F Kc hF hC hT A t.1) := by
  let C := SynCat T
  let PU := (F.obj U1).1
  let N := NFModel T K F Kc hF hC hT
  refine K.trans (inhU_cov T K F Kc hF hC c) (fun d1 g1 ⟨p, _⟩ => ?_)
  let v := tv N (fun _ => p) (resT N g1 t.1)
  refine K.trans (rep_ex T K F hF (inhU_cov T K F Kc hF hC) A.n d1 v) (fun d2 g2 ⟨E, ρ, e, r⟩ => ?_)
  have sat := t.2 d2 (D.comp g2 g1) (fun _ => PU.map g2 p)
  have hv : force K (NFStr T K F) d2 (resV (NFStr T K F) g2 v) A.φ :=
    force_eqval K (NFStr T K F) (fun i => (res_tv N g2 (fun _ => p) (resT N g1 t.1) i).symm) A.φ
      (force_resp K (NFStr T K F) A.φ (tv_eqv N (fun i h => PU.map_comp g2 g1 (t.1 i h))
        (fun _ => (PU.obj d2).refl _)) sat)
  refine cov_mono K ((key_lemma T K F Kc hF hC A.φ A.n A.fv d2 _ E ρ e r).1 hv) (fun d3 g3 ⟨E', w, e', h1, s⟩ => ?_)
  let θ := tupH T (fun i => C.comp w (ρ i)) s
  refine ⟨(F.map θ).app d3 e', fun i h => ?_⟩
  have c1 : (PU.obj d3).eqv ((F.map (projA T A i h)).app d3 ((F.map θ).app d3 e')) ((F.map (C.comp w (ρ i))).app d3 e') :=
    (PU.obj d3).trans ((PU.obj d3).symm (fcomp T K F θ (projA T A i h) e'))
      (F.map_resp (tup_proj T (fun i => C.comp w (ρ i)) s i h) d3 e')
  have c2 : (PU.obj d3).eqv ((F.map (C.comp w (ρ i))).app d3 e') (PU.map g3 (PU.map g2 (v i))) :=
    (PU.obj d3).trans (fcomp T K F w (ρ i) e') ((PU.obj d3).trans ((F.map (ρ i)).app_eqv d3 h1)
      ((PU.obj d3).trans ((F.map (ρ i)).nat g3 e) (PU.map_eqv g3 (r i h))))
  have c3 : (PU.obj d3).eqv (PU.map g3 (PU.map g2 (v i))) (PU.map (D.comp (D.comp g3 g2) g1) (t.1 i h)) := by
    have q : v i = PU.map g1 (t.1 i h) := tv_lt N _ _ h
    rw [q]
    exact (PU.obj d3).trans ((PU.obj d3).symm (PU.map_comp g3 g2 _)) ((PU.obj d3).trans
      ((PU.obj d3).symm (PU.map_comp (D.comp g3 g2) g1 _)) ((PU.obj d3).refl _))
  exact (PU.obj d3).trans c1 ((PU.obj d3).trans c2 c3)

theorem locA_ex (A : SObj) {c : D.Ob} (t : ((FP (NFModel T K F Kc hF hC hT) A).obj c).car) :
    ∃ X : ((F.obj A).1.obj c).car, ∀ i (h : i < A.n),
      ((F.obj U1).1.obj c).eqv ((F.map (projA T A i h)).app c X) (t.1 i h) := by
  let PU := (F.obj U1).1
  let PA := (F.obj A).1
  let S := LocA T K F Kc hF hC hT A t.1
  have hS := locA_cov T K F Kc hF hC hT A t
  let P := fun {d : D.Ob} (f : D.Hom d c) (X : (PA.obj d).car) => ∀ i (h : i < A.n),
    (PU.obj d).eqv ((F.map (projA T A i h)).app d X) (PU.map f (t.1 i h))
  let pick : {d : D.Ob} → (f : D.Hom d c) → S.S f → (PA.obj d).car := fun f hf => Kc.ch (P f) hf
  have pk : ∀ {d} (f : D.Hom d c) (hf : S.S f), P f (pick f hf) := fun f hf => Kc.ch_spec (P f) hf
  let m : MFam PA S :=
    ⟨fun f hf => pick f hf,
     fun {_} {f f'} hf hf' e => proj_monic T K F hF _ _ (fun i h => (PU.obj _).trans (pk f hf i h)
       ((PU.obj _).trans (PU.map_resp _ e) ((PU.obj _).symm (pk f' hf' i h)))),
     fun f hf g => proj_monic T K F hF _ _ (fun i h => (PU.obj _).trans (pk _ (S.stable hf g) i h)
       ((PU.obj _).trans (PU.map_comp g f _) ((PU.obj _).trans (PU.map_eqv g ((PU.obj _).symm (pk f hf i h)))
         ((PU.obj _).symm ((F.map (projA T A i h)).nat g _)))))⟩
  match ((F.obj A).2 c S hS m).1 with
  | ⟨X0, hX0⟩ =>
  exact ⟨X0, fun i h => sheaf_sep K PU (F.obj U1).2 c S hS _ _ (fun d f hf =>
    (PU.obj d).trans ((PU.obj d).symm ((F.map (projA T A i h)).nat f X0)) ((PU.obj d).trans
      ((F.map (projA T A i h)).app_eqv d (hX0 d f hf)) (pk f hf i h)))⟩

/-- the inverse comparison F_{N_F}(A) → F(A) -/
def tauInv (A : SObj) : PNat (FP (NFModel T K F Kc hF hC hT) A) (F.obj A).1 where
  app _ t := Kc.ch _ (locA_ex T K F Kc hF hC hT A t)
  app_eqv _ {t t'} h := proj_monic T K F hF _ _ (fun i hi => ((F.obj U1).1.obj _).trans
    (Kc.ch_spec _ (locA_ex T K F Kc hF hC hT A t) i hi) (((F.obj U1).1.obj _).trans (h i hi)
      (((F.obj U1).1.obj _).symm (Kc.ch_spec _ (locA_ex T K F Kc hF hC hT A t') i hi))))
  nat f t := proj_monic T K F hF _ _ (fun i hi => ((F.obj U1).1.obj _).trans
    (Kc.ch_spec _ (locA_ex T K F Kc hF hC hT A _) i hi) (((F.obj U1).1.obj _).symm (((F.obj U1).1.obj _).trans
      ((F.map (projA T A i hi)).nat f _) ((F.obj U1).1.map_eqv f (Kc.ch_spec _ (locA_ex T K F Kc hF hC hT A t) i hi)))))

theorem tau_inv_l (A : SObj) {c : D.Ob} (X : ((F.obj A).1.obj c).car) :
    ((F.obj A).1.obj c).eqv ((tauInv T K F Kc hF hC hT A).app c ((tauN T K F Kc hF hC hT A).app c X)) X :=
  proj_monic T K F hF _ _ (fun i hi => Kc.ch_spec _ (locA_ex T K F Kc hF hC hT A _) i hi)

theorem tau_inv_r (A : SObj) {c : D.Ob} (t : ((FP (NFModel T K F Kc hF hC hT) A).obj c).car) :
    ((FP (NFModel T K F Kc hF hC hT) A).obj c).eqv ((tauN T K F Kc hF hC hT A).app c ((tauInv T K F Kc hF hC hT A).app c t)) t :=
  fun i hi => Kc.ch_spec _ (locA_ex T K F Kc hF hC hT A t) i hi

end Gt

end Found

#print axioms Found.Gt.LocA
#print axioms Found.Gt.locA_cov
#print axioms Found.Gt.locA_ex
#print axioms Found.Gt.tauInv
#print axioms Found.Gt.tau_inv_l
#print axioms Found.Gt.tau_inv_r

found/Gt68.lean

lean_all/found/Gt68.lean

/-
  T194 (classifying topos), part 68. found/Gt68 — the comparison
  F(A) ≅ F_{N_F}(A) is natural in A: for θ : A → B, the components of
  F(θ)(x) and of x satisfy θ in N_F (the graph of θ, read through the key
  lemma). Hence F ≅ F_{N_F} as functors.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt67

namespace Found

namespace Gt

open Sh

variable (T : Seq → Prop)

/-- the components of u ; θ followed by those of u, padded by v -/
def selG {E A B : SObj} (u : (SynCat T).Hom E A) (θ : (SynCat T).Hom A B) (v : (SynCat T).Hom E U1) (i : Nat) :
    (SynCat T).Hom E U1 :=
  match Nat.decLt i B.n with
  | isTrue h => (SynCat T).comp ((SynCat T).comp u θ) (projA T B i h)
  | isFalse _ => selA T u v (subN i B.n)

theorem selG_lt {E A B : SObj} (u : (SynCat T).Hom E A) (θ : (SynCat T).Hom A B) (v : (SynCat T).Hom E U1) {i : Nat}
    (h : i < B.n) : selG T u θ v i = (SynCat T).comp ((SynCat T).comp u θ) (projA T B i h) := by
  unfold selG
  match Nat.decLt i B.n with
  | isTrue _ => rfl
  | isFalse h' => exact absurd h h'

theorem selG_ge {E A B : SObj} (u : (SynCat T).Hom E A) (θ : (SynCat T).Hom A B) (v : (SynCat T).Hom E U1) (j : Nat)
    (h : j < A.n) : selG T u θ v (j + B.n) = (SynCat T).comp u (projA T A j h) := by
  unfold selG
  match Nat.decLt (j + B.n) B.n with
  | isTrue h' => exact absurd h' (Nat.not_lt_of_le (Nat.le_add_left B.n j))
  | isFalse _ =>
    show selA T u v (subN (j + B.n) B.n) = _
    rw [subN_add, selA_lt T u v h]

/-- values of the graph components satisfy θ -/
theorem sem_graph {E A B : SObj} (u : (SynCat T).Hom E A) (θ : (SynCat T).Hom A B) (v : (SynCat T).Hom E U1) :
    Sem T (B.n + A.n) E (selG T u θ v) θ.1 := by
  intro N c σ w hE hw
  refine force_local N.K N.M N.closed θ.1 w _ (exN_wit N.K N.M _ A.n σ (u.2.2.2.1 N c σ hE)) (fun d g ⟨ys, hl, fu⟩ => ?_)
  have hA := (u.2.2.1 N d _ ys hl fu).2
  refine force_local N.K N.M N.closed θ.1 _ _ (exN_wit N.K N.M _ B.n _ (θ.2.2.2.1 N d _ hA)) (fun d' g' ⟨zs, hzl, fθ⟩ => ?_)
  let σ'' := resV N.M g' (resV N.M g σ)
  let ys'' := lmapG (N.M.P.map g') ys
  have hyk : lenG ys'' = A.n := (len_lmapG _ ys).trans hl
  have fu' : force N.K N.M d' (extLV N.M σ'' ys'') u.1 :=
    force_resp N.K N.M _ (res_block N.M g' _ ys) (force_stable N.K N.M _ g' _ fu)
  have fθ' : force N.K N.M d' (extLV N.M (extLV N.M σ'' ys'') zs) θ.1 :=
    force_resp N.K N.M _ (extLV_eqv N.M (res_block N.M g' _ ys) zs) fθ
  have fuθ : force N.K N.M d' (extLV N.M σ'' zs) ((SynCat T).comp u θ).1 :=
    comp_intro T (Y := A) (Z := B) θ.2.1 N σ'' ys'' zs hyk hzl fu' fθ'
  let W := resV N.M g' (resV N.M g w)
  have hw' : ∀ i, i < B.n + A.n → force N.K N.M d' (extV N.M σ'' (W i)) (selG T u θ v i).1 := fun i hi =>
    force_resp N.K N.M _ (ext_res N.M g' _ _) (force_stable N.K N.M _ g' _
      (force_resp N.K N.M _ (ext_res N.M g σ (w i)) (force_stable N.K N.M _ g _ (hw i hi))))
  refine force_agree N.K N.M θ.1 (B.n + A.n) θ.2.1 (fun i hi => ?_) fθ'
  match Nat.decLt i B.n with
  | isTrue h =>
    have f1 := hw' i hi
    rw [selG_lt T u θ v h] at f1
    have f2 := comp_elim T ((SynCat T).comp u θ).2 (projA T B i h).2 N σ'' zs [W i] hzl rfl f1 fuθ
    rw [extLV_lt N.M (extLV N.M σ'' ys'') σ'' zs i (by rw [hzl]; exact h)]
    exact (N.M.P.obj d').symm ((force_projA T B i h N _ _).1 f2).2
  | isFalse h => match not_lt_split h with
    | ⟨j, eq⟩ =>
      subst eq
      have hj : j < A.n := Nat.lt_of_add_lt_add_right (by rw [Nat.add_comm B.n A.n] at hi; exact hi)
      have f1 := hw' (j + B.n) hi
      rw [selG_ge T u θ v j hj] at f1
      have f2 := comp_elim T u.2 (projA T A j hj).2 N σ'' ys'' [W (j + B.n)] hyk rfl f1 fu'
      rw [shift_val N.M _ zs B.n hzl j]
      exact (N.M.P.obj d').symm ((force_projA T A j hj N _ _).1 f2).2

variable {D : ECat.{0, 0}} (K : GTop D) (F : EFun (SynCat T) (ShCat K)) (Kc : CP.Classical')
  (hF : FlatF K F) (hC : ContF (JTc T Kc) K F) (hT : ∀ σ, T σ → ∃ k, FVg k σ.lhs ∧ FVg k σ.rhs)

/-- the components of x and of F(θ)(x) are related by θ in N_F -/
theorem tau_rel {A B : SObj} (θ : (SynCat T).Hom A B) {c : D.Ob} (x : ((F.obj A).1.obj c).car) :
    RelT (NFModel T K F Kc hF hC hT) θ.1 ((tauN T K F Kc hF hC hT A).app c x).1
      ((tauN T K F Kc hF hC hT B).app c ((F.map θ).app c x)).1 := by
  intro d f ρ
  let PU := (F.obj U1).1
  let N := NFModel T K F Kc hF hC hT
  let X := (tauN T K F Kc hF hC hT A).app c x
  let Y := (tauN T K F Kc hF hC hT B).app c ((F.map θ).app c x)
  let V := tv N (tv N ρ (resT N f X.1)) (resT N f Y.1)
  let xf := (F.obj A).1.map f x
  refine force_local K (NFStr T K F) (nf_closed T K F) θ.1 V _ (repA_cov T K F Kc hF hC A xf)
    (fun d2 g ⟨E, u, v, z, hu⟩ => ?_)
  have rep : RepAt T K F (B.n + A.n) (resV (NFStr T K F) g V) (selG T u θ v) z := fun i hi => by
    show (PU.obj d2).eqv _ (PU.map g (V i))
    match Nat.decLt i B.n with
    | isTrue h =>
      rw [selG_lt T u θ v h]
      have q : V i = PU.map f ((F.map (projA T B i h)).app c ((F.map θ).app c x)) := tv_lt N _ _ h
      rw [q]
      refine (PU.obj d2).trans (fcomp T K F _ _ z) ((PU.obj d2).trans ((F.map _).app_eqv d2
        ((F.obj B).1.obj d2 |>.trans (fcomp T K F u θ z) ((F.map θ).app_eqv d2 hu))) ?_)
      exact (PU.obj d2).trans ((F.map _).app_eqv d2 ((F.map θ).nat g xf)) ((PU.obj d2).trans ((F.map _).nat g _)
        (PU.map_eqv g ((PU.obj d).trans ((F.map _).app_eqv d ((F.map θ).nat f x)) ((F.map _).nat f _))))
    | isFalse h => match not_lt_split h with
      | ⟨j, eq⟩ =>
        subst eq
        have hj : j < A.n := Nat.lt_of_add_lt_add_right (by rw [Nat.add_comm B.n A.n] at hi; exact hi)
        rw [selG_ge T u θ v j hj]
        have q : V (j + B.n) = PU.map f ((F.map (projA T A j hj)).app c x) := by
          show tv N (tv N ρ (resT N f X.1)) (resT N f Y.1) (j + B.n) = _
          rw [tv_ge, tv_lt N ρ _ hj]
          rfl
        rw [q]
        exact (PU.obj d2).trans (fcomp T K F u _ z) ((PU.obj d2).trans ((F.map _).app_eqv d2 hu)
          ((PU.obj d2).trans ((F.map _).nat g xf) (PU.map_eqv g ((F.map _).nat f x))))
  exact (key_lemma T K F Kc hF hC θ.1 (B.n + A.n) θ.2.1 d2 _ E (selG T u θ v) z rep).2
    (cov_mono K (K.top d2) (fun _ h _ => ⟨E, (SynCat T).id E, (F.obj E).1.map h z, F.map_id E _ _,
      sem_heq T (fun i _ => (SynCat T).hsymm ((SynCat T).id_comp _)) (sem_graph T u θ v)⟩))

/-- naturality of the comparison -/
theorem tau_nat {A B : SObj} (θ : (SynCat T).Hom A B) {c : D.Ob} (x : ((F.obj A).1.obj c).car) :
    ((FP (NFModel T K F Kc hF hC hT) B).obj c).eqv
      ((FN (NFModel T K F Kc hF hC hT) Kc (nf_inh T K F Kc hF hC hT)).map θ |>.app c ((tauN T K F Kc hF hC hT A).app c x))
      ((tauN T K F Kc hF hC hT B).app c ((F.map θ).app c x)) :=
  relT_uniq _ (nf_inh T K F Kc hF hC hT) θ.2 (fmap_rel _ Kc (nf_inh T K F Kc hF hC hT) θ.2 _) (tau_rel T K F Kc hF hC hT θ x)

/-- F ≅ F_{N_F}: componentwise inverse natural maps, natural in the syntactic arrows -/
theorem f_iso_fnf :
    (∀ (A : SObj) {c : D.Ob} (X : ((F.obj A).1.obj c).car),
      ((F.obj A).1.obj c).eqv ((tauInv T K F Kc hF hC hT A).app c ((tauN T K F Kc hF hC hT A).app c X)) X) ∧
    (∀ (A : SObj) {c : D.Ob} (t : ((FP (NFModel T K F Kc hF hC hT) A).obj c).car),
      ((FP (NFModel T K F Kc hF hC hT) A).obj c).eqv
        ((tauN T K F Kc hF hC hT A).app c ((tauInv T K F Kc hF hC hT A).app c t)) t) ∧
    (∀ {A B : SObj} (θ : (SynCat T).Hom A B) {c : D.Ob} (x : ((F.obj A).1.obj c).car),
      ((FP (NFModel T K F Kc hF hC hT) B).obj c).eqv
        ((FN (NFModel T K F Kc hF hC hT) Kc (nf_inh T K F Kc hF hC hT)).map θ |>.app c ((tauN T K F Kc hF hC hT A).app c x))
        ((tauN T K F Kc hF hC hT B).app c ((F.map θ).app c x))) :=
  ⟨fun A _ X => tau_inv_l T K F Kc hF hC hT A X, fun A _ t => tau_inv_r T K F Kc hF hC hT A t,
   fun θ _ x => tau_nat T K F Kc hF hC hT θ x⟩

end Gt

end Found

#print axioms Found.Gt.sem_graph
#print axioms Found.Gt.tau_rel
#print axioms Found.Gt.tau_nat
#print axioms Found.Gt.f_iso_fnf

found/Gt69.lean

lean_all/found/Gt69.lean

/-
  T194 (classifying topos), part 69. found/Gt69 — the sheaf Ω_K of closed
  sieves: a sieve is closed when it contains every arrow along which it
  pulls back to a covering sieve. Closed sieves pulled back along arrows
  form a presheaf, and it is a K-sheaf (glue: g is in the glued sieve when
  the stages where the family contains the identity cover).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt68

namespace Found

namespace Gt

open Sh

variable {D : ECat.{0, 0}} (K : GTop D)

def ClosedS {c : D.Ob} (S : Sieve D c) : Prop := ∀ d (f : D.Hom d c), K.cov (pullS f S) → S.S f

theorem pull_comp_seq {c d e : D.Ob} (f : D.Hom d c) (g : D.Hom e d) (S : Sieve D c) :
    SEq (pullS g (pullS f S)) (pullS (D.comp g f) S) := fun _ h =>
  ⟨fun x => S.resp (D.assoc h g f) x, fun x => S.resp (D.hsymm (D.assoc h g f)) x⟩

/-- the presheaf of closed sieves -/
def OmP : Psh.{0, 0, 0} D where
  obj c := ⟨{S : Sieve D c // ClosedS K S}, fun S T => SEq S.1 T.1, fun _ _ _ => Iff.rfl,
    fun h d f => (h d f).symm, fun h k d f => (h d f).trans (k d f)⟩
  map f S := ⟨pullS f S.1, fun _ g h => S.2 _ _ (K.resp (pull_comp_seq f g S.1) h)⟩
  map_eqv f _ _ h _ g := h _ (D.comp g f)
  map_resp S h _ g := ⟨fun x => S.1.resp (D.comp_resp (D.hrefl g) h) x, fun x => S.1.resp (D.comp_resp (D.hrefl g) (D.hsymm h)) x⟩
  map_id _ S _ g := ⟨fun x => S.1.resp (D.comp_id g) x, fun x => S.1.resp (D.hsymm (D.comp_id g)) x⟩
  map_comp f g S _ h := ⟨fun x => S.1.resp (D.hsymm (D.assoc h f g)) x, fun x => S.1.resp (D.assoc h f g) x⟩

section Glue

variable {c : D.Ob} (R : Sieve D c) (m : MFam (OmP K) R)

/-- the family contains the identity at the arrow a -/
def MemX {e : D.Ob} (a : D.Hom e c) : Prop := ∃ hR : R.S a, (m.x a hR).1.S (D.id e)

theorem memX_resp {e : D.Ob} {a b : D.Hom e c} (h : D.heq a b) (x : MemX K R m a) : MemX K R m b :=
  match x with
  | ⟨hR, hx⟩ => ⟨R.resp h hR, (m.resp hR (R.resp h hR) h _ (D.id e)).1 hx⟩

/-- MemX at a restriction of a member -/
theorem memX_of {e e' : D.Ob} (a : D.Hom e c) (hR : R.S a) (k : D.Hom e' e) (hk : (m.x a hR).1.S k) :
    MemX K R m (D.comp k a) :=
  ⟨R.stable hR k, (m.compat a hR k _ (D.id e')).2 ((m.x a hR).1.resp (D.hsymm (D.id_comp k)) hk)⟩

theorem memX_to {e e' : D.Ob} (a : D.Hom e c) (hR : R.S a) (k : D.Hom e' e) (x : MemX K R m (D.comp k a)) :
    (m.x a hR).1.S k :=
  match x with
  | ⟨hR', hx⟩ => (m.x a hR).1.resp (D.id_comp k) ((m.compat a hR k _ (D.id e')).1
      ((m.resp hR' (R.stable hR k) (D.hrefl _) _ (D.id e')).1 hx))

def GSv {e : D.Ob} (g : D.Hom e c) : Sieve D e where
  S := fun {_} h => MemX K R m (D.comp h g)
  resp := fun e x => memX_resp K R m (D.comp_resp e (D.hrefl g)) x
  stable := fun {_ _} {h} x k => match x with
    | ⟨hR, hx⟩ => memX_resp K R m (D.hsymm (D.assoc k h g))
        (memX_of K R m (D.comp h g) hR k (((m.x _ hR).1).resp (D.comp_id k) (((m.x _ hR).1).stable hx k)))

def glueS : Sieve D c where
  S := fun {_} g => K.cov (GSv K R m g)
  resp := fun {_} {f g} e h => K.resp (S := GSv K R m f) (T := GSv K R m g) (fun _ k => ⟨memX_resp K R m (D.comp_resp (D.hrefl k) e),
    memX_resp K R m (D.comp_resp (D.hrefl k) (D.hsymm e))⟩) h
  stable := fun {_ _} {g} h k => cov_mono K (K.pull k h) (fun _ j x => memX_resp K R m (D.assoc j k g) x)

theorem glueS_closed : ClosedS K (glueS K R m) := fun _ g h =>
  K.trans h (fun _ k hk => cov_mono K hk (fun _ j x => memX_resp K R m (D.hsymm (D.assoc j k g)) x))

end Glue

theorem omP_sheaf : IsSheaf K (OmP K) := by
  intro c R hR m
  refine ⟨⟨⟨glueS K R m, glueS_closed K R m⟩, fun d f hf e h => ⟨fun x => ?_, fun x => ?_⟩⟩, fun a b ha hb e g => ?_⟩
  · refine (m.x f hf).2 e h (cov_mono K x (fun _ k y => ?_))
    exact memX_to K R m f hf (D.comp k h) (memX_resp K R m (D.hsymm (D.assoc k h f)) y)
  · exact cov_mono K (K.top e) (fun _ k _ => memX_resp K R m (D.assoc k h f)
      (memX_of K R m f hf (D.comp k h) ((m.x f hf).1.stable x k)))
  · have dir : ∀ (a b : ((OmP K).obj c).car), Amalg (OmP K) m a → Amalg (OmP K) m b →
        a.1.S g → b.1.S g := fun a b ha hb x =>
      b.2 e g (cov_mono K (K.pull g hR) (fun e' h hRh => by
        have xa : (pullS (D.comp h g) a.1).S (D.id e') := a.1.resp (D.hsymm (D.id_comp _)) (a.1.stable x h)
        have xm := (ha e' (D.comp h g) hRh e' (D.id e')).1 xa
        have xb := (hb e' (D.comp h g) hRh e' (D.id e')).2 xm
        exact b.1.resp (D.id_comp _) xb))
    exact ⟨dir a b ha hb, dir b a hb ha⟩

end Gt

end Found

#print axioms Found.Gt.OmP
#print axioms Found.Gt.glueS_closed
#print axioms Found.Gt.omP_sheaf

found/Gt7.lean

lean_all/found/Gt7.lean

/-
  T194 (classifying topos), part 7. found/Gt7 — local character of
  forcing in sheaf structures: if the carrier is a sheaf (separated
  suffices) and the relations are J-closed, then a formula forced on a
  covering sieve is forced. Geometric theories (sequents φ ⊢ ψ) and their
  models in sheaves: every sequent is forced at every stage.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt6

namespace Found

namespace Gt

open Sh

variable {C : ECat.{0, 0}} (J : GTop C) (M : @GStr C)

/-- J-closed relations and a separated carrier -/
structure Closed : Prop where
  sep : Separated J M.P
  rel_loc : ∀ R c (l : List (M.P.obj c).car) (S : Sieve C c), J.cov S →
    (∀ d (f : C.Hom d c), S.S f → M.rel R d (lmapG (M.P.map f) l)) → M.rel R c l

/-- the sieve of stages where some disjunct is forced -/
def supS {c : C.Ob} (ρ : Nat → (M.P.obj c).car) (φ : Nat → GFm) : Sieve C c where
  S := fun g => ∃ i, force J M _ (resV M g ρ) (φ i)
  resp := fun e ⟨i, fi⟩ => ⟨i, force_resp J M (φ i) (fun n => M.P.map_resp (ρ n) e) fi⟩
  stable := fun ⟨i, fi⟩ k => ⟨i, force_resp J M (φ i) (res_comp M _ k ρ) (force_stable J M (φ i) k _ fi)⟩

/-- the sieve of stages with a witness -/
def exS {c : C.Ob} (ρ : Nat → (M.P.obj c).car) (φ : GFm) : Sieve C c where
  S := fun g => ∃ a, force J M _ (extV M (resV M g ρ) a) φ
  resp := fun e ⟨a, fa⟩ => ⟨a, force_resp J M φ (extV_eqv M (fun n => M.P.map_resp (ρ n) e) a) fa⟩
  stable := fun {_ _} {g} ⟨a, fa⟩ k => ⟨M.P.map k a, force_resp J M φ
      (ρ := resV M k (extV M (resV M g ρ) a)) (ρ' := extV M (resV M (C.comp k g) ρ) (M.P.map k a))
      (fun n => match n with
      | 0 => (M.P.obj _).refl _
      | n + 1 => res_comp M g k ρ n) (force_stable J M φ k _ fa)⟩

/-- local character -/
theorem force_local (hc : Closed J M) : (φ : GFm) → ∀ {c : C.Ob} (ρ : Nat → (M.P.obj c).car) (S : Sieve C c),
    J.cov S → (∀ d (f : C.Hom d c), S.S f → force J M d (resV M f ρ) φ) → force J M c ρ φ
  | .atom R l, c, ρ, S, hS, h => hc.rel_loc R c _ S hS (fun d f hf => by
      have := h d f hf
      show M.rel R d (lmapG (M.P.map f) (lmapG ρ l))
      rw [lmapG_comp]
      exact this)
  | .eq i j, _, ρ, S, hS, h => hc.sep _ S hS (ρ i) (ρ j) (fun d f hf => h d f hf)
  | .top, _, _, _, _, _ => trivial
  | .conj a b, c, ρ, S, hS, h => ⟨force_local hc a ρ S hS (fun d f hf => (h d f hf).1),
      force_local hc b ρ S hS (fun d f hf => (h d f hf).2)⟩
  | .sup φ, _, ρ, S, hS, h => ⟨supS J M ρ φ, J.trans hS (fun d f hf => match h d f hf with
      | ⟨T, hT, g⟩ => cov_mono J hT (fun e k hk => match g e k hk with
        | ⟨i, fi⟩ => ⟨i, force_resp J M (φ i) (res_comp M f k ρ) fi⟩)), fun _ _ hg => hg⟩
  | .ex φ, _, ρ, S, hS, h => ⟨exS J M ρ φ, J.trans hS (fun d f hf => match h d f hf with
      | ⟨T, hT, g⟩ => cov_mono J hT (fun e k hk => match g e k hk with
        | ⟨a, fa⟩ => ⟨a, force_resp J M φ (extV_eqv M (res_comp M f k ρ) a) fa⟩)), fun _ _ hg => hg⟩

/-- a geometric sequent φ ⊢ ψ (both in the same variables) -/
structure Seq where
  lhs : GFm
  rhs : GFm

/-- a model of a geometric theory in presheaves on the site -/
def IsModel (T : Seq → Prop) : Prop := ∀ σ, T σ → ∀ c (ρ : Nat → (M.P.obj c).car), force J M c ρ σ.lhs → force J M c ρ σ.rhs

end Gt

end Found

#print axioms Found.Gt.supS
#print axioms Found.Gt.exS
#print axioms Found.Gt.force_local

found/Gt70.lean

lean_all/found/Gt70.lean

/-
  T194 (classifying topos), part 70. found/Gt70 — in the sheaf category
  Sh(D, K), a jointly epimorphic family of maps into Q is jointly locally
  surjective: compare the characteristic map of the locally-hit elements
  with "true", as maps Q → Ω_K.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt69

namespace Found

namespace Gt

open Sh

variable {D : ECat.{0, 0}} (K : GTop D)

section F

variable {I : Type} {P : I → (ShCat K).Ob} {Q : (ShCat K).Ob} (α : (i : I) → (ShCat K).Hom (P i) Q)

/-- the stages where q is hit by the family -/
def HitQ {c : D.Ob} (q : (Q.1.obj c).car) : Sieve D c where
  S := fun {d} g => ∃ (i : I) (y : ((P i).1.obj d).car), (Q.1.obj d).eqv ((α i).app d y) (Q.1.map g q)
  resp := fun e ⟨i, y, h⟩ => ⟨i, y, (Q.1.obj _).trans h (Q.1.map_resp q e)⟩
  stable := fun {_ _} {g} ⟨i, y, h⟩ k => ⟨i, (P i).1.map k y, (Q.1.obj _).trans ((α i).nat k y)
    ((Q.1.obj _).trans (Q.1.map_eqv k h) ((Q.1.obj _).symm (Q.1.map_comp k g q)))⟩

def JEpi : Prop :=
  ∀ (R : (ShCat K).Ob) (β γ : (ShCat K).Hom Q R),
    (∀ i, (ShCat K).heq ((ShCat K).comp (α i) β) ((ShCat K).comp (α i) γ)) → (ShCat K).heq β γ

theorem hitQ_move {c d : D.Ob} {q q' : (Q.1.obj c).car} (h : (Q.1.obj c).eqv q q') {g g' : D.Hom d c}
    (e : D.heq g g') : SEq (HitQ K α (Q.1.map g q)) (HitQ K α (Q.1.map g' q')) := fun _ k =>
  ⟨fun ⟨i, y, hy⟩ => ⟨i, y, (Q.1.obj _).trans hy (Q.1.map_eqv k ((Q.1.obj _).trans (Q.1.map_eqv g h) (Q.1.map_resp q' e)))⟩,
   fun ⟨i, y, hy⟩ => ⟨i, y, (Q.1.obj _).trans hy (Q.1.map_eqv k ((Q.1.obj _).symm
      ((Q.1.obj _).trans (Q.1.map_eqv g h) (Q.1.map_resp q' e))))⟩⟩

theorem hitQ_pull {c d e : D.Ob} (q : (Q.1.obj c).car) (g : D.Hom d c) (k : D.Hom e d) :
    SEq (pullS k (HitQ K α (Q.1.map g q))) (HitQ K α (Q.1.map (D.comp k g) q)) := fun _ j =>
  ⟨fun ⟨i, y, hy⟩ => ⟨i, y, (Q.1.obj _).trans hy ((Q.1.obj _).trans (Q.1.map_comp j k _)
      (Q.1.map_eqv j ((Q.1.obj _).symm (Q.1.map_comp k g q))))⟩,
   fun ⟨i, y, hy⟩ => ⟨i, y, (Q.1.obj _).trans hy ((Q.1.obj _).trans (Q.1.map_eqv j (Q.1.map_comp k g q))
      ((Q.1.obj _).symm (Q.1.map_comp j k _)))⟩⟩

theorem hitQ_eq {c : D.Ob} {q q' : (Q.1.obj c).car} (h : (Q.1.obj c).eqv q q') : SEq (HitQ K α q) (HitQ K α q') :=
  fun _ j => ⟨fun ⟨i, y, hy⟩ => ⟨i, y, (Q.1.obj _).trans hy (Q.1.map_eqv j h)⟩,
    fun ⟨i, y, hy⟩ => ⟨i, y, (Q.1.obj _).trans hy (Q.1.map_eqv j ((Q.1.obj _).symm h))⟩⟩

/-- the characteristic sieve of the locally-hit part -/
def ChiS {c : D.Ob} (q : (Q.1.obj c).car) : Sieve D c where
  S := fun {_} g => K.cov (HitQ K α (Q.1.map g q))
  resp := fun e h => K.resp (hitQ_move K α ((Q.1.obj _).refl q) e) h
  stable := fun {_ _} {g} h k => K.resp (hitQ_pull K α q g k) (K.pull k h)

theorem chiS_closed {c : D.Ob} (q : (Q.1.obj c).car) : ClosedS K (ChiS K α q) := fun _ h hc =>
  K.trans hc (fun _ k hk => K.resp (fun _ j => (hitQ_pull K α q h k _ j).symm) hk)

/-- the characteristic map Q → Ω_K -/
def chiN : PNat Q.1 (OmP K) where
  app _ q := ⟨ChiS K α q, chiS_closed K α q⟩
  app_eqv _ {_ _} h _ g := ⟨fun x => K.resp (hitQ_move K α h (D.hrefl g)) x,
    fun x => K.resp (hitQ_move K α ((Q.1.obj _).symm h) (D.hrefl g)) x⟩
  nat f q _ g := ⟨fun x => K.resp (hitQ_eq K α ((Q.1.obj _).symm (Q.1.map_comp g f q))) x,
    fun x => K.resp (hitQ_eq K α (Q.1.map_comp g f q)) x⟩

/-- the constant map "true" -/
def trueN : PNat Q.1 (OmP K) where
  app c _ := ⟨maxS c, fun _ _ _ => trivial⟩
  app_eqv _ {_ _} _ _ _ := Iff.rfl
  nat _ _ _ _ := Iff.rfl

theorem jepi_loc (h : JEpi K α) {c : D.Ob} (q : (Q.1.obj c).car) : K.cov (HitQ K α q) := by
  have e := h ⟨OmP K, omP_sheaf K⟩ (chiN K α) (trueN K) (fun i d y _ g => ⟨fun _ => trivial, fun _ =>
    cov_mono K (K.top _) (fun _ k _ => ⟨i, (P i).1.map (D.comp k g) y, (Q.1.obj _).trans ((α i).nat _ y)
      ((Q.1.map_comp k g _))⟩)⟩)
  have x := (e c q _ (D.id c)).2 trivial
  exact K.resp (hitQ_eq K α (Q.1.map_id c q)) x

end F

end Gt

end Found

#print axioms Found.Gt.chiN
#print axioms Found.Gt.jepi_loc

found/Gt71.lean

lean_all/found/Gt71.lean

/-
  T194 (classifying topos), part 71. found/Gt71 — jointly epimorphic
  families of representables in a subcanonical site: the arrows of a
  covering sieve, and all elements of a sheaf (Yoneda); left adjoints
  preserve jointly epimorphic families.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt70

namespace Found

namespace Gt

open Sh

/-- jointly epimorphic family in any E-category -/
def JEpiC {C : ECat.{u, v}} {I : Type} {P : I → C.Ob} {Q : C.Ob} (α : (i : I) → C.Hom (P i) Q) : Prop :=
  ∀ (R : C.Ob) (β γ : C.Hom Q R), (∀ i, C.heq (C.comp (α i) β) (C.comp (α i) γ)) → C.heq β γ

theorem adj_jepi {C : ECat.{u, v}} {D : ECat.{u', v'}} {L : EFun C D} {R : EFun D C} (ad : Adj L R)
    {I : Type} {P : I → C.Ob} {Q : C.Ob} (α : (i : I) → C.Hom (P i) Q) (h : JEpiC α) :
    JEpiC (fun i => L.map (α i)) := fun _ β γ hβγ =>
  D.htrans (D.hsymm (ad.psi_phi β)) (D.htrans (ad.psi_resp (h _ (ad.phi β) (ad.phi γ) (fun i =>
    C.htrans (C.hsymm (ad.nat_l (α i) β)) (C.htrans (ad.phi_resp (hβγ i)) (ad.nat_l (α i) γ))))) (ad.psi_phi γ))

variable {C : ECat.{0, 0}} (J : GTop C)

/-- postcomposition with u -/
def yoM {A B : C.Ob} (u : C.Hom B A) : PNat (yo C B) (yo C A) where
  app _ g := C.comp g u
  app_eqv _ {_ _} h := C.comp_resp h (C.hrefl u)
  nat f g := C.assoc f g u

/-- the map from a representable classifying an element -/
def yoEl {P : Psh.{0, 0, 0} C} {E : C.Ob} (p : (P.obj E).car) : PNat (yo C E) P where
  app _ g := P.map g p
  app_eqv _ {_ _} h := P.map_resp p h
  nat f g := P.map_comp f g p

variable (hsub : ∀ A, IsSheaf J (yo C A))

/-- the arrows of a covering sieve are jointly epimorphic -/
theorem cov_jepi {A : C.Ob} (S : Sieve C A) (hS : J.cov S) :
    JEpiC (C := ShCat J) (I := Σ' (B : C.Ob) (u : C.Hom B A), S.S u) (P := fun i => ⟨yo C i.1, hsub _⟩)
      (Q := ⟨yo C A, hsub A⟩) (fun i => yoM i.2.1) := by
  intro R β γ h
  have hb : (R.1.obj A).eqv (β.app A (C.id A)) (γ.app A (C.id A)) :=
    sheaf_sep J R.1 R.2 A S hS _ _ (fun B u hu => (R.1.obj B).trans ((R.1.obj B).symm (β.nat u (C.id A)))
      ((R.1.obj B).trans (β.app_eqv B (C.comp_id u)) ((R.1.obj B).trans
        ((R.1.obj B).trans (β.app_eqv B (C.hsymm (C.id_comp u))) (h ⟨B, u, hu⟩ B (C.id B)))
        ((R.1.obj B).trans (γ.app_eqv B (C.id_comp u)) ((R.1.obj B).trans (γ.app_eqv B (C.hsymm (C.comp_id u)))
          (γ.nat u (C.id A)))))))
  intro E g
  exact (R.1.obj E).trans (β.app_eqv E (C.hsymm (C.comp_id g))) ((R.1.obj E).trans (β.nat g (C.id A))
    ((R.1.obj E).trans (R.1.map_eqv g hb) ((R.1.obj E).trans ((R.1.obj E).symm (γ.nat g (C.id A)))
      (γ.app_eqv E (C.comp_id g)))))

/-- all elements of a sheaf are jointly epimorphic -/
theorem all_jepi (P : (ShCat J).Ob) :
    JEpiC (C := ShCat J) (I := Σ' (E : C.Ob), (P.1.obj E).car) (P := fun i => ⟨yo C i.1, hsub _⟩) (Q := P)
      (fun i => yoEl i.2) := fun R β γ h c x =>
  (R.1.obj c).trans (β.app_eqv c ((P.1.obj c).symm (P.1.map_id c x)))
    ((R.1.obj c).trans (h ⟨c, x⟩ c (C.id c)) (γ.app_eqv c (P.1.map_id c x)))

end Gt

end Found

#print axioms Found.Gt.adj_jepi
#print axioms Found.Gt.cov_jepi
#print axioms Found.Gt.all_jepi

found/Gt72.lean

lean_all/found/Gt72.lean

/-
  T194 (classifying topos), part 72. found/Gt72 — Diaconescu, first half:
  a left exact geometric morphism f : Sh(D, K) → Sh(C, J) over a
  subcanonical site gives the functor f* ∘ y : C → Sh(D, K), which is
  continuous (covering sieves are jointly epimorphic, f* preserves that,
  and jointly epimorphic families of sheaves are locally surjective) and
  flat (f* preserves the terminal object, products and equalizers, each
  covered by representables).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt71

namespace Found

namespace Gt

open Sh

variable {C : ECat.{0, 0}} (J : GTop C) (hsub : ∀ A, IsSheaf J (yo C A))

/-- the Yoneda embedding into sheaves -/
def yoE : EFun C (ShCat J) where
  obj A := ⟨yo C A, hsub A⟩
  map u := yoM u
  map_resp h _ g := C.comp_resp (C.hrefl g) h
  map_id _ _ g := C.comp_id g
  map_comp _ _ _ h := C.hsymm (C.assoc h _ _)

/-- the inclusion of an equalizer -/
def evN {F G : Psh.{0, 0, 0} C} (α β : PNat F G) : PNat (eqP α β) F where
  app _ x := evE x
  app_eqv _ {_ _} h := h
  nat _ _ := (F.obj _).refl _

variable {D : ECat.{0, 0}} (K : GTop D)

/-- f* preserves equalizers (elementwise) -/
def EqPres (f : Geom J K) : Prop :=
  ∀ (P Q : (ShCat J).Ob) (α β : (ShCat J).Hom P Q) (c : D.Ob) (x : ((f.inv.obj P).1.obj c).car),
    ((f.inv.obj Q).1.obj c).eqv ((f.inv.map α).app c x) ((f.inv.map β).app c x) →
    ∃ w : ((f.inv.obj ⟨eqP α β, eqP_sheaf J P.2 Q.2 α β⟩).1.obj c).car,
      ((f.inv.obj P).1.obj c).eqv ((f.inv.map (C := ShCat J) (a := ⟨eqP α β, eqP_sheaf J P.2 Q.2 α β⟩) (b := P) (evN α β)).app c w) x

/-- the functor of a geometric morphism -/
def Ff (f : Geom J K) : EFun C (ShCat K) := EFun.compF (yoE J hsub) f.inv

theorem ff_cont (f : Geom J K) : ContF J K (Ff J hsub K f) := fun A S hS c x => by
  have je := adj_jepi f.adj _ (cov_jepi J hsub S hS)
  exact cov_mono K (jepi_loc K _ je x) (fun _ _ ⟨i, y, hy⟩ => ⟨i.1, i.2.1, i.2.2, y, hy⟩)

theorem ff_ne (f : Geom J K) (c : D.Ob) : K.cov (NeS K (Ff J hsub K f) c) := by
  match f.term_iso with
  | ⟨g, _⟩ =>
  have je := adj_jepi f.adj _ (all_jepi J hsub (shTerminal J).one)
  exact cov_mono K (jepi_loc K _ je (g.app c PUnit.unit)) (fun _ _ ⟨i, y, _⟩ => ⟨i.1, y, trivial⟩)

theorem ff_pair (f : Geom J K) {A B : C.Ob} {c : D.Ob} (x : (((Ff J hsub K f).obj A).1.obj c).car)
    (y : (((Ff J hsub K f).obj B).1.obj c).car) : K.cov (PairS K (Ff J hsub K f) x y) := by
  let Pr := shProduct J ((yoE J hsub).obj A) ((yoE J hsub).obj B)
  match f.prod_iso ((yoE J hsub).obj A) ((yoE J hsub).obj B) with
  | ⟨g, hg⟩ =>
  let w := g.app c (x, y)
  have hw := hg c (x, y)
  have je := adj_jepi f.adj _ (all_jepi J hsub Pr.P)
  refine cov_mono K (jepi_loc K _ je w) (fun d g' ⟨i, z, hz⟩ => ⟨i.1, i.2.1, i.2.2, z, ?_, ?_⟩)
  · have e1 : (ShCat J).heq (a := (yoE J hsub).obj i.1) (b := (yoE J hsub).obj A) (yoM (C := C) i.2.1)
        ((ShCat J).comp (a := (yoE J hsub).obj i.1) (b := Pr.P) (yoEl (P := Pr.P.1) i.2) Pr.p1) :=
      fun _ _ => C.hrefl _
    exact ((f.inv.obj _).1.obj d).trans (f.inv.map_resp e1 d z) ((((f.inv.obj _).1.obj d)).trans
      (f.inv.map_comp _ _ d z) (((f.inv.obj _).1.obj d).trans ((f.inv.map Pr.p1).app_eqv d hz)
        (((f.inv.obj _).1.obj d).trans ((f.inv.map Pr.p1).nat g' w) ((f.inv.obj _).1.map_eqv g' hw.1))))
  · have e2 : (ShCat J).heq (a := (yoE J hsub).obj i.1) (b := (yoE J hsub).obj B) (yoM (C := C) i.2.2)
        ((ShCat J).comp (a := (yoE J hsub).obj i.1) (b := Pr.P) (yoEl (P := Pr.P.1) i.2) Pr.p2) :=
      fun _ _ => C.hrefl _
    exact ((f.inv.obj _).1.obj d).trans (f.inv.map_resp e2 d z) ((((f.inv.obj _).1.obj d)).trans
      (f.inv.map_comp _ _ d z) (((f.inv.obj _).1.obj d).trans ((f.inv.map Pr.p2).app_eqv d hz)
        (((f.inv.obj _).1.obj d).trans ((f.inv.map Pr.p2).nat g' w) ((f.inv.obj _).1.map_eqv g' hw.2))))

theorem ff_eqz (f : Geom J K) (hE : EqPres J K f) {A B : C.Ob} (u v : C.Hom A B) {c : D.Ob}
    (x : (((Ff J hsub K f).obj A).1.obj c).car)
    (h : (((Ff J hsub K f).obj B).1.obj c).eqv (((Ff J hsub K f).map u).app c x) (((Ff J hsub K f).map v).app c x)) :
    K.cov (EqzS K (Ff J hsub K f) u v x) := by
  let Eq : (ShCat J).Ob := ⟨eqP (yoM (C := C) u) (yoM v), eqP_sheaf J (hsub A) (hsub B) _ _⟩
  match hE ((yoE J hsub).obj A) ((yoE J hsub).obj B) (yoM u) (yoM v) c x h with
  | ⟨w, hw⟩ =>
  have je := adj_jepi f.adj _ (all_jepi J hsub Eq)
  refine cov_mono K (jepi_loc K _ je w) (fun d g' ⟨i, z, hz⟩ => ⟨i.1, evE i.2, z, evP i.2, ?_⟩)
  have e1 : (ShCat J).heq (a := (yoE J hsub).obj i.1) (b := (yoE J hsub).obj A) (yoM (C := C) (evE i.2))
      ((ShCat J).comp (a := (yoE J hsub).obj i.1) (b := Eq) (c := (yoE J hsub).obj A) (yoEl (P := Eq.1) i.2)
        (evN (yoM u) (yoM v))) :=
    fun _ _ => C.hrefl _
  exact ((f.inv.obj _).1.obj d).trans (f.inv.map_resp e1 d z) ((((f.inv.obj _).1.obj d)).trans
    (f.inv.map_comp _ _ d z) (((f.inv.obj _).1.obj d).trans ((f.inv.map (a := Eq) (b := (yoE J hsub).obj A) (evN (yoM u) (yoM v))).app_eqv d hz)
      (((f.inv.obj _).1.obj d).trans ((f.inv.map (a := Eq) (b := (yoE J hsub).obj A) (evN (yoM u) (yoM v))).nat g' w) ((f.inv.obj _).1.map_eqv g' hw))))

theorem ff_flat (f : Geom J K) (hE : EqPres J K f) : FlatF K (Ff J hsub K f) :=
  ⟨ff_ne J hsub K f, fun x y => ff_pair J hsub K f x y, fun u v _ x h => ff_eqz J hsub K f hE u v x h⟩

end Gt

end Found

#print axioms Found.Gt.yoE
#print axioms Found.Gt.ff_cont
#print axioms Found.Gt.ff_ne
#print axioms Found.Gt.ff_pair
#print axioms Found.Gt.ff_eqz
#print axioms Found.Gt.ff_flat

found/Gt73.lean

lean_all/found/Gt73.lean

/-
  T194 (classifying topos), part 73. found/Gt73 — Diaconescu, second half:
  the tensor presheaf P ⊗ F for a presheaf P on C and a flat functor
  F : C → Sh(D, K): triples (A, p ∈ P(A), x ∈ F(A)(c)), two triples being
  equivalent when, locally, they come from one triple along arrows
  u, v (F(u)z ≈ x, F(v)z ≈ x', P(u)p ≈ P(v)p'). Flatness makes this an
  equivalence relation.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt72

namespace Found

namespace Gt

open Sh

variable {C : ECat.{0, 0}} {D : ECat.{0, 0}} (K : GTop D) (F : EFun C (ShCat K)) (P : Psh.{0, 0, 0} C)

/-- chaining two factorizations, for any functor into sheaves -/
theorem fcmp {E E1 E3 : C.Ob} (w1 : C.Hom E1 E) (u : C.Hom E3 E1) {c d d' : D.Ob}
    (g : D.Hom d c) (g' : D.Hom d' d) {e : ((F.obj E).1.obj c).car} {e1 : ((F.obj E1).1.obj d).car}
    {e3 : ((F.obj E3).1.obj d').car} (h1 : ((F.obj E).1.obj d).eqv ((F.map w1).app d e1) ((F.obj E).1.map g e))
    (h3 : ((F.obj E1).1.obj d').eqv ((F.map u).app d' e3) ((F.obj E1).1.map g' e1)) :
    ((F.obj E).1.obj d').eqv ((F.map (C.comp u w1)).app d' e3) ((F.obj E).1.map (D.comp g' g) e) :=
  ((F.obj E).1.obj d').trans (F.map_comp u w1 d' e3) (((F.obj E).1.obj d').trans ((F.map w1).app_eqv d' h3)
    (((F.obj E).1.obj d').trans ((F.map w1).nat g' e1) (((F.obj E).1.obj d').trans ((F.obj E).1.map_eqv g' h1)
      (((F.obj E).1.obj d').symm ((F.obj E).1.map_comp g' g e)))))

structure Tri (c : D.Ob) where
  A : C.Ob
  p : (P.obj A).car
  x : ((F.obj A).1.obj c).car

def EqS {c : D.Ob} (t t' : Tri K F P c) : Sieve D c where
  S := fun {d} g => ∃ (E : C.Ob) (u : C.Hom E t.A) (v : C.Hom E t'.A) (z : ((F.obj E).1.obj d).car),
    ((F.obj t.A).1.obj d).eqv ((F.map u).app d z) ((F.obj t.A).1.map g t.x) ∧
    ((F.obj t'.A).1.obj d).eqv ((F.map v).app d z) ((F.obj t'.A).1.map g t'.x) ∧
    (P.obj E).eqv (P.map u t.p) (P.map v t'.p)
  resp := fun h ⟨E, u, v, z, h1, h2, h3⟩ => ⟨E, u, v, z, ((F.obj t.A).1.obj _).trans h1 ((F.obj t.A).1.map_resp _ h),
    ((F.obj t'.A).1.obj _).trans h2 ((F.obj t'.A).1.map_resp _ h), h3⟩
  stable := fun {_ _} {g} ⟨E, u, v, z, h1, h2, h3⟩ k => ⟨E, u, v, (F.obj E).1.map k z,
    ((F.obj t.A).1.obj _).trans ((F.map u).nat k z) (((F.obj t.A).1.obj _).trans ((F.obj t.A).1.map_eqv k h1)
      (((F.obj t.A).1.obj _).symm ((F.obj t.A).1.map_comp k g _))),
    ((F.obj t'.A).1.obj _).trans ((F.map v).nat k z) (((F.obj t'.A).1.obj _).trans ((F.obj t'.A).1.map_eqv k h2)
      (((F.obj t'.A).1.obj _).symm ((F.obj t'.A).1.map_comp k g _))), h3⟩

def LocEq {c : D.Ob} (t t' : Tri K F P c) : Prop := K.cov (EqS K F P t t')

theorem locEq_refl {c : D.Ob} (t : Tri K F P c) : LocEq K F P t t :=
  cov_mono K (K.top c) (fun _ g _ => ⟨t.A, C.id t.A, C.id t.A, (F.obj t.A).1.map g t.x, F.map_id t.A _ _,
    F.map_id t.A _ _, (P.obj t.A).refl _⟩)

theorem locEq_symm {c : D.Ob} {t t' : Tri K F P c} (h : LocEq K F P t t') : LocEq K F P t' t :=
  cov_mono K h (fun _ _ ⟨E, u, v, z, h1, h2, h3⟩ => ⟨E, v, u, z, h2, h1, (P.obj E).symm h3⟩)

theorem locEq_trans (hF : FlatF K F) {c : D.Ob} {t t' t'' : Tri K F P c} (h : LocEq K F P t t')
    (h' : LocEq K F P t' t'') : LocEq K F P t t'' := by
  refine K.trans (cov_meet K h h') (fun d g ⟨⟨E1, u1, v1, z1, a1, b1, c1⟩, ⟨E2, u2, v2, z2, a2, b2, c2⟩⟩ => ?_)
  refine K.trans (hF.pair z1 z2) (fun d' g' ⟨E3, a, b, z3, ha, hb⟩ => ?_)
  let PA' := (F.obj t'.A).1
  have q1 : (PA'.obj d).eqv ((F.map v1).app d z1) ((F.map u2).app d z2) := (PA'.obj d).trans b1 ((PA'.obj d).symm a2)
  have q2 : (PA'.obj d').eqv ((F.map (C.comp a v1)).app d' z3) (PA'.map g' ((F.map v1).app d z1)) :=
    (PA'.obj d').trans (F.map_comp a v1 d' z3) ((PA'.obj d').trans ((F.map v1).app_eqv d' ha) ((F.map v1).nat g' z1))
  have q3 : (PA'.obj d').eqv ((F.map (C.comp b u2)).app d' z3) (PA'.map g' ((F.map u2).app d z2)) :=
    (PA'.obj d').trans (F.map_comp b u2 d' z3) ((PA'.obj d').trans ((F.map u2).app_eqv d' hb) ((F.map u2).nat g' z2))
  have m : (PA'.obj d').eqv ((F.map (C.comp a v1)).app d' z3) ((F.map (C.comp b u2)).app d' z3) :=
    (PA'.obj d').trans q2 ((PA'.obj d').trans (PA'.map_eqv g' q1) ((PA'.obj d').symm q3))
  refine cov_mono K (hF.eqz _ _ z3 m) (fun d'' g'' ⟨E4, w, z4, hw, hz⟩ => ⟨E4, C.comp w (C.comp a u1),
    C.comp w (C.comp b v2), z4, ?_, ?_, ?_⟩)
  · exact ((F.obj t.A).1.obj d'').trans (fcmp K F (C.comp a u1) w (D.comp g' g) g'' (fcmp K F u1 a g g' a1 ha) hz)
      ((F.obj t.A).1.map_resp _ (D.hsymm (D.assoc g'' g' g)))
  · exact ((F.obj t''.A).1.obj d'').trans (fcmp K F (C.comp b v2) w (D.comp g' g) g'' (fcmp K F v2 b g g' b2 hb) hz)
      ((F.obj t''.A).1.map_resp _ (D.hsymm (D.assoc g'' g' g)))
  · let PE := P.obj E4
    exact PE.trans (P.map_comp w _ _) (PE.trans (P.map_eqv w (P.map_comp a u1 _)) (PE.trans
      (P.map_eqv w (P.map_eqv a c1)) (PE.trans (P.map_eqv w ((P.obj E3).symm (P.map_comp a v1 _)))
        (PE.trans (PE.symm (P.map_comp w _ _)) (PE.trans (P.map_resp _ hw) (PE.trans (P.map_comp w _ _)
          (PE.trans (P.map_eqv w (P.map_comp b u2 _)) (PE.trans (P.map_eqv w (P.map_eqv b c2))
            (PE.trans (P.map_eqv w ((P.obj E3).symm (P.map_comp b v2 _))) (PE.symm (P.map_comp w _ _)))))))))))

/-- restricting an EqS witness -/
theorem eqS_pull {c d : D.Ob} (f : D.Hom d c) (t t' : Tri K F P c) {e : D.Ob} (g : D.Hom e d)
    (h : (EqS K F P t t').S (D.comp g f)) :
    (EqS K F P ⟨t.A, t.p, (F.obj t.A).1.map f t.x⟩ ⟨t'.A, t'.p, (F.obj t'.A).1.map f t'.x⟩).S g :=
  match h with
  | ⟨E, u, v, z, h1, h2, h3⟩ => ⟨E, u, v, z, ((F.obj t.A).1.obj e).trans h1 ((F.obj t.A).1.map_comp g f t.x),
      ((F.obj t'.A).1.obj e).trans h2 ((F.obj t'.A).1.map_comp g f t'.x), h3⟩

/-- the tensor presheaf P ⊗ F -/
def TensP (hF : FlatF K F) : Psh.{0, 0, 0} D where
  obj c := ⟨Tri K F P c, LocEq K F P, locEq_refl K F P, fun h => locEq_symm K F P h,
    fun h k => locEq_trans K F P hF h k⟩
  map f t := ⟨t.A, t.p, (F.obj t.A).1.map f t.x⟩
  map_eqv f t t' h := cov_mono K (K.pull f h) (fun _ g hg => eqS_pull K F P f t t' g hg)
  map_resp t h := cov_mono K (K.top _) (fun _ g _ => ⟨t.A, C.id t.A, C.id t.A, (F.obj t.A).1.map g ((F.obj t.A).1.map _ t.x),
    F.map_id t.A _ _, ((F.obj t.A).1.obj _).trans (F.map_id t.A _ _) ((F.obj t.A).1.map_eqv g ((F.obj t.A).1.map_resp t.x h)),
    (P.obj t.A).refl _⟩)
  map_id c t := cov_mono K (K.top c) (fun _ g _ => ⟨t.A, C.id t.A, C.id t.A, (F.obj t.A).1.map g ((F.obj t.A).1.map (D.id c) t.x),
    F.map_id t.A _ _, ((F.obj t.A).1.obj _).trans (F.map_id t.A _ _) ((F.obj t.A).1.map_eqv g ((F.obj t.A).1.map_id c t.x)),
    (P.obj t.A).refl _⟩)
  map_comp f g t := cov_mono K (K.top _) (fun _ h _ => ⟨t.A, C.id t.A, C.id t.A,
    (F.obj t.A).1.map h ((F.obj t.A).1.map (D.comp f g) t.x), F.map_id t.A _ _,
    ((F.obj t.A).1.obj _).trans (F.map_id t.A _ _) ((F.obj t.A).1.map_eqv h ((F.obj t.A).1.map_comp f g t.x)),
    (P.obj t.A).refl _⟩)

theorem tensP_sep (hF : FlatF K F) : Separated K (TensP K F P hF) := fun _ _ hS a b h =>
  K.trans hS (fun _ f hf => cov_mono K (h _ f hf) (fun _ g ⟨E, u, v, z, h1, h2, h3⟩ =>
    ⟨E, u, v, z, ((F.obj a.A).1.obj _).trans h1 (((F.obj a.A).1.obj _).symm ((F.obj a.A).1.map_comp g f a.x)),
      ((F.obj b.A).1.obj _).trans h2 (((F.obj b.A).1.obj _).symm ((F.obj b.A).1.map_comp g f b.x)), h3⟩))

end Gt

end Found

#print axioms Found.Gt.locEq_trans
#print axioms Found.Gt.TensP
#print axioms Found.Gt.tensP_sep

found/Gt74.lean

lean_all/found/Gt74.lean

/-
  T194 (classifying topos), part 74. found/Gt74 — the direct image of a
  functor F : C → Sh(D, K): for a sheaf Q, the presheaf on C
  A ↦ Hom(F(A), Q), restricted by precomposition with F(u).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt73

namespace Found

namespace Gt

open Sh

variable {C : ECat.{0, 0}} {D : ECat.{0, 0}} (K : GTop D) (F : EFun C (ShCat K))

/-- the presheaf A ↦ Hom(F A, Q) -/
def DirP (Q : (ShCat K).Ob) : Psh.{0, 0, 0} C where
  obj A := ⟨PNat (F.obj A).1 Q.1, fun α β => ∀ c x, (Q.1.obj c).eqv (α.app c x) (β.app c x),
    fun _ _ _ => (Q.1.obj _).refl _, fun h c x => (Q.1.obj c).symm (h c x),
    fun h k c x => (Q.1.obj c).trans (h c x) (k c x)⟩
  map u α := compN (F.map u) α
  map_eqv _ _ _ h c _ := h c _
  map_resp α h c x := α.app_eqv c (F.map_resp h c x)
  map_id A α c x := α.app_eqv c (F.map_id A c x)
  map_comp u v α c x := α.app_eqv c (F.map_comp u v c x)

section Sh

variable (hF : FlatF K F) (Q : (ShCat K).Ob) {A : C.Ob} {S : Sieve C A} (m : MFam (DirP K F Q) S)

/-- one side of the well-definedness chain -/
theorem dir_side {d d1 d2 : D.Ob} {B E E' : C.Ob} (u : C.Hom B A) (hu : S.S u) (a : C.Hom E B) (w : C.Hom E' E)
    (g1 : D.Hom d1 d) (g2 : D.Hom d2 d1) (y : ((F.obj B).1.obj d).car) (z : ((F.obj E).1.obj d1).car)
    (z' : ((F.obj E').1.obj d2).car)
    (ha : ((F.obj B).1.obj d1).eqv ((F.map a).app d1 z) ((F.obj B).1.map g1 y))
    (hz' : ((F.obj E).1.obj d2).eqv ((F.map w).app d2 z') ((F.obj E).1.map g2 z)) :
    (Q.1.obj d2).eqv (Q.1.map g2 (Q.1.map g1 ((m.x u hu).app d y)))
      ((m.x (C.comp w (C.comp a u)) (S.resp (C.assoc w a u) (S.stable hu (C.comp w a)))).app d2 z') := by
  let PQ := Q.1
  let α := m.x u hu
  let PB := (F.obj B).1
  have i1 : (PQ.obj d1).eqv (PQ.map g1 (α.app d y)) (α.app d1 (PB.map g1 y)) := (PQ.obj d1).symm (α.nat g1 y)
  have i2 : (PQ.obj d1).eqv (α.app d1 (PB.map g1 y)) (α.app d1 ((F.map a).app d1 z)) := α.app_eqv d1 ((PB.obj d1).symm ha)
  have i3 : (PQ.obj d2).eqv (PQ.map g2 (α.app d1 ((F.map a).app d1 z))) (α.app d2 (PB.map g2 ((F.map a).app d1 z))) :=
    (PQ.obj d2).symm (α.nat g2 _)
  have i4 : (PB.obj d2).eqv (PB.map g2 ((F.map a).app d1 z)) ((F.map a).app d2 ((F.map w).app d2 z')) :=
    (PB.obj d2).trans ((PB.obj d2).symm ((F.map a).nat g2 z)) ((F.map a).app_eqv d2 (((F.obj E).1.obj d2).symm hz'))
  have i5 : (PB.obj d2).eqv ((F.map a).app d2 ((F.map w).app d2 z')) ((F.map (C.comp w a)).app d2 z') :=
    (PB.obj d2).symm (F.map_comp w a d2 z')
  have s1 : (PQ.obj d2).eqv (PQ.map g2 (PQ.map g1 (α.app d y))) (α.app d2 ((F.map (C.comp w a)).app d2 z')) :=
    (PQ.obj d2).trans (PQ.map_eqv g2 ((PQ.obj d1).trans i1 i2)) ((PQ.obj d2).trans i3
      (α.app_eqv d2 ((PB.obj d2).trans i4 i5)))
  exact (PQ.obj d2).trans s1 ((PQ.obj d2).trans ((PQ.obj d2).symm (m.compat u hu (C.comp w a) d2 z'))
    (m.resp _ _ (C.assoc w a u) d2 z'))

include hF in
/-- the matching family gives the same value on two lifts of the same element -/
theorem dir_wd {d : D.Ob} {B B' : C.Ob} (u : C.Hom B A) (hu : S.S u) (u' : C.Hom B' A) (hu' : S.S u')
    (y : ((F.obj B).1.obj d).car) (y' : ((F.obj B').1.obj d).car)
    (h : ((F.obj A).1.obj d).eqv ((F.map u).app d y) ((F.map u').app d y')) :
    (Q.1.obj d).eqv ((m.x u hu).app d y) ((m.x u' hu').app d y') := by
  let PQ := Q.1
  let PA := (F.obj A).1
  refine sheaf_sep K PQ Q.2 d _ (hF.pair y y') _ _ (fun d1 g1 ⟨E, a, b, z, ha, hb⟩ => ?_)
  have hz : (PA.obj d1).eqv ((F.map (C.comp a u)).app d1 z) ((F.map (C.comp b u')).app d1 z) :=
    (PA.obj d1).trans (F.map_comp a u d1 z) ((PA.obj d1).trans ((F.map u).app_eqv d1 ha) ((PA.obj d1).trans
      ((F.map u).nat g1 y) ((PA.obj d1).trans (PA.map_eqv g1 h) ((PA.obj d1).trans ((PA.obj d1).symm ((F.map u').nat g1 y'))
        ((PA.obj d1).trans ((F.map u').app_eqv d1 (((F.obj B').1.obj d1).symm hb)) ((PA.obj d1).symm (F.map_comp b u' d1 z)))))))
  refine sheaf_sep K PQ Q.2 d1 _ (hF.eqz _ _ z hz) _ _ (fun d2 g2 ⟨E', w, z', hw, hz'⟩ => ?_)
  exact (PQ.obj d2).trans (dir_side K F Q m u hu a w g1 g2 y z z' ha hz') ((PQ.obj d2).trans
    (m.resp _ _ hw d2 z') ((PQ.obj d2).symm (dir_side K F Q m u' hu' b w g1 g2 y' z z' hb hz')))

variable (J : GTop C) (Kc : CP.Classical') (hC : ContF J K F) (hS : J.cov S)

/-- the lifts of x along the sieve, at stage g -/
def LiftP {c d : D.Ob} (x : ((F.obj A).1.obj c).car) (g : D.Hom d c)
    (w : Σ' (B : C.Ob) (u : C.Hom B A) (_ : S.S u), ((F.obj B).1.obj d).car) : Prop :=
  ((F.obj A).1.obj d).eqv ((F.map w.2.1).app d w.2.2.2) ((F.obj A).1.map g x)

theorem lift_ex {c d : D.Ob} (x : ((F.obj A).1.obj c).car) {g : D.Hom d c} (hg : (HitS K F S x).S g) :
    ∃ w, LiftP K F (S := S) x g w :=
  match hg with
  | ⟨B, u, hu, y, h⟩ => ⟨⟨B, u, hu, y⟩, h⟩

/-- the local value of the glued map -/
def valL {c d : D.Ob} (x : ((F.obj A).1.obj c).car) {g : D.Hom d c} (hg : (HitS K F S x).S g) : (Q.1.obj d).car :=
  let w := Kc.ch (LiftP K F (S := S) x g) (lift_ex K F x hg)
  (m.x w.2.1 w.2.2.1).app d w.2.2.2

include hF in
theorem valL_spec {c d : D.Ob} (x : ((F.obj A).1.obj c).car) {g : D.Hom d c} (hg : (HitS K F S x).S g)
    {B : C.Ob} (u : C.Hom B A) (hu : S.S u) (y : ((F.obj B).1.obj d).car)
    (h : ((F.obj A).1.obj d).eqv ((F.map u).app d y) ((F.obj A).1.map g x)) :
    (Q.1.obj d).eqv (valL K F Q m Kc x hg) ((m.x u hu).app d y) :=
  have hw := Kc.ch_spec (LiftP K F (S := S) x g) (lift_ex K F x hg)
  dir_wd K F hF Q m _ _ u hu _ y (((F.obj A).1.obj d).trans hw (((F.obj A).1.obj d).symm h))

end Sh


end Gt

end Found

#print axioms Found.Gt.DirP
#print axioms Found.Gt.dir_wd
#print axioms Found.Gt.valL_spec

found/Gt75.lean

lean_all/found/Gt75.lean

/-
  T194 (classifying topos), part 75. found/Gt75 — for a flat continuous F,
  the direct image A ↦ Hom(F(A), Q) is a J-sheaf: a matching family of maps
  on a covering sieve glues to the map sending x ∈ F(A) to the glued local
  values on its lifts (continuity gives lifts locally, flatness makes the
  values agree).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt74

namespace Found

namespace Gt

open Sh

variable {C : ECat.{0, 0}} {D : ECat.{0, 0}} (K : GTop D) (F : EFun C (ShCat K)) (hF : FlatF K F)
  (Q : (ShCat K).Ob) (J : GTop C) (Kc : CP.Classical') (hC : ContF J K F)

section G

variable {A : C.Ob} {S : Sieve C A} (m : MFam (DirP K F Q) S)

/-- the local values form a matching family -/
def valM {c : D.Ob} (x : ((F.obj A).1.obj c).car) : MFam Q.1 (HitS K F S x) where
  x _ hg := valL K F Q m Kc x hg
  resp {_} {_ g'} hg hg' e :=
    let w' := Kc.ch (LiftP K F (S := S) x g') (lift_ex K F x hg')
    have hw' := Kc.ch_spec (LiftP K F (S := S) x g') (lift_ex K F x hg')
    valL_spec K F hF Q m Kc x hg w'.2.1 w'.2.2.1 w'.2.2.2
      (((F.obj A).1.obj _).trans hw' ((F.obj A).1.map_resp x ((D.hsymm e))))
  compat {_ _} g hg k :=
    let w := Kc.ch (LiftP K F (S := S) x g) (lift_ex K F x hg)
    have hw := Kc.ch_spec (LiftP K F (S := S) x g) (lift_ex K F x hg)
    (Q.1.obj _).trans (valL_spec K F hF Q m Kc x (HitS K F S x |>.stable hg k) w.2.1 w.2.2.1
      ((F.obj w.1).1.map k w.2.2.2) (((F.obj A).1.obj _).trans ((F.map w.2.1).nat k w.2.2.2)
        (((F.obj A).1.obj _).trans ((F.obj A).1.map_eqv k hw) (((F.obj A).1.obj _).symm ((F.obj A).1.map_comp k g x)))))
      ((m.x w.2.1 w.2.2.1).nat k w.2.2.2)

variable (hS : J.cov S)

def gluedApp {c : D.Ob} (x : ((F.obj A).1.obj c).car) : (Q.1.obj c).car :=
  Kc.ch (Amalg Q.1 (valM K F hF Q Kc m x)) (Q.2 c _ (hC A S hS c x) (valM K F hF Q Kc m x)).1

theorem glued_spec {c : D.Ob} (x : ((F.obj A).1.obj c).car) :
    Amalg Q.1 (valM K F hF Q Kc m x) (gluedApp K F hF Q J Kc hC m hS x) :=
  Kc.ch_spec (Amalg Q.1 (valM K F hF Q Kc m x)) (Q.2 c _ (hC A S hS c x) (valM K F hF Q Kc m x)).1

/-- the glued value on any lift -/
theorem glued_lift {c d : D.Ob} (x : ((F.obj A).1.obj c).car) (g : D.Hom d c) {B : C.Ob} (u : C.Hom B A)
    (hu : S.S u) (y : ((F.obj B).1.obj d).car) (h : ((F.obj A).1.obj d).eqv ((F.map u).app d y) ((F.obj A).1.map g x)) :
    (Q.1.obj d).eqv (Q.1.map g (gluedApp K F hF Q J Kc hC m hS x)) ((m.x u hu).app d y) :=
  (Q.1.obj d).trans (glued_spec K F hF Q J Kc hC m hS x d g ⟨B, u, hu, y, h⟩)
    (valL_spec K F hF Q m Kc x ⟨B, u, hu, y, h⟩ u hu y h)

def gluedN : PNat (F.obj A).1 Q.1 where
  app _ x := gluedApp K F hF Q J Kc hC m hS x
  app_eqv c {x x'} h := sheaf_sep K Q.1 Q.2 c _ (hC A S hS c x) _ _ (fun _ g ⟨_, u, hu, y, hy⟩ =>
    (Q.1.obj _).trans (glued_lift K F hF Q J Kc hC m hS x g u hu y hy) ((Q.1.obj _).symm
      (glued_lift K F hF Q J Kc hC m hS x' g u hu y (((F.obj A).1.obj _).trans hy ((F.obj A).1.map_eqv g h)))))
  nat f x := sheaf_sep K Q.1 Q.2 _ _ (hC A S hS _ ((F.obj A).1.map f x)) _ _ (fun _ g ⟨_, u, hu, y, hy⟩ =>
    (Q.1.obj _).trans (glued_lift K F hF Q J Kc hC m hS _ g u hu y hy) ((Q.1.obj _).symm ((Q.1.obj _).trans
      ((Q.1.obj _).symm (Q.1.map_comp g f _))
      (glued_lift K F hF Q J Kc hC m hS x (D.comp g f) u hu y (((F.obj A).1.obj _).trans hy
        (((F.obj A).1.obj _).symm ((F.obj A).1.map_comp g f x)))))))

end G

include hF Kc hC in
theorem dirP_sheaf : IsSheaf J (DirP K F Q) := by
  intro A S hS m
  refine ⟨⟨gluedN K F hF Q J Kc hC m hS, fun B u hu c y => ?_⟩, fun a b ha hb c x => ?_⟩
  · exact sheaf_sep K Q.1 Q.2 c _ (hC A S hS c ((F.map u).app c y)) _ _ (fun _ g ⟨B', u', hu', y', hy'⟩ =>
      (Q.1.obj _).trans (glued_lift K F hF Q J Kc hC m hS _ g u' hu' y' hy') ((Q.1.obj _).trans
        (dir_wd K F hF Q m u' hu' u hu y' ((F.obj B).1.map g y) (((F.obj A).1.obj _).trans hy'
          (((F.obj A).1.obj _).symm ((F.map u).nat g y))))
        ((m.x u hu).nat g y)))
  · exact sheaf_sep K Q.1 Q.2 c _ (hC A S hS c x) _ _ (fun _ g ⟨B, u, hu, y, hy⟩ =>
      (Q.1.obj _).trans ((Q.1.obj _).symm (a.nat g x)) ((Q.1.obj _).trans (a.app_eqv _ (((F.obj A).1.obj _).symm hy))
        ((Q.1.obj _).trans (ha B u hu _ y) ((Q.1.obj _).trans ((Q.1.obj _).symm (hb B u hu _ y))
          ((Q.1.obj _).trans (b.app_eqv _ hy) (b.nat g x))))))


end Gt

end Found

#print axioms Found.Gt.valM
#print axioms Found.Gt.gluedN
#print axioms Found.Gt.dirP_sheaf

found/Gt76.lean

lean_all/found/Gt76.lean

/-
  T194 (classifying topos), part 76. found/Gt76 — the direct image functor
  f_* : Sh(D, K) → Sh(C, J) of a flat continuous F; the tensor P ⊗ F is
  functorial in P; a map α : P → f_*(Q) gives a map P ⊗ F → Q,
  (A, p, x) ↦ α(p)(x).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt75

namespace Found

namespace Gt

open Sh

variable {C : ECat.{0, 0}} {D : ECat.{0, 0}} (J : GTop C) (K : GTop D) (F : EFun C (ShCat K)) (hF : FlatF K F)
  (Kc : CP.Classical') (hC : ContF J K F)

/-- the direct image functor -/
def DirF : EFun (ShCat K) (ShCat J) where
  obj Q := ⟨DirP K F Q, dirP_sheaf K F hF Q J Kc hC⟩
  map {_ Q'} β := ⟨fun _ α => compN α β, fun _ {_ _} h c x => β.app_eqv c (h c x), fun _ _ _ _ => (Q'.1.obj _).refl _⟩
  map_resp h _ α c x := h c (α.app c x)
  map_id Q _ _ _ _ := (Q.1.obj _).refl _
  map_comp {_ _ Q''} _ _ _ _ _ _ := (Q''.1.obj _).refl _

section T

variable {P P' : Psh.{0, 0, 0} C}

/-- the tensor is functorial in P -/
def tensMap (π : PNat P P') : PNat (TensP K F P hF) (TensP K F P' hF) where
  app _ t := ⟨t.A, π.app t.A t.p, t.x⟩
  app_eqv _ {_ _} h := cov_mono K h (fun _ _ ⟨E, u, v, z, h1, h2, h3⟩ => ⟨E, u, v, z, h1, h2,
    (P'.obj E).trans ((P'.obj E).symm (π.nat u _)) ((P'.obj E).trans (π.app_eqv E h3) (π.nat v _))⟩)
  nat _ t := locEq_refl K F P' ⟨t.A, π.app t.A t.p, (F.obj t.A).1.map _ t.x⟩

variable (Q : (ShCat K).Ob)

/-- a map into the direct image gives a map out of the tensor -/
def tensTo (α : PNat P (DirP K F Q)) : PNat (TensP K F P hF) Q.1 where
  app _ t := (α.app t.A t.p).app _ t.x
  app_eqv c {t t'} h := sheaf_sep K Q.1 Q.2 c _ h _ _ (fun d g ⟨E, u, v, z, h1, h2, h3⟩ =>
    (Q.1.obj d).trans ((Q.1.obj d).symm ((α.app t.A t.p).nat g t.x)) ((Q.1.obj d).trans
      ((α.app t.A t.p).app_eqv d (((F.obj t.A).1.obj d).symm h1)) ((Q.1.obj d).trans
        ((Q.1.obj d).symm (α.nat u t.p d z)) ((Q.1.obj d).trans (α.app_eqv E h3 d z) ((Q.1.obj d).trans
          (α.nat v t'.p d z) ((Q.1.obj d).trans ((α.app t'.A t'.p).app_eqv d h2) ((α.app t'.A t'.p).nat g t'.x)))))))
  nat f t := (α.app t.A t.p).nat f t.x

end T

end Gt

end Found

#print axioms Found.Gt.DirF
#print axioms Found.Gt.tensMap
#print axioms Found.Gt.tensTo

found/Gt77.lean

lean_all/found/Gt77.lean

/-
  T194 (classifying topos), part 77. found/Gt77 — the inverse image
  f*(P) = (P ⊗ F)⁺ of a flat continuous F (P ⊗ F is separated, so one plus
  construction gives a sheaf), functorial in P, and the adjunction
  f* ⊣ f_*: maps (P ⊗ F)⁺ → Q correspond to maps P → Hom(F(-), Q).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt76

namespace Found

namespace Gt

open Sh

variable {C : ECat.{0, 0}} {D : ECat.{0, 0}} (J : GTop C) (K : GTop D) (F : EFun C (ShCat K)) (hF : FlatF K F)
  (Kc : CP.Classical') (hC : ContF J K F)

def invObj (P : (ShCat J).Ob) : (ShCat K).Ob :=
  ⟨Plus K (TensP K F P.1 hF), plus_sheaf K (TensP K F P.1 hF) Kc (tensP_sep K F P.1 hF)⟩

def invMap {P P' : (ShCat J).Ob} (π : (ShCat J).Hom P P') : (ShCat K).Hom (invObj J K F hF Kc P) (invObj J K F hF Kc P') :=
  extN K (TensP K F P.1 hF) (Plus K (TensP K F P'.1 hF)) (invObj J K F hF Kc P').2
    (compN (tensMap K F hF π) (eta K (TensP K F P'.1 hF))) Kc

theorem invMap_unit {P P' : (ShCat J).Ob} (π : (ShCat J).Hom P P') {c : D.Ob} (t : Tri K F P.1 c) :
    PEq K (TensP K F P'.1 hF) ((invMap J K F hF Kc π).app c (etaE K (TensP K F P.1 hF) t))
      (etaE K (TensP K F P'.1 hF) ⟨t.A, π.app t.A t.p, t.x⟩) :=
  ext_unit K (TensP K F P.1 hF) (Plus K (TensP K F P'.1 hF)) (invObj J K F hF Kc P').2 _ Kc c t

/-- the inverse image functor -/
def InvF : EFun (ShCat J) (ShCat K) where
  obj P := invObj J K F hF Kc P
  map π := invMap J K F hF Kc π
  map_resp {P P'} {π π'} h := ext_uniq K (TensP K F P.1 hF) (Plus K (TensP K F P'.1 hF)) (invObj J K F hF Kc P').2 _ _
    (fun c t => peq_trans K _ (invMap_unit J K F hF Kc π t) (peq_trans K _
      ((eta K (TensP K F P'.1 hF)).app_eqv c (cov_mono K (K.top c) (fun _ g _ => ⟨t.A, C.id t.A, C.id t.A,
        (F.obj t.A).1.map g t.x, F.map_id t.A _ _, F.map_id t.A _ _, P'.1.map_eqv (C.id t.A) (h t.A t.p)⟩)))
      (peq_symm K _ (invMap_unit J K F hF Kc π' t))))
  map_id P := ext_uniq K (TensP K F P.1 hF) (Plus K (TensP K F P.1 hF)) (invObj J K F hF Kc P).2 _ _
    (fun _ t => peq_trans K _ (invMap_unit J K F hF Kc _ t) (peq_refl K _ _))
  map_comp {P _ P''} π π' := ext_uniq K (TensP K F P.1 hF) (Plus K (TensP K F P''.1 hF)) (invObj J K F hF Kc P'').2 _ _
    (fun c t => peq_trans K _ (invMap_unit J K F hF Kc _ t) (peq_symm K _ (peq_trans K _
      ((invMap J K F hF Kc π').app_eqv c (invMap_unit J K F hF Kc π t)) (invMap_unit J K F hF Kc π' _))))

/-- maps out of f*(P) give maps into f_*(Q) -/
def phiD {P : (ShCat J).Ob} {Q : (ShCat K).Ob} (β : (ShCat K).Hom (invObj J K F hF Kc P) Q) :
    (ShCat J).Hom P ((DirF J K F hF Kc hC).obj Q) where
  app A p := ⟨fun c x => β.app c (etaE K (TensP K F P.1 hF) ⟨A, p, x⟩),
    fun c {_ _} h => β.app_eqv c ((eta K (TensP K F P.1 hF)).app_eqv c (cov_mono K (K.top c) (fun _ g _ =>
      ⟨A, C.id A, C.id A, (F.obj A).1.map g _, F.map_id A _ _, ((F.obj A).1.obj _).trans (F.map_id A _ _)
        ((F.obj A).1.map_eqv g h), (P.1.obj A).refl _⟩))),
    fun f x => (Q.1.obj _).trans (β.app_eqv _ ((eta K (TensP K F P.1 hF)).nat f ⟨A, p, x⟩)) (β.nat f _)⟩
  app_eqv A {_ _} h c x := β.app_eqv c ((eta K (TensP K F P.1 hF)).app_eqv c (cov_mono K (K.top c) (fun _ g _ =>
    ⟨A, C.id A, C.id A, (F.obj A).1.map g x, F.map_id A _ _, F.map_id A _ _, P.1.map_eqv (C.id A) h⟩)))
  nat {B _} u _ c y := β.app_eqv c ((eta K (TensP K F P.1 hF)).app_eqv c (cov_mono K (K.top c) (fun _ g _ =>
    ⟨B, C.id B, u, (F.obj B).1.map g y, F.map_id B _ _, (F.map u).nat g y, P.1.map_id B _⟩)))

/-- maps into f_*(Q) give maps out of f*(P) -/
def psiD {P : (ShCat J).Ob} {Q : (ShCat K).Ob} (α : (ShCat J).Hom P ((DirF J K F hF Kc hC).obj Q)) :
    (ShCat K).Hom (invObj J K F hF Kc P) Q :=
  extN K (TensP K F P.1 hF) Q.1 Q.2 (tensTo K F hF Q α) Kc

/-- the adjunction f* ⊣ f_* -/
def adjD : Adj (InvF J K F hF Kc) (DirF J K F hF Kc hC) where
  phi β := phiD J K F hF Kc hC β
  psi α := psiD J K F hF Kc hC α
  phi_resp h _ _ c _ := h c _
  psi_resp {P Q} {α α'} h := ext_uniq K (TensP K F P.1 hF) Q.1 Q.2 _ _ (fun c t =>
    (Q.1.obj c).trans (ext_unit K (TensP K F P.1 hF) Q.1 Q.2 (tensTo K F hF Q α) Kc c t)
      ((Q.1.obj c).trans (h t.A t.p c t.x) ((Q.1.obj c).symm (ext_unit K (TensP K F P.1 hF) Q.1 Q.2 (tensTo K F hF Q α') Kc c t))))
  phi_psi {P Q} α A p c x := ext_unit K (TensP K F P.1 hF) Q.1 Q.2 (tensTo K F hF Q α) Kc c ⟨A, p, x⟩
  psi_phi {P Q} β := ext_uniq K (TensP K F P.1 hF) Q.1 Q.2 _ _ (fun c t =>
    ext_unit K (TensP K F P.1 hF) Q.1 Q.2 (tensTo K F hF Q (phiD J K F hF Kc hC β)) Kc c t)
  nat_l π β A p c x := β.app_eqv c (invMap_unit J K F hF Kc π ⟨A, p, x⟩)
  nat_r {_ _ Q'} _ _ _ _ _ _ := (Q'.1.obj _).refl _

end Gt

end Found

#print axioms Found.Gt.InvF
#print axioms Found.Gt.phiD
#print axioms Found.Gt.adjD

found/Gt78.lean

lean_all/found/Gt78.lean

/-
  T194 (classifying topos), part 78. found/Gt78 — the inverse image of a
  flat F preserves the terminal sheaf: any two triples of 1 ⊗ F are locally
  equivalent (flatness, pairing), and triples exist locally (local
  nonemptiness), so they form a global element of (1 ⊗ F)⁺.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt77

namespace Found

namespace Gt

open Sh

variable {C : ECat.{0, 0}} {D : ECat.{0, 0}} (J : GTop C) (K : GTop D) (F : EFun C (ShCat K)) (hF : FlatF K F)
  (Kc : CP.Classical') (hC : ContF J K F)

include hF in
theorem one_loc {c : D.Ob} (t t' : Tri K F (oneP (C := C)) c) : LocEq K F (oneP (C := C)) t t' :=
  cov_mono K (hF.pair t.x t'.x) (fun _ _ ⟨E, u, v, z, h1, h2⟩ => ⟨E, u, v, z, h1, h2, trivial⟩)

def pickOne {c d : D.Ob} {g : D.Hom d c} (hg : (NeS K F c).S g) : Tri K F (oneP (C := C)) d :=
  let w := Kc.ch (fun (_ : Σ' (A : C.Ob), ((F.obj A).1.obj d).car) => True)
    (match hg with | ⟨A, x, _⟩ => ⟨⟨A, x⟩, trivial⟩)
  ⟨w.1, PUnit.unit, w.2⟩

def oneElt (c : D.Ob) : PElt K (TensP K F (oneP (C := C)) hF) c :=
  ⟨NeS K F c, hF.ne c, ⟨fun _ hg => pickOne K F Kc hg, fun _ _ _ => one_loc K F hF _ _,
    fun _ _ _ => one_loc K F hF _ _⟩⟩

def oneMap : PNat (oneP (C := D)) (Plus K (TensP K F (oneP (C := C)) hF)) where
  app c _ := oneElt K F hF Kc c
  app_eqv _ {_ _} _ := peq_refl K _ _
  nat {_ c} f _ := ⟨meetS (NeS K F _) (pullS f (NeS K F c)), cov_meet K (hF.ne _) (K.pull f (hF.ne c)),
    fun _ _ r => ⟨r.1, r.2⟩, fun _ _ _ _ _ => one_loc K F hF _ _⟩

theorem inv_term : ∃ g : (ShCat K).Hom (shTerminal K).one ((InvF J K F hF Kc).obj (shTerminal J).one),
    (ShCat K).heq ((ShCat K).comp g ((shTerminal K).bang _)) ((ShCat K).id _) :=
  ⟨oneMap K F hF Kc, fun _ _ => trivial⟩

end Gt

end Found

#print axioms Found.Gt.oneMap
#print axioms Found.Gt.inv_term

found/Gt79.lean

lean_all/found/Gt79.lean

/-
  T194 (classifying topos), part 79. found/Gt79 — for a flat F, two triples
  of (P × Q) ⊗ F are locally equivalent as soon as their P-components and
  their Q-components are (combine the two local witnesses by pairing and
  equalize the arrows twice).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt78

namespace Found

namespace Gt

open Sh

variable {C : ECat.{0, 0}} {D : ECat.{0, 0}} (K : GTop D) (F : EFun C (ShCat K)) (hF : FlatF K F)
  (P Q : Psh.{0, 0, 0} C)

def triFst {c : D.Ob} (t : Tri K F (prodP P Q) c) : Tri K F P c := ⟨t.A, t.p.1, t.x⟩
def triSnd {c : D.Ob} (t : Tri K F (prodP P Q) c) : Tri K F Q c := ⟨t.A, t.p.2, t.x⟩

theorem assoc3 {X0 X1 X2 X3 X4 : C.Ob} (k2 : C.Hom X0 X1) (k : C.Hom X1 X2) (a : C.Hom X2 X3) (u : C.Hom X3 X4) :
    C.heq (C.comp k2 (C.comp k (C.comp a u))) (C.comp (C.comp k2 (C.comp k a)) u) :=
  C.htrans (C.comp_resp (C.hrefl k2) (C.hsymm (C.assoc k a u))) (C.hsymm (C.assoc k2 (C.comp k a) u))

include hF in
theorem locEq_pair {c : D.Ob} (t t' : Tri K F (prodP P Q) c) (h1 : LocEq K F P (triFst K F P Q t) (triFst K F P Q t'))
    (h2 : LocEq K F Q (triSnd K F P Q t) (triSnd K F P Q t')) : LocEq K F (prodP P Q) t t' := by
  refine K.trans (cov_meet K h1 h2) (fun d g1 ⟨⟨E1, u1, v1, z1, a1, b1, c1⟩, ⟨E2, u2, v2, z2, a2, b2, c2⟩⟩ => ?_)
  refine K.trans (hF.pair z1 z2) (fun d2 g2 ⟨E3, a, b, z3, ha, hb⟩ => ?_)
  let FA := (F.obj t.A).1
  let FA' := (F.obj t'.A).1
  have m1 : (FA.obj d2).eqv ((F.map (C.comp a u1)).app d2 z3) ((F.map (C.comp b u2)).app d2 z3) :=
    (FA.obj d2).trans (fcmp K F u1 a g1 g2 a1 ha) ((FA.obj d2).symm (fcmp K F u2 b g1 g2 a2 hb))
  refine K.trans (hF.eqz _ _ z3 m1) (fun d3 g3 ⟨E4, k, z4, hk, hz4⟩ => ?_)
  have m2 : (FA'.obj d3).eqv ((F.map (C.comp k (C.comp a v1))).app d3 z4) ((F.map (C.comp k (C.comp b v2))).app d3 z4) :=
    (FA'.obj d3).trans (fcmp K F (C.comp a v1) k (D.comp g2 g1) g3 (fcmp K F v1 a g1 g2 b1 ha) hz4)
      ((FA'.obj d3).symm (fcmp K F (C.comp b v2) k (D.comp g2 g1) g3 (fcmp K F v2 b g1 g2 b2 hb) hz4))
  refine cov_mono K (hF.eqz _ _ z4 m2) (fun d4 g4 ⟨E5, k2, z5, hk2, hz5⟩ => ?_)
  have eG : D.heq (D.comp g4 (D.comp g3 (D.comp g2 g1))) (D.comp (D.comp (D.comp g4 g3) g2) g1) :=
    D.htrans (D.comp_resp (D.hrefl g4) (D.hsymm (D.assoc g3 g2 g1)))
      (D.htrans (D.hsymm (D.assoc g4 (D.comp g3 g2) g1)) (D.comp_resp (D.hsymm (D.assoc g4 g3 g2)) (D.hrefl g1)))
  refine ⟨E5, C.comp k2 (C.comp k (C.comp a u1)), C.comp k2 (C.comp k (C.comp a v1)), z5, ?_, ?_, ⟨?_, ?_⟩⟩
  · exact (FA.obj d4).trans (fcmp K F _ k2 (D.comp g3 (D.comp g2 g1)) g4
      (fcmp K F (C.comp a u1) k (D.comp g2 g1) g3 (fcmp K F u1 a g1 g2 a1 ha) hz4) hz5) (FA.map_resp _ eG)
  · exact (FA'.obj d4).trans (fcmp K F _ k2 (D.comp g3 (D.comp g2 g1)) g4
      (fcmp K F (C.comp a v1) k (D.comp g2 g1) g3 (fcmp K F v1 a g1 g2 b1 ha) hz4) hz5) (FA'.map_resp _ eG)
  · let W := C.comp k2 (C.comp k a)
    exact (P.obj E5).trans (P.map_resp _ (assoc3 k2 k a u1)) ((P.obj E5).trans (P.map_comp W u1 _)
      ((P.obj E5).trans (P.map_eqv W c1) ((P.obj E5).trans ((P.obj E5).symm (P.map_comp W v1 _))
        (P.map_resp _ (C.hsymm (assoc3 k2 k a v1))))))
  · let W' := C.comp k2 (C.comp k b)
    have hu : C.heq (C.comp k2 (C.comp k (C.comp a u1))) (C.comp W' u2) :=
      C.htrans (C.comp_resp (C.hrefl k2) hk) (assoc3 k2 k b u2)
    have hv : C.heq (C.comp W' v2) (C.comp k2 (C.comp k (C.comp a v1))) :=
      C.htrans (C.hsymm (assoc3 k2 k b v2)) (C.hsymm hk2)
    exact (Q.obj E5).trans (Q.map_resp _ hu) ((Q.obj E5).trans (Q.map_comp W' u2 _)
      ((Q.obj E5).trans (Q.map_eqv W' c2) ((Q.obj E5).trans ((Q.obj E5).symm (Q.map_comp W' v2 _))
        (Q.map_resp _ hv))))

end Gt

end Found

#print axioms Found.Gt.locEq_pair

found/Gt8.lean

lean_all/found/Gt8.lean

/-
  T194 (classifying topos), part 8. found/Gt8 — the category Sh(C, J) of
  sheaves of setoids on a site (natural maps up to pointwise equality),
  with a terminal sheaf and binary products computed pointwise.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt7

namespace Found

namespace Gt

open Sh

variable {C : ECat.{0, 0}} (J : GTop C)

def idN (P : Psh.{0, 0, 0} C) : PNat P P where
  app _ x := x
  app_eqv _ {_ _} h := h
  nat _ _ := (P.obj _).refl _

def compN {P Q R : Psh.{0, 0, 0} C} (α : PNat P Q) (β : PNat Q R) : PNat P R where
  app c x := β.app c (α.app c x)
  app_eqv c {_ _} h := β.app_eqv c (α.app_eqv c h)
  nat f x := (R.obj _).trans (β.app_eqv _ (α.nat f x)) (β.nat f (α.app _ x))

def ShCat : ECat.{1, 0} where
  Ob := {P : Psh.{0, 0, 0} C // IsSheaf J P}
  Hom F G := PNat F.1 G.1
  heq {_ G} α β := ∀ c x, (G.1.obj c).eqv (α.app c x) (β.app c x)
  hrefl _ _ _ := (Setd.refl _ _)
  hsymm h c x := Setd.symm _ (h c x)
  htrans h k c x := Setd.trans _ (h c x) (k c x)
  id F := idN F.1
  comp α β := compN α β
  comp_resp {_ _ _} {_ f' g _} h k c x := Setd.trans _ (g.app_eqv c (h c x)) (k c (f'.app c x))
  id_comp _ _ _ := Setd.refl _ _
  comp_id _ _ _ := Setd.refl _ _
  assoc _ _ _ _ _ := Setd.refl _ _

/-! ## terminal sheaf -/

def oneP : Psh.{0, 0, 0} C where
  obj _ := ⟨PUnit, fun _ _ => True, fun _ => trivial, fun _ => trivial, fun _ _ => trivial⟩
  map _ _ := PUnit.unit
  map_eqv {_ _} _ {_ _} _ := trivial
  map_resp _ _ := trivial
  map_id _ _ := trivial
  map_comp _ _ _ := trivial

theorem oneP_sheaf : IsSheaf J (oneP (C := C)) := fun _ _ _ _ => ⟨⟨PUnit.unit, fun _ _ _ => trivial⟩, fun _ _ _ _ => trivial⟩

def shTerminal : Terminal (ShCat J) where
  one := ⟨oneP, oneP_sheaf J⟩
  bang _ := ⟨fun _ _ => PUnit.unit, fun _ {_ _} _ => trivial, fun _ _ => trivial⟩
  uniq _ _ _ := trivial

/-! ## binary products -/

def prodP (F G : Psh.{0, 0, 0} C) : Psh.{0, 0, 0} C where
  obj c := ⟨(F.obj c).car × (G.obj c).car, fun x y => (F.obj c).eqv x.1 y.1 ∧ (G.obj c).eqv x.2 y.2,
    fun _ => ⟨(F.obj c).refl _, (G.obj c).refl _⟩, fun h => ⟨(F.obj c).symm h.1, (G.obj c).symm h.2⟩,
    fun h k => ⟨(F.obj c).trans h.1 k.1, (G.obj c).trans h.2 k.2⟩⟩
  map f x := (F.map f x.1, G.map f x.2)
  map_eqv {_ _} f {_ _} h := ⟨F.map_eqv f h.1, G.map_eqv f h.2⟩
  map_resp x e := ⟨F.map_resp x.1 e, G.map_resp x.2 e⟩
  map_id _ x := ⟨F.map_id _ x.1, G.map_id _ x.2⟩
  map_comp f g x := ⟨F.map_comp f g x.1, G.map_comp f g x.2⟩

def fstM {F G : Psh.{0, 0, 0} C} {c : C.Ob} {S : Sieve C c} (m : MFam (prodP F G) S) : MFam F S where
  x f hf := (m.x f hf).1
  resp hf hf' e := (m.resp hf hf' e).1
  compat f hf g := (m.compat f hf g).1

def sndM {F G : Psh.{0, 0, 0} C} {c : C.Ob} {S : Sieve C c} (m : MFam (prodP F G) S) : MFam G S where
  x f hf := (m.x f hf).2
  resp hf hf' e := (m.resp hf hf' e).2
  compat f hf g := (m.compat f hf g).2

theorem prodP_sheaf {F G : Psh.{0, 0, 0} C} (hF : IsSheaf J F) (hG : IsSheaf J G) : IsSheaf J (prodP F G) :=
  fun c S hS m => match (hF c S hS (fstM m)).1, (hG c S hS (sndM m)).1 with
    | ⟨a, ha⟩, ⟨b, hb⟩ => ⟨⟨(a, b), fun d f hf => ⟨ha d f hf, hb d f hf⟩⟩,
      fun x y hx hy => ⟨(hF c S hS (fstM m)).2 x.1 y.1 (fun d f hf => (hx d f hf).1) (fun d f hf => (hy d f hf).1),
        (hG c S hS (sndM m)).2 x.2 y.2 (fun d f hf => (hx d f hf).2) (fun d f hf => (hy d f hf).2)⟩⟩

def shProduct (F G : (ShCat J).Ob) : Product (ShCat J) F G where
  P := ⟨prodP F.1 G.1, prodP_sheaf J F.2 G.2⟩
  p1 := ⟨fun _ x => x.1, fun _ {_ _} h => h.1, fun _ _ => (F.1.obj _).refl _⟩
  p2 := ⟨fun _ x => x.2, fun _ {_ _} h => h.2, fun _ _ => (G.1.obj _).refl _⟩
  pair α β := ⟨fun c x => (α.app c x, β.app c x), fun c {_ _} h => ⟨α.app_eqv c h, β.app_eqv c h⟩,
    fun f x => ⟨α.nat f x, β.nat f x⟩⟩
  beta1 _ _ _ _ := (F.1.obj _).refl _
  beta2 _ _ _ _ := (G.1.obj _).refl _
  eta _ _ _ := ⟨(F.1.obj _).refl _, (G.1.obj _).refl _⟩
  pair_resp h k c x := ⟨h c x, k c x⟩

end Gt

end Found

#print axioms Found.Gt.ShCat
#print axioms Found.Gt.shTerminal
#print axioms Found.Gt.prodP_sheaf
#print axioms Found.Gt.shProduct

found/Gt80.lean

lean_all/found/Gt80.lean

/-
  T194 (classifying topos), part 80. found/Gt80 — the inverse image of a
  flat F preserves binary products: a pair of elements of (P ⊗ F)⁺ and
  (Q ⊗ F)⁺ gives an element of ((P × Q) ⊗ F)⁺ (locally, pair the two
  triples' F-parts), whose projections are the given elements.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt79

namespace Found

namespace Gt

open Sh

variable {C : ECat.{0, 0}} {D : ECat.{0, 0}} (K : GTop D) (F : EFun C (ShCat K)) (hF : FlatF K F)
  (Kc : CP.Classical')

/-- a triple restricted along a lift is equivalent to the lifted triple -/
theorem tri_lift (P : Psh.{0, 0, 0} C) {c d : D.Ob} (t : Tri K F P c) (h : D.Hom d c) {E : C.Ob} (u : C.Hom E t.A)
    (z : ((F.obj E).1.obj d).car) (hz : ((F.obj t.A).1.obj d).eqv ((F.map u).app d z) ((F.obj t.A).1.map h t.x)) :
    LocEq K F P ⟨E, P.map u t.p, z⟩ ⟨t.A, t.p, (F.obj t.A).1.map h t.x⟩ :=
  cov_mono K (K.top d) (fun _ k _ => ⟨E, C.id E, u, (F.obj E).1.map k z, F.map_id E _ _,
    ((F.obj t.A).1.obj _).trans ((F.map u).nat k z) ((F.obj t.A).1.map_eqv k hz), P.map_id E _⟩)

/-- restricting an element of P⁺ along a member of its sieve gives the unit of its value -/
theorem res_eta {X : Psh.{0, 0, 0} D} {c d : D.Ob} (a : PElt K X c) (f : D.Hom d c) (hf : a.S.S f) :
    PEq K X (resE K X f a) (etaE K X (a.m.x f hf)) :=
  ⟨maxS d, K.top d, fun _ _ _ => ⟨a.S.stable hf _, trivial⟩, fun _ _ _ _ _ => a.m.compat f hf _⟩

section Pr

variable (P Q : Psh.{0, 0, 0} C)

def PrS {c : D.Ob} (a : PElt K (TensP K F P hF) c) (b : PElt K (TensP K F Q hF) c) : Sieve D c where
  S := fun {d} g => ∃ (ha : a.S.S g) (hb : b.S.S g) (w : Tri K F (prodP P Q) d),
    LocEq K F P (triFst K F P Q w) (a.m.x g ha) ∧ LocEq K F Q (triSnd K F P Q w) (b.m.x g hb)
  resp := fun e ⟨ha, hb, w, h1, h2⟩ => ⟨a.S.resp e ha, b.S.resp e hb, w,
    locEq_trans K F P hF h1 (a.m.resp ha (a.S.resp e ha) e), locEq_trans K F Q hF h2 (b.m.resp hb (b.S.resp e hb) e)⟩
  stable := fun {_ _} {g} ⟨ha, hb, w, h1, h2⟩ k => ⟨a.S.stable ha k, b.S.stable hb k, (TensP K F (prodP P Q) hF).map k w,
    locEq_trans K F P hF ((TensP K F P hF).map_eqv k h1) (locEq_symm K F P (a.m.compat g ha k)),
    locEq_trans K F Q hF ((TensP K F Q hF).map_eqv k h2) (locEq_symm K F Q (b.m.compat g hb k))⟩

theorem prS_cov {c : D.Ob} (a : PElt K (TensP K F P hF) c) (b : PElt K (TensP K F Q hF) c) :
    K.cov (PrS K F hF P Q a b) :=
  K.trans (cov_meet K a.cov b.cov) (fun _ g ⟨ha, hb⟩ => cov_mono K (hF.pair (a.m.x g ha).x (b.m.x g hb).x)
    (fun _ h ⟨E, u, v, z, hu, hv⟩ => ⟨a.S.stable ha h, b.S.stable hb h,
      ⟨E, (P.map u (a.m.x g ha).p, Q.map v (b.m.x g hb).p), z⟩,
      locEq_trans K F P hF (tri_lift K F P _ h u z hu) (locEq_symm K F P (a.m.compat g ha h)),
      locEq_trans K F Q hF (tri_lift K F Q _ h v z hv) (locEq_symm K F Q (b.m.compat g hb h))⟩))

section E

variable {c : D.Ob} (a : PElt K (TensP K F P hF) c) (b : PElt K (TensP K F Q hF) c)

theorem prS_a {d : D.Ob} {g : D.Hom d c} (hg : (PrS K F hF P Q a b).S g) : a.S.S g :=
  match hg with | ⟨ha, _, _, _, _⟩ => ha
theorem prS_b {d : D.Ob} {g : D.Hom d c} (hg : (PrS K F hF P Q a b).S g) : b.S.S g :=
  match hg with | ⟨_, hb, _, _, _⟩ => hb

def PrW {d : D.Ob} {g : D.Hom d c} (hg : (PrS K F hF P Q a b).S g) (w : Tri K F (prodP P Q) d) : Prop :=
  LocEq K F P (triFst K F P Q w) (a.m.x g (prS_a K F hF P Q a b hg)) ∧
  LocEq K F Q (triSnd K F P Q w) (b.m.x g (prS_b K F hF P Q a b hg))

theorem prW_ex {d : D.Ob} {g : D.Hom d c} (hg : (PrS K F hF P Q a b).S g) : ∃ w, PrW K F hF P Q a b hg w :=
  match hg with
  | ⟨ha, hb, w, h1, h2⟩ => ⟨w, locEq_trans K F P hF h1 (a.m.resp ha _ (D.hrefl g)),
      locEq_trans K F Q hF h2 (b.m.resp hb _ (D.hrefl g))⟩

def pickPr {d : D.Ob} {g : D.Hom d c} (hg : (PrS K F hF P Q a b).S g) : Tri K F (prodP P Q) d :=
  Kc.ch (PrW K F hF P Q a b hg) (prW_ex K F hF P Q a b hg)

theorem pickPr_spec {d : D.Ob} {g : D.Hom d c} (hg : (PrS K F hF P Q a b).S g) :
    PrW K F hF P Q a b hg (pickPr K F hF Kc P Q a b hg) :=
  Kc.ch_spec (PrW K F hF P Q a b hg) (prW_ex K F hF P Q a b hg)

def prElt : PElt K (TensP K F (prodP P Q) hF) c :=
  ⟨PrS K F hF P Q a b, prS_cov K F hF P Q a b,
   ⟨fun _ hg => pickPr K F hF Kc P Q a b hg,
    fun {_} {_ _} hg hg' e => locEq_pair K F hF P Q _ _
      (locEq_trans K F P hF (pickPr_spec K F hF Kc P Q a b hg).1 (locEq_trans K F P hF (a.m.resp _ _ e)
        (locEq_symm K F P (pickPr_spec K F hF Kc P Q a b hg').1)))
      (locEq_trans K F Q hF (pickPr_spec K F hF Kc P Q a b hg).2 (locEq_trans K F Q hF (b.m.resp _ _ e)
        (locEq_symm K F Q (pickPr_spec K F hF Kc P Q a b hg').2))),
    fun g hg k => locEq_pair K F hF P Q _ _
      (locEq_trans K F P hF (pickPr_spec K F hF Kc P Q a b _).1 (locEq_trans K F P hF (a.m.compat g _ k)
        ((TensP K F P hF).map_eqv k (locEq_symm K F P (pickPr_spec K F hF Kc P Q a b hg).1))))
      (locEq_trans K F Q hF (pickPr_spec K F hF Kc P Q a b _).2 (locEq_trans K F Q hF (b.m.compat g _ k)
        ((TensP K F Q hF).map_eqv k (locEq_symm K F Q (pickPr_spec K F hF Kc P Q a b hg).2))))⟩⟩

end E

end Pr

end Gt

end Found

#print axioms Found.Gt.tri_lift
#print axioms Found.Gt.res_eta
#print axioms Found.Gt.prS_cov
#print axioms Found.Gt.prElt

found/Gt81.lean

lean_all/found/Gt81.lean

/-
  T194 (classifying topos), part 81. found/Gt81 — the inverse image of a
  flat continuous F preserves binary products: the paired element is
  natural, and its images under f*(π₁), f*(π₂) are the given elements
  (compare on the pairing cover in the sheaf (P ⊗ F)⁺).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt80

namespace Found

namespace Gt

open Sh

variable {C : ECat.{0, 0}} {D : ECat.{0, 0}} (J : GTop C) (K : GTop D) (F : EFun C (ShCat K)) (hF : FlatF K F)
  (Kc : CP.Classical') (P Q : Psh.{0, 0, 0} C)

def prodN : PNat (prodP (Plus K (TensP K F P hF)) (Plus K (TensP K F Q hF))) (Plus K (TensP K F (prodP P Q) hF)) where
  app _ ab := prElt K F hF Kc P Q ab.1 ab.2
  app_eqv _ {ab ab'} h := match h.1, h.2 with
    | ⟨R1, hR1, _, e1⟩, ⟨R2, hR2, _, e2⟩ =>
      ⟨meetS (meetS R1 R2) (meetS (PrS K F hF P Q ab.1 ab.2) (PrS K F hF P Q ab'.1 ab'.2)),
       cov_meet K (cov_meet K hR1 hR2) (cov_meet K (prS_cov K F hF P Q _ _) (prS_cov K F hF P Q _ _)),
       fun _ _ r => ⟨r.2.1, r.2.2⟩, fun _ f h1 h2 r => locEq_pair K F hF P Q _ _
        (locEq_trans K F P hF (pickPr_spec K F hF Kc P Q _ _ h1).1 (locEq_trans K F P hF
          (e1 _ f _ (prS_a K F hF P Q _ _ h2) r.1.1) (locEq_symm K F P (pickPr_spec K F hF Kc P Q _ _ h2).1)))
        (locEq_trans K F Q hF (pickPr_spec K F hF Kc P Q _ _ h1).2 (locEq_trans K F Q hF
          (e2 _ f _ (prS_b K F hF P Q _ _ h2) r.1.2) (locEq_symm K F Q (pickPr_spec K F hF Kc P Q _ _ h2).2)))⟩
  nat {_ _} f ab :=
    ⟨meetS (PrS K F hF P Q (resE K _ f ab.1) (resE K _ f ab.2)) (pullS f (PrS K F hF P Q ab.1 ab.2)),
     cov_meet K (prS_cov K F hF P Q _ _) (K.pull f (prS_cov K F hF P Q _ _)),
     fun _ _ r => ⟨r.1, r.2⟩, fun _ _ h1 h2 _ => locEq_pair K F hF P Q _ _
      (locEq_trans K F P hF (pickPr_spec K F hF Kc P Q _ _ h1).1 (locEq_symm K F P
        (locEq_trans K F P hF (pickPr_spec K F hF Kc P Q _ _ h2).1 (ab.1.m.resp _ _ (D.hrefl _)))))
      (locEq_trans K F Q hF (pickPr_spec K F hF Kc P Q _ _ h1).2 (locEq_symm K F Q
        (locEq_trans K F Q hF (pickPr_spec K F hF Kc P Q _ _ h2).2 (ab.2.m.resp _ _ (D.hrefl _)))))⟩

/-- the projections of the paired element -/
theorem inv_prod (P Q : (ShCat J).Ob) :
    ∃ g : (ShCat K).Hom (shProduct K ((InvF J K F hF Kc).obj P) ((InvF J K F hF Kc).obj Q)).P
      ((InvF J K F hF Kc).obj (shProduct J P Q).P),
      (ShCat K).heq ((ShCat K).comp g ((shProduct K ((InvF J K F hF Kc).obj P) ((InvF J K F hF Kc).obj Q)).pair
        ((InvF J K F hF Kc).map (shProduct J P Q).p1) ((InvF J K F hF Kc).map (shProduct J P Q).p2))) ((ShCat K).id _) := by
  refine ⟨prodN K F hF Kc P.1 Q.1, fun c ab => ⟨?_, ?_⟩⟩
  · let X := Plus K (TensP K F P.1 hF)
    refine sheaf_sep K X (plus_sheaf K (TensP K F P.1 hF) Kc (tensP_sep K F P.1 hF)) c _
      (prS_cov K F hF P.1 Q.1 ab.1 ab.2) _ _ (fun d f hf => ?_)
    exact peq_trans K _ (extA_spec K (TensP K F (prodP P.1 Q.1) hF) X (plus_sheaf K (TensP K F P.1 hF) Kc (tensP_sep K F P.1 hF))
      (compN (tensMap K F hF (shProduct J P Q).p1) (eta K (TensP K F P.1 hF))) Kc _ d f hf)
      (peq_trans K _ ((eta K (TensP K F P.1 hF)).app_eqv d (pickPr_spec K F hF Kc P.1 Q.1 ab.1 ab.2 hf).1)
        (peq_symm K _ (res_eta K ab.1 f _)))
  · let X := Plus K (TensP K F Q.1 hF)
    refine sheaf_sep K X (plus_sheaf K (TensP K F Q.1 hF) Kc (tensP_sep K F Q.1 hF)) c _
      (prS_cov K F hF P.1 Q.1 ab.1 ab.2) _ _ (fun d f hf => ?_)
    exact peq_trans K _ (extA_spec K (TensP K F (prodP P.1 Q.1) hF) X (plus_sheaf K (TensP K F Q.1 hF) Kc (tensP_sep K F Q.1 hF))
      (compN (tensMap K F hF (shProduct J P Q).p2) (eta K (TensP K F Q.1 hF))) Kc _ d f hf)
      (peq_trans K _ ((eta K (TensP K F Q.1 hF)).app_eqv d (pickPr_spec K F hF Kc P.1 Q.1 ab.1 ab.2 hf).2)
        (peq_symm K _ (res_eta K ab.2 f _)))

end Gt

end Found

#print axioms Found.Gt.prodN
#print axioms Found.Gt.inv_prod

found/Gt82.lean

lean_all/found/Gt82.lean

/-
  T194 (classifying topos), part 82. found/Gt82 — toward equalizer
  preservation by the inverse image of a flat F: the unit P ⊗ F → (P ⊗ F)⁺
  reflects equivalence; if f*(α)(x) ≈ f*(β)(x), then locally the triples of
  x lift to triples of Eq(α, β) ⊗ F (equalize the two arrows of a local
  witness).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt81

namespace Found

namespace Gt

open Sh

variable {C : ECat.{0, 0}} {D : ECat.{0, 0}} (J : GTop C) (K : GTop D) (F : EFun C (ShCat K)) (hF : FlatF K F)
  (Kc : CP.Classical')

theorem eta_inj (P : Psh.{0, 0, 0} C) {c : D.Ob} {s s' : Tri K F P c}
    (h : PEq K (TensP K F P hF) (etaE K (TensP K F P hF) s) (etaE K (TensP K F P hF) s')) : LocEq K F P s s' :=
  match h with
  | ⟨R, hR, _, e⟩ => tensP_sep K F P hF c R hR s s' (fun d f hf => e d f trivial trivial hf)

section Q

variable {P Q : Psh.{0, 0, 0} C} (α β : PNat P Q)

def evTri {c : D.Ob} (s : Tri K F (eqP α β) c) : Tri K F P c := ⟨s.A, evE s.p, s.x⟩

/-- stages where the triple of x lifts to the equalizer -/
def EqzR {c : D.Ob} (x : PElt K (TensP K F P hF) c) : Sieve D c where
  S := fun {d} g => ∃ (hx : x.S.S g) (s : Tri K F (eqP α β) d), LocEq K F P (evTri K F α β s) (x.m.x g hx)
  resp := fun e ⟨hx, s, h⟩ => ⟨x.S.resp e hx, s, locEq_trans K F P hF h (x.m.resp hx _ e)⟩
  stable := fun {_ _} {g} ⟨hx, s, h⟩ k => ⟨x.S.stable hx k, (TensP K F (eqP α β) hF).map k s,
    locEq_trans K F P hF ((TensP K F P hF).map_eqv k h) (locEq_symm K F P (x.m.compat g hx k))⟩

end Q

theorem eqzR_cov (P Q : (ShCat J).Ob) (α β : (ShCat J).Hom P Q) {c : D.Ob} (x : PElt K (TensP K F P.1 hF) c)
    (h : PEq K (TensP K F Q.1 hF) ((invMap J K F hF Kc α).app c x) ((invMap J K F hF Kc β).app c x)) :
    K.cov (EqzR K F hF α β x) := by
  let hQ := plus_sheaf K (TensP K F Q.1 hF) Kc (tensP_sep K F Q.1 hF)
  refine K.trans x.cov (fun d f hf => ?_)
  let t := x.m.x f hf
  have hα := extA_spec K (TensP K F P.1 hF) (Plus K (TensP K F Q.1 hF)) hQ
    (compN (tensMap K F hF α) (eta K (TensP K F Q.1 hF))) Kc x d f hf
  have hβ := extA_spec K (TensP K F P.1 hF) (Plus K (TensP K F Q.1 hF)) hQ
    (compN (tensMap K F hF β) (eta K (TensP K F Q.1 hF))) Kc x d f hf
  have le := eta_inj K F hF Q.1 (peq_trans K _ (peq_symm K _ hα) (peq_trans K _ (plus_map_eqv K _ f h) hβ))
  refine K.trans le (fun e g ⟨E, u, v, z, h1, h2, h3⟩ => ?_)
  refine cov_mono K (hF.eqz u v z (((F.obj t.A).1.obj e).trans h1 (((F.obj t.A).1.obj e).symm h2)))
    (fun e' g' ⟨E', w, z', hw, hz'⟩ => ?_)
  have hp : (Q.1.obj E').eqv (α.app E' (P.1.map (C.comp w u) t.p)) (β.app E' (P.1.map (C.comp w u) t.p)) :=
    (Q.1.obj E').trans (α.nat _ t.p) ((Q.1.obj E').trans (Q.1.map_comp w u _) ((Q.1.obj E').trans
      (Q.1.map_eqv w h3) ((Q.1.obj E').trans ((Q.1.obj E').symm (Q.1.map_comp w v _)) ((Q.1.obj E').trans
        (Q.1.map_resp _ (C.hsymm hw)) ((Q.1.obj E').symm (β.nat _ t.p))))))
  let sE : Tri K F (eqP α β) e' := ⟨E', mkE (P.1.map (C.comp w u) t.p) hp, z'⟩
  have hx : x.S.S (D.comp (D.comp g' g) f) := x.S.stable hf (D.comp g' g)
  refine ⟨hx, sE, ?_⟩
  exact locEq_trans K F P.1 hF (tri_lift K F P.1 t (D.comp g' g) (C.comp w u) z' (fcmp K F u w g g' h1 hz'))
    (locEq_symm K F P.1 (x.m.compat f hf (D.comp g' g)))

end Gt

end Found

#print axioms Found.Gt.eta_inj
#print axioms Found.Gt.EqzR
#print axioms Found.Gt.eqzR_cov

found/Gt83.lean

lean_all/found/Gt83.lean

/-
  T194 (classifying topos), part 83. found/Gt83 — Diaconescu, second
  half, assembled: the inverse image of a flat continuous F preserves
  equalizers (the local lifts to Eq(α, β) ⊗ F glue), so (f*, f_*) is a
  left exact geometric morphism Sh(D, K) → Sh(C, J).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt82

namespace Found

namespace Gt

open Sh

variable {C : ECat.{0, 0}} {D : ECat.{0, 0}} (J : GTop C) (K : GTop D) (F : EFun C (ShCat K)) (hF : FlatF K F)
  (Kc : CP.Classical') (hC : ContF J K F)

section Q

variable {P Q : Psh.{0, 0, 0} C} (α β : PNat P Q)

theorem locEq_ev {c : D.Ob} {s s' : Tri K F (eqP α β) c} (h : LocEq K F P (evTri K F α β s) (evTri K F α β s')) :
    LocEq K F (eqP α β) s s' :=
  cov_mono K h (fun _ _ ⟨E, u, v, z, h1, h2, h3⟩ => ⟨E, u, v, z, h1, h2, h3⟩)

variable {c : D.Ob} (x : PElt K (TensP K F P hF) c)

def EqzW {d : D.Ob} {g : D.Hom d c} (hg : (EqzR K F hF α β x).S g) (s : Tri K F (eqP α β) d) : Prop :=
  LocEq K F P (evTri K F α β s) (x.m.x g (match hg with | ⟨hx, _, _⟩ => hx))

theorem eqzW_ex {d : D.Ob} {g : D.Hom d c} (hg : (EqzR K F hF α β x).S g) : ∃ s, EqzW K F hF α β x hg s :=
  match hg with
  | ⟨hx, s, h⟩ => ⟨s, locEq_trans K F P hF h (x.m.resp hx _ (D.hrefl g))⟩

def pickEq {d : D.Ob} {g : D.Hom d c} (hg : (EqzR K F hF α β x).S g) : Tri K F (eqP α β) d :=
  Kc.ch (EqzW K F hF α β x hg) (eqzW_ex K F hF α β x hg)

theorem pickEq_spec {d : D.Ob} {g : D.Hom d c} (hg : (EqzR K F hF α β x).S g) :
    EqzW K F hF α β x hg (pickEq K F hF Kc α β x hg) :=
  Kc.ch_spec (EqzW K F hF α β x hg) (eqzW_ex K F hF α β x hg)

def eqMF : MFam (TensP K F (eqP α β) hF) (EqzR K F hF α β x) where
  x _ hg := pickEq K F hF Kc α β x hg
  resp {_} {_ _} hg hg' e := locEq_ev K F α β (locEq_trans K F P hF (pickEq_spec K F hF Kc α β x hg)
    (locEq_trans K F P hF (x.m.resp _ _ e) (locEq_symm K F P (pickEq_spec K F hF Kc α β x hg'))))
  compat g hg k := locEq_ev K F α β (locEq_trans K F P hF (pickEq_spec K F hF Kc α β x _)
    (locEq_trans K F P hF (x.m.compat g _ k) ((TensP K F P hF).map_eqv k
      (locEq_symm K F P (pickEq_spec K F hF Kc α β x hg)))))

end Q

/-- the geometric morphism of a flat continuous functor -/
def geomD : Geom J K where
  inv := InvF J K F hF Kc
  dir := DirF J K F hF Kc hC
  adj := adjD J K F hF Kc hC
  term_iso := inv_term J K F hF Kc
  prod_iso := inv_prod J K F hF Kc

theorem geomD_eq : EqPres J K (geomD J K F hF Kc hC) := by
  intro P Q α β c x h
  let hP := plus_sheaf K (TensP K F P.1 hF) Kc (tensP_sep K F P.1 hF)
  have hcov := eqzR_cov J K F hF Kc P Q α β x h
  let w : PElt K (TensP K F (eqP α β) hF) c := ⟨EqzR K F hF α β x, hcov, eqMF K F hF Kc α β x⟩
  refine ⟨w, sheaf_sep K _ hP c _ hcov _ _ (fun d g hg => ?_)⟩
  exact peq_trans K _ (extA_spec K (TensP K F (eqP α β) hF) (Plus K (TensP K F P.1 hF)) hP
      (compN (tensMap K F hF (evN α β)) (eta K (TensP K F P.1 hF))) Kc w d g hg)
    (peq_trans K _ ((eta K (TensP K F P.1 hF)).app_eqv d (pickEq_spec K F hF Kc α β x hg))
      (peq_symm K _ (res_eta K x g _)))

end Gt

end Found

#print axioms Found.Gt.eqMF
#print axioms Found.Gt.geomD
#print axioms Found.Gt.geomD_eq

found/Gt84.lean

lean_all/found/Gt84.lean

/-
  T194 (classifying topos), part 84. found/Gt84 — Diaconescu round trip on
  functors: for a flat continuous F on a subcanonical site, the functor of
  its geometric morphism, A ↦ f*(y A) = (y A ⊗ F)⁺, is isomorphic to F:
  x ↦ [A, id, x] with inverse [B, u, y] ↦ F(u)(y), natural in A.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt83

namespace Found

namespace Gt

open Sh

variable {C : ECat.{0, 0}} {D : ECat.{0, 0}} (J : GTop C) (K : GTop D) (F : EFun C (ShCat K)) (hF : FlatF K F)
  (Kc : CP.Classical') (A : C.Ob)

/-- evaluation y A ⊗ F → F(A) -/
def evT : PNat (TensP K F (yo C A) hF) (F.obj A).1 where
  app _ t := (F.map t.p).app _ t.x
  app_eqv c {t t'} h := sheaf_sep K (F.obj A).1 (F.obj A).2 c _ h _ _ (fun d g ⟨_, u, v, z, h1, h2, h3⟩ =>
    ((F.obj A).1.obj d).trans (((F.obj A).1.obj d).symm ((F.map t.p).nat g t.x)) (((F.obj A).1.obj d).trans
      ((F.map t.p).app_eqv d (((F.obj t.A).1.obj d).symm h1)) (((F.obj A).1.obj d).trans
        (((F.obj A).1.obj d).symm (F.map_comp u t.p d z)) (((F.obj A).1.obj d).trans (F.map_resp h3 d z)
          (((F.obj A).1.obj d).trans (F.map_comp v t'.p d z) (((F.obj A).1.obj d).trans
            ((F.map t'.p).app_eqv d h2) ((F.map t'.p).nat g t'.x)))))))
  nat _ t := (F.map t.p).nat _ t.x

/-- the unit F(A) → (y A ⊗ F)⁺ -/
def toT : PNat (F.obj A).1 (Plus K (TensP K F (yo C A) hF)) where
  app _ x := etaE K (TensP K F (yo C A) hF) ⟨A, C.id A, x⟩
  app_eqv c {x _} h := (eta K (TensP K F (yo C A) hF)).app_eqv c (cov_mono K (K.top c) (fun _ g _ =>
    ⟨A, C.id A, C.id A, (F.obj A).1.map g x, F.map_id A _ _, ((F.obj A).1.obj _).trans (F.map_id A _ _)
      ((F.obj A).1.map_eqv g h), C.hrefl _⟩))
  nat f x := (eta K (TensP K F (yo C A) hF)).nat f ⟨A, C.id A, x⟩

def fromT : PNat (Plus K (TensP K F (yo C A) hF)) (F.obj A).1 :=
  extN K (TensP K F (yo C A) hF) (F.obj A).1 (F.obj A).2 (evT K F hF A) Kc

theorem from_to {c : D.Ob} (x : ((F.obj A).1.obj c).car) :
    ((F.obj A).1.obj c).eqv ((fromT K F hF Kc A).app c ((toT K F hF A).app c x)) x :=
  ((F.obj A).1.obj c).trans (ext_unit K (TensP K F (yo C A) hF) (F.obj A).1 (F.obj A).2 (evT K F hF A) Kc c _)
    (F.map_id A c x)

theorem tri_id_lift {c : D.Ob} (t : Tri K F (yo C A) c) :
    LocEq K F (yo C A) ⟨A, C.id A, (F.map t.p).app c t.x⟩ t :=
  cov_mono K (K.top c) (fun _ g _ => ⟨t.A, t.p, C.id t.A, (F.obj t.A).1.map g t.x, (F.map t.p).nat g t.x,
    F.map_id t.A _ _, C.htrans (C.comp_id t.p) (C.hsymm (C.id_comp t.p))⟩)

theorem to_from {c : D.Ob} (a : PElt K (TensP K F (yo C A) hF) c) :
    PEq K (TensP K F (yo C A) hF) ((toT K F hF A).app c ((fromT K F hF Kc A).app c a)) a :=
  ext_uniq K (TensP K F (yo C A) hF) (Plus K (TensP K F (yo C A) hF))
    (plus_sheaf K (TensP K F (yo C A) hF) Kc (tensP_sep K F (yo C A) hF))
    (compN (fromT K F hF Kc A) (toT K F hF A)) (idN _) (fun c t => peq_trans K _
      ((toT K F hF A).app_eqv c (ext_unit K (TensP K F (yo C A) hF) (F.obj A).1 (F.obj A).2 (evT K F hF A) Kc c t))
      ((eta K (TensP K F (yo C A) hF)).app_eqv c (tri_id_lift K F A t))) c a

/-- naturality of the unit in A -/
theorem to_nat {A' : C.Ob} (v : C.Hom A A') {c : D.Ob} (x : ((F.obj A).1.obj c).car) :
    LocEq K F (yo C A') ⟨A', C.id A', (F.map v).app c x⟩ ⟨A, C.comp (C.id A) v, x⟩ :=
  cov_mono K (K.top c) (fun _ g _ => ⟨A, v, C.id A, (F.obj A).1.map g x, (F.map v).nat g x, F.map_id A _ _,
    C.htrans (C.comp_id v) (C.hsymm (C.htrans (C.id_comp _) (C.id_comp v)))⟩)

end Gt

end Found

#print axioms Found.Gt.evT
#print axioms Found.Gt.from_to
#print axioms Found.Gt.to_from
#print axioms Found.Gt.to_nat

found/Gt85.lean

lean_all/found/Gt85.lean

/-
  T194 (classifying topos), part 85. found/Gt85 — Diaconescu round trip on
  geometric morphisms: for a geometric morphism f over a subcanonical
  site, f_*(Q)(A) ≅ Hom(f*(y A), Q) (adjunction and Yoneda), naturally in
  A: the direct image of f agrees with that of the geometric morphism of
  the functor f* ∘ y.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt84

namespace Found

namespace Gt

open Sh

variable {C : ECat.{0, 0}} (J : GTop C) (hsub : ∀ A, IsSheaf J (yo C A)) {D : ECat.{0, 0}} (K : GTop D)
  (f : Geom J K) (Q : (ShCat K).Ob)

/-- the element of f_* Q at A as a map out of y A -/
def elMap {A : C.Ob} (q : ((f.dir.obj Q).1.obj A).car) : (ShCat J).Hom ((yoE J hsub).obj A) (f.dir.obj Q) := yoEl q

def dirTo : PNat (f.dir.obj Q).1 (DirP K (Ff J hsub K f) Q) where
  app _ q := f.adj.psi (elMap J hsub K f Q q)
  app_eqv _ {_ _} h := f.adj.psi_resp (fun _ g => (f.dir.obj Q).1.map_eqv g h)
  nat {A' A} u q := by
    let CJ := ShCat J
    let CK := ShCat K
    let yu : CJ.Hom ((yoE J hsub).obj A') ((yoE J hsub).obj A) := (yoE J hsub).map u
    have e1 : CJ.heq (elMap J hsub K f Q ((f.dir.obj Q).1.map u q)) (CJ.comp yu (elMap J hsub K f Q q)) :=
      fun E g => ((f.dir.obj Q).1.obj E).symm ((f.dir.obj Q).1.map_comp g u q)
    have e2 : CJ.heq (CJ.comp yu (elMap J hsub K f Q q)) (CJ.comp yu (f.adj.phi (f.adj.psi (elMap J hsub K f Q q)))) :=
      CJ.comp_resp (CJ.hrefl yu) (CJ.hsymm (f.adj.phi_psi _))
    have e3 : CJ.heq (CJ.comp yu (f.adj.phi (f.adj.psi (elMap J hsub K f Q q))))
        (f.adj.phi (CK.comp (f.inv.map yu) (f.adj.psi (elMap J hsub K f Q q)))) :=
      CJ.hsymm (f.adj.nat_l yu _)
    have e4 := f.adj.psi_resp (CJ.htrans e1 (CJ.htrans e2 e3))
    exact CK.htrans e4 (f.adj.psi_phi _)

def dirFrom : PNat (DirP K (Ff J hsub K f) Q) (f.dir.obj Q).1 where
  app A β := (f.adj.phi β).app A (C.id A)
  app_eqv A {_ _} h := f.adj.phi_resp h A (C.id A)
  nat {A' A} u β := by
    let CJ := ShCat J
    have e := f.adj.nat_l ((yoE J hsub).map u) β A' (C.id A')
    refine ((f.dir.obj Q).1.obj A').trans e ?_
    show ((f.dir.obj Q).1.obj A').eqv ((f.adj.phi β).app A' (C.comp (C.id A') u)) _
    exact ((f.dir.obj Q).1.obj A').trans ((f.adj.phi β).app_eqv A' (C.htrans (C.id_comp u) (C.hsymm (C.comp_id u))))
      ((f.adj.phi β).nat u (C.id A))

theorem dir_from_to {A : C.Ob} (q : ((f.dir.obj Q).1.obj A).car) :
    ((f.dir.obj Q).1.obj A).eqv ((dirFrom J hsub K f Q).app A ((dirTo J hsub K f Q).app A q)) q :=
  ((f.dir.obj Q).1.obj A).trans (f.adj.phi_psi (elMap J hsub K f Q q) A (C.id A)) ((f.dir.obj Q).1.map_id A q)

theorem dir_to_from {A : C.Ob} (β : ((DirP K (Ff J hsub K f) Q).obj A).car) :
    ((DirP K (Ff J hsub K f) Q).obj A).eqv ((dirTo J hsub K f Q).app A ((dirFrom J hsub K f Q).app A β)) β := by
  have e : (ShCat J).heq (elMap J hsub K f Q ((f.adj.phi β).app A (C.id A))) (f.adj.phi β) := fun E g =>
    ((f.dir.obj Q).1.obj E).trans ((f.dir.obj Q).1.obj E |>.symm ((f.adj.phi β).nat g (C.id A)))
      ((f.adj.phi β).app_eqv E (C.comp_id g))
  exact (ShCat K).htrans (f.adj.psi_resp e) (f.adj.psi_phi β)

end Gt

end Found

#print axioms Found.Gt.dirTo
#print axioms Found.Gt.dirFrom
#print axioms Found.Gt.dir_from_to
#print axioms Found.Gt.dir_to_from

found/Gt86.lean

lean_all/found/Gt86.lean

/-
  T194 (classifying topos), part 86. found/Gt86 — natural isomorphisms of
  functors C_T → Sh(D, K) transport the associated structures: if F ≅ G
  then N_F ≅ N_G; isomorphisms of structures compose and invert.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt85

namespace Found

namespace Gt

open Sh

/-- a natural isomorphism of functors into sheaves -/
structure FIso {C D : ECat.{0, 0}} {K : GTop D} (F G : EFun C (ShCat K)) where
  to : (A : C.Ob) → PNat (F.obj A).1 (G.obj A).1
  fr : (A : C.Ob) → PNat (G.obj A).1 (F.obj A).1
  tf : ∀ A c x, ((G.obj A).1.obj c).eqv ((to A).app c ((fr A).app c x)) x
  ft : ∀ A c x, ((F.obj A).1.obj c).eqv ((fr A).app c ((to A).app c x)) x
  nat : ∀ {A B} (u : C.Hom A B) c x,
    ((G.obj B).1.obj c).eqv ((to B).app c ((F.map u).app c x)) ((G.map u).app c ((to A).app c x))

theorem FIso.nat_fr {C D : ECat.{0, 0}} {K : GTop D} {F G : EFun C (ShCat K)} (φ : FIso F G) {A B : C.Ob}
    (u : C.Hom A B) (c : D.Ob) (y : ((G.obj A).1.obj c).car) :
    ((F.obj B).1.obj c).eqv ((φ.fr B).app c ((G.map u).app c y)) ((F.map u).app c ((φ.fr A).app c y)) :=
  ((F.obj B).1.obj c).trans ((φ.fr B).app_eqv c ((G.map u).app_eqv c ((G.obj A).1.obj c |>.symm (φ.tf A c y))))
    (((F.obj B).1.obj c).trans ((φ.fr B).app_eqv c ((G.obj B).1.obj c |>.symm (φ.nat u c _))) (φ.ft B c _))

def FIso.symm {C D : ECat.{0, 0}} {K : GTop D} {F G : EFun C (ShCat K)} (φ : FIso F G) : FIso G F where
  to := φ.fr
  fr := φ.to
  tf := φ.ft
  ft := φ.tf
  nat u c y := φ.nat_fr u c y

def SIso.trans {D : ECat.{0, 0}} {M M' M'' : @GStr D} (a : SIso M M') (b : SIso M' M'') : SIso M M'' where
  fwd := compN a.fwd b.fwd
  bwd := compN b.bwd a.bwd
  fb c x := (M.P.obj c).trans (a.bwd.app_eqv c (b.fb c _)) (a.fb c x)
  bf c y := (M''.P.obj c).trans (b.fwd.app_eqv c (a.bf c _)) (b.bf c y)
  rel_fwd R c l h := M''.rel_resp R c _ _ (by rw [lmap_lmap]; exact leq_refl M''.P _) (b.rel_fwd R c _ (a.rel_fwd R c l h))
  rel_bwd R c l h := M.rel_resp R c _ _ (by rw [lmap_lmap]; exact leq_refl M.P _) (a.rel_bwd R c _ (b.rel_bwd R c l h))

section N

variable (T : Seq → Prop) {D : ECat.{0, 0}} (K : GTop D)

theorem rel_transport {F G : EFun (SynCat T) (ShCat K)} (φ : FIso F G) (R : Nat) (c : D.Ob)
    (l : List (((F.obj U1).1.obj c).car)) (h : relF T K F R c l) : relF T K G R c (lmapG ((φ.to U1).app c) l) :=
  cov_mono K h (fun d g ⟨m, E, ρ, e, hm, h1, s⟩ => by
    refine ⟨m, E, ρ, (φ.to E).app d e, hm.trans (len_lmapG _ l).symm, ?_, s⟩
    have hAB : LEq (G.obj U1).1 d (elsL T K G ρ m ((φ.to E).app d e))
        (lmapG (fun i => (φ.to U1).app d ((F.map (ρ i)).app d e)) (rangeG m)) :=
      leq_of_mem (G.obj U1).1 _ _ _ (fun i _ => ((G.obj U1).1.obj d).symm (φ.nat (ρ i) d e))
    have eBC : lmapG (fun i => (φ.to U1).app d ((F.map (ρ i)).app d e)) (rangeG m) =
        lmapG ((φ.to U1).app d) (elsL T K F ρ m e) := (lmap_lmap _ _ _).symm
    rw [eBC] at hAB
    exact leq_trans _ _ _ _ hAB (leq_trans _ _ _ _ (leq_app (φ.to U1) _ _ h1) (leq_nat (φ.to U1) g l)))

/-- isomorphic functors have isomorphic structures -/
def nf_fiso {F G : EFun (SynCat T) (ShCat K)} (φ : FIso F G) : SIso (NFStr T K F) (NFStr T K G) where
  fwd := φ.to U1
  bwd := φ.fr U1
  fb c x := φ.ft U1 c x
  bf c y := φ.tf U1 c y
  rel_fwd R c l h := rel_transport T K φ R c l h
  rel_bwd R c l h := rel_transport T K φ.symm R c l h

end N

end Gt

end Found

#print axioms Found.Gt.FIso.nat_fr
#print axioms Found.Gt.FIso.symm
#print axioms Found.Gt.SIso.trans
#print axioms Found.Gt.rel_transport
#print axioms Found.Gt.nf_fiso

found/Gt87.lean

lean_all/found/Gt87.lean

/-
  T194 (classifying topos), part 87. found/Gt87 — the round-trip
  isomorphisms as natural isomorphisms of functors: f*(y −) ≅ F for the
  geometric morphism of a flat continuous F, and F ≅ F_{N_F} for the model
  of F; isomorphic functors have isomorphic direct images.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt86

namespace Found

namespace Gt

open Sh

section D1

variable {C : ECat.{0, 0}} (J : GTop C) (hsub : ∀ A, IsSheaf J (yo C A)) {D : ECat.{0, 0}} (K : GTop D)
  (F : EFun C (ShCat K)) (hF : FlatF K F) (Kc : CP.Classical') (hC : ContF J K F)

theorem toT_nat {A B : C.Ob} (u : C.Hom A B) {c : D.Ob} (y : ((F.obj A).1.obj c).car) :
    PEq K (TensP K F (yo C B) hF) (((Ff J hsub K (geomD J K F hF Kc hC)).map u).app c ((toT K F hF A).app c y))
      ((toT K F hF B).app c ((F.map u).app c y)) :=
  peq_trans K _ (invMap_unit J K F hF Kc (P := (yoE J hsub).obj A) (P' := (yoE J hsub).obj B) (yoM u) ⟨A, C.id A, y⟩)
    ((eta K (TensP K F (yo C B) hF)).app_eqv c (locEq_symm K F (yo C B) (to_nat K F A u y)))

/-- f*(y −) ≅ F for the geometric morphism of F -/
def fiso_geomD : FIso (Ff J hsub K (geomD J K F hF Kc hC)) F where
  to A := fromT K F hF Kc A
  fr A := toT K F hF A
  tf A _ x := from_to K F hF Kc A x
  ft A _ a := to_from K F hF Kc A a
  nat {A B} u c a := ((F.obj B).1.obj c).trans
    ((fromT K F hF Kc B).app_eqv c (((Ff J hsub K (geomD J K F hF Kc hC)).map u).app_eqv c
      (peq_symm K _ (to_from K F hF Kc A a))))
    (((F.obj B).1.obj c).trans ((fromT K F hF Kc B).app_eqv c (toT_nat J hsub K F hF Kc hC u _))
      (from_to K F hF Kc B _))

end D1

section D2

variable (T : Seq → Prop) {D : ECat.{0, 0}} (K : GTop D) (F : EFun (SynCat T) (ShCat K)) (Kc : CP.Classical')
  (hF : FlatF K F) (hC : ContF (JTc T Kc) K F) (hT : ∀ σ, T σ → ∃ k, FVg k σ.lhs ∧ FVg k σ.rhs)

/-- F ≅ F_{N_F} -/
def fiso_fnf : FIso F (FN (NFModel T K F Kc hF hC hT) Kc (nf_inh T K F Kc hF hC hT)) where
  to A := tauN T K F Kc hF hC hT A
  fr A := tauInv T K F Kc hF hC hT A
  tf A _ t := tau_inv_r T K F Kc hF hC hT A t
  ft A _ x := tau_inv_l T K F Kc hF hC hT A x
  nat u _ x := ((FP _ _).obj _).symm (tau_nat T K F Kc hF hC hT u x)

end D2

/-- isomorphic functors have isomorphic direct images -/
def dirP_fiso {C D : ECat.{0, 0}} (K : GTop D) {F G : EFun C (ShCat K)} (φ : FIso F G) (Q : (ShCat K).Ob) :
    PNat (DirP K G Q) (DirP K F Q) where
  app A α := compN (φ.to A) α
  app_eqv _ {_ _} h c _ := h c _
  nat {_ B} u α c x := α.app_eqv c (((G.obj B).1.obj c).symm (φ.nat u c x))

theorem dirP_fiso_inv {C D : ECat.{0, 0}} (K : GTop D) {F G : EFun C (ShCat K)} (φ : FIso F G) (Q : (ShCat K).Ob)
    {A : C.Ob} (α : ((DirP K G Q).obj A).car) :
    ((DirP K G Q).obj A).eqv ((dirP_fiso K φ.symm Q).app A ((dirP_fiso K φ Q).app A α)) α :=
  fun c x => α.app_eqv c (φ.tf A c x)

end Gt

end Found

#print axioms Found.Gt.fiso_geomD
#print axioms Found.Gt.fiso_fnf
#print axioms Found.Gt.dirP_fiso
#print axioms Found.Gt.dirP_fiso_inv

found/Gt88.lean

lean_all/found/Gt88.lean

/-
  T194 (classifying topos), part 88. found/Gt88 — the classifying topos of
  a geometric theory T (sequents in finite contexts; models inhabited):
  B[T] = Sh(C_T, J_T). For every site (D, K):
  * a left exact geometric morphism f : Sh(D, K) → B[T] gives an
    inhabited T-model N_f, whose carrier is f*(U), the inverse image of
    the carrier of the universal model;
  * an inhabited T-model N gives a left exact geometric morphism f_N;
  * N_{f_N} ≅ N, and f_{N_f} has the same direct image as f (up to
    natural isomorphism).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt87

namespace Found

namespace Gt

open Sh

variable (T : Seq → Prop) (hT : ∀ σ, T σ → ∃ k, FVg k σ.lhs ∧ FVg k σ.rhs) (Kc : CP.Classical')

/-- the model of a left exact geometric morphism into B[T] -/
def modelOfGeom {D : ECat.{0, 0}} {K : GTop D} (f : Geom (JTc T Kc) K) (hE : EqPres (JTc T Kc) K f) : SModel T :=
  NFModel T K (Ff (JTc T Kc) (yo_sheaf T Kc) K f) Kc (ff_flat (JTc T Kc) (yo_sheaf T Kc) K f hE)
    (ff_cont (JTc T Kc) (yo_sheaf T Kc) K f) hT

/-- the geometric morphism of an inhabited model -/
def geomOfModel (N : SModel T) (hI : Inh N) : Geom (JTc T Kc) N.K :=
  geomD (JTc T Kc) N.K (FN N Kc hI) (fn_flat N Kc hI) Kc (fn_cont N Kc hI)

theorem geomOfModel_lex (N : SModel T) (hI : Inh N) : EqPres (JTc T Kc) N.K (geomOfModel T Kc N hI) :=
  geomD_eq (JTc T Kc) N.K (FN N Kc hI) (fn_flat N Kc hI) Kc (fn_cont N Kc hI)

/-- the carrier of N_f is f*(U) -/
theorem modelOfGeom_carrier {D : ECat.{0, 0}} {K : GTop D} (f : Geom (JTc T Kc) K) (hE : EqPres (JTc T Kc) K f) :
    (modelOfGeom T hT Kc f hE).M.P = (f.inv.obj ⟨(UStr T).P, yo_sheaf T Kc U1⟩).1 := rfl

/-- N_{f_N} ≅ N -/
def model_roundtrip (N : SModel T) (hI : Inh N) :
    SIso (modelOfGeom T hT Kc (geomOfModel T Kc N hI) (geomOfModel_lex T Kc N hI)).M N.M :=
  SIso.trans (nf_fiso T N.K (fiso_geomD (JTc T Kc) (yo_sheaf T Kc) N.K (FN N Kc hI) (fn_flat N Kc hI) Kc
    (fn_cont N Kc hI))) (nfn_iso N Kc hI)

section G

variable {D : ECat.{0, 0}} {K : GTop D} (f : Geom (JTc T Kc) K) (hE : EqPres (JTc T Kc) K f) (Q : (ShCat K).Ob)

/-- f_* Q → (f_{N_f})_* Q -/
def dirForth : PNat (f.dir.obj Q).1
    ((geomOfModel T Kc (modelOfGeom T hT Kc f hE) (nf_inh T K _ Kc _ _ hT)).dir.obj Q).1 :=
  compN (dirTo (JTc T Kc) (yo_sheaf T Kc) K f Q)
    (dirP_fiso K (fiso_fnf T K (Ff (JTc T Kc) (yo_sheaf T Kc) K f) Kc (ff_flat (JTc T Kc) (yo_sheaf T Kc) K f hE)
      (ff_cont (JTc T Kc) (yo_sheaf T Kc) K f) hT).symm Q)

/-- (f_{N_f})_* Q → f_* Q -/
def dirBack : PNat ((geomOfModel T Kc (modelOfGeom T hT Kc f hE) (nf_inh T K _ Kc _ _ hT)).dir.obj Q).1
    (f.dir.obj Q).1 :=
  compN (dirP_fiso K (fiso_fnf T K (Ff (JTc T Kc) (yo_sheaf T Kc) K f) Kc (ff_flat (JTc T Kc) (yo_sheaf T Kc) K f hE)
      (ff_cont (JTc T Kc) (yo_sheaf T Kc) K f) hT) Q)
    (dirFrom (JTc T Kc) (yo_sheaf T Kc) K f Q)

theorem dir_back_forth {A : SObj} (q : ((f.dir.obj Q).1.obj A).car) :
    ((f.dir.obj Q).1.obj A).eqv ((dirBack T hT Kc f hE Q).app A ((dirForth T hT Kc f hE Q).app A q)) q :=
  ((f.dir.obj Q).1.obj A).trans ((dirFrom (JTc T Kc) (yo_sheaf T Kc) K f Q).app_eqv A
    (dirP_fiso_inv K (fiso_fnf T K _ Kc (ff_flat (JTc T Kc) (yo_sheaf T Kc) K f hE)
      (ff_cont (JTc T Kc) (yo_sheaf T Kc) K f) hT).symm Q _))
    (dir_from_to (JTc T Kc) (yo_sheaf T Kc) K f Q q)

theorem dir_forth_back {A : SObj}
    (β : (((geomOfModel T Kc (modelOfGeom T hT Kc f hE) (nf_inh T K _ Kc _ _ hT)).dir.obj Q).1.obj A).car) :
    (((geomOfModel T Kc (modelOfGeom T hT Kc f hE) (nf_inh T K _ Kc _ _ hT)).dir.obj Q).1.obj A).eqv
      ((dirForth T hT Kc f hE Q).app A ((dirBack T hT Kc f hE Q).app A β)) β := fun c x =>
  (Q.1.obj c).trans (dir_to_from (JTc T Kc) (yo_sheaf T Kc) K f Q _ c _)
    (dirP_fiso_inv K (fiso_fnf T K _ Kc (ff_flat (JTc T Kc) (yo_sheaf T Kc) K f hE)
      (ff_cont (JTc T Kc) (yo_sheaf T Kc) K f) hT) Q β c x)

end G

/-- the classifying topos of T -/
theorem classifying_topos :
    (∀ {D : ECat.{0, 0}} {K : GTop D} (f : Geom (JTc T Kc) K) (hE : EqPres (JTc T Kc) K f),
      Inh (modelOfGeom T hT Kc f hE)) ∧
    (∀ (N : SModel T) (hI : Inh N), EqPres (JTc T Kc) N.K (geomOfModel T Kc N hI) ∧
      Nonempty (SIso (modelOfGeom T hT Kc (geomOfModel T Kc N hI) (geomOfModel_lex T Kc N hI)).M N.M)) ∧
    (∀ {D : ECat.{0, 0}} {K : GTop D} (f : Geom (JTc T Kc) K) (hE : EqPres (JTc T Kc) K f) (Q : (ShCat K).Ob)
      (A : SObj), (∀ q, ((f.dir.obj Q).1.obj A).eqv ((dirBack T hT Kc f hE Q).app A ((dirForth T hT Kc f hE Q).app A q)) q) ∧
      ∀ β, (((geomOfModel T Kc (modelOfGeom T hT Kc f hE) (nf_inh T K _ Kc _ _ hT)).dir.obj Q).1.obj A).eqv
        ((dirForth T hT Kc f hE Q).app A ((dirBack T hT Kc f hE Q).app A β)) β) :=
  ⟨fun _ _ => nf_inh T _ _ Kc _ _ hT,
   fun N hI => ⟨geomOfModel_lex T Kc N hI, ⟨model_roundtrip T hT Kc N hI⟩⟩,
   fun f hE Q _ => ⟨fun q => dir_back_forth T hT Kc f hE Q q, fun β => dir_forth_back T hT Kc f hE Q β⟩⟩

end Gt

end Found

#print axioms Found.Gt.model_roundtrip
#print axioms Found.Gt.dir_back_forth
#print axioms Found.Gt.dir_forth_back
#print axioms Found.Gt.classifying_topos

found/Gt9.lean

lean_all/found/Gt9.lean

/-
  T194 (classifying topos), part 9. found/Gt9 — equalizers in Sh(C, J)
  (the subpresheaf where two maps agree, a sheaf because amalgamations are
  unique), and geometric morphisms between sheaf categories (first form): an
  adjunction f* ⊣ f_* whose inverse image f* preserves the terminal object
  and binary products (the comparison maps have inverses); preservation of
  equalizers is added with the comparison later.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Gt8

namespace Found

namespace Gt

open Sh

variable {C : ECat.{0, 0}} (J : GTop C)

/-- the equalizer of two natural maps -/
def eqP {F G : Psh.{0, 0, 0} C} (α β : PNat F G) : Psh.{0, 0, 0} C where
  obj c := ⟨{x : (F.obj c).car // (G.obj c).eqv (α.app c x) (β.app c x)}, fun x y => (F.obj c).eqv x.1 y.1,
    fun x => (F.obj c).refl x.1, fun h => (F.obj c).symm h, fun h k => (F.obj c).trans h k⟩
  map f x := ⟨F.map f x.1, (G.obj _).trans (α.nat f x.1)
    ((G.obj _).trans (G.map_eqv f x.2) ((G.obj _).symm (β.nat f x.1)))⟩
  map_eqv {_ _} f {_ _} h := F.map_eqv f h
  map_resp x e := F.map_resp x.1 e
  map_id _ x := F.map_id _ x.1
  map_comp f g x := F.map_comp f g x.1

def evE {F G : Psh.{0, 0, 0} C} {α β : PNat F G} {c : C.Ob} (x : ((eqP α β).obj c).car) : (F.obj c).car :=
  Subtype.val (p := fun y => (G.obj c).eqv (α.app c y) (β.app c y)) x

theorem evP {F G : Psh.{0, 0, 0} C} {α β : PNat F G} {c : C.Ob} (x : ((eqP α β).obj c).car) :
    (G.obj c).eqv (α.app c (evE x)) (β.app c (evE x)) :=
  Subtype.property (p := fun y => (G.obj c).eqv (α.app c y) (β.app c y)) x

def mkE {F G : Psh.{0, 0, 0} C} {α β : PNat F G} {c : C.Ob} (a : (F.obj c).car)
    (h : (G.obj c).eqv (α.app c a) (β.app c a)) : ((eqP α β).obj c).car := (⟨a, h⟩ : {y // (G.obj c).eqv (α.app c y) (β.app c y)})

theorem eqP_sheaf {F G : Psh.{0, 0, 0} C} (hF : IsSheaf J F) (hG : IsSheaf J G) (α β : PNat F G) :
    IsSheaf J (eqP α β) := fun c S hS m =>
  let m' : MFam F S := ⟨fun f hf => evE (m.x f hf), fun hf hf' e => m.resp hf hf' e, fun f hf g => m.compat f hf g⟩
  match (hF c S hS m').1 with
  | ⟨a, ha⟩ =>
    have agree : (G.obj c).eqv (α.app c a) (β.app c a) := sheaf_sep J G hG c S hS _ _ (fun d f hf =>
      (G.obj d).trans ((G.obj d).symm (α.nat f a)) ((G.obj d).trans (α.app_eqv d (ha d f hf))
        ((G.obj d).trans (evP (m.x f hf)) ((G.obj d).trans ((G.obj d).symm (β.app_eqv d (ha d f hf))) (β.nat f a)))))
    ⟨⟨mkE a agree, fun d f hf => ha d f hf⟩,
     fun x y hx hy => (hF c S hS m').2 (evE x) (evE y) (fun d f hf => hx d f hf) (fun d f hf => hy d f hf)⟩

/-- a geometric morphism Sh(D, K) → Sh(C, J): f* ⊣ f_*, f* preserves the terminal object and binary products -/
structure Geom {D : ECat.{0, 0}} (K : GTop D) where
  inv : EFun (ShCat J) (ShCat K)
  dir : EFun (ShCat K) (ShCat J)
  adj : Adj inv dir
  term_iso : ∃ g : (ShCat K).Hom (shTerminal K).one (inv.obj (shTerminal J).one),
    (ShCat K).heq (ShCat K |>.comp g ((shTerminal K).bang _)) ((ShCat K).id _)
  prod_iso : ∀ F G : (ShCat J).Ob, ∃ g : (ShCat K).Hom (shProduct K (inv.obj F) (inv.obj G)).P (inv.obj (shProduct J F G).P),
    (ShCat K).heq (ShCat K |>.comp g ((shProduct K (inv.obj F) (inv.obj G)).pair (inv.map (shProduct J F G).p1)
      (inv.map (shProduct J F G).p2))) ((ShCat K).id _)

end Gt

end Found

#print axioms Found.Gt.eqP_sheaf

found/HAmr1.lean

lean_all/found/HAmr1.lean

/-
  T180/T187 line HA → T, part 1. found/HAmr1 — Gödel's T in combinatory
  form: finite types over ℕ with products, closed terms built from
  K, S, 0, succ, the recursor R, pairing and projections by application,
  evaluated in the standard model (each constant's equations hold by
  definition).  Open terms with constants and bracket abstraction
  ([x]M) N = M[x := N] (semantically) make λ-notation available while all
  realizers stay closed combinator terms.

  Lean 4 core only, hand-written proofs, no axioms.
-/

namespace Found

namespace MR

inductive Ty where
  | nat : Ty
  | arr : Ty → Ty → Ty
  | prod : Ty → Ty → Ty

@[reducible] def den : Ty → Type
  | .nat => Nat
  | .arr a b => den a → den b
  | .prod a b => den a × den b

def recN {A : Type} (z : A) (s : Nat → A → A) : Nat → A
  | 0 => z
  | n + 1 => s n (recN z s n)

/-- closed combinator terms of Gödel's T -/
inductive CT : Ty → Type where
  | K {a b} : CT (.arr a (.arr b a))
  | S {a b c} : CT (.arr (.arr a (.arr b c)) (.arr (.arr a b) (.arr a c)))
  | zero : CT .nat
  | succ : CT (.arr .nat .nat)
  | R {a} : CT (.arr a (.arr (.arr .nat (.arr a a)) (.arr .nat a)))
  | pair {a b} : CT (.arr a (.arr b (.prod a b)))
  | fst {a b} : CT (.arr (.prod a b) a)
  | snd {a b} : CT (.arr (.prod a b) b)
  | app {a b} : CT (.arr a b) → CT a → CT b

def ev : {a : Ty} → CT a → den a
  | _, .K => fun x _ => x
  | _, .S => fun f g x => f x (g x)
  | _, .zero => (0 : Nat)
  | _, .succ => fun (n : Nat) => n + 1
  | _, .R => fun z s n => recN z s n
  | _, .pair => fun x y => (x, y)
  | _, .fst => fun p => p.1
  | _, .snd => fun p => p.2
  | _, .app f x => ev f (ev x)

/-! ## open terms and bracket abstraction -/

inductive Var : List Ty → Ty → Type where
  | z {Γ a} : Var (a :: Γ) a
  | s {Γ a b} : Var Γ a → Var (b :: Γ) a

/-- open terms: variables, closed constants, application -/
inductive OT (Γ : List Ty) : Ty → Type where
  | var {a} : Var Γ a → OT Γ a
  | cst {a} : CT a → OT Γ a
  | app {a b} : OT Γ (.arr a b) → OT Γ a → OT Γ b

@[reducible] def Env : List Ty → Type
  | [] => Unit
  | a :: Γ => den a × Env Γ

def look : {Γ : List Ty} → {a : Ty} → Var Γ a → Env Γ → den a
  | _ :: _, _, .z, ρ => ρ.1
  | _ :: _, _, .s v, ρ => look v ρ.2

def oev {Γ : List Ty} (ρ : Env Γ) : {a : Ty} → OT Γ a → den a
  | _, .var v => look v ρ
  | _, .cst c => ev c
  | _, .app f x => oev ρ f (oev ρ x)

def I' {a : Ty} : CT (.arr a a) := .app (.app .S .K) (.K (b := .arr a a))

/-- bracket abstraction of the first variable -/
def abs {Γ : List Ty} {a : Ty} : {b : Ty} → OT (a :: Γ) b → OT Γ (.arr a b)
  | _, .var .z => .cst I'
  | _, .var (.s v) => .app (.cst .K) (.var v)
  | _, .cst c => .app (.cst .K) (.cst c)
  | _, .app f x => .app (.app (.cst .S) (abs f)) (abs x)

/-- combinatory completeness -/
theorem abs_spec {Γ : List Ty} {a : Ty} : {b : Ty} → (M : OT (a :: Γ) b) → (ρ : Env Γ) → (x : den a) →
    oev ρ (abs M) x = oev (Γ := a :: Γ) (x, ρ) M
  | _, .var .z, _, _ => rfl
  | _, .var (.s _), _, _ => rfl
  | _, .cst _, _, _ => rfl
  | _, .app f y, ρ, x => by
    show oev ρ (abs f) x (oev ρ (abs y) x) = oev (Γ := a :: Γ) (x, ρ) f (oev (Γ := a :: Γ) (x, ρ) y)
    rw [abs_spec f ρ x, abs_spec y ρ x]

/-- closing an open term of one variable -/
def lam1 {a b : Ty} (M : OT [a] b) : CT (.arr a b) := match abs M with
  | t => close t
where
  close : {c : Ty} → OT [] c → CT c
    | _, .cst c => c
    | _, .app f x => .app (close f) (close x)

theorem close_spec : {c : Ty} → (t : OT [] c) → ev (lam1.close t) = oev (Γ := []) () t
  | _, .cst _ => rfl
  | _, .app f x => by
    show ev (lam1.close f) (ev (lam1.close x)) = oev (Γ := []) () f (oev (Γ := []) () x)
    rw [close_spec f, close_spec x]

theorem lam1_spec {a b : Ty} (M : OT [a] b) (x : den a) : ev (lam1 M) x = oev (Γ := [a]) (x, ()) M := by
  show ev (lam1.close (abs M)) x = _
  rw [close_spec]
  exact abs_spec M () x

end MR

end Found

#print axioms Found.MR.abs_spec
#print axioms Found.MR.close_spec
#print axioms Found.MR.lam1_spec

found/HAmr2.lean

lean_all/found/HAmr2.lean

/-
  T180/T187 line HA → T, part 2. found/HAmr2 — Heyting arithmetic with
  formulas indexed by the type of their realizers (so substitution keeps
  the realizer type by definition), named variables, Kreisel's modified
  realizability in the standard model, the substitution lemma (for terms
  free for the variable), and natural deduction for HA (with the
  equations of 0, S, +, · and the induction rule).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.HAmr1

namespace Found

namespace MR

inductive AT where
  | var : Nat → AT
  | zero : AT
  | succ : AT → AT
  | add : AT → AT → AT
  | mul : AT → AT → AT

def aev (ρ : Nat → Nat) : AT → Nat
  | .var x => ρ x
  | .zero => 0
  | .succ t => aev ρ t + 1
  | .add s t => aev ρ s + aev ρ t
  | .mul s t => aev ρ s * aev ρ t

/-- formulas, indexed by the type of their realizers -/
inductive HF : Ty → Type where
  | eq : AT → AT → HF .nat
  | bot : HF .nat
  | conj {a b} : HF a → HF b → HF (.prod a b)
  | disj {a b} : HF a → HF b → HF (.prod .nat (.prod a b))
  | imp {a b} : HF a → HF b → HF (.arr a b)
  | all {a} : Nat → HF a → HF (.arr .nat a)
  | ex {a} : Nat → HF a → HF (.prod .nat a)

def beq' : Nat → Nat → Bool
  | 0, 0 => true
  | 0, _ + 1 => false
  | _ + 1, 0 => false
  | a + 1, b + 1 => beq' a b

theorem beq'_true : (a b : Nat) → beq' a b = true → a = b
  | 0, 0, _ => rfl
  | 0, _ + 1, h => Bool.noConfusion h
  | _ + 1, 0, h => Bool.noConfusion h
  | a + 1, b + 1, h => congrArg (· + 1) (beq'_true a b h)

theorem beq'_false : (a b : Nat) → beq' a b = false → a ≠ b
  | 0, 0, h, _ => Bool.noConfusion h
  | 0, _ + 1, _, e => Nat.noConfusion e
  | _ + 1, 0, _, e => Nat.noConfusion e
  | a + 1, b + 1, h, e => beq'_false a b h (Nat.succ.inj e)

theorem beq'_self : (a : Nat) → beq' a a = true
  | 0 => rfl
  | a + 1 => beq'_self a

theorem beq'_of_ne (a b : Nat) (h : a ≠ b) : beq' a b = false := by
  cases e : beq' a b with
  | true => exact (h (beq'_true a b e)).elim
  | false => rfl

def upd (ρ : Nat → Nat) (x n : Nat) : Nat → Nat := fun y => cond (beq' y x) n (ρ y)

/-- modified realizability -/
def mr : {a : Ty} → HF a → (Nat → Nat) → den a → Prop
  | _, .eq s t, ρ, _ => aev ρ s = aev ρ t
  | _, .bot, _, _ => False
  | _, .conj A B, ρ, r => mr A ρ r.1 ∧ mr B ρ r.2
  | _, .disj A B, ρ, r => ((show Nat from r.1) = 0 → mr A ρ r.2.1) ∧ ((show Nat from r.1) ≠ 0 → mr B ρ r.2.2)
  | _, .imp A B, ρ, r => ∀ y, mr A ρ y → mr B ρ (r y)
  | _, .all x A, ρ, r => ∀ n, mr A (upd ρ x n) (r n)
  | _, .ex x A, ρ, r => mr A (upd ρ x (show Nat from r.1)) r.2

theorem aev_ext {ρ ρ' : Nat → Nat} (h : ∀ v, ρ v = ρ' v) : (t : AT) → aev ρ t = aev ρ' t
  | .var x => h x
  | .zero => rfl
  | .succ t => congrArg (· + 1) (aev_ext h t)
  | .add s t => by show aev ρ s + aev ρ t = aev ρ' s + aev ρ' t; rw [aev_ext h s, aev_ext h t]
  | .mul s t => by show aev ρ s * aev ρ t = aev ρ' s * aev ρ' t; rw [aev_ext h s, aev_ext h t]

theorem upd_ext {ρ ρ' : Nat → Nat} (h : ∀ v, ρ v = ρ' v) (x n : Nat) : ∀ v, upd ρ x n v = upd ρ' x n v := fun v => by
  show cond (beq' v x) n (ρ v) = cond (beq' v x) n (ρ' v)
  rw [h v]

/-- realizability depends on the environment pointwise -/
theorem mr_ext : {a : Ty} → (A : HF a) → {ρ ρ' : Nat → Nat} → (∀ v, ρ v = ρ' v) → (r : den a) → (mr A ρ r ↔ mr A ρ' r)
  | _, .eq s t, _, _, h, _ => by show aev _ s = aev _ t ↔ aev _ s = aev _ t; rw [aev_ext h s, aev_ext h t]
  | _, .bot, _, _, _, _ => Iff.rfl
  | _, .conj A B, _, _, h, r => ⟨fun ⟨x, y⟩ => ⟨(mr_ext A h _).1 x, (mr_ext B h _).1 y⟩,
      fun ⟨x, y⟩ => ⟨(mr_ext A h _).2 x, (mr_ext B h _).2 y⟩⟩
  | _, .disj A B, _, _, h, r => ⟨fun ⟨x, y⟩ => ⟨fun e => (mr_ext A h _).1 (x e), fun e => (mr_ext B h _).1 (y e)⟩,
      fun ⟨x, y⟩ => ⟨fun e => (mr_ext A h _).2 (x e), fun e => (mr_ext B h _).2 (y e)⟩⟩
  | _, .imp A B, ρ, ρ', h, r => by
    show (∀ y, mr A ρ y → mr B ρ (r y)) ↔ (∀ y, mr A ρ' y → mr B ρ' (r y))
    exact ⟨fun f y hy => (mr_ext B h _).1 (f y ((mr_ext A h _).2 hy)),
      fun f y hy => (mr_ext B h _).2 (f y ((mr_ext A h _).1 hy))⟩
  | _, .all x A, ρ, ρ', h, r => by
    show (∀ n, mr A (upd ρ x n) (r n)) ↔ (∀ n, mr A (upd ρ' x n) (r n))
    exact ⟨fun f n => (mr_ext A (upd_ext h x n) _).1 (f n),
      fun f n => (mr_ext A (upd_ext h x n) _).2 (f n)⟩
  | _, .ex x A, _, _, h, r => ⟨fun f => (mr_ext A (upd_ext h x _) _).1 f, fun f => (mr_ext A (upd_ext h x _) _).2 f⟩

/-! ## free variables and substitution -/

def occT (x : Nat) : AT → Prop
  | .var y => y = x
  | .zero => False
  | .succ t => occT x t
  | .add s t => occT x s ∨ occT x t
  | .mul s t => occT x s ∨ occT x t

/-- x occurs free in A -/
def free (x : Nat) : {a : Ty} → HF a → Prop
  | _, .eq s t => occT x s ∨ occT x t
  | _, .bot => False
  | _, .conj A B => free x A ∨ free x B
  | _, .disj A B => free x A ∨ free x B
  | _, .imp A B => free x A ∨ free x B
  | _, .all y A => y ≠ x ∧ free x A
  | _, .ex y A => y ≠ x ∧ free x A

def substT (x : Nat) (t : AT) : AT → AT
  | .var y => cond (beq' y x) t (.var y)
  | .zero => .zero
  | .succ s => .succ (substT x t s)
  | .add s u => .add (substT x t s) (substT x t u)
  | .mul s u => .mul (substT x t s) (substT x t u)

def subst (x : Nat) (t : AT) : {a : Ty} → HF a → HF a
  | _, .eq s u => .eq (substT x t s) (substT x t u)
  | _, .bot => .bot
  | _, .conj A B => .conj (subst x t A) (subst x t B)
  | _, .disj A B => .disj (subst x t A) (subst x t B)
  | _, .imp A B => .imp (subst x t A) (subst x t B)
  | _, .all y A => cond (beq' y x) (.all y A) (.all y (subst x t A))
  | _, .ex y A => cond (beq' y x) (.ex y A) (.ex y (subst x t A))

/-- t is free for x in A: no free occurrence of x lies under a binder of a variable of t -/
def FreeFor (x : Nat) (t : AT) : {a : Ty} → HF a → Prop
  | _, .eq _ _ => True
  | _, .bot => True
  | _, .conj A B => FreeFor x t A ∧ FreeFor x t B
  | _, .disj A B => FreeFor x t A ∧ FreeFor x t B
  | _, .imp A B => FreeFor x t A ∧ FreeFor x t B
  | _, .all y A => y = x ∨ (¬ occT y t ∧ FreeFor x t A)
  | _, .ex y A => y = x ∨ (¬ occT y t ∧ FreeFor x t A)

theorem aev_subst (ρ : Nat → Nat) (x : Nat) (t : AT) : (s : AT) → aev ρ (substT x t s) = aev (upd ρ x (aev ρ t)) s
  | .var y => by
    show aev ρ (cond (beq' y x) t (.var y)) = cond (beq' y x) (aev ρ t) (ρ y)
    cases beq' y x <;> rfl
  | .zero => rfl
  | .succ s => congrArg (· + 1) (aev_subst ρ x t s)
  | .add s u => by show aev ρ (substT x t s) + aev ρ (substT x t u) = _; rw [aev_subst ρ x t s, aev_subst ρ x t u]; rfl
  | .mul s u => by show aev ρ (substT x t s) * aev ρ (substT x t u) = _; rw [aev_subst ρ x t s, aev_subst ρ x t u]; rfl

theorem aev_upd_notocc (ρ : Nat → Nat) (y n : Nat) : (t : AT) → ¬ occT y t → aev (upd ρ y n) t = aev ρ t
  | .var z, h => by
    show cond (beq' z y) n (ρ z) = ρ z
    rw [beq'_of_ne z y h]
    rfl
  | .zero, _ => rfl
  | .succ t, h => congrArg (· + 1) (aev_upd_notocc ρ y n t h)
  | .add s u, h => by
    show aev _ s + aev _ u = aev ρ s + aev ρ u
    rw [aev_upd_notocc ρ y n s (fun o => h (Or.inl o)), aev_upd_notocc ρ y n u (fun o => h (Or.inr o))]
  | .mul s u, h => by
    show aev _ s * aev _ u = aev ρ s * aev ρ u
    rw [aev_upd_notocc ρ y n s (fun o => h (Or.inl o)), aev_upd_notocc ρ y n u (fun o => h (Or.inr o))]

theorem upd_same (ρ : Nat → Nat) (x a b : Nat) : ∀ v, upd (upd ρ x a) x b v = upd ρ x b v := fun v => by
  show cond (beq' v x) b (cond (beq' v x) a (ρ v)) = cond (beq' v x) b (ρ v)
  cases beq' v x <;> rfl

theorem upd_comm (ρ : Nat → Nat) (x y a b : Nat) (ne : y ≠ x) : ∀ v, upd (upd ρ x a) y b v = upd (upd ρ y b) x a v :=
  fun v => by
    show cond (beq' v y) b (cond (beq' v x) a (ρ v)) = cond (beq' v x) a (cond (beq' v y) b (ρ v))
    cases hy : beq' v y with
    | true =>
      have hx : beq' v x = false := beq'_of_ne v x (fun e => ne ((beq'_true v y hy).symm.trans e))
      rw [hx]
      rfl
    | false => cases beq' v x <;> rfl

/-- the substitution lemma -/
theorem mr_subst (x : Nat) (t : AT) : {a : Ty} → (A : HF a) → FreeFor x t A → (ρ : Nat → Nat) → (r : den a) →
    (mr (subst x t A) ρ r ↔ mr A (upd ρ x (aev ρ t)) r)
  | _, .eq s u, _, ρ, _ => by
    show aev ρ (substT x t s) = aev ρ (substT x t u) ↔ _
    rw [aev_subst, aev_subst]; exact Iff.rfl
  | _, .bot, _, _, _ => Iff.rfl
  | _, .conj A B, ff, ρ, r =>
    have fa : FreeFor x t A := (show FreeFor x t A ∧ FreeFor x t B from ff).1
    have fb : FreeFor x t B := (show FreeFor x t A ∧ FreeFor x t B from ff).2
    ⟨fun ⟨p, q⟩ => ⟨(mr_subst x t A fa ρ _).1 p, (mr_subst x t B fb ρ _).1 q⟩,
      fun ⟨p, q⟩ => ⟨(mr_subst x t A fa ρ _).2 p, (mr_subst x t B fb ρ _).2 q⟩⟩
  | _, .disj A B, ff, ρ, r =>
    have fa : FreeFor x t A := (show FreeFor x t A ∧ FreeFor x t B from ff).1
    have fb : FreeFor x t B := (show FreeFor x t A ∧ FreeFor x t B from ff).2
    ⟨fun ⟨p, q⟩ => ⟨fun e => (mr_subst x t A fa ρ _).1 (p e), fun e => (mr_subst x t B fb ρ _).1 (q e)⟩,
      fun ⟨p, q⟩ => ⟨fun e => (mr_subst x t A fa ρ _).2 (p e), fun e => (mr_subst x t B fb ρ _).2 (q e)⟩⟩
  | _, .imp A B, ff, ρ, r =>
    have fa : FreeFor x t A := (show FreeFor x t A ∧ FreeFor x t B from ff).1
    have fb : FreeFor x t B := (show FreeFor x t A ∧ FreeFor x t B from ff).2
    by
    show (∀ y, mr (subst x t A) ρ y → mr (subst x t B) ρ (r y)) ↔
      (∀ y, mr A (upd ρ x (aev ρ t)) y → mr B (upd ρ x (aev ρ t)) (r y))
    exact ⟨fun f y hy => (mr_subst x t B fb ρ _).1 (f y ((mr_subst x t A fa ρ _).2 hy)),
      fun f y hy => (mr_subst x t B fb ρ _).2 (f y ((mr_subst x t A fa ρ _).1 hy))⟩
  | _, .all y A, ff, ρ, r => by
    cases hb : beq' y x with
    | true =>
      have h := beq'_true y x hb
      subst h
      show mr (cond (beq' y y) (HF.all y A) (HF.all y (subst y t A))) ρ r ↔ _
      rw [beq'_self]
      show (∀ n, mr A (upd ρ y n) (r n)) ↔ (∀ n, mr A (upd (upd ρ y (aev ρ t)) y n) (r n))
      exact ⟨fun f n => (mr_ext A (fun v => (upd_same ρ y _ n v).symm) _).1 (f n),
             fun f n => (mr_ext A (upd_same ρ y _ n) _).1 (f n)⟩
    | false =>
      have h := beq'_false y x hb
      show mr (cond (beq' y x) (HF.all y A) (HF.all y (subst x t A))) ρ r ↔ _
      rw [hb]
      have ff' : ¬ occT y t ∧ FreeFor x t A := ff.elim (fun e => (h e).elim) id
      show (∀ n, mr (subst x t A) (upd ρ y n) (r n)) ↔ (∀ n, mr A (upd (upd ρ x (aev ρ t)) y n) (r n))
      exact ⟨fun f n => by
          have := (mr_subst x t A ff'.2 (upd ρ y n) _).1 (f n)
          rw [aev_upd_notocc ρ y n t ff'.1] at this
          exact (mr_ext A (upd_comm ρ x y _ n h) _).2 this,
        fun f n => by
          have := (mr_ext A (upd_comm ρ x y _ n h) _).1 (f n)
          rw [← aev_upd_notocc ρ y n t ff'.1] at this
          exact (mr_subst x t A ff'.2 (upd ρ y n) _).2 this⟩
  | _, .ex y A, ff, ρ, r => by
    cases hb : beq' y x with
    | true =>
      have h := beq'_true y x hb
      subst h
      show mr (cond (beq' y y) (HF.ex y A) (HF.ex y (subst y t A))) ρ r ↔ _
      rw [beq'_self]
      exact (mr_ext A (fun v => (upd_same ρ y _ r.1 v).symm) _)
    | false =>
      have h := beq'_false y x hb
      show mr (cond (beq' y x) (HF.ex y A) (HF.ex y (subst x t A))) ρ r ↔ _
      rw [hb]
      have ff' : ¬ occT y t ∧ FreeFor x t A := ff.elim (fun e => (h e).elim) id
      exact ⟨fun f => by
          have := (mr_subst x t A ff'.2 (upd ρ y r.1) _).1 f
          rw [aev_upd_notocc ρ y r.1 t ff'.1] at this
          exact (mr_ext A (upd_comm ρ x y _ r.1 h) _).2 this,
        fun f => by
          have := (mr_ext A (upd_comm ρ x y _ r.1 h) _).1 f
          rw [← aev_upd_notocc ρ y r.1 t ff'.1] at this
          exact (mr_subst x t A ff'.2 (upd ρ y r.1) _).2 this⟩

end MR

end Found

#print axioms Found.MR.mr_ext
#print axioms Found.MR.mr_subst

found/HAmr3.lean

lean_all/found/HAmr3.lean

/-
  T180/T187 line HA → T, part 3. found/HAmr3 — auxiliary T-terms
  (default elements, conditional, numerals, +, ·, equality test with a
  numeral, environment update, term evaluation), all closed combinator
  terms of Gödel's T with their standard values.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.HAmr2

namespace Found

namespace MR

/-- a default closed term of every type -/
def dflt : (a : Ty) → CT a
  | .nat => .zero
  | .arr _ b => .app .K (dflt b)
  | .prod a b => .app (.app .pair (dflt a)) (dflt b)

/-- application in open terms -/
abbrev ap {Γ : List Ty} {a b : Ty} (f : OT Γ (.arr a b)) (x : OT Γ a) : OT Γ b := .app f x
abbrev c {Γ : List Ty} {a : Ty} (t : CT a) : OT Γ a := .cst t
abbrev v0 {Γ : List Ty} {a : Ty} : OT (a :: Γ) a := .var .z
abbrev v1 {Γ : List Ty} {a b : Ty} : OT (b :: a :: Γ) a := .var (.s .z)
abbrev v2 {Γ : List Ty} {a b d : Ty} : OT (d :: b :: a :: Γ) a := .var (.s (.s .z))

/-- close a term in two variables -/
def mk2 {a b d : Ty} (M : OT [a, b] d) : CT (.arr b (.arr a d)) := lam1.close (abs (abs M))

theorem mk2_spec {a b d : Ty} (M : OT [a, b] d) (y : den b) (x : den a) :
    ev (mk2 M) y x = oev (Γ := [a, b]) (x, (y, ())) M := by
  show ev (lam1.close (abs (abs M))) y x = _
  rw [close_spec, abs_spec (abs M) () y, abs_spec M (y, ()) x]

/-- close a term in three variables -/
def mk3 {p q r d : Ty} (M : OT [r, q, p] d) : CT (.arr p (.arr q (.arr r d))) := lam1.close (abs (abs (abs M)))

theorem mk3_spec {p q r d : Ty} (M : OT [r, q, p] d) (x : den p) (y : den q) (z : den r) :
    ev (mk3 M) x y z = oev (Γ := [r, q, p]) (z, (y, (x, ()))) M := by
  show ev (lam1.close (abs (abs (abs M)))) x y z = _
  rw [close_spec, abs_spec (abs (abs M)) () x, abs_spec (abs M) (x, ()) y, abs_spec M (y, (x, ())) z]

/-- conditional on zero: cond n x y = x if n = 0, y otherwise -/
def condT (a : Ty) : CT (.arr .nat (.arr a (.arr a a))) :=
  mk3 (ap (ap (ap (c .R) v1) (ap (c .K) (ap (c .K) v0))) v2)

theorem cond_zero (a : Ty) (x y : den a) : ev (condT a) 0 x y = x := by
  unfold condT
  rw [mk3_spec]
  rfl

theorem cond_succ (a : Ty) (n : Nat) (x y : den a) : ev (condT a) (n + 1) x y = y := by
  unfold condT
  rw [mk3_spec]
  rfl

/-- numerals -/
def num : Nat → CT .nat
  | 0 => .zero
  | n + 1 => .app .succ (num n)

theorem num_spec : (n : Nat) → ev (num n) = n
  | 0 => rfl
  | n + 1 => congrArg (· + 1) (num_spec n)

/-- addition: recursion on the second argument -/
def addT : CT (.arr .nat (.arr .nat .nat)) := mk2 (ap (ap (ap (c .R) v1) (ap (c .K) (c .succ))) v0)

theorem recN_add (m : Nat) : (n : Nat) → recN m (fun _ r => r + 1) n = m + n
  | 0 => rfl
  | n + 1 => congrArg (· + 1) (recN_add m n)

theorem addT_spec (m n : Nat) : ev addT m n = m + n := by
  unfold addT
  rw [mk2_spec]
  exact recN_add m n

/-- the step of multiplication, λ k r. r + m, in the context [n, m] -/
def mulStep : OT [.nat, .nat] (.arr .nat (.arr .nat .nat)) :=
  abs (abs (ap (ap (c addT) v0) (.var (.s (.s (.s .z))))))

def mulT : CT (.arr .nat (.arr .nat .nat)) := mk2 (ap (ap (ap (c .R) (c .zero)) mulStep) v0)

theorem mulStep_spec (n0 m k r : Nat) : oev (Γ := [.nat, .nat]) (n0, (m, ())) mulStep k r = r + m := by
  unfold mulStep
  rw [abs_spec, abs_spec]
  exact addT_spec r m

theorem mul_rec (n0 m : Nat) : (k : Nat) → recN 0 (oev (Γ := [.nat, .nat]) (n0, (m, ())) mulStep) k = m * k
  | 0 => rfl
  | k + 1 => by
    show oev (Γ := [.nat, .nat]) (n0, (m, ())) mulStep k (recN 0 (oev (Γ := [.nat, .nat]) (n0, (m, ())) mulStep) k) = m * (k + 1)
    rw [mulStep_spec, mul_rec n0 m k]
    rfl

theorem mulT_spec (m n : Nat) : ev mulT m n = m * n := by
  unfold mulT
  rw [mk2_spec]
  exact mul_rec n m n

def isZeroT : CT (.arr .nat .nat) := lam1 (ap (ap (ap (c (condT .nat)) v0) (c .zero)) (c (num 1)))
def predT : CT (.arr .nat .nat) := lam1 (ap (ap (ap (c .R) (c .zero)) (c .K)) v0)

theorem isZero_zero : ev isZeroT 0 = 0 := by unfold isZeroT; rw [lam1_spec]; exact cond_zero .nat 0 1
theorem isZero_succ (k : Nat) : ev isZeroT (k + 1) = 1 := by unfold isZeroT; rw [lam1_spec]; exact cond_succ .nat k 0 1
theorem pred_succ (k : Nat) : ev predT (k + 1) = k := by unfold predT; rw [lam1_spec]; rfl

/-- test for equality with a fixed numeral: 0 when equal, 1 otherwise -/
def eqConst : Nat → CT (.arr .nat .nat)
  | 0 => isZeroT
  | x + 1 => lam1 (ap (ap (ap (c (condT .nat)) (ap (c isZeroT) v0)) (c (num 1))) (ap (c (eqConst x)) (ap (c predT) v0)))

theorem eqConst_spec : (x k : Nat) → ev (eqConst x) k = cond (beq' k x) 0 1
  | 0, 0 => isZero_zero
  | 0, k + 1 => isZero_succ k
  | x + 1, 0 => by
    show ev (lam1 _) 0 = 1
    rw [lam1_spec]
    show ev (condT .nat) (ev isZeroT 0) (ev (num 1)) (ev (eqConst x) (ev predT 0)) = 1
    rw [isZero_zero, cond_zero, num_spec]
  | x + 1, k + 1 => by
    show ev (lam1 _) (k + 1) = cond (beq' k x) 0 1
    rw [lam1_spec]
    show ev (condT .nat) (ev isZeroT (k + 1)) (ev (num 1)) (ev (eqConst x) (ev predT (k + 1))) = _
    rw [isZero_succ, cond_succ, pred_succ, eqConst_spec x k]

/-- environment update as a T-term -/
def updT (x : Nat) : CT (.arr (.arr .nat .nat) (.arr .nat (.arr .nat .nat))) :=
  mk3 (ap (ap (ap (c (condT .nat)) (ap (c (eqConst x)) v0)) v1) (ap v2 v0))

theorem updT_spec (x : Nat) (e : Nat → Nat) (n k : Nat) : ev (updT x) e n k = upd e x n k := by
  unfold updT
  rw [mk3_spec]
  show ev (condT .nat) (ev (eqConst x) k) n (e k) = cond (beq' k x) n (e k)
  rw [eqConst_spec]
  cases beq' k x with
  | true => exact cond_zero .nat n (e k)
  | false => exact cond_succ .nat 0 n (e k)

/-- evaluation of arithmetic terms as T-terms -/
def tT : AT → CT (.arr (.arr .nat .nat) .nat)
  | .var y => lam1 (ap v0 (c (num y)))
  | .zero => .app .K .zero
  | .succ t => lam1 (ap (c .succ) (ap (c (tT t)) v0))
  | .add s u => lam1 (ap (ap (c addT) (ap (c (tT s)) v0)) (ap (c (tT u)) v0))
  | .mul s u => lam1 (ap (ap (c mulT) (ap (c (tT s)) v0)) (ap (c (tT u)) v0))

theorem tT_spec (ρ : Nat → Nat) : (t : AT) → ev (tT t) ρ = aev ρ t
  | .var y => by unfold tT; rw [lam1_spec]; show ρ (ev (num y)) = ρ y; rw [num_spec]
  | .zero => rfl
  | .succ t => by unfold tT; rw [lam1_spec]; show ev (tT t) ρ + 1 = aev ρ t + 1; rw [tT_spec ρ t]
  | .add s u => by
    unfold tT; rw [lam1_spec]
    show ev addT (ev (tT s) ρ) (ev (tT u) ρ) = aev ρ s + aev ρ u
    rw [addT_spec, tT_spec ρ s, tT_spec ρ u]
  | .mul s u => by
    unfold tT; rw [lam1_spec]
    show ev mulT (ev (tT s) ρ) (ev (tT u) ρ) = aev ρ s * aev ρ u
    rw [mulT_spec, tT_spec ρ s, tT_spec ρ u]

end MR

end Found

#print axioms Found.MR.mk2_spec
#print axioms Found.MR.cond_zero
#print axioms Found.MR.cond_succ
#print axioms Found.MR.num_spec
#print axioms Found.MR.addT_spec
#print axioms Found.MR.mulT_spec
#print axioms Found.MR.eqConst_spec
#print axioms Found.MR.updT_spec
#print axioms Found.MR.tT_spec

found/HAmr4.lean

lean_all/found/HAmr4.lean

/-
  T180/T187 line HA → T, part 4. found/HAmr4 — natural deduction for
  Heyting arithmetic (named variables with eigenvariable and free-for side
  conditions, equality with Leibniz substitution, the equations of 0, S,
  +, · and the induction rule) and the soundness of modified
  realizability: every HA-derivable sequent Γ ⊢ A has a realizer that is a
  closed term of Gödel's T (taking the environment and realizers of Γ) —
  Kreisel's interpretation of HA in T.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.HAmr3

namespace Found

namespace MR

structure Hy where
  ty : Ty
  f : HF ty

def H : List Hy → Ty
  | [] => .nat
  | h :: Γ => .prod h.ty (H Γ)

def mrH : (Γ : List Hy) → (Nat → Nat) → den (H Γ) → Prop
  | [], _, _ => True
  | h :: Γ, ρ, r => mr h.f ρ r.1 ∧ mrH Γ ρ r.2

def NFC (x : Nat) (Γ : List Hy) : Prop := ∀ h, h ∈ Γ → ¬ free x h.f

theorem mr_notfree (x n : Nat) : {a : Ty} → (A : HF a) → ¬ free x A → (ρ : Nat → Nat) → (r : den a) →
    (mr A (upd ρ x n) r ↔ mr A ρ r)
  | _, .eq s t, h, ρ, _ => by
    show aev _ s = aev _ t ↔ aev ρ s = aev ρ t
    rw [aev_upd_notocc ρ x n s (fun o => h (Or.inl o)), aev_upd_notocc ρ x n t (fun o => h (Or.inr o))]
  | _, .bot, _, _, _ => Iff.rfl
  | _, .conj A B, h, ρ, _ => ⟨fun ⟨p, q⟩ => ⟨(mr_notfree x n A (fun o => h (Or.inl o)) ρ _).1 p, (mr_notfree x n B (fun o => h (Or.inr o)) ρ _).1 q⟩,
      fun ⟨p, q⟩ => ⟨(mr_notfree x n A (fun o => h (Or.inl o)) ρ _).2 p, (mr_notfree x n B (fun o => h (Or.inr o)) ρ _).2 q⟩⟩
  | _, .disj A B, h, ρ, _ => ⟨fun ⟨p, q⟩ => ⟨fun e => (mr_notfree x n A (fun o => h (Or.inl o)) ρ _).1 (p e), fun e => (mr_notfree x n B (fun o => h (Or.inr o)) ρ _).1 (q e)⟩,
      fun ⟨p, q⟩ => ⟨fun e => (mr_notfree x n A (fun o => h (Or.inl o)) ρ _).2 (p e), fun e => (mr_notfree x n B (fun o => h (Or.inr o)) ρ _).2 (q e)⟩⟩
  | _, .imp A B, h, ρ, r => by
    show (∀ y, mr A _ y → mr B _ (r y)) ↔ (∀ y, mr A ρ y → mr B ρ (r y))
    exact ⟨fun f y hy => (mr_notfree x n B (fun o => h (Or.inr o)) ρ _).1 (f y ((mr_notfree x n A (fun o => h (Or.inl o)) ρ _).2 hy)),
      fun f y hy => (mr_notfree x n B (fun o => h (Or.inr o)) ρ _).2 (f y ((mr_notfree x n A (fun o => h (Or.inl o)) ρ _).1 hy))⟩
  | _, .all y A, h, ρ, r => by
    show (∀ m, mr A (upd (upd ρ x n) y m) (r m)) ↔ (∀ m, mr A (upd ρ y m) (r m))
    cases hb : beq' y x with
    | true =>
      have e := beq'_true y x hb
      subst e
      exact ⟨fun f m => (mr_ext A (upd_same ρ y n m) _).1 (f m), fun f m => (mr_ext A (upd_same ρ y n m) _).2 (f m)⟩
    | false =>
      have ne := beq'_false y x hb
      have nf : ¬ free x A := fun fa => h ⟨ne, fa⟩
      exact ⟨fun f m => (mr_notfree x n A nf (upd ρ y m) _).1 ((mr_ext A (upd_comm ρ x y n m ne) _).1 (f m)),
        fun f m => (mr_ext A (upd_comm ρ x y n m ne) _).2 ((mr_notfree x n A nf (upd ρ y m) _).2 (f m))⟩
  | _, .ex y A, h, ρ, r => by
    show mr A (upd (upd ρ x n) y r.1) r.2 ↔ mr A (upd ρ y r.1) r.2
    cases hb : beq' y x with
    | true =>
      have e := beq'_true y x hb
      subst e
      exact mr_ext A (upd_same ρ y n r.1) _
    | false =>
      have ne := beq'_false y x hb
      have nf : ¬ free x A := fun fa => h ⟨ne, fa⟩
      exact ⟨fun f => (mr_notfree x n A nf (upd ρ y r.1) _).1 ((mr_ext A (upd_comm ρ x y n r.1 ne) _).1 f),
        fun f => (mr_ext A (upd_comm ρ x y n r.1 ne) _).2 ((mr_notfree x n A nf (upd ρ y r.1) _).2 f)⟩

theorem mrH_notfree (x n : Nat) : (Γ : List Hy) → NFC x Γ → (ρ : Nat → Nat) → (r : den (H Γ)) →
    mrH Γ ρ r → mrH Γ (upd ρ x n) r
  | [], _, _, _, _ => trivial
  | h :: Γ, nf, ρ, _, ⟨p, q⟩ => ⟨(mr_notfree x n h.f (nf h (.head _)) ρ _).2 p,
      mrH_notfree x n Γ (fun k m => nf k (.tail _ m)) ρ _ q⟩

theorem mrH_ext : (Γ : List Hy) → {ρ ρ' : Nat → Nat} → (∀ v, ρ v = ρ' v) → (r : den (H Γ)) → mrH Γ ρ r → mrH Γ ρ' r
  | [], _, _, _, _, _ => trivial
  | h :: Γ, _, _, e, _, ⟨p, q⟩ => ⟨(mr_ext h.f e _).1 p, mrH_ext Γ e _ q⟩

/-! ## natural deduction for HA -/

inductive Pf : List Hy → {a : Ty} → HF a → Prop where
  | hyp {Γ a} {A : HF a} : (⟨a, A⟩ : Hy) ∈ Γ → Pf Γ A
  | botE {Γ a} {A : HF a} : Pf Γ .bot → Pf Γ A
  | conjI {Γ a b} {A : HF a} {B : HF b} : Pf Γ A → Pf Γ B → Pf Γ (.conj A B)
  | conjL {Γ a b} {A : HF a} {B : HF b} : Pf Γ (.conj A B) → Pf Γ A
  | conjR {Γ a b} {A : HF a} {B : HF b} : Pf Γ (.conj A B) → Pf Γ B
  | disjL {Γ a b} {A : HF a} {B : HF b} : Pf Γ A → Pf Γ (.disj A B)
  | disjR {Γ a b} {A : HF a} {B : HF b} : Pf Γ B → Pf Γ (.disj A B)
  | disjE {Γ a b d} {A : HF a} {B : HF b} {C : HF d} :
      Pf Γ (.disj A B) → Pf (⟨a, A⟩ :: Γ) C → Pf (⟨b, B⟩ :: Γ) C → Pf Γ C
  | impI {Γ a b} {A : HF a} {B : HF b} : Pf (⟨a, A⟩ :: Γ) B → Pf Γ (.imp A B)
  | impE {Γ a b} {A : HF a} {B : HF b} : Pf Γ (.imp A B) → Pf Γ A → Pf Γ B
  | allI {Γ a} {A : HF a} (x : Nat) : NFC x Γ → Pf Γ A → Pf Γ (.all x A)
  | allE {Γ a} {A : HF a} (x : Nat) (t : AT) : FreeFor x t A → Pf Γ (.all x A) → Pf Γ (subst x t A)
  | exI {Γ a} {A : HF a} (x : Nat) (t : AT) : FreeFor x t A → Pf Γ (subst x t A) → Pf Γ (.ex x A)
  | exE {Γ a d} {A : HF a} {C : HF d} (x : Nat) : NFC x Γ → ¬ free x C →
      Pf Γ (.ex x A) → Pf (⟨a, A⟩ :: Γ) C → Pf Γ C
  | refl {Γ} (t : AT) : Pf Γ (.eq t t)
  | leib {Γ a} {A : HF a} (x : Nat) (s t : AT) : FreeFor x s A → FreeFor x t A →
      Pf Γ (.eq s t) → Pf Γ (subst x s A) → Pf Γ (subst x t A)
  | sne {Γ} (s : AT) : Pf Γ (.imp (.eq (.succ s) .zero) .bot)
  | sinj {Γ} (s t : AT) : Pf Γ (.imp (.eq (.succ s) (.succ t)) (.eq s t))
  | add0 {Γ} (s : AT) : Pf Γ (.eq (.add s .zero) s)
  | addS {Γ} (s t : AT) : Pf Γ (.eq (.add s (.succ t)) (.succ (.add s t)))
  | mul0 {Γ} (s : AT) : Pf Γ (.eq (.mul s .zero) .zero)
  | mulS {Γ} (s t : AT) : Pf Γ (.eq (.mul s (.succ t)) (.add (.mul s t) s))
  | ind {Γ a} {A : HF a} (x : Nat) : FreeFor x .zero A → FreeFor x (.succ (.var x)) A →
      Pf Γ (subst x .zero A) → Pf Γ (.all x (.imp A (subst x (.succ (.var x)) A))) → Pf Γ (.all x A)

/-! ## soundness of modified realizability -/

theorem proj {a : Ty} {A : HF a} : {Γ : List Hy} → (⟨a, A⟩ : Hy) ∈ Γ →
    ∃ t : CT (.arr (H Γ) a), ∀ ρ h, mrH Γ ρ h → mr A ρ (ev t h) := by
  intro Γ m
  induction m with
  | head => exact ⟨.fst, fun _ _ hh => hh.1⟩
  | tail _ _ ih =>
    match ih with
    | ⟨t, ht⟩ => exact ⟨lam1 (ap (c t) (ap (c .snd) v0)), fun ρ h hh => by
        rw [lam1_spec]
        exact ht ρ h.2 hh.2⟩

/-- the realizer type of a sequent: environment → realizers of Γ → realizer of A -/
abbrev RT (Γ : List Hy) (a : Ty) : Ty := .arr (.arr .nat .nat) (.arr (H Γ) a)

def Realized (Γ : List Hy) {a : Ty} (A : HF a) : Prop :=
  ∃ R : CT (RT Γ a), ∀ ρ h, mrH Γ ρ h → mr A ρ (ev R ρ h)

/-- soundness: every derivable sequent has a closed T-term as modified realizer -/
theorem sound {Γ : List Hy} {a : Ty} {A : HF a} (d : Pf Γ A) : Realized Γ A := by
  induction d with
  | hyp m =>
    match proj m with
    | ⟨t, ht⟩ => exact ⟨mk2 (ap (c t) v0), fun ρ h hh => by rw [mk2_spec]; exact ht ρ h hh⟩
  | @botE Γ a A _ ih =>
    match ih with
    | ⟨R, hR⟩ => exact ⟨mk2 (c (dflt a)), fun ρ h hh => (hR ρ h hh).elim⟩
  | conjI _ _ ih1 ih2 =>
    match ih1, ih2 with
    | ⟨R1, h1⟩, ⟨R2, h2⟩ => exact ⟨mk2 (ap (ap (c .pair) (ap (ap (c R1) v1) v0)) (ap (ap (c R2) v1) v0)),
        fun ρ h hh => by rw [mk2_spec]; exact ⟨h1 ρ h hh, h2 ρ h hh⟩⟩
  | conjL _ ih =>
    match ih with
    | ⟨R, hR⟩ => exact ⟨mk2 (ap (c .fst) (ap (ap (c R) v1) v0)), fun ρ h hh => by rw [mk2_spec]; exact (hR ρ h hh).1⟩
  | conjR _ ih =>
    match ih with
    | ⟨R, hR⟩ => exact ⟨mk2 (ap (c .snd) (ap (ap (c R) v1) v0)), fun ρ h hh => by rw [mk2_spec]; exact (hR ρ h hh).2⟩
  | @disjL Γ a b A B _ ih =>
    match ih with
    | ⟨R, hR⟩ => exact ⟨mk2 (ap (ap (c .pair) (c .zero)) (ap (ap (c .pair) (ap (ap (c R) v1) v0)) (c (dflt b)))),
        fun ρ h hh => by rw [mk2_spec]; exact ⟨fun _ => hR ρ h hh, fun e => (e rfl).elim⟩⟩
  | @disjR Γ a b A B _ ih =>
    match ih with
    | ⟨R, hR⟩ => exact ⟨mk2 (ap (ap (c .pair) (c (num 1))) (ap (ap (c .pair) (c (dflt a))) (ap (ap (c R) v1) v0))),
        fun ρ h hh => by
          rw [mk2_spec]
          refine ⟨fun e => ?_, fun _ => hR ρ h hh⟩
          have e' : (1 : Nat) = 0 := (num_spec 1).symm.trans e
          exact Nat.noConfusion e'⟩
  | @disjE Γ a b d A B C _ _ _ ih0 ih1 ih2 =>
    match ih0, ih1, ih2 with
    | ⟨R0, h0⟩, ⟨R1, h1⟩, ⟨R2, h2⟩ =>
      let D : OT [H Γ, .arr .nat .nat] (.prod .nat (.prod a b)) := ap (ap (c R0) v1) v0
      refine ⟨mk2 (ap (ap (ap (c (condT d)) (ap (c .fst) D))
        (ap (ap (c R1) v1) (ap (ap (c .pair) (ap (c .fst) (ap (c .snd) D))) v0)))
        (ap (ap (c R2) v1) (ap (ap (c .pair) (ap (c .snd) (ap (c .snd) D))) v0))), fun ρ h hh => ?_⟩
      rw [mk2_spec]
      have hd := h0 ρ h hh
      show mr C ρ (ev (condT d) (ev R0 ρ h).1 (ev R1 ρ ((ev R0 ρ h).2.1, h)) (ev R2 ρ ((ev R0 ρ h).2.2, h)))
      match hk : (ev R0 ρ h).1 with
      | 0 =>
        rw [cond_zero]
        exact h1 ρ _ ⟨hd.1 hk, hh⟩
      | k + 1 =>
        rw [cond_succ]
        exact h2 ρ _ ⟨hd.2 (fun e => Nat.noConfusion (hk.symm.trans e)), hh⟩
  | @impI Γ a b A B _ ih =>
    match ih with
    | ⟨R, hR⟩ => exact ⟨mk2 (abs (ap (ap (c R) v2) (ap (ap (c .pair) v0) v1))), fun ρ h hh => by
        rw [mk2_spec]
        intro y hy
        rw [abs_spec]
        exact hR ρ (y, h) ⟨hy, hh⟩⟩
  | impE _ _ ih1 ih2 =>
    match ih1, ih2 with
    | ⟨R1, h1⟩, ⟨R2, h2⟩ => exact ⟨mk2 (ap (ap (ap (c R1) v1) v0) (ap (ap (c R2) v1) v0)),
        fun ρ h hh => by rw [mk2_spec]; exact h1 ρ h hh _ (h2 ρ h hh)⟩
  | @allI Γ a A x nf _ ih =>
    match ih with
    | ⟨R, hR⟩ => exact ⟨mk2 (abs (ap (ap (c R) (ap (ap (c (updT x)) v2) v0)) v1)), fun ρ h hh => by
        rw [mk2_spec]
        intro n
        rw [abs_spec]
        have e : ∀ v, ev (updT x) ρ n v = upd ρ x n v := updT_spec x ρ n
        have hh' : mrH Γ (ev (updT x) ρ n) h :=
          mrH_ext Γ (fun v => (e v).symm) h (mrH_notfree x n Γ nf ρ h hh)
        exact (mr_ext A e _).1 (hR _ h hh')⟩
  | @allE Γ a A x t ff _ ih =>
    match ih with
    | ⟨R, hR⟩ => exact ⟨mk2 (ap (ap (ap (c R) v1) v0) (ap (c (tT t)) v1)), fun ρ h hh => by
        rw [mk2_spec]
        show mr (subst x t A) ρ (ev R ρ h (ev (tT t) ρ))
        rw [tT_spec]
        exact (mr_subst x t A ff ρ _).2 (hR ρ h hh (aev ρ t))⟩
  | @exI Γ a A x t ff _ ih =>
    match ih with
    | ⟨R, hR⟩ => exact ⟨mk2 (ap (ap (c .pair) (ap (c (tT t)) v1)) (ap (ap (c R) v1) v0)), fun ρ h hh => by
        rw [mk2_spec]
        show mr A (upd ρ x (ev (tT t) ρ)) (ev R ρ h)
        rw [tT_spec]
        exact (mr_subst x t A ff ρ _).1 (hR ρ h hh)⟩
  | @exE Γ a d A C x nf nfc _ _ ih0 ih1 =>
    match ih0, ih1 with
    | ⟨R0, h0⟩, ⟨R1, h1⟩ =>
      let D : OT [H Γ, .arr .nat .nat] (.prod .nat a) := ap (ap (c R0) v1) v0
      refine ⟨mk2 (ap (ap (c R1) (ap (ap (c (updT x)) v1) (ap (c .fst) D))) (ap (ap (c .pair) (ap (c .snd) D)) v0)),
        fun ρ h hh => ?_⟩
      rw [mk2_spec]
      show mr C ρ (ev R1 (ev (updT x) ρ (ev R0 ρ h).1) ((ev R0 ρ h).2, h))
      have e : ∀ v, ev (updT x) ρ (ev R0 ρ h).1 v = upd ρ x (ev R0 ρ h).1 v := updT_spec x ρ _
      have hA : mr A (ev (updT x) ρ (ev R0 ρ h).1) (ev R0 ρ h).2 := (mr_ext A e _).2 (h0 ρ h hh)
      have hΓ : mrH Γ (ev (updT x) ρ (ev R0 ρ h).1) h :=
        mrH_ext Γ (fun v => (e v).symm) h (mrH_notfree x _ Γ nf ρ h hh)
      exact (mr_notfree x _ C nfc ρ _).1 ((mr_ext C e _).1 (h1 _ _ ⟨hA, hΓ⟩))
  | refl t => exact ⟨mk2 (c .zero), fun _ _ _ => rfl⟩
  | @leib Γ a A x s t fs ft _ _ ih1 ih2 =>
    match ih1, ih2 with
    | ⟨R1, h1⟩, ⟨R2, h2⟩ => exact ⟨R2, fun ρ h hh => by
        have e : aev ρ s = aev ρ t := h1 ρ h hh
        have p := (mr_subst x s A fs ρ _).1 (h2 ρ h hh)
        rw [e] at p
        exact (mr_subst x t A ft ρ _).2 p⟩
  | sne s => exact ⟨mk2 (c (dflt _)), fun ρ h _ => fun _ e => Nat.noConfusion e⟩
  | sinj s t => exact ⟨mk2 (c (dflt _)), fun ρ h _ => fun _ e => Nat.succ.inj e⟩
  | add0 s => exact ⟨mk2 (c .zero), fun _ _ _ => rfl⟩
  | addS s t => exact ⟨mk2 (c .zero), fun _ _ _ => rfl⟩
  | mul0 s => exact ⟨mk2 (c .zero), fun _ _ _ => rfl⟩
  | mulS s t => exact ⟨mk2 (c .zero), fun _ _ _ => rfl⟩
  | @ind Γ a A x f0 fs _ _ ih0 ihs =>
    match ih0, ihs with
    | ⟨R0, h0⟩, ⟨Rs, hs⟩ =>
      refine ⟨mk2 (abs (ap (ap (ap (c .R) (ap (ap (c R0) v2) v1)) (ap (ap (c Rs) v2) v1)) v0)), fun ρ h hh => ?_⟩
      rw [mk2_spec]
      intro n
      rw [abs_spec]
      show mr A (upd ρ x n) (recN (ev R0 ρ h) (ev Rs ρ h) n)
      induction n with
      | zero => exact (mr_subst x .zero A f0 ρ _).1 (h0 ρ h hh)
      | succ k ihk =>
        have step := hs ρ h hh k _ ihk
        have p := (mr_subst x (.succ (.var x)) A fs (upd ρ x k) _).1 step
        have ek : aev (upd ρ x k) (.succ (.var x)) = k + 1 := by
          show cond (beq' x x) k (ρ x) + 1 = k + 1
          rw [beq'_self]
          rfl
        rw [ek] at p
        exact (mr_ext A (upd_same ρ x k (k + 1)) _).1 p

/-- closed formulas proved in HA are realized by closed T-terms (environment irrelevant) -/
theorem interpret {a : Ty} {A : HF a} (d : Pf [] A) :
    ∃ R : CT (RT [] a), ∀ ρ, mr A ρ (ev R ρ (show den (H []) from (0 : Nat))) :=
  match sound d with
  | ⟨R, hR⟩ => ⟨R, fun ρ => hR ρ (show den (H []) from (0 : Nat)) trivial⟩

end MR

end Found

#print axioms Found.MR.mr_notfree
#print axioms Found.MR.mrH_notfree
#print axioms Found.MR.proj
#print axioms Found.MR.sound
#print axioms Found.MR.interpret

found/HAomega.lean

lean_all/found/HAomega.lean

/-
  T187. found/HAomega — higher-type arithmetic HAω and Gödel's T as a point:
  finite types over ℕ (the core's function space on the NNO), intrinsically
  typed terms with 0, S and the recursor, evaluated in the standard model
  (types ↦ Lean types).
  * Every T-term denotes (totality of Gödel's T in the standard model).
  * The recursor equations hold: R a f 0 = a, R a f (S n) = f n (R a f n).
  * Primitive recursion (T81) is the first-order part; Dialectica (T180)
    interprets HA in T (cited line).

  Lean 4 core only, hand-written proofs, no axioms.
-/

namespace Found

namespace GodelT

inductive Ty where
  | nat : Ty
  | arr : Ty → Ty → Ty

def den : Ty → Type
  | .nat => Nat
  | .arr a b => den a → den b

inductive Var : List Ty → Ty → Type where
  | z {Γ a} : Var (a :: Γ) a
  | s {Γ a b} : Var Γ a → Var (b :: Γ) a

inductive Tm : List Ty → Ty → Type where
  | var {Γ a} : Var Γ a → Tm Γ a
  | lam {Γ a b} : Tm (a :: Γ) b → Tm Γ (.arr a b)
  | app {Γ a b} : Tm Γ (.arr a b) → Tm Γ a → Tm Γ b
  | zero {Γ} : Tm Γ .nat
  | succ {Γ} : Tm Γ (.arr .nat .nat)
  | recT {Γ a} : Tm Γ a → Tm Γ (.arr .nat (.arr a a)) → Tm Γ (.arr .nat a)

def Env : List Ty → Type
  | [] => Unit
  | a :: Γ => den a × Env Γ

def look : {Γ : List Ty} → {a : Ty} → Var Γ a → Env Γ → den a
  | _ :: _, _, .z, ρ => ρ.1
  | _ :: _, _, .s v, ρ => look v ρ.2

def recF {A : Type} (a : A) (f : Nat → A → A) : Nat → A
  | 0 => a
  | n + 1 => f n (recF a f n)

def eval : {Γ : List Ty} → {a : Ty} → Tm Γ a → Env Γ → den a
  | _, _, .var v, ρ => look v ρ
  | _, _, .lam t, ρ => fun x => eval t (x, ρ)
  | _, _, .app t u, ρ => (eval t ρ) (eval u ρ)
  | _, _, .zero, _ => (0 : Nat)
  | _, _, .succ, _ => fun (n : Nat) => n + 1
  | _, _, .recT a f, ρ => recF (eval a ρ) (eval f ρ)

theorem rec_zero {Γ : List Ty} {a : Ty} (x : Tm Γ a) (f : Tm Γ (.arr .nat (.arr a a))) (ρ : Env Γ) :
    eval (.app (.recT x f) .zero) ρ = eval x ρ := rfl

theorem rec_succ {Γ : List Ty} {a : Ty} (x : Tm Γ a) (f : Tm Γ (.arr .nat (.arr a a))) (n : Tm Γ .nat) (ρ : Env Γ) :
    eval (.app (.recT x f) (.app .succ n)) ρ = eval f ρ (eval n ρ) (eval (.app (.recT x f) n) ρ) := rfl

/-- addition in T: add = λm. R m (λ_ r. S r) — denotes + -/
def addT : Tm [] (.arr .nat (.arr .nat .nat)) :=
  .lam (.recT (.var .z) (.lam (.lam (.app .succ (.var .z)))))

theorem addT_correct (m : Nat) : ∀ n : Nat, (eval addT () m n : Nat) = m + n
  | 0 => rfl
  | n + 1 => congrArg (fun k : Nat => k + 1) (addT_correct m n)

end GodelT

end Found

#print axioms Found.GodelT.rec_zero
#print axioms Found.GodelT.rec_succ
#print axioms Found.GodelT.addT_correct

found/HOIL.lean

lean_all/found/HOIL.lean

/-
  T114. found/HOIL — higher-order intuitionistic logic as a point: the
  simple type model of T205 (types over ι and o in the setoid stem) with no
  condition on Ω.
  * Comprehension (standard, T142) and extensionality (T205) hold; the
    quantifiers are the adjoints of T73 (reused).
  * Ω is Boolean ⟺ excluded middle — so HOL (T205) = HOIL + the Boolean
    condition, and topos internal languages are HOIL (T64, T73).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.HOL

namespace Found

namespace HOIL

/-- excluded middle from double negation elimination on Ω -/
theorem lem_of_boolean (hB : Prp.Boolean OmegaHA) (p : Prop) : p ∨ ¬ p :=
  hB (p ∨ ¬ p) (fun n => n (Or.inr (fun hp => n (Or.inl hp))))

theorem boolean_iff_lem : Prp.Boolean OmegaHA ↔ ∀ p : Prop, p ∨ ¬ p :=
  ⟨lem_of_boolean, HOL.classical_condition⟩

/-- the intuitionistic part needs no condition: ¬¬(p ∨ ¬p) holds -/
theorem nn_lem (p : Prop) : ¬ ¬ (p ∨ ¬ p) := fun n => n (Or.inr (fun hp => n (Or.inl hp)))

end HOIL

end Found

#print axioms Found.HOIL.lem_of_boolean
#print axioms Found.HOIL.boolean_iff_lem
#print axioms Found.HOIL.nn_lem

found/HOL.lean

lean_all/found/HOL.lean

/-
  T205. found/HOL — Church's simple type theory / HOL as a point: types
  over ι (individuals, a parameter) and o (propositions) interpreted in the
  core's setoid stem: o ↦ Ω, σ → τ ↦ exponential (T59).
  * Extensionality (functional and Boolean) holds in the model by the core's
    equality (no funext/propext axioms).
  * HOL's classical axiom is the LEM condition on Ω (T66); the Hilbert ε
    of Isabelle/HOL is the choice condition (T87) — both as parameters.

  Lean 4 core only, hand-written proofs, no axioms.
  CIC dependence: Ω = ULift Prop.
-/
import found.Core
import found.Classical

namespace Found

namespace HOL

open Core

inductive Ty where
  | ι : Ty
  | o : Ty
  | arr : Ty → Ty → Ty

def interp (I : Setd.{0}) : Ty → Setd.{0}
  | .ι => I
  | .o => OmegaU
  | .arr a b => expS (interp I a) (interp I b)

/-- functional extensionality in every function type -/
theorem fun_ext (I : Setd.{0}) (a b : Ty) (f g : (interp I (.arr a b)).car)
    (h : ∀ x, (interp I b).eqv (f.fn x) (g.fn x)) : (interp I (.arr a b)).eqv f g := h

/-- Boolean extensionality: propositions equal iff equivalent -/
theorem bool_ext (I : Setd.{0}) (p q : (interp I .o).car) (h : p.down ↔ q.down) : (interp I .o).eqv p q := h

/-- the classical axiom of HOL is the LEM condition, i.e. Ω Boolean (T66) -/
theorem classical_condition (lem : ∀ p : Prop, p ∨ ¬ p) : Prp.Boolean OmegaHA :=
  fun p hp => (lem p).elim id (fun n => (hp n).elim)

end HOL

end Found

#print axioms Found.HOL.fun_ext
#print axioms Found.HOL.bool_ext
#print axioms Found.HOL.classical_condition

found/Hessenberg.lean

lean_all/found/Hessenberg.lean

/-
  T106 (b), slice N3c(iv)(b,c). found/Hessenberg — in the classical point,
  every type ν with an injection ℕ → ν satisfies ν × ν ↪ ν.
  Zorn (preorder form, found/Zorn) on partial codings: a set A ⊇ e(ℕ) with an
  injective, total, functional relation G : A × A → A extending the Cantor
  pairing on e(ℕ).  For a maximal coding, A cannot inject into its
  complement (otherwise the coding extends to A plus a disjoint copy B of A,
  coding the new pairs into B through G and three tags), so the complement
  injects into A, whence ν ↪ A and ν × ν ↪ A × A ↪ A ↪ ν.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Zorn
import found.CountUnion

namespace Found

namespace Hess

open CP WO Card ZL CU

variable (K : Classical') {ν : Type} (e : Nat → ν) (he : Inj e)

structure Obj where
  A : ν → Prop
  G : ν → ν → ν → Prop
  base : ∀ n, A (e n)
  baseG : ∀ x y, G (e x) (e y) (e (RM.pair x y))
  fn : ∀ a b c c', G a b c → G a b c' → c = c'
  tot : ∀ a b, A a → A b → ∃ c, G a b c
  dom : ∀ a b c, G a b c → A a ∧ A b ∧ A c
  inj : ∀ a b a' b' c, G a b c → G a' b' c → a = a' ∧ b = b'

variable {e}

def Le (x y : Obj e) : Prop := (∀ v, x.A v → y.A v) ∧ (∀ a b c, x.G a b c → y.G a b c)

theorem le_refl (x : Obj e) : Le x x := ⟨fun _ h => h, fun _ _ _ h => h⟩
theorem le_trans {x y z : Obj e} (h1 : Le x y) (h2 : Le y z) : Le x z :=
  ⟨fun v h => h2.1 v (h1.1 v h), fun a b c h => h2.2 a b c (h1.2 a b c h)⟩

include he in
/-- the base coding: Cantor pairing on e(ℕ) -/
def base : Obj e where
  A v := ∃ n, v = e n
  G a b c := ∃ x y, a = e x ∧ b = e y ∧ c = e (RM.pair x y)
  base n := ⟨n, rfl⟩
  baseG x y := ⟨x, y, rfl, rfl, rfl⟩
  fn a b c c' := fun ⟨x, y, ha, hb, hc⟩ ⟨x', y', ha', hb', hc'⟩ => by
    have hx : x = x' := he _ _ (ha.symm.trans ha')
    have hy : y = y' := he _ _ (hb.symm.trans hb')
    rw [hc, hc', hx, hy]
  tot a b := fun ⟨x, hx⟩ ⟨y, hy⟩ => ⟨e (RM.pair x y), x, y, hx, hy, rfl⟩
  dom a b c := fun ⟨x, y, ha, hb, hc⟩ => ⟨⟨x, ha⟩, ⟨y, hb⟩, ⟨_, hc⟩⟩
  inj a b a' b' c := fun ⟨x, y, ha, hb, hc⟩ ⟨x', y', ha', hb', hc'⟩ => by
    have hp := pair_inj (he _ _ (hc.symm.trans hc'))
    rw [ha, hb, ha', hb', hp.1, hp.2]
    exact ⟨rfl, rfl⟩

theorem base_le (x : Obj e) : Le (base he) x :=
  ⟨fun _ ⟨n, h⟩ => h ▸ x.base n, fun _ _ _ ⟨a, b, ha, hb, hc⟩ => by rw [ha, hb, hc]; exact x.baseG a b⟩

section Union
variable (C : Obj e → Prop) (hC : Chain Le C)

def Wit (o : Obj e) : Prop := C o ∨ o = base he

include hC in
theorem common {o1 o2 : Obj e} (h1 : Wit he C o1) (h2 : Wit he C o2) :
    ∃ o, Wit he C o ∧ Le o1 o ∧ Le o2 o := by
  rcases h1 with c1 | e1
  · rcases h2 with c2 | e2
    · rcases hC o1 o2 c1 c2 with l | l
      · exact ⟨o2, Or.inl c2, l, le_refl _⟩
      · exact ⟨o1, Or.inl c1, le_refl _, l⟩
    · exact ⟨o1, Or.inl c1, le_refl _, e2 ▸ base_le he o1⟩
  · exact ⟨o2, h2, e1 ▸ base_le he o2, le_refl _⟩

include hC in
/-- the union of a chain (together with the base) is a coding -/
def union : Obj e where
  A v := ∃ o, Wit he C o ∧ o.A v
  G a b c := ∃ o, Wit he C o ∧ o.G a b c
  base n := ⟨base he, Or.inr rfl, (base he).base n⟩
  baseG x y := ⟨base he, Or.inr rfl, (base he).baseG x y⟩
  fn a b c c' := fun ⟨_, w1, g1⟩ ⟨_, w2, g2⟩ => match common he C hC w1 w2 with
    | ⟨o, _, l1, l2⟩ => o.fn a b c c' (l1.2 _ _ _ g1) (l2.2 _ _ _ g2)
  tot a b := fun ⟨_, w1, a1⟩ ⟨_, w2, b2⟩ => match common he C hC w1 w2 with
    | ⟨o, w, l1, l2⟩ => match o.tot a b (l1.1 a a1) (l2.1 b b2) with
      | ⟨c, hc⟩ => ⟨c, o, w, hc⟩
  dom a b c := fun ⟨o, w, g⟩ => match o.dom a b c g with
    | ⟨ha, hb, hc⟩ => ⟨⟨o, w, ha⟩, ⟨o, w, hb⟩, ⟨o, w, hc⟩⟩
  inj a b a' b' c := fun ⟨_, w1, g1⟩ ⟨_, w2, g2⟩ => match common he C hC w1 w2 with
    | ⟨o, _, l1, l2⟩ => o.inj a b a' b' c (l1.2 _ _ _ g1) (l2.2 _ _ _ g2)

theorem union_ub (o : Obj e) (ho : C o) : Le o (union he C hC) :=
  ⟨fun _ h => ⟨o, Or.inl ho, h⟩, fun _ _ _ h => ⟨o, Or.inl ho, h⟩⟩

end Union

include K he in
/-- a maximal coding exists (Zorn) -/
theorem maximal : ∃ m : Obj e, ∀ y, Le m y → Le y m :=
  zorn_pre K Le le_refl (fun h1 h2 => le_trans h1 h2) (fun C hC => ⟨union he C hC, union_ub he C hC⟩)

/-! ## the extension step and the conclusion -/

def tagE (e : Nat → ν) : Bool → Bool → ν
  | false, false => e 0
  | false, true => e 0
  | true, false => e 1
  | true, true => e 2

include he in
theorem tagE_inj : (t1 t2 t1' t2' : Bool) → ¬ (t1 = false ∧ t2 = false) → ¬ (t1' = false ∧ t2' = false) →
    tagE e t1 t2 = tagE e t1' t2' → t1 = t1' ∧ t2 = t2'
  | false, false, _, _, h, _, _ => (h ⟨rfl, rfl⟩).elim
  | _, _, false, false, _, h, _ => (h ⟨rfl, rfl⟩).elim
  | false, true, false, true, _, _, _ => ⟨rfl, rfl⟩
  | false, true, true, false, _, _, x => Nat.noConfusion (he _ _ x)
  | false, true, true, true, _, _, x => Nat.noConfusion (he _ _ x)
  | true, false, false, true, _, _, x => Nat.noConfusion (he _ _ x)
  | true, false, true, false, _, _, _ => ⟨rfl, rfl⟩
  | true, false, true, true, _, _, x => Nat.noConfusion (Nat.succ.inj (he _ _ x))
  | true, true, false, true, _, _, x => Nat.noConfusion (he _ _ x)
  | true, true, true, false, _, _, x => Nat.noConfusion (Nat.succ.inj (he _ _ x))
  | true, true, true, true, _, _, _ => ⟨rfl, rfl⟩

section Ext
variable (m : Obj e) (h : {x // m.A x} → {x // ¬ m.A x}) (hh : Inj h)

def Bs (v : ν) : Prop := ∃ a : {x // m.A x}, (h a).val = v
def A' (v : ν) : Prop := m.A v ∨ Bs m h v

def R (p : ν) (t : Bool) (a : {x // m.A x}) : Prop := (t = false ∧ a.val = p) ∨ (t = true ∧ (h a).val = p)

def NewSpec (p q r : ν) : Prop :=
  ∃ tp ap tq aq inner outer, R m h p tp ap ∧ R m h q tq aq ∧ m.G ap.val aq.val inner ∧
    m.G (tagE e tp tq) inner outer ∧ ∃ ho : m.A outer, r = (h ⟨outer, ho⟩).val

include hh in
theorem R_unique {p : ν} {t t' : Bool} {a a' : {x // m.A x}} (r1 : R m h p t a) (r2 : R m h p t' a') :
    t = t' ∧ a = a' := by
  rcases r1 with ⟨t1, e1⟩ | ⟨t1, e1⟩ <;> rcases r2 with ⟨t2, e2⟩ | ⟨t2, e2⟩
  · subst t1 t2; exact ⟨rfl, Subtype.ext (e1.trans e2.symm)⟩
  · exact ((h a').property (e2 ▸ e1 ▸ a.property)).elim
  · exact ((h a).property (e1 ▸ e2 ▸ a'.property)).elim
  · subst t1 t2; exact ⟨rfl, hh _ _ (Subtype.ext (e1.trans e2.symm))⟩

theorem R_same {p p' : ν} {t : Bool} {a : {x // m.A x}} (r1 : R m h p t a) (r2 : R m h p' t a) : p = p' := by
  rcases r1 with ⟨t1, e1⟩ | ⟨t1, e1⟩ <;> rcases r2 with ⟨t2, e2⟩ | ⟨t2, e2⟩
  · exact e1.symm.trans e2
  · rw [t1] at t2; exact Bool.noConfusion t2
  · rw [t1] at t2; exact Bool.noConfusion t2
  · exact e1.symm.trans e2

theorem R_ex (p : ν) (hp : A' m h p) : ∃ t a, R m h p t a :=
  hp.elim (fun ha => ⟨false, ⟨p, ha⟩, Or.inl ⟨rfl, rfl⟩⟩) (fun ⟨a, ha⟩ => ⟨true, a, Or.inr ⟨rfl, ha⟩⟩)

theorem R_false {p : ν} {a : {x // m.A x}} (r : R m h p false a) : m.A p :=
  r.elim (fun ⟨_, e⟩ => e ▸ a.property) (fun ⟨t, _⟩ => Bool.noConfusion t)

include K hh he in
/-- the extended coding on A ∪ B -/
def ext : Obj e where
  A := A' m h
  G p q r := m.G p q r ∨ (A' m h p ∧ A' m h q ∧ ¬ (m.A p ∧ m.A q) ∧ NewSpec m h p q r)
  base n := Or.inl (m.base n)
  baseG x y := Or.inl (m.baseG x y)
  fn p q r r' := fun g1 g2 => by
    rcases g1 with o1 | ⟨_, _, n1, s1⟩ <;> rcases g2 with o2 | ⟨_, _, n2, s2⟩
    · exact m.fn p q r r' o1 o2
    · exact (n2 ⟨(m.dom p q r o1).1, (m.dom p q r o1).2.1⟩).elim
    · exact (n1 ⟨(m.dom p q r' o2).1, (m.dom p q r' o2).2.1⟩).elim
    · match s1, s2 with
      | ⟨tp, ap, tq, aq, i1, o1, rp, rq, gi, go, ho, er⟩, ⟨tp', ap', tq', aq', i2, o2, rp', rq', gi', go', ho', er'⟩ =>
        have u1 := R_unique m h hh rp rp'
        have u2 := R_unique m h hh rq rq'
        have t1 := u1.1
        have t2 := u2.1
        subst t1 t2
        rw [u1.2, u2.2] at gi
        have ei : i1 = i2 := m.fn _ _ _ _ gi gi'
        subst ei
        have eo : o1 = o2 := m.fn _ _ _ _ go go'
        subst eo
        rw [er, er']
  tot p q := fun hp hq => by
    rcases K.lem (m.A p ∧ m.A q) with both | nb
    · match m.tot p q both.1 both.2 with
      | ⟨c, hc⟩ => exact ⟨c, Or.inl hc⟩
    · match R_ex m h p hp, R_ex m h q hq with
      | ⟨tp, ap, rp⟩, ⟨tq, aq, rq⟩ =>
        match m.tot ap.val aq.val ap.property aq.property with
        | ⟨inner, gi⟩ =>
          have ht : m.A (tagE e tp tq) := by cases tp <;> cases tq <;> exact m.base _
          match m.tot _ inner ht (m.dom _ _ _ gi).2.2 with
          | ⟨outer, go⟩ => exact ⟨_, Or.inr ⟨hp, hq, nb, tp, ap, tq, aq, inner, outer, rp, rq, gi, go,
              (m.dom _ _ _ go).2.2, rfl⟩⟩
  dom p q r := fun g => by
    rcases g with o | ⟨hp, hq, _, sp⟩
    · have d := m.dom p q r o
      exact ⟨Or.inl d.1, Or.inl d.2.1, Or.inl d.2.2⟩
    · match sp with
      | ⟨_, _, _, _, _, outer, _, _, _, _, ho, er⟩ => exact ⟨hp, hq, Or.inr ⟨⟨outer, ho⟩, er.symm⟩⟩
  inj p q p' q' r := fun g1 g2 => by
    rcases g1 with o1 | ⟨_, _, n1, s1⟩ <;> rcases g2 with o2 | ⟨_, _, n2, s2⟩
    · exact m.inj p q p' q' r o1 o2
    · match s2 with
      | ⟨_, _, _, _, _, outer, _, _, _, _, ho, er⟩ =>
        exact ((h ⟨outer, ho⟩).property (er ▸ (m.dom _ _ _ o1).2.2)).elim
    · match s1 with
      | ⟨_, _, _, _, _, outer, _, _, _, _, ho, er⟩ =>
        exact ((h ⟨outer, ho⟩).property (er ▸ (m.dom _ _ _ o2).2.2)).elim
    · match s1, s2 with
      | ⟨tp, ap, tq, aq, i1, o1, rp, rq, gi, go, ho, er⟩, ⟨tp', ap', tq', aq', i2, o2, rp', rq', gi', go', ho', er'⟩ =>
        have eo : o1 = o2 := congrArg Subtype.val (hh _ _ (Subtype.ext (er.symm.trans er')))
        subst eo
        have it := m.inj _ _ _ _ _ go go'
        have nf : ∀ {x y tx ty ax ay}, R m h x tx ax → R m h y ty ay → ¬ (m.A x ∧ m.A y) →
            ¬ (tx = false ∧ ty = false) := fun {x y tx ty ax ay} rx ry n ⟨e1, e2⟩ =>
          n ⟨R_false m h (e1 ▸ rx), R_false m h (e2 ▸ ry)⟩
        have tt := tagE_inj he tp tq tp' tq' (nf rp rq n1) (nf rp' rq' n2) it.1
        have ii : i1 = i2 := it.2
        subst ii
        have aa := m.inj _ _ _ _ _ gi gi'
        have t1 := tt.1
        have t2 := tt.2
        subst t1 t2
        have ea : ap = ap' := Subtype.ext aa.1
        have eb : aq = aq' := Subtype.ext aa.2
        subst ea eb
        exact ⟨R_same m h rp rp', R_same m h rq rq'⟩


theorem ext_le : Le m (ext K he m h hh) := ⟨fun _ hv => Or.inl hv, fun _ _ _ g => Or.inl g⟩

end Ext

include K he in
/-- a maximal coding admits no injection of its domain into its complement -/
theorem no_into_compl (m : Obj e) (hm : ∀ y, Le m y → Le y m) :
    ¬ ∃ h : {x // m.A x} → {x // ¬ m.A x}, Inj h := fun ⟨h, hh⟩ =>
  let x0 : {x // m.A x} := ⟨e 0, m.base 0⟩
  (h x0).property ((hm _ (ext_le K he m h hh)).1 _ (Or.inr ⟨x0, rfl⟩))

section Final
variable (m : Obj e) (g : {x // ¬ m.A x} → {x // m.A x}) (hg : Inj g)

def Spec (v w : ν) : Prop :=
  (m.A v ∧ m.G (e 0) v w) ∨ (∃ hv : ¬ m.A v, m.G (e 1) (g ⟨v, hv⟩).val w)

include K in
theorem spec_ex (v : ν) : ∃ w, Spec m g v w :=
  (K.lem (m.A v)).elim
    (fun hv => match m.tot _ _ (m.base 0) hv with | ⟨w, hw⟩ => ⟨w, Or.inl ⟨hv, hw⟩⟩)
    (fun hv => match m.tot _ _ (m.base 1) (g ⟨v, hv⟩).property with
      | ⟨w, hw⟩ => ⟨w, Or.inr ⟨hv, hw⟩⟩)

def fA (v : ν) : ν := K.ch (Spec m g v) (spec_ex K m g v)

theorem fA_spec (v : ν) : Spec m g v (fA K m g v) := K.ch_spec (Spec m g v) (spec_ex K m g v)

theorem fA_mem (v : ν) : m.A (fA K m g v) :=
  (fA_spec K m g v).elim (fun ⟨_, hw⟩ => (m.dom _ _ _ hw).2.2) (fun ⟨_, hw⟩ => (m.dom _ _ _ hw).2.2)

include he hg in
theorem fA_inj : Inj (fA K m g) := fun v v' ev => by
  have s1 := fA_spec K m g v
  have s2 := fA_spec K m g v'
  rw [ev] at s1
  rcases s1 with ⟨_, w1⟩ | ⟨h1, w1⟩ <;> rcases s2 with ⟨_, w2⟩ | ⟨h2, w2⟩
  · exact (m.inj _ _ _ _ _ w1 w2).2
  · exact Nat.noConfusion (he _ _ (m.inj _ _ _ _ _ w1 w2).1)
  · exact Nat.noConfusion (he _ _ (m.inj _ _ _ _ _ w1 w2).1)
  · have hh := hg _ _ (Subtype.ext (m.inj _ _ _ _ _ w1 w2).2)
    exact congrArg Subtype.val hh

def code (p : ν × ν) : ν :=
  K.ch (m.G (fA K m g p.1) (fA K m g p.2)) (m.tot _ _ (fA_mem K m g p.1) (fA_mem K m g p.2))

include he hg in
theorem code_inj : Inj (code K m g) := fun p q e' => by
  have g1 := K.ch_spec (m.G (fA K m g p.1) (fA K m g p.2)) (m.tot _ _ (fA_mem K m g p.1) (fA_mem K m g p.2))
  have g2 := K.ch_spec (m.G (fA K m g q.1) (fA K m g q.2)) (m.tot _ _ (fA_mem K m g q.1) (fA_mem K m g q.2))
  have g1' : m.G (fA K m g p.1) (fA K m g p.2) (code K m g q) := e' ▸ g1
  have i := m.inj _ _ _ _ _ g1' g2
  have a1 := fA_inj K he m g hg _ _ i.1
  have a2 := fA_inj K he m g hg _ _ i.2
  cases p with
  | mk x y => cases q with
    | mk x' y' => cases a1; cases a2; rfl

end Final

include K he in
/-- Hessenberg: every type containing a copy of ℕ satisfies ν × ν ↪ ν -/
theorem hessenberg : ∃ F : ν × ν → ν, Inj F :=
  match maximal K he with
  | ⟨m, hm⟩ => match comparable K (α := {x // m.A x}) (β := {x // ¬ m.A x}) with
    | Or.inl hin => (no_into_compl K he m hm hin).elim
    | Or.inr ⟨g, hg⟩ => ⟨code K m g, code_inj K he m g hg⟩

end Hess

end Found
#print axioms Found.Hess.ext
#print axioms Found.Hess.no_into_compl
#print axioms Found.Hess.fA_inj
#print axioms Found.Hess.code_inj
#print axioms Found.Hess.hessenberg

found/Higher2.lean

lean_all/found/Higher2.lean

/-
  T193. found/Higher2 — 2-categories as a point: E-categories, functors and
  natural transformations (T61) form a strict 2-category up to ≈.
  * Horizontal composition β ∗ α and its naturality.
  * The interchange law (β' ∘ β) ∗ (α' ∘ α) ≈ (β' ∗ α') ∘ (β ∗ α).
  Double, enriched and multicategories/operads and string diagrams are the
  known variations (cited lines).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Cat

namespace Found

namespace TwoCat

universe u v u' v' u'' v''

variable {C : ECat.{u, v}} {D : ECat.{u', v'}} {E : ECat.{u'', v''}}

/-- horizontal composition component: H(α_a) ; β_{G a} -/
def hcomp {F G : EFun C D} {H K : EFun D E} (α : NatTr F G) (β : NatTr H K) (a : C.Ob) :
    E.Hom (H.obj (F.obj a)) (K.obj (G.obj a)) :=
  E.comp (H.map (α.app a)) (β.app (G.obj a))

/-- the interchange law -/
theorem interchange {F G G' : EFun C D} {H K K' : EFun D E}
    (α : NatTr F G) (α' : NatTr G G') (β : NatTr H K) (β' : NatTr K K') (a : C.Ob) :
    E.heq (hcomp (NatTr.vcomp α α') (NatTr.vcomp β β') a)
      (E.comp (hcomp α β a) (hcomp α' β' a)) := by
  show E.heq (E.comp (H.map (D.comp (α.app a) (α'.app a))) (E.comp (β.app (G'.obj a)) (β'.app (G'.obj a))))
    (E.comp (E.comp (H.map (α.app a)) (β.app (G.obj a))) (E.comp (K.map (α'.app a)) (β'.app (G'.obj a))))
  exact E.htrans (E.lcong (H.map_comp _ _) _)
    (E.htrans (E.assoc _ _ _)
    (E.htrans (E.rcong _ (E.hsymm (E.assoc _ _ _)))
    (E.htrans (E.rcong _ (E.lcong (β.nat (α'.app a)) _))
    (E.htrans (E.rcong _ (E.assoc _ _ _))
    (E.hsymm (E.assoc _ _ _))))))

/-- horizontal composites are natural -/
theorem hcomp_nat {F G : EFun C D} {H K : EFun D E} (α : NatTr F G) (β : NatTr H K) {a b : C.Ob}
    (f : C.Hom a b) :
    E.heq (E.comp (H.map (F.map f)) (hcomp α β b)) (E.comp (hcomp α β a) (K.map (G.map f))) := by
  show E.heq (E.comp (H.map (F.map f)) (E.comp (H.map (α.app b)) (β.app (G.obj b))))
    (E.comp (E.comp (H.map (α.app a)) (β.app (G.obj a))) (K.map (G.map f)))
  exact E.htrans (E.hsymm (E.assoc _ _ _))
    (E.htrans (E.lcong (E.hsymm (H.map_comp _ _)) _)
    (E.htrans (E.lcong (H.map_resp (α.nat f)) _)
    (E.htrans (E.lcong (H.map_comp _ _) _)
    (E.htrans (E.assoc _ _ _)
    (E.htrans (E.rcong _ (β.nat (G.map f)))
    (E.hsymm (E.assoc _ _ _)))))))

end TwoCat

end Found

#print axioms Found.TwoCat.interchange
#print axioms Found.TwoCat.hcomp_nat

found/HigherOrder.lean

lean_all/found/HigherOrder.lean

/-
  T73. found/HigherOrder — quantifiers as adjoints (Lawvere), in the setoid
  topos.

  * For a map f : X → Y of setoids: reindexing f* on Sub(Y) = Pred Y has a
    left adjoint ∃_f and a right adjoint ∀_f:  ∃_f ⊣ f* ⊣ ∀_f.
  * Frobenius reciprocity ∃_f (φ ∧ f* ψ) = ∃_f φ ∧ ψ, and Beck–Chevalley
    for product projections.
  * Equality is ∃ along the diagonal: ∃_Δ ⊤ = the setoid relation.
  * Higher order: predicates on X are the points of the power object
    P X = X ⇒ Ω (T63), and quantifying over P X is ∀/∃ along the
    projection P X × Z → Z; first order = the same with X a base sort.

  Lean 4 core only, hand-written proofs, no axioms.

  CIC dependence: higher-order quantification = impredicative Prop (P X = X ⇒ Prop).
-/
import found.Topos

namespace Found

namespace HO

variable {X Y : Setd.{0}}

def LeP (φ ψ : Pred X) : Prop := ∀ x, φ.p x → ψ.p x

def pull (f : SHom X Y) (ψ : Pred Y) : Pred X :=
  ⟨fun x => ψ.p (f.fn x), fun e h => ψ.resp (f.resp e) h⟩

def exF (f : SHom X Y) (φ : Pred X) : Pred Y :=
  ⟨fun y => ∃ x, φ.p x ∧ Y.eqv (f.fn x) y, fun e ⟨x, h, k⟩ => ⟨x, h, Y.trans k e⟩⟩

def allF (f : SHom X Y) (φ : Pred X) : Pred Y :=
  ⟨fun y => ∀ x, Y.eqv (f.fn x) y → φ.p x, fun e h x k => h x (Y.trans k (Y.symm e))⟩

/-- ∃_f ⊣ f* -/
theorem ex_adj (f : SHom X Y) (φ : Pred X) (ψ : Pred Y) :
    LeP (exF f φ) ψ ↔ LeP φ (pull f ψ) :=
  ⟨fun h x a => h (f.fn x) ⟨x, a, Y.refl _⟩,
   fun h _ ⟨x, a, k⟩ => ψ.resp k (h x a)⟩

/-- f* ⊣ ∀_f -/
theorem all_adj (f : SHom X Y) (ψ : Pred Y) (φ : Pred X) :
    LeP (pull f ψ) φ ↔ LeP ψ (allF f φ) :=
  ⟨fun h _ b x k => h x (ψ.resp (Y.symm k) b),
   fun h x b => h (f.fn x) b x (Y.refl _)⟩

/-- Frobenius reciprocity -/
theorem frobenius (f : SHom X Y) (φ : Pred X) (ψ : Pred Y) (y : Y.car) :
    (∃ x, (φ.p x ∧ ψ.p (f.fn x)) ∧ Y.eqv (f.fn x) y) ↔ ((exF f φ).p y ∧ ψ.p y) :=
  ⟨fun ⟨x, ⟨a, b⟩, k⟩ => ⟨⟨x, a, k⟩, ψ.resp k b⟩,
   fun ⟨⟨x, a, k⟩, b⟩ => ⟨x, ⟨a, ψ.resp (Y.symm k) b⟩, k⟩⟩

/-! ## Beck–Chevalley for projections -/

section BC
variable {Z Z' : Setd.{0}}

def proj (Z X : Setd.{0}) : SHom (prodS Z X) Z := ⟨Prod.fst, fun h => h.1⟩

def gx (g : SHom Z' Z) (X : Setd.{0}) : SHom (prodS Z' X) (prodS Z X) :=
  ⟨fun p => (g.fn p.1, p.2), fun h => ⟨g.resp h.1, h.2⟩⟩

/-- g* ∃_π φ = ∃_π' (g × 1)* φ -/
theorem beck_chevalley (g : SHom Z' Z) (φ : Pred (prodS Z X)) (z' : Z'.car) :
    (pull g (exF (proj Z X) φ)).p z' ↔ (exF (proj Z' X) (pull (gx g X) φ)).p z' :=
  ⟨fun ⟨p, a, k⟩ => ⟨(z', p.2), φ.resp (x := p) (y := (g.fn z', p.2)) ⟨k, X.refl _⟩ a, Z'.refl z'⟩,
   fun ⟨q, a, k⟩ => ⟨(g.fn q.1, q.2), a, g.resp k⟩⟩

end BC

/-! ## Equality as ∃ along the diagonal -/

def diag (X : Setd.{0}) : SHom X (prodS X X) := ⟨fun x => (x, x), fun h => ⟨h, h⟩⟩

def topP (X : Setd.{0}) : Pred X := ⟨fun _ => True, fun _ _ => trivial⟩

theorem eq_is_ex_diag (x y : X.car) : (exF (diag X) (topP X)).p (x, y) ↔ X.eqv x y :=
  ⟨fun ⟨_, _, k⟩ => X.trans (X.symm k.1) k.2, fun e => ⟨x, trivial, X.refl x, e⟩⟩

/-! ## Higher order: predicates are points of P X -/

/-- every respecting predicate is named by a point of the power object -/
def name (φ : Pred X) : (powerS X).car := chi φ

theorem name_mem (φ : Pred X) (x : X.car) : (name φ).fn x ↔ φ.p x := Iff.rfl

/-- second-order comprehension: for every formula on Z × X there is a
  `Z`-indexed family of points of P X naming it -/
theorem comprehension {Z : Setd.{0}} (R : SHom (prodS Z X) OmegaS) (z : Z.car) (x : X.car) :
    ((setdCCC.curry R).fn z).fn x ↔ R.fn (z, x) := Iff.rfl

/-- quantifying over predicates = ∀ along the projection P X × Z → Z -/
theorem all_over_power {Z : Setd.{0}} (φ : Pred (prodS Z (powerS X))) (z : Z.car) :
    (allF (proj Z (powerS X)) φ).p z ↔ ∀ P : (powerS X).car, φ.p (z, P) :=
  ⟨fun h P => h (z, P) (Z.refl z), fun h q k => φ.resp (x := (z, q.2)) (y := q) ⟨Z.symm k, (powerS X).refl q.2⟩ (h q.2)⟩

end HO

end Found

#print axioms Found.HO.ex_adj
#print axioms Found.HO.all_adj
#print axioms Found.HO.frobenius
#print axioms Found.HO.beck_chevalley
#print axioms Found.HO.eq_is_ex_diag
#print axioms Found.HO.name_mem
#print axioms Found.HO.comprehension
#print axioms Found.HO.all_over_power

found/Hilbert.lean

lean_all/found/Hilbert.lean

/-
  T143. found/Hilbert — Hilbert systems as a point of proof theory, and the
  line Hilbert ⟺ natural deduction (T65's PD, set contexts).

  * Hilbert calculus HD: axioms K, S, ∧/∨ introduction-elimination axioms,
    ex falso; the only rule is modus ponens.
  * Deduction theorem (by induction on Hilbert derivations).
  * HD Γ φ ⟺ PD true Γ φ (both directions).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Minimal

namespace Found

namespace Prp

inductive HD (Γ : Form → Prop) : Form → Prop where
  | hyp {a} : Γ a → HD Γ a
  | k (a b : Form) : HD Γ (.imp a (.imp b a))
  | s (a b c : Form) : HD Γ (.imp (.imp a (.imp b c)) (.imp (.imp a b) (.imp a c)))
  | andI (a b : Form) : HD Γ (.imp a (.imp b (.and a b)))
  | andE1 (a b : Form) : HD Γ (.imp (.and a b) a)
  | andE2 (a b : Form) : HD Γ (.imp (.and a b) b)
  | orI1 (a b : Form) : HD Γ (.imp a (.or a b))
  | orI2 (a b : Form) : HD Γ (.imp b (.or a b))
  | orE (a b c : Form) : HD Γ (.imp (.imp a c) (.imp (.imp b c) (.imp (.or a b) c)))
  | efq (a : Form) : HD Γ (.imp .bot a)
  | mp {a b} : HD Γ (.imp a b) → HD Γ a → HD Γ b

theorem hd_id (Γ : Form → Prop) (a : Form) : HD Γ (.imp a a) :=
  .mp (.mp (.s a (.imp a a) a) (.k a (.imp a a))) (.k a a)

/-- the deduction theorem -/
theorem deduction {Γ : Form → Prop} {a b : Form} (d : HD (ext Γ a) b) : HD Γ (.imp a b) := by
  induction d with
  | hyp g => exact g.elim (fun e => e ▸ hd_id Γ a) (fun g' => .mp (.k _ a) (.hyp g'))
  | k x y => exact .mp (.k _ a) (.k x y)
  | s x y z => exact .mp (.k _ a) (.s x y z)
  | andI x y => exact .mp (.k _ a) (.andI x y)
  | andE1 x y => exact .mp (.k _ a) (.andE1 x y)
  | andE2 x y => exact .mp (.k _ a) (.andE2 x y)
  | orI1 x y => exact .mp (.k _ a) (.orI1 x y)
  | orI2 x y => exact .mp (.k _ a) (.orI2 x y)
  | orE x y z => exact .mp (.k _ a) (.orE x y z)
  | efq x => exact .mp (.k _ a) (.efq x)
  | mp _ _ ihf iha => exact .mp (.mp (.s _ _ _) ihf) iha

/-- Hilbert ⇒ natural deduction -/
theorem hd_pd {Γ : Form → Prop} {φ : Form} (d : HD Γ φ) : PD true Γ φ := by
  induction d with
  | hyp g => exact .hyp g
  | k => exact .impI (.impI (.hyp (Or.inr (Or.inl rfl))))
  | s => exact .impI (.impI (.impI (.impE (.impE (.hyp (Or.inr (Or.inr (Or.inl rfl)))) (.hyp (Or.inl rfl)))
      (.impE (.hyp (Or.inr (Or.inl rfl))) (.hyp (Or.inl rfl))))))
  | andI => exact .impI (.impI (.andI (.hyp (Or.inr (Or.inl rfl))) (.hyp (Or.inl rfl))))
  | andE1 => exact .impI (.andE1 (.hyp (Or.inl rfl)))
  | andE2 => exact .impI (.andE2 (.hyp (Or.inl rfl)))
  | orI1 => exact .impI (.orI1 (.hyp (Or.inl rfl)))
  | orI2 => exact .impI (.orI2 (.hyp (Or.inl rfl)))
  | orE => exact .impI (.impI (.impI (.orE (.hyp (Or.inl rfl))
      (.impE (.hyp (Or.inr (Or.inr (Or.inr (Or.inl rfl))))) (.hyp (Or.inl rfl)))
      (.impE (.hyp (Or.inr (Or.inr (Or.inl rfl)))) (.hyp (Or.inl rfl))))))
  | efq => exact .impI (.botE rfl (.hyp (Or.inl rfl)))
  | mp _ _ ihf iha => exact .impE ihf iha

/-- natural deduction ⇒ Hilbert -/
theorem pd_hd {Γ : Form → Prop} {φ : Form} (d : PD true Γ φ) : HD Γ φ := by
  induction d with
  | hyp g => exact .hyp g
  | botE _ _ ih => exact .mp (.efq _) ih
  | andI _ _ iha ihb => exact .mp (.mp (.andI _ _) iha) ihb
  | andE1 _ ih => exact .mp (.andE1 _ _) ih
  | andE2 _ ih => exact .mp (.andE2 _ _) ih
  | orI1 _ ih => exact .mp (.orI1 _ _) ih
  | orI2 _ ih => exact .mp (.orI2 _ _) ih
  | orE _ _ _ ih iha ihb => exact .mp (.mp (.mp (.orE _ _ _) (deduction iha)) (deduction ihb)) ih
  | impI _ ih => exact deduction ih
  | impE _ _ ihf iha => exact .mp ihf iha

theorem hilbert_iff_nd (Γ : Form → Prop) (φ : Form) : HD Γ φ ↔ PD true Γ φ := ⟨hd_pd, pd_hd⟩

end Prp

end Found

#print axioms Found.Prp.hd_id
#print axioms Found.Prp.deduction
#print axioms Found.Prp.hd_pd
#print axioms Found.Prp.pd_hd
#print axioms Found.Prp.hilbert_iff_nd

found/HitLines.lean

lean_all/found/HitLines.lean

/-
  T127 (higher inductive types), Lean side. found/HitLines — the lines
  from the HITs of work/agda/Hits.agda to the setoid points of the chart:
  * set quotient A/R  <->  the quotient setoid by the equivalence closure of
    R (T87): maps out of it into a setoid are the R-respecting maps;
  * propositional truncation ||A||  <->  the setoid with the total relation:
    maps out of it into a propositional setoid are all maps from A; and the
    Prop-valued truncation Nonempty A with (Nonempty A -> P) <-> (A -> P);
  * pushout  <->  the pushout setoid on B + C (the colimit of the span in
    the category of setoids): maps out of it are cocones, uniquely;
  * suspension  <->  the pushout of 1 <- A -> 1.
  The infinity-categorical colimits of these spans are those of found/IC8.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Setoid

namespace Found

namespace HitLines

open SetoidModel

/-! ## set quotients -/

/-- the equivalence relation generated by X.eqv and R -/
inductive EqClo (X : Setd.{0}) (R : X.car → X.car → Prop) : X.car → X.car → Prop where
  | base {a b : X.car} : X.eqv a b → EqClo X R a b
  | rel {a b : X.car} : R a b → EqClo X R a b
  | symm {a b : X.car} : EqClo X R a b → EqClo X R b a
  | trans {a b c : X.car} : EqClo X R a b → EqClo X R b c → EqClo X R a c

/-- the set quotient X / R as a coarser relation -/
def setQuot (X : Setd.{0}) (R : X.car → X.car → Prop) : Coarser X where
  R := EqClo X R
  refl a := .base (X.refl a)
  symm h := .symm h
  trans h k := .trans h k
  coarser h := .base h

/-- [a] = [b] whenever R a b (the path constructor eq/) -/
theorem eq_quot (X : Setd.{0}) (R : X.car → X.car → Prop) {a b : X.car} (h : R a b) :
    (quot (setQuot X R)).eqv ((proj (setQuot X R)).fn a) ((proj (setQuot X R)).fn b) := .rel h

theorem eqClo_resp {X Y : Setd.{0}} {R : X.car → X.car → Prop} (f : SHom X Y)
    (hf : ∀ {a b}, R a b → Y.eqv (f.fn a) (f.fn b)) : ∀ {a b}, EqClo X R a b → Y.eqv (f.fn a) (f.fn b)
  | _, _, .base h => f.resp h
  | _, _, .rel h => hf h
  | _, _, .symm h => Y.symm (eqClo_resp f hf h)
  | _, _, .trans h k => Y.trans (eqClo_resp f hf h) (eqClo_resp f hf k)

/-- maps out of the set quotient: every R-respecting map factors, uniquely -/
def quotLift {X Y : Setd.{0}} (R : X.car → X.car → Prop) (f : SHom X Y)
    (hf : ∀ {a b}, R a b → Y.eqv (f.fn a) (f.fn b)) : SHom (quot (setQuot X R)) Y :=
  lift (setQuot X R) f (eqClo_resp f hf)

theorem quotLift_comm {X Y : Setd.{0}} (R : X.car → X.car → Prop) (f : SHom X Y)
    (hf : ∀ {a b}, R a b → Y.eqv (f.fn a) (f.fn b)) :
    SetdCat.heq (SetdCat.comp (proj (setQuot X R)) (quotLift R f hf)) f := fun x => Y.refl (f.fn x)

theorem quotLift_unique {X Y : Setd.{0}} (R : X.car → X.car → Prop) (f : SHom X Y)
    (hf : ∀ {a b}, R a b → Y.eqv (f.fn a) (f.fn b)) (g : SHom (quot (setQuot X R)) Y)
    (hg : SetdCat.heq (SetdCat.comp (proj (setQuot X R)) g) f) : SetdCat.heq g (quotLift R f hf) := hg

/-- conversely, a map out of the quotient respects R -/
theorem quot_resp {X Y : Setd.{0}} (R : X.car → X.car → Prop) (g : SHom (quot (setQuot X R)) Y)
    {a b : X.car} (h : R a b) : Y.eqv (g.fn a) (g.fn b) := g.resp (.rel h)

/-! ## propositional truncation -/

/-- a propositional setoid: any two elements are equal -/
def IsPropS (Y : Setd.{0}) : Prop := ∀ a b : Y.car, Y.eqv a b

/-- the truncation: the same carrier with the total relation -/
def truncS (X : Setd.{0}) : Setd.{0} := ⟨X.car, fun _ _ => True, fun _ => trivial, fun _ => trivial, fun _ _ => trivial⟩

theorem truncS_prop (X : Setd.{0}) : IsPropS (truncS X) := fun _ _ => trivial

def truncIn (X : Setd.{0}) : SHom X (truncS X) := ⟨fun x => x, fun _ => trivial⟩

/-- maps from the truncation into a propositional setoid are the maps from X -/
def truncLift {X Y : Setd.{0}} (hY : IsPropS Y) (f : SHom X Y) : SHom (truncS X) Y :=
  ⟨f.fn, fun _ => hY _ _⟩

theorem truncLift_comm {X Y : Setd.{0}} (hY : IsPropS Y) (f : SHom X Y) :
    SetdCat.heq (SetdCat.comp (truncIn X) (truncLift hY f)) f := fun x => Y.refl (f.fn x)

theorem truncLift_unique {X Y : Setd.{0}} (hY : IsPropS Y) (f : SHom X Y) (g : SHom (truncS X) Y) :
    SetdCat.heq g (truncLift hY f) := fun _ => hY _ _

/-- the Prop-valued truncation: maps into a proposition -/
theorem nonempty_univ (A : Type) (P : Prop) : (Nonempty A → P) ↔ (A → P) :=
  ⟨fun h a => h ⟨a⟩, fun h n => match n with | ⟨a⟩ => h a⟩

/-! ## pushouts -/

section push

variable {A B C : Setd.{0}} (f : SHom A B) (g : SHom A C)

/-- the relation of the pushout on B + C -/
inductive PoRel : Sum B.car C.car → Sum B.car C.car → Prop where
  | inl {b b' : B.car} : B.eqv b b' → PoRel (.inl b) (.inl b')
  | inr {c c' : C.car} : C.eqv c c' → PoRel (.inr c) (.inr c')
  | push (a : A.car) : PoRel (.inl (f.fn a)) (.inr (g.fn a))
  | symm {x y : Sum B.car C.car} : PoRel x y → PoRel y x
  | trans {x y z : Sum B.car C.car} : PoRel x y → PoRel y z → PoRel x z

theorem poRel_refl : ∀ x : Sum B.car C.car, PoRel f g x x
  | .inl b => .inl (B.refl b)
  | .inr c => .inr (C.refl c)

/-- the pushout setoid -/
def pushoutS : Setd.{0} := ⟨Sum B.car C.car, PoRel f g, poRel_refl f g, fun h => .symm h, fun h k => .trans h k⟩

def inlS : SHom B (pushoutS f g) := ⟨Sum.inl, fun h => .inl h⟩
def inrS : SHom C (pushoutS f g) := ⟨Sum.inr, fun h => .inr h⟩

/-- the path constructor push -/
theorem pushS (a : A.car) : (pushoutS f g).eqv ((inlS f g).fn (f.fn a)) ((inrS f g).fn (g.fn a)) := .push a

variable {D : Setd.{0}} (h : SHom B D) (k : SHom C D) (hk : ∀ a, D.eqv (h.fn (f.fn a)) (k.fn (g.fn a)))

def coFn : Sum B.car C.car → D.car
  | .inl b => h.fn b
  | .inr c => k.fn c

include hk in
theorem coFn_resp : ∀ {x y : Sum B.car C.car}, PoRel f g x y → D.eqv (coFn h k x) (coFn h k y)
  | _, _, .inl e => h.resp e
  | _, _, .inr e => k.resp e
  | _, _, .push a => hk a
  | _, _, .symm e => D.symm (coFn_resp e)
  | _, _, .trans e e' => D.trans (coFn_resp e) (coFn_resp e')

/-- a cocone gives a map out of the pushout -/
def pushLift : SHom (pushoutS f g) D := ⟨coFn h k, coFn_resp f g h k hk⟩

theorem pushLift_inl : SetdCat.heq (SetdCat.comp (inlS f g) (pushLift f g h k hk)) h := fun b => D.refl (h.fn b)
theorem pushLift_inr : SetdCat.heq (SetdCat.comp (inrS f g) (pushLift f g h k hk)) k := fun c => D.refl (k.fn c)

/-- uniqueness: a map agreeing with the cocone on both sides is the induced one -/
theorem pushLift_unique (u : SHom (pushoutS f g) D) (hu1 : SetdCat.heq (SetdCat.comp (inlS f g) u) h)
    (hu2 : SetdCat.heq (SetdCat.comp (inrS f g) u) k) : SetdCat.heq u (pushLift f g h k hk)
  | .inl b => hu1 b
  | .inr c => hu2 c

end push

/-- a map out of the pushout restricts to a cocone -/
theorem toCocone {A B C D : Setd.{0}} (f : SHom A B) (g : SHom A C) (u : SHom (pushoutS f g) D) (a : A.car) :
    D.eqv (u.fn (.inl (f.fn a))) (u.fn (.inr (g.fn a))) := u.resp (.push a)

/-! ## suspension -/

def unitS : Setd.{0} := ⟨Unit, fun _ _ => True, fun _ => trivial, fun _ => trivial, fun _ _ => trivial⟩

def toUnit (A : Setd.{0}) : SHom A unitS := ⟨fun _ => (), fun _ => trivial⟩

/-- the suspension of a setoid: the pushout of 1 <- A -> 1 -/
def suspS (A : Setd.{0}) : Setd.{0} := pushoutS (toUnit A) (toUnit A)

def northS (A : Setd.{0}) : (suspS A).car := .inl ()
def southS (A : Setd.{0}) : (suspS A).car := .inr ()

/-- merid: every point of A joins north to south -/
theorem meridS (A : Setd.{0}) (a : A.car) : (suspS A).eqv (northS A) (southS A) := .push a

end HitLines

end Found

#print axioms Found.HitLines.setQuot
#print axioms Found.HitLines.eq_quot
#print axioms Found.HitLines.eqClo_resp
#print axioms Found.HitLines.quotLift_unique
#print axioms Found.HitLines.quot_resp
#print axioms Found.HitLines.truncLift_unique
#print axioms Found.HitLines.nonempty_univ
#print axioms Found.HitLines.coFn_resp
#print axioms Found.HitLines.pushLift_inl
#print axioms Found.HitLines.pushLift_unique
#print axioms Found.HitLines.toCocone
#print axioms Found.HitLines.meridS

found/HoTTLines.lean

lean_all/found/HoTTLines.lean

/-
  T127. found/HoTTLines — cubical type theory / HoTT as a value of the
  interval coordinate (E, I, Φ) (T127, OrtonPitts: Cubical Agda's I is such
  a value), and its lines to the other points:
  * paths ↔ equality: interval paths in A coincide with Lean equality exactly
    when every map I → A is constant on the endpoints (connectedness of I
    relative to A); equality always gives a path (the A = A chain, T60);
    the two-point interval is not connected (OrtonPitts.two_not_connected),
    so there paths are strictly coarser.
  * setoids / h-sets: at the CIC point every type is an h-set (InfN.collapse,
    T196); the setoid model (T87) lives there.
  * univalence: holds at the Cubical Agda point (work/agda/Univalence.agda:
    ua, uaβ, checked by agda --safe) and FAILS at the CIC point: with proof
    irrelevance, idtoeqv cannot hit the swap of Bool (not_univalent).
  Shulman 2019 (every ∞-topos models HoTT) is the cited instance with a
  Grothendieck E and classical metatheory.

  Lean 4 core only, hand-written proofs, no axioms.
  CIC dependence: proof irrelevance (not_univalent: any e : Bool = Bool equals rfl).
-/
import found.OrtonPitts

namespace Found

namespace HoTTL

open OP

theorem path_eq_iff_connected (J : Interval) (A : Type) :
    (∀ f : J.I → A, f J.i0 = f J.i1) ↔ (∀ a b : A, Path J A a b → a = b) :=
  ⟨fun h _ _ q => q.src.symm.trans ((h q.p).trans q.tgt),
   fun h f => h _ _ ⟨f, rfl, rfl⟩⟩

def eq_to_path (J : Interval) {A : Type} {a b : A} (e : a = b) : Path J A a b := ofEq J e

structure Equiv' (A B : Type) where
  to : A → B
  inv : B → A
  li : ∀ a, inv (to a) = a
  ri : ∀ b, to (inv b) = b

def idtoeqv {A B : Type} (e : A = B) : Equiv' A B := by
  cases e
  exact ⟨fun a => a, fun a => a, fun _ => rfl, fun _ => rfl⟩

def notB : Bool → Bool
  | true => false
  | false => true

theorem notB_notB : (b : Bool) → notB (notB b) = b
  | true => rfl
  | false => rfl

def swapEqv : Equiv' Bool Bool := ⟨notB, notB, notB_notB, notB_notB⟩

/-- univalence (surjectivity of idtoeqv at Bool) fails in CIC -/
theorem not_univalent : ¬ ∃ e : Bool = Bool, idtoeqv e = swapEqv := fun ⟨e, h⟩ => by
  have e1 : e = rfl := rfl
  rw [e1] at h
  have : (idtoeqv (rfl : Bool = Bool)).to true = swapEqv.to true := congrArg (fun q => q.to true) h
  exact Bool.noConfusion this

end HoTTL

end Found

#print axioms Found.HoTTL.path_eq_iff_connected
#print axioms Found.HoTTL.not_univalent

found/Homotopy.lean

lean_all/found/Homotopy.lean

/-
  T135. found/Homotopy — homotopy theory as a point: the homotopy category
  is a localization; on objects it is the core's localization (T59, op 7)
  of the object setoid at "connected by a weak equivalence".
  * π₀ / Ho on objects: Loc(Ob, W) with the universal property (maps of
    objects inverting W factor uniquely).
  * For the trivial model structure (T133) W-related objects are
    isomorphic, so Ho = C on objects.
  The full Ho(C) on morphisms (zig-zags) and Quillen equivalences are the
  known continuation (cited).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.ModelCat
import found.Core

namespace Found

namespace Htpy

open Core

universe u v

variable {C : ECat.{u, v}} (M : Model.ModelStr C)

/-- objects as a discrete setoid -/
def obS (C : ECat.{u, v}) : Setd.{u} := ⟨C.Ob, Eq, fun _ => rfl, fun h => h.symm, fun h k => h.trans k⟩

def WRel (a b : C.Ob) : Prop := ∃ f : C.Hom a b, M.W f

/-- π₀ = objects localized at weak equivalences -/
def pi0 : Setd.{u} := loc (obS C) (WRel M)

theorem we_identified {a b : C.Ob} (f : C.Hom a b) (hf : M.W f) :
    (pi0 M).eqv ((locMap (obS C) (WRel M)).fn a) ((locMap (obS C) (WRel M)).fn b) :=
  loc_inverts (X := obS C) (WRel M) ⟨f, hf⟩

/-- universal property: an invariant of objects that is constant along weak
  equivalences factors through π₀ -/
def factor {Y : Setd.{u}} (F : SHom (obS C) Y) (hF : ∀ {a b}, WRel M a b → Y.eqv (F.fn a) (F.fn b)) :
    SHom (pi0 M) Y := locLift (X := obS C) (WRel M) F hF

theorem factor_comm {Y : Setd.{u}} (F : SHom (obS C) Y) (hF : ∀ {a b}, WRel M a b → Y.eqv (F.fn a) (F.fn b)) :
    SetdCat.heq (SetdCat.comp (locMap (obS C) (WRel M)) (factor M F hF)) F :=
  locLift_comm (X := obS C) (WRel M) F hF

/-- in the trivial model structure, π₀-identified objects are isomorphic -/
theorem trivial_iso {a b : (obS C).car} (h : Gen (obS C) (WRel (Model.trivialModel C)) a b) :
    ∃ f : C.Hom a b, Model.IsIso f := by
  induction h with
  | base e => cases e; exact ⟨C.id _, Model.iso_id _⟩
  | rel r => exact r
  | symm _ ih => exact match ih with
    | ⟨f, ⟨g, h1, h2⟩⟩ => ⟨g, ⟨f, h2, h1⟩⟩
  | trans _ _ ih₁ ih₂ => exact match ih₁, ih₂ with
    | ⟨f, hf⟩, ⟨g, hg⟩ => ⟨C.comp f g, Model.iso_comp hf hg⟩

end Htpy

end Found

#print axioms Found.Htpy.we_identified
#print axioms Found.Htpy.factor_comm
#print axioms Found.Htpy.trivial_iso

found/IC1.lean

lean_all/found/IC1.lean

/-
  T153 ((infinity,1)-categories), part 1. found/IC1 — horns in every
  dimension: the face maps of the simplex category, the simplicial identity
  d_i d_(j+1) = d_j d_i (i <= j), horns Lambda^(n+2)_k as compatible families
  of faces, fillers; quasi-categories (inner horns fill) and Kan complexes
  (all horns fill); every Kan complex is a quasi-category, and a
  quasi-category satisfies the dimension-2 condition Inner2 of found/InfCat.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.InfCat

namespace Found

namespace IC

open Simp

/-- gtb b a = (a < b), by recursion on b -/
def gtb : Nat → Nat → Bool
  | 0, _ => false
  | _ + 1, 0 => true
  | b + 1, a + 1 => gtb b a

theorem gtb_t : (b a : Nat) → gtb b a = true → a < b
  | 0, _, h => Bool.noConfusion h
  | b + 1, 0, _ => Nat.succ_pos b
  | b + 1, a + 1, h => Nat.succ_lt_succ (gtb_t b a h)

theorem gtb_f : (b a : Nat) → gtb b a = false → b ≤ a
  | 0, a, _ => Nat.zero_le a
  | _ + 1, 0, h => Bool.noConfusion h
  | b + 1, a + 1, h => Nat.succ_le_succ (gtb_f b a h)

theorem lt_gtb : (b a : Nat) → a < b → gtb b a = true
  | 0, a, h => absurd h (Nat.not_lt_zero a)
  | _ + 1, 0, _ => rfl
  | b + 1, a + 1, h => lt_gtb b a (Nat.lt_of_succ_lt_succ h)

theorem le_gtb : (b a : Nat) → b ≤ a → gtb b a = false
  | 0, _, _ => rfl
  | b + 1, 0, h => absurd h (Nat.not_succ_le_zero b)
  | b + 1, a + 1, h => le_gtb b a (Nat.le_of_succ_le_succ h)

/-- the i-th coface [n] -> [n+1] skips i -/
def sk (i j : Nat) : Nat := cond (gtb i j) j (j + 1)

theorem sk_lt {i j : Nat} (h : j < i) : sk i j = j :=
  congrArg (fun b => cond b j (j + 1)) (lt_gtb i j h)

theorem sk_ge {i j : Nat} (h : i ≤ j) : sk i j = j + 1 :=
  congrArg (fun b => cond b j (j + 1)) (le_gtb i j h)

theorem self_le_sk (i j : Nat) : j ≤ sk i j :=
  match Nat.lt_or_ge j i with
  | .inl h => by rw [sk_lt h]; exact Nat.le_refl j
  | .inr h => by rw [sk_ge h]; exact Nat.le_succ j

theorem sk_le_succ (i j : Nat) : sk i j ≤ j + 1 :=
  match Nat.lt_or_ge j i with
  | .inl h => by rw [sk_lt h]; exact Nat.le_succ j
  | .inr h => by rw [sk_ge h]; exact Nat.le_refl _

theorem sk_mono (i j : Nat) : sk i j ≤ sk i (j + 1) :=
  match Nat.lt_or_ge j i with
  | .inl h => by rw [sk_lt h]; exact Nat.le_trans (Nat.le_succ j) (self_le_sk i (j + 1))
  | .inr h => by rw [sk_ge h, sk_ge (Nat.le_trans h (Nat.le_succ j))]; exact Nat.le_succ _

def face (n i : Nat) : DHom n (n + 1) :=
  ⟨sk i, fun j _ => sk_mono i j, fun j hj => Nat.le_trans (sk_le_succ i j) (Nat.succ_le_succ hj)⟩

/-- the cosimplicial identity -/
theorem sk_sk {i j : Nat} (hij : i ≤ j) (p : Nat) : sk (j + 1) (sk i p) = sk i (sk j p) :=
  match Nat.lt_or_ge p i with
  | .inl h1 => by
    have h2 : p < j := Nat.lt_of_lt_of_le h1 hij
    rw [sk_lt h1, sk_lt (Nat.lt_trans h2 (Nat.lt_succ_self j)), sk_lt h2, sk_lt h1]
  | .inr h1 => match Nat.lt_or_ge p j with
    | .inl h2 => by
      rw [sk_ge h1, sk_lt (Nat.succ_lt_succ h2), sk_lt h2, sk_ge h1]
    | .inr h2 => by
      rw [sk_ge h1, sk_ge (Nat.succ_le_succ h2), sk_ge h2, sk_ge (Nat.le_trans h1 (Nat.le_succ p))]

theorem face_face (n : Nat) {i j : Nat} (hij : i ≤ j) :
    Delta.heq (Delta.comp (a := (n : Nat)) (b := (n + 1 : Nat)) (c := (n + 2 : Nat)) (face n i) (face (n + 1) (j + 1)))
      (Delta.comp (a := (n : Nat)) (b := (n + 1 : Nat)) (c := (n + 2 : Nat)) (face n j) (face (n + 1) i)) :=
  fun p _ => sk_sk hij p

universe w

/-- simplicial sets (simplices in universe w) -/
abbrev SSet := Psh.{0, 0, w} Delta

/-- the face operator d_i : X_(n+1) -> X_n -/
def dd (X : SSet) {n : Nat} (i : Nat) (x : (X.obj (n + 1 : Nat)).car) : (X.obj (n : Nat)).car :=
  X.map (a := (n : Nat)) (b := (n + 1 : Nat)) (face n i) x

/-- the simplicial identity -/
theorem dd_dd (X : SSet) {n i j : Nat} (hij : i ≤ j) (σ : (X.obj (n + 2 : Nat)).car) :
    (X.obj (n : Nat)).eqv (dd X i (dd X (j + 1) σ)) (dd X j (dd X i σ)) :=
  (X.obj (n : Nat)).trans ((X.obj (n : Nat)).symm (X.map_comp (face n i) (face (n + 1) (j + 1)) σ))
    ((X.obj (n : Nat)).trans (X.map_resp σ (face_face n hij)) (X.map_comp (face n j) (face (n + 1) i) σ))

/-- a horn Lambda^(n+2)_k: faces x_i (i <= n+2, i /= k) agreeing on common faces -/
structure Horn (X : SSet) (n k : Nat) where
  x : Nat → (X.obj (n + 1 : Nat)).car
  compat : ∀ i j, i ≤ j → j ≤ n + 1 → i ≠ k → j + 1 ≠ k →
    (X.obj (n : Nat)).eqv (dd X i (x (j + 1))) (dd X j (x i))

/-- σ fills the horn -/
def Fills {X : SSet} {n k : Nat} (h : Horn X n k) (σ : (X.obj (n + 2 : Nat)).car) : Prop :=
  ∀ i, i ≤ n + 2 → i ≠ k → (X.obj (n + 1 : Nat)).eqv (dd X i σ) (h.x i)

/-- the horn of a simplex -/
def hornOf {X : SSet} {n : Nat} (k : Nat) (σ : (X.obj (n + 2 : Nat)).car) : Horn X n k :=
  ⟨fun i => dd X i σ, fun _ _ hij _ _ _ => dd_dd X hij σ⟩

theorem hornOf_fills {X : SSet} {n : Nat} (k : Nat) (σ : (X.obj (n + 2 : Nat)).car) : Fills (hornOf k σ) σ :=
  fun _ _ _ => (X.obj (n + 1 : Nat)).refl _

/-- quasi-categories: inner horns fill -/
def Quasi (X : SSet) : Prop := ∀ n k, 0 < k → k < n + 2 → ∀ h : Horn X n k, ∃ σ, Fills h σ

/-- Kan complexes: all horns fill -/
def Kan (X : SSet) : Prop := ∀ n k, k ≤ n + 2 → ∀ h : Horn X n k, ∃ σ, Fills h σ

theorem kan_quasi {X : SSet} (hK : Kan X) : Quasi X := fun n k _ hk h => hK n k (Nat.le_of_lt hk) h

/-- the dimension-2 inner horn from two composable edges -/
def horn2 {X : SSet} (a b : (X.obj (1 : Nat)).car)
    (e : (X.obj InfCat.n0).eqv (X.map (a := InfCat.n0) (b := InfCat.n1) InfCat.v1 a)
      (X.map (a := InfCat.n0) (b := InfCat.n1) InfCat.v0 b)) : Horn X 0 1 where
  x i := match i with
    | 0 => b
    | 1 => b
    | _ + 2 => a
  compat i j hij hj hi hj1 := match i, j, hij, hj, hi, hj1 with
    | _, 0, _, _, _, hj1 => absurd rfl hj1
    | 0, 1, _, _, _, _ =>
      (X.obj (0 : Nat)).trans (X.map_resp (f := face 0 0) (g := InfCat.v1) a (fun p hp => match p, hp with
          | 0, _ => rfl
          | q + 1, h => absurd h (Nat.not_succ_le_zero q)))
        ((X.obj (0 : Nat)).trans e (X.map_resp (f := InfCat.v0) (g := face 0 1) b (fun p hp => match p, hp with
          | 0, _ => rfl
          | q + 1, h => absurd h (Nat.not_succ_le_zero q))))
    | 1, 1, _, _, hi, _ => absurd rfl hi
    | _ + 2, 1, h, _, _, _ => absurd (Nat.le_of_succ_le_succ h) (Nat.not_succ_le_zero _)
    | _, _ + 2, _, h, _, _ => absurd (Nat.le_of_succ_le_succ h) (Nat.not_succ_le_zero _)

/-- quasi-categories satisfy Inner2 -/
theorem quasi_inner2 {X : SSet} (hQ : Quasi X) : InfCat.Inner2 X := fun a b e =>
  match hQ 0 1 (Nat.succ_pos 0) (Nat.lt_succ_self 1) (horn2 a b e) with
  | ⟨σ, hσ⟩ => ⟨σ,
      (X.obj (1 : Nat)).trans (X.map_resp (f := InfCat.d2) (g := face 1 2) σ (fun p hp => match p, hp with
          | 0, _ => rfl
          | 1, _ => rfl
          | q + 2, h => absurd (Nat.le_of_succ_le_succ h) (Nat.not_succ_le_zero q)))
        (hσ 2 (Nat.le_refl 2) (fun h => Nat.noConfusion h (fun h' => Nat.noConfusion h'))),
      (X.obj (1 : Nat)).trans (X.map_resp (f := InfCat.d0) (g := face 1 0) σ (fun _ _ => rfl))
        (hσ 0 (Nat.zero_le 2) (fun h => Nat.noConfusion h))⟩

end IC

end Found

#print axioms Found.IC.sk_sk
#print axioms Found.IC.dd_dd
#print axioms Found.IC.hornOf_fills
#print axioms Found.IC.kan_quasi
#print axioms Found.IC.quasi_inner2

found/IC10.lean

lean_all/found/IC10.lean

/-
  T153 ((infinity,1)-categories), part 10. found/IC10 — the Eilenberg–Zilber
  lemma for (setoid) simplicial sets, inside the classical point (excluded
  middle and choice as hypothesis variables).  A simplex is degenerate when
  it is a pullback along a proper surjection; every simplex is a pullback of
  a non-degenerate one along a surjection, and the surjection and the
  non-degenerate simplex are unique.  Ingredients: monotone sections of
  surjections, and the one-step factorization φ = φ ∘ δ_(i+1) ∘ σ_i when
  φ(i) = φ(i+1).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.IC9
import found.ClassicalPoint

namespace Found

namespace IC

open Simp

universe w

/-! ## surjections and their sections -/

def Surj {m k : Nat} (η : DHom m k) : Prop := ∀ j, j ≤ k → ∃ a, a ≤ m ∧ η.f a = j

theorem dmono {m k : Nat} (η : DHom m k) {a b : Nat} (h : a ≤ b) (hb : b ≤ m) : η.f a ≤ η.f b :=
  Simp.Delta.mono_of_succ η h hb

/-- preimages of a monotone map are ordered like the values -/
theorem pre_lt {m k : Nat} (η : DHom m k) {a a' : Nat} (ha : a ≤ m) (h : η.f a < η.f a') : a < a' :=
  match Nat.lt_or_ge a a' with
  | .inl h' => h'
  | .inr h' => absurd (dmono η h' ha) (Nat.not_le_of_gt h)

theorem surj_id (m : Nat) : Surj (dId m) := fun j hj => ⟨j, hj, rfl⟩

theorem surj_comp {m k l : Nat} {η : DHom m k} {θ : DHom k l} (h1 : Surj η) (h2 : Surj θ) : Surj (dComp η θ) :=
  fun j hj => match h2 j hj with
  | ⟨b, hb, eb⟩ => match h1 b hb with
    | ⟨a, ha, ea⟩ => ⟨a, ha, by show θ.f (η.f a) = j; rw [ea, eb]⟩

variable (Kc : CP.Classical')

/-- a chosen preimage -/
def pre {m k : Nat} (η : DHom m k) (hs : Surj η) (j : Nat) : Nat :=
  Kc.ch (fun a => j ≤ k → a ≤ m ∧ η.f a = j) (match Kc.lem (j ≤ k) with
    | .inl h => match hs j h with | ⟨a, ha, e⟩ => ⟨a, fun _ => ⟨ha, e⟩⟩
    | .inr h => ⟨0, fun h' => absurd h' h⟩)

theorem pre_spec {m k : Nat} (η : DHom m k) (hs : Surj η) {j : Nat} (hj : j ≤ k) :
    pre Kc η hs j ≤ m ∧ η.f (pre Kc η hs j) = j :=
  Kc.ch_spec (fun a => j ≤ k → a ≤ m ∧ η.f a = j) _ hj

/-- the section through i: η(i) ↦ i, other values ↦ chosen preimages -/
def secf {m k : Nat} (η : DHom m k) (hs : Surj η) (i j : Nat) : Nat :=
  cond (gtb (η.f i) j) (pre Kc η hs j) (cond (gtb j (η.f i)) (pre Kc η hs j) i)

theorem secf_spec {m k : Nat} (η : DHom m k) (hs : Surj η) {i : Nat} (hi : i ≤ m) {j : Nat} (hj : j ≤ k) :
    secf Kc η hs i j ≤ m ∧ η.f (secf Kc η hs i j) = j :=
  match Nat.lt_or_ge j (η.f i) with
  | .inl h => by
    show cond (gtb (η.f i) j) _ _ ≤ m ∧ η.f (cond (gtb (η.f i) j) _ _) = j
    rw [lt_gtb _ _ h]; exact pre_spec Kc η hs hj
  | .inr h => match Nat.lt_or_ge (η.f i) j with
    | .inl h' => by
      show cond (gtb (η.f i) j) _ (cond (gtb j (η.f i)) _ _) ≤ m ∧
        η.f (cond (gtb (η.f i) j) _ (cond (gtb j (η.f i)) _ _)) = j
      rw [le_gtb _ _ h, lt_gtb _ _ h']; exact pre_spec Kc η hs hj
    | .inr h' => by
      show cond (gtb (η.f i) j) _ (cond (gtb j (η.f i)) _ _) ≤ m ∧
        η.f (cond (gtb (η.f i) j) _ (cond (gtb j (η.f i)) _ _)) = j
      rw [le_gtb _ _ h, le_gtb _ _ h']
      exact ⟨hi, Nat.le_antisymm h h'⟩

theorem secf_at {m k : Nat} (η : DHom m k) (hs : Surj η) (i : Nat) : secf Kc η hs i (η.f i) = i := by
  show cond (gtb (η.f i) (η.f i)) _ (cond (gtb (η.f i) (η.f i)) _ _) = i
  rw [le_gtb _ _ (Nat.le_refl _)]
  rfl

/-- the section of a surjection through i, a map [k] -> [m] -/
def sec {m k : Nat} (η : DHom m k) (hs : Surj η) (i : Nat) (hi : i ≤ m) : DHom k m :=
  ⟨secf Kc η hs i, fun j hj => Nat.le_of_lt (pre_lt η (secf_spec Kc η hs hi (Nat.le_of_lt hj)).1
      (by rw [(secf_spec Kc η hs hi (Nat.le_of_lt hj)).2, (secf_spec Kc η hs hi hj).2]; exact Nat.lt_succ_self j)),
    fun j hj => (secf_spec Kc η hs hi hj).1⟩

/-! ## the one-step factorization -/

theorem succ_pred_of {i j : Nat} (h : i + 1 ≤ j) : Nat.pred j + 1 = j :=
  match j, h with
  | _ + 1, _ => rfl

/-- if φ(i) = φ(i+1) then φ = φ ∘ δ_(i+1) ∘ σ_i on [k+1] -/
theorem factor_step {k k' : Nat} (φ : DHom (k + 1) k') {i : Nat} (hi : i ≤ k) (e : φ.f i = φ.f (i + 1)) :
    ∀ j, j ≤ k + 1 → φ.f j = (dComp (degen k i hi) (dComp (face k (i + 1)) φ)).f j := fun j _ =>
  match Nat.lt_or_ge j (i + 1) with
  | .inl h => by
    show φ.f j = φ.f (sk (i + 1) (dg i j))
    rw [dg_lt h, sk_lt h]
  | .inr h => match Nat.lt_or_ge j (i + 2) with
    | .inl h2 => by
      have ej : j = i + 1 := Nat.le_antisymm (Nat.le_of_lt_succ h2) h
      show φ.f j = φ.f (sk (i + 1) (dg i j))
      rw [dg_ge h, ej]
      show φ.f (i + 1) = φ.f (sk (i + 1) i)
      rw [sk_lt (Nat.lt_succ_self i)]; exact e.symm
    | .inr h2 => by
      show φ.f j = φ.f (sk (i + 1) (dg i j))
      rw [dg_ge h, sk_ge (Nat.le_of_succ_le_succ (Nat.le_trans h2 (Nat.le_of_eq (succ_pred_of h).symm))),
        succ_pred_of h]

theorem surj_degen (k i : Nat) (hi : i ≤ k) : Surj (degen k i hi) := fun j hj =>
  match Nat.lt_or_ge j (i + 1) with
  | .inl h => ⟨j, Nat.le_succ_of_le hj, dg_lt h⟩
  | .inr h => ⟨j + 1, Nat.succ_le_succ hj, (dg_ge (Nat.le_succ_of_le h)).trans rfl⟩

/-! ## strictly monotone maps -/

def StrictM {k k' : Nat} (φ : DHom k k') : Prop := ∀ i, i < k → φ.f i < φ.f (i + 1)

theorem strict_ge {k k' : Nat} {φ : DHom k k'} (h : StrictM φ) : ∀ i, i ≤ k → i ≤ φ.f i
  | 0, _ => Nat.zero_le _
  | i + 1, hi => Nat.succ_le_of_lt (Nat.lt_of_le_of_lt (strict_ge h i (Nat.le_of_succ_le hi)) (h i hi))

theorem strict_dim {k k' : Nat} {φ : DHom k k'} (h : StrictM φ) : k ≤ k' :=
  Nat.le_trans (strict_ge h k (Nat.le_refl k)) (φ.bound k (Nat.le_refl k))

theorem strict_le_aux {k : Nat} {φ : DHom k k} (h : StrictM φ) : ∀ d i, i + d = k → φ.f i ≤ i
  | 0, i, e => e ▸ φ.bound i (Nat.le_of_eq e)
  | d + 1, i, e =>
    have e' : (i + 1) + d = k := (Nat.succ_add i d).trans e
    Nat.le_of_lt_succ (Nat.lt_of_lt_of_le (h i (by rw [← e]; exact Nat.succ_le_succ (Nat.le_add_right i d)))
      (strict_le_aux h d (i + 1) e'))

theorem strict_id {k : Nat} {φ : DHom k k} (h : StrictM φ) : ∀ i, i ≤ k → φ.f i = i := fun i hi =>
  match le_split hi with
  | ⟨d, e⟩ => Nat.le_antisymm (strict_le_aux h d i e) (strict_ge h i hi)
where
  le_split {i k : Nat} (h : i ≤ k) : ∃ d, i + d = k := by
    induction h with
    | refl => exact ⟨0, rfl⟩
    | step _ ih => match ih with | ⟨d, e⟩ => exact ⟨d + 1, congrArg Nat.succ e⟩

/-! ## degenerate and non-degenerate simplices -/

variable {X : SSet.{w}}

def Deg {m : Nat} (τ : (X.obj (m : Nat)).car) : Prop :=
  ∃ (k : Nat) (η : DHom m k) (τ' : (X.obj (k : Nat)).car), Surj η ∧ k < m ∧ (X.obj (m : Nat)).eqv τ (mp X η τ')

def NonDeg {m : Nat} (τ : (X.obj (m : Nat)).car) : Prop := ¬ Deg τ

theorem nondeg_eqv {m : Nat} {τ τ' : (X.obj (m : Nat)).car} (e : (X.obj (m : Nat)).eqv τ τ') (h : NonDeg τ) :
    NonDeg τ' := fun ⟨k, η, σ, hs, hk, e'⟩ => h ⟨k, η, σ, hs, hk, (X.obj _).trans e e'⟩

include Kc in
/-- Eilenberg–Zilber, existence -/
theorem ez_exists : ∀ (N m : Nat), m ≤ N → ∀ τ : (X.obj (m : Nat)).car,
    ∃ (k : Nat) (η : DHom m k) (τ0 : (X.obj (k : Nat)).car), Surj η ∧ NonDeg τ0 ∧ (X.obj (m : Nat)).eqv τ (mp X η τ0)
  | 0, m, hm, τ => match Kc.lem (Deg τ) with
    | .inl ⟨_, _, _, _, hk, _⟩ => absurd (Nat.lt_of_lt_of_le hk hm) (Nat.not_lt_zero _)
    | .inr h => ⟨m, dId m, τ, surj_id m, h, (X.obj _).symm (mp_idl X (dId m) (fun _ _ => rfl) τ)⟩
  | N + 1, m, hm, τ => match Kc.lem (Deg τ) with
    | .inl ⟨k, η, τ', hs, hk, e⟩ =>
      match ez_exists N k (Nat.le_of_lt_succ (Nat.lt_of_lt_of_le hk hm)) τ' with
      | ⟨k0, η', τ0, hs', hn, e'⟩ => ⟨k0, dComp η η', τ0, surj_comp hs hs', hn,
          (X.obj _).trans e ((X.obj _).trans (mp_eqv X η e') (mp_comp X η η' τ0))⟩
    | .inr h => ⟨m, dId m, τ, surj_id m, h, (X.obj _).symm (mp_idl X (dId m) (fun _ _ => rfl) τ)⟩

include Kc in
/-- a non-degenerate simplex pulled back from another one along φ: φ is strictly monotone -/
theorem nondeg_strict {k k' : Nat} {τ0 : (X.obj (k : Nat)).car} (hn : NonDeg τ0) (φ : DHom k k')
    (τ1 : (X.obj (k' : Nat)).car) (e : (X.obj (k : Nat)).eqv τ0 (mp X φ τ1)) : StrictM φ := fun i hi =>
  match Kc.lem (φ.f i < φ.f (i + 1)) with
  | .inl h => h
  | .inr h => absurd e (fun e => match k, φ, τ0, hn, hi, e with
    | k1 + 1, φ, _, hn, hi, e =>
      have eq : φ.f i = φ.f (i + 1) := Nat.le_antisymm (dmono φ (Nat.le_succ i) hi) (Nat.ge_of_not_lt h)
      hn ⟨k1, degen k1 i (Nat.le_of_lt_succ hi), mp X (dComp (face k1 (i + 1)) φ) τ1,
        surj_degen k1 i (Nat.le_of_lt_succ hi), Nat.lt_succ_self k1,
        (X.obj _).trans e ((X.obj _).trans (mp_resp X (factor_step φ (Nat.le_of_lt_succ hi) eq) τ1)
          ((X.obj _).symm (mp_comp X _ _ τ1)))⟩)

/-- pulling back along a surjection and then a section is the identity -/
theorem sec_pull {m k : Nat} (η : DHom m k) (hs : Surj η) {i : Nat} (hi : i ≤ m) (x : (X.obj (k : Nat)).car) :
    (X.obj (k : Nat)).eqv (mp X (sec Kc η hs i hi) (mp X η x)) x :=
  (X.obj _).trans (mp_comp X _ _ x) (mp_idl X _ (fun _ hj => (secf_spec Kc η hs hi hj).2) x)

include Kc in
/-- Eilenberg–Zilber, uniqueness of the dimension -/
theorem ez_dim {m k k' : Nat} {η : DHom m k} {η' : DHom m k'} (hs : Surj η) (hs' : Surj η')
    {τ0 : (X.obj (k : Nat)).car} {τ0' : (X.obj (k' : Nat)).car} (hn : NonDeg τ0) (hn' : NonDeg τ0')
    (e : (X.obj (m : Nat)).eqv (mp X η τ0) (mp X η' τ0')) : k = k' :=
  Nat.le_antisymm
    (strict_dim (nondeg_strict Kc hn (dComp (sec Kc η hs 0 (Nat.zero_le m)) η') τ0'
      ((X.obj _).trans ((X.obj _).symm (sec_pull Kc η hs (Nat.zero_le m) τ0))
        ((X.obj _).trans (mp_eqv X _ e) (mp_comp X _ _ τ0')))))
    (strict_dim (nondeg_strict Kc hn' (dComp (sec Kc η' hs' 0 (Nat.zero_le m)) η) τ0
      ((X.obj _).trans ((X.obj _).symm (sec_pull Kc η' hs' (Nat.zero_le m) τ0'))
        ((X.obj _).trans (mp_eqv X _ ((X.obj _).symm e)) (mp_comp X _ _ τ0)))))

include Kc in
/-- Eilenberg–Zilber, uniqueness of the surjection and of the non-degenerate simplex -/
theorem ez_uniq {m k : Nat} {η η' : DHom m k} (hs : Surj η)
    {τ0 τ0' : (X.obj (k : Nat)).car} (hn : NonDeg τ0)
    (e : (X.obj (m : Nat)).eqv (mp X η τ0) (mp X η' τ0')) :
    (∀ i, i ≤ m → η.f i = η'.f i) ∧ (X.obj (k : Nat)).eqv τ0 τ0' :=
  have key : ∀ i (hi : i ≤ m), (X.obj (k : Nat)).eqv τ0 (mp X (dComp (sec Kc η hs i hi) η') τ0') ∧
      ∀ j, j ≤ k → (dComp (sec Kc η hs i hi) η').f j = j := fun i hi =>
    have e1 : (X.obj (k : Nat)).eqv τ0 (mp X (dComp (sec Kc η hs i hi) η') τ0') :=
      (X.obj _).trans ((X.obj _).symm (sec_pull Kc η hs hi τ0))
        ((X.obj _).trans (mp_eqv X _ e) (mp_comp X _ _ τ0'))
    ⟨e1, strict_id (nondeg_strict Kc hn _ τ0' e1)⟩
  ⟨fun i hi => by
    have h := (key i hi).2 (η.f i) (η.bound i hi)
    show η.f i = η'.f i
    have h' : η'.f (secf Kc η hs i (η.f i)) = η.f i := h
    rw [secf_at] at h'
    exact h'.symm,
   (X.obj _).trans (key 0 (Nat.zero_le m)).1 (mp_idl X _ (key 0 (Nat.zero_le m)).2 τ0')⟩

end IC

end Found

#print axioms Found.IC.sec
#print axioms Found.IC.factor_step
#print axioms Found.IC.strict_id
#print axioms Found.IC.ez_exists
#print axioms Found.IC.nondeg_strict
#print axioms Found.IC.ez_dim
#print axioms Found.IC.ez_uniq

found/IC11.lean

lean_all/found/IC11.lean

/-
  T153 ((infinity,1)-categories), part 11. found/IC11 — the simplex category
  is generated by faces and degeneracies: a property of maps of Delta that
  respects pointwise equality, is closed under composition and holds for
  identities, faces and degeneracies holds for every map.  (A map with a
  repeated value splits off a degeneracy; a strictly increasing map missing
  a value j splits off the face δ_j; a strictly increasing surjection is an
  identity.)  Used to check naturality on generators only.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.IC10

namespace Found

namespace IC

open Simp

theorem pred_succ_of_pos {j : Nat} (h : 0 < j) : Nat.pred j + 1 = j :=
  match j, h with
  | _ + 1, _ => rfl

/-- sk j ∘ dg (pred j) is the identity away from j -/
theorem sk_dg {j x : Nat} (hx : x ≠ j) : sk j (dg (Nat.pred j) x) = x :=
  match Nat.lt_or_ge x j with
  | .inl h =>
    have hp : x < Nat.pred j + 1 := by
      rw [pred_succ_of_pos (Nat.lt_of_le_of_lt (Nat.zero_le x) h)]; exact h
    by rw [dg_lt hp, sk_lt h]
  | .inr h =>
    have hgt : j < x := Nat.lt_of_le_of_ne h (fun e => hx e.symm)
    have hp : Nat.pred j + 1 ≤ x := match j, hgt with
      | 0, hg => hg
      | _ + 1, hg => Nat.le_of_lt hg
    by
      rw [dg_ge hp, sk_ge (Nat.le_of_succ_le_succ (Nat.le_trans hgt (Nat.le_of_eq (succ_pred_of hgt).symm))),
        succ_pred_of hgt]

/-- a strictly increasing surjection is the identity -/
theorem surj_strict_id {m' m : Nat} {g : DHom m' m} (hst : StrictM g) (hs : Surj g) :
    ∀ i, i ≤ m' → g.f i = i
  | 0, _ => match hs 0 (Nat.zero_le m) with
    | ⟨a, ha, e⟩ => Nat.le_antisymm (Nat.le_trans (dmono g (Nat.zero_le a) ha) (Nat.le_of_eq e)) (Nat.zero_le _)
  | i + 1, hi =>
    have ih := surj_strict_id hst hs i (Nat.le_of_succ_le hi)
    have hlt : i < g.f (i + 1) := Nat.lt_of_le_of_lt (Nat.le_of_eq ih.symm) (hst i hi)
    match Nat.lt_or_ge (i + 1) (g.f (i + 1)) with
    | .inr h => Nat.le_antisymm h hlt
    | .inl h =>
      match hs (i + 1) (Nat.le_trans (Nat.le_of_lt h) (g.bound _ hi)) with
      | ⟨a, ha, e⟩ => match Nat.lt_or_ge a (i + 1) with
        | .inl ha' => absurd (Nat.le_trans (Nat.le_of_eq e.symm)
            (Nat.le_trans (dmono g (Nat.le_of_lt_succ ha') (Nat.le_of_succ_le hi)) (Nat.le_of_eq ih)))
            (Nat.not_succ_le_self i)
        | .inr ha' => absurd (Nat.lt_of_lt_of_le h (Nat.le_trans (dmono g ha' ha) (Nat.le_of_eq e)))
            (Nat.lt_irrefl _)

theorem surj_strict_dim {m' m : Nat} {g : DHom m' m} (hst : StrictM g) (hs : Surj g) : m' = m :=
  Nat.le_antisymm (strict_dim hst) (match hs m (Nat.le_refl m) with
    | ⟨a, ha, e⟩ => Nat.le_trans (Nat.le_of_eq (e.symm.trans (surj_strict_id hst hs a ha))) ha)

variable (Kc : CP.Classical')
  (P : ∀ {m' m : Nat}, DHom m' m → Prop)
  (Presp : ∀ {m' m : Nat} {g g' : DHom m' m}, (∀ i, i ≤ m' → g.f i = g'.f i) → P g → P g')
  (Pcomp : ∀ {a b c : Nat} {g : DHom a b} {h : DHom b c}, P g → P h → P (dComp g h))
  (Pid : ∀ m, P (dId m))
  (Pface : ∀ n i, i ≤ n + 1 → P (face n i))
  (Pdeg : ∀ n i (hi : i ≤ n), P (degen n i hi))

include Kc Presp Pcomp Pid Pface Pdeg in
/-- every map of the simplex category has the property -/
theorem gen_all : ∀ (N m' m : Nat), m' + m ≤ N → ∀ g : DHom m' m, P g
  | 0, m', m, hN, g =>
    match m', m, hN, g with
    | 0, 0, _, g => Presp (fun i hi => by
        show i = g.f i
        exact (Nat.eq_zero_of_le_zero hi).trans (Nat.eq_zero_of_le_zero (g.bound i hi)).symm) (Pid 0)
    | _ + 1, _, h, _ => absurd (Nat.le_trans (Nat.le_add_right _ _) h) (Nat.not_succ_le_zero _)
    | 0, _ + 1, h, _ => absurd h (Nat.not_succ_le_zero _)
  | N + 1, m', m, hN, g =>
    match Kc.lem (∃ i, i < m' ∧ g.f i = g.f (i + 1)) with
    | .inl ⟨i, hi, e⟩ =>
      match m', hN, g, hi, e with
      | m1 + 1, hN, g, hi, e =>
        have hm : m1 + m ≤ N := Nat.le_of_succ_le_succ (Nat.le_trans (Nat.le_of_eq (Nat.succ_add m1 m).symm) hN)
        Presp (fun j hj => (factor_step g (Nat.le_of_lt_succ hi) e j hj).symm)
          (Pcomp (Pdeg m1 i (Nat.le_of_lt_succ hi)) (gen_all N m1 m hm (dComp (face m1 (i + 1)) g)))
    | .inr hrep =>
      have hst : StrictM g := fun i hi => match Kc.lem (g.f i < g.f (i + 1)) with
        | .inl h => h
        | .inr h => absurd ⟨i, hi, Nat.le_antisymm (dmono g (Nat.le_succ i) hi) (Nat.ge_of_not_lt h)⟩ hrep
      match Kc.lem (∃ j, j ≤ m ∧ ∀ a, a ≤ m' → g.f a ≠ j) with
      | .inl ⟨j, hj, hmiss⟩ =>
        match m, hN, g, hst, hj, hmiss with
        | 0, _, g, _, hj, hmiss =>
          absurd ((Nat.eq_zero_of_le_zero (g.bound 0 (Nat.zero_le _))).trans (Nat.eq_zero_of_le_zero hj).symm)
            (hmiss 0 (Nat.zero_le _))
        | m2 + 1, hN, g, _, hj, hmiss =>
          have hm : m' + m2 ≤ N := Nat.le_of_succ_le_succ hN
          Presp (fun i hi => sk_dg (hmiss i hi))
            (Pcomp (gen_all N m' m2 hm (dComp g (degen m2 (Nat.pred j) (Nat.pred_le_pred hj))))
              (Pface m2 j hj))
      | .inr hfull =>
        have hs : Surj g := fun j hj => match Kc.lem (∃ a, a ≤ m' ∧ g.f a = j) with
          | .inl h => h
          | .inr h => absurd ⟨j, hj, fun a ha e => h ⟨a, ha, e⟩⟩ hfull
        have hid := surj_strict_id hst hs
        by
          cases surj_strict_dim hst hs
          exact Presp (fun i hi => (hid i hi).symm) (Pid m')

end IC

end Found

#print axioms Found.IC.sk_dg
#print axioms Found.IC.surj_strict_id
#print axioms Found.IC.gen_all

found/IC12.lean

lean_all/found/IC12.lean

/-
  T153 ((infinity,1)-categories), part 12. found/IC12 — index identities for
  joins: the faces and degeneracies of the big simplex [a+1+m] are joins of
  faces/degeneracies with identities (front ones J(δ_i, id) = δ_i, back ones
  J(id, δ_j) = δ_(j+a+1)), and the identity-function maps castD between
  dimensions that are equal only propositionally (to compare simplices of
  X_(a+1+m) with simplices of X_(D+2) without type casts).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.IC11

namespace Found

namespace IC

open Simp

/-- the identity function as a map [a] -> [b] when a <= b -/
def castD (a b : Nat) (h : a ≤ b) : DHom a b := ⟨fun i => i, fun _ _ => Nat.le_succ _, fun _ hi => Nat.le_trans hi h⟩

theorem add1_comm (a j : Nat) : a + 1 + j = j + (a + 1) := Nat.add_comm _ _

/-- front faces of the big simplex -/
theorem J_face_front {n' : Nat} (m : Nat) {i : Nat} (hi : i ≤ n' + 1) (p : Nat) :
    (J (face n' i) (dId m)).f p = sk i p :=
  match split_ic (n' + 1) p with
  | .inl h => jmap_front _ _ h
  | .inr ⟨j, e⟩ => by
    show jmap _ _ p = sk i p
    rw [e, jmap_back, sk_ge (Nat.le_trans hi (Nat.le_add_left _ j))]
    show n' + 1 + 1 + j = j + (n' + 1) + 1
    rw [Nat.add_comm (n' + 1 + 1) j]
    rfl

/-- back faces of the big simplex -/
theorem J_face_back (N : Nat) {m' j : Nat} (p : Nat) :
    (J (dId N) (face m' j)).f p = sk (j + (N + 1)) p :=
  match split_ic (N + 1) p with
  | .inl h => by
    show jmap _ _ p = _
    rw [jmap_front _ _ h, sk_lt (Nat.lt_of_lt_of_le h (Nat.le_add_left _ j))]
    rfl
  | .inr ⟨q, e⟩ => by
    show jmap _ _ p = _
    rw [e, jmap_back]
    show N + 1 + sk j q = sk (j + (N + 1)) (q + (N + 1))
    match Nat.lt_or_ge q j with
    | .inl h => rw [sk_lt h, sk_lt (Nat.add_lt_add_right h _), Nat.add_comm]
    | .inr h => rw [sk_ge h, sk_ge (Nat.add_le_add_right h _), Nat.add_comm (N + 1) (q + 1), Nat.succ_add]

/-- front degeneracies of the big simplex -/
theorem J_degen_front {n' : Nat} (m : Nat) {i : Nat} (hi : i ≤ n') (p : Nat) :
    (J (degen n' i hi) (dId m)).f p = dg i p :=
  match split_ic (n' + 1 + 1) p with
  | .inl h => jmap_front _ _ h
  | .inr ⟨j, e⟩ => by
    show jmap _ _ p = dg i p
    rw [e, jmap_back, dg_ge (Nat.le_trans (Nat.succ_le_succ hi) (Nat.le_trans (Nat.le_succ _) (Nat.le_add_left _ j)))]
    show n' + 1 + j = Nat.pred (j + (n' + 1 + 1))
    rw [Nat.add_comm (n' + 1) j]
    rfl

/-- back degeneracies of the big simplex -/
theorem J_degen_back (N : Nat) {m' j : Nat} (hj : j ≤ m') (p : Nat) :
    (J (dId N) (degen m' j hj)).f p = dg (j + (N + 1)) p :=
  match split_ic (N + 1) p with
  | .inl h => by
    show jmap _ _ p = _
    rw [jmap_front _ _ h, dg_lt (Nat.lt_of_lt_of_le h (Nat.le_trans (Nat.le_add_left _ j) (Nat.le_succ _)))]
    rfl
  | .inr ⟨q, e⟩ => by
    show jmap _ _ p = _
    rw [e, jmap_back]
    show N + 1 + dg j q = dg (j + (N + 1)) (q + (N + 1))
    match Nat.lt_or_ge q (j + 1) with
    | .inl h =>
      rw [dg_lt h, dg_lt (Nat.lt_of_lt_of_le (Nat.add_lt_add_right h (N + 1)) (Nat.le_of_eq (Nat.succ_add j (N + 1)))),
        Nat.add_comm]
    | .inr h =>
      have h' : j + (N + 1) + 1 ≤ q + (N + 1) :=
        Nat.le_trans (Nat.le_of_eq (Nat.succ_add j (N + 1)).symm) (Nat.add_le_add_right h _)
      rw [dg_ge h, dg_ge h']
      match q, h with
      | q' + 1, _ =>
        show N + 1 + q' = Nat.pred (q' + 1 + (N + 1))
        rw [Nat.succ_add q' (N + 1)]
        exact Nat.add_comm (N + 1) q'

end IC

end Found

#print axioms Found.IC.J_face_front
#print axioms Found.IC.J_face_back
#print axioms Found.IC.J_degen_front
#print axioms Found.IC.J_degen_back

found/IC13.lean

lean_all/found/IC13.lean

/-
  T153 ((infinity,1)-categories), part 13. found/IC13 — sections of maps
  with the boundary lifting property, I: the construction.  For π : E -> K
  (inside the classical point at universe w: excluded middle and an
  extensional choice operator, as hypothesis variables) define a candidate
  section sec by recursion on dimension: a degenerate simplex τ = η^* τ0
  (Eilenberg–Zilber, chosen) goes to η^* sec(τ0); a non-degenerate one goes
  to a chosen lift of τ whose faces are the sections of the faces of τ.
  Unfolding lemmas for sec.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.IC12

namespace Found

namespace IC

open Simp

universe w

/-- the classical point at universe w: excluded middle and an extensional choice operator -/
structure ClW where
  lem : ∀ p : Prop, p ∨ ¬ p
  ch : ∀ {α : Type w} (P : α → Prop), (∃ x, P x) → α
  ch_spec : ∀ {α : Type w} (P : α → Prop) (h : ∃ x, P x), P (ch P h)
  ch_ext : ∀ {α : Type w} (P Q : α → Prop) (h : ∃ x, P x) (h' : ∃ x, Q x), (∀ x, P x ↔ Q x) → ch P h = ch Q h'

def cstAll (m : Nat) : DHom m 0 := ⟨fun _ => 0, fun _ _ => Nat.le_refl 0, fun _ _ => Nat.le_refl 0⟩

/-- Eilenberg–Zilber data of an m-simplex -/
def EZD (K : SSet.{w}) (m : Nat) : Type w := Σ k : Nat, DHom m k × (K.obj (k : Nat)).car

def ezP {K : SSet.{w}} {m : Nat} (τ : (K.obj (m : Nat)).car) (D : EZD K m) : Prop :=
  Surj D.2.1 ∧ NonDeg D.2.2 ∧ (K.obj (m : Nat)).eqv τ (mp K D.2.1 D.2.2)

variable (Kw : ClW.{w}) (Kc : CP.Classical') {K E : SSet.{w}} (π : PNat E K)
  (L0 : ∀ τ : (V K).car, ∃ e : (V E).car, (V K).eqv (π.app (0 : Nat) e) τ)

/-- the chosen Eilenberg–Zilber decomposition -/
def ezC {m : Nat} (τ : (K.obj (m : Nat)).car) : EZD K m :=
  Kw.ch (ezP τ) (match ez_exists Kc m m (Nat.le_refl m) τ with
    | ⟨k, η, τ0, h1, h2, h3⟩ => ⟨⟨k, η, τ0⟩, h1, h2, h3⟩)

theorem ezC_spec {m : Nat} (τ : (K.obj (m : Nat)).car) : ezP τ (ezC Kw Kc τ) := Kw.ch_spec _ _

theorem ezC_ext {m : Nat} {τ τ' : (K.obj (m : Nat)).car} (e : (K.obj (m : Nat)).eqv τ τ') :
    ezC Kw Kc τ = ezC Kw Kc τ' :=
  Kw.ch_ext _ _ _ _ (fun _ => ⟨fun ⟨h1, h2, h3⟩ => ⟨h1, h2, (K.obj _).trans ((K.obj _).symm e) h3⟩,
    fun ⟨h1, h2, h3⟩ => ⟨h1, h2, (K.obj _).trans e h3⟩⟩)

include L0 in
theorem inhE (m : Nat) (τ : (K.obj (m : Nat)).car) : ∃ _ : (E.obj (m : Nat)).car, True :=
  match L0 (mp K (cst0 m) τ) with
  | ⟨e, _⟩ => ⟨mp E (cstAll m) e, trivial⟩

/-- choose e with P e when there is one -/
def chT {m : Nat} (τ : (K.obj (m : Nat)).car) (P : (E.obj (m : Nat)).car → Prop) : (E.obj (m : Nat)).car :=
  Kw.ch (fun e => (∃ e', P e') → P e) (match Kw.lem (∃ e', P e') with
    | .inl ⟨e', h⟩ => ⟨e', fun _ => h⟩
    | .inr h => match inhE π L0 m τ with
      | ⟨e, _⟩ => ⟨e, fun h' => absurd h' h⟩)

theorem chT_spec {m : Nat} (τ : (K.obj (m : Nat)).car) (P : (E.obj (m : Nat)).car → Prop) (h : ∃ e, P e) :
    P (chT Kw π L0 τ P) :=
  Kw.ch_spec (fun e => (∃ e', P e') → P e) _ h

theorem chT_ext {m : Nat} (τ τ' : (K.obj (m : Nat)).car) (P Q : (E.obj (m : Nat)).car → Prop)
    (hPQ : ∀ e, P e ↔ Q e) : chT Kw π L0 τ P = chT Kw π L0 τ' Q :=
  Kw.ch_ext _ _ _ _ (fun e => ⟨fun f h => (hPQ e).1 (f (match h with | ⟨e', q⟩ => ⟨e', (hPQ e').2 q⟩)),
    fun f h => (hPQ e).2 (f (match h with | ⟨e', q⟩ => ⟨e', (hPQ e').1 q⟩))⟩)

/-- the lifting condition for a non-degenerate (m'+1)-simplex, given the section below -/
def liftP {m' : Nat} (τ : (K.obj (m' + 1 : Nat)).car) (prev : (K.obj (m' : Nat)).car → (E.obj (m' : Nat)).car)
    (e : (E.obj (m' + 1 : Nat)).car) : Prop :=
  (K.obj (m' + 1 : Nat)).eqv (π.app (m' + 1 : Nat) e) τ ∧
    ∀ j, j ≤ m' + 1 → (E.obj (m' : Nat)).eqv (dd E j e) (prev (dd K j τ))

/-- the stages of the section -/
def stg : (d m : Nat) → m ≤ d → (K.obj (m : Nat)).car → (E.obj (m : Nat)).car
  | 0, 0, _, τ => chT Kw π L0 τ (fun e => (V K).eqv (π.app (0 : Nat) e) τ)
  | 0, _ + 1, _, τ => chT Kw π L0 τ (fun _ => True)
  | d + 1, m, hm, τ =>
    if h : m ≤ d then stg d m h τ else
      match m, hm, h, τ with
      | 0, _, h, _ => absurd (Nat.zero_le d) h
      | m' + 1, hm, _, τ =>
        if hk : (ezC Kw Kc τ).1 < m' + 1 then
          mp E (ezC Kw Kc τ).2.1 (stg d (ezC Kw Kc τ).1
            (Nat.le_trans (Nat.le_of_lt_succ hk) (Nat.le_of_succ_le_succ hm)) (ezC Kw Kc τ).2.2)
        else chT Kw π L0 τ (liftP π τ (fun x => stg d m' (Nat.le_of_succ_le_succ hm) x))

/-- the candidate section -/
def secS (m : Nat) (τ : (K.obj (m : Nat)).car) : (E.obj (m : Nat)).car := stg Kw Kc π L0 m m (Nat.le_refl m) τ

theorem stg_le {d m : Nat} (h : m ≤ d) (hm : m ≤ d + 1) (τ : (K.obj (m : Nat)).car) :
    stg Kw Kc π L0 (d + 1) m hm τ = stg Kw Kc π L0 d m h τ := by
  show dite (m ≤ d) (fun h => stg Kw Kc π L0 d m h τ) _ = _
  rw [dite_eq_left h]

theorem stg_eq : ∀ (d m : Nat) (h : m ≤ d) (τ : (K.obj (m : Nat)).car), stg Kw Kc π L0 d m h τ = secS Kw Kc π L0 m τ
  | 0, 0, _, _ => rfl
  | 0, _ + 1, h, _ => absurd h (Nat.not_succ_le_zero _)
  | d + 1, m, h, τ => match Nat.lt_or_ge d m with
    | .inr hmd => (stg_le Kw Kc π L0 hmd h τ).trans (stg_eq d m hmd τ)
    | .inl hdm => by
      cases Nat.le_antisymm h hdm
      rfl

theorem sec_zero (τ : (V K).car) :
    secS Kw Kc π L0 0 τ = chT Kw π L0 τ (fun e => (V K).eqv (π.app (0 : Nat) e) τ) := rfl

theorem sec_deg {m' : Nat} (τ : (K.obj (m' + 1 : Nat)).car) (hk : (ezC Kw Kc τ).1 < m' + 1) :
    secS Kw Kc π L0 (m' + 1) τ = mp E (ezC Kw Kc τ).2.1 (secS Kw Kc π L0 (ezC Kw Kc τ).1 (ezC Kw Kc τ).2.2) := by
  show dite (m' + 1 ≤ m') _ _ = _
  rw [dite_eq_right (Nat.not_succ_le_self m')]
  show dite ((ezC Kw Kc τ).1 < m' + 1) _ _ = _
  rw [dite_eq_left hk]
  exact congrArg (mp E (ezC Kw Kc τ).2.1) (stg_eq Kw Kc π L0 _ _ _ _)

theorem sec_nd {m' : Nat} (τ : (K.obj (m' + 1 : Nat)).car) (hk : ¬ (ezC Kw Kc τ).1 < m' + 1) :
    secS Kw Kc π L0 (m' + 1) τ = chT Kw π L0 τ (liftP π τ (secS Kw Kc π L0 m')) := by
  show dite (m' + 1 ≤ m') _ _ = _
  rw [dite_eq_right (Nat.not_succ_le_self m')]
  show dite ((ezC Kw Kc τ).1 < m' + 1) _ _ = _
  rw [dite_eq_right hk]
  exact congrArg (chT Kw π L0 τ ∘ liftP π τ) (stg_self m')
where
  stg_self (m' : Nat) : (fun x => stg Kw Kc π L0 m' m' (Nat.le_of_succ_le_succ (Nat.le_refl (m' + 1))) x) =
      secS Kw Kc π L0 m' := rfl

end IC

end Found

#print axioms Found.IC.ezC_ext
#print axioms Found.IC.chT_ext
#print axioms Found.IC.stg_eq
#print axioms Found.IC.sec_deg
#print axioms Found.IC.sec_nd

found/IC14.lean

lean_all/found/IC14.lean

/-
  T153 ((infinity,1)-categories), part 14. found/IC14 — sections of maps
  with the boundary lifting property, II: invariance and degeneracies.
  The candidate section secS takes equivalent simplices to the same value;
  every value is the pullback of the value at the chosen non-degenerate
  simplex along the chosen surjection; and secS commutes with every
  degeneracy (by uniqueness in Eilenberg–Zilber).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.IC13

namespace Found

namespace IC

open Simp

universe w

/-- surjections do not raise dimension -/
theorem surj_le (Kc : CP.Classical') {m k : Nat} (η : DHom m k) (hs : Surj η) : k ≤ m :=
  strict_dim (φ := sec Kc η hs 0 (Nat.zero_le m)) (fun j hj =>
    pre_lt η (secf_spec Kc η hs (Nat.zero_le m) (Nat.le_of_lt hj)).1
      (by show η.f (secf Kc η hs 0 j) < η.f (secf Kc η hs 0 (j + 1))
          rw [(secf_spec Kc η hs (Nat.zero_le m) (Nat.le_of_lt hj)).2, (secf_spec Kc η hs (Nat.zero_le m) hj).2]
          exact Nat.lt_succ_self j))

variable (Kw : ClW.{w}) (Kc : CP.Classical') {K E : SSet.{w}} (π : PNat E K)
  (L0 : ∀ τ : (K.obj (0 : Nat)).car, ∃ e : (E.obj (0 : Nat)).car, (K.obj (0 : Nat)).eqv (π.app (0 : Nat) e) τ)

/-- equivalent simplices have the same section value -/
theorem secS_inv : ∀ (m : Nat) {τ τ' : (K.obj (m : Nat)).car}, (K.obj (m : Nat)).eqv τ τ' →
    secS Kw Kc π L0 m τ = secS Kw Kc π L0 m τ'
  | 0, τ, τ', e => (sec_zero Kw Kc π L0 τ).trans ((chT_ext Kw π L0 τ τ' _ _ (fun _ =>
      ⟨fun h => (K.obj _).trans h e, fun h => (K.obj _).trans h ((K.obj _).symm e)⟩)).trans
      (sec_zero Kw Kc π L0 τ').symm)
  | m' + 1, τ, τ', e => by
    have hz := ezC_ext Kw Kc e
    match Nat.lt_or_ge (ezC Kw Kc τ).1 (m' + 1) with
    | .inl hk =>
      rw [sec_deg Kw Kc π L0 τ hk, sec_deg Kw Kc π L0 τ' (hz ▸ hk), hz]
    | .inr hk =>
      have hk' : ¬ (ezC Kw Kc τ).1 < m' + 1 := Nat.not_lt_of_ge hk
      rw [sec_nd Kw Kc π L0 τ hk', sec_nd Kw Kc π L0 τ' (hz ▸ hk')]
      exact chT_ext Kw π L0 τ τ' _ _ (fun x =>
        ⟨fun ⟨h1, h2⟩ => ⟨(K.obj _).trans h1 e, fun j hj =>
            (secS_inv m' (K.map_eqv (face m' j) e)) ▸ h2 j hj⟩,
         fun ⟨h1, h2⟩ => ⟨(K.obj _).trans h1 ((K.obj _).symm e), fun j hj =>
            (secS_inv m' (K.map_eqv (face m' j) e)).symm ▸ h2 j hj⟩⟩)

include Kw in
/-- a simplex whose chosen decomposition has full dimension is non-degenerate -/
theorem nondeg_of_full {q : Nat} (x : (K.obj (q : Nat)).car) (h : ¬ (ezC Kw Kc x).1 < q) : NonDeg x :=
  fun ⟨k, θ, z, hs, hk, e⟩ =>
    match ezC_spec Kw Kc x, ez_exists Kc k k (Nat.le_refl k) z with
    | ⟨s1, s2, s3⟩, ⟨_, ζ, z0, t1, t2, t3⟩ =>
      have e2 : (K.obj (q : Nat)).eqv (mp K (ezC Kw Kc x).2.1 (ezC Kw Kc x).2.2) (mp K (dComp θ ζ) z0) :=
        (K.obj _).trans ((K.obj _).symm s3) ((K.obj _).trans e ((K.obj _).trans (mp_eqv K θ t3) (mp_comp K θ ζ z0)))
      have hd := ez_dim Kc s1 (surj_comp hs t1) s2 t2 e2
      h (Nat.lt_of_le_of_lt (Nat.le_trans (Nat.le_of_eq hd) (surj_le Kc ζ t1)) hk)

/-- every section value is the pullback of the value at the chosen non-degenerate simplex -/
theorem secS_ez (q : Nat) (x : (K.obj (q : Nat)).car) :
    (E.obj (q : Nat)).eqv (secS Kw Kc π L0 q x)
      (mp E (ezC Kw Kc x).2.1 (secS Kw Kc π L0 (ezC Kw Kc x).1 (ezC Kw Kc x).2.2)) :=
  match Nat.lt_or_ge (ezC Kw Kc x).1 q with
  | .inl hk => match q, x, hk with
    | q' + 1, x, hk => by rw [sec_deg Kw Kc π L0 x hk]; exact (E.obj _).refl _
  | .inr hk => by
    have hnd := nondeg_of_full Kw Kc x (Nat.not_lt_of_ge hk)
    have sp := ezC_spec Kw Kc x
    have hle := surj_le Kc _ sp.1
    revert hk
    revert hle
    revert sp
    generalize ezC Kw Kc x = D
    intro sp hle hk
    obtain ⟨k, η, x0⟩ := D
    have hkq : k = q := Nat.le_antisymm hle hk
    cases hkq
    have u := ez_uniq Kc (η := η) (η' := dId _) sp.1 sp.2.1
      ((K.obj _).trans ((K.obj _).symm sp.2.2) ((K.obj _).symm (mp_idl K (dId _) (fun _ _ => rfl) x)))
    show (E.obj _).eqv _ (mp E η (secS Kw Kc π L0 _ x0))
    rw [secS_inv Kw Kc π L0 _ u.2]
    exact (E.obj _).symm (mp_idl E η u.1 _)

/-- section values commute with degeneracies -/
theorem secS_degen (q : Nat) (x : (K.obj (q : Nat)).car) (i : Nat) (hi : i ≤ q) :
    (E.obj (q + 1 : Nat)).eqv (secS Kw Kc π L0 (q + 1) (mp K (degen q i hi) x))
      (mp E (degen q i hi) (secS Kw Kc π L0 q x)) := by
  have sy := ezC_spec Kw Kc (mp K (degen q i hi) x)
  have sx := ezC_spec Kw Kc x
  have cx := secS_ez Kw Kc π L0 q x
  have ey : (K.obj (q + 1 : Nat)).eqv (mp K (degen q i hi) x)
      (mp K (dComp (degen q i hi) (ezC Kw Kc x).2.1) (ezC Kw Kc x).2.2) :=
    (K.obj _).trans (mp_eqv K _ sx.2.2) (mp_comp K _ _ _)
  have hd := ez_dim Kc sy.1 (surj_comp (surj_degen q i hi) sx.1) sy.2.1 sx.2.1
    ((K.obj _).trans ((K.obj _).symm sy.2.2) ey)
  have hk : (ezC Kw Kc (mp K (degen q i hi) x)).1 < q + 1 :=
    Nat.lt_succ_of_le (Nat.le_trans (Nat.le_of_eq hd) (surj_le Kc _ sx.1))
  rw [sec_deg Kw Kc π L0 _ hk]
  revert hd
  revert ey
  revert cx
  revert sx
  revert sy
  generalize ezC Kw Kc (mp K (degen q i hi) x) = Dy
  generalize ezC Kw Kc x = Dx
  intro sy sx cx ey hd
  obtain ⟨k, η, y0⟩ := Dy
  obtain ⟨k', η', x0⟩ := Dx
  cases hd
  have u := ez_uniq Kc sy.1 sy.2.1 ((K.obj _).trans ((K.obj _).symm sy.2.2) ey)
  show (E.obj _).eqv (mp E η (secS Kw Kc π L0 _ y0)) _
  rw [secS_inv Kw Kc π L0 _ u.2]
  exact (E.obj _).trans (mp_resp E u.1 _)
    ((E.obj _).trans ((E.obj _).symm (mp_comp E _ _ _)) (mp_eqv E _ ((E.obj _).symm cx)))

end IC

end Found

#print axioms Found.IC.surj_le
#print axioms Found.IC.secS_inv
#print axioms Found.IC.nondeg_of_full
#print axioms Found.IC.secS_ez
#print axioms Found.IC.secS_degen

found/IC15.lean

lean_all/found/IC15.lean

/-
  T153 ((infinity,1)-categories), part 15. found/IC15 — sections of maps
  with the boundary lifting property, III: if π : E -> K lifts vertices and
  fills every boundary ∂Delta^(m+1) of E lying over a simplex of K, then π
  has a section (a simplicial map s : K -> E with π s = id).  By induction
  on dimension: non-degenerate simplices get lifts with the right faces,
  degenerate ones are pulled back, and naturality is checked by splitting
  maps of Delta into degeneracies and faces.  This is the engine behind
  "X quasi-category => slices and function complexes are quasi-categories".

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.IC14

namespace Found

namespace IC

open Simp

universe w

variable (Kw : ClW.{w}) (Kc : CP.Classical') {K E : SSet.{w}} (π : PNat E K)
  (L0 : ∀ τ : (K.obj (0 : Nat)).car, ∃ e : (E.obj (0 : Nat)).car, (K.obj (0 : Nat)).eqv (π.app (0 : Nat) e) τ)
  (L1 : ∀ (τ : (K.obj (1 : Nat)).car) (b0 b1 : (E.obj (0 : Nat)).car),
    (K.obj (0 : Nat)).eqv (π.app (0 : Nat) b0) (dd K 0 τ) → (K.obj (0 : Nat)).eqv (π.app (0 : Nat) b1) (dd K 1 τ) →
    ∃ e : (E.obj (1 : Nat)).car, (K.obj (1 : Nat)).eqv (π.app (1 : Nat) e) τ ∧
      (E.obj (0 : Nat)).eqv (dd E 0 e) b0 ∧ (E.obj (0 : Nat)).eqv (dd E 1 e) b1)
  (L2 : ∀ (n : Nat) (τ : (K.obj (n + 2 : Nat)).car) (b : Bdry E n),
    (∀ j, j ≤ n + 2 → (K.obj (n + 1 : Nat)).eqv (π.app (n + 1 : Nat) (b.x j)) (dd K j τ)) →
    ∃ e : (E.obj (n + 2 : Nat)).car, (K.obj (n + 2 : Nat)).eqv (π.app (n + 2 : Nat) e) τ ∧ BFills b e)

def ProjK (k : Nat) : Prop := ∀ τ : (K.obj (k : Nat)).car,
  (K.obj (k : Nat)).eqv (π.app (k : Nat) (secS Kw Kc π L0 k τ)) τ

def NatK (k : Nat) : Prop := ∀ (τ : (K.obj (k : Nat)).car) (m' : Nat) (g : DHom m' k),
  (E.obj (m' : Nat)).eqv (secS Kw Kc π L0 m' (mp K g τ)) (mp E g (secS Kw Kc π L0 k τ))

theorem eqv_of_eq {S : Setd.{w}} {a b : S.car} (h : a = b) : S.eqv a b := h ▸ S.refl a

theorem pull (X : SSet.{w}) {a b c : Nat} {g : DHom a c} {u : DHom a b} {v : DHom b c}
    (h : ∀ i, i ≤ a → g.f i = (dComp u v).f i) (x : (X.obj (c : Nat)).car) :
    (X.obj (a : Nat)).eqv (mp X g x) (mp X u (mp X v x)) :=
  (X.obj _).trans (mp_resp X h x) ((X.obj _).symm (mp_comp X u v x))

/-- naturality over vertices -/
theorem nat0 : NatK Kw Kc π L0 0 := fun τ => by
  intro m' g
  induction m' with
  | zero =>
    have hg : ∀ i, i ≤ 0 → g.f i = i := fun i hi =>
      (Nat.eq_zero_of_le_zero (g.bound i hi)).trans (Nat.eq_zero_of_le_zero hi).symm
    rw [secS_inv Kw Kc π L0 0 (mp_idl K g hg τ)]
    exact (E.obj _).symm (mp_idl E g hg _)
  | succ m1 ih =>
    have hg : ∀ i, i ≤ m1 + 1 → g.f i = (dComp (degen m1 0 (Nat.zero_le m1)) (cstAll m1)).f i := fun i hi =>
      Nat.eq_zero_of_le_zero (g.bound i hi)
    rw [secS_inv Kw Kc π L0 _ (pull K hg τ)]
    exact (E.obj _).trans (secS_degen Kw Kc π L0 m1 _ 0 (Nat.zero_le m1))
      ((E.obj _).trans (mp_eqv E _ (ih (cstAll m1))) ((E.obj _).symm (pull E hg _)))

/-- naturality at a non-degenerate simplex whose faces are right -/
theorem natND (k1 : Nat) (τ : (K.obj (k1 + 1 : Nat)).car)
    (hf : ∀ j, j ≤ k1 + 1 → (E.obj (k1 : Nat)).eqv (dd E j (secS Kw Kc π L0 (k1 + 1) τ)) (secS Kw Kc π L0 k1 (dd K j τ)))
    (hN : NatK Kw Kc π L0 k1) : ∀ (N m' : Nat), m' ≤ N → ∀ g : DHom m' (k1 + 1),
    (E.obj (m' : Nat)).eqv (secS Kw Kc π L0 m' (mp K g τ)) (mp E g (secS Kw Kc π L0 (k1 + 1) τ))
  | N, m', hm, g =>
    match Kc.lem (∃ i, i < m' ∧ g.f i = g.f (i + 1)) with
    | .inl ⟨i, hi, e⟩ =>
      match m', N, hm, g, hi, e with
      | m1 + 1, N' + 1, hm, g, hi, e =>
        have hfac : ∀ j, j ≤ m1 + 1 → g.f j = (dComp (degen m1 i (Nat.le_of_lt_succ hi)) (dComp (face m1 (i + 1)) g)).f j :=
          factor_step g (Nat.le_of_lt_succ hi) e
        by
          rw [secS_inv Kw Kc π L0 _ (pull K hfac τ)]
          exact (E.obj _).trans (secS_degen Kw Kc π L0 m1 _ i _)
            ((E.obj _).trans (mp_eqv E _ (natND k1 τ hf hN N' m1 (Nat.le_of_succ_le_succ hm) _))
              ((E.obj _).symm (pull E hfac _)))
      | _ + 1, 0, hm, _, _, _ => absurd hm (Nat.not_succ_le_zero _)
    | .inr hrep =>
      have hst : StrictM g := fun i hi => match Kc.lem (g.f i < g.f (i + 1)) with
        | .inl h => h
        | .inr h => absurd ⟨i, hi, Nat.le_antisymm (dmono g (Nat.le_succ i) hi) (Nat.ge_of_not_lt h)⟩ hrep
      match Kc.lem (∃ j, j ≤ k1 + 1 ∧ ∀ a, a ≤ m' → g.f a ≠ j) with
      | .inl ⟨j, hj, hmiss⟩ =>
        have hfac : ∀ i, i ≤ m' → g.f i = (dComp (dComp g (degen k1 (Nat.pred j) (Nat.pred_le_pred hj))) (face k1 j)).f i :=
          fun i hi => (sk_dg (hmiss i hi)).symm
        by
          rw [secS_inv Kw Kc π L0 _ (pull K hfac τ)]
          exact (E.obj _).trans (hN (dd K j τ) m' _)
            ((E.obj _).trans (mp_eqv E _ ((E.obj _).symm (hf j hj))) ((E.obj _).symm (pull E hfac _)))
      | .inr hfull =>
        have hs : Surj g := fun j hj => match Kc.lem (∃ a, a ≤ m' ∧ g.f a = j) with
          | .inl h => h
          | .inr h => absurd ⟨j, hj, fun a ha e => h ⟨a, ha, e⟩⟩ hfull
        have hid := surj_strict_id hst hs
        by
          cases surj_strict_dim hst hs
          rw [secS_inv Kw Kc π L0 _ (mp_idl K g hid τ)]
          exact (E.obj _).symm (mp_idl E g hid _)

include L1 L2 in
/-- a non-degenerate simplex above the vertices gets a lift with the right faces -/
theorem liftOK (k1 : Nat) (hP : ProjK Kw Kc π L0 k1) (hN : NatK Kw Kc π L0 k1)
    (τ : (K.obj (k1 + 1 : Nat)).car) (hnd : NonDeg τ) :
    (K.obj (k1 + 1 : Nat)).eqv (π.app (k1 + 1 : Nat) (secS Kw Kc π L0 (k1 + 1) τ)) τ ∧
    ∀ j, j ≤ k1 + 1 → (E.obj (k1 : Nat)).eqv (dd E j (secS Kw Kc π L0 (k1 + 1) τ)) (secS Kw Kc π L0 k1 (dd K j τ)) := by
  have hk : ¬ (ezC Kw Kc τ).1 < k1 + 1 := fun hk => hnd (match ezC_spec Kw Kc τ with
    | ⟨h1, _, h3⟩ => ⟨_, _, _, h1, hk, h3⟩)
  rw [sec_nd Kw Kc π L0 τ hk]
  refine chT_spec Kw π L0 τ (liftP π τ (secS Kw Kc π L0 k1)) ?_
  match k1, τ, hP, hN with
  | 0, τ, hP, _ =>
    match L1 τ (secS Kw Kc π L0 0 (dd K 0 τ)) (secS Kw Kc π L0 0 (dd K 1 τ)) (hP _) (hP _) with
    | ⟨e, h, h0, h1⟩ => exact ⟨e, h, le1_cases (P := fun j => (E.obj (0 : Nat)).eqv (dd E j e)
        (secS Kw Kc π L0 0 (dd K j τ))) h0 h1⟩
  | n + 1, τ, hP, hN =>
    let b : Bdry E n := {
      x := fun j => secS Kw Kc π L0 (n + 1) (dd K j τ)
      compat := fun i j hij _ =>
        (E.obj _).trans ((E.obj _).symm (hN (dd K (j + 1) τ) n (face n i)))
          ((E.obj _).trans (eqv_of_eq (secS_inv Kw Kc π L0 n (dd_dd K hij τ))) (hN (dd K i τ) n (face n j))) }
    match L2 n τ b (fun j _ => hP _) with
    | ⟨e, h, hfill⟩ => exact ⟨e, h, hfill⟩

include L1 L2 in
theorem stepK (k : Nat) (IH : ∀ k0, k0 < k → ProjK Kw Kc π L0 k0 ∧ NatK Kw Kc π L0 k0) :
    ProjK Kw Kc π L0 k ∧ NatK Kw Kc π L0 k :=
  match k, IH with
  | 0, _ => ⟨fun τ => chT_spec Kw π L0 τ _ (L0 τ), nat0 Kw Kc π L0⟩
  | k1 + 1, IH =>
    have IH1 := IH k1 (Nat.lt_succ_self k1)
    have ND := liftOK Kw Kc π L0 L1 L2 k1 IH1.1 IH1.2
    ⟨fun τ => match Nat.lt_or_ge (ezC Kw Kc τ).1 (k1 + 1) with
      | .inl hk => by
        rw [sec_deg Kw Kc π L0 τ hk]
        exact (K.obj _).trans (π.nat _ _)
          ((K.obj _).trans (mp_eqv K _ ((IH _ hk).1 _)) ((K.obj _).symm (ezC_spec Kw Kc τ).2.2))
      | .inr hk => (ND τ (nondeg_of_full Kw Kc τ (Nat.not_lt_of_ge hk))).1,
     fun τ m' g => by
      have sp := ezC_spec Kw Kc τ
      have cz := secS_ez Kw Kc π L0 _ τ
      have hle := surj_le Kc _ sp.1
      have key : ∀ (m'' : Nat) (g' : DHom m'' (ezC Kw Kc τ).1),
          (E.obj (m'' : Nat)).eqv (secS Kw Kc π L0 m'' (mp K g' (ezC Kw Kc τ).2.2))
            (mp E g' (secS Kw Kc π L0 _ (ezC Kw Kc τ).2.2)) := by
        match Nat.lt_or_ge (ezC Kw Kc τ).1 (k1 + 1) with
        | .inl hk => exact (IH _ hk).2 _
        | .inr hk =>
          have hnd := sp.2.1
          revert hnd
          revert hk
          revert hle
          generalize ezC Kw Kc τ = D
          intro hle hk hnd
          obtain ⟨k0, η, τ0⟩ := D
          have h0 : k0 = k1 + 1 := Nat.le_antisymm hle hk
          cases h0
          have hfa := (ND τ0 hnd).2
          exact fun m'' g' => natND Kw Kc π L0 k1 τ0 hfa IH1.2 m'' m'' (Nat.le_refl _) g'
      rw [secS_inv Kw Kc π L0 _ ((K.obj _).trans (mp_eqv K g sp.2.2) (mp_comp K g _ _))]
      exact (E.obj _).trans (key m' _) ((E.obj _).trans ((E.obj _).symm (mp_comp E _ _ _))
        (mp_eqv E g ((E.obj _).symm cz)))⟩

include L1 L2 in
theorem mainK : ∀ (N k : Nat), k ≤ N → ProjK Kw Kc π L0 k ∧ NatK Kw Kc π L0 k
  | 0, k, hk => stepK Kw Kc π L0 L1 L2 k (fun _ h => absurd (Nat.lt_of_lt_of_le h hk) (Nat.not_lt_zero _))
  | N + 1, k, hk => stepK Kw Kc π L0 L1 L2 k (fun k0 h =>
      mainK N k0 (Nat.le_of_lt_succ (Nat.lt_of_lt_of_le h hk)))

/-- the section -/
def secN : PNat K E where
  app m τ := secS Kw Kc π L0 m τ
  app_eqv m {_ _} h := by rw [secS_inv Kw Kc π L0 m h]; exact (E.obj _).refl _
  nat {_ b} g τ := (mainK Kw Kc π L0 L1 L2 b b (Nat.le_refl _)).2 τ _ g

include L1 L2 in
/-- maps with the boundary lifting property have sections -/
theorem secN_proj (m : Nat) (τ : (K.obj (m : Nat)).car) :
    (K.obj (m : Nat)).eqv (π.app (m : Nat) ((secN Kw Kc π L0 L1 L2).app (m : Nat) τ)) τ :=
  (mainK Kw Kc π L0 L1 L2 m m (Nat.le_refl _)).1 τ

end IC

end Found

#print axioms Found.IC.nat0
#print axioms Found.IC.natND
#print axioms Found.IC.liftOK
#print axioms Found.IC.mainK
#print axioms Found.IC.secN
#print axioms Found.IC.secN_proj

found/IC16.lean

lean_all/found/IC16.lean

/-
  T153 ((infinity,1)-categories), part 16. found/IC16 — filling inner horns
  of a slice X_/p, I.  Given an inner horn H : Lambda^(n+2)_k -> X_/p and a
  filler σ of its Delta-part in X, the admissible values over K form a
  simplicial set EW -> K: an m-simplex is τ in K_m with w in X_(n+3+m)
  whose front faces are the ψ τ of the horn faces, whose back is p τ and
  whose front is σ.  Faces and degeneracies act through id * g.  The vertex
  lifting property of EW -> K is an inner horn of X (dimension n+3).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.IC15

namespace Found

namespace IC

open Simp

universe w

/-- pick the first c values from F, the rest from G (shifted) -/
def pick {A : Type w} (F G : Nat → A) : Nat → Nat → A
  | 0, i => G i
  | _ + 1, 0 => F 0
  | c + 1, i + 1 => pick (fun t => F (t + 1)) G c i

theorem pick_front {A : Type w} (G : Nat → A) : (c : Nat) → (F : Nat → A) → (i : Nat) → i < c → pick F G c i = F i
  | 0, _, i, h => absurd h (Nat.not_lt_zero i)
  | _ + 1, _, 0, _ => rfl
  | c + 1, F, i + 1, h => pick_front G c (fun t => F (t + 1)) i (Nat.lt_of_succ_lt_succ h)

theorem pick_back {A : Type w} (G : Nat → A) : (c : Nat) → (F : Nat → A) → (j : Nat) → pick F G c (j + c) = G j
  | 0, _, _ => rfl
  | c + 1, F, j => pick_back G c (fun t => F (t + 1)) j

/-- two composites with the same index function act the same -/
theorem mp_ceq' (X : SSet.{w}) {a b b' c : Nat} (u : DHom a b) (v : DHom b c) (u' : DHom a b') (v' : DHom b' c)
    (h : ∀ i, i ≤ a → v.f (u.f i) = v'.f (u'.f i)) (x : (X.obj (c : Nat)).car) :
    (X.obj (a : Nat)).eqv (mp X u (mp X v x)) (mp X u' (mp X v' x)) :=
  (X.obj _).trans (mp_comp X u v x) ((X.obj _).trans (mp_resp X (g := dComp u' v') h x)
    ((X.obj _).symm (mp_comp X u' v' x)))

variable {K X : SSet.{w}} {p : PNat K X} {n k : Nat} (H : Horn (Slice p) n k) (σ : (X.obj (n + 2 : Nat)).car)

/-- admissible values over an m-simplex of K -/
structure EW (m : Nat) : Type w where
  τ : (K.obj (m : Nat)).car
  w : (X.obj (n + 2 + 1 + m : Nat)).car
  front : ∀ i, i ≤ n + 2 → i ≠ k →
    (X.obj (n + 1 + 1 + m : Nat)).eqv (mp X (J (face (n + 1) i) (dId m)) w) ((H.x i).ψ m τ)
  back : (X.obj (m : Nat)).eqv (mp X (inK (n + 2) m) w) (p.app (m : Nat) τ)
  base : (X.obj (n + 2 : Nat)).eqv (mp X (inD (n + 2) m) w) σ

variable {H σ}

def EW.act {m' m : Nat} (g : DHom m' m) (e : EW H σ m) : EW H σ m' where
  τ := mp K g e.τ
  w := mp X (J (dId (n + 2)) g) e.w
  front i hi hik :=
    (X.obj _).trans (mp_ceq' X _ _ (J (dId (n + 1)) g) (J (face (n + 1) i) (dId m)) (fun q hq =>
        (J_comp (face (n + 1) i) (dId (n + 2)) (dId m') g q hq).trans
          ((J_resp (x' := dComp (dId (n + 1)) (face (n + 1) i)) (y' := dComp g (dId m)) (fun _ _ => rfl)
            (fun _ _ => rfl) q hq).trans (J_comp (dId (n + 1)) (face (n + 1) i) g (dId m) q hq).symm)) e.w)
      ((X.obj _).trans (mp_eqv X _ (e.front i hi hik)) ((X.obj _).symm ((H.x i).ψ_nat g e.τ)))
  back :=
    (X.obj _).trans (mp_ceq' X _ _ g (inK (n + 2) m) (fun j _ => by
        show jmap (dId (n + 2)) g (j + (n + 2 + 1)) = g.f j + (n + 2 + 1)
        rw [jmap_back]; exact Nat.add_comm _ _) e.w)
      ((X.obj _).trans (mp_eqv X g e.back) ((X.obj _).symm (p.nat g e.τ)))
  base :=
    (X.obj _).trans (mp_ceq' X (inD (n + 2) m') (J (dId (n + 2)) g) (dId (n + 2)) (inD (n + 2) m)
        (fun i hi => jmap_front _ _ (Nat.lt_succ_of_le hi)) e.w)
      ((X.obj _).trans (mp_idl X (dId (n + 2)) (fun _ _ => rfl) _) e.base)

variable (H σ)

/-- the simplicial set of admissible values -/
def EWS : SSet.{w} where
  obj := fun (m : Nat) => ⟨EW H σ m, fun e e' => (K.obj m).eqv e.τ e'.τ ∧ (X.obj (n + 2 + 1 + m : Nat)).eqv e.w e'.w,
    fun _ => ⟨(K.obj m).refl _, (X.obj _).refl _⟩, fun h => ⟨(K.obj m).symm h.1, (X.obj _).symm h.2⟩,
    fun h h' => ⟨(K.obj m).trans h.1 h'.1, (X.obj _).trans h.2 h'.2⟩⟩
  map g e := EW.act g e
  map_eqv {_ _} g {_ _} h := ⟨K.map_eqv g h.1, mp_eqv X _ h.2⟩
  map_resp {_ _ _ _} e h := ⟨K.map_resp e.τ h, mp_resp X (J_resp (fun _ _ => rfl) h) e.w⟩
  map_id m e := ⟨K.map_id m e.τ, mp_idl X _ (J_id (n + 2) m) e.w⟩
  map_comp f g e := ⟨K.map_comp f g e.τ,
    (X.obj _).trans (mp_resp X (f := J (dId (n + 2)) (dComp f g)) (g := dComp (J (dId (n + 2)) f) (J (dId (n + 2)) g))
      (fun i hi => (J_comp (dId (n + 2)) (dId (n + 2)) f g i hi).symm) e.w) ((X.obj _).symm (mp_comp X _ _ e.w))⟩

/-- the projection to K -/
def piW : PNat (EWS H σ) K where
  app _ e := e.τ
  app_eqv _ {_ _} h := h.1
  nat _ _ := (K.obj _).refl _

variable {H σ} (hσ : ∀ i, i ≤ n + 2 → i ≠ k → (X.obj (n + 1 : Nat)).eqv (dd X i σ) ((H.x i).σ))
  (hk0 : 0 < k) (hk : k < n + 2) (hQ : Quasi X)

theorem pickL {A : Type w} (F : Nat → A) (a : A) (c : Nat) : pick F (fun _ => a) c c = a := by
  have h := pick_back (fun _ => a) c F 0
  rw [Nat.zero_add] at h
  exact h

include hσ in
/-- the horn over a vertex of K -/
def hornV (τ : (K.obj (0 : Nat)).car) : Horn X (n + 1) k where
  x := pick (fun i => (H.x i).ψ 0 τ) (fun _ => σ) (n + 3)
  compat i j hij hj hi hj1 := match Nat.lt_or_ge (j + 1) (n + 3) with
    | .inl h => by
      rw [pick_front _ _ _ _ h, pick_front _ _ _ _ (Nat.lt_of_le_of_lt hij (Nat.lt_of_succ_lt h))]
      exact (X.obj _).trans (mp_resp X (g := J (face n i) (dId 0)) (fun q _ => (J_face_front 0 (Nat.le_trans hij
          (Nat.le_of_lt_succ (Nat.lt_of_succ_lt_succ h))) q).symm) _)
        ((X.obj _).trans ((H.compat i j hij (Nat.le_of_lt_succ (Nat.lt_of_succ_lt_succ h)) hi hj1).2 0 τ)
          (mp_resp X (g := face (n + 1) j) (fun q _ => J_face_front 0 (Nat.le_of_lt_succ (Nat.lt_of_succ_lt_succ h)) q) _))
    | .inr h => by
      have hj' : j = n + 2 := Nat.le_antisymm hj (Nat.le_of_succ_le_succ h)
      cases hj'
      have e1 : pick (fun i => (H.x i).ψ 0 τ) (fun _ => σ) (n + 3) (n + 2 + 1) = σ := pickL _ σ (n + 3)
      rw [e1, pick_front _ _ _ _ (Nat.lt_succ_of_le hij)]
      exact (X.obj _).trans (hσ i hij hi) ((X.obj _).symm ((X.obj _).trans
        (mp_resp X (g := inD (n + 1) 0) (fun q hq => sk_lt (Nat.lt_succ_of_le hq)) _) ((H.x i).ψ_D 0 τ)))

include hσ hk0 hk hQ in
/-- vertex lifts -/
theorem liftV (τ : (K.obj (0 : Nat)).car) :
    ∃ e : (EWS H σ).obj (0 : Nat) |>.car, (K.obj (0 : Nat)).eqv ((piW H σ).app (0 : Nat) e) τ :=
  match hQ (n + 1) k hk0 (Nat.lt_trans hk (Nat.lt_succ_self _)) (hornV hσ τ) with
  | ⟨t, ht⟩ =>
    have hne : ∀ i, i ≤ n + 2 → i ≠ k → (X.obj (n + 2 : Nat)).eqv (dd X i t) ((H.x i).ψ 0 τ) := fun i hi hik =>
      (pick_front (fun _ => σ) (n + 3) (fun i => (H.x i).ψ 0 τ) i (Nat.lt_succ_of_le hi)) ▸ ht i (Nat.le_succ_of_le hi) hik
    ⟨⟨τ, t,
      fun i hi hik => (X.obj _).trans (mp_resp X (g := face (n + 2) i) (fun q _ => J_face_front 0 hi q) t) (hne i hi hik),
      (X.obj _).trans (mp_ceq' X (inK (n + 1) 0) (face (n + 2) 0) (dId 0) (inK (n + 2) 0) (fun q hq => by
          show sk 0 (q + (n + 1 + 1)) = q + (n + 2 + 1)
          rw [sk_ge (Nat.zero_le _)]; rfl) t |> fun h => (X.obj _).trans ((X.obj _).symm (mp_idl X (dId 0) (fun _ _ => rfl) _))
            ((X.obj _).symm h))
        ((X.obj _).trans (mp_eqv X _ (hne 0 (Nat.zero_le _) (fun h : 0 = k => Nat.lt_irrefl 0 (h.symm ▸ hk0 : 0 < 0)))) ((H.x 0).ψ_K 0 τ)),
      (X.obj _).trans (mp_resp X (g := face (n + 2) (n + 3)) (fun q hq => (sk_lt (Nat.lt_succ_of_le hq)).symm) t)
        ((pickL (fun i => (H.x i).ψ 0 τ) σ (n + 3)) ▸
          ht (n + 3) (Nat.le_refl _) (fun h : n + 3 = k => Nat.not_lt_of_ge (Nat.le_succ (n + 2)) (h.symm ▸ hk : n + 3 < n + 2)))⟩,
     (K.obj _).refl _⟩

end IC

end Found

#print axioms Found.IC.EWS
#print axioms Found.IC.piW
#print axioms Found.IC.liftV

found/IC17.lean

lean_all/found/IC17.lean

/-
  T153 ((infinity,1)-categories), part 17. found/IC17 — filling inner horns
  of a slice X_/p, II: the boundary lifts of EW -> K.  Over an
  (m'+1)-simplex τ of K with admissible values b_j over its faces, the
  front faces (from the horn H) and the back faces (the b_j) form an inner
  horn Lambda^(n+3+m')_k of X (the join Lambda^(n+2)_k * Delta^(m'+1) union
  Delta^(n+2) * ∂Delta^(m'+1)); its filler is an admissible value over τ
  with the given faces.  Index bookkeeping by identity maps castD.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.IC16

namespace Found

namespace IC

open Simp

universe w

variable {K X : SSet.{w}} {p : PNat K X} {n k : Nat} {H : Horn (Slice p) n k} {σ : (X.obj (n + 2 : Nat)).car}
  (hσ : ∀ i, i ≤ n + 2 → i ≠ k → (X.obj (n + 1 : Nat)).eqv (dd X i σ) ((H.x i).σ))
  (hk0 : 0 < k) (hk : k < n + 2) (hQ : Quasi X)

theorem succ_add' (a b : Nat) : a + 1 + b = a + b + 1 := Nat.succ_add a b

/-- X_(n+2+m'+1) -> X_(n+2+1+m') and back, and X_(n+2+1+(m'+1)) <- X_(n+2+m'+2) -/
def castB (n m' : Nat) : DHom (n + 2 + m' + 1) (n + 2 + 1 + m') :=
  castD _ _ (Nat.le_of_eq (succ_add' (n + 2) m').symm)
def castR (n m' : Nat) : DHom (n + 2 + 1 + m') (n + 2 + m' + 1) :=
  castD _ _ (Nat.le_of_eq (succ_add' (n + 2) m'))
def castT (n m' : Nat) : DHom (n + 2 + 1 + (m' + 1)) (n + 2 + m' + 2) :=
  castD _ _ (Nat.le_of_eq (congrArg Nat.succ (succ_add' (n + 2) m')))

variable (H σ)

/-- the horn over an (m'+1)-simplex with given back faces -/
def hornB (m' : Nat) (τ : (K.obj (m' + 1 : Nat)).car) (b : Nat → EW H σ m') :
    Nat → (X.obj (n + 2 + m' + 1 : Nat)).car :=
  pick (fun i => (H.x i).ψ (m' + 1) τ) (fun j => mp X (castB n m') (b j).w) (n + 3)

variable {H σ}

theorem front_eq (m' : Nat) (τ : (K.obj (m' + 1 : Nat)).car) (b : Nat → EW H σ m') {i : Nat} (hi : i < n + 3) :
    hornB H σ m' τ b i = (H.x i).ψ (m' + 1) τ := pick_front _ _ _ _ hi

theorem back_eq (m' : Nat) (τ : (K.obj (m' + 1 : Nat)).car) (b : Nat → EW H σ m') (q : Nat) :
    hornB H σ m' τ b (q + (n + 3)) = mp X (castB n m') (b q).w := pick_back _ _ _ _

theorem succ_cancel {a b : Nat} (h : a + 1 = b + 1) : a = b := Nat.noConfusion h id

def castX (n m' : Nat) : DHom (n + 2 + m') (n + 1 + (m' + 1)) :=
  castD _ _ (Nat.le_of_eq (succ_add' (n + 1) m'))

theorem idx_b {n m' j : Nat} (hj : j ≤ n + 2 + m' + 1) : j + 1 ≤ (m' + 1) + (n + 3) :=
  Nat.le_trans (Nat.succ_le_succ hj) (Nat.le_of_eq
    ((congrArg Nat.succ (succ_add' (n + 2) m')).symm.trans (Nat.add_comm (n + 3) (m' + 1))))

theorem idx_c {n m' j : Nat} (hj : j ≤ n + 2 + m' + 1) : j ≤ m' + (n + 3) :=
  Nat.le_trans hj (Nat.le_of_eq ((succ_add' (n + 2) m').symm.trans (Nat.add_comm (n + 3) m')))

/-- the horn of X over τ -/
def hornBH (m' : Nat) (τ : (K.obj (m' + 1 : Nat)).car) (b : Nat → EW H σ m')
    (hb : ∀ j, j ≤ m' + 1 → (K.obj (m' : Nat)).eqv (b j).τ (dd K j τ))
    (BB : ∀ q q'', q ≤ q'' → q'' ≤ m' →
      (X.obj (n + 2 + m' : Nat)).eqv (dd X (q + (n + 3)) (mp X (castB n m') (b (q'' + 1)).w))
        (dd X (q'' + (n + 3)) (mp X (castB n m') (b q).w))) : Horn X (n + 2 + m') k where
  x := hornB H σ m' τ b
  compat i j hij hj hi hj1 :=
    match split_ic (n + 3) (j + 1), split_ic (n + 3) i with
    | .inl h, _ => by
      have hj' : j ≤ n + 1 := Nat.le_of_lt_succ (Nat.lt_of_succ_lt_succ h)
      rw [front_eq m' τ b h, front_eq m' τ b (Nat.lt_of_le_of_lt hij (Nat.lt_of_succ_lt h))]
      exact (X.obj _).trans
        ((X.obj _).trans (mp_resp X (g := dComp (castX n m') (J (face n i) (dId (m' + 1))))
          (fun r _ => (J_face_front (m' + 1) (Nat.le_trans hij hj') r).symm) _) ((X.obj _).symm (mp_comp X _ _ _)))
        ((X.obj _).trans (mp_eqv X (castX n m') ((H.compat i j hij hj' hi hj1).2 (m' + 1) τ))
          ((X.obj _).trans (mp_comp X _ _ _)
            (mp_resp X (g := face (n + 2 + m') j) (fun r _ => J_face_front (m' + 1) hj' r) _)))
    | .inr ⟨q, e⟩, .inl hi3 => by
      have ej : j = q + (n + 2) := succ_cancel e
      have hq : q ≤ m' + 1 := le_cancel_r (n + 3) (Nat.le_trans (Nat.le_of_eq e.symm) (idx_b (n := n) (m' := m') hj))
      rw [e, back_eq m' τ b q, front_eq m' τ b hi3]
      have hi2 : i ≤ n + 2 := Nat.le_of_lt_succ hi3
      refine (X.obj _).trans (mp_comp X _ _ _) ((X.obj _).trans
        (mp_resp X (g := J (face (n + 1) i) (dId m')) (fun r _ => (J_face_front m' hi2 r).symm) _)
        ((X.obj _).trans ((b q).front i hi2 hi) ((X.obj _).trans ((H.x i).ψ_eqv m' (hb q hq))
          ((X.obj _).trans ((H.x i).ψ_nat (face m' q) τ) ?_))))
      rw [ej]
      exact mp_resp X (fun r _ => J_face_back (n + 1) r) _
    | .inr ⟨q', e⟩, .inr ⟨q, ei⟩ =>
      match q', e with
      | 0, e => absurd (Nat.le_trans (Nat.le_of_eq ei.symm) hij)
          (Nat.not_le_of_gt (Nat.lt_of_lt_of_le (Nat.lt_of_succ_le (Nat.le_of_eq e)) (Nat.add_le_add_right (Nat.zero_le q) _)))
      | q'' + 1, e => by
        have ej : j = q'' + (n + 3) := succ_cancel (e.trans (Nat.succ_add q'' (n + 3)))
        have hq : q ≤ q'' := le_cancel_r (n + 3) (Nat.le_trans (Nat.le_of_eq ei.symm) (Nat.le_trans hij (Nat.le_of_eq ej)))
        have hq'' : q'' ≤ m' := le_cancel_r (n + 3) (Nat.le_trans (Nat.le_of_eq ej.symm) (idx_c (n := n) (m' := m') hj))
        have hb2 : hornB H σ m' τ b (q'' + (n + 3) + 1) = mp X (castB n m') (b (q'' + 1)).w :=
          (congrArg (hornB H σ m' τ b) (Nat.succ_add q'' (n + 3)).symm).trans (back_eq m' τ b (q'' + 1))
        rw [ej, ei, hb2, back_eq m' τ b]
        exact BB q q'' hq hq''

end IC

end Found

#print axioms Found.IC.hornBH

found/IC18.lean

lean_all/found/IC18.lean

/-
  T153 ((infinity,1)-categories), part 18. found/IC18 — slices of
  quasi-categories are quasi-categories (Joyal; Lurie HTT 2.1.2.2), inside
  the classical point.  The filler of the horn of IC17 is an admissible
  value with the prescribed faces, so EW -> K has the boundary lifting
  property; its section (IC15) together with the Delta-part filler σ is a
  simplex of X_/p filling the given inner horn.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.IC17

namespace Found

namespace IC

open Simp

universe w

variable {K X : SSet.{w}} {p : PNat K X} {n k : Nat} {H : Horn (Slice p) n k} {σ : (X.obj (n + 2 : Nat)).car}
  (hσ : ∀ i, i ≤ n + 2 → i ≠ k → (X.obj (n + 1 : Nat)).eqv (dd X i σ) ((H.x i).σ))
  (hk0 : 0 < k) (hk : k < n + 2) (hQ : Quasi X)

theorem ne_back {k n q : Nat} (hk : k < n + 2) : q + (n + 3) ≠ k := fun h =>
  Nat.not_le_of_gt hk (Nat.le_trans (Nat.le_trans (Nat.le_succ _) (Nat.le_add_left _ q)) (Nat.le_of_eq h))

include hk0 hk hQ in
/-- the general boundary lift over an (m'+1)-simplex -/
theorem liftGen (m' : Nat) (τ : (K.obj (m' + 1 : Nat)).car) (b : Nat → EW H σ m')
    (hb : ∀ j, j ≤ m' + 1 → (K.obj (m' : Nat)).eqv (b j).τ (dd K j τ))
    (BB : ∀ q q'', q ≤ q'' → q'' ≤ m' →
      (X.obj (n + 2 + m' : Nat)).eqv (dd X (q + (n + 3)) (mp X (castB n m') (b (q'' + 1)).w))
        (dd X (q'' + (n + 3)) (mp X (castB n m') (b q).w))) :
    ∃ e : EW H σ (m' + 1), e.τ = τ ∧ ∀ j, j ≤ m' + 1 →
      ((EWS H σ).obj (m' : Nat)).eqv ((EWS H σ).map (a := (m' : Nat)) (b := (m' + 1 : Nat)) (face m' j) e) (b j) :=
  match hQ (n + 2 + m') k hk0 (Nat.lt_of_lt_of_le hk (Nat.le_trans (Nat.le_add_right _ m') (Nat.le_add_right _ 2)))
      (hornBH m' τ b hb BB) with
  | ⟨t, ht⟩ =>
    have hfr : ∀ i, i ≤ n + 2 → i ≠ k → (X.obj (n + 2 + m' + 1 : Nat)).eqv (dd X i t) ((H.x i).ψ (m' + 1) τ) :=
      fun i hi hik => (front_eq m' τ b (Nat.lt_succ_of_le hi)) ▸
        ht i (Nat.le_trans hi (Nat.le_trans (Nat.le_add_right _ m') (Nat.le_add_right _ 2))) hik
    have hbk : ∀ q, q ≤ m' + 1 → (X.obj (n + 2 + m' + 1 : Nat)).eqv (dd X (q + (n + 3)) t) (mp X (castB n m') (b q).w) :=
      fun q hq => (back_eq m' τ b q) ▸ ht (q + (n + 3)) (Nat.le_trans (Nat.add_le_add_right hq _)
        (Nat.le_of_eq ((Nat.add_comm _ _).trans (congrArg Nat.succ (succ_add' (n + 2) m'))))) (ne_back hk)
    ⟨⟨τ, mp X (castT n m') t,
      fun i hi hik => (X.obj _).trans (mp_comp X _ _ _)
        ((X.obj _).trans (mp_resp X (g := face (n + 2 + m' + 1) i) (fun r _ => J_face_front (m' + 1) hi r) t)
          (hfr i hi hik)),
      (X.obj _).trans (mp_ceq' X _ _ (inK (n + 1) (m' + 1)) (face (n + 2 + m' + 1) 0) (fun r _ => by
          show r + (n + 2 + 1) = sk 0 (r + (n + 1 + 1))
          rw [sk_ge (Nat.zero_le _)]; rfl) t)
        ((X.obj _).trans (mp_eqv X _ (hfr 0 (Nat.zero_le _) (fun h : 0 = k => Nat.lt_irrefl 0 (h.symm ▸ hk0 : 0 < 0))))
          ((H.x 0).ψ_K (m' + 1) τ)),
      (X.obj _).trans (mp_ceq' X _ _ (dComp (inD (n + 2) m') (castR n m')) (face (n + 2 + m' + 1) (0 + (n + 3)))
          (fun r hr => (sk_lt (Nat.lt_of_lt_of_le (Nat.lt_succ_of_le hr) (Nat.le_of_eq (Nat.zero_add _).symm))).symm) t)
        ((X.obj _).trans (mp_eqv X _ (hbk 0 (Nat.zero_le _)))
          ((X.obj _).trans (mp_comp X _ _ _) ((X.obj _).trans
            (mp_resp X (g := inD (n + 2) m') (fun _ _ => rfl) _) (b 0).base)))⟩,
     rfl,
     fun j hj => ⟨(K.obj _).symm (hb j hj),
      (X.obj _).trans (mp_ceq' X _ _ (castR n m') (face (n + 2 + m' + 1) (j + (n + 3)))
          (fun r _ => J_face_back (n + 2) r) t)
        ((X.obj _).trans (mp_eqv X _ (hbk j hj))
          ((X.obj _).trans (mp_comp X _ _ _) (mp_idl X _ (fun _ _ => rfl) _)))⟩⟩

include hk0 hk hQ in
theorem liftE1 (τ : (K.obj (1 : Nat)).car) (b0 b1 : ((EWS H σ).obj (0 : Nat)).car)
    (h0 : (K.obj (0 : Nat)).eqv ((piW H σ).app (0 : Nat) b0) (dd K 0 τ))
    (h1 : (K.obj (0 : Nat)).eqv ((piW H σ).app (0 : Nat) b1) (dd K 1 τ)) :
    ∃ e : ((EWS H σ).obj (1 : Nat)).car, (K.obj (1 : Nat)).eqv ((piW H σ).app (1 : Nat) e) τ ∧
      ((EWS H σ).obj (0 : Nat)).eqv (dd (EWS H σ) 0 e) b0 ∧ ((EWS H σ).obj (0 : Nat)).eqv (dd (EWS H σ) 1 e) b1 :=
  have hbase : ∀ (c : EW H σ 0), (X.obj (n + 2 + 0 : Nat)).eqv (dd X (0 + (n + 3)) (mp X (castB n 0) c.w)) σ :=
    fun c => (X.obj _).trans (mp_comp X _ _ _) ((X.obj _).trans (mp_resp X (g := inD (n + 2) 0) (fun _ hr =>
      sk_lt (Nat.lt_of_lt_of_le (Nat.lt_succ_of_le hr) (Nat.le_of_eq (Nat.zero_add _).symm))) _) c.base)
  match liftGen hk0 hk hQ 0 τ (fun j => match j with | 0 => b0 | _ + 1 => b1)
      (fun j hj => match j, hj with
        | 0, _ => h0
        | 1, _ => h1
        | _ + 2, h => absurd (Nat.le_of_succ_le_succ h) (Nat.not_succ_le_zero _))
      (fun q q'' hq hq'' => match q, q'', hq, hq'' with
        | 0, 0, _, _ => (X.obj _).trans (hbase b1) ((X.obj _).symm (hbase b0))
        | _, _ + 1, _, h => absurd h (Nat.not_succ_le_zero _)
        | _ + 1, 0, h, _ => absurd h (Nat.not_succ_le_zero _)) with
  | ⟨e, he, hf⟩ => ⟨e, he ▸ (K.obj _).refl _, hf 0 (Nat.zero_le _), hf 1 (Nat.le_refl _)⟩

def castBB (n n' : Nat) : DHom (n + 2 + (n' + 1)) (n + 2 + 1 + n') :=
  castD _ _ (Nat.le_of_eq (succ_add' (n + 2) n').symm)

include hk0 hk hQ in
theorem liftE2 (n' : Nat) (τ : (K.obj (n' + 2 : Nat)).car) (b : Bdry (EWS H σ) n')
    (hb : ∀ j, j ≤ n' + 2 → (K.obj (n' + 1 : Nat)).eqv ((piW H σ).app (n' + 1 : Nat) (b.x j)) (dd K j τ)) :
    ∃ e : ((EWS H σ).obj (n' + 2 : Nat)).car, (K.obj (n' + 2 : Nat)).eqv ((piW H σ).app (n' + 2 : Nat) e) τ ∧
      BFills b e :=
  match liftGen hk0 hk hQ (n' + 1) τ b.x hb (fun q q'' hq hq'' =>
      (X.obj _).trans (mp_ceq' X _ _ (castBB n n') (J (dId (n + 2)) (face n' q)) (fun r _ => (J_face_back (n + 2) r).symm) _)
        ((X.obj _).trans (mp_eqv X _ (b.compat q q'' hq hq'').2)
          (mp_ceq' X _ _ _ _ (fun r _ => J_face_back (n + 2) r) _))) with
  | ⟨e, he, hf⟩ => ⟨e, he ▸ (K.obj _).refl _, hf⟩

/-- slices of quasi-categories are quasi-categories (in the classical point) -/
theorem slice_quasi (Kw : ClW.{w}) (Kc : CP.Classical') {K X : SSet.{w}} (p : PNat K X) (hQ : Quasi X) :
    Quasi (Slice p) := fun n k hk0 hk H =>
  match hQ n k hk0 hk ⟨fun i => (H.x i).σ, fun i j hij hj hi hj1 => (H.compat i j hij hj hi hj1).1⟩ with
  | ⟨σ, hσ⟩ =>
    have hp := secN_proj Kw Kc (piW H σ) (liftV hσ hk0 hk hQ) (liftE1 hk0 hk hQ)
      (fun n' τ b hb => liftE2 hk0 hk hQ n' τ b hb)
    let s := secN Kw Kc (piW H σ) (liftV hσ hk0 hk hQ) (liftE1 hk0 hk hQ)
      (fun n' τ b hb => liftE2 hk0 hk hQ n' τ b hb)
    ⟨⟨σ, fun m τ => (s.app m τ).w, fun m {_ _} h => (s.app_eqv m h).2, fun g τ => (s.nat g τ).2,
       fun m τ => (X.obj _).trans (s.app m τ).back (p.app_eqv _ (hp m τ)), fun m τ => (s.app m τ).base⟩,
     fun i hi hik => ⟨hσ i hi hik, fun m τ =>
       (X.obj _).trans ((s.app m τ).front i hi hik) ((H.x i).ψ_eqv m (hp m τ))⟩⟩

end IC

end Found

#print axioms Found.IC.liftGen
#print axioms Found.IC.liftE1
#print axioms Found.IC.liftE2
#print axioms Found.IC.slice_quasi

found/IC19.lean

lean_all/found/IC19.lean

/-
  T153 ((infinity,1)-categories), part 19. found/IC19 — filling inner horns
  of a coslice X_p/, I (mirror of IC16).  Given an inner horn
  H : Lambda^(n+2)_k -> X_p/ and a filler σ of its Delta-part, admissible
  values over an m-simplex τ of K are w in X_(m+1+(n+2)) whose back faces
  are the ψ τ of the horn faces, whose front is p τ and whose back is σ;
  they form a simplicial set over K, and vertex lifts are inner horns of X.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.IC18

namespace Found

namespace IC

open Simp

universe w

variable {K X : SSet.{w}} {p : PNat K X} {n k : Nat} (H : Horn (Coslice p) n k) (σ : (X.obj (n + 2 : Nat)).car)

/-- admissible values under an m-simplex of K -/
structure EWc (m : Nat) : Type w where
  τ : (K.obj (m : Nat)).car
  w : (X.obj (m + 1 + (n + 2) : Nat)).car
  front : ∀ i, i ≤ n + 2 → i ≠ k →
    (X.obj (m + 1 + (n + 1) : Nat)).eqv (mp X (J (dId m) (face (n + 1) i)) w) ((H.x i).ψ m τ)
  back : (X.obj (m : Nat)).eqv (mp X (inD m (n + 2)) w) (p.app (m : Nat) τ)
  base : (X.obj (n + 2 : Nat)).eqv (mp X (inK m (n + 2)) w) σ

variable {H σ}

def EWc.act {m' m : Nat} (g : DHom m' m) (e : EWc H σ m) : EWc H σ m' where
  τ := mp K g e.τ
  w := mp X (J g (dId (n + 2))) e.w
  front i hi hik :=
    (X.obj _).trans (mp_ceq' X _ _ (J g (dId (n + 1))) (J (dId m) (face (n + 1) i)) (fun q hq =>
        (J_comp (dId m') g (face (n + 1) i) (dId (n + 2)) q hq).trans
          ((J_resp (x' := dComp g (dId m)) (y' := dComp (dId (n + 1)) (face (n + 1) i)) (fun _ _ => rfl)
            (fun _ _ => rfl) q hq).trans (J_comp g (dId m) (dId (n + 1)) (face (n + 1) i) q hq).symm)) e.w)
      ((X.obj _).trans (mp_eqv X _ (e.front i hi hik)) ((X.obj _).symm ((H.x i).ψ_nat g e.τ)))
  back :=
    (X.obj _).trans (mp_ceq' X (inD m' (n + 2)) (J g (dId (n + 2))) g (inD m (n + 2))
        (fun _ hr => jmap_front g _ (Nat.lt_succ_of_le hr)) e.w)
      ((X.obj _).trans (mp_eqv X g e.back) ((X.obj _).symm (p.nat g e.τ)))
  base :=
    (X.obj _).trans (mp_ceq' X (inK m' (n + 2)) (J g (dId (n + 2))) (dId (n + 2)) (inK m (n + 2))
        (fun r _ => (jmap_back g (dId (n + 2)) r).trans (Nat.add_comm _ _)) e.w)
      ((X.obj _).trans (mp_idl X (dId (n + 2)) (fun _ _ => rfl) _) e.base)

variable (H σ)

def EWcS : SSet.{w} where
  obj := fun (m : Nat) => ⟨EWc H σ m, fun e e' => (K.obj m).eqv e.τ e'.τ ∧ (X.obj (m + 1 + (n + 2) : Nat)).eqv e.w e'.w,
    fun _ => ⟨(K.obj m).refl _, (X.obj _).refl _⟩, fun h => ⟨(K.obj m).symm h.1, (X.obj _).symm h.2⟩,
    fun h h' => ⟨(K.obj m).trans h.1 h'.1, (X.obj _).trans h.2 h'.2⟩⟩
  map g e := EWc.act g e
  map_eqv {_ _} g {_ _} h := ⟨K.map_eqv g h.1, mp_eqv X _ h.2⟩
  map_resp {_ _ _ _} e h := ⟨K.map_resp e.τ h, mp_resp X (J_resp h (fun _ _ => rfl)) e.w⟩
  map_id m e := ⟨K.map_id m e.τ, mp_idl X _ (J_id m (n + 2)) e.w⟩
  map_comp f g e := ⟨K.map_comp f g e.τ,
    (X.obj _).trans (mp_resp X (f := J (dComp f g) (dId (n + 2))) (g := dComp (J f (dId (n + 2))) (J g (dId (n + 2))))
      (fun i hi => (J_comp f g (dId (n + 2)) (dId (n + 2)) i hi).symm) e.w) ((X.obj _).symm (mp_comp X _ _ e.w))⟩

def piWc : PNat (EWcS H σ) K where
  app _ e := e.τ
  app_eqv _ {_ _} h := h.1
  nat _ _ := (K.obj _).refl _

variable {H σ} (hσ : ∀ i, i ≤ n + 2 → i ≠ k → (X.obj (n + 1 : Nat)).eqv (dd X i σ) ((H.x i).σ))
  (hk0 : 0 < k) (hk : k < n + 2) (hQ : Quasi X)

/-- identity maps between equal dimensions -/
def cv (a b : Nat) (h : a = b) : DHom a b := castD a b (Nat.le_of_eq h)

theorem one_add (n : Nat) : 0 + 1 + n = n + 1 := by rw [Nat.zero_add, Nat.add_comm]

include hσ in
/-- the horn under a vertex of K -/
def hornVc (τ : (K.obj (0 : Nat)).car) : Horn X (n + 1) (k + 1) where
  x := pick (fun _ => σ) (fun i => mp X (cv (n + 2) (0 + 1 + (n + 1)) (one_add (n + 1)).symm) ((H.x i).ψ 0 τ)) 1
  compat i j hij hj hi hj1 := match i, j, hij, hj, hi, hj1 with
    | 0, j, _, hj, _, hj1 => by
      have hjk : j ≠ k := fun h => hj1 (congrArg (· + 1) h)
      show (X.obj _).eqv (dd X 0 (pick _ _ 1 (j + 1))) (dd X j (pick _ _ 1 0))
      rw [pick_back _ 1 _ j, pick_front _ 1 _ 0 (Nat.succ_pos 0)]
      exact (X.obj _).trans (mp_comp X _ _ _) ((X.obj _).trans
        (mp_resp X (g := inK 0 (n + 1)) (fun r _ => sk_ge (Nat.zero_le r)) _)
        ((X.obj _).trans ((H.x j).ψ_D 0 τ) ((X.obj _).symm (hσ j hj hjk))))
    | i' + 1, j, hij, hj, hi, hj1 =>
      match j, hij, hj, hj1 with
      | 0, h, _, _ => absurd h (Nat.not_succ_le_zero _)
      | j' + 1, hij, hj, hj1 => by
        have hik : i' ≠ k := fun h => hi (congrArg (· + 1) h)
        have hjk : j' + 1 ≠ k := fun h => hj1 (congrArg (· + 1) h)
        show (X.obj _).eqv (dd X (i' + 1) (pick _ _ 1 (j' + 1 + 1))) (dd X (j' + 1) (pick _ _ 1 (i' + 1)))
        rw [pick_back _ 1 _ (j' + 1), pick_back _ 1 _ i']
        exact (X.obj _).trans (mp_comp X _ _ _) ((X.obj _).trans
          (mp_resp X (g := dComp (cv (n + 1) (0 + 1 + n) (one_add n).symm) (J (dId 0) (face n i')))
            (fun r _ => (J_face_back 0 r).symm) _)
          ((X.obj _).trans ((X.obj _).symm (mp_comp X _ _ _))
          ((X.obj _).trans (mp_eqv X _ ((H.compat i' j' (Nat.le_of_succ_le_succ hij)
              (Nat.le_of_succ_le_succ hj) hik hjk).2 0 τ))
          ((X.obj _).trans (mp_comp X _ _ _)
          ((X.obj _).trans (mp_resp X (g := dComp (face (n + 1) (j' + 1)) (cv (n + 2) (0 + 1 + (n + 1)) (one_add (n + 1)).symm))
            (fun r _ => J_face_back 0 r) _)
          ((X.obj _).symm (mp_comp X _ _ _)))))))

include hσ hk0 hk hQ in
/-- vertex lifts under K -/
theorem liftVc (τ : (K.obj (0 : Nat)).car) :
    ∃ e : ((EWcS H σ).obj (0 : Nat)).car, (K.obj (0 : Nat)).eqv ((piWc H σ).app (0 : Nat) e) τ :=
  match hQ (n + 1) (k + 1) (Nat.succ_pos k) (Nat.succ_lt_succ hk) (hornVc hσ τ) with
  | ⟨t, ht⟩ =>
    have hx0 : (X.obj (n + 2 : Nat)).eqv (dd X 0 t) σ :=
      (pick_front (fun i => mp X (cv (n + 2) (0 + 1 + (n + 1)) (one_add (n + 1)).symm) ((H.x i).ψ 0 τ)) 1
        (fun _ => σ) 0 (Nat.succ_pos 0)) ▸ ht 0 (Nat.zero_le _) (fun h => Nat.noConfusion h)
    have hxi : ∀ i, i ≤ n + 2 → i ≠ k → (X.obj (n + 2 : Nat)).eqv (dd X (i + 1) t)
        (mp X (cv (n + 2) (0 + 1 + (n + 1)) (one_add (n + 1)).symm) ((H.x i).ψ 0 τ)) := fun i hi hik =>
      (pick_back (fun i => mp X (cv (n + 2) (0 + 1 + (n + 1)) (one_add (n + 1)).symm) ((H.x i).ψ 0 τ)) 1
        (fun _ => σ) i) ▸ ht (i + 1) (Nat.succ_le_succ hi) (fun h => hik (Nat.noConfusion h id))
    ⟨⟨τ, mp X (cv (0 + 1 + (n + 2)) (n + 3) (one_add (n + 2))) t,
      fun i hi hik => (X.obj _).trans
        (mp_ceq' X _ _ (cv (0 + 1 + (n + 1)) (n + 2) (one_add (n + 1))) (face (n + 2) (i + 1))
          (fun r _ => J_face_back 0 r) t)
        ((X.obj _).trans (mp_eqv X _ (hxi i hi hik))
          ((X.obj _).trans (mp_comp X _ _ _) (mp_idl X _ (fun _ _ => rfl) _))),
      (X.obj _).trans
        (mp_ceq' X _ _ (dComp (inD 0 (n + 1)) (cv (0 + 1 + (n + 1)) (n + 2) (one_add (n + 1)))) (face (n + 2) 1)
          (fun _ hr => (sk_lt (Nat.lt_succ_of_le hr)).symm) t)
        ((X.obj _).trans (mp_eqv X _ (hxi 0 (Nat.zero_le _) (fun h : 0 = k => Nat.lt_irrefl 0 (h.symm ▸ hk0 : 0 < 0))))
          ((X.obj _).trans (mp_comp X _ _ _)
            ((X.obj _).trans (mp_resp X (g := inD 0 (n + 1)) (fun _ _ => rfl) _) ((H.x 0).ψ_K 0 τ)))),
      (X.obj _).trans
        (mp_ceq' X _ _ (dId (n + 2)) (face (n + 2) 0) (fun r _ => (sk_ge (Nat.zero_le r)).symm) t)
        ((X.obj _).trans (mp_idl X _ (fun _ _ => rfl) _) hx0)⟩,
     (K.obj _).refl _⟩

end IC

end Found

#print axioms Found.IC.EWcS
#print axioms Found.IC.hornVc
#print axioms Found.IC.liftVc

found/IC2.lean

lean_all/found/IC2.lean

/-
  T153 ((infinity,1)-categories), part 2. found/IC2 — the homotopy category
  of a quasi-category (Boardman–Vogt): degeneracies; triangles Tri a b c
  (a 2-simplex with edges a, b and composite c); the two 3-dimensional inner
  horn lemmas; homotopy of edges is an equivalence relation, composition
  (a chosen inner 2-horn filler) is well defined, unital and associative up
  to homotopy, giving a category hX whose objects are the vertices.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.IC1

namespace Found

namespace IC

open Simp

universe w

/-! ## degeneracies -/

/-- the i-th codegeneracy [n+1] -> [n] repeats i -/
def dg (i j : Nat) : Nat := cond (gtb (i + 1) j) j (Nat.pred j)

theorem dg_lt {i j : Nat} (h : j < i + 1) : dg i j = j :=
  congrArg (fun b => cond b j (Nat.pred j)) (lt_gtb (i + 1) j h)

theorem dg_ge {i j : Nat} (h : i + 1 ≤ j) : dg i j = Nat.pred j :=
  congrArg (fun b => cond b j (Nat.pred j)) (le_gtb (i + 1) j h)

theorem dg_mono (i j : Nat) : dg i j ≤ dg i (j + 1) :=
  match Nat.lt_or_ge j (i + 1) with
  | .inl h => match Nat.lt_or_ge (j + 1) (i + 1) with
    | .inl h' => by rw [dg_lt h, dg_lt h']; exact Nat.le_succ j
    | .inr h' => by rw [dg_lt h, dg_ge h']; exact Nat.le_refl j
  | .inr h => by
    rw [dg_ge h, dg_ge (Nat.le_trans h (Nat.le_succ j))]
    exact Nat.pred_le j

theorem dg_bound {n i : Nat} (hi : i ≤ n) (j : Nat) (hj : j ≤ n + 1) : dg i j ≤ n :=
  match Nat.lt_or_ge j (i + 1) with
  | .inl h => by rw [dg_lt h]; exact Nat.le_trans (Nat.le_of_lt_succ h) hi
  | .inr h => by
    rw [dg_ge h]
    exact Nat.le_of_succ_le_succ (Nat.le_trans (Nat.le_of_eq (Nat.succ_pred_eq_of_pos
      (Nat.lt_of_lt_of_le (Nat.succ_pos i) h))) hj)

def degen (n i : Nat) (hi : i ≤ n) : DHom (n + 1) n :=
  ⟨dg i, fun j _ => dg_mono i j, fun j hj => dg_bound hi j hj⟩

variable (X : SSet.{w})

abbrev V := X.obj (0 : Nat)
abbrev E := X.obj (1 : Nat)
abbrev T := X.obj (2 : Nat)

/-- s_0 on vertices: identity edges -/
def idv (y : (V X).car) : (E X).car := X.map (a := (1 : Nat)) (b := (0 : Nat)) (degen 0 0 (Nat.le_refl 0)) y
def s0e (f : (E X).car) : (T X).car := X.map (a := (2 : Nat)) (b := (1 : Nat)) (degen 1 0 (Nat.zero_le 1)) f
def s1e (f : (E X).car) : (T X).car := X.map (a := (2 : Nat)) (b := (1 : Nat)) (degen 1 1 (Nat.le_refl 1)) f

theorem le0_cases {P : Nat → Prop} (h0 : P 0) : ∀ p, p ≤ 0 → P p
  | 0, _ => h0
  | q + 1, h => absurd h (Nat.not_succ_le_zero q)

theorem le1_cases {P : Nat → Prop} (h0 : P 0) (h1 : P 1) : ∀ p, p ≤ 1 → P p
  | 0, _ => h0
  | 1, _ => h1
  | q + 2, h => absurd (Nat.le_of_succ_le_succ h) (Nat.not_succ_le_zero q)

/-- a composite of maps that is the identity acts as the identity -/
theorem map_cid {n m : Nat} (a : DHom n m) (b : DHom m n)
    (h : Delta.heq (a := (n : Nat)) (b := (n : Nat)) (Delta.comp (b := (m : Nat)) a b) (Delta.id (n : Nat)))
    (x : (X.obj (n : Nat)).car) : (X.obj (n : Nat)).eqv (X.map a (X.map b x)) x :=
  (X.obj (n : Nat)).trans ((X.obj (n : Nat)).symm (X.map_comp a b x))
    ((X.obj (n : Nat)).trans (X.map_resp x h) (X.map_id _ x))

/-- equal composites act equally -/
theorem map_ceq {n m m' k : Nat} (a : DHom n m) (b : DHom m k) (c : DHom n m') (d : DHom m' k)
    (h : Delta.heq (a := (n : Nat)) (b := (k : Nat)) (Delta.comp (b := (m : Nat)) a b) (Delta.comp (b := (m' : Nat)) c d))
    (x : (X.obj (k : Nat)).car) : (X.obj (n : Nat)).eqv (X.map a (X.map b x)) (X.map c (X.map d x)) :=
  (X.obj (n : Nat)).trans ((X.obj (n : Nat)).symm (X.map_comp a b x))
    ((X.obj (n : Nat)).trans (X.map_resp x h) (X.map_comp c d x))

theorem d0_idv (y : (V X).car) : (V X).eqv (dd X 0 (idv X y)) y :=
  map_cid X (face 0 0) (degen 0 0 (Nat.le_refl 0)) (le0_cases rfl) y
theorem d1_idv (y : (V X).car) : (V X).eqv (dd X 1 (idv X y)) y :=
  map_cid X (face 0 1) (degen 0 0 (Nat.le_refl 0)) (le0_cases rfl) y
theorem d0_s0e (f : (E X).car) : (E X).eqv (dd X 0 (s0e X f)) f :=
  map_cid X (face 1 0) (degen 1 0 (Nat.zero_le 1)) (le1_cases rfl rfl) f
theorem d1_s0e (f : (E X).car) : (E X).eqv (dd X 1 (s0e X f)) f :=
  map_cid X (face 1 1) (degen 1 0 (Nat.zero_le 1)) (le1_cases rfl rfl) f
theorem d2_s0e (f : (E X).car) : (E X).eqv (dd X 2 (s0e X f)) (idv X (dd X 1 f)) :=
  map_ceq X (face 1 2) (degen 1 0 (Nat.zero_le 1)) (degen 0 0 (Nat.le_refl 0)) (face 0 1) (le1_cases rfl rfl) f
theorem d0_s1e (f : (E X).car) : (E X).eqv (dd X 0 (s1e X f)) (idv X (dd X 0 f)) :=
  map_ceq X (face 1 0) (degen 1 1 (Nat.le_refl 1)) (degen 0 0 (Nat.le_refl 0)) (face 0 0) (le1_cases rfl rfl) f
theorem d1_s1e (f : (E X).car) : (E X).eqv (dd X 1 (s1e X f)) f :=
  map_cid X (face 1 1) (degen 1 1 (Nat.le_refl 1)) (le1_cases rfl rfl) f
theorem d2_s1e (f : (E X).car) : (E X).eqv (dd X 2 (s1e X f)) f :=
  map_cid X (face 1 2) (degen 1 1 (Nat.le_refl 1)) (le1_cases rfl rfl) f

/-! ## triangles and the two 3-dimensional horn lemmas -/

/-- a 2-simplex with d2 = a, d0 = b, d1 = c -/
def Tri (a b c : (E X).car) : Prop :=
  ∃ σ : (T X).car, (E X).eqv (dd X 2 σ) a ∧ (E X).eqv (dd X 0 σ) b ∧ (E X).eqv (dd X 1 σ) c

variable {X}

theorem tri_resp {a b c a' b' c' : (E X).car} (t : Tri X a b c) (ha : (E X).eqv a a') (hb : (E X).eqv b b')
    (hc : (E X).eqv c c') : Tri X a' b' c' :=
  match t with
  | ⟨σ, h2, h0, h1⟩ => ⟨σ, (E X).trans h2 ha, (E X).trans h0 hb, (E X).trans h1 hc⟩

theorem no3 (q : Nat) : ¬ q + 3 ≤ 1 + 1 := fun h =>
  Nat.not_succ_le_zero q (Nat.le_of_succ_le_succ (Nat.le_of_succ_le_succ h))

/-- the horn Lambda^3_1 from faces x0, x2, x3 -/
def h31 (x0 x2 x3 : (T X).car) (c01 : (E X).eqv (dd X 0 x2) (dd X 1 x0))
    (c02 : (E X).eqv (dd X 0 x3) (dd X 2 x0)) (c22 : (E X).eqv (dd X 2 x3) (dd X 2 x2)) : Horn X 1 1 where
  x i := match i with
    | 0 => x0
    | 1 => x0
    | 2 => x2
    | _ + 3 => x3
  compat i j hij hj hi hj1 := match i, j, hij, hj, hi, hj1 with
    | 0, 0, _, _, _, h => absurd rfl h
    | 0, 1, _, _, _, _ => c01
    | 0, 2, _, _, _, _ => c02
    | 1, _, _, _, h, _ => absurd rfl h
    | 2, 0, h, _, _, _ => absurd h (Nat.not_succ_le_zero 1)
    | 2, 1, h, _, _, _ => absurd (Nat.le_of_succ_le_succ h) (Nat.not_succ_le_zero 0)
    | 2, 2, _, _, _, _ => c22
    | q + 3, _, h, hj, _, _ => absurd (Nat.le_trans h hj) (no3 q)
    | 0, q + 3, _, hj, _, _ => absurd hj (no3 q)
    | 2, q + 3, _, hj, _, _ => absurd hj (no3 q)

/-- the horn Lambda^3_2 from faces x0, x1, x3 -/
def h32 (x0 x1 x3 : (T X).car) (c00 : (E X).eqv (dd X 0 x1) (dd X 0 x0))
    (c02 : (E X).eqv (dd X 0 x3) (dd X 2 x0)) (c12 : (E X).eqv (dd X 1 x3) (dd X 2 x1)) : Horn X 1 2 where
  x i := match i with
    | 0 => x0
    | 1 => x1
    | 2 => x0
    | _ + 3 => x3
  compat i j hij hj hi hj1 := match i, j, hij, hj, hi, hj1 with
    | 0, 0, _, _, _, _ => c00
    | 0, 1, _, _, _, h => absurd rfl h
    | 0, 2, _, _, _, _ => c02
    | 1, 0, h, _, _, _ => absurd h (Nat.not_succ_le_zero 0)
    | 1, 1, _, _, _, h => absurd rfl h
    | 1, 2, _, _, _, _ => c12
    | 2, _, _, _, h, _ => absurd rfl h
    | q + 3, _, h, hj, _, _ => absurd (Nat.le_trans h hj) (no3 q)
    | 0, q + 3, _, hj, _, _ => absurd hj (no3 q)
    | 1, q + 3, _, hj, _, _ => absurd hj (no3 q)

variable (hQ : Quasi X)

theorem ne31 : (3 : Nat) ≠ 1 := fun h => Nat.noConfusion h (fun h => Nat.noConfusion h)
theorem ne21 : (2 : Nat) ≠ 1 := fun h => Nat.noConfusion h (fun h => Nat.noConfusion h)
theorem ne01 : (0 : Nat) ≠ 1 := fun h => Nat.noConfusion h
theorem ne32 : (3 : Nat) ≠ 2 := fun h => Nat.noConfusion h (fun h => Nat.noConfusion h (fun h => Nat.noConfusion h))
theorem ne12 : (1 : Nat) ≠ 2 := fun h => Nat.noConfusion h (fun h => Nat.noConfusion h)
theorem ne02 : (0 : Nat) ≠ 2 := fun h => Nat.noConfusion h

include hQ in
/-- Lambda^3_1: faces d3 = (a,b,c), d0 = (b,k,l), d2 = (a,l,m) give d1 = (c,k,m) -/
theorem horn31 {a b c k l m : (E X).car} (t3 : Tri X a b c) (t0 : Tri X b k l) (t2 : Tri X a l m) :
    Tri X c k m :=
  match t3, t0, t2 with
  | ⟨σ3, a3, b3, c3⟩, ⟨σ0, b0, k0, l0⟩, ⟨σ2, a2, l2, m2⟩ =>
    match hQ 1 1 (Nat.succ_pos 0) (Nat.lt_trans (Nat.lt_succ_self 1) (Nat.lt_succ_self 2))
      (h31 σ0 σ2 σ3 ((E X).trans l2 ((E X).symm l0)) ((E X).trans b3 ((E X).symm b0))
        ((E X).trans a3 ((E X).symm a2))) with
    | ⟨τ, hτ⟩ =>
      have f0 := hτ 0 (Nat.zero_le 3) ne01
      have f2 := hτ 2 (Nat.le_succ 2) ne21
      have f3 := hτ 3 (Nat.le_refl 3) ne31
      ⟨dd X 1 τ,
        (E X).trans ((E X).symm (dd_dd X (Nat.le_succ 1) τ)) ((E X).trans (X.map_eqv (face 1 1) f3) c3),
        (E X).trans (dd_dd X (Nat.le_refl 0) τ) ((E X).trans (X.map_eqv (face 1 0) f0) k0),
        (E X).trans ((E X).symm (dd_dd X (Nat.le_refl 1) τ)) ((E X).trans (X.map_eqv (face 1 1) f2) m2)⟩

include hQ in
/-- Lambda^3_2: faces d3 = (a,b,c), d0 = (b,k,l), d1 = (c,k,m) give d2 = (a,l,m) -/
theorem horn32 {a b c k l m : (E X).car} (t3 : Tri X a b c) (t0 : Tri X b k l) (t1 : Tri X c k m) :
    Tri X a l m :=
  match t3, t0, t1 with
  | ⟨σ3, a3, b3, c3⟩, ⟨σ0, b0, k0, l0⟩, ⟨σ1, c1, k1, m1⟩ =>
    match hQ 1 2 (Nat.succ_pos 1) (Nat.lt_succ_self 2)
      (h32 σ0 σ1 σ3 ((E X).trans k1 ((E X).symm k0)) ((E X).trans b3 ((E X).symm b0))
        ((E X).trans c3 ((E X).symm c1))) with
    | ⟨τ, hτ⟩ =>
      have f0 := hτ 0 (Nat.zero_le 3) ne02
      have f1 := hτ 1 (Nat.le_succ_of_le (Nat.le_succ 1)) ne12
      have f3 := hτ 3 (Nat.le_refl 3) ne32
      ⟨dd X 2 τ,
        (E X).trans ((E X).symm (dd_dd X (Nat.le_refl 2) τ)) ((E X).trans (X.map_eqv (face 1 2) f3) a3),
        (E X).trans (dd_dd X (Nat.zero_le 1) τ) ((E X).trans (X.map_eqv (face 1 1) f0) l0),
        (E X).trans (dd_dd X (Nat.le_refl 1) τ) ((E X).trans (X.map_eqv (face 1 1) f1) m1)⟩

/-! ## degenerate triangles -/

theorem triL (f : (E X).car) : Tri X (idv X (dd X 1 f)) f f := ⟨s0e X f, d2_s0e X f, d0_s0e X f, d1_s0e X f⟩
theorem triR (f : (E X).car) : Tri X f (idv X (dd X 0 f)) f := ⟨s1e X f, d2_s1e X f, d0_s1e X f, d1_s1e X f⟩

theorem idv_eqv {y y' : (V X).car} (h : (V X).eqv y y') : (E X).eqv (idv X y) (idv X y') :=
  X.map_eqv (degen 0 0 (Nat.le_refl 0)) h

/-! ## the homotopy category -/

/-- edges from x to y -/
def Mor (x y : (V X).car) : Type w := { f : (E X).car // (V X).eqv (dd X 1 f) x ∧ (V X).eqv (dd X 0 f) y }

/-- homotopy of edges x -> y -/
def Hty {x y : (V X).car} (f g : Mor x y) : Prop := Tri X f.1 (idv X y) g.1

theorem triR' {y : (V X).car} (f : (E X).car) (hf : (V X).eqv (dd X 0 f) y) : Tri X f (idv X y) f :=
  tri_resp (triR f) ((E X).refl _) (idv_eqv hf) ((E X).refl _)

theorem triL' {x : (V X).car} (f : (E X).car) (hf : (V X).eqv (dd X 1 f) x) : Tri X (idv X x) f f :=
  tri_resp (triL f) (idv_eqv hf) ((E X).refl _) ((E X).refl _)

theorem tri_ids (y : (V X).car) : Tri X (idv X y) (idv X y) (idv X y) := triR' (idv X y) (d0_idv X y)

include hQ in
theorem hty_symm {y : (V X).car} {f g : (E X).car} (hf : (V X).eqv (dd X 0 f) y)
    (h : Tri X f (idv X y) g) : Tri X g (idv X y) f :=
  horn31 hQ h (tri_ids y) (triR' f hf)

include hQ in
theorem hty_trans {y : (V X).car} {f g h : (E X).car}
    (p : Tri X f (idv X y) g) (q : Tri X g (idv X y) h) : Tri X f (idv X y) h :=
  horn32 hQ p (tri_ids y) q

include hQ in
/-- composites are unique up to homotopy -/
theorem tri_uniq {z : (V X).car} {f g h h' : (E X).car} (hg : (V X).eqv (dd X 0 g) z)
    (t : Tri X f g h) (t' : Tri X f g h') : Tri X h (idv X z) h' :=
  horn31 hQ t (triR' g hg) t'

include hQ in
/-- triangles respect homotopy in the second edge -/
theorem tri_resp2 {z : (V X).car} {f g g' h : (E X).car} (hh : (V X).eqv (dd X 0 h) z)
    (t : Tri X f g h) (e : Tri X g (idv X z) g') : Tri X f g' h :=
  horn32 hQ t e (triR' h hh)

include hQ in
/-- triangles respect homotopy in the first edge -/
theorem tri_resp1 {x y : (V X).car} {f f' g h : (E X).car} (hf1 : (V X).eqv (dd X 1 f) x)
    (hf0 : (V X).eqv (dd X 0 f) y) (hh : (V X).eqv (dd X 1 h) x)
    (t : Tri X f g h) (e : Tri X f (idv X y) f') : Tri X f' g h :=
  horn31 hQ (horn32 hQ (triL' f hf1) (triR' f hf0) e) t (triL' h hh)

variable (c2 : (h : Horn X 0 1) → { σ // Fills h σ })

/-- the inner 2-horn of two composable edges -/
def h21 (f g : (E X).car) (e : (V X).eqv (dd X 0 f) (dd X 1 g)) : Horn X 0 1 where
  x i := match i with
    | 0 => g
    | 1 => g
    | _ + 2 => f
  compat i j hij hj hi hj1 := match i, j, hij, hj, hi, hj1 with
    | _, 0, _, _, _, h => absurd rfl h
    | 0, 1, _, _, _, _ => e
    | 1, 1, _, _, h, _ => absurd rfl h
    | _ + 2, 1, h, _, _, _ => absurd (Nat.le_of_succ_le_succ h) (Nat.not_succ_le_zero _)
    | _, _ + 2, _, h, _, _ => absurd (Nat.le_of_succ_le_succ h) (Nat.not_succ_le_zero _)

def compS {x y z : (V X).car} (f : Mor x y) (g : Mor y z) : (T X).car :=
  (c2 (h21 f.1 g.1 ((V X).trans f.2.2 ((V X).symm g.2.1)))).1

theorem compS_d2 {x y z : (V X).car} (f : Mor x y) (g : Mor y z) : (E X).eqv (dd X 2 (compS c2 f g)) f.1 :=
  (c2 (h21 f.1 g.1 ((V X).trans f.2.2 ((V X).symm g.2.1)))).2 2 (Nat.le_refl 2) ne21

theorem compS_d0 {x y z : (V X).car} (f : Mor x y) (g : Mor y z) : (E X).eqv (dd X 0 (compS c2 f g)) g.1 :=
  (c2 (h21 f.1 g.1 ((V X).trans f.2.2 ((V X).symm g.2.1)))).2 0 (Nat.zero_le 2) ne01

theorem compS_tri {x y z : (V X).car} (f : Mor x y) (g : Mor y z) :
    Tri X f.1 g.1 (dd X 1 (compS c2 f g)) :=
  ⟨compS c2 f g, compS_d2 c2 f g, compS_d0 c2 f g, (E X).refl _⟩

def compM {x y z : (V X).car} (f : Mor x y) (g : Mor y z) : Mor x z :=
  ⟨dd X 1 (compS c2 f g),
    (V X).trans ((V X).symm (dd_dd X (Nat.le_refl 1) (compS c2 f g)))
      ((V X).trans (X.map_eqv (face 0 1) (compS_d2 c2 f g)) f.2.1),
    (V X).trans (dd_dd X (Nat.le_refl 0) (compS c2 f g))
      ((V X).trans (X.map_eqv (face 0 0) (compS_d0 c2 f g)) g.2.2)⟩

def idM (x : (V X).car) : Mor x x := ⟨idv X x, d1_idv X x, d0_idv X x⟩

/-- the homotopy category hX -/
def hCat : ECat.{w, w} where
  Ob := (V X).car
  Hom := Mor
  heq f g := Hty f g
  hrefl f := triR' f.1 f.2.2
  hsymm {_ _} {f _} h := hty_symm hQ f.2.2 h
  htrans h k := hty_trans hQ h k
  id := idM
  comp := compM c2
  comp_resp {_ _ _} {f f' g g'} e e' :=
    tri_uniq hQ g'.2.2
      (tri_resp2 hQ (compM c2 f g).2.2
        (tri_resp1 hQ f.2.1 f.2.2 (compM c2 f g).2.1 (compS_tri c2 f g) e) e')
      (compS_tri c2 f' g')
  id_comp {x _} f := tri_uniq hQ f.2.2 (compS_tri c2 (idM x) f) (triL' f.1 f.2.1)
  comp_id {_ y} f := tri_uniq hQ (d0_idv X y) (compS_tri c2 f (idM y)) (triR' f.1 f.2.2)
  assoc f g h :=
    tri_uniq hQ (compM c2 g h).2.2
      (horn32 hQ (compS_tri c2 f g) (compS_tri c2 g h) (compS_tri c2 (compM c2 f g) h))
      (compS_tri c2 f (compM c2 g h))

end IC

end Found

#print axioms Found.IC.horn31
#print axioms Found.IC.horn32
#print axioms Found.IC.tri_resp1
#print axioms Found.IC.hCat

found/IC20.lean

lean_all/found/IC20.lean

/-
  T153 ((infinity,1)-categories), part 20. found/IC20 — filling inner horns
  of a coslice X_p/, II (mirror of IC17/IC18 liftGen).  Over an
  (m'+1)-simplex τ of K with admissible values b_j under its faces, the
  front faces (the b_j) and back faces (from the horn H) form an inner horn
  of X (Lambda^(m'+1+1+n+2)_(k+m'+2)); its filler is an admissible value
  with the prescribed faces.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.IC19

namespace Found

namespace IC

open Simp

universe w

variable {K X : SSet.{w}} {p : PNat K X} {n k : Nat} {H : Horn (Coslice p) n k} {σ : (X.obj (n + 2 : Nat)).car}
  (hk0 : 0 < k) (hk : k < n + 2) (hQ : Quasi X)

def cvB (m' n : Nat) : DHom (m' + 1 + 1 + n + 1) (m' + 1 + (n + 2)) :=
  cv _ _ (congrArg Nat.succ (succ_add' (m' + 1) n))
def cvR (m' n : Nat) : DHom (m' + 1 + (n + 2)) (m' + 1 + 1 + n + 1) :=
  cv _ _ (congrArg Nat.succ (succ_add' (m' + 1) n)).symm

variable (H σ)

def hornC (m' : Nat) (τ : (K.obj (m' + 1 : Nat)).car) (b : Nat → EWc H σ m') :
    Nat → (X.obj (m' + 1 + 1 + n + 1 : Nat)).car :=
  pick (fun j => mp X (cvB m' n) (b j).w) (fun i => (H.x i).ψ (m' + 1) τ) (m' + 2)

variable {H σ}

def cvD (m' n : Nat) : DHom (m' + 1 + 1 + n) (m' + 1 + (n + 1)) := cv _ _ (succ_add' (m' + 1) n)

/-- the horn of X under τ -/
def hornCH (m' : Nat) (τ : (K.obj (m' + 1 : Nat)).car) (b : Nat → EWc H σ m')
    (hb : ∀ j, j ≤ m' + 1 → (K.obj (m' : Nat)).eqv (b j).τ (dd K j τ))
    (BB : ∀ q q'', q ≤ q'' → q'' ≤ m' →
      (X.obj (m' + 1 + 1 + n : Nat)).eqv (dd X q (mp X (cvB m' n) (b (q'' + 1)).w))
        (dd X q'' (mp X (cvB m' n) (b q).w))) : Horn X (m' + 1 + 1 + n) (k + (m' + 2)) where
  x := hornC H σ m' τ b
  compat i j hij hj hi hj1 :=
    match split_ic (m' + 2) (j + 1), split_ic (m' + 2) i with
    | .inl h, _ => by
      have hj' : j ≤ m' := Nat.le_of_lt_succ (Nat.lt_of_succ_lt_succ h)
      show (X.obj _).eqv (dd X i (pick _ _ (m' + 2) (j + 1))) (dd X j (pick _ _ (m' + 2) i))
      rw [pick_front _ _ _ _ h, pick_front _ _ _ _ (Nat.lt_of_le_of_lt hij (Nat.lt_of_succ_lt h))]
      exact BB i j hij hj'
    | .inr ⟨i'', e⟩, .inl hi2 => by
      have ej : j = i'' + (m' + 1) := succ_cancel e
      have hq : i ≤ m' + 1 := Nat.le_of_lt_succ hi2
      have hi'' : i'' ≤ n + 2 := le_cancel_r (m' + 2) (Nat.le_trans (Nat.le_of_eq e.symm)
        (Nat.le_trans (Nat.succ_le_succ hj) (Nat.le_of_eq (Nat.add_comm (m' + 2) (n + 2)))))
      have hik : i'' ≠ k := fun h => hj1 (e.trans (congrArg (· + (m' + 2)) h))
      show (X.obj _).eqv (dd X i (pick _ _ (m' + 2) (j + 1))) (dd X j (pick _ _ (m' + 2) i))
      rw [e, pick_back _ _ _ i'', pick_front _ _ _ _ hi2]
      refine (X.obj _).trans (mp_resp X (g := dComp (cvD m' n) (J (face m' i) (dId (n + 1))))
          (fun r _ => (J_face_front (n + 1) hq r).symm) _)
        ((X.obj _).trans ((X.obj _).symm (mp_comp X _ _ _))
        ((X.obj _).trans (mp_eqv X _ ((X.obj _).symm ((H.x i'').ψ_nat (face m' i) τ)))
        ((X.obj _).trans (mp_eqv X _ ((H.x i'').ψ_eqv m' ((K.obj _).symm (hb i hq))))
        ((X.obj _).trans (mp_eqv X _ ((X.obj _).symm ((b i).front i'' hi'' hik)))
        ((X.obj _).trans (mp_comp X _ _ _) ?_)))))
      rw [ej]
      exact (X.obj _).trans (mp_resp X (g := dComp (face (m' + 1 + 1 + n) (i'' + (m' + 1))) (cvB m' n))
          (fun r _ => J_face_back m' r) _) ((X.obj _).symm (mp_comp X _ _ _))
    | .inr ⟨j0, e⟩, .inr ⟨i0, ei⟩ =>
      match j0, e with
      | 0, e => absurd (Nat.le_trans (Nat.le_of_eq ei.symm) hij)
          (Nat.not_le_of_gt (Nat.lt_of_lt_of_le (Nat.lt_of_succ_le (Nat.le_of_eq e)) (Nat.add_le_add_right (Nat.zero_le i0) _)))
      | j0' + 1, e => by
        have ej : j = j0' + (m' + 2) := succ_cancel (e.trans (Nat.succ_add j0' (m' + 2)))
        have hq : i0 ≤ j0' := le_cancel_r (m' + 2) (Nat.le_trans (Nat.le_of_eq ei.symm) (Nat.le_trans hij (Nat.le_of_eq ej)))
        have hj0 : j0' ≤ n + 1 := le_cancel_r (m' + 2) (Nat.le_trans (Nat.le_of_eq ej.symm)
          (Nat.le_trans hj (Nat.le_of_eq (Nat.add_comm (m' + 2) (n + 1)))))
        have hi0 : i0 ≠ k := fun h => hi (ei.trans (congrArg (· + (m' + 2)) h))
        have hj0k : j0' + 1 ≠ k := fun h => hj1 (e.trans (congrArg (· + (m' + 2)) h))
        have hb2 : hornC H σ m' τ b (j0' + (m' + 2) + 1) = (H.x (j0' + 1)).ψ (m' + 1) τ :=
          (congrArg (hornC H σ m' τ b) (Nat.succ_add j0' (m' + 2)).symm).trans (pick_back _ _ _ (j0' + 1))
        have hb3 : hornC H σ m' τ b (i0 + (m' + 2)) = (H.x i0).ψ (m' + 1) τ := pick_back _ _ _ i0
        show (X.obj _).eqv (dd X i (hornC H σ m' τ b (j + 1))) (dd X j (hornC H σ m' τ b i))
        rw [ej, ei, hb2, hb3]
        exact (X.obj _).trans (mp_resp X (g := J (dId (m' + 1)) (face n i0)) (fun r _ => (J_face_back (m' + 1) r).symm) _)
          ((X.obj _).trans ((H.compat i0 j0' hq hj0 hi0 hj0k).2 (m' + 1) τ)
            (mp_resp X (fun r _ => J_face_back (m' + 1) r) _))

end IC

end Found

#print axioms Found.IC.hornCH

found/IC21.lean

lean_all/found/IC21.lean

/-
  T153 ((infinity,1)-categories), part 21. found/IC21 — coslices of
  quasi-categories are quasi-categories (dual of IC18), inside the
  classical point: the filler of the horn of IC20 is an admissible value
  with the prescribed faces, so EWc -> K has the boundary lifting property,
  and its section with the Delta-part filler fills the given inner horn of
  X_p/.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.IC20

namespace Found

namespace IC

open Simp

universe w

variable {K X : SSet.{w}} {p : PNat K X} {n k : Nat} {H : Horn (Coslice p) n k} {σ : (X.obj (n + 2 : Nat)).car}
  (hk0 : 0 < k) (hk : k < n + 2) (hQ : Quasi X)

include hk0 hk hQ in
theorem liftGenC (m' : Nat) (τ : (K.obj (m' + 1 : Nat)).car) (b : Nat → EWc H σ m')
    (hb : ∀ j, j ≤ m' + 1 → (K.obj (m' : Nat)).eqv (b j).τ (dd K j τ))
    (BB : ∀ q q'', q ≤ q'' → q'' ≤ m' →
      (X.obj (m' + 1 + 1 + n : Nat)).eqv (dd X q (mp X (cvB m' n) (b (q'' + 1)).w))
        (dd X q'' (mp X (cvB m' n) (b q).w))) :
    ∃ e : EWc H σ (m' + 1), e.τ = τ ∧ ∀ j, j ≤ m' + 1 →
      ((EWcS H σ).obj (m' : Nat)).eqv ((EWcS H σ).map (a := (m' : Nat)) (b := (m' + 1 : Nat)) (face m' j) e) (b j) :=
  match hQ (m' + 1 + 1 + n) (k + (m' + 2)) (Nat.lt_of_lt_of_le hk0 (Nat.le_add_right _ _))
      (by rw [Nat.add_comm k (m' + 2)]
          exact Nat.lt_of_lt_of_le (Nat.add_lt_add_left hk _) (Nat.le_of_eq rfl))
      (hornCH m' τ b hb BB) with
  | ⟨t, ht⟩ =>
    have hfr : ∀ i, i ≤ n + 2 → i ≠ k → (X.obj (m' + 1 + 1 + n + 1 : Nat)).eqv (dd X (i + (m' + 2)) t) ((H.x i).ψ (m' + 1) τ) :=
      fun i hi hik => (pick_back (fun i => (H.x i).ψ (m' + 1) τ) (m' + 2) (fun j => mp X (cvB m' n) (b j).w) i) ▸
        ht (i + (m' + 2)) (Nat.le_trans (Nat.add_le_add_right hi _) (Nat.le_of_eq (Nat.add_comm _ _)))
          (fun h => hik (add_cancel_r (m' + 2) h))
    have hbk : ∀ q, q ≤ m' + 1 → (X.obj (m' + 1 + 1 + n + 1 : Nat)).eqv (dd X q t) (mp X (cvB m' n) (b q).w) :=
      fun q hq => (pick_front (fun i => (H.x i).ψ (m' + 1) τ) (m' + 2) (fun j => mp X (cvB m' n) (b j).w) q
          (Nat.lt_succ_of_le hq)) ▸
        ht q (Nat.le_trans hq (Nat.le_trans (Nat.le_succ _) (Nat.le_trans (Nat.le_add_right _ n) (Nat.le_add_right _ 2))))
          (fun h => Nat.not_le_of_gt (Nat.lt_succ_of_le hq) (Nat.le_trans (Nat.le_add_left _ k) (Nat.le_of_eq h.symm)))
    ⟨⟨τ, t,
      fun i hi hik => (X.obj _).trans (mp_resp X (g := face (m' + 1 + 1 + n + 1) (i + (m' + 2)))
          (fun r _ => J_face_back (m' + 1) r) t) (hfr i hi hik),
      (X.obj _).trans (mp_resp X (g := dComp (inD (m' + 1) (n + 1)) (face (m' + 1 + 1 + n + 1) (0 + (m' + 2))))
          (fun r hr => (sk_lt (Nat.lt_of_lt_of_le (Nat.lt_succ_of_le hr) (Nat.le_of_eq (Nat.zero_add _).symm))).symm) t)
        ((X.obj _).trans ((X.obj _).symm (mp_comp X _ _ t))
          ((X.obj _).trans (mp_eqv X _ (hfr 0 (Nat.zero_le _) (fun h : 0 = k => Nat.lt_irrefl 0 (h.symm ▸ hk0 : 0 < 0))))
            ((H.x 0).ψ_K (m' + 1) τ))),
      (X.obj _).trans (mp_resp X (g := dComp (dComp (inK m' (n + 2)) (cvR m' n)) (face (m' + 1 + 1 + n + 1) 0))
          (fun r _ => by
            show r + (m' + 1 + 1) = sk 0 (r + (m' + 1))
            rw [sk_ge (Nat.zero_le _)]; rfl) t)
        ((X.obj _).trans ((X.obj _).symm (mp_comp X _ _ t))
          ((X.obj _).trans (mp_eqv X _ (hbk 0 (Nat.zero_le _)))
            ((X.obj _).trans (mp_comp X _ _ _)
              ((X.obj _).trans (mp_resp X (g := inK m' (n + 2)) (fun _ _ => rfl) _) (b 0).base))))⟩,
     rfl,
     fun j hj => ⟨(K.obj _).symm (hb j hj),
      (X.obj _).trans (mp_resp X (g := dComp (cvR m' n) (face (m' + 1 + 1 + n + 1) j))
          (fun r _ => J_face_front (n + 2) hj r) t)
        ((X.obj _).trans ((X.obj _).symm (mp_comp X _ _ t))
          ((X.obj _).trans (mp_eqv X _ (hbk j hj))
            ((X.obj _).trans (mp_comp X _ _ _) (mp_idl X _ (fun _ _ => rfl) _))))⟩⟩

def cvS (n : Nat) : DHom (0 + 1 + 1 + n) (n + 2) := cv _ _ (by rw [Nat.zero_add, Nat.add_comm])

include hk0 hk hQ in
theorem liftE1c (τ : (K.obj (1 : Nat)).car) (b0 b1 : ((EWcS H σ).obj (0 : Nat)).car)
    (h0 : (K.obj (0 : Nat)).eqv ((piWc H σ).app (0 : Nat) b0) (dd K 0 τ))
    (h1 : (K.obj (0 : Nat)).eqv ((piWc H σ).app (0 : Nat) b1) (dd K 1 τ)) :
    ∃ e : ((EWcS H σ).obj (1 : Nat)).car, (K.obj (1 : Nat)).eqv ((piWc H σ).app (1 : Nat) e) τ ∧
      ((EWcS H σ).obj (0 : Nat)).eqv (dd (EWcS H σ) 0 e) b0 ∧ ((EWcS H σ).obj (0 : Nat)).eqv (dd (EWcS H σ) 1 e) b1 :=
  have hbase : ∀ (c : EWc H σ 0), (X.obj (0 + 1 + 1 + n : Nat)).eqv (dd X 0 (mp X (cvB 0 n) c.w))
      (mp X (cvS n) σ) := fun c =>
    (X.obj _).trans (mp_ceq' X _ _ (cvS n) (inK 0 (n + 2))
        (fun r _ => by show sk 0 r = r + (0 + 1); rw [sk_ge (Nat.zero_le r)]) c.w)
      (mp_eqv X _ c.base)
  match liftGenC hk0 hk hQ 0 τ (fun j => match j with | 0 => b0 | _ + 1 => b1)
      (fun j hj => match j, hj with
        | 0, _ => h0
        | 1, _ => h1
        | _ + 2, h => absurd (Nat.le_of_succ_le_succ h) (Nat.not_succ_le_zero _))
      (fun q q'' hq hq'' => match q, q'', hq, hq'' with
        | 0, 0, _, _ => (X.obj _).trans (hbase b1) ((X.obj _).symm (hbase b0))
        | _, _ + 1, _, h => absurd h (Nat.not_succ_le_zero _)
        | _ + 1, 0, h, _ => absurd h (Nat.not_succ_le_zero _)) with
  | ⟨e, he, hf⟩ => ⟨e, he ▸ (K.obj _).refl _, hf 0 (Nat.zero_le _), hf 1 (Nat.le_refl _)⟩

def castC (n n' : Nat) : DHom (n' + 1 + 1 + 1 + n) (n' + 1 + (n + 2)) :=
  cv _ _ ((succ_add' (n' + 1 + 1) n).trans (congrArg Nat.succ (succ_add' (n' + 1) n)))

include hk0 hk hQ in
theorem liftE2c (n' : Nat) (τ : (K.obj (n' + 2 : Nat)).car) (b : Bdry (EWcS H σ) n')
    (hb : ∀ j, j ≤ n' + 2 → (K.obj (n' + 1 : Nat)).eqv ((piWc H σ).app (n' + 1 : Nat) (b.x j)) (dd K j τ)) :
    ∃ e : ((EWcS H σ).obj (n' + 2 : Nat)).car, (K.obj (n' + 2 : Nat)).eqv ((piWc H σ).app (n' + 2 : Nat) e) τ ∧
      BFills b e :=
  match liftGenC hk0 hk hQ (n' + 1) τ b.x hb (fun q q'' hq hq'' =>
      (X.obj _).trans (mp_ceq' X _ _ (castC n n') (J (face n' q) (dId (n + 2)))
          (fun r _ => (J_face_front (n + 2) (Nat.le_trans hq hq'') r).symm) _)
        ((X.obj _).trans (mp_eqv X _ (b.compat q q'' hq hq'').2)
          (mp_ceq' X _ _ _ _ (fun r _ => J_face_front (n + 2) hq'' r) _))) with
  | ⟨e, he, hf⟩ => ⟨e, he ▸ (K.obj _).refl _, hf⟩

/-- coslices of quasi-categories are quasi-categories (in the classical point) -/
theorem coslice_quasi (Kw : ClW.{w}) (Kc : CP.Classical') {K X : SSet.{w}} (p : PNat K X) (hQ : Quasi X) :
    Quasi (Coslice p) := fun n k hk0 hk H =>
  match hQ n k hk0 hk ⟨fun i => (H.x i).σ, fun i j hij hj hi hj1 => (H.compat i j hij hj hi hj1).1⟩ with
  | ⟨σ, hσ⟩ =>
    have hp := secN_proj Kw Kc (piWc H σ) (liftVc hσ hk0 hk hQ) (liftE1c hk0 hk hQ)
      (fun n' τ b hb => liftE2c hk0 hk hQ n' τ b hb)
    let s := secN Kw Kc (piWc H σ) (liftVc hσ hk0 hk hQ) (liftE1c hk0 hk hQ)
      (fun n' τ b hb => liftE2c hk0 hk hQ n' τ b hb)
    ⟨⟨σ, fun m τ => (s.app m τ).w, fun m {_ _} h => (s.app_eqv m h).2, fun g τ => (s.nat g τ).2,
       fun m τ => (X.obj _).trans (s.app m τ).back (p.app_eqv _ (hp m τ)), fun m τ => (s.app m τ).base⟩,
     fun i hi hik => ⟨hσ i hi hik, fun m τ =>
       (X.obj _).trans ((s.app m τ).front i hi hik) ((H.x i).ψ_eqv m (hp m τ))⟩⟩

end IC

end Found

#print axioms Found.IC.liftGenC
#print axioms Found.IC.liftE1c
#print axioms Found.IC.liftE2c
#print axioms Found.IC.coslice_quasi

found/IC22.lean

lean_all/found/IC22.lean

/-
  T153 ((infinity,1)-categories), part 22. found/IC22 — the attaching engine
  (inside the classical point at universe w).  Subcomplexes are predicates
  on simplices closed under equivalence and under all simplicial operators;
  a partial map A -> X is a natural family on the simplices of A.  If σ is
  a d-simplex of B that is "injective" (g^*σ = g'^*σ forces g = g') and
  whose intersection with A is the inner horn Λ^d_k (a pullback g^*σ lies in
  A exactly when g misses some vertex i ≠ k), then every partial map on A
  into a quasi-category X extends to A ∪ <σ>.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.IC21

namespace Found

namespace IC

open Simp

universe w

/-- subcomplexes as predicates -/
structure Sub (B : SSet.{w}) where
  P : (q : Nat) → (B.obj (q : Nat)).car → Prop
  P_eqv : ∀ q {z z' : (B.obj (q : Nat)).car}, (B.obj (q : Nat)).eqv z z' → P q z → P q z'
  P_map : ∀ {q' q : Nat} (g : DHom q' q) (z : (B.obj (q : Nat)).car), P q z → P q' (mp B g z)

/-- partial maps defined on a subcomplex -/
structure PMap {B : SSet.{w}} (A : Sub B) (X : SSet.{w}) where
  app : (q : Nat) → (z : (B.obj (q : Nat)).car) → A.P q z → (X.obj (q : Nat)).car
  app_eqv : ∀ q {z z' : (B.obj (q : Nat)).car}, (B.obj (q : Nat)).eqv z z' → ∀ (hz : A.P q z) (hz' : A.P q z'),
    (X.obj (q : Nat)).eqv (app q z hz) (app q z' hz')
  nat : ∀ {q' q : Nat} (g : DHom q' q) (z : (B.obj (q : Nat)).car) (hz : A.P q z),
    (X.obj (q' : Nat)).eqv (app q' (mp B g z) (A.P_map g z hz)) (mp X g (app q z hz))

variable {B X : SSet.{w}}

/-- the subcomplex A ∪ <σ> -/
def genSub (A : Sub B) {d : Nat} (σ : (B.obj (d : Nat)).car) : Sub B where
  P q z := A.P q z ∨ ∃ g : DHom q d, (B.obj (q : Nat)).eqv z (mp B g σ)
  P_eqv q {_ _} e h := h.elim (fun h => .inl (A.P_eqv q e h))
    (fun ⟨g, h⟩ => .inr ⟨g, (B.obj _).trans ((B.obj _).symm e) h⟩)
  P_map g z h := h.elim (fun h => .inl (A.P_map g z h))
    (fun ⟨g', h⟩ => .inr ⟨dComp g g', (B.obj _).trans (mp_eqv B g h) (mp_comp B g g' σ)⟩)

variable (Kw : ClW.{w}) {A : Sub B} (F : PMap A X) {e k : Nat} (σ : (B.obj (e + 2 : Nat)).car)
  (hk0 : 0 < k) (hk : k < e + 2) (hQ : Quasi X)
  (inj : ∀ (q : Nat) (g g' : DHom q (e + 2)), (B.obj (q : Nat)).eqv (mp B g σ) (mp B g' σ) →
    ∀ r, r ≤ q → g.f r = g'.f r)
  (hornIn : ∀ (q : Nat) (g : DHom q (e + 2)), A.P q (mp B g σ) → ∃ i, i ≤ e + 2 ∧ i ≠ k ∧ ∀ r, r ≤ q → g.f r ≠ i)
  (hornOut : ∀ i, i ≤ e + 2 → i ≠ k → A.P (e + 1) (dd B i σ))

/-- the faces of σ, read through F -/
def faceVal (i : Nat) : (X.obj (e + 1 : Nat)).car :=
  Kw.ch (fun x => ∀ h : A.P (e + 1) (dd B i σ), (X.obj (e + 1 : Nat)).eqv x (F.app _ _ h))
    (match Kw.lem (A.P (e + 1) (dd B i σ)) with
      | .inl h => ⟨F.app _ _ h, fun h' => F.app_eqv _ ((B.obj _).refl _) h h'⟩
      | .inr h => ⟨F.app _ _ (hornOut 0 (Nat.zero_le _) (fun h0 : 0 = k => Nat.lt_irrefl 0 (h0.symm ▸ hk0 : 0 < 0))),
          fun h' => absurd h' h⟩)

include hk0 hornOut in
theorem faceVal_spec (i : Nat) (h : A.P (e + 1) (dd B i σ)) :
    (X.obj (e + 1 : Nat)).eqv (faceVal Kw F σ hk0 hornOut i) (F.app _ _ h) :=
  Kw.ch_spec (fun x => ∀ h : A.P (e + 1) (dd B i σ), (X.obj (e + 1 : Nat)).eqv x (F.app _ _ h)) _ h

/-- the horn of X given by the faces of σ -/
def hornS : Horn X e k where
  x := faceVal Kw F σ hk0 hornOut
  compat i j hij hj hi hj1 :=
    have hi' := hornOut i (Nat.le_trans hij (Nat.le_succ_of_le hj)) hi
    have hj' := hornOut (j + 1) (Nat.succ_le_succ hj) hj1
    (X.obj _).trans (mp_eqv X _ (faceVal_spec Kw F σ hk0 hornOut (j + 1) hj'))
      ((X.obj _).trans ((X.obj _).symm (F.nat (face e i) _ hj'))
        ((X.obj _).trans (F.app_eqv _ (dd_dd B hij σ) _ (A.P_map (face e j) _ hi'))
          ((X.obj _).trans (F.nat (face e j) _ hi')
            (mp_eqv X _ ((X.obj _).symm (faceVal_spec Kw F σ hk0 hornOut i hi'))))))

variable (τ : (X.obj (e + 2 : Nat)).car) (hτ : Fills (hornS Kw F σ hk0 hornOut) τ)

include hornIn hτ in
/-- on A ∩ <σ>, F agrees with the pullbacks of the filler τ -/
theorem consist (q : Nat) (g : DHom q (e + 2)) (hA : A.P q (mp B g σ)) :
    (X.obj (q : Nat)).eqv (F.app q (mp B g σ) hA) (mp X g τ) :=
  match hornIn q g hA with
  | ⟨i, hi, hik, hmiss⟩ =>
    have hf : ∀ r, r ≤ q → g.f r =
        (dComp (dComp g (degen (e + 1) (Nat.pred i) (Nat.pred_le_pred hi))) (face (e + 1) i)).f r :=
      fun r hr => (sk_dg (hmiss r hr)).symm
    have hi' := hornOut i hi hik
    (X.obj _).trans (F.app_eqv q (pull B hf σ) hA (A.P_map _ _ hi'))
      ((X.obj _).trans (F.nat _ _ hi')
        ((X.obj _).trans (mp_eqv X _ ((X.obj _).symm (faceVal_spec Kw F σ hk0 hornOut i hi')))
          ((X.obj _).trans (mp_eqv X _ ((X.obj _).symm (hτ i hi hik)))
            ((X.obj _).symm (pull X hf τ)))))

/-- the defining property of the extended value -/
def extP (q : Nat) (z : (B.obj (q : Nat)).car) (x : (X.obj (q : Nat)).car) : Prop :=
  (∀ hA : A.P q z, (X.obj (q : Nat)).eqv x (F.app q z hA)) ∧
  (∀ g : DHom q (e + 2), (B.obj (q : Nat)).eqv z (mp B g σ) → (X.obj (q : Nat)).eqv x (mp X g τ))

include hornIn hτ inj in
theorem extP_ex (q : Nat) (z : (B.obj (q : Nat)).car) (h : (genSub A σ).P q z) :
    ∃ x, extP F σ τ q z x :=
  match Kw.lem (A.P q z) with
  | .inl hA => ⟨F.app q z hA, fun hA' => F.app_eqv q ((B.obj _).refl z) hA hA',
      fun g hg => (X.obj _).trans (F.app_eqv q hg hA (A.P_eqv q hg hA))
        (consist Kw F σ hk0 hornIn hornOut τ hτ q g (A.P_eqv q hg hA))⟩
  | .inr nA => match h with
    | .inl hA => absurd hA nA
    | .inr ⟨g, hg⟩ => ⟨mp X g τ, fun hA => absurd hA nA,
        fun g' hg' => mp_resp X (inj q g g' ((B.obj _).trans ((B.obj _).symm hg) hg')) τ⟩

/-- the extended value at a simplex of A ∪ <σ> -/
def extA (q : Nat) (z : (B.obj (q : Nat)).car) (h : (genSub A σ).P q z) : (X.obj (q : Nat)).car :=
  Kw.ch (extP F σ τ q z) (extP_ex Kw F σ hk0 inj hornIn hornOut τ hτ q z h)

theorem extA_spec (q : Nat) (z : (B.obj (q : Nat)).car) (h : (genSub A σ).P q z) :
    extP F σ τ q z (extA Kw F σ hk0 inj hornIn hornOut τ hτ q z h) :=
  Kw.ch_spec _ _

/-- the extension of F to A ∪ <σ> -/
def extMap : PMap (genSub A σ) X where
  app := extA Kw F σ hk0 inj hornIn hornOut τ hτ
  app_eqv q {z z'} e hz hz' := match Kw.lem (A.P q z) with
    | .inl hA => (X.obj _).trans ((extA_spec Kw F σ hk0 inj hornIn hornOut τ hτ q z hz).1 hA)
        ((X.obj _).trans (F.app_eqv q e hA (A.P_eqv q e hA))
          ((X.obj _).symm ((extA_spec Kw F σ hk0 inj hornIn hornOut τ hτ q z' hz').1 (A.P_eqv q e hA))))
    | .inr nA => match hz with
      | .inl hA => absurd hA nA
      | .inr ⟨g, hg⟩ => (X.obj _).trans ((extA_spec Kw F σ hk0 inj hornIn hornOut τ hτ q z hz).2 g hg)
          ((X.obj _).symm ((extA_spec Kw F σ hk0 inj hornIn hornOut τ hτ q z' hz').2 g
            ((B.obj _).trans ((B.obj _).symm e) hg)))
  nat {q' q} f z hz := match Kw.lem (A.P q z) with
    | .inl hA => (X.obj _).trans
        ((extA_spec Kw F σ hk0 inj hornIn hornOut τ hτ q' _ _).1 (A.P_map f z hA))
        ((X.obj _).trans (F.nat f z hA)
          (mp_eqv X f ((X.obj _).symm ((extA_spec Kw F σ hk0 inj hornIn hornOut τ hτ q z hz).1 hA))))
    | .inr nA => match hz with
      | .inl hA => absurd hA nA
      | .inr ⟨g, hg⟩ => (X.obj _).trans
          ((extA_spec Kw F σ hk0 inj hornIn hornOut τ hτ q' _ _).2 (dComp f g)
            ((B.obj _).trans (mp_eqv B f hg) (mp_comp B f g σ)))
          ((X.obj _).trans ((X.obj _).symm (mp_comp X f g τ))
            (mp_eqv X f ((X.obj _).symm ((extA_spec Kw F σ hk0 inj hornIn hornOut τ hτ q z hz).2 g hg))))

include Kw hk0 hk hQ inj hornIn hornOut in
/-- the attaching step: a partial map on A into a quasi-category extends over an inner horn
    A ∩ <σ> = Λ^(e+2)_k to A ∪ <σ> -/
theorem attach : ∃ G : PMap (genSub A σ) X,
    ∀ q (z : (B.obj (q : Nat)).car) (hA : A.P q z), (X.obj (q : Nat)).eqv (G.app q z (.inl hA)) (F.app q z hA) :=
  match hQ e k hk0 hk (hornS Kw F σ hk0 hornOut) with
  | ⟨τ, hτ⟩ => ⟨extMap Kw F σ hk0 inj hornIn hornOut τ hτ,
      fun q z hA => (extA_spec Kw F σ hk0 inj hornIn hornOut τ hτ q z (.inl hA)).1 hA⟩

end IC

end Found

#print axioms Found.IC.genSub
#print axioms Found.IC.hornS
#print axioms Found.IC.consist
#print axioms Found.IC.extP_ex
#print axioms Found.IC.extMap
#print axioms Found.IC.attach

found/IC23.lean

lean_all/found/IC23.lean

/-
  T153 ((infinity,1)-categories), part 23. found/IC23 — the prism
  Delta^2 x Delta^n, I: coordinates of its simplices, subcomplexes given by
  conditions on vertices (unions, indexed unions, restriction of partial
  maps), the subcomplex A0 = Lambda^2_1 x Delta^n ∪ Delta^2 x ∂Delta^n, and
  the staircase simplices
    sgm a b = <(0,0)..(0,a),(1,a)..(1,b),(2,b+1)..(2,n)>   (a <= b < n),
    tau a b = <(0,0)..(0,a),(1,a)..(1,b),(2,b)..(2,n)>     (a <= b <= n),
  their vertices, injectivity, and: a simplex all of whose vertices lie on a
  staircase is a pullback of it.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.IC22

namespace Found

namespace IC

open Simp

universe w

/-! ## subcomplexes: unions and restriction -/

def sOr {B : SSet.{w}} (S T : Sub B) : Sub B where
  P q z := S.P q z ∨ T.P q z
  P_eqv q {_ _} e h := h.elim (fun h => .inl (S.P_eqv q e h)) (fun h => .inr (T.P_eqv q e h))
  P_map g z h := h.elim (fun h => .inl (S.P_map g z h)) (fun h => .inr (T.P_map g z h))

def sEx {B : SSet.{w}} {ι : Type} (C : ι → Prop) (S : ι → Sub B) : Sub B where
  P q z := ∃ i, C i ∧ (S i).P q z
  P_eqv q {_ _} e h := match h with
    | ⟨i, c, h⟩ => ⟨i, c, (S i).P_eqv q e h⟩
  P_map g z h := match h with
    | ⟨i, c, h⟩ => ⟨i, c, (S i).P_map g z h⟩

/-- restriction of a partial map to a smaller subcomplex -/
def pres {B X : SSet.{w}} {A A' : Sub B} (F : PMap A X) (h : ∀ q z, A'.P q z → A.P q z) : PMap A' X where
  app q z hz := F.app q z (h q z hz)
  app_eqv q {_ _} e _ _ := F.app_eqv q e _ _
  nat g z hz := (X.obj _).trans (F.app_eqv _ ((B.obj _).refl _) _ _) (F.nat g z (h _ z hz))

/-! ## the prism -/

/-- the prism Delta^2 x Delta^n -/
abbrev Pr (n : Nat) : SSet.{w} := SProd (Simp.{w} 2) (Simp.{w} n)

section prism

variable {n : Nat}

def prU {q : Nat} (z : ((Pr.{w} n).obj (q : Nat)).car) : DHom q 2 := z.1.down
def prV {q : Nat} (z : ((Pr.{w} n).obj (q : Nat)).car) : DHom q n := z.2.down

theorem subst2 {Q : Nat → Nat → Prop} {x x' y y' : Nat} (hx : x = x') (hy : y = y') (h : Q x y) : Q x' y' := by
  cases hx; cases hy; exact h

/-- the subcomplex of simplices whose vertices satisfy Q -/
def SubV (Q : Nat → Nat → Prop) : Sub (Pr.{w} n) where
  P q z := ∀ r, r ≤ q → Q ((prU z).f r) ((prV z).f r)
  P_eqv _ {_ _} e h r hr := subst2 (e.1 r hr) (e.2 r hr) (h r hr)
  P_map g _ h r hr := h (g.f r) (g.bound r hr)

/-- A0 = Lambda^2_1 x Delta^n ∪ Delta^2 x ∂Delta^n -/
def A0 : Sub (Pr.{w} n) :=
  sOr (sOr (SubV (fun x _ => x ≠ 0)) (SubV (fun x _ => x ≠ 2)))
    (sEx (ι := Nat) (fun y => y ≤ n) (fun y => SubV (fun _ v => v ≠ y)))

end prism

/-! ## step functions -/

def stp (c i : Nat) : Nat := cond (gtb (c + 1) i) 0 1

theorem stp_lo {c i : Nat} (h : i ≤ c) : stp c i = 0 :=
  congrArg (fun t => cond t 0 1) (lt_gtb (c + 1) i (Nat.lt_succ_of_le h))
theorem stp_hi {c i : Nat} (h : c < i) : stp c i = 1 :=
  congrArg (fun t => cond t 0 1) (le_gtb (c + 1) i h)

theorem stp_le1 (c i : Nat) : stp c i ≤ 1 :=
  match Nat.lt_or_ge c i with
  | .inl h => by rw [stp_hi h]; exact Nat.le_refl 1
  | .inr h => by rw [stp_lo h]; exact Nat.zero_le 1

theorem stp_mono (c : Nat) {i j : Nat} (hij : i ≤ j) : stp c i ≤ stp c j :=
  match Nat.lt_or_ge c i with
  | .inl h => by rw [stp_hi h, stp_hi (Nat.lt_of_lt_of_le h hij)]; exact Nat.le_refl 1
  | .inr h => by rw [stp_lo h]; exact Nat.zero_le _

/-- the first coordinate of the staircases -/
def uW (a b i : Nat) : Nat := stp a i + stp (b + 1) i

def uD (a b d : Nat) : DHom d 2 :=
  ⟨uW a b, fun _ _ => Nat.add_le_add (stp_mono a (Nat.le_succ _)) (stp_mono (b + 1) (Nat.le_succ _)),
    fun i _ => Nat.add_le_add (stp_le1 a i) (stp_le1 (b + 1) i)⟩

section stairs

variable {a b : Nat} (hab : a ≤ b)

include hab in
theorem uW_0 {i : Nat} (h : i ≤ a) : uW a b i = 0 := by
  show stp a i + stp (b + 1) i = 0
  rw [stp_lo h, stp_lo (Nat.le_trans h (Nat.le_trans hab (Nat.le_succ b)))]

theorem uW_1 {y : Nat} (h1 : a ≤ y) (h2 : y ≤ b) : uW a b (y + 1) = 1 := by
  show stp a (y + 1) + stp (b + 1) (y + 1) = 1
  rw [stp_hi (Nat.lt_succ_of_le h1), stp_lo (Nat.succ_le_succ h2)]

include hab in
theorem uW_2 {i : Nat} (h : b + 1 < i) : uW a b i = 2 := by
  show stp a i + stp (b + 1) i = 2
  rw [stp_hi (Nat.lt_trans (Nat.lt_succ_of_le hab) h), stp_hi h]

theorem dg_0 {i : Nat} (h : i ≤ a) : dg a i = i := dg_lt (Nat.lt_succ_of_le h)
theorem dg_1 {y : Nat} (h : a ≤ y) : dg a (y + 1) = y := dg_ge (Nat.succ_le_succ h)

/-- second coordinate of tau -/
def vT (a b i : Nat) : Nat := dg a (dg (b + 1) i)

include hab in
theorem vT_0 {i : Nat} (h : i ≤ a) : vT a b i = i := by
  show dg a (dg (b + 1) i) = i
  rw [dg_lt (Nat.lt_succ_of_le (Nat.le_trans h (Nat.le_trans hab (Nat.le_succ b)))), dg_0 h]

theorem vT_1 {y : Nat} (h1 : a ≤ y) (h2 : y ≤ b) : vT a b (y + 1) = y := by
  show dg a (dg (b + 1) (y + 1)) = y
  rw [dg_lt (Nat.lt_succ_of_le (Nat.succ_le_succ h2)), dg_1 h1]

include hab in
theorem vT_2 {y : Nat} (h : b ≤ y) : vT a b (y + 2) = y := by
  show dg a (dg (b + 1) (y + 1 + 1)) = y
  rw [dg_ge (Nat.succ_le_succ (Nat.succ_le_succ h))]
  exact dg_1 (Nat.le_trans hab h)

/-- vertices of sgm, by cases -/
theorem sg_cases {P : Nat → Prop} (h0 : ∀ i, i ≤ a → P i) (h1 : ∀ y, a ≤ y → y ≤ b → P (y + 1))
    (h2 : ∀ y, b + 1 ≤ y → P (y + 1)) : ∀ i, P i
  | 0 => h0 0 (Nat.zero_le a)
  | y + 1 => match Nat.lt_or_ge a (y + 1) with
    | .inr h => h0 _ h
    | .inl h => match Nat.lt_or_ge b y with
      | .inl h' => h2 y h'
      | .inr h' => h1 y (Nat.le_of_lt_succ h) h'

/-- vertices of tau, by cases -/
theorem ta_cases {P : Nat → Prop} (h0 : ∀ i, i ≤ a → P i) (h1 : ∀ y, a ≤ y → y ≤ b → P (y + 1))
    (h2 : ∀ y, b ≤ y → P (y + 2)) : ∀ i, P i
  | 0 => h0 0 (Nat.zero_le a)
  | 1 => match Nat.lt_or_ge a 1 with
    | .inr h => h0 _ h
    | .inl h => h1 0 (Nat.le_of_lt_succ h) (Nat.zero_le b)
  | y + 2 => match Nat.lt_or_ge a (y + 2) with
    | .inr h => h0 _ h
    | .inl h => match Nat.lt_or_ge y b with
      | .inl h'' => h1 (y + 1) (Nat.le_of_lt_succ h) (Nat.succ_le_of_lt h'')
      | .inr h'' => h2 y h''

end stairs

/-- vertices lying on sgm a b -/
def memS (a b x y : Nat) : Prop := (x = 0 ∧ y ≤ a) ∨ (x = 1 ∧ a ≤ y ∧ y ≤ b) ∨ (x = 2 ∧ b + 1 ≤ y)

/-- vertices lying on tau a b -/
def memT (a b x y : Nat) : Prop := (x = 0 ∧ y ≤ a) ∨ (x = 1 ∧ a ≤ y ∧ y ≤ b) ∨ (x = 2 ∧ b ≤ y)

/-- position of a vertex along the staircases -/
def idxS (x y : Nat) : Nat := y + stp 0 x
def idxT (x y : Nat) : Nat := y + x

section simplices

variable {n : Nat} {a b : Nat} (hab : a ≤ b)

/-- the staircase (n+1)-simplex sgm a b of the prism -/
def sgm (ha : a ≤ n) : ((Pr.{w} n).obj (n + 1 : Nat)).car :=
  (⟨uD a b (n + 1)⟩, ⟨degen n a ha⟩)

/-- the staircase (n+2)-simplex tau a b of the prism -/
def tau (ha : a ≤ n) (hb : b ≤ n) : ((Pr.{w} n).obj (n + 2 : Nat)).car :=
  (⟨uD a b (n + 2)⟩, ⟨dComp (degen (n + 1) (b + 1) (Nat.succ_le_succ hb)) (degen n a ha)⟩)

include hab in
theorem sgm_mem (i : Nat) : memS a b (uW a b i) (dg a i) :=
  sg_cases (P := fun i => memS a b (uW a b i) (dg a i))
    (fun i h => .inl ⟨uW_0 hab h, by rw [dg_0 h]; exact h⟩)
    (fun y h1 h2 => .inr (.inl ⟨uW_1 h1 h2, by rw [dg_1 h1]; exact ⟨h1, h2⟩⟩))
    (fun y h => .inr (.inr ⟨uW_2 hab (Nat.succ_lt_succ (Nat.lt_of_succ_le h)),
      by rw [dg_1 (Nat.le_trans hab (Nat.le_trans (Nat.le_succ b) h))]; exact h⟩)) i

include hab in
theorem tau_mem (i : Nat) : memT a b (uW a b i) (vT a b i) :=
  ta_cases (P := fun i => memT a b (uW a b i) (vT a b i))
    (fun i h => .inl ⟨uW_0 hab h, by rw [vT_0 hab h]; exact h⟩)
    (fun y h1 h2 => .inr (.inl ⟨uW_1 h1 h2, by rw [vT_1 h1 h2]; exact ⟨h1, h2⟩⟩))
    (fun y h => .inr (.inr ⟨uW_2 hab (Nat.succ_lt_succ (Nat.lt_succ_of_le h)), by rw [vT_2 hab h]; exact h⟩)) i

include hab in
theorem sgm_idx (i : Nat) : idxS (uW a b i) (dg a i) = i :=
  sg_cases (P := fun i => idxS (uW a b i) (dg a i) = i)
    (fun i h => by show dg a i + stp 0 (uW a b i) = i; rw [dg_0 h, uW_0 hab h, stp_lo (Nat.zero_le 0)]; exact rfl)
    (fun y h1 h2 => by
      show dg a (y + 1) + stp 0 (uW a b (y + 1)) = y + 1
      rw [dg_1 h1, uW_1 h1 h2, stp_hi (Nat.succ_pos 0)])
    (fun y h => by
      show dg a (y + 1) + stp 0 (uW a b (y + 1)) = y + 1
      rw [dg_1 (Nat.le_trans hab (Nat.le_trans (Nat.le_succ b) h)),
        uW_2 hab (Nat.succ_lt_succ (Nat.lt_of_succ_le h)), stp_hi (Nat.succ_pos 1)]) i

include hab in
theorem tau_idx (i : Nat) : idxT (uW a b i) (vT a b i) = i :=
  ta_cases (P := fun i => idxT (uW a b i) (vT a b i) = i)
    (fun i h => by show vT a b i + uW a b i = i; rw [vT_0 hab h, uW_0 hab h]; exact rfl)
    (fun y h1 h2 => by show vT a b (y + 1) + uW a b (y + 1) = y + 1; rw [vT_1 h1 h2, uW_1 h1 h2])
    (fun y h => by
      show vT a b (y + 2) + uW a b (y + 2) = y + 2
      rw [vT_2 hab h, uW_2 hab (Nat.succ_lt_succ (Nat.lt_succ_of_le h))]) i

include hab in
/-- a vertex on sgm is the vertex at its position -/
theorem sgm_at {x y : Nat} (h : memS a b x y) : uW a b (idxS x y) = x ∧ dg a (idxS x y) = y :=
  match h with
  | .inl ⟨hx, hy⟩ => by
    cases hx
    show uW a b (y + stp 0 0) = 0 ∧ dg a (y + stp 0 0) = y
    rw [stp_lo (Nat.le_refl 0)]
    exact ⟨uW_0 hab hy, dg_0 hy⟩
  | .inr (.inl ⟨hx, h1, h2⟩) => by
    cases hx
    show uW a b (y + stp 0 1) = 1 ∧ dg a (y + stp 0 1) = y
    rw [stp_hi (Nat.succ_pos 0)]
    exact ⟨uW_1 h1 h2, dg_1 h1⟩
  | .inr (.inr ⟨hx, hy2⟩) => by
    cases hx
    show uW a b (y + stp 0 2) = 2 ∧ dg a (y + stp 0 2) = y
    rw [stp_hi (Nat.succ_pos 1)]
    exact ⟨uW_2 hab (Nat.succ_lt_succ (Nat.lt_of_succ_le hy2)),
      dg_1 (Nat.le_trans hab (Nat.le_trans (Nat.le_succ b) hy2))⟩

include hab in
/-- a vertex on tau is the vertex at its position -/
theorem tau_at {x y : Nat} (h : memT a b x y) : uW a b (idxT x y) = x ∧ vT a b (idxT x y) = y :=
  match h with
  | .inl ⟨hx, hy⟩ => by
    cases hx
    exact ⟨uW_0 hab hy, vT_0 hab hy⟩
  | .inr (.inl ⟨hx, h1, h2⟩) => by
    cases hx
    exact ⟨uW_1 h1 h2, vT_1 h1 h2⟩
  | .inr (.inr ⟨hx, hy2⟩) => by
    cases hx
    exact ⟨uW_2 hab (Nat.succ_lt_succ (Nat.lt_succ_of_le hy2)), vT_2 hab hy2⟩

theorem idxS_mono {x x' y y' : Nat} (hx : x ≤ x') (hy : y ≤ y') : idxS x y ≤ idxS x' y' :=
  Nat.add_le_add hy (stp_mono 0 hx)

theorem idxT_mono {x x' y y' : Nat} (hx : x ≤ x') (hy : y ≤ y') : idxT x y ≤ idxT x' y' :=
  Nat.add_le_add hy hx

include hab in
/-- sgm is injective -/
theorem sgm_inj (ha : a ≤ n) (q : Nat) (g g' : DHom q (n + 1))
    (e : ((Pr.{w} n).obj (q : Nat)).eqv (mp (Pr.{w} n) g (sgm (b := b) ha)) (mp (Pr.{w} n) g' (sgm (b := b) ha))) :
    ∀ r, r ≤ q → g.f r = g'.f r := fun r hr =>
  (sgm_idx hab (g.f r)).symm.trans
    ((congrArg (fun x => idxS x (dg a (g.f r))) (e.1 r hr : uW a b (g.f r) = uW a b (g'.f r))).trans
      ((congrArg (fun y => idxS (uW a b (g'.f r)) y) (e.2 r hr : dg a (g.f r) = dg a (g'.f r))).trans (sgm_idx hab (g'.f r))))

include hab in
/-- tau is injective -/
theorem tau_inj (ha : a ≤ n) (hb : b ≤ n) (q : Nat) (g g' : DHom q (n + 2))
    (e : ((Pr.{w} n).obj (q : Nat)).eqv (mp (Pr.{w} n) g (tau ha hb)) (mp (Pr.{w} n) g' (tau ha hb))) :
    ∀ r, r ≤ q → g.f r = g'.f r := fun r hr =>
  (tau_idx hab (g.f r)).symm.trans
    ((congrArg (fun x => idxT x (vT a b (g.f r))) (e.1 r hr : uW a b (g.f r) = uW a b (g'.f r))).trans
      ((congrArg (fun y => idxT (uW a b (g'.f r)) y) (e.2 r hr : vT a b (g.f r) = vT a b (g'.f r))).trans (tau_idx hab (g'.f r))))

include hab in
/-- a simplex with all vertices on sgm is a pullback of sgm -/
theorem sgm_gen (ha : a ≤ n) {q : Nat} (z : ((Pr.{w} n).obj (q : Nat)).car)
    (h : (SubV (n := n) (memS a b)).P q z) :
    ∃ g : DHom q (n + 1), ((Pr.{w} n).obj (q : Nat)).eqv z (mp (Pr.{w} n) g (sgm (b := b) ha)) :=
  ⟨⟨fun r => idxS ((prU z).f r) ((prV z).f r),
    fun r hr => idxS_mono ((prU z).mono r hr) ((prV z).mono r hr),
    fun r hr => Nat.add_le_add ((prV z).bound r hr) (stp_le1 0 _)⟩,
   fun r hr => (sgm_at hab (h r hr)).1.symm, fun r hr => (sgm_at hab (h r hr)).2.symm⟩

include hab in
/-- a simplex with all vertices on tau is a pullback of tau -/
theorem tau_gen (ha : a ≤ n) (hb : b ≤ n) {q : Nat} (z : ((Pr.{w} n).obj (q : Nat)).car)
    (h : (SubV (n := n) (memT a b)).P q z) :
    ∃ g : DHom q (n + 2), ((Pr.{w} n).obj (q : Nat)).eqv z (mp (Pr.{w} n) g (tau ha hb)) :=
  ⟨⟨fun r => idxT ((prU z).f r) ((prV z).f r),
    fun r hr => idxT_mono ((prU z).mono r hr) ((prV z).mono r hr),
    fun r hr => Nat.add_le_add ((prV z).bound r hr) ((prU z).bound r hr)⟩,
   fun r hr => (tau_at hab (h r hr)).1.symm, fun r hr => (tau_at hab (h r hr)).2.symm⟩

end simplices

end IC

end Found

#print axioms Found.IC.pres
#print axioms Found.IC.A0
#print axioms Found.IC.sgm_mem
#print axioms Found.IC.tau_mem
#print axioms Found.IC.sgm_inj
#print axioms Found.IC.tau_inj
#print axioms Found.IC.sgm_gen
#print axioms Found.IC.tau_gen

found/IC24.lean

lean_all/found/IC24.lean

/-
  T153 ((infinity,1)-categories), part 24. found/IC24 — the prism
  Delta^2 x Delta^n, II: one attaching step packaged (a subcomplex S that
  carries an extension of F0 : A0 -> X passes it to S' ⊆ S ∪ <σ> when
  S ∩ <σ> is an inner horn), and the first half of the filtration: the
  simplices sgm a b (a <= b < n), in the order (b, a), each attached along
  its inner horn at the vertex (1,a).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.IC23

namespace Found

namespace IC

open Simp

universe w

/-! ## one attaching step -/

section gen

variable (Kw : ClW.{w}) {B X : SSet.{w}} (Z0 : Sub B) (F0 : PMap Z0 X)

/-- S carries an extension of F0 -/
def Ext (S : Sub B) : Prop :=
  ∃ G : PMap S X, ∀ q (z : (B.obj (q : Nat)).car) (h0 : Z0.P q z) (hS : S.P q z),
    (X.obj (q : Nat)).eqv (G.app q z hS) (F0.app q z h0)

include Kw in
theorem ext_step {S S' : Sub B} (hE : Ext Z0 F0 S) (hA0 : ∀ q z, Z0.P q z → S.P q z)
    {e k : Nat} (σ : (B.obj (e + 2 : Nat)).car) (hk0 : 0 < k) (hk : k < e + 2) (hQ : Quasi X)
    (inj : ∀ (q : Nat) (g g' : DHom q (e + 2)), (B.obj (q : Nat)).eqv (mp B g σ) (mp B g' σ) →
      ∀ r, r ≤ q → g.f r = g'.f r)
    (hornIn : ∀ (q : Nat) (g : DHom q (e + 2)), S.P q (mp B g σ) →
      ∃ i, i ≤ e + 2 ∧ i ≠ k ∧ ∀ r, r ≤ q → g.f r ≠ i)
    (hornOut : ∀ i, i ≤ e + 2 → i ≠ k → S.P (e + 1) (dd B i σ))
    (hincl : ∀ q z, S'.P q z → (genSub S σ).P q z) : Ext Z0 F0 S' :=
  match hE with
  | ⟨G, hG⟩ => match attach Kw G σ hk0 hk hQ inj hornIn hornOut with
    | ⟨G1, h1⟩ => ⟨pres G1 hincl, fun q z h0 _ =>
        (X.obj _).trans (G1.app_eqv q ((B.obj _).refl z) _ (.inl (hA0 q z h0)))
          ((X.obj _).trans (h1 q z (hA0 q z h0)) (hG q z h0 (hA0 q z h0)))⟩

end gen

/-! ## small facts -/

theorem ne_of_lt' {a b : Nat} (h : a < b) : a ≠ b := fun e => by cases e; exact Nat.lt_irrefl a h

theorem ne_succ_self' (a : Nat) : a ≠ a + 1 := ne_of_lt' (Nat.lt_succ_self a)

theorem sk_ne (i r : Nat) : sk i r ≠ i :=
  match Nat.lt_or_ge r i with
  | .inl h => by rw [sk_lt h]; exact ne_of_lt' h
  | .inr h => by rw [sk_ge h]; exact fun e => ne_of_lt' (Nat.lt_succ_of_le h) e.symm

theorem sk0_pos (r : Nat) : 0 < sk 0 r := by rw [sk_ge (Nat.zero_le r)]; exact Nat.succ_pos r

theorem uW_pos {a b j : Nat} (h : a < j) : uW a b j ≠ 0 := fun e => by
  have h1 : uW a b j = stp (b + 1) j + 1 := by
    show stp a j + stp (b + 1) j = stp (b + 1) j + 1
    rw [stp_hi h, Nat.add_comm]
  exact Nat.noConfusion (h1.symm.trans e)

/-- dg a is injective away from a, a+1 -/
theorem dg_inj_off (a : Nat) {i j : Nat} (h1 : i ≠ a) (h2 : i ≠ a + 1) (e : dg a j = dg a i) : j = i :=
  match Nat.lt_or_ge a i with
  | .inr hia =>
    have hlt : i < a := Nat.lt_of_le_of_ne hia h1
    match Nat.lt_or_ge a j with
    | .inr hja => by rw [dg_0 hja, dg_0 hia] at e; exact e
    | .inl haj => match j, haj, e with
      | 0, haj, _ => absurd haj (Nat.not_lt_zero a)
      | t + 1, haj, e => by
        rw [dg_1 (Nat.le_of_lt_succ haj), dg_0 hia] at e
        exact absurd (Nat.lt_of_lt_of_le hlt (Nat.le_trans (Nat.le_of_lt_succ haj) (Nat.le_of_eq e))) (Nat.lt_irrefl i)
  | .inl hai => match i, hai, h2, e with
    | 0, hai, _, _ => absurd hai (Nat.not_lt_zero a)
    | s + 1, hai, h2, e =>
      have has : a < s := Nat.lt_of_le_of_ne (Nat.le_of_lt_succ hai) (fun e' => h2 (congrArg (· + 1) e'.symm))
      match Nat.lt_or_ge a j with
      | .inr hja => by
        rw [dg_0 hja, dg_1 (Nat.le_of_lt has)] at e
        exact absurd (Nat.lt_of_le_of_lt (Nat.le_trans (Nat.le_of_eq e.symm) hja) has) (Nat.lt_irrefl s)
      | .inl haj => match j, haj, e with
        | 0, haj, _ => absurd haj (Nat.not_lt_zero a)
        | t + 1, haj, e => by
          rw [dg_1 (Nat.le_of_lt_succ haj), dg_1 (Nat.le_of_lt has)] at e
          rw [e]

/-- a vertex condition violated at position w is missed -/
theorem missV {Q : Nat → Nat → Prop} {U V : Nat → Nat} {q d : Nat} (g : DHom q d)
    (h : ∀ r, r ≤ q → Q (U (g.f r)) (V (g.f r))) {w : Nat} (hw : ¬ Q (U w) (V w)) :
    ∀ r, r ≤ q → g.f r ≠ w := fun r hr e => hw (by rw [← e]; exact h r hr)

/-! ## the sgm stages -/

/-- (a', b') comes before (a, b) in the order (b, a) -/
def earlier (a' b' a b : Nat) : Prop := b' < b ∨ (b' = b ∧ a' < a)

/-- the stage before sgm a b -/
def PreS (n a b : Nat) : Sub (Pr.{w} n) :=
  sOr A0 (sEx (ι := Nat × Nat) (fun p => p.1 ≤ p.2 ∧ p.2 < n ∧ earlier p.1 p.2 a b)
    (fun p => SubV (memS p.1 p.2)))

section sgmstep

variable {m a b : Nat} (hab : a ≤ b) (hb : b < m + 1) (ha : a ≤ m + 1)

include hab in
theorem nmemS0 {a' b' : Nat} (h : a' < a) : ¬ memS a' b' (uW a b a) (dg a a) := fun hm => by
  rw [uW_0 hab (Nat.le_refl a), dg_0 (Nat.le_refl a)] at hm
  exact match hm with
    | .inl ⟨_, h'⟩ => Nat.lt_irrefl a (Nat.lt_of_le_of_lt h' h)
    | .inr (.inl ⟨h', _⟩) => Nat.noConfusion h'
    | .inr (.inr ⟨h', _⟩) => Nat.noConfusion h'

include hab in
theorem nmemS1 {a' b' : Nat} (h : b' < b) : ¬ memS a' b' (uW a b (b + 1)) (dg a (b + 1)) := fun hm => by
  rw [uW_1 hab (Nat.le_refl b), dg_1 hab] at hm
  exact match hm with
    | .inl ⟨h', _⟩ => Nat.noConfusion h'
    | .inr (.inl ⟨_, _, h'⟩) => Nat.lt_irrefl b (Nat.lt_of_le_of_lt h' h)
    | .inr (.inr ⟨h', _⟩) => Nat.noConfusion h' (fun h'' => Nat.noConfusion h'')

include hab hb in
/-- S ∩ <sgm a b> lies in the horn at (1,a) -/
theorem sgm_hornIn (q : Nat) (g : DHom q (m + 2))
    (h : (PreS.{w} (m + 1) a b).P q (mp (Pr.{w} (m + 1)) g (sgm (b := b) ha))) :
    ∃ i, i ≤ m + 2 ∧ i ≠ a + 1 ∧ ∀ r, r ≤ q → g.f r ≠ i :=
  match h with
  | .inl (.inl (.inl h0)) => ⟨0, Nat.zero_le _, fun e => Nat.noConfusion e,
      missV (Q := fun x _ => x ≠ 0) (U := uW a b) (V := dg a) g h0 (fun hne => hne (uW_0 hab (Nat.zero_le a)))⟩
  | .inl (.inl (.inr h2)) => ⟨m + 2, Nat.le_refl _,
      fun e => Nat.lt_irrefl (m + 1) (Nat.lt_of_le_of_lt (Nat.le_of_eq (congrArg Nat.pred e))
        (Nat.lt_succ_of_le (Nat.le_trans hab (Nat.le_of_lt_succ hb)))),
      missV (Q := fun x _ => x ≠ 2) (U := uW a b) (V := dg a) g h2
        (fun hne => hne (uW_2 hab (Nat.succ_lt_succ hb)))⟩
  | .inl (.inr ⟨y, hy, hv⟩) => match Nat.lt_or_ge a y with
    | .inr hya => ⟨y, Nat.le_trans hy (Nat.le_succ _),
        ne_of_lt' (Nat.lt_succ_of_le hya),
        missV (Q := fun _ v => v ≠ y) (U := uW a b) (V := dg a) g hv (fun hne => hne (dg_0 hya))⟩
    | .inl hay => ⟨y + 1, Nat.succ_le_succ hy,
        fun e => ne_of_lt' hay (congrArg Nat.pred e).symm,
        missV (Q := fun _ v => v ≠ y) (U := uW a b) (V := dg a) g hv (fun hne => hne (dg_1 (Nat.le_of_lt hay)))⟩
  | .inr ⟨⟨a', b'⟩, ⟨hc1, _, hearl⟩, hm⟩ => match Nat.lt_or_ge a' a with
    | .inl ha' => ⟨a, Nat.le_trans ha (Nat.le_succ _), ne_succ_self' a,
        missV (Q := memS a' b') (U := uW a b) (V := dg a) g hm (nmemS0 hab ha')⟩
    | .inr ha' =>
      have hb' : b' < b := hearl.elim id (fun ⟨_, h'⟩ => absurd h' (Nat.not_lt_of_ge ha'))
      have hab' : a < b := Nat.lt_of_le_of_lt (Nat.le_trans ha' hc1) hb'
      ⟨b + 1, Nat.le_succ_of_le (Nat.succ_le_of_lt hb),
        fun e => ne_of_lt' hab' (congrArg Nat.pred e).symm,
        missV (Q := memS a' b') (U := uW a b) (V := dg a) g hm (nmemS1 hab hb')⟩

include hab in
/-- vertices of sgm a b other than (0,a) lie on sgm (a-1) b -/
theorem memS_pred (hp : 0 < a) {j : Nat} (hj : j ≠ a) : memS (Nat.pred a) b (uW a b j) (dg a j) :=
  sg_cases (a := a) (b := b) (P := fun j => j ≠ a → memS (Nat.pred a) b (uW a b j) (dg a j))
    (fun i h hne => .inl ⟨uW_0 hab h, by
      rw [dg_0 h]
      exact Nat.le_of_lt_succ (Nat.lt_of_lt_of_le (Nat.lt_of_le_of_ne h hne)
        (Nat.le_of_eq (pred_succ_of_pos hp).symm))⟩)
    (fun y h1 h2 _ => .inr (.inl ⟨uW_1 h1 h2, by
      rw [dg_1 h1]; exact ⟨Nat.le_trans (Nat.pred_le a) h1, h2⟩⟩))
    (fun y h _ => .inr (.inr ⟨uW_2 hab (Nat.succ_lt_succ (Nat.lt_of_succ_le h)),
      by rw [dg_1 (Nat.le_trans hab (Nat.le_trans (Nat.le_succ b) h))]; exact h⟩)) j hj

include hab hb in
/-- the faces of sgm a b other than the one at (1,a) lie in the previous stage -/
theorem sgm_hornOut (i : Nat) (hi : i ≤ m + 2) (hik : i ≠ a + 1) :
    (PreS.{w} (m + 1) a b).P (m + 1) (dd (Pr.{w} (m + 1)) i (sgm (b := b) ha)) :=
  have off : i ≠ a → (PreS.{w} (m + 1) a b).P (m + 1) (dd (Pr.{w} (m + 1)) i (sgm (b := b) ha)) :=
    fun h1 => .inl (.inr ⟨dg a i, (degen (m + 1) a ha).bound i hi,
      fun r _ e => sk_ne i r (dg_inj_off a h1 hik e)⟩)
  match Nat.lt_or_ge i a with
  | .inl h => off (ne_of_lt' h)
  | .inr h => match Nat.lt_or_ge a i with
    | .inl h' => off (fun e => ne_of_lt' h' e.symm)
    | .inr h' =>
      have he : i = a := Nat.le_antisymm h' h
      match Nat.eq_zero_or_pos a with
      | .inl h0 => .inl (.inl (.inl (fun r _ => uW_pos (by rw [he, h0]; exact sk0_pos r))))
      | .inr hp => .inr ⟨(Nat.pred a, b), ⟨Nat.le_trans (Nat.pred_le a) hab, hb,
          .inr ⟨rfl, Nat.lt_of_lt_of_le (Nat.lt_succ_self _) (Nat.le_of_eq (pred_succ_of_pos hp))⟩⟩,
          fun r _ => memS_pred hab hp (fun e => sk_ne i r (e.trans he.symm))⟩

end sgmstep

/-! ## the order of the sgm stages -/

theorem next_a {a b : Nat} : ∀ a' b', a' ≤ b' → earlier a' b' (a + 1) b → earlier a' b' a b ∨ (a' = a ∧ b' = b)
  | _, _, _, .inl h => .inl (.inl h)
  | a', _, _, .inr ⟨hb, ha'⟩ => match Nat.lt_or_ge a' a with
    | .inl h => .inl (.inr ⟨hb, h⟩)
    | .inr h => .inr ⟨Nat.le_antisymm (Nat.le_of_lt_succ ha') h, hb⟩

theorem next_b {b : Nat} : ∀ a' b', a' ≤ b' → earlier a' b' 0 (b + 1) → earlier a' b' b b ∨ (a' = b ∧ b' = b)
  | a', b', hab', .inl h => match Nat.lt_or_ge b' b with
    | .inl h' => .inl (.inl h')
    | .inr h' =>
      have hbb : b' = b := Nat.le_antisymm (Nat.le_of_lt_succ h) h'
      match Nat.lt_or_ge a' b with
      | .inl h'' => .inl (.inr ⟨hbb, h''⟩)
      | .inr h'' => .inr ⟨Nat.le_antisymm (Nat.le_trans hab' (Nat.le_of_eq hbb)) h'', hbb⟩
  | _, _, _, .inr ⟨_, h⟩ => absurd h (Nat.not_lt_zero _)

/-- the next sgm stage lies in the previous one with sgm a b attached -/
theorem inclS {n a b a2 b2 : Nat} (hab : a ≤ b) (ha : a ≤ n)
    (hnext : ∀ a' b', a' ≤ b' → earlier a' b' a2 b2 → earlier a' b' a b ∨ (a' = a ∧ b' = b)) :
    ∀ q z, (PreS.{w} n a2 b2).P q z → (genSub (PreS.{w} n a b) (sgm (b := b) ha)).P q z
  | _, _, .inl h0 => .inl (.inl h0)
  | _, z, .inr ⟨⟨a', b'⟩, ⟨c1, c2, c3⟩, hm⟩ => match hnext a' b' c1 c3 with
    | .inl he => .inl (.inr ⟨⟨a', b'⟩, ⟨c1, c2, he⟩, hm⟩)
    | .inr ⟨h1, h2⟩ => .inr (sgm_gen hab ha z (by subst h1; subst h2; exact hm))

/-- one sgm step of the filtration -/
theorem stepS (Kw : ClW.{w}) {X : SSet.{w}} (hQ : Quasi X) {m : Nat} (F0 : PMap (A0.{w} (n := m + 1)) X) {a b a2 b2 : Nat}
    (hab : a ≤ b) (hb : b < m + 1)
    (hnext : ∀ a' b', a' ≤ b' → earlier a' b' a2 b2 → earlier a' b' a b ∨ (a' = a ∧ b' = b))
    (hE : Ext A0 F0 (PreS.{w} (m + 1) a b)) : Ext A0 F0 (PreS.{w} (m + 1) a2 b2) :=
  have ha : a ≤ m + 1 := Nat.le_trans hab (Nat.le_of_lt hb)
  ext_step Kw A0 F0 hE (fun _ _ h => .inl h) (e := m) (k := a + 1) (sgm (b := b) ha) (Nat.succ_pos a)
    (Nat.succ_lt_succ (Nat.lt_of_le_of_lt hab hb)) hQ (sgm_inj.{w} hab ha)
    (sgm_hornIn.{w} hab hb ha) (sgm_hornOut.{w} hab hb ha) (inclS hab ha hnext)

end IC

end Found

#print axioms Found.IC.ext_step
#print axioms Found.IC.dg_inj_off
#print axioms Found.IC.sgm_hornIn
#print axioms Found.IC.sgm_hornOut
#print axioms Found.IC.inclS
#print axioms Found.IC.stepS

found/IC25.lean

lean_all/found/IC25.lean

/-
  T153 ((infinity,1)-categories), part 25. found/IC25 — the prism
  Delta^2 x Delta^n, III: the second half of the filtration.  After all
  sgm a b, the simplices tau a b (a <= b <= n) are attached in the order
  (b, a), each along its inner horn at the vertex (1,a): the faces other
  than (1,a) lie in A0, in an earlier tau (the face at (0,a)), in
  sgm a (b-1) (the face at (1,b)) or in sgm a b (the face at (2,b)).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.IC24

namespace Found

namespace IC

open Simp

universe w

/-- the stage before tau a b: A0, every sgm, and the earlier tau -/
def PreT (n a b : Nat) : Sub (Pr.{w} n) :=
  sOr A0 (sOr (sEx (ι := Nat × Nat) (fun p => p.1 ≤ p.2 ∧ p.2 < n) (fun p => SubV (memS p.1 p.2)))
    (sEx (ι := Nat × Nat) (fun p => p.1 ≤ p.2 ∧ p.2 ≤ n ∧ earlier p.1 p.2 a b) (fun p => SubV (memT p.1 p.2))))

/-! ## vertices not on a staircase -/

theorem nS0 {a' b' a : Nat} (h : a' < a) : ¬ memS a' b' 0 a := fun hm => match hm with
  | .inl ⟨_, h'⟩ => Nat.lt_irrefl a (Nat.lt_of_le_of_lt h' h)
  | .inr (.inl ⟨h', _⟩) => Nat.noConfusion h'
  | .inr (.inr ⟨h', _⟩) => Nat.noConfusion h'

theorem nT0 {a' b' a : Nat} (h : a' < a) : ¬ memT a' b' 0 a := fun hm => match hm with
  | .inl ⟨_, h'⟩ => Nat.lt_irrefl a (Nat.lt_of_le_of_lt h' h)
  | .inr (.inl ⟨h', _⟩) => Nat.noConfusion h'
  | .inr (.inr ⟨h', _⟩) => Nat.noConfusion h'

theorem nS1 {a' b' b : Nat} (h : b' < b) : ¬ memS a' b' 1 b := fun hm => match hm with
  | .inl ⟨h', _⟩ => Nat.noConfusion h'
  | .inr (.inl ⟨_, _, h'⟩) => Nat.lt_irrefl b (Nat.lt_of_le_of_lt h' h)
  | .inr (.inr ⟨h', _⟩) => Nat.noConfusion h' (fun h'' => Nat.noConfusion h'')

theorem nT1 {a' b' b : Nat} (h : b' < b) : ¬ memT a' b' 1 b := fun hm => match hm with
  | .inl ⟨h', _⟩ => Nat.noConfusion h'
  | .inr (.inl ⟨_, _, h'⟩) => Nat.lt_irrefl b (Nat.lt_of_le_of_lt h' h)
  | .inr (.inr ⟨h', _⟩) => Nat.noConfusion h' (fun h'' => Nat.noConfusion h'')

theorem nS2 {a' b' b : Nat} (h : b ≤ b') : ¬ memS a' b' 2 b := fun hm => match hm with
  | .inl ⟨h', _⟩ => Nat.noConfusion h'
  | .inr (.inl ⟨h', _⟩) => Nat.noConfusion h' (fun h'' => Nat.noConfusion h'')
  | .inr (.inr ⟨_, h'⟩) => Nat.not_succ_le_self b' (Nat.le_trans h' h)

theorem uW_ne2 {a b j : Nat} (h : j ≤ b + 1) : uW a b j ≠ 2 := fun e => by
  have h1 : uW a b j ≤ 1 := by
    show stp a j + stp (b + 1) j ≤ 1
    rw [stp_lo h]
    exact stp_le1 a j
  rw [e] at h1
  exact Nat.not_succ_le_self 1 h1

section taustep

variable {n a b : Nat} (hab : a ≤ b) (hb : b ≤ n) (ha : a ≤ n)

include hab in
/-- vT is injective away from a, a+1, b+1, b+2 -/
theorem vT_inj_off {i j : Nat} (h1 : i ≠ a) (h2 : i ≠ a + 1) (h3 : i ≠ b + 1) (h4 : i ≠ b + 2)
    (e : vT a b j = vT a b i) : j = i :=
  have hd : dg (b + 1) i ≠ a ∧ dg (b + 1) i ≠ a + 1 := match Nat.lt_or_ge (b + 1) i with
    | .inr hi => by rw [dg_lt (Nat.lt_succ_of_le hi)]; exact ⟨h1, h2⟩
    | .inl hi => match i, hi, h4 with
      | 0, hi, _ => absurd hi (Nat.not_lt_zero _)
      | s + 1, hi, h4 =>
        have hs : b + 1 < s := Nat.lt_of_le_of_ne (Nat.le_of_lt_succ hi) (fun e' => h4 (congrArg (· + 1) e'.symm))
        by
          rw [dg_1 (Nat.le_of_lt_succ hi)]
          exact ⟨fun e' => Nat.lt_irrefl s (Nat.lt_of_le_of_lt (Nat.le_of_eq e') (Nat.lt_trans
              (Nat.lt_succ_of_le hab) hs)),
            fun e' => Nat.lt_irrefl s (Nat.lt_of_le_of_lt (Nat.le_of_eq e') (Nat.lt_of_le_of_lt
              (Nat.succ_le_succ hab) hs))⟩
  dg_inj_off (b + 1) h3 h4 (dg_inj_off a hd.1 hd.2 e)

include hab hb in
/-- S ∩ <tau a b> lies in the horn at (1,a) -/
theorem tau_hornIn (q : Nat) (g : DHom q (n + 2))
    (h : (PreT.{w} n a b).P q (mp (Pr.{w} n) g (tau ha hb))) :
    ∃ i, i ≤ n + 2 ∧ i ≠ a + 1 ∧ ∀ r, r ≤ q → g.f r ≠ i :=
  match h with
  | .inl (.inl (.inl h0)) => ⟨0, Nat.zero_le _, fun e => Nat.noConfusion e,
      missV (Q := fun x _ => x ≠ 0) (U := uW a b) (V := vT a b) g h0 (fun hne => hne (uW_0 hab (Nat.zero_le a)))⟩
  | .inl (.inl (.inr h2)) => ⟨n + 2, Nat.le_refl _,
      fun e => Nat.not_succ_le_self n (Nat.le_trans (Nat.le_of_eq (congrArg Nat.pred e)) ha),
      missV (Q := fun x _ => x ≠ 2) (U := uW a b) (V := vT a b) g h2
        (fun hne => hne (uW_2 hab (Nat.succ_lt_succ (Nat.lt_succ_of_le hb))))⟩
  | .inl (.inr ⟨y, hy, hv⟩) => match Nat.lt_or_ge a y with
    | .inr hya => ⟨y, Nat.le_trans hy (Nat.le_trans (Nat.le_succ n) (Nat.le_succ _)),
        ne_of_lt' (Nat.lt_succ_of_le hya),
        missV (Q := fun _ v => v ≠ y) (U := uW a b) (V := vT a b) g hv (fun hne => hne (vT_0 hab hya))⟩
    | .inl hay => match Nat.lt_or_ge b y with
      | .inr hyb => ⟨y + 1, Nat.le_succ_of_le (Nat.succ_le_succ hy),
          fun e => ne_of_lt' hay (congrArg Nat.pred e).symm,
          missV (Q := fun _ v => v ≠ y) (U := uW a b) (V := vT a b) g hv
            (fun hne => hne (vT_1 (Nat.le_of_lt hay) hyb))⟩
      | .inl hby => ⟨y + 2, Nat.succ_le_succ (Nat.succ_le_succ hy),
          fun e => ne_of_lt' (Nat.lt_trans hay (Nat.lt_succ_self y)) (congrArg Nat.pred e).symm,
          missV (Q := fun _ v => v ≠ y) (U := uW a b) (V := vT a b) g hv
            (fun hne => hne (vT_2 hab (Nat.le_of_lt hby)))⟩
  | .inr (.inl ⟨⟨a', b'⟩, ⟨hc1, _⟩, hm⟩) => match Nat.lt_or_ge b' b with
    | .inl hb' => match Nat.lt_or_ge a b with
      | .inl hab' => ⟨b + 1, Nat.le_succ_of_le (Nat.succ_le_succ hb),
          fun e => ne_of_lt' hab' (congrArg Nat.pred e).symm,
          missV (Q := memS a' b') (U := uW a b) (V := vT a b) g hm
            (by rw [uW_1 hab (Nat.le_refl b), vT_1 hab (Nat.le_refl b)]; exact nS1 hb')⟩
      | .inr hba => ⟨a, Nat.le_trans ha (Nat.le_trans (Nat.le_succ n) (Nat.le_succ _)), ne_succ_self' a,
          missV (Q := memS a' b') (U := uW a b) (V := vT a b) g hm
            (by
              rw [uW_0 hab (Nat.le_refl a), vT_0 hab (Nat.le_refl a)]
              exact nS0 (Nat.lt_of_le_of_lt hc1 (Nat.lt_of_lt_of_le hb' hba)))⟩
    | .inr hbb => ⟨b + 2, Nat.succ_le_succ (Nat.succ_le_succ hb),
        fun e => Nat.not_succ_le_self b (Nat.le_trans (Nat.le_of_eq (congrArg Nat.pred e)) hab),
        missV (Q := memS a' b') (U := uW a b) (V := vT a b) g hm
          (by
            rw [uW_2 hab (Nat.lt_succ_self (b + 1)), vT_2 hab (Nat.le_refl b)]
            exact nS2 hbb)⟩
  | .inr (.inr ⟨⟨a', b'⟩, ⟨hc1, _, hearl⟩, hm⟩) => match Nat.lt_or_ge a' a with
    | .inl ha' => ⟨a, Nat.le_trans ha (Nat.le_trans (Nat.le_succ n) (Nat.le_succ _)), ne_succ_self' a,
        missV (Q := memT a' b') (U := uW a b) (V := vT a b) g hm
          (by rw [uW_0 hab (Nat.le_refl a), vT_0 hab (Nat.le_refl a)]; exact nT0 ha')⟩
    | .inr ha' =>
      have hb' : b' < b := hearl.elim id (fun ⟨_, h'⟩ => absurd h' (Nat.not_lt_of_ge ha'))
      have hab' : a < b := Nat.lt_of_le_of_lt (Nat.le_trans ha' hc1) hb'
      ⟨b + 1, Nat.le_succ_of_le (Nat.succ_le_succ hb),
        fun e => ne_of_lt' hab' (congrArg Nat.pred e).symm,
        missV (Q := memT a' b') (U := uW a b) (V := vT a b) g hm
          (by rw [uW_1 hab (Nat.le_refl b), vT_1 hab (Nat.le_refl b)]; exact nT1 hb')⟩

include hab in
/-- vertices of tau a b other than (0,a) lie on tau (a-1) b -/
theorem memT_pred (hp : 0 < a) {j : Nat} (hj : j ≠ a) : memT (Nat.pred a) b (uW a b j) (vT a b j) :=
  ta_cases (a := a) (b := b) (P := fun j => j ≠ a → memT (Nat.pred a) b (uW a b j) (vT a b j))
    (fun i h hne => .inl ⟨uW_0 hab h, by
      rw [vT_0 hab h]
      exact Nat.le_of_lt_succ (Nat.lt_of_lt_of_le (Nat.lt_of_le_of_ne h hne)
        (Nat.le_of_eq (pred_succ_of_pos hp).symm))⟩)
    (fun y h1 h2 _ => .inr (.inl ⟨uW_1 h1 h2, by
      rw [vT_1 h1 h2]; exact ⟨Nat.le_trans (Nat.pred_le a) h1, h2⟩⟩))
    (fun y h _ => .inr (.inr ⟨uW_2 hab (Nat.succ_lt_succ (Nat.lt_succ_of_le h)),
      by rw [vT_2 hab h]; exact h⟩)) j hj

include hab in
/-- vertices of tau a b other than (1,b) lie on sgm a (b-1) -/
theorem memS_tauB (hlt : a < b) {j : Nat} (hj : j ≠ b + 1) : memS a (Nat.pred b) (uW a b j) (vT a b j) :=
  have hbp : Nat.pred b + 1 = b := pred_succ_of_pos (Nat.lt_of_le_of_lt (Nat.zero_le a) hlt)
  ta_cases (a := a) (b := b) (P := fun j => j ≠ b + 1 → memS a (Nat.pred b) (uW a b j) (vT a b j))
    (fun i h _ => .inl ⟨uW_0 hab h, by rw [vT_0 hab h]; exact h⟩)
    (fun y h1 h2 hne => .inr (.inl ⟨uW_1 h1 h2, by
      rw [vT_1 h1 h2]
      exact ⟨h1, Nat.le_of_lt_succ (Nat.lt_of_lt_of_le
        (Nat.lt_of_le_of_ne h2 (fun e => hne (congrArg (· + 1) e))) (Nat.le_of_eq hbp.symm))⟩⟩))
    (fun y h _ => .inr (.inr ⟨uW_2 hab (Nat.succ_lt_succ (Nat.lt_succ_of_le h)),
      by rw [vT_2 hab h]; exact Nat.le_trans (Nat.le_of_eq hbp) h⟩)) j hj

include hab in
/-- vertices of tau a b other than (2,b) lie on sgm a b -/
theorem memS_tauC {j : Nat} (hj : j ≠ b + 2) : memS a b (uW a b j) (vT a b j) :=
  ta_cases (a := a) (b := b) (P := fun j => j ≠ b + 2 → memS a b (uW a b j) (vT a b j))
    (fun i h _ => .inl ⟨uW_0 hab h, by rw [vT_0 hab h]; exact h⟩)
    (fun y h1 h2 _ => .inr (.inl ⟨uW_1 h1 h2, by rw [vT_1 h1 h2]; exact ⟨h1, h2⟩⟩))
    (fun y h hne => .inr (.inr ⟨uW_2 hab (Nat.succ_lt_succ (Nat.lt_succ_of_le h)), by
      rw [vT_2 hab h]
      exact Nat.lt_of_le_of_ne h (fun e => hne (congrArg (· + 2) e.symm))⟩)) j hj

include hab hb in
/-- the faces of tau a b other than the one at (1,a) lie in the previous stage -/
theorem tau_hornOut (i : Nat) (hi : i ≤ n + 2) (hik : i ≠ a + 1) :
    (PreT.{w} n a b).P (n + 1) (dd (Pr.{w} n) i (tau ha hb)) :=
  match Nat.decEq i a with
  | isTrue he => match Nat.eq_zero_or_pos a with
    | .inl h0 => .inl (.inl (.inl (fun r _ => uW_pos (by rw [he, h0]; exact sk0_pos r))))
    | .inr hp => .inr (.inr ⟨(Nat.pred a, b), ⟨Nat.le_trans (Nat.pred_le a) hab, hb,
        .inr ⟨rfl, Nat.lt_of_lt_of_le (Nat.lt_succ_self _) (Nat.le_of_eq (pred_succ_of_pos hp))⟩⟩,
        fun r _ => memT_pred hab hp (fun e => sk_ne i r (e.trans he.symm))⟩)
  | isFalse hna => match Nat.decEq i (b + 1) with
    | isTrue he =>
      have hlt : a < b := Nat.lt_of_le_of_ne hab (fun e => hik (he.trans (congrArg (· + 1) e.symm)))
      have hbp : Nat.pred b + 1 = b := pred_succ_of_pos (Nat.lt_of_le_of_lt (Nat.zero_le a) hlt)
      .inr (.inl ⟨(a, Nat.pred b),
        ⟨Nat.le_of_lt_succ (Nat.lt_of_lt_of_le hlt (Nat.le_of_eq hbp.symm)),
          Nat.lt_of_lt_of_le (Nat.lt_of_lt_of_le (Nat.lt_succ_self _) (Nat.le_of_eq hbp)) hb⟩,
        fun r _ => memS_tauB hab hlt (fun e => sk_ne i r (e.trans he.symm))⟩)
    | isFalse hnb => match Nat.decEq i (b + 2) with
      | isTrue he => match Nat.lt_or_ge b n with
        | .inl hbn => .inr (.inl ⟨(a, b), ⟨hab, hbn⟩,
            fun r _ => memS_tauC hab (fun e => sk_ne i r (e.trans he.symm))⟩)
        | .inr hnb' => .inl (.inl (.inr (fun r hr => uW_ne2 (by
            rw [he]
            show sk (b + 2) r ≤ b + 1
            rw [sk_lt (Nat.lt_succ_of_le (Nat.le_trans hr (Nat.succ_le_succ hnb')))]
            exact Nat.le_trans hr (Nat.succ_le_succ hnb')))))
      | isFalse hnc => .inl (.inr ⟨vT a b i,
          (dComp (degen (n + 1) (b + 1) (Nat.succ_le_succ hb)) (degen n a ha)).bound i hi,
          fun r _ e => sk_ne i r (vT_inj_off hab hna hik hnb hnc e)⟩)

end taustep

/-- the next tau stage lies in the previous one with tau a b attached -/
theorem inclT {n a b a2 b2 : Nat} (hab : a ≤ b) (hb : b ≤ n) (ha : a ≤ n)
    (hnext : ∀ a' b', a' ≤ b' → earlier a' b' a2 b2 → earlier a' b' a b ∨ (a' = a ∧ b' = b)) :
    ∀ q z, (PreT.{w} n a2 b2).P q z → (genSub (PreT.{w} n a b) (tau ha hb)).P q z
  | _, _, .inl h0 => .inl (.inl h0)
  | _, _, .inr (.inl hs) => .inl (.inr (.inl hs))
  | _, z, .inr (.inr ⟨⟨a', b'⟩, ⟨c1, c2, c3⟩, hm⟩) => match hnext a' b' c1 c3 with
    | .inl he => .inl (.inr (.inr ⟨⟨a', b'⟩, ⟨c1, c2, he⟩, hm⟩))
    | .inr ⟨h1, h2⟩ => .inr (tau_gen hab ha hb z (by subst h1; subst h2; exact hm))

/-- one tau step of the filtration -/
theorem stepT (Kw : ClW.{w}) {X : SSet.{w}} (hQ : Quasi X) {n : Nat} (F0 : PMap (A0.{w} (n := n)) X)
    {a b a2 b2 : Nat} (hab : a ≤ b) (hb : b ≤ n)
    (hnext : ∀ a' b', a' ≤ b' → earlier a' b' a2 b2 → earlier a' b' a b ∨ (a' = a ∧ b' = b))
    (hE : Ext A0 F0 (PreT.{w} n a b)) : Ext A0 F0 (PreT.{w} n a2 b2) :=
  have ha : a ≤ n := Nat.le_trans hab hb
  ext_step Kw A0 F0 hE (fun _ _ h => .inl h) (e := n) (k := a + 1) (tau ha hb) (Nat.succ_pos a)
    (Nat.succ_lt_succ (Nat.lt_succ_of_le ha)) hQ (tau_inj.{w} hab ha hb)
    (tau_hornIn.{w} hab hb ha) (tau_hornOut.{w} hab hb ha) (inclT hab hb ha hnext)

end IC

end Found

#print axioms Found.IC.vT_inj_off
#print axioms Found.IC.tau_hornIn
#print axioms Found.IC.tau_hornOut
#print axioms Found.IC.inclT
#print axioms Found.IC.stepT

found/IC26.lean

lean_all/found/IC26.lean

/-
  T153 ((infinity,1)-categories), part 26. found/IC26 — the prism
  Delta^2 x Delta^n, IV (Joyal; Rezk 78.5): every simplex of the prism lies
  on a staircase tau a b, so running the filtration of IC24–IC25 to the end
  gives: if X is a quasi-category, every map
    Lambda^2_1 x Delta^n ∪ Delta^2 x ∂Delta^n -> X
  extends to Delta^2 x Delta^n -> X  (prism_ext).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.IC25

namespace Found

namespace IC

open Simp

universe w

/-! ## running maxima -/

def sel (c : Nat) (U V : Nat → Nat) (r : Nat) : Nat := cond (gtb (c + 1) (U r)) (V r) 0

theorem sel_lo {c : Nat} {U V : Nat → Nat} {r : Nat} (h : U r ≤ c) : sel c U V r = V r :=
  congrArg (fun t => cond t (V r) 0) (lt_gtb (c + 1) (U r) (Nat.lt_succ_of_le h))

theorem sel_le {c : Nat} {U V : Nat → Nat} {r t : Nat} (h : U r ≤ c → V r ≤ t) : sel c U V r ≤ t :=
  match Nat.lt_or_ge c (U r) with
  | .inl h' => by
    have e : sel c U V r = 0 := congrArg (fun t => cond t (V r) 0) (le_gtb (c + 1) (U r) h')
    rw [e]; exact Nat.zero_le t
  | .inr h' => by rw [sel_lo h']; exact h h'

def mx2 (x y : Nat) : Nat := cond (gtb x y) x y

theorem mx2_gt {x y : Nat} (h : y < x) : mx2 x y = x := congrArg (fun t => cond t x y) (lt_gtb x y h)
theorem mx2_ge {x y : Nat} (h : x ≤ y) : mx2 x y = y := congrArg (fun t => cond t x y) (le_gtb x y h)

theorem le_mx2_l (x y : Nat) : x ≤ mx2 x y :=
  match Nat.lt_or_ge y x with
  | .inl h => by rw [mx2_gt h]; exact Nat.le_refl x
  | .inr h => by rw [mx2_ge h]; exact h

theorem le_mx2_r (x y : Nat) : y ≤ mx2 x y :=
  match Nat.lt_or_ge y x with
  | .inl h => by rw [mx2_gt h]; exact Nat.le_of_lt h
  | .inr h => by rw [mx2_ge h]; exact Nat.le_refl y

theorem mx2_le {x y t : Nat} (hx : x ≤ t) (hy : y ≤ t) : mx2 x y ≤ t :=
  match Nat.lt_or_ge y x with
  | .inl h => by rw [mx2_gt h]; exact hx
  | .inr h => by rw [mx2_ge h]; exact hy

/-- max of V r over r <= q with U r <= c (0 if none) -/
def mxv (c : Nat) (U V : Nat → Nat) : Nat → Nat
  | 0 => sel c U V 0
  | q + 1 => mx2 (mxv c U V q) (sel c U V (q + 1))

theorem le_mxv {c : Nat} {U V : Nat → Nat} : ∀ q r, r ≤ q → U r ≤ c → V r ≤ mxv c U V q
  | 0, r, hr, hu => by
    have e := Nat.eq_zero_of_le_zero hr
    subst e
    show V 0 ≤ sel c U V 0
    rw [sel_lo hu]
    exact Nat.le_refl _
  | q + 1, r, hr, hu => match Nat.lt_or_ge r (q + 1) with
    | .inl h => Nat.le_trans (le_mxv q r (Nat.le_of_lt_succ h) hu) (le_mx2_l _ _)
    | .inr h => by
      have e := Nat.le_antisymm hr h
      subst e
      have h1 : V (q + 1) ≤ sel c U V (q + 1) := by rw [sel_lo hu]; exact Nat.le_refl _
      exact Nat.le_trans h1 (le_mx2_r _ _)

theorem mxv_le {c : Nat} {U V : Nat → Nat} {t : Nat} : ∀ q, (∀ r, r ≤ q → U r ≤ c → V r ≤ t) → mxv c U V q ≤ t
  | 0, h => sel_le (h 0 (Nat.le_refl 0))
  | q + 1, h => mx2_le (mxv_le q (fun r hr => h r (Nat.le_succ_of_le hr))) (sel_le (h (q + 1) (Nat.le_refl _)))

/-! ## every simplex of the prism lies on a staircase -/

theorem before {n q : Nat} {U : DHom q 2} {V : DHom q n} {r r' : Nat} (hr : r ≤ q) (hr' : r' ≤ q)
    (h : U.f r' < U.f r) : V.f r' ≤ V.f r :=
  match Nat.lt_or_ge r' r with
  | .inl h' => dmono V (Nat.le_of_lt h') hr
  | .inr h' => absurd (dmono U h' hr') (Nat.not_le_of_gt h)

theorem cover {n q : Nat} (z : ((Pr.{w} n).obj (q : Nat)).car) : (PreT.{w} n 0 (n + 1)).P q z :=
  have hab : mxv 0 (prU z).f (prV z).f q ≤ mxv 1 (prU z).f (prV z).f q :=
    mxv_le q (fun r hr hu => le_mxv q r hr (Nat.le_trans hu (Nat.zero_le 1)))
  have hb : mxv 1 (prU z).f (prV z).f q ≤ n := mxv_le q (fun r hr _ => (prV z).bound r hr)
  .inr (.inr ⟨(mxv 0 (prU z).f (prV z).f q, mxv 1 (prU z).f (prV z).f q),
    ⟨hab, hb, .inl (Nat.lt_succ_of_le hb)⟩, fun r hr =>
    match Nat.lt_or_ge 0 ((prU z).f r) with
    | .inr h0 => .inl ⟨Nat.eq_zero_of_le_zero h0, le_mxv q r hr h0⟩
    | .inl h0 => match Nat.lt_or_ge 1 ((prU z).f r) with
      | .inr h1 => .inr (.inl ⟨Nat.le_antisymm h1 h0,
          mxv_le q (fun _ hr' hu' => before hr hr' (Nat.lt_of_le_of_lt hu' h0)), le_mxv q r hr h1⟩)
      | .inl h1 => .inr (.inr ⟨Nat.le_antisymm ((prU z).bound r hr) h1,
          mxv_le q (fun _ hr' hu' => before hr hr' (Nat.lt_of_le_of_lt hu' h1))⟩)⟩)

/-! ## running the filtration -/

section run

variable {B X : SSet.{w}} {Z0 : Sub B} {F0 : PMap Z0 X}

theorem ext_mono {S S' : Sub B} (h : ∀ q z, S'.P q z → S.P q z) (hE : Ext Z0 F0 S) : Ext Z0 F0 S' :=
  match hE with
  | ⟨G, hG⟩ => ⟨pres G h, fun q z h0 hS' => hG q z h0 (h q z hS')⟩

theorem ext_base {S : Sub B} (h : ∀ q z, S.P q z → Z0.P q z) : Ext Z0 F0 S :=
  ⟨pres F0 h, fun q z _ _ => F0.app_eqv q ((B.obj _).refl z) _ _⟩

end run

theorem preS_base {n q : Nat} (z : ((Pr.{w} n).obj (q : Nat)).car) (h : (PreS.{w} n 0 0).P q z) : (A0.{w} (n := n)).P q z :=
  match h with
  | .inl h0 => h0
  | .inr ⟨_, ⟨_, _, e⟩, _⟩ => e.elim (fun h => absurd h (Nat.not_lt_zero _)) (fun ⟨_, h⟩ => absurd h (Nat.not_lt_zero _))

theorem preT_base {n q : Nat} (z : ((Pr.{w} n).obj (q : Nat)).car) (h : (PreT.{w} n 0 0).P q z) :
    (PreS.{w} n 0 n).P q z :=
  match h with
  | .inl h0 => .inl h0
  | .inr (.inl ⟨p, ⟨c1, c2⟩, hm⟩) => .inr ⟨p, ⟨c1, c2, .inl c2⟩, hm⟩
  | .inr (.inr ⟨_, ⟨_, _, e⟩, _⟩) => e.elim (fun h => absurd h (Nat.not_lt_zero _)) (fun ⟨_, h⟩ => absurd h (Nat.not_lt_zero _))

section filt

variable (Kw : ClW.{w}) {X : SSet.{w}} (hQ : Quasi X)

include Kw hQ in
theorem rowS {m : Nat} (F0 : PMap (A0.{w} (n := m + 1)) X) {b : Nat} (hb : b < m + 1) :
    ∀ a, a ≤ b → Ext A0 F0 (PreS.{w} (m + 1) 0 b) → Ext A0 F0 (PreS.{w} (m + 1) a b)
  | 0, _, h => h
  | a + 1, ha, h => stepS Kw hQ F0 (a := a) (b := b) (Nat.le_of_succ_le ha) hb next_a
      (rowS F0 hb a (Nat.le_of_succ_le ha) h)

include Kw hQ in
theorem allS' {m : Nat} (F0 : PMap (A0.{w} (n := m + 1)) X) :
    ∀ b, b ≤ m + 1 → Ext A0 F0 (PreS.{w} (m + 1) 0 b)
  | 0, _ => ext_base (fun _ z h => preS_base z h)
  | b + 1, hb => stepS Kw hQ F0 (a := b) (b := b) (Nat.le_refl b) (Nat.lt_of_succ_le hb) next_b
      (rowS Kw hQ F0 (Nat.lt_of_succ_le hb) b (Nat.le_refl b) (allS' F0 b (Nat.le_of_succ_le hb)))

include Kw hQ in
theorem allS : ∀ (n : Nat) (F0 : PMap (A0.{w} (n := n)) X), Ext A0 F0 (PreS.{w} n 0 n)
  | 0, _ => ext_base (fun _ z h => preS_base z h)
  | m + 1, F0 => allS' Kw hQ F0 (m + 1) (Nat.le_refl _)

include Kw hQ in
theorem rowT {n : Nat} (F0 : PMap (A0.{w} (n := n)) X) {b : Nat} (hb : b ≤ n) :
    ∀ a, a ≤ b → Ext A0 F0 (PreT.{w} n 0 b) → Ext A0 F0 (PreT.{w} n a b)
  | 0, _, h => h
  | a + 1, ha, h => stepT Kw hQ F0 (a := a) (b := b) (Nat.le_of_succ_le ha) hb next_a
      (rowT F0 hb a (Nat.le_of_succ_le ha) h)

include Kw hQ in
theorem allT {n : Nat} (F0 : PMap (A0.{w} (n := n)) X) :
    ∀ b, b ≤ n + 1 → Ext A0 F0 (PreT.{w} n 0 b)
  | 0, _ => ext_mono (fun _ z h => preT_base z h) (allS Kw hQ n F0)
  | b + 1, hb => stepT Kw hQ F0 (a := b) (b := b) (Nat.le_refl b) (Nat.le_of_succ_le_succ hb) next_b
      (rowT Kw hQ F0 (Nat.le_of_succ_le_succ hb) b (Nat.le_refl b) (allT F0 b (Nat.le_of_succ_le hb)))

end filt

/-- the whole prism -/
def sTop (B : SSet.{w}) : Sub B where
  P _ _ := True
  P_eqv _ {_ _} _ _ := trivial
  P_map _ _ _ := trivial

/-- Joyal (Rezk 78.5): Lambda^2_1 x Delta^n ∪ Delta^2 x ∂Delta^n ⊂ Delta^2 x Delta^n is inner anodyne,
    in the form: a quasi-category X extends every map from the first to the second -/
theorem prism_ext (Kw : ClW.{w}) {X : SSet.{w}} (hQ : Quasi X) {n : Nat} (F0 : PMap (A0.{w} (n := n)) X) :
    ∃ φ : PNat (Pr.{w} n) X, ∀ q (z : ((Pr.{w} n).obj (q : Nat)).car) (h0 : (A0.{w} (n := n)).P q z),
      (X.obj (q : Nat)).eqv (φ.app q z) (F0.app q z h0) :=
  match ext_mono (S' := sTop (Pr.{w} n)) (fun _ z _ => cover z) (allT Kw hQ F0 (n + 1) (Nat.le_refl _)) with
  | ⟨G, hG⟩ => ⟨{
      app := fun q z => G.app q z trivial
      app_eqv := fun q {_ _} e => G.app_eqv q e trivial trivial
      nat := fun {_ _} g z => (X.obj _).trans (G.app_eqv _ (((Pr.{w} n).obj _).refl _) _ _) (G.nat g z trivial) },
    fun q z h0 => hG q z h0 trivial⟩

end IC

end Found

#print axioms Found.IC.le_mxv
#print axioms Found.IC.mxv_le
#print axioms Found.IC.cover
#print axioms Found.IC.allS
#print axioms Found.IC.allT
#print axioms Found.IC.prism_ext

found/IC27.lean

lean_all/found/IC27.lean

/-
  T153 ((infinity,1)-categories), part 27. found/IC27 — extension along
  Lambda^2_1 x B ∪ Delta^2 x A ⊂ Delta^2 x B for a subcomplex A of B, I:
  the simplicial set EL over B whose n-simplices over β are maps
  Delta^2 x Delta^n -> X agreeing with the given F on the pullback of
  Lambda^2_1 x B ∪ Delta^2 x A along id x β; a section of EL -> B gives the
  extension (ext_of_sec).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.IC26

namespace Found

namespace IC

open Simp

universe w

/-- Lambda^2_1 as a condition on a map to [2] -/
def Lam {q : Nat} (u : DHom q 2) : Prop := (∀ r, r ≤ q → u.f r ≠ 0) ∨ (∀ r, r ≤ q → u.f r ≠ 2)

theorem lam_map {q' q : Nat} (g : DHom q' q) {u : DHom q 2} (h : Lam u) : Lam (dComp g u) :=
  h.elim (fun h => .inl fun r hr => h _ (g.bound r hr)) (fun h => .inr fun r hr => h _ (g.bound r hr))

theorem lam_eqv {q : Nat} {u u' : DHom q 2} (e : ∀ r, r ≤ q → u.f r = u'.f r) (h : Lam u) : Lam u' :=
  h.elim (fun h => .inl fun r hr e' => h r hr ((e r hr).trans e'))
    (fun h => .inr fun r hr e' => h r hr ((e r hr).trans e'))

variable {B : SSet.{w}}

/-- Lambda^2_1 x B ∪ Delta^2 x A -/
def LA (A : Sub B) : Sub (SProd (Simp.{w} 2) B) where
  P q z := Lam z.1.down ∨ A.P q z.2
  P_eqv q {_ _} e h := h.elim (fun h => .inl (lam_eqv e.1 h)) (fun h => .inr (A.P_eqv q e.2 h))
  P_map g _ h := h.elim (fun h => .inl (lam_map g h)) (fun h => .inr (A.P_map g _ h))

/-- the point (u, v^* β) of Delta^2 x B -/
def pt {n q : Nat} (z : ((Pr.{w} n).obj (q : Nat)).car) (β : (B.obj (n : Nat)).car) :
    ((SProd (Simp.{w} 2) B).obj (q : Nat)).car := (z.1, mp B (prV z) β)

/-- id x g : Delta^2 x Delta^n' -> Delta^2 x Delta^n -/
def sMap {n' n : Nat} (g : DHom n' n) : PNat (Pr.{w} n') (Pr.{w} n) where
  app _ z := (z.1, ⟨dComp z.2.down g⟩)
  app_eqv _ {_ _} h := ⟨h.1, fun i hi => congrArg g.f (h.2 i hi)⟩
  nat _ _ := ⟨fun _ _ => rfl, fun _ _ => rfl⟩

variable {X : SSet.{w}} (A : Sub B) (F : PMap (LA A) X)

/-- an n-simplex of EL: a simplex β of B and a map Delta^2 x Delta^n -> X agreeing with F over β -/
structure EL (n : Nat) : Type w where
  β : (B.obj (n : Nat)).car
  φ : PNat (Pr.{w} n) X
  agree : ∀ q (z : ((Pr.{w} n).obj (q : Nat)).car) (h : (LA A).P q (pt z β)),
    (X.obj (q : Nat)).eqv (φ.app q z) (F.app q (pt z β) h)

variable {A F}

theorem pt_comp {n' n q : Nat} (g : DHom n' n) (z : ((Pr.{w} n').obj (q : Nat)).car) (β : (B.obj (n : Nat)).car) :
    ((SProd (Simp.{w} 2) B).obj (q : Nat)).eqv (pt z (mp B g β)) (pt ((sMap g).app q z) β) :=
  ⟨fun _ _ => rfl, mp_comp B _ g β⟩

def EL.act {n' n : Nat} (g : DHom n' n) (e : EL A F n) : EL A F n' where
  β := mp B g e.β
  φ := pcomp (sMap g) e.φ
  agree q z h :=
    have h' := (LA A).P_eqv q (pt_comp g z e.β) h
    (X.obj _).trans (e.agree q _ h') (F.app_eqv q (((SProd (Simp.{w} 2) B).obj _).symm (pt_comp g z e.β)) h' h)

variable (A F)

/-- the simplicial set EL -/
def ELS : SSet.{w} where
  obj := fun (n : Nat) => ⟨EL A F n, fun e e' => (B.obj n).eqv e.β e'.β ∧
      ∀ q (z : ((Pr.{w} n).obj (q : Nat)).car), (X.obj (q : Nat)).eqv (e.φ.app q z) (e'.φ.app q z),
    fun _ => ⟨(B.obj n).refl _, fun _ _ => (X.obj _).refl _⟩,
    fun h => ⟨(B.obj n).symm h.1, fun q z => (X.obj _).symm (h.2 q z)⟩,
    fun h h' => ⟨(B.obj n).trans h.1 h'.1, fun q z => (X.obj _).trans (h.2 q z) (h'.2 q z)⟩⟩
  map g e := EL.act g e
  map_eqv {_ _} _ {_ _} h := ⟨B.map_eqv _ h.1, fun q _ => h.2 q _⟩
  map_resp {_ _ _ _} e hfg := ⟨B.map_resp e.β hfg, fun q z => e.φ.app_eqv q
    ⟨fun _ _ => rfl, fun i hi => hfg _ (z.2.down.bound i hi)⟩⟩
  map_id _ e := ⟨B.map_id _ e.β, fun q _ => e.φ.app_eqv q ⟨fun _ _ => rfl, fun _ _ => rfl⟩⟩
  map_comp _ _ e := ⟨B.map_comp _ _ e.β, fun q _ => e.φ.app_eqv q ⟨fun _ _ => rfl, fun _ _ => rfl⟩⟩

/-- the projection EL -> B -/
def piL : PNat (ELS A F) B where
  app _ e := e.β
  app_eqv _ {_ _} h := h.1
  nat _ _ := (B.obj _).refl _

/-- the identity simplex of Delta^q, paired with u -/
def idz {q : Nat} (u : ((Simp.{w} 2).obj (q : Nat)).car) : ((Pr.{w} q).obj (q : Nat)).car := (u, ⟨dId q⟩)

/-- a section of EL -> B gives an extension of F to Delta^2 x B -/
theorem ext_of_sec (s : PNat B (ELS A F))
    (hs : ∀ (n : Nat) (β : (B.obj (n : Nat)).car), (B.obj (n : Nat)).eqv ((s.app (n : Nat) β).β) β) :
    ∃ Φ : PNat (SProd (Simp.{w} 2) B) X, ∀ q (z : ((SProd (Simp.{w} 2) B).obj (q : Nat)).car)
      (h : (LA A).P q z), (X.obj (q : Nat)).eqv (Φ.app q z) (F.app q z h) :=
  ⟨{ app := fun q z => (s.app q z.2).φ.app q (idz z.1)
     app_eqv := fun q {_ z'} e => (X.obj _).trans ((s.app_eqv q e.2).2 q _)
       ((s.app q z'.2).φ.app_eqv q ⟨e.1, fun _ _ => rfl⟩)
     nat := fun {_ _} g z => (X.obj _).trans ((s.nat g z.2).2 _ _)
       ((X.obj _).trans ((s.app _ z.2).φ.app_eqv _ ⟨fun _ _ => rfl, fun _ _ => rfl⟩)
         ((s.app _ z.2).φ.nat g (idz z.1))) },
   fun q z h =>
    have e1 : ((SProd (Simp.{w} 2) B).obj (q : Nat)).eqv z (pt (idz z.1) ((s.app q z.2).β)) :=
      ⟨fun _ _ => rfl, (B.obj _).symm ((B.obj _).trans (mp_idl B (dId q) (fun _ _ => rfl) _) (hs q z.2))⟩
    (X.obj _).trans ((s.app q z.2).agree q (idz z.1) ((LA A).P_eqv q e1 h))
      (F.app_eqv q ((SProd (Simp.{w} 2) B).obj _ |>.symm e1) _ h)⟩

end IC

end Found

#print axioms Found.IC.LA
#print axioms Found.IC.ELS
#print axioms Found.IC.piL
#print axioms Found.IC.ext_of_sec

found/IC28.lean

lean_all/found/IC28.lean

/-
  T153 ((infinity,1)-categories), part 28. found/IC28 — extension along
  Lambda^2_1 x B ∪ Delta^2 x A ⊂ Delta^2 x B, II: boundary lifting for
  EL -> B.  Given τ in B_(M+1) and simplices x_j of EL over the faces of τ
  that agree where two faces meet (GC), the canonical values (x_j on
  Delta^2 x (j-th face), F on Lambda^2_1 x Delta^(M+1)) form a partial map
  on A0 = Lambda^2_1 x Delta^(M+1) ∪ Delta^2 x ∂Delta^(M+1); the prism
  extension (IC26) fills it, and the result agrees with F over τ because a
  surjection has a section (when τ is not in A); when τ is in A the filler
  is F itself.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.IC27

namespace Found

namespace IC

open Simp

universe w

theorem sk_inj {i a b : Nat} (h : sk i a = sk i b) : a = b :=
  match Nat.lt_or_ge a i, Nat.lt_or_ge b i with
  | .inl ha, .inl hb => by rw [sk_lt ha, sk_lt hb] at h; exact h
  | .inl ha, .inr hb => by
    rw [sk_lt ha, sk_ge hb] at h
    have hba : b < a := Nat.lt_of_lt_of_le (Nat.lt_succ_self b) (Nat.le_of_eq h.symm)
    exact absurd (Nat.lt_trans (Nat.lt_of_lt_of_le ha hb) hba) (Nat.lt_irrefl a)
  | .inr ha, .inl hb => by
    rw [sk_ge ha, sk_lt hb] at h
    have hab : a < b := Nat.lt_of_lt_of_le (Nat.lt_succ_self a) (Nat.le_of_eq h)
    exact absurd (Nat.lt_trans (Nat.lt_of_lt_of_le hb ha) hab) (Nat.lt_irrefl b)
  | .inr ha, .inr hb => by rw [sk_ge ha, sk_ge hb] at h; exact congrArg Nat.pred h

/-- a map that misses j factors through the j-th face -/
def unsk {q M : Nat} (v : DHom q (M + 1)) (j : Nat) (hj : j ≤ M + 1) : DHom q M :=
  dComp v (degen M (Nat.pred j) (Nat.pred_le_pred hj))

theorem unsk_spec {q M : Nat} (v : DHom q (M + 1)) {j : Nat} (hj : j ≤ M + 1) (hv : ∀ r, r ≤ q → v.f r ≠ j) :
    ∀ r, r ≤ q → v.f r = sk j ((unsk v j hj).f r) := fun r hr => (sk_dg (hv r hr)).symm

variable (Kw : ClW.{w}) (Kc : CP.Classical') {B X : SSet.{w}} {A : Sub B} {F : PMap (LA A) X} (hQ : Quasi X)

section glue

variable {M : Nat} (τ : (B.obj (M + 1 : Nat)).car) (xs : Nat → EL A F M)
  (hx : ∀ j, j ≤ M + 1 → (B.obj (M : Nat)).eqv (xs j).β (dd B j τ))
  (GC : ∀ q (u : DHom q 2) (w w' : DHom q M) (j j' : Nat), j < j' → j' ≤ M + 1 →
    (∀ r, r ≤ q → sk j (w.f r) = sk j' (w'.f r)) →
    (X.obj (q : Nat)).eqv ((xs j).φ.app q (⟨u⟩, ⟨w⟩)) ((xs j').φ.app q (⟨u⟩, ⟨w'⟩)))

/-- canonical values on A0: from a face, or from F on Lambda^2_1 -/
def Can {q : Nat} (z : ((Pr.{w} (M + 1)).obj (q : Nat)).car) (x : (X.obj (q : Nat)).car) : Prop :=
  (∃ (j : Nat) (w : DHom q M), j ≤ M + 1 ∧ (∀ r, r ≤ q → (prV z).f r = sk j (w.f r)) ∧
      (X.obj (q : Nat)).eqv x ((xs j).φ.app q (z.1, ⟨w⟩))) ∨
    (∃ hl : Lam z.1.down, (X.obj (q : Nat)).eqv x (F.app q (pt z τ) (.inl hl)))

include hx in
theorem pt_face {q j : Nat} (hj : j ≤ M + 1) (z : ((Pr.{w} (M + 1)).obj (q : Nat)).car) (w : DHom q M)
    (hw : ∀ r, r ≤ q → (prV z).f r = sk j (w.f r)) :
    ((SProd (Simp.{w} 2) B).obj (q : Nat)).eqv (pt (B := B) ((z.1, ⟨w⟩) : ((Pr.{w} M).obj (q : Nat)).car) (xs j).β)
      (pt z τ) :=
  ⟨fun _ _ => rfl, (B.obj _).trans (mp_eqv B w (hx j hj))
    ((B.obj _).trans (mp_comp B w (face M j) τ) (mp_resp B (fun r hr => (hw r hr).symm) τ))⟩

include hx in
theorem can_F {q : Nat} {z : ((Pr.{w} (M + 1)).obj (q : Nat)).car} {x : (X.obj (q : Nat)).car}
    (hc : Can τ xs z x) (h : (LA A).P q (pt z τ)) : (X.obj (q : Nat)).eqv x (F.app q (pt z τ) h) :=
  match hc with
  | .inl ⟨j, w, hj, hw, hxv⟩ =>
    have e := pt_face τ xs hx hj z w hw
    (X.obj _).trans hxv ((X.obj _).trans ((xs j).agree q _ ((LA A).P_eqv q ((SProd (Simp.{w} 2) B).obj _ |>.symm e) h))
      (F.app_eqv q e _ h))
  | .inr ⟨_, hxv⟩ => (X.obj _).trans hxv (F.app_eqv q (((SProd (Simp.{w} 2) B).obj _).refl _) _ h)

include hx GC in
theorem can_unique {q : Nat} {z : ((Pr.{w} (M + 1)).obj (q : Nat)).car} {x y : (X.obj (q : Nat)).car}
    (hcx : Can τ xs z x) (hcy : Can τ xs z y) : (X.obj (q : Nat)).eqv x y :=
  match hcx, hcy with
  | .inl ⟨j, w, hj, hw, hxv⟩, .inl ⟨j', w', hj', hw', hyv⟩ =>
    have hww : ∀ r, r ≤ q → sk j (w.f r) = sk j' (w'.f r) := fun r hr => (hw r hr).symm.trans (hw' r hr)
    (X.obj _).trans hxv ((X.obj _).trans (match Nat.lt_or_ge j j' with
      | .inl h => GC q z.1.down w w' j j' h hj' hww
      | .inr h => match Nat.lt_or_ge j' j with
        | .inl h' => (X.obj _).symm (GC q z.1.down w' w j' j h' hj (fun r hr => (hww r hr).symm))
        | .inr h' => by
          have e : j = j' := Nat.le_antisymm h' h
          subst e
          exact (xs j).φ.app_eqv q ⟨fun _ _ => rfl, fun r hr => sk_inj (hww r hr)⟩) ((X.obj _).symm hyv))
  | .inl ⟨j, w, hj, hw, hxv⟩, .inr ⟨hl, hyv⟩ =>
    (X.obj _).trans (can_F τ xs hx (.inl ⟨j, w, hj, hw, hxv⟩) (.inl hl)) ((X.obj _).symm hyv)
  | .inr ⟨hl, hxv⟩, .inl ⟨j, w, hj, hw, hyv⟩ =>
    (X.obj _).trans hxv ((X.obj _).symm (can_F τ xs hx (.inl ⟨j, w, hj, hw, hyv⟩) (.inl hl)))
  | .inr ⟨_, hxv⟩, .inr ⟨_, hyv⟩ => (X.obj _).trans hxv ((X.obj _).symm hyv)

theorem can_eqv {q : Nat} {z z' : ((Pr.{w} (M + 1)).obj (q : Nat)).car} {x : (X.obj (q : Nat)).car}
    (e : ((Pr.{w} (M + 1)).obj (q : Nat)).eqv z z') (hc : Can τ xs z x) : Can τ xs z' x :=
  match hc with
  | .inl ⟨j, w, hj, hw, hxv⟩ => .inl ⟨j, w, hj, fun r hr => (e.2 r hr).symm.trans (hw r hr),
      (X.obj _).trans hxv ((xs j).φ.app_eqv q ⟨e.1, fun _ _ => rfl⟩)⟩
  | .inr ⟨hl, hxv⟩ => .inr ⟨lam_eqv e.1 hl, (X.obj _).trans hxv
      (F.app_eqv q ⟨e.1, mp_resp B e.2 τ⟩ _ _)⟩

theorem can_map {q' q : Nat} (g : DHom q' q) {z : ((Pr.{w} (M + 1)).obj (q : Nat)).car} {x : (X.obj (q : Nat)).car}
    (hc : Can τ xs z x) : Can τ xs (mp (Pr.{w} (M + 1)) g z) (mp X g x) :=
  match hc with
  | .inl ⟨j, w, hj, hw, hxv⟩ => .inl ⟨j, dComp g w, hj, fun r hr => hw (g.f r) (g.bound r hr),
      (X.obj _).trans (mp_eqv X g hxv) ((X.obj _).symm ((xs j).φ.nat g (z.1, ⟨w⟩)))⟩
  | .inr ⟨hl, hxv⟩ => .inr ⟨lam_map g hl, (X.obj _).trans (mp_eqv X g hxv)
      ((X.obj _).trans ((X.obj _).symm (F.nat g _ _)) (F.app_eqv q' (z := mp (SProd (Simp.{w} 2) B) g (pt z τ)) (z' := pt (mp (Pr.{w} (M + 1)) g z) τ)
        ⟨fun _ _ => rfl, mp_comp B g (prV z) τ⟩ _ _))⟩

theorem can_ex {q : Nat} (z : ((Pr.{w} (M + 1)).obj (q : Nat)).car) (h : (A0.{w} (n := M + 1)).P q z) :
    ∃ x, Can τ xs z x :=
  match h with
  | .inl (.inl h0) => ⟨_, .inr ⟨.inl h0, (X.obj _).refl _⟩⟩
  | .inl (.inr h2) => ⟨_, .inr ⟨.inr h2, (X.obj _).refl _⟩⟩
  | .inr ⟨y, hy, hv⟩ => ⟨_, .inl ⟨y, unsk (prV z) y hy, hy, unsk_spec (prV z) hy hv, (X.obj _).refl _⟩⟩

/-- the partial map on A0 given by the canonical values -/
def canMap : PMap (A0.{w} (n := M + 1)) X where
  app _ z h := Kw.ch (Can τ xs z) (can_ex τ xs z h)
  app_eqv _ {z z'} e h h' := can_unique τ xs hx GC (can_eqv τ xs e (Kw.ch_spec (Can τ xs z) (can_ex τ xs z h)))
    (Kw.ch_spec (Can τ xs z') (can_ex τ xs z' h'))
  nat {_ _} g z h := can_unique τ xs hx GC
    (Kw.ch_spec (Can τ xs (mp (Pr.{w} (M + 1)) g z)) (can_ex τ xs _ ((A0.{w} (n := M + 1)).P_map g z h)))
    (can_map τ xs g (Kw.ch_spec (Can τ xs z) (can_ex τ xs z h)))

theorem canMap_spec {q : Nat} (z : ((Pr.{w} (M + 1)).obj (q : Nat)).car) (h : (A0.{w} (n := M + 1)).P q z) :
    Can τ xs z ((canMap Kw τ xs hx GC).app q z h) :=
  Kw.ch_spec _ _

include Kw Kc in
/-- a simplex of Delta^(M+1) whose pullback of τ lies in A misses a vertex, unless τ lies in A -/
theorem miss_or_A {q : Nat} (v : DHom q (M + 1)) (hA : A.P q (mp B v τ)) (nA : ¬ A.P (M + 1) τ) :
    ∃ y, y ≤ M + 1 ∧ ∀ r, r ≤ q → v.f r ≠ y :=
  match Kw.lem (∃ y, y ≤ M + 1 ∧ ∀ r, r ≤ q → v.f r ≠ y) with
  | .inl h => h
  | .inr hn =>
    have hs : Surj v := fun j hj => match Kw.lem (∃ a, a ≤ q ∧ v.f a = j) with
      | .inl h => h
      | .inr h => absurd ⟨j, hj, fun r hr e => h ⟨r, hr, e⟩⟩ hn
    absurd (A.P_eqv (M + 1) (sec_pull Kc v hs (Nat.zero_le q) τ) (A.P_map _ _ hA)) nA

include Kw Kc hQ hx GC in
/-- boundary lifting for EL -> B -/
theorem liftEL : ∃ e : EL A F (M + 1), (B.obj (M + 1 : Nat)).eqv e.β τ ∧
    ∀ j, j ≤ M + 1 → ((ELS A F).obj (M : Nat)).eqv (dd (ELS A F) j e) (xs j) :=
  match Kw.lem (A.P (M + 1) τ) with
  | .inl hA => ⟨⟨τ, { app := fun q z => F.app q (pt z τ) (.inr (A.P_map (prV z) τ hA))
                      app_eqv := fun q {_ _} e => F.app_eqv q ⟨e.1, mp_resp B e.2 τ⟩ _ _
                      nat := fun {q' _} g z => (X.obj _).trans
                        (F.app_eqv q' (z := pt (mp (Pr.{w} (M + 1)) g z) τ) (z' := mp (SProd (Simp.{w} 2) B) g (pt z τ))
                          ⟨fun _ _ => rfl, (B.obj _).symm (mp_comp B g (prV z) τ)⟩ _ _)
                        (F.nat g (pt z τ) (.inr (A.P_map (prV z) τ hA))) },
      fun q _ h => F.app_eqv q (((SProd (Simp.{w} 2) B).obj _).refl _) _ h⟩,
    (B.obj _).refl _,
    fun j hj => ⟨(B.obj _).symm (hx j hj), fun q z =>
      have e := pt_face τ xs hx (z := (sMap (face M j)).app q z) hj z.2.down (fun _ _ => rfl)
      (X.obj _).trans (F.app_eqv q ((SProd (Simp.{w} 2) B).obj _ |>.symm e) _
          ((LA A).P_eqv q ((SProd (Simp.{w} 2) B).obj _ |>.symm e) (.inr (A.P_map _ τ hA))))
        ((X.obj _).symm ((xs j).agree q z _))⟩⟩
  | .inr nA => match prism_ext Kw hQ (canMap Kw τ xs hx GC) with
    | ⟨φ, hφ⟩ => ⟨⟨τ, φ, fun q z h =>
        have h0 : (A0.{w} (n := M + 1)).P q z := match Kw.lem (∃ y, y ≤ M + 1 ∧ ∀ r, r ≤ q → (prV z).f r ≠ y) with
          | .inl hy => .inr hy
          | .inr hn => match h with
            | .inl (.inl hl) => .inl (.inl hl)
            | .inl (.inr hl) => .inl (.inr hl)
            | .inr hAv => absurd (miss_or_A Kw Kc τ (prV z) hAv nA) hn
        (X.obj _).trans (hφ q z h0) (can_F τ xs hx (canMap_spec Kw τ xs hx GC z h0) h)⟩,
      (B.obj _).refl _,
      fun j hj => ⟨(B.obj _).symm (hx j hj), fun q z =>
        have h0 : (A0.{w} (n := M + 1)).P q ((sMap (face M j)).app q z) :=
          .inr ⟨j, hj, fun r _ => sk_ne j (z.2.down.f r)⟩
        (X.obj _).trans (hφ q _ h0) (can_unique τ xs hx GC (canMap_spec Kw τ xs hx GC _ h0)
          (.inl ⟨j, z.2.down, hj, fun _ _ => rfl, (X.obj _).refl _⟩))⟩⟩

end glue

end IC

end Found

#print axioms Found.IC.sk_inj
#print axioms Found.IC.can_unique
#print axioms Found.IC.canMap
#print axioms Found.IC.miss_or_A
#print axioms Found.IC.liftEL

found/IC29.lean

lean_all/found/IC29.lean

/-
  T153 ((infinity,1)-categories), part 29. found/IC29 — extension along
  Lambda^2_1 x B ∪ Delta^2 x A ⊂ Delta^2 x B, III: vertex lifts, the
  agreement of boundary faces (GC) from the boundary compatibilities, and
  the section of EL -> B (IC15).  Result (lam_ext): for every subcomplex A
  of a simplicial set B and every quasi-category X, each map
  Lambda^2_1 x B ∪ Delta^2 x A -> X extends to Delta^2 x B -> X
  (inside the classical point).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.IC28

namespace Found

namespace IC

open Simp

universe w

variable (Kw : ClW.{w}) (Kc : CP.Classical') {B X : SSet.{w}} {A : Sub B} {F : PMap (LA A) X} (hQ : Quasi X)

/-- over a simplex of A the extension is F itself -/
def elA {n : Nat} (β : (B.obj (n : Nat)).car) (hA : A.P n β) : EL A F n :=
  ⟨β, { app := fun q z => F.app q (pt z β) (.inr (A.P_map (prV z) β hA))
        app_eqv := fun q {_ _} e => F.app_eqv q ⟨e.1, mp_resp B e.2 β⟩ _ _
        nat := fun {q' _} g z => (X.obj _).trans
          (F.app_eqv q' (z := pt (mp (Pr.{w} n) g z) β) (z' := mp (SProd (Simp.{w} 2) B) g (pt z β))
            ⟨fun _ _ => rfl, (B.obj _).symm (mp_comp B g (prV z) β)⟩ _ _)
          (F.nat g (pt z β) (.inr (A.P_map (prV z) β hA))) },
    fun q _ h => F.app_eqv q (((SProd (Simp.{w} 2) B).obj _).refl _) _ h⟩

section vertex

variable (β : (B.obj (0 : Nat)).car)

def Can0 {q : Nat} (z : ((Pr.{w} 0).obj (q : Nat)).car) (x : (X.obj (q : Nat)).car) : Prop :=
  ∃ hl : Lam z.1.down, (X.obj (q : Nat)).eqv x (F.app q (pt z β) (.inl hl))

theorem lam_of_A0 {q : Nat} {z : ((Pr.{w} 0).obj (q : Nat)).car} (h : (A0.{w} (n := 0)).P q z) : Lam z.1.down :=
  match h with
  | .inl (.inl h0) => .inl h0
  | .inl (.inr h2) => .inr h2
  | .inr ⟨_, hy, hv⟩ =>
    have e0 : (prV z).f 0 = 0 := Nat.eq_zero_of_le_zero ((prV z).bound 0 (Nat.zero_le q))
    absurd (e0.trans (Nat.eq_zero_of_le_zero hy).symm) (hv 0 (Nat.zero_le q))

def canMap0 : PMap (A0.{w} (n := 0)) X where
  app q z h := F.app q (pt z β) (.inl (lam_of_A0 h))
  app_eqv q {_ _} e _ _ := F.app_eqv q ⟨e.1, mp_resp B e.2 β⟩ _ _
  nat {q' _} g z _ := (X.obj _).trans
    (F.app_eqv q' (z := pt (mp (Pr.{w} 0) g z) β) (z' := mp (SProd (Simp.{w} 2) B) g (pt z β))
      ⟨fun _ _ => rfl, (B.obj _).symm (mp_comp B g (prV z) β)⟩ _ _) (F.nat g _ _)

/-- the vertex [0] -> [q] -/
def v0 (q : Nat) : DHom 0 q := ⟨fun _ => 0, fun _ h => absurd h (Nat.not_lt_zero _), fun _ _ => Nat.zero_le q⟩

include Kw hQ in
theorem liftEL0 : ∃ e : EL A F 0, (B.obj (0 : Nat)).eqv e.β β :=
  match Kw.lem (A.P 0 β) with
  | .inl hA => ⟨elA β hA, (B.obj _).refl _⟩
  | .inr nA => match prism_ext Kw hQ (canMap0 β (F := F)) with
    | ⟨φ, hφ⟩ => ⟨⟨β, φ, fun q z h => match h with
        | .inl hl => (X.obj _).trans (hφ q z (.inl hl))
            (F.app_eqv q (((SProd (Simp.{w} 2) B).obj _).refl _) _ _)
        | .inr hAv =>
          have e : (B.obj (0 : Nat)).eqv (mp B (v0 q) (mp B (prV z) β)) β :=
            (B.obj _).trans (mp_comp B _ _ β) (mp_idl B _ (fun _ hi =>
              (Nat.eq_zero_of_le_zero ((prV z).bound 0 (Nat.zero_le q))).trans (Nat.eq_zero_of_le_zero hi).symm) β)
          absurd (A.P_eqv 0 e (A.P_map (v0 q) _ hAv)) nA⟩, (B.obj _).refl _⟩

end vertex

/-- the faces x_j of an edge of EL agree trivially -/
theorem gc1 (xs : Nat → EL A F 0) : ∀ q (u : DHom q 2) (w w' : DHom q 0) (j j' : Nat), j < j' → j' ≤ 0 + 1 →
    (∀ r, r ≤ q → sk j (w.f r) = sk j' (w'.f r)) →
    (X.obj (q : Nat)).eqv ((xs j).φ.app q (⟨u⟩, ⟨w⟩)) ((xs j').φ.app q (⟨u⟩, ⟨w'⟩))
  | q, _, w, w', j, j', hjj, hj', hw => by
    have e := hw 0 (Nat.zero_le q)
    rw [Nat.eq_zero_of_le_zero (w.bound 0 (Nat.zero_le q)), Nat.eq_zero_of_le_zero (w'.bound 0 (Nat.zero_le q))] at e
    have hj0 : j = 0 := Nat.eq_zero_of_le_zero (Nat.le_of_lt_succ (Nat.lt_of_lt_of_le hjj hj'))
    have hj1 : j' = 1 := Nat.le_antisymm hj' (Nat.lt_of_le_of_lt (Nat.zero_le j) hjj)
    subst hj0; subst hj1
    rw [sk_ge (Nat.le_refl 0), sk_lt (Nat.succ_pos 0)] at e
    exact Nat.noConfusion e

/-- the faces of a boundary of EL agree where they meet -/
theorem gcB {n : Nat} (b : Bdry (ELS A F) n) : ∀ q (u : DHom q 2) (w w' : DHom q (n + 1)) (j j' : Nat), j < j' →
    j' ≤ n + 1 + 1 → (∀ r, r ≤ q → sk j (w.f r) = sk j' (w'.f r)) →
    (X.obj (q : Nat)).eqv ((b.x j).φ.app q (⟨u⟩, ⟨w⟩)) ((b.x j').φ.app q (⟨u⟩, ⟨w'⟩))
  | _, _, _, _, _, 0, hjj, _, _ => absurd hjj (Nat.not_lt_zero _)
  | q, u, w, w', j, J + 1, hjj, hj', hw =>
    have hjJ : j ≤ J := Nat.le_of_lt_succ hjj
    have hJ : J ≤ n + 1 := Nat.le_of_succ_le_succ hj'
    have hj : j ≤ n + 1 := Nat.le_trans hjJ hJ
    have hmiss : ∀ r, r ≤ q → w'.f r ≠ j := fun r hr e => sk_ne j (w.f r) (by
      rw [hw r hr, e, sk_lt (Nat.lt_succ_of_le hjJ)])
    have ht := unsk_spec w' hj hmiss
    have hwt : ∀ r, r ≤ q → w.f r = sk J ((unsk w' j hj).f r) := fun r hr =>
      sk_inj ((hw r hr).trans (by rw [ht r hr]; exact sk_sk hjJ _))
    let t := unsk w' j hj
    have m1 : (X.obj (q : Nat)).eqv ((b.x j).φ.app q (⟨u⟩, ⟨w⟩)) ((b.x j).φ.app q (⟨u⟩, ⟨dComp t (face n J)⟩)) :=
      (b.x j).φ.app_eqv q (x := (⟨u⟩, ⟨w⟩)) (y := (⟨u⟩, ⟨dComp t (face n J)⟩)) ⟨fun _ _ => rfl, hwt⟩
    have m2 : (X.obj (q : Nat)).eqv ((b.x j).φ.app q (⟨u⟩, ⟨dComp t (face n J)⟩))
        ((b.x (J + 1)).φ.app q (⟨u⟩, ⟨dComp t (face n j)⟩)) :=
      (X.obj _).symm ((b.compat j J hjJ hJ).2 q (⟨u⟩, ⟨t⟩))
    have m3 : (X.obj (q : Nat)).eqv ((b.x (J + 1)).φ.app q (⟨u⟩, ⟨dComp t (face n j)⟩))
        ((b.x (J + 1)).φ.app q (⟨u⟩, ⟨w'⟩)) :=
      (b.x (J + 1)).φ.app_eqv q (x := (⟨u⟩, ⟨dComp t (face n j)⟩)) (y := (⟨u⟩, ⟨w'⟩))
        ⟨fun _ _ => rfl, fun r hr => (ht r hr).symm⟩
    (X.obj _).trans m1 ((X.obj _).trans m2 m3)

include Kw Kc hQ in
/-- Lambda^2_1 x B ∪ Delta^2 x A ⊂ Delta^2 x B: quasi-categories extend along it -/
theorem lam_ext (A : Sub B) (F : PMap (LA A) X) :
    ∃ Φ : PNat (SProd (Simp.{w} 2) B) X, ∀ q (z : ((SProd (Simp.{w} 2) B).obj (q : Nat)).car)
      (h : (LA A).P q z), (X.obj (q : Nat)).eqv (Φ.app q z) (F.app q z h) :=
  have L0 : ∀ β : (B.obj (0 : Nat)).car, ∃ e : ((ELS A F).obj (0 : Nat)).car,
      (B.obj (0 : Nat)).eqv ((piL A F).app (0 : Nat) e) β := fun β => liftEL0 Kw hQ β
  have L1 : ∀ (τ : (B.obj (1 : Nat)).car) (b0 b1 : ((ELS A F).obj (0 : Nat)).car),
      (B.obj (0 : Nat)).eqv ((piL A F).app (0 : Nat) b0) (dd B 0 τ) →
      (B.obj (0 : Nat)).eqv ((piL A F).app (0 : Nat) b1) (dd B 1 τ) →
      ∃ e : ((ELS A F).obj (1 : Nat)).car, (B.obj (1 : Nat)).eqv ((piL A F).app (1 : Nat) e) τ ∧
        ((ELS A F).obj (0 : Nat)).eqv (dd (ELS A F) 0 e) b0 ∧ ((ELS A F).obj (0 : Nat)).eqv (dd (ELS A F) 1 e) b1 :=
    fun τ b0 b1 h0 h1 =>
      match liftEL Kw Kc hQ (M := 0) τ (fun j => match j with | 0 => b0 | _ + 1 => b1)
          (fun j hj => match j, hj with
            | 0, _ => h0
            | 1, _ => h1
            | _ + 2, h => absurd (Nat.le_of_succ_le_succ h) (Nat.not_succ_le_zero _))
          (gc1 _) with
      | ⟨e, he, hf⟩ => ⟨e, he, hf 0 (Nat.zero_le _), hf 1 (Nat.le_refl _)⟩
  have L2 : ∀ (n : Nat) (τ : (B.obj (n + 2 : Nat)).car) (b : Bdry (ELS A F) n),
      (∀ j, j ≤ n + 2 → (B.obj (n + 1 : Nat)).eqv ((piL A F).app (n + 1 : Nat) (b.x j)) (dd B j τ)) →
      ∃ e : ((ELS A F).obj (n + 2 : Nat)).car, (B.obj (n + 2 : Nat)).eqv ((piL A F).app (n + 2 : Nat) e) τ ∧
        BFills b e :=
    fun n τ b hb => match liftEL Kw Kc hQ (M := n + 1) τ b.x hb (gcB b) with
      | ⟨e, he, hf⟩ => ⟨e, he, hf⟩
  ext_of_sec A F (secN Kw Kc (piL A F) L0 L1 L2) (fun n β => secN_proj Kw Kc (piL A F) L0 L1 L2 n β)

end IC

end Found

#print axioms Found.IC.liftEL0
#print axioms Found.IC.gc1
#print axioms Found.IC.gcB
#print axioms Found.IC.lam_ext

found/IC3.lean

lean_all/found/IC3.lean

/-
  T153 ((infinity,1)-categories), part 3. found/IC3 — equivalences and
  infinity-groupoids: an edge of a quasi-category is an equivalence when it
  is invertible in the homotopy category; in any setoid category a map with a
  left and a right inverse is invertible; in a Kan complex the outer horns
  Lambda^2_0 and Lambda^2_2 give left and right inverses, so the homotopy
  category of a Kan complex is a groupoid (Kan complexes are
  infinity-groupoids).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.IC2

namespace Found

namespace IC

open Simp

universe w

/-- a map with a left and a right inverse: the left inverse is two-sided -/
theorem ECat.inv_of_lr {C : ECat.{u, v}} {a b : C.Ob} (f : C.Hom a b) (g h : C.Hom b a)
    (hg : C.heq (C.comp f g) (C.id a)) (hh : C.heq (C.comp h f) (C.id b)) :
    C.heq g h ∧ C.heq (C.comp g f) (C.id b) :=
  have e : C.heq g h :=
    C.htrans (C.hsymm (C.id_comp g)) (C.htrans (C.comp_resp (C.hsymm hh) (C.hrefl g))
      (C.htrans (C.assoc h f g) (C.htrans (C.comp_resp (C.hrefl h) hg) (C.comp_id h))))
  ⟨e, C.htrans (C.comp_resp e (C.hrefl f)) hh⟩

/-- isomorphisms of a setoid category -/
def ECat.IsIso {C : ECat.{u, v}} {a b : C.Ob} (f : C.Hom a b) : Prop :=
  ∃ g : C.Hom b a, C.heq (C.comp f g) (C.id a) ∧ C.heq (C.comp g f) (C.id b)

/-- groupoids: every map is an isomorphism -/
def ECat.Groupoid (C : ECat.{u, v}) : Prop := ∀ {a b : C.Ob} (f : C.Hom a b), ECat.IsIso f

variable {X : SSet.{w}}

/-- the outer horn Lambda^2_0 with d1 = a, d2 = b -/
def h20 (a b : (E X).car) (e : (V X).eqv (dd X 1 b) (dd X 1 a)) : Horn X 0 0 where
  x i := match i with
    | 0 => a
    | 1 => a
    | _ + 2 => b
  compat i j hij hj hi hj1 := match i, j, hij, hj, hi, hj1 with
    | 0, _, _, _, h, _ => absurd rfl h
    | 1, 0, h, _, _, _ => absurd h (Nat.not_succ_le_zero 0)
    | 1, 1, _, _, _, _ => e
    | 1, _ + 2, _, h, _, _ => absurd (Nat.le_of_succ_le_succ h) (Nat.not_succ_le_zero _)
    | _ + 2, _, h, hj, _, _ => absurd (Nat.le_of_succ_le_succ (Nat.le_trans h hj)) (Nat.not_succ_le_zero _)

/-- the outer horn Lambda^2_2 with d0 = a, d1 = b -/
def h22 (a b : (E X).car) (e : (V X).eqv (dd X 0 b) (dd X 0 a)) : Horn X 0 2 where
  x i := match i with
    | 0 => a
    | 1 => b
    | _ + 2 => a
  compat i j hij hj hi hj1 := match i, j, hij, hj, hi, hj1 with
    | 0, 0, _, _, _, _ => e
    | 0, 1, _, _, _, h => absurd rfl h
    | 1, 0, h, _, _, _ => absurd h (Nat.not_succ_le_zero 0)
    | 1, 1, _, _, _, h => absurd rfl h
    | 0, _ + 2, _, h, _, _ => absurd (Nat.le_of_succ_le_succ h) (Nat.not_succ_le_zero _)
    | 1, _ + 2, _, h, _, _ => absurd (Nat.le_of_succ_le_succ h) (Nat.not_succ_le_zero _)
    | _ + 2, _, h, hj, _, _ => absurd (Nat.le_of_succ_le_succ (Nat.le_trans h hj)) (Nat.not_succ_le_zero _)

theorem ne10 : (1 : Nat) ≠ 0 := fun h => Nat.noConfusion h
theorem ne20 : (2 : Nat) ≠ 0 := fun h => Nat.noConfusion h

variable (hK : Kan X)

include hK in
/-- in a Kan complex every edge has a left inverse up to homotopy: g with Tri f g id -/
theorem kan_left {x y : (V X).car} (f : Mor x y) : ∃ g : Mor y x, Tri X f.1 g.1 (idv X x) :=
  match hK 0 0 (Nat.zero_le 2) (h20 (idv X x) f.1 ((V X).trans f.2.1 ((V X).symm (d1_idv X x)))) with
  | ⟨σ, hσ⟩ =>
    have f1 := hσ 1 (Nat.le_succ 1) ne10
    have f2 := hσ 2 (Nat.le_refl 2) ne20
    ⟨⟨dd X 0 σ,
        (V X).trans ((V X).symm (dd_dd X (Nat.zero_le 1) σ)) ((V X).trans (X.map_eqv (face 0 0) f2) f.2.2),
        (V X).trans ((V X).symm (dd_dd X (Nat.le_refl 0) σ))
          ((V X).trans (X.map_eqv (face 0 0) f1) (d0_idv X x))⟩,
      ⟨σ, f2, (E X).refl _, f1⟩⟩

include hK in
/-- in a Kan complex every edge has a right inverse up to homotopy: h with Tri h f id -/
theorem kan_right {x y : (V X).car} (f : Mor x y) : ∃ h : Mor y x, Tri X h.1 f.1 (idv X y) :=
  match hK 0 2 (Nat.le_refl 2) (h22 f.1 (idv X y) ((V X).trans (d0_idv X y) ((V X).symm f.2.2))) with
  | ⟨σ, hσ⟩ =>
    have f0 := hσ 0 (Nat.zero_le 2) ne02
    have f1 := hσ 1 (Nat.le_succ 1) ne12
    ⟨⟨dd X 2 σ,
        (V X).trans (dd_dd X (Nat.le_refl 1) σ) ((V X).trans (X.map_eqv (face 0 1) f1) (d1_idv X y)),
        (V X).trans (dd_dd X (Nat.zero_le 1) σ) ((V X).trans (X.map_eqv (face 0 1) f0) f.2.1)⟩,
      ⟨σ, (E X).refl _, f0, f1⟩⟩

variable (c2 : (h : Horn X 0 1) → { σ // Fills h σ })

/-- equivalences of a quasi-category: edges invertible in the homotopy category -/
def IsEquiv (hQ : Quasi X) {x y : (V X).car} (f : Mor x y) : Prop := ECat.IsIso (C := hCat hQ c2) f

/-- infinity-groupoids: quasi-categories all of whose edges are equivalences -/
def InfGpd (hQ : Quasi X) : Prop := ECat.Groupoid (hCat hQ c2)

include hK in
/-- Kan complexes are infinity-groupoids -/
theorem kan_infGpd : InfGpd c2 (kan_quasi hK) := fun {x y} f =>
  match kan_left hK f, kan_right hK f with
  | ⟨g, tg⟩, ⟨h, th⟩ =>
    have hg : (hCat (kan_quasi hK) c2).heq ((hCat (kan_quasi hK) c2).comp f g) ((hCat (kan_quasi hK) c2).id x) :=
      tri_uniq (kan_quasi hK) g.2.2 (compS_tri c2 f g) tg
    have hh : (hCat (kan_quasi hK) c2).heq ((hCat (kan_quasi hK) c2).comp h f) ((hCat (kan_quasi hK) c2).id y) :=
      tri_uniq (kan_quasi hK) f.2.2 (compS_tri c2 h f) th
    ⟨g, hg, (ECat.inv_of_lr (C := hCat (kan_quasi hK) c2) f g h hg hh).2⟩

end IC

end Found

#print axioms Found.IC.ECat.inv_of_lr
#print axioms Found.IC.kan_left
#print axioms Found.IC.kan_right
#print axioms Found.IC.kan_infGpd

found/IC30.lean

lean_all/found/IC30.lean

/-
  T153 ((infinity,1)-categories), part 30. found/IC30 — function complexes,
  I: the horn Lambda^(n+2)_k x K inside Delta^(n+2) x K as a subcomplex, a
  horn of Fun(K, X) glued into a partial map on it (canonical values, unique
  by the horn compatibilities), and the retraction
    r : Delta^2 x Delta^(n+2) -> Delta^(n+2),  r(0,y) = min(y,k), r(1,y) = k,
    r(2,y) = max(y,k),
  of s(y) = (0,y) (y<k), (1,k), (2,y) (y>k), which maps
  Lambda^2_1 x Delta^(n+2) ∪ Delta^2 x Lambda^(n+2)_k into Lambda^(n+2)_k.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.IC29

namespace Found

namespace IC

open Simp

universe w

/-! ## min -/

def mn2 (x y : Nat) : Nat := cond (gtb x y) y x

theorem mn2_le {x y : Nat} (h : x ≤ y) : mn2 x y = x := congrArg (fun t => cond t y x) (le_gtb x y h)
theorem mn2_gt {x y : Nat} (h : y < x) : mn2 x y = y := congrArg (fun t => cond t y x) (lt_gtb x y h)

theorem mn2_le_l (x y : Nat) : mn2 x y ≤ x :=
  match Nat.lt_or_ge y x with
  | .inl h => by rw [mn2_gt h]; exact Nat.le_of_lt h
  | .inr h => by rw [mn2_le h]; exact Nat.le_refl x

theorem mn2_le_r (x y : Nat) : mn2 x y ≤ y :=
  match Nat.lt_or_ge y x with
  | .inl h => by rw [mn2_gt h]; exact Nat.le_refl y
  | .inr h => by rw [mn2_le h]; exact h

theorem le_mn2 {x y t : Nat} (hx : t ≤ x) (hy : t ≤ y) : t ≤ mn2 x y :=
  match Nat.lt_or_ge y x with
  | .inl h => by rw [mn2_gt h]; exact hy
  | .inr h => by rw [mn2_le h]; exact hx

theorem mn2_cases (x y : Nat) : mn2 x y = x ∨ mn2 x y = y :=
  match Nat.lt_or_ge y x with
  | .inl h => .inr (mn2_gt h)
  | .inr h => .inl (mn2_le h)

theorem mx2_cases (x y : Nat) : mx2 x y = x ∨ mx2 x y = y :=
  match Nat.lt_or_ge y x with
  | .inl h => .inl (mx2_gt h)
  | .inr h => .inr (mx2_ge h)

theorem mx2_mono {x x' y y' : Nat} (hx : x ≤ x') (hy : y ≤ y') : mx2 x y ≤ mx2 x' y' :=
  mx2_le (Nat.le_trans hx (le_mx2_l _ _)) (Nat.le_trans hy (le_mx2_r _ _))

theorem mn2_mono {x x' y y' : Nat} (hx : x ≤ x') (hy : y ≤ y') : mn2 x y ≤ mn2 x' y' :=
  le_mn2 (Nat.le_trans (mn2_le_l _ _) hx) (Nat.le_trans (mn2_le_r _ _) hy)

/-! ## the retraction -/

section retr

variable {N k : Nat} (hkN : k ≤ N)

def loR (k x : Nat) : Nat := cond (gtb 1 x) 0 k
def hiR (N k x : Nat) : Nat := cond (gtb 2 x) k N

theorem lo_0 (k : Nat) : loR k 0 = 0 := rfl
theorem lo_pos {x : Nat} (h : 1 ≤ x) : loR k x = k := congrArg (fun t => cond t 0 k) (le_gtb 1 x h)
theorem hi_lo {x : Nat} (h : x ≤ 1) : hiR N k x = k := congrArg (fun t => cond t k N) (lt_gtb 2 x (Nat.lt_succ_of_le h))
theorem hi_2 {x : Nat} (h : 2 ≤ x) : hiR N k x = N := congrArg (fun t => cond t k N) (le_gtb 2 x h)

theorem lo_mono {x x' : Nat} (h : x ≤ x') : loR k x ≤ loR k x' :=
  match Nat.lt_or_ge x 1 with
  | .inl h0 => by rw [Nat.eq_zero_of_le_zero (Nat.le_of_lt_succ h0), lo_0]; exact Nat.zero_le _
  | .inr h1 => by rw [lo_pos h1, lo_pos (Nat.le_trans h1 h)]; exact Nat.le_refl k

include hkN in
theorem k_le_hi (x : Nat) : k ≤ hiR N k x :=
  match Nat.lt_or_ge x 2 with
  | .inl h => by rw [hi_lo (Nat.le_of_lt_succ h)]; exact Nat.le_refl k
  | .inr h => by rw [hi_2 h]; exact hkN

include hkN in
theorem hi_le (x : Nat) : hiR N k x ≤ N :=
  match Nat.lt_or_ge x 2 with
  | .inl h => by rw [hi_lo (Nat.le_of_lt_succ h)]; exact hkN
  | .inr h => by rw [hi_2 h]; exact Nat.le_refl N

include hkN in
theorem hi_mono {x x' : Nat} (h : x ≤ x') : hiR N k x ≤ hiR N k x' :=
  match Nat.lt_or_ge x' 2 with
  | .inl h' => by
    rw [hi_lo (Nat.le_of_lt_succ h'), hi_lo (Nat.le_of_lt_succ (Nat.lt_of_le_of_lt h h'))]; exact Nat.le_refl k
  | .inr h' => by rw [hi_2 h']; exact hi_le hkN x

/-- r(x, y) = min(max(y, lo x), hi x) -/
def Rf (N k x y : Nat) : Nat := mn2 (mx2 y (loR k x)) (hiR N k x)

include hkN in
theorem Rf_mono {x x' y y' : Nat} (hx : x ≤ x') (hy : y ≤ y') : Rf N k x y ≤ Rf N k x' y' :=
  mn2_mono (mx2_mono hy (lo_mono hx)) (hi_mono hkN hx)

include hkN in
theorem Rf_le (x y : Nat) : Rf N k x y ≤ N := Nat.le_trans (mn2_le_r _ _) (hi_le hkN x)

include hkN in
theorem Rf_ge_k {x : Nat} (y : Nat) (h : 1 ≤ x) : k ≤ Rf N k x y :=
  le_mn2 (by rw [lo_pos h]; exact le_mx2_r _ _) (k_le_hi hkN x)

theorem Rf_le_k {x : Nat} (y : Nat) (h : x ≤ 1) : Rf N k x y ≤ k := by
  show mn2 (mx2 y (loR k x)) (hiR N k x) ≤ k
  rw [hi_lo h]; exact mn2_le_r _ _

include hkN in
theorem Rf_cases (x : Nat) {y : Nat} (hy : y ≤ N) : Rf N k x y = y ∨ Rf N k x y = k :=
  have hm : mx2 y (loR k x) = y ∨ mx2 y (loR k x) = k :=
    match mx2_cases y (loR k x), Nat.lt_or_ge x 1 with
    | .inl e, _ => .inl e
    | .inr e, .inr h1 => .inr (e.trans (lo_pos h1))
    | .inr e, .inl h0 => .inl (Nat.le_antisymm (by
        rw [e, Nat.eq_zero_of_le_zero (Nat.le_of_lt_succ h0)]
        exact Nat.zero_le y) (le_mx2_l _ _))
  have hc : mx2 y (loR k x) ≤ N := hm.elim (fun e => by rw [e]; exact hy) (fun e => by rw [e]; exact hkN)
  match mn2_cases (mx2 y (loR k x)) (hiR N k x), Nat.lt_or_ge x 2 with
  | .inl e, _ => hm.elim (fun e' => .inl (e.trans e')) (fun e' => .inr (e.trans e'))
  | .inr e, .inl h => .inr (e.trans (hi_lo (Nat.le_of_lt_succ h)))
  | .inr e, .inr h =>
    have e2 : mn2 (mx2 y (loR k x)) (hiR N k x) = mx2 y (loR k x) :=
      Nat.le_antisymm (mn2_le_l _ _) (Nat.le_trans hc (Nat.le_of_eq (e.trans (hi_2 h)).symm))
    hm.elim (fun e' => .inl (e2.trans e')) (fun e' => .inr (e2.trans e'))

/-- r ∘ s = id -/
theorem rs_id {k' y : Nat} (hy : y ≤ N) : Rf N (k' + 1) (uW k' k' y) y = y :=
  match Nat.lt_or_ge k' y with
  | .inr h => by
    show mn2 (mx2 y (loR (k' + 1) (uW k' k' y))) (hiR N (k' + 1) (uW k' k' y)) = y
    rw [uW_0 (Nat.le_refl k') h, lo_0, hi_lo (Nat.zero_le 1)]
    have e : mx2 y 0 = y := match mx2_cases y 0 with
      | .inl e => e
      | .inr e => e.trans (Nat.eq_zero_of_le_zero (Nat.le_trans (le_mx2_l y 0) (Nat.le_of_eq e))).symm
    rw [e, mn2_le (Nat.le_succ_of_le h)]
  | .inl h => match Nat.lt_or_ge (k' + 1) y with
    | .inr h' => by
      have e : y = k' + 1 := Nat.le_antisymm h' h
      subst e
      show mn2 (mx2 (k' + 1) (loR (k' + 1) (uW k' k' (k' + 1)))) (hiR N (k' + 1) (uW k' k' (k' + 1))) = k' + 1
      rw [uW_1 (Nat.le_refl k') (Nat.le_refl k'), lo_pos (Nat.le_refl 1), hi_lo (Nat.le_refl 1)]
      have e1 : mx2 (k' + 1) (k' + 1) = k' + 1 := (mx2_cases _ _).elim id id
      rw [e1, mn2_le (Nat.le_refl _)]
    | .inl h' => by
      show mn2 (mx2 y (loR (k' + 1) (uW k' k' y))) (hiR N (k' + 1) (uW k' k' y)) = y
      rw [uW_2 (Nat.le_refl k') h', lo_pos (Nat.le_succ 1), hi_2 (Nat.le_refl 2), mx2_gt h', mn2_le hy]

end retr

/-- the map r on simplices -/
def rD {q N : Nat} (k : Nat) (hkN : k ≤ N) (u : DHom q 2) (α : DHom q N) : DHom q N :=
  ⟨fun t => Rf N k (u.f t) (α.f t), fun t ht => Rf_mono hkN (u.mono t ht) (α.mono t ht),
    fun _ _ => Rf_le hkN _ _⟩

/-! ## horns of Fun(K, X) as partial maps -/

section hornglue

variable (Kw : ClW.{w}) {K X : SSet.{w}} {n k : Nat} (h : Horn (FunS K X) n k)

/-- Lambda^(n+2)_k x K -/
def AH (K : SSet.{w}) (n k : Nat) : Sub (SProd (Simp.{w} (n + 2)) K) where
  P q z := ∃ i, i ≤ n + 2 ∧ i ≠ k ∧ ∀ r, r ≤ q → z.1.down.f r ≠ i
  P_eqv _ {_ _} e hz := match hz with
    | ⟨i, hi, hik, hm⟩ => ⟨i, hi, hik, fun r hr e' => hm r hr ((e.1 r hr).trans e')⟩
  P_map g _ hz := match hz with
    | ⟨i, hi, hik, hm⟩ => ⟨i, hi, hik, fun r hr => hm _ (g.bound r hr)⟩

/-- canonical values of the horn -/
def CanH {q : Nat} (z : ((SProd (Simp.{w} (n + 2)) K).obj (q : Nat)).car) (x : (X.obj (q : Nat)).car) : Prop :=
  ∃ (i : Nat) (a : DHom q (n + 1)), i ≤ n + 2 ∧ i ≠ k ∧ (∀ r, r ≤ q → z.1.down.f r = sk i (a.f r)) ∧
    (X.obj (q : Nat)).eqv x ((h.x i).app q (⟨a⟩, z.2))

theorem canH_two {q : Nat} (κ : (K.obj (q : Nat)).car) (a a' : DHom q (n + 1)) {i J : Nat} (hiJ : i < J + 1)
    (hJ : J + 1 ≤ n + 2) (hik : i ≠ k) (hJk : J + 1 ≠ k) (hw : ∀ r, r ≤ q → sk i (a.f r) = sk (J + 1) (a'.f r)) :
    (X.obj (q : Nat)).eqv ((h.x i).app q (⟨a⟩, κ)) ((h.x (J + 1)).app q (⟨a'⟩, κ)) :=
  have hiJ' : i ≤ J := Nat.le_of_lt_succ hiJ
  have hJ' : J ≤ n + 1 := Nat.le_of_succ_le_succ hJ
  have hi : i ≤ n + 1 := Nat.le_trans hiJ' hJ'
  have hmiss : ∀ r, r ≤ q → a'.f r ≠ i := fun r hr e => sk_ne i (a.f r) (by
    rw [hw r hr, e, sk_lt (Nat.lt_succ_of_le hiJ')])
  have ht := unsk_spec a' hi hmiss
  let t := unsk a' i hi
  have hwt : ∀ r, r ≤ q → a.f r = sk J (t.f r) := fun r hr =>
    sk_inj ((hw r hr).trans (by rw [ht r hr]; exact sk_sk hiJ' _))
  have m1 : (X.obj (q : Nat)).eqv ((h.x i).app q (⟨a⟩, κ)) ((h.x i).app q (⟨dComp t (face n J)⟩, κ)) :=
    (h.x i).app_eqv q (x := (⟨a⟩, κ)) (y := (⟨dComp t (face n J)⟩, κ)) ⟨hwt, (K.obj _).refl _⟩
  have m2 : (X.obj (q : Nat)).eqv ((h.x i).app q (⟨dComp t (face n J)⟩, κ))
      ((h.x (J + 1)).app q (⟨dComp t (face n i)⟩, κ)) :=
    (X.obj _).symm (h.compat i J hiJ' hJ' hik hJk q (⟨t⟩, κ))
  have m3 : (X.obj (q : Nat)).eqv ((h.x (J + 1)).app q (⟨dComp t (face n i)⟩, κ)) ((h.x (J + 1)).app q (⟨a'⟩, κ)) :=
    (h.x (J + 1)).app_eqv q (x := (⟨dComp t (face n i)⟩, κ)) (y := (⟨a'⟩, κ))
      ⟨fun r hr => (ht r hr).symm, (K.obj _).refl _⟩
  (X.obj _).trans m1 ((X.obj _).trans m2 m3)

theorem canH_lt {q : Nat} (κ : (K.obj (q : Nat)).car) (a a' : DHom q (n + 1)) {i i' : Nat} (hl : i < i')
    (hi' : i' ≤ n + 2) (hik : i ≠ k) (hik' : i' ≠ k) (hww : ∀ r, r ≤ q → sk i (a.f r) = sk i' (a'.f r)) :
    (X.obj (q : Nat)).eqv ((h.x i).app q (⟨a⟩, κ)) ((h.x i').app q (⟨a'⟩, κ)) :=
  match i', hl, hi', hik', hww with
  | 0, hl, _, _, _ => absurd hl (Nat.not_lt_zero _)
  | _ + 1, hl, hi', hik', hww => canH_two h κ a a' hl hi' hik hik' hww

theorem canH_unique {q : Nat} {z : ((SProd (Simp.{w} (n + 2)) K).obj (q : Nat)).car} {x y : (X.obj (q : Nat)).car}
    (hx : CanH h z x) (hy : CanH h z y) : (X.obj (q : Nat)).eqv x y :=
  match hx, hy with
  | ⟨i, a, hi, hik, hw, hxv⟩, ⟨i', a', hi', hik', hw', hyv⟩ =>
    have hww : ∀ r, r ≤ q → sk i (a.f r) = sk i' (a'.f r) := fun r hr => (hw r hr).symm.trans (hw' r hr)
    have mid : (X.obj (q : Nat)).eqv ((h.x i).app q (⟨a⟩, z.2)) ((h.x i').app q (⟨a'⟩, z.2)) :=
      match Nat.lt_or_ge i i' with
      | .inl hl => canH_lt h z.2 a a' hl hi' hik hik' hww
      | .inr hge => match Nat.lt_or_ge i' i with
        | .inl hl => (X.obj _).symm (canH_lt h z.2 a' a hl hi hik' hik (fun r hr => (hww r hr).symm))
        | .inr hge' => by
          have e : i = i' := Nat.le_antisymm hge' hge
          subst e
          exact (h.x i).app_eqv q (x := (⟨a⟩, z.2)) (y := (⟨a'⟩, z.2))
            ⟨fun r hr => sk_inj (hww r hr), (K.obj _).refl _⟩
    (X.obj _).trans hxv ((X.obj _).trans mid ((X.obj _).symm hyv))

theorem canH_eqv {q : Nat} {z z' : ((SProd (Simp.{w} (n + 2)) K).obj (q : Nat)).car} {x : (X.obj (q : Nat)).car}
    (e : ((SProd (Simp.{w} (n + 2)) K).obj (q : Nat)).eqv z z') (hc : CanH h z x) : CanH h z' x :=
  match hc with
  | ⟨i, a, hi, hik, hw, hxv⟩ => ⟨i, a, hi, hik, fun r hr => (e.1 r hr).symm.trans (hw r hr),
      (X.obj _).trans hxv ((h.x i).app_eqv q ⟨fun _ _ => rfl, e.2⟩)⟩

theorem canH_map {q' q : Nat} (g : DHom q' q) {z : ((SProd (Simp.{w} (n + 2)) K).obj (q : Nat)).car}
    {x : (X.obj (q : Nat)).car} (hc : CanH h z x) :
    CanH h (mp (SProd (Simp.{w} (n + 2)) K) g z) (mp X g x) :=
  match hc with
  | ⟨i, a, hi, hik, hw, hxv⟩ => ⟨i, dComp g a, hi, hik, fun r hr => hw (g.f r) (g.bound r hr),
      (X.obj _).trans (mp_eqv X g hxv) ((X.obj _).symm ((h.x i).nat g (⟨a⟩, z.2)))⟩

theorem canH_ex {q : Nat} (z : ((SProd (Simp.{w} (n + 2)) K).obj (q : Nat)).car) (hz : (AH K n k).P q z) :
    ∃ x, CanH h z x :=
  match hz with
  | ⟨i, hi, hik, hm⟩ => ⟨_, i, unsk z.1.down i hi, hi, hik, unsk_spec z.1.down hi hm, (X.obj _).refl _⟩

/-- the horn as a partial map on Lambda^(n+2)_k x K -/
def hornMap : PMap (AH K n k) X where
  app _ z hz := Kw.ch (CanH h z) (canH_ex h z hz)
  app_eqv _ {z z'} e hz hz' := canH_unique h (canH_eqv h e (Kw.ch_spec (CanH h z) (canH_ex h z hz)))
    (Kw.ch_spec (CanH h z') (canH_ex h z' hz'))
  nat {_ _} g z hz := canH_unique h
    (Kw.ch_spec (CanH h (mp (SProd (Simp.{w} (n + 2)) K) g z)) (canH_ex h _ ((AH K n k).P_map g z hz)))
    (canH_map h g (Kw.ch_spec (CanH h z) (canH_ex h z hz)))

theorem hornMap_spec {q : Nat} (z : ((SProd (Simp.{w} (n + 2)) K).obj (q : Nat)).car) (hz : (AH K n k).P q z) :
    CanH h z ((hornMap Kw h).app q z hz) :=
  Kw.ch_spec _ _

end hornglue

end IC

end Found

#print axioms Found.IC.Rf_cases
#print axioms Found.IC.rs_id
#print axioms Found.IC.canH_unique
#print axioms Found.IC.hornMap

found/IC31.lean

lean_all/found/IC31.lean

/-
  T153 ((infinity,1)-categories), part 31. found/IC31 — function complexes,
  II (Joyal): if X is a quasi-category then so is Fun(K, X), for every
  simplicial set K (inside the classical point).  A horn of Fun(K, X) is a
  partial map on Lambda^(n+2)_k x K (IC30); composed with the retraction r it
  is a map on Lambda^2_1 x B ∪ Delta^2 x (Lambda^(n+2)_k x K) with
  B = Delta^(n+2) x K, which extends to Delta^2 x B (IC29); composing the
  extension with s x K fills the horn, because r s = id.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.IC30

namespace Found

namespace IC

open Simp

universe w

theorem pos_of_ne0 {x : Nat} (h : x ≠ 0) : 1 ≤ x :=
  match x, h with
  | 0, h => absurd rfl h
  | _ + 1, _ => Nat.succ_le_succ (Nat.zero_le _)

section funq

variable {K X : SSet.{w}} {n k : Nat} (hkN : k ≤ n + 2)

/-- (r x id) on simplices of Delta^2 x (Delta^(n+2) x K) -/
def rpt {q : Nat} (z : ((SProd (Simp.{w} 2) (SProd (Simp.{w} (n + 2)) K)).obj (q : Nat)).car) :
    ((SProd (Simp.{w} (n + 2)) K).obj (q : Nat)).car :=
  (⟨rD k hkN z.1.down z.2.1.down⟩, z.2.2)

include hkN in
theorem rpt_in (hk0 : 0 < k) (hk : k < n + 2) {q : Nat}
    (z : ((SProd (Simp.{w} 2) (SProd (Simp.{w} (n + 2)) K)).obj (q : Nat)).car) (hz : (LA (AH K n k)).P q z) :
    (AH K n k).P q (rpt hkN z) :=
  match hz with
  | .inl (.inl h0) => ⟨0, Nat.zero_le _, fun e => Nat.lt_irrefl 0 (Nat.lt_of_lt_of_le hk0 (Nat.le_of_eq e.symm)),
      fun r hr e => Nat.lt_irrefl 0 (Nat.lt_of_lt_of_le hk0
        (Nat.le_trans (Rf_ge_k hkN _ (pos_of_ne0 (h0 r hr))) (Nat.le_of_eq e)))⟩
  | .inl (.inr h2) => ⟨n + 2, Nat.le_refl _, fun e => ne_of_lt' hk e.symm,
      fun r hr e => Nat.lt_irrefl k (Nat.lt_of_lt_of_le hk (Nat.le_trans (Nat.le_of_eq e.symm)
        (Rf_le_k _ (Nat.le_of_lt_succ (Nat.lt_of_le_of_ne (z.1.down.bound r hr) (h2 r hr))))))⟩
  | .inr ⟨i, hi, hik, hm⟩ => ⟨i, hi, hik, fun r hr e =>
      (Rf_cases hkN (z.1.down.f r) (z.2.1.down.bound r hr)).elim (fun e' => hm r hr (e'.symm.trans e))
        (fun e' => hik (e.symm.trans e'))⟩

/-- the horn, pulled back along r x id -/
def hornR (Kw : ClW.{w}) (hk0 : 0 < k) (hk : k < n + 2) (h : Horn (FunS K X) n k) : PMap (LA (AH K n k)) X where
  app q z hz := (hornMap Kw h).app q (rpt hkN z) (rpt_in hkN hk0 hk z hz)
  app_eqv q {z z'} e _ _ := (hornMap Kw h).app_eqv q (z := rpt hkN z) (z' := rpt hkN z')
    ⟨fun r hr => by
      show Rf (n + 2) k (z.1.down.f r) (z.2.1.down.f r) = Rf (n + 2) k (z'.1.down.f r) (z'.2.1.down.f r)
      rw [e.1 r hr, e.2.1 r hr], e.2.2⟩ _ _
  nat {q' _} g z hz := (X.obj _).trans ((hornMap Kw h).app_eqv q'
      (z := rpt hkN (mp (SProd (Simp.{w} 2) (SProd (Simp.{w} (n + 2)) K)) g z))
      (z' := mp (SProd (Simp.{w} (n + 2)) K) g (rpt hkN z)) ⟨fun _ _ => rfl, (K.obj _).refl _⟩ _ _)
    ((hornMap Kw h).nat g (rpt hkN z) (rpt_in hkN hk0 hk z hz))

end funq

/-- Joyal: function complexes into a quasi-category are quasi-categories -/
theorem fun_quasi (Kw : ClW.{w}) (Kc : CP.Classical') {K X : SSet.{w}} (hQ : Quasi X) : Quasi (FunS K X) :=
  fun n k hk0 hk h => match k, hk0, hk, h with
  | 0, hk0, _, _ => absurd hk0 (Nat.lt_irrefl 0)
  | k' + 1, hk0, hk, h =>
    have hkN : k' + 1 ≤ n + 2 := Nat.le_of_lt hk
    match lam_ext Kw Kc hQ (AH K n (k' + 1)) (hornR hkN Kw hk0 hk h) with
    | ⟨Φ, hΦ⟩ => ⟨{
        app := fun q z => Φ.app q (⟨dComp z.1.down (uD k' k' (n + 2))⟩, z)
        app_eqv := fun q {_ _} e => Φ.app_eqv q ⟨fun r hr => congrArg (uW k' k') (e.1 r hr), e⟩
        nat := fun {_ _} g z => (X.obj _).trans
          (Φ.app_eqv _ ⟨fun _ _ => rfl, ⟨fun _ _ => rfl, (K.obj _).refl _⟩⟩)
          (Φ.nat g (⟨dComp z.1.down (uD k' k' (n + 2))⟩, z)) },
      fun i hi hik q z =>
        have hLA : (LA (AH K n (k' + 1))).P q (⟨dComp (dComp z.1.down (face (n + 1) i)) (uD k' k' (n + 2))⟩,
            (⟨dComp z.1.down (face (n + 1) i)⟩, z.2)) := .inr ⟨i, hi, hik, fun r _ => sk_ne i (z.1.down.f r)⟩
        (X.obj _).trans (hΦ q _ hLA)
          (canH_unique h (hornMap_spec Kw h _ (rpt_in hkN hk0 hk _ hLA))
            ⟨i, z.1.down, hi, hik, fun r hr => rs_id ((face (n + 1) i).bound _ (z.1.down.bound r hr)),
              (X.obj _).refl _⟩)⟩

end IC

end Found

#print axioms Found.IC.rpt_in
#print axioms Found.IC.hornR
#print axioms Found.IC.fun_quasi

found/IC32.lean

lean_all/found/IC32.lean

/-
  T153 ((infinity,1)-categories), part 32. found/IC32 — the opposite
  simplicial set X^op (reverse the order of every [n]: i |-> n - i), its
  faces d_i = d_(n+1-i), and: X quasi-category => X^op quasi-category
  (an inner horn at k becomes an inner horn at n+2-k).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.IC31

namespace Found

namespace IC

open Simp

universe w

/-! ## arithmetic of reversal -/

theorem le_split' {x N : Nat} (h : x ≤ N) : ∃ d, x + d = N := by
  induction h with
  | refl => exact ⟨0, rfl⟩
  | step _ ih => match ih with | ⟨d, e⟩ => exact ⟨d + 1, congrArg Nat.succ e⟩

theorem sub_of_add : ∀ {a b c : Nat}, a + b = c → c - a = b
  | 0, b, _, e => by rw [← e, Nat.zero_add]; rfl
  | a + 1, b, c, e => match c, e with
    | 0, e => Nat.noConfusion ((Nat.succ_add a b).symm.trans e)
    | c + 1, e => by
      show (c + 1) - (a + 1) = b
      rw [Nat.succ_sub_succ]
      exact sub_of_add (Nat.succ.inj ((Nat.succ_add a b).symm.trans e))

theorem sub_sub' {x N : Nat} (h : x ≤ N) : N - (N - x) = x :=
  match le_split' h with
  | ⟨d, e⟩ => by
    rw [sub_of_add e]
    exact sub_of_add ((Nat.add_comm d x).trans e)

theorem sub_le' {x N : Nat} (h : x ≤ N) : N - x ≤ N :=
  match le_split' h with
  | ⟨d, e⟩ => by rw [sub_of_add e, ← e]; exact Nat.le_add_left d x

theorem sub_anti {x y N : Nat} (hxy : x ≤ y) (hy : y ≤ N) : N - y ≤ N - x :=
  match le_split' hxy, le_split' hy with
  | ⟨t, e1⟩, ⟨d, e2⟩ => by
    have e3 : x + (t + d) = N := by rw [← Nat.add_assoc, e1, e2]
    rw [sub_of_add e2, sub_of_add e3]
    exact Nat.le_add_left d t

theorem le_of_add_le_add_r' {a b : Nat} : ∀ c, a + c ≤ b + c → a ≤ b
  | 0, h => h
  | c + 1, h => le_of_add_le_add_r' c (Nat.le_of_succ_le_succ h)

/-- reversing a face: (n+1) - sk i (n - j) = sk (n+1-i) j -/
theorem rev_face {n i j : Nat} (hi : i ≤ n + 1) (hj : j ≤ n) : (n + 1) - sk i (n - j) = sk (n + 1 - i) j :=
  match le_split' hj, le_split' hi with
  | ⟨d, ed⟩, ⟨c, ec⟩ => by
    have hnj : n - j = d := sub_of_add ed
    have hc : n + 1 - i = c := sub_of_add ec
    rw [hnj, hc]
    match Nat.lt_or_ge d i with
    | .inl h =>
      -- d < i, so c ≤ j
      rw [sk_lt h]
      have e1 : d + (j + 1) = n + 1 := by rw [← Nat.add_assoc, Nat.add_comm d j, ed]
      rw [sub_of_add e1]
      have hcj : c ≤ j := le_of_add_le_add_r' i (by
        rw [Nat.add_comm c i, ec, ← ed, Nat.add_comm j d, Nat.add_comm j i]
        exact Nat.le_trans (Nat.le_of_eq (Nat.succ_add d j).symm) (Nat.add_le_add_right (Nat.succ_le_of_lt h) j))
      rw [sk_ge hcj]
    | .inr h =>
      -- i ≤ d, so j < c
      rw [sk_ge h]
      have e1 : (d + 1) + j = n + 1 := by rw [Nat.succ_add, Nat.add_comm d j, ed]
      rw [sub_of_add e1]
      have hjc : j < c := Nat.lt_of_succ_le (le_of_add_le_add_r' i (by
        rw [Nat.add_comm c i, ec, ← ed, Nat.succ_add, Nat.add_comm j d]
        exact Nat.succ_le_succ (Nat.le_trans (Nat.le_of_eq (Nat.add_comm j i)) (Nat.add_le_add_right h j))))
      rw [sk_lt hjc]

/-! ## the opposite simplicial set -/

/-- the reversed map: i |-> n - g (m - i) -/
def opD {m n : Nat} (g : DHom m n) : DHom m n :=
  ⟨fun i => n - g.f (m - i),
    fun i hi => sub_anti (dmono g (sub_anti (Nat.le_succ i) (Nat.succ_le_of_lt hi)) (sub_le' (Nat.le_of_lt hi)))
      (g.bound _ (sub_le' (Nat.le_of_lt hi))),
    fun _ hi => sub_le' (g.bound _ (sub_le' hi))⟩

theorem opD_comp {a b c : Nat} (f : DHom a b) (g : DHom b c) :
    ∀ i, i ≤ a → (opD (dComp f g)).f i = (dComp (opD f) (opD g)).f i := fun i hi => by
  show c - g.f (f.f (a - i)) = c - g.f (b - (b - f.f (a - i)))
  rw [sub_sub' (f.bound _ (sub_le' hi))]

theorem opD_id (n : Nat) : ∀ i, i ≤ n → (opD (dId n)).f i = i := fun _ hi => sub_sub' hi

theorem opD_resp {m n : Nat} {f g : DHom m n} (h : ∀ i, i ≤ m → f.f i = g.f i) :
    ∀ i, i ≤ m → (opD f).f i = (opD g).f i := fun i hi => by
  show n - f.f (m - i) = n - g.f (m - i)
  rw [h _ (sub_le' hi)]

/-- X^op -/
def Op (X : SSet.{w}) : SSet.{w} where
  obj := X.obj
  map g x := X.map (opD g) x
  map_eqv {_ _} g {_ _} h := X.map_eqv (opD g) h
  map_resp {_ _ _ _} x h := X.map_resp (f := opD _) (g := opD _) x (opD_resp h)
  map_id a x := (X.obj a).trans (X.map_resp (f := opD (Delta.id a)) (g := Delta.id a) x (opD_id a)) (X.map_id a x)
  map_comp f g x := (X.obj _).trans (X.map_resp (f := opD (Delta.comp f g)) (g := Delta.comp (opD f) (opD g)) x
    (opD_comp f g)) (X.map_comp (opD f) (opD g) x)

theorem dd_op (X : SSet.{w}) {n : Nat} {i : Nat} (hi : i ≤ n + 1) (x : (X.obj (n + 1 : Nat)).car) :
    (X.obj (n : Nat)).eqv (dd (Op X) i x) (dd X (n + 1 - i) x) :=
  X.map_resp (f := opD (face n i)) (g := face n (n + 1 - i)) x (fun _ hj => rev_face hi hj)

/-- X quasi-category => X^op quasi-category -/
theorem op_quasi {X : SSet.{w}} (hQ : Quasi X) : Quasi (Op X) := by
  intro n k hk0 hk h
  have mm : ∀ i, i ≤ n + 2 → n + 2 - (n + 2 - i) = i := fun i hi => sub_sub' hi
  have hkle : k ≤ n + 2 := Nat.le_of_lt hk
  have hc0 : 0 < n + 2 - k := by
    obtain ⟨d, ed⟩ := le_split' hkle
    rw [sub_of_add ed]
    cases d with
    | zero => exact absurd (Nat.lt_of_lt_of_le hk (Nat.le_of_eq ed.symm)) (Nat.lt_irrefl _)
    | succ d => exact Nat.succ_pos d
  have hc : n + 2 - k < n + 2 := by
    obtain ⟨d, ed⟩ := le_split' hkle
    rw [sub_of_add ed, ← ed]
    exact Nat.lt_add_of_pos_left hk0
  have inj : ∀ i, i ≤ n + 2 → n + 2 - i = n + 2 - k → i = k := fun i hi e =>
    (mm i hi).symm.trans ((congrArg (fun t => n + 2 - t) e).trans (mm k hkle))
  let h' : Horn X n (n + 2 - k) := ⟨fun i => h.x (n + 2 - i), fun i j hij hj hi hj1 => by
    obtain ⟨b, eb⟩ := le_split' (Nat.le_trans hij hj)
    obtain ⟨a, ea⟩ := le_split' hj
    have e1 : n + 2 - (j + 1) = a := sub_of_add (by rw [Nat.succ_add, ea])
    have e2 : n + 2 - i = b + 1 := sub_of_add (by rw [Nat.add_succ, eb])
    have e3 : n + 1 - a = j := sub_of_add ((Nat.add_comm a j).trans ea)
    have e4 : n + 1 - b = i := sub_of_add ((Nat.add_comm b i).trans eb)
    have hab : a ≤ b := le_of_add_le_add_r' j (by
      rw [Nat.add_comm a j, ea, ← eb, Nat.add_comm i b]
      exact Nat.add_le_add_left hij b)
    have hb : b ≤ n + 1 := by rw [← eb]; exact Nat.le_add_left b i
    have ha : a ≠ k := fun e => hj1 (by rw [← e, ← e1, mm (j + 1) (Nat.succ_le_succ hj)])
    have hb1 : b + 1 ≠ k := fun e => hi (by
      rw [← e, ← e2, mm i (Nat.le_trans (Nat.le_trans hij hj) (Nat.le_succ _))])
    have cpt := h.compat a b hab hb ha hb1
    have d1 := dd_op X (Nat.le_trans hab hb) (h.x (b + 1))
    have d2 := dd_op X hb (h.x a)
    rw [e3] at d1
    rw [e4] at d2
    show (X.obj n).eqv (dd X i (h.x (n + 2 - (j + 1)))) (dd X j (h.x (n + 2 - i)))
    rw [e1, e2]
    exact (X.obj n).trans ((X.obj n).symm d2) ((X.obj n).trans ((X.obj n).symm cpt) d1)⟩
  obtain ⟨σ, hσ⟩ := hQ n (n + 2 - k) hc0 hc h'
  refine ⟨σ, fun i hi hik => ?_⟩
  have hne : n + 2 - i ≠ n + 2 - k := fun e => hik (inj i hi e)
  have f1 := hσ (n + 2 - i) (sub_le' hi) hne
  have f2 : (X.obj (n + 1 : Nat)).eqv (dd X (n + 2 - i) σ) (h.x i) := by
    have e := mm i hi
    show (X.obj (n + 1 : Nat)).eqv (dd X (n + 2 - i) σ) (h.x i)
    rw [← e]
    rw [mm (n + 2 - i) (sub_le' hi)]
    exact f1
  exact (X.obj _).trans (dd_op X (n := n + 1) hi σ) f2

end IC

end Found

#print axioms Found.IC.rev_face
#print axioms Found.IC.Op
#print axioms Found.IC.dd_op
#print axioms Found.IC.op_quasi

found/IC33.lean

lean_all/found/IC33.lean

/-
  T153 ((infinity,1)-categories), part 33. found/IC33 — the attaching engine
  for any horn (IC22 generalised): a partial map on A into X extends over
  A ∪ <σ> when A ∩ <σ> is the horn Lambda^(e+2)_k (any k) and X fills that
  horn; e.g. X a Kan complex.  Packaged as one step ext_stepG.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.IC32

namespace Found

namespace IC

open Simp

universe w

section gattach

variable {B X : SSet.{w}}

variable (Kw : ClW.{w}) {A : Sub B} (F : PMap A X) {e k : Nat} (σ : (B.obj (e + 2 : Nat)).car)
  (d0 : Nat) (hd0 : d0 ≤ e + 2) (hd0k : d0 ≠ k) (fillH : ∀ h : Horn X e k, ∃ τ, Fills h τ)
  (inj : ∀ (q : Nat) (g g' : DHom q (e + 2)), (B.obj (q : Nat)).eqv (mp B g σ) (mp B g' σ) →
    ∀ r, r ≤ q → g.f r = g'.f r)
  (hornIn : ∀ (q : Nat) (g : DHom q (e + 2)), A.P q (mp B g σ) → ∃ i, i ≤ e + 2 ∧ i ≠ k ∧ ∀ r, r ≤ q → g.f r ≠ i)
  (hornOut : ∀ i, i ≤ e + 2 → i ≠ k → A.P (e + 1) (dd B i σ))

/-- the faces of σ, read through F -/
def faceValG (i : Nat) : (X.obj (e + 1 : Nat)).car :=
  Kw.ch (fun x => ∀ h : A.P (e + 1) (dd B i σ), (X.obj (e + 1 : Nat)).eqv x (F.app _ _ h))
    (match Kw.lem (A.P (e + 1) (dd B i σ)) with
      | .inl h => ⟨F.app _ _ h, fun h' => F.app_eqv _ ((B.obj _).refl _) h h'⟩
      | .inr h => ⟨F.app _ _ (hornOut d0 hd0 hd0k),
          fun h' => absurd h' h⟩)

include hd0 hd0k hornOut in
theorem faceVal_specG (i : Nat) (h : A.P (e + 1) (dd B i σ)) :
    (X.obj (e + 1 : Nat)).eqv (faceValG Kw F σ d0 hd0 hd0k hornOut i) (F.app _ _ h) :=
  Kw.ch_spec (fun x => ∀ h : A.P (e + 1) (dd B i σ), (X.obj (e + 1 : Nat)).eqv x (F.app _ _ h)) _ h

/-- the horn of X given by the faces of σ -/
def hornSG : Horn X e k where
  x := faceValG Kw F σ d0 hd0 hd0k hornOut
  compat i j hij hj hi hj1 :=
    have hi' := hornOut i (Nat.le_trans hij (Nat.le_succ_of_le hj)) hi
    have hj' := hornOut (j + 1) (Nat.succ_le_succ hj) hj1
    (X.obj _).trans (mp_eqv X _ (faceVal_specG Kw F σ d0 hd0 hd0k hornOut (j + 1) hj'))
      ((X.obj _).trans ((X.obj _).symm (F.nat (face e i) _ hj'))
        ((X.obj _).trans (F.app_eqv _ (dd_dd B hij σ) _ (A.P_map (face e j) _ hi'))
          ((X.obj _).trans (F.nat (face e j) _ hi')
            (mp_eqv X _ ((X.obj _).symm (faceVal_specG Kw F σ d0 hd0 hd0k hornOut i hi'))))))

variable (τ : (X.obj (e + 2 : Nat)).car) (hτ : Fills (hornSG Kw F σ d0 hd0 hd0k hornOut) τ)

include hornIn hτ in
/-- on A ∩ <σ>, F agrees with the pullbacks of the filler τ -/
theorem consistG (q : Nat) (g : DHom q (e + 2)) (hA : A.P q (mp B g σ)) :
    (X.obj (q : Nat)).eqv (F.app q (mp B g σ) hA) (mp X g τ) :=
  match hornIn q g hA with
  | ⟨i, hi, hik, hmiss⟩ =>
    have hf : ∀ r, r ≤ q → g.f r =
        (dComp (dComp g (degen (e + 1) (Nat.pred i) (Nat.pred_le_pred hi))) (face (e + 1) i)).f r :=
      fun r hr => (sk_dg (hmiss r hr)).symm
    have hi' := hornOut i hi hik
    (X.obj _).trans (F.app_eqv q (pull B hf σ) hA (A.P_map _ _ hi'))
      ((X.obj _).trans (F.nat _ _ hi')
        ((X.obj _).trans (mp_eqv X _ ((X.obj _).symm (faceVal_specG Kw F σ d0 hd0 hd0k hornOut i hi')))
          ((X.obj _).trans (mp_eqv X _ ((X.obj _).symm (hτ i hi hik)))
            ((X.obj _).symm (pull X hf τ)))))

/-- the defining property of the extended value -/
def extPG (q : Nat) (z : (B.obj (q : Nat)).car) (x : (X.obj (q : Nat)).car) : Prop :=
  (∀ hA : A.P q z, (X.obj (q : Nat)).eqv x (F.app q z hA)) ∧
  (∀ g : DHom q (e + 2), (B.obj (q : Nat)).eqv z (mp B g σ) → (X.obj (q : Nat)).eqv x (mp X g τ))

include hornIn hτ inj in
theorem extP_exG (q : Nat) (z : (B.obj (q : Nat)).car) (h : (genSub A σ).P q z) :
    ∃ x, extPG F σ τ q z x :=
  match Kw.lem (A.P q z) with
  | .inl hA => ⟨F.app q z hA, fun hA' => F.app_eqv q ((B.obj _).refl z) hA hA',
      fun g hg => (X.obj _).trans (F.app_eqv q hg hA (A.P_eqv q hg hA))
        (consistG Kw F σ d0 hd0 hd0k hornIn hornOut τ hτ q g (A.P_eqv q hg hA))⟩
  | .inr nA => match h with
    | .inl hA => absurd hA nA
    | .inr ⟨g, hg⟩ => ⟨mp X g τ, fun hA => absurd hA nA,
        fun g' hg' => mp_resp X (inj q g g' ((B.obj _).trans ((B.obj _).symm hg) hg')) τ⟩

/-- the extended value at a simplex of A ∪ <σ> -/
def extAG (q : Nat) (z : (B.obj (q : Nat)).car) (h : (genSub A σ).P q z) : (X.obj (q : Nat)).car :=
  Kw.ch (extPG F σ τ q z) (extP_exG Kw F σ d0 hd0 hd0k inj hornIn hornOut τ hτ q z h)

theorem extA_specG (q : Nat) (z : (B.obj (q : Nat)).car) (h : (genSub A σ).P q z) :
    extPG F σ τ q z (extAG Kw F σ d0 hd0 hd0k inj hornIn hornOut τ hτ q z h) :=
  Kw.ch_spec _ _

/-- the extension of F to A ∪ <σ> -/
def extMapG : PMap (genSub A σ) X where
  app := extAG Kw F σ d0 hd0 hd0k inj hornIn hornOut τ hτ
  app_eqv q {z z'} e hz hz' := match Kw.lem (A.P q z) with
    | .inl hA => (X.obj _).trans ((extA_specG Kw F σ d0 hd0 hd0k inj hornIn hornOut τ hτ q z hz).1 hA)
        ((X.obj _).trans (F.app_eqv q e hA (A.P_eqv q e hA))
          ((X.obj _).symm ((extA_specG Kw F σ d0 hd0 hd0k inj hornIn hornOut τ hτ q z' hz').1 (A.P_eqv q e hA))))
    | .inr nA => match hz with
      | .inl hA => absurd hA nA
      | .inr ⟨g, hg⟩ => (X.obj _).trans ((extA_specG Kw F σ d0 hd0 hd0k inj hornIn hornOut τ hτ q z hz).2 g hg)
          ((X.obj _).symm ((extA_specG Kw F σ d0 hd0 hd0k inj hornIn hornOut τ hτ q z' hz').2 g
            ((B.obj _).trans ((B.obj _).symm e) hg)))
  nat {q' q} f z hz := match Kw.lem (A.P q z) with
    | .inl hA => (X.obj _).trans
        ((extA_specG Kw F σ d0 hd0 hd0k inj hornIn hornOut τ hτ q' _ _).1 (A.P_map f z hA))
        ((X.obj _).trans (F.nat f z hA)
          (mp_eqv X f ((X.obj _).symm ((extA_specG Kw F σ d0 hd0 hd0k inj hornIn hornOut τ hτ q z hz).1 hA))))
    | .inr nA => match hz with
      | .inl hA => absurd hA nA
      | .inr ⟨g, hg⟩ => (X.obj _).trans
          ((extA_specG Kw F σ d0 hd0 hd0k inj hornIn hornOut τ hτ q' _ _).2 (dComp f g)
            ((B.obj _).trans (mp_eqv B f hg) (mp_comp B f g σ)))
          ((X.obj _).trans ((X.obj _).symm (mp_comp X f g τ))
            (mp_eqv X f ((X.obj _).symm ((extA_specG Kw F σ d0 hd0 hd0k inj hornIn hornOut τ hτ q z hz).2 g hg))))

include Kw hd0 hd0k fillH inj hornIn hornOut in
/-- the attaching step: a partial map on A into a quasi-category extends over an inner horn
    A ∩ <σ> = Λ^(e+2)_k to A ∪ <σ> -/
theorem attachG : ∃ G : PMap (genSub A σ) X,
    ∀ q (z : (B.obj (q : Nat)).car) (hA : A.P q z), (X.obj (q : Nat)).eqv (G.app q z (.inl hA)) (F.app q z hA) :=
  match fillH (hornSG Kw F σ d0 hd0 hd0k hornOut) with
  | ⟨τ, hτ⟩ => ⟨extMapG Kw F σ d0 hd0 hd0k inj hornIn hornOut τ hτ,
      fun q z hA => (extA_specG Kw F σ d0 hd0 hd0k inj hornIn hornOut τ hτ q z (.inl hA)).1 hA⟩

end gattach

section gstep

variable (Kw : ClW.{w}) {B X : SSet.{w}} (Z0 : Sub B) (F0 : PMap Z0 X)

include Kw in
theorem ext_stepG {S S' : Sub B} (hE : Ext Z0 F0 S) (hA0 : ∀ q z, Z0.P q z → S.P q z)
    {e k : Nat} (σ : (B.obj (e + 2 : Nat)).car) (d0 : Nat) (hd0 : d0 ≤ e + 2) (hd0k : d0 ≠ k)
    (fillH : ∀ h : Horn X e k, ∃ τ, Fills h τ)
    (inj : ∀ (q : Nat) (g g' : DHom q (e + 2)), (B.obj (q : Nat)).eqv (mp B g σ) (mp B g' σ) →
      ∀ r, r ≤ q → g.f r = g'.f r)
    (hornIn : ∀ (q : Nat) (g : DHom q (e + 2)), S.P q (mp B g σ) →
      ∃ i, i ≤ e + 2 ∧ i ≠ k ∧ ∀ r, r ≤ q → g.f r ≠ i)
    (hornOut : ∀ i, i ≤ e + 2 → i ≠ k → S.P (e + 1) (dd B i σ))
    (hincl : ∀ q z, S'.P q z → (genSub S σ).P q z) : Ext Z0 F0 S' :=
  match hE with
  | ⟨G, hG⟩ => match attachG Kw G σ d0 hd0 hd0k fillH inj hornIn hornOut with
    | ⟨G1, h1⟩ => ⟨pres G1 hincl, fun q z h0 _ =>
        (X.obj _).trans (G1.app_eqv q ((B.obj _).refl z) _ (.inl (hA0 q z h0)))
          ((X.obj _).trans (h1 q z (hA0 q z h0)) (hG q z h0 (hA0 q z h0)))⟩

end gstep

end IC

end Found

#print axioms Found.IC.hornSG
#print axioms Found.IC.extMapG
#print axioms Found.IC.attachG
#print axioms Found.IC.ext_stepG

found/IC34.lean

lean_all/found/IC34.lean

/-
  T153 ((infinity,1)-categories), part 34. found/IC34 — the prism
  Delta^1 x Delta^n for Kan complexes: {0} x Delta^n ∪ Delta^1 x ∂Delta^n
  ⊂ Delta^1 x Delta^n is anodyne, in the form: a Kan complex X extends every
  map from the first to the second (prism1_ext).  The simplices
  sj c = <(0,0)..(0,c),(1,c)..(1,n)> are attached for c = n, n-1, ..., 0,
  each along its horn at the vertex (0,c) (an outer horn when c = 0).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.IC33

namespace Found

namespace IC

open Simp

universe w

/-- the prism Delta^1 x Delta^n -/
abbrev Pr1 (n : Nat) : SSet.{w} := SProd (Simp.{w} 1) (Simp.{w} n)

section p1

variable {n : Nat}

def p1U {q : Nat} (z : ((Pr1.{w} n).obj (q : Nat)).car) : DHom q 1 := z.1.down
def p1V {q : Nat} (z : ((Pr1.{w} n).obj (q : Nat)).car) : DHom q n := z.2.down

def SubV1 (Q : Nat → Nat → Prop) : Sub (Pr1.{w} n) where
  P q z := ∀ r, r ≤ q → Q ((p1U z).f r) ((p1V z).f r)
  P_eqv _ {_ _} e h r hr := subst2 (e.1 r hr) (e.2 r hr) (h r hr)
  P_map g _ h r hr := h (g.f r) (g.bound r hr)

/-- {0} x Delta^n ∪ Delta^1 x ∂Delta^n -/
def A1 : Sub (Pr1.{w} n) :=
  sOr (SubV1 (fun x _ => x ≠ 1)) (sEx (ι := Nat) (fun y => y ≤ n) (fun y => SubV1 (fun _ v => v ≠ y)))

end p1

def stD (c d : Nat) : DHom d 1 := ⟨stp c, fun _ _ => stp_mono c (Nat.le_succ _), fun i _ => stp_le1 c i⟩

/-- the staircase <(0,0)..(0,c),(1,c)..(1,n)> -/
def sj {n : Nat} (c : Nat) (hc : c ≤ n) : ((Pr1.{w} n).obj (n + 1 : Nat)).car := (⟨stD c (n + 1)⟩, ⟨degen n c hc⟩)

def memJ (c x y : Nat) : Prop := (x = 0 ∧ y ≤ c) ∨ (x = 1 ∧ c ≤ y)

theorem sj_cases {c : Nat} {P : Nat → Prop} (h0 : ∀ i, i ≤ c → P i) (h1 : ∀ y, c ≤ y → P (y + 1)) : ∀ i, P i
  | 0 => h0 0 (Nat.zero_le c)
  | y + 1 => match Nat.lt_or_ge c (y + 1) with
    | .inr h => h0 _ h
    | .inl h => h1 y (Nat.le_of_lt_succ h)

theorem sj_at {c x y : Nat} (h : memJ c x y) : stp c (y + x) = x ∧ dg c (y + x) = y :=
  match h with
  | .inl ⟨hx, hy⟩ => by cases hx; exact ⟨stp_lo hy, dg_0 hy⟩
  | .inr ⟨hx, hy⟩ => by cases hx; exact ⟨stp_hi (Nat.lt_succ_of_le hy), dg_1 hy⟩

theorem sj_idx (c i : Nat) : dg c i + stp c i = i :=
  sj_cases (c := c) (P := fun i => dg c i + stp c i = i)
    (fun i h => by rw [dg_0 h, stp_lo h]; exact rfl)
    (fun y h => by rw [dg_1 h, stp_hi (Nat.lt_succ_of_le h)]) i

theorem sj_inj {n c : Nat} (hc : c ≤ n) (q : Nat) (g g' : DHom q (n + 1))
    (e : ((Pr1.{w} n).obj (q : Nat)).eqv (mp (Pr1.{w} n) g (sj c hc)) (mp (Pr1.{w} n) g' (sj c hc))) :
    ∀ r, r ≤ q → g.f r = g'.f r := fun r hr =>
  (sj_idx c (g.f r)).symm.trans
    ((congrArg (fun y => y + stp c (g.f r)) (e.2 r hr : dg c (g.f r) = dg c (g'.f r))).trans
      ((congrArg (fun x => dg c (g'.f r) + x) (e.1 r hr : stp c (g.f r) = stp c (g'.f r))).trans (sj_idx c (g'.f r))))

theorem sj_gen {n c : Nat} (hc : c ≤ n) {q : Nat} (z : ((Pr1.{w} n).obj (q : Nat)).car)
    (h : (SubV1 (n := n) (memJ c)).P q z) :
    ∃ g : DHom q (n + 1), ((Pr1.{w} n).obj (q : Nat)).eqv z (mp (Pr1.{w} n) g (sj c hc)) :=
  ⟨⟨fun r => (p1V z).f r + (p1U z).f r,
    fun r hr => Nat.add_le_add ((p1V z).mono r hr) ((p1U z).mono r hr),
    fun r hr => Nat.add_le_add ((p1V z).bound r hr) ((p1U z).bound r hr)⟩,
   fun r hr => (sj_at (h r hr)).1.symm, fun r hr => (sj_at (h r hr)).2.symm⟩

/-- the stage: A1 and the staircases sj j with j >= c -/
def St (n c : Nat) : Sub (Pr1.{w} n) :=
  sOr A1 (sEx (ι := Nat) (fun j => c ≤ j ∧ j ≤ n) (fun j => SubV1 (memJ j)))

section jstep

variable {m c : Nat} (hc : c ≤ m + 1)

theorem nmemJ {j' : Nat} (h : c + 1 ≤ j') : ¬ memJ j' (stp c (c + 1)) (dg c (c + 1)) := fun hm => by
  rw [stp_hi (Nat.lt_succ_self c), dg_1 (Nat.le_refl c)] at hm
  exact match hm with
    | .inl ⟨h', _⟩ => Nat.noConfusion h'
    | .inr ⟨_, h'⟩ => Nat.not_succ_le_self c (Nat.le_trans h h')

include hc in
theorem sj_hornIn (q : Nat) (g : DHom q (m + 2))
    (h : (St.{w} (m + 1) (c + 1)).P q (mp (Pr1.{w} (m + 1)) g (sj c hc))) :
    ∃ i, i ≤ m + 2 ∧ i ≠ c ∧ ∀ r, r ≤ q → g.f r ≠ i :=
  match h with
  | .inl (.inl hu) => ⟨m + 2, Nat.le_refl _, fun e => Nat.not_succ_le_self (m + 1) (Nat.le_trans (Nat.le_of_eq e) hc),
      missV (Q := fun x _ => x ≠ 1) (U := stp c) (V := dg c) g hu
        (fun hne => hne (stp_hi (Nat.lt_succ_of_le hc)))⟩
  | .inl (.inr ⟨y, hy, hv⟩) => match Nat.lt_or_ge y c with
    | .inl hyc => ⟨y, Nat.le_succ_of_le hy, ne_of_lt' hyc,
        missV (Q := fun _ v => v ≠ y) (U := stp c) (V := dg c) g hv (fun hne => hne (dg_0 (Nat.le_of_lt hyc)))⟩
    | .inr hcy => ⟨y + 1, Nat.succ_le_succ hy, fun e => ne_of_lt' (Nat.lt_succ_of_le hcy) e.symm,
        missV (Q := fun _ v => v ≠ y) (U := stp c) (V := dg c) g hv (fun hne => hne (dg_1 hcy))⟩
  | .inr ⟨j', ⟨hj1, _⟩, hm⟩ => ⟨c + 1, Nat.succ_le_succ hc, fun e => ne_succ_self' c e.symm,
      missV (Q := memJ j') (U := stp c) (V := dg c) g hm (nmemJ hj1)⟩

theorem memJ_next {j : Nat} (hj : j ≠ c + 1) : memJ (c + 1) (stp c j) (dg c j) :=
  sj_cases (c := c) (P := fun j => j ≠ c + 1 → memJ (c + 1) (stp c j) (dg c j))
    (fun i h _ => .inl ⟨stp_lo h, by rw [dg_0 h]; exact Nat.le_succ_of_le h⟩)
    (fun y h hne => .inr ⟨stp_hi (Nat.lt_succ_of_le h), by
      rw [dg_1 h]
      exact Nat.lt_of_le_of_ne h (fun e => hne (congrArg (· + 1) e.symm))⟩) j hj

include hc in
theorem sj_hornOut (i : Nat) (hi : i ≤ m + 2) (hik : i ≠ c) :
    (St.{w} (m + 1) (c + 1)).P (m + 1) (dd (Pr1.{w} (m + 1)) i (sj c hc)) :=
  match Nat.decEq i (c + 1) with
  | isTrue he => match Nat.lt_or_ge c (m + 1) with
    | .inl hcn => .inr ⟨c + 1, ⟨Nat.le_refl _, hcn⟩, fun r _ => memJ_next (fun e => sk_ne i r (e.trans he.symm))⟩
    | .inr hnc => .inl (.inl (fun r hr => by
        show stp c (sk i r) ≠ 1
        have hr' : r < i := by rw [he]; exact Nat.lt_succ_of_le (Nat.le_trans hr hnc)
        rw [sk_lt hr', stp_lo (Nat.le_trans hr hnc)]
        exact fun e => Nat.noConfusion e))
  | isFalse hne => .inl (.inr ⟨dg c i, (degen (m + 1) c hc).bound i hi,
      fun r _ e => sk_ne i r (dg_inj_off c hik hne e)⟩)

theorem inclJ : ∀ q z, (St.{w} (m + 1) c).P q z → (genSub (St.{w} (m + 1) (c + 1)) (sj c hc)).P q z
  | _, _, .inl h0 => .inl (.inl h0)
  | _, z, .inr ⟨j', ⟨h1, h2⟩, hm⟩ => match Nat.lt_or_ge c j' with
    | .inl h => .inl (.inr ⟨j', ⟨h, h2⟩, hm⟩)
    | .inr h => .inr (sj_gen hc z (by
        have e : j' = c := Nat.le_antisymm h h1
        subst e
        exact hm))

end jstep

theorem before1 {n q : Nat} {U : DHom q 1} {V : DHom q n} {r r' : Nat} (hr : r ≤ q) (hr' : r' ≤ q)
    (h : U.f r' < U.f r) : V.f r' ≤ V.f r :=
  match Nat.lt_or_ge r' r with
  | .inl h' => dmono V (Nat.le_of_lt h') hr
  | .inr h' => absurd (dmono U h' hr') (Nat.not_le_of_gt h)

theorem cover1 {n q : Nat} (z : ((Pr1.{w} n).obj (q : Nat)).car) : (St.{w} n 0).P q z :=
  .inr ⟨mxv 0 (p1U z).f (p1V z).f q, ⟨Nat.zero_le _, mxv_le q (fun r hr _ => (p1V z).bound r hr)⟩,
    fun r hr => match Nat.lt_or_ge 0 ((p1U z).f r) with
    | .inr h0 => .inl ⟨Nat.eq_zero_of_le_zero h0, le_mxv q r hr h0⟩
    | .inl h0 => .inr ⟨Nat.le_antisymm ((p1U z).bound r hr) h0,
        mxv_le q (fun _ hr' hu' => before1 hr hr' (Nat.lt_of_le_of_lt hu' h0))⟩⟩

section run1

variable (Kw : ClW.{w}) {X : SSet.{w}} (hK : Kan X)

include Kw hK in
theorem allJ {m : Nat} (F0 : PMap (A1.{w} (n := m + 1)) X) :
    ∀ d c, c + d = m + 2 → Ext A1 F0 (St.{w} (m + 1) c)
  | 0, _, h => ext_base (fun _ _ hz => match hz with
      | .inl h0 => h0
      | .inr ⟨_, ⟨h1, h2⟩, _⟩ => absurd (Nat.le_trans (Nat.le_trans (Nat.le_of_eq h.symm) h1) h2)
          (Nat.not_succ_le_self _))
  | d + 1, c, h =>
    have hc : c ≤ m + 1 := Nat.le_trans (Nat.le_add_right c d) (Nat.le_of_eq (Nat.succ.inj h))
    ext_stepG Kw A1 F0 (allJ F0 d (c + 1) ((Nat.succ_add c d).trans h)) (fun _ _ h0 => .inl h0)
      (e := m) (k := c) (sj c hc) (m + 2) (Nat.le_refl _)
      (fun e => Nat.not_succ_le_self (m + 1) (Nat.le_trans (Nat.le_of_eq e) hc))
      (hK m c (Nat.le_succ_of_le hc)) (sj_inj.{w} hc) (sj_hornIn.{w} hc) (sj_hornOut.{w} hc) (inclJ hc)

def z00 : ((Pr1.{w} 0).obj (0 : Nat)).car := (⟨cst0 1⟩, ⟨cst0 0⟩)

include Kw hK in
/-- {0} x Delta^n ∪ Delta^1 x ∂Delta^n ⊂ Delta^1 x Delta^n: Kan complexes extend along it -/
theorem prism1_ext : ∀ (n : Nat) (F0 : PMap (A1.{w} (n := n)) X),
    ∃ φ : PNat (Pr1.{w} n) X, ∀ q (z : ((Pr1.{w} n).obj (q : Nat)).car) (h0 : (A1.{w} (n := n)).P q z),
      (X.obj (q : Nat)).eqv (φ.app q z) (F0.app q z h0)
  | 0, F0 =>
    have h00 : (A1.{w} (n := 0)).P 0 z00 := .inl (fun _ _ e => Nat.noConfusion e)
    ⟨{ app := fun q _ => mp X (cstAll q) (F0.app 0 z00 h00)
       app_eqv := fun _ {_ _} _ => (X.obj _).refl _
       nat := fun {_ _} g _ => (X.obj _).trans (mp_resp X (g := dComp g (cstAll _)) (fun _ _ => rfl) _)
         ((X.obj _).symm (mp_comp X g (cstAll _) _)) },
     fun q z h0 =>
      have hz : ((Pr1.{w} 0).obj (q : Nat)).eqv z (mp (Pr1.{w} 0) (cstAll q) z00) :=
        ⟨fun r hr => match h0 with
          | .inl hu => match Nat.lt_or_ge 0 ((p1U z).f r) with
            | .inr h => Nat.eq_zero_of_le_zero h
            | .inl h => absurd (Nat.le_antisymm ((p1U z).bound r hr) h) (hu r hr)
          | .inr ⟨_, hy, hv⟩ => absurd ((Nat.eq_zero_of_le_zero ((p1V z).bound 0 (Nat.zero_le q))).trans
              (Nat.eq_zero_of_le_zero hy).symm) (hv 0 (Nat.zero_le q)),
         fun r hr => Nat.eq_zero_of_le_zero ((p1V z).bound r hr)⟩
      (X.obj _).symm ((X.obj _).trans (F0.app_eqv q hz h0 ((A1.{w} (n := 0)).P_map (cstAll q) z00 h00))
        (F0.nat (cstAll q) z00 h00))⟩
  | m + 1, F0 => match ext_mono (S' := sTop (Pr1.{w} (m + 1))) (fun _ z _ => cover1 z)
      (allJ Kw hK F0 (m + 2) 0 (Nat.zero_add _)) with
    | ⟨G, hG⟩ => ⟨{
        app := fun q z => G.app q z trivial
        app_eqv := fun q {_ _} e => G.app_eqv q e trivial trivial
        nat := fun {_ _} g z => (X.obj _).trans (G.app_eqv _ (((Pr1.{w} (m + 1)).obj _).refl _) _ _)
          (G.nat g z trivial) },
      fun q z h0 => hG q z h0 trivial⟩

end run1

end IC

end Found

#print axioms Found.IC.sj_hornIn
#print axioms Found.IC.sj_hornOut
#print axioms Found.IC.cover1
#print axioms Found.IC.allJ
#print axioms Found.IC.prism1_ext

found/IC35.lean

lean_all/found/IC35.lean

/-
  T153 ((infinity,1)-categories), part 35. found/IC35 — extension along
  {0} x B ∪ Delta^1 x A ⊂ Delta^1 x B into a Kan complex, for every
  subcomplex A of a simplicial set B (lam1_ext): the argument of IC27–IC29
  (the simplicial set EL1 over B of prism extensions, boundary lifting by
  the canonical values and the Delta^1 prism IC34, a section by IC15) with
  Delta^1 and {0} in place of Delta^2 and Lambda^2_1.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.IC34

namespace Found

namespace IC

open Simp

universe w

/-- {0} as a condition on a map to [1] -/
def Lam1 {q : Nat} (u : DHom q 1) : Prop := ∀ r, r ≤ q → u.f r ≠ 1

theorem lam1_map {q' q : Nat} (g : DHom q' q) {u : DHom q 1} (h : Lam1 u) : Lam1 (dComp g u) :=
  fun r hr => h _ (g.bound r hr)

theorem lam1_eqv {q : Nat} {u u' : DHom q 1} (e : ∀ r, r ≤ q → u.f r = u'.f r) (h : Lam1 u) : Lam1 u' :=
  fun r hr e' => h r hr ((e r hr).trans e')

section part27

variable {B : SSet.{w}}

/-- {0} x B ∪ Delta^1 x A -/
def LA1 (A : Sub B) : Sub (SProd (Simp.{w} 1) B) where
  P q z := Lam1 z.1.down ∨ A.P q z.2
  P_eqv q {_ _} e h := h.elim (fun h => .inl (lam1_eqv e.1 h)) (fun h => .inr (A.P_eqv q e.2 h))
  P_map g _ h := h.elim (fun h => .inl (lam1_map g h)) (fun h => .inr (A.P_map g _ h))

/-- the point (u, v^* β) of Delta^1 x B -/
def pt1 {n q : Nat} (z : ((Pr1.{w} n).obj (q : Nat)).car) (β : (B.obj (n : Nat)).car) :
    ((SProd (Simp.{w} 1) B).obj (q : Nat)).car := (z.1, mp B (p1V z) β)

/-- id x g : Delta^1 x Delta^n' -> Delta^1 x Delta^n -/
def sMap1 {n' n : Nat} (g : DHom n' n) : PNat (Pr1.{w} n') (Pr1.{w} n) where
  app _ z := (z.1, ⟨dComp z.2.down g⟩)
  app_eqv _ {_ _} h := ⟨h.1, fun i hi => congrArg g.f (h.2 i hi)⟩
  nat _ _ := ⟨fun _ _ => rfl, fun _ _ => rfl⟩

variable {X : SSet.{w}} (A : Sub B) (F : PMap (LA1 A) X)

/-- an n-simplex of EL1: a simplex β of B and a map Delta^1 x Delta^n -> X agreeing with F over β -/
structure EL1 (n : Nat) : Type w where
  β : (B.obj (n : Nat)).car
  φ : PNat (Pr1.{w} n) X
  agree : ∀ q (z : ((Pr1.{w} n).obj (q : Nat)).car) (h : (LA1 A).P q (pt1 z β)),
    (X.obj (q : Nat)).eqv (φ.app q z) (F.app q (pt1 z β) h)

variable {A F}

theorem pt1_comp {n' n q : Nat} (g : DHom n' n) (z : ((Pr1.{w} n').obj (q : Nat)).car) (β : (B.obj (n : Nat)).car) :
    ((SProd (Simp.{w} 1) B).obj (q : Nat)).eqv (pt1 z (mp B g β)) (pt1 ((sMap1 g).app q z) β) :=
  ⟨fun _ _ => rfl, mp_comp B _ g β⟩

def EL1.act {n' n : Nat} (g : DHom n' n) (e : EL1 A F n) : EL1 A F n' where
  β := mp B g e.β
  φ := pcomp (sMap1 g) e.φ
  agree q z h :=
    have h' := (LA1 A).P_eqv q (pt1_comp g z e.β) h
    (X.obj _).trans (e.agree q _ h') (F.app_eqv q (((SProd (Simp.{w} 1) B).obj _).symm (pt1_comp g z e.β)) h' h)

variable (A F)

/-- the simplicial set EL1 -/
def ELS1 : SSet.{w} where
  obj := fun (n : Nat) => ⟨EL1 A F n, fun e e' => (B.obj n).eqv e.β e'.β ∧
      ∀ q (z : ((Pr1.{w} n).obj (q : Nat)).car), (X.obj (q : Nat)).eqv (e.φ.app q z) (e'.φ.app q z),
    fun _ => ⟨(B.obj n).refl _, fun _ _ => (X.obj _).refl _⟩,
    fun h => ⟨(B.obj n).symm h.1, fun q z => (X.obj _).symm (h.2 q z)⟩,
    fun h h' => ⟨(B.obj n).trans h.1 h'.1, fun q z => (X.obj _).trans (h.2 q z) (h'.2 q z)⟩⟩
  map g e := EL1.act g e
  map_eqv {_ _} _ {_ _} h := ⟨B.map_eqv _ h.1, fun q _ => h.2 q _⟩
  map_resp {_ _ _ _} e hfg := ⟨B.map_resp e.β hfg, fun q z => e.φ.app_eqv q
    ⟨fun _ _ => rfl, fun i hi => hfg _ (z.2.down.bound i hi)⟩⟩
  map_id _ e := ⟨B.map_id _ e.β, fun q _ => e.φ.app_eqv q ⟨fun _ _ => rfl, fun _ _ => rfl⟩⟩
  map_comp _ _ e := ⟨B.map_comp _ _ e.β, fun q _ => e.φ.app_eqv q ⟨fun _ _ => rfl, fun _ _ => rfl⟩⟩

/-- the projection EL1 -> B -/
def piL1 : PNat (ELS1 A F) B where
  app _ e := e.β
  app_eqv _ {_ _} h := h.1
  nat _ _ := (B.obj _).refl _

/-- the identity simplex of Delta^q, paired with u -/
def idz1 {q : Nat} (u : ((Simp.{w} 1).obj (q : Nat)).car) : ((Pr1.{w} q).obj (q : Nat)).car := (u, ⟨dId q⟩)

/-- a section of EL1 -> B gives an extension of F to Delta^1 x B -/
theorem ext_of_sec1 (s : PNat B (ELS1 A F))
    (hs : ∀ (n : Nat) (β : (B.obj (n : Nat)).car), (B.obj (n : Nat)).eqv ((s.app (n : Nat) β).β) β) :
    ∃ Φ : PNat (SProd (Simp.{w} 1) B) X, ∀ q (z : ((SProd (Simp.{w} 1) B).obj (q : Nat)).car)
      (h : (LA1 A).P q z), (X.obj (q : Nat)).eqv (Φ.app q z) (F.app q z h) :=
  ⟨{ app := fun q z => (s.app q z.2).φ.app q (idz1 z.1)
     app_eqv := fun q {_ z'} e => (X.obj _).trans ((s.app_eqv q e.2).2 q _)
       ((s.app q z'.2).φ.app_eqv q ⟨e.1, fun _ _ => rfl⟩)
     nat := fun {_ _} g z => (X.obj _).trans ((s.nat g z.2).2 _ _)
       ((X.obj _).trans ((s.app _ z.2).φ.app_eqv _ ⟨fun _ _ => rfl, fun _ _ => rfl⟩)
         ((s.app _ z.2).φ.nat g (idz1 z.1))) },
   fun q z h =>
    have e1 : ((SProd (Simp.{w} 1) B).obj (q : Nat)).eqv z (pt1 (idz1 z.1) ((s.app q z.2).β)) :=
      ⟨fun _ _ => rfl, (B.obj _).symm ((B.obj _).trans (mp_idl B (dId q) (fun _ _ => rfl) _) (hs q z.2))⟩
    (X.obj _).trans ((s.app q z.2).agree q (idz1 z.1) ((LA1 A).P_eqv q e1 h))
      (F.app_eqv q ((SProd (Simp.{w} 1) B).obj _ |>.symm e1) _ h)⟩



end part27

section part28

variable (Kw : ClW.{w}) (Kc : CP.Classical') {B X : SSet.{w}} {A : Sub B} {F : PMap (LA1 A) X} (hK : Kan X)

section glue

variable {M : Nat} (τ : (B.obj (M + 1 : Nat)).car) (xs : Nat → EL1 A F M)
  (hx : ∀ j, j ≤ M + 1 → (B.obj (M : Nat)).eqv (xs j).β (dd B j τ))
  (GC : ∀ q (u : DHom q 1) (w w' : DHom q M) (j j' : Nat), j < j' → j' ≤ M + 1 →
    (∀ r, r ≤ q → sk j (w.f r) = sk j' (w'.f r)) →
    (X.obj (q : Nat)).eqv ((xs j).φ.app q (⟨u⟩, ⟨w⟩)) ((xs j').φ.app q (⟨u⟩, ⟨w'⟩)))

/-- canonical values on A1: from a face, or from F on {0} x Delta^n -/
def Can1 {q : Nat} (z : ((Pr1.{w} (M + 1)).obj (q : Nat)).car) (x : (X.obj (q : Nat)).car) : Prop :=
  (∃ (j : Nat) (w : DHom q M), j ≤ M + 1 ∧ (∀ r, r ≤ q → (p1V z).f r = sk j (w.f r)) ∧
      (X.obj (q : Nat)).eqv x ((xs j).φ.app q (z.1, ⟨w⟩))) ∨
    (∃ hl : Lam1 z.1.down, (X.obj (q : Nat)).eqv x (F.app q (pt1 z τ) (.inl hl)))

include hx in
theorem pt1_face {q j : Nat} (hj : j ≤ M + 1) (z : ((Pr1.{w} (M + 1)).obj (q : Nat)).car) (w : DHom q M)
    (hw : ∀ r, r ≤ q → (p1V z).f r = sk j (w.f r)) :
    ((SProd (Simp.{w} 1) B).obj (q : Nat)).eqv (pt1 (B := B) ((z.1, ⟨w⟩) : ((Pr1.{w} M).obj (q : Nat)).car) (xs j).β)
      (pt1 z τ) :=
  ⟨fun _ _ => rfl, (B.obj _).trans (mp_eqv B w (hx j hj))
    ((B.obj _).trans (mp_comp B w (face M j) τ) (mp_resp B (fun r hr => (hw r hr).symm) τ))⟩

include hx in
theorem can1_F {q : Nat} {z : ((Pr1.{w} (M + 1)).obj (q : Nat)).car} {x : (X.obj (q : Nat)).car}
    (hc : Can1 τ xs z x) (h : (LA1 A).P q (pt1 z τ)) : (X.obj (q : Nat)).eqv x (F.app q (pt1 z τ) h) :=
  match hc with
  | .inl ⟨j, w, hj, hw, hxv⟩ =>
    have e := pt1_face τ xs hx hj z w hw
    (X.obj _).trans hxv ((X.obj _).trans ((xs j).agree q _ ((LA1 A).P_eqv q ((SProd (Simp.{w} 1) B).obj _ |>.symm e) h))
      (F.app_eqv q e _ h))
  | .inr ⟨_, hxv⟩ => (X.obj _).trans hxv (F.app_eqv q (((SProd (Simp.{w} 1) B).obj _).refl _) _ h)

include hx GC in
theorem can1_unique {q : Nat} {z : ((Pr1.{w} (M + 1)).obj (q : Nat)).car} {x y : (X.obj (q : Nat)).car}
    (hcx : Can1 τ xs z x) (hcy : Can1 τ xs z y) : (X.obj (q : Nat)).eqv x y :=
  match hcx, hcy with
  | .inl ⟨j, w, hj, hw, hxv⟩, .inl ⟨j', w', hj', hw', hyv⟩ =>
    have hww : ∀ r, r ≤ q → sk j (w.f r) = sk j' (w'.f r) := fun r hr => (hw r hr).symm.trans (hw' r hr)
    (X.obj _).trans hxv ((X.obj _).trans (match Nat.lt_or_ge j j' with
      | .inl h => GC q z.1.down w w' j j' h hj' hww
      | .inr h => match Nat.lt_or_ge j' j with
        | .inl h' => (X.obj _).symm (GC q z.1.down w' w j' j h' hj (fun r hr => (hww r hr).symm))
        | .inr h' => by
          have e : j = j' := Nat.le_antisymm h' h
          subst e
          exact (xs j).φ.app_eqv q ⟨fun _ _ => rfl, fun r hr => sk_inj (hww r hr)⟩) ((X.obj _).symm hyv))
  | .inl ⟨j, w, hj, hw, hxv⟩, .inr ⟨hl, hyv⟩ =>
    (X.obj _).trans (can1_F τ xs hx (.inl ⟨j, w, hj, hw, hxv⟩) (.inl hl)) ((X.obj _).symm hyv)
  | .inr ⟨hl, hxv⟩, .inl ⟨j, w, hj, hw, hyv⟩ =>
    (X.obj _).trans hxv ((X.obj _).symm (can1_F τ xs hx (.inl ⟨j, w, hj, hw, hyv⟩) (.inl hl)))
  | .inr ⟨_, hxv⟩, .inr ⟨_, hyv⟩ => (X.obj _).trans hxv ((X.obj _).symm hyv)

theorem can1_eqv {q : Nat} {z z' : ((Pr1.{w} (M + 1)).obj (q : Nat)).car} {x : (X.obj (q : Nat)).car}
    (e : ((Pr1.{w} (M + 1)).obj (q : Nat)).eqv z z') (hc : Can1 τ xs z x) : Can1 τ xs z' x :=
  match hc with
  | .inl ⟨j, w, hj, hw, hxv⟩ => .inl ⟨j, w, hj, fun r hr => (e.2 r hr).symm.trans (hw r hr),
      (X.obj _).trans hxv ((xs j).φ.app_eqv q ⟨e.1, fun _ _ => rfl⟩)⟩
  | .inr ⟨hl, hxv⟩ => .inr ⟨lam1_eqv e.1 hl, (X.obj _).trans hxv
      (F.app_eqv q ⟨e.1, mp_resp B e.2 τ⟩ _ _)⟩

theorem can1_map {q' q : Nat} (g : DHom q' q) {z : ((Pr1.{w} (M + 1)).obj (q : Nat)).car} {x : (X.obj (q : Nat)).car}
    (hc : Can1 τ xs z x) : Can1 τ xs (mp (Pr1.{w} (M + 1)) g z) (mp X g x) :=
  match hc with
  | .inl ⟨j, w, hj, hw, hxv⟩ => .inl ⟨j, dComp g w, hj, fun r hr => hw (g.f r) (g.bound r hr),
      (X.obj _).trans (mp_eqv X g hxv) ((X.obj _).symm ((xs j).φ.nat g (z.1, ⟨w⟩)))⟩
  | .inr ⟨hl, hxv⟩ => .inr ⟨lam1_map g hl, (X.obj _).trans (mp_eqv X g hxv)
      ((X.obj _).trans ((X.obj _).symm (F.nat g _ _)) (F.app_eqv q' (z := mp (SProd (Simp.{w} 1) B) g (pt1 z τ)) (z' := pt1 (mp (Pr1.{w} (M + 1)) g z) τ)
        ⟨fun _ _ => rfl, mp_comp B g (p1V z) τ⟩ _ _))⟩

theorem can1_ex {q : Nat} (z : ((Pr1.{w} (M + 1)).obj (q : Nat)).car) (h : (A1.{w} (n := M + 1)).P q z) :
    ∃ x, Can1 τ xs z x :=
  match h with
  | .inl h0 => ⟨_, .inr ⟨h0, (X.obj _).refl _⟩⟩
  | .inr ⟨y, hy, hv⟩ => ⟨_, .inl ⟨y, unsk (p1V z) y hy, hy, unsk_spec (p1V z) hy hv, (X.obj _).refl _⟩⟩

/-- the partial map on A1 given by the canonical values -/
def canMap1 : PMap (A1.{w} (n := M + 1)) X where
  app _ z h := Kw.ch (Can1 τ xs z) (can1_ex τ xs z h)
  app_eqv _ {z z'} e h h' := can1_unique τ xs hx GC (can1_eqv τ xs e (Kw.ch_spec (Can1 τ xs z) (can1_ex τ xs z h)))
    (Kw.ch_spec (Can1 τ xs z') (can1_ex τ xs z' h'))
  nat {_ _} g z h := can1_unique τ xs hx GC
    (Kw.ch_spec (Can1 τ xs (mp (Pr1.{w} (M + 1)) g z)) (can1_ex τ xs _ ((A1.{w} (n := M + 1)).P_map g z h)))
    (can1_map τ xs g (Kw.ch_spec (Can1 τ xs z) (can1_ex τ xs z h)))

theorem canMap1_spec {q : Nat} (z : ((Pr1.{w} (M + 1)).obj (q : Nat)).car) (h : (A1.{w} (n := M + 1)).P q z) :
    Can1 τ xs z ((canMap1 Kw τ xs hx GC).app q z h) :=
  Kw.ch_spec _ _

include Kw Kc in
/-- a simplex of Delta^(M+1) whose pullback of τ lies in A misses a vertex, unless τ lies in A -/
theorem miss_or_A1 {q : Nat} (v : DHom q (M + 1)) (hA : A.P q (mp B v τ)) (nA : ¬ A.P (M + 1) τ) :
    ∃ y, y ≤ M + 1 ∧ ∀ r, r ≤ q → v.f r ≠ y :=
  match Kw.lem (∃ y, y ≤ M + 1 ∧ ∀ r, r ≤ q → v.f r ≠ y) with
  | .inl h => h
  | .inr hn =>
    have hs : Surj v := fun j hj => match Kw.lem (∃ a, a ≤ q ∧ v.f a = j) with
      | .inl h => h
      | .inr h => absurd ⟨j, hj, fun r hr e => h ⟨r, hr, e⟩⟩ hn
    absurd (A.P_eqv (M + 1) (sec_pull Kc v hs (Nat.zero_le q) τ) (A.P_map _ _ hA)) nA

include Kw Kc hK hx GC in
/-- boundary lifting for EL1 -> B -/
theorem liftEL1 : ∃ e : EL1 A F (M + 1), (B.obj (M + 1 : Nat)).eqv e.β τ ∧
    ∀ j, j ≤ M + 1 → ((ELS1 A F).obj (M : Nat)).eqv (dd (ELS1 A F) j e) (xs j) :=
  match Kw.lem (A.P (M + 1) τ) with
  | .inl hA => ⟨⟨τ, { app := fun q z => F.app q (pt1 z τ) (.inr (A.P_map (p1V z) τ hA))
                      app_eqv := fun q {_ _} e => F.app_eqv q ⟨e.1, mp_resp B e.2 τ⟩ _ _
                      nat := fun {q' _} g z => (X.obj _).trans
                        (F.app_eqv q' (z := pt1 (mp (Pr1.{w} (M + 1)) g z) τ) (z' := mp (SProd (Simp.{w} 1) B) g (pt1 z τ))
                          ⟨fun _ _ => rfl, (B.obj _).symm (mp_comp B g (p1V z) τ)⟩ _ _)
                        (F.nat g (pt1 z τ) (.inr (A.P_map (p1V z) τ hA))) },
      fun q _ h => F.app_eqv q (((SProd (Simp.{w} 1) B).obj _).refl _) _ h⟩,
    (B.obj _).refl _,
    fun j hj => ⟨(B.obj _).symm (hx j hj), fun q z =>
      have e := pt1_face τ xs hx (z := (sMap1 (face M j)).app q z) hj z.2.down (fun _ _ => rfl)
      (X.obj _).trans (F.app_eqv q ((SProd (Simp.{w} 1) B).obj _ |>.symm e) _
          ((LA1 A).P_eqv q ((SProd (Simp.{w} 1) B).obj _ |>.symm e) (.inr (A.P_map _ τ hA))))
        ((X.obj _).symm ((xs j).agree q z _))⟩⟩
  | .inr nA => match prism1_ext Kw hK _ (canMap1 Kw τ xs hx GC) with
    | ⟨φ, hφ⟩ => ⟨⟨τ, φ, fun q z h =>
        have h0 : (A1.{w} (n := M + 1)).P q z := match Kw.lem (∃ y, y ≤ M + 1 ∧ ∀ r, r ≤ q → (p1V z).f r ≠ y) with
          | .inl hy => .inr hy
          | .inr hn => match h with
            | .inl hl => .inl hl
            | .inr hAv => absurd (miss_or_A1 Kw Kc τ (p1V z) hAv nA) hn
        (X.obj _).trans (hφ q z h0) (can1_F τ xs hx (canMap1_spec Kw τ xs hx GC z h0) h)⟩,
      (B.obj _).refl _,
      fun j hj => ⟨(B.obj _).symm (hx j hj), fun q z =>
        have h0 : (A1.{w} (n := M + 1)).P q ((sMap1 (face M j)).app q z) :=
          .inr ⟨j, hj, fun r _ => sk_ne j (z.2.down.f r)⟩
        (X.obj _).trans (hφ q _ h0) (can1_unique τ xs hx GC (canMap1_spec Kw τ xs hx GC _ h0)
          (.inl ⟨j, z.2.down, hj, fun _ _ => rfl, (X.obj _).refl _⟩))⟩⟩

end glue



end part28

section part29

variable (Kw : ClW.{w}) (Kc : CP.Classical') {B X : SSet.{w}} {A : Sub B} {F : PMap (LA1 A) X} (hK : Kan X)

/-- over a simplex of A the extension is F itself -/
def elA1 {n : Nat} (β : (B.obj (n : Nat)).car) (hA : A.P n β) : EL1 A F n :=
  ⟨β, { app := fun q z => F.app q (pt1 z β) (.inr (A.P_map (p1V z) β hA))
        app_eqv := fun q {_ _} e => F.app_eqv q ⟨e.1, mp_resp B e.2 β⟩ _ _
        nat := fun {q' _} g z => (X.obj _).trans
          (F.app_eqv q' (z := pt1 (mp (Pr1.{w} n) g z) β) (z' := mp (SProd (Simp.{w} 1) B) g (pt1 z β))
            ⟨fun _ _ => rfl, (B.obj _).symm (mp_comp B g (p1V z) β)⟩ _ _)
          (F.nat g (pt1 z β) (.inr (A.P_map (p1V z) β hA))) },
    fun q _ h => F.app_eqv q (((SProd (Simp.{w} 1) B).obj _).refl _) _ h⟩

section vertex

variable (β : (B.obj (0 : Nat)).car)

def Can10 {q : Nat} (z : ((Pr1.{w} 0).obj (q : Nat)).car) (x : (X.obj (q : Nat)).car) : Prop :=
  ∃ hl : Lam1 z.1.down, (X.obj (q : Nat)).eqv x (F.app q (pt1 z β) (.inl hl))

theorem lam1_of_A1 {q : Nat} {z : ((Pr1.{w} 0).obj (q : Nat)).car} (h : (A1.{w} (n := 0)).P q z) : Lam1 z.1.down :=
  match h with
  | .inl h0 => h0
  | .inr ⟨_, hy, hv⟩ =>
    have e0 : (p1V z).f 0 = 0 := Nat.eq_zero_of_le_zero ((p1V z).bound 0 (Nat.zero_le q))
    absurd (e0.trans (Nat.eq_zero_of_le_zero hy).symm) (hv 0 (Nat.zero_le q))

def canMap10 : PMap (A1.{w} (n := 0)) X where
  app q z h := F.app q (pt1 z β) (.inl (lam1_of_A1 h))
  app_eqv q {_ _} e _ _ := F.app_eqv q ⟨e.1, mp_resp B e.2 β⟩ _ _
  nat {q' _} g z _ := (X.obj _).trans
    (F.app_eqv q' (z := pt1 (mp (Pr1.{w} 0) g z) β) (z' := mp (SProd (Simp.{w} 1) B) g (pt1 z β))
      ⟨fun _ _ => rfl, (B.obj _).symm (mp_comp B g (p1V z) β)⟩ _ _) (F.nat g _ _)

include Kw hK in
theorem liftEL10 : ∃ e : EL1 A F 0, (B.obj (0 : Nat)).eqv e.β β :=
  match Kw.lem (A.P 0 β) with
  | .inl hA => ⟨elA1 β hA, (B.obj _).refl _⟩
  | .inr nA => match prism1_ext Kw hK _ (canMap10 β (F := F)) with
    | ⟨φ, hφ⟩ => ⟨⟨β, φ, fun q z h => match h with
        | .inl hl => (X.obj _).trans (hφ q z (.inl hl))
            (F.app_eqv q (((SProd (Simp.{w} 1) B).obj _).refl _) _ _)
        | .inr hAv =>
          have e : (B.obj (0 : Nat)).eqv (mp B (v0 q) (mp B (p1V z) β)) β :=
            (B.obj _).trans (mp_comp B _ _ β) (mp_idl B _ (fun _ hi =>
              (Nat.eq_zero_of_le_zero ((p1V z).bound 0 (Nat.zero_le q))).trans (Nat.eq_zero_of_le_zero hi).symm) β)
          absurd (A.P_eqv 0 e (A.P_map (v0 q) _ hAv)) nA⟩, (B.obj _).refl _⟩

end vertex

/-- the faces x_j of an edge of EL1 agree trivially -/
theorem gc1K (xs : Nat → EL1 A F 0) : ∀ q (u : DHom q 1) (w w' : DHom q 0) (j j' : Nat), j < j' → j' ≤ 0 + 1 →
    (∀ r, r ≤ q → sk j (w.f r) = sk j' (w'.f r)) →
    (X.obj (q : Nat)).eqv ((xs j).φ.app q (⟨u⟩, ⟨w⟩)) ((xs j').φ.app q (⟨u⟩, ⟨w'⟩))
  | q, _, w, w', j, j', hjj, hj', hw => by
    have e := hw 0 (Nat.zero_le q)
    rw [Nat.eq_zero_of_le_zero (w.bound 0 (Nat.zero_le q)), Nat.eq_zero_of_le_zero (w'.bound 0 (Nat.zero_le q))] at e
    have hj0 : j = 0 := Nat.eq_zero_of_le_zero (Nat.le_of_lt_succ (Nat.lt_of_lt_of_le hjj hj'))
    have hj1 : j' = 1 := Nat.le_antisymm hj' (Nat.lt_of_le_of_lt (Nat.zero_le j) hjj)
    subst hj0; subst hj1
    rw [sk_ge (Nat.le_refl 0), sk_lt (Nat.succ_pos 0)] at e
    exact Nat.noConfusion e

/-- the faces of a boundary of EL1 agree where they meet -/
theorem gcBK {n : Nat} (b : Bdry (ELS1 A F) n) : ∀ q (u : DHom q 1) (w w' : DHom q (n + 1)) (j j' : Nat), j < j' →
    j' ≤ n + 1 + 1 → (∀ r, r ≤ q → sk j (w.f r) = sk j' (w'.f r)) →
    (X.obj (q : Nat)).eqv ((b.x j).φ.app q (⟨u⟩, ⟨w⟩)) ((b.x j').φ.app q (⟨u⟩, ⟨w'⟩))
  | _, _, _, _, _, 0, hjj, _, _ => absurd hjj (Nat.not_lt_zero _)
  | q, u, w, w', j, J + 1, hjj, hj', hw =>
    have hjJ : j ≤ J := Nat.le_of_lt_succ hjj
    have hJ : J ≤ n + 1 := Nat.le_of_succ_le_succ hj'
    have hj : j ≤ n + 1 := Nat.le_trans hjJ hJ
    have hmiss : ∀ r, r ≤ q → w'.f r ≠ j := fun r hr e => sk_ne j (w.f r) (by
      rw [hw r hr, e, sk_lt (Nat.lt_succ_of_le hjJ)])
    have ht := unsk_spec w' hj hmiss
    have hwt : ∀ r, r ≤ q → w.f r = sk J ((unsk w' j hj).f r) := fun r hr =>
      sk_inj ((hw r hr).trans (by rw [ht r hr]; exact sk_sk hjJ _))
    let t := unsk w' j hj
    have m1 : (X.obj (q : Nat)).eqv ((b.x j).φ.app q (⟨u⟩, ⟨w⟩)) ((b.x j).φ.app q (⟨u⟩, ⟨dComp t (face n J)⟩)) :=
      (b.x j).φ.app_eqv q (x := (⟨u⟩, ⟨w⟩)) (y := (⟨u⟩, ⟨dComp t (face n J)⟩)) ⟨fun _ _ => rfl, hwt⟩
    have m2 : (X.obj (q : Nat)).eqv ((b.x j).φ.app q (⟨u⟩, ⟨dComp t (face n J)⟩))
        ((b.x (J + 1)).φ.app q (⟨u⟩, ⟨dComp t (face n j)⟩)) :=
      (X.obj _).symm ((b.compat j J hjJ hJ).2 q (⟨u⟩, ⟨t⟩))
    have m3 : (X.obj (q : Nat)).eqv ((b.x (J + 1)).φ.app q (⟨u⟩, ⟨dComp t (face n j)⟩))
        ((b.x (J + 1)).φ.app q (⟨u⟩, ⟨w'⟩)) :=
      (b.x (J + 1)).φ.app_eqv q (x := (⟨u⟩, ⟨dComp t (face n j)⟩)) (y := (⟨u⟩, ⟨w'⟩))
        ⟨fun _ _ => rfl, fun r hr => (ht r hr).symm⟩
    (X.obj _).trans m1 ((X.obj _).trans m2 m3)

include Kw Kc hK in
/-- {0} x B ∪ Delta^1 x A ⊂ Delta^1 x B: Kan complexes extend along it -/
theorem lam1_ext (A : Sub B) (F : PMap (LA1 A) X) :
    ∃ Φ : PNat (SProd (Simp.{w} 1) B) X, ∀ q (z : ((SProd (Simp.{w} 1) B).obj (q : Nat)).car)
      (h : (LA1 A).P q z), (X.obj (q : Nat)).eqv (Φ.app q z) (F.app q z h) :=
  have L0 : ∀ β : (B.obj (0 : Nat)).car, ∃ e : ((ELS1 A F).obj (0 : Nat)).car,
      (B.obj (0 : Nat)).eqv ((piL1 A F).app (0 : Nat) e) β := fun β => liftEL10 Kw hK β
  have L1 : ∀ (τ : (B.obj (1 : Nat)).car) (b0 b1 : ((ELS1 A F).obj (0 : Nat)).car),
      (B.obj (0 : Nat)).eqv ((piL1 A F).app (0 : Nat) b0) (dd B 0 τ) →
      (B.obj (0 : Nat)).eqv ((piL1 A F).app (0 : Nat) b1) (dd B 1 τ) →
      ∃ e : ((ELS1 A F).obj (1 : Nat)).car, (B.obj (1 : Nat)).eqv ((piL1 A F).app (1 : Nat) e) τ ∧
        ((ELS1 A F).obj (0 : Nat)).eqv (dd (ELS1 A F) 0 e) b0 ∧ ((ELS1 A F).obj (0 : Nat)).eqv (dd (ELS1 A F) 1 e) b1 :=
    fun τ b0 b1 h0 h1 =>
      match liftEL1 Kw Kc hK (M := 0) τ (fun j => match j with | 0 => b0 | _ + 1 => b1)
          (fun j hj => match j, hj with
            | 0, _ => h0
            | 1, _ => h1
            | _ + 2, h => absurd (Nat.le_of_succ_le_succ h) (Nat.not_succ_le_zero _))
          (gc1K _) with
      | ⟨e, he, hf⟩ => ⟨e, he, hf 0 (Nat.zero_le _), hf 1 (Nat.le_refl _)⟩
  have L2 : ∀ (n : Nat) (τ : (B.obj (n + 2 : Nat)).car) (b : Bdry (ELS1 A F) n),
      (∀ j, j ≤ n + 2 → (B.obj (n + 1 : Nat)).eqv ((piL1 A F).app (n + 1 : Nat) (b.x j)) (dd B j τ)) →
      ∃ e : ((ELS1 A F).obj (n + 2 : Nat)).car, (B.obj (n + 2 : Nat)).eqv ((piL1 A F).app (n + 2 : Nat) e) τ ∧
        BFills b e :=
    fun n τ b hb => match liftEL1 Kw Kc hK (M := n + 1) τ b.x hb (gcBK b) with
      | ⟨e, he, hf⟩ => ⟨e, he, hf⟩
  ext_of_sec1 A F (secN Kw Kc (piL1 A F) L0 L1 L2) (fun n β => secN_proj Kw Kc (piL1 A F) L0 L1 L2 n β)

end part29

end IC

end Found

#print axioms Found.IC.ELS1
#print axioms Found.IC.ext_of_sec1
#print axioms Found.IC.liftEL1
#print axioms Found.IC.liftEL10
#print axioms Found.IC.gcBK
#print axioms Found.IC.lam1_ext

found/IC36.lean

lean_all/found/IC36.lean

/-
  T153 ((infinity,1)-categories), part 36. found/IC36 — the Delta^1 prism
  for Kan complexes, other end: {1} x Delta^n ∪ Delta^1 x ∂Delta^n ⊂
  Delta^1 x Delta^n (prism1e_ext).  The staircases sj c are attached for
  c = 0, 1, ..., n, each along its horn at the vertex (1,c) (an outer horn
  when c = n).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.IC35

namespace Found

namespace IC

open Simp

universe w

/-- {1} x Delta^n ∪ Delta^1 x ∂Delta^n -/
def A1e {n : Nat} : Sub (Pr1.{w} n) :=
  sOr (SubV1 (fun x _ => x ≠ 0)) (sEx (ι := Nat) (fun y => y ≤ n) (fun y => SubV1 (fun _ v => v ≠ y)))

/-- the stage: A1e and the staircases sj j with j < c -/
def Ste (n c : Nat) : Sub (Pr1.{w} n) :=
  sOr A1e (sEx (ι := Nat) (fun j => j < c ∧ j ≤ n) (fun j => SubV1 (memJ j)))

section estep

variable {m c : Nat} (hc : c ≤ m + 1)

theorem nmemJ0 {j' : Nat} (h : j' < c) : ¬ memJ j' (stp c c) (dg c c) := fun hm => by
  rw [stp_lo (Nat.le_refl c), dg_0 (Nat.le_refl c)] at hm
  exact match hm with
    | .inl ⟨_, h'⟩ => Nat.lt_irrefl c (Nat.lt_of_le_of_lt h' h)
    | .inr ⟨h', _⟩ => Nat.noConfusion h'

include hc in
theorem sje_hornIn (q : Nat) (g : DHom q (m + 2))
    (h : (Ste.{w} (m + 1) c).P q (mp (Pr1.{w} (m + 1)) g (sj c hc))) :
    ∃ i, i ≤ m + 2 ∧ i ≠ c + 1 ∧ ∀ r, r ≤ q → g.f r ≠ i :=
  match h with
  | .inl (.inl hu) => ⟨0, Nat.zero_le _, fun e => Nat.noConfusion e,
      missV (Q := fun x _ => x ≠ 0) (U := stp c) (V := dg c) g hu
        (fun hne => hne (stp_lo (Nat.zero_le c)))⟩
  | .inl (.inr ⟨y, hy, hv⟩) => match Nat.lt_or_ge c y with
    | .inr hyc => ⟨y, Nat.le_succ_of_le hy, ne_of_lt' (Nat.lt_succ_of_le hyc),
        missV (Q := fun _ v => v ≠ y) (U := stp c) (V := dg c) g hv (fun hne => hne (dg_0 hyc))⟩
    | .inl hcy => ⟨y + 1, Nat.succ_le_succ hy, fun e => ne_of_lt' hcy (congrArg Nat.pred e).symm,
        missV (Q := fun _ v => v ≠ y) (U := stp c) (V := dg c) g hv (fun hne => hne (dg_1 (Nat.le_of_lt hcy)))⟩
  | .inr ⟨j', ⟨hj1, _⟩, hm⟩ => ⟨c, Nat.le_succ_of_le hc, ne_succ_self' c,
      missV (Q := memJ j') (U := stp c) (V := dg c) g hm (nmemJ0 hj1)⟩

theorem memJ_prev (hp : 0 < c) {j : Nat} (hj : j ≠ c) : memJ (Nat.pred c) (stp c j) (dg c j) :=
  sj_cases (c := c) (P := fun j => j ≠ c → memJ (Nat.pred c) (stp c j) (dg c j))
    (fun i h hne => .inl ⟨stp_lo h, by
      rw [dg_0 h]
      exact Nat.le_of_lt_succ (Nat.lt_of_lt_of_le (Nat.lt_of_le_of_ne h hne)
        (Nat.le_of_eq (pred_succ_of_pos hp).symm))⟩)
    (fun y h _ => .inr ⟨stp_hi (Nat.lt_succ_of_le h), by rw [dg_1 h]; exact Nat.le_trans (Nat.pred_le c) h⟩) j hj

include hc in
theorem sje_hornOut (i : Nat) (hi : i ≤ m + 2) (hik : i ≠ c + 1) :
    (Ste.{w} (m + 1) c).P (m + 1) (dd (Pr1.{w} (m + 1)) i (sj c hc)) :=
  match Nat.decEq i c with
  | isTrue he => match Nat.eq_zero_or_pos c with
    | .inl h0 => .inl (.inl (fun r _ => by
        show stp c (sk i r) ≠ 0
        rw [he, h0, sk_ge (Nat.zero_le r), stp_hi (Nat.succ_pos r)]
        exact fun e => Nat.noConfusion e))
    | .inr hp => .inr ⟨Nat.pred c, ⟨Nat.lt_of_lt_of_le (Nat.lt_succ_self _) (Nat.le_of_eq (pred_succ_of_pos hp)),
          Nat.le_trans (Nat.pred_le c) hc⟩,
        fun r _ => memJ_prev hp (fun e => sk_ne i r (e.trans he.symm))⟩
  | isFalse hne => .inl (.inr ⟨dg c i, (degen (m + 1) c hc).bound i hi,
      fun r _ e => sk_ne i r (dg_inj_off c hne hik e)⟩)

theorem inclJe : ∀ q z, (Ste.{w} (m + 1) (c + 1)).P q z → (genSub (Ste.{w} (m + 1) c) (sj c hc)).P q z
  | _, _, .inl h0 => .inl (.inl h0)
  | _, z, .inr ⟨j', ⟨h1, h2⟩, hm⟩ => match Nat.lt_or_ge j' c with
    | .inl h => .inl (.inr ⟨j', ⟨h, h2⟩, hm⟩)
    | .inr h => .inr (sj_gen hc z (by
        have e : j' = c := Nat.le_antisymm (Nat.le_of_lt_succ h1) h
        subst e
        exact hm))

end estep

theorem cover1e {n q : Nat} (z : ((Pr1.{w} n).obj (q : Nat)).car) : (Ste.{w} n (n + 1)).P q z :=
  have hb : mxv 0 (p1U z).f (p1V z).f q ≤ n := mxv_le q (fun r hr _ => (p1V z).bound r hr)
  .inr ⟨mxv 0 (p1U z).f (p1V z).f q, ⟨Nat.lt_succ_of_le hb, hb⟩,
    fun r hr => match Nat.lt_or_ge 0 ((p1U z).f r) with
    | .inr h0 => .inl ⟨Nat.eq_zero_of_le_zero h0, le_mxv q r hr h0⟩
    | .inl h0 => .inr ⟨Nat.le_antisymm ((p1U z).bound r hr) h0,
        mxv_le q (fun _ hr' hu' => before1 hr hr' (Nat.lt_of_le_of_lt hu' h0))⟩⟩

section rune

variable (Kw : ClW.{w}) {X : SSet.{w}} (hK : Kan X)

include Kw hK in
theorem allJe {m : Nat} (F0 : PMap (A1e.{w} (n := m + 1)) X) :
    ∀ c, c ≤ m + 2 → Ext A1e F0 (Ste.{w} (m + 1) c)
  | 0, _ => ext_base (fun _ _ hz => match hz with
      | .inl h0 => h0
      | .inr ⟨_, ⟨h1, _⟩, _⟩ => absurd h1 (Nat.not_lt_zero _))
  | c + 1, h =>
    have hc : c ≤ m + 1 := Nat.le_of_succ_le_succ h
    ext_stepG Kw A1e F0 (allJe F0 c (Nat.le_succ_of_le hc)) (fun _ _ h0 => .inl h0)
      (e := m) (k := c + 1) (sj c hc) 0 (Nat.zero_le _) (fun e => Nat.noConfusion e)
      (hK m (c + 1) (Nat.succ_le_succ hc)) (sj_inj.{w} hc) (sje_hornIn.{w} hc) (sje_hornOut.{w} hc) (inclJe hc)

def z10 : ((Pr1.{w} 0).obj (0 : Nat)).car := (⟨⟨fun _ => 1, fun _ h => absurd h (Nat.not_lt_zero _), fun _ _ => Nat.le_refl 1⟩⟩,
  ⟨cst0 0⟩)

include Kw hK in
/-- {1} x Delta^n ∪ Delta^1 x ∂Delta^n ⊂ Delta^1 x Delta^n: Kan complexes extend along it -/
theorem prism1e_ext : ∀ (n : Nat) (F0 : PMap (A1e.{w} (n := n)) X),
    ∃ φ : PNat (Pr1.{w} n) X, ∀ q (z : ((Pr1.{w} n).obj (q : Nat)).car) (h0 : (A1e.{w} (n := n)).P q z),
      (X.obj (q : Nat)).eqv (φ.app q z) (F0.app q z h0)
  | 0, F0 =>
    have h10 : (A1e.{w} (n := 0)).P 0 z10 := .inl (fun _ _ e => Nat.noConfusion e)
    ⟨{ app := fun q _ => mp X (cstAll q) (F0.app 0 z10 h10)
       app_eqv := fun _ {_ _} _ => (X.obj _).refl _
       nat := fun {_ _} g _ => (X.obj _).trans (mp_resp X (g := dComp g (cstAll _)) (fun _ _ => rfl) _)
         ((X.obj _).symm (mp_comp X g (cstAll _) _)) },
     fun q z h0 =>
      have hz : ((Pr1.{w} 0).obj (q : Nat)).eqv z (mp (Pr1.{w} 0) (cstAll q) z10) :=
        ⟨fun r hr => match h0 with
          | .inl hu => match Nat.lt_or_ge 0 ((p1U z).f r) with
            | .inr h => absurd (Nat.eq_zero_of_le_zero h) (hu r hr)
            | .inl h => Nat.le_antisymm ((p1U z).bound r hr) h
          | .inr ⟨_, hy, hv⟩ => absurd ((Nat.eq_zero_of_le_zero ((p1V z).bound 0 (Nat.zero_le q))).trans
              (Nat.eq_zero_of_le_zero hy).symm) (hv 0 (Nat.zero_le q)),
         fun r hr => Nat.eq_zero_of_le_zero ((p1V z).bound r hr)⟩
      (X.obj _).symm ((X.obj _).trans (F0.app_eqv q hz h0 ((A1e.{w} (n := 0)).P_map (cstAll q) z10 h10))
        (F0.nat (cstAll q) z10 h10))⟩
  | m + 1, F0 => match ext_mono (S' := sTop (Pr1.{w} (m + 1))) (fun _ z _ => cover1e z)
      (allJe Kw hK F0 (m + 2) (Nat.le_refl _)) with
    | ⟨G, hG⟩ => ⟨{
        app := fun q z => G.app q z trivial
        app_eqv := fun q {_ _} e => G.app_eqv q e trivial trivial
        nat := fun {_ _} g z => (X.obj _).trans (G.app_eqv _ (((Pr1.{w} (m + 1)).obj _).refl _) _ _)
          (G.nat g z trivial) },
      fun q z h0 => hG q z h0 trivial⟩

end rune

end IC

end Found

#print axioms Found.IC.sje_hornIn
#print axioms Found.IC.sje_hornOut
#print axioms Found.IC.allJe
#print axioms Found.IC.prism1e_ext

found/IC37.lean

lean_all/found/IC37.lean

/-
  T153 ((infinity,1)-categories), part 37. found/IC37 — extension along
  {1} x B ∪ Delta^1 x A ⊂ Delta^1 x B into a Kan complex, for every
  subcomplex A of a simplicial set B (lam1_exte): the argument of IC27–IC29
  (the simplicial set EL1e over B of prism extensions, boundary lifting by
  the canonical values and the Delta^1 prism IC36, a section by IC15) with
  Delta^1 and {1} in place of Delta^2 and Lambda^2_1.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.IC36

namespace Found

namespace IC

open Simp

universe w

/-- {1} as a condition on a map to [1] -/
def Lam1e {q : Nat} (u : DHom q 1) : Prop := ∀ r, r ≤ q → u.f r ≠ 0

theorem lam1_mape {q' q : Nat} (g : DHom q' q) {u : DHom q 1} (h : Lam1e u) : Lam1e (dComp g u) :=
  fun r hr => h _ (g.bound r hr)

theorem lam1_eqve {q : Nat} {u u' : DHom q 1} (e : ∀ r, r ≤ q → u.f r = u'.f r) (h : Lam1e u) : Lam1e u' :=
  fun r hr e' => h r hr ((e r hr).trans e')

section part27

variable {B : SSet.{w}}

/-- {1} x B ∪ Delta^1 x A -/
def LA1e (A : Sub B) : Sub (SProd (Simp.{w} 1) B) where
  P q z := Lam1e z.1.down ∨ A.P q z.2
  P_eqv q {_ _} e h := h.elim (fun h => .inl (lam1_eqve e.1 h)) (fun h => .inr (A.P_eqv q e.2 h))
  P_map g _ h := h.elim (fun h => .inl (lam1_mape g h)) (fun h => .inr (A.P_map g _ h))

/-- the point (u, v^* β) of Delta^1 x B -/
def pt1e {n q : Nat} (z : ((Pr1.{w} n).obj (q : Nat)).car) (β : (B.obj (n : Nat)).car) :
    ((SProd (Simp.{w} 1) B).obj (q : Nat)).car := (z.1, mp B (p1V z) β)

/-- id x g : Delta^1 x Delta^n' -> Delta^1 x Delta^n -/
def sMap1e {n' n : Nat} (g : DHom n' n) : PNat (Pr1.{w} n') (Pr1.{w} n) where
  app _ z := (z.1, ⟨dComp z.2.down g⟩)
  app_eqv _ {_ _} h := ⟨h.1, fun i hi => congrArg g.f (h.2 i hi)⟩
  nat _ _ := ⟨fun _ _ => rfl, fun _ _ => rfl⟩

variable {X : SSet.{w}} (A : Sub B) (F : PMap (LA1e A) X)

/-- an n-simplex of EL1e: a simplex β of B and a map Delta^1 x Delta^n -> X agreeing with F over β -/
structure EL1e (n : Nat) : Type w where
  β : (B.obj (n : Nat)).car
  φ : PNat (Pr1.{w} n) X
  agree : ∀ q (z : ((Pr1.{w} n).obj (q : Nat)).car) (h : (LA1e A).P q (pt1e z β)),
    (X.obj (q : Nat)).eqv (φ.app q z) (F.app q (pt1e z β) h)

variable {A F}

theorem pt1_compe {n' n q : Nat} (g : DHom n' n) (z : ((Pr1.{w} n').obj (q : Nat)).car) (β : (B.obj (n : Nat)).car) :
    ((SProd (Simp.{w} 1) B).obj (q : Nat)).eqv (pt1e z (mp B g β)) (pt1e ((sMap1e g).app q z) β) :=
  ⟨fun _ _ => rfl, mp_comp B _ g β⟩

def EL1e.acte {n' n : Nat} (g : DHom n' n) (e : EL1e A F n) : EL1e A F n' where
  β := mp B g e.β
  φ := pcomp (sMap1e g) e.φ
  agree q z h :=
    have h' := (LA1e A).P_eqv q (pt1_compe g z e.β) h
    (X.obj _).trans (e.agree q _ h') (F.app_eqv q (((SProd (Simp.{w} 1) B).obj _).symm (pt1_compe g z e.β)) h' h)

variable (A F)

/-- the simplicial set EL1e -/
def ELS1e : SSet.{w} where
  obj := fun (n : Nat) => ⟨EL1e A F n, fun e e' => (B.obj n).eqv e.β e'.β ∧
      ∀ q (z : ((Pr1.{w} n).obj (q : Nat)).car), (X.obj (q : Nat)).eqv (e.φ.app q z) (e'.φ.app q z),
    fun _ => ⟨(B.obj n).refl _, fun _ _ => (X.obj _).refl _⟩,
    fun h => ⟨(B.obj n).symm h.1, fun q z => (X.obj _).symm (h.2 q z)⟩,
    fun h h' => ⟨(B.obj n).trans h.1 h'.1, fun q z => (X.obj _).trans (h.2 q z) (h'.2 q z)⟩⟩
  map g e := EL1e.acte g e
  map_eqv {_ _} _ {_ _} h := ⟨B.map_eqv _ h.1, fun q _ => h.2 q _⟩
  map_resp {_ _ _ _} e hfg := ⟨B.map_resp e.β hfg, fun q z => e.φ.app_eqv q
    ⟨fun _ _ => rfl, fun i hi => hfg _ (z.2.down.bound i hi)⟩⟩
  map_id _ e := ⟨B.map_id _ e.β, fun q _ => e.φ.app_eqv q ⟨fun _ _ => rfl, fun _ _ => rfl⟩⟩
  map_comp _ _ e := ⟨B.map_comp _ _ e.β, fun q _ => e.φ.app_eqv q ⟨fun _ _ => rfl, fun _ _ => rfl⟩⟩

/-- the projection EL1e -> B -/
def piL1e : PNat (ELS1e A F) B where
  app _ e := e.β
  app_eqv _ {_ _} h := h.1
  nat _ _ := (B.obj _).refl _

/-- the identity simplex of Delta^q, paired with u -/
def idz1e {q : Nat} (u : ((Simp.{w} 1).obj (q : Nat)).car) : ((Pr1.{w} q).obj (q : Nat)).car := (u, ⟨dId q⟩)

/-- a section of EL1e -> B gives an extension of F to Delta^1 x B -/
theorem ext_of_sec1e (s : PNat B (ELS1e A F))
    (hs : ∀ (n : Nat) (β : (B.obj (n : Nat)).car), (B.obj (n : Nat)).eqv ((s.app (n : Nat) β).β) β) :
    ∃ Φ : PNat (SProd (Simp.{w} 1) B) X, ∀ q (z : ((SProd (Simp.{w} 1) B).obj (q : Nat)).car)
      (h : (LA1e A).P q z), (X.obj (q : Nat)).eqv (Φ.app q z) (F.app q z h) :=
  ⟨{ app := fun q z => (s.app q z.2).φ.app q (idz1e z.1)
     app_eqv := fun q {_ z'} e => (X.obj _).trans ((s.app_eqv q e.2).2 q _)
       ((s.app q z'.2).φ.app_eqv q ⟨e.1, fun _ _ => rfl⟩)
     nat := fun {_ _} g z => (X.obj _).trans ((s.nat g z.2).2 _ _)
       ((X.obj _).trans ((s.app _ z.2).φ.app_eqv _ ⟨fun _ _ => rfl, fun _ _ => rfl⟩)
         ((s.app _ z.2).φ.nat g (idz1e z.1))) },
   fun q z h =>
    have e1 : ((SProd (Simp.{w} 1) B).obj (q : Nat)).eqv z (pt1e (idz1e z.1) ((s.app q z.2).β)) :=
      ⟨fun _ _ => rfl, (B.obj _).symm ((B.obj _).trans (mp_idl B (dId q) (fun _ _ => rfl) _) (hs q z.2))⟩
    (X.obj _).trans ((s.app q z.2).agree q (idz1e z.1) ((LA1e A).P_eqv q e1 h))
      (F.app_eqv q ((SProd (Simp.{w} 1) B).obj _ |>.symm e1) _ h)⟩



end part27

section part28

variable (Kw : ClW.{w}) (Kc : CP.Classical') {B X : SSet.{w}} {A : Sub B} {F : PMap (LA1e A) X} (hK : Kan X)

section glue

variable {M : Nat} (τ : (B.obj (M + 1 : Nat)).car) (xs : Nat → EL1e A F M)
  (hx : ∀ j, j ≤ M + 1 → (B.obj (M : Nat)).eqv (xs j).β (dd B j τ))
  (GC : ∀ q (u : DHom q 1) (w w' : DHom q M) (j j' : Nat), j < j' → j' ≤ M + 1 →
    (∀ r, r ≤ q → sk j (w.f r) = sk j' (w'.f r)) →
    (X.obj (q : Nat)).eqv ((xs j).φ.app q (⟨u⟩, ⟨w⟩)) ((xs j').φ.app q (⟨u⟩, ⟨w'⟩)))

/-- canonical values on A1e: from a face, or from F on {1} x Delta^n -/
def Can1e {q : Nat} (z : ((Pr1.{w} (M + 1)).obj (q : Nat)).car) (x : (X.obj (q : Nat)).car) : Prop :=
  (∃ (j : Nat) (w : DHom q M), j ≤ M + 1 ∧ (∀ r, r ≤ q → (p1V z).f r = sk j (w.f r)) ∧
      (X.obj (q : Nat)).eqv x ((xs j).φ.app q (z.1, ⟨w⟩))) ∨
    (∃ hl : Lam1e z.1.down, (X.obj (q : Nat)).eqv x (F.app q (pt1e z τ) (.inl hl)))

include hx in
theorem pt1_facee {q j : Nat} (hj : j ≤ M + 1) (z : ((Pr1.{w} (M + 1)).obj (q : Nat)).car) (w : DHom q M)
    (hw : ∀ r, r ≤ q → (p1V z).f r = sk j (w.f r)) :
    ((SProd (Simp.{w} 1) B).obj (q : Nat)).eqv (pt1e (B := B) ((z.1, ⟨w⟩) : ((Pr1.{w} M).obj (q : Nat)).car) (xs j).β)
      (pt1e z τ) :=
  ⟨fun _ _ => rfl, (B.obj _).trans (mp_eqv B w (hx j hj))
    ((B.obj _).trans (mp_comp B w (face M j) τ) (mp_resp B (fun r hr => (hw r hr).symm) τ))⟩

include hx in
theorem can1_Fe {q : Nat} {z : ((Pr1.{w} (M + 1)).obj (q : Nat)).car} {x : (X.obj (q : Nat)).car}
    (hc : Can1e τ xs z x) (h : (LA1e A).P q (pt1e z τ)) : (X.obj (q : Nat)).eqv x (F.app q (pt1e z τ) h) :=
  match hc with
  | .inl ⟨j, w, hj, hw, hxv⟩ =>
    have e := pt1_facee τ xs hx hj z w hw
    (X.obj _).trans hxv ((X.obj _).trans ((xs j).agree q _ ((LA1e A).P_eqv q ((SProd (Simp.{w} 1) B).obj _ |>.symm e) h))
      (F.app_eqv q e _ h))
  | .inr ⟨_, hxv⟩ => (X.obj _).trans hxv (F.app_eqv q (((SProd (Simp.{w} 1) B).obj _).refl _) _ h)

include hx GC in
theorem can1_uniquee {q : Nat} {z : ((Pr1.{w} (M + 1)).obj (q : Nat)).car} {x y : (X.obj (q : Nat)).car}
    (hcx : Can1e τ xs z x) (hcy : Can1e τ xs z y) : (X.obj (q : Nat)).eqv x y :=
  match hcx, hcy with
  | .inl ⟨j, w, hj, hw, hxv⟩, .inl ⟨j', w', hj', hw', hyv⟩ =>
    have hww : ∀ r, r ≤ q → sk j (w.f r) = sk j' (w'.f r) := fun r hr => (hw r hr).symm.trans (hw' r hr)
    (X.obj _).trans hxv ((X.obj _).trans (match Nat.lt_or_ge j j' with
      | .inl h => GC q z.1.down w w' j j' h hj' hww
      | .inr h => match Nat.lt_or_ge j' j with
        | .inl h' => (X.obj _).symm (GC q z.1.down w' w j' j h' hj (fun r hr => (hww r hr).symm))
        | .inr h' => by
          have e : j = j' := Nat.le_antisymm h' h
          subst e
          exact (xs j).φ.app_eqv q ⟨fun _ _ => rfl, fun r hr => sk_inj (hww r hr)⟩) ((X.obj _).symm hyv))
  | .inl ⟨j, w, hj, hw, hxv⟩, .inr ⟨hl, hyv⟩ =>
    (X.obj _).trans (can1_Fe τ xs hx (.inl ⟨j, w, hj, hw, hxv⟩) (.inl hl)) ((X.obj _).symm hyv)
  | .inr ⟨hl, hxv⟩, .inl ⟨j, w, hj, hw, hyv⟩ =>
    (X.obj _).trans hxv ((X.obj _).symm (can1_Fe τ xs hx (.inl ⟨j, w, hj, hw, hyv⟩) (.inl hl)))
  | .inr ⟨_, hxv⟩, .inr ⟨_, hyv⟩ => (X.obj _).trans hxv ((X.obj _).symm hyv)

theorem can1_eqve {q : Nat} {z z' : ((Pr1.{w} (M + 1)).obj (q : Nat)).car} {x : (X.obj (q : Nat)).car}
    (e : ((Pr1.{w} (M + 1)).obj (q : Nat)).eqv z z') (hc : Can1e τ xs z x) : Can1e τ xs z' x :=
  match hc with
  | .inl ⟨j, w, hj, hw, hxv⟩ => .inl ⟨j, w, hj, fun r hr => (e.2 r hr).symm.trans (hw r hr),
      (X.obj _).trans hxv ((xs j).φ.app_eqv q ⟨e.1, fun _ _ => rfl⟩)⟩
  | .inr ⟨hl, hxv⟩ => .inr ⟨lam1_eqve e.1 hl, (X.obj _).trans hxv
      (F.app_eqv q ⟨e.1, mp_resp B e.2 τ⟩ _ _)⟩

theorem can1_mape {q' q : Nat} (g : DHom q' q) {z : ((Pr1.{w} (M + 1)).obj (q : Nat)).car} {x : (X.obj (q : Nat)).car}
    (hc : Can1e τ xs z x) : Can1e τ xs (mp (Pr1.{w} (M + 1)) g z) (mp X g x) :=
  match hc with
  | .inl ⟨j, w, hj, hw, hxv⟩ => .inl ⟨j, dComp g w, hj, fun r hr => hw (g.f r) (g.bound r hr),
      (X.obj _).trans (mp_eqv X g hxv) ((X.obj _).symm ((xs j).φ.nat g (z.1, ⟨w⟩)))⟩
  | .inr ⟨hl, hxv⟩ => .inr ⟨lam1_mape g hl, (X.obj _).trans (mp_eqv X g hxv)
      ((X.obj _).trans ((X.obj _).symm (F.nat g _ _)) (F.app_eqv q' (z := mp (SProd (Simp.{w} 1) B) g (pt1e z τ)) (z' := pt1e (mp (Pr1.{w} (M + 1)) g z) τ)
        ⟨fun _ _ => rfl, mp_comp B g (p1V z) τ⟩ _ _))⟩

theorem can1_exe {q : Nat} (z : ((Pr1.{w} (M + 1)).obj (q : Nat)).car) (h : (A1e.{w} (n := M + 1)).P q z) :
    ∃ x, Can1e τ xs z x :=
  match h with
  | .inl h0 => ⟨_, .inr ⟨h0, (X.obj _).refl _⟩⟩
  | .inr ⟨y, hy, hv⟩ => ⟨_, .inl ⟨y, unsk (p1V z) y hy, hy, unsk_spec (p1V z) hy hv, (X.obj _).refl _⟩⟩

/-- the partial map on A1e given by the canonical values -/
def canMap1e : PMap (A1e.{w} (n := M + 1)) X where
  app _ z h := Kw.ch (Can1e τ xs z) (can1_exe τ xs z h)
  app_eqv _ {z z'} e h h' := can1_uniquee τ xs hx GC (can1_eqve τ xs e (Kw.ch_spec (Can1e τ xs z) (can1_exe τ xs z h)))
    (Kw.ch_spec (Can1e τ xs z') (can1_exe τ xs z' h'))
  nat {_ _} g z h := can1_uniquee τ xs hx GC
    (Kw.ch_spec (Can1e τ xs (mp (Pr1.{w} (M + 1)) g z)) (can1_exe τ xs _ ((A1e.{w} (n := M + 1)).P_map g z h)))
    (can1_mape τ xs g (Kw.ch_spec (Can1e τ xs z) (can1_exe τ xs z h)))

theorem canMap1_spece {q : Nat} (z : ((Pr1.{w} (M + 1)).obj (q : Nat)).car) (h : (A1e.{w} (n := M + 1)).P q z) :
    Can1e τ xs z ((canMap1e Kw τ xs hx GC).app q z h) :=
  Kw.ch_spec _ _

include Kw Kc in
/-- a simplex of Delta^(M+1) whose pullback of τ lies in A misses a vertex, unless τ lies in A -/
theorem miss_or_A1e {q : Nat} (v : DHom q (M + 1)) (hA : A.P q (mp B v τ)) (nA : ¬ A.P (M + 1) τ) :
    ∃ y, y ≤ M + 1 ∧ ∀ r, r ≤ q → v.f r ≠ y :=
  match Kw.lem (∃ y, y ≤ M + 1 ∧ ∀ r, r ≤ q → v.f r ≠ y) with
  | .inl h => h
  | .inr hn =>
    have hs : Surj v := fun j hj => match Kw.lem (∃ a, a ≤ q ∧ v.f a = j) with
      | .inl h => h
      | .inr h => absurd ⟨j, hj, fun r hr e => h ⟨r, hr, e⟩⟩ hn
    absurd (A.P_eqv (M + 1) (sec_pull Kc v hs (Nat.zero_le q) τ) (A.P_map _ _ hA)) nA

include Kw Kc hK hx GC in
/-- boundary lifting for EL1e -> B -/
theorem liftEL1e : ∃ e : EL1e A F (M + 1), (B.obj (M + 1 : Nat)).eqv e.β τ ∧
    ∀ j, j ≤ M + 1 → ((ELS1e A F).obj (M : Nat)).eqv (dd (ELS1e A F) j e) (xs j) :=
  match Kw.lem (A.P (M + 1) τ) with
  | .inl hA => ⟨⟨τ, { app := fun q z => F.app q (pt1e z τ) (.inr (A.P_map (p1V z) τ hA))
                      app_eqv := fun q {_ _} e => F.app_eqv q ⟨e.1, mp_resp B e.2 τ⟩ _ _
                      nat := fun {q' _} g z => (X.obj _).trans
                        (F.app_eqv q' (z := pt1e (mp (Pr1.{w} (M + 1)) g z) τ) (z' := mp (SProd (Simp.{w} 1) B) g (pt1e z τ))
                          ⟨fun _ _ => rfl, (B.obj _).symm (mp_comp B g (p1V z) τ)⟩ _ _)
                        (F.nat g (pt1e z τ) (.inr (A.P_map (p1V z) τ hA))) },
      fun q _ h => F.app_eqv q (((SProd (Simp.{w} 1) B).obj _).refl _) _ h⟩,
    (B.obj _).refl _,
    fun j hj => ⟨(B.obj _).symm (hx j hj), fun q z =>
      have e := pt1_facee τ xs hx (z := (sMap1e (face M j)).app q z) hj z.2.down (fun _ _ => rfl)
      (X.obj _).trans (F.app_eqv q ((SProd (Simp.{w} 1) B).obj _ |>.symm e) _
          ((LA1e A).P_eqv q ((SProd (Simp.{w} 1) B).obj _ |>.symm e) (.inr (A.P_map _ τ hA))))
        ((X.obj _).symm ((xs j).agree q z _))⟩⟩
  | .inr nA => match prism1e_ext Kw hK _ (canMap1e Kw τ xs hx GC) with
    | ⟨φ, hφ⟩ => ⟨⟨τ, φ, fun q z h =>
        have h0 : (A1e.{w} (n := M + 1)).P q z := match Kw.lem (∃ y, y ≤ M + 1 ∧ ∀ r, r ≤ q → (p1V z).f r ≠ y) with
          | .inl hy => .inr hy
          | .inr hn => match h with
            | .inl hl => .inl hl
            | .inr hAv => absurd (miss_or_A1e Kw Kc τ (p1V z) hAv nA) hn
        (X.obj _).trans (hφ q z h0) (can1_Fe τ xs hx (canMap1_spece Kw τ xs hx GC z h0) h)⟩,
      (B.obj _).refl _,
      fun j hj => ⟨(B.obj _).symm (hx j hj), fun q z =>
        have h0 : (A1e.{w} (n := M + 1)).P q ((sMap1e (face M j)).app q z) :=
          .inr ⟨j, hj, fun r _ => sk_ne j (z.2.down.f r)⟩
        (X.obj _).trans (hφ q _ h0) (can1_uniquee τ xs hx GC (canMap1_spece Kw τ xs hx GC _ h0)
          (.inl ⟨j, z.2.down, hj, fun _ _ => rfl, (X.obj _).refl _⟩))⟩⟩

end glue



end part28

section part29

variable (Kw : ClW.{w}) (Kc : CP.Classical') {B X : SSet.{w}} {A : Sub B} {F : PMap (LA1e A) X} (hK : Kan X)

/-- over a simplex of A the extension is F itself -/
def elA1e {n : Nat} (β : (B.obj (n : Nat)).car) (hA : A.P n β) : EL1e A F n :=
  ⟨β, { app := fun q z => F.app q (pt1e z β) (.inr (A.P_map (p1V z) β hA))
        app_eqv := fun q {_ _} e => F.app_eqv q ⟨e.1, mp_resp B e.2 β⟩ _ _
        nat := fun {q' _} g z => (X.obj _).trans
          (F.app_eqv q' (z := pt1e (mp (Pr1.{w} n) g z) β) (z' := mp (SProd (Simp.{w} 1) B) g (pt1e z β))
            ⟨fun _ _ => rfl, (B.obj _).symm (mp_comp B g (p1V z) β)⟩ _ _)
          (F.nat g (pt1e z β) (.inr (A.P_map (p1V z) β hA))) },
    fun q _ h => F.app_eqv q (((SProd (Simp.{w} 1) B).obj _).refl _) _ h⟩

section vertex

variable (β : (B.obj (0 : Nat)).car)

def Can10e {q : Nat} (z : ((Pr1.{w} 0).obj (q : Nat)).car) (x : (X.obj (q : Nat)).car) : Prop :=
  ∃ hl : Lam1e z.1.down, (X.obj (q : Nat)).eqv x (F.app q (pt1e z β) (.inl hl))

theorem lam1_of_A1e {q : Nat} {z : ((Pr1.{w} 0).obj (q : Nat)).car} (h : (A1e.{w} (n := 0)).P q z) : Lam1e z.1.down :=
  match h with
  | .inl h0 => h0
  | .inr ⟨_, hy, hv⟩ =>
    have e0 : (p1V z).f 0 = 0 := Nat.eq_zero_of_le_zero ((p1V z).bound 0 (Nat.zero_le q))
    absurd (e0.trans (Nat.eq_zero_of_le_zero hy).symm) (hv 0 (Nat.zero_le q))

def canMap10e : PMap (A1e.{w} (n := 0)) X where
  app q z h := F.app q (pt1e z β) (.inl (lam1_of_A1e h))
  app_eqv q {_ _} e _ _ := F.app_eqv q ⟨e.1, mp_resp B e.2 β⟩ _ _
  nat {q' _} g z _ := (X.obj _).trans
    (F.app_eqv q' (z := pt1e (mp (Pr1.{w} 0) g z) β) (z' := mp (SProd (Simp.{w} 1) B) g (pt1e z β))
      ⟨fun _ _ => rfl, (B.obj _).symm (mp_comp B g (p1V z) β)⟩ _ _) (F.nat g _ _)

include Kw hK in
theorem liftEL10e : ∃ e : EL1e A F 0, (B.obj (0 : Nat)).eqv e.β β :=
  match Kw.lem (A.P 0 β) with
  | .inl hA => ⟨elA1e β hA, (B.obj _).refl _⟩
  | .inr nA => match prism1e_ext Kw hK _ (canMap10e β (F := F)) with
    | ⟨φ, hφ⟩ => ⟨⟨β, φ, fun q z h => match h with
        | .inl hl => (X.obj _).trans (hφ q z (.inl hl))
            (F.app_eqv q (((SProd (Simp.{w} 1) B).obj _).refl _) _ _)
        | .inr hAv =>
          have e : (B.obj (0 : Nat)).eqv (mp B (v0 q) (mp B (p1V z) β)) β :=
            (B.obj _).trans (mp_comp B _ _ β) (mp_idl B _ (fun _ hi =>
              (Nat.eq_zero_of_le_zero ((p1V z).bound 0 (Nat.zero_le q))).trans (Nat.eq_zero_of_le_zero hi).symm) β)
          absurd (A.P_eqv 0 e (A.P_map (v0 q) _ hAv)) nA⟩, (B.obj _).refl _⟩

end vertex

/-- the faces x_j of an edge of EL1e agree trivially -/
theorem gc1Ke (xs : Nat → EL1e A F 0) : ∀ q (u : DHom q 1) (w w' : DHom q 0) (j j' : Nat), j < j' → j' ≤ 0 + 1 →
    (∀ r, r ≤ q → sk j (w.f r) = sk j' (w'.f r)) →
    (X.obj (q : Nat)).eqv ((xs j).φ.app q (⟨u⟩, ⟨w⟩)) ((xs j').φ.app q (⟨u⟩, ⟨w'⟩))
  | q, _, w, w', j, j', hjj, hj', hw => by
    have e := hw 0 (Nat.zero_le q)
    rw [Nat.eq_zero_of_le_zero (w.bound 0 (Nat.zero_le q)), Nat.eq_zero_of_le_zero (w'.bound 0 (Nat.zero_le q))] at e
    have hj0 : j = 0 := Nat.eq_zero_of_le_zero (Nat.le_of_lt_succ (Nat.lt_of_lt_of_le hjj hj'))
    have hj1 : j' = 1 := Nat.le_antisymm hj' (Nat.lt_of_le_of_lt (Nat.zero_le j) hjj)
    subst hj0; subst hj1
    rw [sk_ge (Nat.le_refl 0), sk_lt (Nat.succ_pos 0)] at e
    exact Nat.noConfusion e

/-- the faces of a boundary of EL1e agree where they meet -/
theorem gcBKe {n : Nat} (b : Bdry (ELS1e A F) n) : ∀ q (u : DHom q 1) (w w' : DHom q (n + 1)) (j j' : Nat), j < j' →
    j' ≤ n + 1 + 1 → (∀ r, r ≤ q → sk j (w.f r) = sk j' (w'.f r)) →
    (X.obj (q : Nat)).eqv ((b.x j).φ.app q (⟨u⟩, ⟨w⟩)) ((b.x j').φ.app q (⟨u⟩, ⟨w'⟩))
  | _, _, _, _, _, 0, hjj, _, _ => absurd hjj (Nat.not_lt_zero _)
  | q, u, w, w', j, J + 1, hjj, hj', hw =>
    have hjJ : j ≤ J := Nat.le_of_lt_succ hjj
    have hJ : J ≤ n + 1 := Nat.le_of_succ_le_succ hj'
    have hj : j ≤ n + 1 := Nat.le_trans hjJ hJ
    have hmiss : ∀ r, r ≤ q → w'.f r ≠ j := fun r hr e => sk_ne j (w.f r) (by
      rw [hw r hr, e, sk_lt (Nat.lt_succ_of_le hjJ)])
    have ht := unsk_spec w' hj hmiss
    have hwt : ∀ r, r ≤ q → w.f r = sk J ((unsk w' j hj).f r) := fun r hr =>
      sk_inj ((hw r hr).trans (by rw [ht r hr]; exact sk_sk hjJ _))
    let t := unsk w' j hj
    have m1 : (X.obj (q : Nat)).eqv ((b.x j).φ.app q (⟨u⟩, ⟨w⟩)) ((b.x j).φ.app q (⟨u⟩, ⟨dComp t (face n J)⟩)) :=
      (b.x j).φ.app_eqv q (x := (⟨u⟩, ⟨w⟩)) (y := (⟨u⟩, ⟨dComp t (face n J)⟩)) ⟨fun _ _ => rfl, hwt⟩
    have m2 : (X.obj (q : Nat)).eqv ((b.x j).φ.app q (⟨u⟩, ⟨dComp t (face n J)⟩))
        ((b.x (J + 1)).φ.app q (⟨u⟩, ⟨dComp t (face n j)⟩)) :=
      (X.obj _).symm ((b.compat j J hjJ hJ).2 q (⟨u⟩, ⟨t⟩))
    have m3 : (X.obj (q : Nat)).eqv ((b.x (J + 1)).φ.app q (⟨u⟩, ⟨dComp t (face n j)⟩))
        ((b.x (J + 1)).φ.app q (⟨u⟩, ⟨w'⟩)) :=
      (b.x (J + 1)).φ.app_eqv q (x := (⟨u⟩, ⟨dComp t (face n j)⟩)) (y := (⟨u⟩, ⟨w'⟩))
        ⟨fun _ _ => rfl, fun r hr => (ht r hr).symm⟩
    (X.obj _).trans m1 ((X.obj _).trans m2 m3)

include Kw Kc hK in
/-- {1} x B ∪ Delta^1 x A ⊂ Delta^1 x B: Kan complexes extend along it -/
theorem lam1_exte (A : Sub B) (F : PMap (LA1e A) X) :
    ∃ Φ : PNat (SProd (Simp.{w} 1) B) X, ∀ q (z : ((SProd (Simp.{w} 1) B).obj (q : Nat)).car)
      (h : (LA1e A).P q z), (X.obj (q : Nat)).eqv (Φ.app q z) (F.app q z h) :=
  have L0 : ∀ β : (B.obj (0 : Nat)).car, ∃ e : ((ELS1e A F).obj (0 : Nat)).car,
      (B.obj (0 : Nat)).eqv ((piL1e A F).app (0 : Nat) e) β := fun β => liftEL10e Kw hK β
  have L1 : ∀ (τ : (B.obj (1 : Nat)).car) (b0 b1 : ((ELS1e A F).obj (0 : Nat)).car),
      (B.obj (0 : Nat)).eqv ((piL1e A F).app (0 : Nat) b0) (dd B 0 τ) →
      (B.obj (0 : Nat)).eqv ((piL1e A F).app (0 : Nat) b1) (dd B 1 τ) →
      ∃ e : ((ELS1e A F).obj (1 : Nat)).car, (B.obj (1 : Nat)).eqv ((piL1e A F).app (1 : Nat) e) τ ∧
        ((ELS1e A F).obj (0 : Nat)).eqv (dd (ELS1e A F) 0 e) b0 ∧ ((ELS1e A F).obj (0 : Nat)).eqv (dd (ELS1e A F) 1 e) b1 :=
    fun τ b0 b1 h0 h1 =>
      match liftEL1e Kw Kc hK (M := 0) τ (fun j => match j with | 0 => b0 | _ + 1 => b1)
          (fun j hj => match j, hj with
            | 0, _ => h0
            | 1, _ => h1
            | _ + 2, h => absurd (Nat.le_of_succ_le_succ h) (Nat.not_succ_le_zero _))
          (gc1Ke _) with
      | ⟨e, he, hf⟩ => ⟨e, he, hf 0 (Nat.zero_le _), hf 1 (Nat.le_refl _)⟩
  have L2 : ∀ (n : Nat) (τ : (B.obj (n + 2 : Nat)).car) (b : Bdry (ELS1e A F) n),
      (∀ j, j ≤ n + 2 → (B.obj (n + 1 : Nat)).eqv ((piL1e A F).app (n + 1 : Nat) (b.x j)) (dd B j τ)) →
      ∃ e : ((ELS1e A F).obj (n + 2 : Nat)).car, (B.obj (n + 2 : Nat)).eqv ((piL1e A F).app (n + 2 : Nat) e) τ ∧
        BFills b e :=
    fun n τ b hb => match liftEL1e Kw Kc hK (M := n + 1) τ b.x hb (gcBKe b) with
      | ⟨e, he, hf⟩ => ⟨e, he, hf⟩
  ext_of_sec1e A F (secN Kw Kc (piL1e A F) L0 L1 L2) (fun n β => secN_proj Kw Kc (piL1e A F) L0 L1 L2 n β)

end part29

end IC

end Found

#print axioms Found.IC.ELS1e
#print axioms Found.IC.ext_of_sec1e
#print axioms Found.IC.liftEL1e
#print axioms Found.IC.liftEL10e
#print axioms Found.IC.gcBKe
#print axioms Found.IC.lam1_exte

found/IC38.lean

lean_all/found/IC38.lean

/-
  T153 ((infinity,1)-categories), part 38. found/IC38 — function complexes
  into Kan complexes: if Y is a Kan complex then so is Fun(K, Y), for every
  simplicial set K (inside the classical point).  A horn Lambda^N_k x K
  (N = n + 2) is a retract of a subcomplex along which Kan complexes extend:
    k < N:  {0} x B ∪ Delta^1 x (Lambda^N_k x K), retraction
            r(0,y) = min(y,k), r(1,y) = y, section s(y) = (1,y)   (IC35);
    0 < k:  {1} x B ∪ Delta^1 x (Lambda^N_k x K), retraction
            r(0,y) = y, r(1,y) = max(y,k), section s(y) = (0,y)   (IC37),
  with B = Delta^N x K.  The horn is pulled back along r x id (IC30 glues it
  into a partial map), extended, and restricted along s x id.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.IC37

namespace Found

namespace IC

open Simp

universe w

/-! ## constant maps to [1] -/

def c1D (q : Nat) : DHom q 1 := ⟨fun _ => 1, fun _ _ => Nat.le_refl 1, fun _ _ => Nat.le_refl 1⟩
def c0D (q : Nat) : DHom q 1 := ⟨fun _ => 0, fun _ _ => Nat.le_refl 0, fun _ _ => Nat.zero_le 1⟩

theorem eq0_of_ne1 {x : Nat} (hb : x ≤ 1) (h : x ≠ 1) : x = 0 :=
  Nat.eq_zero_of_le_zero (Nat.le_of_lt_succ (Nat.lt_of_le_of_ne hb h))

theorem pos_of_ne0' {x : Nat} (h : x ≠ 0) : 1 ≤ x :=
  match x, h with
  | 0, h => absurd rfl h
  | _ + 1, _ => Nat.succ_le_succ (Nat.zero_le _)

/-! ## the lower retraction r(0,y) = min(y,k), r(1,y) = y -/

section retrA

variable {N k : Nat} (hkN : k ≤ N)

def hiA (N k x : Nat) : Nat := cond (gtb 1 x) k N

theorem hiA_0 : hiA N k 0 = k := rfl
theorem hiA_pos {x : Nat} (h : 1 ≤ x) : hiA N k x = N := congrArg (fun t => cond t k N) (le_gtb 1 x h)

include hkN in
theorem hiA_le (x : Nat) : hiA N k x ≤ N :=
  match Nat.lt_or_ge x 1 with
  | .inl h0 => by rw [Nat.eq_zero_of_le_zero (Nat.le_of_lt_succ h0), hiA_0]; exact hkN
  | .inr h1 => by rw [hiA_pos h1]; exact Nat.le_refl N

include hkN in
theorem hiA_mono {x x' : Nat} (h : x ≤ x') : hiA N k x ≤ hiA N k x' :=
  match Nat.lt_or_ge x' 1 with
  | .inl h0 => by
    rw [Nat.eq_zero_of_le_zero (Nat.le_of_lt_succ h0),
      Nat.eq_zero_of_le_zero (Nat.le_trans h (Nat.le_of_lt_succ h0))]
    exact Nat.le_refl _
  | .inr h1 => by rw [hiA_pos h1]; exact hiA_le hkN x

/-- r(x, y) = min(y, hi x) -/
def RA (N k x y : Nat) : Nat := mn2 y (hiA N k x)

include hkN in
theorem RA_mono {x x' y y' : Nat} (hx : x ≤ x') (hy : y ≤ y') : RA N k x y ≤ RA N k x' y' :=
  mn2_mono hy (hiA_mono hkN hx)

include hkN in
theorem RA_le (x y : Nat) : RA N k x y ≤ N := Nat.le_trans (mn2_le_r _ _) (hiA_le hkN x)

theorem RA_le_k (y : Nat) : RA N k 0 y ≤ k := mn2_le_r _ _

theorem RA_cases (x : Nat) {y : Nat} (hy : y ≤ N) : RA N k x y = y ∨ RA N k x y = k :=
  match mn2_cases y (hiA N k x), Nat.lt_or_ge x 1 with
  | .inl e, _ => .inl e
  | .inr e, .inl h0 => .inr (by
      have e0 : x = 0 := Nat.eq_zero_of_le_zero (Nat.le_of_lt_succ h0)
      subst e0
      exact e)
  | .inr e, .inr h1 => .inl (Nat.le_antisymm (mn2_le_l _ _)
      (Nat.le_trans hy (Nat.le_of_eq (e.trans (hiA_pos h1)).symm)))

theorem RA_s {y : Nat} (hy : y ≤ N) : RA N k 1 y = y := mn2_le hy

end retrA

def rDA {q N : Nat} (k : Nat) (hkN : k ≤ N) (u : DHom q 1) (α : DHom q N) : DHom q N :=
  ⟨fun t => RA N k (u.f t) (α.f t), fun t ht => RA_mono hkN (u.mono t ht) (α.mono t ht),
    fun _ _ => RA_le hkN _ _⟩

/-! ## the upper retraction r(0,y) = y, r(1,y) = max(y,k) -/

/-- r(x, y) = max(y, lo x) -/
def RB (k x y : Nat) : Nat := mx2 y (loR k x)

theorem RB_mono {k x x' y y' : Nat} (hx : x ≤ x') (hy : y ≤ y') : RB k x y ≤ RB k x' y' :=
  mx2_mono hy (lo_mono hx)

theorem RB_le {N k : Nat} (hkN : k ≤ N) (x : Nat) {y : Nat} (hy : y ≤ N) : RB k x y ≤ N :=
  mx2_le hy (match Nat.lt_or_ge x 1 with
    | .inl h0 => by rw [Nat.eq_zero_of_le_zero (Nat.le_of_lt_succ h0), lo_0]; exact Nat.zero_le N
    | .inr h1 => by rw [lo_pos h1]; exact hkN)

theorem RB_ge_k {k x : Nat} (y : Nat) (h : 1 ≤ x) : k ≤ RB k x y := by
  show k ≤ mx2 y (loR k x)
  rw [lo_pos h]; exact le_mx2_r _ _

theorem RB_s {k : Nat} (y : Nat) : RB k 0 y = y := by
  show mx2 y (loR k 0) = y
  rw [lo_0]
  exact match mx2_cases y 0 with
    | .inl e => e
    | .inr e => e.trans (Nat.eq_zero_of_le_zero (Nat.le_trans (le_mx2_l y 0) (Nat.le_of_eq e))).symm

theorem RB_cases {k : Nat} (x y : Nat) : RB k x y = y ∨ RB k x y = k :=
  match mx2_cases y (loR k x), Nat.lt_or_ge x 1 with
  | .inl e, _ => .inl e
  | .inr e, .inr h1 => .inr (e.trans (lo_pos h1))
  | .inr e, .inl h0 => .inl (by
      have e0 : x = 0 := Nat.eq_zero_of_le_zero (Nat.le_of_lt_succ h0)
      subst e0
      exact RB_s y)

def rDB {q N : Nat} (k : Nat) (hkN : k ≤ N) (u : DHom q 1) (α : DHom q N) : DHom q N :=
  ⟨fun t => RB k (u.f t) (α.f t), fun t ht => RB_mono (u.mono t ht) (α.mono t ht),
    fun t ht => RB_le hkN _ (α.bound t ht)⟩

/-! ## the horn pulled back along r x id -/

section funk

variable {K X : SSet.{w}} {n k : Nat} (hkN : k ≤ n + 2)

def rptA {q : Nat} (z : ((SProd (Simp.{w} 1) (SProd (Simp.{w} (n + 2)) K)).obj (q : Nat)).car) :
    ((SProd (Simp.{w} (n + 2)) K).obj (q : Nat)).car :=
  (⟨rDA k hkN z.1.down z.2.1.down⟩, z.2.2)

def rptB {q : Nat} (z : ((SProd (Simp.{w} 1) (SProd (Simp.{w} (n + 2)) K)).obj (q : Nat)).car) :
    ((SProd (Simp.{w} (n + 2)) K).obj (q : Nat)).car :=
  (⟨rDB k hkN z.1.down z.2.1.down⟩, z.2.2)

include hkN in
theorem rptA_in (hk : k < n + 2) {q : Nat}
    (z : ((SProd (Simp.{w} 1) (SProd (Simp.{w} (n + 2)) K)).obj (q : Nat)).car) (hz : (LA1 (AH K n k)).P q z) :
    (AH K n k).P q (rptA hkN z) :=
  match hz with
  | .inl h0 => ⟨n + 2, Nat.le_refl _, fun e => ne_of_lt' hk e.symm,
      fun r hr e => by
        have hx : z.1.down.f r = 0 := eq0_of_ne1 (z.1.down.bound r hr) (h0 r hr)
        have hle : RA (n + 2) k (z.1.down.f r) (z.2.1.down.f r) ≤ k := by rw [hx]; exact RA_le_k _
        exact Nat.lt_irrefl k (Nat.lt_of_lt_of_le hk (Nat.le_trans (Nat.le_of_eq e.symm) hle))⟩
  | .inr ⟨i, hi, hik, hm⟩ => ⟨i, hi, hik, fun r hr e =>
      (RA_cases (z.1.down.f r) (z.2.1.down.bound r hr)).elim (fun e' => hm r hr (e'.symm.trans e))
        (fun e' => hik (e.symm.trans e'))⟩

include hkN in
theorem rptB_in (hk0 : 0 < k) {q : Nat}
    (z : ((SProd (Simp.{w} 1) (SProd (Simp.{w} (n + 2)) K)).obj (q : Nat)).car) (hz : (LA1e (AH K n k)).P q z) :
    (AH K n k).P q (rptB hkN z) :=
  match hz with
  | .inl h1 => ⟨0, Nat.zero_le _, fun e => ne_of_lt' hk0 e,
      fun r hr e => Nat.lt_irrefl 0 (Nat.lt_of_lt_of_le hk0
        (Nat.le_trans (RB_ge_k _ (pos_of_ne0' (h1 r hr))) (Nat.le_of_eq e)))⟩
  | .inr ⟨i, hi, hik, hm⟩ => ⟨i, hi, hik, fun r hr e =>
      (RB_cases (z.1.down.f r) (z.2.1.down.f r)).elim (fun e' => hm r hr (e'.symm.trans e))
        (fun e' => hik (e.symm.trans e'))⟩

/-- the horn, pulled back along r x id (lower end) -/
def hornRA (Kw : ClW.{w}) (hk : k < n + 2) (h : Horn (FunS K X) n k) : PMap (LA1 (AH K n k)) X where
  app q z hz := (hornMap Kw h).app q (rptA hkN z) (rptA_in hkN hk z hz)
  app_eqv q {z z'} e _ _ := (hornMap Kw h).app_eqv q (z := rptA hkN z) (z' := rptA hkN z')
    ⟨fun r hr => by
      show RA (n + 2) k (z.1.down.f r) (z.2.1.down.f r) = RA (n + 2) k (z'.1.down.f r) (z'.2.1.down.f r)
      rw [e.1 r hr, e.2.1 r hr], e.2.2⟩ _ _
  nat {q' _} g z hz := (X.obj _).trans ((hornMap Kw h).app_eqv q'
      (z := rptA hkN (mp (SProd (Simp.{w} 1) (SProd (Simp.{w} (n + 2)) K)) g z))
      (z' := mp (SProd (Simp.{w} (n + 2)) K) g (rptA hkN z)) ⟨fun _ _ => rfl, (K.obj _).refl _⟩ _ _)
    ((hornMap Kw h).nat g (rptA hkN z) (rptA_in hkN hk z hz))

/-- the horn, pulled back along r x id (upper end) -/
def hornRB (Kw : ClW.{w}) (hk0 : 0 < k) (h : Horn (FunS K X) n k) : PMap (LA1e (AH K n k)) X where
  app q z hz := (hornMap Kw h).app q (rptB hkN z) (rptB_in hkN hk0 z hz)
  app_eqv q {z z'} e _ _ := (hornMap Kw h).app_eqv q (z := rptB hkN z) (z' := rptB hkN z')
    ⟨fun r hr => by
      show RB k (z.1.down.f r) (z.2.1.down.f r) = RB k (z'.1.down.f r) (z'.2.1.down.f r)
      rw [e.1 r hr, e.2.1 r hr], e.2.2⟩ _ _
  nat {q' _} g z hz := (X.obj _).trans ((hornMap Kw h).app_eqv q'
      (z := rptB hkN (mp (SProd (Simp.{w} 1) (SProd (Simp.{w} (n + 2)) K)) g z))
      (z' := mp (SProd (Simp.{w} (n + 2)) K) g (rptB hkN z)) ⟨fun _ _ => rfl, (K.obj _).refl _⟩ _ _)
    ((hornMap Kw h).nat g (rptB hkN z) (rptB_in hkN hk0 z hz))

end funk

/-- horns of Fun(K, Y) with k < n + 2 fill -/
theorem fun_kanA (Kw : ClW.{w}) (Kc : CP.Classical') {K X : SSet.{w}} (hK : Kan X) {n k : Nat}
    (hk : k < n + 2) (h : Horn (FunS K X) n k) : ∃ σ, Fills h σ :=
  have hkN : k ≤ n + 2 := Nat.le_of_lt hk
  match lam1_ext Kw Kc hK (AH K n k) (hornRA hkN Kw hk h) with
  | ⟨Φ, hΦ⟩ => ⟨{
      app := fun q z => Φ.app q (⟨c1D q⟩, z)
      app_eqv := fun q {_ _} e => Φ.app_eqv q ⟨fun _ _ => rfl, e⟩
      nat := fun {_ _} g z => (X.obj _).trans
        (Φ.app_eqv _ ⟨fun _ _ => rfl, ⟨fun _ _ => rfl, (K.obj _).refl _⟩⟩)
        (Φ.nat g (⟨c1D _⟩, z)) },
    fun i hi hik q z =>
      have hLA : (LA1 (AH K n k)).P q (⟨c1D q⟩, (⟨dComp z.1.down (face (n + 1) i)⟩, z.2)) :=
        .inr ⟨i, hi, hik, fun r _ => sk_ne i (z.1.down.f r)⟩
      (X.obj _).trans (hΦ q _ hLA)
        (canH_unique h (hornMap_spec Kw h _ (rptA_in hkN hk _ hLA))
          ⟨i, z.1.down, hi, hik, fun r hr => RA_s ((face (n + 1) i).bound _ (z.1.down.bound r hr)),
            (X.obj _).refl _⟩)⟩

/-- horns of Fun(K, Y) with 0 < k fill -/
theorem fun_kanB (Kw : ClW.{w}) (Kc : CP.Classical') {K X : SSet.{w}} (hK : Kan X) {n k : Nat}
    (hk0 : 0 < k) (hkN : k ≤ n + 2) (h : Horn (FunS K X) n k) : ∃ σ, Fills h σ :=
  match lam1_exte Kw Kc hK (AH K n k) (hornRB hkN Kw hk0 h) with
  | ⟨Φ, hΦ⟩ => ⟨{
      app := fun q z => Φ.app q (⟨c0D q⟩, z)
      app_eqv := fun q {_ _} e => Φ.app_eqv q ⟨fun _ _ => rfl, e⟩
      nat := fun {_ _} g z => (X.obj _).trans
        (Φ.app_eqv _ ⟨fun _ _ => rfl, ⟨fun _ _ => rfl, (K.obj _).refl _⟩⟩)
        (Φ.nat g (⟨c0D _⟩, z)) },
    fun i hi hik q z =>
      have hLA : (LA1e (AH K n k)).P q (⟨c0D q⟩, (⟨dComp z.1.down (face (n + 1) i)⟩, z.2)) :=
        .inr ⟨i, hi, hik, fun r _ => sk_ne i (z.1.down.f r)⟩
      (X.obj _).trans (hΦ q _ hLA)
        (canH_unique h (hornMap_spec Kw h _ (rptB_in hkN hk0 _ hLA))
          ⟨i, z.1.down, hi, hik, fun _ _ => RB_s _, (X.obj _).refl _⟩)⟩

/-- function complexes into a Kan complex are Kan complexes -/
theorem fun_kan (Kw : ClW.{w}) (Kc : CP.Classical') {K X : SSet.{w}} (hK : Kan X) : Kan (FunS K X) :=
  fun _ k hkN h => match k, hkN, h with
  | 0, _, h => fun_kanA Kw Kc hK (Nat.zero_lt_succ _) h
  | k' + 1, hkN, h => fun_kanB Kw Kc hK (Nat.zero_lt_succ k') hkN h

end IC

end Found

#print axioms Found.IC.RA_cases
#print axioms Found.IC.RB_cases
#print axioms Found.IC.rptA_in
#print axioms Found.IC.rptB_in
#print axioms Found.IC.hornRA
#print axioms Found.IC.hornRB
#print axioms Found.IC.fun_kanA
#print axioms Found.IC.fun_kanB
#print axioms Found.IC.fun_kan

found/IC39.lean

lean_all/found/IC39.lean

/-
  T153 ((infinity,1)-categories), part 39. found/IC39 — cubes for the
  homotopy coherent nerve.  The mapping space C[Delta^n](i, j) is the nerve of
  the poset of subsets of {i..j} containing i and j, a cube (Delta^1)^(i,j)
  with one coordinate for each t, i < t < j (coordinate 1 = t is in the
  subset).  A q-simplex of the cube is a family of maps [q] -> [1] indexed by
  the global vertex names t; only i < t < j are compared.  A map
  g : [m] -> [n] acts by images of subsets:
    (g_* c)_u = max { c_s : i < s < j, g s = u }   (0 if there is no s),
  and this action is functorial (cm_id, cm_comp) and respects equality
  (cm_resp, cm_eqv).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.IC38

namespace Found

namespace IC

open Simp

universe w

/-! ## finite maxima -/

/-- bor h m = max (h 0, ..., h (m-1)), 0 for m = 0 -/
def bor (h : Nat → Nat) : Nat → Nat
  | 0 => 0
  | m + 1 => mx2 (bor h m) (h m)

theorem bor_le1 {h : Nat → Nat} (hb : ∀ s, h s ≤ 1) : ∀ m, bor h m ≤ 1
  | 0 => Nat.zero_le 1
  | m + 1 => mx2_le (bor_le1 hb m) (hb m)

theorem bor_mono {h h' : Nat → Nat} (hh : ∀ s, h s ≤ h' s) : ∀ m, bor h m ≤ bor h' m
  | 0 => Nat.le_refl 0
  | m + 1 => mx2_mono (bor_mono hh m) (hh m)

theorem bor_congr {h h' : Nat → Nat} : ∀ m, (∀ s, s < m → h s = h' s) → bor h m = bor h' m
  | 0, _ => rfl
  | m + 1, hh => by
    show mx2 (bor h m) (h m) = mx2 (bor h' m) (h' m)
    rw [bor_congr m (fun s hs => hh s (Nat.lt_succ_of_lt hs)), hh m (Nat.lt_succ_self m)]

theorem le_bor {h : Nat → Nat} : ∀ m s, s < m → h s ≤ bor h m
  | 0, _, hs => absurd hs (Nat.not_lt_zero _)
  | m + 1, s, hs => match Nat.lt_or_ge s m with
    | .inl hl => Nat.le_trans (le_bor m s hl) (le_mx2_l _ _)
    | .inr hg => by
      have e : s = m := Nat.le_antisymm (Nat.le_of_lt_succ hs) hg
      rw [e]; exact le_mx2_r _ _

theorem bor_one {h : Nat → Nat} : ∀ m, bor h m = 1 → ∃ s, s < m ∧ h s = 1
  | 0, e => absurd e (fun e => Nat.noConfusion e)
  | m + 1, e => match mx2_cases (bor h m) (h m) with
    | .inl e' => match bor_one m (e'.symm.trans e) with
      | ⟨s, hs, hv⟩ => ⟨s, Nat.lt_succ_of_lt hs, hv⟩
    | .inr e' => ⟨m, Nat.lt_succ_self m, e'.symm.trans e⟩

theorem one_bor {h : Nat → Nat} (hb : ∀ s, h s ≤ 1) {m s : Nat} (hs : s < m) (hv : h s = 1) : bor h m = 1 :=
  Nat.le_antisymm (bor_le1 hb m) (Nat.le_trans (Nat.le_of_eq hv.symm) (le_bor m s hs))

/-- two values in {0, 1} that are 1 together are equal -/
theorem eq01 {a b : Nat} (ha : a ≤ 1) (hb : b ≤ 1) (h1 : a = 1 → b = 1) (h2 : b = 1 → a = 1) : a = b :=
  match a, b, ha, hb with
  | 0, 0, _, _ => rfl
  | 0, 1, _, _ => absurd (h2 rfl) (fun e => Nat.noConfusion e)
  | 1, 0, _, _ => absurd (h1 rfl) (fun e => Nat.noConfusion e)
  | 1, 1, _, _ => rfl
  | a + 2, _, ha, _ => absurd ha (fun h => Nat.not_succ_le_zero a (Nat.le_of_succ_le_succ h))
  | 0, b + 2, _, hb => absurd hb (fun h => Nat.not_succ_le_zero b (Nat.le_of_succ_le_succ h))
  | 1, b + 2, _, hb => absurd hb (fun h => Nat.not_succ_le_zero b (Nat.le_of_succ_le_succ h))

/-! ## selection of the summands -/

/-- v if i < s and x = t, else 0 -/
def selC (i t s x v : Nat) : Nat := cond (gtb s i) (cond (gtb x t) 0 (cond (gtb t x) 0 v)) 0

theorem selC_in {i t s x v : Nat} (hs : i < s) (hx : x = t) : selC i t s x v = v := by
  show cond (gtb s i) (cond (gtb x t) 0 (cond (gtb t x) 0 v)) 0 = v
  rw [lt_gtb s i hs, hx, le_gtb t t (Nat.le_refl t)]
  rfl

theorem selC_le (i t s x v : Nat) : selC i t s x v ≤ v := by
  show cond (gtb s i) (cond (gtb x t) 0 (cond (gtb t x) 0 v)) 0 ≤ v
  exact match gtb s i, gtb x t, gtb t x with
  | true, true, _ => Nat.zero_le v
  | true, false, true => Nat.zero_le v
  | true, false, false => Nat.le_refl v
  | false, _, _ => Nat.zero_le v

theorem selC_mono (i t s x : Nat) {v v' : Nat} (h : v ≤ v') : selC i t s x v ≤ selC i t s x v' := by
  show cond (gtb s i) (cond (gtb x t) 0 (cond (gtb t x) 0 v)) 0 ≤
    cond (gtb s i) (cond (gtb x t) 0 (cond (gtb t x) 0 v')) 0
  exact match gtb s i, gtb x t, gtb t x with
  | true, true, _ => Nat.le_refl 0
  | true, false, true => Nat.le_refl 0
  | true, false, false => h
  | false, _, _ => Nat.le_refl 0

theorem selC_one {i t s x v : Nat} (h : selC i t s x v = 1) : i < s ∧ x = t ∧ v = 1 := by
  revert h
  show cond (gtb s i) (cond (gtb x t) 0 (cond (gtb t x) 0 v)) 0 = 1 → i < s ∧ x = t ∧ v = 1
  exact match hsi : gtb s i, hxt : gtb x t, htx : gtb t x with
  | true, true, _ => fun e => absurd e (fun e => Nat.noConfusion e)
  | true, false, true => fun e => absurd e (fun e => Nat.noConfusion e)
  | true, false, false => fun e => ⟨gtb_t s i hsi, Nat.le_antisymm (gtb_f x t hxt) (gtb_f t x htx), e⟩
  | false, _, _ => fun e => absurd e (fun e => Nat.noConfusion e)

/-! ## the action on cubes -/

section cube

variable {m n q : Nat}

/-- the summand family at coordinate t and time r -/
def cmH (g : DHom m n) (i t : Nat) (c : Nat → DHom q 1) (r : Nat) (s : Nat) : Nat := selC i t s (g.f s) ((c s).f r)

theorem cmH_le1 (g : DHom m n) (i t : Nat) (c : Nat → DHom q 1) {r : Nat} (hr : r ≤ q) (s : Nat) :
    cmH g i t c r s ≤ 1 :=
  Nat.le_trans (selC_le _ _ _ _ _) ((c s).bound r hr)

/-- the cube map g_* on (i, j) -/
def cm (g : DHom m n) (i j : Nat) (c : Nat → DHom q 1) (t : Nat) : DHom q 1 :=
  ⟨fun r => bor (cmH g i t c r) j,
    fun r hr => bor_mono (fun s => selC_mono _ _ _ _ ((c s).mono r hr)) j,
    fun _ hr => bor_le1 (cmH_le1 g i t c hr) j⟩

theorem cm_one {g : DHom m n} {i j t : Nat} {c : Nat → DHom q 1} {r : Nat} (h : (cm g i j c t).f r = 1) :
    ∃ s, i < s ∧ s < j ∧ g.f s = t ∧ (c s).f r = 1 :=
  match bor_one j h with
  | ⟨s, hs, hv⟩ => match selC_one hv with
    | ⟨h1, h2, h3⟩ => ⟨s, h1, hs, h2, h3⟩

theorem one_cm {g : DHom m n} {i j t : Nat} {c : Nat → DHom q 1} {r : Nat} (hr : r ≤ q) {s : Nat}
    (h1 : i < s) (h2 : s < j) (h3 : g.f s = t) (h4 : (c s).f r = 1) : (cm g i j c t).f r = 1 :=
  one_bor (cmH_le1 g i t c hr) h2 ((selC_in h1 h3).trans h4)

theorem cm_le1 (g : DHom m n) (i j t : Nat) (c : Nat → DHom q 1) {r : Nat} (hr : r ≤ q) : (cm g i j c t).f r ≤ 1 :=
  (cm g i j c t).bound r hr

/-- cubes agree on the open interval (a, b) -/
def CEq (a b : Nat) (c c' : Nat → DHom q 1) : Prop := ∀ t, a < t → t < b → ∀ r, r ≤ q → (c t).f r = (c' t).f r

theorem cm_id {i j : Nat} (c : Nat → DHom q 1) : CEq i j (cm (dId n) i j c) c :=
  fun t hit htj r hr => eq01 (cm_le1 _ _ _ _ _ hr) ((c t).bound r hr)
    (fun h => match cm_one h with
      | ⟨s, _, _, e, hv⟩ => by
        have e' : s = t := e
        rw [← e']; exact hv)
    (fun h => one_cm hr hit htj rfl h)

/-- the action on cubes respects composition: (g ∘ h)_* = g_* ∘ h_* -/
theorem cm_comp {k : Nat} (h : DHom k m) (g : DHom m n) {i j : Nat} (hj : j ≤ k)
    (c : Nat → DHom q 1) :
    CEq (g.f (h.f i)) (g.f (h.f j)) (cm (dComp h g) i j c) (cm g (h.f i) (h.f j) (cm h i j c)) :=
  fun t ht1 ht2 r hr => eq01 (cm_le1 _ _ _ _ _ hr) (cm_le1 _ _ _ _ _ hr)
    (fun e => match cm_one e with
      | ⟨s, his, hsj, hgs, hv⟩ =>
        have hsk : s ≤ k := Nat.le_trans (Nat.le_of_lt hsj) hj
        have hle1 : h.f i ≤ h.f s := dmono h (Nat.le_of_lt his) hsk
        have hle2 : h.f s ≤ h.f j := dmono h (Nat.le_of_lt hsj) hj
        have hgs' : g.f (h.f s) = t := hgs
        have hn1 : h.f i ≠ h.f s := fun e' =>
          Nat.lt_irrefl t (Nat.lt_of_le_of_lt (Nat.le_of_eq ((congrArg g.f e').trans hgs').symm) ht1)
        have hn2 : h.f s ≠ h.f j := fun e' =>
          Nat.lt_irrefl t (Nat.lt_of_lt_of_le ht2 (Nat.le_of_eq ((congrArg g.f e').symm.trans hgs')))
        one_cm hr (Nat.lt_of_le_of_ne hle1 hn1) (Nat.lt_of_le_of_ne hle2 hn2) hgs (one_cm hr his hsj rfl hv))
    (fun e => match cm_one e with
      | ⟨u, _, _, hgu, hv⟩ => match cm_one hv with
        | ⟨s, his, hsj, hsu, hv'⟩ => one_cm hr his hsj (by
            show g.f (h.f s) = t
            rw [hsu]; exact hgu) hv')

/-- pointwise equal maps act equally -/
theorem cm_resp {g g' : DHom m n} (e : ∀ s, s ≤ m → g.f s = g'.f s) {i j : Nat} (hj : j ≤ m)
    (c : Nat → DHom q 1) (t : Nat) : ∀ r, (cm g i j c t).f r = (cm g' i j c t).f r :=
  fun r => bor_congr j (fun s hs => by
    show selC i t s (g.f s) ((c s).f r) = selC i t s (g'.f s) ((c s).f r)
    rw [e s (Nat.le_trans (Nat.le_of_lt hs) hj)])

/-- cubes equal on (i, j) have equal images -/
theorem cm_eqv (g : DHom m n) {i j : Nat} {c c' : Nat → DHom q 1} (e : CEq i j c c') (t : Nat) :
    ∀ r, r ≤ q → (cm g i j c t).f r = (cm g i j c' t).f r :=
  fun r hr => bor_congr j (fun s hs => match Nat.lt_or_ge i s with
    | .inl his => by
      show selC i t s (g.f s) ((c s).f r) = selC i t s (g.f s) ((c' s).f r)
      rw [e s his hs r hr]
    | .inr hsi => by
      show selC i t s (g.f s) ((c s).f r) = selC i t s (g.f s) ((c' s).f r)
      have e1 : gtb s i = false := le_gtb s i hsi
      show cond (gtb s i) (cond (gtb (g.f s) t) 0 (cond (gtb t (g.f s)) 0 ((c s).f r))) 0 =
        cond (gtb s i) (cond (gtb (g.f s) t) 0 (cond (gtb t (g.f s)) 0 ((c' s).f r))) 0
      rw [e1]; rfl)

/-- a coordinate that is 1 maps to a coordinate that is 1 -/
theorem cm_at {g : DHom m n} {i j t : Nat} {c : Nat → DHom q 1} (hit : i < t) (htj : t < j)
    {r : Nat} (hr : r ≤ q) (h : (c t).f r = 1) : (cm g i j c (g.f t)).f r = 1 :=
  one_cm hr hit htj rfl h

/-- restricting the interval: on (g t, g j) the images over (i, j) and over (t, j) agree -/
theorem cm_right {g : DHom m n} {i t j : Nat} (hit : i < t) (htj : t < j) (hj : j ≤ m) (c : Nat → DHom q 1) :
    CEq (g.f t) (g.f j) (cm g i j c) (cm g t j c) :=
  fun u hu1 _ r hr => eq01 (cm_le1 _ _ _ _ _ hr) (cm_le1 _ _ _ _ _ hr)
    (fun e => match cm_one e with
      | ⟨s, _, hsj, hgs, hv⟩ =>
        have hts : t < s := match Nat.lt_or_ge t s with
          | .inl h => h
          | .inr h => absurd (Nat.lt_of_lt_of_le hu1 (by
              rw [← hgs]; exact dmono g h (Nat.le_trans (Nat.le_of_lt htj) hj))) (Nat.lt_irrefl _)
        one_cm hr hts hsj hgs hv)
    (fun e => match cm_one e with
      | ⟨s, hts, hsj, hgs, hv⟩ => one_cm hr (Nat.lt_trans hit hts) hsj hgs hv)

/-- restricting the interval: on (g i, g t) the images over (i, j) and over (i, t) agree -/
theorem cm_left {g : DHom m n} {i t j : Nat} (htj : t < j) (hj : j ≤ m) (c : Nat → DHom q 1) :
    CEq (g.f i) (g.f t) (cm g i j c) (cm g i t c) :=
  fun u _ hu2 r hr => eq01 (cm_le1 _ _ _ _ _ hr) (cm_le1 _ _ _ _ _ hr)
    (fun e => match cm_one e with
      | ⟨s, his, hsj, hgs, hv⟩ =>
        have hst : s < t := match Nat.lt_or_ge s t with
          | .inl h => h
          | .inr h => absurd (Nat.lt_of_lt_of_le hu2 (by
              rw [← hgs]; exact dmono g h (Nat.le_trans (Nat.le_of_lt hsj) hj))) (Nat.lt_irrefl _)
        one_cm hr his hst hgs hv)
    (fun e => match cm_one e with
      | ⟨s, his, hst, hgs, hv⟩ => one_cm hr his (Nat.lt_trans hst htj) hgs hv)

end cube

/-! ## the cube as a simplicial set -/

/-- the cube (Delta^1)^(a, b) -/
def CubeS (a b : Nat) : SSet.{w} where
  obj := fun (q : Nat) => ⟨ULift.{w} (Nat → DHom q 1), fun c c' => CEq a b c.down c'.down,
    fun _ _ _ _ _ _ => rfl, fun h t h1 h2 r hr => (h t h1 h2 r hr).symm,
    fun h k t h1 h2 r hr => (h t h1 h2 r hr).trans (k t h1 h2 r hr)⟩
  map g c := ⟨fun t => dComp g (c.down t)⟩
  map_eqv {_ _} g {_ _} h := fun t h1 h2 r hr => h t h1 h2 _ (g.bound r hr)
  map_resp {_ _ _ _} c e := fun t _ _ r hr => congrArg (c.down t).f (e r hr)
  map_id _ _ := fun _ _ _ _ _ => rfl
  map_comp _ _ _ := fun _ _ _ _ _ => rfl

end IC

end Found

#print axioms Found.IC.bor_one
#print axioms Found.IC.one_bor
#print axioms Found.IC.eq01
#print axioms Found.IC.selC_one
#print axioms Found.IC.cm_id
#print axioms Found.IC.cm_comp
#print axioms Found.IC.cm_resp
#print axioms Found.IC.cm_eqv
#print axioms Found.IC.cm_right
#print axioms Found.IC.cm_left
#print axioms Found.IC.CubeS

found/IC4.lean

lean_all/found/IC4.lean

/-
  T153 ((infinity,1)-categories), part 4. found/IC4 — maps of simplicial
  sets (natural maps of setoid presheaves on the simplex category) are the
  functors of quasi-categories: they carry triangles to triangles, edges
  x -> y to edges Fx -> Fy, homotopies to homotopies, and induce a functor of
  homotopy categories h(F) : hX -> hY.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.IC3

namespace Found

namespace IC

open Simp

universe w

variable {X Y : SSet.{w}} (F : PNat X Y)

/-- F commutes with faces -/
theorem app_dd {n : Nat} (i : Nat) (x : (X.obj (n + 1 : Nat)).car) :
    (Y.obj (n : Nat)).eqv (F.app (n : Nat) (dd X i x)) (dd Y i (F.app (n + 1 : Nat) x)) :=
  F.nat (face n i) x

theorem app_idv (x : (V X).car) : (E Y).eqv (F.app (1 : Nat) (idv X x)) (idv Y (F.app (0 : Nat) x)) :=
  F.nat (degen 0 0 (Nat.le_refl 0)) x

theorem app_tri {a b c : (E X).car} (t : Tri X a b c) :
    Tri Y (F.app (1 : Nat) a) (F.app (1 : Nat) b) (F.app (1 : Nat) c) :=
  match t with
  | ⟨σ, h2, h0, h1⟩ => ⟨F.app (2 : Nat) σ,
      (E Y).trans ((E Y).symm (app_dd F 2 σ)) (F.app_eqv (1 : Nat) h2),
      (E Y).trans ((E Y).symm (app_dd F 0 σ)) (F.app_eqv (1 : Nat) h0),
      (E Y).trans ((E Y).symm (app_dd F 1 σ)) (F.app_eqv (1 : Nat) h1)⟩

/-- the image of an edge x -> y -/
def appMor {x y : (V X).car} (f : Mor x y) : Mor (F.app (0 : Nat) x) (F.app (0 : Nat) y) :=
  ⟨F.app (1 : Nat) f.1,
    (V Y).trans ((V Y).symm (app_dd F 1 f.1)) (F.app_eqv (0 : Nat) f.2.1),
    (V Y).trans ((V Y).symm (app_dd F 0 f.1)) (F.app_eqv (0 : Nat) f.2.2)⟩

variable {hQX : Quasi X} {hQY : Quasi Y}
  (cX : (h : Horn X 0 1) → { σ // Fills h σ }) (cY : (h : Horn Y 0 1) → { σ // Fills h σ })

/-- the induced functor of homotopy categories -/
def hFun (hQX : Quasi X) (hQY : Quasi Y) : EFun (hCat hQX cX) (hCat hQY cY) where
  obj x := F.app (0 : Nat) x
  map f := appMor F f
  map_resp {_ y} {_ _} h :=
    tri_resp (app_tri F h) ((E Y).refl _) (app_idv F y) ((E Y).refl _)
  map_id x :=
    tri_resp (tri_ids (F.app (0 : Nat) x)) ((E Y).symm (app_idv F x)) ((E Y).refl _) ((E Y).refl _)
  map_comp f g :=
    tri_uniq hQY (appMor F g).2.2
      (app_tri F (compS_tri cX f g)) (compS_tri cY (appMor F f) (appMor F g))

end IC

end Found

#print axioms Found.IC.app_tri
#print axioms Found.IC.hFun

found/IC40.lean

lean_all/found/IC40.lean

/-
  T153 ((infinity,1)-categories), part 40. found/IC40 — the
  infinity-category of spaces as a simplicial set: S = N^hc(Kan), the
  homotopy coherent nerve of the simplicial category of Kan complexes (of
  universe w).  An n-simplex is
    * Kan complexes X_0, ..., X_n,
    * maps phi_ij : (Delta^1)^(i,j) x X_i -> X_j (the cubes of IC39),
    * phi_ii = id, and phi_ij = phi_tj ∘ phi_it on the face c_t = 1
      (i < t < j): a simplicial functor C[Delta^n] -> Kan.
  A map g : [m] -> [n] acts by X'_i = X_(g i), phi'_ij = phi_(g i)(g j) ∘
  (g_* x id).  Two n-simplices are identified when their Kan complexes are
  equal and their maps agree after transport along these equalities.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.IC39

namespace Found

namespace IC

open Simp

universe w

/-! ## transport along equalities of simplicial sets -/

def castS {A B : SSet.{w}} (e : A = B) {q : Nat} (x : (A.obj (q : Nat)).car) : (B.obj (q : Nat)).car :=
  @Eq.rec SSet.{w} A (fun B _ => (B.obj (q : Nat)).car) x B e

/-- psi agrees with phi after transport -/
def PhiRel {C A A' B B' : SSet.{w}} (eA : A = A') (eB : B = B') (φ : PNat (SProd C A) B)
    (ψ : PNat (SProd C A') B') : Prop :=
  ∀ (q : Nat) (c : (C.obj (q : Nat)).car) (x : (A.obj (q : Nat)).car),
    (B'.obj (q : Nat)).eqv (castS eB (φ.app q (c, x))) (ψ.app q (c, castS eA x))

theorem phiRel_symm {C A A' B B' : SSet.{w}} {eA : A = A'} {eB : B = B'} {φ : PNat (SProd C A) B}
    {ψ : PNat (SProd C A') B'} (h : PhiRel eA eB φ ψ) : PhiRel eA.symm eB.symm ψ φ := by
  cases eA; cases eB
  exact fun q c x => (B.obj (q : Nat)).symm (h q c x)

theorem phiRel_trans {C A A' A'' B B' B'' : SSet.{w}} {eA : A = A'} {eB : B = B'} {eA' : A' = A''}
    {eB' : B' = B''} {φ : PNat (SProd C A) B} {ψ : PNat (SProd C A') B'} {χ : PNat (SProd C A'') B''}
    (h : PhiRel eA eB φ ψ) (h' : PhiRel eA' eB' ψ χ) : PhiRel (eA.trans eA') (eB.trans eB') φ χ := by
  cases eA; cases eB; cases eA'; cases eB'
  exact fun q c x => (B.obj (q : Nat)).trans (h q c x) (h' q c x)

/-! ## simplices of S -/

/-- an n-simplex of N^hc(Kan) -/
structure SpX (n : Nat) : Type (w + 1) where
  X : Nat → SSet.{w}
  kan : ∀ i, i ≤ n → Kan (X i)
  φ : (i j : Nat) → i ≤ j → j ≤ n → PNat (SProd (CubeS.{w} i j) (X i)) (X j)
  unit : ∀ i (hi : i ≤ n) (q : Nat) (c : ((CubeS.{w} i i).obj (q : Nat)).car) (x : ((X i).obj (q : Nat)).car),
    ((X i).obj (q : Nat)).eqv ((φ i i (Nat.le_refl i) hi).app q (c, x)) x
  comp : ∀ i t j (hit : i < t) (htj : t < j) (hj : j ≤ n) (q : Nat) (c : ((CubeS.{w} i j).obj (q : Nat)).car)
    (x : ((X i).obj (q : Nat)).car), (∀ r, r ≤ q → (c.down t).f r = 1) →
    ((X j).obj (q : Nat)).eqv ((φ i j (Nat.le_of_lt (Nat.lt_trans hit htj)) hj).app q (c, x))
      ((φ t j (Nat.le_of_lt htj) hj).app q
        (c, (φ i t (Nat.le_of_lt hit) (Nat.le_trans (Nat.le_of_lt htj) hj)).app q (c, x)))

/-- identification of n-simplices -/
def SpEq {n : Nat} (σ τ : SpX.{w} n) : Prop :=
  ∃ e : (∀ i, i ≤ n → σ.X i = τ.X i), ∀ i j (hij : i ≤ j) (hj : j ≤ n),
    PhiRel (e i (Nat.le_trans hij hj)) (e j hj) (σ.φ i j hij hj) (τ.φ i j hij hj)

theorem spEq_refl {n : Nat} (σ : SpX.{w} n) : SpEq σ σ :=
  ⟨fun _ _ => rfl, fun _ j _ _ q _ _ => ((σ.X j).obj (q : Nat)).refl _⟩

theorem spEq_symm {n : Nat} {σ τ : SpX.{w} n} (h : SpEq σ τ) : SpEq τ σ :=
  match h with
  | ⟨e, he⟩ => ⟨fun i hi => (e i hi).symm, fun i j hij hj => phiRel_symm (he i j hij hj)⟩

theorem spEq_trans {n : Nat} {σ τ υ : SpX.{w} n} (h : SpEq σ τ) (h' : SpEq τ υ) : SpEq σ υ :=
  match h, h' with
  | ⟨e, he⟩, ⟨e', he'⟩ => ⟨fun i hi => (e i hi).trans (e' i hi),
      fun i j hij hj => phiRel_trans (he i j hij hj) (he' i j hij hj)⟩

/-! ## the action of the simplex category -/

section pull

variable {m n : Nat} (g : DHom m n) (σ : SpX.{w} n)

/-- phi'_ij = phi_(g i)(g j) ∘ (g_* x id) -/
def pbPhi (i j : Nat) (hij : i ≤ j) (hj : j ≤ m) : PNat (SProd (CubeS.{w} i j) (σ.X (g.f i))) (σ.X (g.f j)) where
  app q z := (σ.φ (g.f i) (g.f j) (dmono g hij hj) (g.bound j hj)).app q (⟨cm g i j z.1.down⟩, z.2)
  app_eqv q {_ _} e := (σ.φ _ _ _ _).app_eqv q ⟨fun t _ _ r hr => cm_eqv g e.1 t r hr, e.2⟩
  nat {q' _} h z := ((σ.X (g.f j)).obj (q' : Nat)).trans
    ((σ.φ _ _ _ _).app_eqv q' ⟨fun _ _ _ _ _ => rfl, ((σ.X (g.f i)).obj (q' : Nat)).refl _⟩)
    ((σ.φ _ _ (dmono g hij hj) (g.bound j hj)).nat h (⟨cm g i j z.1.down⟩, z.2))

/-- the composition law with a degenerate first step (a = u) -/
theorem compB {a u b : Nat} (e : a = u) (hab : a ≤ b) (hub : u ≤ b) (hb : b ≤ n) {q : Nat}
    (C C' D : Nat → DHom q 1) (hC : CEq u b C C') (x : ((σ.X a).obj (q : Nat)).car) :
    ((σ.X b).obj (q : Nat)).eqv ((σ.φ a b hab hb).app q (⟨C⟩, x))
      ((σ.φ u b hub hb).app q (⟨C'⟩, (σ.φ a u (Nat.le_of_eq e) (Nat.le_trans hub hb)).app q (⟨D⟩, x))) := by
  cases e
  exact (σ.φ a b hab hb).app_eqv q ⟨hC, ((σ.X a).obj (q : Nat)).symm (σ.unit a (Nat.le_trans hab hb) q ⟨D⟩ x)⟩

/-- the composition law with a degenerate second step (u = b) -/
theorem compC {a u b : Nat} (e : u = b) (hab : a ≤ b) (hau : a ≤ u) (hb : b ≤ n) {q : Nat}
    (C D E : Nat → DHom q 1) (hC : CEq a u C D) (x : ((σ.X a).obj (q : Nat)).car) :
    ((σ.X b).obj (q : Nat)).eqv ((σ.φ a b hab hb).app q (⟨C⟩, x))
      ((σ.φ u b (Nat.le_of_eq e) hb).app q (⟨E⟩, (σ.φ a u hau (Nat.le_trans (Nat.le_of_eq e) hb)).app q (⟨D⟩, x))) := by
  cases e
  exact ((σ.X u).obj (q : Nat)).trans
    ((σ.φ a u hab hb).app_eqv q (x := (⟨C⟩, x)) (y := (⟨D⟩, x)) ⟨hC, ((σ.X a).obj (q : Nat)).refl x⟩)
    (((σ.X u).obj (q : Nat)).symm (σ.unit u hb q ⟨E⟩ _))

theorem pb_comp (i t j : Nat) (hit : i < t) (htj : t < j) (hj : j ≤ m) (q : Nat)
    (c : ((CubeS.{w} i j).obj (q : Nat)).car) (x : ((σ.X (g.f i)).obj (q : Nat)).car)
    (hc : ∀ r, r ≤ q → (c.down t).f r = 1) :
    ((σ.X (g.f j)).obj (q : Nat)).eqv ((pbPhi g σ i j (Nat.le_of_lt (Nat.lt_trans hit htj)) hj).app q (c, x))
      ((pbPhi g σ t j (Nat.le_of_lt htj) hj).app q
        (c, (pbPhi g σ i t (Nat.le_of_lt hit) (Nat.le_trans (Nat.le_of_lt htj) hj)).app q (c, x))) :=
  have htm : t ≤ m := Nat.le_trans (Nat.le_of_lt htj) hj
  have h1 : g.f i ≤ g.f t := dmono g (Nat.le_of_lt hit) htm
  have h2 : g.f t ≤ g.f j := dmono g (Nat.le_of_lt htj) hj
  match Nat.lt_or_ge (g.f i) (g.f t) with
  | .inr hge => compB σ (Nat.le_antisymm h1 hge) _ h2 (g.bound j hj) _ _ _ (cm_right hit htj hj c.down) x
  | .inl hlt => match Nat.lt_or_ge (g.f t) (g.f j) with
    | .inr hge => compC σ (Nat.le_antisymm h2 hge) _ h1 (g.bound j hj) _ _ _ (cm_left htj hj c.down) x
    | .inl hlt' => ((σ.X (g.f j)).obj (q : Nat)).trans
        (σ.comp (g.f i) (g.f t) (g.f j) hlt hlt' (g.bound j hj) q ⟨cm g i j c.down⟩ x
          (fun r hr => cm_at hit htj hr (hc r hr)))
        ((σ.φ (g.f t) (g.f j) _ _).app_eqv q ⟨cm_right hit htj hj c.down,
          (σ.φ (g.f i) (g.f t) _ _).app_eqv q ⟨cm_left htj hj c.down, ((σ.X (g.f i)).obj (q : Nat)).refl x⟩⟩)

/-- the pulled-back simplex g^* σ -/
def pbS : SpX.{w} m where
  X i := σ.X (g.f i)
  kan i hi := σ.kan _ (g.bound i hi)
  φ i j hij hj := pbPhi g σ i j hij hj
  unit i hi q c x := σ.unit (g.f i) (g.bound i hi) q ⟨cm g i i c.down⟩ x
  comp i t j hit htj hj q c x hc := pb_comp g σ i t j hit htj hj q c x hc

end pull

/-- transport of phi along equal vertices -/
theorem phi_transport {n : Nat} (σ : SpX.{w} n) {a a' b b' : Nat} (ea : a = a') (eb : b = b')
    (hab : a ≤ b) (hb : b ≤ n) (hab' : a' ≤ b') (hb' : b' ≤ n) {q : Nat}
    (C C' : Nat → DHom q 1) (hC : CEq a' b' C C') (x : ((σ.X a).obj (q : Nat)).car) :
    ((σ.X b').obj (q : Nat)).eqv (castS (congrArg σ.X eb) ((σ.φ a b hab hb).app q (⟨C⟩, x)))
      ((σ.φ a' b' hab' hb').app q (⟨C'⟩, castS (congrArg σ.X ea) x)) := by
  cases ea; cases eb
  exact (σ.φ a b hab hb).app_eqv q ⟨hC, ((σ.X a).obj (q : Nat)).refl x⟩

/-- the simplicial set S = N^hc(Kan) of spaces -/
def SpS : SSet.{w + 1} where
  obj := fun (n : Nat) => ⟨SpX.{w} n, SpEq, spEq_refl, spEq_symm, spEq_trans⟩
  map g σ := pbS g σ
  map_eqv {_ _} g {_ _} h := match h with
    | ⟨e, he⟩ => ⟨fun i hi => e (g.f i) (g.bound i hi), fun i j hij hj q c x =>
        he (g.f i) (g.f j) (dmono g hij hj) (g.bound j hj) q ⟨cm g i j c.down⟩ x⟩
  map_resp {_ _ g g'} σ e := ⟨fun i hi => congrArg σ.X (e i hi), fun i j hij hj _ c x =>
      phi_transport σ (e i (Nat.le_trans hij hj)) (e j hj) (dmono g hij hj) (g.bound j hj)
        (dmono g' hij hj) (g'.bound j hj) _ _ (fun t _ _ r _ => cm_resp e hj c.down t r) x⟩
  map_id _ σ := ⟨fun _ _ => rfl, fun i j hij hj q c x =>
      (σ.φ i j hij hj).app_eqv q ⟨cm_id c.down, ((σ.X i).obj (q : Nat)).refl x⟩⟩
  map_comp f g σ := ⟨fun _ _ => rfl, fun _ _ _ hj q c x =>
      (σ.φ _ _ _ _).app_eqv q ⟨cm_comp f g hj c.down, ((σ.X _).obj (q : Nat)).refl x⟩⟩

end IC

end Found

#print axioms Found.IC.phiRel_symm
#print axioms Found.IC.phiRel_trans
#print axioms Found.IC.compB
#print axioms Found.IC.compC
#print axioms Found.IC.pb_comp
#print axioms Found.IC.pbS
#print axioms Found.IC.phi_transport
#print axioms Found.IC.SpS

found/IC41.lean

lean_all/found/IC41.lean

/-
  T153 ((infinity,1)-categories), part 41. found/IC41 — inner horns of
  S = N^hc(Kan), I: transport lemmas, the cube maps of the coface maps
  (coordinate t becomes 0, the others are renamed), and the normal form of
  a horn: one family of Kan complexes Xv (Xv 0 from the last face, Xv (v+1)
  from face 0) such that every face of the horn has vertices Xv (sk i a),
  and faces y i rewritten over this family, identified with the given ones.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.IC40

namespace Found

namespace IC

open Simp

universe w

/-! ## transport -/

theorem castS_eqv {A B : SSet.{w}} (e : A = B) {q : Nat} {x y : (A.obj (q : Nat)).car}
    (h : (A.obj (q : Nat)).eqv x y) : (B.obj (q : Nat)).eqv (castS e x) (castS e y) := by
  cases e; exact h

theorem castS_trans {A B C : SSet.{w}} (e1 : A = B) (e2 : B = C) {q : Nat} (x : (A.obj (q : Nat)).car) :
    castS e2 (castS e1 x) = castS (e1.trans e2) x := by
  cases e1; cases e2; rfl

theorem castS_mp {A B : SSet.{w}} (e : A = B) {q' q : Nat} (g : DHom q' q) (x : (A.obj (q : Nat)).car) :
    castS e (mp A g x) = mp B g (castS e x) := by
  cases e; rfl

theorem castS_back {A B : SSet.{w}} (e : A = B) {q : Nat} (x : (A.obj (q : Nat)).car) :
    castS e.symm (castS e x) = x := by
  cases e; rfl

/-- transport of a map along equalities of source and target -/
def castPN {C A A' B B' : SSet.{w}} (eA : A = A') (eB : B = B') (φ : PNat (SProd C A) B) :
    PNat (SProd C A') B' :=
  @Eq.rec SSet.{w} A (fun A'' _ => PNat (SProd C A'') B')
    (@Eq.rec SSet.{w} B (fun B'' _ => PNat (SProd C A) B'') φ B' eB) A' eA

theorem castPN_rel {C A A' B B' : SSet.{w}} (eA : A = A') (eB : B = B') (φ : PNat (SProd C A) B) :
    PhiRel eA eB φ (castPN eA eB φ) := by
  cases eA; cases eB
  exact fun q _ _ => (B.obj (q : Nat)).refl _

theorem castPN_unit {C A A' : SSet.{w}} (e : A = A') (φ : PNat (SProd C A) A) {q : Nat}
    (c : (C.obj (q : Nat)).car) (h : ∀ x, (A.obj (q : Nat)).eqv (φ.app q (c, x)) x) :
    ∀ x, (A'.obj (q : Nat)).eqv ((castPN e e φ).app q (c, x)) x := by
  cases e; exact h

theorem castPN_comp {i t j : Nat} {A1 A2 A3 B1 B2 B3 : SSet.{w}} (e1 : A1 = B1) (e2 : A2 = B2) (e3 : A3 = B3)
    (f : PNat (SProd (CubeS.{w} i j) A1) A3) (f2 : PNat (SProd (CubeS.{w} t j) A2) A3)
    (f1 : PNat (SProd (CubeS.{w} i t) A1) A2) {q : Nat} (c : ((CubeS.{w} i j).obj (q : Nat)).car)
    (h : ∀ x, (A3.obj (q : Nat)).eqv (f.app q (c, x)) (f2.app q (c, f1.app q (c, x)))) :
    ∀ x, (B3.obj (q : Nat)).eqv ((castPN e1 e3 f).app q (c, x))
      ((castPN e2 e3 f2).app q (c, (castPN e1 e2 f1).app q (c, x))) := by
  cases e1; cases e2; cases e3; exact h

theorem kan_cast {A B : SSet.{w}} (e : A = B) (h : Kan A) : Kan B := by
  cases e; exact h

/-! ## cube maps of coface maps -/

theorem sk_succ (i a : Nat) : sk (i + 1) (a + 1) = sk i a + 1 :=
  match Nat.lt_or_ge a i with
  | .inl h => by rw [sk_lt h, sk_lt (Nat.succ_lt_succ h)]
  | .inr h => by rw [sk_ge h, sk_ge (Nat.succ_le_succ h)]

theorem cm_face_out {m q : Nat} (t a b : Nat) (c : Nat → DHom q 1) {r : Nat} (hr : r ≤ q) :
    (cm (face m t) a b c t).f r = 0 :=
  eq0_of_ne1 (cm_le1 _ _ _ _ _ hr) (fun e => match cm_one e with
    | ⟨_, _, _, hs, _⟩ => sk_ne t _ hs)

theorem cm_face_in {m q : Nat} (t : Nat) {a b s : Nat} (has : a < s) (hsb : s < b) (c : Nat → DHom q 1)
    {r : Nat} (hr : r ≤ q) : (cm (face m t) a b c (sk t s)).f r = (c s).f r :=
  eq01 (cm_le1 _ _ _ _ _ hr) ((c s).bound r hr)
    (fun e => match cm_one e with
      | ⟨s', _, _, hs, hv⟩ => by
        have e' : s' = s := sk_inj hs
        rw [← e']; exact hv)
    (fun e => one_cm hr has hsb rfl e)

/-! ## the normal form of a horn -/

section normal

variable {n k : Nat} (h : Horn (SpS.{w}) n k)

/-- the faces as simplices of S -/
def hx (i : Nat) : SpX.{w} (n + 1) := h.x i

/-- the vertex family: Xv 0 from the last face, Xv (v + 1) from face 0 -/
def Xv : Nat → SSet.{w}
  | 0 => (hx h (n + 2)).X 0
  | v + 1 => (hx h 0).X v

theorem hcompat {i j : Nat} (hij : i ≤ j) (hj : j ≤ n + 1) (hik : i ≠ k) (hjk : j + 1 ≠ k) :
    SpEq (pbS (face n i) (hx h (j + 1))) (pbS (face n j) (hx h i)) :=
  h.compat i j hij hj hik hjk

/-- every face has vertices Xv (sk i a) -/
theorem hX (hk0 : 0 < k) (hk : k < n + 2) : ∀ i, i ≤ n + 2 → i ≠ k → ∀ a, a ≤ n + 1 →
    (hx h i).X a = Xv h (sk i a)
  | 0, _, _, _, _ => rfl
  | i' + 1, hi, hik, 0, _ =>
    match Nat.lt_or_ge (i' + 1) (n + 2) with
    | .inl hlt => match hcompat h (Nat.le_of_lt_succ hlt) (Nat.le_refl (n + 1)) hik
        (fun e => ne_of_lt' hk e.symm) with
      | ⟨e, _⟩ => (e 0 (Nat.zero_le n)).symm
    | .inr hge => by
      have e : i' + 1 = n + 2 := Nat.le_antisymm hi hge
      have e' : i' = n + 1 := Nat.succ.inj e
      subst e'
      rfl
  | i' + 1, hi, hik, a' + 1, ha =>
    match hcompat h (Nat.zero_le i') (Nat.le_of_succ_le_succ hi) (fun e => ne_of_lt' hk0 e) hik with
    | ⟨e, _⟩ => (e a' (Nat.le_of_succ_le_succ ha)).trans (congrArg (Xv h) (sk_succ i' a').symm)

variable (hk0 : 0 < k) (hk : k < n + 2)

/-- the faces rewritten over Xv -/
def yF (i : Nat) (hi : i ≤ n + 2) (hik : i ≠ k) : SpX.{w} (n + 1) where
  X a := Xv h (sk i a)
  kan a ha := kan_cast (hX h hk0 hk i hi hik a ha) ((hx h i).kan a ha)
  φ a b hab hb := castPN (hX h hk0 hk i hi hik a (Nat.le_trans hab hb)) (hX h hk0 hk i hi hik b hb)
    ((hx h i).φ a b hab hb)
  unit a ha q c x := castPN_unit (hX h hk0 hk i hi hik a ha) ((hx h i).φ a a (Nat.le_refl a) ha) c
    (fun x => (hx h i).unit a ha q c x) x
  comp a t b hat htb hb q c x hc := castPN_comp _ _ _ _ _ _ c
    (fun x => (hx h i).comp a t b hat htb hb q c x hc) x

theorem yF_eq (i : Nat) (hi : i ≤ n + 2) (hik : i ≠ k) : SpEq (hx h i) (yF h hk0 hk i hi hik) :=
  ⟨fun a ha => hX h hk0 hk i hi hik a ha, fun _ _ _ _ => castPN_rel _ _ _⟩

/-- the horn compatibilities for the normal form -/
theorem ycompat {i j : Nat} (hij : i ≤ j) (hj : j ≤ n + 1) (hik : i ≠ k) (hjk : j + 1 ≠ k) :
    SpEq (pbS (face n i) (yF h hk0 hk (j + 1) (Nat.succ_le_succ hj) hjk))
      (pbS (face n j) (yF h hk0 hk i (Nat.le_trans hij (Nat.le_trans hj (Nat.le_succ _))) hik)) :=
  spEq_trans (SpS.map_eqv (a := (n : Nat)) (b := (n + 1 : Nat)) (face n i)
      (spEq_symm (yF_eq h hk0 hk (j + 1) (Nat.succ_le_succ hj) hjk)))
    (spEq_trans (hcompat h hij hj hik hjk)
      (SpS.map_eqv (a := (n : Nat)) (b := (n + 1 : Nat)) (face n j)
        (yF_eq h hk0 hk i (Nat.le_trans hij (Nat.le_trans hj (Nat.le_succ _))) hik)))

end normal

end IC

end Found

#print axioms Found.IC.castS_eqv
#print axioms Found.IC.castS_trans
#print axioms Found.IC.castS_mp
#print axioms Found.IC.castS_back
#print axioms Found.IC.castPN_rel
#print axioms Found.IC.castPN_unit
#print axioms Found.IC.castPN_comp
#print axioms Found.IC.cm_face_out
#print axioms Found.IC.cm_face_in
#print axioms Found.IC.hX
#print axioms Found.IC.yF
#print axioms Found.IC.yF_eq
#print axioms Found.IC.ycompat

found/IC42.lean

lean_all/found/IC42.lean

/-
  T153 ((infinity,1)-categories), part 42. found/IC42 — inner horns of
  S = N^hc(Kan), II: face descriptions.  For vertices a <= b of the horn and
  a face t (t /= k) not containing a or b, the face yields a value of the map
  phi_ab at a cube c and a point x:
    Dval = phi^(t)_(a1 b1)(c ∘ sk t, x),   sk t a1 = a, sk t b1 = b,
  which is a legitimate value when c_t = 0 in case a < t < b.  Desc collects
  these descriptions; they are invariant under equality and under the
  simplicial operators, and every pair (a, b) other than (0, n+2) has one
  (from face 0 or from the last face).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.IC41

namespace Found

namespace IC

open Simp

universe w

theorem sk_lt_sk (t : Nat) {x y : Nat} (h : x < y) : sk t x < sk t y :=
  match Nat.lt_or_ge x t, Nat.lt_or_ge y t with
  | .inl hx, .inl hy => by rw [sk_lt hx, sk_lt hy]; exact h
  | .inl hx, .inr hy => by rw [sk_lt hx, sk_ge hy]; exact Nat.lt_succ_of_lt h
  | .inr hx, .inl hy => absurd (Nat.lt_of_lt_of_le (Nat.lt_trans h hy) hx) (Nat.lt_irrefl x)
  | .inr hx, .inr hy => by rw [sk_ge hx, sk_ge hy]; exact Nat.succ_lt_succ h

theorem sk_le_sk (t : Nat) {x y : Nat} (h : x ≤ y) : sk t x ≤ sk t y :=
  match Nat.lt_or_ge x y with
  | .inl hl => Nat.le_of_lt (sk_lt_sk t hl)
  | .inr hg => Nat.le_of_eq (congrArg (sk t) (Nat.le_antisymm h hg))

theorem lt_of_sk_lt (t : Nat) {x y : Nat} (h : sk t x < sk t y) : x < y :=
  match Nat.lt_or_ge x y with
  | .inl hl => hl
  | .inr hg => absurd (Nat.lt_of_lt_of_le h (sk_le_sk t hg)) (Nat.lt_irrefl _)

theorem le_of_sk_le (t : Nat) {x y : Nat} (h : sk t x ≤ sk t y) : x ≤ y :=
  match Nat.lt_or_ge y x with
  | .inl hl => absurd (Nat.lt_of_le_of_lt h (sk_lt_sk t hl)) (Nat.lt_irrefl _)
  | .inr hg => hg

section desc

variable {n k : Nat} (h : Horn (SpS.{w}) n k) (hk0 : 0 < k) (hk : k < n + 2)

/-- the value of phi_ab given by face t -/
def Dval (t : Nat) (ht : t ≤ n + 2) (htk : t ≠ k) {a b a1 b1 : Nat} (h1 : a1 ≤ b1) (h2 : b1 ≤ n + 1)
    (ea : sk t a1 = a) (eb : sk t b1 = b) {q : Nat} (c : Nat → DHom q 1) (x : ((Xv h a).obj (q : Nat)).car) :
    ((Xv h b).obj (q : Nat)).car :=
  castS (congrArg (Xv h) eb) (((yF h hk0 hk t ht htk).φ a1 b1 h1 h2).app q
    (⟨fun u => c (sk t u)⟩, castS (congrArg (Xv h) ea.symm) x))

/-- y is a face value of phi_ab at (c, x) -/
def Desc (a b q : Nat) (c : Nat → DHom q 1) (x : ((Xv h a).obj (q : Nat)).car) (y : ((Xv h b).obj (q : Nat)).car) :
    Prop :=
  ∃ (t a1 b1 : Nat) (ht : t ≤ n + 2) (htk : t ≠ k) (h1 : a1 ≤ b1) (h2 : b1 ≤ n + 1) (ea : sk t a1 = a)
    (eb : sk t b1 = b), (a < t → t < b → ∀ r, r ≤ q → (c t).f r = 0) ∧
    ((Xv h b).obj (q : Nat)).eqv y (Dval h hk0 hk t ht htk h1 h2 ea eb c x)

/-- the face cube of a cube equal on (a, b) is equal on (a1, b1) -/
theorem ceq_face {t a b a1 b1 q : Nat} (ea : sk t a1 = a) (eb : sk t b1 = b) {c c' : Nat → DHom q 1}
    (hc : CEq a b c c') : CEq a1 b1 (fun u => c (sk t u)) (fun u => c' (sk t u)) :=
  fun u h1 h2 r hr => hc (sk t u) (Nat.lt_of_le_of_lt (Nat.le_of_eq ea.symm) (sk_lt_sk t h1))
    (Nat.lt_of_lt_of_le (sk_lt_sk t h2) (Nat.le_of_eq eb)) r hr

theorem dval_eqv (t : Nat) (ht : t ≤ n + 2) (htk : t ≠ k) {a b a1 b1 : Nat} (h1 : a1 ≤ b1) (h2 : b1 ≤ n + 1)
    (ea : sk t a1 = a) (eb : sk t b1 = b) {q : Nat} {c c' : Nat → DHom q 1} (hc : CEq a b c c')
    {x x' : ((Xv h a).obj (q : Nat)).car} (hx : ((Xv h a).obj (q : Nat)).eqv x x') :
    ((Xv h b).obj (q : Nat)).eqv (Dval h hk0 hk t ht htk h1 h2 ea eb c x) (Dval h hk0 hk t ht htk h1 h2 ea eb c' x') :=
  castS_eqv _ (((yF h hk0 hk t ht htk).φ a1 b1 h1 h2).app_eqv q
    ⟨ceq_face ea eb hc, castS_eqv _ hx⟩)

theorem desc_eqv {a b q : Nat} {c c' : Nat → DHom q 1} {x x' : ((Xv h a).obj (q : Nat)).car}
    {y y' : ((Xv h b).obj (q : Nat)).car} (hc : CEq a b c c') (hx : ((Xv h a).obj (q : Nat)).eqv x x')
    (hy : ((Xv h b).obj (q : Nat)).eqv y y') (hd : Desc h hk0 hk a b q c x y) : Desc h hk0 hk a b q c' x' y' :=
  match hd with
  | ⟨t, a1, b1, ht, htk, h1, h2, ea, eb, hz, hv⟩ => ⟨t, a1, b1, ht, htk, h1, h2, ea, eb,
      fun hat htb r hr => (hc t hat htb r hr).symm.trans (hz hat htb r hr),
      ((Xv h b).obj (q : Nat)).trans (((Xv h b).obj (q : Nat)).symm hy)
        (((Xv h b).obj (q : Nat)).trans hv (dval_eqv h hk0 hk t ht htk h1 h2 ea eb hc hx))⟩

theorem dval_mp (t : Nat) (ht : t ≤ n + 2) (htk : t ≠ k) {a b a1 b1 : Nat} (h1 : a1 ≤ b1) (h2 : b1 ≤ n + 1)
    (ea : sk t a1 = a) (eb : sk t b1 = b) {q' q : Nat} (g : DHom q' q) (c : Nat → DHom q 1)
    (x : ((Xv h a).obj (q : Nat)).car) :
    ((Xv h b).obj (q' : Nat)).eqv (Dval h hk0 hk t ht htk h1 h2 ea eb (fun u => dComp g (c u)) (mp (Xv h a) g x))
      (mp (Xv h b) g (Dval h hk0 hk t ht htk h1 h2 ea eb c x)) :=
  have e1 : castS (congrArg (Xv h) ea.symm) (mp (Xv h a) g x) = mp (Xv h (sk t a1)) g (castS (congrArg (Xv h) ea.symm) x) :=
    castS_mp _ g x
  have e2 : castS (congrArg (Xv h) eb) (mp (Xv h (sk t b1)) g
      (((yF h hk0 hk t ht htk).φ a1 b1 h1 h2).app q (⟨fun u => c (sk t u)⟩, castS (congrArg (Xv h) ea.symm) x))) =
      mp (Xv h b) g (Dval h hk0 hk t ht htk h1 h2 ea eb c x) := castS_mp _ g _
  have m1 : ((Xv h (sk t b1)).obj (q' : Nat)).eqv
      (((yF h hk0 hk t ht htk).φ a1 b1 h1 h2).app q' (⟨fun u => dComp g (c (sk t u))⟩,
        castS (congrArg (Xv h) ea.symm) (mp (Xv h a) g x)))
      (mp (Xv h (sk t b1)) g (((yF h hk0 hk t ht htk).φ a1 b1 h1 h2).app q
        (⟨fun u => c (sk t u)⟩, castS (congrArg (Xv h) ea.symm) x))) := by
    rw [e1]
    exact ((yF h hk0 hk t ht htk).φ a1 b1 h1 h2).nat g (⟨fun u => c (sk t u)⟩, castS (congrArg (Xv h) ea.symm) x)
  by
    show ((Xv h b).obj (q' : Nat)).eqv (castS (congrArg (Xv h) eb) (((yF h hk0 hk t ht htk).φ a1 b1 h1 h2).app q'
        (⟨fun u => dComp g (c (sk t u))⟩, castS (congrArg (Xv h) ea.symm) (mp (Xv h a) g x)))) _
    rw [← e2]
    exact castS_eqv _ m1

theorem desc_mp {a b q' q : Nat} (g : DHom q' q) {c : Nat → DHom q 1} {x : ((Xv h a).obj (q : Nat)).car}
    {y : ((Xv h b).obj (q : Nat)).car} (hd : Desc h hk0 hk a b q c x y) :
    Desc h hk0 hk a b q' (fun u => dComp g (c u)) (mp (Xv h a) g x) (mp (Xv h b) g y) :=
  match hd with
  | ⟨t, a1, b1, ht, htk, h1, h2, ea, eb, hz, hv⟩ => ⟨t, a1, b1, ht, htk, h1, h2, ea, eb,
      fun hat htb r hr => hz hat htb (g.f r) (g.bound r hr),
      ((Xv h b).obj (q' : Nat)).trans (mp_eqv (Xv h b) g hv)
        (((Xv h b).obj (q' : Nat)).symm (dval_mp h hk0 hk t ht htk h1 h2 ea eb g c x))⟩

/-- every pair other than (0, n + 2) has a face value -/
theorem desc_ex {a b : Nat} (hab : a ≤ b) (hb : b ≤ n + 2) (hne : 0 < a ∨ b < n + 2) {q : Nat}
    (c : Nat → DHom q 1) (x : ((Xv h a).obj (q : Nat)).car) : ∃ y, Desc h hk0 hk a b q c x y :=
  match a, b, hab, hb, hne, x with
  | a' + 1, b' + 1, hab, hb, _, _ => ⟨_, 0, a', b', Nat.zero_le _, ne_of_lt' hk0, Nat.le_of_succ_le_succ hab,
      Nat.le_of_succ_le_succ hb, rfl, rfl, fun h0 _ => absurd h0 (Nat.not_lt_zero _),
      ((Xv h (b' + 1)).obj (q : Nat)).refl _⟩
  | a' + 1, 0, hab, _, _, _ => absurd hab (Nat.not_succ_le_zero a')
  | 0, b, _, _, hne, _ =>
    have hb' : b < n + 2 := hne.elim (fun h0 => absurd h0 (Nat.lt_irrefl 0)) id
    ⟨_, n + 2, 0, b, Nat.le_refl _, fun e => ne_of_lt' hk e.symm, Nat.zero_le b, Nat.le_of_lt_succ hb',
      rfl, sk_lt hb', fun _ h2 => absurd (Nat.lt_trans h2 hb') (Nat.lt_irrefl _),
      ((Xv h b).obj (q : Nat)).refl _⟩

end desc

end IC

end Found

#print axioms Found.IC.sk_lt_sk
#print axioms Found.IC.lt_of_sk_lt
#print axioms Found.IC.le_of_sk_le
#print axioms Found.IC.Dval
#print axioms Found.IC.ceq_face
#print axioms Found.IC.desc_eqv
#print axioms Found.IC.dval_mp
#print axioms Found.IC.desc_mp
#print axioms Found.IC.desc_ex

found/IC43.lean

lean_all/found/IC43.lean

/-
  T153 ((infinity,1)-categories), part 43. found/IC43 — inner horns of
  S = N^hc(Kan), III: face descriptions are unique.  Two faces s < s' = j+1
  describing phi_ab meet in the common face (vertices sk s (sk j v) =
  sk (j+1) (sk s v)), where the horn compatibility identifies them; the
  coordinates s and s' of the cube vanish by the conditions of the
  descriptions (two_core, dval_lt).  Hence any two face values agree
  (desc_unique).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.IC42

namespace Found

namespace IC

open Simp

universe w

theorem eq_or_ne' (u s : Nat) : u = s ∨ u ≠ s :=
  match Nat.lt_or_ge u s with
  | .inl h => .inr (ne_of_lt' h)
  | .inr h => match Nat.lt_or_ge s u with
    | .inl h' => .inr (fun e => ne_of_lt' h' e.symm)
    | .inr h' => .inl (Nat.le_antisymm h' h)

section uniq

variable {n k : Nat} (h : Horn (SpS.{w}) n k) (hk0 : 0 < k) (hk : k < n + 2)

/-- the common face of faces s and j + 1 (s <= j) -/
theorem two_core (s j : Nat) (hsj : s ≤ j) (hj : j ≤ n + 1) (hsk : s ≠ k) (hjk : j + 1 ≠ k)
    {a b : Nat} (hab : a ≤ b) (hb : b ≤ n) {q : Nat} (c : Nat → DHom q 1)
    (z1 : sk s a < s → s < sk s b → ∀ r, r ≤ q → (c s).f r = 0)
    (z2 : sk j a < j → j < sk j b → ∀ r, r ≤ q → (c (j + 1)).f r = 0)
    (x : ((Xv h (sk s (sk j a))).obj (q : Nat)).car) :
    ((Xv h (sk s (sk j b))).obj (q : Nat)).eqv
      (((yF h hk0 hk s (Nat.le_trans hsj (Nat.le_trans hj (Nat.le_succ _))) hsk).φ (sk j a) (sk j b)
        (sk_le_sk j hab) (Nat.le_trans (sk_le_succ j b) (Nat.succ_le_succ hb))).app q (⟨fun u => c (sk s u)⟩, x))
      (castS (congrArg (Xv h) (sk_sk hsj b))
        (((yF h hk0 hk (j + 1) (Nat.succ_le_succ hj) hjk).φ (sk s a) (sk s b)
          (sk_le_sk s hab) (Nat.le_trans (sk_le_succ s b) (Nat.succ_le_succ hb))).app q
          (⟨fun u => c (sk (j + 1) u)⟩, castS (congrArg (Xv h) (sk_sk hsj a)).symm x))) :=
  match ycompat h hk0 hk hsj hj hsk hjk with
  | ⟨E, HE⟩ =>
    let C2 : Nat → DHom q 1 := fun v => c (sk (j + 1) (sk s v))
    let x2 := castS (congrArg (Xv h) (sk_sk hsj a)).symm x
    have hh := HE a b hab hb q ⟨C2⟩ x2
    have ex : castS (E a (Nat.le_trans hab hb)) x2 = x := castS_back (congrArg (Xv h) (sk_sk hsj a)).symm x
    -- the cube of face j + 1
    have P1 : CEq (sk s a) (sk s b) (fun u => c (sk (j + 1) u)) (cm (face n s) a b C2) :=
      fun u hu1 hu2 r hr => match eq_or_ne' u s with
        | .inl e => by
          subst e
          rw [cm_face_out u a b C2 hr]
          show (c (sk (j + 1) u)).f r = 0
          rw [sk_lt (Nat.lt_succ_of_le hsj)]
          exact z1 hu1 hu2 r hr
        | .inr ne => by
          have e : sk s (dg (Nat.pred s) u) = u := sk_dg ne
          rw [← e] at hu1 hu2 ⊢
          rw [cm_face_in s (lt_of_sk_lt s hu1) (lt_of_sk_lt s hu2) C2 hr]
    -- the cube of face s
    have P2 : CEq (sk j a) (sk j b) (cm (face n j) a b C2) (fun u => c (sk s u)) :=
      fun u hu1 hu2 r hr => match eq_or_ne' u j with
        | .inl e => by
          subst e
          rw [cm_face_out u a b C2 hr]
          show 0 = (c (sk s u)).f r
          rw [sk_ge hsj]
          exact (z2 hu1 hu2 r hr).symm
        | .inr ne => by
          have e : sk j (dg (Nat.pred j) u) = u := sk_dg ne
          rw [← e] at hu1 hu2 ⊢
          rw [cm_face_in j (lt_of_sk_lt j hu1) (lt_of_sk_lt j hu2) C2 hr]
          show (c (sk (j + 1) (sk s (dg (Nat.pred j) u)))).f r = (c (sk s (sk j (dg (Nat.pred j) u)))).f r
          rw [sk_sk hsj]
    have m1 : ((Xv h (sk s (sk j b))).obj (q : Nat)).eqv
        (((yF h hk0 hk s (Nat.le_trans hsj (Nat.le_trans hj (Nat.le_succ _))) hsk).φ (sk j a) (sk j b)
          (sk_le_sk j hab) (Nat.le_trans (sk_le_succ j b) (Nat.succ_le_succ hb))).app q (⟨fun u => c (sk s u)⟩, x))
        (((yF h hk0 hk s (Nat.le_trans hsj (Nat.le_trans hj (Nat.le_succ _))) hsk).φ (sk j a) (sk j b)
          (sk_le_sk j hab) (Nat.le_trans (sk_le_succ j b) (Nat.succ_le_succ hb))).app q
          (⟨cm (face n j) a b C2⟩, castS (E a (Nat.le_trans hab hb)) x2)) := by
      rw [ex]
      exact ((yF h hk0 hk s _ hsk).φ _ _ _ _).app_eqv q
        ⟨fun u h1 h2 r hr => (P2 u h1 h2 r hr).symm, ((Xv h (sk s (sk j a))).obj (q : Nat)).refl x⟩
    have m3 : ((Xv h (sk s (sk j b))).obj (q : Nat)).eqv
        (castS (congrArg (Xv h) (sk_sk hsj b))
          (((yF h hk0 hk (j + 1) (Nat.succ_le_succ hj) hjk).φ (sk s a) (sk s b)
            (sk_le_sk s hab) (Nat.le_trans (sk_le_succ s b) (Nat.succ_le_succ hb))).app q
            (⟨cm (face n s) a b C2⟩, x2)))
        (castS (congrArg (Xv h) (sk_sk hsj b))
          (((yF h hk0 hk (j + 1) (Nat.succ_le_succ hj) hjk).φ (sk s a) (sk s b)
            (sk_le_sk s hab) (Nat.le_trans (sk_le_succ s b) (Nat.succ_le_succ hb))).app q
            (⟨fun u => c (sk (j + 1) u)⟩, x2))) :=
      castS_eqv _ (((yF h hk0 hk (j + 1) _ hjk).φ _ _ _ _).app_eqv q
        ⟨fun u h1 h2 r hr => (P1 u h1 h2 r hr).symm, ((Xv h (sk (j + 1) (sk s a))).obj (q : Nat)).refl x2⟩)
    ((Xv h (sk s (sk j b))).obj (q : Nat)).trans m1
      (((Xv h (sk s (sk j b))).obj (q : Nat)).trans (((Xv h (sk s (sk j b))).obj (q : Nat)).symm hh) m3)

/-- two face values from faces s < s' agree -/
theorem dval_lt {s s' : Nat} (hss : s < s') (hs : s ≤ n + 2) (hsk : s ≠ k) (hs' : s' ≤ n + 2) (hsk' : s' ≠ k)
    {a b a1 b1 a2 b2 : Nat} (h1 : a1 ≤ b1) (h2 : b1 ≤ n + 1) (h1' : a2 ≤ b2) (h2' : b2 ≤ n + 1)
    (ea : sk s a1 = a) (eb : sk s b1 = b) (ea' : sk s' a2 = a) (eb' : sk s' b2 = b) {q : Nat}
    (c : Nat → DHom q 1) (hz : a < s → s < b → ∀ r, r ≤ q → (c s).f r = 0)
    (hz' : a < s' → s' < b → ∀ r, r ≤ q → (c s').f r = 0) (x : ((Xv h a).obj (q : Nat)).car) :
    ((Xv h b).obj (q : Nat)).eqv (Dval h hk0 hk s hs hsk h1 h2 ea eb c x) (Dval h hk0 hk s' hs' hsk' h1' h2' ea' eb' c x) :=
  match s', hss, hs', hsk', ea', eb', hz' with
  | 0, hss, _, _, _, _, _ => absurd hss (Nat.not_lt_zero _)
  | j + 1, hss, hs', hsk', ea', eb', hz' => by
    have hsj : s ≤ j := Nat.le_of_lt_succ hss
    have hj : j ≤ n + 1 := Nat.le_of_succ_le_succ hs'
    have na2 : a2 ≠ s := fun e => sk_ne s a1 (ea.trans (ea'.symm.trans (by rw [e]; exact sk_lt hss)))
    have nb2 : b2 ≠ s := fun e => sk_ne s b1 (eb.trans (eb'.symm.trans (by rw [e]; exact sk_lt hss)))
    have ha : sk s (dg (Nat.pred s) a2) = a2 := sk_dg na2
    have hb' : sk s (dg (Nat.pred s) b2) = b2 := sk_dg nb2
    have e1 : sk j (dg (Nat.pred s) a2) = a1 := sk_inj (i := s)
      (((sk_sk hsj _).symm.trans (by rw [ha]; exact ea')).trans ea.symm)
    have f1 : sk j (dg (Nat.pred s) b2) = b1 := sk_inj (i := s)
      (((sk_sk hsj _).symm.trans (by rw [hb']; exact eb')).trans eb.symm)
    have hab : dg (Nat.pred s) a2 ≤ dg (Nat.pred s) b2 := le_of_sk_le s (by rw [ha, hb']; exact h1')
    have hbn : dg (Nat.pred s) b2 ≤ n := match Nat.lt_or_ge (dg (Nat.pred s) b2) (n + 1) with
      | .inl hl => Nat.le_of_lt_succ hl
      | .inr hg => absurd (Nat.le_trans (Nat.le_of_eq (sk_ge (Nat.le_trans hsj hj)).symm)
          (Nat.le_trans (sk_le_sk s hg) (Nat.le_trans (Nat.le_of_eq hb') h2'))) (Nat.not_succ_le_self _)
    generalize dg (Nat.pred s) a2 = a0 at ha e1 hab
    generalize dg (Nat.pred s) b2 = b0 at hb' f1 hab hbn
    subst ha; subst hb'; subst e1; subst f1; subst ea; subst eb
    exact two_core h hk0 hk s j hsj hj hsk hsk' hab hbn c
      (fun l1 l2 => hz (Nat.lt_of_le_of_lt (Nat.le_of_eq (ea'.symm.trans
          (sk_lt (Nat.lt_trans l1 (Nat.lt_succ_of_le hsj))))) l1)
        (Nat.lt_of_lt_of_le l2 (Nat.le_trans (self_le_sk _ _) (Nat.le_of_eq eb'))))
      (fun l1 l2 => hz' (Nat.lt_of_lt_of_le (sk_lt_sk s l1) (Nat.le_of_eq (sk_ge hsj)))
        (Nat.lt_of_le_of_lt (Nat.le_of_eq (sk_ge hsj).symm) (sk_lt_sk s l2)))
      x

/-- face values are unique -/
theorem desc_unique {a b q : Nat} {c : Nat → DHom q 1} {x : ((Xv h a).obj (q : Nat)).car}
    {y y' : ((Xv h b).obj (q : Nat)).car} (hd : Desc h hk0 hk a b q c x y) (hd' : Desc h hk0 hk a b q c x y') :
    ((Xv h b).obj (q : Nat)).eqv y y' :=
  match hd, hd' with
  | ⟨t, a1, b1, ht, htk, h1, h2, ea, eb, hz, hv⟩, ⟨t', a2, b2, ht', htk', h1', h2', ea', eb', hz', hv'⟩ =>
    have mid : ((Xv h b).obj (q : Nat)).eqv (Dval h hk0 hk t ht htk h1 h2 ea eb c x)
        (Dval h hk0 hk t' ht' htk' h1' h2' ea' eb' c x) :=
      match Nat.lt_or_ge t t' with
      | .inl hl => dval_lt h hk0 hk hl ht htk ht' htk' h1 h2 h1' h2' ea eb ea' eb' c hz hz' x
      | .inr hg => match Nat.lt_or_ge t' t with
        | .inl hl => ((Xv h b).obj (q : Nat)).symm
            (dval_lt h hk0 hk hl ht' htk' ht htk h1' h2' h1 h2 ea' eb' ea eb c hz' hz x)
        | .inr hg' => by
          have e : t = t' := Nat.le_antisymm hg' hg
          subst e
          have e2 : a1 = a2 := sk_inj (ea.trans ea'.symm)
          have f2 : b1 = b2 := sk_inj (eb.trans eb'.symm)
          subst e2; subst f2
          exact ((Xv h b).obj (q : Nat)).refl _
    ((Xv h b).obj (q : Nat)).trans hv (((Xv h b).obj (q : Nat)).trans mid (((Xv h b).obj (q : Nat)).symm hv'))

end uniq

end IC

end Found

#print axioms Found.IC.eq_or_ne'
#print axioms Found.IC.two_core
#print axioms Found.IC.dval_lt
#print axioms Found.IC.desc_unique

found/IC44.lean

lean_all/found/IC44.lean

/-
  T153 ((infinity,1)-categories), part 44. found/IC44 — inner horns of
  S = N^hc(Kan), IV: face values compose (desc_comp: if c_t = 1 then the
  value on (a, b) is the value on (t, b) of the value on (a, t), by the
  composition law of the describing face), and the maps phi_ab for
  (a, b) /= (0, n+2) chosen as face values (Phi1, in the classical point).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.IC43

namespace Found

namespace IC

open Simp

universe w

section phi1

variable {n k : Nat} (h : Horn (SpS.{w}) n k) (hk0 : 0 < k) (hk : k < n + 2)

/-- face values compose along a coordinate equal to 1 -/
theorem desc_comp {a t b q : Nat} {c : Nat → DHom q 1} {x : ((Xv h a).obj (q : Nat)).car}
    {y : ((Xv h b).obj (q : Nat)).car} {y1 : ((Xv h t).obj (q : Nat)).car} {y2 : ((Xv h b).obj (q : Nat)).car}
    (hat : a < t) (htb : t < b) (hc : ∀ r, r ≤ q → (c t).f r = 1)
    (hd : Desc h hk0 hk a b q c x y) (hd1 : Desc h hk0 hk a t q c x y1) (hd2 : Desc h hk0 hk t b q c y1 y2) :
    ((Xv h b).obj (q : Nat)).eqv y y2 :=
  match hd with
  | ⟨s, a1, b1, hs, hsk, h1, h2, ea, eb, hz, hv⟩ =>
    have nts : t ≠ s := fun e => by
      have e0 := hz (e ▸ hat) (e ▸ htb) 0 (Nat.zero_le q)
      rw [← e, hc 0 (Nat.zero_le q)] at e0
      exact Nat.noConfusion e0
    have et : sk s (dg (Nat.pred s) t) = t := sk_dg nts
    have l1 : a1 < dg (Nat.pred s) t := lt_of_sk_lt s (by rw [ea, et]; exact hat)
    have l2 : dg (Nat.pred s) t < b1 := lt_of_sk_lt s (by rw [eb, et]; exact htb)
    have hd1' : Desc h hk0 hk a t q c x
        (Dval h hk0 hk s hs hsk (Nat.le_of_lt l1) (Nat.le_trans (Nat.le_of_lt l2) h2) ea et c x) :=
      ⟨s, a1, _, hs, hsk, Nat.le_of_lt l1, Nat.le_trans (Nat.le_of_lt l2) h2, ea, et,
        fun h1' h2' => hz h1' (Nat.lt_trans h2' htb), ((Xv h t).obj (q : Nat)).refl _⟩
    have m1 := desc_unique h hk0 hk hd1 hd1'
    match hd2 with
    | hd2 =>
      have hd2' : Desc h hk0 hk t b q c y1
          (Dval h hk0 hk s hs hsk (Nat.le_of_lt l2) h2 et eb c y1) :=
        ⟨s, _, b1, hs, hsk, Nat.le_of_lt l2, h2, et, eb,
          fun h1' h2' => hz (Nat.lt_trans hat h1') h2', ((Xv h b).obj (q : Nat)).refl _⟩
      have m2 := desc_unique h hk0 hk hd2 hd2'
      have m3 : ((Xv h b).obj (q : Nat)).eqv
          (Dval h hk0 hk s hs hsk (Nat.le_of_lt l2) h2 et eb c y1)
          (Dval h hk0 hk s hs hsk (Nat.le_of_lt l2) h2 et eb c
            (Dval h hk0 hk s hs hsk (Nat.le_of_lt l1) (Nat.le_trans (Nat.le_of_lt l2) h2) ea et c x)) :=
        dval_eqv h hk0 hk s hs hsk _ h2 et eb (fun _ _ _ _ _ => rfl) m1
      have m4 : ((Xv h b).obj (q : Nat)).eqv
          (Dval h hk0 hk s hs hsk h1 h2 ea eb c x)
          (Dval h hk0 hk s hs hsk (Nat.le_of_lt l2) h2 et eb c
            (Dval h hk0 hk s hs hsk (Nat.le_of_lt l1) (Nat.le_trans (Nat.le_of_lt l2) h2) ea et c x)) := by
        generalize dg (Nat.pred s) t = t1 at et l1 l2
        subst et; subst ea; subst eb
        exact (yF h hk0 hk s hs hsk).comp a1 t1 b1 l1 l2 h2 q ⟨fun u => c (sk s u)⟩ x (fun r hr => hc r hr)
      ((Xv h b).obj (q : Nat)).trans hv (((Xv h b).obj (q : Nat)).trans m4
        (((Xv h b).obj (q : Nat)).symm (((Xv h b).obj (q : Nat)).trans m2 m3)))

/-- phi_ab for (a, b) /= (0, n + 2): the face value -/
def Phi1 (Kw : ClW.{w}) (a b : Nat) (hab : a ≤ b) (hb : b ≤ n + 2) (hne : 0 < a ∨ b < n + 2) :
    PNat (SProd (CubeS.{w} a b) (Xv h a)) (Xv h b) where
  app q z := Kw.ch (Desc h hk0 hk a b q z.1.down z.2) (desc_ex h hk0 hk hab hb hne z.1.down z.2)
  app_eqv q {z z'} e := desc_unique h hk0 hk
    (desc_eqv h hk0 hk e.1 e.2 (((Xv h b).obj (q : Nat)).refl _) (Kw.ch_spec _ (desc_ex h hk0 hk hab hb hne z.1.down z.2)))
    (Kw.ch_spec _ (desc_ex h hk0 hk hab hb hne z'.1.down z'.2))
  nat {_ _} g z := desc_unique h hk0 hk
    (Kw.ch_spec _ (desc_ex h hk0 hk hab hb hne (fun u => dComp g (z.1.down u)) (mp (Xv h a) g z.2)))
    (desc_mp h hk0 hk g (Kw.ch_spec _ (desc_ex h hk0 hk hab hb hne z.1.down z.2)))

theorem phi1_spec (Kw : ClW.{w}) {a b : Nat} (hab : a ≤ b) (hb : b ≤ n + 2) (hne : 0 < a ∨ b < n + 2) {q : Nat}
    (c : Nat → DHom q 1) (x : ((Xv h a).obj (q : Nat)).car) :
    Desc h hk0 hk a b q c x ((Phi1 h hk0 hk Kw a b hab hb hne).app q (⟨c⟩, x)) :=
  Kw.ch_spec _ (desc_ex h hk0 hk hab hb hne c x)

end phi1

end IC

end Found

#print axioms Found.IC.desc_comp
#print axioms Found.IC.Phi1
#print axioms Found.IC.phi1_spec

found/IC45.lean

lean_all/found/IC45.lean

/-
  T153 ((infinity,1)-categories), part 45. found/IC45 — inner horns of
  S = N^hc(Kan), V: the open box.  The missing map phi_(0,N) (N = n+2) is
  known on the faces c_s = 0 (s /= k, from face s of the horn) and c_t = 1
  (all t, the composite of phi_tN and phi_0t).  Splitting off the
  coordinate k, this is the subcomplex {1} x B ∪ Delta^1 x A of
  Delta^1 x B with B = (cube without k) x Xv 0 and A = (boundary) x Xv 0;
  the box values glue to a partial map (BoxCan, unique), which extends to
  Delta^1 x B because Xv N is a Kan complex (IC37).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.IC44

namespace Found

namespace IC

open Simp

universe w

/-! ## the cube with coordinate k split off -/

/-- the cube (Delta^1)^(a,b) without the coordinate k -/
def CubeK (a b k : Nat) : SSet.{w} where
  obj := fun (q : Nat) => ⟨ULift.{w} (Nat → DHom q 1),
    fun c c' => ∀ t, a < t → t < b → t ≠ k → ∀ r, r ≤ q → (c.down t).f r = (c'.down t).f r,
    fun _ _ _ _ _ _ _ => rfl, fun h t h1 h2 h3 r hr => (h t h1 h2 h3 r hr).symm,
    fun h h' t h1 h2 h3 r hr => (h t h1 h2 h3 r hr).trans (h' t h1 h2 h3 r hr)⟩
  map g c := ⟨fun t => dComp g (c.down t)⟩
  map_eqv {_ _} g {_ _} h := fun t h1 h2 h3 r hr => h t h1 h2 h3 _ (g.bound r hr)
  map_resp {_ _ _ _} c e := fun t _ _ _ r hr => congrArg (c.down t).f (e r hr)
  map_id _ _ := fun _ _ _ _ _ _ => rfl
  map_comp _ _ _ := fun _ _ _ _ _ _ => rfl

/-- insert the coordinate k -/
def insC (k : Nat) {q : Nat} (u : DHom q 1) (c : Nat → DHom q 1) (t : Nat) : DHom q 1 :=
  cond (gtb k t) (c t) (cond (gtb t k) (c t) u)

theorem ins_k (k : Nat) {q : Nat} (u : DHom q 1) (c : Nat → DHom q 1) : insC k u c k = u := by
  show cond (gtb k k) (c k) (cond (gtb k k) (c k) u) = u
  rw [le_gtb k k (Nat.le_refl k)]; rfl

theorem ins_ne {k t : Nat} (ht : t ≠ k) {q : Nat} (u : DHom q 1) (c : Nat → DHom q 1) : insC k u c t = c t := by
  show cond (gtb k t) (c t) (cond (gtb t k) (c t) u) = c t
  exact match Nat.lt_or_ge t k with
  | .inl hl => by rw [lt_gtb k t hl]; rfl
  | .inr hg => by
    rw [le_gtb k t hg, lt_gtb t k (Nat.lt_of_le_of_ne hg (fun e => ht e.symm))]; rfl

theorem ins_full (k : Nat) {q : Nat} (c : Nat → DHom q 1) (t : Nat) : ∀ r, (insC k (c k) c t).f r = (c t).f r :=
  fun r => match eq_or_ne' t k with
  | .inl e => by subst e; rw [ins_k]
  | .inr ne => by rw [ins_ne ne]

theorem ins_eqv {a b k q : Nat} {u u' : DHom q 1} {c c' : Nat → DHom q 1} (hu : ∀ r, r ≤ q → u.f r = u'.f r)
    (hc : ∀ t, a < t → t < b → t ≠ k → ∀ r, r ≤ q → (c t).f r = (c' t).f r) :
    CEq a b (insC k u c) (insC k u' c') :=
  fun t h1 h2 r hr => match eq_or_ne' t k with
  | .inl e => by subst e; rw [ins_k, ins_k]; exact hu r hr
  | .inr ne => by rw [ins_ne ne, ins_ne ne]; exact hc t h1 h2 ne r hr

theorem ins_mp (k : Nat) {q' q : Nat} (g : DHom q' q) (u : DHom q 1) (c : Nat → DHom q 1) (t : Nat) :
    ∀ r, (insC k (dComp g u) (fun s => dComp g (c s)) t).f r = (dComp g (insC k u c t)).f r :=
  fun r => match eq_or_ne' t k with
  | .inl e => by subst e; rw [ins_k, ins_k]
  | .inr ne => by rw [ins_ne ne, ins_ne ne]

section box

variable {n k : Nat} (h : Horn (SpS.{w}) n k) (hk0 : 0 < k) (hk : k < n + 2)

/-- B = (cube without k) x Xv 0 -/
abbrev BoxB : SSet.{w} := SProd (CubeK.{w} 0 (n + 2) k) (Xv h 0)

/-- A = (boundary of the cube without k) x Xv 0 -/
def BoxA : Sub (BoxB h) where
  P q z := ∃ t, 0 < t ∧ t < n + 2 ∧ t ≠ k ∧
    ((∀ r, r ≤ q → (z.1.down t).f r = 0) ∨ (∀ r, r ≤ q → (z.1.down t).f r = 1))
  P_eqv _ {_ _} e hz := match hz with
    | ⟨t, h1, h2, h3, hv⟩ => ⟨t, h1, h2, h3, hv.elim
        (fun h0 => .inl (fun r hr => (e.1 t h1 h2 h3 r hr).symm.trans (h0 r hr)))
        (fun h1' => .inr (fun r hr => (e.1 t h1 h2 h3 r hr).symm.trans (h1' r hr)))⟩
  P_map g _ hz := match hz with
    | ⟨t, h1, h2, h3, hv⟩ => ⟨t, h1, h2, h3, hv.elim (fun h0 => .inl (fun r hr => h0 _ (g.bound r hr)))
        (fun h1' => .inr (fun r hr => h1' _ (g.bound r hr)))⟩

/-- the values on the open box -/
def BoxCan (q : Nat) (C : Nat → DHom q 1) (x : ((Xv h 0).obj (q : Nat)).car) (y : ((Xv h (n + 2)).obj (q : Nat)).car) :
    Prop :=
  Desc h hk0 hk 0 (n + 2) q C x y ∨ ∃ t, 0 < t ∧ t < n + 2 ∧ (∀ r, r ≤ q → (C t).f r = 1) ∧
    ∃ y1, Desc h hk0 hk 0 t q C x y1 ∧ Desc h hk0 hk t (n + 2) q C y1 y

theorem ceq_sub {a b a' b' q : Nat} {c c' : Nat → DHom q 1} (hc : CEq a b c c') (ha : a ≤ a') (hb : b' ≤ b) :
    CEq a' b' c c' :=
  fun t h1 h2 r hr => hc t (Nat.lt_of_le_of_lt ha h1) (Nat.lt_of_lt_of_le h2 hb) r hr

theorem boxCan_eqv {q : Nat} {C C' : Nat → DHom q 1} {x x' : ((Xv h 0).obj (q : Nat)).car}
    {y y' : ((Xv h (n + 2)).obj (q : Nat)).car} (hc : CEq 0 (n + 2) C C') (hx : ((Xv h 0).obj (q : Nat)).eqv x x')
    (hy : ((Xv h (n + 2)).obj (q : Nat)).eqv y y') (hb : BoxCan h hk0 hk q C x y) : BoxCan h hk0 hk q C' x' y' :=
  hb.elim (fun hd => .inl (desc_eqv h hk0 hk hc hx hy hd))
    (fun ⟨t, h1, h2, h3, y1, d1, d2⟩ => .inr ⟨t, h1, h2, fun r hr => (hc t h1 h2 r hr).symm.trans (h3 r hr), y1,
      desc_eqv h hk0 hk (ceq_sub hc (Nat.le_refl 0) (Nat.le_of_lt h2)) hx (((Xv h t).obj (q : Nat)).refl _) d1,
      desc_eqv h hk0 hk (ceq_sub hc (Nat.zero_le t) (Nat.le_refl _)) (((Xv h t).obj (q : Nat)).refl _) hy d2⟩)

theorem boxCan_mp {q' q : Nat} (g : DHom q' q) {C : Nat → DHom q 1} {x : ((Xv h 0).obj (q : Nat)).car}
    {y : ((Xv h (n + 2)).obj (q : Nat)).car} (hb : BoxCan h hk0 hk q C x y) :
    BoxCan h hk0 hk q' (fun t => dComp g (C t)) (mp (Xv h 0) g x) (mp (Xv h (n + 2)) g y) :=
  hb.elim (fun hd => .inl (desc_mp h hk0 hk g hd))
    (fun ⟨t, h1, h2, h3, y1, d1, d2⟩ => .inr ⟨t, h1, h2, fun r hr => h3 _ (g.bound r hr), mp (Xv h t) g y1,
      desc_mp h hk0 hk g d1, desc_mp h hk0 hk g d2⟩)

/-- composites along two coordinates agree -/
theorem two_ones {q : Nat} {C : Nat → DHom q 1} {x : ((Xv h 0).obj (q : Nat)).car} {t t' : Nat}
    (h1 : 0 < t) (hl : t < t') (h2 : t' < n + 2) (o1 : ∀ r, r ≤ q → (C t).f r = 1) (o2 : ∀ r, r ≤ q → (C t').f r = 1)
    {y1 : ((Xv h t).obj (q : Nat)).car} {y : ((Xv h (n + 2)).obj (q : Nat)).car}
    {y1' : ((Xv h t').obj (q : Nat)).car} {y' : ((Xv h (n + 2)).obj (q : Nat)).car}
    (d1 : Desc h hk0 hk 0 t q C x y1) (d2 : Desc h hk0 hk t (n + 2) q C y1 y)
    (d1' : Desc h hk0 hk 0 t' q C x y1') (d2' : Desc h hk0 hk t' (n + 2) q C y1' y') :
    ((Xv h (n + 2)).obj (q : Nat)).eqv y y' :=
  match desc_ex h hk0 hk (Nat.le_of_lt hl) (Nat.le_of_lt h2) (.inl h1) C y1 with
  | ⟨z, dz⟩ => match desc_ex h hk0 hk (Nat.le_of_lt h2) (Nat.le_refl _) (.inl (Nat.lt_trans h1 hl)) C z with
    | ⟨w, dw⟩ =>
      have e1 : ((Xv h t').obj (q : Nat)).eqv y1' z := desc_comp h hk0 hk h1 hl o1 d1' d1 dz
      have e2 : ((Xv h (n + 2)).obj (q : Nat)).eqv y w := desc_comp h hk0 hk hl h2 o2 d2 dz dw
      have d2'' : Desc h hk0 hk t' (n + 2) q C z y' :=
        desc_eqv h hk0 hk (fun _ _ _ _ _ => rfl) e1 (((Xv h (n + 2)).obj (q : Nat)).refl _) d2'
      ((Xv h (n + 2)).obj (q : Nat)).trans e2 (desc_unique h hk0 hk dw d2'')

theorem boxCan_unique {q : Nat} {C : Nat → DHom q 1} {x : ((Xv h 0).obj (q : Nat)).car}
    {y y' : ((Xv h (n + 2)).obj (q : Nat)).car} (hb : BoxCan h hk0 hk q C x y) (hb' : BoxCan h hk0 hk q C x y') :
    ((Xv h (n + 2)).obj (q : Nat)).eqv y y' :=
  match hb, hb' with
  | .inl d, .inl d' => desc_unique h hk0 hk d d'
  | .inl d, .inr ⟨t, h1, h2, h3, y1, d1, d2⟩ => desc_comp h hk0 hk h1 h2 h3 d d1 d2
  | .inr ⟨t, h1, h2, h3, y1, d1, d2⟩, .inl d =>
    ((Xv h (n + 2)).obj (q : Nat)).symm (desc_comp h hk0 hk h1 h2 h3 d d1 d2)
  | .inr ⟨t, h1, h2, h3, y1, d1, d2⟩, .inr ⟨t', h1', h2', h3', y1', d1', d2'⟩ =>
    match Nat.lt_or_ge t t' with
    | .inl hl => two_ones h hk0 hk h1 hl h2' h3 h3' d1 d2 d1' d2'
    | .inr hg => match Nat.lt_or_ge t' t with
      | .inl hl => ((Xv h (n + 2)).obj (q : Nat)).symm (two_ones h hk0 hk h1' hl h2 h3' h3 d1' d2' d1 d2)
      | .inr hg' => by
        have e : t = t' := Nat.le_antisymm hg' hg
        subst e
        have e1 := desc_unique h hk0 hk d1 d1'
        exact desc_unique h hk0 hk d2
          (desc_eqv h hk0 hk (fun _ _ _ _ _ => rfl) (((Xv h t).obj (q : Nat)).symm e1)
            (((Xv h (n + 2)).obj (q : Nat)).refl _) d2')

/-- the face of the cube where coordinate t vanishes is described by face t -/
theorem desc_zero {q : Nat} {C : Nat → DHom q 1} (x : ((Xv h 0).obj (q : Nat)).car) {t : Nat}
    (h1 : 0 < t) (h2 : t < n + 2) (htk : t ≠ k) (h0 : ∀ r, r ≤ q → (C t).f r = 0) :
    ∃ y, Desc h hk0 hk 0 (n + 2) q C x y :=
  ⟨_, t, 0, n + 1, Nat.le_of_lt h2, htk, Nat.zero_le _, Nat.le_refl _, sk_lt h1, sk_ge (Nat.le_of_lt_succ h2),
    fun _ _ => h0, ((Xv h (n + 2)).obj (q : Nat)).refl _⟩

theorem boxCan_ex {q : Nat} (z : ((SProd (Simp.{w} 1) (BoxB h)).obj (q : Nat)).car) (hz : (LA1e (BoxA h)).P q z) :
    ∃ y, BoxCan h hk0 hk q (insC k z.1.down z.2.1.down) z.2.2 y :=
  have one : ∀ t, 0 < t → t < n + 2 → (∀ r, r ≤ q → (insC k z.1.down z.2.1.down t).f r = 1) →
      ∃ y, BoxCan h hk0 hk q (insC k z.1.down z.2.1.down) z.2.2 y := fun t h1 h2 h3 =>
    match desc_ex h hk0 hk (Nat.zero_le t) (Nat.le_of_lt h2) (.inr h2) (insC k z.1.down z.2.1.down) z.2.2 with
    | ⟨y1, d1⟩ => match desc_ex h hk0 hk (Nat.le_of_lt h2) (Nat.le_refl _) (.inl h1) (insC k z.1.down z.2.1.down) y1 with
      | ⟨y, d2⟩ => ⟨y, .inr ⟨t, h1, h2, h3, y1, d1, d2⟩⟩
  match hz with
  | .inl hl => one k hk0 hk (fun r hr => by
      rw [ins_k]
      exact match Nat.lt_or_ge (z.1.down.f r) 1 with
        | .inl h0 => absurd (Nat.eq_zero_of_le_zero (Nat.le_of_lt_succ h0)) (hl r hr)
        | .inr h1 => Nat.le_antisymm (z.1.down.bound r hr) h1)
  | .inr ⟨t, h1, h2, h3, hv⟩ => hv.elim
      (fun h0 => match desc_zero h hk0 hk z.2.2 h1 h2 h3 (C := insC k z.1.down z.2.1.down)
          (fun r hr => by rw [ins_ne h3]; exact h0 r hr) with
        | ⟨y, d⟩ => ⟨y, .inl d⟩)
      (fun h1' => one t h1 h2 (fun r hr => by rw [ins_ne h3]; exact h1' r hr))

/-- the box values as a partial map on {1} x B ∪ Delta^1 x A -/
def boxMap (Kw : ClW.{w}) : PMap (LA1e (BoxA h)) (Xv h (n + 2)) where
  app q z hz := Kw.ch (BoxCan h hk0 hk q (insC k z.1.down z.2.1.down) z.2.2) (boxCan_ex h hk0 hk z hz)
  app_eqv q {z z'} e hz hz' := boxCan_unique h hk0 hk
    (boxCan_eqv h hk0 hk (ins_eqv e.1 e.2.1) e.2.2 (((Xv h (n + 2)).obj (q : Nat)).refl _)
      (Kw.ch_spec _ (boxCan_ex h hk0 hk z hz)))
    (Kw.ch_spec _ (boxCan_ex h hk0 hk z' hz'))
  nat {_ _} g z hz := boxCan_unique h hk0 hk
    (Kw.ch_spec _ (boxCan_ex h hk0 hk _ ((LA1e (BoxA h)).P_map g z hz)))
    (boxCan_eqv h hk0 hk (fun t _ _ r _ => (ins_mp k g z.1.down z.2.1.down t r).symm)
      (((Xv h 0).obj _).refl _) (((Xv h (n + 2)).obj _).refl _)
      (boxCan_mp h hk0 hk g (Kw.ch_spec _ (boxCan_ex h hk0 hk z hz))))

theorem boxMap_spec (Kw : ClW.{w}) {q : Nat} (z : ((SProd (Simp.{w} 1) (BoxB h)).obj (q : Nat)).car)
    (hz : (LA1e (BoxA h)).P q z) :
    BoxCan h hk0 hk q (insC k z.1.down z.2.1.down) z.2.2 ((boxMap h hk0 hk Kw).app q z hz) :=
  Kw.ch_spec _ (boxCan_ex h hk0 hk z hz)

theorem kan_top : Kan (Xv h (n + 2)) := (hx h 0).kan (n + 1) (Nat.le_refl _)

/-- the open box extends (Xv N is a Kan complex) -/
theorem box_ext (Kw : ClW.{w}) (Kc : CP.Classical') :
    ∃ Φ : PNat (SProd (Simp.{w} 1) (BoxB h)) (Xv h (n + 2)), ∀ q z (hz : (LA1e (BoxA h)).P q z),
      ((Xv h (n + 2)).obj (q : Nat)).eqv (Φ.app q z) ((boxMap h hk0 hk Kw).app q z hz) :=
  lam1_exte Kw Kc (kan_top h) (BoxA h) (boxMap h hk0 hk Kw)

end box

end IC

end Found

#print axioms Found.IC.CubeK
#print axioms Found.IC.ins_eqv
#print axioms Found.IC.ins_mp
#print axioms Found.IC.boxCan_eqv
#print axioms Found.IC.boxCan_mp
#print axioms Found.IC.two_ones
#print axioms Found.IC.boxCan_unique
#print axioms Found.IC.boxCan_ex
#print axioms Found.IC.boxMap
#print axioms Found.IC.box_ext

found/IC46.lean

lean_all/found/IC46.lean

/-
  T153 ((infinity,1)-categories), part 46. found/IC46 — the
  infinity-category of spaces: S = N^hc(Kan) is a quasi-category (inside the
  classical point).  The filler of an inner horn Lambda^N_k (N = n+2) has
  vertices Xv, maps phi_ab given by the faces when (a, b) /= (0, N) and by the
  extension of the open box (IC45) when (a, b) = (0, N); its unit and
  composition laws follow from those of the faces (desc_comp) and from the
  box values; its faces are the faces of the horn (cm_face_out/in).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.IC45

namespace Found

namespace IC

open Simp

universe w

theorem castS_self {A : SSet.{w}} (e : A = A) {q : Nat} (x : (A.obj (q : Nat)).car) : castS e x = x := by
  cases e; rfl

section fill

variable {n k : Nat} (h : Horn (SpS.{w}) n k) (hk0 : 0 < k) (hk : k < n + 2) (Kw : ClW.{w}) (Kc : CP.Classical')

/-- the chosen extension of the open box -/
def PhiB : PNat (SProd (Simp.{w} 1) (BoxB h)) (Xv h (n + 2)) := Kw.ch _ (box_ext h hk0 hk Kw Kc)

theorem phiB_spec {q : Nat} (z : ((SProd (Simp.{w} 1) (BoxB h)).obj (q : Nat)).car) (hz : (LA1e (BoxA h)).P q z) :
    ((Xv h (n + 2)).obj (q : Nat)).eqv ((PhiB h hk0 hk Kw Kc).app q z) ((boxMap h hk0 hk Kw).app q z hz) :=
  Kw.ch_spec _ (box_ext h hk0 hk Kw Kc) q z hz

/-- a value on (0, N) given by a face lies on the box -/
theorem desc_box {q : Nat} {c : Nat → DHom q 1} {x : ((Xv h 0).obj (q : Nat)).car}
    {y : ((Xv h (n + 2)).obj (q : Nat)).car} (d : Desc h hk0 hk 0 (n + 2) q c x y) :
    (BoxA h).P q (⟨c⟩, x) :=
  match d with
  | ⟨s, _, _, hs, hsk, _, _, ea, eb, hz, _⟩ =>
    have h1 : 0 < s := match s, ea with
      | 0, ea => absurd ea (sk_ne 0 _)
      | _ + 1, _ => Nat.succ_pos _
    have h2 : s < n + 2 := Nat.lt_of_le_of_ne hs (fun e => sk_ne s _ (eb.trans e.symm))
    ⟨s, h1, h2, hsk, .inl (hz h1 h2)⟩

/-- PhiB agrees with every box value -/
theorem phiB_box {q : Nat} {c : Nat → DHom q 1} {x : ((Xv h 0).obj (q : Nat)).car}
    {y : ((Xv h (n + 2)).obj (q : Nat)).car} (hb : BoxCan h hk0 hk q c x y) :
    ((Xv h (n + 2)).obj (q : Nat)).eqv ((PhiB h hk0 hk Kw Kc).app q (⟨c k⟩, (⟨c⟩, x))) y :=
  have hz : (LA1e (BoxA h)).P q (⟨c k⟩, (⟨c⟩, x)) := match hb with
    | .inl d => .inr (desc_box h hk0 hk d)
    | .inr ⟨t, h1, h2, h3, _, _, _⟩ => match eq_or_ne' t k with
      | .inl e => .inl (fun r hr e0 => by
          subst e
          rw [h3 r hr] at e0
          exact Nat.noConfusion e0)
      | .inr ne => .inr ⟨t, h1, h2, ne, .inr h3⟩
  ((Xv h (n + 2)).obj (q : Nat)).trans (phiB_spec h hk0 hk Kw Kc _ hz)
    (boxCan_unique h hk0 hk (boxMap_spec h hk0 hk Kw _ hz)
      (boxCan_eqv h hk0 hk (fun t _ _ r _ => (ins_full k c t r).symm) (((Xv h 0).obj _).refl _)
        (((Xv h (n + 2)).obj _).refl _) hb))

/-- the values of the filler -/
def CanS (a b q : Nat) (c : Nat → DHom q 1) (x : ((Xv h a).obj (q : Nat)).car) (y : ((Xv h b).obj (q : Nat)).car) :
    Prop :=
  Desc h hk0 hk a b q c x y ∨ ∃ (ea : a = 0) (eb : b = n + 2), ((Xv h b).obj (q : Nat)).eqv y
    (castS (congrArg (Xv h) eb.symm) ((PhiB h hk0 hk Kw Kc).app q (⟨c k⟩, (⟨c⟩, castS (congrArg (Xv h) ea) x))))

theorem canS_ex {a b : Nat} (hab : a ≤ b) (hb : b ≤ n + 2) {q : Nat} (c : Nat → DHom q 1)
    (x : ((Xv h a).obj (q : Nat)).car) : ∃ y, CanS h hk0 hk Kw Kc a b q c x y :=
  match Nat.lt_or_ge 0 a, Nat.lt_or_ge b (n + 2) with
  | .inl ha, _ => match desc_ex h hk0 hk hab hb (.inl ha) c x with | ⟨y, d⟩ => ⟨y, .inl d⟩
  | _, .inl hb' => match desc_ex h hk0 hk hab hb (.inr hb') c x with | ⟨y, d⟩ => ⟨y, .inl d⟩
  | .inr ha, .inr hb' => ⟨_, .inr ⟨Nat.eq_zero_of_le_zero ha, Nat.le_antisymm hb hb', ((Xv h b).obj (q : Nat)).refl _⟩⟩

theorem canS_unique {a b q : Nat} {c : Nat → DHom q 1} {x : ((Xv h a).obj (q : Nat)).car}
    {y y' : ((Xv h b).obj (q : Nat)).car} (hy : CanS h hk0 hk Kw Kc a b q c x y)
    (hy' : CanS h hk0 hk Kw Kc a b q c x y') : ((Xv h b).obj (q : Nat)).eqv y y' :=
  match hy, hy' with
  | .inl d, .inl d' => desc_unique h hk0 hk d d'
  | .inl d, .inr ⟨ea, eb, hv⟩ => by
    subst ea; subst eb
    exact ((Xv h (n + 2)).obj (q : Nat)).symm (((Xv h (n + 2)).obj (q : Nat)).trans hv
      (phiB_box h hk0 hk Kw Kc (.inl d)))
  | .inr ⟨ea, eb, hv⟩, .inl d => by
    subst ea; subst eb
    exact ((Xv h (n + 2)).obj (q : Nat)).trans hv (phiB_box h hk0 hk Kw Kc (.inl d))
  | .inr ⟨ea, eb, hv⟩, .inr ⟨_, _, hv'⟩ => by
    subst ea; subst eb
    exact ((Xv h (n + 2)).obj (q : Nat)).trans hv (((Xv h (n + 2)).obj (q : Nat)).symm hv')

theorem canS_eqv {a b q : Nat} {c c' : Nat → DHom q 1} {x x' : ((Xv h a).obj (q : Nat)).car}
    {y : ((Xv h b).obj (q : Nat)).car} (hc : CEq a b c c') (hx : ((Xv h a).obj (q : Nat)).eqv x x')
    (hy : CanS h hk0 hk Kw Kc a b q c x y) : CanS h hk0 hk Kw Kc a b q c' x' y :=
  match hy with
  | .inl d => .inl (desc_eqv h hk0 hk hc hx (((Xv h b).obj (q : Nat)).refl _) d)
  | .inr ⟨ea, eb, hv⟩ => by
    subst ea; subst eb
    exact .inr ⟨rfl, rfl, ((Xv h (n + 2)).obj (q : Nat)).trans hv ((PhiB h hk0 hk Kw Kc).app_eqv q
      ⟨fun r hr => hc k hk0 hk r hr, fun t h1 h2 _ r hr => hc t h1 h2 r hr, hx⟩)⟩

theorem canS_mp {a b q' q : Nat} (g : DHom q' q) {c : Nat → DHom q 1} {x : ((Xv h a).obj (q : Nat)).car}
    {y : ((Xv h b).obj (q : Nat)).car} (hy : CanS h hk0 hk Kw Kc a b q c x y) :
    CanS h hk0 hk Kw Kc a b q' (fun u => dComp g (c u)) (mp (Xv h a) g x) (mp (Xv h b) g y) :=
  match hy with
  | .inl d => .inl (desc_mp h hk0 hk g d)
  | .inr ⟨ea, eb, hv⟩ => by
    subst ea; subst eb
    exact .inr ⟨rfl, rfl, ((Xv h (n + 2)).obj (q' : Nat)).trans (mp_eqv (Xv h (n + 2)) g hv)
      (((Xv h (n + 2)).obj (q' : Nat)).symm ((PhiB h hk0 hk Kw Kc).nat g (⟨c k⟩, (⟨c⟩, x))))⟩

/-- the maps of the filler -/
def PhiS (a b : Nat) (hab : a ≤ b) (hb : b ≤ n + 2) : PNat (SProd (CubeS.{w} a b) (Xv h a)) (Xv h b) where
  app q z := Kw.ch (CanS h hk0 hk Kw Kc a b q z.1.down z.2) (canS_ex h hk0 hk Kw Kc hab hb z.1.down z.2)
  app_eqv _ {z z'} e := canS_unique h hk0 hk Kw Kc
    (canS_eqv h hk0 hk Kw Kc e.1 e.2 (Kw.ch_spec _ (canS_ex h hk0 hk Kw Kc hab hb z.1.down z.2)))
    (Kw.ch_spec _ (canS_ex h hk0 hk Kw Kc hab hb z'.1.down z'.2))
  nat {_ _} g z := canS_unique h hk0 hk Kw Kc
    (Kw.ch_spec _ (canS_ex h hk0 hk Kw Kc hab hb (fun u => dComp g (z.1.down u)) (mp (Xv h a) g z.2)))
    (canS_mp h hk0 hk Kw Kc g (Kw.ch_spec _ (canS_ex h hk0 hk Kw Kc hab hb z.1.down z.2)))

theorem phiS_spec {a b : Nat} (hab : a ≤ b) (hb : b ≤ n + 2) {q : Nat} (c : Nat → DHom q 1)
    (x : ((Xv h a).obj (q : Nat)).car) :
    CanS h hk0 hk Kw Kc a b q c x ((PhiS h hk0 hk Kw Kc a b hab hb).app q (⟨c⟩, x)) :=
  Kw.ch_spec _ (canS_ex h hk0 hk Kw Kc hab hb c x)

/-- a value of phi_aa is the identity -/
theorem desc_unit {a q : Nat} {c : Nat → DHom q 1} {x : ((Xv h a).obj (q : Nat)).car}
    {y : ((Xv h a).obj (q : Nat)).car} (d : Desc h hk0 hk a a q c x y) : ((Xv h a).obj (q : Nat)).eqv y x :=
  match d with
  | ⟨s, a1, b1, hs, hsk, h1, h2, ea, eb, _, hv⟩ => by
    have e : b1 = a1 := sk_inj (eb.trans ea.symm)
    subst e; subst ea
    have m := (yF h hk0 hk s hs hsk).unit b1 h2 q ⟨fun u => c (sk s u)⟩ x
    have e2 : Dval h hk0 hk s hs hsk h1 h2 rfl eb c x =
        ((yF h hk0 hk s hs hsk).φ b1 b1 h1 h2).app q (⟨fun u => c (sk s u)⟩, x) := castS_self _ _
    rw [e2] at hv
    exact ((Xv h (sk s b1)).obj (q : Nat)).trans hv m

theorem kanXv : ∀ v, v ≤ n + 2 → Kan (Xv h v)
  | 0, _ => (hx h (n + 2)).kan 0 (Nat.zero_le _)
  | v + 1, hv => (hx h 0).kan v (Nat.le_of_succ_le_succ hv)

theorem phiS_comp (a t b : Nat) (hat : a < t) (htb : t < b) (hb : b ≤ n + 2) (q : Nat)
    (c : Nat → DHom q 1) (x : ((Xv h a).obj (q : Nat)).car) (hc : ∀ r, r ≤ q → (c t).f r = 1) :
    ((Xv h b).obj (q : Nat)).eqv
      ((PhiS h hk0 hk Kw Kc a b (Nat.le_of_lt (Nat.lt_trans hat htb)) hb).app q (⟨c⟩, x))
      ((PhiS h hk0 hk Kw Kc t b (Nat.le_of_lt htb) hb).app q
        (⟨c⟩, (PhiS h hk0 hk Kw Kc a t (Nat.le_of_lt hat) (Nat.le_trans (Nat.le_of_lt htb) hb)).app q (⟨c⟩, x))) :=
  have htn : t < n + 2 := Nat.lt_of_lt_of_le htb hb
  have d1 : Desc h hk0 hk a t q c x
      ((PhiS h hk0 hk Kw Kc a t (Nat.le_of_lt hat) (Nat.le_trans (Nat.le_of_lt htb) hb)).app q (⟨c⟩, x)) :=
    match phiS_spec h hk0 hk Kw Kc (Nat.le_of_lt hat) (Nat.le_trans (Nat.le_of_lt htb) hb) c x with
    | .inl d => d
    | .inr ⟨_, et, _⟩ => absurd et (ne_of_lt' htn)
  have d2 : Desc h hk0 hk t b q c
      ((PhiS h hk0 hk Kw Kc a t (Nat.le_of_lt hat) (Nat.le_trans (Nat.le_of_lt htb) hb)).app q (⟨c⟩, x))
      ((PhiS h hk0 hk Kw Kc t b (Nat.le_of_lt htb) hb).app q
        (⟨c⟩, (PhiS h hk0 hk Kw Kc a t (Nat.le_of_lt hat) (Nat.le_trans (Nat.le_of_lt htb) hb)).app q (⟨c⟩, x))) :=
    match phiS_spec h hk0 hk Kw Kc (Nat.le_of_lt htb) hb c _ with
    | .inl d => d
    | .inr ⟨et, _, _⟩ => absurd et (fun e => ne_of_lt' (Nat.lt_of_le_of_lt (Nat.zero_le a) hat) e.symm)
  match phiS_spec h hk0 hk Kw Kc (Nat.le_of_lt (Nat.lt_trans hat htb)) hb c x with
  | .inl d => desc_comp h hk0 hk hat htb hc d d1 d2
  | .inr ⟨ea, eb, hv⟩ => by
    subst ea; subst eb
    exact ((Xv h (n + 2)).obj (q : Nat)).trans hv
      (phiB_box h hk0 hk Kw Kc (.inr ⟨t, hat, htb, hc, _, d1, d2⟩))

/-- the filler of the inner horn -/
def sFill : SpX.{w} (n + 2) where
  X := Xv h
  kan := kanXv h
  φ a b hab hb := PhiS h hk0 hk Kw Kc a b hab hb
  unit a ha _ c x := match phiS_spec h hk0 hk Kw Kc (Nat.le_refl a) ha c.down x with
    | .inl d => desc_unit h hk0 hk d
    | .inr ⟨ea, eb, _⟩ => absurd (ea.symm.trans eb) (fun e => Nat.noConfusion e)
  comp a t b hat htb hb q c x hc := phiS_comp h hk0 hk Kw Kc a t b hat htb hb q c.down x hc

/-- the faces of the filler are the normal forms of the horn faces -/
theorem sFill_face (i : Nat) (hi : i ≤ n + 2) (hik : i ≠ k) :
    SpEq (pbS (face (n + 1) i) (sFill h hk0 hk Kw Kc)) (yF h hk0 hk i hi hik) :=
  ⟨fun _ _ => rfl, fun a b hab hb q C x =>
    canS_unique h hk0 hk Kw Kc (phiS_spec h hk0 hk Kw Kc (dmono (face (n + 1) i) hab hb) ((face (n + 1) i).bound b hb) _ x)
      (.inl ⟨i, a, b, hi, hik, hab, hb, rfl, rfl, fun _ _ _ hr => cm_face_out i a b C.down hr,
        ((yF h hk0 hk i hi hik).φ a b hab hb).app_eqv q
          ⟨fun _ h1 h2 _ hr => (cm_face_in i h1 h2 C.down hr).symm, ((Xv h (sk i a)).obj (q : Nat)).refl x⟩⟩)⟩

theorem sFill_fills : Fills h (sFill h hk0 hk Kw Kc) :=
  fun i hi hik => spEq_trans (sFill_face h hk0 hk Kw Kc i hi hik) (spEq_symm (yF_eq h hk0 hk i hi hik))

end fill

/-- the infinity-category of spaces: S = N^hc(Kan) is a quasi-category -/
theorem sp_quasi (Kw : ClW.{w}) (Kc : CP.Classical') : Quasi (SpS.{w}) :=
  fun _ _ hk0 hk h => ⟨sFill h hk0 hk Kw Kc, sFill_fills h hk0 hk Kw Kc⟩

end IC

end Found

#print axioms Found.IC.castS_self
#print axioms Found.IC.desc_box
#print axioms Found.IC.phiB_box
#print axioms Found.IC.canS_unique
#print axioms Found.IC.canS_eqv
#print axioms Found.IC.canS_mp
#print axioms Found.IC.PhiS
#print axioms Found.IC.desc_unit
#print axioms Found.IC.phiS_comp
#print axioms Found.IC.sFill
#print axioms Found.IC.sFill_face
#print axioms Found.IC.sFill_fills
#print axioms Found.IC.sp_quasi

found/IC47.lean

lean_all/found/IC47.lean

/-
  T153 ((infinity,1)-categories), part 47. found/IC47 — presheaves of
  spaces: for every simplicial set C (simplices in universe w+1), the
  simplicial set P(C) = Fun(C^op, S) of presheaves of spaces on C is a
  quasi-category (IC31 function complexes, IC32 opposites, IC46 S), inside
  the classical point at both universe levels.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.IC46

namespace Found

namespace IC

open Simp

universe w

/-- presheaves of spaces on C -/
def PshS (C : SSet.{w + 1}) : SSet.{w + 1} := FunS (Op C) SpS.{w}

/-- P(C) is a quasi-category -/
theorem psh_quasi (Kw : ClW.{w}) (Kw' : ClW.{w + 1}) (Kc : CP.Classical') (C : SSet.{w + 1}) :
    Quasi (PshS.{w} C) :=
  fun_quasi Kw' Kc (sp_quasi Kw Kc)

/-- diagrams in S indexed by any simplicial set form a quasi-category -/
theorem diag_quasi (Kw : ClW.{w}) (Kw' : ClW.{w + 1}) (Kc : CP.Classical') (K : SSet.{w + 1}) :
    Quasi (FunS K SpS.{w}) :=
  fun_quasi Kw' Kc (sp_quasi Kw Kc)

/-- the opposite of a quasi-category of presheaves is a quasi-category -/
theorem psh_op_quasi (Kw : ClW.{w}) (Kw' : ClW.{w + 1}) (Kc : CP.Classical') (C : SSet.{w + 1}) :
    Quasi (Op (PshS.{w} C)) :=
  op_quasi (psh_quasi Kw Kw' Kc C)

end IC

end Found

#print axioms Found.IC.PshS
#print axioms Found.IC.psh_quasi
#print axioms Found.IC.diag_quasi
#print axioms Found.IC.psh_op_quasi

found/IC48.lean

lean_all/found/IC48.lean

/-
  T153 ((infinity,1)-categories), part 48. found/IC48 — functors act on
  slices and coslices: F : X -> Y sends cones over p (and cocones under p)
  to cones over F ∘ p (cocones under F ∘ p).  With this: F preserves the
  limit (colimit) of p; finite simplicial sets (generated by finitely many
  simplices); kappa-filtered simplicial sets for a size predicate kappa (a
  free parameter: every kappa-small diagram has a cocone); preservation of
  finite limits and of kappa-filtered colimits.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.IC47

namespace Found

namespace IC

open Simp

universe w

section pres

variable {K X Y : SSet.{w}} (p : PNat K X) (F : PNat X Y)

/-- F on the slice: X_/p -> Y_/(F p) -/
def sliceMap : PNat (Slice p) (Slice (pcomp p F)) where
  app n s := {
    σ := F.app n s.σ
    ψ := fun m τ => F.app _ (s.ψ m τ)
    ψ_eqv := fun m {_ _} h => F.app_eqv _ (s.ψ_eqv m h)
    ψ_nat := fun {m _} g τ => (Y.obj _).trans (F.app_eqv _ (s.ψ_nat g τ)) (F.nat (J (dId (n : Nat)) g) (s.ψ m τ))
    ψ_K := fun m τ => (Y.obj _).trans ((Y.obj _).symm (F.nat (inK n m) (s.ψ m τ))) (F.app_eqv _ (s.ψ_K m τ))
    ψ_D := fun m τ => (Y.obj _).trans ((Y.obj _).symm (F.nat (inD n m) (s.ψ m τ))) (F.app_eqv _ (s.ψ_D m τ)) }
  app_eqv _ {_ _} h := ⟨F.app_eqv _ h.1, fun m τ => F.app_eqv _ (h.2 m τ)⟩
  nat δ s := ⟨F.nat δ s.σ, fun m _ => F.nat (J δ (dId m)) (s.ψ m _)⟩

/-- F on the coslice: X_p/ -> Y_(F p)/ -/
def cosliceMap : PNat (Coslice p) (Coslice (pcomp p F)) where
  app n s := {
    σ := F.app n s.σ
    ψ := fun m τ => F.app _ (s.ψ m τ)
    ψ_eqv := fun m {_ _} h => F.app_eqv _ (s.ψ_eqv m h)
    ψ_nat := fun {m _} g τ => (Y.obj _).trans (F.app_eqv _ (s.ψ_nat g τ)) (F.nat (J g (dId (n : Nat))) (s.ψ m τ))
    ψ_K := fun m τ => (Y.obj _).trans ((Y.obj _).symm (F.nat (inD m n) (s.ψ m τ))) (F.app_eqv _ (s.ψ_K m τ))
    ψ_D := fun m τ => (Y.obj _).trans ((Y.obj _).symm (F.nat (inK m n) (s.ψ m τ))) (F.app_eqv _ (s.ψ_D m τ)) }
  app_eqv _ {_ _} h := ⟨F.app_eqv _ h.1, fun m τ => F.app_eqv _ (h.2 m τ)⟩
  nat δ s := ⟨F.nat δ s.σ, fun m _ => F.nat (J (dId m) δ) (s.ψ m _)⟩

/-- F preserves the limits of p -/
def PresLim : Prop := ∀ c : Cone p, IsLimit p c → IsLimit (pcomp p F) ((sliceMap p F).app (0 : Nat) c)

/-- F preserves the colimits of p -/
def PresColim : Prop :=
  ∀ c : Cocone p, IsColimit p c → IsColimit (pcomp p F) ((cosliceMap p F).app (0 : Nat) c)

end pres

/-- finite simplicial sets: generated by finitely many simplices -/
def Finite (K : SSet.{w}) : Prop :=
  ∃ gens : List ((m : Nat) × (K.obj (m : Nat)).car), ∀ (q : Nat) (τ : (K.obj (q : Nat)).car),
    ∃ g, g ∈ gens ∧ ∃ δ : DHom q g.1, (K.obj (q : Nat)).eqv τ (mp K δ g.2)

/-- kappa-filtered simplicial sets, for a size predicate kappa: every kappa-small diagram has a cocone -/
def Filtered (κ : SSet.{w} → Prop) (K : SSet.{w}) : Prop :=
  ∀ (A : SSet.{w}), κ A → ∀ f : PNat A K, Nonempty (Cocone f)

/-- F preserves finite limits -/
def LeftExact {X Y : SSet.{w}} (F : PNat X Y) : Prop :=
  ∀ (K : SSet.{w}), Finite K → ∀ p : PNat K X, PresLim p F

/-- F preserves kappa-filtered colimits -/
def Accessible (κ : SSet.{w} → Prop) {X Y : SSet.{w}} (F : PNat X Y) : Prop :=
  ∀ (K : SSet.{w}), Filtered κ K → ∀ p : PNat K X, PresColim p F

/-- the identity -/
def idN (X : SSet.{w}) : PNat X X where
  app _ x := x
  app_eqv _ {_ _} h := h
  nat _ _ := (X.obj _).refl _

theorem id_presLim {K X : SSet.{w}} (p : PNat K X) : PresLim p (idN X) := fun _ h => h

theorem id_presColim {K X : SSet.{w}} (p : PNat K X) : PresColim p (idN X) := fun _ h => h

/-- the identity is left exact and accessible -/
theorem id_leftExact (X : SSet.{w}) : LeftExact (idN X) := fun _ _ p => id_presLim p

theorem id_accessible (κ : SSet.{w} → Prop) (X : SSet.{w}) : Accessible κ (idN X) := fun _ _ p => id_presColim p

end IC

end Found

#print axioms Found.IC.sliceMap
#print axioms Found.IC.cosliceMap
#print axioms Found.IC.PresLim
#print axioms Found.IC.Finite
#print axioms Found.IC.Filtered
#print axioms Found.IC.LeftExact
#print axioms Found.IC.Accessible
#print axioms Found.IC.id_presLim
#print axioms Found.IC.id_presColim
#print axioms Found.IC.id_leftExact
#print axioms Found.IC.id_accessible

found/IC49.lean

lean_all/found/IC49.lean

/-
  T153 ((infinity,1)-categories), part 49. found/IC49 — full
  subcategories: for a predicate P on the vertices of X (respecting
  equality), the simplicial set of simplices all of whose vertices satisfy
  P.  Every horn of the full subcategory that fills in X fills in the full
  subcategory (each vertex of the filler lies on a face of the horn, since
  a horn of dimension >= 2 has at least two faces besides the missing one);
  so full subcategories of quasi-categories (Kan complexes) are
  quasi-categories (Kan complexes).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.IC48

namespace Found

namespace IC

open Simp

universe w

/-- the vertex i of an n-simplex -/
def ptD (n i : Nat) (hi : i ≤ n) : DHom 0 n := ⟨fun _ => i, fun _ h => absurd h (Nat.not_lt_zero _), fun _ _ => hi⟩

section full

variable (X : SSet.{w}) (P : (V X).car → Prop) (hP : ∀ {x x' : (V X).car}, (V X).eqv x x' → P x → P x')

include hP in
theorem vtx_map {m n : Nat} (g : DHom m n) (σ : (X.obj (n : Nat)).car)
    (hσ : ∀ i (hi : i ≤ n), P (mp X (ptD n i hi) σ)) : ∀ i (hi : i ≤ m), P (mp X (ptD m i hi) (mp X g σ)) :=
  fun i hi => hP ((V X).symm ((V X).trans (mp_comp X _ _ _) (mp_resp X (g := ptD n (g.f i) (g.bound i hi))
    (fun _ _ => rfl) σ))) (hσ (g.f i) (g.bound i hi))

/-- the full subcategory on the vertices satisfying P -/
def FullSub : SSet.{w} where
  obj := fun (n : Nat) => ⟨{ σ : (X.obj (n : Nat)).car // ∀ i (hi : i ≤ n), P (mp X (ptD n i hi) σ) },
    fun a b => (X.obj (n : Nat)).eqv a.1 b.1, fun a => (X.obj (n : Nat)).refl a.1,
    fun h => (X.obj (n : Nat)).symm h, fun h k => (X.obj (n : Nat)).trans h k⟩
  map g σ := ⟨mp X g σ.1, vtx_map X P hP g σ.1 σ.2⟩
  map_eqv {_ _} g {_ _} h := X.map_eqv g h
  map_resp σ e := X.map_resp σ.1 e
  map_id n σ := X.map_id n σ.1
  map_comp f g σ := X.map_comp f g σ.1

/-- the inclusion of the full subcategory -/
def fullIncl : PNat (FullSub X P hP) X where
  app _ σ := σ.1
  app_eqv _ {_ _} h := h
  nat _ _ := (X.obj _).refl _

/-- a third index -/
theorem pick3 : ∀ (i k N : Nat), 2 ≤ N → ∃ j, j ≤ N ∧ j ≠ i ∧ j ≠ k
  | _ + 1, _ + 1, _, _ => ⟨0, Nat.zero_le _, fun e => Nat.noConfusion e, fun e => Nat.noConfusion e⟩
  | 0, 0, _, hN => ⟨1, Nat.le_trans (Nat.le_succ 1) hN, fun e => Nat.noConfusion e, fun e => Nat.noConfusion e⟩
  | 0, 1, _, hN => ⟨2, hN, fun e => Nat.noConfusion e, fun e => Nat.noConfusion (Nat.succ.inj e)⟩
  | 0, _ + 2, _, hN => ⟨1, Nat.le_trans (Nat.le_succ 1) hN, fun e => Nat.noConfusion e,
      fun e => Nat.noConfusion (Nat.succ.inj e)⟩
  | 1, 0, _, hN => ⟨2, hN, fun e => Nat.noConfusion (Nat.succ.inj e), fun e => Nat.noConfusion e⟩
  | _ + 2, 0, _, hN => ⟨1, Nat.le_trans (Nat.le_succ 1) hN, fun e => Nat.noConfusion (Nat.succ.inj e),
      fun e => Nat.noConfusion e⟩

/-- a horn of the full subcategory, as a horn of X -/
def hornIn {n k : Nat} (h : Horn (FullSub X P hP) n k) : Horn X n k :=
  ⟨fun i => (h.x i).1, fun i j a b c d => h.compat i j a b c d⟩

theorem dg_le_of {j i N : Nat} (hj : j ≤ N + 1) (hi : i ≤ N + 1) (hij : i ≠ j) : dg (Nat.pred j) i ≤ N :=
  match Nat.lt_or_ge (dg (Nat.pred j) i) (N + 1) with
  | .inl hl => Nat.le_of_lt_succ hl
  | .inr hg => absurd (Nat.le_trans (Nat.le_trans (Nat.le_of_eq (sk_ge (Nat.le_trans hj hg)).symm)
      (Nat.le_trans (Nat.le_of_eq (sk_dg hij)) hi)) hg) (Nat.not_succ_le_self _)

/-- a filler in X of a horn of the full subcategory lies in the full subcategory -/
theorem full_fill {n k : Nat} (h : Horn (FullSub X P hP) n k) (σ : (X.obj (n + 2 : Nat)).car)
    (hf : Fills (hornIn X P hP h) σ) : ∃ τ, Fills h τ :=
  ⟨⟨σ, fun i hi => match pick3 i k (n + 2) (Nat.le_add_left 2 n) with
    | ⟨j, hj, hji, hjk⟩ =>
      have hij : i ≠ j := fun e => hji e.symm
      have hi' : dg (Nat.pred j) i ≤ n + 1 := dg_le_of hj hi hij
      have hv := (h.x j).2 (dg (Nat.pred j) i) hi'
      hP ((V X).trans (mp_eqv X _ ((X.obj (n + 1 : Nat)).symm (hf j hj hjk)))
        ((V X).trans (mp_comp X _ _ _) (mp_resp X (g := ptD (n + 2) i hi) (fun _ _ => sk_dg hij) σ))) hv⟩,
    fun i hi hik => hf i hi hik⟩

theorem quasi_full (hQ : Quasi X) : Quasi (FullSub X P hP) :=
  fun n k h0 hk h => match hQ n k h0 hk (hornIn X P hP h) with
    | ⟨σ, hf⟩ => full_fill X P hP h σ hf

theorem kan_full (hK : Kan X) : Kan (FullSub X P hP) :=
  fun n k hk h => match hK n k hk (hornIn X P hP h) with
    | ⟨σ, hf⟩ => full_fill X P hP h σ hf

end full

end IC

end Found

#print axioms Found.IC.vtx_map
#print axioms Found.IC.FullSub
#print axioms Found.IC.pick3
#print axioms Found.IC.full_fill
#print axioms Found.IC.quasi_full
#print axioms Found.IC.kan_full

found/IC5.lean

lean_all/found/IC5.lean

/-
  T153 ((infinity,1)-categories), part 5. found/IC5 — terminal and initial
  objects of a quasi-category (Joyal): y is terminal when every vertex has
  an edge to y and every boundary ∂Delta^(n+2) whose last vertex is y fills.
  Then any two edges x -> y are homotopic, so y is terminal in the homotopy
  category (found/Cat Terminal); any two terminal objects are equivalent.
  Dually for initial objects (first vertex).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.IC4

namespace Found

namespace IC

open Simp

universe w

/-- the constant maps [0] -> [n] at n (last vertex) and at 0 (first vertex) -/
def cstL (n : Nat) : DHom 0 n := ⟨fun _ => n, fun _ h => absurd h (Nat.not_lt_zero _), fun _ _ => Nat.le_refl n⟩
def cst0 (n : Nat) : DHom 0 n := ⟨fun _ => 0, fun _ h => absurd h (Nat.not_lt_zero _), fun _ _ => Nat.zero_le n⟩

variable {X : SSet.{w}}

def lastV {n : Nat} (s : (X.obj (n : Nat)).car) : (V X).car := X.map (a := (0 : Nat)) (b := (n : Nat)) (cstL n) s
def firstV {n : Nat} (s : (X.obj (n : Nat)).car) : (V X).car := X.map (a := (0 : Nat)) (b := (n : Nat)) (cst0 n) s

/-- a boundary ∂Delta^(n+2): faces x_i (i <= n+2) agreeing on common faces -/
structure Bdry (X : SSet.{w}) (n : Nat) where
  x : Nat → (X.obj (n + 1 : Nat)).car
  compat : ∀ i j, i ≤ j → j ≤ n + 1 → (X.obj (n : Nat)).eqv (dd X i (x (j + 1))) (dd X j (x i))

def BFills {n : Nat} (b : Bdry X n) (σ : (X.obj (n + 2 : Nat)).car) : Prop :=
  ∀ i, i ≤ n + 2 → (X.obj (n + 1 : Nat)).eqv (dd X i σ) (b.x i)

/-- terminal objects -/
def Term (y : (V X).car) : Prop :=
  (∀ x : (V X).car, Nonempty (Mor x y)) ∧
  ∀ n (b : Bdry X n), (V X).eqv (lastV (b.x 0)) y → ∃ σ, BFills b σ

/-- initial objects -/
def Init (x : (V X).car) : Prop :=
  (∀ y : (V X).car, Nonempty (Mor x y)) ∧
  ∀ n (b : Bdry X n), (V X).eqv (firstV (b.x (n + 2))) x → ∃ σ, BFills b σ

/-- the boundary of Delta^2 with d0 = a0, d1 = a1, d2 = a2 -/
def b20 (a0 a1 a2 : (E X).car) (c00 : (V X).eqv (dd X 0 a1) (dd X 0 a0))
    (c01 : (V X).eqv (dd X 0 a2) (dd X 1 a0)) (c11 : (V X).eqv (dd X 1 a2) (dd X 1 a1)) : Bdry X 0 where
  x i := match i with
    | 0 => a0
    | 1 => a1
    | _ + 2 => a2
  compat i j hij hj := match i, j, hij, hj with
    | 0, 0, _, _ => c00
    | 0, 1, _, _ => c01
    | 1, 0, h, _ => absurd h (Nat.not_succ_le_zero 0)
    | 1, 1, _, _ => c11
    | 0, _ + 2, _, h => absurd (Nat.le_of_succ_le_succ h) (Nat.not_succ_le_zero _)
    | 1, _ + 2, _, h => absurd (Nat.le_of_succ_le_succ h) (Nat.not_succ_le_zero _)
    | _ + 2, _, h, hj => absurd (Nat.le_of_succ_le_succ (Nat.le_trans h hj)) (Nat.not_succ_le_zero _)

theorem lastV_idv (y : (V X).car) : (V X).eqv (lastV (idv X y)) y :=
  map_cid X (cstL 1) (degen 0 0 (Nat.le_refl 0)) (le0_cases rfl) y

theorem firstV_idv (y : (V X).car) : (V X).eqv (firstV (idv X y)) y :=
  map_cid X (cst0 1) (degen 0 0 (Nat.le_refl 0)) (le0_cases rfl) y

/-- into a terminal object all edges are homotopic -/
theorem term_hty {y : (V X).car} (hT : Term y) {x : (V X).car} (f g : Mor x y) : Hty f g :=
  match hT.2 0 (b20 (idv X y) g.1 f.1 ((V X).trans g.2.2 ((V X).symm (d0_idv X y)))
      ((V X).trans f.2.2 ((V X).symm (d1_idv X y))) ((V X).trans f.2.1 ((V X).symm g.2.1)))
      (lastV_idv y) with
  | ⟨σ, hσ⟩ => ⟨σ, hσ 2 (Nat.le_refl 2), hσ 0 (Nat.zero_le 2), hσ 1 (Nat.le_succ 1)⟩

variable (hQ : Quasi X) (c2 : (h : Horn X 0 1) → { σ // Fills h σ })

include hQ in
/-- out of an initial object all edges are homotopic -/
theorem init_hty {x : (V X).car} (hI : Init x) {y : (V X).car} (f g : Mor x y) : Hty f g :=
  match hI.2 0 (b20 g.1 f.1 (idv X x) ((V X).trans f.2.2 ((V X).symm g.2.2))
      ((V X).trans (d0_idv X x) ((V X).symm g.2.1)) ((V X).trans (d1_idv X x) ((V X).symm f.2.1)))
      (firstV_idv x) with
  | ⟨σ, hσ⟩ =>
    have t : Tri X (idv X x) g.1 f.1 := ⟨σ, hσ 2 (Nat.le_refl 2), hσ 0 (Nat.zero_le 2), hσ 1 (Nat.le_succ 1)⟩
    hty_symm hQ g.2.2 (tri_uniq hQ g.2.2 (triL' g.1 g.2.1) t)

/-- a terminal object of a quasi-category is terminal in its homotopy category -/
def termH {y : (V X).car} (hT : Term y) (bang : (x : (V X).car) → Mor x y) : Terminal (hCat hQ c2) where
  one := y
  bang := bang
  uniq f := term_hty hT f (bang _)

/-- an initial object of a quasi-category is initial in its homotopy category -/
theorem initH {x : (V X).car} (hI : Init x) {y : (V X).car} (f g : (hCat hQ c2).Hom x y) :
    (hCat hQ c2).heq f g := init_hty hQ hI f g

/-- terminal objects are unique up to equivalence -/
theorem term_unique {y y' : (V X).car} (hT : Term y) (hT' : Term y') :
    ∃ f : Mor y y', IsEquiv c2 hQ f :=
  match hT'.1 y, hT.1 y' with
  | ⟨f⟩, ⟨g⟩ => ⟨f, g, term_hty hT _ _, term_hty hT' _ _⟩

/-- initial objects are unique up to equivalence -/
theorem init_unique {x x' : (V X).car} (hI : Init x) (hI' : Init x') :
    ∃ f : Mor x x', IsEquiv c2 hQ f :=
  match hI.1 x', hI'.1 x with
  | ⟨f⟩, ⟨g⟩ => ⟨f, g, init_hty hQ hI _ _, init_hty hQ hI' _ _⟩

end IC

end Found

#print axioms Found.IC.term_hty
#print axioms Found.IC.init_hty
#print axioms Found.IC.termH
#print axioms Found.IC.term_unique
#print axioms Found.IC.init_unique

found/IC50.lean

lean_all/found/IC50.lean

/-
  T153 ((infinity,1)-categories), part 50. found/IC50 — localizations,
  presentable infinity-categories and Grothendieck infinity-topoi.
  * equivalences (edges with a two-sided inverse up to 2-simplices),
    natural transformations as maps Delta^1 x X -> Y, natural equivalences,
    categorical equivalences of simplicial sets;
  * localization functors (L, alpha : id -> L with L(alpha_x) and
    alpha_(Lx) equivalences), local objects, the full subcategory of local
    objects (a quasi-category when X is);
  * presentable (kappa): equivalent to the local objects of an accessible
    localization of P(C), C kappa-small; Grothendieck infinity-topos: the
    same with a left exact localization (kappa and smallness are free
    parameters);
  * P(C) itself is a Grothendieck infinity-topos (the identity
    localization), hence presentable; every topos is presentable.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.IC49

namespace Found

namespace IC

open Simp

universe w

/-! ## equivalences and natural transformations -/

/-- an equivalence: an edge with a two-sided inverse up to 2-simplices -/
def EquivE {X : SSet.{w}} {x y : (V X).car} (f : Mor (X := X) x y) : Prop :=
  ∃ g : Mor (X := X) y x, Tri X f.1 g.1 (idv X x) ∧ Tri X g.1 f.1 (idv X y)

/-- the component of a transformation H : Delta^1 x X -> Y at a vertex -/
def cmpE {X Y : SSet.{w}} (H : PNat (SProd (Simp.{w} 1) X) Y) (x : (V X).car) : (E Y).car :=
  H.app (1 : Nat) (⟨dId 1⟩, idv X x)

/-- H : F => G is a natural equivalence -/
def NatEquiv {X Y : SSet.{w}} (F G : PNat X Y) (H : PNat (SProd (Simp.{w} 1) X) Y) : Prop :=
  (∀ (n : Nat) x, (Y.obj (n : Nat)).eqv (H.app n (⟨c0D n⟩, x)) (F.app n x)) ∧
  (∀ (n : Nat) x, (Y.obj (n : Nat)).eqv (H.app n (⟨c1D n⟩, x)) (G.app n x)) ∧
  ∀ x : (V X).car, ∃ f : Mor (X := Y) (F.app (0 : Nat) x) (G.app (0 : Nat) x), (E Y).eqv f.1 (cmpE H x) ∧ EquivE f

/-- categorical equivalence -/
def CatEquiv (X Y : SSet.{w}) : Prop :=
  ∃ (F : PNat X Y) (G : PNat Y X) (H1 : PNat (SProd (Simp.{w} 1) X) X) (H2 : PNat (SProd (Simp.{w} 1) Y) Y),
    NatEquiv (idN X) (pcomp F G) H1 ∧ NatEquiv (idN Y) (pcomp G F) H2

/-- identities are equivalences -/
theorem idv_equiv {X : SSet.{w}} {x y : (V X).car} (h : (V X).eqv x y) :
    ∃ f : Mor (X := X) x y, (E X).eqv f.1 (idv X x) ∧ EquivE f :=
  ⟨⟨idv X x, d1_idv X x, (V X).trans (d0_idv X x) h⟩, (E X).refl _,
    ⟨⟨idv X x, (V X).trans (d1_idv X x) h, d0_idv X x⟩, tri_ids x,
      tri_resp (tri_ids x) ((E X).refl _) ((E X).refl _) (idv_eqv h)⟩⟩

/-- the projection Delta^1 x X -> X -/
def prS (X : SSet.{w}) : PNat (SProd (Simp.{w} 1) X) X where
  app _ z := z.2
  app_eqv _ {_ _} h := h.2
  nat _ _ := (X.obj _).refl _

/-! ## localizations -/

/-- a localization functor: alpha : id => L with L(alpha_x) and alpha_(Lx) equivalences -/
structure Loc (X : SSet.{w}) where
  L : PNat X X
  H : PNat (SProd (Simp.{w} 1) X) X
  h0 : ∀ (n : Nat) x, (X.obj (n : Nat)).eqv (H.app n (⟨c0D n⟩, x)) x
  h1 : ∀ (n : Nat) x, (X.obj (n : Nat)).eqv (H.app n (⟨c1D n⟩, x)) (L.app n x)
  idem1 : ∀ x : (V X).car, ∃ f : Mor (X := X) (L.app (0 : Nat) x) (L.app (0 : Nat) (L.app (0 : Nat) x)),
    (E X).eqv f.1 (cmpE H (L.app (0 : Nat) x)) ∧ EquivE f
  idem2 : ∀ x : (V X).car, ∃ f : Mor (X := X) (L.app (0 : Nat) x) (L.app (0 : Nat) (L.app (0 : Nat) x)),
    (E X).eqv f.1 (L.app (1 : Nat) (cmpE H x)) ∧ EquivE f

variable {X : SSet.{w}}

/-- local objects: alpha_x is an equivalence -/
def Local (Lc : Loc X) (x : (V X).car) : Prop :=
  ∃ f : Mor (X := X) x (Lc.L.app (0 : Nat) x), (E X).eqv f.1 (cmpE Lc.H x) ∧ EquivE f

theorem local_eqv (Lc : Loc X) {x x' : (V X).car} (h : (V X).eqv x x') (hl : Local Lc x) : Local Lc x' :=
  match hl with
  | ⟨f, hf, ⟨g, t1, t2⟩⟩ =>
    have hL : (V X).eqv (Lc.L.app (0 : Nat) x) (Lc.L.app (0 : Nat) x') := Lc.L.app_eqv _ h
    ⟨⟨f.1, (V X).trans f.2.1 h, (V X).trans f.2.2 hL⟩,
      (E X).trans hf (Lc.H.app_eqv _ ⟨fun _ _ => rfl, idv_eqv h⟩),
      ⟨⟨g.1, (V X).trans g.2.1 hL, (V X).trans g.2.2 h⟩,
        tri_resp t1 ((E X).refl _) ((E X).refl _) (idv_eqv h),
        tri_resp t2 ((E X).refl _) ((E X).refl _) (idv_eqv hL)⟩⟩

/-- the full subcategory of local objects -/
def LocSub (Lc : Loc X) : SSet.{w} := FullSub X (Local Lc) (local_eqv Lc)

theorem locSub_quasi (Lc : Loc X) (hQ : Quasi X) : Quasi (LocSub Lc) := quasi_full X _ _ hQ

variable (X)

/-- the identity localization -/
def idLoc : Loc X where
  L := idN X
  H := prS X
  h0 _ _ := (X.obj _).refl _
  h1 _ _ := (X.obj _).refl _
  idem1 x := idv_equiv ((V X).refl x)
  idem2 x := idv_equiv ((V X).refl x)

theorem local_id (x : (V X).car) : Local (idLoc X) x := idv_equiv ((V X).refl x)

/-- a full subcategory containing every vertex is equivalent to X -/
theorem catEquiv_full_all (P : (V X).car → Prop) (hP : ∀ {x x' : (V X).car}, (V X).eqv x x' → P x → P x')
    (hall : ∀ x, P x) : CatEquiv X (FullSub X P hP) :=
  ⟨{ app := fun _ σ => ⟨σ, fun _ _ => hall _⟩, app_eqv := fun _ {_ _} h => h, nat := fun _ _ => (X.obj _).refl _ },
    fullIncl X P hP, prS X, prS (FullSub X P hP),
    ⟨fun _ _ => (X.obj _).refl _, fun _ _ => (X.obj _).refl _, fun x => idv_equiv ((V X).refl x)⟩,
    ⟨fun _ _ => (X.obj _).refl _, fun _ _ => (X.obj _).refl _,
      fun x => idv_equiv (X := FullSub X P hP) ((V X).refl x.1)⟩⟩

variable {X}

/-! ## presentable infinity-categories and Grothendieck infinity-topoi -/

/-- presentable: the local objects of an accessible localization of P(C), C kappa-small -/
def Presentable (κ : SSet.{w + 1} → Prop) (Y : SSet.{w + 1}) : Prop :=
  ∃ C : SSet.{w + 1}, κ C ∧ ∃ Lc : Loc (PshS.{w} C), Accessible κ Lc.L ∧ CatEquiv Y (LocSub Lc)

/-- Grothendieck infinity-topos: the local objects of an accessible left exact localization of P(C) -/
def GrothTopos (κ : SSet.{w + 1} → Prop) (Y : SSet.{w + 1}) : Prop :=
  ∃ C : SSet.{w + 1}, κ C ∧ ∃ Lc : Loc (PshS.{w} C), Accessible κ Lc.L ∧ LeftExact Lc.L ∧ CatEquiv Y (LocSub Lc)

/-- every Grothendieck infinity-topos is presentable -/
theorem topos_presentable {κ : SSet.{w + 1} → Prop} {Y : SSet.{w + 1}} (h : GrothTopos κ Y) : Presentable κ Y :=
  match h with
  | ⟨C, hC, Lc, ha, _, he⟩ => ⟨C, hC, Lc, ha, he⟩

/-- presheaves of spaces on a kappa-small simplicial set form a Grothendieck infinity-topos -/
theorem psh_topos (κ : SSet.{w + 1} → Prop) (C : SSet.{w + 1}) (hC : κ C) : GrothTopos κ (PshS.{w} C) :=
  ⟨C, hC, idLoc _, id_accessible κ _, id_leftExact _,
    catEquiv_full_all _ (Local (idLoc _)) (local_eqv (idLoc _)) (local_id _)⟩

/-- the local objects of a localization of P(C) form a quasi-category -/
theorem locSub_psh_quasi (Kw : ClW.{w}) (Kw' : ClW.{w + 1}) (Kc : CP.Classical') {C : SSet.{w + 1}}
    (Lc : Loc (PshS.{w} C)) : Quasi (LocSub Lc) :=
  locSub_quasi Lc (psh_quasi Kw Kw' Kc C)

end IC

end Found

#print axioms Found.IC.idv_equiv
#print axioms Found.IC.local_eqv
#print axioms Found.IC.locSub_quasi
#print axioms Found.IC.idLoc
#print axioms Found.IC.local_id
#print axioms Found.IC.catEquiv_full_all
#print axioms Found.IC.topos_presentable
#print axioms Found.IC.psh_topos
#print axioms Found.IC.locSub_psh_quasi

found/IC51.lean

lean_all/found/IC51.lean

/-
  T153 ((infinity,1)-categories), part 51. found/IC51 — limits in full
  subcategories: a cone in a full subcategory whose image is a limit in X is
  a limit in the full subcategory (cones of X over a diagram of the full
  subcategory, with vertex in it, lie in it: the vertices of a simplex of
  the slice are vertices of its cone point or of the diagram).  So the
  limits of an infinity-topos LocSub L that exist in P(C) and are local are
  limits in the topos.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.IC50

namespace Found

namespace IC

open Simp

universe w

section fulllim

variable {X : SSet.{w}} {P : (V X).car → Prop} {hP : ∀ {x x' : (V X).car}, (V X).eqv x x' → P x → P x'}
  {K : SSet.{w}} (p : PNat K (FullSub X P hP))

/-- the diagram in X -/
abbrev pI : PNat K X := pcomp p (fullIncl X P hP)

include hP in
/-- the vertices of a slice simplex are those of its cone point and of the diagram -/
theorem psi_vert {n m : Nat} (s : SlS (pI p) n) (hs : ∀ i (hi : i ≤ n), P (mp X (ptD n i hi) s.σ))
    (τ : (K.obj (m : Nat)).car) : ∀ i (hi : i ≤ n + 1 + m), P (mp X (ptD (n + 1 + m) i hi) (s.ψ m τ)) :=
  fun i hi => match split_ic (n + 1) i with
  | .inl hl =>
    have hin : i ≤ n := Nat.le_of_lt_succ hl
    hP ((V X).symm ((V X).trans (mp_resp X (g := dComp (ptD n i hin) (inD n m)) (fun _ _ => rfl) (s.ψ m τ))
      ((V X).trans ((V X).symm (mp_comp X _ _ _)) (mp_eqv X _ (s.ψ_D m τ))))) (hs i hin)
  | .inr ⟨j, e⟩ =>
    have hjm : j ≤ m := le_cancel_r (n + 1) (by rw [← e, Nat.add_comm m (n + 1)]; exact hi)
    hP ((V X).symm ((V X).trans (mp_resp X (g := dComp (ptD m j hjm) (inK n m)) (fun _ _ => e) (s.ψ m τ))
      ((V X).trans ((V X).symm (mp_comp X _ _ _)) (mp_eqv X _ (s.ψ_K m τ))))) ((p.app (m : Nat) τ).2 j hjm)

/-- a slice simplex of X with cone point in the full subcategory lies in it -/
def liftSl {n : Nat} (s : SlS (pI p) n) (hs : ∀ i (hi : i ≤ n), P (mp X (ptD n i hi) s.σ)) : SlS p n where
  σ := ⟨s.σ, hs⟩
  ψ m τ := ⟨s.ψ m τ, psi_vert p s hs τ⟩
  ψ_eqv m {_ _} h := s.ψ_eqv m h
  ψ_nat g τ := s.ψ_nat g τ
  ψ_K m τ := s.ψ_K m τ
  ψ_D m τ := s.ψ_D m τ

include hP in
/-- the cone point lies in the full subcategory when all its faces do -/
theorem sig_full {n : Nat} (σ : (X.obj (n + 1 : Nat)).car)
    (hf : ∀ j, j ≤ n + 1 → ∀ i' (hi' : i' ≤ n), P (mp X (ptD n i' hi') (mp X (face n j) σ))) :
    ∀ i (hi : i ≤ n + 1), P (mp X (ptD (n + 1) i hi) σ) :=
  fun i hi =>
    have hj : ∃ j, j ≤ n + 1 ∧ i ≠ j := match i with
      | 0 => ⟨1, Nat.succ_le_succ (Nat.zero_le n), fun e => Nat.noConfusion e⟩
      | _ + 1 => ⟨0, Nat.zero_le _, fun e => Nat.noConfusion e⟩
    match hj with
    | ⟨j, hjn, hij⟩ =>
      hP ((V X).trans (mp_comp X _ _ _) (mp_resp X (g := ptD (n + 1) i hi) (fun _ _ => sk_dg hij) σ))
        (hf j hjn _ (dg_le_of hjn hi hij))

include hP in
theorem vert_of_eqv {n : Nat} {σ σ' : (X.obj (n : Nat)).car} (e : (X.obj (n : Nat)).eqv σ σ')
    (h : ∀ i (hi : i ≤ n), P (mp X (ptD n i hi) σ')) : ∀ i (hi : i ≤ n), P (mp X (ptD n i hi) σ) :=
  fun i hi => hP ((V X).symm (mp_eqv X _ e)) (h i hi)

/-- full subcategories reflect limits -/
theorem lim_full (c : Cone p) (h : IsLimit (pI p) ((sliceMap p (fullIncl X P hP)).app (0 : Nat) c)) :
    IsLimit p c :=
  ⟨fun x => match h.1 ((sliceMap p (fullIncl X P hP)).app (0 : Nat) x) with
    | ⟨f⟩ => ⟨⟨liftSl p f.1 (sig_full (hP := hP) f.1.σ (le1_cases
        (fun i' hi' => vert_of_eqv (hP := hP) f.2.2.1 c.σ.2 i' hi')
        (fun i' hi' => vert_of_eqv (hP := hP) f.2.1.1 x.σ.2 i' hi'))), f.2.1, f.2.2⟩⟩,
   fun n b hl =>
    match h.2 n (bdryMap (sliceMap p (fullIncl X P hP)) b)
        ((V (Slice (pI p))).trans ((V (Slice (pI p))).symm ((sliceMap p (fullIncl X P hP)).nat (cstL (n + 1)) (b.x 0)))
          ((sliceMap p (fullIncl X P hP)).app_eqv _ hl)) with
    | ⟨σ, hσ⟩ => ⟨liftSl p σ (sig_full (hP := hP) σ.σ (fun j hj i' hi' =>
          vert_of_eqv (hP := hP) (hσ j hj).1 (b.x j).σ.2 i' hi')), hσ⟩⟩

end fulllim

end IC

end Found

#print axioms Found.IC.psi_vert
#print axioms Found.IC.liftSl
#print axioms Found.IC.sig_full
#print axioms Found.IC.lim_full

found/IC6.lean

lean_all/found/IC6.lean

/-
  T153 ((infinity,1)-categories), part 6. found/IC6 — joins of maps in the
  simplex category: for x : [a'] -> [a] and y : [b'] -> [b] the join
  x * y : [a'+1+b'] -> [a+1+b] acts as x on the first a'+1 vertices and as
  y (shifted by a+1) on the rest.  It is monotone and bounded, respects
  pointwise equality, preserves identities and composites — the
  combinatorics behind slices X_/p and coslices X_p/ (limits, colimits).
  Defined by structural recursion (no subtraction).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.IC5

namespace Found

namespace IC

open Simp

/-- jf N d g c: the first c values from d, then N + g -/
def jf (N : Nat) (d g : Nat → Nat) : Nat → Nat → Nat
  | 0, i => N + g i
  | _ + 1, 0 => d 0
  | c + 1, i + 1 => jf N (fun t => d (t + 1)) g c i

theorem jf_front (N : Nat) (g : Nat → Nat) : (c : Nat) → (d : Nat → Nat) → (i : Nat) → i < c → jf N d g c i = d i
  | 0, _, i, h => absurd h (Nat.not_lt_zero i)
  | _ + 1, _, 0, _ => rfl
  | c + 1, d, i + 1, h => jf_front N g c (fun t => d (t + 1)) i (Nat.lt_of_succ_lt_succ h)

theorem jf_back (N : Nat) (g : Nat → Nat) : (c : Nat) → (d : Nat → Nat) → (j : Nat) → jf N d g c (j + c) = N + g j
  | 0, _, _ => rfl
  | c + 1, d, j => jf_back N g c (fun t => d (t + 1)) j

theorem split_ic : (c i : Nat) → i < c ∨ ∃ j, i = j + c
  | 0, i => .inr ⟨i, rfl⟩
  | c + 1, 0 => .inl (Nat.succ_pos c)
  | c + 1, i + 1 => match split_ic c i with
    | .inl h => .inl (Nat.succ_lt_succ h)
    | .inr ⟨j, e⟩ => .inr ⟨j, congrArg Nat.succ e⟩

theorem not_back_lt {j c : Nat} (h : j + c < c) : False :=
  Nat.lt_irrefl c (Nat.lt_of_le_of_lt (Nat.le_add_left c j) h)

theorem le_cancel_r {j b : Nat} : (c : Nat) → j + c ≤ b + c → j ≤ b
  | 0, h => h
  | c + 1, h => le_cancel_r c (Nat.le_of_succ_le_succ h)

theorem add_cancel_r {j j' : Nat} : (c : Nat) → j + c = j' + c → j = j'
  | 0, h => h
  | c + 1, h => add_cancel_r c (Nat.noConfusion h id)

theorem back_le {j c b : Nat} (h : j + c ≤ c + b) : j ≤ b := by
  rw [Nat.add_comm c b] at h
  exact le_cancel_r c h

theorem back_lt {j c b : Nat} (h : j + c < c + b) : j < b := by
  rw [Nat.add_comm c b] at h
  exact Nat.lt_of_add_lt_add_right h

variable {a' a b' b : Nat}

/-- the join of two maps of the simplex category -/
def jmap (x : DHom a' a) (y : DHom b' b) (i : Nat) : Nat := jf (a + 1) x.f y.f (a' + 1) i

theorem jmap_front (x : DHom a' a) (y : DHom b' b) {i : Nat} (h : i < a' + 1) : jmap x y i = x.f i :=
  jf_front (a + 1) y.f (a' + 1) x.f i h

theorem jmap_back (x : DHom a' a) (y : DHom b' b) (j : Nat) : jmap x y (j + (a' + 1)) = a + 1 + y.f j :=
  jf_back (a + 1) y.f (a' + 1) x.f j

theorem front_le_back (x : DHom a' a) {i : Nat} (h : i < a' + 1) (k : Nat) : x.f i ≤ a + 1 + k :=
  Nat.le_trans (x.bound i (Nat.le_of_lt_succ h)) (Nat.le_trans (Nat.le_succ a) (Nat.le_add_right _ k))

theorem jmap_mono (x : DHom a' a) (y : DHom b' b) (i : Nat) (hi : i < a' + 1 + b') :
    jmap x y i ≤ jmap x y (i + 1) :=
  match split_ic (a' + 1) i, split_ic (a' + 1) (i + 1) with
  | .inl h, .inl h' => by
    rw [jmap_front x y h, jmap_front x y h']
    exact x.mono i (Nat.lt_of_succ_lt_succ h')
  | .inl h, .inr ⟨j', e'⟩ => by
    rw [jmap_front x y h, e', jmap_back x y j']
    exact front_le_back x h _
  | .inr ⟨j, e⟩, .inl h' => absurd (Nat.lt_of_succ_lt (e ▸ h')) (fun h'' => not_back_lt h'')
  | .inr ⟨j, e⟩, .inr ⟨j', e'⟩ => by
    have ej : j' = j + 1 := add_cancel_r (a' + 1) (e'.symm.trans (by rw [e, Nat.succ_add]))
    rw [e', jmap_back x y j', e, jmap_back x y j, ej]
    exact Nat.add_le_add_left (y.mono j (back_lt (e ▸ hi))) _

theorem jmap_bound (x : DHom a' a) (y : DHom b' b) (i : Nat) (hi : i ≤ a' + 1 + b') : jmap x y i ≤ a + 1 + b :=
  match split_ic (a' + 1) i with
  | .inl h => by rw [jmap_front x y h]; exact front_le_back x h b
  | .inr ⟨j, e⟩ => by
    rw [e, jmap_back x y j]
    exact Nat.add_le_add_left (y.bound j (back_le (e ▸ hi))) _

def J (x : DHom a' a) (y : DHom b' b) : DHom (a' + 1 + b') (a + 1 + b) :=
  ⟨jmap x y, jmap_mono x y, jmap_bound x y⟩

/-- identities and composites of the simplex category, as maps (definitionally those of Delta) -/
def dId (n : Nat) : DHom n n := ⟨fun i => i, fun _ _ => Nat.le_succ _, fun _ h => h⟩

def dComp {n m k : Nat} (x : DHom n m) (y : DHom m k) : DHom n k :=
  ⟨fun i => y.f (x.f i), fun i hi => Simp.Delta.mono_of_succ y (x.mono i hi) (x.bound (i + 1) hi),
    fun i hi => y.bound _ (x.bound i hi)⟩

/-- J respects pointwise equality -/
theorem J_resp {x x' : DHom a' a} {y y' : DHom b' b} (hx : ∀ i, i ≤ a' → x.f i = x'.f i)
    (hy : ∀ j, j ≤ b' → y.f j = y'.f j) : ∀ i, i ≤ a' + 1 + b' → (J x y).f i = (J x' y').f i := fun i hi =>
  match split_ic (a' + 1) i with
  | .inl h => (jmap_front x y h).trans ((hx i (Nat.le_of_lt_succ h)).trans (jmap_front x' y' h).symm)
  | .inr ⟨j, e⟩ => by
    show jmap x y i = jmap x' y' i
    rw [e, jmap_back x y j, jmap_back x' y' j, hy j (back_le (e ▸ hi))]

/-- J preserves identities -/
theorem J_id (a' b' : Nat) : ∀ i, i ≤ a' + 1 + b' →
    (J (dId a') (dId b')).f i = i := fun i _ =>
  match split_ic (a' + 1) i with
  | .inl h => jmap_front _ _ h
  | .inr ⟨j, e⟩ => by
    show jmap _ _ i = i
    rw [e, jmap_back]
    exact Nat.add_comm _ _

variable {a'' b'' : Nat}

/-- J preserves composites -/
theorem J_comp (x : DHom a'' a') (x2 : DHom a' a) (y : DHom b'' b') (y2 : DHom b' b) :
    ∀ i, i ≤ a'' + 1 + b'' → (J x2 y2).f ((J x y).f i) =
      (J (dComp x x2) (dComp y y2)).f i := fun i _ =>
  match split_ic (a'' + 1) i with
  | .inl h => by
    show jmap x2 y2 (jmap x y i) = jmap _ _ i
    rw [jmap_front x y h, jmap_front _ _ h, jmap_front x2 y2 (Nat.lt_succ_of_le (x.bound i (Nat.le_of_lt_succ h)))]
    rfl
  | .inr ⟨j, e⟩ => by
    show jmap x2 y2 (jmap x y i) = jmap _ _ i
    rw [e, jmap_back x y j, jmap_back _ _ j, Nat.add_comm (a' + 1), jmap_back x2 y2]
    rfl

end IC

end Found

#print axioms Found.IC.jmap_mono
#print axioms Found.IC.J
#print axioms Found.IC.J_resp
#print axioms Found.IC.J_id
#print axioms Found.IC.J_comp

found/IC7.lean

lean_all/found/IC7.lean

/-
  T153 ((infinity,1)-categories), part 7. found/IC7 — slices: for a diagram
  p : K -> X of simplicial sets, the slice X_/p is the simplicial set whose
  n-simplices are maps Delta^n * K -> X extending p, written out: a simplex
  σ of X_n and, for every m-simplex τ of K, an (n+1+m)-simplex ψ τ of X,
  natural in τ (via the joins id * g), restricting to p τ on the back and to
  σ on the front.  Faces and degeneracies act through the joins δ * id.
  Vertices of X_/p are cones over p; a limit of p is a terminal object of
  X_/p (Joyal, Lurie HTT 1.2.13).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.IC6

namespace Found

namespace IC

open Simp

universe w

/-- the action of a map of the simplex category on a simplicial set -/
def mp (X : SSet.{w}) {k l : Nat} (g : DHom k l) (x : (X.obj (l : Nat)).car) : (X.obj (k : Nat)).car :=
  X.map (a := (k : Nat)) (b := (l : Nat)) g x

theorem mp_comp (X : SSet.{w}) {k l q : Nat} (f : DHom k l) (g : DHom l q) (x : (X.obj (q : Nat)).car) :
    (X.obj (k : Nat)).eqv (mp X f (mp X g x)) (mp X (dComp f g) x) :=
  (X.obj (k : Nat)).symm (X.map_comp f g x)

theorem mp_resp (X : SSet.{w}) {k l : Nat} {f g : DHom k l} (h : ∀ i, i ≤ k → f.f i = g.f i)
    (x : (X.obj (l : Nat)).car) : (X.obj (k : Nat)).eqv (mp X f x) (mp X g x) :=
  X.map_resp (a := (k : Nat)) (b := (l : Nat)) x h

theorem mp_idl (X : SSet.{w}) {k : Nat} (f : DHom k k) (h : ∀ i, i ≤ k → f.f i = i)
    (x : (X.obj (k : Nat)).car) : (X.obj (k : Nat)).eqv (mp X f x) x :=
  (X.obj (k : Nat)).trans (X.map_resp (a := (k : Nat)) (b := (k : Nat)) (g := Delta.id (k : Nat)) x h)
    (X.map_id _ x)

theorem mp_eqv (X : SSet.{w}) {k l : Nat} (f : DHom k l) {x y : (X.obj (l : Nat)).car}
    (h : (X.obj (l : Nat)).eqv x y) : (X.obj (k : Nat)).eqv (mp X f x) (mp X f y) :=
  X.map_eqv (a := (k : Nat)) (b := (l : Nat)) f h

/-- the back inclusion [m] -> [n+1+m] and the front inclusion [n] -> [n+1+m] -/
def inK (n m : Nat) : DHom m (n + 1 + m) :=
  ⟨fun j => j + (n + 1), fun j _ => Nat.add_le_add_right (Nat.le_succ j) _,
    fun j hj => by rw [Nat.add_comm (n + 1) m]; exact Nat.add_le_add_right hj _⟩
def inD (n m : Nat) : DHom n (n + 1 + m) :=
  ⟨fun i => i, fun _ _ => Nat.le_succ _,
    fun _ hi => Nat.le_trans hi (Nat.le_trans (Nat.le_succ n) (Nat.le_add_right _ m))⟩

variable {K X : SSet.{w}} (p : PNat K X)

/-- n-simplices of the slice X_/p -/
structure SlS (n : Nat) : Type w where
  σ : (X.obj (n : Nat)).car
  ψ : (m : Nat) → (K.obj (m : Nat)).car → (X.obj (n + 1 + m : Nat)).car
  ψ_eqv : ∀ m {τ τ'}, (K.obj (m : Nat)).eqv τ τ' → (X.obj (n + 1 + m : Nat)).eqv (ψ m τ) (ψ m τ')
  ψ_nat : ∀ {m m'} (g : DHom m' m) τ,
    (X.obj (n + 1 + m' : Nat)).eqv (ψ m' (mp K g τ)) (mp X (J (dId n) g) (ψ m τ))
  ψ_K : ∀ m τ, (X.obj (m : Nat)).eqv (mp X (inK n m) (ψ m τ)) (p.app (m : Nat) τ)
  ψ_D : ∀ m τ, (X.obj (n : Nat)).eqv (mp X (inD n m) (ψ m τ)) σ

variable {p}

def SlS.eqv {n : Nat} (s s' : SlS p n) : Prop :=
  (X.obj (n : Nat)).eqv s.σ s'.σ ∧ ∀ m τ, (X.obj (n + 1 + m : Nat)).eqv (s.ψ m τ) (s'.ψ m τ)

/-- the slice acts on maps of the simplex category through joins δ * id -/
def SlS.act {n' n : Nat} (δ : DHom n' n) (s : SlS p n) : SlS p n' where
  σ := mp X δ s.σ
  ψ m τ := mp X (J δ (dId m)) (s.ψ m τ)
  ψ_eqv m {_ _} h := mp_eqv X _ (s.ψ_eqv m h)
  ψ_nat {m m'} g τ :=
    (X.obj (n' + 1 + m' : Nat)).trans (mp_eqv X _ (s.ψ_nat g τ))
      ((X.obj (n' + 1 + m' : Nat)).trans (mp_comp X _ _ _)
        ((X.obj (n' + 1 + m' : Nat)).trans
          (mp_resp X (fun i hi => (J_comp δ (dId n) (dId m') g i hi).trans
            ((J_resp (x := dComp δ (dId n)) (x' := dComp (dId n') δ) (y := dComp (dId m') g) (y' := dComp g (dId m))
              (fun _ _ => rfl) (fun _ _ => rfl) i hi).trans (J_comp (dId n') δ g (dId m) i hi).symm)) _)
          ((X.obj (n' + 1 + m' : Nat)).symm (mp_comp X _ _ _))))
  ψ_K m τ :=
    (X.obj (m : Nat)).trans (mp_comp X _ _ _)
      ((X.obj (m : Nat)).trans (mp_resp X (g := inK n m) (fun j _ =>
          (jmap_back δ (dId m) j).trans (Nat.add_comm _ _)) _) (s.ψ_K m τ))
  ψ_D m τ :=
    (X.obj (n' : Nat)).trans (mp_comp X _ _ _)
      ((X.obj (n' : Nat)).trans (mp_resp X (g := dComp δ (inD n m)) (fun _ hi =>
          jmap_front δ (dId m) (Nat.lt_succ_of_le hi)) _)
        ((X.obj (n' : Nat)).trans ((X.obj (n' : Nat)).symm (mp_comp X _ _ _)) (mp_eqv X δ (s.ψ_D m τ))))

variable (p)

/-- the slice simplicial set X_/p -/
def Slice : SSet.{w} where
  obj := fun (n : Nat) => ⟨SlS p n, SlS.eqv,
    fun _ => ⟨(X.obj n).refl _, fun m _ => (X.obj (n + 1 + m : Nat)).refl _⟩,
    fun h => ⟨(X.obj _).symm h.1, fun m τ => (X.obj _).symm (h.2 m τ)⟩,
    fun h k => ⟨(X.obj _).trans h.1 k.1, fun m τ => (X.obj _).trans (h.2 m τ) (k.2 m τ)⟩⟩
  map δ s := SlS.act δ s
  map_eqv {_ _} δ {_ _} h := ⟨mp_eqv X δ h.1, fun m τ => mp_eqv X _ (h.2 m τ)⟩
  map_resp {_ _ _ _} s e :=
    ⟨mp_resp X e s.σ, fun m _ => mp_resp X (J_resp (y := dId m) (y' := dId m) e (fun _ _ => rfl)) _⟩
  map_id n s := ⟨X.map_id _ s.σ, fun m _ => mp_idl X _ (J_id n m) _⟩
  map_comp {_ _ _} f g s :=
    ⟨X.map_comp f g s.σ, fun m _ =>
      (X.obj _).trans
        (mp_resp X (f := J (dComp f g) (dId m)) (g := dComp (J f (dId m)) (J g (dId m)))
          (fun i hi => ((J_comp f g (dId m) (dId m) i hi).symm)) _)
        ((X.obj _).symm (mp_comp X _ _ _))⟩

/-- cones over p: vertices of the slice -/
abbrev Cone := (V (Slice p)).car

/-- limits of p: terminal objects of the slice (Joyal) -/
def IsLimit (c : Cone p) : Prop := Term (X := Slice p) c

def HasLimit : Prop := ∃ c : Cone p, IsLimit p c

/-- edges into a limit cone are all homotopic -/
theorem limit_hty {c : Cone p} (hc : IsLimit p c) {x : Cone p} (f g : Mor (X := Slice p) x c) : Hty f g :=
  term_hty hc f g

/-- when the slice is a quasi-category, limits are unique up to equivalence -/
theorem limit_unique (hQs : Quasi (Slice p)) (c2s : (h : Horn (Slice p) 0 1) → { σ // Fills h σ })
    {c c' : Cone p} (hc : IsLimit p c) (hc' : IsLimit p c') : ∃ f : Mor (X := Slice p) c c', IsEquiv c2s hQs f :=
  term_unique hQs c2s hc hc'

end IC

end Found

#print axioms Found.IC.SlS.act
#print axioms Found.IC.Slice
#print axioms Found.IC.limit_hty
#print axioms Found.IC.limit_unique

found/IC8.lean

lean_all/found/IC8.lean

/-
  T153 ((infinity,1)-categories), part 8. found/IC8 — coslices and colimits,
  and the empty diagram.  The coslice X_p/ has n-simplices K * Delta^n -> X
  extending p (K in front); a colimit of p is an initial object of X_p/.
  Terminal and initial objects transfer along maps F : Y -> X having a map
  G back with F G = id; the slice and coslice over the empty diagram are
  isomorphic to X, so limits of the empty diagram are exactly terminal
  objects and colimits of the empty diagram exactly initial objects.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.IC7

namespace Found

namespace IC

open Simp

universe w

variable {K X : SSet.{w}} (p : PNat K X)

/-- n-simplices of the coslice X_p/ -/
structure CoS (n : Nat) : Type w where
  σ : (X.obj (n : Nat)).car
  ψ : (m : Nat) → (K.obj (m : Nat)).car → (X.obj (m + 1 + n : Nat)).car
  ψ_eqv : ∀ m {τ τ'}, (K.obj (m : Nat)).eqv τ τ' → (X.obj (m + 1 + n : Nat)).eqv (ψ m τ) (ψ m τ')
  ψ_nat : ∀ {m m'} (g : DHom m' m) τ,
    (X.obj (m' + 1 + n : Nat)).eqv (ψ m' (mp K g τ)) (mp X (J g (dId n)) (ψ m τ))
  ψ_K : ∀ m τ, (X.obj (m : Nat)).eqv (mp X (inD m n) (ψ m τ)) (p.app (m : Nat) τ)
  ψ_D : ∀ m τ, (X.obj (n : Nat)).eqv (mp X (inK m n) (ψ m τ)) σ

variable {p}

def CoS.eqv {n : Nat} (s s' : CoS p n) : Prop :=
  (X.obj (n : Nat)).eqv s.σ s'.σ ∧ ∀ m τ, (X.obj (m + 1 + n : Nat)).eqv (s.ψ m τ) (s'.ψ m τ)

def CoS.act {n' n : Nat} (δ : DHom n' n) (s : CoS p n) : CoS p n' where
  σ := mp X δ s.σ
  ψ m τ := mp X (J (dId m) δ) (s.ψ m τ)
  ψ_eqv m {_ _} h := mp_eqv X _ (s.ψ_eqv m h)
  ψ_nat {m m'} g τ :=
    (X.obj _).trans (mp_eqv X _ (s.ψ_nat g τ))
      ((X.obj _).trans (mp_comp X _ _ _)
        ((X.obj _).trans
          (mp_resp X (fun i hi => (J_comp (dId m') g δ (dId n) i hi).trans
            ((J_resp (x := dComp (dId m') g) (x' := dComp g (dId m)) (y := dComp δ (dId n)) (y' := dComp (dId n') δ)
              (fun _ _ => rfl) (fun _ _ => rfl) i hi).trans (J_comp g (dId m) (dId n') δ i hi).symm)) _)
          ((X.obj _).symm (mp_comp X _ _ _))))
  ψ_K m τ :=
    (X.obj _).trans (mp_comp X _ _ _)
      ((X.obj _).trans (mp_resp X (g := inD m n) (fun _ hi => jmap_front (dId m) δ (Nat.lt_succ_of_le hi)) _)
        (s.ψ_K m τ))
  ψ_D m τ :=
    (X.obj _).trans (mp_comp X _ _ _)
      ((X.obj _).trans (mp_resp X (g := dComp δ (inK m n)) (fun j _ =>
          (jmap_back (dId m) δ j).trans (Nat.add_comm _ _)) _)
        ((X.obj _).trans ((X.obj _).symm (mp_comp X _ _ _)) (mp_eqv X δ (s.ψ_D m τ))))

variable (p)

/-- the coslice simplicial set X_p/ -/
def Coslice : SSet.{w} where
  obj := fun (n : Nat) => ⟨CoS p n, CoS.eqv,
    fun _ => ⟨(X.obj n).refl _, fun m _ => (X.obj (m + 1 + n : Nat)).refl _⟩,
    fun h => ⟨(X.obj _).symm h.1, fun m τ => (X.obj _).symm (h.2 m τ)⟩,
    fun h k => ⟨(X.obj _).trans h.1 k.1, fun m τ => (X.obj _).trans (h.2 m τ) (k.2 m τ)⟩⟩
  map δ s := CoS.act δ s
  map_eqv {_ _} δ {_ _} h := ⟨mp_eqv X δ h.1, fun m τ => mp_eqv X _ (h.2 m τ)⟩
  map_resp {_ _ _ _} s e :=
    ⟨mp_resp X e s.σ, fun m _ => mp_resp X (J_resp (x := dId m) (x' := dId m) (fun _ _ => rfl) e) _⟩
  map_id n s := ⟨X.map_id _ s.σ, fun m _ => mp_idl X _ (J_id m n) _⟩
  map_comp {_ _ _} f g s :=
    ⟨X.map_comp f g s.σ, fun m _ =>
      (X.obj _).trans
        (mp_resp X (f := J (dId m) (dComp f g)) (g := dComp (J (dId m) f) (J (dId m) g))
          (fun i hi => ((J_comp (dId m) (dId m) f g i hi).symm)) _)
        ((X.obj _).symm (mp_comp X _ _ _))⟩

/-- cocones under p and colimits: initial objects of the coslice -/
abbrev Cocone := (V (Coslice p)).car

def IsColimit (c : Cocone p) : Prop := Init (X := Coslice p) c

/-! ## transfer of terminal and initial objects -/

variable {Y : SSet.{w}} (F : PNat Y X) (G : PNat X Y)
  (hFG : ∀ (n : Nat) x, (X.obj (n : Nat)).eqv (F.app (n : Nat) (G.app (n : Nat) x)) x)

def bdryMap {n : Nat} (b : Bdry X n) : Bdry Y n where
  x i := G.app (n + 1 : Nat) (b.x i)
  compat i j hij hj :=
    (Y.obj _).trans ((Y.obj _).symm (app_dd G i _))
      ((Y.obj _).trans (G.app_eqv _ (b.compat i j hij hj)) (app_dd G j _))

def morMapT {x : (V X).car} {y : (V Y).car} (f : Mor (G.app (0 : Nat) x) y) : Mor x (F.app (0 : Nat) y) :=
  ⟨F.app (1 : Nat) f.1,
    (V X).trans ((V X).symm (app_dd F 1 f.1)) ((V X).trans (F.app_eqv _ f.2.1) (hFG 0 x)),
    (V X).trans ((V X).symm (app_dd F 0 f.1)) (F.app_eqv _ f.2.2)⟩

def morMapI {x : (V X).car} {y : (V Y).car} (f : Mor y (G.app (0 : Nat) x)) : Mor (F.app (0 : Nat) y) x :=
  ⟨F.app (1 : Nat) f.1,
    (V X).trans ((V X).symm (app_dd F 1 f.1)) (F.app_eqv _ f.2.1),
    (V X).trans ((V X).symm (app_dd F 0 f.1)) ((V X).trans (F.app_eqv _ f.2.2) (hFG 0 x))⟩

include hFG in
/-- terminal objects transfer along F when G F y = y -/
theorem term_transfer {y : (V Y).car} (hGF : (V Y).eqv (G.app (0 : Nat) (F.app (0 : Nat) y)) y)
    (hT : Term y) : Term (X := X) (F.app (0 : Nat) y) :=
  ⟨fun x => match hT.1 (G.app (0 : Nat) x) with
    | ⟨f⟩ => ⟨morMapT F G hFG f⟩,
   fun n b hb => match hT.2 n (bdryMap G b)
      ((V Y).trans ((V Y).symm (G.nat (cstL (n + 1)) (b.x 0))) ((V Y).trans (G.app_eqv _ hb) hGF)) with
    | ⟨σ, hσ⟩ => ⟨F.app _ σ, fun i hi =>
        (X.obj _).trans ((X.obj _).symm (app_dd F i σ)) ((X.obj _).trans (F.app_eqv _ (hσ i hi)) (hFG _ _))⟩⟩

include hFG in
/-- initial objects transfer along F when G F y = y -/
theorem init_transfer {y : (V Y).car} (hGF : (V Y).eqv (G.app (0 : Nat) (F.app (0 : Nat) y)) y)
    (hI : Init y) : Init (X := X) (F.app (0 : Nat) y) :=
  ⟨fun x => match hI.1 (G.app (0 : Nat) x) with
    | ⟨f⟩ => ⟨morMapI F G hFG f⟩,
   fun n b hb => match hI.2 n (bdryMap G b)
      ((V Y).trans ((V Y).symm (G.nat (cst0 (n + 1)) (b.x (n + 2)))) ((V Y).trans (G.app_eqv _ hb) hGF)) with
    | ⟨σ, hσ⟩ => ⟨F.app _ σ, fun i hi =>
        (X.obj _).trans ((X.obj _).symm (app_dd F i σ)) ((X.obj _).trans (F.app_eqv _ (hσ i hi)) (hFG _ _))⟩⟩

omit hFG in
theorem term_eqv {Z : SSet.{w}} {y y' : (V Z).car} (e : (V Z).eqv y y') (hT : Term y) : Term y' :=
  ⟨fun x => match hT.1 x with
    | ⟨f⟩ => ⟨⟨f.1, f.2.1, (V Z).trans f.2.2 e⟩⟩,
   fun n b hb => hT.2 n b ((V Z).trans hb ((V Z).symm e))⟩

omit hFG in
theorem init_eqv {Z : SSet.{w}} {y y' : (V Z).car} (e : (V Z).eqv y y') (hI : Init y) : Init y' :=
  ⟨fun x => match hI.1 x with
    | ⟨f⟩ => ⟨⟨f.1, (V Z).trans f.2.1 e, f.2.2⟩⟩,
   fun n b hb => hI.2 n b ((V Z).trans hb ((V Z).symm e))⟩

/-! ## the empty diagram -/

/-- the empty simplicial set -/
def K0 : SSet.{w} where
  obj _ := ⟨PEmpty, fun _ _ => True, fun _ => trivial, fun _ => trivial, fun _ _ => trivial⟩
  map _ x := x
  map_eqv {_ _} _ {_ _} _ := trivial
  map_resp _ _ := trivial
  map_id _ _ := trivial
  map_comp _ _ _ := trivial

def p0 (X : SSet.{w}) : PNat K0 X where
  app _ e := PEmpty.elim e
  app_eqv _ {e _} _ := PEmpty.elim e
  nat _ e := PEmpty.elim e

def slF (X : SSet.{w}) : PNat (Slice (p0 X)) X where
  app _ s := s.σ
  app_eqv _ {_ _} h := h.1
  nat _ _ := (X.obj _).refl _

def slG (X : SSet.{w}) : PNat X (Slice (p0 X)) where
  app _ x := ⟨x, fun _ e => PEmpty.elim e, fun _ {e _} _ => PEmpty.elim e, fun _ e => PEmpty.elim e,
    fun _ e => PEmpty.elim e, fun _ e => PEmpty.elim e⟩
  app_eqv _ {_ _} h := ⟨h, fun _ e => PEmpty.elim e⟩
  nat _ _ := ⟨(X.obj _).refl _, fun _ e => PEmpty.elim e⟩

def coF (X : SSet.{w}) : PNat (Coslice (p0 X)) X where
  app _ s := s.σ
  app_eqv _ {_ _} h := h.1
  nat _ _ := (X.obj _).refl _

def coG (X : SSet.{w}) : PNat X (Coslice (p0 X)) where
  app _ x := ⟨x, fun _ e => PEmpty.elim e, fun _ {e _} _ => PEmpty.elim e, fun _ e => PEmpty.elim e,
    fun _ e => PEmpty.elim e, fun _ e => PEmpty.elim e⟩
  app_eqv _ {_ _} h := ⟨h, fun _ e => PEmpty.elim e⟩
  nat _ _ := ⟨(X.obj _).refl _, fun _ e => PEmpty.elim e⟩

/-- limits of the empty diagram are exactly the terminal objects -/
theorem limit_empty_iff (c : Cone (p0 X)) : IsLimit (p0 X) c ↔ Term (X := X) c.σ :=
  ⟨fun h => term_transfer (slF X) (slG X) (fun _ _ => (X.obj _).refl _)
      ⟨(X.obj _).refl _, fun _ e => PEmpty.elim e⟩ h,
   fun h => term_eqv (y := (slG X).app (0 : Nat) c.σ) ⟨(X.obj _).refl _, fun _ e => PEmpty.elim e⟩
      (term_transfer (slG X) (slF X) (fun _ _ => ⟨(X.obj _).refl _, fun _ e => PEmpty.elim e⟩)
        ((X.obj _).refl _) h)⟩

/-- colimits of the empty diagram are exactly the initial objects -/
theorem colimit_empty_iff (c : Cocone (p0 X)) : IsColimit (p0 X) c ↔ Init (X := X) c.σ :=
  ⟨fun h => init_transfer (coF X) (coG X) (fun _ _ => (X.obj _).refl _)
      ⟨(X.obj _).refl _, fun _ e => PEmpty.elim e⟩ h,
   fun h => init_eqv (y := (coG X).app (0 : Nat) c.σ) ⟨(X.obj _).refl _, fun _ e => PEmpty.elim e⟩
      (init_transfer (coG X) (coF X) (fun _ _ => ⟨(X.obj _).refl _, fun _ e => PEmpty.elim e⟩)
        ((X.obj _).refl _) h)⟩

end IC

end Found

#print axioms Found.IC.Coslice
#print axioms Found.IC.term_transfer
#print axioms Found.IC.init_transfer
#print axioms Found.IC.limit_empty_iff
#print axioms Found.IC.colimit_empty_iff

found/IC9.lean

lean_all/found/IC9.lean

/-
  T153 ((infinity,1)-categories), part 9. found/IC9 — simplicial sets are
  cartesian closed: the standard simplices Delta^n (in any universe),
  products, composition of simplicial maps, the function complex Fun(K, X)
  (n-simplices = maps Delta^n x K -> X), evaluation Fun(K, X) x K -> X and
  currying, with ev (curry α x id) = α.  Fun(C, X) is the
  infinity-category of functors used for presheaf infinity-categories.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.IC8

namespace Found

namespace IC

open Simp

universe w

/-- the standard n-simplex -/
def Simp (n : Nat) : SSet.{w} where
  obj := fun (m : Nat) => ⟨ULift.{w} (DHom m n), fun a b => ∀ i, i ≤ m → a.down.f i = b.down.f i,
    fun _ _ _ => rfl, fun h i hi => (h i hi).symm, fun h k i hi => (h i hi).trans (k i hi)⟩
  map g a := ⟨dComp g a.down⟩
  map_eqv {_ _} g {_ _} h := fun i hi => h _ (g.bound i hi)
  map_resp {_ _ _ _} a e := fun i hi => congrArg a.down.f (e i hi)
  map_id _ _ := fun _ _ => rfl
  map_comp _ _ _ := fun _ _ => rfl

/-- products of simplicial sets -/
def SProd (X Y : SSet.{w}) : SSet.{w} where
  obj := fun (n : Nat) => ⟨(X.obj n).car × (Y.obj n).car,
    fun a b => (X.obj n).eqv a.1 b.1 ∧ (Y.obj n).eqv a.2 b.2,
    fun a => ⟨(X.obj n).refl a.1, (Y.obj n).refl a.2⟩,
    fun h => ⟨(X.obj n).symm h.1, (Y.obj n).symm h.2⟩,
    fun h k => ⟨(X.obj n).trans h.1 k.1, (Y.obj n).trans h.2 k.2⟩⟩
  map f a := (X.map f a.1, Y.map f a.2)
  map_eqv {_ _} f {_ _} h := ⟨X.map_eqv f h.1, Y.map_eqv f h.2⟩
  map_resp {_ _ _ _} a e := ⟨X.map_resp a.1 e, Y.map_resp a.2 e⟩
  map_id n a := ⟨X.map_id n a.1, Y.map_id n a.2⟩
  map_comp f g a := ⟨X.map_comp f g a.1, Y.map_comp f g a.2⟩

/-- composition of simplicial maps -/
def pcomp {A B C : SSet.{w}} (α : PNat A B) (β : PNat B C) : PNat A C where
  app n x := β.app n (α.app n x)
  app_eqv n {_ _} h := β.app_eqv n (α.app_eqv n h)
  nat f x := (C.obj _).trans (β.app_eqv _ (α.nat f x)) (β.nat f _)

/-- δ x id : Delta^n' x K -> Delta^n x K -/
def dProd {n' n : Nat} (δ : DHom n' n) (K : SSet.{w}) : PNat (SProd (Simp.{w} n') K) (SProd (Simp.{w} n) K) where
  app _ z := (⟨dComp z.1.down δ⟩, z.2)
  app_eqv _ {_ _} h := ⟨fun i hi => congrArg δ.f (h.1 i hi), h.2⟩
  nat _ _ := ⟨fun _ _ => rfl, (K.obj _).refl _⟩

/-- the function complex Fun(K, X) -/
def FunS (K X : SSet.{w}) : SSet.{w} where
  obj := fun (n : Nat) => ⟨PNat (SProd (Simp.{w} n) K) X,
    fun φ ψ => ∀ (m : Nat) z, (X.obj (m : Nat)).eqv (φ.app (m : Nat) z) (ψ.app (m : Nat) z),
    fun _ _ _ => (X.obj _).refl _, fun h m z => (X.obj _).symm (h m z),
    fun h k m z => (X.obj _).trans (h m z) (k m z)⟩
  map δ φ := pcomp (dProd δ K) φ
  map_eqv {_ _} _ {_ _} h := fun m z => h m _
  map_resp {_ _ _ _} φ e := fun _ z => φ.app_eqv _ ⟨fun i _ => e _ (z.1.down.bound i ‹_›), (K.obj _).refl _⟩
  map_id _ φ := fun _ z => φ.app_eqv _ ⟨fun _ _ => rfl, (K.obj _).refl _⟩
  map_comp _ _ φ := fun _ z => φ.app_eqv _ ⟨fun _ _ => rfl, (K.obj _).refl _⟩

variable {K X A : SSet.{w}}

/-- evaluation Fun(K, X) x K -> X -/
def ev : PNat (SProd (FunS K X) K) X where
  app n z := z.1.app n (⟨dId n⟩, z.2)
  app_eqv n {_ b} h := (X.obj _).trans (h.1 n _) (b.1.app_eqv n ⟨fun _ _ => rfl, h.2⟩)
  nat {_ n} f z := (X.obj _).trans (z.1.app_eqv _ ⟨fun _ _ => rfl, (K.obj _).refl _⟩) (z.1.nat f (⟨dId n⟩, z.2))

/-- currying A x K -> X to A -> Fun(K, X) -/
def curry (α : PNat (SProd A K) X) : PNat A (FunS K X) where
  app n a := {
    app := fun m z => α.app m (A.map (a := m) (b := n) z.1.down a, z.2)
    app_eqv := fun m {_ _} h => α.app_eqv m ⟨A.map_resp a h.1, h.2⟩
    nat := fun {m' _} f z => (X.obj m').trans (b := α.app m' (A.map f (A.map z.1.down a), K.map f z.2))
      (α.app_eqv m' ⟨A.map_comp f z.1.down a, (K.obj m').refl _⟩) (α.nat f (A.map z.1.down a, z.2)) }
  app_eqv _ {_ _} h := fun m z => α.app_eqv m ⟨A.map_eqv z.1.down h, (K.obj _).refl _⟩
  nat f a := fun m z => α.app_eqv m ⟨(A.obj _).symm (A.map_comp z.1.down f a), (K.obj _).refl _⟩

/-- ev (curry α x id) = α -/
theorem ev_curry (α : PNat (SProd A K) X) (n : Nat) (a : (A.obj (n : Nat)).car) (k : (K.obj (n : Nat)).car) :
    (X.obj (n : Nat)).eqv (ev.app (n : Nat) ((curry α).app (n : Nat) a, k)) (α.app (n : Nat) (a, k)) :=
  α.app_eqv _ ⟨A.map_id _ a, (K.obj _).refl _⟩

/-- φ x id -/
def prodMap {B : SSet.{w}} (φ : PNat A B) (K : SSet.{w}) : PNat (SProd A K) (SProd B K) where
  app m z := (φ.app m z.1, z.2)
  app_eqv m {_ _} h := ⟨φ.app_eqv m h.1, h.2⟩
  nat f z := ⟨φ.nat f z.1, (K.obj _).refl _⟩

/-- curry (ev ∘ (φ x id)) = φ -/
theorem curry_ev (φ : PNat A (FunS K X)) (n : Nat) (a : (A.obj (n : Nat)).car) :
    ((FunS K X).obj (n : Nat)).eqv ((curry (pcomp (prodMap φ K) ev)).app (n : Nat) a) (φ.app (n : Nat) a) :=
  fun m z => (X.obj _).trans (φ.nat z.1.down a m (⟨dId m⟩, z.2))
    ((φ.app _ a).app_eqv m ⟨fun _ _ => rfl, (K.obj _).refl _⟩)

end IC

end Found

#print axioms Found.IC.FunS
#print axioms Found.IC.ev
#print axioms Found.IC.curry
#print axioms Found.IC.ev_curry
#print axioms Found.IC.curry_ev

found/IST.lean

lean_all/found/IST.lean

/-
  T165. found/IST — Nelson's internal set theory as a point: a new predicate
  `st` (standard) on the existing universe (here on ℕ, the core's NNO) with
  IST's principles as conditions on the parameter.
  * Idealization (instance for <): if every finite list of standard numbers
    has a common strict upper bound, then some y exceeds every standard x.
  * Consequence: there is a nonstandard natural number (st y fails).
  * Transfer/standardization are further conditions (cited); IST is a
    conservative extension of ZFC (line to T89, cited).

  Lean 4 core only, hand-written proofs, no axioms (conditions are hypotheses).
-/

namespace Found

namespace IST

def Ideal (st : Nat → Prop) : Prop :=
  (∀ S : List Nat, (∀ x, x ∈ S → st x) → ∃ y, ∀ x, x ∈ S → x < y) →
    ∃ y, ∀ x, st x → x < y

/-- every finite list has a strict upper bound (classical arithmetic fact, here constructive) -/
theorem bound : (S : List Nat) → ∃ y, ∀ x, x ∈ S → x < y
  | [] => ⟨0, fun _ h => nomatch h⟩
  | a :: S => match bound S with
    | ⟨y, hy⟩ => ⟨a + y + 1, fun x m => by
        cases m with
        | head => exact Nat.lt_succ_of_le (Nat.le_add_right a y)
        | tail _ m' => exact Nat.lt_succ_of_le (Nat.le_trans (Nat.le_of_lt (hy x m')) (Nat.le_add_left y a))⟩

/-- idealization yields a nonstandard number -/
theorem nonstandard (st : Nat → Prop) (hI : Ideal st) : ∃ y, ¬ st y :=
  match hI (fun S _ => bound S) with
  | ⟨y, hy⟩ => ⟨y, fun s => Nat.lt_irrefl y (hy y s)⟩

end IST

end Found

#print axioms Found.IST.bound
#print axioms Found.IST.nonstandard

found/IZ1.lean

lean_all/found/IZ1.lean

/-
  Shared tool R3 (inner set theory), part 1. found/IZ1 — reasoning inside an
  arbitrary model of ZF (found/NbgZ5 ZF(B0)) by definable predicates:
  * a model: a structure (D, ∈) in which every axiom of ZF(B0) holds;
  * a predicate on valuations is definable when some L0 formula has exactly
    its truth values; definable predicates are closed under ∈-atoms, the
    connectives, both quantifiers and renaming of variables — so the schemes
    of ZF can be used for predicates built from these, without writing
    formulas by hand;
  * the axioms read semantically: extensionality (Leibniz), invariance of
    definable predicates under coextension, separation (from replacement),
    replacement, foundation (∈-minimal elements), pairing, union, power
    set, infinity.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.NbgZ7

namespace Found

namespace IZ

open FO FOCT NBGc

structure ZM where
  D : Type
  R : Nat → List D → Prop
  zf : ∀ φ, ZF B0 φ → ∀ v : Nat → D, Sat R φ v
  d0 : D

variable (M : ZM)

abbrev In (x y : M.D) : Prop := mem M.R x y

/-- a definable predicate of valuations -/
def Def (P : (Nat → M.D) → Prop) : Prop := ∃ φ, L0 φ ∧ ∀ v, Sat M.R φ v ↔ P v

theorem def_mem (i j : Nat) : Def M (fun v => In M (v i) (v j)) := ⟨m i j, l0_m i j, fun _ => Iff.rfl⟩

theorem def_congr {P Q : (Nat → M.D) → Prop} (h : ∀ v, P v ↔ Q v) (hP : Def M P) : Def M Q :=
  match hP with
  | ⟨φ, hl, e⟩ => ⟨φ, hl, fun v => (e v).trans (h v)⟩

theorem def_and {P Q : (Nat → M.D) → Prop} (hP : Def M P) (hQ : Def M Q) : Def M (fun v => P v ∧ Q v) :=
  match hP, hQ with
  | ⟨φ, l1, e1⟩, ⟨ψ, l2, e2⟩ => ⟨.conj φ ψ, ⟨l1, l2⟩, fun v =>
    ⟨fun ⟨a, b⟩ => ⟨(e1 v).1 a, (e2 v).1 b⟩, fun ⟨a, b⟩ => ⟨(e1 v).2 a, (e2 v).2 b⟩⟩⟩

theorem def_or {P Q : (Nat → M.D) → Prop} (hP : Def M P) (hQ : Def M Q) : Def M (fun v => P v ∨ Q v) :=
  match hP, hQ with
  | ⟨φ, l1, e1⟩, ⟨ψ, l2, e2⟩ => ⟨.disj φ ψ, ⟨l1, l2⟩, fun v =>
    ⟨fun h => h.elim (fun a => .inl ((e1 v).1 a)) (fun b => .inr ((e2 v).1 b)),
     fun h => h.elim (fun a => .inl ((e1 v).2 a)) (fun b => .inr ((e2 v).2 b))⟩⟩

theorem def_imp {P Q : (Nat → M.D) → Prop} (hP : Def M P) (hQ : Def M Q) : Def M (fun v => P v → Q v) :=
  match hP, hQ with
  | ⟨φ, l1, e1⟩, ⟨ψ, l2, e2⟩ => ⟨.imp φ ψ, ⟨l1, l2⟩, fun v =>
    ⟨fun h a => (e2 v).1 (h ((e1 v).2 a)), fun h a => (e2 v).2 (h ((e1 v).1 a))⟩⟩

theorem def_false : Def M (fun _ => False) := ⟨.bot, trivial, fun _ => Iff.rfl⟩

theorem def_true : Def M (fun _ => True) :=
  ⟨.imp .bot .bot, ⟨trivial, trivial⟩, fun _ => ⟨fun _ => trivial, fun _ h => h⟩⟩

theorem def_not {P : (Nat → M.D) → Prop} (hP : Def M P) : Def M (fun v => ¬ P v) := def_imp M hP (def_false M)

theorem def_iff {P Q : (Nat → M.D) → Prop} (hP : Def M P) (hQ : Def M Q) : Def M (fun v => P v ↔ Q v) :=
  def_congr M (fun _ => ⟨fun ⟨a, b⟩ => ⟨a, b⟩, fun h => ⟨h.1, h.2⟩⟩) (def_and M (def_imp M hP hQ) (def_imp M hQ hP))

theorem def_all {P : (Nat → M.D) → Prop} (hP : Def M P) : Def M (fun v => ∀ d, P (ext v d)) :=
  match hP with
  | ⟨φ, l, e⟩ => ⟨.all φ, l, fun v => ⟨fun h d => (e (ext v d)).1 (h d), fun h d => (e (ext v d)).2 (h d)⟩⟩

theorem def_ex {P : (Nat → M.D) → Prop} (hP : Def M P) : Def M (fun v => ∃ d, P (ext v d)) :=
  match hP with
  | ⟨φ, l, e⟩ => ⟨.ex φ, l, fun v => ⟨fun ⟨d, h⟩ => ⟨d, (e (ext v d)).1 h⟩, fun ⟨d, h⟩ => ⟨d, (e (ext v d)).2 h⟩⟩⟩

theorem def_ren (σ : Nat → Nat) {P : (Nat → M.D) → Prop} (hP : Def M P) : Def M (fun v => P (fun n => v (σ n))) :=
  match hP with
  | ⟨φ, l, e⟩ => ⟨ren σ φ, l0_ren σ φ l, fun v => (sat_ren M.R φ σ v).trans (e _)⟩

/-- coextension is definable -/
theorem def_ceq (i j : Nat) : Def M (fun v => Ceq M.R (v i) (v j)) :=
  def_congr M (fun _ => Iff.rfl) (def_all M (def_iff M (def_mem M 0 (i + 1)) (def_mem M 0 (j + 1))))

/-! ## the axioms, semantically -/

theorem extD : ExtD M.R := extAx_D M.R (M.zf extAx (.inl rfl) (fun _ => M.d0))

/-- definable predicates respect coextension -/
theorem def_resp {P : (Nat → M.D) → Prop} (hP : Def M P) {v w : Nat → M.D} (h : ∀ n, Ceq M.R (v n) (w n))
    (hv : P v) : P w :=
  match hP with
  | ⟨φ, l, e⟩ => (e w).1 (sat_ceq M.R (extD M) φ l h ((e v).2 hv))

theorem ceq_ext0 (v : Nat → M.D) {x y : M.D} (h : Ceq M.R x y) : ∀ n, Ceq M.R (ext v x n) (ext v y n)
  | 0 => h
  | _ + 1 => ceq_refl M.R _

/-- replacement: Θ(w, z) with w = variable 0, z = variable 1 -/
theorem repl {Θ : (Nat → M.D) → Prop} (hΘ : Def M Θ) (v : Nat → M.D) (a : M.D)
    (fn : ∀ w, In M w a → ∀ z z', Θ (ext (ext v z) w) → Θ (ext (ext v z') w) → Ceq M.R z z') :
    ∃ b, ∀ z, In M z b ↔ ∃ w, In M w a ∧ Θ (ext (ext v z) w) :=
  match hΘ with
  | ⟨θ, l, e⟩ =>
    match replZ_use M.R θ v (M.zf _ (.inr (.inr (.inl ⟨θ, l, rfl⟩))) v) a
        (fun w hw z z' h1 h2 => fn w hw z z' ((e _).1 h1) ((e _).1 h2)) with
    | ⟨b, hb⟩ => ⟨b, fun z => (hb z).trans ⟨fun ⟨w, h1, h2⟩ => ⟨w, h1, (e _).1 h2⟩, fun ⟨w, h1, h2⟩ => ⟨w, h1, (e _).2 h2⟩⟩⟩

/-- foundation: variable 0 is the element -/
theorem found {P : (Nat → M.D) → Prop} (hP : Def M P) (v : Nat → M.D) (hx : ∃ x, P (ext v x)) :
    ∃ x, P (ext v x) ∧ ∀ y, In M y x → ¬ P (ext v y) :=
  match hP with
  | ⟨θ, l, e⟩ =>
    match foundZ_use M.R θ v (M.zf _ (.inr (.inr (.inr ⟨θ, l, rfl⟩))) v)
        (match hx with | ⟨x, h⟩ => ⟨x, (e _).2 h⟩) with
    | ⟨x, h1, h2⟩ => ⟨x, (e _).1 h1, fun y hy hP' => h2 y hy ((e _).2 hP')⟩

/-- shifting a predicate by two places for the replacement layout -/
def sh2 (P : (Nat → M.D) → Prop) : (Nat → M.D) → Prop := fun u => P (ext (fun n => u (n + 2)) (u 0))

def σs : Nat → Nat
  | 0 => 0
  | n + 1 => n + 2

theorem def_sh2 {P : (Nat → M.D) → Prop} (hP : Def M P) : Def M (sh2 M P) :=
  def_congr M (fun u => by
    show P (fun n => u (σs n)) ↔ P (ext (fun n => u (n + 2)) (u 0))
    exact ⟨fun h => def_resp M hP (v := fun n => u (σs n)) (w := ext (fun n => u (n + 2)) (u 0))
        (fun n => match n with
        | 0 => ceq_refl M.R _
        | _ + 1 => ceq_refl M.R _) h,
      fun h => def_resp M hP (v := ext (fun n => u (n + 2)) (u 0)) (w := fun n => u (σs n))
        (fun n => match n with
        | 0 => ceq_refl M.R _
        | _ + 1 => ceq_refl M.R _) h⟩) (def_ren M σs hP)

/-- separation: variable 0 is the element -/
theorem sep {P : (Nat → M.D) → Prop} (hP : Def M P) (v : Nat → M.D) (a : M.D) :
    ∃ b, ∀ x, In M x b ↔ In M x a ∧ P (ext v x) :=
  let Θ : (Nat → M.D) → Prop := fun u => sh2 M P u ∧ Ceq M.R (u 1) (u 0)
  have hΘ : Def M Θ := def_and M (def_sh2 M hP) (def_ceq M 1 0)
  match repl M hΘ v a (fun _ _ _ _ h1 h2 => ceq_trans M.R h1.2 (ceq_symm M.R h2.2)) with
  | ⟨b, hb⟩ => ⟨b, fun x =>
    ⟨fun h => match (hb x).1 h with
      | ⟨_, hw, hP', hc⟩ => ⟨mem_resp M.R (extD M) (ceq_symm M.R hc) (ceq_refl M.R a) hw,
          def_resp M hP (ceq_ext0 M v (ceq_symm M.R hc)) hP'⟩,
     fun ⟨h1, h2⟩ => (hb x).2 ⟨x, h1, h2, ceq_refl M.R x⟩⟩⟩

/-! ## the set axioms -/

theorem pairing : Pairing M.R := pairing_of M.R (M.zf pairAx (.inr (.inl (.inl rfl))) (fun _ => M.d0))

theorem union_ex (a : M.D) : ∃ c, ∀ u, In M u c ↔ ∃ t, In M t a ∧ In M u t :=
  match M.zf unionAx (.inr (.inl (.inr (.inl rfl)))) (fun _ => M.d0) a with
  | ⟨c, hc⟩ => ⟨c, fun u => ⟨(hc u).1, (hc u).2⟩⟩

theorem power_ex (a : M.D) : ∃ c, ∀ u, In M u c ↔ ∀ t, In M t u → In M t a :=
  match M.zf powerAx (.inr (.inl (.inr (.inr (.inl rfl))))) (fun _ => M.d0) a with
  | ⟨c, hc⟩ => ⟨c, fun u => ⟨(hc u).1, (hc u).2⟩⟩

theorem inf_ex : ∃ c, (∃ e, In M e c ∧ ∀ s, ¬ In M s e) ∧
    ∀ x, In M x c → ∃ y, In M y c ∧ ∀ s, In M s y ↔ (In M s x ∨ Ceq M.R s x) :=
  match M.zf infAx (.inr (.inl (.inr (.inr (.inr rfl))))) (fun _ => M.d0) with
  | ⟨c, ⟨e, he, hne⟩, hs⟩ => ⟨c, ⟨e, he, fun s hs' => hne s hs'⟩, fun x hx => match hs x hx with
    | ⟨y, hy, hyy⟩ => ⟨y, hy, fun s => ⟨fun h => ((hyy s).1 h).elim .inl (fun e => .inr (c2 M.R e)),
        fun h => (hyy s).2 (h.elim .inl (fun e => .inr (c1 M.R e)))⟩⟩⟩

end IZ

end Found

#print axioms Found.IZ.def_all
#print axioms Found.IZ.def_ren
#print axioms Found.IZ.def_ceq
#print axioms Found.IZ.extD
#print axioms Found.IZ.def_resp
#print axioms Found.IZ.repl
#print axioms Found.IZ.found
#print axioms Found.IZ.sep
#print axioms Found.IZ.pairing
#print axioms Found.IZ.union_ex
#print axioms Found.IZ.power_ex
#print axioms Found.IZ.inf_ex

found/IZ10.lean

lean_all/found/IZ10.lean

/-
  Shared tool R5, part 5. found/IZ10 — the Lévy–Montague reflection theorem
  inside an arbitrary model of ZF:
  * the stages yₙ (n ∈ ω) of the recursion of found/IZ9 are ordinals,
    increasing, and each bounds the witnesses for parameters from the
    previous level;
  * α = ⋃ₙ yₙ; members of V_α lie in some V_{yₙ}, finitely many in a
    common one, and their witnesses in the next;
  * reflection: for every finite list of L0 formulas there is a nonempty
    level V_α such that, for parameters in V_α, each formula holds
    relativised to V_α iff it holds in the model.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.IZ9
import found.Refl3

namespace Found

namespace IZ

open FO FOCT NBGc RF

variable (M : ZM)

theorem def_Val (S : Step M) (i j : Nat) : Def M (fun v => Val S (v i) (v j)) :=
  def_congr M (fun _ => Iff.rfl) (def_ex M (def_ex M (def_and M (def_Att S 1 0)
    (def_and M (def_mem M (i + 2) 0) (def_InG M 1 (i + 2) (j + 2))))))

theorem val_resp (S : Step M) {b z b' z' : M.D} (eb : Ceq M.R b b') (ez : Ceq M.R z z') (h : Val S b z) : Val S b' z' :=
  def_resp M (def_Val M S 0 1) (v := tri b z b) (w := tri b' z' b') (fun n => match n with
    | 0 => eb
    | 1 => ez
    | 2 => eb
    | _ + 3 => eb) h

theorem wbl_get {ψ : Fm} : {Ψ : List Fm} → ψ ∈ Ψ → ∀ {Y w : M.D}, WBL M Ψ Y w → WBv M ψ (bound ψ) Y w
  | _ :: _, .head _, _, _, h => h.1
  | _ :: _, .tail _ m, _, _, h => wbl_get m h.2

theorem witG_of {D : Type} (R : Nat → List D → Prop) (C : D → Prop) : (φ : Fm) →
    (∀ ψ, ψ ∈ qb φ → HasWG R C ψ) → WitG R C φ
  | .atom _ _, _ => trivial
  | .bot, _ => trivial
  | .conj a b, h => ⟨witG_of R C a (fun ψ m => h ψ (mem_lapp_l _ m)), witG_of R C b (fun ψ m => h ψ (mem_lapp_r _ m))⟩
  | .disj a b, h => ⟨witG_of R C a (fun ψ m => h ψ (mem_lapp_l _ m)), witG_of R C b (fun ψ m => h ψ (mem_lapp_r _ m))⟩
  | .imp a b, h => ⟨witG_of R C a (fun ψ m => h ψ (mem_lapp_l _ m)), witG_of R C b (fun ψ m => h ψ (mem_lapp_r _ m))⟩
  | .all a, h => ⟨witG_of R C a (fun ψ m => h ψ (.tail _ m)), h _ (.head _)⟩
  | .ex a, h => ⟨witG_of R C a (fun ψ m => h ψ (.tail _ m)), h _ (.head _)⟩

section R

variable (K : CP.Classical') (Ψ : List Fm) (hΨ : L0L' Ψ)

theorem stage_ord {n y : M.D} (h : Val (stepS M Ψ hΨ K) n y) : Ord M y :=
  match h with
  | ⟨_, _, hat, hnd, hy⟩ => (hat.2.2.2.2 n y hnd hy).1.1

/-- earlier stages are included, and bound the witnesses of their levels -/
theorem stage_step {n m yn ym : M.D} (hnm : In M n m) (hn : Val (stepS M Ψ hΨ K) n yn)
    (hm : Val (stepS M Ψ hΨ K) m ym) :
    (∀ t, In M t yn → In M t ym) ∧ ∃ yu, Vl M yn yu ∧ WBL M Ψ yu ym :=
  match hm with
  | ⟨g, d, hat, hmd, hy⟩ =>
    have hq := (hat.2.2.2.2 m ym hmd hy).1
    have hnd : In M n d := hat.1.1 m n hmd hnm
    match hat.2.1 n hnd with
    | ⟨u, hu⟩ =>
      have e : Ceq M.R u yn := val_uniq (stepS M Ψ hΨ K) K ⟨g, d, hat, hnd, hu⟩ hn
      have hou : Ord M u := stage_ord M K Ψ hΨ ⟨g, d, hat, hnd, hu⟩
      match hq.2 n hnm u hu hou with
      | ⟨hs, yu, hyu, hwb⟩ => ⟨fun t ht => hs t ((e t).2 ht), yu, vl_resp M e (ceq_refl M.R yu) hyu, hwb⟩

end R

theorem mem_lapp_inv {x : Fm} : (l : List Fm) → {k : List Fm} → x ∈ lapp l k → x ∈ l ∨ x ∈ k
  | [], _, m => .inr m
  | _ :: l, _, m => by
    cases m with
    | head => exact .inl (.head _)
    | tail _ m' => exact (mem_lapp_inv l m').elim (fun h => .inl (.tail _ h)) .inr

theorem mem_qbL_inv {ψ : Fm} : (Φ : List Fm) → ψ ∈ qbL Φ → ∃ φ, φ ∈ Φ ∧ ψ ∈ qb φ
  | [], m => nomatch m
  | φ :: Φ, m => (mem_lapp_inv (qb φ) m).elim (fun h => ⟨φ, .head _, h⟩)
      (fun h => match mem_qbL_inv Φ h with | ⟨φ', h1, h2⟩ => ⟨φ', .tail _ h1, h2⟩)

/-- the reflection theorem inside a model of ZF -/
theorem reflection_D (K : CP.Classical') (Φ : List Fm) (hΦ : ∀ φ, φ ∈ Φ → L0 φ) :
    ∃ α yα, Ord M α ∧ Vl M α yα ∧ (∃ e, In M e yα) ∧ ∀ φ, φ ∈ Φ → ∀ v : Nat → M.D,
      (∀ n, In M (v n) yα) → (SatG M.R (fun x => In M x yα) φ v ↔ Sat M.R φ v) := by
  have hΨ : L0L' (.imp .bot .bot :: qbL Φ) := fun ψ m => by
    cases m with
    | head => exact ⟨trivial, trivial⟩
    | tail _ m' => exact match mem_qbL_inv Φ m' with
      | ⟨φ, hφ, hm⟩ => qb_l0 φ (hΦ φ hφ) ψ hm
  let S := stepS M (.imp .bot .bot :: qbL Φ) hΨ K
  match omega_exists M with
  | ⟨w, hInd, hmin⟩ =>
    have hwo : Ord M w := omega_ord M hInd hmin
    have hval : ∀ n, In M n w → ∃ y, Val S n y := fun n hn => val_exists S K (ord_mem M hwo hn)
    match repl M (def_Val M S 0 1) (fun _ => M.d0) w (fun _ _ z z' h1 h2 => val_uniq S K h1 h2) with
    | ⟨Ys, hYs⟩ =>
      have hYo : ∀ y, In M y Ys → Ord M y := fun y hy => match (hYs y).1 hy with
        | ⟨_, _, hv⟩ => stage_ord M K _ hΨ hv
      match union_ex M Ys with
      | ⟨α, hα⟩ =>
        have hαo : Ord M α := ord_union M hYo hα
        match vl_exists M K hαo with
        | ⟨yα, hyα⟩ =>
          have toα : ∀ {n y yl}, In M n w → Val S n y → Vl M y yl → ∀ t, In M t yl → In M t yα :=
            fun {n y yl} hn hv hyl => vl_mono' M K hαo (stage_ord M K _ hΨ hv) hyα hyl
              (fun t ht => (hα t).2 ⟨y, (hYs y).2 ⟨n, hn, hv⟩, ht⟩)
          have memstage : ∀ x, In M x yα → ∃ n y yn, In M n w ∧ Val S n y ∧ Vl M y yn ∧ In M x yn :=
            fun x hx => match (vl_mem M K hyα x).1 hx with
              | ⟨c, hc, u, hu, hs⟩ => match (hα c).1 hc with
                | ⟨y, hy, hcy⟩ => match (hYs y).1 hy with
                  | ⟨n, hn, hv⟩ => match vl_exists M K (stage_ord M K _ hΨ hv) with
                    | ⟨yn, hyn⟩ => ⟨n, y, yn, hn, hv, hyn, (vl_mem M K hyn x).2 ⟨c, hcy, u, hu, hs⟩⟩
          have common : ∀ (k : Nat) (v : Nat → M.D), (∀ i, i < k → In M (v i) yα) →
              ∃ N y yN, In M N w ∧ Val S N y ∧ Vl M y yN ∧ ∀ i, i < k → In M (v i) yN := by
            intro k
            induction k with
            | zero =>
              intro v _
              match hInd.1 with
              | ⟨e, he, _⟩ => match hval e he with
                | ⟨y, hy⟩ => match vl_exists M K (stage_ord M K _ hΨ hy) with
                  | ⟨yN, hyN⟩ => exact ⟨e, y, yN, he, hy, hyN, fun i hi => absurd hi (Nat.not_lt_zero i)⟩
            | succ k ih =>
              intro v hv
              match ih v (fun i hi => hv i (Nat.lt_succ_of_lt hi)), memstage (v k) (hv k (Nat.lt_succ_self k)) with
              | ⟨N1, y1, l1, hN1, hv1, hl1, hin1⟩, ⟨n, y, yn, hn, hvn, hyn, hk⟩ =>
                have o1 : Ord M N1 := ord_mem M hwo hN1
                have on : Ord M n := ord_mem M hwo hn
                have last : ∀ i, i < k + 1 → i < k ∨ i = k := fun i hi =>
                  (Nat.lt_or_ge i k).elim .inl (fun hge => .inr (Nat.le_antisymm (Nat.le_of_lt_succ hi) hge))
                match ord_cmp M K o1 on with
                | .inl h =>
                  have sub := vl_mono' M K (stage_ord M K _ hΨ hvn) (stage_ord M K _ hΨ hv1) hyn hl1
                    (stage_step M K _ hΨ h hv1 hvn).1
                  exact ⟨n, y, yn, hn, hvn, hyn, fun i hi => (last i hi).elim (fun h' => sub _ (hin1 i h'))
                    (fun e => e ▸ hk)⟩
                | .inr (.inl e) =>
                  have ey : Ceq M.R y y1 := val_uniq S K hvn (val_resp M S e (ceq_refl M.R y1) hv1)
                  have el : Ceq M.R yn l1 := vl_uniq M K (vl_resp M ey (ceq_refl M.R yn) hyn) hl1
                  exact ⟨N1, y1, l1, hN1, hv1, hl1, fun i hi => (last i hi).elim (fun h' => hin1 i h')
                    (fun e' => e' ▸ (el _).1 hk)⟩
                | .inr (.inr h) =>
                  have sub := vl_mono' M K (stage_ord M K _ hΨ hv1) (stage_ord M K _ hΨ hvn) hl1 hyn
                    (stage_step M K _ hΨ h hvn hv1).1
                  exact ⟨N1, y1, l1, hN1, hv1, hl1, fun i hi => (last i hi).elim (fun h' => hin1 i h')
                    (fun e => e ▸ sub _ hk)⟩
          -- witnesses in the next stage
          have hW' : ∀ ψ, ψ ∈ (.imp .bot .bot :: qbL Φ) → ∀ v : Nat → M.D, (∀ i, i < bound ψ → In M (v i) yα) →
              (∃ d, Sat M.R ψ (ext v d)) → ∃ d, In M d yα ∧ Sat M.R ψ (ext v d) := fun ψ hψ v hv hex =>
            match common (bound ψ) v hv with
            | ⟨N, y, yN, hN, hvN, hyN, hin⟩ => match hInd.2 N hN with
              | ⟨N', hN', hs'⟩ => match hval N' hN' with
                | ⟨y', hv'⟩ =>
                  match stage_step M K _ hΨ ((hs' N).2 (.inr (ceq_refl M.R N))) hvN hv' with
                  | ⟨_, yu, hyu, hwb⟩ =>
                    have eu : Ceq M.R yN yu := vl_uniq M K hyN hyu
                    match wbv_meta M ψ (bound ψ) (fv_b ψ) (wbl_get M hψ hwb) v
                        (fun i hi => (eu _).1 (hin i hi)) hex with
                    | ⟨d, ⟨yw, hyw, hd⟩, hsd⟩ => ⟨d, toα hN' hv' hyw d hd, hsd⟩
          have hW : ∀ ψ, ψ ∈ (.imp .bot .bot :: qbL Φ) → HasWG M.R (fun x => In M x yα) ψ :=
            fun ψ hψ v hv hex => hW' ψ hψ v (fun i _ => hv i) hex
          have nonempty : ∃ e, In M e yα :=
            match hW' (.imp .bot .bot) (.head _) (fun _ => M.d0) (fun i hi => absurd hi (Nat.not_lt_zero i))
                ⟨M.d0, fun h => h⟩ with
            | ⟨d, hd, _⟩ => ⟨d, hd⟩
          exact ⟨α, yα, hαo, hyα, nonempty, fun φ hφ v hv =>
            tvG M.R (fun x => In M x yα) K φ (witG_of M.R _ φ (fun ψ m => hW ψ (.tail _ (mem_qbL hφ m)))) v hv⟩

end IZ

end Found

#print axioms Found.IZ.stage_step
#print axioms Found.IZ.reflection_D

found/IZ11.lean

lean_all/found/IZ11.lean

/-
  T165 (general), part G1. found/IZ11 — choice functions inside a model of
  ZFC (the axiom of choice of found/NbgZ6, in its disjoint-family form):
  for every set A of nonempty sets there is a set-function c with a value
  in a for every a ∈ A, single-valued up to coextension. The members of a
  are made disjoint as the pairs ⟨a, t⟩; the choice set of the family
  {⟨a, t⟩ | t ∈ a} (a ∈ A), cut down to such pairs, is the function.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.IZ4p

namespace Found

namespace IZ

open FO FOCT NBGc

variable (M : ZM)

/-- the unfolded choice axiom -/
def ACsem : Prop := ∀ a : M.D, (∀ x, In M x a → ∃ t, In M t x) →
  (∀ x y, In M x a → In M y a → (CeqC M.R x y ∨ ((∃ t, In M t x ∧ In M t y) → False))) →
  ∃ c, ∀ x, In M x a → ∃ u, In M u x ∧ In M u c ∧ ∀ s, In M s x → In M s c → CeqC M.R s u

theorem ac_sem (h : ∀ v : Nat → M.D, Sat M.R acAx v) : ACsem M := h (fun _ => M.d0)

/-- the set {⟨a, t⟩ | t ∈ a} -/
theorem prod1_exists (a : M.D) : ∃ w, ∀ p, In M p w ↔ ∃ t, In M t a ∧ OPs M.R p a t :=
  match repl M (Θ := fun u => OPs M.R (u 1) (u 2) (u 0)) (def_op M 1 2 0) (fun _ => a) a
      (fun _ _ _ _ h1 h2 => op_uniq M h1 h2) with
  | ⟨w, hw⟩ => ⟨w, fun p => hw p⟩

theorem choice_fn (K : CP.Classical') (hac : ACsem M) (A : M.D) (hne : ∀ a, In M a A → ∃ t, In M t a) :
    ∃ c, (∀ a, In M a A → ∃ t, In M t a ∧ InG M c a t) ∧
      (∀ a t t', In M a A → InG M c a t → InG M c a t' → Ceq M.R t t') := by
  -- the family of the sets {⟨a, t⟩ | t ∈ a}
  let W : (Nat → M.D) → Prop := fun u => ∀ p, In M p (u 1) ↔ ∃ t, In M t (u 0) ∧ OPs M.R p (u 0) t
  have hW : Def M W := def_congr M (fun _ => Iff.rfl) (def_all M (def_iff M (def_mem M 0 2)
    (def_ex M (def_and M (def_mem M 0 2) (def_op M 1 2 0)))))
  match repl M hW (fun _ => M.d0) A (fun _ _ z z' h1 h2 p => (h1 p).trans (h2 p).symm) with
  | ⟨F, hF⟩ =>
    -- members of F are nonempty and pairwise disjoint or equal
    have memF : ∀ x, In M x F → ∃ a, In M a A ∧ ∀ p, In M p x ↔ ∃ t, In M t a ∧ OPs M.R p a t := fun x hx =>
      match (hF x).1 hx with
      | ⟨a, ha, hw⟩ => ⟨a, ha, hw⟩
    match hac F
        (fun x hx => match memF x hx with
          | ⟨a, ha, hw⟩ => match hne a ha with
            | ⟨t, ht⟩ => match op_exists M a t with
              | ⟨p, hp⟩ => ⟨p, (hw p).2 ⟨t, ht, hp⟩⟩)
        (fun x y hx hy => match memF x hx, memF y hy with
          | ⟨a, _, hwa⟩, ⟨b, _, hwb⟩ =>
            (K.lem (∃ t, In M t x ∧ In M t y)).elim
              (fun ⟨t, h1, h2⟩ => match (hwa t).1 h1, (hwb t).1 h2 with
                | ⟨s1, _, ho1⟩, ⟨s2, _, ho2⟩ =>
                  have eab : Ceq M.R a b := (op_inj' M ho1 ho2).1
                  .inl (c1 M.R (fun p => (hwa p).trans ((hwb p).trans
                    ⟨fun ⟨s, hs, ho⟩ => ⟨s, (eab s).2 hs, op_resp M ho (ceq_refl M.R p) (ceq_symm M.R eab) (ceq_refl M.R s)⟩,
                     fun ⟨s, hs, ho⟩ => ⟨s, (eab s).1 hs, op_resp M ho (ceq_refl M.R p) eab (ceq_refl M.R s)⟩⟩).symm)))
              (fun hn => .inr hn)) with
    | ⟨c0, hc0⟩ =>
      let P : (Nat → M.D) → Prop := fun u => ∃ a, In M a (u 1) ∧ ∃ t, In M t a ∧ OPs M.R (u 0) a t
      have hP : Def M P := def_congr M (fun _ => Iff.rfl) (def_ex M (def_and M (def_mem M 0 2)
        (def_ex M (def_and M (def_mem M 0 1) (def_op M 2 1 0)))))
      match sep M hP (fun _ => A) c0 with
      | ⟨c, hc⟩ =>
        have Wa : ∀ a, In M a A → ∃ w, In M w F ∧ ∀ p, In M p w ↔ ∃ t, In M t a ∧ OPs M.R p a t := fun a ha =>
          match prod1_exists M a with
          | ⟨w, hw⟩ => ⟨w, (hF w).2 ⟨a, ha, hw⟩, hw⟩
        exact ⟨c, fun a ha => match Wa a ha with
          | ⟨w, hwF, hw⟩ => match hc0 w hwF with
            | ⟨u, huw, huc, _⟩ => match (hw u).1 huw with
              | ⟨t, ht, hop⟩ => ⟨t, ht, u, (hc u).2 ⟨huc, a, ha, t, ht, hop⟩, hop⟩,
          fun a t t' ha ⟨p, hp, hop⟩ ⟨p', hp', hop'⟩ => match Wa a ha with
          | ⟨w, hwF, hw⟩ => match hc0 w hwF with
            | ⟨u, _, _, huniq⟩ =>
              have inw : ∀ q s, In M q c → OPs M.R q a s → In M q w := fun q s hq ho =>
                match (hc q).1 hq with
                | ⟨_, a1, _, t1, ht1, ho1⟩ =>
                  have e := op_inj' M ho1 ho
                  (hw q).2 ⟨s, mem_ceq_l M e.2 (mem_ceq_r M e.1 ht1), ho⟩
              have hpu := c2 M.R (huniq p (inw p t hp hop) ((hc p).1 hp).1)
              have hpu' := c2 M.R (huniq p' (inw p' t' hp' hop') ((hc p').1 hp').1)
              (op_inj' M hop (op_resp M hop' (ceq_trans M.R hpu' (ceq_symm M.R hpu)) (ceq_refl M.R a)
                (ceq_refl M.R t'))).2⟩

end IZ

end Found

#print axioms Found.IZ.prod1_exists
#print axioms Found.IZ.choice_fn

found/IZ12.lean

lean_all/found/IZ12.lean

/-
  T165 (general), part G2. found/IZ12 — Zermelo's well-ordering theorem
  inside a model of ZFC: every set X is enumerated without repetition by a
  set-function e on some ordinal δ. The enumeration is the transfinite
  recursion (found/IZ4p, parameters X and a choice function c on the
  nonempty subsets of X, found/IZ11) "the chosen member of what is left";
  the recursion runs out below some ordinal, since otherwise replacement on
  X would collect all ordinals into a set.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.IZ11

namespace Found

namespace IZ

open FO FOCT NBGc

variable (M : ZM)

/-! ## values of a parametrised recursion -/

section V

variable {M} (S : StepP M) (ρ : Nat → M.D)

theorem def_ValP (i j c : Nat) : Def M (fun v => ValP S (fun n => v (n + c)) (v i) (v j)) :=
  def_congr M (fun _ => Iff.rfl) (def_ex M (def_ex M (def_and M (def_AttP S 1 0 (c + 2))
    (def_and M (def_mem M (i + 2) 0) (def_InG M 1 (i + 2) (j + 2))))))

def vec2 (b z : M.D) : Nat → M.D
  | 0 => b
  | 1 => z
  | n + 2 => ρ n

theorem valP_resp {b z b' z' : M.D} (eb : Ceq M.R b b') (ez : Ceq M.R z z') (h : ValP S ρ b z) :
    ValP S ρ b' z' :=
  def_resp M (def_ValP S 0 1 2) (v := vec2 ρ b z) (w := vec2 ρ b' z') (fun n => match n with
    | 0 => eb
    | 1 => ez
    | _ + 2 => ceq_refl M.R _) h

theorem valP_step (K : CP.Classical') {b z : M.D} (h : ValP S ρ b z) :
    ∃ g, S.G z b g ρ ∧ ∀ c, In M c b → ∀ u, (InG M g c u ↔ ValP S ρ c u) :=
  match h with
  | ⟨g, d, ha, hb, hz⟩ => ⟨g, ha.2.2.2.2 b z hb hz, fun c hc _ =>
    have hcd : In M c d := ha.1.1 b c hb hc
    ⟨fun h => ⟨g, d, ha, hcd, h⟩,
     fun h => match ha.2.1 c hcd with
      | ⟨_, hu'⟩ => InG_resp M (ceq_refl M.R _) (ceq_refl M.R _)
          (valP_uniq S ρ K ⟨g, d, ha, hcd, hu'⟩ h) hu'⟩⟩

end V

/-! ## the step -/

def RestZ (g b X t : M.D) : Prop := In M t X ∧ ∀ e z, In M e b → InG M g e z → ¬ Ceq M.R t z

theorem def_RestZ (gi bi Xi ti : Nat) : Def M (fun v => RestZ M (v gi) (v bi) (v Xi) (v ti)) :=
  def_congr M (fun _ => Iff.rfl) (def_and M (def_mem M ti Xi) (def_all M (def_all M (def_imp M
    (def_mem M 1 (bi + 2)) (def_imp M (def_InG M (gi + 2) 1 0) (def_not M (def_ceq M (ti + 2) 0)))))))

def GoodZ (X c : M.D) : Prop := ∀ r, (∀ t, In M t r → In M t X) → (∃ t, In M t r) →
  (∃ y, In M y r ∧ InG M c r y) ∧ ∀ y y', InG M c r y → InG M c r y' → Ceq M.R y y'

theorem def_GoodZ (Xi ci : Nat) : Def M (fun v => GoodZ M (v Xi) (v ci)) :=
  def_congr M (fun _ => Iff.rfl) (def_all M (def_imp M
    (def_all M (def_imp M (def_mem M 0 1) (def_mem M 0 (Xi + 2))))
    (def_imp M (def_ex M (def_mem M 0 1))
      (def_and M (def_ex M (def_and M (def_mem M 0 1) (def_InG M (ci + 2) 1 0)))
        (def_all M (def_all M (def_imp M (def_InG M (ci + 3) 2 1)
          (def_imp M (def_InG M (ci + 3) 2 0) (def_ceq M 1 0)))))))))

def MainZ (y b g X c : M.D) : Prop := ∀ r, (∀ t, In M t r ↔ RestZ M g b X t) →
  ((∃ t, In M t r) → InG M c r y) ∧ (¬ (∃ t, In M t r) → Ceq M.R y X)

theorem def_MainZ (yi bi gi Xi ci : Nat) : Def M (fun v => MainZ M (v yi) (v bi) (v gi) (v Xi) (v ci)) :=
  def_congr M (fun _ => Iff.rfl) (def_all M (def_imp M
    (def_all M (def_iff M (def_mem M 0 1) (def_RestZ M (gi + 2) (bi + 2) (Xi + 2) 0)))
    (def_and M (def_imp M (def_ex M (def_mem M 0 1)) (def_InG M (ci + 1) 0 (yi + 1)))
      (def_imp M (def_not M (def_ex M (def_mem M 0 1))) (def_ceq M (yi + 1) (Xi + 1))))))

def GZ (y b g : M.D) (ρ : Nat → M.D) : Prop :=
  (GoodZ M (ρ 0) (ρ 1) → MainZ M y b g (ρ 0) (ρ 1)) ∧ (¬ GoodZ M (ρ 0) (ρ 1) → Ceq M.R y (ρ 0))

def vec3 (g b X : M.D) : Nat → M.D
  | 0 => g
  | 1 => b
  | _ + 2 => X

theorem rest_ex (g b X : M.D) : ∃ r, ∀ t, In M t r ↔ RestZ M g b X t :=
  match sep M (def_RestZ M 1 2 3 0) (vec3 M g b X) X with
  | ⟨r, hr⟩ => ⟨r, fun t => (hr t).trans ⟨fun h => h.2, fun h => ⟨h.1, h⟩⟩⟩

theorem rest_loc {g g' b X : M.D} (ag : ∀ c, In M c b → ∀ z, InG M g c z ↔ InG M g' c z) (t : M.D) :
    RestZ M g b X t → RestZ M g' b X t := fun ⟨h1, h2⟩ =>
  ⟨h1, fun e z he hz => h2 e z he ((ag e he z).2 hz)⟩

def stepZ (K : CP.Classical') : StepP M where
  G := GZ M
  dG := def_and M (def_imp M (def_GoodZ M 3 4) (def_MainZ M 0 1 2 3 4))
    (def_imp M (def_not M (def_GoodZ M 3 4)) (def_ceq M 0 3))
  loc := fun _ _ _ _ _ ag h => ⟨fun hG r hr => h.1 hG r (fun t => (hr t).trans
      ⟨rest_loc M (fun c hc z => (ag c hc z).symm) t, rest_loc M ag t⟩), h.2⟩
  fn := fun ρ g b y y' h1 h2 => (K.lem (GoodZ M (ρ 0) (ρ 1))).elim
    (fun hG => match rest_ex M g b (ρ 0) with
      | ⟨r, hr⟩ =>
        have m1 := h1.1 hG r hr
        have m2 := h2.1 hG r hr
        (K.lem (∃ t, In M t r)).elim
          (fun hne => (hG r (fun t ht => ((hr t).1 ht).1) hne).2 y y' (m1.1 hne) (m2.1 hne))
          (fun he => ceq_trans M.R (m1.2 he) (ceq_symm M.R (m2.2 he))))
    (fun hn => ceq_trans M.R (h1.2 hn) (ceq_symm M.R (h2.2 hn)))
  ex := fun ρ g b _ _ => (K.lem (GoodZ M (ρ 0) (ρ 1))).elim
    (fun hG => match rest_ex M g b (ρ 0) with
      | ⟨r, hr⟩ => (K.lem (∃ t, In M t r)).elim
        (fun hne => match (hG r (fun t ht => ((hr t).1 ht).1) hne).1 with
          | ⟨y, _, hy⟩ => ⟨y, fun _ r' hr' =>
            have e : Ceq M.R r r' := fun t => (hr t).trans (hr' t).symm
            ⟨fun _ => InG_resp M (ceq_refl M.R _) e (ceq_refl M.R _) hy,
             fun hn => (hn (match hne with | ⟨t, ht⟩ => ⟨t, (e t).1 ht⟩)).elim⟩,
            fun hn => (hn hG).elim⟩)
        (fun he => ⟨ρ 0, fun _ r' hr' =>
          have e : Ceq M.R r r' := fun t => (hr t).trans (hr' t).symm
          ⟨fun hne' => (he (match hne' with | ⟨t, ht⟩ => ⟨t, (e t).2 ht⟩)).elim,
           fun _ => ceq_refl M.R _⟩, fun _ => ceq_refl M.R _⟩))
    (fun hn => ⟨ρ 0, fun hG => (hn hG).elim, fun _ => ceq_refl M.R _⟩)

/-! ## the enumeration -/

section E

variable (K : CP.Classical') (ρ : Nat → M.D)

/-- what is left of X after the values below β -/
def RestV (X β t : M.D) : Prop := In M t X ∧ ∀ γ u, In M γ β → ValP (stepZ M K) ρ γ u → ¬ Ceq M.R t u

theorem def_RestV (xi bi ti c : Nat) : Def M (fun v => RestV M K (fun n => v (n + c)) (v xi) (v bi) (v ti)) :=
  def_congr M (fun _ => Iff.rfl) (def_and M (def_mem M ti xi) (def_all M (def_all M (def_imp M
    (def_mem M 1 (bi + 2)) (def_imp M (def_ValP (stepZ M K) 1 0 (c + 2)) (def_not M (def_ceq M (ti + 2) 0)))))))

theorem zval (hG : GoodZ M (ρ 0) (ρ 1)) {β z : M.D} (h : ValP (stepZ M K) ρ β z) :
    ((∃ t, RestV M K ρ (ρ 0) β t) → RestV M K ρ (ρ 0) β z) ∧ (¬ (∃ t, RestV M K ρ (ρ 0) β t) → Ceq M.R z (ρ 0)) :=
  match valP_step (stepZ M K) ρ K h with
  | ⟨g, hg, ag⟩ =>
    have rr : ∀ t, RestZ M g β (ρ 0) t ↔ RestV M K ρ (ρ 0) β t := fun _ =>
      ⟨fun ⟨h1, h2⟩ => ⟨h1, fun γ u hγ hv => h2 γ u hγ ((ag γ hγ u).2 hv)⟩,
       fun ⟨h1, h2⟩ => ⟨h1, fun γ u hγ hv => h2 γ u hγ ((ag γ hγ u).1 hv)⟩⟩
    match rest_ex M g β (ρ 0) with
    | ⟨r, hr⟩ =>
      have mm := hg.1 hG r hr
      ⟨fun ⟨t, ht⟩ =>
        have hne : ∃ t, In M t r := ⟨t, (hr t).2 ((rr t).2 ht)⟩
        have hsub : ∀ t, In M t r → In M t (ρ 0) := fun t ht => ((hr t).1 ht).1
        match (hG r hsub hne).1 with
        | ⟨y, hyr, hy⟩ =>
          have e : Ceq M.R y z := (hG r hsub hne).2 y z hy (mm.1 hne)
          (rr z).1 ((hr z).1 (mem_ceq_l M e hyr)),
       fun hn => mm.2 (fun ⟨t, ht⟩ => hn ⟨t, (rr t).1 ((hr t).1 ht)⟩)⟩

def ΘO (u : Nat → M.D) : Prop := Ord M (u 1) ∧ ValP (stepZ M K) (fun n => u (n + 2)) (u 1) (u 0)

theorem z_exhaust (hG : GoodZ M (ρ 0) (ρ 1)) : ∃ δ, Ord M δ ∧ ¬ ∃ t, RestV M K ρ (ρ 0) δ t :=
  CP.dne K _ (fun H =>
    have all : ∀ δ, Ord M δ → ∃ t, RestV M K ρ (ρ 0) δ t := fun δ hδ =>
      CP.dne K _ (fun hn => H ⟨δ, hδ, hn⟩)
    have rv : ∀ β z, Ord M β → ValP (stepZ M K) ρ β z → RestV M K ρ (ρ 0) β z := fun β _ hβ hv =>
      (zval M K ρ hG hv).1 (all β hβ)
    have hΘ : Def M (ΘO M K) := def_and M (def_Ord M 1) (def_ValP (stepZ M K) 1 0 2)
    match repl M hΘ ρ (ρ 0) (fun w _ z z' ⟨oz, vz⟩ ⟨oz', vz'⟩ =>
        (ord_cmp M K oz oz').elim
          (fun hm => ((rv z' w oz' vz').2 z w hm vz (ceq_refl M.R w)).elim)
          (fun h => h.elim id (fun hm => ((rv z w oz vz).2 z' w hm vz' (ceq_refl M.R w)).elim))) with
    | ⟨O, hO⟩ =>
      have inO : ∀ β, Ord M β → In M β O := fun β hβ => match valP_exists (stepZ M K) ρ K hβ with
        | ⟨w, hw⟩ => (hO β).2 ⟨w, (rv β w hβ hw).1, hβ, hw⟩
      match union_ex M O with
      | ⟨U, hU⟩ =>
        have hUo : Ord M U := ord_union M (fun y hy => match (hO y).1 hy with
          | ⟨_, _, ho, _⟩ => ho) hU
        match succ_exists M U with
        | ⟨U', hU'⟩ =>
          have hU'o : Ord M U' := ord_succ M hUo hU'
          not_mem_self M U ((hU U).2 ⟨U', inO U' hU'o, (hU' U).2 (.inr (ceq_refl M.R U))⟩))

end E

def vecXc (X c : M.D) : Nat → M.D
  | 0 => X
  | _ + 1 => c

/-- Zermelo: every set is enumerated without repetition by an ordinal -/
theorem zermelo (K : CP.Classical') (hac : ACsem M) (X : M.D) :
    ∃ δ e, Ord M δ ∧ (∀ β, In M β δ → ∃ x, In M x X ∧ InG M e β x) ∧
      (∀ β x, InG M e β x → In M β δ) ∧
      (∀ β x x', InG M e β x → InG M e β x' → Ceq M.R x x') ∧
      (∀ x, In M x X → ∃ β, In M β δ ∧ InG M e β x) ∧
      (∀ β γ x, InG M e β x → InG M e γ x → Ceq M.R β γ) := by
  match power_ex M X with
  | ⟨P, hP⟩ =>
  match sep M (P := fun u => ∃ t, In M t (u 0)) (def_ex M (def_mem M 0 1)) (fun _ => X) P with
  | ⟨A, hA⟩ =>
  match choice_fn M K hac A (fun a ha => ((hA a).1 ha).2) with
  | ⟨c, hc1, hc2⟩ =>
  let ρ := vecXc M X c
  have hG : GoodZ M (ρ 0) (ρ 1) := fun r hsub hne =>
    have hrA : In M r A := (hA r).2 ⟨(hP r).2 hsub, hne⟩
    ⟨hc1 r hrA, fun y y' h1 h2 => hc2 r y y' hrA h1 h2⟩
  let S := stepZ M K
  have hP0 : Def M (fun u => ¬ ∃ t, RestV M K (fun n => u (n + 1)) (u 1) (u 0) t) :=
    def_not M (def_congr M (fun _ => Iff.rfl) (def_ex M (def_RestV M K 2 1 0 2)))
  match ord_least M K hP0 ρ (z_exhaust M K ρ hG) with
  | ⟨δ, hδ, hemp, hmin⟩ =>
  have nonemp : ∀ β, In M β δ → ∃ t, RestV M K ρ (ρ 0) β t := fun β hβ =>
    CP.dne K _ (fun hn => (hmin β (ord_mem M hδ hβ) hn).elim
      (fun hm => not_mem_self M δ (hδ.1 β δ hβ hm))
      (fun e => not_mem_self M β (mem_ceq_r M e hβ)))
  have rv : ∀ β z, In M β δ → ValP S ρ β z → RestV M K ρ (ρ 0) β z := fun β z hβ hv =>
    (zval M K ρ hG hv).1 (nonemp β hβ)
  let Θ : (Nat → M.D) → Prop := fun u => ∃ x, ValP S (fun n => u (n + 2)) (u 0) x ∧ OPs M.R (u 1) (u 0) x
  have hΘ : Def M Θ := def_congr M (fun _ => Iff.rfl)
    (def_ex M (def_and M (def_ValP S 1 0 3) (def_op M 2 1 0)))
  match repl M hΘ ρ δ (fun β _ p p' ⟨x, hx, ho⟩ ⟨x', hx', ho'⟩ =>
      op_uniq M ho (op_resp M ho' (ceq_refl M.R p') (ceq_refl M.R β) (valP_uniq S ρ K hx' hx))) with
  | ⟨e, he⟩ =>
  have eg : ∀ β x, InG M e β x → In M β δ ∧ ValP S ρ β x := fun β x ⟨p, hp, ho⟩ =>
    match (he p).1 hp with
    | ⟨w, hw, x', hv, ho'⟩ =>
      have q := op_inj' M ho' ho
      ⟨mem_ceq_l M q.1 hw, valP_resp S ρ q.1 q.2 hv⟩
  have ge : ∀ β x, In M β δ → ValP S ρ β x → InG M e β x := fun β x hβ hv =>
    match op_exists M β x with
    | ⟨p, hp⟩ => ⟨p, (he p).2 ⟨β, hβ, x, hv, hp⟩, hp⟩
  exact ⟨δ, e, hδ,
    fun β hβ => match valP_exists S ρ K (ord_mem M hδ hβ) with
      | ⟨x, hx⟩ => ⟨x, (rv β x hβ hx).1, ge β x hβ hx⟩,
    fun β x h => (eg β x h).1,
    fun β x x' h1 h2 => valP_uniq S ρ K (eg β x h1).2 (eg β x' h2).2,
    fun x hx => CP.dne K _ (fun hn => hemp ⟨x, hx, fun γ u hγ hv hcu =>
      hn ⟨γ, hγ, ge γ x hγ (valP_resp S ρ (ceq_refl M.R γ) (ceq_symm M.R hcu) hv)⟩⟩),
    fun β γ x h1 h2 =>
      have a1 := eg β x h1
      have a2 := eg γ x h2
      (ord_cmp M K (ord_mem M hδ a1.1) (ord_mem M hδ a2.1)).elim
        (fun hm => ((rv γ x a2.1 a2.2).2 β x hm a1.2 (ceq_refl M.R x)).elim)
        (fun h => h.elim id (fun hm => ((rv β x a1.1 a1.2).2 γ x hm a2.2 (ceq_refl M.R x)).elim))⟩

end IZ

end Found

#print axioms Found.IZ.def_ValP
#print axioms Found.IZ.valP_resp
#print axioms Found.IZ.valP_step
#print axioms Found.IZ.stepZ
#print axioms Found.IZ.zval
#print axioms Found.IZ.z_exhaust
#print axioms Found.IZ.zermelo

found/IZ13.lean

lean_all/found/IZ13.lean

/-
  T165 (general), part G3. found/IZ13 — the ultrafilter theorem inside a
  model of ZFC: every directed family F₀ of nonempty subsets of a set I
  extends to an ultrafilter on I (a set of the model). The subsets of I are
  enumerated by an ordinal δ (found/IZ12); the stage F_β is the upward
  closure of F₀ ∪ ⋃_{γ<β} F_γ cut down by the β-th subset S when S meets
  every member, and by its complement otherwise; F_δ is the ultrafilter.
  Every stage is a proper filter by induction (least failing ordinal).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.IZ12

namespace Found

namespace IZ

open FO FOCT NBGc

variable (M : ZM)

def SubS (a b : M.D) : Prop := ∀ t, In M t a → In M t b

theorem def_SubS (ai bi : Nat) : Def M (fun v => SubS M (v ai) (v bi)) :=
  def_congr M (fun _ => Iff.rfl) (def_all M (def_imp M (def_mem M 0 (ai + 1)) (def_mem M 0 (bi + 1))))

/-- A is in F₀ or in an earlier stage -/
def InH (F0 g b A : M.D) : Prop := In M A F0 ∨ ∃ γ z, In M γ b ∧ InG M g γ z ∧ In M A z

theorem def_InH (fi gi bi ai : Nat) : Def M (fun v => InH M (v fi) (v gi) (v bi) (v ai)) :=
  def_congr M (fun _ => Iff.rfl) (def_or M (def_mem M ai fi) (def_ex M (def_ex M (def_and M
    (def_mem M 1 (bi + 2)) (def_and M (def_InG M (gi + 2) 1 0) (def_mem M (ai + 2) 0))))))

/-- S meets every member of the earlier stages -/
def Mt (F0 g b S : M.D) : Prop := ∀ A, InH M F0 g b A → ∃ t, In M t A ∧ In M t S

theorem def_Mt (fi gi bi si : Nat) : Def M (fun v => Mt M (v fi) (v gi) (v bi) (v si)) :=
  def_congr M (fun _ => Iff.rfl) (def_all M (def_imp M (def_InH M (fi + 1) (gi + 1) (bi + 1) 0)
    (def_ex M (def_and M (def_mem M 0 1) (def_mem M 0 (si + 2))))))

/-- t lies on the chosen side of the b-th subset -/
def Dec (F0 g b e t : M.D) : Prop :=
  ∀ S, InG M e b S → (Mt M F0 g b S → In M t S) ∧ (¬ Mt M F0 g b S → ¬ In M t S)

theorem def_Dec (fi gi bi ei ti : Nat) : Def M (fun v => Dec M (v fi) (v gi) (v bi) (v ei) (v ti)) :=
  def_congr M (fun _ => Iff.rfl) (def_all M (def_imp M (def_InG M (ei + 1) (bi + 1) 0)
    (def_and M (def_imp M (def_Mt M (fi + 1) (gi + 1) (bi + 1) 0) (def_mem M (ti + 1) 0))
      (def_imp M (def_not M (def_Mt M (fi + 1) (gi + 1) (bi + 1) 0)) (def_not M (def_mem M (ti + 1) 0))))))

/-- membership in the stage -/
def RU (I F0 e g b B : M.D) : Prop :=
  SubS M B I ∧ ∃ A, InH M F0 g b A ∧ ∀ t, In M t A → Dec M F0 g b e t → In M t B

theorem def_RU (ii fi ei gi bi Bi : Nat) : Def M (fun v => RU M (v ii) (v fi) (v ei) (v gi) (v bi) (v Bi)) :=
  def_congr M (fun _ => Iff.rfl) (def_and M (def_SubS M Bi ii) (def_ex M (def_and M
    (def_InH M (fi + 1) (gi + 1) (bi + 1) 0)
    (def_all M (def_imp M (def_mem M 0 1) (def_imp M (def_Dec M (fi + 2) (gi + 2) (bi + 2) (ei + 2) 0)
      (def_mem M 0 (Bi + 2))))))))

def GU (y b g : M.D) (ρ : Nat → M.D) : Prop := ∀ B, In M B y ↔ RU M (ρ 0) (ρ 1) (ρ 2) g b B

section L

variable {M} {F0 g g' b : M.D}

theorem inh_loc (ag : ∀ c, In M c b → ∀ z, InG M g c z ↔ InG M g' c z) (A : M.D) : InH M F0 g b A ↔ InH M F0 g' b A :=
  ⟨fun h => h.elim .inl (fun ⟨γ, z, hγ, hg, hA⟩ => .inr ⟨γ, z, hγ, (ag γ hγ z).1 hg, hA⟩),
   fun h => h.elim .inl (fun ⟨γ, z, hγ, hg, hA⟩ => .inr ⟨γ, z, hγ, (ag γ hγ z).2 hg, hA⟩)⟩

theorem mt_loc (ag : ∀ c, In M c b → ∀ z, InG M g c z ↔ InG M g' c z) (S : M.D) : Mt M F0 g b S ↔ Mt M F0 g' b S :=
  ⟨fun h A hA => h A ((inh_loc ag A).2 hA), fun h A hA => h A ((inh_loc ag A).1 hA)⟩

theorem dec_loc (ag : ∀ c, In M c b → ∀ z, InG M g c z ↔ InG M g' c z) (e t : M.D) : Dec M F0 g b e t ↔ Dec M F0 g' b e t :=
  ⟨fun h S hS => ⟨fun m => (h S hS).1 ((mt_loc ag S).2 m), fun nm => (h S hS).2 (fun m => nm ((mt_loc ag S).1 m))⟩,
   fun h S hS => ⟨fun m => (h S hS).1 ((mt_loc ag S).1 m), fun nm => (h S hS).2 (fun m => nm ((mt_loc ag S).2 m))⟩⟩

theorem ru_loc (ag : ∀ c, In M c b → ∀ z, InG M g c z ↔ InG M g' c z) (I e B : M.D) : RU M I F0 e g b B ↔ RU M I F0 e g' b B :=
  ⟨fun ⟨hs, A, hA, ht⟩ => ⟨hs, A, (inh_loc ag A).1 hA, fun t h1 h2 => ht t h1 ((dec_loc ag e t).2 h2)⟩,
   fun ⟨hs, A, hA, ht⟩ => ⟨hs, A, (inh_loc ag A).2 hA, fun t h1 h2 => ht t h1 ((dec_loc ag e t).1 h2)⟩⟩

end L

def stepU : StepP M where
  G := GU M
  dG := def_congr M (fun _ => Iff.rfl) (def_all M (def_iff M (def_mem M 0 1) (def_RU M 4 5 6 3 2 0)))
  loc := fun _ _ _ _ _ ag h B => (h B).trans (ru_loc ag _ _ B)
  fn := fun _ _ _ _ _ h1 h2 B => (h1 B).trans (h2 B).symm
  ex := fun ρ g b _ _ => match power_ex M (ρ 0) with
    | ⟨P, hP⟩ => match sep M (def_RU M 3 4 5 2 1 0) (vec2 ρ b g) P with
      | ⟨y, hy⟩ => ⟨y, fun B => (hy B).trans ⟨fun h => h.2, fun h => ⟨(hP B).2 h.1, h⟩⟩⟩

/-- a proper filter on I containing F₀ -/
def PF (I F0 y : M.D) : Prop :=
  (∀ B, In M B y → SubS M B I) ∧ (∀ A, In M A F0 → In M A y) ∧
  (∀ A B C, In M A y → In M B y → (∀ t, In M t C ↔ In M t A ∧ In M t B) → In M C y) ∧
  (∀ A B, In M A y → SubS M B I → SubS M A B → In M B y) ∧ (∀ A, In M A y → ∃ t, In M t A)

theorem def_PF (ii fi yi : Nat) : Def M (fun v => PF M (v ii) (v fi) (v yi)) :=
  def_congr M (fun _ => Iff.rfl) (def_and M
    (def_all M (def_imp M (def_mem M 0 (yi + 1)) (def_SubS M 0 (ii + 1))))
    (def_and M (def_all M (def_imp M (def_mem M 0 (fi + 1)) (def_mem M 0 (yi + 1))))
    (def_and M (def_all M (def_all M (def_all M (def_imp M (def_mem M 2 (yi + 3)) (def_imp M (def_mem M 1 (yi + 3))
        (def_imp M (def_all M (def_iff M (def_mem M 0 1) (def_and M (def_mem M 0 3) (def_mem M 0 2))))
          (def_mem M 0 (yi + 3))))))))
    (def_and M (def_all M (def_all M (def_imp M (def_mem M 1 (yi + 2)) (def_imp M (def_SubS M 0 (ii + 2))
        (def_imp M (def_SubS M 1 0) (def_mem M 0 (yi + 2)))))))
      (def_all M (def_imp M (def_mem M 0 (yi + 1)) (def_ex M (def_mem M 0 1))))))))

/-- a directed family of nonempty subsets of I -/
def Base (I F0 : M.D) : Prop :=
  (∀ A, In M A F0 → SubS M A I) ∧ (∃ A, In M A F0) ∧
  (∀ A B, In M A F0 → In M B F0 → ∃ C, In M C F0 ∧ SubS M C A ∧ SubS M C B) ∧
  (∀ A, In M A F0 → ∃ t, In M t A)

section St

variable (ρ : Nat → M.D)

theorem chainU (K : CP.Classical') {γ β z y : M.D} (hγβ : In M γ β) (hz : ValP (stepU M) ρ γ z) (hy : ValP (stepU M) ρ β y) :
    ∀ A, In M A z → SubS M A (ρ 0) → In M A y :=
  match valP_step (stepU M) ρ K hy with
  | ⟨_, hg, ag⟩ => fun A hA hs => (hg A).2 ⟨hs, A, .inr ⟨γ, z, hγβ, (ag γ hγβ z).2 hz, hA⟩, fun _ h _ => h⟩

theorem mt_resp {F0 g b S S' : M.D} (e : Ceq M.R S S') (h : Mt M F0 g b S) : Mt M F0 g b S' :=
  fun A hA => match h A hA with
  | ⟨t, h1, h2⟩ => ⟨t, h1, (e t).1 h2⟩

theorem inter_ex (A B : M.D) : ∃ C, ∀ t, In M t C ↔ In M t A ∧ In M t B :=
  match sep M (def_mem M 0 1) (fun _ => B) A with
  | ⟨C, hC⟩ => ⟨C, hC⟩

theorem step_pf (K : CP.Classical') (hB : Base M (ρ 0) (ρ 1)) (he : ∀ β S S', InG M (ρ 2) β S → InG M (ρ 2) β S' → Ceq M.R S S')
    {b y : M.D} (hb : Ord M b) (hy : ValP (stepU M) ρ b y)
    (IH : ∀ γ z, In M γ b → ValP (stepU M) ρ γ z → PF M (ρ 0) (ρ 1) z) : PF M (ρ 0) (ρ 1) y :=
  match valP_step (stepU M) ρ K hy with
  | ⟨g, hg, ag⟩ =>
    have hg' : ∀ B, In M B y ↔ RU M (ρ 0) (ρ 1) (ρ 2) g b B := hg
    have cases : ∀ A, InH M (ρ 1) g b A → In M A (ρ 1) ∨ ∃ γ z, In M γ b ∧ ValP (stepU M) ρ γ z ∧ In M A z :=
      fun _ h => h.elim .inl (fun ⟨γ, z, hγ, hz, hA⟩ => .inr ⟨γ, z, hγ, (ag γ hγ z).1 hz, hA⟩)
    have stage : ∀ γ z A, In M γ b → ValP (stepU M) ρ γ z → In M A z → InH M (ρ 1) g b A :=
      fun γ z _ hγ hz hA => .inr ⟨γ, z, hγ, (ag γ hγ z).2 hz, hA⟩
    have h1 : ∀ A, InH M (ρ 1) g b A → SubS M A (ρ 0) ∧ ∃ t, In M t A := fun A h =>
      (cases A h).elim (fun hA => ⟨hB.1 A hA, hB.2.2.2 A hA⟩)
        (fun ⟨γ, z, hγ, hz, hA⟩ => ⟨(IH γ z hγ hz).1 A hA, (IH γ z hγ hz).2.2.2.2 A hA⟩)
    have interz : ∀ γ z A B, In M γ b → ValP (stepU M) ρ γ z → In M A z → In M B z →
        ∃ C, InH M (ρ 1) g b C ∧ SubS M C A ∧ SubS M C B := fun γ z A B hγ hz hA hB' =>
      match inter_ex M A B with
      | ⟨C, hC⟩ => ⟨C, stage γ z C hγ hz ((IH γ z hγ hz).2.2.1 A B C hA hB' hC),
          fun t ht => ((hC t).1 ht).1, fun t ht => ((hC t).1 ht).2⟩
    have common : ∀ γ z γ' z' A B, In M γ b → ValP (stepU M) ρ γ z → In M A z →
        In M γ' b → ValP (stepU M) ρ γ' z' → In M B z' →
        ∃ C, InH M (ρ 1) g b C ∧ SubS M C A ∧ SubS M C B := fun γ z γ' z' A B hγ hz hA hγ' hz' hB' =>
      (ord_cmp M K (ord_mem M hb hγ) (ord_mem M hb hγ')).elim
        (fun hm => interz γ' z' A B hγ' hz' (chainU M ρ K hm hz hz' A hA ((IH γ z hγ hz).1 A hA)) hB')
        (fun h => h.elim
          (fun e => interz γ' z' A B hγ' hz'
            (mem_ceq_r M (valP_uniq (stepU M) ρ K (valP_resp (stepU M) ρ e (ceq_refl M.R z) hz) hz') hA) hB')
          (fun hm => interz γ z A B hγ hz hA (chainU M ρ K hm hz' hz B hB' ((IH γ' z' hγ' hz').1 B hB'))))
    have directed : ∀ A B, InH M (ρ 1) g b A → InH M (ρ 1) g b B →
        ∃ C, InH M (ρ 1) g b C ∧ SubS M C A ∧ SubS M C B := fun A B hA hB' =>
      match cases A hA, cases B hB' with
      | .inl a, .inl b' => match hB.2.2.1 A B a b' with
        | ⟨C, hC, s1, s2⟩ => ⟨C, .inl hC, s1, s2⟩
      | .inl a, .inr ⟨γ, z, hγ, hz, hBz⟩ => interz γ z A B hγ hz ((IH γ z hγ hz).2.1 A a) hBz
      | .inr ⟨γ, z, hγ, hz, hAz⟩, .inl b' => interz γ z A B hγ hz hAz ((IH γ z hγ hz).2.1 B b')
      | .inr ⟨γ, z, hγ, hz, hAz⟩, .inr ⟨γ', z', hγ', hz', hBz⟩ => common γ z γ' z' A B hγ hz hAz hγ' hz' hBz
    have decpt : ∀ A, InH M (ρ 1) g b A → ∃ t, In M t A ∧ Dec M (ρ 1) g b (ρ 2) t := fun A hA =>
      (K.lem (∃ S, InG M (ρ 2) b S)).elim
        (fun ⟨S0, hS0⟩ => (K.lem (Mt M (ρ 1) g b S0)).elim
          (fun hm => match hm A hA with
            | ⟨t, htA, htS⟩ => ⟨t, htA, fun S hS =>
              have eS := he b S0 S hS0 hS
              ⟨fun _ => (eS t).1 htS, fun nm => (nm (mt_resp M eS hm)).elim⟩⟩)
          (fun hnm =>
            have ex1 : ∃ A1, InH M (ρ 1) g b A1 ∧ ¬ ∃ t, In M t A1 ∧ In M t S0 :=
              CP.dne K _ (fun hn => hnm (fun A1 hA1 => CP.dne K _ (fun hn2 => hn ⟨A1, hA1, hn2⟩)))
            match ex1 with
            | ⟨A1, hA1, hd⟩ => match directed A A1 hA hA1 with
              | ⟨A3, hA3, s3a, s31⟩ => match (h1 A3 hA3).2 with
                | ⟨t, ht⟩ => ⟨t, s3a t ht, fun S hS =>
                  have eS := he b S0 S hS0 hS
                  ⟨fun m => (hnm (mt_resp M (ceq_symm M.R eS) m)).elim,
                   fun _ hS' => hd ⟨t, s31 t ht, (eS t).2 hS'⟩⟩⟩))
        (fun hn => match (h1 A hA).2 with
          | ⟨t, ht⟩ => ⟨t, ht, fun S hS => (hn ⟨S, hS⟩).elim⟩)
    ⟨fun B hB' => ((hg' B).1 hB').1,
     fun A hA => (hg' A).2 ⟨hB.1 A hA, A, .inl hA, fun _ h _ => h⟩,
     fun B1 B2 C h1' h2' hC => match (hg' B1).1 h1', (hg' B2).1 h2' with
      | ⟨s1, A1, hA1, f1⟩, ⟨_, A2, hA2, f2⟩ => match directed A1 A2 hA1 hA2 with
        | ⟨A3, hA3, sa, sb⟩ => (hg' C).2 ⟨fun t ht => s1 t ((hC t).1 ht).1, A3, hA3,
            fun t ht hd => (hC t).2 ⟨f1 t (sa t ht) hd, f2 t (sb t ht) hd⟩⟩,
     fun A B hA hs hsub => match (hg' A).1 hA with
      | ⟨_, A1, hA1, f⟩ => (hg' B).2 ⟨hs, A1, hA1, fun t ht hd => hsub t (f t ht hd)⟩,
     fun B hB' => match (hg' B).1 hB' with
      | ⟨_, A, hA, f⟩ => match decpt A hA with
        | ⟨t, ht, hd⟩ => ⟨t, f t ht hd⟩⟩

def BadU (u : Nat → M.D) : Prop :=
  ∃ y, ValP (stepU M) (fun n => u (n + 1)) (u 0) y ∧ ¬ PF M (u 1) (u 2) y

theorem stage_pf (K : CP.Classical') (hB : Base M (ρ 0) (ρ 1)) (he : ∀ β S S', InG M (ρ 2) β S → InG M (ρ 2) β S' → Ceq M.R S S')
    {β y : M.D} (hβ : Ord M β) (hy : ValP (stepU M) ρ β y) : PF M (ρ 0) (ρ 1) y :=
  have hBad : Def M (BadU M) := def_congr M (fun _ => Iff.rfl)
    (def_ex M (def_and M (def_ValP (stepU M) 1 0 2) (def_not M (def_PF M 2 3 0))))
  CP.dne K _ (fun hn => match ord_least M K hBad ρ ⟨β, hβ, y, hy, hn⟩ with
    | ⟨b, hb, ⟨_, hyb, hnb⟩, bmin⟩ => hnb (step_pf M ρ K hB he hb hyb (fun γ z hγ hz =>
        CP.dne K _ (fun hnz => (bmin γ (ord_mem M hb hγ) ⟨z, hz, hnz⟩).elim
          (fun hm => not_mem_self M b (hb.1 γ b hγ hm))
          (fun e => not_mem_self M γ (mem_ceq_r M e hγ))))))

end St

def vecIFe (I F0 e : M.D) : Nat → M.D
  | 0 => I
  | 1 => F0
  | _ + 2 => e

/-- the ultrafilter theorem in a model of ZFC -/
theorem ultra_ext (K : CP.Classical') (hac : ACsem M) {I F0 : M.D} (hB : Base M I F0) :
    ∃ U, PF M I F0 U ∧ ∀ S C, SubS M S I → (∀ t, In M t C ↔ In M t I ∧ ¬ In M t S) → In M S U ∨ In M C U :=
  match power_ex M I with
  | ⟨P, hP⟩ => match zermelo M K hac P with
    | ⟨_, e, hδ, _, _, efn, surj, _⟩ =>
      let ρ := vecIFe M I F0 e
      have he : ∀ β S S', InG M (ρ 2) β S → InG M (ρ 2) β S' → Ceq M.R S S' := fun β S S' => efn β S S'
      match valP_exists (stepU M) ρ K hδ with
      | ⟨U, hU⟩ => ⟨U, stage_pf M ρ K hB he hδ hU, fun S C hs hC =>
        match surj S ((hP S).2 hs) with
        | ⟨β, hβ, hβS⟩ => match valP_exists (stepU M) ρ K (ord_mem M hδ hβ) with
          | ⟨y, hy⟩ => match valP_step (stepU M) ρ K hy with
            | ⟨g, hg, _⟩ =>
              have hg' : ∀ B, In M B y ↔ RU M I F0 e g β B := hg
              match hB.2.1 with
              | ⟨A0, hA0⟩ => (K.lem (Mt M F0 g β S)).elim
                (fun m => .inl (chainU M ρ K hβ hy hU S
                  ((hg' S).2 ⟨hs, A0, .inl hA0, fun _ _ hd => (hd S hβS).1 m⟩) hs))
                (fun nm =>
                  have hCs : SubS M C I := fun t ht => ((hC t).1 ht).1
                  .inr (chainU M ρ K hβ hy hU C
                    ((hg' C).2 ⟨hCs, A0, .inl hA0, fun t ht hd => (hC t).2 ⟨hB.1 A0 hA0 t ht, (hd S hβS).2 nm⟩⟩) hCs))⟩

end IZ

end Found

#print axioms Found.IZ.def_RU
#print axioms Found.IZ.stepU
#print axioms Found.IZ.def_PF
#print axioms Found.IZ.chainU
#print axioms Found.IZ.step_pf
#print axioms Found.IZ.stage_pf
#print axioms Found.IZ.ultra_ext

found/IZ14.lean

lean_all/found/IZ14.lean

/-
  T165 (general), part G4. found/IZ14 — Kuratowski-finite sets inside a
  model of ZF (the formula FinF of found/Ist1):
  * FinD, the meta reading of FinF, is definable; Kuratowski induction for
    definable properties;
  * the empty set is finite, adjoining one element and binary unions keep
    finiteness;
  * if a set Y contains an empty set and is closed under adjoining one
    element, every finite subset of Y belongs to Y;
  * the fine base on I = {i ∈ Y | i finite}: the sets {i ∈ I | F ⊆ i} for
    F ∈ I form a directed family of nonempty subsets of I, and every
    ultrafilter through it contains {i ∈ I | y ∈ i} for y ∈ Y.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.IZ13
import found.Ist1

namespace Found

namespace IZ

open FO FOCT NBGc

variable (M : ZM)

/-- the meta reading of FinF -/
def FinD (z : M.D) : Prop :=
  ∀ S, (∀ u, In M u S → ∀ t, In M t u → In M t z) → (∃ e, In M e S ∧ ∀ s, In M s e → False) →
    (∀ u, In M u S → ∀ t, In M t z → ∃ u', In M u' S ∧
      ∀ s, (In M s u' → In M s u ∨ CeqC M.R s t) ∧ (In M s u ∨ CeqC M.R s t → In M s u')) →
    ∃ w, In M w S ∧ CeqC M.R w z

theorem sat_FinF (v : Nat → M.D) : Sat M.R IST.FinF v ↔ FinD M (v 0) := Iff.rfl

def σ0 (i : Nat) : Nat → Nat
  | 0 => i
  | n + 1 => n + 1

theorem def_FinD (i : Nat) : Def M (fun v => FinD M (v i)) :=
  def_congr M (fun _ => Iff.rfl) (def_ren M (σ0 i) ⟨IST.FinF, IST.l0_FinF, sat_FinF M⟩)

theorem adj_exists (u t : M.D) : ∃ c, ∀ s, In M s c ↔ (In M s u ∨ Ceq M.R s t) :=
  match sing_exists M t with
  | ⟨st, hst⟩ => match union2_exists M u st with
    | ⟨c, hc⟩ => ⟨c, fun s => (hc s).trans ⟨fun h => h.elim .inl (fun h' => .inr ((hst s).1 h')),
        fun h => h.elim .inl (fun e => .inr ((hst s).2 e))⟩⟩

theorem empty_exists : ∃ e, ∀ s, ¬ In M s e :=
  match sep M (def_false M) (fun _ => M.d0) M.d0 with
  | ⟨e, he⟩ => ⟨e, fun s hs => ((he s).1 hs).2⟩

theorem empty_ceq {e e' : M.D} (h : ∀ s, ¬ In M s e) (h' : ∀ s, ¬ In M s e') : Ceq M.R e e' :=
  fun s => ⟨fun m => (h s m).elim, fun m => (h' s m).elim⟩

/-- Kuratowski induction -/
theorem kur_ind {P : (Nat → M.D) → Prop} (hP : Def M P) (v : Nat → M.D) {z : M.D} (hz : FinD M z)
    (h0 : ∀ e, (∀ s, ¬ In M s e) → P (ext v e))
    (hs : ∀ u t u', SubS M u z → In M t z → P (ext v u) → (∀ s, In M s u' ↔ In M s u ∨ Ceq M.R s t) →
      P (ext v u')) : P (ext v z) :=
  match power_ex M z with
  | ⟨Pz, hPz⟩ => match sep M hP v Pz with
    | ⟨S, hS⟩ =>
      match hz S (fun u hu => (hPz u).1 ((hS u).1 hu).1)
          (match sep M (def_false M) (fun _ => M.d0) z with
            | ⟨e, he⟩ => ⟨e, (hS e).2 ⟨(hPz e).2 (fun s hs => ((he s).1 hs).1),
                h0 e (fun s hs => ((he s).1 hs).2)⟩, fun s hs => ((he s).1 hs).2⟩)
          (fun u hu t ht => match adj_exists M u t with
            | ⟨u', hu'⟩ =>
              have hsub : SubS M u z := (hPz u).1 ((hS u).1 hu).1
              ⟨u', (hS u').2 ⟨(hPz u').2 (fun s hs => ((hu' s).1 hs).elim (hsub s)
                  (fun e => mem_ceq_l M (ceq_symm M.R e) ht)),
                hs u t u' hsub ht ((hS u).1 hu).2 hu'⟩,
               fun s => ⟨fun m => ((hu' s).1 m).elim .inl (fun e => .inr (c1 M.R e)),
                 fun m => (hu' s).2 (m.elim .inl (fun e => .inr (c2 M.R e)))⟩⟩) with
      | ⟨w, hw, e⟩ => def_resp M hP (v := ext v w) (w := ext v z) (ceq_ext0 M v (c2 M.R e)) ((hS w).1 hw).2

theorem fin_resp {z z' : M.D} (e : Ceq M.R z z') (h : FinD M z) : FinD M z' :=
  def_resp M (def_FinD M 0) (v := fun _ => z) (w := fun _ => z') (fun _ => e) h

theorem fin_empty {e : M.D} (he : ∀ s, ¬ In M s e) : FinD M e := fun _ _ ⟨e', h1, h2⟩ _ =>
  ⟨e', h1, c1 M.R (empty_ceq M h2 he)⟩

theorem fin_adj {u t u' : M.D} (hu : FinD M u) (hu' : ∀ s, In M s u' ↔ In M s u ∨ Ceq M.R s t) : FinD M u' :=
  fun S _ h2 h3 =>
  have step : ∀ w x, In M w S → In M x u' → ∃ w', In M w' S ∧ ∀ s, In M s w' ↔ In M s w ∨ Ceq M.R s x :=
    fun w x hw hx => match h3 w hw x hx with
    | ⟨w', hw', sp⟩ => ⟨w', hw', fun s => ⟨fun m => ((sp s).1 m).elim .inl (fun e => .inr (c2 M.R e)),
        fun m => (sp s).2 (m.elim .inl (fun e => .inr (c1 M.R e)))⟩⟩
  have IH : ∃ w, In M w S ∧ Ceq M.R w u :=
    kur_ind M (P := fun v => ∃ w, In M w (v 1) ∧ Ceq M.R w (v 0))
      (def_ex M (def_and M (def_mem M 0 2) (def_ceq M 0 1))) (fun _ => S) hu
      (fun _ he => match h2 with
        | ⟨e', h1', h2'⟩ => ⟨e', h1', empty_ceq M h2' he⟩)
      (fun _ x _ _ hx ⟨w, hw, ew⟩ hv' => match step w x hw ((hu' x).2 (.inl hx)) with
        | ⟨w', hw', sp⟩ => ⟨w', hw', fun s => (sp s).trans ((or_congr_left' (ew s)).trans (hv' s).symm)⟩)
  match IH with
  | ⟨w, hw, ew⟩ => match step w t hw ((hu' t).2 (.inr (ceq_refl M.R t))) with
    | ⟨w', hw', sp⟩ => ⟨w', hw', c1 M.R (fun s => (sp s).trans ((or_congr_left' (ew s)).trans (hu' s).symm))⟩
where
  or_congr_left' {a b c : Prop} (h : a ↔ b) : a ∨ c ↔ b ∨ c :=
    ⟨fun x => x.elim (fun y => .inl (h.1 y)) .inr, fun x => x.elim (fun y => .inl (h.2 y)) .inr⟩

theorem fin_union {a b c : M.D} (ha : FinD M a) (hb : FinD M b) (hc : ∀ s, In M s c ↔ In M s a ∨ In M s b) :
    FinD M c :=
  kur_ind M (P := fun u => ∀ c, (∀ s, In M s c ↔ In M s (u 1) ∨ In M s (u 0)) → FinD M c)
    (def_all M (def_imp M (def_all M (def_iff M (def_mem M 0 1) (def_or M (def_mem M 0 3) (def_mem M 0 2))))
      (def_FinD M 0))) (fun _ => a) hb
    (fun _ he _ hc => fin_resp M (fun s => ⟨fun m => (hc s).2 (.inl m),
        fun m => ((hc s).1 m).elim id (fun m' => (he s m').elim)⟩) ha)
    (fun u _ _ _ _ ih hu' _ hc => match union2_exists M a u with
      | ⟨c0, hc0⟩ => fin_adj M (ih c0 hc0) (fun s => (hc s).trans
          ⟨fun h => h.elim (fun m => .inl ((hc0 s).2 (.inl m)))
              (fun m => ((hu' s).1 m).elim (fun m' => .inl ((hc0 s).2 (.inr m'))) .inr),
           fun h => h.elim (fun m => ((hc0 s).1 m).elim .inl (fun m' => .inr ((hu' s).2 (.inl m'))))
              (fun e => .inr ((hu' s).2 (.inr e)))⟩))
    c hc

/-- finite subsets of a set closed under adjoining belong to it -/
theorem fin_in {Y : M.D} (hE : ∃ e, In M e Y ∧ ∀ s, ¬ In M s e)
    (hA : ∀ u t, In M u Y → In M t Y → ∃ c, In M c Y ∧ ∀ s, In M s c ↔ In M s u ∨ Ceq M.R s t)
    {z : M.D} (hz : FinD M z) (hs : SubS M z Y) : In M z Y :=
  kur_ind M (P := fun u => SubS M (u 0) (u 1) → In M (u 0) (u 1))
    (def_imp M (def_SubS M 0 1) (def_mem M 0 1)) (fun _ => Y) hz
    (fun _ he _ => match hE with
      | ⟨_, he', hne⟩ => mem_ceq_l M (empty_ceq M hne he) he')
    (fun u t _ _ ht ih hu' hsub' =>
      have hsu : SubS M u Y := fun s m => hsub' s ((hu' s).2 (.inl m))
      match hA u t (ih hsu) (hs t ht) with
      | ⟨_, hc, hcs⟩ => mem_ceq_l M (fun s => (hcs s).trans (hu' s).symm) hc) hs

/-! ## the fine base -/

section F

variable {Y I F0 : M.D}

theorem fine_base (hT : ∀ x y, In M y Y → In M x y → In M x Y) (hE : ∃ e, In M e Y ∧ ∀ s, ¬ In M s e)
    (hA : ∀ u t, In M u Y → In M t Y → ∃ c, In M c Y ∧ ∀ s, In M s c ↔ In M s u ∨ Ceq M.R s t)
    (hI : ∀ i, In M i I ↔ In M i Y ∧ FinD M i)
    (hF0 : ∀ A, In M A F0 ↔ SubS M A I ∧ ∃ F, In M F I ∧ ∀ i, In M i A ↔ In M i I ∧ SubS M F i) :
    Base M I F0 :=
  have xF : ∀ F, In M F I → ∃ A, In M A F0 ∧ ∀ i, In M i A ↔ In M i I ∧ SubS M F i := fun F hF =>
    match sep M (def_SubS M 1 0) (fun _ => F) I with
    | ⟨A, hA'⟩ => ⟨A, (hF0 A).2 ⟨fun i hi => ((hA' i).1 hi).1, F, hF, hA'⟩, hA'⟩
  ⟨fun A hA' => ((hF0 A).1 hA').1,
   match hE with
   | ⟨e, he, hne⟩ => match xF e ((hI e).2 ⟨he, fin_empty M hne⟩) with
     | ⟨A, hA', _⟩ => ⟨A, hA'⟩,
   fun A B hA' hB' => match (hF0 A).1 hA', (hF0 B).1 hB' with
    | ⟨_, F, hF, sA⟩, ⟨_, F', hF', sB⟩ => match union2_exists M F F' with
      | ⟨G, hG⟩ =>
        have hGI : In M G I := (hI G).2 ⟨fin_in M hE hA (fin_union M ((hI F).1 hF).2 ((hI F').1 hF').2 hG)
            (fun s m => ((hG s).1 m).elim (fun m' => hT s F ((hI F).1 hF).1 m')
              (fun m' => hT s F' ((hI F').1 hF').1 m')),
          fin_union M ((hI F).1 hF).2 ((hI F').1 hF').2 hG⟩
        match xF G hGI with
        | ⟨C, hC, sC⟩ => ⟨C, hC,
            fun i hi => (sA i).2 ⟨((sC i).1 hi).1, fun s m => ((sC i).1 hi).2 s ((hG s).2 (.inl m))⟩,
            fun i hi => (sB i).2 ⟨((sC i).1 hi).1, fun s m => ((sC i).1 hi).2 s ((hG s).2 (.inr m))⟩⟩,
   fun A hA' => match (hF0 A).1 hA' with
    | ⟨_, F, hF, sA⟩ => ⟨F, (sA F).2 ⟨hF, fun _ m => m⟩⟩⟩

/-- an ultrafilter through the fine base is fine -/
theorem fine_mem (hE : ∃ e, In M e Y ∧ ∀ s, ¬ In M s e)
    (hA : ∀ u t, In M u Y → In M t Y → ∃ c, In M c Y ∧ ∀ s, In M s c ↔ In M s u ∨ Ceq M.R s t)
    (hI : ∀ i, In M i I ↔ In M i Y ∧ FinD M i)
    (hF0 : ∀ A, In M A F0 ↔ SubS M A I ∧ ∃ F, In M F I ∧ ∀ i, In M i A ↔ In M i I ∧ SubS M F i)
    {U : M.D} (hU : PF M I F0 U) {y A : M.D} (hy : In M y Y) (hA' : ∀ i, In M i A ↔ In M i I ∧ In M y i) :
    In M A U :=
  match hE with
  | ⟨e, he, hne⟩ => match hA e y he hy with
    | ⟨sy, hsy, sp⟩ =>
      have hsyI : In M sy I := (hI sy).2 ⟨hsy, fin_adj M (fin_empty M hne) sp⟩
      match sep M (def_SubS M 1 0) (fun _ => sy) I with
      | ⟨X, hX⟩ =>
        have hXF : In M X F0 := (hF0 X).2 ⟨fun i hi => ((hX i).1 hi).1, sy, hsyI, hX⟩
        hU.2.2.2.1 X A (hU.2.1 X hXF) (fun i hi => ((hA' i).1 hi).1)
          (fun i hi => (hA' i).2 ⟨((hX i).1 hi).1, ((hX i).1 hi).2 y ((sp y).2 (.inr (ceq_refl M.R y)))⟩)

end F

end IZ

end Found

#print axioms Found.IZ.def_FinD
#print axioms Found.IZ.kur_ind
#print axioms Found.IZ.fin_adj
#print axioms Found.IZ.fin_union
#print axioms Found.IZ.fin_in
#print axioms Found.IZ.fine_base
#print axioms Found.IZ.fine_mem

found/IZ15.lean

lean_all/found/IZ15.lean

/-
  T165 (general), part G5a. found/IZ15 — definable interpretations inside a
  model of ZF (shared tool): a structure on the model's sets given by
  definable predicates E₀(x, y), E₁(x) and a definable domain C(x), all
  with parameters. Satisfaction of any formula (atoms 0 and 1) in it,
  with quantifiers bounded to C, is definable (with the parameters taken
  from any offset of the valuation); the structure on the subtype of C
  satisfies the same formulas.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.IZ14
import found.IZ10

namespace Found

namespace IZ

open FO FOCT NBGc

section G

variable {D : Type}

/-- the relation of an interpretation -/
def RI (E0 : D → D → Prop) (E1 : D → Prop) : Nat → List D → Prop
  | 0, [] => False
  | 0, [_] => False
  | 0, [a, b] => E0 a b
  | 0, _ :: _ :: _ :: _ => False
  | 1, [] => False
  | 1, [a] => E1 a
  | 1, _ :: _ :: _ => False
  | _ + 2, _ => False

theorem satG_ext (R : Nat → List D → Prop) (C : D → Prop) : (φ : Fm) → {v w : Nat → D} → (∀ n, v n = w n) →
    (SatG R C φ v ↔ SatG R C φ w)
  | .atom r l, _, _, h => by
    show R r (mapD _ l) ↔ R r (mapD _ l)
    rw [mapD_ext h l]
  | .bot, _, _, _ => Iff.rfl
  | .conj a b, _, _, h => ⟨fun ⟨x, y⟩ => ⟨(satG_ext R C a h).1 x, (satG_ext R C b h).1 y⟩,
      fun ⟨x, y⟩ => ⟨(satG_ext R C a h).2 x, (satG_ext R C b h).2 y⟩⟩
  | .disj a b, _, _, h => ⟨fun x => x.elim (fun x => Or.inl ((satG_ext R C a h).1 x)) (fun y => Or.inr ((satG_ext R C b h).1 y)),
      fun x => x.elim (fun x => Or.inl ((satG_ext R C a h).2 x)) (fun y => Or.inr ((satG_ext R C b h).2 y))⟩
  | .imp a b, _, _, h => ⟨fun f x => (satG_ext R C b h).1 (f ((satG_ext R C a h).2 x)),
      fun f x => (satG_ext R C b h).2 (f ((satG_ext R C a h).1 x))⟩
  | .all a, _, _, h => ⟨fun f d hd => (satG_ext R C a (ext_ext h d)).1 (f d hd),
      fun f d hd => (satG_ext R C a (ext_ext h d)).2 (f d hd)⟩
  | .ex a, _, _, h => ⟨fun ⟨d, hd, x⟩ => ⟨d, hd, (satG_ext R C a (ext_ext h d)).1 x⟩,
      fun ⟨d, hd, x⟩ => ⟨d, hd, (satG_ext R C a (ext_ext h d)).2 x⟩⟩

/-! ## the structure on the subtype -/

def mapV {C : D → Prop} : List {x // C x} → List D
  | [] => []
  | a :: l => a.1 :: mapV l

theorem mapV_mapD {C : D → Prop} (w : Nat → {x // C x}) : (l : List Nat) →
    mapV (mapD w l) = mapD (fun n => (w n).1) l
  | [] => rfl
  | a :: l => by
    show (w a).1 :: mapV (mapD w l) = (w a).1 :: mapD (fun n => (w n).1) l
    rw [mapV_mapD w l]

def Rsub (R : Nat → List D → Prop) (C : D → Prop) : Nat → List {x // C x} → Prop := fun r l => R r (mapV l)

theorem sat_sub (R : Nat → List D → Prop) (C : D → Prop) : (φ : Fm) → (w : Nat → {x // C x}) →
    (Sat (Rsub R C) φ w ↔ SatG R C φ (fun n => (w n).1))
  | .atom r l, w => by
    show R r (mapV (mapD w l)) ↔ R r (mapD _ l)
    rw [mapV_mapD w l]
  | .bot, _ => Iff.rfl
  | .conj a b, w => ⟨fun ⟨x, y⟩ => ⟨(sat_sub R C a w).1 x, (sat_sub R C b w).1 y⟩,
      fun ⟨x, y⟩ => ⟨(sat_sub R C a w).2 x, (sat_sub R C b w).2 y⟩⟩
  | .disj a b, w => ⟨fun x => x.elim (fun x => Or.inl ((sat_sub R C a w).1 x)) (fun y => Or.inr ((sat_sub R C b w).1 y)),
      fun x => x.elim (fun x => Or.inl ((sat_sub R C a w).2 x)) (fun y => Or.inr ((sat_sub R C b w).2 y))⟩
  | .imp a b, w => ⟨fun f x => (sat_sub R C b w).1 (f ((sat_sub R C a w).2 x)),
      fun f x => (sat_sub R C b w).2 (f ((sat_sub R C a w).1 x))⟩
  | .all a, w =>
    have e : ∀ d : {x // C x}, ∀ n, (ext w d n).1 = ext (fun n => (w n).1) d.1 n := fun _ n => match n with
      | 0 => rfl
      | _ + 1 => rfl
    ⟨fun f x hx => (satG_ext R C a (e ⟨x, hx⟩)).1 ((sat_sub R C a (ext w ⟨x, hx⟩)).1 (f ⟨x, hx⟩)),
     fun f d => (sat_sub R C a (ext w d)).2 ((satG_ext R C a (e d)).2 (f d.1 d.2))⟩
  | .ex a, w =>
    have e : ∀ d : {x // C x}, ∀ n, (ext w d n).1 = ext (fun n => (w n).1) d.1 n := fun _ n => match n with
      | 0 => rfl
      | _ + 1 => rfl
    ⟨fun ⟨d, x⟩ => ⟨d.1, d.2, (satG_ext R C a (e d)).1 ((sat_sub R C a (ext w d)).1 x)⟩,
     fun ⟨x, hx, s⟩ => ⟨⟨x, hx⟩, (sat_sub R C a (ext w ⟨x, hx⟩)).2 ((satG_ext R C a (e ⟨x, hx⟩)).2 s)⟩⟩

end G

/-! ## definability -/

variable (M : ZM)

def upS (σ : Nat → Nat) : Nat → Nat
  | 0 => 0
  | n + 1 => σ n + 1

section I

variable (E0 : (Nat → M.D) → M.D → M.D → Prop) (E1 : (Nat → M.D) → M.D → Prop) (C : (Nat → M.D) → M.D → Prop)
  (h0 : ∀ a b k, Def M (fun u => E0 (fun n => u (n + k)) (u a) (u b)))
  (h1 : ∀ a k, Def M (fun u => E1 (fun n => u (n + k)) (u a)))
  (hC : ∀ a k, Def M (fun u => C (fun n => u (n + k)) (u a)))

include h0 h1 hC in
/-- satisfaction in a definable interpretation is definable -/
theorem def_interp : (φ : Fm) → (σ : Nat → Nat) → (k : Nat) →
    Def M (fun u => SatG (RI (E0 (fun n => u (n + k))) (E1 (fun n => u (n + k)))) (C (fun n => u (n + k))) φ
      (fun n => u (σ n)))
  | .atom 0 [], _, _ => def_congr M (fun _ => Iff.rfl) (def_false M)
  | .atom 0 [_], _, _ => def_congr M (fun _ => Iff.rfl) (def_false M)
  | .atom 0 [a, b], σ, k => def_congr M (fun _ => Iff.rfl) (h0 (σ a) (σ b) k)
  | .atom 0 (_ :: _ :: _ :: _), _, _ => def_congr M (fun _ => Iff.rfl) (def_false M)
  | .atom 1 [], _, _ => def_congr M (fun _ => Iff.rfl) (def_false M)
  | .atom 1 [a], σ, k => def_congr M (fun _ => Iff.rfl) (h1 (σ a) k)
  | .atom 1 (_ :: _ :: _), _, _ => def_congr M (fun _ => Iff.rfl) (def_false M)
  | .atom (_ + 2) _, _, _ => def_congr M (fun _ => Iff.rfl) (def_false M)
  | .bot, _, _ => def_congr M (fun _ => Iff.rfl) (def_false M)
  | .conj a b, σ, k => def_and M (def_interp a σ k) (def_interp b σ k)
  | .disj a b, σ, k => def_or M (def_interp a σ k) (def_interp b σ k)
  | .imp a b, σ, k => def_imp M (def_interp a σ k) (def_interp b σ k)
  | .all a, σ, k => def_congr M (P := fun u => ∀ d, C (fun n => u (n + k)) d →
        SatG (RI (E0 (fun n => u (n + k))) (E1 (fun n => u (n + k)))) (C (fun n => u (n + k))) a
          (fun n => ext u d (upS σ n))) (fun u => ⟨fun f d hd => (satG_ext _ _ a (v := fun n => ext u d (upS σ n)) (w := ext (fun n => u (σ n)) d) (fun n => match n with
        | 0 => rfl
        | _ + 1 => rfl)).1 (f d hd),
      fun f d hd => (satG_ext _ _ a (v := fun n => ext u d (upS σ n)) (w := ext (fun n => u (σ n)) d) (fun n => match n with
        | 0 => rfl
        | _ + 1 => rfl)).2 (f d hd)⟩)
      (def_all M (def_imp M (hC 0 (k + 1)) (def_interp a (upS σ) (k + 1))))
  | .ex a, σ, k => def_congr M (P := fun u => ∃ d, C (fun n => u (n + k)) d ∧
        SatG (RI (E0 (fun n => u (n + k))) (E1 (fun n => u (n + k)))) (C (fun n => u (n + k))) a
          (fun n => ext u d (upS σ n))) (fun u => ⟨fun ⟨d, hd, f⟩ => ⟨d, hd, (satG_ext _ _ a (v := fun n => ext u d (upS σ n)) (w := ext (fun n => u (σ n)) d) (fun n => match n with
        | 0 => rfl
        | _ + 1 => rfl)).1 f⟩,
      fun ⟨d, hd, f⟩ => ⟨d, hd, (satG_ext _ _ a (v := fun n => ext u d (upS σ n)) (w := ext (fun n => u (σ n)) d) (fun n => match n with
        | 0 => rfl
        | _ + 1 => rfl)).2 f⟩⟩)
      (def_ex M (def_and M (hC 0 (k + 1)) (def_interp a (upS σ) (k + 1))))

end I

end IZ

end Found

#print axioms Found.IZ.satG_ext
#print axioms Found.IZ.sat_sub
#print axioms Found.IZ.def_interp

found/IZ16.lean

lean_all/found/IZ16.lean

/-
  T165 (general), part G5b. found/IZ16 — set-functions from I to Y inside a
  model of ZFC:
  * FnY (total on I, values in Y, single-valued), definable; constants;
  * choice of a function through a definable relation (from the choice
    functions of found/IZ11);
  * the pointwise structure at i ∈ I (f ∈ᵢ g iff f(i) ∈ g(i), quantifiers
    over FnY) satisfies an L0 formula iff Y does at the values at i.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.IZ15

namespace Found

namespace IZ

open FO FOCT NBGc

variable (M : ZM)

def FnY (I Y f : M.D) : Prop :=
  (∀ i, In M i I → ∃ a, In M a Y ∧ InG M f i a) ∧ (∀ i a a', In M i I → InG M f i a → InG M f i a' → Ceq M.R a a')

theorem def_FnY (ii yi fi : Nat) : Def M (fun v => FnY M (v ii) (v yi) (v fi)) :=
  def_congr M (fun _ => Iff.rfl) (def_and M
    (def_all M (def_imp M (def_mem M 0 (ii + 1)) (def_ex M (def_and M (def_mem M 0 (yi + 2)) (def_InG M (fi + 2) 1 0)))))
    (def_all M (def_all M (def_all M (def_imp M (def_mem M 2 (ii + 3)) (def_imp M (def_InG M (fi + 3) 2 1)
      (def_imp M (def_InG M (fi + 3) 2 0) (def_ceq M 1 0))))))))

theorem const_ex (I Y : M.D) {a : M.D} (ha : In M a Y) : ∃ f, FnY M I Y f ∧ ∀ i, In M i I → InG M f i a :=
  match repl M (Θ := fun u => OPs M.R (u 1) (u 0) (u 2)) (def_op M 1 0 2) (fun _ => a) I
      (fun _ _ _ _ h1 h2 => op_uniq M h1 h2) with
  | ⟨f, hf⟩ =>
    have val : ∀ i b, InG M f i b → Ceq M.R b a := fun _ _ ⟨p, hp, ho⟩ => match (hf p).1 hp with
      | ⟨_, _, ho'⟩ => (op_inj' M ho ho').2
    have tot : ∀ i, In M i I → InG M f i a := fun i hi => match op_exists M i a with
      | ⟨p, hp⟩ => ⟨p, (hf p).2 ⟨i, hi, hp⟩, hp⟩
    ⟨f, ⟨fun i hi => ⟨a, ha, tot i hi⟩,
      fun i b b' _ h1 h2 => ceq_trans M.R (val i b h1) (ceq_symm M.R (val i b' h2))⟩, tot⟩

/-! ## choosing a function through a definable relation -/

section C

variable (P : (Nat → M.D) → M.D → M.D → Prop) (hP : ∀ a b k, Def M (fun u => P (fun n => u (n + k)) (u a) (u b)))

def vecIA (i a : M.D) (τ : Nat → M.D) : Nat → M.D
  | 0 => i
  | 1 => a
  | n + 2 => τ n

include hP in
theorem P_resp {τ : Nat → M.D} {i a i' a' : M.D} (ei : Ceq M.R i i') (ea : Ceq M.R a a') (h : P τ i a) : P τ i' a' :=
  def_resp M (hP 0 1 2) (v := vecIA M i a τ) (w := vecIA M i' a' τ) (fun n => match n with
    | 0 => ei
    | 1 => ea
    | _ + 2 => ceq_refl M.R _) h

def vecYt (Y : M.D) (τ : Nat → M.D) : Nat → M.D
  | 0 => Y
  | n + 1 => τ n

def vecCYt (c Y : M.D) (τ : Nat → M.D) : Nat → M.D
  | 0 => c
  | 1 => Y
  | n + 2 => τ n

include hP in
theorem choice_fam (K : CP.Classical') (hac : ACsem M) (τ : Nat → M.D) (I Y : M.D)
    (h : ∀ i, In M i I → ∃ a, In M a Y ∧ P τ i a) :
    ∃ f, FnY M I Y f ∧ ∀ i a, In M i I → InG M f i a → P τ i a := by
  let Θ : (Nat → M.D) → Prop := fun u => ∀ x, In M x (u 1) ↔ In M x (u 2) ∧ P (fun n => u (n + 3)) (u 0) x
  have hΘ : Def M Θ := def_congr M (fun _ => Iff.rfl)
    (def_all M (def_iff M (def_mem M 0 2) (def_and M (def_mem M 0 3) (hP 1 0 4))))
  have wex : ∀ i, ∃ W, ∀ x, In M x W ↔ In M x Y ∧ P τ i x := fun i =>
    match sep M (hP 1 0 2) (ext τ i) Y with
    | ⟨W, hW⟩ => ⟨W, hW⟩
  match repl M hΘ (vecYt M Y τ) I (fun _ _ z z' h1 h2 x => (h1 x).trans (h2 x).symm) with
  | ⟨F, hF⟩ =>
  match choice_fn M K hac F (fun W hW => match (hF W).1 hW with
      | ⟨i, hi, hsp⟩ => match h i hi with
        | ⟨a, ha, hpa⟩ => ⟨a, (hsp a).2 ⟨ha, hpa⟩⟩) with
  | ⟨c, hc1, hc2⟩ =>
  let Θ2 : (Nat → M.D) → Prop := fun u => ∃ W a, (∀ x, In M x W ↔ In M x (u 3) ∧ P (fun n => u (n + 4)) (u 0) x) ∧
    InG M (u 2) W a ∧ OPs M.R (u 1) (u 0) a
  have hΘ2 : Def M Θ2 := def_congr M (fun _ => Iff.rfl) (def_ex M (def_ex M (def_and M
    (def_all M (def_iff M (def_mem M 0 2) (def_and M (def_mem M 0 6) (hP 3 0 7))))
    (def_and M (def_InG M 4 1 0) (def_op M 3 2 0)))))
  have inF : ∀ i W, In M i I → (∀ x, In M x W ↔ In M x Y ∧ P τ i x) → In M W F :=
    fun i W hi hW => (hF W).2 ⟨i, hi, hW⟩
  have cval : ∀ i W W' a a', In M i I → (∀ x, In M x W ↔ In M x Y ∧ P τ i x) →
      (∀ x, In M x W' ↔ In M x Y ∧ P τ i x) → InG M c W a → InG M c W' a' → Ceq M.R a a' :=
    fun i W W' a a' hi hW hW' h1 h2 =>
      have e : Ceq M.R W' W := fun x => (hW' x).trans (hW x).symm
      hc2 W a a' (inF i W hi hW) h1 (InG_resp M (ceq_refl M.R c) e (ceq_refl M.R a') h2)
  match repl M hΘ2 (vecCYt M c Y τ) I (fun i hi p p' ⟨W, a, hW, hca, ho⟩ ⟨W', a', hW', hca', ho'⟩ =>
      op_uniq M ho (op_resp M ho' (ceq_refl M.R p') (ceq_refl M.R i) (ceq_symm M.R (cval i W W' a a' hi hW hW' hca hca')))) with
  | ⟨f, hf⟩ =>
  have inv : ∀ i a, In M i I → InG M f i a → ∃ W a', (∀ x, In M x W ↔ In M x Y ∧ P τ i x) ∧ InG M c W a' ∧ Ceq M.R a' a :=
    fun i a _ ⟨p, hp, ho⟩ => match (hf p).1 hp with
    | ⟨i', _, W, a', hW, hca, ho'⟩ =>
      have q := op_inj' M ho' ho
      ⟨W, a', fun x => (hW x).trans ⟨fun ⟨h1, h2⟩ => ⟨h1, P_resp M P hP q.1 (ceq_refl M.R x) h2⟩,
        fun ⟨h1, h2⟩ => ⟨h1, P_resp M P hP (ceq_symm M.R q.1) (ceq_refl M.R x) h2⟩⟩, hca, q.2⟩
  have inW : ∀ i W a, In M i I → (∀ x, In M x W ↔ In M x Y ∧ P τ i x) → InG M c W a → In M a W :=
    fun i W a hi hW hca => match hc1 W (inF i W hi hW) with
    | ⟨t, ht, hct⟩ => mem_ceq_l M (hc2 W t a (inF i W hi hW) hct hca) ht
  exact ⟨f, ⟨fun i hi => match wex i with
      | ⟨W, hW⟩ => match hc1 W (inF i W hi hW) with
        | ⟨t, ht, hct⟩ => match op_exists M i t with
          | ⟨p, hp⟩ => ⟨t, ((hW t).1 ht).1, p, (hf p).2 ⟨i, hi, W, t, hW, hct, hp⟩, hp⟩,
     fun i a a' hi h1 h2 => match inv i a hi h1, inv i a' hi h2 with
      | ⟨W, b, hW, hcb, eb⟩, ⟨W', b', hW', hcb', eb'⟩ =>
        ceq_trans M.R (ceq_symm M.R eb) (ceq_trans M.R (cval i W W' b b' hi hW hW' hcb hcb') eb')⟩,
    fun i a hi h1 => match inv i a hi h1 with
    | ⟨W, b, hW, hcb, eb⟩ => P_resp M P hP (ceq_refl M.R i) eb ((hW b).1 (inW i W b hi hW hcb)).2⟩

end C

/-! ## the pointwise structure -/

/-- f(i) ∈ g(i) -/
def Ept (i f g : M.D) : Prop := ∀ a b, InG M f i a → InG M g i b → In M a b

theorem def_Ept (ii fi gi : Nat) : Def M (fun v => Ept M (v ii) (v fi) (v gi)) :=
  def_congr M (fun _ => Iff.rfl) (def_all M (def_all M (def_imp M (def_InG M (fi + 2) (ii + 2) 1)
    (def_imp M (def_InG M (gi + 2) (ii + 2) 0) (def_mem M 1 0)))))

/-- the pointwise structure at τ 0 = i, with τ 1 = I, τ 2 = Y -/
def RP (τ : Nat → M.D) : Nat → List M.D → Prop := RI (Ept M (τ 0)) (fun _ => False)

def CP' (τ : Nat → M.D) : M.D → Prop := FnY M (τ 1) (τ 2)

theorem def_PSG (φ : Fm) (σ : Nat → Nat) (k : Nat) :
    Def M (fun u => SatG (RP M (fun n => u (n + k))) (CP' M (fun n => u (n + k))) φ (fun n => u (σ n))) :=
  def_interp M (fun τ => Ept M (τ 0)) (fun _ _ => False) (fun τ => FnY M (τ 1) (τ 2))
    (fun a b k => def_Ept M (0 + k) a b) (fun _ _ => def_false M) (fun a k => def_FnY M (1 + k) (2 + k) a) φ σ k

def YC (Y : M.D) : M.D → Prop := fun x => In M x Y

/-- the pointwise structure at i satisfies an L0 formula iff Y does at the values -/
theorem pt_val {τ : Nat → M.D} (hi : In M (τ 0) (τ 1)) : (φ : Fm) → L0 φ → (fs : Nat → M.D) →
    (∀ n, FnY M (τ 1) (τ 2) (fs n)) → (v : Nat → M.D) → (∀ n, InG M (fs n) (τ 0) (v n)) →
    (SatG (RP M τ) (CP' M τ) φ fs ↔ SatG M.R (YC M (τ 2)) φ v)
  | .atom r l, hl, fs, hf, v, hv => by
    match r, l, hl with
    | 0, [a, b], _ =>
      exact ⟨fun h => h (v a) (v b) (hv a) (hv b),
        fun h a' b' ha hb => mem_ceq_r M ((hf b).2 _ _ _ hi (hv b) hb) (mem_ceq_l M ((hf a).2 _ _ _ hi (hv a) ha) h)⟩
  | .bot, _, _, _, _, _ => Iff.rfl
  | .conj a b, hl, fs, hf, v, hv => ⟨fun ⟨x, y⟩ => ⟨(pt_val hi a hl.1 fs hf v hv).1 x, (pt_val hi b hl.2 fs hf v hv).1 y⟩,
      fun ⟨x, y⟩ => ⟨(pt_val hi a hl.1 fs hf v hv).2 x, (pt_val hi b hl.2 fs hf v hv).2 y⟩⟩
  | .disj a b, hl, fs, hf, v, hv => ⟨fun x => x.elim (fun x => .inl ((pt_val hi a hl.1 fs hf v hv).1 x))
        (fun y => .inr ((pt_val hi b hl.2 fs hf v hv).1 y)),
      fun x => x.elim (fun x => .inl ((pt_val hi a hl.1 fs hf v hv).2 x)) (fun y => .inr ((pt_val hi b hl.2 fs hf v hv).2 y))⟩
  | .imp a b, hl, fs, hf, v, hv => ⟨fun f x => (pt_val hi b hl.2 fs hf v hv).1 (f ((pt_val hi a hl.1 fs hf v hv).2 x)),
      fun f x => (pt_val hi b hl.2 fs hf v hv).2 (f ((pt_val hi a hl.1 fs hf v hv).1 x))⟩
  | .all a, hl, fs, hf, v, hv =>
    ⟨fun f x hx => match const_ex M (τ 1) (τ 2) hx with
      | ⟨g, hg, hgi⟩ => (pt_val hi a hl (ext fs g) (fun n => match n with
          | 0 => hg
          | n + 1 => hf n) (ext v x) (fun n => match n with
          | 0 => hgi (τ 0) hi
          | n + 1 => hv n)).1 (f g hg),
     fun f g hg => match hg.1 (τ 0) hi with
      | ⟨x, hx, hgx⟩ => (pt_val hi a hl (ext fs g) (fun n => match n with
          | 0 => hg
          | n + 1 => hf n) (ext v x) (fun n => match n with
          | 0 => hgx
          | n + 1 => hv n)).2 (f x hx)⟩
  | .ex a, hl, fs, hf, v, hv =>
    ⟨fun ⟨g, hg, s⟩ => match hg.1 (τ 0) hi with
      | ⟨x, hx, hgx⟩ => ⟨x, hx, (pt_val hi a hl (ext fs g) (fun n => match n with
          | 0 => hg
          | n + 1 => hf n) (ext v x) (fun n => match n with
          | 0 => hgx
          | n + 1 => hv n)).1 s⟩,
     fun ⟨x, hx, s⟩ => match const_ex M (τ 1) (τ 2) hx with
      | ⟨g, hg, hgi⟩ => ⟨g, hg, (pt_val hi a hl (ext fs g) (fun n => match n with
          | 0 => hg
          | n + 1 => hf n) (ext v x) (fun n => match n with
          | 0 => hgi (τ 0) hi
          | n + 1 => hv n)).2 s⟩⟩

end IZ

end Found

#print axioms Found.IZ.def_FnY
#print axioms Found.IZ.const_ex
#print axioms Found.IZ.choice_fam
#print axioms Found.IZ.def_PSG
#print axioms Found.IZ.pt_val

found/IZ17.lean

lean_all/found/IZ17.lean

/-
  T165 (general), part G5c. found/IZ17 — the internal ultrapower and Łoś's
  theorem inside a model of ZFC. For an ultrafilter U on I (a set of the
  model) and a nonempty set Y, the structure on the functions I → Y with
  f ∈ g iff f(i) ∈ g(i) for U-almost all i satisfies an L0 formula iff the
  pointwise structures do for U-almost all i (found/IZ16 turns these into
  Y at the values). The quantifier steps choose a witness function with
  found/IZ16's choice_fam.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.IZ16

namespace Found

namespace IZ

open FO FOCT NBGc

variable (M : ZM)

/-- for U-almost all i -/
def AEU (U : M.D) (P : M.D → Prop) : Prop := ∃ A, In M A U ∧ ∀ i, In M i A → P i

/-- satisfaction in the pointwise structure at i -/
def PSi (I Y i : M.D) (φ : Fm) (fs : Nat → M.D) : Prop :=
  SatG (RI (Ept M i) (fun _ => False)) (FnY M I Y) φ fs

/-- the ultrapower membership and standardness -/
def EU (U f g : M.D) : Prop := AEU M U (fun i => Ept M i f g)

def STU (U Y f : M.D) : Prop := ∃ y, In M y Y ∧ AEU M U (fun i => ∀ a, InG M f i a → Ceq M.R a y)

def ρ3 (ii Ii Yi : Nat) (σ : Nat → Nat) : Nat → Nat
  | 0 => ii
  | 1 => Ii
  | 2 => Yi
  | n + 3 => σ n

theorem def_PSG' (φ : Fm) (ii Ii Yi : Nat) (σ : Nat → Nat) :
    Def M (fun u => PSi M (u Ii) (u Yi) (u ii) φ (fun n => u (σ n))) :=
  def_congr M (fun _ => Iff.rfl) (def_ren M (ρ3 ii Ii Yi σ) (def_PSG M φ (fun n => n + 3) 0))

def σx (k : Nat) : Nat → Nat
  | 0 => 0
  | m + 1 => m + 3 + k + 1

theorem def_PSx (φ : Fm) (ii Ii Yi k : Nat) :
    Def M (fun u => PSi M (u Ii) (u Yi) (u ii) φ (ext (fun n => u (n + 3 + k + 1)) (u 0))) :=
  def_congr M (fun u => satG_ext _ _ φ (v := fun n => u (σx k n)) (w := ext (fun n => u (n + 3 + k + 1)) (u 0))
      (fun n => match n with
        | 0 => rfl
        | _ + 1 => rfl))
    (def_PSG' M φ ii Ii Yi (σx k))

/-- pointwise satisfaction only depends on the values at i -/
theorem pt_local {I Y i : M.D} (hi : In M i I) : (φ : Fm) → L0 φ → (fs gs : Nat → M.D) →
    (∀ n, FnY M I Y (fs n)) → (∀ n, FnY M I Y (gs n)) →
    (∀ n x y, InG M (fs n) i x → InG M (gs n) i y → Ceq M.R x y) → (PSi M I Y i φ fs ↔ PSi M I Y i φ gs)
  | .atom r l, hl, fs, gs, hf, hg, ag => by
    match r, l, hl with
    | 0, [a, b], _ =>
      exact ⟨fun h a' b' ha hb => match (hf a).1 i hi, (hf b).1 i hi with
          | ⟨x, _, hx⟩, ⟨y, _, hy⟩ => mem_ceq_r M (ag b y b' hy hb) (mem_ceq_l M (ag a x a' hx ha) (h x y hx hy)),
        fun h a' b' ha hb => match (hg a).1 i hi, (hg b).1 i hi with
          | ⟨x, _, hx⟩, ⟨y, _, hy⟩ => mem_ceq_r M (ceq_symm M.R (ag b b' y hb hy))
              (mem_ceq_l M (ceq_symm M.R (ag a a' x ha hx)) (h x y hx hy))⟩
  | .bot, _, _, _, _, _, _ => Iff.rfl
  | .conj a b, hl, fs, gs, hf, hg, ag => ⟨fun ⟨x, y⟩ => ⟨(pt_local hi a hl.1 fs gs hf hg ag).1 x, (pt_local hi b hl.2 fs gs hf hg ag).1 y⟩,
      fun ⟨x, y⟩ => ⟨(pt_local hi a hl.1 fs gs hf hg ag).2 x, (pt_local hi b hl.2 fs gs hf hg ag).2 y⟩⟩
  | .disj a b, hl, fs, gs, hf, hg, ag => ⟨fun x => x.elim (fun x => .inl ((pt_local hi a hl.1 fs gs hf hg ag).1 x))
        (fun y => .inr ((pt_local hi b hl.2 fs gs hf hg ag).1 y)),
      fun x => x.elim (fun x => .inl ((pt_local hi a hl.1 fs gs hf hg ag).2 x))
        (fun y => .inr ((pt_local hi b hl.2 fs gs hf hg ag).2 y))⟩
  | .imp a b, hl, fs, gs, hf, hg, ag => ⟨fun f x => (pt_local hi b hl.2 fs gs hf hg ag).1 (f ((pt_local hi a hl.1 fs gs hf hg ag).2 x)),
      fun f x => (pt_local hi b hl.2 fs gs hf hg ag).2 (f ((pt_local hi a hl.1 fs gs hf hg ag).1 x))⟩
  | .all a, hl, fs, gs, hf, hg, ag =>
    have h' : ∀ g, FnY M I Y g → (PSi M I Y i a (ext fs g) ↔ PSi M I Y i a (ext gs g)) := fun g hgg =>
      pt_local hi a hl (ext fs g) (ext gs g) (fun n => match n with
          | 0 => hgg
          | n + 1 => hf n) (fun n => match n with
          | 0 => hgg
          | n + 1 => hg n) (fun n => match n with
          | 0 => fun x y h1 h2 => hgg.2 i x y hi h1 h2
          | n + 1 => ag n)
    ⟨fun f g hgg => (h' g hgg).1 (f g hgg), fun f g hgg => (h' g hgg).2 (f g hgg)⟩
  | .ex a, hl, fs, gs, hf, hg, ag =>
    have h' : ∀ g, FnY M I Y g → (PSi M I Y i a (ext fs g) ↔ PSi M I Y i a (ext gs g)) := fun g hgg =>
      pt_local hi a hl (ext fs g) (ext gs g) (fun n => match n with
          | 0 => hgg
          | n + 1 => hf n) (fun n => match n with
          | 0 => hgg
          | n + 1 => hg n) (fun n => match n with
          | 0 => fun x y h1 h2 => hgg.2 i x y hi h1 h2
          | n + 1 => ag n)
    ⟨fun ⟨g, hgg, s⟩ => ⟨g, hgg, (h' g hgg).1 s⟩, fun ⟨g, hgg, s⟩ => ⟨g, hgg, (h' g hgg).2 s⟩⟩

/-! ## the witness relations -/

def Pex (φ : Fm) (τ : Nat → M.D) (i x : M.D) : Prop :=
  In M i (τ 0) → ∃ g, FnY M (τ 1) (τ 2) g ∧ InG M g i x ∧ PSi M (τ 1) (τ 2) i φ (ext (fun n => τ (n + 3)) g)

def Pall (φ : Fm) (τ : Nat → M.D) (i x : M.D) : Prop :=
  In M i (τ 0) → ∃ g, FnY M (τ 1) (τ 2) g ∧ InG M g i x ∧ ¬ PSi M (τ 1) (τ 2) i φ (ext (fun n => τ (n + 3)) g)

theorem def_Pex (φ : Fm) (a b k : Nat) : Def M (fun u => Pex M φ (fun n => u (n + k)) (u a) (u b)) :=
  def_congr M (fun _ => Iff.rfl) (def_imp M (def_mem M a (0 + k)) (def_ex M (def_and M
    (def_FnY M (1 + k + 1) (2 + k + 1) 0) (def_and M (def_InG M 0 (a + 1) (b + 1))
      (def_PSx M φ (a + 1) (1 + k + 1) (2 + k + 1) k)))))

theorem def_Pall (φ : Fm) (a b k : Nat) : Def M (fun u => Pall M φ (fun n => u (n + k)) (u a) (u b)) :=
  def_congr M (fun _ => Iff.rfl) (def_imp M (def_mem M a (0 + k)) (def_ex M (def_and M
    (def_FnY M (1 + k + 1) (2 + k + 1) 0) (def_and M (def_InG M 0 (a + 1) (b + 1))
      (def_not M (def_PSx M φ (a + 1) (1 + k + 1) (2 + k + 1) k))))))

def vecW (A I Y : M.D) (fs : Nat → M.D) : Nat → M.D
  | 0 => A
  | 1 => I
  | 2 => Y
  | n + 3 => fs n

def vecIY (I Y : M.D) (fs : Nat → M.D) : Nat → M.D
  | 0 => I
  | 1 => Y
  | n + 2 => fs n

/-! ## Łoś -/

section L

variable {I Y U F0 : M.D} (K : CP.Classical') (hac : ACsem M) (hU : PF M I F0 U)
  (hult : ∀ S C, SubS M S I → (∀ t, In M t C ↔ In M t I ∧ ¬ In M t S) → In M S U ∨ In M C U)
  (hY : ∃ y, In M y Y)

theorem xset (φ : Fm) (fs : Nat → M.D) : ∃ X, ∀ i, In M i X ↔ In M i I ∧ PSi M I Y i φ fs :=
  match sep M (def_PSG' M φ 0 1 2 (fun n => n + 3)) (vecIY M I Y fs) I with
  | ⟨X, hX⟩ => ⟨X, hX⟩

include hU in
theorem aeu_mono {P Q : M.D → Prop} (h : AEU M U P) (hq : ∀ i, In M i I → P i → Q i) : AEU M U Q :=
  match h with
  | ⟨A, hA, hP⟩ => ⟨A, hA, fun i hi => hq i (hU.1 A hA i hi) (hP i hi)⟩

include hU in
theorem aeu_and {P Q : M.D → Prop} (h1 : AEU M U P) (h2 : AEU M U Q) : AEU M U (fun i => P i ∧ Q i) :=
  match h1, h2 with
  | ⟨A, hA, hP⟩, ⟨B, hB, hQ⟩ => match inter_ex M A B with
    | ⟨C, hC⟩ => ⟨C, hU.2.2.1 A B C hA hB hC, fun i hi => ⟨hP i ((hC i).1 hi).1, hQ i ((hC i).1 hi).2⟩⟩

include hU in
theorem aeu_false {P : M.D → Prop} (h : AEU M U P) (hn : ∀ i, In M i I → P i → False) : False :=
  match h with
  | ⟨A, hA, hP⟩ => match hU.2.2.2.2 A hA with
    | ⟨i, hi⟩ => hn i (hU.1 A hA i hi) (hP i hi)

include hult in
theorem aeu_not (φ : Fm) (fs : Nat → M.D) (h : ¬ AEU M U (fun i => PSi M I Y i φ fs)) :
    AEU M U (fun i => ¬ PSi M I Y i φ fs) :=
  match xset M (I := I) (Y := Y) φ fs with
  | ⟨X, hX⟩ => match sep M (def_not M (def_mem M 0 1)) (fun _ => X) I with
    | ⟨C, hC⟩ => (hult X C (fun t ht => ((hX t).1 ht).1) (fun t => hC t)).elim
      (fun hXU => (h ⟨X, hXU, fun i hi => ((hX i).1 hi).2⟩).elim)
      (fun hCU => ⟨C, hCU, fun i hi hp => ((hC i).1 hi).2 ((hX i).2 ⟨((hC i).1 hi).1, hp⟩)⟩)

include K hac hU hult hY in
theorem los : (φ : Fm) → L0 φ → (fs : Nat → M.D) → (∀ n, FnY M I Y (fs n)) →
    (SatG (RI (EU M U) (STU M U Y)) (FnY M I Y) φ fs ↔ AEU M U (fun i => PSi M I Y i φ fs))
  | .atom r l, hl, fs, _ => by
    match r, l, hl with
    | 0, [_, _], _ => exact Iff.rfl
  | .bot, _, _, _ => ⟨fun h => h.elim, fun h => aeu_false M hU h (fun _ _ x => x)⟩
  | .conj a b, hl, fs, hf => ⟨fun ⟨x, y⟩ => aeu_and M hU ((los a hl.1 fs hf).1 x) ((los b hl.2 fs hf).1 y),
      fun h => ⟨(los a hl.1 fs hf).2 (aeu_mono M hU h (fun _ _ p => p.1)), (los b hl.2 fs hf).2 (aeu_mono M hU h (fun _ _ p => p.2))⟩⟩
  | .disj a b, hl, fs, hf => ⟨fun h => h.elim (fun x => aeu_mono M hU ((los a hl.1 fs hf).1 x) (fun _ _ p => .inl p))
        (fun y => aeu_mono M hU ((los b hl.2 fs hf).1 y) (fun _ _ p => .inr p)),
      fun h => (K.lem (AEU M U (fun i => PSi M I Y i a fs))).elim (fun x => .inl ((los a hl.1 fs hf).2 x))
        (fun hn => .inr ((los b hl.2 fs hf).2 (aeu_mono M hU (aeu_and M hU h (aeu_not M hult a fs hn))
          (fun _ _ p => p.1.elim (fun q => (p.2 q).elim) id))))⟩
  | .imp a b, hl, fs, hf => ⟨fun h => (K.lem (AEU M U (fun i => PSi M I Y i a fs))).elim
        (fun x => aeu_mono M hU ((los b hl.2 fs hf).1 (h ((los a hl.1 fs hf).2 x))) (fun _ _ p _ => p))
        (fun hn => aeu_mono M hU (aeu_not M hult a fs hn) (fun _ _ p q => (p q).elim)),
      fun h x => (los b hl.2 fs hf).2 (aeu_mono M hU (aeu_and M hU h ((los a hl.1 fs hf).1 x)) (fun _ _ p => p.1 p.2))⟩
  | .all a, hl, fs, hf =>
    ⟨fun H => CP.dne K _ (fun hn =>
      match aeu_not M hult (.all a) fs hn with
      | ⟨B, hB, hnB⟩ =>
        match choice_fam M (Pall M a) (def_Pall M a) K hac (vecW M B I Y fs) I Y (fun i hi =>
            (K.lem (In M i B)).elim
              (fun hiB => CP.dne K _ (fun hno => hnB i hiB (fun g hg => CP.dne K _ (fun hng =>
                match hg.1 i hi with
                | ⟨x, hx, hgx⟩ => hno ⟨x, hx, fun _ => ⟨g, hg, hgx, hng⟩⟩))))
              (fun hniB => match hY with
                | ⟨y, hy⟩ => ⟨y, hy, fun h' => (hniB h').elim⟩)) with
        | ⟨f, hfF, hfP⟩ =>
          have hext : ∀ n, FnY M I Y (ext fs f n) := fun n => match n with
            | 0 => hfF
            | n + 1 => hf n
          aeu_false M hU (aeu_and M hU ((los a hl (ext fs f) hext).1 (H f hfF)) ⟨B, hB, fun i hi => hi⟩)
            (fun i hiI ⟨hp, hiB⟩ => match hfF.1 i hiI with
              | ⟨x, _, hfx⟩ => match hfP i x hiI hfx hiB with
                | ⟨g, hg, hgx, hng⟩ => hng ((pt_local M hiI a hl (ext fs f) (ext fs g) hext (fun n => match n with
                    | 0 => hg
                    | n + 1 => hf n) (fun n => match n with
                    | 0 => fun x' y' h1 h2 => ceq_trans M.R (hfF.2 i x' x hiI h1 hfx) (hg.2 i x y' hiI hgx h2)
                    | n + 1 => fun x' y' h1 h2 => (hf n).2 i x' y' hiI h1 h2)).1 hp))),
     fun h g hg => (los a hl (ext fs g) (fun n => match n with
        | 0 => hg
        | n + 1 => hf n)).2 (aeu_mono M hU h (fun _ _ p => p g hg))⟩
  | .ex a, hl, fs, hf =>
    ⟨fun ⟨g, hg, s⟩ => aeu_mono M hU ((los a hl (ext fs g) (fun n => match n with
        | 0 => hg
        | n + 1 => hf n)).1 s) (fun _ _ p => ⟨g, hg, p⟩),
     fun ⟨A, hA, hAP⟩ =>
      match choice_fam M (Pex M a) (def_Pex M a) K hac (vecW M A I Y fs) I Y (fun i hi =>
          (K.lem (In M i A)).elim
            (fun hiA => match hAP i hiA with
              | ⟨g, hg, s⟩ => match hg.1 i hi with
                | ⟨x, hx, hgx⟩ => ⟨x, hx, fun _ => ⟨g, hg, hgx, s⟩⟩)
            (fun hniA => match hY with
              | ⟨y, hy⟩ => ⟨y, hy, fun h' => (hniA h').elim⟩)) with
      | ⟨f, hfF, hfP⟩ =>
        have hext : ∀ n, FnY M I Y (ext fs f n) := fun n => match n with
          | 0 => hfF
          | n + 1 => hf n
        ⟨f, hfF, (los a hl (ext fs f) hext).2 ⟨A, hA, fun i hiA =>
          have hiI : In M i I := hU.1 A hA i hiA
          match hfF.1 i hiI with
          | ⟨x, _, hfx⟩ => match hfP i x hiI hfx hiA with
            | ⟨g, hg, hgx, s⟩ => (pt_local M hiI a hl (ext fs f) (ext fs g) hext (fun n => match n with
                | 0 => hg
                | n + 1 => hf n) (fun n => match n with
                | 0 => fun x' y' h1 h2 => ceq_trans M.R (hfF.2 i x' x hiI h1 hfx) (hg.2 i x y' hiI hgx h2)
                | n + 1 => fun x' y' h1 h2 => (hf n).2 i x' y' hiI h1 h2)).2 s⟩⟩⟩

end L

end IZ

end Found

#print axioms Found.IZ.def_PSG'
#print axioms Found.IZ.pt_local
#print axioms Found.IZ.def_Pex
#print axioms Found.IZ.def_Pall
#print axioms Found.IZ.aeu_not
#print axioms Found.IZ.los

found/IZ18.lean

lean_all/found/IZ18.lean

/-
  T165 (general), part G6a. found/IZ18 — the internal ultrapower as a
  structure with st (f is standard iff it is almost everywhere a constant
  of Y), inside a model of ZFC:
  * U-almost-everywhere equality Sim, and every formula (with ∈ and st) is
    invariant under it;
  * Łoś at constants: at the constant functions of v, an L0 formula holds
    iff it holds in Y at v;
  * satisfaction relative to Y, and in the ultrapower, are definable;
  * dependence on the free variables only (SatG).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.IZ17

namespace Found

namespace IZ

open FO FOCT NBGc RF

variable (M : ZM)

theorem satG_agree {D : Type} (R : Nat → List D → Prop) (C : D → Prop) : (φ : Fm) → (k : Nat) → FV k φ →
    {v w : Nat → D} → (∀ n, n < k → v n = w n) → SatG R C φ v → SatG R C φ w
  | .atom r l, _, hf, _, _, h, s => by
    show R r (mapD _ l)
    rw [← mapD_agreeU h l hf]
    exact s
  | .bot, _, _, _, _, _, s => s
  | .conj a b, k, hf, _, _, h, s => ⟨satG_agree R C a k hf.1 h s.1, satG_agree R C b k hf.2 h s.2⟩
  | .disj a b, k, hf, _, _, h, s => s.elim (fun x => Or.inl (satG_agree R C a k hf.1 h x))
      (fun y => Or.inr (satG_agree R C b k hf.2 h y))
  | .imp a b, k, hf, _, _, h, s => fun x =>
      satG_agree R C b k hf.2 h (s (satG_agree R C a k hf.1 (fun n hn => (h n hn).symm) x))
  | .all a, k, hf, _, _, h, s => fun d hd => satG_agree R C a (k + 1) hf (ext_agreeU h d) (s d hd)
  | .ex a, k, hf, _, _, h, s => match s with
    | ⟨d, hd, x⟩ => ⟨d, hd, satG_agree R C a (k + 1) hf (ext_agreeU h d) x⟩

/-! ## definability of satisfaction relative to Y -/

def RM : Nat → List M.D → Prop := RI (fun x y => In M x y) (fun _ => False)

theorem satRM : (φ : Fm) → L0 φ → (C : M.D → Prop) → (v : Nat → M.D) → (SatG (RM M) C φ v ↔ SatG M.R C φ v)
  | .atom r l, hl, _, _ => by
    match r, l, hl with
    | 0, [_, _], _ => exact Iff.rfl
  | .bot, _, _, _ => Iff.rfl
  | .conj a b, hl, C, v => ⟨fun ⟨x, y⟩ => ⟨(satRM a hl.1 C v).1 x, (satRM b hl.2 C v).1 y⟩,
      fun ⟨x, y⟩ => ⟨(satRM a hl.1 C v).2 x, (satRM b hl.2 C v).2 y⟩⟩
  | .disj a b, hl, C, v => ⟨fun x => x.elim (fun x => .inl ((satRM a hl.1 C v).1 x)) (fun y => .inr ((satRM b hl.2 C v).1 y)),
      fun x => x.elim (fun x => .inl ((satRM a hl.1 C v).2 x)) (fun y => .inr ((satRM b hl.2 C v).2 y))⟩
  | .imp a b, hl, C, v => ⟨fun f x => (satRM b hl.2 C v).1 (f ((satRM a hl.1 C v).2 x)),
      fun f x => (satRM b hl.2 C v).2 (f ((satRM a hl.1 C v).1 x))⟩
  | .all a, hl, C, v => ⟨fun f d hd => (satRM a hl C (ext v d)).1 (f d hd), fun f d hd => (satRM a hl C (ext v d)).2 (f d hd)⟩
  | .ex a, hl, C, v => ⟨fun ⟨d, hd, x⟩ => ⟨d, hd, (satRM a hl C (ext v d)).1 x⟩,
      fun ⟨d, hd, x⟩ => ⟨d, hd, (satRM a hl C (ext v d)).2 x⟩⟩

/-- satisfaction relative to the set at position yi -/
theorem def_SatY (φ : Fm) (hl : L0 φ) (yi : Nat) (σ : Nat → Nat) :
    Def M (fun u => SatG M.R (YC M (u yi)) φ (fun n => u (σ n))) :=
  def_congr M (fun _ => satRM M φ hl _ _)
    (def_ren M (ρ3 yi yi yi σ) (def_interp M (fun _ x y => In M x y) (fun _ _ => False) (fun τ x => In M x (τ 0))
      (fun a b _ => def_mem M a b) (fun _ _ => def_false M) (fun a k => def_mem M a (0 + k)) φ (fun n => n + 3) 0))

/-! ## the ultrapower with st -/

def RSt (U Y : M.D) : Nat → List M.D → Prop := RI (EU M U) (STU M U Y)

/-- almost everywhere equal -/
def Sim (U f g : M.D) : Prop := AEU M U (fun i => ∀ a b, InG M f i a → InG M g i b → Ceq M.R a b)

theorem def_AEUx {P : (Nat → M.D) → Prop} (hP : Def M P) (ui : Nat) :
    Def M (fun u => ∃ A, In M A (u ui) ∧ ∀ i, In M i A → P (ext (ext u A) i)) :=
  def_ex M (def_and M (def_mem M 0 (ui + 1)) (def_all M (def_imp M (def_mem M 0 1) hP)))

theorem def_EU (ui fi gi : Nat) : Def M (fun u => EU M (u ui) (u fi) (u gi)) :=
  def_congr M (fun _ => Iff.rfl) (def_AEUx M (def_Ept M 0 (fi + 2) (gi + 2)) ui)

theorem def_STU (ui yi fi : Nat) : Def M (fun u => STU M (u ui) (u yi) (u fi)) :=
  def_congr M (fun _ => Iff.rfl) (def_ex M (def_and M (def_mem M 0 (yi + 1))
    (def_AEUx M (def_all M (def_imp M (def_InG M (fi + 4) 1 0) (def_ceq M 0 3))) (ui + 1))))

/-- satisfaction in the ultrapower is definable (τ 0 = U, τ 1 = I, τ 2 = Y) -/
theorem def_SatS (φ : Fm) (σ : Nat → Nat) (k : Nat) :
    Def M (fun u => SatG (RSt M (u (0 + k)) (u (2 + k))) (FnY M (u (1 + k)) (u (2 + k))) φ (fun n => u (σ n))) :=
  def_interp M (fun τ => EU M (τ 0)) (fun τ => STU M (τ 0) (τ 2)) (fun τ => FnY M (τ 1) (τ 2))
    (fun a b k => def_EU M (0 + k) a b) (fun a k => def_STU M (0 + k) (2 + k) a)
    (fun a k => def_FnY M (1 + k) (2 + k) a) φ σ k

section S

variable {I Y U F0 : M.D} (hU : PF M I F0 U) (hIU : In M I U)

include hU in
theorem sim_symm {f g : M.D} (h : Sim M U f g) : Sim M U g f :=
  aeu_mono M hU h (fun _ _ p a b ha hb => ceq_symm M.R (p b a hb ha))

include hIU in
theorem sim_refl {f : M.D} (hf : FnY M I Y f) : Sim M U f f :=
  ⟨I, hIU, fun i hi a b ha hb => hf.2 i a b hi ha hb⟩

include hU in
theorem eu_resp {f g f' g' : M.D} (hf : FnY M I Y f) (hg : FnY M I Y g) (e1 : Sim M U f f') (e2 : Sim M U g g')
    (h : EU M U f g) : EU M U f' g' :=
  aeu_mono M hU (aeu_and M hU h (aeu_and M hU e1 e2)) (fun i hi ⟨p, q1, q2⟩ a' b' ha' hb' =>
    match hf.1 i hi, hg.1 i hi with
    | ⟨a, _, ha⟩, ⟨b, _, hb⟩ => mem_ceq_r M (q2 b b' hb hb') (mem_ceq_l M (q1 a a' ha ha') (p a b ha hb)))

include hU in
theorem stu_resp {f f' : M.D} (hf : FnY M I Y f) (e : Sim M U f f') (h : STU M U Y f) : STU M U Y f' :=
  match h with
  | ⟨y, hy, hA⟩ => ⟨y, hy, aeu_mono M hU (aeu_and M hU hA e) (fun i hi ⟨p, q⟩ a' ha' =>
      match hf.1 i hi with
      | ⟨a, _, ha⟩ => ceq_trans M.R (ceq_symm M.R (q a a' ha ha')) (p a ha))⟩

include hU hIU in
/-- every formula is invariant under almost-everywhere equality -/
theorem satS_resp : (φ : Fm) → (fs gs : Nat → M.D) → (∀ n, FnY M I Y (fs n)) → (∀ n, FnY M I Y (gs n)) →
    (∀ n, Sim M U (fs n) (gs n)) → SatG (RSt M U Y) (FnY M I Y) φ fs → SatG (RSt M U Y) (FnY M I Y) φ gs
  | .atom r l, fs, gs, hf, hg, e, s => by
    match r, l with
    | 0, [] => exact s
    | 0, [_] => exact s
    | 0, [a, b] => exact eu_resp M hU (hf a) (hf b) (e a) (e b) s
    | 0, _ :: _ :: _ :: _ => exact s
    | 1, [] => exact s
    | 1, [a] => exact stu_resp M hU (hf a) (e a) s
    | 1, _ :: _ :: _ => exact s
    | _ + 2, _ => exact s
  | .bot, _, _, _, _, _, s => s
  | .conj a b, fs, gs, hf, hg, e, s => ⟨satS_resp a fs gs hf hg e s.1, satS_resp b fs gs hf hg e s.2⟩
  | .disj a b, fs, gs, hf, hg, e, s => s.elim (fun x => .inl (satS_resp a fs gs hf hg e x))
      (fun y => .inr (satS_resp b fs gs hf hg e y))
  | .imp a b, fs, gs, hf, hg, e, s => fun x =>
      satS_resp b fs gs hf hg e (s (satS_resp a gs fs hg hf (fun n => sim_symm M hU (e n)) x))
  | .all a, fs, gs, hf, hg, e, s => fun d hd => satS_resp a (ext fs d) (ext gs d)
      (fun n => match n with
        | 0 => hd
        | n + 1 => hf n) (fun n => match n with
        | 0 => hd
        | n + 1 => hg n) (fun n => match n with
        | 0 => ⟨I, hIU, fun i hi a b ha hb => hd.2 i a b hi ha hb⟩
        | n + 1 => e n) (s d hd)
  | .ex a, fs, gs, hf, hg, e, s => match s with
    | ⟨d, hd, x⟩ => ⟨d, hd, satS_resp a (ext fs d) (ext gs d)
      (fun n => match n with
        | 0 => hd
        | n + 1 => hf n) (fun n => match n with
        | 0 => hd
        | n + 1 => hg n) (fun n => match n with
        | 0 => ⟨I, hIU, fun i hi a b ha hb => hd.2 i a b hi ha hb⟩
        | n + 1 => e n) x⟩

def vecI3 (i I Y : M.D) : Nat → M.D
  | 0 => i
  | 1 => I
  | _ + 2 => Y

include hU hIU in
/-- Łoś at constants -/
theorem losC (K : CP.Classical') (hac : ACsem M)
    (hult : ∀ S C, SubS M S I → (∀ t, In M t C ↔ In M t I ∧ ¬ In M t S) → In M S U ∨ In M C U)
    (hY : ∃ y, In M y Y) (φ : Fm) (hl : L0 φ) (cs v : Nat → M.D) (hc : ∀ n, FnY M I Y (cs n))
    (hcv : ∀ n i, In M i I → InG M (cs n) i (v n)) :
    SatG (RSt M U Y) (FnY M I Y) φ cs ↔ SatG M.R (YC M Y) φ v :=
  (los M K hac hU hult hY φ hl cs hc).trans
    ⟨fun ⟨A, hA, hP⟩ => match hU.2.2.2.2 A hA with
      | ⟨i, hi⟩ => (pt_val M (τ := vecI3 M i I Y) (hU.1 A hA i hi) φ hl cs hc v
          (fun n => hcv n i (hU.1 A hA i hi))).1 (hP i hi),
     fun h => ⟨I, hIU, fun i hi => (pt_val M (τ := vecI3 M i I Y) hi φ hl cs hc v (fun n => hcv n i hi)).2 h⟩⟩

end S

end IZ

end Found

#print axioms Found.IZ.satG_agree
#print axioms Found.IZ.def_SatY
#print axioms Found.IZ.def_SatS
#print axioms Found.IZ.satS_resp
#print axioms Found.IZ.losC

found/IZ19.lean

lean_all/found/IZ19.lean

/-
  T165 (general), part G6b. found/IZ19 — transfer and standardization in
  the internal ultrapower inside a model of ZFC:
  * transfer: standard parameters are a.e. constants; Łoś at constants turns
    "θ for all standard x" into "θ for all x in Y", and Łoś pointwise gives
    θ for every x;
  * standardization: for a standard x ≈ const x₀, the set
    y₀ = {z₀ ∈ x₀ | the ultrapower satisfies Φ at const z₀} exists by
    separation (ultrapower satisfaction is definable), and const y₀ works.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.IZ18
import found.Ist1

namespace Found

namespace IZ

open FO FOCT NBGc RF AK

variable (M : ZM)

/-! ## generic lemmas -/

theorem satG_ren {D : Type} (R : Nat → List D → Prop) (C : D → Prop) : (φ : Fm) → (ρ : Nat → Nat) → (v : Nat → D) →
    (SatG R C (ren ρ φ) v ↔ SatG R C φ (fun n => v (ρ n)))
  | .atom r l, ρ, v => by
    show R r (mapD v (mapL ρ l)) ↔ R r (mapD _ l)
    rw [mapD_mapL]
  | .bot, _, _ => Iff.rfl
  | .conj a b, ρ, v => ⟨fun ⟨x, y⟩ => ⟨(satG_ren R C a ρ v).1 x, (satG_ren R C b ρ v).1 y⟩,
      fun ⟨x, y⟩ => ⟨(satG_ren R C a ρ v).2 x, (satG_ren R C b ρ v).2 y⟩⟩
  | .disj a b, ρ, v => ⟨fun x => x.elim (fun x => Or.inl ((satG_ren R C a ρ v).1 x)) (fun y => Or.inr ((satG_ren R C b ρ v).1 y)),
      fun x => x.elim (fun x => Or.inl ((satG_ren R C a ρ v).2 x)) (fun y => Or.inr ((satG_ren R C b ρ v).2 y))⟩
  | .imp a b, ρ, v => ⟨fun f x => (satG_ren R C b ρ v).1 (f ((satG_ren R C a ρ v).2 x)),
      fun f x => (satG_ren R C b ρ v).2 (f ((satG_ren R C a ρ v).1 x))⟩
  | .all a, ρ, v => ⟨fun f d hd => (satG_ext R C a (ext_up v ρ d)).1 ((satG_ren R C a (up ρ) (ext v d)).1 (f d hd)),
      fun f d hd => (satG_ren R C a (up ρ) (ext v d)).2 ((satG_ext R C a (ext_up v ρ d)).2 (f d hd))⟩
  | .ex a, ρ, v => ⟨fun ⟨d, hd, x⟩ => ⟨d, hd, (satG_ext R C a (ext_up v ρ d)).1 ((satG_ren R C a (up ρ) (ext v d)).1 x)⟩,
      fun ⟨d, hd, x⟩ => ⟨d, hd, (satG_ren R C a (up ρ) (ext v d)).2 ((satG_ext R C a (ext_up v ρ d)).2 x)⟩⟩

theorem guard_introG {D : Type} (R : Nat → List D → Prop) (C : D → Prop) : (k : Nat) → (φ : Fm) → (v : Nat → D) →
    ((∀ i, i < k → R 1 [v i]) → SatG R C φ v) → SatG R C (guardImp k φ) v
  | 0, _, _, h => h (fun i hi => absurd hi (Nat.not_lt_zero i))
  | k + 1, φ, v, h => guard_introG R C k (.imp (Mv k) φ) v (fun hk hkk => h (fun i hi =>
      (Nat.lt_or_ge i k).elim (hk i) (fun hge => by
        have e : i = k := Nat.le_antisymm (Nat.le_of_lt_succ hi) hge
        rw [e]
        exact hkk)))

/-! ## the setting -/

structure UP where
  I : M.D
  Y : M.D
  U : M.D
  F0 : M.D
  hU : PF M I F0 U
  hIU : In M I U
  hult : ∀ S C, SubS M S I → (∀ t, In M t C ↔ In M t I ∧ ¬ In M t S) → In M S U ∨ In M C U
  hY : ∃ y, In M y Y
  hsub : ∀ x z, In M z Y → SubS M x z → In M x Y

variable {M} (P : UP M)

def SS (φ : Fm) (fs : Nat → M.D) : Prop := SatG (RSt M P.U P.Y) (FnY M P.I P.Y) φ fs

/-- c is the constant function with value a -/
def IsC (c a : M.D) : Prop := FnY M P.I P.Y c ∧ ∀ i, In M i P.I → InG M c i a

theorem isC_ex {a : M.D} (ha : In M a P.Y) : ∃ c, IsC P c a :=
  match const_ex M P.I P.Y ha with
  | ⟨c, h1, h2⟩ => ⟨c, h1, h2⟩

theorem isC_val {c a b i : M.D} (hc : IsC P c a) (hi : In M i P.I) (h : InG M c i b) : Ceq M.R b a :=
  hc.1.2 i b a hi h (hc.2 i hi)

theorem aeu_const {p : Prop} : AEU M P.U (fun _ => p) ↔ p :=
  ⟨fun ⟨A, hA, h⟩ => match P.hU.2.2.2.2 A hA with
    | ⟨i, hi⟩ => h i hi, fun h => ⟨P.I, P.hIU, fun _ _ => h⟩⟩

theorem eu_const {c c' a b : M.D} (hc : IsC P c a) (hc' : IsC P c' b) : EU M P.U c c' ↔ In M a b :=
  ⟨fun ⟨A, hA, h⟩ => match P.hU.2.2.2.2 A hA with
    | ⟨i, hi⟩ => h i hi a b (hc.2 i (P.hU.1 A hA i hi)) (hc'.2 i (P.hU.1 A hA i hi)),
   fun h => ⟨P.I, P.hIU, fun _ hi _ _ ha hb =>
     mem_ceq_r M (ceq_symm M.R (isC_val P hc' hi hb)) (mem_ceq_l M (ceq_symm M.R (isC_val P hc hi ha)) h)⟩⟩

theorem stu_const {c a : M.D} (hc : IsC P c a) (ha : In M a P.Y) : STU M P.U P.Y c :=
  ⟨a, ha, P.I, P.hIU, fun _ hi _ hb => isC_val P hc hi hb⟩

theorem sim_C {f c a : M.D} (hc : IsC P c a)
    (h : AEU M P.U (fun i => ∀ b, InG M f i b → Ceq M.R b a)) : Sim M P.U f c :=
  aeu_mono M P.hU h (fun _ hi p b _ hb hb' => ceq_trans M.R (p b hb) (ceq_symm M.R (isC_val P hc hi hb')))

theorem std_sim {f : M.D} (h : STU M P.U P.Y f) :
    ∃ a c, In M a P.Y ∧ IsC P c a ∧ Sim M P.U f c :=
  match h with
  | ⟨a, ha, hA⟩ => match isC_ex P ha with
    | ⟨c, hc⟩ => ⟨a, c, ha, hc, sim_C P hc hA⟩

theorem sim_trans {f g h : M.D} (hg : FnY M P.I P.Y g) (e1 : Sim M P.U f g) (e2 : Sim M P.U g h) : Sim M P.U f h :=
  aeu_mono M P.hU (aeu_and M P.hU e1 e2) (fun i hi ⟨p, q⟩ a c ha hc => match hg.1 i hi with
    | ⟨b, _, hb⟩ => ceq_trans M.R (p a b ha hb) (q b c hb hc))

theorem ss_resp {φ : Fm} {fs gs : Nat → M.D} (hf : ∀ n, FnY M P.I P.Y (fs n)) (hg : ∀ n, FnY M P.I P.Y (gs n))
    (e : ∀ n, Sim M P.U (fs n) (gs n)) : SS P φ fs ↔ SS P φ gs :=
  ⟨satS_resp M P.hU P.hIU φ fs gs hf hg e, satS_resp M P.hU P.hIU φ gs fs hg hf (fun n => sim_symm M P.hU (e n))⟩

theorem fnY_ext {fs : Nat → M.D} (hf : ∀ n, FnY M P.I P.Y (fs n)) {d : M.D} (hd : FnY M P.I P.Y d) :
    ∀ n, FnY M P.I P.Y (ext fs d n)
  | 0 => hd
  | n + 1 => hf n

theorem sim_ext {fs gs : Nat → M.D} (e : ∀ n, Sim M P.U (fs n) (gs n)) {d : M.D} (hd : FnY M P.I P.Y d) :
    ∀ n, Sim M P.U (ext fs d n) (ext gs d n)
  | 0 => sim_refl M P.hIU hd
  | n + 1 => e n

/-! ## transfer -/

theorem trans_S (K : CP.Classical') (hac : ACsem M) {θ : Fm} {k : Nat} (hl : L0 θ) (hf : FV (k + 1) θ)
    (fs : Nat → M.D) (hfs : ∀ n, FnY M P.I P.Y (fs n)) : SS P (IST.transAx θ k) fs :=
  guard_introG _ _ k _ fs (fun hg hyp x hx => by
    let Pn : Nat → M.D → Prop := fun n y => In M y P.Y ∧ (n < k → AEU M P.U (fun i => ∀ a, InG M (fs n) i a → Ceq M.R a y))
    have exn : ∀ n, ∃ y, Pn n y := fun n => (Nat.lt_or_ge n k).elim
      (fun h => match hg n h with
        | ⟨y, hy, hA⟩ => ⟨y, hy, fun _ => hA⟩)
      (fun h => match P.hY with
        | ⟨y, hy⟩ => ⟨y, hy, fun h' => absurd h' (Nat.not_lt_of_le h)⟩)
    let yv : Nat → M.D := fun n => K.ch (Pn n) (exn n)
    have hyv : ∀ n, Pn n (yv n) := fun n => K.ch_spec (Pn n) (exn n)
    let cs : Nat → M.D := fun n => K.ch (fun c => IsC P c (yv n)) (isC_ex P (hyv n).1)
    have hcs : ∀ n, IsC P (cs n) (yv n) := fun n => K.ch_spec (fun c => IsC P c (yv n)) (isC_ex P (hyv n).1)
    let ws : Nat → M.D := mixk M k cs fs
    have hws : ∀ n, FnY M P.I P.Y (ws n) := fun n => (Nat.lt_or_ge n k).elim
      (fun h => by show FnY M P.I P.Y (mixk M k cs fs n); rw [mixk_lt M k cs fs h]; exact (hcs n).1)
      (fun h => by show FnY M P.I P.Y (mixk M k cs fs n); rw [mixk_ge M k cs fs h]; exact hfs n)
    have sw : ∀ n, Sim M P.U (fs n) (ws n) := fun n => (Nat.lt_or_ge n k).elim
      (fun h => by
        show Sim M P.U (fs n) (mixk M k cs fs n)
        rw [mixk_lt M k cs fs h]
        exact sim_C P (hcs n) ((hyv n).2 h))
      (fun h => by
        show Sim M P.U (fs n) (mixk M k cs fs n)
        rw [mixk_ge M k cs fs h]
        exact sim_refl M P.hIU (hfs n))
    have agr : ∀ d, ∀ n, n < k + 1 → ext ws d n = ext cs d n := fun d n hn => match n with
      | 0 => rfl
      | n + 1 => mixk_lt M k cs fs (Nat.lt_of_succ_lt_succ hn)
    have E : ∀ d, FnY M P.I P.Y d → (SS P θ (ext fs d) ↔ SS P θ (ext cs d)) := fun d hd =>
      (ss_resp P (fnY_ext P hfs hd) (fnY_ext P hws hd) (sim_ext P sw hd)).trans
        ⟨satG_agree _ _ θ (k + 1) hf (agr d), satG_agree _ _ θ (k + 1) hf (fun n hn => (agr d n hn).symm)⟩
    have hcsF : ∀ n, FnY M P.I P.Y (cs n) := fun n => (hcs n).1
    have allY : ∀ x0, In M x0 P.Y → SatG M.R (YC M P.Y) θ (ext yv x0) := fun x0 hx0 =>
      match isC_ex P hx0 with
      | ⟨c0, hc0⟩ => (losC M P.hU P.hIU K hac P.hult P.hY θ hl (ext cs c0) (ext yv x0) (fnY_ext P hcsF hc0.1)
          (fun n i hi => match n with
            | 0 => hc0.2 i hi
            | n + 1 => (hcs n).2 i hi)).1 ((E c0 hc0.1).1 (hyp c0 hc0.1 (stu_const P hc0 hx0)))
    exact (E x hx).2 ((los M K hac P.hU P.hult P.hY θ hl (ext cs x) (fnY_ext P hcsF hx)).2 ⟨P.I, P.hIU, fun i hi =>
      have exv : ∃ a, In M a P.Y ∧ InG M x i a := hx.1 i hi
      let xi := K.ch (fun a => In M a P.Y ∧ InG M x i a) exv
      have hxi := K.ch_spec (fun a => In M a P.Y ∧ InG M x i a) exv
      (pt_val M (τ := vecI3 M i P.I P.Y) hi θ hl (ext cs x) (fnY_ext P hcsF hx) (ext yv xi) (fun n => match n with
        | 0 => hxi.2
        | n + 1 => (hcs n).2 i hi)).2 (allY xi hxi.1)⟩))

/-! ## standardization -/

theorem simCC {c c' a : M.D} (hc : IsC P c a) (hc' : IsC P c' a) : Sim M P.U c c' :=
  ⟨P.I, P.hIU, fun _ hi _ _ hb hb' => ceq_trans M.R (isC_val P hc hi hb) (ceq_symm M.R (isC_val P hc' hi hb'))⟩

def σ5 : Nat → Nat
  | 0 => 0
  | m + 1 => m + 5

def vUIY (U I Y : M.D) (fs : Nat → M.D) : Nat → M.D
  | 0 => U
  | 1 => I
  | 2 => Y
  | n + 3 => fs n

theorem std_S (Φ : Fm) (k : Nat) (fs : Nat → M.D) (hfs : ∀ n, FnY M P.I P.Y (fs n)) :
    SS P (IST.stdAx Φ k) fs :=
  guard_introG _ _ k _ fs (fun _ x hx hstx => by
    let Q : (Nat → M.D) → Prop := fun u => ∀ g, FnY M (u 2) (u 3) g → (∀ i, In M i (u 2) → InG M g i (u 0)) →
      SatG (RSt M (u 1) (u 3)) (FnY M (u 2) (u 3)) Φ (ext (fun n => u (n + 4)) g)
    have hX : Def M (fun u => SatG (RSt M (u 2) (u 4)) (FnY M (u 3) (u 4)) Φ (ext (fun n => u (n + 5)) (u 0))) :=
      def_congr M (fun u => satG_ext _ _ Φ (v := fun n => u (σ5 n)) (w := ext (fun n => u (n + 5)) (u 0))
          (fun n => match n with
            | 0 => rfl
            | _ + 1 => rfl))
        (def_SatS M Φ σ5 2)
    have hQ : Def M Q := def_congr M (fun _ => Iff.rfl) (def_all M (def_imp M (def_FnY M 3 4 0)
      (def_imp M (def_all M (def_imp M (def_mem M 0 4) (def_InG M 1 0 2))) hX)))
    match std_sim P hstx with
    | ⟨x0, cx, hx0, hcx, sx⟩ =>
    match sep M hQ (vUIY P.U P.I P.Y fs) x0 with
    | ⟨y0, hy0⟩ =>
    have hy0Y : In M y0 P.Y := P.hsub y0 x0 hx0 (fun t ht => ((hy0 t).1 ht).1)
    match isC_ex P hy0Y with
    | ⟨cy, hcy⟩ =>
    exact ⟨cy, hcy.1, stu_const P hcy hy0Y, fun z hz hstz =>
      match std_sim P hstz with
      | ⟨z0, cz, _, hcz, sz⟩ =>
        have e1 : EU M P.U z cy ↔ In M z0 y0 :=
          ⟨fun h => (eu_const P hcz hcy).1 (eu_resp M P.hU hz hcy.1 sz (sim_refl M P.hIU hcy.1) h),
           fun h => eu_resp M P.hU hcz.1 hcy.1 (sim_symm M P.hU sz) (sim_refl M P.hIU hcy.1) ((eu_const P hcz hcy).2 h)⟩
        have e2 : EU M P.U z x ↔ In M z0 x0 :=
          ⟨fun h => (eu_const P hcz hcx).1 (eu_resp M P.hU hz hx sz sx h),
           fun h => eu_resp M P.hU hcz.1 hcx.1 (sim_symm M P.hU sz) (sim_symm M P.hU sx) ((eu_const P hcz hcx).2 h)⟩
        have e3 : (∀ g, FnY M P.I P.Y g → (∀ i, In M i P.I → InG M g i z0) → SS P Φ (ext fs g)) ↔ SS P Φ (ext fs z) :=
          ⟨fun h => (ss_resp P (fnY_ext P hfs hcz.1) (fnY_ext P hfs hz) (fun n => match n with
              | 0 => sim_symm M P.hU sz
              | n + 1 => sim_refl M P.hIU (hfs n))).1 (h cz hcz.1 hcz.2),
           fun h g hg hgi => (ss_resp P (fnY_ext P hfs hz) (fnY_ext P hfs hg) (fun n => match n with
              | 0 => sim_trans P hcz.1 sz (simCC P hcz ⟨hg, hgi⟩)
              | n + 1 => sim_refl M P.hIU (hfs n))).1 h⟩
        have e4 : SatG (RSt M P.U P.Y) (FnY M P.I P.Y) (ren IST.ρ2 Φ) (ext (ext (ext fs x) cy) z) ↔ SS P Φ (ext fs z) :=
          (satG_ren _ _ Φ IST.ρ2 _).trans (satG_ext _ _ Φ (fun n => match n with
            | 0 => rfl
            | _ + 1 => rfl))
        ⟨fun h => match (hy0 z0).1 (e1.1 h) with
          | ⟨h1, h2⟩ => ⟨e2.2 h1, e4.2 (e3.1 h2)⟩,
         fun ⟨h1, h2⟩ => e1.2 ((hy0 z0).2 ⟨e2.1 h1, e3.2 (e4.1 h2)⟩)⟩⟩)

end IZ

end Found

#print axioms Found.IZ.satG_ren
#print axioms Found.IZ.guard_introG
#print axioms Found.IZ.ss_resp
#print axioms Found.IZ.trans_S
#print axioms Found.IZ.std_S

found/IZ2.lean

lean_all/found/IZ2.lean

/-
  Shared tool R3, part 2. found/IZ2 — ordinals inside a model of ZF:
  * transitive sets and ordinals (transitive sets of transitive sets; with
    foundation these are the von Neumann ordinals), both definable and
    invariant under coextension;
  * members of ordinals are ordinals;
  * trichotomy: any two ordinals are comparable (x ∈ y, x ≈ y or y ∈ x),
    by a double ∈-minimal counterexample (foundation, classical point);
  * every nonempty definable collection of ordinals has an ∈-least member.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.IZ1

namespace Found

namespace IZ

open FO FOCT NBGc

variable (M : ZM)

def Trans (x : M.D) : Prop := ∀ y z, In M y x → In M z y → In M z x

def Ord (x : M.D) : Prop := Trans M x ∧ ∀ y, In M y x → Trans M y

theorem def_Trans (i : Nat) : Def M (fun v => Trans M (v i)) :=
  def_congr M (fun _ => Iff.rfl) (def_all M (def_all M (def_imp M (def_mem M 1 (i + 2))
    (def_imp M (def_mem M 0 1) (def_mem M 0 (i + 2))))))

theorem def_Ord (i : Nat) : Def M (fun v => Ord M (v i)) :=
  def_and M (def_Trans M i) (def_congr M (fun _ => Iff.rfl)
    (def_all M (def_imp M (def_mem M 0 (i + 1)) (def_Trans M 0))))

theorem mem_ceq_l {x x' y : M.D} (e : Ceq M.R x x') (h : In M x y) : In M x' y :=
  mem_resp M.R (extD M) e (ceq_refl M.R y) h

theorem mem_ceq_r {x y y' : M.D} (e : Ceq M.R y y') (h : In M x y) : In M x y' := (e x).1 h

theorem trans_resp {x x' : M.D} (e : Ceq M.R x x') (h : Trans M x) : Trans M x' := fun y z hy hz =>
  mem_ceq_r M e (h y z (mem_ceq_r M (ceq_symm M.R e) hy) hz)

theorem ord_resp {x x' : M.D} (e : Ceq M.R x x') (h : Ord M x) : Ord M x' :=
  ⟨trans_resp M e h.1, fun y hy => h.2 y (mem_ceq_r M (ceq_symm M.R e) hy)⟩

theorem ord_mem {x y : M.D} (hx : Ord M x) (hy : In M y x) : Ord M y :=
  ⟨hx.2 y hy, fun z hz => hx.2 z (hx.1 y z hy hz)⟩

/-- comparability -/
def Cmp (x y : M.D) : Prop := In M x y ∨ Ceq M.R x y ∨ In M y x

theorem def_Cmp (i j : Nat) : Def M (fun v => Cmp M (v i) (v j)) :=
  def_or M (def_mem M i j) (def_or M (def_ceq M i j) (def_mem M j i))

/-- no set is a member of itself -/
theorem not_mem_self (x : M.D) : ¬ In M x x := fun hxx =>
  match found M (P := fun v => In M (v 0) (v 0)) (def_mem M 0 0) (fun _ => x) ⟨x, hxx⟩ with
  | ⟨y, hy, hmin⟩ => hmin y hy hy

/-- trichotomy for ordinals -/
theorem ord_cmp (K : CP.Classical') {x y : M.D} (hx : Ord M x) (hy : Ord M y) : Cmp M x y := by
  -- predicate: x is an ordinal with an incomparable ordinal partner
  let Bad : (Nat → M.D) → Prop := fun v => Ord M (v 0) ∧ ∃ d, Ord M d ∧ ¬ Cmp M (v 0) d
  have hBad : Def M Bad := def_and M (def_Ord M 0)
    (def_ex M (def_and M (def_Ord M 0) (def_not M (def_Cmp M 1 0))))
  refine CP.dne K _ (fun hn => ?_)
  have ex : ∃ a, Bad (ext (fun _ => x) a) := ⟨x, hx, y, hy, hn⟩
  match found M hBad (fun _ => x) ex with
  | ⟨a, ⟨ha, b0, hb0, hab0⟩, amin⟩ =>
    -- minimal partner b for a
    let Bad2 : (Nat → M.D) → Prop := fun v => Ord M (v 0) ∧ ¬ Cmp M (v 1) (v 0)
    have hBad2 : Def M Bad2 := def_and M (def_Ord M 0) (def_not M (def_Cmp M 1 0))
    match found M hBad2 (fun _ => a) ⟨b0, hb0, hab0⟩ with
    | ⟨b, ⟨hb, hab⟩, bmin⟩ =>
      -- every member of a is comparable with b and lies in b
      have h1 : ∀ z, In M z a → In M z b := fun z hz => by
        have hzo : Ord M z := ord_mem M ha hz
        have czb : Cmp M z b := CP.dne K _ (fun hnc => amin z hz ⟨hzo, b, hb, hnc⟩)
        match czb with
        | .inl h => exact h
        | .inr (.inl e) => exact (hab (.inr (.inr (mem_ceq_l M e hz)))).elim
        | .inr (.inr h) => exact (hab (.inr (.inr (ha.1 z b hz h)))).elim
      have h2 : ∀ z, In M z b → In M z a := fun z hz => by
        have hzo : Ord M z := ord_mem M hb hz
        have caz : Cmp M a z := CP.dne K _ (fun hnc => bmin z hz ⟨hzo, hnc⟩)
        match caz with
        | .inl h => exact (hab (.inl (hb.1 z a hz h))).elim
        | .inr (.inl e) => exact (hab (.inl (mem_ceq_l M (ceq_symm M.R e) hz))).elim
        | .inr (.inr h) => exact h
      exact hab (.inr (.inl (fun z => ⟨h1 z, h2 z⟩)))

/-- least elements of definable collections of ordinals -/
theorem ord_least (K : CP.Classical') {P : (Nat → M.D) → Prop} (hP : Def M P) (v : Nat → M.D)
    (hex : ∃ a, Ord M a ∧ P (ext v a)) :
    ∃ a, Ord M a ∧ P (ext v a) ∧ ∀ b, Ord M b → P (ext v b) → In M a b ∨ Ceq M.R a b :=
  match found M (def_and M (def_Ord M 0) hP) v hex with
  | ⟨a, ⟨ha, hpa⟩, amin⟩ => ⟨a, ha, hpa, fun b hb hpb =>
    match ord_cmp M K ha hb with
    | .inl h => .inl h
    | .inr (.inl e) => .inr e
    | .inr (.inr h) => (amin b h ⟨hb, hpb⟩).elim⟩

end IZ

end Found

#print axioms Found.IZ.def_Ord
#print axioms Found.IZ.ord_resp
#print axioms Found.IZ.ord_mem
#print axioms Found.IZ.not_mem_self
#print axioms Found.IZ.ord_cmp
#print axioms Found.IZ.ord_least

found/IZ20.lean

lean_all/found/IZ20.lean

/-
  T165 (general), part G6c. found/IZ20 — idealization in the internal
  ultrapower inside a model of ZFC (I = finite members of Y, U fine):
  * a finite set z₀ of Y: a function almost everywhere in z₀ is almost
    everywhere one of its members (Kuratowski induction);
  * (→) for every i ∈ I, Łoś at constants puts a witness for the finite set
    i into Y; a function choosing them works for every standard y, since
    {i | y ∈ i} ∈ U;
  * (←) elements of a standard finite set are standard.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.IZ19
import found.Ist4

namespace Found

namespace IZ

open FO FOCT NBGc RF AK

variable {M : ZM} (P : UP M)

/-! ## fixing standard parameters -/

theorem cparams (K : CP.Classical') {k : Nat} (fs : Nat → M.D) (hfs : ∀ n, FnY M P.I P.Y (fs n))
    (hg : ∀ i, i < k → STU M P.U P.Y (fs i)) :
    ∃ yv cs : Nat → M.D, (∀ n, In M (yv n) P.Y) ∧ (∀ n, IsC P (cs n) (yv n)) ∧
      (∀ n, Sim M P.U (fs n) (mixk M k cs fs n)) ∧ (∀ n, FnY M P.I P.Y (mixk M k cs fs n)) :=
  let Pn : Nat → M.D → Prop := fun n y => In M y P.Y ∧ (n < k → AEU M P.U (fun i => ∀ a, InG M (fs n) i a → Ceq M.R a y))
  have exn : ∀ n, ∃ y, Pn n y := fun n => (Nat.lt_or_ge n k).elim
    (fun h => match hg n h with
      | ⟨y, hy, hA⟩ => ⟨y, hy, fun _ => hA⟩)
    (fun h => match P.hY with
      | ⟨y, hy⟩ => ⟨y, hy, fun h' => absurd h' (Nat.not_lt_of_le h)⟩)
  let yv : Nat → M.D := fun n => K.ch (Pn n) (exn n)
  have hyv : ∀ n, Pn n (yv n) := fun n => K.ch_spec (Pn n) (exn n)
  let cs : Nat → M.D := fun n => K.ch (fun c => IsC P c (yv n)) (isC_ex P (hyv n).1)
  have hcs : ∀ n, IsC P (cs n) (yv n) := fun n => K.ch_spec (fun c => IsC P c (yv n)) (isC_ex P (hyv n).1)
  ⟨yv, cs, fun n => (hyv n).1, hcs,
   fun n => (Nat.lt_or_ge n k).elim
    (fun h => by
      show Sim M P.U (fs n) (mixk M k cs fs n)
      rw [mixk_lt M k cs fs h]
      exact sim_C P (hcs n) ((hyv n).2 h))
    (fun h => by
      show Sim M P.U (fs n) (mixk M k cs fs n)
      rw [mixk_ge M k cs fs h]
      exact sim_refl M P.hIU (hfs n)),
   fun n => (Nat.lt_or_ge n k).elim
    (fun h => by show FnY M P.I P.Y (mixk M k cs fs n); rw [mixk_lt M k cs fs h]; exact (hcs n).1)
    (fun h => by show FnY M P.I P.Y (mixk M k cs fs n); rw [mixk_ge M k cs fs h]; exact hfs n)⟩

theorem E1 {k : Nat} {fs cs : Nat → M.D} (hfs : ∀ n, FnY M P.I P.Y (fs n))
    (sw : ∀ n, Sim M P.U (fs n) (mixk M k cs fs n)) (hws : ∀ n, FnY M P.I P.Y (mixk M k cs fs n))
    {φ : Fm} (hf : FV (k + 1) φ) {d : M.D} (hd : FnY M P.I P.Y d) : SS P φ (ext fs d) ↔ SS P φ (ext cs d) :=
  have agr : ∀ n, n < k + 1 → ext (mixk M k cs fs) d n = ext cs d n := fun n hn => match n with
    | 0 => rfl
    | _ + 1 => mixk_lt M k cs fs (Nat.lt_of_succ_lt_succ hn)
  (ss_resp P (fnY_ext P hfs hd) (fnY_ext P hws hd) (sim_ext P sw hd)).trans
    ⟨satG_agree _ _ φ (k + 1) hf agr, satG_agree _ _ φ (k + 1) hf (fun n hn => (agr n hn).symm)⟩

theorem E2 {k : Nat} {fs cs : Nat → M.D} (hfs : ∀ n, FnY M P.I P.Y (fs n))
    (sw : ∀ n, Sim M P.U (fs n) (mixk M k cs fs n)) (hws : ∀ n, FnY M P.I P.Y (mixk M k cs fs n))
    {φ : Fm} (hf : FV (k + 2) φ) {d e : M.D} (hd : FnY M P.I P.Y d) (he : FnY M P.I P.Y e) :
    SS P φ (ext (ext fs d) e) ↔ SS P φ (ext (ext cs d) e) :=
  have agr : ∀ n, n < k + 2 → ext (ext (mixk M k cs fs) d) e n = ext (ext cs d) e n := fun n hn => match n with
    | 0 => rfl
    | 1 => rfl
    | _ + 2 => mixk_lt M k cs fs (Nat.lt_of_succ_lt_succ (Nat.lt_of_succ_lt_succ hn))
  (ss_resp P (fnY_ext P (fnY_ext P hfs hd) he) (fnY_ext P (fnY_ext P hws hd) he) (sim_ext P (sim_ext P sw hd) he)).trans
    ⟨satG_agree _ _ φ (k + 2) hf agr, satG_agree _ _ φ (k + 2) hf (fun n hn => (agr n hn).symm)⟩

/-! ## finite standard sets -/

theorem fin_ae (K : CP.Classical') {z0 : M.D} (hz : FinD M z0) {y : M.D} (hy : FnY M P.I P.Y y)
    (h : AEU M P.U (fun i => ∀ a, InG M y i a → In M a z0)) :
    ∃ y0, In M y0 z0 ∧ AEU M P.U (fun i => ∀ a, InG M y i a → Ceq M.R a y0) :=
  kur_ind M (P := fun u => AEU M (u 1) (fun i => ∀ a, InG M (u 2) i a → In M a (u 0)) →
      ∃ y0, In M y0 (u 0) ∧ AEU M (u 1) (fun i => ∀ a, InG M (u 2) i a → Ceq M.R a y0))
    (def_imp M (def_AEUx M (def_all M (def_imp M (def_InG M 5 1 0) (def_mem M 0 3))) 1)
      (def_ex M (def_and M (def_mem M 0 1) (def_AEUx M (def_all M (def_imp M (def_InG M 6 1 0) (def_ceq M 0 3))) 2))))
    (fun n => match n with
      | 0 => P.U
      | _ + 1 => y) hz
    (fun _ he h' => (aeu_false M P.hU h' (fun i hi p => match hy.1 i hi with
      | ⟨a, _, ha⟩ => he a (p a ha))).elim)
    (fun u t _ _ _ ih hu' h' => match sep M (def_all M (def_imp M (def_InG M 2 1 0) (def_mem M 0 3)))
        (fun n => match n with
          | 0 => y
          | _ + 1 => u) P.I with
      | ⟨S, hS⟩ => (K.lem (AEU M P.U (fun i => ∀ a, InG M y i a → In M a u))).elim
        (fun hin => match ih hin with
          | ⟨y0, hy0, hA⟩ => ⟨y0, (hu' y0).2 (.inl hy0), hA⟩)
        (fun hnin => match sep M (def_not M (def_mem M 0 1)) (fun _ => S) P.I with
          | ⟨C, hC⟩ => (P.hult S C (fun t ht => ((hS t).1 ht).1) hC).elim
            (fun hSU => (hnin ⟨S, hSU, fun _ hi => ((hS _).1 hi).2⟩).elim)
            (fun hCU => ⟨t, (hu' t).2 (.inr (ceq_refl M.R t)), aeu_mono M P.hU (aeu_and M P.hU h' ⟨C, hCU, fun _ hi => hi⟩)
              (fun i hiI ⟨p, hiC⟩ a ha => ((hu' a).1 (p a ha)).elim
                (fun hau => (((hC i).1 hiC).2 ((hS i).2 ⟨hiI, fun a' ha' =>
                  mem_ceq_l M (hy.2 i a a' hiI ha ha') hau⟩)).elim)
                id)⟩)))
    h

/-! ## idealization -/

theorem eBG {D : Type} (R : Nat → List D → Prop) (C : D → Prop) (B : Fm) (w : Nat → D) (z x y : D) :
    SatG R C (ren (up (up Nat.succ)) B) (ext (ext (ext w z) x) y) ↔ SatG R C B (ext (ext w x) y) :=
  (satG_ren R C B _ _).trans (satG_ext R C B (fun n => match n with
    | 0 => rfl
    | 1 => rfl
    | _ + 2 => rfl))

def σd (b k : Nat) : Nat → Nat
  | 0 => 0
  | 1 => b + 1
  | n + 2 => n + 1 + k + 1

def Pid (B : Fm) (τ : Nat → M.D) (i a : M.D) : Prop :=
  ∀ y0, In M y0 (τ 0) → In M y0 i → SatG M.R (YC M (τ 0)) B (ext (ext (fun n => τ (n + 1)) a) y0)

theorem def_Pid (B : Fm) (hl : L0 B) (a b k : Nat) : Def M (fun u => Pid B (fun n => u (n + k)) (u a) (u b)) :=
  have hX : Def M (fun u => SatG M.R (YC M (u (0 + k + 1))) B (ext (ext (fun n => u (n + 1 + k + 1)) (u (b + 1))) (u 0))) :=
    def_congr M (fun u => satG_ext _ _ B (v := fun n => u (σd b k n))
        (w := ext (ext (fun n => u (n + 1 + k + 1)) (u (b + 1))) (u 0)) (fun n => match n with
          | 0 => rfl
          | 1 => rfl
          | _ + 2 => rfl))
      (def_SatY M B hl (0 + k + 1) (σd b k))
  def_congr M (fun _ => Iff.rfl) (def_all M (def_imp M (def_mem M 0 (0 + k + 1)) (def_imp M (def_mem M 0 (a + 1)) hX)))

theorem finF_S (K : CP.Classical') (hac : ACsem M)
    (hRF : ∀ v : Nat → M.D, (∀ n, In M (v n) P.Y) → (SatG M.R (YC M P.Y) IST.FinF v ↔ Sat M.R IST.FinF v))
    (fs : Nat → M.D) (hfs : ∀ n, FnY M P.I P.Y (fs n)) {z c z0 : M.D} (hz : FnY M P.I P.Y z) (hc : IsC P c z0)
    (hz0 : In M z0 P.Y) (sz : Sim M P.U z c) : SS P IST.FinF (ext fs z) ↔ FinD M z0 :=
  have hf1 : FV 1 IST.FinF := fvB_ok 1 IST.FinF rfl
  have agr : ∀ n, n < 1 → ext fs c n = (fun _ => c) n := fun n hn => match n with
    | 0 => rfl
    | _ + 1 => absurd (Nat.lt_of_succ_lt_succ hn) (Nat.not_lt_zero _)
  Iff.trans (ss_resp P (fnY_ext P hfs hz) (fnY_ext P hfs hc.1) (fun n => match n with
      | 0 => sz
      | n + 1 => sim_refl M P.hIU (hfs n)))
    (Iff.trans ⟨satG_agree _ _ IST.FinF 1 hf1 agr, satG_agree _ _ IST.FinF 1 hf1 (fun n hn => (agr n hn).symm)⟩
      (Iff.trans (losC M P.hU P.hIU K hac P.hult P.hY IST.FinF IST.l0_FinF (fun _ => c) (fun _ => z0) (fun _ => hc.1)
        (fun _ i hi => hc.2 i hi)) (hRF (fun _ => z0) (fun _ => hz0))))

theorem ideal_S (K : CP.Classical') (hac : ACsem M)
    (hRF : ∀ v : Nat → M.D, (∀ n, In M (v n) P.Y) → (SatG M.R (YC M P.Y) IST.FinF v ↔ Sat M.R IST.FinF v))
    (hfine : ∀ y, In M y P.Y → AEU M P.U (fun i => In M y i))
    (htr : ∀ x y, In M y P.Y → In M x y → In M x P.Y)
    (hI : ∀ i, In M i P.I → In M i P.Y ∧ FinD M i)
    {B : Fm} {k : Nat} (hl : L0 B) (hf : FV (k + 2) B) (fs : Nat → M.D) (hfs : ∀ n, FnY M P.I P.Y (fs n)) :
    SS P (IST.idealAx B k) fs :=
  guard_introG _ _ k _ fs (fun hg =>
    have hlψ : L0 (IST.psiI B) := ⟨l0_m 0 2, l0_ren _ B hl⟩
    have hfψ : FV (k + 1) (IST.psiI B) := ⟨⟨Nat.zero_lt_succ _, Nat.succ_lt_succ (Nat.succ_lt_succ (Nat.zero_lt_succ _)), trivial⟩,
      fv_ren B (IST.uus_lt k) hf⟩
    match cparams P K fs hfs hg with
    | ⟨yv, cs, _, hcs, sw, hws⟩ =>
    have hcsF : ∀ n, FnY M P.I P.Y (cs n) := fun n => (hcs n).1
    ⟨fun hL =>
      have wit : ∀ i, In M i P.I → ∃ a, In M a P.Y ∧ Pid B (ext yv P.Y) i a := fun i0 hi0 =>
        match isC_ex P (hI i0 hi0).1 with
        | ⟨c0, hc0⟩ =>
          have h1 : SS P (IST.psiI B) (ext fs c0) := hL c0 hc0.1 (stu_const P hc0 (hI i0 hi0).1)
            ((finF_S P K hac hRF fs hfs hc0.1 hc0 (hI i0 hi0).1 (sim_refl M P.hIU hc0.1)).2 (hI i0 hi0).2)
          have h2 : SatG M.R (YC M P.Y) (IST.psiI B) (ext yv i0) :=
            (losC M P.hU P.hIU K hac P.hult P.hY (IST.psiI B) hlψ (ext cs c0) (ext yv i0) (fnY_ext P hcsF hc0.1)
              (fun n i hi => match n with
                | 0 => hc0.2 i hi
                | n + 1 => (hcs n).2 i hi)).1 ((E1 P hfs sw hws hfψ hc0.1).1 h1)
          match h2 with
          | ⟨a, ha, h3⟩ => ⟨a, ha, fun y0 hy0 hy0i => (eBG M.R _ B yv i0 a y0).1 (h3 y0 hy0 hy0i)⟩
      match choice_fam M (Pid B) (def_Pid B hl) K hac (ext yv P.Y) P.I P.Y wit with
      | ⟨f, hfF, hfP⟩ => ⟨f, hfF, fun _ hy hsty => match std_sim P hsty with
        | ⟨y0, cy, hy0, hcy, sy⟩ =>
          have hext : ∀ n, FnY M P.I P.Y (ext (ext cs f) cy n) := fnY_ext P (fnY_ext P hcsF hfF) hcy.1
          (ss_resp P (fnY_ext P (fnY_ext P hfs hfF) hcy.1) (fnY_ext P (fnY_ext P hfs hfF) hy) (fun n => match n with
              | 0 => sim_symm M P.hU sy
              | n + 1 => sim_ext P (fun m => sim_refl M P.hIU (hfs m)) hfF n)).1
            ((E2 P hfs sw hws hf hfF hcy.1).2
              ((los M K hac P.hU P.hult P.hY B hl (ext (ext cs f) cy) hext).2
                (aeu_mono M P.hU (hfine y0 hy0) (fun i hi hyi =>
                  have exv : ∃ a, In M a P.Y ∧ InG M f i a := hfF.1 i hi
                  let ai := K.ch (fun a => In M a P.Y ∧ InG M f i a) exv
                  have hai := K.ch_spec (fun a => In M a P.Y ∧ InG M f i a) exv
                  (pt_val M (τ := vecI3 M i P.I P.Y) hi B hl (ext (ext cs f) cy) hext (ext (ext yv ai) y0)
                    (fun n => match n with
                      | 0 => hcy.2 i hi
                      | 1 => hai.2
                      | n + 2 => (hcs n).2 i hi)).2 (hfP i ai hi hai.2 y0 hy0 hyi)))))⟩,
     fun ⟨x, hx, hR⟩ z hz hstz hfinz => match std_sim P hstz with
      | ⟨z0, _, hz0, hcz, sz⟩ =>
        have hfin0 : FinD M z0 := (finF_S P K hac hRF fs hfs hz hcz hz0 sz).1 hfinz
        ⟨x, hx, fun y hy hyz =>
          have hyc : AEU M P.U (fun i => ∀ a, InG M y i a → In M a z0) :=
            aeu_mono M P.hU (eu_resp M P.hU hy hz (sim_refl M P.hIU hy) sz hyz)
              (fun i hi p a ha => p a z0 ha (hcz.2 i hi))
          match fin_ae P K hfin0 hy hyc with
          | ⟨y0, hy0, hA⟩ => (eBG _ _ B fs z x y).2 (hR y hy ⟨y0, htr y0 z0 hz0 hy0, hA⟩)⟩⟩)

end IZ

end Found

#print axioms Found.IZ.cparams
#print axioms Found.IZ.fin_ae
#print axioms Found.IZ.def_Pid
#print axioms Found.IZ.finF_S
#print axioms Found.IZ.ideal_S

found/IZ21.lean

lean_all/found/IZ21.lean

/-
  T165 (general), part G7. found/IZ21 — Nelson's IST is conservative over
  ZFC: if IST proves an internal sentence φ, then ZFC proves φ.
  In any model of ZFC: reflect φ, FinF, the closure formula below and the
  finitely many internal axioms used to a level Y = V_α (found/IZ10);
  I = finite members of Y, an ultrafilter U ⊇ the fine base (found/IZ13,
  IZ14); the functions I → Y modulo U (found/IZ17–20) satisfy every IST
  axiom used, hence φ; by Łoś and reflection φ holds in the model;
  completeness (found/FOClassicalT) gives a ZFC proof.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.IZ20
import found.Ist5

namespace Found

namespace IZ

open FO FOCT NBGc RF AK

theorem zf01 {φ : Fm} (h : ZF B0 φ) : ZF B1 φ :=
  match h with
  | .inl e => .inl e
  | .inr (.inl hb) => .inr (.inl (.inl hb))
  | .inr (.inr r) => .inr (.inr r)

/-- ∀a ∀b ∃d (a ⊆ d ∧ b ∈ d) -/
def adjF : Fm := .all (.all (.ex (.conj (.all (.imp (m 0 3) (m 0 1))) (m 1 0))))

theorem l0_adjF : L0 adjF := ⟨fun_l0, l0_m 1 0⟩
where fun_l0 : L0 (.all (.imp (m 0 3) (m 0 1))) := ⟨l0_m 0 3, l0_m 0 1⟩

def ZMof {D : Type} (R : Nat → List D → Prop) (hM : ModT (ZF B1) R) (d : D) : ZM :=
  ⟨D, R, fun φ h => hM φ (zf01 h), d⟩

theorem ist_zfc (K : CP.Classical') {φ : Fm} (hl : L0 φ) (hf : FV 0 φ) (h : PC IST.Ist [] φ) :
    PC (ZF B1) [] φ :=
  completenessT (ZF B1) K (fun D R v hM _ => by
    let M : ZM := ZMof R hM (v 0)
    have hac : ACsem M := ac_sem M (hM acAx (.inr (.inl (.inr rfl))))
    match h with
    | ⟨Δ, cΔ, d⟩ =>
    match IST.extractI Δ cΔ with
    | ⟨L, hL, hΔ⟩ =>
    have hΦ : ∀ ψ, ψ ∈ φ :: IST.FinF :: adjF :: L → L0 ψ := fun ψ m => by
      cases m with
      | head => exact hl
      | tail _ m' =>
        cases m' with
        | head => exact IST.l0_FinF
        | tail _ m'' =>
          cases m'' with
          | head => exact l0_adjF
          | tail _ m3 => exact IST.zf_l0 (hL ψ m3)
    match reflection_D M K (φ :: IST.FinF :: adjF :: L) hΦ with
    | ⟨α, Y, hαo, hYv, hEY, hR⟩ =>
    have htr : ∀ x y, In M y Y → In M x y → In M x Y := fun x y hy hxy => vl_trans M K hαo hYv y x hy hxy
    have hsub : ∀ x z, In M z Y → SubS M x z → In M x Y := fun _ _ hz hs => vl_sub M K hYv hz hs
    match hEY with
    | ⟨e0, he0⟩ =>
    have hE : ∃ e, In M e Y ∧ ∀ s, ¬ In M s e := match sep M (def_false M) (fun _ => e0) e0 with
      | ⟨e, he⟩ => ⟨e, hsub e e0 he0 (fun s hs => ((he s).1 hs).1), fun s hs => ((he s).1 hs).2⟩
    have hadj : ∀ a b, In M a Y → In M b Y → ∃ d, In M d Y ∧ SubS M a d ∧ In M b d := fun a b ha hb =>
      have s1 : Sat M.R adjF (fun _ => e0) := fun a' b' => match adj_exists M a' b' with
        | ⟨c, hc⟩ => ⟨c, fun s hs => (hc s).2 (.inl hs), (hc b').2 (.inr (ceq_refl M.R b'))⟩
      have s2 := (hR adjF (.tail _ (.tail _ (.head _))) (fun _ => e0) (fun _ => he0)).2 s1
      match s2 a ha b hb with
      | ⟨d, hd, hsd, hbd⟩ => ⟨d, hd, fun s hs => hsd s (htr s a ha hs) hs, hbd⟩
    have hA : ∀ u t, In M u Y → In M t Y → ∃ c, In M c Y ∧ ∀ s, In M s c ↔ In M s u ∨ Ceq M.R s t :=
      fun u t hu ht => match hadj u t hu ht, adj_exists M u t with
        | ⟨d, hd, hud, htd⟩, ⟨c, hc⟩ => ⟨c, hsub c d hd (fun s hs => ((hc s).1 hs).elim (hud s)
            (fun e => mem_ceq_l M (ceq_symm M.R e) htd)), hc⟩
    match sep M (def_FinD M 0) (fun _ => e0) Y with
    | ⟨I, hI⟩ =>
    match power_ex M I with
    | ⟨PI, hPI⟩ =>
    match sep M (P := fun u => ∃ F, In M F (u 1) ∧ ∀ i, In M i (u 0) ↔ In M i (u 1) ∧ SubS M F i)
        (def_ex M (def_and M (def_mem M 0 2) (def_all M (def_iff M (def_mem M 0 2)
          (def_and M (def_mem M 0 3) (def_SubS M 1 0)))))) (fun _ => I) PI with
    | ⟨F0, hF0'⟩ =>
    have hF0 : ∀ A, In M A F0 ↔ SubS M A I ∧ ∃ F, In M F I ∧ ∀ i, In M i A ↔ In M i I ∧ SubS M F i := fun A =>
      (hF0' A).trans ⟨fun ⟨h1, h2⟩ => ⟨(hPI A).1 h1, h2⟩, fun ⟨h1, h2⟩ => ⟨(hPI A).2 h1, h2⟩⟩
    have hbase := fine_base M htr hE hA hI hF0
    match ultra_ext M K hac hbase with
    | ⟨U, hPF, hult⟩ =>
    have hIU : In M I U := match hbase.2.1 with
      | ⟨A0, hA0⟩ => hPF.2.2.2.1 A0 I (hPF.2.1 A0 hA0) (fun _ h => h) (hbase.1 A0 hA0)
    let P : UP M := ⟨I, Y, U, F0, hPF, hIU, hult, ⟨e0, he0⟩, hsub⟩
    have hfine : ∀ y, In M y Y → AEU M U (fun i => In M y i) := fun y hy =>
      match sep M (def_mem M 1 0) (fun _ => y) I with
      | ⟨A, hAs⟩ => ⟨A, fine_mem M hE hA hI hF0 hPF hy hAs, fun i hi => ((hAs i).1 hi).2⟩
    have hRF : ∀ w : Nat → M.D, (∀ n, In M (w n) Y) → (SatG M.R (YC M Y) IST.FinF w ↔ Sat M.R IST.FinF w) :=
      fun w hw => hR IST.FinF (.tail _ (.head _)) w hw
    have hI' : ∀ i, In M i I → In M i Y ∧ FinD M i := fun i hi => (hI i).1 hi
    -- the ultrapower structure
    let C : M.D → Prop := FnY M I Y
    let Rst := Rsub (RSt M U Y) C
    have vals : ∀ (fs : Nat → M.D), (∀ n, C (fs n)) → ∀ i, In M i I →
        ∃ vv : Nat → M.D, (∀ n, In M (vv n) Y) ∧ ∀ n, InG M (fs n) i (vv n) := fun fs hfs i hi =>
      ⟨fun n => K.ch (fun a => In M a Y ∧ InG M (fs n) i a) ((hfs n).1 i hi),
       fun n => (K.ch_spec (fun a => In M a Y ∧ InG M (fs n) i a) ((hfs n).1 i hi)).1,
       fun n => (K.ch_spec (fun a => In M a Y ∧ InG M (fs n) i a) ((hfs n).1 i hi)).2⟩
    have hb : ∀ b, IST.IstIn L b → ∀ w : Nat → {x // C x}, Sat Rst b w := fun b hb w =>
      have hfs : ∀ n, C ((fun n => (w n).1) n) := fun n => (w n).2
      (sat_sub _ C b w).2 (match hb with
        | .inl ⟨hz, m⟩ =>
          (los M K hac hPF hult ⟨e0, he0⟩ b (IST.zf_l0 hz) _ hfs).2 ⟨I, hIU, fun i hi =>
            match vals _ hfs i hi with
            | ⟨vv, hvY, hvv⟩ => (pt_val M (τ := vecI3 M i I Y) hi b (IST.zf_l0 hz) _ hfs vv hvv).2
                ((hR b (.tail _ (.tail _ (.tail _ m))) vv hvY).2 (hM b hz vv))⟩
        | .inr (.inl ⟨θ, k, h1, h2, e⟩) => by rw [e]; exact trans_S P K hac h1 h2 _ hfs
        | .inr (.inr (.inl ⟨B, k, h1, h2, e⟩)) => by rw [e]; exact ideal_S P K hac hRF hfine htr hI' h1 h2 _ hfs
        | .inr (.inr (.inr ⟨Φ', k, _, e⟩)) => by rw [e]; exact std_S P Φ' k _ hfs)
    have hmod : ∀ δ, δ ∈ Δ → ∀ w, Sat Rst δ w := fun δ m w => match hΔ δ m with
      | ⟨j, a, ha, e⟩ => by
        rw [e]
        exact clo_sat _ (fun w' => match ha with
          | .inl ⟨b, eb⟩ => by rw [eb]; exact K.lem (Sat Rst b w')
          | .inr ⟨b, σ, hbb, eb⟩ => by rw [eb]; exact (sat_ren _ b σ w').2 (hb b hbb _)) j w
    match isC_ex P he0 with
    | ⟨c0, hc0⟩ =>
    let w0 : Nat → {x // C x} := fun _ => ⟨c0, hc0.1⟩
    have s : Sat Rst φ w0 := sound Rst d w0 (fun γ m =>
      (mem_app_inv' Δ [] m).elim (fun mΔ => hmod γ mΔ w0) (fun m' => nomatch m'))
    have s1 : SatG M.R (YC M Y) φ (fun _ => e0) :=
      (losC M hPF hIU K hac hult ⟨e0, he0⟩ φ hl (fun _ => c0) (fun _ => e0) (fun _ => hc0.1)
        (fun _ i hi => hc0.2 i hi)).1 ((sat_sub _ C φ w0).1 s)
    have s2 : Sat M.R φ (fun _ => e0) := (hR φ (.head _) (fun _ => e0) (fun _ => he0)).1 s1
    exact sat_agreeU R φ 0 hf (fun n hn => absurd hn (Nat.not_lt_zero n)) s2)

end IZ

end Found

#print axioms Found.IZ.zf01
#print axioms Found.IZ.ist_zfc

found/IZ3.lean

lean_all/found/IZ3.lean

/-
  Shared tool R3, part 3. found/IZ3 — pairs and ordinal arithmetic inside a
  model of ZF:
  * the Kuratowski pair is definable, respects coextension, exists, and is
    determined by its components (up to coextension);
  * singletons, binary unions, successors x ∪ {x};
  * the successor of an ordinal and the union of a set of ordinals are
    ordinals; for ordinals, inclusion is membership or coextension.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.IZ2

namespace Found

namespace IZ

open FO FOCT NBGc

variable (M : ZM)

/-! ## pairs -/

theorem def_op (i j k : Nat) : Def M (fun v => OPs M.R (v i) (v j) (v k)) :=
  ⟨ren (opσ i j k) OPz, l0_ren _ OPz l0_OPz, fun v => (sat_ren M.R OPz _ v).trans
    (sat_ext M.R OPz (fun n => match n with
      | 0 => rfl
      | 1 => rfl
      | 2 => rfl
      | _ + 3 => rfl))⟩

theorem op_resp {p a b p' a' b' : M.D} (h : OPs M.R p a b) (ep : Ceq M.R p p') (ea : Ceq M.R a a')
    (eb : Ceq M.R b b') : OPs M.R p' a' b' :=
  def_resp M (def_op M 0 1 2) (v := tri p a b) (w := tri p' a' b') (fun n => match n with
    | 0 => ep
    | 1 => ea
    | 2 => eb
    | _ + 3 => ep) h

theorem op_uniq {p p' a b : M.D} (h1 : OPs M.R p a b) (h2 : OPs M.R p' a b) : Ceq M.R p p' := fun u =>
  ((op_sem M.R p a b).1 h1 u).trans ((op_sem M.R p' a b).1 h2 u).symm

theorem op_exists (a b : M.D) : ∃ p, OPs M.R p a b := op_ex M.R (pairing M) a b

theorem op_inj' {p a b a' b' : M.D} (h1 : OPs M.R p a b) (h2 : OPs M.R p a' b') :
    Ceq M.R a a' ∧ Ceq M.R b b' := op_inj M.R (pairing M) h1 h2

/-! ## small sets -/

theorem sing_exists (a : M.D) : ∃ c, ∀ s, In M s c ↔ Ceq M.R s a :=
  match pairing M a a with
  | ⟨c, hc⟩ => ⟨c, fun s => ⟨fun h => ((hc s).1 h).elim id id, fun h => (hc s).2 (.inl h)⟩⟩

theorem union2_exists (a b : M.D) : ∃ c, ∀ s, In M s c ↔ (In M s a ∨ In M s b) :=
  match pairing M a b with
  | ⟨p, hp⟩ => match union_ex M p with
    | ⟨c, hc⟩ => ⟨c, fun s =>
      ⟨fun h => match (hc s).1 h with
        | ⟨t, ht, hst⟩ => ((hp t).1 ht).elim (fun e => .inl ((e s).1 hst)) (fun e => .inr ((e s).1 hst)),
       fun h => h.elim (fun ha => (hc s).2 ⟨a, (hp a).2 (.inl (ceq_refl M.R a)), ha⟩)
          (fun hb => (hc s).2 ⟨b, (hp b).2 (.inr (ceq_refl M.R b)), hb⟩)⟩⟩

theorem succ_exists (x : M.D) : ∃ c, ∀ s, In M s c ↔ (In M s x ∨ Ceq M.R s x) :=
  match sing_exists M x with
  | ⟨sx, hsx⟩ => match union2_exists M x sx with
    | ⟨c, hc⟩ => ⟨c, fun s => (hc s).trans ⟨fun h => h.elim .inl (fun h' => .inr ((hsx s).1 h')),
        fun h => h.elim .inl (fun h' => .inr ((hsx s).2 h'))⟩⟩

/-! ## ordinals -/

theorem ord_succ {x c : M.D} (hx : Ord M x) (hc : ∀ s, In M s c ↔ (In M s x ∨ Ceq M.R s x)) : Ord M c :=
  ⟨fun y z hy hz => (hc z).2 (.inl (((hc y).1 hy).elim (fun h => hx.1 y z h hz)
      (fun e => mem_ceq_r M e hz))),
   fun y hy => ((hc y).1 hy).elim (fun h => hx.2 y h) (fun e => trans_resp M (ceq_symm M.R e) hx.1)⟩

theorem ord_union {A U : M.D} (hA : ∀ y, In M y A → Ord M y) (hU : ∀ s, In M s U ↔ ∃ t, In M t A ∧ In M s t) :
    Ord M U :=
  ⟨fun y z hy hz => match (hU y).1 hy with
    | ⟨t, ht, hyt⟩ => (hU z).2 ⟨t, ht, (hA t ht).1 y z hyt hz⟩,
   fun y hy => match (hU y).1 hy with
    | ⟨t, ht, hyt⟩ => (hA t ht).2 y hyt⟩

/-- for ordinals, inclusion is membership or coextension -/
theorem ord_sub (K : CP.Classical') {a b : M.D} (ha : Ord M a) (hb : Ord M b) (hs : ∀ s, In M s a → In M s b) :
    In M a b ∨ Ceq M.R a b :=
  match ord_cmp M K ha hb with
  | .inl h => .inl h
  | .inr (.inl e) => .inr e
  | .inr (.inr h) => (not_mem_self M b (hs b h)).elim

/-- membership in an ordinal gives inclusion -/
theorem ord_mem_sub {a b : M.D} (hb : Ord M b) (h : In M a b) : ∀ s, In M s a → In M s b :=
  fun s hs => hb.1 a s h hs

end IZ

end Found

#print axioms Found.IZ.def_op
#print axioms Found.IZ.op_resp
#print axioms Found.IZ.op_uniq
#print axioms Found.IZ.union2_exists
#print axioms Found.IZ.succ_exists
#print axioms Found.IZ.ord_succ
#print axioms Found.IZ.ord_union
#print axioms Found.IZ.ord_sub

found/IZ4.lean

lean_all/found/IZ4.lean

/-
  Shared tool R3, part 4. found/IZ4 — transfinite recursion inside a model
  of ZF, for a definable step relation G(y, β, g) ("y is the value at β when
  the earlier values are those of the set-function g"):
  * set-functions: InG g β y iff some ordered pair ⟨β, y⟩ is a member of g;
  * an attempt on an ordinal δ: a set-function defined exactly on δ,
    single-valued, whose value at every β ∈ δ satisfies the step;
  * uniqueness: two attempts agree where both are defined (least
    counterexample);
  * existence: every ordinal lies in the domain of an attempt (least
    ordinal without one; the earlier values are collected by replacement and
    the step adds the value at that ordinal).
  The step must be local (depend on g only below β), functional and total.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.IZ3

namespace Found

namespace IZ

open FO FOCT NBGc

variable (M : ZM)

def InG (g b y : M.D) : Prop := ∃ p, In M p g ∧ OPs M.R p b y

theorem def_InG (i j k : Nat) : Def M (fun v => InG M (v i) (v j) (v k)) :=
  def_congr M (fun _ => Iff.rfl) (def_ex M (def_and M (def_mem M 0 (i + 1)) (def_op M 0 (j + 1) (k + 1))))

theorem InG_resp {g b y g' b' y' : M.D} (eg : Ceq M.R g g') (eb : Ceq M.R b b') (ey : Ceq M.R y y')
    (h : InG M g b y) : InG M g' b' y' :=
  match h with
  | ⟨p, hp, ho⟩ => ⟨p, mem_ceq_r M eg hp, op_resp M ho (ceq_refl M.R p) eb ey⟩

/-- a recursion step -/
structure Step where
  G : M.D → M.D → M.D → Prop
  dG : Def M (fun v => G (v 0) (v 1) (v 2))
  loc : ∀ g g' b y, (∀ c, In M c b → ∀ z, InG M g c z ↔ InG M g' c z) → G y b g → G y b g'
  fn : ∀ g b y y', G y b g → G y' b g → Ceq M.R y y'
  ex : ∀ g b, Ord M b → (∀ c, In M c b → ∃ z, InG M g c z) → ∃ y, G y b g

variable {M} (S : Step M)

theorem G_resp {y b g y' b' g' : M.D} (ey : Ceq M.R y y') (eb : Ceq M.R b b') (eg : Ceq M.R g g')
    (h : S.G y b g) : S.G y' b' g' :=
  def_resp M S.dG (v := tri y b g) (w := tri y' b' g') (fun n => match n with
    | 0 => ey
    | 1 => eb
    | 2 => eg
    | _ + 3 => ey) h

/-- an attempt on δ -/
def Att (g d : M.D) : Prop :=
  Ord M d ∧ (∀ c, In M c d → ∃ z, InG M g c z) ∧ (∀ c z, InG M g c z → In M c d) ∧
    (∀ c z z', InG M g c z → InG M g c z' → Ceq M.R z z') ∧ (∀ c z, In M c d → InG M g c z → S.G z c g)

def σ3 (i j k : Nat) : Nat → Nat
  | 0 => i
  | 1 => j
  | 2 => k
  | n + 3 => n

theorem def_G (i j k : Nat) : Def M (fun v => S.G (v i) (v j) (v k)) :=
  def_congr M (fun _ => Iff.rfl) (def_ren M (σ3 i j k) S.dG)

theorem def_Att (i j : Nat) : Def M (fun v => Att S (v i) (v j)) :=
  def_and M (def_Ord M j)
    (def_and M (def_congr M (fun _ => Iff.rfl) (def_all M (def_imp M (def_mem M 0 (j + 1))
        (def_ex M (def_InG M (i + 2) 1 0)))))
      (def_and M (def_congr M (fun _ => Iff.rfl) (def_all M (def_all M (def_imp M (def_InG M (i + 2) 1 0)
          (def_mem M 1 (j + 2))))))
        (def_and M (def_congr M (fun _ => Iff.rfl) (def_all M (def_all M (def_all M (def_imp M
            (def_InG M (i + 3) 2 1) (def_imp M (def_InG M (i + 3) 2 0) (def_ceq M 1 0)))))))
          (def_congr M (fun _ => Iff.rfl) (def_all M (def_all M (def_imp M (def_mem M 1 (j + 2))
            (def_imp M (def_InG M (i + 2) 1 0) (def_G S 0 1 (i + 2))))))))))

/-! ## uniqueness -/

theorem att_uniq (K : CP.Classical') {g d g' d' : M.D} (ha : Att S g d) (ha' : Att S g' d') :
    ∀ b, In M b d → In M b d' → ∀ z z', InG M g b z → InG M g' b z' → Ceq M.R z z' := by
  let Bad : (Nat → M.D) → Prop := fun v =>
    In M (v 0) (v 2) ∧ In M (v 0) (v 4) ∧ ∃ z z', InG M (v 1) (v 0) z ∧ InG M (v 3) (v 0) z' ∧ ¬ Ceq M.R z z'
  have hBad : Def M Bad := def_and M (def_mem M 0 2) (def_and M (def_mem M 0 4)
    (def_congr M (fun _ => Iff.rfl) (def_ex M (def_ex M (def_and M (def_InG M 3 2 1)
      (def_and M (def_InG M 5 2 0) (def_not M (def_ceq M 1 0))))))))
  let par : Nat → M.D := fun n => match n with
    | 0 => g
    | 1 => d
    | 2 => g'
    | 3 => d'
    | _ => g
  intro b hb hb' z z' hz hz'
  refine CP.dne K _ (fun hn => ?_)
  match ord_least M K hBad par ⟨b, ord_mem M ha.1 hb, hb, hb', z, z', hz, hz', hn⟩ with
  | ⟨c, _, ⟨hc, hc', w, w', hw, hw', hnw⟩, cmin⟩ =>
    -- below c, g and g' agree
    have agree : ∀ e, In M e c → ∀ u, InG M g e u ↔ InG M g' e u := fun e he u =>
      have hed : In M e d := ha.1.1 c e hc he
      have hed' : In M e d' := ha'.1.1 c e hc' he
      have eo : Ord M e := ord_mem M ha.1 hed
      have noBad : ∀ u u', InG M g e u → InG M g' e u' → Ceq M.R u u' := fun u u' h1 h2 =>
        CP.dne K _ (fun hne => (cmin e eo ⟨hed, hed', u, u', h1, h2, hne⟩).elim
          (fun hce => not_mem_self M c ((ord_mem M ha.1 hc).1 e c he hce))
          (fun ece => not_mem_self M e (mem_ceq_r M ece he)))
      ⟨fun h1 => match ha'.2.1 e hed' with
        | ⟨u', h2⟩ => InG_resp M (ceq_refl M.R _) (ceq_refl M.R _) (ceq_symm M.R (noBad u u' h1 h2)) h2,
       fun h2 => match ha.2.1 e hed with
        | ⟨u', h1⟩ => InG_resp M (ceq_refl M.R _) (ceq_refl M.R _) (noBad u' u h1 h2) h1⟩
    have hg1 : S.G w c g := ha.2.2.2.2 c w hc hw
    have hg2 : S.G w' c g' := ha'.2.2.2.2 c w' hc' hw'
    exact hnw (S.fn g' c w w' (S.loc g g' c w agree hg1) hg2)

/-! ## existence -/

/-- the value at b, from some attempt -/
def Val (b z : M.D) : Prop := ∃ g d, Att S g d ∧ In M b d ∧ InG M g b z

theorem val_uniq (K : CP.Classical') {b z z' : M.D} (h1 : Val S b z) (h2 : Val S b z') : Ceq M.R z z' :=
  match h1, h2 with
  | ⟨_, _, ha, hb, hz⟩, ⟨_, _, ha', hb', hz'⟩ => att_uniq S K ha ha' b hb hb' z z' hz hz'

theorem att_exists (K : CP.Classical') : ∀ a, Ord M a → ∃ g d, Att S g d ∧ In M a d := by
  intro a0' ha0'
  let Lack : (Nat → M.D) → Prop := fun v => ¬ ∃ g d, Att S g d ∧ In M (v 0) d
  have hLack : Def M Lack := def_not M (def_congr M (fun _ => Iff.rfl)
    (def_ex M (def_ex M (def_and M (def_Att S 1 0) (def_mem M 2 0)))))
  refine CP.dne K _ (fun hn => ?_)
  match ord_least M K hLack (fun _ => M.d0) ⟨a0', ha0', hn⟩ with
  | ⟨a, ha, hLa, amin⟩ =>
    have hasAtt : ∀ b, In M b a → ∃ g d, Att S g d ∧ In M b d := fun b hb =>
      CP.dne K _ (fun hnb => (amin b (ord_mem M ha hb) hnb).elim (fun h => not_mem_self M a (ha.1 b a hb h))
        (fun e => not_mem_self M b (mem_ceq_r M e hb)))
    have hasVal : ∀ b, In M b a → ∃ z, Val S b z := fun b hb => match hasAtt b hb with
      | ⟨g, d, hat, hbd⟩ => match hat.2.1 b hbd with
        | ⟨z, hz⟩ => ⟨z, g, d, hat, hbd, hz⟩
    -- collect the pairs ⟨b, value at b⟩, b ∈ a
    let Θ : (Nat → M.D) → Prop := fun u => ∃ y, Val S (u 0) y ∧ OPs M.R (u 1) (u 0) y
    have hΘ : Def M Θ := def_congr M (fun _ => Iff.rfl) (def_ex M (def_and M
      (def_congr M (fun _ => Iff.rfl) (def_ex M (def_ex M (def_and M (def_Att S 1 0)
        (def_and M (def_mem M 3 0) (def_InG M 1 3 2))))))
      (def_op M 2 1 0)))
    match repl M hΘ (fun _ => M.d0) a (fun w _ z z' ⟨y, hy, hoy⟩ ⟨y', hy', hoy'⟩ =>
        op_uniq M hoy (op_resp M hoy' (ceq_refl M.R _) (ceq_refl M.R _) (ceq_symm M.R (val_uniq S K hy hy')))) with
    | ⟨G0, hG0⟩ =>
      have G0_in : ∀ c z, InG M G0 c z → ∃ b, In M b a ∧ Ceq M.R c b ∧ ∃ y, Val S b y ∧ Ceq M.R z y :=
        fun c z ⟨p, hp, ho⟩ => match (hG0 p).1 hp with
          | ⟨b, hb, y, hy, hoy⟩ =>
            have e := op_inj' M ho hoy
            ⟨b, hb, e.1, y, hy, e.2⟩
      have G0_of : ∀ c z, In M c a → Val S c z → InG M G0 c z := fun c z hc hz =>
        match op_exists M c z with
        | ⟨p, hp⟩ => ⟨p, (hG0 p).2 ⟨c, hc, z, hz, hp⟩, hp⟩
      have G0_tot : ∀ c, In M c a → ∃ z, InG M G0 c z := fun c hc => match hasVal c hc with
        | ⟨z, hz⟩ => ⟨z, G0_of c z hc hz⟩
      -- G0 agrees with an attempt below a point of both domains
      have agreeAtt : ∀ g d b, Att S g d → In M b d → In M b a → ∀ c, In M c b → ∀ z,
          InG M g c z ↔ InG M G0 c z := fun g d b hat hbd hba c hc z =>
        have hcd : In M c d := hat.1.1 b c hbd hc
        have hca : In M c a := ha.1 b c hba hc
        ⟨fun h => G0_of c z hca ⟨g, d, hat, hcd, h⟩,
         fun h => match G0_in c z h with
          | ⟨b', _, ecb, y, hy, ezy⟩ => match hat.2.1 c hcd with
            | ⟨u, hu⟩ =>
              have e1 : Ceq M.R u y := val_uniq S K ⟨g, d, hat, mem_ceq_l M ecb hcd,
                InG_resp M (ceq_refl M.R _) ecb (ceq_refl M.R _) hu⟩ hy
              InG_resp M (ceq_refl M.R _) (ceq_refl M.R _) (ceq_trans M.R e1 (ceq_symm M.R ezy)) hu⟩
      match S.ex G0 a ha G0_tot with
      | ⟨y0, hy0⟩ => match op_exists M a y0 with
        | ⟨p0, hp0⟩ => match sing_exists M p0 with
          | ⟨s0, hs0⟩ => match union2_exists M G0 s0 with
            | ⟨g, hg⟩ => match succ_exists M a with
              | ⟨d, hd⟩ =>
                -- entries of g
                have g_cases : ∀ c z, InG M g c z → InG M G0 c z ∨ (Ceq M.R c a ∧ Ceq M.R z y0) :=
                  fun c z ⟨p, hp, ho⟩ => ((hg p).1 hp).elim (fun h => .inl ⟨p, h, ho⟩)
                    (fun h => .inr (op_inj' M (op_resp M ho ((hs0 p).1 h) (ceq_refl M.R _) (ceq_refl M.R _)) hp0))
                have g_of : ∀ c z, InG M G0 c z → InG M g c z := fun c z ⟨p, hp, ho⟩ => ⟨p, (hg p).2 (.inl hp), ho⟩
                have g_top : InG M g a y0 := ⟨p0, (hg p0).2 (.inr ((hs0 p0).2 (ceq_refl M.R _))), hp0⟩
                have not_top : ∀ c z, InG M G0 c z → ¬ Ceq M.R c a := fun c z h e => match G0_in c z h with
                  | ⟨b, hb, ecb, _, _, _⟩ => not_mem_self M a (mem_ceq_l M (ceq_trans M.R (ceq_symm M.R ecb) e) hb)
                have agreeTop : ∀ c, In M c a → ∀ z, InG M g c z ↔ InG M G0 c z := fun c hc z =>
                  ⟨fun h => (g_cases c z h).elim id (fun ⟨e, _⟩ => (not_mem_self M a (mem_ceq_l M e hc)).elim),
                   g_of c z⟩
                have hatt : Att S g d := ⟨ord_succ M ha hd,
                  fun c hc => ((hd c).1 hc).elim (fun h => match G0_tot c h with
                      | ⟨z, hz⟩ => ⟨z, g_of c z hz⟩)
                    (fun e => ⟨y0, InG_resp M (ceq_refl M.R _) (ceq_symm M.R e) (ceq_refl M.R _) g_top⟩),
                  fun c z h => (g_cases c z h).elim (fun h' => match G0_in c z h' with
                      | ⟨b, hb, ecb, _, _, _⟩ => (hd c).2 (.inl (mem_ceq_l M (ceq_symm M.R ecb) hb)))
                    (fun ⟨e, _⟩ => (hd c).2 (.inr e)),
                  fun c z z' h1 h2 => match g_cases c z h1, g_cases c z' h2 with
                    | .inl h1', .inl h2' => match G0_in c z h1', G0_in c z' h2' with
                      | ⟨b, _, ecb, y, hy, ezy⟩, ⟨b', _, ecb', y', hy', ezy'⟩ =>
                        have hy2 : Val S b' y := match hy with
                          | ⟨g1, d1, a1, b1, i1⟩ => ⟨g1, d1, a1, mem_ceq_l M (ceq_trans M.R (ceq_symm M.R ecb) ecb') b1,
                              InG_resp M (ceq_refl M.R _) (ceq_trans M.R (ceq_symm M.R ecb) ecb') (ceq_refl M.R _) i1⟩
                        ceq_trans M.R ezy (ceq_trans M.R (val_uniq S K hy2 hy') (ceq_symm M.R ezy'))
                    | .inl h1', .inr ⟨e, _⟩ => (not_top c z h1' e).elim
                    | .inr ⟨e, _⟩, .inl h2' => (not_top c z' h2' e).elim
                    | .inr ⟨_, e1⟩, .inr ⟨_, e2⟩ => ceq_trans M.R e1 (ceq_symm M.R e2),
                  fun c z hc h => ((hd c).1 hc).elim
                    (fun hca => match g_cases c z h with
                      | .inl h' => match G0_in c z h' with
                        | ⟨b, hb, ecb, y, ⟨g1, d1, a1, b1, i1⟩, ezy⟩ =>
                          -- the value from attempt g1 satisfies the step there
                          have st : S.G y b g1 := a1.2.2.2.2 b y b1 i1
                          have ag : ∀ e, In M e b → ∀ u, InG M g1 e u ↔ InG M g e u := fun e he u =>
                            (agreeAtt g1 d1 b a1 b1 hb e he u).trans
                              (agreeTop e (ha.1 b e hb he) u).symm
                          G_resp S (ceq_symm M.R ezy) (ceq_symm M.R ecb) (ceq_refl M.R _) (S.loc g1 g b y ag st)
                      | .inr ⟨e, _⟩ => (not_mem_self M a (mem_ceq_l M e hca)).elim)
                    (fun eca => match g_cases c z h with
                      | .inl h' => (not_top c z h' eca).elim
                      | .inr ⟨_, ez⟩ =>
                        G_resp S (ceq_symm M.R ez) (ceq_symm M.R eca) (ceq_refl M.R _)
                          (S.loc G0 g a y0 (fun e he u => (agreeTop e he u).symm) hy0))⟩
                exact hLa ⟨g, d, hatt, (hd a).2 (.inr (ceq_refl M.R a))⟩

/-- the value of the recursion at every ordinal -/
theorem val_exists (K : CP.Classical') {a : M.D} (ha : Ord M a) : ∃ z, Val S a z :=
  match att_exists S K a ha with
  | ⟨g, d, hat, had⟩ => match hat.2.1 a had with
    | ⟨z, hz⟩ => ⟨z, g, d, hat, had, hz⟩

end IZ

end Found

#print axioms Found.IZ.def_Att
#print axioms Found.IZ.att_uniq
#print axioms Found.IZ.att_exists
#print axioms Found.IZ.val_exists

found/IZ4p.lean

lean_all/found/IZ4p.lean

/-
  Shared tool R3, part 4p. found/IZ4p — transfinite recursion inside a
  model of ZF with parameters: the step G(y, β, g, ρ) may use a valuation ρ
  of parameters (definable with the parameters at positions 3, 4, …):
  * set-functions: InG g β y iff some ordered pair ⟨β, y⟩ is a member of g;
  * an attempt on an ordinal δ: a set-function defined exactly on δ,
    single-valued, whose value at every β ∈ δ satisfies the step;
  * uniqueness: two attempts agree where both are defined (least
    counterexample);
  * existence: every ordinal lies in the domain of an attempt (least
    ordinal without one; the earlier values are collected by replacement and
    the step adds the value at that ordinal).
  The step must be local (depend on g only below β), functional and total.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.IZ4

namespace Found

namespace IZ

open FO FOCT NBGc

variable (M : ZM)

/-- a recursion step with parameters -/
structure StepP where
  G : M.D → M.D → M.D → (Nat → M.D) → Prop
  dG : Def M (fun v => G (v 0) (v 1) (v 2) (fun n => v (n + 3)))
  loc : ∀ ρ g g' b y, (∀ c, In M c b → ∀ z, InG M g c z ↔ InG M g' c z) → G y b g ρ → G y b g' ρ
  fn : ∀ ρ g b y y', G y b g ρ → G y' b g ρ → Ceq M.R y y'
  ex : ∀ ρ g b, Ord M b → (∀ c, In M c b → ∃ z, InG M g c z) → ∃ y, G y b g ρ

variable {M} (S : StepP M) (ρ : Nat → M.D)

def tri3 (y b g : M.D) (ρ : Nat → M.D) : Nat → M.D
  | 0 => y
  | 1 => b
  | 2 => g
  | n + 3 => ρ n

theorem GP_resp {y b g y' b' g' : M.D} (ey : Ceq M.R y y') (eb : Ceq M.R b b') (eg : Ceq M.R g g')
    (h : S.G y b g ρ) : S.G y' b' g' ρ :=
  def_resp M S.dG (v := tri3 y b g ρ) (w := tri3 y' b' g' ρ) (fun n => match n with
    | 0 => ey
    | 1 => eb
    | 2 => eg
    | _ + 3 => ceq_refl M.R _) h

/-- an attempt on δ -/
def AttP (g d : M.D) : Prop :=
  Ord M d ∧ (∀ c, In M c d → ∃ z, InG M g c z) ∧ (∀ c z, InG M g c z → In M c d) ∧
    (∀ c z z', InG M g c z → InG M g c z' → Ceq M.R z z') ∧ (∀ c z, In M c d → InG M g c z → S.G z c g ρ)

def σ3p (i j k c : Nat) : Nat → Nat
  | 0 => i
  | 1 => j
  | 2 => k
  | n + 3 => n + c

theorem def_GP (i j k c : Nat) : Def M (fun v => S.G (v i) (v j) (v k) (fun n => v (n + c))) :=
  def_congr M (fun _ => Iff.rfl) (def_ren M (σ3p i j k c) S.dG)

theorem def_AttP (i j c : Nat) : Def M (fun v => AttP S (fun n => v (n + c)) (v i) (v j)) :=
  def_and M (def_Ord M j)
    (def_and M (def_congr M (fun _ => Iff.rfl) (def_all M (def_imp M (def_mem M 0 (j + 1))
        (def_ex M (def_InG M (i + 2) 1 0)))))
      (def_and M (def_congr M (fun _ => Iff.rfl) (def_all M (def_all M (def_imp M (def_InG M (i + 2) 1 0)
          (def_mem M 1 (j + 2))))))
        (def_and M (def_congr M (fun _ => Iff.rfl) (def_all M (def_all M (def_all M (def_imp M
            (def_InG M (i + 3) 2 1) (def_imp M (def_InG M (i + 3) 2 0) (def_ceq M 1 0)))))))
          (def_congr M (fun _ => Iff.rfl) (def_all M (def_all M (def_imp M (def_mem M 1 (j + 2))
            (def_imp M (def_InG M (i + 2) 1 0) (def_GP S 0 1 (i + 2) (c + 2))))))))))

/-! ## uniqueness -/

theorem attP_uniq (K : CP.Classical') {g d g' d' : M.D} (ha : AttP S ρ g d) (ha' : AttP S ρ g' d') :
    ∀ b, In M b d → In M b d' → ∀ z z', InG M g b z → InG M g' b z' → Ceq M.R z z' := by
  let Bad : (Nat → M.D) → Prop := fun v =>
    In M (v 0) (v 2) ∧ In M (v 0) (v 4) ∧ ∃ z z', InG M (v 1) (v 0) z ∧ InG M (v 3) (v 0) z' ∧ ¬ Ceq M.R z z'
  have hBad : Def M Bad := def_and M (def_mem M 0 2) (def_and M (def_mem M 0 4)
    (def_congr M (fun _ => Iff.rfl) (def_ex M (def_ex M (def_and M (def_InG M 3 2 1)
      (def_and M (def_InG M 5 2 0) (def_not M (def_ceq M 1 0))))))))
  let par : Nat → M.D := fun n => match n with
    | 0 => g
    | 1 => d
    | 2 => g'
    | 3 => d'
    | _ => g
  intro b hb hb' z z' hz hz'
  refine CP.dne K _ (fun hn => ?_)
  match ord_least M K hBad par ⟨b, ord_mem M ha.1 hb, hb, hb', z, z', hz, hz', hn⟩ with
  | ⟨c, _, ⟨hc, hc', w, w', hw, hw', hnw⟩, cmin⟩ =>
    -- below c, g and g' agree
    have agree : ∀ e, In M e c → ∀ u, InG M g e u ↔ InG M g' e u := fun e he u =>
      have hed : In M e d := ha.1.1 c e hc he
      have hed' : In M e d' := ha'.1.1 c e hc' he
      have eo : Ord M e := ord_mem M ha.1 hed
      have noBad : ∀ u u', InG M g e u → InG M g' e u' → Ceq M.R u u' := fun u u' h1 h2 =>
        CP.dne K _ (fun hne => (cmin e eo ⟨hed, hed', u, u', h1, h2, hne⟩).elim
          (fun hce => not_mem_self M c ((ord_mem M ha.1 hc).1 e c he hce))
          (fun ece => not_mem_self M e (mem_ceq_r M ece he)))
      ⟨fun h1 => match ha'.2.1 e hed' with
        | ⟨u', h2⟩ => InG_resp M (ceq_refl M.R _) (ceq_refl M.R _) (ceq_symm M.R (noBad u u' h1 h2)) h2,
       fun h2 => match ha.2.1 e hed with
        | ⟨u', h1⟩ => InG_resp M (ceq_refl M.R _) (ceq_refl M.R _) (noBad u' u h1 h2) h1⟩
    have hg1 : S.G w c g ρ := ha.2.2.2.2 c w hc hw
    have hg2 : S.G w' c g' ρ := ha'.2.2.2.2 c w' hc' hw'
    exact hnw (S.fn ρ g' c w w' (S.loc ρ g g' c w agree hg1) hg2)

/-! ## existence -/

/-- the value at b, from some attempt -/
def ValP (b z : M.D) : Prop := ∃ g d, AttP S ρ g d ∧ In M b d ∧ InG M g b z

theorem valP_uniq (K : CP.Classical') {b z z' : M.D} (h1 : ValP S ρ b z) (h2 : ValP S ρ b z') : Ceq M.R z z' :=
  match h1, h2 with
  | ⟨_, _, ha, hb, hz⟩, ⟨_, _, ha', hb', hz'⟩ => attP_uniq S ρ K ha ha' b hb hb' z z' hz hz'

theorem attP_exists (K : CP.Classical') : ∀ a, Ord M a → ∃ g d, AttP S ρ g d ∧ In M a d := by
  intro a0' ha0'
  let Lack : (Nat → M.D) → Prop := fun v => ¬ ∃ g d, AttP S (fun n => v (n + 1)) g d ∧ In M (v 0) d
  have hLack : Def M Lack := def_not M (def_congr M (fun _ => Iff.rfl)
    (def_ex M (def_ex M (def_and M (def_AttP S 1 0 3) (def_mem M 2 0)))))
  refine CP.dne K _ (fun hn => ?_)
  match ord_least M K hLack ρ ⟨a0', ha0', hn⟩ with
  | ⟨a, ha, hLa, amin⟩ =>
    have hasAtt : ∀ b, In M b a → ∃ g d, AttP S ρ g d ∧ In M b d := fun b hb =>
      CP.dne K _ (fun hnb => (amin b (ord_mem M ha hb) hnb).elim (fun h => not_mem_self M a (ha.1 b a hb h))
        (fun e => not_mem_self M b (mem_ceq_r M e hb)))
    have hasVal : ∀ b, In M b a → ∃ z, ValP S ρ b z := fun b hb => match hasAtt b hb with
      | ⟨g, d, hat, hbd⟩ => match hat.2.1 b hbd with
        | ⟨z, hz⟩ => ⟨z, g, d, hat, hbd, hz⟩
    -- collect the pairs ⟨b, value at b⟩, b ∈ a
    let Θ : (Nat → M.D) → Prop := fun u => ∃ y, ValP S (fun n => u (n + 2)) (u 0) y ∧ OPs M.R (u 1) (u 0) y
    have hΘ : Def M Θ := def_congr M (fun _ => Iff.rfl) (def_ex M (def_and M
      (def_congr M (fun _ => Iff.rfl) (def_ex M (def_ex M (def_and M (def_AttP S 1 0 5)
        (def_and M (def_mem M 3 0) (def_InG M 1 3 2))))))
      (def_op M 2 1 0)))
    match repl M hΘ ρ a (fun _ _ z z' ⟨y, hy, hoy⟩ ⟨y', hy', hoy'⟩ =>
        op_uniq M hoy (op_resp M hoy' (ceq_refl M.R _) (ceq_refl M.R _) (ceq_symm M.R (valP_uniq S ρ K hy hy')))) with
    | ⟨G0, hG0⟩ =>
      have G0_in : ∀ c z, InG M G0 c z → ∃ b, In M b a ∧ Ceq M.R c b ∧ ∃ y, ValP S ρ b y ∧ Ceq M.R z y :=
        fun c z ⟨p, hp, ho⟩ => match (hG0 p).1 hp with
          | ⟨b, hb, y, hy, hoy⟩ =>
            have e := op_inj' M ho hoy
            ⟨b, hb, e.1, y, hy, e.2⟩
      have G0_of : ∀ c z, In M c a → ValP S ρ c z → InG M G0 c z := fun c z hc hz =>
        match op_exists M c z with
        | ⟨p, hp⟩ => ⟨p, (hG0 p).2 ⟨c, hc, z, hz, hp⟩, hp⟩
      have G0_tot : ∀ c, In M c a → ∃ z, InG M G0 c z := fun c hc => match hasVal c hc with
        | ⟨z, hz⟩ => ⟨z, G0_of c z hc hz⟩
      -- G0 agrees with an attempt below a point of both domains
      have agreeAtt : ∀ g d b, AttP S ρ g d → In M b d → In M b a → ∀ c, In M c b → ∀ z,
          InG M g c z ↔ InG M G0 c z := fun g d b hat hbd hba c hc z =>
        have hcd : In M c d := hat.1.1 b c hbd hc
        have hca : In M c a := ha.1 b c hba hc
        ⟨fun h => G0_of c z hca ⟨g, d, hat, hcd, h⟩,
         fun h => match G0_in c z h with
          | ⟨b', _, ecb, y, hy, ezy⟩ => match hat.2.1 c hcd with
            | ⟨u, hu⟩ =>
              have e1 : Ceq M.R u y := valP_uniq S ρ K ⟨g, d, hat, mem_ceq_l M ecb hcd,
                InG_resp M (ceq_refl M.R _) ecb (ceq_refl M.R _) hu⟩ hy
              InG_resp M (ceq_refl M.R _) (ceq_refl M.R _) (ceq_trans M.R e1 (ceq_symm M.R ezy)) hu⟩
      match S.ex ρ G0 a ha G0_tot with
      | ⟨y0, hy0⟩ => match op_exists M a y0 with
        | ⟨p0, hp0⟩ => match sing_exists M p0 with
          | ⟨s0, hs0⟩ => match union2_exists M G0 s0 with
            | ⟨g, hg⟩ => match succ_exists M a with
              | ⟨d, hd⟩ =>
                -- entries of g
                have g_cases : ∀ c z, InG M g c z → InG M G0 c z ∨ (Ceq M.R c a ∧ Ceq M.R z y0) :=
                  fun c z ⟨p, hp, ho⟩ => ((hg p).1 hp).elim (fun h => .inl ⟨p, h, ho⟩)
                    (fun h => .inr (op_inj' M (op_resp M ho ((hs0 p).1 h) (ceq_refl M.R _) (ceq_refl M.R _)) hp0))
                have g_of : ∀ c z, InG M G0 c z → InG M g c z := fun c z ⟨p, hp, ho⟩ => ⟨p, (hg p).2 (.inl hp), ho⟩
                have g_top : InG M g a y0 := ⟨p0, (hg p0).2 (.inr ((hs0 p0).2 (ceq_refl M.R _))), hp0⟩
                have not_top : ∀ c z, InG M G0 c z → ¬ Ceq M.R c a := fun c z h e => match G0_in c z h with
                  | ⟨b, hb, ecb, _, _, _⟩ => not_mem_self M a (mem_ceq_l M (ceq_trans M.R (ceq_symm M.R ecb) e) hb)
                have agreeTop : ∀ c, In M c a → ∀ z, InG M g c z ↔ InG M G0 c z := fun c hc z =>
                  ⟨fun h => (g_cases c z h).elim id (fun ⟨e, _⟩ => (not_mem_self M a (mem_ceq_l M e hc)).elim),
                   g_of c z⟩
                have hatt : AttP S ρ g d := ⟨ord_succ M ha hd,
                  fun c hc => ((hd c).1 hc).elim (fun h => match G0_tot c h with
                      | ⟨z, hz⟩ => ⟨z, g_of c z hz⟩)
                    (fun e => ⟨y0, InG_resp M (ceq_refl M.R _) (ceq_symm M.R e) (ceq_refl M.R _) g_top⟩),
                  fun c z h => (g_cases c z h).elim (fun h' => match G0_in c z h' with
                      | ⟨b, hb, ecb, _, _, _⟩ => (hd c).2 (.inl (mem_ceq_l M (ceq_symm M.R ecb) hb)))
                    (fun ⟨e, _⟩ => (hd c).2 (.inr e)),
                  fun c z z' h1 h2 => match g_cases c z h1, g_cases c z' h2 with
                    | .inl h1', .inl h2' => match G0_in c z h1', G0_in c z' h2' with
                      | ⟨b, _, ecb, y, hy, ezy⟩, ⟨b', _, ecb', y', hy', ezy'⟩ =>
                        have hy2 : ValP S ρ b' y := match hy with
                          | ⟨g1, d1, a1, b1, i1⟩ => ⟨g1, d1, a1, mem_ceq_l M (ceq_trans M.R (ceq_symm M.R ecb) ecb') b1,
                              InG_resp M (ceq_refl M.R _) (ceq_trans M.R (ceq_symm M.R ecb) ecb') (ceq_refl M.R _) i1⟩
                        ceq_trans M.R ezy (ceq_trans M.R (valP_uniq S ρ K hy2 hy') (ceq_symm M.R ezy'))
                    | .inl h1', .inr ⟨e, _⟩ => (not_top c z h1' e).elim
                    | .inr ⟨e, _⟩, .inl h2' => (not_top c z' h2' e).elim
                    | .inr ⟨_, e1⟩, .inr ⟨_, e2⟩ => ceq_trans M.R e1 (ceq_symm M.R e2),
                  fun c z hc h => ((hd c).1 hc).elim
                    (fun hca => match g_cases c z h with
                      | .inl h' => match G0_in c z h' with
                        | ⟨b, hb, ecb, y, ⟨g1, d1, a1, b1, i1⟩, ezy⟩ =>
                          -- the value from attempt g1 satisfies the step there
                          have st : S.G y b g1 ρ := a1.2.2.2.2 b y b1 i1
                          have ag : ∀ e, In M e b → ∀ u, InG M g1 e u ↔ InG M g e u := fun e he u =>
                            (agreeAtt g1 d1 b a1 b1 hb e he u).trans
                              (agreeTop e (ha.1 b e hb he) u).symm
                          GP_resp S ρ (ceq_symm M.R ezy) (ceq_symm M.R ecb) (ceq_refl M.R _) (S.loc ρ g1 g b y ag st)
                      | .inr ⟨e, _⟩ => (not_mem_self M a (mem_ceq_l M e hca)).elim)
                    (fun eca => match g_cases c z h with
                      | .inl h' => (not_top c z h' eca).elim
                      | .inr ⟨_, ez⟩ =>
                        GP_resp S ρ (ceq_symm M.R ez) (ceq_symm M.R eca) (ceq_refl M.R _)
                          (S.loc ρ G0 g a y0 (fun e he u => (agreeTop e he u).symm) hy0))⟩
                exact hLa ⟨g, d, hatt, (hd a).2 (.inr (ceq_refl M.R a))⟩

/-- the value of the recursion at every ordinal -/
theorem valP_exists (K : CP.Classical') {a : M.D} (ha : Ord M a) : ∃ z, ValP S ρ a z :=
  match attP_exists S ρ K a ha with
  | ⟨g, d, hat, had⟩ => match hat.2.1 a had with
    | ⟨z, hz⟩ => ⟨z, g, d, hat, had, hz⟩

end IZ

end Found

#print axioms Found.IZ.def_AttP
#print axioms Found.IZ.attP_uniq
#print axioms Found.IZ.attP_exists
#print axioms Found.IZ.valP_exists

found/IZ5.lean

lean_all/found/IZ5.lean

/-
  Shared tool R4. found/IZ5 — the cumulative hierarchy inside a model of
  ZF, as an instance of found/IZ4:
  * the step: y is the set of all x included in an earlier value
    (V_β = ⋃_{γ<β} P(V_γ)); it is local, functional, and total (the
    candidates lie in the power set of ⋃⋃⋃g, so separation suffices);
  * Vl α y (y = V_α): definable, invariant under coextension, defined at
    every ordinal;
  * x ∈ V_α iff x ⊆ V_γ for some γ < α; every level is transitive and
    closed under subsets of members; earlier levels are included and are
    members;
  * every set belongs to some level (least levels of the members,
    collected by replacement, and their union).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.IZ4

namespace Found

namespace IZ

open FO FOCT NBGc

variable (M : ZM)

/-- the step of the hierarchy -/
def GV (y b g : M.D) : Prop := ∀ x, In M x y ↔ ∃ c, In M c b ∧ ∃ u, InG M g c u ∧ ∀ s, In M s x → In M s u

theorem def_GV : Def M (fun v => GV M (v 0) (v 1) (v 2)) :=
  def_congr M (fun _ => Iff.rfl) (def_all M (def_iff M (def_mem M 0 1)
    (def_ex M (def_and M (def_mem M 0 3) (def_ex M (def_and M (def_InG M 5 1 0)
      (def_all M (def_imp M (def_mem M 0 3) (def_mem M 0 1)))))))))

/-- values of a set-function lie in ⋃⋃g -/
theorem val_in_uu {g c u : M.D} (h : InG M g c u) {U1 U2 : M.D}
    (h1 : ∀ s, In M s U1 ↔ ∃ t, In M t g ∧ In M s t) (h2 : ∀ s, In M s U2 ↔ ∃ t, In M t U1 ∧ In M s t) :
    In M u U2 :=
  match h, pairing M c u with
  | ⟨p, hp, ho⟩, ⟨d, hd⟩ =>
    have hdp : In M d p := ((op_sem M.R p c u).1 ho d).2 (.inr hd)
    (h2 u).2 ⟨d, (h1 d).2 ⟨p, hp, hdp⟩, (hd u).2 (.inr (ceq_refl M.R u))⟩

def stepV : Step M where
  G := GV M
  dG := def_GV M
  loc := fun _ _ _ _ ag h x => (h x).trans
    ⟨fun ⟨c, hc, u, hu, hs⟩ => ⟨c, hc, u, (ag c hc u).1 hu, hs⟩,
     fun ⟨c, hc, u, hu, hs⟩ => ⟨c, hc, u, (ag c hc u).2 hu, hs⟩⟩
  fn := fun _ _ _ _ h1 h2 x => (h1 x).trans (h2 x).symm
  ex := fun g b _ _ =>
    match union_ex M g with
    | ⟨U1, h1⟩ => match union_ex M U1 with
      | ⟨U2, h2⟩ => match union_ex M U2 with
        | ⟨U3, h3⟩ => match power_ex M U3 with
          | ⟨P3, hP3⟩ =>
            let Q : (Nat → M.D) → Prop := fun v =>
              ∃ c, In M c (v 2) ∧ ∃ u, InG M (v 3) c u ∧ ∀ s, In M s (v 0) → In M s u
            have hQ : Def M Q := def_congr M (fun _ => Iff.rfl) (def_ex M (def_and M (def_mem M 0 3)
              (def_ex M (def_and M (def_InG M 5 1 0) (def_all M (def_imp M (def_mem M 0 3) (def_mem M 0 1)))))))
            let par : Nat → M.D := fun n => match n with
              | 0 => M.d0
              | 1 => b
              | 2 => g
              | _ => M.d0
            match sep M hQ par P3 with
            | ⟨Y, hY⟩ => ⟨Y, fun x =>
              ⟨fun hx => ((hY x).1 hx).2,
               fun ⟨c, hc, u, hu, hs⟩ => (hY x).2 ⟨(hP3 x).2 (fun s hsx =>
                  (h3 s).2 ⟨u, val_in_uu M hu h1 h2, hs s hsx⟩), c, hc, u, hu, hs⟩⟩⟩

/-- y = V_a -/
def Vl (a y : M.D) : Prop := Val (stepV M) a y

theorem def_Vl (i j : Nat) : Def M (fun v => Vl M (v i) (v j)) :=
  def_congr M (fun _ => Iff.rfl) (def_ex M (def_ex M (def_and M (def_Att (stepV M) 1 0)
    (def_and M (def_mem M (i + 2) 0) (def_InG M 1 (i + 2) (j + 2))))))

theorem vl_resp {a y a' y' : M.D} (ea : Ceq M.R a a') (ey : Ceq M.R y y') (h : Vl M a y) : Vl M a' y' :=
  def_resp M (def_Vl M 0 1) (v := tri a y a) (w := tri a' y' a') (fun n => match n with
    | 0 => ea
    | 1 => ey
    | 2 => ea
    | _ + 3 => ea) h

theorem vl_exists (K : CP.Classical') {a : M.D} (ha : Ord M a) : ∃ y, Vl M a y := val_exists (stepV M) K ha

theorem vl_uniq (K : CP.Classical') {a y y' : M.D} (h1 : Vl M a y) (h2 : Vl M a y') : Ceq M.R y y' := val_uniq (stepV M) K h1 h2

/-- x ∈ V_a iff x ⊆ V_c for some c ∈ a -/
theorem vl_mem (K : CP.Classical') {a y : M.D} (h : Vl M a y) (x : M.D) :
    In M x y ↔ ∃ c, In M c a ∧ ∃ u, Vl M c u ∧ ∀ s, In M s x → In M s u :=
  match h with
  | ⟨g, d, hat, had, hy⟩ =>
    have st : GV M y a g := hat.2.2.2.2 a y had hy
    ⟨fun hx => match (st x).1 hx with
      | ⟨c, hc, u, hu, hs⟩ => ⟨c, hc, u, ⟨g, d, hat, hat.1.1 a c had hc, hu⟩, hs⟩,
     fun ⟨c, hc, u, hu, hs⟩ =>
      have hcd : In M c d := hat.1.1 a c had hc
      match hat.2.1 c hcd with
      | ⟨u', hu'⟩ =>
        have e : Ceq M.R u u' := vl_uniq M K hu ⟨g, d, hat, hcd, hu'⟩
        (st x).2 ⟨c, hc, u', hu', fun s hsx => (e s).1 (hs s hsx)⟩⟩

theorem vl_trans (K : CP.Classical') {a y : M.D} (ha : Ord M a) (h : Vl M a y) : Trans M y := fun x w hx hw =>
  match (vl_mem M K h x).1 hx with
  | ⟨c, hc, _, hu, hs⟩ => match (vl_mem M K hu w).1 (hs w hw) with
    | ⟨c', hc', u', hu', hs'⟩ => (vl_mem M K h w).2 ⟨c', ha.1 c c' hc hc', u', hu', hs'⟩

theorem vl_sub (K : CP.Classical') {a y x z : M.D} (h : Vl M a y) (hz : In M z y) (hs : ∀ s, In M s x → In M s z) : In M x y :=
  match (vl_mem M K h z).1 hz with
  | ⟨c, hc, u, hu, hs'⟩ => (vl_mem M K h x).2 ⟨c, hc, u, hu, fun s hsx => hs' s (hs s hsx)⟩

/-- earlier levels are included -/
theorem vl_mono (K : CP.Classical') {a y c u : M.D} (ha : Ord M a) (h : Vl M a y) (hc : In M c a) (hu : Vl M c u) :
    ∀ s, In M s u → In M s y := fun s hsu =>
  match (vl_mem M K hu s).1 hsu with
  | ⟨c', hc', u', hu', hs⟩ => (vl_mem M K h s).2 ⟨c', ha.1 c c' hc hc', u', hu', hs⟩

theorem vl_mem_of (K : CP.Classical') {a y c u : M.D} (h : Vl M a y) (hc : In M c a) (hu : Vl M c u) : In M u y :=
  (vl_mem M K h u).2 ⟨c, hc, u, hu, fun _ h' => h'⟩

/-- levels grow with inclusion of ordinals -/
theorem vl_mono' (K : CP.Classical') {a y c u : M.D} (ha : Ord M a) (hc : Ord M c) (h : Vl M a y) (hu : Vl M c u)
    (hca : ∀ s, In M s c → In M s a) : ∀ s, In M s u → In M s y :=
  match ord_sub M K hc ha hca with
  | .inl hm => vl_mono M K ha h hm hu
  | .inr e => fun s hs => ((vl_uniq M K (vl_resp M e (ceq_refl M.R u) hu) h) s).1 hs

/-- every set belongs to some level -/
theorem vl_cover (K : CP.Classical') (x : M.D) : ∃ a y, Ord M a ∧ Vl M a y ∧ In M x y := by
  let No : (Nat → M.D) → Prop := fun v => ¬ ∃ a, Ord M a ∧ ∃ y, Vl M a y ∧ In M (v 0) y
  have hNo : Def M No := def_not M (def_congr M (fun _ => Iff.rfl) (def_ex M (def_and M (def_Ord M 0)
    (def_ex M (def_and M (def_Vl M 1 0) (def_mem M 2 0))))))
  refine CP.dne K _ (fun hn => ?_)
  match found M hNo (fun _ => M.d0) ⟨x, fun ⟨a, ha, y, hy, hxy⟩ => hn ⟨a, y, ha, hy, hxy⟩⟩ with
  | ⟨x0, hx0, xmin⟩ =>
    have hmem : ∀ t, In M t x0 → ∃ a, Ord M a ∧ ∃ y, Vl M a y ∧ In M t y := fun t ht =>
      CP.dne K _ (fun hnt => xmin t ht hnt)
    -- least levels of the members
    let L : (Nat → M.D) → Prop := fun u => ∃ y, Vl M (u 0) y ∧ In M (u 1) y
    have hL : Def M L := def_congr M (fun _ => Iff.rfl) (def_ex M (def_and M (def_Vl M 1 0) (def_mem M 2 0)))
    let Least : (Nat → M.D) → Prop := fun u => Ord M (u 1) ∧ (∃ y, Vl M (u 1) y ∧ In M (u 0) y) ∧
      ∀ b, Ord M b → (∃ y, Vl M b y ∧ In M (u 0) y) → In M (u 1) b ∨ Ceq M.R (u 1) b
    have hLeast : Def M Least := def_and M (def_Ord M 1) (def_and M
      (def_congr M (fun _ => Iff.rfl) (def_ex M (def_and M (def_Vl M 2 0) (def_mem M 1 0))))
      (def_congr M (fun _ => Iff.rfl) (def_all M (def_imp M (def_Ord M 0)
        (def_imp M (def_ex M (def_and M (def_Vl M 1 0) (def_mem M 2 0)))
          (def_or M (def_mem M 2 0) (def_ceq M 2 0)))))))
    have least_ex : ∀ t, In M t x0 → ∃ a, Least (ext (ext (fun _ => M.d0) a) t) := fun t ht =>
      match ord_least M K hL (ext (fun _ => M.d0) t) (match hmem t ht with
        | ⟨a, ha, y, hy, hty⟩ => ⟨a, ha, y, hy, hty⟩) with
      | ⟨a, ha, ⟨y, hy, hty⟩, amin⟩ => ⟨a, ha, ⟨y, hy, hty⟩, fun b hb hb' => amin b hb hb'⟩
    match repl M hLeast (fun _ => M.d0) x0 (fun t _ a a' ⟨ha, h1, m1⟩ ⟨ha', h2, m2⟩ =>
        match m1 a' ha' h2, m2 a ha h1 with
        | .inr e, _ => e
        | .inl _, .inr e => ceq_symm M.R e
        | .inl h, .inl h' => (not_mem_self M a' (ha'.1 a a' h h')).elim) with
    | ⟨A, hA⟩ =>
      have hAord : ∀ a, In M a A → Ord M a := fun a haA => match (hA a).1 haA with
        | ⟨_, _, ho, _, _⟩ => ho
      match union_ex M A with
      | ⟨U, hU⟩ =>
        have hUo : Ord M U := ord_union M hAord hU
        match vl_exists M K hUo, succ_exists M U with
        | ⟨yU, hyU⟩, ⟨s, hs⟩ =>
          have hso : Ord M s := ord_succ M hUo hs
          match vl_exists M K hso with
          | ⟨ys, hys⟩ =>
            have sub : ∀ t, In M t x0 → In M t yU := fun t ht => match least_ex t ht with
              | ⟨a, ha, ⟨y, hy, hty⟩, amin'⟩ =>
                have haA : In M a A := (hA a).2 ⟨t, ht, ha, ⟨y, hy, hty⟩, amin'⟩
                vl_mono' M K hUo ha hyU hy (fun s' hs' => (hU s').2 ⟨a, haA, hs'⟩) t hty
            exact hx0 ⟨s, hso, ys, hys, (vl_mem M K hys x0).2 ⟨U, (hs U).2 (.inr (ceq_refl M.R U)), yU, hyU, sub⟩⟩

end IZ

end Found

#print axioms Found.IZ.def_GV
#print axioms Found.IZ.stepV
#print axioms Found.IZ.def_Vl
#print axioms Found.IZ.vl_mem
#print axioms Found.IZ.vl_trans
#print axioms Found.IZ.vl_sub
#print axioms Found.IZ.vl_mono'
#print axioms Found.IZ.vl_cover

found/IZ6.lean

lean_all/found/IZ6.lean

/-
  Shared tool R5, part 1. found/IZ6 — ingredients of reflection inside a
  model of ZF:
  * relativised satisfaction to a class C of an arbitrary structure and the
    Tarski–Vaught criterion (generic in the structure);
  * ω inside the model: the members of an inductive set lying in every
    inductive set; ω is an ordinal containing ∅-like elements and closed
    under successors;
  * witness bounds: for an L0 formula ψ and an ordinal β there is an
    ordinal γ such that whenever parameters from V_β have a witness, they
    have one in V_γ (by meta induction on the number of parameters, each
    step a replacement and a union).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.IZ5

namespace Found

namespace IZ

open FO FOCT NBGc

/-! ## Tarski–Vaught for any structure -/

section TV

variable {D : Type} (R : Nat → List D → Prop) (C : D → Prop)

def SatG : Fm → (Nat → D) → Prop
  | .atom r l, v => R r (mapD v l)
  | .bot, _ => False
  | .conj a b, v => SatG a v ∧ SatG b v
  | .disj a b, v => SatG a v ∨ SatG b v
  | .imp a b, v => SatG a v → SatG b v
  | .all a, v => ∀ d, C d → SatG a (ext v d)
  | .ex a, v => ∃ d, C d ∧ SatG a (ext v d)

def HasWG (b : Fm) : Prop :=
  ∀ v : Nat → D, (∀ n, C (v n)) → (∃ d, Sat R b (ext v d)) → ∃ d, C d ∧ Sat R b (ext v d)

def WitG : Fm → Prop
  | .atom _ _ => True
  | .bot => True
  | .conj a b => WitG a ∧ WitG b
  | .disj a b => WitG a ∧ WitG b
  | .imp a b => WitG a ∧ WitG b
  | .all a => WitG a ∧ HasWG R C (neg a)
  | .ex a => WitG a ∧ HasWG R C a

theorem CG_ext {v : Nat → D} (h : ∀ n, C (v n)) {d : D} (hd : C d) : ∀ n, C (ext v d n)
  | 0 => hd
  | n + 1 => h n

theorem tvG (K : CP.Classical') : (φ : Fm) → WitG R C φ → ∀ v, (∀ n, C (v n)) → (SatG R C φ v ↔ Sat R φ v)
  | .atom _ _, _, _, _ => Iff.rfl
  | .bot, _, _, _ => Iff.rfl
  | .conj a b, w, v, h => ⟨fun s => ⟨(tvG K a w.1 v h).1 s.1, (tvG K b w.2 v h).1 s.2⟩,
      fun s => ⟨(tvG K a w.1 v h).2 s.1, (tvG K b w.2 v h).2 s.2⟩⟩
  | .disj a b, w, v, h => ⟨fun s => s.elim (fun x => Or.inl ((tvG K a w.1 v h).1 x)) (fun y => Or.inr ((tvG K b w.2 v h).1 y)),
      fun s => s.elim (fun x => Or.inl ((tvG K a w.1 v h).2 x)) (fun y => Or.inr ((tvG K b w.2 v h).2 y))⟩
  | .imp a b, w, v, h => ⟨fun s x => (tvG K b w.2 v h).1 (s ((tvG K a w.1 v h).2 x)),
      fun s x => (tvG K b w.2 v h).2 (s ((tvG K a w.1 v h).1 x))⟩
  | .all a, w, v, h =>
    ⟨fun s d => CP.dne K _ (fun hn => match w.2 v h ⟨d, hn⟩ with
        | ⟨d', hd', hn'⟩ => hn' ((tvG K a w.1 (ext v d') (CG_ext C h hd')).1 (s d' hd'))),
     fun s d hd => (tvG K a w.1 (ext v d) (CG_ext C h hd)).2 (s d)⟩
  | .ex a, w, v, h =>
    ⟨fun ⟨d, hd, s⟩ => ⟨d, (tvG K a w.1 (ext v d) (CG_ext C h hd)).1 s⟩,
     fun s => match w.2 v h s with
      | ⟨d, hd, s'⟩ => ⟨d, hd, (tvG K a w.1 (ext v d) (CG_ext C h hd)).2 s'⟩⟩

end TV

/-! ## ω inside the model -/

variable (M : ZM)

def Ind (c : M.D) : Prop :=
  (∃ e, In M e c ∧ ∀ s, ¬ In M s e) ∧ ∀ x, In M x c → ∃ y, In M y c ∧ ∀ s, In M s y ↔ (In M s x ∨ Ceq M.R s x)

theorem def_Ind (i : Nat) : Def M (fun v => Ind M (v i)) :=
  def_and M (def_congr M (fun _ => Iff.rfl) (def_ex M (def_and M (def_mem M 0 (i + 1))
      (def_all M (def_not M (def_mem M 0 1))))))
    (def_congr M (fun _ => Iff.rfl) (def_all M (def_imp M (def_mem M 0 (i + 1))
      (def_ex M (def_and M (def_mem M 0 (i + 2)) (def_all M (def_iff M (def_mem M 0 1)
        (def_or M (def_mem M 0 2) (def_ceq M 0 2)))))))))

/-- ω: the members of an inductive set lying in every inductive set -/
theorem omega_exists : ∃ w, Ind M w ∧ (∀ n, In M n w → ∀ c, Ind M c → In M n c) :=
  match inf_ex M with
  | ⟨c0, hc0⟩ =>
    let P : (Nat → M.D) → Prop := fun v => ∀ c, Ind M c → In M (v 0) c
    have hP : Def M P := def_congr M (fun _ => Iff.rfl) (def_all M (def_imp M (def_Ind M 0) (def_mem M 1 0)))
    match sep M hP (fun _ => M.d0) c0 with
    | ⟨w, hw⟩ => ⟨w, ⟨match hc0.1 with
        | ⟨e, he, hne⟩ => ⟨e, (hw e).2 ⟨he, fun _ hc => match hc.1 with
            | ⟨_, he', hne'⟩ => mem_ceq_l M (fun s => ⟨fun h => (hne' s h).elim, fun h => (hne s h).elim⟩) he'⟩, hne⟩,
      fun x hx => match (hw x).1 hx with
        | ⟨hxc, hxall⟩ => match hc0.2 x hxc with
          | ⟨y, hy, hys⟩ => ⟨y, (hw y).2 ⟨hy, fun c hc => match hc.2 x (hxall c hc) with
              | ⟨_, hy', hys'⟩ => mem_ceq_l M (fun s => (hys' s).trans (hys s).symm) hy'⟩, hys⟩⟩,
      fun n hn c hc => ((hw n).1 hn).2 c hc⟩

/-- ω is an ordinal -/
theorem omega_ord {w : M.D} (hw : Ind M w) (hmin : ∀ n, In M n w → ∀ c, Ind M c → In M n c) : Ord M w :=
  let P : (Nat → M.D) → Prop := fun v => Ord M (v 0) ∧ ∀ m, In M m (v 0) → In M m (v 1)
  have hP : Def M P := def_and M (def_Ord M 0) (def_congr M (fun _ => Iff.rfl)
    (def_all M (def_imp M (def_mem M 0 1) (def_mem M 0 2))))
  match sep M hP (fun _ => w) w with
  | ⟨T, hT⟩ =>
    have h0 : ∃ e, In M e T ∧ ∀ s, ¬ In M s e := match hw.1 with
      | ⟨e, he, hne⟩ =>
        have hord : Ord M e := ⟨fun y _ hy => (hne y hy).elim, fun y hy => (hne y hy).elim⟩
        ⟨e, (hT e).2 ⟨he, hord, fun m hm => (hne m hm).elim⟩, hne⟩
    have hind : Ind M T :=
      ⟨h0,
       fun x hx => match (hT x).1 hx with
        | ⟨hxw, hxo, hxs⟩ => match hw.2 x hxw with
          | ⟨y, hy, hys⟩ => ⟨y, (hT y).2 ⟨hy, ord_succ M hxo hys, fun m hm => ((hys m).1 hm).elim (hxs m)
              (fun e => mem_ceq_l M (ceq_symm M.R e) hxw)⟩, hys⟩⟩
    have allT : ∀ n, In M n w → Ord M n ∧ ∀ m, In M m n → In M m w := fun n hn =>
      ((hT n).1 (hmin n hn T hind)).2
    ⟨fun y z hy hz => (allT y hy).2 z hz, fun y hy => (allT y hy).1.1⟩

end IZ

end Found

#print axioms Found.IZ.tvG
#print axioms Found.IZ.def_Ind
#print axioms Found.IZ.omega_exists
#print axioms Found.IZ.omega_ord

found/IZ7.lean

lean_all/found/IZ7.lean

/-
  Shared tool R5, part 2. found/IZ7 — witness bounds inside a model of ZF:
  for an L0 formula ψ(d, p₀ … p_{k−1}) and a set Y (a level), there is an
  ordinal w such that whenever parameters from Y have a witness, they have
  one in V_w.
  Valuations carry the parameters at 0 … k−1, Y at k and the bound w at
  k+1; Bnd j says "for all choices of the first j parameters in Y …" with
  bounded internal quantifiers (definable for every j, by meta recursion);
  least bounds for one more parameter are collected by replacement and
  their union bounds them all.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.IZ6
import found.Refl2

namespace Found

namespace IZ

open FO FOCT NBGc RF

variable (M : ZM)

/-- update one position of a valuation -/
def upd (v : Nat → M.D) (j : Nat) (s : M.D) (i : Nat) : M.D :=
  match Nat.decEq i j with
  | isTrue _ => s
  | isFalse _ => v i

theorem upd_eq (v : Nat → M.D) (j : Nat) (s : M.D) : upd M v j s j = s := by
  unfold upd
  match Nat.decEq j j with
  | isTrue _ => rfl
  | isFalse h => exact absurd rfl h

theorem upd_ne (v : Nat → M.D) {i j : Nat} (s : M.D) (h : i ≠ j) : upd M v j s i = v i := by
  unfold upd
  match Nat.decEq i j with
  | isTrue h' => exact absurd h' h
  | isFalse _ => rfl

theorem eqc {v w : Nat → M.D} (h : ∀ n, v n = w n) : ∀ n, Ceq M.R (v n) (w n) := fun n => h n ▸ ceq_refl M.R _

theorem def_pw {P : (Nat → M.D) → Prop} (hP : Def M P) {v w : Nat → M.D} (h : ∀ n, v n = w n) (hv : P v) : P w :=
  def_resp M hP (eqc M h) hv

section W

variable (ψ : Fm) (k : Nat)

/-- the bounded statement -/
def Bnd : Nat → (Nat → M.D) → Prop
  | 0, v => (∃ d, Sat M.R ψ (ext v d)) → ∃ d, (∃ yw, Vl M (v (k + 1)) yw ∧ In M d yw) ∧ Sat M.R ψ (ext v d)
  | j + 1, v => ∀ s, In M s (v k) → Bnd j (upd M v j s)

def σu (j : Nat) (i : Nat) : Nat :=
  match Nat.decEq i j with
  | isTrue _ => 0
  | isFalse _ => i + 1

theorem upd_σ (u : Nat → M.D) (j : Nat) : ∀ i, u (σu j i) = upd M (fun n => u (n + 1)) j (u 0) i := fun i => by
  unfold σu upd
  match Nat.decEq i j with
  | isTrue _ => rfl
  | isFalse _ => rfl

theorem def_Bnd (hl : L0 ψ) : (j : Nat) → Def M (Bnd M ψ k j)
  | 0 => def_imp M (def_ex M ⟨ψ, hl, fun _ => Iff.rfl⟩)
      (def_ex M (def_and M (def_congr M (fun _ => Iff.rfl) (def_ex M (def_and M (def_Vl M (k + 3) 0) (def_mem M 1 0))))
        ⟨ψ, hl, fun _ => Iff.rfl⟩))
  | j + 1 =>
    have h1 : Def M (fun u => Bnd M ψ k j (upd M (fun n => u (n + 1)) j (u 0))) :=
      def_congr M (fun u => ⟨def_pw M (def_Bnd hl j) (upd_σ M u j),
        def_pw M (def_Bnd hl j) (fun i => (upd_σ M u j i).symm)⟩) (def_ren M (σu j) (def_Bnd hl j))
    def_congr M (fun _ => Iff.rfl) (def_all M (def_imp M (def_mem M 0 (k + 1)) h1))

/-- the formula only sees positions below k -/
theorem psi_agree (hf : FV (k + 1) ψ) {v v' : Nat → M.D} (h : ∀ i, i < k → v i = v' i) (d : M.D) :
    Sat M.R ψ (ext v d) ↔ Sat M.R ψ (ext v' d) :=
  ⟨sat_agreeU M.R ψ (k + 1) hf (ext_agreeU h d), sat_agreeU M.R ψ (k + 1) hf (ext_agreeU (fun i hi => (h i hi).symm) d)⟩

/-- unfolding Bnd into meta quantifiers -/
theorem bnd_meta (hf : FV (k + 1) ψ) : (j : Nat) → j ≤ k → ∀ v, Bnd M ψ k j v → ∀ v' : Nat → M.D,
    (∀ i, j ≤ i → v' i = v i) → (∀ i, i < j → In M (v' i) (v k)) → (∃ d, Sat M.R ψ (ext v' d)) →
    ∃ d, (∃ yw, Vl M (v (k + 1)) yw ∧ In M d yw) ∧ Sat M.R ψ (ext v' d)
  | 0, _, v, hb, v', hag, _, hex =>
    have e : ∀ d, Sat M.R ψ (ext v d) ↔ Sat M.R ψ (ext v' d) :=
      psi_agree M ψ k hf (fun i _ => (hag i (Nat.zero_le i)).symm)
    match hb (match hex with | ⟨d, h⟩ => ⟨d, (e d).2 h⟩) with
    | ⟨d, hd, hs⟩ => ⟨d, hd, (e d).1 hs⟩
  | j + 1, hj, v, hb, v', hag, hin, hex =>
    have hjk : j < k := Nat.lt_of_succ_le hj
    have hs : In M (v' j) (v k) := hin j (Nat.lt_succ_self j)
    have hb' := hb (v' j) hs
    match bnd_meta hf j (Nat.le_of_lt hjk) (upd M v j (v' j)) hb' v'
        (fun i hi => (Nat.lt_or_ge j i).elim
          (fun hlt => by
            rw [upd_ne M v (v' j) (Nat.ne_of_gt hlt)]
            exact hag i hlt)
          (fun hge => by
            have e : i = j := Nat.le_antisymm hge hi
            rw [e, upd_eq]))
        (fun i hi => by
          rw [upd_ne M v (v' j) (Nat.ne_of_gt hjk)]
          exact hin i (Nat.lt_succ_of_lt hi)) hex with
    | ⟨d, ⟨yw, hyw, hd⟩, hs'⟩ => ⟨d, ⟨yw, by
        rw [upd_ne M v (v' j) (Nat.ne_of_gt (Nat.lt_of_lt_of_le hjk (Nat.le_succ k)))] at hyw
        exact hyw, hd⟩, hs'⟩

/-! ## updates commute -/

theorem upd_upd (v : Nat → M.D) (p : Nat) (a b : M.D) : ∀ i, upd M (upd M v p a) p b i = upd M v p b i := fun i => by
  unfold upd
  match Nat.decEq i p with
  | isTrue _ => rfl
  | isFalse _ => rfl

theorem upd_comm (v : Nat → M.D) {p q : Nat} (h : p ≠ q) (a b : M.D) :
    ∀ i, upd M (upd M v p a) q b i = upd M (upd M v q b) p a i := fun i => by
  unfold upd
  match Nat.decEq i q, Nat.decEq i p with
  | isTrue h1, isTrue h2 => exact absurd (h2.symm.trans h1) h
  | isTrue _, isFalse _ => rfl
  | isFalse _, isTrue _ => rfl
  | isFalse _, isFalse _ => rfl

/-- the position map placing s (at 0) at j and w (at 1) at k+1, parameters shifted by c -/
def ρ2 (j k c : Nat) (a b : Nat) (i : Nat) : Nat :=
  match Nat.decEq i j with
  | isTrue _ => a
  | isFalse _ => match Nat.decEq i (k + 1) with
    | isTrue _ => b
    | isFalse _ => i + c

theorem upd_ρ2 (u : Nat → M.D) (j k c a b : Nat) (hjk : j ≠ k + 1) :
    ∀ i, u (ρ2 j k c a b i) = upd M (upd M (fun n => u (n + c)) j (u a)) (k + 1) (u b) i := fun i => by
  unfold ρ2 upd
  match Nat.decEq i j, Nat.decEq i (k + 1) with
  | isTrue h1, isTrue h2 => exact absurd (h1.symm.trans h2) hjk
  | isTrue _, isFalse _ => rfl
  | isFalse _, isTrue _ => rfl
  | isFalse _, isFalse _ => rfl

theorem def_Bnd2 (hl : L0 ψ) (j c a b : Nat) (hjk : j ≠ k + 1) :
    Def M (fun u => Bnd M ψ k j (upd M (upd M (fun n => u (n + c)) j (u a)) (k + 1) (u b))) :=
  def_congr M (fun u => ⟨def_pw M (def_Bnd M ψ k hl j) (upd_ρ2 M u j k c a b hjk),
    def_pw M (def_Bnd M ψ k hl j) (fun i => (upd_ρ2 M u j k c a b hjk i).symm)⟩)
    (def_ren M (ρ2 j k c a b) (def_Bnd M ψ k hl j))

/-! ## monotonicity and existence of bounds -/

theorem bnd_mono (K : CP.Classical') (hl : L0 ψ) (hf : FV (k + 1) ψ) : (j : Nat) → j ≤ k → ∀ v w',
    Ord M (v (k + 1)) → Ord M w' → (∀ s, In M s (v (k + 1)) → In M s w') →
    Bnd M ψ k j v → Bnd M ψ k j (upd M v (k + 1) w')
  | 0, _, v, w', ho, hw', hs, hb => fun hex =>
    have e : ∀ d, Sat M.R ψ (ext v d) ↔ Sat M.R ψ (ext (upd M v (k + 1) w') d) :=
      psi_agree M ψ k hf (fun i hi => (upd_ne M v w' (Nat.ne_of_lt (Nat.lt_succ_of_lt hi))).symm)
    match hb (match hex with | ⟨d, h⟩ => ⟨d, (e d).2 h⟩) with
    | ⟨d, ⟨yw, hyw, hd⟩, hsd⟩ => match vl_exists M K hw' with
      | ⟨yw', hyw'⟩ => ⟨d, ⟨yw', by rw [upd_eq]; exact hyw', vl_mono' M K hw' ho hyw' hyw hs d hd⟩, (e d).1 hsd⟩
  | j + 1, hj, v, w', ho, hw', hs, hb => fun s hsY =>
    have hjk : j < k := Nat.lt_of_succ_le hj
    have hne : j ≠ k + 1 := Nat.ne_of_lt (Nat.lt_succ_of_lt hjk)
    have hY : In M s (v k) := by
      rw [upd_ne M v w' (Nat.ne_of_lt (Nat.lt_succ_self k))] at hsY
      exact hsY
    have h1 := bnd_mono K hl hf j (Nat.le_of_lt hjk) (upd M v j s) w'
      (by rw [upd_ne M v s (Nat.ne_of_gt (Nat.lt_succ_of_lt hjk))]; exact ho) hw'
      (by rw [upd_ne M v s (Nat.ne_of_gt (Nat.lt_succ_of_lt hjk))]; exact hs) (hb s hY)
    def_pw M (def_Bnd M ψ k hl j) (upd_comm M v hne s w') h1

theorem empty_ord : ∃ e, Ord M e :=
  match inf_ex M with
  | ⟨_, ⟨e, _, hne⟩, _⟩ => ⟨e, fun y _ hy => (hne y hy).elim, fun y hy => (hne y hy).elim⟩

theorem bnd_ex (K : CP.Classical') (hl : L0 ψ) (hf : FV (k + 1) ψ) : (j : Nat) → j ≤ k → ∀ v,
    ∃ w, Ord M w ∧ Bnd M ψ k j (upd M v (k + 1) w)
  | 0, _, v =>
    have e : ∀ w d, Sat M.R ψ (ext v d) ↔ Sat M.R ψ (ext (upd M v (k + 1) w) d) := fun w =>
      psi_agree M ψ k hf (fun i hi => (upd_ne M v w (Nat.ne_of_lt (Nat.lt_succ_of_lt hi))).symm)
    (K.lem (∃ d, Sat M.R ψ (ext v d))).elim
      (fun ⟨d, hd⟩ => match vl_cover M K d with
        | ⟨a, y, ha, hy, hdy⟩ => ⟨a, ha, fun _ => ⟨d, ⟨y, by rw [upd_eq]; exact hy, hdy⟩, (e a d).1 hd⟩⟩)
      (fun hn => match empty_ord M with
        | ⟨e0, he0⟩ => ⟨e0, he0, fun ⟨d, hd⟩ => (hn ⟨d, (e e0 d).2 hd⟩).elim⟩)
  | j + 1, hj, v =>
    have hjk : j < k := Nat.lt_of_succ_le hj
    have hne : j ≠ k + 1 := Nat.ne_of_lt (Nat.lt_succ_of_lt hjk)
    -- the least bound for each s
    let L : (Nat → M.D) → Prop := fun u => Bnd M ψ k j (upd M (upd M (fun n => u (n + 2)) j (u 1)) (k + 1) (u 0))
    have hL : Def M L := def_Bnd2 M ψ k hl j 2 1 0 hne
    have least : ∀ s, ∃ w, Ord M w ∧ L (ext (ext v s) w) ∧ ∀ w', Ord M w' → L (ext (ext v s) w') →
        In M w w' ∨ Ceq M.R w w' := fun s =>
      match bnd_ex K hl hf j (Nat.le_of_lt hjk) (upd M v j s) with
      | ⟨w0, hw0, hb0⟩ => ord_least M K hL (ext v s) ⟨w0, hw0, hb0⟩
    let Θ : (Nat → M.D) → Prop := fun u => Ord M (u 1) ∧
      Bnd M ψ k j (upd M (upd M (fun n => u (n + 2)) j (u 0)) (k + 1) (u 1)) ∧
      ∀ w', Ord M w' → Bnd M ψ k j (upd M (upd M (fun n => u (n + 2)) j (u 0)) (k + 1) w') →
        ∀ t, In M t (u 1) → In M t w'
    have hΘ : Def M Θ := def_and M (def_Ord M 1) (def_and M (def_Bnd2 M ψ k hl j 2 0 1 hne)
      (def_congr M (fun _ => Iff.rfl) (def_all M (def_imp M (def_Ord M 0) (def_imp M (def_Bnd2 M ψ k hl j 3 1 0 hne)
        (def_all M (def_imp M (def_mem M 0 3) (def_mem M 0 1))))))))
    have sub_of : ∀ {w w' : M.D}, Ord M w' → (In M w w' ∨ Ceq M.R w w') → ∀ t, In M t w → In M t w' :=
      fun hw' h t ht => h.elim (fun hm => hw'.1 _ t hm ht) (fun e => (e t).1 ht)
    match repl M hΘ v (v k) (fun _ _ z z' ⟨o1, h1, m1⟩ ⟨o2, h2, m2⟩ =>
        fun t => ⟨fun ht => m1 z' o2 h2 t ht, fun ht => m2 z o1 h1 t ht⟩) with
    | ⟨W, hW⟩ => match union_ex M W with
      | ⟨U, hU⟩ =>
        have hWo : ∀ w, In M w W → Ord M w := fun w hw => match (hW w).1 hw with
          | ⟨_, _, ho, _, _⟩ => ho
        have hUo : Ord M U := ord_union M hWo hU
        ⟨U, hUo, fun s hsY =>
          have hY : In M s (v k) := by
            rw [upd_ne M v U (Nat.ne_of_lt (Nat.lt_succ_self k))] at hsY
            exact hsY
          match least s with
          | ⟨w, hw, hLw, hmin⟩ =>
            have hwW : In M w W := (hW w).2 ⟨s, hY, hw, hLw, fun w' hw' hb' => sub_of hw' (hmin w' hw' hb')⟩
            have hwU : ∀ t, In M t w → In M t U := fun t ht => (hU t).2 ⟨w, hwW, ht⟩
            have h1 := bnd_mono M ψ k K hl hf j (Nat.le_of_lt hjk) (upd M (upd M v j s) (k + 1) w) U
              (by rw [upd_eq]; exact hw) hUo (by rw [upd_eq]; exact hwU) hLw
            def_pw M (def_Bnd M ψ k hl j)
              (fun i => (upd_upd M (upd M v j s) (k + 1) w U i).trans (upd_comm M v hne s U i)) h1⟩

def mixk (k : Nat) (a b : Nat → M.D) (i : Nat) : M.D :=
  match Nat.decLt i k with
  | isTrue _ => a i
  | isFalse _ => b i

theorem mixk_lt (k : Nat) (a b : Nat → M.D) {i : Nat} (h : i < k) : mixk M k a b i = a i := by
  unfold mixk
  match Nat.decLt i k with
  | isTrue _ => rfl
  | isFalse h' => exact absurd h h'

theorem mixk_ge (k : Nat) (a b : Nat → M.D) {i : Nat} (h : k ≤ i) : mixk M k a b i = b i := by
  unfold mixk
  match Nat.decLt i k with
  | isTrue h' => exact absurd h' (Nat.not_lt_of_le h)
  | isFalse _ => rfl

/-- witness bounds -/
theorem witness_bound (K : CP.Classical') (hl : L0 ψ) (hf : FV (k + 1) ψ) (Y : M.D) :
    ∃ w, Ord M w ∧ ∀ v' : Nat → M.D, (∀ i, i < k → In M (v' i) Y) → (∃ d, Sat M.R ψ (ext v' d)) →
      ∃ d, (∃ yw, Vl M w yw ∧ In M d yw) ∧ Sat M.R ψ (ext v' d) :=
  let v0 : Nat → M.D := upd M (fun _ => M.d0) k Y
  match bnd_ex M ψ k K hl hf k (Nat.le_refl k) v0 with
  | ⟨w, hw, hb⟩ => ⟨w, hw, fun v' hin hex =>
    let v'' := mixk M k v' (upd M v0 (k + 1) w)
    have e : ∀ d, Sat M.R ψ (ext v' d) ↔ Sat M.R ψ (ext v'' d) :=
      psi_agree M ψ k hf (fun i hi => (mixk_lt M k v' _ hi).symm)
    match bnd_meta M ψ k hf k (Nat.le_refl k) _ hb v'' (fun i hi => mixk_ge M k v' _ hi)
        (fun i hi => by
          show In M (mixk M k v' (upd M (upd M (fun _ => M.d0) k Y) (k + 1) w) i)
            (upd M (upd M (fun _ => M.d0) k Y) (k + 1) w k)
          rw [mixk_lt M k v' _ hi, upd_ne M _ w (Nat.ne_of_lt (Nat.lt_succ_self k)), upd_eq]
          exact hin i hi)
        (match hex with | ⟨d, h⟩ => ⟨d, (e d).1 h⟩) with
    | ⟨d, ⟨yw, hyw, hd⟩, hs⟩ => ⟨d, ⟨yw, by rw [upd_eq] at hyw; exact hyw, hd⟩, (e d).2 hs⟩⟩

end W

end IZ

end Found

#print axioms Found.IZ.upd_eq
#print axioms Found.IZ.upd_ne
#print axioms Found.IZ.def_Bnd
#print axioms Found.IZ.bnd_meta
#print axioms Found.IZ.bnd_mono
#print axioms Found.IZ.bnd_ex
#print axioms Found.IZ.witness_bound

found/IZ8.lean

lean_all/found/IZ8.lean

/-
  Shared tool R5, part 3. found/IZ8 — bounds for a finite list of formulas
  inside a model of ZF:
  * Bnd k only depends on the positions k (the level) and k+1 (the bound);
  * WBv ψ Y w: "parameters from Y with a witness for ψ have one in V_w",
    definable in Y and w; monotone in w; for every Y some ordinal w works;
  * for a finite list of L0 formulas and an ordinal u, an ordinal y ⊇ u
    works for all of them at the level V_u (WBL), and WBL is monotone.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.IZ7

namespace Found

namespace IZ

open FO FOCT NBGc RF

variable (M : ZM)

section B

variable (ψ : Fm) (k : Nat)

theorem bnd_agree (hf : FV (k + 1) ψ) : (j : Nat) → j ≤ k → ∀ v v' : Nat → M.D,
    (∀ i, j ≤ i → i ≤ k + 1 → v i = v' i) → Bnd M ψ k j v → Bnd M ψ k j v'
  | 0, _, v, v', h, hb => fun hex =>
    have e : ∀ d, Sat M.R ψ (ext v d) ↔ Sat M.R ψ (ext v' d) :=
      psi_agree M ψ k hf (fun i hi => h i (Nat.zero_le i) (Nat.le_succ_of_le (Nat.le_of_lt hi)))
    match hb (match hex with | ⟨d, hd⟩ => ⟨d, (e d).2 hd⟩) with
    | ⟨d, ⟨yw, hyw, hd⟩, hs⟩ => ⟨d, ⟨yw, by rw [← h (k + 1) (Nat.zero_le _) (Nat.le_refl _)]; exact hyw, hd⟩, (e d).1 hs⟩
  | j + 1, hj, v, v', h, hb => fun s hs =>
    have hjk : j < k := Nat.lt_of_succ_le hj
    have hk : v k = v' k := h k hj (Nat.le_succ k)
    bnd_agree hf j (Nat.le_of_lt hjk) (upd M v j s) (upd M v' j s) (fun i hi hik =>
      (Nat.lt_or_ge j i).elim
        (fun hlt => by
          rw [upd_ne M v s (Nat.ne_of_gt hlt), upd_ne M v' s (Nat.ne_of_gt hlt)]
          exact h i hlt hik)
        (fun hge => by
          have e : i = j := Nat.le_antisymm hge hi
          rw [e, upd_eq, upd_eq]))
      (hb s (by rw [hk]; exact hs))

/-- parameters from Y with a witness have one in V_w -/
def WBv (Y w : M.D) : Prop := Bnd M ψ k k (upd M (fun _ => w) k Y)

def σkw (k a b : Nat) (i : Nat) : Nat :=
  match Nat.decEq i k with
  | isTrue _ => a
  | isFalse _ => b

theorem def_WBv (hl : L0 ψ) (hf : FV (k + 1) ψ) (a b : Nat) : Def M (fun u => WBv M ψ k (u a) (u b)) :=
  def_congr M (fun u => ⟨bnd_agree M ψ k hf k (Nat.le_refl k) _ _ (fun i hi hik => by
      unfold σkw upd
      match Nat.decEq i k with
      | isTrue _ => rfl
      | isFalse h' =>
        have e : i = k + 1 := Nat.le_antisymm hik (Nat.lt_of_le_of_ne hi (fun e => h' e.symm))
        rfl),
    bnd_agree M ψ k hf k (Nat.le_refl k) _ _ (fun i hi hik => by
      unfold σkw upd
      match Nat.decEq i k with
      | isTrue _ => rfl
      | isFalse _ => rfl)⟩)
    (def_ren M (σkw k a b) (def_Bnd M ψ k hl k))

theorem wbv_mono (K : CP.Classical') (hl : L0 ψ) (hf : FV (k + 1) ψ) {Y w w' : M.D} (hw : Ord M w) (hw' : Ord M w')
    (hs : ∀ t, In M t w → In M t w') (h : WBv M ψ k Y w) : WBv M ψ k Y w' :=
  have h1 := bnd_mono M ψ k K hl hf k (Nat.le_refl k) (upd M (fun _ => w) k Y) w'
    (by rw [upd_ne M _ Y (Nat.ne_of_gt (Nat.lt_succ_self k))]; exact hw) hw'
    (by rw [upd_ne M _ Y (Nat.ne_of_gt (Nat.lt_succ_self k))]; exact hs) h
  bnd_agree M ψ k hf k (Nat.le_refl k) _ _ (fun i hi hik => by
    unfold upd
    match Nat.decEq i (k + 1), Nat.decEq i k with
    | isTrue _, isTrue h2 => exact absurd (h2 ▸ ‹i = k + 1›) (Nat.ne_of_lt (Nat.lt_succ_self k))
    | isTrue _, isFalse _ => rfl
    | isFalse _, isTrue _ => rfl
    | isFalse h1, isFalse h2 => exact absurd (Nat.le_antisymm hik (Nat.lt_of_le_of_ne hi (fun e => h2 e.symm))) h1) h1

theorem wbv_ex (K : CP.Classical') (hl : L0 ψ) (hf : FV (k + 1) ψ) (Y : M.D) : ∃ w, Ord M w ∧ WBv M ψ k Y w :=
  match bnd_ex M ψ k K hl hf k (Nat.le_refl k) (upd M (fun _ => M.d0) k Y) with
  | ⟨w, hw, hb⟩ => ⟨w, hw, bnd_agree M ψ k hf k (Nat.le_refl k) _ _ (fun i hi hik => by
      unfold upd
      match Nat.decEq i (k + 1), Nat.decEq i k with
      | isTrue h1, isTrue h2 => exact absurd (h2.symm.trans h1) (Nat.ne_of_lt (Nat.lt_succ_self k))
      | isTrue _, isFalse _ => rfl
      | isFalse _, isTrue _ => rfl
      | isFalse h1, isFalse h2 => exact absurd (Nat.le_antisymm hik (Nat.lt_of_le_of_ne hi (fun e => h2 e.symm))) h1) hb⟩

/-- the meta reading of WBv -/
theorem wbv_meta (hf : FV (k + 1) ψ) {Y w : M.D} (h : WBv M ψ k Y w) (v' : Nat → M.D)
    (hin : ∀ i, i < k → In M (v' i) Y) (hex : ∃ d, Sat M.R ψ (ext v' d)) :
    ∃ d, (∃ yw, Vl M w yw ∧ In M d yw) ∧ Sat M.R ψ (ext v' d) :=
  let v'' := mixk M k v' (upd M (fun _ => w) k Y)
  have e : ∀ d, Sat M.R ψ (ext v' d) ↔ Sat M.R ψ (ext v'' d) :=
    psi_agree M ψ k hf (fun i hi => (mixk_lt M k v' _ hi).symm)
  match bnd_meta M ψ k hf k (Nat.le_refl k) _ h v'' (fun i hi => mixk_ge M k v' _ hi)
      (fun i hi => by
        show In M (mixk M k v' (upd M (fun _ => w) k Y) i) (upd M (fun _ => w) k Y k)
        rw [mixk_lt M k v' _ hi, upd_eq]
        exact hin i hi)
      (match hex with | ⟨d, hd⟩ => ⟨d, (e d).1 hd⟩) with
  | ⟨d, ⟨yw, hyw, hd⟩, hs⟩ => ⟨d, ⟨yw, by
      rw [upd_ne M _ Y (Nat.ne_of_gt (Nat.lt_succ_self k))] at hyw
      exact hyw, hd⟩, (e d).2 hs⟩

end B

end IZ

end Found

#print axioms Found.IZ.bnd_agree
#print axioms Found.IZ.def_WBv
#print axioms Found.IZ.wbv_mono
#print axioms Found.IZ.wbv_ex
#print axioms Found.IZ.wbv_meta

found/IZ9.lean

lean_all/found/IZ9.lean

/-
  Shared tool R5, part 4. found/IZ9 — the stage recursion for reflection
  inside a model of ZF, for a finite list Ψ of L0 formulas:
  * WBL Ψ Y w: every formula of Ψ has its witness bound at level Y below w;
    definable, monotone, and some ordinal above any given ordinal works;
  * the stage step: the value at b is the least ordinal including every
    earlier (ordinal) value u and bounding the witnesses of Ψ for
    parameters from V_u; it is local, functional and total (bounds for each
    member of the set-function are collected by replacement).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.IZ8

namespace Found

namespace IZ

open FO FOCT NBGc RF

variable (M : ZM)

def WBL : List Fm → M.D → M.D → Prop
  | [], _, _ => True
  | ψ :: Ψ, Y, w => WBv M ψ (bound ψ) Y w ∧ WBL Ψ Y w

def L0L' (Ψ : List Fm) : Prop := ∀ ψ, ψ ∈ Ψ → L0 ψ

theorem fv_b (ψ : Fm) : FV (bound ψ + 1) ψ := fv_mono ψ (Nat.le_succ _) (fv_bound ψ)

theorem def_WBL : (Ψ : List Fm) → L0L' Ψ → (a b : Nat) → Def M (fun u => WBL M Ψ (u a) (u b))
  | [], _, _, _ => def_true M
  | ψ :: Ψ, h, a, b => def_and M (def_WBv M ψ (bound ψ) (h ψ (.head _)) (fv_b ψ) a b)
      (def_WBL Ψ (fun x m => h x (.tail _ m)) a b)

theorem wbl_mono (K : CP.Classical') : (Ψ : List Fm) → L0L' Ψ → ∀ {Y w w' : M.D}, Ord M w → Ord M w' →
    (∀ t, In M t w → In M t w') → WBL M Ψ Y w → WBL M Ψ Y w'
  | [], _, _, _, _, _, _, _, _ => trivial
  | ψ :: Ψ, h, _, _, _, hw, hw', hs, hb =>
    ⟨wbv_mono M ψ (bound ψ) K (h ψ (.head _)) (fv_b ψ) hw hw' hs hb.1,
     wbl_mono K Ψ (fun x m => h x (.tail _ m)) hw hw' hs hb.2⟩

/-- an ordinal including two ordinals -/
theorem ord_join {a b : M.D} (ha : Ord M a) (hb : Ord M b) :
    ∃ c, Ord M c ∧ (∀ t, In M t a → In M t c) ∧ (∀ t, In M t b → In M t c) :=
  match pairing M a b with
  | ⟨p, hp⟩ => match union_ex M p with
    | ⟨c, hc⟩ => ⟨c, ord_union M (fun y hy => ((hp y).1 hy).elim (fun e => ord_resp M (ceq_symm M.R e) ha)
        (fun e => ord_resp M (ceq_symm M.R e) hb)) hc,
      fun t ht => (hc t).2 ⟨a, (hp a).2 (.inl (ceq_refl M.R a)), ht⟩,
      fun t ht => (hc t).2 ⟨b, (hp b).2 (.inr (ceq_refl M.R b)), ht⟩⟩

theorem wbl_ex (K : CP.Classical') : (Ψ : List Fm) → L0L' Ψ → ∀ (Y u : M.D), Ord M u →
    ∃ w, Ord M w ∧ (∀ t, In M t u → In M t w) ∧ WBL M Ψ Y w
  | [], _, _, u, hu => ⟨u, hu, fun _ h => h, trivial⟩
  | ψ :: Ψ, h, Y, u, hu => match wbl_ex K Ψ (fun x m => h x (.tail _ m)) Y u hu,
      wbv_ex M ψ (bound ψ) K (h ψ (.head _)) (fv_b ψ) Y with
    | ⟨_, hw1, hs1, hb1⟩, ⟨_, hw2, hb2⟩ => match ord_join M hw1 hw2 with
      | ⟨c, hc, h1, h2⟩ => ⟨c, hc, fun t ht => h1 t (hs1 t ht),
          wbv_mono M ψ (bound ψ) K (h ψ (.head _)) (fv_b ψ) hw2 hc h2 hb2,
          wbl_mono M K Ψ (fun x m => h x (.tail _ m)) hw1 hc h1 hb1⟩

section S

variable (Ψ : List Fm) (hΨ : L0L' Ψ)

/-- the condition on a stage value y at b, given earlier values g -/
def QS (y b g : M.D) : Prop :=
  Ord M y ∧ ∀ c, In M c b → ∀ u, InG M g c u → Ord M u →
    (∀ t, In M t u → In M t y) ∧ ∃ yu, Vl M u yu ∧ WBL M Ψ yu y

include hΨ in
theorem def_QS (yi bi gi : Nat) : Def M (fun v => QS M Ψ (v yi) (v bi) (v gi)) :=
  def_and M (def_Ord M yi) (def_congr M (fun _ => Iff.rfl) (def_all M (def_imp M (def_mem M 0 (bi + 1))
    (def_all M (def_imp M (def_InG M (gi + 2) 1 0) (def_imp M (def_Ord M 0)
      (def_and M (def_all M (def_imp M (def_mem M 0 1) (def_mem M 0 (yi + 3))))
        (def_ex M (def_and M (def_Vl M 1 0) (def_WBL M Ψ hΨ 0 (yi + 3)))))))))))

def GS (y b g : M.D) : Prop := QS M Ψ y b g ∧ ∀ y', QS M Ψ y' b g → ∀ t, In M t y → In M t y'

include hΨ in
theorem def_GS : Def M (fun v => GS M Ψ (v 0) (v 1) (v 2)) :=
  def_and M (def_QS M Ψ hΨ 0 1 2) (def_congr M (fun _ => Iff.rfl) (def_all M (def_imp M (def_QS M Ψ hΨ 0 2 3)
    (def_all M (def_imp M (def_mem M 0 2) (def_mem M 0 1))))))

theorem qs_loc {g g' b y : M.D} (ag : ∀ c, In M c b → ∀ z, InG M g c z ↔ InG M g' c z) (h : QS M Ψ y b g) :
    QS M Ψ y b g' :=
  ⟨h.1, fun c hc u hu hou => h.2 c hc u ((ag c hc u).2 hu) hou⟩

/-- the bound for one member p = ⟨c, u⟩ of the set-function -/
def RS (v : Nat → M.D) : Prop := ∀ c u, OPs M.R (v 1) c u → In M c (v 2) → Ord M u →
    (∀ t, In M t u → In M t (v 0)) ∧ ∃ yu, Vl M u yu ∧ WBL M Ψ yu (v 0)

def ThS (v : Nat → M.D) : Prop := Ord M (v 1) ∧ RS M Ψ (tri (v 1) (v 0) (v 2)) ∧
    ∀ y', Ord M y' → RS M Ψ (tri y' (v 0) (v 2)) → ∀ t, In M t (v 1) → In M t y'

include hΨ in
theorem qs_ex (K : CP.Classical') (g b : M.D) : ∃ y, QS M Ψ y b g := by
  have hR : Def M (RS M Ψ) := def_congr M (fun _ => Iff.rfl) (def_all M (def_all M (def_imp M (def_op M 3 1 0)
    (def_imp M (def_mem M 1 4) (def_imp M (def_Ord M 0) (def_and M
      (def_all M (def_imp M (def_mem M 0 1) (def_mem M 0 3)))
      (def_ex M (def_and M (def_Vl M 1 0) (def_WBL M Ψ hΨ 0 3)))))))))
  have rex : ∀ p, ∃ y, Ord M y ∧ RS M Ψ (ext (ext (fun _ => b) p) y) := fun p =>
    (K.lem (∃ c u, OPs M.R p c u ∧ In M c b ∧ Ord M u)).elim
      (fun ⟨c, u, hop, _, hou⟩ => match vl_exists M K hou with
        | ⟨yu, hyu⟩ => match wbl_ex M K Ψ hΨ yu u hou with
          | ⟨w, hw, hs, hwb⟩ => ⟨w, hw, fun c' u' hop' _ hou' =>
            have e := op_inj' M hop hop'
            ⟨fun t ht => hs t ((e.2 t).2 ht), yu, vl_resp M e.2 (ceq_refl M.R yu) hyu, hwb⟩⟩)
      (fun hn => match empty_ord M with
        | ⟨e0, he0⟩ => ⟨e0, he0, fun c u hop hc hou => (hn ⟨c, u, hop, hc, hou⟩).elim⟩)
  have hΘ : Def M (ThS M Ψ) := def_and M (def_Ord M 1) (def_and M
    (def_congr M (fun _ => Iff.rfl) (def_ren M (opσ 1 0 2) hR))
    (def_congr M (fun _ => Iff.rfl) (def_all M (def_imp M (def_Ord M 0)
      (def_imp M (def_congr M (fun _ => Iff.rfl) (def_ren M (opσ 0 1 3) hR))
        (def_all M (def_imp M (def_mem M 0 3) (def_mem M 0 1))))))))
  have least : ∀ p, ∃ y, ThS M Ψ (ext (ext (fun _ => b) y) p) := fun p =>
    match ord_least M K hR (ext (fun _ => b) p) (rex p) with
    | ⟨y, hy, hRy, ymin⟩ => ⟨y, hy, hRy, fun y' hy' hR' t ht => (ymin y' hy' hR').elim
        (fun hm => hy'.1 y t hm ht) (fun e => (e t).1 ht)⟩
  match repl M hΘ (fun _ => b) g (fun _ _ z z' ⟨o1, h1, m1⟩ ⟨o2, h2, m2⟩ t =>
      ⟨fun ht => m1 z' o2 h2 t ht, fun ht => m2 z o1 h1 t ht⟩) with
  | ⟨W, hW⟩ => match union_ex M W with
    | ⟨U, hU⟩ =>
      have hWo : ∀ y, In M y W → Ord M y := fun y hy => match (hW y).1 hy with
        | ⟨_, _, ho, _, _⟩ => ho
      exact ⟨U, ord_union M hWo hU, fun c hc u ⟨p, hp, hop⟩ hou =>
        match least p with
        | ⟨y, hy, hRy, ymin⟩ =>
          have hyW : In M y W := (hW y).2 ⟨p, hp, hy, hRy, ymin⟩
          have hyU : ∀ t, In M t y → In M t U := fun t ht => (hU t).2 ⟨y, hyW, ht⟩
          match hRy c u hop hc hou with
          | ⟨hs, yu, hyu, hwb⟩ => ⟨fun t ht => hyU t (hs t ht), yu, hyu,
              wbl_mono M K Ψ hΨ hy (ord_union M hWo hU) hyU hwb⟩⟩

include hΨ in
/-- the stage step -/
def stepS (K : CP.Classical') : Step M where
  G := GS M Ψ
  dG := def_GS M Ψ hΨ
  loc := fun _ _ _ _ ag h => ⟨qs_loc M Ψ ag h.1, fun y' hq => h.2 y' (qs_loc M Ψ (fun c hc z => (ag c hc z).symm) hq)⟩
  fn := fun _ _ _ _ h1 h2 t => ⟨fun ht => h1.2 _ h2.1 t ht, fun ht => h2.2 _ h1.1 t ht⟩
  ex := fun g b _ _ =>
    let P : (Nat → M.D) → Prop := fun v => QS M Ψ (v 0) (v 1) (v 2)
    match ord_least M K (P := P) (def_QS M Ψ hΨ 0 1 2) (ext (fun _ => g) b)
        (match qs_ex M Ψ hΨ K g b with
          | ⟨y, hy⟩ => ⟨y, hy.1, hy⟩) with
    | ⟨y, _, hq, ymin⟩ => ⟨y, hq, fun y' hq' t ht => (ymin y' hq'.1 hq').elim
        (fun hm => hq'.1.1 y t hm ht) (fun e => (e t).1 ht)⟩

end S

end IZ

end Found

#print axioms Found.IZ.def_WBL
#print axioms Found.IZ.wbl_ex
#print axioms Found.IZ.def_GS
#print axioms Found.IZ.qs_ex
#print axioms Found.IZ.stepS

found/IZF.lean

lean_all/found/IZF.lean

/-
  T88. found/IZF — IZF / CZF as a point: Aczel's tree universe V (T109)
  with bisimilarity as equality (no quotient).

  Axioms verified in V (each as an iff specification up to ≈):
    empty set, pairing, union, full separation (for ≈-respecting predicates),
    power set, infinity (ω with ∅ and successor), collection in Σ-form,
    ∈-induction and extensionality (reused from T109).
  Power set and full separation use that Prop is impredicative (CIC);
  without it one gets CZF's subset collection / bounded separation (known,
  Aczel) — drawn as the line IZF → CZF.

  Lean 4 core only, hand-written proofs, no axioms.
  CIC dependence: impredicative Prop (power set: a.I → Prop : Type; separation).
-/
import found.Mostowski


universe u

namespace Found

namespace IZF

open WF

def empty : V.{u} := .sup PEmpty.{u + 1} (fun e => nomatch e)

theorem empty_spec (x : V.{u}) : ¬ Mem x empty := fun ⟨j, _⟩ => nomatch j

def pair (a b : V.{u}) : V.{u} := .sup (ULift.{u} Bool) (fun t => match t.down with | true => a | false => b)

theorem pair_spec (a b x : V.{u}) : Mem x (pair a b) ↔ Eq' x a ∨ Eq' x b :=
  ⟨fun ⟨t, e⟩ => match t, e with
     | ⟨true⟩, e => Or.inl e
     | ⟨false⟩, e => Or.inr e,
   fun h => h.elim (fun e => ⟨⟨true⟩, e⟩) (fun e => ⟨⟨false⟩, e⟩)⟩

def union (a : V.{u}) : V.{u} := .sup ((i : a.I) × (a.f i).I) (fun p => (a.f p.1).f p.2)

theorem union_spec (a x : V.{u}) : Mem x (union a) ↔ ∃ y : V.{u}, Mem y a ∧ Mem x y :=
  ⟨fun ⟨⟨i, k⟩, e⟩ => ⟨a.f i, ⟨i, eq_refl _⟩, ⟨k, e⟩⟩,
   fun ⟨_, ⟨i, ey⟩, m⟩ => match mem_resp_right m ey with
     | ⟨k, e⟩ => ⟨⟨i, k⟩, e⟩⟩

def Resp (φ : V.{u} → Prop) : Prop := ∀ x y, Eq' x y → φ x → φ y

def sep (a : V.{u}) (φ : V.{u} → Prop) : V.{u} := .sup {i : a.I // φ (a.f i)} (fun p => a.f p.val)

theorem sep_spec (a : V.{u}) (φ : V.{u} → Prop) (hφ : Resp φ) (x : V.{u}) :
    Mem x (sep a φ) ↔ Mem x a ∧ φ x :=
  ⟨fun ⟨⟨i, h⟩, e⟩ => ⟨⟨i, e⟩, hφ _ _ (eq_symm _ _ e) h⟩,
   fun ⟨⟨i, e⟩, h⟩ => ⟨⟨i, hφ _ _ e h⟩, e⟩⟩

def power (a : V.{u}) : V.{u} := .sup (a.I → Prop) (fun P => .sup {i : a.I // P i} (fun p => a.f p.val))

theorem power_spec (a x : V.{u}) : Mem x (power a) ↔ ∀ z : V.{u}, Mem z x → Mem z a := by
  constructor
  · intro ⟨P, e⟩ z m
    match mem_resp_right m e with
    | ⟨⟨i, _⟩, e'⟩ => exact ⟨i, e'⟩
  · intro h
    refine ⟨fun i => Mem (a.f i) x, ?_⟩
    match x, h with
    | .sup J g, h =>
      exact ⟨fun j => match h (g j) ⟨j, eq_refl _⟩ with
               | ⟨i, e⟩ => ⟨⟨i, mem_resp_left e ⟨j, eq_refl _⟩⟩, e⟩,
             fun ⟨i, ⟨j, e⟩⟩ => ⟨j, eq_symm _ _ e⟩⟩

def succ (x : V.{u}) : V.{u} := .sup (Option x.I) (fun o => match o with | none => x | some i => x.f i)

theorem succ_resp : (x y : V.{u}) → Eq' x y → Eq' (succ x) (succ y)
  | .sup _ _, .sup _ _, ⟨e1, e2⟩ =>
    ⟨fun o => match o with
       | none => ⟨none, ⟨e1, e2⟩⟩
       | some i => match e1 i with | ⟨j, e⟩ => ⟨some j, e⟩,
     fun o => match o with
       | none => ⟨none, ⟨e1, e2⟩⟩
       | some j => match e2 j with | ⟨i, e⟩ => ⟨some i, e⟩⟩

def natV : Nat → V.{u}
  | 0 => empty
  | n + 1 => succ (natV n)

def omega : V.{u} := .sup (ULift.{u} Nat) (fun n => natV n.down)

theorem infinity : Mem empty omega ∧ ∀ x : V.{u}, Mem x omega → Mem (succ x) omega :=
  ⟨⟨⟨Nat.zero⟩, eq_refl _⟩, fun _ ⟨n, e⟩ => ⟨⟨Nat.succ n.down⟩, succ_resp _ _ e⟩⟩

/-- collection, Σ-form: a chosen witness for each index -/
theorem collection (a : V.{u}) (φ : V.{u} → V.{u} → Prop) (hφ : ∀ x x' y, Eq' x x' → φ x y → φ x' y)
    (h : (i : a.I) → {y : V.{u} // φ (a.f i) y}) :
    ∃ b : V.{u}, ∀ x : V.{u}, Mem x a → ∃ y : V.{u}, Mem y b ∧ φ x y :=
  ⟨.sup a.I (fun i => (h i).val), fun _ ⟨i, e⟩ =>
    ⟨(h i).val, ⟨i, eq_refl _⟩, hφ _ _ _ (eq_symm _ _ e) (h i).property⟩⟩

/-- ∈-induction and extensionality are T109's (reused) -/
theorem ext_reused (x y : V.{u}) (h : ∀ z : V.{u}, Mem z x ↔ Mem z y) : Eq' x y := extensionality x y h

end IZF

end Found

#print axioms Found.IZF.empty_spec
#print axioms Found.IZF.pair_spec
#print axioms Found.IZF.union_spec
#print axioms Found.IZF.sep_spec
#print axioms Found.IZF.power_spec
#print axioms Found.IZF.succ_resp
#print axioms Found.IZF.infinity
#print axioms Found.IZF.collection
#print axioms Found.IZF.ext_reused

found/InfCat.lean

lean_all/found/InfCat.lean

/-
  T153. found/InfCat — (∞,1)-categories as a point: quasi-categories, i.e.
  simplicial sets (T134) with inner horn fillers; stated here in dimension 2
  for any simplicial set X (the parameter):
    for 1-simplices a, b with  target(a) ≈ source(b)  there is σ ∈ X₂ with
    d₂σ ≈ a and d₀σ ≈ b.
  * Nerves of preorders are quasi-categories in dimension 2 (T134's fill).
  Higher inner horns, ∞-toposes and (∞,n)-categories are the known
  continuation (cited; T196).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Simplicial

namespace Found

namespace InfCat

open Simp

def v0 : DHom 0 1 := ⟨fun _ => 0, fun _ h => absurd h (Nat.not_lt_zero _), fun _ _ => Nat.zero_le _⟩
def v1 : DHom 0 1 := ⟨fun _ => 1, fun _ h => absurd h (Nat.not_lt_zero _), fun _ _ => Nat.le_refl _⟩
def d2 : DHom 1 2 := ⟨fun i => i, fun _ _ => Nat.le_succ _, fun _ h => Nat.le_trans h (Nat.le_succ _)⟩
def d0 : DHom 1 2 := ⟨fun i => i + 1, fun _ _ => Nat.le_succ _, fun _ h => Nat.succ_le_succ h⟩

/-- inner horn Λ²₁ filling for a simplicial set -/
def n0 : Delta.Ob := (0 : Nat)
def n1 : Delta.Ob := (1 : Nat)
def n2 : Delta.Ob := (2 : Nat)

def Inner2 (X : Psh.{0, 0, 0} Delta) : Prop :=
  ∀ a b : (X.obj n1).car, (X.obj n0).eqv (X.map (a := n0) (b := n1) v1 a) (X.map (a := n0) (b := n1) v0 b) →
    ∃ σ : (X.obj n2).car, (X.obj n1).eqv (X.map (a := n1) (b := n2) d2 σ) a ∧
      (X.obj n1).eqv (X.map (a := n1) (b := n2) d0 σ) b

theorem le_one {i : Nat} (h : i ≤ 1) : i = 0 ∨ i = 1 := by
  cases i with
  | zero => exact Or.inl rfl
  | succ k => cases k with
    | zero => exact Or.inr rfl
    | succ k => exact absurd (Nat.le_of_succ_le_succ h) (Nat.not_succ_le_zero k)

/-- nerves of preorders are quasi-categories (dimension 2) -/
theorem nerve_inner2 (Q : Preorder') : Inner2 (nerve Q) := by
  intro a b e
  have e' : a.c 1 = b.c 0 := e 0 (Nat.le_refl 0)
  refine ⟨fill a b e', fun i hi => ?_, fun i hi => ?_⟩
  · rcases le_one hi with h | h <;> (cases h; rfl)
  · rcases le_one hi with h | h <;> cases h
    · exact e'
    · rfl

end InfCat

end Found

#print axioms Found.InfCat.nerve_inner2

found/InfN.lean

lean_all/found/InfN.lean

/-
  T196. found/InfN — (∞,n)-categories as a point, at the definition level:
  globular sets (cells of every dimension with source/target satisfying the
  globular identities) are the shape parameter; (∞,n) means cells above
  dimension n are invertible.
  * Every type A gives a globular set by iterated identity types
    (cells: x, then (x, y, x = y), …): the ∞-groupoid of a type, an
    (∞,0)-category; the globular identities hold.
  * In the CIC point all higher cells collapse: any two cells with the same
    boundary are equal (proof irrelevance) — CIC types are 0-truncated; the
    non-truncated version is Cubical Agda's (work/agda/Univalence.agda).
  Complicial sets, Θₙ-spaces and comparison theorems are cited.

  Lean 4 core only, hand-written proofs, no axioms.
  CIC dependence: proof irrelevance (collapse).
-/

namespace Found

namespace InfN

universe u

structure Glob where
  C : Nat → Type u
  s : (k : Nat) → C (k + 1) → C k
  t : (k : Nat) → C (k + 1) → C k
  glob_s : ∀ k (x : C (k + 2)), s k (s (k + 1) x) = s k (t (k + 1) x)
  glob_t : ∀ k (x : C (k + 2)), t k (s (k + 1) x) = t k (t (k + 1) x)

/-- iterated identity cells of a type -/
def Cell (A : Type u) : Nat → Type u
  | 0 => A
  | k + 1 => (x : Cell A k) × (y : Cell A k) × PLift (x = y)

def src (A : Type u) (k : Nat) (c : Cell A (k + 1)) : Cell A k := c.1
def tgt (A : Type u) (k : Nat) (c : Cell A (k + 1)) : Cell A k := c.2.1

theorem bnd_eq (A : Type u) (k : Nat) (c : Cell A (k + 1)) : src A k c = tgt A k c := c.2.2.down

/-- the ∞-groupoid of a type -/
def ofType (A : Type u) : Glob.{u} where
  C := Cell A
  s := src A
  t := tgt A
  glob_s k x := congrArg (src A k) (bnd_eq A (k + 1) x)
  glob_t k x := congrArg (tgt A k) (bnd_eq A (k + 1) x)

/-- every cell has an inverse cell (groupoid: (∞,0)) -/
def inv (A : Type u) (k : Nat) (c : Cell A (k + 1)) : Cell A (k + 1) :=
  ⟨c.2.1, c.1, ⟨c.2.2.down.symm⟩⟩

theorem inv_bnd (A : Type u) (k : Nat) (c : Cell A (k + 1)) :
    src A k (inv A k c) = tgt A k c ∧ tgt A k (inv A k c) = src A k c := ⟨rfl, rfl⟩

/-- CIC collapse: cells with the same boundary are equal -/
theorem collapse (A : Type u) (k : Nat) (c d : Cell A (k + 1))
    (hs : src A k c = src A k d) (ht : tgt A k c = tgt A k d) : c = d :=
  match c, d with
  | ⟨x, y, ⟨_⟩⟩, ⟨x', y', ⟨_⟩⟩ => by
    cases hs
    cases ht
    rfl

end InfN

end Found

#print axioms Found.InfN.ofType
#print axioms Found.InfN.inv_bnd
#print axioms Found.InfN.collapse

found/Institutions.lean

lean_all/found/Institutions.lean

/-
  T157. found/Institutions — Goguen–Burstall institutions as a point: a
  category of signatures, a sentence functor, a model functor and the
  satisfaction condition (truth is invariant under change of notation).
  * Abstract structure (signatures and morphisms as a parameter).
  * Instance: propositional logic — signatures = atom types, morphisms =
    renamings, sentences = formulas, models = Prop-valuations, reduct =
    precomposition; the satisfaction condition holds (by induction).
  Every logic point of the map with a signature notion is an institution;
  comorphisms are the translations (lines).

  Lean 4 core only, hand-written proofs, no axioms.
-/

namespace Found

namespace Inst

universe u v w

structure Institution where
  Sig : Type u
  Hom : Sig → Sig → Type v
  Sen : Sig → Type w
  Mod : Sig → Type w
  sen : {S₁ S₂ : Sig} → Hom S₁ S₂ → Sen S₁ → Sen S₂
  red : {S₁ S₂ : Sig} → Hom S₁ S₂ → Mod S₂ → Mod S₁
  sat : (S : Sig) → Mod S → Sen S → Prop
  condition : ∀ {S₁ S₂} (σ : Hom S₁ S₂) (M : Mod S₂) (φ : Sen S₁),
    sat S₂ M (sen σ φ) ↔ sat S₁ (red σ M) φ

inductive PF (A : Type) where
  | atom : A → PF A
  | bot : PF A
  | and : PF A → PF A → PF A
  | or : PF A → PF A → PF A
  | imp : PF A → PF A → PF A

def ren {A B : Type} (σ : A → B) : PF A → PF B
  | .atom a => .atom (σ a)
  | .bot => .bot
  | .and p q => .and (ren σ p) (ren σ q)
  | .or p q => .or (ren σ p) (ren σ q)
  | .imp p q => .imp (ren σ p) (ren σ q)

def sat {A : Type} (v : A → Prop) : PF A → Prop
  | .atom a => v a
  | .bot => False
  | .and p q => sat v p ∧ sat v q
  | .or p q => sat v p ∨ sat v q
  | .imp p q => sat v p → sat v q

theorem condition {A B : Type} (σ : A → B) (v : B → Prop) :
    (φ : PF A) → (sat v (ren σ φ) ↔ sat (fun a => v (σ a)) φ)
  | .atom _ => Iff.rfl
  | .bot => Iff.rfl
  | .and p q => ⟨fun h => ⟨(condition σ v p).mp h.1, (condition σ v q).mp h.2⟩,
                 fun h => ⟨(condition σ v p).mpr h.1, (condition σ v q).mpr h.2⟩⟩
  | .or p q => ⟨fun h => h.elim (fun a => Or.inl ((condition σ v p).mp a)) (fun b => Or.inr ((condition σ v q).mp b)),
                fun h => h.elim (fun a => Or.inl ((condition σ v p).mpr a)) (fun b => Or.inr ((condition σ v q).mpr b))⟩
  | .imp p q => ⟨fun h x => (condition σ v q).mp (h ((condition σ v p).mpr x)),
                 fun h x => (condition σ v q).mpr (h ((condition σ v p).mp x))⟩

def propInst : Institution.{1, 0, 1} where
  Sig := Type
  Hom A B := A → B
  Sen A := ULift (PF A)
  Mod A := ULift (A → Prop)
  sen σ φ := ⟨ren σ φ.down⟩
  red σ M := ⟨fun a => M.down (σ a)⟩
  sat _ M φ := sat M.down φ.down
  condition σ M φ := condition σ M.down φ.down

end Inst

end Found

#print axioms Found.Inst.condition
#print axioms Found.Inst.propInst

found/Internal.lean

lean_all/found/Internal.lean

/-
  T64. found/Internal — the internal language of the setoid topos.

  * Formulas of first-order logic over one sort (de Bruijn variables),
    atoms interpreted by respecting predicates `Pred X`, equality by the
    setoid relation.
  * Semantics in Ω = (Prop, ↔), environments `Nat → X`.
  * Every formula denotes a subobject of the environment setoid (it respects
    pointwise equivalence): the Mitchell–Bénabou reading "formula = map to Ω".
  * Intuitionistic natural deduction with ∀, ∃ and Leibniz equality is sound.

  Lean 4 core only, hand-written proofs, no axioms.

  CIC dependence: truth values = impredicative Prop.
-/
import found.Topos

namespace Found

namespace IL

inductive Form where
  | top : Form
  | bot : Form
  | atom : Nat → Nat → Form      -- predicate symbol, variable
  | eq : Nat → Nat → Form
  | and : Form → Form → Form
  | or : Form → Form → Form
  | imp : Form → Form → Form
  | all : Form → Form
  | ex : Form → Form

def lift (f : Nat → Nat) : Nat → Nat
  | 0 => 0
  | n + 1 => f n + 1

def rename (f : Nat → Nat) : Form → Form
  | .top => .top
  | .bot => .bot
  | .atom r i => .atom r (f i)
  | .eq i j => .eq (f i) (f j)
  | .and a b => .and (rename f a) (rename f b)
  | .or a b => .or (rename f a) (rename f b)
  | .imp a b => .imp (rename f a) (rename f b)
  | .all a => .all (rename (lift f) a)
  | .ex a => .ex (rename (lift f) a)

def shift : Form → Form := rename Nat.succ

def instF (i : Nat) : Nat → Nat
  | 0 => i
  | n + 1 => n

/-- `φ[i/0]` -/
def inst (i : Nat) : Form → Form := rename (instF i)

section Sem
variable {X : Setd.{0}} (R : Nat → Pred X)

def cons (x : X.car) (ρ : Nat → X.car) : Nat → X.car
  | 0 => x
  | n + 1 => ρ n

def sem : Form → (Nat → X.car) → Prop
  | .top, _ => True
  | .bot, _ => False
  | .atom r i, ρ => (R r).p (ρ i)
  | .eq i j, ρ => X.eqv (ρ i) (ρ j)
  | .and a b, ρ => sem a ρ ∧ sem b ρ
  | .or a b, ρ => sem a ρ ∨ sem b ρ
  | .imp a b, ρ => sem a ρ → sem b ρ
  | .all a, ρ => ∀ x, sem a (cons x ρ)
  | .ex a, ρ => ∃ x, sem a (cons x ρ)

def EnvEqv (ρ σ : Nat → X.car) : Prop := ∀ n, X.eqv (ρ n) (σ n)

theorem envEqv_symm {ρ σ : Nat → X.car} (h : EnvEqv ρ σ) : EnvEqv σ ρ := fun n => X.symm (h n)

theorem envEqv_cons (x : X.car) {ρ σ : Nat → X.car} (h : EnvEqv ρ σ) :
    EnvEqv (cons x ρ) (cons x σ)
  | 0 => X.refl x
  | n + 1 => h n

/-- every formula respects equivalence of environments: it is a subobject -/
theorem sem_resp : (φ : Form) → ∀ {ρ σ : Nat → X.car}, EnvEqv ρ σ → sem R φ ρ → sem R φ σ
  | .top, _, _, _, h => h
  | .bot, _, _, _, h => h
  | .atom r i, _, _, e, h => (R r).resp (e i) h
  | .eq i j, _, _, e, h => X.trans (X.symm (e i)) (X.trans h (e j))
  | .and a b, _, _, e, h => ⟨sem_resp a e h.1, sem_resp b e h.2⟩
  | .or a b, _, _, e, h => h.elim (fun x => Or.inl (sem_resp a e x)) (fun y => Or.inr (sem_resp b e y))
  | .imp a b, _, _, e, h => fun x => sem_resp b e (h (sem_resp a (envEqv_symm e) x))
  | .all a, _, _, e, h => fun x => sem_resp a (envEqv_cons x e) (h x)
  | .ex a, _, _, e, h => match h with | ⟨x, hx⟩ => ⟨x, sem_resp a (envEqv_cons x e) hx⟩

theorem sem_iff (φ : Form) {ρ σ : Nat → X.car} (e : EnvEqv ρ σ) : sem R φ ρ ↔ sem R φ σ :=
  ⟨sem_resp R φ e, sem_resp R φ (envEqv_symm e)⟩

/-- the environments as a setoid, and a formula as a subobject of it -/
def envS (X : Setd.{0}) : Setd.{0} where
  car := Nat → X.car
  eqv := @EnvEqv X
  refl ρ n := X.refl (ρ n)
  symm h := envEqv_symm h
  trans h k n := X.trans (h n) (k n)

def toPred (φ : Form) : Pred (envS X) := ⟨sem R φ, fun e h => sem_resp R φ e h⟩

theorem env_lift (f : Nat → Nat) (x : X.car) (ρ : Nat → X.car) :
    EnvEqv (fun n => cons x ρ (lift f n)) (cons x (fun n => ρ (f n)))
  | 0 => X.refl x
  | _ + 1 => X.refl _

theorem rename_sem : (φ : Form) → ∀ (f : Nat → Nat) (ρ : Nat → X.car),
    sem R (rename f φ) ρ ↔ sem R φ (fun n => ρ (f n))
  | .top, _, _ => Iff.rfl
  | .bot, _, _ => Iff.rfl
  | .atom _ _, _, _ => Iff.rfl
  | .eq _ _, _, _ => Iff.rfl
  | .and a b, f, ρ =>
    ⟨fun h => ⟨(rename_sem a f ρ).mp h.1, (rename_sem b f ρ).mp h.2⟩,
     fun h => ⟨(rename_sem a f ρ).mpr h.1, (rename_sem b f ρ).mpr h.2⟩⟩
  | .or a b, f, ρ =>
    ⟨fun h => h.elim (fun x => Or.inl ((rename_sem a f ρ).mp x)) (fun y => Or.inr ((rename_sem b f ρ).mp y)),
     fun h => h.elim (fun x => Or.inl ((rename_sem a f ρ).mpr x)) (fun y => Or.inr ((rename_sem b f ρ).mpr y))⟩
  | .imp a b, f, ρ =>
    ⟨fun h x => (rename_sem b f ρ).mp (h ((rename_sem a f ρ).mpr x)),
     fun h x => (rename_sem b f ρ).mpr (h ((rename_sem a f ρ).mp x))⟩
  | .all a, f, ρ =>
    ⟨fun h x => (sem_iff R a (env_lift f x ρ)).mp ((rename_sem a (lift f) (cons x ρ)).mp (h x)),
     fun h x => (rename_sem a (lift f) (cons x ρ)).mpr ((sem_iff R a (env_lift f x ρ)).mpr (h x))⟩
  | .ex a, f, ρ =>
    ⟨fun ⟨x, hx⟩ => ⟨x, (sem_iff R a (env_lift f x ρ)).mp ((rename_sem a (lift f) (cons x ρ)).mp hx)⟩,
     fun ⟨x, hx⟩ => ⟨x, (rename_sem a (lift f) (cons x ρ)).mpr ((sem_iff R a (env_lift f x ρ)).mpr hx)⟩⟩

theorem shift_sem (φ : Form) (x : X.car) (ρ : Nat → X.car) :
    sem R (shift φ) (cons x ρ) ↔ sem R φ ρ := rename_sem R φ Nat.succ (cons x ρ)

theorem env_inst (i : Nat) (ρ : Nat → X.car) :
    EnvEqv (fun n => ρ (instF i n)) (cons (ρ i) ρ)
  | 0 => X.refl _
  | _ + 1 => X.refl _

theorem inst_sem (φ : Form) (i : Nat) (ρ : Nat → X.car) :
    sem R (inst i φ) ρ ↔ sem R φ (cons (ρ i) ρ) :=
  (rename_sem R φ (instF i) ρ).trans (sem_iff R φ (env_inst i ρ))

end Sem

/-! ## Natural deduction -/

inductive In : Form → List Form → Prop where
  | here (a : Form) (l : List Form) : In a (a :: l)
  | there (a b : Form) (l : List Form) : In a l → In a (b :: l)

inductive Deriv : List Form → Form → Prop where
  | hyp {Γ a} : In a Γ → Deriv Γ a
  | topI {Γ} : Deriv Γ .top
  | botE {Γ a} : Deriv Γ .bot → Deriv Γ a
  | andI {Γ a b} : Deriv Γ a → Deriv Γ b → Deriv Γ (.and a b)
  | andE1 {Γ a b} : Deriv Γ (.and a b) → Deriv Γ a
  | andE2 {Γ a b} : Deriv Γ (.and a b) → Deriv Γ b
  | orI1 {Γ a b} : Deriv Γ a → Deriv Γ (.or a b)
  | orI2 {Γ a b} : Deriv Γ b → Deriv Γ (.or a b)
  | orE {Γ a b c} : Deriv Γ (.or a b) → Deriv (a :: Γ) c → Deriv (b :: Γ) c → Deriv Γ c
  | impI {Γ a b} : Deriv (a :: Γ) b → Deriv Γ (.imp a b)
  | impE {Γ a b} : Deriv Γ (.imp a b) → Deriv Γ a → Deriv Γ b
  | allI {Γ a} : Deriv (Γ.map shift) a → Deriv Γ (.all a)
  | allE {Γ a} (i : Nat) : Deriv Γ (.all a) → Deriv Γ (inst i a)
  | exI {Γ a} (i : Nat) : Deriv Γ (inst i a) → Deriv Γ (.ex a)
  | exE {Γ a c} : Deriv Γ (.ex a) → Deriv (a :: Γ.map shift) (shift c) → Deriv Γ c
  | eqRefl {Γ} (i : Nat) : Deriv Γ (.eq i i)
  | eqSubst {Γ a} (i j : Nat) : Deriv Γ (.eq i j) → Deriv Γ (inst i a) → Deriv Γ (inst j a)

section Sound
variable {X : Setd.{0}} (R : Nat → Pred X)

def AllTrue (ρ : Nat → X.car) : List Form → Prop
  | [] => True
  | a :: l => sem R a ρ ∧ AllTrue ρ l

theorem allTrue_in {ρ : Nat → X.car} {a : Form} {Γ : List Form} (m : In a Γ) :
    AllTrue R ρ Γ → sem R a ρ := by
  induction m with
  | here _ => exact fun h => h.1
  | there _ _ _ ih => exact fun h => ih h.2

theorem allTrue_shift (x : X.car) (ρ : Nat → X.car) :
    (Γ : List Form) → AllTrue R ρ Γ → AllTrue R (cons x ρ) (Γ.map shift)
  | [], _ => trivial
  | a :: l, h => ⟨(shift_sem R a x ρ).mpr h.1, allTrue_shift x ρ l h.2⟩

/-- soundness of intuitionistic natural deduction in the setoid topos -/
theorem soundness {Γ : List Form} {φ : Form} (d : Deriv Γ φ) :
    ∀ ρ : Nat → X.car, AllTrue R ρ Γ → sem R φ ρ := by
  induction d with
  | hyp m => exact fun ρ h => allTrue_in R m h
  | topI => exact fun _ _ => trivial
  | botE _ ih => exact fun ρ h => (ih ρ h).elim
  | andI _ _ iha ihb => exact fun ρ h => ⟨iha ρ h, ihb ρ h⟩
  | andE1 _ ih => exact fun ρ h => (ih ρ h).1
  | andE2 _ ih => exact fun ρ h => (ih ρ h).2
  | orI1 _ ih => exact fun ρ h => Or.inl (ih ρ h)
  | orI2 _ ih => exact fun ρ h => Or.inr (ih ρ h)
  | orE _ _ _ ih iha ihb =>
    exact fun ρ h => (ih ρ h).elim (fun x => iha ρ ⟨x, h⟩) (fun y => ihb ρ ⟨y, h⟩)
  | impI _ ih => exact fun ρ h x => ih ρ ⟨x, h⟩
  | impE _ _ ihf iha => exact fun ρ h => ihf ρ h (iha ρ h)
  | allI _ ih => exact fun ρ h x => ih (cons x ρ) (allTrue_shift R x ρ _ h)
  | allE i _ ih => exact fun ρ h => (inst_sem R _ i ρ).mpr (ih ρ h (ρ i))
  | exI i _ ih => exact fun ρ h => ⟨ρ i, (inst_sem R _ i ρ).mp (ih ρ h)⟩
  | exE _ _ ih ihc =>
    exact fun ρ h => match ih ρ h with
      | ⟨x, hx⟩ => (shift_sem R _ x ρ).mp (ihc (cons x ρ) ⟨hx, allTrue_shift R x ρ _ h⟩)
  | eqRefl i => exact fun ρ _ => X.refl (ρ i)
  | @eqSubst _ a i j _ _ ihe iha =>
    exact fun ρ h =>
      have e : EnvEqv (cons (ρ i) ρ) (cons (ρ j) ρ) := fun n =>
        match n with
        | 0 => ihe ρ h
        | _ + 1 => X.refl _
      (inst_sem R a j ρ).mpr (sem_resp R a e ((inst_sem R a i ρ).mp (iha ρ h)))

/-- consistency: ⊥ is not derivable from no hypotheses (any inhabited sort) -/
theorem consistent (R : Nat → Pred X) (x : X.car) : ¬ Deriv [] .bot :=
  fun d => soundness (X := X) R d (fun _ => x) trivial

end Sound

/-- an example derivation: ∀x (P x) → ∃x (P x), on an inhabited sort -/
theorem ex_all_ex : Deriv [] (.imp (.all (.atom 0 0)) (.ex (.atom 0 0))) :=
  .impI (.exI 0 (.allE 0 (.hyp (.here _ _))))

end IL

end Found

#print axioms Found.IL.sem_resp
#print axioms Found.IL.sem_iff
#print axioms Found.IL.toPred
#print axioms Found.IL.rename_sem
#print axioms Found.IL.shift_sem
#print axioms Found.IL.inst_sem
#print axioms Found.IL.allTrue_in
#print axioms Found.IL.allTrue_shift
#print axioms Found.IL.soundness
#print axioms Found.IL.consistent
#print axioms Found.IL.ex_all_ex

found/Ist1.lean

lean_all/found/Ist1.lean

/-
  T165, IST, part 1. found/Ist1 — Nelson's internal set theory as a
  first-order theory in ∈ (symbol 0) and st (symbol 1):
  * Kuratowski finiteness as an L0 formula FinF(z): z belongs (up to
    coextension) to every family of subsets of z that contains ∅ and is
    closed under adjoining one element of z;
  * in Aczel's universe V.{0}: FinF(z) holds iff z is list-finite (its
    members are, up to bisimilarity, the entries of a list);
  * IST: the axioms of ZFC (internal, found/NbgZ6 B1), transfer (for
    internal θ with standard parameters: ∀ˢᵗx θ → ∀x θ), idealization (for
    internal B with standard parameters:
    ∀ˢᵗ fin z ∃x ∀y∈z B(x,y) ↔ ∃x ∀ˢᵗy B(x,y)), standardization (for every
    formula Φ with standard parameters: ∀ˢᵗx ∃ˢᵗy ∀ˢᵗz (z∈y ↔ z∈x ∧ Φ)).

  Lean 4 core only, hand-written proofs, no axioms.
  CIC dependence: impredicative Prop (separation by list-finiteness).
-/
import found.ZFV
import found.Ack2

namespace Found

namespace IST

open WF FO FOCT NBGc RF AK

/-! ## Kuratowski finiteness -/

def finA1 : Fm := .all (.imp (m 0 1) (.all (.imp (m 0 1) (m 0 3))))
def finA2 : Fm := .ex (.conj (m 0 1) (.all (.imp (m 0 1) .bot)))
def finA3 : Fm := .all (.imp (m 0 1) (.all (.imp (m 0 3) (.ex (.conj (m 0 3)
  (.all (iffF (m 0 1) (.disj (m 0 3) (EqF 0 2)))))))))
def finA4 : Fm := .ex (.conj (m 0 1) (EqF 0 2))

/-- FinF(z), z = variable 0 -/
def FinF : Fm := .all (.imp finA1 (.imp finA2 (.imp finA3 finA4)))

/-- list-finite: the members are, up to bisimilarity, the entries of a list -/
def LFin (c : V.{0}) : Prop := ∃ l : List V.{0}, ∀ s, Mem s c ↔ ∃ t, t ∈ l ∧ Eq' s t

theorem lfin_resp : ∀ x y : V.{0}, Eq' x y → LFin x → LFin y := fun _ _ e ⟨l, h⟩ =>
  ⟨l, fun s => ⟨fun m => (h s).1 (mem_resp_right m (eq_symm _ _ e)), fun hs => mem_resp_right ((h s).2 hs) e⟩⟩

/-- adjoining one element -/
def adj (u t : V.{0}) : V.{0} := .sup (Option u.I) (fun o => match o with | none => t | some i => u.f i)

theorem mem_adj (u t s : V.{0}) : Mem s (adj u t) ↔ Mem s u ∨ Eq' s t :=
  ⟨fun ⟨o, e⟩ => match o, e with
    | none, e => .inr e
    | some i, e => .inl ⟨i, e⟩,
   fun h => h.elim (fun ⟨i, e⟩ => ⟨some i, e⟩) (fun e => ⟨none, e⟩)⟩

theorem lfin_V {c : V.{0}} (h : LFin c) (v : Nat → V.{0}) : Sat RV FinF (ext v c) := by
  intro S hA1 hA2 hA3
  match h with
  | ⟨l, hl⟩ =>
    -- every list of members of c is (up to bisimilarity) an element of S
    have build : ∀ l' : List V.{0}, (∀ t, t ∈ l' → Mem t c) →
        ∃ u, Mem u S ∧ ∀ s, Mem s u ↔ ∃ t, t ∈ l' ∧ Eq' s t := fun l' => by
      induction l' with
      | nil =>
        intro _
        match hA2 with
        | ⟨e, he, hne⟩ => exact ⟨e, he, fun s => ⟨fun m => (hne s m).elim, fun ⟨_, m, _⟩ => nomatch m⟩⟩
      | cons t l' ih =>
        intro ht
        match ih (fun t' m => ht t' (.tail _ m)) with
        | ⟨u, hu, hmem⟩ => match hA3 u hu t (ht t (.head _)) with
          | ⟨u', hu', hu'm⟩ => exact ⟨u', hu', fun s =>
            ⟨fun m => ((hu'm s).1 m).elim (fun ms => match (hmem s).1 ms with
                | ⟨t', ht', e⟩ => ⟨t', .tail _ ht', e⟩)
                (fun e => ⟨t, .head _, ceqC_iff.1 e⟩),
             fun ⟨t', m, e⟩ => (hu'm s).2 (by
                cases m with
                | head => exact .inr (ceqC_iff.2 e)
                | tail _ m' => exact .inl ((hmem s).2 ⟨t', m', e⟩))⟩⟩
    match build l (fun t m => (hl t).2 ⟨t, m, eq_refl _⟩) with
    | ⟨u, hu, hmem⟩ => exact ⟨u, hu, ceqC_iff.2 (extensionality u c (fun s =>
        ⟨fun m => (hl s).2 ((hmem s).1 m), fun m => (hmem s).2 ((hl s).1 m)⟩))⟩

theorem V_lfin {c : V.{0}} (v : Nat → V.{0}) (h : Sat RV FinF (ext v c)) : LFin c :=
  let S := IZF.sep (IZF.power c) LFin
  have hS : ∀ u, Mem u S ↔ (∀ s, Mem s u → Mem s c) ∧ LFin u := fun u =>
    (IZF.sep_spec (IZF.power c) LFin lfin_resp u).trans
      ⟨fun ⟨h1, h2⟩ => ⟨(IZF.power_spec c u).1 h1, h2⟩, fun ⟨h1, h2⟩ => ⟨(IZF.power_spec c u).2 h1, h2⟩⟩
  match h S (fun u hu s hs => ((hS u).1 hu).1 s hs)
      ⟨IZF.empty, (hS _).2 ⟨fun s hs => (IZF.empty_spec s hs).elim, [], fun s =>
        ⟨fun hs => (IZF.empty_spec s hs).elim, fun ⟨_, m, _⟩ => nomatch m⟩⟩, fun s hs => IZF.empty_spec s hs⟩
      (fun u hu t ht => match (hS u).1 hu with
        | ⟨hsub, l, hl⟩ => ⟨adj u t, (hS _).2 ⟨fun s hs => ((mem_adj u t s).1 hs).elim (hsub s)
              (fun e => mem_resp_left (eq_symm _ _ e) ht),
            t :: l, fun s => ⟨fun hs => ((mem_adj u t s).1 hs).elim
                (fun m => match (hl s).1 m with | ⟨t', ht', e⟩ => ⟨t', .tail _ ht', e⟩)
                (fun e => ⟨t, .head _, e⟩),
              fun ⟨t', m, e⟩ => (mem_adj u t s).2 (by
                cases m with
                | head => exact .inr e
                | tail _ m' => exact .inl ((hl s).2 ⟨t', m', e⟩))⟩⟩,
          fun s => ⟨fun hs => ((mem_adj u t s).1 hs).elim .inl (fun e => .inr (ceqC_iff.2 e)),
            fun hs => (mem_adj u t s).2 (hs.elim .inl (fun e => .inr (ceqC_iff.1 e)))⟩⟩) with
  | ⟨w, hw, e⟩ => lfin_resp w c (ceqC_iff.1 e) ((hS w).1 hw).2

/-! ## the axioms of IST -/

/-- transfer for θ(x, p₀ … p_{k−1}) -/
def transAx (θ : Fm) (k : Nat) : Fm := guardImp k (.imp (.all (.imp (Mv 0) θ)) (.all θ))

/-- idealization for B(y, x, p₀ … p_{k−1}) (variable 0 = y, 1 = x) -/
def idealAx (B : Fm) (k : Nat) : Fm :=
  guardImp k (iffF
    (.all (.imp (Mv 0) (.imp FinF (.ex (.all (.imp (m 0 2) (ren (up (up Nat.succ)) B)))))))
    (.ex (.all (.imp (Mv 0) B))))

def ρ2 : Nat → Nat
  | 0 => 0
  | n + 1 => n + 3

/-- standardization for Φ(z, p₀ … p_{k−1}) -/
def stdAx (Φ : Fm) (k : Nat) : Fm :=
  guardImp k (.all (.imp (Mv 0) (.ex (.conj (Mv 0) (.all (.imp (Mv 0) (iffF (m 0 1) (.conj (m 0 2) (ren ρ2 Φ)))))))))

inductive Ist : Fm → Prop where
  | zf {φ : Fm} : ZF B1 φ → Ist φ
  | tr (θ : Fm) (k : Nat) : L0 θ → FV (k + 1) θ → Ist (transAx θ k)
  | id (B : Fm) (k : Nat) : L0 B → FV (k + 2) B → Ist (idealAx B k)
  | std (Φ : Fm) (k : Nat) : FV (k + 1) Φ → Ist (stdAx Φ k)

theorem l0_FinF : L0 FinF := l0B_ok FinF rfl

end IST

end Found

#print axioms Found.IST.lfin_resp
#print axioms Found.IST.mem_adj
#print axioms Found.IST.lfin_V
#print axioms Found.IST.V_lfin
#print axioms Found.IST.l0_FinF

found/Ist2.lean

lean_all/found/Ist2.lean

/-
  T165, IST, part 2 (shared: fine ultrafilters). found/Ist2 — in the
  classical point, for every type J there is an ultrafilter on the finite
  lists of elements of J that is fine: for every b, the lists containing b
  form a member of the ultrafilter. Built from the prime filter theorem
  (found/PrimeFilter) on the Boolean algebra of predicates on lists; the
  lists containing b have the finite intersection property (a list
  containing finitely many given elements).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.PrimeFilter
import found.Los

namespace Found

namespace IST

open CP PF

/-- predicates on a type as a Heyting algebra -/
def PowHA (X : Type) : HA.{0} where
  car := X → Prop
  le A B := ∀ x, A x → B x
  le_refl _ _ h := h
  le_trans h1 h2 x h := h2 x (h1 x h)
  top := fun _ => True
  bot := fun _ => False
  meet A B := fun x => A x ∧ B x
  join A B := fun x => A x ∨ B x
  imp A B := fun x => A x → B x
  le_top _ _ _ := trivial
  bot_le _ _ h := h.elim
  meet_l _ _ _ h := h.1
  meet_r _ _ _ h := h.2
  meet_univ h1 h2 x h := ⟨h1 x h, h2 x h⟩
  join_l _ _ _ h := Or.inl h
  join_r _ _ _ h := Or.inr h
  join_univ h1 h2 x h := h.elim (h1 x) (h2 x)
  imp_adj := ⟨fun h x hc ha => h x ⟨hc, ha⟩, fun h x hca => h x hca.1 hca.2⟩

variable (K : Classical') (J : Type)

/-- the generators: lists containing b -/
def Bfine (A : List J → Prop) : Prop := ∃ b : J, A = fun l => b ∈ l

theorem mL_holds : (L : List (List J → Prop)) → (∀ A, A ∈ L → Bfine J A) →
    ∃ l0 : List J, ∀ A, A ∈ L → (∀ l, (∀ x, x ∈ l0 → x ∈ l) → A l)
  | [], _ => ⟨[], fun _ m => nomatch m⟩
  | A :: L, h => match mL_holds L (fun A' m => h A' (.tail _ m)), h A (.head _) with
    | ⟨l0, hl0⟩, ⟨b, e⟩ => ⟨b :: l0, fun A' m l hl => by
        cases m with
        | head => rw [e]; exact hl b (.head _)
        | tail _ m' => exact hl0 A' m' l (fun x hx => hl x (.tail _ hx))⟩

theorem mL_at (L : List (List J → Prop)) (l : List J) (h : ∀ A, A ∈ L → A l) : mL (PowHA (List J)) L l :=
  match L, h with
  | [], _ => trivial
  | A :: L, h => ⟨h A (.head _), mL_at L l (fun A' m => h A' (.tail _ m))⟩

theorem fip : ¬ Gen (PowHA (List J)) (Bfine J) (PowHA (List J)).bot := fun ⟨L, hL, hle⟩ =>
  match mL_holds J L hL with
  | ⟨l0, hl0⟩ => hle l0 (mL_at J L l0 (fun A m => hl0 A m l0 (fun _ h => h)))

/-- a fine ultrafilter on the finite lists of J -/
def fineU : Los.Ultra (List J) where
  mem A := F K (PowHA (List J)) (Bfine J) (PowHA (List J)).bot A
  univ := gen_top _ _
  up h hA := gen_up _ hA h
  inter hA hB := gen_meet _ hA hB
  proper := F_avoids K (PowHA (List J)) (Bfine J) (PowHA (List J)).bot (fip J)
  ultra A := F_prime K (PowHA (List J)) (Bfine J) (PowHA (List J)).bot (fip J) A (fun l => ¬ A l)
    (gen_up _ (gen_top _ _) (fun l _ => K.lem (A l)))

theorem fineU_fine (b : J) : (fineU K J).mem (fun l => b ∈ l) := gen_of _ (Or.inl ⟨b, rfl⟩)

end IST

end Found

#print axioms Found.IST.fip
#print axioms Found.IST.fineU
#print axioms Found.IST.fineU_fine

found/Ist3.lean

lean_all/found/Ist3.lean

/-
  T165, IST, part 3. found/Ist3 — the ultrapower model over a level:
  * a level V_α as a small structure: its index type with membership read
    through the level's tree; for L0 formulas, truth in the index structure
    is truth relativised to V_α;
  * the ultrapower of the index structure by a fine ultrafilter on finite
    lists of indices, with st = "almost everywhere equal to a constant";
  * on L0 formulas the ultrapower's truth is the ultraproduct truth of
    found/Los, hence (Łoś) almost-everywhere truth in V_α; constants give
    back truth in V_α; truth is invariant under almost-everywhere equality.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Ist1
import found.Ist2

namespace Found

namespace IST

open WF FO FOCT NBGc RF AK

section A

variable (α : V.{0})

abbrev Ix : Type := (Vlev α).I

def hh : Ix α → V.{0} := (Vlev α).f

def RI : Nat → List (Ix α) → Prop
  | 0, [] => False
  | 0, [_] => False
  | 0, [a, b] => Mem (hh α a) (hh α b)
  | 0, _ :: _ :: _ :: _ => False
  | _ + 1, _ => False

theorem satC_eq : (φ : Fm) → L0 φ → {v w : Nat → V.{0}} → (∀ n, Eq' (v n) (w n)) →
    SatC (SL α) φ v → SatC (SL α) φ w
  | .atom _ _, hl, v, w, h, s => match hl with
    | ⟨e0, a, b, e⟩ => by
      subst e0; subst e
      exact mem_resp_right (mem_resp_left (h a) s) (h b)
  | .bot, _, _, _, _, s => s
  | .conj a b, hl, _, _, h, s => ⟨satC_eq a hl.1 h s.1, satC_eq b hl.2 h s.2⟩
  | .disj a b, hl, _, _, h, s => s.elim (fun x => Or.inl (satC_eq a hl.1 h x)) (fun y => Or.inr (satC_eq b hl.2 h y))
  | .imp a b, hl, _, _, h, s => fun x => satC_eq b hl.2 h (s (satC_eq a hl.1 (fun n => eq_symm _ _ (h n)) x))
  | .all a, hl, _, _, h, s => fun d hd => satC_eq a hl (ext_eq h d) (s d hd)
  | .ex a, hl, _, _, h, s => match s with
    | ⟨d, hd, x⟩ => ⟨d, hd, satC_eq a hl (ext_eq h d) x⟩

theorem hh_ext (w : Nat → Ix α) (d : Ix α) :
    ∀ n, Eq' (hh α (ext w d n)) (ext (fun n => hh α (w n)) (hh α d) n)
  | 0 => eq_refl _
  | _ + 1 => eq_refl _

/-- the index structure is V_α -/
theorem satI_iff : (φ : Fm) → L0 φ → ∀ w : Nat → Ix α, Sat (RI α) φ w ↔ SatC (SL α) φ (fun n => hh α (w n))
  | .atom _ _, hl, w => match hl with
    | ⟨e0, a, b, e⟩ => by subst e0; subst e; exact Iff.rfl
  | .bot, _, _ => Iff.rfl
  | .conj a b, hl, w => ⟨fun s => ⟨(satI_iff a hl.1 w).1 s.1, (satI_iff b hl.2 w).1 s.2⟩,
      fun s => ⟨(satI_iff a hl.1 w).2 s.1, (satI_iff b hl.2 w).2 s.2⟩⟩
  | .disj a b, hl, w => ⟨fun s => s.elim (fun x => Or.inl ((satI_iff a hl.1 w).1 x)) (fun y => Or.inr ((satI_iff b hl.2 w).1 y)),
      fun s => s.elim (fun x => Or.inl ((satI_iff a hl.1 w).2 x)) (fun y => Or.inr ((satI_iff b hl.2 w).2 y))⟩
  | .imp a b, hl, w => ⟨fun s x => (satI_iff b hl.2 w).1 (s ((satI_iff a hl.1 w).2 x)),
      fun s x => (satI_iff b hl.2 w).2 (s ((satI_iff a hl.1 w).1 x))⟩
  | .all a, hl, w =>
    ⟨fun s x hx => match hx with
      | ⟨j, e⟩ => satC_eq α a hl (fun n => eq_trans _ _ _ (hh_ext α w j n) (ext_eq0 _ (eq_symm _ _ e) n))
          ((satI_iff a hl (ext w j)).1 (s j)),
     fun s d => (satI_iff a hl (ext w d)).2
        (satC_eq α a hl (fun n => eq_symm _ _ (hh_ext α w d n)) (s (hh α d) ⟨d, eq_refl _⟩))⟩
  | .ex a, hl, w =>
    ⟨fun ⟨d, s⟩ => ⟨hh α d, ⟨d, eq_refl _⟩,
        satC_eq α a hl (hh_ext α w d) ((satI_iff a hl (ext w d)).1 s)⟩,
     fun ⟨x, hx, s⟩ => match hx with
      | ⟨j, e⟩ => ⟨j, (satI_iff a hl (ext w j)).2
          (satC_eq α a hl (fun n => eq_trans _ _ _ (ext_eq0 _ e n) (eq_symm _ _ (hh_ext α w j n))) s)⟩⟩

/-! ## the ultrapower -/

variable (K : CP.Classical')

def UF : Los.Ultra (List (Ix α)) := fineU K (Ix α)

abbrev Xe : Type := List (Ix α) → Ix α

def Sim (f g : Xe α) : Prop := (UF α K).mem (fun i => Eq' (hh α (f i)) (hh α (g i)))

/-- standard: almost everywhere equal to a constant -/
def Std (f : Xe α) : Prop := ∃ a, Sim α K f (fun _ => a)

def RIST : Nat → List (Xe α) → Prop
  | 0, [] => False
  | 0, [_] => False
  | 0, [f, g] => (UF α K).mem (fun i => Mem (hh α (f i)) (hh α (g i)))
  | 0, _ :: _ :: _ :: _ => False
  | 1, [] => False
  | 1, [f] => Std α K f
  | 1, _ :: _ :: _ => False
  | _ + 2, _ => False

theorem sim_refl (f : Xe α) : Sim α K f f := (UF α K).up (fun _ _ => eq_refl _) (UF α K).univ
theorem sim_symm {f g : Xe α} (h : Sim α K f g) : Sim α K g f := (UF α K).up (fun _ e => eq_symm _ _ e) h
theorem sim_trans {f g k : Xe α} (h1 : Sim α K f g) (h2 : Sim α K g k) : Sim α K f k :=
  (UF α K).up (fun _ e => eq_trans _ _ _ e.1 e.2) ((UF α K).inter h1 h2)

def SimL : List (Xe α) → List (Xe α) → Prop
  | [], [] => True
  | a :: l, b :: k => Sim α K a b ∧ SimL l k
  | [], _ :: _ => False
  | _ :: _, [] => False

theorem RIST_resp : (r : Nat) → (l k : List (Xe α)) → SimL α K l k → RIST α K r l → RIST α K r k
  | 0, [], [], _, h => h
  | 0, [_], [_], _, h => h
  | 0, [_, _], [_, _], e, h => (UF α K).up (fun _ hh' => mem_resp_right (mem_resp_left hh'.1.1 hh'.2) hh'.1.2)
      ((UF α K).inter ((UF α K).inter e.1 e.2.1) h)
  | 0, _ :: _ :: _ :: _, _ :: _ :: _ :: _, _, h => h
  | 1, [], [], _, h => h
  | 1, [_], [_], e, h => match h with
    | ⟨a, ha⟩ => ⟨a, sim_trans α K (sim_symm α K e.1) ha⟩
  | 1, _ :: _ :: _, _ :: _ :: _, _, h => h
  | _ + 2, _, _, _, h => h
  | 0, [], _ :: _, e, _ => e.elim
  | 0, _ :: _, [], e, _ => e.elim
  | 0, [_], _ :: _ :: _, e, _ => e.2.elim
  | 0, _ :: _ :: _, [_], e, _ => e.2.elim
  | 0, [_, _], _ :: _ :: _ :: _, e, _ => e.2.2.elim
  | 0, _ :: _ :: _ :: _, [_, _], e, _ => e.2.2.elim
  | 1, [], _ :: _, e, _ => e.elim
  | 1, _ :: _, [], e, _ => e.elim
  | 1, [_], _ :: _ :: _, e, _ => e.2.elim
  | 1, _ :: _ :: _, [_], e, _ => e.2.elim

theorem simL_mapD {v w : Nat → Xe α} (h : ∀ n, Sim α K (v n) (w n)) : (l : List Nat) → SimL α K (mapD v l) (mapD w l)
  | [] => trivial
  | a :: l => ⟨h a, simL_mapD h l⟩

theorem ext_sim {v w : Nat → Xe α} (h : ∀ n, Sim α K (v n) (w n)) (d : Xe α) : ∀ n, Sim α K (ext v d n) (ext w d n)
  | 0 => sim_refl α K d
  | n + 1 => h n

/-- truth in the ultrapower is invariant under almost-everywhere equality -/
theorem satU_resp : (φ : Fm) → {v w : Nat → Xe α} → (∀ n, Sim α K (v n) (w n)) →
    Sat (RIST α K) φ v → Sat (RIST α K) φ w
  | .atom r l, _, _, h, s => RIST_resp α K r _ _ (simL_mapD α K h l) s
  | .bot, _, _, _, s => s
  | .conj a b, _, _, h, s => ⟨satU_resp a h s.1, satU_resp b h s.2⟩
  | .disj a b, _, _, h, s => s.elim (fun x => Or.inl (satU_resp a h x)) (fun y => Or.inr (satU_resp b h y))
  | .imp a b, _, _, h, s => fun x => satU_resp b h (s (satU_resp a (fun n => sim_symm α K (h n)) x))
  | .all a, _, _, h, s => fun d => satU_resp a (ext_sim α K h d) (s d)
  | .ex a, _, _, h, s => match s with
    | ⟨d, x⟩ => ⟨d, satU_resp a (ext_sim α K h d) x⟩

/-- on L0 formulas, the ultrapower is the ultraproduct of found/Los -/
theorem satU_psat : (φ : Fm) → L0 φ → ∀ w : Nat → Xe α,
    (Sat (RIST α K) φ w ↔ Los.psat (UF α K) (fun _ => Ix α) (fun _ => RI α) φ w)
  | .atom _ _, hl, w => match hl with
    | ⟨e0, a, b, e⟩ => by subst e0; subst e; exact Iff.rfl
  | .bot, _, _ => Iff.rfl
  | .conj a b, hl, w => ⟨fun s => ⟨(satU_psat a hl.1 w).1 s.1, (satU_psat b hl.2 w).1 s.2⟩,
      fun s => ⟨(satU_psat a hl.1 w).2 s.1, (satU_psat b hl.2 w).2 s.2⟩⟩
  | .disj a b, hl, w => ⟨fun s => s.elim (fun x => Or.inl ((satU_psat a hl.1 w).1 x)) (fun y => Or.inr ((satU_psat b hl.2 w).1 y)),
      fun s => s.elim (fun x => Or.inl ((satU_psat a hl.1 w).2 x)) (fun y => Or.inr ((satU_psat b hl.2 w).2 y))⟩
  | .imp a b, hl, w => ⟨fun s x => (satU_psat b hl.2 w).1 (s ((satU_psat a hl.1 w).2 x)),
      fun s x => (satU_psat b hl.2 w).2 (s ((satU_psat a hl.1 w).1 x))⟩
  | .all a, hl, w => ⟨fun s d => (satU_psat a hl (ext w d)).1 (s d), fun s d => (satU_psat a hl (ext w d)).2 (s d)⟩
  | .ex a, hl, w => ⟨fun ⟨d, s⟩ => ⟨d, (satU_psat a hl (ext w d)).1 s⟩, fun ⟨d, s⟩ => ⟨d, (satU_psat a hl (ext w d)).2 s⟩⟩

theorem lsat_sat (i : List (Ix α)) : (φ : Fm) → ∀ v : Nat → Ix α,
    (Los.sat (fun _ => Ix α) (fun _ => RI α) i φ v ↔ Sat (RI α) φ v)
  | .atom _ _, _ => Iff.rfl
  | .bot, _ => Iff.rfl
  | .conj a b, v => ⟨fun s => ⟨(lsat_sat i a v).1 s.1, (lsat_sat i b v).1 s.2⟩,
      fun s => ⟨(lsat_sat i a v).2 s.1, (lsat_sat i b v).2 s.2⟩⟩
  | .disj a b, v => ⟨fun s => s.elim (fun x => Or.inl ((lsat_sat i a v).1 x)) (fun y => Or.inr ((lsat_sat i b v).1 y)),
      fun s => s.elim (fun x => Or.inl ((lsat_sat i a v).2 x)) (fun y => Or.inr ((lsat_sat i b v).2 y))⟩
  | .imp a b, v => ⟨fun s x => (lsat_sat i b v).1 (s ((lsat_sat i a v).2 x)),
      fun s x => (lsat_sat i b v).2 (s ((lsat_sat i a v).1 x))⟩
  | .all a, v => ⟨fun s d => (lsat_sat i a (ext v d)).1 (s d), fun s d => (lsat_sat i a (ext v d)).2 (s d)⟩
  | .ex a, v => ⟨fun ⟨d, s⟩ => ⟨d, (lsat_sat i a (ext v d)).1 s⟩, fun ⟨d, s⟩ => ⟨d, (lsat_sat i a (ext v d)).2 s⟩⟩

/-- Łoś for the ultrapower, on L0 formulas -/
theorem los_U (e0 : Ix α) (φ : Fm) (hl : L0 φ) (w : Nat → Xe α) :
    Sat (RIST α K) φ w ↔ (UF α K).mem (fun i => Sat (RI α) φ (fun n => w n i)) :=
  (satU_psat α K φ hl w).trans ((Los.los (UF α K) (fun _ => Ix α) (fun _ => RI α) K (fun _ => e0) φ w).trans
    ⟨fun h => (UF α K).up (fun i s => (lsat_sat α i φ _).1 s) h, fun h => (UF α K).up (fun i s => (lsat_sat α i φ _).2 s) h⟩)

theorem mem_const (P : Prop) : (UF α K).mem (fun _ => P) ↔ P :=
  ⟨fun h => CP.dne K _ (fun hn => (UF α K).proper ((UF α K).up (fun _ hp => hn hp) h)),
   fun hp => (UF α K).up (fun _ _ => hp) (UF α K).univ⟩

/-- on constants, the ultrapower is V_α -/
theorem los_const (e0 : Ix α) (φ : Fm) (hl : L0 φ) (c : Nat → Ix α) :
    Sat (RIST α K) φ (fun n _ => c n) ↔ Sat (RI α) φ c :=
  (los_U α K e0 φ hl _).trans (mem_const α K _)

theorem std_const (a : Ix α) : Std α K (fun _ => a) := ⟨a, sim_refl α K _⟩

end A

end IST

end Found

#print axioms Found.IST.satC_eq
#print axioms Found.IST.satI_iff
#print axioms Found.IST.RIST_resp
#print axioms Found.IST.satU_resp
#print axioms Found.IST.satU_psat
#print axioms Found.IST.los_U
#print axioms Found.IST.los_const

found/Ist4.lean

lean_all/found/Ist4.lean

/-
  T165, IST, part 4. found/Ist4 — the axioms of IST in the ultrapower:
  * standard parameters may be replaced by constants;
  * internal axioms true in V_α are true in the ultrapower (Łoś);
  * transfer: Łoś on constants;
  * standardization: the external collection {z ∈ x | Φ(z)} of standard
    elements is a subset of a member of V_α, hence a member (fullness of
    the universe point);
  * idealization: (→) by fineness of the ultrafilter, choosing for every
    finite list a witness in V_α; (←) members of a standard finite set are
    standard (finitely many candidates, ultrafilter).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Ist3

namespace Found

namespace IST

open WF FO FOCT NBGc RF AK

section B

variable (α : V.{0}) (K : CP.Classical') (e0 : Ix α)

/-- constants for standard parameters -/
def cst (w : Nat → Xe α) (k : Nat) (hg : ∀ i, i < k → Std α K (w i)) (n : Nat) : Ix α :=
  match Nat.decLt n k with
  | isTrue h => K.ch (fun a => Sim α K (w n) (fun _ => a)) (hg n h)
  | isFalse _ => e0

theorem cst_sim (w : Nat → Xe α) (k : Nat) (hg : ∀ i, i < k → Std α K (w i)) :
    ∀ n, n < k → Sim α K (w n) (fun _ => cst α K e0 w k hg n) := fun n hn => by
  show Sim α K (w n) (fun _ => (match Nat.decLt n k with
    | isTrue h => K.ch (fun a => Sim α K (w n) (fun _ => a)) (hg n h)
    | isFalse _ => e0))
  match Nat.decLt n k with
  | isTrue h => exact K.ch_spec (fun a => Sim α K (w n) (fun _ => a)) (hg n h)
  | isFalse h' => exact absurd hn h'

/-- the valuation with constants below k -/
def wmix (w : Nat → Xe α) (k : Nat) (c : Nat → Ix α) (n : Nat) : Xe α :=
  match Nat.decLt n k with
  | isTrue _ => fun _ => c n
  | isFalse _ => w n

theorem wmix_sim (w : Nat → Xe α) (k : Nat) (c : Nat → Ix α) (hc : ∀ n, n < k → Sim α K (w n) (fun _ => c n)) :
    ∀ n, Sim α K (w n) (wmix α w k c n) := fun n => by
  show Sim α K (w n) (match Nat.decLt n k with
    | isTrue _ => fun _ => c n
    | isFalse _ => w n)
  match Nat.decLt n k with
  | isTrue h => exact hc n h
  | isFalse _ => exact sim_refl α K _

theorem wmix_agree (w : Nat → Xe α) (k : Nat) (c : Nat → Ix α) :
    ∀ n, n < k → wmix α w k c n = fun _ => c n := fun n hn => by
  show (match Nat.decLt n k with
    | isTrue _ => fun _ => c n
    | isFalse _ => w n) = fun _ => c n
  match Nat.decLt n k with
  | isTrue _ => rfl
  | isFalse h' => exact absurd hn h'

/-! ## internal axioms -/

include e0 in
theorem internal_U {φ : Fm} (hl : L0 φ) (hR : Refl α φ) (hV : ∀ v : Nat → V.{0}, Sat RV φ v) (w : Nat → Xe α) :
    Sat (RIST α K) φ w :=
  (los_U α K e0 φ hl w).2 ((UF α K).up (fun i _ => (satI_iff α φ hl _).2
    ((hR _ (fun n => ⟨w n i, eq_refl _⟩)).2 (hV _))) (UF α K).univ)

/-! ## constants and the index structure -/

theorem ext_const (c : Nat → Ix α) (b : Ix α) :
    ∀ n, ext (fun n (_ : List (Ix α)) => c n) (fun _ => b) n = fun _ => ext c b n
  | 0 => rfl
  | _ + 1 => rfl

theorem ext_at' (w : Nat → Xe α) (x : Xe α) (i : List (Ix α)) :
    ∀ n, (fun n => ext w x n i) n = ext (fun n => w n i) (x i) n
  | 0 => rfl
  | _ + 1 => rfl

/-- a standard-parameter formula: valuations below the parameters may be mixed -/
theorem sat_mix {φ : Fm} {k : Nat} (hf : FV (k + 1) φ) (w : Nat → Xe α) (c : Nat → Ix α)
    (hc : ∀ n, n < k → Sim α K (w n) (fun _ => c n)) (x : Xe α) :
    Sat (RIST α K) φ (ext w x) ↔ Sat (RIST α K) φ (ext (fun n _ => c n) x) :=
  have h1 : ∀ n, Sim α K (ext w x n) (ext (wmix α w k c) x n) := ext_sim α K (wmix_sim α K w k c hc) x
  have h2 : ∀ n, n < k + 1 → ext (wmix α w k c) x n = ext (fun n _ => c n) x n :=
    ext_agreeU (wmix_agree α w k c) x
  ⟨fun s => sat_agreeU (RIST α K) φ (k + 1) hf h2 (satU_resp α K φ h1 s),
   fun s => satU_resp α K φ (fun n => sim_symm α K (h1 n))
    (sat_agreeU (RIST α K) φ (k + 1) hf (fun n hn => (h2 n hn).symm) s)⟩

/-! ## transfer -/

include e0 in
theorem trans_U {θ : Fm} {k : Nat} (hl : L0 θ) (hf : FV (k + 1) θ) (w : Nat → Xe α) :
    Sat (RIST α K) (transAx θ k) w :=
  guard_intro (RIST α K) k _ w (fun hg hyp x =>
    let c := cst α K e0 w k hg
    have hc := cst_sim α K e0 w k hg
    have hb : ∀ b : Ix α, Sat (RI α) θ (ext c b) := fun b =>
      have s1 : Sat (RIST α K) θ (ext w (fun _ => b)) := hyp _ (std_const α K b)
      have s2 := (sat_mix α K hf w c hc _).1 s1
      (los_const α K e0 θ hl (ext c b)).1 ((sat_ext _ θ (ext_const α c b)).1 s2)
    (sat_mix α K hf w c hc x).2 ((los_U α K e0 θ hl _).2 ((UF α K).up (fun i _ =>
      (sat_ext (RI α) θ (ext_at' α (fun n _ => c n) x i)).2 (hb (x i))) (UF α K).univ)))

/-! ## standardization -/

theorem sim_const {a b : Ix α} : Sim α K (fun _ => a) (fun _ => b) ↔ Eq' (hh α a) (hh α b) := mem_const α K _

theorem ext_sim0 (w : Nat → Xe α) {x y : Xe α} (h : Sim α K x y) : ∀ n, Sim α K (ext w x n) (ext w y n)
  | 0 => h
  | _ + 1 => sim_refl α K _

theorem mem_std {z x : Xe α} {e a : Ix α} (hz : Sim α K z (fun _ => e)) (hx : Sim α K x (fun _ => a)) :
    RIST α K 0 [z, x] ↔ Mem (hh α e) (hh α a) :=
  ⟨fun h => (mem_const α K _).1 (RIST_resp α K 0 [z, x] [fun _ => e, fun _ => a] ⟨hz, hx, trivial⟩ h),
   fun h => RIST_resp α K 0 [fun _ => e, fun _ => a] [z, x] ⟨sim_symm α K hz, sim_symm α K hx, trivial⟩
     ((mem_const α K _).2 h)⟩

theorem std_U {Φ : Fm} {k : Nat} (hsub : ∀ x y, Mem y (Vlev α) → Subs x y → Mem x (Vlev α))
    (w : Nat → Xe α) : Sat (RIST α K) (stdAx Φ k) w :=
  guard_intro (RIST α K) k _ w (fun _ x hx => match hx with
    | ⟨a, ha⟩ =>
      have eΦ : ∀ y z, Sat (RIST α K) (ren ρ2 Φ) (ext (ext (ext w x) y) z) ↔ Sat (RIST α K) Φ (ext w z) :=
        fun _ _ => (sat_ren _ Φ ρ2 _).trans (sat_ext _ Φ (fun n => match n with
          | 0 => rfl
          | _ + 1 => rfl))
      let P : V.{0} → Prop := fun t => ∃ e : Ix α, Eq' t (hh α e) ∧ Sat (RIST α K) Φ (ext w (fun _ => e))
      have hP : IZF.Resp P := fun _ _ et ⟨e, he, s⟩ => ⟨e, eq_trans _ _ _ (eq_symm _ _ et) he, s⟩
      have hy0 : Mem (IZF.sep (hh α a) P) (Vlev α) :=
        hsub _ (hh α a) ⟨a, eq_refl _⟩ (fun s hs => ((IZF.sep_spec (hh α a) P hP s).1 hs).1)
      match hy0 with
      | ⟨b, eb⟩ =>
        have Pe : ∀ e : Ix α, P (hh α e) ↔ Sat (RIST α K) Φ (ext w (fun _ => e)) := fun e =>
          ⟨fun ⟨_, ee, s⟩ => satU_resp α K Φ (ext_sim0 α K w ((sim_const α K).2 (eq_symm _ _ ee))) s,
           fun s => ⟨e, eq_refl _, s⟩⟩
        ⟨fun _ => b, std_const α K b, fun z hz => match hz with
          | ⟨e, he⟩ =>
            have Φz : Sat (RIST α K) Φ (ext w z) ↔ Sat (RIST α K) Φ (ext w (fun _ => e)) :=
              ⟨satU_resp α K Φ (ext_sim0 α K w he), satU_resp α K Φ (ext_sim0 α K w (sim_symm α K he))⟩
            have my : RIST α K 0 [z, fun _ => b] ↔ Mem (hh α e) (hh α b) := mem_std α K he (sim_refl α K _)
            have mx : RIST α K 0 [z, x] ↔ Mem (hh α e) (hh α a) := mem_std α K he ha
            have key : Mem (hh α e) (hh α b) ↔ Mem (hh α e) (hh α a) ∧ Sat (RIST α K) Φ (ext w (fun _ => e)) :=
              ⟨fun h => match (IZF.sep_spec (hh α a) P hP _).1 (mem_resp_right h (eq_symm _ _ eb)) with
                | ⟨h1, h2⟩ => ⟨h1, (Pe e).1 h2⟩,
               fun ⟨h1, h2⟩ => mem_resp_right ((IZF.sep_spec (hh α a) P hP _).2 ⟨h1, (Pe e).2 h2⟩) eb⟩
            ⟨fun h => match key.1 (my.1 h) with
              | ⟨h1, h2⟩ => ⟨mx.2 h1, (eΦ _ z).2 (Φz.2 h2)⟩,
             fun ⟨h1, h2⟩ => my.2 (key.2 ⟨mx.1 h1, Φz.1 ((eΦ _ z).1 h2)⟩)⟩⟩)

/-! ## idealization -/

def mapH : List (Ix α) → List V.{0}
  | [] => []
  | j :: l => hh α j :: mapH l

theorem mem_mapH {t : V.{0}} : (l : List (Ix α)) → (t ∈ mapH α l ↔ ∃ j, j ∈ l ∧ t = hh α j)
  | [] => ⟨(fun m => nomatch m), (fun ⟨_, m, _⟩ => nomatch m)⟩
  | j :: l => ⟨fun m => by
      cases m with
      | head => exact ⟨j, .head _, rfl⟩
      | tail _ m' => exact match (mem_mapH l).1 m' with | ⟨j', h, e⟩ => ⟨j', .tail _ h, e⟩,
    fun ⟨j', h, e⟩ => by
      cases h with
      | head => rw [e]; exact .head _
      | tail _ m' => exact .tail _ ((mem_mapH l).2 ⟨j', m', e⟩)⟩

theorem funion {Q : V.{0} → List (Ix α) → Prop} : (L : List V.{0}) →
    (UF α K).mem (fun i => ∃ t, t ∈ L ∧ Q t i) → ∃ t, t ∈ L ∧ (UF α K).mem (Q t)
  | [], h => ((UF α K).proper ((UF α K).up (fun _ ⟨_, m, _⟩ => nomatch m) h)).elim
  | t :: L, h => ((UF α K).ultra (Q t)).elim (fun ht => ⟨t, .head _, ht⟩) (fun hn =>
      match funion L ((UF α K).up (fun _ hh' => match hh'.2 with
        | ⟨t', m, q⟩ => by
          cases m with
          | head => exact (hh'.1 q).elim
          | tail _ m' => exact ⟨t', m', q⟩) ((UF α K).inter hn h)) with
      | ⟨t', m, q⟩ => ⟨t', .tail _ m, q⟩)

theorem sat_mix2 {φ : Fm} {k : Nat} (hf : FV (k + 2) φ) (w : Nat → Xe α) (c : Nat → Ix α)
    (hc : ∀ n, n < k → Sim α K (w n) (fun _ => c n)) (x y : Xe α) :
    Sat (RIST α K) φ (ext (ext w x) y) ↔ Sat (RIST α K) φ (ext (ext (fun n _ => c n) x) y) :=
  have h1 : ∀ n, Sim α K (ext (ext w x) y n) (ext (ext (wmix α w k c) x) y n) :=
    ext_sim α K (ext_sim α K (wmix_sim α K w k c hc) x) y
  have h2 : ∀ n, n < k + 2 → ext (ext (wmix α w k c) x) y n = ext (ext (fun n _ => c n) x) y n :=
    ext_agreeU (ext_agreeU (wmix_agree α w k c) x) y
  ⟨fun s => sat_agreeU (RIST α K) φ (k + 2) hf h2 (satU_resp α K φ h1 s),
   fun s => satU_resp α K φ (fun n => sim_symm α K (h1 n))
    (sat_agreeU (RIST α K) φ (k + 2) hf (fun n hn => (h2 n hn).symm) s)⟩

theorem uus_lt (k : Nat) : ∀ n, n < k + 2 → up (up Nat.succ) n < k + 3
  | 0, _ => Nat.zero_lt_succ _
  | 1, _ => Nat.succ_lt_succ (Nat.zero_lt_succ _)
  | _ + 2, hn => Nat.succ_lt_succ (Nat.succ_lt_succ (Nat.succ_lt_succ (Nat.lt_of_succ_lt_succ (Nat.lt_of_succ_lt_succ hn))))

/-- ∃x ∀y (y ∈ z → B(y, x)), z = variable 0 -/
def psiI (B : Fm) : Fm := .ex (.all (.imp (m 0 2) (ren (up (up Nat.succ)) B)))

theorem eB_gen {D : Type _} (R : Nat → List D → Prop) (B : Fm) (w : Nat → D) (z x y : D) :
    Sat R (ren (up (up Nat.succ)) B) (ext (ext (ext w z) x) y) ↔ Sat R B (ext (ext w x) y) :=
  (sat_ren R B _ _).trans (sat_ext R B (fun n => match n with
    | 0 => rfl
    | 1 => rfl
    | _ + 2 => rfl))

include e0 in
theorem chainFin (rFin : Refl α FinF) (w : Nat → Xe α) (b : Ix α) :
    Sat (RIST α K) FinF (ext w (fun _ => b)) ↔ Sat RV FinF (ext (fun _ => hh α e0) (hh α b)) :=
  have hf1 : FV 1 FinF := fvB_ok 1 FinF rfl
  have a1 : Sat (RIST α K) FinF (ext w (fun _ => b)) ↔
      Sat (RIST α K) FinF (fun n _ => ext (fun _ => e0) b n) :=
    ⟨sat_agreeU _ FinF 1 hf1 (fun n hn => match n with
      | 0 => rfl
      | _ + 1 => absurd (Nat.lt_of_succ_lt_succ hn) (Nat.not_lt_zero _)),
     sat_agreeU _ FinF 1 hf1 (fun n hn => match n with
      | 0 => rfl
      | _ + 1 => absurd (Nat.lt_of_succ_lt_succ hn) (Nat.not_lt_zero _))⟩
  a1.trans ((los_const α K e0 FinF l0_FinF _).trans ((satI_iff α FinF l0_FinF _).trans
    ((rFin _ (fun n => ⟨ext (fun _ => e0) b n, eq_refl _⟩)).trans
      (sat_ext RV FinF (fun n => match n with
        | 0 => rfl
        | _ + 1 => rfl)))))

include e0 in
theorem ideal_U {B : Fm} {k : Nat} (hl : L0 B) (hf : FV (k + 2) B)
    (hfin : ∀ l : List (Ix α), ∃ b : Ix α, ∀ s, Mem s (hh α b) ↔ ∃ j, j ∈ l ∧ Eq' s (hh α j))
    (rFin : Refl α FinF) (w : Nat → Xe α) : Sat (RIST α K) (idealAx B k) w :=
  guard_intro (RIST α K) k _ w (fun hg =>
    let c := cst α K e0 w k hg
    have hc := cst_sim α K e0 w k hg
    have hlψ : L0 (psiI B) := ⟨l0_m 0 2, l0_ren _ B hl⟩
    have hfψ : FV (k + 1) (psiI B) := ⟨⟨Nat.zero_lt_succ _, Nat.succ_lt_succ (Nat.succ_lt_succ (Nat.zero_lt_succ _)), trivial⟩,
      fv_ren B (uus_lt k) hf⟩
    ⟨fun hL =>
      let bl : List (Ix α) → Ix α := fun l =>
        K.ch (fun b => ∀ s, Mem s (hh α b) ↔ ∃ j, j ∈ l ∧ Eq' s (hh α j)) (hfin l)
      have hbl : ∀ l s, Mem s (hh α (bl l)) ↔ ∃ j, j ∈ l ∧ Eq' s (hh α j) := fun l =>
        K.ch_spec (fun b => ∀ s, Mem s (hh α b) ↔ ∃ j, j ∈ l ∧ Eq' s (hh α j)) (hfin l)
      have fin_l : ∀ l, Sat (RIST α K) FinF (ext w (fun _ => bl l)) := fun l =>
        (chainFin α K e0 rFin w (bl l)).2 (lfin_V ⟨mapH α l, fun s => (hbl l s).trans
          ⟨fun ⟨j, m, e⟩ => ⟨hh α j, (mem_mapH α l).2 ⟨j, m, rfl⟩, e⟩,
           fun ⟨_, mt, e⟩ => match (mem_mapH α l).1 mt with
            | ⟨j, m, et⟩ => ⟨j, m, et ▸ e⟩⟩⟩ _)
      have ex_l : ∀ l, ∃ x : Ix α, ∀ y : Ix α, Mem (hh α y) (hh α (bl l)) → Sat (RI α) B (ext (ext c x) y) :=
        fun l => match hL (fun _ => bl l) (std_const α K _) (fin_l l) with
        | ⟨x, hx⟩ =>
          have s1 : Sat (RIST α K) (psiI B) (ext w (fun _ => bl l)) := ⟨x, hx⟩
          have s2 := (sat_mix α K hfψ w c hc _).1 s1
          have s3 := (los_const α K e0 (psiI B) hlψ (ext c (bl l))).1 ((sat_ext _ (psiI B) (ext_const α c (bl l))).1 s2)
          match s3 with
          | ⟨x', hx'⟩ => ⟨x', fun y hy => (eB_gen (RI α) B c (bl l) x' y).1 (hx' y hy)⟩
      let xs : Xe α := fun l => K.ch (fun x => ∀ y : Ix α, Mem (hh α y) (hh α (bl l)) → Sat (RI α) B (ext (ext c x) y)) (ex_l l)
      have hxs : ∀ l y, Mem (hh α y) (hh α (bl l)) → Sat (RI α) B (ext (ext c (xs l)) y) := fun l =>
        K.ch_spec (fun x => ∀ y : Ix α, Mem (hh α y) (hh α (bl l)) → Sat (RI α) B (ext (ext c x) y)) (ex_l l)
      ⟨xs, fun _ hy => match hy with
        | ⟨e, he⟩ =>
          have t1 : Sat (RIST α K) B (ext (ext (fun n _ => c n) xs) (fun _ => e)) :=
            (los_U α K e0 B hl _).2 ((UF α K).up (fun l (hel : e ∈ l) =>
              (sat_ext (RI α) B (v := ext (ext c (xs l)) e)
                (w := fun n => ext (ext (fun n _ => c n) xs) (fun _ => e) n l) (fun n => match n with
                | 0 => rfl
                | 1 => rfl
                | _ + 2 => rfl)).1 (hxs l e ((hbl l (hh α e)).2 ⟨e, hel, eq_refl _⟩))) (fineU_fine K (Ix α) e))
          satU_resp α K B (ext_sim0 α K _ (sim_symm α K he)) ((sat_mix2 α K hf w c hc xs (fun _ => e)).2 t1)⟩,
     fun ⟨x, hx⟩ z hz hfz => match hz with
      | ⟨a, ha⟩ =>
        have hfa : Sat (RIST α K) FinF (ext w (fun _ => a)) := satU_resp α K FinF (ext_sim0 α K w ha) hfz
        match V_lfin _ ((chainFin α K e0 rFin w a).1 hfa) with
        | ⟨L, hL⟩ => ⟨x, fun y hyz =>
          have m1 : (UF α K).mem (fun i => Mem (hh α (y i)) (hh α a)) :=
            RIST_resp α K 0 [y, z] [y, fun _ => a] ⟨sim_refl α K y, ha, trivial⟩ hyz
          match funion α K L ((UF α K).up (fun _ h => (hL _).1 h) m1) with
          | ⟨t, mt, ht⟩ =>
            have tV : Mem t (Vlev α) := trans_Vlev α (hh α a) t ((hL t).2 ⟨t, mt, eq_refl _⟩) ⟨a, eq_refl _⟩
            match tV with
            | ⟨e, et⟩ =>
              have hy : Std α K y := ⟨e, (UF α K).up (fun _ h => eq_trans _ _ _ h et) ht⟩
              (eB_gen (RIST α K) B w z x y).2 (hx y hy)⟩⟩)

end B



end IST

end Found

#print axioms Found.IST.cst_sim
#print axioms Found.IST.sat_mix
#print axioms Found.IST.internal_U
#print axioms Found.IST.trans_U
#print axioms Found.IST.std_U
#print axioms Found.IST.funion
#print axioms Found.IST.chainFin
#print axioms Found.IST.ideal_U

found/Ist5.lean

lean_all/found/Ist5.lean

/-
  T165, IST, part 5. found/Ist5 — IST in the universe-hierarchy point:
  * the reflecting level α = top (found/Refl3) contains every finite set of
    its members and every subset of its members;
  * a derivation from IST uses finitely many internal (ZFC) axioms; the
    level reflecting them, FinF and the conclusion gives, through the
    ultrapower by a fine ultrafilter, a model of every IST axiom used;
  * theorem: if IST proves an internal sentence φ, then φ is true in Aczel's
    universe V.{0}.

  Lean 4 core only, hand-written proofs, no axioms.
  Classical point: K (Type) and K1 (choice on Type 1), as parameters.
-/
import found.Ist4

namespace Found

namespace IST

open WF FO FOCT NBGc RF AK

/-! ## the reflecting level -/

section T

variable (K : CP.Classical') (K1 : Classical1) (Ψ : List Fm) (β0 : V.{0})

theorem top_mem (x : V.{0}) : Mem x (Vlev (top K K1 Ψ β0)) ↔ ∃ n, Subs x (Vlev (stages K K1 Ψ β0 n)) :=
  mem_Vlev Nat _ x

/-- finitely many members of the top level lie in a common stage -/
theorem common_list (l : List (Ix (top K K1 Ψ β0))) :
    ∃ M, ∀ j, j ∈ l → Mem (hh (top K K1 Ψ β0) j) (Vlev (stages K K1 Ψ β0 M)) := by
  induction l with
  | nil => exact ⟨0, fun _ m => nomatch m⟩
  | cons j l ih =>
    match ih, (top_mem K K1 Ψ β0 _).1 ⟨j, eq_refl _⟩ with
    | ⟨M, hM⟩, ⟨m, hm⟩ =>
      exact ⟨M + (m + 1), fun j' mj => by
        cases mj with
        | head =>
          rw [Nat.add_comm M (m + 1)]
          exact stage_mono K K1 Ψ β0 (m + 1) M _ (stage_up K K1 Ψ β0 m hm)
        | tail _ m' => exact stage_mono K K1 Ψ β0 M (m + 1) _ (hM j' m')⟩

def setOf {α : V.{0}} (l : List (Ix α)) : V.{0} := .sup {j : Ix α // j ∈ l} (fun p => hh α p.1)

theorem top_fin (l : List (Ix (top K K1 Ψ β0))) :
    ∃ b, ∀ s, Mem s (hh (top K K1 Ψ β0) b) ↔ ∃ j, j ∈ l ∧ Eq' s (hh (top K K1 Ψ β0) j) :=
  match common_list K K1 Ψ β0 l with
  | ⟨M, hM⟩ =>
    have hin : Mem (setOf l) (Vlev (top K K1 Ψ β0)) :=
      to_top K K1 Ψ β0 (M + 1) _ (stage_up K K1 Ψ β0 M (fun s (hs : Mem s (setOf l)) => match hs with
        | ⟨(p : {j : Ix (top K K1 Ψ β0) // j ∈ l}), e⟩ => mem_resp_left (eq_symm _ _ e) (hM p.1 p.2)))
    match hin with
    | ⟨b, eb⟩ => ⟨b, fun s =>
      ⟨fun h => match (mem_resp_right h (eq_symm _ _ eb) : Mem s (setOf l)) with
        | ⟨(p : {j : Ix (top K K1 Ψ β0) // j ∈ l}), e⟩ => ⟨p.1, p.2, e⟩,
       fun ⟨j, m, e⟩ => mem_resp_right (show Mem s (setOf l) from ⟨(⟨j, m⟩ : {j : Ix (top K K1 Ψ β0) // j ∈ l}), e⟩) eb⟩⟩

theorem lev_sub : (α : V.{0}) → ∀ x y, Mem y (Vlev α) → Subs x y → Mem x (Vlev α)
  | .sup I f, x, y, hy, hs => match (mem_Vlev I f y).1 hy with
    | ⟨i, hi⟩ => (mem_Vlev I f x).2 ⟨i, fun z hz => hi z (hs z hz)⟩

theorem top_refl {Φ : List Fm} (hΦ : ∀ φ, φ ∈ Φ → L0 φ) {φ : Fm} (hφ : φ ∈ Φ) :
    Refl (top K K1 (qbL Φ) β0) φ := fun v hv =>
  tv K _ φ (wit_of _ φ (fun ψ m => top_wit K K1 (qbL Φ) β0 (mem_qbL hφ m) (qb_l0 φ (hΦ φ hφ) ψ m))) v hv

end T

/-! ## the internal axioms are L0 -/

theorem l0_replZ {θ : Fm} (h : L0 θ) : L0 (replZ θ) :=
  ⟨⟨l0_m _ _, ⟨l0_ren _ θ h, l0_ren _ θ h⟩, l0_iff (l0_m _ _) (l0_m _ _)⟩,
   l0_iff (l0_m _ _) ⟨l0_m _ _, l0_ren _ θ h⟩⟩

theorem l0_foundZ {θ : Fm} (h : L0 θ) : L0 (foundZ θ) :=
  ⟨h, h, l0_m _ _, l0_ren _ θ h, trivial⟩

theorem zf_l0 {b : Fm} (h : ZF B1 b) : L0 b :=
  match h with
  | .inl e => by rw [e]; exact l0B_ok extAx rfl
  | .inr (.inl hb) => (base_B1 b hb).1
  | .inr (.inr (.inl ⟨θ, hl, e⟩)) => by rw [e]; exact l0_replZ hl
  | .inr (.inr (.inr ⟨θ, hl, e⟩)) => by rw [e]; exact l0_foundZ hl

/-! ## finitely many internal axioms -/

def IstIn (L : List Fm) (b : Fm) : Prop :=
  (ZF B1 b ∧ b ∈ L) ∨ (∃ θ k, L0 θ ∧ FV (k + 1) θ ∧ b = transAx θ k) ∨
    (∃ B k, L0 B ∧ FV (k + 2) B ∧ b = idealAx B k) ∨ (∃ Φ k, FV (k + 1) Φ ∧ b = stdAx Φ k)

theorem istIn_mono {L L' : List Fm} (hs : ∀ x, x ∈ L → x ∈ L') {b : Fm} (h : IstIn L b) : IstIn L' b :=
  match h with
  | .inl ⟨h1, h2⟩ => .inl ⟨h1, hs _ h2⟩
  | .inr r => .inr r

theorem ist_in {b : Fm} (h : Ist b) : ∃ L, (∀ x, x ∈ L → ZF B1 x) ∧ IstIn L b := by
  cases h with
  | zf hz => exact ⟨[_], (fun x m => by
      cases m with
      | head => exact hz
      | tail _ m' => exact nomatch m'), .inl ⟨hz, .head _⟩⟩
  | tr θ k hl hf => exact ⟨[], (fun _ m => nomatch m), .inr (.inl ⟨θ, k, hl, hf, rfl⟩)⟩
  | id B k hl hf => exact ⟨[], (fun _ m => nomatch m), .inr (.inr (.inl ⟨B, k, hl, hf, rfl⟩))⟩
  | std Φ k hf => exact ⟨[], (fun _ m => nomatch m), .inr (.inr (.inr ⟨Φ, k, hf, rfl⟩))⟩

theorem extractI : (Δ : List Fm) → CtxT Ist Δ → ∃ L, (∀ x, x ∈ L → ZF B1 x) ∧ ∀ δ, δ ∈ Δ → InT (IstIn L) δ
  | [], _ => ⟨[], (fun _ m => nomatch m), (fun _ m => nomatch m)⟩
  | δ :: Δ, h =>
    match extractI Δ (fun x m => h x (.tail _ m)), h δ (.head _) with
    | ⟨L1, h1, hΔ⟩, ⟨j, a, ha, e⟩ =>
      match ha with
      | .inl hl => ⟨L1, h1, fun x m => by
          cases m with
          | head => exact ⟨j, a, .inl hl, e⟩
          | tail _ m' => exact hΔ x m'⟩
      | .inr ⟨b, σ, hb, ea⟩ => match ist_in hb with
        | ⟨L2, h2, hin⟩ => ⟨gapp L2 L1, fun p m => (mem_gapp_inv L2 m).elim (h2 p) (h1 p), fun x m => by
            cases m with
            | head => exact ⟨j, a, .inr ⟨b, σ, istIn_mono (fun _ m => mem_gapp_l L1 m) hin, ea⟩, e⟩
            | tail _ m' => exact match hΔ x m' with
              | ⟨j', a', ha', e'⟩ => ⟨j', a', ha'.elim .inl (fun ⟨b', σ', hb', ea'⟩ =>
                  .inr ⟨b', σ', istIn_mono (fun _ m => mem_gapp_r L2 m) hb', ea'⟩), e'⟩⟩

/-- IST proves only truths of Aczel's universe (universe-hierarchy point) -/
theorem ist_meta (K : CP.Classical') (K1 : Classical1) {φ : Fm} (hl : L0 φ) (hf : FV 0 φ)
    (h : PC Ist [] φ) (v : Nat → V.{0}) : Sat RV φ v :=
  match h with
  | ⟨Δ, cΔ, d⟩ => match extractI Δ cΔ with
    | ⟨L, hL, hΔ⟩ =>
      have hΦ : ∀ ψ, ψ ∈ φ :: FinF :: L → L0 ψ := fun ψ m => by
        cases m with
        | head => exact hl
        | tail _ m' =>
          cases m' with
          | head => exact l0_FinF
          | tail _ m'' => exact zf_l0 (hL ψ m'')
      let α := top K K1 (qbL (φ :: FinF :: L)) (sgl vEmpty)
      have hE : Mem vEmpty (Vlev α) := to_top K K1 (qbL (φ :: FinF :: L)) (sgl vEmpty) 0 _ (mem_sgl_lev vEmpty)
      let e0 : Ix α := K.ch (fun j => Eq' vEmpty (hh α j)) hE
      have hb : ∀ b, IstIn L b → ∀ w, Sat (RIST α K) b w := fun b hb w => match hb with
        | .inl ⟨hz, m⟩ => internal_U α K e0 (zf_l0 hz) (top_refl K K1 (sgl vEmpty) hΦ (.tail _ (.tail _ m)))
            (RF.zfc_V K K1 b hz) w
        | .inr (.inl ⟨θ, k, h1, h2, e⟩) => by rw [e]; exact trans_U α K e0 h1 h2 w
        | .inr (.inr (.inl ⟨B, k, h1, h2, e⟩)) => by
          rw [e]
          exact ideal_U α K e0 h1 h2 (top_fin K K1 (qbL (φ :: FinF :: L)) (sgl vEmpty))
            (top_refl K K1 (sgl vEmpty) hΦ (.tail _ (.head _))) w
        | .inr (.inr (.inr ⟨Φ', k, _, e⟩)) => by rw [e]; exact std_U α K (lev_sub α) w
      have hmod : ∀ δ, δ ∈ Δ → ∀ w, Sat (RIST α K) δ w := fun δ m w => match hΔ δ m with
        | ⟨j, a, ha, e⟩ => by
          rw [e]
          exact clo_sat _ (fun w' => match ha with
            | .inl ⟨b, eb⟩ => by rw [eb]; exact K.lem (Sat (RIST α K) b w')
            | .inr ⟨b, σ, hbb, eb⟩ => by rw [eb]; exact (sat_ren _ b σ w').2 (hb b hbb _)) j w
      let w0 : Nat → Xe α := fun _ _ => e0
      have s : Sat (RIST α K) φ w0 := sound (RIST α K) d w0 (fun γ m =>
        (mem_app_inv' Δ [] m).elim (fun mΔ => hmod γ mΔ w0) (fun m' => nomatch m'))
      have s1 : Sat (RI α) φ (fun _ => e0) := (los_const α K e0 φ hl (fun _ => e0)).1 s
      have s2 := (top_refl K K1 (sgl vEmpty) hΦ (.head _) _ (fun _ => ⟨e0, eq_refl _⟩)).1 ((satI_iff α φ hl _).1 s1)
      sat_agreeU RV φ 0 hf (fun n hn => absurd hn (Nat.not_lt_zero n)) s2

end IST

end Found

#print axioms Found.IST.common_list
#print axioms Found.IST.top_fin
#print axioms Found.IST.lev_sub
#print axioms Found.IST.top_refl
#print axioms Found.IST.zf_l0
#print axioms Found.IST.extractI
#print axioms Found.IST.ist_meta

found/Justification.lean

lean_all/found/Justification.lean

/-
  T178. found/Justification — justification logic (Artemov's LP) as a point:
  Fitting models = worlds of the relation coordinate plus an evidence
  function; t : A holds at w iff t is evidence for A at w and A holds at all
  R-successors.
  * Application t:(A→B) → s:A → (t·s):B, sum s:A → (s+t):A, and reflection
    t:A → A on reflexive frames are valid (under the evidence closure
    conditions, which are the parameters' conditions).
  * Forgetful projection: t:A ⇒ □A — the line LP → S4 (Artemov's
    realization theorem, the converse, is cited).

  Lean 4 core only, hand-written proofs, no axioms.
-/

namespace Found

namespace Just

universe u

inductive Tm where
  | c : Nat → Tm
  | app : Tm → Tm → Tm
  | sum : Tm → Tm → Tm

variable {W : Type u} (R : W → W → Prop)

structure Ev (W : Type u) where
  E : Tm → (W → Prop) → W → Prop
  app : ∀ s t (A B : W → Prop) w, E s (fun v => A v → B v) w → E t A w → E (.app s t) B w
  sumL : ∀ s t A w, E s A w → E (.sum s t) A w
  sumR : ∀ s t A w, E t A w → E (.sum s t) A w

def box (A : W → Prop) (w : W) : Prop := ∀ v, R w v → A v

def jt (M : Ev W) (t : Tm) (A : W → Prop) (w : W) : Prop := M.E t A w ∧ box R A w

theorem application (M : Ev W) (s t : Tm) (A B : W → Prop) (w : W)
    (h1 : jt R M s (fun v => A v → B v) w) (h2 : jt R M t A w) : jt R M (.app s t) B w :=
  ⟨M.app s t A B w h1.1 h2.1, fun v r => h1.2 v r (h2.2 v r)⟩

theorem sum_left (M : Ev W) (s t : Tm) (A : W → Prop) (w : W) (h : jt R M s A w) : jt R M (.sum s t) A w :=
  ⟨M.sumL s t A w h.1, h.2⟩

theorem reflection (hr : ∀ w, R w w) (M : Ev W) (t : Tm) (A : W → Prop) (w : W) (h : jt R M t A w) : A w :=
  h.2 w (hr w)

/-- forgetful projection: justification ⇒ necessity -/
theorem forget (M : Ev W) (t : Tm) (A : W → Prop) (w : W) (h : jt R M t A w) : box R A w := h.2

end Just

end Found

#print axioms Found.Just.application
#print axioms Found.Just.sum_left
#print axioms Found.Just.reflection
#print axioms Found.Just.forget

found/K1a.lean

lean_all/found/K1a.lean

/-
  T96b, K1 part a. found/K1a — a universal μ-recursive program for λ-codes:
  U on [a, x], with a the code of a closed λ-term t, starts the machine of
  found/LamMu2–LamMu4 on t applied to the Church numeral of x and reads off
  the value; if t ⌜x⌝ →β* ⌜n⌝ then U evaluates to n (U_correct). Codes of
  plain terms are one-to-one.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.LamMu4

namespace Found

namespace K1A

open UL Rec RM CT3 LM

theorem cT_inj : (t u : Tm) → cT t = cT u → t = u
  | .var i, .var j, h => by
    have := congrArg sndU h; rw [show cT (.var i) = pair 0 i from rfl, show cT (.var j) = pair 0 j from rfl,
      sndU_pair, sndU_pair] at this; rw [this]
  | .lam a, .lam b, h => by
    have := congrArg sndU h; rw [show cT (.lam a) = pair 1 (cT a) from rfl, show cT (.lam b) = pair 1 (cT b) from rfl,
      sndU_pair, sndU_pair] at this; rw [cT_inj a b this]
  | .app a b, .app c d, h => by
    have := congrArg sndU h
    rw [show cT (.app a b) = pair 2 (pair (cT a) (cT b)) from rfl, show cT (.app c d) = pair 2 (pair (cT c) (cT d)) from rfl,
      sndU_pair, sndU_pair] at this
    have e1 := congrArg fstU this; have e2 := congrArg sndU this
    rw [fstU_pair, fstU_pair] at e1; rw [sndU_pair, sndU_pair] at e2
    rw [cT_inj a c e1, cT_inj b d e2]
  | .var i, .lam b, h => by have := congrArg fstU h; rw [show cT (.var i) = pair 0 i from rfl, show cT (.lam b) = pair 1 (cT b) from rfl, fstU_pair, fstU_pair] at this; exact Nat.noConfusion this
  | .var i, .app c d, h => by have := congrArg fstU h; rw [show cT (.var i) = pair 0 i from rfl, show cT (.app c d) = pair 2 (pair (cT c) (cT d)) from rfl, fstU_pair, fstU_pair] at this; exact Nat.noConfusion this
  | .lam a, .var j, h => by have := congrArg fstU h; rw [show cT (.lam a) = pair 1 (cT a) from rfl, show cT (.var j) = pair 0 j from rfl, fstU_pair, fstU_pair] at this; exact Nat.noConfusion this
  | .lam a, .app c d, h => by have := congrArg fstU h; rw [show cT (.lam a) = pair 1 (cT a) from rfl, show cT (.app c d) = pair 2 (pair (cT c) (cT d)) from rfl, fstU_pair, fstU_pair] at this; exact Nat.noConfusion (Nat.succ.inj this)
  | .app a b, .var j, h => by have := congrArg fstU h; rw [show cT (.app a b) = pair 2 (pair (cT a) (cT b)) from rfl, show cT (.var j) = pair 0 j from rfl, fstU_pair, fstU_pair] at this; exact Nat.noConfusion this
  | .app a b, .lam c, h => by have := congrArg fstU h; rw [show cT (.app a b) = pair 2 (pair (cT a) (cT b)) from rfl, show cT (.lam c) = pair 1 (cT c) from rfl, fstU_pair, fstU_pair] at this; exact Nat.noConfusion (Nat.succ.inj this)

/-- at [a, x]: the code of the machine state starting t applied to ⌜x⌝ -/
def x0U : PR := .comp pairP [cst 1, .comp pairP [.comp pairP [cst 2, .comp pairP [.proj 0, .comp churchP [.proj 1]]], cst 0]]

theorem x0U_eval (t : T []) (x : Nat) : eval x0U [cT (er t), x] = cS (.ret (er (T.app t (church x))) []) := by
  show eval pairP [eval (cst 1) _, eval pairP [eval pairP [eval (cst 2) _, eval pairP [cT (er t), eval churchP [x]]], eval (cst 0) _]] = _
  rw [churchP_eval, cst_eval, cst_eval, cst_eval, pairP_eval, pairP_eval, pairP_eval, pairP_eval, ← er_church (n := []) x]; rfl

/-- the universal program -/
def Umu : MR := .comp outM [emb x0U]

/-- the universal program computes the value of t ⌜x⌝ -/
theorem U_correct (t : T []) (x n : Nat) (h : Star Beta (T.app t (church x)) (church n)) : Ev Umu [cT (er t), x] n := by
  have hm := machine_value h
  obtain ⟨t0, ht⟩ := reach_iter hm
  let S0 : St := .ret (er (T.app t (church x))) []
  have h0 : eval haltF ((0 + t0) :: [cS S0]) = 0 := by
    rw [Nat.zero_add, haltF_eval]
    show den haltE (cS (miter t0 (.ret (er (T.app t (church x))) []))) = 0
    rw [ht]
    exact (halt_val (.done n)).2 ⟨n, rfl⟩
  have hs := gsearch_spec haltF [cS S0] t0 0 h0
  generalize gsearch _ 0 t0 = m at hs
  have hm0 := hs.2
  rw [haltF_eval] at hm0
  obtain ⟨n', hn'⟩ := (halt_val _).1 hm0
  have h1 : miter (t0 + m) S0 = .done n := by
    rw [miter_add]
    show miter m (miter t0 (.ret (er (T.app t (church x))) [])) = _
    rw [ht, done_stays]
  have h2 : miter (t0 + m) S0 = .done n' := by
    rw [Nat.add_comm, miter_add, hn', done_stays]
  have e : n' = n := St.done.inj (h2.symm.trans h1)
  have hx : eval runP' [m, cS S0] = cS (.done n) := by
    rw [runP'_eval, hn', e]
  have ho : eval (toPR (.snd .x)) [eval runP' [m, cS S0]] = n := by
    rw [hx, toPR_eval]; show sndU (pair 3 n) = n; rw [sndU_pair]
  have evOut : Ev outM [cS S0] n := by
    rw [← ho]
    exact .comp (.cons (.comp (.cons (.mu hs.1) (.cons (.proj 0 [cS S0]) (.nil _)))
      (emb_ev runP' _)) (.nil _)) (emb_ev _ _)
  have ex := emb_ev x0U [cT (er t), x]
  rw [x0U_eval] at ex
  exact .comp (.cons ex (.nil _)) evOut

end K1A

end Found

#print axioms Found.K1A.cT_inj
#print axioms Found.K1A.x0U_eval
#print axioms Found.K1A.U_correct

found/K1b.lean

lean_all/found/K1b.lean

/-
  T96b, K1 part b. found/K1b — λ-terms for Kleene's first algebra in the
  λ-numbering (a · x = n iff the closed term with code a applied to ⌜x⌝
  reduces to ⌜n⌝):
  * closed terms of one or two arguments applied to numerals reduce to the
    substitution of the numerals (app1, app2);
  * the universal λ-term Uλ = λλ. mr(U) (found/ChurchTuring3 on the
    universal μ-program of found/K1a): Uλ ⌜code t⌝ ⌜x⌝ →β* ⌜n⌝ whenever
    t ⌜x⌝ →β* ⌜n⌝;
  * primitive recursive functions as λ-terms of one and two arguments;
  * closed terms weakened into a context keep their code, and substituting
    into them changes nothing.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.K1a

namespace Found

namespace K1B

open UL Rec RM CT CT3 LM K1A

/-! ## applying closed terms to numerals -/

theorem app1 (M : T (ctx 1)) (a : Nat) : Star Beta (T.app (.lam M) (church a)) (sub (env 1 [a]) M) :=
  Star.one (by
    have e : inst M (church a) = sub (env 1 [a]) M := sub_ext M (fun v => match v with
      | .z => rfl
      | .s w => nomatch w)
    rw [← e]; exact .beta M (church a))

theorem app2 (M : T (ctx 2)) (a b : Nat) :
    Star Beta (T.app (T.app (.lam (.lam M)) (church a)) (church b)) (sub (env 2 [b, a]) M) := by
  have s1 : Beta (T.app (.lam (.lam M)) (church a)) (.lam (sub (liftS (single (church a))) M)) := .beta (.lam M) (church a)
  have e : inst (sub (liftS (single (church a))) M) (church b) = sub (env 2 [b, a]) M := by
    unfold inst
    rw [sub_sub]
    exact sub_ext M (fun v => match v with
      | .z => rfl
      | .s .z => by show sub (single (church b)) (ren V.s (church a)) = church a; rw [ren_church, sub_church]
      | .s (.s w) => nomatch w)
  exact .step (.appL _ s1) (Star.one (by rw [← e]; exact .beta _ (church b)))

/-! ## the universal λ-term -/

/-- the universal program with its arguments exchanged -/
def Uswap : MR := .comp Umu [.proj 1, .proj 0]

def Ulam : T [] := .lam (.lam (mr Uswap 2))

theorem U_lam (t : T []) (x n : Nat) (h : Star Beta (T.app t (church x)) (church n)) :
    Star Beta (T.app (T.app Ulam (church (cT (er t)))) (church x)) (church n) := by
  have ev : Ev Uswap [x, cT (er t)] n :=
    .comp (.cons (.proj 1 [x, cT (er t)]) (.cons (.proj 0 [x, cT (er t)]) (.nil _))) (U_correct t x n h)
  exact star_trans (app2 _ _ _) (cor ev 2 rfl)

/-! ## primitive recursive functions as λ-terms -/

def lam1 (f : PR) : T [] := .lam (CT.comp f 1)
def lam2 (f : PR) : T [] := .lam (.lam (CT.comp f 2))

theorem lam1_red (f : PR) (a : Nat) : Star Beta (T.app (lam1 f) (church a)) (church (eval f [a])) :=
  star_trans (app1 _ a) (correct f 1 [a] rfl)

/-- note the order: lam2 f ⌜a⌝ ⌜b⌝ computes f on [b, a] -/
theorem lam2_red (f : PR) (a b : Nat) : Star Beta (T.app (T.app (lam2 f) (church a)) (church b)) (church (eval f [b, a])) :=
  star_trans (app2 _ a b) (correct f 2 [b, a] rfl)

/-! ## weakening closed terms -/

def emptyV {Δ : List Unit} (v : V []) : V Δ := nomatch v

def wk {Δ : List Unit} (t : T []) : T Δ := ren emptyV t

theorem er_ren : {Γ : List Unit} → (t : T Γ) → ∀ {Δ : List Unit} (r : V Γ → V Δ), (∀ v, vi (r v) = vi v) → er (ren r t) = er t
  | _, .var v, _, _, h => by show Tm.var (vi _) = Tm.var (vi v); rw [h v]
  | _, .app a b, _, r, h => by show Tm.app (er (ren r a)) (er (ren r b)) = Tm.app (er a) (er b); rw [er_ren a r h, er_ren b r h]
  | _, .lam t, _, r, h => by
    show Tm.lam (er (ren (lift r) t)) = Tm.lam (er t)
    rw [er_ren t (lift r) (fun v => match v with
      | .z => rfl
      | .s w => by show vi (r w) + 1 = vi w + 1; rw [h w])]

theorem er_wk {Δ : List Unit} (t : T []) : er (wk (Δ := Δ) t) = er t := er_ren t _ (fun v => nomatch v)

theorem sub_wk {Δ Δ' : List Unit} (σ : V Δ → T Δ') (t : T []) : sub σ (wk t) = wk t := by
  unfold wk
  rw [sub_ren]
  have e1 : sub (fun x => σ (emptyV x)) t = sub (fun x => T.var (emptyV x)) t := sub_ext t (fun v => nomatch v)
  have e2 : sub (fun x => T.var (emptyV (Δ := Δ') x)) t = ren emptyV t := by rw [← sub_ren t T.var emptyV, sub_var]
  rw [e1, e2]

theorem ren_fix : {Γ : List Unit} → (t : T Γ) → (r : V Γ → V Γ) → (∀ v, r v = v) → ren r t = t
  | _, .var v, _, h => congrArg T.var (h v)
  | _, .app a b, r, h => cong2 T.app (ren_fix a r h) (ren_fix b r h)
  | _, .lam t, r, h => congrArg T.lam (ren_fix t (lift r) (fun v => match v with
      | .z => rfl
      | .s w => congrArg V.s (h w)))

theorem wk_closed (t : T []) : wk (Δ := []) t = t := ren_fix t emptyV (fun v => nomatch v)

end K1B

end Found

#print axioms Found.K1B.app1
#print axioms Found.K1B.app2
#print axioms Found.K1B.U_lam
#print axioms Found.K1B.lam2_red
#print axioms Found.K1B.er_wk
#print axioms Found.K1B.sub_wk
#print axioms Found.K1B.wk_closed

found/K1c.lean

lean_all/found/K1c.lean

/-
  T96b, K1 part c. found/K1c — Kleene's first algebra is a partial
  combinatory algebra. Elements are numbers; a · x = n when a is the code of
  a closed λ-term t with t ⌜x⌝ →β* ⌜n⌝ (an acceptable numbering of the
  partial recursive functions, by the Church–Turing line λ ↔ μ). Application
  is single-valued by Church–Rosser; k sends a to the code of λ_.⌜a⌝, and s
  sends a, b to the code of λx. U (U ⌜a⌝ x) (U ⌜b⌝ x) with the universal
  λ-term U, computed by primitive recursive code builders.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.K1b
import found.Eff11

namespace Found

namespace K1C

open UL Rec RM CT CT3 LM K1A K1B

/-- application in the λ-numbering -/
def kap (a x n : Nat) : Prop := ∃ t : T [], cT (er t) = a ∧ Star Beta (T.app t (church x)) (church n)

theorem church_inj {Γ : List Unit} {a b : Nat} (h : (church a : T Γ) = church b) : a = b :=
  churchN_inj (by rw [← er_church (n := Γ) a, ← er_church (n := Γ) b, h])

theorem kap_det {a x n n' : Nat} (h : kap a x n) (h' : kap a x n') : n = n' :=
  match h, h' with
  | ⟨t, e, r⟩, ⟨t', e', r'⟩ => by
    have tt : t = t' := er_inj t t' (cT_inj _ _ (e.trans e'.symm))
    subst tt
    exact church_inj (nf_unique r r' (church_normal n) (church_normal n'))

/-! ## code builders -/

def appP (f u : PR) : PR := .comp pairP [cst 2, .comp pairP [f, u]]
def lamP (b : PR) : PR := .comp pairP [cst 1, b]

theorem appP_eval (f u : PR) (args : List Nat) : eval (appP f u) args = pair 2 (pair (eval f args) (eval u args)) := by
  show eval pairP [eval (cst 2) args, eval pairP [eval f args, eval u args]] = _
  rw [cst_eval, pairP_eval, pairP_eval]

theorem lamP_eval (b : PR) (args : List Nat) : eval (lamP b) args = pair 1 (eval b args) := by
  show eval pairP [eval (cst 1) args, eval b args] = _
  rw [cst_eval, pairP_eval]

/-! ## k -/

def fK : PR := lamP churchP

def kT : T [] := lam1 fK

theorem k_ok (a : Nat) : ∃ c, kap (cT (er kT)) a c ∧ ∀ b, kap c b a := by
  refine ⟨cT (er (T.lam (church a) : T [])), ⟨kT, rfl, ?_⟩, fun b => ⟨T.lam (church a), rfl, ?_⟩⟩
  · have e : eval fK [a] = cT (er (T.lam (church a) : T [])) := by
      unfold fK; rw [lamP_eval, churchP_eval]; show _ = pair 1 (cT (er (church a : T [()]))); rw [er_church]
    rw [← e]; exact lam1_red fK a
  · exact Star.one (by
      have : inst (church a : T [()]) (church b) = church a := sub_church _ a
      rw [← this]; exact .beta _ _)

/-! ## s -/

/-- the code of the variable of a one-variable context -/
def cV : Nat := pair 0 0

def cU : Nat := cT (er Ulam)

/-- λx. U (U ⌜a⌝ x) (U ⌜b⌝ x) -/
def Sab (a b : Nat) : T [] :=
  .lam (.app (.app (wk Ulam) (.app (.app (wk Ulam) (church a)) (.var .z))) (.app (.app (wk Ulam) (church b)) (.var .z)))

/-- its code, on [b, a] -/
def gP : PR :=
  lamP (appP (appP (cst cU) (appP (appP (cst cU) (.comp churchP [.proj 1])) (cst cV)))
    (appP (appP (cst cU) (.comp churchP [.proj 0])) (cst cV)))

theorem gP_eval (a b : Nat) : eval gP [b, a] = cT (er (Sab a b)) := by
  unfold gP
  rw [lamP_eval, appP_eval, appP_eval, appP_eval, appP_eval, appP_eval, appP_eval, cst_eval, cst_eval]
  show pair 1 (pair 2 (pair (pair 2 (pair cU (pair 2 (pair (pair 2 (pair cU (eval churchP [a]))) cV))))
    (pair 2 (pair (pair 2 (pair cU (eval churchP [b]))) cV)))) = _
  rw [churchP_eval, churchP_eval]
  show _ = pair 1 (pair 2 (pair (pair 2 (pair (cT (er (wk (Δ := [()]) Ulam))) (pair 2 (pair (pair 2 (pair (cT (er (wk (Δ := [()]) Ulam)))
    (cT (er (church a : T [()]))))) (cT (Tm.var 0)))))) (pair 2 (pair (pair 2 (pair (cT (er (wk (Δ := [()]) Ulam))) (cT (er (church b : T [()])))))
      (cT (Tm.var 0))))))
  rw [er_wk, er_church, er_church]; rfl

def Gl : T [] := lam2 gP

/-- λb. G ⌜a⌝ b -/
def Ta (a : Nat) : T [] := .lam (.app (.app (wk Gl) (church a)) (.var .z))

def hP : PR := lamP (appP (appP (cst (cT (er Gl))) churchP) (cst cV))

theorem hP_eval (a : Nat) : eval hP [a] = cT (er (Ta a)) := by
  unfold hP
  rw [lamP_eval, appP_eval, appP_eval, cst_eval, cst_eval, churchP_eval]
  show _ = pair 1 (pair 2 (pair (pair 2 (pair (cT (er (wk (Δ := [()]) Gl))) (cT (er (church a : T [()]))))) (cT (Tm.var 0))))
  rw [er_wk, er_church]; rfl

def sT : T [] := lam1 hP

theorem inst_app {t : T [()]} {u : T []} {v : T []} (e : inst t u = v) : Star Beta (T.app (.lam t) u) v :=
  Star.one (by rw [← e]; exact .beta t u)

theorem s_ok (a : Nat) : ∃ c, kap (cT (er sT)) a c ∧ ∀ b, ∃ d, kap c b d ∧
    ∀ x p q v, kap a x p → kap b x q → kap p q v → kap d x v := by
  refine ⟨cT (er (Ta a)), ⟨sT, rfl, by rw [← hP_eval]; exact lam1_red hP a⟩, fun b => ⟨cT (er (Sab a b)), ⟨Ta a, rfl, ?_⟩, ?_⟩⟩
  · -- Ta a ⌜b⌝ →β G ⌜a⌝ ⌜b⌝ →β* ⌜code (Sab a b)⌝
    have e : inst (.app (.app (wk Gl) (church a)) (.var .z) : T [()]) (church b) = T.app (T.app Gl (church a)) (church b) := by
      show T.app (T.app (sub (single (church b)) (wk Gl)) (sub (single (church b)) (church a))) (church b) = _
      rw [sub_wk, sub_church, wk_closed]
    exact star_trans (inst_app e) (by rw [← gP_eval]; exact lam2_red gP a b)
  · intro x p q v ⟨ta, ea, ra⟩ ⟨tb, eb, rb⟩ ⟨tp, ep, rp⟩
    refine ⟨Sab a b, rfl, ?_⟩
    have e : inst (.app (.app (wk Ulam) (.app (.app (wk Ulam) (church a)) (.var .z))) (.app (.app (wk Ulam) (church b)) (.var .z)) : T [()])
        (church x) = T.app (T.app Ulam (T.app (T.app Ulam (church a)) (church x))) (T.app (T.app Ulam (church b)) (church x)) := by
      show T.app (T.app (sub (single (church x)) (wk Ulam)) (T.app (T.app (sub (single (church x)) (wk Ulam))
        (sub (single (church x)) (church a))) (church x))) (T.app (T.app (sub (single (church x)) (wk Ulam))
        (sub (single (church x)) (church b))) (church x)) = _
      rw [sub_wk, sub_church, sub_church, wk_closed]
    have up : Star Beta (T.app (T.app Ulam (church a)) (church x)) (church p) := by rw [← ea]; exact U_lam ta x p ra
    have uq : Star Beta (T.app (T.app Ulam (church b)) (church x)) (church q) := by rw [← eb]; exact U_lam tb x q rb
    have uv : Star Beta (T.app (T.app Ulam (church p)) (church q)) (church v) := by rw [← ep]; exact U_lam tp q v rp
    exact star_trans (inst_app e) (star_trans (star_appL _ (star_appR _ up)) (star_trans (star_appR _ uq) uv))

/-- Kleene's first algebra -/
def K1 : Eff.PCA where
  A := Nat
  ap := kap
  det := fun h h' => kap_det h h'
  k := cT (er kT)
  s := cT (er sT)
  k1 := k_ok
  s1 := s_ok

end K1C

end Found

#print axioms Found.K1C.kap_det
#print axioms Found.K1C.k_ok
#print axioms Found.K1C.gP_eval
#print axioms Found.K1C.s_ok
#print axioms Found.K1C.K1

found/K1d.lean

lean_all/found/K1d.lean

/-
  T96b, K1 part d. found/K1d — the effective topos: the realizability
  topos over Kleene's first algebra K1 (found/K1c), with the topos structure
  of found/Eff1–Eff11 specialised: a terminal object, binary products and
  equalizers (finite limits), and power objects classifying relations.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.K1c

namespace Found

namespace Eff

open K1C

/-- the effective topos: objects and arrows over K1 -/
abbrev EffObj := Obj K1
abbrev EffFn (A B : EffObj) := Fn A B

/-- the effective topos has finite limits and power objects -/
theorem effective_topos :
    (∀ (A : EffObj) (G : EffFn A (one K1)), FEq G (bang A)) ∧
    (∀ (A B C : EffObj) (F : EffFn C A) (G : EffFn C B),
      FEq (compF (pairF F G) (pi1 A B)) F ∧ FEq (compF (pairF F G) (pi2 A B)) G ∧
      ∀ K : EffFn C (prodO A B), FEq (compF K (pi1 A B)) F → FEq (compF K (pi2 A B)) G → FEq K (pairF F G)) ∧
    (∀ (A B C : EffObj) (F G : EffFn A B) (H : EffFn C A) (heq : FEq (compF H F) (compF H G)),
      FEq (compF (incl F G) F) (compF (incl F G) G) ∧ FEq (compF (liftK F G H heq) (incl F G)) H ∧
      ∀ K : EffFn C (eqO F G), FEq (compF K (incl F G)) H → FEq K (liftK F G H heq)) ∧
    (∀ (B A : EffObj) (R : Rel B A),
      (Ent (fun p : B.X × A.X => R.R p.1 p.2) (pbR (chiF R)) ∧ Ent (pbR (chiF R)) (fun p : B.X × A.X => R.R p.1 p.2)) ∧
      ∀ χ : EffFn B (powO A), Ent (fun p : B.X × A.X => R.R p.1 p.2) (pbR χ) → Ent (pbR χ) (fun p : B.X × A.X => R.R p.1 p.2) →
        FEq χ (chiF R)) :=
  ⟨fun _ G => bang_uniq G,
   fun _ _ _ F G => ⟨beta1 F G, beta2 F G, fun K h1 h2 => prod_uniq F G K h1 h2⟩,
   fun _ _ _ F G H heq => ⟨incl_eq F G, lift_comm F G H heq, fun K hK => lift_uniq F G H heq K hK⟩,
   fun _ _ R => ⟨chi_pb R, fun χ h1 h2 => chi_uniq R χ h1 h2⟩⟩

end Eff

end Found

#print axioms Found.Eff.effective_topos

found/KP.lean

lean_all/found/KP.lean

/-
  T90. found/KP — Kripke–Platek as a point: the Δ₀ fragment of the IZF point.

  * Δ₀ formulas over V with variables (de Bruijn environment), bounded
    quantifiers ∀y∈x, ∃y∈x, ∈ and ≈ atoms.
  * Every Δ₀ formula respects ≈ in its free variables (needed for
    separation), proved once by recursion on formulas.
  * Δ₀-separation is the instance of T88's separation (not re-proved);
    Δ₀-collection is T88's Σ-collection restricted; ∈-induction (T109).
  So KP ⊂ CZF ⊂ IZF (as conditions on which formulas may be used), the
  known inclusion lines of the map.

  Lean 4 core only, hand-written proofs, no axioms.
  CIC dependence: ≈ on V is Prop-valued (T109).
-/
import found.IZF


universe u

namespace Found

namespace KP

open WF

inductive D0 where
  | mem : Nat → Nat → D0
  | eq : Nat → Nat → D0
  | bot : D0
  | and : D0 → D0 → D0
  | or : D0 → D0 → D0
  | imp : D0 → D0 → D0
  | ball : Nat → D0 → D0      -- ∀ y ∈ x_n, φ  (y = variable 0 inside)
  | bex : Nat → D0 → D0       -- ∃ y ∈ x_n, φ

def cons (x : V.{u}) (ρ : Nat → V.{u}) : Nat → V.{u}
  | 0 => x
  | n + 1 => ρ n

def sat : D0 → (Nat → V.{u}) → Prop
  | .mem i j, ρ => Mem (ρ i) (ρ j)
  | .eq i j, ρ => Eq' (ρ i) (ρ j)
  | .bot, _ => False
  | .and a b, ρ => sat a ρ ∧ sat b ρ
  | .or a b, ρ => sat a ρ ∨ sat b ρ
  | .imp a b, ρ => sat a ρ → sat b ρ
  | .ball n a, ρ => ∀ j : (ρ n).I, sat a (cons ((ρ n).f j) ρ)
  | .bex n a, ρ => ∃ j : (ρ n).I, sat a (cons ((ρ n).f j) ρ)

def EnvEq (ρ σ : Nat → V.{u}) : Prop := ∀ n, Eq' (ρ n) (σ n)

theorem envEq_cons {ρ σ : Nat → V.{u}} (h : EnvEq ρ σ) {x y : V.{u}} (e : Eq' x y) : EnvEq (cons x ρ) (cons y σ)
  | 0 => e
  | _ + 1 => h _

theorem envEq_symm {ρ σ : Nat → V.{u}} (h : EnvEq ρ σ) : EnvEq σ ρ := fun n => eq_symm _ _ (h n)

theorem ball_tr : {x y : V.{u}} → Eq' x y → (P Q : V.{u} → Prop) → (∀ u v, Eq' u v → P u → Q v) →
    (∀ j : x.I, P (x.f j)) → ∀ k : y.I, Q (y.f k)
  | .sup _ _, .sup _ _, ⟨_, e2⟩, _, _, t, h, k => match e2 k with | ⟨j, e⟩ => t _ _ e (h j)

theorem bex_tr : {x y : V.{u}} → Eq' x y → (P Q : V.{u} → Prop) → (∀ u v, Eq' u v → P u → Q v) →
    (∃ j : x.I, P (x.f j)) → ∃ k : y.I, Q (y.f k)
  | .sup _ _, .sup _ _, ⟨e1, _⟩, _, _, t, ⟨j, h⟩ => match e1 j with | ⟨k, e⟩ => ⟨k, t _ _ e h⟩

/-- Δ₀ formulas respect ≈ -/
theorem sat_resp : (φ : D0) → ∀ {ρ σ : Nat → V.{u}}, EnvEq ρ σ → sat φ ρ → sat φ σ
  | .mem i j, _, _, h, m => mem_resp_right (mem_resp_left (h i) m) (h j)
  | .eq i j, _, _, h, e => eq_trans _ _ _ (eq_symm _ _ (h i)) (eq_trans _ _ _ e (h j))
  | .bot, _, _, _, x => x
  | .and a b, _, _, h, x => ⟨sat_resp a h x.1, sat_resp b h x.2⟩
  | .or a b, _, _, h, x => x.elim (fun y => Or.inl (sat_resp a h y)) (fun y => Or.inr (sat_resp b h y))
  | .imp a b, _, _, h, x => fun y => sat_resp b h (x (sat_resp a (envEq_symm h) y))
  | .ball n a, ρ, σ, h, x =>
      ball_tr (h n) (fun u => sat a (cons u ρ)) (fun v => sat a (cons v σ))
        (fun _ _ e => sat_resp a (envEq_cons h e)) x
  | .bex n a, ρ, σ, h, x =>
      bex_tr (h n) (fun u => sat a (cons u ρ)) (fun v => sat a (cons v σ))
        (fun _ _ e => sat_resp a (envEq_cons h e)) x

/-- Δ₀-separation: the instance of T88's separation -/
theorem d0_separation (φ : D0) (ρ : Nat → V.{u}) (a x : V.{u}) :
    Mem x (IZF.sep a (fun y => sat φ (cons y ρ))) ↔ Mem x a ∧ sat φ (cons x ρ) :=
  IZF.sep_spec a _ (fun _ _ e h => sat_resp φ (envEq_cons (fun _ => eq_refl _) e) h) x

end KP

end Found

#print axioms Found.KP.sat_resp
#print axioms Found.KP.d0_separation

found/KanExt.lean

lean_all/found/KanExt.lean

/-
  T149. found/KanExt — Kan extensions as a point, in the order-enriched
  case of the category parameter: for monotone f : P → Q and g : P → L into
  a frame L (T115, complete), the left Kan extension
      (Lan_f g)(q) = ⋁ { g p | f p ≤ q }
  satisfies the universal property  Lan_f g ≤ h  ⟺  g ≤ h ∘ f  (h monotone),
  and is itself monotone. The right Kan extension is dual (meets).
  (Set-valued Kan extensions as coends are the line to T95/T59 localization.)

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Locale

namespace Found

namespace Kan

universe u

variable (L : Loc.Frame.{u}) {P Q : Type u} (leP : P → P → Prop) (leQ : Q → Q → Prop)
  (transQ : ∀ {a b c}, leQ a b → leQ b c → leQ a c) (reflQ : ∀ a, leQ a a)

def lan (f : P → Q) (g : P → L.car) (q : Q) : L.car :=
  L.sup (fun x : {p : P // leQ (f p) q} => g x.val)

include transQ in
theorem lan_mono (f : P → Q) (g : P → L.car) {q q' : Q} (h : leQ q q') :
    L.le (lan L leQ f g q) (lan L leQ f g q') :=
  L.sup_least _ _ (fun x => L.sup_ub (fun x : {p : P // leQ (f p) q'} => g x.val) ⟨x.val, transQ x.property h⟩)

include reflQ in
/-- the universal property of the left Kan extension -/
theorem lan_universal (f : P → Q) (g : P → L.car) (h : Q → L.car)
    (hm : ∀ {q q'}, leQ q q' → L.le (h q) (h q')) :
    (∀ q, L.le (lan L leQ f g q) (h q)) ↔ (∀ p, L.le (g p) (h (f p))) :=
  ⟨fun k p => L.le_trans (L.sup_ub (fun x : {p' : P // leQ (f p') (f p)} => g x.val) ⟨p, reflQ (f p)⟩) (k (f p)),
   fun k _ => L.sup_least _ _ (fun x => L.le_trans (k x.val) (hm x.property))⟩

end Kan

end Found

#print axioms Found.Kan.lan_mono
#print axioms Found.Kan.lan_universal

found/Kleene3.lean

lean_all/found/Kleene3.lean

/-
  T140. found/Kleene3 — strong Kleene three-valued logic K3 as a point:
  the same three-valued tables as LP (T70, reused), with designated set {1}
  instead of {½, 1}.

  * K3 has no tautologies: the all-½ valuation keeps every formula at ½.
  * K3 is explosive (p, ¬p ⊨ q), unlike LP.
  * The line K3 ↔ LP: φ ⊨_K3 ψ  ⟺  ¬ψ ⊨_LP ¬φ (designation duality),
    constructively, by cases on the three values.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Paracons

namespace Found

namespace Para

def DesK (x : T3) : Prop := x = .o

theorem half_closed : (φ : LF) → lval (fun _ => .h) φ = .h
  | .var _ => rfl
  | .neg a => by rw [lval, half_closed a]; rfl
  | .and a b => by rw [lval, half_closed a, half_closed b]; rfl
  | .or a b => by rw [lval, half_closed a, half_closed b]; rfl

/-- K3 has no tautologies -/
theorem k3_no_tautology (φ : LF) : ¬ ∀ v, DesK (lval v φ) :=
  fun h => T3.noConfusion ((half_closed φ).symm.trans (h (fun _ => .h)))

/-- K3 is explosive: no valuation designates both p and ¬p -/
theorem k3_explosive (v : Nat → T3) (p : LF) (h1 : DesK (lval v p)) (h2 : DesK (lval v (.neg p))) : False := by
  unfold DesK at h1 h2
  rw [lval, h1] at h2
  exact T3.noConfusion h2

def EntK (φ ψ : LF) : Prop := ∀ v, DesK (lval v φ) → DesK (lval v ψ)
def EntLP (φ ψ : LF) : Prop := ∀ v, Des (lval v φ) → Des (lval v ψ)

theorem des_neg_iff (x : T3) : Des (lneg x) ↔ ¬ DesK x := by
  cases x
  · exact ⟨fun _ e => T3.noConfusion e, fun _ e => T3.noConfusion e⟩
  · exact ⟨fun _ e => T3.noConfusion e, fun _ e => T3.noConfusion e⟩
  · exact ⟨fun h _ => h rfl, fun h _ => h rfl⟩

theorem desK_dec (x : T3) : DesK x ∨ ¬ DesK x := by
  cases x
  · exact Or.inr (fun e => T3.noConfusion e)
  · exact Or.inr (fun e => T3.noConfusion e)
  · exact Or.inl rfl

/-- designation duality: φ ⊨_K3 ψ ⟺ ¬ψ ⊨_LP ¬φ -/
theorem k3_lp_dual (φ ψ : LF) : EntK φ ψ ↔ EntLP (.neg ψ) (.neg φ) :=
  ⟨fun h v hn => (des_neg_iff _).mpr (fun hφ => (des_neg_iff _).mp hn (h v hφ)),
   fun h v hφ => (desK_dec (lval v ψ)).elim id
     (fun nψ => ((des_neg_iff _).mp (h v ((des_neg_iff _).mpr nψ)) hφ).elim)⟩

end Para

end Found

#print axioms Found.Para.half_closed
#print axioms Found.Para.k3_no_tautology
#print axioms Found.Para.k3_explosive
#print axioms Found.Para.k3_lp_dual

found/Kripke.lean

lean_all/found/Kripke.lean

/-
  T192 line. found/Kripke — Kripke's theory of truth as the least fixed
  point of the strong Kleene jump (the K3 point, T140):
  * sentences with base atoms and a truth predicate T(c) on codes c
    (decode is a parameter: self-reference is allowed);
  * partial interpretations (extension, anti-extension); strong Kleene
    evaluation is monotone; the jump has a least fixed point
    (Knaster–Tarski, impredicatively);
  * the least fixed point is consistent (no code in both) and transparent
    (T(⌜φ⌝) is true/false exactly when φ is), and a Liar sentence is a gap.
  Constructive: no excluded middle is used.

  Lean 4 core only, hand-written proofs, no axioms.
  CIC dependence: impredicative Prop (the least fixed point).
-/

namespace Found

namespace Kripke

inductive Sen where
  | atom : Nat → Sen
  | tr : Nat → Sen
  | neg : Sen → Sen
  | and : Sen → Sen → Sen
  | or : Sen → Sen → Sen

variable (base : Nat → Bool) (decode : Nat → Sen)

/-- a partial interpretation of T: extension and anti-extension -/
structure PI where
  E : Nat → Prop
  A : Nat → Prop

/-- strong Kleene: (is true, is false) -/
def tv (I : PI) : Sen → Prop × Prop
  | .atom n => (base n = true, base n = false)
  | .tr c => (I.E c, I.A c)
  | .neg s => ((tv I s).2, (tv I s).1)
  | .and s t => ((tv I s).1 ∧ (tv I t).1, (tv I s).2 ∨ (tv I t).2)
  | .or s t => ((tv I s).1 ∨ (tv I t).1, (tv I s).2 ∧ (tv I t).2)

def Le (I J : PI) : Prop := (∀ c, I.E c → J.E c) ∧ (∀ c, I.A c → J.A c)

theorem tv_mono {I J : PI} (h : Le I J) : (s : Sen) →
    ((tv base I s).1 → (tv base J s).1) ∧ ((tv base I s).2 → (tv base J s).2)
  | .atom _ => ⟨id, id⟩
  | .tr c => ⟨h.1 c, h.2 c⟩
  | .neg s => ⟨(tv_mono h s).2, (tv_mono h s).1⟩
  | .and s t => ⟨fun ⟨a, b⟩ => ⟨(tv_mono h s).1 a, (tv_mono h t).1 b⟩,
      fun x => x.elim (fun a => Or.inl ((tv_mono h s).2 a)) (fun b => Or.inr ((tv_mono h t).2 b))⟩
  | .or s t => ⟨fun x => x.elim (fun a => Or.inl ((tv_mono h s).1 a)) (fun b => Or.inr ((tv_mono h t).1 b)),
      fun ⟨a, b⟩ => ⟨(tv_mono h s).2 a, (tv_mono h t).2 b⟩⟩

/-- the Kripke jump -/
def jump (I : PI) : PI := ⟨fun c => (tv base I (decode c)).1, fun c => (tv base I (decode c)).2⟩

theorem jump_mono {I J : PI} (h : Le I J) : Le (jump base decode I) (jump base decode J) :=
  ⟨fun c => (tv_mono base h (decode c)).1, fun c => (tv_mono base h (decode c)).2⟩

def Pre (I : PI) : Prop := Le (jump base decode I) I

/-- the least fixed point (Knaster–Tarski) -/
def lfp : PI := ⟨fun c => ∀ I, Pre base decode I → I.E c, fun c => ∀ I, Pre base decode I → I.A c⟩

theorem lfp_le (I : PI) (h : Pre base decode I) : Le (lfp base decode) I := ⟨fun _ x => x I h, fun _ x => x I h⟩

theorem lfp_pre : Pre base decode (lfp base decode) :=
  ⟨fun c hc I hI => hI.1 c ((jump_mono base decode (lfp_le base decode I hI)).1 c hc),
   fun c hc I hI => hI.2 c ((jump_mono base decode (lfp_le base decode I hI)).2 c hc)⟩

theorem lfp_post : Le (lfp base decode) (jump base decode (lfp base decode)) :=
  lfp_le base decode _ (jump_mono base decode (lfp_pre base decode))

/-- transparency: T(c) is true / false at the fixed point exactly when the sentence coded by c is -/
theorem transparent (c : Nat) :
    ((lfp base decode).E c ↔ (tv base (lfp base decode) (decode c)).1) ∧
    ((lfp base decode).A c ↔ (tv base (lfp base decode) (decode c)).2) :=
  ⟨⟨(lfp_post base decode).1 c, (lfp_pre base decode).1 c⟩, ⟨(lfp_post base decode).2 c, (lfp_pre base decode).2 c⟩⟩

/-- the consistent part: (E ∧ ¬A, A ∧ ¬E) -/
def cons (I : PI) : PI := ⟨fun c => I.E c ∧ ¬ I.A c, fun c => I.A c ∧ ¬ I.E c⟩

theorem no_clash (I : PI) : (s : Sen) →
    ((tv base (cons I) s).1 → ¬ (tv base I s).2) ∧ ((tv base (cons I) s).2 → ¬ (tv base I s).1)
  | .atom _ => ⟨fun h h' => Bool.noConfusion (h.symm.trans h'), fun h h' => Bool.noConfusion (h'.symm.trans h)⟩
  | .tr _ => ⟨fun h h' => h.2 h', fun h h' => h.2 h'⟩
  | .neg s => ⟨(no_clash I s).2, (no_clash I s).1⟩
  | .and s t => ⟨fun ⟨a, b⟩ h' => h'.elim ((no_clash I s).1 a) ((no_clash I t).1 b),
      fun h ⟨a, b⟩ => h.elim (fun x => (no_clash I s).2 x a) (fun y => (no_clash I t).2 y b)⟩
  | .or s t => ⟨fun h ⟨a, b⟩ => h.elim (fun x => (no_clash I s).1 x a) (fun y => (no_clash I t).1 y b),
      fun ⟨a, b⟩ h' => h'.elim ((no_clash I s).2 a) ((no_clash I t).2 b)⟩

/-- the least fixed point is consistent -/
theorem consistent (c : Nat) : ¬ ((lfp base decode).E c ∧ (lfp base decode).A c) := by
  have hle : Le (cons (lfp base decode)) (lfp base decode) := ⟨fun _ h => h.1, fun _ h => h.1⟩
  have hpre : Pre base decode (cons (lfp base decode)) :=
    ⟨fun c' hc => ⟨(lfp_pre base decode).1 c' ((jump_mono base decode hle).1 c' hc),
        fun ha => (no_clash base (lfp base decode) (decode c')).1 hc ((lfp_post base decode).2 c' ha)⟩,
     fun c' hc => ⟨(lfp_pre base decode).2 c' ((jump_mono base decode hle).2 c' hc),
        fun he => (no_clash base (lfp base decode) (decode c')).2 hc ((lfp_post base decode).1 c' he)⟩⟩
  exact fun ⟨he, ha⟩ => ((lfp_le base decode _ hpre).1 c he).2 ha

/-- a Liar sentence (decode c = ¬T(c)) is a truth-value gap -/
theorem liar_gap (c : Nat) (hl : decode c = .neg (.tr c)) :
    ¬ (lfp base decode).E c ∧ ¬ (lfp base decode).A c := by
  have t := transparent base decode c
  rw [hl] at t
  exact ⟨fun he => consistent base decode c ⟨he, t.1.mp he⟩, fun ha => consistent base decode c ⟨t.2.mp ha, ha⟩⟩

end Kripke

end Found

#print axioms Found.Kripke.transparent
#print axioms Found.Kripke.consistent
#print axioms Found.Kripke.liar_gap

found/KripkeCompl.lean

lean_all/found/KripkeCompl.lean

/-
  T93 line, part 2. found/KripkeCompl — Kripke completeness of
  intuitionistic propositional logic, in the classical point: a formula
  forced at every world of every Kripke model is derivable.  The canonical
  model: worlds = prime filters of the Lindenbaum algebra (found/AlgebraicLogic),
  ordered by inclusion; the truth lemma uses the prime filter theorem
  (found/PrimeFilter) for implications.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.PrimeFilter
import found.AlgebraicLogic

namespace Found

namespace KC

open CP PF Prp

structure KM where
  W : Type
  R : W → W → Prop
  V : Nat → W → Prop

def force (M : KM) : M.W → Form → Prop
  | w, .var n => M.V n w
  | _, .bot => False
  | w, .and a b => force M w a ∧ force M w b
  | w, .or a b => force M w a ∨ force M w b
  | w, .imp a b => ∀ w', M.R w w' → force M w' a → force M w' b

abbrev LH : HA.{0} := LT (fun _ => False)

structure PFilt (P : Form → Prop) : Prop where
  up : ∀ a b, LH.le a b → P a → P b
  meet : ∀ a b, P a → P b → P (Form.and a b)
  top : P LH.top
  nobot : ¬ P Form.bot
  prime : ∀ a b, P (Form.or a b) → P a ∨ P b

def canon : KM where
  W := {P : Form → Prop // PFilt P}
  R w w' := ∀ φ, w.val φ → w'.val φ
  V n w := w.val (.var n)

theorem filt_gen {P : Form → Prop} (hP : PFilt P) : (l : List Form) → (∀ x, x ∈ l → P x) → P (mL LH l)
  | [], _ => hP.top
  | x :: l, h => hP.meet _ _ (h x (.head _)) (filt_gen hP l (fun y m => h y (.tail _ m)))

theorem filt_of_gen {P : Form → Prop} (hP : PFilt P) {c : Form} (h : Gen LH P c) : P c :=
  match h with | ⟨l, hl, hle⟩ => hP.up _ _ hle (filt_gen hP l hl)

variable (K : Classical')

include K in
/-- the prime filter extending B and avoiding b, as a world -/
theorem extend (B : Form → Prop) (b : Form) (hB : ¬ Gen LH B b) :
    ∃ w : canon.W, (∀ x, B x → w.val x) ∧ ¬ w.val b :=
  ⟨⟨F K LH B b, ⟨fun _ _ hle h => gen_up LH h hle, fun _ _ h1 h2 => gen_meet LH h1 h2, gen_top LH _,
     fun hb => F_avoids K LH B b hB (gen_up LH hb (LH.bot_le b)), fun a c h => F_prime K LH B b hB a c h⟩⟩,
   fun _ hx => gen_of LH (Or.inl hx), F_avoids K LH B b hB⟩

include K in
/-- the truth lemma -/
theorem truth : (φ : Form) → (w : canon.W) → (force canon w φ ↔ w.val φ)
  | .var _, _ => Iff.rfl
  | .bot, w => ⟨fun h => h.elim, fun h => w.property.nobot h⟩
  | .and a b, w => ⟨fun ⟨x, y⟩ => w.property.meet _ _ ((truth a w).mp x) ((truth b w).mp y),
      fun h => ⟨(truth a w).mpr (w.property.up _ _ (LH.meet_l a b) h), (truth b w).mpr (w.property.up _ _ (LH.meet_r a b) h)⟩⟩
  | .or a b, w => ⟨fun h => h.elim (fun x => w.property.up _ _ (LH.join_l a b) ((truth a w).mp x))
        (fun y => w.property.up _ _ (LH.join_r a b) ((truth b w).mp y)),
      fun h => (w.property.prime a b h).elim (fun x => Or.inl ((truth a w).mpr x)) (fun y => Or.inr ((truth b w).mpr y))⟩
  | .imp a b, w => ⟨fun h => dne K _ (fun hn => by
        have hB : ¬ Gen LH (fun x => w.val x ∨ x = a) b := fun g =>
          hn (filt_of_gen w.property (gen_ded LH g))
        match extend K (fun x => w.val x ∨ x = a) b hB with
        | ⟨w', hw', hb'⟩ =>
          exact hb' ((truth b w').mp (h w' (fun φ hφ => hw' φ (Or.inl hφ)) ((truth a w').mpr (hw' a (Or.inr rfl)))))),
      fun h w' hr ha => (truth b w').mpr (w'.property.up _ _ (HA.mp LH a b)
        (w'.property.meet _ _ (hr _ h) ((truth a w').mp ha)))⟩

theorem tops : (l : List Form) → (∀ x, x ∈ l → x = LH.top) → LH.le LH.top (mL LH l)
  | [], _ => LH.le_refl _
  | x :: l, h => LH.meet_univ (by rw [h x (.head _)]; exact LH.le_refl _) (tops l (fun y m => h y (.tail _ m)))

include K in
/-- Kripke completeness: forced everywhere in every Kripke model ⇒ derivable -/
theorem kripke_completeness (φ : Form) (h : ∀ (M : KM) (w : M.W), force M w φ) :
    PD true (fun _ => False) φ := by
  refine dne K _ (fun hn => ?_)
  have hB : ¬ Gen LH (fun x => x = LH.top) φ := fun ⟨l, hl, hle⟩ => by
    have htop : LH.le LH.top (mL LH l) := tops l hl
    exact hn (.impE (LH.le_trans htop hle) (.impI h0))
  match extend K _ φ hB with
  | ⟨w, _, hφ⟩ => exact hφ ((truth K φ w).mp (h canon w))

end KC

end Found

#print axioms Found.KC.truth
#print axioms Found.KC.kripke_completeness

found/Krivine.lean

lean_all/found/Krivine.lean

/-
  T216. found/Krivine — Krivine's classical realizability as a point of the
  chart: a realizability structure (terms, stacks, push, application, the
  control operator cc, continuations k_π) with a pole (a free parameter: a
  set of processes closed under anti-evaluation). Formulas are falsity
  values (predicates on stacks, i.e. values of the power operation Ω^Π);
  a term realizes a formula when it is orthogonal to every stack of it.
  * modus ponens is sound: realizers of A → B and A give one of B;
  * cc realizes Peirce's law ((A → B) → A) → A for every A, B — the line to
    the classical point (T66); without cc this is the intuitionistic
    realizability point (T96);
  * with the empty pole, realizability collapses to two-valued truth
    (a formula is true iff its falsity value is empty, and → is the Boolean
    implication).
  Krivine's realizability models of ZF with dependent choice (T89) are
  internal theorems of the point (out of scope).

  Lean 4 core only, hand-written proofs, no axioms.
-/

namespace Found

namespace Krivine

/-- a realizability structure with a pole -/
structure KS where
  Tm : Type
  Stk : Type
  push : Tm → Stk → Stk
  app : Tm → Tm → Tm
  cc : Tm
  k : Stk → Tm
  pole : Tm → Stk → Prop
  sat_app : ∀ t u π, pole t (push u π) → pole (app t u) π
  sat_cc : ∀ t π, pole t (push (k π) π) → pole cc (push t π)
  sat_k : ∀ t π ρ, pole t π → pole (k π) (push t ρ)

variable (K : KS)

/-- formulas as falsity values -/
abbrev Fal := K.Stk → Prop

/-- t realizes A -/
def Real (A : Fal K) (t : K.Tm) : Prop := ∀ π, A π → K.pole t π

/-- falsity value of an implication -/
def Arr (A B : Fal K) : Fal K := fun π => ∃ t ρ, Real K A t ∧ B ρ ∧ π = K.push t ρ

/-- modus ponens -/
theorem mp (A B : Fal K) (t u : K.Tm) (ht : Real K (Arr K A B) t) (hu : Real K A u) :
    Real K B (K.app t u) :=
  fun π hπ => K.sat_app t u π (ht _ ⟨u, π, hu, hπ, rfl⟩)

/-- the continuation k_ρ realizes A → B whenever ρ is a stack of A -/
theorem k_real (A B : Fal K) (ρ : K.Stk) (hρ : A ρ) : Real K (Arr K A B) (K.k ρ) :=
  fun _ ⟨u, σ, hu, _, e⟩ => e ▸ K.sat_k u ρ σ (hu ρ hρ)

/-- cc realizes Peirce's law -/
theorem peirce (A B : Fal K) : Real K (Arr K (Arr K (Arr K A B) A) A) K.cc :=
  fun _ ⟨t, ρ, ht, hρ, e⟩ => e ▸ K.sat_cc t ρ (ht _ ⟨K.k ρ, ρ, k_real K A B ρ hρ, hρ, rfl⟩)

/-! ## the empty pole: two-valued truth -/

/-- a formula is true when its falsity value is empty -/
def True' (A : Fal K) : Prop := ∀ π, ¬ A π

theorem empty_real (h0 : ∀ t π, ¬ K.pole t π) (A : Fal K) (t : K.Tm) : Real K A t ↔ True' K A :=
  ⟨fun h π a => h0 t π (h π a), fun h π a => False.elim (h π a)⟩

theorem empty_arr (h0 : ∀ t π, ¬ K.pole t π) (A B : Fal K) :
    True' K (Arr K A B) ↔ (True' K A → True' K B) :=
  ⟨fun h ha ρ hb => h _ ⟨K.cc, ρ, (empty_real K h0 A K.cc).2 ha, hb, rfl⟩,
   fun h _ ⟨t, ρ, ht, hb, _⟩ => h ((empty_real K h0 A t).1 ht) ρ hb⟩

end Krivine

end Found

#print axioms Found.Krivine.mp
#print axioms Found.Krivine.k_real
#print axioms Found.Krivine.peirce
#print axioms Found.Krivine.empty_real
#print axioms Found.Krivine.empty_arr

found/LamMu1.lean

lean_all/found/LamMu1.lean

/-
  Line λ → μ, part 1. found/LamMu1 — λ-terms as plain de Bruijn trees Tm
  (indices as numbers) with shifting shN, substitution suN and complete
  development cdN; the erasure er from the well-scoped terms of found/Lambda
  is injective and commutes: er (ren …) = shN, er (inst t u) = suN 0, er (cd t)
  = cdN (er t), er (church k) = churchN k.  Takahashi's argument from the
  triangle lemma: a reduction t →β* u gives u ⇒* cd^d t, so if t reduces to
  a Church numeral then cd^d t is that numeral for some d (cd_reaches), and by
  Church–Rosser no other numeral is reached (numeral_unique).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.ChurchTuring3
import found.RegComp1

namespace Found

namespace LM

open UL RC

inductive Tm where
  | var : Nat → Tm
  | lam : Tm → Tm
  | app : Tm → Tm → Tm

def vi : {n : List Unit} → V n → Nat
  | _, .z => 0
  | _, .s v => vi v + 1

def er : {n : List Unit} → T n → Tm
  | _, .var v => .var (vi v)
  | _, .app a b => .app (er a) (er b)
  | _, .lam t => .lam (er t)

/-- add k to the indices ≥ c -/
def shf (k c i : Nat) : Nat := cond (Nat.blt i c) i (i + k)

def shN (k : Nat) : Nat → Tm → Tm
  | c, .var i => .var (shf k c i)
  | c, .lam b => .lam (shN k (c + 1) b)
  | c, .app a b => .app (shN k c a) (shN k c b)

def vcase (i k : Nat) (u : Tm) : Tm := cond (Nat.blt i k) (.var i) (cond (bq i k) (shN k 0 u) (.var (i - 1)))

def suN (u : Tm) : Nat → Tm → Tm
  | k, .var i => vcase i k u
  | k, .lam b => .lam (suN u (k + 1) b)
  | k, .app a b => .app (suN u k a) (suN u k b)

def cdN : Tm → Tm
  | .var i => .var i
  | .lam b => .lam (cdN b)
  | .app (.lam b) u => suN (cdN u) 0 (cdN b)
  | .app (.var i) u => .app (.var i) (cdN u)
  | .app (.app a b) u => .app (cdN (.app a b)) (cdN u)

def iterN : Nat → Tm
  | 0 => .var 0
  | k + 1 => .app (.var 1) (iterN k)

def churchN (k : Nat) : Tm := .lam (.lam (iterN k))

theorem cg2 {α β γ : Type} (f : α → β → γ) {a a' : α} {b b' : β} (h1 : a = a') (h2 : b = b') : f a b = f a' b' := by
  rw [h1, h2]

/-! ## comparisons -/

theorem blt_t {i k : Nat} (h : i < k) : Nat.blt i k = true := Nat.ble_eq_true_of_le h

theorem blt_f {i k : Nat} (h : k ≤ i) : Nat.blt i k = false := by
  cases e : Nat.blt i k with
  | false => rfl
  | true => exact absurd (Nat.lt_of_lt_of_le (Nat.le_of_ble_eq_true e) h) (Nat.lt_irrefl _)

theorem shf_lt {k c i : Nat} (h : i < c) : shf k c i = i := by unfold shf; rw [blt_t h]; rfl
theorem shf_ge {k c i : Nat} (h : c ≤ i) : shf k c i = i + k := by unfold shf; rw [blt_f h]; rfl

theorem vc_lt {i k : Nat} (u : Tm) (h : i < k) : vcase i k u = .var i := by unfold vcase; rw [blt_t h]; rfl
theorem vc_eq (k : Nat) (u : Tm) : vcase k k u = shN k 0 u := by
  unfold vcase; rw [blt_f (Nat.le_refl k), bq_self]; rfl
theorem vc_gt {i k : Nat} (u : Tm) (h : k < i) : vcase i k u = .var (i - 1) := by
  unfold vcase; rw [blt_f (Nat.le_of_lt h), bq_ne i k (fun e => Nat.lt_irrefl _ (e ▸ h))]; rfl

/-! ## erasure and renaming / substitution -/

theorem er_ren {m : List Unit} (t : T m) : ∀ {n : List Unit} (r : V m → V n) (k c : Nat),
    (∀ v, vi (r v) = shf k c (vi v)) → er (ren r t) = shN k c (er t) := by
  induction t with
  | var v => intro n r k c h; exact congrArg Tm.var (h v)
  | app a b iha ihb => intro n r k c h; exact cg2 Tm.app (iha r k c h) (ihb r k c h)
  | lam t ih =>
    intro n r k c h
    refine congrArg Tm.lam (ih (lift r) k (c + 1) (fun v => ?_))
    cases v with
    | z => exact (shf_lt (Nat.succ_pos c)).symm
    | s v =>
      show vi (r v) + 1 = shf k (c + 1) (vi v + 1)
      rw [h v]
      cases Nat.lt_or_ge (vi v) c with
      | inl hl => rw [shf_lt hl, shf_lt (Nat.succ_lt_succ hl)]
      | inr hg => rw [shf_ge hg, shf_ge (Nat.succ_le_succ hg), Nat.add_right_comm]

theorem sh_zero : (t : Tm) → (c : Nat) → shN 0 c t = t
  | .var i, c => by
    show Tm.var (shf 0 c i) = _
    cases Nat.lt_or_ge i c with
    | inl hl => rw [shf_lt hl]
    | inr hg => rw [shf_ge hg]; rfl
  | .lam b, c => congrArg Tm.lam (sh_zero b (c + 1))
  | .app a b, c => cg2 Tm.app (sh_zero a c) (sh_zero b c)

theorem sh_sh : (t : Tm) → (k c : Nat) → shN 1 c (shN k c t) = shN (k + 1) c t
  | .var i, k, c => by
    show Tm.var (shf 1 c (shf k c i)) = Tm.var (shf (k + 1) c i)
    cases Nat.lt_or_ge i c with
    | inl hl => rw [shf_lt hl, shf_lt hl, shf_lt hl]
    | inr hg => rw [shf_ge hg, shf_ge hg, shf_ge (Nat.le_trans hg (Nat.le_add_right i k)), Nat.add_assoc]
  | .lam b, k, c => congrArg Tm.lam (sh_sh b k (c + 1))
  | .app a b, k, c => cg2 Tm.app (sh_sh a k c) (sh_sh b k c)

theorem vi_s {n : List Unit} (v : V n) : vi (V.s v) = shf 1 0 (vi v) := (shf_ge (Nat.zero_le _)).symm

theorem er_sub {m : List Unit} (t : T m) : ∀ (n : List Unit) (σ : V m → T n) (k : Nat) (U : Tm),
    (∀ v, er (σ v) = vcase (vi v) k U) → er (sub σ t) = suN U k (er t) := by
  induction t with
  | var v => intro n σ k U h; exact h v
  | app a b iha ihb => intro n σ k U h; exact cg2 Tm.app (iha n σ k U h) (ihb n σ k U h)
  | lam t ih =>
    intro n σ k U h
    refine congrArg Tm.lam (ih _ (liftS σ) (k + 1) U (fun v => ?_))
    cases v with
    | z => exact (vc_lt U (Nat.succ_pos k)).symm
    | s v =>
      show er (ren V.s (σ v)) = vcase (vi v + 1) (k + 1) U
      rw [er_ren (σ v) V.s 1 0 vi_s, h v]
      cases Nat.lt_or_ge (vi v) k with
      | inl hl =>
        rw [vc_lt U hl, vc_lt U (Nat.succ_lt_succ hl)]
        show Tm.var (shf 1 0 (vi v)) = _
        rw [shf_ge (Nat.zero_le _)]
      | inr hg =>
        cases le_cases' hg with
        | inl e => rw [← e, vc_eq, vc_eq, sh_sh]
        | inr hl =>
          rw [vc_gt U hl, vc_gt U (Nat.succ_lt_succ hl)]
          show Tm.var (shf 1 0 (vi v - 1)) = _
          rw [shf_ge (Nat.zero_le _)]
          obtain ⟨j, hj⟩ : ∃ j, vi v = j + 1 := match vi v, hl with
            | j + 1, _ => ⟨j, rfl⟩
          rw [hj]; rfl
where
  le_cases' {a b : Nat} (h : a ≤ b) : a = b ∨ a < b := by
    induction h with
    | refl => exact Or.inl rfl
    | step h _ => exact Or.inr (Nat.lt_succ_of_le h)

theorem er_inst {n : List Unit} (t : T (() :: n)) (u : T n) : er (inst t u) = suN (er u) 0 (er t) :=
  er_sub t _ (single u) 0 (er u) (fun v => by
    cases v with
    | z => show er u = vcase 0 0 (er u); rw [vc_eq, sh_zero]
    | s v => show Tm.var (vi v) = vcase (vi v + 1) 0 (er u); rw [vc_gt _ (Nat.succ_pos _)]; rfl)

theorem er_cd {n : List Unit} : (t : T n) → er (cd t) = cdN (er t)
  | .var _ => rfl
  | .lam t => congrArg Tm.lam (er_cd t)
  | .app (.lam t) u => by
    show er (inst (cd t) (cd u)) = _
    rw [er_inst, er_cd t, er_cd u]; rfl
  | .app (.var _) u => congrArg (Tm.app _) (er_cd u)
  | .app (.app a b) u => cg2 Tm.app (er_cd (.app a b)) (er_cd u)

theorem er_iter {n : List Unit} : (k : Nat) → er (iterApp (T.var (V.s V.z) : T (() :: () :: n)) (T.var V.z) k) = iterN k
  | 0 => rfl
  | k + 1 => congrArg (Tm.app (.var 1)) (er_iter k)

theorem er_church {n : List Unit} (k : Nat) : er (church k : T n) = churchN k :=
  congrArg Tm.lam (congrArg Tm.lam (er_iter k))

/-! ## injectivity -/

theorem vi_inj : {n : List Unit} → (v w : V n) → vi v = vi w → v = w
  | _, .z, .z, _ => rfl
  | _, .z, .s _, h => Nat.noConfusion h
  | _, .s _, .z, h => Nat.noConfusion h
  | _, .s v, .s w, h => congrArg V.s (vi_inj v w (Nat.succ.inj h))

theorem er_inj {n : List Unit} : (t u : T n) → er t = er u → t = u
  | .var v, .var w, h => congrArg T.var (vi_inj v w (Tm.var.inj h))
  | .var _, .app _ _, h => Tm.noConfusion h
  | .var _, .lam _, h => Tm.noConfusion h
  | .app _ _, .var _, h => Tm.noConfusion h
  | .app a b, .app c d, h => cg2 T.app (er_inj a c (Tm.app.inj h).1) (er_inj b d (Tm.app.inj h).2)
  | .app _ _, .lam _, h => Tm.noConfusion h
  | .lam _, .var _, h => Tm.noConfusion h
  | .lam _, .app _ _, h => Tm.noConfusion h
  | .lam t, .lam u, h => congrArg T.lam (er_inj t u (Tm.lam.inj h))

theorem iterN_inj : (a b : Nat) → iterN a = iterN b → a = b
  | 0, 0, _ => rfl
  | 0, _ + 1, h => Tm.noConfusion h
  | _ + 1, 0, h => Tm.noConfusion h
  | a + 1, b + 1, h => congrArg (· + 1) (iterN_inj a b (Tm.app.inj h).2)

theorem churchN_inj {a b : Nat} (h : churchN a = churchN b) : a = b :=
  iterN_inj a b (Tm.lam.inj (Tm.lam.inj h))

/-! ## iterated complete development (Takahashi) -/

def cdi {n : List Unit} : Nat → T n → T n
  | 0, t => t
  | d + 1, t => cdi d (cd t)

def cdNi : Nat → Tm → Tm
  | 0, t => t
  | d + 1, t => cdNi d (cdN t)

theorem er_cdi {n : List Unit} : (d : Nat) → (t : T n) → er (cdi d t) = cdNi d (er t)
  | 0, _ => rfl
  | d + 1, t => by show er (cdi d (cd t)) = cdNi d (cdN (er t)); rw [er_cdi d (cd t), er_cd]

theorem par_mono {n : List Unit} {a b : T n} (h : Par a b) : Par (cd a) (cd b) := triangle (triangle h)

theorem par_cdi {n : List Unit} : (k : Nat) → {a b : T n} → Par a b → Par (cdi k a) (cdi k b)
  | 0, _, _, h => h
  | k + 1, _, _, h => par_cdi k (par_mono h)

theorem pars_cdi {n : List Unit} {t u : T n} (h : Star Par t u) : ∃ d, Star Par u (cdi d t) := by
  induction h with
  | refl a => exact ⟨0, .refl a⟩
  | @step t t1 u p _ ih =>
    match ih with
    | ⟨d, s⟩ => exact ⟨d + 1, s.trans' (Star.one (par_cdi d (triangle p)))⟩

theorem iter_normal {n : List Unit} : (k : Nat) → (u : T (() :: () :: n)) →
    ¬ Beta (iterApp (T.var (V.s V.z)) (T.var V.z) k) u
  | 0, _, h => nomatch h
  | k + 1, _, h => by
    cases h with
    | appL _ r => nomatch r
    | appR _ r => exact iter_normal k _ r

theorem church_normal {n : List Unit} (k : Nat) : Normal (church k : T n) := fun u h => by
  cases h with
  | lam r => cases r with
    | lam r' => exact iter_normal k _ r'

theorem cd_star {n : List Unit} : (d : Nat) → (t : T n) → Star Beta t (cdi d t)
  | 0, t => .refl t
  | d + 1, t => (par_betas (triangle (par_refl t))).trans' (cd_star d (cd t))

/-- a term reducing to a numeral reaches it by iterated complete development -/
theorem cd_reaches {n : List Unit} {t : T n} {k : Nat} (h : Star Beta t (church k)) : ∃ d, cdi d t = church k :=
  match pars_cdi (betas_pars h) with
  | ⟨d, s⟩ => ⟨d, (normal_star (church_normal k) (pars_betas s)).symm⟩

/-- and no other numeral appears among the developments -/
theorem numeral_unique {n : List Unit} {t : T n} {k : Nat} (h : Star Beta t (church k)) (d m : Nat)
    (hm : cdNi d (er t) = churchN m) : m = k := by
  have e : cdi d t = church m := er_inj _ _ (by rw [er_cdi, hm, er_church])
  have hs := cd_star d t
  rw [e] at hs
  have := nf_unique hs h (church_normal m) (church_normal k)
  exact churchN_inj (by rw [← er_church (n := n) m, ← er_church (n := n) k, this])

end LM

end Found

#print axioms Found.LM.er_ren
#print axioms Found.LM.er_sub
#print axioms Found.LM.er_inst
#print axioms Found.LM.er_cd
#print axioms Found.LM.er_inj
#print axioms Found.LM.pars_cdi
#print axioms Found.LM.church_normal
#print axioms Found.LM.cd_reaches
#print axioms Found.LM.numeral_unique

found/LamMu2.lean

lean_all/found/LamMu2.lean

/-
  Line λ → μ, part 2. found/LamMu2 — a flat machine (no recursion in its
  step) computing iterated complete development: tasks cd t / sh k c t /
  su u k t evaluated with an explicit stack of frames; every task reaches
  its value (task_run); at the top level the machine checks whether the
  current term is a Church numeral (walking down its body) and either stops
  with its value or develops once more.  If some development cd^d t is a
  numeral, the machine stops, with the value of a numeral among the
  developments (loop_halts); for a λ-term reducing to church k it stops with
  k (machine_value).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.LamMu1

namespace Found

namespace LM

open UL RC

inductive Task where
  | cd : Tm → Task
  | sh : Nat → Nat → Tm → Task
  | su : Tm → Nat → Tm → Task

def val : Task → Tm
  | .cd t => cdN t
  | .sh k c t => shN k c t
  | .su u k t => suN u k t

inductive Fr where
  | lamK : Fr
  | app1 : Task → Fr
  | app2 : Tm → Fr
  | beta1 : Tm → Fr
  | beta2 : Tm → Fr

inductive St where
  | ev : Task → List Fr → St
  | ret : Tm → List Fr → St
  | chk : Tm → Tm → Nat → St
  | done : Nat → St

/-- the numeral check on the body of λλ.body -/
def chkStep (r : Tm) (n : Nat) : Tm → St
  | .var 0 => .done n
  | .var (_ + 1) => .ev (.cd r) []
  | .lam _ => .ev (.cd r) []
  | .app (.var 1) b => .chk r b (n + 1)
  | .app (.var 0) _ => .ev (.cd r) []
  | .app (.var (_ + 2)) _ => .ev (.cd r) []
  | .app (.lam _) _ => .ev (.cd r) []
  | .app (.app _ _) _ => .ev (.cd r) []

def topStep : Tm → St
  | .lam (.lam b) => .chk (.lam (.lam b)) b 0
  | .lam (.var i) => .ev (.cd (.lam (.var i))) []
  | .lam (.app a b) => .ev (.cd (.lam (.app a b))) []
  | .var i => .ev (.cd (.var i)) []
  | .app a b => .ev (.cd (.app a b)) []

def mstep : St → St
  | .ev (.cd (.var i)) κ => .ret (.var i) κ
  | .ev (.cd (.lam b)) κ => .ev (.cd b) (.lamK :: κ)
  | .ev (.cd (.app (.lam b) u)) κ => .ev (.cd b) (.beta1 u :: κ)
  | .ev (.cd (.app (.var i) u)) κ => .ev (.cd (.var i)) (.app1 (.cd u) :: κ)
  | .ev (.cd (.app (.app a b) u)) κ => .ev (.cd (.app a b)) (.app1 (.cd u) :: κ)
  | .ev (.sh k c (.var i)) κ => .ret (.var (shf k c i)) κ
  | .ev (.sh k c (.lam b)) κ => .ev (.sh k (c + 1) b) (.lamK :: κ)
  | .ev (.sh k c (.app a b)) κ => .ev (.sh k c a) (.app1 (.sh k c b) :: κ)
  | .ev (.su u k (.var i)) κ =>
      cond (Nat.blt i k) (.ret (.var i) κ) (cond (bq i k) (.ev (.sh k 0 u) κ) (.ret (.var (i - 1)) κ))
  | .ev (.su u k (.lam b)) κ => .ev (.su u (k + 1) b) (.lamK :: κ)
  | .ev (.su u k (.app a b)) κ => .ev (.su u k a) (.app1 (.su u k b) :: κ)
  | .ret r (.lamK :: κ) => .ret (.lam r) κ
  | .ret r (.app1 τ :: κ) => .ev τ (.app2 r :: κ)
  | .ret r (.app2 r1 :: κ) => .ret (.app r1 r) κ
  | .ret r (.beta1 u :: κ) => .ev (.cd u) (.beta2 r :: κ)
  | .ret v (.beta2 r :: κ) => .ev (.su v 0 r) κ
  | .ret r [] => topStep r
  | .chk r b n => chkStep r n b
  | .done n => .done n

inductive MR' : St → St → Prop where
  | refl (s : St) : MR' s s
  | step {s t : St} : MR' (mstep s) t → MR' s t

theorem mtrans {a b c : St} (h1 : MR' a b) (h2 : MR' b c) : MR' a c := by
  induction h1 with
  | refl => exact h2
  | step _ ih => exact .step (ih h2)

theorem m1 {s t : St} (h : mstep s = t) : MR' s t := .step (h ▸ .refl _)

/-! ## every task reaches its value -/

theorem sh_run : (t : Tm) → (k c : Nat) → (κ : List Fr) → MR' (.ev (.sh k c t) κ) (.ret (shN k c t) κ)
  | .var _, _, _, _ => m1 rfl
  | .lam b, k, c, κ => .step (mtrans (sh_run b k (c + 1) (.lamK :: κ)) (m1 rfl))
  | .app a b, k, c, _ => .step (mtrans (sh_run a k c _) (.step (mtrans (sh_run b k c _) (m1 rfl))))

theorem su_run (u : Tm) : (t : Tm) → (k : Nat) → (κ : List Fr) → MR' (.ev (.su u k t) κ) (.ret (suN u k t) κ)
  | .var i, k, κ => by
    show MR' _ (.ret (vcase i k u) κ)
    refine .step ?_
    show MR' (cond (Nat.blt i k) _ _) _
    unfold vcase
    cases Nat.blt i k with
    | true => exact .refl _
    | false =>
      cases bq i k with
      | true => exact sh_run u k 0 κ
      | false => exact .refl _
  | .lam b, k, κ => .step (mtrans (su_run u b (k + 1) (.lamK :: κ)) (m1 rfl))
  | .app a b, k, κ => .step (mtrans (su_run u a k _) (.step (mtrans (su_run u b k _) (m1 rfl))))

theorem cd_run : (t : Tm) → (κ : List Fr) → MR' (.ev (.cd t) κ) (.ret (cdN t) κ)
  | .var _, _ => m1 rfl
  | .lam b, κ => .step (mtrans (cd_run b (.lamK :: κ)) (m1 rfl))
  | .app (.lam b) u, κ => .step (mtrans (cd_run b _) (.step (mtrans (cd_run u _) (.step (su_run _ _ 0 κ)))))
  | .app (.var i) u, _ => .step (mtrans (cd_run (.var i) _) (.step (mtrans (cd_run u _) (m1 rfl))))
  | .app (.app a b) u, _ => .step (mtrans (cd_run (.app a b) _) (.step (mtrans (cd_run u _) (m1 rfl))))

theorem task_run (τ : Task) (κ : List Fr) : MR' (.ev τ κ) (.ret (val τ) κ) := by
  cases τ with
  | cd t => exact cd_run t κ
  | sh k c t => exact sh_run t k c κ
  | su u k t => exact su_run u t k κ

/-! ## the numeral check and the loop -/

theorem chk_run (r : Tm) : (b : Tm) → (n : Nat) →
    (∃ m, b = iterN m ∧ MR' (.chk r b n) (.done (n + m))) ∨
    ((∀ m, b ≠ iterN m) ∧ MR' (.chk r b n) (.ev (.cd r) []))
  | .var 0, n => Or.inl ⟨0, rfl, m1 rfl⟩
  | .var (_ + 1), _ => Or.inr ⟨fun m e => by
      cases m with
      | zero => exact Nat.noConfusion (Tm.var.inj e)
      | succ m => exact Tm.noConfusion e, m1 rfl⟩
  | .lam _, _ => Or.inr ⟨fun m e => by cases m <;> exact Tm.noConfusion e, m1 rfl⟩
  | .app (.var 1) b, n => match chk_run r b (n + 1) with
    | Or.inl ⟨m, e, h⟩ => Or.inl ⟨m + 1, by rw [e]; rfl, .step (by rw [show n + (m + 1) = n + 1 + m by rw [Nat.add_assoc, Nat.add_comm 1 m]]; exact h)⟩
    | Or.inr ⟨ne, h⟩ => Or.inr ⟨fun m e => by
        cases m with
        | zero => exact Tm.noConfusion e
        | succ m => exact ne m (Tm.app.inj e).2, .step h⟩
  | .app (.var 0) _, _ => Or.inr ⟨fun m e => by
      cases m with
      | zero => exact Tm.noConfusion e
      | succ m => exact Nat.noConfusion (Tm.var.inj (Tm.app.inj e).1), m1 rfl⟩
  | .app (.var (_ + 2)) _, _ => Or.inr ⟨fun m e => by
      cases m with
      | zero => exact Tm.noConfusion e
      | succ m => exact Nat.noConfusion (Nat.succ.inj (Tm.var.inj (Tm.app.inj e).1)), m1 rfl⟩
  | .app (.lam _) _, _ => Or.inr ⟨fun m e => by
      cases m with
      | zero => exact Tm.noConfusion e
      | succ m => exact Tm.noConfusion (Tm.app.inj e).1, m1 rfl⟩
  | .app (.app _ _) _, _ => Or.inr ⟨fun m e => by
      cases m with
      | zero => exact Tm.noConfusion e
      | succ m => exact Tm.noConfusion (Tm.app.inj e).1, m1 rfl⟩

theorem top_run (r : Tm) :
    (∃ m, r = churchN m ∧ MR' (.ret r []) (.done m)) ∨
    ((∀ m, r ≠ churchN m) ∧ MR' (.ret r []) (.ret (cdN r) [])) := by
  match r with
  | .lam (.lam b) =>
    match chk_run (.lam (.lam b)) b 0 with
    | Or.inl ⟨m, e, h⟩ => exact Or.inl ⟨m, by rw [e]; rfl, .step (by rw [Nat.zero_add] at h; exact h)⟩
    | Or.inr ⟨ne, h⟩ => exact Or.inr ⟨fun m e => ne m (Tm.lam.inj (Tm.lam.inj e)), .step (mtrans h (cd_run _ []))⟩
  | .lam (.var _) => exact Or.inr ⟨fun m e => Tm.noConfusion (Tm.lam.inj e), .step (cd_run _ [])⟩
  | .lam (.app _ _) => exact Or.inr ⟨fun m e => Tm.noConfusion (Tm.lam.inj e), .step (cd_run _ [])⟩
  | .var _ => exact Or.inr ⟨fun m e => Tm.noConfusion e, .step (cd_run _ [])⟩
  | .app _ _ => exact Or.inr ⟨fun m e => Tm.noConfusion e, .step (cd_run _ [])⟩

/-- if some development is a numeral, the machine stops with a numeral among the developments -/
theorem loop_halts : (d : Nat) → (r : Tm) → (k : Nat) → cdNi d r = churchN k →
    ∃ m d', MR' (.ret r []) (.done m) ∧ cdNi d' r = churchN m
  | 0, r, k, h => match top_run r with
    | Or.inl ⟨m, e, hm⟩ => ⟨m, 0, hm, e⟩
    | Or.inr ⟨ne, _⟩ => absurd h (ne k)
  | d + 1, r, k, h => match top_run r with
    | Or.inl ⟨m, e, hm⟩ => ⟨m, 0, hm, e⟩
    | Or.inr ⟨_, hm⟩ => match loop_halts d (cdN r) k h with
      | ⟨m, d', h1, h2⟩ => ⟨m, d' + 1, mtrans hm h1, h2⟩

/-- a λ-term reducing to church k drives the machine to stop with k -/
theorem machine_value {n : List Unit} {t : T n} {k : Nat} (h : Star Beta t (church k)) :
    MR' (.ret (er t) []) (.done k) :=
  match cd_reaches h with
  | ⟨d, e⟩ => match loop_halts d (er t) k (by rw [← er_cdi, e, er_church]) with
    | ⟨m, d', h1, h2⟩ => numeral_unique h d' m h2 ▸ h1

end LM

end Found

#print axioms Found.LM.sh_run
#print axioms Found.LM.su_run
#print axioms Found.LM.cd_run
#print axioms Found.LM.task_run
#print axioms Found.LM.chk_run
#print axioms Found.LM.top_run
#print axioms Found.LM.loop_halts
#print axioms Found.LM.machine_value

found/LamMu3.lean

lean_all/found/LamMu3.lean

/-
  Line λ → μ, part 3. found/LamMu3 — arithmetisation of the machine of
  found/LamMu2: a small expression language E in one variable (constants,
  Cantor pairing and projections, successor, predecessor, +, truncated −,
  zero test), compiled to primitive recursive terms with evaluation proved
  once (toPR_eval); codes of terms, tasks, frames, stacks and states; the
  step function stepE with den stepE (code s) = code (mstep s) for every
  state (step_code).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.LamMu2
import found.RegToMu
import lib.Gcd

namespace Found

namespace LM

open Rec RM RC

inductive E where
  | x : E
  | c : Nat → E
  | fst : E → E
  | snd : E → E
  | pr : E → E → E
  | succ : E → E
  | pred : E → E
  | add : E → E → E
  | sub : E → E → E
  | ite : E → E → E → E

def ifz : Nat → Nat → Nat → Nat
  | 0, a, _ => a
  | _ + 1, _, b => b

def den : E → Nat → Nat
  | .x, x => x
  | .c n, _ => n
  | .fst e, x => fstU (den e x)
  | .snd e, x => sndU (den e x)
  | .pr a b, x => pair (den a x) (den b x)
  | .succ e, x => den e x + 1
  | .pred e, x => den e x - 1
  | .add a b, x => den a x + den b x
  | .sub a b, x => den a x - den b x
  | .ite a b c, x => ifz (den a x) (den b x) (den c x)

def toPR : E → PR
  | .x => .proj 0
  | .c n => cst n
  | .fst e => .comp fstP [toPR e]
  | .snd e => .comp sndP [toPR e]
  | .pr a b => .comp pairP [toPR a, toPR b]
  | .succ e => .comp .succ [toPR e]
  | .pred e => .comp predP [toPR e]
  | .add a b => .comp addPR [toPR a, toPR b]
  | .sub a b => .comp subP [toPR a, toPR b]
  | .ite a b c => .comp condP [toPR a, toPR b, toPR c]

theorem condP_ifz (k a b : Nat) : eval condP [k, a, b] = ifz k a b := by
  cases k with
  | zero => rfl
  | succ k => rfl

theorem toPR_eval (x : Nat) : (e : E) → eval (toPR e) [x] = den e x
  | .x => rfl
  | .c n => cst_eval [x] n
  | .fst e => by show eval fstP [eval (toPR e) [x]] = _; rw [fstP_eval, toPR_eval x e]; rfl
  | .snd e => by show eval sndP [eval (toPR e) [x]] = _; rw [sndP_eval, toPR_eval x e]; rfl
  | .pr a b => by show eval pairP [eval (toPR a) [x], eval (toPR b) [x]] = _; rw [pairP_eval, toPR_eval x a, toPR_eval x b]; rfl
  | .succ e => by show eval (toPR e) [x] + 1 = _; rw [toPR_eval x e]; rfl
  | .pred e => by show eval predP [eval (toPR e) [x]] = _; rw [predP_eval, toPR_eval x e]; rfl
  | .add a b => by show eval addPR [eval (toPR a) [x], eval (toPR b) [x]] = _; rw [addP_eval, toPR_eval x a, toPR_eval x b]; rfl
  | .sub a b => by show eval subP [eval (toPR a) [x], eval (toPR b) [x]] = _; rw [subP_eval, toPR_eval x a, toPR_eval x b]; rfl
  | .ite a b c => by
    show eval condP [eval (toPR a) [x], eval (toPR b) [x], eval (toPR c) [x]] = _
    rw [condP_ifz, toPR_eval x a, toPR_eval x b, toPR_eval x c]; rfl

/-! ## codes -/

def cT : Tm → Nat
  | .var i => pair 0 i
  | .lam b => pair 1 (cT b)
  | .app a b => pair 2 (pair (cT a) (cT b))

def cK : Task → Nat
  | .cd t => pair 0 (cT t)
  | .sh k c t => pair 1 (pair k (pair c (cT t)))
  | .su u k t => pair 2 (pair (cT u) (pair k (cT t)))

def cF : Fr → Nat
  | .lamK => pair 0 0
  | .app1 τ => pair 1 (cK τ)
  | .app2 r => pair 2 (cT r)
  | .beta1 u => pair 3 (cT u)
  | .beta2 r => pair 4 (cT r)

def cL : List Fr → Nat
  | [] => 0
  | f :: κ => pair (cF f) (cL κ) + 1

def cS : St → Nat
  | .ev τ κ => pair 0 (pair (cK τ) (cL κ))
  | .ret r κ => pair 1 (pair (cT r) (cL κ))
  | .chk r b n => pair 2 (pair (cT r) (pair (cT b) n))
  | .done n => pair 3 n

/-! ## the step function -/

def F' (e : E) : E := .fst e
def S' (e : E) : E := .snd e
def P' (a b : E) : E := .pr a b
def K' (n : Nat) : E := .c n
def cons' (f l : E) : E := .succ (.pr f l)
def sel3 (t a b c : E) : E := .ite t a (.ite (.pred t) b c)
def sel4 (t a b c d : E) : E := .ite t a (.ite (.pred t) b (.ite (.pred (.pred t)) c d))
def sel5 (t a b c d e : E) : E :=
  .ite t a (.ite (.pred t) b (.ite (.pred (.pred t)) c (.ite (.pred (.pred (.pred t))) d e)))

section
variable (body : E)

def lamKE : E := P' (K' 0) (K' 0)
def evK : E := S' body
def evT : E := F' body
def evD : E := S' (evT body)

-- cd
def cdA : E := F' (S' (evD body))
def cdU : E := S' (S' (evD body))
def cdGen : E := P' (K' 0) (P' (P' (K' 0) (cdA body)) (cons' (P' (K' 1) (P' (K' 0) (cdU body))) (evK body)))
def cdE : E := sel3 (F' (evD body))
  (P' (K' 1) (P' (evD body) (evK body)))
  (P' (K' 0) (P' (P' (K' 0) (S' (evD body))) (cons' lamKE (evK body))))
  (sel3 (F' (cdA body)) (cdGen body)
    (P' (K' 0) (P' (P' (K' 0) (S' (cdA body))) (cons' (P' (K' 3) (cdU body)) (evK body)))) (cdGen body))

-- sh / su share the layout (first, second, term)
def p1 : E := F' (evD body)
def p2 : E := F' (S' (evD body))
def p3 : E := S' (S' (evD body))

def shE : E := sel3 (F' (p3 body))
  (P' (K' 1) (P' (P' (K' 0) (.ite (.sub (p2 body) (S' (p3 body))) (.add (S' (p3 body)) (p1 body)) (S' (p3 body)))) (evK body)))
  (P' (K' 0) (P' (P' (K' 1) (P' (p1 body) (P' (.succ (p2 body)) (S' (p3 body))))) (cons' lamKE (evK body))))
  (P' (K' 0) (P' (P' (K' 1) (P' (p1 body) (P' (p2 body) (F' (S' (p3 body))))))
    (cons' (P' (K' 1) (P' (K' 1) (P' (p1 body) (P' (p2 body) (S' (S' (p3 body))))))) (evK body))))

def suE : E := sel3 (F' (p3 body))
  (.ite (.sub (p2 body) (S' (p3 body)))
    (.ite (.sub (S' (p3 body)) (p2 body))
      (P' (K' 0) (P' (P' (K' 1) (P' (p2 body) (P' (K' 0) (p1 body)))) (evK body)))
      (P' (K' 1) (P' (P' (K' 0) (.pred (S' (p3 body)))) (evK body))))
    (P' (K' 1) (P' (P' (K' 0) (S' (p3 body))) (evK body))))
  (P' (K' 0) (P' (P' (K' 2) (P' (p1 body) (P' (.succ (p2 body)) (S' (p3 body))))) (cons' lamKE (evK body))))
  (P' (K' 0) (P' (P' (K' 2) (P' (p1 body) (P' (p2 body) (F' (S' (p3 body))))))
    (cons' (P' (K' 1) (P' (K' 2) (P' (p1 body) (P' (p2 body) (S' (S' (p3 body))))))) (evK body))))

def evE : E := sel3 (F' (evT body)) (cdE body) (shE body) (suE body)

-- ret
def rR : E := F' body
def rK : E := S' body
def rF : E := F' (.pred (rK body))
def rK' : E := S' (.pred (rK body))
def rD : E := S' (rF body)
def evcdr (r : E) : E := P' (K' 0) (P' (P' (K' 0) r) (K' 0))

def topE : E := .ite (F' (rR body)) (evcdr (rR body))
  (.ite (.pred (F' (rR body)))
    (.ite (F' (S' (rR body))) (evcdr (rR body))
      (.ite (.pred (F' (S' (rR body)))) (P' (K' 2) (P' (rR body) (P' (S' (S' (rR body))) (K' 0)))) (evcdr (rR body))))
    (evcdr (rR body)))

def frameE : E := sel5 (F' (rF body))
  (P' (K' 1) (P' (P' (K' 1) (rR body)) (rK' body)))
  (P' (K' 0) (P' (rD body) (cons' (P' (K' 2) (rR body)) (rK' body))))
  (P' (K' 1) (P' (P' (K' 2) (P' (rD body) (rR body))) (rK' body)))
  (P' (K' 0) (P' (P' (K' 0) (rD body)) (cons' (P' (K' 4) (rR body)) (rK' body))))
  (P' (K' 0) (P' (P' (K' 2) (P' (rR body) (P' (K' 0) (rD body)))) (rK' body)))

def retE : E := .ite (rK body) (topE body) (frameE body)

-- chk
def kR : E := F' body
def kB : E := F' (S' body)
def kN : E := S' (S' body)
def kA : E := F' (S' (kB body))

def chkE : E := sel3 (F' (kB body))
  (.ite (S' (kB body)) (P' (K' 3) (kN body)) (evcdr (kR body)))
  (evcdr (kR body))
  (.ite (F' (kA body))
    (.ite (S' (kA body)) (evcdr (kR body))
      (.ite (.pred (S' (kA body))) (P' (K' 2) (P' (kR body) (P' (S' (S' (kB body))) (.succ (kN body))))) (evcdr (kR body))))
    (evcdr (kR body)))

end

def stepE : E := sel4 (F' .x) (evE (S' .x)) (retE (S' .x)) (chkE (S' .x)) .x

theorem sub1 (n : Nat) : n + 1 - 1 = n := rfl

theorem cT_var (i : Nat) : cT (.var i) = pair 0 i := rfl
theorem cT_lam (b : Tm) : cT (.lam b) = pair 1 (cT b) := rfl
theorem cT_app (a b : Tm) : cT (.app a b) = pair 2 (pair (cT a) (cT b)) := rfl
theorem cK_cd (t : Tm) : cK (.cd t) = pair 0 (cT t) := rfl
theorem cK_sh (k c : Nat) (t : Tm) : cK (.sh k c t) = pair 1 (pair k (pair c (cT t))) := rfl
theorem cK_su (u : Tm) (k : Nat) (t : Tm) : cK (.su u k t) = pair 2 (pair (cT u) (pair k (cT t))) := rfl
theorem cF_lamK : cF .lamK = pair 0 0 := rfl
theorem cF_app1 (τ : Task) : cF (.app1 τ) = pair 1 (cK τ) := rfl
theorem cF_app2 (r : Tm) : cF (.app2 r) = pair 2 (cT r) := rfl
theorem cF_beta1 (r : Tm) : cF (.beta1 r) = pair 3 (cT r) := rfl
theorem cF_beta2 (r : Tm) : cF (.beta2 r) = pair 4 (cT r) := rfl
theorem cL_nil : cL [] = 0 := rfl
theorem cL_cons (f : Fr) (κ : List Fr) : cL (f :: κ) = pair (cF f) (cL κ) + 1 := rfl
theorem cS_ev (τ : Task) (κ : List Fr) : cS (.ev τ κ) = pair 0 (pair (cK τ) (cL κ)) := rfl
theorem cS_ret (r : Tm) (κ : List Fr) : cS (.ret r κ) = pair 1 (pair (cT r) (cL κ)) := rfl
theorem cS_chk (r b : Tm) (n : Nat) : cS (.chk r b n) = pair 2 (pair (cT r) (pair (cT b) n)) := rfl
theorem cS_done (n : Nat) : cS (.done n) = pair 3 n := rfl

macro "evstep" : tactic => `(tactic| (repeat (first | unfold stepE | unfold sel3 | unfold sel4 | unfold sel5 | unfold F' | unfold S' | unfold P' | unfold K' | unfold cons' | unfold lamKE | unfold evE | unfold retE | unfold chkE | unfold cdE | unfold shE | unfold suE | unfold cdGen | unfold cdA | unfold cdU | unfold evK | unfold evT | unfold evD | unfold p1 | unfold p2 | unfold p3 | unfold rR | unf  -- (+441 chars; full line in the Zenodo source)

theorem sub_zero' {a b : Nat} (h : a ≤ b) : a - b = 0 := by
  induction h with
  | refl => exact Nat.sub_self a
  | step _ ih => rw [Nat.sub_succ, ih]; rfl

theorem sub_pos' {a b : Nat} (h : a < b) : ∃ d, b - a = d + 1 := by
  obtain ⟨d, hd⟩ := LibGcd.le_dest h
  exact ⟨d, by rw [hd, show a + 1 + d = a + (d + 1) by lib_nat]; exact add_sub_left' a (d + 1)⟩

theorem shf_code (k c i : Nat) : ifz (c - i) (i + k) i = shf k c i := by
  cases Nat.lt_or_ge i c with
  | inl hl => obtain ⟨d, hd⟩ := sub_pos' hl; rw [hd, shf_lt hl]; rfl
  | inr hg => rw [sub_zero' hg, shf_ge hg]; rfl

set_option maxHeartbeats 4000000 in
theorem sc0 {v1 v2} : den stepE (cS (.ev (.cd (.var v1)) v2)) = cS (mstep (.ev (.cd (.var v1)) v2)) := by evstep; rfl

set_option maxHeartbeats 4000000 in
theorem sc1 {v1 v2} : den stepE (cS (.ev (.cd (.lam v1)) v2)) = cS (mstep (.ev (.cd (.lam v1)) v2)) := by evstep; rfl

set_option maxHeartbeats 4000000 in
theorem sc2 {v1 v2 v3} : den stepE (cS (.ev (.cd (.app (.lam v1) v2)) v3)) = cS (mstep (.ev (.cd (.app (.lam v1) v2)) v3)) := by evstep; rfl

set_option maxHeartbeats 4000000 in
theorem sc3 {v1 v2 v3} : den stepE (cS (.ev (.cd (.app (.var v1) v2)) v3)) = cS (mstep (.ev (.cd (.app (.var v1) v2)) v3)) := by evstep; rfl

set_option maxHeartbeats 4000000 in
theorem sc4 {v1 v2 v3 v4} : den stepE (cS (.ev (.cd (.app (.app v1 v2) v3)) v4)) = cS (mstep (.ev (.cd (.app (.app v1 v2) v3)) v4)) := by evstep; rfl

set_option maxHeartbeats 4000000 in
theorem sc5 {k c i v1} : den stepE (cS (.ev (.sh k c (.var i)) v1)) = cS (mstep (.ev (.sh k c (.var i)) v1)) := by evstep; rw [shf_code]; rfl

set_option maxHeartbeats 4000000 in
theorem sc6 {v1 v2 v3 v4} : den stepE (cS (.ev (.sh v1 v2 (.lam v3)) v4)) = cS (mstep (.ev (.sh v1 v2 (.lam v3)) v4)) := by evstep; rfl

set_option maxHeartbeats 4000000 in
theorem sc7 {v1 v2 v3 v4 v5} : den stepE (cS (.ev (.sh v1 v2 (.app v3 v4)) v5)) = cS (mstep (.ev (.sh v1 v2 (.app v3 v4)) v5)) := by evstep; rfl

set_option maxHeartbeats 4000000 in
theorem sc8 {u k i κ} : den stepE (cS (.ev (.su u k (.var i)) κ)) = cS (mstep (.ev (.su u k (.var i)) κ)) := by
    evstep
    show _ = cS (cond (Nat.blt i k) (.ret (.var i) κ) (cond (bq i k) (.ev (.sh k 0 u) κ) (.ret (.var (i - 1)) κ)))
    cases Nat.lt_or_ge i k with
    | inl hl => obtain ⟨d, hd⟩ := sub_pos' hl; rw [hd, blt_t hl]; rfl
    | inr hg =>
      rw [sub_zero' hg, blt_f hg]
      cases er_sub.le_cases' hg with
      | inl e => rw [e, Nat.sub_self, bq_self]; rfl
      | inr hl =>
        obtain ⟨d, hd⟩ := sub_pos' hl
        rw [hd, bq_ne i k (fun e => Nat.lt_irrefl _ (e ▸ hl))]; rfl

set_option maxHeartbeats 4000000 in
theorem sc9 {v1 v2 v3 v4} : den stepE (cS (.ev (.su v1 v2 (.lam v3)) v4)) = cS (mstep (.ev (.su v1 v2 (.lam v3)) v4)) := by evstep; rfl

set_option maxHeartbeats 4000000 in
theorem sc10 {v1 v2 v3 v4 v5} : den stepE (cS (.ev (.su v1 v2 (.app v3 v4)) v5)) = cS (mstep (.ev (.su v1 v2 (.app v3 v4)) v5)) := by evstep; rfl

set_option maxHeartbeats 4000000 in
theorem sc11 {v1 v2} : den stepE (cS (.ret v1 (.lamK :: v2))) = cS (mstep (.ret v1 (.lamK :: v2))) := by evstep; rfl

set_option maxHeartbeats 4000000 in
theorem sc12 {v1 v2 v3} : den stepE (cS (.ret v1 (.app1 v2 :: v3))) = cS (mstep (.ret v1 (.app1 v2 :: v3))) := by evstep; rfl

set_option maxHeartbeats 4000000 in
theorem sc13 {v1 v2 v3} : den stepE (cS (.ret v1 (.app2 v2 :: v3))) = cS (mstep (.ret v1 (.app2 v2 :: v3))) := by evstep; rfl

set_option maxHeartbeats 4000000 in
theorem sc14 {v1 v2 v3} : den stepE (cS (.ret v1 (.beta1 v2 :: v3))) = cS (mstep (.ret v1 (.beta1 v2 :: v3))) := by evstep; rfl

set_option maxHeartbeats 4000000 in
theorem sc15 {v1 v2 v3} : den stepE (cS (.ret v1 (.beta2 v2 :: v3))) = cS (mstep (.ret v1 (.beta2 v2 :: v3))) := by evstep; rfl

set_option maxHeartbeats 4000000 in
theorem sc16 {v1} : den stepE (cS (.ret (.lam (.lam v1)) [])) = cS (mstep (.ret (.lam (.lam v1)) [])) := by evstep; rfl

set_option maxHeartbeats 4000000 in
theorem sc17 {v1} : den stepE (cS (.ret (.lam (.var v1)) [])) = cS (mstep (.ret (.lam (.var v1)) [])) := by evstep; rfl

set_option maxHeartbeats 4000000 in
theorem sc18 {v1 v2} : den stepE (cS (.ret (.lam (.app v1 v2)) [])) = cS (mstep (.ret (.lam (.app v1 v2)) [])) := by evstep; rfl

set_option maxHeartbeats 4000000 in
theorem sc19 {v1} : den stepE (cS (.ret (.var v1) [])) = cS (mstep (.ret (.var v1) [])) := by evstep; rfl

set_option maxHeartbeats 4000000 in
theorem sc20 {v1 v2} : den stepE (cS (.ret (.app v1 v2) [])) = cS (mstep (.ret (.app v1 v2) [])) := by evstep; rfl

set_option maxHeartbeats 4000000 in
theorem sc21 {v1 v2} : den stepE (cS (.chk v1 (.var 0) v2)) = cS (mstep (.chk v1 (.var 0) v2)) := by evstep; rfl

set_option maxHeartbeats 4000000 in
theorem sc22 {v1 v2 v3} : den stepE (cS (.chk v1 (.var (v2 + 1)) v3)) = cS (mstep (.chk v1 (.var (v2 + 1)) v3)) := by evstep; rfl

set_option maxHeartbeats 4000000 in
theorem sc23 {v1 v2 v3} : den stepE (cS (.chk v1 (.lam v2) v3)) = cS (mstep (.chk v1 (.lam v2) v3)) := by evstep; rfl

set_option maxHeartbeats 4000000 in
theorem sc24 {v1 v2 v3} : den stepE (cS (.chk v1 (.app (.var 1) v2) v3)) = cS (mstep (.chk v1 (.app (.var 1) v2) v3)) := by evstep; rfl

set_option maxHeartbeats 4000000 in
theorem sc25 {v1 v2 v3} : den stepE (cS (.chk v1 (.app (.var 0) v2) v3)) = cS (mstep (.chk v1 (.app (.var 0) v2) v3)) := by evstep; rfl

set_option maxHeartbeats 4000000 in
theorem sc26 {v1 v2 v3 v4} : den stepE (cS (.chk v1 (.app (.var (v2 + 2)) v3) v4)) = cS (mstep (.chk v1 (.app (.var (v2 + 2)) v3) v4)) := by evstep; rfl

set_option maxHeartbeats 4000000 in
theorem sc27 {v1 v2 v3 v4} : den stepE (cS (.chk v1 (.app (.lam v2) v3) v4)) = cS (mstep (.chk v1 (.app (.lam v2) v3) v4)) := by evstep; rfl

set_option maxHeartbeats 4000000 in
theorem sc28 {v1 v2 v3 v4 v5} : den stepE (cS (.chk v1 (.app (.app v2 v3) v4) v5)) = cS (mstep (.chk v1 (.app (.app v2 v3) v4) v5)) := by evstep; rfl

set_option maxHeartbeats 4000000 in
theorem sc29 {v1} : den stepE (cS (.done v1)) = cS (mstep (.done v1)) := by evstep; rfl

/-- the arithmetised step is the machine step -/
theorem step_code : (s : St) → den stepE (cS s) = cS (mstep s)
  | .ev (.cd (.var _)) _ => sc0
  | .ev (.cd (.lam _)) _ => sc1
  | .ev (.cd (.app (.lam _) _)) _ => sc2
  | .ev (.cd (.app (.var _) _)) _ => sc3
  | .ev (.cd (.app (.app _ _) _)) _ => sc4
  | .ev (.sh _ _ (.var _)) _ => sc5
  | .ev (.sh _ _ (.lam _)) _ => sc6
  | .ev (.sh _ _ (.app _ _)) _ => sc7
  | .ev (.su _ _ (.var _)) _ => sc8
  | .ev (.su _ _ (.lam _)) _ => sc9
  | .ev (.su _ _ (.app _ _)) _ => sc10
  | .ret _ (.lamK :: _) => sc11
  | .ret _ (.app1 _ :: _) => sc12
  | .ret _ (.app2 _ :: _) => sc13
  | .ret _ (.beta1 _ :: _) => sc14
  | .ret _ (.beta2 _ :: _) => sc15
  | .ret (.lam (.lam _)) [] => sc16
  | .ret (.lam (.var _)) [] => sc17
  | .ret (.lam (.app _ _)) [] => sc18
  | .ret (.var _) [] => sc19
  | .ret (.app _ _) [] => sc20
  | .chk _ (.var 0) _ => sc21
  | .chk _ (.var (_ + 1)) _ => sc22
  | .chk _ (.lam _) _ => sc23
  | .chk _ (.app (.var 1) _) _ => sc24
  | .chk _ (.app (.var 0) _) _ => sc25
  | .chk _ (.app (.var (_ + 2)) _) _ => sc26
  | .chk _ (.app (.lam _) _) _ => sc27
  | .chk _ (.app (.app _ _) _) _ => sc28
  | .done _ => sc29

end LM

end Found

#print axioms Found.LM.toPR_eval
#print axioms Found.LM.shf_code
#print axioms Found.LM.step_code

found/LamMu4.lean

lean_all/found/LamMu4.lean

/-
  Line λ → μ, part 4. found/LamMu4 — λ-definable functions are μ-recursive.
  For a closed λ-term F and arity k, the μ-recursive program lamToMu F k
  computes the code of F applied to the Church numerals of its arguments,
  runs the arithmetised machine of found/LamMu3 for t steps (primitive
  recursion), searches the least t at which it has stopped (μ), and reads
  off the value.  If F ⌜a₁⌝ … ⌜a_k⌝ →β* ⌜n⌝ then lamToMu F k evaluates to n
  on [a₁, …, a_k] (lambda_to_mu).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.LamMu3

namespace Found

namespace LM

open UL Rec RM CT3

/-! ## iterating the machine -/

def miter : Nat → St → St
  | 0, s => s
  | t + 1, s => mstep (miter t s)

theorem miter_succ' : (t : Nat) → (s : St) → miter (t + 1) s = miter t (mstep s)
  | 0, _ => rfl
  | t + 1, s => by show mstep (miter (t + 1) s) = mstep (miter t (mstep s)); rw [miter_succ' t s]

theorem reach_iter {s s' : St} (h : MR' s s') : ∃ t, miter t s = s' := by
  induction h with
  | refl s => exact ⟨0, rfl⟩
  | @step s _ _ ih => match ih with
    | ⟨t, e⟩ => exact ⟨t + 1, by rw [miter_succ']; exact e⟩

theorem done_stays (n : Nat) : (t : Nat) → miter t (.done n) = .done n
  | 0 => rfl
  | t + 1 => by show mstep (miter t (.done n)) = _; rw [done_stays n t]; rfl

def stepP : PR := toPR stepE

theorem stepP_eval (s : St) : eval stepP [cS s] = cS (mstep s) := by
  unfold stepP; rw [toPR_eval, step_code]

/-- run, at [t, x0] -/
def runP' : PR := .prec (.proj 0) (.comp stepP [.proj 1])

theorem runP'_eval (s : St) : (t : Nat) → eval runP' [t, cS s] = cS (miter t s)
  | 0 => rfl
  | t + 1 => by
    show eval stepP [eval runP' [t, cS s]] = _
    rw [runP'_eval s t, stepP_eval]; rfl

def haltE : E := .ite (.sub (.fst .x) (.c 2)) (.c 1) (.c 0)

theorem halt_val : (s : St) → (den haltE (cS s) = 0 ↔ ∃ n, s = .done n)
  | .ev τ κ => ⟨fun h => by
      have : den haltE (pair 0 (pair (cK τ) (cL κ))) = 1 := by
        show ifz (fstU (pair 0 _) - 2) 1 0 = 1; rw [fstU_pair]; rfl
      exact absurd (this.symm.trans h) (fun e => Nat.noConfusion e), fun ⟨_, e⟩ => St.noConfusion e⟩
  | .ret r κ => ⟨fun h => by
      have : den haltE (pair 1 (pair (cT r) (cL κ))) = 1 := by
        show ifz (fstU (pair 1 _) - 2) 1 0 = 1; rw [fstU_pair]; rfl
      exact absurd (this.symm.trans h) (fun e => Nat.noConfusion e), fun ⟨_, e⟩ => St.noConfusion e⟩
  | .chk r b n => ⟨fun h => by
      have : den haltE (pair 2 (pair (cT r) (pair (cT b) n))) = 1 := by
        show ifz (fstU (pair 2 _) - 2) 1 0 = 1; rw [fstU_pair]; rfl
      exact absurd (this.symm.trans h) (fun e => Nat.noConfusion e), fun ⟨_, e⟩ => St.noConfusion e⟩
  | .done n => ⟨fun _ => ⟨n, rfl⟩, fun _ => by
      show ifz (fstU (pair 3 n) - 2) 1 0 = 0; rw [fstU_pair]; rfl⟩

/-! ## the start code from the arguments -/

def iterP : PR := .prec (cst (pair 0 0)) (.comp pairP [cst 2, .comp pairP [cst (pair 0 1), .proj 1]])

theorem iterP_eval : (a : Nat) → eval iterP [a] = cT (iterN a)
  | 0 => cst_eval [] _
  | a + 1 => by
    show eval pairP [eval (cst 2) _, eval pairP [eval (cst (pair 0 1)) _, eval iterP [a]]] = _
    rw [iterP_eval a, cst_eval, cst_eval, pairP_eval, pairP_eval]; rfl

def churchP : PR := .comp pairP [cst 1, .comp pairP [cst 1, iterP]]

theorem churchP_eval (a : Nat) : eval churchP [a] = cT (churchN a) := by
  show eval pairP [eval (cst 1) [a], eval pairP [eval (cst 1) [a], eval iterP [a]]] = _
  rw [iterP_eval, cst_eval, pairP_eval, pairP_eval]; rfl

/-- F applied to the numerals of the first j arguments -/
def termAt (F : T []) (args : List Nat) : Nat → T []
  | 0 => F
  | j + 1 => .app (termAt F args j) (church (Rec.nth args j))

def initT (F : T []) : Nat → PR
  | 0 => cst (cT (er F))
  | j + 1 => .comp pairP [cst 2, .comp pairP [initT F j, .comp churchP [.proj j]]]

theorem initT_eval (F : T []) (args : List Nat) : (j : Nat) → eval (initT F j) args = cT (er (termAt F args j))
  | 0 => cst_eval _ _
  | j + 1 => by
    show eval pairP [eval (cst 2) args, eval pairP [eval (initT F j) args, eval churchP [Rec.nth args j]]] = _
    rw [initT_eval F args j, churchP_eval, cst_eval, pairP_eval, pairP_eval, ← er_church]; rfl

def x0P (F : T []) (k : Nat) : PR := .comp pairP [cst 1, .comp pairP [initT F k, cst 0]]

theorem x0P_eval (F : T []) (k : Nat) (args : List Nat) :
    eval (x0P F k) args = cS (.ret (er (termAt F args k)) []) := by
  show eval pairP [eval (cst 1) args, eval pairP [eval (initT F k) args, eval (cst 0) args]] = _
  rw [initT_eval, cst_eval, cst_eval, pairP_eval, pairP_eval]; rfl

/-! ## the μ-recursive program -/

/-- at [t, x0]: 0 once the machine has stopped after t steps -/
def haltF : PR := .comp (toPR haltE) [runP']

/-- at [x0]: run until stopped and read the value -/
def outM : MR := .comp (emb (toPR (.snd .x))) [.comp (emb runP') [.mu (emb haltF), .proj 0]]

def lamToMu (F : T []) (k : Nat) : MR := .comp outM [emb (x0P F k)]

def s0 (F : T []) (args : List Nat) (k : Nat) : St := .ret (er (termAt F args k)) []

theorem haltF_eval (s : St) (t : Nat) : eval haltF [t, cS s] = den haltE (cS (miter t s)) := by
  show eval (toPR haltE) [eval runP' [t, cS s]] = _
  rw [runP'_eval, toPR_eval]

theorem miter_add (s : St) (a : Nat) : (b : Nat) → miter (a + b) s = miter b (miter a s)
  | 0 => rfl
  | b + 1 => by show mstep (miter (a + b) s) = mstep (miter b (miter a s)); rw [miter_add s a b]

def gsearch (v : Nat → Nat) (j : Nat) : Nat → Nat
  | 0 => j
  | k + 1 => match v j with
    | 0 => j
    | _ + 1 => gsearch v (j + 1) k

theorem gsearch_spec (f : PR) (rest : List Nat) : (k j : Nat) → eval f ((j + k) :: rest) = 0 →
    MuS (emb f) rest j (gsearch (fun t => eval f (t :: rest)) j k) ∧
      eval f (gsearch (fun t => eval f (t :: rest)) j k :: rest) = 0
  | 0, j, h => by
    have ef := emb_ev f (j :: rest)
    have h0 : eval f (j :: rest) = 0 := h
    rw [h0] at ef
    exact ⟨.stop ef, h0⟩
  | k + 1, j, h => by
    have ef := emb_ev f (j :: rest)
    show MuS (emb f) rest j (match eval f (j :: rest) with | 0 => j | _ + 1 => gsearch _ (j + 1) k) ∧
      eval f ((match eval f (j :: rest) with | 0 => j | _ + 1 => gsearch _ (j + 1) k) :: rest) = 0
    cases hj : eval f (j :: rest) with
    | zero => rw [hj] at ef; exact ⟨.stop ef, hj⟩
    | succ n =>
      rw [hj] at ef
      have ih := gsearch_spec f rest k (j + 1) (by rw [Nat.add_right_comm, Nat.add_assoc]; exact h)
      exact ⟨.next ef ih.1, ih.2⟩

/-- λ-definable ⟹ μ-recursive -/
theorem lambda_to_mu (F : T []) (args : List Nat) (n : Nat)
    (h : Star Beta (termAt F args args.length) (church n)) :
    Ev (lamToMu F args.length) args n := by
  have hm := machine_value h
  obtain ⟨t0, ht⟩ := reach_iter hm
  let S0 := s0 F args args.length
  have h0 : eval haltF ((0 + t0) :: [cS S0]) = 0 := by
    rw [Nat.zero_add, haltF_eval]
    show den haltE (cS (miter t0 (.ret (er (termAt F args args.length)) []))) = 0
    rw [ht]
    exact (halt_val (.done n)).2 ⟨n, rfl⟩
  have hs := gsearch_spec haltF [cS S0] t0 0 h0
  generalize gsearch _ 0 t0 = m at hs
  have hm0 := hs.2
  rw [haltF_eval] at hm0
  obtain ⟨n', hn'⟩ := (halt_val _).1 hm0
  have h1 : miter (t0 + m) S0 = .done n := by
    rw [miter_add]
    show miter m (miter t0 (.ret (er (termAt F args args.length)) [])) = _
    rw [ht, done_stays]
  have h2 : miter (t0 + m) S0 = .done n' := by
    rw [Nat.add_comm, miter_add, hn', done_stays]
  have e : n' = n := St.done.inj (h2.symm.trans h1)
  have hx : eval runP' [m, cS S0] = cS (.done n) := by
    rw [runP'_eval, hn', e]
  have ho : eval (toPR (.snd .x)) [eval runP' [m, cS S0]] = n := by
    rw [hx, toPR_eval]; show sndU (pair 3 n) = n; rw [sndU_pair]
  have hx0 : eval (x0P F args.length) args = cS S0 := x0P_eval F args.length args
  have evOut : Ev outM [cS S0] n := by
    rw [← ho]
    exact .comp (.cons (.comp (.cons (.mu hs.1) (.cons (.proj 0 [cS S0]) (.nil _)))
      (emb_ev runP' _)) (.nil _)) (emb_ev _ _)
  have ex := emb_ev (x0P F args.length) args
  rw [hx0] at ex
  exact .comp (.cons ex (.nil _)) evOut

end LM

end Found

#print axioms Found.LM.reach_iter
#print axioms Found.LM.stepP_eval
#print axioms Found.LM.runP'_eval
#print axioms Found.LM.halt_val
#print axioms Found.LM.x0P_eval
#print axioms Found.LM.haltF_eval
#print axioms Found.LM.gsearch_spec
#print axioms Found.LM.lambda_to_mu

found/Lambda.lean

lean_all/found/Lambda.lean

/-
  T78. found/Lambda — the untyped λ-calculus and Church–Rosser (scopes indexed by List Unit: Nat-indexed matching pulls in propext).

  * Well-scoped de Bruijn terms `T n`, renaming and simultaneous
    substitution; the σ-calculus laws are proved pointwise (no funext).
  * β-reduction, parallel reduction, complete development (Takahashi);
    triangle lemma, diamond for parallel reduction, Church–Rosser for β*.
  * Consequence: a term has at most one β-normal form.

  Lean 4 core only, hand-written proofs, no arithmetic, no axioms.
-/

namespace Found

namespace UL

inductive V : List Unit → Type where
  | z {n} : V (() :: n)
  | s {n} : V n → V (() :: n)

inductive T : List Unit → Type where
  | var {n} : V n → T n
  | app {n} : T n → T n → T n
  | lam {n} : T (() :: n) → T n

def lift {m n : List Unit} (r : V m → V n) : V (() :: m) → V (() :: n)
  | .z => .z
  | .s v => .s (r v)

def ren {m n : List Unit} (r : V m → V n) : T m → T n
  | .var v => .var (r v)
  | .app a b => .app (ren r a) (ren r b)
  | .lam t => .lam (ren (lift r) t)

def liftS {m n : List Unit} (σ : V m → T n) : V (() :: m) → T (() :: n)
  | .z => .var .z
  | .s v => ren V.s (σ v)

def sub {m n : List Unit} (σ : V m → T n) : T m → T n
  | .var v => σ v
  | .app a b => .app (sub σ a) (sub σ b)
  | .lam t => .lam (sub (liftS σ) t)

def single {n : List Unit} (u : T n) : V (() :: n) → T n
  | .z => u
  | .s v => .var v

def inst {n : List Unit} (t : T (() :: n)) (u : T n) : T n := sub (single u) t

/-! ## σ-calculus, pointwise -/

theorem lift_ext {m n : List Unit} {r r' : V m → V n} (h : ∀ v, r v = r' v) : ∀ v, lift r v = lift r' v
  | .z => rfl
  | .s v => congrArg V.s (h v)

theorem cong2 {α β γ : Type} (f : α → β → γ) {a a' : α} {b b' : β} (h1 : a = a') (h2 : b = b') :
    f a b = f a' b' := by
  cases h1
  cases h2
  rfl

theorem ren_ext : {m : List Unit} → (t : T m) → ∀ {n : List Unit} {r r' : V m → V n},
    (∀ v, r v = r' v) → ren r t = ren r' t
  | _, .var v, _, _, _, h => congrArg T.var (h v)
  | _, .app a b, _, _, _, h => cong2 T.app (ren_ext a h) (ren_ext b h)
  | _, .lam t, _, _, _, h => congrArg T.lam (ren_ext t (lift_ext h))

theorem ren_ren : {m : List Unit} → (t : T m) → ∀ {n k : List Unit} (r : V n → V k) (r' : V m → V n),
    ren r (ren r' t) = ren (fun x => r (r' x)) t
  | _, .var _, _, _, _, _ => rfl
  | _, .app a b, _, _, r, r' => cong2 T.app (ren_ren a r r') (ren_ren b r r')
  | _, .lam t, _, _, r, r' => congrArg T.lam ((ren_ren t (lift r) (lift r')).trans
      (ren_ext t (fun v => match v with | .z => rfl | .s _ => rfl)))

theorem liftS_ext {m n : List Unit} {σ τ : V m → T n} (h : ∀ v, σ v = τ v) : ∀ v, liftS σ v = liftS τ v
  | .z => rfl
  | .s v => congrArg (ren V.s) (h v)

theorem sub_ext : {m : List Unit} → (t : T m) → ∀ {n : List Unit} {σ τ : V m → T n},
    (∀ v, σ v = τ v) → sub σ t = sub τ t
  | _, .var v, _, _, _, h => h v
  | _, .app a b, _, _, _, h => cong2 T.app (sub_ext a h) (sub_ext b h)
  | _, .lam t, _, _, _, h => congrArg T.lam (sub_ext t (liftS_ext h))

theorem sub_ren : {m : List Unit} → (t : T m) → ∀ {n k : List Unit} (σ : V n → T k) (r : V m → V n),
    sub σ (ren r t) = sub (fun x => σ (r x)) t
  | _, .var _, _, _, _, _ => rfl
  | _, .app a b, _, _, σ, r => cong2 T.app (sub_ren a σ r) (sub_ren b σ r)
  | _, .lam t, _, _, σ, r => congrArg T.lam ((sub_ren t (liftS σ) (lift r)).trans
      (sub_ext t (fun v => match v with | .z => rfl | .s _ => rfl)))

theorem ren_sub : {m : List Unit} → (t : T m) → ∀ {n k : List Unit} (r : V n → V k) (σ : V m → T n),
    ren r (sub σ t) = sub (fun x => ren r (σ x)) t
  | _, .var _, _, _, _, _ => rfl
  | _, .app a b, _, _, r, σ => cong2 T.app (ren_sub a r σ) (ren_sub b r σ)
  | _, .lam t, _, _, r, σ => congrArg T.lam ((ren_sub t (lift r) (liftS σ)).trans
      (sub_ext t (fun v => match v with
        | .z => rfl
        | .s w => (ren_ren (σ w) (lift r) V.s).trans (ren_ren (σ w) V.s r).symm)))

theorem sub_sub : {m : List Unit} → (t : T m) → ∀ {n k : List Unit} (σ : V n → T k) (τ : V m → T n),
    sub σ (sub τ t) = sub (fun x => sub σ (τ x)) t
  | _, .var _, _, _, _, _ => rfl
  | _, .app a b, _, _, σ, τ => cong2 T.app (sub_sub a σ τ) (sub_sub b σ τ)
  | _, .lam t, _, _, σ, τ => congrArg T.lam ((sub_sub t (liftS σ) (liftS τ)).trans
      (sub_ext t (fun v => match v with
        | .z => rfl
        | .s w => (sub_ren (τ w) (liftS σ) V.s).trans (ren_sub (τ w) V.s σ).symm)))

theorem sub_var : {m : List Unit} → (t : T m) → sub T.var t = t
  | _, .var _ => rfl
  | _, .app a b => cong2 T.app (sub_var a) (sub_var b)
  | _, .lam t => congrArg T.lam
      ((sub_ext t (σ := liftS T.var) (τ := T.var) (fun v => match v with | .z => rfl | .s _ => rfl)).trans (sub_var t))

/-- substitution commutes with instantiation -/
theorem sub_inst {m n : List Unit} (σ : V m → T n) (t : T (() :: m)) (u : T m) :
    inst (sub (liftS σ) t) (sub σ u) = sub σ (inst t u) :=
  (sub_sub t (single (sub σ u)) (liftS σ)).trans ((sub_ext t
    (σ := fun x => sub (single (sub σ u)) (liftS σ x)) (τ := fun x => sub σ (single u x))
    (fun v => match v with
    | .z => rfl
    | .s w => (sub_ren (σ w) (single (sub σ u)) V.s).trans (sub_var (σ w)))).trans (sub_sub t σ (single u)).symm)

theorem ren_inst {m n : List Unit} (r : V m → V n) (t : T (() :: m)) (u : T m) :
    ren r (inst t u) = inst (ren (lift r) t) (ren r u) :=
  (ren_sub t r (single u)).trans ((sub_ext t
    (σ := fun x => ren r (single u x)) (τ := fun x => single (ren r u) (lift r x))
    (fun v => match v with
    | .z => rfl
    | .s _ => rfl)).trans (sub_ren t (single (ren r u)) (lift r)).symm)

/-! ## Reductions -/

inductive Beta : {n : List Unit} → T n → T n → Prop where
  | beta {n} (t : T (() :: n)) (u : T n) : Beta (.app (.lam t) u) (inst t u)
  | appL {n} {a a' : T n} (b : T n) : Beta a a' → Beta (.app a b) (.app a' b)
  | appR {n} (a : T n) {b b' : T n} : Beta b b' → Beta (.app a b) (.app a b')
  | lam {n} {t t' : T (() :: n)} : Beta t t' → Beta (.lam t) (.lam t')

inductive Star {α : Type} (R : α → α → Prop) : α → α → Prop where
  | refl (a : α) : Star R a a
  | step {a b c : α} : R a b → Star R b c → Star R a c

theorem Star.trans' {α : Type} {R : α → α → Prop} {a b c : α} (h : Star R a b) (k : Star R b c) :
    Star R a c := by
  induction h with
  | refl => exact k
  | step r _ ih => exact .step r (ih k)

theorem Star.one {α : Type} {R : α → α → Prop} {a b : α} (r : R a b) : Star R a b := .step r (.refl b)

theorem Star.map {α β : Type} {R : α → α → Prop} {S : β → β → Prop} (f : α → β)
    (hf : ∀ {a b}, R a b → S (f a) (f b)) {a b : α} (h : Star R a b) : Star S (f a) (f b) := by
  induction h with
  | refl => exact .refl _
  | step r _ ih => exact .step (hf r) ih

inductive Par : {n : List Unit} → T n → T n → Prop where
  | var {n} (v : V n) : Par (.var v) (.var v)
  | app {n} {a a' b b' : T n} : Par a a' → Par b b' → Par (.app a b) (.app a' b')
  | lam {n} {t t' : T (() :: n)} : Par t t' → Par (.lam t) (.lam t')
  | beta {n} {t t' : T (() :: n)} {u u' : T n} : Par t t' → Par u u' → Par (.app (.lam t) u) (inst t' u')

theorem par_refl {n : List Unit} (t : T n) : Par t t := by
  induction t with
  | var v => exact .var v
  | app a b iha ihb => exact .app iha ihb
  | lam t ih => exact .lam ih

theorem par_ren {m : List Unit} {t t' : T m} (h : Par t t') : ∀ {n : List Unit} (r : V m → V n), Par (ren r t) (ren r t') := by
  induction h with
  | var v => exact fun r => .var (r v)
  | app _ _ iha ihb => exact fun r => .app (iha r) (ihb r)
  | lam _ ih => exact fun r => .lam (ih (lift r))
  | @beta _ t t' u u' _ _ iht ihu =>
    exact fun r => (ren_inst r t' u') ▸ Par.beta (iht (lift r)) (ihu r)

theorem par_sub {m : List Unit} {t t' : T m} (h : Par t t') :
    ∀ {n : List Unit} {σ τ : V m → T n}, (∀ v, Par (σ v) (τ v)) → Par (sub σ t) (sub τ t') := by
  induction h with
  | var v => exact fun hs => hs v
  | app _ _ iha ihb => exact fun hs => .app (iha hs) (ihb hs)
  | lam _ ih => exact fun hs => .lam (ih (fun v => match v with
      | .z => .var .z
      | .s w => par_ren (hs w) V.s))
  | @beta _ t t' u u' _ _ iht ihu =>
    exact fun {_ σ τ} hs => (sub_inst τ t' u') ▸ Par.beta (iht (fun v => match v with
      | .z => .var .z
      | .s w => par_ren (hs w) V.s)) (ihu hs)

theorem par_inst {n : List Unit} {t t' : T (() :: n)} {u u' : T n} (ht : Par t t') (hu : Par u u') :
    Par (inst t u) (inst t' u') :=
  par_sub ht (fun v => match v with
    | .z => hu
    | .s w => .var w)

/-! ## Complete development and the triangle -/

def cd {n : List Unit} : T n → T n
  | .var v => .var v
  | .lam t => .lam (cd t)
  | .app (.lam t) u => inst (cd t) (cd u)
  | .app (.var v) u => .app (.var v) (cd u)
  | .app (.app a b) u => .app (cd (.app a b)) (cd u)

theorem par_lam_inv {n : List Unit} {t : T (() :: n)} {s : T n} (h : Par (.lam t) s) :
    ∃ t', s = .lam t' ∧ Par t t' := by
  cases h with
  | lam k => exact ⟨_, rfl, k⟩

theorem triangle {n : List Unit} {t u : T n} (h : Par t u) : Par u (cd t) := by
  induction h with
  | var v => exact .var v
  | lam _ ih => exact .lam ih
  | @app _ a a' b b' ha _ iha ihb =>
    cases a with
    | var v => cases ha; exact .app (.var v) ihb
    | app a₁ a₂ => exact .app iha ihb
    | lam t =>
      match par_lam_inv ha with
      | ⟨t', e, k⟩ =>
        cases e
        match par_lam_inv iha with
        | ⟨_, e', k'⟩ =>
          cases e'
          exact .beta k' ihb
  | beta _ _ iht ihu => exact par_inst iht ihu

theorem par_diamond {n : List Unit} {t u₁ u₂ : T n} (h₁ : Par t u₁) (h₂ : Par t u₂) :
    ∃ w, Par u₁ w ∧ Par u₂ w := ⟨cd t, triangle h₁, triangle h₂⟩

/-! ## Church–Rosser -/

theorem strip {n : List Unit} {t u₁ u₂ : T n} (h₁ : Par t u₁) (h₂ : Star Par t u₂) :
    ∃ w, Star Par u₁ w ∧ Par u₂ w := by
  induction h₂ generalizing u₁ with
  | refl => exact ⟨u₁, .refl _, h₁⟩
  | step r _ ih =>
    match par_diamond h₁ r with
    | ⟨w, k₁, k₂⟩ =>
      match ih k₂ with
      | ⟨w', s, k⟩ => exact ⟨w', .step k₁ s, k⟩

theorem par_cr {n : List Unit} {t u₁ u₂ : T n} (h₁ : Star Par t u₁) (h₂ : Star Par t u₂) :
    ∃ w, Star Par u₁ w ∧ Star Par u₂ w := by
  induction h₁ generalizing u₂ with
  | refl => exact ⟨u₂, h₂, .refl _⟩
  | step r _ ih =>
    match strip r h₂ with
    | ⟨w, s, k⟩ =>
      match ih s with
      | ⟨w', s₁, s₂⟩ => exact ⟨w', s₁, .step k s₂⟩

theorem beta_par {n : List Unit} {t u : T n} (h : Beta t u) : Par t u := by
  induction h with
  | beta t u => exact .beta (par_refl t) (par_refl u)
  | appL b _ ih => exact .app ih (par_refl b)
  | appR a _ ih => exact .app (par_refl a) ih
  | lam _ ih => exact .lam ih

theorem par_betas {n : List Unit} {t u : T n} (h : Par t u) : Star Beta t u := by
  induction h with
  | var v => exact .refl _
  | @app _ a a' b b' _ _ iha ihb =>
    exact (Star.map (fun x => T.app x b) (fun r => .appL b r) iha).trans'
      (Star.map (fun y => T.app a' y) (fun r => .appR a' r) ihb)
  | lam _ ih => exact Star.map T.lam (fun r => .lam r) ih
  | @beta _ t t' u u' _ _ iht ihu =>
    exact ((Star.map (fun x => T.app (T.lam x) u) (fun r => .appL u (.lam r)) iht).trans'
      (Star.map (fun y => T.app (T.lam t') y) (fun r => .appR _ r) ihu)).trans' (.one (.beta t' u'))

theorem betas_pars {n : List Unit} {t u : T n} (h : Star Beta t u) : Star Par t u :=
  Star.map (fun x => x) (fun r => beta_par r) h

theorem pars_betas {n : List Unit} {t u : T n} (h : Star Par t u) : Star Beta t u := by
  induction h with
  | refl => exact .refl _
  | step r _ ih => exact (par_betas r).trans' ih

/-- Church–Rosser for β -/
theorem church_rosser {n : List Unit} {t u₁ u₂ : T n} (h₁ : Star Beta t u₁) (h₂ : Star Beta t u₂) :
    ∃ w, Star Beta u₁ w ∧ Star Beta u₂ w :=
  match par_cr (betas_pars h₁) (betas_pars h₂) with
  | ⟨w, s₁, s₂⟩ => ⟨w, pars_betas s₁, pars_betas s₂⟩

def Normal {n : List Unit} (t : T n) : Prop := ∀ u, ¬ Beta t u

theorem normal_star {n : List Unit} {t u : T n} (hn : Normal t) (h : Star Beta t u) : t = u := by
  cases h with
  | refl => rfl
  | step r _ => exact (hn _ r).elim

/-- uniqueness of normal forms -/
theorem nf_unique {n : List Unit} {t u₁ u₂ : T n} (h₁ : Star Beta t u₁) (h₂ : Star Beta t u₂)
    (n₁ : Normal u₁) (n₂ : Normal u₂) : u₁ = u₂ :=
  match church_rosser h₁ h₂ with
  | ⟨_, s₁, s₂⟩ => (normal_star n₁ s₁).trans (normal_star n₂ s₂).symm

end UL

end Found

#print axioms Found.UL.ren_ren
#print axioms Found.UL.sub_ren
#print axioms Found.UL.ren_sub
#print axioms Found.UL.sub_sub
#print axioms Found.UL.sub_var
#print axioms Found.UL.sub_inst
#print axioms Found.UL.ren_inst
#print axioms Found.UL.par_ren
#print axioms Found.UL.par_sub
#print axioms Found.UL.triangle
#print axioms Found.UL.par_diamond
#print axioms Found.UL.church_rosser
#print axioms Found.UL.nf_unique

found/Lambek.lean

lean_all/found/Lambek.lean

/-
  T112. found/Lambek — the Lambek calculus as a point: residuated monoids
  without exchange (two residuals \ and /), a free parameter.

  * Lambek algebra: preorder, associative ⊗, a ⊗ b ≤ c ⟺ b ≤ a \ c ⟺ a ≤ c / b.
  * Instance: languages over any alphabet (Prop-valued sets of words) with
    concatenation — the standard model; it is not commutative.
  * Line to linear logic (T68): with exchange (a⊗b ≤ b⊗a) the two residuals
    coincide up to ≤ both ways, which is the CRL case.

  Lean 4 core only, hand-written proofs, no axioms.
-/

namespace Found

namespace Lambek

universe u

structure LA where
  car : Type u
  le : car → car → Prop
  le_refl : ∀ a, le a a
  le_trans : ∀ {a b c}, le a b → le b c → le a c
  tens : car → car → car
  under : car → car → car     -- a \ c
  over : car → car → car      -- c / b
  res_r : ∀ {a b c}, le (tens a b) c ↔ le b (under a c)
  res_l : ∀ {a b c}, le (tens a b) c ↔ le a (over c b)

/-- with exchange the two residuals agree -/
theorem exchange_residuals (L : LA.{u}) (exch : ∀ a b, L.le (L.tens a b) (L.tens b a)) (a c : L.car) :
    L.le (L.under a c) (L.over c a) ∧ L.le (L.over c a) (L.under a c) :=
  ⟨L.res_l.mp (L.le_trans (exch _ _) (L.res_r.mpr (L.le_refl _))),
   L.res_r.mp (L.le_trans (exch _ _) (L.res_l.mpr (L.le_refl _)))⟩

/-! ## languages -/

section Lang
variable (α : Type u)

def Lang : Type u := List α → Prop

def cat (L K : Lang α) : Lang α := fun w => ∃ x y, w = x ++ y ∧ L x ∧ K y
def under (L K : Lang α) : Lang α := fun y => ∀ x, L x → K (x ++ y)
def over (K L : Lang α) : Lang α := fun x => ∀ y, L y → K (x ++ y)
def sub (L K : Lang α) : Prop := ∀ w, L w → K w

def langLA : LA.{u} where
  car := Lang α
  le := sub α
  le_refl _ _ h := h
  le_trans h k w x := k w (h w x)
  tens := cat α
  under := under α
  over := over α
  res_r := ⟨fun h y m x l => h _ ⟨x, y, rfl, l, m⟩,
            fun h _ ⟨x, y, e, l, m⟩ => e ▸ h y m x l⟩
  res_l := ⟨fun h x l y m => h _ ⟨x, y, rfl, l, m⟩,
            fun h _ ⟨x, y, e, l, m⟩ => e ▸ h x l y m⟩

end Lang

def single {α : Type} (w : List α) : Lang α := fun v => v = w

/-- the language model is not commutative: {a}{b} ⊄ {b}{a} for a ≠ b -/
theorem not_commutative : ¬ sub Bool (cat Bool (single [true]) (single [false]))
    (cat Bool (single [false]) (single [true])) := by
  intro h
  match h [true, false] ⟨[true], [false], rfl, rfl, rfl⟩ with
  | ⟨x, y, e, hx, hy⟩ =>
    cases hx
    cases hy
    exact Bool.noConfusion (List.cons.inj e).1

end Lambek

end Found

#print axioms Found.Lambek.exchange_residuals
#print axioms Found.Lambek.langLA
#print axioms Found.Lambek.not_commutative

found/LargeCardinals.lean

lean_all/found/LargeCardinals.lean

/-
  T188. found/LargeCardinals — large cardinals as a point, in the form
  available from the coordinates: the meta-language's universe hierarchy
  gives a tower of universes in Aczel's V (generalizing T104 to every level).
  * liftU : V_u → V_{u+1}, respecting ≈.
  * U_u := {liftU a | a ∈ V_u} ∈ V_{u+1}; every lifted set lies in U_u.
  * Tower: the lift of U_u lies in U_{u+1} (an inaccessible-like hierarchy:
    "every set lies in a universe", TG, at every level).
  Specific large-cardinal axioms (measurable, Woodin, …) are further
  conditions (cited).

  Lean 4 core only, hand-written proofs, no axioms.
  CIC dependence: universe hierarchy.
-/
import found.Mostowski

namespace Found

namespace LC

open WF

universe u

def liftU : V.{u} → V.{u + 1}
  | .sup I f => .sup (ULift.{u + 1} I) (fun i => liftU (f i.down))

theorem liftU_eq : (x y : V.{u}) → Eq' x y → Eq' (liftU x) (liftU y)
  | .sup _ _, .sup _ _, ⟨e1, e2⟩ =>
    ⟨fun i => match e1 i.down with | ⟨j, e⟩ => ⟨⟨j⟩, liftU_eq _ _ e⟩,
     fun j => match e2 j.down with | ⟨i, e⟩ => ⟨⟨i⟩, liftU_eq _ _ e⟩⟩

def U : V.{u + 1} := .sup V.{u} liftU

theorem in_U (a : V.{u}) : Mem (liftU a) U.{u} := ⟨a, eq_refl _⟩

/-- the tower: U_u (lifted) is a member of U_{u+1} -/
theorem tower : Mem (liftU U.{u}) U.{u + 1} := in_U U.{u}

theorem U_resp {a b : V.{u}} (h : Eq' a b) : Eq' (liftU a) (liftU b) := liftU_eq a b h

end LC

end Found

#print axioms Found.LC.liftU_eq
#print axioms Found.LC.in_U
#print axioms Found.LC.tower

found/LawDeon.lean

lean_all/found/LawDeon.lean

/-
  T313. found/LawDeon — rules read from original statute texts, built at the
  deontic point (T113: the relation coordinate R with the value "serial")
  and carried to the intuitionistic (T65), linear (T68), paraconsistent
  (T70) and many-valued (T71) points.

  Atoms are numbers only; the comment after each rule names the original
  text it was read from (Korean texts from the law.go.kr OPEN API, United
  States texts from the US Code XML, release point 119-142).
    r1 : 0 ∧ (1 ∨ 2) → box 3        (Korean Civil Act, Art. 750)
    r2 : 4 ∧ (0 ∧ (1 ∨ 2)) → box 3  (Korean State Compensation Act, Art. 2(1))
    r3 : 7 ∧ ¬6 → box 5             (9 U.S.C. sec. 2)
    r4 : box ¬8                     (4 U.S.C. sec. 8)
  * A three-world serial model makes every rule true at every world.
  * Detachment, "box implies dia" and "no box p ∧ box ¬p" on every serial frame;
    without seriality a world with no successor carries box p ∧ box ¬p.
  * Carried along: detachment in every Heyting algebra and every
    commutative residuated lattice; the exception clause "∧ ¬6" is not
    decided in the three-element Heyting algebra; one copy of a condition
    does not fire a rule twice in the three-element residuated chain;
    box 3 and box ¬3 together do not designate everything in the three-valued
    designated-glut table, while on two values they do; any two rule
    conditions are comparable in the three-element chain and need not be in
    the five-element algebra.

  CIC dependence: Prop-valued satisfaction (as T113).
  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.ModalExt
import found.ManyValued
import found.Paracons

namespace Found

namespace LawDeon

open Modal

/-! ## Rules -/

def neg (p : MForm) : MForm := .imp p .bot

def r1 : MForm := .imp (.and (.var 0) (.or (.var 1) (.var 2))) (.box (.var 3))
def r2 : MForm := .imp (.and (.var 4) (.and (.var 0) (.or (.var 1) (.var 2)))) (.box (.var 3))
def r3 : MForm := .imp (.and (.var 7) (neg (.var 6))) (.box (.var 5))
def r4 : MForm := .box (neg (.var 8))

/-! ## A serial model -/

inductive LW where
  | a : LW
  | b : LW
  | i : LW

/-- every world sees exactly the world `i` -/
def R0 : LW → LW → Prop
  | .a, .a => False
  | .a, .b => False
  | .a, .i => True
  | .b, .a => False
  | .b, .b => False
  | .b, .i => True
  | .i, .a => False
  | .i, .b => False
  | .i, .i => True

def mem (n : Nat) : List Nat → Bool
  | [] => false
  | m :: l => Nat.beq n m || mem n l

def atoms : LW → List Nat
  | .a => [0, 1, 4, 7]
  | .b => []
  | .i => [3, 5]

def V0 (w : LW) (n : Nat) : Prop := mem n (atoms w) = true

theorem serial0 : Serial R0 := fun w =>
  match w with
  | .a => ⟨.i, trivial⟩
  | .b => ⟨.i, trivial⟩
  | .i => ⟨.i, trivial⟩

theorem toI : ∀ {w v : LW}, R0 w v → v = .i
  | .a, .a, h => h.elim
  | .a, .b, h => h.elim
  | .a, .i, _ => rfl
  | .b, .a, h => h.elim
  | .b, .b, h => h.elim
  | .b, .i, _ => rfl
  | .i, .a, h => h.elim
  | .i, .b, h => h.elim
  | .i, .i, _ => rfl

theorem boxI {φ : MForm} (h : sat R0 V0 .i φ) (w : LW) : sat R0 V0 w (.box φ) :=
  fun _ hv => (toI hv) ▸ h

theorem obl3 (w : LW) : sat R0 V0 w (.box (.var 3)) := boxI (φ := .var 3) (show mem 3 [3, 5] = true from rfl) w
theorem obl5 (w : LW) : sat R0 V0 w (.box (.var 5)) := boxI (φ := .var 5) (show mem 5 [3, 5] = true from rfl) w
theorem obl_not8 (w : LW) : sat R0 V0 w r4 :=
  boxI (φ := neg (.var 8)) (show mem 8 [3, 5] = true → False from fun h => Bool.noConfusion h) w

/-- all four rules hold at every world of the model -/
theorem rules_hold (w : LW) :
    sat R0 V0 w r1 ∧ sat R0 V0 w r2 ∧ sat R0 V0 w r3 ∧ sat R0 V0 w r4 :=
  ⟨fun _ => obl3 w, fun _ => obl3 w, fun _ => obl5 w, obl_not8 w⟩

/-- at world `a` the conditions of r1, r2, r3 hold -/
theorem cond_a : sat R0 V0 .a (.and (.var 0) (.or (.var 1) (.var 2))) ∧
    sat R0 V0 .a (.and (.var 4) (.and (.var 0) (.or (.var 1) (.var 2)))) ∧
    sat R0 V0 .a (.and (.var 7) (neg (.var 6))) :=
  ⟨⟨rfl, Or.inl rfl⟩, ⟨rfl, rfl, Or.inl rfl⟩, ⟨rfl, fun h => Bool.noConfusion h⟩⟩

/-! ## The deontic point: general facts -/

section General
universe u
variable {W : Type u} (R : W → W → Prop) (V : W → Nat → Prop)

theorem detach {w : W} {c o : MForm} (hr : sat R V w (.imp c o)) (hc : sat R V w c) : sat R V w o := hr hc

theorem ought_may (hs : Serial R) {w : W} {p : MForm} (h : sat R V w (.box p)) : sat R V w (.dia p) :=
  match hs w with
  | ⟨v, r⟩ => ⟨v, r, h v r⟩

theorem no_conflict (hs : Serial R) {w : W} {p : MForm} :
    ¬ (sat R V w (.box p) ∧ sat R V w (.box (neg p))) :=
  fun h => match hs w with
    | ⟨v, r⟩ => h.2 v r (h.1 v r)

end General

/-- the derived obligations at `a`, and the permission that follows -/
theorem derived_a : sat R0 V0 .a (.box (.var 3)) ∧ sat R0 V0 .a (.box (.var 5)) ∧ sat R0 V0 .a (.dia (.var 3)) :=
  ⟨detach R0 V0 ((rules_hold .a).1) cond_a.1, detach R0 V0 ((rules_hold .a).2.2.1) cond_a.2.2,
   ought_may R0 V0 serial0 (detach R0 V0 ((rules_hold .a).2.1) cond_a.2.1)⟩

/-- without seriality (the K point): one world with no successor carries box p ∧ box ¬p -/
theorem conflict_without_serial (p : MForm) :
    sat (fun (_ : Unit) (_ : Unit) => False) (fun _ _ => False) () (.and (.box p) (.box (neg p))) :=
  ⟨fun _ h => h.elim, fun _ h => h.elim⟩

/-! ## Carried to other points -/

/-- intuitionistic point (T65): detachment in every Heyting algebra -/
theorem detach_HA (H : HA) (c o : H.car) : H.le (H.meet (H.imp c o) c) o := HA.mp H c o

/-- the exception clause is not decided in the three-element Heyting algebra:
    with atom 6 at 1/2, 6 ∨ ¬6 is not top -/
theorem exception_undecided :
    ¬ Prp.Three.HA3.le Prp.Three.HA3.top (Prp.Three.HA3.join Prp.Three.h (HA.neg Prp.Three.HA3 Prp.Three.h)) :=
  fun p => p

/-- linear point (T68): detachment in every commutative residuated lattice -/
theorem detach_CRL (M : Lin.CRL) (c o : M.car) : M.le (M.tens c (M.lolli c o)) o :=
  M.adj.mpr (M.le_refl _)

/-- one copy of a condition at 1/2 does not fire a rule twice in the three-element chain -/
theorem no_double_fire :
    ¬ Lin.L3.M3.le (Lin.L3.M3.tens Lin.L3.h (Lin.L3.M3.lolli Lin.L3.h Lin.L3.h)) (Lin.L3.M3.tens Lin.L3.h Lin.L3.h) :=
  fun e => Bool.noConfusion e

/-- paraconsistent point (T70): a content and its negation designated together
    do not designate an unrelated atom (atom 0 = the obligated content, atom 1 = any other) -/
theorem conflict_contained : ¬ Para.Ent [.var 0, .neg (.var 0)] (.var 1) false := Para.lp_no_explosion

/-- on two values the same conflict designates everything -/
theorem conflict_two_valued (q : Para.LF) : Para.Ent [.var 0, .neg (.var 0)] q true := Para.two_valued_explosion q

/-- many-valued point (T71): any two rule conditions are comparable in the three-element chain -/
theorem conditions_comparable : MV.Prelinear Prp.Three.HA3 := MV.three_prelinear

/-- and need not be in the five-element algebra -/
theorem conditions_incomparable : ¬ MV.Prelinear MV.F5.HA5 := MV.f5_not_prelinear

end LawDeon

end Found

#print axioms Found.LawDeon.serial0
#print axioms Found.LawDeon.toI
#print axioms Found.LawDeon.boxI
#print axioms Found.LawDeon.obl3
#print axioms Found.LawDeon.obl5
#print axioms Found.LawDeon.obl_not8
#print axioms Found.LawDeon.rules_hold
#print axioms Found.LawDeon.cond_a
#print axioms Found.LawDeon.detach
#print axioms Found.LawDeon.ought_may
#print axioms Found.LawDeon.no_conflict
#print axioms Found.LawDeon.derived_a
#print axioms Found.LawDeon.conflict_without_serial
#print axioms Found.LawDeon.detach_HA
#print axioms Found.LawDeon.exception_undecided
#print axioms Found.LawDeon.detach_CRL
#print axioms Found.LawDeon.no_double_fire
#print axioms Found.LawDeon.conflict_contained
#print axioms Found.LawDeon.conflict_two_valued
#print axioms Found.LawDeon.conditions_comparable
#print axioms Found.LawDeon.conditions_incomparable

found/Lin1.lean

lean_all/found/Lin1.lean

/-
  T190 (Lindström), part 1. found/Lin1 — Cantor's back-and-forth argument
  for relational structures: if two countable structures (surjections from
  ℕ) carry a back-and-forth system of finite partial isomorphisms (lists of
  pairs), then they are isomorphic. The stages alternate "forth" for the
  k-th element of A and "back" for the k-th element of B (choices by the
  classical point K); the isomorphism sends a to the partner of a in any
  stage.

  Lean 4 core only, hand-written proofs, no axioms (classical logic only as
  the parameter K).
-/
import found.ClassicalPoint

namespace Found

namespace Lind

section BF

variable {A B : Type} (RA : Nat → List A → Prop) (RB : Nat → List B → Prop)

def fstL : List (A × B) → List A
  | [] => []
  | x :: l => x.1 :: fstL l

def sndL : List (A × B) → List B
  | [] => []
  | x :: l => x.2 :: sndL l

def lmap {X Y : Type} (f : X → Y) : List X → List Y
  | [] => []
  | x :: l => f x :: lmap f l

/-- a finite partial isomorphism -/
def PIso (p : List (A × B)) : Prop :=
  (∀ r (l : List (A × B)), (∀ x, x ∈ l → x ∈ p) → (RA r (fstL l) ↔ RB r (sndL l))) ∧
  ∀ x y, x ∈ p → y ∈ p → (x.1 = y.1 ↔ x.2 = y.2)

structure BFS (I : List (A × B) → Prop) : Prop where
  nil : I []
  piso : ∀ p, I p → PIso RA RB p
  forth : ∀ p, I p → ∀ a, ∃ b, I ((a, b) :: p)
  back : ∀ p, I p → ∀ b, ∃ a, I ((a, b) :: p)

structure Iso where
  f : A → B
  g : B → A
  gf : ∀ a, g (f a) = a
  fg : ∀ b, f (g b) = b
  pres : ∀ r l, RA r l ↔ RB r (lmap f l)

end BF

section Cantor

variable {A B : Type} {RA : Nat → List A → Prop} {RB : Nat → List B → Prop}
variable (K : CP.Classical') {I : List (A × B) → Prop} (hI : BFS RA RB I) (eA : Nat → A) (eB : Nat → B)

/-- the stages -/
def stage : Nat → {p : List (A × B) // I p}
  | 0 => ⟨[], hI.nil⟩
  | k + 1 =>
    let p := stage k
    let b := K.ch (fun b => I ((eA k, b) :: p.1)) (hI.forth p.1 p.2 (eA k))
    have h1 : I ((eA k, b) :: p.1) := K.ch_spec (fun b => I ((eA k, b) :: p.1)) (hI.forth p.1 p.2 (eA k))
    let a := K.ch (fun a => I ((a, eB k) :: (eA k, b) :: p.1)) (hI.back _ h1 (eB k))
    ⟨(a, eB k) :: (eA k, b) :: p.1, K.ch_spec (fun a => I ((a, eB k) :: (eA k, b) :: p.1)) (hI.back _ h1 (eB k))⟩

theorem stage_succ_sub (k : Nat) (x : A × B) (h : x ∈ (stage K hI eA eB k).1) : x ∈ (stage K hI eA eB (k + 1)).1 :=
  .tail _ (.tail _ h)

theorem stage_mono (k : Nat) : (j : Nat) → ∀ x, x ∈ (stage K hI eA eB k).1 → x ∈ (stage K hI eA eB (k + j)).1
  | 0, _, h => h
  | j + 1, x, h => stage_succ_sub K hI eA eB (k + j) x (stage_mono k j x h)

theorem forth_in (k : Nat) : ∃ b, (eA k, b) ∈ (stage K hI eA eB (k + 1)).1 :=
  ⟨_, .tail _ (.head _)⟩

theorem back_in (k : Nat) : ∃ a, (a, eB k) ∈ (stage K hI eA eB (k + 1)).1 :=
  ⟨_, .head _⟩

/-- two stages lie in a common stage -/
theorem common (k1 k2 : Nat) : ∀ x y, x ∈ (stage K hI eA eB k1).1 → y ∈ (stage K hI eA eB k2).1 →
    x ∈ (stage K hI eA eB (k1 + k2)).1 ∧ y ∈ (stage K hI eA eB (k1 + k2)).1 := fun x y hx hy =>
  ⟨stage_mono K hI eA eB k1 k2 x hx, by
    have h := stage_mono K hI eA eB k2 k1 y hy
    rw [Nat.add_comm] at h
    exact h⟩


theorem exA (sA : ∀ a, ∃ k, eA k = a) (a : A) : ∃ b, ∃ k, (a, b) ∈ (stage K hI eA eB k).1 :=
  match sA a with
  | ⟨k, e⟩ => match forth_in K hI eA eB k with
    | ⟨b, h⟩ => ⟨b, k + 1, by rw [← e]; exact h⟩

theorem exB (sB : ∀ b, ∃ k, eB k = b) (b : B) : ∃ a, ∃ k, (a, b) ∈ (stage K hI eA eB k).1 :=
  match sB b with
  | ⟨k, e⟩ => match back_in K hI eA eB k with
    | ⟨a, h⟩ => ⟨a, k + 1, by rw [← e]; exact h⟩

def fM (sA : ∀ a, ∃ k, eA k = a) (a : A) : B := K.ch (fun b => ∃ k, (a, b) ∈ (stage K hI eA eB k).1) (exA K hI eA eB sA a)
def gM (sB : ∀ b, ∃ k, eB k = b) (b : B) : A := K.ch (fun a => ∃ k, (a, b) ∈ (stage K hI eA eB k).1) (exB K hI eA eB sB b)

theorem fM_spec (sA : ∀ a, ∃ k, eA k = a) (a : A) : ∃ k, (a, fM K hI eA eB sA a) ∈ (stage K hI eA eB k).1 :=
  K.ch_spec (fun b => ∃ k, (a, b) ∈ (stage K hI eA eB k).1) (exA K hI eA eB sA a)
theorem gM_spec (sB : ∀ b, ∃ k, eB k = b) (b : B) : ∃ k, (gM K hI eA eB sB b, b) ∈ (stage K hI eA eB k).1 :=
  K.ch_spec (fun a => ∃ k, (a, b) ∈ (stage K hI eA eB k).1) (exB K hI eA eB sB b)

/-- in a common stage, partners agree -/
theorem agree {x y : A × B} {k1 k2 : Nat} (hx : x ∈ (stage K hI eA eB k1).1) (hy : y ∈ (stage K hI eA eB k2).1) :
    x.1 = y.1 ↔ x.2 = y.2 :=
  match common K hI eA eB k1 k2 x y hx hy with
  | ⟨h1, h2⟩ => (hI.piso _ (stage K hI eA eB (k1 + k2)).2).2 x y h1 h2

/-- all pairs (a, f a) for a in a list lie in one stage -/
theorem list_stage (sA : ∀ a, ∃ k, eA k = a) : (l : List A) → ∃ N, ∀ a, a ∈ l → (a, fM K hI eA eB sA a) ∈ (stage K hI eA eB N).1
  | [] => ⟨0, fun _ m => nomatch m⟩
  | a :: l => match fM_spec K hI eA eB sA a, list_stage sA l with
    | ⟨k, hk⟩, ⟨N, hN⟩ => ⟨k + N, fun x m => by
        cases m with
        | head => exact stage_mono K hI eA eB k N _ hk
        | tail _ m' => exact (common K hI eA eB k N _ _ hk (hN x m')).2⟩

def pairs (f : A → B) : List A → List (A × B)
  | [] => []
  | a :: l => (a, f a) :: pairs f l

theorem fst_pairs (f : A → B) : (l : List A) → fstL (pairs f l) = l
  | [] => rfl
  | a :: l => congrArg (List.cons a) (fst_pairs f l)

theorem snd_pairs (f : A → B) : (l : List A) → sndL (pairs f l) = lmap f l
  | [] => rfl
  | a :: l => congrArg (List.cons (f a)) (snd_pairs f l)

theorem mem_pairs (f : A → B) : (l : List A) → ∀ x, x ∈ pairs f l → ∃ a, a ∈ l ∧ x = (a, f a)
  | [], _, m => nomatch m
  | a :: l, x, m => by
    cases m with
    | head => exact ⟨a, .head _, rfl⟩
    | tail _ m' => match mem_pairs f l x m' with
      | ⟨b, hb, e⟩ => exact ⟨b, .tail _ hb, e⟩

include K hI in
/-- Cantor: a back-and-forth system between countable structures gives an isomorphism -/
def cantor (sA : ∀ a, ∃ k, eA k = a) (sB : ∀ b, ∃ k, eB k = b) : Iso RA RB :=
  { f := fM K hI eA eB sA
    g := gM K hI eA eB sB
    gf := fun a => match fM_spec K hI eA eB sA a, gM_spec K hI eA eB sB (fM K hI eA eB sA a) with
      | ⟨_, h1⟩, ⟨_, h2⟩ => ((agree K hI eA eB h2 h1).2 rfl)
    fg := fun b => match gM_spec K hI eA eB sB b, fM_spec K hI eA eB sA (gM K hI eA eB sB b) with
      | ⟨_, h1⟩, ⟨_, h2⟩ => ((agree K hI eA eB h2 h1).1 rfl)
    pres := fun r l => match list_stage K hI eA eB sA l with
      | ⟨N, hN⟩ => by
        have h := (hI.piso _ (stage K hI eA eB N).2).1 r (pairs (fM K hI eA eB sA) l) (fun x m =>
          match mem_pairs _ l x m with
          | ⟨a, ha, e⟩ => by rw [e]; exact hN a ha)
        rw [fst_pairs, snd_pairs] at h
        exact h }

end Cantor

end Lind

end Found

#print axioms Found.Lind.stage_mono
#print axioms Found.Lind.common
#print axioms Found.Lind.list_stage
#print axioms Found.Lind.cantor

found/Lin10.lean

lean_all/found/Lin10.lean

/-
  T190 (Lindström), part 10. found/Lin10 — in a countable normal model of
  θ_τ with a nonempty set Dd of levels (symbol dS) closed under
  predecessors, the finite lists of pairs inside the graph of some code at
  a level of Dd form a back-and-forth system between the A-part and the
  B-part (with the copied τ-relations); by Cantor these parts are
  isomorphic.

  Lean 4 core only, hand-written proofs, no axioms (classical logic only as
  the parameter K).
-/
import found.Lin9

namespace Found

namespace Lind

open FO FOCT NBGc

def thetaD (dS : Nat) : Fm :=
  .conj (.ex (m1 dS 0)) (.all (.imp (m1 dS 0) (.conj (m1 3 0) (.ex (.conj (m1 dS 0) (m2 4 0 1))))))

theorem fv_thetaD (dS : Nat) : FV 0 (thetaD dS) := fvB_ok 0 _ rfl

section BF

variable {C : Type} (R : Nat → List C → Prop) (τ : List (Nat × Nat)) (dS : Nat) (ρ : Nat → C)

abbrev SA := {x : C // R 1 [x]}
abbrev SB := {y : C // R 2 [y]}

def RA2 (r : Nat) (l : List (SA R)) : Prop := (r, lenB l) ∈ τ ∧ R (cA r) (lmap Subtype.val l)
def RB2 (r : Nat) (l : List (SB R)) : Prop := (r, lenB l) ∈ τ ∧ R (cB r) (lmap Subtype.val l)

def Isys (L : List (SA R × SB R)) : Prop :=
  ∃ u p, R dS [u] ∧ R 6 [p, u] ∧ ∀ q, q ∈ L → R 7 [p, q.1.val, q.2.val]

theorem getT {x : Fm} (hθ : Sat R (theta τ) ρ) (hx : x ∈ [T1, T2, T3, T4, T6, T8, T9, NA, NB]) : Sat R x ρ :=
  (sat_conjL R _ ρ).1 hθ x (mem_appL_l _ _ hx)

theorem getP {r ar : Nat} (hθ : Sat R (theta τ) ρ) (h : (r, ar) ∈ τ) : Sat R (presF 7 (cA r) (cB r) ar) ρ :=
  (sat_conjL R _ ρ).1 hθ _ (mem_appL_r _ _ (lmap_mem (fun q => presF 7 (cA q.1) (cB q.1) q.2) τ h))

def vals (l : List (SA R × SB R)) : List (C × C) := lmap (fun q => (q.1.val, q.2.val)) l

theorem fst_vals : (l : List (SA R × SB R)) → fstL (vals R l) = lmap Subtype.val (fstL l)
  | [] => rfl
  | _ :: l => congrArg (List.cons _) (fst_vals l)

theorem snd_vals : (l : List (SA R × SB R)) → sndL (vals R l) = lmap Subtype.val (sndL l)
  | [] => rfl
  | _ :: l => congrArg (List.cons _) (snd_vals l)

theorem len_vals : (l : List (SA R × SB R)) → lenB (vals R l) = lenB l
  | [] => rfl
  | _ :: l => congrArg (· + 1) (len_vals l)

theorem mem_vals {l : List (SA R × SB R)} {q : C × C} (h : q ∈ vals R l) : ∃ q', q' ∈ l ∧ q = (q'.1.val, q'.2.val) :=
  mem_lmap l h

theorem bfs (K : CP.Classical') (hN : Normal R) (hθ : Sat R (theta τ) ρ) (hD : Sat R (thetaD dS) ρ) :
    BFS (RA2 R τ) (RB2 R τ) (Isys R dS) where
  nil := match hD.1 with
    | ⟨u, hu⟩ => match getT R τ ρ hθ (.head _) u (hD.2 u hu).1 with
      | ⟨p, _, hp⟩ => ⟨u, p, hu, hp, fun _ m => nomatch m⟩
  piso := fun L ⟨u, p, _, _, hL⟩ => by
    refine ⟨fun r l hl => ?_, fun x y hx hy => ?_⟩
    · refine (K.lem ((r, lenB (fstL l)) ∈ τ)).elim (fun hin => ?_) (fun hn => ?_)
      · have hin' : (r, lenB l) ∈ τ := by rw [len_fst] at hin; exact hin
        have pr := (sat_presL R 7 (cA r) (cB r) (lenB l) ρ).1 (getP R τ ρ hθ hin') p (vals R l) (len_vals R l)
          (fun q hq => match mem_vals R hq with
            | ⟨q', hq', e⟩ => by rw [e]; exact hL q' (hl q' hq'))
        rw [fst_vals, snd_vals] at pr
        have hinB : (r, lenB (sndL l)) ∈ τ := by rw [len_snd]; exact hin'
        exact ⟨fun h => ⟨hinB, pr.1 h.2⟩, fun h => ⟨hin, pr.2 h.2⟩⟩
      · have hnB : (r, lenB (sndL l)) ∈ τ → False := fun h => hn (by rw [len_fst]; rw [len_snd] at h; exact h)
        exact ⟨fun h => (hn h.1).elim, fun h => (hnB h.1).elim⟩
    · have t4 := getT R τ ρ hθ (.tail _ (.tail _ (.tail _ (.head _)))) p x.1.val x.2.val y.1.val y.2.val
        ⟨hL x hx, hL y hy⟩
      have e : x.1.val = y.1.val ↔ x.2.val = y.2.val :=
        ⟨fun h => (hN _ _).1 (t4.1 ((hN _ _).2 h)), fun h => (hN _ _).1 (t4.2 ((hN _ _).2 h))⟩
      exact ⟨fun h => Subtype.ext (e.1 (congrArg Subtype.val h)), fun h => Subtype.ext (e.2 (congrArg Subtype.val h))⟩
  forth := fun L ⟨u, p, hu, hp, hL⟩ a => match (hD.2 u hu).2 with
    | ⟨u', hu', hpre⟩ => match getT R τ ρ hθ (.tail _ (.head _)) p u u' ⟨hp, hpre⟩ a.val a.property with
      | ⟨q, hq, hext, y, hy, hg⟩ => ⟨⟨y, hy⟩, u', q, hu', hq, fun z hz => by
          cases hz with
          | head => exact hg
          | tail _ hz' => exact hext _ _ (hL z hz')⟩
  back := fun L ⟨u, p, hu, hp, hL⟩ b => match (hD.2 u hu).2 with
    | ⟨u', hu', hpre⟩ => match getT R τ ρ hθ (.tail _ (.tail _ (.head _))) p u u' ⟨hp, hpre⟩ b.val b.property with
      | ⟨q, hq, hext, x, hx, hg⟩ => ⟨⟨x, hx⟩, u', q, hu', hq, fun z hz => by
          cases hz with
          | head => exact hg
          | tail _ hz' => exact hext _ _ (hL z hz')⟩

/-- enumerating a subtype of a countable type -/
theorem cnt_sub (K : CP.Classical') {P : C → Prop} (hc : Cnt C) (a0 : {x // P x}) : Cnt {x // P x} :=
  match hc with
  | ⟨e, he⟩ =>
    have ex : ∀ k, ∃ a : {x // P x}, a.val = e k ∨ ((P (e k) → False) ∧ a = a0) := fun k =>
      (K.lem (P (e k))).elim (fun h => ⟨⟨e k, h⟩, .inl rfl⟩) (fun h => ⟨a0, .inr ⟨h, rfl⟩⟩)
    ⟨fun k => K.ch _ (ex k), fun a => match he a.val with
      | ⟨k, hk⟩ => ⟨k, (K.ch_spec _ (ex k)).elim (fun h => Subtype.ext (h.trans hk))
          (fun ⟨h, _⟩ => (h (by rw [hk]; exact a.property)).elim)⟩⟩

/-- the two parts are isomorphic -/
def partsIso (K : CP.Classical') (hN : Normal R) (hθ : Sat R (theta τ) ρ) (hD : Sat R (thetaD dS) ρ)
    (hc : Cnt C) (a0 : SA R) (b0 : SB R) : Iso (RA2 R τ) (RB2 R τ) :=
  let hA := cnt_sub K hc a0
  let hB := cnt_sub K hc b0
  cantor K (bfs R τ dS ρ K hN hθ hD) (K.ch (fun e : Nat → SA R => ∀ a, ∃ k, e k = a) hA)
    (K.ch (fun e : Nat → SB R => ∀ b, ∃ k, e k = b) hB)
    (K.ch_spec (fun e : Nat → SA R => ∀ a, ∃ k, e k = a) hA) (K.ch_spec (fun e : Nat → SB R => ∀ b, ∃ k, e k = b) hB)

end BF

end Lind

end Found

#print axioms Found.Lind.fv_thetaD
#print axioms Found.Lind.bfs
#print axioms Found.Lind.cnt_sub
#print axioms Found.Lind.partsIso

found/Lin11.lean

lean_all/found/Lin11.lean

/-
  T190 (Lindström), part 11. found/Lin11 — Lindström's theorem: every
  sentence of an abstract logic extending first-order logic (closed under
  ¬, ∧, relativization and renaming, invariant under isomorphism, with
  compactness and the downward Löwenheim–Skolem property) is equivalent on
  normal structures to a first-order sentence.
  If not, for every n there are n-equivalent A ⊨ s and B ⊭ s; the combined
  structures C_n satisfy "s on the A-part, ¬s on the B-part, θ_τ, Ch k
  (k ≤ n)"; by compactness some model has a constant with predecessors of
  every length; the levels below it form a predecessor-closed set, which is
  added as a fresh symbol; by Löwenheim–Skolem there is a countable model,
  where the codes give a back-and-forth system, so the two parts are
  isomorphic — but s holds on one and fails on the other.

  Lean 4 core only, hand-written proofs, no axioms (classical logic only as
  the parameter K).
-/
import found.Lin10

namespace Found

namespace Lind

open FO FOCT NBGc

section M

variable (K : CP.Classical') (L : ALogic)

def foS (φ : Fm) : L.Sen :=
  K.ch (fun t => (∀ p, p ∈ L.voc t → p ∈ symsF φ) ∧ ∀ {A : Type} (R : Nat → List A → Prop) (v : Nat → A),
    FV 0 φ → (L.sat R t ↔ Sat R φ v)) (L.fo φ)

theorem foS_spec (φ : Fm) {A : Type} (R : Nat → List A → Prop) (v : Nat → A) (h : FV 0 φ) :
    L.sat R (foS K L φ) ↔ Sat R φ v :=
  (K.ch_spec (fun t => (∀ p, p ∈ L.voc t → p ∈ symsF φ) ∧ ∀ {A : Type} (R : Nat → List A → Prop) (v : Nat → A),
    FV 0 φ → (L.sat R t ↔ Sat R φ v)) (L.fo φ)).2 R v h

def negS (s : L.Sen) : L.Sen :=
  K.ch (fun t => (∀ p, p ∈ L.voc t → p ∈ L.voc s) ∧ ∀ {A : Type} (R : Nat → List A → Prop), L.sat R t ↔ ¬ L.sat R s) (L.neg s)

theorem negS_spec (s : L.Sen) {A : Type} (R : Nat → List A → Prop) : L.sat R (negS K L s) ↔ ¬ L.sat R s :=
  (K.ch_spec (fun t => (∀ p, p ∈ L.voc t → p ∈ L.voc s) ∧ ∀ {A : Type} (R : Nat → List A → Prop),
    L.sat R t ↔ ¬ L.sat R s) (L.neg s)).2 R

def conjS (s t : L.Sen) : L.Sen :=
  K.ch (fun u => (∀ p, p ∈ L.voc u → p ∈ L.voc s ∨ p ∈ L.voc t) ∧ ∀ {A : Type} (R : Nat → List A → Prop),
    L.sat R u ↔ L.sat R s ∧ L.sat R t) (L.conj s t)

theorem conjS_spec (s t : L.Sen) {A : Type} (R : Nat → List A → Prop) : L.sat R (conjS K L s t) ↔ L.sat R s ∧ L.sat R t :=
  (K.ch_spec (fun u => (∀ p, p ∈ L.voc u → p ∈ L.voc s ∨ p ∈ L.voc t) ∧ ∀ {A : Type} (R : Nat → List A → Prop),
    L.sat R u ↔ L.sat R s ∧ L.sat R t) (L.conj s t)).2 R

def relS (s : L.Sen) (P : Nat) : L.Sen :=
  K.ch (fun t => (∀ p, p ∈ L.voc t → p ∈ L.voc s ∨ p = (P, 1)) ∧ ∀ {A : Type} (R : Nat → List A → Prop),
    (∃ a, R P [a]) → (L.sat R t ↔ L.sat (Rsub R P) s)) (L.rel s P)

theorem relS_spec (s : L.Sen) (P : Nat) {A : Type} (R : Nat → List A → Prop) (h : ∃ a, R P [a]) :
    L.sat R (relS K L s P) ↔ L.sat (Rsub R P) s :=
  (K.ch_spec (fun t => (∀ p, p ∈ L.voc t → p ∈ L.voc s ∨ p = (P, 1)) ∧ ∀ {A : Type} (R : Nat → List A → Prop),
    (∃ a, R P [a]) → (L.sat R t ↔ L.sat (Rsub R P) s)) (L.rel s P)).2 R h

def renS (s : L.Sen) (σ : Nat → Nat) : L.Sen :=
  K.ch (fun t => (∀ p, p ∈ L.voc t → ∃ q, q ∈ L.voc s ∧ p = (σ q.1, q.2)) ∧ ∀ {A : Type} (R : Nat → List A → Prop),
    L.sat R t ↔ L.sat (fun r l => R (σ r) l) s) (L.ren s σ)

theorem renS_spec (s : L.Sen) (σ : Nat → Nat) {A : Type} (R : Nat → List A → Prop) :
    L.sat R (renS K L s σ) ↔ L.sat (fun r l => R (σ r) l) s :=
  (K.ch_spec (fun t => (∀ p, p ∈ L.voc t → ∃ q, q ∈ L.voc s ∧ p = (σ q.1, q.2)) ∧ ∀ {A : Type} (R : Nat → List A → Prop),
    L.sat R t ↔ L.sat (fun r l => R (σ r) l) s) (L.ren s σ)).2 R

variable (s : L.Sen)

def tauS : List (Nat × Nat) := (0, 2) :: L.voc s
def sAS : L.Sen := relS K L (renS K L s cA) 1
def sBS : L.Sen := relS K L (renS K L (negS K L s) cB) 2
def psiS : L.Sen := conjS K L (conjS K L (sAS K L s) (sBS K L s)) (foS K L (theta (tauS L s)))
def ChS (k : Nat) : L.Sen := foS K L (Ch k)

def sumF : List (Nat × Nat) → Nat
  | [] => 0
  | p :: l => p.1 + sumF l

theorem lt_sumF : (l : List (Nat × Nat)) → ∀ p, p ∈ l → p.1 < sumF l + 13
  | [], _, h => nomatch h
  | q :: l, p, h => by
    cases h with
    | head => exact Nat.lt_of_lt_of_le (Nat.lt_add_of_pos_right (Nat.zero_lt_succ 12)) (Nat.add_le_add_right (Nat.le_add_right _ _) 13)
    | tail _ h' => exact Nat.lt_of_lt_of_le (lt_sumF l p h') (Nat.add_le_add_right (Nat.le_add_left _ _) 13)

theorem ne_big (x k : Nat) (hk : k < 13) : k = x + 13 → False := fun e =>
  Nat.lt_irrefl k (Nat.lt_of_lt_of_le hk (by rw [e]; exact Nat.le_add_left 13 x))

/-- the combined structure satisfies ψ and the chain sentences up to n -/
theorem cn_sat {A B : Type} (RA : Nat → List A → Prop) (RB : Nat → List B → Prop) (n : Nat) (v : Nat → A) (w : Nat → B)
    (hA : Normal RA) (hB : Normal RB) (hE : EQ RA RB (tauS L s) n 0 v w) (hs : L.sat RA s) (hns : L.sat RB s → False) :
    L.sat (Rc RA RB (tauS L s) n v w) (psiS K L s) ∧
      ∀ k, k ≤ n → L.sat (Rc RA RB (tauS L s) n v w) (ChS K L k) := by
  let R := Rc RA RB (tauS L s) n v w
  let ρ : Nat → CU A B := fun _ => CU.lv 0
  refine ⟨(conjS_spec K L _ _ R).2 ⟨(conjS_spec K L _ _ R).2 ⟨?_, ?_⟩, ?_⟩, fun k hk => ?_⟩
  · refine (relS_spec K L _ 1 R ⟨CU.a (v 0), v 0, rfl⟩).2 ((renS_spec K L s cA _).2 ?_)
    exact L.iso s RA _ (isoA RA RB (tauS L s) n v w) hs
  · refine (relS_spec K L _ 2 R ⟨CU.b (w 0), w 0, rfl⟩).2 ((renS_spec K L _ cB _).2 ((negS_spec K L s _).2 ?_))
    exact fun h => hns (L.iso s _ RB (isoB RA RB (tauS L s) n v w).symm' h)
  · exact (foS_spec K L _ R ρ (fv_theta _)).2 (sTheta RA RB (tauS L s) n v w hA hB (.head _) hE ρ)
  · exact (foS_spec K L _ R ρ (fv_Ch k)).2 (sCh RA RB (tauS L s) n v w k hk ρ)

/-- bounding the chain sentences in a finite list -/
theorem bound (T : L.Sen → Prop) (hT : ∀ t, T t ↔ t = psiS K L s ∨ ∃ k, t = ChS K L k) :
    (Ls : List L.Sen) → (∀ t, t ∈ Ls → T t) → ∃ N, ∀ t, t ∈ Ls → t = psiS K L s ∨ ∃ k, k ≤ N ∧ t = ChS K L k
  | [], _ => ⟨0, fun _ m => nomatch m⟩
  | t :: Ls, h => match bound T hT Ls (fun x m => h x (.tail _ m)) with
    | ⟨N, hN⟩ => ((hT t).1 (h t (.head _))).elim
        (fun e => ⟨N, fun x m => by
          cases m with
          | head => exact .inl e
          | tail _ m' => exact hN x m'⟩)
        (fun ⟨k, e⟩ => ⟨N + k, fun x m => by
          cases m with
          | head => exact .inr ⟨k, Nat.le_add_left k N, e⟩
          | tail _ m' => exact (hN x m').elim .inl (fun ⟨j, hj, ej⟩ => .inr ⟨j, Nat.le_trans hj (Nat.le_add_right N k), ej⟩)⟩)

include K in
/-- no n-equivalence bound gives a contradiction -/
theorem no_bound (hcex : ∀ n, ∃ (A B : Type) (RA : Nat → List A → Prop) (RB : Nat → List B → Prop) (v : Nat → A)
    (w : Nat → B), Normal RA ∧ Normal RB ∧ EQ RA RB (tauS L s) n 0 v w ∧ L.sat RA s ∧ (L.sat RB s → False)) : False := by
  let τ := tauS L s
  let ψ := psiS K L s
  let T : L.Sen → Prop := fun t => t = ψ ∨ ∃ k, t = ChS K L k
  -- compactness
  have fin : ∀ Ls : List L.Sen, (∀ t, t ∈ Ls → T t) →
      ∃ (A : Type) (R : Nat → List A → Prop), Normal R ∧ Nonempty A ∧ ∀ t, t ∈ Ls → L.sat R t := fun Ls hLs =>
    match bound K L s T (fun _ => Iff.rfl) Ls hLs with
    | ⟨N, hN⟩ => match hcex N with
      | ⟨A, B, RA, RB, v, w, hA, hB, hE, hs, hns⟩ =>
        have c := cn_sat K L s RA RB N v w hA hB hE hs hns
        ⟨CU A B, Rc RA RB τ N v w, rc_normal RA RB τ N v w, ⟨CU.lv 0⟩, fun t ht => (hN t ht).elim
          (fun e => by rw [e]; exact c.1) (fun ⟨k, hk, e⟩ => by rw [e]; exact c.2 k hk)⟩
  match L.compact T fin with
  | ⟨C0, R0, hN0, ⟨c'⟩, hall⟩ =>
  let ρ0 : Nat → C0 := fun _ => c'
  have hψ0 := hall ψ (.inl rfl)
  have hθ0 : Sat R0 (theta τ) ρ0 :=
    (foS_spec K L _ R0 ρ0 (fv_theta τ)).1 ((conjS_spec K L _ _ R0).1 hψ0).2
  have hCh : ∀ k, Sat R0 (Ch k) ρ0 := fun k => (foS_spec K L _ R0 ρ0 (fv_Ch k)).1 (hall _ (.inr ⟨k, rfl⟩))
  have t8 := getT R0 τ ρ0 hθ0 (.tail _ (.tail _ (.tail _ (.tail _ (.tail _ (.head _))))))
  have t9 := getT R0 τ ρ0 hθ0 (.tail _ (.tail _ (.tail _ (.tail _ (.tail _ (.tail _ (.head _)))))))
  have agr : ∀ k (ρ ρ' : Nat → C0) (u : C0), Sat R0 (chainF k) (ext ρ u) → Sat R0 (chainF k) (ext ρ' u) :=
    fun k ρ ρ' u h => (sat_agree R0 (chainF k) 1 (fv_chain k) (v := ext ρ u) (w := ext ρ' u) (fun n hn => match n, hn with
      | 0, _ => rfl
      | _ + 1, hn => (Nat.not_succ_le_zero _ (Nat.le_of_succ_le_succ hn)).elim)).1 h
  -- the levels with predecessors of every length
  let Dp : C0 → Prop := fun u => R0 3 [u] ∧ ∀ k, Sat R0 (chainF k) (ext ρ0 u)
  have c0D : ∃ c, Dp c := match hCh 0 with
    | ⟨c, hc8, hc3, _⟩ => ⟨c, hc3, fun k => match hCh k with
      | ⟨ck, hk8, _, hk⟩ =>
        have e : ck = c := (hN0 ck c).1 (t9 ck c ⟨hk8, hc8⟩)
        by rw [← e]; exact agr k _ _ ck hk⟩
  have predD : ∀ u, Dp u → ∃ u1, Dp u1 ∧ R0 4 [u1, u] := fun u ⟨_, hu⟩ =>
    match hu 1 with
    | ⟨u1, h41, h31, _⟩ => ⟨u1, ⟨h31, fun k => match hu (k + 1) with
        | ⟨u2, h42, _, hk⟩ =>
          have e : u2 = u1 := (hN0 u2 u1).1 (t8 u u2 u1 ⟨h42, h41⟩)
          by rw [← e]; exact agr k _ _ u2 hk⟩, h41⟩
  -- a fresh symbol for these levels
  let dS := sumF (L.voc ψ) + 13
  let R1 : Nat → List C0 → Prop := fun r l => (r = dS ∧ ∃ u, l = [u] ∧ Dp u) ∨ ((r = dS → False) ∧ R0 r l)
  have n0 : (0 = dS → False) := ne_big _ 0 (by exact Nat.zero_lt_succ 12)
  have n3 : (3 = dS → False) := ne_big _ 3 (Nat.lt_succ_of_le (Nat.le_of_ble_eq_true rfl))
  have n4 : (4 = dS → False) := ne_big _ 4 (Nat.lt_succ_of_le (Nat.le_of_ble_eq_true rfl))
  have hN1 : Normal R1 := fun x y => ⟨fun h => h.elim (fun ⟨e, _⟩ => (n0 e).elim) (fun ⟨_, h⟩ => (hN0 x y).1 h),
    fun e => .inr ⟨n0, (hN0 x y).2 e⟩⟩
  have agree1 : Agree (L.voc ψ) R0 R1 := fun r l hr =>
    have ne : r = dS → False := fun e => Nat.lt_irrefl r (Nat.lt_of_lt_of_le (lt_sumF _ _ hr) (by rw [e]; exact Nat.le_refl _))
    ⟨fun h => .inr ⟨ne, h⟩, fun h => h.elim (fun ⟨e, _⟩ => (ne e).elim) (fun ⟨_, h⟩ => h)⟩
  have hψ1 : L.sat R1 ψ := (L.dep ψ R0 R1 agree1).1 hψ0
  have hD1 : Sat R1 (thetaD dS) ρ0 := by
    match c0D with
    | ⟨c, hc⟩ =>
    refine ⟨⟨c, .inl ⟨rfl, c, rfl, hc⟩⟩, fun u hu => ?_⟩
    have hDu : Dp u := hu.elim (fun ⟨_, u', e, h⟩ => by
        have e' : u = u' := inj1 e
        rw [e']; exact h) (fun ⟨ne, _⟩ => (ne rfl).elim)
    match predD u hDu with
    | ⟨u1, hD1, h4⟩ => exact ⟨.inr ⟨n3, hDu.1⟩, u1, .inl ⟨rfl, u1, rfl, hD1⟩, .inr ⟨n4, h4⟩⟩
  let ψ' := conjS K L ψ (foS K L (thetaD dS))
  have hψ'1 : L.sat R1 ψ' := (conjS_spec K L _ _ R1).2 ⟨hψ1, (foS_spec K L _ R1 ρ0 (fv_thetaD dS)).2 hD1⟩
  -- Löwenheim–Skolem
  match L.ls ψ' R1 hN1 ⟨c'⟩ hψ'1 with
  | ⟨C2, R2, hN2, ⟨c2⟩, hc2, hψ'2⟩ =>
  let ρ2 : Nat → C2 := fun _ => c2
  have hs2 := (conjS_spec K L _ _ R2).1 hψ'2
  have hD2 : Sat R2 (thetaD dS) ρ2 := (foS_spec K L _ R2 ρ2 (fv_thetaD dS)).1 hs2.2
  have hψ2 := (conjS_spec K L _ _ R2).1 hs2.1
  have hAB := (conjS_spec K L _ _ R2).1 hψ2.1
  have hθ2 : Sat R2 (theta τ) ρ2 := (foS_spec K L _ R2 ρ2 (fv_theta τ)).1 hψ2.2
  have na := getT R2 τ ρ2 hθ2 (.tail _ (.tail _ (.tail _ (.tail _ (.tail _ (.tail _ (.tail _ (.head _))))))))
  have nb := getT R2 τ ρ2 hθ2 (.tail _ (.tail _ (.tail _ (.tail _ (.tail _ (.tail _ (.tail _ (.tail _ (.head _)))))))))
  match na, nb with
  | ⟨a0, ha0⟩, ⟨b0, hb0⟩ =>
  have sA2 : L.sat (fun r l => Rsub R2 1 (cA r) l) s :=
    (renS_spec K L s cA _).1 ((relS_spec K L _ 1 R2 ⟨a0, ha0⟩).1 hAB.1)
  have sB2 : L.sat (fun r l => Rsub R2 2 (cB r) l) s → False :=
    (negS_spec K L s _).1 ((renS_spec K L _ cB _).1 ((relS_spec K L _ 2 R2 ⟨b0, hb0⟩).1 hAB.2))
  have iso := partsIso R2 τ dS ρ2 K hN2 hθ2 hD2 hc2 ⟨a0, ha0⟩ ⟨b0, hb0⟩
  have agA : Agree (L.voc s) (fun r l => Rsub R2 1 (cA r) l) (RA2 R2 τ) := fun _ _ hr =>
    ⟨fun h => ⟨.tail _ hr, h⟩, fun h => h.2⟩
  have agB : Agree (L.voc s) (RB2 R2 τ) (fun r l => Rsub R2 2 (cB r) l) := fun _ _ _ =>
    ⟨fun h => h.2, fun h => ⟨.tail _ (by assumption), h⟩⟩
  exact sB2 ((L.dep s _ _ agB).1 (L.iso s _ _ iso ((L.dep s _ _ agA).1 sA2)))

include K in
/-- Lindström's theorem -/
theorem lindstrom : ∃ φ : Fm, ∀ (A : Type) (RA : Nat → List A → Prop) (v : Nat → A), Normal RA →
    (L.sat RA s ↔ Sat RA φ v) := by
  refine (K.lem (∃ n, ∀ (A B : Type) (RA : Nat → List A → Prop) (RB : Nat → List B → Prop) (v : Nat → A) (w : Nat → B),
      Normal RA → Normal RB → EQ RA RB (tauS L s) n 0 v w → L.sat RA s → L.sat RB s)).elim (fun ⟨n, hn⟩ => ?_)
    (fun hno => (no_bound K L s (fun n => ?_)).elim)
  · match closed_defN K (tauS L s) n (fun A RA => L.sat RA s) hn with
    | ⟨φs, _, hφ⟩ => exact ⟨disjL φs, hφ⟩
  · -- push the negation inside
    refine (K.lem _).elim (fun h => h) (fun hne => (hno ⟨n, fun A B RA RB v w hA hB hE hs => ?_⟩).elim)
    exact (K.lem (L.sat RB s)).elim (fun h => h) (fun h => (hne ⟨A, B, RA, RB, v, w, hA, hB, hE, hs, h⟩).elim)

end M

end Lind

end Found

#print axioms Found.Lind.foS_spec
#print axioms Found.Lind.relS_spec
#print axioms Found.Lind.lt_sumF
#print axioms Found.Lind.cn_sat
#print axioms Found.Lind.bound
#print axioms Found.Lind.no_bound
#print axioms Found.Lind.lindstrom

found/Lin2.lean

lean_all/found/Lin2.lean

/-
  T190 (Lindström), part 2. found/Lin2 — Ehrenfeucht–Fraïssé equivalence
  for a finite relational vocabulary τ (symbols with arities): valuations
  v (in A) and w (in B) are 0-equivalent on k variables when they satisfy
  the same τ-atoms on the first k variables, and (n+1)-equivalent when
  moreover every extension on one side is matched by an extension on the
  other side up to n-equivalence on k + 1 variables. n-equivalence
  preserves every formula of quantifier rank ≤ n in the τ-atoms on the
  first k variables.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Lin1
import found.FOClassicalT

namespace Found

namespace Lind

open FO FOCT

section EF

variable {A B : Type} (RA : Nat → List A → Prop) (RB : Nat → List B → Prop) (τ : List (Nat × Nat))

def lenN : List Nat → Nat
  | [] => 0
  | _ :: l => lenN l + 1

/-- a τ-atom on the first k variables -/
def AtOK (k r : Nat) (l : List Nat) : Prop := (r, lenN l) ∈ τ ∧ ∀ i, i ∈ l → i < k

def Atm (k : Nat) (v : Nat → A) (w : Nat → B) : Prop :=
  ∀ r l, AtOK τ k r l → (RA r (mapD v l) ↔ RB r (mapD w l))

def EQ : Nat → Nat → (Nat → A) → (Nat → B) → Prop
  | 0, k, v, w => Atm RA RB τ k v w
  | n + 1, k, v, w => Atm RA RB τ k v w ∧ (∀ a, ∃ b, EQ n (k + 1) (ext v a) (ext w b)) ∧
      (∀ b, ∃ a, EQ n (k + 1) (ext v a) (ext w b))

theorem eq_atm : (n k : Nat) → {v : Nat → A} → {w : Nat → B} → EQ RA RB τ n k v w → Atm RA RB τ k v w
  | 0, _, _, _, h => h
  | _ + 1, _, _, _, h => h.1

/-- formulas of quantifier rank ≤ n in τ-atoms on the first k variables -/
def Frag : Fm → Nat → Nat → Prop
  | .atom r l, _, k => AtOK τ k r l
  | .bot, _, _ => True
  | .conj a b, n, k => Frag a n k ∧ Frag b n k
  | .disj a b, n, k => Frag a n k ∧ Frag b n k
  | .imp a b, n, k => Frag a n k ∧ Frag b n k
  | .all a, n, k => match n with
    | 0 => False
    | n + 1 => Frag a n (k + 1)
  | .ex a, n, k => match n with
    | 0 => False
    | n + 1 => Frag a n (k + 1)

/-- n-equivalence preserves the fragment -/
theorem eq_pres : (φ : Fm) → (n k : Nat) → {v : Nat → A} → {w : Nat → B} → EQ RA RB τ n k v w →
    Frag τ φ n k → (Sat RA φ v ↔ Sat RB φ w)
  | .atom r l, n, k, _, _, h, f => eq_atm RA RB τ n k h r l f
  | .bot, _, _, _, _, _, _ => Iff.rfl
  | .conj a b, n, k, _, _, h, f => ⟨fun ⟨x, y⟩ => ⟨(eq_pres a n k h f.1).1 x, (eq_pres b n k h f.2).1 y⟩,
      fun ⟨x, y⟩ => ⟨(eq_pres a n k h f.1).2 x, (eq_pres b n k h f.2).2 y⟩⟩
  | .disj a b, n, k, _, _, h, f => ⟨fun o => o.elim (fun x => .inl ((eq_pres a n k h f.1).1 x)) (fun y => .inr ((eq_pres b n k h f.2).1 y)),
      fun o => o.elim (fun x => .inl ((eq_pres a n k h f.1).2 x)) (fun y => .inr ((eq_pres b n k h f.2).2 y))⟩
  | .imp a b, n, k, _, _, h, f => ⟨fun g x => (eq_pres b n k h f.2).1 (g ((eq_pres a n k h f.1).2 x)),
      fun g x => (eq_pres b n k h f.2).2 (g ((eq_pres a n k h f.1).1 x))⟩
  | .all _, 0, _, _, _, _, f => f.elim
  | .all a, n + 1, k, _, _, h, f => ⟨fun g e => match h.2.2 e with
        | ⟨d, hd⟩ => (eq_pres a n (k + 1) hd f).1 (g d),
      fun g d => match h.2.1 d with
        | ⟨e, he⟩ => (eq_pres a n (k + 1) he f).2 (g e)⟩
  | .ex _, 0, _, _, _, _, f => f.elim
  | .ex a, n + 1, k, _, _, h, f => ⟨fun ⟨d, x⟩ => match h.2.1 d with
        | ⟨e, he⟩ => ⟨e, (eq_pres a n (k + 1) he f).1 x⟩,
      fun ⟨e, x⟩ => match h.2.2 e with
        | ⟨d, hd⟩ => ⟨d, (eq_pres a n (k + 1) hd f).2 x⟩⟩

end EF

end Lind

end Found

#print axioms Found.Lind.eq_atm
#print axioms Found.Lind.eq_pres

found/Lin3.lean

lean_all/found/Lin3.lean

/-
  T190 (Lindström), part 3. found/Lin3 — Hintikka formulas for a finite
  relational vocabulary τ: a finite list H n k of formulas of quantifier
  rank ≤ n on k variables such that every valuation satisfies one of them,
  and two valuations satisfying the same one are n-equivalent. Hence a
  class of structures closed under n-equivalence (on sentences) is defined
  by the disjunction of the Hintikka sentences it realizes.

  Lean 4 core only, hand-written proofs, no axioms (classical logic only as
  the parameter K, for choosing the realized formulas).
-/
import found.Lin2

namespace Found

namespace Lind

open FO FOCT

/-! ## finite lists -/

def bindL {X Y : Type} (f : X → List Y) : List X → List Y
  | [] => []
  | x :: l => f x ++ bindL f l

theorem mem_appL {X : Type} {y : X} : (l k : List X) → y ∈ l ++ k → y ∈ l ∨ y ∈ k
  | [], _, h => .inr h
  | _ :: l, k, h => by
    cases h with
    | head => exact .inl (.head _)
    | tail _ m => exact (mem_appL l k m).elim (fun m' => .inl (.tail _ m')) .inr

theorem mem_appL_l {X : Type} {y : X} : (l k : List X) → y ∈ l → y ∈ l ++ k
  | _ :: _, _, .head _ => .head _
  | _ :: l, k, .tail _ m => .tail _ (mem_appL_l l k m)

theorem mem_appL_r {X : Type} {y : X} : (l k : List X) → y ∈ k → y ∈ l ++ k
  | [], _, h => h
  | _ :: l, k, h => .tail _ (mem_appL_r l k h)

theorem mem_bind {X Y : Type} {f : X → List Y} {y : Y} : (l : List X) → y ∈ bindL f l → ∃ x, x ∈ l ∧ y ∈ f x
  | [], h => nomatch h
  | x :: l, h => (mem_appL _ _ h).elim (fun m => ⟨x, .head _, m⟩)
      (fun m => match mem_bind l m with | ⟨x', hx, hy⟩ => ⟨x', .tail _ hx, hy⟩)

theorem bind_mem {X Y : Type} {f : X → List Y} {y : Y} {x : X} : (l : List X) → x ∈ l → y ∈ f x → y ∈ bindL f l
  | _ :: _, .head _, h => mem_appL_l _ _ h
  | _ :: l, .tail _ m, h => mem_appL_r _ _ (bind_mem l m h)

theorem mem_lmap {X Y : Type} {f : X → Y} {y : Y} : (l : List X) → y ∈ lmap f l → ∃ x, x ∈ l ∧ y = f x
  | [], h => nomatch h
  | x :: l, h => by
    cases h with
    | head => exact ⟨x, .head _, rfl⟩
    | tail _ m => match mem_lmap l m with | ⟨x', hx, e⟩ => exact ⟨x', .tail _ hx, e⟩

theorem lmap_mem {X Y : Type} (f : X → Y) {x : X} : (l : List X) → x ∈ l → f x ∈ lmap f l
  | _ :: _, .head _ => .head _
  | _ :: l, .tail _ m => .tail _ (lmap_mem f l m)

def rangeL : Nat → List Nat
  | 0 => []
  | k + 1 => k :: rangeL k

theorem range_mem : (k : Nat) → ∀ i, i < k → i ∈ rangeL k
  | 0, _, h => (Nat.not_lt_zero _ h).elim
  | k + 1, i, h => match Nat.decEq i k with
    | isTrue e => by rw [e]; exact .head _
    | isFalse ne => .tail _ (range_mem k i (Nat.lt_of_le_of_ne (Nat.le_of_lt_succ h) ne))

def tuples (k : Nat) : Nat → List (List Nat)
  | 0 => [[]]
  | m + 1 => bindL (fun i => lmap (List.cons i) (tuples k m)) (rangeL k)

theorem tuples_mem (k : Nat) : (l : List Nat) → (∀ i, i ∈ l → i < k) → l ∈ tuples k (lenN l)
  | [], _ => .head _
  | i :: l, h => bind_mem (rangeL k) (range_mem k i (h i (.head _)))
      (lmap_mem (List.cons i) _ (tuples_mem k l (fun j m => h j (.tail _ m))))

variable (τ : List (Nat × Nat))

def atomsK (k : Nat) : List (Nat × List Nat) :=
  bindL (fun p => lmap (fun l => (p.1, l)) (tuples k p.2)) τ

theorem atoms_mem {k r : Nat} {l : List Nat} (h : AtOK τ k r l) : (r, l) ∈ atomsK τ k :=
  bind_mem τ h.1 (lmap_mem (fun l => (r, l)) _ (tuples_mem k l h.2))

/-! ## formulas -/

def negF (a : Fm) : Fm := .imp a .bot
def topF : Fm := .imp .bot .bot

def conjL : List Fm → Fm
  | [] => topF
  | a :: l => .conj a (conjL l)

def disjL : List Fm → Fm
  | [] => .bot
  | a :: l => .disj a (disjL l)

section Sat

variable {A : Type} (RA : Nat → List A → Prop)

theorem sat_conjL : (l : List Fm) → (v : Nat → A) → (Sat RA (conjL l) v ↔ ∀ x, x ∈ l → Sat RA x v)
  | [], _ => ⟨(fun _ _ m => nomatch m), (fun _ h => h)⟩
  | a :: l, v => ⟨fun ⟨h1, h2⟩ x m => by
      cases m with
      | head => exact h1
      | tail _ m' => exact (sat_conjL l v).1 h2 x m',
    fun h => ⟨h a (.head _), (sat_conjL l v).2 (fun x m => h x (.tail _ m))⟩⟩

theorem sat_disjL : (l : List Fm) → (v : Nat → A) → (Sat RA (disjL l) v ↔ ∃ x, x ∈ l ∧ Sat RA x v)
  | [], _ => ⟨fun h => h.elim, fun ⟨_, m, _⟩ => nomatch m⟩
  | a :: l, v => ⟨fun h => h.elim (fun s => ⟨a, .head _, s⟩)
      (fun s => match (sat_disjL l v).1 s with | ⟨x, m, sx⟩ => ⟨x, .tail _ m, sx⟩),
    fun ⟨x, m, sx⟩ => by
      cases m with
      | head => exact .inl sx
      | tail _ m' => exact .inr ((sat_disjL l v).2 ⟨x, m', sx⟩)⟩

end Sat

def lit (p : Nat × List Nat) : Bool → Fm
  | true => .atom p.1 p.2
  | false => negF (.atom p.1 p.2)

def diag : List (Nat × List Nat) → List Bool → List Fm
  | a :: as, b :: bs => lit a b :: diag as bs
  | [], _ => []
  | _ :: _, [] => []

def lenB {X : Type} : List X → Nat
  | [] => 0
  | _ :: l => lenB l + 1

def bits : Nat → List (List Bool)
  | 0 => [[]]
  | m + 1 => lmap (List.cons true) (bits m) ++ lmap (List.cons false) (bits m)

theorem bits_mem : (bs : List Bool) → bs ∈ bits (lenB bs)
  | [] => .head _
  | true :: bs => mem_appL_l _ _ (lmap_mem _ _ (bits_mem bs))
  | false :: bs => mem_appL_r _ _ (lmap_mem _ _ (bits_mem bs))

def sublists {X : Type} : List X → List (List X)
  | [] => [[]]
  | a :: l => sublists l ++ lmap (List.cons a) (sublists l)

theorem sublists_sub {X : Type} : (l : List X) → ∀ S, S ∈ sublists l → ∀ x, x ∈ S → x ∈ l
  | [], S, h, x, m => by
    cases h with
    | head => exact nomatch m
    | tail _ h' => exact nomatch h'
  | a :: l, S, h, x, m => (mem_appL _ _ h).elim (fun h1 => .tail _ (sublists_sub l S h1 x m))
      (fun h2 => match mem_lmap _ h2 with
        | ⟨S', hS', e⟩ => by
          rw [e] at m
          cases m with
          | head => exact .head _
          | tail _ m' => exact .tail _ (sublists_sub l S' hS' x m'))

/-- the Hintikka formulas -/
def H0 (k : Nat) : List Fm := lmap (fun bs => conjL (diag (atomsK τ k) bs)) (bits (lenB (atomsK τ k)))

def H : Nat → Nat → List Fm
  | 0, k => H0 τ k
  | n + 1, k => bindL (fun d => lmap (fun S => Fm.conj d (Fm.conj (conjL (lmap Fm.ex S)) (Fm.all (disjL S))))
      (sublists (H n (k + 1)))) (H0 τ k)

/-! ## classical choices -/

def bdec (K : CP.Classical') (p : Prop) : Bool :=
  K.ch (fun b : Bool => (b = true ↔ p)) (match K.lem p with
    | .inl h => ⟨true, fun _ => h, fun _ => rfl⟩
    | .inr h => ⟨false, fun e => Bool.noConfusion e, fun x => (h x).elim⟩)

theorem bdec_spec (K : CP.Classical') (p : Prop) : bdec K p = true ↔ p :=
  K.ch_spec (fun b : Bool => (b = true ↔ p)) (match K.lem p with
    | .inl h => ⟨true, fun _ => h, fun _ => rfl⟩
    | .inr h => ⟨false, fun e => Bool.noConfusion e, fun x => (h x).elim⟩)

def filt (K : CP.Classical') {X : Type} (P : X → Prop) : List X → List X
  | [] => []
  | x :: l => match bdec K (P x) with
    | true => x :: filt K P l
    | false => filt K P l

theorem filt_spec (K : CP.Classical') {X : Type} (P : X → Prop) : (l : List X) → ∀ x, x ∈ filt K P l → x ∈ l ∧ P x
  | [], _, m => nomatch m
  | y :: l, x, m => by
    unfold filt at m
    cases h : bdec K (P y) with
    | true =>
      rw [h] at m
      cases m with
      | head => exact ⟨.head _, (bdec_spec K _).1 h⟩
      | tail _ m' => match filt_spec K P l x m' with | ⟨a, b⟩ => exact ⟨.tail _ a, b⟩
    | false =>
      rw [h] at m
      match filt_spec K P l x m with | ⟨a, b⟩ => exact ⟨.tail _ a, b⟩

theorem filt_mem (K : CP.Classical') {X : Type} (P : X → Prop) : (l : List X) → ∀ x, x ∈ l → P x → x ∈ filt K P l
  | y :: l, x, m, hp => by
    unfold filt
    cases h : bdec K (P y) with
    | true =>
      cases m with
      | head => exact .head _
      | tail _ m' => exact .tail _ (filt_mem K P l x m' hp)
    | false =>
      cases m with
      | head => exact (Bool.noConfusion (h.symm.trans ((bdec_spec K _).2 hp)))
      | tail _ m' => exact filt_mem K P l x m' hp

theorem filt_sub (K : CP.Classical') {X : Type} (P : X → Prop) : (l : List X) → filt K P l ∈ sublists l
  | [] => .head _
  | y :: l => by
    unfold filt
    cases bdec K (P y) with
    | true => exact mem_appL_r _ _ (lmap_mem _ _ (filt_sub K P l))
    | false => exact mem_appL_l _ _ (filt_sub K P l)

/-! ## the base level -/

theorem diag_lit : (as : List (Nat × List Nat)) → (f : Nat × List Nat → Bool) → ∀ x, x ∈ diag as (lmap f as) →
    ∃ q, q ∈ as ∧ x = lit q (f q)
  | [], _, _, m => nomatch m
  | a :: as, f, x, m => by
    cases m with
    | head => exact ⟨a, .head _, rfl⟩
    | tail _ m' => match diag_lit as f x m' with | ⟨q, hq, e⟩ => exact ⟨q, .tail _ hq, e⟩

theorem diag_has : (as : List (Nat × List Nat)) → (bs : List Bool) → lenB bs = lenB as → ∀ q, q ∈ as →
    ∃ b, lit q b ∈ diag as bs
  | [], _, _, _, m => nomatch m
  | a :: as, [], e, _, _ => Nat.noConfusion e
  | a :: as, b :: bs, e, q, m => by
    cases m with
    | head => exact ⟨b, .head _⟩
    | tail _ m' => match diag_has as bs (Nat.succ.inj e) q m' with | ⟨b', h⟩ => exact ⟨b', .tail _ h⟩

theorem len_lmap {X Y : Type} (f : X → Y) : (l : List X) → lenB (lmap f l) = lenB l
  | [] => rfl
  | _ :: l => congrArg (· + 1) (len_lmap f l)

theorem bits_len : (m : Nat) → ∀ bs, bs ∈ bits m → lenB bs = m
  | 0, bs, h => by
    cases h with
    | head => rfl
    | tail _ h' => exact nomatch h'
  | m + 1, bs, h => (mem_appL _ _ h).elim
      (fun h1 => match mem_lmap _ h1 with | ⟨c, hc, e⟩ => by rw [e]; exact congrArg (· + 1) (bits_len m c hc))
      (fun h2 => match mem_lmap _ h2 with | ⟨c, hc, e⟩ => by rw [e]; exact congrArg (· + 1) (bits_len m c hc))

theorem base_ex (K : CP.Classical') (k : Nat) {A : Type} (RA : Nat → List A → Prop) (v : Nat → A) :
    ∃ χ, χ ∈ H τ 0 k ∧ Sat RA χ v := by
  let f : Nat × List Nat → Bool := fun q => bdec K (RA q.1 (mapD v q.2))
  refine ⟨conjL (diag (atomsK τ k) (lmap f (atomsK τ k))), ?_, ?_⟩
  · have hb := bits_mem (lmap f (atomsK τ k))
    rw [len_lmap] at hb
    exact lmap_mem (fun bs => conjL (diag (atomsK τ k) bs)) _ hb
  · refine (sat_conjL RA _ v).2 (fun x m => ?_)
    match diag_lit _ f x m with
    | ⟨q, _, e⟩ =>
      rw [e]
      cases h : f q with
      | true => exact (bdec_spec K _).1 h
      | false => exact fun hq => Bool.noConfusion (h.symm.trans ((bdec_spec K _).2 hq))

theorem base_char (k : Nat) : ∀ χ, χ ∈ H τ 0 k → ∀ {A B : Type} (RA : Nat → List A → Prop) (RB : Nat → List B → Prop)
    (v : Nat → A) (w : Nat → B), Sat RA χ v → Sat RB χ w → EQ RA RB τ 0 k v w := fun χ hχ _ _ RA RB v w s1 s2 =>
  match mem_lmap _ hχ with
  | ⟨bs, hbs, e⟩ => fun r l hok =>
    have hlen := bits_len _ bs hbs
    match diag_has (atomsK τ k) bs hlen (r, l) (atoms_mem τ hok) with
    | ⟨b, hb⟩ =>
      have h1 := (sat_conjL RA _ v).1 (by rw [← e]; exact s1) _ hb
      have h2 := (sat_conjL RB _ w).1 (by rw [← e]; exact s2) _ hb
      match b, h1, h2 with
      | true, h1, h2 => ⟨fun _ => h2, fun _ => h1⟩
      | false, h1, h2 => ⟨fun x => (h1 x).elim, fun y => (h2 y).elim⟩

/-! ## all levels -/

theorem hint (K : CP.Classical') : (n k : Nat) →
    (∀ {A : Type} (RA : Nat → List A → Prop) (v : Nat → A), ∃ χ, χ ∈ H τ n k ∧ Sat RA χ v) ∧
    (∀ χ, χ ∈ H τ n k → ∀ {A B : Type} (RA : Nat → List A → Prop) (RB : Nat → List B → Prop)
      (v : Nat → A) (w : Nat → B), Sat RA χ v → Sat RB χ w → EQ RA RB τ n k v w)
  | 0, k => ⟨fun RA v => base_ex τ K k RA v, base_char τ k⟩
  | n + 1, k =>
    have ih := hint K n (k + 1)
    ⟨fun RA v => by
      match base_ex τ K k RA v with
      | ⟨d, hd, sd⟩ =>
      let S := filt K (fun χ => ∃ a, Sat RA χ (ext v a)) (H τ n (k + 1))
      refine ⟨Fm.conj d (Fm.conj (conjL (lmap Fm.ex S)) (Fm.all (disjL S))),
        bind_mem _ hd (lmap_mem _ _ (filt_sub K _ _)), sd, (sat_conjL RA _ v).2 (fun x m => ?_), fun a => ?_⟩
      · match mem_lmap _ m with
        | ⟨χ', hχ', e⟩ => rw [e]; exact (filt_spec K _ _ χ' hχ').2
      · match ih.1 RA (ext v a) with
        | ⟨χ', hχ', s'⟩ => exact (sat_disjL RA _ _).2 ⟨χ', filt_mem K _ _ χ' hχ' ⟨a, s'⟩, s'⟩,
     fun χ hχ _ _ RA RB v w s1 s2 => by
      match mem_bind _ hχ with
      | ⟨d, hd, hχ2⟩ =>
      match mem_lmap _ hχ2 with
      | ⟨S, hS, e⟩ =>
      rw [e] at s1 s2
      refine ⟨base_char τ k d hd RA RB v w s1.1 s2.1, fun a => ?_, fun b => ?_⟩
      · match (sat_disjL RA _ _).1 (s1.2.2 a) with
        | ⟨χ', hχ', s'⟩ =>
          match (sat_conjL RB _ w).1 s2.2.1 (Fm.ex χ') (lmap_mem _ _ hχ') with
          | ⟨b, sb⟩ => exact ⟨b, ih.2 χ' (sublists_sub _ S hS χ' hχ') RA RB _ _ s' sb⟩
      · match (sat_disjL RB _ _).1 (s2.2.2 b) with
        | ⟨χ', hχ', s'⟩ =>
          match (sat_conjL RA _ v).1 s1.2.1 (Fm.ex χ') (lmap_mem _ _ hχ') with
          | ⟨a, sa⟩ => exact ⟨a, ih.2 χ' (sublists_sub _ S hS χ' hχ') RA RB _ _ sa s'⟩⟩

/-- a class closed under n-equivalence is defined by a disjunction of Hintikka sentences -/
theorem closed_def (K : CP.Classical') (n : Nat) (Q : (A : Type) → (Nat → List A → Prop) → Prop)
    (cl : ∀ (A B : Type) (RA : Nat → List A → Prop) (RB : Nat → List B → Prop) (v : Nat → A) (w : Nat → B),
      EQ RA RB τ n 0 v w → Q A RA → Q B RB) :
    ∃ φ, (∀ χ, χ ∈ φ → χ ∈ H τ n 0) ∧ ∀ (A : Type) (RA : Nat → List A → Prop) (v : Nat → A), Q A RA ↔ Sat RA (disjL φ) v :=
  ⟨filt K (fun χ => ∃ (A : Type) (RA : Nat → List A → Prop) (v : Nat → A), Q A RA ∧ Sat RA χ v) (H τ n 0),
   fun χ m => (filt_spec K _ _ χ m).1,
   fun A RA v => ⟨fun q => match (hint τ K n 0).1 RA v with
      | ⟨χ, hχ, s⟩ => (sat_disjL RA _ v).2 ⟨χ, filt_mem K _ _ χ hχ ⟨A, RA, v, q, s⟩, s⟩,
    fun s => match (sat_disjL RA _ v).1 s with
      | ⟨χ, m, sχ⟩ => match filt_spec K _ _ χ m with
        | ⟨hχ, A', RA', v', q', s'⟩ => cl A' A RA' RA v' v ((hint τ K n 0).2 χ hχ RA' RA v' v s' sχ) q'⟩⟩

end Lind

end Found

#print axioms Found.Lind.tuples_mem
#print axioms Found.Lind.atoms_mem
#print axioms Found.Lind.sat_conjL
#print axioms Found.Lind.sat_disjL
#print axioms Found.Lind.bits_mem
#print axioms Found.Lind.sublists_sub
#print axioms Found.Lind.bdec_spec
#print axioms Found.Lind.filt_spec
#print axioms Found.Lind.filt_mem
#print axioms Found.Lind.filt_sub
#print axioms Found.Lind.base_ex
#print axioms Found.Lind.base_char
#print axioms Found.Lind.hint
#print axioms Found.Lind.closed_def

found/Lin4.lean

lean_all/found/Lin4.lean

/-
  T190 (Lindström), part 4. found/Lin4 — abstract logics extending
  first-order logic (with equality as the relation symbol 0 of "normal"
  structures): sentences with a finite vocabulary (a list of relation
  symbols) whose truth only depends on these symbols, invariant under
  isomorphism, containing the first-order sentences, closed under ¬, ∧,
  relativization to a unary symbol and renaming of symbols, with
  compactness and the downward Löwenheim–Skolem property (countable models).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Lin3
import found.NbgZ4

namespace Found

namespace Lind

open FO FOCT NBGc

/-- normal structures: symbol 0 is equality -/
def Normal {A : Type} (R : Nat → List A → Prop) : Prop := ∀ x y : A, R 0 [x, y] ↔ x = y

/-- the substructure on the elements satisfying the unary symbol P -/
def Rsub {A : Type} (R : Nat → List A → Prop) (P : Nat) : Nat → List {a : A // R P [a]} → Prop :=
  fun r l => R r (lmap Subtype.val l)

/-- countable: a surjection from ℕ -/
def Cnt (A : Type) : Prop := ∃ e : Nat → A, ∀ a, ∃ k, e k = a

/-- the symbols (with arities) of a formula -/
def symsF : Fm → List (Nat × Nat)
  | .atom r l => [(r, lenN l)]
  | .bot => []
  | .conj a b => symsF a ++ symsF b
  | .disj a b => symsF a ++ symsF b
  | .imp a b => symsF a ++ symsF b
  | .all a => symsF a
  | .ex a => symsF a

/-- R and R' agree on the symbols of a vocabulary -/
def Agree {A : Type} (τ : List (Nat × Nat)) (R R' : Nat → List A → Prop) : Prop :=
  ∀ r l, (r, lenB l) ∈ τ → (R r l ↔ R' r l)

structure ALogic where
  Sen : Type
  sat : {A : Type} → (Nat → List A → Prop) → Sen → Prop
  voc : Sen → List (Nat × Nat)
  dep : ∀ (s : Sen) {A : Type} (R R' : Nat → List A → Prop), Agree (voc s) R R' → (sat R s ↔ sat R' s)
  iso : ∀ (s : Sen) {A B : Type} (RA : Nat → List A → Prop) (RB : Nat → List B → Prop), Iso RA RB → sat RA s → sat RB s
  fo : ∀ φ : Fm, ∃ s : Sen, (∀ p, p ∈ voc s → p ∈ symsF φ) ∧
    ∀ {A : Type} (R : Nat → List A → Prop) (v : Nat → A), FV 0 φ → (sat R s ↔ Sat R φ v)
  neg : ∀ s : Sen, ∃ t : Sen, (∀ p, p ∈ voc t → p ∈ voc s) ∧ ∀ {A : Type} (R : Nat → List A → Prop), sat R t ↔ ¬ sat R s
  conj : ∀ s t : Sen, ∃ u : Sen, (∀ p, p ∈ voc u → p ∈ voc s ∨ p ∈ voc t) ∧
    ∀ {A : Type} (R : Nat → List A → Prop), sat R u ↔ sat R s ∧ sat R t
  rel : ∀ (s : Sen) (P : Nat), ∃ t : Sen, (∀ p, p ∈ voc t → p ∈ voc s ∨ p = (P, 1)) ∧
    ∀ {A : Type} (R : Nat → List A → Prop), (∃ a, R P [a]) → (sat R t ↔ sat (Rsub R P) s)
  ren : ∀ (s : Sen) (σ : Nat → Nat), ∃ t : Sen, (∀ p, p ∈ voc t → ∃ q, q ∈ voc s ∧ p = (σ q.1, q.2)) ∧
    ∀ {A : Type} (R : Nat → List A → Prop), sat R t ↔ sat (fun r l => R (σ r) l) s
  compact : ∀ T : Sen → Prop, (∀ L : List Sen, (∀ s, s ∈ L → T s) →
      ∃ (A : Type) (R : Nat → List A → Prop), Normal R ∧ Nonempty A ∧ ∀ s, s ∈ L → sat R s) →
    ∃ (A : Type) (R : Nat → List A → Prop), Normal R ∧ Nonempty A ∧ ∀ s, T s → sat R s
  ls : ∀ (s : Sen) {A : Type} (R : Nat → List A → Prop), Normal R → Nonempty A → sat R s →
    ∃ (C : Type) (RC : Nat → List C → Prop), Normal RC ∧ Nonempty C ∧ Cnt C ∧ sat RC s

/-- a class of normal structures closed under n-equivalence is defined by a disjunction of Hintikka sentences -/
theorem closed_defN (K : CP.Classical') (τ : List (Nat × Nat)) (n : Nat)
    (Q : (A : Type) → (Nat → List A → Prop) → Prop)
    (cl : ∀ (A B : Type) (RA : Nat → List A → Prop) (RB : Nat → List B → Prop) (v : Nat → A) (w : Nat → B),
      Normal RA → Normal RB → EQ RA RB τ n 0 v w → Q A RA → Q B RB) :
    ∃ φ, (∀ χ, χ ∈ φ → χ ∈ H τ n 0) ∧ ∀ (A : Type) (RA : Nat → List A → Prop) (v : Nat → A), Normal RA →
      (Q A RA ↔ Sat RA (disjL φ) v) :=
  ⟨filt K (fun χ => ∃ (A : Type) (RA : Nat → List A → Prop) (v : Nat → A), Normal RA ∧ Q A RA ∧ Sat RA χ v) (H τ n 0),
   fun χ m => (filt_spec K _ _ χ m).1,
   fun A RA v hn => ⟨fun q => match (hint τ K n 0).1 RA v with
      | ⟨χ, hχ, s⟩ => (sat_disjL RA _ v).2 ⟨χ, filt_mem K _ _ χ hχ ⟨A, RA, v, hn, q, s⟩, s⟩,
    fun s => match (sat_disjL RA _ v).1 s with
      | ⟨χ, m, sχ⟩ => match filt_spec K _ _ χ m with
        | ⟨hχ, A', RA', v', hn', q', s'⟩ => cl A' A RA' RA v' v hn' hn ((hint τ K n 0).2 χ hχ RA' RA v' v s' sχ) q'⟩⟩

end Lind

end Found

#print axioms Found.Lind.closed_defN

found/Lin5.lean

lean_all/found/Lin5.lean

/-
  T190 (Lindström), part 5. found/Lin5 — blocks of quantifiers in
  de Bruijn form: extending a valuation by a list of values, ∀ over m
  variables, the list of the m newest variables, and the pairing formula
  "G p x_i y_i for all i < m" used to say that a coded partial map preserves
  a relation of arity m.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Lin4

namespace Found

namespace Lind

open FO FOCT

section T

variable {D : Type}

def extL (v : Nat → D) : List D → Nat → D
  | [] => v
  | d :: ds => extL (ext v d) ds

def allN : Nat → Fm → Fm
  | 0, φ => φ
  | m + 1, φ => .all (allN m φ)

def idxL : Nat → List Nat
  | 0 => []
  | m + 1 => m :: idxL m

def shiftL (j : Nat) : List Nat → List Nat
  | [] => []
  | i :: l => (i + j) :: shiftL j l

theorem sat_allN (R : Nat → List D → Prop) (φ : Fm) : (m : Nat) → (v : Nat → D) →
    (Sat R (allN m φ) v ↔ ∀ ds : List D, lenB ds = m → Sat R φ (extL v ds))
  | 0, _ => ⟨fun h ds e => match ds, e with
      | [], _ => h,
    fun h => h [] rfl⟩
  | m + 1, v => ⟨fun h ds e => match ds, e with
      | d :: ds, e => (sat_allN R φ m (ext v d)).1 (h d) ds (Nat.succ.inj e),
    fun h d => (sat_allN R φ m (ext v d)).2 (fun ds e => h (d :: ds) (congrArg (· + 1) e))⟩

theorem extL_off (v : Nat → D) : (ds : List D) → (i : Nat) → extL v ds (i + lenB ds) = v i
  | [], _ => rfl
  | d :: ds, i => by
    show extL (ext v d) ds ((i + lenB ds) + 1) = v i
    have h := extL_off (ext v d) ds (i + 1)
    rw [Nat.succ_add] at h
    exact h

theorem map_idx (v : Nat → D) : (ds : List D) → mapD (extL v ds) (idxL (lenB ds)) = ds
  | [] => rfl
  | d :: ds => by
    show extL (ext v d) ds (lenB ds) :: mapD (extL (ext v d) ds) (idxL (lenB ds)) = d :: ds
    have h0 : extL (ext v d) ds (lenB ds) = d := by
      have := extL_off (ext v d) ds 0
      rw [Nat.zero_add] at this
      exact this
    rw [h0, map_idx (ext v d) ds]

theorem map_shift (u : Nat → D) (ys : List D) : (l : List Nat) → mapD (extL u ys) (shiftL (lenB ys) l) = mapD u l
  | [] => rfl
  | i :: l => by
    show extL u ys (i + lenB ys) :: mapD (extL u ys) (shiftL (lenB ys) l) = u i :: mapD u l
    rw [extL_off, map_shift u ys l]

/-- the newest m variables in order, and the m before them -/
def ysL (m : Nat) : List Nat := idxL m
def xsL (m : Nat) : List Nat := shiftL m (idxL m)

theorem map_ys (u : Nat → D) (ys : List D) : mapD (extL u ys) (ysL (lenB ys)) = ys := map_idx u ys

theorem map_xs (v : Nat → D) (xs ys : List D) (e : lenB xs = lenB ys) :
    mapD (extL (extL v xs) ys) (xsL (lenB ys)) = xs := by
  show mapD (extL (extL v xs) ys) (shiftL (lenB ys) (idxL (lenB ys))) = xs
  rw [map_shift, ← e, map_idx]

theorem extL_lt (u u' : Nat → D) : (ds : List D) → ∀ i, i < lenB ds → extL u ds i = extL u' ds i
  | [], _, h => (Nat.not_lt_zero _ h).elim
  | d :: ds, i, h => match Nat.decEq i (lenB ds) with
    | isTrue e => by
      show extL (ext u d) ds i = extL (ext u' d) ds i
      have h1 := extL_off (ext u d) ds 0
      have h2 := extL_off (ext u' d) ds 0
      rw [Nat.zero_add] at h1 h2
      rw [e, h1, h2]; rfl
    | isFalse ne => extL_lt (ext u d) (ext u' d) ds i (Nat.lt_of_le_of_ne (Nat.le_of_lt_succ h) ne)

theorem len_fst {X Y : Type} : (l : List (X × Y)) → lenB (fstL l) = lenB l
  | [] => rfl
  | _ :: l => congrArg (· + 1) (len_fst l)

theorem len_snd {X Y : Type} : (l : List (X × Y)) → lenB (sndL l) = lenB l
  | [] => rfl
  | _ :: l => congrArg (· + 1) (len_snd l)

/-- the i-th newest values of the two lists come from one pair -/
theorem pair_ix {X Y : Type} (u : Nat → X) (w : Nat → Y) : (l : List (X × Y)) → ∀ i, i < lenB l →
    (extL u (fstL l) i, extL w (sndL l) i) ∈ l
  | [], _, h => (Nat.not_lt_zero _ h).elim
  | p :: l, i, h => match Nat.decEq i (lenB l) with
    | isTrue e => by
      show (extL (ext u p.1) (fstL l) i, extL (ext w p.2) (sndL l) i) ∈ p :: l
      have h1 := extL_off (ext u p.1) (fstL l) 0
      have h2 := extL_off (ext w p.2) (sndL l) 0
      rw [Nat.zero_add, len_fst] at h1
      rw [Nat.zero_add, len_snd] at h2
      rw [e, h1, h2]
      exact .head _
    | isFalse ne => .tail _ (pair_ix (ext u p.1) (ext w p.2) l i (Nat.lt_of_le_of_ne (Nat.le_of_lt_succ h) ne))

/-- "G p x_i y_i for all i < m" with p the variable 2m -/
def pairsF (gS m : Nat) : Fm := conjL (lmap (fun i => Fm.atom gS [m + m, i + m, i]) (rangeL m))

/-- the coded partial map p preserves the relation (symbol cA on the left, cB on the right) of arity m -/
def presF (gS cA cB m : Nat) : Fm :=
  .all (allN m (allN m (.imp (pairsF gS m) (NBGc.iffF (.atom cA (xsL m)) (.atom cB (ysL m))))))

theorem mem_range : (m : Nat) → ∀ i, i ∈ rangeL m → i < m
  | 0, _, h => nomatch h
  | m + 1, i, h => by
    cases h with
    | head => exact Nat.lt_succ_self m
    | tail _ h' => exact Nat.lt_succ_of_lt (mem_range m i h')

theorem sat_pairsF (R : Nat → List D → Prop) (gS m : Nat) (v : Nat → D) :
    Sat R (pairsF gS m) v ↔ ∀ i, i < m → R gS [v (m + m), v (i + m), v i] :=
  (sat_conjL R _ v).trans ⟨fun h i hi => h _ (lmap_mem _ _ (range_mem m i hi)),
    fun h x hx => match mem_lmap _ hx with
      | ⟨i, hi, e⟩ => by rw [e]; exact h i (mem_range m i hi)⟩

end T

/-- the semantic content of presF -/
theorem sat_presF {D : Type} (R : Nat → List D → Prop) (gS cA cB m : Nat) (v : Nat → D) :
    Sat R (presF gS cA cB m) v ↔ ∀ (p : D) (xs ys : List D), lenB xs = m → lenB ys = m →
      (∀ i, i < m → R gS [p, extL (ext v p) xs i, extL (ext v p) ys i]) → (R cA xs ↔ R cB ys) := by
  constructor
  · intro h p xs ys ex ey hp
    have h1 := (sat_allN R _ m _).1 (h p) xs ex
    have h2 := (sat_allN R _ m _).1 h1 ys ey
    have hpv : extL (extL (ext v p) xs) ys (m + m) = p := by
      have a1 := extL_off (extL (ext v p) xs) ys m
      have a2 := extL_off (ext v p) xs 0
      rw [ey] at a1
      rw [Nat.zero_add, ex] at a2
      rw [a1, a2]; rfl
    have hg := h2 ((sat_pairsF R gS m _).2 (fun i hi => by
      have a1 := extL_off (extL (ext v p) xs) ys i
      rw [ey] at a1
      rw [hpv, a1, extL_lt (extL (ext v p) xs) (ext v p) ys i (by rw [ey]; exact hi)]
      exact hp i hi))
    have mx := map_xs (ext v p) xs ys (ex.trans ey.symm)
    have my := map_ys (extL (ext v p) xs) ys
    rw [ey] at mx my
    have hx : Sat R (Fm.atom cA (xsL m)) (extL (extL (ext v p) xs) ys) ↔ R cA xs := by
      show R cA (mapD _ (xsL m)) ↔ _
      rw [mx]
    have hy : Sat R (Fm.atom cB (ysL m)) (extL (extL (ext v p) xs) ys) ↔ R cB ys := by
      show R cB (mapD _ (ysL m)) ↔ _
      rw [my]
    exact ⟨fun a => hy.1 (hg.1 (hx.2 a)), fun b => hx.1 (hg.2 (hy.2 b))⟩
  · intro h p
    refine (sat_allN R _ m _).2 (fun xs ex => (sat_allN R _ m _).2 (fun ys ey hg => ?_))
    have hpv : extL (extL (ext v p) xs) ys (m + m) = p := by
      have a1 := extL_off (extL (ext v p) xs) ys m
      have a2 := extL_off (ext v p) xs 0
      rw [ey] at a1
      rw [Nat.zero_add, ex] at a2
      rw [a1, a2]; rfl
    have pr := (sat_pairsF R gS m _).1 hg
    have e := h p xs ys ex ey (fun i hi => by
      have q := pr i hi
      have a1 := extL_off (extL (ext v p) xs) ys i
      rw [ey] at a1
      rw [hpv, a1, extL_lt (extL (ext v p) xs) (ext v p) ys i (by rw [ey]; exact hi)] at q
      exact q)
    have mx := map_xs (ext v p) xs ys (ex.trans ey.symm)
    have my := map_ys (extL (ext v p) xs) ys
    rw [ey] at mx my
    show (R cA (mapD _ (xsL m)) → R cB (mapD _ (ysL m))) ∧ (R cB (mapD _ (ysL m)) → R cA (mapD _ (xsL m)))
    rw [mx, my]
    exact ⟨e.1, e.2⟩


end Lind

end Found

#print axioms Found.Lind.sat_allN
#print axioms Found.Lind.extL_off
#print axioms Found.Lind.map_idx
#print axioms Found.Lind.map_shift
#print axioms Found.Lind.map_xs
#print axioms Found.Lind.extL_lt
#print axioms Found.Lind.pair_ix
#print axioms Found.Lind.sat_pairsF
#print axioms Found.Lind.sat_presF

found/Lin6.lean

lean_all/found/Lin6.lean

/-
  T190 (Lindström), part 6. found/Lin6 — the first-order sentence θ_τ that
  says, in a combined structure with symbols PA (1), PB (2), levels U (3),
  predecessor Pre (4), codes Pc (5), level of a code Lev (6), graph G (7),
  a constant Cst (8): every level has a code; codes extend forth and back
  from a level to its predecessor; codes are injective partial maps from
  PA to PB preserving every τ-relation (copied as 2r+12 on the PA side and
  2r+13 on the PB side); Pre is functional, Cst has at most one element, PA
  and PB are nonempty. The chain sentences Ch k say that the constant has
  k predecessors below it. All are first-order sentences.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Lin5
import found.NbgZ5

namespace Found

namespace Lind

open FO FOCT NBGc

def cA (r : Nat) : Nat := 2 * r + 12
def cB (r : Nat) : Nat := 2 * r + 13

def m1 (r a : Nat) : Fm := .atom r [a]
def m2 (r a b : Nat) : Fm := .atom r [a, b]

def T1 : Fm := .all (.imp (m1 3 0) (.ex (.conj (m1 5 0) (m2 6 0 1))))
def extF : Fm := .all (.all (.imp (.atom 7 [6, 1, 0]) (.atom 7 [2, 1, 0])))
def T2 : Fm := .all (.all (.all (.imp (.conj (m2 6 2 1) (m2 4 0 1)) (.all (.imp (m1 1 0)
  (.ex (.conj (m2 6 0 2) (.conj extF (.ex (.conj (m1 2 0) (.atom 7 [1, 2, 0])))))))))))
def T3 : Fm := .all (.all (.all (.imp (.conj (m2 6 2 1) (m2 4 0 1)) (.all (.imp (m1 2 0)
  (.ex (.conj (m2 6 0 2) (.conj extF (.ex (.conj (m1 1 0) (.atom 7 [1, 0, 2])))))))))))
def T4 : Fm := .all (.all (.all (.all (.all (.imp (.conj (.atom 7 [4, 3, 2]) (.atom 7 [4, 1, 0]))
  (iffF (m2 0 3 1) (m2 0 2 0)))))))
def T6 : Fm := .all (.all (.all (.imp (.atom 7 [2, 1, 0]) (.conj (m1 1 1) (m1 2 0)))))
def T8 : Fm := .all (.all (.all (.imp (.conj (m2 4 1 2) (m2 4 0 2)) (m2 0 1 0))))
def T9 : Fm := .all (.all (.imp (.conj (m1 8 1) (m1 8 0)) (m2 0 1 0)))
def NA : Fm := .ex (m1 1 0)
def NB : Fm := .ex (m1 2 0)

def chainF : Nat → Fm
  | 0 => topF
  | k + 1 => .ex (.conj (m2 4 0 1) (.conj (m1 3 0) (chainF k)))

def Ch (k : Nat) : Fm := .ex (.conj (m1 8 0) (.conj (m1 3 0) (chainF k)))

def theta (τ : List (Nat × Nat)) : Fm :=
  conjL ([T1, T2, T3, T4, T6, T8, T9, NA, NB] ++ lmap (fun q => presF 7 (cA q.1) (cB q.1) q.2) τ)

/-! ## closedness -/

theorem fv_mono : (φ : Fm) → {k j : Nat} → k ≤ j → FV k φ → FV j φ
  | .atom _ l, _, _, h, f => by
    show AllLt _ l
    exact allLt_mono l h f
  | .bot, _, _, _, _ => trivial
  | .conj a b, _, _, h, f => ⟨fv_mono a h f.1, fv_mono b h f.2⟩
  | .disj a b, _, _, h, f => ⟨fv_mono a h f.1, fv_mono b h f.2⟩
  | .imp a b, _, _, h, f => ⟨fv_mono a h f.1, fv_mono b h f.2⟩
  | .all a, _, _, h, f => fv_mono a (Nat.succ_le_succ h) f
  | .ex a, _, _, h, f => fv_mono a (Nat.succ_le_succ h) f
where
  allLt_mono : (l : List Nat) → {k j : Nat} → k ≤ j → AllLt k l → AllLt j l
    | [], _, _, _, _ => trivial
    | _ :: l, _, _, h, f => ⟨Nat.lt_of_lt_of_le f.1 h, allLt_mono l h f.2⟩

theorem fv_allN : (m k : Nat) → (φ : Fm) → FV (k + m) φ → FV k (allN m φ)
  | 0, _, _, h => h
  | m + 1, k, φ, h => by
    show FV (k + 1) (allN m φ)
    apply fv_allN m (k + 1) φ
    rw [Nat.succ_add]
    exact h

theorem fv_conjL : (l : List Fm) → (k : Nat) → (∀ x, x ∈ l → FV k x) → FV k (conjL l)
  | [], _, _ => ⟨trivial, trivial⟩
  | a :: l, k, h => ⟨h a (.head _), fv_conjL l k (fun x m => h x (.tail _ m))⟩

theorem allLt_idx : (m j : Nat) → m ≤ j → AllLt j (idxL m)
  | 0, _, _ => trivial
  | m + 1, j, h => ⟨h, allLt_idx m j (Nat.le_of_succ_le h)⟩

theorem allLt_shift (s : Nat) : (l : List Nat) → (j : Nat) → AllLt j l → AllLt (j + s) (shiftL s l)
  | [], _, _ => trivial
  | _ :: l, j, h => ⟨Nat.add_lt_add_right h.1 s, allLt_shift s l j h.2⟩

theorem fv_presF (gS c1 c2 m : Nat) : FV 0 (presF gS c1 c2 m) := by
  show FV 1 (allN m (allN m _))
  apply fv_allN m 1
  apply fv_allN m (1 + m)
  refine ⟨fv_conjL _ _ (fun x hx => ?_), ⟨⟨?_, ?_⟩, ?_, ?_⟩⟩
  · match mem_lmap _ hx with
    | ⟨i, hi, e⟩ =>
      rw [e]
      have hi' := mem_range m i hi
      refine ⟨?_, ?_, ?_, trivial⟩
      · show m + m < 1 + m + m
        rw [Nat.add_comm 1 m, Nat.add_right_comm m 1 m]
        exact Nat.lt_succ_self _
      · show i + m < 1 + m + m
        exact Nat.lt_of_lt_of_le (Nat.add_lt_add_right hi' m) (Nat.add_le_add_right (Nat.le_add_left m 1) m)
      · show i < 1 + m + m
        exact Nat.lt_of_lt_of_le hi' (Nat.le_trans (Nat.le_add_left m 1) (Nat.le_add_right _ _))
  · show AllLt (1 + m + m) (shiftL m (idxL m))
    have h := allLt_shift m (idxL m) m (allLt_idx m m (Nat.le_refl m))
    exact fv_mono.allLt_mono _ (by rw [Nat.add_assoc 1 m m, Nat.add_comm 1 (m + m)]; exact Nat.le_succ _) h
  · show AllLt (1 + m + m) (idxL m)
    exact allLt_idx m _ (Nat.le_trans (Nat.le_add_left m 1) (Nat.le_add_right _ _))
  · show AllLt (1 + m + m) (idxL m)
    exact allLt_idx m _ (Nat.le_trans (Nat.le_add_left m 1) (Nat.le_add_right _ _))
  · show AllLt (1 + m + m) (shiftL m (idxL m))
    have h := allLt_shift m (idxL m) m (allLt_idx m m (Nat.le_refl m))
    exact fv_mono.allLt_mono _ (by rw [Nat.add_assoc 1 m m, Nat.add_comm 1 (m + m)]; exact Nat.le_succ _) h

theorem fv_theta (τ : List (Nat × Nat)) : FV 0 (theta τ) :=
  fv_conjL _ 0 (fun x hx => (mem_appL _ _ hx).elim
    (fun h => by
      have h1 := fvB_ok 0 T1 rfl
      have h2 := fvB_ok 0 T2 rfl
      have h3 := fvB_ok 0 T3 rfl
      have h4 := fvB_ok 0 T4 rfl
      have h6 := fvB_ok 0 T6 rfl
      have h8 := fvB_ok 0 T8 rfl
      have h9 := fvB_ok 0 T9 rfl
      have ha := fvB_ok 0 NA rfl
      have hb := fvB_ok 0 NB rfl
      cases h with
      | head => exact h1
      | tail _ h => cases h with
        | head => exact h2
        | tail _ h => cases h with
          | head => exact h3
          | tail _ h => cases h with
            | head => exact h4
            | tail _ h => cases h with
              | head => exact h6
              | tail _ h => cases h with
                | head => exact h8
                | tail _ h => cases h with
                  | head => exact h9
                  | tail _ h => cases h with
                    | head => exact ha
                    | tail _ h => cases h with
                      | head => exact hb
                      | tail _ h => exact nomatch h)
    (fun h => match mem_lmap _ h with
      | ⟨q, _, e⟩ => by rw [e]; exact fv_presF 7 _ _ _))

theorem fv_chain : (k : Nat) → FV 1 (chainF k)
  | 0 => ⟨trivial, trivial⟩
  | k + 1 => ⟨⟨Nat.zero_lt_succ 1, Nat.lt_succ_self 1, trivial⟩, ⟨Nat.zero_lt_succ 1, trivial⟩,
      fv_mono (chainF k) (Nat.le_succ 1) (fv_chain k)⟩

theorem fv_Ch (k : Nat) : FV 0 (Ch k) := ⟨⟨Nat.zero_lt_succ _, trivial⟩, ⟨Nat.zero_lt_succ _, trivial⟩, fv_chain k⟩

end Lind

end Found

#print axioms Found.Lind.fv_presF
#print axioms Found.Lind.fv_theta
#print axioms Found.Lind.fv_Ch

found/Lin7.lean

lean_all/found/Lin7.lean

/-
  T190 (Lindström), part 7. found/Lin7 — the list form of the preservation
  formula: presF holds iff for every element p and every list of pairs all
  G-related through p, the relation holds on the first components exactly
  when it holds on the second components.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Lin6

namespace Found

namespace Lind

open FO FOCT NBGc

section Z

variable {D : Type}

def zipL : List D → List D → List (D × D)
  | x :: xs, y :: ys => (x, y) :: zipL xs ys
  | [], _ => []
  | _ :: _, [] => []

theorem fst_zip : (xs ys : List D) → lenB xs = lenB ys → fstL (zipL xs ys) = xs
  | [], [], _ => rfl
  | x :: xs, _ :: ys, e => congrArg (List.cons x) (fst_zip xs ys (Nat.succ.inj e))
  | [], _ :: _, e => Nat.noConfusion e
  | _ :: _, [], e => Nat.noConfusion e

theorem snd_zip : (xs ys : List D) → lenB xs = lenB ys → sndL (zipL xs ys) = ys
  | [], [], _ => rfl
  | _ :: xs, y :: ys, e => congrArg (List.cons y) (snd_zip xs ys (Nat.succ.inj e))
  | [], _ :: _, e => Nat.noConfusion e
  | _ :: _, [], e => Nat.noConfusion e

theorem len_zip : (xs ys : List D) → lenB xs = lenB ys → lenB (zipL xs ys) = lenB xs
  | [], [], _ => rfl
  | _ :: xs, _ :: ys, e => congrArg (· + 1) (len_zip xs ys (Nat.succ.inj e))
  | [], _ :: _, e => Nat.noConfusion e
  | _ :: _, [], e => Nat.noConfusion e

theorem mem_zip_ix (u w : Nat → D) : (xs ys : List D) → lenB xs = lenB ys → ∀ q, q ∈ zipL xs ys →
    ∃ i, i < lenB xs ∧ q = (extL u xs i, extL w ys i)
  | [], [], _, _, m => nomatch m
  | x :: xs, y :: ys, e, q, m => by
    cases m with
    | head =>
      refine ⟨lenB xs, Nat.lt_succ_self _, ?_⟩
      show (x, y) = (extL (ext u x) xs (lenB xs), extL (ext w y) ys (lenB xs))
      have h1 := extL_off (ext u x) xs 0
      have h2 := extL_off (ext w y) ys 0
      rw [Nat.zero_add] at h1 h2
      have e' : lenB xs = lenB ys := Nat.succ.inj e
      rw [h1]
      rw [e', h2]
      rfl
    | tail _ m' => match mem_zip_ix (ext u x) (ext w y) xs ys (Nat.succ.inj e) q m' with
      | ⟨i, hi, eq⟩ => exact ⟨i, Nat.lt_succ_of_lt hi, eq⟩
  | [], _ :: _, e, _, _ => Nat.noConfusion e
  | _ :: _, [], e, _, _ => Nat.noConfusion e

end Z

theorem sat_presL {D : Type} (R : Nat → List D → Prop) (gS c1 c2 m : Nat) (v : Nat → D) :
    Sat R (presF gS c1 c2 m) v ↔ ∀ (p : D) (l : List (D × D)), lenB l = m →
      (∀ q, q ∈ l → R gS [p, q.1, q.2]) → (R c1 (fstL l) ↔ R c2 (sndL l)) :=
  (sat_presF R gS c1 c2 m v).trans ⟨fun h p l el hl =>
      h p (fstL l) (sndL l) ((len_fst l).trans el) ((len_snd l).trans el) (fun i hi =>
        hl _ (pair_ix (ext v p) (ext v p) l i (by rw [el]; exact hi))),
    fun h p xs ys ex ey hp => by
      have e := ex.trans ey.symm
      have r := h p (zipL xs ys) ((len_zip xs ys e).trans ex) (fun q hq =>
        match mem_zip_ix (ext v p) (ext v p) xs ys e q hq with
        | ⟨i, hi, eq⟩ => by rw [eq]; exact hp i (by rw [← ex]; exact hi))
      rw [fst_zip xs ys e, snd_zip xs ys e] at r
      exact r⟩

end Lind

end Found

#print axioms Found.Lind.mem_zip_ix
#print axioms Found.Lind.sat_presL

found/Lin8.lean

lean_all/found/Lin8.lean

/-
  T190 (Lindström), part 8. found/Lin8 — the combined structure C_n built
  from two n-equivalent normal structures (A, RA, v) and (B, RB, w): the
  elements of A and of B, the levels 0..n, and the codes (m, L) of finite
  lists of pairs L that are m-equivalent at their own valuations. Symbols:
  0 equality, 1 A, 2 B, 3 levels, 4 predecessor, 5 codes, 6 level of a code,
  7 graph, 8 the top level n, 2r+12 / 2r+13 the copies of r in A / in B.
  C_n is normal and satisfies θ_τ and the chain sentences Ch k for k ≤ n.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Lin7

namespace Found

namespace Lind

open FO FOCT NBGc

inductive CU (A B : Type) where
  | a : A → CU A B
  | b : B → CU A B
  | lv : Nat → CU A B
  | cd : Nat → List (A × B) → CU A B

section C

variable {A B : Type} (RA : Nat → List A → Prop) (RB : Nat → List B → Prop) (τ : List (Nat × Nat))
  (n : Nat) (v : Nat → A) (w : Nat → B)

def lvA : List (A × B) → Nat → A
  | [] => v
  | p :: L => ext (lvA L) p.1

def lvB : List (A × B) → Nat → B
  | [] => w
  | p :: L => ext (lvB L) p.2

def good (m : Nat) (L : List (A × B)) : Prop := EQ RA RB τ m (lenB L) (lvA v L) (lvB w L)

def RcHi (k : Nat) (l : List (CU A B)) : Prop :=
  (∃ q xs, k = 2 * q ∧ l = lmap CU.a xs ∧ RA q xs) ∨ (∃ q ys, k = 2 * q + 1 ∧ l = lmap CU.b ys ∧ RB q ys)

def Rc : Nat → List (CU A B) → Prop
  | 0, l => ∃ x, l = [x, x]
  | 1, l => ∃ x, l = [CU.a x]
  | 2, l => ∃ y, l = [CU.b y]
  | 3, l => ∃ m, l = [CU.lv m] ∧ m ≤ n
  | 4, l => ∃ m, l = [CU.lv m, CU.lv (m + 1)] ∧ m + 1 ≤ n
  | 5, l => ∃ m L, l = [CU.cd m L] ∧ m ≤ n ∧ good RA RB τ v w m L
  | 6, l => ∃ m L, l = [CU.cd m L, CU.lv m] ∧ m ≤ n ∧ good RA RB τ v w m L
  | 7, l => ∃ m L x y, l = [CU.cd m L, CU.a x, CU.b y] ∧ m ≤ n ∧ good RA RB τ v w m L ∧ (x, y) ∈ L
  | 8, l => l = [CU.lv n]
  | 9, _ => False
  | 10, _ => False
  | 11, _ => False
  | k + 12, l => RcHi RA RB k l

/-! ## basic facts -/

theorem rc_normal : Normal (Rc RA RB τ n v w) := fun x y =>
  ⟨fun ⟨z, h⟩ => have h1 := List.cons.inj h
      have h2 := List.cons.inj h1.2
      h1.1.trans h2.1.symm,
   fun e => ⟨x, by rw [e]⟩⟩

theorem eq_down : (m k : Nat) → {v : Nat → A} → {w : Nat → B} → EQ RA RB τ (m + 1) k v w → EQ RA RB τ m k v w
  | 0, _, _, _, h => h.1
  | m + 1, _, _, _, h => ⟨h.1, fun a => match h.2.1 a with | ⟨b, hb⟩ => ⟨b, eq_down m _ hb⟩,
      fun b => match h.2.2 b with | ⟨a, ha⟩ => ⟨a, eq_down m _ ha⟩⟩

theorem eq_le (m k : Nat) {v : Nat → A} {w : Nat → B} : (j : Nat) → EQ RA RB τ (m + j) k v w → EQ RA RB τ m k v w
  | 0, h => h
  | j + 1, h => eq_le m k j (eq_down RA RB τ (m + j) k h)

theorem le_split {m n' : Nat} (hle : m ≤ n') : ∃ j, n' = m + j := by
  induction hle with
  | refl => exact ⟨0, rfl⟩
  | step _ ih => match ih with | ⟨j, e⟩ => exact ⟨j + 1, by rw [e]; rfl⟩

theorem eq_of_le {m n' k : Nat} {v : Nat → A} {w : Nat → B} (hle : m ≤ n') (h : EQ RA RB τ n' k v w) :
    EQ RA RB τ m k v w := by
  match le_split hle with
  | ⟨j, e⟩ =>
    rw [e] at h
    exact eq_le RA RB τ m k j h

theorem pos (v : Nat → A) (w : Nat → B) : (L : List (A × B)) → ∀ x y, (x, y) ∈ L →
    ∃ i, i < lenB L ∧ lvA v L i = x ∧ lvB w L i = y
  | p :: L, x, y, m => by
    cases m with
    | head => exact ⟨0, Nat.zero_lt_succ _, rfl, rfl⟩
    | tail _ m' => match pos v w L x y m' with
      | ⟨i, hi, e1, e2⟩ => exact ⟨i + 1, Nat.succ_lt_succ hi, e1, e2⟩

theorem idx (v : Nat → A) (w : Nat → B) (L : List (A × B)) : (L' : List (A × B)) → (∀ q, q ∈ L' → q ∈ L) →
    ∃ l : List Nat, lenN l = lenB L' ∧ (∀ i, i ∈ l → i < lenB L) ∧ mapD (lvA v L) l = fstL L' ∧ mapD (lvB w L) l = sndL L'
  | [], _ => ⟨[], rfl, (fun _ m => nomatch m), rfl, rfl⟩
  | q :: L', h => match pos v w L q.1 q.2 (h q (.head _)), idx v w L L' (fun x m => h x (.tail _ m)) with
    | ⟨i, hi, e1, e2⟩, ⟨l, hl, hlt, f1, f2⟩ => ⟨i :: l, congrArg (· + 1) hl, fun j m => by
        cases m with
        | head => exact hi
        | tail _ m' => exact hlt j m',
      by show lvA v L i :: mapD (lvA v L) l = q.1 :: fstL L'; rw [e1, f1],
      by show lvB w L i :: mapD (lvB w L) l = q.2 :: sndL L'; rw [e2, f2]⟩

theorem two_inj : (q q' : Nat) → 2 * q = 2 * q' → q = q'
  | 0, 0, _ => rfl
  | 0, _ + 1, h => Nat.noConfusion h
  | _ + 1, 0, h => Nat.noConfusion h
  | q + 1, q' + 1, h => congrArg (· + 1) (two_inj q q' (Nat.succ.inj (Nat.succ.inj h)))

theorem two_ne : (q q' : Nat) → 2 * q = 2 * q' + 1 → False
  | 0, 0, h => Nat.noConfusion h
  | 0, _ + 1, h => Nat.noConfusion h
  | _ + 1, 0, h => Nat.noConfusion (Nat.succ.inj h)
  | q + 1, q' + 1, h => two_ne q q' (Nat.succ.inj (Nat.succ.inj h))

theorem lmap_inj {X Y : Type} (f : X → Y) (hf : ∀ x x', f x = f x' → x = x') :
    (l l' : List X) → lmap f l = lmap f l' → l = l'
  | [], [], _ => rfl
  | [], _ :: _, h => by cases h
  | _ :: _, [], h => by cases h
  | x :: l, x' :: l', h => have e := List.cons.inj h
      by rw [hf x x' e.1, lmap_inj f hf l l' e.2]

theorem rcA (q : Nat) (xs : List A) : Rc RA RB τ n v w (cA q) (lmap CU.a xs) ↔ RA q xs :=
  ⟨fun h => h.elim (fun ⟨q', xs', e1, e2, r⟩ => by
      have eq := two_inj q q' e1
      have ex := lmap_inj CU.a (fun _ _ e => CU.a.inj e) xs xs' e2
      rw [eq, ex]; exact r)
    (fun ⟨q', _, e1, _, _⟩ => (two_ne q q' e1).elim),
   fun r => .inl ⟨q, xs, rfl, rfl, r⟩⟩

theorem rcB (q : Nat) (ys : List B) : Rc RA RB τ n v w (cB q) (lmap CU.b ys) ↔ RB q ys :=
  ⟨fun h => h.elim (fun ⟨q', _, e1, _, _⟩ => (two_ne q' q e1.symm).elim)
    (fun ⟨q', ys', e1, e2, r⟩ => by
      have eq := two_inj q q' (Nat.succ.inj e1)
      have ey := lmap_inj CU.b (fun _ _ e => CU.b.inj e) ys ys' e2
      rw [eq, ey]; exact r),
   fun r => .inr ⟨q, ys, rfl, rfl, r⟩⟩

theorem inj1 {X : Type} {x x' : X} (h : [x] = [x']) : x = x' := (List.cons.inj h).1
theorem inj2 {X : Type} {x y x' y' : X} (h : [x, y] = [x', y']) : x = x' ∧ y = y' :=
  ⟨(List.cons.inj h).1, (List.cons.inj (List.cons.inj h).2).1⟩
theorem inj3 {X : Type} {x y z x' y' z' : X} (h : [x, y, z] = [x', y', z']) : x = x' ∧ y = y' ∧ z = z' :=
  ⟨(List.cons.inj h).1, (List.cons.inj (List.cons.inj h).2).1, (List.cons.inj (List.cons.inj (List.cons.inj h).2).2).1⟩

section Th

variable (hA : Normal RA) (hB : Normal RB) (h02 : (0, 2) ∈ τ) (hE : EQ RA RB τ n 0 v w)

include hE in
theorem sT1 (ρ : Nat → CU A B) : Sat (Rc RA RB τ n v w) T1 ρ := by
  intro u ⟨m, hm, hmn⟩
  have hu : u = CU.lv m := inj1 hm
  subst hu
  have g : good RA RB τ v w m [] := eq_of_le RA RB τ hmn hE
  exact ⟨CU.cd m [], ⟨m, [], rfl, hmn, g⟩, ⟨m, [], rfl, hmn, g⟩⟩

theorem sT2 (ρ : Nat → CU A B) : Sat (Rc RA RB τ n v w) T2 ρ := by
  intro p u u' ⟨⟨m, L, e6, hmn, g⟩, ⟨m', e4, hm'⟩⟩ x ⟨a, ex⟩
  have hp : p = CU.cd m L := (inj2 e6).1
  have hu : u = CU.lv m := (inj2 e6).2
  have hu' : u' = CU.lv m' := (inj2 e4).1
  have hu2 : u = CU.lv (m' + 1) := (inj2 e4).2
  have hx : x = CU.a a := inj1 ex
  have hmm : m = m' + 1 := CU.lv.inj (hu.symm.trans hu2)
  subst hp hu' hx hmm
  match g.2.1 a with
  | ⟨b, hb⟩ =>
  refine ⟨CU.cd m' ((a, b) :: L), ⟨m', (a, b) :: L, rfl, Nat.le_of_succ_le hm', hb⟩, ?_,
    CU.b b, ⟨b, rfl⟩, ⟨m', (a, b) :: L, a, b, rfl, Nat.le_of_succ_le hm', hb, .head _⟩⟩
  intro x' y' ⟨m2, L2, x0, y0, e7, _, _, mem⟩
  have e7' := inj3 e7
  have ep : CU.cd (m' + 1) L = CU.cd m2 L2 := e7'.1
  have hx' : x' = CU.a x0 := e7'.2.1
  have hy' : y' = CU.b y0 := e7'.2.2
  have eL : L = L2 := (CU.cd.inj ep).2
  subst hx' hy'
  rw [← eL] at mem
  exact ⟨m', (a, b) :: L, x0, y0, rfl, Nat.le_of_succ_le hm', hb, .tail _ mem⟩

theorem sT3 (ρ : Nat → CU A B) : Sat (Rc RA RB τ n v w) T3 ρ := by
  intro p u u' ⟨⟨m, L, e6, hmn, g⟩, ⟨m', e4, hm'⟩⟩ y ⟨b, ey⟩
  have hp : p = CU.cd m L := (inj2 e6).1
  have hu : u = CU.lv m := (inj2 e6).2
  have hu' : u' = CU.lv m' := (inj2 e4).1
  have hu2 : u = CU.lv (m' + 1) := (inj2 e4).2
  have hy : y = CU.b b := inj1 ey
  have hmm : m = m' + 1 := CU.lv.inj (hu.symm.trans hu2)
  subst hp hu' hy hmm
  match g.2.2 b with
  | ⟨a, ha⟩ =>
  refine ⟨CU.cd m' ((a, b) :: L), ⟨m', (a, b) :: L, rfl, Nat.le_of_succ_le hm', ha⟩, ?_,
    CU.a a, ⟨a, rfl⟩, ⟨m', (a, b) :: L, a, b, rfl, Nat.le_of_succ_le hm', ha, .head _⟩⟩
  intro x' y' ⟨m2, L2, x0, y0, e7, _, _, mem⟩
  have e7' := inj3 e7
  have ep : CU.cd (m' + 1) L = CU.cd m2 L2 := e7'.1
  have hx' : x' = CU.a x0 := e7'.2.1
  have hy' : y' = CU.b y0 := e7'.2.2
  have eL : L = L2 := (CU.cd.inj ep).2
  subst hx' hy'
  rw [← eL] at mem
  exact ⟨m', (a, b) :: L, x0, y0, rfl, Nat.le_of_succ_le hm', ha, .tail _ mem⟩

include hA hB h02 in
theorem sT4 (ρ : Nat → CU A B) : Sat (Rc RA RB τ n v w) T4 ρ := by
  intro p x y x' y' ⟨⟨m, L, a, b, e1, _, g, mem1⟩, ⟨m2, L2, a', b', e2, _, _, mem2⟩⟩
  have e1' := inj3 e1
  have e2' := inj3 e2
  have hp1 : p = CU.cd m L := e1'.1
  have hp2 : p = CU.cd m2 L2 := e2'.1
  have hx : x = CU.a a := e1'.2.1
  have hy : y = CU.b b := e1'.2.2
  have hx' : x' = CU.a a' := e2'.2.1
  have hy' : y' = CU.b b' := e2'.2.2
  have eL : L2 = L := (CU.cd.inj (hp2.symm.trans hp1)).2
  subst hx hy hx' hy'
  rw [eL] at mem2
  match pos v w L a b mem1, pos v w L a' b' mem2 with
  | ⟨i, hi, ai, bi⟩, ⟨j, hj, aj, bj⟩ =>
  have at0 := eq_atm RA RB τ m (lenB L) g 0 [i, j] ⟨h02, fun q hq => by
    cases hq with
    | head => exact hi
    | tail _ hq' => cases hq' with
      | head => exact hj
      | tail _ h'' => exact nomatch h''⟩
  have at1 : RA 0 [a, a'] ↔ RB 0 [b, b'] := by
    have h := at0
    have e1a : mapD (lvA v L) [i, j] = [a, a'] := by show [lvA v L i, lvA v L j] = [a, a']; rw [ai, aj]
    have e1b : mapD (lvB w L) [i, j] = [b, b'] := by show [lvB w L i, lvB w L j] = [b, b']; rw [bi, bj]
    rw [e1a, e1b] at h
    exact h
  have ab : a = a' ↔ b = b' := ((hA a a').symm.trans at1).trans (hB b b')
  show ((∃ z, [CU.a a, CU.a a'] = [z, z]) → (∃ z, [(CU.b b : CU A B), CU.b b'] = [z, z])) ∧
    ((∃ z, [(CU.b b : CU A B), CU.b b'] = [z, z]) → (∃ z, [(CU.a a : CU A B), CU.a a'] = [z, z]))
  refine ⟨fun ⟨z, hz⟩ => ?_, fun ⟨z, hz⟩ => ?_⟩
  · have hz' := inj2 hz
    have ebb := ab.1 (CU.a.inj (hz'.1.trans hz'.2.symm))
    exact ⟨CU.b b, by rw [ebb]⟩
  · have hz' := inj2 hz
    have eaa := ab.2 (CU.b.inj (hz'.1.trans hz'.2.symm))
    exact ⟨CU.a a, by rw [eaa]⟩

theorem sT6 (ρ : Nat → CU A B) : Sat (Rc RA RB τ n v w) T6 ρ := by
  intro p x y ⟨m, L, a, b, e7, _, _, _⟩
  have hx : x = CU.a a := (inj3 e7).2.1
  have hy : y = CU.b b := (inj3 e7).2.2
  subst hx hy
  exact ⟨⟨a, rfl⟩, ⟨b, rfl⟩⟩

theorem sT8 (ρ : Nat → CU A B) : Sat (Rc RA RB τ n v w) T8 ρ := by
  intro u a b ⟨⟨m1, e1, _⟩, ⟨m2, e2, _⟩⟩
  have ha : a = CU.lv m1 := (inj2 e1).1
  have hb : b = CU.lv m2 := (inj2 e2).1
  have hu1 : u = CU.lv (m1 + 1) := (inj2 e1).2
  have hu2 : u = CU.lv (m2 + 1) := (inj2 e2).2
  have em' : m1 = m2 := Nat.succ.inj (CU.lv.inj (hu1.symm.trans hu2))
  subst ha hb em'
  exact ⟨CU.lv m1, rfl⟩

theorem sT9 (ρ : Nat → CU A B) : Sat (Rc RA RB τ n v w) T9 ρ := by
  intro c c' ⟨e1, e2⟩
  have h1 : c = CU.lv n := inj1 e1
  have h2 : c' = CU.lv n := inj1 e2
  subst h1 h2
  exact ⟨CU.lv n, rfl⟩

theorem sNA (ρ : Nat → CU A B) : Sat (Rc RA RB τ n v w) NA ρ := ⟨CU.a (v 0), v 0, rfl⟩
theorem sNB (ρ : Nat → CU A B) : Sat (Rc RA RB τ n v w) NB ρ := ⟨CU.b (w 0), w 0, rfl⟩

theorem unwrap (m : Nat) (L : List (A × B)) (p : CU A B) (hp : p = CU.cd m L) : (l : List (CU A B × CU A B)) →
    (∀ q, q ∈ l → Rc RA RB τ n v w 7 [p, q.1, q.2]) →
    ∃ L' : List (A × B), (∀ q, q ∈ L' → q ∈ L) ∧ fstL l = lmap CU.a (fstL L') ∧ sndL l = lmap CU.b (sndL L')
  | [], _ => ⟨[], (fun _ m => nomatch m), rfl, rfl⟩
  | q :: l, h => match h q (.head _), unwrap m L p hp l (fun x m => h x (.tail _ m)) with
    | ⟨m2, L2, a, b, e7, _, _, mem⟩, ⟨L', sub, f1, f2⟩ => by
      have e7' := inj3 e7
      have eL : L2 = L := (CU.cd.inj (e7'.1.symm.trans hp)).2
      rw [eL] at mem
      have h1 : q.1 = CU.a a := e7'.2.1
      have h2 : q.2 = CU.b b := e7'.2.2
      refine ⟨(a, b) :: L', fun x mx => ?_, ?_, ?_⟩
      · cases mx with
        | head => exact mem
        | tail _ m' => exact sub x m'
      · show q.1 :: fstL l = CU.a a :: lmap CU.a (fstL L'); rw [h1, f1]
      · show q.2 :: sndL l = CU.b b :: lmap CU.b (sndL L'); rw [h2, f2]

include hE in
theorem sPres (r ar : Nat) (hτ : (r, ar) ∈ τ) (ρ : Nat → CU A B) :
    Sat (Rc RA RB τ n v w) (presF 7 (cA r) (cB r) ar) ρ := by
  refine (sat_presL _ 7 (cA r) (cB r) ar ρ).2 (fun p l el hl => ?_)
  match l, el, hl with
  | [], el, _ =>
    have e0 : ar = 0 := el.symm
    rw [e0] at hτ
    have at0 := eq_atm RA RB τ n 0 hE r [] ⟨hτ, fun _ m => nomatch m⟩
    exact (rcA RA RB τ n v w r []).trans (at0.trans (rcB RA RB τ n v w r []).symm)
  | q0 :: l', el, hl =>
    match hl q0 (.head _) with
    | ⟨m, L, a, b, e7, _, g, _⟩ =>
    have hp : p = CU.cd m L := (inj3 e7).1
    match unwrap RA RB τ n v w m L p hp (q0 :: l') hl with
    | ⟨L', sub, f1, f2⟩ =>
    match idx v w L L' sub with
    | ⟨ix, hlen, hlt, m1, m2⟩ =>
    have hlenL : lenB L' = ar := by
      have a1 : lenB (fstL (q0 :: l')) = lenB (lmap (CU.a (B := B)) (fstL L')) := by rw [f1]
      rw [len_fst, len_lmap, len_fst] at a1
      exact a1.symm.trans el
    have at1 := eq_atm RA RB τ m (lenB L) g r ix ⟨by rw [hlen, hlenL]; exact hτ, hlt⟩
    rw [m1, m2] at at1
    rw [f1, f2]
    exact (rcA RA RB τ n v w r _).trans (at1.trans (rcB RA RB τ n v w r _).symm)

theorem chain_at : (k j : Nat) → k ≤ j → j ≤ n → ∀ ρ : Nat → CU A B,
    Sat (Rc RA RB τ n v w) (chainF k) (ext ρ (CU.lv j))
  | 0, _, _, _, _ => fun h => h
  | k + 1, 0, h, _, _ => (Nat.not_succ_le_zero k h).elim
  | k + 1, j + 1, h, hj, _ => ⟨CU.lv j, ⟨j, rfl, hj⟩, ⟨j, rfl, Nat.le_of_succ_le hj⟩,
      chain_at k j (Nat.le_of_succ_le_succ h) (Nat.le_of_succ_le hj) _⟩

theorem sCh (k : Nat) (hk : k ≤ n) (ρ : Nat → CU A B) : Sat (Rc RA RB τ n v w) (Ch k) ρ :=
  ⟨CU.lv n, rfl, ⟨n, rfl, Nat.le_refl n⟩, chain_at RA RB τ n v w k n hk (Nat.le_refl n) ρ⟩

include hA hB h02 hE in
theorem sTheta (ρ : Nat → CU A B) : Sat (Rc RA RB τ n v w) (theta τ) ρ :=
  (sat_conjL _ _ ρ).2 (fun x hx => (mem_appL _ _ hx).elim
    (fun h => by
      cases h with
      | head => exact sT1 RA RB τ n v w hE ρ
      | tail _ h => cases h with
        | head => exact sT2 RA RB τ n v w ρ
        | tail _ h => cases h with
          | head => exact sT3 RA RB τ n v w ρ
          | tail _ h => cases h with
            | head => exact sT4 RA RB τ n v w hA hB h02 ρ
            | tail _ h => cases h with
              | head => exact sT6 RA RB τ n v w ρ
              | tail _ h => cases h with
                | head => exact sT8 RA RB τ n v w ρ
                | tail _ h => cases h with
                  | head => exact sT9 RA RB τ n v w ρ
                  | tail _ h => cases h with
                    | head => exact sNA RA RB τ n v w ρ
                    | tail _ h => cases h with
                      | head => exact sNB RA RB τ n v w ρ
                      | tail _ h => exact nomatch h)
    (fun h => match mem_lmap _ h with
      | ⟨q, hq, e⟩ => by rw [e]; exact sPres RA RB τ n v w hE q.1 q.2 hq ρ))

end Th

end C

end Lind

end Found

#print axioms Found.Lind.Rc
#print axioms Found.Lind.rc_normal
#print axioms Found.Lind.eq_of_le
#print axioms Found.Lind.idx
#print axioms Found.Lind.rcA
#print axioms Found.Lind.rcB
#print axioms Found.Lind.sT1
#print axioms Found.Lind.sT2
#print axioms Found.Lind.sT3
#print axioms Found.Lind.sT4
#print axioms Found.Lind.sT6
#print axioms Found.Lind.sT8
#print axioms Found.Lind.sT9
#print axioms Found.Lind.sPres
#print axioms Found.Lind.sCh
#print axioms Found.Lind.sTheta

found/Lin9.lean

lean_all/found/Lin9.lean

/-
  T190 (Lindström), part 9. found/Lin9 — inside C_n, the substructure on
  the A-elements with the copied symbols is isomorphic to (A, RA), and the
  one on the B-elements to (B, RB); isomorphisms can be inverted.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Lin8

namespace Found

namespace Lind

open FO FOCT NBGc

theorem lmap_comp {X Y Z : Type} (f : X → Y) (g : Y → Z) : (l : List X) → lmap g (lmap f l) = lmap (fun x => g (f x)) l
  | [] => rfl
  | x :: l => congrArg (List.cons (g (f x))) (lmap_comp f g l)

theorem lmap_id {X : Type} (f : X → X) (hf : ∀ x, f x = x) : (l : List X) → lmap f l = l
  | [] => rfl
  | x :: l => by show f x :: lmap f l = x :: l; rw [hf x, lmap_id f hf l]

/-- inverse isomorphism -/
def Iso.symm' {A B : Type} {RA : Nat → List A → Prop} {RB : Nat → List B → Prop} (i : Iso RA RB) : Iso RB RA where
  f := i.g
  g := i.f
  gf := i.fg
  fg := i.gf
  pres := fun r l => by
    have h := i.pres r (lmap i.g l)
    rw [lmap_comp, lmap_id _ i.fg] at h
    exact h.symm

section P

variable {A B : Type} (RA : Nat → List A → Prop) (RB : Nat → List B → Prop) (τ : List (Nat × Nat))
  (n : Nat) (v : Nat → A) (w : Nat → B)

def gA : CU A B → A
  | .a x => x
  | .b _ => v 0
  | .lv _ => v 0
  | .cd _ _ => v 0

def gB : CU A B → B
  | .a _ => w 0
  | .b y => y
  | .lv _ => w 0
  | .cd _ _ => w 0

/-- the A-part of C_n -/
def isoA : Iso RA (fun r l => Rsub (Rc RA RB τ n v w) 1 (cA r) l) where
  f := fun x => ⟨CU.a x, x, rfl⟩
  g := fun z => gA v z.val
  gf := fun _ => rfl
  fg := fun z => match z with
    | ⟨z0, x, e⟩ => by
      have h : z0 = CU.a x := inj1 e
      subst h
      rfl
  pres := fun r l => by
    show RA r l ↔ Rc RA RB τ n v w (cA r) (lmap Subtype.val (lmap (fun x => (⟨CU.a x, x, rfl⟩ : {z : CU A B // Rc RA RB τ n v w 1 [z]})) l))
    rw [lmap_comp]
    exact (rcA RA RB τ n v w r l).symm

/-- the B-part of C_n -/
def isoB : Iso RB (fun r l => Rsub (Rc RA RB τ n v w) 2 (cB r) l) where
  f := fun y => ⟨CU.b y, y, rfl⟩
  g := fun z => gB w z.val
  gf := fun _ => rfl
  fg := fun z => match z with
    | ⟨z0, y, e⟩ => by
      have h : z0 = CU.b y := inj1 e
      subst h
      rfl
  pres := fun r l => by
    show RB r l ↔ Rc RA RB τ n v w (cB r) (lmap Subtype.val (lmap (fun y => (⟨CU.b y, y, rfl⟩ : {z : CU A B // Rc RA RB τ n v w 2 [z]})) l))
    rw [lmap_comp]
    exact (rcB RA RB τ n v w r l).symm

end P

end Lind

end Found

#print axioms Found.Lind.Iso.symm'
#print axioms Found.Lind.isoA
#print axioms Found.Lind.isoB

found/Linear.lean

lean_all/found/Linear.lean

/-
  T68. found/Linear — intuitionistic linear logic as a condition.

  * Sequent calculus for intuitionistic linear logic (⊗, 1, ⊸, &, ⊕, 0, !)
    with list contexts and an explicit permutation rule.
  * Models: commutative residuated lattices with an exponential `!`
    (the poset shadow of a symmetric monoidal closed category with a
    comonad).  Soundness of the calculus.
  * Condition: a residuated lattice with weakening and contraction is a
    Heyting algebra (⊗ = ∧, ⊸ = →); conversely every Heyting algebra is a
    model with ! = id, so linear derivations erase to intuitionistic truths.
  * Girard: the co-Kleisli order a ≤! b :⟺ !a ≤ b of any model is a Heyting
    algebra with a → b := !a ⊸ b, a ∨ b := !a ⊕ !b (Seely); so every
    intuitionistic theorem is valid under the Girard translation.
  * Separation: Łukasiewicz three-valued algebra refutes contraction p ⊸ p ⊗ p.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Minimal


universe u

namespace Found

namespace Lin

inductive LForm where
  | var : Nat → LForm
  | one : LForm
  | zero : LForm
  | tens : LForm → LForm → LForm
  | lolli : LForm → LForm → LForm
  | wth : LForm → LForm → LForm
  | pls : LForm → LForm → LForm
  | bang : LForm → LForm

inductive Perm : List LForm → List LForm → Prop where
  | nil : Perm [] []
  | cons (x : LForm) {l₁ l₂ : List LForm} : Perm l₁ l₂ → Perm (x :: l₁) (x :: l₂)
  | swap (x y : LForm) (l : List LForm) : Perm (x :: y :: l) (y :: x :: l)
  | trans {l₁ l₂ l₃ : List LForm} : Perm l₁ l₂ → Perm l₂ l₃ → Perm l₁ l₃

inductive LD : List LForm → LForm → Prop where
  | ax (a : LForm) : LD [a] a
  | perm {Γ Δ c} : Perm Γ Δ → LD Γ c → LD Δ c
  | cut {Γ Δ a c} : LD Γ a → LD (a :: Δ) c → LD (Γ ++ Δ) c
  | tensR {Γ Δ a b} : LD Γ a → LD Δ b → LD (Γ ++ Δ) (.tens a b)
  | tensL {Γ a b c} : LD (a :: b :: Γ) c → LD (.tens a b :: Γ) c
  | oneR : LD [] .one
  | oneL {Γ c} : LD Γ c → LD (.one :: Γ) c
  | lolliR {Γ a b} : LD (a :: Γ) b → LD Γ (.lolli a b)
  | lolliL {Γ Δ a b c} : LD Γ a → LD (b :: Δ) c → LD (.lolli a b :: (Γ ++ Δ)) c
  | wthR {Γ a b} : LD Γ a → LD Γ b → LD Γ (.wth a b)
  | wthL1 {Γ a b c} : LD (a :: Γ) c → LD (.wth a b :: Γ) c
  | wthL2 {Γ a b c} : LD (b :: Γ) c → LD (.wth a b :: Γ) c
  | plsR1 {Γ a b} : LD Γ a → LD Γ (.pls a b)
  | plsR2 {Γ a b} : LD Γ b → LD Γ (.pls a b)
  | plsL {Γ a b c} : LD (a :: Γ) c → LD (b :: Γ) c → LD (.pls a b :: Γ) c
  | zeroL {Γ c} : LD (.zero :: Γ) c
  | derel {Γ a c} : LD (a :: Γ) c → LD (.bang a :: Γ) c
  | weak {Γ a c} : LD Γ c → LD (.bang a :: Γ) c
  | contr {Γ a c} : LD (.bang a :: .bang a :: Γ) c → LD (.bang a :: Γ) c
  | prom {Γ : List LForm} {a : LForm} : LD (Γ.map .bang) a → LD (Γ.map .bang) (.bang a)

/-! ## Models: commutative residuated lattices with an exponential -/

structure CRL where
  car : Type u
  le : car → car → Prop
  le_refl : ∀ a, le a a
  le_trans : ∀ {a b c}, le a b → le b c → le a c
  one : car
  tens : car → car → car
  lolli : car → car → car
  wth : car → car → car
  pls : car → car → car
  zero : car
  bang : car → car
  tens_mono : ∀ {a a' b b'}, le a a' → le b b' → le (tens a b) (tens a' b')
  tens_comm : ∀ a b, le (tens a b) (tens b a)
  assoc1 : ∀ a b c, le (tens (tens a b) c) (tens a (tens b c))
  assoc2 : ∀ a b c, le (tens a (tens b c)) (tens (tens a b) c)
  unit1 : ∀ a, le (tens one a) a
  unit2 : ∀ a, le a (tens one a)
  adj : ∀ {a b c}, le (tens a b) c ↔ le b (lolli a c)
  wth_l : ∀ a b, le (wth a b) a
  wth_r : ∀ a b, le (wth a b) b
  wth_univ : ∀ {a b c}, le c a → le c b → le c (wth a b)
  pls_l : ∀ a b, le a (pls a b)
  pls_r : ∀ a b, le b (pls a b)
  pls_univ : ∀ {a b c}, le a c → le b c → le (pls a b) c
  zero_le : ∀ a, le zero a
  bang_le : ∀ a, le (bang a) a
  bang_unit : ∀ a, le (bang a) one
  bang_dup : ∀ a, le (bang a) (tens (bang a) (bang a))

namespace CRL
variable (M : CRL)

def ctx : List M.car → M.car
  | [] => M.one
  | a :: l => M.tens a (ctx l)

/-- promotion: a product of banged elements below `a` is below `!a` -/
def PromOK : Prop := ∀ (l : List M.car) (a : M.car), M.le (M.ctx (l.map M.bang)) a →
  M.le (M.ctx (l.map M.bang)) (M.bang a)

theorem ev (a b : M.car) : M.le (M.tens (M.lolli a b) a) b :=
  M.le_trans (M.tens_comm _ _) (M.adj.mpr (M.le_refl _))

theorem swap (x y g : M.car) : M.le (M.tens x (M.tens y g)) (M.tens y (M.tens x g)) :=
  M.le_trans (M.assoc2 _ _ _) (M.le_trans (M.tens_mono (M.tens_comm x y) (M.le_refl g)) (M.assoc1 _ _ _))

theorem ctx_append : (l₁ l₂ : List M.car) →
    M.le (M.ctx (l₁ ++ l₂)) (M.tens (M.ctx l₁) (M.ctx l₂))
  | [], _ => M.unit2 _
  | a :: l, l₂ => M.le_trans (M.tens_mono (M.le_refl a) (ctx_append l l₂)) (M.assoc2 _ _ _)

end CRL

section Sound
variable (M : CRL) (v : Nat → M.car)

def interp : LForm → M.car
  | .var n => v n
  | .one => M.one
  | .zero => M.zero
  | .tens a b => M.tens (interp a) (interp b)
  | .lolli a b => M.lolli (interp a) (interp b)
  | .wth a b => M.wth (interp a) (interp b)
  | .pls a b => M.pls (interp a) (interp b)
  | .bang a => M.bang (interp a)

def cI (Γ : List LForm) : M.car := M.ctx (Γ.map (interp M v))

theorem perm_le {Γ Δ : List LForm} (p : Perm Γ Δ) : M.le (cI M v Γ) (cI M v Δ) := by
  induction p with
  | nil => exact M.le_refl _
  | cons x _ ih => exact M.tens_mono (M.le_refl _) ih
  | swap x y l => exact M.swap _ _ _
  | trans _ _ ih₁ ih₂ => exact M.le_trans ih₁ ih₂

theorem perm_ge {Γ Δ : List LForm} (p : Perm Γ Δ) : M.le (cI M v Δ) (cI M v Γ) := by
  induction p with
  | nil => exact M.le_refl _
  | cons x _ ih => exact M.tens_mono (M.le_refl _) ih
  | swap x y l => exact M.swap _ _ _
  | trans _ _ ih₁ ih₂ => exact M.le_trans ih₂ ih₁

theorem cI_append (Γ Δ : List LForm) :
    M.le (cI M v (Γ ++ Δ)) (M.tens (cI M v Γ) (cI M v Δ)) := by
  have e : (Γ ++ Δ).map (interp M v) = Γ.map (interp M v) ++ Δ.map (interp M v) :=
    map_append_eq Γ Δ
  unfold cI
  rw [e]
  exact M.ctx_append _ _
where
  map_append_eq : (Γ Δ : List LForm) →
      (Γ ++ Δ).map (interp M v) = Γ.map (interp M v) ++ Δ.map (interp M v)
    | [], _ => rfl
    | a :: l, Δ => congrArg (List.cons (interp M v a)) (map_append_eq l Δ)

theorem cI_map_bang (Γ : List LForm) :
    cI M v (Γ.map .bang) = M.ctx ((Γ.map (interp M v)).map M.bang) := by
  induction Γ with
  | nil => rfl
  | cons a l ih => exact congrArg (M.tens (M.bang (interp M v a))) ih

/-- soundness of linear sequents: ⊗Γ ≤ c -/
theorem soundness (hP : M.PromOK) {Γ : List LForm} {c : LForm} (d : LD Γ c) :
    M.le (cI M v Γ) (interp M v c) := by
  induction d with
  | ax a => exact M.le_trans (M.tens_comm _ _) (M.unit1 _)
  | perm p _ ih => exact M.le_trans (perm_ge M v p) ih
  | cut _ _ ih₁ ih₂ => exact M.le_trans (cI_append M v _ _) (M.le_trans (M.tens_mono ih₁ (M.le_refl _)) ih₂)
  | tensR _ _ ih₁ ih₂ => exact M.le_trans (cI_append M v _ _) (M.tens_mono ih₁ ih₂)
  | tensL _ ih => exact M.le_trans (M.assoc1 _ _ _) ih
  | oneR => exact M.le_refl _
  | oneL _ ih => exact M.le_trans (M.unit1 _) ih
  | lolliR _ ih => exact M.adj.mp ih
  | lolliL _ _ ih₁ ih₂ =>
    exact M.le_trans (M.tens_mono (M.le_refl _) (cI_append M v _ _))
      (M.le_trans (M.assoc2 _ _ _)
      (M.le_trans (M.tens_mono (M.le_trans (M.tens_mono (M.le_refl _) ih₁) (M.ev _ _)) (M.le_refl _)) ih₂))
  | wthR _ _ ih₁ ih₂ => exact M.wth_univ ih₁ ih₂
  | wthL1 _ ih => exact M.le_trans (M.tens_mono (M.wth_l _ _) (M.le_refl _)) ih
  | wthL2 _ ih => exact M.le_trans (M.tens_mono (M.wth_r _ _) (M.le_refl _)) ih
  | plsR1 _ ih => exact M.le_trans ih (M.pls_l _ _)
  | plsR2 _ ih => exact M.le_trans ih (M.pls_r _ _)
  | plsL _ _ ih₁ ih₂ =>
    exact M.le_trans (M.tens_comm _ _) (M.adj.mpr (M.pls_univ
      (M.adj.mp (M.le_trans (M.tens_comm _ _) ih₁)) (M.adj.mp (M.le_trans (M.tens_comm _ _) ih₂))))
  | zeroL => exact M.le_trans (M.tens_comm _ _) (M.adj.mpr (M.zero_le _))
  | derel _ ih => exact M.le_trans (M.tens_mono (M.bang_le _) (M.le_refl _)) ih
  | weak _ ih => exact M.le_trans (M.tens_mono (M.bang_unit _) (M.le_refl _)) (M.le_trans (M.unit1 _) ih)
  | contr _ ih =>
    exact M.le_trans (M.tens_mono (M.bang_dup _) (M.le_refl _)) (M.le_trans (M.assoc1 _ _ _) ih)
  | @prom Γ _ _ ih =>
    exact (cI_map_bang M v Γ) ▸ hP _ _ ((cI_map_bang M v Γ) ▸ ih)

end Sound

/-! ## Condition: weakening + contraction turn a model into a Heyting algebra -/

section ToHA
variable (M : CRL) (wk : ∀ a b, M.le (M.tens a b) a) (ct : ∀ a, M.le a (M.tens a a))

include wk in
theorem tens_le_wth (a b : M.car) : M.le (M.tens a b) (M.wth a b) :=
  M.wth_univ (wk a b) (M.le_trans (M.tens_comm a b) (wk b a))

include ct in
theorem wth_le_tens (a b : M.car) : M.le (M.wth a b) (M.tens a b) :=
  M.le_trans (ct _) (M.tens_mono (M.wth_l a b) (M.wth_r a b))

include wk ct in
/-- with weakening and contraction, a residuated lattice is a Heyting algebra -/
def toHA : HA.{u} where
  car := M.car
  le := M.le
  le_refl := M.le_refl
  le_trans := M.le_trans
  top := M.one
  bot := M.zero
  meet := M.wth
  join := M.pls
  imp := M.lolli
  le_top a := M.le_trans (M.unit2 a) (wk _ _)
  bot_le := M.zero_le
  meet_l := M.wth_l
  meet_r := M.wth_r
  meet_univ := M.wth_univ
  join_l := M.pls_l
  join_r := M.pls_r
  join_univ := M.pls_univ
  imp_adj := ⟨fun h => M.adj.mp (M.le_trans (M.tens_comm _ _) (M.le_trans (tens_le_wth M wk _ _) h)),
              fun h => M.le_trans (wth_le_tens M ct _ _) (M.le_trans (M.tens_comm _ _) (M.adj.mpr h))⟩

end ToHA

/-- every Heyting algebra is a linear model with ⊗ = ∧ and ! = id -/
def ofHA (H : HA.{u}) : CRL.{u} where
  car := H.car
  le := H.le
  le_refl := H.le_refl
  le_trans := H.le_trans
  one := H.top
  tens := H.meet
  lolli := H.imp
  wth := H.meet
  pls := H.join
  zero := H.bot
  bang a := a
  tens_mono h k := H.meet_univ (H.le_trans (H.meet_l _ _) h) (H.le_trans (H.meet_r _ _) k)
  tens_comm _ _ := H.meet_univ (H.meet_r _ _) (H.meet_l _ _)
  assoc1 _ _ _ := H.meet_univ (H.le_trans (H.meet_l _ _) (H.meet_l _ _))
    (H.meet_univ (H.le_trans (H.meet_l _ _) (H.meet_r _ _)) (H.meet_r _ _))
  assoc2 _ _ _ := H.meet_univ (H.meet_univ (H.meet_l _ _) (H.le_trans (H.meet_r _ _) (H.meet_l _ _)))
    (H.le_trans (H.meet_r _ _) (H.meet_r _ _))
  unit1 _ := H.meet_r _ _
  unit2 a := H.meet_univ (H.le_top a) (H.le_refl a)
  adj := ⟨fun h => H.imp_adj.mp (H.le_trans (H.meet_univ (H.meet_r _ _) (H.meet_l _ _)) h),
          fun h => H.le_trans (H.meet_univ (H.meet_r _ _) (H.meet_l _ _)) (H.imp_adj.mpr h)⟩
  wth_l := H.meet_l
  wth_r := H.meet_r
  wth_univ := H.meet_univ
  pls_l := H.join_l
  pls_r := H.join_r
  pls_univ := H.join_univ
  zero_le := H.bot_le
  bang_le a := H.le_refl a
  bang_unit a := H.le_top a
  bang_dup a := H.meet_univ (H.le_refl a) (H.le_refl a)

theorem ofHA_prom (H : HA.{u}) : (ofHA H).PromOK := fun _ _ h => h

/-! ## Girard: the co-Kleisli order of `!` is a Heyting algebra -/

section Girard
variable (M : CRL) (hP : M.PromOK)

include hP in
theorem bang_mono {a b : M.car} (h : M.le (M.bang a) b) : M.le (M.bang a) (M.bang b) :=
  M.le_trans (M.le_trans (M.unit2 _) (M.tens_comm _ _))
    (hP [a] b (M.le_trans (M.le_trans (M.tens_comm _ _) (M.unit1 _)) h))

include hP in
theorem seely1 (c a : M.car) : M.le (M.tens (M.bang c) (M.bang a)) (M.bang (M.wth c a)) := by
  have k : M.le (M.ctx ([c, a].map M.bang)) (M.wth c a) :=
    M.wth_univ
      (M.le_trans (M.tens_mono (M.bang_le c) (M.le_trans (M.tens_mono (M.bang_unit a) (M.le_refl _)) (M.unit1 _)))
        (M.le_trans (M.tens_comm _ _) (M.unit1 _)))
      (M.le_trans (M.tens_mono (M.bang_unit c) (M.le_trans (M.tens_mono (M.bang_le a) (M.le_refl _))
          (M.le_trans (M.tens_comm _ _) (M.unit1 _)))) (M.unit1 _))
  exact M.le_trans (M.tens_mono (M.le_refl _) (M.le_trans (M.unit2 _) (M.tens_comm _ _)))
    (hP [c, a] _ k)

include hP in
theorem seely2 (c a : M.car) : M.le (M.bang (M.wth c a)) (M.tens (M.bang c) (M.bang a)) :=
  M.le_trans (M.bang_dup _)
    (M.tens_mono (bang_mono M hP (M.le_trans (M.bang_le _) (M.wth_l _ _)))
                 (bang_mono M hP (M.le_trans (M.bang_le _) (M.wth_r _ _))))

include hP in
/-- the co-Kleisli Heyting algebra: a ≤! b ⟺ !a ≤ b -/
def coKleisli : HA.{u} where
  car := M.car
  le a b := M.le (M.bang a) b
  le_refl a := M.bang_le a
  le_trans h k := M.le_trans (bang_mono M hP h) k
  top := M.one
  bot := M.zero
  meet := M.wth
  join a b := M.pls (M.bang a) (M.bang b)
  imp a b := M.lolli (M.bang a) b
  le_top a := M.bang_unit a
  bot_le a := M.le_trans (M.bang_le _) (M.zero_le a)
  meet_l a b := M.le_trans (M.bang_le _) (M.wth_l a b)
  meet_r a b := M.le_trans (M.bang_le _) (M.wth_r a b)
  meet_univ h k := M.wth_univ h k
  join_l _ _ := M.pls_l _ _
  join_r _ _ := M.pls_r _ _
  join_univ h k := M.le_trans (M.bang_le _) (M.pls_univ h k)
  imp_adj := ⟨fun h => M.adj.mp (M.le_trans (M.tens_comm _ _) (M.le_trans (seely1 M hP _ _) h)),
              fun h => M.le_trans (seely2 M hP _ _) (M.le_trans (M.tens_comm _ _) (M.adj.mpr h))⟩

theorem cong2 {α β γ : Sort _} (f : α → β → γ) {a a' : α} {b b' : β} (h1 : a = a') (h2 : b = b') :
    f a b = f a' b' := by
  cases h1
  cases h2
  rfl

/-- the Girard translation into linear formulas -/
def girard : Prp.Form → LForm
  | .var n => .var n
  | .bot => .zero
  | .and a b => .wth (girard a) (girard b)
  | .or a b => .pls (.bang (girard a)) (.bang (girard b))
  | .imp a b => .lolli (.bang (girard a)) (girard b)

theorem girard_interp (v : Nat → M.car) : (φ : Prp.Form) →
    Prp.interp (coKleisli M hP) v M.zero φ = interp M v (girard φ)
  | .var _ => rfl
  | .bot => rfl
  | .and a b => cong2 M.wth (girard_interp v a) (girard_interp v b)
  | .or a b => cong2 (fun x y => M.pls (M.bang x) (M.bang y)) (girard_interp v a) (girard_interp v b)
  | .imp a b => cong2 (fun x y => M.lolli (M.bang x) y) (girard_interp v a) (girard_interp v b)

include hP in
/-- every intuitionistic theorem is valid under the Girard translation: !1 ≤ ⟦φ*⟧ -/
theorem girard_valid {φ : Prp.Form} (d : Prp.PD true (fun _ => False) φ) (v : Nat → M.car) :
    M.le (M.bang M.one) (interp M v (girard φ)) :=
  girard_interp M hP v φ ▸
    Prp.valid (coKleisli M hP) d v M.zero (fun _ x => M.le_trans (M.bang_le _) (M.zero_le x))

end Girard

/-! ## Łukasiewicz three-valued model: contraction fails -/

inductive L3 where
  | z : L3
  | h : L3
  | o : L3

namespace L3

def rk : L3 → Nat
  | z => 0
  | h => 1
  | o => 2

def le (a b : L3) : Prop := Nat.ble (rk a) (rk b) = true

def tens : L3 → L3 → L3
  | z, z => z
  | z, h => z
  | z, o => z
  | h, z => z
  | h, h => z
  | h, o => h
  | o, z => z
  | o, h => h
  | o, o => o

def lolli : L3 → L3 → L3
  | z, z => o
  | z, h => o
  | z, o => o
  | h, z => h
  | h, h => o
  | h, o => o
  | o, z => z
  | o, h => h
  | o, o => o

def wth : L3 → L3 → L3
  | z, z => z
  | z, h => z
  | z, o => z
  | h, z => z
  | h, h => h
  | h, o => h
  | o, z => z
  | o, h => h
  | o, o => o

def pls : L3 → L3 → L3
  | z, z => z
  | z, h => h
  | z, o => o
  | h, z => h
  | h, h => h
  | h, o => o
  | o, z => o
  | o, h => o
  | o, o => o

def bang : L3 → L3
  | z => z
  | h => z
  | o => o

def M3 : CRL where
  car := L3
  le := le
  le_refl a := by cases a <;> rfl
  le_trans {a b c} := by cases a <;> cases b <;> cases c <;> intro p q <;> first | rfl | exact p | exact q
  one := o
  tens := tens
  lolli := lolli
  wth := wth
  pls := pls
  zero := z
  bang := bang
  tens_mono {a a' b b'} := by
    cases a <;> cases a' <;> cases b <;> cases b' <;> intro p q <;> first | rfl | exact p | exact q
  tens_comm a b := by cases a <;> cases b <;> rfl
  assoc1 a b c := by cases a <;> cases b <;> cases c <;> rfl
  assoc2 a b c := by cases a <;> cases b <;> cases c <;> rfl
  unit1 a := by cases a <;> rfl
  unit2 a := by cases a <;> rfl
  adj {a b c} := by
    cases a <;> cases b <;> cases c <;> exact ⟨fun p => p, fun p => p⟩
  wth_l a b := by cases a <;> cases b <;> rfl
  wth_r a b := by cases a <;> cases b <;> rfl
  wth_univ {a b c} := by cases a <;> cases b <;> cases c <;> intro p q <;> first | rfl | exact p | exact q
  pls_l a b := by cases a <;> cases b <;> rfl
  pls_r a b := by cases a <;> cases b <;> rfl
  pls_univ {a b c} := by cases a <;> cases b <;> cases c <;> intro p q <;> first | rfl | exact p | exact q
  zero_le a := by cases a <;> rfl
  bang_le a := by cases a <;> rfl
  bang_unit a := by cases a <;> rfl
  bang_dup a := by cases a <;> rfl

theorem tens_z (c : L3) : tens z c = z := by cases c <;> rfl

theorem tens_o (c : L3) : tens o c = c := by cases c <;> rfl

/-- a product of banged elements is `o` or `z` -/
theorem ctx_bang : (l : List L3) → M3.ctx (l.map bang) = o ∨ M3.ctx (l.map bang) = z
  | [] => Or.inl rfl
  | z :: _ => Or.inr (tens_z _)
  | h :: _ => Or.inr (tens_z _)
  | o :: l => match ctx_bang l with
    | Or.inl e => Or.inl ((tens_o _).trans e)
    | Or.inr e => Or.inr ((tens_o _).trans e)

theorem key : (c a : L3) → (c = o ∨ c = z) → le c a → le c (bang a)
  | o, o, _, _ => rfl
  | o, h, _, k => k
  | o, z, _, k => k
  | z, _, _, _ => rfl
  | h, _, e, _ => (e.elim (fun q => L3.noConfusion q) (fun q => L3.noConfusion q))

theorem prom : M3.PromOK := fun l a k => key _ a (ctx_bang l) k

end L3

/-- contraction p ⊸ p ⊗ p is not derivable -/
theorem no_contraction : ¬ LD [] (.lolli (.var 0) (.tens (.var 0) (.var 0))) :=
  fun d => Bool.noConfusion (soundness L3.M3 (fun _ => L3.h) L3.prom d)

end Lin

end Found

#print axioms Found.Lin.CRL.ev
#print axioms Found.Lin.CRL.swap
#print axioms Found.Lin.CRL.ctx_append
#print axioms Found.Lin.perm_le
#print axioms Found.Lin.cI_append
#print axioms Found.Lin.soundness
#print axioms Found.Lin.tens_le_wth
#print axioms Found.Lin.wth_le_tens
#print axioms Found.Lin.toHA
#print axioms Found.Lin.ofHA
#print axioms Found.Lin.ofHA_prom
#print axioms Found.Lin.bang_mono
#print axioms Found.Lin.seely1
#print axioms Found.Lin.seely2
#print axioms Found.Lin.coKleisli
#print axioms Found.Lin.girard_interp
#print axioms Found.Lin.girard_valid
#print axioms Found.Lin.L3.M3
#print axioms Found.Lin.L3.prom
#print axioms Found.Lin.no_contraction

found/Lines.lean

lean_all/found/Lines.lean

/-
  T126. found/Lines — every proved point's general theorems re-exported in one file
  (generated by work/found/gen_lines.py from CHAIN.md; do not edit by hand).
  Lean 4 core only, no axioms.
-/
import found.AD
import found.AST
import found.Ack2
import found.AckD
import found.Ackermann
import found.AlgebraicLogic
import found.Automata
import found.Beth
import found.Bishop
import found.Brouwer
import found.CIC
import found.ChurchTuring2
import found.ChurchTuring3
import found.Coalgebra
import found.Compactness
import found.Completeness
import found.Condensed
import found.Conditional
import found.Core
import found.CwF
import found.DRSound
import found.DaCosta
import found.Dependence
import found.Description
import found.Dialectica
import found.Domain
import found.ETCC
import found.ETCS
import found.ETCSZF
import found.ETCSZF2
import found.ETCSZF3
import found.EffectiveK1
import found.Epsilon
import found.FOClassicalT
import found.FOCompact
import found.FOCompleteness
import found.Fagin
import found.Fibration
import found.Finitism
import found.FixpointLogic
import found.Frege
import found.GPK
import found.Game
import found.GeometricLogic
import found.GoedelBridge
import found.Grothendieck
import found.HAomega
import found.HOIL
import found.HOL
import found.Higher2
import found.Hilbert
import found.HoTTLines
import found.Homotopy
import found.IST
import found.InfN
import found.Institutions
import found.Justification
import found.KP
import found.KanExt
import found.Krivine
import found.LargeCardinals
import found.LocalUniverse
import found.MLTTVariants
import found.MacLane
import found.ManySorted
import found.Markov
import found.MartinAxiom
import found.Mereology
import found.MetaFrameworks
import found.ModalMu
import found.NFAlt
import found.NFAssemble
import found.NFCountStep
import found.NFLabel
import found.NFReduce
import found.NFStruct
import found.NFSupp
import found.Nominal
import found.NonMono
import found.OTT
import found.Ordinal
import found.PAEmbed
import found.PAHA
import found.PTS
import found.Plural
import found.Predicative
import found.Presburger
import found.Presentable
import found.ProbLogic
import found.Process
import found.ProofNets
import found.Quantum
import found.QuantumCirc
import found.Ramified
import found.Recursive
import found.RegToMu
import found.Register
import found.SDG
import found.SEAR
import found.SK
import found.STLC_F
import found.Schanuel
import found.Schanuel2
import found.Schanuel3
import found.SecondOrder
import found.SecondOrder2
import found.ShulmanCor
import found.ShulmanPath
import found.StrDiag1
import found.StrDiag2
import found.Truth
import found.TwoLevel
import found.TypeVariants
import found.TypedCalculi
import found.Ultraproduct
import found.UniversalLogic
import found.VeqL
import found.Zinf

namespace Found.Lines

/-- T107: Found.ETCS.well_pointed -/
theorem line_T107_0 : type_of% @Found.ETCS.well_pointed := @Found.ETCS.well_pointed
#print axioms line_T107_0
/-- T90: Found.KP.sat_resp -/
theorem line_T90_0 : type_of% @Found.KP.sat_resp := @Found.KP.sat_resp
#print axioms line_T90_0
/-- T91: Found.FOCT.completenessT -/
theorem line_T91_0 : type_of% @Found.FOCT.completenessT := @Found.FOCT.completenessT
#print axioms line_T91_0
/-- T119: Found.SDG.kl_not_all_zero -/
theorem line_T119_0 : type_of% @Found.SDG.kl_not_all_zero := @Found.SDG.kl_not_all_zero
#print axioms line_T119_0
/-- T119: Found.SDG.nn_zero -/
theorem line_T119_1 : type_of% @Found.SDG.nn_zero := @Found.SDG.nn_zero
#print axioms line_T119_1
/-- T111: Found.Quantum.toHA_boolean -/
theorem line_T111_0 : type_of% @Found.Quantum.toHA_boolean := @Found.Quantum.toHA_boolean
#print axioms line_T111_0
/-- T59: Found.Core.copair_uniq -/
theorem line_T59_0 : type_of% @Found.Core.copair_uniq := @Found.Core.copair_uniq
#print axioms line_T59_0
/-- T59: Found.Core.chiU_spec -/
theorem line_T59_1 : type_of% @Found.Core.chiU_spec := @Found.Core.chiU_spec
#print axioms line_T59_1
/-- T59: Found.Core.qlift_uniq -/
theorem line_T59_2 : type_of% @Found.Core.qlift_uniq := @Found.Core.qlift_uniq
#print axioms line_T59_2
/-- T59: Found.Core.fold_uniq -/
theorem line_T59_3 : type_of% @Found.Core.fold_uniq := @Found.Core.fold_uniq
#print axioms line_T59_3
/-- T59: Found.Core.unfold_uniq -/
theorem line_T59_4 : type_of% @Found.Core.unfold_uniq := @Found.Core.unfold_uniq
#print axioms line_T59_4
/-- T59: Found.Core.locLift_uniq -/
theorem line_T59_5 : type_of% @Found.Core.locLift_uniq := @Found.Core.locLift_uniq
#print axioms line_T59_5
/-- T143: Found.Prp.deduction -/
theorem line_T143_0 : type_of% @Found.Prp.deduction := @Found.Prp.deduction
#print axioms line_T143_0
/-- T116: Found.Dom.fix_le -/
theorem line_T116_0 : type_of% @Found.Dom.fix_le := @Found.Dom.fix_le
#print axioms line_T116_0
/-- T116: Found.Dom.le_fix -/
theorem line_T116_1 : type_of% @Found.Dom.le_fix := @Found.Dom.le_fix
#print axioms line_T116_1
/-- T116: Found.Dom.fix_least -/
theorem line_T116_2 : type_of% @Found.Dom.fix_least := @Found.Dom.fix_least
#print axioms line_T116_2
/-- T156: Found.Coalg.comp_hom -/
theorem line_T156_0 : type_of% @Found.Coalg.comp_hom := @Found.Coalg.comp_hom
#print axioms line_T156_0
/-- T156: Found.Coalg.final_iso -/
theorem line_T156_1 : type_of% @Found.Coalg.final_iso := @Found.Coalg.final_iso
#print axioms line_T156_1
/-- T156: Found.Coalg.lambek -/
theorem line_T156_2 : type_of% @Found.Coalg.lambek := @Found.Coalg.lambek
#print axioms line_T156_2
/-- T139: Found.DaCosta.lem -/
theorem line_T139_0 : type_of% @Found.DaCosta.lem := @Found.DaCosta.lem
#print axioms line_T139_0
/-- T104: Found.Groth.lift_eq -/
theorem line_T104_0 : type_of% @Found.Groth.lift_eq := @Found.Groth.lift_eq
#print axioms line_T104_0
/-- T104: Found.Groth.mem_lift -/
theorem line_T104_1 : type_of% @Found.Groth.mem_lift := @Found.Groth.mem_lift
#print axioms line_T104_1
/-- T171: Found.Mu.ef_eq_reach -/
theorem line_T171_0 : type_of% @Found.Mu.ef_eq_reach := @Found.Mu.ef_eq_reach
#print axioms line_T171_0
/-- T177: Found.LFP.tc_lfp -/
theorem line_T177_0 : type_of% @Found.LFP.tc_lfp := @Found.LFP.tc_lfp
#print axioms line_T177_0
/-- T176: Found.DL.tr_correct -/
theorem line_T176_0 : type_of% @Found.DL.tr_correct := @Found.DL.tr_correct
#print axioms line_T176_0
/-- T176: Found.DL.subsumption_iff -/
theorem line_T176_1 : type_of% @Found.DL.subsumption_iff := @Found.DL.subsumption_iff
#print axioms line_T176_1
/-- T172: Found.Cond.identity -/
theorem line_T172_0 : type_of% @Found.Cond.identity := @Found.Cond.identity
#print axioms line_T172_0
/-- T172: Found.Cond.no_strengthening -/
theorem line_T172_1 : type_of% @Found.Cond.no_strengthening := @Found.Cond.no_strengthening
#print axioms line_T172_1
/-- T173: Found.NonMono.refl -/
theorem line_T173_0 : type_of% @Found.NonMono.refl := @Found.NonMono.refl
#print axioms line_T173_0
/-- T173: Found.NonMono.and_rule -/
theorem line_T173_1 : type_of% @Found.NonMono.and_rule := @Found.NonMono.and_rule
#print axioms line_T173_1
/-- T173: Found.NonMono.not_monotone -/
theorem line_T173_2 : type_of% @Found.NonMono.not_monotone := @Found.NonMono.not_monotone
#print axioms line_T173_2
/-- T175: Found.Team.dep_down -/
theorem line_T175_0 : type_of% @Found.Team.dep_down := @Found.Team.dep_down
#print axioms line_T175_0
/-- T175: Found.Team.tensor_down -/
theorem line_T175_1 : type_of% @Found.Team.tensor_down := @Found.Team.tensor_down
#print axioms line_T175_1
/-- T175: Found.Team.not_flat -/
theorem line_T175_2 : type_of% @Found.Team.not_flat := @Found.Team.not_flat
#print axioms line_T175_2
/-- T178: Found.Just.application -/
theorem line_T178_0 : type_of% @Found.Just.application := @Found.Just.application
#print axioms line_T178_0
/-- T178: Found.Just.reflection -/
theorem line_T178_1 : type_of% @Found.Just.reflection := @Found.Just.reflection
#print axioms line_T178_1
/-- T178: Found.Just.forget -/
theorem line_T178_2 : type_of% @Found.Just.forget := @Found.Just.forget
#print axioms line_T178_2
/-- T182: Found.Auto.run_spec -/
theorem line_T182_0 : type_of% @Found.Auto.run_spec := @Found.Auto.run_spec
#print axioms line_T182_0
/-- T182: Found.Auto.subset_construction -/
theorem line_T182_1 : type_of% @Found.Auto.subset_construction := @Found.Auto.subset_construction
#print axioms line_T182_1
/-- T183: Found.Proc.bis_trans -/
theorem line_T183_0 : type_of% @Found.Proc.bis_trans := @Found.Proc.bis_trans
#print axioms line_T183_0
/-- T183: Found.Proc.hm_invariance -/
theorem line_T183_1 : type_of% @Found.Proc.hm_invariance := @Found.Proc.hm_invariance
#print axioms line_T183_1
/-- T198: Found.Markov.lem_mp -/
theorem line_T198_0 : type_of% @Found.Markov.lem_mp := @Found.Markov.lem_mp
#print axioms line_T198_0
/-- T198: Found.Markov.mp_not_all -/
theorem line_T198_1 : type_of% @Found.Markov.mp_not_all := @Found.Markov.mp_not_all
#print axioms line_T198_1
/-- T201: Found.Mereo.strong_supp -/
theorem line_T201_0 : type_of% @Found.Mereo.strong_supp := @Found.Mereo.strong_supp
#print axioms line_T201_0
/-- T201: Found.Mereo.fusion_least -/
theorem line_T201_1 : type_of% @Found.Mereo.fusion_least := @Found.Mereo.fusion_least
#print axioms line_T201_1
/-- T191: Found.Prp.interp_LT -/
theorem line_T191_0 : type_of% @Found.Prp.interp_LT := @Found.Prp.interp_LT
#print axioms line_T191_0
/-- T191: Found.Prp.completeness -/
theorem line_T191_1 : type_of% @Found.Prp.completeness := @Found.Prp.completeness
#print axioms line_T191_1
/-- T191: Found.Prp.heyting_iff -/
theorem line_T191_2 : type_of% @Found.Prp.heyting_iff := @Found.Prp.heyting_iff
#print axioms line_T191_2
/-- T192: Found.Truth.lawvere -/
theorem line_T192_0 : type_of% @Found.Truth.lawvere := @Found.Truth.lawvere
#print axioms line_T192_0
/-- T192: Found.Truth.cantor -/
theorem line_T192_1 : type_of% @Found.Truth.cantor := @Found.Truth.cantor
#print axioms line_T192_1
/-- T192: Found.Truth.undefinable -/
theorem line_T192_2 : type_of% @Found.Truth.undefinable := @Found.Truth.undefinable
#print axioms line_T192_2
/-- T142: Found.SO.henkin_to_standard -/
theorem line_T142_0 : type_of% @Found.SO.henkin_to_standard := @Found.SO.henkin_to_standard
#print axioms line_T142_0
/-- T170: Found.MS.relativize -/
theorem line_T170_0 : type_of% @Found.MS.relativize := @Found.MS.relativize
#print axioms line_T170_0
/-- T170: Found.MS.ofD2_all -/
theorem line_T170_1 : type_of% @Found.MS.ofD2_all := @Found.MS.ofD2_all
#print axioms line_T170_1
/-- T146: Found.Zermelo.zermelo_sep -/
theorem line_T146_0 : type_of% @Found.Zermelo.zermelo_sep := @Found.Zermelo.zermelo_sep
#print axioms line_T146_0
/-- T162: Found.Zermelo.tg -/
theorem line_T162_0 : type_of% @Found.Zermelo.tg := @Found.Zermelo.tg
#print axioms line_T162_0
/-- T81: Found.Rec.add_correct -/
theorem line_T81_0 : type_of% @Found.Rec.add_correct := @Found.Rec.add_correct
#print axioms line_T81_0
/-- T81: Found.Rec.mu_det -/
theorem line_T81_1 : type_of% @Found.Rec.mu_det := @Found.Rec.mu_det
#print axioms line_T81_1
/-- T145: Found.Reg.add_runs -/
theorem line_T145_0 : type_of% @Found.Reg.add_runs := @Found.Reg.add_runs
#print axioms line_T145_0
/-- T186: Found.Weak.d0_decidable -/
theorem line_T186_0 : type_of% @Found.Weak.d0_decidable := @Found.Weak.d0_decidable
#print axioms line_T186_0
/-- T186: Found.Weak.bounded_all -/
theorem line_T186_1 : type_of% @Found.Weak.bounded_all := @Found.Weak.bounded_all
#print axioms line_T186_1
/-- T93: Found.Prp.classical_iff -/
theorem line_T93_0 : type_of% @Found.Prp.classical_iff := @Found.Prp.classical_iff
#print axioms line_T93_0
/-- T94: Found.Prp.finite_hyps -/
theorem line_T94_0 : type_of% @Found.Prp.finite_hyps := @Found.Prp.finite_hyps
#print axioms line_T94_0
/-- T94: Found.Prp.strong_completeness -/
theorem line_T94_1 : type_of% @Found.Prp.strong_completeness := @Found.Prp.strong_completeness
#print axioms line_T94_1
/-- T94: Found.Prp.compactness -/
theorem line_T94_2 : type_of% @Found.Prp.compactness := @Found.Prp.compactness
#print axioms line_T94_2
/-- T83: Found.PA.nat_PA -/
theorem line_T83_0 : type_of% @Found.PA.nat_PA := @Found.PA.nat_PA
#print axioms line_T83_0
/-- T83: Found.PA.one_not_Q -/
theorem line_T83_1 : type_of% @Found.PA.one_not_Q := @Found.PA.one_not_Q
#print axioms line_T83_1
/-- T83: Found.PA.reach -/
theorem line_T83_2 : type_of% @Found.PA.reach := @Found.PA.reach
#print axioms line_T83_2
/-- T84: Found.RM.comp_mono -/
theorem line_T84_0 : type_of% @Found.RM.comp_mono := @Found.RM.comp_mono
#print axioms line_T84_0
/-- T84: Found.RM.chain -/
theorem line_T84_1 : type_of% @Found.RM.chain := @Found.RM.chain
#print axioms line_T84_1
/-- T84: Found.RM.full_model -/
theorem line_T84_2 : type_of% @Found.RM.full_model := @Found.RM.full_model
#print axioms line_T84_2
/-- T151: Found.Hyper.ex_mono -/
theorem line_T151_0 : type_of% @Found.Hyper.ex_mono := @Found.Hyper.ex_mono
#print axioms line_T151_0
/-- T151: Found.Hyper.all_mono -/
theorem line_T151_1 : type_of% @Found.Hyper.all_mono := @Found.Hyper.all_mono
#print axioms line_T151_1
/-- T110: Found.Nom.atoms_nominal -/
theorem line_T110_0 : type_of% @Found.Nom.atoms_nominal := @Found.Nom.atoms_nominal
#print axioms line_T110_0
/-- T110: Found.Nom.prod_nominal -/
theorem line_T110_1 : type_of% @Found.Nom.prod_nominal := @Found.Nom.prod_nominal
#print axioms line_T110_1
/-- T197: Found.Brouwer.wcn_not_lem -/
theorem line_T197_0 : type_of% @Found.Brouwer.wcn_not_lem := @Found.Brouwer.wcn_not_lem
#print axioms line_T197_0
/-- T164: Found.GPK.univ -/
theorem line_T164_0 : type_of% @Found.GPK.univ := @Found.GPK.univ
#print axioms line_T164_0
/-- T164: Found.GPK.union -/
theorem line_T164_1 : type_of% @Found.GPK.union := @Found.GPK.union
#print axioms line_T164_1
/-- T164: Found.GPK.inter -/
theorem line_T164_2 : type_of% @Found.GPK.inter := @Found.GPK.inter
#print axioms line_T164_2
/-- T180: Found.Dial.hom_is_dialectica -/
theorem line_T180_0 : type_of% @Found.Dial.hom_is_dialectica := @Found.Dial.hom_is_dialectica
#print axioms line_T180_0
/-- T99: Found.CICPoint.impProp_holds -/
theorem line_T99_0 : type_of% @Found.CICPoint.impProp_holds := @Found.CICPoint.impProp_holds
#print axioms line_T99_0
/-- T99: Found.CICPoint.proof_irrel -/
theorem line_T99_1 : type_of% @Found.CICPoint.proof_irrel := @Found.CICPoint.proof_irrel
#print axioms line_T99_1
/-- T99: Found.CICPoint.uip -/
theorem line_T99_2 : type_of% @Found.CICPoint.uip := @Found.CICPoint.uip
#print axioms line_T99_2
/-- T200: Found.Rec.mul_correct -/
theorem line_T200_0 : type_of% @Found.Rec.mul_correct := @Found.Rec.mul_correct
#print axioms line_T200_0
/-- T122: Found.TwoL.innerConstToOuter -/
theorem line_T122_0 : type_of% @Found.TwoL.innerConstToOuter := @Found.TwoL.innerConstToOuter
#print axioms line_T122_0
/-- T121: Found.OTT.funext_obs -/
theorem line_T121_0 : type_of% @Found.OTT.funext_obs := @Found.OTT.funext_obs
#print axioms line_T121_0
/-- T121: Found.OTT.pair_obs -/
theorem line_T121_2 : type_of% @Found.OTT.pair_obs := @Found.OTT.pair_obs
#print axioms line_T121_2
/-- T148: Found.Beth.beth_sound -/
theorem line_T148_0 : type_of% @Found.Beth.beth_sound := @Found.Beth.beth_sound
#print axioms line_T148_0
/-- T147: Found.SEAR.tab_spec -/
theorem line_T147_0 : type_of% @Found.SEAR.tab_spec := @Found.SEAR.tab_spec
#print axioms line_T147_0
/-- T147: Found.SEAR.jointly_monic -/
theorem line_T147_1 : type_of% @Found.SEAR.jointly_monic := @Found.SEAR.jointly_monic
#print axioms line_T147_1
/-- T206: Found.UL2.nd_tarskian -/
theorem line_T206_0 : type_of% @Found.UL2.nd_tarskian := @Found.UL2.nd_tarskian
#print axioms line_T206_0
/-- T206: Found.UL2.sem_tarskian -/
theorem line_T206_1 : type_of% @Found.UL2.sem_tarskian := @Found.UL2.sem_tarskian
#print axioms line_T206_1
/-- T206: Found.UL2.pref_not_monotone -/
theorem line_T206_2 : type_of% @Found.UL2.pref_not_monotone := @Found.UL2.pref_not_monotone
#print axioms line_T206_2
/-- T157: Found.Inst.condition -/
theorem line_T157_0 : type_of% @Found.Inst.condition := @Found.Inst.condition
#print axioms line_T157_0
/-- T80: Found.UL.K_beta -/
theorem line_T80_0 : type_of% @Found.UL.K_beta := @Found.UL.K_beta
#print axioms line_T80_0
/-- T80: Found.UL.S_beta -/
theorem line_T80_1 : type_of% @Found.UL.S_beta := @Found.UL.S_beta
#print axioms line_T80_1
/-- T80: Found.CL.simulate -/
theorem line_T80_2 : type_of% @Found.CL.simulate := @Found.CL.simulate
#print axioms line_T80_2
/-- T205: Found.HOL.fun_ext -/
theorem line_T205_0 : type_of% @Found.HOL.fun_ext := @Found.HOL.fun_ext
#print axioms line_T205_0
/-- T205: Found.HOL.bool_ext -/
theorem line_T205_1 : type_of% @Found.HOL.bool_ext := @Found.HOL.bool_ext
#print axioms line_T205_1
/-- T205: Found.HOL.classical_condition -/
theorem line_T205_2 : type_of% @Found.HOL.classical_condition := @Found.HOL.classical_condition
#print axioms line_T205_2
/-- T174: Found.Prob.incl_excl -/
theorem line_T174_0 : type_of% @Found.Prob.incl_excl := @Found.Prob.incl_excl
#print axioms line_T174_0
/-- T174: Found.Prob.complement -/
theorem line_T174_1 : type_of% @Found.Prob.complement := @Found.Prob.complement
#print axioms line_T174_1
/-- T174: Found.Prob.monotone -/
theorem line_T174_2 : type_of% @Found.Prob.monotone := @Found.Prob.monotone
#print axioms line_T174_2
/-- T193: Found.TwoCat.interchange -/
theorem line_T193_0 : type_of% @Found.TwoCat.interchange := @Found.TwoCat.interchange
#print axioms line_T193_0
/-- T193: Found.TwoCat.hcomp_nat -/
theorem line_T193_1 : type_of% @Found.TwoCat.hcomp_nat := @Found.TwoCat.hcomp_nat
#print axioms line_T193_1
/-- T193: Found.SD.sound -/
theorem line_T193_2 : type_of% @Found.SD.sound := @Found.SD.sound
#print axioms line_T193_2
/-- T193: Found.SD.term_read -/
theorem line_T193_3 : type_of% @Found.SD.term_read := @Found.SD.term_read
#print axioms line_T193_3
/-- T193: Found.SD.joyal_street -/
theorem line_T193_4 : type_of% @Found.SD.joyal_street := @Found.SD.joyal_street
#print axioms line_T193_4
/-- T187: Found.GodelT.rec_zero -/
theorem line_T187_0 : type_of% @Found.GodelT.rec_zero := @Found.GodelT.rec_zero
#print axioms line_T187_0
/-- T187: Found.GodelT.addT_correct -/
theorem line_T187_1 : type_of% @Found.GodelT.addT_correct := @Found.GodelT.addT_correct
#print axioms line_T187_1
/-- T152: Found.CwF.pi_beta -/
theorem line_T152_0 : type_of% @Found.CwF.pi_beta := @Found.CwF.pi_beta
#print axioms line_T152_0
/-- T152: Found.CwF.sig_eta -/
theorem line_T152_1 : type_of% @Found.CwF.sig_eta := @Found.CwF.sig_eta
#print axioms line_T152_1
/-- T152: Found.CwF.id_reflect -/
theorem line_T152_2 : type_of% @Found.CwF.id_reflect := @Found.CwF.id_reflect
#print axioms line_T152_2
/-- T152: Found.CwF.id_uip -/
theorem line_T152_3 : type_of% @Found.CwF.id_uip := @Found.CwF.id_uip
#print axioms line_T152_3
/-- T98: Found.CwF.pi_beta -/
theorem line_T98_0 : type_of% @Found.CwF.pi_beta := @Found.CwF.pi_beta
#print axioms line_T98_0
/-- T98: Found.CwF.sig_eta -/
theorem line_T98_1 : type_of% @Found.CwF.sig_eta := @Found.CwF.sig_eta
#print axioms line_T98_1
/-- T98: Found.CwF.id_reflect -/
theorem line_T98_2 : type_of% @Found.CwF.id_reflect := @Found.CwF.id_reflect
#print axioms line_T98_2
/-- T120: Found.MLTTV.not_uip -/
theorem line_T120_0 : type_of% @Found.MLTTV.not_uip := @Found.MLTTV.not_uip
#print axioms line_T120_0
/-- T120: Found.MLTTV.uip_extensional -/
theorem line_T120_1 : type_of% @Found.MLTTV.uip_extensional := @Found.MLTTV.uip_extensional
#print axioms line_T120_1
/-- T160: Found.Meta.lf_adequate -/
theorem line_T160_0 : type_of% @Found.Meta.lf_adequate := @Found.Meta.lf_adequate
#print axioms line_T160_0
/-- T158: Found.Meta.mmt_compose -/
theorem line_T158_0 : type_of% @Found.Meta.mmt_compose := @Found.Meta.mmt_compose
#print axioms line_T158_0
/-- T159: Found.Meta.dedukti_rule -/
theorem line_T159_0 : type_of% @Found.Meta.dedukti_rule := @Found.Meta.dedukti_rule
#print axioms line_T159_0
/-- T203: Found.Meta.metamath_subst -/
theorem line_T203_0 : type_of% @Found.Meta.metamath_subst := @Found.Meta.metamath_subst
#print axioms line_T203_0
/-- T204: Found.Meta.mizar -/
theorem line_T204_0 : type_of% @Found.Meta.mizar := @Found.Meta.mizar
#print axioms line_T204_0
/-- T168: Found.Det.determined -/
theorem line_T168_0 : type_of% @Found.Det.determined := @Found.Det.determined
#print axioms line_T168_0
/-- T168: Found.Det.not_both -/
theorem line_T168_1 : type_of% @Found.Det.not_both := @Found.Det.not_both
#print axioms line_T168_1
/-- T117: Found.GameSem.win_iff -/
theorem line_T117_0 : type_of% @Found.GameSem.win_iff := @Found.GameSem.win_iff
#print axioms line_T117_0
/-- T117: Found.GameSem.determined -/
theorem line_T117_1 : type_of% @Found.GameSem.determined := @Found.GameSem.determined
#print axioms line_T117_1
/-- T165: Found.IST.bound -/
theorem line_T165_0 : type_of% @Found.IST.bound := @Found.IST.bound
#print axioms line_T165_0
/-- T165: Found.IST.nonstandard -/
theorem line_T165_1 : type_of% @Found.IST.nonstandard := @Found.IST.nonstandard
#print axioms line_T165_1
/-- T188: Found.LC.liftU_eq -/
theorem line_T188_0 : type_of% @Found.LC.liftU_eq := @Found.LC.liftU_eq
#print axioms line_T188_0
/-- T188: Found.LC.in_U -/
theorem line_T188_1 : type_of% @Found.LC.in_U := @Found.LC.in_U
#print axioms line_T188_1
/-- T188: Found.LC.tower -/
theorem line_T188_2 : type_of% @Found.LC.tower := @Found.LC.tower
#print axioms line_T188_2
/-- T114: Found.HOIL.boolean_iff_lem -/
theorem line_T114_0 : type_of% @Found.HOIL.boolean_iff_lem := @Found.HOIL.boolean_iff_lem
#print axioms line_T114_0
/-- T114: Found.HOIL.nn_lem -/
theorem line_T114_1 : type_of% @Found.HOIL.nn_lem := @Found.HOIL.nn_lem
#print axioms line_T114_1
/-- T149: Found.Kan.lan_mono -/
theorem line_T149_0 : type_of% @Found.Kan.lan_mono := @Found.Kan.lan_mono
#print axioms line_T149_0
/-- T149: Found.Kan.lan_universal -/
theorem line_T149_1 : type_of% @Found.Kan.lan_universal := @Found.Kan.lan_universal
#print axioms line_T149_1
/-- T135: Found.Htpy.we_identified -/
theorem line_T135_0 : type_of% @Found.Htpy.we_identified := @Found.Htpy.we_identified
#print axioms line_T135_0
/-- T135: Found.Htpy.factor_comm -/
theorem line_T135_1 : type_of% @Found.Htpy.factor_comm := @Found.Htpy.factor_comm
#print axioms line_T135_1
/-- T135: Found.Htpy.trivial_iso -/
theorem line_T135_2 : type_of% @Found.Htpy.trivial_iso := @Found.Htpy.trivial_iso
#print axioms line_T135_2
/-- T194: Found.Geo.preserve -/
theorem line_T194_0 : type_of% @Found.Geo.preserve := @Found.Geo.preserve
#print axioms line_T194_0
/-- T190: Found.Ultra.op_resp -/
theorem line_T190_0 : type_of% @Found.Ultra.op_resp := @Found.Ultra.op_resp
#print axioms line_T190_0
/-- T190: Found.Ultra.los_eq -/
theorem line_T190_1 : type_of% @Found.Ultra.los_eq := @Found.Ultra.los_eq
#print axioms line_T190_1
/-- T190: Found.Ultra.los_and -/
theorem line_T190_2 : type_of% @Found.Ultra.los_and := @Found.Ultra.los_and
#print axioms line_T190_2
/-- T195: Found.Session.dual_dual -/
theorem line_T195_0 : type_of% @Found.Session.dual_dual := @Found.Session.dual_dual
#print axioms line_T195_0
/-- T195: Found.Session.size_dual -/
theorem line_T195_1 : type_of% @Found.Session.size_dual := @Found.Session.size_dual
#print axioms line_T195_1
/-- T167: Found.Ack.classSet_spec -/
theorem line_T167_0 : type_of% @Found.Ack.classSet_spec := @Found.Ack.classSet_spec
#print axioms line_T167_0
/-- T167: Found.Ack.lift_mem -/
theorem line_T167_1 : type_of% @Found.Ack.lift_mem := @Found.Ack.lift_mem
#print axioms line_T167_1
/-- T167: Found.AK.ack_meta -/
theorem line_T167_2 : type_of% @Found.AK.ack_meta := @Found.AK.ack_meta
#print axioms line_T167_2
/-- T167: Found.AKD.ack_zf -/
theorem line_T167_3 : type_of% @Found.AKD.ack_zf := @Found.AKD.ack_zf
#print axioms line_T167_3
/-- T185: Found.Fagin.check_correct -/
theorem line_T185_0 : type_of% @Found.Fagin.check_correct := @Found.Fagin.check_correct
#print axioms line_T185_0
/-- T185: Found.Fagin.eso_iff -/
theorem line_T185_1 : type_of% @Found.Fagin.eso_iff := @Found.Fagin.eso_iff
#print axioms line_T185_1
/-- T184: Found.QC.three_cnots -/
theorem line_T184_0 : type_of% @Found.QC.three_cnots := @Found.QC.three_cnots
#print axioms line_T184_0
/-- T184: Found.QC.H_sq -/
theorem line_T184_1 : type_of% @Found.QC.H_sq := @Found.QC.H_sq
#print axioms line_T184_1
/-- T184: Found.QC.XZ_anti -/
theorem line_T184_2 : type_of% @Found.QC.XZ_anti := @Found.QC.XZ_anti
#print axioms line_T184_2
/-- T166: Found.AST.semiset_is_set -/
theorem line_T166_0 : type_of% @Found.AST.semiset_is_set := @Found.AST.semiset_is_set
#print axioms line_T166_0
/-- T166: Found.AST.ast_contradicts_separation -/
theorem line_T166_1 : type_of% @Found.AST.ast_contradicts_separation := @Found.AST.ast_contradicts_separation
#print axioms line_T166_1
/-- T202: Found.Cond2.sum_to_product -/
theorem line_T202_0 : type_of% @Found.Cond2.sum_to_product := @Found.Cond2.sum_to_product
#print axioms line_T202_0
/-- T202: Found.Cond2.empty_to_point -/
theorem line_T202_1 : type_of% @Found.Cond2.empty_to_point := @Found.Cond2.empty_to_point
#print axioms line_T202_1
/-- T189: Found.MA.meets -/
theorem line_T189_0 : type_of% @Found.MA.meets := @Found.MA.meets
#print axioms line_T189_0
/-- T189: Found.MA.filter_directed -/
theorem line_T189_1 : type_of% @Found.MA.filter_directed := @Found.MA.filter_directed
#print axioms line_T189_1
/-- T189: Found.MA.filter_up -/
theorem line_T189_2 : type_of% @Found.MA.filter_up := @Found.MA.filter_up
#print axioms line_T189_2
/-- T199: Found.Pred.ext_and -/
theorem line_T199_0 : type_of% @Found.Pred.ext_and := @Found.Pred.ext_and
#print axioms line_T199_0
/-- T199: Found.Pred.ext_ex -/
theorem line_T199_1 : type_of% @Found.Pred.ext_ex := @Found.Pred.ext_ex
#print axioms line_T199_1
/-- T199: Found.Pred.weyl -/
theorem line_T199_2 : type_of% @Found.Pred.weyl := @Found.Pred.weyl
#print axioms line_T199_2
/-- T179: Found.PN.dual_dual -/
theorem line_T179_0 : type_of% @Found.PN.dual_dual := @Found.PN.dual_dual
#print axioms line_T179_0
/-- T179: Found.PN.euler -/
theorem line_T179_1 : type_of% @Found.PN.euler := @Found.PN.euler
#print axioms line_T179_1
/-- T179: Found.DR.dr_sound -/
theorem line_T179_2 : type_of% @Found.DR.dr_sound := @Found.DR.dr_sound
#print axioms line_T179_2
/-- T179: Found.DR.danos_regnier_sound -/
theorem line_T179_3 : type_of% @Found.DR.danos_regnier_sound := @Found.DR.danos_regnier_sound
#print axioms line_T179_3
/-- T196: Found.InfN.inv_bnd -/
theorem line_T196_0 : type_of% @Found.InfN.inv_bnd := @Found.InfN.inv_bnd
#print axioms line_T196_0
/-- T196: Found.InfN.collapse -/
theorem line_T196_1 : type_of% @Found.InfN.collapse := @Found.InfN.collapse
#print axioms line_T196_1
/-- T136: Found.Shulman.path -/
theorem line_T136_0 : type_of% @Found.Shulman.path := @Found.Shulman.path
#print axioms line_T136_0
/-- T136: Found.ShulmanCor.univalent -/
theorem line_T136_1 : type_of% @Found.ShulmanCor.univalent := @Found.ShulmanCor.univalent
#print axioms line_T136_1
/-- T136: Found.ShulmanCor.shulman -/
theorem line_T136_2 : type_of% @Found.ShulmanCor.shulman := @Found.ShulmanCor.shulman
#print axioms line_T136_2
/-- T155: Found.LU.sub_comp -/
theorem line_T155_0 : type_of% @Found.LU.sub_comp := @Found.LU.sub_comp
#print axioms line_T155_0
/-- T155: Found.LU.q_pair -/
theorem line_T155_1 : type_of% @Found.LU.q_pair := @Found.LU.q_pair
#print axioms line_T155_1
/-- T155: Found.LU.pair_sub -/
theorem line_T155_2 : type_of% @Found.LU.pair_sub := @Found.LU.pair_sub
#print axioms line_T155_2
/-- T154: Found.Pres.union_of_finite -/
theorem line_T154_0 : type_of% @Found.Pres.union_of_finite := @Found.Pres.union_of_finite
#print axioms line_T154_0
/-- T154: Found.Pres.directed -/
theorem line_T154_1 : type_of% @Found.Pres.directed := @Found.Pres.directed
#print axioms line_T154_1
/-- T154: Found.Pres.reflection -/
theorem line_T154_2 : type_of% @Found.Pres.reflection := @Found.Pres.reflection
#print axioms line_T154_2
/-- T169: Found.VL.ev_resp -/
theorem line_T169_0 : type_of% @Found.VL.ev_resp := @Found.VL.ev_resp
#print axioms line_T169_0
/-- T169: Found.VL.def_sub -/
theorem line_T169_1 : type_of% @Found.VL.def_sub := @Found.VL.def_sub
#print axioms line_T169_1
/-- T169: Found.VL.sub_def -/
theorem line_T169_2 : type_of% @Found.VL.sub_def := @Found.VL.sub_def
#print axioms line_T169_2
/-- T144: Found.PCF.fact5 -/
theorem line_T144_0 : type_of% @Found.PCF.fact5 := @Found.PCF.fact5
#print axioms line_T144_0
/-- T144: Found.PCF.fact_type -/
theorem line_T144_1 : type_of% @Found.PCF.fact_type := @Found.PCF.fact_type
#print axioms line_T144_1
/-- T181: Found.PTS.mono -/
theorem line_T181_0 : type_of% @Found.PTS.mono := @Found.PTS.mono
#print axioms line_T181_0
/-- T181: Found.PTS.cube_sub -/
theorem line_T181_1 : type_of% @Found.PTS.cube_sub := @Found.PTS.cube_sub
#print axioms line_T181_1
/-- T181: Found.PTS.into_CoC -/
theorem line_T181_2 : type_of% @Found.PTS.into_CoC := @Found.PTS.into_CoC
#print axioms line_T181_2
/-- T79: Found.NbE.rr_good -/
theorem line_T79_0 : type_of% @Found.NbE.rr_good := @Found.NbE.rr_good
#print axioms line_T79_0
/-- T79: Found.NbE.eval_good -/
theorem line_T79_1 : type_of% @Found.NbE.eval_good := @Found.NbE.eval_good
#print axioms line_T79_1
/-- T79: Found.NbE.nf_normal -/
theorem line_T79_2 : type_of% @Found.NbE.nf_normal := @Found.NbE.nf_normal
#print axioms line_T79_2
/-- T85: Found.GoedelBridge.loeb_both -/
theorem line_T85_0 : type_of% @Found.GoedelBridge.loeb_both := @Found.GoedelBridge.loeb_both
#print axioms line_T85_0
/-- T85: Found.GoedelBridge.goedel2_from_loeb -/
theorem line_T85_1 : type_of% @Found.GoedelBridge.goedel2_from_loeb := @Found.GoedelBridge.goedel2_from_loeb
#print axioms line_T85_1
/-- T208: Found.FO.complete_iff -/
theorem line_T208_0 : type_of% @Found.FO.complete_iff := @Found.FO.complete_iff
#print axioms line_T208_0
/-- T208: Found.FO.lowenheim_skolem -/
theorem line_T208_1 : type_of% @Found.FO.lowenheim_skolem := @Found.FO.lowenheim_skolem
#print axioms line_T208_1
/-- T208: Found.FO.compactness -/
theorem line_T208_2 : type_of% @Found.FO.compactness := @Found.FO.compactness
#print axioms line_T208_2
/-- T127: Found.HoTTL.path_eq_iff_connected -/
theorem line_T127_0 : type_of% @Found.HoTTL.path_eq_iff_connected := @Found.HoTTL.path_eq_iff_connected
#print axioms line_T127_0
/-- T127: Found.HoTTL.not_univalent -/
theorem line_T127_1 : type_of% @Found.HoTTL.not_univalent := @Found.HoTTL.not_univalent
#print axioms line_T127_1
/-- T209: Found.ETCSZF.power_iso -/
theorem line_T209_0 : type_of% @Found.ETCSZF.power_iso := @Found.ETCSZF.power_iso
#print axioms line_T209_0
/-- T209: Found.ETCSZF2.prod_iso -/
theorem line_T209_1 : type_of% @Found.ETCSZF2.prod_iso := @Found.ETCSZF2.prod_iso
#print axioms line_T209_1
/-- T209: Found.ETCSZF2.exp_iso -/
theorem line_T209_2 : type_of% @Found.ETCSZF2.exp_iso := @Found.ETCSZF2.exp_iso
#print axioms line_T209_2
/-- T209: Found.ETCSZF3.maclaneW -/
theorem line_T209_3 : type_of% @Found.ETCSZF3.maclaneW := @Found.ETCSZF3.maclaneW
#print axioms line_T209_3
/-- T210: Found.Nom.equivariant -/
theorem line_T210_0 : type_of% @Found.Nom.equivariant := @Found.Nom.equivariant
#print axioms line_T210_0
/-- T210: Found.Nom.swap_inter -/
theorem line_T210_1 : type_of% @Found.Nom.swap_inter := @Found.Nom.swap_inter
#print axioms line_T210_1
/-- T210: Found.Nom.glue_nominal -/
theorem line_T210_2 : type_of% @Found.Nom.glue_nominal := @Found.Nom.glue_nominal
#print axioms line_T210_2
/-- T210: Found.Nom.roundtrip -/
theorem line_T210_3 : type_of% @Found.Nom.roundtrip := @Found.Nom.roundtrip
#print axioms line_T210_3
/-- T118: Found.Bishop.four_upper -/
theorem line_T118_0 : type_of% @Found.Bishop.four_upper := @Found.Bishop.four_upper
#print axioms line_T118_0
/-- T118: Found.Bishop.arch -/
theorem line_T118_1 : type_of% @Found.Bishop.arch := @Found.Bishop.arch
#print axioms line_T118_1
/-- T118: Found.Bishop.eqv_trans -/
theorem line_T118_2 : type_of% @Found.Bishop.eqv_trans := @Found.Bishop.eqv_trans
#print axioms line_T118_2
/-- T118: Found.Bishop.neg_resp -/
theorem line_T118_3 : type_of% @Found.Bishop.neg_resp := @Found.Bishop.neg_resp
#print axioms line_T118_3
/-- T96b: Found.K1.dec_enc -/
theorem line_T96b_0 : type_of% @Found.K1.dec_enc := @Found.K1.dec_enc
#print axioms line_T96b_0
/-- T96b: Found.K1.ev_det -/
theorem line_T96b_1 : type_of% @Found.K1.ev_det := @Found.K1.ev_det
#print axioms line_T96b_1
/-- T96b: Found.K1.church_thesis -/
theorem line_T96b_2 : type_of% @Found.K1.church_thesis := @Found.K1.church_thesis
#print axioms line_T96b_2
/-- T96b: Found.K1.no_halting_realizer -/
theorem line_T96b_3 : type_of% @Found.K1.no_halting_realizer := @Found.K1.no_halting_realizer
#print axioms line_T96b_3
/-- T207: Found.CT.correct -/
theorem line_T207_0 : type_of% @Found.CT.correct := @Found.CT.correct
#print axioms line_T207_0
/-- T207: Found.CT3.cor -/
theorem line_T207_1 : type_of% @Found.CT3.cor := @Found.CT3.cor
#print axioms line_T207_1
/-- T207: Found.RM.reg_to_mu -/
theorem line_T207_2 : type_of% @Found.RM.reg_to_mu := @Found.RM.reg_to_mu
#print axioms line_T207_2
/-- T82: Found.CT3.cor -/
theorem line_T82_0 : type_of% @Found.CT3.cor := @Found.CT3.cor
#print axioms line_T82_0
/-- T82: Found.RM.reg_to_mu -/
theorem line_T82_1 : type_of% @Found.RM.reg_to_mu := @Found.RM.reg_to_mu
#print axioms line_T82_1
/-- T82: Found.K1.no_halting_realizer -/
theorem line_T82_2 : type_of% @Found.K1.no_halting_realizer := @Found.K1.no_halting_realizer
#print axioms line_T82_2
/-- T124: Found.Ord.nf_acc -/
theorem line_T124_0 : type_of% @Found.Ord.nf_acc := @Found.Ord.nf_acc
#print axioms line_T124_0
/-- T124: Found.Zinf.cut_elim -/
theorem line_T124_1 : type_of% @Found.Zinf.cut_elim := @Found.Zinf.cut_elim
#print axioms line_T124_1
/-- T124: Found.PAE.embed -/
theorem line_T124_2 : type_of% @Found.PAE.embed := @Found.PAE.embed
#print axioms line_T124_2
/-- T124: Found.PAE.pa_consistent -/
theorem line_T124_3 : type_of% @Found.PAE.pa_consistent := @Found.PAE.pa_consistent
#print axioms line_T124_3
/-- T106: Found.NFS.allowable_mul -/
theorem line_T106_0 : type_of% @Found.NFS.allowable_mul := @Found.NFS.allowable_mul
#print axioms line_T106_0
/-- T106: Found.NFA.alt_equivariant -/
theorem line_T106_1 : type_of% @Found.NFA.alt_equivariant := @Found.NFA.alt_equivariant
#print axioms line_T106_1
/-- T106: Found.NFU.supp_act -/
theorem line_T106_2 : type_of% @Found.NFU.supp_act := @Found.NFU.supp_act
#print axioms line_T106_2
/-- T106: Found.NFR.fmap_inj -/
theorem line_T106_3 : type_of% @Found.NFR.fmap_inj := @Found.NFR.fmap_inj
#print axioms line_T106_3
/-- T106: Found.NFAs.assemble -/
theorem line_T106_4 : type_of% @Found.NFAs.assemble := @Found.NFAs.assemble
#print axioms line_T106_4
/-- T106: Found.NFCS.bound_of_small -/
theorem line_T106_5 : type_of% @Found.NFCS.bound_of_small := @Found.NFCS.bound_of_small
#print axioms line_T106_5
/-- T106: Found.NFLb.orbitSmall_of_labels -/
theorem line_T106_6 : type_of% @Found.NFLb.orbitSmall_of_labels := @Found.NFLb.orbitSmall_of_labels
#print axioms line_T106_6
/-- T211: Found.Frege.russell -/
theorem line_T211_0 : type_of% @Found.Frege.russell := @Found.Frege.russell
#print axioms line_T211_0
/-- T211: Found.Frege.cantor -/
theorem line_T211_1 : type_of% @Found.Frege.cantor := @Found.Frege.cantor
#print axioms line_T211_1
/-- T211: Found.Frege.zero_not_succ -/
theorem line_T211_2 : type_of% @Found.Frege.zero_not_succ := @Found.Frege.zero_not_succ
#print axioms line_T211_2
/-- T211: Found.Frege.nat_ind -/
theorem line_T211_3 : type_of% @Found.Frege.nat_ind := @Found.Frege.nat_ind
#print axioms line_T211_3
/-- T212: Found.Ramified.collapse_all -/
theorem line_T212_0 : type_of% @Found.Ramified.collapse_all := @Found.Ramified.collapse_all
#print axioms line_T212_0
/-- T212: Found.Ramified.collapse_ex -/
theorem line_T212_1 : type_of% @Found.Ramified.collapse_ex := @Found.Ramified.collapse_ex
#print axioms line_T212_1
/-- T212: Found.Ramified.bool_not_reducible -/
theorem line_T212_2 : type_of% @Found.Ramified.bool_not_reducible := @Found.Ramified.bool_not_reducible
#print axioms line_T212_2
/-- T213: Found.ETCC.toArr_ofArr -/
theorem line_T213_0 : type_of% @Found.ETCC.toArr_ofArr := @Found.ETCC.toArr_ofArr
#print axioms line_T213_0
/-- T213: Found.ETCC.ofArr_obj -/
theorem line_T213_1 : type_of% @Found.ETCC.ofArr_obj := @Found.ETCC.ofArr_obj
#print axioms line_T213_1
/-- T213: Found.ETCC.discF_obj -/
theorem line_T213_2 : type_of% @Found.ETCC.discF_obj := @Found.ETCC.discF_obj
#print axioms line_T213_2
/-- T214: Found.Epsilon.ex_iff -/
theorem line_T214_0 : type_of% @Found.Epsilon.ex_iff := @Found.Epsilon.ex_iff
#print axioms line_T214_0
/-- T214: Found.Epsilon.all_iff -/
theorem line_T214_1 : type_of% @Found.Epsilon.all_iff := @Found.Epsilon.all_iff
#print axioms line_T214_1
/-- T214: Found.Epsilon.choice -/
theorem line_T214_2 : type_of% @Found.Epsilon.choice := @Found.Epsilon.choice
#print axioms line_T214_2
/-- T214: Found.Epsilon.diaconescu -/
theorem line_T214_3 : type_of% @Found.Epsilon.diaconescu := @Found.Epsilon.diaconescu
#print axioms line_T214_3
/-- T215: Found.Plural.compr -/
theorem line_T215_0 : type_of% @Found.Plural.compr := @Found.Plural.compr
#print axioms line_T215_0
/-- T215: Found.Plural.boolos_ex -/
theorem line_T215_1 : type_of% @Found.Plural.boolos_ex := @Found.Plural.boolos_ex
#print axioms line_T215_1
/-- T215: Found.Plural.boolos_all -/
theorem line_T215_2 : type_of% @Found.Plural.boolos_all := @Found.Plural.boolos_all
#print axioms line_T215_2
/-- T215: Found.Plural.oneOf_iff_part -/
theorem line_T215_3 : type_of% @Found.Plural.oneOf_iff_part := @Found.Plural.oneOf_iff_part
#print axioms line_T215_3
/-- T216: Found.Krivine.mp -/
theorem line_T216_0 : type_of% @Found.Krivine.mp := @Found.Krivine.mp
#print axioms line_T216_0
/-- T216: Found.Krivine.k_real -/
theorem line_T216_1 : type_of% @Found.Krivine.k_real := @Found.Krivine.k_real
#print axioms line_T216_1
/-- T216: Found.Krivine.peirce -/
theorem line_T216_2 : type_of% @Found.Krivine.peirce := @Found.Krivine.peirce
#print axioms line_T216_2
/-- T216: Found.Krivine.empty_arr -/
theorem line_T216_3 : type_of% @Found.Krivine.empty_arr := @Found.Krivine.empty_arr
#print axioms line_T216_3

end Found.Lines

-- not resolved to a theorem in the listed files (definitions or prose): T62:Adj, T63:CCC, T63:H, T64:H, T65:H, T65:PD, T66:H, T66:PD, T67:R, T68:M, T68:H, T70:H, T71:H, T71:M, T71:PD, T72:R, T73:H, T76:PD, T77:PD, T108:H, T108:R, T108:PD, T96:H, T95:H, T86:NNO, T107:T86, T107:NNO, T107:T66, T107:T87, T88:T109, T89:T109, T89:T87, T90:T88, T90:T109, T91:T88, T91:T109, T91:NBGc.nbg_conservative, T91  -- (+960 chars; full line in the Zenodo source)

found/LocalUniverse.lean

lean_all/found/LocalUniverse.lean

/-
  T155. found/LocalUniverse — local universes (Lumsdaine–Warren) as a
  point: the coherence problem of type-theoretic models is solved by
  representing a type in context Γ as a map Γ → U into a universe
  (U, El) — a value of the universe coordinate — so substitution is
  precomposition and is strictly functorial.
  * types Γ ⊢ A := Γ → U; terms := sections of El ∘ A; substitution by
    precomposition; strict laws A[id] = A, A[σ][τ] = A[σ∘τ] (rfl).
  * comprehension Γ.A := Σ γ, El (A γ) with projection and pairing; the
    substitution of a pair is the pair of substitutions (rfl).
  Type-theoretic model toposes (Shulman) are cited.

  Lean 4 core only, hand-written proofs, no axioms.
-/

namespace Found

namespace LU

universe u v

structure Univ where
  U : Type u
  El : U → Type u

variable (W : Univ.{u})

def Ty (Γ : Type u) : Type u := Γ → W.U
def Tm (Γ : Type u) (A : Ty W Γ) : Type u := (γ : Γ) → W.El (A γ)

def subTy {Δ Γ : Type u} (A : Ty W Γ) (σ : Δ → Γ) : Ty W Δ := fun δ => A (σ δ)
def subTm {Δ Γ : Type u} {A : Ty W Γ} (a : Tm W Γ A) (σ : Δ → Γ) : Tm W Δ (subTy W A σ) := fun δ => a (σ δ)

theorem sub_id {Γ : Type u} (A : Ty W Γ) : subTy W A (fun γ => γ) = A := rfl
theorem sub_comp {Θ Δ Γ : Type u} (A : Ty W Γ) (σ : Δ → Γ) (τ : Θ → Δ) :
    subTy W (subTy W A σ) τ = subTy W A (fun θ => σ (τ θ)) := rfl
theorem subTm_comp {Θ Δ Γ : Type u} {A : Ty W Γ} (a : Tm W Γ A) (σ : Δ → Γ) (τ : Θ → Δ) :
    subTm W (subTm W a σ) τ = subTm W a (fun θ => σ (τ θ)) := rfl

def ext (Γ : Type u) (A : Ty W Γ) : Type u := (γ : Γ) × W.El (A γ)
def p {Γ : Type u} {A : Ty W Γ} : ext W Γ A → Γ := fun x => x.1
def q {Γ : Type u} {A : Ty W Γ} : Tm W (ext W Γ A) (subTy W A (p W)) := fun x => x.2
def pair {Δ Γ : Type u} {A : Ty W Γ} (σ : Δ → Γ) (a : Tm W Δ (subTy W A σ)) : Δ → ext W Γ A :=
  fun δ => ⟨σ δ, a δ⟩

theorem p_pair {Δ Γ : Type u} {A : Ty W Γ} (σ : Δ → Γ) (a : Tm W Δ (subTy W A σ)) :
    (fun δ => p W (pair W σ a δ)) = σ := rfl
theorem q_pair {Δ Γ : Type u} {A : Ty W Γ} (σ : Δ → Γ) (a : Tm W Δ (subTy W A σ)) :
    subTm W (q W) (pair W σ a) = a := rfl
theorem pair_sub {Θ Δ Γ : Type u} {A : Ty W Γ} (σ : Δ → Γ) (a : Tm W Δ (subTy W A σ)) (τ : Θ → Δ) :
    (fun θ => pair W σ a (τ θ)) = pair W (fun θ => σ (τ θ)) (subTm W a τ) := rfl

end LU

end Found

#print axioms Found.LU.sub_id
#print axioms Found.LU.sub_comp
#print axioms Found.LU.subTm_comp
#print axioms Found.LU.p_pair
#print axioms Found.LU.q_pair
#print axioms Found.LU.pair_sub

found/Locale.lean

lean_all/found/Locale.lean

/-
  T115. found/Locale — locales (pointless topology) as a point: frames are a
  free parameter (preorder, finite meets, joins of every family, infinite
  distributivity).

  * Every frame is a Heyting algebra (a → b := ⋁{c | c ∧ a ≤ b}) — the line
    frames → Heyting values → intuitionistic logic (T65), and sheaf truth
    values (T95).
  * Instance: the opens of any topological space (Prop-valued predicates
    closed under all unions and finite intersections) form a frame.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Topos

namespace Found

namespace Loc

universe u

structure Frame where
  car : Type u
  le : car → car → Prop
  le_refl : ∀ a, le a a
  le_trans : ∀ {a b c}, le a b → le b c → le a c
  top : car
  meet : car → car → car
  le_top : ∀ a, le a top
  meet_l : ∀ a b, le (meet a b) a
  meet_r : ∀ a b, le (meet a b) b
  meet_univ : ∀ {a b c}, le c a → le c b → le c (meet a b)
  sup : {I : Type u} → (I → car) → car
  sup_ub : ∀ {I : Type u} (f : I → car) (i : I), le (f i) (sup f)
  sup_least : ∀ {I : Type u} (f : I → car) (c : car), (∀ i, le (f i) c) → le (sup f) c
  distrib : ∀ {I : Type u} (a : car) (f : I → car), le (meet a (sup f)) (sup (fun i => meet a (f i)))

variable (L : Frame.{u})

def toHA : HA.{u} where
  car := L.car
  le := L.le
  le_refl := L.le_refl
  le_trans := L.le_trans
  top := L.top
  bot := L.sup (fun e : PEmpty.{u + 1} => nomatch e)
  meet := L.meet
  join a b := L.sup (fun t : ULift.{u} Bool => match t.down with | true => a | false => b)
  imp a b := L.sup (fun c : {c : L.car // L.le (L.meet c a) b} => c.val)
  le_top := L.le_top
  bot_le c := L.sup_least _ c (fun e => nomatch e)
  meet_l := L.meet_l
  meet_r := L.meet_r
  meet_univ := L.meet_univ
  join_l a b := L.sup_ub (fun t : ULift.{u} Bool => match t.down with | true => a | false => b) ⟨true⟩
  join_r a b := L.sup_ub (fun t : ULift.{u} Bool => match t.down with | true => a | false => b) ⟨false⟩
  join_univ {_ _ c} ha hb := L.sup_least _ c (fun t => match t with
    | ⟨true⟩ => ha
    | ⟨false⟩ => hb)
  imp_adj {a b c} :=
    ⟨fun h => L.sup_ub (fun c : {c : L.car // L.le (L.meet c a) b} => c.val) ⟨c, h⟩,
     fun h => L.le_trans (L.meet_univ (L.meet_r _ _) (L.le_trans (L.meet_l _ _) h))
       (L.le_trans (L.distrib a _)
         (L.sup_least _ b (fun i => L.le_trans (L.meet_univ (L.meet_r _ _) (L.meet_l _ _)) i.property)))⟩

/-! ## opens of a topological space -/

structure Top (X : Type u) where
  isOpen : (X → Prop) → Prop
  univ_open : isOpen (fun _ => True)
  inter_open : ∀ {U V}, isOpen U → isOpen V → isOpen (fun x => U x ∧ V x)
  union_open : ∀ {I : Type u} (f : I → X → Prop), (∀ i, isOpen (f i)) → isOpen (fun x => ∃ i, f i x)

def opens {X : Type u} (T : Top X) : Frame.{u} where
  car := {U : X → Prop // T.isOpen U}
  le U V := ∀ x, U.val x → V.val x
  le_refl _ _ h := h
  le_trans h k x a := k x (h x a)
  top := ⟨fun _ => True, T.univ_open⟩
  meet U V := ⟨fun x => U.val x ∧ V.val x, T.inter_open U.property V.property⟩
  le_top _ _ _ := trivial
  meet_l _ _ _ h := h.1
  meet_r _ _ _ h := h.2
  meet_univ h k x a := ⟨h x a, k x a⟩
  sup f := ⟨fun x => ∃ i, (f i).val x, T.union_open _ (fun i => (f i).property)⟩
  sup_ub _ i _ h := ⟨i, h⟩
  sup_least _ _ h x := fun ⟨i, hi⟩ => h i x hi
  distrib _ _ _ := fun ⟨ha, i, hi⟩ => ⟨i, ha, hi⟩

/-- the opens of a space give Heyting truth values -/
def spaceHA {X : Type u} (T : Top X) : HA.{u} := toHA (opens T)

end Loc

end Found

#print axioms Found.Loc.toHA
#print axioms Found.Loc.opens
#print axioms Found.Loc.spaceHA

found/Los.lean

lean_all/found/Los.lean

/-
  T190 line. found/Los — Łoś's theorem for all first-order formulas, in the
  classical point (found/ClassicalPoint): for an ultrafilter U on I and
  structures (M i, R i), a formula holds in the ultraproduct (Tarski
  semantics on the product, atoms decided by U) iff the set of indices where
  it holds belongs to U.  Choice gives the witnesses for ∀/∃ (each M i is
  inhabited).  Syntax: the first-order formulas of found/FOCompleteness.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.FOCompleteness
import found.ClassicalPoint

namespace Found

namespace Los

open CP FO

structure Ultra (I : Type) where
  mem : (I → Prop) → Prop
  univ : mem (fun _ => True)
  up : ∀ {A B : I → Prop}, (∀ i, A i → B i) → mem A → mem B
  inter : ∀ {A B : I → Prop}, mem A → mem B → mem (fun i => A i ∧ B i)
  proper : ¬ mem (fun _ => False)
  ultra : ∀ A, mem A ∨ mem (fun i => ¬ A i)

variable {I : Type} (U : Ultra I) (M : I → Type) (R : (i : I) → Nat → List (M i) → Prop)

/-- satisfaction in one factor -/
def sat (i : I) : Fm → (Nat → M i) → Prop
  | .atom r l, v => R i r (mapD v l)
  | .bot, _ => False
  | .conj a b, v => sat i a v ∧ sat i b v
  | .disj a b, v => sat i a v ∨ sat i b v
  | .imp a b, v => sat i a v → sat i b v
  | .all a, v => ∀ m, sat i a (ext v m)
  | .ex a, v => ∃ m, sat i a (ext v m)

/-- Tarski satisfaction in the ultraproduct (elements: all sections) -/
def psat : Fm → (Nat → (i : I) → M i) → Prop
  | .atom r l, w => U.mem (fun i => R i r (mapD (fun n => w n i) l))
  | .bot, _ => False
  | .conj a b, w => psat a w ∧ psat b w
  | .disj a b, w => psat a w ∨ psat b w
  | .imp a b, w => psat a w → psat b w
  | .all a, w => ∀ x : (i : I) → M i, psat a (ext w x)
  | .ex a, w => ∃ x : (i : I) → M i, psat a (ext w x)

theorem sat_ext (i : I) : (φ : Fm) → {v v' : Nat → M i} → (∀ n, v n = v' n) → (sat M R i φ v ↔ sat M R i φ v')
  | .atom r l, _, _, h => by show R i r _ ↔ R i r _; rw [mapD_ext h l]
  | .bot, _, _, _ => Iff.rfl
  | .conj a b, _, _, h => ⟨fun ⟨x, y⟩ => ⟨(sat_ext i a h).mp x, (sat_ext i b h).mp y⟩,
      fun ⟨x, y⟩ => ⟨(sat_ext i a h).mpr x, (sat_ext i b h).mpr y⟩⟩
  | .disj a b, _, _, h => ⟨fun x => x.elim (fun x => Or.inl ((sat_ext i a h).mp x)) (fun y => Or.inr ((sat_ext i b h).mp y)),
      fun x => x.elim (fun x => Or.inl ((sat_ext i a h).mpr x)) (fun y => Or.inr ((sat_ext i b h).mpr y))⟩
  | .imp a b, _, _, h => ⟨fun f x => (sat_ext i b h).mp (f ((sat_ext i a h).mpr x)),
      fun f x => (sat_ext i b h).mpr (f ((sat_ext i a h).mp x))⟩
  | .all a, _, _, h => ⟨fun f m => (sat_ext i a (ext_ext h m)).mp (f m), fun f m => (sat_ext i a (ext_ext h m)).mpr (f m)⟩
  | .ex a, _, _, h => ⟨fun ⟨m, x⟩ => ⟨m, (sat_ext i a (ext_ext h m)).mp x⟩, fun ⟨m, x⟩ => ⟨m, (sat_ext i a (ext_ext h m)).mpr x⟩⟩

theorem ext_at (w : Nat → (i : I) → M i) (x : (i : I) → M i) (i : I) :
    ∀ n, (fun n => ext w x n i) n = ext (fun n => w n i) (x i) n
  | 0 => rfl
  | _ + 1 => rfl

variable (K : Classical') (hne : (i : I) → M i)

include K hne in
/-- Łoś's theorem -/
theorem los : (φ : Fm) → (w : Nat → (i : I) → M i) →
    (psat U M R φ w ↔ U.mem (fun i => sat M R i φ (fun n => w n i)))
  | .atom _ _, _ => Iff.rfl
  | .bot, _ => ⟨fun h => h.elim, fun h => U.proper h⟩
  | .conj a b, w => ⟨fun ⟨x, y⟩ => U.inter ((los a w).mp x) ((los b w).mp y),
      fun h => ⟨(los a w).mpr (U.up (fun _ h => h.1) h), (los b w).mpr (U.up (fun _ h => h.2) h)⟩⟩
  | .disj a b, w => ⟨fun h => h.elim (fun x => U.up (fun _ h => Or.inl h) ((los a w).mp x))
        (fun y => U.up (fun _ h => Or.inr h) ((los b w).mp y)),
      fun h => (U.ultra (fun i => sat M R i a (fun n => w n i))).elim (fun ha => Or.inl ((los a w).mpr ha))
        (fun hna => Or.inr ((los b w).mpr (U.up (fun _ hh => hh.2.elim (fun x => (hh.1 x).elim) id) (U.inter hna h))))⟩
  | .imp a b, w => ⟨fun f => (U.ultra (fun i => sat M R i a (fun n => w n i))).elim
        (fun ha => U.up (fun _ hb _ => hb) ((los b w).mp (f ((los a w).mpr ha))))
        (fun hna => U.up (fun _ na x => (na x).elim) hna),
      fun h x => (los b w).mpr (U.up (fun _ hh => hh.1 hh.2) (U.inter h ((los a w).mp x)))⟩
  | .all a, w => ⟨fun f => (U.ultra (fun i => ∀ m, sat M R i a (ext (fun n => w n i) m))).elim id (fun hnq => by
        let P : (i : I) → M i → Prop := fun i m => ¬ sat M R i a (ext (fun n => w n i) m) ∨
          ∀ m', sat M R i a (ext (fun n => w n i) m')
        have hP : ∀ i, ∃ m, P i m := fun i =>
          (K.lem (∀ m', sat M R i a (ext (fun n => w n i) m'))).elim (fun hall => ⟨hne i, Or.inr hall⟩)
            (fun hn => dne K _ (fun hno => hn (fun m' => dne K _ (fun hs => hno ⟨m', Or.inl hs⟩))))
        let x : (i : I) → M i := fun i => K.ch (P i) (hP i)
        have hx : ∀ i, P i (x i) := fun i => K.ch_spec (P i) (hP i)
        have h1 := (los a (ext w x)).mp (f x)
        have h2 : U.mem (fun i => sat M R i a (ext (fun n => w n i) (x i))) :=
          U.up (fun i h => (sat_ext M R i a (ext_at M w x i)).mp h) h1
        exact (U.proper (U.up (fun i hh => (hx i).elim (fun hn => hn hh.2) (fun ha => hh.1 ha)) (U.inter hnq h2))).elim),
      fun h x => (los a (ext w x)).mpr (U.up (fun i hq => (sat_ext M R i a (ext_at M w x i)).mpr (hq (x i))) h)⟩
  | .ex a, w => ⟨fun ⟨x, hx⟩ => U.up (fun i h => ⟨x i, (sat_ext M R i a (ext_at M w x i)).mp h⟩) ((los a (ext w x)).mp hx),
      fun h => by
        let P : (i : I) → M i → Prop := fun i m => sat M R i a (ext (fun n => w n i) m) ∨
          ¬ ∃ m', sat M R i a (ext (fun n => w n i) m')
        have hP : ∀ i, ∃ m, P i m := fun i =>
          (K.lem (∃ m', sat M R i a (ext (fun n => w n i) m'))).elim (fun ⟨m', hm⟩ => ⟨m', Or.inl hm⟩)
            (fun hn => ⟨hne i, Or.inr hn⟩)
        let x : (i : I) → M i := fun i => K.ch (P i) (hP i)
        have hx : ∀ i, P i (x i) := fun i => K.ch_spec (P i) (hP i)
        refine ⟨x, (los a (ext w x)).mpr (U.up (fun i he => (sat_ext M R i a (ext_at M w x i)).mpr
          ((hx i).elim id (fun hn => (hn he).elim))) h)⟩⟩

end Los

end Found

#print axioms Found.Los.los

found/MAfail.lean

lean_all/found/MAfail.lean

/-
  T189 line. found/MAfail — the boundary of Martin's axiom: MA fails for
  continuum many dense sets.  For Cohen forcing (conditions (n, f): the first
  n bits of f; stronger = longer and agreeing), the dense sets
  D_n (length > n) and E_f (disagrees with f somewhere), indexed by
  ℕ ⊕ (ℕ → Bool), are met by no filter: the generic function would have to
  disagree with itself.  (MA_ℵ₀ holds: found/MartinAxiom.)  The bit read off
  the filter uses the choice of the classical point.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.ClassicalPoint

namespace Found

namespace MAF

open CP

/-- Cohen conditions: the first `n` bits of `f` -/
abbrev Cond := Nat × (Nat → Bool)

/-- q is stronger than p -/
def Le (q p : Cond) : Prop := p.1 ≤ q.1 ∧ ∀ i, i < p.1 → q.2 i = p.2 i

def Dn (n : Nat) (p : Cond) : Prop := n < p.1
def Ef (f : Nat → Bool) (p : Cond) : Prop := ∃ i, i < p.1 ∧ p.2 i ≠ f i

structure Filter' where
  G : Cond → Prop
  up : ∀ p q, G q → Le q p → G p
  dir : ∀ p q, G p → G q → ∃ r, G r ∧ Le r p ∧ Le r q

/-- no filter meets every D_n and every E_f -/
theorem ma_fails (K : Classical') (F : Filter') (hD : ∀ n, ∃ p, F.G p ∧ Dn n p)
    (hE : ∀ f, ∃ p, F.G p ∧ Ef f p) : False := by
  let g : Nat → Bool := fun n => (K.ch (fun p => F.G p ∧ Dn n p) (hD n)).2 n
  have hg : ∀ n, F.G (K.ch (fun p => F.G p ∧ Dn n p) (hD n)) ∧ Dn n (K.ch (fun p => F.G p ∧ Dn n p) (hD n)) :=
    fun n => K.ch_spec (fun p => F.G p ∧ Dn n p) (hD n)
  match hE g with
  | ⟨s, hs, i, hi, hne⟩ =>
    match F.dir s (K.ch (fun p => F.G p ∧ Dn i p) (hD i)) hs (hg i).1 with
    | ⟨r, _, hrs, hrt⟩ =>
      exact hne ((hrs.2 i hi).symm.trans (hrt.2 i (hg i).2))

end MAF

end Found

#print axioms Found.MAF.ma_fails

found/MLTTVariants.lean

lean_all/found/MLTTVariants.lean

/-
  T120. found/MLTTVariants — extensional versus intensional MLTT as two
  values of one condition (UIP) on the identity types of the CwF parameter.
  * Extensional: the family model (T152) — identity reflects, UIP holds.
  * Intensional: the groupoid (Hofmann–Streicher) reading of the universe —
    identities between types are equivalences (data), and UIP fails: Bool
    has two distinct self-identifications (identity and negation).
    Univalence makes these the paths of the universe (Agda: ua, T127).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.CwF

namespace Found

namespace MLTTV

universe u

structure Equiv (A B : Type u) where
  fwd : A → B
  bwd : B → A
  left : ∀ a, bwd (fwd a) = a
  right : ∀ b, fwd (bwd b) = b

def idE (A : Type u) : Equiv A A := ⟨fun a => a, fun a => a, fun _ => rfl, fun _ => rfl⟩

def notE : Equiv Bool Bool :=
  ⟨fun b => !b, fun b => !b, fun b => by cases b <;> rfl, fun b => by cases b <;> rfl⟩

/-- UIP fails for identities-as-equivalences -/
theorem not_uip : (idE Bool).fwd true ≠ notE.fwd true := fun e => Bool.noConfusion e

/-- while in the extensional family model all identity proofs agree (T152) -/
theorem uip_extensional {Γ : Type} (A : Γ → Type) (a b : (γ : Γ) → A γ)
    (h k : (γ : Γ) → CwF.Id A a b γ) (γ : Γ) : h γ = k γ := CwF.id_uip A a b h k γ

end MLTTV

end Found

#print axioms Found.MLTTV.not_uip
#print axioms Found.MLTTV.uip_extensional

found/MacLane.lean

lean_all/found/MacLane.lean

/-
  T163 / T146 / T162. found/MacLane — Zermelo-style points on the tree
  coordinate V, assembled from proved pieces (no re-proofs):
  * Mac Lane set theory (Z with Δ₀-separation): pairing, union, power,
    infinity (T88) + Δ₀-separation (T90) — the point equiconsistent with
    ETCS (line to T107, cited);
  * Zermelo Z: the same with full separation (T88) and, classically, LEM (T89);
  * Tarski–Grothendieck: every set of V.{0} lies in the universe U ∈ V.{1}
    (T104), which is closed under the operations.

  Lean 4 core only, hand-written proofs, no axioms.
  CIC dependence: impredicative Prop of T88/T104.
-/
import found.KP
import found.Grothendieck

namespace Found

namespace Zermelo

open WF

universe u

/-- the Mac Lane axioms hold in V (collected) -/
theorem maclane (a b x : V.{u}) (φ : KP.D0) (ρ : Nat → V.{u}) :
    (Mem x (IZF.pair a b) ↔ Eq' x a ∨ Eq' x b) ∧
    (Mem x (IZF.union a) ↔ ∃ y : V.{u}, Mem y a ∧ Mem x y) ∧
    (Mem x (IZF.power a) ↔ ∀ z : V.{u}, Mem z x → Mem z a) ∧
    (Mem IZF.empty IZF.omega ∧ ∀ y : V.{u}, Mem y IZF.omega → Mem (IZF.succ y) IZF.omega) ∧
    (Mem x (IZF.sep a (fun y => KP.sat φ (KP.cons y ρ))) ↔ Mem x a ∧ KP.sat φ (KP.cons x ρ)) :=
  ⟨IZF.pair_spec a b x, IZF.union_spec a x, IZF.power_spec a x, IZF.infinity, KP.d0_separation φ ρ a x⟩

/-- Zermelo: full separation for any respecting predicate -/
theorem zermelo_sep (a x : V.{u}) (φ : V.{u} → Prop) (hφ : IZF.Resp φ) :
    Mem x (IZF.sep a φ) ↔ Mem x a ∧ φ x := IZF.sep_spec a φ hφ x

/-- Tarski–Grothendieck: every set of V.{0} is (lifted) in the universe U -/
theorem tg (a : V.{0}) : Mem (Groth.lift a) Groth.U := ⟨a, eq_refl _⟩

end Zermelo

end Found

#print axioms Found.Zermelo.maclane
#print axioms Found.Zermelo.zermelo_sep
#print axioms Found.Zermelo.tg

found/ManySorted.lean

lean_all/found/ManySorted.lean

/-
  T170. found/ManySorted — many-sorted logic as a point: a family of
  carriers C : S → Type (a parameter); relativization to one sort.
  * Quantifying over a sort = quantifying over the total space Σ s, C s
    relativized by the sort predicate (both directions).
  * Henkin second-order logic (T142) is the two-sorted case: individuals
    and a family D₂ of predicates with an application relation.

  Lean 4 core only, hand-written proofs, no axioms.
-/

namespace Found

namespace MS

universe u v

variable {S : Type u} (C : S → Type v)

def Tot : Type (max u v) := (s : S) × C s

theorem relativize (s : S) (ψ : Tot C → Prop) :
    (∀ x : C s, ψ ⟨s, x⟩) ↔ (∀ y : Tot C, y.1 = s → ψ y) :=
  ⟨fun h y e => by
     cases y with
     | mk s' x =>
       cases e
       exact h x,
   fun h x => h ⟨s, x⟩ rfl⟩

theorem relativize_ex (s : S) (ψ : Tot C → Prop) :
    (∃ x : C s, ψ ⟨s, x⟩) ↔ (∃ y : Tot C, y.1 = s ∧ ψ y) :=
  ⟨fun ⟨x, h⟩ => ⟨⟨s, x⟩, rfl, h⟩,
   fun ⟨y, e, h⟩ => by
     cases y with
     | mk s' x =>
       cases e
       exact ⟨x, h⟩⟩

/-- Henkin structures as two-sorted structures: individuals, predicate
  objects, and application -/
structure Henkin2 where
  Ind : Type u
  Pr : Type u
  ap : Pr → Ind → Prop

/-- every Henkin second-order domain D₂ ⊆ (A → Prop) is a two-sorted structure -/
def ofD2 {A : Type u} (D2 : (A → Prop) → Prop) : Henkin2.{u} :=
  ⟨A, {P : A → Prop // D2 P}, fun P x => P.val x⟩

theorem ofD2_all {A : Type u} (D2 : (A → Prop) → Prop) (φ : (A → Prop) → Prop) :
    (∀ P, D2 P → φ P) ↔ (∀ P : (ofD2 D2).Pr, φ P.val) :=
  ⟨fun h P => h P.val P.property, fun h P hP => h ⟨P, hP⟩⟩

end MS

end Found

#print axioms Found.MS.relativize
#print axioms Found.MS.relativize_ex
#print axioms Found.MS.ofD2_all

found/ManyValued.lean

lean_all/found/ManyValued.lean

/-
  T71. found/ManyValued — many-valued and fuzzy logics as conditions.

  * Prelinearity (a → b) ∨ (b → a) = ⊤ on a Heyting algebra: Gödel–Dummett
    logic = intuitionistic logic + prelinearity, sound in prelinear Heyting
    algebras (e.g. every chain, the three-element one).
  * Intuitionistic logic does not prove prelinearity (five-element algebra
    0 < a, b < a ∨ b < 1).
  * Residuated models (T68): integral + idempotent ⊗ is a Heyting algebra;
    adding involution ¬¬a = a (MV) gives a Boolean algebra: MV + idempotent
    = classical.  Łukasiewicz Ł3 is MV, integral, prelinear, not idempotent.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Relevant
import found.Classical


universe u

namespace Found

namespace MV

open Prp

/-! ## Gödel–Dummett: prelinearity -/

def Prelinear (H : HA.{u}) : Prop := ∀ a b, H.le H.top (H.join (H.imp a b) (H.imp b a))

def prelin (p q : Form) : Form := .or (.imp p q) (.imp q p)

def PL (Γ : Form → Prop) : Form → Prop := fun x => Γ x ∨ ∃ p q, x = prelin p q

/-- Gödel–Dummett derivability -/
def GD (Γ : Form → Prop) (φ : Form) : Prop := PD true (PL Γ) φ

theorem gd_sound (H : HA.{u}) (hP : Prelinear H) {φ : Form} (d : GD (fun _ => False) φ)
    (v : Nat → H.car) : H.le H.top (interp H v H.bot φ) :=
  soundness H d v H.bot (fun _ => H.bot_le) H.top (fun _ k => k.elim (fun g => g.elim)
    (fun ⟨_, _, q⟩ => q ▸ hP _ _))

theorem three_prelinear : Prelinear Three.HA3 := fun a b => by cases a <;> cases b <;> exact trivial

/-- the five-element Heyting algebra 0 < a, b < c < 1 -/
inductive F5 where
  | zz : F5
  | a : F5
  | b : F5
  | c : F5
  | one : F5

namespace F5

def le : F5 → F5 → Prop
  | zz, zz => True
  | zz, a => True
  | zz, b => True
  | zz, c => True
  | zz, one => True
  | a, zz => False
  | a, a => True
  | a, b => False
  | a, c => True
  | a, one => True
  | b, zz => False
  | b, a => False
  | b, b => True
  | b, c => True
  | b, one => True
  | c, zz => False
  | c, a => False
  | c, b => False
  | c, c => True
  | c, one => True
  | one, zz => False
  | one, a => False
  | one, b => False
  | one, c => False
  | one, one => True

def meet : F5 → F5 → F5
  | zz, zz => zz
  | zz, a => zz
  | zz, b => zz
  | zz, c => zz
  | zz, one => zz
  | a, zz => zz
  | a, a => a
  | a, b => zz
  | a, c => a
  | a, one => a
  | b, zz => zz
  | b, a => zz
  | b, b => b
  | b, c => b
  | b, one => b
  | c, zz => zz
  | c, a => a
  | c, b => b
  | c, c => c
  | c, one => c
  | one, zz => zz
  | one, a => a
  | one, b => b
  | one, c => c
  | one, one => one

def join : F5 → F5 → F5
  | zz, zz => zz
  | zz, a => a
  | zz, b => b
  | zz, c => c
  | zz, one => one
  | a, zz => a
  | a, a => a
  | a, b => c
  | a, c => c
  | a, one => one
  | b, zz => b
  | b, a => c
  | b, b => b
  | b, c => c
  | b, one => one
  | c, zz => c
  | c, a => c
  | c, b => c
  | c, c => c
  | c, one => one
  | one, zz => one
  | one, a => one
  | one, b => one
  | one, c => one
  | one, one => one

def imp : F5 → F5 → F5
  | zz, zz => one
  | zz, a => one
  | zz, b => one
  | zz, c => one
  | zz, one => one
  | a, zz => b
  | a, a => one
  | a, b => b
  | a, c => one
  | a, one => one
  | b, zz => a
  | b, a => a
  | b, b => one
  | b, c => one
  | b, one => one
  | c, zz => zz
  | c, a => a
  | c, b => b
  | c, c => one
  | c, one => one
  | one, zz => zz
  | one, a => a
  | one, b => b
  | one, c => c
  | one, one => one

def HA5 : HA.{0} where
  car := F5
  le := le
  le_refl x := by cases x <;> exact trivial
  le_trans {x y z} := by
    cases x <;> cases y <;> cases z <;> intro p q <;> first | exact trivial | exact p.elim | exact q.elim
  top := one
  bot := zz
  meet := meet
  join := join
  imp := imp
  le_top x := by cases x <;> exact trivial
  bot_le x := by cases x <;> exact trivial
  meet_l x y := by cases x <;> cases y <;> exact trivial
  meet_r x y := by cases x <;> cases y <;> exact trivial
  meet_univ {x y z} := by
    cases x <;> cases y <;> cases z <;> intro p q <;> first | exact trivial | exact p.elim | exact q.elim
  join_l x y := by cases x <;> cases y <;> exact trivial
  join_r x y := by cases x <;> cases y <;> exact trivial
  join_univ {x y z} := by
    cases x <;> cases y <;> cases z <;> intro p q <;> first | exact trivial | exact p.elim | exact q.elim
  imp_adj {x y z} := by cases x <;> cases y <;> cases z <;> exact ⟨fun p => p, fun p => p⟩

end F5

theorem f5_not_prelinear : ¬ Prelinear F5.HA5 := fun h => h F5.a F5.b

/-- intuitionistic logic does not prove prelinearity -/
theorem int_not_prelin : ¬ PD true (fun _ => False) (prelin (.var 0) (.var 1)) :=
  fun d => valid F5.HA5 d (fun n => match n with | 0 => F5.a | _ => F5.b) F5.zz
    (fun _ x => by cases x <;> exact trivial)

/-- Gödel–Dummett proves prelinearity -/
theorem gd_prelin (p q : Form) : GD (fun _ => False) (prelin p q) := .hyp (Or.inr ⟨p, q, rfl⟩)

/-! ## Residuated side: MV, idempotence, Boolean -/

open Lin

def Integral (M : CRL) : Prop := ∀ a, M.le a M.one
def Idempotent (M : CRL) : Prop := ∀ a, M.le a (M.tens a a)
def Involutive (M : CRL) : Prop := ∀ a, M.le (M.lolli (M.lolli a M.zero) M.zero) a

theorem integral_weakening (M : CRL) (hI : Integral M) : Weakening M :=
  fun a b => M.le_trans (M.tens_mono (M.le_refl a) (hI b))
    (M.le_trans (M.tens_comm _ _) (M.unit1 a))

/-- integral + idempotent: a Heyting algebra (Gödel side) -/
def toHA' (M : CRL.{u}) (hI : Integral M) (hE : Idempotent M) : HA.{u} :=
  toHA M (integral_weakening M hI) hE

/-- MV + idempotent = Boolean: the classical corner -/
theorem mv_idem_boolean (M : CRL) (hI : Integral M) (hE : Idempotent M) (hV : Involutive M) :
    Boolean (toHA' M hI hE) := hV

theorem L3_integral : Integral L3.M3 := fun a => by cases a <;> rfl
theorem L3_involutive : Involutive L3.M3 := fun a => by cases a <;> rfl
theorem L3_not_idempotent : ¬ Idempotent L3.M3 := L3_not_contraction

/-- prelinearity in a residuated model: 1 ≤ (a ⊸ b) ∨ (b ⊸ a) -/
def RPrelinear (M : CRL) : Prop := ∀ a b, M.le M.one (M.pls (M.lolli a b) (M.lolli b a))

theorem L3_prelinear : RPrelinear L3.M3 := fun a b => by cases a <;> cases b <;> rfl

end MV

end Found

#print axioms Found.MV.gd_sound
#print axioms Found.MV.three_prelinear
#print axioms Found.MV.F5.HA5
#print axioms Found.MV.f5_not_prelinear
#print axioms Found.MV.int_not_prelin
#print axioms Found.MV.gd_prelin
#print axioms Found.MV.integral_weakening
#print axioms Found.MV.toHA'
#print axioms Found.MV.mv_idem_boolean
#print axioms Found.MV.L3_integral
#print axioms Found.MV.L3_involutive
#print axioms Found.MV.L3_not_idempotent
#print axioms Found.MV.L3_prelinear

found/Markov.lean

lean_all/found/Markov.lean

/-
  T198. found/Markov — Russian constructivism as a point: Markov's principle
  as a condition on the truth-value coordinate (for decidable predicates on
  ℕ, the core's NNO).
  * MP: P decidable ∧ ¬¬∃n P n ⇒ ∃n P n.
  * LEM ⇒ MP; MP ⟺ ¬¬-stability of Σ⁰₁ statements (decidable P).
  * Kleene realizability over K1 validates MP (cited; T96b) — the line
    Russian constructivism ↔ effective topos; Brouwer (T197) rejects it.

  Lean 4 core only, hand-written proofs, no axioms (MP, LEM are hypotheses).
-/

namespace Found

namespace Markov

def Dec (P : Nat → Prop) : Prop := ∀ n, P n ∨ ¬ P n

def MP : Prop := ∀ P : Nat → Prop, Dec P → ¬ ¬ (∃ n, P n) → ∃ n, P n

theorem lem_mp (lem : ∀ p : Prop, p ∨ ¬ p) : MP :=
  fun P _ h => (lem (∃ n, P n)).elim id (fun n => (h n).elim)

def Sigma1Stable : Prop := ∀ P : Nat → Prop, Dec P → (¬ ¬ ∃ n, P n) → ∃ n, P n

theorem mp_iff : MP ↔ Sigma1Stable := Iff.rfl

/-- MP gives: a decidable predicate that is not everywhere false has a witness -/
theorem mp_not_all (hm : MP) (P : Nat → Prop) (hd : Dec P) (h : ¬ ∀ n, ¬ P n) : ∃ n, P n :=
  hm P hd (fun ne => h (fun n hn => ne ⟨n, hn⟩))

end Markov

end Found

#print axioms Found.Markov.lem_mp
#print axioms Found.Markov.mp_iff
#print axioms Found.Markov.mp_not_all

found/MarkovK1.lean

lean_all/found/MarkovK1.lean

/-
  T198 line. found/MarkovK1 — Markov's principle is realized in Kleene's
  first algebra (found/EffectiveK1): for a decidable predicate given by a
  program e (always answering 0 or 1), the μ-search program μn.e(n) is a
  uniform realizer of ¬¬∃n (e n = 0) → ∃n (e n = 0).  The metatheory step
  ¬¬∃ → ∃ is taken in the classical point (found/ClassicalPoint), as for the
  effective topos.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.EffectiveK1
import found.ClassicalPoint

namespace Found

namespace MK

open CT3 CP

/-- the uniform realizer: search for the first n with e(n) = 0 -/
def mpProg (e : MR) : MR := .mu (.comp e [.proj 0])

theorem ev_f (e : MR) (j v : Nat) (h : Ev e [j] v) : Ev (.comp e [.proj 0]) (j :: []) v :=
  .comp (.cons (.proj 0 [j]) (.nil _)) h

theorem search (e : MR) (hdec : ∀ n, Ev e [n] 0 ∨ Ev e [n] 1) :
    (k j : Nat) → Ev e [j + k] 0 → ∃ m, MuS (.comp e [.proj 0]) [] j m ∧ Ev e [m] 0
  | 0, j, h => ⟨j, .stop (ev_f e j 0 h), h⟩
  | k + 1, j, h => (hdec j).elim
      (fun h0 => ⟨j, .stop (ev_f e j 0 h0), h0⟩)
      (fun h1 => match search e hdec k (j + 1) (by rw [Nat.add_right_comm, Nat.add_assoc]; exact h) with
        | ⟨m, ms, hm⟩ => ⟨m, .next (ev_f e j 1 h1) ms, hm⟩)

/-- Markov's principle, realized: the search program halts at a witness -/
theorem mp_realized (K : Classical') (e : MR) (hdec : ∀ n, Ev e [n] 0 ∨ Ev e [n] 1)
    (h : ¬ ¬ ∃ n, Ev e [n] 0) : ∃ m, Ev (mpProg e) [] m ∧ Ev e [m] 0 :=
  match dne K _ h with
  | ⟨n, hn⟩ => match search e hdec n 0 (by rw [Nat.zero_add]; exact hn) with
    | ⟨m, ms, hm⟩ => ⟨m, .mu ms, hm⟩

end MK

end Found

#print axioms Found.MK.search
#print axioms Found.MK.mp_realized

found/MartinAxiom.lean

lean_all/found/MartinAxiom.lean

/-
  T189. found/MartinAxiom — Martin's axiom as a point: the
  Rasiowa–Sikorski lemma (its countable, provable case) on a poset of
  conditions (the forcing coordinate, T108):
  * given dense sets Dₙ with explicit extension maps eₙ (eₙ p ≤ p,
    eₙ p ∈ Dₙ), the recursive sequence pₙ₊₁ = eₙ pₙ is descending and meets
    every Dₙ; its upward closure is a filter meeting all Dₙ (generic for the
    family).
  MA_κ (κ < 𝔠 dense sets, ccc posets) is the condition beyond the countable
  case (cited); V = L and GCH are T169.

  Lean 4 core only, hand-written proofs, no axioms.
-/

namespace Found

namespace MA

universe u

variable {P : Type u} (le : P → P → Prop) (refl : ∀ p, le p p)
  (trans : ∀ {a b c}, le a b → le b c → le a c)

def seq (e : Nat → P → P) (p0 : P) : Nat → P
  | 0 => p0
  | n + 1 => e n (seq e p0 n)

theorem descending (e : Nat → P → P) (he : ∀ n p, le (e n p) p) (p0 : P) :
    ∀ n, le (seq e p0 (n + 1)) (seq e p0 n) := fun n => he n _

include refl trans in
theorem desc_le (e : Nat → P → P) (he : ∀ n p, le (e n p) p) (p0 : P) :
    ∀ n k, le (seq e p0 (n + k)) (seq e p0 n)
  | _, 0 => refl _
  | n, k + 1 => trans (he _ _) (desc_le e he p0 n k)

/-- the generic filter: conditions above some element of the sequence -/
def G (e : Nat → P → P) (p0 : P) (q : P) : Prop := ∃ n, le (seq e p0 n) q

include refl in
theorem meets (D : Nat → P → Prop) (e : Nat → P → P) (hD : ∀ n p, D n (e n p)) (p0 : P) (n : Nat) :
    ∃ q, D n q ∧ G le e p0 q := ⟨seq e p0 (n + 1), hD n _, n + 1, refl _⟩

include refl trans in
/-- G is a filter: upward closed and any two members have a common lower member in G -/
theorem filter_directed (e : Nat → P → P) (he : ∀ n p, le (e n p) p) (p0 : P) {q r : P}
    (hq : G le e p0 q) (hr : G le e p0 r) : ∃ s, G le e p0 s ∧ le s q ∧ le s r :=
  match hq, hr with
  | ⟨n, hn⟩, ⟨m, hm⟩ => ⟨seq e p0 (n + m), ⟨n + m, refl _⟩,
      trans (desc_le le refl trans e he p0 n m) hn,
      trans (by rw [Nat.add_comm]; exact desc_le le refl trans e he p0 m n) hm⟩

include trans in
theorem filter_up (e : Nat → P → P) (p0 : P) {q r : P} (hq : G le e p0 q) (h : le q r) : G le e p0 r :=
  match hq with
  | ⟨n, hn⟩ => ⟨n, trans hn h⟩

end MA

end Found

#print axioms Found.MA.desc_le
#print axioms Found.MA.meets
#print axioms Found.MA.filter_directed
#print axioms Found.MA.filter_up

found/MeasStrong.lean

lean_all/found/MeasStrong.lean

/-
  T188 line. found/MeasStrong — in the classical point, a measurable κ
  (a κ-complete nonprincipal ultrafilter, found/Measurable) is a strong
  limit: for every λ smaller than κ, κ does not inject (extensionally) into
  the subsets 2^λ.  With regularity (found/Measurable) a measurable κ is
  therefore a regular strong limit — inaccessible when uncountable — the
  first step of the consistency-strength ordering of large cardinals
  (measurable above inaccessible).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Measurable

namespace Found

namespace MeasS

open CP NFP Meas

variable (K : Classical') {κ : Type} (U : Measure κ)

include K U in
/-- measurable cardinals are strong limits -/
theorem strong_limit (Lam : Type) (hL : Smaller Lam κ) :
    ¬ ∃ f : κ → (Lam → Bool), ∀ x y, (∀ a, f x a = f y a) → x = y := fun ⟨f, hf⟩ => by
  -- the majority value at every coordinate
  let maj : Lam → Bool := fun a => dec K (U.mem (fun ξ => f ξ a = true))
  have hA : ∀ a, U.mem (fun ξ => f ξ a = maj a) := fun a => by
    cases hd : dec K (U.mem (fun ξ => f ξ a = true)) with
    | true =>
      have hm := (dec_spec K _).1 hd
      exact U.up (fun ξ h => by show f ξ a = dec K _; rw [hd]; exact h) hm
    | false =>
      have hn : ¬ U.mem (fun ξ => f ξ a = true) := fun h => Bool.noConfusion (hd.symm.trans ((dec_spec K _).2 h))
      have hc := (U.ultra (fun ξ => f ξ a = true)).elim (fun h => (hn h).elim) id
      exact U.up (fun ξ h => by
        show f ξ a = dec K _
        rw [hd]
        cases hfx : f ξ a with
        | true => exact (h hfx).elim
        | false => rfl) hc
  have hC := U.complete Lam (fun a ξ => f ξ a = maj a) hL hA
  have ne : ∃ ξ, ∀ a, f ξ a = maj a := dne K _ (fun hn => U.proper (U.up (fun ξ h => hn ⟨ξ, h⟩) hC))
  match ne with
  | ⟨ξ, hξ⟩ => exact U.nonprincipal ξ (U.up (fun η h => hf _ _ (fun a => (h a).trans (hξ a).symm)) hC)

end MeasS

end Found

#print axioms Found.MeasS.strong_limit

found/Measurable.lean

lean_all/found/Measurable.lean

/-
  T188 line. found/Measurable — a specific large cardinal as a condition on
  a type κ, in the classical point: κ is measurable when it carries a
  κ-complete nonprincipal ultrafilter.  Theorem: then every subset smaller
  than κ has measure zero, and κ is regular in the cardinal sense (fewer
  than κ sets, each smaller than κ, never cover κ).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.NFParams

namespace Found

namespace Meas

open CP NFP

variable (K : Classical') (κ : Type)

structure Measure where
  mem : (κ → Prop) → Prop
  up : ∀ {A B : κ → Prop}, (∀ x, A x → B x) → mem A → mem B
  inter : ∀ {A B : κ → Prop}, mem A → mem B → mem (fun x => A x ∧ B x)
  proper : ¬ mem (fun _ => False)
  ultra : ∀ A, mem A ∨ mem (fun x => ¬ A x)
  /-- κ-completeness: intersections of fewer than κ members are members -/
  complete : ∀ (I : Type) (A : I → κ → Prop), Smaller I κ → (∀ i, mem (A i)) → mem (fun x => ∀ i, A i x)
  /-- nonprincipal -/
  nonprincipal : ∀ x, ¬ mem (fun y => y = x)

variable {κ} (U : Measure κ)

/-- small sets have measure zero -/
theorem small_null (S : κ → Prop) (hS : Smaller {x // S x} κ) : ¬ U.mem S := fun hm => by
  have hc : U.mem (fun y => ∀ i : {x // S x}, ¬ (y = i.val)) :=
    U.complete _ (fun i y => ¬ (y = i.val)) hS (fun i => (U.ultra (fun y => y = i.val)).elim
      (fun h => (U.nonprincipal i.val h).elim) id)
  exact U.proper (U.up (fun y h => h.2 ⟨y, h.1⟩ rfl) (U.inter hm hc))

include K U in
/-- a measurable cardinal is regular: fewer than κ small sets never cover κ -/
theorem regular (I : Type) (hI : Smaller I κ) (A : I → κ → Prop) (hA : ∀ i, Smaller {x // A i x} κ) :
    ∃ x, ∀ i, ¬ A i x := by
  have hc : U.mem (fun x => ∀ i, ¬ A i x) :=
    U.complete I (fun i x => ¬ A i x) hI (fun i => (U.ultra (A i)).elim (fun h => (small_null U (A i) (hA i) h).elim) id)
  exact dne K _ (fun hn => U.proper (U.up (fun x hx => hn ⟨x, hx⟩) hc))

end Meas

end Found

#print axioms Found.Meas.small_null
#print axioms Found.Meas.regular

found/MereoBack.lean

lean_all/found/MereoBack.lean

/-
  T201 line, converse. found/MereoBack — Tarski: classical extensional
  mereology (partial order, strong supplementation, fusions of inhabited
  families) with an added bottom element is a complete Boolean algebra
  (the structure CBA of found/MereoCBA), in the classical point.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.MereoCBA

namespace Found

namespace MB

open CP MCBA

structure CEM where
  E : Type
  P : E → E → Prop
  refl : ∀ x, P x x
  trans : ∀ {x y z}, P x y → P y z → P x z
  antisymm : ∀ {x y}, P x y → P y x → x = y
  /-- strong supplementation -/
  ssp : ∀ x y, (∀ z, P z x → ∃ w, P w z ∧ P w y) → P x y
  fus : (S : E → Prop) → (∃ x, S x) → E
  fus_ub : ∀ S h x, S x → P x (fus S h)
  fus_least : ∀ S h y, (∀ x, S x → P x y) → P (fus S h) y
  /-- fusion in the overlap sense: what overlaps the fusion overlaps a member -/
  fus_ov : ∀ S h y, (∃ w, P w y ∧ P w (fus S h)) → ∃ x, S x ∧ ∃ w, P w y ∧ P w x

variable (K : Classical') (M : CEM)

/-- the order on E with a bottom added -/
def Le : Option M.E → Option M.E → Prop
  | none, _ => True
  | some _, none => False
  | some x, some y => M.P x y

theorem le_refl (a : Option M.E) : Le M a a := by cases a with | none => trivial | some x => exact M.refl x

theorem le_trans {a b c : Option M.E} (h1 : Le M a b) (h2 : Le M b c) : Le M a c := by
  cases a with
  | none => trivial
  | some x => cases b with
    | none => exact h1.elim
    | some y => cases c with
      | none => exact h2.elim
      | some z => exact M.trans h1 h2

theorem le_antisymm {a b : Option M.E} (h1 : Le M a b) (h2 : Le M b a) : a = b := by
  cases a with
  | none => cases b with
    | none => rfl
    | some _ => exact h2.elim
  | some x => cases b with
    | none => exact h1.elim
    | some y => exact congrArg some (M.antisymm h1 h2)

def IsSup (S : Option M.E → Prop) (o : Option M.E) : Prop :=
  (∀ a, S a → Le M a o) ∧ (∀ c, (∀ a, S a → Le M a c) → Le M o c)

include K in
theorem sup_ex (S : Option M.E → Prop) : ∃ o, IsSup M S o :=
  (K.lem (∃ x, S (some x))).elim
    (fun h => ⟨some (M.fus (fun x => S (some x)) h),
      fun a ha => by
        cases a with
        | none => trivial
        | some x => exact M.fus_ub _ h x ha,
      fun c hc => by
        cases c with
        | none => match h with | ⟨x, hx⟩ => exact (hc (some x) hx).elim
        | some y => exact M.fus_least _ h y (fun x hx => hc (some x) hx)⟩)
    (fun hn => ⟨none, fun a ha => by
        cases a with
        | none => trivial
        | some x => exact (hn ⟨x, ha⟩).elim,
      fun _ _ => trivial⟩)

/-- suprema, chosen -/
def sup (S : Option M.E → Prop) : Option M.E := K.ch (IsSup M S) (sup_ex K M S)

theorem sup_spec (S : Option M.E → Prop) : IsSup M S (sup K M S) := K.ch_spec (IsSup M S) (sup_ex K M S)

def bot : Option M.E := none
def top : Option M.E := sup K M (fun _ => True)
def join (a b : Option M.E) : Option M.E := sup K M (fun c => c = a ∨ c = b)
def meet (a b : Option M.E) : Option M.E := sup K M (fun c => Le M c a ∧ Le M c b)
def neg (a : Option M.E) : Option M.E := sup K M (fun c => Le M (meet K M c a) none)

def isS : Option M.E → Bool
  | some _ => true
  | none => false

theorem some_ne (x : M.E) : (some x : Option M.E) ≠ none := fun e => Bool.noConfusion (congrArg (isS M) e)

def Ov (a b : Option M.E) : Prop := ∃ w, w ≠ none ∧ Le M w a ∧ Le M w b

theorem ne_none_some {w : Option M.E} (h : w ≠ none) : ∃ x, w = some x := by
  cases w with
  | none => exact (h rfl).elim
  | some x => exact ⟨x, rfl⟩

/-- strong supplementation, lifted -/
theorem ssp' (a b : Option M.E) (h : ∀ z, z ≠ none → Le M z a → Ov M z b) : Le M a b := by
  cases a with
  | none => trivial
  | some x =>
    cases b with
    | none =>
      match h (some x) (some_ne M x) (M.refl x) with
      | ⟨w, hw, _, hwb⟩ => match ne_none_some M hw with
        | ⟨w', e⟩ => rw [e] at hwb; exact hwb.elim
    | some y =>
      exact M.ssp x y (fun z hz => match h (some z) (some_ne M z) hz with
        | ⟨w, hw, h1, h2⟩ => match ne_none_some M hw with
          | ⟨w', e⟩ => by rw [e] at h1 h2; exact ⟨w', h1, h2⟩)

/-- overlapping a supremum means overlapping a member -/
theorem ov_sup (S : Option M.E → Prop) (z : Option M.E) (hz : Ov M z (sup K M S)) : ∃ c, S c ∧ Ov M z c := by
  refine dne K _ (fun hn => ?_)
  rcases K.lem (∃ x, S (some x)) with hinh | hno
  · have e : sup K M S = some (M.fus (fun x => S (some x)) hinh) := by
      refine le_antisymm M ((sup_spec K M S).2 _ (fun a ha => ?_)) ?_
      · cases a with
        | none => trivial
        | some x => exact M.fus_ub _ hinh x ha
      · cases h : sup K M S with
        | none =>
          match hinh with
          | ⟨x, hx⟩ => have := (sup_spec K M S).1 (some x) hx; rw [h] at this; exact this.elim
        | some y => exact M.fus_least _ hinh y (fun x hx => by
            have := (sup_spec K M S).1 (some x) hx; rw [h] at this; exact this)
    rw [e] at hz
    match hz with
    | ⟨w, hw, h1, h2⟩ => match ne_none_some M hw with
      | ⟨w', ew⟩ =>
        rw [ew] at h1 h2
        cases z with
        | none => exact h1.elim
        | some z' =>
          match M.fus_ov _ hinh z' ⟨w', h1, h2⟩ with
          | ⟨x, hx, w2, hw1, hw2⟩ => exact hn ⟨some x, hx, some w2, some_ne M w2, hw1, hw2⟩
  · match hz with
    | ⟨w, hw, _, h2⟩ =>
      have hle : Le M (sup K M S) none := (sup_spec K M S).2 none (fun a ha => by
        cases a with
        | none => trivial
        | some x => exact (hno ⟨x, ha⟩).elim)
      match ne_none_some M hw with
      | ⟨w', ew⟩ => rw [ew] at h2; exact (le_trans M h2 hle).elim

theorem meet_l (a b : Option M.E) : Le M (meet K M a b) a := (sup_spec K M _).2 a (fun _ h => h.1)
theorem meet_r (a b : Option M.E) : Le M (meet K M a b) b := (sup_spec K M _).2 b (fun _ h => h.2)
theorem meet_univ {a b c : Option M.E} (h1 : Le M c a) (h2 : Le M c b) : Le M c (meet K M a b) :=
  (sup_spec K M _).1 c ⟨h1, h2⟩
theorem join_l (a b : Option M.E) : Le M a (join K M a b) := (sup_spec K M _).1 a (Or.inl rfl)
theorem join_r (a b : Option M.E) : Le M b (join K M a b) := (sup_spec K M _).1 b (Or.inr rfl)
theorem join_univ {a b c : Option M.E} (h1 : Le M a c) (h2 : Le M b c) : Le M (join K M a b) c :=
  (sup_spec K M _).2 c (fun _ hx => hx.elim (fun e => e ▸ h1) (fun e => e ▸ h2))

theorem ov_self (z : Option M.E) (hz : z ≠ none) : Ov M z z := ⟨z, hz, le_refl M z, le_refl M z⟩

theorem ov_mono {z a b : Option M.E} (h : Ov M z a) (hab : Le M a b) : Ov M z b :=
  match h with | ⟨w, hw, h1, h2⟩ => ⟨w, hw, h1, le_trans M h2 hab⟩

theorem meet_neg (a : Option M.E) : Le M (meet K M a (neg K M a)) none := by
  refine dne K _ (fun hn => ?_)
  have hm : meet K M a (neg K M a) ≠ none := fun e => hn (by rw [e]; trivial)
  have hov : Ov M (meet K M a (neg K M a)) (neg K M a) := ov_mono M (ov_self M _ hm) (meet_r K M _ _)
  match ov_sup K M _ _ hov with
  | ⟨c, hc, w, hw, h1, h2⟩ =>
    exact hw (le_antisymm M (le_trans M (meet_univ K M h2 (le_trans M h1 (meet_l K M _ _))) hc) trivial)

theorem join_neg (a : Option M.E) : Le M (top K M) (join K M a (neg K M a)) := by
  refine ssp' M _ _ (fun z hz _ => ?_)
  rcases K.lem (Ov M z a) with ho | hno
  · exact ov_mono M ho (join_l K M _ _)
  · have hzn : Le M z (neg K M a) := (sup_spec K M _).1 z (dne K _ (fun hm => hno
      ⟨meet K M z a, fun e => hm (by rw [e]; trivial), meet_l K M _ _, meet_r K M _ _⟩))
    exact ov_mono M (ov_self M z hz) (le_trans M hzn (join_r K M _ _))

theorem distrib (a b c : Option M.E) :
    Le M (meet K M a (join K M b c)) (join K M (meet K M a b) (meet K M a c)) := by
  refine ssp' M _ _ (fun z hz hzl => ?_)
  have hza : Le M z a := le_trans M hzl (meet_l K M _ _)
  have hzj : Le M z (join K M b c) := le_trans M hzl (meet_r K M _ _)
  match ov_sup K M _ z (ov_mono M (ov_self M z hz) hzj) with
  | ⟨d, hd, w, hw, h1, h2⟩ =>
    have hwa : Le M w a := le_trans M h1 hza
    exact hd.elim
      (fun e => ⟨w, hw, h1, le_trans M (meet_univ K M hwa (e ▸ h2)) (join_l K M _ _)⟩)
      (fun e => ⟨w, hw, h1, le_trans M (meet_univ K M hwa (e ▸ h2)) (join_r K M _ _)⟩)

/-- Tarski: classical extensional mereology plus a bottom is a complete Boolean algebra -/
def toCBA : CBA where
  car := Option M.E
  le := Le M
  refl := le_refl M
  trans := le_trans M
  antisymm := le_antisymm M
  bot := none
  top := top K M
  meet := meet K M
  join := join K M
  neg := neg K M
  bot_le _ := trivial
  meet_l := meet_l K M
  meet_r := meet_r K M
  meet_univ := meet_univ K M
  join_l := join_l K M
  join_r := join_r K M
  join_univ := join_univ K M
  distrib := distrib K M
  meet_neg := meet_neg K M
  join_neg := join_neg K M
  le_top a := (sup_spec K M _).1 a trivial
  sup := sup K M
  sup_ub S a h := (sup_spec K M S).1 a h
  sup_least S c h := (sup_spec K M S).2 c h

end MB

end Found

#print axioms Found.MB.ov_sup
#print axioms Found.MB.toCBA

found/MereoCBA.lean

lean_all/found/MereoCBA.lean

/-
  T201 line. found/MereoCBA — Tarski: the nonzero elements of a complete
  Boolean algebra form a model of classical extensional mereology
  (parthood = ≤, antisymmetric; strong supplementation; every inhabited
  family has a fusion = its supremum).  Strong supplementation uses the
  classical point (found/ClassicalPoint) for "x ∧ ¬y is not nonzero ⟹ it is 0".

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Mereology
import found.ClassicalPoint

namespace Found

namespace MCBA

open CP Mereo

structure CBA where
  car : Type
  le : car → car → Prop
  refl : ∀ a, le a a
  trans : ∀ {a b c}, le a b → le b c → le a c
  antisymm : ∀ {a b}, le a b → le b a → a = b
  bot : car
  top : car
  meet : car → car → car
  join : car → car → car
  neg : car → car
  bot_le : ∀ a, le bot a
  meet_l : ∀ a b, le (meet a b) a
  meet_r : ∀ a b, le (meet a b) b
  meet_univ : ∀ {a b c}, le c a → le c b → le c (meet a b)
  join_l : ∀ a b, le a (join a b)
  join_r : ∀ a b, le b (join a b)
  join_univ : ∀ {a b c}, le a c → le b c → le (join a b) c
  distrib : ∀ a b c, le (meet a (join b c)) (join (meet a b) (meet a c))
  meet_neg : ∀ a, le (meet a (neg a)) bot
  join_neg : ∀ a, le top (join a (neg a))
  le_top : ∀ a, le a top
  sup : (car → Prop) → car
  sup_ub : ∀ S a, S a → le a (sup S)
  sup_least : ∀ S c, (∀ a, S a → le a c) → le (sup S) c

variable (B : CBA)

/-- x ∧ ¬y = 0 gives x ≤ y -/
theorem le_of_meet_neg {x y : B.car} (h : B.le (B.meet x (B.neg y)) B.bot) : B.le x y := by
  have h1 : B.le x (B.meet x (B.join y (B.neg y))) := B.meet_univ (B.refl x) (B.trans (B.le_top x) (B.join_neg y))
  have h2 := B.distrib x y (B.neg y)
  exact B.trans h1 (B.trans h2 (B.join_univ (B.meet_r x y) (B.trans h (B.bot_le y))))

/-- the positive part as a parthood structure -/
def pos : Parthood where
  E := {x : B.car // x ≠ B.bot}
  P x y := B.le x.val y.val
  refl x := B.refl x.val
  trans h k := B.trans h k

theorem pos_antisymm (x y : (pos B).E) (h1 : (pos B).P x y) (h2 : (pos B).P y x) : x.val = y.val :=
  B.antisymm h1 h2

/-- strong supplementation (classical point) -/
theorem pos_supp (K : Classical') (x y : (pos B).E) (h : ∀ z, (pos B).P z x → Ov (pos B) z y) :
    (pos B).P x y := by
  refine le_of_meet_neg B (dne K _ (fun hn => ?_))
  have hz : B.meet x.val (B.neg y.val) ≠ B.bot := fun e => hn (by rw [e]; exact B.refl _)
  match h ⟨_, hz⟩ (B.meet_l _ _) with
  | ⟨w, hw1, hw2⟩ =>
    have hw : B.le w.val B.bot :=
      B.trans (B.meet_univ (B.trans hw1 (B.meet_r _ _)) hw2) (B.trans (B.meet_univ (B.meet_r _ _) (B.meet_l _ _)) (B.meet_neg y.val))
    exact w.property (B.antisymm hw (B.bot_le _))

theorem le_of_eq' {a b : B.car} (e : a = b) : B.le a b := e ▸ B.refl a

def Fam (S : (pos B).E → Prop) : B.car → Prop := fun a => ∃ x : (pos B).E, S x ∧ x.val = a

/-- fusion: an inhabited family of positive elements has its supremum as least upper bound -/
def fusion (S : (pos B).E → Prop) (x0 : (pos B).E) (h0 : S x0) : (pos B).E :=
  ⟨B.sup (Fam B S), fun e =>
    x0.property (B.antisymm (B.trans (B.sup_ub (Fam B S) x0.val ⟨x0, h0, rfl⟩) (le_of_eq' B e)) (B.bot_le _))⟩

theorem fusion_ub (S : (pos B).E → Prop) (x0 : (pos B).E) (h0 : S x0) (x : (pos B).E) (hx : S x) :
    (pos B).P x (fusion B S x0 h0) := by
  show B.le x.val (B.sup (Fam B S))
  exact B.sup_ub _ _ ⟨x, hx, rfl⟩

theorem fusion_least (S : (pos B).E → Prop) (x0 : (pos B).E) (h0 : S x0) (y : (pos B).E)
    (h : ∀ x, S x → (pos B).P x y) : (pos B).P (fusion B S x0 h0) y := by
  show B.le (B.sup (Fam B S)) y.val
  exact B.sup_least _ _ (fun a ⟨x, hx, e⟩ => by rw [← e]; exact h x hx)

/-- disjoint from z means below ¬z -/
theorem le_neg_of_disj {x z : B.car} (h : B.le (B.meet x z) B.bot) : B.le x (B.neg z) := by
  have h1 : B.le x (B.meet x (B.join z (B.neg z))) := B.meet_univ (B.refl x) (B.trans (B.le_top x) (B.join_neg z))
  exact B.trans h1 (B.trans (B.distrib x z (B.neg z)) (B.join_univ (B.trans h (B.bot_le _)) (B.meet_r _ _)))

/-- fusion in the overlap sense (classical point): what overlaps the fusion overlaps a member -/
theorem fusion_ov (K : Classical') (S : (pos B).E → Prop) (x0 : (pos B).E) (h0 : S x0) (y : (pos B).E)
    (h : Ov (pos B) y (fusion B S x0 h0)) : ∃ x, S x ∧ Ov (pos B) y x := by
  refine dne K _ (fun hn => ?_)
  have hall : ∀ x, S x → B.le x.val (B.neg y.val) := fun x hx => le_neg_of_disj B (dne K _ (fun hne => hn
    ⟨x, hx, ⟨B.meet x.val y.val, fun e => hne (le_of_eq' B e)⟩, B.meet_r _ _, B.meet_l _ _⟩))
  have hsup : B.le (fusion B S x0 h0).val (B.neg y.val) :=
    B.sup_least _ _ (fun a ⟨x, hx, e⟩ => by rw [← e]; exact hall x hx)
  match h with
  | ⟨w, hw1, hw2⟩ =>
    have hb : B.le w.val B.bot := B.trans (B.meet_univ hw1 (B.trans hw2 hsup)) (B.meet_neg y.val)
    exact w.property (B.antisymm hb (B.bot_le _))

end MCBA

end Found

#print axioms Found.MCBA.le_of_meet_neg
#print axioms Found.MCBA.pos_supp
#print axioms Found.MCBA.fusion_least
#print axioms Found.MCBA.fusion_ov

found/Mereology.lean

lean_all/found/Mereology.lean

/-
  T201. found/Mereology — mereology as a point: a parthood preorder (free
  parameter) with overlap, supplementation and fusion.
  * General: overlap is reflexive on inhabited parts and symmetric.
  * Instance: inhabited predicates on any type with inclusion; strong
    supplementation (if every part of x overlaps y then x is part of y)
    holds constructively (singleton parts), and every family has a fusion
    (its union) — the line mereology ↔ Boolean algebras without ⊥ (cited).

  Lean 4 core only, hand-written proofs, no axioms.
-/

namespace Found

namespace Mereo

universe u

structure Parthood where
  E : Type u
  P : E → E → Prop
  refl : ∀ x, P x x
  trans : ∀ {x y z}, P x y → P y z → P x z

variable (M : Parthood.{u})

def Ov (x y : M.E) : Prop := ∃ z, M.P z x ∧ M.P z y

theorem ov_refl (x : M.E) : Ov M x x := ⟨x, M.refl x, M.refl x⟩
theorem ov_symm {x y : M.E} (h : Ov M x y) : Ov M y x := match h with | ⟨z, a, b⟩ => ⟨z, b, a⟩

/-! ## instance: inhabited predicates -/

variable (A : Type u)

def setsM : Parthood.{u} where
  E := {S : A → Prop // ∃ a, S a}
  P x y := ∀ a, x.val a → y.val a
  refl _ _ h := h
  trans h k a x := k a (h a x)

/-- strong supplementation holds for inhabited predicates -/
theorem strong_supp (x y : (setsM A).E) (h : ∀ z, (setsM A).P z x → Ov (setsM A) z y) :
    (setsM A).P x y := fun a ha =>
  match h ⟨fun b => b = a, a, rfl⟩ (fun _ e => e ▸ ha) with
  | ⟨w, hz1, hz2⟩ => match w.property with
    | ⟨b, hb⟩ => hz1 b hb ▸ hz2 b hb

/-- fusion: every inhabited family has a least upper bound (its union) -/
def fusion {I : Type u} (F : I → (setsM A).E) (i0 : I) : (setsM A).E :=
  ⟨fun a => ∃ i, (F i).val a, match (F i0).property with | ⟨a, h⟩ => ⟨a, i0, h⟩⟩

theorem fusion_ub {I : Type u} (F : I → (setsM A).E) (i0 : I) (i : I) : (setsM A).P (F i) (fusion A F i0) :=
  fun _ h => ⟨i, h⟩

theorem fusion_least {I : Type u} (F : I → (setsM A).E) (i0 : I) (y : (setsM A).E)
    (h : ∀ i, (setsM A).P (F i) y) : (setsM A).P (fusion A F i0) y :=
  fun a ⟨i, hi⟩ => h i a hi

end Mereo

end Found

#print axioms Found.Mereo.ov_symm
#print axioms Found.Mereo.strong_supp
#print axioms Found.Mereo.fusion_ub
#print axioms Found.Mereo.fusion_least

found/MetaFrameworks.lean

lean_all/found/MetaFrameworks.lean

/-
  T158–T161, T203, T204. found/MetaFrameworks — other people's meta-
  frameworks as points of the same coordinates (known range):
  * LF (T160): judgments as types — `pf φ` is the type of derivations; the
    encoding is adequate: pf φ ⟺ Hilbert/ND derivability (T143).
  * Dedukti / λΠ-modulo (T159): rewrite rules are definitional equations;
    the shallow embedding prf (a ⇒ b) ⟶ (prf a → prf b) holds by rfl.
  * Isabelle/Pure (T161): the meta-logic ⋀, ⟹ is intuitionistic higher-order
    logic on Ω (T73, T114); its rules are sound (introduction/elimination).
  * MMT (T158): theory graphs = institutions (T157) with composable
    morphisms; satisfaction is functorial along composites.
  * Metamath (T203): proofs are substitution instances of schemes plus MP;
    Hilbert derivability is closed under substitution of formulas for
    variables.
  * Mizar (T204): Tarski–Grothendieck (T162) with classical logic (T66, T89).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Hilbert
import found.Institutions
import found.MacLane
import found.ZF

namespace Found

namespace Meta

open Prp

/-! ## LF -/

def pf (φ : Form) : Prop := HD (fun _ => False) φ

theorem lf_adequate (φ : Form) : pf φ ↔ PD true (fun _ => False) φ := hilbert_iff_nd _ φ

/-! ## Dedukti: rewriting as definitional equality -/

def prf (v : Nat → Prop) : Form → Prop
  | .var n => v n
  | .bot => False
  | .and a b => prf v a ∧ prf v b
  | .or a b => prf v a ∨ prf v b
  | .imp a b => prf v a → prf v b

theorem dedukti_rule (v : Nat → Prop) (a b : Form) : prf v (.imp a b) = (prf v a → prf v b) := rfl

/-! ## Isabelle/Pure -/

theorem pure_imp_intro {A B : Prop} (h : A → B) : A → B := h
theorem pure_imp_elim {A B : Prop} (h : A → B) (a : A) : B := h a
theorem pure_all_intro {α : Type} {P : α → Prop} (h : ∀ x, P x) : ∀ x, P x := h
theorem pure_all_elim {α : Type} {P : α → Prop} (h : ∀ x, P x) (t : α) : P t := h t

/-! ## MMT: theory morphisms compose -/

theorem mmt_compose {A B C : Type} (σ : A → B) (τ : B → C) (v : C → Prop) (φ : Inst.PF A) :
    Inst.sat v (Inst.ren τ (Inst.ren σ φ)) ↔ Inst.sat (fun a => v (τ (σ a))) φ :=
  (Inst.condition τ v _).trans (Inst.condition σ (fun b => v (τ b)) φ)

/-! ## Metamath: closure under substitution -/

def subst (s : Nat → Form) : Form → Form
  | .var n => s n
  | .bot => .bot
  | .and a b => .and (subst s a) (subst s b)
  | .or a b => .or (subst s a) (subst s b)
  | .imp a b => .imp (subst s a) (subst s b)

theorem metamath_subst (s : Nat → Form) {φ : Form} (d : HD (fun _ => False) φ) :
    HD (fun _ => False) (subst s φ) := by
  induction d with
  | hyp g => exact g.elim
  | k a b => exact .k _ _
  | s a b c => exact .s _ _ _
  | andI a b => exact .andI _ _
  | andE1 a b => exact .andE1 _ _
  | andE2 a b => exact .andE2 _ _
  | orI1 a b => exact .orI1 _ _
  | orI2 a b => exact .orI2 _ _
  | orE a b c => exact .orE _ _ _
  | efq a => exact .efq _
  | mp _ _ ihf iha => exact .mp ihf iha

/-! ## Mizar: TG + classical -/

theorem mizar (lem : ∀ p : Prop, p ∨ ¬ p) (a : WF.V.{0}) :
    WF.Mem (Groth.lift a) Groth.U ∧ ((∃ x, WF.Mem x a) → ∃ m, ZF.Minimal a m) :=
  ⟨Zermelo.tg a, ZF.foundation_classical lem a⟩

end Meta

end Found

#print axioms Found.Meta.lf_adequate
#print axioms Found.Meta.dedukti_rule
#print axioms Found.Meta.pure_all_elim
#print axioms Found.Meta.mmt_compose
#print axioms Found.Meta.metamath_subst
#print axioms Found.Meta.mizar

found/Minimal.lean

lean_all/found/Minimal.lean

/-
  T65. found/Minimal — minimal and intuitionistic propositional logic.

  * One natural deduction system `PD I`, with ex falso exactly when `I = true`:
    `PD false` = minimal logic, `PD true` = intuitionistic logic.
  * Condition in the frame: both are sound in every Heyting algebra (T63);
    minimal logic interprets ⊥ by an arbitrary element, intuitionistic logic
    by the least element.
  * Translation: Γ ⊢_int φ  ⟺  Γ ∪ {⊥ → p | p atom} ⊢_min φ.
  * Separations: minimal ⊬ ⊥ → p (⊥ read as True in Ω);
    intuitionistic ⊬ p ∨ ¬p and ⊬ ¬¬p → p (three-element Heyting algebra).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Topos


universe u

namespace Found

namespace Prp

inductive Form where
  | var : Nat → Form
  | bot : Form
  | and : Form → Form → Form
  | or : Form → Form → Form
  | imp : Form → Form → Form

def neg (a : Form) : Form := .imp a .bot

def ext (Γ : Form → Prop) (a : Form) : Form → Prop := fun x => x = a ∨ Γ x

inductive PD (I : Bool) : (Form → Prop) → Form → Prop where
  | hyp {Γ a} : Γ a → PD I Γ a
  | botE {Γ a} : I = true → PD I Γ .bot → PD I Γ a
  | andI {Γ a b} : PD I Γ a → PD I Γ b → PD I Γ (.and a b)
  | andE1 {Γ a b} : PD I Γ (.and a b) → PD I Γ a
  | andE2 {Γ a b} : PD I Γ (.and a b) → PD I Γ b
  | orI1 {Γ a b} : PD I Γ a → PD I Γ (.or a b)
  | orI2 {Γ a b} : PD I Γ b → PD I Γ (.or a b)
  | orE {Γ a b c} : PD I Γ (.or a b) → PD I (ext Γ a) c → PD I (ext Γ b) c → PD I Γ c
  | impI {Γ a b} : PD I (ext Γ a) b → PD I Γ (.imp a b)
  | impE {Γ a b} : PD I Γ (.imp a b) → PD I Γ a → PD I Γ b

/-! ## Structural facts -/

theorem ext_mono {Γ Δ : Form → Prop} (h : ∀ x, Γ x → Δ x) (a : Form) :
    ∀ x, ext Γ a x → ext Δ a x :=
  fun x e => e.elim Or.inl (fun g => Or.inr (h x g))

theorem mono {I : Bool} {Γ : Form → Prop} {φ : Form} (d : PD I Γ φ) :
    ∀ {Δ : Form → Prop}, (∀ x, Γ x → Δ x) → PD I Δ φ := by
  induction d with
  | hyp g => exact fun h => .hyp (h _ g)
  | botE e _ ih => exact fun h => .botE e (ih h)
  | andI _ _ iha ihb => exact fun h => .andI (iha h) (ihb h)
  | andE1 _ ih => exact fun h => .andE1 (ih h)
  | andE2 _ ih => exact fun h => .andE2 (ih h)
  | orI1 _ ih => exact fun h => .orI1 (ih h)
  | orI2 _ ih => exact fun h => .orI2 (ih h)
  | orE _ _ _ ih iha ihb => exact fun h => .orE (ih h) (iha (ext_mono h _)) (ihb (ext_mono h _))
  | impI _ ih => exact fun h => .impI (ih (ext_mono h _))
  | impE _ _ ihf iha => exact fun h => .impE (ihf h) (iha h)

/-- cut: derivable hypotheses can be discharged -/
theorem cut {I : Bool} {Δ : Form → Prop} {φ : Form} (d : PD I Δ φ) :
    ∀ {Γ : Form → Prop}, (∀ x, Δ x → PD I Γ x) → PD I Γ φ := by
  induction d with
  | hyp g => exact fun h => h _ g
  | botE e _ ih => exact fun h => .botE e (ih h)
  | andI _ _ iha ihb => exact fun h => .andI (iha h) (ihb h)
  | andE1 _ ih => exact fun h => .andE1 (ih h)
  | andE2 _ ih => exact fun h => .andE2 (ih h)
  | orI1 _ ih => exact fun h => .orI1 (ih h)
  | orI2 _ ih => exact fun h => .orI2 (ih h)
  | @orE _ a b _ _ _ _ ih iha ihb =>
    exact fun {Γ} h => .orE (ih h)
      (iha (Γ := ext Γ a) (fun x e => e.elim (fun q => q ▸ .hyp (Or.inl rfl))
        (fun g => mono (h x g) (fun _ k => Or.inr k))))
      (ihb (Γ := ext Γ b) (fun x e => e.elim (fun q => q ▸ .hyp (Or.inl rfl))
        (fun g => mono (h x g) (fun _ k => Or.inr k))))
  | @impI _ a _ _ ih =>
    exact fun {Γ} h => .impI (ih (Γ := ext Γ a) (fun x e => e.elim (fun q => q ▸ .hyp (Or.inl rfl))
        (fun g => mono (h x g) (fun _ k => Or.inr k))))
  | impE _ _ ihf iha => exact fun h => .impE (ihf h) (iha h)

/-- minimal derivations are intuitionistic derivations -/
theorem min_to_int {Γ : Form → Prop} {φ : Form} (d : PD false Γ φ) : PD true Γ φ := by
  induction d with
  | hyp g => exact .hyp g
  | botE e _ _ => exact Bool.noConfusion e
  | andI _ _ iha ihb => exact .andI iha ihb
  | andE1 _ ih => exact .andE1 ih
  | andE2 _ ih => exact .andE2 ih
  | orI1 _ ih => exact .orI1 ih
  | orI2 _ ih => exact .orI2 ih
  | orE _ _ _ ih iha ihb => exact .orE ih iha ihb
  | impI _ ih => exact .impI ih
  | impE _ _ ihf iha => exact .impE ihf iha

/-! ## Translation: intuitionistic = minimal + atomic ex falso -/

def EFQ (Γ : Form → Prop) : Form → Prop := fun x => Γ x ∨ ∃ n, x = .imp .bot (.var n)

def HasEFQ (Δ : Form → Prop) : Prop := ∀ n, Δ (.imp .bot (.var n))

theorem hasEFQ_ext {Δ : Form → Prop} (h : HasEFQ Δ) (a : Form) : HasEFQ (ext Δ a) :=
  fun n => Or.inr (h n)

theorem here {Δ : Form → Prop} {a : Form} : PD false (ext Δ a) a := .hyp (Or.inl rfl)

theorem efq_all : (ψ : Form) → ∀ {Δ : Form → Prop}, HasEFQ Δ → PD false Δ (.imp .bot ψ)
  | .var n, _, h => .hyp (h n)
  | .bot, _, _ => .impI here
  | .and a b, _, h =>
    .impI (.andI (.impE (efq_all a (hasEFQ_ext h _)) here) (.impE (efq_all b (hasEFQ_ext h _)) here))
  | .or a _, _, h => .impI (.orI1 (.impE (efq_all a (hasEFQ_ext h _)) here))
  | .imp _ b, _, h =>
    .impI (.impI (.impE (efq_all b (hasEFQ_ext (hasEFQ_ext h _) _)) (.hyp (Or.inr (Or.inl rfl)))))

theorem int_to_min {Γ : Form → Prop} {φ : Form} (d : PD true Γ φ) :
    ∀ {Δ : Form → Prop}, (∀ x, Γ x → Δ x) → HasEFQ Δ → PD false Δ φ := by
  induction d with
  | hyp g => exact fun h _ => .hyp (h _ g)
  | @botE _ a _ _ ih => exact fun h e => .impE (efq_all a e) (ih h e)
  | andI _ _ iha ihb => exact fun h e => .andI (iha h e) (ihb h e)
  | andE1 _ ih => exact fun h e => .andE1 (ih h e)
  | andE2 _ ih => exact fun h e => .andE2 (ih h e)
  | orI1 _ ih => exact fun h e => .orI1 (ih h e)
  | orI2 _ ih => exact fun h e => .orI2 (ih h e)
  | orE _ _ _ ih iha ihb =>
    exact fun h e => .orE (ih h e) (iha (ext_mono h _) (hasEFQ_ext e _)) (ihb (ext_mono h _) (hasEFQ_ext e _))
  | impI _ ih => exact fun h e => .impI (ih (ext_mono h _) (hasEFQ_ext e _))
  | impE _ _ ihf iha => exact fun h e => .impE (ihf h e) (iha h e)

/-- Γ ⊢_int φ ⟺ Γ ∪ EFQ ⊢_min φ -/
theorem int_iff_min_efq (Γ : Form → Prop) (φ : Form) :
    PD true Γ φ ↔ PD false (EFQ Γ) φ :=
  ⟨fun d => int_to_min d (fun _ g => Or.inl g) (fun n => Or.inr ⟨n, rfl⟩),
   fun d => cut (min_to_int d) (fun _ e => e.elim (fun g => .hyp g)
     (fun ⟨_, q⟩ => q ▸ .impI (.botE rfl here_i)))⟩
where
  here_i {Δ : Form → Prop} {a : Form} : PD true (ext Δ a) a := .hyp (Or.inl rfl)

/-! ## Heyting semantics -/

section Sem
variable (H : HA.{u})

theorem distrib (c a b : H.car) :
    H.le (H.meet c (H.join a b)) (H.join (H.meet c a) (H.meet c b)) := by
  have ha : H.le a (H.imp c (H.join (H.meet c a) (H.meet c b))) :=
    H.imp_adj.mp (H.le_trans (H.meet_univ (H.meet_r _ _) (H.meet_l _ _)) (H.join_l _ _))
  have hb : H.le b (H.imp c (H.join (H.meet c a) (H.meet c b))) :=
    H.imp_adj.mp (H.le_trans (H.meet_univ (H.meet_r _ _) (H.meet_l _ _)) (H.join_r _ _))
  exact H.le_trans (H.meet_univ (H.meet_r _ _) (H.meet_l _ _)) (H.imp_adj.mpr (H.join_univ ha hb))

def interp (v : Nat → H.car) (f : H.car) : Form → H.car
  | .var n => v n
  | .bot => f
  | .and a b => H.meet (interp v f a) (interp v f b)
  | .or a b => H.join (interp v f a) (interp v f b)
  | .imp a b => H.imp (interp v f a) (interp v f b)

/-- soundness in any Heyting algebra; ⊥ is read as `f`, which must be least
  when ex falso is allowed -/
theorem soundness {I : Bool} {Γ : Form → Prop} {φ : Form} (d : PD I Γ φ)
    (v : Nat → H.car) (f : H.car) (hf : I = true → ∀ x, H.le f x) :
    ∀ c, (∀ ψ, Γ ψ → H.le c (interp H v f ψ)) → H.le c (interp H v f φ) := by
  induction d with
  | hyp g => exact fun c h => h _ g
  | botE e _ ih => exact fun c h => H.le_trans (ih c h) (hf e _)
  | andI _ _ iha ihb => exact fun c h => H.meet_univ (iha c h) (ihb c h)
  | andE1 _ ih => exact fun c h => H.le_trans (ih c h) (H.meet_l _ _)
  | andE2 _ ih => exact fun c h => H.le_trans (ih c h) (H.meet_r _ _)
  | orI1 _ ih => exact fun c h => H.le_trans (ih c h) (H.join_l _ _)
  | orI2 _ ih => exact fun c h => H.le_trans (ih c h) (H.join_r _ _)
  | @orE _ a b _ _ _ _ ih iha ihb =>
    exact fun c h =>
      have ka := iha (H.meet c (interp H v f a)) (fun ψ e => by
        cases e with
        | inl q => cases q; exact H.meet_r _ _
        | inr g => exact H.le_trans (H.meet_l _ _) (h ψ g))
      have kb := ihb (H.meet c (interp H v f b)) (fun ψ e => by
        cases e with
        | inl q => cases q; exact H.meet_r _ _
        | inr g => exact H.le_trans (H.meet_l _ _) (h ψ g))
      H.le_trans (H.meet_univ (H.le_refl c) (ih c h))
        (H.le_trans (distrib H _ _ _) (H.join_univ ka kb))
  | @impI _ a _ _ ih =>
    exact fun c h => H.imp_adj.mp (ih (H.meet c (interp H v f a)) (fun ψ e => by
        cases e with
        | inl q => cases q; exact H.meet_r _ _
        | inr g => exact H.le_trans (H.meet_l _ _) (h ψ g)))
  | impE _ _ ihf iha => exact fun c h => H.imp_elim (ihf c h) (iha c h)

/-- a theorem (no hypotheses) is valued top -/
theorem valid {I : Bool} {φ : Form} (d : PD I (fun _ => False) φ)
    (v : Nat → H.car) (f : H.car) (hf : I = true → ∀ x, H.le f x) :
    H.le H.top (interp H v f φ) :=
  soundness H d v f hf H.top (fun _ g => g.elim)

end Sem

/-! ## Separations -/

/-- minimal logic does not prove ⊥ → p: read ⊥ as True, p as False in Ω -/
theorem min_not_efq : ¬ PD false (fun _ => False) (.imp .bot (.var 0)) :=
  fun d => valid OmegaHA d (fun _ => False) True (fun e => Bool.noConfusion e) trivial trivial

/-- the three-element Heyting algebra 0 < ½ < 1 -/
inductive Three where
  | z : Three
  | h : Three
  | o : Three

namespace Three

def le : Three → Three → Prop
  | z, z => True
  | z, h => True
  | z, o => True
  | h, z => False
  | h, h => True
  | h, o => True
  | o, z => False
  | o, h => False
  | o, o => True

def meet : Three → Three → Three
  | z, z => z
  | z, h => z
  | z, o => z
  | h, z => z
  | h, h => h
  | h, o => h
  | o, z => z
  | o, h => h
  | o, o => o

def join : Three → Three → Three
  | z, z => z
  | z, h => h
  | z, o => o
  | h, z => h
  | h, h => h
  | h, o => o
  | o, z => o
  | o, h => o
  | o, o => o

def imp : Three → Three → Three
  | z, z => o
  | z, h => o
  | z, o => o
  | h, z => z
  | h, h => o
  | h, o => o
  | o, z => z
  | o, h => h
  | o, o => o

def HA3 : HA.{0} where
  car := Three
  le := le
  le_refl a := by cases a <;> exact trivial
  le_trans {a b c} := by
    cases a <;> cases b <;> cases c <;> intro p q <;> first | exact trivial | exact p.elim | exact q.elim
  top := o
  bot := z
  meet := meet
  join := join
  imp := imp
  le_top a := by cases a <;> exact trivial
  bot_le a := by cases a <;> exact trivial
  meet_l a b := by cases a <;> cases b <;> exact trivial
  meet_r a b := by cases a <;> cases b <;> exact trivial
  meet_univ {a b c} := by
    cases a <;> cases b <;> cases c <;> intro p q <;> first | exact trivial | exact p.elim | exact q.elim
  join_l a b := by cases a <;> cases b <;> exact trivial
  join_r a b := by cases a <;> cases b <;> exact trivial
  join_univ {a b c} := by
    cases a <;> cases b <;> cases c <;> intro p q <;> first | exact trivial | exact p.elim | exact q.elim
  imp_adj {a b c} := by
    cases a <;> cases b <;> cases c <;>
      exact ⟨fun p => p, fun p => p⟩

end Three

/-- intuitionistic logic does not prove p ∨ ¬p -/
theorem int_not_lem : ¬ PD true (fun _ => False) (.or (.var 0) (neg (.var 0))) :=
  fun d => valid Three.HA3 d (fun _ => Three.h) Three.z (fun _ x => by cases x <;> exact trivial)

/-- intuitionistic logic does not prove ¬¬p → p -/
theorem int_not_dne : ¬ PD true (fun _ => False) (.imp (neg (neg (.var 0))) (.var 0)) :=
  fun d => valid Three.HA3 d (fun _ => Three.h) Three.z (fun _ x => by cases x <;> exact trivial)

end Prp

end Found

#print axioms Found.Prp.mono
#print axioms Found.Prp.cut
#print axioms Found.Prp.min_to_int
#print axioms Found.Prp.efq_all
#print axioms Found.Prp.int_to_min
#print axioms Found.Prp.int_iff_min_efq
#print axioms Found.Prp.distrib
#print axioms Found.Prp.soundness
#print axioms Found.Prp.valid
#print axioms Found.Prp.min_not_efq
#print axioms Found.Prp.Three.HA3
#print axioms Found.Prp.int_not_lem
#print axioms Found.Prp.int_not_dne

found/Minsky1.lean

lean_all/found/Minsky1.lean

/-
  Line register machines → two-counter machines (Minsky), part 1.
  found/Minsky1 — the arithmetic of the encoding: the moduli are Sylvester's
  numbers m_n = P_n + 1 with P_0 = 1, P_{n+1} = P_n · m_n (pairwise coprime:
  m_k divides P_n for k < n, and a common divisor of P_n and P_n + 1 is 1);
  a store s on registers below K is encoded as E s K = ∏_{r<K} m_r^{s r}.
  Increment of r multiplies by m_r, a non-zero register can be decremented by
  dividing by m_r, and m_r divides E s K exactly when s r ≠ 0 (Gauss's lemma
  from lib/Gcd).  Division with remainder and its uniqueness.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import lib.Gcd
import found.RegComp1

namespace Found

namespace Mk

open LibGcd RC

theorem le_cases {n m : Nat} (h : n ≤ m) : n = m ∨ n < m := by
  induction h with
  | refl => exact Or.inl rfl
  | step h _ => exact Or.inr (Nat.lt_succ_of_le h)

theorem add_cancel' : (k : Nat) → {i j : Nat} → k + i = k + j → i = j
  | 0, _, _, h => by rw [Nat.zero_add, Nat.zero_add] at h; exact h
  | k + 1, _, _, h => add_cancel' k (Nat.succ.inj (by rw [Nat.succ_add, Nat.succ_add] at h; exact h))

/-! ## Sylvester's moduli -/

def P : Nat → Nat
  | 0 => 1
  | n + 1 => P n * (P n + 1)

def mo (n : Nat) : Nat := P n + 1

theorem P_pos : (n : Nat) → ∃ k, P n = k + 1
  | 0 => ⟨0, rfl⟩
  | n + 1 => match P_pos n with
    | ⟨k, hk⟩ => ⟨k * (k + 2) + k + 1, by show P n * (P n + 1) = _; rw [hk]; lib_nat⟩

theorem mo_two (n : Nat) : ∃ k, mo n = k + 2 :=
  match P_pos n with
  | ⟨k, hk⟩ => ⟨k, by show P n + 1 = k + 2; rw [hk]⟩

theorem mo_ne_one (n : Nat) : mo n ≠ 1 := fun e =>
  match mo_two n with
  | ⟨_, hk⟩ => Nat.noConfusion (Nat.succ.inj (hk.symm.trans e))

theorem dvd_P : (n k : Nat) → k < n → mo k ∣ P n
  | 0, _, h => absurd h (Nat.not_lt_zero _)
  | n + 1, k, h => (le_cases (Nat.le_of_lt_succ h)).elim
      (fun e => by
        subst e
        exact ⟨P k, by show P k * (P k + 1) = (P k + 1) * P k; lib_nat⟩)
      (fun hl => by
        show mo k ∣ P n * (P n + 1)
        exact dvd_mul_right' (dvd_P n k hl) _)

theorem dvd_succ_one {d a : Nat} (h1 : d ∣ a) (h2 : d ∣ a + 1) : d = 1 := by
  obtain ⟨c1, e1⟩ := h1
  obtain ⟨c2, e2⟩ := h2
  apply dvd_one'
  apply idv_nat
  refine ⟨Int.ofNat c2 - Int.ofNat c1, ?_⟩
  have i1 : Int.ofNat a = Int.ofNat d * Int.ofNat c1 := by rw [e1]; rfl
  have i2 : Int.ofNat a + 1 = Int.ofNat d * Int.ofNat c2 := by
    show Int.ofNat (a + 1) = _
    rw [e2]; rfl
  calc Int.ofNat 1 = (Int.ofNat a + 1) - Int.ofNat a := by
        show (1 : Int) = _
        lib_int
    _ = Int.ofNat d * (Int.ofNat c2 - Int.ofNat c1) := by rw [i2, i1]; lib_int

/-- coprimality -/
def Cop (a b : Nat) : Prop := ∀ d, d ∣ a → d ∣ b → d = 1

theorem mo_cop {i j : Nat} (h : i ≠ j) : Cop (mo i) (mo j) := fun _ hi hj =>
  (Nat.lt_or_ge i j).elim
    (fun hl => dvd_succ_one (dvd_trans' hi (dvd_P j i hl)) hj)
    (fun hg => (le_cases hg).elim (fun e => absurd e.symm h)
      (fun hl => dvd_succ_one (dvd_trans' hj (dvd_P i j hl)) hi))

theorem cop_mul {a b c : Nat} (hb : Cop a b) (hc : Cop a c) : Cop a (b * c) := fun d ha hbc =>
  have hdb : Cop d b := fun e he1 he2 => hb e (dvd_trans' he1 ha) he2
  hc d ha (gauss hdb (by obtain ⟨k, hk⟩ := hbc; exact ⟨k, by rw [← hk]; lib_nat⟩))

def pw (b : Nat) : Nat → Nat
  | 0 => 1
  | e + 1 => pw b e * b

theorem cop_pw {a b : Nat} (h : Cop a b) : (e : Nat) → Cop a (pw b e)
  | 0 => fun _ _ h1 => dvd_one' h1
  | e + 1 => cop_mul (cop_pw h e) h

/-! ## the encoding of a store -/

def E (s : Nat → Nat) : Nat → Nat
  | 0 => 1
  | K + 1 => E s K * pw (mo K) (s K)

theorem E_congr (s t : Nat → Nat) : (K : Nat) → (∀ j, j < K → s j = t j) → E s K = E t K
  | 0, _ => rfl
  | K + 1, h => by
    show E s K * pw (mo K) (s K) = E t K * pw (mo K) (t K)
    rw [E_congr s t K (fun j hj => h j (Nat.lt_trans hj (Nat.lt_succ_self K))), h K (Nat.lt_succ_self K)]

theorem E_inc (s : Nat → Nat) (r : Nat) : (K : Nat) → r < K → E (upd s r (s r + 1)) K = mo r * E s K
  | 0, h => absurd h (Nat.not_lt_zero _)
  | K + 1, h => by
    show E (upd s r (s r + 1)) K * pw (mo K) (upd s r (s r + 1) K) = mo r * (E s K * pw (mo K) (s K))
    cases le_cases (Nat.le_of_lt_succ h) with
    | inl e =>
      subst e
      rw [E_congr _ s r (fun j hj => upd_ne s r _ j (fun z => Nat.lt_irrefl _ (z ▸ hj))), upd_eq]
      show E s r * (pw (mo r) (s r) * mo r) = _
      lib_nat
    | inr hl =>
      rw [E_inc s r K hl, upd_ne s r _ K (fun z => Nat.lt_irrefl _ (z ▸ hl))]
      lib_nat

theorem E_dec (s : Nat → Nat) (r v K : Nat) (hv : s r = v + 1) (h : r < K) :
    E s K = mo r * E (upd s r v) K := by
  rw [← E_inc (upd s r v) r K h]
  apply E_congr
  intro j _
  match Nat.decEq j r with
  | isTrue e => rw [e, upd_eq, upd_eq, hv]
  | isFalse ne => rw [upd_ne _ _ _ _ ne, upd_ne _ _ _ _ ne]

theorem E_ndvd (s : Nat → Nat) (r : Nat) (hz : s r = 0) : (K : Nat) → ¬ mo r ∣ E s K
  | 0, h => mo_ne_one r (dvd_one' h)
  | K + 1, h => by
    match Nat.decEq K r with
    | isTrue e =>
      subst e
      have h' : mo K ∣ E s K * pw (mo K) (s K) := h
      rw [hz] at h'
      exact E_ndvd s K hz K (by obtain ⟨c, hc⟩ := h'; exact ⟨c, by rw [← hc]; show E s K = E s K * 1; lib_nat⟩)
    | isFalse ne =>
      exact E_ndvd s r hz K (gauss (cop_pw (mo_cop (fun z => ne z.symm)) (s K)) h)

theorem E_dvd (s : Nat → Nat) (r v K : Nat) (hv : s r = v + 1) (h : r < K) : mo r ∣ E s K :=
  ⟨E (upd s r v) K, E_dec s r v K hv h⟩

/-! ## division with remainder -/

theorem div_ex (m' : Nat) : (x : Nat) → ∃ q j, x = (m' + 1) * q + j ∧ j < m' + 1
  | 0 => ⟨0, 0, rfl, Nat.succ_pos m'⟩
  | x + 1 => match div_ex m' x with
    | ⟨q, j, hx, hj⟩ => (le_cases (Nat.succ_le_of_lt hj)).elim
        (fun e => ⟨q + 1, 0, by rw [hx, Nat.add_assoc, show j + 1 = m' + 1 from e]; lib_nat, Nat.succ_pos m'⟩)
        (fun hl => ⟨q, j + 1, by rw [hx, Nat.add_assoc], hl⟩)

theorem uniq {m q q' j : Nat} (h : m * q + j = m * q') (hj : j < m) : j = 0 ∧ q = q' := by
  cases Nat.lt_or_ge q q' with
  | inl hlt =>
    obtain ⟨d, hd⟩ := le_dest hlt
    have e : m * q + j = m * q + (m * d + m) := h.trans (by rw [hd]; lib_nat)
    have e2 := add_cancel' _ e
    rw [e2] at hj
    exact absurd hj (Nat.not_lt_of_le (Nat.le_add_left m _))
  | inr hge =>
    obtain ⟨d, hd⟩ := le_dest hge
    have e : m * q' + (m * d + j) = m * q' + 0 := ((by rw [hd]; lib_nat : m * q' + (m * d + j) = m * q + j).trans h)
    have e2 := add_cancel' _ e
    cases j with
    | succ j => exact Nat.noConfusion e2
    | zero =>
      refine ⟨rfl, ?_⟩
      cases m with
      | zero => exact absurd hj (Nat.lt_irrefl 0)
      | succ m' =>
        cases d with
        | zero => rw [hd]; rfl
        | succ d => exact Nat.noConfusion (show (m' + 1) * d + m' + 1 = 0 by rw [← e2]; lib_nat)

end Mk

end Found

#print axioms Found.Mk.P_pos
#print axioms Found.Mk.dvd_P
#print axioms Found.Mk.dvd_succ_one
#print axioms Found.Mk.mo_cop
#print axioms Found.Mk.cop_pw
#print axioms Found.Mk.E_inc
#print axioms Found.Mk.E_dec
#print axioms Found.Mk.E_ndvd
#print axioms Found.Mk.E_dvd
#print axioms Found.Mk.div_ex
#print axioms Found.Mk.uniq

found/Minsky2.lean

lean_all/found/Minsky2.lean

/-
  Line register machines → two-counter machines (Minsky), part 2.
  found/Minsky2 — gadgets for the two-counter machines of found/RegToMu
  (inc0/inc1/dec0/dec1 with jumps), with counter 1 = 0 between gadgets:
  * code placed at an address (At), reachability (Reach) and its transitivity;
  * straight chains of increments, upward and downward;
  * multiplication of counter 0 by m (mulC);
  * the divide/test gadget dtC: counter 0 is consumed in chunks of m,
    counting the chunks in counter 1; if it was a multiple m·q the machine
    jumps to Y with f·q in counter 0 (f = 1 divides, f = m restores), and if
    it was not a multiple it jumps to N with counter 0 restored.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Minsky1
import found.RegToMu

namespace Found

namespace Mk

open RM

/-! ## placed code and reachability -/

def At (prog : List Ins) : Nat → List Ins → Prop
  | _, [] => True
  | a, i :: l => fetch prog a = i ∧ At prog (a + 1) l

def len : List Ins → Nat
  | [] => 0
  | _ :: l => len l + 1

theorem len_app : (l1 l2 : List Ins) → len (l1 ++ l2) = len l1 + len l2
  | [], l2 => (Nat.zero_add _).symm
  | _ :: l1, l2 => by
    show len (l1 ++ l2) + 1 = len l1 + 1 + len l2
    rw [len_app l1 l2]
    lib_nat

theorem at_app (prog : List Ins) : (a : Nat) → (l1 l2 : List Ins) → At prog a (l1 ++ l2) →
    At prog a l1 ∧ At prog (a + len l1) l2
  | _, [], _, h => ⟨trivial, h⟩
  | a, i :: l1, l2, h => match at_app prog (a + 1) l1 l2 h.2 with
    | ⟨h1, h2⟩ => ⟨⟨h.1, h1⟩, by
        rw [show a + len (i :: l1) = a + 1 + len l1 by show a + (len l1 + 1) = _; lib_nat]
        exact h2⟩

def Reach (prog : List Ins) (c c' : Cf) : Prop := ∃ t, run prog c t = c'

theorem run_add (prog : List Ins) (c : Cf) (t1 : Nat) : (t2 : Nat) → run prog c (t1 + t2) = run prog (run prog c t1) t2
  | 0 => rfl
  | t2 + 1 => by
    show step prog (run prog c (t1 + t2)) = step prog (run prog (run prog c t1) t2)
    rw [run_add prog c t1 t2]

theorem rrefl (prog : List Ins) (c : Cf) : Reach prog c c := ⟨0, rfl⟩

theorem rtrans {prog : List Ins} {a b c : Cf} (h1 : Reach prog a b) (h2 : Reach prog b c) : Reach prog a c :=
  match h1, h2 with
  | ⟨t1, e1⟩, ⟨t2, e2⟩ => ⟨t1 + t2, by rw [run_add, e1, e2]⟩

theorem rstep {prog : List Ins} {c : Cf} {i : Ins} (h : fetch prog c.pc = i) : Reach prog c (exec i c) :=
  ⟨1, by show exec (fetch prog c.pc) c = _; rw [h]⟩

theorem s_inc0 {prog : List Ins} {p g x y : Nat} (h : fetch prog p = .inc0 g) :
    Reach prog ⟨p, x, y⟩ ⟨g, x + 1, y⟩ := rstep (c := ⟨p, x, y⟩) h
theorem s_inc1 {prog : List Ins} {p g x y : Nat} (h : fetch prog p = .inc1 g) :
    Reach prog ⟨p, x, y⟩ ⟨g, x, y + 1⟩ := rstep (c := ⟨p, x, y⟩) h
theorem s_dec0z {prog : List Ins} {p a b y : Nat} (h : fetch prog p = .dec0 a b) :
    Reach prog ⟨p, 0, y⟩ ⟨a, 0, y⟩ := rstep (c := ⟨p, 0, y⟩) h
theorem s_dec0s {prog : List Ins} {p a b k y : Nat} (h : fetch prog p = .dec0 a b) :
    Reach prog ⟨p, k + 1, y⟩ ⟨b, k, y⟩ := rstep (c := ⟨p, k + 1, y⟩) h
theorem s_dec1z {prog : List Ins} {p a b x : Nat} (h : fetch prog p = .dec1 a b) :
    Reach prog ⟨p, x, 0⟩ ⟨a, x, 0⟩ := rstep (c := ⟨p, x, 0⟩) h
theorem s_dec1s {prog : List Ins} {p a b x k : Nat} (h : fetch prog p = .dec1 a b) :
    Reach prog ⟨p, x, k + 1⟩ ⟨b, x, k⟩ := rstep (c := ⟨p, x, k + 1⟩) h

theorem reach_cast {prog : List Ins} {c : Cf} {p x x' y y' : Nat} (h : Reach prog c ⟨p, x, y⟩)
    (ex : x = x') (ey : y = y') : Reach prog c ⟨p, x', y'⟩ := by
  rw [← ex, ← ey]; exact h

/-! ## chains -/

def ups0 (tgt : Nat) : Nat → Nat → List Ins
  | _, 0 => [.inc0 tgt]
  | b, n + 1 => .inc0 (b + 1) :: ups0 tgt (b + 1) n

def ups1 (tgt : Nat) : Nat → Nat → List Ins
  | _, 0 => [.inc1 tgt]
  | b, n + 1 => .inc1 (b + 1) :: ups1 tgt (b + 1) n

theorem len_ups1 (tgt : Nat) : (b n : Nat) → len (ups1 tgt b n) = n + 1
  | _, 0 => rfl
  | b, n + 1 => by show len (ups1 tgt (b + 1) n) + 1 = _; rw [len_ups1 tgt (b + 1) n]

theorem ups0_run (prog : List Ins) (tgt : Nat) : (n b x y : Nat) → At prog b (ups0 tgt b n) →
    Reach prog ⟨b, x, y⟩ ⟨tgt, x + (n + 1), y⟩
  | 0, _, _, _, h => s_inc0 h.1
  | n + 1, b, x, y, h => rtrans (s_inc0 h.1)
      (reach_cast (ups0_run prog tgt n (b + 1) (x + 1) y h.2) (by lib_nat) rfl)

theorem ups1_run (prog : List Ins) (tgt : Nat) : (n b x y : Nat) → At prog b (ups1 tgt b n) →
    Reach prog ⟨b, x, y⟩ ⟨tgt, x, y + (n + 1)⟩
  | 0, _, _, _, h => s_inc1 h.1
  | n + 1, b, x, y, h => rtrans (s_inc1 h.1)
      (reach_cast (ups1_run prog tgt n (b + 1) x (y + 1) h.2) rfl (by lib_nat))

def dt (b tgt : Nat) : Nat → Nat
  | 0 => tgt
  | i + 1 => b + i

def dn0 (b tgt : Nat) : Nat → List Ins
  | 0 => []
  | n + 1 => dn0 b tgt n ++ [.inc0 (dt b tgt n)]

theorem len_dn0 (b tgt : Nat) : (n : Nat) → len (dn0 b tgt n) = n
  | 0 => rfl
  | n + 1 => by show len (dn0 b tgt n ++ _) = _; rw [len_app, len_dn0 b tgt n]; rfl

theorem dn0_fetch (prog : List Ins) (b tgt : Nat) : (n : Nat) → At prog b (dn0 b tgt n) →
    ∀ i, i < n → fetch prog (b + i) = .inc0 (dt b tgt i)
  | 0, _, _, hi => absurd hi (Nat.not_lt_zero _)
  | n + 1, h, i, hi => match at_app prog b _ _ h with
    | ⟨h1, h2⟩ => (le_cases (Nat.le_of_lt_succ hi)).elim
        (fun e => by
          subst e
          rw [len_dn0] at h2
          exact h2.1)
        (fun hl => dn0_fetch prog b tgt n h1 i hl)

theorem dn0_run (prog : List Ins) (b tgt n : Nat) (h : At prog b (dn0 b tgt n)) :
    (i x y : Nat) → i < n → Reach prog ⟨b + i, x, y⟩ ⟨tgt, x + (i + 1), y⟩
  | 0, _, _, hi => s_inc0 (dn0_fetch prog b tgt n h 0 hi)
  | i + 1, x, y, hi => rtrans (s_inc0 (dn0_fetch prog b tgt n h (i + 1) hi))
      (reach_cast (dn0_run prog b tgt n h i (x + 1) y (Nat.lt_trans (Nat.lt_succ_self i) hi)) (by lib_nat) rfl)

/-! ## the chunk loop -/

def zt (Y b : Nat) : Nat → Nat
  | 0 => Y
  | j + 1 => b + j

def chk (a Y b : Nat) : Nat → List Ins
  | 0 => []
  | j + 1 => chk a Y b j ++ [.dec0 (zt Y b j) (a + j + 1)]

theorem len_chk (a Y b : Nat) : (m : Nat) → len (chk a Y b m) = m
  | 0 => rfl
  | m + 1 => by show len (chk a Y b m ++ _) = _; rw [len_app, len_chk a Y b m]; rfl

theorem chk_fetch (prog : List Ins) (a Y b : Nat) : (m : Nat) → At prog a (chk a Y b m) →
    ∀ j, j < m → fetch prog (a + j) = .dec0 (zt Y b j) (a + j + 1)
  | 0, _, _, hj => absurd hj (Nat.not_lt_zero _)
  | m + 1, h, j, hj => match at_app prog a _ _ h with
    | ⟨h1, h2⟩ => (le_cases (Nat.le_of_lt_succ hj)).elim
        (fun e => by
          subst e
          rw [len_chk] at h2
          exact h2.1)
        (fun hl => chk_fetch prog a Y b m h1 j hl)

def chunk (a Y b m : Nat) : List Ins := chk a Y b m ++ [.inc1 a]

theorem lt_of_add_lt {j k m : Nat} (h : j + k < m) : j < m := Nat.lt_of_le_of_lt (Nat.le_add_right j k) h

theorem chunk_run (prog : List Ins) (a Y b m : Nat) (h : At prog a (chunk a Y b m)) :
    ∀ q j y, j < m → Reach prog ⟨a, m * q + j, y⟩ ⟨zt Y b j, 0, y + q⟩ := by
  have hc := at_app prog a _ _ h
  have F := chk_fetch prog a Y b m hc.1
  have F2 : fetch prog (a + m) = .inc1 a := by
    have := hc.2
    rw [len_chk] at this
    exact this.1
  have part : ∀ x j y, j + x < m → Reach prog ⟨a + j, x, y⟩ ⟨zt Y b (j + x), 0, y⟩ := by
    intro x
    induction x with
    | zero => intro j y hj; exact s_dec0z (F j hj)
    | succ x ih =>
      intro j y hj
      refine rtrans (s_dec0s (F j (lt_of_add_lt hj))) ?_
      have := ih (j + 1) y (by rw [Nat.add_assoc, Nat.add_comm 1 x]; exact hj)
      rw [show j + 1 + x = j + (x + 1) by lib_nat] at this
      exact this
  have full : ∀ k j x y, j + k = m → Reach prog ⟨a + j, k + x, y⟩ ⟨a, x, y + 1⟩ := by
    intro k
    induction k with
    | zero =>
      intro j x y e
      have e' : j = m := e
      subst e'
      rw [Nat.zero_add]
      exact s_inc1 F2
    | succ k ih =>
      intro j x y e
      rw [show k + 1 + x = (k + x) + 1 by lib_nat]
      refine rtrans (s_dec0s (F j (by rw [← e]; exact Nat.lt_add_of_pos_right (Nat.succ_pos k)))) ?_
      exact ih (j + 1) x y (by rw [← e]; lib_nat)
  intro q
  induction q with
  | zero =>
    intro j y hj
    rw [Nat.mul_zero, Nat.zero_add]
    have := part j 0 y (by rw [Nat.zero_add]; exact hj)
    rw [Nat.zero_add, Nat.add_zero] at this
    exact this
  | succ q ih =>
    intro j y hj
    rw [show m * (q + 1) + j = m + (m * q + j) by lib_nat]
    have h1 := full m 0 (m * q + j) y (Nat.zero_add m)
    rw [Nat.add_zero] at h1
    exact rtrans h1 (reach_cast (ih j (y + 1) hj) rfl (by lib_nat))

/-! ## multiplying back -/

def mbC (L E f' : Nat) : List Ins := .dec1 E (L + 1) :: ups0 L (L + 1) f'

theorem len_mbC (L E : Nat) : (f' : Nat) → len (mbC L E f') = f' + 2
  | 0 => rfl
  | f' + 1 => by
    show len (ups0 L (L + 1 + 1) f') + 1 + 1 = _
    have : ∀ b n, len (ups0 L b n) = n + 1 := by
      intro b n
      induction n generalizing b with
      | zero => rfl
      | succ n ih => show len (ups0 L (b + 1) n) + 1 = _; rw [ih]
    rw [this]

/-- counter 0 += f · counter 1, then jump to E -/
theorem mb_run (prog : List Ins) (L E f' : Nat) (h : At prog L (mbC L E f')) :
    (y x : Nat) → Reach prog ⟨L, x, y⟩ ⟨E, x + (f' + 1) * y, 0⟩
  | 0, _ => s_dec1z h.1
  | y + 1, x => rtrans (s_dec1s h.1) (rtrans (ups0_run prog L f' (L + 1) x y h.2)
      (reach_cast (mb_run prog L E f' h y (x + (f' + 1))) (by lib_nat) rfl))

/-! ## multiplication -/

def mulC (a m' : Nat) : List Ins := .dec0 (a + m' + 2) (a + 1) :: (ups1 a (a + 1) m' ++ mbC (a + m' + 2) (a + m' + 4) 0)

theorem mul_spec (prog : List Ins) (a m' : Nat) (h : At prog a (mulC a m')) (x : Nat) :
    Reach prog ⟨a, x, 0⟩ ⟨a + m' + 4, (m' + 1) * x, 0⟩ := by
  have hc := at_app prog (a + 1) _ _ h.2
  have hm := hc.2
  rw [len_ups1, show a + 1 + (m' + 1) = a + m' + 2 by lib_nat] at hm
  have loop : ∀ x y, Reach prog ⟨a, x, y⟩ ⟨a + m' + 2, 0, y + (m' + 1) * x⟩ := by
    intro x
    induction x with
    | zero => intro y; exact s_dec0z h.1
    | succ x ih =>
      intro y
      exact rtrans (s_dec0s h.1) (rtrans (ups1_run prog a m' (a + 1) x y hc.1)
        (reach_cast (ih (y + (m' + 1))) rfl (by lib_nat)))
  exact rtrans (loop x 0) (reach_cast (mb_run prog _ _ 0 hm _ 0) (by lib_nat) rfl)

/-! ## the divide / test gadget -/

/-- chunk at a, chain at a+n+2, multiply-back-to-N at a+2n+2, yes-path at a+3n+4 -/
def dtC (a n f' Y N : Nat) : List Ins :=
  chunk a (a + (n + 2) + n + (n + 2)) (a + (n + 2)) (n + 1) ++
    (dn0 (a + (n + 2)) (a + (n + 2) + n) n ++
      (mbC (a + (n + 2) + n) N n ++ mbC (a + (n + 2) + n + (n + 2)) Y f'))

def dtLen (n f' : Nat) : Nat := (n + 2) + (n + ((n + 2) + (f' + 2)))

theorem len_dtC (a n f' Y N : Nat) : len (dtC a n f' Y N) = dtLen n f' := by
  unfold dtC dtLen chunk
  rw [len_app, len_app, len_app, len_app, len_chk, len_dn0, len_mbC, len_mbC]
  rfl

theorem dt_parts (prog : List Ins) (a n f' Y N : Nat) (h : At prog a (dtC a n f' Y N)) :
    At prog a (chunk a (a + (n + 2) + n + (n + 2)) (a + (n + 2)) (n + 1)) ∧
    At prog (a + (n + 2)) (dn0 (a + (n + 2)) (a + (n + 2) + n) n) ∧
    At prog (a + (n + 2) + n) (mbC (a + (n + 2) + n) N n) ∧
    At prog (a + (n + 2) + n + (n + 2)) (mbC (a + (n + 2) + n + (n + 2)) Y f') := by
  have h1 := at_app prog a _ _ h
  have l1 : len (chunk a (a + (n + 2) + n + (n + 2)) (a + (n + 2)) (n + 1)) = n + 2 := by
    unfold chunk; rw [len_app, len_chk]; rfl
  have h2 := h1.2
  rw [l1] at h2
  have h3 := at_app prog _ _ _ h2
  have h4 := h3.2
  rw [len_dn0] at h4
  have h5 := at_app prog _ _ _ h4
  have h6 := h5.2
  rw [len_mbC] at h6
  exact ⟨h1.1, h3.1, h5.1, h6⟩

/-- a multiple of m = n+1: jump to Y with f·q -/
theorem dt_yes (prog : List Ins) (a n f' Y N : Nat) (h : At prog a (dtC a n f' Y N)) (q : Nat) :
    Reach prog ⟨a, (n + 1) * q, 0⟩ ⟨Y, (f' + 1) * q, 0⟩ :=
  match dt_parts prog a n f' Y N h with
  | ⟨hc, _, _, hy⟩ =>
    rtrans (chunk_run prog a _ _ (n + 1) hc q 0 0 (Nat.succ_pos n))
      (reach_cast (mb_run prog _ Y f' hy (0 + q) 0) (by lib_nat) rfl)

/-- not a multiple: jump to N with counter 0 restored -/
theorem dt_no (prog : List Ins) (a n f' Y N : Nat) (h : At prog a (dtC a n f' Y N)) (x : Nat)
    (hn : ¬ (n + 1) ∣ x) : Reach prog ⟨a, x, 0⟩ ⟨N, x, 0⟩ :=
  match dt_parts prog a n f' Y N h, div_ex n x with
  | ⟨hc, hd, hm, _⟩, ⟨q, j, hx, hj⟩ => by
    cases j with
    | zero => exact absurd ⟨q, hx⟩ hn
    | succ j' =>
      rw [hx]
      refine rtrans (chunk_run prog a _ _ (n + 1) hc q (j' + 1) 0 hj) ?_
      refine rtrans (dn0_run prog _ _ n hd j' 0 (0 + q) (Nat.lt_of_succ_lt_succ hj)) ?_
      exact reach_cast (mb_run prog _ N n hm (0 + q) (0 + (j' + 1))) (by lib_nat) rfl

end Mk

end Found

#print axioms Found.Mk.at_app
#print axioms Found.Mk.rtrans
#print axioms Found.Mk.ups0_run
#print axioms Found.Mk.ups1_run
#print axioms Found.Mk.dn0_run
#print axioms Found.Mk.chunk_run
#print axioms Found.Mk.mb_run
#print axioms Found.Mk.mul_spec
#print axioms Found.Mk.len_dtC
#print axioms Found.Mk.dt_yes
#print axioms Found.Mk.dt_no

found/Minsky3.lean

lean_all/found/Minsky3.lean

/-
  Line register machines → two-counter machines (Minsky), part 3.
  found/Minsky3 — compiling structured register programs (found/RegComp1:
  inc, dec, seq, while r ≠ 0) on registers below K into two-counter
  programs (found/RegToMu) over the encoding E s K = ∏ m_r^{s r}
  (found/Minsky1) with the gadgets of found/Minsky2: inc r multiplies by m_r,
  dec r divides by m_r when it divides and otherwise does nothing, and
  while r ≠ 0 tests divisibility by m_r (restoring), runs the body and jumps
  back.  Correctness: every run s → s' of the structured program is a run of
  the two-counter program from E s K to E s' K (minsky).  With reg_to_mu the
  μ-recursive program F computes E s K ↦ E s' K (register_to_mu), so
  structured register machines, and with found/TMSim Turing machines, are
  computed by μ-recursive functions.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Minsky2

namespace Found

namespace Mk

open RM RC Rec CT3

/-- the registers used by a program are below K -/
def Uses (K : Nat) : Prog → Prop
  | .inc r => r < K
  | .dec r => r < K
  | .seq p q => Uses K p ∧ Uses K q
  | .loop r p => r < K ∧ Uses K p

def clen : Prog → Nat
  | .inc r => P r + 4
  | .dec r => dtLen (P r) 0
  | .seq p q => clen p + clen q
  | .loop r p => dtLen (P r) (P r) + clen p + 1

def comp : Prog → Nat → List Ins
  | .inc r, a => mulC a (P r)
  | .dec r, a => dtC a (P r) 0 (a + dtLen (P r) 0) (a + dtLen (P r) 0)
  | .seq p q, a => comp p a ++ comp q (a + clen p)
  | .loop r p, a => dtC a (P r) (P r) (a + dtLen (P r) (P r)) (a + dtLen (P r) (P r) + clen p + 1) ++
      (comp p (a + dtLen (P r) (P r)) ++ [.dec1 a a])

theorem len_comp : (p : Prog) → (a : Nat) → len (comp p a) = clen p
  | .inc r, a => by
    show len (ups1 a (a + 1) (P r) ++ mbC (a + P r + 2) (a + P r + 4) 0) + 1 = P r + 4
    rw [len_app, len_ups1, len_mbC]
  | .dec r, a => len_dtC _ _ _ _ _
  | .seq p q, a => by
    show len (comp p a ++ comp q (a + clen p)) = clen p + clen q
    rw [len_app, len_comp p, len_comp q]
  | .loop r p, a => by
    show len (dtC a (P r) (P r) _ _ ++ (comp p _ ++ [.dec1 a a])) = _
    rw [len_app, len_app, len_dtC, len_comp p]
    rfl

theorem reach_eq {prog : List Ins} {c c' c'' : Cf} (h : Reach prog c c') (e : c' = c'') : Reach prog c c'' := e ▸ h

/-- compile correctness -/
theorem comp_ok (K : Nat) {p : Prog} {s s' : Store} (h : Run p s s') :
    ∀ a prog, Uses K p → At prog a (comp p a) → Reach prog ⟨a, E s K, 0⟩ ⟨a + clen p, E s' K, 0⟩ := by
  induction h with
  | @inc r s =>
    intro a prog hu hat
    exact reach_eq (mul_spec prog a (P r) hat (E s K))
      (by rw [E_inc s r K hu]; show _ = (⟨a + (P r + 4), (P r + 1) * E s K, 0⟩ : Cf); rw [Nat.add_assoc])
  | @dec r s =>
    intro a prog hu hat
    match hv : s r with
    | 0 =>
      have e : E (upd s r (0 - 1)) K = E s K :=
        E_congr _ _ K (fun j _ => match Nat.decEq j r with
          | isTrue ej => by rw [ej, upd_eq, hv]
          | isFalse ne => upd_ne s r _ j ne)
      rw [e]
      exact dt_no prog a (P r) 0 _ _ hat (E s K) (E_ndvd s r hv K)
    | v + 1 =>
      show Reach prog _ ⟨_, E (upd s r v) K, 0⟩
      have hd := dt_yes prog a (P r) 0 _ _ hat (E (upd s r v) K)
      rw [← (show E s K = (P r + 1) * E (upd s r v) K from E_dec s r v K hv hu), Nat.one_mul] at hd
      exact hd
  | @seq p q s t u _ _ ih1 ih2 =>
    intro a prog hu hat
    have hs := at_app prog a _ _ hat
    have h2 := hs.2
    rw [len_comp] at h2
    exact reach_eq (rtrans (ih1 a prog hu.1 hs.1) (ih2 (a + clen p) prog hu.2 h2))
      (by show (⟨a + clen p + clen q, _, _⟩ : Cf) = ⟨a + (clen p + clen q), _, _⟩; rw [Nat.add_assoc])
  | @loopZ r p s hz =>
    intro a prog hu hat
    have hs := at_app prog a _ _ hat
    exact reach_eq (dt_no prog a (P r) (P r) _ _ hs.1 (E s K) (E_ndvd s r hz K))
      (by show (⟨a + dtLen (P r) (P r) + clen p + 1, _, _⟩ : Cf) = ⟨a + (dtLen (P r) (P r) + clen p + 1), _, _⟩
          rw [Nat.add_assoc, Nat.add_assoc, Nat.add_assoc])
  | @loopS r p s t u hnz _ _ ih1 ih2 =>
    intro a prog hu hat
    have hs := at_app prog a _ _ hat
    have h2 := hs.2
    rw [len_dtC] at h2
    have hs2 := at_app prog _ _ _ h2
    have h3 := hs2.2
    rw [len_comp] at h3
    match hv : s r with
    | 0 => exact absurd hv hnz
    | v + 1 =>
      have hd := dt_yes prog a (P r) (P r) _ _ hs.1 (E (upd s r v) K)
      rw [← (show E s K = (P r + 1) * E (upd s r v) K from E_dec s r v K hv hu.1)] at hd
      exact rtrans hd (rtrans (ih1 _ prog hu.2 hs2.1) (rtrans (s_dec1z h3.1) (ih2 a prog hu hat)))

/-! ## the whole program -/

theorem fetch_mid : (pre : List Ins) → (i : Ins) → (rest : List Ins) → fetch (pre ++ i :: rest) (len pre) = i
  | [], _, _ => rfl
  | _ :: pre, i, rest => fetch_mid pre i rest

theorem app_assoc' : (l1 l2 l3 : List Ins) → (l1 ++ l2) ++ l3 = l1 ++ (l2 ++ l3)
  | [], _, _ => rfl
  | i :: l1, l2, l3 => congrArg (List.cons i) (app_assoc' l1 l2 l3)

theorem at_prefix : (l pre r : List Ins) → At (pre ++ (l ++ r)) (len pre) l
  | [], _, _ => trivial
  | i :: l, pre, r => ⟨fetch_mid pre i (l ++ r), by
      have := at_prefix l (pre ++ [i]) r
      rw [app_assoc' pre [i] (l ++ r), len_app] at this
      exact this⟩

def prog (p : Prog) : List Ins := comp p 0 ++ [.halt]

/-- Minsky: a structured register machine is simulated by a two-counter machine -/
theorem minsky (K : Nat) (p : Prog) (hu : Uses K p) {s s' : Store} (h : Run p s s') :
    Reach (prog p) ⟨0, E s K, 0⟩ ⟨clen p, E s' K, 0⟩ ∧ fetch (prog p) (clen p) = .halt := by
  have hat : At (prog p) 0 (comp p 0) := at_prefix (comp p 0) [] [.halt]
  refine ⟨reach_eq (comp_ok K h 0 (prog p) hu hat) (by rw [Nat.zero_add]), ?_⟩
  have := fetch_mid (comp p 0) .halt []
  rw [len_comp] at this
  exact this

theorem haltNum_zero : (i : Ins) → haltNum i = 0 → i = .halt
  | .halt, _ => rfl
  | .inc0 _, h => Nat.noConfusion h
  | .inc1 _, h => Nat.noConfusion h
  | .dec0 _ _, h => Nat.noConfusion h
  | .dec1 _ _, h => Nat.noConfusion h

theorem run_stays (pr : List Ins) (c : Cf) (m : Nat) (h : fetch pr (run pr c m).pc = .halt) :
    (k : Nat) → run pr c (m + k) = run pr c m
  | 0 => rfl
  | k + 1 => by
    show step pr (run pr c (m + k)) = _
    rw [run_stays pr c m h k]
    show exec (fetch pr (run pr c m).pc) _ = _
    rw [h]
    rfl

/-- structured register machines → μ-recursive functions, through the two-counter machine -/
theorem register_to_mu (K : Nat) (p : Prog) (hu : Uses K p) {s s' : Store} (h : Run p s s') :
    Ev (F (prog p)) [E s K, 0] (E s' K) := by
  match minsky K p hu h with
  | ⟨⟨t0, ht⟩, hh⟩ =>
    have h0 : hv (prog p) (E s K) 0 t0 = 0 := by
      unfold hv
      rw [ht]
      show haltNum (fetch (prog p) (clen p)) = 0
      rw [hh]
      rfl
    have R := reg_to_mu (prog p) (E s K) 0 t0 h0
    generalize lsearch (prog p) (E s K) 0 0 t0 = m at R
    have hm : m ≤ t0 := (Nat.lt_or_ge t0 m).elim (fun hl => absurd h0 (R.2.2 t0 hl)) id
    obtain ⟨k, hk⟩ := LibGcd.le_dest hm
    have hf : fetch (prog p) (run (prog p) ⟨0, E s K, 0⟩ m).pc = .halt := haltNum_zero _ R.2.1
    have e : run (prog p) ⟨0, E s K, 0⟩ m = ⟨clen p, E s' K, 0⟩ := by
      rw [← run_stays _ _ m hf k, ← hk, ht]
    have R1 := R.1
    rw [e] at R1
    exact R1

end Mk

end Found

#print axioms Found.Mk.len_comp
#print axioms Found.Mk.comp_ok
#print axioms Found.Mk.at_prefix
#print axioms Found.Mk.minsky
#print axioms Found.Mk.run_stays
#print axioms Found.Mk.register_to_mu

found/Modal.lean

lean_all/found/Modal.lean

/-
  T67. found/Modal — modal logics K, T, S4, S5 as conditions on □.

  * Modal formulas, Kripke semantics in Ω over a frame (W, R).
  * A Hilbert system with optional axioms T, 4, B, 5; soundness on frames
    with the matching conditions.
  * Correspondence (both directions, constructive):
      T ⇔ reflexive, 4 ⇔ transitive, B ⇔ symmetric, 5 ⇔ euclidean.
    So K, T, S4, S5 are the same □ with conditions on R.
  * Upward-closed predicates on a preorder form a Heyting algebra; hence
    intuitionistic logic (T65) is sound in Kripke models.
  * Gödel–McKinsey–Tarski: on every S4 frame the intuitionistic value of φ
    (with boxed valuation) is the modal value of its box translation.

  Lean 4 core only, hand-written proofs, no axioms.

  CIC dependence: Kripke semantics is Prop-valued.
-/
import found.Classical


universe u

namespace Found

namespace Modal

inductive MForm where
  | var : Nat → MForm
  | bot : MForm
  | and : MForm → MForm → MForm
  | or : MForm → MForm → MForm
  | imp : MForm → MForm → MForm
  | box : MForm → MForm
  | dia : MForm → MForm

section Sem
variable {W : Type u} (R : W → W → Prop) (V : W → Nat → Prop)

def sat : W → MForm → Prop
  | w, .var n => V w n
  | _, .bot => False
  | w, .and a b => sat w a ∧ sat w b
  | w, .or a b => sat w a ∨ sat w b
  | w, .imp a b => sat w a → sat w b
  | w, .box a => ∀ v, R w v → sat v a
  | w, .dia a => ∃ v, R w v ∧ sat v a

end Sem

structure MLogic where
  t : Bool
  four : Bool
  b : Bool
  five : Bool

def K : MLogic := ⟨false, false, false, false⟩
def LT : MLogic := ⟨true, false, false, false⟩
def S4 : MLogic := ⟨true, true, false, false⟩
def S5 : MLogic := ⟨true, true, true, false⟩

inductive HD (L : MLogic) : MForm → Prop where
  | k1 (a b : MForm) : HD L (.imp a (.imp b a))
  | s (a b c : MForm) : HD L (.imp (.imp a (.imp b c)) (.imp (.imp a b) (.imp a c)))
  | andI (a b : MForm) : HD L (.imp a (.imp b (.and a b)))
  | andE1 (a b : MForm) : HD L (.imp (.and a b) a)
  | andE2 (a b : MForm) : HD L (.imp (.and a b) b)
  | orI1 (a b : MForm) : HD L (.imp a (.or a b))
  | orI2 (a b : MForm) : HD L (.imp b (.or a b))
  | orE (a b c : MForm) : HD L (.imp (.imp a c) (.imp (.imp b c) (.imp (.or a b) c)))
  | efq (a : MForm) : HD L (.imp .bot a)
  | kax (a b : MForm) : HD L (.imp (.box (.imp a b)) (.imp (.box a) (.box b)))
  | tax (a : MForm) : L.t = true → HD L (.imp (.box a) a)
  | fourax (a : MForm) : L.four = true → HD L (.imp (.box a) (.box (.box a)))
  | bax (a : MForm) : L.b = true → HD L (.imp a (.box (.dia a)))
  | fiveax (a : MForm) : L.five = true → HD L (.imp (.dia a) (.box (.dia a)))
  | mp {a b : MForm} : HD L (.imp a b) → HD L a → HD L b
  | nec {a : MForm} : HD L a → HD L (.box a)

section Frames
variable {W : Type u} (R : W → W → Prop)

def Refl : Prop := ∀ w, R w w
def Trans : Prop := ∀ u v w, R u v → R v w → R u w
def Symm : Prop := ∀ u v, R u v → R v u
def Eucl : Prop := ∀ u v w, R u v → R u w → R v w

structure FrameOK (L : MLogic) : Prop where
  refl : L.t = true → Refl R
  trans : L.four = true → Trans R
  symm : L.b = true → Symm R
  eucl : L.five = true → Eucl R

/-- soundness of the Hilbert system on frames satisfying the conditions -/
theorem soundness {L : MLogic} (hF : FrameOK R L) {φ : MForm} (d : HD L φ) :
    ∀ (V : W → Nat → Prop) w, sat R V w φ := by
  induction d with
  | k1 => exact fun _ _ x _ => x
  | s => exact fun _ _ f g x => f x (g x)
  | andI => exact fun _ _ x y => ⟨x, y⟩
  | andE1 => exact fun _ _ x => x.1
  | andE2 => exact fun _ _ x => x.2
  | orI1 => exact fun _ _ x => Or.inl x
  | orI2 => exact fun _ _ x => Or.inr x
  | orE => exact fun _ _ f g x => x.elim f g
  | efq => exact fun _ _ x => x.elim
  | kax => exact fun _ _ f g v r => f v r (g v r)
  | tax _ e => exact fun _ w x => x w (hF.refl e w)
  | fourax _ e => exact fun _ w x v r u r' => x u (hF.trans e w v u r r')
  | bax _ e => exact fun _ w x v r => ⟨w, hF.symm e w v r, x⟩
  | fiveax _ e => exact fun _ w x v r => match x with
      | ⟨u, r', y⟩ => ⟨u, hF.eucl e w v u r r', y⟩
  | mp _ _ ihf iha => exact fun V w => ihf V w (iha V w)
  | nec _ ih => exact fun V _ v _ => ih V v

/-! ## Correspondence -/

theorem t_iff_refl :
    (∀ (V : W → Nat → Prop) w, sat R V w (.imp (.box (.var 0)) (.var 0))) ↔ Refl R :=
  ⟨fun h w => h (fun v _ => R w v) w (fun _ r => r),
   fun hr _ w x => x w (hr w)⟩

theorem four_iff_trans :
    (∀ (V : W → Nat → Prop) w, sat R V w (.imp (.box (.var 0)) (.box (.box (.var 0))))) ↔ Trans R :=
  ⟨fun h u v w r r' => h (fun x _ => R u x) u (fun _ k => k) v r w r',
   fun ht _ w x v r u r' => x u (ht w v u r r')⟩

theorem b_iff_symm :
    (∀ (V : W → Nat → Prop) w, sat R V w (.imp (.var 0) (.box (.dia (.var 0))))) ↔ Symm R :=
  ⟨fun h u v r => match h (fun x _ => x = u) u rfl v r with
     | ⟨_, r', e⟩ => e ▸ r',
   fun hs _ w x v r => ⟨w, hs w v r, x⟩⟩

theorem five_iff_eucl :
    (∀ (V : W → Nat → Prop) w, sat R V w (.imp (.dia (.var 0)) (.box (.dia (.var 0))))) ↔ Eucl R :=
  ⟨fun h u v w r r' => match h (fun x _ => x = w) u ⟨w, r', rfl⟩ v r with
     | ⟨_, r'', e⟩ => e ▸ r'',
   fun he _ w x v r => match x with
     | ⟨u, r', y⟩ => ⟨u, he w v u r r', y⟩⟩

/-- S5 frames are exactly equivalence relations; with T, 5 gives B and 4 -/
theorem eucl_refl_symm (hr : Refl R) (he : Eucl R) : Symm R :=
  fun u v r => he u v u r (hr u)

theorem eucl_refl_trans (hr : Refl R) (he : Eucl R) : Trans R :=
  fun u v w r r' => he v u w (eucl_refl_symm R hr he u v r) r'

end Frames

/-! ## Kripke models of intuitionistic logic = upsets of a preorder -/

section Up
variable {W : Type u} (R : W → W → Prop) (hr : Refl R) (ht : Trans R)

structure Up where
  p : W → Prop
  up : ∀ w v, R w v → p w → p v

def UpHA : HA.{u} where
  car := Up R
  le P Q := ∀ w, P.p w → Q.p w
  le_refl _ _ h := h
  le_trans h k w x := k w (h w x)
  top := ⟨fun _ => True, fun _ _ _ _ => trivial⟩
  bot := ⟨fun _ => False, fun _ _ _ h => h⟩
  meet P Q := ⟨fun w => P.p w ∧ Q.p w, fun w v r h => ⟨P.up w v r h.1, Q.up w v r h.2⟩⟩
  join P Q := ⟨fun w => P.p w ∨ Q.p w,
    fun w v r h => h.elim (fun a => Or.inl (P.up w v r a)) (fun b => Or.inr (Q.up w v r b))⟩
  imp P Q := ⟨fun w => ∀ v, R w v → P.p v → Q.p v, fun _ _ r h u r' x => h u (ht _ _ _ r r') x⟩
  le_top _ _ _ := trivial
  bot_le _ _ h := h.elim
  meet_l _ _ _ h := h.1
  meet_r _ _ _ h := h.2
  meet_univ h k w x := ⟨h w x, k w x⟩
  join_l _ _ _ h := Or.inl h
  join_r _ _ _ h := Or.inr h
  join_univ h k w x := x.elim (h w) (k w)
  imp_adj {_ _ C} := ⟨fun h w c v r x => h v ⟨C.up w v r c, x⟩,
                      fun h w x => h w x.1 w (hr w) x.2⟩

/-- the boxed valuation: an upset for any valuation -/
def boxV (V : W → Nat → Prop) (n : Nat) : Up R :=
  ⟨fun w => ∀ v, R w v → V v n, fun _ _ r h u r' => h u (ht _ _ _ r r')⟩

/-- the Gödel–McKinsey–Tarski translation -/
def gmt : Prp.Form → MForm
  | .var n => .box (.var n)
  | .bot => .bot
  | .and a b => .and (gmt a) (gmt b)
  | .or a b => .or (gmt a) (gmt b)
  | .imp a b => .box (.imp (gmt a) (gmt b))

theorem gmt_sem (V : W → Nat → Prop) : (φ : Prp.Form) → ∀ w,
    (Prp.interp (UpHA R hr ht) (boxV R ht V) (UpHA R hr ht).bot φ).p w ↔ sat R V w (gmt φ)
  | .var _, _ => Iff.rfl
  | .bot, _ => Iff.rfl
  | .and a b, w =>
    ⟨fun h => ⟨(gmt_sem V a w).mp h.1, (gmt_sem V b w).mp h.2⟩,
     fun h => ⟨(gmt_sem V a w).mpr h.1, (gmt_sem V b w).mpr h.2⟩⟩
  | .or a b, w =>
    ⟨fun h => h.elim (fun x => Or.inl ((gmt_sem V a w).mp x)) (fun y => Or.inr ((gmt_sem V b w).mp y)),
     fun h => h.elim (fun x => Or.inl ((gmt_sem V a w).mpr x)) (fun y => Or.inr ((gmt_sem V b w).mpr y))⟩
  | .imp a b, _ =>
    ⟨fun h v r x => (gmt_sem V b v).mp (h v r ((gmt_sem V a v).mpr x)),
     fun h v r x => (gmt_sem V b v).mpr (h v r ((gmt_sem V a v).mp x))⟩

include hr ht in
/-- every intuitionistic theorem becomes, by GMT, valid on every S4 frame -/
theorem gmt_valid {φ : Prp.Form} (d : Prp.PD true (fun _ => False) φ)
    (V : W → Nat → Prop) (w : W) : sat R V w (gmt φ) :=
  (gmt_sem R hr ht V φ w).mp
    (Prp.valid (UpHA R hr ht) d (boxV R ht V) (UpHA R hr ht).bot (fun _ _ _ h => h.elim) w trivial)

end Up

/-- the two-world S4 frame 0 ≤ 1 refutes the GMT image of p ∨ ¬p -/
theorem gmt_lem_fails :
    ¬ sat (fun (a b : Bool) => a = false ∨ b = true) (fun w _ => w = true) false
      (gmt (.or (.var 0) (Prp.neg (.var 0)))) :=
  fun h => h.elim
    (fun x => Bool.noConfusion (x false (Or.inl rfl)))
    (fun x => x true (Or.inl rfl) (fun _ r => r.elim (fun e => Bool.noConfusion e) (fun e => e)))

end Modal

end Found

#print axioms Found.Modal.soundness
#print axioms Found.Modal.t_iff_refl
#print axioms Found.Modal.four_iff_trans
#print axioms Found.Modal.b_iff_symm
#print axioms Found.Modal.five_iff_eucl
#print axioms Found.Modal.eucl_refl_symm
#print axioms Found.Modal.eucl_refl_trans
#print axioms Found.Modal.UpHA
#print axioms Found.Modal.gmt_sem
#print axioms Found.Modal.gmt_valid
#print axioms Found.Modal.gmt_lem_fails

found/ModalExt.lean

lean_all/found/ModalExt.lean

/-
  T113. found/ModalExt — deontic logic and extensions as points on the
  relation coordinate R (T67, T72 reused).

  * Deontic D: □p → ◇p  ⟺  R serial (every world has a successor).
  * D is the condition "seriality" on R, between K (none) and T (reflexive):
    reflexive ⇒ serial.  Deontic modalities are the same □ with this value.
  * Epistemic/temporal extensions are T72's (common knowledge, tense
    adjunctions) — reused, not re-proved.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.TemporalEpistemic

namespace Found

namespace Modal

universe u

variable {W : Type u} (R : W → W → Prop)

def Serial : Prop := ∀ w, ∃ v, R w v

theorem d_iff_serial :
    (∀ (V : W → Nat → Prop) w, sat R V w (.imp (.box (.var 0)) (.dia (.var 0)))) ↔ Serial R :=
  ⟨fun h w => match h (fun _ _ => True) w (fun _ _ => trivial) with
     | ⟨v, r, _⟩ => ⟨v, r⟩,
   fun hs _ w x => match hs w with
     | ⟨v, r⟩ => ⟨v, r, x v r⟩⟩

theorem refl_serial (hr : Refl R) : Serial R := fun w => ⟨w, hr w⟩

end Modal

end Found

#print axioms Found.Modal.d_iff_serial
#print axioms Found.Modal.refl_serial

found/ModalMu.lean

lean_all/found/ModalMu.lean

/-
  T171. found/ModalMu — the modal μ-calculus as a point: least and greatest
  fixed points of monotone operators on the predicate lattice Ω^W of the
  relation coordinate (Knaster–Tarski, impredicative form).

  * lfp f := ⋂ {P | f P ⊆ P}, gfp f := ⋃ {P | P ⊆ f P}; fixed points, least /
    greatest, induction and coinduction rules.
  * EF p = μX. p ∨ ◇X is reachability along R* (T138's star) — the line
    μ-calculus ↔ CTL/PDL.

  Lean 4 core only, hand-written proofs, no axioms.
  CIC dependence: impredicative Prop (the ∀ P in lfp).
-/
import found.Dynamic

namespace Found

namespace Mu

universe u

variable {W : Type u}

def Le (P Q : W → Prop) : Prop := ∀ w, P w → Q w

def Mono (f : (W → Prop) → (W → Prop)) : Prop := ∀ {P Q}, Le P Q → Le (f P) (f Q)

def lfp (f : (W → Prop) → (W → Prop)) : W → Prop := fun w => ∀ P, Le (f P) P → P w
def gfp (f : (W → Prop) → (W → Prop)) : W → Prop := fun w => ∃ P, Le P (f P) ∧ P w

variable {f : (W → Prop) → (W → Prop)} (hf : Mono f)

theorem lfp_induct (P : W → Prop) (h : Le (f P) P) : Le (lfp f) P := fun _ x => x P h

include hf in
theorem f_lfp_le : Le (f (lfp f)) (lfp f) :=
  fun _ x P hP => hP _ (hf (lfp_induct P hP) _ x)

include hf in
theorem lfp_le_f : Le (lfp f) (f (lfp f)) :=
  lfp_induct _ (hf (f_lfp_le hf))

theorem gfp_coinduct (P : W → Prop) (h : Le P (f P)) : Le P (gfp f) := fun _ x => ⟨P, h, x⟩

include hf in
theorem gfp_le_f : Le (gfp f) (f (gfp f)) :=
  fun w ⟨P, hP, x⟩ => hf (gfp_coinduct P hP) w (hP w x)

include hf in
theorem f_le_gfp : Le (f (gfp f)) (gfp f) :=
  gfp_coinduct _ (hf (gfp_le_f hf))

/-! ## EF p = μX. p ∨ ◇X = reachability -/

variable (R : W → W → Prop) (p : W → Prop)

def efOp (X : W → Prop) : W → Prop := fun w => p w ∨ ∃ v, R w v ∧ X v

theorem efOp_mono : Mono (efOp R p) :=
  fun h _ x => x.elim Or.inl (fun ⟨v, r, hv⟩ => Or.inr ⟨v, r, h v hv⟩)

def reach (w : W) : Prop := ∃ v, Dyn.star R w v ∧ p v

theorem ef_eq_reach (w : W) : lfp (efOp R p) w ↔ reach R p w := by
  constructor
  · intro h
    exact h (reach R p) (fun x hx => hx.elim (fun hp => ⟨x, .refl x, hp⟩)
      (fun ⟨v, r, ⟨u, s, hu⟩⟩ => ⟨u, .step r s, hu⟩))
  · intro ⟨v, s, hv⟩
    induction s with
    | refl x => exact f_lfp_le (efOp_mono R p) x (Or.inl hv)
    | step r _ ih => exact f_lfp_le (efOp_mono R p) _ (Or.inr ⟨_, r, ih hv⟩)

end Mu

end Found

#print axioms Found.Mu.lfp_induct
#print axioms Found.Mu.f_lfp_le
#print axioms Found.Mu.lfp_le_f
#print axioms Found.Mu.gfp_coinduct
#print axioms Found.Mu.gfp_le_f
#print axioms Found.Mu.f_le_gfp
#print axioms Found.Mu.ef_eq_reach

found/ModelCat.lean

lean_all/found/ModelCat.lean

/-
  T133. found/ModelCat — Quillen model structures as a parameter value on
  the category parameter C (setoid homs, no quotient).

  * Isomorphisms, 2-out-of-3 for isomorphisms (general).
  * Lifting property of a square, model structure (W, Cof, Fib): W has
    2-out-of-3 and contains identities, Cof ∩ W ⧄ Fib, Cof ⧄ Fib ∩ W,
    both factorizations.
  * Instance, for every E-category C: the trivial model structure
    (W = isomorphisms, Cof = Fib = all maps).  Its homotopy category is C.
  Known range only (simplicial/Kan instance: T134; Shulman's path: T136).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Cat

namespace Found

namespace Model

universe u v

variable {C : ECat.{u, v}}

def IsIso {a b : C.Ob} (f : C.Hom a b) : Prop :=
  ∃ g : C.Hom b a, C.heq (C.comp f g) (C.id a) ∧ C.heq (C.comp g f) (C.id b)

theorem iso_id (a : C.Ob) : IsIso (C.id a) := ⟨C.id a, C.id_comp _, C.id_comp _⟩

theorem iso_comp {a b c : C.Ob} {f : C.Hom a b} {g : C.Hom b c} (hf : IsIso f) (hg : IsIso g) :
    IsIso (C.comp f g) :=
  match hf, hg with
  | ⟨f', f1, f2⟩, ⟨g', g1, g2⟩ =>
    ⟨C.comp g' f',
     C.htrans (C.assoc _ _ _) (C.htrans (C.rcong f (C.htrans (C.hsymm (C.assoc _ _ _))
       (C.htrans (C.lcong g1 f') (C.id_comp f')))) f1),
     C.htrans (C.assoc _ _ _) (C.htrans (C.rcong g' (C.htrans (C.hsymm (C.assoc _ _ _))
       (C.htrans (C.lcong f2 g) (C.id_comp g)))) g2)⟩

theorem iso_inv_left {a b c : C.Ob} {f : C.Hom a b} {f' : C.Hom b a} {g : C.Hom b c} {h : C.Hom c a}
    (f2 : C.heq (C.comp f' f) (C.id b)) (h1 : C.heq (C.comp (C.comp f g) h) (C.id a)) :
    C.heq (C.comp g (C.comp h f)) (C.id b) :=
  C.htrans (C.hsymm (C.id_comp _)) (C.htrans (C.lcong (C.hsymm f2) _)
    (C.htrans (C.assoc _ _ _) (C.htrans (C.rcong f' (C.htrans (C.hsymm (C.assoc _ _ _))
      (C.htrans (C.hsymm (C.assoc _ _ _)) (C.htrans (C.lcong h1 f) (C.id_comp f))))) f2)))

/-- 2-out-of-3: f and f ; g isomorphisms ⇒ g isomorphism -/
theorem iso_right {a b c : C.Ob} {f : C.Hom a b} {g : C.Hom b c} (hf : IsIso f)
    (hfg : IsIso (C.comp f g)) : IsIso g :=
  match hf, hfg with
  | ⟨_, _, f2⟩, ⟨h, h1, h2⟩ => ⟨C.comp h f, iso_inv_left f2 h1, C.htrans (C.assoc _ _ _) h2⟩

/-- lifting property of i against p -/
def Lifts {a b x y : C.Ob} (i : C.Hom a b) (p : C.Hom x y) : Prop :=
  ∀ (u : C.Hom a x) (w : C.Hom b y), C.heq (C.comp u p) (C.comp i w) →
    ∃ h : C.Hom b x, C.heq (C.comp i h) u ∧ C.heq (C.comp h p) w

def Cls (C : ECat.{u, v}) : Type (max u v) := ∀ {a b : C.Ob}, C.Hom a b → Prop

structure ModelStr (C : ECat.{u, v}) where
  W : Cls C
  Cof : Cls C
  Fib : Cls C
  W_id : ∀ a, W (C.id a)
  W_comp : ∀ {a b c} {f : C.Hom a b} {g : C.Hom b c}, W f → W g → W (C.comp f g)
  W_right : ∀ {a b c} {f : C.Hom a b} {g : C.Hom b c}, W f → W (C.comp f g) → W g
  lift1 : ∀ {a b x y} {i : C.Hom a b} {p : C.Hom x y}, Cof i → W i → Fib p → Lifts i p
  lift2 : ∀ {a b x y} {i : C.Hom a b} {p : C.Hom x y}, Cof i → Fib p → W p → Lifts i p
  fact1 : ∀ {a b} (f : C.Hom a b), ∃ m, ∃ i : C.Hom a m, ∃ p : C.Hom m b,
    Cof i ∧ W i ∧ Fib p ∧ C.heq (C.comp i p) f
  fact2 : ∀ {a b} (f : C.Hom a b), ∃ m, ∃ i : C.Hom a m, ∃ p : C.Hom m b,
    Cof i ∧ Fib p ∧ W p ∧ C.heq (C.comp i p) f

theorem lift_of_iso_left {a b x y : C.Ob} {i : C.Hom a b} (p : C.Hom x y) (hi : IsIso i) : Lifts i p :=
  fun u w e => match hi with
  | ⟨j, i1, i2⟩ =>
    ⟨C.comp j u,
     C.htrans (C.hsymm (C.assoc _ _ _)) (C.htrans (C.lcong i1 u) (C.id_comp u)),
     C.htrans (C.assoc _ _ _) (C.htrans (C.rcong j e)
       (C.htrans (C.hsymm (C.assoc _ _ _)) (C.htrans (C.lcong i2 w) (C.id_comp w))))⟩

theorem lift_of_iso_right {a b x y : C.Ob} (i : C.Hom a b) {p : C.Hom x y} (hp : IsIso p) : Lifts i p :=
  fun u w e => match hp with
  | ⟨q, p1, p2⟩ =>
    ⟨C.comp w q,
     C.htrans (C.hsymm (C.assoc _ _ _)) (C.htrans (C.lcong (C.hsymm e) q)
       (C.htrans (C.assoc _ _ _) (C.htrans (C.rcong u p1) (C.comp_id u)))),
     C.htrans (C.assoc _ _ _) (C.htrans (C.rcong w p2) (C.comp_id w))⟩

/-- every E-category carries the trivial model structure -/
def trivialModel (C : ECat.{u, v}) : ModelStr C where
  W := fun f => IsIso f
  Cof := fun _ => True
  Fib := fun _ => True
  W_id a := iso_id a
  W_comp hf hg := iso_comp hf hg
  W_right hf hfg := iso_right hf hfg
  lift1 _ hi _ := lift_of_iso_left _ hi
  lift2 _ _ hp := lift_of_iso_right _ hp
  fact1 f := ⟨_, C.id _, f, True.intro, iso_id _, True.intro, C.id_comp f⟩
  fact2 f := ⟨_, f, C.id _, True.intro, True.intro, iso_id _, C.comp_id f⟩

end Model

end Found

#print axioms Found.Model.iso_comp
#print axioms Found.Model.iso_right
#print axioms Found.Model.lift_of_iso_left
#print axioms Found.Model.lift_of_iso_right
#print axioms Found.Model.trivialModel

found/MonoidalClosed.lean

lean_all/found/MonoidalClosed.lean

/-
  T150. found/MonoidalClosed — closed (monoidal) structure as a value on the
  category parameter: for each object b an adjunction (− ⊗ b) ⊣ (b ⊸ −)
  (T61's Adj; coherence of ⊗ is not needed for the statements here).

  Instances (no re-proofs):
  * every CCC (T62): ⊗ = ×, ⊸ = ⇒, the adjunction K.adj;
  * every residuated order with ! (T68's CRL) seen as a thin category:
    the linear-logic models are exactly the order shadows — the line
    linear logic (T68) ↔ closed monoidal categories.

  Lean 4 core only, hand-written proofs, no axioms.
  CIC dependence: homs of a thin category = PLift (Prop) (morphism equivalence = True by proof irrelevance).
-/
import found.CCC
import found.Linear

namespace Found

namespace MonClosed

universe u v

structure Closed (C : ECat.{u, v}) where
  F : C.Ob → EFun C C
  G : C.Ob → EFun C C
  adj : ∀ b, Adj (F b) (G b)

/-- every CCC is closed -/
def ofCCC {C : ECat.{u, v}} (K : CCC C) : Closed C :=
  ⟨K.prodF, K.expF, K.adj⟩

/-! ## a preorder as a thin category -/

def thin (M : Lin.CRL.{u}) : ECat.{u, 0} where
  Ob := M.car
  Hom a b := PLift (M.le a b)
  heq _ _ := True
  hrefl _ := trivial
  hsymm _ := trivial
  htrans _ _ := trivial
  id a := ⟨M.le_refl a⟩
  comp f g := ⟨M.le_trans f.down g.down⟩
  comp_resp _ _ := trivial
  id_comp _ := trivial
  comp_id _ := trivial
  assoc _ _ _ := trivial

def tensF (M : Lin.CRL.{u}) (b : M.car) : EFun (thin M) (thin M) where
  obj a := M.tens a b
  map h := ⟨M.tens_mono h.down (M.le_refl b)⟩
  map_resp _ := trivial
  map_id _ := trivial
  map_comp _ _ := trivial

def lolliF (M : Lin.CRL.{u}) (b : M.car) : EFun (thin M) (thin M) where
  obj c := M.lolli b c
  map h := ⟨M.adj.mp (M.le_trans (M.adj.mpr (M.le_refl _)) h.down)⟩
  map_resp _ := trivial
  map_id _ := trivial
  map_comp _ _ := trivial

/-- every residuated order is closed (as a thin category) -/
def ofCRL (M : Lin.CRL.{u}) : Closed (thin M) where
  F := tensF M
  G := lolliF M
  adj b :=
    { phi := fun h => ⟨M.adj.mp (M.le_trans (M.tens_comm b _) h.down)⟩
      psi := fun h => ⟨M.le_trans (M.tens_comm _ b) (M.adj.mpr h.down)⟩
      phi_resp := fun _ => trivial
      psi_resp := fun _ => trivial
      phi_psi := fun _ => trivial
      psi_phi := fun _ => trivial
      nat_l := fun _ _ => trivial
      nat_r := fun _ _ => trivial }

/-- the residuation law read back from the closed structure -/
theorem residuation (M : Lin.CRL.{u}) (a b c : M.car) :
    M.le (M.tens a b) c → M.le a (M.lolli b c) :=
  fun h => ((ofCRL M).adj b).phi (a := a) (b := c) ⟨h⟩ |>.down

end MonClosed

end Found

#print axioms Found.MonClosed.ofCCC
#print axioms Found.MonClosed.thin
#print axioms Found.MonClosed.ofCRL
#print axioms Found.MonClosed.residuation

found/Mostowski.lean

lean_all/found/Mostowski.lean

/-
  T109. found/Mostowski — the well-founded side: Mostowski collapse and
  Aczel's tree universe V (the initial algebra; T105 is the final coalgebra).

  * Graph level: on a well-founded (accessible) extensional graph every
    bisimulation is contained in equality — the Mostowski collapse
    n ↦ (set of collapses of children) is injective.  Well-founded graphs
    have no loops.
  * Aczel's V: well-founded trees sup (I, f) with bisimilarity defined by
    recursion (a setoid, no quotient); membership; extensionality holds by
    definition, and ∈-induction (foundation) holds: no x ∈ x, no 2-cycles.
  * Contrast with AFA (T105): there Ω ∈ Ω; here ¬ x ∈ x — initial algebra
    versus final coalgebra of the same powerset functor.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.AFA
import found.Base

namespace Found

namespace WF

/-! ## Graph level -/

section Graph
variable {N : Type} (E : N → N → Prop)

def Child (c n : N) : Prop := E n c

def WellFounded' : Prop := ∀ n, Acc (Child E) n

def Extensional : Prop :=
  ∀ n m, (∀ c, E n c → E m c) → (∀ c, E m c → E n c) → n = m

def GBisim (S : N → N → Prop) : Prop :=
  ∀ a b, S a b → (∀ a', E a a' → ∃ b', E b b' ∧ S a' b') ∧ (∀ b', E b b' → ∃ a', E a a' ∧ S a' b')

/-- Mostowski: on a well-founded extensional graph, bisimilar nodes are equal -/
theorem bisim_eq (hW : WellFounded' E) (hX : Extensional E) {S : N → N → Prop} (hS : GBisim E S) :
    ∀ n m, S n m → n = m := by
  intro n
  induction hW n with
  | intro n _ ih =>
    intro m s
    apply hX n m
    · intro c e
      match (hS n m s).1 c e with
      | ⟨d, e', s'⟩ => exact (ih c e d s') ▸ e'
    · intro d e
      match (hS n m s).2 d e with
      | ⟨c, e', s'⟩ => exact (ih c e' d s').symm ▸ e'

/-- the collapse is injective: equal decorations (in the AFA universe) only for equal nodes -/
theorem collapse_injective (hW : WellFounded' E) (hX : Extensional E) (n m : N)
    (h : AFA.Eqv (AFA.dec ⟨N, E⟩ n) (AFA.dec ⟨N, E⟩ m)) : n = m :=
  match h with
  | ⟨S, hS, s⟩ => bisim_eq E hW hX (S := S) hS n m s

/-- well-founded graphs have no loops -/
theorem no_loop (hW : WellFounded' E) (n : N) : ¬ E n n := by
  induction hW n with
  | intro n _ ih => exact fun e => ih n e e

end Graph

/-! ## Aczel's V: well-founded trees -/

universe uV

inductive V : Type (uV + 1) where
  | sup (I : Type uV) (f : I → V) : V

def V.I : V.{uV} → Type uV
  | .sup I _ => I

def V.f : (x : V.{uV}) → x.I → V.{uV}
  | .sup _ f => f

/-- bisimilarity of trees, by recursion -/
def Eq' : V.{uV} → V.{uV} → Prop
  | .sup _ f, .sup _ g => (∀ i, ∃ j, Eq' (f i) (g j)) ∧ (∀ j, ∃ i, Eq' (f i) (g j))

def Mem (x y : V.{uV}) : Prop := ∃ j, Eq' x (y.f j)

theorem eq_refl : (x : V.{uV}) → Eq' x x
  | .sup _ f => ⟨fun i => ⟨i, eq_refl (f i)⟩, fun j => ⟨j, eq_refl (f j)⟩⟩

theorem eq_symm : (x y : V.{uV}) → Eq' x y → Eq' y x
  | .sup _ _, .sup _ _, ⟨h1, h2⟩ =>
    ⟨fun j => match h2 j with | ⟨i, e⟩ => ⟨i, eq_symm _ _ e⟩,
     fun i => match h1 i with | ⟨j, e⟩ => ⟨j, eq_symm _ _ e⟩⟩

theorem eq_trans : (x y z : V.{uV}) → Eq' x y → Eq' y z → Eq' x z
  | .sup _ _, .sup _ _, .sup _ _, ⟨a1, a2⟩, ⟨b1, b2⟩ =>
    ⟨fun i => match a1 i with
       | ⟨j, e⟩ => match b1 j with
         | ⟨k, e'⟩ => ⟨k, eq_trans _ _ _ e e'⟩,
     fun k => match b2 k with
       | ⟨j, e'⟩ => match a2 j with
         | ⟨i, e⟩ => ⟨i, eq_trans _ _ _ e e'⟩⟩

/-- the trees with bisimilarity: Aczel's setoid model of sets -/
def VS : Found.Setd.{uV + 1} := ⟨V.{uV}, Eq', eq_refl, fun h => eq_symm _ _ h, fun h k => eq_trans _ _ _ h k⟩

theorem mem_resp_right {x y z : V.{uV}} (m : Mem x y) (e : Eq' y z) : Mem x z :=
  match y, z, m, e with
  | .sup _ _, .sup _ _, ⟨j, ex⟩, ⟨e1, _⟩ =>
    match e1 j with
    | ⟨k, e'⟩ => ⟨k, eq_trans _ _ _ ex e'⟩

theorem mem_resp_left {x x' y : V.{uV}} (e : Eq' x x') (m : Mem x y) : Mem x' y :=
  match m with
  | ⟨j, ex⟩ => ⟨j, eq_trans _ _ _ (eq_symm _ _ e) ex⟩

/-- extensionality -/
theorem extensionality (x y : V.{uV}) (h : ∀ z : V.{uV}, Mem z x ↔ Mem z y) : Eq' x y :=
  match x, y with
  | .sup _ f, .sup _ g =>
    ⟨fun i => (h (f i)).mp ⟨i, eq_refl _⟩,
     fun j => match (h (g j)).mpr ⟨j, eq_refl _⟩ with
       | ⟨i, e⟩ => ⟨i, eq_symm _ _ e⟩⟩

/-- ∈-induction (foundation as an induction principle, for respecting predicates) -/
theorem mem_induction (P : V.{uV} → Prop) (hP : ∀ x y, Eq' x y → P x → P y)
    (step : ∀ x : V.{uV}, (∀ y : V.{uV}, Mem y x → P y) → P x) : ∀ x : V.{uV}, P x
  | .sup _ f => step _ (fun _ ⟨i, e⟩ => hP _ _ (eq_symm _ _ e) (mem_induction P hP step (f i)))

/-- foundation: no set is a member of itself -/
theorem not_mem_self : ∀ x : V.{uV}, ¬ Mem x x :=
  mem_induction (fun x => ¬ Mem x x)
    (fun _ _ e h m => h (mem_resp_left (eq_symm _ _ e) (mem_resp_right m (eq_symm _ _ e))))
    (fun x ih m => ih x m m)

/-- the initial/final contrast: V.{uV} has foundation, the AFA universe does not -/
theorem wf_vs_afa : (∀ x : V.{uV}, ¬ Mem x x) ∧ ¬ (∀ x : AFA.HS, ¬ AFA.Mem x x) :=
  ⟨not_mem_self, AFA.not_foundation⟩

end WF

end Found

#print axioms Found.WF.bisim_eq
#print axioms Found.WF.collapse_injective
#print axioms Found.WF.no_loop
#print axioms Found.WF.eq_refl
#print axioms Found.WF.eq_symm
#print axioms Found.WF.eq_trans
#print axioms Found.WF.VS
#print axioms Found.WF.mem_resp_right
#print axioms Found.WF.mem_resp_left
#print axioms Found.WF.extensionality
#print axioms Found.WF.mem_induction
#print axioms Found.WF.not_mem_self
#print axioms Found.WF.wf_vs_afa

found/NBG.lean

lean_all/found/NBG.lean

/-
  T91. found/NBG — NBG and Morse–Kelley as points: classes over the IZF
  point V (T88) are ≈-respecting predicates.

  * Classes, class extensionality (a setoid), sets as classes.
  * Comprehension: every respecting predicate is a class — Morse–Kelley's
    impredicative comprehension holds because predicates live in Prop
    (CIC); NBG restricts comprehension to formulas quantifying over sets
    only (its conservativity over ZF is the known line NBG → ZF, cited).
  * A subclass of a set is a set (separation, T88 reused).
  * The universal class (= Russell's class, by foundation T109) is proper.

  Lean 4 core only, hand-written proofs, no axioms.
  CIC dependence: impredicative Prop (MK comprehension).
-/
import found.IZF


universe u

namespace Found

namespace NBG

open WF

structure Cls where
  p : V.{u} → Prop
  resp : IZF.Resp p

def ClsEq (A B : Cls) : Prop := ∀ x : V.{u}, A.p x ↔ B.p x

theorem clsEq_refl (A : Cls) : ClsEq A A := fun _ => Iff.rfl
theorem clsEq_symm {A B : Cls} (h : ClsEq A B) : ClsEq B A := fun x => (h x).symm
theorem clsEq_trans {A B C : Cls} (h : ClsEq A B) (k : ClsEq B C) : ClsEq A C := fun x => (h x).trans (k x)

/-- every set is a class -/
def ofSet (a : V.{u}) : Cls := ⟨fun z => Mem z a, fun _ _ e m => mem_resp_left e m⟩

/-- comprehension (Morse–Kelley form): any respecting predicate is a class -/
def compr (φ : V.{u} → Prop) (h : IZF.Resp φ) : Cls := ⟨φ, h⟩

theorem compr_spec (φ : V.{u} → Prop) (h : IZF.Resp φ) (x : V.{u}) : (compr φ h).p x ↔ φ x := Iff.rfl

/-- a subclass of a set is a set (separation reused) -/
theorem subclass_set (A : Cls) (a : V.{u}) (hA : ∀ x : V.{u}, A.p x → Mem x a) :
    ∃ s : V.{u}, ∀ x : V.{u}, Mem x s ↔ A.p x :=
  ⟨IZF.sep a A.p, fun x => ⟨fun m => ((IZF.sep_spec a A.p A.resp x).mp m).2,
    fun h => (IZF.sep_spec a A.p A.resp x).mpr ⟨hA x h, h⟩⟩⟩

def univ : Cls := ⟨fun _ => True, fun _ _ _ _ => trivial⟩

def russell : Cls := ⟨fun x => ¬ Mem x x,
  fun _ _ e h m => h (mem_resp_left (eq_symm _ _ e) (mem_resp_right m (eq_symm _ _ e)))⟩

/-- by foundation, Russell's class is the universe -/
theorem russell_univ : ClsEq russell univ := fun x => ⟨fun _ => trivial, fun _ => not_mem_self x⟩

/-- the universe is a proper class -/
theorem univ_proper : ¬ ∃ s : V.{u}, ∀ x : V.{u}, Mem x s ↔ univ.p x :=
  fun ⟨s, h⟩ => not_mem_self s ((h s).mpr trivial)

end NBG

end Found

#print axioms Found.NBG.clsEq_trans
#print axioms Found.NBG.ofSet
#print axioms Found.NBG.compr_spec
#print axioms Found.NBG.subclass_set
#print axioms Found.NBG.russell
#print axioms Found.NBG.russell_univ
#print axioms Found.NBG.univ_proper

found/ND.lean

lean_all/found/ND.lean

/-
  T74. found/ND — natural deduction for →, ∧ and its normalization.

  * Derivations are intrinsically typed terms (de Bruijn): `Tm Γ A`.
  * Normal derivations: `Nf` (introductions) over `Ne` (eliminations on a
    hypothesis); only atoms are switched from Ne to Nf (η-long).
  * Normalization by evaluation into the Kripke model of contexts and
    order-preserving embeddings (the presheaf topos over contexts):
    every derivation has a normal derivation of the same formula.
    Everything is structural recursion: no termination_by, no axioms.
  * Consequences: no normal (hence no) closed derivation of an atom
    (consistency), and normal closed derivations end in an introduction.

  Lean 4 core only, hand-written proofs, no axioms.
-/

namespace Found

namespace ND

inductive Ty where
  | base : Nat → Ty
  | arr : Ty → Ty → Ty
  | prod : Ty → Ty → Ty

inductive Var : List Ty → Ty → Type where
  | z {Γ A} : Var (A :: Γ) A
  | s {Γ A B} : Var Γ A → Var (B :: Γ) A

inductive Tm : List Ty → Ty → Type where
  | var {Γ A} : Var Γ A → Tm Γ A
  | lam {Γ A B} : Tm (A :: Γ) B → Tm Γ (.arr A B)
  | app {Γ A B} : Tm Γ (.arr A B) → Tm Γ A → Tm Γ B
  | pair {Γ A B} : Tm Γ A → Tm Γ B → Tm Γ (.prod A B)
  | fst {Γ A B} : Tm Γ (.prod A B) → Tm Γ A
  | snd {Γ A B} : Tm Γ (.prod A B) → Tm Γ B

mutual
inductive Ne : List Ty → Ty → Type where
  | var {Γ A} : Var Γ A → Ne Γ A
  | app {Γ A B} : Ne Γ (.arr A B) → Nf Γ A → Ne Γ B
  | fst {Γ A B} : Ne Γ (.prod A B) → Ne Γ A
  | snd {Γ A B} : Ne Γ (.prod A B) → Ne Γ B
inductive Nf : List Ty → Ty → Type where
  | ne {Γ n} : Ne Γ (.base n) → Nf Γ (.base n)
  | lam {Γ A B} : Nf (A :: Γ) B → Nf Γ (.arr A B)
  | pair {Γ A B} : Nf Γ A → Nf Γ B → Nf Γ (.prod A B)
end

/-! ## Order-preserving embeddings (weakenings) -/

inductive Wk : List Ty → List Ty → Type where
  | nil : Wk [] []
  | skip {Γ Δ A} : Wk Γ Δ → Wk (A :: Γ) Δ
  | keep {Γ Δ A} : Wk Γ Δ → Wk (A :: Γ) (A :: Δ)

def Wk.id : (Γ : List Ty) → Wk Γ Γ
  | [] => .nil
  | _ :: Γ => .keep (Wk.id Γ)

def Wk.comp {Γ Δ Θ : List Ty} : Wk Θ Δ → Wk Δ Γ → Wk Θ Γ
  | .nil, w => w
  | .skip w', w => .skip (Wk.comp w' w)
  | .keep w', .skip w => .skip (Wk.comp w' w)
  | .keep w', .keep w => .keep (Wk.comp w' w)

def renV {Γ Δ : List Ty} {A : Ty} : Wk Δ Γ → Var Γ A → Var Δ A
  | .skip w, v => .s (renV w v)
  | .keep _, .z => .z
  | .keep w, .s v => .s (renV w v)

mutual
def renNe {Γ Δ : List Ty} {A : Ty} (w : Wk Δ Γ) : Ne Γ A → Ne Δ A
  | .var v => .var (renV w v)
  | .app n m => .app (renNe w n) (renNf w m)
  | .fst n => .fst (renNe w n)
  | .snd n => .snd (renNe w n)
def renNf {Γ Δ : List Ty} {A : Ty} (w : Wk Δ Γ) : Nf Γ A → Nf Δ A
  | .ne n => .ne (renNe w n)
  | .lam m => .lam (renNf (.keep w) m)
  | .pair a b => .pair (renNf w a) (renNf w b)
end

/-! ## The Kripke model -/

def Sem : Ty → List Ty → Type
  | .base n, Γ => Nf Γ (.base n)
  | .arr A B, Γ => (Δ : List Ty) → Wk Δ Γ → Sem A Δ → Sem B Δ
  | .prod A B, Γ => Sem A Γ × Sem B Γ

def wkSem : (A : Ty) → {Γ Δ : List Ty} → Wk Δ Γ → Sem A Γ → Sem A Δ
  | .base _, _, _, w, x => renNf w x
  | .arr _ _, _, _, w, f => fun Θ w' a => f Θ (Wk.comp w' w) a
  | .prod A B, _, _, w, p => (wkSem A w p.1, wkSem B w p.2)

/-- reflection and reification, together by recursion on the formula -/
def rr : (A : Ty) → ((Γ : List Ty) → Ne Γ A → Sem A Γ) × ((Γ : List Ty) → Sem A Γ → Nf Γ A)
  | .base _ => (fun _ n => .ne n, fun _ x => x)
  | .arr A B =>
    (fun _ n Δ w a => (rr B).1 Δ (.app (renNe w n) ((rr A).2 Δ a)),
     fun Γ f => .lam ((rr B).2 (A :: Γ) (f (A :: Γ) (.skip (Wk.id Γ)) ((rr A).1 (A :: Γ) (.var .z)))))
  | .prod A B =>
    (fun Γ n => ((rr A).1 Γ (.fst n), (rr B).1 Γ (.snd n)),
     fun Γ p => .pair ((rr A).2 Γ p.1) ((rr B).2 Γ p.2))

def reflect (A : Ty) {Γ : List Ty} (n : Ne Γ A) : Sem A Γ := (rr A).1 Γ n
def reify (A : Ty) {Γ : List Ty} (x : Sem A Γ) : Nf Γ A := (rr A).2 Γ x

def Env (Δ Γ : List Ty) : Type := (A : Ty) → Var Γ A → Sem A Δ

def Env.ext {Δ Γ : List Ty} {A : Ty} (ρ : Env Δ Γ) (a : Sem A Δ) : Env Δ (A :: Γ) :=
  fun _ v => match v with
    | .z => a
    | .s v => ρ _ v

def Env.wk {Δ Θ Γ : List Ty} (w : Wk Θ Δ) (ρ : Env Δ Γ) : Env Θ Γ := fun B v => wkSem B w (ρ B v)

def eval {Γ : List Ty} {A : Ty} : Tm Γ A → {Δ : List Ty} → Env Δ Γ → Sem A Δ
  | .var v, _, ρ => ρ _ v
  | .lam t, _, ρ => fun _ w a => eval t ((ρ.wk w).ext a)
  | .app t u, Δ, ρ => (eval t ρ) Δ (Wk.id Δ) (eval u ρ)
  | .pair t u, _, ρ => (eval t ρ, eval u ρ)
  | .fst t, _, ρ => (eval t ρ).1
  | .snd t, _, ρ => (eval t ρ).2

/-- normalization: every derivation has a normal derivation of the same formula -/
def nf {Γ : List Ty} {A : Ty} (t : Tm Γ A) : Nf Γ A :=
  reify A (eval t (fun B v => reflect B (.var v)))

/-! ## Consequences -/

theorem ne_empty : {Γ : List Ty} → {A : Ty} → Ne Γ A → Γ = [] → False
  | _, _, .var v, e => by cases e; cases v
  | _, _, .app n _, e => ne_empty n e
  | _, _, .fst n, e => ne_empty n e
  | _, _, .snd n, e => ne_empty n e

theorem no_closed_ne {A : Ty} (n : Ne [] A) : False := ne_empty n rfl

/-- consistency: no closed derivation of an atom -/
theorem no_closed_atom (n : Nat) (t : Tm [] (.base n)) : False :=
  match nf t with
  | .ne m => no_closed_ne m

/-- closed normal derivations of an implication end with →I -/
theorem arr_nf {Γ : List Ty} {A B : Ty} (x : Nf Γ (.arr A B)) : ∃ m : Nf (A :: Γ) B, x = .lam m :=
  match x with
  | .lam m => ⟨m, rfl⟩

theorem closed_arr_intro {A B : Ty} (t : Tm [] (.arr A B)) : ∃ m : Nf [A] B, nf t = .lam m :=
  arr_nf (nf t)

end ND

end Found

#print axioms Found.ND.Wk.comp
#print axioms Found.ND.renNe
#print axioms Found.ND.wkSem
#print axioms Found.ND.rr
#print axioms Found.ND.eval
#print axioms Found.ND.nf
#print axioms Found.ND.ne_empty
#print axioms Found.ND.no_closed_ne
#print axioms Found.ND.no_closed_atom
#print axioms Found.ND.closed_arr_intro

found/NF.lean

lean_all/found/NF.lean

/-
  T106 (step 1). found/NF — Quine's NF and NFU: language and stratified
  comprehension, stated for an arbitrary structure (M, ∈) — a free parameter.

  * Formulas of set theory (∈, =, ¬, ∧, ∀ over named variables), Tarski
    satisfaction in any (M, ∈).
  * Stratification: a type assignment t with t(x)+1 = t(y) for x ∈ y and
    t(x) = t(y) for x = y.
  * NF-structure: extensionality + stratified comprehension; NFU-structure:
    atoms allowed (extensionality for sets only).
  * Consequences in every such structure: a universal set V with V ∈ V,
    complements; x ∉ x is not stratified, and comprehension for it is
    contradictory; every NF-structure is an NFU-structure.
  Models (steps 2–3) are recorded in OPEN_LEMMAS (routes tried).

  Lean 4 core only, hand-written proofs, no axioms.
-/

namespace Found

namespace NF

universe u

inductive Fm where
  | mem : Nat → Nat → Fm
  | eq : Nat → Nat → Fm
  | neg : Fm → Fm
  | and : Fm → Fm → Fm
  | all : Nat → Fm → Fm

def Strat (t : Nat → Nat) : Fm → Prop
  | .mem i j => t j = t i + 1
  | .eq i j => t i = t j
  | .neg a => Strat t a
  | .and a b => Strat t a ∧ Strat t b
  | .all _ a => Strat t a

def Stratified (φ : Fm) : Prop := ∃ t, Strat t φ

section Sem
variable {M : Type u} (mem : M → M → Prop)

def upd (ρ : Nat → M) (n : Nat) (m : M) : Nat → M := fun k => if k = n then m else ρ k

def sat : (Nat → M) → Fm → Prop
  | ρ, .mem i j => mem (ρ i) (ρ j)
  | ρ, .eq i j => ρ i = ρ j
  | ρ, .neg a => ¬ sat ρ a
  | ρ, .and a b => sat ρ a ∧ sat ρ b
  | ρ, .all n a => ∀ m, sat (upd ρ n m) a

/-- comprehension for φ with element variable 0 -/
def Comp (φ : Fm) : Prop := ∀ ρ : Nat → M, ∃ y : M, ∀ x : M, mem x y ↔ sat mem (upd ρ 0 x) φ

end Sem

structure NFStr where
  M : Type u
  mem : M → M → Prop
  ext : ∀ a b, (∀ x, mem x a ↔ mem x b) → a = b
  comp : ∀ φ, Stratified φ → Comp mem φ

structure NFUStr where
  M : Type u
  mem : M → M → Prop
  isSet : M → Prop
  atoms_empty : ∀ a x, ¬ isSet a → ¬ mem x a
  ext : ∀ a b, isSet a → isSet b → (∀ x, mem x a ↔ mem x b) → a = b
  comp : ∀ φ, Stratified φ → ∀ ρ : Nat → M, ∃ y, isSet y ∧ ∀ x, mem x y ↔ sat mem (upd ρ 0 x) φ

/-! ## consequences -/

theorem upd_same {M : Type u} (ρ : Nat → M) (m : M) : upd ρ 0 m 0 = m := rfl

theorem upd_one {M : Type u} (ρ : Nat → M) (m : M) : upd ρ 0 m 1 = ρ 1 := rfl

/-- the universal set exists and contains itself -/
theorem univ (N : NFStr.{u}) (m : N.M) : ∃ V : N.M, (∀ x, N.mem x V) ∧ N.mem V V :=
  match N.comp (.eq 0 0) ⟨fun _ => 0, rfl⟩ (fun _ => m) with
  | ⟨V, h⟩ => ⟨V, fun x => (h x).mpr rfl, (h V).mpr rfl⟩

/-- complements exist -/
theorem compl (N : NFStr.{u}) (a : N.M) : ∃ c : N.M, ∀ x, N.mem x c ↔ ¬ N.mem x a :=
  match N.comp (.neg (.mem 0 1)) ⟨fun k => match k with | 0 => 0 | _ => 1, rfl⟩ (fun _ => a) with
  | ⟨c, h⟩ => ⟨c, fun x => h x⟩

theorem succ_ne_self : (n : Nat) → n + 1 ≠ n
  | 0 => fun e => Nat.noConfusion e
  | n + 1 => fun e => succ_ne_self n (Nat.succ.inj e)

/-- Russell's formula x ∉ x is not stratified -/
theorem russell_unstratified : ¬ Stratified (.neg (.mem 0 0)) :=
  fun ⟨t, h⟩ => succ_ne_self (t 0) h.symm

/-- comprehension for it is contradictory in any structure -/
theorem russell_comp_false {M : Type u} (mem : M → M → Prop) (m : M)
    (h : Comp mem (.neg (.mem 0 0))) : False :=
  match h (fun _ => m) with
  | ⟨y, hy⟩ =>
    have k : mem y y ↔ ¬ mem y y := hy y
    have n : ¬ mem y y := fun p => k.mp p p
    n (k.mpr n)

/-- every NF-structure is an NFU-structure (all objects are sets) -/
def toNFU (N : NFStr.{u}) : NFUStr.{u} where
  M := N.M
  mem := N.mem
  isSet _ := True
  atoms_empty _ _ h := (h trivial).elim
  ext a b _ _ h := N.ext a b h
  comp φ hφ ρ := match N.comp φ hφ ρ with
    | ⟨y, h⟩ => ⟨y, trivial, h⟩

/-- NFU also has a universal set -/
theorem nfu_univ (N : NFUStr.{u}) (m : N.M) : ∃ V : N.M, N.isSet V ∧ ∀ x, N.mem x V :=
  match N.comp (.eq 0 0) ⟨fun _ => 0, rfl⟩ (fun _ => m) with
  | ⟨V, s, h⟩ => ⟨V, s, fun x => (h x).mpr rfl⟩

end NF

end Found

#print axioms Found.NF.univ
#print axioms Found.NF.compl
#print axioms Found.NF.succ_ne_self
#print axioms Found.NF.russell_unstratified
#print axioms Found.NF.russell_comp_false
#print axioms Found.NF.toNFU
#print axioms Found.NF.nfu_univ

found/NFAlt.lean

lean_all/found/NFAlt.lean

/-
  T106 (b), slice N4c(iv). found/NFAlt — in the classical point, codes and
  alternative extensions of the tangled-type construction (con-nf):
  * near-litters as data, their litter N° (the unique litter they are near),
    and the action of base permutations: (π N)° = π̂ (N°);
  * typed near-litters: lower data at γ embeds near-litters as γ-tangles,
    equivariantly;
  * a code at α is (β < α, a set B of β-tangles); allowable permutations act
    on codes; the alternative γ-extension (γ ≠ β) is the union of the local
    cardinals of the litters fuzz b, b ∈ B:
        A_γ(β, B) = { typed N | N° = fuzz b for some b ∈ B };
  * equivariance: for allowable π, A_γ(π • c) = π • A_γ(c) (pointwise ↔),
    from the exact coherence of f-maps (found/NFLitter).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.NFLitter

namespace Found

namespace NFA

open CP WO Card NFP NFPO NFB NFS NFL

variable (K : Classical') {P : ParamsO}

/-- near-litters as data -/
abbrev NLit (P : ParamsO) : Type := {S : Atom P → Prop // NearLitter P S}

def nlAct (π : BPerm P) (N : NLit P) : NLit P := ⟨img P π.g N.val, π.near _ N.property⟩

/-- the litter a near-litter is near -/
def litterOf (N : NLit P) : P.μ := K.ch (fun i => NearTo P N.val i) N.property

theorem litterOf_spec (N : NLit P) : NearTo P N.val (litterOf K N) := K.ch_spec (fun i => NearTo P N.val i) N.property

theorem litterOf_act (π : BPerm P) (N : NLit P) : litterOf K (nlAct π N) = lmap K π (litterOf K N) :=
  near_unique K _ _ _ (litterOf_spec K _)
    (trans_small K P _ _ _ (img_small P π _ _ (litterOf_spec K N)) (lmap_spec K π _))

/-- typed near-litters at a lower level γ -/
structure Typed {γ : P.Λ} (Lg : Lower P γ) where
  typed : NLit P → Lg.T
  typed_act : ∀ ρ, Lg.Allow ρ → ∀ N, Lg.eqv (Lg.act ρ (typed N)) (typed (nlAct (ρ (.bot γ)) N))

theorem act_inv_l {β : P.Λ} (Lb : Lower P β) (ρ : StrPerm P β) (hρ : Lb.Allow ρ) (t : Lb.T) :
    Lb.eqv (Lb.act (SInv P ρ) (Lb.act ρ t)) t :=
  have hi := Lb.allow_inv _ hρ
  Lb.eqv_trans (Lb.eqv_symm (Lb.act_mul _ _ hρ hi t))
    (Lb.eqv_trans (Lb.act_ext _ (SOne P β) (Lb.allow_mul _ _ hρ hi) Lb.allow_one (smul_inv P ρ) t) (Lb.act_one t))

theorem act_inv_r {β : P.Λ} (Lb : Lower P β) (ρ : StrPerm P β) (hρ : Lb.Allow ρ) (t : Lb.T) :
    Lb.eqv (Lb.act ρ (Lb.act (SInv P ρ) t)) t :=
  have hi := Lb.allow_inv _ hρ
  Lb.eqv_trans (Lb.eqv_symm (Lb.act_mul _ _ hi hρ t))
    (Lb.eqv_trans (Lb.act_ext _ (SOne P β) (Lb.allow_mul _ _ hi hρ) Lb.allow_one (sinv_mul P ρ) t) (Lb.act_one t))

variable {α : P.Λ} (L : ∀ β, P.ltΛ β α → Lower P β)

/-- a set of tangles respecting their equivalence -/
def RespS {β : P.Λ} (Lb : Lower P β) (B : Lb.T → Prop) : Prop := ∀ t t', Lb.eqv t t' → B t → B t'

/-- a code: a lower level and a set of its tangles -/
structure Code where
  β : P.Λ
  hβ : P.ltΛ β α
  B : (L β hβ).T → Prop
  resp : RespS (L β hβ) B

/-- the action on sets of γ-tangles -/
def setAct (γ : P.Λ) (hγ : P.ltΛ γ α) (π : StrPerm P α) (S : (L γ hγ).T → Prop) : (L γ hγ).T → Prop :=
  fun u => S ((L γ hγ).act (SInv P (deriv P π hγ)) u)

theorem setAct_resp (γ : P.Λ) (hγ : P.ltΛ γ α) (π : StrPerm P α) (hπ : Allowable L π) (S : (L γ hγ).T → Prop)
    (hS : RespS (L γ hγ) S) : RespS (L γ hγ) (setAct L γ hγ π S) :=
  fun _ _ e h => hS _ _ ((L γ hγ).act_resp _ ((L γ hγ).allow_inv _ (hπ.1 γ hγ)) e) h

def codeAct (π : StrPerm P α) (hπ : Allowable L π) (c : Code L) : Code L :=
  ⟨c.β, c.hβ, setAct L c.β c.hβ π c.B, setAct_resp L c.β c.hβ π hπ c.B c.resp⟩

/-- the alternative γ-extension of a code -/
def alt (γ : P.Λ) (hγ : P.ltΛ γ α) (Ty : Typed (L γ hγ)) (c : Code L) : (L γ hγ).T → Prop :=
  fun u => ∃ N b, c.B b ∧ litterOf K N = (L c.β c.hβ).fuzz b ∧ (L γ hγ).eqv (Ty.typed N) u

theorem alt_resp (γ : P.Λ) (hγ : P.ltΛ γ α) (Ty : Typed (L γ hγ)) (c : Code L) :
    RespS (L γ hγ) (alt K L γ hγ Ty c) :=
  fun _ _ e ⟨N, b, hb, hl, hu⟩ => ⟨N, b, hb, hl, (L γ hγ).eqv_trans hu e⟩

/-- equivariance of alternative extensions -/
theorem alt_equivariant (γ : P.Λ) (hγ : P.ltΛ γ α) (Ty : Typed (L γ hγ)) (π : StrPerm P α)
    (hπ : Allowable L π) (c : Code L) (ne : c.β ≠ γ) (u : (L γ hγ).T) :
    alt K L γ hγ Ty (codeAct L π hπ c) u ↔ setAct L γ hγ π (alt K L γ hγ Ty c) u := by
  let Lb := L c.β c.hβ
  let Lg := L γ hγ
  have hg := hπ.1 γ hγ
  have hgi := Lg.allow_inv _ hg
  constructor
  · intro ⟨N, b, hb, hl, hu⟩
    refine ⟨nlAct (BPerm.inv P (atLit π hγ)) N, Lb.act (SInv P (deriv P π c.hβ)) b, hb, ?_, ?_⟩
    · rw [litterOf_act, hl]
      exact coherence_exact K L (SInv P π) (allowable_inv L π hπ) c.β c.hβ γ hγ ne b
    · exact Lg.eqv_trans (Lg.eqv_symm (Ty.typed_act _ hgi N)) (Lg.act_resp _ hgi hu)
  · intro ⟨N, b, hb, hl, hu⟩
    refine ⟨nlAct (atLit π hγ) N, Lb.act (deriv P π c.hβ) b, ?_, ?_, ?_⟩
    · exact c.resp _ _ (Lb.eqv_symm (act_inv_l Lb _ (hπ.1 c.β c.hβ) b)) hb
    · rw [litterOf_act, hl]
      exact coherence_exact K L π hπ c.β c.hβ γ hγ ne b
    · exact Lg.eqv_trans (Lg.eqv_symm (Ty.typed_act _ hg N))
        (Lg.eqv_trans (Lg.act_resp _ hg hu) (act_inv_r Lg _ hg u))

end NFA

end Found

#print axioms Found.NFA.litterOf_act
#print axioms Found.NFA.act_inv_l
#print axioms Found.NFA.act_inv_r
#print axioms Found.NFA.alt_resp
#print axioms Found.NFA.setAct_resp
#print axioms Found.NFA.alt_equivariant

found/NFAssemble.lean

lean_all/found/NFAssemble.lean

/-
  T106 (b), slice N4d(vii). found/NFAssemble — in the classical point,
  freedom of action one level up (con-nf): if the lower levels' allowable
  permutations and actions do not depend on the base permutation at their
  own bottom path, and the f-maps of different levels have disjoint images
  and are injective up to equality, then every family of lower allowable
  permutations (ρ_β)_{β<α} is the family of derivatives (off the bottom
  paths) of an allowable permutation of level α: the base permutation at
  each path γ → ⊥ is the one forced on inflexible litters
  (found/NFInflex), which gives the coherence of f-maps exactly.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.NFInflex
import found.NFAlt
import found.NFStruct

namespace Found

namespace NFAs

open CP WO Card NFP NFPO NFB NFS NFL NFI

variable (K : Classical') {P : ParamsO}

/-- two structural permutations agree off the bottom path -/
def OffBot {β : P.Λ} (σ σ' : StrPerm P β) : Prop := ∀ δ (h : P.ltΛ δ β) A a, (σ (.cons δ h A)).f a = (σ' (.cons δ h A)).f a

/-- lower data not looking at its own bottom path -/
structure BotIndep {β : P.Λ} (Lb : Lower P β) : Prop where
  allow : ∀ σ σ', OffBot σ σ' → Lb.Allow σ → Lb.Allow σ'
  act : ∀ σ σ', OffBot σ σ' → Lb.Allow σ → ∀ t, Lb.eqv (Lb.act σ t) (Lb.act σ' t)

variable {α : P.Λ} (L : ∀ β, P.ltΛ β α → Lower P β)

/-- the assembled structural permutation -/
def asm (base : ∀ γ, P.ltΛ γ α → BPerm P) (ρ : ∀ β (_ : P.ltΛ β α), StrPerm P β) : StrPerm P α
  | .bot _ => BPerm.one P
  | .cons γ h (.bot _) => base γ h
  | .cons γ h (.cons δ h' A) => ρ γ h (.cons δ h' A)

theorem asm_off (base : ∀ γ, P.ltΛ γ α → BPerm P) (ρ : ∀ β (_ : P.ltΛ β α), StrPerm P β)
    (β : P.Λ) (h : P.ltΛ β α) : OffBot (deriv P (asm base ρ) h) (ρ β h) := fun _ _ _ _ => rfl

include K in
/-- freedom of action at level α -/
theorem assemble (bi : ∀ β h, BotIndep (L β h))
    (fz_inj : ∀ β h t t', (L β h).fuzz t = (L β h).fuzz t' → (L β h).eqv t t')
    (fz_disj : ∀ β h β' h' t t', (L β h).fuzz t = (L β' h').fuzz t' → β = β')
    (ρ : ∀ β (_ : P.ltΛ β α), StrPerm P β) (hρ : ∀ β h, (L β h).Allow (ρ β h)) :
    ∃ π, Allowable L π ∧ ∀ β h, OffBot (deriv P π h) (ρ β h) := by
  let I := {β : P.Λ // P.ltΛ β α}
  let T : I → Type := fun i => (L i.1 i.2).T
  have ex := free_inflex K T (fun i => (L i.1 i.2).eqv) (fun i => (L i.1 i.2).fuzz)
    (fun i _ _ e => (L i.1 i.2).fuzz_resp e) (fun i => fz_inj i.1 i.2)
    (fun i j t t' e => Subtype.ext (fz_disj i.1 i.2 j.1 j.2 t t' e))
    (fun i => (L i.1 i.2).act (ρ i.1 i.2)) (fun i => (L i.1 i.2).act (SInv P (ρ i.1 i.2)))
    (fun i _ _ e => (L i.1 i.2).act_resp _ (hρ i.1 i.2) e)
    (fun i _ _ e => (L i.1 i.2).act_resp _ ((L i.1 i.2).allow_inv _ (hρ i.1 i.2)) e)
    (fun i t => NFA.act_inv_r (L i.1 i.2) _ (hρ i.1 i.2) t)
    (fun i t => NFA.act_inv_l (L i.1 i.2) _ (hρ i.1 i.2) t)
  match ex with
  | ⟨b, hb⟩ =>
    refine ⟨asm (fun _ _ => b) ρ, ⟨fun β h => (bi β h).allow (ρ β h) _ (fun _ _ _ _ => rfl) (hρ β h),
      fun β hβ γ hγ _ t => ?_⟩, asm_off (fun _ _ => b) ρ⟩
    have e1 : (L β hβ).fuzz ((L β hβ).act (deriv P (asm (fun _ _ => b) ρ) hβ) t) = (L β hβ).fuzz ((L β hβ).act (ρ β hβ) t) :=
      (L β hβ).fuzz_resp ((L β hβ).eqv_symm ((bi β hβ).act (ρ β hβ) (deriv P (asm (fun _ _ => b) ρ) hβ) (fun _ _ _ _ => rfl) (hρ β hβ) t))
    have e2 : lmap K b ((L β hβ).fuzz t) = (L β hβ).fuzz ((L β hβ).act (ρ β hβ) t) := hb ⟨β, hβ⟩ t
    show NearTo P (img P b.g (litter P ((L β hβ).fuzz t))) _
    rw [e1, ← e2]
    exact lmap_spec K b _

end NFAs

end Found

#print axioms Found.NFAs.asm_off
#print axioms Found.NFAs.assemble

found/NFBase.lean

lean_all/found/NFBase.lean

/-
  T106 (b), slice N4a. found/NFBase — the base type of the tangled-type
  construction (con-nf), over a parameter value P (found/NFParamsO), in the
  classical point:
  * atoms = μ × κ, the litter L_i = {(i, k) | k : κ};
  * small = strictly smaller than κ; small sets are closed under binary
    unions (κ regular and linearly well-ordered);
  * near-litters: sets whose symmetric difference with some litter is small;
  * base permutations: bijections of atoms (inverse given) mapping
    near-litters to near-litters both ways; they form a group (identity,
    composition, inverse), with the laws holding pointwise.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.NFParamsO

namespace Found

namespace NFB

open CP WO Card NFP NFPO

variable (K : Classical') (P : ParamsO)

theorem irrefl_wf {α : Type} {r : α → α → Prop} (w : WellFounded r) (x : α) : ¬ r x x := fun h => by
  have := w.apply x
  induction this with
  | intro y _ ih => exact ih y h h

theorem asymm_wf {α : Type} {r : α → α → Prop} (w : WellFounded r) (x y : α) (h : r x y) : ¬ r y x := fun h' => by
  have := w.apply x
  induction this generalizing y with
  | intro z _ ih => exact ih y h' z h' h

def Small (A : Type) : Prop := Smaller A P.κ

include K in
/-- the union of two small sets is small -/
theorem small_sum (A B : Type) (hA : Small P A) (hB : Small P B) : Small P (A ⊕ B) := by
  have no : ¬ ∃ g : P.κ → A ⊕ B, Inj g := fun ⟨g, hg⟩ => by
    let SA : P.κ → Prop := fun x => ∃ a, g x = .inl a
    let SB : P.κ → Prop := fun x => ∃ b, g x = .inr b
    have sA : Smaller {x // SA x} P.κ := ⟨⟨fun x => x.val, fun x y e => Subtype.ext e⟩, fun ⟨f, hf⟩ =>
      hA.2 ⟨fun k => K.ch (fun a => g (f k).val = .inl a) (f k).property, fun k l e => hf _ _ (Subtype.ext (hg _ _ (by
        have h1 := K.ch_spec (fun a => g (f k).val = .inl a) (f k).property
        have h2 := K.ch_spec (fun a => g (f l).val = .inl a) (f l).property
        have e' : K.ch (fun a => g (f k).val = .inl a) (f k).property = K.ch (fun a => g (f l).val = .inl a) (f l).property := e
        rw [e'] at h1
        exact h1.trans h2.symm)))⟩⟩
    have sB : Smaller {x // SB x} P.κ := ⟨⟨fun x => x.val, fun x y e => Subtype.ext e⟩, fun ⟨f, hf⟩ =>
      hB.2 ⟨fun k => K.ch (fun b => g (f k).val = .inr b) (f k).property, fun k l e => hf _ _ (Subtype.ext (hg _ _ (by
        have h1 := K.ch_spec (fun b => g (f k).val = .inr b) (f k).property
        have h2 := K.ch_spec (fun b => g (f l).val = .inr b) (f l).property
        have e' : K.ch (fun b => g (f k).val = .inr b) (f k).property = K.ch (fun b => g (f l).val = .inr b) (f l).property := e
        rw [e'] at h1
        exact h1.trans h2.symm)))⟩⟩
    obtain ⟨bA, hbA⟩ := P.κ_regular SA sA
    obtain ⟨bB, hbB⟩ := P.κ_regular SB sB
    have side : ∀ x, P.ltκ x bA ∨ P.ltκ x bB := fun x => by
      cases hgx : g x with
      | inl a => exact Or.inl (hbA x ⟨a, hgx⟩)
      | inr b => exact Or.inr (hbB x ⟨b, hgx⟩)
    rcases P.linκ bA bB with h | h | h
    · exact (side bB).elim (fun h' => asymm_wf P.wfκ _ _ h h') (fun h' => irrefl_wf P.wfκ _ h')
    · subst h
      exact (side bA).elim (fun h' => irrefl_wf P.wfκ _ h') (fun h' => irrefl_wf P.wfκ _ h')
    · exact (side bA).elim (fun h' => irrefl_wf P.wfκ _ h') (fun h' => asymm_wf P.wfκ _ _ h h')
  exact (comparable K (α := A ⊕ B) (β := P.κ)).elim (fun h => ⟨h, no⟩) (fun h => (no h).elim)

/-! ## atoms, litters, near-litters -/

abbrev Atom : Type := P.μ × P.κ

def litter (i : P.μ) (a : Atom P) : Prop := a.1 = i

/-- the symmetric difference of two sets of atoms -/
def SymDiff (S T : Atom P → Prop) : Type := {a : Atom P // (S a ∧ ¬ T a) ∨ (T a ∧ ¬ S a)}

def NearLitter (S : Atom P → Prop) : Prop := ∃ i, Small P (SymDiff P S (litter P i))

include K in
theorem kappa_ne : ∃ _ : P.κ, True :=
  dne K _ (fun hn => P.Λ_small.2 ⟨fun x => (hn ⟨x, trivial⟩).elim, fun x _ _ => (hn ⟨x, trivial⟩).elim⟩)

include K in
/-- the empty type is small -/
theorem small_empty (E : Type) (hE : E → False) : Small P E :=
  ⟨⟨fun e => (hE e).elim, fun e _ _ => (hE e).elim⟩, fun ⟨g, _⟩ => match kappa_ne K P with
    | ⟨k, _⟩ => hE (g k)⟩

theorem symm_self_empty (S : Atom P → Prop) (a : SymDiff P S S) : False :=
  a.property.elim (fun h => h.2 h.1) (fun h => h.2 h.1)

include K in
theorem litter_near (i : P.μ) : NearLitter P (litter P i) :=
  ⟨i, small_empty K P _ (symm_self_empty P _)⟩

/-! ## base permutations -/

/-- the image of a set under a bijection with inverse g -/
def img (g : Atom P → Atom P) (S : Atom P → Prop) : Atom P → Prop := fun a => S (g a)

structure BPerm where
  f : Atom P → Atom P
  g : Atom P → Atom P
  fg : ∀ a, f (g a) = a
  gf : ∀ a, g (f a) = a
  near : ∀ S, NearLitter P S → NearLitter P (img P g S)
  near_inv : ∀ S, NearLitter P S → NearLitter P (img P f S)

def BPerm.one : BPerm P := ⟨id, id, fun _ => rfl, fun _ => rfl, fun _ h => h, fun _ h => h⟩

def BPerm.mul (π ρ : BPerm P) : BPerm P where
  f a := ρ.f (π.f a)
  g a := π.g (ρ.g a)
  fg a := by
    show ρ.f (π.f (π.g (ρ.g a))) = a
    rw [π.fg, ρ.fg]
  gf a := by
    show π.g (ρ.g (ρ.f (π.f a))) = a
    rw [ρ.gf, π.gf]
  near S h := ρ.near _ (π.near S h)
  near_inv S h := π.near_inv _ (ρ.near_inv S h)

def BPerm.inv (π : BPerm P) : BPerm P := ⟨π.g, π.f, π.gf, π.fg, π.near_inv, π.near⟩

/-- pointwise equality of base permutations -/
def BEq (π ρ : BPerm P) : Prop := ∀ a, π.f a = ρ.f a

theorem mul_assoc (π ρ σ : BPerm P) : BEq P (BPerm.mul P (BPerm.mul P π ρ) σ) (BPerm.mul P π (BPerm.mul P ρ σ)) :=
  fun _ => rfl
theorem one_mul (π : BPerm P) : BEq P (BPerm.mul P (BPerm.one P) π) π := fun _ => rfl
theorem mul_one (π : BPerm P) : BEq P (BPerm.mul P π (BPerm.one P)) π := fun _ => rfl
theorem mul_inv (π : BPerm P) : BEq P (BPerm.mul P π (BPerm.inv P π)) (BPerm.one P) := fun a => π.gf a
theorem inv_mul (π : BPerm P) : BEq P (BPerm.mul P (BPerm.inv P π) π) (BPerm.one P) := fun a => π.fg a

/-- base permutations act on near-litters -/
theorem act_near (π : BPerm P) (S : Atom P → Prop) (h : NearLitter P S) : NearLitter P (img P π.g S) :=
  π.near S h

theorem img_mem (π : BPerm P) (S : Atom P → Prop) (a : Atom P) : img P π.g S (π.f a) ↔ S a := by
  show S (π.g (π.f a)) ↔ S a
  rw [π.gf]

end NFB

end Found

#print axioms Found.NFB.small_sum
#print axioms Found.NFB.kappa_ne
#print axioms Found.NFB.small_empty
#print axioms Found.NFB.litter_near
#print axioms Found.NFB.BPerm.mul
#print axioms Found.NFB.mul_assoc
#print axioms Found.NFB.mul_inv
#print axioms Found.NFB.inv_mul
#print axioms Found.NFB.act_near
#print axioms Found.NFB.img_mem

found/NFCode.lean

lean_all/found/NFCode.lean

/-
  T106 (b), slice N4c(ii). found/NFCode — in the classical point, over a
  parameter value P with a copy of ℕ in μ: codes in μ for the data of the
  tangled-type construction:
  * small subsets of any type that injects into μ are coded into μ
    (injective up to pointwise equivalence);
  * finite lists over μ inject into μ (length and nested pairs);
  * paths α → ⊥ inject into lists over Λ, hence into μ;
  * addresses (path, atom) inject into μ, so supports (small sets of
    addresses) are coded into μ: there are at most μ supports.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.NFCount

namespace Found

namespace NFD

open CP WO Card NFP NFPO NFB NFS NFC

variable (K : Classical') (P : ParamsO) {e : Nat → P.μ} (he : Inj e)

/-! ## small subsets of a type injecting into μ -/

def imgG {A : Type} (i : A → P.μ) (B : SSet P A) : SSet P P.μ where
  S y := ∃ a, i a = y ∧ B.S a
  small := small_of_inj P (fun y => (⟨K.ch (fun a => i a = y.val ∧ B.S a) y.property,
      (K.ch_spec (fun a => i a = y.val ∧ B.S a) y.property).2⟩ : {a // B.S a}))
    (fun y z eq => Subtype.ext (by
      have hy := (K.ch_spec (fun a => i a = y.val ∧ B.S a) y.property).1
      have hz := (K.ch_spec (fun a => i a = z.val ∧ B.S a) z.property).1
      have e' := congrArg Subtype.val eq
      have e'' : K.ch (fun a => i a = y.val ∧ B.S a) y.property =
          K.ch (fun a => i a = z.val ∧ B.S a) z.property := e'
      rw [e''] at hy
      exact hy.symm.trans hz)) B.small

def codeG {A : Type} (i : A → P.μ) (B : SSet P A) : P.μ := code K P he (imgG K P i B)

theorem codeG_inj {A : Type} (i : A → P.μ) (hi : Inj i) (B C : SSet P A) (h : codeG K P he i B = codeG K P he i C) :
    ∀ a, B.S a ↔ C.S a := by
  have c := code_inj K P he _ _ h
  intro a
  constructor
  · intro ha
    match (c (i a)).1 ⟨a, rfl, ha⟩ with
    | ⟨b, hb, hb'⟩ => exact (hi _ _ hb) ▸ hb'
  · intro ha
    match (c (i a)).2 ⟨a, rfl, ha⟩ with
    | ⟨b, hb, hb'⟩ => exact (hi _ _ hb) ▸ hb'

/-! ## lists over μ -/

def fold : List P.μ → P.μ
  | [] => e 0
  | x :: l => pair K P he (x, fold l)

def lenL {A : Type} : List A → Nat
  | [] => 0
  | _ :: l => lenL l + 1

def codeL (l : List P.μ) : P.μ := pair K P he (e (lenL l), fold K P he l)

theorem fold_inj : (n : Nat) → (l m : List P.μ) → lenL l = n → lenL m = n →
    fold K P he l = fold K P he m → l = m
  | 0, [], [], _, _, _ => rfl
  | 0, _ :: _, _, h, _, _ => Nat.noConfusion h
  | 0, [], _ :: _, _, h, _ => Nat.noConfusion h
  | _ + 1, [], _, h, _, _ => Nat.noConfusion h
  | _ + 1, _ :: _, [], _, h, _ => Nat.noConfusion h
  | n + 1, x :: l, y :: m, hl, hm, h => by
    have hp : (x, fold K P he l) = (y, fold K P he m) := pair_inj K P he _ _ h
    have h1' := congrArg Prod.fst hp
    have h2' := congrArg Prod.snd hp
    have hx : x = y := h1'
    have hr : fold K P he l = fold K P he m := h2'
    rw [hx, fold_inj n l m (Nat.succ.inj hl) (Nat.succ.inj hm) hr]

theorem codeL_inj : Inj (codeL K P he) := fun l m h => by
  have hp : (e (lenL l), fold K P he l) = (e (lenL m), fold K P he m) := pair_inj K P he _ _ h
  have h1' := congrArg Prod.fst hp
  have h2' := congrArg Prod.snd hp
  have hn : lenL l = lenL m := he _ _ h1'
  exact fold_inj K P he (lenL m) l m hn rfl h2'

/-! ## paths and addresses -/

def isNil {A : Type} : List A → Bool
  | [] => true
  | _ :: _ => false


def toList {α : P.Λ} : Path P α → List P.Λ
  | .bot _ => []
  | .cons β _ p => β :: toList p

theorem toList_inj : {α : P.Λ} → (p q : Path P α) → toList P p = toList P q → p = q
  | _, .bot _, .bot _, _ => rfl
  | _, .bot _, .cons _ _ _, h => Bool.noConfusion (congrArg isNil h)
  | _, .cons _ _ _, .bot _, h => Bool.noConfusion (congrArg isNil h)
  | _, .cons β h1 p, .cons β' h2 q, h => by
    have hb : β = β' := List.head_eq_of_cons_eq h
    subst hb
    rw [toList_inj p q (List.tail_eq_of_cons_eq h)]

/-- Λ injects into μ (Λ < κ < μ) -/
def lamCode (a : P.Λ) : P.μ :=
  K.ch (fun f : P.κ → P.μ => Inj f) P.κ_lt_μ.1 (K.ch (fun f : P.Λ → P.κ => Inj f) P.Λ_small.1 a)

theorem lamCode_inj : Inj (lamCode K P) := fun _ _ h =>
  K.ch_spec (fun f : P.Λ → P.κ => Inj f) P.Λ_small.1 _ _ (K.ch_spec (fun f : P.κ → P.μ => Inj f) P.κ_lt_μ.1 _ _ h)

def mapL {A B : Type} (f : A → B) : List A → List B
  | [] => []
  | x :: l => f x :: mapL f l

theorem mapL_inj {A B : Type} (f : A → B) (hf : Inj f) : Inj (mapL f) := fun l m h => by
  induction l generalizing m with
  | nil => cases m with
    | nil => rfl
    | cons _ _ => exact Bool.noConfusion (congrArg isNil h)
  | cons x l ih => cases m with
    | nil => exact Bool.noConfusion (congrArg isNil h)
    | cons y m =>
      have h1 : f x = f y := List.head_eq_of_cons_eq h
      rw [hf _ _ h1, ih m (List.tail_eq_of_cons_eq h)]

def pathCode {α : P.Λ} (p : Path P α) : P.μ := codeL K P he (mapL (lamCode K P) (toList P p))

theorem pathCode_inj (α : P.Λ) : Inj (pathCode K P he (α := α)) := fun p q h =>
  toList_inj P p q (mapL_inj _ (lamCode_inj K P) _ _ (codeL_inj K P he _ _ h))

def addrCode {α : P.Λ} (s : Addr (P := P) α) : P.μ := pair K P he (pathCode K P he s.1, atomCode K P he s.2)

theorem addrCode_inj (α : P.Λ) : Inj (addrCode K P he (α := α)) := fun s t h => by
  have hp := pair_inj K P he _ _ h
  have h1' := congrArg Prod.fst hp
  have h2' := congrArg Prod.snd hp
  have e1 : s.1 = t.1 := pathCode_inj K P he α _ _ h1'
  have e2 : s.2 = t.2 := atomCode_inj K P he _ _ h2'
  match s, t, e1, e2 with
  | (_, _), (_, _), e1, e2 => exact Prod.ext e1 e2

/-- supports at level α are coded into μ: at most μ supports -/
def suppCode {α : P.Λ} (B : SSet P (Addr (P := P) α)) : P.μ := codeG K P he (addrCode K P he) B

theorem suppCode_inj (α : P.Λ) (B C : SSet P (Addr (P := P) α)) (h : suppCode K P he B = suppCode K P he C) :
    ∀ s, B.S s ↔ C.S s :=
  codeG_inj K P he _ (addrCode_inj K P he α) B C h

end NFD

end Found

#print axioms Found.NFD.codeG_inj
#print axioms Found.NFD.codeL_inj
#print axioms Found.NFD.toList_inj
#print axioms Found.NFD.lamCode_inj
#print axioms Found.NFD.pathCode_inj
#print axioms Found.NFD.addrCode_inj
#print axioms Found.NFD.suppCode_inj

found/NFCount.lean

lean_all/found/NFCount.lean

/-
  T106 (b), slice N4c(i). found/NFCount — in the classical point, over a
  parameter value P with a copy of ℕ in μ: the small subsets of μ inject
  into μ (μ^{<κ} = μ, the counting fact behind the f-maps of con-nf), with
  injectivity up to pointwise equivalence (no function extensionality):
  * each segment {x | x < m} has its power set coded into μ (strong limit);
  * a small set is bounded (cof μ ≥ κ); its code is the pair (bound, code of
    its trace on the segment below the bound), paired by Hessenberg;
  * small sets of atoms (μ × κ) are coded into μ as well.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.NFStruct
import found.Hessenberg

namespace Found

namespace NFC

open CP WO Card NFP NFPO NFB NFS

variable (K : Classical') (P : ParamsO)

/-- a small subset of a type A -/
structure SSet (A : Type) where
  S : A → Prop
  small : Small P {x // S x}

/-- the power set of each segment of μ is coded into μ, injectively up to ↔ -/
theorem seg_code (m : P.μ) :
    ∃ g : ({x // P.ltμ x m} → Prop) → P.μ, ∀ S T, g S = g T → ∀ x, S x ↔ T x :=
  match P.μ_strong m with
  | ⟨_, ⟨f, hf⟩, ⟨⟨h, hh⟩, _⟩⟩ => ⟨fun S => h (fun b => ∃ x, f x = b ∧ S x), fun S T eq x => by
      have e2 := congrFun (hh _ _ eq) (f x)
      constructor
      · intro hs
        have w : ∃ y, f y = f x ∧ T y := e2 ▸ ⟨x, rfl, hs⟩
        match w with
        | ⟨y, hy, ht⟩ => exact (hf _ _ hy) ▸ ht
      · intro ht
        have w : ∃ y, f y = f x ∧ S y := e2.symm ▸ ⟨x, rfl, ht⟩
        match w with
        | ⟨y, hy, hs⟩ => exact (hf _ _ hy) ▸ hs⟩

def SegCode (m : P.μ) (g : ({x // P.ltμ x m} → Prop) → P.μ) : Prop := ∀ S T, g S = g T → ∀ x, S x ↔ T x

def G (m : P.μ) : ({x // P.ltμ x m} → Prop) → P.μ := K.ch (SegCode P m) (seg_code P m)

theorem G_spec (m : P.μ) : SegCode P m (G K P m) := K.ch_spec (SegCode P m) (seg_code P m)

def bound (A : SSet P P.μ) : P.μ := K.ch (fun b => ∀ x, A.S x → P.ltμ x b) (P.μ_cof A.S A.small)

theorem bound_spec (A : SSet P P.μ) : ∀ x, A.S x → P.ltμ x (bound K P A) :=
  K.ch_spec (fun b => ∀ x, A.S x → P.ltμ x b) (P.μ_cof A.S A.small)

variable {e : Nat → P.μ} (he : Inj e)

def pair : P.μ × P.μ → P.μ := K.ch (fun F : P.μ × P.μ → P.μ => Inj F) (Hess.hessenberg K he)

theorem pair_inj : Inj (pair K P he) := K.ch_spec (fun F : P.μ × P.μ → P.μ => Inj F) (Hess.hessenberg K he)

/-- the code of a small subset of μ -/
def code (A : SSet P P.μ) : P.μ :=
  pair K P he (bound K P A, G K P (bound K P A) (fun x => A.S x.val))

theorem code_aux (A B : SSet P P.μ) : ∀ a b, a = b → (∀ x, A.S x → P.ltμ x a) → (∀ x, B.S x → P.ltμ x b) →
    G K P a (fun x => A.S x.val) = G K P b (fun x => B.S x.val) → ∀ x, A.S x ↔ B.S x := by
  intro a b hab ha hb eg x
  subst hab
  have sp := G_spec K P a _ _ eg
  exact ⟨fun hx => (sp ⟨x, ha x hx⟩).1 hx, fun hx => (sp ⟨x, hb x hx⟩).2 hx⟩

/-- small subsets of μ inject into μ, up to pointwise equivalence -/
theorem code_inj (A B : SSet P P.μ) (h : code K P he A = code K P he B) : ∀ x, A.S x ↔ B.S x := by
  have hp := pair_inj K P he _ _ h
  exact code_aux K P A B _ _ (congrArg Prod.fst hp) (bound_spec K P A) (bound_spec K P B) (congrArg Prod.snd hp)

/-! ## small sets of atoms -/

/-- an injection of atoms μ × κ into μ -/
def atomCode : Atom P → P.μ := fun a =>
  pair K P he (a.1, K.ch (fun f : P.κ → P.μ => Inj f) P.κ_lt_μ.1 a.2)

theorem atomCode_inj : Inj (atomCode K P he) := fun a b h => by
  have hp : (a.1, K.ch (fun f : P.κ → P.μ => Inj f) P.κ_lt_μ.1 a.2) =
      (b.1, K.ch (fun f : P.κ → P.μ => Inj f) P.κ_lt_μ.1 b.2) := pair_inj K P he _ _ h
  have h1' := congrArg Prod.fst hp
  have h1 : a.1 = b.1 := h1'
  have h2' := congrArg Prod.snd hp
  have h2 : a.2 = b.2 := K.ch_spec (fun f : P.κ → P.μ => Inj f) P.κ_lt_μ.1 _ _ h2'
  match a, b, h1, h2 with
  | (_, _), (_, _), h1, h2 => exact Prod.ext h1 h2

/-- the image of a small set of atoms is a small subset of μ -/
def imgSet (A : SSet P (Atom P)) : SSet P P.μ where
  S y := ∃ a, atomCode K P he a = y ∧ A.S a
  small := small_of_inj P (fun y => (⟨K.ch (fun a => atomCode K P he a = y.val ∧ A.S a) y.property,
      (K.ch_spec (fun a => atomCode K P he a = y.val ∧ A.S a) y.property).2⟩ : {a // A.S a}))
    (fun y z e => Subtype.ext (by
      have hy := (K.ch_spec (fun a => atomCode K P he a = y.val ∧ A.S a) y.property).1
      have hz := (K.ch_spec (fun a => atomCode K P he a = z.val ∧ A.S a) z.property).1
      have e' := congrArg Subtype.val e
      have e'' : K.ch (fun a => atomCode K P he a = y.val ∧ A.S a) y.property =
          K.ch (fun a => atomCode K P he a = z.val ∧ A.S a) z.property := e'
      rw [e''] at hy
      exact hy.symm.trans hz)) A.small

def codeA (A : SSet P (Atom P)) : P.μ := code K P he (imgSet K P he A)

/-- small sets of atoms inject into μ, up to pointwise equivalence -/
theorem codeA_inj (A B : SSet P (Atom P)) (h : codeA K P he A = codeA K P he B) : ∀ a, A.S a ↔ B.S a := by
  have c := code_inj K P he _ _ h
  intro a
  constructor
  · intro ha
    match (c (atomCode K P he a)).1 ⟨a, rfl, ha⟩ with
    | ⟨b, hb, hb'⟩ => exact (atomCode_inj K P he _ _ hb) ▸ hb'
  · intro ha
    match (c (atomCode K P he a)).2 ⟨a, rfl, ha⟩ with
    | ⟨b, hb, hb'⟩ => exact (atomCode_inj K P he _ _ hb) ▸ hb'

end NFC

end Found

#print axioms Found.NFC.seg_code
#print axioms Found.NFC.G_spec
#print axioms Found.NFC.bound_spec
#print axioms Found.NFC.pair_inj
#print axioms Found.NFC.code_inj
#print axioms Found.NFC.atomCode_inj
#print axioms Found.NFC.imgSet
#print axioms Found.NFC.codeA_inj

found/NFCountStep.lean

lean_all/found/NFCountStep.lean

/-
  T106 (b), slice N4d(xii). found/NFCountStep — in the classical point, the
  counting step in cardinal form:
  * cardinal strong limit: every type smaller than μ has its power set
    smaller than μ (holds for the parameter value of found/NFParamsO via
    the Beth stages);
  * if the orbits of lower tangles under the S-fixing allowable
    permutations have fewer than μ labels (labels of different levels
    differ), then the t-sets supported by S inject into μ (up to equality,
    by a map respecting equality) through the power set of the labels,
    made extensional by canonical representatives: the per-support bound
    of found/NFReduce, hence the f-map;
  * the parameter value of found/NFParamsO satisfies the cardinal strong
    limit (every ν < μ lies in a Beth stage whose power set is < μ).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.NFOrbit
import found.NFParamsO

namespace Found

namespace NFCS

open CP WO Card NFP NFPO NFB NFS NFL NFA NFT NFU NFR NFO NFC

variable (K : Classical') {P : ParamsO} {α : P.Λ}
  (L : ∀ β, P.ltΛ β α → Lower P β) (Ty : ∀ γ (h : P.ltΛ γ α), Typed (L γ h))

/-- cardinal strong limit (extensional form): the power set of a type smaller than μ is coded into μ -/
def CardStrong (P : ParamsO) : Prop :=
  ∀ R : Type, Smaller R P.μ → ∃ c : (R → Prop) → P.μ, ∀ Q Q', c Q = c Q' → ∀ r, Q r ↔ Q' r

/-- fewer than μ orbit labels -/
def OrbitSmall (S : Addr (P := P) α → Prop) : Prop :=
  ∃ (R : Type), Smaller R P.μ ∧ ∃ r : ∀ β (h : P.ltΛ β α), (L β h).T → R,
    (∀ β h t β' h' t', r β h t = r β' h' t' → β = β') ∧ (∀ β h t t', r β h t = r β h t' → Orb L S β h t t')

/-- the labels of a pre-t-set: orbits meeting its extensions -/
def lab {R : Type} (r : ∀ β (h : P.ltΛ β α), (L β h).T → R) (X : Pre K L Ty) : R → Prop :=
  fun q => ∃ β h t, r β h t = q ∧ X.E β h t

theorem lab_determines {R : Type} (S : Addr (P := P) α → Prop) (r : ∀ β (h : P.ltΛ β α), (L β h).T → R)
    (disj : ∀ β h t β' h' t', r β h t = r β' h' t' → β = β')
    (refl : ∀ β h t t', r β h t = r β h t' → Orb L S β h t t')
    (X Y : Pre K L Ty) (hX : SuppT K L Ty X S) (hY : SuppT K L Ty Y S)
    (hl : ∀ q, lab K L Ty r X q ↔ lab K L Ty r Y q) : PEq K L Ty X Y := by
  intro β h t
  constructor
  · intro ht
    match (hl _).1 ⟨β, h, t, rfl, ht⟩ with
    | ⟨β', h', t', hq, ht'⟩ =>
      have eb := disj _ _ _ _ _ _ hq
      subst eb
      exact invariant K L Ty Y S hY _ _ t' t (refl _ _ _ _ hq) ht'
  · intro ht
    match (hl _).2 ⟨β, h, t, rfl, ht⟩ with
    | ⟨β', h', t', hq, ht'⟩ =>
      have eb := disj _ _ _ _ _ _ hq
      subst eb
      exact invariant K L Ty X S hX _ _ t' t (refl _ _ _ _ hq) ht'

include K in
/-- the counting step: small orbit counts give the per-support bound, hence the f-map -/
theorem bound_of_small (hc : CardStrong P) (hO : ∀ B : SSet P (Addr (P := P) α), OrbitSmall L B.S) :
    Bound K L Ty := fun B =>
  match hO B with
  | ⟨R, hR, r, disj, refl⟩ => match hc R hR with
    | ⟨c, hcc⟩ =>
      ⟨fun X => c (can K (lab K L Ty r X.val)),
       fun _ _ e => congrArg c (can_ext K _ _ (fun _ =>
         ⟨fun ⟨β, h, t, hq, ht⟩ => ⟨β, h, t, hq, (e β h t).1 ht⟩,
          fun ⟨β, h, t, hq, ht⟩ => ⟨β, h, t, hq, (e β h t).2 ht⟩⟩)),
       fun X Y eq => lab_determines K L Ty B.S r disj refl X.val Y.val X.property Y.property (fun q =>
         ((can_spec K _ q).symm.trans (hcc _ _ eq q)).trans (can_spec K _ q))⟩

/-- the concrete parameter value is a cardinal strong limit -/
theorem params_cardStrong (K : Classical') : CardStrong (NFPO.params K) := fun R hR =>
  match BethS.strong_limit K R hR.2 with
  | ⟨_, ⟨f, hf⟩, ⟨⟨h, hh⟩, _⟩⟩ => ⟨fun Q => h (fun b => ∃ r, f r = b ∧ Q r), fun Q Q' e r =>
      have e2 := congrFun (hh _ _ e) (f r)
      ⟨fun hq => match (show ∃ r', f r' = f r ∧ Q' r' from e2 ▸ ⟨r, rfl, hq⟩) with
        | ⟨_, hr', hq'⟩ => (hf _ _ hr') ▸ hq',
       fun hq => match (show ∃ r', f r' = f r ∧ Q r' from e2.symm ▸ ⟨r, rfl, hq⟩) with
        | ⟨_, hr', hq'⟩ => (hf _ _ hr') ▸ hq'⟩⟩

end NFCS

end Found

#print axioms Found.NFCS.lab_determines
#print axioms Found.NFCS.bound_of_small
#print axioms Found.NFCS.params_cardStrong

found/NFExtend.lean

lean_all/found/NFExtend.lean

/-
  T106 (b), slice N4d(iv). found/NFExtend — in the classical point, the
  cardinal base of con-nf's freedom of action:
  * the complement of a small subset of κ contains a copy of κ;
  * (with ℕ ↪ μ) the complement of a small subset of μ contains a copy of
    μ (some column of μ × μ ↪ μ misses it);
  * on any type whose small complements are this big, a bijection between
    two small subsets extends to a permutation of the whole type
    (Cantor–Schröder–Bernstein on the complements); so on κ and on μ.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.NFCount
import found.NFLift

namespace Found

namespace NFE

open CP WO Card NFP NFPO NFB NFS NFC NFLi

variable (K : Classical') (P : ParamsO)

include K in
/-- the complement of a small subset of κ contains a copy of κ -/
theorem compl_kappa (D : P.κ → Prop) (hD : Small P {x // D x}) : ∃ f : P.κ → {x // ¬ D x}, Inj f := by
  refine dne K _ (fun hn => ?_)
  have hc : Small P {x // ¬ D x} := ⟨⟨fun x => x.val, fun x y e => Subtype.ext e⟩, hn⟩
  have hs := small_sum K P _ _ hD hc
  have ex : ∀ x : P.κ, ∃ s : {x // D x} ⊕ {x // ¬ D x}, (match s with | .inl a => a.val | .inr b => b.val) = x :=
    fun x => (K.lem (D x)).elim (fun h => ⟨.inl ⟨x, h⟩, rfl⟩) (fun h => ⟨.inr ⟨x, h⟩, rfl⟩)
  exact hs.2 ⟨fun x => K.ch _ (ex x), fun x y e => by
    have hx := K.ch_spec _ (ex x)
    have hy := K.ch_spec _ (ex y)
    have e' : K.ch _ (ex x) = K.ch _ (ex y) := e
    rw [e'] at hx
    exact hx.symm.trans hy⟩

variable {e : Nat → P.μ} (he : Inj e)

include K he in
/-- the complement of a small subset of μ contains a copy of μ -/
theorem compl_mu (D : P.μ → Prop) (hD : Small P {x // D x}) : ∃ f : P.μ → {x // ¬ D x}, Inj f := by
  have col : ∃ i, ∀ j, ¬ D (pair K P he (i, j)) := dne K _ (fun hn => by
    have hall : ∀ i, ∃ j, D (pair K P he (i, j)) := fun i => dne K _ (fun hj => hn ⟨i, fun j hd => hj ⟨j, hd⟩⟩)
    match hD.1 with
    | ⟨g, hg⟩ => exact P.κ_lt_μ.2 ⟨fun i => g ⟨_, K.ch_spec _ (hall i)⟩, fun i i' eq => by
        have e1 := congrArg Subtype.val (hg _ _ eq)
        have e2 := pair_inj K P he _ _ e1
        exact congrArg Prod.fst e2⟩)
  match col with
  | ⟨i, hi⟩ => exact ⟨fun j => ⟨pair K P he (i, j), hi j⟩, fun j j' eq => by
      have e1 := congrArg Subtype.val eq
      have e2 := pair_inj K P he _ _ e1
      exact congrArg Prod.snd e2⟩

/-! ## extending small partial bijections -/

theorem bcase_false {A : Sort _} : (b : Bool) → (t : b = true → A) → (f : b = false → A) → (h : b = false) →
    bcase b t f = f h
  | false, _, _, rfl => rfl

theorem bool_false : (b : Bool) → (b = true → False) → b = false
  | true, h => (h rfl).elim
  | false, _ => rfl

section Ext

variable {X : Type} (D R : X → Prop)
  (bigD : ∃ f : X → {x // ¬ D x}, Inj f) (bigR : ∃ f : X → {x // ¬ R x}, Inj f)
  (f : {x // D x} → {x // R x}) (g : {x // R x} → {x // D x})
  (fg : ∀ y, f (g y) = y) (gf : ∀ x, g (f x) = x)

include K bigD bigR in
theorem compl_iso : ∃ h : {x // ¬ D x} → {x // ¬ R x}, ∃ k : {x // ¬ R x} → {x // ¬ D x},
    (∀ y, h (k y) = y) ∧ (∀ x, k (h x) = x) :=
  match bigD, bigR with
  | ⟨a, ha⟩, ⟨b, hb⟩ =>
    let u : {x // ¬ D x} → {x // ¬ R x} := fun x => b x.val
    let v : {x // ¬ R x} → {x // ¬ D x} := fun y => a y.val
    have hu : Inj u := fun _ _ e => Subtype.ext (hb _ _ e)
    have hv : Inj v := fun _ _ e => Subtype.ext (ha _ _ e)
    ⟨csb K u v, csbInv K u v hv, csb_inv_right K u v hv, csb_inv_left K u v hu hv⟩

include K bigD bigR fg gf in
/-- a bijection between small subsets extends to a permutation -/
theorem extend : ∃ p q : X → X, (∀ x, p (q x) = x) ∧ (∀ x, q (p x) = x) ∧ ∀ x (hx : D x), p x = (f ⟨x, hx⟩).val :=
  match compl_iso K D R bigD bigR with
  | ⟨h, k, hk, kh⟩ =>
    let p : X → X := fun x => bcase (dec K (D x)) (fun c => (f ⟨x, (dec_spec K _).1 c⟩).val)
      (fun c => (h ⟨x, fun d => Bool.noConfusion (c.symm.trans ((dec_spec K _).2 d))⟩).val)
    let q : X → X := fun y => bcase (dec K (R y)) (fun c => (g ⟨y, (dec_spec K _).1 c⟩).val)
      (fun c => (k ⟨y, fun d => Bool.noConfusion (c.symm.trans ((dec_spec K _).2 d))⟩).val)
    have p_in : ∀ x (hx : D x), p x = (f ⟨x, hx⟩).val := fun x hx =>
      bcase_true _ _ _ ((dec_spec K _).2 hx)
    have p_out : ∀ x (hx : ¬ D x), p x = (h ⟨x, hx⟩).val := fun x hx =>
      bcase_false _ _ _ (bool_false _ (fun c => hx ((dec_spec K _).1 c)))
    have q_in : ∀ y (hy : R y), q y = (g ⟨y, hy⟩).val := fun y hy =>
      bcase_true _ _ _ ((dec_spec K _).2 hy)
    have q_out : ∀ y (hy : ¬ R y), q y = (k ⟨y, hy⟩).val := fun y hy =>
      bcase_false _ _ _ (bool_false _ (fun c => hy ((dec_spec K _).1 c)))
    ⟨p, q,
     fun y => (K.lem (R y)).elim
       (fun hy => by
         rw [q_in y hy, p_in _ (g ⟨y, hy⟩).property]
         exact congrArg Subtype.val (fg ⟨y, hy⟩))
       (fun hy => by
         rw [q_out y hy, p_out _ (k ⟨y, hy⟩).property]
         exact congrArg Subtype.val (hk ⟨y, hy⟩)),
     fun x => (K.lem (D x)).elim
       (fun hx => by
         rw [p_in x hx, q_in _ (f ⟨x, hx⟩).property]
         exact congrArg Subtype.val (gf ⟨x, hx⟩))
       (fun hx => by
         rw [p_out x hx, q_out _ (h ⟨x, hx⟩).property]
         exact congrArg Subtype.val (kh ⟨x, hx⟩)),
     p_in⟩

end Ext

include K in
/-- small partial bijections of κ extend to permutations of κ -/
theorem extend_kappa (D R : P.κ → Prop) (hD : Small P {x // D x}) (hR : Small P {x // R x})
    (f : {x // D x} → {x // R x}) (g : {x // R x} → {x // D x}) (fg : ∀ y, f (g y) = y) (gf : ∀ x, g (f x) = x) :
    ∃ p q : P.κ → P.κ, (∀ x, p (q x) = x) ∧ (∀ x, q (p x) = x) ∧ ∀ x (hx : D x), p x = (f ⟨x, hx⟩).val :=
  extend K D R (compl_kappa K P D hD) (compl_kappa K P R hR) f g fg gf

include K he in
/-- small partial bijections of μ extend to permutations of μ -/
theorem extend_mu (D R : P.μ → Prop) (hD : Small P {x // D x}) (hR : Small P {x // R x})
    (f : {x // D x} → {x // R x}) (g : {x // R x} → {x // D x}) (fg : ∀ y, f (g y) = y) (gf : ∀ x, g (f x) = x) :
    ∃ p q : P.μ → P.μ, (∀ x, p (q x) = x) ∧ (∀ x, q (p x) = x) ∧ ∀ x (hx : D x), p x = (f ⟨x, hx⟩).val :=
  extend K D R (compl_mu K P he D hD) (compl_mu K P he R hR) f g fg gf

end NFE

end Found

#print axioms Found.NFE.compl_kappa
#print axioms Found.NFE.compl_mu
#print axioms Found.NFE.compl_iso
#print axioms Found.NFE.extend
#print axioms Found.NFE.extend_kappa
#print axioms Found.NFE.extend_mu

found/NFFree.lean

lean_all/found/NFFree.lean

/-
  T106 (b), slice N4d(v). found/NFFree — in the classical point (ℕ ↪ μ),
  freedom of action for base permutations (con-nf): a small specification
  * a bijection φ between two small sets of litters,
  * for every litter i, a bijection ψ_i between two small sets of positions
    in κ (atoms (i, k) of litter i go to atoms (φ i, ψ_i k)),
  extends to a base permutation π: litters go to litters exactly
  (π[L_i] = L_{p i} with p a permutation of μ extending φ), near-litters to
  near-litters, and π(i, k) = (φ i, ψ_i k) on the specification.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.NFExtend

namespace Found

namespace NFF

open CP WO Card NFP NFPO NFB NFS NFC NFE

variable (K : Classical') (P : ParamsO) {e : Nat → P.μ} (he : Inj e)

/-- a permutation of a type extending given data -/
def PermExt {X : Type} (D R : X → Prop) (f : {x // D x} → {x // R x}) (pq : (X → X) × (X → X)) : Prop :=
  (∀ x, pq.1 (pq.2 x) = x) ∧ (∀ x, pq.2 (pq.1 x) = x) ∧ ∀ x (hx : D x), pq.1 x = (f ⟨x, hx⟩).val

variable (DL RL : P.μ → Prop) (hDL : Small P {x // DL x}) (hRL : Small P {x // RL x})
  (φ : {x // DL x} → {x // RL x}) (φ' : {x // RL x} → {x // DL x})
  (φφ : ∀ y, φ (φ' y) = y) (φ'φ : ∀ x, φ' (φ x) = x)
  (Da Ra : P.μ → P.κ → Prop) (hDa : ∀ i, Small P {x // Da i x}) (hRa : ∀ i, Small P {x // Ra i x})
  (ψ : ∀ i, {x // Da i x} → {x // Ra i x}) (ψ' : ∀ i, {x // Ra i x} → {x // Da i x})
  (ψψ : ∀ i y, ψ i (ψ' i y) = y) (ψ'ψ : ∀ i x, ψ' i (ψ i x) = x)

include K he hDL hRL φφ φ'φ in
theorem lit_ex : ∃ pq, PermExt DL RL φ pq :=
  match extend_mu K P he DL RL hDL hRL φ φ' φφ φ'φ with
  | ⟨p, q, h1, h2, h3⟩ => ⟨(p, q), h1, h2, h3⟩

include K hDa hRa ψψ ψ'ψ in
theorem pos_ex (i : P.μ) : ∃ st, PermExt (Da i) (Ra i) (ψ i) st :=
  match extend_kappa K P (Da i) (Ra i) (hDa i) (hRa i) (ψ i) (ψ' i) (ψψ i) (ψ'ψ i) with
  | ⟨p, q, h1, h2, h3⟩ => ⟨(p, q), h1, h2, h3⟩

include K he hDL hRL φφ φ'φ hDa hRa ψψ ψ'ψ in
/-- freedom of action for base permutations -/
theorem free_base : ∃ π : BPerm P, (∀ i (hi : DL i) k (hk : Da i k),
      π.f (i, k) = ((φ ⟨i, hi⟩).val, (ψ i ⟨k, hk⟩).val)) ∧
    ∃ p : P.μ → P.μ, ∀ i a, (img P π.g (litter P i) a ↔ litter P (p i) a) := by
  obtain ⟨⟨p, q⟩, hpq1', hpq2', hpq3'⟩ := lit_ex K P he DL RL hDL hRL φ φ' φφ φ'φ
  have hpq1 : ∀ x, p (q x) = x := hpq1'
  have hpq2 : ∀ x, q (p x) = x := hpq2'
  have hpq3 : ∀ x (hx : DL x), p x = (φ ⟨x, hx⟩).val := hpq3'
  let st : ∀ i : P.μ, (P.κ → P.κ) × (P.κ → P.κ) := fun i => K.ch _ (pos_ex K P Da Ra hDa hRa ψ ψ' ψψ ψ'ψ i)
  have hst : ∀ i, PermExt (Da i) (Ra i) (ψ i) (st i) := fun i => K.ch_spec _ (pos_ex K P Da Ra hDa hRa ψ ψ' ψψ ψ'ψ i)
  let F : Atom P → Atom P := fun a => (p a.1, (st a.1).1 a.2)
  let G : Atom P → Atom P := fun a => (q a.1, (st (q a.1)).2 a.2)
  have FG : ∀ a, F (G a) = a := fun a => by
    show (p (q a.1), (st (q a.1)).1 ((st (q a.1)).2 a.2)) = a
    rw [hpq1 a.1, (hst (q a.1)).1 a.2]
  have GF : ∀ a, G (F a) = a := fun a => by
    show (q (p a.1), (st (q (p a.1))).2 ((st a.1).1 a.2)) = a
    rw [hpq2 a.1, (hst a.1).2.1 a.2]
  -- litters go exactly to litters
  have litG : ∀ i a, litter P i (G a) ↔ litter P (p i) a := fun i a =>
    ⟨fun h => by
      have h' : q a.1 = i := h
      show a.1 = p i
      rw [← h', hpq1 a.1],
     fun h => by
      have h' : a.1 = p i := h
      show q a.1 = i
      rw [h', hpq2 i]⟩
  have litF : ∀ i a, litter P i (F a) ↔ litter P (q i) a := fun i a =>
    ⟨fun h => by
      have h' : p a.1 = i := h
      show a.1 = q i
      rw [← h', hpq2 a.1],
     fun h => by
      have h' : a.1 = q i := h
      show p a.1 = i
      rw [h', hpq1 i]⟩
  have nearG : ∀ S, NearLitter P S → NearLitter P (img P G S) := fun S ⟨i, hi⟩ =>
    ⟨p i, small_of_inj P (fun x => (⟨G x.val, x.property.elim
        (fun h => Or.inl ⟨h.1, fun l => h.2 ((litG i x.val).1 l)⟩)
        (fun h => Or.inr ⟨(litG i x.val).2 h.1, h.2⟩)⟩ : SymDiff P S (litter P i)))
      (fun x y eq => Subtype.ext (by
        have e1 : G x.val = G y.val := congrArg Subtype.val eq
        have e2 := congrArg F e1
        rw [FG, FG] at e2
        exact e2)) hi⟩
  have nearF : ∀ S, NearLitter P S → NearLitter P (img P F S) := fun S ⟨i, hi⟩ =>
    ⟨q i, small_of_inj P (fun x => (⟨F x.val, x.property.elim
        (fun h => Or.inl ⟨h.1, fun l => h.2 ((litF i x.val).1 l)⟩)
        (fun h => Or.inr ⟨(litF i x.val).2 h.1, h.2⟩)⟩ : SymDiff P S (litter P i)))
      (fun x y eq => Subtype.ext (by
        have e1 : F x.val = F y.val := congrArg Subtype.val eq
        have e2 := congrArg G e1
        rw [GF, GF] at e2
        exact e2)) hi⟩
  refine ⟨⟨F, G, FG, GF, nearG, nearF⟩, fun i hi k hk => ?_, p, litG⟩
  show (p i, (st i).1 k) = _
  rw [hpq3 i hi, (hst i).2.2 k hk]

end NFF

end Found

#print axioms Found.NFF.lit_ex
#print axioms Found.NFF.pos_ex
#print axioms Found.NFF.free_base

found/NFInflex.lean

lean_all/found/NFInflex.lean

/-
  T106 (b), slice N4d(vi). found/NFInflex — in the classical point, freedom
  of action on inflexible litters (con-nf): given lower levels i with
  tangles T_i (an equivalence as equality), jointly injective f-maps
  fz_i : T_i → μ (different levels have disjoint images), and permutations
  r_i of every T_i (up to the equivalence), the litter map
      fz_i t ↦ fz_i (r_i t)  on the inflexible litters, identity on the
      flexible ones (not in any image)
  is a permutation of μ, and a base permutation realises it exactly
  (litters to litters); hence its induced litter map π̂ satisfies the
  coherence π̂ (fz_i t) = fz_i (r_i t) required of allowable permutations.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.NFLitter

namespace Found

namespace NFI

open CP WO Card NFP NFPO NFB NFS NFL

variable (K : Classical') {P : ParamsO}

/-- every permutation of μ gives a base permutation moving litters exactly -/
def ofLit (p q : P.μ → P.μ) (pq : ∀ i, p (q i) = i) (qp : ∀ i, q (p i) = i) : BPerm P where
  f a := (p a.1, a.2)
  g a := (q a.1, a.2)
  fg a := by
    show (p (q a.1), a.2) = a
    rw [pq]
  gf a := by
    show (q (p a.1), a.2) = a
    rw [qp]
  near S := fun ⟨i, hi⟩ => ⟨p i, small_of_inj P (fun x => (⟨(q x.val.1, x.val.2), x.property.elim
      (fun h => Or.inl ⟨h.1, fun l => h.2 (show x.val.1 = p i from
        (pq x.val.1).symm.trans (congrArg p (show q x.val.1 = i from l)))⟩)
      (fun h => Or.inr ⟨show q x.val.1 = i from
        (congrArg q (show x.val.1 = p i from h.1)).trans (qp i), h.2⟩)⟩ : SymDiff P S (litter P i)))
    (fun x y eq => Subtype.ext (by
      have e1 : ((q x.val.1, x.val.2) : Atom P) = (q y.val.1, y.val.2) := congrArg Subtype.val eq
      have e2 := congrArg (fun a : Atom P => (p a.1, a.2)) e1
      show x.val = y.val
      have ex : (p (q x.val.1), x.val.2) = x.val := by rw [pq]
      have ey : (p (q y.val.1), y.val.2) = y.val := by rw [pq]
      exact ex.symm.trans (e2.trans ey))) hi⟩
  near_inv S := fun ⟨i, hi⟩ => ⟨q i, small_of_inj P (fun x => (⟨(p x.val.1, x.val.2), x.property.elim
      (fun h => Or.inl ⟨h.1, fun l => h.2 (show x.val.1 = q i from
        (qp x.val.1).symm.trans (congrArg q (show p x.val.1 = i from l)))⟩)
      (fun h => Or.inr ⟨show p x.val.1 = i from
        (congrArg p (show x.val.1 = q i from h.1)).trans (pq i), h.2⟩)⟩ : SymDiff P S (litter P i)))
    (fun x y eq => Subtype.ext (by
      have e1 : ((p x.val.1, x.val.2) : Atom P) = (p y.val.1, y.val.2) := congrArg Subtype.val eq
      have e2 := congrArg (fun a : Atom P => (q a.1, a.2)) e1
      show x.val = y.val
      have ex : (q (p x.val.1), x.val.2) = x.val := by rw [qp]
      have ey : (q (p y.val.1), y.val.2) = y.val := by rw [qp]
      exact ex.symm.trans (e2.trans ey))) hi⟩

/-- its induced litter map is p -/
theorem lmap_ofLit (p q : P.μ → P.μ) (pq : ∀ i, p (q i) = i) (qp : ∀ i, q (p i) = i) (i : P.μ) :
    lmap K (ofLit p q pq qp) i = p i :=
  lmap_eq K _ i _ (congr_small P (litter P (p i)) _ _ (fun a =>
      ⟨fun h => by
        have h' : a.1 = p i := h
        show q a.1 = i
        rw [h', qp],
       fun h => by
        have h' : q a.1 = i := h
        show a.1 = p i
        rw [← h', pq]⟩)
    (small_empty K P _ (symm_self_empty P _)))

section Inflex

variable {I : Type} (T : I → Type) (eqv : ∀ i, T i → T i → Prop)
  (esymm : ∀ i {t t'}, eqv i t t' → eqv i t' t)
  (fz : ∀ i, T i → P.μ) (fz_resp : ∀ i t t', eqv i t t' → fz i t = fz i t')
  (fz_inj : ∀ i t t', fz i t = fz i t' → eqv i t t')
  (fz_disj : ∀ i j t t', fz i t = fz j t' → i = j)
  (r r' : ∀ i, T i → T i) (r_resp : ∀ i t t', eqv i t t' → eqv i (r i t) (r i t'))
  (r'_resp : ∀ i t t', eqv i t t' → eqv i (r' i t) (r' i t'))
  (rr' : ∀ i t, eqv i (r i (r' i t)) t) (r'r : ∀ i t, eqv i (r' i (r i t)) t)

/-- the forced litter map: on images of f-maps, transport by r; identity elsewhere -/
def Spec (s : ∀ i, T i → T i) (i0 j : P.μ) : Prop :=
  (∃ i t, fz i t = i0 ∧ fz i (s i t) = j) ∨ ((¬ ∃ i t, fz i t = i0) ∧ j = i0)

include K in
theorem spec_ex (s : ∀ i, T i → T i) (i0 : P.μ) : ∃ j, Spec T fz s i0 j :=
  (K.lem (∃ i t, fz i t = i0)).elim (fun ⟨i, t, h⟩ => ⟨_, Or.inl ⟨i, t, h, rfl⟩⟩) (fun hn => ⟨i0, Or.inr ⟨hn, rfl⟩⟩)

def lit (s : ∀ i, T i → T i) (i0 : P.μ) : P.μ := K.ch _ (spec_ex K T fz s i0)

include K fz_resp fz_inj fz_disj in
theorem lit_in (s : ∀ i, T i → T i) (s_resp : ∀ i t t', eqv i t t' → eqv i (s i t) (s i t'))
    (i : I) (t : T i) : lit K T fz s (fz i t) = fz i (s i t) :=
  (K.ch_spec _ (spec_ex K T fz s (fz i t))).elim
    (fun ⟨j, u, hu, hj⟩ => by
      have eij := fz_disj _ _ _ _ hu
      subst eij
      exact (show lit K T fz s (fz _ t) = fz _ (s _ u) from hj.symm).trans
        (fz_resp _ _ _ (s_resp _ _ _ (fz_inj _ _ _ hu))))
    (fun ⟨hn, _⟩ => (hn ⟨i, t, rfl⟩).elim)

include K in
theorem lit_out (s : ∀ i, T i → T i) (i0 : P.μ) (hn : ¬ ∃ i t, fz i t = i0) : lit K T fz s i0 = i0 :=
  (K.ch_spec _ (spec_ex K T fz s i0)).elim (fun ⟨i, t, hu, _⟩ => (hn ⟨i, t, hu⟩).elim) (fun ⟨_, h⟩ => h)

include K fz_resp fz_inj fz_disj r_resp r'_resp rr' in
theorem lit_inv_r (i0 : P.μ) : lit K T fz r (lit K T fz r' i0) = i0 :=
  (K.lem (∃ i t, fz i t = i0)).elim
    (fun ⟨i, t, h⟩ => by
      rw [← h, lit_in K T eqv fz fz_resp fz_inj fz_disj r' r'_resp, lit_in K T eqv fz fz_resp fz_inj fz_disj r r_resp]
      exact fz_resp _ _ _ (rr' i t))
    (fun hn => by rw [lit_out K T fz r' i0 hn, lit_out K T fz r i0 hn])

include K fz_resp fz_inj fz_disj r_resp r'_resp r'r in
theorem lit_inv_l (i0 : P.μ) : lit K T fz r' (lit K T fz r i0) = i0 :=
  (K.lem (∃ i t, fz i t = i0)).elim
    (fun ⟨i, t, h⟩ => by
      rw [← h, lit_in K T eqv fz fz_resp fz_inj fz_disj r r_resp, lit_in K T eqv fz fz_resp fz_inj fz_disj r' r'_resp]
      exact fz_resp _ _ _ (r'r i t))
    (fun hn => by rw [lit_out K T fz r i0 hn, lit_out K T fz r' i0 hn])

include K fz_resp fz_inj fz_disj r_resp r'_resp rr' r'r in
/-- freedom of action on inflexible litters: a base permutation with the forced litter map -/
theorem free_inflex : ∃ π : BPerm P, ∀ i t, lmap K π (fz i t) = fz i (r i t) :=
  ⟨ofLit (lit K T fz r) (lit K T fz r')
      (lit_inv_r K T eqv fz fz_resp fz_inj fz_disj r r' r_resp r'_resp rr')
      (lit_inv_l K T eqv fz fz_resp fz_inj fz_disj r r' r_resp r'_resp r'r),
   fun i t => (lmap_ofLit K _ _ _ _ _).trans (lit_in K T eqv fz fz_resp fz_inj fz_disj r r_resp i t)⟩

end Inflex

end NFI

end Found

#print axioms Found.NFI.ofLit
#print axioms Found.NFI.lmap_ofLit
#print axioms Found.NFI.lit_in
#print axioms Found.NFI.lit_inv_r
#print axioms Found.NFI.lit_inv_l
#print axioms Found.NFI.free_inflex

found/NFJensen.lean

lean_all/found/NFJensen.lean

/-
  T106 (a), first half. found/NFJensen — the parameter route to NFU/NF
  (Specker, Jensen): a model of typed set theory TST (levels as predicates on
  one carrier, typed comprehension with quantifiers relativized to levels)
  together with a type-raising automorphism σ (σ maps level n onto level
  n+1, injective, preserves membership) yields, on level 0 with
  x ∈' y := x ∈ σ y, stratified comprehension (NFU / NF comprehension).
  The existence of such a value (Jensen: via a model of Mac Lane set theory
  with an automorphism) is not proved here.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.NF

namespace Found

namespace NFJ

open NF

/-- typed satisfaction: quantified variable n ranges over level t n -/
def satT {M : Type} (E : M → M → Prop) (L : Nat → M → Prop) (t : Nat → Nat) : (Nat → M) → Fm → Prop
  | ρ, .mem i j => E (ρ i) (ρ j)
  | ρ, .eq i j => ρ i = ρ j
  | ρ, .neg a => ¬ satT E L t ρ a
  | ρ, .and a b => satT E L t ρ a ∧ satT E L t ρ b
  | ρ, .all n a => ∀ m, L (t n) m → satT E L t (upd ρ n m) a

structure TSTA where
  M : Type
  E : M → M → Prop
  L : Nat → M → Prop
  σ : M → M
  σL : ∀ n x, L n x → L (n + 1) (σ x)
  σonto : ∀ n y, L (n + 1) y → ∃ x, L n x ∧ σ x = y
  σE : ∀ x y, E x y ↔ E (σ x) (σ y)
  σinj : ∀ x y, σ x = σ y → x = y
  /-- typed comprehension: quantifiers range over the level of their variable -/
  comp : ∀ (φ : Fm) (t : Nat → Nat), Strat t φ → ∀ ρ : Nat → M, (∀ v, L (t v) (ρ v)) →
    ∃ z, L (t 0 + 1) z ∧ ∀ x, L (t 0) x → (E x z ↔ satT E L t (upd ρ 0 x) φ)

theorem ite_t {α : Type} {c : Prop} [inst : Decidable c] {a b : α} (h : c) : (if c then a else b) = a := by
  cases inst with
  | isTrue _ => rfl
  | isFalse h' => exact absurd h h'

theorem ite_f {α : Type} {c : Prop} [inst : Decidable c] {a b : α} (h : ¬ c) : (if c then a else b) = b := by
  cases inst with
  | isTrue h' => exact absurd h' h
  | isFalse _ => rfl

variable (T : TSTA)

def pw (σ : T.M → T.M) : Nat → T.M → T.M
  | 0, x => x
  | k + 1, x => σ (pw σ k x)

theorem pw_L : (k : Nat) → (x : T.M) → T.L 0 x → T.L k (pw T T.σ k x)
  | 0, _, h => h
  | k + 1, x, h => T.σL k _ (pw_L k x h)

theorem pw_onto : (k : Nat) → (y : T.M) → T.L k y → ∃ x, T.L 0 x ∧ pw T T.σ k x = y
  | 0, y, h => ⟨y, h, rfl⟩
  | k + 1, y, h => match T.σonto k y h with
    | ⟨y', hy', e⟩ => match pw_onto k y' hy' with
      | ⟨x, hx, e'⟩ => ⟨x, hx, by show T.σ (pw T T.σ k x) = y; rw [e', e]⟩

theorem pw_E : (k : Nat) → (x y : T.M) → (T.E x y ↔ T.E (pw T T.σ k x) (pw T T.σ k y))
  | 0, _, _ => Iff.rfl
  | k + 1, x, y => (pw_E k x y).trans (T.σE _ _)

theorem pw_shift : (k : Nat) → (y : T.M) → pw T T.σ k (T.σ y) = pw T T.σ (k + 1) y
  | 0, _ => rfl
  | k + 1, y => congrArg T.σ (pw_shift k y)

theorem pw_inj : (k : Nat) → (x y : T.M) → pw T T.σ k x = pw T T.σ k y → x = y
  | 0, _, _, h => h
  | k + 1, x, y, h => pw_inj k x y (T.σinj _ _ h)

/-- the NFU structure on level 0: x ∈' y iff x ∈ σ y, quantifiers over level 0 -/
def satN : (Nat → T.M) → Fm → Prop
  | ρ, .mem i j => T.E (ρ i) (T.σ (ρ j))
  | ρ, .eq i j => ρ i = ρ j
  | ρ, .neg a => ¬ satN ρ a
  | ρ, .and a b => satN ρ a ∧ satN ρ b
  | ρ, .all n a => ∀ m, T.L 0 m → satN (upd ρ n m) a

theorem satT_ext {t : Nat → Nat} : (φ : Fm) → {ρ ρ' : Nat → T.M} → (∀ v, ρ v = ρ' v) →
    (satT T.E T.L t ρ φ ↔ satT T.E T.L t ρ' φ)
  | .mem i j, _, _, h => by show T.E _ _ ↔ T.E _ _; rw [h i, h j]
  | .eq i j, _, _, h => by show _ = _ ↔ _ = _; rw [h i, h j]
  | .neg a, _, _, h => not_congr' (satT_ext a h)
  | .and a b, _, _, h => and_congr' (satT_ext a h) (satT_ext b h)
  | .all n a, ρ, ρ', h => ⟨fun k m hm => (satT_ext a (upd_ext h n m)).mp (k m hm),
      fun k m hm => (satT_ext a (upd_ext h n m)).mpr (k m hm)⟩
where
  not_congr' {p q : Prop} (h : p ↔ q) : ¬ p ↔ ¬ q := ⟨fun np x => np (h.mpr x), fun nq x => nq (h.mp x)⟩
  and_congr' {p q r s : Prop} (h1 : p ↔ q) (h2 : r ↔ s) : p ∧ r ↔ q ∧ s :=
    ⟨fun ⟨a, b⟩ => ⟨h1.mp a, h2.mp b⟩, fun ⟨a, b⟩ => ⟨h1.mpr a, h2.mpr b⟩⟩
  upd_ext {ρ ρ' : Nat → T.M} (h : ∀ v, ρ v = ρ' v) (n : Nat) (m : T.M) : ∀ v, upd ρ n m v = upd ρ' n m v :=
    fun v => by
      unfold upd
      cases Nat.decEq v n with
      | isTrue e => rw [ite_t e, ite_t e]
      | isFalse e => rw [ite_f e, ite_f e, h v]

/-- the transformed valuation: variable v of type t v goes to σ^(t v) -/
def tr (t : Nat → Nat) (ρ : Nat → T.M) : Nat → T.M := fun v => pw T T.σ (t v) (ρ v)

theorem tr_upd (t : Nat → Nat) (ρ : Nat → T.M) (n : Nat) (m : T.M) :
    ∀ v, tr T t (upd ρ n m) v = upd (tr T t ρ) n (pw T T.σ (t n) m) v := fun v => by
  unfold tr upd
  cases Nat.decEq v n with
  | isTrue e => rw [ite_t e, ite_t e, e]
  | isFalse e => rw [ite_f e, ite_f e]

/-- transfer: a stratified formula holds in the NFU structure iff it holds in
  TST at the σ-shifted valuation -/
theorem transfer (t : Nat → Nat) : (φ : Fm) → Strat t φ → (ρ : Nat → T.M) → (∀ v, T.L 0 (ρ v)) →
    (satN T ρ φ ↔ satT T.E T.L t (tr T t ρ) φ)
  | .mem i j, h, ρ, _ => by
    show T.E (ρ i) (T.σ (ρ j)) ↔ T.E (pw T T.σ (t i) (ρ i)) (pw T T.σ (t j) (ρ j))
    have hj : t j = t i + 1 := h
    rw [hj, ← pw_shift]
    exact pw_E T (t i) _ _
  | .eq i j, h, ρ, _ => by
    show ρ i = ρ j ↔ pw T T.σ (t i) (ρ i) = pw T T.σ (t j) (ρ j)
    have hij : t i = t j := h
    rw [hij]
    exact ⟨fun e => by rw [e], pw_inj T (t j) _ _⟩
  | .neg a, h, ρ, hρ => satT_ext.not_congr' (transfer t a h ρ hρ)
  | .and a b, h, ρ, hρ => satT_ext.and_congr' (transfer t a h.1 ρ hρ) (transfer t b h.2 ρ hρ)
  | .all n a, h, ρ, hρ => by
    have hup : ∀ m, T.L 0 m → ∀ v, T.L 0 (upd ρ n m v) := fun m hm v => by
      unfold upd
      cases Nat.decEq v n with
      | isTrue e => rw [ite_t e]; exact hm
      | isFalse e => rw [ite_f e]; exact hρ v
    constructor
    · intro k m' hm'
      match pw_onto T (t n) m' hm' with
      | ⟨m, hm, e⟩ =>
        have := (transfer t a h (upd ρ n m) (hup m hm)).mp (k m hm)
        rw [← e]
        exact (satT_ext T a (tr_upd T t ρ n m)).mp this
    · intro k m hm
      exact (transfer t a h (upd ρ n m) (hup m hm)).mpr
        ((satT_ext T a (tr_upd T t ρ n m)).mpr (k _ (pw_L T (t n) m hm)))

/-- stratified comprehension in the NFU structure on level 0 -/
theorem nfu_comp (φ : Fm) (t : Nat → Nat) (h : Strat t φ) (ρ : Nat → T.M) (hρ : ∀ v, T.L 0 (ρ v)) :
    ∃ y, T.L 0 y ∧ ∀ x, T.L 0 x → (T.E x (T.σ y) ↔ satN T (upd ρ 0 x) φ) :=
  match T.comp φ t h (tr T t ρ) (fun v => pw_L T (t v) (ρ v) (hρ v)) with
  | ⟨z, hz, hcomp⟩ => match pw_onto T (t 0 + 1) z hz with
    | ⟨y, hy, ey⟩ => ⟨y, hy, fun x hx => by
        have hup : ∀ v, T.L 0 (upd ρ 0 x v) := fun v => by
          unfold upd
          cases Nat.decEq v 0 with
          | isTrue e => rw [ite_t e]; exact hx
          | isFalse e => rw [ite_f e]; exact hρ v
        refine (pw_E T (t 0) x (T.σ y)).trans ?_
        rw [pw_shift, ey]
        refine (hcomp _ (pw_L T (t 0) x hx)).trans ?_
        refine (satT_ext T φ (fun v => (tr_upd T t ρ 0 x v).symm)).trans ?_
        exact (transfer T t φ h (upd ρ 0 x) hup).symm⟩

/-- extensionality at level 1 of TST gives extensionality of ∈' on level 0:
  with it the level-0 structure is an NF model (Specker), without it NFU -/
theorem nf_ext (hext : ∀ a b, T.L 1 a → T.L 1 b → (∀ x, T.L 0 x → (T.E x a ↔ T.E x b)) → a = b)
    (a b : T.M) (ha : T.L 0 a) (hb : T.L 0 b) (h : ∀ x, T.L 0 x → (T.E x (T.σ a) ↔ T.E x (T.σ b))) : a = b :=
  T.σinj _ _ (hext _ _ (T.σL 0 a ha) (T.σL 0 b hb) h)

/-! ## Holmes' tangled type theory as a parameter

  A TTT model has levels indexed by ℕ and a membership E α β for every α < β,
  with typed comprehension for every strictly increasing assignment of levels.
  Given a uniform shift σ (level n onto n+1, preserving every E α β), its
  restriction to the chain 0 < 1 < 2 < … is a TST model with a type-raising
  automorphism, so the theorems above give NF / NFU on level 0.  (Holmes'
  general route, TTT ⟹ NF without a given shift, is the classical-point
  construction of part (b).) -/
structure TTTShift where
  M : Type
  E : Nat → Nat → M → M → Prop
  L : Nat → M → Prop
  σ : M → M
  σL : ∀ n x, L n x → L (n + 1) (σ x)
  σonto : ∀ n y, L (n + 1) y → ∃ x, L n x ∧ σ x = y
  σE : ∀ α β x y, E α β x y ↔ E (α + 1) (β + 1) (σ x) (σ y)
  σinj : ∀ x y, σ x = σ y → x = y
  /-- consecutive levels: the memberships along the chain agree with E 0 1 after shifting -/
  chain : ∀ x y, E 0 1 x y ↔ E 0 1 (σ x) (σ y)
  comp01 : ∀ (φ : Fm) (t : Nat → Nat), Strat t φ → ∀ ρ : Nat → M, (∀ v, L (t v) (ρ v)) →
    ∃ z, L (t 0 + 1) z ∧ ∀ x, L (t 0) x → (E 0 1 x z ↔ satT (E 0 1) L t (upd ρ 0 x) φ)

def TTTShift.toTSTA (S : TTTShift) : TSTA where
  M := S.M
  E := S.E 0 1
  L := S.L
  σ := S.σ
  σL := S.σL
  σonto := S.σonto
  σE := S.chain
  σinj := S.σinj
  comp := S.comp01

theorem ttt_nfu_comp (S : TTTShift) (φ : Fm) (t : Nat → Nat) (h : Strat t φ) (ρ : Nat → S.M)
    (hρ : ∀ v, S.L 0 (ρ v)) :
    ∃ y, S.L 0 y ∧ ∀ x, S.L 0 x → (S.E 0 1 x (S.σ y) ↔ satN S.toTSTA (upd ρ 0 x) φ) :=
  nfu_comp S.toTSTA φ t h ρ hρ

end NFJ

end Found

#print axioms Found.NFJ.transfer
#print axioms Found.NFJ.nfu_comp
#print axioms Found.NFJ.nf_ext
#print axioms Found.NFJ.ttt_nfu_comp

found/NFLabel.lean

lean_all/found/NFLabel.lean

/-
  T106 (b), slice N4d(xiii). found/NFLabel — in the classical point:
  * for the concrete parameter value (found/NFParamsO), the product of two
    types smaller than μ is smaller than μ (both lie in one Beth stage, and
    a stage squared embeds in itself by Hessenberg);
  * orbit labels assembled from a level, a type τ and an index n: if equal
    level, type and index mean the same S-fixing orbit, and the types and
    indices range over types smaller than μ, then there are fewer than μ
    orbit labels (found/NFCountStep's OrbitSmall).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.NFCountStep

namespace Found

namespace NFLb

open CP WO Card NFP NFPO NFB NFS NFL NFA NFT NFU NFO NFCS BethS W1 CU

variable (K : Classical')

theorem single_inj {X : Type} : Inj (fun x : X => (fun z : X => z = x)) := fun a _ e => cast (congrFun e a) rfl

/-- earlier stages embed in later ones -/
theorem stage_mono (α β : W1 K) (h : Lt K β α) : ∃ f : B K β → B K α, Inj f :=
  ⟨fun x => up K α β h (fun z => z = x), fun _ _ e => single_inj _ _ (up_inj K α β h _ _ e)⟩

theorem stage_common (α α' : W1 K) : ∃ γ, (∃ f : B K α → B K γ, Inj f) ∧ (∃ f : B K α' → B K γ, Inj f) :=
  (linW K α α').elim (fun h => ⟨α', stage_mono K α' α h, ⟨fun x => x, fun _ _ e => e⟩⟩)
    (fun h => h.elim (fun e => by subst e; exact ⟨α, ⟨fun x => x, fun _ _ e => e⟩, ⟨fun x => x, fun _ _ e => e⟩⟩)
      (fun h => ⟨α, ⟨fun x => x, fun _ _ e => e⟩, stage_mono K α α' h⟩))

theorem stage_sq (γ : W1 K) : ∃ F : B K γ × B K γ → B K γ, Inj F :=
  Hess.hessenberg K (e := fun k => low K γ (nat_pn k)) (fun _ _ e => nat_pn_inj _ _ (low_inj K γ _ _ e))

/-- products of types smaller than μ are smaller than μ (concrete parameters) -/
theorem prod_small (A C : Type) (hA : Smaller A (mu K)) (hC : Smaller C (mu K)) : Smaller (A × C) (mu K) :=
  match below_stage K A hA.2, below_stage K C hC.2 with
  | ⟨α, f, hf⟩, ⟨α', g, hg⟩ => match stage_common K α α' with
    | ⟨γ, ⟨i, hi⟩, ⟨j, hj⟩⟩ => match stage_sq K γ with
      | ⟨F, hF⟩ =>
        let emb : A × C → B K γ := fun p => F (i (f p.1), j (g p.2))
        have he : Inj emb := fun p q e => by
          have e' := hF _ _ e
          have e1 : i (f p.1) = i (f q.1) := congrArg Prod.fst e'
          have e2 : j (g p.2) = j (g q.2) := congrArg Prod.snd e'
          exact Prod.ext (hf _ _ (hi _ _ e1)) (hg _ _ (hj _ _ e2))
        ⟨⟨fun p => ⟨γ, emb p⟩, fun p q e => he _ _ (stage_inj K γ _ _ e)⟩,
         fun ⟨k, hk⟩ => (strong_stage K γ).2 ⟨fun m => (fun z => z = emb (k m)), fun m m' e =>
           hk _ _ (he _ _ (single_inj _ _ e))⟩⟩

include K in
/-- ℕ is smaller than μ -/
theorem nat_small : Smaller Nat (mu K) :=
  match w1_ne K with
  | ⟨γ, _⟩ =>
    ⟨⟨fun k => ⟨γ, low K γ (nat_pn k)⟩, fun _ _ e => nat_pn_inj _ _ (low_inj K γ _ _ (stage_inj K γ _ _ e))⟩,
     fun ⟨k, hk⟩ => (strong_stage K γ).2 ⟨fun m => (fun z => z = low K γ (nat_pn (k m))), fun _ _ e =>
       hk _ _ (nat_pn_inj _ _ (low_inj K γ _ _ (single_inj _ _ e)))⟩⟩

/-- orbit labels from level, type and index -/
theorem orbitSmall_of_labels {α : (params K).Λ} (L : ∀ β, (params K).ltΛ β α → Lower (params K) β)
    (S : Addr (P := params K) α → Prop) (Typ C : Type) (hT : Smaller Typ (mu K)) (hC : Smaller C (mu K))
    (τ : ∀ β (h : (params K).ltΛ β α), (L β h).T → Typ) (n : ∀ β (h : (params K).ltΛ β α), (L β h).T → C)
    (orb : ∀ β h t t', τ β h t = τ β h t' → n β h t = n β h t' → Orb L S β h t t') :
    OrbitSmall L S :=
  ⟨Nat × (Typ × C), prod_small K _ _ (nat_small K) (prod_small K _ _ hT hC),
   fun β h t => (β, (τ β h t, n β h t)),
   fun _ _ _ _ _ _ e => congrArg Prod.fst e,
   fun β h t t' e => orb β h t t' (congrArg (fun p => p.2.1) e) (congrArg (fun p => p.2.2) e)⟩

end NFLb

end Found

#print axioms Found.NFLb.stage_common
#print axioms Found.NFLb.prod_small
#print axioms Found.NFLb.orbitSmall_of_labels

found/NFLift.lean

lean_all/found/NFLift.lean

/-
  T106 (b), slice N4c(vii). found/NFLift — in the classical point, the
  recursion step of the tangled-type construction modulo the f-map: given
  lower data at every β < α (found/NFStruct, with equality as an
  equivalence) and typed near-litters, the t-sets of level α
  (found/NFSupp) with
  * pointwise equivalence of extensions as their equality,
  * the allowable permutations of level α (found/NFStruct) acting on them
    (extended by the identity to the other structural permutations, decided
    in the classical point),
  * any f-map TSet α → μ respecting the equality,
  form lower data at level α: the next level of the recursion.  The f-map
  itself (an injection TSet α → μ, i.e. at most μ t-sets) is the remaining
  counting part of the construction.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.NFSupp

namespace Found

namespace NFLi

open CP WO Card NFP NFPO NFB NFS NFL NFA NFT NFU

def bcase {A : Sort _} : (b : Bool) → (b = true → A) → (b = false → A) → A
  | true, t, _ => t rfl
  | false, _, f => f rfl

theorem bcase_true {A : Sort _} : (b : Bool) → (t : b = true → A) → (f : b = false → A) → (h : b = true) →
    bcase b t f = t h
  | true, _, _, rfl => rfl

variable (K : Classical') {P : ParamsO} {α : P.Λ} (L : ∀ β, P.ltΛ β α → Lower P β)
  (Ty : ∀ γ (h : P.ltΛ γ α), Typed (L γ h))

/-- the action of a structural permutation: the allowable action, the identity otherwise -/
def lact (π : StrPerm P α) (X : TSet K L Ty) : TSet K L Ty :=
  bcase (dec K (Allowable L π)) (fun h => tact K L Ty π ((dec_spec K _).1 h) X) (fun _ => X)

theorem lact_eq (π : StrPerm P α) (hπ : Allowable L π) (X : TSet K L Ty) :
    lact K L Ty π X = tact K L Ty π hπ X :=
  bcase_true _ _ _ ((dec_spec K _).2 hπ)

/-- g agrees where f agrees (for base permutations) -/
theorem g_of_f (π ρ : BPerm P) (h : ∀ a, π.f a = ρ.f a) (a : Atom P) : π.g a = ρ.g a := by
  have e1 := h (ρ.g a)
  rw [ρ.fg] at e1
  have e2 := congrArg π.g e1
  rw [π.gf] at e2
  exact e2.symm

/-- allowability depends only on the base permutations along non-bottom paths -/
theorem allowable_off (σ σ' : StrPerm P α)
    (off : ∀ δ (h : P.ltΛ δ α) A a, (σ (.cons δ h A)).f a = (σ' (.cons δ h A)).f a)
    (hσ : Allowable L σ) : Allowable L σ' :=
  ⟨fun β h => (L β h).allow_ext (deriv P σ h) (deriv P σ' h) (fun A a => off β h A a) (hσ.1 β h),
   fun β hβ γ hγ ne t => by
    have e1 : (L β hβ).fuzz ((L β hβ).act (deriv P σ hβ) t) = (L β hβ).fuzz ((L β hβ).act (deriv P σ' hβ) t) :=
      (L β hβ).fuzz_resp ((L β hβ).act_ext _ _ (hσ.1 β hβ)
        ((L β hβ).allow_ext (deriv P σ hβ) (deriv P σ' hβ) (fun A a => off β hβ A a) (hσ.1 β hβ))
        (fun A a => off β hβ A a) t)
    have h0 := hσ.2 β hβ γ hγ ne t
    rw [e1] at h0
    exact congr_small P _ _ _ (fun a => by
      show litter P _ ((σ (.cons γ hγ (.bot γ))).g a) ↔ litter P _ ((σ' (.cons γ hγ (.bot γ))).g a)
      rw [g_of_f _ _ (fun b => off γ hγ (.bot γ) b) a]) h0⟩

/-- the next level: t-sets of level α as lower data -/
def lift (fz : TSet K L Ty → P.μ) (fz_resp : ∀ X Y : TSet K L Ty, PEq K L Ty X.val Y.val → fz X = fz Y) :
    Lower P α where
  T := TSet K L Ty
  eqv X Y := PEq K L Ty X.val Y.val
  eqv_refl _ := fun _ _ _ => Iff.rfl
  eqv_symm e := peq_symm K L Ty e
  eqv_trans e e' := peq_trans K L Ty e e'
  act := lact K L Ty
  Allow := Allowable L
  allow_one := allowable_one K L
  allow_mul := allowable_mul K L
  allow_inv := allowable_inv L
  allow_ext π ρ e hπ := allowable_off L π ρ (fun δ h A a => e (.cons δ h A) a) hπ
  act_resp π hπ {X Y} e := by
    rw [lact_eq K L Ty π hπ, lact_eq K L Ty π hπ]
    exact NFT.act_resp K L Ty π hπ _ _ e
  act_ext π ρ hπ hρ e X := by
    rw [lact_eq K L Ty π hπ, lact_eq K L Ty ρ hρ]
    exact act_congr K L Ty π ρ hπ hρ e X.val
  act_one X := by
    rw [lact_eq K L Ty _ (allowable_one K L)]
    exact NFT.act_one K L Ty X.val
  act_mul π ρ hπ hρ X := by
    rw [lact_eq K L Ty _ (allowable_mul K L π ρ hπ hρ), lact_eq K L Ty π hπ, lact_eq K L Ty ρ hρ]
    exact NFT.act_mul K L Ty π ρ hπ hρ X.val
  fuzz := fz
  fuzz_resp {X Y} e := fz_resp X Y e

end NFLi

end Found

#print axioms Found.NFLi.bcase_true
#print axioms Found.NFLi.lact_eq
#print axioms Found.NFLi.allowable_off
#print axioms Found.NFLi.lift

found/NFLiftBI.lean

lean_all/found/NFLiftBI.lean

/-
  T106 (b), slice N4d(viii). found/NFLiftBI — the lifted level (found/NFLift)
  satisfies the hypothesis of freedom of action one level up
  (found/NFAssemble): its allowable permutations and its action on t-sets
  do not depend on the base permutation at its own bottom path.  So the
  free-action step can be iterated along the levels.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.NFAssemble
import found.NFLift

namespace Found

namespace NFLB

open CP WO Card NFP NFPO NFB NFS NFL NFA NFT NFU NFLi NFAs

variable (K : Classical') {P : ParamsO} {α : P.Λ} (L : ∀ β, P.ltΛ β α → Lower P β)
  (Ty : ∀ γ (h : P.ltΛ γ α), Typed (L γ h))

/-- the action on pre-t-sets only sees non-bottom paths -/
theorem act_off (σ σ' : StrPerm P α) (hσ : Allowable L σ) (hσ' : Allowable L σ') (off : OffBot σ σ')
    (X : Pre K L Ty) : PEq K L Ty (act K L Ty σ hσ X) (act K L Ty σ' hσ' X) := fun γ h u =>
  E_iff K L Ty X γ h ((L γ h).act_ext (SInv P (deriv P σ h)) (SInv P (deriv P σ' h)) ((L γ h).allow_inv _ (hσ.1 γ h))
    ((L γ h).allow_inv _ (hσ'.1 γ h)) (fun A a => by
      show (σ (.cons γ h A)).g a = (σ' (.cons γ h A)).g a
      exact g_of_f _ _ (fun b => off γ h A b) a) u)

theorem lact_off (σ σ' : StrPerm P α) (off : OffBot σ σ') (hσ : Allowable L σ) (X : TSet K L Ty) :
    PEq K L Ty (lact K L Ty σ X).val (lact K L Ty σ' X).val := by
  rw [lact_eq K L Ty σ hσ, lact_eq K L Ty σ' (allowable_off L σ σ' off hσ)]
  exact act_off K L Ty σ σ' hσ (allowable_off L σ σ' off hσ) off X.val

/-- the lifted level is bottom-independent -/
theorem lift_botIndep (fz : TSet K L Ty → P.μ) (fz_resp : ∀ X Y : TSet K L Ty, PEq K L Ty X.val Y.val → fz X = fz Y) :
    BotIndep (lift K L Ty fz fz_resp) where
  allow σ σ' off hσ := allowable_off L σ σ' off hσ
  act σ σ' off hσ X := lact_off K L Ty σ σ' off hσ X

end NFLB

end Found

#print axioms Found.NFLB.act_off
#print axioms Found.NFLB.lift_botIndep

found/NFLitter.lean

lean_all/found/NFLitter.lean

/-
  T106 (b), slice N4c(iii). found/NFLitter — in the classical point, the
  action of base permutations on litters (con-nf):
  * two distinct litters are not near each other (their symmetric
    difference contains a copy of κ), so a set near a litter is near
    exactly one litter;
  * every base permutation π induces a map on litters π̂ with
    π[L_i] near L_{π̂ i}; π̂ respects identity and products, and π̂ of the
    inverse is a two-sided inverse: π̂ is a permutation of μ;
  * the coherence of allowable permutations (found/NFStruct), stated up to
    near-ness, holds exactly on litter indices: π̂_γ (fuzz t) = fuzz (π_β • t).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.NFStruct

namespace Found

namespace NFL

open CP WO Card NFP NFPO NFB NFS

variable (K : Classical') {P : ParamsO}

include K in
/-- distinct litters are not near: near litters are equal -/
theorem litter_near_eq (i j : P.μ) (h : Small P (SymDiff P (litter P i) (litter P j))) : i = j :=
  dne K _ (fun ne => h.2 ⟨fun k => ⟨(i, k), Or.inl ⟨rfl, fun e => ne e⟩⟩, fun k l e => by
    have e' := congrArg Subtype.val e
    have e2 : ((i, k) : Atom P).2 = ((i, l) : Atom P).2 := congrArg Prod.snd e'
    exact e2⟩)

include K in
/-- a set is near at most one litter -/
theorem near_unique (S : Atom P → Prop) (i j : P.μ) (hi : NearTo P S i) (hj : NearTo P S j) : i = j :=
  litter_near_eq K i j (trans_small K P _ _ _ (symm_small P _ _ hi) hj)

include K in
theorem near_ex (π : BPerm P) (i : P.μ) : ∃ j, NearTo P (img P π.g (litter P i)) j :=
  π.near _ (⟨i, small_empty K P _ (symm_self_empty P _)⟩ : NearLitter P (litter P i))

/-- the induced action on litters -/
def lmap (π : BPerm P) (i : P.μ) : P.μ := K.ch (fun j => NearTo P (img P π.g (litter P i)) j) (near_ex K π i)

theorem lmap_spec (π : BPerm P) (i : P.μ) : NearTo P (img P π.g (litter P i)) (lmap K π i) :=
  K.ch_spec (fun j => NearTo P (img P π.g (litter P i)) j) (near_ex K π i)

/-- characterisation: π̂ i is the unique litter near π[L_i] -/
theorem lmap_eq (π : BPerm P) (i j : P.μ) (h : NearTo P (img P π.g (litter P i)) j) : lmap K π i = j :=
  near_unique K _ _ _ (lmap_spec K π i) h

theorem lmap_one (i : P.μ) : lmap K (BPerm.one P) i = i :=
  lmap_eq K _ i i (small_empty K P _ (symm_self_empty P _))

theorem lmap_mul (π ρ : BPerm P) (i : P.μ) : lmap K (BPerm.mul P π ρ) i = lmap K ρ (lmap K π i) :=
  lmap_eq K _ i _ (trans_small K P _ _ _ (img_small P ρ _ _ (lmap_spec K π i)) (lmap_spec K ρ (lmap K π i)))

/-- pointwise equal base permutations induce the same litter map -/
theorem lmap_congr (π ρ : BPerm P) (h : ∀ a, π.g a = ρ.g a) (i : P.μ) : lmap K π i = lmap K ρ i :=
  lmap_eq K π i _ (congr_small P _ _ _ (fun a => by
    show litter P i (ρ.g a) ↔ litter P i (π.g a)
    rw [h a]) (lmap_spec K ρ i))

theorem lmap_inv_left (π : BPerm P) (i : P.μ) : lmap K (BPerm.inv P π) (lmap K π i) = i := by
  rw [← lmap_mul]
  exact (lmap_congr K _ _ (fun a => π.gf a) i).trans (lmap_one K i)

theorem lmap_inv_right (π : BPerm P) (i : P.μ) : lmap K π (lmap K (BPerm.inv P π) i) = i := by
  rw [← lmap_mul]
  exact (lmap_congr K _ _ (fun a => π.fg a) i).trans (lmap_one K i)

/-- the near coherence of allowable permutations is exact on litter indices -/
theorem coherence_exact {α : P.Λ} (L : ∀ β, P.ltΛ β α → Lower P β) (π : StrPerm P α) (hπ : Allowable L π)
    (β : P.Λ) (hβ : P.ltΛ β α) (γ : P.Λ) (hγ : P.ltΛ γ α) (ne : β ≠ γ) (t : (L β hβ).T) :
    lmap K (atLit π hγ) ((L β hβ).fuzz t) = (L β hβ).fuzz ((L β hβ).act (deriv P π hβ) t) :=
  lmap_eq K _ _ _ (hπ.2 β hβ γ hγ ne t)

end NFL

end Found

#print axioms Found.NFL.litter_near_eq
#print axioms Found.NFL.near_unique
#print axioms Found.NFL.lmap_spec
#print axioms Found.NFL.lmap_one
#print axioms Found.NFL.lmap_mul
#print axioms Found.NFL.lmap_congr
#print axioms Found.NFL.lmap_inv_left
#print axioms Found.NFL.lmap_inv_right
#print axioms Found.NFL.coherence_exact

found/NFNear.lean

lean_all/found/NFNear.lean

/-
  T106 (b), slice N4d(i). found/NFNear — in the classical point, over a
  parameter value with ℕ ↪ μ: there are at most μ near-litters.  A
  near-litter N is coded by its litter N° and the small set N Δ L_{N°};
  equal codes give pointwise equivalent near-litters.  (A counting
  ingredient of |TSet α| ≤ μ.)

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.NFAlt
import found.NFCode

namespace Found

namespace NFN

open CP WO Card NFP NFPO NFB NFS NFL NFA NFC NFD

variable (K : Classical') {P : ParamsO} {e : Nat → P.μ} (he : Inj e)

/-- the symmetric difference with the associated litter, as a small set -/
def diffSet (N : NLit P) : SSet P (Atom P) where
  S a := (N.val a ∧ ¬ litter P (litterOf K N) a) ∨ (litter P (litterOf K N) a ∧ ¬ N.val a)
  small := litterOf_spec K N

def nlCode (N : NLit P) : P.μ := pair K P he (litterOf K N, codeA K P he (diffSet K N))

include K in
/-- recover a set from a litter and the symmetric difference -/
theorem recover (S S' Lt : Atom P → Prop)
    (hd : ∀ a, ((S a ∧ ¬ Lt a) ∨ (Lt a ∧ ¬ S a)) ↔ ((S' a ∧ ¬ Lt a) ∨ (Lt a ∧ ¬ S' a))) (a : Atom P) :
    S a ↔ S' a := by
  have h := hd a
  constructor
  · intro hs
    refine (K.lem (Lt a)).elim (fun hl => dne K _ (fun hn => ?_)) (fun hl => ?_)
    · exact (h.2 (Or.inr ⟨hl, hn⟩)).elim (fun x => x.2 hl) (fun x => x.2 hs)
    · exact (h.1 (Or.inl ⟨hs, hl⟩)).elim (fun x => x.1) (fun x => (hl x.1).elim)
  · intro hs
    refine (K.lem (Lt a)).elim (fun hl => dne K _ (fun hn => ?_)) (fun hl => ?_)
    · exact (h.1 (Or.inr ⟨hl, hn⟩)).elim (fun x => x.2 hl) (fun x => x.2 hs)
    · exact (h.2 (Or.inl ⟨hs, hl⟩)).elim (fun x => x.1) (fun x => (hl x.1).elim)

/-- near-litters inject into μ, up to pointwise equivalence -/
theorem nlCode_inj (N N' : NLit P) (h : nlCode K he N = nlCode K he N') : ∀ a, N.val a ↔ N'.val a := by
  have hp := pair_inj K P he _ _ h
  have h1' := congrArg Prod.fst hp
  have h2' := congrArg Prod.snd hp
  have hl : litterOf K N = litterOf K N' := h1'
  have hd := codeA_inj K P he _ _ h2'
  refine recover K N.val N'.val (litter P (litterOf K N)) (fun a => ?_)
  have this : (N.val a ∧ ¬ litter P (litterOf K N) a ∨ litter P (litterOf K N) a ∧ ¬ N.val a) ↔
      (N'.val a ∧ ¬ litter P (litterOf K N') a ∨ litter P (litterOf K N') a ∧ ¬ N'.val a) := hd a
  rw [← hl] at this
  exact this

end NFN

end Found

#print axioms Found.NFN.recover
#print axioms Found.NFN.nlCode_inj

found/NFOrbSeg.lean

lean_all/found/NFOrbSeg.lean

/-
  T106 (b), slice N4d(xi). found/NFOrbSeg — in the classical point, the
  orbit bound (found/NFOrbit) reduces to labelling orbits inside one
  segment of μ: if the lower tangles of all levels are labelled by elements
  {x < m} so that equal labels mean same level and same orbit of the
  S-fixing allowable permutations, then the orbit bound holds for S (the
  power set of the segment is coded into μ, found/NFPow).  In particular
  it holds when the labels form a small set.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.NFOrbit
import found.NFPow

namespace Found

namespace NFOS

open CP WO Card NFP NFPO NFB NFS NFL NFA NFT NFU NFO NFPw

variable (K : Classical') {P : ParamsO} {α : P.Λ} (L : ∀ β, P.ltΛ β α → Lower P β)

/-- orbit labels in a segment of μ -/
def SegLabels (S : Addr (P := P) α → Prop) : Prop :=
  ∃ m, ∃ r : ∀ β (h : P.ltΛ β α), (L β h).T → {x // P.ltμ x m},
    (∀ β h t β' h' t', r β h t = r β' h' t' → β = β') ∧ (∀ β h t t', r β h t = r β h t' → Orb L S β h t t')

theorem orbit_of_seg (S : Addr (P := P) α → Prop) (h : SegLabels L S) : OrbitBound L S :=
  match h with
  | ⟨m, r, disj, refl⟩ => match pow_seg P {x // P.ltμ x m} m (fun x => x) (fun _ _ e => e) with
    | ⟨c, hc⟩ => ⟨{x // P.ltμ x m}, r, disj, refl, c, hc⟩

/-- labels in a small type also suffice -/
def SmallLabels (S : Addr (P := P) α → Prop) : Prop :=
  ∃ R : Type, Small P R ∧ ∃ r : ∀ β (h : P.ltΛ β α), (L β h).T → R,
    (∀ β h t β' h' t', r β h t = r β' h' t' → β = β') ∧ (∀ β h t t', r β h t = r β h t' → Orb L S β h t t')

include K in
theorem orbit_of_small (S : Addr (P := P) α → Prop) (h : SmallLabels L S) : OrbitBound L S :=
  match h with
  | ⟨R, hR, r, disj, refl⟩ => match pow_small K P R hR with
    | ⟨c, hc⟩ => ⟨R, r, disj, refl, c, hc⟩

end NFOS

end Found

#print axioms Found.NFOS.orbit_of_seg
#print axioms Found.NFOS.orbit_of_small

found/NFOrbit.lean

lean_all/found/NFOrbit.lean

/-
  T106 (b), slice N4d(iii). found/NFOrbit — in the classical point, the
  per-support bound (found/NFReduce) reduces to an orbit bound:
  * a t-set supported by S has extensions invariant under every allowable
    permutation fixing S (they are unions of orbits);
  * if the orbits of the lower tangles under the S-fixing allowable
    permutations have a set of labels R (labels of different levels are
    different) whose power set is coded into μ (injectively up to ↔), then
    the t-sets supported by S inject into μ: a t-set is determined by the
    set of labels of orbits meeting its extensions; the code is made
    extensional by an extensional choice of a canonical representative.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.NFReduce

namespace Found

namespace NFO

open CP WO Card NFP NFPO NFB NFS NFL NFA NFT NFU NFR NFC

variable (K : Classical') {P : ParamsO} {α : P.Λ}
  (L : ∀ β, P.ltΛ β α → Lower P β) (Ty : ∀ γ (h : P.ltΛ γ α), Typed (L γ h))

/-- the orbit relation of S-fixing allowable permutations on β-tangles -/
def Orb (S : Addr (P := P) α → Prop) (β : P.Λ) (h : P.ltΛ β α) (t t' : (L β h).T) : Prop :=
  ∃ ρ, ∃ _ : Allowable L ρ, (∀ s, S s → actA ρ s = s) ∧ (L β h).eqv ((L β h).act (deriv P ρ h) t) t'

/-- extensions of a supported t-set are unions of orbits -/
theorem invariant (Y : Pre K L Ty) (S : Addr (P := P) α → Prop) (hY : SuppT K L Ty Y S)
    (β : P.Λ) (h : P.ltΛ β α) (t t' : (L β h).T) (ho : Orb L S β h t t') (ht : Y.E β h t) : Y.E β h t' :=
  match ho with
  | ⟨ρ, hρ, fix, e⟩ =>
    have hd := hρ.1 β h
    have p := (hY.2 ρ hρ fix β h ((L β h).act (deriv P ρ h) t)).1
      (Y.resp β h _ _ ((L β h).eqv_symm (act_inv_l (L β h) _ hd t)) ht)
    Y.resp β h _ _ e p

/-- the orbit bound for a support S -/
def OrbitBound (S : Addr (P := P) α → Prop) : Prop :=
  ∃ (R : Type) (r : ∀ β (h : P.ltΛ β α), (L β h).T → R),
    (∀ β h t β' h' t', r β h t = r β' h' t' → β = β') ∧
    (∀ β h t t', r β h t = r β h t' → Orb L S β h t t') ∧
    ∃ c : (R → Prop) → P.μ, ∀ Q Q', c Q = c Q' → ∀ q, Q q ↔ Q' q

/-- an extensional canonical representative of a predicate -/
def can {R : Type} (Q : R → Prop) : R → Prop := K.ch (fun Q' : R → Prop => ∀ q, Q' q ↔ Q q) ⟨Q, fun _ => Iff.rfl⟩

theorem can_spec {R : Type} (Q : R → Prop) : ∀ q, can K Q q ↔ Q q :=
  K.ch_spec (fun Q' : R → Prop => ∀ q, Q' q ↔ Q q) ⟨Q, fun _ => Iff.rfl⟩

theorem can_ext {R : Type} (Q Q' : R → Prop) (h : ∀ q, Q q ↔ Q' q) : can K Q = can K Q' :=
  K.ch_ext _ _ _ _ (fun _ => ⟨fun x q => (x q).trans (h q), fun x q => (x q).trans (h q).symm⟩)

/-- the orbit bound gives the per-support bound -/
theorem bound_of_orbits (hO : ∀ B : SSet P (Addr (P := P) α), OrbitBound L B.S) : Bound K L Ty := fun B =>
  match hO B with
  | ⟨R, r, disj, refl, c, hc⟩ =>
    let lab : Pre K L Ty → R → Prop := fun X q => ∃ β h t, r β h t = q ∧ X.E β h t
    ⟨fun X => c (can K (lab X.val)),
     fun X Y e => congrArg c (can_ext K _ _ (fun q =>
       ⟨fun ⟨β, h, t, hq, ht⟩ => ⟨β, h, t, hq, (e β h t).1 ht⟩,
        fun ⟨β, h, t, hq, ht⟩ => ⟨β, h, t, hq, (e β h t).2 ht⟩⟩)),
     fun X Y eq => by
       have hQ := hc _ _ eq
       have hl : ∀ q, lab X.val q ↔ lab Y.val q := fun q =>
         ((can_spec K _ q).symm.trans (hQ q)).trans (can_spec K _ q)
       intro β h t
       constructor
       · intro ht
         match (hl _).1 ⟨β, h, t, rfl, ht⟩ with
         | ⟨β', h', t', hq, ht'⟩ =>
           have eb := disj _ _ _ _ _ _ hq
           subst eb
           exact invariant K L Ty Y.val B.S Y.property _ _ t' t (refl _ _ _ _ hq) ht'
       · intro ht
         match (hl _).2 ⟨β, h, t, rfl, ht⟩ with
         | ⟨β', h', t', hq, ht'⟩ =>
           have eb := disj _ _ _ _ _ _ hq
           subst eb
           exact invariant K L Ty X.val B.S X.property _ _ t' t (refl _ _ _ _ hq) ht'⟩

end NFO

end Found

#print axioms Found.NFO.invariant
#print axioms Found.NFO.can_spec
#print axioms Found.NFO.can_ext
#print axioms Found.NFO.bound_of_orbits

found/NFParams.lean

lean_all/found/NFParams.lean

/-
  T106 (b), slice N3b. found/NFParams — the cardinal parameters of Holmes'
  model of NF (as in the con-nf formalization), stated inside the classical
  point:
  * strict cardinal order Smaller A B (an injection A → B and none back),
    transitive and irreflexive, with Cantor's theorem A < (A → Prop);
  * regular κ (small subsets are bounded), strong limit μ (power sets of
    proper initial segments stay below μ), and the parameter interface
    (Λ without a maximum, #Λ < κ, κ < μ, cofinality of μ at least κ).
  The existence of such parameters and the tangle construction on them are
  the next slices (N4, N5).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Cardinal
import found.NoSelfVerify

namespace Found

namespace NFP

open CP WO Card

def Smaller (A B : Type) : Prop := (∃ f : A → B, Inj f) ∧ ¬ ∃ g : B → A, Inj g

theorem smaller_irrefl (A : Type) : ¬ Smaller A A := fun ⟨_, h⟩ => h ⟨id, fun _ _ e => e⟩

theorem smaller_trans {A B C : Type} (h1 : Smaller A B) (h2 : Smaller B C) : Smaller A C :=
  match h1, h2 with
  | ⟨⟨f, hf⟩, _⟩, ⟨⟨g, hg⟩, hn⟩ => ⟨⟨fun a => g (f a), fun _ _ e => hf _ _ (hg _ _ e)⟩,
      fun ⟨k, hk⟩ => hn ⟨fun c => f (k c), fun _ _ e => hk _ _ (hf _ _ e)⟩⟩

/-- Cantor: every type is smaller than its power type -/
theorem cantor (A : Type) : Smaller A (A → Prop) :=
  ⟨⟨fun x y => y = x, fun x _ e => Eq.mp (congrArg (fun P : A → Prop => P x) e) rfl⟩,
   fun ⟨g, hg⟩ => NoSelfVerify.cantor_inj g hg⟩

variable (K : Classical')

/-- the proper initial segment below m -/
def Seg {μ : Type} (m : μ) : Type := {x : μ // Lt' K x m}

/-- κ is regular: every subset smaller than κ is bounded -/
def Regular (κ : Type) : Prop :=
  ∀ S : κ → Prop, Smaller {x // S x} κ → ∃ b : κ, ∀ x, S x → Lt' K x b

/-- μ is a strong limit: power sets of proper initial segments stay below μ -/
def StrongLimit (μ : Type) : Prop := ∀ m : μ, Smaller (Seg K m → Prop) μ

/-- cofinality of μ is at least κ: subsets of μ smaller than κ are bounded -/
def CofGe (μ κ : Type) : Prop := ∀ S : μ → Prop, Smaller {x // S x} κ → ∃ b : μ, ∀ x, S x → Lt' K x b

/-- the parameters of the construction -/
structure Params where
  Λ : Type
  κ : Type
  μ : Type
  noMax : ∀ i : Λ, ∃ j, Lt' K i j
  Λ_small : Smaller Λ κ
  κ_regular : Regular K κ
  κ_lt_μ : Smaller κ μ
  μ_strong : StrongLimit K μ
  μ_cof : CofGe K μ κ

end NFP

end Found

#print axioms Found.NFP.smaller_trans
#print axioms Found.NFP.cantor

found/NFParamsO.lean

lean_all/found/NFParamsO.lean

/-
  T106 (b), slice N3d. found/NFParamsO — a value of the NF-model parameters
  inside the classical point, with the orders given explicitly (the
  Zermelo order Lt' of found/Cardinal is arbitrary on each type, so the
  parameter structure takes its orders as data):
  * Λ = ℕ with <, countable without a largest element;
  * κ = ω₁ (found/Omega1) with its inherited order: regular;
  * μ = ℶ_{ω₁} = Σ α, B α (found/BethStages) with the lexicographic order:
    well-founded, κ < μ, cofinality ≥ κ, and a strong limit in the form
    "each initial segment injects into a stage whose power set is smaller
    than μ" (power types compared by Eq-injections need funext otherwise).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Omega1Sub

namespace Found

namespace NFPO

open CP WO Card NFP W1 CU BethS

structure ParamsO where
  Λ : Type
  κ : Type
  μ : Type
  ltΛ : Λ → Λ → Prop
  ltκ : κ → κ → Prop
  ltμ : μ → μ → Prop
  wfΛ : WellFounded ltΛ
  wfκ : WellFounded ltκ
  wfμ : WellFounded ltμ
  linκ : ∀ x y, ltκ x y ∨ x = y ∨ ltκ y x
  noMax : ∀ i : Λ, ∃ j, ltΛ i j
  Λ_small : Smaller Λ κ
  κ_regular : ∀ S : κ → Prop, Smaller {x // S x} κ → ∃ b, ∀ x, S x → ltκ x b
  κ_lt_μ : Smaller κ μ
  μ_strong : ∀ m : μ, ∃ B : Type, (∃ f : {x // ltμ x m} → B, Inj f) ∧ Smaller (B → Prop) μ
  μ_cof : ∀ S : μ → Prop, Smaller {x // S x} κ → ∃ b, ∀ x, S x → ltμ x b

variable (K : Classical')

/-! ## sizes below ω₁ -/

include K in
/-- every uncountable type receives an injection from ω₁ -/
theorem uncount_inj (S : Type) (hS : ¬ Countable S) : ∃ f : W1 K → S, Inj f :=
  match comparable K (α := S) (β := W1 K) with
  | Or.inr h => h
  | Or.inl ⟨f, hf⟩ =>
    let Im : W1 K → Prop := fun y => ∃ s, f s = y
    have hIm : ¬ Countable {y // Im y} := fun ⟨g, hg⟩ =>
      hS ⟨fun s => g ⟨f s, s, rfl⟩, fun a b e => hf _ _ (congrArg Subtype.val (hg _ _ e))⟩
    match W1Sub.omega1_sub K Im hIm with
    | ⟨h, hh⟩ =>
      ⟨fun x => K.ch (fun s => f s = (h x).val) (h x).property, fun x y e => by
        have hx := K.ch_spec (fun s => f s = (h x).val) (h x).property
        have hy := K.ch_spec (fun s => f s = (h y).val) (h y).property
        have e' : K.ch (fun s => f s = (h x).val) (h x).property = K.ch (fun s => f s = (h y).val) (h y).property := e
        rw [e'] at hx
        exact hh _ _ (Subtype.ext (hx.symm.trans hy))⟩

include K in
theorem countable_of_smaller (S : Type) (h : Smaller S (W1 K)) : Countable S :=
  dne K _ (fun hn => h.2 (uncount_inj K S hn))

/-! ## the order of ω₁ -/

theorem lt_trans {a b c : W1 K} (h1 : Lt K a b) (h2 : Lt K b c) : Lt K a c :=
  ⟨le_trans K h1.1 h2.1, fun hca => h1.2 (le_trans K h2.1 hca)⟩

theorem lt_irrefl (a : W1 K) : ¬ Lt K a a := fun h => h.2 h.1

theorem linW (x y : W1 K) : Lt K x y ∨ x = y ∨ Lt K y x :=
  (K.lem (x = y)).elim (fun e => Or.inr (Or.inl e)) (fun ne =>
    (le_total K x.val y.val).elim
      (fun h => Or.inl ⟨h, fun h' => ne (sub_eq K (le_antisymm K _ _ h h'))⟩)
      (fun h => Or.inr (Or.inr ⟨h, fun h' => ne (sub_eq K (le_antisymm K _ _ h' h))⟩)))

/-! ## a countable chain in ω₁ -/

def chainW : Nat → W1 K
  | 0 => K.ch (fun _ => True) (w1_ne K)
  | n + 1 => K.ch (fun a => Lt K (chainW n) a) (no_max K (chainW n))

theorem chain_step (n : Nat) : Lt K (chainW K n) (chainW K (n + 1)) :=
  K.ch_spec (fun a => Lt K (chainW K n) a) (no_max K (chainW K n))

theorem chain_lt (m : Nat) : (k : Nat) → Lt K (chainW K m) (chainW K (m + k + 1))
  | 0 => chain_step K m
  | k + 1 => lt_trans K (chain_lt m k) (chain_step K (m + k + 1))

theorem le_ex : {a b : Nat} → a ≤ b → ∃ r, b = a + r
  | _, _, .refl => ⟨0, rfl⟩
  | _, _, .step h => match le_ex h with
    | ⟨r, e⟩ => ⟨r + 1, by rw [e]; rfl⟩

theorem lt_ex {m n : Nat} (h : m < n) : ∃ k, n = m + k + 1 :=
  match le_ex h with
  | ⟨k, e⟩ => ⟨k, e.trans (Nat.succ_add m k)⟩

theorem chain_inj : Inj (chainW K) := fun m n e => by
  rcases Nat.lt_or_ge m n with h | h
  · obtain ⟨k, hk⟩ := lt_ex h
    have := chain_lt K m k
    rw [← hk, e] at this
    exact (lt_irrefl K _ this).elim
  · rcases Nat.eq_or_lt_of_le h with h' | h'
    · exact h'.symm
    · obtain ⟨k, hk⟩ := lt_ex h'
      have := chain_lt K n k
      rw [← hk, e] at this
      exact (lt_irrefl K _ this).elim

/-! ## the lexicographic order on μ -/

def LexLt (p q : mu K) : Prop :=
  Lt K p.1 q.1 ∨ ∃ e : p.1 = q.1, Lt' K (cast (congrArg (B K) e) p.2) q.2

theorem lex_acc (α : W1 K) (hα : Acc (Lt K) α) : ∀ x : B K α, Acc (LexLt K) ⟨α, x⟩ := by
  induction hα with
  | intro α _ ihα =>
    intro x
    have hx := (wf K (B K α)).apply x
    induction hx with
    | intro x _ ihx =>
      constructor
      intro q hq
      cases q with
      | mk β z =>
        rcases hq with h | ⟨e, h⟩
        · exact ihα β h z
        · have e' : β = α := e
          subst e'
          exact ihx z h

theorem wfLex : WellFounded (LexLt K) := ⟨fun p => lex_acc K p.1 ((wfW K).apply p.1) p.2⟩

def pn0 : PN := fun _ => True

/-! ## the strong limit property for initial segments -/

theorem sing_eq (β β' : W1 K) (y : B K β) (y' : B K β') (e : β = β')
    (hh : HEq (fun z : B K β => z = y) (fun z : B K β' => z = y')) : (⟨β, y⟩ : mu K) = ⟨β', y'⟩ := by
  subst e
  have hP := eq_of_heq hh
  have hy : y = y' := cast (congrFun hP y) rfl
  rw [hy]

theorem up_inj2 (α β β' : W1 K) (h : Lt K β α) (h' : Lt K β' α) (y : B K β) (y' : B K β')
    (e : up K α β h (fun z => z = y) = up K α β' h' (fun z => z = y')) : (⟨β, y⟩ : mu K) = ⟨β', y'⟩ := by
  have e1 := cast_inj _ e
  have e2 : (⟨⟨β, h⟩, fun z => z = y⟩ : (γ : {γ : W1 K // Lt K γ α}) × (B K γ.val → Prop)) = ⟨⟨β', h'⟩, fun z => z = y'⟩ :=
    Sum.inr.inj e1
  have e3 := Sigma.mk.inj e2
  exact sing_eq K β β' y y' (congrArg Subtype.val e3.1) e3.2

theorem seg_below (m : mu K) (α' : W1 K) (hα : Lt K m.1 α') (y : mu K) (hy : LexLt K y m) : Lt K y.1 α' :=
  hy.elim (fun h => lt_trans K h hα) (fun ⟨e, _⟩ => e ▸ hα)

theorem strong (m : mu K) : ∃ C : Type, (∃ f : {x // LexLt K x m} → C, Inj f) ∧ Smaller (C → Prop) (mu K) :=
  match no_max K m.1 with
  | ⟨α', hα⟩ => ⟨B K α',
      ⟨fun y => up K α' y.val.1 (seg_below K m α' hα y.val y.property) (fun z => z = y.val.2),
       fun _ _ e => Subtype.ext (up_inj2 K α' _ _ _ _ _ _ e)⟩,
      strong_stage K α'⟩

/-! ## the parameter value -/

include K in
theorem omega1_regular' (S : W1 K → Prop) (h : Smaller {x // S x} (W1 K)) : ∃ b, ∀ x, S x → Lt K x b :=
  match bounded K {x // S x} (countable_of_smaller K _ h) (fun x => x.val) with
  | ⟨b, hb⟩ => ⟨b, fun x hx => hb ⟨x, hx⟩⟩

theorem kappa_lt_mu : Smaller (W1 K) (mu K) :=
  ⟨⟨fun α => ⟨α, low K α pn0⟩, fun _ _ e => congrArg Sigma.fst e⟩,
   fun ⟨g, hg⟩ => match w1_ne K with
    | ⟨α0, _⟩ => match strong_stage K α0 with
      | ⟨⟨f, hf⟩, _⟩ => (cantor (B K α0)).2 ⟨fun P => low K α0 (g (f P)).val,
          fun _ _ e => hf _ _ (hg _ _ (Subtype.ext (low_inj K α0 _ _ e)))⟩⟩

include K in
theorem mu_cof (S : mu K → Prop) (h : Smaller {x // S x} (W1 K)) : ∃ b, ∀ x, S x → LexLt K x b :=
  match bounded K {x // S x} (countable_of_smaller K _ h) (fun x => x.val.1) with
  | ⟨b, hb⟩ => ⟨⟨b, low K b pn0⟩, fun x hx => Or.inl (hb ⟨x, hx⟩)⟩

/-- the parameters exist in the classical point -/
def params : ParamsO where
  Λ := Nat
  κ := W1 K
  μ := mu K
  ltΛ a b := a < b
  ltκ := Lt K
  ltμ := LexLt K
  wfΛ := Nat.lt_wfRel.wf
  wfκ := wfW K
  wfμ := wfLex K
  linκ := linW K
  noMax i := ⟨i + 1, Nat.lt_succ_self i⟩
  Λ_small := ⟨⟨chainW K, chain_inj K⟩, uncountable K⟩
  κ_regular := omega1_regular' K
  κ_lt_μ := kappa_lt_mu K
  μ_strong := strong K
  μ_cof := mu_cof K

end NFPO

end Found

#print axioms Found.NFPO.uncount_inj
#print axioms Found.NFPO.countable_of_smaller
#print axioms Found.NFPO.linW
#print axioms Found.NFPO.chain_inj
#print axioms Found.NFPO.wfLex
#print axioms Found.NFPO.up_inj2
#print axioms Found.NFPO.strong
#print axioms Found.NFPO.omega1_regular'
#print axioms Found.NFPO.kappa_lt_mu
#print axioms Found.NFPO.mu_cof
#print axioms Found.NFPO.params

found/NFPow.lean

lean_all/found/NFPow.lean

/-
  T106 (b), slice N4d(x). found/NFPow — in the classical point, power sets
  of small types are coded into μ (the coding part of the orbit bound,
  found/NFOrbit):
  * a type injecting into a segment {x < m} of μ has its power set coded
    into μ, injectively up to ↔ (strong limit);
  * every small type (< κ) injects into some segment of μ (κ < μ, and
    cof μ ≥ κ bounds the image);
  * hence the power set of every small type is coded into μ.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.NFCount

namespace Found

namespace NFPw

open CP WO Card NFP NFPO NFB NFS NFC

variable (K : Classical') (P : ParamsO)

/-- power sets of types below a segment of μ are coded into μ -/
theorem pow_seg (R : Type) (m : P.μ) (f : R → {x // P.ltμ x m}) (hf : Inj f) :
    ∃ c : (R → Prop) → P.μ, ∀ Q Q', c Q = c Q' → ∀ r, Q r ↔ Q' r :=
  match seg_code P m with
  | ⟨g, hg⟩ => ⟨fun Q => g (fun s => ∃ r, f r = s ∧ Q r), fun _ _ e r =>
      ⟨fun hq => match (hg _ _ e (f r)).1 ⟨r, rfl, hq⟩ with
        | ⟨_, hr', hq'⟩ => (hf _ _ hr') ▸ hq',
       fun hq => match (hg _ _ e (f r)).2 ⟨r, rfl, hq⟩ with
        | ⟨_, hr', hq'⟩ => (hf _ _ hr') ▸ hq'⟩⟩

include K in
/-- every small type lies below a segment of μ -/
theorem small_seg (R : Type) (hR : Small P R) : ∃ m, ∃ f : R → {x // P.ltμ x m}, Inj f :=
  match hR.1, P.κ_lt_μ.1 with
  | ⟨a, ha⟩, ⟨b, hb⟩ =>
    let i : R → P.μ := fun r => b (a r)
    have hi : Inj i := fun r r' e => ha _ _ (hb _ _ e)
    let S : P.μ → Prop := fun x => ∃ r, i r = x
    have sS : Smaller {x // S x} P.κ := ⟨⟨fun x => a (K.ch (fun r => i r = x.val) x.property), fun x y e => by
        have e1 := ha _ _ e
        have hx := K.ch_spec (fun r => i r = x.val) x.property
        have hy := K.ch_spec (fun r => i r = y.val) y.property
        rw [e1] at hx
        exact Subtype.ext (hx.symm.trans hy)⟩,
      fun ⟨k, hk⟩ => hR.2 ⟨fun z => K.ch (fun r => i r = (k z).val) (k z).property, fun z w e => hk _ _ (Subtype.ext (by
        have hz := K.ch_spec (fun r => i r = (k z).val) (k z).property
        have hw := K.ch_spec (fun r => i r = (k w).val) (k w).property
        have e' : K.ch (fun r => i r = (k z).val) (k z).property = K.ch (fun r => i r = (k w).val) (k w).property := e
        rw [e'] at hz
        exact hz.symm.trans hw))⟩⟩
    match P.μ_cof S sS with
    | ⟨m, hm⟩ => ⟨m, fun r => ⟨i r, hm _ ⟨r, rfl⟩⟩, fun r r' e => hi _ _ (congrArg Subtype.val e)⟩

include K in
/-- power sets of small types are coded into μ -/
theorem pow_small (R : Type) (hR : Small P R) : ∃ c : (R → Prop) → P.μ, ∀ Q Q', c Q = c Q' → ∀ r, Q r ↔ Q' r :=
  match small_seg K P R hR with
  | ⟨m, f, hf⟩ => pow_seg P R m f hf

end NFPw

end Found

#print axioms Found.NFPw.pow_seg
#print axioms Found.NFPw.small_seg
#print axioms Found.NFPw.pow_small

found/NFReduce.lean

lean_all/found/NFReduce.lean

/-
  T106 (b), slice N4d(ii). found/NFReduce — in the classical point, the
  f-map at level α reduces to a bound per support: if for every support S
  the t-sets supported by S inject into μ (up to pointwise equivalence),
  then all t-sets of level α inject into μ, by a map respecting their
  equality — exactly the f-map required by found/NFLift.  The support of a
  t-set is chosen extensionally (equivalent t-sets get the same support),
  and canonically up to its code in μ (found/NFCode).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.NFLift
import found.NFCode

namespace Found

namespace NFR

open CP WO Card NFP NFPO NFB NFS NFL NFA NFT NFU NFC NFD

variable (K : Classical') {P : ParamsO} {e : Nat → P.μ} (he : Inj e) {α : P.Λ}
  (L : ∀ β, P.ltΛ β α → Lower P β) (Ty : ∀ γ (h : P.ltΛ γ α), Typed (L γ h))

theorem supp_congr (X : Pre K L Ty) (S S' : Addr (P := P) α → Prop) (h : ∀ s, S s ↔ S' s)
    (hs : SuppT K L Ty X S) : SuppT K L Ty X S' :=
  ⟨small_of_inj P (fun s => (⟨s.val, (h s.val).2 s.property⟩ : {s // S s}))
    (fun s t eq => Subtype.ext (by have e' := congrArg Subtype.val eq; exact e')) hs.1,
   fun ρ hρ fix => hs.2 ρ hρ (fun s hs' => fix s ((h s).1 hs'))⟩

theorem supp_peq (X Y : Pre K L Ty) (e : PEq K L Ty X Y) (S : Addr (P := P) α → Prop)
    (hs : SuppT K L Ty X S) : SuppT K L Ty Y S :=
  ⟨hs.1, fun ρ hρ fix => peq_trans K L Ty (NFT.act_resp K L Ty ρ hρ _ _ (peq_symm K L Ty e))
    (peq_trans K L Ty (hs.2 ρ hρ fix) e)⟩

/-- the per-support bound -/
def Bound : Prop := ∀ B : SSet P (Addr (P := P) α), ∃ g : {X : Pre K L Ty // SuppT K L Ty X B.S} → P.μ,
  (∀ X Y, PEq K L Ty X.val Y.val → g X = g Y) ∧ (∀ X Y, g X = g Y → PEq K L Ty X.val Y.val)

variable (hB : Bound K L Ty)

def gsel (B : SSet P (Addr (P := P) α)) : {X : Pre K L Ty // SuppT K L Ty X B.S} → P.μ :=
  K.ch _ (hB B)

theorem gsel_spec (B : SSet P (Addr (P := P) α)) :
    (∀ X Y, PEq K L Ty X.val Y.val → gsel K L Ty hB B X = gsel K L Ty hB B Y) ∧
    (∀ X Y, gsel K L Ty hB B X = gsel K L Ty hB B Y → PEq K L Ty X.val Y.val) :=
  K.ch_spec _ (hB B)

theorem supp_ex (X : TSet K L Ty) : ∃ B : SSet P (Addr (P := P) α), SuppT K L Ty X.val B.S :=
  match X.property with
  | ⟨S, hS⟩ => ⟨⟨S, hS.1⟩, hS⟩

/-- the chosen support -/
def supp (X : TSet K L Ty) : SSet P (Addr (P := P) α) := K.ch _ (supp_ex K L Ty X)

/-- the canonical support with the same code -/
def rep (X : TSet K L Ty) : SSet P (Addr (P := P) α) :=
  K.ch (fun B => suppCode K P he B = suppCode K P he (supp K L Ty X)) ⟨_, rfl⟩

theorem rep_supp (X : TSet K L Ty) : SuppT K L Ty X.val (rep K he L Ty X).S :=
  supp_congr K L Ty X.val _ _
    (fun s => (suppCode_inj K P he α _ _
      (K.ch_spec (fun B => suppCode K P he B = suppCode K P he (supp K L Ty X)) ⟨_, rfl⟩).symm s))
    (K.ch_spec _ (supp_ex K L Ty X))

/-- the f-map -/
def fmap (X : TSet K L Ty) : P.μ :=
  pair K P he (suppCode K P he (supp K L Ty X), gsel K L Ty hB (rep K he L Ty X) ⟨X.val, rep_supp K he L Ty X⟩)

theorem rep_eq (X Y : TSet K L Ty) (h : suppCode K P he (supp K L Ty X) = suppCode K P he (supp K L Ty Y)) :
    rep K he L Ty X = rep K he L Ty Y :=
  K.ch_ext _ _ _ _ (fun B => by rw [h])

theorem gsel_transport (B B' : SSet P (Addr (P := P) α)) (eB : B = B') (X Y : Pre K L Ty)
    (hx : SuppT K L Ty X B.S) (hy : SuppT K L Ty Y B'.S) :
    gsel K L Ty hB B ⟨X, hx⟩ = gsel K L Ty hB B' ⟨Y, hy⟩ → PEq K L Ty X Y := by
  subst eB
  exact (gsel_spec K L Ty hB B).2 ⟨X, hx⟩ ⟨Y, hy⟩

/-- the f-map is injective up to equality of t-sets -/
theorem fmap_inj (X Y : TSet K L Ty) (h : fmap K he L Ty hB X = fmap K he L Ty hB Y) : PEq K L Ty X.val Y.val := by
  have hp := pair_inj K P he _ _ h
  have h1' := congrArg Prod.fst hp
  have h2' := congrArg Prod.snd hp
  exact gsel_transport K L Ty hB _ _ (rep_eq K he L Ty X Y h1') X.val Y.val _ _ h2'

theorem supp_resp (X Y : TSet K L Ty) (e : PEq K L Ty X.val Y.val) : supp K L Ty X = supp K L Ty Y :=
  K.ch_ext _ _ _ _ (fun B => ⟨supp_peq K L Ty _ _ e B.S, supp_peq K L Ty _ _ (peq_symm K L Ty e) B.S⟩)

theorem gsel_transport' (B B' : SSet P (Addr (P := P) α)) (eB : B = B') (X Y : Pre K L Ty)
    (hx : SuppT K L Ty X B.S) (hy : SuppT K L Ty Y B'.S) (e : PEq K L Ty X Y) :
    gsel K L Ty hB B ⟨X, hx⟩ = gsel K L Ty hB B' ⟨Y, hy⟩ := by
  subst eB
  exact (gsel_spec K L Ty hB B).1 ⟨X, hx⟩ ⟨Y, hy⟩ e

/-- the f-map respects equality of t-sets -/
theorem fmap_resp (X Y : TSet K L Ty) (e : PEq K L Ty X.val Y.val) : fmap K he L Ty hB X = fmap K he L Ty hB Y := by
  have hs := supp_resp K L Ty X Y e
  have hc : suppCode K P he (supp K L Ty X) = suppCode K P he (supp K L Ty Y) := by rw [hs]
  show pair K P he (_, _) = pair K P he (_, _)
  rw [hc, gsel_transport' K L Ty hB _ _ (rep_eq K he L Ty X Y hc) X.val Y.val _ _ e]

/-- the next level of the construction, from the per-support bound -/
def liftB : Lower P α := NFLi.lift K L Ty (fmap K he L Ty hB) (fmap_resp K he L Ty hB)

end NFR

end Found

#print axioms Found.NFR.supp_congr
#print axioms Found.NFR.supp_peq
#print axioms Found.NFR.rep_supp
#print axioms Found.NFR.fmap_inj
#print axioms Found.NFR.fmap_resp
#print axioms Found.NFR.liftB

found/NFStruct.lean

lean_all/found/NFStruct.lean

/-
  T106 (b), slice N4b. found/NFStruct — paths, structural permutations,
  allowable permutations and supports of the tangled-type construction
  (con-nf), over a parameter value P (found/NFParamsO), in the classical point:
  * paths from a type level α down to the base, descending along ltΛ;
  * structural permutations at α: a base permutation for every path, with
    the group laws pointwise, and derivatives along β < α;
  * small sets: closed under injections, images by base permutations,
    and symmetric differences compose (near-litter transitivity);
  * allowable permutations at α, given lower tangle data at every β < α
    (tangles, an action, and an f-map `fuzz` into litters): the derivatives
    are allowable and the f-maps are respected up to small differences;
    allowable permutations contain 1 and are closed under products and
    inverses;
  * supports: a small set of addresses (path, atom) whose pointwise fixer
    fixes the object; the image of a support under π supports π • x.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.NFBase

namespace Found

namespace NFS

open CP WO Card NFP NFPO NFB

variable (K : Classical') (P : ParamsO)

/-! ## paths and structural permutations -/

inductive Path (P : ParamsO) : P.Λ → Type where
  | bot (α : P.Λ) : Path P α
  | cons {α : P.Λ} (β : P.Λ) (h : P.ltΛ β α) (p : Path P β) : Path P α

def StrPerm (α : P.Λ) : Type := Path P α → BPerm P

def SOne (α : P.Λ) : StrPerm P α := fun _ => BPerm.one P
def SMul {α : P.Λ} (π ρ : StrPerm P α) : StrPerm P α := fun A => BPerm.mul P (π A) (ρ A)
def SInv {α : P.Λ} (π : StrPerm P α) : StrPerm P α := fun A => BPerm.inv P (π A)

/-- pointwise equality of structural permutations -/
def SEq {α : P.Λ} (π ρ : StrPerm P α) : Prop := ∀ A a, (π A).f a = (ρ A).f a

theorem smul_assoc {α : P.Λ} (π ρ σ : StrPerm P α) : SEq P (SMul P (SMul P π ρ) σ) (SMul P π (SMul P ρ σ)) :=
  fun _ _ => rfl
theorem sone_mul {α : P.Λ} (π : StrPerm P α) : SEq P (SMul P (SOne P α) π) π := fun _ _ => rfl
theorem smul_one {α : P.Λ} (π : StrPerm P α) : SEq P (SMul P π (SOne P α)) π := fun _ _ => rfl
theorem smul_inv {α : P.Λ} (π : StrPerm P α) : SEq P (SMul P π (SInv P π)) (SOne P α) := fun A a => (π A).gf a
theorem sinv_mul {α : P.Λ} (π : StrPerm P α) : SEq P (SMul P (SInv P π) π) (SOne P α) := fun A a => (π A).fg a

/-- the derivative along β < α -/
def deriv {α β : P.Λ} (π : StrPerm P α) (h : P.ltΛ β α) : StrPerm P β := fun A => π (.cons β h A)

theorem deriv_one {α β : P.Λ} (h : P.ltΛ β α) : deriv P (SOne P α) h = SOne P β := rfl
theorem deriv_mul {α β : P.Λ} (π ρ : StrPerm P α) (h : P.ltΛ β α) :
    deriv P (SMul P π ρ) h = SMul P (deriv P π h) (deriv P ρ h) := rfl
theorem deriv_inv {α β : P.Λ} (π : StrPerm P α) (h : P.ltΛ β α) :
    deriv P (SInv P π) h = SInv P (deriv P π h) := rfl

/-! ## small sets -/

theorem small_of_inj {A B : Type} (f : A → B) (hf : Inj f) (hB : Small P B) : Small P A :=
  match hB.1 with
  | ⟨g, hg⟩ => ⟨⟨fun a => g (f a), fun _ _ e => hf _ _ (hg _ _ e)⟩,
      fun ⟨k, hk⟩ => hB.2 ⟨fun x => f (k x), fun _ _ e => hk _ _ (hf _ _ e)⟩⟩

theorem symm_small (S T : Atom P → Prop) (h : Small P (SymDiff P S T)) : Small P (SymDiff P T S) :=
  small_of_inj P (fun x => ⟨x.val, x.property.elim Or.inr Or.inl⟩) (fun x y e => Subtype.ext (by have e' := congrArg Subtype.val e; exact e')) h

theorem congr_small (S S' T : Atom P → Prop) (e : ∀ a, S a ↔ S' a) (h : Small P (SymDiff P S T)) :
    Small P (SymDiff P S' T) :=
  small_of_inj P (fun x => ⟨x.val, x.property.elim (fun h => Or.inl ⟨(e _).2 h.1, h.2⟩)
      (fun h => Or.inr ⟨h.1, fun h' => h.2 ((e _).1 h')⟩)⟩)
    (fun x y e => Subtype.ext (by have e' := congrArg Subtype.val e; exact e')) h

/-- the atom underlying an element of a sum of two symmetric differences -/
def proj {S T U V : Atom P → Prop} : SymDiff P S T ⊕ SymDiff P U V → Atom P
  | .inl x => x.val
  | .inr x => x.val

include K in
/-- near-litter transitivity: S Δ U ⊆ (S Δ T) ∪ (T Δ U) -/
theorem trans_small (S T U : Atom P → Prop) (h1 : Small P (SymDiff P S T)) (h2 : Small P (SymDiff P T U)) :
    Small P (SymDiff P S U) := by
  have ex : ∀ x : SymDiff P S U, ∃ s : SymDiff P S T ⊕ SymDiff P T U, proj P s = x.val := fun x =>
    (K.lem (T x.val)).elim
      (fun ht => x.property.elim (fun h => ⟨.inr ⟨x.val, Or.inl ⟨ht, h.2⟩⟩, rfl⟩)
        (fun h => ⟨.inl ⟨x.val, Or.inr ⟨ht, h.2⟩⟩, rfl⟩))
      (fun hn => x.property.elim (fun h => ⟨.inl ⟨x.val, Or.inl ⟨h.1, hn⟩⟩, rfl⟩)
        (fun h => ⟨.inr ⟨x.val, Or.inr ⟨h.1, hn⟩⟩, rfl⟩))
  have hs := small_sum K P _ _ h1 h2
  exact small_of_inj P (fun x => K.ch (fun s => proj P s = x.val) (ex x)) (fun x y e => by
    have hx := K.ch_spec (fun s => proj P s = x.val) (ex x)
    have hy := K.ch_spec (fun s => proj P s = y.val) (ex y)
    have e' : K.ch (fun s => proj P s = x.val) (ex x) = K.ch (fun s => proj P s = y.val) (ex y) := e
    rw [e'] at hx
    exact Subtype.ext (hx.symm.trans hy)) hs

/-- images under a base permutation preserve small symmetric differences -/
theorem img_small (π : BPerm P) (S T : Atom P → Prop) (h : Small P (SymDiff P S T)) :
    Small P (SymDiff P (img P π.g S) (img P π.g T)) :=
  small_of_inj P (fun x => ⟨π.g x.val, x.property⟩) (fun x y e => Subtype.ext (by
    have e' : π.g x.val = π.g y.val := congrArg Subtype.val e
    have := congrArg π.f e'
    rw [π.fg, π.fg] at this
    exact this)) h

theorem img_img (π : BPerm P) (S : Atom P → Prop) (a : Atom P) : img P π.f (img P π.g S) a ↔ S a := by
  show S (π.g (π.f a)) ↔ S a
  rw [π.gf]

/-! ## lower tangle data and allowable permutations -/

/-- a set S of atoms is near the litter i -/
def NearTo (S : Atom P → Prop) (i : P.μ) : Prop := Small P (SymDiff P S (litter P i))

/-- tangle data at a lower level β: tangles with an equivalence (their equality), allowable
  permutations acting on them (laws for allowable permutations, up to the equivalence), f-map -/
structure Lower (β : P.Λ) where
  T : Type
  eqv : T → T → Prop
  eqv_refl : ∀ t, eqv t t
  eqv_symm : ∀ {t t'}, eqv t t' → eqv t' t
  eqv_trans : ∀ {t t' t''}, eqv t t' → eqv t' t'' → eqv t t''
  act : StrPerm P β → T → T
  Allow : StrPerm P β → Prop
  allow_one : Allow (SOne P β)
  allow_mul : ∀ π ρ, Allow π → Allow ρ → Allow (SMul P π ρ)
  allow_inv : ∀ π, Allow π → Allow (SInv P π)
  allow_ext : ∀ π ρ, SEq P π ρ → Allow π → Allow ρ
  act_resp : ∀ π, Allow π → ∀ {t t'}, eqv t t' → eqv (act π t) (act π t')
  act_ext : ∀ π ρ, Allow π → Allow ρ → SEq P π ρ → ∀ t, eqv (act π t) (act ρ t)
  act_one : ∀ t, eqv (act (SOne P β) t) t
  act_mul : ∀ π ρ, Allow π → Allow ρ → ∀ t, eqv (act (SMul P π ρ) t) (act ρ (act π t))
  fuzz : T → P.μ
  fuzz_resp : ∀ {t t'}, eqv t t' → fuzz t = fuzz t'

variable {P}

/-- the base permutation acting on the litters at level γ < α -/
def atLit {α γ : P.Λ} (π : StrPerm P α) (h : P.ltΛ γ α) : BPerm P := π (.cons γ h (.bot γ))

/-- allowable permutations at α, given lower data L at every β < α -/
def Allowable {α : P.Λ} (L : ∀ β, P.ltΛ β α → Lower P β) (π : StrPerm P α) : Prop :=
  (∀ β h, (L β h).Allow (deriv P π h)) ∧
  (∀ β hβ γ (hγ : P.ltΛ γ α), β ≠ γ → ∀ t : (L β hβ).T,
    NearTo P (img P (atLit π hγ).g (litter P ((L β hβ).fuzz t))) ((L β hβ).fuzz ((L β hβ).act (deriv P π hβ) t)))

include K in
theorem allowable_one {α : P.Λ} (L : ∀ β, P.ltΛ β α → Lower P β) : Allowable L (SOne P α) :=
  ⟨fun β h => (L β h).allow_one, fun β hβ γ hγ _ t => by
    show Small P (SymDiff P (litter P ((L β hβ).fuzz t)) (litter P ((L β hβ).fuzz ((L β hβ).act (SOne P β) t))))
    rw [(L β hβ).fuzz_resp ((L β hβ).act_one t)]
    exact small_empty K P _ (symm_self_empty P _)⟩

include K in
theorem allowable_mul {α : P.Λ} (L : ∀ β, P.ltΛ β α → Lower P β) (π ρ : StrPerm P α)
    (hπ : Allowable L π) (hρ : Allowable L ρ) : Allowable L (SMul P π ρ) :=
  ⟨fun β h => (L β h).allow_mul _ _ (hπ.1 β h) (hρ.1 β h), fun β hβ γ hγ ne t => by
    let Lb := L β hβ
    have e1 := img_small P (atLit ρ hγ) _ _ (hπ.2 β hβ γ hγ ne t)
    have e2 := hρ.2 β hβ γ hγ ne (Lb.act (deriv P π hβ) t)
    have e3 := trans_small K P _ _ _ e1 e2
    show Small P (SymDiff P (img P (atLit ρ hγ).g (img P (atLit π hγ).g (litter P (Lb.fuzz t))))
      (litter P (Lb.fuzz (Lb.act (SMul P (deriv P π hβ) (deriv P ρ hβ)) t))))
    rw [Lb.fuzz_resp (Lb.act_mul _ _ (hπ.1 β hβ) (hρ.1 β hβ) t)]
    exact e3⟩

theorem allowable_inv {α : P.Λ} (L : ∀ β, P.ltΛ β α → Lower P β) (π : StrPerm P α)
    (hπ : Allowable L π) : Allowable L (SInv P π) :=
  ⟨fun β h => (L β h).allow_inv _ (hπ.1 β h), fun β hβ γ hγ ne t => by
    let Lb := L β hβ
    let s := Lb.act (SInv P (deriv P π hβ)) t
    have back : Lb.fuzz (Lb.act (deriv P π hβ) s) = Lb.fuzz t := by
      have hd := hπ.1 β hβ
      have hi := Lb.allow_inv _ hd
      exact Lb.fuzz_resp (Lb.eqv_trans (Lb.eqv_symm (Lb.act_mul _ _ hi hd t))
        (Lb.eqv_trans (Lb.act_ext _ (SOne P β) (Lb.allow_mul _ _ hi hd) Lb.allow_one (sinv_mul P _) t) (Lb.act_one t)))
    have e1 := hπ.2 β hβ γ hγ ne s
    rw [back] at e1
    have e2 := img_small P (BPerm.inv P (atLit π hγ)) _ _ e1
    have e3 := congr_small P _ _ _ (img_img P (atLit π hγ) (litter P (Lb.fuzz s))) e2
    exact symm_small P _ _ e3⟩

/-! ## supports -/

abbrev Addr (α : P.Λ) : Type := Path P α × Atom P

def actA {α : P.Λ} (π : StrPerm P α) (s : Addr α) : Addr α := (s.1, (π s.1).f s.2)

/-- a type with an action of structural permutations at α -/
structure Act (α : P.Λ) where
  X : Type
  act : StrPerm P α → X → X
  act_ext : ∀ π ρ, SEq P π ρ → ∀ x, act π x = act ρ x
  act_mul : ∀ π ρ x, act (SMul P π ρ) x = act ρ (act π x)

def Supports {α : P.Λ} (M : Act α) (S : Addr α → Prop) (x : M.X) : Prop :=
  Small P {s // S s} ∧ ∀ π, (∀ s, S s → actA π s = s) → M.act π x = x

/-- the image of a set of addresses under π -/
def imgA {α : P.Λ} (π : StrPerm P α) (S : Addr α → Prop) : Addr α → Prop := fun s => S (actA (SInv P π) s)

theorem actA_inv {α : P.Λ} (π : StrPerm P α) (s : Addr α) : actA (SInv P π) (actA π s) = s := by
  show (s.1, (π s.1).g ((π s.1).f s.2)) = s
  rw [(π s.1).gf]

theorem actA_inv' {α : P.Λ} (π : StrPerm P α) (s : Addr α) : actA π (actA (SInv P π) s) = s := by
  show (s.1, (π s.1).f ((π s.1).g s.2)) = s
  rw [(π s.1).fg]

theorem actA_mul {α : P.Λ} (π ρ : StrPerm P α) (s : Addr α) : actA (SMul P π ρ) s = actA ρ (actA π s) := rfl

/-- if S supports x then π S supports π • x -/
theorem supports_act {α : P.Λ} (M : Act α) (S : Addr α → Prop) (x : M.X) (h : Supports M S x)
    (π : StrPerm P α) : Supports M (imgA π S) (M.act π x) := by
  refine ⟨small_of_inj P (fun s => ⟨actA (SInv P π) s.val, s.property⟩) (fun s t e => Subtype.ext (by
    have e' : actA (SInv P π) s.val = actA (SInv P π) t.val := congrArg Subtype.val e
    have := congrArg (actA π) e'
    rw [actA_inv', actA_inv'] at this
    exact this)) h.1, fun ρ hρ => ?_⟩
  let σ := SMul P (SMul P π ρ) (SInv P π)
  have fix : ∀ s, S s → actA σ s = s := fun s hs => by
    have hs' : imgA π S (actA π s) := by
      show S (actA (SInv P π) (actA π s))
      rw [actA_inv]
      exact hs
    show actA (SInv P π) (actA ρ (actA π s)) = s
    rw [hρ _ hs', actA_inv]
  have hσ := h.2 σ fix
  have e : SEq P (SMul P σ π) (SMul P π ρ) := fun A a => by
    show (π A).f ((π A).g ((ρ A).f ((π A).f a))) = (ρ A).f ((π A).f a)
    rw [(π A).fg]
  rw [← M.act_mul, ← M.act_ext _ _ e, M.act_mul, hσ]

end NFS

end Found

#print axioms Found.NFS.smul_assoc
#print axioms Found.NFS.smul_inv
#print axioms Found.NFS.sinv_mul
#print axioms Found.NFS.deriv_mul
#print axioms Found.NFS.deriv_inv
#print axioms Found.NFS.small_of_inj
#print axioms Found.NFS.symm_small
#print axioms Found.NFS.congr_small
#print axioms Found.NFS.trans_small
#print axioms Found.NFS.img_small
#print axioms Found.NFS.img_img
#print axioms Found.NFS.allowable_one
#print axioms Found.NFS.allowable_mul
#print axioms Found.NFS.allowable_inv
#print axioms Found.NFS.actA_inv
#print axioms Found.NFS.supports_act

found/NFSupp.lean

lean_all/found/NFSupp.lean

/-
  T106 (b), slice N4c(vi). found/NFSupp — in the classical point, supports
  of pre-t-sets (con-nf's symmetry condition):
  * a support of a pre-t-set X is a small set S of addresses (path, atom)
    such that every allowable permutation fixing S pointwise fixes X (up to
    pointwise equivalence of extensions);
  * supports are transported: if S supports X then π S supports π • X, for
    every allowable π (conjugation π⁻¹ ρ π stays allowable);
  * the t-sets of level α (supported pre-t-sets) are closed under the action.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.NFTSet

namespace Found

namespace NFU

open CP WO Card NFP NFPO NFB NFS NFL NFA NFT

variable (K : Classical') {P : ParamsO} {α : P.Λ} (L : ∀ β, P.ltΛ β α → Lower P β)
  (Ty : ∀ γ (h : P.ltΛ γ α), Typed (L γ h))

/-- S supports X -/
def SuppT (X : Pre K L Ty) (S : Addr (P := P) α → Prop) : Prop :=
  Small P {s // S s} ∧ ∀ ρ (hρ : Allowable L ρ), (∀ s, S s → actA ρ s = s) → PEq K L Ty (act K L Ty ρ hρ X) X

theorem peq_symm {X Y : Pre K L Ty} (e : PEq K L Ty X Y) : PEq K L Ty Y X := fun γ h u => (e γ h u).symm
theorem peq_trans {X Y Z : Pre K L Ty} (e : PEq K L Ty X Y) (e' : PEq K L Ty Y Z) : PEq K L Ty X Z :=
  fun γ h u => (e γ h u).trans (e' γ h u)

/-- the action depends only on the permutation pointwise -/
theorem act_congr (π ρ : StrPerm P α) (hπ : Allowable L π) (hρ : Allowable L ρ) (e : SEq P π ρ) (X : Pre K L Ty) :
    PEq K L Ty (act K L Ty π hπ X) (act K L Ty ρ hρ X) := fun γ h u =>
  E_iff K L Ty X γ h ((L γ h).act_ext (SInv P (deriv P π h)) (SInv P (deriv P ρ h)) ((L γ h).allow_inv _ (hπ.1 γ h))
    ((L γ h).allow_inv _ (hρ.1 γ h)) (fun A a => by
    show (π (.cons γ h A)).g a = (ρ (.cons γ h A)).g a
    have e1 := e (.cons γ h A) ((ρ (.cons γ h A)).g a)
    rw [(ρ (.cons γ h A)).fg] at e1
    have e2 := congrArg (π (.cons γ h A)).g e1
    rw [(π (.cons γ h A)).gf] at e2
    exact e2.symm) u)

/-- supports are transported by the action -/
theorem supp_act (X : Pre K L Ty) (S : Addr (P := P) α → Prop) (h : SuppT K L Ty X S)
    (π : StrPerm P α) (hπ : Allowable L π) : SuppT K L Ty (act K L Ty π hπ X) (imgA π S) := by
  refine ⟨small_of_inj P (fun s => ⟨actA (SInv P π) s.val, s.property⟩) (fun s t e => Subtype.ext (by
    have e' : actA (SInv P π) s.val = actA (SInv P π) t.val := congrArg Subtype.val e
    have := congrArg (actA π) e'
    rw [actA_inv', actA_inv'] at this
    exact this)) h.1, fun ρ hρ hfix => ?_⟩
  let σ := SMul P (SMul P π ρ) (SInv P π)
  have hσ : Allowable L σ := allowable_mul K L _ _ (allowable_mul K L _ _ hπ hρ) (allowable_inv L π hπ)
  have fix : ∀ s, S s → actA σ s = s := fun s hs => by
    have hs' : imgA π S (actA π s) := by
      show S (actA (SInv P π) (actA π s))
      rw [actA_inv]
      exact hs
    show actA (SInv P π) (actA ρ (actA π s)) = s
    rw [hfix _ hs', actA_inv]
  have h1 := h.2 σ hσ fix
  -- act ρ (act π X) ≈ act (π ρ) X ≈ act (σ π) X ≈ act π (act σ X) ≈ act π X
  have e : SEq P (SMul P σ π) (SMul P π ρ) := fun A a => by
    show (π A).f ((π A).g ((ρ A).f ((π A).f a))) = (ρ A).f ((π A).f a)
    rw [(π A).fg]
  exact peq_trans K L Ty (peq_symm K L Ty (NFT.act_mul K L Ty π ρ hπ hρ X))
    (peq_trans K L Ty (act_congr K L Ty _ _ _ (allowable_mul K L _ _ hσ hπ) (fun A a => (e A a).symm) X)
      (peq_trans K L Ty (NFT.act_mul K L Ty σ π hσ hπ X) (act_resp K L Ty π hπ _ _ h1)))

/-- t-sets of level α: supported pre-t-sets -/
def TSet : Type := {X : Pre K L Ty // ∃ S, SuppT K L Ty X S}

def tact (π : StrPerm P α) (hπ : Allowable L π) (X : TSet K L Ty) : TSet K L Ty :=
  ⟨act K L Ty π hπ X.val, match X.property with
    | ⟨S, hS⟩ => ⟨imgA π S, supp_act K L Ty X.val S hS π hπ⟩⟩

end NFU

end Found

#print axioms Found.NFU.act_congr
#print axioms Found.NFU.supp_act
#print axioms Found.NFU.tact

found/NFTSet.lean

lean_all/found/NFTSet.lean

/-
  T106 (b), slice N4c(v). found/NFTSet — in the classical point, the
  extensional part of the t-sets of level α (con-nf):
  * a pre-t-set is a family of extensions E_γ (sets of γ-tangles, every
    γ < α) generated by one of them: for some β < α, every other extension
    is the alternative extension of (β, E_β);
  * allowable permutations act on pre-t-sets (extension-wise) and the action
    preserves the generation condition (equivariance of alternative
    extensions, found/NFAlt);
  * the action is a group action up to pointwise equivalence of extensions:
    identity, products.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.NFAlt

namespace Found

namespace NFT

open CP WO Card NFP NFPO NFB NFS NFL NFA

variable (K : Classical') {P : ParamsO} {α : P.Λ} (L : ∀ β, P.ltΛ β α → Lower P β)
  (Ty : ∀ γ (h : P.ltΛ γ α), Typed (L γ h))

/-- a pre-t-set: extensions generated by the β-extension -/
structure Pre where
  E : ∀ γ (h : P.ltΛ γ α), (L γ h).T → Prop
  resp : ∀ γ h, RespS (L γ h) (E γ h)
  β : P.Λ
  hβ : P.ltΛ β α
  gen : ∀ γ (hγ : P.ltΛ γ α), β ≠ γ → ∀ u, E γ hγ u ↔ alt K L γ hγ (Ty γ hγ) ⟨β, hβ, E β hβ, resp β hβ⟩ u

/-- pointwise equivalence of extensions -/
def PEq (X Y : Pre K L Ty) : Prop := ∀ γ h u, X.E γ h u ↔ Y.E γ h u

/-- the action of an allowable permutation -/
def act (π : StrPerm P α) (hπ : Allowable L π) (X : Pre K L Ty) : Pre K L Ty where
  E γ h := setAct L γ h π (X.E γ h)
  resp γ h := setAct_resp L γ h π hπ _ (X.resp γ h)
  β := X.β
  hβ := X.hβ
  gen γ hγ ne u := (X.gen γ hγ ne _).trans
    (alt_equivariant K L γ hγ (Ty γ hγ) π hπ ⟨X.β, X.hβ, X.E X.β X.hβ, X.resp X.β X.hβ⟩ ne u).symm

/-- E respects equivalence in both directions -/
theorem E_iff (X : Pre K L Ty) (γ : P.Λ) (h : P.ltΛ γ α) {u v : (L γ h).T} (e : (L γ h).eqv u v) :
    X.E γ h u ↔ X.E γ h v :=
  ⟨X.resp γ h u v e, X.resp γ h v u ((L γ h).eqv_symm e)⟩

theorem act_one (X : Pre K L Ty) : PEq K L Ty (act K L Ty (SOne P α) (allowable_one K L) X) X :=
  fun γ h u => E_iff K L Ty X γ h ((L γ h).eqv_trans
    ((L γ h).act_ext (SInv P (deriv P (SOne P α) h)) (SOne P γ) ((L γ h).allow_inv _ (L γ h).allow_one)
      (L γ h).allow_one (fun _ _ => rfl) u) ((L γ h).act_one u))

theorem act_mul (π ρ : StrPerm P α) (hπ : Allowable L π) (hρ : Allowable L ρ) (X : Pre K L Ty) :
    PEq K L Ty (act K L Ty (SMul P π ρ) (allowable_mul K L π ρ hπ hρ) X)
      (act K L Ty ρ hρ (act K L Ty π hπ X)) :=
  fun γ h u =>
    have a1 := (L γ h).allow_inv _ (hρ.1 γ h)
    have a2 := (L γ h).allow_inv _ (hπ.1 γ h)
    E_iff K L Ty X γ h ((L γ h).eqv_trans
      ((L γ h).act_ext (SInv P (deriv P (SMul P π ρ) h)) (SMul P (SInv P (deriv P ρ h)) (SInv P (deriv P π h)))
        ((L γ h).allow_inv _ ((L γ h).allow_mul _ _ (hπ.1 γ h) (hρ.1 γ h))) ((L γ h).allow_mul _ _ a1 a2)
        (fun _ _ => rfl) u)
      ((L γ h).act_mul _ _ a1 a2 u))

/-- the action respects pointwise equivalence -/
theorem act_resp (π : StrPerm P α) (hπ : Allowable L π) (X Y : Pre K L Ty) (e : PEq K L Ty X Y) :
    PEq K L Ty (act K L Ty π hπ X) (act K L Ty π hπ Y) :=
  fun γ h _ => e γ h _

end NFT

end Found

#print axioms Found.NFT.act
#print axioms Found.NFT.act_one
#print axioms Found.NFT.act_mul
#print axioms Found.NFT.act_resp

found/NFTag.lean

lean_all/found/NFTag.lean

/-
  T106 (b), slice N4d(ix). found/NFTag — level-tagged f-maps: the f-map of
  level α (found/NFReduce) paired with the code of α in μ.  Tagged f-maps of
  different levels have disjoint images and stay injective up to equality,
  which is the hypothesis of freedom of action (found/NFAssemble); the
  tagged lift is again bottom-independent (found/NFLiftBI).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.NFReduce
import found.NFLiftBI

namespace Found

namespace NFTg

open CP WO Card NFP NFPO NFB NFS NFL NFA NFT NFU NFC NFD NFR NFLi NFAs NFLB

variable (K : Classical') {P : ParamsO} {e : Nat → P.μ} (he : Inj e)

/-- tag a code with its level -/
def tag (α : P.Λ) (x : P.μ) : P.μ := pair K P he (lamCode K P α, x)

theorem tag_level (α β : P.Λ) (x y : P.μ) (h : tag K he α x = tag K he β y) : α = β :=
  lamCode_inj K P _ _ (congrArg Prod.fst (pair_inj K P he _ _ h))

theorem tag_inj (α : P.Λ) (x y : P.μ) (h : tag K he α x = tag K he α y) : x = y :=
  congrArg Prod.snd (pair_inj K P he _ _ h)

variable {α : P.Λ} (L : ∀ β, P.ltΛ β α → Lower P β) (Ty : ∀ γ (h : P.ltΛ γ α), Typed (L γ h))
  (hB : Bound K L Ty)

/-- the tagged next level -/
def liftT : Lower P α :=
  lift K L Ty (fun X => tag K he α (fmap K he L Ty hB X))
    (fun X Y eq => congrArg (tag K he α) (fmap_resp K he L Ty hB X Y eq))

theorem liftT_inj (X Y : (liftT K he L Ty hB).T) (h : (liftT K he L Ty hB).fuzz X = (liftT K he L Ty hB).fuzz Y) :
    (liftT K he L Ty hB).eqv X Y :=
  fmap_inj K he L Ty hB X Y (tag_inj K he α _ _ h)

theorem liftT_botIndep : BotIndep (liftT K he L Ty hB) :=
  lift_botIndep K L Ty _ _

end NFTg

end Found

#print axioms Found.NFTg.tag_level
#print axioms Found.NFTg.tag_inj
#print axioms Found.NFTg.liftT
#print axioms Found.NFTg.liftT_inj
#print axioms Found.NFTg.liftT_botIndep

found/NNO.lean

lean_all/found/NNO.lean

/-
  T86. found/NNO — natural numbers objects as a parameter value, and the
  bridge to arithmetic.

  * General: an NNO in any E-category C with a terminal object
    (zero, succ, iteration, uniqueness up to ≈) — stated for the parameter C.
  * Instance: the discrete setoid of ℕ is an NNO in the setoid category.
  * Uniqueness: any two NNOs in the same C are isomorphic (general proof).
  * Bridge to arithmetic: iteration in the instance is primitive recursion,
    the operations of the standard model used by ArithBridge (T85a) — so
    Heyting arithmetic's standard model is the NNO instance of the
    category parameter.

  Lean 4 core only, hand-written proofs, no axioms.
  CIC dependence: structural recursion on Nat (iteration).
-/
import found.CCC

namespace Found

namespace NNOs

universe u v

structure NNO (C : ECat.{u, v}) (T : Terminal C) where
  N : C.Ob
  z : C.Hom T.one N
  s : C.Hom N N
  iter : {X : C.Ob} → C.Hom T.one X → C.Hom X X → C.Hom N X
  iter_z : ∀ {X} (x : C.Hom T.one X) (f : C.Hom X X), C.heq (C.comp z (iter x f)) x
  iter_s : ∀ {X} (x : C.Hom T.one X) (f : C.Hom X X),
    C.heq (C.comp s (iter x f)) (C.comp (iter x f) f)
  uniq : ∀ {X} (x : C.Hom T.one X) (f : C.Hom X X) (h : C.Hom N X),
    C.heq (C.comp z h) x → C.heq (C.comp s h) (C.comp h f) → C.heq h (iter x f)

/-- two NNOs are isomorphic (general, for any C) -/
theorem nno_iso {C : ECat.{u, v}} {T : Terminal C} (A B : NNO C T) :
    C.heq (C.comp (A.iter B.z B.s) (B.iter A.z A.s)) (C.id A.N) := by
  have h1 : C.heq (C.comp A.z (C.comp (A.iter B.z B.s) (B.iter A.z A.s))) A.z :=
    C.htrans (C.hsymm (C.assoc _ _ _)) (C.htrans (C.lcong (A.iter_z _ _) _) (B.iter_z _ _))
  have h2 : C.heq (C.comp A.s (C.comp (A.iter B.z B.s) (B.iter A.z A.s)))
      (C.comp (C.comp (A.iter B.z B.s) (B.iter A.z A.s)) A.s) :=
    C.htrans (C.hsymm (C.assoc _ _ _))
    (C.htrans (C.lcong (A.iter_s _ _) _)
    (C.htrans (C.assoc _ _ _)
    (C.htrans (C.rcong _ (B.iter_s _ _)) (C.hsymm (C.assoc _ _ _)))))
  have k1 := A.uniq A.z A.s _ h1 h2
  have k2 := A.uniq A.z A.s (C.id A.N) (C.comp_id A.z)
    (C.htrans (C.comp_id A.s) (C.hsymm (C.id_comp A.s)))
  exact C.htrans k1 (C.hsymm k2)

/-! ## Instance: ℕ in setoids -/

def natS : Setd.{0} := ⟨Nat, Eq, fun _ => rfl, fun h => h.symm, fun h k => h.trans k⟩

def iterF {X : Setd.{0}} (x0 : X.car) (f : SHom X X) : Nat → X.car
  | 0 => x0
  | n + 1 => f.fn (iterF x0 f n)

def natNNO : NNO SetdCat.{0} setdTerminal where
  N := natS
  z := ⟨fun _ => Nat.zero, fun _ => rfl⟩
  s := ⟨Nat.succ, fun h => congrArg Nat.succ h⟩
  iter x f := ⟨iterF (x.fn PUnit.unit) f, fun {a b} h => by cases h; exact Setd.refl _ _⟩
  iter_z {X} x _ _ := X.refl _
  iter_s {X} _ _ _ := X.refl _
  uniq {X} x f h hz hs := by
    intro n
    induction n with
    | zero => exact hz PUnit.unit
    | succ n ih => exact X.trans (hs n) (f.resp ih)

/-- the bridge: iteration of successor from zero is the identity on ℕ, and
  iteration of `+ m` computes addition — the standard model's operations -/
theorem iter_succ_id (n : Nat) : iterF (X := natS) Nat.zero ⟨Nat.succ, fun h => congrArg Nat.succ h⟩ n = n := by
  induction n with
  | zero => rfl
  | succ n ih => exact congrArg Nat.succ ih

end NNOs

end Found

#print axioms Found.NNOs.nno_iso
#print axioms Found.NNOs.natNNO
#print axioms Found.NNOs.iter_succ_id

found/NbgZ1.lean

lean_all/found/NbgZ1.lean

/-
  T91, NBG over ZF, part 1. found/NbgZ1 — the class model over a model of
  ZF (Leibniz extensionality), for the semantic proof of conservativity:
  * L0: formulas whose only atoms are membership atoms ∈ (symbol 0, two
    arguments);
  * in a structure D with Leibniz extensionality (coextensive sets are
    members of the same sets), every L0 formula is invariant under replacing
    variables by coextensive ones;
  * the class structure D' = D ⊕ (definable classes: an L0 formula with
    parameters); the extension of an element, coextension, membership
    (A ∈ B iff A is coextensive with a set lying in the extension of B) and
    the set predicate M (coextensive with a set) — all invariant under
    coextension.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.FOClassicalT

namespace Found

namespace NBGc

open FO FOCT

/-- the language of ZF: membership atoms only -/
def L0 : Fm → Prop
  | .atom r l => r = 0 ∧ ∃ a b, l = [a, b]
  | .bot => True
  | .conj a b => L0 a ∧ L0 b
  | .disj a b => L0 a ∧ L0 b
  | .imp a b => L0 a ∧ L0 b
  | .all a => L0 a
  | .ex a => L0 a

theorem l0_ren (ρ : Nat → Nat) : (φ : Fm) → L0 φ → L0 (ren ρ φ)
  | .atom _ _, h => match h with
    | ⟨e0, a, b, e⟩ => ⟨e0, ρ a, ρ b, by rw [e]; rfl⟩
  | .bot, _ => trivial
  | .conj a b, h => ⟨l0_ren ρ a h.1, l0_ren ρ b h.2⟩
  | .disj a b, h => ⟨l0_ren ρ a h.1, l0_ren ρ b h.2⟩
  | .imp a b, h => ⟨l0_ren ρ a h.1, l0_ren ρ b h.2⟩
  | .all a, h => l0_ren (up ρ) a h
  | .ex a, h => l0_ren (up ρ) a h

section D

variable {D : Type} (R : Nat → List D → Prop)

def mem (x y : D) : Prop := R 0 [x, y]

/-- coextension -/
def Ceq (x y : D) : Prop := ∀ u, mem R u x ↔ mem R u y

/-- Leibniz extensionality: coextensive sets lie in the same sets -/
def ExtD : Prop := ∀ x y z, Ceq R x y → mem R x z → mem R y z

theorem ceq_refl (x : D) : Ceq R x x := fun _ => Iff.rfl
theorem ceq_symm {x y : D} (h : Ceq R x y) : Ceq R y x := fun u => (h u).symm

theorem mem_resp (hE : ExtD R) {x x' y y' : D} (hx : Ceq R x x') (hy : Ceq R y y') (h : mem R x y) : mem R x' y' :=
  (hy x').1 (hE x x' y hx h)

theorem ext_ceq {v w : Nat → D} (h : ∀ n, Ceq R (v n) (w n)) (d : D) : ∀ n, Ceq R (ext v d n) (ext w d n)
  | 0 => ceq_refl R d
  | n + 1 => h n

/-- L0 formulas are invariant under coextension -/
theorem sat_ceq (hE : ExtD R) : (φ : Fm) → L0 φ → {v w : Nat → D} → (∀ n, Ceq R (v n) (w n)) →
    Sat R φ v → Sat R φ w
  | .atom _ _, hl, v, w, h, s => match hl with
    | ⟨e0, a, b, e⟩ => by
      subst e0; subst e
      exact mem_resp R hE (h a) (h b) s
  | .bot, _, _, _, _, s => s
  | .conj a b, hl, _, _, h, s => ⟨sat_ceq hE a hl.1 h s.1, sat_ceq hE b hl.2 h s.2⟩
  | .disj a b, hl, _, _, h, s => s.elim (fun x => Or.inl (sat_ceq hE a hl.1 h x))
      (fun y => Or.inr (sat_ceq hE b hl.2 h y))
  | .imp a b, hl, _, _, h, s => fun x =>
      sat_ceq hE b hl.2 h (s (sat_ceq hE a hl.1 (fun n => ceq_symm R (h n)) x))
  | .all a, hl, _, _, h, s => fun d => sat_ceq hE a hl (ext_ceq R h d) (s d)
  | .ex a, hl, _, _, h, s => match s with
    | ⟨d, x⟩ => ⟨d, sat_ceq hE a hl (ext_ceq R h d) x⟩

theorem sat_ceq_iff (hE : ExtD R) (φ : Fm) (hl : L0 φ) {v w : Nat → D} (h : ∀ n, Ceq R (v n) (w n)) :
    Sat R φ v ↔ Sat R φ w :=
  ⟨sat_ceq R hE φ hl h, sat_ceq R hE φ hl (fun n => ceq_symm R (h n))⟩

/-! ## the class structure -/

/-- a definable class: an L0 formula (variable 0 = the member) with parameters -/
abbrev Cl (D : Type) : Type := {χ : Fm // L0 χ} × (Nat → D)

abbrev D' (D : Type) : Type := D ⊕ Cl D

/-- the extension of an element of D' -/
def ext' : D' D → D → Prop
  | .inl x => fun u => mem R u x
  | .inr c => fun u => Sat R c.1.1 (ext c.2 u)

def Ceq' (A B : D' D) : Prop := ∀ u, ext' R A u ↔ ext' R B u

def memb (A B : D' D) : Prop := ∃ x, Ceq' R A (.inl x) ∧ ext' R B x

def IsM (A : D' D) : Prop := ∃ x, Ceq' R A (.inl x)

/-- the relations of the class structure: 0 = membership, 1 = being a set -/
def R' : Nat → List (D' D) → Prop
  | 0, [] => False
  | 0, [_] => False
  | 0, [a, b] => memb R a b
  | 0, _ :: _ :: _ :: _ => False
  | 1, [] => False
  | 1, [a] => IsM R a
  | 1, _ :: _ :: _ => False
  | _ + 2, _ => False

theorem ceq'_refl (A : D' D) : Ceq' R A A := fun _ => Iff.rfl
theorem ceq'_symm {A B : D' D} (h : Ceq' R A B) : Ceq' R B A := fun u => (h u).symm
theorem ceq'_trans {A B C : D' D} (h1 : Ceq' R A B) (h2 : Ceq' R B C) : Ceq' R A C :=
  fun u => (h1 u).trans (h2 u)

theorem ext_ceq0 (p : Nat → D) {u u' : D} (h : Ceq R u u') : ∀ n, Ceq R (ext p u n) (ext p u' n)
  | 0 => h
  | _ + 1 => ceq_refl R _

/-- extensions respect coextension of the argument -/
theorem ext'_resp (hE : ExtD R) : (A : D' D) → {u u' : D} → Ceq R u u' → ext' R A u → ext' R A u'
  | .inl _, _, _, h, s => hE _ _ _ h s
  | .inr c, _, _, h, s => sat_ceq R hE c.1.1 c.1.2 (ext_ceq0 R c.2 h) s

theorem ceq'_inl {x y : D} : Ceq' R (.inl x) (.inl y) ↔ Ceq R x y := Iff.rfl

theorem memb_resp {A A' B B' : D' D} (ha : Ceq' R A A') (hb : Ceq' R B B') (h : memb R A B) : memb R A' B' :=
  match h with
  | ⟨x, hx, hm⟩ => ⟨x, ceq'_trans R (ceq'_symm R ha) hx, (hb x).1 hm⟩

theorem isM_resp {A A' : D' D} (ha : Ceq' R A A') (h : IsM R A) : IsM R A' :=
  match h with
  | ⟨x, hx⟩ => ⟨x, ceq'_trans R (ceq'_symm R ha) hx⟩

/-- on sets, membership of D' is membership of D -/
theorem memb_inl (hE : ExtD R) (x y : D) : memb R (.inl x) (.inl y) ↔ mem R x y :=
  ⟨fun ⟨x', hx, hm⟩ => hE x' x y (ceq_symm R hx) hm, fun h => ⟨x, ceq'_refl R _, h⟩⟩

theorem isM_inl (x : D) : IsM R (.inl x) := ⟨x, ceq'_refl R _⟩

/-- members of anything are sets -/
theorem memb_isM {A B : D' D} (h : memb R A B) : IsM R A :=
  match h with
  | ⟨x, hx, _⟩ => ⟨x, hx⟩

/-- for a set u, membership in B is lying in the extension of B -/
theorem memb_inl_l (hE : ExtD R) (u : D) (B : D' D) : memb R (.inl u) B ↔ ext' R B u :=
  ⟨fun ⟨_, hx, hm⟩ => ext'_resp R hE B (ceq_symm R hx) hm, fun h => ⟨u, ceq'_refl R _, h⟩⟩

/-- every set is coextensive with a class -/
def asCl (x : D) : Cl D := (⟨.atom 0 [0, 1], ⟨rfl, 0, 1, rfl⟩⟩, fun _ => x)

theorem asCl_ceq (x : D) : Ceq' R (.inl x) (.inr (asCl x)) := fun _ => Iff.rfl

end D

end NBGc

end Found

#print axioms Found.NBGc.l0_ren
#print axioms Found.NBGc.mem_resp
#print axioms Found.NBGc.sat_ceq
#print axioms Found.NBGc.sat_ceq_iff
#print axioms Found.NBGc.R'
#print axioms Found.NBGc.ceq'_trans
#print axioms Found.NBGc.ext'_resp
#print axioms Found.NBGc.memb_resp
#print axioms Found.NBGc.isM_resp
#print axioms Found.NBGc.memb_inl
#print axioms Found.NBGc.memb_isM
#print axioms Found.NBGc.memb_inl_l
#print axioms Found.NBGc.asCl_ceq

found/NbgZ2.lean

lean_all/found/NbgZ2.lean

/-
  T91, NBG over ZF, part 2. found/NbgZ2 — predicative formulas and their
  translation into the language of ZF:
  * PF: formulas in ∈ and M whose quantifiers range over sets; emb : PF → Fm
    with guarded quantifiers ∀x (M x → …), ∃x (M x ∧ …);
  * an environment assigns to each variable either a set variable of D or
    a definable class (an L0 formula with parameters taken from variables);
  * the translation tr env ψ is an L0 formula, and (truth lemma) it holds in
    D exactly when emb ψ holds in the class structure D' under any
    valuation agreeing with the environment up to coextension.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.NbgZ1

namespace Found

namespace NBGc

open FO FOCT

inductive PF where
  | mem : Nat → Nat → PF
  | isM : Nat → PF
  | bot : PF
  | conj : PF → PF → PF
  | disj : PF → PF → PF
  | imp : PF → PF → PF
  | allS : PF → PF
  | exS : PF → PF

def Mv (n : Nat) : Fm := .atom 1 [n]

def emb : PF → Fm
  | .mem a b => .atom 0 [a, b]
  | .isM a => Mv a
  | .bot => .bot
  | .conj p q => .conj (emb p) (emb q)
  | .disj p q => .disj (emb p) (emb q)
  | .imp p q => .imp (emb p) (emb q)
  | .allS p => .all (.imp (Mv 0) (emb p))
  | .exS p => .ex (.conj (Mv 0) (emb p))

/-- what a variable stands for: a set variable, or a class with parameter variables -/
inductive Kd where
  | set : Nat → Kd
  | cls : {χ : Fm // L0 χ} → (Nat → Nat) → Kd

def shK : Kd → Kd
  | .set i => .set (i + 1)
  | .cls χ π => .cls χ (fun k => π k + 1)

def sg (m : Nat) (π : Nat → Nat) : Nat → Nat
  | 0 => m
  | k + 1 => π k

/-- "variable m lies in the extension of k" -/
def extF (m : Nat) : Kd → Fm
  | .set i => .atom 0 [m, i]
  | .cls χ π => ren (sg m π) χ.1

def iffF (a b : Fm) : Fm := .conj (.imp a b) (.imp b a)

/-- at depth one: "variable 0 is coextensive with k" -/
def coF (k : Kd) : Fm := .all (iffF (.atom 0 [0, 1]) (extF 0 (shK k)))

def trM (ka kb : Kd) : Fm := .ex (.conj (coF (shK ka)) (extF 0 (shK kb)))

def trS (k : Kd) : Fm := .ex (coF (shK k))

def cons0 (env : Nat → Kd) : Nat → Kd
  | 0 => .set 0
  | n + 1 => shK (env n)

def tr : (Nat → Kd) → PF → Fm
  | env, .mem a b => trM (env a) (env b)
  | env, .isM a => trS (env a)
  | _, .bot => .bot
  | env, .conj p q => .conj (tr env p) (tr env q)
  | env, .disj p q => .disj (tr env p) (tr env q)
  | env, .imp p q => .imp (tr env p) (tr env q)
  | env, .allS p => .all (tr (cons0 env) p)
  | env, .exS p => .ex (tr (cons0 env) p)

theorem l0_extF (m : Nat) : (k : Kd) → L0 (extF m k)
  | .set i => ⟨rfl, m, i, rfl⟩
  | .cls χ π => l0_ren (sg m π) χ.1 χ.2

theorem l0_coF (k : Kd) : L0 (coF k) :=
  ⟨⟨⟨rfl, 0, 1, rfl⟩, l0_extF 0 _⟩, ⟨l0_extF 0 _, ⟨rfl, 0, 1, rfl⟩⟩⟩

theorem l0_tr : (env : Nat → Kd) → (p : PF) → L0 (tr env p)
  | _, .mem _ _ => ⟨l0_coF _, l0_extF 0 _⟩
  | _, .isM _ => l0_coF _
  | _, .bot => trivial
  | env, .conj p q => ⟨l0_tr env p, l0_tr env q⟩
  | env, .disj p q => ⟨l0_tr env p, l0_tr env q⟩
  | env, .imp p q => ⟨l0_tr env p, l0_tr env q⟩
  | env, .allS p => l0_tr (cons0 env) p
  | env, .exS p => l0_tr (cons0 env) p

section D

variable {D : Type} (R : Nat → List D → Prop)

def val (q : Nat → D) : Kd → D' D
  | .set i => .inl (q i)
  | .cls χ π => .inr (χ, fun k => q (π k))

theorem val_sh (q : Nat → D) (d : D) : (k : Kd) → val (ext q d) (shK k) = val q k
  | .set _ => rfl
  | .cls _ _ => rfl

theorem extF_sat (q : Nat → D) (m : Nat) : (k : Kd) → (Sat R (extF m k) q ↔ ext' R (val q k) (q m))
  | .set _ => Iff.rfl
  | .cls χ π => (sat_ren R χ.1 (sg m π) q).trans (sat_ext R χ.1 (fun n => match n with
      | 0 => rfl
      | _ + 1 => rfl))

theorem extF_sh1 (q : Nat → D) (x : D) (k : Kd) :
    Sat R (extF 0 (shK k)) (ext q x) ↔ ext' R (val q k) x := by
  have h := extF_sat R (ext q x) 0 (shK k)
  rw [val_sh] at h
  exact h

theorem extF_sh2 (q : Nat → D) (x u : D) (k : Kd) :
    Sat R (extF 0 (shK (shK k))) (ext (ext q x) u) ↔ ext' R (val q k) u := by
  have h := extF_sat R (ext (ext q x) u) 0 (shK (shK k))
  rw [val_sh, val_sh] at h
  exact h

theorem coF_sat (q : Nat → D) (x : D) (k : Kd) :
    Sat R (coF (shK k)) (ext q x) ↔ Ceq' R (val q k) (.inl x) :=
  ⟨fun h u => ⟨fun e => (h u).2 ((extF_sh2 R q x u k).2 e), fun m => (extF_sh2 R q x u k).1 ((h u).1 m)⟩,
   fun h u => ⟨fun m => (extF_sh2 R q x u k).2 ((h u).2 m), fun e => (h u).1 ((extF_sh2 R q x u k).1 e)⟩⟩

theorem trM_sat (q : Nat → D) (ka kb : Kd) : Sat R (trM ka kb) q ↔ memb R (val q ka) (val q kb) :=
  ⟨fun ⟨x, h1, h2⟩ => ⟨x, (coF_sat R q x ka).1 h1, (extF_sh1 R q x kb).1 h2⟩,
   fun ⟨x, h1, h2⟩ => ⟨x, (coF_sat R q x ka).2 h1, (extF_sh1 R q x kb).2 h2⟩⟩

theorem trS_sat (q : Nat → D) (k : Kd) : Sat R (trS k) q ↔ IsM R (val q k) :=
  ⟨fun ⟨x, h⟩ => ⟨x, (coF_sat R q x k).1 h⟩, fun ⟨x, h⟩ => ⟨x, (coF_sat R q x k).2 h⟩⟩

/-- a valuation of D' agrees with an environment up to coextension -/
def Cons (env : Nat → Kd) (q : Nat → D) (w : Nat → D' D) : Prop := ∀ n, Ceq' R (w n) (val q (env n))

theorem cons_ext {env : Nat → Kd} {q : Nat → D} {w : Nat → D' D} (h : Cons R env q w) {X : D' D} {x : D}
    (hx : Ceq' R X (.inl x)) : Cons R (cons0 env) (ext q x) (ext w X)
  | 0 => hx
  | n + 1 => by
    show Ceq' R (w n) (val (ext q x) (shK (env n)))
    rw [val_sh]
    exact h n

/-- the truth lemma of the translation -/
theorem truth : (p : PF) → (env : Nat → Kd) → (q : Nat → D) → (w : Nat → D' D) → Cons R env q w →
    (Sat R (tr env p) q ↔ Sat (R' R) (emb p) w)
  | .mem a b, env, q, _, h => (trM_sat R q (env a) (env b)).trans
      ⟨memb_resp R (ceq'_symm R (h a)) (ceq'_symm R (h b)), memb_resp R (h a) (h b)⟩
  | .isM a, env, q, _, h => (trS_sat R q (env a)).trans
      ⟨isM_resp R (ceq'_symm R (h a)), isM_resp R (h a)⟩
  | .bot, _, _, _, _ => Iff.rfl
  | .conj p p', env, q, w, h => ⟨fun ⟨x, y⟩ => ⟨(truth p env q w h).1 x, (truth p' env q w h).1 y⟩,
      fun ⟨x, y⟩ => ⟨(truth p env q w h).2 x, (truth p' env q w h).2 y⟩⟩
  | .disj p p', env, q, w, h => ⟨fun s => s.elim (fun x => Or.inl ((truth p env q w h).1 x))
        (fun y => Or.inr ((truth p' env q w h).1 y)),
      fun s => s.elim (fun x => Or.inl ((truth p env q w h).2 x)) (fun y => Or.inr ((truth p' env q w h).2 y))⟩
  | .imp p p', env, q, w, h => ⟨fun f x => (truth p' env q w h).1 (f ((truth p env q w h).2 x)),
      fun f x => (truth p' env q w h).2 (f ((truth p env q w h).1 x))⟩
  | .allS p, env, q, w, h =>
    ⟨fun f X hX => match hX with
      | ⟨x, hx⟩ => (truth p (cons0 env) (ext q x) (ext w X) (cons_ext R h hx)).1 (f x),
     fun f d => (truth p (cons0 env) (ext q d) (ext w (.inl d)) (cons_ext R h (ceq'_refl R _))).2
        (f (.inl d) (isM_inl R d))⟩
  | .exS p, env, q, w, h =>
    ⟨fun ⟨d, s⟩ => ⟨.inl d, isM_inl R d,
        (truth p (cons0 env) (ext q d) (ext w (.inl d)) (cons_ext R h (ceq'_refl R _))).1 s⟩,
     fun ⟨X, hX, s⟩ => match hX with
      | ⟨x, hx⟩ => ⟨x, (truth p (cons0 env) (ext q x) (ext w X) (cons_ext R h hx)).2 s⟩⟩

end D

end NBGc

end Found

#print axioms Found.NBGc.l0_tr
#print axioms Found.NBGc.val_sh
#print axioms Found.NBGc.extF_sat
#print axioms Found.NBGc.coF_sat
#print axioms Found.NBGc.trM_sat
#print axioms Found.NBGc.trS_sat
#print axioms Found.NBGc.cons_ext
#print axioms Found.NBGc.truth

found/NbgZ3.lean

lean_all/found/NbgZ3.lean

/-
  T91, NBG over ZF, part 3. found/NbgZ3 — in the class structure D' over a
  model D of Leibniz extensionality:
  * every valuation of D' is matched by an environment and a valuation of D
    (sets as set variables, classes as their formulas with parameters coded
    by the pairing ⟨a,b⟩ = 2^a(2b+1) of found/EffectiveK1);
  * predicative class comprehension: for every predicative ψ and all
    parameters there is a class Y whose members are exactly the sets
    satisfying ψ;
  * the relativisation of an L0 formula (quantifiers over sets) holds in D'
    on sets exactly when the formula holds in D;
  * class extensionality and "the sets are the members" hold in D'.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.NbgZ2
import found.EffectiveK1

namespace Found

namespace NBGc

open FO FOCT

/-! ## formulas -/

def m (a b : Nat) : Fm := .atom 0 [a, b]

def topF : Fm := .imp .bot .bot

theorem l0_top : L0 topF := ⟨trivial, trivial⟩

/-- Leibniz extensionality: ∀x ∀y (∀u (u∈x ↔ u∈y) → ∀z (x∈z → y∈z)) -/
def extAx : Fm := .all (.all (.imp (.all (iffF (m 0 2) (m 0 1))) (.all (.imp (m 2 0) (m 1 0)))))

/-- the sets are exactly the members: ∀X (M X ↔ ∃Y X∈Y) -/
def setAx : Fm := .all (iffF (Mv 0) (.ex (m 1 0)))

def σc : Nat → Nat
  | 0 => 0
  | n + 1 => n + 2

/-- predicative comprehension: ∃Y ∀x (M x → (x∈Y ↔ ψ)), parameters free -/
def compAx (p : PF) : Fm := .ex (.all (.imp (Mv 0) (iffF (m 0 1) (ren σc (emb p)))))

def pfA : List Nat → PF
  | [] => .bot
  | [_] => .bot
  | [a, b] => .mem a b
  | _ :: _ :: _ :: _ => .bot

/-- L0 formulas as predicative formulas (quantifiers over sets) -/
def pf : Fm → PF
  | .atom _ l => pfA l
  | .bot => .bot
  | .conj a b => .conj (pf a) (pf b)
  | .disj a b => .disj (pf a) (pf b)
  | .imp a b => .imp (pf a) (pf b)
  | .all a => .allS (pf a)
  | .ex a => .exS (pf a)

/-- relativisation to the sets -/
def relE (φ : Fm) : Fm := emb (pf φ)

section D

variable {D : Type} (R : Nat → List D → Prop)

/-! ## environments for valuations of D' -/

def sv (d0 : D) : D' D → D
  | .inl x => x
  | .inr _ => d0

def pv (d0 : D) : D' D → Nat → D
  | .inl _ => fun _ => d0
  | .inr c => c.2

def q0 (d0 : D) (w : Nat → D' D) (k : Nat) : D :=
  match K1.unpS k with
  | (0, n) => sv d0 (w n)
  | (a + 1, j) => pv d0 (w a) j

def kd (n : Nat) : D' D → Kd
  | .inl _ => .set (K1.pr 0 n)
  | .inr c => .cls c.1 (fun j => K1.pr (n + 1) j)

def env0 (w : Nat → D' D) (n : Nat) : Kd := kd n (w n)

theorem q0_set (d0 : D) (w : Nat → D' D) (n : Nat) : q0 d0 w (K1.pr 0 n) = sv d0 (w n) := by
  unfold q0
  rw [K1.unpS_pr]

theorem q0_par (d0 : D) (w : Nat → D' D) (n j : Nat) : q0 d0 w (K1.pr (n + 1) j) = pv d0 (w n) j := by
  unfold q0
  rw [K1.unpS_pr]

theorem cons_key (q : Nat → D) (d0 : D) (n : Nat) : (A : D' D) → q (K1.pr 0 n) = sv d0 A →
    (∀ j, q (K1.pr (n + 1) j) = pv d0 A j) → Ceq' R A (val q (kd n A))
  | .inl x, h1, _ => by
    show Ceq' R (.inl x) (.inl (q (K1.pr 0 n)))
    rw [h1]
    exact ceq'_refl R _
  | .inr c, _, h2 => fun u => sat_ext R c.1.1 (ext_ext (fun j => (h2 j).symm) u)

theorem cons_env0 (d0 : D) (w : Nat → D' D) : Cons R (env0 w) (q0 d0 w) w := fun n =>
  cons_key R (q0 d0 w) d0 n (w n) (q0_set d0 w n) (fun j => q0_par d0 w n j)

/-! ## comprehension -/

theorem comp (d0 : D) (p : PF) (w : Nat → D' D) :
    ∃ Y : D' D, ∀ X, IsM R X → (memb R X Y ↔ Sat (R' R) (emb p) (ext w X)) :=
  ⟨.inr (⟨tr (cons0 (env0 w)) p, l0_tr _ p⟩, q0 d0 w), fun X hX => match hX with
    | ⟨x0, hx0⟩ =>
      ⟨fun ⟨x, hx, hs⟩ => (truth R p _ (ext (q0 d0 w) x) (ext w X) (cons_ext R (cons_env0 R d0 w) hx)).1 hs,
       fun h => ⟨x0, hx0, (truth R p _ _ _ (cons_ext R (cons_env0 R d0 w) hx0)).2 h⟩⟩⟩

theorem compAx_sat (d0 : D) (p : PF) (w : Nat → D' D) : Sat (R' R) (compAx p) w :=
  match comp R d0 p w with
  | ⟨Y, hY⟩ => ⟨Y, fun X hX =>
    have e : Sat (R' R) (ren σc (emb p)) (ext (ext w Y) X) ↔ Sat (R' R) (emb p) (ext w X) :=
      (sat_ren (R' R) (emb p) σc _).trans (sat_ext (R' R) (emb p) (fun n => match n with
        | 0 => rfl
        | _ + 1 => rfl))
    ⟨fun h => e.2 ((hY X hX).1 h), fun h => (hY X hX).2 (e.1 h)⟩⟩

/-! ## relativisation -/

theorem ceq_inl_ext {w : Nat → D' D} {v : Nat → D} (h : ∀ n, Ceq' R (w n) (.inl (v n))) {X : D' D} {x : D}
    (hx : Ceq' R X (.inl x)) : ∀ n, Ceq' R (ext w X n) (.inl (ext v x n))
  | 0 => hx
  | n + 1 => h n

/-- the relativisation of an L0 formula holds on sets iff the formula holds in D -/
theorem rel_sat (hE : ExtD R) : (φ : Fm) → L0 φ → (w : Nat → D' D) → (v : Nat → D) →
    (∀ n, Ceq' R (w n) (.inl (v n))) → (Sat (R' R) (relE φ) w ↔ Sat R φ v)
  | .atom _ _, hl, w, v, h => match hl with
    | ⟨e0, a, b, e⟩ => by
      subst e0; subst e
      exact ⟨fun s => (memb_inl R hE (v a) (v b)).1 (memb_resp R (h a) (h b) s),
        fun s => memb_resp R (ceq'_symm R (h a)) (ceq'_symm R (h b)) ((memb_inl R hE (v a) (v b)).2 s)⟩
  | .bot, _, _, _, _ => Iff.rfl
  | .conj a b, hl, w, v, h => ⟨fun ⟨x, y⟩ => ⟨(rel_sat hE a hl.1 w v h).1 x, (rel_sat hE b hl.2 w v h).1 y⟩,
      fun ⟨x, y⟩ => ⟨(rel_sat hE a hl.1 w v h).2 x, (rel_sat hE b hl.2 w v h).2 y⟩⟩
  | .disj a b, hl, w, v, h => ⟨fun s => s.elim (fun x => Or.inl ((rel_sat hE a hl.1 w v h).1 x))
        (fun y => Or.inr ((rel_sat hE b hl.2 w v h).1 y)),
      fun s => s.elim (fun x => Or.inl ((rel_sat hE a hl.1 w v h).2 x))
        (fun y => Or.inr ((rel_sat hE b hl.2 w v h).2 y))⟩
  | .imp a b, hl, w, v, h => ⟨fun f x => (rel_sat hE b hl.2 w v h).1 (f ((rel_sat hE a hl.1 w v h).2 x)),
      fun f x => (rel_sat hE b hl.2 w v h).2 (f ((rel_sat hE a hl.1 w v h).1 x))⟩
  | .all a, hl, w, v, h =>
    ⟨fun f d => (rel_sat hE a hl (ext w (.inl d)) (ext v d) (ceq_inl_ext R h (ceq'_refl R _))).1
        (f (.inl d) (isM_inl R d)),
     fun f X hX => match hX with
      | ⟨x, hx⟩ => (rel_sat hE a hl (ext w X) (ext v x) (ceq_inl_ext R h hx)).2 (f x)⟩
  | .ex a, hl, w, v, h =>
    ⟨fun ⟨X, hX, s⟩ => match hX with
      | ⟨x, hx⟩ => ⟨x, (rel_sat hE a hl (ext w X) (ext v x) (ceq_inl_ext R h hx)).1 s⟩,
     fun ⟨d, s⟩ => ⟨.inl d, isM_inl R d,
        (rel_sat hE a hl (ext w (.inl d)) (ext v d) (ceq_inl_ext R h (ceq'_refl R _))).2 s⟩⟩

/-! ## extensionality and sets -/

/-- in D, the extensionality axiom is Leibniz extensionality -/
theorem extAx_D {v : Nat → D} (h : Sat R extAx v) : ExtD R :=
  fun x y z hc hm => h x y (fun u => ⟨(hc u).1, (hc u).2⟩) z hm

theorem ceq'_of (hE : ExtD R) {X Y : D' D} (h : ∀ U, memb R U X ↔ memb R U Y) : Ceq' R X Y := fun u =>
  ⟨fun e => (memb_inl_l R hE u Y).1 ((h _).1 ((memb_inl_l R hE u X).2 e)),
   fun e => (memb_inl_l R hE u X).1 ((h _).2 ((memb_inl_l R hE u Y).2 e))⟩

/-- class extensionality in D' -/
theorem extAx_D' (hE : ExtD R) (w : Nat → D' D) : Sat (R' R) extAx w :=
  fun _ _ h Z hm => memb_resp R (ceq'_of R hE (fun U => ⟨(h U).1, (h U).2⟩)) (ceq'_refl R Z) hm

/-- the universal class -/
def univ (d0 : D) : D' D := .inr (⟨topF, l0_top⟩, fun _ => d0)

theorem setAx_D' (d0 : D) (w : Nat → D' D) : Sat (R' R) setAx w := fun _ =>
  ⟨fun hX => match hX with
    | ⟨x, hx⟩ => ⟨univ d0, x, hx, fun f => f⟩,
   fun ⟨_, hm⟩ => memb_isM R hm⟩

end D

end NBGc

end Found

#print axioms Found.NBGc.q0_set
#print axioms Found.NBGc.q0_par
#print axioms Found.NBGc.cons_key
#print axioms Found.NBGc.cons_env0
#print axioms Found.NBGc.comp
#print axioms Found.NBGc.compAx_sat
#print axioms Found.NBGc.rel_sat
#print axioms Found.NBGc.extAx_D
#print axioms Found.NBGc.ceq'_of
#print axioms Found.NBGc.extAx_D'
#print axioms Found.NBGc.setAx_D'

found/NbgZ4.lean

lean_all/found/NbgZ4.lean

/-
  T91, NBG over ZF, part 4. found/NbgZ4 — the class axioms of replacement
  and foundation in the class structure D':
  * free variables below k (FV) and agreement: a formula only sees the
    values of its free variables; relativised L0 sentences true in D are
    true in D' under every valuation;
  * the Kuratowski pair p = ⟨a, b⟩ as an L0 formula;
  * ZF replacement scheme (for every L0 formula θ(w, z, parameters): a
    θ-functional relation on a set has a set image) and ZF foundation scheme
    (∈-minimal elements of every nonempty definable collection);
  * NBG class replacement (the image of a set under a functional class of
    pairs is a set) and class foundation hold in D' whenever the schemes
    hold in D.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.NbgZ3

namespace Found

namespace NBGc

open FO FOCT

/-! ## free variables -/

def AllLt (k : Nat) : List Nat → Prop
  | [] => True
  | a :: l => a < k ∧ AllLt k l

/-- all free variables are below k -/
def FV : Nat → Fm → Prop
  | k, .atom _ l => AllLt k l
  | _, .bot => True
  | k, .conj a b => FV k a ∧ FV k b
  | k, .disj a b => FV k a ∧ FV k b
  | k, .imp a b => FV k a ∧ FV k b
  | k, .all a => FV (k + 1) a
  | k, .ex a => FV (k + 1) a

theorem mapD_agree {D : Type} {k : Nat} {v w : Nat → D} (h : ∀ n, n < k → v n = w n) :
    (l : List Nat) → AllLt k l → mapD v l = mapD w l
  | [], _ => rfl
  | a :: l, hl => by
    show v a :: mapD v l = w a :: mapD w l
    rw [h a hl.1, mapD_agree h l hl.2]

theorem ext_agree {D : Type} {k : Nat} {v w : Nat → D} (h : ∀ n, n < k → v n = w n) (d : D) :
    ∀ n, n < k + 1 → ext v d n = ext w d n
  | 0, _ => rfl
  | n + 1, hn => h n (Nat.lt_of_succ_lt_succ hn)

theorem sat_agree {D : Type} (R : Nat → List D → Prop) : (φ : Fm) → (k : Nat) → FV k φ →
    {v w : Nat → D} → (∀ n, n < k → v n = w n) → (Sat R φ v ↔ Sat R φ w)
  | .atom r l, _, hf, _, _, h => by
    show R r (mapD _ l) ↔ R r (mapD _ l)
    rw [mapD_agree h l hf]
  | .bot, _, _, _, _, _ => Iff.rfl
  | .conj a b, k, hf, _, _, h => ⟨fun ⟨x, y⟩ => ⟨(sat_agree R a k hf.1 h).1 x, (sat_agree R b k hf.2 h).1 y⟩,
      fun ⟨x, y⟩ => ⟨(sat_agree R a k hf.1 h).2 x, (sat_agree R b k hf.2 h).2 y⟩⟩
  | .disj a b, k, hf, _, _, h => ⟨fun s => s.elim (fun x => Or.inl ((sat_agree R a k hf.1 h).1 x))
        (fun y => Or.inr ((sat_agree R b k hf.2 h).1 y)),
      fun s => s.elim (fun x => Or.inl ((sat_agree R a k hf.1 h).2 x))
        (fun y => Or.inr ((sat_agree R b k hf.2 h).2 y))⟩
  | .imp a b, k, hf, _, _, h => ⟨fun f x => (sat_agree R b k hf.2 h).1 (f ((sat_agree R a k hf.1 h).2 x)),
      fun f x => (sat_agree R b k hf.2 h).2 (f ((sat_agree R a k hf.1 h).1 x))⟩
  | .all a, k, hf, _, _, h => ⟨fun f d => (sat_agree R a (k + 1) hf (ext_agree h d)).1 (f d),
      fun f d => (sat_agree R a (k + 1) hf (ext_agree h d)).2 (f d)⟩
  | .ex a, k, hf, _, _, h => ⟨fun ⟨d, x⟩ => ⟨d, (sat_agree R a (k + 1) hf (ext_agree h d)).1 x⟩,
      fun ⟨d, x⟩ => ⟨d, (sat_agree R a (k + 1) hf (ext_agree h d)).2 x⟩⟩

theorem fv_pfA (k : Nat) : (l : List Nat) → AllLt k l → FV k (emb (pfA l))
  | [], _ => trivial
  | [_], _ => trivial
  | [_, _], h => h
  | _ :: _ :: _ :: _, _ => trivial

theorem fv_rel : (φ : Fm) → (k : Nat) → FV k φ → FV k (relE φ)
  | .atom _ l, k, h => fv_pfA k l h
  | .bot, _, _ => trivial
  | .conj a b, k, h => ⟨fv_rel a k h.1, fv_rel b k h.2⟩
  | .disj a b, k, h => ⟨fv_rel a k h.1, fv_rel b k h.2⟩
  | .imp a b, k, h => ⟨fv_rel a k h.1, fv_rel b k h.2⟩
  | .all a, k, h => ⟨⟨Nat.zero_lt_succ k, trivial⟩, fv_rel a (k + 1) h⟩
  | .ex a, k, h => ⟨⟨Nat.zero_lt_succ k, trivial⟩, fv_rel a (k + 1) h⟩

/-! ## formulas: pairs, replacement, foundation -/

theorem l0_m (a b : Nat) : L0 (m a b) := ⟨rfl, a, b, rfl⟩

theorem l0_iff {a b : Fm} (ha : L0 a) (hb : L0 b) : L0 (iffF a b) := ⟨⟨ha, hb⟩, ⟨hb, ha⟩⟩

/-- "i ≈ j": coextension at the current depth -/
def EqF (i j : Nat) : Fm := .all (iffF (m 0 (i + 1)) (m 0 (j + 1)))

theorem l0_eqF (i j : Nat) : L0 (EqF i j) := l0_iff (l0_m _ _) (l0_m _ _)

/-- the Kuratowski pair: variable 0 = {{1}, {1, 2}} -/
def OPz : Fm := .all (iffF (m 0 1) (.disj (.all (iffF (m 0 1) (EqF 0 3)))
  (.all (iffF (m 0 1) (.disj (EqF 0 3) (EqF 0 4))))))

theorem l0_OPz : L0 OPz :=
  l0_iff (l0_m _ _) ⟨l0_iff (l0_m _ _) (l0_eqF _ _), l0_iff (l0_m _ _) ⟨l0_eqF _ _, l0_eqF _ _⟩⟩

def opσ (a b c : Nat) : Nat → Nat
  | 0 => a
  | 1 => b
  | 2 => c
  | _ + 3 => a

def tri {α : Type} (p a b : α) : Nat → α
  | 0 => p
  | 1 => a
  | 2 => b
  | _ + 3 => p

def r3 (a b : Nat) : Nat → Nat
  | 0 => a
  | 1 => b
  | k + 2 => k + 4

def σ3 : Nat → Nat
  | 0 => 0
  | k + 1 => k + 3

/-- ZF replacement for θ(w, z, parameters):
  ∀x (∀w (w∈x → ∀z ∀z' (θ(w,z) ∧ θ(w,z') → z ≈ z')) → ∃y ∀z (z∈y ↔ ∃w (w∈x ∧ θ(w,z)))) -/
def replZ (θ : Fm) : Fm :=
  .all (.imp (.all (.imp (m 0 1) (.all (.all (.imp (.conj (ren (r3 2 1) θ) (ren (r3 2 0) θ))
      (.all (iffF (m 0 2) (m 0 1))))))))
    (.ex (.all (iffF (m 0 1) (.ex (.conj (m 0 3) (ren (r3 0 1) θ)))))))

/-- ZF foundation for θ(x, parameters): ∃x θ(x) → ∃x (θ(x) ∧ ∀y (y∈x → ¬θ(y))) -/
def foundZ (θ : Fm) : Fm :=
  .imp (.ex θ) (.ex (.conj θ (.all (.imp (m 0 1) (.imp (ren (up Nat.succ) θ) .bot)))))

/-- "⟨w, z⟩ ∈ f" in the class language -/
def PIN (w z f : Nat) : Fm := .ex (.conj (Mv 0) (.conj (ren (opσ 0 (w + 1) (z + 1)) (relE OPz)) (m 0 (f + 1))))

/-- F is a function on sets -/
def FunN : Fm := .all (.imp (Mv 0) (.all (.imp (Mv 0) (.all (.imp (Mv 0) (.imp (.conj (PIN 2 1 3) (PIN 2 0 3))
  (.all (.imp (Mv 0) (iffF (m 0 2) (m 0 1))))))))))

/-- the image of every set under F is a set -/
def ImgN : Fm := .all (.imp (Mv 0) (.ex (.conj (Mv 0) (.all (.imp (Mv 0) (iffF (m 0 1)
  (.ex (.conj (Mv 0) (.conj (m 0 3) (PIN 0 1 4))))))))))

/-- NBG class replacement -/
def creplAx : Fm := .all (.imp FunN ImgN)

/-- NBG class foundation: ∀X (∃x (M x ∧ x∈X) → ∃x (M x ∧ x∈X ∧ ∀y (M y → y∈x → ¬ y∈X))) -/
def cfoundAx : Fm := .all (.imp (.ex (.conj (Mv 0) (m 0 1)))
  (.ex (.conj (Mv 0) (.conj (m 0 1) (.all (.imp (Mv 0) (.imp (m 0 1) (.imp (m 0 2) .bot))))))))

/-- the formula θ_χ(w, z) = ∃p (p = ⟨w, z⟩ ∧ χ(p)) -/
def thR (χ : Fm) : Fm := .ex (.conj (ren (opσ 0 1 2) OPz) (ren σ3 χ))

theorem l0_thR {χ : Fm} (h : L0 χ) : L0 (thR χ) := ⟨l0_ren _ OPz l0_OPz, l0_ren σ3 χ h⟩

section D

variable {D : Type} (R : Nat → List D → Prop)

/-- a relativised L0 sentence true in D is true in D' -/
theorem base_D' (hE : ExtD R) (d0 : D) {β : Fm} (hl : L0 β) (hf : FV 0 β) (hβ : ∀ v, Sat R β v)
    (w : Nat → D' D) : Sat (R' R) (relE β) w :=
  (sat_agree (R' R) (relE β) 0 (fv_rel β 0 hf) (v := w) (w := fun _ => .inl d0)
    (fun n hn => absurd hn (Nat.not_lt_zero n))).2
    ((rel_sat R hE β hl (fun _ => .inl d0) (fun _ => d0) (fun _ => ceq'_refl R _)).2 (hβ _))

/-- every element of D' is a definable class -/
theorem clsOf : (F : D' D) → ∃ χ : Fm, L0 χ ∧ ∃ pF : Nat → D, ∀ u, ext' R F u ↔ Sat R χ (ext pF u)
  | .inl f => ⟨m 0 1, l0_m 0 1, fun _ => f, fun _ => Iff.rfl⟩
  | .inr c => ⟨c.1.1, c.1.2, c.2, fun _ => Iff.rfl⟩

def OPd (P W Z : D' D) : Prop := Sat (R' R) (relE OPz) (tri P W Z)

def OPs (p a b : D) : Prop := Sat R OPz (tri p a b)

theorem op_sets (hE : ExtD R) {P W Z : D' D} {p a b : D} (hP : Ceq' R P (.inl p)) (hW : Ceq' R W (.inl a))
    (hZ : Ceq' R Z (.inl b)) : OPd R P W Z ↔ OPs R p a b :=
  rel_sat R hE OPz l0_OPz (tri P W Z) (tri p a b) (fun n => match n with
    | 0 => hP
    | 1 => hW
    | 2 => hZ
    | _ + 3 => hP)

theorem PIN_sat (v : Nat → D' D) (w z f : Nat) :
    Sat (R' R) (PIN w z f) v ↔ ∃ P, IsM R P ∧ OPd R P (v w) (v z) ∧ memb R P (v f) :=
  have e : ∀ P, Sat (R' R) (ren (opσ 0 (w + 1) (z + 1)) (relE OPz)) (ext v P) ↔ OPd R P (v w) (v z) := fun _ =>
    (sat_ren (R' R) (relE OPz) _ _).trans (sat_ext (R' R) (relE OPz) (fun n => match n with
      | 0 => rfl
      | 1 => rfl
      | 2 => rfl
      | _ + 3 => rfl))
  ⟨fun ⟨P, h1, h2, h3⟩ => ⟨P, h1, (e P).1 h2, h3⟩, fun ⟨P, h1, h2, h3⟩ => ⟨P, h1, (e P).2 h2, h3⟩⟩

/-- for sets, pair membership in a class is the formula θ_χ -/
theorem pi_sets (hE : ExtD R) {F : D' D} {χ : Fm} {pF : Nat → D} (hF : ∀ u, ext' R F u ↔ Sat R χ (ext pF u))
    {W Z : D' D} {a b : D} (hW : Ceq' R W (.inl a)) (hZ : Ceq' R Z (.inl b)) :
    (∃ P, IsM R P ∧ OPd R P W Z ∧ memb R P F) ↔ ∃ p, OPs R p a b ∧ Sat R χ (ext pF p) :=
  ⟨fun ⟨_, hP, ho, hm⟩ => match hP with
    | ⟨p, hp⟩ => ⟨p, (op_sets R hE hp hW hZ).1 ho,
        (hF p).1 ((memb_inl_l R hE p F).1 (memb_resp R hp (ceq'_refl R F) hm))⟩,
   fun ⟨p, ho, hs⟩ => ⟨.inl p, isM_inl R p, (op_sets R hE (ceq'_refl R _) hW hZ).2 ho,
     (memb_inl_l R hE p F).2 ((hF p).2 hs)⟩⟩

theorem thR_sat (χ : Fm) (pF : Nat → D) (w z : D) :
    Sat R (thR χ) (ext (ext pF z) w) ↔ ∃ p, OPs R p w z ∧ Sat R χ (ext pF p) :=
  have e1 : ∀ p, Sat R (ren (opσ 0 1 2) OPz) (ext (ext (ext pF z) w) p) ↔ OPs R p w z := fun _ =>
    (sat_ren R OPz _ _).trans (sat_ext R OPz (fun n => match n with
      | 0 => rfl
      | 1 => rfl
      | 2 => rfl
      | _ + 3 => rfl))
  have e2 : ∀ p, Sat R (ren σ3 χ) (ext (ext (ext pF z) w) p) ↔ Sat R χ (ext pF p) := fun _ =>
    (sat_ren R χ _ _).trans (sat_ext R χ (fun n => match n with
      | 0 => rfl
      | _ + 1 => rfl))
  ⟨fun ⟨p, h1, h2⟩ => ⟨p, (e1 p).1 h1, (e2 p).1 h2⟩, fun ⟨p, h1, h2⟩ => ⟨p, (e1 p).2 h1, (e2 p).2 h2⟩⟩

/-- what the replacement scheme says in D -/
theorem replZ_use (θ : Fm) (pF : Nat → D) (h : Sat R (replZ θ) pF) (x : D)
    (pre : ∀ w, mem R w x → ∀ z z', Sat R θ (ext (ext pF z) w) → Sat R θ (ext (ext pF z') w) → Ceq R z z') :
    ∃ y, ∀ z, mem R z y ↔ ∃ w, mem R w x ∧ Sat R θ (ext (ext pF z) w) :=
  have eA : ∀ w z z', Sat R (ren (r3 2 1) θ) (ext (ext (ext (ext pF x) w) z) z') ↔ Sat R θ (ext (ext pF z) w) :=
    fun _ _ _ => (sat_ren R θ _ _).trans (sat_ext R θ (fun n => match n with
      | 0 => rfl
      | 1 => rfl
      | _ + 2 => rfl))
  have eB : ∀ w z z', Sat R (ren (r3 2 0) θ) (ext (ext (ext (ext pF x) w) z) z') ↔ Sat R θ (ext (ext pF z') w) :=
    fun _ _ _ => (sat_ren R θ _ _).trans (sat_ext R θ (fun n => match n with
      | 0 => rfl
      | 1 => rfl
      | _ + 2 => rfl))
  have eC : ∀ y z w, Sat R (ren (r3 0 1) θ) (ext (ext (ext (ext pF x) y) z) w) ↔ Sat R θ (ext (ext pF z) w) :=
    fun _ _ _ => (sat_ren R θ _ _).trans (sat_ext R θ (fun n => match n with
      | 0 => rfl
      | 1 => rfl
      | _ + 2 => rfl))
  match h x (fun w hw z z' hc u =>
      have c := pre w hw z z' ((eA w z z').1 hc.1) ((eB w z z').1 hc.2) u
      ⟨c.1, c.2⟩) with
  | ⟨y, hy⟩ => ⟨y, fun z =>
    ⟨fun hz => match (hy z).1 hz with
      | ⟨w, h1, h2⟩ => ⟨w, h1, (eC y z w).1 h2⟩,
     fun ⟨w, h1, h2⟩ => (hy z).2 ⟨w, h1, (eC y z w).2 h2⟩⟩⟩

/-- class replacement holds in D' -/
theorem creplAx_D' (hE : ExtD R) (hRepl : ∀ χ, L0 χ → ∀ v, Sat R (replZ (thR χ)) v) (w : Nat → D' D) :
    Sat (R' R) creplAx w := fun F hFun _ hX =>
  match clsOf R F, hX with
  | ⟨χ, hχ, pF, hF⟩, ⟨x, hx⟩ =>
    have pre : ∀ a, mem R a x → ∀ z z', Sat R (thR χ) (ext (ext pF z) a) → Sat R (thR χ) (ext (ext pF z') a) →
        Ceq R z z' := fun a _ z z' h1 h2 u =>
      have p1 := (pi_sets R hE hF (ceq'_refl R (.inl a)) (ceq'_refl R (.inl z))).2 ((thR_sat R χ pF a z).1 h1)
      have p2 := (pi_sets R hE hF (ceq'_refl R (.inl a)) (ceq'_refl R (.inl z'))).2 ((thR_sat R χ pF a z').1 h2)
      have c := hFun (.inl a) (isM_inl R a) (.inl z) (isM_inl R z) (.inl z') (isM_inl R z')
        ⟨(PIN_sat R _ 2 1 3).2 p1, (PIN_sat R _ 2 0 3).2 p2⟩ (.inl u) (isM_inl R u)
      ⟨fun e => (memb_inl R hE u z').1 (c.1 ((memb_inl R hE u z).2 e)),
       fun e => (memb_inl R hE u z).1 (c.2 ((memb_inl R hE u z').2 e))⟩
    match replZ_use R (thR χ) pF (hRepl χ hχ pF) x pre with
    | ⟨y, hy⟩ => ⟨.inl y, isM_inl R y, fun _ hZ => match hZ with
      | ⟨z, hz⟩ =>
        ⟨fun hm => match (hy z).1 ((memb_inl R hE z y).1 (memb_resp R hz (ceq'_refl R _) hm)) with
          | ⟨a, ha, ht⟩ => ⟨.inl a, isM_inl R a,
              memb_resp R (ceq'_refl R _) (ceq'_symm R hx) ((memb_inl R hE a x).2 ha),
              (PIN_sat R _ 0 1 4).2 ((pi_sets R hE hF (ceq'_refl R _) hz).2 ((thR_sat R χ pF a z).1 ht))⟩,
         fun ⟨_, hW, hWX, hpi⟩ => match hW with
          | ⟨a, ha⟩ =>
            have ax := (memb_inl R hE a x).1 (memb_resp R ha hx hWX)
            have ht := (thR_sat R χ pF a z).2 ((pi_sets R hE hF ha hz).1 ((PIN_sat R _ 0 1 4).1 hpi))
            memb_resp R (ceq'_symm R hz) (ceq'_refl R _) ((memb_inl R hE z y).2 ((hy z).2 ⟨a, ax, ht⟩))⟩⟩

/-- what the foundation scheme says in D -/
theorem foundZ_use (θ : Fm) (pF : Nat → D) (h : Sat R (foundZ θ) pF) (hx : ∃ x, Sat R θ (ext pF x)) :
    ∃ x, Sat R θ (ext pF x) ∧ ∀ y, mem R y x → ¬ Sat R θ (ext pF y) :=
  have e : ∀ x y, Sat R (ren (up Nat.succ) θ) (ext (ext pF x) y) ↔ Sat R θ (ext pF y) := fun _ _ =>
    (sat_ren R θ _ _).trans (sat_ext R θ (fun n => match n with
      | 0 => rfl
      | _ + 1 => rfl))
  match h hx with
  | ⟨x, h1, h2⟩ => ⟨x, h1, fun y hy ht => h2 y hy ((e x y).2 ht)⟩

/-- class foundation holds in D' -/
theorem cfoundAx_D' (hE : ExtD R) (hF : ∀ χ, L0 χ → ∀ v, Sat R (foundZ χ) v) (w : Nat → D' D) :
    Sat (R' R) cfoundAx w := fun X hne =>
  match clsOf R X, hne with
  | ⟨χ, hχ, pF, hX⟩, ⟨_, hM, hm⟩ => match hM with
    | ⟨x, hx⟩ =>
      match foundZ_use R χ pF (hF χ hχ pF)
          ⟨x, (hX x).1 ((memb_inl_l R hE x X).1 (memb_resp R hx (ceq'_refl R X) hm))⟩ with
      | ⟨x0, h0, hmin⟩ => ⟨.inl x0, isM_inl R x0, (memb_inl_l R hE x0 X).2 ((hX x0).2 h0),
          fun _ hY hYx hYX => match hY with
          | ⟨y, hy⟩ => hmin y ((memb_inl R hE y x0).1 (memb_resp R hy (ceq'_refl R _) hYx))
              ((hX y).1 ((memb_inl_l R hE y X).1 (memb_resp R hy (ceq'_refl R X) hYX)))⟩

end D

end NBGc

end Found

#print axioms Found.NBGc.sat_agree
#print axioms Found.NBGc.fv_rel
#print axioms Found.NBGc.l0_OPz
#print axioms Found.NBGc.l0_thR
#print axioms Found.NBGc.base_D'
#print axioms Found.NBGc.clsOf
#print axioms Found.NBGc.op_sets
#print axioms Found.NBGc.PIN_sat
#print axioms Found.NBGc.pi_sets
#print axioms Found.NBGc.thR_sat
#print axioms Found.NBGc.replZ_use
#print axioms Found.NBGc.creplAx_D'
#print axioms Found.NBGc.foundZ_use
#print axioms Found.NBGc.cfoundAx_D'

found/NbgZ5.lean

lean_all/found/NbgZ5.lean

/-
  T91, NBG over ZF, part 5. found/NbgZ5 — conservativity of NBG over ZF
  (in the classical point, through the completeness theorem for infinite
  theories, found/FOClassicalT):
  * ZF(B): Leibniz extensionality, the base axioms B (L0 sentences), the
    replacement scheme and the foundation scheme;
  * NBG(B) in the language ∈, M (M = "is a set"): class extensionality,
    "the sets are the members", the relativised base axioms, predicative
    class comprehension (scheme), class replacement and class foundation;
  * every model D of ZF(B) gives the model D' of NBG(B) whose sets are D and
    whose classes are the definable classes (found/NbgZ1–4);
  * conservativity: for every ZF formula φ, if NBG(B) derives φ relativised
    to sets, then ZF(B) derives φ;
  * the base B0 = pairing, union, power set, infinity, checked to be L0
    sentences by computation.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.NbgZ4

namespace Found

namespace NBGc

open FO FOCT

/-- ZF over base axioms B -/
def ZF (B : Fm → Prop) (φ : Fm) : Prop :=
  φ = extAx ∨ B φ ∨ (∃ θ, L0 θ ∧ φ = replZ θ) ∨ (∃ θ, L0 θ ∧ φ = foundZ θ)

/-- NBG over the same base -/
def NBG (B : Fm → Prop) (φ : Fm) : Prop :=
  φ = extAx ∨ φ = setAx ∨ (∃ β, B β ∧ φ = relE β) ∨ (∃ p, φ = compAx p) ∨ φ = creplAx ∨ φ = cfoundAx

/-- the base axioms are sentences of the language of ZF -/
def Base (B : Fm → Prop) : Prop := ∀ β, B β → L0 β ∧ FV 0 β

/-- every model of ZF(B) carries the class model of NBG(B) -/
theorem model_D' {B : Fm → Prop} (hB : Base B) {D : Type} (R : Nat → List D → Prop) (hM : ModT (ZF B) R)
    (d0 : D) : ModT (NBG B) (R' R) := fun φ hφ w =>
  have hE : ExtD R := extAx_D R (hM extAx (Or.inl rfl) (fun _ => d0))
  match hφ with
  | .inl e => by rw [e]; exact extAx_D' R hE w
  | .inr (.inl e) => by rw [e]; exact setAx_D' R d0 w
  | .inr (.inr (.inl ⟨β, hb, e⟩)) => by
    rw [e]
    exact base_D' R hE d0 (hB β hb).1 (hB β hb).2 (fun v => hM β (Or.inr (Or.inl hb)) v) w
  | .inr (.inr (.inr (.inl ⟨p, e⟩))) => by rw [e]; exact compAx_sat R d0 p w
  | .inr (.inr (.inr (.inr (.inl e)))) => by
    rw [e]
    exact creplAx_D' R hE (fun χ hχ v => hM _ (Or.inr (Or.inr (Or.inl ⟨thR χ, l0_thR hχ, rfl⟩))) v) w
  | .inr (.inr (.inr (.inr (.inr e)))) => by
    rw [e]
    exact cfoundAx_D' R hE (fun χ hχ v => hM _ (Or.inr (Or.inr (Or.inr ⟨χ, hχ, rfl⟩))) v) w

/-- NBG is conservative over ZF -/
theorem nbg_conservative (K : CP.Classical') {B : Fm → Prop} (hB : Base B) {φ : Fm} (hl : L0 φ)
    (h : PC (NBG B) [] (relE φ)) : PC (ZF B) [] φ :=
  completenessT (ZF B) K (fun D R v hM _ =>
    have hE : ExtD R := extAx_D R (hM extAx (Or.inl rfl) v)
    (rel_sat R hE φ hl (fun n => .inl (v n)) v (fun _ => ceq'_refl R _)).1
      (soundnessT (NBG B) K h (D' D) (R' R) (fun n => .inl (v n)) (model_D' hB R hM (v 0))
        (fun _ m => nomatch m)))

/-! ## the base: pairing, union, power set, infinity -/

def pairAx : Fm := .all (.all (.ex (.all (iffF (m 0 1) (.disj (EqF 0 3) (EqF 0 2))))))
def unionAx : Fm := .all (.ex (.all (iffF (m 0 1) (.ex (.conj (m 0 3) (m 1 0))))))
def powerAx : Fm := .all (.ex (.all (iffF (m 0 1) (.all (.imp (m 0 1) (m 0 3))))))
def infAx : Fm := .ex (.conj (.ex (.conj (m 0 1) (.all (.imp (m 0 1) .bot))))
  (.all (.imp (m 0 1) (.ex (.conj (m 0 2) (.all (iffF (m 0 1) (.disj (m 0 2) (EqF 0 2)))))))))

def B0 (φ : Fm) : Prop := φ = pairAx ∨ φ = unionAx ∨ φ = powerAx ∨ φ = infAx

/-! ## checking L0 and closedness by computation -/

def pairB : List Nat → Bool
  | [] => false
  | [_] => false
  | [_, _] => true
  | _ :: _ :: _ :: _ => false

def l0B : Fm → Bool
  | .atom r l => Nat.beq r 0 && pairB l
  | .bot => true
  | .conj a b => l0B a && l0B b
  | .disj a b => l0B a && l0B b
  | .imp a b => l0B a && l0B b
  | .all a => l0B a
  | .ex a => l0B a

def ltB (k : Nat) : List Nat → Bool
  | [] => true
  | a :: l => Nat.ble (a + 1) k && ltB k l

def fvB : Nat → Fm → Bool
  | k, .atom _ l => ltB k l
  | _, .bot => true
  | k, .conj a b => fvB k a && fvB k b
  | k, .disj a b => fvB k a && fvB k b
  | k, .imp a b => fvB k a && fvB k b
  | k, .all a => fvB (k + 1) a
  | k, .ex a => fvB (k + 1) a

theorem andB : (a b : Bool) → (a && b) = true → a = true ∧ b = true
  | true, true, _ => ⟨rfl, rfl⟩
  | true, false, h => Bool.noConfusion h
  | false, _, h => Bool.noConfusion h

theorem pairB_ok : (l : List Nat) → pairB l = true → ∃ a b, l = [a, b]
  | [], h => Bool.noConfusion h
  | [_], h => Bool.noConfusion h
  | [a, b], _ => ⟨a, b, rfl⟩
  | _ :: _ :: _ :: _, h => Bool.noConfusion h

theorem l0B_ok : (φ : Fm) → l0B φ = true → L0 φ
  | .atom _ l, h => ⟨Nat.eq_of_beq_eq_true (andB _ _ h).1, pairB_ok l (andB _ _ h).2⟩
  | .bot, _ => trivial
  | .conj a b, h => ⟨l0B_ok a (andB _ _ h).1, l0B_ok b (andB _ _ h).2⟩
  | .disj a b, h => ⟨l0B_ok a (andB _ _ h).1, l0B_ok b (andB _ _ h).2⟩
  | .imp a b, h => ⟨l0B_ok a (andB _ _ h).1, l0B_ok b (andB _ _ h).2⟩
  | .all a, h => l0B_ok a h
  | .ex a, h => l0B_ok a h

theorem ltB_ok (k : Nat) : (l : List Nat) → ltB k l = true → AllLt k l
  | [], _ => trivial
  | _ :: l, h => ⟨Nat.le_of_ble_eq_true (andB _ _ h).1, ltB_ok k l (andB _ _ h).2⟩

theorem fvB_ok : (k : Nat) → (φ : Fm) → fvB k φ = true → FV k φ
  | k, .atom _ l, h => ltB_ok k l h
  | _, .bot, _ => trivial
  | k, .conj a b, h => ⟨fvB_ok k a (andB _ _ h).1, fvB_ok k b (andB _ _ h).2⟩
  | k, .disj a b, h => ⟨fvB_ok k a (andB _ _ h).1, fvB_ok k b (andB _ _ h).2⟩
  | k, .imp a b, h => ⟨fvB_ok k a (andB _ _ h).1, fvB_ok k b (andB _ _ h).2⟩
  | k, .all a, h => fvB_ok (k + 1) a h
  | k, .ex a, h => fvB_ok (k + 1) a h

theorem sentence_ok (φ : Fm) (h1 : l0B φ = true) (h2 : fvB 0 φ = true) : L0 φ ∧ FV 0 φ :=
  ⟨l0B_ok φ h1, fvB_ok 0 φ h2⟩

theorem base_B0 : Base B0 := fun β hb => match hb with
  | .inl e => by rw [e]; exact sentence_ok pairAx rfl rfl
  | .inr (.inl e) => by rw [e]; exact sentence_ok unionAx rfl rfl
  | .inr (.inr (.inl e)) => by rw [e]; exact sentence_ok powerAx rfl rfl
  | .inr (.inr (.inr e)) => by rw [e]; exact sentence_ok infAx rfl rfl

/-- NBG is conservative over ZF (base: pairing, union, power set, infinity) -/
theorem nbg_zf (K : CP.Classical') {φ : Fm} (hl : L0 φ) (h : PC (NBG B0) [] (relE φ)) : PC (ZF B0) [] φ :=
  nbg_conservative K base_B0 hl h

end NBGc

end Found

#print axioms Found.NBGc.model_D'
#print axioms Found.NBGc.nbg_conservative
#print axioms Found.NBGc.andB
#print axioms Found.NBGc.l0B_ok
#print axioms Found.NBGc.fvB_ok
#print axioms Found.NBGc.base_B0
#print axioms Found.NBGc.nbg_zf

found/NbgZ6.lean

lean_all/found/NbgZ6.lean

/-
  T91, NBG over ZF, part 6. found/NbgZ6 — ZFC, and the sets of a model of
  NBG:
  * ZFC: the base B1 = pairing, union, power set, infinity, choice (every set
    of nonempty pairwise disjoint sets has a choice set); NBG(B1) is
    conservative over ZF(B1) (nbg_zfc);
  * in any structure for the language ∈, M, the sets (elements with M) form
    a structure for ∈, and the relativisation of an L0 formula holds on sets
    iff the formula holds in the structure of sets;
  * Kuratowski pairs in a structure satisfying pairing: every two sets have
    a pair, and pairs determine their components up to coextension.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.NbgZ5

namespace Found

namespace NBGc

open FO FOCT

/-! ## ZFC -/

/-- choice: ∀a ((∀x∈a ∃t t∈x) → (∀x∈a ∀y∈a (x ≈ y ∨ ¬∃t (t∈x ∧ t∈y))) →
  ∃c ∀x∈a ∃u (u∈x ∧ u∈c ∧ ∀s (s∈x → s∈c → s ≈ u))) -/
def acAx : Fm := .all (.imp (.all (.imp (m 0 1) (.ex (m 0 1))))
  (.imp (.all (.all (.imp (m 1 2) (.imp (m 0 2) (.disj (EqF 1 0) (.imp (.ex (.conj (m 0 2) (m 0 1))) .bot))))))
    (.ex (.all (.imp (m 0 2) (.ex (.conj (m 0 1) (.conj (m 0 2)
      (.all (.imp (m 0 2) (.imp (m 0 3) (EqF 0 1))))))))))))

def B1 (φ : Fm) : Prop := B0 φ ∨ φ = acAx

theorem base_B1 : Base B1 := fun β hb => match hb with
  | .inl h => base_B0 β h
  | .inr e => by rw [e]; exact sentence_ok acAx rfl rfl

/-- NBG with choice is conservative over ZFC -/
theorem nbg_zfc (K : CP.Classical') {φ : Fm} (hl : L0 φ) (h : PC (NBG B1) [] (relE φ)) : PC (ZF B1) [] φ :=
  nbg_conservative K base_B1 hl h

/-! ## the structure of sets of a structure for ∈, M -/

section D

variable {D : Type} (R : Nat → List D → Prop)

abbrev St : Type := {x : D // R 1 [x]}

def valL : List (St R) → List D
  | [] => []
  | s :: l => s.1 :: valL l

def Rs (r : Nat) (l : List (St R)) : Prop := R r (valL R l)

theorem mapD_val {w : Nat → D} {v : Nat → St R} (h : ∀ n, w n = (v n).1) :
    (l : List Nat) → mapD w l = valL R (mapD v l)
  | [] => rfl
  | a :: l => by
    show w a :: mapD w l = (v a).1 :: valL R (mapD v l)
    rw [h a, mapD_val h l]

theorem ext_val {w : Nat → D} {v : Nat → St R} (h : ∀ n, w n = (v n).1) (s : St R) :
    ∀ n, ext w s.1 n = (ext v s n).1
  | 0 => rfl
  | n + 1 => h n

/-- the relativisation of an L0 formula holds on sets iff it holds in the structure of sets -/
theorem rel_back : (φ : Fm) → L0 φ → (w : Nat → D) → (v : Nat → St R) → (∀ n, w n = (v n).1) →
    (Sat R (relE φ) w ↔ Sat (Rs R) φ v)
  | .atom _ _, hl, w, v, h => match hl with
    | ⟨e0, a, b, e⟩ => by
      subst e0; subst e
      show R 0 (mapD w [a, b]) ↔ R 0 (valL R (mapD v [a, b]))
      rw [mapD_val R h [a, b]]
  | .bot, _, _, _, _ => Iff.rfl
  | .conj a b, hl, w, v, h => ⟨fun ⟨x, y⟩ => ⟨(rel_back a hl.1 w v h).1 x, (rel_back b hl.2 w v h).1 y⟩,
      fun ⟨x, y⟩ => ⟨(rel_back a hl.1 w v h).2 x, (rel_back b hl.2 w v h).2 y⟩⟩
  | .disj a b, hl, w, v, h => ⟨fun s => s.elim (fun x => Or.inl ((rel_back a hl.1 w v h).1 x))
        (fun y => Or.inr ((rel_back b hl.2 w v h).1 y)),
      fun s => s.elim (fun x => Or.inl ((rel_back a hl.1 w v h).2 x))
        (fun y => Or.inr ((rel_back b hl.2 w v h).2 y))⟩
  | .imp a b, hl, w, v, h => ⟨fun f x => (rel_back b hl.2 w v h).1 (f ((rel_back a hl.1 w v h).2 x)),
      fun f x => (rel_back b hl.2 w v h).2 (f ((rel_back a hl.1 w v h).1 x))⟩
  | .all a, hl, w, v, h =>
    ⟨fun f s => (rel_back a hl (ext w s.1) (ext v s) (ext_val R h s)).1 (f s.1 s.2),
     fun f X hX => (rel_back a hl (ext w X) (ext v ⟨X, hX⟩) (ext_val R h ⟨X, hX⟩)).2 (f ⟨X, hX⟩)⟩
  | .ex a, hl, w, v, h =>
    ⟨fun ⟨X, hX, s⟩ => ⟨⟨X, hX⟩, (rel_back a hl (ext w X) (ext v ⟨X, hX⟩) (ext_val R h ⟨X, hX⟩)).1 s⟩,
     fun ⟨s, x⟩ => ⟨s.1, s.2, (rel_back a hl (ext w s.1) (ext v s) (ext_val R h s)).2 x⟩⟩

/-- pair membership in an arbitrary structure -/
theorem PIN_gen (v : Nat → D) (w z f : Nat) :
    Sat R (PIN w z f) v ↔ ∃ P, R 1 [P] ∧ Sat R (relE OPz) (tri P (v w) (v z)) ∧ R 0 [P, v f] :=
  have e : ∀ P, Sat R (ren (opσ 0 (w + 1) (z + 1)) (relE OPz)) (ext v P) ↔ Sat R (relE OPz) (tri P (v w) (v z)) :=
    fun _ => (sat_ren R (relE OPz) _ _).trans (sat_ext R (relE OPz) (fun n => match n with
      | 0 => rfl
      | 1 => rfl
      | 2 => rfl
      | _ + 3 => rfl))
  ⟨fun ⟨P, h1, h2, h3⟩ => ⟨P, h1, (e P).1 h2, h3⟩, fun ⟨P, h1, h2, h3⟩ => ⟨P, h1, (e P).2 h2, h3⟩⟩

end D

/-! ## Kuratowski pairs -/

section E

variable {E : Type} (R : Nat → List E → Prop)

theorem ceq_trans {x y z : E} (h1 : Ceq R x y) (h2 : Ceq R y z) : Ceq R x z := fun u => (h1 u).trans (h2 u)

/-- the unfolded coextension of the formula EqF -/
def CeqC (x y : E) : Prop := ∀ s, (mem R s x → mem R s y) ∧ (mem R s y → mem R s x)

theorem c2 {x y : E} (h : CeqC R x y) : Ceq R x y := fun s => ⟨(h s).1, (h s).2⟩
theorem c1 {x y : E} (h : Ceq R x y) : CeqC R x y := fun s => ⟨(h s).1, (h s).2⟩

def Sing (u a : E) : Prop := ∀ t, mem R t u ↔ Ceq R t a
def Dbl (u a b : E) : Prop := ∀ t, mem R t u ↔ (Ceq R t a ∨ Ceq R t b)

theorem orC {A B A' B' : Prop} (f : A → A') (g : B → B') : A ∨ B → A' ∨ B' := fun h => h.elim (fun x => .inl (f x)) (fun y => .inr (g y))

/-- what the pair formula says -/
theorem op_sem (p a b : E) : OPs R p a b ↔ ∀ u, mem R u p ↔ (Sing R u a ∨ Dbl R u a b) :=
  ⟨fun h u => ⟨fun hm => orC
      (fun s t => ⟨fun x => c2 R ((s t).1 x), fun y => (s t).2 (c1 R y)⟩)
      (fun s t => ⟨fun x => orC (c2 R) (c2 R) ((s t).1 x), fun y => (s t).2 (orC (c1 R) (c1 R) y)⟩)
      ((h u).1 hm),
    fun hd => (h u).2 (orC
      (fun s t => ⟨fun x => c1 R ((s t).1 x), fun y => (s t).2 (c2 R y)⟩)
      (fun s t => ⟨fun x => orC (c1 R) (c1 R) ((s t).1 x), fun y => (s t).2 (orC (c2 R) (c2 R) y)⟩) hd)⟩,
   fun h u => ⟨fun hm => orC
      (fun s t => ⟨fun x => c1 R ((s t).1 x), fun y => (s t).2 (c2 R y)⟩)
      (fun s t => ⟨fun x => orC (c1 R) (c1 R) ((s t).1 x), fun y => (s t).2 (orC (c2 R) (c2 R) y)⟩)
      ((h u).1 hm),
    fun hd => (h u).2 (orC
      (fun s t => ⟨fun x => c2 R ((s t).1 x), fun y => (s t).2 (c1 R y)⟩)
      (fun s t => ⟨fun x => orC (c2 R) (c2 R) ((s t).1 x), fun y => (s t).2 (orC (c1 R) (c1 R) y)⟩) hd)⟩⟩

/-- pairing in a structure (the unfolded pairing axiom) -/
def Pairing : Prop := ∀ a b : E, ∃ c, ∀ u, mem R u c ↔ (Ceq R u a ∨ Ceq R u b)

theorem pairing_of {v : Nat → E} (h : Sat R pairAx v) : Pairing R := fun a b =>
  match h a b with
  | ⟨c, hc⟩ => ⟨c, fun u => ⟨fun x => orC (c2 R) (c2 R) ((hc u).1 x), fun y => (hc u).2 (orC (c1 R) (c1 R) y)⟩⟩

theorem sing_ex (hP : Pairing R) (a : E) : ∃ s, Sing R s a :=
  match hP a a with
  | ⟨s, hs⟩ => ⟨s, fun t => ⟨fun x => ((hs t).1 x).elim id id, fun y => (hs t).2 (.inl y)⟩⟩

theorem dbl_ex (hP : Pairing R) (a b : E) : ∃ d, Dbl R d a b :=
  match hP a b with
  | ⟨d, hd⟩ => ⟨d, hd⟩

/-- every two elements have a Kuratowski pair -/
theorem op_ex (hP : Pairing R) (a b : E) : ∃ p, OPs R p a b :=
  match sing_ex R hP a, dbl_ex R hP a b with
  | ⟨s, hs⟩, ⟨d, hd⟩ => match hP s d with
    | ⟨p, hp⟩ => ⟨p, (op_sem R p a b).2 (fun u =>
      ⟨fun hm => orC (fun c t => (c t).trans (hs t)) (fun c t => (c t).trans (hd t)) ((hp u).1 hm),
       fun h => (hp u).2 (orC (fun x t => (x t).trans (hs t).symm) (fun x t => (x t).trans (hd t).symm) h)⟩)⟩

theorem sing_self {s a : E} (h : Sing R s a) : mem R a s := (h a).2 (ceq_refl R a)

/-- pairs determine their components -/
theorem op_inj (hP : Pairing R) {p a b a' b' : E} (h1 : OPs R p a b) (h2 : OPs R p a' b') :
    Ceq R a a' ∧ Ceq R b b' :=
  have o1 := (op_sem R p a b).1 h1
  have o2 := (op_sem R p a' b').1 h2
  match sing_ex R hP a, dbl_ex R hP a b, dbl_ex R hP a' b' with
  | ⟨s, hs⟩, ⟨d, hd⟩, ⟨d', hd'⟩ =>
    have ha : Ceq R a a' := ((o2 s).1 ((o1 s).2 (.inl hs))).elim
      (fun h => (h a).1 (sing_self R hs))
      (fun h => ceq_symm R ((hs a').1 ((h a').2 (.inl (ceq_refl R a')))))
    have bd : mem R b d := (hd b).2 (.inr (ceq_refl R b))
    have bd' : mem R b' d' := (hd' b').2 (.inr (ceq_refl R b'))
    have dd := (o2 d).1 ((o1 d).2 (.inr hd))
    have dd' := (o1 d').1 ((o2 d').2 (.inr hd'))
    -- b' is coextensive with a or with b
    have b'ab : Ceq R b' a ∨ Ceq R b' b := dd'.elim (fun h => .inl ((h b').1 bd')) (fun h => (h b').1 bd')
    have hb : Ceq R b b' := dd.elim
      (fun h =>
        -- b ≈ a' ≈ a
        have bA : Ceq R b a := ceq_trans R ((h b).1 bd) (ceq_symm R ha)
        b'ab.elim (fun x => ceq_trans R bA (ceq_symm R x)) (fun x => ceq_symm R x))
      (fun h => ((h b).1 bd).elim
        (fun bA' =>
          -- b ≈ a' ≈ a
          have bA : Ceq R b a := ceq_trans R bA' (ceq_symm R ha)
          b'ab.elim (fun x => ceq_trans R bA (ceq_symm R x)) (fun x => ceq_symm R x))
        id)
    ⟨ha, hb⟩

end E

end NBGc

end Found

#print axioms Found.NBGc.base_B1
#print axioms Found.NBGc.nbg_zfc
#print axioms Found.NBGc.mapD_val
#print axioms Found.NBGc.rel_back
#print axioms Found.NBGc.PIN_gen
#print axioms Found.NBGc.op_sem
#print axioms Found.NBGc.pairing_of
#print axioms Found.NBGc.op_ex
#print axioms Found.NBGc.op_inj

found/NbgZ7.lean

lean_all/found/NbgZ7.lean

/-
  T91, NBG over ZF, part 7. found/NbgZ7 — the easy direction and the
  equivalences:
  * the sets of any model of NBG(B) form a model of ZF(B) (B containing
    pairing): extensionality from class extensionality, the base axioms by
    relativisation, foundation from comprehension and class foundation,
    replacement from comprehension (the class of pairs ⟨w, z⟩ with w ∈ x and
    θ(w, z)), Kuratowski pairs and class replacement;
  * hence every ZF(B) theorem relativised to sets is an NBG(B) theorem
    (B containing pairing and infinity, φ a sentence);
  * NBG ↔ ZF and NBG ↔ ZFC: for every sentence φ of the language of set
    theory, NBG ⊢ φ^M iff ZF ⊢ φ (and with choice in the base).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.NbgZ6

namespace Found

namespace NBGc

open FO FOCT

/-! ## the replacement and foundation schemes, read semantically -/

section E

variable {E : Type} (R : Nat → List E → Prop)

theorem replZ_intro (θ : Fm) (pF : Nat → E)
    (H : ∀ x, (∀ w, mem R w x → ∀ z z', Sat R θ (ext (ext pF z) w) → Sat R θ (ext (ext pF z') w) → Ceq R z z') →
      ∃ y, ∀ z, mem R z y ↔ ∃ w, mem R w x ∧ Sat R θ (ext (ext pF z) w)) :
    Sat R (replZ θ) pF := fun x hpre =>
  have eA : ∀ w z z', Sat R (ren (r3 2 1) θ) (ext (ext (ext (ext pF x) w) z) z') ↔ Sat R θ (ext (ext pF z) w) :=
    fun _ _ _ => (sat_ren R θ _ _).trans (sat_ext R θ (fun n => match n with
      | 0 => rfl
      | 1 => rfl
      | _ + 2 => rfl))
  have eB : ∀ w z z', Sat R (ren (r3 2 0) θ) (ext (ext (ext (ext pF x) w) z) z') ↔ Sat R θ (ext (ext pF z') w) :=
    fun _ _ _ => (sat_ren R θ _ _).trans (sat_ext R θ (fun n => match n with
      | 0 => rfl
      | 1 => rfl
      | _ + 2 => rfl))
  have eC : ∀ y z w, Sat R (ren (r3 0 1) θ) (ext (ext (ext (ext pF x) y) z) w) ↔ Sat R θ (ext (ext pF z) w) :=
    fun _ _ _ => (sat_ren R θ _ _).trans (sat_ext R θ (fun n => match n with
      | 0 => rfl
      | 1 => rfl
      | _ + 2 => rfl))
  match H x (fun w hw z z' h1 h2 => c2 R (hpre w hw z z' ⟨(eA w z z').2 h1, (eB w z z').2 h2⟩)) with
  | ⟨y, hy⟩ => ⟨y, fun z =>
    ⟨fun hz => match (hy z).1 hz with
      | ⟨w, h1, h2⟩ => ⟨w, h1, (eC y z w).2 h2⟩,
     fun ⟨w, h1, h2⟩ => (hy z).2 ⟨w, h1, (eC y z w).1 h2⟩⟩⟩

theorem foundZ_intro (θ : Fm) (pF : Nat → E)
    (H : (∃ x, Sat R θ (ext pF x)) → ∃ x, Sat R θ (ext pF x) ∧ ∀ y, mem R y x → ¬ Sat R θ (ext pF y)) :
    Sat R (foundZ θ) pF := fun hx =>
  have e : ∀ x y, Sat R (ren (up Nat.succ) θ) (ext (ext pF x) y) ↔ Sat R θ (ext pF y) := fun _ _ =>
    (sat_ren R θ _ _).trans (sat_ext R θ (fun n => match n with
      | 0 => rfl
      | _ + 1 => rfl))
  match H hx with
  | ⟨x, h1, h2⟩ => ⟨x, h1, fun y hy ht => h2 y hy ((e x y).1 ht)⟩

end E

/-- the class of pairs: ψ(p) = ∃w (w ∈ x ∧ ∃z (p = ⟨w, z⟩ ∧ θ(w, z))), variable 1 = x -/
def psiZ (θ : Fm) : Fm := .ex (.conj (m 0 2) (.ex (.conj (ren (opσ 2 1 0) OPz) (ren (r3 1 0) θ))))

theorem l0_psiZ {θ : Fm} (h : L0 θ) : L0 (psiZ θ) :=
  ⟨l0_m 0 2, l0_ren _ OPz l0_OPz, l0_ren _ θ h⟩

section D

variable {D : Type} (R : Nat → List D → Prop) {B : Fm → Prop}

theorem psiZ_sat (θ : Fm) (v : Nat → St R) (x p : St R) :
    Sat (Rs R) (psiZ θ) (ext (ext v x) p) ↔
      ∃ a, mem (Rs R) a x ∧ ∃ z, OPs (Rs R) p a z ∧ Sat (Rs R) θ (ext (ext v z) a) :=
  have e1 : ∀ a z, Sat (Rs R) (ren (opσ 2 1 0) OPz) (ext (ext (ext (ext v x) p) a) z) ↔ OPs (Rs R) p a z :=
    fun _ _ => (sat_ren (Rs R) OPz _ _).trans (sat_ext (Rs R) OPz (fun n => match n with
      | 0 => rfl
      | 1 => rfl
      | 2 => rfl
      | _ + 3 => rfl))
  have e2 : ∀ a z, Sat (Rs R) (ren (r3 1 0) θ) (ext (ext (ext (ext v x) p) a) z) ↔
      Sat (Rs R) θ (ext (ext v z) a) :=
    fun _ _ => (sat_ren (Rs R) θ _ _).trans (sat_ext (Rs R) θ (fun n => match n with
      | 0 => rfl
      | 1 => rfl
      | _ + 2 => rfl))
  ⟨fun ⟨a, ha, z, h1, h2⟩ => ⟨a, ha, z, (e1 a z).1 h1, (e2 a z).1 h2⟩,
   fun ⟨a, ha, z, h1, h2⟩ => ⟨a, ha, z, (e1 a z).2 h1, (e2 a z).2 h2⟩⟩

/-- comprehension in an arbitrary structure -/
theorem comp_gen (p : PF) (w : Nat → D) (h : Sat R (compAx p) w) :
    ∃ Y, ∀ X, R 1 [X] → (R 0 [X, Y] ↔ Sat R (emb p) (ext w X)) :=
  match h with
  | ⟨Y, hY⟩ => ⟨Y, fun X hX =>
    have e : Sat R (ren σc (emb p)) (ext (ext w Y) X) ↔ Sat R (emb p) (ext w X) :=
      (sat_ren R (emb p) σc _).trans (sat_ext R (emb p) (fun n => match n with
        | 0 => rfl
        | _ + 1 => rfl))
    ⟨fun h1 => e.1 ((hY X hX).1 h1), fun h2 => (hY X hX).2 (e.2 h2)⟩⟩

/-- comprehension for an L0 formula, read on the sets -/
theorem comp_set (hN : ModT (NBG B) R) (θ : Fm) (hl : L0 θ) (v : Nat → St R) :
    ∃ Y, ∀ s : St R, R 0 [s.1, Y] ↔ Sat (Rs R) θ (ext v s) :=
  match comp_gen R (pf θ) (fun n => (v n).1) (hN _ (.inr (.inr (.inr (.inl ⟨pf θ, rfl⟩)))) _) with
  | ⟨Y, hY⟩ => ⟨Y, fun s => (hY s.1 s.2).trans
      (rel_back R θ hl (ext (fun n => (v n).1) s.1) (ext v s) (ext_val R (fun _ => rfl) s))⟩

theorem setM (hN : ModT (NBG B) R) (w : Nat → D) {X Y : D} (h : R 0 [X, Y]) : R 1 [X] :=
  ((hN setAx (.inr (.inl rfl)) w) X).2 ⟨Y, h⟩

theorem extD_sets (hN : ModT (NBG B) R) (w : Nat → D) : ExtD (Rs R) := fun x y z hc hm =>
  (hN extAx (.inl rfl) w) x.1 y.1
    (fun U => ⟨fun h => (hc ⟨U, setM R hN w h⟩).1 h, fun h => (hc ⟨U, setM R hN w h⟩).2 h⟩) z.1 hm

/-- the sets of a model of NBG(B) form a model of ZF(B) -/
theorem sets_ZF (hB : Base B) (hpB : B pairAx) (hN : ModT (NBG B) R) : ModT (ZF B) (Rs R) := fun φ hφ v =>
  have w : Nat → D := fun n => (v n).1
  have hE : ExtD (Rs R) := extD_sets R hN w
  have hbase : ∀ β, B β → ∀ v' : Nat → St R, Sat (Rs R) β v' := fun β hb v' =>
    (rel_back R β (hB β hb).1 (fun n => (v' n).1) v' (fun _ => rfl)).1
      (hN _ (.inr (.inr (.inl ⟨β, hb, rfl⟩))) _)
  have hP : Pairing (Rs R) := pairing_of (Rs R) (hbase pairAx hpB v)
  match hφ with
  | .inl e => by
    rw [e]
    exact fun x y hc z hm => hE x y z (c2 (Rs R) hc) hm
  | .inr (.inl hb) => hbase φ hb v
  | .inr (.inr (.inl ⟨θ, hl, e⟩)) => by
    rw [e]
    exact replZ_intro (Rs R) θ v (fun x pre =>
      -- the class F of pairs ⟨a, z⟩ with a ∈ x and θ(a, z)
      match comp_set R hN (psiZ θ) (l0_psiZ hl) (ext v x) with
      | ⟨F, hF⟩ =>
        have inF : ∀ p : St R, R 0 [p.1, F] ↔
            ∃ a, mem (Rs R) a x ∧ ∃ z, OPs (Rs R) p a z ∧ Sat (Rs R) θ (ext (ext v z) a) :=
          fun p => (hF p).trans (psiZ_sat R θ v x p)
        have thC : ∀ a a' z z', Ceq (Rs R) a a' → Ceq (Rs R) z z' → Sat (Rs R) θ (ext (ext v z) a) →
            Sat (Rs R) θ (ext (ext v z') a') := fun a a' z z' ha hz ht =>
          sat_ceq (Rs R) hE θ hl (v := ext (ext v z) a) (w := ext (ext v z') a') (fun n => match n with
            | 0 => ha
            | 1 => hz
            | _ + 2 => ceq_refl (Rs R) _) ht
        have opD : ∀ (P W Z : St R), Sat R (relE OPz) (tri P.1 W.1 Z.1) ↔ OPs (Rs R) P W Z := fun P W Z =>
          rel_back R OPz l0_OPz (tri P.1 W.1 Z.1) (tri P W Z) (fun n => match n with
            | 0 => rfl
            | 1 => rfl
            | 2 => rfl
            | _ + 3 => rfl)
        have hRep := hN creplAx (.inr (.inr (.inr (.inr (.inl rfl))))) w F
          (fun W hW Z hZ Z' hZ' hc U hU =>
            match (PIN_gen R _ 2 1 3).1 hc.1, (PIN_gen R _ 2 0 3).1 hc.2 with
            | ⟨P, hP1, ho, hm⟩, ⟨P', hP1', ho', hm'⟩ =>
              match (inF ⟨P, hP1⟩).1 hm, (inF ⟨P', hP1'⟩).1 hm' with
              | ⟨a1, ha1, z1, hp1, ht1⟩, ⟨a2, _, z2, hp2, ht2⟩ =>
                have i1 := op_inj (Rs R) hP ((opD ⟨P, hP1⟩ ⟨W, hW⟩ ⟨Z, hZ⟩).1 ho) hp1
                have i2 := op_inj (Rs R) hP ((opD ⟨P', hP1'⟩ ⟨W, hW⟩ ⟨Z', hZ'⟩).1 ho') hp2
                have a21 : Ceq (Rs R) a2 a1 := ceq_trans (Rs R) (ceq_symm (Rs R) i2.1) i1.1
                have zz := pre a1 ha1 z1 z2 ht1 (thC a2 a1 z2 z2 a21 (ceq_refl (Rs R) z2) ht2)
                have c : Ceq (Rs R) ⟨Z, hZ⟩ ⟨Z', hZ'⟩ :=
                  ceq_trans (Rs R) i1.2 (ceq_trans (Rs R) zz (ceq_symm (Rs R) i2.2))
                ⟨(c ⟨U, hU⟩).1, (c ⟨U, hU⟩).2⟩)
          x.1 x.2
        match hRep with
        | ⟨Y, hY, hYi⟩ => ⟨⟨Y, hY⟩, fun z =>
          ⟨fun hz => match (hYi z.1 z.2).1 hz with
            | ⟨W, hW, hWx, hpi⟩ => match (PIN_gen R _ 0 1 4).1 hpi with
              | ⟨P, hP1, ho, hm⟩ => match (inF ⟨P, hP1⟩).1 hm with
                | ⟨a1, ha1, z1, hp1, ht1⟩ =>
                  have i1 := op_inj (Rs R) hP ((opD ⟨P, hP1⟩ ⟨W, hW⟩ z).1 ho) hp1
                  ⟨a1, ha1, thC a1 a1 z1 z (ceq_refl (Rs R) a1) (ceq_symm (Rs R) i1.2) ht1⟩,
           fun ⟨a, ha, ht⟩ => match op_ex (Rs R) hP a z with
            | ⟨p, hp⟩ => (hYi z.1 z.2).2 ⟨a.1, a.2, ha,
                (PIN_gen R _ 0 1 4).2 ⟨p.1, p.2, (opD p a z).2 hp, (inF p).2 ⟨a, ha, z, hp, ht⟩⟩⟩⟩⟩)
  | .inr (.inr (.inr ⟨θ, hl, e⟩)) => by
    rw [e]
    exact foundZ_intro (Rs R) θ v (fun ⟨x, hx⟩ =>
      match comp_set R hN θ hl v with
      | ⟨Y, hY⟩ =>
        match hN cfoundAx (.inr (.inr (.inr (.inr (.inr rfl))))) w Y ⟨x.1, x.2, (hY x).2 hx⟩ with
        | ⟨X0, h1, h2, h3⟩ => ⟨⟨X0, h1⟩, (hY ⟨X0, h1⟩).1 h2, fun y hy ht => h3 y.1 y.2 hy ((hY y).2 ht)⟩)

/-- there is a set (from infinity) -/
theorem set_ex (hiB : B infAx) (hN : ModT (NBG B) R) (w : Nat → D) : Nonempty (St R) :=
  match hN _ (.inr (.inr (.inl ⟨infAx, hiB, rfl⟩))) w with
  | ⟨X, hX, _⟩ => ⟨⟨X, hX⟩⟩

end D

/-- the easy direction: ZF(B) theorems, relativised, are NBG(B) theorems -/
theorem zf_to_nbg (K : CP.Classical') {B : Fm → Prop} (hB : Base B) (hpB : B pairAx) (hiB : B infAx)
    {φ : Fm} (hl : L0 φ) (hf : FV 0 φ) (h : PC (ZF B) [] φ) : PC (NBG B) [] (relE φ) :=
  completenessT (NBG B) K (fun _ R w hN _ =>
    match set_ex R hiB hN w with
    | ⟨s0⟩ =>
      have hs := soundnessT (ZF B) K h (St R) (Rs R) (fun _ => s0) (sets_ZF R hB hpB hN) (fun _ m => nomatch m)
      have hr := (rel_back R φ hl (fun _ => s0.1) (fun _ => s0) (fun _ => rfl)).2 hs
      (sat_agree R (relE φ) 0 (fv_rel φ 0 hf) (v := w) (w := fun _ => s0.1)
        (fun n hn => absurd hn (Nat.not_lt_zero n))).2 hr)

/-- NBG ↔ ZF on sentences of set theory -/
theorem nbg_iff_zf (K : CP.Classical') {φ : Fm} (hl : L0 φ) (hf : FV 0 φ) :
    PC (NBG B0) [] (relE φ) ↔ PC (ZF B0) [] φ :=
  ⟨nbg_zf K hl, zf_to_nbg K base_B0 (.inl rfl) (.inr (.inr (.inr rfl))) hl hf⟩

/-- NBG with choice ↔ ZFC on sentences of set theory -/
theorem nbg_iff_zfc (K : CP.Classical') {φ : Fm} (hl : L0 φ) (hf : FV 0 φ) :
    PC (NBG B1) [] (relE φ) ↔ PC (ZF B1) [] φ :=
  ⟨nbg_zfc K hl, zf_to_nbg K base_B1 (.inl (.inl rfl)) (.inl (.inr (.inr (.inr rfl)))) hl hf⟩

end NBGc

end Found

#print axioms Found.NBGc.replZ_intro
#print axioms Found.NBGc.foundZ_intro
#print axioms Found.NBGc.psiZ_sat
#print axioms Found.NBGc.comp_gen
#print axioms Found.NBGc.comp_set
#print axioms Found.NBGc.extD_sets
#print axioms Found.NBGc.sets_ZF
#print axioms Found.NBGc.set_ex
#print axioms Found.NBGc.zf_to_nbg
#print axioms Found.NBGc.nbg_iff_zf
#print axioms Found.NBGc.nbg_iff_zfc

found/NoSelfVerify.lean

lean_all/found/NoSelfVerify.lean

/-
  (found/ version: the axiom-free version of ~/Downloads/NoSelfVerify.lean. The original and
  work/imported/NoSelfVerify.lean are unchanged; here Classical.em in cantor_inj is replaced by
  the constructive diagonal, and simp in no_self_checker / not_every_map_coded by Bool case
  splits and rw. The statements are the same.)
  No prover verifies itself.
  Core Lean 4, no Mathlib.

  Part 1 (conditional). A proof system is given abstractly: sentences,
  implication, falsity, a sentence `box a` read as "a is provable", and the
  theorems `Pr`. It is assumed to have modus ponens, the axioms K and S, the
  three derivability conditions D1-D3 and a diagonal (self-reference). Then:
    Loeb:      if it proves  box b -> b,  it proves b;
    Goedel I:  if consistent, some sentence is not provable;
    Goedel II: if consistent, it does not prove its own consistency.
  A model shows the assumptions hold together.

  Part 2 (no assumptions). Lean's own types: no type at a level names all
  the types of that level.

  Part 3 (no assumptions). Types themselves: for any type `Code` and any way
  of running or reading codes, no code answers the diagonal question, some
  map is the behaviour of no code, and there is no truth predicate inside.

  Part 4. Always one more: a level is held one level up, and in a tower of
  consistent provers none verifies itself.

  The assumptions ask only for a minimum. Adding rules removes none of it,
  so the statements hold for every stronger system as well.
-/

namespace NoSelfVerify

structure ProvSys where
  S : Type
  imp : S → S → S
  bot : S
  box : S → S
  Pr : S → Prop
  mp : ∀ {a b : S}, Pr (imp a b) → Pr a → Pr b
  axK : ∀ a b : S, Pr (imp a (imp b a))
  axS : ∀ a b c : S, Pr (imp (imp a (imp b c)) (imp (imp a b) (imp a c)))
  d1 : ∀ {a : S}, Pr a → Pr (box a)
  d2 : ∀ a b : S, Pr (imp (box (imp a b)) (imp (box a) (box b)))
  d3 : ∀ a : S, Pr (imp (box a) (box (box a)))
  diag : ∀ b : S, ∃ L : S, Pr (imp L (imp (box L) b)) ∧ Pr (imp (imp (box L) b) L)

variable (T : ProvSys)

/-- chaining two implications, from K and S -/
theorem impTrans {a b c : T.S} (h1 : T.Pr (T.imp a b)) (h2 : T.Pr (T.imp b c)) :
    T.Pr (T.imp a c) := by
  have h3 : T.Pr (T.imp a (T.imp b c)) := T.mp (T.axK (T.imp b c) a) h2
  exact T.mp (T.mp (T.axS a b c) h3) h1

/-- Loeb: proving "if b is provable then b" already proves b -/
theorem loeb (b : T.S) (h : T.Pr (T.imp (T.box b) b)) : T.Pr b := by
  obtain ⟨L, hL1, hL2⟩ := T.diag b
  have h3 : T.Pr (T.box (T.imp L (T.imp (T.box L) b))) := T.d1 hL1
  have h4 : T.Pr (T.imp (T.box L) (T.box (T.imp (T.box L) b))) :=
    T.mp (T.d2 L (T.imp (T.box L) b)) h3
  have h5 : T.Pr (T.imp (T.box (T.imp (T.box L) b)) (T.imp (T.box (T.box L)) (T.box b))) :=
    T.d2 (T.box L) b
  have h6 : T.Pr (T.imp (T.box L) (T.imp (T.box (T.box L)) (T.box b))) :=
    impTrans T h4 h5
  have h7 : T.Pr (T.imp (T.box L) (T.box (T.box L))) := T.d3 L
  have h8 : T.Pr (T.imp (T.box L) (T.box b)) :=
    T.mp (T.mp (T.axS (T.box L) (T.box (T.box L)) (T.box b)) h6) h7
  have h9 : T.Pr (T.imp (T.box L) b) := impTrans T h8 h
  have h10 : T.Pr L := T.mp hL2 h9
  have h11 : T.Pr (T.box L) := T.d1 h10
  exact T.mp h9 h11

/-- Goedel I: a consistent system has an unprovable sentence -/
theorem goedel1 (hc : ¬ T.Pr T.bot) : ∃ G : T.S, ¬ T.Pr G := by
  obtain ⟨L, hL1, _⟩ := T.diag T.bot
  exact ⟨L, fun hL => hc (T.mp (T.mp hL1 hL) (T.d1 hL))⟩

/-- Goedel II: a consistent system does not prove its own consistency -/
theorem goedel2 (hc : ¬ T.Pr T.bot) : ¬ T.Pr (T.imp (T.box T.bot) T.bot) :=
  fun h => hc (loeb T T.bot h)

/-- the same, read the other way: proving its own consistency makes it inconsistent -/
theorem selfVerify_breaks (h : T.Pr (T.imp (T.box T.bot) T.bot)) : T.Pr T.bot :=
  loeb T T.bot h

/-! ## A model: all the assumptions hold together -/

def model : ProvSys where
  S := Prop
  imp := fun a b => a → b
  bot := False
  box := fun _ => True
  Pr := fun p => p
  mp := by
    intro a b h x
    exact h x
  axK := by
    intro a b x _
    exact x
  axS := by
    intro a b c f g x
    exact f x (g x)
  d1 := by
    intro a _
    trivial
  d2 := by
    intro a b _ _
    trivial
  d3 := by
    intro a _
    trivial
  diag := by
    intro b
    refine ⟨b, ?_, ?_⟩
    · intro x _
      exact x
    · intro f
      exact f trivial

/-- the model is consistent -/
theorem model_consistent : ¬ model.Pr model.bot := fun h => h

/-- and it does not prove its own consistency -/
theorem model_no_self_verify : ¬ model.Pr (model.imp (model.box model.bot) model.bot) :=
  fun h => h trivial

/-- as goedel2 says it cannot -/
example : ¬ model.Pr (model.imp (model.box model.bot) model.bot) :=
  goedel2 model model_consistent

/-! ## Lean's own types as a whole

Here nothing is assumed. The statements are about Lean's own universe
`Type u`: it cannot be held inside itself. -/

universe u

/-- the diagonal part for a map `g` from parts of `T` back into `T` -/
abbrev diagSet {T : Type u} (g : (T → Prop) → T) : T → Prop :=
  fun t => ∃ s : T → Prop, g s = t ∧ ¬ s t

/-- the parts of `T` cannot be put back into `T` one-to-one (copy, read, deny) -/
theorem cantor_inj {T : Type u} (g : (T → Prop) → T)
    (hg : ∀ s t, g s = g t → s = t) : False :=
  have hn : ¬ diagSet g (g (diagSet g)) := fun h =>
    match h with
    | ⟨s, hs, hns⟩ =>
      have e : s = diagSet g := hg s (diagSet g) hs
      hns (by rw [e]; exact h)
  hn ⟨diagSet g, rfl, hn⟩

theorem cast_inj {A B : Type u} (h : A = B) (x y : A) (e : cast h x = cast h y) : x = y := by
  cases h
  exact e

/-- No type of Lean at level `u` names all the types of level `u`:
    whatever `α` and decoder `f` are given, some type is missed. -/
theorem lean_types_not_inside_lean {α : Type u} (f : α → Type u) :
    ¬ ∀ X : Type u, ∃ a, f a = X := by
  intro hf
  cases hf (Sigma f → Prop) with
  | intro U hU =>
    apply cantor_inj (fun s : Sigma f → Prop => (⟨U, cast hU.symm s⟩ : Sigma f))
    intro s t h
    have h' : (⟨U, cast hU.symm s⟩ : Sigma f) = ⟨U, cast hU.symm t⟩ := h
    have h2 : cast hU.symm s = cast hU.symm t := eq_of_heq (Sigma.mk.inj h').2
    exact cast_inj hU.symm s t h2

/-- So Lean has no type of codes for all of its own types, at any level. -/
theorem no_code_of_all_types : ¬ ∃ (α : Type u) (f : α → Type u), ∀ X : Type u, ∃ a, f a = X :=
  fun ⟨_, f, hf⟩ => lean_types_not_inside_lean f hf

/-! ## Types themselves

Whatever is built from types has codes of some type `Code` and runs or reads
them by a map of types. The three statements below hold for every such
`Code`, with nothing assumed; Lean is built from types, so they hold for it. -/

/-- No total system runs its own diagonal: if `eval d c` is the answer of
    program `d` on input `c`, no program answers "does `c` reject itself?". -/
theorem no_self_checker {Code : Type u} (eval : Code → Code → Bool) :
    ¬ ∃ d : Code, ∀ c, eval d c = !(eval c c) := by
  intro ⟨d, h⟩
  have h2 : eval d d = !(eval d d) := h d
  cases hd : eval d d <;> (rw [hd] at h2; exact Bool.noConfusion h2)

/-- So some map of types `Code → Bool` is not the behaviour of any code:
    the system cannot run all of itself. -/
theorem not_every_map_coded {Code : Type u} (eval : Code → Code → Bool) :
    ∃ f : Code → Bool, ∀ d, eval d ≠ f :=
  ⟨fun c => !(eval c c), fun d h => by
    have h2 : eval d d = !(eval d d) := congrFun h d
    cases hd : eval d d <;> (rw [hd] at h2; exact Bool.noConfusion h2)⟩

/-- The same for statements: if `holds d c` says "sentence `d` is true of `c`",
    no sentence says "`c` is not true of itself". There is no truth predicate inside. -/
theorem no_truth_inside {Code : Type u} (holds : Code → Code → Prop) :
    ¬ ∃ d : Code, ∀ c, holds d c ↔ ¬ holds c c :=
  fun ⟨d, h⟩ =>
    have hd : holds d d ↔ ¬ holds d d := h d
    have hn : ¬ holds d d := fun x => (hd.mp x) x
    hn (hd.mpr hn)

/-! ## Always one more -/

/-- the types of level `u` are held one level up -/
example : Type (u + 1) := Type u

/-- A tower of provers, each consistent: at no height does one verify itself.
    Whatever verifies prover `n` is not prover `n`. -/
theorem tower_never_closes (T : Nat → ProvSys) (hc : ∀ n, ¬ (T n).Pr (T n).bot) :
    ∀ n, ¬ (T n).Pr ((T n).imp ((T n).box (T n).bot) (T n).bot) :=
  fun n => goedel2 (T n) (hc n)

end NoSelfVerify

found/Nominal.lean

lean_all/found/Nominal.lean

/-
  T110. found/Nominal — Fraenkel–Mostowski / nominal sets as a point: a set
  with an action of atom permutations (a parameter) in which every element
  has a finite support.
  * Permutations of atoms (functions with a two-sided inverse, compared
    pointwise — no funext).
  * Nominal sets; the atoms are nominal (support {a}); products of nominal
    sets are nominal (support = union).
  The Schanuel topos and FM set theory (atoms + finite support) are the
  known lines (cited).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Compactness

namespace Found

namespace Nom

structure Perm where
  f : Nat → Nat
  g : Nat → Nat
  fg : ∀ a, f (g a) = a
  gf : ∀ a, g (f a) = a

structure NSet where
  X : Type
  act : Perm → X → X
  resp : ∀ π σ, (∀ a, π.f a = σ.f a) → ∀ x, act π x = act σ x

def Fixes (π : Perm) (S : List Nat) : Prop := ∀ a, a ∈ S → π.f a = a

def Supports (N : NSet) (S : List Nat) (x : N.X) : Prop := ∀ π, Fixes π S → N.act π x = x

def Nominal (N : NSet) : Prop := ∀ x, ∃ S, Supports N S x

def atoms : NSet := ⟨Nat, fun π a => π.f a, fun _ _ h a => h a⟩

theorem atoms_nominal : Nominal atoms := fun a => ⟨[a], fun _ h => h a (.head _)⟩

def prod (N M : NSet) : NSet :=
  ⟨N.X × M.X, fun π p => (N.act π p.1, M.act π p.2),
   fun π σ h p => by
     show (N.act π p.1, M.act π p.2) = (N.act σ p.1, M.act σ p.2)
     rw [N.resp π σ h p.1, M.resp π σ h p.2]⟩

theorem prod_nominal (N M : NSet) (hN : Nominal N) (hM : Nominal M) : Nominal (prod N M) :=
  fun p => match hN p.1, hM p.2 with
  | ⟨S, hS⟩, ⟨T, hT⟩ => ⟨S ++ T, fun π h => by
      show (N.act π p.1, M.act π p.2) = p
      rw [hS π (fun a m => h a (Prp.mem_app_l m)), hT π (fun a m => h a (Prp.mem_app_r S m))]
      rfl⟩

end Nom

end Found

#print axioms Found.Nom.atoms_nominal
#print axioms Found.Nom.prod_nominal

found/NonMono.lean

lean_all/found/NonMono.lean

/-
  T173. found/NonMono — nonmonotonic reasoning as a point: preferential
  models (KLM) on the relation coordinate (a preference relation on worlds);
  A |~ B :⟺ every preferred-minimal A-world is a B-world.
  * Reflexivity, right weakening, And hold; monotonicity fails (the
    bird/penguin countermodel) — the line to classical consequence is
    "empty preference" (all A-worlds minimal).

  Lean 4 core only, hand-written proofs, no axioms.
-/

namespace Found

namespace NonMono

universe u

variable {W : Type u} (lt : W → W → Prop)

def Minimal (A : W → Prop) (w : W) : Prop := A w ∧ ∀ v, lt v w → ¬ A v

def nm (A B : W → Prop) : Prop := ∀ w, Minimal lt A w → B w

theorem refl (A : W → Prop) : nm lt A A := fun _ h => h.1

theorem right_weak (A B C : W → Prop) (h : nm lt A B) (hBC : ∀ w, B w → C w) : nm lt A C :=
  fun w m => hBC w (h w m)

theorem and_rule (A B C : W → Prop) (h1 : nm lt A B) (h2 : nm lt A C) : nm lt A (fun w => B w ∧ C w) :=
  fun w m => ⟨h1 w m, h2 w m⟩

/-- empty preference gives classical consequence -/
theorem classical_of_empty (A B : W → Prop) : nm (fun _ _ => False) A B ↔ ∀ w, A w → B w :=
  ⟨fun h w a => h w ⟨a, fun _ f => f.elim⟩, fun h w m => h w m.1⟩

/-- worlds: `true` = penguin, `false` = normal bird; normal is preferred -/
def pref (v w : Bool) : Prop := v = false ∧ w = true

theorem not_monotone :
    nm pref (fun _ => True) (fun w => w = false) ∧
    ¬ nm pref (fun w => True ∧ w = true) (fun w => w = false) := by
  constructor
  · intro w m
    cases w with
    | false => rfl
    | true => exact (m.2 false ⟨rfl, rfl⟩ trivial).elim
  · intro h
    exact Bool.noConfusion (h true ⟨⟨trivial, rfl⟩, fun v hv hA => Bool.noConfusion (hv.1.symm.trans hA.2)⟩)

end NonMono

end Found

#print axioms Found.NonMono.refl
#print axioms Found.NonMono.right_weak
#print axioms Found.NonMono.and_rule
#print axioms Found.NonMono.classical_of_empty
#print axioms Found.NonMono.not_monotone

found/OP3.lean

lean_all/found/OP3.lean

/-
  T127 (core, slice OP3). found/OP3 — composition structures and
  fibrations over an interval (Orton–Pitts), in the reading of a topos in
  Lean's types (OP1–OP2).  The one topos fact used beyond OP1–OP2 is a
  hypothesis (a field of OrRec), never a Lean axiom: partial elements on
  two propositions that agree on the overlap glue to a partial element on
  the disjunction (unique choice for ∨, true in every topos).
  * Comp A: composition for a line of types A : I → Type: a partial path on
    a cofibrant φ and a start extending it give an end extending it.
  * Fib A: fibration structure on a family over Γ (composition along every
    path of Γ); stable under reindexing.
  * transport along a fibration (φ = (i0 = i1), empty), and the filler of
    a composition problem, built from composition along the connection
    i ↦ i ∧ j: it starts at the given start and extends the partial path.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.OrtonPitts

namespace Found

namespace OP

/-- gluing partial elements along a disjunction -/
structure OrRec where
  glue : ∀ {φ ψ : Prop} {A : Type} (f : φ → A) (g : ψ → A), (∀ h h', f h = g h') → (φ ∨ ψ) → A
  glue_l : ∀ {φ ψ : Prop} {A : Type} (f : φ → A) (g : ψ → A) (e : ∀ h h', f h = g h') (h : φ),
    glue f g e (.inl h) = f h
  glue_r : ∀ {φ ψ : Prop} {A : Type} (f : φ → A) (g : ψ → A) (e : ∀ h h', f h = g h') (h : ψ),
    glue f g e (.inr h) = g h

/-- transport along an equality of types -/
def castT {X Y : Type} (e : X = Y) (x : X) : Y := e ▸ x

theorem castT_rfl {X : Type} (x : X) : castT rfl x = x := rfl

theorem castT_trans {X Y Z : Type} (e : X = Y) (e' : Y = Z) (x : X) :
    castT e' (castT e x) = castT (e.trans e') x := by
  cases e; cases e'; rfl

theorem castT_back {X Y : Type} (e : X = Y) (x : X) : castT e.symm (castT e x) = x := by
  cases e; rfl

theorem castT_self {X : Type} (e : X = X) (x : X) : castT e x = x := by
  cases e; rfl

/-- a dependent function commutes with transport along equal arguments -/
theorem castT_app {α : Type} {B : α → Type} (f : (a : α) → B a) {a b : α} (e : a = b) :
    castT (congrArg B e) (f a) = f b := by
  cases e; rfl

section comp

variable (J : Interval) (C : Cof J)

/-- composition for a line of types -/
structure Comp (A : J.I → Type) where
  c : (φ : Prop) → C.isCof φ → (u : (i : J.I) → φ → A i) → (a0 : A J.i0) → (∀ h, u J.i0 h = a0) → A J.i1
  ext : ∀ φ hφ u a0 hu (h : φ), c φ hφ u a0 hu = u J.i1 h

/-- fibration structure on a family over Γ -/
def Fib {Γ : Type} (A : Γ → Type) : Type := (p : J.I → Γ) → Comp J C (fun i => A (p i))

/-- fibrations are stable under reindexing -/
def fibReindex {Γ Δ : Type} (f : Δ → Γ) {A : Γ → Type} (F : Fib J C A) : Fib J C (fun d => A (f d)) :=
  fun p => F (fun i => f (p i))

/-- the empty cofibrant proposition i0 = i1 -/
theorem cof_empty : C.isCof (J.i0 = J.i1) := C.eq1 J.i0

/-- transport along a line with composition -/
def transp {A : J.I → Type} (F : Comp J C A) (a0 : A J.i0) : A J.i1 :=
  F.c (J.i0 = J.i1) (cof_empty J C) (fun _ h => absurd h J.ne) a0 (fun h => absurd h J.ne)

variable {J C}

/-- reindexing a partial element along an equality of points -/
theorem pe_move {Γ : Type} {A : Γ → Type} {φ : Prop} (u : (x : Γ) → φ → A x) {x y : Γ} (e : x = y) (h : φ) :
    u y h = castT (congrArg A e) (u x h) := by
  cases e; rfl

end comp

section fill

variable {J : Interval} {C : Cof J} (O : OrRec)

/-- the start of the connection line, transported -/
def startAt {Γ : Type} {A : Γ → Type} (p : J.I → Γ) (j : J.I) (a0 : A (p J.i0)) : A (p (J.meet J.i0 j)) :=
  castT (congrArg (fun i => A (p i)) (J.i0_meet j).symm) a0

/-- the partial element of the filling problem on φ ∨ (j = i0) -/
def fillPE {Γ : Type} {A : Γ → Type} (p : J.I → Γ) (j : J.I) (φ : Prop) (u : (i : J.I) → φ → A (p i))
    (a0 : A (p J.i0)) (hu : ∀ h, u J.i0 h = a0) (i : J.I) : (φ ∨ j = J.i0) → A (p (J.meet i j)) :=
  O.glue (fun h => u (J.meet i j) h)
    (fun ej => castT (congrArg (fun t => A (p t)) ((congrArg (J.meet i) ej).trans (J.meet_i0 i)).symm) a0)
    (fun h ej => by
      rw [pe_move (A := fun t => A (p t)) u ((congrArg (J.meet i) ej).trans (J.meet_i0 i)).symm h, hu h])

/-- the filler: composition along i ↦ p (i ∧ j) with the extra face j = i0 -/
def fill {Γ : Type} {A : Γ → Type} (F : Fib J C A) (p : J.I → Γ) (φ : Prop) (hφ : C.isCof φ)
    (u : (i : J.I) → φ → A (p i)) (a0 : A (p J.i0)) (hu : ∀ h, u J.i0 h = a0) (j : J.I) : A (p j) :=
  castT (congrArg (fun t => A (p t)) (J.i1_meet j))
    ((F (fun i => p (J.meet i j))).c (φ ∨ j = J.i0) (C.or hφ (C.eq0 j)) (fillPE O p j φ u a0 hu)
      (startAt p j a0)
      (fun h => by
        show fillPE O p j φ u a0 hu J.i0 h = startAt p j a0
        unfold fillPE startAt
        cases h with
        | inl h1 => rw [O.glue_l _ _ _ h1, pe_move (A := fun t => A (p t)) u (J.i0_meet j).symm h1, hu h1]
        | inr h2 => rw [O.glue_r _ _ _ h2]))

/-- the filler extends the partial path -/
theorem fill_ext {Γ : Type} {A : Γ → Type} (F : Fib J C A) (p : J.I → Γ) (φ : Prop) (hφ : C.isCof φ)
    (u : (i : J.I) → φ → A (p i)) (a0 : A (p J.i0)) (hu : ∀ h, u J.i0 h = a0) (j : J.I) (h : φ) :
    fill O F p φ hφ u a0 hu j = u j h := by
  unfold fill
  rw [(F (fun i => p (J.meet i j))).ext _ _ _ _ _ (.inl h), fillPE, O.glue_l _ _ _ h]
  exact (pe_move (A := fun t => A (p t)) u (J.i1_meet j) h).symm

/-- the filler starts at the start -/
theorem fill_start {Γ : Type} {A : Γ → Type} (F : Fib J C A) (p : J.I → Γ) (φ : Prop) (hφ : C.isCof φ)
    (u : (i : J.I) → φ → A (p i)) (a0 : A (p J.i0)) (hu : ∀ h, u J.i0 h = a0) :
    fill O F p φ hφ u a0 hu J.i0 = a0 := by
  unfold fill
  rw [(F (fun i => p (J.meet i J.i0))).ext _ _ _ _ _ (.inr rfl), fillPE, O.glue_r _ _ _ rfl, castT_trans]
  exact castT_self _ a0

end fill

end OP

end Found

#print axioms Found.OP.castT_trans
#print axioms Found.OP.fibReindex
#print axioms Found.OP.transp
#print axioms Found.OP.pe_move
#print axioms Found.OP.fill
#print axioms Found.OP.fill_ext
#print axioms Found.OP.fill_start

found/OP4.lean

lean_all/found/OP4.lean

/-
  T127 (core, slice OP4). found/OP4 — fibrations are closed under Σ and
  path types (Orton–Pitts).  For Σ: fill in the base family, then compose
  in the fibre family along the filled path.  For paths: compose in A with
  the extra faces j = i0 and j = i1 (glued by OrRec); comparing paths uses
  function extensionality on the interval, taken as a hypothesis (a field of
  FunI, true in every topos), never a Lean axiom.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.OP3

namespace Found

namespace OP

/-- function extensionality for maps out of the interval -/
structure FunI (J : Interval) where
  ext : ∀ {A : Type} {f g : J.I → A}, (∀ i, f i = g i) → f = g

theorem sigma_cast {α : Type} {P : α → Type} (x : (a : α) × P a) {a' : α} (e : x.1 = a') :
    (⟨a', castT (congrArg P e) x.2⟩ : (a : α) × P a) = x := by
  cases x with
  | mk a b => cases e; rfl

theorem pair_cast {α : Type} {P : α → Type} {x x0 : (a : α) × P a} {a1 : α}
    (hx : x = x0) (e1 : x.1 = a1) (e2 : x0.1 = a1) :
    castT (congrArg P e1) x.2 = castT (congrArg P e2) x0.2 := by
  cases hx; rfl

section sigma

variable {J : Interval} {C : Cof J} (O : OrRec)
variable {Γ : Type} {A : Γ → Type} {B : ((γ : Γ) × A γ) → Type} (FA : Fib J C A) (FB : Fib J C B)

/-- the filled path in the base -/
def sigBase (p : J.I → Γ) (φ : Prop) (hφ : C.isCof φ) (u : (i : J.I) → φ → (a : A (p i)) × B ⟨p i, a⟩)
    (x0 : (a : A (p J.i0)) × B ⟨p J.i0, a⟩) (hu : ∀ h, u J.i0 h = x0) (j : J.I) : A (p j) :=
  fill O FA p φ hφ (fun i h => (u i h).1) x0.1 (fun h => congrArg Sigma.fst (hu h)) j

section sig
variable (p : J.I → Γ) (φ : Prop) (hφ : C.isCof φ) (u : (i : J.I) → φ → (a : A (p i)) × B ⟨p i, a⟩)
  (x0 : (a : A (p J.i0)) × B ⟨p J.i0, a⟩) (hu : ∀ h, u J.i0 h = x0)

/-- the filled path, as a path in Σ γ, A γ -/
def sigQ (i : J.I) : (γ : Γ) × A γ := ⟨p i, sigBase O FA p φ hφ u x0 hu i⟩

theorem sigFE (i : J.I) (h : φ) : (u i h).1 = sigBase O FA p φ hφ u x0 hu i :=
  (fill_ext O FA p φ hφ (fun i h => (u i h).1) x0.1 (fun h => congrArg Sigma.fst (hu h)) i h).symm

theorem sigFS : x0.1 = sigBase O FA p φ hφ u x0 hu J.i0 :=
  (fill_start O FA p φ hφ (fun i h => (u i h).1) x0.1 (fun h => congrArg Sigma.fst (hu h))).symm

def sigV (i : J.I) (h : φ) : B (sigQ O FA p φ hφ u x0 hu i) :=
  castT (congrArg (fun a => B ⟨p i, a⟩) (sigFE O FA p φ hφ u x0 hu i h)) (u i h).2

def sigB0 : B (sigQ O FA p φ hφ u x0 hu J.i0) :=
  castT (congrArg (fun a => B ⟨p J.i0, a⟩) (sigFS O FA p φ hφ u x0 hu)) x0.2

theorem sigHv (h : φ) : sigV O FA p φ hφ u x0 hu J.i0 h = sigB0 O FA p φ hφ u x0 hu :=
  pair_cast (P := fun a => B ⟨p J.i0, a⟩) (hu h) (sigFE O FA p φ hφ u x0 hu J.i0 h) (sigFS O FA p φ hφ u x0 hu)

end sig

/-- composition in Σ -/
def sigComp (p : J.I → Γ) : Comp J C (fun i => (a : A (p i)) × B ⟨p i, a⟩) where
  c φ hφ u x0 hu := ⟨sigBase O FA p φ hφ u x0 hu J.i1, (FB (sigQ O FA p φ hφ u x0 hu)).c φ hφ
    (sigV O FA p φ hφ u x0 hu) (sigB0 O FA p φ hφ u x0 hu) (sigHv O FA p φ hφ u x0 hu)⟩
  ext φ hφ u x0 hu h := by
    show (⟨sigBase O FA p φ hφ u x0 hu J.i1, (FB (sigQ O FA p φ hφ u x0 hu)).c φ hφ
      (sigV O FA p φ hφ u x0 hu) (sigB0 O FA p φ hφ u x0 hu) (sigHv O FA p φ hφ u x0 hu)⟩ :
        (a : A (p J.i1)) × B ⟨p J.i1, a⟩) = u J.i1 h
    rw [(FB (sigQ O FA p φ hφ u x0 hu)).ext _ _ _ _ _ h]
    exact sigma_cast (P := fun a => B ⟨p J.i1, a⟩) (u J.i1 h) (sigFE O FA p φ hφ u x0 hu J.i1 h)

/-- Σ of fibrations is a fibration -/
def fibSigma : Fib J C (fun γ => (a : A γ) × B ⟨γ, a⟩) := fun p => sigComp O FA FB p

end sigma

section paths

variable {J : Interval} {C : Cof J} (O : OrRec) (FE : FunI J)
variable {Γ : Type} {A : Γ → Type} (FA : Fib J C A)

/-- the family of path types over Σ γ, A γ × A γ -/
def PathFam (J : Interval) {Γ : Type} {A : Γ → Type} (z : (γ : Γ) × (A γ × A γ)) : Type := Path J (A z.1) z.2.1 z.2.2

/-- the partial element on φ ∨ (j = i0 ∨ j = i1) -/
def pathPE (p : J.I → (γ : Γ) × (A γ × A γ)) (φ : Prop) (u : (i : J.I) → φ → PathFam J (A := A) (p i)) (j : J.I)
    (i : J.I) : (φ ∨ (j = J.i0 ∨ j = J.i1)) → A (p i).1 :=
  O.glue (fun h => (u i h).p j)
    (O.glue (fun e0 => (p i).2.1) (fun e1 => (p i).2.2) (fun e0 e1 => absurd (e0.symm.trans e1) J.ne))
    (fun h ee => by
      cases ee with
      | inl e0 => rw [O.glue_l _ _ _ e0, e0]; exact (u i h).src
      | inr e1 => rw [O.glue_r _ _ _ e1, e1]; exact (u i h).tgt)

theorem cof_ends (C : Cof J) (j : J.I) : C.isCof (j = J.i0 ∨ j = J.i1) := C.or (C.eq0 j) (C.eq1 j)

/-- composition of paths, pointwise in j -/
def pathAt (p : J.I → (γ : Γ) × (A γ × A γ)) (φ : Prop) (hφ : C.isCof φ)
    (u : (i : J.I) → φ → PathFam J (A := A) (p i)) (q0 : PathFam J (A := A) (p J.i0)) (hu : ∀ h, u J.i0 h = q0)
    (j : J.I) : A (p J.i1).1 :=
  (FA (fun i => (p i).1)).c (φ ∨ (j = J.i0 ∨ j = J.i1)) (C.or hφ (cof_ends C j)) (pathPE O p φ u j) (q0.p j)
    (fun hh => by
      unfold pathPE
      cases hh with
      | inl h => rw [O.glue_l _ _ _ h, hu h]
      | inr ee => cases ee with
        | inl e0 => rw [O.glue_r _ _ _ (.inl e0), O.glue_l _ _ _ e0, e0]; exact q0.src.symm
        | inr e1 => rw [O.glue_r _ _ _ (.inr e1), O.glue_r _ _ _ e1, e1]; exact q0.tgt.symm)

/-- composition in path types -/
def pathComp (p : J.I → (γ : Γ) × (A γ × A γ)) : Comp J C (fun i => PathFam J (A := A) (p i)) where
  c φ hφ u q0 hu := ⟨pathAt O FA p φ hφ u q0 hu,
    by
      unfold pathAt
      rw [(FA _).ext _ _ _ _ _ (.inr (.inl rfl)), pathPE, O.glue_r _ _ _ (.inl rfl), O.glue_l _ _ _ rfl],
    by
      unfold pathAt
      rw [(FA _).ext _ _ _ _ _ (.inr (.inr rfl)), pathPE, O.glue_r _ _ _ (.inr rfl), O.glue_r _ _ _ rfl]⟩
  ext φ hφ u q0 hu h := by
    have e : pathAt O FA p φ hφ u q0 hu = (u J.i1 h).p := FE.ext (fun j => by
      unfold pathAt
      rw [(FA _).ext _ _ _ _ _ (.inl h), pathPE, O.glue_l _ _ _ h])
    cases hq : u J.i1 h with
    | mk pp s t =>
      rw [hq] at e
      show (⟨pathAt O FA p φ hφ u q0 hu, _, _⟩ : Path J (A (p J.i1).1) _ _) = ⟨pp, s, t⟩
      cases e
      rfl

/-- path types of a fibration form a fibration -/
def fibPath : Fib J C (PathFam J (A := A)) := fun p => pathComp O FE FA p

end paths

end OP

end Found

#print axioms Found.OP.sigma_cast
#print axioms Found.OP.pair_cast
#print axioms Found.OP.sigComp
#print axioms Found.OP.fibSigma
#print axioms Found.OP.pathComp
#print axioms Found.OP.fibPath

found/OP5.lean

lean_all/found/OP5.lean

/-
  T127 (core, slice OP5). found/OP5 — Glue types and univalence paths
  (Orton–Pitts).  The strictification property of the topos (a partial type
  on a cofibrant φ, isomorphic on φ to a total type B, extends to a total
  type isomorphic to B, strictly agreeing with the partial type on φ) is a
  hypothesis (Strict), never a Lean axiom; Cubical Agda's Glue is an
  instance (work/agda/Univalence.agda).  With gluing of partial elements of
  the universe on a disjunction (OrRec1):
  * ua: every isomorphism A ≅ B gives a path of types A ~> B over the
    interval (the line i ↦ Glue on the boundary φ_i = (i = 0 ∨ i = 1)),
  * the glued isomorphism at the start of the line is the given one (uaβ at
    the level of the glued equivalence).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.OP4

namespace Found

namespace OP

/-- isomorphisms of types -/
structure TIso (A B : Type) where
  to : A → B
  inv : B → A
  li : ∀ a, inv (to a) = a
  ri : ∀ b, to (inv b) = b

def TIso.refl (A : Type) : TIso A A := ⟨fun a => a, fun a => a, fun _ => rfl, fun _ => rfl⟩

/-- gluing partial elements of Type 1 on a disjunction -/
structure OrRec1 where
  glue : ∀ {φ ψ : Prop} {A : Type 1} (f : φ → A) (g : ψ → A), (∀ h h', f h = g h') → (φ ∨ ψ) → A
  glue_l : ∀ {φ ψ : Prop} {A : Type 1} (f : φ → A) (g : ψ → A) (e : ∀ h h', f h = g h') (h : φ),
    glue f g e (.inl h) = f h
  glue_r : ∀ {φ ψ : Prop} {A : Type 1} (f : φ → A) (g : ψ → A) (e : ∀ h h', f h = g h') (h : ψ),
    glue f g e (.inr h) = g h

/-- strictification: Glue types -/
structure Strict (J : Interval) (C : Cof J) where
  G : (φ : Prop) → C.isCof φ → (T : φ → Type) → (B : Type) → ((h : φ) → TIso (T h) B) → Type
  eqT : ∀ φ hφ T B f (h : φ), T h = G φ hφ T B f
  iso : ∀ φ hφ T B f, TIso (G φ hφ T B f) B
  iso_ext : ∀ φ hφ T B f (h : φ) (x : T h), (iso φ hφ T B f).to (castT (eqT φ hφ T B f h) x) = (f h).to x

section ua

variable {J : Interval} {C : Cof J} (O1 : OrRec1) (S : Strict J C) {A B : Type} (e : TIso A B)

/-- the boundary of the interval -/
def bd (J : Interval) (i : J.I) : Prop := i = J.i0 ∨ i = J.i1

theorem cof_bd (i : J.I) : C.isCof (bd J i) := C.or (C.eq0 i) (C.eq1 i)

/-- the partial type with its isomorphism to B on the boundary: (A, e) at 0, (B, id) at 1 -/
def bdPart (i : J.I) : bd J i → (X : Type) × TIso X B :=
  O1.glue (fun _ => ⟨A, e⟩) (fun _ => ⟨B, TIso.refl B⟩) (fun e0 e1 => absurd (e0.symm.trans e1) J.ne)

/-- the Glue line -/
def uaLine (i : J.I) : Type :=
  S.G (bd J i) (cof_bd i) (fun h => (bdPart O1 e i h).1) B (fun h => (bdPart O1 e i h).2)

theorem bdPart_0 : bdPart O1 e J.i0 (Or.inl rfl) = ⟨A, e⟩ := O1.glue_l _ _ _ rfl

theorem bdPart_1 : bdPart O1 e J.i1 (Or.inr rfl) = ⟨B, TIso.refl B⟩ := O1.glue_r _ _ _ rfl

/-- paths of types over the interval -/
structure TPath (J : Interval) (A B : Type) where
  p : J.I → Type
  src : p J.i0 = A
  tgt : p J.i1 = B

/-- univalence as a path of types over the interval -/
def ua : TPath J A B where
  p := uaLine O1 S e
  src := (S.eqT _ _ _ _ _ (Or.inl rfl)).symm.trans (congrArg Sigma.fst (bdPart_0 O1 e))
  tgt := (S.eqT _ _ _ _ _ (Or.inr rfl)).symm.trans (congrArg Sigma.fst (bdPart_1 O1 e))

/-- the isomorphism glued at a boundary point -/
theorem glued_iso_at (i : J.I) (h : bd J i) (x : (bdPart O1 e i h).1) :
    (S.iso _ (cof_bd i) (fun h => (bdPart O1 e i h).1) B (fun h => (bdPart O1 e i h).2)).to
      (castT (S.eqT _ (cof_bd i) (fun h => (bdPart O1 e i h).1) B (fun h => (bdPart O1 e i h).2) h) x) =
      (bdPart O1 e i h).2.to x :=
  S.iso_ext (bd J i) (cof_bd i) (fun h => (bdPart O1 e i h).1) B (fun h => (bdPart O1 e i h).2) h x

theorem sig_to {A B : Type} {e : TIso A B} (b : (X : Type) × TIso X B) (hb : b = ⟨A, e⟩) (a : A) :
    b.2.to (castT (congrArg Sigma.fst hb).symm a) = e.to a := by
  cases hb; rfl

/-- uaβ, at the level of the glued isomorphism: at the start of the line it is e -/
theorem ua_beta_iso (a : A) :
    (S.iso _ (cof_bd J.i0) (fun h => (bdPart O1 e J.i0 h).1) B (fun h => (bdPart O1 e J.i0 h).2)).to (castT (S.eqT _ (cof_bd J.i0) (fun h => (bdPart O1 e J.i0 h).1) B
      (fun h => (bdPart O1 e J.i0 h).2) (Or.inl rfl)) (castT (congrArg Sigma.fst (bdPart_0 O1 e)).symm a)) = e.to a := by
  rw [glued_iso_at O1 S e J.i0 (Or.inl rfl)]
  exact sig_to _ (bdPart_0 O1 e) a

end ua

end OP

end Found

#print axioms Found.OP.uaLine
#print axioms Found.OP.ua
#print axioms Found.OP.glued_iso_at
#print axioms Found.OP.ua_beta_iso

found/OP6.lean

lean_all/found/OP6.lean

/-
  T127 (core, slice OP6). found/OP6 — composition for Glue lines and the
  computation rule of univalence (Orton–Pitts).
  * Composition transfers along a pointwise isomorphism of lines: if Y has
    composition and X i ≅ Y i for every i, then X has composition (compose
    the image and pull back; the result extends the partial path because the
    isomorphisms are inverse).
  * The Glue line of an isomorphism e : A ≅ B (OP5) is pointwise isomorphic
    to B (strictification), so it is a fibration over the interval whenever
    B is (fibUa).
  * uaβ: if composition in B is regular on the empty face (a constant line
    transports trivially; true in Cubical Agda's transp), then transport
    along ua e, read at the start in A and at the end in B, is e itself.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.OP5

namespace Found

namespace OP

section glueComp

variable {J : Interval} {C : Cof J}

/-- composition transfers along a pointwise isomorphism of lines -/
def compIso {X Y : J.I → Type} (E : ∀ i, TIso (X i) (Y i)) (F : Comp J C Y) : Comp J C X where
  c φ hφ u a0 hu := (E J.i1).inv (F.c φ hφ (fun i h => (E i).to (u i h)) ((E J.i0).to a0)
    (fun h => congrArg (E J.i0).to (hu h)))
  ext φ hφ u _ _ h := (congrArg (E J.i1).inv (F.ext φ hφ _ _ _ h)).trans ((E J.i1).li (u J.i1 h))

/-- fibrations transfer along a pointwise isomorphism of families -/
def fibIso {Γ : Type} {X Y : Γ → Type} (E : ∀ γ, TIso (X γ) (Y γ)) (F : Fib J C Y) : Fib J C X :=
  fun p => compIso (fun i => E (p i)) (F p)

variable (O1 : OrRec1) (S : Strict J C) {A B : Type} (e : TIso A B)

/-- the Glue line is pointwise isomorphic to B -/
def uaIso (i : J.I) : TIso (uaLine O1 S e i) B :=
  S.iso _ (cof_bd i) (fun h => (bdPart O1 e i h).1) B (fun h => (bdPart O1 e i h).2)

/-- composition on the Glue line -/
def uaComp (F : Comp J C (fun _ => B)) : Comp J C (uaLine O1 S e) := compIso (uaIso O1 S e) F

/-- the Glue line is a fibration over the interval -/
def fibUa (F : Fib J C (fun _ : J.I => B)) : Fib J C (uaLine O1 S e) := fibIso (uaIso O1 S e) F

/-- at the end of the line the glued isomorphism is the identity of B -/
theorem uaIso_1_inv (y : B) :
    (uaIso O1 S e J.i1).inv y = castT (S.eqT _ (cof_bd J.i1) (fun h => (bdPart O1 e J.i1 h).1) B
      (fun h => (bdPart O1 e J.i1 h).2) (Or.inr rfl)) (castT (congrArg Sigma.fst (bdPart_1 O1 e)).symm y) :=
  have h := (glued_iso_at O1 S e J.i1 (Or.inr rfl) (castT (congrArg Sigma.fst (bdPart_1 O1 e)).symm y)).trans
    (sig_to _ (bdPart_1 O1 e) y)
  (congrArg (uaIso O1 S e J.i1).inv h).symm.trans ((uaIso O1 S e J.i1).li _)

/-- reading the end of the Glue line in B undoes the glued isomorphism -/
theorem ua_end (y : B) : castT (ua O1 S e).tgt ((uaIso O1 S e J.i1).inv y) = y :=
  (congrArg (castT (ua O1 S e).tgt) (uaIso_1_inv O1 S e y)).trans
    ((castT_trans _ _ _).trans ((castT_trans _ _ y).trans (castT_self _ y)))

/-- reading the start of the Glue line in A, the glued isomorphism is e -/
theorem ua_start (a : A) : (uaIso O1 S e J.i0).to (castT (ua O1 S e).src.symm a) = e.to a :=
  (congrArg (uaIso O1 S e J.i0).to (castT_trans (congrArg Sigma.fst (bdPart_0 O1 e)).symm
    (S.eqT _ (cof_bd J.i0) (fun h => (bdPart O1 e J.i0 h).1) B (fun h => (bdPart O1 e J.i0 h).2) (Or.inl rfl)) a).symm).trans
    (ua_beta_iso O1 S e a)

/-- regularity of composition on the empty face -/
def Reg (F : Comp J C (fun _ => B)) : Prop :=
  ∀ u b hu, F.c (J.i0 = J.i1) (cof_empty J C) u b hu = b

/-- uaβ: transport along ua e is e -/
theorem ua_beta (F : Comp J C (fun _ => B)) (hF : Reg F)
    (u : (i : J.I) → (J.i0 = J.i1) → uaLine O1 S e i) (a : A) (hu : ∀ h, u J.i0 h = castT (ua O1 S e).src.symm a) :
    castT (ua O1 S e).tgt ((uaComp O1 S e F).c _ (cof_empty J C) u (castT (ua O1 S e).src.symm a) hu) = e.to a :=
  (congrArg (fun y => castT (ua O1 S e).tgt ((uaIso O1 S e J.i1).inv y)) (hF _ _ _)).trans
    ((ua_end O1 S e _).trans (ua_start O1 S e a))

/-- uaβ for the transport of OP3 -/
theorem ua_beta_transp (F : Comp J C (fun _ => B)) (hF : Reg F) (a : A) :
    castT (ua O1 S e).tgt (transp J C (uaComp O1 S e F) (castT (ua O1 S e).src.symm a)) = e.to a :=
  ua_beta O1 S e F hF _ a _

end glueComp

end OP

end Found

#print axioms Found.OP.compIso
#print axioms Found.OP.fibIso
#print axioms Found.OP.fibUa
#print axioms Found.OP.uaIso_1_inv
#print axioms Found.OP.ua_end
#print axioms Found.OP.ua_start
#print axioms Found.OP.ua_beta
#print axioms Found.OP.ua_beta_transp

found/OP7.lean

lean_all/found/OP7.lean

/-
  T127 (core, slice OP7). found/OP7 — a univalent universe (Orton–Pitts).
  A universe is a type of codes U with decoding El, closed under Glue codes
  (the code of a Glue type decodes to the strictified type of OP5 and agrees
  on its face with the partial code).  The universe itself is data (a
  hypothesis structure, as in Licata–Orton–Pitts–Spitters where its
  construction needs a tiny interval), never a Lean axiom.  Then:
  * every isomorphism e : El a ≅ El b gives a path a ~> b in U (uaPath),
  * the decoded line along it is a fibration whenever El b is (fibUaU),
  * uaβ: transport along the decoded line, read at the start in El a and at
    the end in El b, is e (given regular composition on the empty face).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.OP6

namespace Found

namespace OP

/-- composition of isomorphisms -/
def TIso.comp {X Y Z : Type} (e1 : TIso X Y) (e2 : TIso Y Z) : TIso X Z :=
  ⟨fun x => e2.to (e1.to x), fun z => e1.inv (e2.inv z),
   fun x => (congrArg e1.inv (e2.li (e1.to x))).trans (e1.li x),
   fun z => (congrArg e2.to (e1.ri (e2.inv z))).trans (e2.ri z)⟩

/-- the isomorphism given by an equality of types -/
def castIso {X Y : Type} (h : X = Y) : TIso X Y :=
  ⟨castT h, castT h.symm, castT_back h, castT_back h.symm⟩

/-- a universe closed under Glue codes -/
structure Univ (J : Interval) (C : Cof J) (S : Strict J C) where
  U : Type
  El : U → Type
  code : (φ : Prop) → C.isCof φ → (T : φ → U) → (b : U) → ((h : φ) → TIso (El (T h)) (El b)) → U
  el_code : ∀ φ hφ T b f, El (code φ hφ T b f) = S.G φ hφ (fun h => El (T h)) (El b) f
  code_on : ∀ φ hφ T b f (h : φ), code φ hφ T b f = T h

section univ

variable {J : Interval} {C : Cof J} {S : Strict J C} (O : OrRec) (V : Univ J C S)
  {a b : V.U} (e : TIso (V.El a) (V.El b))

theorem sigU_to {b : V.U} {x : V.U} {e : TIso (V.El x) (V.El b)} (p : (X : V.U) × TIso (V.El X) (V.El b))
    (hp : p = ⟨x, e⟩) (y : V.El x) :
    p.2.to (castT (congrArg (fun q : (X : V.U) × TIso (V.El X) (V.El b) => V.El q.1) hp).symm y) = e.to y := by
  cases hp; rfl

/-- the partial code on the boundary: (a, e) at 0, (b, id) at 1 -/
def bdU (i : J.I) : bd J i → (X : V.U) × TIso (V.El X) (V.El b) :=
  O.glue (fun _ => ⟨a, e⟩) (fun _ => ⟨b, TIso.refl _⟩) (fun e0 e1 => absurd (e0.symm.trans e1) J.ne)

theorem bdU_0 : bdU O V e J.i0 (Or.inl rfl) = ⟨a, e⟩ := O.glue_l _ _ _ rfl

theorem bdU_1 : bdU O V e J.i1 (Or.inr rfl) = ⟨b, TIso.refl _⟩ := O.glue_r _ _ _ rfl

/-- the Glue code line -/
def uaU (i : J.I) : V.U :=
  V.code (bd J i) (cof_bd i) (fun h => (bdU O V e i h).1) b (fun h => (bdU O V e i h).2)

theorem uaU_0 : uaU O V e J.i0 = a :=
  (V.code_on _ _ _ _ _ (Or.inl rfl)).trans (congrArg Sigma.fst (bdU_0 O V e))

theorem uaU_1 : uaU O V e J.i1 = b :=
  (V.code_on _ _ _ _ _ (Or.inr rfl)).trans (congrArg Sigma.fst (bdU_1 O V e))

/-- univalence: a path in the universe from an isomorphism -/
def uaPath : Path J V.U a b := ⟨uaU O V e, uaU_0 O V e, uaU_1 O V e⟩

/-- the decoded line is pointwise isomorphic to El b -/
def elIso (i : J.I) : TIso (V.El (uaU O V e i)) (V.El b) :=
  TIso.comp (castIso (V.el_code _ (cof_bd i) _ b _))
    (S.iso _ (cof_bd i) (fun h => V.El (bdU O V e i h).1) (V.El b) (fun h => (bdU O V e i h).2))

/-- the decoded line is a fibration whenever El b is -/
def fibUaU (F : Fib J C (fun _ : J.I => V.El b)) : Fib J C (fun i => V.El (uaU O V e i)) :=
  fibIso (elIso O V e) F

/-- composition on the decoded line -/
def compUaU (F : Comp J C (fun _ => V.El b)) : Comp J C (fun i => V.El (uaU O V e i)) :=
  compIso (elIso O V e) F

/-- at the start the glued isomorphism is e -/
theorem elIso_start (x : V.El a) :
    (elIso O V e J.i0).to (castT (congrArg V.El (uaU_0 O V e)).symm x) = e.to x :=
  let q := congrArg (fun p : (X : V.U) × TIso (V.El X) (V.El b) => V.El p.1) (bdU_0 O V e)
  have A := (castT_trans (congrArg V.El (uaU_0 O V e)).symm
    (V.el_code _ (cof_bd J.i0) (fun h => (bdU O V e J.i0 h).1) b (fun h => (bdU O V e J.i0 h).2)) x).trans
    (castT_trans q.symm (S.eqT _ (cof_bd J.i0) (fun h => V.El (bdU O V e J.i0 h).1) (V.El b)
      (fun h => (bdU O V e J.i0 h).2) (Or.inl rfl)) x).symm
  (congrArg (S.iso _ (cof_bd J.i0) (fun h => V.El (bdU O V e J.i0 h).1) (V.El b)
      (fun h => (bdU O V e J.i0 h).2)).to A).trans
    ((S.iso_ext _ (cof_bd J.i0) (fun h => V.El (bdU O V e J.i0 h).1) (V.El b)
      (fun h => (bdU O V e J.i0 h).2) (Or.inl rfl) _).trans (sigU_to V _ (bdU_0 O V e) x))

/-- at the end the glued isomorphism is the identity -/
theorem elIso_end (y : V.El b) :
    castT (congrArg V.El (uaU_1 O V e)) ((elIso O V e J.i1).inv y) = y :=
  let q := congrArg (fun p : (X : V.U) × TIso (V.El X) (V.El b) => V.El p.1) (bdU_1 O V e)
  let E := S.eqT _ (cof_bd J.i1) (fun h => V.El (bdU O V e J.i1 h).1) (V.El b)
      (fun h => (bdU O V e J.i1 h).2) (Or.inr rfl)
  let I := S.iso _ (cof_bd J.i1) (fun h => V.El (bdU O V e J.i1 h).1) (V.El b) (fun h => (bdU O V e J.i1 h).2)
  have h1 : I.to (castT E (castT q.symm y)) = y :=
    (S.iso_ext _ (cof_bd J.i1) (fun h => V.El (bdU O V e J.i1 h).1) (V.El b)
      (fun h => (bdU O V e J.i1 h).2) (Or.inr rfl) _).trans (sigU_to V _ (bdU_1 O V e) y)
  have h2 : I.inv y = castT E (castT q.symm y) := (congrArg I.inv h1).symm.trans (I.li _)
  (congrArg (fun z => castT (congrArg V.El (uaU_1 O V e))
      (castT (V.el_code _ (cof_bd J.i1) (fun h => (bdU O V e J.i1 h).1) b (fun h => (bdU O V e J.i1 h).2)).symm z)) h2).trans
    ((congrArg (castT (congrArg V.El (uaU_1 O V e))) ((castT_trans _ _ _).trans (castT_trans _ _ _))).trans
      ((castT_trans _ _ _).trans (castT_self _ y)))

/-- uaβ in the universe: transport along the decoded line of ua e is e -/
theorem uaU_beta (F : Comp J C (fun _ => V.El b)) (hF : Reg F)
    (u : (i : J.I) → (J.i0 = J.i1) → V.El (uaU O V e i)) (x : V.El a)
    (hu : ∀ h, u J.i0 h = castT (congrArg V.El (uaU_0 O V e)).symm x) :
    castT (congrArg V.El (uaU_1 O V e))
      ((compUaU O V e F).c _ (cof_empty J C) u (castT (congrArg V.El (uaU_0 O V e)).symm x) hu) = e.to x :=
  (congrArg (fun y => castT (congrArg V.El (uaU_1 O V e)) ((elIso O V e J.i1).inv y)) (hF _ _ _)).trans
    ((elIso_end O V e _).trans (elIso_start O V e x))

end univ

end OP

end Found

#print axioms Found.OP.TIso.comp
#print axioms Found.OP.castIso
#print axioms Found.OP.sigU_to
#print axioms Found.OP.uaU_0
#print axioms Found.OP.uaU_1
#print axioms Found.OP.uaPath
#print axioms Found.OP.fibUaU
#print axioms Found.OP.elIso_start
#print axioms Found.OP.elIso_end
#print axioms Found.OP.uaU_beta

found/OTT.lean

lean_all/found/OTT.lean

/-
  T121. found/OTT — observational type theory as a point: equality is
  defined by the structure of the type (on Π pointwise, on Σ componentwise,
  on Prop by ↔), which is exactly the core's setoid stem (T59).
  * On function setoids the observational equality is pointwise: funext is
    a definitional theorem (Lean's Eq would need the funext axiom, T62).
  * On Ω it is ↔: propositional extensionality is definitional too.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Core

namespace Found

namespace OTT

open Core

universe u

theorem funext_obs {X Y : Setd.{u}} (f g : SHom X Y) (h : ∀ x, Y.eqv (f.fn x) (g.fn x)) :
    (expS X Y).eqv f g := h

theorem prop_ext_obs (p q : Prop) (h : p ↔ q) : (OmegaU.{u}).eqv ⟨p⟩ ⟨q⟩ := h

theorem pair_obs {X Y : Setd.{u}} (a b : (prodS X Y).car) (h1 : X.eqv a.1 b.1) (h2 : Y.eqv a.2 b.2) :
    (prodS X Y).eqv a b := ⟨h1, h2⟩

end OTT

end Found

#print axioms Found.OTT.funext_obs
#print axioms Found.OTT.prop_ext_obs
#print axioms Found.OTT.pair_obs

found/Omega1.lean

lean_all/found/Omega1.lean

/-
  T106 (b), slice N3c(i). found/Omega1 — ω₁ in the classical point: in the
  Zermelo well-order of P(ℕ) = (ℕ → Prop), the elements whose initial
  segment is countable form a type W1 that is uncountable, while each of
  its proper initial segments is countable (the defining property of ω₁).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.NFParams

namespace Found

namespace W1

open CP WO Card NFP

variable (K : Classical')

abbrev PN := Nat → Prop

def Seg (x : PN) : Type := {y : PN // Lt' K y x}

def Countable (A : Type) : Prop := ∃ f : A → Nat, Inj f

def W1 : Type := {x : PN // Countable (Seg K x)}

/-- each proper initial segment of W1 is countable -/
theorem seg_countable (x : W1 K) : Countable {y : W1 K // Lt' K y.val x.val} :=
  match x.property with
  | ⟨f, hf⟩ => ⟨fun y => f ⟨y.val.val, y.property⟩, fun a b e => by
      have := hf _ _ e
      cases a with
      | mk a ha => cases b with
        | mk b hb => cases a with
          | mk a' ha' => cases b with
            | mk b' hb' =>
              have : a' = b' := congrArg Subtype.val this
              subst this
              rfl⟩

include K in
/-- W1 is uncountable -/
theorem uncountable : ¬ Countable (W1 K) := fun ⟨g, hg⟩ => by
  rcases K.lem (∃ z : PN, ¬ Countable (Seg K z)) with hz | hall
  · match least K (fun z => ¬ Countable (Seg K z)) hz with
    | ⟨m, hm, hl⟩ =>
      -- every element below m is in W1, so Seg m injects into W1, hence into ℕ
      apply hm
      refine ⟨fun y => g ⟨y.val, dne K _ (fun hn => y.property.2 (hl y.val hn))⟩, fun a b e => ?_⟩
      have := hg _ _ e
      cases a with
      | mk a ha => cases b with
        | mk b hb =>
          have : a = b := congrArg Subtype.val this
          subst this
          rfl
  · have hc : ∀ z : PN, Countable (Seg K z) := fun z => dne K _ (fun hn => hall ⟨z, hn⟩)
    exact (cantor Nat).2 ⟨fun z => g ⟨z, hc z⟩, fun a b e => congrArg Subtype.val (hg _ _ e)⟩
      |> fun h => h.elim

end W1

end Found

#print axioms Found.W1.seg_countable
#print axioms Found.W1.uncountable

found/Omega1Sub.lean

lean_all/found/Omega1Sub.lean

/-
  T106 (b), slice N3c(ii). found/Omega1Sub — in the classical point, every
  uncountable subset S of ω₁ has size ω₁: the increasing enumeration of S
  (transfinite recursion along ω₁; each step takes an element of S above
  the countably many earlier values, which are bounded by cofinality, and S
  is unbounded since bounded sets are countable) is an injection ω₁ ↪ S.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.BethStages

namespace Found

namespace W1Sub

open CP WO Card W1 BethS

variable (K : Classical') (S : W1 K → Prop) (hS : ¬ Countable {x // S x})

theorem lt_le {a b c : PN} (h : Lt' K a b) (h' : Le K b c) : Lt' K a c :=
  ⟨le_trans K h.1 h', fun hca => h.2 (le_trans K h' hca)⟩

theorem lt_irrefl (a : PN) : ¬ Lt' K a a := fun h => h.2 h.1

include hS in
/-- S is unbounded in ω₁ -/
theorem unbounded (b : W1 K) : ∃ t, S t ∧ Le K b.val t.val :=
  dne K _ (fun hn => hS (match upto_countable K b with
    | ⟨f, hf⟩ =>
      have hle : ∀ t : {x // S x}, Le K t.val.val b.val := fun t =>
        (le_total K t.val.val b.val).elim id (fun h => (hn ⟨t.val, t.property, h⟩).elim)
      ⟨fun t => f ⟨t.val, hle t⟩, fun x y e => by
        have := congrArg Subtype.val (hf _ _ e)
        cases x with
        | mk x hx => cases y with
          | mk y hy => cases this; rfl⟩))

def Spec (α : W1 K) (rec : ∀ β, Lt K β α → {x // S x}) (s : {x // S x}) : Prop :=
  ∀ β h, Lt K (rec β h).val s.val

include hS in
theorem spec_ex (α : W1 K) (rec : ∀ β, Lt K β α → {x // S x}) : ∃ s, Spec K S α rec s :=
  match bounded K {β // Lt K β α} (seg_countable K α) (fun i => (rec i.val i.property).val) with
  | ⟨b, hb⟩ => match unbounded K S hS b with
    | ⟨t, ht, hle⟩ => ⟨⟨t, ht⟩, fun β h => lt_le K (hb ⟨β, h⟩) hle⟩

/-- the increasing enumeration of S -/
def g : W1 K → {x // S x} :=
  (wfW K).fix (fun α rec => K.ch (Spec K S α rec) (spec_ex K S hS α rec))

theorem g_spec (α : W1 K) : ∀ β, Lt K β α → Lt K (g K S hS β).val (g K S hS α).val := by
  have e : g K S hS α = K.ch (Spec K S α (fun β _ => g K S hS β)) (spec_ex K S hS α (fun β _ => g K S hS β)) :=
    (wfW K).fix_eq _ α
  rw [e]
  exact K.ch_spec (Spec K S α (fun β _ => g K S hS β)) (spec_ex K S hS α (fun β _ => g K S hS β))

theorem g_inj : Inj (g K S hS) := fun a b e => dne K _ (fun ne => by
  rcases le_total K a.val b.val with h | h
  · have hl : Lt K a b := ⟨h, fun h' => ne (sub_eq K (le_antisymm K _ _ h h'))⟩
    have := g_spec K S hS b a hl
    rw [e] at this
    exact lt_irrefl K _ this
  · have hl : Lt K b a := ⟨h, fun h' => ne (sub_eq K (le_antisymm K _ _ h' h))⟩
    have := g_spec K S hS a b hl
    rw [e] at this
    exact lt_irrefl K _ this)

include hS in
/-- every uncountable subset of ω₁ has size ω₁ -/
theorem omega1_sub : ∃ f : W1 K → {x // S x}, Inj f := ⟨g K S hS, g_inj K S hS⟩

end W1Sub

end Found

#print axioms Found.W1Sub.unbounded
#print axioms Found.W1Sub.g_spec
#print axioms Found.W1Sub.g_inj
#print axioms Found.W1Sub.omega1_sub

found/Operad.lean

lean_all/found/Operad.lean

/-
  T193 line. found/Operad — (planar) operads as a point, in the
  partial-composition presentation: operations with arities, a unit of
  arity 1, and F ∘ᵢ G of arity |F| + |G| - 1, subject to the unit laws and
  the sequential and parallel associativity laws.
  * The endomorphism operad End X (operations = functions on lists of
    inputs, F ∘ᵢ G plugs G into the i-th input) satisfies all the laws,
    up to agreement on inputs of the right length.
  * An algebra of an operad on X is a law-preserving map into End X.
  Own list toolkit (append, take, drop, length) proved by structural
  recursion, since the core list lemmas use propext.

  Lean 4 core only, hand-written proofs, no axioms.
-/
import lib.Semi

namespace Found

namespace Opd

variable {X : Type}

def app : List X → List X → List X
  | [], l => l
  | a :: k, l => a :: app k l

def len : List X → Nat
  | [] => 0
  | _ :: l => len l + 1

def tk : Nat → List X → List X
  | 0, [] => []
  | 0, _ :: _ => []
  | _ + 1, [] => []
  | n + 1, a :: l => a :: tk n l

def dr : Nat → List X → List X
  | 0, [] => []
  | 0, a :: l => a :: l
  | _ + 1, [] => []
  | n + 1, _ :: l => dr n l

theorem app_nil : (l : List X) → app l [] = l
  | [] => rfl
  | a :: l => congrArg (a :: ·) (app_nil l)

theorem app_assoc : (a b c : List X) → app (app a b) c = app a (app b c)
  | [], _, _ => rfl
  | x :: a, b, c => congrArg (x :: ·) (app_assoc a b c)

theorem tk_zero : (l : List X) → tk 0 l = []
  | [] => rfl
  | _ :: _ => rfl

theorem dr_zero : (l : List X) → dr 0 l = l
  | [] => rfl
  | _ :: _ => rfl

theorem tk_nil : (n : Nat) → tk n ([] : List X) = []
  | 0 => rfl
  | _ + 1 => rfl

theorem dr_nil : (n : Nat) → dr n ([] : List X) = []
  | 0 => rfl
  | _ + 1 => rfl

theorem tk_app : (a r : List X) → (k : Nat) → tk (len a + k) (app a r) = app a (tk k r)
  | [], r, k => by
    show tk (0 + k) r = tk k r
    rw [Nat.zero_add]
  | x :: a, r, k => by
    show tk (len a + 1 + k) (x :: app a r) = x :: app a (tk k r)
    rw [Nat.succ_add]
    exact congrArg (x :: ·) (tk_app a r k)

theorem dr_app : (a r : List X) → (k : Nat) → dr (len a + k) (app a r) = dr k r
  | [], r, k => by
    show dr (0 + k) r = dr k r
    rw [Nat.zero_add]
  | x :: a, r, k => by
    show dr (len a + 1 + k) (x :: app a r) = dr k r
    rw [Nat.succ_add]
    exact dr_app a r k

theorem tk_app0 (a r : List X) : tk (len a) (app a r) = a := by
  have := tk_app a r 0
  rw [Nat.add_zero, tk_zero, app_nil] at this
  exact this

theorem dr_app0 (a r : List X) : dr (len a) (app a r) = r := by
  have := dr_app a r 0
  rw [Nat.add_zero, dr_zero] at this
  exact this

theorem tk_add : (i j : Nat) → (l : List X) → tk (i + j) l = app (tk i l) (tk j (dr i l))
  | 0, j, l => by rw [Nat.zero_add, tk_zero, dr_zero]; rfl
  | i + 1, j, [] => by rw [Nat.succ_add, tk_nil, tk_nil, dr_nil, tk_nil]; rfl
  | i + 1, j, a :: l => by
    rw [Nat.succ_add]
    exact congrArg (a :: ·) (tk_add i j l)

theorem dr_add : (i j : Nat) → (l : List X) → dr (i + j) l = dr j (dr i l)
  | 0, j, l => by rw [Nat.zero_add, dr_zero]
  | i + 1, j, [] => by rw [Nat.succ_add, dr_nil, dr_nil, dr_nil]
  | i + 1, j, _ :: l => by
    rw [Nat.succ_add]
    exact dr_add i j l

theorem len_tk : (i : Nat) → (l : List X) → i ≤ len l → len (tk i l) = i
  | 0, [], _ => rfl
  | 0, _ :: _, _ => rfl
  | _ + 1, [], h => absurd h (Nat.not_succ_le_zero _)
  | i + 1, _ :: l, h => congrArg (· + 1) (len_tk i l (Nat.le_of_succ_le_succ h))

theorem len_dr : (i m : Nat) → (l : List X) → len l = i + m → len (dr i l) = m
  | 0, m, [], h => h.trans (Nat.zero_add m)
  | 0, m, _ :: _, h => h.trans (Nat.zero_add m)
  | i + 1, m, [], h => Nat.noConfusion ((Nat.succ_add i m).symm.trans h.symm)
  | i + 1, m, _ :: l, h => len_dr i m l (Nat.succ.inj (h.trans (Nat.succ_add i m)))

theorem tk_len : (l : List X) → tk (len l) l = l
  | [] => rfl
  | a :: l => congrArg (a :: ·) (tk_len l)

theorem tk_tk : (i k : Nat) → (l : List X) → tk i (tk (i + k) l) = tk i l
  | 0, _, l => by rw [tk_zero, tk_zero]
  | i + 1, k, [] => by rw [tk_nil, tk_nil]
  | i + 1, k, a :: l => by
    rw [Nat.succ_add]
    exact congrArg (a :: ·) (tk_tk i k l)

theorem dr_tk : (i k : Nat) → (l : List X) → dr i (tk (i + k) l) = tk k (dr i l)
  | 0, k, l => by rw [Nat.zero_add, dr_zero, dr_zero]
  | i + 1, k, [] => by rw [tk_nil, dr_nil, tk_nil]
  | i + 1, k, _ :: l => by
    rw [Nat.succ_add]
    exact dr_tk i k l

def hd (x0 : X) : List X → X
  | [] => x0
  | a :: _ => a

theorem split (x0 : X) : (i : Nat) → (xs : List X) → i < len xs →
    app (tk i xs) (hd x0 (tk 1 (dr i xs)) :: dr (i + 1) xs) = xs
  | 0, [], h => absurd h (Nat.not_lt_zero 0)
  | 0, a :: l, _ => by
    show a :: dr 0 l = a :: l
    rw [dr_zero]
  | _ + 1, [], h => absurd h (Nat.not_lt_zero _)
  | i + 1, a :: l, h => congrArg (a :: ·) (split x0 i l (Nat.lt_of_succ_lt_succ h))

/-! ## the endomorphism operad -/

structure Op (X : Type) where
  n : Nat
  f : List X → X

def comp (F G : Op X) (i : Nat) : Op X :=
  ⟨Nat.pred (G.n + F.n), fun xs => F.f (app (tk i xs) (G.f (tk G.n (dr i xs)) :: dr (i + G.n) xs))⟩

def unit (x0 : X) : Op X := ⟨1, hd x0⟩

def Eqv (F G : Op X) : Prop := F.n = G.n ∧ ∀ xs, len xs = F.n → F.f xs = G.f xs

theorem eqv_refl (F : Op X) : Eqv F F := ⟨rfl, fun _ _ => rfl⟩
theorem eqv_symm {F G : Op X} (h : Eqv F G) : Eqv G F := ⟨h.1.symm, fun xs hx => (h.2 xs (hx.trans h.1.symm)).symm⟩
theorem eqv_trans {F G H : Op X} (h : Eqv F G) (k : Eqv G H) : Eqv F H :=
  ⟨h.1.trans k.1, fun xs hx => (h.2 xs hx).trans (k.2 xs (hx.trans h.1))⟩

theorem unit_left (x0 : X) (G : Op X) : Eqv (comp (unit x0) G 0) G :=
  ⟨rfl, fun xs hx => by
    show hd x0 (app (tk 0 xs) (G.f (tk G.n (dr 0 xs)) :: dr (0 + G.n) xs)) = G.f xs
    have hx' : len xs = G.n := hx
    rw [tk_zero, dr_zero, ← hx', tk_len]
    rfl⟩

theorem pred_one_add (n : Nat) : Nat.pred (1 + n) = n := by
  rw [Nat.add_comm]
  rfl

theorem unit_right (x0 : X) (F : Op X) (i : Nat) (hi : i < F.n) : Eqv (comp F (unit x0) i) F :=
  ⟨pred_one_add F.n, fun xs hx => by
    have hx' : len xs = F.n := hx.trans (pred_one_add F.n)
    show F.f (app (tk i xs) (hd x0 (tk 1 (dr i xs)) :: dr (i + 1) xs)) = F.f xs
    rw [split x0 i xs (hx' ▸ hi)]⟩

theorem tk_app' (a r : List X) (k m : Nat) (h : len a = k) : tk (k + m) (app a r) = app a (tk m r) :=
  h ▸ tk_app a r m
theorem dr_app' (a r : List X) (k m : Nat) (h : len a = k) : dr (k + m) (app a r) = dr m r :=
  h ▸ dr_app a r m
theorem tk_app0' (a r : List X) (k : Nat) (h : len a = k) : tk k (app a r) = a := h ▸ tk_app0 a r
theorem dr_app0' (a r : List X) (k : Nat) (h : len a = k) : dr k (app a r) = r := h ▸ dr_app0 a r

theorem le_ex : {a b : Nat} → a ≤ b → ∃ r, b = a + r
  | _, _, .refl => ⟨0, rfl⟩
  | _, _, .step h => match le_ex h with
    | ⟨r, e⟩ => ⟨r + 1, by rw [e]; rfl⟩

/-- sequential associativity: (F ∘ᵢ G) ∘ᵢ₊ⱼ H ≈ F ∘ᵢ (G ∘ⱼ H) -/
theorem seq_assoc (F G H : Op X) (i j : Nat) (hi : i < F.n) (hj : j < G.n) :
    Eqv (comp (comp F G i) H (i + j)) (comp F (comp G H j) i) := by
  cases F with
  | mk fn ff => cases G with
    | mk gn gf => cases H with
      | mk hn hf =>
        cases fn with
        | zero => exact absurd hi (Nat.not_lt_zero _)
        | succ p => cases gn with
          | zero => exact absurd hj (Nat.not_lt_zero _)
          | succ q =>
            obtain ⟨s, hs⟩ := le_ex (Nat.le_of_lt_succ hi)
            obtain ⟨r, hr⟩ := le_ex (Nat.le_of_lt_succ hj)
            subst hs hr
            refine ⟨?_, fun xs hx => ?_⟩
            · show Nat.pred (hn + Nat.pred (j + r + 1 + (i + s + 1))) = Nat.pred (Nat.pred (hn + (j + r + 1)) + (i + s + 1))
              have e : j + r + 1 + (i + s + 1) = (j + r + 1 + (i + s)) + 1 := rfl
              rw [e]
              show Nat.pred (hn + (j + r + 1 + (i + s))) = hn + (j + r) + (i + s)
              have e2 : hn + (j + r + 1 + (i + s)) = (hn + (j + r) + (i + s)) + 1 := by lib_nat
              rw [e2]
              rfl
            · have e2 : hn + (j + r + 1 + (i + s)) = (hn + (j + r) + (i + s)) + 1 := by lib_nat
              have hx1 : len xs = Nat.pred (hn + (j + r + 1 + (i + s))) := hx
              rw [e2] at hx1
              have hx1' : len xs = hn + (j + r) + (i + s) := hx1
              have hx2 : len xs = i + (s + (hn + (j + r))) := hx1'.trans (by lib_nat)
              have lA : len (tk i xs) = i := len_tk i xs (hx2 ▸ Nat.le_add_right _ _)
              have lD : len (dr i xs) = s + (hn + (j + r)) := len_dr i _ xs hx2
              have lB : len (tk j (dr i xs)) = j :=
                len_tk j _ (by rw [lD, show s + (hn + (j + r)) = j + (s + hn + r) by lib_nat]; exact Nat.le_add_right _ _)
              -- left side
              have hy : app (tk (i + j) xs) (hf (tk hn (dr (i + j) xs)) :: dr (i + j + hn) xs)
                  = app (tk i xs) (app (tk j (dr i xs)) (hf (tk hn (dr (i + j) xs)) :: dr (i + j + hn) xs)) := by
                rw [tk_add, app_assoc]
              show ff (app (tk i _) (gf (tk (j + r + 1) (dr i _)) :: dr (i + (j + r + 1)) _)) = ff (app (tk i xs)
                (gf (app (tk j (tk (hn + (j + r)) (dr i xs))) (hf (tk hn (dr j (tk (hn + (j + r)) (dr i xs))))
                  :: dr (j + hn) (tk (hn + (j + r)) (dr i xs)))) :: dr (i + (hn + (j + r))) xs))
              rw [hy, tk_app0' _ _ i lA, dr_add i (j + r + 1), dr_app0' _ _ i lA]
              have L3 := tk_app' (tk j (dr i xs)) (hf (tk hn (dr (i + j) xs)) :: dr (i + j + hn) xs) j (r + 1) lB
              have L4 := dr_app' (tk j (dr i xs)) (hf (tk hn (dr (i + j) xs)) :: dr (i + j + hn) xs) j (r + 1) lB
              have eA : j + r + 1 = j + (r + 1) := rfl
              rw [eA, L3, L4]
              -- right side
              have z1 : hn + (j + r) = j + (r + hn) := by lib_nat
              have z2 : hn + (j + r) = (j + hn) + r := by lib_nat
              have R1 : tk j (tk (hn + (j + r)) (dr i xs)) = tk j (dr i xs) := by rw [z1, tk_tk]
              have R2 : tk hn (dr j (tk (hn + (j + r)) (dr i xs))) = tk hn (dr (i + j) xs) := by
                rw [z1, dr_tk, Nat.add_comm r hn, tk_tk, dr_add]
              have R3 : dr (j + hn) (tk (hn + (j + r)) (dr i xs)) = tk r (dr (i + j + hn) xs) := by
                rw [z2, dr_tk, ← dr_add, Nat.add_assoc]
              have R4 : dr (i + (hn + (j + r))) xs = dr r (dr (i + j + hn) xs) := by
                rw [← dr_add, show i + (hn + (j + r)) = i + j + hn + r by lib_nat]
              rw [R1, R2, R3, R4]
              rfl

/-- parallel associativity: (F ∘ₖ H) ∘ᵢ G ≈ (F ∘ᵢ G) ∘ₖ' H for i < k, k' = k + |G| - 1 -/
theorem par_assoc (F G H : Op X) (i c : Nat) (hk : i + (c + 1) < F.n) :
    Eqv (comp (comp F H (i + (c + 1))) G i) (comp (comp F G i) H (i + (G.n + c))) := by
  cases F with
  | mk fn ff => cases G with
    | mk gn gf => cases H with
      | mk hn hf =>
        cases fn with
        | zero => exact absurd hk (Nat.not_lt_zero _)
        | succ p =>
          obtain ⟨s, hs⟩ := le_ex (Nat.le_of_lt_succ hk)
          subst hs
          have aL : gn + (hn + (i + (c + 1) + s)) = (gn + hn + i + c + s) + 1 := by lib_nat
          have aR : hn + (gn + (i + (c + 1) + s)) = (gn + hn + i + c + s) + 1 := by lib_nat
          refine ⟨?_, fun xs hx => ?_⟩
          · show Nat.pred (gn + (hn + (i + (c + 1) + s))) = Nat.pred (hn + (gn + (i + (c + 1) + s)))
            rw [aL, aR]
          · have hx1 : len xs = Nat.pred (gn + (hn + (i + (c + 1) + s))) := hx
            rw [aL] at hx1
            have hx1' : len xs = gn + hn + i + c + s := hx1
            have hx2 : len xs = i + (gn + (c + hn + s)) := hx1'.trans (by lib_nat)
            have lA : len (tk i xs) = i := len_tk i xs (hx2 ▸ Nat.le_add_right _ _)
            have lD : len (dr i xs) = gn + (c + hn + s) := len_dr i _ xs hx2
            have l1 : len (tk gn (dr i xs)) = gn := len_tk gn _ (lD ▸ Nat.le_add_right _ _)
            show ff (app (tk (i + (c + 1)) (app (tk i xs) (gf (tk gn (dr i xs)) :: dr (i + gn) xs)))
                (hf (tk hn (dr (i + (c + 1)) (app (tk i xs) (gf (tk gn (dr i xs)) :: dr (i + gn) xs))))
                  :: dr (i + (c + 1) + hn) (app (tk i xs) (gf (tk gn (dr i xs)) :: dr (i + gn) xs))))
              = ff (app (tk i (app (tk (i + (gn + c)) xs) (hf (tk hn (dr (i + (gn + c)) xs)) :: dr (i + (gn + c) + hn) xs)))
                (gf (tk gn (dr i (app (tk (i + (gn + c)) xs) (hf (tk hn (dr (i + (gn + c)) xs)) :: dr (i + (gn + c) + hn) xs))))
                  :: dr (i + gn) (app (tk (i + (gn + c)) xs) (hf (tk hn (dr (i + (gn + c)) xs)) :: dr (i + (gn + c) + hn) xs))))
            -- left side
            have eL : i + (c + 1) + hn = i + ((c + hn) + 1) := by lib_nat
            rw [tk_app' _ _ i (c + 1) lA, dr_app' _ _ i (c + 1) lA, eL, dr_app' _ _ i _ lA]
            -- right side
            have hZ : app (tk (i + (gn + c)) xs) (hf (tk hn (dr (i + (gn + c)) xs)) :: dr (i + (gn + c) + hn) xs)
                = app (tk i xs) (app (tk gn (dr i xs)) (app (tk c (dr gn (dr i xs)))
                    (hf (tk hn (dr (i + (gn + c)) xs)) :: dr (i + (gn + c) + hn) xs))) := by
              rw [tk_add i (gn + c) xs, tk_add gn c (dr i xs), app_assoc, app_assoc]
            have D : ∀ l, dr (i + gn) (app (tk i xs) l) = dr gn l := fun l => by rw [dr_add, dr_app0' _ _ i lA]
            have E2 : dr (i + (gn + c)) xs = dr c (dr gn (dr i xs)) := by rw [dr_add i, dr_add gn]
            have E3 : dr (i + (gn + c) + hn) xs = dr (c + hn) (dr gn (dr i xs)) := by
              rw [show i + (gn + c) + hn = i + (gn + (c + hn)) by lib_nat, dr_add i, dr_add gn]
            rw [hZ, tk_app0' _ _ i lA, D, dr_app0' _ _ i lA, tk_app0' _ _ gn l1, dr_app0' _ _ gn l1, E2, E3, dr_add i gn xs, app_assoc]
            rfl

theorem len_app : (a b : List X) → len (app a b) = len a + len b
  | [], b => (Nat.zero_add _).symm
  | _ :: a, b => by
    show len (app a b) + 1 = len a + 1 + len b
    rw [len_app a b, Nat.succ_add]

theorem len_tk_eq (n : Nat) (l : List X) (h : len l = n) : len (tk n l) = n := len_tk n l (h ▸ Nat.le_refl _)

/-- composition respects agreement -/
theorem comp_resp {F F' G G' : Op X} (i : Nat) (hi : i < F.n) (hF : Eqv F F') (hG : Eqv G G') :
    Eqv (comp F G i) (comp F' G' i) := by
  cases F with
  | mk fn ff => cases F' with
    | mk fn' ff' => cases G with
      | mk gn gf => cases G' with
        | mk gn' gf' =>
          have e1 : fn = fn' := hF.1
          have e2 : gn = gn' := hG.1
          subst e1 e2
          cases fn with
          | zero => exact absurd hi (Nat.not_lt_zero _)
          | succ p =>
            obtain ⟨s, hs⟩ := le_ex (Nat.le_of_lt_succ hi)
            subst hs
            refine ⟨rfl, fun xs hx => ?_⟩
            have hx1 : len xs = gn + (i + s) := hx
            have hx2 : len xs = i + (gn + s) := hx1.trans (by lib_nat)
            have lA : len (tk i xs) = i := len_tk i xs (hx2 ▸ Nat.le_add_right _ _)
            have lD : len (dr i xs) = gn + s := len_dr i _ xs hx2
            have lG : len (tk gn (dr i xs)) = gn := len_tk gn _ (lD ▸ Nat.le_add_right _ _)
            have lR : len (dr (i + gn) xs) = s := len_dr (i + gn) s xs (hx2.trans (Nat.add_assoc _ _ _).symm)
            show ff (app (tk i xs) (gf (tk gn (dr i xs)) :: dr (i + gn) xs))
              = ff' (app (tk i xs) (gf' (tk gn (dr i xs)) :: dr (i + gn) xs))
            have hg : gf (tk gn (dr i xs)) = gf' (tk gn (dr i xs)) := hG.2 _ lG
            rw [hg]
            refine hF.2 _ ?_
            show len (app (tk i xs) (gf' (tk gn (dr i xs)) :: dr (i + gn) xs)) = i + s + 1
            rw [len_app, lA]
            show i + (len (dr (i + gn) xs) + 1) = i + s + 1
            rw [lR]
            rfl

/-! ## operads and algebras -/

/-- a (planar) operad in the partial-composition presentation -/
structure Operad where
  Op : Type
  ar : Op → Nat
  e : Op
  comp : Op → Op → Nat → Op
  eqv : Op → Op → Prop
  refl : ∀ F, eqv F F
  symm : ∀ {F G}, eqv F G → eqv G F
  trans : ∀ {F G H}, eqv F G → eqv G H → eqv F H
  ar_e : ar e = 1
  ar_comp : ∀ F G i, i < ar F → ar (comp F G i) = Nat.pred (ar G + ar F)
  resp : ∀ {F F' G G'} i, i < ar F → eqv F F' → eqv G G' → eqv (comp F G i) (comp F' G' i)
  unit_l : ∀ G, eqv (comp e G 0) G
  unit_r : ∀ F i, i < ar F → eqv (comp F e i) F
  seq : ∀ F G H i j, i < ar F → j < ar G → eqv (comp (comp F G i) H (i + j)) (comp F (comp G H j) i)
  par : ∀ F G H i c, i + (c + 1) < ar F →
    eqv (comp (comp F H (i + (c + 1))) G i) (comp (comp F G i) H (i + (ar G + c)))

/-- the endomorphism operad of X (x0 only fills the unused default of the unit) -/
def End (x0 : X) : Operad where
  Op := Op X
  ar F := F.n
  e := unit x0
  comp := comp
  eqv := Eqv
  refl := eqv_refl
  symm := eqv_symm
  trans := eqv_trans
  ar_e := rfl
  ar_comp _ _ _ _ := rfl
  resp i hi hF hG := comp_resp i hi hF hG
  unit_l := unit_left x0
  unit_r := unit_right x0
  seq := seq_assoc
  par := par_assoc

/-- an algebra of an operad on X: a law-preserving map into End X -/
structure Alg (O : Operad) (X : Type) (x0 : X) where
  act : O.Op → Op X
  ar : ∀ F, (act F).n = O.ar F
  resp : ∀ {F G}, O.eqv F G → Eqv (act F) (act G)
  unit : Eqv (act O.e) (Opd.unit x0)
  comp : ∀ F G i, i < O.ar F → Eqv (act (O.comp F G i)) (Opd.comp (act F) (act G) i)

/-- End X is an algebra over itself (on X) -/
def tautological (x0 : X) : Alg (End x0) X x0 where
  act F := F
  ar _ := rfl
  resp h := h
  unit := eqv_refl _
  comp _ _ _ _ := eqv_refl _

end Opd

end Found

#print axioms Found.Opd.split
#print axioms Found.Opd.unit_left
#print axioms Found.Opd.unit_right
#print axioms Found.Opd.seq_assoc
#print axioms Found.Opd.par_assoc
#print axioms Found.Opd.comp_resp
#print axioms Found.Opd.End
#print axioms Found.Opd.tautological

found/OrdArith.lean

lean_all/found/OrdArith.lean

/-
  T124 (b), part 1. found/OrdArith — arithmetic of the notations below ε₀
  (found/Ordinal): transitivity of the order, ordinal addition on Cantor
  normal forms, its preservation of normal forms, strict monotonicity in the
  right argument, and the bounds used by cut elimination (ω^a + ω^a < ω^b for
  a < b).

  Lean 4 core only, hand-written proofs, no axioms.
-/
import found.Ordinal

namespace Found

namespace Ord

theorem cmpN_trans_lt : (a b c : Nat) → cmpN a b = .lt → cmpN b c = .lt → cmpN a c = .lt
  | 0, 0, _, h, _ => C3.noConfusion h
  | 0, _ + 1, 0, _, h => C3.noConfusion h
  | 0, _ + 1, _ + 1, _, _ => rfl
  | _ + 1, 0, _, h, _ => C3.noConfusion h
  | _ + 1, _ + 1, 0, _, h => C3.noConfusion h
  | a + 1, b + 1, c + 1, h1, h2 => cmpN_trans_lt a b c h1 h2

theorem cmpN_eq_lt {a b c : Nat} (h1 : cmpN a b = .eq) (h2 : cmpN b c = .lt) : cmpN a c = .lt := by
  rw [cmpN_eq a b h1]; exact h2

theorem cmpN_lt_eq {a b c : Nat} (h1 : cmpN a b = .lt) (h2 : cmpN b c = .eq) : cmpN a c = .lt := by
  rw [← cmpN_eq b c h2]; exact h1

theorem cmpN_of_lt : {m k : Nat} → m < k → cmpN m k = .lt
  | 0, 0, h => absurd h (Nat.lt_irrefl 0)
  | 0, _ + 1, _ => rfl
  | _ + 1, 0, h => absurd h (Nat.not_lt_zero _)
  | m + 1, k + 1, h => cmpN_of_lt (m := m) (k := k) (Nat.lt_of_succ_lt_succ h)

theorem lt_of_exp {a a' : ON} {n n' : Nat} {b b' : ON} (h : Lt a a') : Lt (.oadd a n b) (.oadd a' n' b') := by
  show (match cmp a a' with
    | .lt => .lt | .gt => .gt
    | .eq => match cmpN n n' with | .lt => .lt | .gt => .gt | .eq => cmp b b') = C3.lt
  have h' : cmp a a' = .lt := h
  rw [h']

theorem lt_of_coef {a : ON} {n n' : Nat} {b b' : ON} (h : n < n') : Lt (.oadd a n b) (.oadd a n' b') := by
  show (match cmp a a with
    | .lt => .lt | .gt => .gt
    | .eq => match cmpN n n' with | .lt => .lt | .gt => .gt | .eq => cmp b b') = C3.lt
  rw [cmp_refl, cmpN_of_lt h]

theorem lt_of_tail {a : ON} {n : Nat} {b b' : ON} (h : Lt b b') : Lt (.oadd a n b) (.oadd a n b') := by
  show (match cmp a a with
    | .lt => .lt | .gt => .gt
    | .eq => match cmpN n n with | .lt => .lt | .gt => .gt | .eq => cmp b b') = C3.lt
  rw [cmp_refl, cmpN_refl]
  exact h

/-- transitivity of the order on all notations -/
theorem lt_trans : (x y z : ON) → Lt x y → Lt y z → Lt x z
  | .zero, .zero, _, h, _ => C3.noConfusion h
  | .zero, .oadd _ _ _, .zero, _, h => C3.noConfusion h
  | .zero, .oadd _ _ _, .oadd _ _ _, _, _ => rfl
  | .oadd _ _ _, .zero, _, h, _ => C3.noConfusion h
  | .oadd _ _ _, .oadd _ _ _, .zero, _, h => C3.noConfusion h
  | .oadd a n b, .oadd a' n' b', .oadd a'' n'' b'', h1, h2 => by
    rcases lt_oadd_inv h1 with ha | ⟨e1, hn⟩ | ⟨e1, e2, hb⟩
    · rcases lt_oadd_inv h2 with ha' | ⟨e, _⟩ | ⟨e, _, _⟩
      · exact lt_of_exp (lt_trans a a' a'' ha ha')
      · subst e; exact lt_of_exp ha
      · subst e; exact lt_of_exp ha
    · subst e1
      rcases lt_oadd_inv h2 with ha' | ⟨e, hn'⟩ | ⟨e, en, _⟩
      · exact lt_of_exp ha'
      · subst e; exact lt_of_coef (Nat.lt_trans hn hn')
      · subst e; subst en; exact lt_of_coef hn
    · subst e1 e2
      rcases lt_oadd_inv h2 with ha' | ⟨e, hn'⟩ | ⟨e, en, hb'⟩
      · exact lt_of_exp ha'
      · subst e; exact lt_of_coef hn'
      · subst e; subst en; exact lt_of_tail (lt_trans b b' b'' hb hb')

/-! ## ordinal addition on Cantor normal forms -/

def add : ON → ON → ON
  | .zero, y => y
  | .oadd a n b, .zero => .oadd a n b
  | .oadd a n b, .oadd a' n' b' => match cmp a a' with
    | .lt => .oadd a' n' b'
    | .eq => .oadd a (n + n' + 1) b'
    | .gt => .oadd a n (add b (.oadd a' n' b'))

theorem add_eq_lt {a n b a' n' b'} (h : cmp a a' = .lt) : add (.oadd a n b) (.oadd a' n' b') = .oadd a' n' b' := by
  show (match cmp a a' with
    | .lt => ON.oadd a' n' b' | .eq => .oadd a (n + n' + 1) b' | .gt => .oadd a n (add b (.oadd a' n' b'))) = _
  rw [h]
theorem add_eq_eq {a n b a' n' b'} (h : cmp a a' = .eq) : add (.oadd a n b) (.oadd a' n' b') = .oadd a (n + n' + 1) b' := by
  show (match cmp a a' with
    | .lt => ON.oadd a' n' b' | .eq => .oadd a (n + n' + 1) b' | .gt => .oadd a n (add b (.oadd a' n' b'))) = _
  rw [h]
theorem add_eq_gt {a n b a' n' b'} (h : cmp a a' = .gt) :
    add (.oadd a n b) (.oadd a' n' b') = .oadd a n (add b (.oadd a' n' b')) := by
  show (match cmp a a' with
    | .lt => ON.oadd a' n' b' | .eq => .oadd a (n + n' + 1) b' | .gt => .oadd a n (add b (.oadd a' n' b'))) = _
  rw [h]

theorem gt_iff {a b : ON} (h : cmp a b = .gt) : Lt b a := by
  unfold Lt; rw [cmp_swap, h]; rfl

/-- x < x + y for y ≠ 0 -/
theorem lt_add_pos : (x : ON) → (a' : ON) → (n' : Nat) → (b' : ON) → Lt x (add x (.oadd a' n' b'))
  | .zero, _, _, _ => rfl
  | .oadd a n b, a', n', b' => by
    cases h : cmp a a' with
    | lt => rw [add_eq_lt h]; exact lt_of_exp h
    | eq => rw [add_eq_eq h]; exact lt_of_coef (Nat.lt_succ_of_le (Nat.le_add_right n n'))
    | gt => rw [add_eq_gt h]; exact lt_of_tail (lt_add_pos b a' n' b')

/-- ordinal addition is strictly monotone in its right argument -/
theorem add_mono : (x y y' : ON) → Lt y y' → Lt (add x y) (add x y')
  | .zero, _, _, h => h
  | .oadd a n b, .zero, .zero, h => absurd h (lt_irrefl _)
  | .oadd a n b, .zero, .oadd a2 n2 b2, _ => lt_add_pos _ a2 n2 b2
  | .oadd a n b, .oadd _ _ _, .zero, h => C3.noConfusion h
  | .oadd a n b, .oadd a1 n1 b1, .oadd a2 n2 b2, h => by
    have hy := lt_oadd_inv h
    cases h1 : cmp a a1 with
    | lt =>
      -- a < a1 ≤ a2
      have ha2 : Lt a a2 := by
        rcases hy with h' | ⟨e, _⟩ | ⟨e, _, _⟩
        · exact lt_trans _ _ _ h1 h'
        · rw [← e]; exact h1
        · rw [← e]; exact h1
      rw [add_eq_lt h1, add_eq_lt ha2]; exact h
    | eq =>
      have e1 := cmp_eq _ _ h1
      subst e1
      rcases hy with h' | ⟨e, hn⟩ | ⟨e, en, hb⟩
      · rw [add_eq_eq h1, add_eq_lt h']; exact lt_of_exp h'
      · subst e; rw [add_eq_eq h1, add_eq_eq h1]
        exact lt_of_coef (Nat.succ_lt_succ (Nat.add_lt_add_left hn n))
      · subst e; subst en; rw [add_eq_eq h1, add_eq_eq h1]; exact lt_of_tail hb
    | gt =>
      rw [add_eq_gt h1]
      cases h2 : cmp a a2 with
      | lt => rw [add_eq_lt h2]; exact lt_of_exp h2
      | eq =>
        have e2 := cmp_eq _ _ h2
        rw [add_eq_eq h2]
        exact lt_of_coef (Nat.lt_succ_of_le (Nat.le_add_right n n2))
      | gt => rw [add_eq_gt h2]; exact lt_of_tail (add_mono b _ _ h)

/-- the cut-elimination bound: ω^c + ω^d < ω^e when c, d < e -/
theorem omega_add_lt {c d e : ON} (hc : Lt c e) (hd : Lt d e) :
    Lt (add (.oadd c 0 .zero) (.oadd d 0 .zero)) (.oadd e 0 .zero) := by
  cases h : cmp c d with
  | lt => rw [add_eq_lt h]; exact lt_of_exp hd
  | eq => rw [add_eq_eq h]; exact lt_of_exp hc
  | gt => rw [add_eq_gt h]; exact lt_of_exp hc

end Ord

end Found

#print axioms Found.Ord.lt_trans
#print axioms Found.Ord.lt_add_pos
#print axioms Found.Ord.add_mono
#print axioms Found.Ord.omega_add_lt