Research | Knitting | Functor drawℋ: Hand-knitting drawing

Functor drawℋ: Hand-knitting drawing

drawℋ draws each hand-knitting program as a tangle. A state goes to the number of strands at its top edge. A program goes to the drawing of its actions.

The drawing functor drawℋ : ℋ ⥤ 𝒯

Part Definition
Objects A state goes to the number of strands at its top edge (State.width).
Arrows A hand-knitting program goes to the class of its drawing by draw (drawing). wf_drawing: the drawing fits the two top edges.
Composition draw_app: the drawing of p ++ q is the drawing of p, then the drawing of q.
namespace Hand
open CategoryTheory
theorem drawing_eq {s t : State} {p : Program} {u : MWord} (h : draw s p = some (t, u)) : drawing s p = u
Proof
:= by
  simp [drawing, h]
theorem draw_drawing {a b : HandObj} (p : HandProgram a b) :
    draw a.state p.1 = some (b.state, drawing a.state p.1)
Proof
:= by
  obtain ⟨u, hu, -⟩ := p.2
  rw [drawing_eq hu]; exact hu
theorem wf_drawing {a b : HandObj} (p : HandProgram a b) :
    WF a.state.width b.state.width (drawing a.state p.1)
Proof
:= by
  obtain ⟨u, hu, wu⟩ := p.2
  rw [drawing_eq hu]; exact wu
theorem hom_congr {m n : Nat} {u v : MWord} (hu : WF m n u) (hv : WF m n v) (h : u = v) :
    TangleObj.hom u hu = TangleObj.hom v hv
Proof
:= by
  subst h; rfl
def drawH : HandObj ⥤ TangleObj where
  obj a := ⟨a.state.width⟩
  map {a b} p := TangleObj.hom (drawing a.state (show HandProgram a b from p).1) (wf_drawing p)
  map_id _ := hom_congr _ _ rfl
  map_comp {a b c} p q := by
    show TangleObj.hom _ _ = TangleObj.hom _ _ ≫ TangleObj.hom _ _
    rw [TangleObj.hom_comp]
    apply hom_congr
    exact drawing_eq (draw_app (draw_drawing (show HandProgram a b from p))
      (draw_drawing (show HandProgram b c from q)))
end Hand

The other side of the work keeps the moves

rot w u is the drawing u seen from the other side of the work. Each strand at position j of a row of w' strands goes to position w' - 1 - j. A crossing keeps its value lo: the strand in front is now behind, and it comes from the other side.

Declaration Statement
rot_eq_mirror rot is the mirror image (mirrorAux) of the drawing with all crossings changed
rot_wf rot keeps the top width
rot_rot rot two times gives the drawing again, if the drawing fits
Mv.rot A move, seen from the other side
Mv.rot_spec The two sides of a move, seen from the other side, are the two sides of a move
Iso.rot Related drawings, seen from the other side, are related
Iso.rot_trans A chain for a drawing and a chain from its other side give a chain for the other side of the start
  • Mv.rot keeps the sign of each crossing. R3 changes its direction: the left side goes to the right side.
  • A far commutation to the right (farR) goes to a far commutation to the left (farL), and the other way.
  • A stitch that the knitter works on a wrong-side row, seen from the right side, is its twin stitch worked to the left, seen from the other side. Iso.rot changes a chain of moves for the stitch into a chain for the twin.
namespace Hand
theorem rotGen_widthAfter (w : Nat) (g : Gen) (v : Nat) : (rotGen w g).widthAfter v = g.widthAfter v
Proof
:= by
  cases g <;> rfl
theorem rot_cons (w : Nat) (g : Gen) (gs : MWord) : rot w (g :: gs) = rotGen w g :: rot (g.widthAfter w) gs
Proof
:= rfl
theorem rot_eq_mirror (w : Nat) (u : MWord) : rot w u = mirrorAux w (reverseCrossings u)
Proof
:= by
  induction u generalizing w with
  | nil => rfl
  | cons g gs ih =>
    cases g <;> simp [rot, rotGen, mirrorAux, reverseCrossings, Gen.widthAfter, ih]
theorem rot_wf {w c : Nat} {u : MWord} (h : topWidth w u = some c) : topWidth w (rot w u) = some c
Proof
:= by
  rw [rot_eq_mirror]
  exact mirror_wf (by rw [reverseCrossings_wf]; exact h)
theorem rot_append {b c : Nat} {u : MWord} (h : topWidth b u = some c) (v : MWord) :
    rot b (u ++ v) = rot b u ++ rot c v
Proof
:= by
  induction u generalizing b with
  | nil =>
    simp only [topWidth, Option.some.injEq] at h
    subst h; rfl
  | cons g gs ih =>
    obtain ⟨-, h'⟩ := fits_of_topWidth h
    rw [List.cons_append, rot_cons, rot_cons, ih h', List.cons_append]
theorem rotGen_rotGen {w : Nat} {g : Gen} (h : g.fits w = true) : rotGen w (rotGen w g) = g
Proof
:= by
  cases g <;> simp only [Gen.fits, decide_eq_true_eq] at h <;> simp [rotGen] <;> omega
theorem rot_rot {w c : Nat} {u : MWord} (h : topWidth w u = some c) : rot w (rot w u) = u
Proof
:= by
  induction u generalizing w with
  | nil => rfl
  | cons g gs ih =>
    obtain ⟨hf, h'⟩ := fits_of_topWidth h
    rw [rot_cons, rot_cons, rotGen_widthAfter, rotGen_rotGen hf, ih h']

The moves

def Mv.rot (w : Nat) : Mv → Mv
  | .farR g h => .farL (Hand.rotGen w g) (Hand.rotGen (g.widthAfter w) h)
  | .farL g h => .farR (Hand.rotGen w g) (Hand.rotGen (g.widthAfter w) h)
  | .zigR i => .zigL (w - i - 1)
  | .zigL i => .zigR (w - i - 1)
  | .cupSlideR i s => .cupSlideL (w - i - 1) s
  | .cupSlideL i s => .cupSlideR (w - i - 1) s
  | .capSlideR i s => .capSlideL (w - 3 - i) s
  | .capSlideL i s => .capSlideR (w - 3 - i) s
  | .r1R i s => .r1L (w - i - 1) s
  | .r1L i s => .r1R (w - i - 1) s
  | .r1cup i s => .r1cup (w - i) s
  | .r1cap i s => .r1cap (w - 2 - i) s
  | .r2 i s => .r2 (w - 2 - i) s
  | .r3 i s => .r3 (w - 3 - i) s
theorem topWidth_cons_eq_some {w c : Nat} {g : Gen} {gs : MWord} :
    topWidth w (g :: gs) = some c ↔ g.fits w = true ∧ topWidth (g.widthAfter w) gs = some c
Proof
:= by
  simp only [topWidth]; split <;> simp_all
theorem topWidth_nil_eq_some {w c : Nat} : topWidth w [] = some c ↔ w = c
Proof
:= by simp [topWidth]
macro "rot_arith" h:ident : tactic => `(tactic| (
  simp only [Mv.lhs, topWidth_cons_eq_some, topWidth_nil_eq_some, Gen.widthAfter, Gen.fits,
    decide_eq_true_eq] at $h:ident))
theorem Mv.rot_spec (m : Mv) (hok : m.ok = true) {w c : Nat} (h : topWidth w m.lhs = some c) :
    (m.rot w).ok = true ∧
    ((Hand.rot w m.lhs = (m.rot w).lhs ∧ Hand.rot w m.rhs = (m.rot w).rhs) ∨
     (Hand.rot w m.lhs = (m.rot w).rhs ∧ Hand.rot w m.rhs = (m.rot w).lhs))
Proof
:= by
  cases m with
  | farR g h' =>
    simp only [Mv.ok, decide_eq_true_eq] at hok
    refine ⟨?_, Or.inl ⟨rfl, ?_⟩⟩ <;>
    cases g <;> cases h' <;> rot_arith h <;>
      simp_all [Mv.ok, Mv.rot, Mv.rhs, Hand.rotGen, Gen.lo, Gen.ins, Gen.outs, Gen.withLo, Hand.rot,
        decide_eq_true_eq, Gen.widthAfter] <;> first | omega | (apply decide_eq_true; omega)
  | farL g h' =>
    simp only [Mv.ok, decide_eq_true_eq] at hok
    refine ⟨?_, Or.inl ⟨rfl, ?_⟩⟩ <;>
    cases g <;> cases h' <;> rot_arith h <;>
      simp_all [Mv.ok, Mv.rot, Mv.rhs, Hand.rotGen, Gen.lo, Gen.ins, Gen.outs, Gen.withLo, Hand.rot,
        decide_eq_true_eq, Gen.widthAfter] <;> first | omega | (apply decide_eq_true; omega)
  | r3 i s =>
    rot_arith h
    refine ⟨rfl, Or.inr ⟨?_, ?_⟩⟩ <;> simp [Mv.rot, Mv.lhs, Mv.rhs, Hand.rot, Hand.rotGen, Gen.widthAfter] <;> omega
  | _ =>
    rot_arith h
    refine ⟨rfl, Or.inl ⟨?_, ?_⟩⟩ <;> simp [Mv.rot, Mv.lhs, Mv.rhs, Hand.rot, Hand.rotGen, Gen.widthAfter] <;> omega
theorem topWidth_split_three {b c : Nat} {p x q : MWord} (h : topWidth b (p ++ x ++ q) = some c) :
    ∃ y z, topWidth b p = some y ∧ topWidth y x = some z ∧ topWidth z q = some c
Proof
:= by
  rw [topWidth_append, topWidth_append] at h
  cases hp : topWidth b p with
  | none => simp [hp] at h
  | some y =>
    simp only [hp, Option.bind_some] at h
    cases hx : topWidth y x with
    | none => simp [hx] at h
    | some z => simp only [hx, Option.bind_some] at h; exact ⟨y, z, rfl, hx, h⟩
theorem Step1.rot {b : Nat} {u v : MWord} (h : Step1 b u v) :
    Step1 b (Hand.rot b u) (Hand.rot b v) ∨ Step1 b (Hand.rot b v) (Hand.rot b u)
Proof
:= by
  obtain ⟨hu, hv, p, q, m, hok, rfl, rfl⟩ := h
  obtain ⟨c, hc⟩ := Option.isSome_iff_exists.mp hu
  obtain ⟨c', hc'⟩ := Option.isSome_iff_exists.mp hv
  obtain ⟨y, z, hp, hl, hq⟩ := topWidth_split_three hc
  have hr := Mv.wf_fwd m hok hl
  have eu : Hand.rot b (p ++ m.lhs ++ q) = Hand.rot b p ++ Hand.rot y m.lhs ++ Hand.rot z q := by
    rw [Hand.rot_append (topWidth_append_of hp hl), Hand.rot_append hp]
  have ev : Hand.rot b (p ++ m.rhs ++ q) = Hand.rot b p ++ Hand.rot y m.rhs ++ Hand.rot z q := by
    rw [Hand.rot_append (topWidth_append_of hp hr), Hand.rot_append hp]
  have su : (topWidth b (Hand.rot b (p ++ m.lhs ++ q))).isSome := by rw [Hand.rot_wf hc]; rfl
  have sv : (topWidth b (Hand.rot b (p ++ m.rhs ++ q))).isSome := by rw [Hand.rot_wf hc']; rfl
  obtain ⟨hok', ⟨e1, e2⟩ | ⟨e1, e2⟩⟩ := m.rot_spec hok hl
  · exact Or.inl ⟨su, sv, _, _, m.rot y, hok', by rw [eu, e1], by rw [ev, e2]⟩
  · exact Or.inr ⟨sv, su, _, _, m.rot y, hok', by rw [ev, e2], by rw [eu, e1]⟩
theorem Iso.rot {b : Nat} {u v : MWord} (h : Iso b u v) : Iso b (Hand.rot b u) (Hand.rot b v)
Proof
:= by
  induction h with
  | rel _ _ h =>
    rcases h.rot with h | h
    · exact Relation.EqvGen.rel _ _ h
    · exact (Relation.EqvGen.rel _ _ h).symm
  | refl => exact Iso.refl _ _
  | symm _ _ _ ih => exact ih.symm
  | trans _ _ _ _ _ ih1 ih2 => exact ih1.trans ih2
theorem Iso.rot_trans {b : Nat} {u v w : MWord} (h : Iso b u v) (hw : Iso b (Hand.rot b v) w) :
    Iso b (Hand.rot b u) w
Proof
:=
  h.rot.trans hw

From hand knitting to the shorthand: the rule for c loops together

A rule changes a hand program into a shorthand program. Each rule has a proof: the drawings of the two programs are related by moves (Iso). This file has the first rule. It is for one new loop through c loops of the left needle, for each c ≥ 1.

Theorem Hand program, from start c y Shorthand program, from replicate c (.stack 1) ++ [.yarn]
ktog_rule work .along c ++ [.letOff .left c], y = .back replicate (c - 1) (.merge 0) ++ [.knit .L .f]
ptog_rule work .against c ++ [.letOff .left c], y = .front replicate (c - 1) (.merge 0) ++ [.knit .L .b]
  • For c = 1 the hand programs are knit and purl. For c = 2 they are k2tog and p2tog.
  • The hand program ends in oneLoop y: one loop on the right needle. The shorthand program ends in the row [.yarn, .stack 1]. In the two drawings, the yarn end is the strand on the left and the new loop is on its right.
  • The theorems hold for each c. The proof is an induction on c.

The proof

  • slide: a strand that goes over all strands, or under all strands, slides through a drawing. The proof uses one move for each event: R3 for a crossing, a cup slide for a cup, a cap slide for a cap.
  • shortW: the shorthand drawing in a closed form (knitInterior_L, D_ktog). shortW_peel: the outside loop of the stack goes round the knit through the other loops.
  • Knit: the hand drawing (kHand) and the shorthand drawing (shortW true) have the same step from c loops to c + 1 loops: the drawing for c loops, moved two strands to the right, then kF (kHand_succ, kShort_succ). kF is a fixed drawing on 5 strands: the new legs go through the outside loop.
  • Purl: the hand drawing (pHand) and the shorthand drawing (shortW false) have the same step: the yarn goes over the last loop, then the drawing for c loops, then pG (pHand_succ, pShort_inner). The shorthand step uses the step for c - 1 loops inside the outside loop.
  • The fixed drawings on 4, 5 and 7 strands are related by chains of moves that checkChain accepts (bandCap0, kFix, f2Fix, l7Fix).

Moves in a context

theorem iso_mv {b : Nat} {p q : MWord} (m : Mv) (hok : m.ok = true)
    (hw : (topWidth b (p ++ m.lhs ++ q)).isSome) : Iso b (p ++ m.lhs ++ q) (p ++ m.rhs ++ q)
Proof
:= by
  obtain ⟨c, hc⟩ := Option.isSome_iff_exists.mp hw
  exact Relation.EqvGen.rel _ _ (Step1.of_wf hok hc)
theorem topWidth_append_isSome {b : Nat} {u v : MWord} (h : (topWidth b (u ++ v)).isSome) :
    ∃ c, topWidth b u = some c ∧ (topWidth c v).isSome
Proof
:= by
  rw [topWidth_append] at h
  cases hu : topWidth b u with
  | none => simp [hu] at h
  | some c => exact ⟨c, rfl, by simpa [hu] using h⟩
theorem Iso.wf {b : Nat} {u v : MWord} (h : Iso b u v) (hu : (topWidth b u).isSome) : (topWidth b v).isSome
Proof
:= by
  rwa [← h.topWidth_eq]
theorem Iso.mid {b : Nat} {P u v Q : MWord}
    (h : ∀ c, topWidth b P = some c → (topWidth c u).isSome → Iso c u v)
    (hw : (topWidth b (P ++ u ++ Q)).isSome) : Iso b (P ++ u ++ Q) (P ++ v ++ Q)
Proof
:= by
  obtain ⟨c, hP, hu⟩ := topWidth_append_isSome (by rwa [List.append_assoc] at hw)
  obtain ⟨d, hd, -⟩ := topWidth_append_isSome hu
  exact Iso.suffix Q (Iso.prefix hP (h c hP (by rw [hd]; rfl))) hw
theorem Iso.low {b : Nat} {u v Q : MWord} (h : (topWidth b u).isSome → Iso b u v)
    (hw : (topWidth b (u ++ Q)).isSome) : Iso b (u ++ Q) (v ++ Q)
Proof
:= by
  obtain ⟨c, hc, -⟩ := topWidth_append_isSome hw
  exact Iso.suffix Q (h (by rw [hc]; rfl)) hw
theorem Iso.high {b : Nat} {P u v : MWord}
    (h : ∀ c, topWidth b P = some c → (topWidth c u).isSome → Iso c u v)
    (hw : (topWidth b (P ++ u)).isSome) : Iso b (P ++ u) (P ++ v)
Proof
:= by
  have := Iso.mid (Q := []) h (by simpa using hw)
  simpa using this

Far commutation

theorem farR_ok {g h : Gen} (hg : g.lo + g.outs ≤ h.lo) : (Mv.farR g h).ok = true
Proof
:= by
  simp only [Mv.ok]; exact decide_eq_true hg
theorem farL_ok {g h : Gen} (hg : h.lo + h.ins ≤ g.lo) : (Mv.farL g h).ok = true
Proof
:= by
  simp only [Mv.ok]; exact decide_eq_true hg
theorem Gen.withLo_cross_lo {h : Gen} (hh : 2 ≤ h.lo) (i : Nat) (s : Bool) :
    h.withLo (h.lo - (cross i s).outs + (cross i s).ins) = h
Proof
:= by
  cases h <;> simp only [Gen.lo] at hh <;> simp only [Gen.withLo, Gen.lo, Gen.outs, Gen.ins] <;> congr 1 <;> omega
theorem shiftW_cons (k : Nat) (g : Gen) (u : MWord) : shiftW k (g :: u) = g.shift k :: shiftW k u
Proof
:= rfl
theorem shiftW_nil (k : Nat) : shiftW k [] = []
Proof
:= rfl
theorem iso_early_farR {b : Nat} {h : Gen} :
    ∀ {u : MWord}, (∀ g ∈ u, ∃ i s, g = cross i s ∧ i + 2 ≤ h.lo) →
    (topWidth b (u ++ [h])).isSome → Iso b (u ++ [h]) (h :: u)
Proof
  | [], _, _ => Iso.refl _ _
  | g :: gs, hu, hw => by
    obtain ⟨i, s, rfl, hi⟩ := hu g (by simp)
    have h1 : Iso b ([cross i s] ++ (gs ++ [h])) ([cross i s] ++ (h :: gs)) :=
      Iso.high (fun c _ hc => iso_early_farR (fun g' hg' => hu g' (by simp [hg'])) hc) (by simpa using hw)
    have h2 := iso_mv (b := b) (p := []) (q := gs) (Mv.farR (cross i s) h)
      (farR_ok (show i + 2 ≤ h.lo by omega))
      (by have := Iso.wf h1 (by simpa using hw); simpa [Mv.lhs] using this)
    simp only [Mv.lhs, Mv.rhs, Gen.withLo_cross_lo (show 2 ≤ h.lo by omega), List.nil_append, List.cons_append] at h2
    simp only [List.singleton_append] at h1
    exact (by simpa using h1 : Iso b (cross i s :: (gs ++ [h])) (cross i s :: h :: gs)).trans h2
theorem iso_early_farL {b : Nat} {h : Gen} : ∀ {u : MWord}, (∀ g ∈ u, h.lo + h.ins ≤ g.lo) →
    (topWidth b (u ++ [h])).isSome → Iso b (u ++ [h]) (h :: u.map fun g => g.withLo (g.lo + h.outs - h.ins))
Proof
  | [], _, _ => Iso.refl _ _
  | g :: gs, hu, hw => by
    have hg := hu g (by simp)
    have h1 : Iso b ([g] ++ (gs ++ [h])) ([g] ++ (h :: gs.map fun g => g.withLo (g.lo + h.outs - h.ins))) :=
      Iso.high (fun c _ hc => iso_early_farL (fun g' hg' => hu g' (by simp [hg'])) hc) (by simpa using hw)
    have h2 := iso_mv (b := b) (p := []) (q := gs.map fun g => g.withLo (g.lo + h.outs - h.ins))
      (Mv.farL g h) (farL_ok hg) (by have := Iso.wf h1 (by simpa using hw); simpa [Mv.lhs] using this)
    simp only [Mv.lhs, Mv.rhs, List.nil_append, List.cons_append] at h2
    simp only [List.singleton_append] at h1
    exact (by simpa using h1 : Iso b (g :: (gs ++ [h])) (g :: h :: _)).trans (by simpa using h2)
theorem iso_late_word {b q : Nat} : ∀ {T u : MWord}, (∀ t ∈ T, ∃ p s, t = cross p s ∧ p + 2 ≤ q) →
    (∀ g ∈ u, q ≤ g.lo) → (topWidth b (T ++ u)).isSome → Iso b (T ++ u) (u ++ T)
Proof
  | [], u, _, _, _ => by simpa using Iso.refl b u
  | t :: T, u, hT, hu, hw => by
    obtain ⟨p, s, rfl, hp⟩ := hT t (by simp)
    have h1 : Iso b ([cross p s] ++ (T ++ u)) ([cross p s] ++ (u ++ T)) :=
      Iso.high (fun c _ hc => iso_late_word (fun t' ht' => hT t' (by simp [ht'])) hu hc) (by simpa using hw)
    have hw2 : (topWidth b ((cross p s :: u) ++ T)).isSome := by
      have := Iso.wf h1 (by simpa using hw); simpa using this
    obtain ⟨c, hc, -⟩ := topWidth_append_isSome hw2
    have h2 := Iso.suffix T (iso_pass_left (fun g hg => by have := hu g hg; omega) hc) hw2
    exact (by simpa using h1 : Iso b (cross p s :: (T ++ u)) (cross p s :: (u ++ T))).trans (by simpa using h2)

A strand that passes one way

theorem strandRight_succ (i k : Nat) (f : Bool) :
    strandRight i (k + 1) f = strandRight i k f ++ [cross (i + k) f]
Proof
:= by
  simp [strandRight, List.range_succ]
theorem strandRight_succ' (i k : Nat) (f : Bool) :
    strandRight i (k + 1) f = cross i f :: strandRight (i + 1) k f
Proof
:= by
  simp only [strandRight, List.range_succ_eq_map, List.map_cons, List.map_map, Nat.add_zero]
  congr 1
  apply List.map_congr_left
  intro j _
  simp only [Function.comp, Nat.succ_eq_add_one]
  congr 1; omega
theorem shiftW_strandRight (k i n : Nat) (f : Bool) : shiftW k (strandRight i n f) = strandRight (i + k) n f
Proof
:= by
  simp only [shiftW, strandRight, List.map_map]
  apply List.map_congr_left
  intro j _
  simp only [Function.comp, Gen.shift]
  congr 1; omega
theorem mem_strandRight {g : Gen} {i n : Nat} {f : Bool} (h : g ∈ strandRight i n f) :
    ∃ j, j < n ∧ g = cross (i + j) f
Proof
:= by
  simp only [strandRight, List.mem_map, List.mem_range] at h
  obtain ⟨j, hj, rfl⟩ := h
  exact ⟨j, hj, rfl⟩
theorem strandRight_wf {w i n : Nat} (f : Bool) (h : i + n < w) : topWidth w (strandRight i n f) = some w
Proof
:=
  topWidth_crosses fun g hg => by
    obtain ⟨j, hj, rfl⟩ := mem_strandRight hg
    exact ⟨_, _, rfl, by omega⟩
theorem isSome_of_eq {o : Option Nat} {c : Nat} (h : o = some c) : o.isSome
Proof
:= by rw [h]; rfl
theorem wf_cons_cross {w c i : Nat} {s : Bool} {u : MWord} (hi : i + 1 < w) (h : topWidth w u = some c) :
    topWidth w (cross i s :: u) = some c
Proof
:= by
  rw [topWidth_cons_fits (by simp [Gen.fits]; omega)]; exact h
theorem wf_cons_cup {w c i : Nat} {u : MWord} (hi : i ≤ w) (h : topWidth (w + 2) u = some c) :
    topWidth w (cup i :: u) = some c
Proof
:= by
  rw [topWidth_cons_fits (by simp [Gen.fits]; omega)]; exact h
theorem wf_cons_cap {w c i : Nat} {u : MWord} (hi : i + 1 < w) (h : topWidth (w - 2) u = some c) :
    topWidth w (cap i :: u) = some c
Proof
:= by
  rw [topWidth_cons_fits (by simp [Gen.fits]; omega)]; exact h
theorem wf_nil (w : Nat) : topWidth w [] = some w
Proof
:= rfl

A strand that passes all strands on one side slides through each event

theorem slide_cross (f t : Bool) : ∀ (i a : Nat), i + 1 < a →
    Iso (a + 1) (strandRight 0 a f ++ [cross i t]) (cross (i + 1) t :: strandRight 0 a f)
Proof
  | 0, a, h => by
    obtain ⟨n, rfl⟩ : ∃ n, a = n + 2 := ⟨a - 2, by omega⟩
    have e : strandRight 0 (n + 2) f = [cross 0 f, cross 1 f] ++ strandRight 2 n f := by
      rw [strandRight_succ', strandRight_succ']; rfl
    rw [e]
    have hw0 : (topWidth (n + 3) ([cross 0 f, cross 1 f] ++ (strandRight 2 n f ++ [cross 0 t]))).isSome :=
      isSome_of_eq (topWidth_append_of (wf_cons_cross (by omega) (wf_cons_cross (by omega) (wf_nil _)))
        (topWidth_append_of (strandRight_wf f (by omega)) (wf_cons_cross (by omega) (wf_nil _))))
    have h1 : Iso (n + 3) ([cross 0 f, cross 1 f] ++ (strandRight 2 n f ++ [cross 0 t]))
        ([cross 0 f, cross 1 f] ++ (cross 0 t :: strandRight 2 n f)) := by
      refine Iso.high (fun c _ hc => ?_) hw0
      have := iso_early_farL (b := c) (h := cross 0 t) (u := strandRight 2 n f)
        (fun g hg => by obtain ⟨j, -, rfl⟩ := mem_strandRight hg; simp [Gen.lo, Gen.ins]) hc
      have e2 : (strandRight 2 n f).map (fun g => g.withLo (g.lo + (cross 0 t).outs - (cross 0 t).ins)) =
          strandRight 2 n f := by
        apply List.map_id''
        intro g
        cases g <;> simp [Gen.withLo, Gen.lo, Gen.outs, Gen.ins]
      rwa [e2] at this
    have h2 := r3Mixed (b := n + 3) (i := 0) f f t (Or.inl rfl) (by omega)
    have h3 := Iso.low (Q := strandRight 2 n f) (fun _ => h2) (by
      have := Iso.wf h1 hw0; simpa using this)
    have h1' : Iso (n + 3) (cross 0 f :: cross 1 f :: (strandRight 2 n f ++ [cross 0 t]))
        (cross 0 f :: cross 1 f :: cross 0 t :: strandRight 2 n f) := by simpa using h1
    have h3' : Iso (n + 3) (cross 0 f :: cross 1 f :: cross 0 t :: strandRight 2 n f)
        (cross 1 t :: cross 0 f :: cross 1 f :: strandRight 2 n f) := by simpa using h3
    simpa using h1'.trans h3'
  | i + 1, a, h => by
    obtain ⟨n, rfl⟩ : ∃ n, a = n + 1 := ⟨a - 1, by omega⟩
    have ih := (slide_cross f t i n (by omega)).shiftTo (i := 1) (b := n + 2) (by omega)
    simp only [shiftW_append, shiftW_strandRight, shiftW_cons, shiftW_nil, Gen.shift] at ih
    rw [strandRight_succ']
    have hw0 : (topWidth (n + 2) ([cross 0 f] ++ (strandRight 1 n f ++ [cross (i + 1) t]))).isSome :=
      isSome_of_eq (topWidth_append_of (wf_cons_cross (by omega) (wf_nil _))
        (topWidth_append_of (strandRight_wf f (by omega)) (wf_cons_cross (by omega) (wf_nil _))))
    have h1 : Iso (n + 2) ([cross 0 f] ++ (strandRight 1 n f ++ [cross (i + 1) t]))
        ([cross 0 f] ++ (cross (i + 1 + 1) t :: strandRight 1 n f)) := by
      refine Iso.high (fun c hc _ => ?_) hw0
      rw [wf_cons_cross (by omega) (wf_nil _)] at hc
      cases hc
      simpa [Nat.add_comm] using ih
    have h2 := iso_mv (b := n + 2) (p := []) (q := strandRight 1 n f) (Mv.farR (cross 0 f) (cross (i + 2) t))
      (farR_ok (by simp [Gen.lo, Gen.outs])) (by have := Iso.wf h1 hw0; simpa [Mv.lhs] using this)
    simp only [Mv.lhs, Mv.rhs, Gen.withLo, Gen.lo, Gen.outs, Gen.ins, List.nil_append, List.cons_append] at h2
    have h1' : Iso (n + 2) (cross 0 f :: (strandRight 1 n f ++ [cross (i + 1) t]))
        (cross 0 f :: cross (i + 2) t :: strandRight 1 n f) := by simpa using h1
    simpa using h1'.trans h2
theorem strandRight_map {i i' p q : Nat} (n : Nat) (f : Bool) (h : i + p = i' + q) :
    (strandRight i n f).map (fun g => g.withLo (g.lo + p - q)) = strandRight i' n f
Proof
:= by
  simp only [strandRight, List.map_map]
  apply List.map_congr_left
  intro j _
  simp only [Function.comp, Gen.withLo, Gen.lo]
  congr 1; omega
theorem slide_cup (f : Bool) : ∀ (i a : Nat), i ≤ a →
    Iso (a + 1) (strandRight 0 a f ++ [cup i]) (cup (i + 1) :: strandRight 0 (a + 2) f)
Proof
  | 0, a, _ => by
    have hw0 : (topWidth (a + 1) (strandRight 0 a f ++ [cup 0])).isSome :=
      isSome_of_eq (topWidth_append_of (strandRight_wf f (by omega)) (wf_cons_cup (by omega) (wf_nil _)))
    have h1 := iso_early_farL (b := a + 1) (h := cup 0) (u := strandRight 0 a f)
      (fun g _ => by simp [Gen.lo, Gen.ins]) hw0
    rw [strandRight_map (i' := 2) (p := (cup 0).outs) (q := (cup 0).ins) a f rfl] at h1
    have hw1 : (topWidth (a + 1) ([] ++ (Mv.cupSlideR 0 f).lhs ++ strandRight 2 a f)).isSome := by
      simp only [Mv.lhs, List.nil_append]
      exact isSome_of_eq (wf_cons_cup (by omega) (wf_cons_cross (by omega) (wf_cons_cross (by omega)
        (strandRight_wf f (by omega)))))
    have h2 := iso_mv (Mv.cupSlideR 0 f) rfl hw1
    simp only [Mv.lhs, Mv.rhs, List.nil_append, List.cons_append] at h2
    rw [strandRight_succ', strandRight_succ']
    exact h1.trans h2.symm
  | i + 1, a, h => by
    obtain ⟨n, rfl⟩ : ∃ n, a = n + 1 := ⟨a - 1, by omega⟩
    have ih := (slide_cup f i n (by omega)).shiftTo (i := 1) (b := n + 2) (by omega)
    simp only [shiftW_append, shiftW_strandRight, shiftW_cons, shiftW_nil, Gen.shift] at ih
    rw [strandRight_succ', show n + 1 + 2 = (n + 2) + 1 by omega, strandRight_succ']
    have hw0 : (topWidth (n + 2) ([cross 0 f] ++ (strandRight 1 n f ++ [cup (i + 1)]))).isSome :=
      isSome_of_eq (topWidth_append_of (wf_cons_cross (by omega) (wf_nil _))
        (topWidth_append_of (strandRight_wf f (by omega)) (wf_cons_cup (by omega) (wf_nil _))))
    have h1 : Iso (n + 2) ([cross 0 f] ++ (strandRight 1 n f ++ [cup (i + 1)]))
        ([cross 0 f] ++ (cup (i + 2) :: strandRight 1 (n + 2) f)) := by
      refine Iso.high (fun c hc _ => ?_) hw0
      rw [wf_cons_cross (by omega) (wf_nil _)] at hc
      cases hc
      exact ih
    have h2 := iso_mv (b := n + 2) (p := []) (q := strandRight 1 (n + 2) f) (Mv.farR (cross 0 f) (cup (i + 2)))
      (farR_ok (by simp [Gen.lo, Gen.outs])) (by have := Iso.wf h1 hw0; simpa [Mv.lhs] using this)
    simp only [Mv.lhs, Mv.rhs, Gen.withLo, Gen.lo, Gen.outs, Gen.ins, List.nil_append, List.cons_append] at h2
    have h1' : Iso (n + 2) (cross 0 f :: (strandRight 1 n f ++ [cup (i + 1)]))
        (cross 0 f :: cup (i + 2) :: strandRight 1 (n + 2) f) := by simpa using h1
    have e : i + 2 - 2 + 2 = i + 1 + 1 := by omega
    rw [e] at h2
    simpa using h1'.trans h2
theorem slide_cap (f : Bool) : ∀ (i a : Nat), i + 1 < a →
    Iso (a + 1) (strandRight 0 a f ++ [cap i]) (cap (i + 1) :: strandRight 0 (a - 2) f)
Proof
  | 0, a, h => by
    obtain ⟨n, rfl⟩ : ∃ n, a = n + 2 := ⟨a - 2, by omega⟩
    have e : strandRight 0 (n + 2) f = [cross 0 f, cross 1 f] ++ strandRight 2 n f := by
      rw [strandRight_succ', strandRight_succ']; rfl
    rw [e]
    have hw0 : (topWidth (n + 3) ([cross 0 f, cross 1 f] ++ (strandRight 2 n f ++ [cap 0]))).isSome :=
      isSome_of_eq (topWidth_append_of (wf_cons_cross (by omega) (wf_cons_cross (by omega) (wf_nil _)))
        (topWidth_append_of (strandRight_wf f (by omega)) (wf_cons_cap (by omega) (wf_nil _))))
    have h1 : Iso (n + 3) ([cross 0 f, cross 1 f] ++ (strandRight 2 n f ++ [cap 0]))
        ([cross 0 f, cross 1 f] ++ (cap 0 :: strandRight 0 n f)) := by
      refine Iso.high (fun c _ hc => ?_) hw0
      have := iso_early_farL (b := c) (h := cap 0) (u := strandRight 2 n f)
        (fun g hg => by obtain ⟨j, -, rfl⟩ := mem_strandRight hg; simp [Gen.lo, Gen.ins]) hc
      rwa [strandRight_map (i' := 0) (p := (cap 0).outs) (q := (cap 0).ins) n f rfl] at this
    have hw1 : (topWidth (n + 3) ([] ++ (Mv.capSlideR 0 f).lhs ++ strandRight 0 n f)).isSome := by
      have := Iso.wf h1 hw0; simpa [Mv.lhs] using this
    have h2 := iso_mv (Mv.capSlideR 0 f) rfl hw1
    simp only [Mv.lhs, Mv.rhs, List.nil_append, List.cons_append] at h2
    have h1' : Iso (n + 3) (cross 0 f :: cross 1 f :: (strandRight 2 n f ++ [cap 0]))
        (cross 0 f :: cross 1 f :: cap 0 :: strandRight 0 n f) := by simpa using h1
    simpa using h1'.trans h2
  | i + 1, a, h => by
    obtain ⟨n, rfl⟩ : ∃ n, a = n + 1 := ⟨a - 1, by omega⟩
    have ih := (slide_cap f i n (by omega)).shiftTo (i := 1) (b := n + 2) (by omega)
    simp only [shiftW_append, shiftW_strandRight, shiftW_cons, shiftW_nil, Gen.shift] at ih
    rw [strandRight_succ', show n + 1 - 2 = (n - 2) + 1 by omega, strandRight_succ']
    have hw0 : (topWidth (n + 2) ([cross 0 f] ++ (strandRight 1 n f ++ [cap (i + 1)]))).isSome :=
      isSome_of_eq (topWidth_append_of (wf_cons_cross (by omega) (wf_nil _))
        (topWidth_append_of (strandRight_wf f (by omega)) (wf_cons_cap (by omega) (wf_nil _))))
    have h1 : Iso (n + 2) ([cross 0 f] ++ (strandRight 1 n f ++ [cap (i + 1)]))
        ([cross 0 f] ++ (cap (i + 2) :: strandRight 1 (n - 2) f)) := by
      refine Iso.high (fun c hc _ => ?_) hw0
      rw [wf_cons_cross (by omega) (wf_nil _)] at hc
      cases hc
      exact ih
    have h2 := iso_mv (b := n + 2) (p := []) (q := strandRight 1 (n - 2) f) (Mv.farR (cross 0 f) (cap (i + 2)))
      (farR_ok (by simp [Gen.lo, Gen.outs])) (by have := Iso.wf h1 hw0; simpa [Mv.lhs] using this)
    simp only [Mv.lhs, Mv.rhs, Gen.withLo, Gen.lo, Gen.outs, Gen.ins, List.nil_append, List.cons_append] at h2
    have h1' : Iso (n + 2) (cross 0 f :: (strandRight 1 n f ++ [cap (i + 1)]))
        (cross 0 f :: cap (i + 2) :: strandRight 1 (n - 2) f) := by simpa using h1
    have e : i + 2 - 2 + 2 = i + 1 + 1 := by omega
    rw [e] at h2
    simpa using h1'.trans h2
theorem slide (f : Bool) : ∀ (u : MWord) (a b : Nat), topWidth a u = some b →
    Iso (a + 1) (strandRight 0 a f ++ u) (shiftW 1 u ++ strandRight 0 b f)
Proof
  | [], a, b, h => by
    simp only [topWidth, Option.some.injEq] at h
    subst h
    simpa [shiftW_nil] using Iso.refl (a + 1) (strandRight 0 a f)
  | g :: gs, a, b, h => by
    obtain ⟨hf, hrest⟩ := fits_of_topWidth h
    have hs : Iso (a + 1) (strandRight 0 a f ++ [g]) (g.shift 1 :: strandRight 0 (g.widthAfter a) f) := by
      cases g with
      | cross i t =>
        simp only [Gen.fits, decide_eq_true_eq] at hf
        simpa [Gen.shift, Gen.widthAfter, Nat.add_comm] using slide_cross f t i a hf
      | cup i =>
        simp only [Gen.fits, decide_eq_true_eq] at hf
        simpa [Gen.shift, Gen.widthAfter, Nat.add_comm] using slide_cup f i a hf
      | cap i =>
        simp only [Gen.fits, decide_eq_true_eq] at hf
        simpa [Gen.shift, Gen.widthAfter, Nat.add_comm] using slide_cap f i a hf
    have hg1 : topWidth (a + 1) [g] = some (g.widthAfter a + 1) := by
      cases g with
      | cross i t =>
        simp only [Gen.fits, decide_eq_true_eq] at hf
        exact wf_cons_cross (by omega) (wf_nil _)
      | cup i =>
        simp only [Gen.fits, decide_eq_true_eq] at hf
        rw [show (cup i).widthAfter a + 1 = a + 1 + 2 by simp only [Gen.widthAfter]]
        exact wf_cons_cup (by omega) (wf_nil _)
      | cap i =>
        simp only [Gen.fits, decide_eq_true_eq] at hf
        rw [show (cap i).widthAfter a + 1 = a + 1 - 2 by simp only [Gen.widthAfter]; omega]
        exact wf_cons_cap (by omega) (wf_nil _)
    have hg2 : topWidth (a + 1) [g.shift 1] = some (g.widthAfter a + 1) := by
      cases g with
      | cross i t =>
        simp only [Gen.fits, decide_eq_true_eq] at hf
        exact wf_cons_cross (by omega) (wf_nil _)
      | cup i =>
        simp only [Gen.fits, decide_eq_true_eq] at hf
        rw [show (cup i).widthAfter a + 1 = a + 1 + 2 by simp only [Gen.widthAfter]]
        exact wf_cons_cup (by omega) (wf_nil _)
      | cap i =>
        simp only [Gen.fits, decide_eq_true_eq] at hf
        rw [show (cap i).widthAfter a + 1 = a + 1 - 2 by simp only [Gen.widthAfter]; omega]
        exact wf_cons_cap (by omega) (wf_nil _)
    have hr1 : topWidth (g.widthAfter a + 1) gs = some (b + 1) := by
      have := topWidth_shift hrest 0 1
      simpa [shiftW_zero] using this
    have hw : (topWidth (a + 1) (strandRight 0 a f ++ [g] ++ gs)).isSome :=
      isSome_of_eq (topWidth_append_of (topWidth_append_of (strandRight_wf f (by omega)) hg1) hr1)
    have h1 := Iso.low (Q := gs) (fun _ => hs) hw
    have h2 : Iso (a + 1) ([g.shift 1] ++ (strandRight 0 (g.widthAfter a) f ++ gs))
        ([g.shift 1] ++ (shiftW 1 gs ++ strandRight 0 b f)) := Iso.prefix hg2 (slide f gs _ b hrest)
    have h1' : Iso (a + 1) (strandRight 0 a f ++ g :: gs)
        (g.shift 1 :: (strandRight 0 (g.widthAfter a) f ++ gs)) := by simpa using h1
    simpa [shiftW_cons] using h1'.trans h2

The pieces of the drawings, one entry at a time

def prAux (i k : Nat) (l : List Bool) : MWord :=
  (l.zipIdx k |>.map fun (over, j) => [Gen.cross (i + 1 + j) over, Gen.cross (i + j) over]).flatten
theorem prAux_cons (i k : Nat) (o : Bool) (l : List Bool) :
    prAux i k (o :: l) = cross (i + 1 + k) o :: cross (i + k) o :: prAux i (k + 1) l
Proof
:= by
  simp [prAux]
theorem prAux_succ (i k : Nat) (l : List Bool) : prAux i (k + 1) l = prAux (i + 1) k l
Proof
:= by
  induction l generalizing k with
  | nil => rfl
  | cons o l ih =>
    rw [prAux_cons, prAux_cons, ih, show i + 1 + (k + 1) = i + 1 + 1 + k by omega,
      show i + (k + 1) = i + 1 + k by omega]
theorem pairRight_nil (i : Nat) : pairRight i [] = []
Proof
:= rfl
theorem pairRight_cons (i : Nat) (o : Bool) (l : List Bool) :
    pairRight i (o :: l) = cross (i + 1) o :: cross i o :: pairRight (i + 1) l
Proof
:= by
  have := prAux_cons i 0 o l
  rw [prAux_succ] at this
  exact this
theorem pairRight_append (i : Nat) (l₁ l₂ : List Bool) :
    pairRight i (l₁ ++ l₂) = pairRight i l₁ ++ pairRight (i + l₁.length) l₂
Proof
:= by
  induction l₁ generalizing i with
  | nil => simp [pairRight_nil]
  | cons o l ih =>
    rw [List.cons_append, pairRight_cons, pairRight_cons, ih]
    simp only [List.length_cons, List.cons_append]
    rw [show i + 1 + l.length = i + (l.length + 1) by omega]
theorem mem_pairRight {g : Gen} {i : Nat} {l : List Bool} (h : g ∈ pairRight i l) :
    ∃ k s, g = cross k s ∧ i ≤ k ∧ k ≤ i + l.length
Proof
:= by
  induction l generalizing i with
  | nil => simp [pairRight_nil] at h
  | cons o l ih =>
    rw [pairRight_cons] at h
    simp only [List.mem_cons] at h
    rcases h with rfl | rfl | h
    · exact ⟨_, _, rfl, by omega, by simp⟩
    · exact ⟨_, _, rfl, by omega, by simp⟩
    · obtain ⟨k, s, rfl, h1, h2⟩ := ih h
      exact ⟨k, s, rfl, by omega, by simp; omega⟩
theorem shiftW_pairRight (k i : Nat) (l : List Bool) : shiftW k (pairRight i l) = pairRight (i + k) l
Proof
:= by
  induction l generalizing i with
  | nil => rfl
  | cons o l ih =>
    rw [pairRight_cons, pairRight_cons, shiftW_cons, shiftW_cons, ih]
    simp only [Gen.shift]
    rw [show i + 1 + k = i + k + 1 by omega]
def plAux (i k : Nat) (l : List Bool) : MWord :=
  (l.zipIdx k |>.map fun (over, j) => [Gen.cross (i - 1 - j) (!over), Gen.cross (i - j) (!over)]).flatten
theorem plAux_cons (i k : Nat) (o : Bool) (l : List Bool) :
    plAux i k (o :: l) = cross (i - 1 - k) (!o) :: cross (i - k) (!o) :: plAux i (k + 1) l
Proof
:= by
  simp [plAux]
theorem plAux_succ (i k : Nat) (l : List Bool) : plAux i (k + 1) l = plAux (i - 1) k l
Proof
:= by
  induction l generalizing k with
  | nil => rfl
  | cons o l ih =>
    rw [plAux_cons, plAux_cons, ih, show i - 1 - (k + 1) = i - 1 - 1 - k by omega,
      show i - (k + 1) = i - 1 - k by omega]
theorem pairLeft_cons (i : Nat) (o : Bool) (l : List Bool) :
    pairLeft i (o :: l) = cross (i - 1) (!o) :: cross i (!o) :: pairLeft (i - 1) l
Proof
:= by
  have := plAux_cons i 0 o l
  rw [plAux_succ] at this
  exact this
theorem pairLeft_append (i : Nat) (l₁ l₂ : List Bool) :
    pairLeft i (l₁ ++ l₂) = pairLeft i l₁ ++ pairLeft (i - l₁.length) l₂
Proof
:= by
  induction l₁ generalizing i with
  | nil => rfl
  | cons o l ih =>
    rw [List.cons_append, pairLeft_cons, pairLeft_cons, ih]
    simp only [List.length_cons, List.cons_append]
    rw [show i - 1 - l.length = i - (l.length + 1) by omega]
theorem shiftW_pairLeft (k i : Nat) (l : List Bool) (h : l.length ≤ i) :
    shiftW k (pairLeft i l) = pairLeft (i + k) l
Proof
:= by
  induction l generalizing i with
  | nil => rfl
  | cons o l ih =>
    simp only [List.length_cons] at h
    rw [pairLeft_cons, pairLeft_cons, shiftW_cons, shiftW_cons, ih _ (by omega)]
    simp only [Gen.shift]
    rw [show i - 1 + k = i + k - 1 by omega]
theorem alternate_add_two (b : Bool) (k : Nat) : alternate b (k + 2) = b :: (!b) :: alternate b k
Proof
:= by
  simp [alternate]
theorem alternate_two (b : Bool) (m : Nat) : alternate b (2 * m + 2) = alternate b (2 * m) ++ [b, !b]
Proof
:= by
  rw [alternate_add_two]
  induction m with
  | zero => rfl
  | succ m ih =>
    rw [show 2 * (m + 1) = 2 * m + 2 by omega, alternate_add_two, List.cons_append, List.cons_append, ← ih]
theorem length_alternate (b : Bool) (k : Nat) : (alternate b k).length = k
Proof
:= by
  induction k generalizing b with
  | zero => rfl
  | succ k ih => simp [alternate, ih]
theorem closeLeft_succ (y k : Nat) : closeLeft y (k + 1) = cap (y - 2) :: closeLeft (y - 2) k
Proof
:= rfl
theorem shiftW_closeLeft (j y k : Nat) (h : 2 * k ≤ y) : shiftW j (closeLeft y k) = closeLeft (y + j) k
Proof
:= by
  induction k generalizing y with
  | zero => rfl
  | succ k ih =>
    rw [closeLeft_succ, closeLeft_succ, shiftW_cons, ih _ (by omega)]
    simp only [Gen.shift]
    rw [show y - 2 + j = y + j - 2 by omega]
theorem closeLeft_snoc (y k : Nat) : closeLeft y (k + 1) = closeLeft y k ++ [cap (y - 2 * k - 2)]
Proof
:= by
  induction k generalizing y with
  | zero => rfl
  | succ k ih =>
    rw [closeLeft_succ, ih, closeLeft_succ]
    simp only [List.cons_append]
    rw [show y - 2 - 2 * k - 2 = y - 2 * (k + 1) - 2 by omega]
theorem closeLeft_wf (k : Nat) : ∀ (y w : Nat), 2 * k ≤ y → y ≤ w →
    topWidth w (closeLeft y k) = some (w - 2 * k)
Proof
:= by
  induction k with
  | zero => intro y w _ _; rfl
  | succ k ih =>
    intro y w h1 h2
    rw [closeLeft_succ, wf_cons_cap (by omega) (ih _ _ (by omega) (by omega))]
    congr 1; omega
theorem mem_closeLeft {g : Gen} {y k : Nat} (h : g ∈ closeLeft y k) : ∃ j, g = cap j ∧ y - 2 * k ≤ j
Proof
:= by
  induction k generalizing y with
  | zero => simp [closeLeft] at h
  | succ k ih =>
    rw [closeLeft_succ] at h
    simp only [List.mem_cons] at h
    rcases h with rfl | h
    · exact ⟨_, rfl, by omega⟩
    · obtain ⟨j, rfl, hj⟩ := ih h
      exact ⟨j, rfl, by omega⟩
theorem strandLeft_succ (i k : Nat) (o : Bool) :
    strandLeft i (k + 1) o = strandLeft i k o ++ [cross (i - 1 - k) (!o)]
Proof
:= by
  simp [strandLeft, List.range_succ]
theorem strandLeft_succ' (i k : Nat) (o : Bool) :
    strandLeft i (k + 1) o = cross (i - 1) (!o) :: strandLeft (i - 1) k o
Proof
:= by
  simp only [strandLeft, List.range_succ_eq_map, List.map_cons, List.map_map, Nat.sub_zero]
  congr 1
  apply List.map_congr_left
  intro j _
  simp only [Function.comp, Nat.succ_eq_add_one]
  congr 1; omega
theorem strandLeft_add (i m n : Nat) (o : Bool) :
    strandLeft i (m + n) o = strandLeft i m o ++ strandLeft (i - m) n o
Proof
:= by
  induction m generalizing i with
  | zero => simp [strandLeft]
  | succ m ih =>
    rw [show m + 1 + n = (m + n) + 1 by omega, strandLeft_succ', strandLeft_succ', ih]
    simp only [List.cons_append]
    rw [show i - 1 - m = i - (m + 1) by omega]
theorem shiftW_strandLeft (k i n : Nat) (o : Bool) (h : n ≤ i) :
    shiftW k (strandLeft i n o) = strandLeft (i + k) n o
Proof
:= by
  simp only [shiftW, strandLeft, List.map_map]
  apply List.map_congr_left
  intro j hj
  rw [List.mem_range] at hj
  simp only [Function.comp, Gen.shift]
  congr 1; omega
theorem mem_strandLeft {g : Gen} {i n : Nat} {o : Bool} (h : g ∈ strandLeft i n o) :
    ∃ j, j < n ∧ g = cross (i - 1 - j) (!o)
Proof
:= by
  simp only [strandLeft, List.mem_map, List.mem_range] at h
  obtain ⟨j, hj, rfl⟩ := h
  exact ⟨j, hj, rfl⟩
theorem strandLeft_wf {w i n : Nat} (o : Bool) (h : n ≤ i) (hi : i < w) :
    topWidth w (strandLeft i n o) = some w
Proof
:=
  topWidth_crosses fun g hg => by
    obtain ⟨j, hj, rfl⟩ := mem_strandLeft hg
    exact ⟨_, _, rfl, by omega⟩
theorem shiftW_map_withLo {k k' p q : Nat} (v : MWord) (h : k + p = k' + q) :
    (shiftW k v).map (fun g => g.withLo (g.lo + p - q)) = shiftW k' v
Proof
:= by
  simp only [shiftW, List.map_map]
  apply List.map_congr_left
  intro g _
  cases g <;> simp only [Function.comp, Gen.shift, Gen.withLo, Gen.lo] <;> congr 1 <;> omega
theorem lo_shift_ge {g : Gen} {k : Nat} {v : MWord} (hg : g ∈ shiftW k v) : ∃ g₀ ∈ v, g.lo = g₀.lo + k
Proof
:= by
  simp only [shiftW, List.mem_map] at hg
  obtain ⟨g₀, h0, rfl⟩ := hg
  exact ⟨g₀, h0, Gen.shift_lo g₀ k⟩
theorem capsPast {b y : Nat} (v : MWord) (hv : ∀ g ∈ v, y ≤ g.lo) : ∀ (m : Nat),
    (topWidth b (shiftW (2 * m) v ++ closeLeft (y + 2 * m) m)).isSome →
    Iso b (shiftW (2 * m) v ++ closeLeft (y + 2 * m) m) (closeLeft (y + 2 * m) m ++ v)
Proof
  | 0, _ => by simpa [shiftW_zero, closeLeft] using Iso.refl b v
  | m + 1, hw => by
    rw [show y + 2 * (m + 1) = (y + 2 * m) + 2 by omega, closeLeft_succ, Nat.add_sub_cancel] at hw ⊢
    have h1 : Iso b (shiftW (2 * (m + 1)) v ++ [cap (y + 2 * m)] ++ closeLeft (y + 2 * m) m)
        ((cap (y + 2 * m) :: shiftW (2 * m) v) ++ closeLeft (y + 2 * m) m) := by
      refine Iso.low (fun hc => ?_) (by simpa using hw)
      have := iso_early_farL (b := b) (h := cap (y + 2 * m)) (u := shiftW (2 * (m + 1)) v)
        (fun g hg => by
          obtain ⟨g₀, h0, he⟩ := lo_shift_ge hg
          have := hv g₀ h0
          show y + 2 * m + 2 ≤ g.lo
          omega) hc
      rwa [shiftW_map_withLo (k' := 2 * m) v (by simp [Gen.outs, Gen.ins]; omega)] at this
    have h2 : Iso b ([cap (y + 2 * m)] ++ (shiftW (2 * m) v ++ closeLeft (y + 2 * m) m))
        ([cap (y + 2 * m)] ++ (closeLeft (y + 2 * m) m ++ v)) :=
      Iso.high (fun c _ hc => capsPast v hv m hc) (by have := Iso.wf h1 (by simpa using hw); simpa using this)
    have h1' : Iso b (shiftW (2 * (m + 1)) v ++ cap (y + 2 * m) :: closeLeft (y + 2 * m) m)
        (cap (y + 2 * m) :: (shiftW (2 * m) v ++ closeLeft (y + 2 * m) m)) := by simpa using h1
    simpa using h1'.trans h2
theorem iso_comm_crosses {b : Nat} (T : MWord) : ∀ {U : MWord},
    (∀ g ∈ U, ∃ i s, g = cross i s ∧ ∀ t ∈ T, i + 2 ≤ t.lo) →
    (topWidth b (T ++ U)).isSome → Iso b (T ++ U) (U ++ T)
Proof
  | [], _, _ => by simpa using Iso.refl b T
  | g :: U, hU, hw => by
    obtain ⟨i, s, rfl, hi⟩ := hU g List.mem_cons_self
    have h1 : Iso b (T ++ [cross i s] ++ U) ((cross i s :: T) ++ U) := by
      refine Iso.low (fun hc => ?_) (by simpa using hw)
      have := iso_early_farL (b := b) (h := cross i s) (u := T) (fun t ht => by
        have := hi t ht; show i + 2 ≤ t.lo; omega) hc
      have e : T.map (fun g => g.withLo (g.lo + (cross i s).outs - (cross i s).ins)) = T := by
        apply List.map_id''
        intro g
        cases g <;> simp [Gen.withLo, Gen.lo, Gen.outs, Gen.ins]
      rwa [e] at this
    have h2 : Iso b ([cross i s] ++ (T ++ U)) ([cross i s] ++ (U ++ T)) :=
      Iso.high (fun c _ hc => iso_comm_crosses T (fun g hg => hU g (by simp [hg])) hc)
        (by have := Iso.wf h1 (by simpa using hw); simpa using this)
    have h1' : Iso b (T ++ cross i s :: U) (cross i s :: (T ++ U)) := by simpa using h1
    simpa using h1'.trans h2

The shorthand: a knit to the left through a stack

def capsW (s : Bool) : Nat → MWord
  | 0 => []
  | n + 1 => [cross (n + 3) s, cross (n + 2) s, cap (n + 1)] ++ capsW s n

def shortW (s : Bool) (c : Nat) : MWord :=
  nestK 0 c ++ strandLeft (2 * c) c (!s) ++ [cup c] ++ strandLeft c c (!s) ++ capsW s c

def mirrorCross (w : Nat) : Gen → Gen
  | cross i lo => cross (w - 2 - i) (!lo)
  | g => g
theorem mirrorAux_crosses (w : Nat) (v : MWord) : ∀ {u : MWord}, (∀ g ∈ u, ∃ i lo, g = cross i lo) →
    mirrorAux w (u ++ v) = u.map (mirrorCross w) ++ mirrorAux w v
Proof
  | [], _ => rfl
  | g :: u, h => by
    obtain ⟨i, lo, rfl⟩ := h g List.mem_cons_self
    simp only [List.cons_append, mirrorAux, Gen.widthAfter, List.map_cons, mirrorCross]
    rw [mirrorAux_crosses w v (fun g hg => h g (List.mem_cons_of_mem _ hg))]
theorem mirrorAux_pullTail (j : Nat) :
    mirrorAux (2 * j + 3) ((List.range j).reverse.flatMap fun j =>
      [Gen.cross j false, Gen.cross (j + 1) false, Gen.cap (j + 2)]) = capsW true j
Proof
:= by
  induction j with
  | zero => rfl
  | succ j ih =>
    rw [List.range_succ, List.reverse_append, List.reverse_singleton, List.singleton_append, List.flatMap_cons]
    simp only [List.cons_append, List.nil_append, mirrorAux, Gen.widthAfter, Bool.not_false]
    rw [show 2 * (j + 1) + 3 - 2 = 2 * j + 3 by omega, ih]
    simp only [capsW, List.cons_append, List.nil_append]
    rw [show 2 * j + 3 - j = j + 3 by omega, show 2 * j + 3 - (j + 1) = j + 2 by omega,
      show 2 * j + 3 - (j + 2) = j + 1 by omega]
theorem mirrorAux_pullThrough (n : Nat) :
    mirrorAux (2 * n + 1) (pullThrough n) =
      strandLeft (2 * n) n false ++ [cup n] ++ strandLeft n n false ++ capsW true n
Proof
:= by
  have hA : ((List.range n).map fun k => Gen.cross k false).map (mirrorCross (2 * n + 1)) =
      strandLeft (2 * n) n false := by
    simp only [List.map_map, strandLeft]
    apply List.map_congr_left
    intro k hk
    rw [List.mem_range] at hk
    simp only [Function.comp, mirrorCross, Bool.not_false]
    rw [show 2 * n + 1 - 2 - k = 2 * n - 1 - k by omega]
  have hB : ((List.range n).map fun k => Gen.cross (n + 2 + k) false).map (mirrorCross (2 * n + 1 + 2)) =
      strandLeft n n false := by
    simp only [List.map_map, strandLeft]
    apply List.map_congr_left
    intro k hk
    rw [List.mem_range] at hk
    simp only [Function.comp, mirrorCross, Bool.not_false]
    congr 1; omega
  have hc : ∀ (u : List Nat) (f : Nat → Nat),
      (∀ g ∈ u.map (fun k => Gen.cross (f k) false), ∃ i lo, g = cross i lo) := by
    intro u f g hg
    simp only [List.mem_map] at hg
    obtain ⟨k, -, rfl⟩ := hg
    exact ⟨_, _, rfl⟩
  unfold pullThrough
  rw [List.append_assoc, List.append_assoc, mirrorAux_crosses _ _ (hc _ _), hA, List.singleton_append]
  simp only [mirrorAux, Gen.widthAfter]
  rw [mirrorAux_crosses _ _ (hc _ _), hB, show 2 * n + 1 + 2 = 2 * n + 3 by omega, mirrorAux_pullTail,
    show 2 * n + 1 - (n + 1) = n by omega]
  simp [List.append_assoc]
theorem reverseCrossings_append (u v : MWord) :
    reverseCrossings (u ++ v) = reverseCrossings u ++ reverseCrossings v
Proof
:= by
  simp [reverseCrossings]
theorem mirrorAux_reverseCrossings : ∀ (w : Nat) (u : MWord),
    mirrorAux w (reverseCrossings u) = reverseCrossings (mirrorAux w u)
Proof
  | _, [] => rfl
  | w, .cross i lo :: u => by
    show cross (w - 2 - i) (!!lo) :: mirrorAux w (reverseCrossings u) =
      cross (w - 2 - i) (!!lo) :: reverseCrossings (mirrorAux w u)
    rw [mirrorAux_reverseCrossings w u]
  | w, .cup i :: u => by
    show cup (w - i) :: mirrorAux (w + 2) (reverseCrossings u) = cup (w - i) :: reverseCrossings (mirrorAux (w + 2) u)
    rw [mirrorAux_reverseCrossings (w + 2) u]
  | w, .cap i :: u => by
    show cap (w - 2 - i) :: mirrorAux (w - 2) (reverseCrossings u) =
      cap (w - 2 - i) :: reverseCrossings (mirrorAux (w - 2) u)
    rw [mirrorAux_reverseCrossings (w - 2) u]
theorem reverseCrossings_strandLeft (i k : Nat) (o : Bool) :
    reverseCrossings (strandLeft i k o) = strandLeft i k (!o)
Proof
:= by
  simp [reverseCrossings, strandLeft]
theorem reverseCrossings_capsW (s : Bool) : ∀ n, reverseCrossings (capsW s n) = capsW (!s) n
Proof
  | 0 => rfl
  | n + 1 => by
    simp only [capsW, reverseCrossings_append]
    rw [reverseCrossings_capsW s n]
    rfl
theorem knitInterior_L (bd : Bed) (c : Nat) : knitInterior .L bd c = shortW (decide (bd = .f)) c
Proof
:= by
  cases bd
  · show nestK 0 c ++ mirrorAux (2 * c + 1) (pullThrough c) = _
    rw [mirrorAux_pullThrough]
    simp [shortW, List.append_assoc]
  · show nestK 0 c ++ mirrorAux (2 * c + 1) (reverseCrossings (pullThrough c)) = _
    rw [mirrorAux_reverseCrossings, mirrorAux_pullThrough]
    simp only [reverseCrossings_append, reverseCrossings_strandLeft, reverseCrossings_capsW]
    simp [shortW, reverseCrossings, List.append_assoc]
theorem shortW_wf (s : Bool) (c : Nat) : topWidth (2 * c + 1) (shortW s c) = some 3
Proof
:= by
  cases s
  · have := knitInterior_wf .L .b c
    rwa [knitInterior_L] at this
  · have := knitInterior_wf .L .f c
    rwa [knitInterior_L] at this
theorem D_cons_nil {r r' : Row} {s : Step} (ss : List Step) (h : runStep r s = some (r', [])) :
    D r (s :: ss) = D r' ss
Proof
:= by
  simp only [D, h]
  cases D r' ss <;> simp
theorem D_merges (ss : List Step) : ∀ (m a : Nat), 0 < a →
    D (.stack a :: (List.replicate m (.stack 1) ++ [.yarn])) (List.replicate m (.merge 0) ++ ss) =
      D [.stack (a + m), .yarn] ss
Proof
  | 0, a, _ => rfl
  | m + 1, a, ha => by
    rw [List.replicate_succ, List.replicate_succ, List.cons_append, List.cons_append, D_cons_nil _ (r' :=
      .stack (a + 1) :: (List.replicate m (.stack 1) ++ [.yarn]))]
    · rw [D_merges ss m (a + 1) (by omega), show a + 1 + m = a + (m + 1) by omega]
    · simp [runStep, stackAt, ha, splice]
theorem D_ktog (bd : Bed) (c : Nat) (hc : 0 < c) :
    D (List.replicate c (.stack 1) ++ [.yarn]) (List.replicate (c - 1) (.merge 0) ++ [.knit .L bd]) =
      some ([.yarn, .stack 1], shortW (decide (bd = .f)) c)
Proof
:= by
  obtain ⟨n, rfl⟩ : ∃ n, c = n + 1 := ⟨c - 1, by omega⟩
  rw [List.replicate_succ, List.cons_append, Nat.add_sub_cancel, D_merges _ n 1 (by omega),
    ← knitInterior_L, Nat.add_comm 1 n]
  simp [D, runStep, hasBeside, yarnIdx, besideIdx, stackAt, firstIdx, splice, tuckRow, offset, rowWidth,
    shiftW_zero]

The shorthand: the outside loop of the stack

theorem swapBlocks_one (a r : Nat) (lo : Bool) : swapBlocks a 1 r lo = strandRight a r lo
Proof
:= by
  simp only [swapBlocks, strandRight, List.range_one, List.map_cons, List.map_nil]
  induction r with
  | zero => rfl
  | succ r ih =>
    rw [List.range_succ, List.flatMap_append, ih, List.map_append]
    simp
theorem nestK_succ (c : Nat) : nestK 0 (c + 1) = strandRight 1 (2 * c) true ++ shiftW 1 (nestK 0 c)
Proof
:= by
  unfold nestK
  rw [List.range_succ_eq_map, List.flatMap_cons, List.flatMap_map]
  simp only [Nat.zero_add, Nat.sub_zero, Nat.add_sub_cancel]
  rw [swapBlocks_one, shiftW, List.map_flatMap]
  congr 1
  apply List.flatMap_congr
  intro i hi
  rw [List.mem_range] at hi
  rw [← shiftW, shiftW_swapBlocks, Nat.succ_eq_add_one, show c + 1 - (i + 1) - 1 = c - i - 1 by omega]
theorem mem_nestK {g : Gen} {c : Nat} (h : g ∈ nestK 0 c) : ∃ i, g = cross i true ∧ i + 2 ≤ 2 * c
Proof
:= by
  simp only [nestK, List.mem_flatMap, List.mem_range] at h
  obtain ⟨i, hi, hg⟩ := h
  rw [swapBlocks_one] at hg
  obtain ⟨j, hj, rfl⟩ := mem_strandRight hg
  exact ⟨_, rfl, by omega⟩
theorem capsW_succ' (s : Bool) : ∀ c, capsW s (c + 1) = shiftW 1 (capsW s c) ++ [cross 3 s, cross 2 s, cap 1]
Proof
  | 0 => rfl
  | c + 1 => by
    have e1 : capsW s (c + 1 + 1) = [cross (c + 1 + 3) s, cross (c + 1 + 2) s, cap (c + 1 + 1)] ++ capsW s (c + 1) :=
      rfl
    have e2 : capsW s (c + 1) = [cross (c + 3) s, cross (c + 2) s, cap (c + 1)] ++ capsW s c := rfl
    conv_rhs => rw [e2]
    rw [e1, capsW_succ' s c, shiftW_append]
    rfl
theorem mem_capsW {g : Gen} {s : Bool} : ∀ {c : Nat}, g ∈ capsW s c → 1 ≤ g.lo
Proof
  | 0, h => by simp [capsW] at h
  | c + 1, h => by
    simp only [capsW, List.cons_append, List.nil_append, List.mem_cons] at h
    rcases h with rfl | rfl | rfl | h
    · simp [Gen.lo]
    · simp [Gen.lo]
    · simp [Gen.lo]
    · exact mem_capsW h
theorem shortW_succ (s : Bool) (c : Nat) :
    shortW s (c + 1) = strandRight 1 (2 * c) true ++ shiftW 1 (nestK 0 c) ++ [cross (2 * c + 1) s] ++
      shiftW 1 (strandLeft (2 * c) c (!s)) ++ [cup (c + 1)] ++ shiftW 1 (strandLeft c c (!s)) ++ [cross 0 s] ++
      shiftW 1 (capsW s c) ++ [cross 3 s, cross 2 s, cap 1]
Proof
:= by
  rw [shortW, nestK_succ, show 2 * (c + 1) = 2 * c + 1 + 1 by omega, strandLeft_succ', Nat.add_sub_cancel,
    shiftW_strandLeft _ _ _ _ (by omega), strandLeft_succ, shiftW_strandLeft _ _ _ _ (le_refl c), capsW_succ',
    Bool.not_not, show c + 1 - 1 - c = 0 by omega]
  simp [List.append_assoc]
theorem shortW_peel (s : Bool) (c : Nat) :
    Iso (2 * c + 3) (shortW s (c + 1))
      (strandRight 1 (2 * c) true ++ [cross (2 * c + 1) s] ++ shiftW 1 (shortW s c) ++
        [cross 0 s, cross 3 s, cross 2 s, cap 1])
Proof
:= by
  have hw : (topWidth (2 * c + 3) (shortW s (c + 1))).isSome := isSome_of_eq (shortW_wf s (c + 1))
  have e0 : shortW s (c + 1) = strandRight 1 (2 * c) true ++ (shiftW 1 (nestK 0 c) ++ [cross (2 * c + 1) s]) ++
      (shiftW 1 (strandLeft (2 * c) c (!s)) ++ [cup (c + 1)] ++ shiftW 1 (strandLeft c c (!s)) ++ [cross 0 s] ++
        shiftW 1 (capsW s c) ++ [cross 3 s, cross 2 s, cap 1]) := by
    rw [shortW_succ]; simp [List.append_assoc]
  rw [e0] at hw ⊢
  have h1 := Iso.mid (fun d _ hd => iso_early_farR (fun g hg => by
    simp only [shiftW, List.mem_map] at hg
    obtain ⟨g₀, h0, rfl⟩ := hg
    obtain ⟨i, rfl, hi⟩ := mem_nestK h0
    exact ⟨_, _, rfl, by simp [Gen.lo]; omega⟩) hd) hw
  have hw1 := Iso.wf h1 hw
  have e1 : strandRight 1 (2 * c) true ++ (cross (2 * c + 1) s :: shiftW 1 (nestK 0 c)) ++
      (shiftW 1 (strandLeft (2 * c) c (!s)) ++ [cup (c + 1)] ++ shiftW 1 (strandLeft c c (!s)) ++ [cross 0 s] ++
        shiftW 1 (capsW s c) ++ [cross 3 s, cross 2 s, cap 1]) =
      (strandRight 1 (2 * c) true ++ [cross (2 * c + 1) s] ++ shiftW 1 (nestK 0 c) ++
        shiftW 1 (strandLeft (2 * c) c (!s)) ++ [cup (c + 1)] ++ shiftW 1 (strandLeft c c (!s))) ++
      (cross 0 s :: shiftW 1 (capsW s c)) ++ [cross 3 s, cross 2 s, cap 1] := by
    simp [List.append_assoc]
  rw [e1] at hw1
  have h2 := Iso.mid (fun d _ hd => by
    obtain ⟨d', hd'⟩ := Option.isSome_iff_exists.mp hd
    exact iso_pass_left (fun g hg => by
      simp only [shiftW, List.mem_map] at hg
      obtain ⟨g₀, h0, rfl⟩ := hg
      have := mem_capsW h0
      rw [Gen.shift_lo]; omega) hd') hw1
  refine h1.trans ?_
  rw [e1]
  refine h2.trans ?_
  rw [shortW]
  simp only [shiftW_append, shiftW_cons, Gen.shift, List.append_assoc, List.cons_append, List.nil_append]
  exact Iso.refl _ _
def kF : MWord := [cross 1 true, cross 2 true, cross 3 true, cross 0 true, cross 3 true, cross 2 true, cap 1]
theorem kShort_succ (c : Nat) : Iso (2 * c + 3) (shortW true (c + 1)) (shiftW 2 (shortW true c) ++ kF)
Proof
:= by
  have e : strandRight 1 (2 * c) true ++ [cross (2 * c + 1) true] = strandRight 1 (2 * c + 1) true := by
    rw [strandRight_succ, Nat.add_comm 1 (2 * c)]
  refine (shortW_peel true c).trans ?_
  rw [e]
  have hs := (slide true (shortW true c) (2 * c + 1) 3 (shortW_wf true c)).shiftTo (i := 1) (b := 2 * c + 3)
    (by omega)
  simp only [shiftW_append, shiftW_strandRight, shiftW_shiftW] at hs
  have hw : (topWidth (2 * c + 3) (strandRight 1 (2 * c + 1) true ++ shiftW 1 (shortW true c) ++
      [cross 0 true, cross 3 true, cross 2 true, cap 1])).isSome := by
    have := Iso.wf (shortW_peel true c) (isSome_of_eq (shortW_wf true (c + 1)))
    rwa [e] at this
  have := Iso.low (Q := [cross 0 true, cross 3 true, cross 2 true, cap 1]) (fun _ => hs) hw
  simpa [kF, strandRight, List.range_succ, List.append_assoc] using this

Hand knitting: knit through c loops

def kPre (c : Nat) : MWord := [cup (2 * c)] ++ pairLeft (2 * c + 1) [true] ++ pairLeft (2 * c) (alternate false (2 * c))

def kRaw (c : Nat) : MWord :=
  kPre c ++ pairRight 0 (List.replicate (2 * c) true) ++ pairRight (2 * c) [true] ++ closeLeft (2 * c) c

def kHand (c : Nat) : MWord := kPre c ++ closeLeft (2 * c + 2) c ++ [cross 1 true, cross 0 true]
theorem draw_knitTog (n : Nat) :
    draw (start (n + 1) .back) (work .along (n + 1) ++ [.letOff .left (n + 1)]) =
      some (oneLoop .back, kRaw (n + 1))
Proof
:= by
  simp [draw, work, step, wrapRight, takeOutRight, wrapWord, takeOutWord, State.yarnAt, State.between,
    State.isBack, start, canInsert, room, oneLoop, strandRight, strandLeft, pairRight, kRaw, kPre]
theorem mem_pairLeft {g : Gen} {i : Nat} {l : List Bool} (h : g ∈ pairLeft i l) :
    ∃ k s, g = cross k s ∧ k ≤ i
Proof
:= by
  induction l generalizing i with
  | nil => simp [pairLeft] at h
  | cons o l ih =>
    rw [pairLeft_cons] at h
    simp only [List.mem_cons] at h
    rcases h with rfl | rfl | h
    · exact ⟨_, _, rfl, by omega⟩
    · exact ⟨_, _, rfl, by omega⟩
    · obtain ⟨k, s, rfl, h1⟩ := ih h
      exact ⟨k, s, rfl, by omega⟩
theorem kPre_wf (c : Nat) : topWidth (2 * c + 1) (kPre c) = some (2 * c + 3)
Proof
:= by
  unfold kPre
  refine topWidth_append_of (b := 2 * c + 3) (topWidth_append_of (b := 2 * c + 3)
    (wf_cons_cup (by omega) (wf_nil _)) ?_) ?_
  · exact topWidth_crosses fun g hg => by
      obtain ⟨k, s, rfl, hk⟩ := mem_pairLeft hg; exact ⟨k, s, rfl, by omega⟩
  · exact topWidth_crosses fun g hg => by
      obtain ⟨k, s, rfl, hk⟩ := mem_pairLeft hg; exact ⟨k, s, rfl, by omega⟩
theorem pairRight_wf {w i : Nat} {l : List Bool} (h : i + l.length + 1 < w) : topWidth w (pairRight i l) = some w
Proof
:=
  topWidth_crosses fun g hg => by obtain ⟨k, s, rfl, h1, h2⟩ := mem_pairRight hg; exact ⟨k, s, rfl, by omega⟩
theorem kRaw_wf (c : Nat) : topWidth (2 * c + 1) (kRaw c) = some 3
Proof
:= by
  unfold kRaw
  refine topWidth_append_of (topWidth_append_of (topWidth_append_of (kPre_wf c)
    (pairRight_wf (by simp only [List.length_replicate]; omega))) (pairRight_wf (by simp))) ?_
  rw [closeLeft_wf c _ _ (by omega) (by omega)]
  congr 1; omega
theorem kHand_wf (c : Nat) : topWidth (2 * c + 1) (kHand c) = some 3
Proof
:= by
  unfold kHand
  refine topWidth_append_of (topWidth_append_of (kPre_wf c) (closeLeft_wf c _ _ (by omega) (by omega))) ?_
  rw [show 2 * c + 3 - 2 * c = 3 by omega]
  rfl
theorem bandCap0 : Iso 4 [cross 1 true, cross 0 true, cross 2 true, cross 1 true, cap 0] [cap 2]
Proof
:=
  iso_of_checkChain _ [(1, .farR (.cross 0 true) (.cross 2 true), true), (2, .capSlideR 0 true, true),
    (0, .capSlideR 1 true, true)] _ (by decide)
theorem bandCap (i b : Nat) (hb : i + 4 ≤ b) :
    Iso b [cross (i + 1) true, cross i true, cross (i + 2) true, cross (i + 1) true, cap i] [cap (i + 2)]
Proof
:= by
  have := bandCap0.shiftTo (i := i) (b := b) hb
  simpa [shiftW, Gen.shift, Nat.add_comm] using this
theorem bandCaps : ∀ (c b : Nat), 2 * c + 2 ≤ b →
    (topWidth b (pairRight 0 (List.replicate (2 * c) true) ++ closeLeft (2 * c) c)).isSome →
    Iso b (pairRight 0 (List.replicate (2 * c) true) ++ closeLeft (2 * c) c) (closeLeft (2 * c + 2) c)
Proof
  | 0, b, _, _ => Iso.refl _ _
  | c + 1, b, hb, hw => by
    have e1 : pairRight 0 (List.replicate (2 * (c + 1)) true) =
        pairRight 0 (List.replicate (2 * c) true) ++ pairRight (2 * c) [true, true] := by
      rw [show 2 * (c + 1) = 2 * c + 2 by omega, List.replicate_add, pairRight_append, List.length_replicate,
        Nat.zero_add]
      rfl
    have e2 : closeLeft (2 * (c + 1)) (c + 1) = cap (2 * c) :: closeLeft (2 * c) c := by
      rw [closeLeft_succ, show 2 * (c + 1) - 2 = 2 * c by omega]
    have e3 : closeLeft (2 * (c + 1) + 2) (c + 1) = cap (2 * c + 2) :: closeLeft (2 * c + 2) c := by
      rw [closeLeft_succ, show 2 * (c + 1) + 2 - 2 = 2 * c + 2 by omega]
    have hPR : topWidth b (pairRight 0 (List.replicate (2 * c) true)) = some b := pairRight_wf (by simp; omega)
    rw [e1, e2] at hw
    rw [e1, e2, e3]
    have hw0 : (topWidth b (pairRight 0 (List.replicate (2 * c) true) ++
        (pairRight (2 * c) [true, true] ++ [cap (2 * c)]) ++ closeLeft (2 * c) c)).isSome := by
      simpa [List.append_assoc] using hw
    have h1 := Iso.mid (Q := closeLeft (2 * c) c) (v := [cap (2 * c + 2)]) (fun d hd _ => by
      rw [hPR] at hd
      cases hd
      rw [pairRight_cons, pairRight_cons, pairRight_nil]
      exact bandCap (2 * c) b (by omega)) hw0
    have hw1 := Iso.wf h1 hw0
    have h2 := Iso.low (Q := closeLeft (2 * c) c) (fun hc => iso_early_farR (h := cap (2 * c + 2))
      (u := pairRight 0 (List.replicate (2 * c) true))
      (fun g hg => by
        obtain ⟨k, s, rfl, -, h2⟩ := mem_pairRight hg
        have h3 := @List.length_replicate _ (2 * c) true
        exact ⟨k, s, rfl, show k + 2 ≤ 2 * c + 2 by omega⟩) hc) (by simpa [List.append_assoc] using hw1)
    have h3 : Iso b ([cap (2 * c + 2)] ++ (pairRight 0 (List.replicate (2 * c) true) ++ closeLeft (2 * c) c))
        ([cap (2 * c + 2)] ++ closeLeft (2 * c + 2) c) := Iso.high (fun d hd hu => by
          rw [wf_cons_cap (by omega) (wf_nil _)] at hd
          cases hd
          exact bandCaps c (b - 2) (by omega) hu) (by
            have := Iso.wf h2 (by simpa [List.append_assoc] using hw1); simpa using this)
    have h1' : Iso b (pairRight 0 (List.replicate (2 * c) true) ++ (pairRight (2 * c) [true, true] ++
        cap (2 * c) :: closeLeft (2 * c) c))
        (pairRight 0 (List.replicate (2 * c) true) ++ [cap (2 * c + 2)] ++ closeLeft (2 * c) c) := by
      simpa [List.append_assoc] using h1
    have := h1'.trans (by simpa [List.append_assoc] using h2.trans (by simpa using h3))
    simpa [List.append_assoc] using this
theorem kRaw_iso (c : Nat) : Iso (2 * c + 1) (kRaw c) (kHand c)
Proof
:= by
  have hw : (topWidth (2 * c + 1) (kRaw c)).isSome := isSome_of_eq (kRaw_wf c)
  unfold kRaw at hw ⊢
  have h1 := Iso.mid (P := kPre c ++ pairRight 0 (List.replicate (2 * c) true)) (Q := [])
    (u := pairRight (2 * c) [true] ++ closeLeft (2 * c) c) (v := closeLeft (2 * c) c ++ pairRight 0 [true])
    (fun d _ hd => by
      have := capsPast (b := d) (y := 0) (pairRight 0 [true]) (fun g hg => Nat.zero_le _) c
      simp only [shiftW_pairRight, Nat.zero_add] at this
      exact this hd) (by simpa [List.append_assoc] using hw)
  have h2 := Iso.mid (P := kPre c) (Q := pairRight 0 [true])
    (u := pairRight 0 (List.replicate (2 * c) true) ++ closeLeft (2 * c) c) (v := closeLeft (2 * c + 2) c)
    (fun d hd hu => by
      rw [kPre_wf] at hd
      cases hd
      exact bandCaps c _ (by omega) hu) (by
        have := Iso.wf h1 (by simpa [List.append_assoc] using hw); simpa [List.append_assoc] using this)
  have h1' : Iso (2 * c + 1) (kPre c ++ pairRight 0 (List.replicate (2 * c) true) ++ pairRight (2 * c) [true] ++
      closeLeft (2 * c) c) (kPre c ++ (pairRight 0 (List.replicate (2 * c) true) ++ closeLeft (2 * c) c) ++
      pairRight 0 [true]) := by simpa [List.append_assoc] using h1
  exact h1'.trans (by simpa [kHand, pairRight_cons, pairRight_nil] using h2)
theorem kFix : Iso 5 ([cross 1 true, cross 2 true, cross 0 false, cross 1 false] ++ [cap 2, cross 1 true, cross 0 true])
    ([cross 3 true, cross 2 true] ++ kF)
Proof
:=
  iso_of_checkChain _
    [(2, .r2 2 true, false), (3, .farL (.cross 2 false) (.cross 0 false), true), (3, .r2 1 true, false),
      (6, .capSlideL 1 false, true), (4, .capSlideL 0 false, true),
      (4, .farR (.cap 0) (.cross 1 true), true), (5, .farL (.cross 2 true) (.cap 0), false),
      (6, .capSlideL 0 true, false),
      (3, .farR (.cross 1 true) (.cross 3 true), true), (4, .r3 1 true, true),
      (6, .farR (.cross 0 true) (.cross 2 true), false), (2, .r3 2 true, true),
      (4, .farR (.cross 1 true) (.cross 3 true), false), (5, .farR (.cross 0 true) (.cross 3 true), false),
      (1, .r3 2 true, true), (3, .farR (.cross 1 true) (.cross 3 true), false),
      (4, .farR (.cross 0 true) (.cross 3 true), false), (0, .farL (.cross 3 true) (.cross 1 true), false),
      (1, .r3 1 true, true), (3, .farR (.cross 0 true) (.cross 2 true), false),
      (3, .farR (.cross 0 true) (.cross 2 true), true), (4, .farL (.cross 3 true) (.cross 0 true), false)]
    _ (by decide)
theorem kPre_succ (c : Nat) : kPre (c + 1) = shiftW 2 (kPre c) ++ pairLeft 2 [false, true]
Proof
:= by
  unfold kPre
  rw [shiftW_append, shiftW_append, shiftW_pairLeft _ _ _ (by simp), shiftW_pairLeft _ _ _ (by
    rw [length_alternate]), show 2 * (c + 1) = 2 * c + 2 by omega, alternate_two, pairLeft_append,
    length_alternate, show 2 * c + 2 - 2 * c = 2 by omega]
  simp [shiftW, Gen.shift, List.append_assoc]
theorem pairLeft_two : pairLeft 2 [false, true] = [cross 1 true, cross 2 true, cross 0 false, cross 1 false]
Proof
:= rfl
theorem kHand_succ (c : Nat) : Iso (2 * c + 3) (kHand (c + 1)) (shiftW 2 (kHand c) ++ kF)
Proof
:= by
  have hw : (topWidth (2 * c + 3) (kHand (c + 1))).isSome := isSome_of_eq (kHand_wf (c + 1))
  have e : kHand (c + 1) = shiftW 2 (kPre c) ++
      ([cross 1 true, cross 2 true, cross 0 false, cross 1 false] ++ shiftW 2 (closeLeft (2 * c + 2) c)) ++
      [cap 2, cross 1 true, cross 0 true] := by
    rw [kHand, kPre_succ, show 2 * (c + 1) + 2 = 2 * c + 4 by omega, closeLeft_snoc,
      shiftW_closeLeft _ _ _ (by omega), show 2 * c + 4 - 2 * c - 2 = 2 by omega, pairLeft_two]
    simp [List.append_assoc]
  rw [e] at hw ⊢
  have h1 := Iso.mid (v := shiftW 2 (closeLeft (2 * c + 2) c) ++
      [cross 1 true, cross 2 true, cross 0 false, cross 1 false]) (fun d _ hd =>
    iso_late_word (q := 4) (fun t ht => by
      simp only [List.mem_cons, List.not_mem_nil, or_false] at ht
      rcases ht with rfl | rfl | rfl | rfl <;> exact ⟨_, _, rfl, by omega⟩)
      (fun g hg => by
        obtain ⟨g₀, h0, he⟩ := lo_shift_ge hg
        obtain ⟨j, rfl, hj⟩ := mem_closeLeft h0
        rw [he]
        show 4 ≤ j + 2
        omega) hd) hw
  have hP : topWidth (2 * c + 3) (shiftW 2 (kPre c ++ closeLeft (2 * c + 2) c)) = some 5 := by
    have := topWidth_shift (topWidth_append_of (kPre_wf c) (closeLeft_wf c (2 * c + 2) (2 * c + 3) (by omega)
      (by omega))) 2 0
    rw [show 2 * c + 3 - 2 * c = 3 by omega, show 2 + (2 * c + 1) + 0 = 2 * c + 3 by omega] at this
    exact this
  have e2 : shiftW 2 (kPre c) ++ (shiftW 2 (closeLeft (2 * c + 2) c) ++
      [cross 1 true, cross 2 true, cross 0 false, cross 1 false]) ++
      [cap 2, cross 1 true, cross 0 true] = shiftW 2 (kPre c ++ closeLeft (2 * c + 2) c) ++
      ([cross 1 true, cross 2 true, cross 0 false, cross 1 false] ++ [cap 2, cross 1 true, cross 0 true]) := by
    simp [shiftW_append, List.append_assoc]
  have hw2 := Iso.wf h1 hw
  rw [e2] at hw2
  have h2 := Iso.high (P := shiftW 2 (kPre c ++ closeLeft (2 * c + 2) c)) (fun d hd _ => by
    rw [hP] at hd
    cases hd
    exact kFix) hw2
  refine h1.trans ?_
  rw [e2]
  refine h2.trans ?_
  rw [kHand]
  simp only [shiftW_append, shiftW_cons, shiftW_nil, Gen.shift, List.append_assoc, kF, List.cons_append,
    List.nil_append]
  exact Iso.refl _ _
theorem kHand_iso_short : ∀ c, Iso (2 * c + 1) (kHand c) (shortW true c)
Proof
  | 0 => iso_of_checkChain _ [(2, .r2 1 false, true), (1, .r2 0 false, true)] _ (by decide)
  | c + 1 => by
    have ih := (kHand_iso_short c).shift 2 0
    rw [show 2 + (2 * c + 1) + 0 = 2 * c + 3 by omega] at ih
    have h1 := Iso.suffix kF ih (Iso.wf (kHand_succ c) (isSome_of_eq (kHand_wf (c + 1))))
    exact ((kHand_succ c).trans h1).trans (kShort_succ c).symm

Hand knitting: purl through c loops

def pPre (c : Nat) : MWord := strandLeft (2 * c) (2 * c) true ++ [cup 0] ++ pairRight 1 (alternate true (2 * c))

def pRaw (c : Nat) : MWord := pPre c ++ strandRight 0 (2 * c) true ++ closeLeft (2 * c) c

def pHand (c : Nat) : MWord := pPre c ++ closeLeft (2 * c + 1) c

def pG : MWord := [cross 2 true, cross 1 true, cross 3 false, cross 2 false, cap 1]
theorem draw_purlTog (n : Nat) :
    draw (start (n + 1) .front) (work .against (n + 1) ++ [.letOff .left (n + 1)]) =
      some (oneLoop .front, pRaw (n + 1))
Proof
:= by
  simp [draw, work, step, wrapRight, takeOutRight, wrapWord, takeOutWord, State.yarnAt, State.between,
    State.isBack, start, canInsert, room, oneLoop, strandRight, strandLeft, pairRight, pRaw, pPre]
theorem pPre_wf (c : Nat) : topWidth (2 * c + 1) (pPre c) = some (2 * c + 3)
Proof
:= by
  unfold pPre
  refine topWidth_append_of (b := 2 * c + 3) (topWidth_append_of (b := 2 * c + 1)
    (strandLeft_wf _ (le_refl _) (by omega)) (wf_cons_cup (by omega) (wf_nil _))) ?_
  exact pairRight_wf (by rw [length_alternate]; omega)
theorem pRaw_wf (c : Nat) : topWidth (2 * c + 1) (pRaw c) = some 3
Proof
:= by
  unfold pRaw
  refine topWidth_append_of (topWidth_append_of (pPre_wf c) (strandRight_wf _ (by omega))) ?_
  rw [closeLeft_wf c _ _ (by omega) (by omega)]
  congr 1; omega
theorem pHand_wf (c : Nat) : topWidth (2 * c + 1) (pHand c) = some 3
Proof
:= by
  unfold pHand
  refine topWidth_append_of (pPre_wf c) ?_
  rw [closeLeft_wf c _ _ (by omega) (by omega)]
  congr 1; omega
theorem pRaw_iso (c : Nat) : Iso (2 * c + 1) (pRaw c) (pHand c)
Proof
:= by
  have hw : (topWidth (2 * c + 1) (pRaw c)).isSome := isSome_of_eq (pRaw_wf c)
  unfold pRaw at hw
  unfold pRaw pHand
  have h := Iso.high (P := pPre c) (u := strandRight 0 (2 * c) true ++ closeLeft (2 * c) c)
    (v := closeLeft (2 * c + 1) c) (fun d hd _ => by
      rw [pPre_wf] at hd
      cases hd
      have := (slide true (closeLeft (2 * c) c) (2 * c) 0
        (by rw [closeLeft_wf c _ _ (le_refl _) (le_refl _)]; congr 1; omega)).shift 0 2
      rw [shiftW_zero, shiftW_zero, shiftW_closeLeft _ _ _ (le_refl _), show 0 + (2 * c + 1) + 2 = 2 * c + 3 by omega]
        at this
      simpa [strandRight] using this) (by simpa [List.append_assoc] using hw)
  simpa [List.append_assoc] using h
theorem pPre_succ (c : Nat) :
    pPre (c + 1) = strandLeft (2 * c + 2) 2 true ++ pPre c ++ pairRight (2 * c + 1) [true, false]
Proof
:= by
  unfold pPre
  rw [show 2 * (c + 1) = 2 * c + 2 by omega, show 2 * c + 2 = 2 + 2 * c by omega, strandLeft_add,
    show 2 + 2 * c - 2 = 2 * c by omega, show 2 + 2 * c = 2 * c + 2 by omega, alternate_two, pairRight_append,
    length_alternate]
  simp [List.append_assoc, Nat.add_comm]
theorem pG_lo : ∀ g ∈ pG, 1 ≤ g.lo
Proof
:= by decide
theorem shiftW_pG (c : Nat) : shiftW (2 * c) pG = pairRight (2 * c + 1) [true, false] ++ [cap (2 * c + 1)]
Proof
:= by
  simp [pG, shiftW, Gen.shift, pairRight_cons, pairRight_nil]
  omega
theorem pHand_succ (c : Nat) :
    Iso (2 * c + 3) (pHand (c + 1)) (strandLeft (2 * c + 2) 2 true ++ pHand c ++ pG)
Proof
:= by
  have hw : (topWidth (2 * c + 3) (pHand (c + 1))).isSome := isSome_of_eq (pHand_wf (c + 1))
  have e : pHand (c + 1) = strandLeft (2 * c + 2) 2 true ++ pPre c ++
      (shiftW (2 * c) pG ++ closeLeft (1 + 2 * c) c) := by
    rw [pHand, pPre_succ, show 2 * (c + 1) + 1 = 2 * c + 3 by omega, closeLeft_succ,
      show 2 * c + 3 - 2 = 1 + 2 * c by omega, shiftW_pG, show 1 + 2 * c = 2 * c + 1 by omega]
    simp [List.append_assoc]
  rw [e] at hw ⊢
  have h := Iso.high (fun d _ hd => capsPast (b := d) pG pG_lo c hd) hw
  refine h.trans ?_
  rw [pHand, show 1 + 2 * c = 2 * c + 1 by omega]
  simp only [List.append_assoc]
  exact Iso.refl _ _

The shorthand: purl, the inside loop of the stack

def pE : MWord := [cross 0 false, cross 3 false, cross 2 false, cap 1]
theorem f2Fix : Iso 4 [cross 0 true, cross 1 true, cross 2 false, cross 1 false, cross 0 false]
    [cross 2 false, cross 1 false, cross 0 false, cross 1 true, cross 2 true]
Proof
:=
  iso_of_checkChain _
    [(4, .r2 2 false, false), (2, .r3 1 false, false), (1, .r2 1 true, true),
      (0, .farR (.cross 0 true) (.cross 2 false), true), (3, .farL (.cross 2 true) (.cross 0 false), true),
      (4, .r2 1 false, false), (2, .r3 0 false, false), (1, .r2 0 true, true)]
    _ (by decide)
theorem f2At (i b : Nat) (hb : i + 4 ≤ b) :
    Iso b [cross i true, cross (i + 1) true, cross (i + 2) false, cross (i + 1) false, cross i false]
      [cross (i + 2) false, cross (i + 1) false, cross i false, cross (i + 1) true, cross (i + 2) true]
Proof
:= by
  have := f2Fix.shiftTo (i := i) (b := b) hb
  simpa [shiftW, Gen.shift, Nat.add_comm] using this
theorem l7Fix : Iso 7 ([cross 4 true, cross 5 true] ++ shiftW 1 pG ++ pE) (pE ++ pG)
Proof
:=
  iso_of_checkChain _
    [(6, .farL (.cap 2) (.cross 0 false), true), (5, .farL (.cross 3 false) (.cross 0 false), true),
      (4, .farL (.cross 4 false) (.cross 0 false), true), (3, .farL (.cross 2 true) (.cross 0 false), true),
      (2, .farL (.cross 3 true) (.cross 0 false), true), (1, .farL (.cross 5 true) (.cross 0 false), true),
      (0, .farL (.cross 4 true) (.cross 0 false), true), (7, .farR (.cap 2) (.cross 3 false), true),
      (8, .farL (.cross 4 false) (.cap 2), false), (9, .capSlideL 2 false, false),
      (6, .farR (.cross 3 false) (.cross 5 false), true), (7, .r3 3 false, true),
      (9, .farR (.cross 2 false) (.cross 4 false), false), (5, .r3 4 false, true),
      (7, .farR (.cross 3 false) (.cross 5 false), false), (8, .farR (.cross 2 false) (.cross 5 false), false),
      (4, .farR (.cross 2 true) (.cross 5 false), true), (5, .farR (.cross 2 true) (.cross 4 false), true),
      (9, .r2 3 false, false), (7, .r3 2 false, false), (6, .r2 2 true, true),
      (3, .farR (.cross 3 true) (.cross 5 false), true), (7, .r2 4 false, false), (5, .r3 3 false, false),
      (4, .r2 3 true, true), (6, .farL (.cross 4 true) (.cross 2 false), true), (2, .r2 5 true, true),
      (1, .r2 4 true, true), (7, .farL (.cap 3) (.cap 1), true), (6, .farL (.cross 4 false) (.cap 1), true),
      (5, .farL (.cross 5 false) (.cap 1), true), (4, .farL (.cross 3 true) (.cap 1), true),
      (3, .farL (.cross 4 true) (.cap 1), true)]
    _ (by decide)
theorem pShort_inner : ∀ c,
    Iso (2 * c + 3) (shortW false (c + 1)) (strandLeft (2 * c + 2) 2 true ++ shortW false c ++ pG)
Proof
  | 0 => iso_of_checkChain _
      [(3, .r2 1 false, false), (4, .r2 2 false, false), (1, .cupSlideR 0 false, true),
        (1, .farL (.cross 0 false) (.cup 0), false)] _ (by decide)
  | c + 1 => by
    show Iso (2 * c + 5) (shortW false (c + 2)) (strandLeft (2 * c + 4) 2 true ++ shortW false (c + 1) ++ pG)
    have hU := shortW_wf false c
    have hw0 : (topWidth (2 * c + 5) (shortW false (c + 2))).isSome := isSome_of_eq (shortW_wf false (c + 2))
    have hA : Iso (2 * c + 5) (shortW false (c + 2)) (strandRight 1 (2 * c + 2) true ++ [cross (2 * c + 3) false] ++
        shiftW 1 (shortW false (c + 1)) ++ pE) := shortW_peel false (c + 1)
    have hwA := Iso.wf hA hw0
    have hB : Iso (2 * c + 5) (strandRight 1 (2 * c + 2) true ++ [cross (2 * c + 3) false] ++
        shiftW 1 (shortW false (c + 1)) ++ pE) (strandRight 1 (2 * c + 2) true ++ [cross (2 * c + 3) false] ++
        shiftW 1 (strandLeft (2 * c + 2) 2 true ++ shortW false c ++ pG) ++ pE) := by
      refine Iso.mid (fun d hd _ => ?_) hwA
      rw [topWidth_append_of (strandRight_wf _ (by omega)) (wf_cons_cross (by omega) (wf_nil _))] at hd
      cases hd
      have := (pShort_inner c).shift 1 1
      rwa [show 1 + (2 * c + 3) + 1 = 2 * c + 5 by omega] at this
    have hwB := Iso.wf hB hwA
    have e1 : strandRight 1 (2 * c + 2) true ++ [cross (2 * c + 3) false] ++
        shiftW 1 (strandLeft (2 * c + 2) 2 true ++ shortW false c ++ pG) ++ pE =
        strandRight 1 (2 * c) true ++ [cross (2 * c + 1) true, cross (2 * c + 2) true, cross (2 * c + 3) false,
          cross (2 * c + 2) false, cross (2 * c + 1) false] ++ (shiftW 1 (shortW false c) ++ shiftW 1 pG ++ pE) := by
      rw [show 2 * c + 2 = (2 * c + 1) + 1 by omega, strandRight_succ, strandRight_succ, shiftW_append,
        shiftW_append, shiftW_strandLeft _ _ _ _ (by omega)]
      simp [strandLeft, List.range_succ, List.append_assoc]
      omega
    rw [e1] at hwB
    have hC := Iso.mid (v := [cross (2 * c + 3) false, cross (2 * c + 2) false, cross (2 * c + 1) false,
      cross (2 * c + 2) true, cross (2 * c + 3) true]) (fun d hd _ => by
        rw [strandRight_wf _ (by omega)] at hd
        cases hd
        exact f2At (2 * c + 1) _ (by omega)) hwB
    have hwC := Iso.wf hC hwB
    have e2 : strandRight 1 (2 * c) true ++ [cross (2 * c + 3) false, cross (2 * c + 2) false, cross (2 * c + 1) false,
        cross (2 * c + 2) true, cross (2 * c + 3) true] ++ (shiftW 1 (shortW false c) ++ shiftW 1 pG ++ pE) =
        (strandRight 1 (2 * c) true ++ [cross (2 * c + 3) false, cross (2 * c + 2) false, cross (2 * c + 1) false]) ++
        ([cross (2 * c + 2) true, cross (2 * c + 3) true] ++ shiftW 1 (shortW false c)) ++ (shiftW 1 pG ++ pE) := by
      simp [List.append_assoc]
    rw [e2] at hwC
    have hp1 : passR (2 * c + 3) (shiftW 1 (shortW false c)) = some 5 := by
      have := passR_shift (k := 1) (j := 1) hU
      rwa [show 1 + (2 * c + 1) + 1 = 2 * c + 3 by omega] at this
    have hp0 : passR (2 * c + 2) (shiftW 1 (shortW false c)) = some 4 := by
      have := passR_shift (k := 1) (j := 0) hU
      rwa [show 1 + (2 * c + 1) + 0 = 2 * c + 2 by omega] at this
    have hD := Iso.mid (v := shiftW 1 (shortW false c) ++ [cross 4 true, cross 5 true]) (fun d _ hd => by
      have h1 : Iso d ([cross (2 * c + 2) true] ++ (cross (2 * c + 3) true :: shiftW 1 (shortW false c)))
          ([cross (2 * c + 2) true] ++ (shiftW 1 (shortW false c) ++ [cross 5 true])) :=
        Iso.high (fun d' _ hu => by
          obtain ⟨e, he⟩ := Option.isSome_iff_exists.mp hu
          exact iso_pass_right hp1 he) (by simpa using hd)
      have hw1 := Iso.wf h1 (by simpa using hd)
      have h2 := Iso.low (b := d) (Q := [cross 5 true]) (u := cross (2 * c + 2) true :: shiftW 1 (shortW false c))
        (v := shiftW 1 (shortW false c) ++ [cross 4 true]) (fun hu => by
          obtain ⟨e, he⟩ := Option.isSome_iff_exists.mp hu
          exact iso_pass_right hp0 he) (by simpa using hw1)
      have h1' : Iso d ([cross (2 * c + 2) true, cross (2 * c + 3) true] ++ shiftW 1 (shortW false c))
          (cross (2 * c + 2) true :: shiftW 1 (shortW false c) ++ [cross 5 true]) := by simpa using h1
      simpa using h1'.trans h2) hwC
    have hwD := Iso.wf hD hwC
    have e3 : (strandRight 1 (2 * c) true ++
        [cross (2 * c + 3) false, cross (2 * c + 2) false, cross (2 * c + 1) false]) ++
        (shiftW 1 (shortW false c) ++ [cross 4 true, cross 5 true]) ++ (shiftW 1 pG ++ pE) =
        (strandRight 1 (2 * c) true ++ [cross (2 * c + 3) false, cross (2 * c + 2) false, cross (2 * c + 1) false] ++
          shiftW 1 (shortW false c)) ++ ([cross 4 true, cross 5 true] ++ shiftW 1 pG ++ pE) := by
      simp [List.append_assoc]
    rw [e3] at hwD
    have hP : topWidth (2 * c + 5) (strandRight 1 (2 * c) true ++ [cross (2 * c + 3) false, cross (2 * c + 2) false,
        cross (2 * c + 1) false] ++ shiftW 1 (shortW false c)) = some 7 := by
      refine topWidth_append_of (topWidth_append_of (strandRight_wf _ (by omega))
        (wf_cons_cross (by omega) (wf_cons_cross (by omega) (wf_cons_cross (by omega) (wf_nil _))))) ?_
      have := topWidth_shift hU 1 3
      rw [show 1 + (2 * c + 1) + 3 = 2 * c + 5 by omega] at this
      exact this
    have hE := Iso.high (v := pE ++ pG) (fun d hd _ => by
      rw [hP] at hd
      cases hd
      exact l7Fix) hwD
    have hL := ((((hA.trans hB).trans (e1 ▸ hC)).trans (e2 ▸ hD)).trans (e3 ▸ hE))
    have hR0 : (topWidth (2 * c + 5) (strandLeft (2 * c + 4) 2 true ++ shortW false (c + 1) ++ pG)).isSome := by
      refine isSome_of_eq (topWidth_append_of (topWidth_append_of (strandLeft_wf _ (by omega) (by omega)) ?_)
        (by rfl : topWidth 5 pG = some 3))
      have := topWidth_shift (shortW_wf false (c + 1)) 0 2
      rwa [shiftW_zero, show 0 + (2 * (c + 1) + 1) + 2 = 2 * c + 5 by omega] at this
    have hF := Iso.mid (v := strandRight 1 (2 * c) true ++ [cross (2 * c + 1) false] ++ shiftW 1 (shortW false c) ++ pE)
      (fun d hd _ => by
        rw [strandLeft_wf _ (by omega) (by omega)] at hd
        cases hd
        have := (shortW_peel false c).shift 0 2
        rwa [shiftW_zero, shiftW_zero, show 0 + (2 * c + 3) + 2 = 2 * c + 5 by omega] at this) hR0
    have hwF := Iso.wf hF hR0
    have e4 : strandLeft (2 * c + 4) 2 true ++ (strandRight 1 (2 * c) true ++ [cross (2 * c + 1) false] ++
        shiftW 1 (shortW false c) ++ pE) ++ pG =
        ([cross (2 * c + 3) false, cross (2 * c + 2) false] ++ strandRight 1 (2 * c) true) ++
        ([cross (2 * c + 1) false] ++ shiftW 1 (shortW false c) ++ pE ++ pG) := by
      simp [strandLeft, List.range_succ, List.append_assoc]
    rw [e4] at hwF
    have hG := Iso.low (fun hu => iso_comm_crosses [cross (2 * c + 3) false, cross (2 * c + 2) false]
      (U := strandRight 1 (2 * c) true) (fun g hg => by
        obtain ⟨j, hj, rfl⟩ := mem_strandRight hg
        refine ⟨_, _, rfl, fun t ht => ?_⟩
        simp only [List.mem_cons, List.not_mem_nil, or_false] at ht
        rcases ht with rfl | rfl <;> simp only [Gen.lo] <;> omega) hu) hwF
    have hR := hF.trans (e4 ▸ hG)
    refine hL.trans (hR.trans ?_).symm
    simp only [List.append_assoc, List.cons_append, List.nil_append]
    exact Iso.refl _ _
theorem pHand_iso_short : ∀ c, Iso (2 * c + 1) (pHand c) (shortW false c)
Proof
  | 0 => by
    rw [show pHand 0 = shortW false 0 from rfl]
    exact Iso.refl _ _
  | c + 1 => by
    have hw : (topWidth (2 * c + 3) (strandLeft (2 * c + 2) 2 true ++ pHand c ++ pG)).isSome :=
      Iso.wf (pHand_succ c) (isSome_of_eq (pHand_wf (c + 1)))
    have h := Iso.mid (v := shortW false c) (fun d hd _ => by
      rw [strandLeft_wf _ (by omega) (by omega)] at hd
      cases hd
      have := (pHand_iso_short c).shift 0 2
      rwa [shiftW_zero, shiftW_zero, show 0 + (2 * c + 1) + 2 = 2 * c + 3 by omega] at this) hw
    exact ((pHand_succ c).trans h).trans (pShort_inner c).symm
theorem start_width (c : Nat) (y : Side) : (start c y).width = 2 * c + 1
Proof
:= by
  simp [State.width, State.yarnAt, start, State.wrapping]
theorem oneLoop_width (y : Side) : (oneLoop y).width = 3
Proof
:= by
  simp [State.width, State.yarnAt, oneLoop, State.wrapping]
open Compile
theorem ktog_rule (c : Nat) (hc : 1 ≤ c) :
    Runs (start c .back) (work .along c ++ [.letOff .left c]) (oneLoop .back) ∧
      ∃ u, D (List.replicate c (.stack 1) ++ [.yarn]) (List.replicate (c - 1) (.merge 0) ++ [.knit .L .f]) =
          some ([.yarn, .stack 1], u) ∧
        Iso (2 * c + 1) (drawing (start c .back) (work .along c ++ [.letOff .left c])) u
Proof
:= by
  obtain ⟨n, rfl⟩ : ∃ n, c = n + 1 := ⟨c - 1, by omega⟩
  refine ⟨?_, shortW true (n + 1), D_ktog .f (n + 1) (by omega), ?_⟩
  · unfold Runs
    rw [draw_knitTog, start_width, oneLoop_width]
    exact ⟨rfl, kRaw_wf (n + 1)⟩
  · unfold drawing
    rw [draw_knitTog]
    exact (kRaw_iso (n + 1)).trans (kHand_iso_short (n + 1))
theorem ptog_rule (c : Nat) (hc : 1 ≤ c) :
    Runs (start c .front) (work .against c ++ [.letOff .left c]) (oneLoop .front) ∧
      ∃ u, D (List.replicate c (.stack 1) ++ [.yarn]) (List.replicate (c - 1) (.merge 0) ++ [.knit .L .b]) =
          some ([.yarn, .stack 1], u) ∧
        Iso (2 * c + 1) (drawing (start c .front) (work .against c ++ [.letOff .left c])) u
Proof
:= by
  obtain ⟨n, rfl⟩ : ∃ n, c = n + 1 := ⟨c - 1, by omega⟩
  refine ⟨?_, shortW false (n + 1), D_ktog .b (n + 1) (by omega), ?_⟩
  · unfold Runs
    rw [draw_purlTog, start_width, oneLoop_width]
    exact ⟨rfl, pRaw_wf (n + 1)⟩
  · unfold drawing
    rw [draw_purlTog]
    exact (pRaw_iso (n + 1)).trans (pHand_iso_short (n + 1))

The shorthand in a longer row: objects before and after the row

A shorthand program works only near the yarn. Objects before the row (A) and after the row (Z) do not change what it does.

  • Step.shift n: the step with its object numbers moved by n (x, merge, drop). The other steps find their objects from the yarn.
  • runStep_frame, D_frame: if r gives r' with the drawing u, then A ++ r ++ Z gives A ++ r' ++ Z, with u moved rowWidth A strands to the right. The only condition: A does not contain the yarn.
  • The objects of Z add strands on the right. They change no event.
def Step.shift (n : Nat) : Step → Step
  | .x a f => .x (a + n) f
  | .merge a => .merge (a + n)
  | .drop a => .drop (a + n)
  | s => s
namespace Frame
theorem rowWidth_append (A B : Row) : rowWidth (A ++ B) = rowWidth A + rowWidth B
Proof
:= by
  simp [rowWidth]
theorem mem_mid {A r Z : Row} {o : Obj} (h : o ∈ r) : o ∈ A ++ r ++ Z
Proof
:= by
  simp [h]
theorem yarnIdx_mid {A r Z : Row} (hA : Obj.yarn ∉ A) (hr : Obj.yarn ∈ r) :
    yarnIdx (A ++ r ++ Z) = A.length + yarnIdx r
Proof
:= by
  simp only [yarnIdx, List.append_assoc, List.idxOf_append, hA, hr, ite_false, ite_true]
  omega
theorem yarnIdx_lt {r : Row} (hr : Obj.yarn ∈ r) : yarnIdx r < r.length
Proof
:= List.idxOf_lt_length_of_mem hr
theorem getElem?_mid (A r Z : Row) {a : Nat} (h : a < r.length) : (A ++ r ++ Z)[A.length + a]? = r[a]?
Proof
:= by
  rw [List.append_assoc, List.getElem?_append_right (by omega), Nat.add_sub_cancel_left,
    List.getElem?_append_left h]
theorem stackAt_mid (A r Z : Row) {a : Nat} (h : a < r.length) : stackAt (A ++ r ++ Z) (A.length + a) = stackAt r a
Proof
:= by
  simp only [stackAt, getElem?_mid A r Z h]
theorem lt_of_stackAt {r : Row} {a k : Nat} (h : stackAt r a = some k) : a < r.length
Proof
:= by
  unfold stackAt at h
  cases he : r[a]? with
  | none => simp [he] at h
  | some o => exact (List.getElem?_eq_some_iff.mp he).1
theorem offset_mid (A r Z : Row) {a : Nat} (h : a ≤ r.length) :
    offset (A ++ r ++ Z) (A.length + a) = rowWidth A + offset r a
Proof
:= by
  simp only [offset, List.append_assoc, List.take_append, List.take_of_length_le (Nat.le_add_right _ _),
    Nat.add_sub_cancel_left, rowWidth_append, List.take_append_of_le_length h]
theorem splice_mid {A r Z : Row} {f len : Nat} (new : Row) (h : f + len ≤ r.length) :
    splice (A ++ r ++ Z) (A.length + f) len new = A ++ splice r f len new ++ Z
Proof
:= by
  simp only [splice, List.append_assoc, List.take_append, List.drop_append,
    List.take_of_length_le (show A.length ≤ A.length + f by omega),
    List.drop_of_length_le (show A.length ≤ A.length + f + len by omega), Nat.add_sub_cancel_left,
    show A.length + f + len - A.length = f + len by omega,
    List.take_append_of_le_length (show f ≤ r.length by omega), List.drop_append_of_le_length h, List.nil_append]
theorem eraseIdx_mid (A r Z : Row) {a : Nat} (h : a < r.length) :
    (A ++ r ++ Z).eraseIdx (A.length + a) = A ++ r.eraseIdx a ++ Z
Proof
:= by
  rw [List.append_assoc, List.eraseIdx_append_of_length_le (by omega), Nat.add_sub_cancel_left,
    List.eraseIdx_append_of_lt_length h, List.append_assoc]
theorem getD_mid (A r Z : Row) {a : Nat} (h : a < r.length) (d : Obj) :
    (A ++ r ++ Z).getD (A.length + a) d = r.getD a d
Proof
:= by
  simp only [List.getD_eq_getElem?_getD, getElem?_mid A r Z h]
open Frame
theorem knit_frame {r : Row} {d : Dir} {k : Nat} (A Z : Row) (hA : Obj.yarn ∉ A) (hy : Obj.yarn ∈ r)
    (hb : hasBeside r d = true) (h1 : stackAt r (besideIdx r d) = some k) :
    hasBeside (A ++ r ++ Z) d = true ∧ stackAt (A ++ r ++ Z) (besideIdx (A ++ r ++ Z) d) = some k ∧
      firstIdx (A ++ r ++ Z) d = A.length + firstIdx r d ∧ firstIdx r d + 2 ≤ r.length
Proof
:= by
  have hl := yarnIdx_lt hy
  have hbl := lt_of_stackAt h1
  cases d with
  | R =>
    simp only [hasBeside, decide_eq_true_eq] at hb
    simp only [besideIdx] at h1 hbl
    refine ⟨?_, ?_, ?_, ?_⟩
    · simp only [hasBeside, yarnIdx_mid hA hy, decide_eq_true_eq, List.length_append]; omega
    · simp only [besideIdx, yarnIdx_mid hA hy]
      rw [Nat.add_assoc, stackAt_mid A r Z hbl, h1]
    · simp only [firstIdx, yarnIdx_mid hA hy]
    · simp only [firstIdx]; omega
  | L =>
    simp only [hasBeside, decide_eq_true_eq] at hb
    simp only [besideIdx] at h1 hbl
    refine ⟨?_, ?_, ?_, ?_⟩
    · simp only [hasBeside, yarnIdx_mid hA hy, decide_eq_true_eq]; omega
    · simp only [besideIdx, yarnIdx_mid hA hy]
      rw [show A.length + yarnIdx r - 1 = A.length + (yarnIdx r - 1) by omega, stackAt_mid A r Z hbl, h1]
    · simp only [firstIdx, yarnIdx_mid hA hy]; omega
    · simp only [firstIdx]; omega
theorem runStep_frame {r r' : Row} {s : Step} {u : MWord} (A Z : Row) (hA : Obj.yarn ∉ A)
    (h : runStep r s = some (r', u)) :
    runStep (A ++ r ++ Z) (s.shift A.length) = some (A ++ r' ++ Z, shiftW (rowWidth A) u)
Proof
:= by
  cases s with
  | x a f =>
    simp only [runStep] at h
    split_ifs at h with hc
    obtain ⟨hy, ha⟩ := hc
    simp only [Option.some.injEq, Prod.mk.injEq] at h
    obtain ⟨rfl, rfl⟩ := h
    simp only [Step.shift, runStep]
    rw [ite_eq_left (show Obj.yarn ∈ A ++ r ++ Z ∧ a + A.length + 1 < (A ++ r ++ Z).length from
      ⟨mem_mid hy, by simp; omega⟩), Nat.add_comm a A.length]
    rw [getD_mid A r Z (by omega), show A.length + a + 1 = A.length + (a + 1) by omega, getD_mid A r Z ha,
      splice_mid _ (by omega), offset_mid A r Z (by omega), shiftW_swapBlocks, Nat.add_comm (rowWidth A)]
  | tuck d =>
    simp only [runStep] at h
    split_ifs at h with hy
    simp only [Option.some.injEq, Prod.mk.injEq] at h
    obtain ⟨rfl, rfl⟩ := h
    have hl := yarnIdx_lt hy
    simp only [Step.shift, runStep]
    rw [ite_eq_left (mem_mid hy : Obj.yarn ∈ A ++ r ++ Z), yarnIdx_mid hA hy, splice_mid _ (by omega),
      offset_mid A r Z (by omega), shiftW_shiftW, Nat.add_comm (offset r _)]
  | merge a =>
    simp only [runStep] at h
    split_ifs at h with hy
    cases h1 : stackAt r a with
    | none => simp [h1] at h
    | some k =>
      cases h2 : stackAt r (a + 1) with
      | none => simp [h1, h2] at h
      | some l =>
        simp only [h1, h2, Option.some.injEq, Prod.mk.injEq] at h
        obtain ⟨rfl, rfl⟩ := h
        have ha := lt_of_stackAt h2
        simp only [Step.shift, runStep]
        rw [ite_eq_left (mem_mid hy : Obj.yarn ∈ A ++ r ++ Z), Nat.add_comm a A.length,
          stackAt_mid A r Z (show a < r.length by omega), show A.length + a + 1 = A.length + (a + 1) by omega,
          stackAt_mid A r Z ha, h1, h2]
        dsimp only
        rw [splice_mid _ (by omega)]
        rfl
  | knit d bd =>
    simp only [runStep] at h
    split_ifs at h with hc
    obtain ⟨hy, hb⟩ := hc
    cases h1 : stackAt r (besideIdx r d) with
    | none => simp [h1] at h
    | some k =>
      simp only [h1, Option.some.injEq, Prod.mk.injEq] at h
      obtain ⟨rfl, rfl⟩ := h
      obtain ⟨hb', hs', hf', hl'⟩ := knit_frame A Z hA hy hb h1
      simp only [Step.shift, runStep]
      rw [ite_eq_left (And.intro (mem_mid hy : Obj.yarn ∈ A ++ r ++ Z) hb'), hs']
      dsimp only
      rw [hf', splice_mid _ hl',
        offset_mid A r Z (by omega), shiftW_shiftW, Nat.add_comm (offset r _)]
  | split d bd =>
    simp only [runStep] at h
    split_ifs at h with hc
    obtain ⟨hy, hb⟩ := hc
    cases h1 : stackAt r (besideIdx r d) with
    | none => simp [h1] at h
    | some k =>
      simp only [h1, Option.some.injEq, Prod.mk.injEq] at h
      obtain ⟨rfl, rfl⟩ := h
      obtain ⟨hb', hs', hf', hl'⟩ := knit_frame A Z hA hy hb h1
      simp only [Step.shift, runStep]
      rw [ite_eq_left (And.intro (mem_mid hy : Obj.yarn ∈ A ++ r ++ Z) hb'), hs']
      dsimp only
      rw [hf', splice_mid _ hl',
        offset_mid A r Z (by omega), shiftW_shiftW, Nat.add_comm (offset r _)]
  | drop a =>
    simp only [runStep] at h
    split_ifs at h with hy
    cases h1 : stackAt r a with
    | none => simp [h1] at h
    | some k =>
      simp only [h1, Option.some.injEq, Prod.mk.injEq] at h
      obtain ⟨rfl, rfl⟩ := h
      have ha := lt_of_stackAt h1
      simp only [Step.shift, runStep]
      rw [ite_eq_left (mem_mid hy : Obj.yarn ∈ A ++ r ++ Z), Nat.add_comm a A.length, stackAt_mid A r Z ha, h1,
        eraseIdx_mid A r Z ha, offset_mid A r Z (by omega)]
      simp [shiftW, Gen.shift, Nat.add_comm]
theorem D_frame (A Z : Row) (hA : Obj.yarn ∉ A) : ∀ {r r' : Row} {w : List Step} {u : MWord},
    D r w = some (r', u) → D (A ++ r ++ Z) (w.map (Step.shift A.length)) = some (A ++ r' ++ Z, shiftW (rowWidth A) u)
Proof
  | r, r', [], u, h => by
    simp only [D, Option.some.injEq, Prod.mk.injEq] at h
    obtain ⟨rfl, rfl⟩ := h
    rfl
  | r, r', s :: ss, u, h => by
    simp only [D] at h
    cases h1 : runStep r s with
    | none => simp [h1] at h
    | some p =>
      obtain ⟨r₁, u₁⟩ := p
      simp only [h1] at h
      cases h2 : D r₁ ss with
      | none => simp [h2] at h
      | some q =>
        obtain ⟨r₂, u₂⟩ := q
        simp only [h2, Option.some.injEq, Prod.mk.injEq] at h
        obtain ⟨rfl, rfl⟩ := h
        simp only [List.map_cons, D, runStep_frame A Z hA h1, D_frame A Z hA h2, shiftW_append]

Hand knitting in a longer row: more loops at the far ends of the needles

A hand program works only at the needle tips. Loops at the far end of the left needle and at the far end of the right needle do not change what it does.

  • State.pad k m s: the state s with k more loops at the far end of the left needle and m more loops at the far end of the right needle.
  • In the drawing, the loops of the left needle are the strands on the left of the yarn end, and the far end is on the left. So k more loops move each event 2 * k strands to the right. The loops of the right needle are the strands on the right of the yarn end, and the far end is on the right. So m more loops add strands on the right and change no event.
  • State.Fits s: a needle that is in loops is in no more loops than its needle holds, and the pair of needles is a pair of hand knitting (canInsert). Each state with no needle in loops fits (State.fits_of_inside). Each action keeps this property (step_fits).
Theorem Statement
step_pad s.Fits → step s a = some (s', u) → step (s.pad k m) a = some (s'.pad k m, shiftW (2 * k) u)
draw_pad s.Fits → draw s p = some (s', u) → draw (s.pad k m) p = some (s'.pad k m, shiftW (2 * k) u)
rot_shift Seen from the other side of the work, strands on the right become strands on the left

The condition State.Fits is necessary. Without it, a state can have the right needle in more loops than the needle holds. Then the positions of takeOut go below strand 0, and the shift of the word is not the word of the longer row.

namespace Hand
open Compile Gen
def State.pad (k m : Nat) (s : State) : State := { s with left := s.left + k, right := s.right + m }

def State.Fits (s : State) : Prop :=
  match s.inside with
  | none => True
  | some i => canInsert i.needle i.into = true ∧ i.count ≤ room s i.into
theorem State.fits_of_inside {s : State} (h : s.inside = none) : s.Fits
Proof
:= by
  simp [State.Fits, h]

The longer state

namespace Local
@[simp] theorem pad_left (k m : Nat) (s : State) : (s.pad k m).left = s.left + k
Proof
:= rfl
@[simp] theorem pad_right (k m : Nat) (s : State) : (s.pad k m).right = s.right + m := rfl
@[simp] theorem pad_yarn (k m : Nat) (s : State) : (s.pad k m).yarn = s.yarn := rfl
@[simp] theorem pad_inside (k m : Nat) (s : State) : (s.pad k m).inside = s.inside := rfl
@[simp] theorem pad_cable (k m : Nat) (s : State) : (s.pad k m).cable = s.cable := rfl
@[simp] theorem pad_hold (k m : Nat) (s : State) : (s.pad k m).hold = s.hold := rfl
@[simp] theorem pad_bar (k m : Nat) (s : State) : (s.pad k m).bar = s.bar := rfl
theorem pad_yarnAt (k m : Nat) (s : State) : (s.pad k m).yarnAt = s.yarnAt + 2 * k
Proof
:= by
  cases hb : s.bar <;> simp [State.yarnAt, State.pad, hb] <;> omega
theorem pad_width (k m : Nat) (s : State) : (s.pad k m).width = 2 * k + s.width + 2 * m
Proof
:= by
  have := pad_yarnAt k m s
  cases hi : s.inside with
  | none => simp [State.width, State.wrapping, hi, this]; omega
  | some i => cases hw : i.wrapped <;> simp [State.width, State.wrapping, hi, hw, this] <;> omega
theorem pad_isBack (k m : Nat) (s : State) : (s.pad k m).isBack = s.isBack
Proof
:= rfl
theorem pad_heldOver (k m : Nat) (s : State) : (s.pad k m).heldOver = s.heldOver
Proof
:= rfl
theorem pad_between (k m : Nat) (s : State) (n : Needle) : (s.pad k m).between n = s.between n
Proof
:= rfl
theorem pad_flip (k m : Nat) (s : State) : (s.pad k m).flip = s.flip.pad m k
Proof
:= rfl
theorem pad_ext {s t : State} (h1 : s.left = t.left) (h2 : s.right = t.right) (h3 : s.yarn = t.yarn)
    (h4 : s.inside = t.inside) (h5 : s.cable = t.cable) (h6 : s.hold = t.hold) (h7 : s.bar = t.bar) : s = t
Proof
:= by
  cases s; cases t; simp_all
macro "pad_eq" : tactic =>
  `(tactic| (apply FormalizeKnitting.Hand.Local.pad_ext <;> first | rfl | (simp only [State.pad]; omega) | simp [State.pad] | omega))

macro "if_left" : tactic => `(tactic| (rw [ite_eq_left]; rotate_left))

Words moved to the right

theorem shiftW_halfTurn (j i : Nat) (lo : Bool) : ∀ n, shiftW j (halfTurn i lo n) = halfTurn (i + j) lo n
Proof
  | 0 => rfl
  | n + 1 => by
    simp only [halfTurn, shiftW_append, shiftW_halfTurn j i lo n]
    congr 1
    simp only [shiftW, List.map_map]
    apply List.map_congr_left
    intro x _
    simp only [Function.comp, Gen.shift]
    congr 1; omega
theorem shiftW_passLeftAll (j : Nat) : ∀ (n y : Nat), 2 * n + 2 ≤ y →
    shiftW j (passLeftAll y n) = passLeftAll (y + j) n
Proof
  | 0, _, _ => rfl
  | n + 1, y, h => by
    simp only [passLeftAll, shiftW_append, shiftW_passLeftAll j n (y - 2) (by omega)]
    rw [show y + j - 2 = y - 2 + j by omega]
    congr 1
    simp only [passLeft, shiftW, List.map, Gen.shift, List.cons.injEq, Gen.cross.injEq, Gen.cap.injEq, and_true]
    omega
theorem shiftW_replicate (j n : Nat) (g : Gen) : shiftW j (List.replicate n g) = List.replicate n (g.shift j)
Proof
:= by
  simp [shiftW, List.map_replicate]
theorem shiftW_flatten (j : Nat) (l : List MWord) : shiftW j l.flatten = (l.map (shiftW j)).flatten
Proof
:= by
  rw [shiftW, List.map_flatten]; rfl
theorem wrapWord_pad (k m : Nat) (s : State) (n : Needle) (w : Way) (c : Nat) (sense : Sense)
    (h : s.between n + 2 * c ≤ s.yarnAt) :
    wrapWord (s.pad k m) n w c sense = shiftW (2 * k) (wrapWord s n w c sense)
Proof
:= by
  have e1 : s.yarnAt + 2 * k - s.between n = s.yarnAt - s.between n + 2 * k := by omega
  have e2 : s.yarnAt - s.between n + 2 * k - 2 * c = s.yarnAt - s.between n - 2 * c + 2 * k := by omega
  have e3 : ∀ x, x + 2 * k + 1 = x + 1 + 2 * k := by omega
  cases w <;> cases sense <;>
    simp only [wrapWord, pad_yarnAt, pad_between, pad_heldOver, pad_isBack, e1, e2, e3, shiftW_append,
      shiftW_strandLeft _ _ _ _ (show s.between n ≤ s.yarnAt by omega),
      shiftW_strandLeft _ _ _ _ (show 2 * c ≤ s.yarnAt - s.between n by omega), shiftW_cons, shiftW_nil, Gen.shift]
theorem takeOutWord_pad (k m : Nat) (s : State) (n : Needle) (w : Way) (c : Nat) (r : Side)
    (h : s.between n + 2 * c ≤ s.yarnAt) :
    takeOutWord (s.pad k m) n w c r = shiftW (2 * k) (takeOutWord s n w c r)
Proof
:= by
  have e1 : s.yarnAt + 2 * k - s.between n = s.yarnAt - s.between n + 2 * k := by omega
  have e2 : s.yarnAt - s.between n + 2 * k - 2 * c = s.yarnAt - s.between n - 2 * c + 2 * k := by omega
  have e3 : ∀ x, x + 2 * k + 1 = x + 1 + 2 * k := by omega
  cases w <;>
    simp only [takeOutWord, pad_yarnAt, pad_between, pad_heldOver, pad_isBack, e1, e2, e3, shiftW_append,
      shiftW_pairRight, shiftW_strandRight,
      shiftW_pairLeft _ _ _ (show [s.isBack].length ≤ s.yarnAt - s.between n + 1 by simp),
      shiftW_pairLeft _ _ _ (show (alternate false (2 * c)).length ≤ s.yarnAt - s.between n by
        rw [length_alternate]; omega)]

Words that fit

theorem pairLeft_wf {w i : Nat} {l : List Bool} (h : i + 1 < w) : topWidth w (pairLeft i l) = some w
Proof
:=
  topWidth_crosses fun g hg => by obtain ⟨k, s, rfl, hk⟩ := mem_pairLeft hg; exact ⟨k, s, rfl, by omega⟩
theorem wrapWord_wf (s : State) (n : Needle) (w : Way) (c : Nat) (sense : Sense) (e : Nat)
    (h : s.between n + 2 * c ≤ s.yarnAt) :
    topWidth (s.yarnAt + 1 + e) (wrapWord s n w c sense) = some (s.yarnAt + 3 + e)
Proof
:= by
  have hL := strandLeft_wf (w := s.yarnAt + 1 + e) s.heldOver (show s.between n ≤ s.yarnAt by omega) (by omega)
  cases w <;> cases sense <;> simp only [wrapWord]
  · refine topWidth_append_of (topWidth_append_of hL (wf_cons_cup (by omega) (wf_nil _))) ?_
    rw [show s.yarnAt + 1 + e + 2 = s.yarnAt + 3 + e by omega]; rfl
  · refine topWidth_append_of (topWidth_append_of hL (wf_cons_cup (by omega) (wf_nil _))) ?_
    rw [show s.yarnAt + 1 + e + 2 = s.yarnAt + 3 + e by omega]
    exact wf_cons_cross (by omega) (wf_nil _)
  · refine topWidth_append_of (topWidth_append_of (topWidth_append_of hL
      (strandLeft_wf _ (show 2 * c ≤ s.yarnAt - s.between n by omega) (by omega)))
      (wf_cons_cup (by omega) (wf_nil _))) (by rw [show s.yarnAt + 1 + e + 2 = s.yarnAt + 3 + e by omega]; rfl)
  · refine topWidth_append_of (topWidth_append_of (topWidth_append_of hL
      (strandLeft_wf _ (show 2 * c ≤ s.yarnAt - s.between n by omega) (by omega)))
      (wf_cons_cup (by omega) (wf_nil _))) ?_
    rw [show s.yarnAt + 1 + e + 2 = s.yarnAt + 3 + e by omega]
    exact wf_cons_cross (by omega) (wf_nil _)
theorem takeOutWord_wf (s : State) (n : Needle) (w : Way) (c : Nat) (r : Side) (e : Nat)
    (h : s.between n + 2 * c ≤ s.yarnAt) :
    topWidth (s.yarnAt + 3 + e) (takeOutWord s n w c r) = some (s.yarnAt + 3 + e)
Proof
:= by
  cases w <;> simp only [takeOutWord]
  · refine topWidth_append_of (topWidth_append_of (topWidth_append_of (topWidth_append_of (topWidth_append_of
      (pairLeft_wf (by omega)) (pairLeft_wf (by omega))) (pairRight_wf ?_)) (pairRight_wf ?_)) (pairRight_wf ?_))
      (strandRight_wf _ (by omega))
    · simp only [List.length_replicate]; omega
    · simp only [List.length_cons, List.length_nil]; omega
    · simp only [List.length_replicate]; omega
  · refine topWidth_append_of (topWidth_append_of (topWidth_append_of (pairRight_wf ?_) (pairRight_wf ?_))
      (strandRight_wf _ (by omega))) (strandRight_wf _ (by omega))
    · rw [length_alternate]; omega
    · simp only [List.length_replicate]; omega

The other side of the work

theorem rot_shift (a b : Nat) : ∀ (v : MWord) (w : Nat), (topWidth w v).isSome →
    rot (a + w + b) (shiftW b v) = shiftW a (rot w v)
Proof
  | [], _, _ => rfl
  | g :: gs, w, hw => by
    obtain ⟨c, hc⟩ := Option.isSome_iff_exists.mp hw
    obtain ⟨hf, hr⟩ := fits_of_topWidth hc
    have ih := rot_shift a b gs (g.widthAfter w) (by rw [hr]; rfl)
    cases g with
    | cross i lo =>
      simp only [Gen.fits, decide_eq_true_eq] at hf
      simp only [shiftW_cons, rot_cons, Gen.shift, rotGen, Gen.widthAfter] at ih ⊢
      rw [ih, show a + w + b - 2 - (i + b) = w - 2 - i + a by omega]
    | cup i =>
      simp only [Gen.fits, decide_eq_true_eq] at hf
      simp only [shiftW_cons, rot_cons, Gen.shift, rotGen, Gen.widthAfter] at ih ⊢
      rw [show a + w + b + 2 = a + (w + 2) + b by omega, ih, show a + w + b - (i + b) = w - i + a by omega]
    | cap i =>
      simp only [Gen.fits, decide_eq_true_eq] at hf
      simp only [shiftW_cons, rot_cons, Gen.shift, rotGen, Gen.widthAfter] at ih ⊢
      rw [show a + w + b - 2 = a + (w - 2) + b by omega, ih, show a + (w - 2) + b - (i + b) = w - 2 - i + a by omega]

One action

theorem room_pad_ge (k m : Nat) (s : State) (n : Needle) : room s n ≤ room (s.pad k m) n
Proof
:= by
  cases n <;> simp [room]
theorem between_le (s : State) {n : Needle} {c : Nat} (hn : n ≠ .right) (hr : c ≤ room s n) :
    s.between n + 2 * c ≤ s.yarnAt
Proof
:= by
  cases n <;> simp_all [State.between, State.yarnAt, room] <;> split <;> omega
theorem into_ne_right {i : Inside} (hc : canInsert i.needle i.into = true) (hn : i.needle = .right) :
    i.into ≠ .right
Proof
:= by
  intro h; rw [hn, h] at hc; exact absurd hc (by decide)
theorem flip_into_ne_right {i : Inside} (hc : canInsert i.needle i.into = true) (hn : i.needle = .left) :
    i.flip.into ≠ .right
Proof
:= by
  show i.into.flip ≠ .right
  cases h : i.into <;> rw [hn, h] at hc <;> simp_all [Needle.flip, canInsert]
theorem room_flip (s : State) (n : Needle) : room s.flip n.flip = room s n
Proof
:= by cases n <;> rfl
theorem wrapRight_pad (k m : Nat) {s s' : State} {i : Inside} {sense : Sense} {u : MWord}
    (hr : i.count ≤ room s i.into) (h : wrapRight s i sense = some (s', u)) :
    wrapRight (s.pad k m) i sense = some (s'.pad k m, shiftW (2 * k) u)
Proof
:= by
  unfold wrapRight at h ⊢
  split_ifs at h with hc
  obtain ⟨h1, h2, h3, h4⟩ := hc
  simp only [Option.some.injEq, Prod.mk.injEq] at h
  obtain ⟨rfl, rfl⟩ := h
  rw [ite_eq_left ⟨h1, h2, h3, h4⟩, wrapWord_pad k m s _ _ _ _ (between_le s (by rcases h3 with h3 | h3 <;> rw [h3] <;>
    decide) hr)]
  rfl
theorem wrapRight_word {s s' : State} {i : Inside} {sense : Sense} {u : MWord}
    (h : wrapRight s i sense = some (s', u)) : u = wrapWord s i.into i.way i.count sense ∧ i.wrapped = false ∧
      (i.into = .left ∨ i.into = .cable)
Proof
:= by
  unfold wrapRight at h
  split_ifs at h with hc
  simp only [Option.some.injEq, Prod.mk.injEq] at h
  exact ⟨h.2.symm, hc.2.1, hc.2.2.1⟩
theorem takeOutRight_word {s s' : State} {i : Inside} {u : MWord}
    (h : takeOutRight s i = some (s', u)) : u = takeOutWord s i.into i.way i.count i.round ∧ i.wrapped = true
Proof
:= by
  unfold takeOutRight at h
  split_ifs at h with hc
  simp only [Option.some.injEq, Prod.mk.injEq] at h
  exact ⟨h.2.symm, hc.2⟩
theorem takeOutRight_pad (k m : Nat) {s s' : State} {i : Inside} {u : MWord}
    (hb : s.between i.into + 2 * i.count ≤ s.yarnAt) (h : takeOutRight s i = some (s', u)) :
    takeOutRight (s.pad k m) i = some (s'.pad k m, shiftW (2 * k) u)
Proof
:= by
  unfold takeOutRight at h ⊢
  split_ifs at h with hc
  simp only [Option.some.injEq, Prod.mk.injEq] at h
  obtain ⟨rfl, rfl⟩ := h
  rw [ite_eq_left hc, takeOutWord_pad k m s _ _ _ _ hb]
  simp only [Option.some.injEq, Prod.mk.injEq, and_true]
  pad_eq
theorem asLeft_pad (k m : Nat) {s s' : State} {u : MWord} {f : State → Option (State × MWord)}
    (hf : s.cable = 0 → s.bar = false → ∀ t v, f s.flip = some (t, v) →
      f (s.flip.pad m k) = some (t.pad m k, shiftW (2 * m) v) ∧ (topWidth s.width v).isSome)
    (h : asLeft s f = some (s', u)) : asLeft (s.pad k m) f = some (s'.pad k m, shiftW (2 * k) u)
Proof
:= by
  unfold asLeft at h ⊢
  split_ifs at h with hc
  rw [ite_eq_left (show (s.pad k m).cable = 0 ∧ (s.pad k m).bar = false from hc)]
  cases hfs : f s.flip with
  | none => simp [hfs] at h
  | some p =>
    obtain ⟨t, v⟩ := p
    simp only [hfs, Option.map_some, Option.some.injEq, Prod.mk.injEq] at h
    obtain ⟨rfl, rfl⟩ := h
    obtain ⟨h1, h2⟩ := hf hc.1 hc.2 t v hfs
    rw [pad_flip, h1, Option.map_some, pad_width, rot_shift _ _ v _ h2]
    rfl
theorem flip_inside {s : State} {i : Inside} (hi : s.inside = some i) : s.flip.inside = some i.flip
Proof
:= by
  simp [State.flip, hi]
theorem wrapLeft_pad (k m : Nat) {s s' : State} {u : MWord} {i : Inside} {sense : Sense} (hi : s.inside = some i)
    (hn : i.needle = .left) (hc : canInsert i.needle i.into = true) (hr : i.count ≤ room s i.into)
    (h : asLeft s (fun t => t.inside.bind fun j => wrapRight t j sense) = some (s', u)) :
    asLeft (s.pad k m) (fun t => t.inside.bind fun j => wrapRight t j sense) = some (s'.pad k m, shiftW (2 * k) u)
Proof
:= by
  refine asLeft_pad k m (fun hc0 hb0 t v hw => ?_) h
  have hfi := flip_inside hi
  simp only [hfi, Option.bind_some] at hw
  have hr' : i.flip.count ≤ room s.flip i.flip.into := by
    rw [show i.flip.into = i.into.flip from rfl, room_flip]; exact hr
  refine ⟨?_, ?_⟩
  · simp only [pad_inside, hfi, Option.bind_some]
    exact wrapRight_pad m k hr' hw
  · obtain ⟨rfl, hw0, -⟩ := wrapRight_word hw
    have hb := between_le s.flip (flip_into_ne_right hc hn) hr'
    have hw1 : i.wrapped = false := hw0
    have e : s.width = s.flip.yarnAt + 1 + 2 * s.left := by
      simp [State.width, State.yarnAt, State.flip, State.wrapping, hi, hw1, hc0, hb0]; omega
    rw [e, wrapWord_wf _ _ _ _ _ _ hb]; rfl
theorem takeOutLeft_pad (k m : Nat) {s s' : State} {u : MWord} {i : Inside} (hi : s.inside = some i)
    (hn : i.needle = .left) (hc : canInsert i.needle i.into = true) (hr : i.count ≤ room s i.into)
    (h : asLeft s (fun t => t.inside.bind fun j => takeOutRight t j) = some (s', u)) :
    asLeft (s.pad k m) (fun t => t.inside.bind fun j => takeOutRight t j) = some (s'.pad k m, shiftW (2 * k) u)
Proof
:= by
  refine asLeft_pad k m (fun hc0 hb0 t v hw => ?_) h
  have hfi := flip_inside hi
  simp only [hfi, Option.bind_some] at hw
  have hr' : i.flip.count ≤ room s.flip i.flip.into := by
    rw [show i.flip.into = i.into.flip from rfl, room_flip]; exact hr
  have hb := between_le s.flip (flip_into_ne_right hc hn) hr'
  refine ⟨?_, ?_⟩
  · simp only [pad_inside, hfi, Option.bind_some]
    exact takeOutRight_pad m k hb hw
  · obtain ⟨rfl, hw0⟩ := takeOutRight_word hw
    have hw1 : i.wrapped = true := hw0
    have e : s.width = s.flip.yarnAt + 3 + 2 * s.left := by
      simp [State.width, State.yarnAt, State.flip, State.wrapping, hi, hw1, hc0, hb0]; omega
    rw [e, takeOutWord_wf _ _ _ _ _ _ hb]; rfl
theorem transfer_pad (k m : Nat) {s s' : State} {i : Inside} {n : Needle} {count : Nat} {u : MWord}
    (hr : i.count ≤ room s i.into) (h : transfer s i n count = some (s', u)) :
    transfer (s.pad k m) i n count = some (s'.pad k m, shiftW (2 * k) u)
Proof
:= by
  unfold transfer at h ⊢
  split_ifs at h with h0
  rw [ite_eq_right h0]
  simp only [not_or, not_not] at h0
  obtain ⟨-, h0⟩ := h0
  subst h0
  cases hn1 : i.needle <;> cases hn2 : i.into <;> simp only [hn1, hn2] at h ⊢ <;> rw [hn2] at hr <;>
    simp only [room] at hr
  all_goals try (simp at h; done)
  · split_ifs at h with hp
    if_left
    · exact hp
    simp only [Option.some.injEq, Prod.mk.injEq] at h ⊢
    obtain ⟨rfl, rfl⟩ := h
    refine ⟨by pad_eq, ?_⟩
    rw [shiftW_append, pad_yarnAt, pad_isBack, shiftW_strandRight]
    cases i.way <;> simp only [shiftW_halfTurn, shiftW_nil]
  · simp only [Option.some.injEq, Prod.mk.injEq] at h ⊢
    obtain ⟨rfl, rfl⟩ := h
    exact ⟨by pad_eq, rfl⟩
  · split_ifs at h with hp
    if_left
    · exact hp
    simp only [decide_eq_true_eq] at hp
    simp only [Option.some.injEq, Prod.mk.injEq] at h ⊢
    obtain ⟨rfl, rfl⟩ := h
    have hy : s.yarnAt = 2 * s.left := by simp [State.yarnAt, hp.1, hp.2]
    refine ⟨by pad_eq, ?_⟩
    rw [shiftW_append, pad_yarnAt, pad_isBack, shiftW_strandLeft _ _ _ _ (by omega)]
    cases i.way <;> simp only [shiftW_halfTurn, shiftW_nil, List.append_cancel_left_eq]
    congr 1; omega
  · simp only [Option.some.injEq, Prod.mk.injEq] at h ⊢
    obtain ⟨rfl, rfl⟩ := h
    exact ⟨by pad_eq, rfl⟩
theorem fits_inside {s : State} {i : Inside} (hs : s.Fits) (hi : s.inside = some i) :
    canInsert i.needle i.into = true ∧ i.count ≤ room s i.into
Proof
:= by
  simpa [State.Fits, hi] using hs
open Local
theorem step_pad (k m : Nat) {s s' : State} {a : Action} {u : MWord} (hs : s.Fits)
    (h : step s a = some (s', u)) : step (s.pad k m) a = some (s'.pad k m, shiftW (2 * k) u)
Proof
:= by
  cases a with
  | yarnBetween =>
    cases hi : s.inside with
    | none =>
      simp only [step, hi, pad_inside, Option.some.injEq, Prod.mk.injEq] at h ⊢
      obtain ⟨rfl, rfl⟩ := h
      exact ⟨rfl, rfl⟩
    | some i =>
      simp only [step, hi, pad_inside] at h ⊢
      split_ifs at h ⊢
      simp only [Option.some.injEq, Prod.mk.injEq] at h ⊢
      obtain ⟨rfl, rfl⟩ := h
      exact ⟨rfl, rfl⟩
  | yarnOver sense =>
    cases hi : s.inside <;> cases sense <;> cases hy : s.yarn <;>
      simp only [step, hi, hy, pad_inside, pad_yarn, reduceCtorEq] at h ⊢ <;>
      simp only [Option.some.injEq, Prod.mk.injEq] at h ⊢ <;>
      obtain ⟨rfl, rfl⟩ := h <;>
      exact ⟨by pad_eq, by simp only [shiftW, List.map, Gen.shift, pad_yarnAt, Nat.add_right_comm]⟩
  | insert n n' w count round =>
    simp only [step] at h ⊢
    split_ifs at h with hc
    obtain ⟨h1, h2, h3, h4⟩ := hc
    if_left
    · exact ⟨h1, h2, h3, le_trans h4 (room_pad_ge k m s n')⟩
    simp only [Option.some.injEq, Prod.mk.injEq] at h ⊢
    obtain ⟨rfl, rfl⟩ := h
    exact ⟨rfl, rfl⟩
  | wrap sense =>
    cases hi : s.inside with
    | none => simp [step, hi] at h
    | some i =>
      have hf := fits_inside hs hi
      simp only [step, hi, pad_inside] at h ⊢
      cases hn : i.needle with
      | right => simp only [hn] at h ⊢; exact wrapRight_pad k m hf.2 h
      | left => simp only [hn] at h ⊢; exact wrapLeft_pad k m hi hn hf.1 hf.2 h
      | cable => simp [hn] at h
  | takeOut =>
    cases hi : s.inside with
    | none => simp [step, hi] at h
    | some i =>
      have hf := fits_inside hs hi
      simp only [step, hi, pad_inside] at h ⊢
      split_ifs at h ⊢ with hw
      · cases hn : i.needle with
        | right =>
          simp only [hn] at h ⊢
          exact takeOutRight_pad k m (between_le s (into_ne_right hf.1 hn) hf.2) h
        | left => simp only [hn] at h ⊢; exact takeOutLeft_pad k m hi hn hf.1 hf.2 h
        | cable => simp [hn] at h
      · simp only [Option.some.injEq, Prod.mk.injEq] at h ⊢
        obtain ⟨rfl, rfl⟩ := h
        exact ⟨rfl, rfl⟩
  | letOff n count =>
    cases hi : s.inside with
    | none =>
      cases n with
      | left =>
        simp only [step, hi, pad_inside] at h ⊢
        split_ifs at h with hc
        if_left
        · exact Nat.le_trans hc (Nat.le_add_right _ _)
        simp only [Option.some.injEq, Prod.mk.injEq] at h ⊢
        obtain ⟨rfl, rfl⟩ := h
        refine ⟨by pad_eq, ?_⟩
        rw [shiftW_closeLeft _ _ _ (by omega)]
        congr 1; simp only [pad_left]; omega
      | right =>
        simp only [step, hi, pad_inside] at h ⊢
        split_ifs at h with hc
        if_left
        · exact Nat.le_trans hc (Nat.le_add_right _ _)
        simp only [Option.some.injEq, Prod.mk.injEq] at h ⊢
        obtain ⟨rfl, rfl⟩ := h
        refine ⟨by pad_eq, ?_⟩
        rw [shiftW_replicate, pad_yarnAt]
        simp only [Gen.shift, Nat.add_right_comm]
      | cable =>
        simp only [step, hi, pad_inside] at h ⊢
        split_ifs at h with hc
        if_left
        · exact hc
        simp only [Option.some.injEq, Prod.mk.injEq] at h ⊢
        obtain ⟨rfl, rfl⟩ := h
        refine ⟨rfl, ?_⟩
        rw [shiftW_closeLeft _ _ _ (by omega)]
        congr 1; simp only [pad_left, pad_cable]; omega
    | some i =>
      have hf := fits_inside hs hi
      simp only [step, hi, pad_inside] at h ⊢
      split_ifs at h ⊢
      exact transfer_pad k m hf.2 h
  | passOver n count =>
    cases n with
    | right =>
      simp only [step] at h ⊢
      split_ifs at h with hc h2
      if_left
      · exact hc
      if_left
      · exact Nat.le_trans h2 (Nat.le_add_right _ _)
      simp only [Option.some.injEq, Prod.mk.injEq] at h ⊢
      obtain ⟨rfl, rfl⟩ := h
      refine ⟨by pad_eq, ?_⟩
      rw [shiftW_flatten, List.map_replicate, pad_yarnAt]
      congr 2
      simp only [passRight, shiftW, List.map, Gen.shift, Nat.add_right_comm]
    | left =>
      simp only [step] at h ⊢
      split_ifs at h with hc h2
      if_left
      · exact hc
      if_left
      · exact Nat.le_trans h2 (Nat.le_add_right _ _)
      simp only [Option.some.injEq, Prod.mk.injEq] at h ⊢
      obtain ⟨rfl, rfl⟩ := h
      refine ⟨by pad_eq, ?_⟩
      rw [pad_yarnAt, shiftW_passLeftAll _ _ _ (by simp only [State.yarnAt]; split <;> omega)]
    | cable => simp [step] at h
  | hold side =>
    simp only [step] at h ⊢
    split_ifs at h with hc
    if_left
    · exact hc
    simp only [Option.some.injEq, Prod.mk.injEq] at h ⊢
    obtain ⟨rfl, rfl⟩ := h
    exact ⟨rfl, rfl⟩
  | pickUp side =>
    simp only [step] at h ⊢
    split_ifs at h with hc
    if_left
    · exact hc
    simp only [Option.some.injEq, Prod.mk.injEq] at h ⊢
    obtain ⟨rfl, rfl⟩ := h
    refine ⟨by pad_eq, ?_⟩
    cases side
    · rfl
    · simp only [shiftW, List.map, Gen.shift, pad_left]; congr 2; omega
namespace Local
theorem Needle.flip_flip (n : Needle) : n.flip.flip = n
Proof
:= by cases n <;> rfl
theorem Side.flip_flip (s : Side) : s.flip.flip = s
Proof
:= by cases s <;> rfl
theorem fits_of_same {s t : State} (hs : s.Fits) (hi : t.inside = s.inside) (hl : t.left = s.left)
    (hr : t.right = s.right) (hc : t.cable = s.cable) : t.Fits
Proof
:= by
  unfold State.Fits at hs ⊢
  rw [hi]
  cases h : s.inside with
  | none => trivial
  | some i =>
    simp only [h] at hs
    refine ⟨hs.1, ?_⟩
    cases hn : i.into <;> rw [hn] at hs <;> simp only [room, hl, hr, hc] at hs ⊢ <;> exact hs.2
theorem fits_wrapLeft {s s' : State} {u : MWord} {i : Inside} {sense : Sense} (hs : s.Fits)
    (hi : s.inside = some i) (h : asLeft s (fun t => t.inside.bind fun j => wrapRight t j sense) = some (s', u)) :
    s'.Fits
Proof
:= by
  unfold asLeft at h
  split_ifs at h with hc
  simp only [flip_inside hi, Option.bind_some, wrapRight] at h
  split_ifs at h
  rotate_left
  · simp at h
  simp only [Option.map_some, Option.some.injEq, Prod.mk.injEq] at h
  obtain ⟨rfl, -⟩ := h
  have hf := fits_inside hs hi
  simp only [State.Fits, State.flip, Option.map_some, Inside.flip, Needle.flip_flip, Side.flip_flip]
  refine ⟨hf.1, ?_⟩
  cases hn : i.into <;> rw [hn] at hf <;> simpa [room, Needle.flip] using hf.2
theorem fits_takeOutLeft {s s' : State} {u : MWord} {i : Inside}
    (hi : s.inside = some i) (h : asLeft s (fun t => t.inside.bind fun j => takeOutRight t j) = some (s', u)) :
    s'.Fits
Proof
:= by
  unfold asLeft at h
  split_ifs at h with hc
  simp only [flip_inside hi, Option.bind_some, takeOutRight] at h
  split_ifs at h
  rotate_left
  · simp at h
  simp only [Option.map_some, Option.some.injEq, Prod.mk.injEq] at h
  obtain ⟨rfl, -⟩ := h
  exact State.fits_of_inside rfl
theorem step_fits {s s' : State} {a : Action} {u : MWord} (hs : s.Fits) (h : step s a = some (s', u)) :
    s'.Fits
Proof
:= by
  cases a with
  | yarnBetween =>
    cases hi : s.inside with
    | none =>
      simp only [step, hi, Option.some.injEq, Prod.mk.injEq] at h
      obtain ⟨rfl, -⟩ := h
      exact State.fits_of_inside rfl
    | some i =>
      simp only [step, hi] at h
      split_ifs at h
      simp only [Option.some.injEq, Prod.mk.injEq] at h
      obtain ⟨rfl, -⟩ := h
      exact fits_of_same hs hi.symm rfl rfl rfl
  | yarnOver sense =>
    cases hi : s.inside <;> cases sense <;> cases hy : s.yarn <;>
      simp only [step, hi, hy, reduceCtorEq, Option.some.injEq, Prod.mk.injEq] at h <;>
      obtain ⟨rfl, -⟩ := h <;> exact State.fits_of_inside rfl
  | insert n n' w count round =>
    simp only [step] at h
    split_ifs at h with hc
    simp only [Option.some.injEq, Prod.mk.injEq] at h
    obtain ⟨rfl, -⟩ := h
    refine ⟨hc.2.1, ?_⟩
    cases n' <;> simpa [room] using hc.2.2.2
  | wrap sense =>
    cases hi : s.inside with
    | none => simp [step, hi] at h
    | some i =>
      simp only [step, hi] at h
      cases hn : i.needle with
      | right =>
        simp only [hn, wrapRight] at h
        split_ifs at h
        simp only [Option.some.injEq, Prod.mk.injEq] at h
        obtain ⟨rfl, -⟩ := h
        have hf := fits_inside hs hi
        rw [hn] at hf
        refine ⟨hf.1, ?_⟩
        cases hn' : i.into <;> rw [hn'] at hf <;> simpa [room] using hf.2
      | left => simp only [hn] at h; exact fits_wrapLeft hs hi h
      | cable => simp [hn] at h
  | takeOut =>
    cases hi : s.inside with
    | none => simp [step, hi] at h
    | some i =>
      simp only [step, hi] at h
      split_ifs at h
      · cases hn : i.needle with
        | right =>
          simp only [hn, takeOutRight] at h
          split_ifs at h
          simp only [Option.some.injEq, Prod.mk.injEq] at h
          obtain ⟨rfl, -⟩ := h
          exact State.fits_of_inside rfl
        | left => simp only [hn] at h; exact fits_takeOutLeft hi h
        | cable => simp [hn] at h
      · simp only [Option.some.injEq, Prod.mk.injEq] at h
        obtain ⟨rfl, -⟩ := h
        exact State.fits_of_inside rfl
  | letOff n count =>
    cases hi : s.inside with
    | none =>
      cases n <;> simp only [step, hi] at h <;> split_ifs at h <;>
        simp only [Option.some.injEq, Prod.mk.injEq] at h <;> obtain ⟨rfl, -⟩ := h <;> exact State.fits_of_inside rfl
    | some i =>
      simp only [step, hi] at h
      split_ifs at h
      unfold transfer at h
      split_ifs at h
      cases hn1 : i.needle <;> cases n <;> simp only [hn1] at h <;> (try split_ifs at h) <;>
        simp only [reduceCtorEq, Option.some.injEq, Prod.mk.injEq] at h <;> obtain ⟨rfl, -⟩ := h <;>
        exact State.fits_of_inside rfl
  | passOver n count =>
    cases n <;> simp only [step] at h <;> split_ifs at h with hc <;>
      simp only [Option.some.injEq, Prod.mk.injEq] at h <;> obtain ⟨rfl, -⟩ := h <;>
      exact State.fits_of_inside hc.1
  | hold side =>
    simp only [step] at h
    split_ifs at h with hc
    simp only [Option.some.injEq, Prod.mk.injEq] at h
    obtain ⟨rfl, -⟩ := h
    exact State.fits_of_inside hc
  | pickUp side =>
    simp only [step] at h
    split_ifs at h with hc
    simp only [Option.some.injEq, Prod.mk.injEq] at h
    obtain ⟨rfl, -⟩ := h
    exact State.fits_of_inside hc.1
theorem draw_fits : ∀ {s s' : State} {p : Program} {u : MWord}, s.Fits → draw s p = some (s', u) → s'.Fits
Proof
  | s, s', [], u, hs, h => by
    simp only [draw, Option.some.injEq, Prod.mk.injEq] at h
    obtain ⟨rfl, -⟩ := h
    exact hs
  | s, s', a :: as, u, hs, h => by
    simp only [draw] at h
    cases h1 : step s a with
    | none => simp [h1] at h
    | some p =>
      obtain ⟨t, v⟩ := p
      simp only [h1] at h
      cases h2 : draw t as with
      | none => simp [h2] at h
      | some q =>
        obtain ⟨t', w⟩ := q
        simp only [h2, Option.some.injEq, Prod.mk.injEq] at h
        obtain ⟨rfl, -⟩ := h
        exact draw_fits (step_fits hs h1) h2
theorem draw_pad (k m : Nat) : ∀ {s s' : State} {p : Program} {u : MWord}, s.Fits → draw s p = some (s', u) →
    draw (s.pad k m) p = some (s'.pad k m, shiftW (2 * k) u)
Proof
  | s, s', [], u, _, h => by
    simp only [draw, Option.some.injEq, Prod.mk.injEq] at h
    obtain ⟨rfl, rfl⟩ := h
    rfl
  | s, s', a :: as, u, hs, h => by
    simp only [draw] at h
    cases h1 : step s a with
    | none => simp [h1] at h
    | some p =>
      obtain ⟨t, v⟩ := p
      simp only [h1] at h
      cases h2 : draw t as with
      | none => simp [h2] at h
      | some q =>
        obtain ⟨t', w⟩ := q
        simp only [h2, Option.some.injEq, Prod.mk.injEq] at h
        obtain ⟨rfl, rfl⟩ := h
        simp only [draw, step_pad k m hs h1, draw_pad k m (step_fits hs h1) h2, shiftW_append]
theorem Runs.pad (k m : Nat) {s e : State} {p : Program} (hs : s.Fits) (h : Runs s p e) :
    Runs (s.pad k m) p (e.pad k m)
Proof
:= by
  unfold Runs at h ⊢
  cases hd : draw s p with
  | none => simp [hd] at h
  | some q =>
    obtain ⟨e', u⟩ := q
    rw [hd] at h
    obtain ⟨rfl, hw⟩ := h
    rw [draw_pad k m hs hd, pad_width, pad_width]
    exact ⟨rfl, topWidth_shift hw _ _⟩
theorem drawing_pad (k m : Nat) {s s' : State} {p : Program} {u : MWord} (hs : s.Fits)
    (h : draw s p = some (s', u)) : drawing (s.pad k m) p = shiftW (2 * k) (drawing s p)
Proof
:= by
  simp only [drawing, h, draw_pad k m hs h]

From hand knitting to the shorthand: rules in any row

A rule (Rule s p s' r w r') says three things:

  • The hand program p runs from s to s', and its drawing fits (Runs).
  • The shorthand program w runs from the row r to the row r'.
  • The two drawings are related by moves (Iso).

Rule.inRow: a rule from a state that fits (State.Fits) is also a rule in a longer row. The hand state gets k more loops at the far end of the left needle and m more loops at the far end of the right needle (State.pad). The shorthand row gets the objects A before it and Z after it (D_frame). The condition: A has no yarn and rowWidth A = 2 * k. The proof uses draw_pad, D_frame and Iso.shift.

Rule.append: two rules, one after the other, make a rule for the two programs, one after the other.

Rule Hand program, from Shorthand program, from Shorthand row at the end
ktog_rule_inRow work .along c ++ [.letOff .left c], c loops, yarn at the back c - 1 merges, knit .L .f [.yarn, .stack 1]
ptog_rule_inRow work .against c ++ [.letOff .left c], c loops, yarn in front c - 1 merges, knit .L .b [.yarn, .stack 1]
yo_rule [.yarnOver .usual], yarn in front [.tuck .L] [.yarn, .stack 1]
yo_other_rule [.yarnOver .other], yarn at the back [.tuck .R, .x 0 false] [.yarn, .stack 1]
slip_rule slipPurlwise, one loop, yarn at the back or in front [.x 0 b], b: the yarn is at the back [.yarn, .stack 1]
keep_knit_rule work .along, one loop, yarn at the back [.split .L .f, .x 1 true, .x 0 false] [.stack 1, .yarn, .stack 1]
keep_purl_rule work .against, one loop, yarn in front [.split .L .b, .x 0 true] [.stack 1, .yarn, .stack 1]
  • Each rule of the table also has a form in any row (…_inRow).
  • slip_rule and yo_rule: the two drawings are the same word.
  • keep_knit_rule and keep_purl_rule: the first half of a knit or a purl into the front and the back of one loop. The old loop stays on the left needle. In the shorthand, a split makes the new loop and keeps the old loop. Then exchanges put the old loop on the left of the yarn and the new loop on its right. The chains of moves are proved by checkChain.
namespace Hand
open Gen
def Rule (s : State) (p : Program) (s' : State) (r : Row) (w : List Step) (r' : Row) : Prop :=
  Runs s p s' ∧ ∃ v, D r w = some (r', v) ∧ Iso s.width (drawing s p) v
theorem Runs.spec {s e : State} {p : Program} (h : Runs s p e) :
    ∃ u, draw s p = some (e, u) ∧ topWidth s.width u = some e.width
Proof
:= by
  unfold Runs at h
  cases hd : draw s p with
  | none => rw [hd] at h; exact h.elim
  | some q =>
    obtain ⟨e', u⟩ := q
    rw [hd] at h
    obtain ⟨rfl, hw⟩ := h
    exact ⟨u, rfl, hw⟩
theorem Rule.inRow {s s' : State} {p : Program} {r r' : Row} {w : List Step} (hs : s.Fits)
    (h : Rule s p s' r w r') (k m : Nat) (A Z : Row) (hA : Obj.yarn ∉ A) (hk : rowWidth A = 2 * k) :
    Rule (s.pad k m) p (s'.pad k m) (A ++ r ++ Z) (w.map (Step.shift A.length)) (A ++ r' ++ Z)
Proof
:= by
  obtain ⟨hr, v, hD, hI⟩ := h
  obtain ⟨u, hd, -⟩ := hr.spec
  refine ⟨hr.pad k m hs, shiftW (2 * k) v, ?_, ?_⟩
  · rw [← hk]; exact D_frame A Z hA hD
  · rw [drawing_pad k m hs hd, Local.pad_width]
    exact hI.shift (2 * k) (2 * m)
theorem Rule.append {s s' s'' : State} {p q : Program} {r r' r'' : Row} {w w' : List Step}
    (h1 : Rule s p s' r w r') (h2 : Rule s' q s'' r' w' r'') : Rule s (p ++ q) s'' r (w ++ w') r''
Proof
:= by
  obtain ⟨hr1, v1, hD1, hI1⟩ := h1
  obtain ⟨hr2, v2, hD2, hI2⟩ := h2
  obtain ⟨u1, hd1, hw1⟩ := hr1.spec
  obtain ⟨u2, hd2, hw2⟩ := hr2.spec
  have hd := draw_app hd1 hd2
  refine ⟨?_, v1 ++ v2, ?_, ?_⟩
  · unfold Runs
    rw [hd]
    exact ⟨rfl, topWidth_append_of hw1 hw2⟩
  · rw [D_append, hD1]
    simp [hD2]
  · simp only [drawing, hd, hd1, hd2] at hI1 hI2 ⊢
    have hv1 : topWidth s.width v1 = some s'.width := by rw [← hI1.topWidth_eq]; exact hw1
    exact (Iso.suffix u2 hI1 (by rw [topWidth_append_of hw1 hw2]; rfl)).trans (Iso.prefix hv1 hI2)
theorem start_pad (c k m : Nat) (y : Side) : (start c y).pad k m = { left
Proof
:= c + k, right := m, yarn := y } := by
  simp [State.pad, start]
theorem start_fits (c : Nat) (y : Side) : (start c y).Fits
Proof
:= State.fits_of_inside rfl

Knit and purl through c loops, in any row

theorem rule_of_ktog {c : Nat} {y : Side} {p : Program} {w : List Step}
    (h : Runs (start c y) p (oneLoop y) ∧ ∃ u, D (List.replicate c (.stack 1) ++ [.yarn]) w = some ([.yarn, .stack 1], u) ∧
      Iso (2 * c + 1) (drawing (start c y) p) u) :
    Rule (start c y) p (oneLoop y) (List.replicate c (.stack 1) ++ [.yarn]) w [.yarn, .stack 1]
Proof
:= by
  obtain ⟨hr, u, hD, hI⟩ := h
  exact ⟨hr, u, hD, by rw [Compile.start_width]; exact hI⟩
theorem map_shift_ktog (n c : Nat) (d : Dir) (bd : Bed) :
    (List.replicate (c - 1) (Step.merge 0) ++ [Step.knit d bd]).map (Step.shift n) =
      List.replicate (c - 1) (.merge n) ++ [.knit d bd]
Proof
:= by
  simp [List.map_replicate, Step.shift]
theorem ktog_rule_inRow (c : Nat) (hc : 1 ≤ c) (k m : Nat) (A Z : Row) (hA : Obj.yarn ∉ A)
    (hk : rowWidth A = 2 * k) :
    Rule ((start c .back).pad k m) (work .along c ++ [.letOff .left c]) ((oneLoop .back).pad k m)
      (A ++ (List.replicate c (.stack 1) ++ [.yarn]) ++ Z) (List.replicate (c - 1) (.merge A.length) ++ [.knit .L .f])
      (A ++ [.yarn, .stack 1] ++ Z)
Proof
:= by
  have := (rule_of_ktog (ktog_rule c hc)).inRow (start_fits c .back) k m A Z hA hk
  rwa [map_shift_ktog] at this
theorem ptog_rule_inRow (c : Nat) (hc : 1 ≤ c) (k m : Nat) (A Z : Row) (hA : Obj.yarn ∉ A)
    (hk : rowWidth A = 2 * k) :
    Rule ((start c .front).pad k m) (work .against c ++ [.letOff .left c]) ((oneLoop .front).pad k m)
      (A ++ (List.replicate c (.stack 1) ++ [.yarn]) ++ Z) (List.replicate (c - 1) (.merge A.length) ++ [.knit .L .b])
      (A ++ [.yarn, .stack 1] ++ Z)
Proof
:= by
  have := (rule_of_ktog (ptog_rule c hc)).inRow (start_fits c .front) k m A Z hA hk
  rwa [map_shift_ktog] at this

Yarn over

theorem yo_rule : Rule (start 0 .front) [.yarnOver .usual] (oneLoop .back) [.yarn] [.tuck .L] [.yarn, .stack 1]
Proof
:=
  ⟨by decide, [cup 0], rfl, Iso.refl _ _⟩
theorem yo_other_rule :
    Rule (start 0 .back) [.yarnOver .other] (oneLoop .front) [.yarn] [.tuck .R, .x 0 false] [.yarn, .stack 1]
Proof
:=
  ⟨by decide, [cup 1, cross 1 false, cross 0 false], rfl,
    (iso_of_checkChain _ [(0, .r1cup 1 false, true), (2, .r2 1 false, false), (0, .cupSlideR 0 false, true)] _
      (by decide)).symm⟩
theorem yo_rule_inRow (k m : Nat) (A Z : Row) (hA : Obj.yarn ∉ A) (hk : rowWidth A = 2 * k) :
    Rule ((start 0 .front).pad k m) [.yarnOver .usual] ((oneLoop .back).pad k m) (A ++ [.yarn] ++ Z) [.tuck .L]
      (A ++ [.yarn, .stack 1] ++ Z)
Proof
:=
  yo_rule.inRow (start_fits 0 .front) k m A Z hA hk
theorem yo_other_rule_inRow (k m : Nat) (A Z : Row) (hA : Obj.yarn ∉ A) (hk : rowWidth A = 2 * k) :
    Rule ((start 0 .back).pad k m) [.yarnOver .other] ((oneLoop .front).pad k m) (A ++ [.yarn] ++ Z)
      [.tuck .R, .x A.length false] (A ++ [.yarn, .stack 1] ++ Z)
Proof
:= by
  have := yo_other_rule.inRow (start_fits 0 .back) k m A Z hA hk
  simpa [Step.shift] using this

Slip purlwise

theorem slip_rule (y : Side) :
    Rule (start 1 y) slipPurlwise (oneLoop y) [.stack 1, .yarn] [.x 0 (decide (y = .back))] [.yarn, .stack 1]
Proof
:= by
  cases y
  · exact ⟨by decide, [cross 1 false, cross 0 false], rfl, Iso.refl _ _⟩
  · exact ⟨by decide, [cross 1 true, cross 0 true], rfl, Iso.refl _ _⟩
theorem slip_rule_inRow (y : Side) (k m : Nat) (A Z : Row) (hA : Obj.yarn ∉ A) (hk : rowWidth A = 2 * k) :
    Rule ((start 1 y).pad k m) slipPurlwise ((oneLoop y).pad k m) (A ++ [.stack 1, .yarn] ++ Z)
      [.x A.length (decide (y = .back))] (A ++ [.yarn, .stack 1] ++ Z)
Proof
:= by
  have := (slip_rule y).inRow (start_fits 1 y) k m A Z hA hk
  simpa [Step.shift] using this

Work into a loop and keep it

def twoLoops (y : Side) : State := { left := 1, right := 1, yarn := y }
theorem keep_knit_rule :
    Rule (start 1 .back) (work .along) (twoLoops .back) [.stack 1, .yarn] [.split .L .f, .x 1 true, .x 0 false]
      [.stack 1, .yarn, .stack 1]
Proof
:=
  ⟨by decide, _, rfl, iso_of_checkChain _
    [(6, .r2 1 false, true), (5, .r2 0 false, true),
      (9, .r2 1 true, false), (6, .farR (.cross 1 true) (.cross 3 true), true), (7, .r3 1 true, true),
      (5, .r3 2 true, true), (4, .r3 2 true, true), (3, .farR (.cross 1 true) (.cross 3 true), true),
      (2, .r2 3 false, true), (0, .r1cup 2 false, true), (4, .farL (.cross 3 true) (.cross 1 true), true),
      (0, .cupSlideR 1 true, true), (0, .farL (.cross 1 true) (.cup 1), false), (1, .r1cup 1 true, true),
      (2, .r2 0 true, false), (3, .farL (.cross 3 true) (.cross 0 false), false),
      (4, .farL (.cross 2 true) (.cross 0 false), false), (5, .r2 2 false, false), (6, .r2 3 false, false),
      (6, .r2 1 false, false), (7, .farL (.cross 3 false) (.cross 1 true), false), (8, .r2 2 false, false)]
    _ (by decide)⟩
theorem keep_purl_rule :
    Rule (start 1 .front) (work .against) (twoLoops .front) [.stack 1, .yarn] [.split .L .b, .x 0 true]
      [.stack 1, .yarn, .stack 1]
Proof
:=
  ⟨by decide, _, rfl, iso_of_checkChain _
    [(1, .farL (.cross 0 false) (.cup 0), true), (2, .r2 2 false, true), (1, .cupSlideR 0 false, false),
      (3, .r2 1 false, true)] _ (by decide)⟩
theorem twoLoops_pad (y : Side) (k m : Nat) : (twoLoops y).pad k m = { left
Proof
:= 1 + k, right := 1 + m, yarn := y } :=
  rfl
theorem keep_knit_rule_inRow (k m : Nat) (A Z : Row) (hA : Obj.yarn ∉ A) (hk : rowWidth A = 2 * k) :
    Rule ((start 1 .back).pad k m) (work .along) ((twoLoops .back).pad k m) (A ++ [.stack 1, .yarn] ++ Z)
      [.split .L .f, .x (1 + A.length) true, .x A.length false] (A ++ [.stack 1, .yarn, .stack 1] ++ Z)
Proof
:= by
  have := keep_knit_rule.inRow (start_fits 1 .back) k m A Z hA hk
  simpa [Step.shift, Nat.add_comm] using this
theorem keep_purl_rule_inRow (k m : Nat) (A Z : Row) (hA : Obj.yarn ∉ A) (hk : rowWidth A = 2 * k) :
    Rule ((start 1 .front).pad k m) (work .against) ((twoLoops .front).pad k m) (A ++ [.stack 1, .yarn] ++ Z)
      [.split .L .b, .x A.length true] (A ++ [.stack 1, .yarn, .stack 1] ++ Z)
Proof
:= by
  have := keep_purl_rule.inRow (start_fits 1 .front) k m A Z hA hk
  simpa [Step.shift] using this