Research | Knitting | Category ℋ: Hand-knitting program

Category ℋ: Hand-knitting program

A hand-knitting program is a list of needle actions. A state has loops on the left needle, loops on the right needle and the yarn end between them. Each action draws a piece of tangle. A stitch is a program: knit is insert, wrap, take out, let off. Hand-knitting programs form a category. An arrow is a program. Composition runs one program after the other.

Hand knitting: the needles, the actions, and what each action draws

  • A state (State) has loops on the left needle, loops on the right needle and the yarn end between the needle tips. The yarn end is in front of the work or at the back. A state can also have loops on a cable needle, and a bar.
  • The top edge of the drawing, from the left: the loops of the left needle, the loops of the cable needle, the bar, the yarn end, the loops of the right needle. Each loop and the bar are two strands. The yarn end is one strand.
  • A needle is not a part of the drawing. A needle only fixes the order of its loops: a loop goes on at a tip and comes off at a tip.
  • The cable needle holds loops in front of the work or at the back (hold). It has two tips. The right needle works the loops at its right end. The left needle takes back the loops at its left end.
  • The bar is the strand between two stitches of the row below. It is a loop on no needle.
Action Hand movement Drawing
yarnBetween The yarn goes to the other side between the needles Nothing
yarnOver s The yarn goes to the other side over the right needle A cup: one new loop on the right needle
insert n m w count round Needle n goes into the count tip loops of needle m Nothing
wrap s The yarn goes one time round the needle that is in the loops A cup: the yarn end and one leg of a new loop
takeOut The needle comes out of the loops, with its wrap The two legs of the new loop go through the loops
letOff n count Needle n lets its count tip loops go The loops go to the needle that is in them, or one cap for each loop
passOver n count count loops of needle n go over its tip loop and off the needle The loop goes round the tip loop; one cap
hold side The cable needle goes in front of the work or to the back Nothing
pickUp side The left needle takes the bar, from the front or from the back Nothing, or a half turn of the bar

Rules of the drawing:

  1. A strand that passes a leg in front goes over the leg. A strand that passes a leg behind goes under the leg. A strand that goes through a loop makes no crossing.
  2. A needle that goes through a loop to the right passes in front of the left leg and behind the right leg. To the left: behind the right leg and in front of the left leg.
  3. along: the needle points the same way as the needle that holds the loops. It goes into the loop that is farthest from the tip first. round tells if it comes to that loop in front of the loops or behind them. against: the needle points the other way. It goes into the tip loop first.
  4. The yarn goes to the tip of the needle on its side of the work. A wrap is a cup at the yarn.
  5. The two legs of the new loop go back along the way of the needle. Then the yarn end goes back to the gap.
  6. A strand that passes the yarn end goes over it when the yarn is at the back, and under it when the yarn is in front.
  7. A strand that passes the loops of the cable needle goes over them when the cable needle is at the back, and under them when it is in front.
  8. A loop that goes to the other needle with the needle along makes a half turn: its two legs cross. Loops that go together turn as one band.
  9. A loop that comes off its needle, with no needle in it, closes: a cap.
  10. A loop that goes over the tip loop: its back leg passes behind the tip loop, its front leg passes in front.
  11. The left needle does what the right needle does, seen from the other side of the work (State.flip, rot). The left needle uses this rule for wrap and takeOut.
  • step: one action. It gives the new state and the drawing of the action.
  • draw: a program. Its drawing is the drawings of its actions, one on top of the other.
  • drawing: the drawing only.
namespace Hand
inductive Needle where
  | left
  | right
  | cable
  deriving DecidableEq, Repr

inductive Side where
  | front
  | back
  deriving DecidableEq, Repr

inductive Way where
  | along
  | against
  deriving DecidableEq, Repr

inductive Sense where
  | usual
  | other
  deriving DecidableEq, Repr

def Side.flip : Side → Side
  | .front => .back
  | .back => .front

def Needle.flip : Needle → Needle
  | .left => .right
  | .right => .left
  | .cable => .cable

structure Inside where
  needle : Needle
  into : Needle
  way : Way
  count : Nat
  round : Side
  wrapped : Bool
  deriving DecidableEq, Repr

structure State where
  left : Nat
  right : Nat
  yarn : Side
  inside : Option Inside := none
  cable : Nat := 0
  hold : Side := .back
  bar : Bool := false
  deriving DecidableEq, Repr

inductive Action where
  | yarnBetween
  | yarnOver (s : Sense)
  | insert (n m : Needle) (w : Way) (count : Nat) (round : Side)
  | wrap (s : Sense)
  | takeOut
  | letOff (n : Needle) (count : Nat)
  | passOver (n : Needle) (count : Nat)
  | hold (side : Side)
  | pickUp (side : Side)
  deriving DecidableEq, Repr

abbrev Program := List Action

Pieces of drawing

def strandRight (i k : Nat) (over : Bool) : MWord :=
  (List.range k).map fun j => .cross (i + j) over

def strandLeft (i k : Nat) (over : Bool) : MWord :=
  (List.range k).map fun j => .cross (i - 1 - j) (!over)

def pairRight (i : Nat) (overs : List Bool) : MWord :=
  (overs.zipIdx.map fun (over, j) => [Gen.cross (i + 1 + j) over, Gen.cross (i + j) over]).flatten

def pairLeft (i : Nat) (overs : List Bool) : MWord :=
  (overs.zipIdx.map fun (over, j) => [Gen.cross (i - 1 - j) (!over), Gen.cross (i - j) (!over)]).flatten

def alternate (b : Bool) : Nat → List Bool
  | 0 => []
  | k + 1 => b :: alternate (!b) k

def halfTurn (i : Nat) (lo : Bool) : Nat → MWord
  | 0 => []
  | n + 1 => (List.range n).map (fun k => Gen.cross (i + k) lo) ++ halfTurn i lo n

def rotGen (w : Nat) : Gen → Gen
  | .cross i lo => .cross (w - 2 - i) lo
  | .cup i => .cup (w - i)
  | .cap i => .cap (w - 2 - i)

def rot : Nat → MWord → MWord
  | _, [] => []
  | w, g :: gs => rotGen w g :: rot (g.widthAfter w) gs

Positions

def State.yarnAt (s : State) : Nat := 2 * (s.left + s.cable) + (if s.bar then 2 else 0)

def State.wrapping (s : State) : Bool :=
  match s.inside with
  | some i => i.wrapped
  | none => false

def State.width (s : State) : Nat := s.yarnAt + 1 + 2 * s.right + (if s.wrapping then 2 else 0)

def State.isBack (s : State) : Bool := decide (s.yarn = Side.back)

def State.heldOver (s : State) : Bool := decide (s.hold = Side.back)

def State.between (s : State) (m : Needle) : Nat :=
  match m with
  | .left => 2 * s.cable
  | _ => 0

def Inside.flip (i : Inside) : Inside :=
  { i with needle := i.needle.flip, into := i.into.flip, round := i.round.flip }

def State.flip (s : State) : State :=
  { s with left := s.right, right := s.left, yarn := s.yarn.flip, inside := s.inside.map Inside.flip,
           hold := s.hold.flip }

The right needle in loops

def wrapWord (s : State) (m : Needle) (w : Way) (c : Nat) (sense : Sense) : MWord :=
  let y := s.yarnAt
  let h := s.between m
  let cupAt := match w with
    | .along => y - h
    | .against => y - h - 2 * c
  let travel := match w with
    | .along => strandLeft y h s.heldOver
    | .against => strandLeft y h s.heldOver ++ strandLeft (y - h) (2 * c) (!s.isBack)
  let turn := match sense with
    | .usual => []
    | .other => [Gen.cross (cupAt + 1) true]
  travel ++ [.cup cupAt] ++ turn

def takeOutWord (s : State) (m : Needle) (w : Way) (c : Nat) (round : Side) : MWord :=
  let h := s.between m
  let y := s.yarnAt - h
  match w with
  | .against =>
    pairRight (y - 2 * c + 1) (alternate true (2 * c)) ++ pairRight (y + 1) (List.replicate h s.heldOver) ++
      strandRight (y - 2 * c) (2 * c) (!s.isBack) ++ strandRight y h s.heldOver
  | .along =>
    pairLeft (y + 1) [s.isBack] ++ pairLeft y (alternate false (2 * c)) ++
      pairRight (y - 2 * c) (List.replicate (2 * c) (decide (round = Side.front))) ++ pairRight y [s.isBack] ++
      pairRight (y + 1) (List.replicate h s.heldOver) ++ strandRight y h s.heldOver

def wrapRight (s : State) (i : Inside) (sense : Sense) : Option (State × MWord) :=
  if i.needle = .right ∧ i.wrapped = false ∧ (i.into = .left ∨ i.into = .cable) ∧ s.bar = false then
    some ({ s with inside := some { i with wrapped := true } }, wrapWord s i.into i.way i.count sense)
  else none

def takeOutRight (s : State) (i : Inside) : Option (State × MWord) :=
  if i.needle = .right ∧ i.wrapped = true then
    some ({ s with right := s.right + 1, inside := none }, takeOutWord s i.into i.way i.count i.round)
  else none

def asLeft (s : State) (f : State → Option (State × MWord)) : Option (State × MWord) :=
  if s.cable = 0 ∧ s.bar = false then (f s.flip).map fun p => (p.1.flip, rot s.width p.2) else none

Letting go and passing over

def passRight (y : Nat) : MWord :=
  [.cross (y + 2) true, .cross (y + 1) true, .cross (y + 3) false, .cross (y + 2) false, .cap (y + 1)]

def passLeft (y : Nat) : MWord :=
  [.cross (y - 3) true, .cross (y - 2) true, .cross (y - 4) false, .cross (y - 3) false, .cap (y - 2)]

def passLeftAll (y : Nat) : Nat → MWord
  | 0 => []
  | k + 1 => passLeft y ++ passLeftAll (y - 2) k

def closeLeft (y : Nat) : Nat → MWord
  | 0 => []
  | k + 1 => Gen.cap (y - 2) :: closeLeft (y - 2) k

def transfer (s : State) (i : Inside) (n : Needle) (count : Nat) : Option (State × MWord) :=
  if count ≠ i.count ∨ i.into ≠ n then none else
  let c := i.count
  let plain := decide (s.cable = 0 ∧ s.bar = false)
  match i.needle, n with
  | .right, .left =>
    if plain then
      let turn := match i.way with
        | .along => halfTurn (s.yarnAt - 2 * c + 1) (decide (i.round = Side.front)) (2 * c)
        | .against => []
      some ({ s with left := s.left - c, right := s.right + c, inside := none },
        strandLeft s.yarnAt (2 * c) (!s.isBack) ++ turn)
    else none
  | .left, .right =>
    if plain then
      let turn := match i.way with
        | .along => halfTurn s.yarnAt (decide (i.round = Side.back)) (2 * c)
        | .against => []
      some ({ s with left := s.left + c, right := s.right - c, inside := none },
        strandRight s.yarnAt (2 * c) (!s.isBack) ++ turn)
    else none
  | .cable, .left => some ({ s with left := s.left - c, cable := s.cable + c, inside := none }, [])
  | .left, .cable => some ({ s with left := s.left + c, cable := s.cable - c, inside := none }, [])
  | _, _ => none

One action

def room (s : State) : Needle → Nat
  | .left => s.left
  | .right => s.right
  | .cable => s.cable

def canInsert : Needle → Needle → Bool
  | .right, .left => true
  | .left, .right => true
  | .right, .cable => true
  | .cable, .left => true
  | .left, .cable => true
  | _, _ => false
step: 66 lines of Lean
def step (s : State) : Action → Option (State × MWord)
  | .yarnBetween =>
    match s.inside with
    | some i => if i.wrapped then none else some ({ s with yarn := s.yarn.flip }, [])
    | none => some ({ s with yarn := s.yarn.flip }, [])
  | .yarnOver sense =>
    match s.inside, sense, s.yarn with
    | none, .usual, .front => some ({ s with right := s.right + 1, yarn := .back }, [.cup s.yarnAt])
    | none, .other, .back =>
      some ({ s with right := s.right + 1, yarn := .front }, [.cup s.yarnAt, .cross (s.yarnAt + 1) true])
    | _, _, _ => none
  | .insert n m w count round =>
    if s.inside = none ∧ canInsert n m ∧ 0 < count ∧ count ≤ room s m then
      some ({ s with inside := some ⟨n, m, w, count, round, false⟩ }, [])
    else none
  | .wrap sense =>
    match s.inside with
    | some i =>
      match i.needle with
      | .right => wrapRight s i sense
      | .left => asLeft s fun t => t.inside.bind fun j => wrapRight t j sense
      | .cable => none
    | none => none
  | .takeOut =>
    match s.inside with
    | some i =>
      if i.wrapped then
        match i.needle with
        | .right => takeOutRight s i
        | .left => asLeft s fun t => t.inside.bind fun j => takeOutRight t j
        | .cable => none
      else some ({ s with inside := none }, [])
    | none => none
  | .letOff n count =>
    match s.inside with
    | none =>
      match n with
      | .left => if count ≤ s.left then some ({ s with left := s.left - count }, closeLeft (2 * s.left) count) else none
      | .right =>
        if count ≤ s.right then
          some ({ s with right := s.right - count }, List.replicate count (.cap (s.yarnAt + 1)))
        else none
      | .cable =>
        if count ≤ s.cable ∧ s.bar = false then
          some ({ s with cable := s.cable - count }, closeLeft (2 * (s.left + s.cable)) count)
        else none
    | some i => if i.wrapped then none else transfer s i n count
  | .passOver n count =>
    if s.inside = none ∧ s.cable = 0 ∧ s.bar = false then
      match n with
      | .right =>
        if count + 1 ≤ s.right then
          some ({ s with right := s.right - count }, (List.replicate count (passRight s.yarnAt)).flatten)
        else none
      | .left =>
        if count + 1 ≤ s.left then some ({ s with left := s.left - count }, passLeftAll s.yarnAt count) else none
      | .cable => none
    else none
  | .hold side => if s.inside = none then some ({ s with hold := side }, []) else none
  | .pickUp side =>
    if s.inside = none ∧ s.bar = true ∧ s.cable = 0 then
      let turn := match side with
        | .front => []
        | .back => [Gen.cross (2 * s.left) true]
      some ({ s with left := s.left + 1, bar := false }, turn)
    else none

Programs

def draw : State → Program → Option (State × MWord)
  | s, [] => some (s, [])
  | s, a :: as =>
    match step s a with
    | none => none
    | some (s', u) =>
      match draw s' as with
      | none => none
      | some (s'', v) => some (s'', u ++ v)

A knit, one action at a time, drawn by step. The needle goes into the loop first (insert .right .left .along 1 .front), which draws nothing. Bottom: the top edge before the action. Top: the top edge after it. L: a loop on the left needle. Y: the yarn end. W: the wrap round the right needle. R: a loop on the right needle.

L Y L Y W
wrap .usual
L Y W L Y R
takeOut
L Y R Y R
letOff .left 1
L Y Y R
knit: the whole program
def drawing (s : State) (p : Program) : MWord :=
  match draw s p with
  | some (_, u) => u
  | none => []

def start (left : Nat) (yarn : Side) : State := { left := left, right := 0, yarn := yarn }
end Hand

Hand knitting: eight stitches as programs

Stitch Start: loops on the left needle, yarn Program
knit 1, back work along; let off
purl 1, front work against; let off
k2tog 2, back work along through 2 loops; let off 2
p2tog 2, front work against through 2 loops; let off 2
ssk 2, back slip 2 loops knitwise, one at a time; put them back; work against through 2 loops; let off 2
ssp 2, front slip 2 loops knitwise, one at a time; put them back; work along through 2 loops, round the back; let off 2
sk2p 3, back slip 1 loop knitwise; k2tog; pass the slipped loop over
dsd 3, back ssk; put the new loop back; pass the next loop of the left needle over it; slip the new loop purlwise
  • work w count round: the right needle goes into count loops, the yarn goes round it, and it comes out with its wrap. This makes one new loop on the right needle. The old loops stay on the left needle.
  • slipKnitwise: the loop goes to the right needle and makes a half turn. slipPurlwise: it goes with no turn.
  • putBack: the left needle takes the tip loop of the right needle back, tip to tip.
  • Runs s p e: program p runs from state s to state e, and its drawing fits the two top edges.
namespace Hand
def work (w : Way) (count : Nat := 1) (round : Side := .front) : Program :=
  [.insert .right .left w count round, .wrap .usual, .takeOut]

def slipKnitwise : Program := [.insert .right .left .along 1 .front, .letOff .left 1]

def slipPurlwise : Program := [.insert .right .left .against 1 .front, .letOff .left 1]

def putBack : Program := [.insert .left .right .against 1 .front, .letOff .right 1]

def knit : Program := work .along ++ [.letOff .left 1]

def purl : Program := work .against ++ [.letOff .left 1]

def k2tog : Program := work .along 2 ++ [.letOff .left 2]

def p2tog : Program := work .against 2 ++ [.letOff .left 2]

def ssk : Program :=
  slipKnitwise ++ slipKnitwise ++ putBack ++ putBack ++ work .against 2 ++ [.letOff .left 2]

def ssp : Program :=
  slipKnitwise ++ slipKnitwise ++ putBack ++ putBack ++ work .along 2 .back ++ [.letOff .left 2]

def sk2p : Program := slipKnitwise ++ k2tog ++ [.passOver .right 1]

def dsd : Program := ssk ++ putBack ++ [.passOver .left 1] ++ slipPurlwise

The drawings of the eight programs, drawn by draw from their start states. Bottom: the loops on the left needle and the yarn end. Top: the yarn end and the new loop on the right needle.

L Y Y R
knit
L Y Y R
purl
L L Y Y R
k2tog
L L Y Y R
p2tog
L L Y Y R
ssk
L L Y Y R
ssp
L L L Y Y R
sk2p
L L L Y Y R
dsd
def Runs (s : State) (p : Program) (e : State) : Prop :=
  match draw s p with
  | some (e', u) => e' = e ∧ topWidth s.width u = some e.width
  | none => False

instance (s : State) (p : Program) (e : State) : Decidable (Runs s p e) := by
  unfold Runs
  split <;> infer_instance

def oneLoop (yarn : Side) : State := { left := 0, right := 1, yarn := yarn }
theorem knit_runs : Runs (start 1 .back) knit (oneLoop .back)
Proof
:= by decide
theorem purl_runs : Runs (start 1 .front) purl (oneLoop .front)
Proof
:= by decide
theorem k2tog_runs : Runs (start 2 .back) k2tog (oneLoop .back)
Proof
:= by decide
theorem p2tog_runs : Runs (start 2 .front) p2tog (oneLoop .front)
Proof
:= by decide
theorem ssk_runs : Runs (start 2 .back) ssk (oneLoop .back)
Proof
:= by decide
theorem ssp_runs : Runs (start 2 .front) ssp (oneLoop .front)
Proof
:= by decide
theorem sk2p_runs : Runs (start 3 .back) sk2p (oneLoop .back)
Proof
:= by decide
theorem dsd_runs : Runs (start 3 .back) dsd (oneLoop .back)
Proof
:= by decide

The category of hand-knitting programs, ℋ

Part Definition
Objects HandObj: a state of the needles
Arrows a ⟶ b HandProgram a b: a list of actions that goes from a to b. Its drawing goes from the top edge of a to the top edge of b
Composition List concatenation: one program, then the other (draw_append)
Identity on a The empty program
  • Two arrows are equal only when they are the same list of actions. There is no quotient.
  • An arrow holds the proof that its drawing fits the two top edges (WF). This package has no general theorem that each action gives a drawing that fits.
namespace Hand
open CategoryTheory
theorem draw_append (s : State) (p q : Program) :
    draw s (p ++ q) = (draw s p).bind fun x => (draw x.1 q).map fun y => (y.1, x.2 ++ y.2)
Proof
:= by
  induction p generalizing s with
  | nil => simp only [List.nil_append, draw, Option.bind_some, List.nil_append]; cases draw s q <;> rfl
  | cons a as ih =>
    simp only [List.cons_append, draw]
    cases step s a with
    | none => rfl
    | some x =>
      simp only
      rw [ih]
      cases draw x.1 as with
      | none => rfl
      | some y =>
        simp only [Option.bind_some]
        cases draw y.1 q <;> simp
theorem draw_app {s t r : State} {p q : Program} {u v : MWord} (hp : draw s p = some (t, u))
    (hq : draw t q = some (r, v)) : draw s (p ++ q) = some (r, u ++ v)
Proof
:= by
  rw [draw_append, hp]; simp [hq]
structure HandObj where
  state : State

def HandProgram (a b : HandObj) : Type :=
  {p : Program // ∃ u, draw a.state p = some (b.state, u) ∧ WF a.state.width b.state.width u}

def HandProgram.comp {a b c : HandObj} (p : HandProgram a b) (q : HandProgram b c) : HandProgram a c :=
  ⟨p.1 ++ q.1, by
    obtain ⟨u, hu, wu⟩ := p.2
    obtain ⟨v, hv, wv⟩ := q.2
    exact ⟨u ++ v, draw_app hu hv, topWidth_append_of wu wv⟩⟩

instance : SmallCategory HandObj where
  Hom := HandProgram
  id _ := ⟨[], [], rfl, rfl⟩
  comp p q := HandProgram.comp p q
  id_comp _ := Subtype.ext (List.nil_append _)
  comp_id _ := Subtype.ext (List.append_nil _)
  assoc _ _ _ := Subtype.ext (List.append_assoc _ _ _)

def HandProgram.mk {s t : State} (p : Program) (u : MWord) (h : draw s p = some (t, u))
    (w : WF s.width t.width u) : (⟨s⟩ : HandObj) ⟶ ⟨t⟩ :=
  ⟨p, u, h, w⟩
end Hand