Research | Knitting | Algorithm: Tangle equality

Algorithm: Tangle equality

The algorithm compares two drawings of tangles. It answers equal, not equal or unknown. An answer of equal comes with a chain of moves that Lean checks. An answer of not equal comes with an invariant that has different values on the two drawings: the connectivity or the linking counts. An answer of unknown makes no claim.

Connectivity of a drawing

The first invariant. It tells which boundary point the yarn joins to which other boundary point. It forgets the crossings. It forgets closed pieces of yarn.

Points

A drawing from b bottom strands to t top strands has b + t boundary points.

Point Number
Bottom point j j, for j < b
Top point j b + j, for j < t

The state

  • The fold reads the drawing from bottom to top.
  • At a height, the points are the b bottom points and the strands of that height. Strand i is the number b + i.
  • The state S : Nat → Nat sends each point to the point that the drawing below joins it to.
  • At the start, bottom point j is joined to strand j (Conn.start).
Event Action on the state
Crossing at n Exchanges the points n and n + 1 (Conn.crossAct). The sign of the crossing does not matter.
Cup at n Adds two points at n, n + 1. They are joined to each other (Conn.cupAct).
Cap at n Joins the two points that n and n + 1 are joined to, then removes n and n + 1 (Conn.capAct).
  • conn b u: the state after all of u. Point x is joined to point conn b u x. A number above the last point goes to itself.
  • connList b t u: the values of conn b u on the b + t boundary points, as a list.
  • Conn.Inv N S: S pairs the points below N and fixes all other numbers. Each event that fits keeps it (Conn.inv_run).
namespace FormalizeKnitting.Equality
namespace Conn
def swapAt (n x : Nat) : Nat := if x = n then n + 1 else if x = n + 1 then n else x

def upAt (n y : Nat) : Nat := if y < n then y else y + 2

def downAt (n y : Nat) : Nat := if y < n then y else y - 2

def crossAct (n : Nat) (S : Nat → Nat) : Nat → Nat := fun x => swapAt n (S (swapAt n x))

def cupAct (n : Nat) (S : Nat → Nat) : Nat → Nat := fun x =>
  if x = n then n + 1 else if x = n + 1 then n else upAt n (S (downAt n x))

def through (n : Nat) (S : Nat → Nat) (z : Nat) : Nat :=
  if z = n then S (n + 1) else if z = n + 1 then S n else z

def capAct (n : Nat) (S : Nat → Nat) : Nat → Nat := fun x => downAt n (through n S (S (upAt n x)))

def genAct (b : Nat) : Gen → (Nat → Nat) → Nat → Nat
  | .cross i _ => crossAct (b + i)
  | .cup i => cupAct (b + i)
  | .cap i => capAct (b + i)

def run (b : Nat) : (Nat → Nat) → MWord → Nat → Nat
  | S, [] => S
  | S, g :: gs => run b (genAct b g S) gs

def start (b : Nat) (x : Nat) : Nat := if x < b then x + b else if x < 2 * b then x - b else x

def Inv (N : Nat) (S : Nat → Nat) : Prop :=
  (∀ x, S (S x) = x) ∧ (∀ x, x < N → S x ≠ x) ∧ (∀ x, N ≤ x → S x = x)
end Conn

open Conn
def conn (b : Nat) (u : MWord) : Nat → Nat := run b (start b) u

def connList (b t : Nat) (u : MWord) : List Nat := (List.range (b + t)).map (conn b u)
namespace Conn
theorem run_append (b : Nat) (S : Nat → Nat) (u v : MWord) : run b S (u ++ v) = run b (run b S u) v
Proof
:= by
  induction u generalizing S with
  | nil => rfl
  | cons g gs ih => exact ih _
theorem swapAt_l {n x : Nat} (h : x = n) : swapAt n x = n + 1
Proof
:= by simp [swapAt, h]
theorem swapAt_r {n x : Nat} (h : x = n + 1) : swapAt n x = n
Proof
:= by simp [swapAt, h]
theorem swapAt_o {n x : Nat} (h₁ : x ≠ n) (h₂ : x ≠ n + 1) : swapAt n x = x
Proof
:= by simp [swapAt, h₁, h₂]
theorem upAt_lt {n y : Nat} (h : y < n) : upAt n y = y
Proof
:= by simp [upAt, h]
theorem upAt_ge {n y : Nat} (h : n ≤ y) : upAt n y = y + 2
Proof
:= by simp [upAt]; omega
theorem crossAct_apply (n : Nat) (S : Nat → Nat) (x : Nat) : crossAct n S x = swapAt n (S (swapAt n x))
Proof
:= rfl
theorem cupAct_l {n x : Nat} (S : Nat → Nat) (h : x = n) : cupAct n S x = n + 1
Proof
:= by simp [cupAct, h]
theorem cupAct_r {n x : Nat} (S : Nat → Nat) (h : x = n + 1) : cupAct n S x = n
Proof
:= by simp [cupAct, h]
theorem cupAct_o {n x : Nat} (S : Nat → Nat) (h₁ : x ≠ n) (h₂ : x ≠ n + 1) :
    cupAct n S x = upAt n (S (downAt n x))
Proof
:= by simp [cupAct, h₁, h₂]

Each event keeps Inv

theorem inv_cross {N n : Nat} {S : Nat → Nat} (hS : Inv N S) (hn : n + 1 < N) : Inv N (crossAct n S)
Proof
:= by
  obtain ⟨h1, h2, h3⟩ := hS
  refine ⟨fun x => ?_, fun x hx => ?_, fun x hx => ?_⟩ <;> simp only [crossAct, swapAt] <;> grind
theorem inv_cup {N n : Nat} {S : Nat → Nat} (hS : Inv N S) (hn : n ≤ N) : Inv (N + 2) (cupAct n S)
Proof
:= by
  obtain ⟨h1, h2, h3⟩ := hS
  refine ⟨fun x => ?_, fun x hx => ?_, fun x hx => ?_⟩ <;> simp only [cupAct, upAt, downAt] <;> grind
theorem inv_cap {N n : Nat} {S : Nat → Nat} (hS : Inv N S) (hn : n + 1 < N) : Inv (N - 2) (capAct n S)
Proof
:= by
  obtain ⟨h1, h2, h3⟩ := hS
  refine ⟨fun x => ?_, fun x hx => ?_, fun x hx => ?_⟩ <;> simp only [capAct, through, upAt, downAt] <;>
    grind (splits := 40)
theorem inv_start (b : Nat) : Inv (b + b) (start b)
Proof
:= by
  refine ⟨fun x => ?_, fun x hx => ?_, fun x hx => ?_⟩ <;> simp only [start] <;> grind
theorem inv_genAct {b w : Nat} {S : Nat → Nat} (g : Gen) (hS : Inv (b + w) S) (hg : g.fits w = true) :
    Inv (b + g.widthAfter w) (genAct b g S)
Proof
:= by
  cases g with
  | cross i lo =>
    simp only [Gen.fits, decide_eq_true_eq] at hg
    simp only [Gen.widthAfter, genAct]
    exact inv_cross (N := b + w) (n := b + i) hS (by omega)
  | cup i =>
    simp only [Gen.fits, decide_eq_true_eq] at hg
    simp only [Gen.widthAfter, genAct]
    exact inv_cup (N := b + w) (n := b + i) hS (by omega)
  | cap i =>
    simp only [Gen.fits, decide_eq_true_eq] at hg
    have := inv_cap (n := b + i) hS (by omega)
    simp only [Gen.widthAfter, genAct]
    rwa [show b + (w - 2) = b + w - 2 by omega]
theorem inv_run (b : Nat) : ∀ (u : MWord) (w w' : Nat) (S : Nat → Nat), Inv (b + w) S →
    topWidth w u = some w' → Inv (b + w') (run b S u)
Proof
  | [], w, w', S, hS, h => by
    simp only [topWidth, Option.some.injEq] at h
    subst h
    exact hS
  | g :: gs, w, w', S, hS, h => by
    simp only [topWidth] at h
    split at h
    · exact inv_run b gs _ w' _ (inv_genAct g hS ‹_›) h
    · cases h
theorem fits_cons {w : Nat} {g : Gen} {gs : MWord} (h : (topWidth w (g :: gs)).isSome = true) :
    g.fits w = true ∧ (topWidth (g.widthAfter w) gs).isSome = true
Proof
:= by
  simp only [topWidth] at h
  split at h
  · exact ⟨‹_›, h⟩
  · simp at h

Connectivity is an invariant of Iso

Theorem Statement
Conn.run_move A move whose left side fits sends each state with Inv to the same state as its right side
conn_step1 One move keeps conn
conn_iso Iso b u v → conn b u = conn b v
connList_iso Iso b u v → connList b t u = connList b t v
not_iso_of_connList_ne Different connectivity lists: not Iso
  • Conn.Check m: the local check of the move m, as a statement. Conn.check proves it for each move.
  • There is one theorem for each kind of move (Conn.check_zigR, …) and for each pair of events of a far commutation (Conn.check_farR_cap_cap, …). The macro conn_check unfolds the states. It compares two functions Nat → Nat point by point. grind does the case analysis on the position of the point.
  • Two caps side by side need the most cases. Conn.capAct_capAct proves them on their own.
  • Conn.cupSlideR_eq, …, Conn.r1cap_eq: single moves as equations of states. The linking proofs use them.
  • conn_step1: the state after the part below the move satisfies Inv (Conn.inv_run). The move sends it to the same state. The part above the move then runs from equal states (Conn.run_append).
namespace FormalizeKnitting.Equality
namespace Conn
theorem isSome_topWidth_cons (w : Nat) (g : Gen) (gs : MWord) :
    (topWidth w (g :: gs)).isSome = (g.fits w && (topWidth (g.widthAfter w) gs).isSome)
Proof
:= by
  simp only [topWidth]
  split <;> simp_all
macro "conn_check" : tactic => `(tactic| (
  simp only [Mv.lhs, Mv.rhs, Mv.ok, isSome_topWidth_cons, topWidth, Option.isSome_some, Bool.and_true,
    Bool.and_eq_true, Gen.fits, Gen.widthAfter, Gen.withLo, Gen.lo, Gen.ins, Gen.outs, decide_eq_true_eq] at *
  funext x
  simp only [run, genAct, crossAct, cupAct, capAct, through, swapAt, upAt, downAt]
  grind (splits := 60)))

def Check (m : Mv) : Prop :=
  ∀ (b w : Nat) (S : Nat → Nat), m.ok = true → Inv (b + w) S → (topWidth w m.lhs).isSome = true →
    run b S m.lhs = run b S m.rhs
theorem check_farR_cross_cross (a s c t : _) : Check (.farR (.cross a s) (.cross c t))
Proof
:= by
  intro b w S hok hS hfit; obtain ⟨h1, h2, h3⟩ := hS; conn_check
theorem check_farR_cross_cup (a s c : _) : Check (.farR (.cross a s) (.cup c))
Proof
:= by
  intro b w S hok hS hfit; obtain ⟨h1, h2, h3⟩ := hS; conn_check
theorem check_farR_cross_cap (a s c : _) : Check (.farR (.cross a s) (.cap c))
Proof
:= by
  intro b w S hok hS hfit; obtain ⟨h1, h2, h3⟩ := hS; conn_check
theorem check_farR_cup_cross (a c t : _) : Check (.farR (.cup a) (.cross c t))
Proof
:= by
  intro b w S hok hS hfit; obtain ⟨h1, h2, h3⟩ := hS; conn_check
theorem check_farR_cup_cup (a c : _) : Check (.farR (.cup a) (.cup c))
Proof
:= by
  intro b w S hok hS hfit; obtain ⟨h1, h2, h3⟩ := hS; conn_check
theorem check_farR_cup_cap (a c : _) : Check (.farR (.cup a) (.cap c))
Proof
:= by
  intro b w S hok hS hfit; obtain ⟨h1, h2, h3⟩ := hS; conn_check
theorem check_farR_cap_cross (a c t : _) : Check (.farR (.cap a) (.cross c t))
Proof
:= by
  intro b w S hok hS hfit; obtain ⟨h1, h2, h3⟩ := hS; conn_check
theorem check_farR_cap_cup (a c : _) : Check (.farR (.cap a) (.cup c))
Proof
:= by
  intro b w S hok hS hfit; obtain ⟨h1, h2, h3⟩ := hS; conn_check
theorem check_farR_cap_cap (a c : _) : Check (.farR (.cap a) (.cap c))
Proof
:= by
  intro b w S hok hS hfit; obtain ⟨h1, h2, h3⟩ := hS; conn_check
theorem check_farL_cross_cross (a s c t : _) : Check (.farL (.cross a s) (.cross c t))
Proof
:= by
  intro b w S hok hS hfit; obtain ⟨h1, h2, h3⟩ := hS; conn_check
theorem check_farL_cross_cup (a s c : _) : Check (.farL (.cross a s) (.cup c))
Proof
:= by
  intro b w S hok hS hfit; obtain ⟨h1, h2, h3⟩ := hS; conn_check
theorem check_farL_cross_cap (a s c : _) : Check (.farL (.cross a s) (.cap c))
Proof
:= by
  intro b w S hok hS hfit; obtain ⟨h1, h2, h3⟩ := hS; conn_check
theorem check_farL_cup_cross (a c t : _) : Check (.farL (.cup a) (.cross c t))
Proof
:= by
  intro b w S hok hS hfit; obtain ⟨h1, h2, h3⟩ := hS; conn_check
theorem check_farL_cup_cup (a c : _) : Check (.farL (.cup a) (.cup c))
Proof
:= by
  intro b w S hok hS hfit; obtain ⟨h1, h2, h3⟩ := hS; conn_check
theorem check_farL_cup_cap (a c : _) : Check (.farL (.cup a) (.cap c))
Proof
:= by
  intro b w S hok hS hfit; obtain ⟨h1, h2, h3⟩ := hS; conn_check
theorem check_farL_cap_cross (a c t : _) : Check (.farL (.cap a) (.cross c t))
Proof
:= by
  intro b w S hok hS hfit; obtain ⟨h1, h2, h3⟩ := hS; conn_check
theorem check_farL_cap_cup (a c : _) : Check (.farL (.cap a) (.cup c))
Proof
:= by
  intro b w S hok hS hfit; obtain ⟨h1, h2, h3⟩ := hS; conn_check
theorem capAct_capAct {N n m : Nat} {S : Nat → Nat} (hS : Inv N S) (h : n ≤ m) (hn : n + 1 < N) (hm : m + 3 < N) :
    capAct m (capAct n S) = capAct n (capAct (m + 2) S)
Proof
:= by
  obtain ⟨h1, h2, h3⟩ := hS
  funext x
  simp only [capAct, through, upAt, downAt]
  grind (splits := 60)
theorem check_farL_cap_cap (a c : Nat) : Check (.farL (.cap a) (.cap c))
Proof
:= by
  intro b w S hok hS hfit
  obtain ⟨f1, hfit⟩ := fits_cons hfit
  obtain ⟨f2, -⟩ := fits_cons hfit
  simp only [Mv.ok, Gen.fits, Gen.widthAfter, Gen.lo, Gen.ins] at hok f1 f2
  replace hok := of_decide_eq_true hok
  replace f1 := of_decide_eq_true f1
  replace f2 := of_decide_eq_true f2
  obtain ⟨d, rfl⟩ : ∃ d, a = c + 2 + d := ⟨a - c - 2, by omega⟩
  simp only [Mv.lhs, Mv.rhs, run, genAct, Gen.withLo, Gen.lo, Gen.ins, Gen.outs]
  rw [show b + (c + 2 + d + 0 - 2) = b + c + d by omega, show b + (c + 2 + d) = b + c + d + 2 by omega]
  exact (capAct_capAct hS (by omega) (by omega) (by omega)).symm
theorem check_zigR (i : _) : Check (.zigR i)
Proof
:= by
  intro b w S hok hS hfit; obtain ⟨h1, h2, h3⟩ := hS; conn_check
theorem check_zigL (i : _) : Check (.zigL i)
Proof
:= by
  intro b w S hok hS hfit; obtain ⟨h1, h2, h3⟩ := hS; conn_check
theorem check_cupSlideR (i s : _) : Check (.cupSlideR i s)
Proof
:= by
  intro b w S hok hS hfit; obtain ⟨h1, h2, h3⟩ := hS; conn_check
theorem check_cupSlideL (i s : _) : Check (.cupSlideL i s)
Proof
:= by
  intro b w S hok hS hfit; obtain ⟨h1, h2, h3⟩ := hS; conn_check
theorem check_capSlideR (i s : _) : Check (.capSlideR i s)
Proof
:= by
  intro b w S hok hS hfit; obtain ⟨h1, h2, h3⟩ := hS; conn_check
theorem check_capSlideL (i s : _) : Check (.capSlideL i s)
Proof
:= by
  intro b w S hok hS hfit; obtain ⟨h1, h2, h3⟩ := hS; conn_check
theorem check_r1R (i s : _) : Check (.r1R i s)
Proof
:= by
  intro b w S hok hS hfit; obtain ⟨h1, h2, h3⟩ := hS; conn_check
theorem check_r1L (i s : _) : Check (.r1L i s)
Proof
:= by
  intro b w S hok hS hfit; obtain ⟨h1, h2, h3⟩ := hS; conn_check
theorem check_r1cup (i s : _) : Check (.r1cup i s)
Proof
:= by
  intro b w S hok hS hfit; obtain ⟨h1, h2, h3⟩ := hS; conn_check
theorem check_r1cap (i s : _) : Check (.r1cap i s)
Proof
:= by
  intro b w S hok hS hfit; obtain ⟨h1, h2, h3⟩ := hS; conn_check
theorem check_r2 (i s : _) : Check (.r2 i s)
Proof
:= by
  intro b w S hok hS hfit; obtain ⟨h1, h2, h3⟩ := hS; conn_check
theorem check_r3 (i s : _) : Check (.r3 i s)
Proof
:= by
  intro b w S hok hS hfit; obtain ⟨h1, h2, h3⟩ := hS; conn_check
theorem check_farR (g h : Gen) : Check (.farR g h)
Proof
:= by
  cases g <;> cases h
  exacts [check_farR_cross_cross _ _ _ _, check_farR_cross_cup _ _ _, check_farR_cross_cap _ _ _, check_farR_cup_cross _ _ _, check_farR_cup_cup _ _, check_farR_cup_cap _ _, check_farR_cap_cross _ _ _, check_farR_cap_cup _ _, check_farR_cap_cap _ _]
theorem check_farL (g h : Gen) : Check (.farL g h)
Proof
:= by
  cases g <;> cases h
  exacts [check_farL_cross_cross _ _ _ _, check_farL_cross_cup _ _ _, check_farL_cross_cap _ _ _, check_farL_cup_cross _ _ _, check_farL_cup_cup _ _, check_farL_cup_cap _ _, check_farL_cap_cross _ _ _, check_farL_cap_cup _ _, check_farL_cap_cap _ _]
theorem check (m : Mv) : Check m
Proof
:= by
  cases m with
  | farR g h => exact check_farR g h
  | farL g h => exact check_farL g h
  | zigR i => exact check_zigR i
  | zigL i => exact check_zigL i
  | cupSlideR i s => exact check_cupSlideR i s
  | cupSlideL i s => exact check_cupSlideL i s
  | capSlideR i s => exact check_capSlideR i s
  | capSlideL i s => exact check_capSlideL i s
  | r1R i s => exact check_r1R i s
  | r1L i s => exact check_r1L i s
  | r1cup i s => exact check_r1cup i s
  | r1cap i s => exact check_r1cap i s
  | r2 i s => exact check_r2 i s
  | r3 i s => exact check_r3 i s
theorem run_move (b w : Nat) (S : Nat → Nat) (m : Mv) (hok : m.ok = true) (hS : Inv (b + w) S)
    (hfit : (topWidth w m.lhs).isSome = true) : run b S m.lhs = run b S m.rhs
Proof
:=
  check m b w S hok hS hfit

Single moves as equations of states

theorem cupSlideR_eq {N n : Nat} {S : Nat → Nat} (hS : Inv N S) :
    crossAct (n + 1) (crossAct n (cupAct (n + 1) S)) = cupAct n S
Proof
:= by
  obtain ⟨h1, h2, h3⟩ := hS
  funext x
  simp only [crossAct, cupAct, swapAt, upAt, downAt]
  grind (splits := 60)
theorem cupSlideL_eq {N n : Nat} {S : Nat → Nat} (hS : Inv N S) :
    crossAct n (crossAct (n + 1) (cupAct n S)) = cupAct (n + 1) S
Proof
:= by
  obtain ⟨h1, h2, h3⟩ := hS
  funext x
  simp only [crossAct, cupAct, swapAt, upAt, downAt]
  grind (splits := 60)
theorem capSlideR_eq {N n : Nat} {S : Nat → Nat} (hS : Inv N S) (hn : n + 2 < N) :
    capAct n (crossAct (n + 1) (crossAct n S)) = capAct (n + 1) S
Proof
:= by
  obtain ⟨h1, h2, h3⟩ := hS
  funext x
  simp only [crossAct, capAct, through, swapAt, upAt, downAt]
  grind (splits := 60)
theorem capSlideL_eq {N n : Nat} {S : Nat → Nat} (hS : Inv N S) (hn : n + 2 < N) :
    capAct (n + 1) (crossAct n (crossAct (n + 1) S)) = capAct n S
Proof
:= by
  obtain ⟨h1, h2, h3⟩ := hS
  funext x
  simp only [crossAct, capAct, through, swapAt, upAt, downAt]
  grind (splits := 60)
theorem r1cap_eq {N n : Nat} {S : Nat → Nat} (hS : Inv N S) (hn : n + 1 < N) :
    capAct n (crossAct n S) = capAct n S
Proof
:= by
  obtain ⟨h1, h2, h3⟩ := hS
  funext x
  simp only [crossAct, capAct, through, swapAt, upAt, downAt]
  grind (splits := 60)
open Conn
theorem conn_step1 {b : Nat} {u v : MWord} (h : Step1 b u v) : conn b u = conn b v
Proof
:= by
  obtain ⟨hu, -, p, q, m, hok, rfl, rfl⟩ := h
  unfold conn
  rw [run_append, run_append, run_append, run_append]
  rw [topWidth_append, topWidth_append] at hu
  cases hp : topWidth b p with
  | none => simp [hp] at hu
  | some w =>
    rw [hp] at hu
    cases hl : topWidth w m.lhs with
    | none => simp [hl] at hu
    | some w₂ =>
      rw [run_move b w _ m hok (inv_run b p b w (start b) (inv_start b) hp) (by simp [hl])]
theorem conn_iso {b : Nat} {u v : MWord} (h : Iso b u v) : conn b u = conn b v
Proof
:= by
  induction h with
  | rel _ _ h => exact conn_step1 h
  | refl => rfl
  | symm _ _ _ ih => exact ih.symm
  | trans _ _ _ _ _ ih₁ ih₂ => exact ih₁.trans ih₂
theorem connList_iso {b t : Nat} {u v : MWord} (h : Iso b u v) : connList b t u = connList b t v
Proof
:= by
  simp only [connList, conn_iso h]
theorem not_iso_of_connList_ne {b t : Nat} {u v : MWord} (h : connList b t u ≠ connList b t v) : ¬ Iso b u v
Proof
:=
  fun hi => h (connList_iso hi)

Linking counts of a drawing

The second invariant. It counts the crossings between two different arcs, with signs. It tells a crossing from its mirror image. Connectivity does not.

Arcs and signs

  • An arc is a piece of yarn with two ends on the boundary. The points have the numbers of conn.
  • Each arc runs from its smaller end to its larger end (Link.orient).
  • linkCount b u p q: the sum of the signs of the crossings between the arc through point p and the arc through point q. It is 0 when p and q are on one arc.
  • The count does not include crossings of an arc with itself. It does not include closed pieces of yarn.
  • The sign of cross i lo with two strands that go up is +1 when lo = true and -1 when lo = false (Link.sgn). A strand that goes down changes the sign.

The fold

The fold reads the drawing from bottom to top. The state Link.St has two parts.

Part Content
S The connectivity state of Conn. Link.run_S proves it.
M x y The signed count between the piece that ends at x and the piece that ends at y. Each piece runs to the end that names it.
  • A piece in progress is a pair {x, S x}. Each of its two ends names it.
  • M x y = 0 when x and y name one piece (Link.clean, Link.Clean).
Event Action on M
Crossing at n Relabels the table by swapAt n. Adds the sign to the two pieces that cross (Link.crossL, Link.edge).
Cup at n Adds a piece without crossings (Link.cupL).
Cap at n Joins two pieces. The row of each end gets the row of the other leg of the cap (Link.capL, Link.capV).

The move checks

  • Link.MoveChecks P: for each move m with P m, the two sides of m give the same state from each clean state with Inv.
  • Link.famFar, …, Link.famR3: the nine families of moves. Link.LinkFarChecks, …, Link.LinkR3Checks: their checks, as statements. The proofs follow in this article.
namespace FormalizeKnitting.Equality
namespace Link
open Conn
structure St where
  S : Nat → Nat
  M : Nat → Nat → Int

def edge (k : Nat) (S : Nat → Nat) (x : Nat) : Int := (if x = k then 1 else 0) - (if S x = k then 1 else 0)

def clean (S : Nat → Nat) (M : Nat → Nat → Int) : Nat → Nat → Int := fun x y => if y = x ∨ y = S x then 0 else M x y

def sgn (lo : Bool) : Int := if lo then 1 else -1

def crossL (n : Nat) (lo : Bool) (st : St) : St :=
  ⟨crossAct n st.S, clean (crossAct n st.S) fun x y => st.M (swapAt n x) (swapAt n y) +
    sgn lo * (edge n (crossAct n st.S) x * edge (n + 1) (crossAct n st.S) y +
      edge (n + 1) (crossAct n st.S) x * edge n (crossAct n st.S) y)⟩

def cupL (n : Nat) (st : St) : St :=
  ⟨cupAct n st.S, clean (cupAct n st.S) fun x y =>
    if x = n ∨ x = n + 1 ∨ y = n ∨ y = n + 1 then 0 else st.M (downAt n x) (downAt n y)⟩

def capV (n : Nat) (S : Nat → Nat) (v : Nat → Int) (a : Nat) : Int :=
  v a + (if S a = n then v (n + 1) else if S a = n + 1 then v n else 0)

def capL (n : Nat) (st : St) : St :=
  ⟨capAct n st.S, clean (capAct n st.S) fun x y =>
    capV n st.S (fun a => capV n st.S (st.M a) (upAt n y)) (upAt n x)⟩

def genL (b : Nat) : Gen → St → St
  | .cross i lo => crossL (b + i) lo
  | .cup i => cupL (b + i)
  | .cap i => capL (b + i)

def run (b : Nat) : St → MWord → St
  | st, [] => st
  | st, g :: gs => run b (genL b g st) gs

def init (b : Nat) : St := ⟨start b, fun _ _ => 0⟩

def orient (S : Nat → Nat) (p : Nat) : Int := if S p < p then 1 else -1
theorem run_append (b : Nat) (st : St) (u v : MWord) : run b st (u ++ v) = run b (run b st u) v
Proof
:= by
  induction u generalizing st with
  | nil => rfl
  | cons g gs ih => exact ih _
theorem genL_S (b : Nat) (g : Gen) (st : St) : (genL b g st).S = Conn.genAct b g st.S
Proof
:= by
  cases g <;> rfl
theorem run_S (b : Nat) (st : St) (u : MWord) : (run b st u).S = Conn.run b st.S u
Proof
:= by
  induction u generalizing st with
  | nil => rfl
  | cons g gs ih => simp only [run, Conn.run, ih, genL_S]
def Clean (st : St) : Prop := ∀ x y, y = x ∨ y = st.S x → st.M x y = 0
theorem genL_clean (b : Nat) (g : Gen) (st : St) : Clean (genL b g st)
Proof
:= by
  cases g <;> exact fun x y h => ite_eq_left h
theorem run_clean (b : Nat) (st : St) (hst : Clean st) (u : MWord) : Clean (run b st u)
Proof
:= by
  induction u generalizing st with
  | nil => exact hst
  | cons g gs ih => exact ih _ (genL_clean b g st)
theorem init_clean (b : Nat) : Clean (init b)
Proof
:= fun _ _ _ => rfl
def MoveChecks (P : Mv → Prop) : Prop :=
  ∀ (b w : Nat) (st : St) (m : Mv), P m → m.ok = true → Inv (b + w) st.S → Clean st →
    (topWidth w m.lhs).isSome = true → run b st m.lhs = run b st m.rhs

def famFar : Mv → Prop
  | .farR .. => True
  | .farL .. => True
  | _ => False

def famZig : Mv → Prop
  | .zigR _ => True
  | .zigL _ => True
  | _ => False

def famCupSlide : Mv → Prop
  | .cupSlideR .. => True
  | .cupSlideL .. => True
  | _ => False

def famCapSlide : Mv → Prop
  | .capSlideR .. => True
  | .capSlideL .. => True
  | _ => False

def famR1 : Mv → Prop
  | .r1R .. => True
  | .r1L .. => True
  | _ => False

def famR1Cup : Mv → Prop
  | .r1cup .. => True
  | _ => False

def famR1Cap : Mv → Prop
  | .r1cap .. => True
  | _ => False

def famR2 : Mv → Prop
  | .r2 .. => True
  | _ => False

def famR3 : Mv → Prop
  | .r3 .. => True
  | _ => False

def LinkFarChecks : Prop := MoveChecks famFar

def LinkZigChecks : Prop := MoveChecks famZig

def LinkCupSlideChecks : Prop := MoveChecks famCupSlide

def LinkCapSlideChecks : Prop := MoveChecks famCapSlide

def LinkR1Checks : Prop := MoveChecks famR1

def LinkR1CupChecks : Prop := MoveChecks famR1Cup

def LinkR1CapChecks : Prop := MoveChecks famR1Cap

def LinkR2Checks : Prop := MoveChecks famR2

def LinkR3Checks : Prop := MoveChecks famR3
end Link

open Link
def linkCount (b : Nat) (u : MWord) (p q : Nat) : Int :=
  orient (run b (init b) u).S p * orient (run b (init b) u).S q * (run b (init b) u).M p q
theorem linkTable_S (b : Nat) (u : MWord) : (run b (init b) u).S = conn b u
Proof
:= run_S b _ u

Linking counts: R2, R3, R1 at a cup, and the lists of points below

Three checks on crossings

Theorem Move Reason
Link.linkR2Checks R2 The two crossings add two products of edge with opposite signs. They cancel.
Link.linkR3Checks R3 The two sides add the same three products of edge.
Link.linkR1CupChecks R1 at a cup The crossing is between the two legs of one piece. clean removes its sign.
  • Link.nonself_cross: a crossing sends a pair of points of two different pieces to such a pair.
  • Link.edge_pull: an edge of the state below a crossing is an edge of the state above, at the other strand.

The lists of points below

Link.NonSelf S x y: the points x and y are on two different pieces of S. At such a pair, the table above an event is a sum of entries of the table below (Link.evalL), plus the crossing term (Link.crossE).

Event Points below for the point x above
Crossing at n [swapAt n x] (Link.crossP)
Cup at n [] for the two new strands, else [downAt n x] (Link.cupP)
Cap at n upAt n x, then the other leg when the piece of upAt n x reaches the cap (Link.capP, Link.capRow)
  • Each pair of points in these lists is a pair of two different pieces below (Link.cross_respects, Link.cup_respects, Link.cap_respects). So clean does not act on them.
  • Link.checks_of_nonself: a move check needs only the pairs of two different pieces.
namespace FormalizeKnitting.Equality.Link
open Conn
theorem St.ext' {a c : St} (h1 : a.S = c.S) (h2 : a.M = c.M) : a = c
Proof
:= by
  cases a; cases c; cases h1; cases h2; rfl
theorem swapAt_swapAt (n x : Nat) : swapAt n (swapAt n x) = x
Proof
:= by unfold swapAt; split_ifs <;> omega
theorem swapAt_eq_iff (n a k : Nat) : swapAt n a = k ↔ a = swapAt n k
Proof
:= by unfold swapAt; split_ifs <;> omega
theorem swapAt_inj (n a c : Nat) : swapAt n a = swapAt n c ↔ a = c
Proof
:= by unfold swapAt; split_ifs <;> omega
theorem swapAt_braid (n x : Nat) :
    swapAt n (swapAt (n + 1) (swapAt n x)) = swapAt (n + 1) (swapAt n (swapAt (n + 1) x))
Proof
:= by
  unfold swapAt; split_ifs <;> omega
theorem crossAct_swap (n : Nat) (S : Nat → Nat) (x : Nat) : crossAct n S (swapAt n x) = swapAt n (S x)
Proof
:= by
  simp only [crossAct, swapAt_swapAt]
theorem edge_pull (k n : Nat) (S : Nat → Nat) (x : Nat) :
    edge k S (swapAt n x) = edge (swapAt n k) (crossAct n S) x
Proof
:= by
  simp only [edge, crossAct, swapAt_eq_iff, swapAt_swapAt]
theorem nonself_cross {n : Nat} {S : Nat → Nat} {x y : Nat} (h : ¬ (y = x ∨ y = crossAct n S x)) :
    ¬ (swapAt n y = swapAt n x ∨ swapAt n y = S (swapAt n x))
Proof
:= by
  simp only [crossAct, swapAt_inj] at h ⊢
  rw [swapAt_eq_iff]
  exact h
theorem crossL_M_of {n : Nat} {lo : Bool} {st : St} {x y : Nat} (h : ¬ (y = x ∨ y = crossAct n st.S x)) :
    (crossL n lo st).M x y = st.M (swapAt n x) (swapAt n y) +
      sgn lo * (edge n (crossAct n st.S) x * edge (n + 1) (crossAct n st.S) y +
        edge (n + 1) (crossAct n st.S) x * edge n (crossAct n st.S) y)
Proof
:= by
  simp only [crossL, clean, ite_eq_right h]
theorem crossL_M_self {n : Nat} {lo : Bool} {st : St} {x y : Nat} (h : y = x ∨ y = crossAct n st.S x) :
    (crossL n lo st).M x y = 0
Proof
:= by
  simp only [crossL, clean, ite_eq_left h]
theorem crossL_S (n : Nat) (lo : Bool) (st : St) : (crossL n lo st).S = crossAct n st.S
Proof
:= rfl
theorem sgn_not (s : Bool) : sgn (!s) = - sgn s
Proof
:= by cases s <;> rfl
theorem r2_link (n : Nat) (s : Bool) (st : St) (hC : Clean st) : crossL n (!s) (crossL n s st) = st
Proof
:= by
  have hS : crossAct n (crossAct n st.S) = st.S := funext fun x => by simp only [crossAct, swapAt_swapAt]
  refine St.ext' (by simp only [crossL_S, hS]) (funext fun x => funext fun y => ?_)
  by_cases h : y = x ∨ y = st.S x
  · rw [crossL_M_self (by rw [crossL_S, hS]; exact h), hC x y h]
  · have h' : ¬ (y = x ∨ y = crossAct n (crossL n s st).S x) := by rw [crossL_S, hS]; exact h
    rw [crossL_M_of h', crossL_M_of (nonself_cross h'), swapAt_swapAt, swapAt_swapAt, crossL_S, hS,
      edge_pull, edge_pull, edge_pull, edge_pull, sgn_not, hS]
    have e1 : swapAt n n = n + 1 := by unfold swapAt; split_ifs <;> omega
    have e2 : swapAt n (n + 1) = n := by unfold swapAt; split_ifs <;> omega
    rw [e1, e2]
    grind
theorem r3_link (n : Nat) (s : Bool) (st : St) :
    crossL n s (crossL (n + 1) s (crossL n s st)) = crossL (n + 1) s (crossL n s (crossL (n + 1) s st))
Proof
:= by
  have hS : crossAct n (crossAct (n + 1) (crossAct n st.S)) = crossAct (n + 1) (crossAct n (crossAct (n + 1) st.S)) :=
    funext fun x => by simp only [crossAct]; rw [swapAt_braid, swapAt_braid]
  refine St.ext' (by simp only [crossL_S, hS]) (funext fun x => funext fun y => ?_)
  by_cases h : y = x ∨ y = crossAct n (crossAct (n + 1) (crossAct n st.S)) x
  · rw [crossL_M_self (by simp only [crossL_S]; exact h), crossL_M_self (by simp only [crossL_S, ← hS]; exact h)]
  · have hl : ¬ (y = x ∨ y = crossAct n (crossL (n + 1) s (crossL n s st)).S x) := by simp only [crossL_S]; exact h
    have hr : ¬ (y = x ∨ y = crossAct (n + 1) (crossL n s (crossL (n + 1) s st)).S x) := by
      simp only [crossL_S, ← hS]; exact h
    have hl2 := nonself_cross hl
    have hr2 := nonself_cross hr
    rw [crossL_M_of hl, crossL_M_of hl2, crossL_M_of (nonself_cross hl2), crossL_M_of hr, crossL_M_of hr2,
      crossL_M_of (nonself_cross hr2)]
    simp only [crossL_S, edge_pull]
    rw [swapAt_braid, swapAt_braid]
    simp only [← hS]
    have e1 : swapAt n n = n + 1 := by unfold swapAt; split_ifs <;> omega
    have e2 : swapAt n (n + 1) = n := by unfold swapAt; split_ifs <;> omega
    have e3 : swapAt (n + 1) n = n := by unfold swapAt; split_ifs <;> omega
    have e4 : swapAt (n + 1) (n + 1) = n + 2 := by unfold swapAt; split_ifs <;> omega
    have e5 : swapAt (n + 1) (n + 1 + 1) = n + 1 := by unfold swapAt; split_ifs <;> omega
    have e6 : swapAt n (n + 2) = n + 2 := by unfold swapAt; split_ifs <;> omega
    simp only [e1, e2, e3, e4, e5, e6, show n + 1 + 1 = n + 2 from rfl]
    grind
theorem linkR2Checks : LinkR2Checks
Proof
:= by
  intro b w st m hm hok hS hC hfit
  cases m <;> simp only [famR2] at hm
  case r2 i s => exact r2_link _ _ st hC
theorem linkR3Checks : LinkR3Checks
Proof
:= by
  intro b w st m hm hok hS hC hfit
  cases m <;> simp only [famR3] at hm
  case r3 i s =>
  show crossL (b + i) s (crossL (b + (i + 1)) s (crossL (b + i) s st)) =
    crossL (b + (i + 1)) s (crossL (b + i) s (crossL (b + (i + 1)) s st))
  rw [show b + (i + 1) = b + i + 1 by omega]
  exact r3_link _ _ st
theorem r1cup_link (n : Nat) (s : Bool) (st : St) : crossL n s (cupL n st) = cupL n st
Proof
:= by
  have hS : crossAct n (cupAct n st.S) = cupAct n st.S := funext fun x => by
    simp only [crossAct, cupAct, swapAt, upAt, downAt]; grind
  refine St.ext' hS (funext fun x => funext fun y => ?_)
  simp only [crossL, cupL, hS, clean, edge, sgn]
  simp only [cupAct, swapAt, upAt, downAt]
  grind (splits := 100)
theorem linkR1CupChecks : LinkR1CupChecks
Proof
:= by
  intro b w st m hm hok hS hC hfit
  cases m <;> simp only [famR1Cup] at hm
  case r1cup i s => exact r1cup_link _ _ st
def NonSelf (S : Nat → Nat) (x y : Nat) : Prop := ¬ (y = x ∨ y = S x)

def evalL (M : Nat → Nat → Int) (l1 l2 : List Nat) : Int := (l1.map fun a => (l2.map (M a)).sum).sum

def vsum (e : Nat → Int) (l : List Nat) : Int := (l.map e).sum

def crossP (n x : Nat) : List Nat := [swapAt n x]

def cupP (n x : Nat) : List Nat := if x = n ∨ x = n + 1 then [] else [downAt n x]

def capRow (n : Nat) (S : Nat → Nat) (a : Nat) : List Nat :=
  a :: (if S a = n then [n + 1] else if S a = n + 1 then [n] else [])

def capP (n : Nat) (S : Nat → Nat) (x : Nat) : List Nat := capRow n S (upAt n x)

def crossE (n : Nat) (S : Nat → Nat) (x y : Nat) : Int :=
  edge n S x * edge (n + 1) S y + edge (n + 1) S x * edge n S y

Sums over lists

theorem evalL_nil_left (M : Nat → Nat → Int) (l : List Nat) : evalL M [] l = 0
Proof
:= rfl
theorem evalL_cons_left (M : Nat → Nat → Int) (a : Nat) (l1 l2 : List Nat) :
    evalL M (a :: l1) l2 = (l2.map (M a)).sum + evalL M l1 l2
Proof
:= by
  simp [evalL]
theorem evalL_single (M : Nat → Nat → Int) (a c : Nat) : evalL M [a] [c] = M a c
Proof
:= by simp [evalL]
theorem evalL_congr {M M' : Nat → Nat → Int} {l1 l2 : List Nat} (h : ∀ a ∈ l1, ∀ c ∈ l2, M a c = M' a c) :
    evalL M l1 l2 = evalL M' l1 l2
Proof
:= by
  unfold evalL
  congr 1
  apply List.map_congr_left
  intro a ha
  congr 1
  exact List.map_congr_left (h a ha)
theorem sum_flatMap_map (f : Nat → List Nat) (g : Nat → Int) (l : List Nat) :
    ((l.flatMap f).map g).sum = (l.map fun a => ((f a).map g).sum).sum
Proof
:= by
  induction l with
  | nil => rfl
  | cons a l ih => simp [List.flatMap_cons, List.map_append, List.sum_append, ih]
theorem sum_map_add' (f g : Nat → Int) (l : List Nat) :
    (l.map fun c => f c + g c).sum = (l.map f).sum + (l.map g).sum
Proof
:= by
  induction l with
  | nil => simp
  | cons c l ih => simp only [List.map_cons, List.sum_cons, ih]; grind
theorem sum_map_mul_left' (k : Int) (f : Nat → Int) (l : List Nat) :
    (l.map fun c => k * f c).sum = k * (l.map f).sum
Proof
:= by
  induction l with
  | nil => simp
  | cons c l ih => simp only [List.map_cons, List.sum_cons, ih]; grind
theorem sum_map_sum_comm (g : Nat → Nat → Int) (l1 l2 : List Nat) :
    (l1.map fun a => (l2.map (g a)).sum).sum = (l2.map fun c => (l1.map fun a => g a c).sum).sum
Proof
:= by
  induction l1 with
  | nil => simp
  | cons a l ih => simp only [List.map_cons, List.sum_cons, ih, sum_map_add']
theorem evalL_bind (M : Nat → Nat → Int) (f : Nat → List Nat) (l1 l2 : List Nat) :
    evalL (fun a c => evalL M (f a) (f c)) l1 l2 = evalL M (l1.flatMap f) (l2.flatMap f)
Proof
:= by
  unfold evalL
  rw [sum_flatMap_map]
  congr 1
  apply List.map_congr_left
  intro a _
  have : ∀ a' : Nat, ((l2.flatMap f).map (M a')).sum = (l2.map fun c => ((f c).map (M a')).sum).sum :=
    fun a' => sum_flatMap_map f (M a') l2
  simp only [this]
  exact sum_map_sum_comm (fun c a' => M a' c |> fun _ => ((f c).map (M a')).sum) l2 (f a) |>.trans (by rfl)
theorem evalL_add (M N : Nat → Nat → Int) (l1 l2 : List Nat) :
    evalL (fun a c => M a c + N a c) l1 l2 = evalL M l1 l2 + evalL N l1 l2
Proof
:= by
  unfold evalL
  simp only [sum_map_add']
theorem evalL_mul (e f : Nat → Int) (l1 l2 : List Nat) :
    evalL (fun a c => e a * f c) l1 l2 = vsum e l1 * vsum f l2
Proof
:= by
  unfold evalL vsum
  simp only [sum_map_mul_left']
  induction l1 with
  | nil => simp
  | cons a l ih => simp only [List.map_cons, List.sum_cons, ih]; grind
theorem evalL_crossE (n : Nat) (S : Nat → Nat) (l1 l2 : List Nat) :
    evalL (crossE n S) l1 l2 = vsum (edge n S) l1 * vsum (edge (n + 1) S) l2 +
      vsum (edge (n + 1) S) l1 * vsum (edge n S) l2
Proof
:= by
  unfold crossE
  rw [evalL_add (fun a c => edge n S a * edge (n + 1) S c), evalL_mul, evalL_mul]
theorem evalL_smul (k : Int) (M : Nat → Nat → Int) (l1 l2 : List Nat) :
    evalL (fun a c => k * M a c) l1 l2 = k * evalL M l1 l2
Proof
:= by
  unfold evalL
  simp only [sum_map_mul_left']
theorem evalL_perm (M : Nat → Nat → Int) {l1 l1' l2 l2' : List Nat} (h1 : l1.Perm l1') (h2 : l2.Perm l2') :
    evalL M l1 l2 = evalL M l1' l2'
Proof
:= by
  unfold evalL
  rw [(h1.map _).sum_eq]
  congr 1
  apply List.map_congr_left
  intro a _
  exact (h2.map _).sum_eq
theorem vsum_perm (e : Nat → Int) {l l' : List Nat} (h : l.Perm l') : vsum e l = vsum e l'
Proof
:= (h.map _).sum_eq

Each event at a pair of points of two different pieces

theorem crossL_M_nonself {n : Nat} {lo : Bool} {st : St} {x y : Nat} (h : NonSelf (crossAct n st.S) x y) :
    (crossL n lo st).M x y = evalL st.M (crossP n x) (crossP n y) + sgn lo * crossE n (crossAct n st.S) x y
Proof
:= by
  rw [crossL_M_of h, crossP, crossP, evalL_single]; rfl
theorem cupL_M_nonself {n : Nat} {st : St} {x y : Nat} (h : NonSelf (cupAct n st.S) x y) :
    (cupL n st).M x y = evalL st.M (cupP n x) (cupP n y)
Proof
:= by
  show (if y = x ∨ y = cupAct n st.S x then 0 else _) = _
  rw [ite_eq_right h]
  dsimp only
  unfold cupP
  split_ifs <;> simp_all [evalL]
theorem capL_M_nonself {n : Nat} {st : St} {x y : Nat} (h : NonSelf (capAct n st.S) x y) :
    (capL n st).M x y = evalL st.M (capP n st.S x) (capP n st.S y)
Proof
:= by
  show (if y = x ∨ y = capAct n st.S x then 0 else _) = _
  rw [ite_eq_right h]
  dsimp only
  unfold capP capRow capV evalL
  split_ifs <;> simp only [List.map_cons, List.map_nil, List.sum_cons, List.sum_nil] <;> grind
theorem cross_respects {n : Nat} {S : Nat → Nat} {x y : Nat} (h : NonSelf (crossAct n S) x y) :
    ∀ a ∈ crossP n x, ∀ c ∈ crossP n y, NonSelf S a c
Proof
:= by
  intro a ha c hc
  simp only [crossP, List.mem_singleton] at ha hc
  subst ha hc
  exact nonself_cross h
theorem cup_respects {n : Nat} {S : Nat → Nat} {x y : Nat} (h : NonSelf (cupAct n S) x y) :
    ∀ a ∈ cupP n x, ∀ c ∈ cupP n y, NonSelf S a c
Proof
:= by
  intro a ha c hc
  unfold cupP at ha hc
  split_ifs at ha hc with hx hy
  · simp at ha
  · simp at ha
  · simp at hc
  · simp only [List.mem_singleton] at ha hc
    subst ha hc
    unfold NonSelf at h ⊢
    simp only [cupAct, ite_eq_right (show ¬ x = n by omega), ite_eq_right (show ¬ x = n + 1 by omega)] at h
    intro hh
    apply h
    unfold upAt downAt at *
    split_ifs at hh ⊢ <;> omega
theorem cap_respects {N n : Nat} {S : Nat → Nat} (hS : Inv N S) (hn : n + 1 < N) {x y : Nat}
    (h : NonSelf (capAct n S) x y) : ∀ a ∈ capP n S x, ∀ c ∈ capP n S y, NonSelf S a c
Proof
:= by
  obtain ⟨h1, h2, -⟩ := hS
  intro a ha c hc
  unfold capP capRow at ha hc
  have hA : upAt n x ≠ n ∧ upAt n x ≠ n + 1 := by unfold upAt; split_ifs <;> omega
  have hC : upAt n y ≠ n ∧ upAt n y ≠ n + 1 := by unfold upAt; split_ifs <;> omega
  have hdx : downAt n (upAt n x) = x := by unfold upAt downAt; split_ifs <;> omega
  have hdy : downAt n (upAt n y) = y := by unfold upAt downAt; split_ifs <;> omega
  have hAC : upAt n x ≠ upAt n y := fun e => h (Or.inl (by rw [← hdx, ← hdy, e]))
  have hT : ¬ (upAt n y = through n S (S (upAt n x))) := fun e =>
    h (Or.inr (by show y = downAt n (through n S (S (upAt n x))); rw [← e, hdy]))
  have f1 : S (upAt n x) = n → S n = upAt n x := fun e => by have := h1 (upAt n x); rwa [e] at this
  have f2 : S (upAt n x) = n + 1 → S (n + 1) = upAt n x := fun e => by have := h1 (upAt n x); rwa [e] at this
  have f3 : S (upAt n y) = n → S n = upAt n y := fun e => by have := h1 (upAt n y); rwa [e] at this
  have f4 : S (upAt n y) = n + 1 → S (n + 1) = upAt n y := fun e => by have := h1 (upAt n y); rwa [e] at this
  have f5 : S n = n + 1 → S (n + 1) = n := fun e => by have := h1 n; rwa [e] at this
  have f6 : S (n + 1) = n → S n = n + 1 := fun e => by have := h1 (n + 1); rwa [e] at this
  have hn0 : S n ≠ n := h2 n (by omega)
  have hn1 : S (n + 1) ≠ n + 1 := h2 (n + 1) hn
  unfold through at hT
  unfold NonSelf
  split_ifs at ha hc <;> simp only [List.mem_cons, List.not_mem_nil, or_false] at ha hc <;>
    rcases ha with rfl | rfl <;> rcases hc with rfl | rfl <;> split_ifs at hT <;> omega
theorem checks_of_nonself (P : Mv → Prop)
    (hM : ∀ (b w : Nat) (st : St) (m : Mv), P m → m.ok = true → Inv (b + w) st.S → Clean st →
      (topWidth w m.lhs).isSome = true → ∀ x y, NonSelf (run b st m.lhs).S x y →
        (run b st m.lhs).M x y = (run b st m.rhs).M x y) : MoveChecks P
Proof
:= by
  intro b w st m hm hok hS hC hfit
  have eS : (run b st m.lhs).S = (run b st m.rhs).S := by
    rw [run_S, run_S]; exact Conn.run_move b w st.S m hok hS hfit
  refine St.ext' eS (funext fun x => funext fun y => ?_)
  by_cases h : y = x ∨ y = (run b st m.lhs).S x
  · rw [run_clean b st hC _ x y h, run_clean b st hC _ x y (eS ▸ h)]
  · exact hM b w st m hm hok hS hC hfit x y h

Linking counts: zig-zags, slides and R1

Theorem Moves Reason
Link.linkZigChecks zigR, zigL No crossing. The list of points below x is [x] (Link.zigR_list, Link.zigL_list).
Link.linkR1CapChecks r1cap The crossing term through the cap is −2 · v x · v y. Two different pieces cannot both reach the cap (Link.cap_touch_both). So the term is 0.
Link.linkCupSlideChecks cupSlideR, cupSlideL The strand crosses the two legs of the cup with one sign. The legs are one piece and run in opposite directions. The two terms cancel (Link.edge_cup_sum).
Link.linkCapSlideChecks capSlideR, capSlideL The strand crosses the two legs of the cap with one sign. Through the cap, the counts of the two legs are opposite (Link.capRow_edges). The two terms cancel.
Link.linkR1Checks r1R, r1L The crossing is between the strand and a leg of the cup. The cap joins the two into one piece. Through the cap, the term is 2 · v x · v y. Two different pieces cannot both reach the cap. So the term is 0.
  • In each check, the lists of points below are the lists of the right side (Link.capSlideR_list, Link.r1R_list, …).
namespace FormalizeKnitting.Equality.Link
open Conn
theorem zigR_list (n : Nat) (S : Nat → Nat) (x : Nat) :
    (capP n (cupAct (n + 1) S) x).flatMap (cupP (n + 1)) = [x]
Proof
:= by
  unfold capP capRow
  split_ifs <;> simp only [List.flatMap_cons, List.flatMap_nil, List.append_nil] <;> unfold cupP <;>
    split_ifs <;> (try simp only [List.nil_append, List.append_nil, List.cons_append, List.cons.injEq, and_true,
      reduceCtorEq]) <;> unfold cupAct upAt downAt at * <;> split_ifs at * <;> omega
theorem zigL_list (n : Nat) (S : Nat → Nat) (x : Nat) :
    (capP (n + 1) (cupAct n S) x).flatMap (cupP n) = [x]
Proof
:= by
  unfold capP capRow
  split_ifs <;> simp only [List.flatMap_cons, List.flatMap_nil, List.append_nil] <;> unfold cupP <;>
    split_ifs <;> (try simp only [List.nil_append, List.append_nil, List.cons_append, List.cons.injEq, and_true,
      reduceCtorEq]) <;> unfold cupAct upAt downAt at * <;> split_ifs at * <;> omega
theorem cup_cap_M {N n k : Nat} {st : St} (hS : Inv N st.S) (hk : k ≤ N) (hn : n + 1 < N + 2) {x y : Nat}
    (h : NonSelf (capAct n (cupAct k st.S)) x y) :
    (capL n (cupL k st)).M x y =
      evalL st.M ((capP n (cupAct k st.S) x).flatMap (cupP k)) ((capP n (cupAct k st.S) y).flatMap (cupP k))
Proof
:= by
  rw [capL_M_nonself h, ← evalL_bind]
  apply evalL_congr
  intro a ha c hc
  exact cupL_M_nonself (cap_respects (inv_cup hS hk) hn h a ha c hc)
theorem linkZigChecks : LinkZigChecks
Proof
:= by
  apply checks_of_nonself
  intro b w st m hm hok hS hC hfit x y h
  cases m <;> simp only [famZig] at hm
  case zigR i =>
    obtain ⟨f1, hfit⟩ := fits_cons hfit
    obtain ⟨f2, -⟩ := fits_cons hfit
    simp only [Gen.fits, Gen.widthAfter, decide_eq_true_eq] at f1 f2
    replace f2 := of_decide_eq_true f2
    change NonSelf (capAct (b + i) (cupAct (b + (i + 1)) st.S)) x y at h
    show (capL (b + i) (cupL (b + (i + 1)) st)).M x y = st.M x y
    rw [show b + (i + 1) = b + i + 1 by omega] at h ⊢
    rw [cup_cap_M hS (by omega) (by omega) h, zigR_list, zigR_list, evalL_single]
  case zigL i =>
    obtain ⟨f1, hfit⟩ := fits_cons hfit
    obtain ⟨f2, -⟩ := fits_cons hfit
    simp only [Gen.fits, Gen.widthAfter, decide_eq_true_eq] at f1 f2
    replace f2 := of_decide_eq_true f2
    change NonSelf (capAct (b + (i + 1)) (cupAct (b + i) st.S)) x y at h
    show (capL (b + (i + 1)) (cupL (b + i) st)).M x y = st.M x y
    rw [show b + (i + 1) = b + i + 1 by omega] at h ⊢
    rw [cup_cap_M hS (by omega) (by omega) h, zigL_list, zigL_list, evalL_single]
theorem cap_touch_both {N n : Nat} {S : Nat → Nat} (hS : Inv N S) {x y : Nat}
    (h : NonSelf (capAct n S) x y) (hx : S (upAt n x) = n ∨ S (upAt n x) = n + 1)
    (hy : S (upAt n y) = n ∨ S (upAt n y) = n + 1) : False
Proof
:= by
  obtain ⟨h1, -, -⟩ := hS
  have hdx : downAt n (upAt n x) = x := by unfold upAt downAt; split_ifs <;> omega
  have hdy : downAt n (upAt n y) = y := by unfold upAt downAt; split_ifs <;> omega
  have hAC : upAt n x ≠ upAt n y := fun e => h (Or.inl (by rw [← hdx, ← hdy, e]))
  have hT : ¬ (upAt n y = through n S (S (upAt n x))) := fun e =>
    h (Or.inr (by show y = downAt n (through n S (S (upAt n x))); rw [← e, hdy]))
  have ex := h1 (upAt n x)
  have ey := h1 (upAt n y)
  unfold through at hT
  rcases hx with hx | hx <;> rcases hy with hy | hy <;> rw [hx] at ex hT <;> rw [hy] at ey <;> simp at hT <;>
    omega
theorem capRow_edges {N n : Nat} {S : Nat → Nat} (hS : Inv N S) (hn : n + 1 < N) {A : Nat} (hA1 : A ≠ n)
    (hA2 : A ≠ n + 1) :
    vsum (edge n S) (capRow n S A) = - vsum (edge (n + 1) S) (capRow n S A) ∧
      (¬ (S A = n ∨ S A = n + 1) → vsum (edge n S) (capRow n S A) = 0)
Proof
:= by
  obtain ⟨h1, h2, -⟩ := hS
  have e0 := h2 n (by omega)
  have e1 := h2 (n + 1) hn
  have s0 := h1 n
  have s1 := h1 (n + 1)
  have f1 : S A = n → S n = A := fun e => by have := h1 A; rwa [e] at this
  have f2 : S A = n + 1 → S (n + 1) = A := fun e => by have := h1 A; rwa [e] at this
  have f5 : S n = n + 1 → S (n + 1) = n := fun e => by have := s0; rwa [e] at this
  have f6 : S (n + 1) = n → S n = n + 1 := fun e => by have := s1; rwa [e] at this
  unfold capRow vsum edge
  split_ifs <;> simp only [List.map_cons, List.map_nil, List.sum_cons, List.sum_nil] <;> split_ifs <;> omega
theorem r1cap_list (n : Nat) (S : Nat → Nat) (x : Nat) :
    (capP n (crossAct n S) x).flatMap (crossP n) = capP n S x
Proof
:= by
  have hA : upAt n x ≠ n ∧ upAt n x ≠ n + 1 := by unfold upAt; split_ifs <;> omega
  unfold capP capRow
  have e : crossAct n S (upAt n x) = swapAt n (S (upAt n x)) := by
    simp only [crossAct]; rw [swapAt_o hA.1 hA.2]
  rw [e]
  have hsA : swapAt n (upAt n x) = upAt n x := swapAt_o hA.1 hA.2
  generalize S (upAt n x) = s at *
  rcases (by omega : s = n ∨ s = n + 1 ∨ (s ≠ n ∧ s ≠ n + 1)) with e | e | ⟨e1, e2⟩
  · rw [e]; simp [swapAt, crossP, hA.1, hA.2]
  · rw [e]; simp [swapAt, crossP, hA.1, hA.2]
  · rw [swapAt_o e1 e2]; simp [crossP, hsA, e1, e2]
theorem r1cap_link {N n : Nat} (s : Bool) (st : St) (hS : Inv N st.S) (hn : n + 1 < N) (x y : Nat)
    (h : NonSelf (capAct n (crossAct n st.S)) x y) : (capL n (crossL n s st)).M x y = (capL n st).M x y
Proof
:= by
  have hS1 : Inv N (crossAct n st.S) := inv_cross hS hn
  have eq : capAct n (crossAct n st.S) = capAct n st.S := r1cap_eq hS hn
  rw [capL_M_nonself h, capL_M_nonself (eq ▸ h), crossL_S]
  have step : evalL (crossL n s st).M (capP n (crossAct n st.S) x) (capP n (crossAct n st.S) y) =
      evalL (fun a c => evalL st.M (crossP n a) (crossP n c) + sgn s * crossE n (crossAct n st.S) a c)
        (capP n (crossAct n st.S) x) (capP n (crossAct n st.S) y) :=
    evalL_congr fun a ha c hc => crossL_M_nonself (cap_respects hS1 hn h a ha c hc)
  rw [step, evalL_add, evalL_bind, r1cap_list, r1cap_list, evalL_smul, evalL_crossE]
  have hA : upAt n x ≠ n ∧ upAt n x ≠ n + 1 := by unfold upAt; split_ifs <;> omega
  have hC : upAt n y ≠ n ∧ upAt n y ≠ n + 1 := by unfold upAt; split_ifs <;> omega
  obtain ⟨ex, zx⟩ := capRow_edges hS1 hn hA.1 hA.2
  obtain ⟨ey, zy⟩ := capRow_edges hS1 hn hC.1 hC.2
  unfold capP
  rw [ex, ey]
  by_cases tx : crossAct n st.S (upAt n x) = n ∨ crossAct n st.S (upAt n x) = n + 1
  · by_cases ty : crossAct n st.S (upAt n y) = n ∨ crossAct n st.S (upAt n y) = n + 1
    · exact (cap_touch_both hS1 h tx ty).elim
    · have fy : vsum (edge (n + 1) (crossAct n st.S)) (capRow n (crossAct n st.S) (upAt n y)) = 0 := by
        have := zy ty; rw [ey] at this; omega
      rw [fy]; grind
  · have fx : vsum (edge (n + 1) (crossAct n st.S)) (capRow n (crossAct n st.S) (upAt n x)) = 0 := by
      have := zx tx; rw [ex] at this; omega
    rw [fx]; grind
theorem linkR1CapChecks : LinkR1CapChecks
Proof
:= by
  apply checks_of_nonself
  intro b w st m hm hok hS hC hfit x y h
  cases m <;> simp only [famR1Cap] at hm
  case r1cap i s =>
    obtain ⟨f1, -⟩ := fits_cons hfit
    simp only [Gen.fits, decide_eq_true_eq] at f1
    exact r1cap_link (n := b + i) s st hS (by omega) x y h
theorem edge_cup_sum (n : Nat) (S : Nat → Nat) (z : Nat) :
    edge n (cupAct n S) z + edge (n + 1) (cupAct n S) z = 0
Proof
:= by
  unfold edge cupAct upAt
  split_ifs <;> omega
theorem cupSlideR_list (n x : Nat) : cupP (n + 1) (swapAt n (swapAt (n + 1) x)) = cupP n x
Proof
:= by
  unfold cupP swapAt downAt
  split_ifs <;> (try simp only [List.cons.injEq, and_true]) <;> omega
theorem cupSlideL_list (n x : Nat) : cupP n (swapAt (n + 1) (swapAt n x)) = cupP (n + 1) x
Proof
:= by
  unfold cupP swapAt downAt
  split_ifs <;> (try simp only [List.cons.injEq, and_true]) <;> omega
theorem cupSlideR_link {N n : Nat} (s : Bool) (st : St) (hS : Inv N st.S) (x y : Nat)
    (h : NonSelf (crossAct (n + 1) (crossAct n (cupAct (n + 1) st.S))) x y) :
    (crossL (n + 1) s (crossL n s (cupL (n + 1) st))).M x y = (cupL n st).M x y
Proof
:= by
  have eq := cupSlideR_eq (n := n) hS
  have h2 := nonself_cross h
  have h1 := nonself_cross h2
  rw [crossL_M_of (st := crossL n s (cupL (n + 1) st)) h, crossL_M_of (st := cupL (n + 1) st) h2,
    cupL_M_nonself (st := st) h1, cupL_M_nonself (st := st) (eq ▸ h), cupSlideR_list, cupSlideR_list]
  have cS : (cupL (n + 1) st).S = cupAct (n + 1) st.S := rfl
  simp only [crossL_S, cS, edge_pull]
  rw [eq]
  have e1 : swapAt (n + 1) n = n := by unfold swapAt; split_ifs <;> omega
  have e2 : swapAt (n + 1) (n + 1) = n + 1 + 1 := by unfold swapAt; split_ifs <;> omega
  rw [e1, e2]
  have a := edge_cup_sum n st.S x
  have b := edge_cup_sum n st.S y
  have ax : edge (n + 1) (cupAct n st.S) x = - edge n (cupAct n st.S) x := by omega
  have bx : edge (n + 1) (cupAct n st.S) y = - edge n (cupAct n st.S) y := by omega
  rw [ax, bx]
  grind
theorem cupSlideL_link {N n : Nat} (s : Bool) (st : St) (hS : Inv N st.S) (x y : Nat)
    (h : NonSelf (crossAct n (crossAct (n + 1) (cupAct n st.S))) x y) :
    (crossL n s (crossL (n + 1) s (cupL n st))).M x y = (cupL (n + 1) st).M x y
Proof
:= by
  have eq := cupSlideL_eq (n := n) hS
  have h2 := nonself_cross h
  have h1 := nonself_cross h2
  rw [crossL_M_of (st := crossL (n + 1) s (cupL n st)) h, crossL_M_of (st := cupL n st) h2,
    cupL_M_nonself (st := st) h1, cupL_M_nonself (st := st) (eq ▸ h), cupSlideL_list, cupSlideL_list]
  have cS : (cupL n st).S = cupAct n st.S := rfl
  simp only [crossL_S, cS, edge_pull]
  rw [eq]
  have e1 : swapAt n (n + 1) = n := by unfold swapAt; split_ifs <;> omega
  have e2 : swapAt n (n + 1 + 1) = n + 1 + 1 := by unfold swapAt; split_ifs <;> omega
  rw [e1, e2]
  have a := edge_cup_sum (n + 1) st.S x
  have b := edge_cup_sum (n + 1) st.S y
  have ax : edge (n + 1 + 1) (cupAct (n + 1) st.S) x = - edge (n + 1) (cupAct (n + 1) st.S) x := by omega
  have bx : edge (n + 1 + 1) (cupAct (n + 1) st.S) y = - edge (n + 1) (cupAct (n + 1) st.S) y := by omega
  rw [ax, bx]
  grind
theorem linkCupSlideChecks : LinkCupSlideChecks
Proof
:= by
  apply checks_of_nonself
  intro b w st m hm hok hS hC hfit x y h
  cases m <;> simp only [famCupSlide] at hm
  case cupSlideR i s =>
    change NonSelf (crossAct (b + (i + 1)) (crossAct (b + i) (cupAct (b + (i + 1)) st.S))) x y at h
    show (crossL (b + (i + 1)) s (crossL (b + i) s (cupL (b + (i + 1)) st))).M x y = (cupL (b + i) st).M x y
    rw [show b + (i + 1) = b + i + 1 by omega] at h ⊢
    exact cupSlideR_link s st hS x y h
  case cupSlideL i s =>
    change NonSelf (crossAct (b + i) (crossAct (b + (i + 1)) (cupAct (b + i) st.S))) x y at h
    show (crossL (b + i) s (crossL (b + (i + 1)) s (cupL (b + i) st))).M x y = (cupL (b + (i + 1)) st).M x y
    rw [show b + (i + 1) = b + i + 1 by omega] at h ⊢
    exact cupSlideL_link s st hS x y h
theorem vsum_add (e f : Nat → Int) (l : List Nat) : vsum (fun z => e z + f z) l = vsum e l + vsum f l
Proof
:=
  sum_map_add' e f l
theorem capSlideR_list (n : Nat) (S : Nat → Nat) (x : Nat) :
    (capP n (crossAct (n + 1) (crossAct n S)) x).flatMap (fun a => [swapAt n (swapAt (n + 1) a)]) =
      capP (n + 1) S x
Proof
:= by
  have hA : swapAt n (swapAt (n + 1) (upAt n x)) = upAt (n + 1) x := by
    unfold swapAt upAt; split_ifs <;> omega
  have e : crossAct (n + 1) (crossAct n S) (upAt n x) = swapAt (n + 1) (swapAt n (S (upAt (n + 1) x))) := by
    simp only [crossAct]; rw [hA]
  unfold capP capRow
  rw [e, List.flatMap_cons, hA]
  clear e
  generalize S (upAt (n + 1) x) = t
  have hx : upAt (n + 1) x ≠ n + 1 ∧ upAt (n + 1) x ≠ n + 1 + 1 := by unfold upAt; split_ifs <;> omega
  rcases (by omega : t = n ∨ t = n + 1 ∨ t = n + 1 + 1 ∨ (t ≠ n ∧ t ≠ n + 1 ∧ t ≠ n + 1 + 1)) with
    hc | hc | hc | ⟨c1, c2, c3⟩
  all_goals (try rw [hc])
  all_goals simp (disch := omega) [swapAt_l, swapAt_r, swapAt_o, *]
  split_ifs <;> first | omega | simp
theorem capSlideL_list (n : Nat) (S : Nat → Nat) (x : Nat) :
    (capP (n + 1) (crossAct n (crossAct (n + 1) S)) x).flatMap (fun a => [swapAt (n + 1) (swapAt n a)]) =
      capP n S x
Proof
:= by
  have hA : swapAt (n + 1) (swapAt n (upAt (n + 1) x)) = upAt n x := by
    unfold swapAt upAt; split_ifs <;> omega
  have e : crossAct n (crossAct (n + 1) S) (upAt (n + 1) x) = swapAt n (swapAt (n + 1) (S (upAt n x))) := by
    simp only [crossAct]; rw [hA]
  unfold capP capRow
  rw [e, List.flatMap_cons, hA]
  clear e
  generalize S (upAt n x) = t
  have hx : upAt n x ≠ n ∧ upAt n x ≠ n + 1 := by unfold upAt; split_ifs <;> omega
  rcases (by omega : t = n ∨ t = n + 1 ∨ t = n + 1 + 1 ∨ (t ≠ n ∧ t ≠ n + 1 ∧ t ≠ n + 1 + 1)) with
    hc | hc | hc | ⟨c1, c2, c3⟩
  all_goals (try rw [hc])
  all_goals simp (disch := omega) [swapAt_l, swapAt_r, swapAt_o, *]
  split_ifs <;> first | omega | simp
theorem capSlideR_link {N n : Nat} (s : Bool) (st : St) (hS : Inv N st.S) (hn : n + 2 < N) (x y : Nat)
    (h : NonSelf (capAct n (crossAct (n + 1) (crossAct n st.S))) x y) :
    (capL n (crossL (n + 1) s (crossL n s st))).M x y = (capL (n + 1) st).M x y
Proof
:= by
  have eq := capSlideR_eq hS hn
  have hS2 : Inv N (crossAct (n + 1) (crossAct n st.S)) := inv_cross (inv_cross hS (by omega)) (by omega)
  rw [capL_M_nonself h, capL_M_nonself (eq ▸ h), crossL_S, crossL_S]
  have step : evalL (crossL (n + 1) s (crossL n s st)).M (capP n (crossAct (n + 1) (crossAct n st.S)) x)
        (capP n (crossAct (n + 1) (crossAct n st.S)) y) =
      evalL (fun a c => st.M (swapAt n (swapAt (n + 1) a)) (swapAt n (swapAt (n + 1) c)) +
          sgn s * crossE n (crossAct n st.S) (swapAt (n + 1) a) (swapAt (n + 1) c) +
          sgn s * crossE (n + 1) (crossAct (n + 1) (crossAct n st.S)) a c)
        (capP n (crossAct (n + 1) (crossAct n st.S)) x) (capP n (crossAct (n + 1) (crossAct n st.S)) y) := by
    apply evalL_congr
    intro a ha c hc
    have h2 := cap_respects hS2 (by omega) h a ha c hc
    rw [crossL_M_of (st := crossL n s st) h2, crossL_M_of (st := st) (nonself_cross h2)]
    rfl
  rw [step, evalL_add, evalL_add, show (fun a c => st.M (swapAt n (swapAt (n + 1) a)) (swapAt n (swapAt (n + 1) c)))
    = fun a c => evalL st.M [swapAt n (swapAt (n + 1) a)] [swapAt n (swapAt (n + 1) c)] from by
      funext a c; rw [evalL_single], evalL_bind, capSlideR_list, capSlideR_list, evalL_smul, evalL_smul,
    evalL_crossE]
  have e2 : (fun a c => crossE n (crossAct n st.S) (swapAt (n + 1) a) (swapAt (n + 1) c)) =
      fun a c => edge n (crossAct (n + 1) (crossAct n st.S)) a * edge (n + 2) (crossAct (n + 1) (crossAct n st.S)) c +
        edge (n + 2) (crossAct (n + 1) (crossAct n st.S)) a * edge n (crossAct (n + 1) (crossAct n st.S)) c := by
    funext a c
    unfold crossE
    rw [edge_pull, edge_pull, edge_pull, edge_pull]
    have e1 : swapAt (n + 1) n = n := by unfold swapAt; split_ifs <;> omega
    have e2 : swapAt (n + 1) (n + 1) = n + 2 := by unfold swapAt; split_ifs <;> omega
    rw [e1, e2]
  rw [e2, evalL_add (fun a c => _ * _), evalL_mul, evalL_mul]
  have hA : upAt n x ≠ n ∧ upAt n x ≠ n + 1 := by unfold upAt; split_ifs <;> omega
  have hC : upAt n y ≠ n ∧ upAt n y ≠ n + 1 := by unfold upAt; split_ifs <;> omega
  obtain ⟨ex, -⟩ := capRow_edges hS2 (by omega) hA.1 hA.2
  obtain ⟨ey, -⟩ := capRow_edges hS2 (by omega) hC.1 hC.2
  unfold capP
  rw [ex, ey]
  grind
theorem capSlideL_link {N n : Nat} (s : Bool) (st : St) (hS : Inv N st.S) (hn : n + 2 < N) (x y : Nat)
    (h : NonSelf (capAct (n + 1) (crossAct n (crossAct (n + 1) st.S))) x y) :
    (capL (n + 1) (crossL n s (crossL (n + 1) s st))).M x y = (capL n st).M x y
Proof
:= by
  have eq := capSlideL_eq hS hn
  have hS2 : Inv N (crossAct n (crossAct (n + 1) st.S)) := inv_cross (inv_cross hS (by omega)) (by omega)
  rw [capL_M_nonself h, capL_M_nonself (eq ▸ h), crossL_S, crossL_S]
  have step : evalL (crossL n s (crossL (n + 1) s st)).M (capP (n + 1) (crossAct n (crossAct (n + 1) st.S)) x)
        (capP (n + 1) (crossAct n (crossAct (n + 1) st.S)) y) =
      evalL (fun a c => st.M (swapAt (n + 1) (swapAt n a)) (swapAt (n + 1) (swapAt n c)) +
          sgn s * crossE (n + 1) (crossAct (n + 1) st.S) (swapAt n a) (swapAt n c) +
          sgn s * crossE n (crossAct n (crossAct (n + 1) st.S)) a c)
        (capP (n + 1) (crossAct n (crossAct (n + 1) st.S)) x)
        (capP (n + 1) (crossAct n (crossAct (n + 1) st.S)) y) := by
    apply evalL_congr
    intro a ha c hc
    have h2 := cap_respects hS2 (by omega) h a ha c hc
    rw [crossL_M_of (st := crossL (n + 1) s st) h2, crossL_M_of (st := st) (nonself_cross h2)]
    rfl
  rw [step, evalL_add, evalL_add, show (fun a c => st.M (swapAt (n + 1) (swapAt n a)) (swapAt (n + 1) (swapAt n c)))
    = fun a c => evalL st.M [swapAt (n + 1) (swapAt n a)] [swapAt (n + 1) (swapAt n c)] from by
      funext a c; rw [evalL_single], evalL_bind, capSlideL_list, capSlideL_list, evalL_smul, evalL_smul,
    evalL_crossE]
  have e2 : (fun a c => crossE (n + 1) (crossAct (n + 1) st.S) (swapAt n a) (swapAt n c)) =
      fun a c => edge n (crossAct n (crossAct (n + 1) st.S)) a * edge (n + 2) (crossAct n (crossAct (n + 1) st.S)) c +
        edge (n + 2) (crossAct n (crossAct (n + 1) st.S)) a * edge n (crossAct n (crossAct (n + 1) st.S)) c := by
    funext a c
    unfold crossE
    rw [edge_pull, edge_pull, edge_pull, edge_pull]
    have e1 : swapAt n (n + 1) = n := by unfold swapAt; split_ifs <;> omega
    have e2 : swapAt n (n + 1 + 1) = n + 2 := by unfold swapAt; split_ifs <;> omega
    rw [e1, e2]
  rw [e2, evalL_add (fun a c => _ * _), evalL_mul, evalL_mul]
  have hA : upAt (n + 1) x ≠ n + 1 ∧ upAt (n + 1) x ≠ n + 1 + 1 := by unfold upAt; split_ifs <;> omega
  have hC : upAt (n + 1) y ≠ n + 1 ∧ upAt (n + 1) y ≠ n + 1 + 1 := by unfold upAt; split_ifs <;> omega
  obtain ⟨ex, -⟩ := capRow_edges hS2 (by omega) hA.1 hA.2
  obtain ⟨ey, -⟩ := capRow_edges hS2 (by omega) hC.1 hC.2
  unfold capP
  rw [show n + 2 = n + 1 + 1 from rfl, ex, ey]
  grind
theorem linkCapSlideChecks : LinkCapSlideChecks
Proof
:= by
  apply checks_of_nonself
  intro b w st m hm hok hS hC hfit x y h
  cases m <;> simp only [famCapSlide] at hm
  case capSlideR i s =>
    obtain ⟨f1, hfit⟩ := fits_cons hfit
    obtain ⟨f2, -⟩ := fits_cons hfit
    simp only [Gen.fits, Gen.widthAfter, decide_eq_true_eq] at f1 f2
    replace f2 := of_decide_eq_true f2
    change NonSelf (capAct (b + i) (crossAct (b + (i + 1)) (crossAct (b + i) st.S))) x y at h
    show (capL (b + i) (crossL (b + (i + 1)) s (crossL (b + i) s st))).M x y = (capL (b + (i + 1)) st).M x y
    rw [show b + (i + 1) = b + i + 1 by omega] at h ⊢
    exact capSlideR_link s st hS (by omega) x y h
  case capSlideL i s =>
    obtain ⟨f1, -⟩ := fits_cons hfit
    simp only [Gen.fits, decide_eq_true_eq] at f1
    change NonSelf (capAct (b + (i + 1)) (crossAct (b + i) (crossAct (b + (i + 1)) st.S))) x y at h
    show (capL (b + (i + 1)) (crossL (b + i) s (crossL (b + (i + 1)) s st))).M x y = (capL (b + i) st).M x y
    rw [show b + (i + 1) = b + i + 1 by omega] at h ⊢
    exact capSlideL_link s st hS (by omega) x y h
theorem r1R_edges {N n : Nat} {T : Nat → Nat} (h1 : ∀ z, T (T z) = z) (h2 : ∀ z, z < N → T z ≠ z) (hn : n + 1 + 1 < N)
    (hT : T n = n + 1 + 1) {A : Nat} (hA1 : A ≠ n + 1) (hA2 : A ≠ n + 1 + 1) :
    vsum (edge n T) (capRow (n + 1) T A) = vsum (edge (n + 1) T) (capRow (n + 1) T A) ∧
      (¬ (T A = n + 1 ∨ T A = n + 1 + 1) → vsum (edge n T) (capRow (n + 1) T A) = 0)
Proof
:= by
  have t2 : T (n + 1 + 1) = n := by rw [← hT, h1]
  have e1 := h2 (n + 1) (by omega)
  have f1 : T A = n + 1 → T (n + 1) = A := fun e => by have := h1 A; rwa [e] at this
  have f2 : T A = n + 1 + 1 → A = n := fun e => by have := h1 A; rw [e, t2] at this; omega
  have f3 : T (n + 1) = n → False := fun e => by have := h1 (n + 1); rw [e, hT] at this; omega
  have f4 : T (n + 1) = n + 1 + 1 → False := fun e => by have := h1 (n + 1); rw [e, t2] at this; omega
  have f5 : A = n → T A = n + 1 + 1 := fun e => by rw [e, hT]
  have f6 : T A = n → A = n + 1 + 1 := fun e => by have := h1 A; rw [e, hT] at this; omega
  unfold capRow vsum edge
  split_ifs <;> simp only [List.map_cons, List.map_nil, List.sum_cons, List.sum_nil] <;> split_ifs <;> omega
theorem r1L_edges {N n : Nat} {T : Nat → Nat} (h1 : ∀ z, T (T z) = z) (h2 : ∀ z, z < N → T z ≠ z) (hn : n + 1 + 1 < N)
    (hT : T n = n + 1 + 1) {A : Nat} (hA1 : A ≠ n) (hA2 : A ≠ n + 1) :
    vsum (edge (n + 1) T) (capRow n T A) = vsum (edge (n + 1 + 1) T) (capRow n T A) ∧
      (¬ (T A = n ∨ T A = n + 1) → vsum (edge (n + 1) T) (capRow n T A) = 0)
Proof
:= by
  have t2 : T (n + 1 + 1) = n := by rw [← hT, h1]
  have e1 := h2 (n + 1) (by omega)
  have f1 : T A = n + 1 → T (n + 1) = A := fun e => by have := h1 A; rwa [e] at this
  have f2 : T A = n → A = n + 1 + 1 := fun e => by have := h1 A; rw [e, hT] at this; omega
  have f3 : T (n + 1) = n + 1 + 1 → False := fun e => by have := h1 (n + 1); rw [e, t2] at this; omega
  have f4 : T (n + 1) = n → False := fun e => by have := h1 (n + 1); rw [e, hT] at this; omega
  have f5 : A = n + 1 + 1 → T A = n := fun e => by rw [e, t2]
  have f6 : T A = n + 1 + 1 → A = n := fun e => by have := h1 A; rw [e, t2] at this; omega
  unfold capRow vsum edge
  split_ifs <;> simp only [List.map_cons, List.map_nil, List.sum_cons, List.sum_nil] <;> split_ifs <;> omega
theorem r1R_list (n : Nat) (S : Nat → Nat) (x : Nat) :
    (capP (n + 1) (crossAct n (cupAct (n + 1) S)) x).flatMap (fun a => cupP (n + 1) (swapAt n a)) = [x]
Proof
:= by
  by_cases hx : x = n
  · rw [hx]
    have hu : upAt (n + 1) n = n := upAt_lt (by omega)
    have hT : crossAct n (cupAct (n + 1) S) n = n + 1 + 1 := by
      rw [crossAct_apply, swapAt_l rfl, cupAct_l _ rfl, swapAt_o (by omega) (by omega)]
    simp only [capP, capRow, hu, hT]
    simp [cupP, swapAt, downAt]
    omega
  · have hA : upAt (n + 1) x ≠ n ∧ upAt (n + 1) x ≠ n + 1 ∧ upAt (n + 1) x ≠ n + 1 + 1 := by
      unfold upAt; split_ifs <;> omega
    have hd : downAt (n + 1) (upAt (n + 1) x) = x := by unfold upAt downAt; split_ifs <;> omega
    have hT : crossAct n (cupAct (n + 1) S) (upAt (n + 1) x) = swapAt n (upAt (n + 1) (S x)) := by
      rw [crossAct_apply, swapAt_o hA.1 hA.2.1, cupAct_o _ hA.2.1 hA.2.2, hd]
    have g1 : cupP (n + 1) (swapAt n (upAt (n + 1) x)) = [x] := by
      rw [swapAt_o hA.1 hA.2.1, cupP, ite_eq_right (by omega), hd]
    have g2 : cupP (n + 1) (swapAt n (n + 1 + 1)) = [] := by
      rw [swapAt_o (by omega) (by omega), cupP, ite_eq_left (Or.inr rfl)]
    simp only [capP, capRow, hT, List.flatMap_cons, g1]
    generalize S x = t
    have c1 : swapAt n (upAt (n + 1) t) ≠ n + 1 + 1 := by unfold swapAt upAt; split_ifs <;> omega
    rw [ite_eq_right c1]
    split_ifs
    · rw [List.flatMap_cons, g2]; rfl
    · rfl
theorem r1L_list (n : Nat) (S : Nat → Nat) (x : Nat) :
    (capP n (crossAct (n + 1) (cupAct n S)) x).flatMap (fun a => cupP n (swapAt (n + 1) a)) = [x]
Proof
:= by
  by_cases hx : x = n
  · rw [hx]
    have hu : upAt n n = n + 1 + 1 := upAt_ge (by omega)
    have hT : crossAct (n + 1) (cupAct n S) (n + 1 + 1) = n := by
      rw [crossAct_apply, swapAt_r rfl, cupAct_r _ rfl, swapAt_o (by omega) (by omega)]
    simp only [capP, capRow, hu, hT]
    simp [cupP, swapAt, downAt]
    split_ifs <;> first | omega | simp
  · have hA : upAt n x ≠ n ∧ upAt n x ≠ n + 1 ∧ upAt n x ≠ n + 1 + 1 := by
      unfold upAt; split_ifs <;> omega
    have hd : downAt n (upAt n x) = x := by unfold upAt downAt; split_ifs <;> omega
    have hT : crossAct (n + 1) (cupAct n S) (upAt n x) = swapAt (n + 1) (upAt n (S x)) := by
      rw [crossAct_apply, swapAt_o hA.2.1 hA.2.2, cupAct_o _ hA.1 hA.2.1, hd]
    have g1 : cupP n (swapAt (n + 1) (upAt n x)) = [x] := by
      rw [swapAt_o hA.2.1 hA.2.2, cupP, ite_eq_right (by omega), hd]
    have g2 : cupP n (swapAt (n + 1) n) = [] := by
      rw [swapAt_o (by omega) (by omega), cupP, ite_eq_left (Or.inl rfl)]
    simp only [capP, capRow, hT, List.flatMap_cons, g1]
    generalize S x = t
    have c1 : swapAt (n + 1) (upAt n t) ≠ n := by unfold swapAt upAt; split_ifs <;> omega
    rw [ite_eq_right c1]
    split_ifs
    · rw [List.flatMap_cons, g2]; rfl
    · rfl
theorem r1R_link {N n : Nat} (s : Bool) (st : St) (hS : Inv N st.S) (hn : n < N) (x y : Nat)
    (h : NonSelf (capAct (n + 1) (crossAct n (cupAct (n + 1) st.S))) x y) :
    (capL (n + 1) (crossL n s (cupL (n + 1) st))).M x y = st.M x y
Proof
:= by
  have hT : Inv (N + 2) (crossAct n (cupAct (n + 1) st.S)) := inv_cross (inv_cup hS (by omega)) (by omega)
  have cS : (cupL (n + 1) st).S = cupAct (n + 1) st.S := rfl
  rw [capL_M_nonself h, crossL_S, cS]
  have step : evalL (crossL n s (cupL (n + 1) st)).M (capP (n + 1) (crossAct n (cupAct (n + 1) st.S)) x)
        (capP (n + 1) (crossAct n (cupAct (n + 1) st.S)) y) =
      evalL (fun a c => evalL st.M (cupP (n + 1) (swapAt n a)) (cupP (n + 1) (swapAt n c)) +
          sgn s * crossE n (crossAct n (cupAct (n + 1) st.S)) a c)
        (capP (n + 1) (crossAct n (cupAct (n + 1) st.S)) x) (capP (n + 1) (crossAct n (cupAct (n + 1) st.S)) y) := by
    apply evalL_congr
    intro a ha c hc
    have h2 := cap_respects hT (by omega) h a ha c hc
    rw [crossL_M_of (st := cupL (n + 1) st) h2, cupL_M_nonself (st := st) (nonself_cross h2)]
    rfl
  rw [step, evalL_add, evalL_bind st.M (fun a => cupP (n + 1) (swapAt n a)), r1R_list, r1R_list, evalL_single,
    evalL_smul, evalL_crossE]
  have tn : crossAct n (cupAct (n + 1) st.S) n = n + 1 + 1 := by
    rw [crossAct_apply, swapAt_l rfl, cupAct_l _ rfl, swapAt_o (by omega) (by omega)]
  have hA : upAt (n + 1) x ≠ n + 1 ∧ upAt (n + 1) x ≠ n + 1 + 1 := by unfold upAt; split_ifs <;> omega
  have hC : upAt (n + 1) y ≠ n + 1 ∧ upAt (n + 1) y ≠ n + 1 + 1 := by unfold upAt; split_ifs <;> omega
  obtain ⟨ex, zx⟩ := r1R_edges hT.1 hT.2.1 (by omega) tn hA.1 hA.2
  obtain ⟨ey, zy⟩ := r1R_edges hT.1 hT.2.1 (by omega) tn hC.1 hC.2
  unfold capP
  rw [← ex, ← ey]
  by_cases tx : crossAct n (cupAct (n + 1) st.S) (upAt (n + 1) x) = n + 1 ∨
      crossAct n (cupAct (n + 1) st.S) (upAt (n + 1) x) = n + 1 + 1
  · by_cases ty : crossAct n (cupAct (n + 1) st.S) (upAt (n + 1) y) = n + 1 ∨
        crossAct n (cupAct (n + 1) st.S) (upAt (n + 1) y) = n + 1 + 1
    · exact (cap_touch_both hT h tx ty).elim
    · rw [zy ty]; grind
  · rw [zx tx]; grind
theorem r1L_link {N n : Nat} (s : Bool) (st : St) (hS : Inv N st.S) (hn : n < N) (x y : Nat)
    (h : NonSelf (capAct n (crossAct (n + 1) (cupAct n st.S))) x y) :
    (capL n (crossL (n + 1) s (cupL n st))).M x y = st.M x y
Proof
:= by
  have hT : Inv (N + 2) (crossAct (n + 1) (cupAct n st.S)) := inv_cross (inv_cup hS (by omega)) (by omega)
  have cS : (cupL n st).S = cupAct n st.S := rfl
  rw [capL_M_nonself h, crossL_S, cS]
  have step : evalL (crossL (n + 1) s (cupL n st)).M (capP n (crossAct (n + 1) (cupAct n st.S)) x)
        (capP n (crossAct (n + 1) (cupAct n st.S)) y) =
      evalL (fun a c => evalL st.M (cupP n (swapAt (n + 1) a)) (cupP n (swapAt (n + 1) c)) +
          sgn s * crossE (n + 1) (crossAct (n + 1) (cupAct n st.S)) a c)
        (capP n (crossAct (n + 1) (cupAct n st.S)) x) (capP n (crossAct (n + 1) (cupAct n st.S)) y) := by
    apply evalL_congr
    intro a ha c hc
    have h2 := cap_respects hT (by omega) h a ha c hc
    rw [crossL_M_of (st := cupL n st) h2, cupL_M_nonself (st := st) (nonself_cross h2)]
    rfl
  rw [step, evalL_add, evalL_bind st.M (fun a => cupP n (swapAt (n + 1) a)), r1L_list, r1L_list, evalL_single,
    evalL_smul, evalL_crossE]
  have tn : crossAct (n + 1) (cupAct n st.S) n = n + 1 + 1 := by
    rw [crossAct_apply, swapAt_o (n := n + 1) (x := n) (by omega) (by omega), cupAct_l _ rfl, swapAt_l rfl]
  have hA : upAt n x ≠ n ∧ upAt n x ≠ n + 1 := by unfold upAt; split_ifs <;> omega
  have hC : upAt n y ≠ n ∧ upAt n y ≠ n + 1 := by unfold upAt; split_ifs <;> omega
  obtain ⟨ex, zx⟩ := r1L_edges hT.1 hT.2.1 (by omega) tn hA.1 hA.2
  obtain ⟨ey, zy⟩ := r1L_edges hT.1 hT.2.1 (by omega) tn hC.1 hC.2
  unfold capP
  rw [← ex, ← ey]
  by_cases tx : crossAct (n + 1) (cupAct n st.S) (upAt n x) = n ∨
      crossAct (n + 1) (cupAct n st.S) (upAt n x) = n + 1
  · by_cases ty : crossAct (n + 1) (cupAct n st.S) (upAt n y) = n ∨
        crossAct (n + 1) (cupAct n st.S) (upAt n y) = n + 1
    · exact (cap_touch_both hT h tx ty).elim
    · rw [zy ty]; grind
  · rw [zx tx]; grind
theorem linkR1Checks : LinkR1Checks
Proof
:= by
  apply checks_of_nonself
  intro b w st m hm hok hS hC hfit x y h
  cases m <;> simp only [famR1] at hm
  case r1R i s =>
    obtain ⟨f1, -⟩ := fits_cons hfit
    simp only [Gen.fits, decide_eq_true_eq] at f1
    change NonSelf (capAct (b + (i + 1)) (crossAct (b + i) (cupAct (b + (i + 1)) st.S))) x y at h
    show (capL (b + (i + 1)) (crossL (b + i) s (cupL (b + (i + 1)) st))).M x y = st.M x y
    rw [show b + (i + 1) = b + i + 1 by omega] at h ⊢
    exact r1R_link s st hS (by omega) x y h
  case r1L i s =>
    obtain ⟨f1, hfit⟩ := fits_cons hfit
    obtain ⟨f2, -⟩ := fits_cons hfit
    simp only [Gen.fits, Gen.widthAfter, decide_eq_true_eq] at f1 f2
    replace f2 := of_decide_eq_true f2
    change NonSelf (capAct (b + i) (crossAct (b + (i + 1)) (cupAct (b + i) st.S))) x y at h
    show (capL (b + i) (crossL (b + (i + 1)) s (cupL (b + i) st))).M x y = st.M x y
    rw [show b + (i + 1) = b + i + 1 by omega] at h ⊢
    exact r1L_link s st hS (by omega) x y h

Linking counts: far commutation

A far commutation exchanges two events on different strands. There are nine pairs of kinds of events.

Two events at a time

  • Link.act g: the action of the event g at an absolute point number (Link.genL_eq_act).
  • Link.gP g S x: the list of points below g for the point x above. Link.gC g S' x y: the crossing term of g.
  • Link.two_step: the table above two events is the table below over the composite lists, plus the term of the first event carried through the second, plus the term of the second event.
  • Link.cross_carry: through a far event, the term of a crossing is the term of the same crossing at its new position.

The list identity

  • Link.farShift L R: the event R, moved below or above L.
  • Link.FarList L R: the composite lists of points are equal in the two orders.
  • Link.linkFarChecks_of: the far check follows from FarList for each pair (Link.farR_data, Link.farL_data hold the positions).
Pair Theorem
Crossing and crossing, crossing and cup, cup and crossing, cup and cup Link.farListPair_00, Link.farListPair_01, Link.farListPair_10, Link.farListPair_11
Crossing and cap, cap and crossing Link.farListPair_02, Link.farListPair_20
Cup and cap, cap and cup Link.farListPair_12, Link.farListPair_21
Cap and cap Link.farListPair_22
  • Cap and cap is the longest proof. Both composite lists start with the end Z below both caps. The proof splits on S Z and on the partner of the leg that Z reaches.
namespace FormalizeKnitting.Equality.Link
open Conn
def act : Gen → St → St
  | .cross n lo => crossL n lo
  | .cup n => cupL n
  | .cap n => capL n

def gS : Gen → (Nat → Nat) → Nat → Nat
  | .cross n _ => crossAct n
  | .cup n => cupAct n
  | .cap n => capAct n

def gP : Gen → (Nat → Nat) → Nat → List Nat
  | .cross n _, _ => crossP n
  | .cup n, _ => cupP n
  | .cap n, S => capP n S

def gC : Gen → (Nat → Nat) → Nat → Nat → Int
  | .cross n lo, S' => fun x y => sgn lo * crossE n S' x y
  | _, _ => fun _ _ => 0

def gK : Gen → Nat → Nat
  | .cross .., k => k
  | .cup n, k => upAt n k
  | .cap n, k => downAt n k
theorem act_S (g : Gen) (st : St) : (act g st).S = gS g st.S
Proof
:= by cases g <;> rfl
theorem genL_eq_act (b : Nat) (g : Gen) (st : St) : genL b g st = act (g.shift b) st
Proof
:= by
  cases g <;> simp only [genL, act, Gen.shift, Nat.add_comm]
theorem inv_act {N : Nat} {S : Nat → Nat} (g : Gen) (hS : Inv N S) (hf : g.fits N = true) :
    Inv (g.widthAfter N) (gS g S)
Proof
:= by
  cases g with
  | cross n lo => simp only [Gen.fits, decide_eq_true_eq] at hf; exact inv_cross hS hf
  | cup n => simp only [Gen.fits, decide_eq_true_eq] at hf; exact inv_cup hS hf
  | cap n => simp only [Gen.fits, decide_eq_true_eq] at hf; exact inv_cap hS hf
theorem one_step (g : Gen) {st : St} {x y : Nat} (h : NonSelf (gS g st.S) x y) :
    (act g st).M x y = evalL st.M (gP g st.S x) (gP g st.S y) + gC g (gS g st.S) x y
Proof
:= by
  cases g with
  | cross n lo => exact crossL_M_nonself h
  | cup n => show (cupL n st).M x y = _; rw [cupL_M_nonself h]; simp [gC, gP]
  | cap n => show (capL n st).M x y = _; rw [capL_M_nonself h]; simp [gC, gP]
theorem respects {N : Nat} (g : Gen) {S : Nat → Nat} (hS : Inv N S) (hf : g.fits N = true) {x y : Nat}
    (h : NonSelf (gS g S) x y) : ∀ a ∈ gP g S x, ∀ c ∈ gP g S y, NonSelf S a c
Proof
:= by
  cases g with
  | cross n lo => exact cross_respects h
  | cup n => exact cup_respects h
  | cap n => simp only [Gen.fits, decide_eq_true_eq] at hf; exact cap_respects hS hf h
theorem two_step {N : Nat} (g h : Gen) {st : St} (hS : Inv N st.S) (hg : g.fits N = true)
    (hh : h.fits (g.widthAfter N) = true) {x y : Nat} (hn : NonSelf (gS h (gS g st.S)) x y) :
    (act h (act g st)).M x y =
      evalL st.M ((gP h (gS g st.S) x).flatMap (gP g st.S)) ((gP h (gS g st.S) y).flatMap (gP g st.S)) +
        evalL (gC g (gS g st.S)) (gP h (gS g st.S) x) (gP h (gS g st.S) y) + gC h (gS h (gS g st.S)) x y
Proof
:= by
  have e := act_S g st
  rw [one_step h (st := act g st) (e ▸ hn), e]
  have r := respects h (inv_act g hS hg) hh hn
  rw [evalL_congr (M' := fun a c => evalL st.M (gP g st.S a) (gP g st.S c) + gC g (gS g st.S) a c)
    (fun a ha c hc => one_step g (r a ha c hc)), evalL_add, evalL_bind]
def gIn : Gen → Nat → Prop
  | .cross n _, k => k = n ∨ k = n + 1
  | .cup _, _ => False
  | .cap n, k => k = n ∨ k = n + 1
theorem edge_carry {N : Nat} (h : Gen) {T : Nat → Nat} (hT : Inv N T) (hf : h.fits N = true) {k : Nat}
    (hk : ¬ gIn h k) (x : Nat) : vsum (edge k T) (gP h T x) = edge (gK h k) (gS h T) x
Proof
:= by
  cases h with
  | cross n lo =>
    simp only [gIn] at hk
    simp only [gP, gS, gK, crossP, vsum, List.map_cons, List.map_nil, List.sum_cons, List.sum_nil, add_zero]
    rw [edge_pull, swapAt_o (by omega) (by omega)]
  | cup n =>
    simp only [gP, gS, gK, cupP, vsum]
    unfold edge cupAct upAt downAt
    split_ifs <;> simp_all <;> omega
  | cap n =>
    simp only [gIn] at hk
    simp only [Gen.fits, decide_eq_true_eq] at hf
    obtain ⟨h1, h2, -⟩ := hT
    show vsum (edge k T) (capRow n T (upAt n x)) = edge (downAt n k) (capAct n T) x
    have hc : capAct n T x = downAt n (through n T (T (upAt n x))) := rfl
    have hA : upAt n x ≠ n ∧ upAt n x ≠ n + 1 := by unfold upAt; split_ifs <;> omega
    have dx : (x = downAt n k) ↔ (upAt n x = k) := by unfold upAt downAt; split_ifs <;> omega
    have dk : ∀ z, z ≠ n → z ≠ n + 1 → (downAt n z = downAt n k ↔ z = k) := by
      intro z hz1 hz2; unfold downAt; split_ifs <;> omega
    have hn0 : T n ≠ n := h2 n (by omega)
    have hn1 : T (n + 1) ≠ n + 1 := h2 (n + 1) hf
    have f1 : T (upAt n x) = n → T n = upAt n x := fun e => by have := h1 (upAt n x); rwa [e] at this
    have f2 : T (upAt n x) = n + 1 → T (n + 1) = upAt n x := fun e => by have := h1 (upAt n x); rwa [e] at this
    have f5 : T n = n + 1 → T (n + 1) = n := fun e => by have := h1 n; rwa [e] at this
    have f6 : T (n + 1) = n → T n = n + 1 := fun e => by have := h1 (n + 1); rwa [e] at this
    have hτ : through n T (T (upAt n x)) ≠ n ∧ through n T (T (upAt n x)) ≠ n + 1 := by
      unfold through; split_ifs <;> omega
    unfold edge
    rw [hc, if_congr dx rfl rfl, if_congr (dk _ hτ.1 hτ.2) rfl rfl]
    generalize upAt n x = A at *
    unfold through capRow vsum
    split_ifs <;> simp only [List.map_cons, List.map_nil, List.sum_cons, List.sum_nil] <;> split_ifs <;> omega
theorem evalL_zero (l1 l2 : List Nat) : evalL (fun _ _ => 0) l1 l2 = 0
Proof
:= by
  unfold evalL; simp
theorem cross_carry {N : Nat} (h : Gen) {T : Nat → Nat} (hT : Inv N T) (hf : h.fits N = true) (a : Nat) (lo : Bool)
    (ha : ¬ gIn h a) (ha1 : ¬ gIn h (a + 1)) (hs : gK h (a + 1) = gK h a + 1) (x y : Nat) :
    evalL (gC (.cross a lo) T) (gP h T x) (gP h T y) = gC (.cross (gK h a) lo) (gS h T) x y
Proof
:= by
  simp only [gC]
  rw [evalL_smul, evalL_crossE, edge_carry h hT hf ha, edge_carry h hT hf ha, edge_carry h hT hf ha1,
    edge_carry h hT hf ha1, hs]
  rfl
theorem far_generic {N : Nat} (L R R' : Gen) {st : St} (hS : Inv N st.S) (hL : L.fits N = true)
    (hR : R.fits (L.widthAfter N) = true) (hR' : R'.fits N = true) (hL' : L.fits (R'.widthAfter N) = true)
    (hSS : gS R (gS L st.S) = gS L (gS R' st.S))
    (hlist : ∀ x, (gP R (gS L st.S) x).flatMap (gP L st.S) = (gP L (gS R' st.S) x).flatMap (gP R' st.S))
    (hcL : ∀ x y, evalL (gC L (gS L st.S)) (gP R (gS L st.S) x) (gP R (gS L st.S) y) =
      gC L (gS R (gS L st.S)) x y)
    (hcR : ∀ x y, evalL (gC R' (gS R' st.S)) (gP L (gS R' st.S) x) (gP L (gS R' st.S) y) =
      gC R (gS R (gS L st.S)) x y)
    {x y : Nat} (hn : NonSelf (gS R (gS L st.S)) x y) : (act R (act L st)).M x y = (act L (act R' st)).M x y
Proof
:= by
  rw [two_step L R hS hL hR hn, two_step R' L hS hR' hL' (hSS ▸ hn), hlist, hlist, hcL, hcR, ← hSS]
  grind
def farShift (L R : Gen) : Gen := R.withLo (R.lo + L.outs - L.ins)

def FarList (L R : Gen) : Prop :=
  ∀ (N : Nat) (S : Nat → Nat), Inv N S → L.fits N = true → R.fits N = true → L.lo + L.ins ≤ R.lo → ∀ x,
    (gP (farShift L R) (gS L S) x).flatMap (gP L S) = (gP L (gS R S) x).flatMap (gP R S)

def gKind : Gen → Nat
  | .cross .. => 0
  | .cup _ => 1
  | .cap _ => 2

def FarListPair (kl kr : Nat) : Prop := ∀ L R : Gen, gKind L = kl → gKind R = kr → FarList L R
theorem genAct_eq (b : Nat) (g : Gen) : Conn.genAct b g = gS (g.shift b)
Proof
:= by
  cases g <;> simp only [Conn.genAct, gS, Gen.shift, Nat.add_comm]
theorem carryL {N : Nat} (L R' : Gen) {T : Nat → Nat} (hT : Inv N T) (hf : R'.fits N = true)
    (hp : L.lo + L.outs ≤ R'.lo) (x y : Nat) :
    evalL (gC L T) (gP R' T x) (gP R' T y) = gC L (gS R' T) x y
Proof
:= by
  cases L with
  | cross a lo =>
    simp only [Gen.lo, Gen.outs] at hp
    have h1 : ¬ gIn R' a := by cases R' <;> simp [gIn] at hp ⊢ <;> omega
    have h2 : ¬ gIn R' (a + 1) := by cases R' <;> simp [gIn] at hp ⊢ <;> omega
    have h3 : gK R' a = a := by cases R' <;> simp only [gK, upAt, downAt] at hp ⊢ <;> first | omega | (split_ifs <;> omega)
    have h4 : gK R' (a + 1) = gK R' a + 1 := by
      rw [h3]; cases R' <;> simp only [gK, upAt, downAt] at hp ⊢ <;> first | omega | (split_ifs <;> omega)
    rw [cross_carry R' hT hf a lo h1 h2 h4, h3]
  | cup a => exact evalL_zero _ _
  | cap a => exact evalL_zero _ _
theorem carryR {N : Nat} (L R : Gen) {T : Nat → Nat} (hT : Inv N T) (hf : L.fits N = true)
    (hp : L.lo + L.ins ≤ R.lo) (x y : Nat) :
    evalL (gC R T) (gP L T x) (gP L T y) = gC (farShift L R) (gS L T) x y
Proof
:= by
  cases R with
  | cross c lo =>
    simp only [Gen.lo] at hp
    have h1 : ¬ gIn L c := by cases L <;> simp [gIn, Gen.ins] at hp ⊢ <;> omega
    have h2 : ¬ gIn L (c + 1) := by cases L <;> simp [gIn, Gen.ins] at hp ⊢ <;> omega
    have h3 : gK L c = c + L.outs - L.ins := by
      cases L <;> simp only [gK, Gen.ins, Gen.outs, upAt, downAt] at hp ⊢ <;> first | omega | (split_ifs <;> omega)
    have h4 : gK L (c + 1) = gK L c + 1 := by
      rw [h3]; cases L <;> simp only [gK, Gen.ins, Gen.outs, upAt, downAt] at hp ⊢ <;> first | omega | (split_ifs <;> omega)
    rw [cross_carry L hT hf c lo h1 h2 h4, h3]
    rfl
  | cup c => exact evalL_zero _ _
  | cap c => exact evalL_zero _ _
theorem far_of_list {N : Nat} (L R : Gen) (hl : FarList L R) {st : St} (hS : Inv N st.S)
    (hL : L.fits N = true) (hR : R.fits N = true) (hR2 : (farShift L R).fits (L.widthAfter N) = true)
    (hL2 : L.fits (R.widthAfter N) = true) (hp : L.lo + L.ins ≤ R.lo) (hp2 : L.lo + L.outs ≤ (farShift L R).lo)
    (hSS : gS (farShift L R) (gS L st.S) = gS L (gS R st.S)) {x y : Nat}
    (hn : NonSelf (gS (farShift L R) (gS L st.S)) x y) :
    (act (farShift L R) (act L st)).M x y = (act L (act R st)).M x y
Proof
:=
  far_generic L (farShift L R) R hS hL hR2 hR hL2 hSS (hl N st.S hS hL hR hp)
    (carryL L _ (inv_act L hS hL) hR2 hp2) (fun x y => hSS ▸ carryR L R (inv_act R hS hR) hL2 hp x y) hn
theorem farR_data (b w : Nat) (g h : Gen) (hok : g.lo + g.outs ≤ h.lo) (hg : g.fits w = true)
    (hh : h.fits (g.widthAfter w) = true) :
    (g.shift b).fits (b + w) = true ∧ ((h.withLo (h.lo - g.outs + g.ins)).shift b).fits (b + w) = true ∧
      (farShift (g.shift b) ((h.withLo (h.lo - g.outs + g.ins)).shift b)).fits ((g.shift b).widthAfter (b + w)) = true ∧
      (g.shift b).fits (((h.withLo (h.lo - g.outs + g.ins)).shift b).widthAfter (b + w)) = true ∧
      (g.shift b).lo + (g.shift b).ins ≤ ((h.withLo (h.lo - g.outs + g.ins)).shift b).lo ∧
      (g.shift b).lo + (g.shift b).outs ≤ (farShift (g.shift b) ((h.withLo (h.lo - g.outs + g.ins)).shift b)).lo ∧
      farShift (g.shift b) ((h.withLo (h.lo - g.outs + g.ins)).shift b) = h.shift b
Proof
:= by
  cases g <;> cases h <;> simp only [Gen.fits, Gen.widthAfter] at hg hh <;>
    replace hg := of_decide_eq_true hg <;> replace hh := of_decide_eq_true hh <;>
    simp only [farShift, Gen.fits, Gen.widthAfter, Gen.shift, Gen.withLo, Gen.lo, Gen.ins, Gen.outs,
      decide_eq_true_eq, Gen.cross.injEq, Gen.cup.injEq, Gen.cap.injEq] at hok ⊢ <;>
    refine ⟨?_, ?_, ?_, ?_, ?_, ?_, ?_⟩ <;> (try simp only [and_true]) <;> omega
theorem farL_data (b w : Nat) (g h : Gen) (hok : h.lo + h.ins ≤ g.lo) (hg : g.fits w = true)
    (hh : h.fits (g.widthAfter w) = true) :
    (h.shift b).fits (b + w) = true ∧ (g.shift b).fits (b + w) = true ∧
      (farShift (h.shift b) (g.shift b)).fits ((h.shift b).widthAfter (b + w)) = true ∧
      (h.shift b).fits ((g.shift b).widthAfter (b + w)) = true ∧
      (h.shift b).lo + (h.shift b).ins ≤ (g.shift b).lo ∧
      (h.shift b).lo + (h.shift b).outs ≤ (farShift (h.shift b) (g.shift b)).lo ∧
      farShift (h.shift b) (g.shift b) = (g.withLo (g.lo + h.outs - h.ins)).shift b
Proof
:= by
  cases g <;> cases h <;> simp only [Gen.fits, Gen.widthAfter] at hg hh <;>
    replace hg := of_decide_eq_true hg <;> replace hh := of_decide_eq_true hh <;>
    simp only [farShift, Gen.fits, Gen.widthAfter, Gen.shift, Gen.withLo, Gen.lo, Gen.ins, Gen.outs,
      decide_eq_true_eq, Gen.cross.injEq, Gen.cup.injEq, Gen.cap.injEq] at hok ⊢ <;>
    refine ⟨?_, ?_, ?_, ?_, ?_, ?_, ?_⟩ <;> (try simp only [and_true]) <;> omega
theorem linkFarChecks_of (hl : ∀ L R : Gen, FarList L R) : LinkFarChecks
Proof
:= by
  apply checks_of_nonself
  intro b w st m hm hok hS hC hfit x y hn
  have eS : (run b st m.lhs).S = (run b st m.rhs).S := by
    rw [run_S, run_S]; exact Conn.run_move b w st.S m hok hS hfit
  cases m <;> simp only [famFar] at hm
  case farR g h =>
    obtain ⟨f1, hfit⟩ := fits_cons hfit
    obtain ⟨f2, -⟩ := fits_cons hfit
    simp only [Mv.ok, decide_eq_true_eq] at hok
    obtain ⟨d1, d2, d3, d4, d5, d6, d7⟩ := farR_data b w g h hok f1 f2
    simp only [Mv.lhs, Mv.rhs, run, genL_eq_act, act_S] at hn eS ⊢
    rw [← d7] at hn eS ⊢
    have hn' : NonSelf (gS (farShift (g.shift b) ((h.withLo (h.lo - g.outs + g.ins)).shift b)) (gS (g.shift b) st.S)) x y := by
      simpa only [act_S] using hn
    have eS' : gS (farShift (g.shift b) ((h.withLo (h.lo - g.outs + g.ins)).shift b)) (gS (g.shift b) st.S) =
        gS (g.shift b) (gS ((h.withLo (h.lo - g.outs + g.ins)).shift b) st.S) := by
      simpa only [act_S] using eS
    exact far_of_list _ _ (hl _ _) hS d1 d2 d3 d4 d5 d6 eS' hn'
  case farL g h =>
    obtain ⟨f1, hfit⟩ := fits_cons hfit
    obtain ⟨f2, -⟩ := fits_cons hfit
    simp only [Mv.ok, decide_eq_true_eq] at hok
    obtain ⟨d1, d2, d3, d4, d5, d6, d7⟩ := farL_data b w g h hok f1 f2
    simp only [Mv.lhs, Mv.rhs, run, genL_eq_act, act_S] at hn eS ⊢
    rw [← d7] at eS ⊢
    have eS' : gS (farShift (h.shift b) (g.shift b)) (gS (h.shift b) st.S) =
        gS (h.shift b) (gS (g.shift b) st.S) := by
      simpa only [act_S] using eS.symm
    have hn' : NonSelf (gS (farShift (h.shift b) (g.shift b)) (gS (h.shift b) st.S)) x y := by
      rw [eS']; simpa only [act_S] using hn
    exact (far_of_list _ _ (hl _ _) hS d1 d2 d3 d4 d5 d6 eS' hn').symm
macro "far_easy" : tactic => `(tactic| (
  intro N S _ _ _ hp x
  simp only [farShift, gP, gS, Gen.withLo, Gen.lo, Gen.ins, Gen.outs, Nat.add_sub_cancel, Nat.add_zero,
    Nat.sub_zero, crossP, cupP] at hp ⊢
  (try split_ifs) <;> simp only [List.flatMap_cons, List.flatMap_nil, List.append_nil, List.nil_append,
    crossP, cupP] <;> (try simp only [swapAt, downAt]) <;> split_ifs <;>
    (try simp only [List.cons.injEq, and_true, List.nil_eq, reduceCtorEq]) <;> (try rfl) <;> omega))
theorem farList_cross_cross (a c : Nat) (lo lo' : Bool) : FarList (.cross a lo) (.cross c lo')
Proof
:= by far_easy
theorem farList_cross_cup (a c : Nat) (lo : Bool) : FarList (.cross a lo) (.cup c)
Proof
:= by far_easy
theorem farList_cup_cross (a c : Nat) (lo : Bool) : FarList (.cup a) (.cross c lo)
Proof
:= by far_easy
theorem farList_cup_cup (a c : Nat) : FarList (.cup a) (.cup c)
Proof
:= by far_easy
theorem farListPair_00 : FarListPair 0 0
Proof
:= by
  intro L R hL hR; cases L <;> cases R <;> simp only [gKind] at hL hR <;> first | omega | exact farList_cross_cross _ _ _ _
theorem farListPair_01 : FarListPair 0 1
Proof
:= by
  intro L R hL hR; cases L <;> cases R <;> simp only [gKind] at hL hR <;> first | omega | exact farList_cross_cup _ _ _
theorem farListPair_10 : FarListPair 1 0
Proof
:= by
  intro L R hL hR; cases L <;> cases R <;> simp only [gKind] at hL hR <;> first | omega | exact farList_cup_cross _ _ _
theorem farListPair_11 : FarListPair 1 1
Proof
:= by
  intro L R hL hR; cases L <;> cases R <;> simp only [gKind] at hL hR <;> first | omega | exact farList_cup_cup _ _
theorem farList_cross_cap (a c : Nat) (lo : Bool) : FarList (.cross a lo) (.cap c)
Proof
:= by
  intro N S _ _ _ hp x
  simp only [farShift, gP, gS, Gen.withLo, Gen.lo, Gen.ins, Gen.outs, Nat.add_sub_cancel, crossP, capP] at hp ⊢
  have hc : swapAt a (upAt c x) = upAt c (swapAt a x) := by unfold swapAt upAt; split_ifs <;> omega
  have e : crossAct a S (upAt c x) = swapAt a (S (upAt c (swapAt a x))) := by simp only [crossAct, hc]
  have s0 : swapAt a c = c := swapAt_o (by omega) (by omega)
  have s1 : swapAt a (c + 1) = c + 1 := swapAt_o (by omega) (by omega)
  unfold capRow
  rw [e, List.flatMap_cons]
  simp only [crossP, hc, List.flatMap_cons, List.flatMap_nil, List.append_nil, capP, capRow,
    List.cons_append, List.cons.injEq, true_and]
  generalize S (upAt c (swapAt a x)) = t
  have i0 : swapAt a t = c ↔ t = c := by rw [swapAt_eq_iff, s0]
  have i1 : swapAt a t = c + 1 ↔ t = c + 1 := by rw [swapAt_eq_iff, s1]
  by_cases h0 : t = c
  · rw [ite_eq_left (i0.mpr h0), ite_eq_left h0]; simp [crossP, s1]
  · rw [ite_eq_right (mt i0.mp h0), ite_eq_right h0]
    by_cases h1 : t = c + 1
    · rw [ite_eq_left (i1.mpr h1), ite_eq_left h1]; simp [crossP, s0]
    · rw [ite_eq_right (mt i1.mp h1), ite_eq_right h1]; rfl
theorem farList_cap_cross (a c : Nat) (lo : Bool) : FarList (.cap a) (.cross c lo)
Proof
:= by
  intro N S _ _ _ hp x
  simp only [farShift, gP, gS, Gen.withLo, Gen.lo, Gen.ins, Gen.outs, Nat.add_zero, crossP, capP] at hp ⊢
  have hc : swapAt c (upAt a x) = upAt a (swapAt (c - 2) x) := by unfold swapAt upAt; split_ifs <;> omega
  have e : crossAct c S (upAt a x) = swapAt c (S (upAt a (swapAt (c - 2) x))) := by simp only [crossAct, hc]
  have s0 : swapAt c a = a := swapAt_o (by omega) (by omega)
  have s1 : swapAt c (a + 1) = a + 1 := swapAt_o (by omega) (by omega)
  unfold capRow
  rw [e, List.flatMap_cons]
  simp only [crossP, hc, List.flatMap_cons, List.flatMap_nil, List.append_nil, capP, capRow,
    List.cons_append, List.cons.injEq, true_and]
  generalize S (upAt a (swapAt (c - 2) x)) = t
  have i0 : swapAt c t = a ↔ t = a := by rw [swapAt_eq_iff, s0]
  have i1 : swapAt c t = a + 1 ↔ t = a + 1 := by rw [swapAt_eq_iff, s1]
  by_cases h0 : t = a
  · rw [ite_eq_left (i0.mpr h0), ite_eq_left h0]; simp [crossP, s1]
  · rw [ite_eq_right (mt i0.mp h0), ite_eq_right h0]
    by_cases h1 : t = a + 1
    · rw [ite_eq_left (i1.mpr h1), ite_eq_left h1]; simp [crossP, s0]
    · rw [ite_eq_right (mt i1.mp h1), ite_eq_right h1]; rfl
theorem farListPair_02 : FarListPair 0 2
Proof
:= by
  intro L R hL hR; cases L <;> cases R <;> simp only [gKind] at hL hR <;> first | omega | exact farList_cross_cap _ _ _
theorem farListPair_20 : FarListPair 2 0
Proof
:= by
  intro L R hL hR; cases L <;> cases R <;> simp only [gKind] at hL hR <;> first | omega | exact farList_cap_cross _ _ _
theorem farList_cup_cap (a c : Nat) : FarList (.cup a) (.cap c)
Proof
:= by
  intro N S _ _ _ hp x
  simp only [farShift, gP, gS, Gen.withLo, Gen.lo, Gen.ins, Gen.outs, Nat.add_zero, Nat.sub_zero] at hp ⊢
  have g1 : cupP a (c + 2 + 1) = [c + 1] := by
    unfold cupP downAt; split_ifs <;> (try simp only [List.cons.injEq, and_true]) <;> (try rfl) <;> omega
  have g2 : cupP a (c + 2) = [c] := by
    unfold cupP downAt; split_ifs <;> (try simp only [List.cons.injEq, and_true]) <;> (try rfl) <;> omega
  by_cases hx : x = a ∨ x = a + 1
  · have u : upAt (c + 2) x = x := upAt_lt (by omega)
    have v : cupAct a S x = a + 1 ∨ cupAct a S x = a := by
      rcases hx with e | e
      · exact Or.inl (cupAct_l _ e)
      · exact Or.inr (cupAct_r _ e)
    simp only [capP, capRow, u, cupP, ite_eq_left hx, List.flatMap_nil]
    rw [ite_eq_right (by omega), ite_eq_right (by omega)]
    simp [cupP, hx]
  · have hA : upAt (c + 2) x ≠ a ∧ upAt (c + 2) x ≠ a + 1 := by unfold upAt; split_ifs <;> omega
    have hd : downAt a (upAt (c + 2) x) = upAt c (downAt a x) := by unfold upAt downAt; split_ifs <;> omega
    have e : cupAct a S (upAt (c + 2) x) = upAt a (S (upAt c (downAt a x))) := by
      rw [cupAct_o _ hA.1 hA.2, hd]
    have g0 : cupP a (upAt (c + 2) x) = [upAt c (downAt a x)] := by
      rw [cupP, ite_eq_right (by omega), hd]
    simp only [capP, capRow, e, cupP, ite_eq_right hx, List.flatMap_cons, List.flatMap_nil, List.append_nil]
    rw [← cupP, g0, List.singleton_append]
    congr 1
    generalize S (upAt c (downAt a x)) = t
    have i0 : upAt a t = c + 2 ↔ t = c := by unfold upAt; split_ifs <;> omega
    have i1 : upAt a t = c + 2 + 1 ↔ t = c + 1 := by unfold upAt; split_ifs <;> omega
    by_cases h0 : t = c
    · rw [ite_eq_left (i0.mpr h0), ite_eq_left h0, List.flatMap_cons, g1]; rfl
    · rw [ite_eq_right (mt i0.mp h0), ite_eq_right h0]
      by_cases h1 : t = c + 1
      · rw [ite_eq_left (i1.mpr h1), ite_eq_left h1, List.flatMap_cons, g2]; rfl
      · rw [ite_eq_right (mt i1.mp h1), ite_eq_right h1]; rfl
theorem farListPair_12 : FarListPair 1 2
Proof
:= by
  intro L R hL hR; cases L <;> cases R <;> simp only [gKind] at hL hR <;> first | omega | exact farList_cup_cap _ _
theorem farList_cap_cup (a c : Nat) : FarList (.cap a) (.cup c)
Proof
:= by
  intro N S _ _ _ hp x
  simp only [farShift, gP, gS, Gen.withLo, Gen.lo, Gen.ins, Gen.outs, Nat.add_zero] at hp ⊢
  have g1 : cupP c (a + 1) = [a + 1] := by
    unfold cupP downAt; split_ifs <;> (try simp only [List.cons.injEq, and_true]) <;> omega
  have g2 : cupP c a = [a] := by
    unfold cupP downAt; split_ifs <;> (try simp only [List.cons.injEq, and_true]) <;> omega
  by_cases hx : x = c - 2 ∨ x = c - 2 + 1
  · have u : upAt a x = x + 2 := upAt_ge (by omega)
    have v : cupAct c S (x + 2) = c + 1 ∨ cupAct c S (x + 2) = c := by
      rcases hx with e | e
      · exact Or.inl (cupAct_l _ (by omega))
      · exact Or.inr (cupAct_r _ (by omega))
    simp only [capP, capRow, u, cupP, ite_eq_left hx, List.flatMap_nil]
    rw [ite_eq_right (by omega), ite_eq_right (by omega)]
    simp only [List.flatMap_cons, List.flatMap_nil, List.append_nil]
    rw [cupP, ite_eq_left (by omega)]
  · have hA : upAt a x ≠ c ∧ upAt a x ≠ c + 1 := by unfold upAt; split_ifs <;> omega
    have hd : downAt c (upAt a x) = upAt a (downAt (c - 2) x) := by unfold upAt downAt; split_ifs <;> omega
    have e : cupAct c S (upAt a x) = upAt c (S (upAt a (downAt (c - 2) x))) := by
      rw [cupAct_o _ hA.1 hA.2, hd]
    have g0 : cupP c (upAt a x) = [upAt a (downAt (c - 2) x)] := by
      rw [cupP, ite_eq_right (by omega), hd]
    simp only [capP, capRow, e, cupP, ite_eq_right hx, List.flatMap_cons, List.flatMap_nil, List.append_nil]
    rw [← cupP, g0, List.singleton_append]
    congr 1
    generalize S (upAt a (downAt (c - 2) x)) = t
    have i0 : upAt c t = a ↔ t = a := by unfold upAt; split_ifs <;> omega
    have i1 : upAt c t = a + 1 ↔ t = a + 1 := by unfold upAt; split_ifs <;> omega
    by_cases h0 : t = a
    · rw [ite_eq_left (i0.mpr h0), ite_eq_left h0, List.flatMap_cons, g1]; rfl
    · rw [ite_eq_right (mt i0.mp h0), ite_eq_right h0]
      by_cases h1 : t = a + 1
      · rw [ite_eq_left (i1.mpr h1), ite_eq_left h1, List.flatMap_cons, g2]; rfl
      · rw [ite_eq_right (mt i1.mp h1), ite_eq_right h1]; rfl
theorem farListPair_21 : FarListPair 2 1
Proof
:= by
  intro L R hL hR; cases L <;> cases R <;> simp only [gKind] at hL hR <;> first | omega | exact farList_cap_cup _ _
theorem inv_eq {S : Nat → Nat} (h1 : ∀ x, S (S x) = x) {p q : Nat} (h : S p = q) : S q = p
Proof
:= by
  rw [← h, h1]
theorem inv_ne {S : Nat → Nat} (h1 : ∀ x, S (S x) = x) {p q r : Nat} (h : S p = q) (hr : r ≠ p) : S r ≠ q
Proof
:=
  fun e => hr (by rw [← h1 r, e, ← h, h1])
theorem farList_cap_cap (a c : Nat) : FarList (.cap a) (.cap c)
Proof
:= by
  intro N S hS _ hR hp x
  obtain ⟨h1, h2, -⟩ := hS
  simp only [Gen.fits, decide_eq_true_eq] at hR
  simp only [farShift, gP, gS, Gen.withLo, Gen.lo, Gen.ins, Gen.outs, Nat.add_zero] at hp ⊢
  have hz : upAt c (upAt a x) = upAt a (upAt (c - 2) x) := by unfold upAt; split_ifs <;> omega
  have hZ : upAt a (upAt (c - 2) x) ≠ a ∧ upAt a (upAt (c - 2) x) ≠ a + 1 ∧
      upAt a (upAt (c - 2) x) ≠ c ∧ upAt a (upAt (c - 2) x) ≠ c + 1 := by unfold upAt; split_ifs <;> omega
  have e1 : capAct a S (upAt (c - 2) x) = downAt a (through a S (S (upAt a (upAt (c - 2) x)))) := rfl
  have e2 : capAct c S (upAt a x) = downAt c (through c S (S (upAt a (upAt (c - 2) x)))) := by
    show downAt c (through c S (S (upAt c (upAt a x)))) = _; rw [hz]
  have u1 : upAt a (c - 2 + 1) = c + 1 := by rw [upAt_ge (by omega)]; omega
  have u2 : upAt a (c - 2) = c := by rw [upAt_ge (by omega)]; omega
  have u3 : upAt c (a + 1) = a + 1 := upAt_lt (by omega)
  have u4 : upAt c a = a := upAt_lt (by omega)
  unfold capP
  simp only [capRow, e1, e2, List.flatMap_cons, hz]
  generalize upAt a (upAt (c - 2) x) = Z at *
  clear hz e1 e2
  obtain ⟨z0, z1, z2, z3⟩ := hZ
  have n0 : S a ≠ a := h2 a (by omega)
  have n1 : S (a + 1) ≠ a + 1 := h2 (a + 1) (by omega)
  have n2 : S c ≠ c := h2 c (by omega)
  have n3 : S (c + 1) ≠ c + 1 := h2 (c + 1) hR
  clear h2
  have d1 : a ≠ c := by omega
  have d2 : a ≠ c + 1 := by omega
  have d3 : a + 1 ≠ c := by omega
  have d4 : a + 1 ≠ c + 1 := by omega
  have d5 : c ≠ a := by omega
  have d6 : c ≠ a + 1 := by omega
  have d7 : c + 1 ≠ a := by omega
  have d8 : c + 1 ≠ a + 1 := by omega
  have dA : ∀ T, T ≠ a → T ≠ a + 1 → (downAt a T = c - 2 ↔ T = c) ∧ (downAt a T = c - 2 + 1 ↔ T = c + 1) := by
    intro T h3 h4; unfold downAt; constructor <;> split_ifs <;> omega
  have dC : ∀ T, T ≠ c → T ≠ c + 1 → (downAt c T = a ↔ T = a) ∧ (downAt c T = a + 1 ↔ T = a + 1) := by
    intro T h3 h4; unfold downAt; constructor <;> split_ifs <;> omega
  have hT : through a S (S Z) ≠ a ∧ through a S (S Z) ≠ a + 1 := by
    unfold through
    by_cases k : S Z = a
    · simp only [k, ↓reduceIte]; exact ⟨inv_ne h1 k (Ne.symm z1), n1⟩
    · simp only [k, ↓reduceIte]
      by_cases k' : S Z = a + 1
      · simp only [k', ↓reduceIte]; exact ⟨n0, inv_ne h1 k' (Ne.symm z0)⟩
      · simp only [k', ↓reduceIte]; exact ⟨k, k'⟩
  have hU : through c S (S Z) ≠ c ∧ through c S (S Z) ≠ c + 1 := by
    unfold through
    by_cases k : S Z = c
    · simp only [k, ↓reduceIte]; exact ⟨inv_ne h1 k (Ne.symm z3), n3⟩
    · simp only [k, ↓reduceIte]
      by_cases k' : S Z = c + 1
      · simp only [k', ↓reduceIte]; exact ⟨n2, inv_ne h1 k' (Ne.symm z2)⟩
      · simp only [k', ↓reduceIte]; exact ⟨k, k'⟩
  rw [if_congr (dA _ hT.1 hT.2).1 rfl rfl, if_congr (dA _ hT.1 hT.2).2 rfl rfl,
    if_congr (dC _ hU.1 hU.2).1 rfl rfl, if_congr (dC _ hU.1 hU.2).2 rfl rfl]
  clear dA dC hT hU
  unfold through
  have c0 : c ≠ c + 1 := by omega
  have c1 : a ≠ a + 1 := by omega
  by_cases k : S Z = a
  · have f0 : S (a + 1) ≠ a := inv_ne h1 k (Ne.symm z1)
    simp only [k, List.flatMap_cons, List.flatMap_nil, List.append_nil, u3, List.cons_append, List.nil_append, d1, d2, ite_true, ite_false]
    by_cases k1 : S (a + 1) = c
    · have f1 : S (c + 1) ≠ a := inv_ne h1 k (Ne.symm z3)
      have f2 : S (c + 1) ≠ a + 1 := inv_ne h1 (inv_eq h1 k1) (Ne.symm c0)
      simp only [k1, f1, f2, List.flatMap_cons, List.flatMap_nil, List.append_nil, u1, ite_true, ite_false]
    · by_cases k2 : S (a + 1) = c + 1
      · have f1 : S c ≠ a := inv_ne h1 k (Ne.symm z2)
        have f2 : S c ≠ a + 1 := inv_ne h1 (inv_eq h1 k2) c0
        simp only [k2, f1, f2, List.flatMap_cons, List.flatMap_nil, List.append_nil, u2, ite_true, ite_false, Nat.succ_ne_self]
      · simp only [k1, k2, List.flatMap_nil, ite_false]
  by_cases k' : S Z = a + 1
  · have f0 : S a ≠ a + 1 := inv_ne h1 k' (Ne.symm z0)
    simp only [k', List.flatMap_cons, List.flatMap_nil, List.append_nil, u4, List.cons_append, List.nil_append, d3, d4, ite_true, ite_false, Nat.succ_ne_self]
    by_cases k1 : S a = c
    · have f1 : S (c + 1) ≠ a + 1 := inv_ne h1 k' (Ne.symm z3)
      have f2 : S (c + 1) ≠ a := inv_ne h1 (inv_eq h1 k1) (Ne.symm c0)
      simp only [k1, f1, f2, List.flatMap_cons, List.flatMap_nil, List.append_nil, u1, ite_true, ite_false]
    · by_cases k2 : S a = c + 1
      · have f1 : S c ≠ a + 1 := inv_ne h1 k' (Ne.symm z2)
        have f2 : S c ≠ a := inv_ne h1 (inv_eq h1 k2) c0
        simp only [k2, f1, f2, List.flatMap_cons, List.flatMap_nil, List.append_nil, u2, ite_true, ite_false, Nat.succ_ne_self]
      · simp only [k1, k2, List.flatMap_nil, ite_false]
  by_cases m : S Z = c
  · have f0 : S (c + 1) ≠ c := inv_ne h1 m (Ne.symm z3)
    simp only [m, List.flatMap_cons, List.flatMap_nil, List.append_nil, u1, List.cons_append, List.nil_append, d5, d6, ite_true, ite_false]
    by_cases k1 : S (c + 1) = a
    · have f1 : S (a + 1) ≠ c := inv_ne h1 m (Ne.symm z1)
      have f2 : S (a + 1) ≠ c + 1 := inv_ne h1 (inv_eq h1 k1) (Ne.symm c1)
      simp only [k1, f1, f2, List.flatMap_cons, List.flatMap_nil, List.append_nil, u3, ite_true, ite_false]
    · by_cases k2 : S (c + 1) = a + 1
      · have f1 : S a ≠ c := inv_ne h1 m (Ne.symm z0)
        have f2 : S a ≠ c + 1 := inv_ne h1 (inv_eq h1 k2) c1
        simp only [k2, f1, f2, List.flatMap_cons, List.flatMap_nil, List.append_nil, u4, ite_true, ite_false, Nat.succ_ne_self]
      · simp only [k1, k2, List.flatMap_nil, ite_false]
  by_cases m' : S Z = c + 1
  · have f0 : S c ≠ c + 1 := inv_ne h1 m' (Ne.symm z2)
    simp only [m', List.flatMap_cons, List.flatMap_nil, List.append_nil, u2, List.cons_append, List.nil_append, d7, d8, ite_true, ite_false, Nat.succ_ne_self]
    by_cases k1 : S c = a
    · have f1 : S (a + 1) ≠ c + 1 := inv_ne h1 m' (Ne.symm z1)
      have f2 : S (a + 1) ≠ c := inv_ne h1 (inv_eq h1 k1) (Ne.symm c1)
      simp only [k1, f1, f2, List.flatMap_cons, List.flatMap_nil, List.append_nil, u3, ite_true, ite_false]
    · by_cases k2 : S c = a + 1
      · have f1 : S a ≠ c + 1 := inv_ne h1 m' (Ne.symm z0)
        have f2 : S a ≠ c := inv_ne h1 (inv_eq h1 k2) c1
        simp only [k2, f1, f2, List.flatMap_cons, List.flatMap_nil, List.append_nil, u4, ite_true, ite_false, Nat.succ_ne_self]
      · simp only [k1, k2, List.flatMap_nil, ite_false]
  simp only [k, k', m, m', List.flatMap_nil, List.append_nil, ite_false]
theorem farListPair_22 : FarListPair 2 2
Proof
:= by
  intro L R hL hR; cases L <;> cases R <;> simp only [gKind] at hL hR <;> first | omega | exact farList_cap_cap _ _

Linking counts are an invariant of Iso

Theorem Statement
Link.run_move A move whose left side fits sends each clean state with Inv to the same state as its right side
link_step1 One move keeps the last state of the fold
linkState_iso Iso b u v → the last states of the fold are equal
linkCount_iso Iso b u v → linkCount b u p q = linkCount b v p q
linkList_iso Iso b u v → linkList b t u = linkList b t v
not_iso_of_linkList_ne Different lists of linking counts: not Iso
  • Link.run_move takes the check of each move family: far commutation (linkFarChecks_of, Link.farList), zig-zag, cup slide, cap slide, R1 on a strand, R1 at a cup, R1 at a cap, R2, R3.
  • link_step1: the state after the part below the move has Inv and is clean. The move sends it to the same state. The part above the move runs from equal states (Link.run_append).
  • No hypothesis is left. The checks of all 14 moves are proved.
namespace FormalizeKnitting.Equality
namespace Link
open Conn
theorem farList (L R : Gen) : FarList L R
Proof
:= by
  cases L <;> cases R
  · exact farListPair_00 _ _ rfl rfl
  · exact farListPair_01 _ _ rfl rfl
  · exact farListPair_02 _ _ rfl rfl
  · exact farListPair_10 _ _ rfl rfl
  · exact farListPair_11 _ _ rfl rfl
  · exact farListPair_12 _ _ rfl rfl
  · exact farListPair_20 _ _ rfl rfl
  · exact farListPair_21 _ _ rfl rfl
  · exact farListPair_22 _ _ rfl rfl
theorem run_move (b w : Nat) (st : St) (m : Mv) (hok : m.ok = true) (hS : Inv (b + w) st.S) (hC : Clean st)
    (hfit : (topWidth w m.lhs).isSome = true) : run b st m.lhs = run b st m.rhs
Proof
:= by
  cases m with
  | farR g h' => exact linkFarChecks_of farList b w st _ trivial hok hS hC hfit
  | farL g h' => exact linkFarChecks_of farList b w st _ trivial hok hS hC hfit
  | zigR i => exact linkZigChecks b w st _ trivial hok hS hC hfit
  | zigL i => exact linkZigChecks b w st _ trivial hok hS hC hfit
  | cupSlideR i s => exact linkCupSlideChecks b w st _ trivial hok hS hC hfit
  | cupSlideL i s => exact linkCupSlideChecks b w st _ trivial hok hS hC hfit
  | capSlideR i s => exact linkCapSlideChecks b w st _ trivial hok hS hC hfit
  | capSlideL i s => exact linkCapSlideChecks b w st _ trivial hok hS hC hfit
  | r1R i s => exact linkR1Checks b w st _ trivial hok hS hC hfit
  | r1L i s => exact linkR1Checks b w st _ trivial hok hS hC hfit
  | r1cup i s => exact linkR1CupChecks b w st _ trivial hok hS hC hfit
  | r1cap i s => exact linkR1CapChecks b w st _ trivial hok hS hC hfit
  | r2 i s => exact linkR2Checks b w st _ trivial hok hS hC hfit
  | r3 i s => exact linkR3Checks b w st _ trivial hok hS hC hfit
open Link
theorem link_step1 {b : Nat} {u v : MWord} (hs : Step1 b u v) : run b (init b) u = run b (init b) v
Proof
:= by
  obtain ⟨hu, -, p, q, m, hok, rfl, rfl⟩ := hs
  rw [Link.run_append, Link.run_append, Link.run_append, Link.run_append]
  rw [topWidth_append, topWidth_append] at hu
  cases hp : topWidth b p with
  | none => simp [hp] at hu
  | some w =>
    rw [hp] at hu
    cases hl : topWidth w m.lhs with
    | none => simp [hl] at hu
    | some w₂ =>
      have hS : Conn.Inv (b + w) (run b (init b) p).S := by
        rw [run_S]; exact Conn.inv_run b p b w (Conn.start b) (Conn.inv_start b) hp
      rw [Link.run_move b w _ m hok hS (run_clean b _ (init_clean b) p) (by simp [hl])]
theorem linkState_iso {b : Nat} {u v : MWord} (hi : Iso b u v) : run b (init b) u = run b (init b) v
Proof
:= by
  induction hi with
  | rel _ _ hs => exact link_step1 hs
  | refl => rfl
  | symm _ _ _ ih => exact ih.symm
  | trans _ _ _ _ _ ih₁ ih₂ => exact ih₁.trans ih₂
theorem linkCount_iso {b : Nat} {u v : MWord} (hi : Iso b u v) (p q : Nat) :
    linkCount b u p q = linkCount b v p q
Proof
:= by
  unfold linkCount
  rw [linkState_iso hi]
def linkList (b t : Nat) (u : MWord) : List Int :=
  (List.range (b + t)).flatMap fun p => (List.range (b + t)).map (linkCount b u p)
theorem linkList_iso {b t : Nat} {u v : MWord} (hi : Iso b u v) : linkList b t u = linkList b t v
Proof
:= by
  have h : linkCount b u = linkCount b v := funext fun p => funext fun q => linkCount_iso hi p q
  simp only [linkList, h]
theorem not_iso_of_linkList_ne {b t : Nat} {u v : MWord} (h : linkList b t u ≠ linkList b t v) : ¬ Iso b u v
Proof
:=
  fun hi => h (linkList_iso hi)

The search for a chain of moves

search b u v budget looks for a chain of moves from u to v. The chain has the format of checkChain: a list of certificate steps (k, m, fwd). The result is some chain or none.

The neighbours of a drawing

steps b u: each drawing that one move gives from u, with the certificate step. Each result is well formed.

Direction Moves tried at position k
Forward (fwd = true) The moves whose left side can start with the events at k (fwdMoves)
Backward (fwd = false) R3 at a crossing. The moves with right side [cup i] or [cap i] at that event. The moves with an empty right side, at each strand of the height k (bwdMoves)
  • A backward far commutation is a forward far commutation of the other kind. fwdMoves gives both kinds.
  • applyCert keeps only the candidates that match the drawing. A wrong candidate costs time, not soundness.

The order of the search

  • The search starts from both drawings. Each side keeps the drawings it found (Side.seen) and the drawings it must expand (Side.todo).
  • todo is in order of length: the fewest events first. A new drawing goes after the drawings of its length.
  • Each round expands one drawing. The side whose next drawing is longer expands. A tie expands the side of u.
  • A drawing that the other side found ends the search. The chain is the chain of the first side, then the reversed chain of the other side (revChain).
  • budget is the number of rounds.

Facts

  • search is not complete. none says only that the budget ran out, or that no drawing was left to expand.
  • The decision function compare checks each chain with checkChain before it uses it. So the search needs no proof of soundness.
namespace FormalizeKnitting.Equality
abbrev Cert := Nat × Mv × Bool

def fwdMoves : MWord → List Mv
  | g :: h :: _ =>
    let s := match h with
      | .cross _ s => s
      | _ => true
    [Mv.farR g h, Mv.farL g h] ++
      match g with
      | .cup j => [Mv.zigR (j - 1), Mv.zigL j, Mv.cupSlideR (j - 1) s, Mv.cupSlideL j s, Mv.r1R (j - 1) s,
          Mv.r1L j s, Mv.r1cup j s]
      | .cross j t => [Mv.capSlideR j t, Mv.capSlideL (j - 1) t, Mv.r1cap j t, Mv.r2 j t, Mv.r3 j t]
      | .cap _ => []
  | _ => []

def insertMoves (i : Nat) : List Mv :=
  [Mv.zigR i, Mv.zigL i, Mv.r1R i true, Mv.r1R i false, Mv.r1L i true, Mv.r1L i false, Mv.r2 i true, Mv.r2 i false]

def bwdMoves (w : Nat) (u : MWord) : List Mv :=
  let heads := match u with
    | .cup j :: _ => [true, false].flatMap fun s => [Mv.cupSlideR j s, Mv.cupSlideL (j - 1) s, Mv.r1cup j s]
    | .cap j :: _ => [true, false].flatMap fun s => [Mv.capSlideR (j - 1) s, Mv.capSlideL j s, Mv.r1cap j s]
    | .cross j s :: _ => [Mv.r3 (j - 1) s]
    | [] => []
  heads ++ (List.range (w + 1)).flatMap insertMoves

def candidates (b : Nat) (u : MWord) (k : Nat) : List Cert :=
  let rest := u.drop k
  let w := (topWidth b (u.take k)).getD 0
  (fwdMoves rest).map (fun m => (k, m, true)) ++ (bwdMoves w rest).map (fun m => (k, m, false))

def stepTo (b : Nat) (u : MWord) (c : Cert) : List (MWord × Cert) :=
  match applyCert u c with
  | some u' => if (topWidth b u').isSome then [(u', c)] else []
  | none => []

def steps (b : Nat) (u : MWord) : List (MWord × Cert) :=
  (List.range (u.length + 1)).flatMap fun k => (candidates b u k).flatMap (stepTo b u)

def revChain (cs : List Cert) : List Cert := cs.reverse.map fun c => (c.1, c.2.1, !c.2.2)

structure Side where
  seen : List (MWord × List Cert)
  todo : List (MWord × List Cert)

def chainOf (w : MWord) : List (MWord × List Cert) → Option (List Cert)
  | [] => none
  | e :: es => if e.1 = w then some e.2 else chainOf w es

def insertByLength (e : MWord × List Cert) : List (MWord × List Cert) → List (MWord × List Cert)
  | [] => [e]
  | f :: fs => if f.1.length ≤ e.1.length then f :: insertByLength e fs else e :: f :: fs

def absorb (other : List (MWord × List Cert)) (c : List Cert) :
    List (MWord × Cert) → Side → Side ⊕ List Cert
  | [], s => .inl s
  | (w, st) :: rest, s =>
    match chainOf w s.seen with
    | some _ => absorb other c rest s
    | none =>
      match chainOf w other with
      | some c' => .inr (c ++ [st] ++ revChain c')
      | none => absorb other c rest ⟨(w, c ++ [st]) :: s.seen, insertByLength (w, c ++ [st]) s.todo⟩

def expand (b : Nat) (s other : Side) : Side ⊕ List Cert :=
  match s.todo with
  | [] => .inl s
  | (w, c) :: rest => absorb other.seen c (steps b w) ⟨s.seen, rest⟩

def searchLoop (b : Nat) : Nat → Side → Side → Option (List Cert)
  | 0, _, _ => none
  | fuel + 1, A, B =>
    match A.todo, B.todo with
    | [], [] => none
    | a :: _, b' :: _ =>
      if b'.1.length ≤ a.1.length then
        match expand b A B with
        | .inr cs => some cs
        | .inl A' => searchLoop b fuel A' B
      else
        match expand b B A with
        | .inr cs => some (revChain cs)
        | .inl B' => searchLoop b fuel A B'
    | _ :: _, [] =>
      match expand b A B with
      | .inr cs => some cs
      | .inl A' => searchLoop b fuel A' B
    | [], _ :: _ =>
      match expand b B A with
      | .inr cs => some (revChain cs)
      | .inl B' => searchLoop b fuel A B'

def search (b : Nat) (u v : MWord) (budget : Nat) : Option (List Cert) :=
  if u = v then some [] else searchLoop b budget ⟨[(u, [])], [(u, [])]⟩ ⟨[(v, [])], [(v, [])]⟩
end FormalizeKnitting.Equality

The decision function

compare b u v budget compares two drawings on b bottom strands. It gives one of three answers. Each answer of equal or notEqual has a proof.

Answer Meaning Certificate Theorem
equal chain Iso b u v A chain of moves that checkChain accepts iso_of_compare_eq_equal
notEqual reason ¬ Iso b u v An invariant with different values on u and v not_iso_of_compare_eq_notEqual
unknown No claim None None

The order of the tests

differ makes tests 1 to 3. differ_eq_none: related drawings pass all three.

  1. The top widths (topWidth). Different: notEqual .width (Iso.topWidth_eq).
  2. The connectivity lists (connList). Different: notEqual .connectivity (connList_iso).
  3. The lists of linking counts (linkList). Different: notEqual .linking (linkList_iso).
  4. The search (search). A chain that checkChain accepts: equal chain (iso_of_checkChain).
  5. Else unknown.

Limits

  • unknown is not notEqual. A search that finds no chain proves nothing.
  • The invariants are sound, not complete. Different tangles can have the same top width, the same connectivity and the same linking counts. For these, compare answers equal or unknown, never notEqual.
  • notEqual means "not related by the 14 moves Mv". Iso is the relation that the moves generate. This package does not prove that the moves give all isotopies rel boundary: the pitchfork move is not derived, and Lean does not contain the Reidemeister theorem for these drawings.
  • equal holds without these limits: each move is an isotopy.
namespace FormalizeKnitting.Equality
inductive Reason where
  | width
  | connectivity
  | linking
  deriving DecidableEq, Repr

inductive Verdict where
  | equal (chain : List Cert)
  | notEqual (reason : Reason)
  | unknown
  deriving DecidableEq, Repr

def differ (b : Nat) (u v : MWord) : Option Reason :=
  if topWidth b u ≠ topWidth b v then some .width
  else if connList b ((topWidth b u).getD 0) u ≠ connList b ((topWidth b u).getD 0) v then some .connectivity
  else if linkList b ((topWidth b u).getD 0) u ≠ linkList b ((topWidth b u).getD 0) v then some .linking
  else none

def compare (b : Nat) (u v : MWord) (budget : Nat) : Verdict :=
  match differ b u v with
  | some r => .notEqual r
  | none =>
    match search b u v budget with
    | some cs => if checkChain b u cs v then .equal cs else .unknown
    | none => .unknown
theorem differ_eq_none {b : Nat} {u v : MWord} (hi : Iso b u v) : differ b u v = none
Proof
:= by
  simp [differ, hi.topWidth_eq, connList_iso hi, linkList_iso hi]
theorem iso_of_compare_eq_equal {b : Nat} {u v : MWord} {budget : Nat} {cs : List Cert}
    (h : compare b u v budget = .equal cs) : checkChain b u cs v = true ∧ Iso b u v
Proof
:= by
  unfold compare at h
  split at h
  · cases h
  · split at h
    · split_ifs at h with hc
      cases h
      exact ⟨hc, iso_of_checkChain u cs v hc⟩
    · cases h
theorem not_iso_of_compare_eq_notEqual {b : Nat} {u v : MWord} {budget : Nat} {r : Reason}
    (h : compare b u v budget = .notEqual r) : ¬ Iso b u v
Proof
:= by
  intro hi
  unfold compare at h
  rw [differ_eq_none hi] at h
  cases hs : search b u v budget <;> simp [hs] at h
  split_ifs at h
def Verdict.isEqual : Verdict → Bool
  | .equal _ => true
  | _ => false

def Verdict.isNotEqual : Verdict → Bool
  | .notEqual _ => true
  | _ => false
theorem iso_of_isEqual {b : Nat} {u v : MWord} {budget : Nat} (h : (compare b u v budget).isEqual = true) :
    Iso b u v
Proof
:= by
  cases hc : compare b u v budget with
  | equal cs => exact (iso_of_compare_eq_equal hc).2
  | notEqual r => simp [hc, Verdict.isEqual] at h
  | unknown => simp [hc, Verdict.isEqual] at h
theorem not_iso_of_isNotEqual {b : Nat} {u v : MWord} {budget : Nat}
    (h : (compare b u v budget).isNotEqual = true) : ¬ Iso b u v
Proof
:= by
  cases hc : compare b u v budget with
  | equal cs => simp [hc, Verdict.isNotEqual] at h
  | notEqual r => exact not_iso_of_compare_eq_notEqual hc
  | unknown => simp [hc, Verdict.isNotEqual] at h

Examples

Each example is checked with decide. The kernel runs compare.

Drawings Bottom strands Answer
One crossing, its mirror 2 notEqual .linking
One crossing, two straight strands 2 notEqual .connectivity
A cap, two straight strands 2 notEqual .width
Two crossings of opposite sign (R2), two straight strands 2 equal, one move
A zig-zag, one straight strand 1 equal, one move
A kink (R1), one straight strand 1 equal, one move
The R2 pair again, budget 0 2 unknown
open Gen
example : compare 2 [cross 0 true] [cross 0 false] 0 = .notEqual .linking
Proof
:= by decide
example : ¬ Iso 2 [cross 0 true] [cross 0 false]
Proof
:= not_iso_of_isNotEqual (budget := 0) (by decide)
example : compare 2 [cross 0 true] [] 0 = .notEqual .connectivity
Proof
:= by decide
example : compare 2 [cap 0] [] 0 = .notEqual .width
Proof
:= by decide
example : compare 2 [cross 0 true, cross 0 false] [] 1 = .equal [(0, .r2 0 true, true)]
Proof
:= by decide
example : Iso 2 [cross 0 true, cross 0 false] []
Proof
:= iso_of_isEqual (budget := 1) (by decide)
example : Iso 1 [cup 1, cap 0] []
Proof
:= iso_of_isEqual (budget := 1) (by decide)
example : Iso 1 [cup 1, cross 0 true, cap 1] []
Proof
:= iso_of_isEqual (budget := 1) (by decide)
example : compare 2 [cross 0 true, cross 0 false] [] 0 = .unknown
Proof
:= by decide

References

  1. Rolfsen. 1976. "Knots and Links". Publish or Perish. Chapter 5, the linking number.
  2. Reidemeister. 1927. "Elementare Begründung der Knotentheorie". Abh. Math. Sem. Univ. Hamburg 5.
  3. Turaev. 1989. "Operator invariants of tangles, and R-matrices". Izv. Akad. Nauk SSSR 53:5. §7.3, Theorem 3.2.