Research | Knitting | Category 𝒮: Shorthand program

Category 𝒮: Shorthand program

The shorthand gives knitting as steps on a row. The row holds one yarn and stacks of loops. Each step draws a piece of tangle. Shorthand programs form a category. An arrow is a program. Composition runs one program after the other.

The direction a stitch is worked in, and the bed it is worked on

inductive Dir where
  | R
  | L
  deriving DecidableEq, Repr

inductive Bed where
  | f
  | b
  deriving DecidableEq, Repr

The shorthand: rows of the yarn and stacks of loops, its steps, and what each step draws

  • A row (Row), from left to right, holds the yarn and stacks.
  • stack k: k ≥ 1 loops on one needle. Drawn as 2k adjacent strands. The two legs of each loop are adjacent.
Step Machine operation Row Drawing
x a front Miss or rack Objects a and a + 1 change places. front: the left object is in front. Two blocks of strands cross
tuck d Tuck on an empty needle A new stack of one loop next to the yarn A cup
merge a Transfer onto an occupied needle Stacks a and a + 1 become one stack, left loops first Nothing
knit d bd Knit The stack next to the yarn is knit through. A new stack of one loop replaces it. The yarn goes through all loops of the stack
split d bd Split A new loop through the stack next to the yarn. The stack stays the same and goes to the needle across. The yarn goes through the stack
drop a Drop Stack a is removed One cap for each loop
  • runStep: one step. It returns the new row and the drawing of the step.
  • D: a program. Its drawing is the drawings of its steps, stacked.
  • The stitch insides follow Lin Fig. 12. The legs of a stack are bundles of strands.
  • Row after a split (splitRow): right, front bed [N, S, Y]; right, back bed [S, N, Y]; left, front bed [Y, N, S]; left, back bed [Y, S, N]. N: new loop. S: old stack. Y: yarn.

Rows of stacks

inductive Obj where
  | yarn
  | stack (k : Nat)
  deriving DecidableEq, Repr

abbrev Row := List Obj

def Obj.width : Obj → Nat
  | .yarn => 1
  | .stack k => 2 * k

def rowWidth (r : Row) : Nat := (r.map Obj.width).sum

def NoEmpty (r : Row) : Prop := ∀ k, Obj.stack k ∈ r → 0 < k

def offset (r : Row) (k : Nat) : Nat := rowWidth (r.take k)

def yarnIdx (r : Row) : Nat := r.idxOf .yarn

def splice (r : Row) (first len : Nat) (new : Row) : Row := r.take first ++ new ++ r.drop (first + len)

def stackAt (r : Row) (a : Nat) : Option Nat :=
  match r[a]? with
  | some (.stack k) => if 0 < k then some k else none
  | _ => none

The insides of the stitches

def pullThrough (n : Nat) : MWord :=
  ((List.range n).map fun k => Gen.cross k false) ++
  [Gen.cup (n + 1)] ++
  ((List.range n).map fun k => Gen.cross (n + 2 + k) false) ++
  ((List.range n).reverse.flatMap fun j => [Gen.cross j false, Gen.cross (j + 1) false, Gen.cap (j + 2)])

def tuckInterior : Dir → MWord
  | .R => [Gen.cup 1]
  | .L => [Gen.cup 0]

def knitInterior (d : Dir) (bd : Bed) (k : Nat) : MWord :=
  let firstLoop := if d = .R then 1 else 0
  let nest := (List.range k).flatMap fun i => swapBlocks (firstLoop + i + 1) 1 (2 * (k - i - 1)) true
  let front := pullThrough k
  let onBed := if bd = .f then front else reverseCrossings front
  let worked := if d = .R then onBed else mirrorAux (2 * k + 1) onBed
  nest ++ worked

def splitThrough : MWord :=
  [Gen.cross 0 false, Gen.cup 2, Gen.cross 3 false, Gen.cross 0 false, Gen.cross 1 false]

def splitInterior (d : Dir) (bd : Bed) : MWord :=
  let drawn₁ := if bd = .b then reverseCrossings splitThrough else splitThrough
  let drawn := if d = .L then mirrorAux 3 drawn₁ else drawn₁
  drawn ++ match d, bd with
    | .R, .f => []
    | .L, .b => []
    | .L, .f => swapBlocks 1 2 2 false
    | .R, .b => swapBlocks 0 2 2 false

def nestK (o k : Nat) : MWord :=
  (List.range k).flatMap fun i => swapBlocks (o + i + 1) 1 (2 * (k - i - 1)) true

def unnestK (o k : Nat) : MWord := reverseCrossings (nestK o k).reverse

def splitThroughK (k : Nat) : MWord :=
  swapBlocks 0 1 k false ++ [Gen.cup (k + 1)] ++ swapBlocks (k + 2) 1 k false ++ swapBlocks 0 k 1 false ++
    swapBlocks 1 k 1 false

def oldAt : Dir → Bed → Nat
  | .R, .f => 2
  | .R, .b => 0
  | .L, .f => 3
  | .L, .b => 1

def splitInteriorK (d : Dir) (bd : Bed) (k : Nat) : MWord :=
  let drawn₁ := if bd = .b then reverseCrossings (splitThroughK k) else splitThroughK k
  let drawn := if d = .L then mirrorAux (2 * k + 1) drawn₁ else drawn₁
  nestK (if d = .R then 1 else 0) k ++ drawn ++
    (match d, bd with
      | .R, .f => []
      | .L, .b => []
      | .L, .f => swapBlocks 1 (2 * k) 2 false
      | .R, .b => swapBlocks 0 2 (2 * k) false) ++
    unnestK (oldAt d bd) k

def splitRow : Dir → Bed → Nat → Row
  | .R, .f, k => [.stack 1, .stack k, .yarn]
  | .R, .b, k => [.stack k, .stack 1, .yarn]
  | .L, .f, k => [.yarn, .stack 1, .stack k]
  | .L, .b, k => [.yarn, .stack k, .stack 1]

def tuckRow : Dir → Row
  | .R => [.stack 1, .yarn]
  | .L => [.yarn, .stack 1]

Steps

inductive Step where
  | x (at_ : Nat) (front : Bool)
  | tuck (d : Dir)
  | merge (at_ : Nat)
  | knit (d : Dir) (bd : Bed)
  | split (d : Dir) (bd : Bed)
  | drop (at_ : Nat)
  deriving DecidableEq, Repr

def besideIdx (r : Row) : Dir → Nat
  | .R => yarnIdx r + 1
  | .L => yarnIdx r - 1

def hasBeside (r : Row) : Dir → Bool
  | .R => decide (yarnIdx r + 1 < r.length)
  | .L => decide (1 ≤ yarnIdx r)

def firstIdx (r : Row) : Dir → Nat
  | .R => yarnIdx r
  | .L => yarnIdx r - 1

def runStep (r : Row) : Step → Option (Row × MWord)
  | .x a front =>
    if Obj.yarn ∈ r ∧ a + 1 < r.length then
      some (splice r a 2 [r.getD (a + 1) .yarn, r.getD a .yarn],
            swapBlocks (offset r a) (r.getD a .yarn).width (r.getD (a + 1) .yarn).width front)
    else none
  | .tuck d =>
    if Obj.yarn ∈ r then
      some (splice r (yarnIdx r) 1 (tuckRow d), shiftW (offset r (yarnIdx r)) (tuckInterior d))
    else none
  | .merge a =>
    if Obj.yarn ∈ r then
      match stackAt r a, stackAt r (a + 1) with
      | some k, some l => some (splice r a 2 [.stack (k + l)], [])
      | _, _ => none
    else none
  | .knit d bd =>
    if Obj.yarn ∈ r ∧ hasBeside r d then
      match stackAt r (besideIdx r d) with
      | some k =>
        some (splice r (firstIdx r d) 2 (tuckRow d), shiftW (offset r (firstIdx r d)) (knitInterior d bd k))
      | none => none
    else none
  | .split d bd =>
    if Obj.yarn ∈ r ∧ hasBeside r d then
      match stackAt r (besideIdx r d) with
      | some k =>
        some (splice r (firstIdx r d) 2 (splitRow d bd k),
              shiftW (offset r (firstIdx r d)) (splitInteriorK d bd k))
      | none => none
    else none
  | .drop a =>
    if Obj.yarn ∈ r then
      match stackAt r a with
      | some k => some (r.eraseIdx a, List.replicate k (Gen.cap (offset r a)))
      | none => none
    else none

One instance of each step, drawn by runStep. Bottom: the row before the step. Top: the row after the step. Y: the yarn. Sk: a stack of k loops.

Y S1 S1 Y
x 0 true
Y S1 Y
tuck R
Y S1 S1 Y S2
merge 1
Y S1 S1 Y
knit R f
Y S2 S1 Y
knit R f
Y S1 S1 S1 Y
split R f
Y S1 Y
drop 1
def D : Row → List Step → Option (Row × MWord)
  | r, [] => some (r, [])
  | r, s :: ss =>
    match runStep r s with
    | none => none
    | some (r', u) =>
      match D r' ss with
      | none => none
      | some (r'', v) => some (r'', u ++ v)

Well-formedness of the shorthand split inside

The inside of a split through a stack of k loops is well formed.

Theorem Statement
splitInteriorK_one splitInteriorK d bd 1 = splitInterior d bd
nestK_wf, unnestK_wf nestK o k and unnestK o k keep w strands if o + 2k ≤ w
splitThroughK_wf splitThroughK k goes from 2k + 1 strands to 2k + 3 strands
splitInteriorK_wf splitInteriorK d bd k goes from 2k + 1 strands to 2k + 3 strands
theorem splitInteriorK_one (d : Dir) (bd : Bed) : splitInteriorK d bd 1 = splitInterior d bd
Proof
:= by
  cases d <;> cases bd <;> rfl

Well-formedness of splitInteriorK

theorem nestK_wf (o k w : Nat) (h : o + 2 * k ≤ w) : topWidth w (nestK o k) = some w
Proof
:= by
  unfold nestK
  refine topWidth_flatMap_keep _ _ ?_
  intro i hi
  rw [List.mem_range] at hi
  exact swapBlocks_wf true (by omega)
theorem topWidth_reverse_crosses {w : Nat} {u : MWord}
    (h : ∀ g ∈ u, ∃ k s, g = Gen.cross k s ∧ k + 1 < w) : topWidth w (reverseCrossings u.reverse) = some w
Proof
:= by
  apply topWidth_crosses
  intro g hg
  simp only [reverseCrossings, List.mem_map, List.mem_reverse] at hg
  obtain ⟨g', hg', rfl⟩ := hg
  obtain ⟨k, s, rfl, hk⟩ := h g' hg'
  exact ⟨k, !s, rfl, hk⟩
theorem nestK_crosses (o k w : Nat) (h : o + 2 * k ≤ w) :
    ∀ g ∈ nestK o k, ∃ j s, g = Gen.cross j s ∧ j + 1 < w
Proof
:= by
  intro g hg
  simp only [nestK, swapBlocks, List.mem_flatMap, List.mem_range, List.mem_map] at hg
  obtain ⟨i, hi, moved, hm, step1, hs, rfl⟩ := hg
  exact ⟨_, _, rfl, by omega⟩
theorem unnestK_wf (o k w : Nat) (h : o + 2 * k ≤ w) : topWidth w (unnestK o k) = some w
Proof
:=
  topWidth_reverse_crosses (nestK_crosses o k w h)
theorem splitThroughK_wf (k : Nat) : topWidth (2 * k + 1) (splitThroughK k) = some (2 * k + 3)
Proof
:= by
  unfold splitThroughK
  refine topWidth_append_of (topWidth_append_of (topWidth_append_of (topWidth_append_of
    (swapBlocks_wf false (by omega)) ?_) (swapBlocks_wf false (by omega))) (swapBlocks_wf false (by omega)))
    (swapBlocks_wf false (by omega))
  rw [topWidth_cons_fits (by simp [Gen.fits]; omega)]
  simp [topWidth, Gen.widthAfter]
theorem splitInteriorK_wf (d : Dir) (bd : Bed) (k : Nat) :
    topWidth (2 * k + 1) (splitInteriorK d bd k) = some (2 * k + 3)
Proof
:= by
  have hb : topWidth (2 * k + 1) (if bd = .b then reverseCrossings (splitThroughK k) else splitThroughK k) =
      some (2 * k + 3) := by
    split
    · rw [reverseCrossings_wf]; exact splitThroughK_wf k
    · exact splitThroughK_wf k
  have hd : topWidth (2 * k + 1) (if d = .L then mirrorAux (2 * k + 1)
      (if bd = .b then reverseCrossings (splitThroughK k) else splitThroughK k)
      else (if bd = .b then reverseCrossings (splitThroughK k) else splitThroughK k)) = some (2 * k + 3) := by
    split
    · exact mirror_wf hb
    · exact hb
  have hn : topWidth (2 * k + 1) (nestK (if d = .R then 1 else 0) k) = some (2 * k + 1) :=
    nestK_wf _ _ _ (by split <;> omega)
  have hu : topWidth (2 * k + 3) (unnestK (oldAt d bd) k) = some (2 * k + 3) :=
    unnestK_wf _ _ _ (by cases d <;> cases bd <;> simp [oldAt] <;> omega)
  unfold splitInteriorK
  refine topWidth_append_of (topWidth_append_of (topWidth_append_of hn hd) ?_) hu
  cases d <;> cases bd
  · rfl
  · exact swapBlocks_wf false (by omega)
  · exact swapBlocks_wf false (by omega)
  · rfl

Cells in the shorthand

The rows of a cell hold single loops. In the shorthand a single loop is a stack of one loop.

Declaration Content
embedObj, embedRow A cell object or row as a shorthand object or row. A loop becomes .stack 1
rowWidth_embed, mem_embed, length_embed embedRow keeps width, yarn membership and length
def embedObj : CellObj → Obj
  | .yarn => .yarn
  | .loop => .stack 1

def embedRow (r : CellRow) : Row := r.map embedObj

Rows

theorem embedObj_width (o : CellObj) : (embedObj o).width = o.width
Proof
:= by cases o <;> rfl
theorem rowWidth_embed (r : CellRow) : rowWidth (embedRow r) = cellRowWidth r
Proof
:= by
  induction r with
  | nil => rfl
  | cons o r ih =>
    simp only [embedRow, List.map_cons, rowWidth, cellRowWidth, List.sum_cons] at ih ⊢
    rw [ih, embedObj_width]
theorem mem_embed (r : CellRow) : Obj.yarn ∈ embedRow r ↔ CellObj.yarn ∈ r
Proof
:= by
  simp only [embedRow, List.mem_map]
  constructor
  · rintro ⟨o, ho, he⟩
    cases o
    · exact ho
    · simp [embedObj] at he
  · intro h
    exact ⟨_, h, rfl⟩
theorem length_embed (r : CellRow) : (embedRow r).length = r.length
Proof
:= List.length_map _

Stacks never part

Each shorthand step changes its row in one of six ways. No step cuts a stack in two.

Theorem Statement
stacks_never_part Each step is a StackChange: a splice of at most two objects. The objects outside are the same
loopCounts_never_part The same on the multiset of stack sizes
exchange_draws_blocks An exchange draws swapBlocks of the full widths of the two objects
merge_draws_nothing A merge draws nothing
drop_draws_caps A drop of a stack of k loops draws k caps at the offset of the stack

The six changes (StackChange):

Change Row before Row after
exchange a, b b, a
tuck the yarn tuckRow d
merge stack k, stack l stack (k + l)
knit the yarn and stack k tuckRow d: the yarn and a new stack of one loop
split the yarn and stack k splitRow d bd k: the stack stays, beside a new stack of one loop
drop stack k nothing
  • A stack in a row is only a count (Obj.stack k). So stacks_never_part holds almost by definition.
  • The tuck, knit and split draw tuckInterior, knitInterior, splitInteriorK at the offset of the yarn.
  • The knit and split take the whole stack as one block: nestK acts on all k loops.
  • No theorem states that no other step crosses two strands of one stack.
  • The machine statement is shared_order_kept.
def besideRow : Dir → Nat → Row
  | .R, k => [.yarn, .stack k]
  | .L, k => [.stack k, .yarn]

inductive StackChange : Row → Row → Prop
  | exchange (pre post : Row) (a b : Obj) : StackChange (pre ++ a :: b :: post) (pre ++ b :: a :: post)
  | tuck (pre post : Row) (d : Dir) : StackChange (pre ++ .yarn :: post) (pre ++ tuckRow d ++ post)
  | merge (pre post : Row) (k l : Nat) (hk : 0 < k) (hl : 0 < l) :
      StackChange (pre ++ .stack k :: .stack l :: post) (pre ++ .stack (k + l) :: post)
  | knit (pre post : Row) (d : Dir) (k : Nat) (hk : 0 < k) :
      StackChange (pre ++ besideRow d k ++ post) (pre ++ tuckRow d ++ post)
  | split (pre post : Row) (d : Dir) (bd : Bed) (k : Nat) (hk : 0 < k) :
      StackChange (pre ++ besideRow d k ++ post) (pre ++ splitRow d bd k ++ post)
  | drop (pre post : Row) (k : Nat) (hk : 0 < k) : StackChange (pre ++ .stack k :: post) (pre ++ post)
theorem stackAt_some {r : Row} {a k : Nat} (h : stackAt r a = some k) : r[a]? = some (.stack k) ∧ 0 < k
Proof
:= by
  unfold stackAt at h
  split at h
  · rename_i k' hk'
    split at h
    · simp only [Option.some.injEq] at h
      subst h
      exact ⟨hk', ‹_›⟩
    · exact absurd h (by simp)
  · exact absurd h (by simp)
theorem cut1 {r : Row} {a : Nat} {o : Obj} (h : r[a]? = some o) : r = r.take a ++ o :: r.drop (a + 1)
Proof
:= by
  obtain ⟨ha, hg⟩ := List.getElem?_eq_some_iff.mp h
  conv_lhs => rw [← List.take_append_drop a r]
  rw [List.drop_eq_getElem_cons ha, hg]
theorem cut2 {r : Row} {a : Nat} {o₁ o₂ : Obj} (h₁ : r[a]? = some o₁) (h₂ : r[a + 1]? = some o₂) :
    r = r.take a ++ o₁ :: o₂ :: r.drop (a + 2)
Proof
:= by
  obtain ⟨ha, hg⟩ := List.getElem?_eq_some_iff.mp h₂
  have h := cut1 h₁
  rw [List.drop_eq_getElem_cons ha, hg] at h
  exact h
theorem yarn_at {r : Row} (hy : Obj.yarn ∈ r) : r[yarnIdx r]? = some .yarn
Proof
:= by
  have hl : yarnIdx r < r.length := List.idxOf_lt_length_of_mem hy
  rw [List.getElem?_eq_getElem hl]
  exact congrArg some (List.getElem_idxOf hl)
theorem stacks_never_part {r r' : Row} {s : Step} {u : MWord} (h : runStep r s = some (r', u)) :
    StackChange r r'
Proof
:= by
  cases s with
  | x a front =>
    simp only [runStep] at h
    split at h
    · rename_i hc
      simp only [Option.some.injEq, Prod.mk.injEq] at h
      obtain ⟨rfl, -⟩ := h
      have hr := cut2 (r := r) (a := a) (o₁ := r.getD a .yarn) (o₂ := r.getD (a + 1) .yarn)
        (by rw [List.getD_eq_getElem?_getD, List.getElem?_eq_getElem (by omega)]; rfl)
        (by rw [List.getD_eq_getElem?_getD, List.getElem?_eq_getElem (by omega)]; rfl)
      unfold splice
      conv => enter [1]; rw [hr]
      simpa using StackChange.exchange (r.take a) (r.drop (a + 2)) (r.getD a .yarn) (r.getD (a + 1) .yarn)
    · exact absurd h (by simp)
  | tuck d =>
    simp only [runStep] at h
    split at h
    · rename_i hy
      simp only [Option.some.injEq, Prod.mk.injEq] at h
      obtain ⟨rfl, -⟩ := h
      have hr := cut1 (yarn_at hy)
      unfold splice
      conv => enter [1]; rw [hr]
      simpa using StackChange.tuck (r.take (yarnIdx r)) (r.drop (yarnIdx r + 1)) d
    · exact absurd h (by simp)
  | merge a =>
    simp only [runStep] at h
    split at h
    · split at h
      · rename_i k l hk hl
        simp only [Option.some.injEq, Prod.mk.injEq] at h
        obtain ⟨rfl, -⟩ := h
        obtain ⟨hk1, hk2⟩ := stackAt_some hk
        obtain ⟨hl1, hl2⟩ := stackAt_some hl
        have hr := cut2 hk1 hl1
        unfold splice
        conv => enter [1]; rw [hr]
        simpa using StackChange.merge (r.take a) (r.drop (a + 2)) k l hk2 hl2
      · exact absurd h (by simp)
    · exact absurd h (by simp)
  | knit d bd =>
    simp only [runStep] at h
    split at h
    · rename_i hc
      split at h
      · rename_i k hk
        simp only [Option.some.injEq, Prod.mk.injEq] at h
        obtain ⟨rfl, -⟩ := h
        obtain ⟨hk1, hk2⟩ := stackAt_some hk
        have hy := yarn_at hc.1
        cases d with
        | R =>
          simp only [firstIdx, besideIdx] at hk1 ⊢
          have hr := cut2 hy hk1
          unfold splice
          conv => enter [1]; rw [hr]
          simpa [besideRow] using StackChange.knit (r.take (yarnIdx r)) (r.drop (yarnIdx r + 2)) .R k hk2
        | L =>
          simp only [firstIdx, besideIdx] at hk1 ⊢
          have h1 : 1 ≤ yarnIdx r := by simpa [hasBeside] using hc.2
          have hy' : r[yarnIdx r - 1 + 1]? = some .yarn := by rw [Nat.sub_add_cancel h1]; exact hy
          have hr := cut2 hk1 hy'
          unfold splice
          conv => enter [1]; rw [hr]
          simpa [besideRow] using
            StackChange.knit (r.take (yarnIdx r - 1)) (r.drop (yarnIdx r - 1 + 2)) .L k hk2
      · exact absurd h (by simp)
    · exact absurd h (by simp)
  | split d bd =>
    simp only [runStep] at h
    split at h
    · rename_i hc
      split at h
      · rename_i k hk
        simp only [Option.some.injEq, Prod.mk.injEq] at h
        obtain ⟨rfl, -⟩ := h
        obtain ⟨hk1, hk2⟩ := stackAt_some hk
        have hy := yarn_at hc.1
        cases d with
        | R =>
          simp only [firstIdx, besideIdx] at hk1 ⊢
          have hr := cut2 hy hk1
          unfold splice
          conv => enter [1]; rw [hr]
          simpa [besideRow] using
            StackChange.split (r.take (yarnIdx r)) (r.drop (yarnIdx r + 2)) .R bd k hk2
        | L =>
          simp only [firstIdx, besideIdx] at hk1 ⊢
          have h1 : 1 ≤ yarnIdx r := by simpa [hasBeside] using hc.2
          have hy' : r[yarnIdx r - 1 + 1]? = some .yarn := by rw [Nat.sub_add_cancel h1]; exact hy
          have hr := cut2 hk1 hy'
          unfold splice
          conv => enter [1]; rw [hr]
          simpa [besideRow] using
            StackChange.split (r.take (yarnIdx r - 1)) (r.drop (yarnIdx r - 1 + 2)) .L bd k hk2
      · exact absurd h (by simp)
    · exact absurd h (by simp)
  | drop a =>
    simp only [runStep] at h
    split at h
    · split at h
      · rename_i k hk
        simp only [Option.some.injEq, Prod.mk.injEq] at h
        obtain ⟨rfl, -⟩ := h
        obtain ⟨hk1, hk2⟩ := stackAt_some hk
        have hr := cut1 hk1
        have ha : a < r.length := (List.getElem?_eq_some_iff.mp hk1).1
        rw [List.eraseIdx_eq_take_drop_succ]
        conv => enter [1]; rw [hr]
        simpa using StackChange.drop (r.take a) (r.drop (a + 1)) k hk2
      · exact absurd h (by simp)
    · exact absurd h (by simp)

The same, read on the multiset of stack sizes

def sizes (r : Row) : List Nat := r.filterMap fun | .yarn => none | .stack k => some k
theorem sizes_append (r s : Row) : sizes (r ++ s) = sizes r ++ sizes s
Proof
:= by simp [sizes, List.filterMap_append]
theorem coe_middle (A B : List Nat) (x : Nat) :
    ((A ++ x :: B : List Nat) : Multiset Nat) = x ::ₘ ((A ++ B : List Nat) : Multiset Nat)
Proof
:= by
  rw [Multiset.cons_coe, Multiset.coe_eq_coe]
  exact List.perm_middle
theorem loopCounts_never_part {r r' : Row} {s : Step} {u : MWord} (h : runStep r s = some (r', u)) :
    (sizes r' : Multiset Nat) = sizes r ∨
    (sizes r' : Multiset Nat) = 1 ::ₘ (sizes r : Multiset Nat) ∨
    (∃ k l rest, (sizes r : Multiset Nat) = k ::ₘ l ::ₘ rest ∧ (sizes r' : Multiset Nat) = (k + l) ::ₘ rest) ∨
    (∃ k rest, (sizes r : Multiset Nat) = k ::ₘ rest ∧ (sizes r' : Multiset Nat) = 1 ::ₘ rest) ∨
    (∃ k rest, (sizes r : Multiset Nat) = k ::ₘ rest ∧ (sizes r' : Multiset Nat) = 1 ::ₘ k ::ₘ rest) ∨
    (∃ k rest, (sizes r : Multiset Nat) = k ::ₘ rest ∧ (sizes r' : Multiset Nat) = rest)
Proof
:= by
  have hc := stacks_never_part h
  cases hc with
  | exchange pre post a b =>
    left
    rw [sizes_append, sizes_append, Multiset.coe_eq_coe]
    apply List.Perm.append_left
    cases a <;> cases b <;> simp only [sizes, List.filterMap_cons, List.Perm.refl]; exact List.Perm.swap _ _ _
  | tuck pre post d =>
    right; left
    cases d <;> simp [sizes, tuckRow, coe_middle]
  | merge pre post k l hk hl =>
    right; right; left
    refine ⟨k, l, ↑(sizes pre ++ sizes post), ?_, ?_⟩ <;>
      simp [sizes, coe_middle]
  | knit pre post d k hk =>
    right; right; right; left
    refine ⟨k, ↑(sizes pre ++ sizes post), ?_, ?_⟩ <;>
      cases d <;> simp [sizes, besideRow, tuckRow, coe_middle]
  | split pre post d bd k hk =>
    right; right; right; right; left
    refine ⟨k, ↑(sizes pre ++ sizes post), ?_, ?_⟩ <;>
      cases d <;> cases bd <;> simp [sizes, besideRow, splitRow, coe_middle] <;> exact List.Perm.swap _ _ _
  | drop pre post k hk =>
    right; right; right; right; right
    refine ⟨k, ↑(sizes pre ++ sizes post), ?_, ?_⟩ <;> simp [sizes, coe_middle]

What the steps draw

theorem exchange_draws_blocks {r r' : Row} {a : Nat} {front : Bool} {u : MWord}
    (h : runStep r (.x a front) = some (r', u)) :
    ∃ pre post o₁ o₂, r = pre ++ o₁ :: o₂ :: post ∧ r' = pre ++ o₂ :: o₁ :: post ∧
      u = swapBlocks (rowWidth pre) o₁.width o₂.width front
Proof
:= by
  simp only [runStep] at h
  split at h
  · rename_i hc
    simp only [Option.some.injEq, Prod.mk.injEq] at h
    obtain ⟨rfl, rfl⟩ := h
    have hr := cut2 (r := r) (a := a) (o₁ := r.getD a .yarn) (o₂ := r.getD (a + 1) .yarn)
      (by rw [List.getD_eq_getElem?_getD, List.getElem?_eq_getElem (by omega)]; rfl)
      (by rw [List.getD_eq_getElem?_getD, List.getElem?_eq_getElem (by omega)]; rfl)
    refine ⟨r.take a, r.drop (a + 2), r.getD a .yarn, r.getD (a + 1) .yarn, hr, ?_, rfl⟩
    simp [splice]
  · exact absurd h (by simp)
theorem merge_draws_nothing {r r' : Row} {a : Nat} {u : MWord} (h : runStep r (.merge a) = some (r', u)) :
    u = []
Proof
:= by
  simp only [runStep] at h
  split at h
  · split at h
    · simp only [Option.some.injEq, Prod.mk.injEq] at h
      exact h.2.symm
    · exact absurd h (by simp)
  · exact absurd h (by simp)
theorem drop_draws_caps {r r' : Row} {a : Nat} {u : MWord} (h : runStep r (.drop a) = some (r', u)) :
    ∃ pre post k, r = pre ++ .stack k :: post ∧ r' = pre ++ post ∧
      u = List.replicate k (Gen.cap (rowWidth pre))
Proof
:= by
  simp only [runStep] at h
  split at h
  · split at h
    · rename_i k hk
      simp only [Option.some.injEq, Prod.mk.injEq] at h
      obtain ⟨rfl, rfl⟩ := h
      obtain ⟨hk1, -⟩ := stackAt_some hk
      have hr := cut1 hk1
      have hlen : a < r.length := (List.getElem?_eq_some_iff.mp hk1).1
      refine ⟨r.take a, r.drop (a + 1), k, hr, ?_, rfl⟩
      rw [List.eraseIdx_eq_take_drop_succ]
    · exact absurd h (by simp)
  · exact absurd h (by simp)

The category of shorthand programs, 𝒮

Part Definition
Objects ShorthandObj: a row with exactly one yarn and no empty stack
Arrows r ⟶ r' ShorthandProgram r r': a list of steps that goes from r to r'
Composition List concatenation: one program, then the other (D_append)
Identity on r The empty program
  • Two arrows are equal only when they are the same list of steps. There is no quotient.
  • A step never makes a second yarn or an empty stack. A program from an object ends on an object.
open CategoryTheory
theorem D_append (r : Row) (w₁ w₂ : List Step) :
    D r (w₁ ++ w₂) = (D r w₁).bind fun p => (D p.1 w₂).map fun q => (q.1, p.2 ++ q.2)
Proof
:= by
  induction w₁ generalizing r with
  | nil => simp only [List.nil_append, D, Option.bind_some, List.nil_append]; cases D r w₂ <;> rfl
  | cons s ss ih =>
    simp only [List.cons_append, D]
    cases runStep r s with
    | none => rfl
    | some p =>
      simp only
      rw [ih]
      cases D p.1 ss with
      | none => rfl
      | some q =>
        simp only [Option.bind_some]
        cases D q.1 w₂ <;> simp
structure ShorthandObj where
  row : Row
  yarn : row.count .yarn = 1
  ne : NoEmpty row
theorem ShorthandObj.ext' {a b : ShorthandObj} (h : a.row = b.row) : a = b
Proof
:= by
  cases a; cases b; simp only at h; subst h; rfl
def ShorthandProgram (a b : ShorthandObj) : Type := {w : List Step // ∃ M, D a.row w = some (b.row, M)}

def ShorthandProgram.comp {a b c : ShorthandObj} (p : ShorthandProgram a b) (q : ShorthandProgram b c) :
    ShorthandProgram a c :=
  ⟨p.1 ++ q.1, by
    obtain ⟨M, hM⟩ := p.2
    obtain ⟨N, hN⟩ := q.2
    exact ⟨M ++ N, by rw [D_append, hM]; simp [hN]⟩⟩

instance : SmallCategory ShorthandObj where
  Hom := ShorthandProgram
  id _ := ⟨[], [], rfl⟩
  comp p q := ShorthandProgram.comp p q
  id_comp _ := Subtype.ext (List.nil_append _)
  comp_id _ := Subtype.ext (List.append_nil _)
  assoc _ _ _ := Subtype.ext (List.append_assoc _ _ _)
theorem ShorthandObj.hom_val_comp {a b c : ShorthandObj} (p : a ⟶ b) (q : b ⟶ c) :
    (show ShorthandProgram a c from p ≫ q).1 =
      (show ShorthandProgram a b from p).1 ++ (show ShorthandProgram b c from q).1
Proof
:= rfl
theorem ShorthandObj.eqToHom_val {a b : ShorthandObj} (h : a = b) :
    (show ShorthandProgram a b from eqToHom h).1 = []
Proof
:= by
  subst h; rfl

References

  1. Lin, Narayanan, Ikarashi, Ragan-Kelley, Bernstein, McCann, "Semantics and Scheduling for Machine Knitting Compilers", ACM TOG 42(4), 2023. Fig. 12 (stitch insides).