Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

Typed field coordinates and quartic guard data

Definition
rsg_quartic_runtime_04

by sirawit · Nov 30, 2026 · Mathlib 0df444a (Lean v4.33.1)

complexity-theorygraph-theorygroup-theory

Defines the fixed field coordinates, the second profile, the quartic data tensor, and the typed guard and coefficient data for extracting an assignment.

Definition code
import Definitions.Def_rsg_quartic_runtime_03
import Mathlib
import Definitions.Def_rsg_paper_structure
import Mathlib.LinearAlgebra.Pi
import Mathlib.Data.Fintype.EquivFin
import Mathlib.Data.Finset.Card
import Mathlib.Tactic.Abel
import Definitions.Def_rsg_recognition_core
import Mathlib.Data.Fintype.BigOperators
import Mathlib.Combinatorics.SimpleGraph.Connectivity.Connected
import Mathlib.GroupTheory.PGroup
import Mathlib.GroupTheory.Nilpotent
import Mathlib.Combinatorics.SimpleGraph.Coloring.Vertex
import Mathlib.LinearAlgebra.Basis.VectorSpace
import Mathlib.LinearAlgebra.Isomorphisms
import Mathlib.Logic.Equiv.Defs
import Mathlib.Data.Fintype.Basic
import Mathlib.Data.Finset.Erase
import Mathlib.Logic.Function.Basic
import Mathlib.Data.Fintype.Sigma
import Mathlib.Algebra.BigOperators.Group.Finset.Basic
import Mathlib.LinearAlgebra.Dual.Defs
import Mathlib.LinearAlgebra.Basis.Prod
import Mathlib.LinearAlgebra.Dual.Basis
import Mathlib.Algebra.CharP.Two
import Mathlib.Algebra.Polynomial.Roots
import Mathlib.Data.Set.Card
import Mathlib.FieldTheory.Finite.Trace
import Mathlib.Tactic.FieldSimp
import Mathlib.Tactic.Ring
import Mathlib.LinearAlgebra.BilinearForm.Orthogonal
import Mathlib.LinearAlgebra.Dimension.Finrank
import Mathlib.FieldTheory.Finite.GaloisField
import Mathlib.Logic.Equiv.Fin.Basic

section

set_option autoImplicit false
set_option maxHeartbeats 1450000

noncomputable section

namespace RSGPaper.QuarticFieldCoordinates

open BooleanMonomials
open QuarticChannelLayout
open QuarticField

abbrev BinaryField (degree : ℕ) := GaloisField 2 degree

def basis (degree : ℕ) (positive : degree ≠ 0) : Module.Basis (Fin degree) Bit (BinaryField degree) :=
  QuarticBinaryField.fieldBasis degree (Nat.pos_of_ne_zero positive)

theorem basis_is_effective (degree : ℕ) (positive : degree ≠ 0) :
    basis degree positive = QuarticBinaryField.fieldBasis degree (Nat.pos_of_ne_zero positive) := rfl

def coordinates (degree : ℕ) (positive : degree ≠ 0) : Cube degree ≃ₗ[Bit] BinaryField degree :=
  (basis degree positive).equivFun.symm

def dualCoordinates (degree : ℕ) (positive : degree ≠ 0) :
    Cube degree ≃ₗ[Bit] Module.Dual Bit (BinaryField degree) :=
  (basis degree positive).dualBasis.equivFun.symm

def removeLinearCube (degree : ℕ) (positive : degree ≠ 0) : BinaryField degree →ₗ[Bit] BinaryField degree :=
  (basis degree positive).constr Bit (fun j => (basis degree positive j)^3)

theorem dual_evaluate (degree : ℕ) (positive : degree ≠ 0)
    (u v : Cube degree) : dualCoordinates degree positive v (coordinates degree positive u) = CubicGauge.dot u v := by
  let b := basis degree positive
  let t := dualCoordinates degree positive v
  let a := coordinates degree positive u
  have u_read (j : Fin degree) : b.repr a j = u j := by
    have h := congrFun (b.equivFun.apply_symm_apply u) j
    exact h
  have v_read (j : Fin degree) : (b.dualBasis.repr t) j = v j := by
    have h := congrFun (b.dualBasis.equivFun.apply_symm_apply v) j
    exact h
  have coeff (j : Fin degree) : t (b j) = v j := (Module.Basis.dualBasis_repr b t j).symm.trans (v_read j)
  change t a = CubicGauge.dot u v
  calc
    _ = t (∑ j, b.repr a j • b j) := congrArg t (b.sum_repr a).symm
    _ = ∑ j, u j * v j := by simp only [map_sum,map_smul,u_read,coeff,smul_eq_mul]
    _ = _ := rfl

attribute [irreducible] basis

end RSGPaper.QuarticFieldCoordinates

end

section

set_option autoImplicit false
set_option maxHeartbeats 1550000

namespace RSGPaper.QuarticDifferenceDegree

open BooleanMonomials

variable {A B : Type} [AddCommGroup A] [Module Bit A] [AddCommGroup B] [Module Bit B]

def Kills : ℕ → (A → Bit) → Prop
  | 0,f => f = 0
  | n+1,f => ∀ a, Kills n (QuarticJointGuards.delta a f)

def shift (a : A) (f : A → Bit) : A → Bit := fun x => f (x+a)

theorem delta_zero (a : A) : QuarticJointGuards.delta a (0 : A → Bit) = 0 := by
  funext x
  change (0 : Bit)+0=0
  simp

theorem kills_zero (n : ℕ) : Kills n (0 : A → Bit) := by
  induction n with
  | zero => rfl
  | succ n ih =>
    intro a
    rw [delta_zero]
    exact ih

theorem kills_later (n : ℕ) (f : A → Bit) (h : Kills n f) : Kills (n+1) f := by
  induction n generalizing f with
  | zero =>
    change f = 0 at h
    rw [h]
    exact kills_zero _
  | succ n ih =>
    intro a
    exact ih _ (h a)

theorem kills_mono (n m : ℕ) (h : n ≤ m) (f : A → Bit) (hf : Kills n f) : Kills m f := by
  induction m, h using Nat.le_induction with
  | base => exact hf
  | succ m _ ih => exact kills_later m f ih

theorem delta_add (a : A) (f g : A → Bit) : QuarticJointGuards.delta a (f+g) = QuarticJointGuards.delta a f+QuarticJointGuards.delta a g := by
  funext x
  change f (x+a)+g (x+a)+(f x+g x) = f (x+a)+f x+(g (x+a)+g x)
  abel

theorem kills_add (n : ℕ) (f g : A → Bit) (hf : Kills n f) (hg : Kills n g) : Kills n (f+g) := by
  induction n generalizing f g with
  | zero =>
    change f = 0 at hf
    change g = 0 at hg
    change f+g=0
    simp [hf,hg]
  | succ n ih =>
    intro a
    rw [delta_add]
    exact ih _ _ (hf a) (hg a)

theorem delta_shift (a b : A) (f : A → Bit) : QuarticJointGuards.delta b (shift a f) = shift a (QuarticJointGuards.delta b f) := by
  funext x
  simp only [QuarticJointGuards.delta,shift,add_assoc,add_comm a b]

theorem kills_shift (n : ℕ) (a : A) (f : A → Bit) (hf : Kills n f) : Kills n (shift a f) := by
  induction n generalizing f with
  | zero =>
    change f=0 at hf
    subst f
    rfl
  | succ n ih =>
    intro b
    rw [delta_shift]
    exact ih _ (hf b)

theorem delta_product (a : A) (f g : A → Bit) :
    QuarticJointGuards.delta a (f*g) = QuarticJointGuards.delta a f * shift a g + f * QuarticJointGuards.delta a g := by
  funext x
  change f (x+a)*g (x+a)+f x*g x =
    (f (x+a)+f x)*g (x+a)+f x*(g (x+a)+g x)
  calc
    _ = f (x+a)*g (x+a)+f x*g x+(f x*g (x+a)+f x*g (x+a)) := by
      rw [QuarticCayley.double,add_zero]
    _ = _ := by ring

theorem kills_product (m n : ℕ) (f g : A → Bit) (hf : Kills (m+1) f) (hg : Kills (n+1) g) :
    Kills (m+n+1) (f*g) := by
  have bound (total : ℕ) : ∀ m n : ℕ, m+n = total → ∀ f g : A → Bit,
      Kills (m+1) f → Kills (n+1) g → Kills (m+n+1) (f*g) := by
    induction total using Nat.strong_induction_on with
    | h total smaller =>
      intro p q sum u v hu hv a
      rw [delta_product]
      apply kills_add
      · cases p with
        | zero =>
          have vanish : QuarticJointGuards.delta a u = 0 := hu a
          rw [vanish,zero_mul]
          exact kills_zero _
        | succ p =>
          have lower := smaller (p+q) (by omega) p q rfl (QuarticJointGuards.delta a u)
            (shift a v) (hu a) (kills_shift _ a v hv)
          simpa only [Nat.succ_add] using lower
      · cases q with
        | zero =>
          have vanish : QuarticJointGuards.delta a v = 0 := hv a
          rw [vanish,mul_zero]
          exact kills_zero _
        | succ q => exact smaller (p+q) (by omega) p q rfl u (QuarticJointGuards.delta a v) hu (hv a)
  exact bound (m+n) m n rfl f g hf hg

theorem kills_constant (c : Bit) : Kills 1 (fun _ : A => c) := by
  intro a
  funext x
  exact QuarticCayley.double c

theorem kills_linear (f : A →ₗ[Bit] Bit) : Kills 2 f := by
  intro a
  have constant : QuarticJointGuards.delta a f = fun _ => f a := by
    funext x
    simp only [QuarticJointGuards.delta,map_add]
    rw [add_right_comm,QuarticCayley.double,zero_add]
  rw [constant]
  exact kills_constant _

theorem kills_smul (n : ℕ) (c : Bit) (f : A → Bit) (h : Kills n f) : Kills n (c•f) := by
  have bit : ∀ b : Bit, b=0 ∨ b=1 := by decide
  rcases bit c with rfl | rfl
  · rw [zero_smul]
    exact kills_zero _
  · rwa [one_smul]

theorem kills_sum {I : Type} (n : ℕ) (s : Finset I) (f : I → A → Bit)
    (h : ∀ i ∈ s, Kills n (f i)) : Kills n (∑ i ∈ s, f i) := by
  classical
  induction s using Finset.induction_on with
  | empty => simpa only [Finset.sum_empty] using (kills_zero n : Kills n (0 : A → Bit))
  | @insert i s outside ih =>
    rw [Finset.sum_insert outside]
    exact kills_add n _ _ (h i (Finset.mem_insert_self _ _)) (ih fun j mem => h j (Finset.mem_insert_of_mem mem))

theorem kills_product_linear {I : Type} (s : Finset I) (forms : I → A →ₗ[Bit] Bit) :
    Kills (s.card+1) (∏ i ∈ s, (forms i : A → Bit)) := by
  classical
  induction s using Finset.induction_on with
  | empty =>
    rw [Finset.card_empty,zero_add,Finset.prod_empty]
    change Kills 1 (fun _ : A => (1 : Bit))
    exact kills_constant 1
  | @insert i s outside ih =>
    rw [Finset.prod_insert outside,Finset.card_insert_of_notMem outside]
    simpa only [Nat.one_add] using kills_product 1 s.card _ _ (kills_linear (forms i)) ih

theorem kills_precompose (n : ℕ) (f : B → Bit) (map : A →ₗ[Bit] B) (hf : Kills n f) : Kills n (f ∘ map) := by
  induction n generalizing f with
  | zero =>
    change f=0 at hf
    subst f
    rfl
  | succ n ih =>
    intro a
    have eq : QuarticJointGuards.delta a (f ∘ map) = QuarticJointGuards.delta (map a) f ∘ map := by
      funext x
      simp [QuarticJointGuards.delta,map_add]
    rw [eq]
    exact ih _ (hf (map a))

variable {k : ℕ}

theorem kills_monomial (s : Finset (Fin k)) : Kills (s.card+1) (monomial s) := by
  have expand : monomial s = ∏ i ∈ s, (LinearMap.proj i : Cube k →ₗ[Bit] Bit).toFun := by
    funext x
    simp [monomial,Finset.prod_apply,LinearMap.proj_apply]
  rw [expand]
  exact kills_product_linear s (fun i => LinearMap.proj i)

theorem kills_eval (c : Coefficients k) (degree : ℕ) (bound : ∀ s, degree < s.card → c s = 0) :
    Kills (degree+1) (eval c) := by
  have expand : eval c = ∑ s : Finset (Fin k), c s • monomial s := by
    funext x
    simp [eval,Finset.sum_apply,smul_eq_mul]
  rw [expand]
  apply kills_sum
  intro s _
  by_cases good : s.card ≤ degree
  · exact kills_smul _ _ _ (kills_mono _ _ (Nat.add_le_add_right good 1) _ (kills_monomial s))
  · rw [bound s (Nat.lt_of_not_ge good),zero_smul]
    exact kills_zero _

theorem kills_gradient (c : Coefficients k) (degree : ℕ) (bound : ∀ s, degree+1 < s.card → c s = 0)
    (i : Fin k) : Kills (degree+1) (fun x => CubicGauge.gradient c x i) := by
  change Kills (degree+1) (eval (derivative i c))
  apply kills_eval
  intro s large
  unfold derivative
  split
  · rfl
  · rename_i outside
    apply bound
    rw [Finset.card_insert_of_notMem outside]
    omega

end RSGPaper.QuarticDifferenceDegree

end

section

set_option autoImplicit false
set_option maxHeartbeats 1550000

noncomputable section

namespace RSGPaper.QuarticNativeCorrection

open BooleanMonomials
open CubicGauge
open QuarticDirectionSpan
open QuarticFirstPolar
open QuarticFirstCorrection

variable {m : ℕ}

theorem canonical_difference (M : Cube m →ₗ[Bit] Cube m) (a x : Position m) :
    profile M (x+a)+profile M x = profile M a+polar a x := by
  have polarRead := QuarticNormalizeProfile.profile_polar M a x
  change profile M (a+x)+profile M a+profile M x = polar a x at polarRead
  calc
    _ = profile M (x+a)+profile M x+(profile M a+profile M a) := by
      rw [QuarticCayley.double,add_zero]
    _ = profile M a+(profile M (a+x)+profile M a+profile M x) := by
      rw [add_comm x a]
      abel
    _ = _ := by rw [polarRead]

end RSGPaper.QuarticNativeCorrection

end

section

set_option autoImplicit false
set_option maxHeartbeats 1450000

namespace RSGPaper.QuarticSecondProfile

open QuarticField
open QuarticSecondPolar
open QuarticNativeCorrection
open QuarticFirstPolar

variable {F : Type} [Field F] [CharP F 2] [Algebra (ZMod 2) F]

local notation "K" => ZMod 2

theorem difference (linear : F →ₗ[K] F) (a x : Position F) :
    secondProfile linear (x+a)+secondProfile linear x = secondProfile linear a+secondPolar a x := by
  have pol := second_polar_correct linear a x
  calc
    _ = secondProfile linear (x+a)+secondProfile linear x+(secondProfile linear a+secondProfile linear a) := by
      rw [QuarticCayley.double,add_zero]
    _ = secondProfile linear a+(secondProfile linear (a+x)+secondProfile linear a+secondProfile linear x) := by
      rw [add_comm x a]
      abel
    _ = _ := by rw [pol]

variable {m : ℕ}

/-- Transport the second quadratic profile from the field coordinates into the primary chart. -/
def native (P : QuarticDirectionSpan.Position m →ₗ[K] Position F)
    (embed : Gradient F →ₗ[K] QuarticDirectionSpan.Position m) (linear : F →ₗ[K] F)
    (x : QuarticDirectionSpan.Position m) : QuarticDirectionSpan.Position m := embed (secondProfile linear (P x))

theorem native_difference (P : QuarticDirectionSpan.Position m →ₗ[K] Position F)
    (embed : Gradient F →ₗ[K] QuarticDirectionSpan.Position m) (linear : F →ₗ[K] F)
    (a x : QuarticDirectionSpan.Position m) :
    native P embed linear (x+a)+native P embed linear x = native P embed linear a+embed (secondPolar (P a) (P x)) := by
  unfold native
  rw [map_add P,← map_add embed,difference,map_add embed]

end RSGPaper.QuarticSecondProfile

end

section

set_option autoImplicit false
set_option maxHeartbeats 1700000

noncomputable section

namespace RSGPaper.QuarticNativeSecond

open BooleanMonomials
open QuarticChannelLayout
open QuarticFieldCoordinates

abbrev width (data : ℕ) := fieldWidth data
abbrev E (data : ℕ) := BinaryField (width data)

 theorem width_pos (data : ℕ) : width data ≠ 0 := by
  unfold width fieldWidth
  omega

def blocks (data : ℕ) : Cube (nativeWidth data) ≃ₗ[Bit] Cube (width data) × Cube (width data) × Cube (width data) :=
  (splitCube (width data) (width data+width data)).trans
    ((LinearEquiv.refl Bit (Cube (width data))).prodCongr (splitCube (width data) (width data)))

def positionFields (data : ℕ) : Cube (nativeWidth data) ≃ₗ[Bit] QuarticSecondPolar.Position (E data) :=
  (blocks data).trans ((coordinates _ (width_pos data)).prodCongr
    ((dualCoordinates _ (width_pos data)).prodCongr (coordinates _ (width_pos data))))

def outputFields (data : ℕ) : Cube (nativeWidth data) ≃ₗ[Bit] QuarticSecondPolar.Gradient (E data) :=
  (blocks data).trans ((dualCoordinates _ (width_pos data)).prodCongr
    ((coordinates _ (width_pos data)).prodCongr (coordinates _ (width_pos data))))

def projection (data : ℕ) :
    QuarticDirectionSpan.Position (nativeWidth data) →ₗ[Bit] QuarticSecondPolar.Position (E data) :=
  (positionFields data).toLinearMap.comp (LinearMap.fst _ _ _)

def embedding (data : ℕ) :
    QuarticSecondPolar.Gradient (E data) →ₗ[Bit] QuarticDirectionSpan.Position (nativeWidth data) :=
  (LinearMap.inl _ _ _).comp (outputFields data).symm.toLinearMap

def profile (data : ℕ) :=
  QuarticSecondProfile.native (projection data) (embedding data) (removeLinearCube _ (width_pos data))

end RSGPaper.QuarticNativeSecond

end

section

set_option autoImplicit false
set_option maxHeartbeats 1450000

namespace RSGPaper.QuarticComplementPairs

open QuarticChannelLayout

def entries (p : Pair) : Finset Group := {p.val.1,p.val.2}

theorem entries_card (p : Pair) : (entries p).card = 2 := by
  have distinct : p.val.1 ≠ p.val.2 := ne_of_lt p.property
  rw [entries,Finset.card_insert_of_notMem (by simpa using distinct),Finset.card_singleton]

def remaining (p : Pair) : Finset Group := Finset.univ \ entries p

theorem remaining_card (p : Pair) : (remaining p).card = 2 := by
  rw [remaining,Finset.card_sdiff_of_subset (Finset.subset_univ _),entries_card]
  decide

private theorem remaining_nonempty (p : Pair) : (remaining p).Nonempty :=
  Finset.card_pos.mp (by rw [remaining_card]; decide)

private theorem remaining_extremes (p : Pair) :
    (remaining p).min' (remaining_nonempty p) < (remaining p).max' (remaining_nonempty p) := by
  let S := remaining p
  let lo := S.min' (remaining_nonempty p)
  let hi := S.max' (remaining_nonempty p)
  have read (x : Group) (inside : x ∈ S) : lo ≤ x ∧ x ≤ hi :=
    ⟨Finset.min'_le S x inside,Finset.le_max' S x inside⟩
  have lo_in : lo ∈ S := Finset.min'_mem S _
  apply lt_of_le_of_ne (read lo lo_in).2
  intro equal
  have only : S ⊆ {lo} := by
    intro x inside
    have r := read x inside
    rw [← equal] at r
    exact Finset.mem_singleton.mpr (le_antisymm r.2 r.1)
  have card := Finset.card_le_card only
  change (remaining p).card ≤ ({lo} : Finset Group).card at card
  rw [remaining_card,Finset.card_singleton] at card
  omega

def opposite (p : Pair) : Pair :=
  ⟨((remaining p).min' (remaining_nonempty p),(remaining p).max' (remaining_nonempty p)),remaining_extremes p⟩

theorem opposite_entries (p : Pair) : entries (opposite p) = remaining p := by
  have included : entries (opposite p) ⊆ remaining p := by
    intro x member
    rcases Finset.mem_insert.mp member with h | h
    · exact h ▸ Finset.min'_mem (remaining p) (remaining_nonempty p)
    · exact (Finset.mem_singleton.mp h) ▸ Finset.max'_mem (remaining p) (remaining_nonempty p)
  apply Finset.eq_of_subset_of_card_le included
  rw [remaining_card,entries_card]

end RSGPaper.QuarticComplementPairs

end

section

set_option autoImplicit false
set_option maxHeartbeats 1550000

namespace RSGPaper.QuarticSliceLayout

open QuarticChannelLayout
open QuarticComplementPairs

structure SliceTerm (side : ℕ) where
  allocation : Pair
  squared : Bool
  aux : Fin (side+1)
  left : Fin (side+1)
  right : Fin (side+1)
  deriving DecidableEq

instance (side : ℕ) : Fintype (SliceTerm side) := Fintype.ofEquiv
  (Pair × Bool × Fin (side+1) × Fin (side+1) × Fin (side+1))
    ⟨fun ⟨p,b,a,x,y⟩ => ⟨p,b,a,x,y⟩,fun t => (t.allocation,t.squared,t.aux,t.left,t.right),
      by rintro ⟨p,b,a,x,y⟩; rfl,by rintro ⟨p,b,a,x,y⟩; rfl⟩

variable {side : ℕ}

def leftCore (t : SliceTerm side) : DataIndex side := .inl ((opposite t.allocation).val.1,t.left)
def rightCore (t : SliceTerm side) : DataIndex side := .inl ((opposite t.allocation).val.2,t.right)

def sliceSupport (t : SliceTerm side) : Finset (DataIndex side) :=
  {selector t.allocation,auxiliary t.allocation t.squared t.aux,leftCore t,rightCore t}

theorem slice_card (t : SliceTerm side) : (sliceSupport t).card = 4 := by
  have separate : (opposite t.allocation).val.1 ≠ (opposite t.allocation).val.2 := ne_of_lt (opposite t.allocation).property
  have left : leftCore t ∉ ({rightCore t} : Finset (DataIndex side)) := by
    simp [leftCore,rightCore,separate]
  have aux : auxiliary t.allocation t.squared t.aux ∉ ({leftCore t,rightCore t} : Finset (DataIndex side)) := by
    simp [auxiliary,leftCore,rightCore]
  have select : selector t.allocation ∉ ({auxiliary t.allocation t.squared t.aux,leftCore t,rightCore t} : Finset (DataIndex side)) := by
    simp [selector,auxiliary,leftCore,rightCore]
  rw [sliceSupport,Finset.card_insert_of_notMem select,Finset.card_insert_of_notMem aux,
    Finset.card_insert_of_notMem left,Finset.card_singleton]

/-- All six allocations use disjoint named auxiliary slots, with one selector and two field copies per pair. -/
theorem group_image (t : SliceTerm side) : (sliceSupport t).image dataGroup = Finset.univ := by
  have first : (sliceSupport t).image dataGroup = entries t.allocation ∪ entries (opposite t.allocation) := by
    simp [sliceSupport,Finset.image_insert,entries,dataGroup,selector,auxiliary,leftCore,rightCore]
    ext g
    simp only [Finset.mem_insert,Finset.mem_singleton]
    tauto
  rw [first,opposite_entries,remaining]
  ext g
  simp

theorem groups_injective (t : SliceTerm side) : Set.InjOn dataGroup (sliceSupport t : Set (DataIndex side)) := by
  apply (Finset.card_image_iff.mp ?_)
  rw [group_image,slice_card]
  decide

def realSupport (slot : Slot side) : Finset (DataIndex side) := Finset.univ.image fun g => realCoord g (slot g)

theorem realSupport_card (slot : Slot side) : (realSupport slot).card = 4 := by
  unfold realSupport
  have inj : Function.Injective (fun g => realCoord g (slot g)) := by
    intro g h equal
    exact congrArg dataGroup equal
  rw [Finset.card_image_of_injective _ inj]
  decide

end RSGPaper.QuarticSliceLayout

end

section

set_option autoImplicit false

namespace RSGPaper.QuarticFiniteIndex

open QuarticChannelLayout

def binaryIndex : Bool ≃ Fin 2 where
  toFun b := if b then 1 else 0
  invFun i := decide (i = 1)
  left_inv := by decide
  right_inv := by decide

def pairAt : Fin 6 → Pair :=
  ![⟨(0,1),by decide⟩,⟨(0,2),by decide⟩,⟨(0,3),by decide⟩,
    ⟨(1,2),by decide⟩,⟨(1,3),by decide⟩,⟨(2,3),by decide⟩]

def pairRank (p : Pair) : Fin 6 :=
  if p.val.1 = 0 then ⟨p.val.2.val-1,by have := p.val.2.isLt; omega⟩
  else if p.val.1 = 1 then (if p.val.2 = 2 then 3 else 4) else 5

def pairIndex : Pair ≃ Fin 6 where
  toFun := pairRank
  invFun := pairAt
  left_inv := by decide
  right_inv := by decide

def dataIndex (side : ℕ) : DataIndex side ≃ Fin (dataWidth side) :=
  let core : Group × Fin (side+1) ≃ Fin (4*(side+1)) := finProdFinEquiv
  let aux : Pair × Bool × Fin (side+1) ≃ Fin (6*(2*(side+1))) :=
    (pairIndex.prodCongr ((binaryIndex.prodCongr (Equiv.refl _)).trans finProdFinEquiv)).trans finProdFinEquiv
  let branch := (pairIndex.sumCongr aux).trans finSumFinEquiv
  ((core.sumCongr branch).trans finSumFinEquiv).trans (finCongr (by unfold dataWidth; omega))

private def splitLast (k n : ℕ) : (Fin (k+1) → Fin n) ≃ (Fin k → Fin n) × Fin n where
  toFun x := (fun i => x i.succ,x 0)
  invFun x := Fin.cases x.2 x.1
  left_inv x := by funext i; refine Fin.cases ?_ (fun j => ?_) i <;> simp
  right_inv x := by apply Prod.ext <;> simp

def functionIndex (n : ℕ) : (k : ℕ) → (Fin k → Fin n) ≃ Fin (n^k)
  | 0 => {
      toFun := fun _ => 0
      invFun := fun _ i => Fin.elim0 i
      left_inv := fun _ => funext fun i => Fin.elim0 i
      right_inv := fun i => by
        apply Fin.ext
        have hi : i.val < 1 := by simpa only [pow_zero] using i.isLt
        change 0 = i.val
        omega }
  | k+1 => ((splitLast k n).trans ((functionIndex n k).prodCongr (Equiv.refl _))).trans finProdFinEquiv

end RSGPaper.QuarticFiniteIndex

end

section

set_option autoImplicit false
set_option maxHeartbeats 1600000

noncomputable section

namespace RSGPaper.QuarticSliceCoefficients

open BooleanMonomials
open QuarticChannelLayout
open QuarticSliceLayout
open QuarticFieldCoordinates

variable {side : ℕ}

def coreBasis (side : ℕ) := basis (side+1) (Nat.succ_ne_zero side)

def traceValue (term : SliceTerm side) : Bit :=
  let b := coreBasis side
  Algebra.trace Bit (BinaryField (side+1))
    (b term.aux * (if term.squared then (b term.left)^2 else b term.left) * b term.right)

def fixedQuartic (support : Finset (DataIndex side)) : Bit :=
  ∑ term : SliceTerm side, if sliceSupport term = support then traceValue term else 0

def realQuartic (weight : Slot side → Bit) (support : Finset (DataIndex side)) : Bit :=
  ∑ slot : Slot side, if realSupport slot = support then weight slot else 0

def fixedCubic (side : ℕ) (support : Finset (DataIndex side)) : Bit :=
  if support = polarityTags side then 1 else 0

def channelCoefficients (weight : Slot side → Bit) (support : Finset (DataIndex side)) : Bit :=
  fixedCubic side support + fixedQuartic support + realQuartic weight support

theorem fixed_vanishes {support : Finset (DataIndex side)}
    (absent : ∀ term : SliceTerm side, sliceSupport term ≠ support) : fixedQuartic support = 0 := by
  simp [fixedQuartic,absent]

theorem real_vanishes (weight : Slot side → Bit) {support : Finset (DataIndex side)}
    (absent : ∀ slot : Slot side, realSupport slot ≠ support) : realQuartic weight support = 0 := by
  simp [realQuartic,absent]

theorem polarity_card (side : ℕ) : (polarityTags side).card = 3 := by
  unfold polarityTags
  have inj : Function.Injective (tagCoord side) := by
    intro i j equal
    exact congrArg dataGroup equal
  rw [Finset.card_image_of_injective _ inj]
  decide

theorem polarity_injective (side : ℕ) :
    Set.InjOn dataGroup (polarityTags side : Set (DataIndex side)) := by
  intro i hi j hj eq
  obtain ⟨g,_,rfl⟩ := Finset.mem_image.mp hi
  obtain ⟨h,_,rfl⟩ := Finset.mem_image.mp hj
  exact congrArg (tagCoord side) eq

theorem real_group_injective (slot : Slot side) :
    Set.InjOn dataGroup (realSupport slot : Set (DataIndex side)) := by
  intro i hi j hj eq
  obtain ⟨g,_,rfl⟩ := Finset.mem_image.mp hi
  obtain ⟨h,_,rfl⟩ := Finset.mem_image.mp hj
  exact congrArg (fun g => realCoord g (slot g)) eq

theorem allowed_support (weight : Slot side → Bit) (support : Finset (DataIndex side))
    (nonzero : channelCoefficients weight support ≠ 0) :
    (support.card = 3 ∨ support.card = 4) ∧ Set.InjOn dataGroup (support : Set (DataIndex side)) := by
  classical
  by_cases cubic : support = polarityTags side
  · subst support
    exact ⟨Or.inl (polarity_card side),polarity_injective side⟩
  by_cases fixed : ∃ term : SliceTerm side, sliceSupport term = support
  · obtain ⟨term,rfl⟩ := fixed
    exact ⟨Or.inr (slice_card term),groups_injective term⟩
  by_cases real : ∃ slot : Slot side, realSupport slot = support
  · obtain ⟨slot,rfl⟩ := real
    exact ⟨Or.inr (realSupport_card slot),real_group_injective slot⟩
  exact False.elim (nonzero (by
    rw [channelCoefficients,fixedCubic,if_neg cubic,
      fixed_vanishes (fun term same => fixed ⟨term,same⟩),
      real_vanishes weight (fun slot same => real ⟨slot,same⟩)]
    simp))

def index (side : ℕ) : DataIndex side ≃ Fin (dataWidth side) := QuarticFiniteIndex.dataIndex side

def potential (weight : Slot side → Bit) : Coefficients (dataWidth side) :=
  fun support => channelCoefficients weight (support.map (index side).symm.toEmbedding)

attribute [irreducible] index

end RSGPaper.QuarticSliceCoefficients

end

section

set_option autoImplicit false
set_option maxHeartbeats 1600000

namespace RSGPaper.QuarticClauseBit

open RSGRecognition
open BooleanMonomials
open CubicGauge

variable {k : ℕ}

def excludeGroup (g : Fin k → Fin 4) (side : Fin 4) (x : Cube k) : Cube k :=
  fun i => if g i = side then 0 else x i

end RSGPaper.QuarticClauseBit

end

section

set_option autoImplicit false
set_option maxHeartbeats 1050000

namespace RSGPaper.QuarticMask

open BooleanMonomials
open CubicGauge
open QuarticFirstPolar

variable {k : ℕ}

def project (keep : Fin k → Bool) : Cube k →ₗ[Bit] Cube k :=
  LinearMap.pi fun i => if keep i then LinearMap.proj i else 0

end RSGPaper.QuarticMask

end

section

set_option autoImplicit false
set_option maxHeartbeats 1750000

noncomputable section

namespace RSGPaper.QuarticLiteralDirection

open BooleanMonomials
open QuarticChannelLayout
open QuarticFieldCoordinates
open QuarticNativeSecond
open QuarticSliceCoefficients

abbrev U (side : ℕ) := Cube (firstWidth side)
abbrev Position (side : ℕ) := QuarticDirectionSpan.Position (firstWidth side)
abbrev Data (side : ℕ) := Cube (dataWidth side)

def group (side : ℕ) (i : Fin (dataWidth side)) : Group := dataGroup ((index side).symm i)

def literal {side : ℕ} (slot : Slot side) (negative : Bool) : Data side :=
  indicator ((literalSupport slot negative).map (index side).toEmbedding)

def keepFirst (side : ℕ) (i : Fin (firstWidth side)) : Bool :=
  if inside : i.val < dataWidth side then decide (group side ⟨i.val,inside⟩ = 0) else false

def mask (side : ℕ) : U side →ₗ[Bit] U side := QuarticMask.project (keepFirst side)

def dataField {side : ℕ} (x : Data side) : E (dataWidth side) :=
  coordinates (fieldWidth (dataWidth side)) (by unfold fieldWidth; omega)
    ((splitCube (dataWidth side) (dataWidth side+3)).symm (x,0))

def firstInput {side : ℕ} (x : Data side) : U side :=
  let j := dataField x
  let b := dataField (QuarticClauseBit.excludeGroup (group side) 0 x)
  let φ := removeLinearCube (fieldWidth (dataWidth side)) (by unfold fieldWidth; omega)
  (positionFields (dataWidth side)).symm
    (j,0,QuarticSecondPolar.squarefreePart φ j + QuarticSecondPolar.fieldPolar j b)

def a {side : ℕ} (x : Data side) : Position side := (firstInput x,0,1)

def firstProfile (side : ℕ) := QuarticFirstPolar.profile (mask side)

theorem firstFields {side : ℕ} (x : Data side) :
    positionFields (dataWidth side) (firstInput x) =
      (dataField x,0,
        QuarticSecondPolar.squarefreePart
          (removeLinearCube (fieldWidth (dataWidth side)) (by unfold fieldWidth; omega)) (dataField x) +
        QuarticSecondPolar.fieldPolar (dataField x) (dataField (QuarticClauseBit.excludeGroup (group side) 0 x))) :=
  LinearEquiv.apply_symm_apply _ _

end RSGPaper.QuarticLiteralDirection

end

section

set_option autoImplicit false
set_option maxHeartbeats 1500000

namespace RSGPaper.QuarticTripleGuard

open BooleanMonomials
open CubicGauge

variable {k : ℕ}

theorem direction_commute (a b : Cube k) (f : Cube k → Bit) :
    direction a (direction b f) = direction b (direction a f) := by
  funext x
  simp only [direction,add_assoc,add_comm a b]
  abel

theorem direction_join (a b : Cube k) (f : Cube k → Bit) (x : Cube k) :
    direction (a+b) f x = direction a f x+direction b f x+direction a (direction b f) x := by
  simp only [direction,add_assoc]
  symm
  calc
    _ = f (x+(a+b))+f x+(f x+f x)+(f (x+a)+f (x+a))+(f (x+b)+f (x+b)) := by abel
    _ = _ := by simp only [QuarticCayley.double,add_zero]

def tripleCoefficients (c : Coefficients k) (i j l : Fin k) : Coefficients k :=
  derivative i (derivative j (derivative l c))

end RSGPaper.QuarticTripleGuard

end

section

set_option autoImplicit false
set_option maxHeartbeats 1550000

namespace RSGPaper.QuarticTripleRows

open BooleanMonomials
open CubicGauge
open QuarticDirectionSpan
open QuarticFirstPolar
open QuarticTripleGuard
open QuarticNativeCorrection

variable {m k : ℕ}

def linearRow (c : Coefficients k) (i j l : Fin k) : Cube k →ₗ[Bit] Bit :=
  ∑ q, tripleCoefficients c i j l {q} • LinearMap.proj q

def dataSlope (P : Position m →ₗ[Bit] Cube k) (c : Coefficients k) (a x : Position m) : Bit :=
  dotLinear (P a) (QuarticJointGuards.delta a (fun p => gradient c (P p)) x)

def Constraint (M : Cube m →ₗ[Bit] Cube m) (P : Position m →ₗ[Bit] Cube k) (a : Position m)
    (ρ : Position m →ₗ[Bit] Bit) (constant : Bit) (c : Coefficients k) : Prop :=
  ∀ x, dataSlope P c a x + ρ (QuarticJointGuards.delta a (profile M) x) = constant + ρ (profile M a)

end RSGPaper.QuarticTripleRows

end

section

set_option autoImplicit false
set_option maxHeartbeats 1650000

noncomputable section

namespace RSGPaper.QuarticProjectedData

open BooleanMonomials
open CubicGauge
open QuarticChannelLayout
open QuarticLiteralDirection
open QuarticPrimaryLayout
open QuarticNativeSecond

def read (side : ℕ) : Position side →ₗ[Bit] Data side :=
  LinearMap.pi fun i => (LinearMap.proj ((layout side).dataInput i)).comp (LinearMap.fst _ _ _)

def lift (side : ℕ) : Data side →ₗ[Bit] Position side :=
  (LinearMap.inl _ _ _).comp ((blocks (dataWidth side)).symm.toLinearMap.comp
    ((LinearMap.inl _ _ _).comp ((splitCube (dataWidth side) (dataWidth side+3)).symm.toLinearMap.comp
      (LinearMap.inl _ _ _))))

end RSGPaper.QuarticProjectedData

end

section

set_option autoImplicit false
set_option maxHeartbeats 1500000

namespace RSGPaper.QuarticThirdDerivatives

open BooleanMonomials
open CubicGauge
open QuarticDifferenceDegree
open QuarticTripleGuard

variable {k : ℕ}

def mixed (f : Cube k → Bit) (a b c : Cube k) : Cube k → Bit := direction a (direction b (direction c f))

def value (f : Cube k → Bit) (a b c : Cube k) : Bit := mixed f a b c 0

theorem constant (f : Cube k → Bit) (degree : Kills 4 f) (a b c x : Cube k) : mixed f a b c x = value f a b c := by
  have disappear : direction x (mixed f a b c) = 0 := degree c b a x
  have at_zero := congrFun disappear (0 : Cube k)
  change mixed f a b c (0+x)+mixed f a b c 0 = 0 at at_zero
  rw [zero_add] at at_zero
  exact add_right_cancel (at_zero.trans (QuarticCayley.double _).symm)

theorem first_add (f : Cube k → Bit) (degree : Kills 4 f) (a a' b c : Cube k) :
    value f (a+a') b c = value f a b c + value f a' b c := by
  have vanish : direction a (direction a' (direction b (direction c f))) = 0 := degree c b a' a
  unfold value mixed
  rw [direction_join,vanish] <;> simp

theorem first_comm (f : Cube k → Bit) (a b c : Cube k) : value f a b c = value f b a c := by
  unfold value mixed
  rw [direction_commute a b (direction c f)]

theorem last_comm (f : Cube k → Bit) (a b c : Cube k) : value f a b c = value f a c b := by
  unfold value mixed
  rw [direction_commute b c f]

theorem second_add (f : Cube k → Bit) (degree : Kills 4 f) (a b b' c : Cube k) :
    value f a (b+b') c = value f a b c+value f a b' c := by
  rw [first_comm f a (b+b') c,first_add f degree,first_comm f b a c,first_comm f b' a c]

theorem third_add (f : Cube k → Bit) (degree : Kills 4 f) (a b c c' : Cube k) :
    value f a b (c+c') = value f a b c+value f a b c' := by
  rw [last_comm f a b (c+c'),second_add f degree,last_comm f a c b,last_comm f a c' b]

theorem first_zero (f : Cube k → Bit) (b c : Cube k) : value f 0 b c = 0 := by
  unfold value mixed direction
  rw [add_zero,QuarticCayley.double]

theorem second_zero (f : Cube k → Bit) (a c : Cube k) : value f a 0 c = 0 := by
  rw [first_comm f a 0 c,first_zero]

theorem third_zero (f : Cube k → Bit) (a b : Cube k) : value f a b 0 = 0 := by
  rw [last_comm f a b 0,second_zero]

/-- The mixed third differences of a degree-three coordinate form a trilinear functional. -/
def functional (f : Cube k → Bit) (degree : Kills 4 f) :
    Cube k →ₗ[Bit] Cube k →ₗ[Bit] Cube k →ₗ[Bit] Bit where
  toFun a := {
    toFun b := {
      toFun c := value f a b c
      map_add' c c' := third_add f degree a b c c'
      map_smul' t c := by
        have bit : ∀ t : Bit, t = 0 ∨ t = 1 := by decide
        change value f a b (t•c) = t • value f a b c
        rcases bit t with rfl | rfl <;> simp [third_zero]
    }
    map_add' b b' := by
      apply DFunLike.ext
      intro c
      exact second_add f degree a b b' c
    map_smul' t b := by
      apply DFunLike.ext
      intro c
      have bit : ∀ t : Bit, t = 0 ∨ t = 1 := by decide
      change value f a (t•b) c = t • value f a b c
      rcases bit t with rfl | rfl <;> simp [second_zero]
  }
  map_add' a a' := by
    apply DFunLike.ext
    intro b
    apply DFunLike.ext
    intro c
    exact first_add f degree a a' b c
  map_smul' t a := by
    apply DFunLike.ext
    intro b
    apply DFunLike.ext
    intro c
    have bit : ∀ t : Bit, t = 0 ∨ t = 1 := by decide
    change value f (t•a) b c = t • value f a b c
    rcases bit t with rfl | rfl <;> simp [first_zero]

end RSGPaper.QuarticThirdDerivatives

end

section

set_option autoImplicit false
set_option maxHeartbeats 1650000

namespace RSGPaper.QuarticDataTensor

open BooleanMonomials
open CubicGauge
open QuarticDifferenceDegree
open QuarticThirdDerivatives

variable {k : ℕ}

def tensor (p : Coefficients k) (a b c : Cube k) : Cube k :=
  fun i => value (coordinate (gradient p) i) a b c

def form (p : Coefficients k) (u a b c : Cube k) : Bit := dot u (tensor p a b c)

def HomogeneousBound (p : Coefficients k) : Prop := ∀ s, 4 < s.card → p s = 0

theorem coordinate_degree (p : Coefficients k) (bound : HomogeneousBound p) (i : Fin k) :
    Kills 4 (coordinate (gradient p) i) := kills_gradient p 3 bound i

theorem dot_basis (x : Cube k) (i : Fin k) : dot (e i) x = x i := by
  change (∑ j, indicator {i} j * x j) = _
  simp [indicator,eq_comm]

theorem basis_entry (p : Coefficients k) (i j l q : Fin k) :
    form p (e i) (e j) (e l) (e q) =
      if i ≠ j ∧ i ≠ l ∧ i ≠ q ∧ j ≠ l ∧ j ≠ q ∧ l ≠ q then p {i,q,l,j} else 0 := by
  rw [form,dot_basis]
  have reads (t : Fin k) (c : Coefficients k) : direction (e t) (eval c) = eval (derivative t c) :=
    funext (fun x => (derivative_eval t c x).symm)
  have eval_origin (c : Coefficients k) : eval c 0 = c ∅ := by
    have point : indicator (∅ : Finset (Fin k)) = 0 := by funext i; simp [indicator]
    simpa only [point,Finset.powerset_empty,Finset.sum_singleton] using eval_indicator c ∅
  change direction (e j) (direction (e l) (direction (e q) (eval (derivative i p)))) 0 = _
  rw [reads q,reads l,reads j,eval_origin]
  by_cases ij : i = j <;> by_cases il : i = l <;> by_cases iq : i = q <;>
    by_cases jl : j = l <;> by_cases jq : j = q <;> by_cases lq : l = q <;>
    simp_all [derivative,eq_comm]

theorem tensor_comm_first (p : Coefficients k) (a b c : Cube k) :
    tensor p a b c = tensor p b a c := by
  funext i
  exact first_comm _ _ _ _

theorem tensor_comm_last (p : Coefficients k) (a b c : Cube k) :
    tensor p a b c = tensor p a c b := by
  funext i
  exact last_comm _ _ _ _

theorem tensor_representation (p : Coefficients k) (bound : HomogeneousBound p) (a b c x : Cube k) :
    (fun i => mixed (coordinate (gradient p) i) a b c x) = tensor p a b c := by
  funext i
  exact constant _ (coordinate_degree p bound i) _ _ _ _

def linearFirst (p : Coefficients k) (bound : HomogeneousBound p) (b c : Cube k) :
    Cube k →ₗ[Bit] Cube k :=
  LinearMap.pi fun i =>
    LinearMap.flip (LinearMap.flip (functional (coordinate (gradient p) i) (coordinate_degree p bound i)) b) c

def contraction (u : Cube k) : Cube k →ₗ[Bit] Bit where
  toFun := dot u
  map_add' := QuarticFirstPolar.dot_right_add u
  map_smul' t v := QuarticFirstPolar.dot_right_smul u v t

def firstForm (p : Coefficients k) (a b c : Cube k) : Cube k →ₗ[Bit] Bit where
  toFun u := form p u a b c
  map_add' u v := QuarticFirstPolar.dot_left_add u v _
  map_smul' t u := by
    change dot (t • u) (tensor p a b c) = t * dot u (tensor p a b c)
    rw [QuarticFirstPolar.dot_swap (t • u),QuarticFirstPolar.dot_right_smul,
      QuarticFirstPolar.dot_swap (tensor p a b c)]

def secondForm (p : Coefficients k) (bound : HomogeneousBound p) (u b c : Cube k) : Cube k →ₗ[Bit] Bit :=
  (contraction u).comp (linearFirst p bound b c)

theorem form_swap_inputs (p : Coefficients k) (u a b c : Cube k) :
    form p u a b c = form p u b a c := by
  rw [form,form,tensor_comm_first]

theorem form_cycle_inputs (p : Coefficients k) (u a b c : Cube k) :
    form p u a b c = form p u c a b := by
  rw [form,form,tensor_comm_last p a b c,tensor_comm_first p a c b]

theorem linear_ext (f g : Cube k →ₗ[Bit] Bit) (same : ∀ i, f (e i) = g (e i)) : f = g := by
  apply (Pi.basisFun Bit (Fin k)).ext
  intro i
  have point : Pi.basisFun Bit (Fin k) i = e i := by
    funext j
    simp [Pi.basisFun_apply,e,indicator,Pi.single_apply,eq_comm]
  rw [point,same]

theorem form_swap (p : Coefficients k) (bound : HomogeneousBound p) (u a b c : Cube k) :
    form p u a b c = form p a u b c := by
  have points (i j : Fin k) : form p (e i) (e j) b c = form p (e j) (e i) b c := by
    let left := secondForm p bound (e i) (e j) c
    let right := secondForm p bound (e j) (e i) c
    have same : left = right := by
      apply linear_ext
      intro l
      change form p (e i) (e l) (e j) c = form p (e j) (e l) (e i) c
      rw [form_cycle_inputs p (e i),form_cycle_inputs p (e j)]
      change secondForm p bound (e i) (e l) (e j) c = secondForm p bound (e j) (e l) (e i) c
      have sameLast : secondForm p bound (e i) (e l) (e j) = secondForm p bound (e j) (e l) (e i) := by
        apply linear_ext
        intro q
        change form p (e i) (e q) (e l) (e j) = form p (e j) (e q) (e l) (e i)
        rw [basis_entry,basis_entry]
        have sets : ({i,j,l,q} : Finset (Fin k)) = {j,i,l,q} := by ext; simp [or_left_comm]
        rw [sets]
        congr 1
        apply propext
        constructor <;> intro h <;> aesop
      rw [sameLast]
    have equal := DFunLike.congr_fun same b
    exact (form_swap_inputs p _ _ _ _).trans (equal.trans (form_swap_inputs p _ _ _ _).symm)
  have extend_first (j : Fin k) : firstForm p (e j) b c = secondForm p bound (e j) b c := by
    apply linear_ext
    exact fun i => points i j
  have extend_second : secondForm p bound u b c = firstForm p u b c := by
    apply linear_ext
    intro j
    exact DFunLike.congr_fun (extend_first j) u
  exact DFunLike.congr_fun extend_second a

theorem form_repeated (p : Coefficients k) (bound : HomogeneousBound p) (a b c : Cube k) :
    form p a a b c = 0 := by
  let q : Cube k →ₗ[Bit] Bit := {
    toFun := fun x => form p x x b c
    map_add' x y := by
      change firstForm p (x+y) b c (x+y) = _
      rw [map_add]
      change secondForm p bound x b c (x+y) + secondForm p bound y b c (x+y) = _
      rw [map_add,map_add]
      change form p x x b c + form p x y b c + (form p y x b c + form p y y b c) = _
      rw [form_swap p bound y x]
      calc
        _ = form p x x b c + form p y y b c + (form p x y b c + form p x y b c) := by abel
        _ = _ := by rw [QuarticCayley.double,add_zero]
    map_smul' t x := by
      have bit : ∀ z : Bit, z = 0 ∨ z = 1 := by decide
      change form p (t•x) (t•x) b c = t • form p x x b c
      rcases bit t with rfl | rfl
      · simp [form,CubicGauge.dot]
      · simp }
  have basis_zero (i : Fin k) : q (e i) = 0 := by
    let e₀ := e i
    have at_second (j : Fin k) : secondForm p bound e₀ e₀ (e j) = 0 := by
      apply linear_ext
      intro l
      change form p e₀ (e l) e₀ (e j) = 0
      rw [form_swap_inputs]
      simp [e₀,basis_entry]
    have outer : secondForm p bound e₀ c e₀ = 0 := by
      apply linear_ext
      intro j
      change form p e₀ (e j) c e₀ = 0
      rw [form_cycle_inputs]
      have h := DFunLike.congr_fun (at_second j) c
      exact (form_cycle_inputs p _ _ _ _).trans h
    change form p e₀ e₀ b c = 0
    rw [form_cycle_inputs,form_cycle_inputs]
    exact DFunLike.congr_fun outer b
  have zero : q = 0 := linear_ext q 0 basis_zero
  exact DFunLike.congr_fun zero a

end RSGPaper.QuarticDataTensor

end

section

set_option autoImplicit false
set_option maxHeartbeats 1850000

noncomputable section

namespace RSGPaper.QuarticSliceRead

open BooleanMonomials
open CubicGauge
open QuarticChannelLayout
open QuarticComplementPairs
open QuarticSliceLayout
open QuarticFieldCoordinates
open QuarticSliceCoefficients
open QuarticDataTensor

variable {side : ℕ}

abbrev FieldAt (side : ℕ) := BinaryField (side+1)
abbrev Data (side : ℕ) := Cube (dataWidth side)
abbrev Potential (side : ℕ) := Coefficients (dataWidth side)

def named (i : DataIndex side) : Data side := e (index side i)

def QuarticEntries (p : Potential side) (weight : Slot side → Bit) : Prop :=
  ∀ T : Finset (DataIndex side), T.card = 4 →
    p (T.map (index side).toEmbedding) = channelCoefficients weight T

theorem canonical_degree (weight : Slot side → Bit) : HomogeneousBound (potential weight) := by
  intro T large
  by_contra h
  have small := (allowed_support weight (T.map (index side).symm.toEmbedding) h).1
  rw [Finset.card_map] at small
  omega

def core (g : Group) : FieldAt side →ₗ[Bit] Data side :=
  (coreBasis side).constr Bit (fun i => named (.inl (g,i)))

end RSGPaper.QuarticSliceRead

end

section

set_option autoImplicit false
set_option maxHeartbeats 1700000

namespace RSGPaper.QuarticCanonicalData

open BooleanMonomials
open CubicGauge
open QuarticChannelLayout
open QuarticLiteralDirection
open QuarticProjectedData

variable {side : ℕ}

def profile (p : Coefficients (dataWidth side)) (x : Position side) : Data side :=
  gradient p (read side x)

theorem profile_delta (p : Coefficients (dataWidth side)) (a x : Position side) :
    QuarticJointGuards.delta a (profile p) x =
      QuarticJointGuards.delta (read side a) (gradient p) (read side x) := by
  unfold QuarticJointGuards.delta profile
  rw [map_add]

end RSGPaper.QuarticCanonicalData

end

section

set_option autoImplicit false
set_option maxHeartbeats 1700000

noncomputable section

namespace RSGPaper.QuarticFreeEntries

open BooleanMonomials
open QuarticChannelLayout
open QuarticSliceLayout
open QuarticSliceCoefficients
open QuarticSliceRead

variable {side : ℕ}

def IsReal : DataIndex side → Prop
  | .inl (_,i) => i.val < side
  | .inr _ => False

def weightOf (p : Potential side) (slot : Slot side) : Bit :=
  p ((realSupport slot).map (index side).toEmbedding)

end RSGPaper.QuarticFreeEntries

end

section

set_option autoImplicit false
set_option maxHeartbeats 1900000

noncomputable section

namespace RSGPaper.QuarticFixedRows

open BooleanMonomials
open CubicGauge
open QuarticChannelLayout
open QuarticLiteralDirection
open QuarticProjectedData
open QuarticSliceCoefficients
open QuarticSliceRead
open QuarticFreeEntries
open QuarticTripleRows

variable {side : ℕ}

def triple (side : ℕ) (i j l : Fin (dataWidth side)) : Position side :=
  lift side (e i + e j + e l) + (0,0,1)

def row (i j l : Fin (dataWidth side)) : Position side →ₗ[Bit] Bit :=
  QuarticGuardRows.explicitRow ((linearRow (QuarticSliceCoefficients.potential 0) i j l).comp (read side))

def Constrained (p : Potential side) : Prop :=
  ∀ i j l : DataIndex side, ¬ IsReal i →
    dataGroup i ≠ dataGroup j → dataGroup i ≠ dataGroup l → dataGroup j ≠ dataGroup l →
    let I := index side i
    let J := index side j
    let L := index side l
    Constraint (mask side) (read side) (triple side I J L) (row I J L)
      ((QuarticSliceCoefficients.potential 0) {L,J,I}) p

end RSGPaper.QuarticFixedRows

end

section

set_option autoImplicit false
set_option maxHeartbeats 1900000

noncomputable section

namespace RSGPaper.QuarticAssignmentRead

open BooleanMonomials
open CubicGauge
open QuarticChannelLayout
open QuarticSliceLayout
open QuarticSliceCoefficients
open QuarticSliceRead
open QuarticFreeEntries
open QuarticDataTensor

variable {side : ℕ}

def realCubic (p : Potential side) : Potential side := by
  classical
  exact fun S => if S.card = 3 ∧ ∀ i ∈ S, IsReal ((index side).symm i) then p S else 0

def assignment (p : Potential side) (slot : Slot side) : Bit :=
  weightOf p slot + eval (realCubic p) (QuarticLiteralDirection.literal slot false)

end RSGPaper.QuarticAssignmentRead

end

section

set_option autoImplicit false
set_option maxHeartbeats 1750000

noncomputable section

namespace RSGPaper.QuarticCoreCoordinates

open BooleanMonomials
open CubicGauge
open QuarticChannelLayout
open QuarticFieldCoordinates
open QuarticSliceCoefficients
open QuarticDataTensor
open QuarticSliceRead

variable {side : ℕ}

def coreVectors (v : Group → FieldAt side) : Data side := ∑ g, core g (v g)

end RSGPaper.QuarticCoreCoordinates

end

section

set_option autoImplicit false
set_option maxHeartbeats 700000

noncomputable section

namespace RSGPaper.QuarticCoreHyperplane

open BooleanMonomials
open QuarticChannelLayout
open QuarticComplementPairs
open QuarticSliceCoefficients
open QuarticDataTensor
open QuarticSliceRead
open QuarticCoreCoordinates

variable {side : ℕ}

def contracted (p : Potential side) (u : Data side) : Submodule Bit (Data side) :=
  Submodule.span Bit {z | ∃ b c, z = tensor p u b c}

end RSGPaper.QuarticCoreHyperplane

end

section

set_option autoImplicit false
set_option maxHeartbeats 1900000

noncomputable section

namespace RSGPaper.QuarticCoreProjection

open BooleanMonomials
open CubicGauge
open QuarticChannelLayout
open QuarticFieldCoordinates
open QuarticSliceRead
open QuarticCoreCoordinates
open QuarticSliceCoefficients

variable {side : ℕ}

def hasCore : DataIndex side → Bool
  | .inl _ => true
  | .inr _ => false

def corePart (side : ℕ) : Data side →ₗ[Bit] Data side :=
  QuarticMask.project (fun i => hasCore ((index side).symm i))

def auxPart (side : ℕ) : Data side →ₗ[Bit] Data side :=
  LinearMap.id + corePart side

end RSGPaper.QuarticCoreProjection

end
Source
Sirawit Pongnakintr, Riemannian Symmetric Graphs, Hexagon v2 preprint, https://hexagonmath.org/2611.01261v2, Theorem 5.2 and Appendix A.5

View graph

Get started

Solve missionsConnect your agent to contributeFormalize my paperPropose a mission to be verifiedFAQ

About Prove2Me

Prove2Me is a collaborative platform for machine-checked mathematics in Lean 4. Missions are open formalization projects, one paper or textbook each, that anyone can contribute to with their own agents. Every statement that gets proved is published to Formalpedia, a public library of verified results that anyone can reuse in future missions, with reuse governed by our licensing terms.

How Prove2Me worksResearch paperProve2Me Team
SKILL.mdTourFAQContactTerms
© 2026 Prove2Me